iw_send.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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 <linux/ratelimit.h>
  38. #include "rds.h"
  39. #include "iw.h"
  40. static void rds_iw_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_iw_send_unmap_rdma(struct rds_iw_connection *ic,
  60. struct rm_rdma_op *op)
  61. {
  62. if (op->op_mapped) {
  63. ib_dma_unmap_sg(ic->i_cm_id->device,
  64. op->op_sg, op->op_nents,
  65. op->op_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  66. op->op_mapped = 0;
  67. }
  68. }
  69. static void rds_iw_send_unmap_rm(struct rds_iw_connection *ic,
  70. struct rds_iw_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->data.op_sg, rm->data.op_nents,
  77. DMA_TO_DEVICE);
  78. if (rm->rdma.op_active) {
  79. rds_iw_send_unmap_rdma(ic, &rm->rdma);
  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_iw_send_rdma_complete(rm, wc_status);
  101. if (rm->rdma.op_write)
  102. rds_stats_add(s_send_rdma_bytes, rm->rdma.op_bytes);
  103. else
  104. rds_stats_add(s_recv_rdma_bytes, rm->rdma.op_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_iw_send_init_ring(struct rds_iw_connection *ic)
  113. {
  114. struct rds_iw_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_mapping = NULL;
  121. send->s_wr.next = NULL;
  122. send->s_wr.wr_id = i;
  123. send->s_wr.sg_list = send->s_sge;
  124. send->s_wr.num_sge = 1;
  125. send->s_wr.opcode = IB_WR_SEND;
  126. send->s_wr.send_flags = 0;
  127. send->s_wr.ex.imm_data = 0;
  128. sge = rds_iw_data_sge(ic, send->s_sge);
  129. sge->lkey = 0;
  130. sge = rds_iw_header_sge(ic, send->s_sge);
  131. sge->addr = ic->i_send_hdrs_dma + (i * sizeof(struct rds_header));
  132. sge->length = sizeof(struct rds_header);
  133. sge->lkey = 0;
  134. send->s_mr = ib_alloc_mr(ic->i_pd, IB_MR_TYPE_MEM_REG,
  135. fastreg_message_size);
  136. if (IS_ERR(send->s_mr)) {
  137. printk(KERN_WARNING "RDS/IW: ib_alloc_mr failed\n");
  138. break;
  139. }
  140. send->s_page_list = ib_alloc_fast_reg_page_list(
  141. ic->i_cm_id->device, fastreg_message_size);
  142. if (IS_ERR(send->s_page_list)) {
  143. printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_page_list failed\n");
  144. break;
  145. }
  146. }
  147. }
  148. void rds_iw_send_clear_ring(struct rds_iw_connection *ic)
  149. {
  150. struct rds_iw_send_work *send;
  151. u32 i;
  152. for (i = 0, send = ic->i_sends; i < ic->i_send_ring.w_nr; i++, send++) {
  153. BUG_ON(!send->s_mr);
  154. ib_dereg_mr(send->s_mr);
  155. BUG_ON(!send->s_page_list);
  156. ib_free_fast_reg_page_list(send->s_page_list);
  157. if (send->s_wr.opcode == 0xdead)
  158. continue;
  159. if (send->s_rm)
  160. rds_iw_send_unmap_rm(ic, send, IB_WC_WR_FLUSH_ERR);
  161. if (send->s_op)
  162. rds_iw_send_unmap_rdma(ic, send->s_op);
  163. }
  164. }
  165. /*
  166. * The _oldest/_free ring operations here race cleanly with the alloc/unalloc
  167. * operations performed in the send path. As the sender allocs and potentially
  168. * unallocs the next free entry in the ring it doesn't alter which is
  169. * the next to be freed, which is what this is concerned with.
  170. */
  171. void rds_iw_send_cq_comp_handler(struct ib_cq *cq, void *context)
  172. {
  173. struct rds_connection *conn = context;
  174. struct rds_iw_connection *ic = conn->c_transport_data;
  175. struct ib_wc wc;
  176. struct rds_iw_send_work *send;
  177. u32 completed;
  178. u32 oldest;
  179. u32 i;
  180. int ret;
  181. rdsdebug("cq %p conn %p\n", cq, conn);
  182. rds_iw_stats_inc(s_iw_tx_cq_call);
  183. ret = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  184. if (ret)
  185. rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
  186. while (ib_poll_cq(cq, 1, &wc) > 0) {
  187. rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n",
  188. (unsigned long long)wc.wr_id, wc.status, wc.byte_len,
  189. be32_to_cpu(wc.ex.imm_data));
  190. rds_iw_stats_inc(s_iw_tx_cq_event);
  191. if (wc.status != IB_WC_SUCCESS) {
  192. printk(KERN_ERR "WC Error: status = %d opcode = %d\n", wc.status, wc.opcode);
  193. break;
  194. }
  195. if (wc.opcode == IB_WC_LOCAL_INV && wc.wr_id == RDS_IW_LOCAL_INV_WR_ID) {
  196. ic->i_fastreg_posted = 0;
  197. continue;
  198. }
  199. if (wc.opcode == IB_WC_FAST_REG_MR && wc.wr_id == RDS_IW_FAST_REG_WR_ID) {
  200. ic->i_fastreg_posted = 1;
  201. continue;
  202. }
  203. if (wc.wr_id == RDS_IW_ACK_WR_ID) {
  204. if (time_after(jiffies, ic->i_ack_queued + HZ/2))
  205. rds_iw_stats_inc(s_iw_tx_stalled);
  206. rds_iw_ack_send_complete(ic);
  207. continue;
  208. }
  209. oldest = rds_iw_ring_oldest(&ic->i_send_ring);
  210. completed = rds_iw_ring_completed(&ic->i_send_ring, wc.wr_id, oldest);
  211. for (i = 0; i < completed; i++) {
  212. send = &ic->i_sends[oldest];
  213. /* In the error case, wc.opcode sometimes contains garbage */
  214. switch (send->s_wr.opcode) {
  215. case IB_WR_SEND:
  216. if (send->s_rm)
  217. rds_iw_send_unmap_rm(ic, send, wc.status);
  218. break;
  219. case IB_WR_FAST_REG_MR:
  220. case IB_WR_RDMA_WRITE:
  221. case IB_WR_RDMA_READ:
  222. case IB_WR_RDMA_READ_WITH_INV:
  223. /* Nothing to be done - the SG list will be unmapped
  224. * when the SEND completes. */
  225. break;
  226. default:
  227. printk_ratelimited(KERN_NOTICE
  228. "RDS/IW: %s: unexpected opcode 0x%x in WR!\n",
  229. __func__, send->s_wr.opcode);
  230. break;
  231. }
  232. send->s_wr.opcode = 0xdead;
  233. send->s_wr.num_sge = 1;
  234. if (time_after(jiffies, send->s_queued + HZ/2))
  235. rds_iw_stats_inc(s_iw_tx_stalled);
  236. /* If a RDMA operation produced an error, signal this right
  237. * away. If we don't, the subsequent SEND that goes with this
  238. * RDMA will be canceled with ERR_WFLUSH, and the application
  239. * never learn that the RDMA failed. */
  240. if (unlikely(wc.status == IB_WC_REM_ACCESS_ERR && send->s_op)) {
  241. struct rds_message *rm;
  242. rm = rds_send_get_message(conn, send->s_op);
  243. if (rm)
  244. rds_iw_send_rdma_complete(rm, wc.status);
  245. }
  246. oldest = (oldest + 1) % ic->i_send_ring.w_nr;
  247. }
  248. rds_iw_ring_free(&ic->i_send_ring, completed);
  249. if (test_and_clear_bit(RDS_LL_SEND_FULL, &conn->c_flags) ||
  250. test_bit(0, &conn->c_map_queued))
  251. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  252. /* We expect errors as the qp is drained during shutdown */
  253. if (wc.status != IB_WC_SUCCESS && rds_conn_up(conn)) {
  254. rds_iw_conn_error(conn,
  255. "send completion on %pI4 "
  256. "had status %u, disconnecting and reconnecting\n",
  257. &conn->c_faddr, wc.status);
  258. }
  259. }
  260. }
  261. /*
  262. * This is the main function for allocating credits when sending
  263. * messages.
  264. *
  265. * Conceptually, we have two counters:
  266. * - send credits: this tells us how many WRs we're allowed
  267. * to submit without overruning the receiver's queue. For
  268. * each SEND WR we post, we decrement this by one.
  269. *
  270. * - posted credits: this tells us how many WRs we recently
  271. * posted to the receive queue. This value is transferred
  272. * to the peer as a "credit update" in a RDS header field.
  273. * Every time we transmit credits to the peer, we subtract
  274. * the amount of transferred credits from this counter.
  275. *
  276. * It is essential that we avoid situations where both sides have
  277. * exhausted their send credits, and are unable to send new credits
  278. * to the peer. We achieve this by requiring that we send at least
  279. * one credit update to the peer before exhausting our credits.
  280. * When new credits arrive, we subtract one credit that is withheld
  281. * until we've posted new buffers and are ready to transmit these
  282. * credits (see rds_iw_send_add_credits below).
  283. *
  284. * The RDS send code is essentially single-threaded; rds_send_xmit
  285. * grabs c_send_lock to ensure exclusive access to the send ring.
  286. * However, the ACK sending code is independent and can race with
  287. * message SENDs.
  288. *
  289. * In the send path, we need to update the counters for send credits
  290. * and the counter of posted buffers atomically - when we use the
  291. * last available credit, we cannot allow another thread to race us
  292. * and grab the posted credits counter. Hence, we have to use a
  293. * spinlock to protect the credit counter, or use atomics.
  294. *
  295. * Spinlocks shared between the send and the receive path are bad,
  296. * because they create unnecessary delays. An early implementation
  297. * using a spinlock showed a 5% degradation in throughput at some
  298. * loads.
  299. *
  300. * This implementation avoids spinlocks completely, putting both
  301. * counters into a single atomic, and updating that atomic using
  302. * atomic_add (in the receive path, when receiving fresh credits),
  303. * and using atomic_cmpxchg when updating the two counters.
  304. */
  305. int rds_iw_send_grab_credits(struct rds_iw_connection *ic,
  306. u32 wanted, u32 *adv_credits, int need_posted, int max_posted)
  307. {
  308. unsigned int avail, posted, got = 0, advertise;
  309. long oldval, newval;
  310. *adv_credits = 0;
  311. if (!ic->i_flowctl)
  312. return wanted;
  313. try_again:
  314. advertise = 0;
  315. oldval = newval = atomic_read(&ic->i_credits);
  316. posted = IB_GET_POST_CREDITS(oldval);
  317. avail = IB_GET_SEND_CREDITS(oldval);
  318. rdsdebug("wanted=%u credits=%u posted=%u\n",
  319. wanted, avail, posted);
  320. /* The last credit must be used to send a credit update. */
  321. if (avail && !posted)
  322. avail--;
  323. if (avail < wanted) {
  324. struct rds_connection *conn = ic->i_cm_id->context;
  325. /* Oops, there aren't that many credits left! */
  326. set_bit(RDS_LL_SEND_FULL, &conn->c_flags);
  327. got = avail;
  328. } else {
  329. /* Sometimes you get what you want, lalala. */
  330. got = wanted;
  331. }
  332. newval -= IB_SET_SEND_CREDITS(got);
  333. /*
  334. * If need_posted is non-zero, then the caller wants
  335. * the posted regardless of whether any send credits are
  336. * available.
  337. */
  338. if (posted && (got || need_posted)) {
  339. advertise = min_t(unsigned int, posted, max_posted);
  340. newval -= IB_SET_POST_CREDITS(advertise);
  341. }
  342. /* Finally bill everything */
  343. if (atomic_cmpxchg(&ic->i_credits, oldval, newval) != oldval)
  344. goto try_again;
  345. *adv_credits = advertise;
  346. return got;
  347. }
  348. void rds_iw_send_add_credits(struct rds_connection *conn, unsigned int credits)
  349. {
  350. struct rds_iw_connection *ic = conn->c_transport_data;
  351. if (credits == 0)
  352. return;
  353. rdsdebug("credits=%u current=%u%s\n",
  354. credits,
  355. IB_GET_SEND_CREDITS(atomic_read(&ic->i_credits)),
  356. test_bit(RDS_LL_SEND_FULL, &conn->c_flags) ? ", ll_send_full" : "");
  357. atomic_add(IB_SET_SEND_CREDITS(credits), &ic->i_credits);
  358. if (test_and_clear_bit(RDS_LL_SEND_FULL, &conn->c_flags))
  359. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  360. WARN_ON(IB_GET_SEND_CREDITS(credits) >= 16384);
  361. rds_iw_stats_inc(s_iw_rx_credit_updates);
  362. }
  363. void rds_iw_advertise_credits(struct rds_connection *conn, unsigned int posted)
  364. {
  365. struct rds_iw_connection *ic = conn->c_transport_data;
  366. if (posted == 0)
  367. return;
  368. atomic_add(IB_SET_POST_CREDITS(posted), &ic->i_credits);
  369. /* Decide whether to send an update to the peer now.
  370. * If we would send a credit update for every single buffer we
  371. * post, we would end up with an ACK storm (ACK arrives,
  372. * consumes buffer, we refill the ring, send ACK to remote
  373. * advertising the newly posted buffer... ad inf)
  374. *
  375. * Performance pretty much depends on how often we send
  376. * credit updates - too frequent updates mean lots of ACKs.
  377. * Too infrequent updates, and the peer will run out of
  378. * credits and has to throttle.
  379. * For the time being, 16 seems to be a good compromise.
  380. */
  381. if (IB_GET_POST_CREDITS(atomic_read(&ic->i_credits)) >= 16)
  382. set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  383. }
  384. static inline void
  385. rds_iw_xmit_populate_wr(struct rds_iw_connection *ic,
  386. struct rds_iw_send_work *send, unsigned int pos,
  387. unsigned long buffer, unsigned int length,
  388. int send_flags)
  389. {
  390. struct ib_sge *sge;
  391. WARN_ON(pos != send - ic->i_sends);
  392. send->s_wr.send_flags = send_flags;
  393. send->s_wr.opcode = IB_WR_SEND;
  394. send->s_wr.num_sge = 2;
  395. send->s_wr.next = NULL;
  396. send->s_queued = jiffies;
  397. send->s_op = NULL;
  398. if (length != 0) {
  399. sge = rds_iw_data_sge(ic, send->s_sge);
  400. sge->addr = buffer;
  401. sge->length = length;
  402. sge->lkey = rds_iw_local_dma_lkey(ic);
  403. sge = rds_iw_header_sge(ic, send->s_sge);
  404. } else {
  405. /* We're sending a packet with no payload. There is only
  406. * one SGE */
  407. send->s_wr.num_sge = 1;
  408. sge = &send->s_sge[0];
  409. }
  410. sge->addr = ic->i_send_hdrs_dma + (pos * sizeof(struct rds_header));
  411. sge->length = sizeof(struct rds_header);
  412. sge->lkey = rds_iw_local_dma_lkey(ic);
  413. }
  414. /*
  415. * This can be called multiple times for a given message. The first time
  416. * we see a message we map its scatterlist into the IB device so that
  417. * we can provide that mapped address to the IB scatter gather entries
  418. * in the IB work requests. We translate the scatterlist into a series
  419. * of work requests that fragment the message. These work requests complete
  420. * in order so we pass ownership of the message to the completion handler
  421. * once we send the final fragment.
  422. *
  423. * The RDS core uses the c_send_lock to only enter this function once
  424. * per connection. This makes sure that the tx ring alloc/unalloc pairs
  425. * don't get out of sync and confuse the ring.
  426. */
  427. int rds_iw_xmit(struct rds_connection *conn, struct rds_message *rm,
  428. unsigned int hdr_off, unsigned int sg, unsigned int off)
  429. {
  430. struct rds_iw_connection *ic = conn->c_transport_data;
  431. struct ib_device *dev = ic->i_cm_id->device;
  432. struct rds_iw_send_work *send = NULL;
  433. struct rds_iw_send_work *first;
  434. struct rds_iw_send_work *prev;
  435. struct ib_send_wr *failed_wr;
  436. struct scatterlist *scat;
  437. u32 pos;
  438. u32 i;
  439. u32 work_alloc;
  440. u32 credit_alloc;
  441. u32 posted;
  442. u32 adv_credits = 0;
  443. int send_flags = 0;
  444. int sent;
  445. int ret;
  446. int flow_controlled = 0;
  447. BUG_ON(off % RDS_FRAG_SIZE);
  448. BUG_ON(hdr_off != 0 && hdr_off != sizeof(struct rds_header));
  449. /* Fastreg support */
  450. if (rds_rdma_cookie_key(rm->m_rdma_cookie) && !ic->i_fastreg_posted) {
  451. ret = -EAGAIN;
  452. goto out;
  453. }
  454. /* FIXME we may overallocate here */
  455. if (be32_to_cpu(rm->m_inc.i_hdr.h_len) == 0)
  456. i = 1;
  457. else
  458. i = ceil(be32_to_cpu(rm->m_inc.i_hdr.h_len), RDS_FRAG_SIZE);
  459. work_alloc = rds_iw_ring_alloc(&ic->i_send_ring, i, &pos);
  460. if (work_alloc == 0) {
  461. set_bit(RDS_LL_SEND_FULL, &conn->c_flags);
  462. rds_iw_stats_inc(s_iw_tx_ring_full);
  463. ret = -ENOMEM;
  464. goto out;
  465. }
  466. credit_alloc = work_alloc;
  467. if (ic->i_flowctl) {
  468. credit_alloc = rds_iw_send_grab_credits(ic, work_alloc, &posted, 0, RDS_MAX_ADV_CREDIT);
  469. adv_credits += posted;
  470. if (credit_alloc < work_alloc) {
  471. rds_iw_ring_unalloc(&ic->i_send_ring, work_alloc - credit_alloc);
  472. work_alloc = credit_alloc;
  473. flow_controlled++;
  474. }
  475. if (work_alloc == 0) {
  476. set_bit(RDS_LL_SEND_FULL, &conn->c_flags);
  477. rds_iw_stats_inc(s_iw_tx_throttle);
  478. ret = -ENOMEM;
  479. goto out;
  480. }
  481. }
  482. /* map the message the first time we see it */
  483. if (!ic->i_rm) {
  484. /*
  485. printk(KERN_NOTICE "rds_iw_xmit prep msg dport=%u flags=0x%x len=%d\n",
  486. be16_to_cpu(rm->m_inc.i_hdr.h_dport),
  487. rm->m_inc.i_hdr.h_flags,
  488. be32_to_cpu(rm->m_inc.i_hdr.h_len));
  489. */
  490. if (rm->data.op_nents) {
  491. rm->data.op_count = ib_dma_map_sg(dev,
  492. rm->data.op_sg,
  493. rm->data.op_nents,
  494. DMA_TO_DEVICE);
  495. rdsdebug("ic %p mapping rm %p: %d\n", ic, rm, rm->data.op_count);
  496. if (rm->data.op_count == 0) {
  497. rds_iw_stats_inc(s_iw_tx_sg_mapping_failure);
  498. rds_iw_ring_unalloc(&ic->i_send_ring, work_alloc);
  499. ret = -ENOMEM; /* XXX ? */
  500. goto out;
  501. }
  502. } else {
  503. rm->data.op_count = 0;
  504. }
  505. ic->i_unsignaled_wrs = rds_iw_sysctl_max_unsig_wrs;
  506. ic->i_unsignaled_bytes = rds_iw_sysctl_max_unsig_bytes;
  507. rds_message_addref(rm);
  508. rm->data.op_dmasg = 0;
  509. rm->data.op_dmaoff = 0;
  510. ic->i_rm = rm;
  511. /* Finalize the header */
  512. if (test_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags))
  513. rm->m_inc.i_hdr.h_flags |= RDS_FLAG_ACK_REQUIRED;
  514. if (test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))
  515. rm->m_inc.i_hdr.h_flags |= RDS_FLAG_RETRANSMITTED;
  516. /* If it has a RDMA op, tell the peer we did it. This is
  517. * used by the peer to release use-once RDMA MRs. */
  518. if (rm->rdma.op_active) {
  519. struct rds_ext_header_rdma ext_hdr;
  520. ext_hdr.h_rdma_rkey = cpu_to_be32(rm->rdma.op_rkey);
  521. rds_message_add_extension(&rm->m_inc.i_hdr,
  522. RDS_EXTHDR_RDMA, &ext_hdr, sizeof(ext_hdr));
  523. }
  524. if (rm->m_rdma_cookie) {
  525. rds_message_add_rdma_dest_extension(&rm->m_inc.i_hdr,
  526. rds_rdma_cookie_key(rm->m_rdma_cookie),
  527. rds_rdma_cookie_offset(rm->m_rdma_cookie));
  528. }
  529. /* Note - rds_iw_piggyb_ack clears the ACK_REQUIRED bit, so
  530. * we should not do this unless we have a chance of at least
  531. * sticking the header into the send ring. Which is why we
  532. * should call rds_iw_ring_alloc first. */
  533. rm->m_inc.i_hdr.h_ack = cpu_to_be64(rds_iw_piggyb_ack(ic));
  534. rds_message_make_checksum(&rm->m_inc.i_hdr);
  535. /*
  536. * Update adv_credits since we reset the ACK_REQUIRED bit.
  537. */
  538. rds_iw_send_grab_credits(ic, 0, &posted, 1, RDS_MAX_ADV_CREDIT - adv_credits);
  539. adv_credits += posted;
  540. BUG_ON(adv_credits > 255);
  541. }
  542. send = &ic->i_sends[pos];
  543. first = send;
  544. prev = NULL;
  545. scat = &rm->data.op_sg[rm->data.op_dmasg];
  546. sent = 0;
  547. i = 0;
  548. /* Sometimes you want to put a fence between an RDMA
  549. * READ and the following SEND.
  550. * We could either do this all the time
  551. * or when requested by the user. Right now, we let
  552. * the application choose.
  553. */
  554. if (rm->rdma.op_active && rm->rdma.op_fence)
  555. send_flags = IB_SEND_FENCE;
  556. /*
  557. * We could be copying the header into the unused tail of the page.
  558. * That would need to be changed in the future when those pages might
  559. * be mapped userspace pages or page cache pages. So instead we always
  560. * use a second sge and our long-lived ring of mapped headers. We send
  561. * the header after the data so that the data payload can be aligned on
  562. * the receiver.
  563. */
  564. /* handle a 0-len message */
  565. if (be32_to_cpu(rm->m_inc.i_hdr.h_len) == 0) {
  566. rds_iw_xmit_populate_wr(ic, send, pos, 0, 0, send_flags);
  567. goto add_header;
  568. }
  569. /* if there's data reference it with a chain of work reqs */
  570. for (; i < work_alloc && scat != &rm->data.op_sg[rm->data.op_count]; i++) {
  571. unsigned int len;
  572. send = &ic->i_sends[pos];
  573. len = min(RDS_FRAG_SIZE,
  574. ib_sg_dma_len(dev, scat) - rm->data.op_dmaoff);
  575. rds_iw_xmit_populate_wr(ic, send, pos,
  576. ib_sg_dma_address(dev, scat) + rm->data.op_dmaoff, len,
  577. send_flags);
  578. /*
  579. * We want to delay signaling completions just enough to get
  580. * the batching benefits but not so much that we create dead time
  581. * on the wire.
  582. */
  583. if (ic->i_unsignaled_wrs-- == 0) {
  584. ic->i_unsignaled_wrs = rds_iw_sysctl_max_unsig_wrs;
  585. send->s_wr.send_flags |= IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  586. }
  587. ic->i_unsignaled_bytes -= len;
  588. if (ic->i_unsignaled_bytes <= 0) {
  589. ic->i_unsignaled_bytes = rds_iw_sysctl_max_unsig_bytes;
  590. send->s_wr.send_flags |= IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  591. }
  592. /*
  593. * Always signal the last one if we're stopping due to flow control.
  594. */
  595. if (flow_controlled && i == (work_alloc-1))
  596. send->s_wr.send_flags |= IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  597. rdsdebug("send %p wr %p num_sge %u next %p\n", send,
  598. &send->s_wr, send->s_wr.num_sge, send->s_wr.next);
  599. sent += len;
  600. rm->data.op_dmaoff += len;
  601. if (rm->data.op_dmaoff == ib_sg_dma_len(dev, scat)) {
  602. scat++;
  603. rm->data.op_dmaoff = 0;
  604. rm->data.op_dmasg++;
  605. }
  606. add_header:
  607. /* Tack on the header after the data. The header SGE should already
  608. * have been set up to point to the right header buffer. */
  609. memcpy(&ic->i_send_hdrs[pos], &rm->m_inc.i_hdr, sizeof(struct rds_header));
  610. if (0) {
  611. struct rds_header *hdr = &ic->i_send_hdrs[pos];
  612. printk(KERN_NOTICE "send WR dport=%u flags=0x%x len=%d\n",
  613. be16_to_cpu(hdr->h_dport),
  614. hdr->h_flags,
  615. be32_to_cpu(hdr->h_len));
  616. }
  617. if (adv_credits) {
  618. struct rds_header *hdr = &ic->i_send_hdrs[pos];
  619. /* add credit and redo the header checksum */
  620. hdr->h_credit = adv_credits;
  621. rds_message_make_checksum(hdr);
  622. adv_credits = 0;
  623. rds_iw_stats_inc(s_iw_tx_credit_updates);
  624. }
  625. if (prev)
  626. prev->s_wr.next = &send->s_wr;
  627. prev = send;
  628. pos = (pos + 1) % ic->i_send_ring.w_nr;
  629. }
  630. /* Account the RDS header in the number of bytes we sent, but just once.
  631. * The caller has no concept of fragmentation. */
  632. if (hdr_off == 0)
  633. sent += sizeof(struct rds_header);
  634. /* if we finished the message then send completion owns it */
  635. if (scat == &rm->data.op_sg[rm->data.op_count]) {
  636. prev->s_rm = ic->i_rm;
  637. prev->s_wr.send_flags |= IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  638. ic->i_rm = NULL;
  639. }
  640. if (i < work_alloc) {
  641. rds_iw_ring_unalloc(&ic->i_send_ring, work_alloc - i);
  642. work_alloc = i;
  643. }
  644. if (ic->i_flowctl && i < credit_alloc)
  645. rds_iw_send_add_credits(conn, credit_alloc - i);
  646. /* XXX need to worry about failed_wr and partial sends. */
  647. failed_wr = &first->s_wr;
  648. ret = ib_post_send(ic->i_cm_id->qp, &first->s_wr, &failed_wr);
  649. rdsdebug("ic %p first %p (wr %p) ret %d wr %p\n", ic,
  650. first, &first->s_wr, ret, failed_wr);
  651. BUG_ON(failed_wr != &first->s_wr);
  652. if (ret) {
  653. printk(KERN_WARNING "RDS/IW: ib_post_send to %pI4 "
  654. "returned %d\n", &conn->c_faddr, ret);
  655. rds_iw_ring_unalloc(&ic->i_send_ring, work_alloc);
  656. if (prev->s_rm) {
  657. ic->i_rm = prev->s_rm;
  658. prev->s_rm = NULL;
  659. }
  660. goto out;
  661. }
  662. ret = sent;
  663. out:
  664. BUG_ON(adv_credits);
  665. return ret;
  666. }
  667. static void rds_iw_build_send_fastreg(struct rds_iw_device *rds_iwdev, struct rds_iw_connection *ic, struct rds_iw_send_work *send, int nent, int len, u64 sg_addr)
  668. {
  669. BUG_ON(nent > send->s_page_list->max_page_list_len);
  670. /*
  671. * Perform a WR for the fast_reg_mr. Each individual page
  672. * in the sg list is added to the fast reg page list and placed
  673. * inside the fast_reg_mr WR.
  674. */
  675. send->s_wr.opcode = IB_WR_FAST_REG_MR;
  676. send->s_wr.wr.fast_reg.length = len;
  677. send->s_wr.wr.fast_reg.rkey = send->s_mr->rkey;
  678. send->s_wr.wr.fast_reg.page_list = send->s_page_list;
  679. send->s_wr.wr.fast_reg.page_list_len = nent;
  680. send->s_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
  681. send->s_wr.wr.fast_reg.access_flags = IB_ACCESS_REMOTE_WRITE;
  682. send->s_wr.wr.fast_reg.iova_start = sg_addr;
  683. ib_update_fast_reg_key(send->s_mr, send->s_remap_count++);
  684. }
  685. int rds_iw_xmit_rdma(struct rds_connection *conn, struct rm_rdma_op *op)
  686. {
  687. struct rds_iw_connection *ic = conn->c_transport_data;
  688. struct rds_iw_send_work *send = NULL;
  689. struct rds_iw_send_work *first;
  690. struct rds_iw_send_work *prev;
  691. struct ib_send_wr *failed_wr;
  692. struct rds_iw_device *rds_iwdev;
  693. struct scatterlist *scat;
  694. unsigned long len;
  695. u64 remote_addr = op->op_remote_addr;
  696. u32 pos, fr_pos;
  697. u32 work_alloc;
  698. u32 i;
  699. u32 j;
  700. int sent;
  701. int ret;
  702. int num_sge;
  703. rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client);
  704. /* map the message the first time we see it */
  705. if (!op->op_mapped) {
  706. op->op_count = ib_dma_map_sg(ic->i_cm_id->device,
  707. op->op_sg, op->op_nents, (op->op_write) ?
  708. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  709. rdsdebug("ic %p mapping op %p: %d\n", ic, op, op->op_count);
  710. if (op->op_count == 0) {
  711. rds_iw_stats_inc(s_iw_tx_sg_mapping_failure);
  712. ret = -ENOMEM; /* XXX ? */
  713. goto out;
  714. }
  715. op->op_mapped = 1;
  716. }
  717. if (!op->op_write) {
  718. /* Alloc space on the send queue for the fastreg */
  719. work_alloc = rds_iw_ring_alloc(&ic->i_send_ring, 1, &fr_pos);
  720. if (work_alloc != 1) {
  721. rds_iw_ring_unalloc(&ic->i_send_ring, work_alloc);
  722. rds_iw_stats_inc(s_iw_tx_ring_full);
  723. ret = -ENOMEM;
  724. goto out;
  725. }
  726. }
  727. /*
  728. * Instead of knowing how to return a partial rdma read/write we insist that there
  729. * be enough work requests to send the entire message.
  730. */
  731. i = ceil(op->op_count, rds_iwdev->max_sge);
  732. work_alloc = rds_iw_ring_alloc(&ic->i_send_ring, i, &pos);
  733. if (work_alloc != i) {
  734. rds_iw_ring_unalloc(&ic->i_send_ring, work_alloc);
  735. rds_iw_stats_inc(s_iw_tx_ring_full);
  736. ret = -ENOMEM;
  737. goto out;
  738. }
  739. send = &ic->i_sends[pos];
  740. if (!op->op_write) {
  741. first = prev = &ic->i_sends[fr_pos];
  742. } else {
  743. first = send;
  744. prev = NULL;
  745. }
  746. scat = &op->op_sg[0];
  747. sent = 0;
  748. num_sge = op->op_count;
  749. for (i = 0; i < work_alloc && scat != &op->op_sg[op->op_count]; i++) {
  750. send->s_wr.send_flags = 0;
  751. send->s_queued = jiffies;
  752. /*
  753. * We want to delay signaling completions just enough to get
  754. * the batching benefits but not so much that we create dead time on the wire.
  755. */
  756. if (ic->i_unsignaled_wrs-- == 0) {
  757. ic->i_unsignaled_wrs = rds_iw_sysctl_max_unsig_wrs;
  758. send->s_wr.send_flags = IB_SEND_SIGNALED;
  759. }
  760. /* To avoid the need to have the plumbing to invalidate the fastreg_mr used
  761. * for local access after RDS is finished with it, using
  762. * IB_WR_RDMA_READ_WITH_INV will invalidate it after the read has completed.
  763. */
  764. if (op->op_write)
  765. send->s_wr.opcode = IB_WR_RDMA_WRITE;
  766. else
  767. send->s_wr.opcode = IB_WR_RDMA_READ_WITH_INV;
  768. send->s_wr.wr.rdma.remote_addr = remote_addr;
  769. send->s_wr.wr.rdma.rkey = op->op_rkey;
  770. send->s_op = op;
  771. if (num_sge > rds_iwdev->max_sge) {
  772. send->s_wr.num_sge = rds_iwdev->max_sge;
  773. num_sge -= rds_iwdev->max_sge;
  774. } else
  775. send->s_wr.num_sge = num_sge;
  776. send->s_wr.next = NULL;
  777. if (prev)
  778. prev->s_wr.next = &send->s_wr;
  779. for (j = 0; j < send->s_wr.num_sge && scat != &op->op_sg[op->op_count]; j++) {
  780. len = ib_sg_dma_len(ic->i_cm_id->device, scat);
  781. if (send->s_wr.opcode == IB_WR_RDMA_READ_WITH_INV)
  782. send->s_page_list->page_list[j] = ib_sg_dma_address(ic->i_cm_id->device, scat);
  783. else {
  784. send->s_sge[j].addr = ib_sg_dma_address(ic->i_cm_id->device, scat);
  785. send->s_sge[j].length = len;
  786. send->s_sge[j].lkey = rds_iw_local_dma_lkey(ic);
  787. }
  788. sent += len;
  789. rdsdebug("ic %p sent %d remote_addr %llu\n", ic, sent, remote_addr);
  790. remote_addr += len;
  791. scat++;
  792. }
  793. if (send->s_wr.opcode == IB_WR_RDMA_READ_WITH_INV) {
  794. send->s_wr.num_sge = 1;
  795. send->s_sge[0].addr = conn->c_xmit_rm->m_rs->rs_user_addr;
  796. send->s_sge[0].length = conn->c_xmit_rm->m_rs->rs_user_bytes;
  797. send->s_sge[0].lkey = ic->i_sends[fr_pos].s_mr->lkey;
  798. }
  799. rdsdebug("send %p wr %p num_sge %u next %p\n", send,
  800. &send->s_wr, send->s_wr.num_sge, send->s_wr.next);
  801. prev = send;
  802. if (++send == &ic->i_sends[ic->i_send_ring.w_nr])
  803. send = ic->i_sends;
  804. }
  805. /* if we finished the message then send completion owns it */
  806. if (scat == &op->op_sg[op->op_count])
  807. first->s_wr.send_flags = IB_SEND_SIGNALED;
  808. if (i < work_alloc) {
  809. rds_iw_ring_unalloc(&ic->i_send_ring, work_alloc - i);
  810. work_alloc = i;
  811. }
  812. /* On iWARP, local memory access by a remote system (ie, RDMA Read) is not
  813. * recommended. Putting the lkey on the wire is a security hole, as it can
  814. * allow for memory access to all of memory on the remote system. Some
  815. * adapters do not allow using the lkey for this at all. To bypass this use a
  816. * fastreg_mr (or possibly a dma_mr)
  817. */
  818. if (!op->op_write) {
  819. rds_iw_build_send_fastreg(rds_iwdev, ic, &ic->i_sends[fr_pos],
  820. op->op_count, sent, conn->c_xmit_rm->m_rs->rs_user_addr);
  821. work_alloc++;
  822. }
  823. failed_wr = &first->s_wr;
  824. ret = ib_post_send(ic->i_cm_id->qp, &first->s_wr, &failed_wr);
  825. rdsdebug("ic %p first %p (wr %p) ret %d wr %p\n", ic,
  826. first, &first->s_wr, ret, failed_wr);
  827. BUG_ON(failed_wr != &first->s_wr);
  828. if (ret) {
  829. printk(KERN_WARNING "RDS/IW: rdma ib_post_send to %pI4 "
  830. "returned %d\n", &conn->c_faddr, ret);
  831. rds_iw_ring_unalloc(&ic->i_send_ring, work_alloc);
  832. goto out;
  833. }
  834. out:
  835. return ret;
  836. }
  837. void rds_iw_xmit_complete(struct rds_connection *conn)
  838. {
  839. struct rds_iw_connection *ic = conn->c_transport_data;
  840. /* We may have a pending ACK or window update we were unable
  841. * to send previously (due to flow control). Try again. */
  842. rds_iw_attempt_ack(ic);
  843. }