ib_send.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * Copyright (c) 2006 Oracle. 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
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/in.h>
  35. #include <linux/device.h>
  36. #include <linux/dmapool.h>
  37. #include "rds.h"
  38. #include "rdma.h"
  39. #include "ib.h"
  40. static void rds_ib_send_rdma_complete(struct rds_message *rm,
  41. int wc_status)
  42. {
  43. int notify_status;
  44. switch (wc_status) {
  45. case IB_WC_WR_FLUSH_ERR:
  46. return;
  47. case IB_WC_SUCCESS:
  48. notify_status = RDS_RDMA_SUCCESS;
  49. break;
  50. case IB_WC_REM_ACCESS_ERR:
  51. notify_status = RDS_RDMA_REMOTE_ERROR;
  52. break;
  53. default:
  54. notify_status = RDS_RDMA_OTHER_ERROR;
  55. break;
  56. }
  57. rds_rdma_send_complete(rm, notify_status);
  58. }
  59. static void rds_ib_send_unmap_rdma(struct rds_ib_connection *ic,
  60. struct rds_rdma_op *op)
  61. {
  62. if (op->r_mapped) {
  63. ib_dma_unmap_sg(ic->i_cm_id->device,
  64. op->r_sg, op->r_nents,
  65. op->r_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  66. op->r_mapped = 0;
  67. }
  68. }
  69. static void rds_ib_send_unmap_rm(struct rds_ib_connection *ic,
  70. struct rds_ib_send_work *send,
  71. int wc_status)
  72. {
  73. struct rds_message *rm = send->s_rm;
  74. rdsdebug("ic %p send %p rm %p\n", ic, send, rm);
  75. ib_dma_unmap_sg(ic->i_cm_id->device,
  76. rm->m_sg, rm->m_nents,
  77. DMA_TO_DEVICE);
  78. if (rm->m_rdma_op != NULL) {
  79. rds_ib_send_unmap_rdma(ic, rm->m_rdma_op);
  80. /* If the user asked for a completion notification on this
  81. * message, we can implement three different semantics:
  82. * 1. Notify when we received the ACK on the RDS message
  83. * that was queued with the RDMA. This provides reliable
  84. * notification of RDMA status at the expense of a one-way
  85. * packet delay.
  86. * 2. Notify when the IB stack gives us the completion event for
  87. * the RDMA operation.
  88. * 3. Notify when the IB stack gives us the completion event for
  89. * the accompanying RDS messages.
  90. * Here, we implement approach #3. To implement approach #2,
  91. * call rds_rdma_send_complete from the cq_handler. To implement #1,
  92. * don't call rds_rdma_send_complete at all, and fall back to the notify
  93. * handling in the ACK processing code.
  94. *
  95. * Note: There's no need to explicitly sync any RDMA buffers using
  96. * ib_dma_sync_sg_for_cpu - the completion for the RDMA
  97. * operation itself unmapped the RDMA buffers, which takes care
  98. * of synching.
  99. */
  100. rds_ib_send_rdma_complete(rm, wc_status);
  101. if (rm->m_rdma_op->r_write)
  102. rds_stats_add(s_send_rdma_bytes, rm->m_rdma_op->r_bytes);
  103. else
  104. rds_stats_add(s_recv_rdma_bytes, rm->m_rdma_op->r_bytes);
  105. }
  106. /* If anyone waited for this message to get flushed out, wake
  107. * them up now */
  108. rds_message_unmapped(rm);
  109. rds_message_put(rm);
  110. send->s_rm = NULL;
  111. }
  112. void rds_ib_send_init_ring(struct rds_ib_connection *ic)
  113. {
  114. struct rds_ib_send_work *send;
  115. u32 i;
  116. for (i = 0, send = ic->i_sends; i < ic->i_send_ring.w_nr; i++, send++) {
  117. struct ib_sge *sge;
  118. send->s_rm = NULL;
  119. send->s_op = NULL;
  120. send->s_wr.wr_id = i;
  121. send->s_wr.sg_list = send->s_sge;
  122. send->s_wr.num_sge = 1;
  123. send->s_wr.opcode = IB_WR_SEND;
  124. send->s_wr.send_flags = 0;
  125. send->s_wr.ex.imm_data = 0;
  126. sge = rds_ib_data_sge(ic, send->s_sge);
  127. sge->lkey = ic->i_mr->lkey;
  128. sge = rds_ib_header_sge(ic, send->s_sge);
  129. sge->addr = ic->i_send_hdrs_dma + (i * sizeof(struct rds_header));
  130. sge->length = sizeof(struct rds_header);
  131. sge->lkey = ic->i_mr->lkey;
  132. }
  133. }
  134. void rds_ib_send_clear_ring(struct rds_ib_connection *ic)
  135. {
  136. struct rds_ib_send_work *send;
  137. u32 i;
  138. for (i = 0, send = ic->i_sends; i < ic->i_send_ring.w_nr; i++, send++) {
  139. if (send->s_wr.opcode == 0xdead)
  140. continue;
  141. if (send->s_rm)
  142. rds_ib_send_unmap_rm(ic, send, IB_WC_WR_FLUSH_ERR);
  143. if (send->s_op)
  144. rds_ib_send_unmap_rdma(ic, send->s_op);
  145. }
  146. }
  147. /*
  148. * The _oldest/_free ring operations here race cleanly with the alloc/unalloc
  149. * operations performed in the send path. As the sender allocs and potentially
  150. * unallocs the next free entry in the ring it doesn't alter which is
  151. * the next to be freed, which is what this is concerned with.
  152. */
  153. void rds_ib_send_cq_comp_handler(struct ib_cq *cq, void *context)
  154. {
  155. struct rds_connection *conn = context;
  156. struct rds_ib_connection *ic = conn->c_transport_data;
  157. struct ib_wc wc;
  158. struct rds_ib_send_work *send;
  159. u32 completed;
  160. u32 oldest;
  161. u32 i = 0;
  162. int ret;
  163. rdsdebug("cq %p conn %p\n", cq, conn);
  164. rds_ib_stats_inc(s_ib_tx_cq_call);
  165. ret = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  166. if (ret)
  167. rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
  168. while (ib_poll_cq(cq, 1, &wc) > 0) {
  169. rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n",
  170. (unsigned long long)wc.wr_id, wc.status, wc.byte_len,
  171. be32_to_cpu(wc.ex.imm_data));
  172. rds_ib_stats_inc(s_ib_tx_cq_event);
  173. if (wc.wr_id == RDS_IB_ACK_WR_ID) {
  174. if (ic->i_ack_queued + HZ/2 < jiffies)
  175. rds_ib_stats_inc(s_ib_tx_stalled);
  176. rds_ib_ack_send_complete(ic);
  177. continue;
  178. }
  179. oldest = rds_ib_ring_oldest(&ic->i_send_ring);
  180. completed = rds_ib_ring_completed(&ic->i_send_ring, wc.wr_id, oldest);
  181. for (i = 0; i < completed; i++) {
  182. send = &ic->i_sends[oldest];
  183. /* In the error case, wc.opcode sometimes contains garbage */
  184. switch (send->s_wr.opcode) {
  185. case IB_WR_SEND:
  186. if (send->s_rm)
  187. rds_ib_send_unmap_rm(ic, send, wc.status);
  188. break;
  189. case IB_WR_RDMA_WRITE:
  190. case IB_WR_RDMA_READ:
  191. /* Nothing to be done - the SG list will be unmapped
  192. * when the SEND completes. */
  193. break;
  194. default:
  195. if (printk_ratelimit())
  196. printk(KERN_NOTICE
  197. "RDS/IB: %s: unexpected opcode 0x%x in WR!\n",
  198. __func__, send->s_wr.opcode);
  199. break;
  200. }
  201. send->s_wr.opcode = 0xdead;
  202. send->s_wr.num_sge = 1;
  203. if (send->s_queued + HZ/2 < jiffies)
  204. rds_ib_stats_inc(s_ib_tx_stalled);
  205. /* If a RDMA operation produced an error, signal this right
  206. * away. If we don't, the subsequent SEND that goes with this
  207. * RDMA will be canceled with ERR_WFLUSH, and the application
  208. * never learn that the RDMA failed. */
  209. if (unlikely(wc.status == IB_WC_REM_ACCESS_ERR && send->s_op)) {
  210. struct rds_message *rm;
  211. rm = rds_send_get_message(conn, send->s_op);
  212. if (rm)
  213. rds_ib_send_rdma_complete(rm, wc.status);
  214. }
  215. oldest = (oldest + 1) % ic->i_send_ring.w_nr;
  216. }
  217. rds_ib_ring_free(&ic->i_send_ring, completed);
  218. if (test_and_clear_bit(RDS_LL_SEND_FULL, &conn->c_flags)
  219. || test_bit(0, &conn->c_map_queued))
  220. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  221. /* We expect errors as the qp is drained during shutdown */
  222. if (wc.status != IB_WC_SUCCESS && rds_conn_up(conn)) {
  223. rds_ib_conn_error(conn,
  224. "send completion on %pI4 "
  225. "had status %u, disconnecting and reconnecting\n",
  226. &conn->c_faddr, wc.status);
  227. }
  228. }
  229. }
  230. /*
  231. * This is the main function for allocating credits when sending
  232. * messages.
  233. *
  234. * Conceptually, we have two counters:
  235. * - send credits: this tells us how many WRs we're allowed
  236. * to submit without overruning the reciever's queue. For
  237. * each SEND WR we post, we decrement this by one.
  238. *
  239. * - posted credits: this tells us how many WRs we recently
  240. * posted to the receive queue. This value is transferred
  241. * to the peer as a "credit update" in a RDS header field.
  242. * Every time we transmit credits to the peer, we subtract
  243. * the amount of transferred credits from this counter.
  244. *
  245. * It is essential that we avoid situations where both sides have
  246. * exhausted their send credits, and are unable to send new credits
  247. * to the peer. We achieve this by requiring that we send at least
  248. * one credit update to the peer before exhausting our credits.
  249. * When new credits arrive, we subtract one credit that is withheld
  250. * until we've posted new buffers and are ready to transmit these
  251. * credits (see rds_ib_send_add_credits below).
  252. *
  253. * The RDS send code is essentially single-threaded; rds_send_xmit
  254. * grabs c_send_lock to ensure exclusive access to the send ring.
  255. * However, the ACK sending code is independent and can race with
  256. * message SENDs.
  257. *
  258. * In the send path, we need to update the counters for send credits
  259. * and the counter of posted buffers atomically - when we use the
  260. * last available credit, we cannot allow another thread to race us
  261. * and grab the posted credits counter. Hence, we have to use a
  262. * spinlock to protect the credit counter, or use atomics.
  263. *
  264. * Spinlocks shared between the send and the receive path are bad,
  265. * because they create unnecessary delays. An early implementation
  266. * using a spinlock showed a 5% degradation in throughput at some
  267. * loads.
  268. *
  269. * This implementation avoids spinlocks completely, putting both
  270. * counters into a single atomic, and updating that atomic using
  271. * atomic_add (in the receive path, when receiving fresh credits),
  272. * and using atomic_cmpxchg when updating the two counters.
  273. */
  274. int rds_ib_send_grab_credits(struct rds_ib_connection *ic,
  275. u32 wanted, u32 *adv_credits, int need_posted)
  276. {
  277. unsigned int avail, posted, got = 0, advertise;
  278. long oldval, newval;
  279. *adv_credits = 0;
  280. if (!ic->i_flowctl)
  281. return wanted;
  282. try_again:
  283. advertise = 0;
  284. oldval = newval = atomic_read(&ic->i_credits);
  285. posted = IB_GET_POST_CREDITS(oldval);
  286. avail = IB_GET_SEND_CREDITS(oldval);
  287. rdsdebug("rds_ib_send_grab_credits(%u): credits=%u posted=%u\n",
  288. wanted, avail, posted);
  289. /* The last credit must be used to send a credit update. */
  290. if (avail && !posted)
  291. avail--;
  292. if (avail < wanted) {
  293. struct rds_connection *conn = ic->i_cm_id->context;
  294. /* Oops, there aren't that many credits left! */
  295. set_bit(RDS_LL_SEND_FULL, &conn->c_flags);
  296. got = avail;
  297. } else {
  298. /* Sometimes you get what you want, lalala. */
  299. got = wanted;
  300. }
  301. newval -= IB_SET_SEND_CREDITS(got);
  302. /*
  303. * If need_posted is non-zero, then the caller wants
  304. * the posted regardless of whether any send credits are
  305. * available.
  306. */
  307. if (posted && (got || need_posted)) {
  308. advertise = min_t(unsigned int, posted, RDS_MAX_ADV_CREDIT);
  309. newval -= IB_SET_POST_CREDITS(advertise);
  310. }
  311. /* Finally bill everything */
  312. if (atomic_cmpxchg(&ic->i_credits, oldval, newval) != oldval)
  313. goto try_again;
  314. *adv_credits = advertise;
  315. return got;
  316. }
  317. void rds_ib_send_add_credits(struct rds_connection *conn, unsigned int credits)
  318. {
  319. struct rds_ib_connection *ic = conn->c_transport_data;
  320. if (credits == 0)
  321. return;
  322. rdsdebug("rds_ib_send_add_credits(%u): current=%u%s\n",
  323. credits,
  324. IB_GET_SEND_CREDITS(atomic_read(&ic->i_credits)),
  325. test_bit(RDS_LL_SEND_FULL, &conn->c_flags) ? ", ll_send_full" : "");
  326. atomic_add(IB_SET_SEND_CREDITS(credits), &ic->i_credits);
  327. if (test_and_clear_bit(RDS_LL_SEND_FULL, &conn->c_flags))
  328. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  329. WARN_ON(IB_GET_SEND_CREDITS(credits) >= 16384);
  330. rds_ib_stats_inc(s_ib_rx_credit_updates);
  331. }
  332. void rds_ib_advertise_credits(struct rds_connection *conn, unsigned int posted)
  333. {
  334. struct rds_ib_connection *ic = conn->c_transport_data;
  335. if (posted == 0)
  336. return;
  337. atomic_add(IB_SET_POST_CREDITS(posted), &ic->i_credits);
  338. /* Decide whether to send an update to the peer now.
  339. * If we would send a credit update for every single buffer we
  340. * post, we would end up with an ACK storm (ACK arrives,
  341. * consumes buffer, we refill the ring, send ACK to remote
  342. * advertising the newly posted buffer... ad inf)
  343. *
  344. * Performance pretty much depends on how often we send
  345. * credit updates - too frequent updates mean lots of ACKs.
  346. * Too infrequent updates, and the peer will run out of
  347. * credits and has to throttle.
  348. * For the time being, 16 seems to be a good compromise.
  349. */
  350. if (IB_GET_POST_CREDITS(atomic_read(&ic->i_credits)) >= 16)
  351. set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  352. }
  353. static inline void
  354. rds_ib_xmit_populate_wr(struct rds_ib_connection *ic,
  355. struct rds_ib_send_work *send, unsigned int pos,
  356. unsigned long buffer, unsigned int length,
  357. int send_flags)
  358. {
  359. struct ib_sge *sge;
  360. WARN_ON(pos != send - ic->i_sends);
  361. send->s_wr.send_flags = send_flags;
  362. send->s_wr.opcode = IB_WR_SEND;
  363. send->s_wr.num_sge = 2;
  364. send->s_wr.next = NULL;
  365. send->s_queued = jiffies;
  366. send->s_op = NULL;
  367. if (length != 0) {
  368. sge = rds_ib_data_sge(ic, send->s_sge);
  369. sge->addr = buffer;
  370. sge->length = length;
  371. sge->lkey = ic->i_mr->lkey;
  372. sge = rds_ib_header_sge(ic, send->s_sge);
  373. } else {
  374. /* We're sending a packet with no payload. There is only
  375. * one SGE */
  376. send->s_wr.num_sge = 1;
  377. sge = &send->s_sge[0];
  378. }
  379. sge->addr = ic->i_send_hdrs_dma + (pos * sizeof(struct rds_header));
  380. sge->length = sizeof(struct rds_header);
  381. sge->lkey = ic->i_mr->lkey;
  382. }
  383. /*
  384. * This can be called multiple times for a given message. The first time
  385. * we see a message we map its scatterlist into the IB device so that
  386. * we can provide that mapped address to the IB scatter gather entries
  387. * in the IB work requests. We translate the scatterlist into a series
  388. * of work requests that fragment the message. These work requests complete
  389. * in order so we pass ownership of the message to the completion handler
  390. * once we send the final fragment.
  391. *
  392. * The RDS core uses the c_send_lock to only enter this function once
  393. * per connection. This makes sure that the tx ring alloc/unalloc pairs
  394. * don't get out of sync and confuse the ring.
  395. */
  396. int rds_ib_xmit(struct rds_connection *conn, struct rds_message *rm,
  397. unsigned int hdr_off, unsigned int sg, unsigned int off)
  398. {
  399. struct rds_ib_connection *ic = conn->c_transport_data;
  400. struct ib_device *dev = ic->i_cm_id->device;
  401. struct rds_ib_send_work *send = NULL;
  402. struct rds_ib_send_work *first;
  403. struct rds_ib_send_work *prev;
  404. struct ib_send_wr *failed_wr;
  405. struct scatterlist *scat;
  406. u32 pos;
  407. u32 i;
  408. u32 work_alloc;
  409. u32 credit_alloc;
  410. u32 posted;
  411. u32 adv_credits = 0;
  412. int send_flags = 0;
  413. int sent;
  414. int ret;
  415. int flow_controlled = 0;
  416. BUG_ON(off % RDS_FRAG_SIZE);
  417. BUG_ON(hdr_off != 0 && hdr_off != sizeof(struct rds_header));
  418. /* FIXME we may overallocate here */
  419. if (be32_to_cpu(rm->m_inc.i_hdr.h_len) == 0)
  420. i = 1;
  421. else
  422. i = ceil(be32_to_cpu(rm->m_inc.i_hdr.h_len), RDS_FRAG_SIZE);
  423. work_alloc = rds_ib_ring_alloc(&ic->i_send_ring, i, &pos);
  424. if (work_alloc == 0) {
  425. set_bit(RDS_LL_SEND_FULL, &conn->c_flags);
  426. rds_ib_stats_inc(s_ib_tx_ring_full);
  427. ret = -ENOMEM;
  428. goto out;
  429. }
  430. credit_alloc = work_alloc;
  431. if (ic->i_flowctl) {
  432. credit_alloc = rds_ib_send_grab_credits(ic, work_alloc, &posted, 0);
  433. adv_credits += posted;
  434. if (credit_alloc < work_alloc) {
  435. rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc - credit_alloc);
  436. work_alloc = credit_alloc;
  437. flow_controlled++;
  438. }
  439. if (work_alloc == 0) {
  440. rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
  441. rds_ib_stats_inc(s_ib_tx_throttle);
  442. ret = -ENOMEM;
  443. goto out;
  444. }
  445. }
  446. /* map the message the first time we see it */
  447. if (ic->i_rm == NULL) {
  448. /*
  449. printk(KERN_NOTICE "rds_ib_xmit prep msg dport=%u flags=0x%x len=%d\n",
  450. be16_to_cpu(rm->m_inc.i_hdr.h_dport),
  451. rm->m_inc.i_hdr.h_flags,
  452. be32_to_cpu(rm->m_inc.i_hdr.h_len));
  453. */
  454. if (rm->m_nents) {
  455. rm->m_count = ib_dma_map_sg(dev,
  456. rm->m_sg, rm->m_nents, DMA_TO_DEVICE);
  457. rdsdebug("ic %p mapping rm %p: %d\n", ic, rm, rm->m_count);
  458. if (rm->m_count == 0) {
  459. rds_ib_stats_inc(s_ib_tx_sg_mapping_failure);
  460. rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
  461. ret = -ENOMEM; /* XXX ? */
  462. goto out;
  463. }
  464. } else {
  465. rm->m_count = 0;
  466. }
  467. ic->i_unsignaled_wrs = rds_ib_sysctl_max_unsig_wrs;
  468. ic->i_unsignaled_bytes = rds_ib_sysctl_max_unsig_bytes;
  469. rds_message_addref(rm);
  470. ic->i_rm = rm;
  471. /* Finalize the header */
  472. if (test_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags))
  473. rm->m_inc.i_hdr.h_flags |= RDS_FLAG_ACK_REQUIRED;
  474. if (test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))
  475. rm->m_inc.i_hdr.h_flags |= RDS_FLAG_RETRANSMITTED;
  476. /* If it has a RDMA op, tell the peer we did it. This is
  477. * used by the peer to release use-once RDMA MRs. */
  478. if (rm->m_rdma_op) {
  479. struct rds_ext_header_rdma ext_hdr;
  480. ext_hdr.h_rdma_rkey = cpu_to_be32(rm->m_rdma_op->r_key);
  481. rds_message_add_extension(&rm->m_inc.i_hdr,
  482. RDS_EXTHDR_RDMA, &ext_hdr, sizeof(ext_hdr));
  483. }
  484. if (rm->m_rdma_cookie) {
  485. rds_message_add_rdma_dest_extension(&rm->m_inc.i_hdr,
  486. rds_rdma_cookie_key(rm->m_rdma_cookie),
  487. rds_rdma_cookie_offset(rm->m_rdma_cookie));
  488. }
  489. /* Note - rds_ib_piggyb_ack clears the ACK_REQUIRED bit, so
  490. * we should not do this unless we have a chance of at least
  491. * sticking the header into the send ring. Which is why we
  492. * should call rds_ib_ring_alloc first. */
  493. rm->m_inc.i_hdr.h_ack = cpu_to_be64(rds_ib_piggyb_ack(ic));
  494. rds_message_make_checksum(&rm->m_inc.i_hdr);
  495. /*
  496. * Update adv_credits since we reset the ACK_REQUIRED bit.
  497. */
  498. rds_ib_send_grab_credits(ic, 0, &posted, 1);
  499. adv_credits += posted;
  500. BUG_ON(adv_credits > 255);
  501. } else if (ic->i_rm != rm)
  502. BUG();
  503. send = &ic->i_sends[pos];
  504. first = send;
  505. prev = NULL;
  506. scat = &rm->m_sg[sg];
  507. sent = 0;
  508. i = 0;
  509. /* Sometimes you want to put a fence between an RDMA
  510. * READ and the following SEND.
  511. * We could either do this all the time
  512. * or when requested by the user. Right now, we let
  513. * the application choose.
  514. */
  515. if (rm->m_rdma_op && rm->m_rdma_op->r_fence)
  516. send_flags = IB_SEND_FENCE;
  517. /*
  518. * We could be copying the header into the unused tail of the page.
  519. * That would need to be changed in the future when those pages might
  520. * be mapped userspace pages or page cache pages. So instead we always
  521. * use a second sge and our long-lived ring of mapped headers. We send
  522. * the header after the data so that the data payload can be aligned on
  523. * the receiver.
  524. */
  525. /* handle a 0-len message */
  526. if (be32_to_cpu(rm->m_inc.i_hdr.h_len) == 0) {
  527. rds_ib_xmit_populate_wr(ic, send, pos, 0, 0, send_flags);
  528. goto add_header;
  529. }
  530. /* if there's data reference it with a chain of work reqs */
  531. for (; i < work_alloc && scat != &rm->m_sg[rm->m_count]; i++) {
  532. unsigned int len;
  533. send = &ic->i_sends[pos];
  534. len = min(RDS_FRAG_SIZE, ib_sg_dma_len(dev, scat) - off);
  535. rds_ib_xmit_populate_wr(ic, send, pos,
  536. ib_sg_dma_address(dev, scat) + off, len,
  537. send_flags);
  538. /*
  539. * We want to delay signaling completions just enough to get
  540. * the batching benefits but not so much that we create dead time
  541. * on the wire.
  542. */
  543. if (ic->i_unsignaled_wrs-- == 0) {
  544. ic->i_unsignaled_wrs = rds_ib_sysctl_max_unsig_wrs;
  545. send->s_wr.send_flags |= IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  546. }
  547. ic->i_unsignaled_bytes -= len;
  548. if (ic->i_unsignaled_bytes <= 0) {
  549. ic->i_unsignaled_bytes = rds_ib_sysctl_max_unsig_bytes;
  550. send->s_wr.send_flags |= IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  551. }
  552. /*
  553. * Always signal the last one if we're stopping due to flow control.
  554. */
  555. if (flow_controlled && i == (work_alloc-1))
  556. send->s_wr.send_flags |= IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  557. rdsdebug("send %p wr %p num_sge %u next %p\n", send,
  558. &send->s_wr, send->s_wr.num_sge, send->s_wr.next);
  559. sent += len;
  560. off += len;
  561. if (off == ib_sg_dma_len(dev, scat)) {
  562. scat++;
  563. off = 0;
  564. }
  565. add_header:
  566. /* Tack on the header after the data. The header SGE should already
  567. * have been set up to point to the right header buffer. */
  568. memcpy(&ic->i_send_hdrs[pos], &rm->m_inc.i_hdr, sizeof(struct rds_header));
  569. if (0) {
  570. struct rds_header *hdr = &ic->i_send_hdrs[pos];
  571. printk(KERN_NOTICE "send WR dport=%u flags=0x%x len=%d\n",
  572. be16_to_cpu(hdr->h_dport),
  573. hdr->h_flags,
  574. be32_to_cpu(hdr->h_len));
  575. }
  576. if (adv_credits) {
  577. struct rds_header *hdr = &ic->i_send_hdrs[pos];
  578. /* add credit and redo the header checksum */
  579. hdr->h_credit = adv_credits;
  580. rds_message_make_checksum(hdr);
  581. adv_credits = 0;
  582. rds_ib_stats_inc(s_ib_tx_credit_updates);
  583. }
  584. if (prev)
  585. prev->s_wr.next = &send->s_wr;
  586. prev = send;
  587. pos = (pos + 1) % ic->i_send_ring.w_nr;
  588. }
  589. /* Account the RDS header in the number of bytes we sent, but just once.
  590. * The caller has no concept of fragmentation. */
  591. if (hdr_off == 0)
  592. sent += sizeof(struct rds_header);
  593. /* if we finished the message then send completion owns it */
  594. if (scat == &rm->m_sg[rm->m_count]) {
  595. prev->s_rm = ic->i_rm;
  596. prev->s_wr.send_flags |= IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  597. ic->i_rm = NULL;
  598. }
  599. if (i < work_alloc) {
  600. rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc - i);
  601. work_alloc = i;
  602. }
  603. if (ic->i_flowctl && i < credit_alloc)
  604. rds_ib_send_add_credits(conn, credit_alloc - i);
  605. /* XXX need to worry about failed_wr and partial sends. */
  606. failed_wr = &first->s_wr;
  607. ret = ib_post_send(ic->i_cm_id->qp, &first->s_wr, &failed_wr);
  608. rdsdebug("ic %p first %p (wr %p) ret %d wr %p\n", ic,
  609. first, &first->s_wr, ret, failed_wr);
  610. BUG_ON(failed_wr != &first->s_wr);
  611. if (ret) {
  612. printk(KERN_WARNING "RDS/IB: ib_post_send to %pI4 "
  613. "returned %d\n", &conn->c_faddr, ret);
  614. rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
  615. if (prev->s_rm) {
  616. ic->i_rm = prev->s_rm;
  617. prev->s_rm = NULL;
  618. }
  619. /* Finesse this later */
  620. BUG();
  621. goto out;
  622. }
  623. ret = sent;
  624. out:
  625. BUG_ON(adv_credits);
  626. return ret;
  627. }
  628. int rds_ib_xmit_rdma(struct rds_connection *conn, struct rds_rdma_op *op)
  629. {
  630. struct rds_ib_connection *ic = conn->c_transport_data;
  631. struct rds_ib_send_work *send = NULL;
  632. struct rds_ib_send_work *first;
  633. struct rds_ib_send_work *prev;
  634. struct ib_send_wr *failed_wr;
  635. struct rds_ib_device *rds_ibdev;
  636. struct scatterlist *scat;
  637. unsigned long len;
  638. u64 remote_addr = op->r_remote_addr;
  639. u32 pos;
  640. u32 work_alloc;
  641. u32 i;
  642. u32 j;
  643. int sent;
  644. int ret;
  645. int num_sge;
  646. rds_ibdev = ib_get_client_data(ic->i_cm_id->device, &rds_ib_client);
  647. /* map the message the first time we see it */
  648. if (!op->r_mapped) {
  649. op->r_count = ib_dma_map_sg(ic->i_cm_id->device,
  650. op->r_sg, op->r_nents, (op->r_write) ?
  651. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  652. rdsdebug("ic %p mapping op %p: %d\n", ic, op, op->r_count);
  653. if (op->r_count == 0) {
  654. rds_ib_stats_inc(s_ib_tx_sg_mapping_failure);
  655. ret = -ENOMEM; /* XXX ? */
  656. goto out;
  657. }
  658. op->r_mapped = 1;
  659. }
  660. /*
  661. * Instead of knowing how to return a partial rdma read/write we insist that there
  662. * be enough work requests to send the entire message.
  663. */
  664. i = ceil(op->r_count, rds_ibdev->max_sge);
  665. work_alloc = rds_ib_ring_alloc(&ic->i_send_ring, i, &pos);
  666. if (work_alloc != i) {
  667. rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
  668. rds_ib_stats_inc(s_ib_tx_ring_full);
  669. ret = -ENOMEM;
  670. goto out;
  671. }
  672. send = &ic->i_sends[pos];
  673. first = send;
  674. prev = NULL;
  675. scat = &op->r_sg[0];
  676. sent = 0;
  677. num_sge = op->r_count;
  678. for (i = 0; i < work_alloc && scat != &op->r_sg[op->r_count]; i++) {
  679. send->s_wr.send_flags = 0;
  680. send->s_queued = jiffies;
  681. /*
  682. * We want to delay signaling completions just enough to get
  683. * the batching benefits but not so much that we create dead time on the wire.
  684. */
  685. if (ic->i_unsignaled_wrs-- == 0) {
  686. ic->i_unsignaled_wrs = rds_ib_sysctl_max_unsig_wrs;
  687. send->s_wr.send_flags = IB_SEND_SIGNALED;
  688. }
  689. send->s_wr.opcode = op->r_write ? IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
  690. send->s_wr.wr.rdma.remote_addr = remote_addr;
  691. send->s_wr.wr.rdma.rkey = op->r_key;
  692. send->s_op = op;
  693. if (num_sge > rds_ibdev->max_sge) {
  694. send->s_wr.num_sge = rds_ibdev->max_sge;
  695. num_sge -= rds_ibdev->max_sge;
  696. } else {
  697. send->s_wr.num_sge = num_sge;
  698. }
  699. send->s_wr.next = NULL;
  700. if (prev)
  701. prev->s_wr.next = &send->s_wr;
  702. for (j = 0; j < send->s_wr.num_sge && scat != &op->r_sg[op->r_count]; j++) {
  703. len = ib_sg_dma_len(ic->i_cm_id->device, scat);
  704. send->s_sge[j].addr =
  705. ib_sg_dma_address(ic->i_cm_id->device, scat);
  706. send->s_sge[j].length = len;
  707. send->s_sge[j].lkey = ic->i_mr->lkey;
  708. sent += len;
  709. rdsdebug("ic %p sent %d remote_addr %llu\n", ic, sent, remote_addr);
  710. remote_addr += len;
  711. scat++;
  712. }
  713. rdsdebug("send %p wr %p num_sge %u next %p\n", send,
  714. &send->s_wr, send->s_wr.num_sge, send->s_wr.next);
  715. prev = send;
  716. if (++send == &ic->i_sends[ic->i_send_ring.w_nr])
  717. send = ic->i_sends;
  718. }
  719. /* if we finished the message then send completion owns it */
  720. if (scat == &op->r_sg[op->r_count])
  721. prev->s_wr.send_flags = IB_SEND_SIGNALED;
  722. if (i < work_alloc) {
  723. rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc - i);
  724. work_alloc = i;
  725. }
  726. failed_wr = &first->s_wr;
  727. ret = ib_post_send(ic->i_cm_id->qp, &first->s_wr, &failed_wr);
  728. rdsdebug("ic %p first %p (wr %p) ret %d wr %p\n", ic,
  729. first, &first->s_wr, ret, failed_wr);
  730. BUG_ON(failed_wr != &first->s_wr);
  731. if (ret) {
  732. printk(KERN_WARNING "RDS/IB: rdma ib_post_send to %pI4 "
  733. "returned %d\n", &conn->c_faddr, ret);
  734. rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
  735. goto out;
  736. }
  737. if (unlikely(failed_wr != &first->s_wr)) {
  738. printk(KERN_WARNING "RDS/IB: ib_post_send() rc=%d, but failed_wqe updated!\n", ret);
  739. BUG_ON(failed_wr != &first->s_wr);
  740. }
  741. out:
  742. return ret;
  743. }
  744. void rds_ib_xmit_complete(struct rds_connection *conn)
  745. {
  746. struct rds_ib_connection *ic = conn->c_transport_data;
  747. /* We may have a pending ACK or window update we were unable
  748. * to send previously (due to flow control). Try again. */
  749. rds_ib_attempt_ack(ic);
  750. }