loop.c 19 KB

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