nvdimm.rst 32 KB

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