verbs.c 42 KB

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