rdma.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVMe over Fabrics RDMA target.
  4. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/atomic.h>
  8. #include <linux/blk-integrity.h>
  9. #include <linux/ctype.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/nvme.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/wait.h>
  18. #include <linux/inet.h>
  19. #include <asm/unaligned.h>
  20. #include <rdma/ib_verbs.h>
  21. #include <rdma/rdma_cm.h>
  22. #include <rdma/rw.h>
  23. #include <rdma/ib_cm.h>
  24. #include <linux/nvme-rdma.h>
  25. #include "nvmet.h"
  26. /*
  27. * We allow at least 1 page, up to 4 SGEs, and up to 16KB of inline data
  28. */
  29. #define NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE PAGE_SIZE
  30. #define NVMET_RDMA_MAX_INLINE_SGE 4
  31. #define NVMET_RDMA_MAX_INLINE_DATA_SIZE max_t(int, SZ_16K, PAGE_SIZE)
  32. /* Assume mpsmin == device_page_size == 4KB */
  33. #define NVMET_RDMA_MAX_MDTS 8
  34. #define NVMET_RDMA_MAX_METADATA_MDTS 5
  35. struct nvmet_rdma_srq;
  36. struct nvmet_rdma_cmd {
  37. struct ib_sge sge[NVMET_RDMA_MAX_INLINE_SGE + 1];
  38. struct ib_cqe cqe;
  39. struct ib_recv_wr wr;
  40. struct scatterlist inline_sg[NVMET_RDMA_MAX_INLINE_SGE];
  41. struct nvme_command *nvme_cmd;
  42. struct nvmet_rdma_queue *queue;
  43. struct nvmet_rdma_srq *nsrq;
  44. };
  45. enum {
  46. NVMET_RDMA_REQ_INLINE_DATA = (1 << 0),
  47. NVMET_RDMA_REQ_INVALIDATE_RKEY = (1 << 1),
  48. };
  49. struct nvmet_rdma_rsp {
  50. struct ib_sge send_sge;
  51. struct ib_cqe send_cqe;
  52. struct ib_send_wr send_wr;
  53. struct nvmet_rdma_cmd *cmd;
  54. struct nvmet_rdma_queue *queue;
  55. struct ib_cqe read_cqe;
  56. struct ib_cqe write_cqe;
  57. struct rdma_rw_ctx rw;
  58. struct nvmet_req req;
  59. bool allocated;
  60. u8 n_rdma;
  61. u32 flags;
  62. u32 invalidate_rkey;
  63. struct list_head wait_list;
  64. struct list_head free_list;
  65. };
  66. enum nvmet_rdma_queue_state {
  67. NVMET_RDMA_Q_CONNECTING,
  68. NVMET_RDMA_Q_LIVE,
  69. NVMET_RDMA_Q_DISCONNECTING,
  70. };
  71. struct nvmet_rdma_queue {
  72. struct rdma_cm_id *cm_id;
  73. struct ib_qp *qp;
  74. struct nvmet_port *port;
  75. struct ib_cq *cq;
  76. atomic_t sq_wr_avail;
  77. struct nvmet_rdma_device *dev;
  78. struct nvmet_rdma_srq *nsrq;
  79. spinlock_t state_lock;
  80. enum nvmet_rdma_queue_state state;
  81. struct nvmet_cq nvme_cq;
  82. struct nvmet_sq nvme_sq;
  83. struct nvmet_rdma_rsp *rsps;
  84. struct list_head free_rsps;
  85. spinlock_t rsps_lock;
  86. struct nvmet_rdma_cmd *cmds;
  87. struct work_struct release_work;
  88. struct list_head rsp_wait_list;
  89. struct list_head rsp_wr_wait_list;
  90. spinlock_t rsp_wr_wait_lock;
  91. int idx;
  92. int host_qid;
  93. int comp_vector;
  94. int recv_queue_size;
  95. int send_queue_size;
  96. struct list_head queue_list;
  97. };
  98. struct nvmet_rdma_port {
  99. struct nvmet_port *nport;
  100. struct sockaddr_storage addr;
  101. struct rdma_cm_id *cm_id;
  102. struct delayed_work repair_work;
  103. };
  104. struct nvmet_rdma_srq {
  105. struct ib_srq *srq;
  106. struct nvmet_rdma_cmd *cmds;
  107. struct nvmet_rdma_device *ndev;
  108. };
  109. struct nvmet_rdma_device {
  110. struct ib_device *device;
  111. struct ib_pd *pd;
  112. struct nvmet_rdma_srq **srqs;
  113. int srq_count;
  114. size_t srq_size;
  115. struct kref ref;
  116. struct list_head entry;
  117. int inline_data_size;
  118. int inline_page_count;
  119. };
  120. static bool nvmet_rdma_use_srq;
  121. module_param_named(use_srq, nvmet_rdma_use_srq, bool, 0444);
  122. MODULE_PARM_DESC(use_srq, "Use shared receive queue.");
  123. static int srq_size_set(const char *val, const struct kernel_param *kp);
  124. static const struct kernel_param_ops srq_size_ops = {
  125. .set = srq_size_set,
  126. .get = param_get_int,
  127. };
  128. static int nvmet_rdma_srq_size = 1024;
  129. module_param_cb(srq_size, &srq_size_ops, &nvmet_rdma_srq_size, 0644);
  130. MODULE_PARM_DESC(srq_size, "set Shared Receive Queue (SRQ) size, should >= 256 (default: 1024)");
  131. static DEFINE_IDA(nvmet_rdma_queue_ida);
  132. static LIST_HEAD(nvmet_rdma_queue_list);
  133. static DEFINE_MUTEX(nvmet_rdma_queue_mutex);
  134. static LIST_HEAD(device_list);
  135. static DEFINE_MUTEX(device_list_mutex);
  136. static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp);
  137. static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc);
  138. static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
  139. static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc);
  140. static void nvmet_rdma_write_data_done(struct ib_cq *cq, struct ib_wc *wc);
  141. static void nvmet_rdma_qp_event(struct ib_event *event, void *priv);
  142. static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue);
  143. static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev,
  144. struct nvmet_rdma_rsp *r);
  145. static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev,
  146. struct nvmet_rdma_rsp *r);
  147. static const struct nvmet_fabrics_ops nvmet_rdma_ops;
  148. static int srq_size_set(const char *val, const struct kernel_param *kp)
  149. {
  150. int n = 0, ret;
  151. ret = kstrtoint(val, 10, &n);
  152. if (ret != 0 || n < 256)
  153. return -EINVAL;
  154. return param_set_int(val, kp);
  155. }
  156. static int num_pages(int len)
  157. {
  158. return 1 + (((len - 1) & PAGE_MASK) >> PAGE_SHIFT);
  159. }
  160. static inline bool nvmet_rdma_need_data_in(struct nvmet_rdma_rsp *rsp)
  161. {
  162. return nvme_is_write(rsp->req.cmd) &&
  163. rsp->req.transfer_len &&
  164. !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA);
  165. }
  166. static inline bool nvmet_rdma_need_data_out(struct nvmet_rdma_rsp *rsp)
  167. {
  168. return !nvme_is_write(rsp->req.cmd) &&
  169. rsp->req.transfer_len &&
  170. !rsp->req.cqe->status &&
  171. !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA);
  172. }
  173. static inline struct nvmet_rdma_rsp *
  174. nvmet_rdma_get_rsp(struct nvmet_rdma_queue *queue)
  175. {
  176. struct nvmet_rdma_rsp *rsp;
  177. unsigned long flags;
  178. spin_lock_irqsave(&queue->rsps_lock, flags);
  179. rsp = list_first_entry_or_null(&queue->free_rsps,
  180. struct nvmet_rdma_rsp, free_list);
  181. if (likely(rsp))
  182. list_del(&rsp->free_list);
  183. spin_unlock_irqrestore(&queue->rsps_lock, flags);
  184. if (unlikely(!rsp)) {
  185. int ret;
  186. rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
  187. if (unlikely(!rsp))
  188. return NULL;
  189. ret = nvmet_rdma_alloc_rsp(queue->dev, rsp);
  190. if (unlikely(ret)) {
  191. kfree(rsp);
  192. return NULL;
  193. }
  194. rsp->allocated = true;
  195. }
  196. return rsp;
  197. }
  198. static inline void
  199. nvmet_rdma_put_rsp(struct nvmet_rdma_rsp *rsp)
  200. {
  201. unsigned long flags;
  202. if (unlikely(rsp->allocated)) {
  203. nvmet_rdma_free_rsp(rsp->queue->dev, rsp);
  204. kfree(rsp);
  205. return;
  206. }
  207. spin_lock_irqsave(&rsp->queue->rsps_lock, flags);
  208. list_add_tail(&rsp->free_list, &rsp->queue->free_rsps);
  209. spin_unlock_irqrestore(&rsp->queue->rsps_lock, flags);
  210. }
  211. static void nvmet_rdma_free_inline_pages(struct nvmet_rdma_device *ndev,
  212. struct nvmet_rdma_cmd *c)
  213. {
  214. struct scatterlist *sg;
  215. struct ib_sge *sge;
  216. int i;
  217. if (!ndev->inline_data_size)
  218. return;
  219. sg = c->inline_sg;
  220. sge = &c->sge[1];
  221. for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) {
  222. if (sge->length)
  223. ib_dma_unmap_page(ndev->device, sge->addr,
  224. sge->length, DMA_FROM_DEVICE);
  225. if (sg_page(sg))
  226. __free_page(sg_page(sg));
  227. }
  228. }
  229. static int nvmet_rdma_alloc_inline_pages(struct nvmet_rdma_device *ndev,
  230. struct nvmet_rdma_cmd *c)
  231. {
  232. struct scatterlist *sg;
  233. struct ib_sge *sge;
  234. struct page *pg;
  235. int len;
  236. int i;
  237. if (!ndev->inline_data_size)
  238. return 0;
  239. sg = c->inline_sg;
  240. sg_init_table(sg, ndev->inline_page_count);
  241. sge = &c->sge[1];
  242. len = ndev->inline_data_size;
  243. for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) {
  244. pg = alloc_page(GFP_KERNEL);
  245. if (!pg)
  246. goto out_err;
  247. sg_assign_page(sg, pg);
  248. sge->addr = ib_dma_map_page(ndev->device,
  249. pg, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  250. if (ib_dma_mapping_error(ndev->device, sge->addr))
  251. goto out_err;
  252. sge->length = min_t(int, len, PAGE_SIZE);
  253. sge->lkey = ndev->pd->local_dma_lkey;
  254. len -= sge->length;
  255. }
  256. return 0;
  257. out_err:
  258. for (; i >= 0; i--, sg--, sge--) {
  259. if (sge->length)
  260. ib_dma_unmap_page(ndev->device, sge->addr,
  261. sge->length, DMA_FROM_DEVICE);
  262. if (sg_page(sg))
  263. __free_page(sg_page(sg));
  264. }
  265. return -ENOMEM;
  266. }
  267. static int nvmet_rdma_alloc_cmd(struct nvmet_rdma_device *ndev,
  268. struct nvmet_rdma_cmd *c, bool admin)
  269. {
  270. /* NVMe command / RDMA RECV */
  271. c->nvme_cmd = kmalloc(sizeof(*c->nvme_cmd), GFP_KERNEL);
  272. if (!c->nvme_cmd)
  273. goto out;
  274. c->sge[0].addr = ib_dma_map_single(ndev->device, c->nvme_cmd,
  275. sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
  276. if (ib_dma_mapping_error(ndev->device, c->sge[0].addr))
  277. goto out_free_cmd;
  278. c->sge[0].length = sizeof(*c->nvme_cmd);
  279. c->sge[0].lkey = ndev->pd->local_dma_lkey;
  280. if (!admin && nvmet_rdma_alloc_inline_pages(ndev, c))
  281. goto out_unmap_cmd;
  282. c->cqe.done = nvmet_rdma_recv_done;
  283. c->wr.wr_cqe = &c->cqe;
  284. c->wr.sg_list = c->sge;
  285. c->wr.num_sge = admin ? 1 : ndev->inline_page_count + 1;
  286. return 0;
  287. out_unmap_cmd:
  288. ib_dma_unmap_single(ndev->device, c->sge[0].addr,
  289. sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
  290. out_free_cmd:
  291. kfree(c->nvme_cmd);
  292. out:
  293. return -ENOMEM;
  294. }
  295. static void nvmet_rdma_free_cmd(struct nvmet_rdma_device *ndev,
  296. struct nvmet_rdma_cmd *c, bool admin)
  297. {
  298. if (!admin)
  299. nvmet_rdma_free_inline_pages(ndev, c);
  300. ib_dma_unmap_single(ndev->device, c->sge[0].addr,
  301. sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
  302. kfree(c->nvme_cmd);
  303. }
  304. static struct nvmet_rdma_cmd *
  305. nvmet_rdma_alloc_cmds(struct nvmet_rdma_device *ndev,
  306. int nr_cmds, bool admin)
  307. {
  308. struct nvmet_rdma_cmd *cmds;
  309. int ret = -EINVAL, i;
  310. cmds = kcalloc(nr_cmds, sizeof(struct nvmet_rdma_cmd), GFP_KERNEL);
  311. if (!cmds)
  312. goto out;
  313. for (i = 0; i < nr_cmds; i++) {
  314. ret = nvmet_rdma_alloc_cmd(ndev, cmds + i, admin);
  315. if (ret)
  316. goto out_free;
  317. }
  318. return cmds;
  319. out_free:
  320. while (--i >= 0)
  321. nvmet_rdma_free_cmd(ndev, cmds + i, admin);
  322. kfree(cmds);
  323. out:
  324. return ERR_PTR(ret);
  325. }
  326. static void nvmet_rdma_free_cmds(struct nvmet_rdma_device *ndev,
  327. struct nvmet_rdma_cmd *cmds, int nr_cmds, bool admin)
  328. {
  329. int i;
  330. for (i = 0; i < nr_cmds; i++)
  331. nvmet_rdma_free_cmd(ndev, cmds + i, admin);
  332. kfree(cmds);
  333. }
  334. static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev,
  335. struct nvmet_rdma_rsp *r)
  336. {
  337. /* NVMe CQE / RDMA SEND */
  338. r->req.cqe = kmalloc(sizeof(*r->req.cqe), GFP_KERNEL);
  339. if (!r->req.cqe)
  340. goto out;
  341. r->send_sge.addr = ib_dma_map_single(ndev->device, r->req.cqe,
  342. sizeof(*r->req.cqe), DMA_TO_DEVICE);
  343. if (ib_dma_mapping_error(ndev->device, r->send_sge.addr))
  344. goto out_free_rsp;
  345. if (ib_dma_pci_p2p_dma_supported(ndev->device))
  346. r->req.p2p_client = &ndev->device->dev;
  347. r->send_sge.length = sizeof(*r->req.cqe);
  348. r->send_sge.lkey = ndev->pd->local_dma_lkey;
  349. r->send_cqe.done = nvmet_rdma_send_done;
  350. r->send_wr.wr_cqe = &r->send_cqe;
  351. r->send_wr.sg_list = &r->send_sge;
  352. r->send_wr.num_sge = 1;
  353. r->send_wr.send_flags = IB_SEND_SIGNALED;
  354. /* Data In / RDMA READ */
  355. r->read_cqe.done = nvmet_rdma_read_data_done;
  356. /* Data Out / RDMA WRITE */
  357. r->write_cqe.done = nvmet_rdma_write_data_done;
  358. return 0;
  359. out_free_rsp:
  360. kfree(r->req.cqe);
  361. out:
  362. return -ENOMEM;
  363. }
  364. static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev,
  365. struct nvmet_rdma_rsp *r)
  366. {
  367. ib_dma_unmap_single(ndev->device, r->send_sge.addr,
  368. sizeof(*r->req.cqe), DMA_TO_DEVICE);
  369. kfree(r->req.cqe);
  370. }
  371. static int
  372. nvmet_rdma_alloc_rsps(struct nvmet_rdma_queue *queue)
  373. {
  374. struct nvmet_rdma_device *ndev = queue->dev;
  375. int nr_rsps = queue->recv_queue_size * 2;
  376. int ret = -EINVAL, i;
  377. queue->rsps = kcalloc(nr_rsps, sizeof(struct nvmet_rdma_rsp),
  378. GFP_KERNEL);
  379. if (!queue->rsps)
  380. goto out;
  381. for (i = 0; i < nr_rsps; i++) {
  382. struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
  383. ret = nvmet_rdma_alloc_rsp(ndev, rsp);
  384. if (ret)
  385. goto out_free;
  386. list_add_tail(&rsp->free_list, &queue->free_rsps);
  387. }
  388. return 0;
  389. out_free:
  390. while (--i >= 0) {
  391. struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
  392. list_del(&rsp->free_list);
  393. nvmet_rdma_free_rsp(ndev, rsp);
  394. }
  395. kfree(queue->rsps);
  396. out:
  397. return ret;
  398. }
  399. static void nvmet_rdma_free_rsps(struct nvmet_rdma_queue *queue)
  400. {
  401. struct nvmet_rdma_device *ndev = queue->dev;
  402. int i, nr_rsps = queue->recv_queue_size * 2;
  403. for (i = 0; i < nr_rsps; i++) {
  404. struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
  405. list_del(&rsp->free_list);
  406. nvmet_rdma_free_rsp(ndev, rsp);
  407. }
  408. kfree(queue->rsps);
  409. }
  410. static int nvmet_rdma_post_recv(struct nvmet_rdma_device *ndev,
  411. struct nvmet_rdma_cmd *cmd)
  412. {
  413. int ret;
  414. ib_dma_sync_single_for_device(ndev->device,
  415. cmd->sge[0].addr, cmd->sge[0].length,
  416. DMA_FROM_DEVICE);
  417. if (cmd->nsrq)
  418. ret = ib_post_srq_recv(cmd->nsrq->srq, &cmd->wr, NULL);
  419. else
  420. ret = ib_post_recv(cmd->queue->qp, &cmd->wr, NULL);
  421. if (unlikely(ret))
  422. pr_err("post_recv cmd failed\n");
  423. return ret;
  424. }
  425. static void nvmet_rdma_process_wr_wait_list(struct nvmet_rdma_queue *queue)
  426. {
  427. spin_lock(&queue->rsp_wr_wait_lock);
  428. while (!list_empty(&queue->rsp_wr_wait_list)) {
  429. struct nvmet_rdma_rsp *rsp;
  430. bool ret;
  431. rsp = list_entry(queue->rsp_wr_wait_list.next,
  432. struct nvmet_rdma_rsp, wait_list);
  433. list_del(&rsp->wait_list);
  434. spin_unlock(&queue->rsp_wr_wait_lock);
  435. ret = nvmet_rdma_execute_command(rsp);
  436. spin_lock(&queue->rsp_wr_wait_lock);
  437. if (!ret) {
  438. list_add(&rsp->wait_list, &queue->rsp_wr_wait_list);
  439. break;
  440. }
  441. }
  442. spin_unlock(&queue->rsp_wr_wait_lock);
  443. }
  444. static u16 nvmet_rdma_check_pi_status(struct ib_mr *sig_mr)
  445. {
  446. struct ib_mr_status mr_status;
  447. int ret;
  448. u16 status = 0;
  449. ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
  450. if (ret) {
  451. pr_err("ib_check_mr_status failed, ret %d\n", ret);
  452. return NVME_SC_INVALID_PI;
  453. }
  454. if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
  455. switch (mr_status.sig_err.err_type) {
  456. case IB_SIG_BAD_GUARD:
  457. status = NVME_SC_GUARD_CHECK;
  458. break;
  459. case IB_SIG_BAD_REFTAG:
  460. status = NVME_SC_REFTAG_CHECK;
  461. break;
  462. case IB_SIG_BAD_APPTAG:
  463. status = NVME_SC_APPTAG_CHECK;
  464. break;
  465. }
  466. pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n",
  467. mr_status.sig_err.err_type,
  468. mr_status.sig_err.expected,
  469. mr_status.sig_err.actual);
  470. }
  471. return status;
  472. }
  473. static void nvmet_rdma_set_sig_domain(struct blk_integrity *bi,
  474. struct nvme_command *cmd, struct ib_sig_domain *domain,
  475. u16 control, u8 pi_type)
  476. {
  477. domain->sig_type = IB_SIG_TYPE_T10_DIF;
  478. domain->sig.dif.bg_type = IB_T10DIF_CRC;
  479. domain->sig.dif.pi_interval = 1 << bi->interval_exp;
  480. domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag);
  481. if (control & NVME_RW_PRINFO_PRCHK_REF)
  482. domain->sig.dif.ref_remap = true;
  483. domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.apptag);
  484. domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.appmask);
  485. domain->sig.dif.app_escape = true;
  486. if (pi_type == NVME_NS_DPS_PI_TYPE3)
  487. domain->sig.dif.ref_escape = true;
  488. }
  489. static void nvmet_rdma_set_sig_attrs(struct nvmet_req *req,
  490. struct ib_sig_attrs *sig_attrs)
  491. {
  492. struct nvme_command *cmd = req->cmd;
  493. u16 control = le16_to_cpu(cmd->rw.control);
  494. u8 pi_type = req->ns->pi_type;
  495. struct blk_integrity *bi;
  496. bi = bdev_get_integrity(req->ns->bdev);
  497. memset(sig_attrs, 0, sizeof(*sig_attrs));
  498. if (control & NVME_RW_PRINFO_PRACT) {
  499. /* for WRITE_INSERT/READ_STRIP no wire domain */
  500. sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
  501. nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
  502. pi_type);
  503. /* Clear the PRACT bit since HCA will generate/verify the PI */
  504. control &= ~NVME_RW_PRINFO_PRACT;
  505. cmd->rw.control = cpu_to_le16(control);
  506. /* PI is added by the HW */
  507. req->transfer_len += req->metadata_len;
  508. } else {
  509. /* for WRITE_PASS/READ_PASS both wire/memory domains exist */
  510. nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
  511. pi_type);
  512. nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
  513. pi_type);
  514. }
  515. if (control & NVME_RW_PRINFO_PRCHK_REF)
  516. sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG;
  517. if (control & NVME_RW_PRINFO_PRCHK_GUARD)
  518. sig_attrs->check_mask |= IB_SIG_CHECK_GUARD;
  519. if (control & NVME_RW_PRINFO_PRCHK_APP)
  520. sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG;
  521. }
  522. static int nvmet_rdma_rw_ctx_init(struct nvmet_rdma_rsp *rsp, u64 addr, u32 key,
  523. struct ib_sig_attrs *sig_attrs)
  524. {
  525. struct rdma_cm_id *cm_id = rsp->queue->cm_id;
  526. struct nvmet_req *req = &rsp->req;
  527. int ret;
  528. if (req->metadata_len)
  529. ret = rdma_rw_ctx_signature_init(&rsp->rw, cm_id->qp,
  530. cm_id->port_num, req->sg, req->sg_cnt,
  531. req->metadata_sg, req->metadata_sg_cnt, sig_attrs,
  532. addr, key, nvmet_data_dir(req));
  533. else
  534. ret = rdma_rw_ctx_init(&rsp->rw, cm_id->qp, cm_id->port_num,
  535. req->sg, req->sg_cnt, 0, addr, key,
  536. nvmet_data_dir(req));
  537. return ret;
  538. }
  539. static void nvmet_rdma_rw_ctx_destroy(struct nvmet_rdma_rsp *rsp)
  540. {
  541. struct rdma_cm_id *cm_id = rsp->queue->cm_id;
  542. struct nvmet_req *req = &rsp->req;
  543. if (req->metadata_len)
  544. rdma_rw_ctx_destroy_signature(&rsp->rw, cm_id->qp,
  545. cm_id->port_num, req->sg, req->sg_cnt,
  546. req->metadata_sg, req->metadata_sg_cnt,
  547. nvmet_data_dir(req));
  548. else
  549. rdma_rw_ctx_destroy(&rsp->rw, cm_id->qp, cm_id->port_num,
  550. req->sg, req->sg_cnt, nvmet_data_dir(req));
  551. }
  552. static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp)
  553. {
  554. struct nvmet_rdma_queue *queue = rsp->queue;
  555. atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail);
  556. if (rsp->n_rdma)
  557. nvmet_rdma_rw_ctx_destroy(rsp);
  558. if (rsp->req.sg != rsp->cmd->inline_sg)
  559. nvmet_req_free_sgls(&rsp->req);
  560. if (unlikely(!list_empty_careful(&queue->rsp_wr_wait_list)))
  561. nvmet_rdma_process_wr_wait_list(queue);
  562. nvmet_rdma_put_rsp(rsp);
  563. }
  564. static void nvmet_rdma_error_comp(struct nvmet_rdma_queue *queue)
  565. {
  566. if (queue->nvme_sq.ctrl) {
  567. nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl);
  568. } else {
  569. /*
  570. * we didn't setup the controller yet in case
  571. * of admin connect error, just disconnect and
  572. * cleanup the queue
  573. */
  574. nvmet_rdma_queue_disconnect(queue);
  575. }
  576. }
  577. static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
  578. {
  579. struct nvmet_rdma_rsp *rsp =
  580. container_of(wc->wr_cqe, struct nvmet_rdma_rsp, send_cqe);
  581. struct nvmet_rdma_queue *queue = wc->qp->qp_context;
  582. nvmet_rdma_release_rsp(rsp);
  583. if (unlikely(wc->status != IB_WC_SUCCESS &&
  584. wc->status != IB_WC_WR_FLUSH_ERR)) {
  585. pr_err("SEND for CQE 0x%p failed with status %s (%d).\n",
  586. wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status);
  587. nvmet_rdma_error_comp(queue);
  588. }
  589. }
  590. static void nvmet_rdma_queue_response(struct nvmet_req *req)
  591. {
  592. struct nvmet_rdma_rsp *rsp =
  593. container_of(req, struct nvmet_rdma_rsp, req);
  594. struct rdma_cm_id *cm_id = rsp->queue->cm_id;
  595. struct ib_send_wr *first_wr;
  596. if (rsp->flags & NVMET_RDMA_REQ_INVALIDATE_RKEY) {
  597. rsp->send_wr.opcode = IB_WR_SEND_WITH_INV;
  598. rsp->send_wr.ex.invalidate_rkey = rsp->invalidate_rkey;
  599. } else {
  600. rsp->send_wr.opcode = IB_WR_SEND;
  601. }
  602. if (nvmet_rdma_need_data_out(rsp)) {
  603. if (rsp->req.metadata_len)
  604. first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp,
  605. cm_id->port_num, &rsp->write_cqe, NULL);
  606. else
  607. first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp,
  608. cm_id->port_num, NULL, &rsp->send_wr);
  609. } else {
  610. first_wr = &rsp->send_wr;
  611. }
  612. nvmet_rdma_post_recv(rsp->queue->dev, rsp->cmd);
  613. ib_dma_sync_single_for_device(rsp->queue->dev->device,
  614. rsp->send_sge.addr, rsp->send_sge.length,
  615. DMA_TO_DEVICE);
  616. if (unlikely(ib_post_send(cm_id->qp, first_wr, NULL))) {
  617. pr_err("sending cmd response failed\n");
  618. nvmet_rdma_release_rsp(rsp);
  619. }
  620. }
  621. static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc)
  622. {
  623. struct nvmet_rdma_rsp *rsp =
  624. container_of(wc->wr_cqe, struct nvmet_rdma_rsp, read_cqe);
  625. struct nvmet_rdma_queue *queue = wc->qp->qp_context;
  626. u16 status = 0;
  627. WARN_ON(rsp->n_rdma <= 0);
  628. atomic_add(rsp->n_rdma, &queue->sq_wr_avail);
  629. rsp->n_rdma = 0;
  630. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  631. nvmet_rdma_rw_ctx_destroy(rsp);
  632. nvmet_req_uninit(&rsp->req);
  633. nvmet_rdma_release_rsp(rsp);
  634. if (wc->status != IB_WC_WR_FLUSH_ERR) {
  635. pr_info("RDMA READ for CQE 0x%p failed with status %s (%d).\n",
  636. wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status);
  637. nvmet_rdma_error_comp(queue);
  638. }
  639. return;
  640. }
  641. if (rsp->req.metadata_len)
  642. status = nvmet_rdma_check_pi_status(rsp->rw.reg->mr);
  643. nvmet_rdma_rw_ctx_destroy(rsp);
  644. if (unlikely(status))
  645. nvmet_req_complete(&rsp->req, status);
  646. else
  647. rsp->req.execute(&rsp->req);
  648. }
  649. static void nvmet_rdma_write_data_done(struct ib_cq *cq, struct ib_wc *wc)
  650. {
  651. struct nvmet_rdma_rsp *rsp =
  652. container_of(wc->wr_cqe, struct nvmet_rdma_rsp, write_cqe);
  653. struct nvmet_rdma_queue *queue = wc->qp->qp_context;
  654. struct rdma_cm_id *cm_id = rsp->queue->cm_id;
  655. u16 status;
  656. if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
  657. return;
  658. WARN_ON(rsp->n_rdma <= 0);
  659. atomic_add(rsp->n_rdma, &queue->sq_wr_avail);
  660. rsp->n_rdma = 0;
  661. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  662. nvmet_rdma_rw_ctx_destroy(rsp);
  663. nvmet_req_uninit(&rsp->req);
  664. nvmet_rdma_release_rsp(rsp);
  665. if (wc->status != IB_WC_WR_FLUSH_ERR) {
  666. pr_info("RDMA WRITE for CQE failed with status %s (%d).\n",
  667. ib_wc_status_msg(wc->status), wc->status);
  668. nvmet_rdma_error_comp(queue);
  669. }
  670. return;
  671. }
  672. /*
  673. * Upon RDMA completion check the signature status
  674. * - if succeeded send good NVMe response
  675. * - if failed send bad NVMe response with appropriate error
  676. */
  677. status = nvmet_rdma_check_pi_status(rsp->rw.reg->mr);
  678. if (unlikely(status))
  679. rsp->req.cqe->status = cpu_to_le16(status << 1);
  680. nvmet_rdma_rw_ctx_destroy(rsp);
  681. if (unlikely(ib_post_send(cm_id->qp, &rsp->send_wr, NULL))) {
  682. pr_err("sending cmd response failed\n");
  683. nvmet_rdma_release_rsp(rsp);
  684. }
  685. }
  686. static void nvmet_rdma_use_inline_sg(struct nvmet_rdma_rsp *rsp, u32 len,
  687. u64 off)
  688. {
  689. int sg_count = num_pages(len);
  690. struct scatterlist *sg;
  691. int i;
  692. sg = rsp->cmd->inline_sg;
  693. for (i = 0; i < sg_count; i++, sg++) {
  694. if (i < sg_count - 1)
  695. sg_unmark_end(sg);
  696. else
  697. sg_mark_end(sg);
  698. sg->offset = off;
  699. sg->length = min_t(int, len, PAGE_SIZE - off);
  700. len -= sg->length;
  701. if (!i)
  702. off = 0;
  703. }
  704. rsp->req.sg = rsp->cmd->inline_sg;
  705. rsp->req.sg_cnt = sg_count;
  706. }
  707. static u16 nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp *rsp)
  708. {
  709. struct nvme_sgl_desc *sgl = &rsp->req.cmd->common.dptr.sgl;
  710. u64 off = le64_to_cpu(sgl->addr);
  711. u32 len = le32_to_cpu(sgl->length);
  712. if (!nvme_is_write(rsp->req.cmd)) {
  713. rsp->req.error_loc =
  714. offsetof(struct nvme_common_command, opcode);
  715. return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  716. }
  717. if (off + len > rsp->queue->dev->inline_data_size) {
  718. pr_err("invalid inline data offset!\n");
  719. return NVME_SC_SGL_INVALID_OFFSET | NVME_SC_DNR;
  720. }
  721. /* no data command? */
  722. if (!len)
  723. return 0;
  724. nvmet_rdma_use_inline_sg(rsp, len, off);
  725. rsp->flags |= NVMET_RDMA_REQ_INLINE_DATA;
  726. rsp->req.transfer_len += len;
  727. return 0;
  728. }
  729. static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp,
  730. struct nvme_keyed_sgl_desc *sgl, bool invalidate)
  731. {
  732. u64 addr = le64_to_cpu(sgl->addr);
  733. u32 key = get_unaligned_le32(sgl->key);
  734. struct ib_sig_attrs sig_attrs;
  735. int ret;
  736. rsp->req.transfer_len = get_unaligned_le24(sgl->length);
  737. /* no data command? */
  738. if (!rsp->req.transfer_len)
  739. return 0;
  740. if (rsp->req.metadata_len)
  741. nvmet_rdma_set_sig_attrs(&rsp->req, &sig_attrs);
  742. ret = nvmet_req_alloc_sgls(&rsp->req);
  743. if (unlikely(ret < 0))
  744. goto error_out;
  745. ret = nvmet_rdma_rw_ctx_init(rsp, addr, key, &sig_attrs);
  746. if (unlikely(ret < 0))
  747. goto error_out;
  748. rsp->n_rdma += ret;
  749. if (invalidate) {
  750. rsp->invalidate_rkey = key;
  751. rsp->flags |= NVMET_RDMA_REQ_INVALIDATE_RKEY;
  752. }
  753. return 0;
  754. error_out:
  755. rsp->req.transfer_len = 0;
  756. return NVME_SC_INTERNAL;
  757. }
  758. static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp)
  759. {
  760. struct nvme_keyed_sgl_desc *sgl = &rsp->req.cmd->common.dptr.ksgl;
  761. switch (sgl->type >> 4) {
  762. case NVME_SGL_FMT_DATA_DESC:
  763. switch (sgl->type & 0xf) {
  764. case NVME_SGL_FMT_OFFSET:
  765. return nvmet_rdma_map_sgl_inline(rsp);
  766. default:
  767. pr_err("invalid SGL subtype: %#x\n", sgl->type);
  768. rsp->req.error_loc =
  769. offsetof(struct nvme_common_command, dptr);
  770. return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  771. }
  772. case NVME_KEY_SGL_FMT_DATA_DESC:
  773. switch (sgl->type & 0xf) {
  774. case NVME_SGL_FMT_ADDRESS | NVME_SGL_FMT_INVALIDATE:
  775. return nvmet_rdma_map_sgl_keyed(rsp, sgl, true);
  776. case NVME_SGL_FMT_ADDRESS:
  777. return nvmet_rdma_map_sgl_keyed(rsp, sgl, false);
  778. default:
  779. pr_err("invalid SGL subtype: %#x\n", sgl->type);
  780. rsp->req.error_loc =
  781. offsetof(struct nvme_common_command, dptr);
  782. return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  783. }
  784. default:
  785. pr_err("invalid SGL type: %#x\n", sgl->type);
  786. rsp->req.error_loc = offsetof(struct nvme_common_command, dptr);
  787. return NVME_SC_SGL_INVALID_TYPE | NVME_SC_DNR;
  788. }
  789. }
  790. static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp)
  791. {
  792. struct nvmet_rdma_queue *queue = rsp->queue;
  793. if (unlikely(atomic_sub_return(1 + rsp->n_rdma,
  794. &queue->sq_wr_avail) < 0)) {
  795. pr_debug("IB send queue full (needed %d): queue %u cntlid %u\n",
  796. 1 + rsp->n_rdma, queue->idx,
  797. queue->nvme_sq.ctrl->cntlid);
  798. atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail);
  799. return false;
  800. }
  801. if (nvmet_rdma_need_data_in(rsp)) {
  802. if (rdma_rw_ctx_post(&rsp->rw, queue->qp,
  803. queue->cm_id->port_num, &rsp->read_cqe, NULL))
  804. nvmet_req_complete(&rsp->req, NVME_SC_DATA_XFER_ERROR);
  805. } else {
  806. rsp->req.execute(&rsp->req);
  807. }
  808. return true;
  809. }
  810. static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue,
  811. struct nvmet_rdma_rsp *cmd)
  812. {
  813. u16 status;
  814. ib_dma_sync_single_for_cpu(queue->dev->device,
  815. cmd->cmd->sge[0].addr, cmd->cmd->sge[0].length,
  816. DMA_FROM_DEVICE);
  817. ib_dma_sync_single_for_cpu(queue->dev->device,
  818. cmd->send_sge.addr, cmd->send_sge.length,
  819. DMA_TO_DEVICE);
  820. if (!nvmet_req_init(&cmd->req, &queue->nvme_cq,
  821. &queue->nvme_sq, &nvmet_rdma_ops))
  822. return;
  823. status = nvmet_rdma_map_sgl(cmd);
  824. if (status)
  825. goto out_err;
  826. if (unlikely(!nvmet_rdma_execute_command(cmd))) {
  827. spin_lock(&queue->rsp_wr_wait_lock);
  828. list_add_tail(&cmd->wait_list, &queue->rsp_wr_wait_list);
  829. spin_unlock(&queue->rsp_wr_wait_lock);
  830. }
  831. return;
  832. out_err:
  833. nvmet_req_complete(&cmd->req, status);
  834. }
  835. static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
  836. {
  837. struct nvmet_rdma_cmd *cmd =
  838. container_of(wc->wr_cqe, struct nvmet_rdma_cmd, cqe);
  839. struct nvmet_rdma_queue *queue = wc->qp->qp_context;
  840. struct nvmet_rdma_rsp *rsp;
  841. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  842. if (wc->status != IB_WC_WR_FLUSH_ERR) {
  843. pr_err("RECV for CQE 0x%p failed with status %s (%d)\n",
  844. wc->wr_cqe, ib_wc_status_msg(wc->status),
  845. wc->status);
  846. nvmet_rdma_error_comp(queue);
  847. }
  848. return;
  849. }
  850. if (unlikely(wc->byte_len < sizeof(struct nvme_command))) {
  851. pr_err("Ctrl Fatal Error: capsule size less than 64 bytes\n");
  852. nvmet_rdma_error_comp(queue);
  853. return;
  854. }
  855. cmd->queue = queue;
  856. rsp = nvmet_rdma_get_rsp(queue);
  857. if (unlikely(!rsp)) {
  858. /*
  859. * we get here only under memory pressure,
  860. * silently drop and have the host retry
  861. * as we can't even fail it.
  862. */
  863. nvmet_rdma_post_recv(queue->dev, cmd);
  864. return;
  865. }
  866. rsp->queue = queue;
  867. rsp->cmd = cmd;
  868. rsp->flags = 0;
  869. rsp->req.cmd = cmd->nvme_cmd;
  870. rsp->req.port = queue->port;
  871. rsp->n_rdma = 0;
  872. if (unlikely(queue->state != NVMET_RDMA_Q_LIVE)) {
  873. unsigned long flags;
  874. spin_lock_irqsave(&queue->state_lock, flags);
  875. if (queue->state == NVMET_RDMA_Q_CONNECTING)
  876. list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
  877. else
  878. nvmet_rdma_put_rsp(rsp);
  879. spin_unlock_irqrestore(&queue->state_lock, flags);
  880. return;
  881. }
  882. nvmet_rdma_handle_command(queue, rsp);
  883. }
  884. static void nvmet_rdma_destroy_srq(struct nvmet_rdma_srq *nsrq)
  885. {
  886. nvmet_rdma_free_cmds(nsrq->ndev, nsrq->cmds, nsrq->ndev->srq_size,
  887. false);
  888. ib_destroy_srq(nsrq->srq);
  889. kfree(nsrq);
  890. }
  891. static void nvmet_rdma_destroy_srqs(struct nvmet_rdma_device *ndev)
  892. {
  893. int i;
  894. if (!ndev->srqs)
  895. return;
  896. for (i = 0; i < ndev->srq_count; i++)
  897. nvmet_rdma_destroy_srq(ndev->srqs[i]);
  898. kfree(ndev->srqs);
  899. }
  900. static struct nvmet_rdma_srq *
  901. nvmet_rdma_init_srq(struct nvmet_rdma_device *ndev)
  902. {
  903. struct ib_srq_init_attr srq_attr = { NULL, };
  904. size_t srq_size = ndev->srq_size;
  905. struct nvmet_rdma_srq *nsrq;
  906. struct ib_srq *srq;
  907. int ret, i;
  908. nsrq = kzalloc(sizeof(*nsrq), GFP_KERNEL);
  909. if (!nsrq)
  910. return ERR_PTR(-ENOMEM);
  911. srq_attr.attr.max_wr = srq_size;
  912. srq_attr.attr.max_sge = 1 + ndev->inline_page_count;
  913. srq_attr.attr.srq_limit = 0;
  914. srq_attr.srq_type = IB_SRQT_BASIC;
  915. srq = ib_create_srq(ndev->pd, &srq_attr);
  916. if (IS_ERR(srq)) {
  917. ret = PTR_ERR(srq);
  918. goto out_free;
  919. }
  920. nsrq->cmds = nvmet_rdma_alloc_cmds(ndev, srq_size, false);
  921. if (IS_ERR(nsrq->cmds)) {
  922. ret = PTR_ERR(nsrq->cmds);
  923. goto out_destroy_srq;
  924. }
  925. nsrq->srq = srq;
  926. nsrq->ndev = ndev;
  927. for (i = 0; i < srq_size; i++) {
  928. nsrq->cmds[i].nsrq = nsrq;
  929. ret = nvmet_rdma_post_recv(ndev, &nsrq->cmds[i]);
  930. if (ret)
  931. goto out_free_cmds;
  932. }
  933. return nsrq;
  934. out_free_cmds:
  935. nvmet_rdma_free_cmds(ndev, nsrq->cmds, srq_size, false);
  936. out_destroy_srq:
  937. ib_destroy_srq(srq);
  938. out_free:
  939. kfree(nsrq);
  940. return ERR_PTR(ret);
  941. }
  942. static int nvmet_rdma_init_srqs(struct nvmet_rdma_device *ndev)
  943. {
  944. int i, ret;
  945. if (!ndev->device->attrs.max_srq_wr || !ndev->device->attrs.max_srq) {
  946. /*
  947. * If SRQs aren't supported we just go ahead and use normal
  948. * non-shared receive queues.
  949. */
  950. pr_info("SRQ requested but not supported.\n");
  951. return 0;
  952. }
  953. ndev->srq_size = min(ndev->device->attrs.max_srq_wr,
  954. nvmet_rdma_srq_size);
  955. ndev->srq_count = min(ndev->device->num_comp_vectors,
  956. ndev->device->attrs.max_srq);
  957. ndev->srqs = kcalloc(ndev->srq_count, sizeof(*ndev->srqs), GFP_KERNEL);
  958. if (!ndev->srqs)
  959. return -ENOMEM;
  960. for (i = 0; i < ndev->srq_count; i++) {
  961. ndev->srqs[i] = nvmet_rdma_init_srq(ndev);
  962. if (IS_ERR(ndev->srqs[i])) {
  963. ret = PTR_ERR(ndev->srqs[i]);
  964. goto err_srq;
  965. }
  966. }
  967. return 0;
  968. err_srq:
  969. while (--i >= 0)
  970. nvmet_rdma_destroy_srq(ndev->srqs[i]);
  971. kfree(ndev->srqs);
  972. return ret;
  973. }
  974. static void nvmet_rdma_free_dev(struct kref *ref)
  975. {
  976. struct nvmet_rdma_device *ndev =
  977. container_of(ref, struct nvmet_rdma_device, ref);
  978. mutex_lock(&device_list_mutex);
  979. list_del(&ndev->entry);
  980. mutex_unlock(&device_list_mutex);
  981. nvmet_rdma_destroy_srqs(ndev);
  982. ib_dealloc_pd(ndev->pd);
  983. kfree(ndev);
  984. }
  985. static struct nvmet_rdma_device *
  986. nvmet_rdma_find_get_device(struct rdma_cm_id *cm_id)
  987. {
  988. struct nvmet_rdma_port *port = cm_id->context;
  989. struct nvmet_port *nport = port->nport;
  990. struct nvmet_rdma_device *ndev;
  991. int inline_page_count;
  992. int inline_sge_count;
  993. int ret;
  994. mutex_lock(&device_list_mutex);
  995. list_for_each_entry(ndev, &device_list, entry) {
  996. if (ndev->device->node_guid == cm_id->device->node_guid &&
  997. kref_get_unless_zero(&ndev->ref))
  998. goto out_unlock;
  999. }
  1000. ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
  1001. if (!ndev)
  1002. goto out_err;
  1003. inline_page_count = num_pages(nport->inline_data_size);
  1004. inline_sge_count = max(cm_id->device->attrs.max_sge_rd,
  1005. cm_id->device->attrs.max_recv_sge) - 1;
  1006. if (inline_page_count > inline_sge_count) {
  1007. pr_warn("inline_data_size %d cannot be supported by device %s. Reducing to %lu.\n",
  1008. nport->inline_data_size, cm_id->device->name,
  1009. inline_sge_count * PAGE_SIZE);
  1010. nport->inline_data_size = inline_sge_count * PAGE_SIZE;
  1011. inline_page_count = inline_sge_count;
  1012. }
  1013. ndev->inline_data_size = nport->inline_data_size;
  1014. ndev->inline_page_count = inline_page_count;
  1015. if (nport->pi_enable && !(cm_id->device->attrs.kernel_cap_flags &
  1016. IBK_INTEGRITY_HANDOVER)) {
  1017. pr_warn("T10-PI is not supported by device %s. Disabling it\n",
  1018. cm_id->device->name);
  1019. nport->pi_enable = false;
  1020. }
  1021. ndev->device = cm_id->device;
  1022. kref_init(&ndev->ref);
  1023. ndev->pd = ib_alloc_pd(ndev->device, 0);
  1024. if (IS_ERR(ndev->pd))
  1025. goto out_free_dev;
  1026. if (nvmet_rdma_use_srq) {
  1027. ret = nvmet_rdma_init_srqs(ndev);
  1028. if (ret)
  1029. goto out_free_pd;
  1030. }
  1031. list_add(&ndev->entry, &device_list);
  1032. out_unlock:
  1033. mutex_unlock(&device_list_mutex);
  1034. pr_debug("added %s.\n", ndev->device->name);
  1035. return ndev;
  1036. out_free_pd:
  1037. ib_dealloc_pd(ndev->pd);
  1038. out_free_dev:
  1039. kfree(ndev);
  1040. out_err:
  1041. mutex_unlock(&device_list_mutex);
  1042. return NULL;
  1043. }
  1044. static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue *queue)
  1045. {
  1046. struct ib_qp_init_attr qp_attr = { };
  1047. struct nvmet_rdma_device *ndev = queue->dev;
  1048. int nr_cqe, ret, i, factor;
  1049. /*
  1050. * Reserve CQ slots for RECV + RDMA_READ/RDMA_WRITE + RDMA_SEND.
  1051. */
  1052. nr_cqe = queue->recv_queue_size + 2 * queue->send_queue_size;
  1053. queue->cq = ib_cq_pool_get(ndev->device, nr_cqe + 1,
  1054. queue->comp_vector, IB_POLL_WORKQUEUE);
  1055. if (IS_ERR(queue->cq)) {
  1056. ret = PTR_ERR(queue->cq);
  1057. pr_err("failed to create CQ cqe= %d ret= %d\n",
  1058. nr_cqe + 1, ret);
  1059. goto out;
  1060. }
  1061. qp_attr.qp_context = queue;
  1062. qp_attr.event_handler = nvmet_rdma_qp_event;
  1063. qp_attr.send_cq = queue->cq;
  1064. qp_attr.recv_cq = queue->cq;
  1065. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  1066. qp_attr.qp_type = IB_QPT_RC;
  1067. /* +1 for drain */
  1068. qp_attr.cap.max_send_wr = queue->send_queue_size + 1;
  1069. factor = rdma_rw_mr_factor(ndev->device, queue->cm_id->port_num,
  1070. 1 << NVMET_RDMA_MAX_MDTS);
  1071. qp_attr.cap.max_rdma_ctxs = queue->send_queue_size * factor;
  1072. qp_attr.cap.max_send_sge = max(ndev->device->attrs.max_sge_rd,
  1073. ndev->device->attrs.max_send_sge);
  1074. if (queue->nsrq) {
  1075. qp_attr.srq = queue->nsrq->srq;
  1076. } else {
  1077. /* +1 for drain */
  1078. qp_attr.cap.max_recv_wr = 1 + queue->recv_queue_size;
  1079. qp_attr.cap.max_recv_sge = 1 + ndev->inline_page_count;
  1080. }
  1081. if (queue->port->pi_enable && queue->host_qid)
  1082. qp_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
  1083. ret = rdma_create_qp(queue->cm_id, ndev->pd, &qp_attr);
  1084. if (ret) {
  1085. pr_err("failed to create_qp ret= %d\n", ret);
  1086. goto err_destroy_cq;
  1087. }
  1088. queue->qp = queue->cm_id->qp;
  1089. atomic_set(&queue->sq_wr_avail, qp_attr.cap.max_send_wr);
  1090. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
  1091. __func__, queue->cq->cqe, qp_attr.cap.max_send_sge,
  1092. qp_attr.cap.max_send_wr, queue->cm_id);
  1093. if (!queue->nsrq) {
  1094. for (i = 0; i < queue->recv_queue_size; i++) {
  1095. queue->cmds[i].queue = queue;
  1096. ret = nvmet_rdma_post_recv(ndev, &queue->cmds[i]);
  1097. if (ret)
  1098. goto err_destroy_qp;
  1099. }
  1100. }
  1101. out:
  1102. return ret;
  1103. err_destroy_qp:
  1104. rdma_destroy_qp(queue->cm_id);
  1105. err_destroy_cq:
  1106. ib_cq_pool_put(queue->cq, nr_cqe + 1);
  1107. goto out;
  1108. }
  1109. static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue *queue)
  1110. {
  1111. ib_drain_qp(queue->qp);
  1112. if (queue->cm_id)
  1113. rdma_destroy_id(queue->cm_id);
  1114. ib_destroy_qp(queue->qp);
  1115. ib_cq_pool_put(queue->cq, queue->recv_queue_size + 2 *
  1116. queue->send_queue_size + 1);
  1117. }
  1118. static void nvmet_rdma_free_queue(struct nvmet_rdma_queue *queue)
  1119. {
  1120. pr_debug("freeing queue %d\n", queue->idx);
  1121. nvmet_sq_destroy(&queue->nvme_sq);
  1122. nvmet_rdma_destroy_queue_ib(queue);
  1123. if (!queue->nsrq) {
  1124. nvmet_rdma_free_cmds(queue->dev, queue->cmds,
  1125. queue->recv_queue_size,
  1126. !queue->host_qid);
  1127. }
  1128. nvmet_rdma_free_rsps(queue);
  1129. ida_free(&nvmet_rdma_queue_ida, queue->idx);
  1130. kfree(queue);
  1131. }
  1132. static void nvmet_rdma_release_queue_work(struct work_struct *w)
  1133. {
  1134. struct nvmet_rdma_queue *queue =
  1135. container_of(w, struct nvmet_rdma_queue, release_work);
  1136. struct nvmet_rdma_device *dev = queue->dev;
  1137. nvmet_rdma_free_queue(queue);
  1138. kref_put(&dev->ref, nvmet_rdma_free_dev);
  1139. }
  1140. static int
  1141. nvmet_rdma_parse_cm_connect_req(struct rdma_conn_param *conn,
  1142. struct nvmet_rdma_queue *queue)
  1143. {
  1144. struct nvme_rdma_cm_req *req;
  1145. req = (struct nvme_rdma_cm_req *)conn->private_data;
  1146. if (!req || conn->private_data_len == 0)
  1147. return NVME_RDMA_CM_INVALID_LEN;
  1148. if (le16_to_cpu(req->recfmt) != NVME_RDMA_CM_FMT_1_0)
  1149. return NVME_RDMA_CM_INVALID_RECFMT;
  1150. queue->host_qid = le16_to_cpu(req->qid);
  1151. /*
  1152. * req->hsqsize corresponds to our recv queue size plus 1
  1153. * req->hrqsize corresponds to our send queue size
  1154. */
  1155. queue->recv_queue_size = le16_to_cpu(req->hsqsize) + 1;
  1156. queue->send_queue_size = le16_to_cpu(req->hrqsize);
  1157. if (!queue->host_qid && queue->recv_queue_size > NVME_AQ_DEPTH)
  1158. return NVME_RDMA_CM_INVALID_HSQSIZE;
  1159. /* XXX: Should we enforce some kind of max for IO queues? */
  1160. return 0;
  1161. }
  1162. static int nvmet_rdma_cm_reject(struct rdma_cm_id *cm_id,
  1163. enum nvme_rdma_cm_status status)
  1164. {
  1165. struct nvme_rdma_cm_rej rej;
  1166. pr_debug("rejecting connect request: status %d (%s)\n",
  1167. status, nvme_rdma_cm_msg(status));
  1168. rej.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
  1169. rej.sts = cpu_to_le16(status);
  1170. return rdma_reject(cm_id, (void *)&rej, sizeof(rej),
  1171. IB_CM_REJ_CONSUMER_DEFINED);
  1172. }
  1173. static struct nvmet_rdma_queue *
  1174. nvmet_rdma_alloc_queue(struct nvmet_rdma_device *ndev,
  1175. struct rdma_cm_id *cm_id,
  1176. struct rdma_cm_event *event)
  1177. {
  1178. struct nvmet_rdma_port *port = cm_id->context;
  1179. struct nvmet_rdma_queue *queue;
  1180. int ret;
  1181. queue = kzalloc(sizeof(*queue), GFP_KERNEL);
  1182. if (!queue) {
  1183. ret = NVME_RDMA_CM_NO_RSC;
  1184. goto out_reject;
  1185. }
  1186. ret = nvmet_sq_init(&queue->nvme_sq);
  1187. if (ret) {
  1188. ret = NVME_RDMA_CM_NO_RSC;
  1189. goto out_free_queue;
  1190. }
  1191. ret = nvmet_rdma_parse_cm_connect_req(&event->param.conn, queue);
  1192. if (ret)
  1193. goto out_destroy_sq;
  1194. /*
  1195. * Schedules the actual release because calling rdma_destroy_id from
  1196. * inside a CM callback would trigger a deadlock. (great API design..)
  1197. */
  1198. INIT_WORK(&queue->release_work, nvmet_rdma_release_queue_work);
  1199. queue->dev = ndev;
  1200. queue->cm_id = cm_id;
  1201. queue->port = port->nport;
  1202. spin_lock_init(&queue->state_lock);
  1203. queue->state = NVMET_RDMA_Q_CONNECTING;
  1204. INIT_LIST_HEAD(&queue->rsp_wait_list);
  1205. INIT_LIST_HEAD(&queue->rsp_wr_wait_list);
  1206. spin_lock_init(&queue->rsp_wr_wait_lock);
  1207. INIT_LIST_HEAD(&queue->free_rsps);
  1208. spin_lock_init(&queue->rsps_lock);
  1209. INIT_LIST_HEAD(&queue->queue_list);
  1210. queue->idx = ida_alloc(&nvmet_rdma_queue_ida, GFP_KERNEL);
  1211. if (queue->idx < 0) {
  1212. ret = NVME_RDMA_CM_NO_RSC;
  1213. goto out_destroy_sq;
  1214. }
  1215. /*
  1216. * Spread the io queues across completion vectors,
  1217. * but still keep all admin queues on vector 0.
  1218. */
  1219. queue->comp_vector = !queue->host_qid ? 0 :
  1220. queue->idx % ndev->device->num_comp_vectors;
  1221. ret = nvmet_rdma_alloc_rsps(queue);
  1222. if (ret) {
  1223. ret = NVME_RDMA_CM_NO_RSC;
  1224. goto out_ida_remove;
  1225. }
  1226. if (ndev->srqs) {
  1227. queue->nsrq = ndev->srqs[queue->comp_vector % ndev->srq_count];
  1228. } else {
  1229. queue->cmds = nvmet_rdma_alloc_cmds(ndev,
  1230. queue->recv_queue_size,
  1231. !queue->host_qid);
  1232. if (IS_ERR(queue->cmds)) {
  1233. ret = NVME_RDMA_CM_NO_RSC;
  1234. goto out_free_responses;
  1235. }
  1236. }
  1237. ret = nvmet_rdma_create_queue_ib(queue);
  1238. if (ret) {
  1239. pr_err("%s: creating RDMA queue failed (%d).\n",
  1240. __func__, ret);
  1241. ret = NVME_RDMA_CM_NO_RSC;
  1242. goto out_free_cmds;
  1243. }
  1244. return queue;
  1245. out_free_cmds:
  1246. if (!queue->nsrq) {
  1247. nvmet_rdma_free_cmds(queue->dev, queue->cmds,
  1248. queue->recv_queue_size,
  1249. !queue->host_qid);
  1250. }
  1251. out_free_responses:
  1252. nvmet_rdma_free_rsps(queue);
  1253. out_ida_remove:
  1254. ida_free(&nvmet_rdma_queue_ida, queue->idx);
  1255. out_destroy_sq:
  1256. nvmet_sq_destroy(&queue->nvme_sq);
  1257. out_free_queue:
  1258. kfree(queue);
  1259. out_reject:
  1260. nvmet_rdma_cm_reject(cm_id, ret);
  1261. return NULL;
  1262. }
  1263. static void nvmet_rdma_qp_event(struct ib_event *event, void *priv)
  1264. {
  1265. struct nvmet_rdma_queue *queue = priv;
  1266. switch (event->event) {
  1267. case IB_EVENT_COMM_EST:
  1268. rdma_notify(queue->cm_id, event->event);
  1269. break;
  1270. case IB_EVENT_QP_LAST_WQE_REACHED:
  1271. pr_debug("received last WQE reached event for queue=0x%p\n",
  1272. queue);
  1273. break;
  1274. default:
  1275. pr_err("received IB QP event: %s (%d)\n",
  1276. ib_event_msg(event->event), event->event);
  1277. break;
  1278. }
  1279. }
  1280. static int nvmet_rdma_cm_accept(struct rdma_cm_id *cm_id,
  1281. struct nvmet_rdma_queue *queue,
  1282. struct rdma_conn_param *p)
  1283. {
  1284. struct rdma_conn_param param = { };
  1285. struct nvme_rdma_cm_rep priv = { };
  1286. int ret = -ENOMEM;
  1287. param.rnr_retry_count = 7;
  1288. param.flow_control = 1;
  1289. param.initiator_depth = min_t(u8, p->initiator_depth,
  1290. queue->dev->device->attrs.max_qp_init_rd_atom);
  1291. param.private_data = &priv;
  1292. param.private_data_len = sizeof(priv);
  1293. priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
  1294. priv.crqsize = cpu_to_le16(queue->recv_queue_size);
  1295. ret = rdma_accept(cm_id, &param);
  1296. if (ret)
  1297. pr_err("rdma_accept failed (error code = %d)\n", ret);
  1298. return ret;
  1299. }
  1300. static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id,
  1301. struct rdma_cm_event *event)
  1302. {
  1303. struct nvmet_rdma_device *ndev;
  1304. struct nvmet_rdma_queue *queue;
  1305. int ret = -EINVAL;
  1306. ndev = nvmet_rdma_find_get_device(cm_id);
  1307. if (!ndev) {
  1308. nvmet_rdma_cm_reject(cm_id, NVME_RDMA_CM_NO_RSC);
  1309. return -ECONNREFUSED;
  1310. }
  1311. queue = nvmet_rdma_alloc_queue(ndev, cm_id, event);
  1312. if (!queue) {
  1313. ret = -ENOMEM;
  1314. goto put_device;
  1315. }
  1316. if (queue->host_qid == 0) {
  1317. /* Let inflight controller teardown complete */
  1318. flush_workqueue(nvmet_wq);
  1319. }
  1320. ret = nvmet_rdma_cm_accept(cm_id, queue, &event->param.conn);
  1321. if (ret) {
  1322. /*
  1323. * Don't destroy the cm_id in free path, as we implicitly
  1324. * destroy the cm_id here with non-zero ret code.
  1325. */
  1326. queue->cm_id = NULL;
  1327. goto free_queue;
  1328. }
  1329. mutex_lock(&nvmet_rdma_queue_mutex);
  1330. list_add_tail(&queue->queue_list, &nvmet_rdma_queue_list);
  1331. mutex_unlock(&nvmet_rdma_queue_mutex);
  1332. return 0;
  1333. free_queue:
  1334. nvmet_rdma_free_queue(queue);
  1335. put_device:
  1336. kref_put(&ndev->ref, nvmet_rdma_free_dev);
  1337. return ret;
  1338. }
  1339. static void nvmet_rdma_queue_established(struct nvmet_rdma_queue *queue)
  1340. {
  1341. unsigned long flags;
  1342. spin_lock_irqsave(&queue->state_lock, flags);
  1343. if (queue->state != NVMET_RDMA_Q_CONNECTING) {
  1344. pr_warn("trying to establish a connected queue\n");
  1345. goto out_unlock;
  1346. }
  1347. queue->state = NVMET_RDMA_Q_LIVE;
  1348. while (!list_empty(&queue->rsp_wait_list)) {
  1349. struct nvmet_rdma_rsp *cmd;
  1350. cmd = list_first_entry(&queue->rsp_wait_list,
  1351. struct nvmet_rdma_rsp, wait_list);
  1352. list_del(&cmd->wait_list);
  1353. spin_unlock_irqrestore(&queue->state_lock, flags);
  1354. nvmet_rdma_handle_command(queue, cmd);
  1355. spin_lock_irqsave(&queue->state_lock, flags);
  1356. }
  1357. out_unlock:
  1358. spin_unlock_irqrestore(&queue->state_lock, flags);
  1359. }
  1360. static void __nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue)
  1361. {
  1362. bool disconnect = false;
  1363. unsigned long flags;
  1364. pr_debug("cm_id= %p queue->state= %d\n", queue->cm_id, queue->state);
  1365. spin_lock_irqsave(&queue->state_lock, flags);
  1366. switch (queue->state) {
  1367. case NVMET_RDMA_Q_CONNECTING:
  1368. while (!list_empty(&queue->rsp_wait_list)) {
  1369. struct nvmet_rdma_rsp *rsp;
  1370. rsp = list_first_entry(&queue->rsp_wait_list,
  1371. struct nvmet_rdma_rsp,
  1372. wait_list);
  1373. list_del(&rsp->wait_list);
  1374. nvmet_rdma_put_rsp(rsp);
  1375. }
  1376. fallthrough;
  1377. case NVMET_RDMA_Q_LIVE:
  1378. queue->state = NVMET_RDMA_Q_DISCONNECTING;
  1379. disconnect = true;
  1380. break;
  1381. case NVMET_RDMA_Q_DISCONNECTING:
  1382. break;
  1383. }
  1384. spin_unlock_irqrestore(&queue->state_lock, flags);
  1385. if (disconnect) {
  1386. rdma_disconnect(queue->cm_id);
  1387. queue_work(nvmet_wq, &queue->release_work);
  1388. }
  1389. }
  1390. static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue)
  1391. {
  1392. bool disconnect = false;
  1393. mutex_lock(&nvmet_rdma_queue_mutex);
  1394. if (!list_empty(&queue->queue_list)) {
  1395. list_del_init(&queue->queue_list);
  1396. disconnect = true;
  1397. }
  1398. mutex_unlock(&nvmet_rdma_queue_mutex);
  1399. if (disconnect)
  1400. __nvmet_rdma_queue_disconnect(queue);
  1401. }
  1402. static void nvmet_rdma_queue_connect_fail(struct rdma_cm_id *cm_id,
  1403. struct nvmet_rdma_queue *queue)
  1404. {
  1405. WARN_ON_ONCE(queue->state != NVMET_RDMA_Q_CONNECTING);
  1406. mutex_lock(&nvmet_rdma_queue_mutex);
  1407. if (!list_empty(&queue->queue_list))
  1408. list_del_init(&queue->queue_list);
  1409. mutex_unlock(&nvmet_rdma_queue_mutex);
  1410. pr_err("failed to connect queue %d\n", queue->idx);
  1411. queue_work(nvmet_wq, &queue->release_work);
  1412. }
  1413. /**
  1414. * nvmet_rdma_device_removal() - Handle RDMA device removal
  1415. * @cm_id: rdma_cm id, used for nvmet port
  1416. * @queue: nvmet rdma queue (cm id qp_context)
  1417. *
  1418. * DEVICE_REMOVAL event notifies us that the RDMA device is about
  1419. * to unplug. Note that this event can be generated on a normal
  1420. * queue cm_id and/or a device bound listener cm_id (where in this
  1421. * case queue will be null).
  1422. *
  1423. * We registered an ib_client to handle device removal for queues,
  1424. * so we only need to handle the listening port cm_ids. In this case
  1425. * we nullify the priv to prevent double cm_id destruction and destroying
  1426. * the cm_id implicitely by returning a non-zero rc to the callout.
  1427. */
  1428. static int nvmet_rdma_device_removal(struct rdma_cm_id *cm_id,
  1429. struct nvmet_rdma_queue *queue)
  1430. {
  1431. struct nvmet_rdma_port *port;
  1432. if (queue) {
  1433. /*
  1434. * This is a queue cm_id. we have registered
  1435. * an ib_client to handle queues removal
  1436. * so don't interfear and just return.
  1437. */
  1438. return 0;
  1439. }
  1440. port = cm_id->context;
  1441. /*
  1442. * This is a listener cm_id. Make sure that
  1443. * future remove_port won't invoke a double
  1444. * cm_id destroy. use atomic xchg to make sure
  1445. * we don't compete with remove_port.
  1446. */
  1447. if (xchg(&port->cm_id, NULL) != cm_id)
  1448. return 0;
  1449. /*
  1450. * We need to return 1 so that the core will destroy
  1451. * it's own ID. What a great API design..
  1452. */
  1453. return 1;
  1454. }
  1455. static int nvmet_rdma_cm_handler(struct rdma_cm_id *cm_id,
  1456. struct rdma_cm_event *event)
  1457. {
  1458. struct nvmet_rdma_queue *queue = NULL;
  1459. int ret = 0;
  1460. if (cm_id->qp)
  1461. queue = cm_id->qp->qp_context;
  1462. pr_debug("%s (%d): status %d id %p\n",
  1463. rdma_event_msg(event->event), event->event,
  1464. event->status, cm_id);
  1465. switch (event->event) {
  1466. case RDMA_CM_EVENT_CONNECT_REQUEST:
  1467. ret = nvmet_rdma_queue_connect(cm_id, event);
  1468. break;
  1469. case RDMA_CM_EVENT_ESTABLISHED:
  1470. nvmet_rdma_queue_established(queue);
  1471. break;
  1472. case RDMA_CM_EVENT_ADDR_CHANGE:
  1473. if (!queue) {
  1474. struct nvmet_rdma_port *port = cm_id->context;
  1475. queue_delayed_work(nvmet_wq, &port->repair_work, 0);
  1476. break;
  1477. }
  1478. fallthrough;
  1479. case RDMA_CM_EVENT_DISCONNECTED:
  1480. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  1481. nvmet_rdma_queue_disconnect(queue);
  1482. break;
  1483. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  1484. ret = nvmet_rdma_device_removal(cm_id, queue);
  1485. break;
  1486. case RDMA_CM_EVENT_REJECTED:
  1487. pr_debug("Connection rejected: %s\n",
  1488. rdma_reject_msg(cm_id, event->status));
  1489. fallthrough;
  1490. case RDMA_CM_EVENT_UNREACHABLE:
  1491. case RDMA_CM_EVENT_CONNECT_ERROR:
  1492. nvmet_rdma_queue_connect_fail(cm_id, queue);
  1493. break;
  1494. default:
  1495. pr_err("received unrecognized RDMA CM event %d\n",
  1496. event->event);
  1497. break;
  1498. }
  1499. return ret;
  1500. }
  1501. static void nvmet_rdma_delete_ctrl(struct nvmet_ctrl *ctrl)
  1502. {
  1503. struct nvmet_rdma_queue *queue;
  1504. restart:
  1505. mutex_lock(&nvmet_rdma_queue_mutex);
  1506. list_for_each_entry(queue, &nvmet_rdma_queue_list, queue_list) {
  1507. if (queue->nvme_sq.ctrl == ctrl) {
  1508. list_del_init(&queue->queue_list);
  1509. mutex_unlock(&nvmet_rdma_queue_mutex);
  1510. __nvmet_rdma_queue_disconnect(queue);
  1511. goto restart;
  1512. }
  1513. }
  1514. mutex_unlock(&nvmet_rdma_queue_mutex);
  1515. }
  1516. static void nvmet_rdma_destroy_port_queues(struct nvmet_rdma_port *port)
  1517. {
  1518. struct nvmet_rdma_queue *queue, *tmp;
  1519. struct nvmet_port *nport = port->nport;
  1520. mutex_lock(&nvmet_rdma_queue_mutex);
  1521. list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list,
  1522. queue_list) {
  1523. if (queue->port != nport)
  1524. continue;
  1525. list_del_init(&queue->queue_list);
  1526. __nvmet_rdma_queue_disconnect(queue);
  1527. }
  1528. mutex_unlock(&nvmet_rdma_queue_mutex);
  1529. }
  1530. static void nvmet_rdma_disable_port(struct nvmet_rdma_port *port)
  1531. {
  1532. struct rdma_cm_id *cm_id = xchg(&port->cm_id, NULL);
  1533. if (cm_id)
  1534. rdma_destroy_id(cm_id);
  1535. /*
  1536. * Destroy the remaining queues, which are not belong to any
  1537. * controller yet. Do it here after the RDMA-CM was destroyed
  1538. * guarantees that no new queue will be created.
  1539. */
  1540. nvmet_rdma_destroy_port_queues(port);
  1541. }
  1542. static int nvmet_rdma_enable_port(struct nvmet_rdma_port *port)
  1543. {
  1544. struct sockaddr *addr = (struct sockaddr *)&port->addr;
  1545. struct rdma_cm_id *cm_id;
  1546. int ret;
  1547. cm_id = rdma_create_id(&init_net, nvmet_rdma_cm_handler, port,
  1548. RDMA_PS_TCP, IB_QPT_RC);
  1549. if (IS_ERR(cm_id)) {
  1550. pr_err("CM ID creation failed\n");
  1551. return PTR_ERR(cm_id);
  1552. }
  1553. /*
  1554. * Allow both IPv4 and IPv6 sockets to bind a single port
  1555. * at the same time.
  1556. */
  1557. ret = rdma_set_afonly(cm_id, 1);
  1558. if (ret) {
  1559. pr_err("rdma_set_afonly failed (%d)\n", ret);
  1560. goto out_destroy_id;
  1561. }
  1562. ret = rdma_bind_addr(cm_id, addr);
  1563. if (ret) {
  1564. pr_err("binding CM ID to %pISpcs failed (%d)\n", addr, ret);
  1565. goto out_destroy_id;
  1566. }
  1567. ret = rdma_listen(cm_id, 128);
  1568. if (ret) {
  1569. pr_err("listening to %pISpcs failed (%d)\n", addr, ret);
  1570. goto out_destroy_id;
  1571. }
  1572. port->cm_id = cm_id;
  1573. return 0;
  1574. out_destroy_id:
  1575. rdma_destroy_id(cm_id);
  1576. return ret;
  1577. }
  1578. static void nvmet_rdma_repair_port_work(struct work_struct *w)
  1579. {
  1580. struct nvmet_rdma_port *port = container_of(to_delayed_work(w),
  1581. struct nvmet_rdma_port, repair_work);
  1582. int ret;
  1583. nvmet_rdma_disable_port(port);
  1584. ret = nvmet_rdma_enable_port(port);
  1585. if (ret)
  1586. queue_delayed_work(nvmet_wq, &port->repair_work, 5 * HZ);
  1587. }
  1588. static int nvmet_rdma_add_port(struct nvmet_port *nport)
  1589. {
  1590. struct nvmet_rdma_port *port;
  1591. __kernel_sa_family_t af;
  1592. int ret;
  1593. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1594. if (!port)
  1595. return -ENOMEM;
  1596. nport->priv = port;
  1597. port->nport = nport;
  1598. INIT_DELAYED_WORK(&port->repair_work, nvmet_rdma_repair_port_work);
  1599. switch (nport->disc_addr.adrfam) {
  1600. case NVMF_ADDR_FAMILY_IP4:
  1601. af = AF_INET;
  1602. break;
  1603. case NVMF_ADDR_FAMILY_IP6:
  1604. af = AF_INET6;
  1605. break;
  1606. default:
  1607. pr_err("address family %d not supported\n",
  1608. nport->disc_addr.adrfam);
  1609. ret = -EINVAL;
  1610. goto out_free_port;
  1611. }
  1612. if (nport->inline_data_size < 0) {
  1613. nport->inline_data_size = NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE;
  1614. } else if (nport->inline_data_size > NVMET_RDMA_MAX_INLINE_DATA_SIZE) {
  1615. pr_warn("inline_data_size %u is too large, reducing to %u\n",
  1616. nport->inline_data_size,
  1617. NVMET_RDMA_MAX_INLINE_DATA_SIZE);
  1618. nport->inline_data_size = NVMET_RDMA_MAX_INLINE_DATA_SIZE;
  1619. }
  1620. ret = inet_pton_with_scope(&init_net, af, nport->disc_addr.traddr,
  1621. nport->disc_addr.trsvcid, &port->addr);
  1622. if (ret) {
  1623. pr_err("malformed ip/port passed: %s:%s\n",
  1624. nport->disc_addr.traddr, nport->disc_addr.trsvcid);
  1625. goto out_free_port;
  1626. }
  1627. ret = nvmet_rdma_enable_port(port);
  1628. if (ret)
  1629. goto out_free_port;
  1630. pr_info("enabling port %d (%pISpcs)\n",
  1631. le16_to_cpu(nport->disc_addr.portid),
  1632. (struct sockaddr *)&port->addr);
  1633. return 0;
  1634. out_free_port:
  1635. kfree(port);
  1636. return ret;
  1637. }
  1638. static void nvmet_rdma_remove_port(struct nvmet_port *nport)
  1639. {
  1640. struct nvmet_rdma_port *port = nport->priv;
  1641. cancel_delayed_work_sync(&port->repair_work);
  1642. nvmet_rdma_disable_port(port);
  1643. kfree(port);
  1644. }
  1645. static void nvmet_rdma_disc_port_addr(struct nvmet_req *req,
  1646. struct nvmet_port *nport, char *traddr)
  1647. {
  1648. struct nvmet_rdma_port *port = nport->priv;
  1649. struct rdma_cm_id *cm_id = port->cm_id;
  1650. if (inet_addr_is_any((struct sockaddr *)&cm_id->route.addr.src_addr)) {
  1651. struct nvmet_rdma_rsp *rsp =
  1652. container_of(req, struct nvmet_rdma_rsp, req);
  1653. struct rdma_cm_id *req_cm_id = rsp->queue->cm_id;
  1654. struct sockaddr *addr = (void *)&req_cm_id->route.addr.src_addr;
  1655. sprintf(traddr, "%pISc", addr);
  1656. } else {
  1657. memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE);
  1658. }
  1659. }
  1660. static u8 nvmet_rdma_get_mdts(const struct nvmet_ctrl *ctrl)
  1661. {
  1662. if (ctrl->pi_support)
  1663. return NVMET_RDMA_MAX_METADATA_MDTS;
  1664. return NVMET_RDMA_MAX_MDTS;
  1665. }
  1666. static u16 nvmet_rdma_get_max_queue_size(const struct nvmet_ctrl *ctrl)
  1667. {
  1668. return NVME_RDMA_MAX_QUEUE_SIZE;
  1669. }
  1670. static const struct nvmet_fabrics_ops nvmet_rdma_ops = {
  1671. .owner = THIS_MODULE,
  1672. .type = NVMF_TRTYPE_RDMA,
  1673. .msdbd = 1,
  1674. .flags = NVMF_KEYED_SGLS | NVMF_METADATA_SUPPORTED,
  1675. .add_port = nvmet_rdma_add_port,
  1676. .remove_port = nvmet_rdma_remove_port,
  1677. .queue_response = nvmet_rdma_queue_response,
  1678. .delete_ctrl = nvmet_rdma_delete_ctrl,
  1679. .disc_traddr = nvmet_rdma_disc_port_addr,
  1680. .get_mdts = nvmet_rdma_get_mdts,
  1681. .get_max_queue_size = nvmet_rdma_get_max_queue_size,
  1682. };
  1683. static void nvmet_rdma_remove_one(struct ib_device *ib_device, void *client_data)
  1684. {
  1685. struct nvmet_rdma_queue *queue, *tmp;
  1686. struct nvmet_rdma_device *ndev;
  1687. bool found = false;
  1688. mutex_lock(&device_list_mutex);
  1689. list_for_each_entry(ndev, &device_list, entry) {
  1690. if (ndev->device == ib_device) {
  1691. found = true;
  1692. break;
  1693. }
  1694. }
  1695. mutex_unlock(&device_list_mutex);
  1696. if (!found)
  1697. return;
  1698. /*
  1699. * IB Device that is used by nvmet controllers is being removed,
  1700. * delete all queues using this device.
  1701. */
  1702. mutex_lock(&nvmet_rdma_queue_mutex);
  1703. list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list,
  1704. queue_list) {
  1705. if (queue->dev->device != ib_device)
  1706. continue;
  1707. pr_info("Removing queue %d\n", queue->idx);
  1708. list_del_init(&queue->queue_list);
  1709. __nvmet_rdma_queue_disconnect(queue);
  1710. }
  1711. mutex_unlock(&nvmet_rdma_queue_mutex);
  1712. flush_workqueue(nvmet_wq);
  1713. }
  1714. static struct ib_client nvmet_rdma_ib_client = {
  1715. .name = "nvmet_rdma",
  1716. .remove = nvmet_rdma_remove_one
  1717. };
  1718. static int __init nvmet_rdma_init(void)
  1719. {
  1720. int ret;
  1721. ret = ib_register_client(&nvmet_rdma_ib_client);
  1722. if (ret)
  1723. return ret;
  1724. ret = nvmet_register_transport(&nvmet_rdma_ops);
  1725. if (ret)
  1726. goto err_ib_client;
  1727. return 0;
  1728. err_ib_client:
  1729. ib_unregister_client(&nvmet_rdma_ib_client);
  1730. return ret;
  1731. }
  1732. static void __exit nvmet_rdma_exit(void)
  1733. {
  1734. nvmet_unregister_transport(&nvmet_rdma_ops);
  1735. ib_unregister_client(&nvmet_rdma_ib_client);
  1736. WARN_ON_ONCE(!list_empty(&nvmet_rdma_queue_list));
  1737. ida_destroy(&nvmet_rdma_queue_ida);
  1738. }
  1739. module_init(nvmet_rdma_init);
  1740. module_exit(nvmet_rdma_exit);
  1741. MODULE_LICENSE("GPL v2");
  1742. MODULE_ALIAS("nvmet-transport-1"); /* 1 == NVMF_TRTYPE_RDMA */