fabrics.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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:NVMf: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. union nvme_result res;
  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, &res, NULL, 0, 0,
  146. NVME_QID_ANY, 0, 0);
  147. if (ret >= 0)
  148. *val = le64_to_cpu(res.u64);
  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. union nvme_result res;
  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, &res, NULL, 0, 0,
  188. NVME_QID_ANY, 0, 0);
  189. if (ret >= 0)
  190. *val = le64_to_cpu(res.u64);
  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. union nvme_result res;
  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, &res,
  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(res.u32),
  360. &cmd, data);
  361. goto out_free_data;
  362. }
  363. ctrl->cntlid = le16_to_cpu(res.u16);
  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. union nvme_result res;
  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, &res,
  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(res.u32),
  412. &cmd, data);
  413. }
  414. kfree(data);
  415. return ret;
  416. }
  417. EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
  418. bool nvmf_should_reconnect(struct nvme_ctrl *ctrl)
  419. {
  420. if (ctrl->opts->max_reconnects != -1 &&
  421. ctrl->opts->nr_reconnects < ctrl->opts->max_reconnects)
  422. return true;
  423. return false;
  424. }
  425. EXPORT_SYMBOL_GPL(nvmf_should_reconnect);
  426. /**
  427. * nvmf_register_transport() - NVMe Fabrics Library registration function.
  428. * @ops: Transport ops instance to be registered to the
  429. * common fabrics library.
  430. *
  431. * API function that registers the type of specific transport fabric
  432. * being implemented to the common NVMe fabrics library. Part of
  433. * the overall init sequence of starting up a fabrics driver.
  434. */
  435. int nvmf_register_transport(struct nvmf_transport_ops *ops)
  436. {
  437. if (!ops->create_ctrl)
  438. return -EINVAL;
  439. mutex_lock(&nvmf_transports_mutex);
  440. list_add_tail(&ops->entry, &nvmf_transports);
  441. mutex_unlock(&nvmf_transports_mutex);
  442. return 0;
  443. }
  444. EXPORT_SYMBOL_GPL(nvmf_register_transport);
  445. /**
  446. * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
  447. * @ops: Transport ops instance to be unregistered from the
  448. * common fabrics library.
  449. *
  450. * Fabrics API function that unregisters the type of specific transport
  451. * fabric being implemented from the common NVMe fabrics library.
  452. * Part of the overall exit sequence of unloading the implemented driver.
  453. */
  454. void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
  455. {
  456. mutex_lock(&nvmf_transports_mutex);
  457. list_del(&ops->entry);
  458. mutex_unlock(&nvmf_transports_mutex);
  459. }
  460. EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
  461. static struct nvmf_transport_ops *nvmf_lookup_transport(
  462. struct nvmf_ctrl_options *opts)
  463. {
  464. struct nvmf_transport_ops *ops;
  465. lockdep_assert_held(&nvmf_transports_mutex);
  466. list_for_each_entry(ops, &nvmf_transports, entry) {
  467. if (strcmp(ops->name, opts->transport) == 0)
  468. return ops;
  469. }
  470. return NULL;
  471. }
  472. static const match_table_t opt_tokens = {
  473. { NVMF_OPT_TRANSPORT, "transport=%s" },
  474. { NVMF_OPT_TRADDR, "traddr=%s" },
  475. { NVMF_OPT_TRSVCID, "trsvcid=%s" },
  476. { NVMF_OPT_NQN, "nqn=%s" },
  477. { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
  478. { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
  479. { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
  480. { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" },
  481. { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
  482. { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
  483. { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
  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. /* Set defaults */
  495. opts->queue_size = NVMF_DEF_QUEUE_SIZE;
  496. opts->nr_io_queues = num_online_cpus();
  497. opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
  498. options = o = kstrdup(buf, GFP_KERNEL);
  499. if (!options)
  500. return -ENOMEM;
  501. while ((p = strsep(&o, ",\n")) != NULL) {
  502. if (!*p)
  503. continue;
  504. token = match_token(p, opt_tokens, args);
  505. opts->mask |= token;
  506. switch (token) {
  507. case NVMF_OPT_TRANSPORT:
  508. p = match_strdup(args);
  509. if (!p) {
  510. ret = -ENOMEM;
  511. goto out;
  512. }
  513. opts->transport = p;
  514. break;
  515. case NVMF_OPT_NQN:
  516. p = match_strdup(args);
  517. if (!p) {
  518. ret = -ENOMEM;
  519. goto out;
  520. }
  521. opts->subsysnqn = p;
  522. nqnlen = strlen(opts->subsysnqn);
  523. if (nqnlen >= NVMF_NQN_SIZE) {
  524. pr_err("%s needs to be < %d bytes\n",
  525. opts->subsysnqn, NVMF_NQN_SIZE);
  526. ret = -EINVAL;
  527. goto out;
  528. }
  529. opts->discovery_nqn =
  530. !(strcmp(opts->subsysnqn,
  531. NVME_DISC_SUBSYS_NAME));
  532. if (opts->discovery_nqn)
  533. opts->nr_io_queues = 0;
  534. break;
  535. case NVMF_OPT_TRADDR:
  536. p = match_strdup(args);
  537. if (!p) {
  538. ret = -ENOMEM;
  539. goto out;
  540. }
  541. opts->traddr = p;
  542. break;
  543. case NVMF_OPT_TRSVCID:
  544. p = match_strdup(args);
  545. if (!p) {
  546. ret = -ENOMEM;
  547. goto out;
  548. }
  549. opts->trsvcid = p;
  550. break;
  551. case NVMF_OPT_QUEUE_SIZE:
  552. if (match_int(args, &token)) {
  553. ret = -EINVAL;
  554. goto out;
  555. }
  556. if (token < NVMF_MIN_QUEUE_SIZE ||
  557. token > NVMF_MAX_QUEUE_SIZE) {
  558. pr_err("Invalid queue_size %d\n", token);
  559. ret = -EINVAL;
  560. goto out;
  561. }
  562. opts->queue_size = token;
  563. break;
  564. case NVMF_OPT_NR_IO_QUEUES:
  565. if (match_int(args, &token)) {
  566. ret = -EINVAL;
  567. goto out;
  568. }
  569. if (token <= 0) {
  570. pr_err("Invalid number of IOQs %d\n", token);
  571. ret = -EINVAL;
  572. goto out;
  573. }
  574. opts->nr_io_queues = min_t(unsigned int,
  575. num_online_cpus(), token);
  576. break;
  577. case NVMF_OPT_KATO:
  578. if (match_int(args, &token)) {
  579. ret = -EINVAL;
  580. goto out;
  581. }
  582. if (opts->discovery_nqn) {
  583. pr_err("Discovery controllers cannot accept keep_alive_tmo != 0\n");
  584. ret = -EINVAL;
  585. goto out;
  586. }
  587. if (token < 0) {
  588. pr_err("Invalid keep_alive_tmo %d\n", token);
  589. ret = -EINVAL;
  590. goto out;
  591. } else if (token == 0) {
  592. /* Allowed for debug */
  593. pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
  594. }
  595. opts->kato = token;
  596. break;
  597. case NVMF_OPT_CTRL_LOSS_TMO:
  598. if (match_int(args, &token)) {
  599. ret = -EINVAL;
  600. goto out;
  601. }
  602. if (token < 0)
  603. pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
  604. ctrl_loss_tmo = token;
  605. break;
  606. case NVMF_OPT_HOSTNQN:
  607. if (opts->host) {
  608. pr_err("hostnqn already user-assigned: %s\n",
  609. opts->host->nqn);
  610. ret = -EADDRINUSE;
  611. goto out;
  612. }
  613. p = match_strdup(args);
  614. if (!p) {
  615. ret = -ENOMEM;
  616. goto out;
  617. }
  618. nqnlen = strlen(p);
  619. if (nqnlen >= NVMF_NQN_SIZE) {
  620. pr_err("%s needs to be < %d bytes\n",
  621. p, NVMF_NQN_SIZE);
  622. kfree(p);
  623. ret = -EINVAL;
  624. goto out;
  625. }
  626. opts->host = nvmf_host_add(p);
  627. kfree(p);
  628. if (!opts->host) {
  629. ret = -ENOMEM;
  630. goto out;
  631. }
  632. break;
  633. case NVMF_OPT_RECONNECT_DELAY:
  634. if (match_int(args, &token)) {
  635. ret = -EINVAL;
  636. goto out;
  637. }
  638. if (token <= 0) {
  639. pr_err("Invalid reconnect_delay %d\n", token);
  640. ret = -EINVAL;
  641. goto out;
  642. }
  643. opts->reconnect_delay = token;
  644. break;
  645. case NVMF_OPT_HOST_TRADDR:
  646. p = match_strdup(args);
  647. if (!p) {
  648. ret = -ENOMEM;
  649. goto out;
  650. }
  651. opts->host_traddr = p;
  652. break;
  653. default:
  654. pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
  655. p);
  656. ret = -EINVAL;
  657. goto out;
  658. }
  659. }
  660. if (ctrl_loss_tmo < 0)
  661. opts->max_reconnects = -1;
  662. else
  663. opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
  664. opts->reconnect_delay);
  665. if (!opts->host) {
  666. kref_get(&nvmf_default_host->ref);
  667. opts->host = nvmf_default_host;
  668. }
  669. out:
  670. if (!opts->discovery_nqn && !opts->kato)
  671. opts->kato = NVME_DEFAULT_KATO;
  672. kfree(options);
  673. return ret;
  674. }
  675. static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
  676. unsigned int required_opts)
  677. {
  678. if ((opts->mask & required_opts) != required_opts) {
  679. int i;
  680. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  681. if ((opt_tokens[i].token & required_opts) &&
  682. !(opt_tokens[i].token & opts->mask)) {
  683. pr_warn("missing parameter '%s'\n",
  684. opt_tokens[i].pattern);
  685. }
  686. }
  687. return -EINVAL;
  688. }
  689. return 0;
  690. }
  691. static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
  692. unsigned int allowed_opts)
  693. {
  694. if (opts->mask & ~allowed_opts) {
  695. int i;
  696. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  697. if (opt_tokens[i].token & ~allowed_opts) {
  698. pr_warn("invalid parameter '%s'\n",
  699. opt_tokens[i].pattern);
  700. }
  701. }
  702. return -EINVAL;
  703. }
  704. return 0;
  705. }
  706. void nvmf_free_options(struct nvmf_ctrl_options *opts)
  707. {
  708. nvmf_host_put(opts->host);
  709. kfree(opts->transport);
  710. kfree(opts->traddr);
  711. kfree(opts->trsvcid);
  712. kfree(opts->subsysnqn);
  713. kfree(opts->host_traddr);
  714. kfree(opts);
  715. }
  716. EXPORT_SYMBOL_GPL(nvmf_free_options);
  717. #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
  718. #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
  719. NVMF_OPT_KATO | NVMF_OPT_HOSTNQN)
  720. static struct nvme_ctrl *
  721. nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
  722. {
  723. struct nvmf_ctrl_options *opts;
  724. struct nvmf_transport_ops *ops;
  725. struct nvme_ctrl *ctrl;
  726. int ret;
  727. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  728. if (!opts)
  729. return ERR_PTR(-ENOMEM);
  730. ret = nvmf_parse_options(opts, buf);
  731. if (ret)
  732. goto out_free_opts;
  733. /*
  734. * Check the generic options first as we need a valid transport for
  735. * the lookup below. Then clear the generic flags so that transport
  736. * drivers don't have to care about them.
  737. */
  738. ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
  739. if (ret)
  740. goto out_free_opts;
  741. opts->mask &= ~NVMF_REQUIRED_OPTS;
  742. mutex_lock(&nvmf_transports_mutex);
  743. ops = nvmf_lookup_transport(opts);
  744. if (!ops) {
  745. pr_info("no handler found for transport %s.\n",
  746. opts->transport);
  747. ret = -EINVAL;
  748. goto out_unlock;
  749. }
  750. ret = nvmf_check_required_opts(opts, ops->required_opts);
  751. if (ret)
  752. goto out_unlock;
  753. ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
  754. ops->allowed_opts | ops->required_opts);
  755. if (ret)
  756. goto out_unlock;
  757. ctrl = ops->create_ctrl(dev, opts);
  758. if (IS_ERR(ctrl)) {
  759. ret = PTR_ERR(ctrl);
  760. goto out_unlock;
  761. }
  762. mutex_unlock(&nvmf_transports_mutex);
  763. return ctrl;
  764. out_unlock:
  765. mutex_unlock(&nvmf_transports_mutex);
  766. out_free_opts:
  767. nvmf_free_options(opts);
  768. return ERR_PTR(ret);
  769. }
  770. static struct class *nvmf_class;
  771. static struct device *nvmf_device;
  772. static DEFINE_MUTEX(nvmf_dev_mutex);
  773. static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
  774. size_t count, loff_t *pos)
  775. {
  776. struct seq_file *seq_file = file->private_data;
  777. struct nvme_ctrl *ctrl;
  778. const char *buf;
  779. int ret = 0;
  780. if (count > PAGE_SIZE)
  781. return -ENOMEM;
  782. buf = memdup_user_nul(ubuf, count);
  783. if (IS_ERR(buf))
  784. return PTR_ERR(buf);
  785. mutex_lock(&nvmf_dev_mutex);
  786. if (seq_file->private) {
  787. ret = -EINVAL;
  788. goto out_unlock;
  789. }
  790. ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
  791. if (IS_ERR(ctrl)) {
  792. ret = PTR_ERR(ctrl);
  793. goto out_unlock;
  794. }
  795. seq_file->private = ctrl;
  796. out_unlock:
  797. mutex_unlock(&nvmf_dev_mutex);
  798. kfree(buf);
  799. return ret ? ret : count;
  800. }
  801. static int nvmf_dev_show(struct seq_file *seq_file, void *private)
  802. {
  803. struct nvme_ctrl *ctrl;
  804. int ret = 0;
  805. mutex_lock(&nvmf_dev_mutex);
  806. ctrl = seq_file->private;
  807. if (!ctrl) {
  808. ret = -EINVAL;
  809. goto out_unlock;
  810. }
  811. seq_printf(seq_file, "instance=%d,cntlid=%d\n",
  812. ctrl->instance, ctrl->cntlid);
  813. out_unlock:
  814. mutex_unlock(&nvmf_dev_mutex);
  815. return ret;
  816. }
  817. static int nvmf_dev_open(struct inode *inode, struct file *file)
  818. {
  819. /*
  820. * The miscdevice code initializes file->private_data, but doesn't
  821. * make use of it later.
  822. */
  823. file->private_data = NULL;
  824. return single_open(file, nvmf_dev_show, NULL);
  825. }
  826. static int nvmf_dev_release(struct inode *inode, struct file *file)
  827. {
  828. struct seq_file *seq_file = file->private_data;
  829. struct nvme_ctrl *ctrl = seq_file->private;
  830. if (ctrl)
  831. nvme_put_ctrl(ctrl);
  832. return single_release(inode, file);
  833. }
  834. static const struct file_operations nvmf_dev_fops = {
  835. .owner = THIS_MODULE,
  836. .write = nvmf_dev_write,
  837. .read = seq_read,
  838. .open = nvmf_dev_open,
  839. .release = nvmf_dev_release,
  840. };
  841. static struct miscdevice nvmf_misc = {
  842. .minor = MISC_DYNAMIC_MINOR,
  843. .name = "nvme-fabrics",
  844. .fops = &nvmf_dev_fops,
  845. };
  846. static int __init nvmf_init(void)
  847. {
  848. int ret;
  849. nvmf_default_host = nvmf_host_default();
  850. if (!nvmf_default_host)
  851. return -ENOMEM;
  852. nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
  853. if (IS_ERR(nvmf_class)) {
  854. pr_err("couldn't register class nvme-fabrics\n");
  855. ret = PTR_ERR(nvmf_class);
  856. goto out_free_host;
  857. }
  858. nvmf_device =
  859. device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
  860. if (IS_ERR(nvmf_device)) {
  861. pr_err("couldn't create nvme-fabris device!\n");
  862. ret = PTR_ERR(nvmf_device);
  863. goto out_destroy_class;
  864. }
  865. ret = misc_register(&nvmf_misc);
  866. if (ret) {
  867. pr_err("couldn't register misc device: %d\n", ret);
  868. goto out_destroy_device;
  869. }
  870. return 0;
  871. out_destroy_device:
  872. device_destroy(nvmf_class, MKDEV(0, 0));
  873. out_destroy_class:
  874. class_destroy(nvmf_class);
  875. out_free_host:
  876. nvmf_host_put(nvmf_default_host);
  877. return ret;
  878. }
  879. static void __exit nvmf_exit(void)
  880. {
  881. misc_deregister(&nvmf_misc);
  882. device_destroy(nvmf_class, MKDEV(0, 0));
  883. class_destroy(nvmf_class);
  884. nvmf_host_put(nvmf_default_host);
  885. BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
  886. BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
  887. BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
  888. BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
  889. }
  890. MODULE_LICENSE("GPL v2");
  891. module_init(nvmf_init);
  892. module_exit(nvmf_exit);