fabrics.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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. * anything marked for failfast or nvme multipath is immediately failed.
  475. *
  476. * Note: commands used to initialize the controller will be marked for failfast.
  477. * Note: nvme cli/ioctl commands are marked for failfast.
  478. */
  479. blk_status_t nvmf_fail_nonready_command(struct request *rq)
  480. {
  481. if (!blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH))
  482. return BLK_STS_RESOURCE;
  483. nvme_req(rq)->status = NVME_SC_ABORT_REQ;
  484. return BLK_STS_IOERR;
  485. }
  486. EXPORT_SYMBOL_GPL(nvmf_fail_nonready_command);
  487. bool __nvmf_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
  488. bool queue_live)
  489. {
  490. struct nvme_request *req = nvme_req(rq);
  491. /*
  492. * If we are in some state of setup or teardown only allow
  493. * internally generated commands.
  494. */
  495. if (!blk_rq_is_passthrough(rq) || (req->flags & NVME_REQ_USERCMD))
  496. return false;
  497. /*
  498. * Only allow commands on a live queue, except for the connect command,
  499. * which is require to set the queue live in the appropinquate states.
  500. */
  501. switch (ctrl->state) {
  502. case NVME_CTRL_NEW:
  503. case NVME_CTRL_CONNECTING:
  504. if (req->cmd->common.opcode == nvme_fabrics_command &&
  505. req->cmd->fabrics.fctype == nvme_fabrics_type_connect)
  506. return true;
  507. break;
  508. default:
  509. break;
  510. case NVME_CTRL_DEAD:
  511. return false;
  512. }
  513. return queue_live;
  514. }
  515. EXPORT_SYMBOL_GPL(__nvmf_check_ready);
  516. static const match_table_t opt_tokens = {
  517. { NVMF_OPT_TRANSPORT, "transport=%s" },
  518. { NVMF_OPT_TRADDR, "traddr=%s" },
  519. { NVMF_OPT_TRSVCID, "trsvcid=%s" },
  520. { NVMF_OPT_NQN, "nqn=%s" },
  521. { NVMF_OPT_QUEUE_SIZE, "queue_size=%d" },
  522. { NVMF_OPT_NR_IO_QUEUES, "nr_io_queues=%d" },
  523. { NVMF_OPT_RECONNECT_DELAY, "reconnect_delay=%d" },
  524. { NVMF_OPT_CTRL_LOSS_TMO, "ctrl_loss_tmo=%d" },
  525. { NVMF_OPT_KATO, "keep_alive_tmo=%d" },
  526. { NVMF_OPT_HOSTNQN, "hostnqn=%s" },
  527. { NVMF_OPT_HOST_TRADDR, "host_traddr=%s" },
  528. { NVMF_OPT_HOST_ID, "hostid=%s" },
  529. { NVMF_OPT_DUP_CONNECT, "duplicate_connect" },
  530. { NVMF_OPT_ERR, NULL }
  531. };
  532. static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  533. const char *buf)
  534. {
  535. substring_t args[MAX_OPT_ARGS];
  536. char *options, *o, *p;
  537. int token, ret = 0;
  538. size_t nqnlen = 0;
  539. int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
  540. uuid_t hostid;
  541. /* Set defaults */
  542. opts->queue_size = NVMF_DEF_QUEUE_SIZE;
  543. opts->nr_io_queues = num_online_cpus();
  544. opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
  545. opts->kato = NVME_DEFAULT_KATO;
  546. opts->duplicate_connect = false;
  547. options = o = kstrdup(buf, GFP_KERNEL);
  548. if (!options)
  549. return -ENOMEM;
  550. uuid_gen(&hostid);
  551. while ((p = strsep(&o, ",\n")) != NULL) {
  552. if (!*p)
  553. continue;
  554. token = match_token(p, opt_tokens, args);
  555. opts->mask |= token;
  556. switch (token) {
  557. case NVMF_OPT_TRANSPORT:
  558. p = match_strdup(args);
  559. if (!p) {
  560. ret = -ENOMEM;
  561. goto out;
  562. }
  563. kfree(opts->transport);
  564. opts->transport = p;
  565. break;
  566. case NVMF_OPT_NQN:
  567. p = match_strdup(args);
  568. if (!p) {
  569. ret = -ENOMEM;
  570. goto out;
  571. }
  572. kfree(opts->subsysnqn);
  573. opts->subsysnqn = p;
  574. nqnlen = strlen(opts->subsysnqn);
  575. if (nqnlen >= NVMF_NQN_SIZE) {
  576. pr_err("%s needs to be < %d bytes\n",
  577. opts->subsysnqn, NVMF_NQN_SIZE);
  578. ret = -EINVAL;
  579. goto out;
  580. }
  581. opts->discovery_nqn =
  582. !(strcmp(opts->subsysnqn,
  583. NVME_DISC_SUBSYS_NAME));
  584. break;
  585. case NVMF_OPT_TRADDR:
  586. p = match_strdup(args);
  587. if (!p) {
  588. ret = -ENOMEM;
  589. goto out;
  590. }
  591. kfree(opts->traddr);
  592. opts->traddr = p;
  593. break;
  594. case NVMF_OPT_TRSVCID:
  595. p = match_strdup(args);
  596. if (!p) {
  597. ret = -ENOMEM;
  598. goto out;
  599. }
  600. kfree(opts->trsvcid);
  601. opts->trsvcid = p;
  602. break;
  603. case NVMF_OPT_QUEUE_SIZE:
  604. if (match_int(args, &token)) {
  605. ret = -EINVAL;
  606. goto out;
  607. }
  608. if (token < NVMF_MIN_QUEUE_SIZE ||
  609. token > NVMF_MAX_QUEUE_SIZE) {
  610. pr_err("Invalid queue_size %d\n", token);
  611. ret = -EINVAL;
  612. goto out;
  613. }
  614. opts->queue_size = token;
  615. break;
  616. case NVMF_OPT_NR_IO_QUEUES:
  617. if (match_int(args, &token)) {
  618. ret = -EINVAL;
  619. goto out;
  620. }
  621. if (token <= 0) {
  622. pr_err("Invalid number of IOQs %d\n", token);
  623. ret = -EINVAL;
  624. goto out;
  625. }
  626. if (opts->discovery_nqn) {
  627. pr_debug("Ignoring nr_io_queues value for discovery controller\n");
  628. break;
  629. }
  630. opts->nr_io_queues = min_t(unsigned int,
  631. num_online_cpus(), token);
  632. break;
  633. case NVMF_OPT_KATO:
  634. if (match_int(args, &token)) {
  635. ret = -EINVAL;
  636. goto out;
  637. }
  638. if (token < 0) {
  639. pr_err("Invalid keep_alive_tmo %d\n", token);
  640. ret = -EINVAL;
  641. goto out;
  642. } else if (token == 0 && !opts->discovery_nqn) {
  643. /* Allowed for debug */
  644. pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
  645. }
  646. opts->kato = token;
  647. if (opts->discovery_nqn && opts->kato) {
  648. pr_err("Discovery controllers cannot accept KATO != 0\n");
  649. ret = -EINVAL;
  650. goto out;
  651. }
  652. break;
  653. case NVMF_OPT_CTRL_LOSS_TMO:
  654. if (match_int(args, &token)) {
  655. ret = -EINVAL;
  656. goto out;
  657. }
  658. if (token < 0)
  659. pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
  660. ctrl_loss_tmo = token;
  661. break;
  662. case NVMF_OPT_HOSTNQN:
  663. if (opts->host) {
  664. pr_err("hostnqn already user-assigned: %s\n",
  665. opts->host->nqn);
  666. ret = -EADDRINUSE;
  667. goto out;
  668. }
  669. p = match_strdup(args);
  670. if (!p) {
  671. ret = -ENOMEM;
  672. goto out;
  673. }
  674. nqnlen = strlen(p);
  675. if (nqnlen >= NVMF_NQN_SIZE) {
  676. pr_err("%s needs to be < %d bytes\n",
  677. p, NVMF_NQN_SIZE);
  678. kfree(p);
  679. ret = -EINVAL;
  680. goto out;
  681. }
  682. nvmf_host_put(opts->host);
  683. opts->host = nvmf_host_add(p);
  684. kfree(p);
  685. if (!opts->host) {
  686. ret = -ENOMEM;
  687. goto out;
  688. }
  689. break;
  690. case NVMF_OPT_RECONNECT_DELAY:
  691. if (match_int(args, &token)) {
  692. ret = -EINVAL;
  693. goto out;
  694. }
  695. if (token <= 0) {
  696. pr_err("Invalid reconnect_delay %d\n", token);
  697. ret = -EINVAL;
  698. goto out;
  699. }
  700. opts->reconnect_delay = token;
  701. break;
  702. case NVMF_OPT_HOST_TRADDR:
  703. p = match_strdup(args);
  704. if (!p) {
  705. ret = -ENOMEM;
  706. goto out;
  707. }
  708. kfree(opts->host_traddr);
  709. opts->host_traddr = p;
  710. break;
  711. case NVMF_OPT_HOST_ID:
  712. p = match_strdup(args);
  713. if (!p) {
  714. ret = -ENOMEM;
  715. goto out;
  716. }
  717. ret = uuid_parse(p, &hostid);
  718. if (ret) {
  719. pr_err("Invalid hostid %s\n", p);
  720. ret = -EINVAL;
  721. kfree(p);
  722. goto out;
  723. }
  724. kfree(p);
  725. break;
  726. case NVMF_OPT_DUP_CONNECT:
  727. opts->duplicate_connect = true;
  728. break;
  729. default:
  730. pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
  731. p);
  732. ret = -EINVAL;
  733. goto out;
  734. }
  735. }
  736. if (opts->discovery_nqn) {
  737. opts->kato = 0;
  738. opts->nr_io_queues = 0;
  739. opts->duplicate_connect = true;
  740. }
  741. if (ctrl_loss_tmo < 0)
  742. opts->max_reconnects = -1;
  743. else
  744. opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
  745. opts->reconnect_delay);
  746. if (!opts->host) {
  747. kref_get(&nvmf_default_host->ref);
  748. opts->host = nvmf_default_host;
  749. }
  750. uuid_copy(&opts->host->id, &hostid);
  751. out:
  752. kfree(options);
  753. return ret;
  754. }
  755. static int nvmf_check_required_opts(struct nvmf_ctrl_options *opts,
  756. unsigned int required_opts)
  757. {
  758. if ((opts->mask & required_opts) != required_opts) {
  759. int i;
  760. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  761. if ((opt_tokens[i].token & required_opts) &&
  762. !(opt_tokens[i].token & opts->mask)) {
  763. pr_warn("missing parameter '%s'\n",
  764. opt_tokens[i].pattern);
  765. }
  766. }
  767. return -EINVAL;
  768. }
  769. return 0;
  770. }
  771. static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
  772. unsigned int allowed_opts)
  773. {
  774. if (opts->mask & ~allowed_opts) {
  775. int i;
  776. for (i = 0; i < ARRAY_SIZE(opt_tokens); i++) {
  777. if ((opt_tokens[i].token & opts->mask) &&
  778. (opt_tokens[i].token & ~allowed_opts)) {
  779. pr_warn("invalid parameter '%s'\n",
  780. opt_tokens[i].pattern);
  781. }
  782. }
  783. return -EINVAL;
  784. }
  785. return 0;
  786. }
  787. void nvmf_free_options(struct nvmf_ctrl_options *opts)
  788. {
  789. nvmf_host_put(opts->host);
  790. kfree(opts->transport);
  791. kfree(opts->traddr);
  792. kfree(opts->trsvcid);
  793. kfree(opts->subsysnqn);
  794. kfree(opts->host_traddr);
  795. kfree(opts);
  796. }
  797. EXPORT_SYMBOL_GPL(nvmf_free_options);
  798. #define NVMF_REQUIRED_OPTS (NVMF_OPT_TRANSPORT | NVMF_OPT_NQN)
  799. #define NVMF_ALLOWED_OPTS (NVMF_OPT_QUEUE_SIZE | NVMF_OPT_NR_IO_QUEUES | \
  800. NVMF_OPT_KATO | NVMF_OPT_HOSTNQN | \
  801. NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT)
  802. static struct nvme_ctrl *
  803. nvmf_create_ctrl(struct device *dev, const char *buf, size_t count)
  804. {
  805. struct nvmf_ctrl_options *opts;
  806. struct nvmf_transport_ops *ops;
  807. struct nvme_ctrl *ctrl;
  808. int ret;
  809. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  810. if (!opts)
  811. return ERR_PTR(-ENOMEM);
  812. ret = nvmf_parse_options(opts, buf);
  813. if (ret)
  814. goto out_free_opts;
  815. request_module("nvme-%s", opts->transport);
  816. /*
  817. * Check the generic options first as we need a valid transport for
  818. * the lookup below. Then clear the generic flags so that transport
  819. * drivers don't have to care about them.
  820. */
  821. ret = nvmf_check_required_opts(opts, NVMF_REQUIRED_OPTS);
  822. if (ret)
  823. goto out_free_opts;
  824. opts->mask &= ~NVMF_REQUIRED_OPTS;
  825. down_read(&nvmf_transports_rwsem);
  826. ops = nvmf_lookup_transport(opts);
  827. if (!ops) {
  828. pr_info("no handler found for transport %s.\n",
  829. opts->transport);
  830. ret = -EINVAL;
  831. goto out_unlock;
  832. }
  833. if (!try_module_get(ops->module)) {
  834. ret = -EBUSY;
  835. goto out_unlock;
  836. }
  837. up_read(&nvmf_transports_rwsem);
  838. ret = nvmf_check_required_opts(opts, ops->required_opts);
  839. if (ret)
  840. goto out_module_put;
  841. ret = nvmf_check_allowed_opts(opts, NVMF_ALLOWED_OPTS |
  842. ops->allowed_opts | ops->required_opts);
  843. if (ret)
  844. goto out_module_put;
  845. ctrl = ops->create_ctrl(dev, opts);
  846. if (IS_ERR(ctrl)) {
  847. ret = PTR_ERR(ctrl);
  848. goto out_module_put;
  849. }
  850. module_put(ops->module);
  851. return ctrl;
  852. out_module_put:
  853. module_put(ops->module);
  854. goto out_free_opts;
  855. out_unlock:
  856. up_read(&nvmf_transports_rwsem);
  857. out_free_opts:
  858. nvmf_free_options(opts);
  859. return ERR_PTR(ret);
  860. }
  861. static struct class *nvmf_class;
  862. static struct device *nvmf_device;
  863. static DEFINE_MUTEX(nvmf_dev_mutex);
  864. static ssize_t nvmf_dev_write(struct file *file, const char __user *ubuf,
  865. size_t count, loff_t *pos)
  866. {
  867. struct seq_file *seq_file = file->private_data;
  868. struct nvme_ctrl *ctrl;
  869. const char *buf;
  870. int ret = 0;
  871. if (count > PAGE_SIZE)
  872. return -ENOMEM;
  873. buf = memdup_user_nul(ubuf, count);
  874. if (IS_ERR(buf))
  875. return PTR_ERR(buf);
  876. mutex_lock(&nvmf_dev_mutex);
  877. if (seq_file->private) {
  878. ret = -EINVAL;
  879. goto out_unlock;
  880. }
  881. ctrl = nvmf_create_ctrl(nvmf_device, buf, count);
  882. if (IS_ERR(ctrl)) {
  883. ret = PTR_ERR(ctrl);
  884. goto out_unlock;
  885. }
  886. seq_file->private = ctrl;
  887. out_unlock:
  888. mutex_unlock(&nvmf_dev_mutex);
  889. kfree(buf);
  890. return ret ? ret : count;
  891. }
  892. static int nvmf_dev_show(struct seq_file *seq_file, void *private)
  893. {
  894. struct nvme_ctrl *ctrl;
  895. int ret = 0;
  896. mutex_lock(&nvmf_dev_mutex);
  897. ctrl = seq_file->private;
  898. if (!ctrl) {
  899. ret = -EINVAL;
  900. goto out_unlock;
  901. }
  902. seq_printf(seq_file, "instance=%d,cntlid=%d\n",
  903. ctrl->instance, ctrl->cntlid);
  904. out_unlock:
  905. mutex_unlock(&nvmf_dev_mutex);
  906. return ret;
  907. }
  908. static int nvmf_dev_open(struct inode *inode, struct file *file)
  909. {
  910. /*
  911. * The miscdevice code initializes file->private_data, but doesn't
  912. * make use of it later.
  913. */
  914. file->private_data = NULL;
  915. return single_open(file, nvmf_dev_show, NULL);
  916. }
  917. static int nvmf_dev_release(struct inode *inode, struct file *file)
  918. {
  919. struct seq_file *seq_file = file->private_data;
  920. struct nvme_ctrl *ctrl = seq_file->private;
  921. if (ctrl)
  922. nvme_put_ctrl(ctrl);
  923. return single_release(inode, file);
  924. }
  925. static const struct file_operations nvmf_dev_fops = {
  926. .owner = THIS_MODULE,
  927. .write = nvmf_dev_write,
  928. .read = seq_read,
  929. .open = nvmf_dev_open,
  930. .release = nvmf_dev_release,
  931. };
  932. static struct miscdevice nvmf_misc = {
  933. .minor = MISC_DYNAMIC_MINOR,
  934. .name = "nvme-fabrics",
  935. .fops = &nvmf_dev_fops,
  936. };
  937. static int __init nvmf_init(void)
  938. {
  939. int ret;
  940. nvmf_default_host = nvmf_host_default();
  941. if (!nvmf_default_host)
  942. return -ENOMEM;
  943. nvmf_class = class_create(THIS_MODULE, "nvme-fabrics");
  944. if (IS_ERR(nvmf_class)) {
  945. pr_err("couldn't register class nvme-fabrics\n");
  946. ret = PTR_ERR(nvmf_class);
  947. goto out_free_host;
  948. }
  949. nvmf_device =
  950. device_create(nvmf_class, NULL, MKDEV(0, 0), NULL, "ctl");
  951. if (IS_ERR(nvmf_device)) {
  952. pr_err("couldn't create nvme-fabris device!\n");
  953. ret = PTR_ERR(nvmf_device);
  954. goto out_destroy_class;
  955. }
  956. ret = misc_register(&nvmf_misc);
  957. if (ret) {
  958. pr_err("couldn't register misc device: %d\n", ret);
  959. goto out_destroy_device;
  960. }
  961. return 0;
  962. out_destroy_device:
  963. device_destroy(nvmf_class, MKDEV(0, 0));
  964. out_destroy_class:
  965. class_destroy(nvmf_class);
  966. out_free_host:
  967. nvmf_host_put(nvmf_default_host);
  968. return ret;
  969. }
  970. static void __exit nvmf_exit(void)
  971. {
  972. misc_deregister(&nvmf_misc);
  973. device_destroy(nvmf_class, MKDEV(0, 0));
  974. class_destroy(nvmf_class);
  975. nvmf_host_put(nvmf_default_host);
  976. BUILD_BUG_ON(sizeof(struct nvmf_connect_command) != 64);
  977. BUILD_BUG_ON(sizeof(struct nvmf_property_get_command) != 64);
  978. BUILD_BUG_ON(sizeof(struct nvmf_property_set_command) != 64);
  979. BUILD_BUG_ON(sizeof(struct nvmf_connect_data) != 1024);
  980. }
  981. MODULE_LICENSE("GPL v2");
  982. module_init(nvmf_init);
  983. module_exit(nvmf_exit);