fabrics.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /*
  2. * NVMe over Fabrics common host code.
  3. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/init.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/parser.h>
  20. #include <linux/seq_file.h>
  21. #include "nvme.h"
  22. #include "fabrics.h"
  23. static LIST_HEAD(nvmf_transports);
  24. static DECLARE_RWSEM(nvmf_transports_rwsem);
  25. static LIST_HEAD(nvmf_hosts);
  26. static DEFINE_MUTEX(nvmf_hosts_mutex);
  27. static struct nvmf_host *nvmf_default_host;
  28. static struct nvmf_host *__nvmf_host_find(const char *hostnqn)
  29. {
  30. struct nvmf_host *host;
  31. list_for_each_entry(host, &nvmf_hosts, list) {
  32. if (!strcmp(host->nqn, hostnqn))
  33. return host;
  34. }
  35. return NULL;
  36. }
  37. static struct nvmf_host *nvmf_host_add(const char *hostnqn)
  38. {
  39. struct nvmf_host *host;
  40. mutex_lock(&nvmf_hosts_mutex);
  41. host = __nvmf_host_find(hostnqn);
  42. if (host) {
  43. kref_get(&host->ref);
  44. goto out_unlock;
  45. }
  46. host = kmalloc(sizeof(*host), GFP_KERNEL);
  47. if (!host)
  48. goto out_unlock;
  49. kref_init(&host->ref);
  50. strlcpy(host->nqn, hostnqn, NVMF_NQN_SIZE);
  51. list_add_tail(&host->list, &nvmf_hosts);
  52. out_unlock:
  53. mutex_unlock(&nvmf_hosts_mutex);
  54. return host;
  55. }
  56. static struct nvmf_host *nvmf_host_default(void)
  57. {
  58. struct nvmf_host *host;
  59. host = kmalloc(sizeof(*host), GFP_KERNEL);
  60. if (!host)
  61. return NULL;
  62. kref_init(&host->ref);
  63. uuid_gen(&host->id);
  64. snprintf(host->nqn, NVMF_NQN_SIZE,
  65. "nqn.2014-08.org.nvmexpress:uuid:%pUb", &host->id);
  66. mutex_lock(&nvmf_hosts_mutex);
  67. list_add_tail(&host->list, &nvmf_hosts);
  68. mutex_unlock(&nvmf_hosts_mutex);
  69. return host;
  70. }
  71. static void nvmf_host_destroy(struct kref *ref)
  72. {
  73. struct nvmf_host *host = container_of(ref, struct nvmf_host, ref);
  74. mutex_lock(&nvmf_hosts_mutex);
  75. list_del(&host->list);
  76. mutex_unlock(&nvmf_hosts_mutex);
  77. kfree(host);
  78. }
  79. static void nvmf_host_put(struct nvmf_host *host)
  80. {
  81. if (host)
  82. kref_put(&host->ref, nvmf_host_destroy);
  83. }
  84. /**
  85. * nvmf_get_address() - Get address/port
  86. * @ctrl: Host NVMe controller instance which we got the address
  87. * @buf: OUTPUT parameter that will contain the address/port
  88. * @size: buffer size
  89. */
  90. int nvmf_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
  91. {
  92. int len = 0;
  93. if (ctrl->opts->mask & NVMF_OPT_TRADDR)
  94. len += snprintf(buf, size, "traddr=%s", ctrl->opts->traddr);
  95. if (ctrl->opts->mask & NVMF_OPT_TRSVCID)
  96. len += snprintf(buf + len, size - len, "%strsvcid=%s",
  97. (len) ? "," : "", ctrl->opts->trsvcid);
  98. if (ctrl->opts->mask & NVMF_OPT_HOST_TRADDR)
  99. len += snprintf(buf + len, size - len, "%shost_traddr=%s",
  100. (len) ? "," : "", ctrl->opts->host_traddr);
  101. len += snprintf(buf + len, size - len, "\n");
  102. return len;
  103. }
  104. EXPORT_SYMBOL_GPL(nvmf_get_address);
  105. /**
  106. * nvmf_reg_read32() - NVMe Fabrics "Property Get" API function.
  107. * @ctrl: Host NVMe controller instance maintaining the admin
  108. * queue used to submit the property read command to
  109. * the allocated NVMe controller resource on the target system.
  110. * @off: Starting offset value of the targeted property
  111. * register (see the fabrics section of the NVMe standard).
  112. * @val: OUTPUT parameter that will contain the value of
  113. * the property after a successful read.
  114. *
  115. * Used by the host system to retrieve a 32-bit capsule property value
  116. * from an NVMe controller on the target system.
  117. *
  118. * ("Capsule property" is an "PCIe register concept" applied to the
  119. * NVMe fabrics space.)
  120. *
  121. * Return:
  122. * 0: successful read
  123. * > 0: NVMe error status code
  124. * < 0: Linux errno error code
  125. */
  126. int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
  127. {
  128. struct nvme_command cmd;
  129. union nvme_result res;
  130. int ret;
  131. memset(&cmd, 0, sizeof(cmd));
  132. cmd.prop_get.opcode = nvme_fabrics_command;
  133. cmd.prop_get.fctype = nvme_fabrics_type_property_get;
  134. cmd.prop_get.offset = cpu_to_le32(off);
  135. ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
  136. NVME_QID_ANY, 0, 0);
  137. if (ret >= 0)
  138. *val = le64_to_cpu(res.u64);
  139. if (unlikely(ret != 0))
  140. dev_err(ctrl->device,
  141. "Property Get error: %d, offset %#x\n",
  142. ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
  143. return ret;
  144. }
  145. EXPORT_SYMBOL_GPL(nvmf_reg_read32);
  146. /**
  147. * nvmf_reg_read64() - NVMe Fabrics "Property Get" API function.
  148. * @ctrl: Host NVMe controller instance maintaining the admin
  149. * queue used to submit the property read command to
  150. * the allocated controller resource on the target system.
  151. * @off: Starting offset value of the targeted property
  152. * register (see the fabrics section of the NVMe standard).
  153. * @val: OUTPUT parameter that will contain the value of
  154. * the property after a successful read.
  155. *
  156. * Used by the host system to retrieve a 64-bit capsule property value
  157. * from an NVMe controller on the target system.
  158. *
  159. * ("Capsule property" is an "PCIe register concept" applied to the
  160. * NVMe fabrics space.)
  161. *
  162. * Return:
  163. * 0: successful read
  164. * > 0: NVMe error status code
  165. * < 0: Linux errno error code
  166. */
  167. int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
  168. {
  169. struct nvme_command cmd;
  170. union nvme_result res;
  171. int ret;
  172. memset(&cmd, 0, sizeof(cmd));
  173. cmd.prop_get.opcode = nvme_fabrics_command;
  174. cmd.prop_get.fctype = nvme_fabrics_type_property_get;
  175. cmd.prop_get.attrib = 1;
  176. cmd.prop_get.offset = cpu_to_le32(off);
  177. ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res, NULL, 0, 0,
  178. NVME_QID_ANY, 0, 0);
  179. if (ret >= 0)
  180. *val = le64_to_cpu(res.u64);
  181. if (unlikely(ret != 0))
  182. dev_err(ctrl->device,
  183. "Property Get error: %d, offset %#x\n",
  184. ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
  185. return ret;
  186. }
  187. EXPORT_SYMBOL_GPL(nvmf_reg_read64);
  188. /**
  189. * nvmf_reg_write32() - NVMe Fabrics "Property Write" API function.
  190. * @ctrl: Host NVMe controller instance maintaining the admin
  191. * queue used to submit the property read command to
  192. * the allocated NVMe controller resource on the target system.
  193. * @off: Starting offset value of the targeted property
  194. * register (see the fabrics section of the NVMe standard).
  195. * @val: Input parameter that contains the value to be
  196. * written to the property.
  197. *
  198. * Used by the NVMe host system to write a 32-bit capsule property value
  199. * to an NVMe controller on the target system.
  200. *
  201. * ("Capsule property" is an "PCIe register concept" applied to the
  202. * NVMe fabrics space.)
  203. *
  204. * Return:
  205. * 0: successful write
  206. * > 0: NVMe error status code
  207. * < 0: Linux errno error code
  208. */
  209. int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
  210. {
  211. struct nvme_command cmd;
  212. int ret;
  213. memset(&cmd, 0, sizeof(cmd));
  214. cmd.prop_set.opcode = nvme_fabrics_command;
  215. cmd.prop_set.fctype = nvme_fabrics_type_property_set;
  216. cmd.prop_set.attrib = 0;
  217. cmd.prop_set.offset = cpu_to_le32(off);
  218. cmd.prop_set.value = cpu_to_le64(val);
  219. ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, NULL, 0, 0,
  220. NVME_QID_ANY, 0, 0);
  221. if (unlikely(ret))
  222. dev_err(ctrl->device,
  223. "Property Set error: %d, offset %#x\n",
  224. ret > 0 ? ret & ~NVME_SC_DNR : ret, off);
  225. return ret;
  226. }
  227. EXPORT_SYMBOL_GPL(nvmf_reg_write32);
  228. /**
  229. * nvmf_log_connect_error() - Error-parsing-diagnostic print
  230. * out function for connect() errors.
  231. *
  232. * @ctrl: the specific /dev/nvmeX device that had the error.
  233. *
  234. * @errval: Error code to be decoded in a more human-friendly
  235. * printout.
  236. *
  237. * @offset: For use with the NVMe error code NVME_SC_CONNECT_INVALID_PARAM.
  238. *
  239. * @cmd: This is the SQE portion of a submission capsule.
  240. *
  241. * @data: This is the "Data" portion of a submission capsule.
  242. */
  243. static void nvmf_log_connect_error(struct nvme_ctrl *ctrl,
  244. int errval, int offset, struct nvme_command *cmd,
  245. struct nvmf_connect_data *data)
  246. {
  247. int err_sctype = errval & (~NVME_SC_DNR);
  248. switch (err_sctype) {
  249. case (NVME_SC_CONNECT_INVALID_PARAM):
  250. if (offset >> 16) {
  251. char *inv_data = "Connect Invalid Data Parameter";
  252. switch (offset & 0xffff) {
  253. case (offsetof(struct nvmf_connect_data, cntlid)):
  254. dev_err(ctrl->device,
  255. "%s, cntlid: %d\n",
  256. inv_data, data->cntlid);
  257. break;
  258. case (offsetof(struct nvmf_connect_data, hostnqn)):
  259. dev_err(ctrl->device,
  260. "%s, hostnqn \"%s\"\n",
  261. inv_data, data->hostnqn);
  262. break;
  263. case (offsetof(struct nvmf_connect_data, subsysnqn)):
  264. dev_err(ctrl->device,
  265. "%s, subsysnqn \"%s\"\n",
  266. inv_data, data->subsysnqn);
  267. break;
  268. default:
  269. dev_err(ctrl->device,
  270. "%s, starting byte offset: %d\n",
  271. inv_data, offset & 0xffff);
  272. break;
  273. }
  274. } else {
  275. char *inv_sqe = "Connect Invalid SQE Parameter";
  276. switch (offset) {
  277. case (offsetof(struct nvmf_connect_command, qid)):
  278. dev_err(ctrl->device,
  279. "%s, qid %d\n",
  280. inv_sqe, cmd->connect.qid);
  281. break;
  282. default:
  283. dev_err(ctrl->device,
  284. "%s, starting byte offset: %d\n",
  285. inv_sqe, offset);
  286. }
  287. }
  288. break;
  289. case NVME_SC_CONNECT_INVALID_HOST:
  290. dev_err(ctrl->device,
  291. "Connect for subsystem %s is not allowed, hostnqn: %s\n",
  292. data->subsysnqn, data->hostnqn);
  293. break;
  294. case NVME_SC_CONNECT_CTRL_BUSY:
  295. dev_err(ctrl->device,
  296. "Connect command failed: controller is busy or not available\n");
  297. break;
  298. case NVME_SC_CONNECT_FORMAT:
  299. dev_err(ctrl->device,
  300. "Connect incompatible format: %d",
  301. cmd->connect.recfmt);
  302. break;
  303. default:
  304. dev_err(ctrl->device,
  305. "Connect command failed, error wo/DNR bit: %d\n",
  306. err_sctype);
  307. break;
  308. } /* switch (err_sctype) */
  309. }
  310. /**
  311. * nvmf_connect_admin_queue() - NVMe Fabrics Admin Queue "Connect"
  312. * API function.
  313. * @ctrl: Host nvme controller instance used to request
  314. * a new NVMe controller allocation on the target
  315. * system and establish an NVMe Admin connection to
  316. * that controller.
  317. *
  318. * This function enables an NVMe host device to request a new allocation of
  319. * an NVMe controller resource on a target system as well establish a
  320. * fabrics-protocol connection of the NVMe Admin queue between the
  321. * host system device and the allocated NVMe controller on the
  322. * target system via a NVMe Fabrics "Connect" command.
  323. *
  324. * Return:
  325. * 0: success
  326. * > 0: NVMe error status code
  327. * < 0: Linux errno error code
  328. *
  329. */
  330. int nvmf_connect_admin_queue(struct nvme_ctrl *ctrl)
  331. {
  332. struct nvme_command cmd;
  333. union nvme_result res;
  334. struct nvmf_connect_data *data;
  335. int ret;
  336. memset(&cmd, 0, sizeof(cmd));
  337. cmd.connect.opcode = nvme_fabrics_command;
  338. cmd.connect.fctype = nvme_fabrics_type_connect;
  339. cmd.connect.qid = 0;
  340. cmd.connect.sqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
  341. /*
  342. * Set keep-alive timeout in seconds granularity (ms * 1000)
  343. * and add a grace period for controller kato enforcement
  344. */
  345. cmd.connect.kato = ctrl->opts->discovery_nqn ? 0 :
  346. cpu_to_le32((ctrl->kato + NVME_KATO_GRACE) * 1000);
  347. data = kzalloc(sizeof(*data), GFP_KERNEL);
  348. if (!data)
  349. return -ENOMEM;
  350. uuid_copy(&data->hostid, &ctrl->opts->host->id);
  351. data->cntlid = cpu_to_le16(0xffff);
  352. strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
  353. strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
  354. ret = __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, &res,
  355. data, sizeof(*data), 0, NVME_QID_ANY, 1,
  356. BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
  357. if (ret) {
  358. nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
  359. &cmd, data);
  360. goto out_free_data;
  361. }
  362. ctrl->cntlid = le16_to_cpu(res.u16);
  363. out_free_data:
  364. kfree(data);
  365. return ret;
  366. }
  367. EXPORT_SYMBOL_GPL(nvmf_connect_admin_queue);
  368. /**
  369. * nvmf_connect_io_queue() - NVMe Fabrics I/O Queue "Connect"
  370. * API function.
  371. * @ctrl: Host nvme controller instance used to establish an
  372. * NVMe I/O queue connection to the already allocated NVMe
  373. * controller on the target system.
  374. * @qid: NVMe I/O queue number for the new I/O connection between
  375. * host and target (note qid == 0 is illegal as this is
  376. * the Admin queue, per NVMe standard).
  377. *
  378. * This function issues a fabrics-protocol connection
  379. * of a NVMe I/O queue (via NVMe Fabrics "Connect" command)
  380. * between the host system device and the allocated NVMe controller
  381. * on the target system.
  382. *
  383. * Return:
  384. * 0: success
  385. * > 0: NVMe error status code
  386. * < 0: Linux errno error code
  387. */
  388. int nvmf_connect_io_queue(struct nvme_ctrl *ctrl, u16 qid)
  389. {
  390. struct nvme_command cmd;
  391. struct nvmf_connect_data *data;
  392. union nvme_result res;
  393. int ret;
  394. memset(&cmd, 0, sizeof(cmd));
  395. cmd.connect.opcode = nvme_fabrics_command;
  396. cmd.connect.fctype = nvme_fabrics_type_connect;
  397. cmd.connect.qid = cpu_to_le16(qid);
  398. cmd.connect.sqsize = cpu_to_le16(ctrl->sqsize);
  399. data = kzalloc(sizeof(*data), GFP_KERNEL);
  400. if (!data)
  401. return -ENOMEM;
  402. uuid_copy(&data->hostid, &ctrl->opts->host->id);
  403. data->cntlid = cpu_to_le16(ctrl->cntlid);
  404. strncpy(data->subsysnqn, ctrl->opts->subsysnqn, NVMF_NQN_SIZE);
  405. strncpy(data->hostnqn, ctrl->opts->host->nqn, NVMF_NQN_SIZE);
  406. ret = __nvme_submit_sync_cmd(ctrl->connect_q, &cmd, &res,
  407. data, sizeof(*data), 0, qid, 1,
  408. BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
  409. if (ret) {
  410. nvmf_log_connect_error(ctrl, ret, le32_to_cpu(res.u32),
  411. &cmd, data);
  412. }
  413. kfree(data);
  414. return ret;
  415. }
  416. EXPORT_SYMBOL_GPL(nvmf_connect_io_queue);
  417. bool nvmf_should_reconnect(struct nvme_ctrl *ctrl)
  418. {
  419. if (ctrl->opts->max_reconnects == -1 ||
  420. ctrl->nr_reconnects < ctrl->opts->max_reconnects)
  421. return true;
  422. return false;
  423. }
  424. EXPORT_SYMBOL_GPL(nvmf_should_reconnect);
  425. /**
  426. * nvmf_register_transport() - NVMe Fabrics Library registration function.
  427. * @ops: Transport ops instance to be registered to the
  428. * common fabrics library.
  429. *
  430. * API function that registers the type of specific transport fabric
  431. * being implemented to the common NVMe fabrics library. Part of
  432. * the overall init sequence of starting up a fabrics driver.
  433. */
  434. int nvmf_register_transport(struct nvmf_transport_ops *ops)
  435. {
  436. if (!ops->create_ctrl)
  437. return -EINVAL;
  438. down_write(&nvmf_transports_rwsem);
  439. list_add_tail(&ops->entry, &nvmf_transports);
  440. up_write(&nvmf_transports_rwsem);
  441. return 0;
  442. }
  443. EXPORT_SYMBOL_GPL(nvmf_register_transport);
  444. /**
  445. * nvmf_unregister_transport() - NVMe Fabrics Library unregistration function.
  446. * @ops: Transport ops instance to be unregistered from the
  447. * common fabrics library.
  448. *
  449. * Fabrics API function that unregisters the type of specific transport
  450. * fabric being implemented from the common NVMe fabrics library.
  451. * Part of the overall exit sequence of unloading the implemented driver.
  452. */
  453. void nvmf_unregister_transport(struct nvmf_transport_ops *ops)
  454. {
  455. down_write(&nvmf_transports_rwsem);
  456. list_del(&ops->entry);
  457. up_write(&nvmf_transports_rwsem);
  458. }
  459. EXPORT_SYMBOL_GPL(nvmf_unregister_transport);
  460. static struct nvmf_transport_ops *nvmf_lookup_transport(
  461. struct nvmf_ctrl_options *opts)
  462. {
  463. struct nvmf_transport_ops *ops;
  464. lockdep_assert_held(&nvmf_transports_rwsem);
  465. list_for_each_entry(ops, &nvmf_transports, entry) {
  466. if (strcmp(ops->name, opts->transport) == 0)
  467. return ops;
  468. }
  469. return NULL;
  470. }
  471. /*
  472. * For something we're not in a state to send to the device the default action
  473. * is to busy it and retry it after the controller state is recovered. However,
  474. * if the controller is deleting or if anything is marked for failfast or
  475. * nvme multipath it is immediately failed.
  476. *
  477. * Note: commands used to initialize the controller will be marked for failfast.
  478. * Note: nvme cli/ioctl commands are marked for failfast.
  479. */
  480. blk_status_t nvmf_fail_nonready_command(struct nvme_ctrl *ctrl,
  481. struct request *rq)
  482. {
  483. if (ctrl->state != NVME_CTRL_DELETING &&
  484. ctrl->state != NVME_CTRL_DEAD &&
  485. !blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH))
  486. return BLK_STS_RESOURCE;
  487. nvme_req(rq)->status = NVME_SC_HOST_PATH_ERROR;
  488. blk_mq_start_request(rq);
  489. nvme_complete_rq(rq);
  490. return BLK_STS_OK;
  491. }
  492. EXPORT_SYMBOL_GPL(nvmf_fail_nonready_command);
  493. bool __nvmf_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
  494. bool queue_live)
  495. {
  496. struct nvme_request *req = nvme_req(rq);
  497. /*
  498. * If we are in some state of setup or teardown only allow
  499. * internally generated commands.
  500. */
  501. if (!blk_rq_is_passthrough(rq) || (req->flags & NVME_REQ_USERCMD))
  502. return false;
  503. /*
  504. * Only allow commands on a live queue, except for the connect command,
  505. * which is require to set the queue live in the appropinquate states.
  506. */
  507. switch (ctrl->state) {
  508. case NVME_CTRL_CONNECTING:
  509. if (req->cmd->common.opcode == nvme_fabrics_command &&
  510. req->cmd->fabrics.fctype == nvme_fabrics_type_connect)
  511. return true;
  512. break;
  513. default:
  514. break;
  515. case NVME_CTRL_DEAD:
  516. return false;
  517. }
  518. return queue_live;
  519. }
  520. EXPORT_SYMBOL_GPL(__nvmf_check_ready);
  521. static const match_table_t opt_tokens = {
  522. { NVMF_OPT_TRANSPORT, "transport=%s" },
  523. { NVMF_OPT_TRADDR, "traddr=%s" },
  524. { NVMF_OPT_TRSVCID, "trsvcid=%s" },
  525. { NVMF_OPT_NQN, "nqn=%s" },
  526. { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
  527. { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
  528. { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
  529. { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" },
  530. { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
  531. { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
  532. { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
  533. { NVMF_OPT_HOST_ID, "hostid=%s" },
  534. { NVMF_OPT_DUP_CONNECT, "duplicate_connect" },
  535. { NVMF_OPT_ERR, NULL }
  536. };
  537. static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  538. const char *buf)
  539. {
  540. substring_t args[MAX_OPT_ARGS];
  541. char *options, *o, *p;
  542. int token, ret = 0;
  543. size_t nqnlen = 0;
  544. int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
  545. uuid_t hostid;
  546. /* Set defaults */
  547. opts->queue_size = NVMF_DEF_QUEUE_SIZE;
  548. opts->nr_io_queues = num_online_cpus();
  549. opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
  550. opts->kato = NVME_DEFAULT_KATO;
  551. opts->duplicate_connect = false;
  552. options = o = kstrdup(buf, GFP_KERNEL);
  553. if (!options)
  554. return -ENOMEM;
  555. uuid_gen(&hostid);
  556. while ((p = strsep(&o, ",\n")) != NULL) {
  557. if (!*p)
  558. continue;
  559. token = match_token(p, opt_tokens, args);
  560. opts->mask |= token;
  561. switch (token) {
  562. case NVMF_OPT_TRANSPORT:
  563. p = match_strdup(args);
  564. if (!p) {
  565. ret = -ENOMEM;
  566. goto out;
  567. }
  568. kfree(opts->transport);
  569. opts->transport = p;
  570. break;
  571. case NVMF_OPT_NQN:
  572. p = match_strdup(args);
  573. if (!p) {
  574. ret = -ENOMEM;
  575. goto out;
  576. }
  577. kfree(opts->subsysnqn);
  578. opts->subsysnqn = p;
  579. nqnlen = strlen(opts->subsysnqn);
  580. if (nqnlen >= NVMF_NQN_SIZE) {
  581. pr_err("%s needs to be < %d bytes\n",
  582. opts->subsysnqn, NVMF_NQN_SIZE);
  583. ret = -EINVAL;
  584. goto out;
  585. }
  586. opts->discovery_nqn =
  587. !(strcmp(opts->subsysnqn,
  588. NVME_DISC_SUBSYS_NAME));
  589. break;
  590. case NVMF_OPT_TRADDR:
  591. p = match_strdup(args);
  592. if (!p) {
  593. ret = -ENOMEM;
  594. goto out;
  595. }
  596. kfree(opts->traddr);
  597. opts->traddr = p;
  598. break;
  599. case NVMF_OPT_TRSVCID:
  600. p = match_strdup(args);
  601. if (!p) {
  602. ret = -ENOMEM;
  603. goto out;
  604. }
  605. kfree(opts->trsvcid);
  606. opts->trsvcid = p;
  607. break;
  608. case NVMF_OPT_QUEUE_SIZE:
  609. if (match_int(args, &token)) {
  610. ret = -EINVAL;
  611. goto out;
  612. }
  613. if (token < NVMF_MIN_QUEUE_SIZE ||
  614. token > NVMF_MAX_QUEUE_SIZE) {
  615. pr_err("Invalid queue_size %d\n", token);
  616. ret = -EINVAL;
  617. goto out;
  618. }
  619. opts->queue_size = token;
  620. break;
  621. case NVMF_OPT_NR_IO_QUEUES:
  622. if (match_int(args, &token)) {
  623. ret = -EINVAL;
  624. goto out;
  625. }
  626. if (token <= 0) {
  627. pr_err("Invalid number of IOQs %d\n", token);
  628. ret = -EINVAL;
  629. goto out;
  630. }
  631. if (opts->discovery_nqn) {
  632. pr_debug("Ignoring nr_io_queues value for discovery controller\n");
  633. break;
  634. }
  635. opts->nr_io_queues = min_t(unsigned int,
  636. num_online_cpus(), token);
  637. break;
  638. case NVMF_OPT_KATO:
  639. if (match_int(args, &token)) {
  640. ret = -EINVAL;
  641. goto out;
  642. }
  643. if (token < 0) {
  644. pr_err("Invalid keep_alive_tmo %d\n", token);
  645. ret = -EINVAL;
  646. goto out;
  647. } else if (token == 0 && !opts->discovery_nqn) {
  648. /* Allowed for debug */
  649. pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
  650. }
  651. opts->kato = token;
  652. if (opts->discovery_nqn && opts->kato) {
  653. pr_err("Discovery controllers cannot accept KATO != 0\n");
  654. ret = -EINVAL;
  655. goto out;
  656. }
  657. break;
  658. case NVMF_OPT_CTRL_LOSS_TMO:
  659. if (match_int(args, &token)) {
  660. ret = -EINVAL;
  661. goto out;
  662. }
  663. if (token < 0)
  664. pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
  665. ctrl_loss_tmo = token;
  666. break;
  667. case NVMF_OPT_HOSTNQN:
  668. if (opts->host) {
  669. pr_err("hostnqn already user-assigned: %s\n",
  670. opts->host->nqn);
  671. ret = -EADDRINUSE;
  672. goto out;
  673. }
  674. p = match_strdup(args);
  675. if (!p) {
  676. ret = -ENOMEM;
  677. goto out;
  678. }
  679. nqnlen = strlen(p);
  680. if (nqnlen >= NVMF_NQN_SIZE) {
  681. pr_err("%s needs to be < %d bytes\n",
  682. p, NVMF_NQN_SIZE);
  683. kfree(p);
  684. ret = -EINVAL;
  685. goto out;
  686. }
  687. nvmf_host_put(opts->host);
  688. opts->host = nvmf_host_add(p);
  689. kfree(p);
  690. if (!opts->host) {
  691. ret = -ENOMEM;
  692. goto out;
  693. }
  694. break;
  695. case NVMF_OPT_RECONNECT_DELAY:
  696. if (match_int(args, &token)) {
  697. ret = -EINVAL;
  698. goto out;
  699. }
  700. if (token <= 0) {
  701. pr_err("Invalid reconnect_delay %d\n", token);
  702. ret = -EINVAL;
  703. goto out;
  704. }
  705. opts->reconnect_delay = token;
  706. break;
  707. case NVMF_OPT_HOST_TRADDR:
  708. p = match_strdup(args);
  709. if (!p) {
  710. ret = -ENOMEM;
  711. goto out;
  712. }
  713. kfree(opts->host_traddr);
  714. opts->host_traddr = p;
  715. break;
  716. case NVMF_OPT_HOST_ID:
  717. p = match_strdup(args);
  718. if (!p) {
  719. ret = -ENOMEM;
  720. goto out;
  721. }
  722. ret = uuid_parse(p, &hostid);
  723. if (ret) {
  724. pr_err("Invalid hostid %s\n", p);
  725. ret = -EINVAL;
  726. kfree(p);
  727. goto out;
  728. }
  729. kfree(p);
  730. break;
  731. case NVMF_OPT_DUP_CONNECT:
  732. opts->duplicate_connect = true;
  733. break;
  734. default:
  735. pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
  736. p);
  737. ret = -EINVAL;
  738. goto out;
  739. }
  740. }
  741. if (opts->discovery_nqn) {
  742. opts->kato = 0;
  743. opts->nr_io_queues = 0;
  744. opts->duplicate_connect = true;
  745. }
  746. if (ctrl_loss_tmo < 0)
  747. opts->max_reconnects = -1;
  748. else
  749. opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
  750. opts->reconnect_delay);
  751. if (!opts->host) {
  752. kref_get(&nvmf_default_host->ref);
  753. opts->host = nvmf_default_host;
  754. }
  755. uuid_copy(&opts->host->id, &hostid);
  756. out:
  757. kfree(options);
  758. return ret;
  759. }
  760. static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
  761. unsigned int required_opts)
  762. {
  763. if ((opts->mask & required_opts) != required_opts) {
  764. int i;
  765. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  766. if ((opt_tokens[i].token & required_opts) &&
  767. !(opt_tokens[i].token & opts->mask)) {
  768. pr_warn("missing parameter '%s'\n",
  769. opt_tokens[i].pattern);
  770. }
  771. }
  772. return -EINVAL;
  773. }
  774. return 0;
  775. }
  776. static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
  777. unsigned int allowed_opts)
  778. {
  779. if (opts->mask & ~allowed_opts) {
  780. int i;
  781. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  782. if ((opt_tokens[i].token & opts->mask) &&
  783. (opt_tokens[i].token & ~allowed_opts)) {
  784. pr_warn("invalid parameter '%s'\n",
  785. opt_tokens[i].pattern);
  786. }
  787. }
  788. return -EINVAL;
  789. }
  790. return 0;
  791. }
  792. void nvmf_free_options(struct nvmf_ctrl_options *opts)
  793. {
  794. nvmf_host_put(opts->host);
  795. kfree(opts->transport);
  796. kfree(opts->traddr);
  797. kfree(opts->trsvcid);
  798. kfree(opts->subsysnqn);
  799. kfree(opts->host_traddr);
  800. kfree(opts);
  801. }
  802. EXPORT_SYMBOL_GPL(nvmf_free_options);
  803. #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
  804. #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
  805. NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
  806. NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT)
  807. static struct nvme_ctrl *
  808. nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
  809. {
  810. struct nvmf_ctrl_options *opts;
  811. struct nvmf_transport_ops *ops;
  812. struct nvme_ctrl *ctrl;
  813. int ret;
  814. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  815. if (!opts)
  816. return ERR_PTR(-ENOMEM);
  817. ret = nvmf_parse_options(opts, buf);
  818. if (ret)
  819. goto out_free_opts;
  820. request_module("nvme-%s", opts->transport);
  821. /*
  822. * Check the generic options first as we need a valid transport for
  823. * the lookup below. Then clear the generic flags so that transport
  824. * drivers don't have to care about them.
  825. */
  826. ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
  827. if (ret)
  828. goto out_free_opts;
  829. opts->mask &= ~NVMF_REQUIRED_OPTS;
  830. down_read(&nvmf_transports_rwsem);
  831. ops = nvmf_lookup_transport(opts);
  832. if (!ops) {
  833. pr_info("no handler found for transport %s.\n",
  834. opts->transport);
  835. ret = -EINVAL;
  836. goto out_unlock;
  837. }
  838. if (!try_module_get(ops->module)) {
  839. ret = -EBUSY;
  840. goto out_unlock;
  841. }
  842. up_read(&nvmf_transports_rwsem);
  843. ret = nvmf_check_required_opts(opts, ops->required_opts);
  844. if (ret)
  845. goto out_module_put;
  846. ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
  847. ops->allowed_opts | ops->required_opts);
  848. if (ret)
  849. goto out_module_put;
  850. ctrl = ops->create_ctrl(dev, opts);
  851. if (IS_ERR(ctrl)) {
  852. ret = PTR_ERR(ctrl);
  853. goto out_module_put;
  854. }
  855. module_put(ops->module);
  856. return ctrl;
  857. out_module_put:
  858. module_put(ops->module);
  859. goto out_free_opts;
  860. out_unlock:
  861. up_read(&nvmf_transports_rwsem);
  862. out_free_opts:
  863. nvmf_free_options(opts);
  864. return ERR_PTR(ret);
  865. }
  866. static struct class *nvmf_class;
  867. static struct device *nvmf_device;
  868. static DEFINE_MUTEX(nvmf_dev_mutex);
  869. static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
  870. size_t count, loff_t *pos)
  871. {
  872. struct seq_file *seq_file = file->private_data;
  873. struct nvme_ctrl *ctrl;
  874. const char *buf;
  875. int ret = 0;
  876. if (count > PAGE_SIZE)
  877. return -ENOMEM;
  878. buf = memdup_user_nul(ubuf, count);
  879. if (IS_ERR(buf))
  880. return PTR_ERR(buf);
  881. mutex_lock(&nvmf_dev_mutex);
  882. if (seq_file->private) {
  883. ret = -EINVAL;
  884. goto out_unlock;
  885. }
  886. ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
  887. if (IS_ERR(ctrl)) {
  888. ret = PTR_ERR(ctrl);
  889. goto out_unlock;
  890. }
  891. seq_file->private = ctrl;
  892. out_unlock:
  893. mutex_unlock(&nvmf_dev_mutex);
  894. kfree(buf);
  895. return ret ? ret : count;
  896. }
  897. static int nvmf_dev_show(struct seq_file *seq_file, void *private)
  898. {
  899. struct nvme_ctrl *ctrl;
  900. int ret = 0;
  901. mutex_lock(&nvmf_dev_mutex);
  902. ctrl = seq_file->private;
  903. if (!ctrl) {
  904. ret = -EINVAL;
  905. goto out_unlock;
  906. }
  907. seq_printf(seq_file, "instance=%d,cntlid=%d\n",
  908. ctrl->instance, ctrl->cntlid);
  909. out_unlock:
  910. mutex_unlock(&nvmf_dev_mutex);
  911. return ret;
  912. }
  913. static int nvmf_dev_open(struct inode *inode, struct file *file)
  914. {
  915. /*
  916. * The miscdevice code initializes file->private_data, but doesn't
  917. * make use of it later.
  918. */
  919. file->private_data = NULL;
  920. return single_open(file, nvmf_dev_show, NULL);
  921. }
  922. static int nvmf_dev_release(struct inode *inode, struct file *file)
  923. {
  924. struct seq_file *seq_file = file->private_data;
  925. struct nvme_ctrl *ctrl = seq_file->private;
  926. if (ctrl)
  927. nvme_put_ctrl(ctrl);
  928. return single_release(inode, file);
  929. }
  930. static const struct file_operations nvmf_dev_fops = {
  931. .owner = THIS_MODULE,
  932. .write = nvmf_dev_write,
  933. .read = seq_read,
  934. .open = nvmf_dev_open,
  935. .release = nvmf_dev_release,
  936. };
  937. static struct miscdevice nvmf_misc = {
  938. .minor = MISC_DYNAMIC_MINOR,
  939. .name = "nvme-fabrics",
  940. .fops = &nvmf_dev_fops,
  941. };
  942. static int __init nvmf_init(void)
  943. {
  944. int ret;
  945. nvmf_default_host = nvmf_host_default();
  946. if (!nvmf_default_host)
  947. return -ENOMEM;
  948. nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
  949. if (IS_ERR(nvmf_class)) {
  950. pr_err("couldn't register class nvme-fabrics\n");
  951. ret = PTR_ERR(nvmf_class);
  952. goto out_free_host;
  953. }
  954. nvmf_device =
  955. device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
  956. if (IS_ERR(nvmf_device)) {
  957. pr_err("couldn't create nvme-fabris device!\n");
  958. ret = PTR_ERR(nvmf_device);
  959. goto out_destroy_class;
  960. }
  961. ret = misc_register(&nvmf_misc);
  962. if (ret) {
  963. pr_err("couldn't register misc device: %d\n", ret);
  964. goto out_destroy_device;
  965. }
  966. return 0;
  967. out_destroy_device:
  968. device_destroy(nvmf_class, MKDEV(0, 0));
  969. out_destroy_class:
  970. class_destroy(nvmf_class);
  971. out_free_host:
  972. nvmf_host_put(nvmf_default_host);
  973. return ret;
  974. }
  975. static void __exit nvmf_exit(void)
  976. {
  977. misc_deregister(&nvmf_misc);
  978. device_destroy(nvmf_class, MKDEV(0, 0));
  979. class_destroy(nvmf_class);
  980. nvmf_host_put(nvmf_default_host);
  981. BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
  982. BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
  983. BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
  984. BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
  985. }
  986. MODULE_LICENSE("GPL v2");
  987. module_init(nvmf_init);
  988. module_exit(nvmf_exit);