fabrics.c 25 KB

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