fabrics.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. bool nvmf_ip_options_match(struct nvme_ctrl *ctrl,
  778. struct nvmf_ctrl_options *opts)
  779. {
  780. if (!nvmf_ctlr_matches_baseopts(ctrl, opts) ||
  781. strcmp(opts->traddr, ctrl->opts->traddr) ||
  782. strcmp(opts->trsvcid, ctrl->opts->trsvcid))
  783. return false;
  784. /*
  785. * Checking the local address is rough. In most cases, none is specified
  786. * and the host port is selected by the stack.
  787. *
  788. * Assume no match if:
  789. * - local address is specified and address is not the same
  790. * - local address is not specified but remote is, or vice versa
  791. * (admin using specific host_traddr when it matters).
  792. */
  793. if ((opts->mask & NVMF_OPT_HOST_TRADDR) &&
  794. (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) {
  795. if (strcmp(opts->host_traddr, ctrl->opts->host_traddr))
  796. return false;
  797. } else if ((opts->mask & NVMF_OPT_HOST_TRADDR) ||
  798. (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)) {
  799. return false;
  800. }
  801. return true;
  802. }
  803. EXPORT_SYMBOL_GPL(nvmf_ip_options_match);
  804. static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
  805. unsigned int allowed_opts)
  806. {
  807. if (opts->mask & ~allowed_opts) {
  808. int i;
  809. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  810. if ((opt_tokens[i].token & opts->mask) &&
  811. (opt_tokens[i].token & ~allowed_opts)) {
  812. pr_warn("invalid parameter '%s'\n",
  813. opt_tokens[i].pattern);
  814. }
  815. }
  816. return -EINVAL;
  817. }
  818. return 0;
  819. }
  820. void nvmf_free_options(struct nvmf_ctrl_options *opts)
  821. {
  822. nvmf_host_put(opts->host);
  823. kfree(opts->transport);
  824. kfree(opts->traddr);
  825. kfree(opts->trsvcid);
  826. kfree(opts->subsysnqn);
  827. kfree(opts->host_traddr);
  828. kfree(opts);
  829. }
  830. EXPORT_SYMBOL_GPL(nvmf_free_options);
  831. #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
  832. #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
  833. NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
  834. NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT)
  835. static struct nvme_ctrl *
  836. nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
  837. {
  838. struct nvmf_ctrl_options *opts;
  839. struct nvmf_transport_ops *ops;
  840. struct nvme_ctrl *ctrl;
  841. int ret;
  842. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  843. if (!opts)
  844. return ERR_PTR(-ENOMEM);
  845. ret = nvmf_parse_options(opts, buf);
  846. if (ret)
  847. goto out_free_opts;
  848. request_module("nvme-%s", opts->transport);
  849. /*
  850. * Check the generic options first as we need a valid transport for
  851. * the lookup below. Then clear the generic flags so that transport
  852. * drivers don't have to care about them.
  853. */
  854. ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
  855. if (ret)
  856. goto out_free_opts;
  857. opts->mask &= ~NVMF_REQUIRED_OPTS;
  858. down_read(&nvmf_transports_rwsem);
  859. ops = nvmf_lookup_transport(opts);
  860. if (!ops) {
  861. pr_info("no handler found for transport %s.\n",
  862. opts->transport);
  863. ret = -EINVAL;
  864. goto out_unlock;
  865. }
  866. if (!try_module_get(ops->module)) {
  867. ret = -EBUSY;
  868. goto out_unlock;
  869. }
  870. up_read(&nvmf_transports_rwsem);
  871. ret = nvmf_check_required_opts(opts, ops->required_opts);
  872. if (ret)
  873. goto out_module_put;
  874. ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
  875. ops->allowed_opts | ops->required_opts);
  876. if (ret)
  877. goto out_module_put;
  878. ctrl = ops->create_ctrl(dev, opts);
  879. if (IS_ERR(ctrl)) {
  880. ret = PTR_ERR(ctrl);
  881. goto out_module_put;
  882. }
  883. module_put(ops->module);
  884. return ctrl;
  885. out_module_put:
  886. module_put(ops->module);
  887. goto out_free_opts;
  888. out_unlock:
  889. up_read(&nvmf_transports_rwsem);
  890. out_free_opts:
  891. nvmf_free_options(opts);
  892. return ERR_PTR(ret);
  893. }
  894. static struct class *nvmf_class;
  895. static struct device *nvmf_device;
  896. static DEFINE_MUTEX(nvmf_dev_mutex);
  897. static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
  898. size_t count, loff_t *pos)
  899. {
  900. struct seq_file *seq_file = file->private_data;
  901. struct nvme_ctrl *ctrl;
  902. const char *buf;
  903. int ret = 0;
  904. if (count > PAGE_SIZE)
  905. return -ENOMEM;
  906. buf = memdup_user_nul(ubuf, count);
  907. if (IS_ERR(buf))
  908. return PTR_ERR(buf);
  909. mutex_lock(&nvmf_dev_mutex);
  910. if (seq_file->private) {
  911. ret = -EINVAL;
  912. goto out_unlock;
  913. }
  914. ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
  915. if (IS_ERR(ctrl)) {
  916. ret = PTR_ERR(ctrl);
  917. goto out_unlock;
  918. }
  919. seq_file->private = ctrl;
  920. out_unlock:
  921. mutex_unlock(&nvmf_dev_mutex);
  922. kfree(buf);
  923. return ret ? ret : count;
  924. }
  925. static int nvmf_dev_show(struct seq_file *seq_file, void *private)
  926. {
  927. struct nvme_ctrl *ctrl;
  928. int ret = 0;
  929. mutex_lock(&nvmf_dev_mutex);
  930. ctrl = seq_file->private;
  931. if (!ctrl) {
  932. ret = -EINVAL;
  933. goto out_unlock;
  934. }
  935. seq_printf(seq_file, "instance=%d,cntlid=%d\n",
  936. ctrl->instance, ctrl->cntlid);
  937. out_unlock:
  938. mutex_unlock(&nvmf_dev_mutex);
  939. return ret;
  940. }
  941. static int nvmf_dev_open(struct inode *inode, struct file *file)
  942. {
  943. /*
  944. * The miscdevice code initializes file->private_data, but doesn't
  945. * make use of it later.
  946. */
  947. file->private_data = NULL;
  948. return single_open(file, nvmf_dev_show, NULL);
  949. }
  950. static int nvmf_dev_release(struct inode *inode, struct file *file)
  951. {
  952. struct seq_file *seq_file = file->private_data;
  953. struct nvme_ctrl *ctrl = seq_file->private;
  954. if (ctrl)
  955. nvme_put_ctrl(ctrl);
  956. return single_release(inode, file);
  957. }
  958. static const struct file_operations nvmf_dev_fops = {
  959. .owner = THIS_MODULE,
  960. .write = nvmf_dev_write,
  961. .read = seq_read,
  962. .open = nvmf_dev_open,
  963. .release = nvmf_dev_release,
  964. };
  965. static struct miscdevice nvmf_misc = {
  966. .minor = MISC_DYNAMIC_MINOR,
  967. .name = "nvme-fabrics",
  968. .fops = &nvmf_dev_fops,
  969. };
  970. static int __init nvmf_init(void)
  971. {
  972. int ret;
  973. nvmf_default_host = nvmf_host_default();
  974. if (!nvmf_default_host)
  975. return -ENOMEM;
  976. nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
  977. if (IS_ERR(nvmf_class)) {
  978. pr_err("couldn't register class nvme-fabrics\n");
  979. ret = PTR_ERR(nvmf_class);
  980. goto out_free_host;
  981. }
  982. nvmf_device =
  983. device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
  984. if (IS_ERR(nvmf_device)) {
  985. pr_err("couldn't create nvme-fabris device!\n");
  986. ret = PTR_ERR(nvmf_device);
  987. goto out_destroy_class;
  988. }
  989. ret = misc_register(&nvmf_misc);
  990. if (ret) {
  991. pr_err("couldn't register misc device: %d\n", ret);
  992. goto out_destroy_device;
  993. }
  994. return 0;
  995. out_destroy_device:
  996. device_destroy(nvmf_class, MKDEV(0, 0));
  997. out_destroy_class:
  998. class_destroy(nvmf_class);
  999. out_free_host:
  1000. nvmf_host_put(nvmf_default_host);
  1001. return ret;
  1002. }
  1003. static void __exit nvmf_exit(void)
  1004. {
  1005. misc_deregister(&nvmf_misc);
  1006. device_destroy(nvmf_class, MKDEV(0, 0));
  1007. class_destroy(nvmf_class);
  1008. nvmf_host_put(nvmf_default_host);
  1009. BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
  1010. BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
  1011. BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
  1012. BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
  1013. }
  1014. MODULE_LICENSE("GPL v2");
  1015. module_init(nvmf_init);
  1016. module_exit(nvmf_exit);