fabrics.c 33 KB

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