fabrics.c 26 KB

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