rdma.c 52 KB

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