rdma.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. /*
  2. * NVMe over Fabrics RDMA target.
  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/atomic.h>
  16. #include <linux/ctype.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/nvme.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/wait.h>
  25. #include <linux/inet.h>
  26. #include <asm/unaligned.h>
  27. #include <rdma/ib_verbs.h>
  28. #include <rdma/rdma_cm.h>
  29. #include <rdma/rw.h>
  30. #include <linux/nvme-rdma.h>
  31. #include "nvmet.h"
  32. /*
  33. * We allow up to a page of inline data to go with the SQE
  34. */
  35. #define NVMET_RDMA_INLINE_DATA_SIZE PAGE_SIZE
  36. struct nvmet_rdma_cmd {
  37. struct ib_sge sge[2];
  38. struct ib_cqe cqe;
  39. struct ib_recv_wr wr;
  40. struct scatterlist inline_sg;
  41. struct page *inline_page;
  42. struct nvme_command *nvme_cmd;
  43. struct nvmet_rdma_queue *queue;
  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 rdma_rw_ctx rw;
  57. struct nvmet_req req;
  58. u8 n_rdma;
  59. u32 flags;
  60. u32 invalidate_rkey;
  61. struct list_head wait_list;
  62. struct list_head free_list;
  63. };
  64. enum nvmet_rdma_queue_state {
  65. NVMET_RDMA_Q_CONNECTING,
  66. NVMET_RDMA_Q_LIVE,
  67. NVMET_RDMA_Q_DISCONNECTING,
  68. NVMET_RDMA_IN_DEVICE_REMOVAL,
  69. };
  70. struct nvmet_rdma_queue {
  71. struct rdma_cm_id *cm_id;
  72. struct nvmet_port *port;
  73. struct ib_cq *cq;
  74. atomic_t sq_wr_avail;
  75. struct nvmet_rdma_device *dev;
  76. spinlock_t state_lock;
  77. enum nvmet_rdma_queue_state state;
  78. struct nvmet_cq nvme_cq;
  79. struct nvmet_sq nvme_sq;
  80. struct nvmet_rdma_rsp *rsps;
  81. struct list_head free_rsps;
  82. spinlock_t rsps_lock;
  83. struct nvmet_rdma_cmd *cmds;
  84. struct work_struct release_work;
  85. struct list_head rsp_wait_list;
  86. struct list_head rsp_wr_wait_list;
  87. spinlock_t rsp_wr_wait_lock;
  88. int idx;
  89. int host_qid;
  90. int recv_queue_size;
  91. int send_queue_size;
  92. struct list_head queue_list;
  93. };
  94. struct nvmet_rdma_device {
  95. struct ib_device *device;
  96. struct ib_pd *pd;
  97. struct ib_srq *srq;
  98. struct nvmet_rdma_cmd *srq_cmds;
  99. size_t srq_size;
  100. struct kref ref;
  101. struct list_head entry;
  102. };
  103. static bool nvmet_rdma_use_srq;
  104. module_param_named(use_srq, nvmet_rdma_use_srq, bool, 0444);
  105. MODULE_PARM_DESC(use_srq, "Use shared receive queue.");
  106. static DEFINE_IDA(nvmet_rdma_queue_ida);
  107. static LIST_HEAD(nvmet_rdma_queue_list);
  108. static DEFINE_MUTEX(nvmet_rdma_queue_mutex);
  109. static LIST_HEAD(device_list);
  110. static DEFINE_MUTEX(device_list_mutex);
  111. static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp);
  112. static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc);
  113. static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
  114. static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc);
  115. static void nvmet_rdma_qp_event(struct ib_event *event, void *priv);
  116. static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue);
  117. static struct nvmet_fabrics_ops nvmet_rdma_ops;
  118. /* XXX: really should move to a generic header sooner or later.. */
  119. static inline u32 get_unaligned_le24(const u8 *p)
  120. {
  121. return (u32)p[0] | (u32)p[1] << 8 | (u32)p[2] << 16;
  122. }
  123. static inline bool nvmet_rdma_need_data_in(struct nvmet_rdma_rsp *rsp)
  124. {
  125. return nvme_is_write(rsp->req.cmd) &&
  126. rsp->req.data_len &&
  127. !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA);
  128. }
  129. static inline bool nvmet_rdma_need_data_out(struct nvmet_rdma_rsp *rsp)
  130. {
  131. return !nvme_is_write(rsp->req.cmd) &&
  132. rsp->req.data_len &&
  133. !rsp->req.rsp->status &&
  134. !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA);
  135. }
  136. static inline struct nvmet_rdma_rsp *
  137. nvmet_rdma_get_rsp(struct nvmet_rdma_queue *queue)
  138. {
  139. struct nvmet_rdma_rsp *rsp;
  140. unsigned long flags;
  141. spin_lock_irqsave(&queue->rsps_lock, flags);
  142. rsp = list_first_entry(&queue->free_rsps,
  143. struct nvmet_rdma_rsp, free_list);
  144. list_del(&rsp->free_list);
  145. spin_unlock_irqrestore(&queue->rsps_lock, flags);
  146. return rsp;
  147. }
  148. static inline void
  149. nvmet_rdma_put_rsp(struct nvmet_rdma_rsp *rsp)
  150. {
  151. unsigned long flags;
  152. spin_lock_irqsave(&rsp->queue->rsps_lock, flags);
  153. list_add_tail(&rsp->free_list, &rsp->queue->free_rsps);
  154. spin_unlock_irqrestore(&rsp->queue->rsps_lock, flags);
  155. }
  156. static void nvmet_rdma_free_sgl(struct scatterlist *sgl, unsigned int nents)
  157. {
  158. struct scatterlist *sg;
  159. int count;
  160. if (!sgl || !nents)
  161. return;
  162. for_each_sg(sgl, sg, nents, count)
  163. __free_page(sg_page(sg));
  164. kfree(sgl);
  165. }
  166. static int nvmet_rdma_alloc_sgl(struct scatterlist **sgl, unsigned int *nents,
  167. u32 length)
  168. {
  169. struct scatterlist *sg;
  170. struct page *page;
  171. unsigned int nent;
  172. int i = 0;
  173. nent = DIV_ROUND_UP(length, PAGE_SIZE);
  174. sg = kmalloc_array(nent, sizeof(struct scatterlist), GFP_KERNEL);
  175. if (!sg)
  176. goto out;
  177. sg_init_table(sg, nent);
  178. while (length) {
  179. u32 page_len = min_t(u32, length, PAGE_SIZE);
  180. page = alloc_page(GFP_KERNEL);
  181. if (!page)
  182. goto out_free_pages;
  183. sg_set_page(&sg[i], page, page_len, 0);
  184. length -= page_len;
  185. i++;
  186. }
  187. *sgl = sg;
  188. *nents = nent;
  189. return 0;
  190. out_free_pages:
  191. while (i > 0) {
  192. i--;
  193. __free_page(sg_page(&sg[i]));
  194. }
  195. kfree(sg);
  196. out:
  197. return NVME_SC_INTERNAL;
  198. }
  199. static int nvmet_rdma_alloc_cmd(struct nvmet_rdma_device *ndev,
  200. struct nvmet_rdma_cmd *c, bool admin)
  201. {
  202. /* NVMe command / RDMA RECV */
  203. c->nvme_cmd = kmalloc(sizeof(*c->nvme_cmd), GFP_KERNEL);
  204. if (!c->nvme_cmd)
  205. goto out;
  206. c->sge[0].addr = ib_dma_map_single(ndev->device, c->nvme_cmd,
  207. sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
  208. if (ib_dma_mapping_error(ndev->device, c->sge[0].addr))
  209. goto out_free_cmd;
  210. c->sge[0].length = sizeof(*c->nvme_cmd);
  211. c->sge[0].lkey = ndev->pd->local_dma_lkey;
  212. if (!admin) {
  213. c->inline_page = alloc_pages(GFP_KERNEL,
  214. get_order(NVMET_RDMA_INLINE_DATA_SIZE));
  215. if (!c->inline_page)
  216. goto out_unmap_cmd;
  217. c->sge[1].addr = ib_dma_map_page(ndev->device,
  218. c->inline_page, 0, NVMET_RDMA_INLINE_DATA_SIZE,
  219. DMA_FROM_DEVICE);
  220. if (ib_dma_mapping_error(ndev->device, c->sge[1].addr))
  221. goto out_free_inline_page;
  222. c->sge[1].length = NVMET_RDMA_INLINE_DATA_SIZE;
  223. c->sge[1].lkey = ndev->pd->local_dma_lkey;
  224. }
  225. c->cqe.done = nvmet_rdma_recv_done;
  226. c->wr.wr_cqe = &c->cqe;
  227. c->wr.sg_list = c->sge;
  228. c->wr.num_sge = admin ? 1 : 2;
  229. return 0;
  230. out_free_inline_page:
  231. if (!admin) {
  232. __free_pages(c->inline_page,
  233. get_order(NVMET_RDMA_INLINE_DATA_SIZE));
  234. }
  235. out_unmap_cmd:
  236. ib_dma_unmap_single(ndev->device, c->sge[0].addr,
  237. sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
  238. out_free_cmd:
  239. kfree(c->nvme_cmd);
  240. out:
  241. return -ENOMEM;
  242. }
  243. static void nvmet_rdma_free_cmd(struct nvmet_rdma_device *ndev,
  244. struct nvmet_rdma_cmd *c, bool admin)
  245. {
  246. if (!admin) {
  247. ib_dma_unmap_page(ndev->device, c->sge[1].addr,
  248. NVMET_RDMA_INLINE_DATA_SIZE, DMA_FROM_DEVICE);
  249. __free_pages(c->inline_page,
  250. get_order(NVMET_RDMA_INLINE_DATA_SIZE));
  251. }
  252. ib_dma_unmap_single(ndev->device, c->sge[0].addr,
  253. sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
  254. kfree(c->nvme_cmd);
  255. }
  256. static struct nvmet_rdma_cmd *
  257. nvmet_rdma_alloc_cmds(struct nvmet_rdma_device *ndev,
  258. int nr_cmds, bool admin)
  259. {
  260. struct nvmet_rdma_cmd *cmds;
  261. int ret = -EINVAL, i;
  262. cmds = kcalloc(nr_cmds, sizeof(struct nvmet_rdma_cmd), GFP_KERNEL);
  263. if (!cmds)
  264. goto out;
  265. for (i = 0; i < nr_cmds; i++) {
  266. ret = nvmet_rdma_alloc_cmd(ndev, cmds + i, admin);
  267. if (ret)
  268. goto out_free;
  269. }
  270. return cmds;
  271. out_free:
  272. while (--i >= 0)
  273. nvmet_rdma_free_cmd(ndev, cmds + i, admin);
  274. kfree(cmds);
  275. out:
  276. return ERR_PTR(ret);
  277. }
  278. static void nvmet_rdma_free_cmds(struct nvmet_rdma_device *ndev,
  279. struct nvmet_rdma_cmd *cmds, int nr_cmds, bool admin)
  280. {
  281. int i;
  282. for (i = 0; i < nr_cmds; i++)
  283. nvmet_rdma_free_cmd(ndev, cmds + i, admin);
  284. kfree(cmds);
  285. }
  286. static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev,
  287. struct nvmet_rdma_rsp *r)
  288. {
  289. /* NVMe CQE / RDMA SEND */
  290. r->req.rsp = kmalloc(sizeof(*r->req.rsp), GFP_KERNEL);
  291. if (!r->req.rsp)
  292. goto out;
  293. r->send_sge.addr = ib_dma_map_single(ndev->device, r->req.rsp,
  294. sizeof(*r->req.rsp), DMA_TO_DEVICE);
  295. if (ib_dma_mapping_error(ndev->device, r->send_sge.addr))
  296. goto out_free_rsp;
  297. r->send_sge.length = sizeof(*r->req.rsp);
  298. r->send_sge.lkey = ndev->pd->local_dma_lkey;
  299. r->send_cqe.done = nvmet_rdma_send_done;
  300. r->send_wr.wr_cqe = &r->send_cqe;
  301. r->send_wr.sg_list = &r->send_sge;
  302. r->send_wr.num_sge = 1;
  303. r->send_wr.send_flags = IB_SEND_SIGNALED;
  304. /* Data In / RDMA READ */
  305. r->read_cqe.done = nvmet_rdma_read_data_done;
  306. return 0;
  307. out_free_rsp:
  308. kfree(r->req.rsp);
  309. out:
  310. return -ENOMEM;
  311. }
  312. static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev,
  313. struct nvmet_rdma_rsp *r)
  314. {
  315. ib_dma_unmap_single(ndev->device, r->send_sge.addr,
  316. sizeof(*r->req.rsp), DMA_TO_DEVICE);
  317. kfree(r->req.rsp);
  318. }
  319. static int
  320. nvmet_rdma_alloc_rsps(struct nvmet_rdma_queue *queue)
  321. {
  322. struct nvmet_rdma_device *ndev = queue->dev;
  323. int nr_rsps = queue->recv_queue_size * 2;
  324. int ret = -EINVAL, i;
  325. queue->rsps = kcalloc(nr_rsps, sizeof(struct nvmet_rdma_rsp),
  326. GFP_KERNEL);
  327. if (!queue->rsps)
  328. goto out;
  329. for (i = 0; i < nr_rsps; i++) {
  330. struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
  331. ret = nvmet_rdma_alloc_rsp(ndev, rsp);
  332. if (ret)
  333. goto out_free;
  334. list_add_tail(&rsp->free_list, &queue->free_rsps);
  335. }
  336. return 0;
  337. out_free:
  338. while (--i >= 0) {
  339. struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
  340. list_del(&rsp->free_list);
  341. nvmet_rdma_free_rsp(ndev, rsp);
  342. }
  343. kfree(queue->rsps);
  344. out:
  345. return ret;
  346. }
  347. static void nvmet_rdma_free_rsps(struct nvmet_rdma_queue *queue)
  348. {
  349. struct nvmet_rdma_device *ndev = queue->dev;
  350. int i, nr_rsps = queue->recv_queue_size * 2;
  351. for (i = 0; i < nr_rsps; i++) {
  352. struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
  353. list_del(&rsp->free_list);
  354. nvmet_rdma_free_rsp(ndev, rsp);
  355. }
  356. kfree(queue->rsps);
  357. }
  358. static int nvmet_rdma_post_recv(struct nvmet_rdma_device *ndev,
  359. struct nvmet_rdma_cmd *cmd)
  360. {
  361. struct ib_recv_wr *bad_wr;
  362. ib_dma_sync_single_for_device(ndev->device,
  363. cmd->sge[0].addr, cmd->sge[0].length,
  364. DMA_FROM_DEVICE);
  365. if (ndev->srq)
  366. return ib_post_srq_recv(ndev->srq, &cmd->wr, &bad_wr);
  367. return ib_post_recv(cmd->queue->cm_id->qp, &cmd->wr, &bad_wr);
  368. }
  369. static void nvmet_rdma_process_wr_wait_list(struct nvmet_rdma_queue *queue)
  370. {
  371. spin_lock(&queue->rsp_wr_wait_lock);
  372. while (!list_empty(&queue->rsp_wr_wait_list)) {
  373. struct nvmet_rdma_rsp *rsp;
  374. bool ret;
  375. rsp = list_entry(queue->rsp_wr_wait_list.next,
  376. struct nvmet_rdma_rsp, wait_list);
  377. list_del(&rsp->wait_list);
  378. spin_unlock(&queue->rsp_wr_wait_lock);
  379. ret = nvmet_rdma_execute_command(rsp);
  380. spin_lock(&queue->rsp_wr_wait_lock);
  381. if (!ret) {
  382. list_add(&rsp->wait_list, &queue->rsp_wr_wait_list);
  383. break;
  384. }
  385. }
  386. spin_unlock(&queue->rsp_wr_wait_lock);
  387. }
  388. static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp)
  389. {
  390. struct nvmet_rdma_queue *queue = rsp->queue;
  391. atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail);
  392. if (rsp->n_rdma) {
  393. rdma_rw_ctx_destroy(&rsp->rw, queue->cm_id->qp,
  394. queue->cm_id->port_num, rsp->req.sg,
  395. rsp->req.sg_cnt, nvmet_data_dir(&rsp->req));
  396. }
  397. if (rsp->req.sg != &rsp->cmd->inline_sg)
  398. nvmet_rdma_free_sgl(rsp->req.sg, rsp->req.sg_cnt);
  399. if (unlikely(!list_empty_careful(&queue->rsp_wr_wait_list)))
  400. nvmet_rdma_process_wr_wait_list(queue);
  401. nvmet_rdma_put_rsp(rsp);
  402. }
  403. static void nvmet_rdma_error_comp(struct nvmet_rdma_queue *queue)
  404. {
  405. if (queue->nvme_sq.ctrl) {
  406. nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl);
  407. } else {
  408. /*
  409. * we didn't setup the controller yet in case
  410. * of admin connect error, just disconnect and
  411. * cleanup the queue
  412. */
  413. nvmet_rdma_queue_disconnect(queue);
  414. }
  415. }
  416. static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
  417. {
  418. struct nvmet_rdma_rsp *rsp =
  419. container_of(wc->wr_cqe, struct nvmet_rdma_rsp, send_cqe);
  420. nvmet_rdma_release_rsp(rsp);
  421. if (unlikely(wc->status != IB_WC_SUCCESS &&
  422. wc->status != IB_WC_WR_FLUSH_ERR)) {
  423. pr_err("SEND for CQE 0x%p failed with status %s (%d).\n",
  424. wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status);
  425. nvmet_rdma_error_comp(rsp->queue);
  426. }
  427. }
  428. static void nvmet_rdma_queue_response(struct nvmet_req *req)
  429. {
  430. struct nvmet_rdma_rsp *rsp =
  431. container_of(req, struct nvmet_rdma_rsp, req);
  432. struct rdma_cm_id *cm_id = rsp->queue->cm_id;
  433. struct ib_send_wr *first_wr, *bad_wr;
  434. if (rsp->flags & NVMET_RDMA_REQ_INVALIDATE_RKEY) {
  435. rsp->send_wr.opcode = IB_WR_SEND_WITH_INV;
  436. rsp->send_wr.ex.invalidate_rkey = rsp->invalidate_rkey;
  437. } else {
  438. rsp->send_wr.opcode = IB_WR_SEND;
  439. }
  440. if (nvmet_rdma_need_data_out(rsp))
  441. first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp,
  442. cm_id->port_num, NULL, &rsp->send_wr);
  443. else
  444. first_wr = &rsp->send_wr;
  445. nvmet_rdma_post_recv(rsp->queue->dev, rsp->cmd);
  446. ib_dma_sync_single_for_device(rsp->queue->dev->device,
  447. rsp->send_sge.addr, rsp->send_sge.length,
  448. DMA_TO_DEVICE);
  449. if (ib_post_send(cm_id->qp, first_wr, &bad_wr)) {
  450. pr_err("sending cmd response failed\n");
  451. nvmet_rdma_release_rsp(rsp);
  452. }
  453. }
  454. static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc)
  455. {
  456. struct nvmet_rdma_rsp *rsp =
  457. container_of(wc->wr_cqe, struct nvmet_rdma_rsp, read_cqe);
  458. struct nvmet_rdma_queue *queue = cq->cq_context;
  459. WARN_ON(rsp->n_rdma <= 0);
  460. atomic_add(rsp->n_rdma, &queue->sq_wr_avail);
  461. rdma_rw_ctx_destroy(&rsp->rw, queue->cm_id->qp,
  462. queue->cm_id->port_num, rsp->req.sg,
  463. rsp->req.sg_cnt, nvmet_data_dir(&rsp->req));
  464. rsp->n_rdma = 0;
  465. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  466. nvmet_req_uninit(&rsp->req);
  467. nvmet_rdma_release_rsp(rsp);
  468. if (wc->status != IB_WC_WR_FLUSH_ERR) {
  469. pr_info("RDMA READ for CQE 0x%p failed with status %s (%d).\n",
  470. wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status);
  471. nvmet_rdma_error_comp(queue);
  472. }
  473. return;
  474. }
  475. rsp->req.execute(&rsp->req);
  476. }
  477. static void nvmet_rdma_use_inline_sg(struct nvmet_rdma_rsp *rsp, u32 len,
  478. u64 off)
  479. {
  480. sg_init_table(&rsp->cmd->inline_sg, 1);
  481. sg_set_page(&rsp->cmd->inline_sg, rsp->cmd->inline_page, len, off);
  482. rsp->req.sg = &rsp->cmd->inline_sg;
  483. rsp->req.sg_cnt = 1;
  484. }
  485. static u16 nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp *rsp)
  486. {
  487. struct nvme_sgl_desc *sgl = &rsp->req.cmd->common.dptr.sgl;
  488. u64 off = le64_to_cpu(sgl->addr);
  489. u32 len = le32_to_cpu(sgl->length);
  490. if (!nvme_is_write(rsp->req.cmd))
  491. return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  492. if (off + len > NVMET_RDMA_INLINE_DATA_SIZE) {
  493. pr_err("invalid inline data offset!\n");
  494. return NVME_SC_SGL_INVALID_OFFSET | NVME_SC_DNR;
  495. }
  496. /* no data command? */
  497. if (!len)
  498. return 0;
  499. nvmet_rdma_use_inline_sg(rsp, len, off);
  500. rsp->flags |= NVMET_RDMA_REQ_INLINE_DATA;
  501. return 0;
  502. }
  503. static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp,
  504. struct nvme_keyed_sgl_desc *sgl, bool invalidate)
  505. {
  506. struct rdma_cm_id *cm_id = rsp->queue->cm_id;
  507. u64 addr = le64_to_cpu(sgl->addr);
  508. u32 len = get_unaligned_le24(sgl->length);
  509. u32 key = get_unaligned_le32(sgl->key);
  510. int ret;
  511. u16 status;
  512. /* no data command? */
  513. if (!len)
  514. return 0;
  515. status = nvmet_rdma_alloc_sgl(&rsp->req.sg, &rsp->req.sg_cnt,
  516. len);
  517. if (status)
  518. return status;
  519. ret = rdma_rw_ctx_init(&rsp->rw, cm_id->qp, cm_id->port_num,
  520. rsp->req.sg, rsp->req.sg_cnt, 0, addr, key,
  521. nvmet_data_dir(&rsp->req));
  522. if (ret < 0)
  523. return NVME_SC_INTERNAL;
  524. rsp->n_rdma += ret;
  525. if (invalidate) {
  526. rsp->invalidate_rkey = key;
  527. rsp->flags |= NVMET_RDMA_REQ_INVALIDATE_RKEY;
  528. }
  529. return 0;
  530. }
  531. static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp)
  532. {
  533. struct nvme_keyed_sgl_desc *sgl = &rsp->req.cmd->common.dptr.ksgl;
  534. switch (sgl->type >> 4) {
  535. case NVME_SGL_FMT_DATA_DESC:
  536. switch (sgl->type & 0xf) {
  537. case NVME_SGL_FMT_OFFSET:
  538. return nvmet_rdma_map_sgl_inline(rsp);
  539. default:
  540. pr_err("invalid SGL subtype: %#x\n", sgl->type);
  541. return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  542. }
  543. case NVME_KEY_SGL_FMT_DATA_DESC:
  544. switch (sgl->type & 0xf) {
  545. case NVME_SGL_FMT_ADDRESS | NVME_SGL_FMT_INVALIDATE:
  546. return nvmet_rdma_map_sgl_keyed(rsp, sgl, true);
  547. case NVME_SGL_FMT_ADDRESS:
  548. return nvmet_rdma_map_sgl_keyed(rsp, sgl, false);
  549. default:
  550. pr_err("invalid SGL subtype: %#x\n", sgl->type);
  551. return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  552. }
  553. default:
  554. pr_err("invalid SGL type: %#x\n", sgl->type);
  555. return NVME_SC_SGL_INVALID_TYPE | NVME_SC_DNR;
  556. }
  557. }
  558. static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp)
  559. {
  560. struct nvmet_rdma_queue *queue = rsp->queue;
  561. if (unlikely(atomic_sub_return(1 + rsp->n_rdma,
  562. &queue->sq_wr_avail) < 0)) {
  563. pr_debug("IB send queue full (needed %d): queue %u cntlid %u\n",
  564. 1 + rsp->n_rdma, queue->idx,
  565. queue->nvme_sq.ctrl->cntlid);
  566. atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail);
  567. return false;
  568. }
  569. if (nvmet_rdma_need_data_in(rsp)) {
  570. if (rdma_rw_ctx_post(&rsp->rw, queue->cm_id->qp,
  571. queue->cm_id->port_num, &rsp->read_cqe, NULL))
  572. nvmet_req_complete(&rsp->req, NVME_SC_DATA_XFER_ERROR);
  573. } else {
  574. rsp->req.execute(&rsp->req);
  575. }
  576. return true;
  577. }
  578. static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue,
  579. struct nvmet_rdma_rsp *cmd)
  580. {
  581. u16 status;
  582. ib_dma_sync_single_for_cpu(queue->dev->device,
  583. cmd->cmd->sge[0].addr, cmd->cmd->sge[0].length,
  584. DMA_FROM_DEVICE);
  585. ib_dma_sync_single_for_cpu(queue->dev->device,
  586. cmd->send_sge.addr, cmd->send_sge.length,
  587. DMA_TO_DEVICE);
  588. if (!nvmet_req_init(&cmd->req, &queue->nvme_cq,
  589. &queue->nvme_sq, &nvmet_rdma_ops))
  590. return;
  591. status = nvmet_rdma_map_sgl(cmd);
  592. if (status)
  593. goto out_err;
  594. if (unlikely(!nvmet_rdma_execute_command(cmd))) {
  595. spin_lock(&queue->rsp_wr_wait_lock);
  596. list_add_tail(&cmd->wait_list, &queue->rsp_wr_wait_list);
  597. spin_unlock(&queue->rsp_wr_wait_lock);
  598. }
  599. return;
  600. out_err:
  601. nvmet_req_complete(&cmd->req, status);
  602. }
  603. static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
  604. {
  605. struct nvmet_rdma_cmd *cmd =
  606. container_of(wc->wr_cqe, struct nvmet_rdma_cmd, cqe);
  607. struct nvmet_rdma_queue *queue = cq->cq_context;
  608. struct nvmet_rdma_rsp *rsp;
  609. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  610. if (wc->status != IB_WC_WR_FLUSH_ERR) {
  611. pr_err("RECV for CQE 0x%p failed with status %s (%d)\n",
  612. wc->wr_cqe, ib_wc_status_msg(wc->status),
  613. wc->status);
  614. nvmet_rdma_error_comp(queue);
  615. }
  616. return;
  617. }
  618. if (unlikely(wc->byte_len < sizeof(struct nvme_command))) {
  619. pr_err("Ctrl Fatal Error: capsule size less than 64 bytes\n");
  620. nvmet_rdma_error_comp(queue);
  621. return;
  622. }
  623. cmd->queue = queue;
  624. rsp = nvmet_rdma_get_rsp(queue);
  625. rsp->queue = queue;
  626. rsp->cmd = cmd;
  627. rsp->flags = 0;
  628. rsp->req.cmd = cmd->nvme_cmd;
  629. rsp->req.port = queue->port;
  630. rsp->n_rdma = 0;
  631. if (unlikely(queue->state != NVMET_RDMA_Q_LIVE)) {
  632. unsigned long flags;
  633. spin_lock_irqsave(&queue->state_lock, flags);
  634. if (queue->state == NVMET_RDMA_Q_CONNECTING)
  635. list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
  636. else
  637. nvmet_rdma_put_rsp(rsp);
  638. spin_unlock_irqrestore(&queue->state_lock, flags);
  639. return;
  640. }
  641. nvmet_rdma_handle_command(queue, rsp);
  642. }
  643. static void nvmet_rdma_destroy_srq(struct nvmet_rdma_device *ndev)
  644. {
  645. if (!ndev->srq)
  646. return;
  647. nvmet_rdma_free_cmds(ndev, ndev->srq_cmds, ndev->srq_size, false);
  648. ib_destroy_srq(ndev->srq);
  649. }
  650. static int nvmet_rdma_init_srq(struct nvmet_rdma_device *ndev)
  651. {
  652. struct ib_srq_init_attr srq_attr = { NULL, };
  653. struct ib_srq *srq;
  654. size_t srq_size;
  655. int ret, i;
  656. srq_size = 4095; /* XXX: tune */
  657. srq_attr.attr.max_wr = srq_size;
  658. srq_attr.attr.max_sge = 2;
  659. srq_attr.attr.srq_limit = 0;
  660. srq_attr.srq_type = IB_SRQT_BASIC;
  661. srq = ib_create_srq(ndev->pd, &srq_attr);
  662. if (IS_ERR(srq)) {
  663. /*
  664. * If SRQs aren't supported we just go ahead and use normal
  665. * non-shared receive queues.
  666. */
  667. pr_info("SRQ requested but not supported.\n");
  668. return 0;
  669. }
  670. ndev->srq_cmds = nvmet_rdma_alloc_cmds(ndev, srq_size, false);
  671. if (IS_ERR(ndev->srq_cmds)) {
  672. ret = PTR_ERR(ndev->srq_cmds);
  673. goto out_destroy_srq;
  674. }
  675. ndev->srq = srq;
  676. ndev->srq_size = srq_size;
  677. for (i = 0; i < srq_size; i++)
  678. nvmet_rdma_post_recv(ndev, &ndev->srq_cmds[i]);
  679. return 0;
  680. out_destroy_srq:
  681. ib_destroy_srq(srq);
  682. return ret;
  683. }
  684. static void nvmet_rdma_free_dev(struct kref *ref)
  685. {
  686. struct nvmet_rdma_device *ndev =
  687. container_of(ref, struct nvmet_rdma_device, ref);
  688. mutex_lock(&device_list_mutex);
  689. list_del(&ndev->entry);
  690. mutex_unlock(&device_list_mutex);
  691. nvmet_rdma_destroy_srq(ndev);
  692. ib_dealloc_pd(ndev->pd);
  693. kfree(ndev);
  694. }
  695. static struct nvmet_rdma_device *
  696. nvmet_rdma_find_get_device(struct rdma_cm_id *cm_id)
  697. {
  698. struct nvmet_rdma_device *ndev;
  699. int ret;
  700. mutex_lock(&device_list_mutex);
  701. list_for_each_entry(ndev, &device_list, entry) {
  702. if (ndev->device->node_guid == cm_id->device->node_guid &&
  703. kref_get_unless_zero(&ndev->ref))
  704. goto out_unlock;
  705. }
  706. ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
  707. if (!ndev)
  708. goto out_err;
  709. ndev->device = cm_id->device;
  710. kref_init(&ndev->ref);
  711. ndev->pd = ib_alloc_pd(ndev->device, 0);
  712. if (IS_ERR(ndev->pd))
  713. goto out_free_dev;
  714. if (nvmet_rdma_use_srq) {
  715. ret = nvmet_rdma_init_srq(ndev);
  716. if (ret)
  717. goto out_free_pd;
  718. }
  719. list_add(&ndev->entry, &device_list);
  720. out_unlock:
  721. mutex_unlock(&device_list_mutex);
  722. pr_debug("added %s.\n", ndev->device->name);
  723. return ndev;
  724. out_free_pd:
  725. ib_dealloc_pd(ndev->pd);
  726. out_free_dev:
  727. kfree(ndev);
  728. out_err:
  729. mutex_unlock(&device_list_mutex);
  730. return NULL;
  731. }
  732. static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue *queue)
  733. {
  734. struct ib_qp_init_attr qp_attr;
  735. struct nvmet_rdma_device *ndev = queue->dev;
  736. int comp_vector, nr_cqe, ret, i;
  737. /*
  738. * Spread the io queues across completion vectors,
  739. * but still keep all admin queues on vector 0.
  740. */
  741. comp_vector = !queue->host_qid ? 0 :
  742. queue->idx % ndev->device->num_comp_vectors;
  743. /*
  744. * Reserve CQ slots for RECV + RDMA_READ/RDMA_WRITE + RDMA_SEND.
  745. */
  746. nr_cqe = queue->recv_queue_size + 2 * queue->send_queue_size;
  747. queue->cq = ib_alloc_cq(ndev->device, queue,
  748. nr_cqe + 1, comp_vector,
  749. IB_POLL_WORKQUEUE);
  750. if (IS_ERR(queue->cq)) {
  751. ret = PTR_ERR(queue->cq);
  752. pr_err("failed to create CQ cqe= %d ret= %d\n",
  753. nr_cqe + 1, ret);
  754. goto out;
  755. }
  756. memset(&qp_attr, 0, sizeof(qp_attr));
  757. qp_attr.qp_context = queue;
  758. qp_attr.event_handler = nvmet_rdma_qp_event;
  759. qp_attr.send_cq = queue->cq;
  760. qp_attr.recv_cq = queue->cq;
  761. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  762. qp_attr.qp_type = IB_QPT_RC;
  763. /* +1 for drain */
  764. qp_attr.cap.max_send_wr = queue->send_queue_size + 1;
  765. qp_attr.cap.max_rdma_ctxs = queue->send_queue_size;
  766. qp_attr.cap.max_send_sge = max(ndev->device->attrs.max_sge_rd,
  767. ndev->device->attrs.max_sge);
  768. if (ndev->srq) {
  769. qp_attr.srq = ndev->srq;
  770. } else {
  771. /* +1 for drain */
  772. qp_attr.cap.max_recv_wr = 1 + queue->recv_queue_size;
  773. qp_attr.cap.max_recv_sge = 2;
  774. }
  775. ret = rdma_create_qp(queue->cm_id, ndev->pd, &qp_attr);
  776. if (ret) {
  777. pr_err("failed to create_qp ret= %d\n", ret);
  778. goto err_destroy_cq;
  779. }
  780. atomic_set(&queue->sq_wr_avail, qp_attr.cap.max_send_wr);
  781. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
  782. __func__, queue->cq->cqe, qp_attr.cap.max_send_sge,
  783. qp_attr.cap.max_send_wr, queue->cm_id);
  784. if (!ndev->srq) {
  785. for (i = 0; i < queue->recv_queue_size; i++) {
  786. queue->cmds[i].queue = queue;
  787. nvmet_rdma_post_recv(ndev, &queue->cmds[i]);
  788. }
  789. }
  790. out:
  791. return ret;
  792. err_destroy_cq:
  793. ib_free_cq(queue->cq);
  794. goto out;
  795. }
  796. static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue *queue)
  797. {
  798. ib_drain_qp(queue->cm_id->qp);
  799. rdma_destroy_qp(queue->cm_id);
  800. ib_free_cq(queue->cq);
  801. }
  802. static void nvmet_rdma_free_queue(struct nvmet_rdma_queue *queue)
  803. {
  804. pr_info("freeing queue %d\n", queue->idx);
  805. nvmet_sq_destroy(&queue->nvme_sq);
  806. nvmet_rdma_destroy_queue_ib(queue);
  807. if (!queue->dev->srq) {
  808. nvmet_rdma_free_cmds(queue->dev, queue->cmds,
  809. queue->recv_queue_size,
  810. !queue->host_qid);
  811. }
  812. nvmet_rdma_free_rsps(queue);
  813. ida_simple_remove(&nvmet_rdma_queue_ida, queue->idx);
  814. kfree(queue);
  815. }
  816. static void nvmet_rdma_release_queue_work(struct work_struct *w)
  817. {
  818. struct nvmet_rdma_queue *queue =
  819. container_of(w, struct nvmet_rdma_queue, release_work);
  820. struct rdma_cm_id *cm_id = queue->cm_id;
  821. struct nvmet_rdma_device *dev = queue->dev;
  822. enum nvmet_rdma_queue_state state = queue->state;
  823. nvmet_rdma_free_queue(queue);
  824. if (state != NVMET_RDMA_IN_DEVICE_REMOVAL)
  825. rdma_destroy_id(cm_id);
  826. kref_put(&dev->ref, nvmet_rdma_free_dev);
  827. }
  828. static int
  829. nvmet_rdma_parse_cm_connect_req(struct rdma_conn_param *conn,
  830. struct nvmet_rdma_queue *queue)
  831. {
  832. struct nvme_rdma_cm_req *req;
  833. req = (struct nvme_rdma_cm_req *)conn->private_data;
  834. if (!req || conn->private_data_len == 0)
  835. return NVME_RDMA_CM_INVALID_LEN;
  836. if (le16_to_cpu(req->recfmt) != NVME_RDMA_CM_FMT_1_0)
  837. return NVME_RDMA_CM_INVALID_RECFMT;
  838. queue->host_qid = le16_to_cpu(req->qid);
  839. /*
  840. * req->hsqsize corresponds to our recv queue size plus 1
  841. * req->hrqsize corresponds to our send queue size
  842. */
  843. queue->recv_queue_size = le16_to_cpu(req->hsqsize) + 1;
  844. queue->send_queue_size = le16_to_cpu(req->hrqsize);
  845. if (!queue->host_qid && queue->recv_queue_size > NVME_AQ_DEPTH)
  846. return NVME_RDMA_CM_INVALID_HSQSIZE;
  847. /* XXX: Should we enforce some kind of max for IO queues? */
  848. return 0;
  849. }
  850. static int nvmet_rdma_cm_reject(struct rdma_cm_id *cm_id,
  851. enum nvme_rdma_cm_status status)
  852. {
  853. struct nvme_rdma_cm_rej rej;
  854. pr_debug("rejecting connect request: status %d (%s)\n",
  855. status, nvme_rdma_cm_msg(status));
  856. rej.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
  857. rej.sts = cpu_to_le16(status);
  858. return rdma_reject(cm_id, (void *)&rej, sizeof(rej));
  859. }
  860. static struct nvmet_rdma_queue *
  861. nvmet_rdma_alloc_queue(struct nvmet_rdma_device *ndev,
  862. struct rdma_cm_id *cm_id,
  863. struct rdma_cm_event *event)
  864. {
  865. struct nvmet_rdma_queue *queue;
  866. int ret;
  867. queue = kzalloc(sizeof(*queue), GFP_KERNEL);
  868. if (!queue) {
  869. ret = NVME_RDMA_CM_NO_RSC;
  870. goto out_reject;
  871. }
  872. ret = nvmet_sq_init(&queue->nvme_sq);
  873. if (ret) {
  874. ret = NVME_RDMA_CM_NO_RSC;
  875. goto out_free_queue;
  876. }
  877. ret = nvmet_rdma_parse_cm_connect_req(&event->param.conn, queue);
  878. if (ret)
  879. goto out_destroy_sq;
  880. /*
  881. * Schedules the actual release because calling rdma_destroy_id from
  882. * inside a CM callback would trigger a deadlock. (great API design..)
  883. */
  884. INIT_WORK(&queue->release_work, nvmet_rdma_release_queue_work);
  885. queue->dev = ndev;
  886. queue->cm_id = cm_id;
  887. spin_lock_init(&queue->state_lock);
  888. queue->state = NVMET_RDMA_Q_CONNECTING;
  889. INIT_LIST_HEAD(&queue->rsp_wait_list);
  890. INIT_LIST_HEAD(&queue->rsp_wr_wait_list);
  891. spin_lock_init(&queue->rsp_wr_wait_lock);
  892. INIT_LIST_HEAD(&queue->free_rsps);
  893. spin_lock_init(&queue->rsps_lock);
  894. INIT_LIST_HEAD(&queue->queue_list);
  895. queue->idx = ida_simple_get(&nvmet_rdma_queue_ida, 0, 0, GFP_KERNEL);
  896. if (queue->idx < 0) {
  897. ret = NVME_RDMA_CM_NO_RSC;
  898. goto out_destroy_sq;
  899. }
  900. ret = nvmet_rdma_alloc_rsps(queue);
  901. if (ret) {
  902. ret = NVME_RDMA_CM_NO_RSC;
  903. goto out_ida_remove;
  904. }
  905. if (!ndev->srq) {
  906. queue->cmds = nvmet_rdma_alloc_cmds(ndev,
  907. queue->recv_queue_size,
  908. !queue->host_qid);
  909. if (IS_ERR(queue->cmds)) {
  910. ret = NVME_RDMA_CM_NO_RSC;
  911. goto out_free_responses;
  912. }
  913. }
  914. ret = nvmet_rdma_create_queue_ib(queue);
  915. if (ret) {
  916. pr_err("%s: creating RDMA queue failed (%d).\n",
  917. __func__, ret);
  918. ret = NVME_RDMA_CM_NO_RSC;
  919. goto out_free_cmds;
  920. }
  921. return queue;
  922. out_free_cmds:
  923. if (!ndev->srq) {
  924. nvmet_rdma_free_cmds(queue->dev, queue->cmds,
  925. queue->recv_queue_size,
  926. !queue->host_qid);
  927. }
  928. out_free_responses:
  929. nvmet_rdma_free_rsps(queue);
  930. out_ida_remove:
  931. ida_simple_remove(&nvmet_rdma_queue_ida, queue->idx);
  932. out_destroy_sq:
  933. nvmet_sq_destroy(&queue->nvme_sq);
  934. out_free_queue:
  935. kfree(queue);
  936. out_reject:
  937. nvmet_rdma_cm_reject(cm_id, ret);
  938. return NULL;
  939. }
  940. static void nvmet_rdma_qp_event(struct ib_event *event, void *priv)
  941. {
  942. struct nvmet_rdma_queue *queue = priv;
  943. switch (event->event) {
  944. case IB_EVENT_COMM_EST:
  945. rdma_notify(queue->cm_id, event->event);
  946. break;
  947. default:
  948. pr_err("received IB QP event: %s (%d)\n",
  949. ib_event_msg(event->event), event->event);
  950. break;
  951. }
  952. }
  953. static int nvmet_rdma_cm_accept(struct rdma_cm_id *cm_id,
  954. struct nvmet_rdma_queue *queue,
  955. struct rdma_conn_param *p)
  956. {
  957. struct rdma_conn_param param = { };
  958. struct nvme_rdma_cm_rep priv = { };
  959. int ret = -ENOMEM;
  960. param.rnr_retry_count = 7;
  961. param.flow_control = 1;
  962. param.initiator_depth = min_t(u8, p->initiator_depth,
  963. queue->dev->device->attrs.max_qp_init_rd_atom);
  964. param.private_data = &priv;
  965. param.private_data_len = sizeof(priv);
  966. priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
  967. priv.crqsize = cpu_to_le16(queue->recv_queue_size);
  968. ret = rdma_accept(cm_id, &param);
  969. if (ret)
  970. pr_err("rdma_accept failed (error code = %d)\n", ret);
  971. return ret;
  972. }
  973. static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id,
  974. struct rdma_cm_event *event)
  975. {
  976. struct nvmet_rdma_device *ndev;
  977. struct nvmet_rdma_queue *queue;
  978. int ret = -EINVAL;
  979. ndev = nvmet_rdma_find_get_device(cm_id);
  980. if (!ndev) {
  981. nvmet_rdma_cm_reject(cm_id, NVME_RDMA_CM_NO_RSC);
  982. return -ECONNREFUSED;
  983. }
  984. queue = nvmet_rdma_alloc_queue(ndev, cm_id, event);
  985. if (!queue) {
  986. ret = -ENOMEM;
  987. goto put_device;
  988. }
  989. queue->port = cm_id->context;
  990. if (queue->host_qid == 0) {
  991. /* Let inflight controller teardown complete */
  992. flush_scheduled_work();
  993. }
  994. ret = nvmet_rdma_cm_accept(cm_id, queue, &event->param.conn);
  995. if (ret)
  996. goto release_queue;
  997. mutex_lock(&nvmet_rdma_queue_mutex);
  998. list_add_tail(&queue->queue_list, &nvmet_rdma_queue_list);
  999. mutex_unlock(&nvmet_rdma_queue_mutex);
  1000. return 0;
  1001. release_queue:
  1002. nvmet_rdma_free_queue(queue);
  1003. put_device:
  1004. kref_put(&ndev->ref, nvmet_rdma_free_dev);
  1005. return ret;
  1006. }
  1007. static void nvmet_rdma_queue_established(struct nvmet_rdma_queue *queue)
  1008. {
  1009. unsigned long flags;
  1010. spin_lock_irqsave(&queue->state_lock, flags);
  1011. if (queue->state != NVMET_RDMA_Q_CONNECTING) {
  1012. pr_warn("trying to establish a connected queue\n");
  1013. goto out_unlock;
  1014. }
  1015. queue->state = NVMET_RDMA_Q_LIVE;
  1016. while (!list_empty(&queue->rsp_wait_list)) {
  1017. struct nvmet_rdma_rsp *cmd;
  1018. cmd = list_first_entry(&queue->rsp_wait_list,
  1019. struct nvmet_rdma_rsp, wait_list);
  1020. list_del(&cmd->wait_list);
  1021. spin_unlock_irqrestore(&queue->state_lock, flags);
  1022. nvmet_rdma_handle_command(queue, cmd);
  1023. spin_lock_irqsave(&queue->state_lock, flags);
  1024. }
  1025. out_unlock:
  1026. spin_unlock_irqrestore(&queue->state_lock, flags);
  1027. }
  1028. static void __nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue)
  1029. {
  1030. bool disconnect = false;
  1031. unsigned long flags;
  1032. pr_debug("cm_id= %p queue->state= %d\n", queue->cm_id, queue->state);
  1033. spin_lock_irqsave(&queue->state_lock, flags);
  1034. switch (queue->state) {
  1035. case NVMET_RDMA_Q_CONNECTING:
  1036. case NVMET_RDMA_Q_LIVE:
  1037. queue->state = NVMET_RDMA_Q_DISCONNECTING;
  1038. case NVMET_RDMA_IN_DEVICE_REMOVAL:
  1039. disconnect = true;
  1040. break;
  1041. case NVMET_RDMA_Q_DISCONNECTING:
  1042. break;
  1043. }
  1044. spin_unlock_irqrestore(&queue->state_lock, flags);
  1045. if (disconnect) {
  1046. rdma_disconnect(queue->cm_id);
  1047. schedule_work(&queue->release_work);
  1048. }
  1049. }
  1050. static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue)
  1051. {
  1052. bool disconnect = false;
  1053. mutex_lock(&nvmet_rdma_queue_mutex);
  1054. if (!list_empty(&queue->queue_list)) {
  1055. list_del_init(&queue->queue_list);
  1056. disconnect = true;
  1057. }
  1058. mutex_unlock(&nvmet_rdma_queue_mutex);
  1059. if (disconnect)
  1060. __nvmet_rdma_queue_disconnect(queue);
  1061. }
  1062. static void nvmet_rdma_queue_connect_fail(struct rdma_cm_id *cm_id,
  1063. struct nvmet_rdma_queue *queue)
  1064. {
  1065. WARN_ON_ONCE(queue->state != NVMET_RDMA_Q_CONNECTING);
  1066. mutex_lock(&nvmet_rdma_queue_mutex);
  1067. if (!list_empty(&queue->queue_list))
  1068. list_del_init(&queue->queue_list);
  1069. mutex_unlock(&nvmet_rdma_queue_mutex);
  1070. pr_err("failed to connect queue %d\n", queue->idx);
  1071. schedule_work(&queue->release_work);
  1072. }
  1073. /**
  1074. * nvme_rdma_device_removal() - Handle RDMA device removal
  1075. * @cm_id: rdma_cm id, used for nvmet port
  1076. * @queue: nvmet rdma queue (cm id qp_context)
  1077. *
  1078. * DEVICE_REMOVAL event notifies us that the RDMA device is about
  1079. * to unplug. Note that this event can be generated on a normal
  1080. * queue cm_id and/or a device bound listener cm_id (where in this
  1081. * case queue will be null).
  1082. *
  1083. * We registered an ib_client to handle device removal for queues,
  1084. * so we only need to handle the listening port cm_ids. In this case
  1085. * we nullify the priv to prevent double cm_id destruction and destroying
  1086. * the cm_id implicitely by returning a non-zero rc to the callout.
  1087. */
  1088. static int nvmet_rdma_device_removal(struct rdma_cm_id *cm_id,
  1089. struct nvmet_rdma_queue *queue)
  1090. {
  1091. struct nvmet_port *port;
  1092. if (queue) {
  1093. /*
  1094. * This is a queue cm_id. we have registered
  1095. * an ib_client to handle queues removal
  1096. * so don't interfear and just return.
  1097. */
  1098. return 0;
  1099. }
  1100. port = cm_id->context;
  1101. /*
  1102. * This is a listener cm_id. Make sure that
  1103. * future remove_port won't invoke a double
  1104. * cm_id destroy. use atomic xchg to make sure
  1105. * we don't compete with remove_port.
  1106. */
  1107. if (xchg(&port->priv, NULL) != cm_id)
  1108. return 0;
  1109. /*
  1110. * We need to return 1 so that the core will destroy
  1111. * it's own ID. What a great API design..
  1112. */
  1113. return 1;
  1114. }
  1115. static int nvmet_rdma_cm_handler(struct rdma_cm_id *cm_id,
  1116. struct rdma_cm_event *event)
  1117. {
  1118. struct nvmet_rdma_queue *queue = NULL;
  1119. int ret = 0;
  1120. if (cm_id->qp)
  1121. queue = cm_id->qp->qp_context;
  1122. pr_debug("%s (%d): status %d id %p\n",
  1123. rdma_event_msg(event->event), event->event,
  1124. event->status, cm_id);
  1125. switch (event->event) {
  1126. case RDMA_CM_EVENT_CONNECT_REQUEST:
  1127. ret = nvmet_rdma_queue_connect(cm_id, event);
  1128. break;
  1129. case RDMA_CM_EVENT_ESTABLISHED:
  1130. nvmet_rdma_queue_established(queue);
  1131. break;
  1132. case RDMA_CM_EVENT_ADDR_CHANGE:
  1133. case RDMA_CM_EVENT_DISCONNECTED:
  1134. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  1135. /*
  1136. * We might end up here when we already freed the qp
  1137. * which means queue release sequence is in progress,
  1138. * so don't get in the way...
  1139. */
  1140. if (queue)
  1141. nvmet_rdma_queue_disconnect(queue);
  1142. break;
  1143. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  1144. ret = nvmet_rdma_device_removal(cm_id, queue);
  1145. break;
  1146. case RDMA_CM_EVENT_REJECTED:
  1147. pr_debug("Connection rejected: %s\n",
  1148. rdma_reject_msg(cm_id, event->status));
  1149. /* FALLTHROUGH */
  1150. case RDMA_CM_EVENT_UNREACHABLE:
  1151. case RDMA_CM_EVENT_CONNECT_ERROR:
  1152. nvmet_rdma_queue_connect_fail(cm_id, queue);
  1153. break;
  1154. default:
  1155. pr_err("received unrecognized RDMA CM event %d\n",
  1156. event->event);
  1157. break;
  1158. }
  1159. return ret;
  1160. }
  1161. static void nvmet_rdma_delete_ctrl(struct nvmet_ctrl *ctrl)
  1162. {
  1163. struct nvmet_rdma_queue *queue;
  1164. restart:
  1165. mutex_lock(&nvmet_rdma_queue_mutex);
  1166. list_for_each_entry(queue, &nvmet_rdma_queue_list, queue_list) {
  1167. if (queue->nvme_sq.ctrl == ctrl) {
  1168. list_del_init(&queue->queue_list);
  1169. mutex_unlock(&nvmet_rdma_queue_mutex);
  1170. __nvmet_rdma_queue_disconnect(queue);
  1171. goto restart;
  1172. }
  1173. }
  1174. mutex_unlock(&nvmet_rdma_queue_mutex);
  1175. }
  1176. static int nvmet_rdma_add_port(struct nvmet_port *port)
  1177. {
  1178. struct rdma_cm_id *cm_id;
  1179. struct sockaddr_storage addr = { };
  1180. __kernel_sa_family_t af;
  1181. int ret;
  1182. switch (port->disc_addr.adrfam) {
  1183. case NVMF_ADDR_FAMILY_IP4:
  1184. af = AF_INET;
  1185. break;
  1186. case NVMF_ADDR_FAMILY_IP6:
  1187. af = AF_INET6;
  1188. break;
  1189. default:
  1190. pr_err("address family %d not supported\n",
  1191. port->disc_addr.adrfam);
  1192. return -EINVAL;
  1193. }
  1194. ret = inet_pton_with_scope(&init_net, af, port->disc_addr.traddr,
  1195. port->disc_addr.trsvcid, &addr);
  1196. if (ret) {
  1197. pr_err("malformed ip/port passed: %s:%s\n",
  1198. port->disc_addr.traddr, port->disc_addr.trsvcid);
  1199. return ret;
  1200. }
  1201. cm_id = rdma_create_id(&init_net, nvmet_rdma_cm_handler, port,
  1202. RDMA_PS_TCP, IB_QPT_RC);
  1203. if (IS_ERR(cm_id)) {
  1204. pr_err("CM ID creation failed\n");
  1205. return PTR_ERR(cm_id);
  1206. }
  1207. /*
  1208. * Allow both IPv4 and IPv6 sockets to bind a single port
  1209. * at the same time.
  1210. */
  1211. ret = rdma_set_afonly(cm_id, 1);
  1212. if (ret) {
  1213. pr_err("rdma_set_afonly failed (%d)\n", ret);
  1214. goto out_destroy_id;
  1215. }
  1216. ret = rdma_bind_addr(cm_id, (struct sockaddr *)&addr);
  1217. if (ret) {
  1218. pr_err("binding CM ID to %pISpcs failed (%d)\n",
  1219. (struct sockaddr *)&addr, ret);
  1220. goto out_destroy_id;
  1221. }
  1222. ret = rdma_listen(cm_id, 128);
  1223. if (ret) {
  1224. pr_err("listening to %pISpcs failed (%d)\n",
  1225. (struct sockaddr *)&addr, ret);
  1226. goto out_destroy_id;
  1227. }
  1228. pr_info("enabling port %d (%pISpcs)\n",
  1229. le16_to_cpu(port->disc_addr.portid), (struct sockaddr *)&addr);
  1230. port->priv = cm_id;
  1231. return 0;
  1232. out_destroy_id:
  1233. rdma_destroy_id(cm_id);
  1234. return ret;
  1235. }
  1236. static void nvmet_rdma_remove_port(struct nvmet_port *port)
  1237. {
  1238. struct rdma_cm_id *cm_id = xchg(&port->priv, NULL);
  1239. if (cm_id)
  1240. rdma_destroy_id(cm_id);
  1241. }
  1242. static struct nvmet_fabrics_ops nvmet_rdma_ops = {
  1243. .owner = THIS_MODULE,
  1244. .type = NVMF_TRTYPE_RDMA,
  1245. .sqe_inline_size = NVMET_RDMA_INLINE_DATA_SIZE,
  1246. .msdbd = 1,
  1247. .has_keyed_sgls = 1,
  1248. .add_port = nvmet_rdma_add_port,
  1249. .remove_port = nvmet_rdma_remove_port,
  1250. .queue_response = nvmet_rdma_queue_response,
  1251. .delete_ctrl = nvmet_rdma_delete_ctrl,
  1252. };
  1253. static void nvmet_rdma_remove_one(struct ib_device *ib_device, void *client_data)
  1254. {
  1255. struct nvmet_rdma_queue *queue;
  1256. /* Device is being removed, delete all queues using this device */
  1257. mutex_lock(&nvmet_rdma_queue_mutex);
  1258. list_for_each_entry(queue, &nvmet_rdma_queue_list, queue_list) {
  1259. if (queue->dev->device != ib_device)
  1260. continue;
  1261. pr_info("Removing queue %d\n", queue->idx);
  1262. __nvmet_rdma_queue_disconnect(queue);
  1263. }
  1264. mutex_unlock(&nvmet_rdma_queue_mutex);
  1265. flush_scheduled_work();
  1266. }
  1267. static struct ib_client nvmet_rdma_ib_client = {
  1268. .name = "nvmet_rdma",
  1269. .remove = nvmet_rdma_remove_one
  1270. };
  1271. static int __init nvmet_rdma_init(void)
  1272. {
  1273. int ret;
  1274. ret = ib_register_client(&nvmet_rdma_ib_client);
  1275. if (ret)
  1276. return ret;
  1277. ret = nvmet_register_transport(&nvmet_rdma_ops);
  1278. if (ret)
  1279. goto err_ib_client;
  1280. return 0;
  1281. err_ib_client:
  1282. ib_unregister_client(&nvmet_rdma_ib_client);
  1283. return ret;
  1284. }
  1285. static void __exit nvmet_rdma_exit(void)
  1286. {
  1287. struct nvmet_rdma_queue *queue;
  1288. nvmet_unregister_transport(&nvmet_rdma_ops);
  1289. flush_scheduled_work();
  1290. mutex_lock(&nvmet_rdma_queue_mutex);
  1291. while ((queue = list_first_entry_or_null(&nvmet_rdma_queue_list,
  1292. struct nvmet_rdma_queue, queue_list))) {
  1293. list_del_init(&queue->queue_list);
  1294. mutex_unlock(&nvmet_rdma_queue_mutex);
  1295. __nvmet_rdma_queue_disconnect(queue);
  1296. mutex_lock(&nvmet_rdma_queue_mutex);
  1297. }
  1298. mutex_unlock(&nvmet_rdma_queue_mutex);
  1299. flush_scheduled_work();
  1300. ib_unregister_client(&nvmet_rdma_ib_client);
  1301. ida_destroy(&nvmet_rdma_queue_ida);
  1302. }
  1303. module_init(nvmet_rdma_init);
  1304. module_exit(nvmet_rdma_exit);
  1305. MODULE_LICENSE("GPL v2");
  1306. MODULE_ALIAS("nvmet-transport-1"); /* 1 == NVMF_TRTYPE_RDMA */