rdma.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. /*
  2. * NVMe over Fabrics RDMA host code.
  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/module.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <rdma/mr_pool.h>
  19. #include <linux/err.h>
  20. #include <linux/string.h>
  21. #include <linux/atomic.h>
  22. #include <linux/blk-mq.h>
  23. #include <linux/blk-mq-rdma.h>
  24. #include <linux/types.h>
  25. #include <linux/list.h>
  26. #include <linux/mutex.h>
  27. #include <linux/scatterlist.h>
  28. #include <linux/nvme.h>
  29. #include <asm/unaligned.h>
  30. #include <rdma/ib_verbs.h>
  31. #include <rdma/rdma_cm.h>
  32. #include <linux/nvme-rdma.h>
  33. #include "nvme.h"
  34. #include "fabrics.h"
  35. #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
  36. #define NVME_RDMA_MAX_SEGMENTS 256
  37. #define NVME_RDMA_MAX_INLINE_SEGMENTS 1
  38. struct nvme_rdma_device {
  39. struct ib_device *dev;
  40. struct ib_pd *pd;
  41. struct kref ref;
  42. struct list_head entry;
  43. };
  44. struct nvme_rdma_qe {
  45. struct ib_cqe cqe;
  46. void *data;
  47. u64 dma;
  48. };
  49. struct nvme_rdma_queue;
  50. struct nvme_rdma_request {
  51. struct nvme_request req;
  52. struct ib_mr *mr;
  53. struct nvme_rdma_qe sqe;
  54. union nvme_result result;
  55. __le16 status;
  56. refcount_t ref;
  57. struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
  58. u32 num_sge;
  59. int nents;
  60. struct ib_reg_wr reg_wr;
  61. struct ib_cqe reg_cqe;
  62. struct nvme_rdma_queue *queue;
  63. struct sg_table sg_table;
  64. struct scatterlist first_sgl[];
  65. };
  66. enum nvme_rdma_queue_flags {
  67. NVME_RDMA_Q_ALLOCATED = 0,
  68. NVME_RDMA_Q_LIVE = 1,
  69. NVME_RDMA_Q_TR_READY = 2,
  70. };
  71. struct nvme_rdma_queue {
  72. struct nvme_rdma_qe *rsp_ring;
  73. int queue_size;
  74. size_t cmnd_capsule_len;
  75. struct nvme_rdma_ctrl *ctrl;
  76. struct nvme_rdma_device *device;
  77. struct ib_cq *ib_cq;
  78. struct ib_qp *qp;
  79. unsigned long flags;
  80. struct rdma_cm_id *cm_id;
  81. int cm_error;
  82. struct completion cm_done;
  83. };
  84. struct nvme_rdma_ctrl {
  85. /* read only in the hot path */
  86. struct nvme_rdma_queue *queues;
  87. /* other member variables */
  88. struct blk_mq_tag_set tag_set;
  89. struct work_struct err_work;
  90. struct nvme_rdma_qe async_event_sqe;
  91. struct delayed_work reconnect_work;
  92. struct list_head list;
  93. struct blk_mq_tag_set admin_tag_set;
  94. struct nvme_rdma_device *device;
  95. u32 max_fr_pages;
  96. struct sockaddr_storage addr;
  97. struct sockaddr_storage src_addr;
  98. struct nvme_ctrl ctrl;
  99. };
  100. static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
  101. {
  102. return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
  103. }
  104. static LIST_HEAD(device_list);
  105. static DEFINE_MUTEX(device_list_mutex);
  106. static LIST_HEAD(nvme_rdma_ctrl_list);
  107. static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
  108. /*
  109. * Disabling this option makes small I/O goes faster, but is fundamentally
  110. * unsafe. With it turned off we will have to register a global rkey that
  111. * allows read and write access to all physical memory.
  112. */
  113. static bool register_always = true;
  114. module_param(register_always, bool, 0444);
  115. MODULE_PARM_DESC(register_always,
  116. "Use memory registration even for contiguous memory regions");
  117. static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
  118. struct rdma_cm_event *event);
  119. static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
  120. static const struct blk_mq_ops nvme_rdma_mq_ops;
  121. static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
  122. /* XXX: really should move to a generic header sooner or later.. */
  123. static inline void put_unaligned_le24(u32 val, u8 *p)
  124. {
  125. *p++ = val;
  126. *p++ = val >> 8;
  127. *p++ = val >> 16;
  128. }
  129. static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
  130. {
  131. return queue - queue->ctrl->queues;
  132. }
  133. static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
  134. {
  135. return queue->cmnd_capsule_len - sizeof(struct nvme_command);
  136. }
  137. static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
  138. size_t capsule_size, enum dma_data_direction dir)
  139. {
  140. ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
  141. kfree(qe->data);
  142. }
  143. static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
  144. size_t capsule_size, enum dma_data_direction dir)
  145. {
  146. qe->data = kzalloc(capsule_size, GFP_KERNEL);
  147. if (!qe->data)
  148. return -ENOMEM;
  149. qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
  150. if (ib_dma_mapping_error(ibdev, qe->dma)) {
  151. kfree(qe->data);
  152. return -ENOMEM;
  153. }
  154. return 0;
  155. }
  156. static void nvme_rdma_free_ring(struct ib_device *ibdev,
  157. struct nvme_rdma_qe *ring, size_t ib_queue_size,
  158. size_t capsule_size, enum dma_data_direction dir)
  159. {
  160. int i;
  161. for (i = 0; i < ib_queue_size; i++)
  162. nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
  163. kfree(ring);
  164. }
  165. static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
  166. size_t ib_queue_size, size_t capsule_size,
  167. enum dma_data_direction dir)
  168. {
  169. struct nvme_rdma_qe *ring;
  170. int i;
  171. ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
  172. if (!ring)
  173. return NULL;
  174. for (i = 0; i < ib_queue_size; i++) {
  175. if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
  176. goto out_free_ring;
  177. }
  178. return ring;
  179. out_free_ring:
  180. nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
  181. return NULL;
  182. }
  183. static void nvme_rdma_qp_event(struct ib_event *event, void *context)
  184. {
  185. pr_debug("QP event %s (%d)\n",
  186. ib_event_msg(event->event), event->event);
  187. }
  188. static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
  189. {
  190. wait_for_completion_interruptible_timeout(&queue->cm_done,
  191. msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
  192. return queue->cm_error;
  193. }
  194. static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
  195. {
  196. struct nvme_rdma_device *dev = queue->device;
  197. struct ib_qp_init_attr init_attr;
  198. int ret;
  199. memset(&init_attr, 0, sizeof(init_attr));
  200. init_attr.event_handler = nvme_rdma_qp_event;
  201. /* +1 for drain */
  202. init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
  203. /* +1 for drain */
  204. init_attr.cap.max_recv_wr = queue->queue_size + 1;
  205. init_attr.cap.max_recv_sge = 1;
  206. init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
  207. init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  208. init_attr.qp_type = IB_QPT_RC;
  209. init_attr.send_cq = queue->ib_cq;
  210. init_attr.recv_cq = queue->ib_cq;
  211. ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
  212. queue->qp = queue->cm_id->qp;
  213. return ret;
  214. }
  215. static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
  216. struct request *rq, unsigned int hctx_idx)
  217. {
  218. struct nvme_rdma_ctrl *ctrl = set->driver_data;
  219. struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
  220. int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
  221. struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
  222. struct nvme_rdma_device *dev = queue->device;
  223. nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
  224. DMA_TO_DEVICE);
  225. }
  226. static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
  227. struct request *rq, unsigned int hctx_idx,
  228. unsigned int numa_node)
  229. {
  230. struct nvme_rdma_ctrl *ctrl = set->driver_data;
  231. struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
  232. int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
  233. struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
  234. struct nvme_rdma_device *dev = queue->device;
  235. struct ib_device *ibdev = dev->dev;
  236. int ret;
  237. ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
  238. DMA_TO_DEVICE);
  239. if (ret)
  240. return ret;
  241. req->queue = queue;
  242. return 0;
  243. }
  244. static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  245. unsigned int hctx_idx)
  246. {
  247. struct nvme_rdma_ctrl *ctrl = data;
  248. struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
  249. BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
  250. hctx->driver_data = queue;
  251. return 0;
  252. }
  253. static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  254. unsigned int hctx_idx)
  255. {
  256. struct nvme_rdma_ctrl *ctrl = data;
  257. struct nvme_rdma_queue *queue = &ctrl->queues[0];
  258. BUG_ON(hctx_idx != 0);
  259. hctx->driver_data = queue;
  260. return 0;
  261. }
  262. static void nvme_rdma_free_dev(struct kref *ref)
  263. {
  264. struct nvme_rdma_device *ndev =
  265. container_of(ref, struct nvme_rdma_device, ref);
  266. mutex_lock(&device_list_mutex);
  267. list_del(&ndev->entry);
  268. mutex_unlock(&device_list_mutex);
  269. ib_dealloc_pd(ndev->pd);
  270. kfree(ndev);
  271. }
  272. static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
  273. {
  274. kref_put(&dev->ref, nvme_rdma_free_dev);
  275. }
  276. static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
  277. {
  278. return kref_get_unless_zero(&dev->ref);
  279. }
  280. static struct nvme_rdma_device *
  281. nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
  282. {
  283. struct nvme_rdma_device *ndev;
  284. mutex_lock(&device_list_mutex);
  285. list_for_each_entry(ndev, &device_list, entry) {
  286. if (ndev->dev->node_guid == cm_id->device->node_guid &&
  287. nvme_rdma_dev_get(ndev))
  288. goto out_unlock;
  289. }
  290. ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
  291. if (!ndev)
  292. goto out_err;
  293. ndev->dev = cm_id->device;
  294. kref_init(&ndev->ref);
  295. ndev->pd = ib_alloc_pd(ndev->dev,
  296. register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
  297. if (IS_ERR(ndev->pd))
  298. goto out_free_dev;
  299. if (!(ndev->dev->attrs.device_cap_flags &
  300. IB_DEVICE_MEM_MGT_EXTENSIONS)) {
  301. dev_err(&ndev->dev->dev,
  302. "Memory registrations not supported.\n");
  303. goto out_free_pd;
  304. }
  305. list_add(&ndev->entry, &device_list);
  306. out_unlock:
  307. mutex_unlock(&device_list_mutex);
  308. return ndev;
  309. out_free_pd:
  310. ib_dealloc_pd(ndev->pd);
  311. out_free_dev:
  312. kfree(ndev);
  313. out_err:
  314. mutex_unlock(&device_list_mutex);
  315. return NULL;
  316. }
  317. static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
  318. {
  319. struct nvme_rdma_device *dev;
  320. struct ib_device *ibdev;
  321. if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
  322. return;
  323. dev = queue->device;
  324. ibdev = dev->dev;
  325. ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
  326. /*
  327. * The cm_id object might have been destroyed during RDMA connection
  328. * establishment error flow to avoid getting other cma events, thus
  329. * the destruction of the QP shouldn't use rdma_cm API.
  330. */
  331. ib_destroy_qp(queue->qp);
  332. ib_free_cq(queue->ib_cq);
  333. nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
  334. sizeof(struct nvme_completion), DMA_FROM_DEVICE);
  335. nvme_rdma_dev_put(dev);
  336. }
  337. static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
  338. {
  339. return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
  340. ibdev->attrs.max_fast_reg_page_list_len);
  341. }
  342. static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
  343. {
  344. struct ib_device *ibdev;
  345. const int send_wr_factor = 3; /* MR, SEND, INV */
  346. const int cq_factor = send_wr_factor + 1; /* + RECV */
  347. int comp_vector, idx = nvme_rdma_queue_idx(queue);
  348. int ret;
  349. queue->device = nvme_rdma_find_get_device(queue->cm_id);
  350. if (!queue->device) {
  351. dev_err(queue->cm_id->device->dev.parent,
  352. "no client data found!\n");
  353. return -ECONNREFUSED;
  354. }
  355. ibdev = queue->device->dev;
  356. /*
  357. * Spread I/O queues completion vectors according their queue index.
  358. * Admin queues can always go on completion vector 0.
  359. */
  360. comp_vector = idx == 0 ? idx : idx - 1;
  361. /* +1 for ib_stop_cq */
  362. queue->ib_cq = ib_alloc_cq(ibdev, queue,
  363. cq_factor * queue->queue_size + 1,
  364. comp_vector, IB_POLL_SOFTIRQ);
  365. if (IS_ERR(queue->ib_cq)) {
  366. ret = PTR_ERR(queue->ib_cq);
  367. goto out_put_dev;
  368. }
  369. ret = nvme_rdma_create_qp(queue, send_wr_factor);
  370. if (ret)
  371. goto out_destroy_ib_cq;
  372. queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
  373. sizeof(struct nvme_completion), DMA_FROM_DEVICE);
  374. if (!queue->rsp_ring) {
  375. ret = -ENOMEM;
  376. goto out_destroy_qp;
  377. }
  378. ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
  379. queue->queue_size,
  380. IB_MR_TYPE_MEM_REG,
  381. nvme_rdma_get_max_fr_pages(ibdev));
  382. if (ret) {
  383. dev_err(queue->ctrl->ctrl.device,
  384. "failed to initialize MR pool sized %d for QID %d\n",
  385. queue->queue_size, idx);
  386. goto out_destroy_ring;
  387. }
  388. set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
  389. return 0;
  390. out_destroy_ring:
  391. nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
  392. sizeof(struct nvme_completion), DMA_FROM_DEVICE);
  393. out_destroy_qp:
  394. rdma_destroy_qp(queue->cm_id);
  395. out_destroy_ib_cq:
  396. ib_free_cq(queue->ib_cq);
  397. out_put_dev:
  398. nvme_rdma_dev_put(queue->device);
  399. return ret;
  400. }
  401. static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
  402. int idx, size_t queue_size)
  403. {
  404. struct nvme_rdma_queue *queue;
  405. struct sockaddr *src_addr = NULL;
  406. int ret;
  407. queue = &ctrl->queues[idx];
  408. queue->ctrl = ctrl;
  409. init_completion(&queue->cm_done);
  410. if (idx > 0)
  411. queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
  412. else
  413. queue->cmnd_capsule_len = sizeof(struct nvme_command);
  414. queue->queue_size = queue_size;
  415. queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
  416. RDMA_PS_TCP, IB_QPT_RC);
  417. if (IS_ERR(queue->cm_id)) {
  418. dev_info(ctrl->ctrl.device,
  419. "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
  420. return PTR_ERR(queue->cm_id);
  421. }
  422. if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
  423. src_addr = (struct sockaddr *)&ctrl->src_addr;
  424. queue->cm_error = -ETIMEDOUT;
  425. ret = rdma_resolve_addr(queue->cm_id, src_addr,
  426. (struct sockaddr *)&ctrl->addr,
  427. NVME_RDMA_CONNECT_TIMEOUT_MS);
  428. if (ret) {
  429. dev_info(ctrl->ctrl.device,
  430. "rdma_resolve_addr failed (%d).\n", ret);
  431. goto out_destroy_cm_id;
  432. }
  433. ret = nvme_rdma_wait_for_cm(queue);
  434. if (ret) {
  435. dev_info(ctrl->ctrl.device,
  436. "rdma connection establishment failed (%d)\n", ret);
  437. goto out_destroy_cm_id;
  438. }
  439. set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
  440. return 0;
  441. out_destroy_cm_id:
  442. rdma_destroy_id(queue->cm_id);
  443. nvme_rdma_destroy_queue_ib(queue);
  444. return ret;
  445. }
  446. static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
  447. {
  448. if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
  449. return;
  450. rdma_disconnect(queue->cm_id);
  451. ib_drain_qp(queue->qp);
  452. }
  453. static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
  454. {
  455. if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
  456. return;
  457. nvme_rdma_destroy_queue_ib(queue);
  458. rdma_destroy_id(queue->cm_id);
  459. }
  460. static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
  461. {
  462. int i;
  463. for (i = 1; i < ctrl->ctrl.queue_count; i++)
  464. nvme_rdma_free_queue(&ctrl->queues[i]);
  465. }
  466. static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
  467. {
  468. int i;
  469. for (i = 1; i < ctrl->ctrl.queue_count; i++)
  470. nvme_rdma_stop_queue(&ctrl->queues[i]);
  471. }
  472. static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
  473. {
  474. int ret;
  475. if (idx)
  476. ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
  477. else
  478. ret = nvmf_connect_admin_queue(&ctrl->ctrl);
  479. if (!ret)
  480. set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
  481. else
  482. dev_info(ctrl->ctrl.device,
  483. "failed to connect queue: %d ret=%d\n", idx, ret);
  484. return ret;
  485. }
  486. static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
  487. {
  488. int i, ret = 0;
  489. for (i = 1; i < ctrl->ctrl.queue_count; i++) {
  490. ret = nvme_rdma_start_queue(ctrl, i);
  491. if (ret)
  492. goto out_stop_queues;
  493. }
  494. return 0;
  495. out_stop_queues:
  496. for (i--; i >= 1; i--)
  497. nvme_rdma_stop_queue(&ctrl->queues[i]);
  498. return ret;
  499. }
  500. static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
  501. {
  502. struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
  503. struct ib_device *ibdev = ctrl->device->dev;
  504. unsigned int nr_io_queues;
  505. int i, ret;
  506. nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
  507. /*
  508. * we map queues according to the device irq vectors for
  509. * optimal locality so we don't need more queues than
  510. * completion vectors.
  511. */
  512. nr_io_queues = min_t(unsigned int, nr_io_queues,
  513. ibdev->num_comp_vectors);
  514. ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
  515. if (ret)
  516. return ret;
  517. ctrl->ctrl.queue_count = nr_io_queues + 1;
  518. if (ctrl->ctrl.queue_count < 2)
  519. return 0;
  520. dev_info(ctrl->ctrl.device,
  521. "creating %d I/O queues.\n", nr_io_queues);
  522. for (i = 1; i < ctrl->ctrl.queue_count; i++) {
  523. ret = nvme_rdma_alloc_queue(ctrl, i,
  524. ctrl->ctrl.sqsize + 1);
  525. if (ret)
  526. goto out_free_queues;
  527. }
  528. return 0;
  529. out_free_queues:
  530. for (i--; i >= 1; i--)
  531. nvme_rdma_free_queue(&ctrl->queues[i]);
  532. return ret;
  533. }
  534. static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
  535. struct blk_mq_tag_set *set)
  536. {
  537. struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
  538. blk_mq_free_tag_set(set);
  539. nvme_rdma_dev_put(ctrl->device);
  540. }
  541. static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
  542. bool admin)
  543. {
  544. struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
  545. struct blk_mq_tag_set *set;
  546. int ret;
  547. if (admin) {
  548. set = &ctrl->admin_tag_set;
  549. memset(set, 0, sizeof(*set));
  550. set->ops = &nvme_rdma_admin_mq_ops;
  551. set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
  552. set->reserved_tags = 2; /* connect + keep-alive */
  553. set->numa_node = NUMA_NO_NODE;
  554. set->cmd_size = sizeof(struct nvme_rdma_request) +
  555. SG_CHUNK_SIZE * sizeof(struct scatterlist);
  556. set->driver_data = ctrl;
  557. set->nr_hw_queues = 1;
  558. set->timeout = ADMIN_TIMEOUT;
  559. set->flags = BLK_MQ_F_NO_SCHED;
  560. } else {
  561. set = &ctrl->tag_set;
  562. memset(set, 0, sizeof(*set));
  563. set->ops = &nvme_rdma_mq_ops;
  564. set->queue_depth = nctrl->sqsize + 1;
  565. set->reserved_tags = 1; /* fabric connect */
  566. set->numa_node = NUMA_NO_NODE;
  567. set->flags = BLK_MQ_F_SHOULD_MERGE;
  568. set->cmd_size = sizeof(struct nvme_rdma_request) +
  569. SG_CHUNK_SIZE * sizeof(struct scatterlist);
  570. set->driver_data = ctrl;
  571. set->nr_hw_queues = nctrl->queue_count - 1;
  572. set->timeout = NVME_IO_TIMEOUT;
  573. }
  574. ret = blk_mq_alloc_tag_set(set);
  575. if (ret)
  576. goto out;
  577. /*
  578. * We need a reference on the device as long as the tag_set is alive,
  579. * as the MRs in the request structures need a valid ib_device.
  580. */
  581. ret = nvme_rdma_dev_get(ctrl->device);
  582. if (!ret) {
  583. ret = -EINVAL;
  584. goto out_free_tagset;
  585. }
  586. return set;
  587. out_free_tagset:
  588. blk_mq_free_tag_set(set);
  589. out:
  590. return ERR_PTR(ret);
  591. }
  592. static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
  593. bool remove)
  594. {
  595. if (remove) {
  596. blk_cleanup_queue(ctrl->ctrl.admin_q);
  597. nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
  598. }
  599. if (ctrl->async_event_sqe.data) {
  600. nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
  601. sizeof(struct nvme_command), DMA_TO_DEVICE);
  602. ctrl->async_event_sqe.data = NULL;
  603. }
  604. nvme_rdma_free_queue(&ctrl->queues[0]);
  605. }
  606. static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
  607. bool new)
  608. {
  609. int error;
  610. error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
  611. if (error)
  612. return error;
  613. ctrl->device = ctrl->queues[0].device;
  614. ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
  615. error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
  616. sizeof(struct nvme_command), DMA_TO_DEVICE);
  617. if (error)
  618. goto out_free_queue;
  619. if (new) {
  620. ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
  621. if (IS_ERR(ctrl->ctrl.admin_tagset)) {
  622. error = PTR_ERR(ctrl->ctrl.admin_tagset);
  623. goto out_free_async_qe;
  624. }
  625. ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
  626. if (IS_ERR(ctrl->ctrl.admin_q)) {
  627. error = PTR_ERR(ctrl->ctrl.admin_q);
  628. goto out_free_tagset;
  629. }
  630. }
  631. error = nvme_rdma_start_queue(ctrl, 0);
  632. if (error)
  633. goto out_cleanup_queue;
  634. error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
  635. &ctrl->ctrl.cap);
  636. if (error) {
  637. dev_err(ctrl->ctrl.device,
  638. "prop_get NVME_REG_CAP failed\n");
  639. goto out_stop_queue;
  640. }
  641. ctrl->ctrl.sqsize =
  642. min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
  643. error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
  644. if (error)
  645. goto out_stop_queue;
  646. ctrl->ctrl.max_hw_sectors =
  647. (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
  648. error = nvme_init_identify(&ctrl->ctrl);
  649. if (error)
  650. goto out_stop_queue;
  651. return 0;
  652. out_stop_queue:
  653. nvme_rdma_stop_queue(&ctrl->queues[0]);
  654. out_cleanup_queue:
  655. if (new)
  656. blk_cleanup_queue(ctrl->ctrl.admin_q);
  657. out_free_tagset:
  658. if (new)
  659. nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
  660. out_free_async_qe:
  661. nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
  662. sizeof(struct nvme_command), DMA_TO_DEVICE);
  663. out_free_queue:
  664. nvme_rdma_free_queue(&ctrl->queues[0]);
  665. return error;
  666. }
  667. static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
  668. bool remove)
  669. {
  670. if (remove) {
  671. blk_cleanup_queue(ctrl->ctrl.connect_q);
  672. nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
  673. }
  674. nvme_rdma_free_io_queues(ctrl);
  675. }
  676. static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
  677. {
  678. int ret;
  679. ret = nvme_rdma_alloc_io_queues(ctrl);
  680. if (ret)
  681. return ret;
  682. if (new) {
  683. ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
  684. if (IS_ERR(ctrl->ctrl.tagset)) {
  685. ret = PTR_ERR(ctrl->ctrl.tagset);
  686. goto out_free_io_queues;
  687. }
  688. ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
  689. if (IS_ERR(ctrl->ctrl.connect_q)) {
  690. ret = PTR_ERR(ctrl->ctrl.connect_q);
  691. goto out_free_tag_set;
  692. }
  693. } else {
  694. blk_mq_update_nr_hw_queues(&ctrl->tag_set,
  695. ctrl->ctrl.queue_count - 1);
  696. }
  697. ret = nvme_rdma_start_io_queues(ctrl);
  698. if (ret)
  699. goto out_cleanup_connect_q;
  700. return 0;
  701. out_cleanup_connect_q:
  702. if (new)
  703. blk_cleanup_queue(ctrl->ctrl.connect_q);
  704. out_free_tag_set:
  705. if (new)
  706. nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
  707. out_free_io_queues:
  708. nvme_rdma_free_io_queues(ctrl);
  709. return ret;
  710. }
  711. static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
  712. {
  713. struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
  714. cancel_work_sync(&ctrl->err_work);
  715. cancel_delayed_work_sync(&ctrl->reconnect_work);
  716. }
  717. static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
  718. {
  719. struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
  720. if (list_empty(&ctrl->list))
  721. goto free_ctrl;
  722. mutex_lock(&nvme_rdma_ctrl_mutex);
  723. list_del(&ctrl->list);
  724. mutex_unlock(&nvme_rdma_ctrl_mutex);
  725. nvmf_free_options(nctrl->opts);
  726. free_ctrl:
  727. kfree(ctrl->queues);
  728. kfree(ctrl);
  729. }
  730. static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
  731. {
  732. /* If we are resetting/deleting then do nothing */
  733. if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
  734. WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
  735. ctrl->ctrl.state == NVME_CTRL_LIVE);
  736. return;
  737. }
  738. if (nvmf_should_reconnect(&ctrl->ctrl)) {
  739. dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
  740. ctrl->ctrl.opts->reconnect_delay);
  741. queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
  742. ctrl->ctrl.opts->reconnect_delay * HZ);
  743. } else {
  744. nvme_delete_ctrl(&ctrl->ctrl);
  745. }
  746. }
  747. static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
  748. {
  749. struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
  750. struct nvme_rdma_ctrl, reconnect_work);
  751. bool changed;
  752. int ret;
  753. ++ctrl->ctrl.nr_reconnects;
  754. ret = nvme_rdma_configure_admin_queue(ctrl, false);
  755. if (ret)
  756. goto requeue;
  757. if (ctrl->ctrl.queue_count > 1) {
  758. ret = nvme_rdma_configure_io_queues(ctrl, false);
  759. if (ret)
  760. goto destroy_admin;
  761. }
  762. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
  763. if (!changed) {
  764. /* state change failure is ok if we're in DELETING state */
  765. WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
  766. return;
  767. }
  768. nvme_start_ctrl(&ctrl->ctrl);
  769. dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
  770. ctrl->ctrl.nr_reconnects);
  771. ctrl->ctrl.nr_reconnects = 0;
  772. return;
  773. destroy_admin:
  774. nvme_rdma_stop_queue(&ctrl->queues[0]);
  775. nvme_rdma_destroy_admin_queue(ctrl, false);
  776. requeue:
  777. dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
  778. ctrl->ctrl.nr_reconnects);
  779. nvme_rdma_reconnect_or_remove(ctrl);
  780. }
  781. static void nvme_rdma_error_recovery_work(struct work_struct *work)
  782. {
  783. struct nvme_rdma_ctrl *ctrl = container_of(work,
  784. struct nvme_rdma_ctrl, err_work);
  785. nvme_stop_keep_alive(&ctrl->ctrl);
  786. if (ctrl->ctrl.queue_count > 1) {
  787. nvme_stop_queues(&ctrl->ctrl);
  788. nvme_rdma_stop_io_queues(ctrl);
  789. blk_mq_tagset_busy_iter(&ctrl->tag_set,
  790. nvme_cancel_request, &ctrl->ctrl);
  791. nvme_rdma_destroy_io_queues(ctrl, false);
  792. }
  793. blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
  794. nvme_rdma_stop_queue(&ctrl->queues[0]);
  795. blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
  796. nvme_cancel_request, &ctrl->ctrl);
  797. nvme_rdma_destroy_admin_queue(ctrl, false);
  798. /*
  799. * queues are not a live anymore, so restart the queues to fail fast
  800. * new IO
  801. */
  802. blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
  803. nvme_start_queues(&ctrl->ctrl);
  804. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
  805. /* state change failure is ok if we're in DELETING state */
  806. WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
  807. return;
  808. }
  809. nvme_rdma_reconnect_or_remove(ctrl);
  810. }
  811. static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
  812. {
  813. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
  814. return;
  815. queue_work(nvme_wq, &ctrl->err_work);
  816. }
  817. static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
  818. const char *op)
  819. {
  820. struct nvme_rdma_queue *queue = cq->cq_context;
  821. struct nvme_rdma_ctrl *ctrl = queue->ctrl;
  822. if (ctrl->ctrl.state == NVME_CTRL_LIVE)
  823. dev_info(ctrl->ctrl.device,
  824. "%s for CQE 0x%p failed with status %s (%d)\n",
  825. op, wc->wr_cqe,
  826. ib_wc_status_msg(wc->status), wc->status);
  827. nvme_rdma_error_recovery(ctrl);
  828. }
  829. static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
  830. {
  831. if (unlikely(wc->status != IB_WC_SUCCESS))
  832. nvme_rdma_wr_error(cq, wc, "MEMREG");
  833. }
  834. static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
  835. {
  836. struct nvme_rdma_request *req =
  837. container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
  838. struct request *rq = blk_mq_rq_from_pdu(req);
  839. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  840. nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
  841. return;
  842. }
  843. if (refcount_dec_and_test(&req->ref))
  844. nvme_end_request(rq, req->status, req->result);
  845. }
  846. static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
  847. struct nvme_rdma_request *req)
  848. {
  849. struct ib_send_wr *bad_wr;
  850. struct ib_send_wr wr = {
  851. .opcode = IB_WR_LOCAL_INV,
  852. .next = NULL,
  853. .num_sge = 0,
  854. .send_flags = IB_SEND_SIGNALED,
  855. .ex.invalidate_rkey = req->mr->rkey,
  856. };
  857. req->reg_cqe.done = nvme_rdma_inv_rkey_done;
  858. wr.wr_cqe = &req->reg_cqe;
  859. return ib_post_send(queue->qp, &wr, &bad_wr);
  860. }
  861. static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
  862. struct request *rq)
  863. {
  864. struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
  865. struct nvme_rdma_device *dev = queue->device;
  866. struct ib_device *ibdev = dev->dev;
  867. if (!blk_rq_payload_bytes(rq))
  868. return;
  869. if (req->mr) {
  870. ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
  871. req->mr = NULL;
  872. }
  873. ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
  874. req->nents, rq_data_dir(rq) ==
  875. WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  876. nvme_cleanup_cmd(rq);
  877. sg_free_table_chained(&req->sg_table, true);
  878. }
  879. static int nvme_rdma_set_sg_null(struct nvme_command *c)
  880. {
  881. struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
  882. sg->addr = 0;
  883. put_unaligned_le24(0, sg->length);
  884. put_unaligned_le32(0, sg->key);
  885. sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
  886. return 0;
  887. }
  888. static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
  889. struct nvme_rdma_request *req, struct nvme_command *c)
  890. {
  891. struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
  892. req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
  893. req->sge[1].length = sg_dma_len(req->sg_table.sgl);
  894. req->sge[1].lkey = queue->device->pd->local_dma_lkey;
  895. sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
  896. sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
  897. sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
  898. req->num_sge++;
  899. return 0;
  900. }
  901. static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
  902. struct nvme_rdma_request *req, struct nvme_command *c)
  903. {
  904. struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
  905. sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
  906. put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
  907. put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
  908. sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
  909. return 0;
  910. }
  911. static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
  912. struct nvme_rdma_request *req, struct nvme_command *c,
  913. int count)
  914. {
  915. struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
  916. int nr;
  917. req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
  918. if (WARN_ON_ONCE(!req->mr))
  919. return -EAGAIN;
  920. /*
  921. * Align the MR to a 4K page size to match the ctrl page size and
  922. * the block virtual boundary.
  923. */
  924. nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
  925. if (unlikely(nr < count)) {
  926. ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
  927. req->mr = NULL;
  928. if (nr < 0)
  929. return nr;
  930. return -EINVAL;
  931. }
  932. ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
  933. req->reg_cqe.done = nvme_rdma_memreg_done;
  934. memset(&req->reg_wr, 0, sizeof(req->reg_wr));
  935. req->reg_wr.wr.opcode = IB_WR_REG_MR;
  936. req->reg_wr.wr.wr_cqe = &req->reg_cqe;
  937. req->reg_wr.wr.num_sge = 0;
  938. req->reg_wr.mr = req->mr;
  939. req->reg_wr.key = req->mr->rkey;
  940. req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
  941. IB_ACCESS_REMOTE_READ |
  942. IB_ACCESS_REMOTE_WRITE;
  943. sg->addr = cpu_to_le64(req->mr->iova);
  944. put_unaligned_le24(req->mr->length, sg->length);
  945. put_unaligned_le32(req->mr->rkey, sg->key);
  946. sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
  947. NVME_SGL_FMT_INVALIDATE;
  948. return 0;
  949. }
  950. static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
  951. struct request *rq, struct nvme_command *c)
  952. {
  953. struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
  954. struct nvme_rdma_device *dev = queue->device;
  955. struct ib_device *ibdev = dev->dev;
  956. int count, ret;
  957. req->num_sge = 1;
  958. refcount_set(&req->ref, 2); /* send and recv completions */
  959. c->common.flags |= NVME_CMD_SGL_METABUF;
  960. if (!blk_rq_payload_bytes(rq))
  961. return nvme_rdma_set_sg_null(c);
  962. req->sg_table.sgl = req->first_sgl;
  963. ret = sg_alloc_table_chained(&req->sg_table,
  964. blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
  965. if (ret)
  966. return -ENOMEM;
  967. req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
  968. count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
  969. rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  970. if (unlikely(count <= 0)) {
  971. ret = -EIO;
  972. goto out_free_table;
  973. }
  974. if (count == 1) {
  975. if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
  976. blk_rq_payload_bytes(rq) <=
  977. nvme_rdma_inline_data_size(queue)) {
  978. ret = nvme_rdma_map_sg_inline(queue, req, c);
  979. goto out;
  980. }
  981. if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
  982. ret = nvme_rdma_map_sg_single(queue, req, c);
  983. goto out;
  984. }
  985. }
  986. ret = nvme_rdma_map_sg_fr(queue, req, c, count);
  987. out:
  988. if (unlikely(ret))
  989. goto out_unmap_sg;
  990. return 0;
  991. out_unmap_sg:
  992. ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
  993. req->nents, rq_data_dir(rq) ==
  994. WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  995. out_free_table:
  996. sg_free_table_chained(&req->sg_table, true);
  997. return ret;
  998. }
  999. static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
  1000. {
  1001. struct nvme_rdma_qe *qe =
  1002. container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
  1003. struct nvme_rdma_request *req =
  1004. container_of(qe, struct nvme_rdma_request, sqe);
  1005. struct request *rq = blk_mq_rq_from_pdu(req);
  1006. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  1007. nvme_rdma_wr_error(cq, wc, "SEND");
  1008. return;
  1009. }
  1010. if (refcount_dec_and_test(&req->ref))
  1011. nvme_end_request(rq, req->status, req->result);
  1012. }
  1013. static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
  1014. struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
  1015. struct ib_send_wr *first)
  1016. {
  1017. struct ib_send_wr wr, *bad_wr;
  1018. int ret;
  1019. sge->addr = qe->dma;
  1020. sge->length = sizeof(struct nvme_command),
  1021. sge->lkey = queue->device->pd->local_dma_lkey;
  1022. wr.next = NULL;
  1023. wr.wr_cqe = &qe->cqe;
  1024. wr.sg_list = sge;
  1025. wr.num_sge = num_sge;
  1026. wr.opcode = IB_WR_SEND;
  1027. wr.send_flags = IB_SEND_SIGNALED;
  1028. if (first)
  1029. first->next = &wr;
  1030. else
  1031. first = &wr;
  1032. ret = ib_post_send(queue->qp, first, &bad_wr);
  1033. if (unlikely(ret)) {
  1034. dev_err(queue->ctrl->ctrl.device,
  1035. "%s failed with error code %d\n", __func__, ret);
  1036. }
  1037. return ret;
  1038. }
  1039. static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
  1040. struct nvme_rdma_qe *qe)
  1041. {
  1042. struct ib_recv_wr wr, *bad_wr;
  1043. struct ib_sge list;
  1044. int ret;
  1045. list.addr = qe->dma;
  1046. list.length = sizeof(struct nvme_completion);
  1047. list.lkey = queue->device->pd->local_dma_lkey;
  1048. qe->cqe.done = nvme_rdma_recv_done;
  1049. wr.next = NULL;
  1050. wr.wr_cqe = &qe->cqe;
  1051. wr.sg_list = &list;
  1052. wr.num_sge = 1;
  1053. ret = ib_post_recv(queue->qp, &wr, &bad_wr);
  1054. if (unlikely(ret)) {
  1055. dev_err(queue->ctrl->ctrl.device,
  1056. "%s failed with error code %d\n", __func__, ret);
  1057. }
  1058. return ret;
  1059. }
  1060. static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
  1061. {
  1062. u32 queue_idx = nvme_rdma_queue_idx(queue);
  1063. if (queue_idx == 0)
  1064. return queue->ctrl->admin_tag_set.tags[queue_idx];
  1065. return queue->ctrl->tag_set.tags[queue_idx - 1];
  1066. }
  1067. static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
  1068. {
  1069. if (unlikely(wc->status != IB_WC_SUCCESS))
  1070. nvme_rdma_wr_error(cq, wc, "ASYNC");
  1071. }
  1072. static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
  1073. {
  1074. struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
  1075. struct nvme_rdma_queue *queue = &ctrl->queues[0];
  1076. struct ib_device *dev = queue->device->dev;
  1077. struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
  1078. struct nvme_command *cmd = sqe->data;
  1079. struct ib_sge sge;
  1080. int ret;
  1081. ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
  1082. memset(cmd, 0, sizeof(*cmd));
  1083. cmd->common.opcode = nvme_admin_async_event;
  1084. cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
  1085. cmd->common.flags |= NVME_CMD_SGL_METABUF;
  1086. nvme_rdma_set_sg_null(cmd);
  1087. sqe->cqe.done = nvme_rdma_async_done;
  1088. ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
  1089. DMA_TO_DEVICE);
  1090. ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
  1091. WARN_ON_ONCE(ret);
  1092. }
  1093. static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
  1094. struct nvme_completion *cqe, struct ib_wc *wc, int tag)
  1095. {
  1096. struct request *rq;
  1097. struct nvme_rdma_request *req;
  1098. int ret = 0;
  1099. rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
  1100. if (!rq) {
  1101. dev_err(queue->ctrl->ctrl.device,
  1102. "tag 0x%x on QP %#x not found\n",
  1103. cqe->command_id, queue->qp->qp_num);
  1104. nvme_rdma_error_recovery(queue->ctrl);
  1105. return ret;
  1106. }
  1107. req = blk_mq_rq_to_pdu(rq);
  1108. req->status = cqe->status;
  1109. req->result = cqe->result;
  1110. if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
  1111. if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
  1112. dev_err(queue->ctrl->ctrl.device,
  1113. "Bogus remote invalidation for rkey %#x\n",
  1114. req->mr->rkey);
  1115. nvme_rdma_error_recovery(queue->ctrl);
  1116. }
  1117. } else if (req->mr) {
  1118. ret = nvme_rdma_inv_rkey(queue, req);
  1119. if (unlikely(ret < 0)) {
  1120. dev_err(queue->ctrl->ctrl.device,
  1121. "Queueing INV WR for rkey %#x failed (%d)\n",
  1122. req->mr->rkey, ret);
  1123. nvme_rdma_error_recovery(queue->ctrl);
  1124. }
  1125. /* the local invalidation completion will end the request */
  1126. return 0;
  1127. }
  1128. if (refcount_dec_and_test(&req->ref)) {
  1129. if (rq->tag == tag)
  1130. ret = 1;
  1131. nvme_end_request(rq, req->status, req->result);
  1132. }
  1133. return ret;
  1134. }
  1135. static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
  1136. {
  1137. struct nvme_rdma_qe *qe =
  1138. container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
  1139. struct nvme_rdma_queue *queue = cq->cq_context;
  1140. struct ib_device *ibdev = queue->device->dev;
  1141. struct nvme_completion *cqe = qe->data;
  1142. const size_t len = sizeof(struct nvme_completion);
  1143. int ret = 0;
  1144. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  1145. nvme_rdma_wr_error(cq, wc, "RECV");
  1146. return 0;
  1147. }
  1148. ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
  1149. /*
  1150. * AEN requests are special as they don't time out and can
  1151. * survive any kind of queue freeze and often don't respond to
  1152. * aborts. We don't even bother to allocate a struct request
  1153. * for them but rather special case them here.
  1154. */
  1155. if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
  1156. cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
  1157. nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
  1158. &cqe->result);
  1159. else
  1160. ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
  1161. ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
  1162. nvme_rdma_post_recv(queue, qe);
  1163. return ret;
  1164. }
  1165. static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
  1166. {
  1167. __nvme_rdma_recv_done(cq, wc, -1);
  1168. }
  1169. static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
  1170. {
  1171. int ret, i;
  1172. for (i = 0; i < queue->queue_size; i++) {
  1173. ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
  1174. if (ret)
  1175. goto out_destroy_queue_ib;
  1176. }
  1177. return 0;
  1178. out_destroy_queue_ib:
  1179. nvme_rdma_destroy_queue_ib(queue);
  1180. return ret;
  1181. }
  1182. static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
  1183. struct rdma_cm_event *ev)
  1184. {
  1185. struct rdma_cm_id *cm_id = queue->cm_id;
  1186. int status = ev->status;
  1187. const char *rej_msg;
  1188. const struct nvme_rdma_cm_rej *rej_data;
  1189. u8 rej_data_len;
  1190. rej_msg = rdma_reject_msg(cm_id, status);
  1191. rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
  1192. if (rej_data && rej_data_len >= sizeof(u16)) {
  1193. u16 sts = le16_to_cpu(rej_data->sts);
  1194. dev_err(queue->ctrl->ctrl.device,
  1195. "Connect rejected: status %d (%s) nvme status %d (%s).\n",
  1196. status, rej_msg, sts, nvme_rdma_cm_msg(sts));
  1197. } else {
  1198. dev_err(queue->ctrl->ctrl.device,
  1199. "Connect rejected: status %d (%s).\n", status, rej_msg);
  1200. }
  1201. return -ECONNRESET;
  1202. }
  1203. static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
  1204. {
  1205. int ret;
  1206. ret = nvme_rdma_create_queue_ib(queue);
  1207. if (ret)
  1208. return ret;
  1209. ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
  1210. if (ret) {
  1211. dev_err(queue->ctrl->ctrl.device,
  1212. "rdma_resolve_route failed (%d).\n",
  1213. queue->cm_error);
  1214. goto out_destroy_queue;
  1215. }
  1216. return 0;
  1217. out_destroy_queue:
  1218. nvme_rdma_destroy_queue_ib(queue);
  1219. return ret;
  1220. }
  1221. static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
  1222. {
  1223. struct nvme_rdma_ctrl *ctrl = queue->ctrl;
  1224. struct rdma_conn_param param = { };
  1225. struct nvme_rdma_cm_req priv = { };
  1226. int ret;
  1227. param.qp_num = queue->qp->qp_num;
  1228. param.flow_control = 1;
  1229. param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
  1230. /* maximum retry count */
  1231. param.retry_count = 7;
  1232. param.rnr_retry_count = 7;
  1233. param.private_data = &priv;
  1234. param.private_data_len = sizeof(priv);
  1235. priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
  1236. priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
  1237. /*
  1238. * set the admin queue depth to the minimum size
  1239. * specified by the Fabrics standard.
  1240. */
  1241. if (priv.qid == 0) {
  1242. priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
  1243. priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
  1244. } else {
  1245. /*
  1246. * current interpretation of the fabrics spec
  1247. * is at minimum you make hrqsize sqsize+1, or a
  1248. * 1's based representation of sqsize.
  1249. */
  1250. priv.hrqsize = cpu_to_le16(queue->queue_size);
  1251. priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
  1252. }
  1253. ret = rdma_connect(queue->cm_id, &param);
  1254. if (ret) {
  1255. dev_err(ctrl->ctrl.device,
  1256. "rdma_connect failed (%d).\n", ret);
  1257. goto out_destroy_queue_ib;
  1258. }
  1259. return 0;
  1260. out_destroy_queue_ib:
  1261. nvme_rdma_destroy_queue_ib(queue);
  1262. return ret;
  1263. }
  1264. static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
  1265. struct rdma_cm_event *ev)
  1266. {
  1267. struct nvme_rdma_queue *queue = cm_id->context;
  1268. int cm_error = 0;
  1269. dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
  1270. rdma_event_msg(ev->event), ev->event,
  1271. ev->status, cm_id);
  1272. switch (ev->event) {
  1273. case RDMA_CM_EVENT_ADDR_RESOLVED:
  1274. cm_error = nvme_rdma_addr_resolved(queue);
  1275. break;
  1276. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  1277. cm_error = nvme_rdma_route_resolved(queue);
  1278. break;
  1279. case RDMA_CM_EVENT_ESTABLISHED:
  1280. queue->cm_error = nvme_rdma_conn_established(queue);
  1281. /* complete cm_done regardless of success/failure */
  1282. complete(&queue->cm_done);
  1283. return 0;
  1284. case RDMA_CM_EVENT_REJECTED:
  1285. nvme_rdma_destroy_queue_ib(queue);
  1286. cm_error = nvme_rdma_conn_rejected(queue, ev);
  1287. break;
  1288. case RDMA_CM_EVENT_ROUTE_ERROR:
  1289. case RDMA_CM_EVENT_CONNECT_ERROR:
  1290. case RDMA_CM_EVENT_UNREACHABLE:
  1291. nvme_rdma_destroy_queue_ib(queue);
  1292. case RDMA_CM_EVENT_ADDR_ERROR:
  1293. dev_dbg(queue->ctrl->ctrl.device,
  1294. "CM error event %d\n", ev->event);
  1295. cm_error = -ECONNRESET;
  1296. break;
  1297. case RDMA_CM_EVENT_DISCONNECTED:
  1298. case RDMA_CM_EVENT_ADDR_CHANGE:
  1299. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  1300. dev_dbg(queue->ctrl->ctrl.device,
  1301. "disconnect received - connection closed\n");
  1302. nvme_rdma_error_recovery(queue->ctrl);
  1303. break;
  1304. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  1305. /* device removal is handled via the ib_client API */
  1306. break;
  1307. default:
  1308. dev_err(queue->ctrl->ctrl.device,
  1309. "Unexpected RDMA CM event (%d)\n", ev->event);
  1310. nvme_rdma_error_recovery(queue->ctrl);
  1311. break;
  1312. }
  1313. if (cm_error) {
  1314. queue->cm_error = cm_error;
  1315. complete(&queue->cm_done);
  1316. }
  1317. return 0;
  1318. }
  1319. static enum blk_eh_timer_return
  1320. nvme_rdma_timeout(struct request *rq, bool reserved)
  1321. {
  1322. struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
  1323. dev_warn(req->queue->ctrl->ctrl.device,
  1324. "I/O %d QID %d timeout, reset controller\n",
  1325. rq->tag, nvme_rdma_queue_idx(req->queue));
  1326. /* queue error recovery */
  1327. nvme_rdma_error_recovery(req->queue->ctrl);
  1328. /* fail with DNR on cmd timeout */
  1329. nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
  1330. return BLK_EH_DONE;
  1331. }
  1332. static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
  1333. const struct blk_mq_queue_data *bd)
  1334. {
  1335. struct nvme_ns *ns = hctx->queue->queuedata;
  1336. struct nvme_rdma_queue *queue = hctx->driver_data;
  1337. struct request *rq = bd->rq;
  1338. struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
  1339. struct nvme_rdma_qe *sqe = &req->sqe;
  1340. struct nvme_command *c = sqe->data;
  1341. struct ib_device *dev;
  1342. bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
  1343. blk_status_t ret;
  1344. int err;
  1345. WARN_ON_ONCE(rq->tag < 0);
  1346. if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
  1347. return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
  1348. dev = queue->device->dev;
  1349. ib_dma_sync_single_for_cpu(dev, sqe->dma,
  1350. sizeof(struct nvme_command), DMA_TO_DEVICE);
  1351. ret = nvme_setup_cmd(ns, rq, c);
  1352. if (ret)
  1353. return ret;
  1354. blk_mq_start_request(rq);
  1355. err = nvme_rdma_map_data(queue, rq, c);
  1356. if (unlikely(err < 0)) {
  1357. dev_err(queue->ctrl->ctrl.device,
  1358. "Failed to map data (%d)\n", err);
  1359. nvme_cleanup_cmd(rq);
  1360. goto err;
  1361. }
  1362. sqe->cqe.done = nvme_rdma_send_done;
  1363. ib_dma_sync_single_for_device(dev, sqe->dma,
  1364. sizeof(struct nvme_command), DMA_TO_DEVICE);
  1365. err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
  1366. req->mr ? &req->reg_wr.wr : NULL);
  1367. if (unlikely(err)) {
  1368. nvme_rdma_unmap_data(queue, rq);
  1369. goto err;
  1370. }
  1371. return BLK_STS_OK;
  1372. err:
  1373. if (err == -ENOMEM || err == -EAGAIN)
  1374. return BLK_STS_RESOURCE;
  1375. return BLK_STS_IOERR;
  1376. }
  1377. static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
  1378. {
  1379. struct nvme_rdma_queue *queue = hctx->driver_data;
  1380. struct ib_cq *cq = queue->ib_cq;
  1381. struct ib_wc wc;
  1382. int found = 0;
  1383. while (ib_poll_cq(cq, 1, &wc) > 0) {
  1384. struct ib_cqe *cqe = wc.wr_cqe;
  1385. if (cqe) {
  1386. if (cqe->done == nvme_rdma_recv_done)
  1387. found |= __nvme_rdma_recv_done(cq, &wc, tag);
  1388. else
  1389. cqe->done(cq, &wc);
  1390. }
  1391. }
  1392. return found;
  1393. }
  1394. static void nvme_rdma_complete_rq(struct request *rq)
  1395. {
  1396. struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
  1397. nvme_rdma_unmap_data(req->queue, rq);
  1398. nvme_complete_rq(rq);
  1399. }
  1400. static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
  1401. {
  1402. struct nvme_rdma_ctrl *ctrl = set->driver_data;
  1403. return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
  1404. }
  1405. static const struct blk_mq_ops nvme_rdma_mq_ops = {
  1406. .queue_rq = nvme_rdma_queue_rq,
  1407. .complete = nvme_rdma_complete_rq,
  1408. .init_request = nvme_rdma_init_request,
  1409. .exit_request = nvme_rdma_exit_request,
  1410. .init_hctx = nvme_rdma_init_hctx,
  1411. .poll = nvme_rdma_poll,
  1412. .timeout = nvme_rdma_timeout,
  1413. .map_queues = nvme_rdma_map_queues,
  1414. };
  1415. static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
  1416. .queue_rq = nvme_rdma_queue_rq,
  1417. .complete = nvme_rdma_complete_rq,
  1418. .init_request = nvme_rdma_init_request,
  1419. .exit_request = nvme_rdma_exit_request,
  1420. .init_hctx = nvme_rdma_init_admin_hctx,
  1421. .timeout = nvme_rdma_timeout,
  1422. };
  1423. static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
  1424. {
  1425. if (ctrl->ctrl.queue_count > 1) {
  1426. nvme_stop_queues(&ctrl->ctrl);
  1427. nvme_rdma_stop_io_queues(ctrl);
  1428. blk_mq_tagset_busy_iter(&ctrl->tag_set,
  1429. nvme_cancel_request, &ctrl->ctrl);
  1430. nvme_rdma_destroy_io_queues(ctrl, shutdown);
  1431. }
  1432. if (shutdown)
  1433. nvme_shutdown_ctrl(&ctrl->ctrl);
  1434. else
  1435. nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
  1436. blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
  1437. nvme_rdma_stop_queue(&ctrl->queues[0]);
  1438. blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
  1439. nvme_cancel_request, &ctrl->ctrl);
  1440. blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
  1441. nvme_rdma_destroy_admin_queue(ctrl, shutdown);
  1442. }
  1443. static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
  1444. {
  1445. nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
  1446. }
  1447. static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
  1448. {
  1449. struct nvme_rdma_ctrl *ctrl =
  1450. container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
  1451. int ret;
  1452. bool changed;
  1453. nvme_stop_ctrl(&ctrl->ctrl);
  1454. nvme_rdma_shutdown_ctrl(ctrl, false);
  1455. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
  1456. /* state change failure should never happen */
  1457. WARN_ON_ONCE(1);
  1458. return;
  1459. }
  1460. ret = nvme_rdma_configure_admin_queue(ctrl, false);
  1461. if (ret)
  1462. goto out_fail;
  1463. if (ctrl->ctrl.queue_count > 1) {
  1464. ret = nvme_rdma_configure_io_queues(ctrl, false);
  1465. if (ret)
  1466. goto out_fail;
  1467. }
  1468. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
  1469. if (!changed) {
  1470. /* state change failure is ok if we're in DELETING state */
  1471. WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
  1472. return;
  1473. }
  1474. nvme_start_ctrl(&ctrl->ctrl);
  1475. return;
  1476. out_fail:
  1477. ++ctrl->ctrl.nr_reconnects;
  1478. nvme_rdma_reconnect_or_remove(ctrl);
  1479. }
  1480. static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
  1481. .name = "rdma",
  1482. .module = THIS_MODULE,
  1483. .flags = NVME_F_FABRICS,
  1484. .reg_read32 = nvmf_reg_read32,
  1485. .reg_read64 = nvmf_reg_read64,
  1486. .reg_write32 = nvmf_reg_write32,
  1487. .free_ctrl = nvme_rdma_free_ctrl,
  1488. .submit_async_event = nvme_rdma_submit_async_event,
  1489. .delete_ctrl = nvme_rdma_delete_ctrl,
  1490. .get_address = nvmf_get_address,
  1491. .stop_ctrl = nvme_rdma_stop_ctrl,
  1492. };
  1493. static inline bool
  1494. __nvme_rdma_options_match(struct nvme_rdma_ctrl *ctrl,
  1495. struct nvmf_ctrl_options *opts)
  1496. {
  1497. char *stdport = __stringify(NVME_RDMA_IP_PORT);
  1498. if (!nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts) ||
  1499. strcmp(opts->traddr, ctrl->ctrl.opts->traddr))
  1500. return false;
  1501. if (opts->mask & NVMF_OPT_TRSVCID &&
  1502. ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) {
  1503. if (strcmp(opts->trsvcid, ctrl->ctrl.opts->trsvcid))
  1504. return false;
  1505. } else if (opts->mask & NVMF_OPT_TRSVCID) {
  1506. if (strcmp(opts->trsvcid, stdport))
  1507. return false;
  1508. } else if (ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) {
  1509. if (strcmp(stdport, ctrl->ctrl.opts->trsvcid))
  1510. return false;
  1511. }
  1512. /* else, it's a match as both have stdport. Fall to next checks */
  1513. /*
  1514. * checking the local address is rough. In most cases, one
  1515. * is not specified and the host port is selected by the stack.
  1516. *
  1517. * Assume no match if:
  1518. * local address is specified and address is not the same
  1519. * local address is not specified but remote is, or vice versa
  1520. * (admin using specific host_traddr when it matters).
  1521. */
  1522. if (opts->mask & NVMF_OPT_HOST_TRADDR &&
  1523. ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) {
  1524. if (strcmp(opts->host_traddr, ctrl->ctrl.opts->host_traddr))
  1525. return false;
  1526. } else if (opts->mask & NVMF_OPT_HOST_TRADDR ||
  1527. ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
  1528. return false;
  1529. /*
  1530. * if neither controller had an host port specified, assume it's
  1531. * a match as everything else matched.
  1532. */
  1533. return true;
  1534. }
  1535. /*
  1536. * Fails a connection request if it matches an existing controller
  1537. * (association) with the same tuple:
  1538. * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
  1539. *
  1540. * if local address is not specified in the request, it will match an
  1541. * existing controller with all the other parameters the same and no
  1542. * local port address specified as well.
  1543. *
  1544. * The ports don't need to be compared as they are intrinsically
  1545. * already matched by the port pointers supplied.
  1546. */
  1547. static bool
  1548. nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
  1549. {
  1550. struct nvme_rdma_ctrl *ctrl;
  1551. bool found = false;
  1552. mutex_lock(&nvme_rdma_ctrl_mutex);
  1553. list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
  1554. found = __nvme_rdma_options_match(ctrl, opts);
  1555. if (found)
  1556. break;
  1557. }
  1558. mutex_unlock(&nvme_rdma_ctrl_mutex);
  1559. return found;
  1560. }
  1561. static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
  1562. struct nvmf_ctrl_options *opts)
  1563. {
  1564. struct nvme_rdma_ctrl *ctrl;
  1565. int ret;
  1566. bool changed;
  1567. char *port;
  1568. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  1569. if (!ctrl)
  1570. return ERR_PTR(-ENOMEM);
  1571. ctrl->ctrl.opts = opts;
  1572. INIT_LIST_HEAD(&ctrl->list);
  1573. if (opts->mask & NVMF_OPT_TRSVCID)
  1574. port = opts->trsvcid;
  1575. else
  1576. port = __stringify(NVME_RDMA_IP_PORT);
  1577. ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
  1578. opts->traddr, port, &ctrl->addr);
  1579. if (ret) {
  1580. pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
  1581. goto out_free_ctrl;
  1582. }
  1583. if (opts->mask & NVMF_OPT_HOST_TRADDR) {
  1584. ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
  1585. opts->host_traddr, NULL, &ctrl->src_addr);
  1586. if (ret) {
  1587. pr_err("malformed src address passed: %s\n",
  1588. opts->host_traddr);
  1589. goto out_free_ctrl;
  1590. }
  1591. }
  1592. if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
  1593. ret = -EALREADY;
  1594. goto out_free_ctrl;
  1595. }
  1596. INIT_DELAYED_WORK(&ctrl->reconnect_work,
  1597. nvme_rdma_reconnect_ctrl_work);
  1598. INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
  1599. INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
  1600. ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
  1601. ctrl->ctrl.sqsize = opts->queue_size - 1;
  1602. ctrl->ctrl.kato = opts->kato;
  1603. ret = -ENOMEM;
  1604. ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
  1605. GFP_KERNEL);
  1606. if (!ctrl->queues)
  1607. goto out_free_ctrl;
  1608. ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
  1609. 0 /* no quirks, we're perfect! */);
  1610. if (ret)
  1611. goto out_kfree_queues;
  1612. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
  1613. WARN_ON_ONCE(!changed);
  1614. ret = nvme_rdma_configure_admin_queue(ctrl, true);
  1615. if (ret)
  1616. goto out_uninit_ctrl;
  1617. /* sanity check icdoff */
  1618. if (ctrl->ctrl.icdoff) {
  1619. dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
  1620. ret = -EINVAL;
  1621. goto out_remove_admin_queue;
  1622. }
  1623. /* sanity check keyed sgls */
  1624. if (!(ctrl->ctrl.sgls & (1 << 2))) {
  1625. dev_err(ctrl->ctrl.device,
  1626. "Mandatory keyed sgls are not supported!\n");
  1627. ret = -EINVAL;
  1628. goto out_remove_admin_queue;
  1629. }
  1630. /* only warn if argument is too large here, will clamp later */
  1631. if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
  1632. dev_warn(ctrl->ctrl.device,
  1633. "queue_size %zu > ctrl sqsize %u, clamping down\n",
  1634. opts->queue_size, ctrl->ctrl.sqsize + 1);
  1635. }
  1636. /* warn if maxcmd is lower than sqsize+1 */
  1637. if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
  1638. dev_warn(ctrl->ctrl.device,
  1639. "sqsize %u > ctrl maxcmd %u, clamping down\n",
  1640. ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
  1641. ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
  1642. }
  1643. if (opts->nr_io_queues) {
  1644. ret = nvme_rdma_configure_io_queues(ctrl, true);
  1645. if (ret)
  1646. goto out_remove_admin_queue;
  1647. }
  1648. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
  1649. WARN_ON_ONCE(!changed);
  1650. dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
  1651. ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
  1652. nvme_get_ctrl(&ctrl->ctrl);
  1653. mutex_lock(&nvme_rdma_ctrl_mutex);
  1654. list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
  1655. mutex_unlock(&nvme_rdma_ctrl_mutex);
  1656. nvme_start_ctrl(&ctrl->ctrl);
  1657. return &ctrl->ctrl;
  1658. out_remove_admin_queue:
  1659. nvme_rdma_stop_queue(&ctrl->queues[0]);
  1660. nvme_rdma_destroy_admin_queue(ctrl, true);
  1661. out_uninit_ctrl:
  1662. nvme_uninit_ctrl(&ctrl->ctrl);
  1663. nvme_put_ctrl(&ctrl->ctrl);
  1664. if (ret > 0)
  1665. ret = -EIO;
  1666. return ERR_PTR(ret);
  1667. out_kfree_queues:
  1668. kfree(ctrl->queues);
  1669. out_free_ctrl:
  1670. kfree(ctrl);
  1671. return ERR_PTR(ret);
  1672. }
  1673. static struct nvmf_transport_ops nvme_rdma_transport = {
  1674. .name = "rdma",
  1675. .module = THIS_MODULE,
  1676. .required_opts = NVMF_OPT_TRADDR,
  1677. .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
  1678. NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
  1679. .create_ctrl = nvme_rdma_create_ctrl,
  1680. };
  1681. static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
  1682. {
  1683. struct nvme_rdma_ctrl *ctrl;
  1684. struct nvme_rdma_device *ndev;
  1685. bool found = false;
  1686. mutex_lock(&device_list_mutex);
  1687. list_for_each_entry(ndev, &device_list, entry) {
  1688. if (ndev->dev == ib_device) {
  1689. found = true;
  1690. break;
  1691. }
  1692. }
  1693. mutex_unlock(&device_list_mutex);
  1694. if (!found)
  1695. return;
  1696. /* Delete all controllers using this device */
  1697. mutex_lock(&nvme_rdma_ctrl_mutex);
  1698. list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
  1699. if (ctrl->device->dev != ib_device)
  1700. continue;
  1701. nvme_delete_ctrl(&ctrl->ctrl);
  1702. }
  1703. mutex_unlock(&nvme_rdma_ctrl_mutex);
  1704. flush_workqueue(nvme_delete_wq);
  1705. }
  1706. static struct ib_client nvme_rdma_ib_client = {
  1707. .name = "nvme_rdma",
  1708. .remove = nvme_rdma_remove_one
  1709. };
  1710. static int __init nvme_rdma_init_module(void)
  1711. {
  1712. int ret;
  1713. ret = ib_register_client(&nvme_rdma_ib_client);
  1714. if (ret)
  1715. return ret;
  1716. ret = nvmf_register_transport(&nvme_rdma_transport);
  1717. if (ret)
  1718. goto err_unreg_client;
  1719. return 0;
  1720. err_unreg_client:
  1721. ib_unregister_client(&nvme_rdma_ib_client);
  1722. return ret;
  1723. }
  1724. static void __exit nvme_rdma_cleanup_module(void)
  1725. {
  1726. nvmf_unregister_transport(&nvme_rdma_transport);
  1727. ib_unregister_client(&nvme_rdma_ib_client);
  1728. }
  1729. module_init(nvme_rdma_init_module);
  1730. module_exit(nvme_rdma_cleanup_module);
  1731. MODULE_LICENSE("GPL v2");