loop.c 19 KB

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