fabrics.c 26 KB

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