sysfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sysfs interface for the NVMe core driver.
  4. *
  5. * Copyright (c) 2011-2014, Intel Corporation.
  6. */
  7. #include <linux/nvme-auth.h>
  8. #include "nvme.h"
  9. #include "fabrics.h"
  10. static ssize_t nvme_sysfs_reset(struct device *dev,
  11. struct device_attribute *attr, const char *buf,
  12. size_t count)
  13. {
  14. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  15. int ret;
  16. ret = nvme_reset_ctrl_sync(ctrl);
  17. if (ret < 0)
  18. return ret;
  19. return count;
  20. }
  21. static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
  22. static ssize_t nvme_sysfs_rescan(struct device *dev,
  23. struct device_attribute *attr, const char *buf,
  24. size_t count)
  25. {
  26. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  27. nvme_queue_scan(ctrl);
  28. return count;
  29. }
  30. static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
  31. static ssize_t nvme_adm_passthru_err_log_enabled_show(struct device *dev,
  32. struct device_attribute *attr, char *buf)
  33. {
  34. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  35. return sysfs_emit(buf,
  36. ctrl->passthru_err_log_enabled ? "on\n" : "off\n");
  37. }
  38. static ssize_t nvme_adm_passthru_err_log_enabled_store(struct device *dev,
  39. struct device_attribute *attr, const char *buf, size_t count)
  40. {
  41. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  42. bool passthru_err_log_enabled;
  43. int err;
  44. err = kstrtobool(buf, &passthru_err_log_enabled);
  45. if (err)
  46. return -EINVAL;
  47. ctrl->passthru_err_log_enabled = passthru_err_log_enabled;
  48. return count;
  49. }
  50. static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
  51. {
  52. struct gendisk *disk = dev_to_disk(dev);
  53. if (nvme_disk_is_ns_head(disk))
  54. return disk->private_data;
  55. return nvme_get_ns_from_dev(dev)->head;
  56. }
  57. static ssize_t nvme_io_passthru_err_log_enabled_show(struct device *dev,
  58. struct device_attribute *attr, char *buf)
  59. {
  60. struct nvme_ns_head *head = dev_to_ns_head(dev);
  61. return sysfs_emit(buf, head->passthru_err_log_enabled ? "on\n" : "off\n");
  62. }
  63. static ssize_t nvme_io_passthru_err_log_enabled_store(struct device *dev,
  64. struct device_attribute *attr, const char *buf, size_t count)
  65. {
  66. struct nvme_ns_head *head = dev_to_ns_head(dev);
  67. bool passthru_err_log_enabled;
  68. int err;
  69. err = kstrtobool(buf, &passthru_err_log_enabled);
  70. if (err)
  71. return -EINVAL;
  72. head->passthru_err_log_enabled = passthru_err_log_enabled;
  73. return count;
  74. }
  75. static struct device_attribute dev_attr_adm_passthru_err_log_enabled = \
  76. __ATTR(passthru_err_log_enabled, S_IRUGO | S_IWUSR, \
  77. nvme_adm_passthru_err_log_enabled_show, nvme_adm_passthru_err_log_enabled_store);
  78. static struct device_attribute dev_attr_io_passthru_err_log_enabled = \
  79. __ATTR(passthru_err_log_enabled, S_IRUGO | S_IWUSR, \
  80. nvme_io_passthru_err_log_enabled_show, nvme_io_passthru_err_log_enabled_store);
  81. static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
  82. char *buf)
  83. {
  84. struct nvme_ns_head *head = dev_to_ns_head(dev);
  85. struct nvme_ns_ids *ids = &head->ids;
  86. struct nvme_subsystem *subsys = head->subsys;
  87. int serial_len = sizeof(subsys->serial);
  88. int model_len = sizeof(subsys->model);
  89. if (!uuid_is_null(&ids->uuid))
  90. return sysfs_emit(buf, "uuid.%pU\n", &ids->uuid);
  91. if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
  92. return sysfs_emit(buf, "eui.%16phN\n", ids->nguid);
  93. if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
  94. return sysfs_emit(buf, "eui.%8phN\n", ids->eui64);
  95. while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
  96. subsys->serial[serial_len - 1] == '\0'))
  97. serial_len--;
  98. while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
  99. subsys->model[model_len - 1] == '\0'))
  100. model_len--;
  101. return sysfs_emit(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
  102. serial_len, subsys->serial, model_len, subsys->model,
  103. head->ns_id);
  104. }
  105. static DEVICE_ATTR_RO(wwid);
  106. static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
  107. char *buf)
  108. {
  109. return sysfs_emit(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
  110. }
  111. static DEVICE_ATTR_RO(nguid);
  112. static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
  113. char *buf)
  114. {
  115. struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
  116. /* For backward compatibility expose the NGUID to userspace if
  117. * we have no UUID set
  118. */
  119. if (uuid_is_null(&ids->uuid)) {
  120. dev_warn_once(dev,
  121. "No UUID available providing old NGUID\n");
  122. return sysfs_emit(buf, "%pU\n", ids->nguid);
  123. }
  124. return sysfs_emit(buf, "%pU\n", &ids->uuid);
  125. }
  126. static DEVICE_ATTR_RO(uuid);
  127. static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
  128. char *buf)
  129. {
  130. return sysfs_emit(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
  131. }
  132. static DEVICE_ATTR_RO(eui);
  133. static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
  134. char *buf)
  135. {
  136. return sysfs_emit(buf, "%d\n", dev_to_ns_head(dev)->ns_id);
  137. }
  138. static DEVICE_ATTR_RO(nsid);
  139. static ssize_t csi_show(struct device *dev, struct device_attribute *attr,
  140. char *buf)
  141. {
  142. return sysfs_emit(buf, "%u\n", dev_to_ns_head(dev)->ids.csi);
  143. }
  144. static DEVICE_ATTR_RO(csi);
  145. static ssize_t metadata_bytes_show(struct device *dev,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. return sysfs_emit(buf, "%u\n", dev_to_ns_head(dev)->ms);
  149. }
  150. static DEVICE_ATTR_RO(metadata_bytes);
  151. static int ns_head_update_nuse(struct nvme_ns_head *head)
  152. {
  153. struct nvme_id_ns *id;
  154. struct nvme_ns *ns;
  155. int srcu_idx, ret = -EWOULDBLOCK;
  156. /* Avoid issuing commands too often by rate limiting the update */
  157. if (!__ratelimit(&head->rs_nuse))
  158. return 0;
  159. srcu_idx = srcu_read_lock(&head->srcu);
  160. ns = nvme_find_path(head);
  161. if (!ns)
  162. goto out_unlock;
  163. ret = nvme_identify_ns(ns->ctrl, head->ns_id, &id);
  164. if (ret)
  165. goto out_unlock;
  166. head->nuse = le64_to_cpu(id->nuse);
  167. kfree(id);
  168. out_unlock:
  169. srcu_read_unlock(&head->srcu, srcu_idx);
  170. return ret;
  171. }
  172. static int ns_update_nuse(struct nvme_ns *ns)
  173. {
  174. struct nvme_id_ns *id;
  175. int ret;
  176. /* Avoid issuing commands too often by rate limiting the update. */
  177. if (!__ratelimit(&ns->head->rs_nuse))
  178. return 0;
  179. ret = nvme_identify_ns(ns->ctrl, ns->head->ns_id, &id);
  180. if (ret)
  181. return ret;
  182. ns->head->nuse = le64_to_cpu(id->nuse);
  183. kfree(id);
  184. return 0;
  185. }
  186. static ssize_t nuse_show(struct device *dev, struct device_attribute *attr,
  187. char *buf)
  188. {
  189. struct nvme_ns_head *head = dev_to_ns_head(dev);
  190. struct gendisk *disk = dev_to_disk(dev);
  191. int ret;
  192. if (nvme_disk_is_ns_head(disk))
  193. ret = ns_head_update_nuse(head);
  194. else
  195. ret = ns_update_nuse(disk->private_data);
  196. if (ret)
  197. return ret;
  198. return sysfs_emit(buf, "%llu\n", head->nuse);
  199. }
  200. static DEVICE_ATTR_RO(nuse);
  201. static struct attribute *nvme_ns_attrs[] = {
  202. &dev_attr_wwid.attr,
  203. &dev_attr_uuid.attr,
  204. &dev_attr_nguid.attr,
  205. &dev_attr_eui.attr,
  206. &dev_attr_csi.attr,
  207. &dev_attr_nsid.attr,
  208. &dev_attr_metadata_bytes.attr,
  209. &dev_attr_nuse.attr,
  210. #ifdef CONFIG_NVME_MULTIPATH
  211. &dev_attr_ana_grpid.attr,
  212. &dev_attr_ana_state.attr,
  213. #endif
  214. &dev_attr_io_passthru_err_log_enabled.attr,
  215. NULL,
  216. };
  217. static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
  218. struct attribute *a, int n)
  219. {
  220. struct device *dev = container_of(kobj, struct device, kobj);
  221. struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
  222. if (a == &dev_attr_uuid.attr) {
  223. if (uuid_is_null(&ids->uuid) &&
  224. !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
  225. return 0;
  226. }
  227. if (a == &dev_attr_nguid.attr) {
  228. if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
  229. return 0;
  230. }
  231. if (a == &dev_attr_eui.attr) {
  232. if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
  233. return 0;
  234. }
  235. #ifdef CONFIG_NVME_MULTIPATH
  236. if (a == &dev_attr_ana_grpid.attr || a == &dev_attr_ana_state.attr) {
  237. /* per-path attr */
  238. if (nvme_disk_is_ns_head(dev_to_disk(dev)))
  239. return 0;
  240. if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl))
  241. return 0;
  242. }
  243. #endif
  244. return a->mode;
  245. }
  246. static const struct attribute_group nvme_ns_attr_group = {
  247. .attrs = nvme_ns_attrs,
  248. .is_visible = nvme_ns_attrs_are_visible,
  249. };
  250. const struct attribute_group *nvme_ns_attr_groups[] = {
  251. &nvme_ns_attr_group,
  252. NULL,
  253. };
  254. #define nvme_show_str_function(field) \
  255. static ssize_t field##_show(struct device *dev, \
  256. struct device_attribute *attr, char *buf) \
  257. { \
  258. struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
  259. return sysfs_emit(buf, "%.*s\n", \
  260. (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
  261. } \
  262. static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
  263. nvme_show_str_function(model);
  264. nvme_show_str_function(serial);
  265. nvme_show_str_function(firmware_rev);
  266. #define nvme_show_int_function(field) \
  267. static ssize_t field##_show(struct device *dev, \
  268. struct device_attribute *attr, char *buf) \
  269. { \
  270. struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
  271. return sysfs_emit(buf, "%d\n", ctrl->field); \
  272. } \
  273. static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
  274. nvme_show_int_function(cntlid);
  275. nvme_show_int_function(numa_node);
  276. nvme_show_int_function(queue_count);
  277. nvme_show_int_function(sqsize);
  278. nvme_show_int_function(kato);
  279. static ssize_t nvme_sysfs_delete(struct device *dev,
  280. struct device_attribute *attr, const char *buf,
  281. size_t count)
  282. {
  283. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  284. if (!test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags))
  285. return -EBUSY;
  286. if (device_remove_file_self(dev, attr))
  287. nvme_delete_ctrl_sync(ctrl);
  288. return count;
  289. }
  290. static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
  291. static ssize_t nvme_sysfs_show_transport(struct device *dev,
  292. struct device_attribute *attr,
  293. char *buf)
  294. {
  295. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  296. return sysfs_emit(buf, "%s\n", ctrl->ops->name);
  297. }
  298. static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
  299. static ssize_t nvme_sysfs_show_state(struct device *dev,
  300. struct device_attribute *attr,
  301. char *buf)
  302. {
  303. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  304. unsigned state = (unsigned)nvme_ctrl_state(ctrl);
  305. static const char *const state_name[] = {
  306. [NVME_CTRL_NEW] = "new",
  307. [NVME_CTRL_LIVE] = "live",
  308. [NVME_CTRL_RESETTING] = "resetting",
  309. [NVME_CTRL_CONNECTING] = "connecting",
  310. [NVME_CTRL_DELETING] = "deleting",
  311. [NVME_CTRL_DELETING_NOIO]= "deleting (no IO)",
  312. [NVME_CTRL_DEAD] = "dead",
  313. };
  314. if (state < ARRAY_SIZE(state_name) && state_name[state])
  315. return sysfs_emit(buf, "%s\n", state_name[state]);
  316. return sysfs_emit(buf, "unknown state\n");
  317. }
  318. static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
  319. static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
  320. struct device_attribute *attr,
  321. char *buf)
  322. {
  323. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  324. return sysfs_emit(buf, "%s\n", ctrl->subsys->subnqn);
  325. }
  326. static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
  327. static ssize_t nvme_sysfs_show_hostnqn(struct device *dev,
  328. struct device_attribute *attr,
  329. char *buf)
  330. {
  331. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  332. return sysfs_emit(buf, "%s\n", ctrl->opts->host->nqn);
  333. }
  334. static DEVICE_ATTR(hostnqn, S_IRUGO, nvme_sysfs_show_hostnqn, NULL);
  335. static ssize_t nvme_sysfs_show_hostid(struct device *dev,
  336. struct device_attribute *attr,
  337. char *buf)
  338. {
  339. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  340. return sysfs_emit(buf, "%pU\n", &ctrl->opts->host->id);
  341. }
  342. static DEVICE_ATTR(hostid, S_IRUGO, nvme_sysfs_show_hostid, NULL);
  343. static ssize_t nvme_sysfs_show_address(struct device *dev,
  344. struct device_attribute *attr,
  345. char *buf)
  346. {
  347. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  348. return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
  349. }
  350. static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
  351. static ssize_t nvme_ctrl_loss_tmo_show(struct device *dev,
  352. struct device_attribute *attr, char *buf)
  353. {
  354. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  355. struct nvmf_ctrl_options *opts = ctrl->opts;
  356. if (ctrl->opts->max_reconnects == -1)
  357. return sysfs_emit(buf, "off\n");
  358. return sysfs_emit(buf, "%d\n",
  359. opts->max_reconnects * opts->reconnect_delay);
  360. }
  361. static ssize_t nvme_ctrl_loss_tmo_store(struct device *dev,
  362. struct device_attribute *attr, const char *buf, size_t count)
  363. {
  364. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  365. struct nvmf_ctrl_options *opts = ctrl->opts;
  366. int ctrl_loss_tmo, err;
  367. err = kstrtoint(buf, 10, &ctrl_loss_tmo);
  368. if (err)
  369. return -EINVAL;
  370. if (ctrl_loss_tmo < 0)
  371. opts->max_reconnects = -1;
  372. else
  373. opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
  374. opts->reconnect_delay);
  375. return count;
  376. }
  377. static DEVICE_ATTR(ctrl_loss_tmo, S_IRUGO | S_IWUSR,
  378. nvme_ctrl_loss_tmo_show, nvme_ctrl_loss_tmo_store);
  379. static ssize_t nvme_ctrl_reconnect_delay_show(struct device *dev,
  380. struct device_attribute *attr, char *buf)
  381. {
  382. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  383. if (ctrl->opts->reconnect_delay == -1)
  384. return sysfs_emit(buf, "off\n");
  385. return sysfs_emit(buf, "%d\n", ctrl->opts->reconnect_delay);
  386. }
  387. static ssize_t nvme_ctrl_reconnect_delay_store(struct device *dev,
  388. struct device_attribute *attr, const char *buf, size_t count)
  389. {
  390. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  391. unsigned int v;
  392. int err;
  393. err = kstrtou32(buf, 10, &v);
  394. if (err)
  395. return err;
  396. ctrl->opts->reconnect_delay = v;
  397. return count;
  398. }
  399. static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR,
  400. nvme_ctrl_reconnect_delay_show, nvme_ctrl_reconnect_delay_store);
  401. static ssize_t nvme_ctrl_fast_io_fail_tmo_show(struct device *dev,
  402. struct device_attribute *attr, char *buf)
  403. {
  404. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  405. if (ctrl->opts->fast_io_fail_tmo == -1)
  406. return sysfs_emit(buf, "off\n");
  407. return sysfs_emit(buf, "%d\n", ctrl->opts->fast_io_fail_tmo);
  408. }
  409. static ssize_t nvme_ctrl_fast_io_fail_tmo_store(struct device *dev,
  410. struct device_attribute *attr, const char *buf, size_t count)
  411. {
  412. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  413. struct nvmf_ctrl_options *opts = ctrl->opts;
  414. int fast_io_fail_tmo, err;
  415. err = kstrtoint(buf, 10, &fast_io_fail_tmo);
  416. if (err)
  417. return -EINVAL;
  418. if (fast_io_fail_tmo < 0)
  419. opts->fast_io_fail_tmo = -1;
  420. else
  421. opts->fast_io_fail_tmo = fast_io_fail_tmo;
  422. return count;
  423. }
  424. static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
  425. nvme_ctrl_fast_io_fail_tmo_show, nvme_ctrl_fast_io_fail_tmo_store);
  426. static ssize_t cntrltype_show(struct device *dev,
  427. struct device_attribute *attr, char *buf)
  428. {
  429. static const char * const type[] = {
  430. [NVME_CTRL_IO] = "io\n",
  431. [NVME_CTRL_DISC] = "discovery\n",
  432. [NVME_CTRL_ADMIN] = "admin\n",
  433. };
  434. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  435. if (ctrl->cntrltype > NVME_CTRL_ADMIN || !type[ctrl->cntrltype])
  436. return sysfs_emit(buf, "reserved\n");
  437. return sysfs_emit(buf, type[ctrl->cntrltype]);
  438. }
  439. static DEVICE_ATTR_RO(cntrltype);
  440. static ssize_t dctype_show(struct device *dev,
  441. struct device_attribute *attr, char *buf)
  442. {
  443. static const char * const type[] = {
  444. [NVME_DCTYPE_NOT_REPORTED] = "none\n",
  445. [NVME_DCTYPE_DDC] = "ddc\n",
  446. [NVME_DCTYPE_CDC] = "cdc\n",
  447. };
  448. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  449. if (ctrl->dctype > NVME_DCTYPE_CDC || !type[ctrl->dctype])
  450. return sysfs_emit(buf, "reserved\n");
  451. return sysfs_emit(buf, type[ctrl->dctype]);
  452. }
  453. static DEVICE_ATTR_RO(dctype);
  454. #ifdef CONFIG_NVME_HOST_AUTH
  455. static ssize_t nvme_ctrl_dhchap_secret_show(struct device *dev,
  456. struct device_attribute *attr, char *buf)
  457. {
  458. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  459. struct nvmf_ctrl_options *opts = ctrl->opts;
  460. if (!opts->dhchap_secret)
  461. return sysfs_emit(buf, "none\n");
  462. return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
  463. }
  464. static ssize_t nvme_ctrl_dhchap_secret_store(struct device *dev,
  465. struct device_attribute *attr, const char *buf, size_t count)
  466. {
  467. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  468. struct nvmf_ctrl_options *opts = ctrl->opts;
  469. char *dhchap_secret;
  470. if (!ctrl->opts->dhchap_secret)
  471. return -EINVAL;
  472. if (count < 7)
  473. return -EINVAL;
  474. if (memcmp(buf, "DHHC-1:", 7))
  475. return -EINVAL;
  476. dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
  477. if (!dhchap_secret)
  478. return -ENOMEM;
  479. memcpy(dhchap_secret, buf, count);
  480. nvme_auth_stop(ctrl);
  481. if (strcmp(dhchap_secret, opts->dhchap_secret)) {
  482. struct nvme_dhchap_key *key, *host_key;
  483. int ret;
  484. ret = nvme_auth_generate_key(dhchap_secret, &key);
  485. if (ret) {
  486. kfree(dhchap_secret);
  487. return ret;
  488. }
  489. kfree(opts->dhchap_secret);
  490. opts->dhchap_secret = dhchap_secret;
  491. host_key = ctrl->host_key;
  492. mutex_lock(&ctrl->dhchap_auth_mutex);
  493. ctrl->host_key = key;
  494. mutex_unlock(&ctrl->dhchap_auth_mutex);
  495. nvme_auth_free_key(host_key);
  496. } else
  497. kfree(dhchap_secret);
  498. /* Start re-authentication */
  499. dev_info(ctrl->device, "re-authenticating controller\n");
  500. queue_work(nvme_wq, &ctrl->dhchap_auth_work);
  501. return count;
  502. }
  503. static DEVICE_ATTR(dhchap_secret, S_IRUGO | S_IWUSR,
  504. nvme_ctrl_dhchap_secret_show, nvme_ctrl_dhchap_secret_store);
  505. static ssize_t nvme_ctrl_dhchap_ctrl_secret_show(struct device *dev,
  506. struct device_attribute *attr, char *buf)
  507. {
  508. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  509. struct nvmf_ctrl_options *opts = ctrl->opts;
  510. if (!opts->dhchap_ctrl_secret)
  511. return sysfs_emit(buf, "none\n");
  512. return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
  513. }
  514. static ssize_t nvme_ctrl_dhchap_ctrl_secret_store(struct device *dev,
  515. struct device_attribute *attr, const char *buf, size_t count)
  516. {
  517. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  518. struct nvmf_ctrl_options *opts = ctrl->opts;
  519. char *dhchap_secret;
  520. if (!ctrl->opts->dhchap_ctrl_secret)
  521. return -EINVAL;
  522. if (count < 7)
  523. return -EINVAL;
  524. if (memcmp(buf, "DHHC-1:", 7))
  525. return -EINVAL;
  526. dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
  527. if (!dhchap_secret)
  528. return -ENOMEM;
  529. memcpy(dhchap_secret, buf, count);
  530. nvme_auth_stop(ctrl);
  531. if (strcmp(dhchap_secret, opts->dhchap_ctrl_secret)) {
  532. struct nvme_dhchap_key *key, *ctrl_key;
  533. int ret;
  534. ret = nvme_auth_generate_key(dhchap_secret, &key);
  535. if (ret) {
  536. kfree(dhchap_secret);
  537. return ret;
  538. }
  539. kfree(opts->dhchap_ctrl_secret);
  540. opts->dhchap_ctrl_secret = dhchap_secret;
  541. ctrl_key = ctrl->ctrl_key;
  542. mutex_lock(&ctrl->dhchap_auth_mutex);
  543. ctrl->ctrl_key = key;
  544. mutex_unlock(&ctrl->dhchap_auth_mutex);
  545. nvme_auth_free_key(ctrl_key);
  546. } else
  547. kfree(dhchap_secret);
  548. /* Start re-authentication */
  549. dev_info(ctrl->device, "re-authenticating controller\n");
  550. queue_work(nvme_wq, &ctrl->dhchap_auth_work);
  551. return count;
  552. }
  553. static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR,
  554. nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);
  555. #endif
  556. static struct attribute *nvme_dev_attrs[] = {
  557. &dev_attr_reset_controller.attr,
  558. &dev_attr_rescan_controller.attr,
  559. &dev_attr_model.attr,
  560. &dev_attr_serial.attr,
  561. &dev_attr_firmware_rev.attr,
  562. &dev_attr_cntlid.attr,
  563. &dev_attr_delete_controller.attr,
  564. &dev_attr_transport.attr,
  565. &dev_attr_subsysnqn.attr,
  566. &dev_attr_address.attr,
  567. &dev_attr_state.attr,
  568. &dev_attr_numa_node.attr,
  569. &dev_attr_queue_count.attr,
  570. &dev_attr_sqsize.attr,
  571. &dev_attr_hostnqn.attr,
  572. &dev_attr_hostid.attr,
  573. &dev_attr_ctrl_loss_tmo.attr,
  574. &dev_attr_reconnect_delay.attr,
  575. &dev_attr_fast_io_fail_tmo.attr,
  576. &dev_attr_kato.attr,
  577. &dev_attr_cntrltype.attr,
  578. &dev_attr_dctype.attr,
  579. #ifdef CONFIG_NVME_HOST_AUTH
  580. &dev_attr_dhchap_secret.attr,
  581. &dev_attr_dhchap_ctrl_secret.attr,
  582. #endif
  583. &dev_attr_adm_passthru_err_log_enabled.attr,
  584. NULL
  585. };
  586. static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
  587. struct attribute *a, int n)
  588. {
  589. struct device *dev = container_of(kobj, struct device, kobj);
  590. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  591. if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
  592. return 0;
  593. if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
  594. return 0;
  595. if (a == &dev_attr_hostnqn.attr && !ctrl->opts)
  596. return 0;
  597. if (a == &dev_attr_hostid.attr && !ctrl->opts)
  598. return 0;
  599. if (a == &dev_attr_ctrl_loss_tmo.attr && !ctrl->opts)
  600. return 0;
  601. if (a == &dev_attr_reconnect_delay.attr && !ctrl->opts)
  602. return 0;
  603. if (a == &dev_attr_fast_io_fail_tmo.attr && !ctrl->opts)
  604. return 0;
  605. #ifdef CONFIG_NVME_HOST_AUTH
  606. if (a == &dev_attr_dhchap_secret.attr && !ctrl->opts)
  607. return 0;
  608. if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
  609. return 0;
  610. #endif
  611. return a->mode;
  612. }
  613. const struct attribute_group nvme_dev_attrs_group = {
  614. .attrs = nvme_dev_attrs,
  615. .is_visible = nvme_dev_attrs_are_visible,
  616. };
  617. EXPORT_SYMBOL_GPL(nvme_dev_attrs_group);
  618. #ifdef CONFIG_NVME_TCP_TLS
  619. static ssize_t tls_key_show(struct device *dev,
  620. struct device_attribute *attr, char *buf)
  621. {
  622. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  623. if (!ctrl->tls_pskid)
  624. return 0;
  625. return sysfs_emit(buf, "%08x\n", ctrl->tls_pskid);
  626. }
  627. static DEVICE_ATTR_RO(tls_key);
  628. static ssize_t tls_configured_key_show(struct device *dev,
  629. struct device_attribute *attr, char *buf)
  630. {
  631. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  632. struct key *key = ctrl->opts->tls_key;
  633. return sysfs_emit(buf, "%08x\n", key_serial(key));
  634. }
  635. static DEVICE_ATTR_RO(tls_configured_key);
  636. static ssize_t tls_keyring_show(struct device *dev,
  637. struct device_attribute *attr, char *buf)
  638. {
  639. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  640. struct key *keyring = ctrl->opts->keyring;
  641. return sysfs_emit(buf, "%s\n", keyring->description);
  642. }
  643. static DEVICE_ATTR_RO(tls_keyring);
  644. static struct attribute *nvme_tls_attrs[] = {
  645. &dev_attr_tls_key.attr,
  646. &dev_attr_tls_configured_key.attr,
  647. &dev_attr_tls_keyring.attr,
  648. NULL,
  649. };
  650. static umode_t nvme_tls_attrs_are_visible(struct kobject *kobj,
  651. struct attribute *a, int n)
  652. {
  653. struct device *dev = container_of(kobj, struct device, kobj);
  654. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  655. if (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp"))
  656. return 0;
  657. if (a == &dev_attr_tls_key.attr &&
  658. !ctrl->opts->tls)
  659. return 0;
  660. if (a == &dev_attr_tls_configured_key.attr &&
  661. !ctrl->opts->tls_key)
  662. return 0;
  663. if (a == &dev_attr_tls_keyring.attr &&
  664. !ctrl->opts->keyring)
  665. return 0;
  666. return a->mode;
  667. }
  668. static const struct attribute_group nvme_tls_attrs_group = {
  669. .attrs = nvme_tls_attrs,
  670. .is_visible = nvme_tls_attrs_are_visible,
  671. };
  672. #endif
  673. const struct attribute_group *nvme_dev_attr_groups[] = {
  674. &nvme_dev_attrs_group,
  675. #ifdef CONFIG_NVME_TCP_TLS
  676. &nvme_tls_attrs_group,
  677. #endif
  678. NULL,
  679. };
  680. #define SUBSYS_ATTR_RO(_name, _mode, _show) \
  681. struct device_attribute subsys_attr_##_name = \
  682. __ATTR(_name, _mode, _show, NULL)
  683. static ssize_t nvme_subsys_show_nqn(struct device *dev,
  684. struct device_attribute *attr,
  685. char *buf)
  686. {
  687. struct nvme_subsystem *subsys =
  688. container_of(dev, struct nvme_subsystem, dev);
  689. return sysfs_emit(buf, "%s\n", subsys->subnqn);
  690. }
  691. static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
  692. static ssize_t nvme_subsys_show_type(struct device *dev,
  693. struct device_attribute *attr,
  694. char *buf)
  695. {
  696. struct nvme_subsystem *subsys =
  697. container_of(dev, struct nvme_subsystem, dev);
  698. switch (subsys->subtype) {
  699. case NVME_NQN_DISC:
  700. return sysfs_emit(buf, "discovery\n");
  701. case NVME_NQN_NVME:
  702. return sysfs_emit(buf, "nvm\n");
  703. default:
  704. return sysfs_emit(buf, "reserved\n");
  705. }
  706. }
  707. static SUBSYS_ATTR_RO(subsystype, S_IRUGO, nvme_subsys_show_type);
  708. #define nvme_subsys_show_str_function(field) \
  709. static ssize_t subsys_##field##_show(struct device *dev, \
  710. struct device_attribute *attr, char *buf) \
  711. { \
  712. struct nvme_subsystem *subsys = \
  713. container_of(dev, struct nvme_subsystem, dev); \
  714. return sysfs_emit(buf, "%.*s\n", \
  715. (int)sizeof(subsys->field), subsys->field); \
  716. } \
  717. static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
  718. nvme_subsys_show_str_function(model);
  719. nvme_subsys_show_str_function(serial);
  720. nvme_subsys_show_str_function(firmware_rev);
  721. static struct attribute *nvme_subsys_attrs[] = {
  722. &subsys_attr_model.attr,
  723. &subsys_attr_serial.attr,
  724. &subsys_attr_firmware_rev.attr,
  725. &subsys_attr_subsysnqn.attr,
  726. &subsys_attr_subsystype.attr,
  727. #ifdef CONFIG_NVME_MULTIPATH
  728. &subsys_attr_iopolicy.attr,
  729. #endif
  730. NULL,
  731. };
  732. static const struct attribute_group nvme_subsys_attrs_group = {
  733. .attrs = nvme_subsys_attrs,
  734. };
  735. const struct attribute_group *nvme_subsys_attrs_groups[] = {
  736. &nvme_subsys_attrs_group,
  737. NULL,
  738. };