loop.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * NVMe over Fabrics loopback device.
  3. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/scatterlist.h>
  16. #include <linux/blk-mq.h>
  17. #include <linux/nvme.h>
  18. #include <linux/module.h>
  19. #include <linux/parser.h>
  20. #include "nvmet.h"
  21. #include "../host/nvme.h"
  22. #include "../host/fabrics.h"
  23. #define NVME_LOOP_MAX_SEGMENTS 256
  24. struct nvme_loop_iod {
  25. struct nvme_request nvme_req;
  26. struct nvme_command cmd;
  27. struct nvme_completion rsp;
  28. struct nvmet_req req;
  29. struct nvme_loop_queue *queue;
  30. struct work_struct work;
  31. struct sg_table sg_table;
  32. struct scatterlist first_sgl[];
  33. };
  34. struct nvme_loop_ctrl {
  35. struct nvme_loop_queue *queues;
  36. struct blk_mq_tag_set admin_tag_set;
  37. struct list_head list;
  38. struct blk_mq_tag_set tag_set;
  39. struct nvme_loop_iod async_event_iod;
  40. struct nvme_ctrl ctrl;
  41. struct nvmet_ctrl *target_ctrl;
  42. struct nvmet_port *port;
  43. };
  44. static inline struct nvme_loop_ctrl *to_loop_ctrl(struct nvme_ctrl *ctrl)
  45. {
  46. return container_of(ctrl, struct nvme_loop_ctrl, ctrl);
  47. }
  48. enum nvme_loop_queue_flags {
  49. NVME_LOOP_Q_LIVE = 0,
  50. };
  51. struct nvme_loop_queue {
  52. struct nvmet_cq nvme_cq;
  53. struct nvmet_sq nvme_sq;
  54. struct nvme_loop_ctrl *ctrl;
  55. unsigned long flags;
  56. };
  57. static LIST_HEAD(nvme_loop_ports);
  58. static DEFINE_MUTEX(nvme_loop_ports_mutex);
  59. static LIST_HEAD(nvme_loop_ctrl_list);
  60. static DEFINE_MUTEX(nvme_loop_ctrl_mutex);
  61. static void nvme_loop_queue_response(struct nvmet_req *nvme_req);
  62. static void nvme_loop_delete_ctrl(struct nvmet_ctrl *ctrl);
  63. static const struct nvmet_fabrics_ops nvme_loop_ops;
  64. static inline int nvme_loop_queue_idx(struct nvme_loop_queue *queue)
  65. {
  66. return queue - queue->ctrl->queues;
  67. }
  68. static void nvme_loop_complete_rq(struct request *req)
  69. {
  70. struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
  71. nvme_cleanup_cmd(req);
  72. sg_free_table_chained(&iod->sg_table, true);
  73. nvme_complete_rq(req);
  74. }
  75. static struct blk_mq_tags *nvme_loop_tagset(struct nvme_loop_queue *queue)
  76. {
  77. u32 queue_idx = nvme_loop_queue_idx(queue);
  78. if (queue_idx == 0)
  79. return queue->ctrl->admin_tag_set.tags[queue_idx];
  80. return queue->ctrl->tag_set.tags[queue_idx - 1];
  81. }
  82. static void nvme_loop_queue_response(struct nvmet_req *req)
  83. {
  84. struct nvme_loop_queue *queue =
  85. container_of(req->sq, struct nvme_loop_queue, nvme_sq);
  86. struct nvme_completion *cqe = req->rsp;
  87. /*
  88. * AEN requests are special as they don't time out and can
  89. * survive any kind of queue freeze and often don't respond to
  90. * aborts. We don't even bother to allocate a struct request
  91. * for them but rather special case them here.
  92. */
  93. if (unlikely(nvme_loop_queue_idx(queue) == 0 &&
  94. cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) {
  95. nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
  96. &cqe->result);
  97. } else {
  98. struct request *rq;
  99. rq = blk_mq_tag_to_rq(nvme_loop_tagset(queue), cqe->command_id);
  100. if (!rq) {
  101. dev_err(queue->ctrl->ctrl.device,
  102. "tag 0x%x on queue %d not found\n",
  103. cqe->command_id, nvme_loop_queue_idx(queue));
  104. return;
  105. }
  106. nvme_end_request(rq, cqe->status, cqe->result);
  107. }
  108. }
  109. static void nvme_loop_execute_work(struct work_struct *work)
  110. {
  111. struct nvme_loop_iod *iod =
  112. container_of(work, struct nvme_loop_iod, work);
  113. nvmet_req_execute(&iod->req);
  114. }
  115. static enum blk_eh_timer_return
  116. nvme_loop_timeout(struct request *rq, bool reserved)
  117. {
  118. struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(rq);
  119. /* queue error recovery */
  120. nvme_reset_ctrl(&iod->queue->ctrl->ctrl);
  121. /* fail with DNR on admin cmd timeout */
  122. nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
  123. return BLK_EH_DONE;
  124. }
  125. static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx,
  126. const struct blk_mq_queue_data *bd)
  127. {
  128. struct nvme_ns *ns = hctx->queue->queuedata;
  129. struct nvme_loop_queue *queue = hctx->driver_data;
  130. struct request *req = bd->rq;
  131. struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
  132. bool queue_ready = test_bit(NVME_LOOP_Q_LIVE, &queue->flags);
  133. blk_status_t ret;
  134. if (!nvmf_check_ready(&queue->ctrl->ctrl, req, queue_ready))
  135. return nvmf_fail_nonready_command(req);
  136. ret = nvme_setup_cmd(ns, req, &iod->cmd);
  137. if (ret)
  138. return ret;
  139. blk_mq_start_request(req);
  140. iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
  141. iod->req.port = queue->ctrl->port;
  142. if (!nvmet_req_init(&iod->req, &queue->nvme_cq,
  143. &queue->nvme_sq, &nvme_loop_ops))
  144. return BLK_STS_OK;
  145. if (blk_rq_nr_phys_segments(req)) {
  146. iod->sg_table.sgl = iod->first_sgl;
  147. if (sg_alloc_table_chained(&iod->sg_table,
  148. blk_rq_nr_phys_segments(req),
  149. iod->sg_table.sgl))
  150. return BLK_STS_RESOURCE;
  151. iod->req.sg = iod->sg_table.sgl;
  152. iod->req.sg_cnt = blk_rq_map_sg(req->q, req, iod->sg_table.sgl);
  153. iod->req.transfer_len = blk_rq_payload_bytes(req);
  154. }
  155. schedule_work(&iod->work);
  156. return BLK_STS_OK;
  157. }
  158. static void nvme_loop_submit_async_event(struct nvme_ctrl *arg)
  159. {
  160. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(arg);
  161. struct nvme_loop_queue *queue = &ctrl->queues[0];
  162. struct nvme_loop_iod *iod = &ctrl->async_event_iod;
  163. memset(&iod->cmd, 0, sizeof(iod->cmd));
  164. iod->cmd.common.opcode = nvme_admin_async_event;
  165. iod->cmd.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
  166. iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
  167. if (!nvmet_req_init(&iod->req, &queue->nvme_cq, &queue->nvme_sq,
  168. &nvme_loop_ops)) {
  169. dev_err(ctrl->ctrl.device, "failed async event work\n");
  170. return;
  171. }
  172. schedule_work(&iod->work);
  173. }
  174. static int nvme_loop_init_iod(struct nvme_loop_ctrl *ctrl,
  175. struct nvme_loop_iod *iod, unsigned int queue_idx)
  176. {
  177. iod->req.cmd = &iod->cmd;
  178. iod->req.rsp = &iod->rsp;
  179. iod->queue = &ctrl->queues[queue_idx];
  180. INIT_WORK(&iod->work, nvme_loop_execute_work);
  181. return 0;
  182. }
  183. static int nvme_loop_init_request(struct blk_mq_tag_set *set,
  184. struct request *req, unsigned int hctx_idx,
  185. unsigned int numa_node)
  186. {
  187. struct nvme_loop_ctrl *ctrl = set->driver_data;
  188. return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req),
  189. (set == &ctrl->tag_set) ? hctx_idx + 1 : 0);
  190. }
  191. static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  192. unsigned int hctx_idx)
  193. {
  194. struct nvme_loop_ctrl *ctrl = data;
  195. struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1];
  196. BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
  197. hctx->driver_data = queue;
  198. return 0;
  199. }
  200. static int nvme_loop_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  201. unsigned int hctx_idx)
  202. {
  203. struct nvme_loop_ctrl *ctrl = data;
  204. struct nvme_loop_queue *queue = &ctrl->queues[0];
  205. BUG_ON(hctx_idx != 0);
  206. hctx->driver_data = queue;
  207. return 0;
  208. }
  209. static const struct blk_mq_ops nvme_loop_mq_ops = {
  210. .queue_rq = nvme_loop_queue_rq,
  211. .complete = nvme_loop_complete_rq,
  212. .init_request = nvme_loop_init_request,
  213. .init_hctx = nvme_loop_init_hctx,
  214. .timeout = nvme_loop_timeout,
  215. };
  216. static const struct blk_mq_ops nvme_loop_admin_mq_ops = {
  217. .queue_rq = nvme_loop_queue_rq,
  218. .complete = nvme_loop_complete_rq,
  219. .init_request = nvme_loop_init_request,
  220. .init_hctx = nvme_loop_init_admin_hctx,
  221. .timeout = nvme_loop_timeout,
  222. };
  223. static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl)
  224. {
  225. clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
  226. nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
  227. blk_cleanup_queue(ctrl->ctrl.admin_q);
  228. blk_mq_free_tag_set(&ctrl->admin_tag_set);
  229. }
  230. static void nvme_loop_free_ctrl(struct nvme_ctrl *nctrl)
  231. {
  232. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
  233. if (list_empty(&ctrl->list))
  234. goto free_ctrl;
  235. mutex_lock(&nvme_loop_ctrl_mutex);
  236. list_del(&ctrl->list);
  237. mutex_unlock(&nvme_loop_ctrl_mutex);
  238. if (nctrl->tagset) {
  239. blk_cleanup_queue(ctrl->ctrl.connect_q);
  240. blk_mq_free_tag_set(&ctrl->tag_set);
  241. }
  242. kfree(ctrl->queues);
  243. nvmf_free_options(nctrl->opts);
  244. free_ctrl:
  245. kfree(ctrl);
  246. }
  247. static void nvme_loop_destroy_io_queues(struct nvme_loop_ctrl *ctrl)
  248. {
  249. int i;
  250. for (i = 1; i < ctrl->ctrl.queue_count; i++) {
  251. clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
  252. nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
  253. }
  254. }
  255. static int nvme_loop_init_io_queues(struct nvme_loop_ctrl *ctrl)
  256. {
  257. struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
  258. unsigned int nr_io_queues;
  259. int ret, i;
  260. nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
  261. ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
  262. if (ret || !nr_io_queues)
  263. return ret;
  264. dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", nr_io_queues);
  265. for (i = 1; i <= nr_io_queues; i++) {
  266. ctrl->queues[i].ctrl = ctrl;
  267. ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq);
  268. if (ret)
  269. goto out_destroy_queues;
  270. ctrl->ctrl.queue_count++;
  271. }
  272. return 0;
  273. out_destroy_queues:
  274. nvme_loop_destroy_io_queues(ctrl);
  275. return ret;
  276. }
  277. static int nvme_loop_connect_io_queues(struct nvme_loop_ctrl *ctrl)
  278. {
  279. int i, ret;
  280. for (i = 1; i < ctrl->ctrl.queue_count; i++) {
  281. ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
  282. if (ret)
  283. return ret;
  284. set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
  285. }
  286. return 0;
  287. }
  288. static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl)
  289. {
  290. int error;
  291. memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
  292. ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops;
  293. ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
  294. ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
  295. ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
  296. ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
  297. SG_CHUNK_SIZE * sizeof(struct scatterlist);
  298. ctrl->admin_tag_set.driver_data = ctrl;
  299. ctrl->admin_tag_set.nr_hw_queues = 1;
  300. ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
  301. ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
  302. ctrl->queues[0].ctrl = ctrl;
  303. error = nvmet_sq_init(&ctrl->queues[0].nvme_sq);
  304. if (error)
  305. return error;
  306. ctrl->ctrl.queue_count = 1;
  307. error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
  308. if (error)
  309. goto out_free_sq;
  310. ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
  311. ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
  312. if (IS_ERR(ctrl->ctrl.admin_q)) {
  313. error = PTR_ERR(ctrl->ctrl.admin_q);
  314. goto out_free_tagset;
  315. }
  316. error = nvmf_connect_admin_queue(&ctrl->ctrl);
  317. if (error)
  318. goto out_cleanup_queue;
  319. set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
  320. error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap);
  321. if (error) {
  322. dev_err(ctrl->ctrl.device,
  323. "prop_get NVME_REG_CAP failed\n");
  324. goto out_cleanup_queue;
  325. }
  326. ctrl->ctrl.sqsize =
  327. min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
  328. error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
  329. if (error)
  330. goto out_cleanup_queue;
  331. ctrl->ctrl.max_hw_sectors =
  332. (NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9);
  333. error = nvme_init_identify(&ctrl->ctrl);
  334. if (error)
  335. goto out_cleanup_queue;
  336. return 0;
  337. out_cleanup_queue:
  338. blk_cleanup_queue(ctrl->ctrl.admin_q);
  339. out_free_tagset:
  340. blk_mq_free_tag_set(&ctrl->admin_tag_set);
  341. out_free_sq:
  342. nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
  343. return error;
  344. }
  345. static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
  346. {
  347. if (ctrl->ctrl.queue_count > 1) {
  348. nvme_stop_queues(&ctrl->ctrl);
  349. blk_mq_tagset_busy_iter(&ctrl->tag_set,
  350. nvme_cancel_request, &ctrl->ctrl);
  351. nvme_loop_destroy_io_queues(ctrl);
  352. }
  353. if (ctrl->ctrl.state == NVME_CTRL_LIVE)
  354. nvme_shutdown_ctrl(&ctrl->ctrl);
  355. blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
  356. blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
  357. nvme_cancel_request, &ctrl->ctrl);
  358. blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
  359. nvme_loop_destroy_admin_queue(ctrl);
  360. }
  361. static void nvme_loop_delete_ctrl_host(struct nvme_ctrl *ctrl)
  362. {
  363. nvme_loop_shutdown_ctrl(to_loop_ctrl(ctrl));
  364. }
  365. static void nvme_loop_delete_ctrl(struct nvmet_ctrl *nctrl)
  366. {
  367. struct nvme_loop_ctrl *ctrl;
  368. mutex_lock(&nvme_loop_ctrl_mutex);
  369. list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) {
  370. if (ctrl->ctrl.cntlid == nctrl->cntlid)
  371. nvme_delete_ctrl(&ctrl->ctrl);
  372. }
  373. mutex_unlock(&nvme_loop_ctrl_mutex);
  374. }
  375. static void nvme_loop_reset_ctrl_work(struct work_struct *work)
  376. {
  377. struct nvme_loop_ctrl *ctrl =
  378. container_of(work, struct nvme_loop_ctrl, ctrl.reset_work);
  379. bool changed;
  380. int ret;
  381. nvme_stop_ctrl(&ctrl->ctrl);
  382. nvme_loop_shutdown_ctrl(ctrl);
  383. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
  384. /* state change failure should never happen */
  385. WARN_ON_ONCE(1);
  386. return;
  387. }
  388. ret = nvme_loop_configure_admin_queue(ctrl);
  389. if (ret)
  390. goto out_disable;
  391. ret = nvme_loop_init_io_queues(ctrl);
  392. if (ret)
  393. goto out_destroy_admin;
  394. ret = nvme_loop_connect_io_queues(ctrl);
  395. if (ret)
  396. goto out_destroy_io;
  397. blk_mq_update_nr_hw_queues(&ctrl->tag_set,
  398. ctrl->ctrl.queue_count - 1);
  399. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
  400. WARN_ON_ONCE(!changed);
  401. nvme_start_ctrl(&ctrl->ctrl);
  402. return;
  403. out_destroy_io:
  404. nvme_loop_destroy_io_queues(ctrl);
  405. out_destroy_admin:
  406. nvme_loop_destroy_admin_queue(ctrl);
  407. out_disable:
  408. dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
  409. nvme_uninit_ctrl(&ctrl->ctrl);
  410. nvme_put_ctrl(&ctrl->ctrl);
  411. }
  412. static const struct nvme_ctrl_ops nvme_loop_ctrl_ops = {
  413. .name = "loop",
  414. .module = THIS_MODULE,
  415. .flags = NVME_F_FABRICS,
  416. .reg_read32 = nvmf_reg_read32,
  417. .reg_read64 = nvmf_reg_read64,
  418. .reg_write32 = nvmf_reg_write32,
  419. .free_ctrl = nvme_loop_free_ctrl,
  420. .submit_async_event = nvme_loop_submit_async_event,
  421. .delete_ctrl = nvme_loop_delete_ctrl_host,
  422. .get_address = nvmf_get_address,
  423. };
  424. static int nvme_loop_create_io_queues(struct nvme_loop_ctrl *ctrl)
  425. {
  426. int ret;
  427. ret = nvme_loop_init_io_queues(ctrl);
  428. if (ret)
  429. return ret;
  430. memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
  431. ctrl->tag_set.ops = &nvme_loop_mq_ops;
  432. ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
  433. ctrl->tag_set.reserved_tags = 1; /* fabric connect */
  434. ctrl->tag_set.numa_node = NUMA_NO_NODE;
  435. ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  436. ctrl->tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
  437. SG_CHUNK_SIZE * sizeof(struct scatterlist);
  438. ctrl->tag_set.driver_data = ctrl;
  439. ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
  440. ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
  441. ctrl->ctrl.tagset = &ctrl->tag_set;
  442. ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
  443. if (ret)
  444. goto out_destroy_queues;
  445. ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
  446. if (IS_ERR(ctrl->ctrl.connect_q)) {
  447. ret = PTR_ERR(ctrl->ctrl.connect_q);
  448. goto out_free_tagset;
  449. }
  450. ret = nvme_loop_connect_io_queues(ctrl);
  451. if (ret)
  452. goto out_cleanup_connect_q;
  453. return 0;
  454. out_cleanup_connect_q:
  455. blk_cleanup_queue(ctrl->ctrl.connect_q);
  456. out_free_tagset:
  457. blk_mq_free_tag_set(&ctrl->tag_set);
  458. out_destroy_queues:
  459. nvme_loop_destroy_io_queues(ctrl);
  460. return ret;
  461. }
  462. static struct nvmet_port *nvme_loop_find_port(struct nvme_ctrl *ctrl)
  463. {
  464. struct nvmet_port *p, *found = NULL;
  465. mutex_lock(&nvme_loop_ports_mutex);
  466. list_for_each_entry(p, &nvme_loop_ports, entry) {
  467. /* if no transport address is specified use the first port */
  468. if ((ctrl->opts->mask & NVMF_OPT_TRADDR) &&
  469. strcmp(ctrl->opts->traddr, p->disc_addr.traddr))
  470. continue;
  471. found = p;
  472. break;
  473. }
  474. mutex_unlock(&nvme_loop_ports_mutex);
  475. return found;
  476. }
  477. static struct nvme_ctrl *nvme_loop_create_ctrl(struct device *dev,
  478. struct nvmf_ctrl_options *opts)
  479. {
  480. struct nvme_loop_ctrl *ctrl;
  481. bool changed;
  482. int ret;
  483. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  484. if (!ctrl)
  485. return ERR_PTR(-ENOMEM);
  486. ctrl->ctrl.opts = opts;
  487. INIT_LIST_HEAD(&ctrl->list);
  488. INIT_WORK(&ctrl->ctrl.reset_work, nvme_loop_reset_ctrl_work);
  489. ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_loop_ctrl_ops,
  490. 0 /* no quirks, we're perfect! */);
  491. if (ret)
  492. goto out_put_ctrl;
  493. ret = -ENOMEM;
  494. ctrl->ctrl.sqsize = opts->queue_size - 1;
  495. ctrl->ctrl.kato = opts->kato;
  496. ctrl->port = nvme_loop_find_port(&ctrl->ctrl);
  497. ctrl->queues = kcalloc(opts->nr_io_queues + 1, sizeof(*ctrl->queues),
  498. GFP_KERNEL);
  499. if (!ctrl->queues)
  500. goto out_uninit_ctrl;
  501. ret = nvme_loop_configure_admin_queue(ctrl);
  502. if (ret)
  503. goto out_free_queues;
  504. if (opts->queue_size > ctrl->ctrl.maxcmd) {
  505. /* warn if maxcmd is lower than queue_size */
  506. dev_warn(ctrl->ctrl.device,
  507. "queue_size %zu > ctrl maxcmd %u, clamping down\n",
  508. opts->queue_size, ctrl->ctrl.maxcmd);
  509. opts->queue_size = ctrl->ctrl.maxcmd;
  510. }
  511. if (opts->nr_io_queues) {
  512. ret = nvme_loop_create_io_queues(ctrl);
  513. if (ret)
  514. goto out_remove_admin_queue;
  515. }
  516. nvme_loop_init_iod(ctrl, &ctrl->async_event_iod, 0);
  517. dev_info(ctrl->ctrl.device,
  518. "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn);
  519. nvme_get_ctrl(&ctrl->ctrl);
  520. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
  521. WARN_ON_ONCE(!changed);
  522. mutex_lock(&nvme_loop_ctrl_mutex);
  523. list_add_tail(&ctrl->list, &nvme_loop_ctrl_list);
  524. mutex_unlock(&nvme_loop_ctrl_mutex);
  525. nvme_start_ctrl(&ctrl->ctrl);
  526. return &ctrl->ctrl;
  527. out_remove_admin_queue:
  528. nvme_loop_destroy_admin_queue(ctrl);
  529. out_free_queues:
  530. kfree(ctrl->queues);
  531. out_uninit_ctrl:
  532. nvme_uninit_ctrl(&ctrl->ctrl);
  533. out_put_ctrl:
  534. nvme_put_ctrl(&ctrl->ctrl);
  535. if (ret > 0)
  536. ret = -EIO;
  537. return ERR_PTR(ret);
  538. }
  539. static int nvme_loop_add_port(struct nvmet_port *port)
  540. {
  541. mutex_lock(&nvme_loop_ports_mutex);
  542. list_add_tail(&port->entry, &nvme_loop_ports);
  543. mutex_unlock(&nvme_loop_ports_mutex);
  544. return 0;
  545. }
  546. static void nvme_loop_remove_port(struct nvmet_port *port)
  547. {
  548. mutex_lock(&nvme_loop_ports_mutex);
  549. list_del_init(&port->entry);
  550. mutex_unlock(&nvme_loop_ports_mutex);
  551. }
  552. static const struct nvmet_fabrics_ops nvme_loop_ops = {
  553. .owner = THIS_MODULE,
  554. .type = NVMF_TRTYPE_LOOP,
  555. .add_port = nvme_loop_add_port,
  556. .remove_port = nvme_loop_remove_port,
  557. .queue_response = nvme_loop_queue_response,
  558. .delete_ctrl = nvme_loop_delete_ctrl,
  559. };
  560. static struct nvmf_transport_ops nvme_loop_transport = {
  561. .name = "loop",
  562. .module = THIS_MODULE,
  563. .create_ctrl = nvme_loop_create_ctrl,
  564. .allowed_opts = NVMF_OPT_TRADDR,
  565. };
  566. static int __init nvme_loop_init_module(void)
  567. {
  568. int ret;
  569. ret = nvmet_register_transport(&nvme_loop_ops);
  570. if (ret)
  571. return ret;
  572. ret = nvmf_register_transport(&nvme_loop_transport);
  573. if (ret)
  574. nvmet_unregister_transport(&nvme_loop_ops);
  575. return ret;
  576. }
  577. static void __exit nvme_loop_cleanup_module(void)
  578. {
  579. struct nvme_loop_ctrl *ctrl, *next;
  580. nvmf_unregister_transport(&nvme_loop_transport);
  581. nvmet_unregister_transport(&nvme_loop_ops);
  582. mutex_lock(&nvme_loop_ctrl_mutex);
  583. list_for_each_entry_safe(ctrl, next, &nvme_loop_ctrl_list, list)
  584. nvme_delete_ctrl(&ctrl->ctrl);
  585. mutex_unlock(&nvme_loop_ctrl_mutex);
  586. flush_workqueue(nvme_delete_wq);
  587. }
  588. module_init(nvme_loop_init_module);
  589. module_exit(nvme_loop_cleanup_module);
  590. MODULE_LICENSE("GPL v2");
  591. MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */