loop.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVMe over Fabrics loopback device.
  4. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/scatterlist.h>
  8. #include <linux/blk-mq.h>
  9. #include <linux/nvme.h>
  10. #include <linux/module.h>
  11. #include <linux/parser.h>
  12. #include "nvmet.h"
  13. #include "../host/nvme.h"
  14. #include "../host/fabrics.h"
  15. #define NVME_LOOP_MAX_SEGMENTS 256
  16. struct nvme_loop_iod {
  17. struct nvme_request nvme_req;
  18. struct nvme_command cmd;
  19. struct nvme_completion cqe;
  20. struct nvmet_req req;
  21. struct nvme_loop_queue *queue;
  22. struct work_struct work;
  23. struct sg_table sg_table;
  24. struct scatterlist first_sgl[];
  25. };
  26. struct nvme_loop_ctrl {
  27. struct nvme_loop_queue *queues;
  28. struct blk_mq_tag_set admin_tag_set;
  29. struct list_head list;
  30. struct blk_mq_tag_set tag_set;
  31. struct nvme_ctrl ctrl;
  32. struct nvmet_port *port;
  33. /* Must be last --ends in a flexible-array member. */
  34. struct nvme_loop_iod async_event_iod;
  35. };
  36. static inline struct nvme_loop_ctrl *to_loop_ctrl(struct nvme_ctrl *ctrl)
  37. {
  38. return container_of(ctrl, struct nvme_loop_ctrl, ctrl);
  39. }
  40. enum nvme_loop_queue_flags {
  41. NVME_LOOP_Q_LIVE = 0,
  42. };
  43. struct nvme_loop_queue {
  44. struct nvmet_cq nvme_cq;
  45. struct nvmet_sq nvme_sq;
  46. struct nvme_loop_ctrl *ctrl;
  47. unsigned long flags;
  48. };
  49. static LIST_HEAD(nvme_loop_ports);
  50. static DEFINE_MUTEX(nvme_loop_ports_mutex);
  51. static LIST_HEAD(nvme_loop_ctrl_list);
  52. static DEFINE_MUTEX(nvme_loop_ctrl_mutex);
  53. static void nvme_loop_queue_response(struct nvmet_req *nvme_req);
  54. static void nvme_loop_delete_ctrl(struct nvmet_ctrl *ctrl);
  55. static const struct nvmet_fabrics_ops nvme_loop_ops;
  56. static inline int nvme_loop_queue_idx(struct nvme_loop_queue *queue)
  57. {
  58. return queue - queue->ctrl->queues;
  59. }
  60. static void nvme_loop_complete_rq(struct request *req)
  61. {
  62. struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
  63. sg_free_table_chained(&iod->sg_table, NVME_INLINE_SG_CNT);
  64. nvme_complete_rq(req);
  65. }
  66. static struct blk_mq_tags *nvme_loop_tagset(struct nvme_loop_queue *queue)
  67. {
  68. u32 queue_idx = nvme_loop_queue_idx(queue);
  69. if (queue_idx == 0)
  70. return queue->ctrl->admin_tag_set.tags[queue_idx];
  71. return queue->ctrl->tag_set.tags[queue_idx - 1];
  72. }
  73. static void nvme_loop_queue_response(struct nvmet_req *req)
  74. {
  75. struct nvme_loop_queue *queue =
  76. container_of(req->sq, struct nvme_loop_queue, nvme_sq);
  77. struct nvme_completion *cqe = req->cqe;
  78. /*
  79. * AEN requests are special as they don't time out and can
  80. * survive any kind of queue freeze and often don't respond to
  81. * aborts. We don't even bother to allocate a struct request
  82. * for them but rather special case them here.
  83. */
  84. if (unlikely(nvme_is_aen_req(nvme_loop_queue_idx(queue),
  85. cqe->command_id))) {
  86. nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
  87. &cqe->result);
  88. } else {
  89. struct request *rq;
  90. rq = nvme_find_rq(nvme_loop_tagset(queue), cqe->command_id);
  91. if (!rq) {
  92. dev_err(queue->ctrl->ctrl.device,
  93. "got bad command_id %#x on queue %d\n",
  94. cqe->command_id, nvme_loop_queue_idx(queue));
  95. return;
  96. }
  97. if (!nvme_try_complete_req(rq, cqe->status, cqe->result))
  98. nvme_loop_complete_rq(rq);
  99. }
  100. }
  101. static void nvme_loop_execute_work(struct work_struct *work)
  102. {
  103. struct nvme_loop_iod *iod =
  104. container_of(work, struct nvme_loop_iod, work);
  105. iod->req.execute(&iod->req);
  106. }
  107. static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx,
  108. const struct blk_mq_queue_data *bd)
  109. {
  110. struct nvme_ns *ns = hctx->queue->queuedata;
  111. struct nvme_loop_queue *queue = hctx->driver_data;
  112. struct request *req = bd->rq;
  113. struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
  114. bool queue_ready = test_bit(NVME_LOOP_Q_LIVE, &queue->flags);
  115. blk_status_t ret;
  116. if (!nvme_check_ready(&queue->ctrl->ctrl, req, queue_ready))
  117. return nvme_fail_nonready_command(&queue->ctrl->ctrl, req);
  118. ret = nvme_setup_cmd(ns, req);
  119. if (ret)
  120. return ret;
  121. nvme_start_request(req);
  122. iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
  123. iod->req.port = queue->ctrl->port;
  124. if (!nvmet_req_init(&iod->req, &queue->nvme_sq, &nvme_loop_ops))
  125. return BLK_STS_OK;
  126. if (blk_rq_nr_phys_segments(req)) {
  127. iod->sg_table.sgl = iod->first_sgl;
  128. if (sg_alloc_table_chained(&iod->sg_table,
  129. blk_rq_nr_phys_segments(req),
  130. iod->sg_table.sgl, NVME_INLINE_SG_CNT)) {
  131. nvme_cleanup_cmd(req);
  132. return BLK_STS_RESOURCE;
  133. }
  134. iod->req.sg = iod->sg_table.sgl;
  135. iod->req.sg_cnt = blk_rq_map_sg(req, iod->sg_table.sgl);
  136. iod->req.transfer_len = blk_rq_payload_bytes(req);
  137. }
  138. queue_work(nvmet_wq, &iod->work);
  139. return BLK_STS_OK;
  140. }
  141. static void nvme_loop_submit_async_event(struct nvme_ctrl *arg)
  142. {
  143. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(arg);
  144. struct nvme_loop_queue *queue = &ctrl->queues[0];
  145. struct nvme_loop_iod *iod = &ctrl->async_event_iod;
  146. memset(&iod->cmd, 0, sizeof(iod->cmd));
  147. iod->cmd.common.opcode = nvme_admin_async_event;
  148. iod->cmd.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
  149. iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
  150. if (!nvmet_req_init(&iod->req, &queue->nvme_sq, &nvme_loop_ops)) {
  151. dev_err(ctrl->ctrl.device, "failed async event work\n");
  152. return;
  153. }
  154. queue_work(nvmet_wq, &iod->work);
  155. }
  156. static int nvme_loop_init_iod(struct nvme_loop_ctrl *ctrl,
  157. struct nvme_loop_iod *iod, unsigned int queue_idx)
  158. {
  159. iod->req.cmd = &iod->cmd;
  160. iod->req.cqe = &iod->cqe;
  161. iod->queue = &ctrl->queues[queue_idx];
  162. INIT_WORK(&iod->work, nvme_loop_execute_work);
  163. return 0;
  164. }
  165. static int nvme_loop_init_request(struct blk_mq_tag_set *set,
  166. struct request *req, unsigned int hctx_idx,
  167. unsigned int numa_node)
  168. {
  169. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(set->driver_data);
  170. struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
  171. nvme_req(req)->ctrl = &ctrl->ctrl;
  172. nvme_req(req)->cmd = &iod->cmd;
  173. return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req),
  174. (set == &ctrl->tag_set) ? hctx_idx + 1 : 0);
  175. }
  176. static struct lock_class_key loop_hctx_fq_lock_key;
  177. static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  178. unsigned int hctx_idx)
  179. {
  180. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(data);
  181. struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1];
  182. BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
  183. /*
  184. * flush_end_io() can be called recursively for us, so use our own
  185. * lock class key for avoiding lockdep possible recursive locking,
  186. * then we can remove the dynamically allocated lock class for each
  187. * flush queue, that way may cause horrible boot delay.
  188. */
  189. blk_mq_hctx_set_fq_lock_class(hctx, &loop_hctx_fq_lock_key);
  190. hctx->driver_data = queue;
  191. return 0;
  192. }
  193. static int nvme_loop_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  194. unsigned int hctx_idx)
  195. {
  196. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(data);
  197. struct nvme_loop_queue *queue = &ctrl->queues[0];
  198. BUG_ON(hctx_idx != 0);
  199. hctx->driver_data = queue;
  200. return 0;
  201. }
  202. static const struct blk_mq_ops nvme_loop_mq_ops = {
  203. .queue_rq = nvme_loop_queue_rq,
  204. .complete = nvme_loop_complete_rq,
  205. .init_request = nvme_loop_init_request,
  206. .init_hctx = nvme_loop_init_hctx,
  207. };
  208. static const struct blk_mq_ops nvme_loop_admin_mq_ops = {
  209. .queue_rq = nvme_loop_queue_rq,
  210. .complete = nvme_loop_complete_rq,
  211. .init_request = nvme_loop_init_request,
  212. .init_hctx = nvme_loop_init_admin_hctx,
  213. };
  214. static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl)
  215. {
  216. if (!test_and_clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags))
  217. return;
  218. /*
  219. * It's possible that some requests might have been added
  220. * after admin queue is stopped/quiesced. So now start the
  221. * queue to flush these requests to the completion.
  222. */
  223. nvme_unquiesce_admin_queue(&ctrl->ctrl);
  224. nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
  225. nvmet_cq_put(&ctrl->queues[0].nvme_cq);
  226. nvme_remove_admin_tag_set(&ctrl->ctrl);
  227. }
  228. static void nvme_loop_free_ctrl(struct nvme_ctrl *nctrl)
  229. {
  230. struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
  231. if (list_empty(&ctrl->list))
  232. goto free_ctrl;
  233. mutex_lock(&nvme_loop_ctrl_mutex);
  234. list_del(&ctrl->list);
  235. mutex_unlock(&nvme_loop_ctrl_mutex);
  236. if (nctrl->tagset)
  237. nvme_remove_io_tag_set(nctrl);
  238. kfree(ctrl->queues);
  239. nvmf_free_options(nctrl->opts);
  240. free_ctrl:
  241. kfree(ctrl);
  242. }
  243. static void nvme_loop_destroy_io_queues(struct nvme_loop_ctrl *ctrl)
  244. {
  245. int i;
  246. for (i = 1; i < ctrl->ctrl.queue_count; i++) {
  247. clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
  248. nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
  249. nvmet_cq_put(&ctrl->queues[i].nvme_cq);
  250. }
  251. ctrl->ctrl.queue_count = 1;
  252. /*
  253. * It's possible that some requests might have been added
  254. * after io queue is stopped/quiesced. So now start the
  255. * queue to flush these requests to the completion.
  256. */
  257. nvme_unquiesce_io_queues(&ctrl->ctrl);
  258. }
  259. static int nvme_loop_init_io_queues(struct nvme_loop_ctrl *ctrl)
  260. {
  261. struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
  262. unsigned int nr_io_queues;
  263. int ret, i;
  264. nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
  265. ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
  266. if (ret || !nr_io_queues)
  267. return ret;
  268. dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", nr_io_queues);
  269. for (i = 1; i <= nr_io_queues; i++) {
  270. ctrl->queues[i].ctrl = ctrl;
  271. nvmet_cq_init(&ctrl->queues[i].nvme_cq);
  272. ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq,
  273. &ctrl->queues[i].nvme_cq);
  274. if (ret) {
  275. nvmet_cq_put(&ctrl->queues[i].nvme_cq);
  276. goto out_destroy_queues;
  277. }
  278. ctrl->ctrl.queue_count++;
  279. }
  280. return 0;
  281. out_destroy_queues:
  282. nvme_loop_destroy_io_queues(ctrl);
  283. return ret;
  284. }
  285. static int nvme_loop_connect_io_queues(struct nvme_loop_ctrl *ctrl)
  286. {
  287. int i, ret;
  288. for (i = 1; i < ctrl->ctrl.queue_count; i++) {
  289. ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
  290. if (ret)
  291. return ret;
  292. set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
  293. }
  294. return 0;
  295. }
  296. static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl)
  297. {
  298. int error;
  299. ctrl->queues[0].ctrl = ctrl;
  300. nvmet_cq_init(&ctrl->queues[0].nvme_cq);
  301. error = nvmet_sq_init(&ctrl->queues[0].nvme_sq,
  302. &ctrl->queues[0].nvme_cq);
  303. if (error) {
  304. nvmet_cq_put(&ctrl->queues[0].nvme_cq);
  305. return error;
  306. }
  307. ctrl->ctrl.queue_count = 1;
  308. error = nvme_alloc_admin_tag_set(&ctrl->ctrl, &ctrl->admin_tag_set,
  309. &nvme_loop_admin_mq_ops,
  310. sizeof(struct nvme_loop_iod) +
  311. NVME_INLINE_SG_CNT * sizeof(struct scatterlist));
  312. if (error)
  313. goto out_free_sq;
  314. /* reset stopped state for the fresh admin queue */
  315. clear_bit(NVME_CTRL_ADMIN_Q_STOPPED, &ctrl->ctrl.flags);
  316. error = nvmf_connect_admin_queue(&ctrl->ctrl);
  317. if (error)
  318. goto out_cleanup_tagset;
  319. set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
  320. error = nvme_enable_ctrl(&ctrl->ctrl);
  321. if (error)
  322. goto out_cleanup_tagset;
  323. ctrl->ctrl.max_hw_sectors =
  324. (NVME_LOOP_MAX_SEGMENTS - 1) << PAGE_SECTORS_SHIFT;
  325. nvme_unquiesce_admin_queue(&ctrl->ctrl);
  326. error = nvme_init_ctrl_finish(&ctrl->ctrl, false);
  327. if (error)
  328. goto out_cleanup_tagset;
  329. return 0;
  330. out_cleanup_tagset:
  331. clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
  332. nvme_remove_admin_tag_set(&ctrl->ctrl);
  333. out_free_sq:
  334. nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
  335. nvmet_cq_put(&ctrl->queues[0].nvme_cq);
  336. return error;
  337. }
  338. static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
  339. {
  340. if (ctrl->ctrl.queue_count > 1) {
  341. nvme_quiesce_io_queues(&ctrl->ctrl);
  342. nvme_cancel_tagset(&ctrl->ctrl);
  343. nvme_loop_destroy_io_queues(ctrl);
  344. }
  345. nvme_quiesce_admin_queue(&ctrl->ctrl);
  346. if (nvme_ctrl_state(&ctrl->ctrl) == NVME_CTRL_LIVE)
  347. nvme_disable_ctrl(&ctrl->ctrl, true);
  348. nvme_cancel_admin_tagset(&ctrl->ctrl);
  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. int ret;
  370. nvme_stop_ctrl(&ctrl->ctrl);
  371. nvme_loop_shutdown_ctrl(ctrl);
  372. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
  373. enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl);
  374. if (state != NVME_CTRL_DELETING &&
  375. state != NVME_CTRL_DELETING_NOIO)
  376. /* state change failure for non-deleted ctrl? */
  377. WARN_ON_ONCE(1);
  378. return;
  379. }
  380. ret = nvme_loop_configure_admin_queue(ctrl);
  381. if (ret)
  382. goto out_disable;
  383. ret = nvme_loop_init_io_queues(ctrl);
  384. if (ret)
  385. goto out_destroy_admin;
  386. ret = nvme_loop_connect_io_queues(ctrl);
  387. if (ret)
  388. goto out_destroy_io;
  389. blk_mq_update_nr_hw_queues(&ctrl->tag_set,
  390. ctrl->ctrl.queue_count - 1);
  391. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE))
  392. WARN_ON_ONCE(1);
  393. nvme_start_ctrl(&ctrl->ctrl);
  394. return;
  395. out_destroy_io:
  396. nvme_loop_destroy_io_queues(ctrl);
  397. out_destroy_admin:
  398. nvme_quiesce_admin_queue(&ctrl->ctrl);
  399. nvme_cancel_admin_tagset(&ctrl->ctrl);
  400. nvme_loop_destroy_admin_queue(ctrl);
  401. out_disable:
  402. dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
  403. nvme_uninit_ctrl(&ctrl->ctrl);
  404. }
  405. static const struct nvme_ctrl_ops nvme_loop_ctrl_ops = {
  406. .name = "loop",
  407. .module = THIS_MODULE,
  408. .flags = NVME_F_FABRICS,
  409. .reg_read32 = nvmf_reg_read32,
  410. .reg_read64 = nvmf_reg_read64,
  411. .reg_write32 = nvmf_reg_write32,
  412. .free_ctrl = nvme_loop_free_ctrl,
  413. .submit_async_event = nvme_loop_submit_async_event,
  414. .delete_ctrl = nvme_loop_delete_ctrl_host,
  415. .get_address = nvmf_get_address,
  416. .get_virt_boundary = nvme_get_virt_boundary,
  417. };
  418. static int nvme_loop_create_io_queues(struct nvme_loop_ctrl *ctrl)
  419. {
  420. int ret;
  421. ret = nvme_loop_init_io_queues(ctrl);
  422. if (ret)
  423. return ret;
  424. ret = nvme_alloc_io_tag_set(&ctrl->ctrl, &ctrl->tag_set,
  425. &nvme_loop_mq_ops, 1,
  426. sizeof(struct nvme_loop_iod) +
  427. NVME_INLINE_SG_CNT * sizeof(struct scatterlist));
  428. if (ret)
  429. goto out_destroy_queues;
  430. ret = nvme_loop_connect_io_queues(ctrl);
  431. if (ret)
  432. goto out_cleanup_tagset;
  433. return 0;
  434. out_cleanup_tagset:
  435. nvme_remove_io_tag_set(&ctrl->ctrl);
  436. out_destroy_queues:
  437. nvme_loop_destroy_io_queues(ctrl);
  438. return ret;
  439. }
  440. static struct nvmet_port *nvme_loop_find_port(struct nvme_ctrl *ctrl)
  441. {
  442. struct nvmet_port *p, *found = NULL;
  443. mutex_lock(&nvme_loop_ports_mutex);
  444. list_for_each_entry(p, &nvme_loop_ports, entry) {
  445. /* if no transport address is specified use the first port */
  446. if ((ctrl->opts->mask & NVMF_OPT_TRADDR) &&
  447. strcmp(ctrl->opts->traddr, p->disc_addr.traddr))
  448. continue;
  449. found = p;
  450. break;
  451. }
  452. mutex_unlock(&nvme_loop_ports_mutex);
  453. return found;
  454. }
  455. static struct nvme_ctrl *nvme_loop_create_ctrl(struct device *dev,
  456. struct nvmf_ctrl_options *opts)
  457. {
  458. struct nvme_loop_ctrl *ctrl;
  459. int ret;
  460. ctrl = kzalloc_obj(*ctrl);
  461. if (!ctrl)
  462. return ERR_PTR(-ENOMEM);
  463. ctrl->ctrl.opts = opts;
  464. INIT_LIST_HEAD(&ctrl->list);
  465. INIT_WORK(&ctrl->ctrl.reset_work, nvme_loop_reset_ctrl_work);
  466. ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_loop_ctrl_ops,
  467. 0 /* no quirks, we're perfect! */);
  468. if (ret) {
  469. kfree(ctrl);
  470. goto out;
  471. }
  472. ret = nvme_add_ctrl(&ctrl->ctrl);
  473. if (ret)
  474. goto out_put_ctrl;
  475. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
  476. WARN_ON_ONCE(1);
  477. ret = -ENOMEM;
  478. ctrl->ctrl.kato = opts->kato;
  479. ctrl->port = nvme_loop_find_port(&ctrl->ctrl);
  480. ctrl->queues = kzalloc_objs(*ctrl->queues, opts->nr_io_queues + 1);
  481. if (!ctrl->queues)
  482. goto out_uninit_ctrl;
  483. ret = nvme_loop_configure_admin_queue(ctrl);
  484. if (ret)
  485. goto out_free_queues;
  486. if (opts->queue_size > ctrl->ctrl.maxcmd) {
  487. /* warn if maxcmd is lower than queue_size */
  488. dev_warn(ctrl->ctrl.device,
  489. "queue_size %zu > ctrl maxcmd %u, clamping down\n",
  490. opts->queue_size, ctrl->ctrl.maxcmd);
  491. opts->queue_size = ctrl->ctrl.maxcmd;
  492. }
  493. ctrl->ctrl.sqsize = opts->queue_size - 1;
  494. if (opts->nr_io_queues) {
  495. ret = nvme_loop_create_io_queues(ctrl);
  496. if (ret)
  497. goto out_remove_admin_queue;
  498. }
  499. nvme_loop_init_iod(ctrl, &ctrl->async_event_iod, 0);
  500. dev_info(ctrl->ctrl.device,
  501. "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn);
  502. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE))
  503. WARN_ON_ONCE(1);
  504. mutex_lock(&nvme_loop_ctrl_mutex);
  505. list_add_tail(&ctrl->list, &nvme_loop_ctrl_list);
  506. mutex_unlock(&nvme_loop_ctrl_mutex);
  507. nvme_start_ctrl(&ctrl->ctrl);
  508. return &ctrl->ctrl;
  509. out_remove_admin_queue:
  510. nvme_quiesce_admin_queue(&ctrl->ctrl);
  511. nvme_cancel_admin_tagset(&ctrl->ctrl);
  512. nvme_loop_destroy_admin_queue(ctrl);
  513. out_free_queues:
  514. kfree(ctrl->queues);
  515. out_uninit_ctrl:
  516. nvme_uninit_ctrl(&ctrl->ctrl);
  517. out_put_ctrl:
  518. nvme_put_ctrl(&ctrl->ctrl);
  519. out:
  520. if (ret > 0)
  521. ret = -EIO;
  522. return ERR_PTR(ret);
  523. }
  524. static int nvme_loop_add_port(struct nvmet_port *port)
  525. {
  526. mutex_lock(&nvme_loop_ports_mutex);
  527. list_add_tail(&port->entry, &nvme_loop_ports);
  528. mutex_unlock(&nvme_loop_ports_mutex);
  529. return 0;
  530. }
  531. static void nvme_loop_remove_port(struct nvmet_port *port)
  532. {
  533. mutex_lock(&nvme_loop_ports_mutex);
  534. list_del_init(&port->entry);
  535. mutex_unlock(&nvme_loop_ports_mutex);
  536. /*
  537. * Ensure any ctrls that are in the process of being
  538. * deleted are in fact deleted before we return
  539. * and free the port. This is to prevent active
  540. * ctrls from using a port after it's freed.
  541. */
  542. flush_workqueue(nvme_delete_wq);
  543. }
  544. static const struct nvmet_fabrics_ops nvme_loop_ops = {
  545. .owner = THIS_MODULE,
  546. .type = NVMF_TRTYPE_LOOP,
  547. .add_port = nvme_loop_add_port,
  548. .remove_port = nvme_loop_remove_port,
  549. .queue_response = nvme_loop_queue_response,
  550. .delete_ctrl = nvme_loop_delete_ctrl,
  551. };
  552. static struct nvmf_transport_ops nvme_loop_transport = {
  553. .name = "loop",
  554. .module = THIS_MODULE,
  555. .create_ctrl = nvme_loop_create_ctrl,
  556. .allowed_opts = NVMF_OPT_TRADDR,
  557. };
  558. static int __init nvme_loop_init_module(void)
  559. {
  560. int ret;
  561. ret = nvmet_register_transport(&nvme_loop_ops);
  562. if (ret)
  563. return ret;
  564. ret = nvmf_register_transport(&nvme_loop_transport);
  565. if (ret)
  566. nvmet_unregister_transport(&nvme_loop_ops);
  567. return ret;
  568. }
  569. static void __exit nvme_loop_cleanup_module(void)
  570. {
  571. struct nvme_loop_ctrl *ctrl, *next;
  572. nvmf_unregister_transport(&nvme_loop_transport);
  573. nvmet_unregister_transport(&nvme_loop_ops);
  574. mutex_lock(&nvme_loop_ctrl_mutex);
  575. list_for_each_entry_safe(ctrl, next, &nvme_loop_ctrl_list, list)
  576. nvme_delete_ctrl(&ctrl->ctrl);
  577. mutex_unlock(&nvme_loop_ctrl_mutex);
  578. flush_workqueue(nvme_delete_wq);
  579. }
  580. module_init(nvme_loop_init_module);
  581. module_exit(nvme_loop_cleanup_module);
  582. MODULE_DESCRIPTION("NVMe target loop transport driver");
  583. MODULE_LICENSE("GPL v2");
  584. MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */