fabrics.c 39 KB

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