fabrics.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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. strlcpy(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)
  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. /*
  472. * For something we're not in a state to send to the device the default action
  473. * is to busy it and retry it after the controller state is recovered. However,
  474. * if the controller is deleting or if anything is marked for failfast or
  475. * nvme multipath it is immediately failed.
  476. *
  477. * Note: commands used to initialize the controller will be marked for failfast.
  478. * Note: nvme cli/ioctl commands are marked for failfast.
  479. */
  480. blk_status_t nvmf_fail_nonready_command(struct nvme_ctrl *ctrl,
  481. struct request *rq)
  482. {
  483. if (ctrl->state != NVME_CTRL_DELETING &&
  484. ctrl->state != NVME_CTRL_DEAD &&
  485. !blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH))
  486. return BLK_STS_RESOURCE;
  487. nvme_req(rq)->status = NVME_SC_HOST_PATH_ERROR;
  488. blk_mq_start_request(rq);
  489. nvme_complete_rq(rq);
  490. return BLK_STS_OK;
  491. }
  492. EXPORT_SYMBOL_GPL(nvmf_fail_nonready_command);
  493. bool __nvmf_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
  494. bool queue_live)
  495. {
  496. struct nvme_request *req = nvme_req(rq);
  497. /*
  498. * If we are in some state of setup or teardown only allow
  499. * internally generated commands.
  500. */
  501. if (!blk_rq_is_passthrough(rq) || (req->flags & NVME_REQ_USERCMD))
  502. return false;
  503. /*
  504. * Only allow commands on a live queue, except for the connect command,
  505. * which is require to set the queue live in the appropinquate states.
  506. */
  507. switch (ctrl->state) {
  508. case NVME_CTRL_NEW:
  509. case NVME_CTRL_CONNECTING:
  510. if (req->cmd->common.opcode == nvme_fabrics_command &&
  511. req->cmd->fabrics.fctype == nvme_fabrics_type_connect)
  512. return true;
  513. break;
  514. default:
  515. break;
  516. case NVME_CTRL_DEAD:
  517. return false;
  518. }
  519. return queue_live;
  520. }
  521. EXPORT_SYMBOL_GPL(__nvmf_check_ready);
  522. static const match_table_t opt_tokens = {
  523. { NVMF_OPT_TRANSPORT, "transport=%s" },
  524. { NVMF_OPT_TRADDR, "traddr=%s" },
  525. { NVMF_OPT_TRSVCID, "trsvcid=%s" },
  526. { NVMF_OPT_NQN, "nqn=%s" },
  527. { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
  528. { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
  529. { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
  530. { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" },
  531. { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
  532. { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
  533. { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
  534. { NVMF_OPT_HOST_ID, "hostid=%s" },
  535. { NVMF_OPT_DUP_CONNECT, "duplicate_connect" },
  536. { NVMF_OPT_ERR, NULL }
  537. };
  538. static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  539. const char *buf)
  540. {
  541. substring_t args[MAX_OPT_ARGS];
  542. char *options, *o, *p;
  543. int token, ret = 0;
  544. size_t nqnlen = 0;
  545. int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
  546. uuid_t hostid;
  547. /* Set defaults */
  548. opts->queue_size = NVMF_DEF_QUEUE_SIZE;
  549. opts->nr_io_queues = num_online_cpus();
  550. opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
  551. opts->kato = NVME_DEFAULT_KATO;
  552. opts->duplicate_connect = false;
  553. options = o = kstrdup(buf, GFP_KERNEL);
  554. if (!options)
  555. return -ENOMEM;
  556. uuid_gen(&hostid);
  557. while ((p = strsep(&o, ",\n")) != NULL) {
  558. if (!*p)
  559. continue;
  560. token = match_token(p, opt_tokens, args);
  561. opts->mask |= token;
  562. switch (token) {
  563. case NVMF_OPT_TRANSPORT:
  564. p = match_strdup(args);
  565. if (!p) {
  566. ret = -ENOMEM;
  567. goto out;
  568. }
  569. kfree(opts->transport);
  570. opts->transport = p;
  571. break;
  572. case NVMF_OPT_NQN:
  573. p = match_strdup(args);
  574. if (!p) {
  575. ret = -ENOMEM;
  576. goto out;
  577. }
  578. kfree(opts->subsysnqn);
  579. opts->subsysnqn = p;
  580. nqnlen = strlen(opts->subsysnqn);
  581. if (nqnlen >= NVMF_NQN_SIZE) {
  582. pr_err("%s needs to be < %d bytes\n",
  583. opts->subsysnqn, NVMF_NQN_SIZE);
  584. ret = -EINVAL;
  585. goto out;
  586. }
  587. opts->discovery_nqn =
  588. !(strcmp(opts->subsysnqn,
  589. NVME_DISC_SUBSYS_NAME));
  590. break;
  591. case NVMF_OPT_TRADDR:
  592. p = match_strdup(args);
  593. if (!p) {
  594. ret = -ENOMEM;
  595. goto out;
  596. }
  597. kfree(opts->traddr);
  598. opts->traddr = p;
  599. break;
  600. case NVMF_OPT_TRSVCID:
  601. p = match_strdup(args);
  602. if (!p) {
  603. ret = -ENOMEM;
  604. goto out;
  605. }
  606. kfree(opts->trsvcid);
  607. opts->trsvcid = p;
  608. break;
  609. case NVMF_OPT_QUEUE_SIZE:
  610. if (match_int(args, &token)) {
  611. ret = -EINVAL;
  612. goto out;
  613. }
  614. if (token < NVMF_MIN_QUEUE_SIZE ||
  615. token > NVMF_MAX_QUEUE_SIZE) {
  616. pr_err("Invalid queue_size %d\n", token);
  617. ret = -EINVAL;
  618. goto out;
  619. }
  620. opts->queue_size = token;
  621. break;
  622. case NVMF_OPT_NR_IO_QUEUES:
  623. if (match_int(args, &token)) {
  624. ret = -EINVAL;
  625. goto out;
  626. }
  627. if (token <= 0) {
  628. pr_err("Invalid number of IOQs %d\n", token);
  629. ret = -EINVAL;
  630. goto out;
  631. }
  632. if (opts->discovery_nqn) {
  633. pr_debug("Ignoring nr_io_queues value for discovery controller\n");
  634. break;
  635. }
  636. opts->nr_io_queues = min_t(unsigned int,
  637. num_online_cpus(), token);
  638. break;
  639. case NVMF_OPT_KATO:
  640. if (match_int(args, &token)) {
  641. ret = -EINVAL;
  642. goto out;
  643. }
  644. if (token < 0) {
  645. pr_err("Invalid keep_alive_tmo %d\n", token);
  646. ret = -EINVAL;
  647. goto out;
  648. } else if (token == 0 && !opts->discovery_nqn) {
  649. /* Allowed for debug */
  650. pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
  651. }
  652. opts->kato = token;
  653. if (opts->discovery_nqn && opts->kato) {
  654. pr_err("Discovery controllers cannot accept KATO != 0\n");
  655. ret = -EINVAL;
  656. goto out;
  657. }
  658. break;
  659. case NVMF_OPT_CTRL_LOSS_TMO:
  660. if (match_int(args, &token)) {
  661. ret = -EINVAL;
  662. goto out;
  663. }
  664. if (token < 0)
  665. pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
  666. ctrl_loss_tmo = token;
  667. break;
  668. case NVMF_OPT_HOSTNQN:
  669. if (opts->host) {
  670. pr_err("hostnqn already user-assigned: %s\n",
  671. opts->host->nqn);
  672. ret = -EADDRINUSE;
  673. goto out;
  674. }
  675. p = match_strdup(args);
  676. if (!p) {
  677. ret = -ENOMEM;
  678. goto out;
  679. }
  680. nqnlen = strlen(p);
  681. if (nqnlen >= NVMF_NQN_SIZE) {
  682. pr_err("%s needs to be < %d bytes\n",
  683. p, NVMF_NQN_SIZE);
  684. kfree(p);
  685. ret = -EINVAL;
  686. goto out;
  687. }
  688. nvmf_host_put(opts->host);
  689. opts->host = nvmf_host_add(p);
  690. kfree(p);
  691. if (!opts->host) {
  692. ret = -ENOMEM;
  693. goto out;
  694. }
  695. break;
  696. case NVMF_OPT_RECONNECT_DELAY:
  697. if (match_int(args, &token)) {
  698. ret = -EINVAL;
  699. goto out;
  700. }
  701. if (token <= 0) {
  702. pr_err("Invalid reconnect_delay %d\n", token);
  703. ret = -EINVAL;
  704. goto out;
  705. }
  706. opts->reconnect_delay = token;
  707. break;
  708. case NVMF_OPT_HOST_TRADDR:
  709. p = match_strdup(args);
  710. if (!p) {
  711. ret = -ENOMEM;
  712. goto out;
  713. }
  714. kfree(opts->host_traddr);
  715. opts->host_traddr = p;
  716. break;
  717. case NVMF_OPT_HOST_ID:
  718. p = match_strdup(args);
  719. if (!p) {
  720. ret = -ENOMEM;
  721. goto out;
  722. }
  723. ret = uuid_parse(p, &hostid);
  724. if (ret) {
  725. pr_err("Invalid hostid %s\n", p);
  726. ret = -EINVAL;
  727. kfree(p);
  728. goto out;
  729. }
  730. kfree(p);
  731. break;
  732. case NVMF_OPT_DUP_CONNECT:
  733. opts->duplicate_connect = true;
  734. break;
  735. default:
  736. pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
  737. p);
  738. ret = -EINVAL;
  739. goto out;
  740. }
  741. }
  742. if (opts->discovery_nqn) {
  743. opts->kato = 0;
  744. opts->nr_io_queues = 0;
  745. opts->duplicate_connect = true;
  746. }
  747. if (ctrl_loss_tmo < 0)
  748. opts->max_reconnects = -1;
  749. else
  750. opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
  751. opts->reconnect_delay);
  752. if (!opts->host) {
  753. kref_get(&nvmf_default_host->ref);
  754. opts->host = nvmf_default_host;
  755. }
  756. uuid_copy(&opts->host->id, &hostid);
  757. out:
  758. kfree(options);
  759. return ret;
  760. }
  761. static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
  762. unsigned int required_opts)
  763. {
  764. if ((opts->mask & required_opts) != required_opts) {
  765. int i;
  766. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  767. if ((opt_tokens[i].token & required_opts) &&
  768. !(opt_tokens[i].token & opts->mask)) {
  769. pr_warn("missing parameter '%s'\n",
  770. opt_tokens[i].pattern);
  771. }
  772. }
  773. return -EINVAL;
  774. }
  775. return 0;
  776. }
  777. static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
  778. unsigned int allowed_opts)
  779. {
  780. if (opts->mask & ~allowed_opts) {
  781. int i;
  782. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  783. if ((opt_tokens[i].token & opts->mask) &&
  784. (opt_tokens[i].token & ~allowed_opts)) {
  785. pr_warn("invalid parameter '%s'\n",
  786. opt_tokens[i].pattern);
  787. }
  788. }
  789. return -EINVAL;
  790. }
  791. return 0;
  792. }
  793. void nvmf_free_options(struct nvmf_ctrl_options *opts)
  794. {
  795. nvmf_host_put(opts->host);
  796. kfree(opts->transport);
  797. kfree(opts->traddr);
  798. kfree(opts->trsvcid);
  799. kfree(opts->subsysnqn);
  800. kfree(opts->host_traddr);
  801. kfree(opts);
  802. }
  803. EXPORT_SYMBOL_GPL(nvmf_free_options);
  804. #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
  805. #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
  806. NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
  807. NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT)
  808. static struct nvme_ctrl *
  809. nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
  810. {
  811. struct nvmf_ctrl_options *opts;
  812. struct nvmf_transport_ops *ops;
  813. struct nvme_ctrl *ctrl;
  814. int ret;
  815. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  816. if (!opts)
  817. return ERR_PTR(-ENOMEM);
  818. ret = nvmf_parse_options(opts, buf);
  819. if (ret)
  820. goto out_free_opts;
  821. request_module("nvme-%s", opts->transport);
  822. /*
  823. * Check the generic options first as we need a valid transport for
  824. * the lookup below. Then clear the generic flags so that transport
  825. * drivers don't have to care about them.
  826. */
  827. ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
  828. if (ret)
  829. goto out_free_opts;
  830. opts->mask &= ~NVMF_REQUIRED_OPTS;
  831. down_read(&nvmf_transports_rwsem);
  832. ops = nvmf_lookup_transport(opts);
  833. if (!ops) {
  834. pr_info("no handler found for transport %s.\n",
  835. opts->transport);
  836. ret = -EINVAL;
  837. goto out_unlock;
  838. }
  839. if (!try_module_get(ops->module)) {
  840. ret = -EBUSY;
  841. goto out_unlock;
  842. }
  843. up_read(&nvmf_transports_rwsem);
  844. ret = nvmf_check_required_opts(opts, ops->required_opts);
  845. if (ret)
  846. goto out_module_put;
  847. ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
  848. ops->allowed_opts | ops->required_opts);
  849. if (ret)
  850. goto out_module_put;
  851. ctrl = ops->create_ctrl(dev, opts);
  852. if (IS_ERR(ctrl)) {
  853. ret = PTR_ERR(ctrl);
  854. goto out_module_put;
  855. }
  856. module_put(ops->module);
  857. return ctrl;
  858. out_module_put:
  859. module_put(ops->module);
  860. goto out_free_opts;
  861. out_unlock:
  862. up_read(&nvmf_transports_rwsem);
  863. out_free_opts:
  864. nvmf_free_options(opts);
  865. return ERR_PTR(ret);
  866. }
  867. static struct class *nvmf_class;
  868. static struct device *nvmf_device;
  869. static DEFINE_MUTEX(nvmf_dev_mutex);
  870. static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
  871. size_t count, loff_t *pos)
  872. {
  873. struct seq_file *seq_file = file->private_data;
  874. struct nvme_ctrl *ctrl;
  875. const char *buf;
  876. int ret = 0;
  877. if (count > PAGE_SIZE)
  878. return -ENOMEM;
  879. buf = memdup_user_nul(ubuf, count);
  880. if (IS_ERR(buf))
  881. return PTR_ERR(buf);
  882. mutex_lock(&nvmf_dev_mutex);
  883. if (seq_file->private) {
  884. ret = -EINVAL;
  885. goto out_unlock;
  886. }
  887. ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
  888. if (IS_ERR(ctrl)) {
  889. ret = PTR_ERR(ctrl);
  890. goto out_unlock;
  891. }
  892. seq_file->private = ctrl;
  893. out_unlock:
  894. mutex_unlock(&nvmf_dev_mutex);
  895. kfree(buf);
  896. return ret ? ret : count;
  897. }
  898. static int nvmf_dev_show(struct seq_file *seq_file, void *private)
  899. {
  900. struct nvme_ctrl *ctrl;
  901. int ret = 0;
  902. mutex_lock(&nvmf_dev_mutex);
  903. ctrl = seq_file->private;
  904. if (!ctrl) {
  905. ret = -EINVAL;
  906. goto out_unlock;
  907. }
  908. seq_printf(seq_file, "instance=%d,cntlid=%d\n",
  909. ctrl->instance, ctrl->cntlid);
  910. out_unlock:
  911. mutex_unlock(&nvmf_dev_mutex);
  912. return ret;
  913. }
  914. static int nvmf_dev_open(struct inode *inode, struct file *file)
  915. {
  916. /*
  917. * The miscdevice code initializes file->private_data, but doesn't
  918. * make use of it later.
  919. */
  920. file->private_data = NULL;
  921. return single_open(file, nvmf_dev_show, NULL);
  922. }
  923. static int nvmf_dev_release(struct inode *inode, struct file *file)
  924. {
  925. struct seq_file *seq_file = file->private_data;
  926. struct nvme_ctrl *ctrl = seq_file->private;
  927. if (ctrl)
  928. nvme_put_ctrl(ctrl);
  929. return single_release(inode, file);
  930. }
  931. static const struct file_operations nvmf_dev_fops = {
  932. .owner = THIS_MODULE,
  933. .write = nvmf_dev_write,
  934. .read = seq_read,
  935. .open = nvmf_dev_open,
  936. .release = nvmf_dev_release,
  937. };
  938. static struct miscdevice nvmf_misc = {
  939. .minor = MISC_DYNAMIC_MINOR,
  940. .name = "nvme-fabrics",
  941. .fops = &nvmf_dev_fops,
  942. };
  943. static int __init nvmf_init(void)
  944. {
  945. int ret;
  946. nvmf_default_host = nvmf_host_default();
  947. if (!nvmf_default_host)
  948. return -ENOMEM;
  949. nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
  950. if (IS_ERR(nvmf_class)) {
  951. pr_err("couldn't register class nvme-fabrics\n");
  952. ret = PTR_ERR(nvmf_class);
  953. goto out_free_host;
  954. }
  955. nvmf_device =
  956. device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
  957. if (IS_ERR(nvmf_device)) {
  958. pr_err("couldn't create nvme-fabris device!\n");
  959. ret = PTR_ERR(nvmf_device);
  960. goto out_destroy_class;
  961. }
  962. ret = misc_register(&nvmf_misc);
  963. if (ret) {
  964. pr_err("couldn't register misc device: %d\n", ret);
  965. goto out_destroy_device;
  966. }
  967. return 0;
  968. out_destroy_device:
  969. device_destroy(nvmf_class, MKDEV(0, 0));
  970. out_destroy_class:
  971. class_destroy(nvmf_class);
  972. out_free_host:
  973. nvmf_host_put(nvmf_default_host);
  974. return ret;
  975. }
  976. static void __exit nvmf_exit(void)
  977. {
  978. misc_deregister(&nvmf_misc);
  979. device_destroy(nvmf_class, MKDEV(0, 0));
  980. class_destroy(nvmf_class);
  981. nvmf_host_put(nvmf_default_host);
  982. BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
  983. BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
  984. BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
  985. BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
  986. }
  987. MODULE_LICENSE("GPL v2");
  988. module_init(nvmf_init);
  989. module_exit(nvmf_exit);