sysfs.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <linux/kernel.h>
  2. #include <linux/lightnvm.h>
  3. #include <linux/miscdevice.h>
  4. #include <linux/kobject.h>
  5. #include <linux/blk-mq.h>
  6. #include "lightnvm.h"
  7. static ssize_t nvm_dev_attr_show(struct device *dev,
  8. struct device_attribute *dattr, char *page)
  9. {
  10. struct nvm_dev *ndev = container_of(dev, struct nvm_dev, dev);
  11. struct nvm_id *id = &ndev->identity;
  12. struct nvm_id_group *grp = &id->groups[0];
  13. struct attribute *attr = &dattr->attr;
  14. if (strcmp(attr->name, "version") == 0) {
  15. return scnprintf(page, PAGE_SIZE, "%u\n", id->ver_id);
  16. } else if (strcmp(attr->name, "vendor_opcode") == 0) {
  17. return scnprintf(page, PAGE_SIZE, "%u\n", id->vmnt);
  18. } else if (strcmp(attr->name, "capabilities") == 0) {
  19. return scnprintf(page, PAGE_SIZE, "%u\n", id->cap);
  20. } else if (strcmp(attr->name, "device_mode") == 0) {
  21. return scnprintf(page, PAGE_SIZE, "%u\n", id->dom);
  22. } else if (strcmp(attr->name, "media_manager") == 0) {
  23. if (!ndev->mt)
  24. return scnprintf(page, PAGE_SIZE, "%s\n", "none");
  25. return scnprintf(page, PAGE_SIZE, "%s\n", ndev->mt->name);
  26. } else if (strcmp(attr->name, "ppa_format") == 0) {
  27. return scnprintf(page, PAGE_SIZE,
  28. "0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  29. id->ppaf.ch_offset, id->ppaf.ch_len,
  30. id->ppaf.lun_offset, id->ppaf.lun_len,
  31. id->ppaf.pln_offset, id->ppaf.pln_len,
  32. id->ppaf.blk_offset, id->ppaf.blk_len,
  33. id->ppaf.pg_offset, id->ppaf.pg_len,
  34. id->ppaf.sect_offset, id->ppaf.sect_len);
  35. } else if (strcmp(attr->name, "media_type") == 0) { /* u8 */
  36. return scnprintf(page, PAGE_SIZE, "%u\n", grp->mtype);
  37. } else if (strcmp(attr->name, "flash_media_type") == 0) {
  38. return scnprintf(page, PAGE_SIZE, "%u\n", grp->fmtype);
  39. } else if (strcmp(attr->name, "num_channels") == 0) {
  40. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_ch);
  41. } else if (strcmp(attr->name, "num_luns") == 0) {
  42. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_lun);
  43. } else if (strcmp(attr->name, "num_planes") == 0) {
  44. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pln);
  45. } else if (strcmp(attr->name, "num_blocks") == 0) { /* u16 */
  46. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_blk);
  47. } else if (strcmp(attr->name, "num_pages") == 0) {
  48. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pg);
  49. } else if (strcmp(attr->name, "page_size") == 0) {
  50. return scnprintf(page, PAGE_SIZE, "%u\n", grp->fpg_sz);
  51. } else if (strcmp(attr->name, "hw_sector_size") == 0) {
  52. return scnprintf(page, PAGE_SIZE, "%u\n", grp->csecs);
  53. } else if (strcmp(attr->name, "oob_sector_size") == 0) {/* u32 */
  54. return scnprintf(page, PAGE_SIZE, "%u\n", grp->sos);
  55. } else if (strcmp(attr->name, "read_typ") == 0) {
  56. return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdt);
  57. } else if (strcmp(attr->name, "read_max") == 0) {
  58. return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdm);
  59. } else if (strcmp(attr->name, "prog_typ") == 0) {
  60. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprt);
  61. } else if (strcmp(attr->name, "prog_max") == 0) {
  62. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprm);
  63. } else if (strcmp(attr->name, "erase_typ") == 0) {
  64. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbet);
  65. } else if (strcmp(attr->name, "erase_max") == 0) {
  66. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbem);
  67. } else if (strcmp(attr->name, "multiplane_modes") == 0) {
  68. return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mpos);
  69. } else if (strcmp(attr->name, "media_capabilities") == 0) {
  70. return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mccap);
  71. } else if (strcmp(attr->name, "max_phys_secs") == 0) {
  72. return scnprintf(page, PAGE_SIZE, "%u\n",
  73. ndev->ops->max_phys_sect);
  74. } else {
  75. return scnprintf(page,
  76. PAGE_SIZE,
  77. "Unhandled attr(%s) in `nvm_dev_attr_show`\n",
  78. attr->name);
  79. }
  80. }
  81. #define NVM_DEV_ATTR_RO(_name) \
  82. DEVICE_ATTR(_name, S_IRUGO, nvm_dev_attr_show, NULL)
  83. static NVM_DEV_ATTR_RO(version);
  84. static NVM_DEV_ATTR_RO(vendor_opcode);
  85. static NVM_DEV_ATTR_RO(capabilities);
  86. static NVM_DEV_ATTR_RO(device_mode);
  87. static NVM_DEV_ATTR_RO(ppa_format);
  88. static NVM_DEV_ATTR_RO(media_manager);
  89. static NVM_DEV_ATTR_RO(media_type);
  90. static NVM_DEV_ATTR_RO(flash_media_type);
  91. static NVM_DEV_ATTR_RO(num_channels);
  92. static NVM_DEV_ATTR_RO(num_luns);
  93. static NVM_DEV_ATTR_RO(num_planes);
  94. static NVM_DEV_ATTR_RO(num_blocks);
  95. static NVM_DEV_ATTR_RO(num_pages);
  96. static NVM_DEV_ATTR_RO(page_size);
  97. static NVM_DEV_ATTR_RO(hw_sector_size);
  98. static NVM_DEV_ATTR_RO(oob_sector_size);
  99. static NVM_DEV_ATTR_RO(read_typ);
  100. static NVM_DEV_ATTR_RO(read_max);
  101. static NVM_DEV_ATTR_RO(prog_typ);
  102. static NVM_DEV_ATTR_RO(prog_max);
  103. static NVM_DEV_ATTR_RO(erase_typ);
  104. static NVM_DEV_ATTR_RO(erase_max);
  105. static NVM_DEV_ATTR_RO(multiplane_modes);
  106. static NVM_DEV_ATTR_RO(media_capabilities);
  107. static NVM_DEV_ATTR_RO(max_phys_secs);
  108. #define NVM_DEV_ATTR(_name) (dev_attr_##_name##)
  109. static struct attribute *nvm_dev_attrs[] = {
  110. &dev_attr_version.attr,
  111. &dev_attr_vendor_opcode.attr,
  112. &dev_attr_capabilities.attr,
  113. &dev_attr_device_mode.attr,
  114. &dev_attr_media_manager.attr,
  115. &dev_attr_ppa_format.attr,
  116. &dev_attr_media_type.attr,
  117. &dev_attr_flash_media_type.attr,
  118. &dev_attr_num_channels.attr,
  119. &dev_attr_num_luns.attr,
  120. &dev_attr_num_planes.attr,
  121. &dev_attr_num_blocks.attr,
  122. &dev_attr_num_pages.attr,
  123. &dev_attr_page_size.attr,
  124. &dev_attr_hw_sector_size.attr,
  125. &dev_attr_oob_sector_size.attr,
  126. &dev_attr_read_typ.attr,
  127. &dev_attr_read_max.attr,
  128. &dev_attr_prog_typ.attr,
  129. &dev_attr_prog_max.attr,
  130. &dev_attr_erase_typ.attr,
  131. &dev_attr_erase_max.attr,
  132. &dev_attr_multiplane_modes.attr,
  133. &dev_attr_media_capabilities.attr,
  134. &dev_attr_max_phys_secs.attr,
  135. NULL,
  136. };
  137. static struct attribute_group nvm_dev_attr_group = {
  138. .name = "lightnvm",
  139. .attrs = nvm_dev_attrs,
  140. };
  141. static const struct attribute_group *nvm_dev_attr_groups[] = {
  142. &nvm_dev_attr_group,
  143. NULL,
  144. };
  145. static void nvm_dev_release(struct device *device)
  146. {
  147. struct nvm_dev *dev = container_of(device, struct nvm_dev, dev);
  148. struct request_queue *q = dev->q;
  149. pr_debug("nvm/sysfs: `nvm_dev_release`\n");
  150. blk_mq_unregister_dev(device, q);
  151. nvm_free(dev);
  152. }
  153. static struct device_type nvm_type = {
  154. .name = "lightnvm",
  155. .groups = nvm_dev_attr_groups,
  156. .release = nvm_dev_release,
  157. };
  158. int nvm_sysfs_register_dev(struct nvm_dev *dev)
  159. {
  160. int ret;
  161. if (!dev->parent_dev)
  162. return 0;
  163. dev->dev.parent = dev->parent_dev;
  164. dev_set_name(&dev->dev, "%s", dev->name);
  165. dev->dev.type = &nvm_type;
  166. device_initialize(&dev->dev);
  167. ret = device_add(&dev->dev);
  168. if (!ret)
  169. blk_mq_register_dev(&dev->dev, dev->q);
  170. return ret;
  171. }
  172. void nvm_sysfs_unregister_dev(struct nvm_dev *dev)
  173. {
  174. if (dev && dev->parent_dev)
  175. kobject_put(&dev->dev.kobj);
  176. }