loop.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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. .map_queue = blk_mq_map_queue,
  226. .init_request = nvme_loop_init_request,
  227. .init_hctx = nvme_loop_init_hctx,
  228. .timeout = nvme_loop_timeout,
  229. };
  230. static struct blk_mq_ops nvme_loop_admin_mq_ops = {
  231. .queue_rq = nvme_loop_queue_rq,
  232. .complete = nvme_loop_complete_rq,
  233. .map_queue = blk_mq_map_queue,
  234. .init_request = nvme_loop_init_admin_request,
  235. .init_hctx = nvme_loop_init_admin_hctx,
  236. .timeout = nvme_loop_timeout,
  237. };
  238. static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl)
  239. {
  240. blk_cleanup_queue(ctrl->ctrl.admin_q);
  241. blk_mq_free_tag_set(&ctrl->admin_tag_set);
  242. nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
  243. }
  244. static void nvme_loop_free_ctrl(struct nvme_ctrl *nctrl)
  245. {
  246. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
  247. if (list_empty(&ctrl->list))
  248. goto free_ctrl;
  249. mutex_lock(&nvme_loop_ctrl_mutex);
  250. list_del(&ctrl->list);
  251. mutex_unlock(&nvme_loop_ctrl_mutex);
  252. if (nctrl->tagset) {
  253. blk_cleanup_queue(ctrl->ctrl.connect_q);
  254. blk_mq_free_tag_set(&ctrl->tag_set);
  255. }
  256. kfree(ctrl->queues);
  257. nvmf_free_options(nctrl->opts);
  258. free_ctrl:
  259. kfree(ctrl);
  260. }
  261. static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl)
  262. {
  263. int error;
  264. memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
  265. ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops;
  266. ctrl->admin_tag_set.queue_depth = NVME_LOOP_AQ_BLKMQ_DEPTH;
  267. ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
  268. ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
  269. ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
  270. SG_CHUNK_SIZE * sizeof(struct scatterlist);
  271. ctrl->admin_tag_set.driver_data = ctrl;
  272. ctrl->admin_tag_set.nr_hw_queues = 1;
  273. ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
  274. ctrl->queues[0].ctrl = ctrl;
  275. error = nvmet_sq_init(&ctrl->queues[0].nvme_sq);
  276. if (error)
  277. return error;
  278. ctrl->queue_count = 1;
  279. error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
  280. if (error)
  281. goto out_free_sq;
  282. ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
  283. if (IS_ERR(ctrl->ctrl.admin_q)) {
  284. error = PTR_ERR(ctrl->ctrl.admin_q);
  285. goto out_free_tagset;
  286. }
  287. error = nvmf_connect_admin_queue(&ctrl->ctrl);
  288. if (error)
  289. goto out_cleanup_queue;
  290. error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
  291. if (error) {
  292. dev_err(ctrl->ctrl.device,
  293. "prop_get NVME_REG_CAP failed\n");
  294. goto out_cleanup_queue;
  295. }
  296. ctrl->ctrl.sqsize =
  297. min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
  298. error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
  299. if (error)
  300. goto out_cleanup_queue;
  301. ctrl->ctrl.max_hw_sectors =
  302. (NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9);
  303. error = nvme_init_identify(&ctrl->ctrl);
  304. if (error)
  305. goto out_cleanup_queue;
  306. nvme_start_keep_alive(&ctrl->ctrl);
  307. return 0;
  308. out_cleanup_queue:
  309. blk_cleanup_queue(ctrl->ctrl.admin_q);
  310. out_free_tagset:
  311. blk_mq_free_tag_set(&ctrl->admin_tag_set);
  312. out_free_sq:
  313. nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
  314. return error;
  315. }
  316. static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
  317. {
  318. int i;
  319. nvme_stop_keep_alive(&ctrl->ctrl);
  320. if (ctrl->queue_count > 1) {
  321. nvme_stop_queues(&ctrl->ctrl);
  322. blk_mq_tagset_busy_iter(&ctrl->tag_set,
  323. nvme_cancel_request, &ctrl->ctrl);
  324. for (i = 1; i < ctrl->queue_count; i++)
  325. nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
  326. }
  327. if (ctrl->ctrl.state == NVME_CTRL_LIVE)
  328. nvme_shutdown_ctrl(&ctrl->ctrl);
  329. blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
  330. blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
  331. nvme_cancel_request, &ctrl->ctrl);
  332. nvme_loop_destroy_admin_queue(ctrl);
  333. }
  334. static void nvme_loop_del_ctrl_work(struct work_struct *work)
  335. {
  336. struct nvme_loop_ctrl *ctrl = container_of(work,
  337. struct nvme_loop_ctrl, delete_work);
  338. nvme_uninit_ctrl(&ctrl->ctrl);
  339. nvme_loop_shutdown_ctrl(ctrl);
  340. nvme_put_ctrl(&ctrl->ctrl);
  341. }
  342. static int __nvme_loop_del_ctrl(struct nvme_loop_ctrl *ctrl)
  343. {
  344. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
  345. return -EBUSY;
  346. if (!schedule_work(&ctrl->delete_work))
  347. return -EBUSY;
  348. return 0;
  349. }
  350. static int nvme_loop_del_ctrl(struct nvme_ctrl *nctrl)
  351. {
  352. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
  353. int ret;
  354. ret = __nvme_loop_del_ctrl(ctrl);
  355. if (ret)
  356. return ret;
  357. flush_work(&ctrl->delete_work);
  358. return 0;
  359. }
  360. static void nvme_loop_delete_ctrl(struct nvmet_ctrl *nctrl)
  361. {
  362. struct nvme_loop_ctrl *ctrl;
  363. mutex_lock(&nvme_loop_ctrl_mutex);
  364. list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) {
  365. if (ctrl->ctrl.cntlid == nctrl->cntlid)
  366. __nvme_loop_del_ctrl(ctrl);
  367. }
  368. mutex_unlock(&nvme_loop_ctrl_mutex);
  369. }
  370. static void nvme_loop_reset_ctrl_work(struct work_struct *work)
  371. {
  372. struct nvme_loop_ctrl *ctrl = container_of(work,
  373. struct nvme_loop_ctrl, reset_work);
  374. bool changed;
  375. int i, ret;
  376. nvme_loop_shutdown_ctrl(ctrl);
  377. ret = nvme_loop_configure_admin_queue(ctrl);
  378. if (ret)
  379. goto out_disable;
  380. for (i = 1; i <= ctrl->ctrl.opts->nr_io_queues; i++) {
  381. ctrl->queues[i].ctrl = ctrl;
  382. ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq);
  383. if (ret)
  384. goto out_free_queues;
  385. ctrl->queue_count++;
  386. }
  387. for (i = 1; i <= ctrl->ctrl.opts->nr_io_queues; i++) {
  388. ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
  389. if (ret)
  390. goto out_free_queues;
  391. }
  392. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
  393. WARN_ON_ONCE(!changed);
  394. nvme_queue_scan(&ctrl->ctrl);
  395. nvme_queue_async_events(&ctrl->ctrl);
  396. nvme_start_queues(&ctrl->ctrl);
  397. return;
  398. out_free_queues:
  399. for (i = 1; i < ctrl->queue_count; i++)
  400. nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
  401. nvme_loop_destroy_admin_queue(ctrl);
  402. out_disable:
  403. dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
  404. nvme_uninit_ctrl(&ctrl->ctrl);
  405. nvme_put_ctrl(&ctrl->ctrl);
  406. }
  407. static int nvme_loop_reset_ctrl(struct nvme_ctrl *nctrl)
  408. {
  409. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
  410. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
  411. return -EBUSY;
  412. if (!schedule_work(&ctrl->reset_work))
  413. return -EBUSY;
  414. flush_work(&ctrl->reset_work);
  415. return 0;
  416. }
  417. static const struct nvme_ctrl_ops nvme_loop_ctrl_ops = {
  418. .name = "loop",
  419. .module = THIS_MODULE,
  420. .is_fabrics = true,
  421. .reg_read32 = nvmf_reg_read32,
  422. .reg_read64 = nvmf_reg_read64,
  423. .reg_write32 = nvmf_reg_write32,
  424. .reset_ctrl = nvme_loop_reset_ctrl,
  425. .free_ctrl = nvme_loop_free_ctrl,
  426. .submit_async_event = nvme_loop_submit_async_event,
  427. .delete_ctrl = nvme_loop_del_ctrl,
  428. .get_subsysnqn = nvmf_get_subsysnqn,
  429. };
  430. static int nvme_loop_create_io_queues(struct nvme_loop_ctrl *ctrl)
  431. {
  432. struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
  433. int ret, i;
  434. ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
  435. if (ret || !opts->nr_io_queues)
  436. return ret;
  437. dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n",
  438. opts->nr_io_queues);
  439. for (i = 1; i <= opts->nr_io_queues; i++) {
  440. ctrl->queues[i].ctrl = ctrl;
  441. ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq);
  442. if (ret)
  443. goto out_destroy_queues;
  444. ctrl->queue_count++;
  445. }
  446. memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
  447. ctrl->tag_set.ops = &nvme_loop_mq_ops;
  448. ctrl->tag_set.queue_depth = ctrl->ctrl.sqsize;
  449. ctrl->tag_set.reserved_tags = 1; /* fabric connect */
  450. ctrl->tag_set.numa_node = NUMA_NO_NODE;
  451. ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  452. ctrl->tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
  453. SG_CHUNK_SIZE * sizeof(struct scatterlist);
  454. ctrl->tag_set.driver_data = ctrl;
  455. ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
  456. ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
  457. ctrl->ctrl.tagset = &ctrl->tag_set;
  458. ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
  459. if (ret)
  460. goto out_destroy_queues;
  461. ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
  462. if (IS_ERR(ctrl->ctrl.connect_q)) {
  463. ret = PTR_ERR(ctrl->ctrl.connect_q);
  464. goto out_free_tagset;
  465. }
  466. for (i = 1; i <= opts->nr_io_queues; i++) {
  467. ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
  468. if (ret)
  469. goto out_cleanup_connect_q;
  470. }
  471. return 0;
  472. out_cleanup_connect_q:
  473. blk_cleanup_queue(ctrl->ctrl.connect_q);
  474. out_free_tagset:
  475. blk_mq_free_tag_set(&ctrl->tag_set);
  476. out_destroy_queues:
  477. for (i = 1; i < ctrl->queue_count; i++)
  478. nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
  479. return ret;
  480. }
  481. static struct nvme_ctrl *nvme_loop_create_ctrl(struct device *dev,
  482. struct nvmf_ctrl_options *opts)
  483. {
  484. struct nvme_loop_ctrl *ctrl;
  485. bool changed;
  486. int ret;
  487. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  488. if (!ctrl)
  489. return ERR_PTR(-ENOMEM);
  490. ctrl->ctrl.opts = opts;
  491. INIT_LIST_HEAD(&ctrl->list);
  492. INIT_WORK(&ctrl->delete_work, nvme_loop_del_ctrl_work);
  493. INIT_WORK(&ctrl->reset_work, nvme_loop_reset_ctrl_work);
  494. ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_loop_ctrl_ops,
  495. 0 /* no quirks, we're perfect! */);
  496. if (ret)
  497. goto out_put_ctrl;
  498. spin_lock_init(&ctrl->lock);
  499. ret = -ENOMEM;
  500. ctrl->ctrl.sqsize = opts->queue_size;
  501. ctrl->ctrl.kato = opts->kato;
  502. ctrl->queues = kcalloc(opts->nr_io_queues + 1, sizeof(*ctrl->queues),
  503. GFP_KERNEL);
  504. if (!ctrl->queues)
  505. goto out_uninit_ctrl;
  506. ret = nvme_loop_configure_admin_queue(ctrl);
  507. if (ret)
  508. goto out_free_queues;
  509. if (opts->queue_size > ctrl->ctrl.maxcmd) {
  510. /* warn if maxcmd is lower than queue_size */
  511. dev_warn(ctrl->ctrl.device,
  512. "queue_size %zu > ctrl maxcmd %u, clamping down\n",
  513. opts->queue_size, ctrl->ctrl.maxcmd);
  514. opts->queue_size = ctrl->ctrl.maxcmd;
  515. }
  516. if (opts->nr_io_queues) {
  517. ret = nvme_loop_create_io_queues(ctrl);
  518. if (ret)
  519. goto out_remove_admin_queue;
  520. }
  521. nvme_loop_init_iod(ctrl, &ctrl->async_event_iod, 0);
  522. dev_info(ctrl->ctrl.device,
  523. "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn);
  524. kref_get(&ctrl->ctrl.kref);
  525. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
  526. WARN_ON_ONCE(!changed);
  527. mutex_lock(&nvme_loop_ctrl_mutex);
  528. list_add_tail(&ctrl->list, &nvme_loop_ctrl_list);
  529. mutex_unlock(&nvme_loop_ctrl_mutex);
  530. if (opts->nr_io_queues) {
  531. nvme_queue_scan(&ctrl->ctrl);
  532. nvme_queue_async_events(&ctrl->ctrl);
  533. }
  534. return &ctrl->ctrl;
  535. out_remove_admin_queue:
  536. nvme_loop_destroy_admin_queue(ctrl);
  537. out_free_queues:
  538. kfree(ctrl->queues);
  539. out_uninit_ctrl:
  540. nvme_uninit_ctrl(&ctrl->ctrl);
  541. out_put_ctrl:
  542. nvme_put_ctrl(&ctrl->ctrl);
  543. if (ret > 0)
  544. ret = -EIO;
  545. return ERR_PTR(ret);
  546. }
  547. static int nvme_loop_add_port(struct nvmet_port *port)
  548. {
  549. /*
  550. * XXX: disalow adding more than one port so
  551. * there is no connection rejections when a
  552. * a subsystem is assigned to a port for which
  553. * loop doesn't have a pointer.
  554. * This scenario would be possible if we allowed
  555. * more than one port to be added and a subsystem
  556. * was assigned to a port other than nvmet_loop_port.
  557. */
  558. if (nvmet_loop_port)
  559. return -EPERM;
  560. nvmet_loop_port = port;
  561. return 0;
  562. }
  563. static void nvme_loop_remove_port(struct nvmet_port *port)
  564. {
  565. if (port == nvmet_loop_port)
  566. nvmet_loop_port = NULL;
  567. }
  568. static struct nvmet_fabrics_ops nvme_loop_ops = {
  569. .owner = THIS_MODULE,
  570. .type = NVMF_TRTYPE_LOOP,
  571. .add_port = nvme_loop_add_port,
  572. .remove_port = nvme_loop_remove_port,
  573. .queue_response = nvme_loop_queue_response,
  574. .delete_ctrl = nvme_loop_delete_ctrl,
  575. };
  576. static struct nvmf_transport_ops nvme_loop_transport = {
  577. .name = "loop",
  578. .create_ctrl = nvme_loop_create_ctrl,
  579. };
  580. static int __init nvme_loop_init_module(void)
  581. {
  582. int ret;
  583. ret = nvmet_register_transport(&nvme_loop_ops);
  584. if (ret)
  585. return ret;
  586. nvmf_register_transport(&nvme_loop_transport);
  587. return 0;
  588. }
  589. static void __exit nvme_loop_cleanup_module(void)
  590. {
  591. struct nvme_loop_ctrl *ctrl, *next;
  592. nvmf_unregister_transport(&nvme_loop_transport);
  593. nvmet_unregister_transport(&nvme_loop_ops);
  594. mutex_lock(&nvme_loop_ctrl_mutex);
  595. list_for_each_entry_safe(ctrl, next, &nvme_loop_ctrl_list, list)
  596. __nvme_loop_del_ctrl(ctrl);
  597. mutex_unlock(&nvme_loop_ctrl_mutex);
  598. flush_scheduled_work();
  599. }
  600. module_init(nvme_loop_init_module);
  601. module_exit(nvme_loop_cleanup_module);
  602. MODULE_LICENSE("GPL v2");
  603. MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */