loop.c 19 KB

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