loop.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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(&queue->ctrl->ctrl, 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. nvme_req(req)->ctrl = &ctrl->ctrl;
  189. return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req),
  190. (set == &ctrl->tag_set) ? hctx_idx + 1 : 0);
  191. }
  192. static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  193. unsigned int hctx_idx)
  194. {
  195. struct nvme_loop_ctrl *ctrl = data;
  196. struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1];
  197. BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
  198. hctx->driver_data = queue;
  199. return 0;
  200. }
  201. static int nvme_loop_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  202. unsigned int hctx_idx)
  203. {
  204. struct nvme_loop_ctrl *ctrl = data;
  205. struct nvme_loop_queue *queue = &ctrl->queues[0];
  206. BUG_ON(hctx_idx != 0);
  207. hctx->driver_data = queue;
  208. return 0;
  209. }
  210. static const struct blk_mq_ops nvme_loop_mq_ops = {
  211. .queue_rq = nvme_loop_queue_rq,
  212. .complete = nvme_loop_complete_rq,
  213. .init_request = nvme_loop_init_request,
  214. .init_hctx = nvme_loop_init_hctx,
  215. .timeout = nvme_loop_timeout,
  216. };
  217. static const struct blk_mq_ops nvme_loop_admin_mq_ops = {
  218. .queue_rq = nvme_loop_queue_rq,
  219. .complete = nvme_loop_complete_rq,
  220. .init_request = nvme_loop_init_request,
  221. .init_hctx = nvme_loop_init_admin_hctx,
  222. .timeout = nvme_loop_timeout,
  223. };
  224. static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl)
  225. {
  226. clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
  227. nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
  228. blk_cleanup_queue(ctrl->ctrl.admin_q);
  229. blk_mq_free_tag_set(&ctrl->admin_tag_set);
  230. }
  231. static void nvme_loop_free_ctrl(struct nvme_ctrl *nctrl)
  232. {
  233. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
  234. if (list_empty(&ctrl->list))
  235. goto free_ctrl;
  236. mutex_lock(&nvme_loop_ctrl_mutex);
  237. list_del(&ctrl->list);
  238. mutex_unlock(&nvme_loop_ctrl_mutex);
  239. if (nctrl->tagset) {
  240. blk_cleanup_queue(ctrl->ctrl.connect_q);
  241. blk_mq_free_tag_set(&ctrl->tag_set);
  242. }
  243. kfree(ctrl->queues);
  244. nvmf_free_options(nctrl->opts);
  245. free_ctrl:
  246. kfree(ctrl);
  247. }
  248. static void nvme_loop_destroy_io_queues(struct nvme_loop_ctrl *ctrl)
  249. {
  250. int i;
  251. for (i = 1; i < ctrl->ctrl.queue_count; i++) {
  252. clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
  253. nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
  254. }
  255. }
  256. static int nvme_loop_init_io_queues(struct nvme_loop_ctrl *ctrl)
  257. {
  258. struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
  259. unsigned int nr_io_queues;
  260. int ret, i;
  261. nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
  262. ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
  263. if (ret || !nr_io_queues)
  264. return ret;
  265. dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", nr_io_queues);
  266. for (i = 1; i <= nr_io_queues; i++) {
  267. ctrl->queues[i].ctrl = ctrl;
  268. ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq);
  269. if (ret)
  270. goto out_destroy_queues;
  271. ctrl->ctrl.queue_count++;
  272. }
  273. return 0;
  274. out_destroy_queues:
  275. nvme_loop_destroy_io_queues(ctrl);
  276. return ret;
  277. }
  278. static int nvme_loop_connect_io_queues(struct nvme_loop_ctrl *ctrl)
  279. {
  280. int i, ret;
  281. for (i = 1; i < ctrl->ctrl.queue_count; i++) {
  282. ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
  283. if (ret)
  284. return ret;
  285. set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
  286. }
  287. return 0;
  288. }
  289. static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl)
  290. {
  291. int error;
  292. memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
  293. ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops;
  294. ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
  295. ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
  296. ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
  297. ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
  298. SG_CHUNK_SIZE * sizeof(struct scatterlist);
  299. ctrl->admin_tag_set.driver_data = ctrl;
  300. ctrl->admin_tag_set.nr_hw_queues = 1;
  301. ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
  302. ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
  303. ctrl->queues[0].ctrl = ctrl;
  304. error = nvmet_sq_init(&ctrl->queues[0].nvme_sq);
  305. if (error)
  306. return error;
  307. ctrl->ctrl.queue_count = 1;
  308. error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
  309. if (error)
  310. goto out_free_sq;
  311. ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
  312. ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
  313. if (IS_ERR(ctrl->ctrl.admin_q)) {
  314. error = PTR_ERR(ctrl->ctrl.admin_q);
  315. goto out_free_tagset;
  316. }
  317. error = nvmf_connect_admin_queue(&ctrl->ctrl);
  318. if (error)
  319. goto out_cleanup_queue;
  320. set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
  321. error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap);
  322. if (error) {
  323. dev_err(ctrl->ctrl.device,
  324. "prop_get NVME_REG_CAP failed\n");
  325. goto out_cleanup_queue;
  326. }
  327. ctrl->ctrl.sqsize =
  328. min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
  329. error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
  330. if (error)
  331. goto out_cleanup_queue;
  332. ctrl->ctrl.max_hw_sectors =
  333. (NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9);
  334. error = nvme_init_identify(&ctrl->ctrl);
  335. if (error)
  336. goto out_cleanup_queue;
  337. return 0;
  338. out_cleanup_queue:
  339. blk_cleanup_queue(ctrl->ctrl.admin_q);
  340. out_free_tagset:
  341. blk_mq_free_tag_set(&ctrl->admin_tag_set);
  342. out_free_sq:
  343. nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
  344. return error;
  345. }
  346. static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
  347. {
  348. if (ctrl->ctrl.queue_count > 1) {
  349. nvme_stop_queues(&ctrl->ctrl);
  350. blk_mq_tagset_busy_iter(&ctrl->tag_set,
  351. nvme_cancel_request, &ctrl->ctrl);
  352. nvme_loop_destroy_io_queues(ctrl);
  353. }
  354. if (ctrl->ctrl.state == NVME_CTRL_LIVE)
  355. nvme_shutdown_ctrl(&ctrl->ctrl);
  356. blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
  357. blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
  358. nvme_cancel_request, &ctrl->ctrl);
  359. blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
  360. nvme_loop_destroy_admin_queue(ctrl);
  361. }
  362. static void nvme_loop_delete_ctrl_host(struct nvme_ctrl *ctrl)
  363. {
  364. nvme_loop_shutdown_ctrl(to_loop_ctrl(ctrl));
  365. }
  366. static void nvme_loop_delete_ctrl(struct nvmet_ctrl *nctrl)
  367. {
  368. struct nvme_loop_ctrl *ctrl;
  369. mutex_lock(&nvme_loop_ctrl_mutex);
  370. list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) {
  371. if (ctrl->ctrl.cntlid == nctrl->cntlid)
  372. nvme_delete_ctrl(&ctrl->ctrl);
  373. }
  374. mutex_unlock(&nvme_loop_ctrl_mutex);
  375. }
  376. static void nvme_loop_reset_ctrl_work(struct work_struct *work)
  377. {
  378. struct nvme_loop_ctrl *ctrl =
  379. container_of(work, struct nvme_loop_ctrl, ctrl.reset_work);
  380. bool changed;
  381. int ret;
  382. nvme_stop_ctrl(&ctrl->ctrl);
  383. nvme_loop_shutdown_ctrl(ctrl);
  384. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
  385. /* state change failure should never happen */
  386. WARN_ON_ONCE(1);
  387. return;
  388. }
  389. ret = nvme_loop_configure_admin_queue(ctrl);
  390. if (ret)
  391. goto out_disable;
  392. ret = nvme_loop_init_io_queues(ctrl);
  393. if (ret)
  394. goto out_destroy_admin;
  395. ret = nvme_loop_connect_io_queues(ctrl);
  396. if (ret)
  397. goto out_destroy_io;
  398. blk_mq_update_nr_hw_queues(&ctrl->tag_set,
  399. ctrl->ctrl.queue_count - 1);
  400. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
  401. WARN_ON_ONCE(!changed);
  402. nvme_start_ctrl(&ctrl->ctrl);
  403. return;
  404. out_destroy_io:
  405. nvme_loop_destroy_io_queues(ctrl);
  406. out_destroy_admin:
  407. nvme_loop_destroy_admin_queue(ctrl);
  408. out_disable:
  409. dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
  410. nvme_uninit_ctrl(&ctrl->ctrl);
  411. nvme_put_ctrl(&ctrl->ctrl);
  412. }
  413. static const struct nvme_ctrl_ops nvme_loop_ctrl_ops = {
  414. .name = "loop",
  415. .module = THIS_MODULE,
  416. .flags = NVME_F_FABRICS,
  417. .reg_read32 = nvmf_reg_read32,
  418. .reg_read64 = nvmf_reg_read64,
  419. .reg_write32 = nvmf_reg_write32,
  420. .free_ctrl = nvme_loop_free_ctrl,
  421. .submit_async_event = nvme_loop_submit_async_event,
  422. .delete_ctrl = nvme_loop_delete_ctrl_host,
  423. .get_address = nvmf_get_address,
  424. };
  425. static int nvme_loop_create_io_queues(struct nvme_loop_ctrl *ctrl)
  426. {
  427. int ret;
  428. ret = nvme_loop_init_io_queues(ctrl);
  429. if (ret)
  430. return ret;
  431. memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
  432. ctrl->tag_set.ops = &nvme_loop_mq_ops;
  433. ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
  434. ctrl->tag_set.reserved_tags = 1; /* fabric connect */
  435. ctrl->tag_set.numa_node = NUMA_NO_NODE;
  436. ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  437. ctrl->tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
  438. SG_CHUNK_SIZE * sizeof(struct scatterlist);
  439. ctrl->tag_set.driver_data = ctrl;
  440. ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
  441. ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
  442. ctrl->ctrl.tagset = &ctrl->tag_set;
  443. ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
  444. if (ret)
  445. goto out_destroy_queues;
  446. ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
  447. if (IS_ERR(ctrl->ctrl.connect_q)) {
  448. ret = PTR_ERR(ctrl->ctrl.connect_q);
  449. goto out_free_tagset;
  450. }
  451. ret = nvme_loop_connect_io_queues(ctrl);
  452. if (ret)
  453. goto out_cleanup_connect_q;
  454. return 0;
  455. out_cleanup_connect_q:
  456. blk_cleanup_queue(ctrl->ctrl.connect_q);
  457. out_free_tagset:
  458. blk_mq_free_tag_set(&ctrl->tag_set);
  459. out_destroy_queues:
  460. nvme_loop_destroy_io_queues(ctrl);
  461. return ret;
  462. }
  463. static struct nvmet_port *nvme_loop_find_port(struct nvme_ctrl *ctrl)
  464. {
  465. struct nvmet_port *p, *found = NULL;
  466. mutex_lock(&nvme_loop_ports_mutex);
  467. list_for_each_entry(p, &nvme_loop_ports, entry) {
  468. /* if no transport address is specified use the first port */
  469. if ((ctrl->opts->mask & NVMF_OPT_TRADDR) &&
  470. strcmp(ctrl->opts->traddr, p->disc_addr.traddr))
  471. continue;
  472. found = p;
  473. break;
  474. }
  475. mutex_unlock(&nvme_loop_ports_mutex);
  476. return found;
  477. }
  478. static struct nvme_ctrl *nvme_loop_create_ctrl(struct device *dev,
  479. struct nvmf_ctrl_options *opts)
  480. {
  481. struct nvme_loop_ctrl *ctrl;
  482. bool changed;
  483. int ret;
  484. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  485. if (!ctrl)
  486. return ERR_PTR(-ENOMEM);
  487. ctrl->ctrl.opts = opts;
  488. INIT_LIST_HEAD(&ctrl->list);
  489. INIT_WORK(&ctrl->ctrl.reset_work, nvme_loop_reset_ctrl_work);
  490. ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_loop_ctrl_ops,
  491. 0 /* no quirks, we're perfect! */);
  492. if (ret)
  493. goto out_put_ctrl;
  494. ret = -ENOMEM;
  495. ctrl->ctrl.sqsize = opts->queue_size - 1;
  496. ctrl->ctrl.kato = opts->kato;
  497. ctrl->port = nvme_loop_find_port(&ctrl->ctrl);
  498. ctrl->queues = kcalloc(opts->nr_io_queues + 1, sizeof(*ctrl->queues),
  499. GFP_KERNEL);
  500. if (!ctrl->queues)
  501. goto out_uninit_ctrl;
  502. ret = nvme_loop_configure_admin_queue(ctrl);
  503. if (ret)
  504. goto out_free_queues;
  505. if (opts->queue_size > ctrl->ctrl.maxcmd) {
  506. /* warn if maxcmd is lower than queue_size */
  507. dev_warn(ctrl->ctrl.device,
  508. "queue_size %zu > ctrl maxcmd %u, clamping down\n",
  509. opts->queue_size, ctrl->ctrl.maxcmd);
  510. opts->queue_size = ctrl->ctrl.maxcmd;
  511. }
  512. if (opts->nr_io_queues) {
  513. ret = nvme_loop_create_io_queues(ctrl);
  514. if (ret)
  515. goto out_remove_admin_queue;
  516. }
  517. nvme_loop_init_iod(ctrl, &ctrl->async_event_iod, 0);
  518. dev_info(ctrl->ctrl.device,
  519. "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn);
  520. nvme_get_ctrl(&ctrl->ctrl);
  521. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
  522. WARN_ON_ONCE(!changed);
  523. mutex_lock(&nvme_loop_ctrl_mutex);
  524. list_add_tail(&ctrl->list, &nvme_loop_ctrl_list);
  525. mutex_unlock(&nvme_loop_ctrl_mutex);
  526. nvme_start_ctrl(&ctrl->ctrl);
  527. return &ctrl->ctrl;
  528. out_remove_admin_queue:
  529. nvme_loop_destroy_admin_queue(ctrl);
  530. out_free_queues:
  531. kfree(ctrl->queues);
  532. out_uninit_ctrl:
  533. nvme_uninit_ctrl(&ctrl->ctrl);
  534. out_put_ctrl:
  535. nvme_put_ctrl(&ctrl->ctrl);
  536. if (ret > 0)
  537. ret = -EIO;
  538. return ERR_PTR(ret);
  539. }
  540. static int nvme_loop_add_port(struct nvmet_port *port)
  541. {
  542. mutex_lock(&nvme_loop_ports_mutex);
  543. list_add_tail(&port->entry, &nvme_loop_ports);
  544. mutex_unlock(&nvme_loop_ports_mutex);
  545. return 0;
  546. }
  547. static void nvme_loop_remove_port(struct nvmet_port *port)
  548. {
  549. mutex_lock(&nvme_loop_ports_mutex);
  550. list_del_init(&port->entry);
  551. mutex_unlock(&nvme_loop_ports_mutex);
  552. /*
  553. * Ensure any ctrls that are in the process of being
  554. * deleted are in fact deleted before we return
  555. * and free the port. This is to prevent active
  556. * ctrls from using a port after it's freed.
  557. */
  558. flush_workqueue(nvme_delete_wq);
  559. }
  560. static const struct nvmet_fabrics_ops nvme_loop_ops = {
  561. .owner = THIS_MODULE,
  562. .type = NVMF_TRTYPE_LOOP,
  563. .add_port = nvme_loop_add_port,
  564. .remove_port = nvme_loop_remove_port,
  565. .queue_response = nvme_loop_queue_response,
  566. .delete_ctrl = nvme_loop_delete_ctrl,
  567. };
  568. static struct nvmf_transport_ops nvme_loop_transport = {
  569. .name = "loop",
  570. .module = THIS_MODULE,
  571. .create_ctrl = nvme_loop_create_ctrl,
  572. .allowed_opts = NVMF_OPT_TRADDR,
  573. };
  574. static int __init nvme_loop_init_module(void)
  575. {
  576. int ret;
  577. ret = nvmet_register_transport(&nvme_loop_ops);
  578. if (ret)
  579. return ret;
  580. ret = nvmf_register_transport(&nvme_loop_transport);
  581. if (ret)
  582. nvmet_unregister_transport(&nvme_loop_ops);
  583. return ret;
  584. }
  585. static void __exit nvme_loop_cleanup_module(void)
  586. {
  587. struct nvme_loop_ctrl *ctrl, *next;
  588. nvmf_unregister_transport(&nvme_loop_transport);
  589. nvmet_unregister_transport(&nvme_loop_ops);
  590. mutex_lock(&nvme_loop_ctrl_mutex);
  591. list_for_each_entry_safe(ctrl, next, &nvme_loop_ctrl_list, list)
  592. nvme_delete_ctrl(&ctrl->ctrl);
  593. mutex_unlock(&nvme_loop_ctrl_mutex);
  594. flush_workqueue(nvme_delete_wq);
  595. }
  596. module_init(nvme_loop_init_module);
  597. module_exit(nvme_loop_cleanup_module);
  598. MODULE_LICENSE("GPL v2");
  599. MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */