fabrics.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 DEFINE_MUTEX(nvmf_transports_mutex);
  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. goto out_unlock;
  44. host = kmalloc(sizeof(*host), GFP_KERNEL);
  45. if (!host)
  46. goto out_unlock;
  47. kref_init(&host->ref);
  48. memcpy(host->nqn, hostnqn, NVMF_NQN_SIZE);
  49. uuid_le_gen(&host->id);
  50. list_add_tail(&host->list, &nvmf_hosts);
  51. out_unlock:
  52. mutex_unlock(&nvmf_hosts_mutex);
  53. return host;
  54. }
  55. static struct nvmf_host *nvmf_host_default(void)
  56. {
  57. struct nvmf_host *host;
  58. host = kmalloc(sizeof(*host), GFP_KERNEL);
  59. if (!host)
  60. return NULL;
  61. kref_init(&host->ref);
  62. uuid_le_gen(&host->id);
  63. snprintf(host->nqn, NVMF_NQN_SIZE,
  64. "nqn.2014-08.org.nvmexpress:NVMf:uuid:%pUl", &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. return snprintf(buf, size, "traddr=%s,trsvcid=%s\n",
  92. ctrl->opts->traddr, ctrl->opts->trsvcid);
  93. }
  94. EXPORT_SYMBOL_GPL(nvmf_get_address);
  95. /**
  96. * nvmf_get_subsysnqn() - Get subsystem NQN
  97. * @ctrl: Host NVMe controller instance which we got the NQN
  98. */
  99. const char *nvmf_get_subsysnqn(struct nvme_ctrl *ctrl)
  100. {
  101. return ctrl->opts->subsysnqn;
  102. }
  103. EXPORT_SYMBOL_GPL(nvmf_get_subsysnqn);
  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. struct nvme_completion cqe;
  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, &cqe, NULL, 0, 0,
  135. NVME_QID_ANY, 0, 0);
  136. if (ret >= 0)
  137. *val = le64_to_cpu(cqe.result64);
  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. struct nvme_completion cqe;
  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, &cqe, NULL, 0, 0,
  177. NVME_QID_ANY, 0, 0);
  178. if (ret >= 0)
  179. *val = le64_to_cpu(cqe.result64);
  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. default:
  289. dev_err(ctrl->device,
  290. "Connect command failed, error wo/DNR bit: %d\n",
  291. err_sctype);
  292. break;
  293. } /* switch (err_sctype) */
  294. }
  295. /**
  296. * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
  297. * API function.
  298. * @ctrl: Host nvme controller instance used to request
  299. * a new NVMe controller allocation on the target
  300. * system and establish an NVMe Admin connection to
  301. * that controller.
  302. *
  303. * This function enables an NVMe host device to request a new allocation of
  304. * an NVMe controller resource on a target system as well establish a
  305. * fabrics-protocol connection of the NVMe Admin queue between the
  306. * host system device and the allocated NVMe controller on the
  307. * target system via a NVMe Fabrics "Connect" command.
  308. *
  309. * Return:
  310. * 0: success
  311. * > 0: NVMe error status code
  312. * < 0: Linux errno error code
  313. *
  314. */
  315. int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl)
  316. {
  317. struct nvme_command cmd;
  318. struct nvme_completion cqe;
  319. struct nvmf_connect_data *data;
  320. int ret;
  321. memset(&cmd, 0, sizeof(cmd));
  322. cmd.connect.opcode = nvme_fabrics_command;
  323. cmd.connect.fctype = nvme_fabrics_type_connect;
  324. cmd.connect.qid = 0;
  325. cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
  326. /*
  327. * Set keep-alive timeout in seconds granularity (ms * 1000)
  328. * and add a grace period for controller kato enforcement
  329. */
  330. cmd.connect.kato = ctrl->opts->discovery_nqn ? 0 :
  331. cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000);
  332. data = kzalloc(sizeof(*data), GFP_KERNEL);
  333. if (!data)
  334. return -ENOMEM;
  335. memcpy(&data->hostid, &ctrl->opts->host->id, sizeof(uuid_le));
  336. data->cntlid = cpu_to_le16(0xffff);
  337. strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
  338. strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
  339. ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &cqe,
  340. data, sizeof(*data), 0, NVME_QID_ANY, 1,
  341. BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
  342. if (ret) {
  343. nvmf_log_connect_error(ctrl, ret, le32_to_cpu(cqe.result),
  344. &cmd, data);
  345. goto out_free_data;
  346. }
  347. ctrl->cntlid = le16_to_cpu(cqe.result16);
  348. out_free_data:
  349. kfree(data);
  350. return ret;
  351. }
  352. EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
  353. /**
  354. * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
  355. * API function.
  356. * @ctrl: Host nvme controller instance used to establish an
  357. * NVMe I/O queue connection to the already allocated NVMe
  358. * controller on the target system.
  359. * @qid: NVMe I/O queue number for the new I/O connection between
  360. * host and target (note qid == 0 is illegal as this is
  361. * the Admin queue, per NVMe standard).
  362. *
  363. * This function issues a fabrics-protocol connection
  364. * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
  365. * between the host system device and the allocated NVMe controller
  366. * on the target system.
  367. *
  368. * Return:
  369. * 0: success
  370. * > 0: NVMe error status code
  371. * < 0: Linux errno error code
  372. */
  373. int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid)
  374. {
  375. struct nvme_command cmd;
  376. struct nvmf_connect_data *data;
  377. struct nvme_completion cqe;
  378. int ret;
  379. memset(&cmd, 0, sizeof(cmd));
  380. cmd.connect.opcode = nvme_fabrics_command;
  381. cmd.connect.fctype = nvme_fabrics_type_connect;
  382. cmd.connect.qid = cpu_to_le16(qid);
  383. cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
  384. data = kzalloc(sizeof(*data), GFP_KERNEL);
  385. if (!data)
  386. return -ENOMEM;
  387. memcpy(&data->hostid, &ctrl->opts->host->id, sizeof(uuid_le));
  388. data->cntlid = cpu_to_le16(ctrl->cntlid);
  389. strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
  390. strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
  391. ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &cqe,
  392. data, sizeof(*data), 0, qid, 1,
  393. BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
  394. if (ret) {
  395. nvmf_log_connect_error(ctrl, ret, le32_to_cpu(cqe.result),
  396. &cmd, data);
  397. }
  398. kfree(data);
  399. return ret;
  400. }
  401. EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
  402. /**
  403. * nvmf_register_transport() - NVMe Fabrics Library registration function.
  404. * @ops: Transport ops instance to be registered to the
  405. * common fabrics library.
  406. *
  407. * API function that registers the type of specific transport fabric
  408. * being implemented to the common NVMe fabrics library. Part of
  409. * the overall init sequence of starting up a fabrics driver.
  410. */
  411. void nvmf_register_transport(struct nvmf_transport_ops *ops)
  412. {
  413. mutex_lock(&nvmf_transports_mutex);
  414. list_add_tail(&ops->entry, &nvmf_transports);
  415. mutex_unlock(&nvmf_transports_mutex);
  416. }
  417. EXPORT_SYMBOL_GPL(nvmf_register_transport);
  418. /**
  419. * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
  420. * @ops: Transport ops instance to be unregistered from the
  421. * common fabrics library.
  422. *
  423. * Fabrics API function that unregisters the type of specific transport
  424. * fabric being implemented from the common NVMe fabrics library.
  425. * Part of the overall exit sequence of unloading the implemented driver.
  426. */
  427. void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
  428. {
  429. mutex_lock(&nvmf_transports_mutex);
  430. list_del(&ops->entry);
  431. mutex_unlock(&nvmf_transports_mutex);
  432. }
  433. EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
  434. static struct nvmf_transport_ops *nvmf_lookup_transport(
  435. struct nvmf_ctrl_options *opts)
  436. {
  437. struct nvmf_transport_ops *ops;
  438. lockdep_assert_held(&nvmf_transports_mutex);
  439. list_for_each_entry(ops, &nvmf_transports, entry) {
  440. if (strcmp(ops->name, opts->transport) == 0)
  441. return ops;
  442. }
  443. return NULL;
  444. }
  445. static const match_table_t opt_tokens = {
  446. { NVMF_OPT_TRANSPORT, "transport=%s" },
  447. { NVMF_OPT_TRADDR, "traddr=%s" },
  448. { NVMF_OPT_TRSVCID, "trsvcid=%s" },
  449. { NVMF_OPT_NQN, "nqn=%s" },
  450. { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
  451. { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
  452. { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
  453. { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
  454. { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
  455. { NVMF_OPT_ERR, NULL }
  456. };
  457. static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  458. const char *buf)
  459. {
  460. substring_t args[MAX_OPT_ARGS];
  461. char *options, *o, *p;
  462. int token, ret = 0;
  463. size_t nqnlen = 0;
  464. /* Set defaults */
  465. opts->queue_size = NVMF_DEF_QUEUE_SIZE;
  466. opts->nr_io_queues = num_online_cpus();
  467. opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
  468. options = o = kstrdup(buf, GFP_KERNEL);
  469. if (!options)
  470. return -ENOMEM;
  471. while ((p = strsep(&o, ",\n")) != NULL) {
  472. if (!*p)
  473. continue;
  474. token = match_token(p, opt_tokens, args);
  475. opts->mask |= token;
  476. switch (token) {
  477. case NVMF_OPT_TRANSPORT:
  478. p = match_strdup(args);
  479. if (!p) {
  480. ret = -ENOMEM;
  481. goto out;
  482. }
  483. opts->transport = p;
  484. break;
  485. case NVMF_OPT_NQN:
  486. p = match_strdup(args);
  487. if (!p) {
  488. ret = -ENOMEM;
  489. goto out;
  490. }
  491. opts->subsysnqn = p;
  492. nqnlen = strlen(opts->subsysnqn);
  493. if (nqnlen >= NVMF_NQN_SIZE) {
  494. pr_err("%s needs to be < %d bytes\n",
  495. opts->subsysnqn, NVMF_NQN_SIZE);
  496. ret = -EINVAL;
  497. goto out;
  498. }
  499. opts->discovery_nqn =
  500. !(strcmp(opts->subsysnqn,
  501. NVME_DISC_SUBSYS_NAME));
  502. if (opts->discovery_nqn)
  503. opts->nr_io_queues = 0;
  504. break;
  505. case NVMF_OPT_TRADDR:
  506. p = match_strdup(args);
  507. if (!p) {
  508. ret = -ENOMEM;
  509. goto out;
  510. }
  511. opts->traddr = p;
  512. break;
  513. case NVMF_OPT_TRSVCID:
  514. p = match_strdup(args);
  515. if (!p) {
  516. ret = -ENOMEM;
  517. goto out;
  518. }
  519. opts->trsvcid = p;
  520. break;
  521. case NVMF_OPT_QUEUE_SIZE:
  522. if (match_int(args, &token)) {
  523. ret = -EINVAL;
  524. goto out;
  525. }
  526. if (token < NVMF_MIN_QUEUE_SIZE ||
  527. token > NVMF_MAX_QUEUE_SIZE) {
  528. pr_err("Invalid queue_size %d\n", token);
  529. ret = -EINVAL;
  530. goto out;
  531. }
  532. opts->queue_size = token;
  533. break;
  534. case NVMF_OPT_NR_IO_QUEUES:
  535. if (match_int(args, &token)) {
  536. ret = -EINVAL;
  537. goto out;
  538. }
  539. if (token <= 0) {
  540. pr_err("Invalid number of IOQs %d\n", token);
  541. ret = -EINVAL;
  542. goto out;
  543. }
  544. opts->nr_io_queues = min_t(unsigned int,
  545. num_online_cpus(), token);
  546. break;
  547. case NVMF_OPT_KATO:
  548. if (match_int(args, &token)) {
  549. ret = -EINVAL;
  550. goto out;
  551. }
  552. if (opts->discovery_nqn) {
  553. pr_err("Discovery controllers cannot accept keep_alive_tmo != 0\n");
  554. ret = -EINVAL;
  555. goto out;
  556. }
  557. if (token < 0) {
  558. pr_err("Invalid keep_alive_tmo %d\n", token);
  559. ret = -EINVAL;
  560. goto out;
  561. } else if (token == 0) {
  562. /* Allowed for debug */
  563. pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
  564. }
  565. opts->kato = token;
  566. break;
  567. case NVMF_OPT_HOSTNQN:
  568. if (opts->host) {
  569. pr_err("hostnqn already user-assigned: %s\n",
  570. opts->host->nqn);
  571. ret = -EADDRINUSE;
  572. goto out;
  573. }
  574. p = match_strdup(args);
  575. if (!p) {
  576. ret = -ENOMEM;
  577. goto out;
  578. }
  579. nqnlen = strlen(p);
  580. if (nqnlen >= NVMF_NQN_SIZE) {
  581. pr_err("%s needs to be < %d bytes\n",
  582. p, NVMF_NQN_SIZE);
  583. ret = -EINVAL;
  584. goto out;
  585. }
  586. opts->host = nvmf_host_add(p);
  587. if (!opts->host) {
  588. ret = -ENOMEM;
  589. goto out;
  590. }
  591. break;
  592. case NVMF_OPT_RECONNECT_DELAY:
  593. if (match_int(args, &token)) {
  594. ret = -EINVAL;
  595. goto out;
  596. }
  597. if (token <= 0) {
  598. pr_err("Invalid reconnect_delay %d\n", token);
  599. ret = -EINVAL;
  600. goto out;
  601. }
  602. opts->reconnect_delay = token;
  603. break;
  604. default:
  605. pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
  606. p);
  607. ret = -EINVAL;
  608. goto out;
  609. }
  610. }
  611. if (!opts->host) {
  612. kref_get(&nvmf_default_host->ref);
  613. opts->host = nvmf_default_host;
  614. }
  615. out:
  616. if (!opts->discovery_nqn && !opts->kato)
  617. opts->kato = NVME_DEFAULT_KATO;
  618. kfree(options);
  619. return ret;
  620. }
  621. static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
  622. unsigned int required_opts)
  623. {
  624. if ((opts->mask & required_opts) != required_opts) {
  625. int i;
  626. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  627. if ((opt_tokens[i].token & required_opts) &&
  628. !(opt_tokens[i].token & opts->mask)) {
  629. pr_warn("missing parameter '%s'\n",
  630. opt_tokens[i].pattern);
  631. }
  632. }
  633. return -EINVAL;
  634. }
  635. return 0;
  636. }
  637. static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
  638. unsigned int allowed_opts)
  639. {
  640. if (opts->mask & ~allowed_opts) {
  641. int i;
  642. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  643. if (opt_tokens[i].token & ~allowed_opts) {
  644. pr_warn("invalid parameter '%s'\n",
  645. opt_tokens[i].pattern);
  646. }
  647. }
  648. return -EINVAL;
  649. }
  650. return 0;
  651. }
  652. void nvmf_free_options(struct nvmf_ctrl_options *opts)
  653. {
  654. nvmf_host_put(opts->host);
  655. kfree(opts->transport);
  656. kfree(opts->traddr);
  657. kfree(opts->trsvcid);
  658. kfree(opts->subsysnqn);
  659. kfree(opts);
  660. }
  661. EXPORT_SYMBOL_GPL(nvmf_free_options);
  662. #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
  663. #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
  664. NVMF_OPT_KATO | NVMF_OPT_HOSTNQN)
  665. static struct nvme_ctrl *
  666. nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
  667. {
  668. struct nvmf_ctrl_options *opts;
  669. struct nvmf_transport_ops *ops;
  670. struct nvme_ctrl *ctrl;
  671. int ret;
  672. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  673. if (!opts)
  674. return ERR_PTR(-ENOMEM);
  675. ret = nvmf_parse_options(opts, buf);
  676. if (ret)
  677. goto out_free_opts;
  678. /*
  679. * Check the generic options first as we need a valid transport for
  680. * the lookup below. Then clear the generic flags so that transport
  681. * drivers don't have to care about them.
  682. */
  683. ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
  684. if (ret)
  685. goto out_free_opts;
  686. opts->mask &= ~NVMF_REQUIRED_OPTS;
  687. mutex_lock(&nvmf_transports_mutex);
  688. ops = nvmf_lookup_transport(opts);
  689. if (!ops) {
  690. pr_info("no handler found for transport %s.\n",
  691. opts->transport);
  692. ret = -EINVAL;
  693. goto out_unlock;
  694. }
  695. ret = nvmf_check_required_opts(opts, ops->required_opts);
  696. if (ret)
  697. goto out_unlock;
  698. ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
  699. ops->allowed_opts | ops->required_opts);
  700. if (ret)
  701. goto out_unlock;
  702. ctrl = ops->create_ctrl(dev, opts);
  703. if (IS_ERR(ctrl)) {
  704. ret = PTR_ERR(ctrl);
  705. goto out_unlock;
  706. }
  707. mutex_unlock(&nvmf_transports_mutex);
  708. return ctrl;
  709. out_unlock:
  710. mutex_unlock(&nvmf_transports_mutex);
  711. out_free_opts:
  712. nvmf_host_put(opts->host);
  713. kfree(opts);
  714. return ERR_PTR(ret);
  715. }
  716. static struct class *nvmf_class;
  717. static struct device *nvmf_device;
  718. static DEFINE_MUTEX(nvmf_dev_mutex);
  719. static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
  720. size_t count, loff_t *pos)
  721. {
  722. struct seq_file *seq_file = file->private_data;
  723. struct nvme_ctrl *ctrl;
  724. const char *buf;
  725. int ret = 0;
  726. if (count > PAGE_SIZE)
  727. return -ENOMEM;
  728. buf = memdup_user_nul(ubuf, count);
  729. if (IS_ERR(buf))
  730. return PTR_ERR(buf);
  731. mutex_lock(&nvmf_dev_mutex);
  732. if (seq_file->private) {
  733. ret = -EINVAL;
  734. goto out_unlock;
  735. }
  736. ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
  737. if (IS_ERR(ctrl)) {
  738. ret = PTR_ERR(ctrl);
  739. goto out_unlock;
  740. }
  741. seq_file->private = ctrl;
  742. out_unlock:
  743. mutex_unlock(&nvmf_dev_mutex);
  744. kfree(buf);
  745. return ret ? ret : count;
  746. }
  747. static int nvmf_dev_show(struct seq_file *seq_file, void *private)
  748. {
  749. struct nvme_ctrl *ctrl;
  750. int ret = 0;
  751. mutex_lock(&nvmf_dev_mutex);
  752. ctrl = seq_file->private;
  753. if (!ctrl) {
  754. ret = -EINVAL;
  755. goto out_unlock;
  756. }
  757. seq_printf(seq_file, "instance=%d,cntlid=%d\n",
  758. ctrl->instance, ctrl->cntlid);
  759. out_unlock:
  760. mutex_unlock(&nvmf_dev_mutex);
  761. return ret;
  762. }
  763. static int nvmf_dev_open(struct inode *inode, struct file *file)
  764. {
  765. /*
  766. * The miscdevice code initializes file->private_data, but doesn't
  767. * make use of it later.
  768. */
  769. file->private_data = NULL;
  770. return single_open(file, nvmf_dev_show, NULL);
  771. }
  772. static int nvmf_dev_release(struct inode *inode, struct file *file)
  773. {
  774. struct seq_file *seq_file = file->private_data;
  775. struct nvme_ctrl *ctrl = seq_file->private;
  776. if (ctrl)
  777. nvme_put_ctrl(ctrl);
  778. return single_release(inode, file);
  779. }
  780. static const struct file_operations nvmf_dev_fops = {
  781. .owner = THIS_MODULE,
  782. .write = nvmf_dev_write,
  783. .read = seq_read,
  784. .open = nvmf_dev_open,
  785. .release = nvmf_dev_release,
  786. };
  787. static struct miscdevice nvmf_misc = {
  788. .minor = MISC_DYNAMIC_MINOR,
  789. .name = "nvme-fabrics",
  790. .fops = &nvmf_dev_fops,
  791. };
  792. static int __init nvmf_init(void)
  793. {
  794. int ret;
  795. nvmf_default_host = nvmf_host_default();
  796. if (!nvmf_default_host)
  797. return -ENOMEM;
  798. nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
  799. if (IS_ERR(nvmf_class)) {
  800. pr_err("couldn't register class nvme-fabrics\n");
  801. ret = PTR_ERR(nvmf_class);
  802. goto out_free_host;
  803. }
  804. nvmf_device =
  805. device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
  806. if (IS_ERR(nvmf_device)) {
  807. pr_err("couldn't create nvme-fabris device!\n");
  808. ret = PTR_ERR(nvmf_device);
  809. goto out_destroy_class;
  810. }
  811. ret = misc_register(&nvmf_misc);
  812. if (ret) {
  813. pr_err("couldn't register misc device: %d\n", ret);
  814. goto out_destroy_device;
  815. }
  816. return 0;
  817. out_destroy_device:
  818. device_destroy(nvmf_class, MKDEV(0, 0));
  819. out_destroy_class:
  820. class_destroy(nvmf_class);
  821. out_free_host:
  822. nvmf_host_put(nvmf_default_host);
  823. return ret;
  824. }
  825. static void __exit nvmf_exit(void)
  826. {
  827. misc_deregister(&nvmf_misc);
  828. device_destroy(nvmf_class, MKDEV(0, 0));
  829. class_destroy(nvmf_class);
  830. nvmf_host_put(nvmf_default_host);
  831. BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
  832. BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
  833. BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
  834. BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
  835. }
  836. MODULE_LICENSE("GPL v2");
  837. module_init(nvmf_init);
  838. module_exit(nvmf_exit);