fabrics.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. * NVMe over Fabrics common host code.
  3. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/init.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/parser.h>
  20. #include <linux/seq_file.h>
  21. #include "nvme.h"
  22. #include "fabrics.h"
  23. static LIST_HEAD(nvmf_transports);
  24. static DECLARE_RWSEM(nvmf_transports_rwsem);
  25. static LIST_HEAD(nvmf_hosts);
  26. static DEFINE_MUTEX(nvmf_hosts_mutex);
  27. static struct nvmf_host *nvmf_default_host;
  28. static struct nvmf_host *__nvmf_host_find(const char *hostnqn)
  29. {
  30. struct nvmf_host *host;
  31. list_for_each_entry(host, &nvmf_hosts, list) {
  32. if (!strcmp(host->nqn, hostnqn))
  33. return host;
  34. }
  35. return NULL;
  36. }
  37. static struct nvmf_host *nvmf_host_add(const char *hostnqn)
  38. {
  39. struct nvmf_host *host;
  40. mutex_lock(&nvmf_hosts_mutex);
  41. host = __nvmf_host_find(hostnqn);
  42. if (host) {
  43. kref_get(&host->ref);
  44. goto out_unlock;
  45. }
  46. host = kmalloc(sizeof(*host), GFP_KERNEL);
  47. if (!host)
  48. goto out_unlock;
  49. kref_init(&host->ref);
  50. memcpy(host->nqn, hostnqn, NVMF_NQN_SIZE);
  51. list_add_tail(&host->list, &nvmf_hosts);
  52. out_unlock:
  53. mutex_unlock(&nvmf_hosts_mutex);
  54. return host;
  55. }
  56. static struct nvmf_host *nvmf_host_default(void)
  57. {
  58. struct nvmf_host *host;
  59. host = kmalloc(sizeof(*host), GFP_KERNEL);
  60. if (!host)
  61. return NULL;
  62. kref_init(&host->ref);
  63. uuid_gen(&host->id);
  64. snprintf(host->nqn, NVMF_NQN_SIZE,
  65. "nqn.2014-08.org.nvmexpress:uuid:%pUb", &host->id);
  66. mutex_lock(&nvmf_hosts_mutex);
  67. list_add_tail(&host->list, &nvmf_hosts);
  68. mutex_unlock(&nvmf_hosts_mutex);
  69. return host;
  70. }
  71. static void nvmf_host_destroy(struct kref *ref)
  72. {
  73. struct nvmf_host *host = container_of(ref, struct nvmf_host, ref);
  74. mutex_lock(&nvmf_hosts_mutex);
  75. list_del(&host->list);
  76. mutex_unlock(&nvmf_hosts_mutex);
  77. kfree(host);
  78. }
  79. static void nvmf_host_put(struct nvmf_host *host)
  80. {
  81. if (host)
  82. kref_put(&host->ref, nvmf_host_destroy);
  83. }
  84. /**
  85. * nvmf_get_address() - Get address/port
  86. * @ctrl: Host NVMe controller instance which we got the address
  87. * @buf: OUTPUT parameter that will contain the address/port
  88. * @size: buffer size
  89. */
  90. int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
  91. {
  92. int len = 0;
  93. if (ctrl->opts->mask & NVMF_OPT_TRADDR)
  94. len += snprintf(buf, size, "traddr=%s", ctrl->opts->traddr);
  95. if (ctrl->opts->mask & NVMF_OPT_TRSVCID)
  96. len += snprintf(buf + len, size - len, "%strsvcid=%s",
  97. (len) ? "," : "", ctrl->opts->trsvcid);
  98. if (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)
  99. len += snprintf(buf + len, size - len, "%shost_traddr=%s",
  100. (len) ? "," : "", ctrl->opts->host_traddr);
  101. len += snprintf(buf + len, size - len, "\n");
  102. return len;
  103. }
  104. EXPORT_SYMBOL_GPL(nvmf_get_address);
  105. /**
  106. * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function.
  107. * @ctrl: Host NVMe controller instance maintaining the admin
  108. * queue used to submit the property read command to
  109. * the allocated NVMe controller resource on the target system.
  110. * @off: Starting offset value of the targeted property
  111. * register (see the fabrics section of the NVMe standard).
  112. * @val: OUTPUT parameter that will contain the value of
  113. * the property after a successful read.
  114. *
  115. * Used by the host system to retrieve a 32-bit capsule property value
  116. * from an NVMe controller on the target system.
  117. *
  118. * ("Capsule property" is an "PCIe register concept" applied to the
  119. * NVMe fabrics space.)
  120. *
  121. * Return:
  122. * 0: successful read
  123. * > 0: NVMe error status code
  124. * < 0: Linux errno error code
  125. */
  126. int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
  127. {
  128. struct nvme_command cmd;
  129. union nvme_result res;
  130. int ret;
  131. memset(&cmd, 0, sizeof(cmd));
  132. cmd.prop_get.opcode = nvme_fabrics_command;
  133. cmd.prop_get.fctype = nvme_fabrics_type_property_get;
  134. cmd.prop_get.offset = cpu_to_le32(off);
  135. ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
  136. NVME_QID_ANY, 0, 0);
  137. if (ret >= 0)
  138. *val = le64_to_cpu(res.u64);
  139. if (unlikely(ret != 0))
  140. dev_err(ctrl->device,
  141. "Property Get error: %d, offset %#x\n",
  142. ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
  143. return ret;
  144. }
  145. EXPORT_SYMBOL_GPL(nvmf_reg_read32);
  146. /**
  147. * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function.
  148. * @ctrl: Host NVMe controller instance maintaining the admin
  149. * queue used to submit the property read command to
  150. * the allocated controller resource on the target system.
  151. * @off: Starting offset value of the targeted property
  152. * register (see the fabrics section of the NVMe standard).
  153. * @val: OUTPUT parameter that will contain the value of
  154. * the property after a successful read.
  155. *
  156. * Used by the host system to retrieve a 64-bit capsule property value
  157. * from an NVMe controller on the target system.
  158. *
  159. * ("Capsule property" is an "PCIe register concept" applied to the
  160. * NVMe fabrics space.)
  161. *
  162. * Return:
  163. * 0: successful read
  164. * > 0: NVMe error status code
  165. * < 0: Linux errno error code
  166. */
  167. int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
  168. {
  169. struct nvme_command cmd;
  170. union nvme_result res;
  171. int ret;
  172. memset(&cmd, 0, sizeof(cmd));
  173. cmd.prop_get.opcode = nvme_fabrics_command;
  174. cmd.prop_get.fctype = nvme_fabrics_type_property_get;
  175. cmd.prop_get.attrib = 1;
  176. cmd.prop_get.offset = cpu_to_le32(off);
  177. ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
  178. NVME_QID_ANY, 0, 0);
  179. if (ret >= 0)
  180. *val = le64_to_cpu(res.u64);
  181. if (unlikely(ret != 0))
  182. dev_err(ctrl->device,
  183. "Property Get error: %d, offset %#x\n",
  184. ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
  185. return ret;
  186. }
  187. EXPORT_SYMBOL_GPL(nvmf_reg_read64);
  188. /**
  189. * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function.
  190. * @ctrl: Host NVMe controller instance maintaining the admin
  191. * queue used to submit the property read command to
  192. * the allocated NVMe controller resource on the target system.
  193. * @off: Starting offset value of the targeted property
  194. * register (see the fabrics section of the NVMe standard).
  195. * @val: Input parameter that contains the value to be
  196. * written to the property.
  197. *
  198. * Used by the NVMe host system to write a 32-bit capsule property value
  199. * to an NVMe controller on the target system.
  200. *
  201. * ("Capsule property" is an "PCIe register concept" applied to the
  202. * NVMe fabrics space.)
  203. *
  204. * Return:
  205. * 0: successful write
  206. * > 0: NVMe error status code
  207. * < 0: Linux errno error code
  208. */
  209. int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
  210. {
  211. struct nvme_command cmd;
  212. int ret;
  213. memset(&cmd, 0, sizeof(cmd));
  214. cmd.prop_set.opcode = nvme_fabrics_command;
  215. cmd.prop_set.fctype = nvme_fabrics_type_property_set;
  216. cmd.prop_set.attrib = 0;
  217. cmd.prop_set.offset = cpu_to_le32(off);
  218. cmd.prop_set.value = cpu_to_le64(val);
  219. ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, NULL, 0, 0,
  220. NVME_QID_ANY, 0, 0);
  221. if (unlikely(ret))
  222. dev_err(ctrl->device,
  223. "Property Set error: %d, offset %#x\n",
  224. ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
  225. return ret;
  226. }
  227. EXPORT_SYMBOL_GPL(nvmf_reg_write32);
  228. /**
  229. * nvmf_log_connect_error() - Error-parsing-diagnostic print
  230. * out function for connect() errors.
  231. *
  232. * @ctrl: the specific /dev/nvmeX device that had the error.
  233. *
  234. * @errval: Error code to be decoded in a more human-friendly
  235. * printout.
  236. *
  237. * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM.
  238. *
  239. * @cmd: This is the SQE portion of a submission capsule.
  240. *
  241. * @data: This is the "Data" portion of a submission capsule.
  242. */
  243. static void nvmf_log_connect_error(struct nvme_ctrl *ctrl,
  244. int errval, int offset, struct nvme_command *cmd,
  245. struct nvmf_connect_data *data)
  246. {
  247. int err_sctype = errval & (~NVME_SC_DNR);
  248. switch (err_sctype) {
  249. case (NVME_SC_CONNECT_INVALID_PARAM):
  250. if (offset >> 16) {
  251. char *inv_data = "Connect Invalid Data Parameter";
  252. switch (offset & 0xffff) {
  253. case (offsetof(struct nvmf_connect_data, cntlid)):
  254. dev_err(ctrl->device,
  255. "%s, cntlid: %d\n",
  256. inv_data, data->cntlid);
  257. break;
  258. case (offsetof(struct nvmf_connect_data, hostnqn)):
  259. dev_err(ctrl->device,
  260. "%s, hostnqn \"%s\"\n",
  261. inv_data, data->hostnqn);
  262. break;
  263. case (offsetof(struct nvmf_connect_data, subsysnqn)):
  264. dev_err(ctrl->device,
  265. "%s, subsysnqn \"%s\"\n",
  266. inv_data, data->subsysnqn);
  267. break;
  268. default:
  269. dev_err(ctrl->device,
  270. "%s, starting byte offset: %d\n",
  271. inv_data, offset & 0xffff);
  272. break;
  273. }
  274. } else {
  275. char *inv_sqe = "Connect Invalid SQE Parameter";
  276. switch (offset) {
  277. case (offsetof(struct nvmf_connect_command, qid)):
  278. dev_err(ctrl->device,
  279. "%s, qid %d\n",
  280. inv_sqe, cmd->connect.qid);
  281. break;
  282. default:
  283. dev_err(ctrl->device,
  284. "%s, starting byte offset: %d\n",
  285. inv_sqe, offset);
  286. }
  287. }
  288. break;
  289. case NVME_SC_CONNECT_INVALID_HOST:
  290. dev_err(ctrl->device,
  291. "Connect for subsystem %s is not allowed, hostnqn: %s\n",
  292. data->subsysnqn, data->hostnqn);
  293. break;
  294. case NVME_SC_CONNECT_CTRL_BUSY:
  295. dev_err(ctrl->device,
  296. "Connect command failed: controller is busy or not available\n");
  297. break;
  298. case NVME_SC_CONNECT_FORMAT:
  299. dev_err(ctrl->device,
  300. "Connect incompatible format: %d",
  301. cmd->connect.recfmt);
  302. break;
  303. default:
  304. dev_err(ctrl->device,
  305. "Connect command failed, error wo/DNR bit: %d\n",
  306. err_sctype);
  307. break;
  308. } /* switch (err_sctype) */
  309. }
  310. /**
  311. * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
  312. * API function.
  313. * @ctrl: Host nvme controller instance used to request
  314. * a new NVMe controller allocation on the target
  315. * system and establish an NVMe Admin connection to
  316. * that controller.
  317. *
  318. * This function enables an NVMe host device to request a new allocation of
  319. * an NVMe controller resource on a target system as well establish a
  320. * fabrics-protocol connection of the NVMe Admin queue between the
  321. * host system device and the allocated NVMe controller on the
  322. * target system via a NVMe Fabrics "Connect" command.
  323. *
  324. * Return:
  325. * 0: success
  326. * > 0: NVMe error status code
  327. * < 0: Linux errno error code
  328. *
  329. */
  330. int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl)
  331. {
  332. struct nvme_command cmd;
  333. union nvme_result res;
  334. struct nvmf_connect_data *data;
  335. int ret;
  336. memset(&cmd, 0, sizeof(cmd));
  337. cmd.connect.opcode = nvme_fabrics_command;
  338. cmd.connect.fctype = nvme_fabrics_type_connect;
  339. cmd.connect.qid = 0;
  340. cmd.connect.sqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
  341. /*
  342. * Set keep-alive timeout in seconds granularity (ms * 1000)
  343. * and add a grace period for controller kato enforcement
  344. */
  345. cmd.connect.kato = ctrl->opts->discovery_nqn ? 0 :
  346. cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000);
  347. data = kzalloc(sizeof(*data), GFP_KERNEL);
  348. if (!data)
  349. return -ENOMEM;
  350. uuid_copy(&data->hostid, &ctrl->opts->host->id);
  351. data->cntlid = cpu_to_le16(0xffff);
  352. strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
  353. strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
  354. ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res,
  355. data, sizeof(*data), 0, NVME_QID_ANY, 1,
  356. BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
  357. if (ret) {
  358. nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
  359. &cmd, data);
  360. goto out_free_data;
  361. }
  362. ctrl->cntlid = le16_to_cpu(res.u16);
  363. out_free_data:
  364. kfree(data);
  365. return ret;
  366. }
  367. EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
  368. /**
  369. * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
  370. * API function.
  371. * @ctrl: Host nvme controller instance used to establish an
  372. * NVMe I/O queue connection to the already allocated NVMe
  373. * controller on the target system.
  374. * @qid: NVMe I/O queue number for the new I/O connection between
  375. * host and target (note qid == 0 is illegal as this is
  376. * the Admin queue, per NVMe standard).
  377. *
  378. * This function issues a fabrics-protocol connection
  379. * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
  380. * between the host system device and the allocated NVMe controller
  381. * on the target system.
  382. *
  383. * Return:
  384. * 0: success
  385. * > 0: NVMe error status code
  386. * < 0: Linux errno error code
  387. */
  388. int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid)
  389. {
  390. struct nvme_command cmd;
  391. struct nvmf_connect_data *data;
  392. union nvme_result res;
  393. int ret;
  394. memset(&cmd, 0, sizeof(cmd));
  395. cmd.connect.opcode = nvme_fabrics_command;
  396. cmd.connect.fctype = nvme_fabrics_type_connect;
  397. cmd.connect.qid = cpu_to_le16(qid);
  398. cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
  399. data = kzalloc(sizeof(*data), GFP_KERNEL);
  400. if (!data)
  401. return -ENOMEM;
  402. uuid_copy(&data->hostid, &ctrl->opts->host->id);
  403. data->cntlid = cpu_to_le16(ctrl->cntlid);
  404. strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
  405. strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
  406. ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res,
  407. data, sizeof(*data), 0, qid, 1,
  408. BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
  409. if (ret) {
  410. nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
  411. &cmd, data);
  412. }
  413. kfree(data);
  414. return ret;
  415. }
  416. EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
  417. bool nvmf_should_reconnect(struct nvme_ctrl *ctrl)
  418. {
  419. if (ctrl->opts->max_reconnects != -1 &&
  420. ctrl->nr_reconnects < ctrl->opts->max_reconnects)
  421. return true;
  422. return false;
  423. }
  424. EXPORT_SYMBOL_GPL(nvmf_should_reconnect);
  425. /**
  426. * nvmf_register_transport() - NVMe Fabrics Library registration function.
  427. * @ops: Transport ops instance to be registered to the
  428. * common fabrics library.
  429. *
  430. * API function that registers the type of specific transport fabric
  431. * being implemented to the common NVMe fabrics library. Part of
  432. * the overall init sequence of starting up a fabrics driver.
  433. */
  434. int nvmf_register_transport(struct nvmf_transport_ops *ops)
  435. {
  436. if (!ops->create_ctrl || !ops->module)
  437. return -EINVAL;
  438. down_write(&nvmf_transports_rwsem);
  439. list_add_tail(&ops->entry, &nvmf_transports);
  440. up_write(&nvmf_transports_rwsem);
  441. return 0;
  442. }
  443. EXPORT_SYMBOL_GPL(nvmf_register_transport);
  444. /**
  445. * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
  446. * @ops: Transport ops instance to be unregistered from the
  447. * common fabrics library.
  448. *
  449. * Fabrics API function that unregisters the type of specific transport
  450. * fabric being implemented from the common NVMe fabrics library.
  451. * Part of the overall exit sequence of unloading the implemented driver.
  452. */
  453. void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
  454. {
  455. down_write(&nvmf_transports_rwsem);
  456. list_del(&ops->entry);
  457. up_write(&nvmf_transports_rwsem);
  458. }
  459. EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
  460. static struct nvmf_transport_ops *nvmf_lookup_transport(
  461. struct nvmf_ctrl_options *opts)
  462. {
  463. struct nvmf_transport_ops *ops;
  464. lockdep_assert_held(&nvmf_transports_rwsem);
  465. list_for_each_entry(ops, &nvmf_transports, entry) {
  466. if (strcmp(ops->name, opts->transport) == 0)
  467. return ops;
  468. }
  469. return NULL;
  470. }
  471. static const match_table_t opt_tokens = {
  472. { NVMF_OPT_TRANSPORT, "transport=%s" },
  473. { NVMF_OPT_TRADDR, "traddr=%s" },
  474. { NVMF_OPT_TRSVCID, "trsvcid=%s" },
  475. { NVMF_OPT_NQN, "nqn=%s" },
  476. { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
  477. { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
  478. { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
  479. { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" },
  480. { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
  481. { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
  482. { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
  483. { NVMF_OPT_HOST_ID, "hostid=%s" },
  484. { NVMF_OPT_DUP_CONNECT, "duplicate_connect" },
  485. { NVMF_OPT_ERR, NULL }
  486. };
  487. static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  488. const char *buf)
  489. {
  490. substring_t args[MAX_OPT_ARGS];
  491. char *options, *o, *p;
  492. int token, ret = 0;
  493. size_t nqnlen = 0;
  494. int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
  495. uuid_t hostid;
  496. /* Set defaults */
  497. opts->queue_size = NVMF_DEF_QUEUE_SIZE;
  498. opts->nr_io_queues = num_online_cpus();
  499. opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
  500. opts->kato = NVME_DEFAULT_KATO;
  501. opts->duplicate_connect = false;
  502. options = o = kstrdup(buf, GFP_KERNEL);
  503. if (!options)
  504. return -ENOMEM;
  505. uuid_gen(&hostid);
  506. while ((p = strsep(&o, ",\n")) != NULL) {
  507. if (!*p)
  508. continue;
  509. token = match_token(p, opt_tokens, args);
  510. opts->mask |= token;
  511. switch (token) {
  512. case NVMF_OPT_TRANSPORT:
  513. p = match_strdup(args);
  514. if (!p) {
  515. ret = -ENOMEM;
  516. goto out;
  517. }
  518. opts->transport = p;
  519. break;
  520. case NVMF_OPT_NQN:
  521. p = match_strdup(args);
  522. if (!p) {
  523. ret = -ENOMEM;
  524. goto out;
  525. }
  526. opts->subsysnqn = p;
  527. nqnlen = strlen(opts->subsysnqn);
  528. if (nqnlen >= NVMF_NQN_SIZE) {
  529. pr_err("%s needs to be < %d bytes\n",
  530. opts->subsysnqn, NVMF_NQN_SIZE);
  531. ret = -EINVAL;
  532. goto out;
  533. }
  534. opts->discovery_nqn =
  535. !(strcmp(opts->subsysnqn,
  536. NVME_DISC_SUBSYS_NAME));
  537. if (opts->discovery_nqn)
  538. opts->nr_io_queues = 0;
  539. break;
  540. case NVMF_OPT_TRADDR:
  541. p = match_strdup(args);
  542. if (!p) {
  543. ret = -ENOMEM;
  544. goto out;
  545. }
  546. opts->traddr = p;
  547. break;
  548. case NVMF_OPT_TRSVCID:
  549. p = match_strdup(args);
  550. if (!p) {
  551. ret = -ENOMEM;
  552. goto out;
  553. }
  554. opts->trsvcid = p;
  555. break;
  556. case NVMF_OPT_QUEUE_SIZE:
  557. if (match_int(args, &token)) {
  558. ret = -EINVAL;
  559. goto out;
  560. }
  561. if (token < NVMF_MIN_QUEUE_SIZE ||
  562. token > NVMF_MAX_QUEUE_SIZE) {
  563. pr_err("Invalid queue_size %d\n", token);
  564. ret = -EINVAL;
  565. goto out;
  566. }
  567. opts->queue_size = token;
  568. break;
  569. case NVMF_OPT_NR_IO_QUEUES:
  570. if (match_int(args, &token)) {
  571. ret = -EINVAL;
  572. goto out;
  573. }
  574. if (token <= 0) {
  575. pr_err("Invalid number of IOQs %d\n", token);
  576. ret = -EINVAL;
  577. goto out;
  578. }
  579. opts->nr_io_queues = min_t(unsigned int,
  580. num_online_cpus(), token);
  581. break;
  582. case NVMF_OPT_KATO:
  583. if (match_int(args, &token)) {
  584. ret = -EINVAL;
  585. goto out;
  586. }
  587. if (token < 0) {
  588. pr_err("Invalid keep_alive_tmo %d\n", token);
  589. ret = -EINVAL;
  590. goto out;
  591. } else if (token == 0 && !opts->discovery_nqn) {
  592. /* Allowed for debug */
  593. pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
  594. }
  595. opts->kato = token;
  596. if (opts->discovery_nqn && opts->kato) {
  597. pr_err("Discovery controllers cannot accept KATO != 0\n");
  598. ret = -EINVAL;
  599. goto out;
  600. }
  601. break;
  602. case NVMF_OPT_CTRL_LOSS_TMO:
  603. if (match_int(args, &token)) {
  604. ret = -EINVAL;
  605. goto out;
  606. }
  607. if (token < 0)
  608. pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
  609. ctrl_loss_tmo = token;
  610. break;
  611. case NVMF_OPT_HOSTNQN:
  612. if (opts->host) {
  613. pr_err("hostnqn already user-assigned: %s\n",
  614. opts->host->nqn);
  615. ret = -EADDRINUSE;
  616. goto out;
  617. }
  618. p = match_strdup(args);
  619. if (!p) {
  620. ret = -ENOMEM;
  621. goto out;
  622. }
  623. nqnlen = strlen(p);
  624. if (nqnlen >= NVMF_NQN_SIZE) {
  625. pr_err("%s needs to be < %d bytes\n",
  626. p, NVMF_NQN_SIZE);
  627. kfree(p);
  628. ret = -EINVAL;
  629. goto out;
  630. }
  631. opts->host = nvmf_host_add(p);
  632. kfree(p);
  633. if (!opts->host) {
  634. ret = -ENOMEM;
  635. goto out;
  636. }
  637. break;
  638. case NVMF_OPT_RECONNECT_DELAY:
  639. if (match_int(args, &token)) {
  640. ret = -EINVAL;
  641. goto out;
  642. }
  643. if (token <= 0) {
  644. pr_err("Invalid reconnect_delay %d\n", token);
  645. ret = -EINVAL;
  646. goto out;
  647. }
  648. opts->reconnect_delay = token;
  649. break;
  650. case NVMF_OPT_HOST_TRADDR:
  651. p = match_strdup(args);
  652. if (!p) {
  653. ret = -ENOMEM;
  654. goto out;
  655. }
  656. opts->host_traddr = p;
  657. break;
  658. case NVMF_OPT_HOST_ID:
  659. p = match_strdup(args);
  660. if (!p) {
  661. ret = -ENOMEM;
  662. goto out;
  663. }
  664. ret = uuid_parse(p, &hostid);
  665. if (ret) {
  666. pr_err("Invalid hostid %s\n", p);
  667. ret = -EINVAL;
  668. kfree(p);
  669. goto out;
  670. }
  671. kfree(p);
  672. break;
  673. case NVMF_OPT_DUP_CONNECT:
  674. opts->duplicate_connect = true;
  675. break;
  676. default:
  677. pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
  678. p);
  679. ret = -EINVAL;
  680. goto out;
  681. }
  682. }
  683. if (ctrl_loss_tmo < 0)
  684. opts->max_reconnects = -1;
  685. else
  686. opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
  687. opts->reconnect_delay);
  688. if (!opts->host) {
  689. kref_get(&nvmf_default_host->ref);
  690. opts->host = nvmf_default_host;
  691. }
  692. uuid_copy(&opts->host->id, &hostid);
  693. out:
  694. kfree(options);
  695. return ret;
  696. }
  697. static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
  698. unsigned int required_opts)
  699. {
  700. if ((opts->mask & required_opts) != required_opts) {
  701. int i;
  702. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  703. if ((opt_tokens[i].token & required_opts) &&
  704. !(opt_tokens[i].token & opts->mask)) {
  705. pr_warn("missing parameter '%s'\n",
  706. opt_tokens[i].pattern);
  707. }
  708. }
  709. return -EINVAL;
  710. }
  711. return 0;
  712. }
  713. static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
  714. unsigned int allowed_opts)
  715. {
  716. if (opts->mask & ~allowed_opts) {
  717. int i;
  718. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  719. if ((opt_tokens[i].token & opts->mask) &&
  720. (opt_tokens[i].token & ~allowed_opts)) {
  721. pr_warn("invalid parameter '%s'\n",
  722. opt_tokens[i].pattern);
  723. }
  724. }
  725. return -EINVAL;
  726. }
  727. return 0;
  728. }
  729. void nvmf_free_options(struct nvmf_ctrl_options *opts)
  730. {
  731. nvmf_host_put(opts->host);
  732. kfree(opts->transport);
  733. kfree(opts->traddr);
  734. kfree(opts->trsvcid);
  735. kfree(opts->subsysnqn);
  736. kfree(opts->host_traddr);
  737. kfree(opts);
  738. }
  739. EXPORT_SYMBOL_GPL(nvmf_free_options);
  740. #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
  741. #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
  742. NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
  743. NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT)
  744. static struct nvme_ctrl *
  745. nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
  746. {
  747. struct nvmf_ctrl_options *opts;
  748. struct nvmf_transport_ops *ops;
  749. struct nvme_ctrl *ctrl;
  750. int ret;
  751. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  752. if (!opts)
  753. return ERR_PTR(-ENOMEM);
  754. ret = nvmf_parse_options(opts, buf);
  755. if (ret)
  756. goto out_free_opts;
  757. request_module("nvme-%s", opts->transport);
  758. /*
  759. * Check the generic options first as we need a valid transport for
  760. * the lookup below. Then clear the generic flags so that transport
  761. * drivers don't have to care about them.
  762. */
  763. ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
  764. if (ret)
  765. goto out_free_opts;
  766. opts->mask &= ~NVMF_REQUIRED_OPTS;
  767. down_read(&nvmf_transports_rwsem);
  768. ops = nvmf_lookup_transport(opts);
  769. if (!ops) {
  770. pr_info("no handler found for transport %s.\n",
  771. opts->transport);
  772. ret = -EINVAL;
  773. goto out_unlock;
  774. }
  775. if (!try_module_get(ops->module)) {
  776. ret = -EBUSY;
  777. goto out_unlock;
  778. }
  779. ret = nvmf_check_required_opts(opts, ops->required_opts);
  780. if (ret)
  781. goto out_module_put;
  782. ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
  783. ops->allowed_opts | ops->required_opts);
  784. if (ret)
  785. goto out_module_put;
  786. ctrl = ops->create_ctrl(dev, opts);
  787. if (IS_ERR(ctrl)) {
  788. ret = PTR_ERR(ctrl);
  789. goto out_module_put;
  790. }
  791. if (strcmp(ctrl->subsys->subnqn, opts->subsysnqn)) {
  792. dev_warn(ctrl->device,
  793. "controller returned incorrect NQN: \"%s\".\n",
  794. ctrl->subsys->subnqn);
  795. module_put(ops->module);
  796. up_read(&nvmf_transports_rwsem);
  797. nvme_delete_ctrl_sync(ctrl);
  798. return ERR_PTR(-EINVAL);
  799. }
  800. module_put(ops->module);
  801. up_read(&nvmf_transports_rwsem);
  802. return ctrl;
  803. out_module_put:
  804. module_put(ops->module);
  805. out_unlock:
  806. up_read(&nvmf_transports_rwsem);
  807. out_free_opts:
  808. nvmf_free_options(opts);
  809. return ERR_PTR(ret);
  810. }
  811. static struct class *nvmf_class;
  812. static struct device *nvmf_device;
  813. static DEFINE_MUTEX(nvmf_dev_mutex);
  814. static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
  815. size_t count, loff_t *pos)
  816. {
  817. struct seq_file *seq_file = file->private_data;
  818. struct nvme_ctrl *ctrl;
  819. const char *buf;
  820. int ret = 0;
  821. if (count > PAGE_SIZE)
  822. return -ENOMEM;
  823. buf = memdup_user_nul(ubuf, count);
  824. if (IS_ERR(buf))
  825. return PTR_ERR(buf);
  826. mutex_lock(&nvmf_dev_mutex);
  827. if (seq_file->private) {
  828. ret = -EINVAL;
  829. goto out_unlock;
  830. }
  831. ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
  832. if (IS_ERR(ctrl)) {
  833. ret = PTR_ERR(ctrl);
  834. goto out_unlock;
  835. }
  836. seq_file->private = ctrl;
  837. out_unlock:
  838. mutex_unlock(&nvmf_dev_mutex);
  839. kfree(buf);
  840. return ret ? ret : count;
  841. }
  842. static int nvmf_dev_show(struct seq_file *seq_file, void *private)
  843. {
  844. struct nvme_ctrl *ctrl;
  845. int ret = 0;
  846. mutex_lock(&nvmf_dev_mutex);
  847. ctrl = seq_file->private;
  848. if (!ctrl) {
  849. ret = -EINVAL;
  850. goto out_unlock;
  851. }
  852. seq_printf(seq_file, "instance=%d,cntlid=%d\n",
  853. ctrl->instance, ctrl->cntlid);
  854. out_unlock:
  855. mutex_unlock(&nvmf_dev_mutex);
  856. return ret;
  857. }
  858. static int nvmf_dev_open(struct inode *inode, struct file *file)
  859. {
  860. /*
  861. * The miscdevice code initializes file->private_data, but doesn't
  862. * make use of it later.
  863. */
  864. file->private_data = NULL;
  865. return single_open(file, nvmf_dev_show, NULL);
  866. }
  867. static int nvmf_dev_release(struct inode *inode, struct file *file)
  868. {
  869. struct seq_file *seq_file = file->private_data;
  870. struct nvme_ctrl *ctrl = seq_file->private;
  871. if (ctrl)
  872. nvme_put_ctrl(ctrl);
  873. return single_release(inode, file);
  874. }
  875. static const struct file_operations nvmf_dev_fops = {
  876. .owner = THIS_MODULE,
  877. .write = nvmf_dev_write,
  878. .read = seq_read,
  879. .open = nvmf_dev_open,
  880. .release = nvmf_dev_release,
  881. };
  882. static struct miscdevice nvmf_misc = {
  883. .minor = MISC_DYNAMIC_MINOR,
  884. .name = "nvme-fabrics",
  885. .fops = &nvmf_dev_fops,
  886. };
  887. static int __init nvmf_init(void)
  888. {
  889. int ret;
  890. nvmf_default_host = nvmf_host_default();
  891. if (!nvmf_default_host)
  892. return -ENOMEM;
  893. nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
  894. if (IS_ERR(nvmf_class)) {
  895. pr_err("couldn't register class nvme-fabrics\n");
  896. ret = PTR_ERR(nvmf_class);
  897. goto out_free_host;
  898. }
  899. nvmf_device =
  900. device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
  901. if (IS_ERR(nvmf_device)) {
  902. pr_err("couldn't create nvme-fabris device!\n");
  903. ret = PTR_ERR(nvmf_device);
  904. goto out_destroy_class;
  905. }
  906. ret = misc_register(&nvmf_misc);
  907. if (ret) {
  908. pr_err("couldn't register misc device: %d\n", ret);
  909. goto out_destroy_device;
  910. }
  911. return 0;
  912. out_destroy_device:
  913. device_destroy(nvmf_class, MKDEV(0, 0));
  914. out_destroy_class:
  915. class_destroy(nvmf_class);
  916. out_free_host:
  917. nvmf_host_put(nvmf_default_host);
  918. return ret;
  919. }
  920. static void __exit nvmf_exit(void)
  921. {
  922. misc_deregister(&nvmf_misc);
  923. device_destroy(nvmf_class, MKDEV(0, 0));
  924. class_destroy(nvmf_class);
  925. nvmf_host_put(nvmf_default_host);
  926. BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
  927. BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
  928. BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
  929. BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
  930. }
  931. MODULE_LICENSE("GPL v2");
  932. module_init(nvmf_init);
  933. module_exit(nvmf_exit);