loop.c 18 KB

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