fabrics.c 26 KB

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