ib_send.c 26 KB

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