loop.c 18 KB

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