Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
0004  */
0005 #ifndef __LINUX_ND_H__
0006 #define __LINUX_ND_H__
0007 #include <linux/fs.h>
0008 #include <linux/ndctl.h>
0009 #include <linux/device.h>
0010 #include <linux/badblocks.h>
0011 #include <linux/perf_event.h>
0012 
0013 enum nvdimm_event {
0014     NVDIMM_REVALIDATE_POISON,
0015     NVDIMM_REVALIDATE_REGION,
0016 };
0017 
0018 enum nvdimm_claim_class {
0019     NVDIMM_CCLASS_NONE,
0020     NVDIMM_CCLASS_BTT,
0021     NVDIMM_CCLASS_BTT2,
0022     NVDIMM_CCLASS_PFN,
0023     NVDIMM_CCLASS_DAX,
0024     NVDIMM_CCLASS_UNKNOWN,
0025 };
0026 
0027 #define NVDIMM_EVENT_VAR(_id)  event_attr_##_id
0028 #define NVDIMM_EVENT_PTR(_id)  (&event_attr_##_id.attr.attr)
0029 
0030 #define NVDIMM_EVENT_ATTR(_name, _id)               \
0031     PMU_EVENT_ATTR(_name, NVDIMM_EVENT_VAR(_id), _id,   \
0032             nvdimm_events_sysfs_show)
0033 
0034 /* Event attribute array index */
0035 #define NVDIMM_PMU_FORMAT_ATTR  0
0036 #define NVDIMM_PMU_EVENT_ATTR   1
0037 #define NVDIMM_PMU_CPUMASK_ATTR 2
0038 #define NVDIMM_PMU_NULL_ATTR    3
0039 
0040 /**
0041  * struct nvdimm_pmu - data structure for nvdimm perf driver
0042  * @pmu: pmu data structure for nvdimm performance stats.
0043  * @dev: nvdimm device pointer.
0044  * @cpu: designated cpu for counter access.
0045  * @node: node for cpu hotplug notifier link.
0046  * @cpuhp_state: state for cpu hotplug notification.
0047  * @arch_cpumask: cpumask to get designated cpu for counter access.
0048  */
0049 struct nvdimm_pmu {
0050     struct pmu pmu;
0051     struct device *dev;
0052     int cpu;
0053     struct hlist_node node;
0054     enum cpuhp_state cpuhp_state;
0055     /* cpumask provided by arch/platform specific code */
0056     struct cpumask arch_cpumask;
0057 };
0058 
0059 struct platform_device;
0060 
0061 #ifdef CONFIG_PERF_EVENTS
0062 extern ssize_t nvdimm_events_sysfs_show(struct device *dev,
0063                     struct device_attribute *attr,
0064                     char *page);
0065 
0066 int register_nvdimm_pmu(struct nvdimm_pmu *nvdimm, struct platform_device *pdev);
0067 void unregister_nvdimm_pmu(struct nvdimm_pmu *nd_pmu);
0068 
0069 #else
0070 static inline int register_nvdimm_pmu(struct nvdimm_pmu *nvdimm, struct platform_device *pdev)
0071 {
0072     return -ENXIO;
0073 }
0074 
0075 static inline void unregister_nvdimm_pmu(struct nvdimm_pmu *nd_pmu) { }
0076 #endif
0077 
0078 struct nd_device_driver {
0079     struct device_driver drv;
0080     unsigned long type;
0081     int (*probe)(struct device *dev);
0082     void (*remove)(struct device *dev);
0083     void (*shutdown)(struct device *dev);
0084     void (*notify)(struct device *dev, enum nvdimm_event event);
0085 };
0086 
0087 static inline struct nd_device_driver *to_nd_device_driver(
0088         struct device_driver *drv)
0089 {
0090     return container_of(drv, struct nd_device_driver, drv);
0091 };
0092 
0093 /**
0094  * struct nd_namespace_common - core infrastructure of a namespace
0095  * @force_raw: ignore other personalities for the namespace (e.g. btt)
0096  * @dev: device model node
0097  * @claim: when set a another personality has taken ownership of the namespace
0098  * @claim_class: restrict claim type to a given class
0099  * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
0100  */
0101 struct nd_namespace_common {
0102     int force_raw;
0103     struct device dev;
0104     struct device *claim;
0105     enum nvdimm_claim_class claim_class;
0106     int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
0107             void *buf, size_t size, int rw, unsigned long flags);
0108 };
0109 
0110 static inline struct nd_namespace_common *to_ndns(struct device *dev)
0111 {
0112     return container_of(dev, struct nd_namespace_common, dev);
0113 }
0114 
0115 /**
0116  * struct nd_namespace_io - device representation of a persistent memory range
0117  * @dev: namespace device created by the nd region driver
0118  * @res: struct resource conversion of a NFIT SPA table
0119  * @size: cached resource_size(@res) for fast path size checks
0120  * @addr: virtual address to access the namespace range
0121  * @bb: badblocks list for the namespace range
0122  */
0123 struct nd_namespace_io {
0124     struct nd_namespace_common common;
0125     struct resource res;
0126     resource_size_t size;
0127     void *addr;
0128     struct badblocks bb;
0129 };
0130 
0131 /**
0132  * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
0133  * @nsio: device and system physical address range to drive
0134  * @lbasize: logical sector size for the namespace in block-device-mode
0135  * @alt_name: namespace name supplied in the dimm label
0136  * @uuid: namespace name supplied in the dimm label
0137  * @id: ida allocated id
0138  */
0139 struct nd_namespace_pmem {
0140     struct nd_namespace_io nsio;
0141     unsigned long lbasize;
0142     char *alt_name;
0143     uuid_t *uuid;
0144     int id;
0145 };
0146 
0147 static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
0148 {
0149     return container_of(dev, struct nd_namespace_io, common.dev);
0150 }
0151 
0152 static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
0153 {
0154     struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
0155 
0156     return container_of(nsio, struct nd_namespace_pmem, nsio);
0157 }
0158 
0159 /**
0160  * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
0161  * @ndns: device to read
0162  * @offset: namespace-relative starting offset
0163  * @buf: buffer to fill
0164  * @size: transfer length
0165  *
0166  * @buf is up-to-date upon return from this routine.
0167  */
0168 static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
0169         resource_size_t offset, void *buf, size_t size,
0170         unsigned long flags)
0171 {
0172     return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
0173 }
0174 
0175 /**
0176  * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
0177  * @ndns: device to write
0178  * @offset: namespace-relative starting offset
0179  * @buf: buffer to drain
0180  * @size: transfer length
0181  *
0182  * NVDIMM Namepaces disks do not implement sectors internally.  Depending on
0183  * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
0184  * or on backing memory media upon return from this routine.  Flushing
0185  * to media is handled internal to the @ndns driver, if at all.
0186  */
0187 static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
0188         resource_size_t offset, void *buf, size_t size,
0189         unsigned long flags)
0190 {
0191     return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
0192 }
0193 
0194 #define MODULE_ALIAS_ND_DEVICE(type) \
0195     MODULE_ALIAS("nd:t" __stringify(type) "*")
0196 #define ND_DEVICE_MODALIAS_FMT "nd:t%d"
0197 
0198 struct nd_region;
0199 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
0200 int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
0201         struct module *module, const char *mod_name);
0202 static inline void nd_driver_unregister(struct nd_device_driver *drv)
0203 {
0204     driver_unregister(&drv->drv);
0205 }
0206 #define nd_driver_register(driver) \
0207     __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
0208 #define module_nd_driver(driver) \
0209     module_driver(driver, nd_driver_register, nd_driver_unregister)
0210 #endif /* __LINUX_ND_H__ */