loop.c 18 KB

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