svc_rdma_transport.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. /*
  2. * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the BSD-type
  8. * license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials provided
  20. * with the distribution.
  21. *
  22. * Neither the name of the Network Appliance, Inc. nor the names of
  23. * its contributors may be used to endorse or promote products
  24. * derived from this software without specific prior written
  25. * permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * Author: Tom Tucker <tom@opengridcomputing.com>
  40. */
  41. #include <linux/sunrpc/svc_xprt.h>
  42. #include <linux/sunrpc/debug.h>
  43. #include <linux/sunrpc/rpc_rdma.h>
  44. #include <linux/spinlock.h>
  45. #include <rdma/ib_verbs.h>
  46. #include <rdma/rdma_cm.h>
  47. #include <linux/sunrpc/svc_rdma.h>
  48. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  49. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  50. struct sockaddr *sa, int salen,
  51. int flags);
  52. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
  53. static void svc_rdma_release_rqst(struct svc_rqst *);
  54. static void dto_tasklet_func(unsigned long data);
  55. static void svc_rdma_detach(struct svc_xprt *xprt);
  56. static void svc_rdma_free(struct svc_xprt *xprt);
  57. static int svc_rdma_has_wspace(struct svc_xprt *xprt);
  58. static void rq_cq_reap(struct svcxprt_rdma *xprt);
  59. static void sq_cq_reap(struct svcxprt_rdma *xprt);
  60. DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
  61. static DEFINE_SPINLOCK(dto_lock);
  62. static LIST_HEAD(dto_xprt_q);
  63. static struct svc_xprt_ops svc_rdma_ops = {
  64. .xpo_create = svc_rdma_create,
  65. .xpo_recvfrom = svc_rdma_recvfrom,
  66. .xpo_sendto = svc_rdma_sendto,
  67. .xpo_release_rqst = svc_rdma_release_rqst,
  68. .xpo_detach = svc_rdma_detach,
  69. .xpo_free = svc_rdma_free,
  70. .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
  71. .xpo_has_wspace = svc_rdma_has_wspace,
  72. .xpo_accept = svc_rdma_accept,
  73. };
  74. struct svc_xprt_class svc_rdma_class = {
  75. .xcl_name = "rdma",
  76. .xcl_owner = THIS_MODULE,
  77. .xcl_ops = &svc_rdma_ops,
  78. .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
  79. };
  80. static int rdma_bump_context_cache(struct svcxprt_rdma *xprt)
  81. {
  82. int target;
  83. int at_least_one = 0;
  84. struct svc_rdma_op_ctxt *ctxt;
  85. target = min(xprt->sc_ctxt_cnt + xprt->sc_ctxt_bump,
  86. xprt->sc_ctxt_max);
  87. spin_lock_bh(&xprt->sc_ctxt_lock);
  88. while (xprt->sc_ctxt_cnt < target) {
  89. xprt->sc_ctxt_cnt++;
  90. spin_unlock_bh(&xprt->sc_ctxt_lock);
  91. ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
  92. spin_lock_bh(&xprt->sc_ctxt_lock);
  93. if (ctxt) {
  94. at_least_one = 1;
  95. INIT_LIST_HEAD(&ctxt->free_list);
  96. list_add(&ctxt->free_list, &xprt->sc_ctxt_free);
  97. } else {
  98. /* kmalloc failed...give up for now */
  99. xprt->sc_ctxt_cnt--;
  100. break;
  101. }
  102. }
  103. spin_unlock_bh(&xprt->sc_ctxt_lock);
  104. dprintk("svcrdma: sc_ctxt_max=%d, sc_ctxt_cnt=%d\n",
  105. xprt->sc_ctxt_max, xprt->sc_ctxt_cnt);
  106. return at_least_one;
  107. }
  108. struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
  109. {
  110. struct svc_rdma_op_ctxt *ctxt;
  111. while (1) {
  112. spin_lock_bh(&xprt->sc_ctxt_lock);
  113. if (unlikely(list_empty(&xprt->sc_ctxt_free))) {
  114. /* Try to bump my cache. */
  115. spin_unlock_bh(&xprt->sc_ctxt_lock);
  116. if (rdma_bump_context_cache(xprt))
  117. continue;
  118. printk(KERN_INFO "svcrdma: sleeping waiting for "
  119. "context memory on xprt=%p\n",
  120. xprt);
  121. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  122. continue;
  123. }
  124. ctxt = list_entry(xprt->sc_ctxt_free.next,
  125. struct svc_rdma_op_ctxt,
  126. free_list);
  127. list_del_init(&ctxt->free_list);
  128. spin_unlock_bh(&xprt->sc_ctxt_lock);
  129. ctxt->xprt = xprt;
  130. INIT_LIST_HEAD(&ctxt->dto_q);
  131. ctxt->count = 0;
  132. atomic_inc(&xprt->sc_ctxt_used);
  133. break;
  134. }
  135. return ctxt;
  136. }
  137. static void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
  138. {
  139. struct svcxprt_rdma *xprt = ctxt->xprt;
  140. int i;
  141. for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
  142. atomic_dec(&xprt->sc_dma_used);
  143. ib_dma_unmap_single(xprt->sc_cm_id->device,
  144. ctxt->sge[i].addr,
  145. ctxt->sge[i].length,
  146. ctxt->direction);
  147. }
  148. }
  149. void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
  150. {
  151. struct svcxprt_rdma *xprt;
  152. int i;
  153. BUG_ON(!ctxt);
  154. xprt = ctxt->xprt;
  155. if (free_pages)
  156. for (i = 0; i < ctxt->count; i++)
  157. put_page(ctxt->pages[i]);
  158. spin_lock_bh(&xprt->sc_ctxt_lock);
  159. list_add(&ctxt->free_list, &xprt->sc_ctxt_free);
  160. spin_unlock_bh(&xprt->sc_ctxt_lock);
  161. atomic_dec(&xprt->sc_ctxt_used);
  162. }
  163. /* Temporary NFS request map cache. Created in svc_rdma.c */
  164. extern struct kmem_cache *svc_rdma_map_cachep;
  165. /*
  166. * Temporary NFS req mappings are shared across all transport
  167. * instances. These are short lived and should be bounded by the number
  168. * of concurrent server threads * depth of the SQ.
  169. */
  170. struct svc_rdma_req_map *svc_rdma_get_req_map(void)
  171. {
  172. struct svc_rdma_req_map *map;
  173. while (1) {
  174. map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
  175. if (map)
  176. break;
  177. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  178. }
  179. map->count = 0;
  180. return map;
  181. }
  182. void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
  183. {
  184. kmem_cache_free(svc_rdma_map_cachep, map);
  185. }
  186. /* ib_cq event handler */
  187. static void cq_event_handler(struct ib_event *event, void *context)
  188. {
  189. struct svc_xprt *xprt = context;
  190. dprintk("svcrdma: received CQ event id=%d, context=%p\n",
  191. event->event, context);
  192. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  193. }
  194. /* QP event handler */
  195. static void qp_event_handler(struct ib_event *event, void *context)
  196. {
  197. struct svc_xprt *xprt = context;
  198. switch (event->event) {
  199. /* These are considered benign events */
  200. case IB_EVENT_PATH_MIG:
  201. case IB_EVENT_COMM_EST:
  202. case IB_EVENT_SQ_DRAINED:
  203. case IB_EVENT_QP_LAST_WQE_REACHED:
  204. dprintk("svcrdma: QP event %d received for QP=%p\n",
  205. event->event, event->element.qp);
  206. break;
  207. /* These are considered fatal events */
  208. case IB_EVENT_PATH_MIG_ERR:
  209. case IB_EVENT_QP_FATAL:
  210. case IB_EVENT_QP_REQ_ERR:
  211. case IB_EVENT_QP_ACCESS_ERR:
  212. case IB_EVENT_DEVICE_FATAL:
  213. default:
  214. dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
  215. "closing transport\n",
  216. event->event, event->element.qp);
  217. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  218. break;
  219. }
  220. }
  221. /*
  222. * Data Transfer Operation Tasklet
  223. *
  224. * Walks a list of transports with I/O pending, removing entries as
  225. * they are added to the server's I/O pending list. Two bits indicate
  226. * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
  227. * spinlock that serializes access to the transport list with the RQ
  228. * and SQ interrupt handlers.
  229. */
  230. static void dto_tasklet_func(unsigned long data)
  231. {
  232. struct svcxprt_rdma *xprt;
  233. unsigned long flags;
  234. spin_lock_irqsave(&dto_lock, flags);
  235. while (!list_empty(&dto_xprt_q)) {
  236. xprt = list_entry(dto_xprt_q.next,
  237. struct svcxprt_rdma, sc_dto_q);
  238. list_del_init(&xprt->sc_dto_q);
  239. spin_unlock_irqrestore(&dto_lock, flags);
  240. rq_cq_reap(xprt);
  241. sq_cq_reap(xprt);
  242. svc_xprt_put(&xprt->sc_xprt);
  243. spin_lock_irqsave(&dto_lock, flags);
  244. }
  245. spin_unlock_irqrestore(&dto_lock, flags);
  246. }
  247. /*
  248. * Receive Queue Completion Handler
  249. *
  250. * Since an RQ completion handler is called on interrupt context, we
  251. * need to defer the handling of the I/O to a tasklet
  252. */
  253. static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
  254. {
  255. struct svcxprt_rdma *xprt = cq_context;
  256. unsigned long flags;
  257. /* Guard against unconditional flush call for destroyed QP */
  258. if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
  259. return;
  260. /*
  261. * Set the bit regardless of whether or not it's on the list
  262. * because it may be on the list already due to an SQ
  263. * completion.
  264. */
  265. set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
  266. /*
  267. * If this transport is not already on the DTO transport queue,
  268. * add it
  269. */
  270. spin_lock_irqsave(&dto_lock, flags);
  271. if (list_empty(&xprt->sc_dto_q)) {
  272. svc_xprt_get(&xprt->sc_xprt);
  273. list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
  274. }
  275. spin_unlock_irqrestore(&dto_lock, flags);
  276. /* Tasklet does all the work to avoid irqsave locks. */
  277. tasklet_schedule(&dto_tasklet);
  278. }
  279. /*
  280. * rq_cq_reap - Process the RQ CQ.
  281. *
  282. * Take all completing WC off the CQE and enqueue the associated DTO
  283. * context on the dto_q for the transport.
  284. *
  285. * Note that caller must hold a transport reference.
  286. */
  287. static void rq_cq_reap(struct svcxprt_rdma *xprt)
  288. {
  289. int ret;
  290. struct ib_wc wc;
  291. struct svc_rdma_op_ctxt *ctxt = NULL;
  292. if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
  293. return;
  294. ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  295. atomic_inc(&rdma_stat_rq_poll);
  296. while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
  297. ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
  298. ctxt->wc_status = wc.status;
  299. ctxt->byte_len = wc.byte_len;
  300. svc_rdma_unmap_dma(ctxt);
  301. if (wc.status != IB_WC_SUCCESS) {
  302. /* Close the transport */
  303. dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
  304. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  305. svc_rdma_put_context(ctxt, 1);
  306. svc_xprt_put(&xprt->sc_xprt);
  307. continue;
  308. }
  309. spin_lock_bh(&xprt->sc_rq_dto_lock);
  310. list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
  311. spin_unlock_bh(&xprt->sc_rq_dto_lock);
  312. svc_xprt_put(&xprt->sc_xprt);
  313. }
  314. if (ctxt)
  315. atomic_inc(&rdma_stat_rq_prod);
  316. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  317. /*
  318. * If data arrived before established event,
  319. * don't enqueue. This defers RPC I/O until the
  320. * RDMA connection is complete.
  321. */
  322. if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
  323. svc_xprt_enqueue(&xprt->sc_xprt);
  324. }
  325. /*
  326. * Send Queue Completion Handler - potentially called on interrupt context.
  327. *
  328. * Note that caller must hold a transport reference.
  329. */
  330. static void sq_cq_reap(struct svcxprt_rdma *xprt)
  331. {
  332. struct svc_rdma_op_ctxt *ctxt = NULL;
  333. struct ib_wc wc;
  334. struct ib_cq *cq = xprt->sc_sq_cq;
  335. int ret;
  336. if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
  337. return;
  338. ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  339. atomic_inc(&rdma_stat_sq_poll);
  340. while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
  341. ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
  342. xprt = ctxt->xprt;
  343. svc_rdma_unmap_dma(ctxt);
  344. if (wc.status != IB_WC_SUCCESS)
  345. /* Close the transport */
  346. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  347. /* Decrement used SQ WR count */
  348. atomic_dec(&xprt->sc_sq_count);
  349. wake_up(&xprt->sc_send_wait);
  350. switch (ctxt->wr_op) {
  351. case IB_WR_SEND:
  352. svc_rdma_put_context(ctxt, 1);
  353. break;
  354. case IB_WR_RDMA_WRITE:
  355. svc_rdma_put_context(ctxt, 0);
  356. break;
  357. case IB_WR_RDMA_READ:
  358. if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
  359. struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
  360. BUG_ON(!read_hdr);
  361. set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
  362. spin_lock_bh(&xprt->sc_read_complete_lock);
  363. list_add_tail(&read_hdr->dto_q,
  364. &xprt->sc_read_complete_q);
  365. spin_unlock_bh(&xprt->sc_read_complete_lock);
  366. svc_xprt_enqueue(&xprt->sc_xprt);
  367. }
  368. svc_rdma_put_context(ctxt, 0);
  369. break;
  370. default:
  371. printk(KERN_ERR "svcrdma: unexpected completion type, "
  372. "opcode=%d, status=%d\n",
  373. wc.opcode, wc.status);
  374. break;
  375. }
  376. svc_xprt_put(&xprt->sc_xprt);
  377. }
  378. if (ctxt)
  379. atomic_inc(&rdma_stat_sq_prod);
  380. }
  381. static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
  382. {
  383. struct svcxprt_rdma *xprt = cq_context;
  384. unsigned long flags;
  385. /* Guard against unconditional flush call for destroyed QP */
  386. if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
  387. return;
  388. /*
  389. * Set the bit regardless of whether or not it's on the list
  390. * because it may be on the list already due to an RQ
  391. * completion.
  392. */
  393. set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
  394. /*
  395. * If this transport is not already on the DTO transport queue,
  396. * add it
  397. */
  398. spin_lock_irqsave(&dto_lock, flags);
  399. if (list_empty(&xprt->sc_dto_q)) {
  400. svc_xprt_get(&xprt->sc_xprt);
  401. list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
  402. }
  403. spin_unlock_irqrestore(&dto_lock, flags);
  404. /* Tasklet does all the work to avoid irqsave locks. */
  405. tasklet_schedule(&dto_tasklet);
  406. }
  407. static void create_context_cache(struct svcxprt_rdma *xprt,
  408. int ctxt_count, int ctxt_bump, int ctxt_max)
  409. {
  410. struct svc_rdma_op_ctxt *ctxt;
  411. int i;
  412. xprt->sc_ctxt_max = ctxt_max;
  413. xprt->sc_ctxt_bump = ctxt_bump;
  414. xprt->sc_ctxt_cnt = 0;
  415. atomic_set(&xprt->sc_ctxt_used, 0);
  416. INIT_LIST_HEAD(&xprt->sc_ctxt_free);
  417. for (i = 0; i < ctxt_count; i++) {
  418. ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
  419. if (ctxt) {
  420. INIT_LIST_HEAD(&ctxt->free_list);
  421. list_add(&ctxt->free_list, &xprt->sc_ctxt_free);
  422. xprt->sc_ctxt_cnt++;
  423. }
  424. }
  425. }
  426. static void destroy_context_cache(struct svcxprt_rdma *xprt)
  427. {
  428. while (!list_empty(&xprt->sc_ctxt_free)) {
  429. struct svc_rdma_op_ctxt *ctxt;
  430. ctxt = list_entry(xprt->sc_ctxt_free.next,
  431. struct svc_rdma_op_ctxt,
  432. free_list);
  433. list_del_init(&ctxt->free_list);
  434. kfree(ctxt);
  435. }
  436. }
  437. static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
  438. int listener)
  439. {
  440. struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
  441. if (!cma_xprt)
  442. return NULL;
  443. svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
  444. INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
  445. INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
  446. INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
  447. INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
  448. init_waitqueue_head(&cma_xprt->sc_send_wait);
  449. spin_lock_init(&cma_xprt->sc_lock);
  450. spin_lock_init(&cma_xprt->sc_read_complete_lock);
  451. spin_lock_init(&cma_xprt->sc_ctxt_lock);
  452. spin_lock_init(&cma_xprt->sc_rq_dto_lock);
  453. cma_xprt->sc_ord = svcrdma_ord;
  454. cma_xprt->sc_max_req_size = svcrdma_max_req_size;
  455. cma_xprt->sc_max_requests = svcrdma_max_requests;
  456. cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
  457. atomic_set(&cma_xprt->sc_sq_count, 0);
  458. atomic_set(&cma_xprt->sc_ctxt_used, 0);
  459. if (!listener) {
  460. int reqs = cma_xprt->sc_max_requests;
  461. create_context_cache(cma_xprt,
  462. reqs << 1, /* starting size */
  463. reqs, /* bump amount */
  464. reqs +
  465. cma_xprt->sc_sq_depth +
  466. RPCRDMA_MAX_THREADS + 1); /* max */
  467. if (list_empty(&cma_xprt->sc_ctxt_free)) {
  468. kfree(cma_xprt);
  469. return NULL;
  470. }
  471. clear_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
  472. } else
  473. set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
  474. return cma_xprt;
  475. }
  476. struct page *svc_rdma_get_page(void)
  477. {
  478. struct page *page;
  479. while ((page = alloc_page(GFP_KERNEL)) == NULL) {
  480. /* If we can't get memory, wait a bit and try again */
  481. printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
  482. "jiffies.\n");
  483. schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
  484. }
  485. return page;
  486. }
  487. int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
  488. {
  489. struct ib_recv_wr recv_wr, *bad_recv_wr;
  490. struct svc_rdma_op_ctxt *ctxt;
  491. struct page *page;
  492. unsigned long pa;
  493. int sge_no;
  494. int buflen;
  495. int ret;
  496. ctxt = svc_rdma_get_context(xprt);
  497. buflen = 0;
  498. ctxt->direction = DMA_FROM_DEVICE;
  499. for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
  500. BUG_ON(sge_no >= xprt->sc_max_sge);
  501. page = svc_rdma_get_page();
  502. ctxt->pages[sge_no] = page;
  503. atomic_inc(&xprt->sc_dma_used);
  504. pa = ib_dma_map_page(xprt->sc_cm_id->device,
  505. page, 0, PAGE_SIZE,
  506. DMA_FROM_DEVICE);
  507. ctxt->sge[sge_no].addr = pa;
  508. ctxt->sge[sge_no].length = PAGE_SIZE;
  509. ctxt->sge[sge_no].lkey = xprt->sc_phys_mr->lkey;
  510. buflen += PAGE_SIZE;
  511. }
  512. ctxt->count = sge_no;
  513. recv_wr.next = NULL;
  514. recv_wr.sg_list = &ctxt->sge[0];
  515. recv_wr.num_sge = ctxt->count;
  516. recv_wr.wr_id = (u64)(unsigned long)ctxt;
  517. svc_xprt_get(&xprt->sc_xprt);
  518. ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
  519. if (ret) {
  520. svc_xprt_put(&xprt->sc_xprt);
  521. svc_rdma_put_context(ctxt, 1);
  522. }
  523. return ret;
  524. }
  525. /*
  526. * This function handles the CONNECT_REQUEST event on a listening
  527. * endpoint. It is passed the cma_id for the _new_ connection. The context in
  528. * this cma_id is inherited from the listening cma_id and is the svc_xprt
  529. * structure for the listening endpoint.
  530. *
  531. * This function creates a new xprt for the new connection and enqueues it on
  532. * the accept queue for the listent xprt. When the listen thread is kicked, it
  533. * will call the recvfrom method on the listen xprt which will accept the new
  534. * connection.
  535. */
  536. static void handle_connect_req(struct rdma_cm_id *new_cma_id)
  537. {
  538. struct svcxprt_rdma *listen_xprt = new_cma_id->context;
  539. struct svcxprt_rdma *newxprt;
  540. struct sockaddr *sa;
  541. /* Create a new transport */
  542. newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
  543. if (!newxprt) {
  544. dprintk("svcrdma: failed to create new transport\n");
  545. return;
  546. }
  547. newxprt->sc_cm_id = new_cma_id;
  548. new_cma_id->context = newxprt;
  549. dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
  550. newxprt, newxprt->sc_cm_id, listen_xprt);
  551. /* Set the local and remote addresses in the transport */
  552. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
  553. svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  554. sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
  555. svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
  556. /*
  557. * Enqueue the new transport on the accept queue of the listening
  558. * transport
  559. */
  560. spin_lock_bh(&listen_xprt->sc_lock);
  561. list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
  562. spin_unlock_bh(&listen_xprt->sc_lock);
  563. /*
  564. * Can't use svc_xprt_received here because we are not on a
  565. * rqstp thread
  566. */
  567. set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
  568. svc_xprt_enqueue(&listen_xprt->sc_xprt);
  569. }
  570. /*
  571. * Handles events generated on the listening endpoint. These events will be
  572. * either be incoming connect requests or adapter removal events.
  573. */
  574. static int rdma_listen_handler(struct rdma_cm_id *cma_id,
  575. struct rdma_cm_event *event)
  576. {
  577. struct svcxprt_rdma *xprt = cma_id->context;
  578. int ret = 0;
  579. switch (event->event) {
  580. case RDMA_CM_EVENT_CONNECT_REQUEST:
  581. dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
  582. "event=%d\n", cma_id, cma_id->context, event->event);
  583. handle_connect_req(cma_id);
  584. break;
  585. case RDMA_CM_EVENT_ESTABLISHED:
  586. /* Accept complete */
  587. dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
  588. "cm_id=%p\n", xprt, cma_id);
  589. break;
  590. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  591. dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
  592. xprt, cma_id);
  593. if (xprt)
  594. set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
  595. break;
  596. default:
  597. dprintk("svcrdma: Unexpected event on listening endpoint %p, "
  598. "event=%d\n", cma_id, event->event);
  599. break;
  600. }
  601. return ret;
  602. }
  603. static int rdma_cma_handler(struct rdma_cm_id *cma_id,
  604. struct rdma_cm_event *event)
  605. {
  606. struct svc_xprt *xprt = cma_id->context;
  607. struct svcxprt_rdma *rdma =
  608. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  609. switch (event->event) {
  610. case RDMA_CM_EVENT_ESTABLISHED:
  611. /* Accept complete */
  612. svc_xprt_get(xprt);
  613. dprintk("svcrdma: Connection completed on DTO xprt=%p, "
  614. "cm_id=%p\n", xprt, cma_id);
  615. clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
  616. svc_xprt_enqueue(xprt);
  617. break;
  618. case RDMA_CM_EVENT_DISCONNECTED:
  619. dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
  620. xprt, cma_id);
  621. if (xprt) {
  622. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  623. svc_xprt_enqueue(xprt);
  624. svc_xprt_put(xprt);
  625. }
  626. break;
  627. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  628. dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
  629. "event=%d\n", cma_id, xprt, event->event);
  630. if (xprt) {
  631. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  632. svc_xprt_enqueue(xprt);
  633. }
  634. break;
  635. default:
  636. dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
  637. "event=%d\n", cma_id, event->event);
  638. break;
  639. }
  640. return 0;
  641. }
  642. /*
  643. * Create a listening RDMA service endpoint.
  644. */
  645. static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  646. struct sockaddr *sa, int salen,
  647. int flags)
  648. {
  649. struct rdma_cm_id *listen_id;
  650. struct svcxprt_rdma *cma_xprt;
  651. struct svc_xprt *xprt;
  652. int ret;
  653. dprintk("svcrdma: Creating RDMA socket\n");
  654. cma_xprt = rdma_create_xprt(serv, 1);
  655. if (!cma_xprt)
  656. return ERR_PTR(-ENOMEM);
  657. xprt = &cma_xprt->sc_xprt;
  658. listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
  659. if (IS_ERR(listen_id)) {
  660. ret = PTR_ERR(listen_id);
  661. dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
  662. goto err0;
  663. }
  664. ret = rdma_bind_addr(listen_id, sa);
  665. if (ret) {
  666. dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
  667. goto err1;
  668. }
  669. cma_xprt->sc_cm_id = listen_id;
  670. ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
  671. if (ret) {
  672. dprintk("svcrdma: rdma_listen failed = %d\n", ret);
  673. goto err1;
  674. }
  675. /*
  676. * We need to use the address from the cm_id in case the
  677. * caller specified 0 for the port number.
  678. */
  679. sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
  680. svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
  681. return &cma_xprt->sc_xprt;
  682. err1:
  683. rdma_destroy_id(listen_id);
  684. err0:
  685. kfree(cma_xprt);
  686. return ERR_PTR(ret);
  687. }
  688. /*
  689. * This is the xpo_recvfrom function for listening endpoints. Its
  690. * purpose is to accept incoming connections. The CMA callback handler
  691. * has already created a new transport and attached it to the new CMA
  692. * ID.
  693. *
  694. * There is a queue of pending connections hung on the listening
  695. * transport. This queue contains the new svc_xprt structure. This
  696. * function takes svc_xprt structures off the accept_q and completes
  697. * the connection.
  698. */
  699. static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
  700. {
  701. struct svcxprt_rdma *listen_rdma;
  702. struct svcxprt_rdma *newxprt = NULL;
  703. struct rdma_conn_param conn_param;
  704. struct ib_qp_init_attr qp_attr;
  705. struct ib_device_attr devattr;
  706. int ret;
  707. int i;
  708. listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
  709. clear_bit(XPT_CONN, &xprt->xpt_flags);
  710. /* Get the next entry off the accept list */
  711. spin_lock_bh(&listen_rdma->sc_lock);
  712. if (!list_empty(&listen_rdma->sc_accept_q)) {
  713. newxprt = list_entry(listen_rdma->sc_accept_q.next,
  714. struct svcxprt_rdma, sc_accept_q);
  715. list_del_init(&newxprt->sc_accept_q);
  716. }
  717. if (!list_empty(&listen_rdma->sc_accept_q))
  718. set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
  719. spin_unlock_bh(&listen_rdma->sc_lock);
  720. if (!newxprt)
  721. return NULL;
  722. dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
  723. newxprt, newxprt->sc_cm_id);
  724. ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
  725. if (ret) {
  726. dprintk("svcrdma: could not query device attributes on "
  727. "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
  728. goto errout;
  729. }
  730. /* Qualify the transport resource defaults with the
  731. * capabilities of this particular device */
  732. newxprt->sc_max_sge = min((size_t)devattr.max_sge,
  733. (size_t)RPCSVC_MAXPAGES);
  734. newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
  735. (size_t)svcrdma_max_requests);
  736. newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
  737. newxprt->sc_ord = min((size_t)devattr.max_qp_rd_atom,
  738. (size_t)svcrdma_ord);
  739. newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
  740. if (IS_ERR(newxprt->sc_pd)) {
  741. dprintk("svcrdma: error creating PD for connect request\n");
  742. goto errout;
  743. }
  744. newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  745. sq_comp_handler,
  746. cq_event_handler,
  747. newxprt,
  748. newxprt->sc_sq_depth,
  749. 0);
  750. if (IS_ERR(newxprt->sc_sq_cq)) {
  751. dprintk("svcrdma: error creating SQ CQ for connect request\n");
  752. goto errout;
  753. }
  754. newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
  755. rq_comp_handler,
  756. cq_event_handler,
  757. newxprt,
  758. newxprt->sc_max_requests,
  759. 0);
  760. if (IS_ERR(newxprt->sc_rq_cq)) {
  761. dprintk("svcrdma: error creating RQ CQ for connect request\n");
  762. goto errout;
  763. }
  764. memset(&qp_attr, 0, sizeof qp_attr);
  765. qp_attr.event_handler = qp_event_handler;
  766. qp_attr.qp_context = &newxprt->sc_xprt;
  767. qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
  768. qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
  769. qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
  770. qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
  771. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  772. qp_attr.qp_type = IB_QPT_RC;
  773. qp_attr.send_cq = newxprt->sc_sq_cq;
  774. qp_attr.recv_cq = newxprt->sc_rq_cq;
  775. dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
  776. " cm_id->device=%p, sc_pd->device=%p\n"
  777. " cap.max_send_wr = %d\n"
  778. " cap.max_recv_wr = %d\n"
  779. " cap.max_send_sge = %d\n"
  780. " cap.max_recv_sge = %d\n",
  781. newxprt->sc_cm_id, newxprt->sc_pd,
  782. newxprt->sc_cm_id->device, newxprt->sc_pd->device,
  783. qp_attr.cap.max_send_wr,
  784. qp_attr.cap.max_recv_wr,
  785. qp_attr.cap.max_send_sge,
  786. qp_attr.cap.max_recv_sge);
  787. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
  788. if (ret) {
  789. /*
  790. * XXX: This is a hack. We need a xx_request_qp interface
  791. * that will adjust the qp_attr's with a best-effort
  792. * number
  793. */
  794. qp_attr.cap.max_send_sge -= 2;
  795. qp_attr.cap.max_recv_sge -= 2;
  796. ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
  797. &qp_attr);
  798. if (ret) {
  799. dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
  800. goto errout;
  801. }
  802. newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
  803. newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
  804. newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
  805. newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
  806. }
  807. newxprt->sc_qp = newxprt->sc_cm_id->qp;
  808. /* Register all of physical memory */
  809. newxprt->sc_phys_mr = ib_get_dma_mr(newxprt->sc_pd,
  810. IB_ACCESS_LOCAL_WRITE |
  811. IB_ACCESS_REMOTE_WRITE);
  812. if (IS_ERR(newxprt->sc_phys_mr)) {
  813. dprintk("svcrdma: Failed to create DMA MR ret=%d\n", ret);
  814. goto errout;
  815. }
  816. /* Post receive buffers */
  817. for (i = 0; i < newxprt->sc_max_requests; i++) {
  818. ret = svc_rdma_post_recv(newxprt);
  819. if (ret) {
  820. dprintk("svcrdma: failure posting receive buffers\n");
  821. goto errout;
  822. }
  823. }
  824. /* Swap out the handler */
  825. newxprt->sc_cm_id->event_handler = rdma_cma_handler;
  826. /*
  827. * Arm the CQs for the SQ and RQ before accepting so we can't
  828. * miss the first message
  829. */
  830. ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
  831. ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
  832. /* Accept Connection */
  833. set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
  834. memset(&conn_param, 0, sizeof conn_param);
  835. conn_param.responder_resources = 0;
  836. conn_param.initiator_depth = newxprt->sc_ord;
  837. ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
  838. if (ret) {
  839. dprintk("svcrdma: failed to accept new connection, ret=%d\n",
  840. ret);
  841. goto errout;
  842. }
  843. dprintk("svcrdma: new connection %p accepted with the following "
  844. "attributes:\n"
  845. " local_ip : %d.%d.%d.%d\n"
  846. " local_port : %d\n"
  847. " remote_ip : %d.%d.%d.%d\n"
  848. " remote_port : %d\n"
  849. " max_sge : %d\n"
  850. " sq_depth : %d\n"
  851. " max_requests : %d\n"
  852. " ord : %d\n",
  853. newxprt,
  854. NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
  855. route.addr.src_addr)->sin_addr.s_addr),
  856. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  857. route.addr.src_addr)->sin_port),
  858. NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
  859. route.addr.dst_addr)->sin_addr.s_addr),
  860. ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
  861. route.addr.dst_addr)->sin_port),
  862. newxprt->sc_max_sge,
  863. newxprt->sc_sq_depth,
  864. newxprt->sc_max_requests,
  865. newxprt->sc_ord);
  866. return &newxprt->sc_xprt;
  867. errout:
  868. dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
  869. /* Take a reference in case the DTO handler runs */
  870. svc_xprt_get(&newxprt->sc_xprt);
  871. if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
  872. ib_destroy_qp(newxprt->sc_qp);
  873. rdma_destroy_id(newxprt->sc_cm_id);
  874. /* This call to put will destroy the transport */
  875. svc_xprt_put(&newxprt->sc_xprt);
  876. return NULL;
  877. }
  878. static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
  879. {
  880. }
  881. /*
  882. * When connected, an svc_xprt has at least two references:
  883. *
  884. * - A reference held by the cm_id between the ESTABLISHED and
  885. * DISCONNECTED events. If the remote peer disconnected first, this
  886. * reference could be gone.
  887. *
  888. * - A reference held by the svc_recv code that called this function
  889. * as part of close processing.
  890. *
  891. * At a minimum one references should still be held.
  892. */
  893. static void svc_rdma_detach(struct svc_xprt *xprt)
  894. {
  895. struct svcxprt_rdma *rdma =
  896. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  897. dprintk("svc: svc_rdma_detach(%p)\n", xprt);
  898. /* Disconnect and flush posted WQE */
  899. rdma_disconnect(rdma->sc_cm_id);
  900. }
  901. static void __svc_rdma_free(struct work_struct *work)
  902. {
  903. struct svcxprt_rdma *rdma =
  904. container_of(work, struct svcxprt_rdma, sc_work);
  905. dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
  906. /* We should only be called from kref_put */
  907. BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
  908. /*
  909. * Destroy queued, but not processed read completions. Note
  910. * that this cleanup has to be done before destroying the
  911. * cm_id because the device ptr is needed to unmap the dma in
  912. * svc_rdma_put_context.
  913. */
  914. spin_lock_bh(&rdma->sc_read_complete_lock);
  915. while (!list_empty(&rdma->sc_read_complete_q)) {
  916. struct svc_rdma_op_ctxt *ctxt;
  917. ctxt = list_entry(rdma->sc_read_complete_q.next,
  918. struct svc_rdma_op_ctxt,
  919. dto_q);
  920. list_del_init(&ctxt->dto_q);
  921. svc_rdma_put_context(ctxt, 1);
  922. }
  923. spin_unlock_bh(&rdma->sc_read_complete_lock);
  924. /* Destroy queued, but not processed recv completions */
  925. spin_lock_bh(&rdma->sc_rq_dto_lock);
  926. while (!list_empty(&rdma->sc_rq_dto_q)) {
  927. struct svc_rdma_op_ctxt *ctxt;
  928. ctxt = list_entry(rdma->sc_rq_dto_q.next,
  929. struct svc_rdma_op_ctxt,
  930. dto_q);
  931. list_del_init(&ctxt->dto_q);
  932. svc_rdma_put_context(ctxt, 1);
  933. }
  934. spin_unlock_bh(&rdma->sc_rq_dto_lock);
  935. /* Warn if we leaked a resource or under-referenced */
  936. WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
  937. WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
  938. /* Destroy the QP if present (not a listener) */
  939. if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
  940. ib_destroy_qp(rdma->sc_qp);
  941. if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
  942. ib_destroy_cq(rdma->sc_sq_cq);
  943. if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
  944. ib_destroy_cq(rdma->sc_rq_cq);
  945. if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
  946. ib_dereg_mr(rdma->sc_phys_mr);
  947. if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
  948. ib_dealloc_pd(rdma->sc_pd);
  949. /* Destroy the CM ID */
  950. rdma_destroy_id(rdma->sc_cm_id);
  951. destroy_context_cache(rdma);
  952. kfree(rdma);
  953. }
  954. static void svc_rdma_free(struct svc_xprt *xprt)
  955. {
  956. struct svcxprt_rdma *rdma =
  957. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  958. INIT_WORK(&rdma->sc_work, __svc_rdma_free);
  959. schedule_work(&rdma->sc_work);
  960. }
  961. static int svc_rdma_has_wspace(struct svc_xprt *xprt)
  962. {
  963. struct svcxprt_rdma *rdma =
  964. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  965. /*
  966. * If there are fewer SQ WR available than required to send a
  967. * simple response, return false.
  968. */
  969. if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
  970. return 0;
  971. /*
  972. * ...or there are already waiters on the SQ,
  973. * return false.
  974. */
  975. if (waitqueue_active(&rdma->sc_send_wait))
  976. return 0;
  977. /* Otherwise return true. */
  978. return 1;
  979. }
  980. int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
  981. {
  982. struct ib_send_wr *bad_wr;
  983. int ret;
  984. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  985. return -ENOTCONN;
  986. BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
  987. BUG_ON(((struct svc_rdma_op_ctxt *)(unsigned long)wr->wr_id)->wr_op !=
  988. wr->opcode);
  989. /* If the SQ is full, wait until an SQ entry is available */
  990. while (1) {
  991. spin_lock_bh(&xprt->sc_lock);
  992. if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
  993. spin_unlock_bh(&xprt->sc_lock);
  994. atomic_inc(&rdma_stat_sq_starve);
  995. /* See if we can opportunistically reap SQ WR to make room */
  996. sq_cq_reap(xprt);
  997. /* Wait until SQ WR available if SQ still full */
  998. wait_event(xprt->sc_send_wait,
  999. atomic_read(&xprt->sc_sq_count) <
  1000. xprt->sc_sq_depth);
  1001. if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
  1002. return 0;
  1003. continue;
  1004. }
  1005. /* Bumped used SQ WR count and post */
  1006. svc_xprt_get(&xprt->sc_xprt);
  1007. ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
  1008. if (!ret)
  1009. atomic_inc(&xprt->sc_sq_count);
  1010. else {
  1011. svc_xprt_put(&xprt->sc_xprt);
  1012. dprintk("svcrdma: failed to post SQ WR rc=%d, "
  1013. "sc_sq_count=%d, sc_sq_depth=%d\n",
  1014. ret, atomic_read(&xprt->sc_sq_count),
  1015. xprt->sc_sq_depth);
  1016. }
  1017. spin_unlock_bh(&xprt->sc_lock);
  1018. break;
  1019. }
  1020. return ret;
  1021. }
  1022. void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
  1023. enum rpcrdma_errcode err)
  1024. {
  1025. struct ib_send_wr err_wr;
  1026. struct ib_sge sge;
  1027. struct page *p;
  1028. struct svc_rdma_op_ctxt *ctxt;
  1029. u32 *va;
  1030. int length;
  1031. int ret;
  1032. p = svc_rdma_get_page();
  1033. va = page_address(p);
  1034. /* XDR encode error */
  1035. length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
  1036. /* Prepare SGE for local address */
  1037. atomic_inc(&xprt->sc_dma_used);
  1038. sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
  1039. p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  1040. sge.lkey = xprt->sc_phys_mr->lkey;
  1041. sge.length = length;
  1042. ctxt = svc_rdma_get_context(xprt);
  1043. ctxt->count = 1;
  1044. ctxt->pages[0] = p;
  1045. /* Prepare SEND WR */
  1046. memset(&err_wr, 0, sizeof err_wr);
  1047. ctxt->wr_op = IB_WR_SEND;
  1048. err_wr.wr_id = (unsigned long)ctxt;
  1049. err_wr.sg_list = &sge;
  1050. err_wr.num_sge = 1;
  1051. err_wr.opcode = IB_WR_SEND;
  1052. err_wr.send_flags = IB_SEND_SIGNALED;
  1053. /* Post It */
  1054. ret = svc_rdma_send(xprt, &err_wr);
  1055. if (ret) {
  1056. dprintk("svcrdma: Error %d posting send for protocol error\n",
  1057. ret);
  1058. svc_rdma_put_context(ctxt, 1);
  1059. }
  1060. }