nvdimm.txt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. LIBNVDIMM: Non-Volatile Devices
  2. libnvdimm - kernel / libndctl - userspace helper library
  3. linux-nvdimm@lists.01.org
  4. v13
  5. Glossary
  6. Overview
  7. Supporting Documents
  8. Git Trees
  9. LIBNVDIMM PMEM and BLK
  10. Why BLK?
  11. PMEM vs BLK
  12. BLK-REGIONs, PMEM-REGIONs, Atomic Sectors, and DAX
  13. Example NVDIMM Platform
  14. LIBNVDIMM Kernel Device Model and LIBNDCTL Userspace API
  15. LIBNDCTL: Context
  16. libndctl: instantiate a new library context example
  17. LIBNVDIMM/LIBNDCTL: Bus
  18. libnvdimm: control class device in /sys/class
  19. libnvdimm: bus
  20. libndctl: bus enumeration example
  21. LIBNVDIMM/LIBNDCTL: DIMM (NMEM)
  22. libnvdimm: DIMM (NMEM)
  23. libndctl: DIMM enumeration example
  24. LIBNVDIMM/LIBNDCTL: Region
  25. libnvdimm: region
  26. libndctl: region enumeration example
  27. Why Not Encode the Region Type into the Region Name?
  28. How Do I Determine the Major Type of a Region?
  29. LIBNVDIMM/LIBNDCTL: Namespace
  30. libnvdimm: namespace
  31. libndctl: namespace enumeration example
  32. libndctl: namespace creation example
  33. Why the Term "namespace"?
  34. LIBNVDIMM/LIBNDCTL: Block Translation Table "btt"
  35. libnvdimm: btt layout
  36. libndctl: btt creation example
  37. Summary LIBNDCTL Diagram
  38. Glossary
  39. --------
  40. PMEM: A system-physical-address range where writes are persistent. A
  41. block device composed of PMEM is capable of DAX. A PMEM address range
  42. may span an interleave of several DIMMs.
  43. BLK: A set of one or more programmable memory mapped apertures provided
  44. by a DIMM to access its media. This indirection precludes the
  45. performance benefit of interleaving, but enables DIMM-bounded failure
  46. modes.
  47. DPA: DIMM Physical Address, is a DIMM-relative offset. With one DIMM in
  48. the system there would be a 1:1 system-physical-address:DPA association.
  49. Once more DIMMs are added a memory controller interleave must be
  50. decoded to determine the DPA associated with a given
  51. system-physical-address. BLK capacity always has a 1:1 relationship
  52. with a single-DIMM's DPA range.
  53. DAX: File system extensions to bypass the page cache and block layer to
  54. mmap persistent memory, from a PMEM block device, directly into a
  55. process address space.
  56. DSM: Device Specific Method: ACPI method to to control specific
  57. device - in this case the firmware.
  58. DCR: NVDIMM Control Region Structure defined in ACPI 6 Section 5.2.25.5.
  59. It defines a vendor-id, device-id, and interface format for a given DIMM.
  60. BTT: Block Translation Table: Persistent memory is byte addressable.
  61. Existing software may have an expectation that the power-fail-atomicity
  62. of writes is at least one sector, 512 bytes. The BTT is an indirection
  63. table with atomic update semantics to front a PMEM/BLK block device
  64. driver and present arbitrary atomic sector sizes.
  65. LABEL: Metadata stored on a DIMM device that partitions and identifies
  66. (persistently names) storage between PMEM and BLK. It also partitions
  67. BLK storage to host BTTs with different parameters per BLK-partition.
  68. Note that traditional partition tables, GPT/MBR, are layered on top of a
  69. BLK or PMEM device.
  70. Overview
  71. --------
  72. The LIBNVDIMM subsystem provides support for three types of NVDIMMs, namely,
  73. PMEM, BLK, and NVDIMM devices that can simultaneously support both PMEM
  74. and BLK mode access. These three modes of operation are described by
  75. the "NVDIMM Firmware Interface Table" (NFIT) in ACPI 6. While the LIBNVDIMM
  76. implementation is generic and supports pre-NFIT platforms, it was guided
  77. by the superset of capabilities need to support this ACPI 6 definition
  78. for NVDIMM resources. The bulk of the kernel implementation is in place
  79. to handle the case where DPA accessible via PMEM is aliased with DPA
  80. accessible via BLK. When that occurs a LABEL is needed to reserve DPA
  81. for exclusive access via one mode a time.
  82. Supporting Documents
  83. ACPI 6: http://www.uefi.org/sites/default/files/resources/ACPI_6.0.pdf
  84. NVDIMM Namespace: http://pmem.io/documents/NVDIMM_Namespace_Spec.pdf
  85. DSM Interface Example: http://pmem.io/documents/NVDIMM_DSM_Interface_Example.pdf
  86. Driver Writer's Guide: http://pmem.io/documents/NVDIMM_Driver_Writers_Guide.pdf
  87. Git Trees
  88. LIBNVDIMM: https://git.kernel.org/cgit/linux/kernel/git/djbw/nvdimm.git
  89. LIBNDCTL: https://github.com/pmem/ndctl.git
  90. PMEM: https://github.com/01org/prd
  91. LIBNVDIMM PMEM and BLK
  92. ------------------
  93. Prior to the arrival of the NFIT, non-volatile memory was described to a
  94. system in various ad-hoc ways. Usually only the bare minimum was
  95. provided, namely, a single system-physical-address range where writes
  96. are expected to be durable after a system power loss. Now, the NFIT
  97. specification standardizes not only the description of PMEM, but also
  98. BLK and platform message-passing entry points for control and
  99. configuration.
  100. For each NVDIMM access method (PMEM, BLK), LIBNVDIMM provides a block
  101. device driver:
  102. 1. PMEM (nd_pmem.ko): Drives a system-physical-address range. This
  103. range is contiguous in system memory and may be interleaved (hardware
  104. memory controller striped) across multiple DIMMs. When interleaved the
  105. platform may optionally provide details of which DIMMs are participating
  106. in the interleave.
  107. Note that while LIBNVDIMM describes system-physical-address ranges that may
  108. alias with BLK access as ND_NAMESPACE_PMEM ranges and those without
  109. alias as ND_NAMESPACE_IO ranges, to the nd_pmem driver there is no
  110. distinction. The different device-types are an implementation detail
  111. that userspace can exploit to implement policies like "only interface
  112. with address ranges from certain DIMMs". It is worth noting that when
  113. aliasing is present and a DIMM lacks a label, then no block device can
  114. be created by default as userspace needs to do at least one allocation
  115. of DPA to the PMEM range. In contrast ND_NAMESPACE_IO ranges, once
  116. registered, can be immediately attached to nd_pmem.
  117. 2. BLK (nd_blk.ko): This driver performs I/O using a set of platform
  118. defined apertures. A set of apertures will access just one DIMM.
  119. Multiple windows (apertures) allow multiple concurrent accesses, much like
  120. tagged-command-queuing, and would likely be used by different threads or
  121. different CPUs.
  122. The NFIT specification defines a standard format for a BLK-aperture, but
  123. the spec also allows for vendor specific layouts, and non-NFIT BLK
  124. implementations may have other designs for BLK I/O. For this reason
  125. "nd_blk" calls back into platform-specific code to perform the I/O.
  126. One such implementation is defined in the "Driver Writer's Guide" and "DSM
  127. Interface Example".
  128. Why BLK?
  129. --------
  130. While PMEM provides direct byte-addressable CPU-load/store access to
  131. NVDIMM storage, it does not provide the best system RAS (recovery,
  132. availability, and serviceability) model. An access to a corrupted
  133. system-physical-address address causes a CPU exception while an access
  134. to a corrupted address through an BLK-aperture causes that block window
  135. to raise an error status in a register. The latter is more aligned with
  136. the standard error model that host-bus-adapter attached disks present.
  137. Also, if an administrator ever wants to replace a memory it is easier to
  138. service a system at DIMM module boundaries. Compare this to PMEM where
  139. data could be interleaved in an opaque hardware specific manner across
  140. several DIMMs.
  141. PMEM vs BLK
  142. BLK-apertures solve these RAS problems, but their presence is also the
  143. major contributing factor to the complexity of the ND subsystem. They
  144. complicate the implementation because PMEM and BLK alias in DPA space.
  145. Any given DIMM's DPA-range may contribute to one or more
  146. system-physical-address sets of interleaved DIMMs, *and* may also be
  147. accessed in its entirety through its BLK-aperture. Accessing a DPA
  148. through a system-physical-address while simultaneously accessing the
  149. same DPA through a BLK-aperture has undefined results. For this reason,
  150. DIMMs with this dual interface configuration include a DSM function to
  151. store/retrieve a LABEL. The LABEL effectively partitions the DPA-space
  152. into exclusive system-physical-address and BLK-aperture accessible
  153. regions. For simplicity a DIMM is allowed a PMEM "region" per each
  154. interleave set in which it is a member. The remaining DPA space can be
  155. carved into an arbitrary number of BLK devices with discontiguous
  156. extents.
  157. BLK-REGIONs, PMEM-REGIONs, Atomic Sectors, and DAX
  158. --------------------------------------------------
  159. One of the few
  160. reasons to allow multiple BLK namespaces per REGION is so that each
  161. BLK-namespace can be configured with a BTT with unique atomic sector
  162. sizes. While a PMEM device can host a BTT the LABEL specification does
  163. not provide for a sector size to be specified for a PMEM namespace.
  164. This is due to the expectation that the primary usage model for PMEM is
  165. via DAX, and the BTT is incompatible with DAX. However, for the cases
  166. where an application or filesystem still needs atomic sector update
  167. guarantees it can register a BTT on a PMEM device or partition. See
  168. LIBNVDIMM/NDCTL: Block Translation Table "btt"
  169. Example NVDIMM Platform
  170. -----------------------
  171. For the remainder of this document the following diagram will be
  172. referenced for any example sysfs layouts.
  173. (a) (b) DIMM BLK-REGION
  174. +-------------------+--------+--------+--------+
  175. +------+ | pm0.0 | blk2.0 | pm1.0 | blk2.1 | 0 region2
  176. | imc0 +--+- - - region0- - - +--------+ +--------+
  177. +--+---+ | pm0.0 | blk3.0 | pm1.0 | blk3.1 | 1 region3
  178. | +-------------------+--------v v--------+
  179. +--+---+ | |
  180. | cpu0 | region1
  181. +--+---+ | |
  182. | +----------------------------^ ^--------+
  183. +--+---+ | blk4.0 | pm1.0 | blk4.0 | 2 region4
  184. | imc1 +--+----------------------------| +--------+
  185. +------+ | blk5.0 | pm1.0 | blk5.0 | 3 region5
  186. +----------------------------+--------+--------+
  187. In this platform we have four DIMMs and two memory controllers in one
  188. socket. Each unique interface (BLK or PMEM) to DPA space is identified
  189. by a region device with a dynamically assigned id (REGION0 - REGION5).
  190. 1. The first portion of DIMM0 and DIMM1 are interleaved as REGION0. A
  191. single PMEM namespace is created in the REGION0-SPA-range that spans most
  192. of DIMM0 and DIMM1 with a user-specified name of "pm0.0". Some of that
  193. interleaved system-physical-address range is reclaimed as BLK-aperture
  194. accessed space starting at DPA-offset (a) into each DIMM. In that
  195. reclaimed space we create two BLK-aperture "namespaces" from REGION2 and
  196. REGION3 where "blk2.0" and "blk3.0" are just human readable names that
  197. could be set to any user-desired name in the LABEL.
  198. 2. In the last portion of DIMM0 and DIMM1 we have an interleaved
  199. system-physical-address range, REGION1, that spans those two DIMMs as
  200. well as DIMM2 and DIMM3. Some of REGION1 is allocated to a PMEM namespace
  201. named "pm1.0", the rest is reclaimed in 4 BLK-aperture namespaces (for
  202. each DIMM in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
  203. "blk5.0".
  204. 3. The portion of DIMM2 and DIMM3 that do not participate in the REGION1
  205. interleaved system-physical-address range (i.e. the DPA address past
  206. offset (b) are also included in the "blk4.0" and "blk5.0" namespaces.
  207. Note, that this example shows that BLK-aperture namespaces don't need to
  208. be contiguous in DPA-space.
  209. This bus is provided by the kernel under the device
  210. /sys/devices/platform/nfit_test.0 when CONFIG_NFIT_TEST is enabled and
  211. the nfit_test.ko module is loaded. This not only test LIBNVDIMM but the
  212. acpi_nfit.ko driver as well.
  213. LIBNVDIMM Kernel Device Model and LIBNDCTL Userspace API
  214. ----------------------------------------------------
  215. What follows is a description of the LIBNVDIMM sysfs layout and a
  216. corresponding object hierarchy diagram as viewed through the LIBNDCTL
  217. API. The example sysfs paths and diagrams are relative to the Example
  218. NVDIMM Platform which is also the LIBNVDIMM bus used in the LIBNDCTL unit
  219. test.
  220. LIBNDCTL: Context
  221. Every API call in the LIBNDCTL library requires a context that holds the
  222. logging parameters and other library instance state. The library is
  223. based on the libabc template:
  224. https://git.kernel.org/cgit/linux/kernel/git/kay/libabc.git
  225. LIBNDCTL: instantiate a new library context example
  226. struct ndctl_ctx *ctx;
  227. if (ndctl_new(&ctx) == 0)
  228. return ctx;
  229. else
  230. return NULL;
  231. LIBNVDIMM/LIBNDCTL: Bus
  232. -------------------
  233. A bus has a 1:1 relationship with an NFIT. The current expectation for
  234. ACPI based systems is that there is only ever one platform-global NFIT.
  235. That said, it is trivial to register multiple NFITs, the specification
  236. does not preclude it. The infrastructure supports multiple busses and
  237. we we use this capability to test multiple NFIT configurations in the
  238. unit test.
  239. LIBNVDIMM: control class device in /sys/class
  240. This character device accepts DSM messages to be passed to DIMM
  241. identified by its NFIT handle.
  242. /sys/class/nd/ndctl0
  243. |-- dev
  244. |-- device -> ../../../ndbus0
  245. |-- subsystem -> ../../../../../../../class/nd
  246. LIBNVDIMM: bus
  247. struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
  248. struct nvdimm_bus_descriptor *nfit_desc);
  249. /sys/devices/platform/nfit_test.0/ndbus0
  250. |-- commands
  251. |-- nd
  252. |-- nfit
  253. |-- nmem0
  254. |-- nmem1
  255. |-- nmem2
  256. |-- nmem3
  257. |-- power
  258. |-- provider
  259. |-- region0
  260. |-- region1
  261. |-- region2
  262. |-- region3
  263. |-- region4
  264. |-- region5
  265. |-- uevent
  266. `-- wait_probe
  267. LIBNDCTL: bus enumeration example
  268. Find the bus handle that describes the bus from Example NVDIMM Platform
  269. static struct ndctl_bus *get_bus_by_provider(struct ndctl_ctx *ctx,
  270. const char *provider)
  271. {
  272. struct ndctl_bus *bus;
  273. ndctl_bus_foreach(ctx, bus)
  274. if (strcmp(provider, ndctl_bus_get_provider(bus)) == 0)
  275. return bus;
  276. return NULL;
  277. }
  278. bus = get_bus_by_provider(ctx, "nfit_test.0");
  279. LIBNVDIMM/LIBNDCTL: DIMM (NMEM)
  280. ---------------------------
  281. The DIMM device provides a character device for sending commands to
  282. hardware, and it is a container for LABELs. If the DIMM is defined by
  283. NFIT then an optional 'nfit' attribute sub-directory is available to add
  284. NFIT-specifics.
  285. Note that the kernel device name for "DIMMs" is "nmemX". The NFIT
  286. describes these devices via "Memory Device to System Physical Address
  287. Range Mapping Structure", and there is no requirement that they actually
  288. be physical DIMMs, so we use a more generic name.
  289. LIBNVDIMM: DIMM (NMEM)
  290. struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
  291. const struct attribute_group **groups, unsigned long flags,
  292. unsigned long *dsm_mask);
  293. /sys/devices/platform/nfit_test.0/ndbus0
  294. |-- nmem0
  295. | |-- available_slots
  296. | |-- commands
  297. | |-- dev
  298. | |-- devtype
  299. | |-- driver -> ../../../../../bus/nd/drivers/nvdimm
  300. | |-- modalias
  301. | |-- nfit
  302. | | |-- device
  303. | | |-- format
  304. | | |-- handle
  305. | | |-- phys_id
  306. | | |-- rev_id
  307. | | |-- serial
  308. | | `-- vendor
  309. | |-- state
  310. | |-- subsystem -> ../../../../../bus/nd
  311. | `-- uevent
  312. |-- nmem1
  313. [..]
  314. LIBNDCTL: DIMM enumeration example
  315. Note, in this example we are assuming NFIT-defined DIMMs which are
  316. identified by an "nfit_handle" a 32-bit value where:
  317. Bit 3:0 DIMM number within the memory channel
  318. Bit 7:4 memory channel number
  319. Bit 11:8 memory controller ID
  320. Bit 15:12 socket ID (within scope of a Node controller if node controller is present)
  321. Bit 27:16 Node Controller ID
  322. Bit 31:28 Reserved
  323. static struct ndctl_dimm *get_dimm_by_handle(struct ndctl_bus *bus,
  324. unsigned int handle)
  325. {
  326. struct ndctl_dimm *dimm;
  327. ndctl_dimm_foreach(bus, dimm)
  328. if (ndctl_dimm_get_handle(dimm) == handle)
  329. return dimm;
  330. return NULL;
  331. }
  332. #define DIMM_HANDLE(n, s, i, c, d) \
  333. (((n & 0xfff) << 16) | ((s & 0xf) << 12) | ((i & 0xf) << 8) \
  334. | ((c & 0xf) << 4) | (d & 0xf))
  335. dimm = get_dimm_by_handle(bus, DIMM_HANDLE(0, 0, 0, 0, 0));
  336. LIBNVDIMM/LIBNDCTL: Region
  337. ----------------------
  338. A generic REGION device is registered for each PMEM range or BLK-aperture
  339. set. Per the example there are 6 regions: 2 PMEM and 4 BLK-aperture
  340. sets on the "nfit_test.0" bus. The primary role of regions are to be a
  341. container of "mappings". A mapping is a tuple of <DIMM,
  342. DPA-start-offset, length>.
  343. LIBNVDIMM provides a built-in driver for these REGION devices. This driver
  344. is responsible for reconciling the aliased DPA mappings across all
  345. regions, parsing the LABEL, if present, and then emitting NAMESPACE
  346. devices with the resolved/exclusive DPA-boundaries for the nd_pmem or
  347. nd_blk device driver to consume.
  348. In addition to the generic attributes of "mapping"s, "interleave_ways"
  349. and "size" the REGION device also exports some convenience attributes.
  350. "nstype" indicates the integer type of namespace-device this region
  351. emits, "devtype" duplicates the DEVTYPE variable stored by udev at the
  352. 'add' event, "modalias" duplicates the MODALIAS variable stored by udev
  353. at the 'add' event, and finally, the optional "spa_index" is provided in
  354. the case where the region is defined by a SPA.
  355. LIBNVDIMM: region
  356. struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
  357. struct nd_region_desc *ndr_desc);
  358. struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
  359. struct nd_region_desc *ndr_desc);
  360. /sys/devices/platform/nfit_test.0/ndbus0
  361. |-- region0
  362. | |-- available_size
  363. | |-- btt0
  364. | |-- btt_seed
  365. | |-- devtype
  366. | |-- driver -> ../../../../../bus/nd/drivers/nd_region
  367. | |-- init_namespaces
  368. | |-- mapping0
  369. | |-- mapping1
  370. | |-- mappings
  371. | |-- modalias
  372. | |-- namespace0.0
  373. | |-- namespace_seed
  374. | |-- numa_node
  375. | |-- nfit
  376. | | `-- spa_index
  377. | |-- nstype
  378. | |-- set_cookie
  379. | |-- size
  380. | |-- subsystem -> ../../../../../bus/nd
  381. | `-- uevent
  382. |-- region1
  383. [..]
  384. LIBNDCTL: region enumeration example
  385. Sample region retrieval routines based on NFIT-unique data like
  386. "spa_index" (interleave set id) for PMEM and "nfit_handle" (dimm id) for
  387. BLK.
  388. static struct ndctl_region *get_pmem_region_by_spa_index(struct ndctl_bus *bus,
  389. unsigned int spa_index)
  390. {
  391. struct ndctl_region *region;
  392. ndctl_region_foreach(bus, region) {
  393. if (ndctl_region_get_type(region) != ND_DEVICE_REGION_PMEM)
  394. continue;
  395. if (ndctl_region_get_spa_index(region) == spa_index)
  396. return region;
  397. }
  398. return NULL;
  399. }
  400. static struct ndctl_region *get_blk_region_by_dimm_handle(struct ndctl_bus *bus,
  401. unsigned int handle)
  402. {
  403. struct ndctl_region *region;
  404. ndctl_region_foreach(bus, region) {
  405. struct ndctl_mapping *map;
  406. if (ndctl_region_get_type(region) != ND_DEVICE_REGION_BLOCK)
  407. continue;
  408. ndctl_mapping_foreach(region, map) {
  409. struct ndctl_dimm *dimm = ndctl_mapping_get_dimm(map);
  410. if (ndctl_dimm_get_handle(dimm) == handle)
  411. return region;
  412. }
  413. }
  414. return NULL;
  415. }
  416. Why Not Encode the Region Type into the Region Name?
  417. ----------------------------------------------------
  418. At first glance it seems since NFIT defines just PMEM and BLK interface
  419. types that we should simply name REGION devices with something derived
  420. from those type names. However, the ND subsystem explicitly keeps the
  421. REGION name generic and expects userspace to always consider the
  422. region-attributes for four reasons:
  423. 1. There are already more than two REGION and "namespace" types. For
  424. PMEM there are two subtypes. As mentioned previously we have PMEM where
  425. the constituent DIMM devices are known and anonymous PMEM. For BLK
  426. regions the NFIT specification already anticipates vendor specific
  427. implementations. The exact distinction of what a region contains is in
  428. the region-attributes not the region-name or the region-devtype.
  429. 2. A region with zero child-namespaces is a possible configuration. For
  430. example, the NFIT allows for a DCR to be published without a
  431. corresponding BLK-aperture. This equates to a DIMM that can only accept
  432. control/configuration messages, but no i/o through a descendant block
  433. device. Again, this "type" is advertised in the attributes ('mappings'
  434. == 0) and the name does not tell you much.
  435. 3. What if a third major interface type arises in the future? Outside
  436. of vendor specific implementations, it's not difficult to envision a
  437. third class of interface type beyond BLK and PMEM. With a generic name
  438. for the REGION level of the device-hierarchy old userspace
  439. implementations can still make sense of new kernel advertised
  440. region-types. Userspace can always rely on the generic region
  441. attributes like "mappings", "size", etc and the expected child devices
  442. named "namespace". This generic format of the device-model hierarchy
  443. allows the LIBNVDIMM and LIBNDCTL implementations to be more uniform and
  444. future-proof.
  445. 4. There are more robust mechanisms for determining the major type of a
  446. region than a device name. See the next section, How Do I Determine the
  447. Major Type of a Region?
  448. How Do I Determine the Major Type of a Region?
  449. ----------------------------------------------
  450. Outside of the blanket recommendation of "use libndctl", or simply
  451. looking at the kernel header (/usr/include/linux/ndctl.h) to decode the
  452. "nstype" integer attribute, here are some other options.
  453. 1. module alias lookup:
  454. The whole point of region/namespace device type differentiation is to
  455. decide which block-device driver will attach to a given LIBNVDIMM namespace.
  456. One can simply use the modalias to lookup the resulting module. It's
  457. important to note that this method is robust in the presence of a
  458. vendor-specific driver down the road. If a vendor-specific
  459. implementation wants to supplant the standard nd_blk driver it can with
  460. minimal impact to the rest of LIBNVDIMM.
  461. In fact, a vendor may also want to have a vendor-specific region-driver
  462. (outside of nd_region). For example, if a vendor defined its own LABEL
  463. format it would need its own region driver to parse that LABEL and emit
  464. the resulting namespaces. The output from module resolution is more
  465. accurate than a region-name or region-devtype.
  466. 2. udev:
  467. The kernel "devtype" is registered in the udev database
  468. # udevadm info --path=/devices/platform/nfit_test.0/ndbus0/region0
  469. P: /devices/platform/nfit_test.0/ndbus0/region0
  470. E: DEVPATH=/devices/platform/nfit_test.0/ndbus0/region0
  471. E: DEVTYPE=nd_pmem
  472. E: MODALIAS=nd:t2
  473. E: SUBSYSTEM=nd
  474. # udevadm info --path=/devices/platform/nfit_test.0/ndbus0/region4
  475. P: /devices/platform/nfit_test.0/ndbus0/region4
  476. E: DEVPATH=/devices/platform/nfit_test.0/ndbus0/region4
  477. E: DEVTYPE=nd_blk
  478. E: MODALIAS=nd:t3
  479. E: SUBSYSTEM=nd
  480. ...and is available as a region attribute, but keep in mind that the
  481. "devtype" does not indicate sub-type variations and scripts should
  482. really be understanding the other attributes.
  483. 3. type specific attributes:
  484. As it currently stands a BLK-aperture region will never have a
  485. "nfit/spa_index" attribute, but neither will a non-NFIT PMEM region. A
  486. BLK region with a "mappings" value of 0 is, as mentioned above, a DIMM
  487. that does not allow I/O. A PMEM region with a "mappings" value of zero
  488. is a simple system-physical-address range.
  489. LIBNVDIMM/LIBNDCTL: Namespace
  490. -------------------------
  491. A REGION, after resolving DPA aliasing and LABEL specified boundaries,
  492. surfaces one or more "namespace" devices. The arrival of a "namespace"
  493. device currently triggers either the nd_blk or nd_pmem driver to load
  494. and register a disk/block device.
  495. LIBNVDIMM: namespace
  496. Here is a sample layout from the three major types of NAMESPACE where
  497. namespace0.0 represents DIMM-info-backed PMEM (note that it has a 'uuid'
  498. attribute), namespace2.0 represents a BLK namespace (note it has a
  499. 'sector_size' attribute) that, and namespace6.0 represents an anonymous
  500. PMEM namespace (note that has no 'uuid' attribute due to not support a
  501. LABEL).
  502. /sys/devices/platform/nfit_test.0/ndbus0/region0/namespace0.0
  503. |-- alt_name
  504. |-- devtype
  505. |-- dpa_extents
  506. |-- force_raw
  507. |-- modalias
  508. |-- numa_node
  509. |-- resource
  510. |-- size
  511. |-- subsystem -> ../../../../../../bus/nd
  512. |-- type
  513. |-- uevent
  514. `-- uuid
  515. /sys/devices/platform/nfit_test.0/ndbus0/region2/namespace2.0
  516. |-- alt_name
  517. |-- devtype
  518. |-- dpa_extents
  519. |-- force_raw
  520. |-- modalias
  521. |-- numa_node
  522. |-- sector_size
  523. |-- size
  524. |-- subsystem -> ../../../../../../bus/nd
  525. |-- type
  526. |-- uevent
  527. `-- uuid
  528. /sys/devices/platform/nfit_test.1/ndbus1/region6/namespace6.0
  529. |-- block
  530. | `-- pmem0
  531. |-- devtype
  532. |-- driver -> ../../../../../../bus/nd/drivers/pmem
  533. |-- force_raw
  534. |-- modalias
  535. |-- numa_node
  536. |-- resource
  537. |-- size
  538. |-- subsystem -> ../../../../../../bus/nd
  539. |-- type
  540. `-- uevent
  541. LIBNDCTL: namespace enumeration example
  542. Namespaces are indexed relative to their parent region, example below.
  543. These indexes are mostly static from boot to boot, but subsystem makes
  544. no guarantees in this regard. For a static namespace identifier use its
  545. 'uuid' attribute.
  546. static struct ndctl_namespace *get_namespace_by_id(struct ndctl_region *region,
  547. unsigned int id)
  548. {
  549. struct ndctl_namespace *ndns;
  550. ndctl_namespace_foreach(region, ndns)
  551. if (ndctl_namespace_get_id(ndns) == id)
  552. return ndns;
  553. return NULL;
  554. }
  555. LIBNDCTL: namespace creation example
  556. Idle namespaces are automatically created by the kernel if a given
  557. region has enough available capacity to create a new namespace.
  558. Namespace instantiation involves finding an idle namespace and
  559. configuring it. For the most part the setting of namespace attributes
  560. can occur in any order, the only constraint is that 'uuid' must be set
  561. before 'size'. This enables the kernel to track DPA allocations
  562. internally with a static identifier.
  563. static int configure_namespace(struct ndctl_region *region,
  564. struct ndctl_namespace *ndns,
  565. struct namespace_parameters *parameters)
  566. {
  567. char devname[50];
  568. snprintf(devname, sizeof(devname), "namespace%d.%d",
  569. ndctl_region_get_id(region), paramaters->id);
  570. ndctl_namespace_set_alt_name(ndns, devname);
  571. /* 'uuid' must be set prior to setting size! */
  572. ndctl_namespace_set_uuid(ndns, paramaters->uuid);
  573. ndctl_namespace_set_size(ndns, paramaters->size);
  574. /* unlike pmem namespaces, blk namespaces have a sector size */
  575. if (parameters->lbasize)
  576. ndctl_namespace_set_sector_size(ndns, parameters->lbasize);
  577. ndctl_namespace_enable(ndns);
  578. }
  579. Why the Term "namespace"?
  580. 1. Why not "volume" for instance? "volume" ran the risk of confusing
  581. ND (libnvdimm subsystem) to a volume manager like device-mapper.
  582. 2. The term originated to describe the sub-devices that can be created
  583. within a NVME controller (see the nvme specification:
  584. http://www.nvmexpress.org/specifications/), and NFIT namespaces are
  585. meant to parallel the capabilities and configurability of
  586. NVME-namespaces.
  587. LIBNVDIMM/LIBNDCTL: Block Translation Table "btt"
  588. ---------------------------------------------
  589. A BTT (design document: http://pmem.io/2014/09/23/btt.html) is a stacked
  590. block device driver that fronts either the whole block device or a
  591. partition of a block device emitted by either a PMEM or BLK NAMESPACE.
  592. LIBNVDIMM: btt layout
  593. Every region will start out with at least one BTT device which is the
  594. seed device. To activate it set the "namespace", "uuid", and
  595. "sector_size" attributes and then bind the device to the nd_pmem or
  596. nd_blk driver depending on the region type.
  597. /sys/devices/platform/nfit_test.1/ndbus0/region0/btt0/
  598. |-- namespace
  599. |-- delete
  600. |-- devtype
  601. |-- modalias
  602. |-- numa_node
  603. |-- sector_size
  604. |-- subsystem -> ../../../../../bus/nd
  605. |-- uevent
  606. `-- uuid
  607. LIBNDCTL: btt creation example
  608. Similar to namespaces an idle BTT device is automatically created per
  609. region. Each time this "seed" btt device is configured and enabled a new
  610. seed is created. Creating a BTT configuration involves two steps of
  611. finding and idle BTT and assigning it to consume a PMEM or BLK namespace.
  612. static struct ndctl_btt *get_idle_btt(struct ndctl_region *region)
  613. {
  614. struct ndctl_btt *btt;
  615. ndctl_btt_foreach(region, btt)
  616. if (!ndctl_btt_is_enabled(btt)
  617. && !ndctl_btt_is_configured(btt))
  618. return btt;
  619. return NULL;
  620. }
  621. static int configure_btt(struct ndctl_region *region,
  622. struct btt_parameters *parameters)
  623. {
  624. btt = get_idle_btt(region);
  625. ndctl_btt_set_uuid(btt, parameters->uuid);
  626. ndctl_btt_set_sector_size(btt, parameters->sector_size);
  627. ndctl_btt_set_namespace(btt, parameters->ndns);
  628. /* turn off raw mode device */
  629. ndctl_namespace_disable(parameters->ndns);
  630. /* turn on btt access */
  631. ndctl_btt_enable(btt);
  632. }
  633. Once instantiated a new inactive btt seed device will appear underneath
  634. the region.
  635. Once a "namespace" is removed from a BTT that instance of the BTT device
  636. will be deleted or otherwise reset to default values. This deletion is
  637. only at the device model level. In order to destroy a BTT the "info
  638. block" needs to be destroyed. Note, that to destroy a BTT the media
  639. needs to be written in raw mode. By default, the kernel will autodetect
  640. the presence of a BTT and disable raw mode. This autodetect behavior
  641. can be suppressed by enabling raw mode for the namespace via the
  642. ndctl_namespace_set_raw_mode() API.
  643. Summary LIBNDCTL Diagram
  644. ------------------------
  645. For the given example above, here is the view of the objects as seen by the
  646. LIBNDCTL API:
  647. +---+
  648. |CTX| +---------+ +--------------+ +---------------+
  649. +-+-+ +-> REGION0 +---> NAMESPACE0.0 +--> PMEM8 "pm0.0" |
  650. | | +---------+ +--------------+ +---------------+
  651. +-------+ | | +---------+ +--------------+ +---------------+
  652. | DIMM0 <-+ | +-> REGION1 +---> NAMESPACE1.0 +--> PMEM6 "pm1.0" |
  653. +-------+ | | | +---------+ +--------------+ +---------------+
  654. | DIMM1 <-+ +-v--+ | +---------+ +--------------+ +---------------+
  655. +-------+ +-+BUS0+---> REGION2 +-+-> NAMESPACE2.0 +--> ND6 "blk2.0" |
  656. | DIMM2 <-+ +----+ | +---------+ | +--------------+ +----------------------+
  657. +-------+ | | +-> NAMESPACE2.1 +--> ND5 "blk2.1" | BTT2 |
  658. | DIMM3 <-+ | +--------------+ +----------------------+
  659. +-------+ | +---------+ +--------------+ +---------------+
  660. +-> REGION3 +-+-> NAMESPACE3.0 +--> ND4 "blk3.0" |
  661. | +---------+ | +--------------+ +----------------------+
  662. | +-> NAMESPACE3.1 +--> ND3 "blk3.1" | BTT1 |
  663. | +--------------+ +----------------------+
  664. | +---------+ +--------------+ +---------------+
  665. +-> REGION4 +---> NAMESPACE4.0 +--> ND2 "blk4.0" |
  666. | +---------+ +--------------+ +---------------+
  667. | +---------+ +--------------+ +----------------------+
  668. +-> REGION5 +---> NAMESPACE5.0 +--> ND1 "blk5.0" | BTT0 |
  669. +---------+ +--------------+ +---------------+------+