verbs.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  1. /*
  2. * Copyright (c) 2003-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. /*
  40. * verbs.c
  41. *
  42. * Encapsulates the major functions managing:
  43. * o adapters
  44. * o endpoints
  45. * o connections
  46. * o buffer memory
  47. */
  48. #include <linux/interrupt.h>
  49. #include <linux/slab.h>
  50. #include <asm/bitops.h>
  51. #include "xprt_rdma.h"
  52. /*
  53. * Globals/Macros
  54. */
  55. #ifdef RPC_DEBUG
  56. # define RPCDBG_FACILITY RPCDBG_TRANS
  57. #endif
  58. /*
  59. * internal functions
  60. */
  61. /*
  62. * handle replies in tasklet context, using a single, global list
  63. * rdma tasklet function -- just turn around and call the func
  64. * for all replies on the list
  65. */
  66. static DEFINE_SPINLOCK(rpcrdma_tk_lock_g);
  67. static LIST_HEAD(rpcrdma_tasklets_g);
  68. static void
  69. rpcrdma_run_tasklet(unsigned long data)
  70. {
  71. struct rpcrdma_rep *rep;
  72. void (*func)(struct rpcrdma_rep *);
  73. unsigned long flags;
  74. data = data;
  75. spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
  76. while (!list_empty(&rpcrdma_tasklets_g)) {
  77. rep = list_entry(rpcrdma_tasklets_g.next,
  78. struct rpcrdma_rep, rr_list);
  79. list_del(&rep->rr_list);
  80. func = rep->rr_func;
  81. rep->rr_func = NULL;
  82. spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
  83. if (func)
  84. func(rep);
  85. else
  86. rpcrdma_recv_buffer_put(rep);
  87. spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
  88. }
  89. spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
  90. }
  91. static DECLARE_TASKLET(rpcrdma_tasklet_g, rpcrdma_run_tasklet, 0UL);
  92. static inline void
  93. rpcrdma_schedule_tasklet(struct rpcrdma_rep *rep)
  94. {
  95. unsigned long flags;
  96. spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
  97. list_add_tail(&rep->rr_list, &rpcrdma_tasklets_g);
  98. spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
  99. tasklet_schedule(&rpcrdma_tasklet_g);
  100. }
  101. static void
  102. rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
  103. {
  104. struct rpcrdma_ep *ep = context;
  105. dprintk("RPC: %s: QP error %X on device %s ep %p\n",
  106. __func__, event->event, event->device->name, context);
  107. if (ep->rep_connected == 1) {
  108. ep->rep_connected = -EIO;
  109. ep->rep_func(ep);
  110. wake_up_all(&ep->rep_connect_wait);
  111. }
  112. }
  113. static void
  114. rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context)
  115. {
  116. struct rpcrdma_ep *ep = context;
  117. dprintk("RPC: %s: CQ error %X on device %s ep %p\n",
  118. __func__, event->event, event->device->name, context);
  119. if (ep->rep_connected == 1) {
  120. ep->rep_connected = -EIO;
  121. ep->rep_func(ep);
  122. wake_up_all(&ep->rep_connect_wait);
  123. }
  124. }
  125. static void
  126. rpcrdma_sendcq_process_wc(struct ib_wc *wc)
  127. {
  128. struct rpcrdma_mw *frmr = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
  129. dprintk("RPC: %s: frmr %p status %X opcode %d\n",
  130. __func__, frmr, wc->status, wc->opcode);
  131. if (wc->wr_id == 0ULL)
  132. return;
  133. if (wc->status != IB_WC_SUCCESS)
  134. return;
  135. if (wc->opcode == IB_WC_FAST_REG_MR)
  136. frmr->r.frmr.state = FRMR_IS_VALID;
  137. else if (wc->opcode == IB_WC_LOCAL_INV)
  138. frmr->r.frmr.state = FRMR_IS_INVALID;
  139. }
  140. static int
  141. rpcrdma_sendcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
  142. {
  143. struct ib_wc *wcs;
  144. int budget, count, rc;
  145. budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
  146. do {
  147. wcs = ep->rep_send_wcs;
  148. rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
  149. if (rc <= 0)
  150. return rc;
  151. count = rc;
  152. while (count-- > 0)
  153. rpcrdma_sendcq_process_wc(wcs++);
  154. } while (rc == RPCRDMA_POLLSIZE && --budget);
  155. return 0;
  156. }
  157. /*
  158. * Handle send, fast_reg_mr, and local_inv completions.
  159. *
  160. * Send events are typically suppressed and thus do not result
  161. * in an upcall. Occasionally one is signaled, however. This
  162. * prevents the provider's completion queue from wrapping and
  163. * losing a completion.
  164. */
  165. static void
  166. rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
  167. {
  168. struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
  169. int rc;
  170. rc = rpcrdma_sendcq_poll(cq, ep);
  171. if (rc) {
  172. dprintk("RPC: %s: ib_poll_cq failed: %i\n",
  173. __func__, rc);
  174. return;
  175. }
  176. rc = ib_req_notify_cq(cq,
  177. IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
  178. if (rc == 0)
  179. return;
  180. if (rc < 0) {
  181. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  182. __func__, rc);
  183. return;
  184. }
  185. rpcrdma_sendcq_poll(cq, ep);
  186. }
  187. static void
  188. rpcrdma_recvcq_process_wc(struct ib_wc *wc)
  189. {
  190. struct rpcrdma_rep *rep =
  191. (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
  192. dprintk("RPC: %s: rep %p status %X opcode %X length %u\n",
  193. __func__, rep, wc->status, wc->opcode, wc->byte_len);
  194. if (wc->status != IB_WC_SUCCESS) {
  195. rep->rr_len = ~0U;
  196. goto out_schedule;
  197. }
  198. if (wc->opcode != IB_WC_RECV)
  199. return;
  200. rep->rr_len = wc->byte_len;
  201. ib_dma_sync_single_for_cpu(rdmab_to_ia(rep->rr_buffer)->ri_id->device,
  202. rep->rr_iov.addr, rep->rr_len, DMA_FROM_DEVICE);
  203. if (rep->rr_len >= 16) {
  204. struct rpcrdma_msg *p = (struct rpcrdma_msg *)rep->rr_base;
  205. unsigned int credits = ntohl(p->rm_credit);
  206. if (credits == 0)
  207. credits = 1; /* don't deadlock */
  208. else if (credits > rep->rr_buffer->rb_max_requests)
  209. credits = rep->rr_buffer->rb_max_requests;
  210. atomic_set(&rep->rr_buffer->rb_credits, credits);
  211. }
  212. out_schedule:
  213. rpcrdma_schedule_tasklet(rep);
  214. }
  215. static int
  216. rpcrdma_recvcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
  217. {
  218. struct ib_wc *wcs;
  219. int budget, count, rc;
  220. budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
  221. do {
  222. wcs = ep->rep_recv_wcs;
  223. rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
  224. if (rc <= 0)
  225. return rc;
  226. count = rc;
  227. while (count-- > 0)
  228. rpcrdma_recvcq_process_wc(wcs++);
  229. } while (rc == RPCRDMA_POLLSIZE && --budget);
  230. return 0;
  231. }
  232. /*
  233. * Handle receive completions.
  234. *
  235. * It is reentrant but processes single events in order to maintain
  236. * ordering of receives to keep server credits.
  237. *
  238. * It is the responsibility of the scheduled tasklet to return
  239. * recv buffers to the pool. NOTE: this affects synchronization of
  240. * connection shutdown. That is, the structures required for
  241. * the completion of the reply handler must remain intact until
  242. * all memory has been reclaimed.
  243. */
  244. static void
  245. rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
  246. {
  247. struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
  248. int rc;
  249. rc = rpcrdma_recvcq_poll(cq, ep);
  250. if (rc) {
  251. dprintk("RPC: %s: ib_poll_cq failed: %i\n",
  252. __func__, rc);
  253. return;
  254. }
  255. rc = ib_req_notify_cq(cq,
  256. IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
  257. if (rc == 0)
  258. return;
  259. if (rc < 0) {
  260. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  261. __func__, rc);
  262. return;
  263. }
  264. rpcrdma_recvcq_poll(cq, ep);
  265. }
  266. #ifdef RPC_DEBUG
  267. static const char * const conn[] = {
  268. "address resolved",
  269. "address error",
  270. "route resolved",
  271. "route error",
  272. "connect request",
  273. "connect response",
  274. "connect error",
  275. "unreachable",
  276. "rejected",
  277. "established",
  278. "disconnected",
  279. "device removal"
  280. };
  281. #endif
  282. static int
  283. rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
  284. {
  285. struct rpcrdma_xprt *xprt = id->context;
  286. struct rpcrdma_ia *ia = &xprt->rx_ia;
  287. struct rpcrdma_ep *ep = &xprt->rx_ep;
  288. #ifdef RPC_DEBUG
  289. struct sockaddr_in *addr = (struct sockaddr_in *) &ep->rep_remote_addr;
  290. #endif
  291. struct ib_qp_attr attr;
  292. struct ib_qp_init_attr iattr;
  293. int connstate = 0;
  294. switch (event->event) {
  295. case RDMA_CM_EVENT_ADDR_RESOLVED:
  296. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  297. ia->ri_async_rc = 0;
  298. complete(&ia->ri_done);
  299. break;
  300. case RDMA_CM_EVENT_ADDR_ERROR:
  301. ia->ri_async_rc = -EHOSTUNREACH;
  302. dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
  303. __func__, ep);
  304. complete(&ia->ri_done);
  305. break;
  306. case RDMA_CM_EVENT_ROUTE_ERROR:
  307. ia->ri_async_rc = -ENETUNREACH;
  308. dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
  309. __func__, ep);
  310. complete(&ia->ri_done);
  311. break;
  312. case RDMA_CM_EVENT_ESTABLISHED:
  313. connstate = 1;
  314. ib_query_qp(ia->ri_id->qp, &attr,
  315. IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
  316. &iattr);
  317. dprintk("RPC: %s: %d responder resources"
  318. " (%d initiator)\n",
  319. __func__, attr.max_dest_rd_atomic, attr.max_rd_atomic);
  320. goto connected;
  321. case RDMA_CM_EVENT_CONNECT_ERROR:
  322. connstate = -ENOTCONN;
  323. goto connected;
  324. case RDMA_CM_EVENT_UNREACHABLE:
  325. connstate = -ENETDOWN;
  326. goto connected;
  327. case RDMA_CM_EVENT_REJECTED:
  328. connstate = -ECONNREFUSED;
  329. goto connected;
  330. case RDMA_CM_EVENT_DISCONNECTED:
  331. connstate = -ECONNABORTED;
  332. goto connected;
  333. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  334. connstate = -ENODEV;
  335. connected:
  336. dprintk("RPC: %s: %s: %pI4:%u (ep 0x%p event 0x%x)\n",
  337. __func__,
  338. (event->event <= 11) ? conn[event->event] :
  339. "unknown connection error",
  340. &addr->sin_addr.s_addr,
  341. ntohs(addr->sin_port),
  342. ep, event->event);
  343. atomic_set(&rpcx_to_rdmax(ep->rep_xprt)->rx_buf.rb_credits, 1);
  344. dprintk("RPC: %s: %sconnected\n",
  345. __func__, connstate > 0 ? "" : "dis");
  346. ep->rep_connected = connstate;
  347. ep->rep_func(ep);
  348. wake_up_all(&ep->rep_connect_wait);
  349. break;
  350. default:
  351. dprintk("RPC: %s: unexpected CM event %d\n",
  352. __func__, event->event);
  353. break;
  354. }
  355. #ifdef RPC_DEBUG
  356. if (connstate == 1) {
  357. int ird = attr.max_dest_rd_atomic;
  358. int tird = ep->rep_remote_cma.responder_resources;
  359. printk(KERN_INFO "rpcrdma: connection to %pI4:%u "
  360. "on %s, memreg %d slots %d ird %d%s\n",
  361. &addr->sin_addr.s_addr,
  362. ntohs(addr->sin_port),
  363. ia->ri_id->device->name,
  364. ia->ri_memreg_strategy,
  365. xprt->rx_buf.rb_max_requests,
  366. ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
  367. } else if (connstate < 0) {
  368. printk(KERN_INFO "rpcrdma: connection to %pI4:%u closed (%d)\n",
  369. &addr->sin_addr.s_addr,
  370. ntohs(addr->sin_port),
  371. connstate);
  372. }
  373. #endif
  374. return 0;
  375. }
  376. static struct rdma_cm_id *
  377. rpcrdma_create_id(struct rpcrdma_xprt *xprt,
  378. struct rpcrdma_ia *ia, struct sockaddr *addr)
  379. {
  380. struct rdma_cm_id *id;
  381. int rc;
  382. init_completion(&ia->ri_done);
  383. id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
  384. if (IS_ERR(id)) {
  385. rc = PTR_ERR(id);
  386. dprintk("RPC: %s: rdma_create_id() failed %i\n",
  387. __func__, rc);
  388. return id;
  389. }
  390. ia->ri_async_rc = -ETIMEDOUT;
  391. rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
  392. if (rc) {
  393. dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
  394. __func__, rc);
  395. goto out;
  396. }
  397. wait_for_completion_interruptible_timeout(&ia->ri_done,
  398. msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
  399. rc = ia->ri_async_rc;
  400. if (rc)
  401. goto out;
  402. ia->ri_async_rc = -ETIMEDOUT;
  403. rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
  404. if (rc) {
  405. dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
  406. __func__, rc);
  407. goto out;
  408. }
  409. wait_for_completion_interruptible_timeout(&ia->ri_done,
  410. msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
  411. rc = ia->ri_async_rc;
  412. if (rc)
  413. goto out;
  414. return id;
  415. out:
  416. rdma_destroy_id(id);
  417. return ERR_PTR(rc);
  418. }
  419. /*
  420. * Drain any cq, prior to teardown.
  421. */
  422. static void
  423. rpcrdma_clean_cq(struct ib_cq *cq)
  424. {
  425. struct ib_wc wc;
  426. int count = 0;
  427. while (1 == ib_poll_cq(cq, 1, &wc))
  428. ++count;
  429. if (count)
  430. dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
  431. __func__, count, wc.opcode);
  432. }
  433. /*
  434. * Exported functions.
  435. */
  436. /*
  437. * Open and initialize an Interface Adapter.
  438. * o initializes fields of struct rpcrdma_ia, including
  439. * interface and provider attributes and protection zone.
  440. */
  441. int
  442. rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
  443. {
  444. int rc, mem_priv;
  445. struct ib_device_attr devattr;
  446. struct rpcrdma_ia *ia = &xprt->rx_ia;
  447. ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
  448. if (IS_ERR(ia->ri_id)) {
  449. rc = PTR_ERR(ia->ri_id);
  450. goto out1;
  451. }
  452. ia->ri_pd = ib_alloc_pd(ia->ri_id->device);
  453. if (IS_ERR(ia->ri_pd)) {
  454. rc = PTR_ERR(ia->ri_pd);
  455. dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
  456. __func__, rc);
  457. goto out2;
  458. }
  459. /*
  460. * Query the device to determine if the requested memory
  461. * registration strategy is supported. If it isn't, set the
  462. * strategy to a globally supported model.
  463. */
  464. rc = ib_query_device(ia->ri_id->device, &devattr);
  465. if (rc) {
  466. dprintk("RPC: %s: ib_query_device failed %d\n",
  467. __func__, rc);
  468. goto out2;
  469. }
  470. if (devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) {
  471. ia->ri_have_dma_lkey = 1;
  472. ia->ri_dma_lkey = ia->ri_id->device->local_dma_lkey;
  473. }
  474. if (memreg == RPCRDMA_FRMR) {
  475. /* Requires both frmr reg and local dma lkey */
  476. if ((devattr.device_cap_flags &
  477. (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) !=
  478. (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) {
  479. dprintk("RPC: %s: FRMR registration "
  480. "not supported by HCA\n", __func__);
  481. memreg = RPCRDMA_MTHCAFMR;
  482. } else {
  483. /* Mind the ia limit on FRMR page list depth */
  484. ia->ri_max_frmr_depth = min_t(unsigned int,
  485. RPCRDMA_MAX_DATA_SEGS,
  486. devattr.max_fast_reg_page_list_len);
  487. }
  488. }
  489. if (memreg == RPCRDMA_MTHCAFMR) {
  490. if (!ia->ri_id->device->alloc_fmr) {
  491. dprintk("RPC: %s: MTHCAFMR registration "
  492. "not supported by HCA\n", __func__);
  493. #if RPCRDMA_PERSISTENT_REGISTRATION
  494. memreg = RPCRDMA_ALLPHYSICAL;
  495. #else
  496. rc = -ENOMEM;
  497. goto out2;
  498. #endif
  499. }
  500. }
  501. /*
  502. * Optionally obtain an underlying physical identity mapping in
  503. * order to do a memory window-based bind. This base registration
  504. * is protected from remote access - that is enabled only by binding
  505. * for the specific bytes targeted during each RPC operation, and
  506. * revoked after the corresponding completion similar to a storage
  507. * adapter.
  508. */
  509. switch (memreg) {
  510. case RPCRDMA_FRMR:
  511. break;
  512. #if RPCRDMA_PERSISTENT_REGISTRATION
  513. case RPCRDMA_ALLPHYSICAL:
  514. mem_priv = IB_ACCESS_LOCAL_WRITE |
  515. IB_ACCESS_REMOTE_WRITE |
  516. IB_ACCESS_REMOTE_READ;
  517. goto register_setup;
  518. #endif
  519. case RPCRDMA_MTHCAFMR:
  520. if (ia->ri_have_dma_lkey)
  521. break;
  522. mem_priv = IB_ACCESS_LOCAL_WRITE;
  523. #if RPCRDMA_PERSISTENT_REGISTRATION
  524. register_setup:
  525. #endif
  526. ia->ri_bind_mem = ib_get_dma_mr(ia->ri_pd, mem_priv);
  527. if (IS_ERR(ia->ri_bind_mem)) {
  528. printk(KERN_ALERT "%s: ib_get_dma_mr for "
  529. "phys register failed with %lX\n",
  530. __func__, PTR_ERR(ia->ri_bind_mem));
  531. rc = -ENOMEM;
  532. goto out2;
  533. }
  534. break;
  535. default:
  536. printk(KERN_ERR "RPC: Unsupported memory "
  537. "registration mode: %d\n", memreg);
  538. rc = -ENOMEM;
  539. goto out2;
  540. }
  541. dprintk("RPC: %s: memory registration strategy is %d\n",
  542. __func__, memreg);
  543. /* Else will do memory reg/dereg for each chunk */
  544. ia->ri_memreg_strategy = memreg;
  545. return 0;
  546. out2:
  547. rdma_destroy_id(ia->ri_id);
  548. ia->ri_id = NULL;
  549. out1:
  550. return rc;
  551. }
  552. /*
  553. * Clean up/close an IA.
  554. * o if event handles and PD have been initialized, free them.
  555. * o close the IA
  556. */
  557. void
  558. rpcrdma_ia_close(struct rpcrdma_ia *ia)
  559. {
  560. int rc;
  561. dprintk("RPC: %s: entering\n", __func__);
  562. if (ia->ri_bind_mem != NULL) {
  563. rc = ib_dereg_mr(ia->ri_bind_mem);
  564. dprintk("RPC: %s: ib_dereg_mr returned %i\n",
  565. __func__, rc);
  566. }
  567. if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
  568. if (ia->ri_id->qp)
  569. rdma_destroy_qp(ia->ri_id);
  570. rdma_destroy_id(ia->ri_id);
  571. ia->ri_id = NULL;
  572. }
  573. if (ia->ri_pd != NULL && !IS_ERR(ia->ri_pd)) {
  574. rc = ib_dealloc_pd(ia->ri_pd);
  575. dprintk("RPC: %s: ib_dealloc_pd returned %i\n",
  576. __func__, rc);
  577. }
  578. }
  579. /*
  580. * Create unconnected endpoint.
  581. */
  582. int
  583. rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
  584. struct rpcrdma_create_data_internal *cdata)
  585. {
  586. struct ib_device_attr devattr;
  587. struct ib_cq *sendcq, *recvcq;
  588. int rc, err;
  589. rc = ib_query_device(ia->ri_id->device, &devattr);
  590. if (rc) {
  591. dprintk("RPC: %s: ib_query_device failed %d\n",
  592. __func__, rc);
  593. return rc;
  594. }
  595. /* check provider's send/recv wr limits */
  596. if (cdata->max_requests > devattr.max_qp_wr)
  597. cdata->max_requests = devattr.max_qp_wr;
  598. ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
  599. ep->rep_attr.qp_context = ep;
  600. /* send_cq and recv_cq initialized below */
  601. ep->rep_attr.srq = NULL;
  602. ep->rep_attr.cap.max_send_wr = cdata->max_requests;
  603. switch (ia->ri_memreg_strategy) {
  604. case RPCRDMA_FRMR: {
  605. int depth = 7;
  606. /* Add room for frmr register and invalidate WRs.
  607. * 1. FRMR reg WR for head
  608. * 2. FRMR invalidate WR for head
  609. * 3. N FRMR reg WRs for pagelist
  610. * 4. N FRMR invalidate WRs for pagelist
  611. * 5. FRMR reg WR for tail
  612. * 6. FRMR invalidate WR for tail
  613. * 7. The RDMA_SEND WR
  614. */
  615. /* Calculate N if the device max FRMR depth is smaller than
  616. * RPCRDMA_MAX_DATA_SEGS.
  617. */
  618. if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) {
  619. int delta = RPCRDMA_MAX_DATA_SEGS -
  620. ia->ri_max_frmr_depth;
  621. do {
  622. depth += 2; /* FRMR reg + invalidate */
  623. delta -= ia->ri_max_frmr_depth;
  624. } while (delta > 0);
  625. }
  626. ep->rep_attr.cap.max_send_wr *= depth;
  627. if (ep->rep_attr.cap.max_send_wr > devattr.max_qp_wr) {
  628. cdata->max_requests = devattr.max_qp_wr / depth;
  629. if (!cdata->max_requests)
  630. return -EINVAL;
  631. ep->rep_attr.cap.max_send_wr = cdata->max_requests *
  632. depth;
  633. }
  634. break;
  635. }
  636. default:
  637. break;
  638. }
  639. ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
  640. ep->rep_attr.cap.max_send_sge = (cdata->padding ? 4 : 2);
  641. ep->rep_attr.cap.max_recv_sge = 1;
  642. ep->rep_attr.cap.max_inline_data = 0;
  643. ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  644. ep->rep_attr.qp_type = IB_QPT_RC;
  645. ep->rep_attr.port_num = ~0;
  646. dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
  647. "iovs: send %d recv %d\n",
  648. __func__,
  649. ep->rep_attr.cap.max_send_wr,
  650. ep->rep_attr.cap.max_recv_wr,
  651. ep->rep_attr.cap.max_send_sge,
  652. ep->rep_attr.cap.max_recv_sge);
  653. /* set trigger for requesting send completion */
  654. ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
  655. if (ep->rep_cqinit <= 2)
  656. ep->rep_cqinit = 0;
  657. INIT_CQCOUNT(ep);
  658. ep->rep_ia = ia;
  659. init_waitqueue_head(&ep->rep_connect_wait);
  660. INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
  661. sendcq = ib_create_cq(ia->ri_id->device, rpcrdma_sendcq_upcall,
  662. rpcrdma_cq_async_error_upcall, ep,
  663. ep->rep_attr.cap.max_send_wr + 1, 0);
  664. if (IS_ERR(sendcq)) {
  665. rc = PTR_ERR(sendcq);
  666. dprintk("RPC: %s: failed to create send CQ: %i\n",
  667. __func__, rc);
  668. goto out1;
  669. }
  670. rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
  671. if (rc) {
  672. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  673. __func__, rc);
  674. goto out2;
  675. }
  676. recvcq = ib_create_cq(ia->ri_id->device, rpcrdma_recvcq_upcall,
  677. rpcrdma_cq_async_error_upcall, ep,
  678. ep->rep_attr.cap.max_recv_wr + 1, 0);
  679. if (IS_ERR(recvcq)) {
  680. rc = PTR_ERR(recvcq);
  681. dprintk("RPC: %s: failed to create recv CQ: %i\n",
  682. __func__, rc);
  683. goto out2;
  684. }
  685. rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
  686. if (rc) {
  687. dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
  688. __func__, rc);
  689. ib_destroy_cq(recvcq);
  690. goto out2;
  691. }
  692. ep->rep_attr.send_cq = sendcq;
  693. ep->rep_attr.recv_cq = recvcq;
  694. /* Initialize cma parameters */
  695. /* RPC/RDMA does not use private data */
  696. ep->rep_remote_cma.private_data = NULL;
  697. ep->rep_remote_cma.private_data_len = 0;
  698. /* Client offers RDMA Read but does not initiate */
  699. ep->rep_remote_cma.initiator_depth = 0;
  700. if (devattr.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
  701. ep->rep_remote_cma.responder_resources = 32;
  702. else
  703. ep->rep_remote_cma.responder_resources = devattr.max_qp_rd_atom;
  704. ep->rep_remote_cma.retry_count = 7;
  705. ep->rep_remote_cma.flow_control = 0;
  706. ep->rep_remote_cma.rnr_retry_count = 0;
  707. return 0;
  708. out2:
  709. err = ib_destroy_cq(sendcq);
  710. if (err)
  711. dprintk("RPC: %s: ib_destroy_cq returned %i\n",
  712. __func__, err);
  713. out1:
  714. return rc;
  715. }
  716. /*
  717. * rpcrdma_ep_destroy
  718. *
  719. * Disconnect and destroy endpoint. After this, the only
  720. * valid operations on the ep are to free it (if dynamically
  721. * allocated) or re-create it.
  722. */
  723. void
  724. rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  725. {
  726. int rc;
  727. dprintk("RPC: %s: entering, connected is %d\n",
  728. __func__, ep->rep_connected);
  729. cancel_delayed_work_sync(&ep->rep_connect_worker);
  730. if (ia->ri_id->qp) {
  731. rc = rpcrdma_ep_disconnect(ep, ia);
  732. if (rc)
  733. dprintk("RPC: %s: rpcrdma_ep_disconnect"
  734. " returned %i\n", __func__, rc);
  735. rdma_destroy_qp(ia->ri_id);
  736. ia->ri_id->qp = NULL;
  737. }
  738. /* padding - could be done in rpcrdma_buffer_destroy... */
  739. if (ep->rep_pad_mr) {
  740. rpcrdma_deregister_internal(ia, ep->rep_pad_mr, &ep->rep_pad);
  741. ep->rep_pad_mr = NULL;
  742. }
  743. rpcrdma_clean_cq(ep->rep_attr.recv_cq);
  744. rc = ib_destroy_cq(ep->rep_attr.recv_cq);
  745. if (rc)
  746. dprintk("RPC: %s: ib_destroy_cq returned %i\n",
  747. __func__, rc);
  748. rpcrdma_clean_cq(ep->rep_attr.send_cq);
  749. rc = ib_destroy_cq(ep->rep_attr.send_cq);
  750. if (rc)
  751. dprintk("RPC: %s: ib_destroy_cq returned %i\n",
  752. __func__, rc);
  753. }
  754. /*
  755. * Connect unconnected endpoint.
  756. */
  757. int
  758. rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  759. {
  760. struct rdma_cm_id *id;
  761. int rc = 0;
  762. int retry_count = 0;
  763. if (ep->rep_connected != 0) {
  764. struct rpcrdma_xprt *xprt;
  765. retry:
  766. dprintk("RPC: %s: reconnecting...\n", __func__);
  767. rc = rpcrdma_ep_disconnect(ep, ia);
  768. if (rc && rc != -ENOTCONN)
  769. dprintk("RPC: %s: rpcrdma_ep_disconnect"
  770. " status %i\n", __func__, rc);
  771. rpcrdma_clean_cq(ep->rep_attr.recv_cq);
  772. rpcrdma_clean_cq(ep->rep_attr.send_cq);
  773. xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
  774. id = rpcrdma_create_id(xprt, ia,
  775. (struct sockaddr *)&xprt->rx_data.addr);
  776. if (IS_ERR(id)) {
  777. rc = -EHOSTUNREACH;
  778. goto out;
  779. }
  780. /* TEMP TEMP TEMP - fail if new device:
  781. * Deregister/remarshal *all* requests!
  782. * Close and recreate adapter, pd, etc!
  783. * Re-determine all attributes still sane!
  784. * More stuff I haven't thought of!
  785. * Rrrgh!
  786. */
  787. if (ia->ri_id->device != id->device) {
  788. printk("RPC: %s: can't reconnect on "
  789. "different device!\n", __func__);
  790. rdma_destroy_id(id);
  791. rc = -ENETUNREACH;
  792. goto out;
  793. }
  794. /* END TEMP */
  795. rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
  796. if (rc) {
  797. dprintk("RPC: %s: rdma_create_qp failed %i\n",
  798. __func__, rc);
  799. rdma_destroy_id(id);
  800. rc = -ENETUNREACH;
  801. goto out;
  802. }
  803. rdma_destroy_qp(ia->ri_id);
  804. rdma_destroy_id(ia->ri_id);
  805. ia->ri_id = id;
  806. } else {
  807. dprintk("RPC: %s: connecting...\n", __func__);
  808. rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
  809. if (rc) {
  810. dprintk("RPC: %s: rdma_create_qp failed %i\n",
  811. __func__, rc);
  812. /* do not update ep->rep_connected */
  813. return -ENETUNREACH;
  814. }
  815. }
  816. ep->rep_connected = 0;
  817. rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
  818. if (rc) {
  819. dprintk("RPC: %s: rdma_connect() failed with %i\n",
  820. __func__, rc);
  821. goto out;
  822. }
  823. wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
  824. /*
  825. * Check state. A non-peer reject indicates no listener
  826. * (ECONNREFUSED), which may be a transient state. All
  827. * others indicate a transport condition which has already
  828. * undergone a best-effort.
  829. */
  830. if (ep->rep_connected == -ECONNREFUSED &&
  831. ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
  832. dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
  833. goto retry;
  834. }
  835. if (ep->rep_connected <= 0) {
  836. /* Sometimes, the only way to reliably connect to remote
  837. * CMs is to use same nonzero values for ORD and IRD. */
  838. if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
  839. (ep->rep_remote_cma.responder_resources == 0 ||
  840. ep->rep_remote_cma.initiator_depth !=
  841. ep->rep_remote_cma.responder_resources)) {
  842. if (ep->rep_remote_cma.responder_resources == 0)
  843. ep->rep_remote_cma.responder_resources = 1;
  844. ep->rep_remote_cma.initiator_depth =
  845. ep->rep_remote_cma.responder_resources;
  846. goto retry;
  847. }
  848. rc = ep->rep_connected;
  849. } else {
  850. dprintk("RPC: %s: connected\n", __func__);
  851. }
  852. out:
  853. if (rc)
  854. ep->rep_connected = rc;
  855. return rc;
  856. }
  857. /*
  858. * rpcrdma_ep_disconnect
  859. *
  860. * This is separate from destroy to facilitate the ability
  861. * to reconnect without recreating the endpoint.
  862. *
  863. * This call is not reentrant, and must not be made in parallel
  864. * on the same endpoint.
  865. */
  866. int
  867. rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
  868. {
  869. int rc;
  870. rpcrdma_clean_cq(ep->rep_attr.recv_cq);
  871. rpcrdma_clean_cq(ep->rep_attr.send_cq);
  872. rc = rdma_disconnect(ia->ri_id);
  873. if (!rc) {
  874. /* returns without wait if not connected */
  875. wait_event_interruptible(ep->rep_connect_wait,
  876. ep->rep_connected != 1);
  877. dprintk("RPC: %s: after wait, %sconnected\n", __func__,
  878. (ep->rep_connected == 1) ? "still " : "dis");
  879. } else {
  880. dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
  881. ep->rep_connected = rc;
  882. }
  883. return rc;
  884. }
  885. /*
  886. * Initialize buffer memory
  887. */
  888. int
  889. rpcrdma_buffer_create(struct rpcrdma_buffer *buf, struct rpcrdma_ep *ep,
  890. struct rpcrdma_ia *ia, struct rpcrdma_create_data_internal *cdata)
  891. {
  892. char *p;
  893. size_t len, rlen, wlen;
  894. int i, rc;
  895. struct rpcrdma_mw *r;
  896. buf->rb_max_requests = cdata->max_requests;
  897. spin_lock_init(&buf->rb_lock);
  898. atomic_set(&buf->rb_credits, 1);
  899. /* Need to allocate:
  900. * 1. arrays for send and recv pointers
  901. * 2. arrays of struct rpcrdma_req to fill in pointers
  902. * 3. array of struct rpcrdma_rep for replies
  903. * 4. padding, if any
  904. * 5. mw's, fmr's or frmr's, if any
  905. * Send/recv buffers in req/rep need to be registered
  906. */
  907. len = buf->rb_max_requests *
  908. (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *));
  909. len += cdata->padding;
  910. switch (ia->ri_memreg_strategy) {
  911. case RPCRDMA_FRMR:
  912. len += buf->rb_max_requests * RPCRDMA_MAX_SEGS *
  913. sizeof(struct rpcrdma_mw);
  914. break;
  915. case RPCRDMA_MTHCAFMR:
  916. /* TBD we are perhaps overallocating here */
  917. len += (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS *
  918. sizeof(struct rpcrdma_mw);
  919. break;
  920. default:
  921. break;
  922. }
  923. /* allocate 1, 4 and 5 in one shot */
  924. p = kzalloc(len, GFP_KERNEL);
  925. if (p == NULL) {
  926. dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n",
  927. __func__, len);
  928. rc = -ENOMEM;
  929. goto out;
  930. }
  931. buf->rb_pool = p; /* for freeing it later */
  932. buf->rb_send_bufs = (struct rpcrdma_req **) p;
  933. p = (char *) &buf->rb_send_bufs[buf->rb_max_requests];
  934. buf->rb_recv_bufs = (struct rpcrdma_rep **) p;
  935. p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests];
  936. /*
  937. * Register the zeroed pad buffer, if any.
  938. */
  939. if (cdata->padding) {
  940. rc = rpcrdma_register_internal(ia, p, cdata->padding,
  941. &ep->rep_pad_mr, &ep->rep_pad);
  942. if (rc)
  943. goto out;
  944. }
  945. p += cdata->padding;
  946. INIT_LIST_HEAD(&buf->rb_mws);
  947. r = (struct rpcrdma_mw *)p;
  948. switch (ia->ri_memreg_strategy) {
  949. case RPCRDMA_FRMR:
  950. for (i = buf->rb_max_requests * RPCRDMA_MAX_SEGS; i; i--) {
  951. r->r.frmr.fr_mr = ib_alloc_fast_reg_mr(ia->ri_pd,
  952. ia->ri_max_frmr_depth);
  953. if (IS_ERR(r->r.frmr.fr_mr)) {
  954. rc = PTR_ERR(r->r.frmr.fr_mr);
  955. dprintk("RPC: %s: ib_alloc_fast_reg_mr"
  956. " failed %i\n", __func__, rc);
  957. goto out;
  958. }
  959. r->r.frmr.fr_pgl = ib_alloc_fast_reg_page_list(
  960. ia->ri_id->device,
  961. ia->ri_max_frmr_depth);
  962. if (IS_ERR(r->r.frmr.fr_pgl)) {
  963. rc = PTR_ERR(r->r.frmr.fr_pgl);
  964. dprintk("RPC: %s: "
  965. "ib_alloc_fast_reg_page_list "
  966. "failed %i\n", __func__, rc);
  967. ib_dereg_mr(r->r.frmr.fr_mr);
  968. goto out;
  969. }
  970. list_add(&r->mw_list, &buf->rb_mws);
  971. ++r;
  972. }
  973. break;
  974. case RPCRDMA_MTHCAFMR:
  975. /* TBD we are perhaps overallocating here */
  976. for (i = (buf->rb_max_requests+1) * RPCRDMA_MAX_SEGS; i; i--) {
  977. static struct ib_fmr_attr fa =
  978. { RPCRDMA_MAX_DATA_SEGS, 1, PAGE_SHIFT };
  979. r->r.fmr = ib_alloc_fmr(ia->ri_pd,
  980. IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ,
  981. &fa);
  982. if (IS_ERR(r->r.fmr)) {
  983. rc = PTR_ERR(r->r.fmr);
  984. dprintk("RPC: %s: ib_alloc_fmr"
  985. " failed %i\n", __func__, rc);
  986. goto out;
  987. }
  988. list_add(&r->mw_list, &buf->rb_mws);
  989. ++r;
  990. }
  991. break;
  992. default:
  993. break;
  994. }
  995. /*
  996. * Allocate/init the request/reply buffers. Doing this
  997. * using kmalloc for now -- one for each buf.
  998. */
  999. wlen = 1 << fls(cdata->inline_wsize + sizeof(struct rpcrdma_req));
  1000. rlen = 1 << fls(cdata->inline_rsize + sizeof(struct rpcrdma_rep));
  1001. dprintk("RPC: %s: wlen = %zu, rlen = %zu\n",
  1002. __func__, wlen, rlen);
  1003. for (i = 0; i < buf->rb_max_requests; i++) {
  1004. struct rpcrdma_req *req;
  1005. struct rpcrdma_rep *rep;
  1006. req = kmalloc(wlen, GFP_KERNEL);
  1007. if (req == NULL) {
  1008. dprintk("RPC: %s: request buffer %d alloc"
  1009. " failed\n", __func__, i);
  1010. rc = -ENOMEM;
  1011. goto out;
  1012. }
  1013. memset(req, 0, sizeof(struct rpcrdma_req));
  1014. buf->rb_send_bufs[i] = req;
  1015. buf->rb_send_bufs[i]->rl_buffer = buf;
  1016. rc = rpcrdma_register_internal(ia, req->rl_base,
  1017. wlen - offsetof(struct rpcrdma_req, rl_base),
  1018. &buf->rb_send_bufs[i]->rl_handle,
  1019. &buf->rb_send_bufs[i]->rl_iov);
  1020. if (rc)
  1021. goto out;
  1022. buf->rb_send_bufs[i]->rl_size = wlen -
  1023. sizeof(struct rpcrdma_req);
  1024. rep = kmalloc(rlen, GFP_KERNEL);
  1025. if (rep == NULL) {
  1026. dprintk("RPC: %s: reply buffer %d alloc failed\n",
  1027. __func__, i);
  1028. rc = -ENOMEM;
  1029. goto out;
  1030. }
  1031. memset(rep, 0, sizeof(struct rpcrdma_rep));
  1032. buf->rb_recv_bufs[i] = rep;
  1033. buf->rb_recv_bufs[i]->rr_buffer = buf;
  1034. rc = rpcrdma_register_internal(ia, rep->rr_base,
  1035. rlen - offsetof(struct rpcrdma_rep, rr_base),
  1036. &buf->rb_recv_bufs[i]->rr_handle,
  1037. &buf->rb_recv_bufs[i]->rr_iov);
  1038. if (rc)
  1039. goto out;
  1040. }
  1041. dprintk("RPC: %s: max_requests %d\n",
  1042. __func__, buf->rb_max_requests);
  1043. /* done */
  1044. return 0;
  1045. out:
  1046. rpcrdma_buffer_destroy(buf);
  1047. return rc;
  1048. }
  1049. /*
  1050. * Unregister and destroy buffer memory. Need to deal with
  1051. * partial initialization, so it's callable from failed create.
  1052. * Must be called before destroying endpoint, as registrations
  1053. * reference it.
  1054. */
  1055. void
  1056. rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
  1057. {
  1058. int rc, i;
  1059. struct rpcrdma_ia *ia = rdmab_to_ia(buf);
  1060. struct rpcrdma_mw *r;
  1061. /* clean up in reverse order from create
  1062. * 1. recv mr memory (mr free, then kfree)
  1063. * 2. send mr memory (mr free, then kfree)
  1064. * 3. padding (if any) [moved to rpcrdma_ep_destroy]
  1065. * 4. arrays
  1066. */
  1067. dprintk("RPC: %s: entering\n", __func__);
  1068. for (i = 0; i < buf->rb_max_requests; i++) {
  1069. if (buf->rb_recv_bufs && buf->rb_recv_bufs[i]) {
  1070. rpcrdma_deregister_internal(ia,
  1071. buf->rb_recv_bufs[i]->rr_handle,
  1072. &buf->rb_recv_bufs[i]->rr_iov);
  1073. kfree(buf->rb_recv_bufs[i]);
  1074. }
  1075. if (buf->rb_send_bufs && buf->rb_send_bufs[i]) {
  1076. rpcrdma_deregister_internal(ia,
  1077. buf->rb_send_bufs[i]->rl_handle,
  1078. &buf->rb_send_bufs[i]->rl_iov);
  1079. kfree(buf->rb_send_bufs[i]);
  1080. }
  1081. }
  1082. while (!list_empty(&buf->rb_mws)) {
  1083. r = list_entry(buf->rb_mws.next,
  1084. struct rpcrdma_mw, mw_list);
  1085. list_del(&r->mw_list);
  1086. switch (ia->ri_memreg_strategy) {
  1087. case RPCRDMA_FRMR:
  1088. rc = ib_dereg_mr(r->r.frmr.fr_mr);
  1089. if (rc)
  1090. dprintk("RPC: %s:"
  1091. " ib_dereg_mr"
  1092. " failed %i\n",
  1093. __func__, rc);
  1094. ib_free_fast_reg_page_list(r->r.frmr.fr_pgl);
  1095. break;
  1096. case RPCRDMA_MTHCAFMR:
  1097. rc = ib_dealloc_fmr(r->r.fmr);
  1098. if (rc)
  1099. dprintk("RPC: %s:"
  1100. " ib_dealloc_fmr"
  1101. " failed %i\n",
  1102. __func__, rc);
  1103. break;
  1104. default:
  1105. break;
  1106. }
  1107. }
  1108. kfree(buf->rb_pool);
  1109. }
  1110. /*
  1111. * Get a set of request/reply buffers.
  1112. *
  1113. * Reply buffer (if needed) is attached to send buffer upon return.
  1114. * Rule:
  1115. * rb_send_index and rb_recv_index MUST always be pointing to the
  1116. * *next* available buffer (non-NULL). They are incremented after
  1117. * removing buffers, and decremented *before* returning them.
  1118. */
  1119. struct rpcrdma_req *
  1120. rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
  1121. {
  1122. struct rpcrdma_req *req;
  1123. unsigned long flags;
  1124. int i;
  1125. struct rpcrdma_mw *r;
  1126. spin_lock_irqsave(&buffers->rb_lock, flags);
  1127. if (buffers->rb_send_index == buffers->rb_max_requests) {
  1128. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1129. dprintk("RPC: %s: out of request buffers\n", __func__);
  1130. return ((struct rpcrdma_req *)NULL);
  1131. }
  1132. req = buffers->rb_send_bufs[buffers->rb_send_index];
  1133. if (buffers->rb_send_index < buffers->rb_recv_index) {
  1134. dprintk("RPC: %s: %d extra receives outstanding (ok)\n",
  1135. __func__,
  1136. buffers->rb_recv_index - buffers->rb_send_index);
  1137. req->rl_reply = NULL;
  1138. } else {
  1139. req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
  1140. buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
  1141. }
  1142. buffers->rb_send_bufs[buffers->rb_send_index++] = NULL;
  1143. if (!list_empty(&buffers->rb_mws)) {
  1144. i = RPCRDMA_MAX_SEGS - 1;
  1145. do {
  1146. r = list_entry(buffers->rb_mws.next,
  1147. struct rpcrdma_mw, mw_list);
  1148. list_del(&r->mw_list);
  1149. req->rl_segments[i].mr_chunk.rl_mw = r;
  1150. } while (--i >= 0);
  1151. }
  1152. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1153. return req;
  1154. }
  1155. /*
  1156. * Put request/reply buffers back into pool.
  1157. * Pre-decrement counter/array index.
  1158. */
  1159. void
  1160. rpcrdma_buffer_put(struct rpcrdma_req *req)
  1161. {
  1162. struct rpcrdma_buffer *buffers = req->rl_buffer;
  1163. struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
  1164. int i;
  1165. unsigned long flags;
  1166. spin_lock_irqsave(&buffers->rb_lock, flags);
  1167. buffers->rb_send_bufs[--buffers->rb_send_index] = req;
  1168. req->rl_niovs = 0;
  1169. if (req->rl_reply) {
  1170. buffers->rb_recv_bufs[--buffers->rb_recv_index] = req->rl_reply;
  1171. req->rl_reply->rr_func = NULL;
  1172. req->rl_reply = NULL;
  1173. }
  1174. switch (ia->ri_memreg_strategy) {
  1175. case RPCRDMA_FRMR:
  1176. case RPCRDMA_MTHCAFMR:
  1177. /*
  1178. * Cycle mw's back in reverse order, and "spin" them.
  1179. * This delays and scrambles reuse as much as possible.
  1180. */
  1181. i = 1;
  1182. do {
  1183. struct rpcrdma_mw **mw;
  1184. mw = &req->rl_segments[i].mr_chunk.rl_mw;
  1185. list_add_tail(&(*mw)->mw_list, &buffers->rb_mws);
  1186. *mw = NULL;
  1187. } while (++i < RPCRDMA_MAX_SEGS);
  1188. list_add_tail(&req->rl_segments[0].mr_chunk.rl_mw->mw_list,
  1189. &buffers->rb_mws);
  1190. req->rl_segments[0].mr_chunk.rl_mw = NULL;
  1191. break;
  1192. default:
  1193. break;
  1194. }
  1195. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1196. }
  1197. /*
  1198. * Recover reply buffers from pool.
  1199. * This happens when recovering from error conditions.
  1200. * Post-increment counter/array index.
  1201. */
  1202. void
  1203. rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
  1204. {
  1205. struct rpcrdma_buffer *buffers = req->rl_buffer;
  1206. unsigned long flags;
  1207. if (req->rl_iov.length == 0) /* special case xprt_rdma_allocate() */
  1208. buffers = ((struct rpcrdma_req *) buffers)->rl_buffer;
  1209. spin_lock_irqsave(&buffers->rb_lock, flags);
  1210. if (buffers->rb_recv_index < buffers->rb_max_requests) {
  1211. req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
  1212. buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
  1213. }
  1214. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1215. }
  1216. /*
  1217. * Put reply buffers back into pool when not attached to
  1218. * request. This happens in error conditions.
  1219. */
  1220. void
  1221. rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
  1222. {
  1223. struct rpcrdma_buffer *buffers = rep->rr_buffer;
  1224. unsigned long flags;
  1225. rep->rr_func = NULL;
  1226. spin_lock_irqsave(&buffers->rb_lock, flags);
  1227. buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep;
  1228. spin_unlock_irqrestore(&buffers->rb_lock, flags);
  1229. }
  1230. /*
  1231. * Wrappers for internal-use kmalloc memory registration, used by buffer code.
  1232. */
  1233. int
  1234. rpcrdma_register_internal(struct rpcrdma_ia *ia, void *va, int len,
  1235. struct ib_mr **mrp, struct ib_sge *iov)
  1236. {
  1237. struct ib_phys_buf ipb;
  1238. struct ib_mr *mr;
  1239. int rc;
  1240. /*
  1241. * All memory passed here was kmalloc'ed, therefore phys-contiguous.
  1242. */
  1243. iov->addr = ib_dma_map_single(ia->ri_id->device,
  1244. va, len, DMA_BIDIRECTIONAL);
  1245. iov->length = len;
  1246. if (ia->ri_have_dma_lkey) {
  1247. *mrp = NULL;
  1248. iov->lkey = ia->ri_dma_lkey;
  1249. return 0;
  1250. } else if (ia->ri_bind_mem != NULL) {
  1251. *mrp = NULL;
  1252. iov->lkey = ia->ri_bind_mem->lkey;
  1253. return 0;
  1254. }
  1255. ipb.addr = iov->addr;
  1256. ipb.size = iov->length;
  1257. mr = ib_reg_phys_mr(ia->ri_pd, &ipb, 1,
  1258. IB_ACCESS_LOCAL_WRITE, &iov->addr);
  1259. dprintk("RPC: %s: phys convert: 0x%llx "
  1260. "registered 0x%llx length %d\n",
  1261. __func__, (unsigned long long)ipb.addr,
  1262. (unsigned long long)iov->addr, len);
  1263. if (IS_ERR(mr)) {
  1264. *mrp = NULL;
  1265. rc = PTR_ERR(mr);
  1266. dprintk("RPC: %s: failed with %i\n", __func__, rc);
  1267. } else {
  1268. *mrp = mr;
  1269. iov->lkey = mr->lkey;
  1270. rc = 0;
  1271. }
  1272. return rc;
  1273. }
  1274. int
  1275. rpcrdma_deregister_internal(struct rpcrdma_ia *ia,
  1276. struct ib_mr *mr, struct ib_sge *iov)
  1277. {
  1278. int rc;
  1279. ib_dma_unmap_single(ia->ri_id->device,
  1280. iov->addr, iov->length, DMA_BIDIRECTIONAL);
  1281. if (NULL == mr)
  1282. return 0;
  1283. rc = ib_dereg_mr(mr);
  1284. if (rc)
  1285. dprintk("RPC: %s: ib_dereg_mr failed %i\n", __func__, rc);
  1286. return rc;
  1287. }
  1288. /*
  1289. * Wrappers for chunk registration, shared by read/write chunk code.
  1290. */
  1291. static void
  1292. rpcrdma_map_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg, int writing)
  1293. {
  1294. seg->mr_dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1295. seg->mr_dmalen = seg->mr_len;
  1296. if (seg->mr_page)
  1297. seg->mr_dma = ib_dma_map_page(ia->ri_id->device,
  1298. seg->mr_page, offset_in_page(seg->mr_offset),
  1299. seg->mr_dmalen, seg->mr_dir);
  1300. else
  1301. seg->mr_dma = ib_dma_map_single(ia->ri_id->device,
  1302. seg->mr_offset,
  1303. seg->mr_dmalen, seg->mr_dir);
  1304. if (ib_dma_mapping_error(ia->ri_id->device, seg->mr_dma)) {
  1305. dprintk("RPC: %s: mr_dma %llx mr_offset %p mr_dma_len %zu\n",
  1306. __func__,
  1307. (unsigned long long)seg->mr_dma,
  1308. seg->mr_offset, seg->mr_dmalen);
  1309. }
  1310. }
  1311. static void
  1312. rpcrdma_unmap_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg)
  1313. {
  1314. if (seg->mr_page)
  1315. ib_dma_unmap_page(ia->ri_id->device,
  1316. seg->mr_dma, seg->mr_dmalen, seg->mr_dir);
  1317. else
  1318. ib_dma_unmap_single(ia->ri_id->device,
  1319. seg->mr_dma, seg->mr_dmalen, seg->mr_dir);
  1320. }
  1321. static int
  1322. rpcrdma_register_frmr_external(struct rpcrdma_mr_seg *seg,
  1323. int *nsegs, int writing, struct rpcrdma_ia *ia,
  1324. struct rpcrdma_xprt *r_xprt)
  1325. {
  1326. struct rpcrdma_mr_seg *seg1 = seg;
  1327. struct ib_send_wr invalidate_wr, frmr_wr, *bad_wr, *post_wr;
  1328. u8 key;
  1329. int len, pageoff;
  1330. int i, rc;
  1331. int seg_len;
  1332. u64 pa;
  1333. int page_no;
  1334. pageoff = offset_in_page(seg1->mr_offset);
  1335. seg1->mr_offset -= pageoff; /* start of page */
  1336. seg1->mr_len += pageoff;
  1337. len = -pageoff;
  1338. if (*nsegs > ia->ri_max_frmr_depth)
  1339. *nsegs = ia->ri_max_frmr_depth;
  1340. for (page_no = i = 0; i < *nsegs;) {
  1341. rpcrdma_map_one(ia, seg, writing);
  1342. pa = seg->mr_dma;
  1343. for (seg_len = seg->mr_len; seg_len > 0; seg_len -= PAGE_SIZE) {
  1344. seg1->mr_chunk.rl_mw->r.frmr.fr_pgl->
  1345. page_list[page_no++] = pa;
  1346. pa += PAGE_SIZE;
  1347. }
  1348. len += seg->mr_len;
  1349. ++seg;
  1350. ++i;
  1351. /* Check for holes */
  1352. if ((i < *nsegs && offset_in_page(seg->mr_offset)) ||
  1353. offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
  1354. break;
  1355. }
  1356. dprintk("RPC: %s: Using frmr %p to map %d segments\n",
  1357. __func__, seg1->mr_chunk.rl_mw, i);
  1358. if (unlikely(seg1->mr_chunk.rl_mw->r.frmr.state == FRMR_IS_VALID)) {
  1359. dprintk("RPC: %s: frmr %x left valid, posting invalidate.\n",
  1360. __func__,
  1361. seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey);
  1362. /* Invalidate before using. */
  1363. memset(&invalidate_wr, 0, sizeof invalidate_wr);
  1364. invalidate_wr.wr_id = (unsigned long)(void *)seg1->mr_chunk.rl_mw;
  1365. invalidate_wr.next = &frmr_wr;
  1366. invalidate_wr.opcode = IB_WR_LOCAL_INV;
  1367. invalidate_wr.send_flags = IB_SEND_SIGNALED;
  1368. invalidate_wr.ex.invalidate_rkey =
  1369. seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
  1370. DECR_CQCOUNT(&r_xprt->rx_ep);
  1371. post_wr = &invalidate_wr;
  1372. } else
  1373. post_wr = &frmr_wr;
  1374. /* Prepare FRMR WR */
  1375. memset(&frmr_wr, 0, sizeof frmr_wr);
  1376. frmr_wr.wr_id = (unsigned long)(void *)seg1->mr_chunk.rl_mw;
  1377. frmr_wr.opcode = IB_WR_FAST_REG_MR;
  1378. frmr_wr.send_flags = IB_SEND_SIGNALED;
  1379. frmr_wr.wr.fast_reg.iova_start = seg1->mr_dma;
  1380. frmr_wr.wr.fast_reg.page_list = seg1->mr_chunk.rl_mw->r.frmr.fr_pgl;
  1381. frmr_wr.wr.fast_reg.page_list_len = page_no;
  1382. frmr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
  1383. frmr_wr.wr.fast_reg.length = page_no << PAGE_SHIFT;
  1384. if (frmr_wr.wr.fast_reg.length < len) {
  1385. while (seg1->mr_nsegs--)
  1386. rpcrdma_unmap_one(ia, seg++);
  1387. return -EIO;
  1388. }
  1389. /* Bump the key */
  1390. key = (u8)(seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey & 0x000000FF);
  1391. ib_update_fast_reg_key(seg1->mr_chunk.rl_mw->r.frmr.fr_mr, ++key);
  1392. frmr_wr.wr.fast_reg.access_flags = (writing ?
  1393. IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
  1394. IB_ACCESS_REMOTE_READ);
  1395. frmr_wr.wr.fast_reg.rkey = seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
  1396. DECR_CQCOUNT(&r_xprt->rx_ep);
  1397. rc = ib_post_send(ia->ri_id->qp, post_wr, &bad_wr);
  1398. if (rc) {
  1399. dprintk("RPC: %s: failed ib_post_send for register,"
  1400. " status %i\n", __func__, rc);
  1401. while (i--)
  1402. rpcrdma_unmap_one(ia, --seg);
  1403. } else {
  1404. seg1->mr_rkey = seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
  1405. seg1->mr_base = seg1->mr_dma + pageoff;
  1406. seg1->mr_nsegs = i;
  1407. seg1->mr_len = len;
  1408. }
  1409. *nsegs = i;
  1410. return rc;
  1411. }
  1412. static int
  1413. rpcrdma_deregister_frmr_external(struct rpcrdma_mr_seg *seg,
  1414. struct rpcrdma_ia *ia, struct rpcrdma_xprt *r_xprt)
  1415. {
  1416. struct rpcrdma_mr_seg *seg1 = seg;
  1417. struct ib_send_wr invalidate_wr, *bad_wr;
  1418. int rc;
  1419. while (seg1->mr_nsegs--)
  1420. rpcrdma_unmap_one(ia, seg++);
  1421. memset(&invalidate_wr, 0, sizeof invalidate_wr);
  1422. invalidate_wr.wr_id = (unsigned long)(void *)seg1->mr_chunk.rl_mw;
  1423. invalidate_wr.opcode = IB_WR_LOCAL_INV;
  1424. invalidate_wr.send_flags = IB_SEND_SIGNALED;
  1425. invalidate_wr.ex.invalidate_rkey = seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
  1426. DECR_CQCOUNT(&r_xprt->rx_ep);
  1427. rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
  1428. if (rc)
  1429. dprintk("RPC: %s: failed ib_post_send for invalidate,"
  1430. " status %i\n", __func__, rc);
  1431. return rc;
  1432. }
  1433. static int
  1434. rpcrdma_register_fmr_external(struct rpcrdma_mr_seg *seg,
  1435. int *nsegs, int writing, struct rpcrdma_ia *ia)
  1436. {
  1437. struct rpcrdma_mr_seg *seg1 = seg;
  1438. u64 physaddrs[RPCRDMA_MAX_DATA_SEGS];
  1439. int len, pageoff, i, rc;
  1440. pageoff = offset_in_page(seg1->mr_offset);
  1441. seg1->mr_offset -= pageoff; /* start of page */
  1442. seg1->mr_len += pageoff;
  1443. len = -pageoff;
  1444. if (*nsegs > RPCRDMA_MAX_DATA_SEGS)
  1445. *nsegs = RPCRDMA_MAX_DATA_SEGS;
  1446. for (i = 0; i < *nsegs;) {
  1447. rpcrdma_map_one(ia, seg, writing);
  1448. physaddrs[i] = seg->mr_dma;
  1449. len += seg->mr_len;
  1450. ++seg;
  1451. ++i;
  1452. /* Check for holes */
  1453. if ((i < *nsegs && offset_in_page(seg->mr_offset)) ||
  1454. offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
  1455. break;
  1456. }
  1457. rc = ib_map_phys_fmr(seg1->mr_chunk.rl_mw->r.fmr,
  1458. physaddrs, i, seg1->mr_dma);
  1459. if (rc) {
  1460. dprintk("RPC: %s: failed ib_map_phys_fmr "
  1461. "%u@0x%llx+%i (%d)... status %i\n", __func__,
  1462. len, (unsigned long long)seg1->mr_dma,
  1463. pageoff, i, rc);
  1464. while (i--)
  1465. rpcrdma_unmap_one(ia, --seg);
  1466. } else {
  1467. seg1->mr_rkey = seg1->mr_chunk.rl_mw->r.fmr->rkey;
  1468. seg1->mr_base = seg1->mr_dma + pageoff;
  1469. seg1->mr_nsegs = i;
  1470. seg1->mr_len = len;
  1471. }
  1472. *nsegs = i;
  1473. return rc;
  1474. }
  1475. static int
  1476. rpcrdma_deregister_fmr_external(struct rpcrdma_mr_seg *seg,
  1477. struct rpcrdma_ia *ia)
  1478. {
  1479. struct rpcrdma_mr_seg *seg1 = seg;
  1480. LIST_HEAD(l);
  1481. int rc;
  1482. list_add(&seg1->mr_chunk.rl_mw->r.fmr->list, &l);
  1483. rc = ib_unmap_fmr(&l);
  1484. while (seg1->mr_nsegs--)
  1485. rpcrdma_unmap_one(ia, seg++);
  1486. if (rc)
  1487. dprintk("RPC: %s: failed ib_unmap_fmr,"
  1488. " status %i\n", __func__, rc);
  1489. return rc;
  1490. }
  1491. int
  1492. rpcrdma_register_external(struct rpcrdma_mr_seg *seg,
  1493. int nsegs, int writing, struct rpcrdma_xprt *r_xprt)
  1494. {
  1495. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  1496. int rc = 0;
  1497. switch (ia->ri_memreg_strategy) {
  1498. #if RPCRDMA_PERSISTENT_REGISTRATION
  1499. case RPCRDMA_ALLPHYSICAL:
  1500. rpcrdma_map_one(ia, seg, writing);
  1501. seg->mr_rkey = ia->ri_bind_mem->rkey;
  1502. seg->mr_base = seg->mr_dma;
  1503. seg->mr_nsegs = 1;
  1504. nsegs = 1;
  1505. break;
  1506. #endif
  1507. /* Registration using frmr registration */
  1508. case RPCRDMA_FRMR:
  1509. rc = rpcrdma_register_frmr_external(seg, &nsegs, writing, ia, r_xprt);
  1510. break;
  1511. /* Registration using fmr memory registration */
  1512. case RPCRDMA_MTHCAFMR:
  1513. rc = rpcrdma_register_fmr_external(seg, &nsegs, writing, ia);
  1514. break;
  1515. default:
  1516. return -1;
  1517. }
  1518. if (rc)
  1519. return -1;
  1520. return nsegs;
  1521. }
  1522. int
  1523. rpcrdma_deregister_external(struct rpcrdma_mr_seg *seg,
  1524. struct rpcrdma_xprt *r_xprt)
  1525. {
  1526. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  1527. int nsegs = seg->mr_nsegs, rc;
  1528. switch (ia->ri_memreg_strategy) {
  1529. #if RPCRDMA_PERSISTENT_REGISTRATION
  1530. case RPCRDMA_ALLPHYSICAL:
  1531. rpcrdma_unmap_one(ia, seg);
  1532. break;
  1533. #endif
  1534. case RPCRDMA_FRMR:
  1535. rc = rpcrdma_deregister_frmr_external(seg, ia, r_xprt);
  1536. break;
  1537. case RPCRDMA_MTHCAFMR:
  1538. rc = rpcrdma_deregister_fmr_external(seg, ia);
  1539. break;
  1540. default:
  1541. break;
  1542. }
  1543. return nsegs;
  1544. }
  1545. /*
  1546. * Prepost any receive buffer, then post send.
  1547. *
  1548. * Receive buffer is donated to hardware, reclaimed upon recv completion.
  1549. */
  1550. int
  1551. rpcrdma_ep_post(struct rpcrdma_ia *ia,
  1552. struct rpcrdma_ep *ep,
  1553. struct rpcrdma_req *req)
  1554. {
  1555. struct ib_send_wr send_wr, *send_wr_fail;
  1556. struct rpcrdma_rep *rep = req->rl_reply;
  1557. int rc;
  1558. if (rep) {
  1559. rc = rpcrdma_ep_post_recv(ia, ep, rep);
  1560. if (rc)
  1561. goto out;
  1562. req->rl_reply = NULL;
  1563. }
  1564. send_wr.next = NULL;
  1565. send_wr.wr_id = 0ULL; /* no send cookie */
  1566. send_wr.sg_list = req->rl_send_iov;
  1567. send_wr.num_sge = req->rl_niovs;
  1568. send_wr.opcode = IB_WR_SEND;
  1569. if (send_wr.num_sge == 4) /* no need to sync any pad (constant) */
  1570. ib_dma_sync_single_for_device(ia->ri_id->device,
  1571. req->rl_send_iov[3].addr, req->rl_send_iov[3].length,
  1572. DMA_TO_DEVICE);
  1573. ib_dma_sync_single_for_device(ia->ri_id->device,
  1574. req->rl_send_iov[1].addr, req->rl_send_iov[1].length,
  1575. DMA_TO_DEVICE);
  1576. ib_dma_sync_single_for_device(ia->ri_id->device,
  1577. req->rl_send_iov[0].addr, req->rl_send_iov[0].length,
  1578. DMA_TO_DEVICE);
  1579. if (DECR_CQCOUNT(ep) > 0)
  1580. send_wr.send_flags = 0;
  1581. else { /* Provider must take a send completion every now and then */
  1582. INIT_CQCOUNT(ep);
  1583. send_wr.send_flags = IB_SEND_SIGNALED;
  1584. }
  1585. rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
  1586. if (rc)
  1587. dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
  1588. rc);
  1589. out:
  1590. return rc;
  1591. }
  1592. /*
  1593. * (Re)post a receive buffer.
  1594. */
  1595. int
  1596. rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
  1597. struct rpcrdma_ep *ep,
  1598. struct rpcrdma_rep *rep)
  1599. {
  1600. struct ib_recv_wr recv_wr, *recv_wr_fail;
  1601. int rc;
  1602. recv_wr.next = NULL;
  1603. recv_wr.wr_id = (u64) (unsigned long) rep;
  1604. recv_wr.sg_list = &rep->rr_iov;
  1605. recv_wr.num_sge = 1;
  1606. ib_dma_sync_single_for_cpu(ia->ri_id->device,
  1607. rep->rr_iov.addr, rep->rr_iov.length, DMA_BIDIRECTIONAL);
  1608. rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
  1609. if (rc)
  1610. dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
  1611. rc);
  1612. return rc;
  1613. }