ib_recv.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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/slab.h>
  35. #include <linux/pci.h>
  36. #include <linux/dma-mapping.h>
  37. #include <rdma/rdma_cm.h>
  38. #include "rds.h"
  39. #include "ib.h"
  40. static struct kmem_cache *rds_ib_incoming_slab;
  41. static struct kmem_cache *rds_ib_frag_slab;
  42. static atomic_t rds_ib_allocation = ATOMIC_INIT(0);
  43. /* Free frag and attached recv buffer f_sg */
  44. static void rds_ib_frag_free(struct rds_page_frag *frag)
  45. {
  46. rdsdebug("frag %p page %p\n", frag, sg_page(&frag->f_sg));
  47. __free_page(sg_page(&frag->f_sg));
  48. kmem_cache_free(rds_ib_frag_slab, frag);
  49. }
  50. void rds_ib_recv_init_ring(struct rds_ib_connection *ic)
  51. {
  52. struct rds_ib_recv_work *recv;
  53. u32 i;
  54. for (i = 0, recv = ic->i_recvs; i < ic->i_recv_ring.w_nr; i++, recv++) {
  55. struct ib_sge *sge;
  56. recv->r_ibinc = NULL;
  57. recv->r_frag = NULL;
  58. recv->r_wr.next = NULL;
  59. recv->r_wr.wr_id = i;
  60. recv->r_wr.sg_list = recv->r_sge;
  61. recv->r_wr.num_sge = RDS_IB_RECV_SGE;
  62. sge = &recv->r_sge[0];
  63. sge->addr = ic->i_recv_hdrs_dma + (i * sizeof(struct rds_header));
  64. sge->length = sizeof(struct rds_header);
  65. sge->lkey = ic->i_mr->lkey;
  66. sge = &recv->r_sge[1];
  67. sge->addr = 0;
  68. sge->length = RDS_FRAG_SIZE;
  69. sge->lkey = ic->i_mr->lkey;
  70. }
  71. }
  72. static void rds_ib_recv_clear_one(struct rds_ib_connection *ic,
  73. struct rds_ib_recv_work *recv)
  74. {
  75. if (recv->r_ibinc) {
  76. rds_inc_put(&recv->r_ibinc->ii_inc);
  77. recv->r_ibinc = NULL;
  78. }
  79. if (recv->r_frag) {
  80. ib_dma_unmap_sg(ic->i_cm_id->device, &recv->r_frag->f_sg, 1, DMA_FROM_DEVICE);
  81. rds_ib_frag_free(recv->r_frag);
  82. recv->r_frag = NULL;
  83. }
  84. }
  85. void rds_ib_recv_clear_ring(struct rds_ib_connection *ic)
  86. {
  87. u32 i;
  88. for (i = 0; i < ic->i_recv_ring.w_nr; i++)
  89. rds_ib_recv_clear_one(ic, &ic->i_recvs[i]);
  90. }
  91. static int rds_ib_recv_refill_one(struct rds_connection *conn,
  92. struct rds_ib_recv_work *recv)
  93. {
  94. struct rds_ib_connection *ic = conn->c_transport_data;
  95. struct ib_sge *sge;
  96. int ret = -ENOMEM;
  97. /*
  98. * ibinc was taken from recv if recv contained the start of a message.
  99. * recvs that were continuations will still have this allocated.
  100. */
  101. if (!recv->r_ibinc) {
  102. if (!atomic_add_unless(&rds_ib_allocation, 1, rds_ib_sysctl_max_recv_allocation)) {
  103. rds_ib_stats_inc(s_ib_rx_alloc_limit);
  104. goto out;
  105. }
  106. recv->r_ibinc = kmem_cache_alloc(rds_ib_incoming_slab, GFP_NOWAIT);
  107. if (!recv->r_ibinc) {
  108. atomic_dec(&rds_ib_allocation);
  109. goto out;
  110. }
  111. INIT_LIST_HEAD(&recv->r_ibinc->ii_frags);
  112. rds_inc_init(&recv->r_ibinc->ii_inc, conn, conn->c_faddr);
  113. }
  114. WARN_ON(recv->r_frag); /* leak! */
  115. recv->r_frag = kmem_cache_alloc(rds_ib_frag_slab, GFP_NOWAIT);
  116. if (!recv->r_frag)
  117. goto out;
  118. INIT_LIST_HEAD(&recv->r_frag->f_item);
  119. sg_init_table(&recv->r_frag->f_sg, 1);
  120. ret = rds_page_remainder_alloc(&recv->r_frag->f_sg,
  121. RDS_FRAG_SIZE, GFP_NOWAIT);
  122. if (ret) {
  123. kmem_cache_free(rds_ib_frag_slab, recv->r_frag);
  124. recv->r_frag = NULL;
  125. goto out;
  126. }
  127. ret = ib_dma_map_sg(ic->i_cm_id->device, &recv->r_frag->f_sg,
  128. 1, DMA_FROM_DEVICE);
  129. WARN_ON(ret != 1);
  130. sge = &recv->r_sge[0];
  131. sge->addr = ic->i_recv_hdrs_dma + (recv - ic->i_recvs) * sizeof(struct rds_header);
  132. sge->length = sizeof(struct rds_header);
  133. sge = &recv->r_sge[1];
  134. sge->addr = sg_dma_address(&recv->r_frag->f_sg);
  135. sge->length = sg_dma_len(&recv->r_frag->f_sg);
  136. ret = 0;
  137. out:
  138. return ret;
  139. }
  140. /*
  141. * This tries to allocate and post unused work requests after making sure that
  142. * they have all the allocations they need to queue received fragments into
  143. * sockets. The i_recv_mutex is held here so that ring_alloc and _unalloc
  144. * pairs don't go unmatched.
  145. *
  146. * -1 is returned if posting fails due to temporary resource exhaustion.
  147. */
  148. int rds_ib_recv_refill(struct rds_connection *conn, int prefill)
  149. {
  150. struct rds_ib_connection *ic = conn->c_transport_data;
  151. struct rds_ib_recv_work *recv;
  152. struct ib_recv_wr *failed_wr;
  153. unsigned int posted = 0;
  154. int ret = 0;
  155. u32 pos;
  156. while ((prefill || rds_conn_up(conn)) &&
  157. rds_ib_ring_alloc(&ic->i_recv_ring, 1, &pos)) {
  158. if (pos >= ic->i_recv_ring.w_nr) {
  159. printk(KERN_NOTICE "Argh - ring alloc returned pos=%u\n",
  160. pos);
  161. ret = -EINVAL;
  162. break;
  163. }
  164. recv = &ic->i_recvs[pos];
  165. ret = rds_ib_recv_refill_one(conn, recv);
  166. if (ret) {
  167. ret = -1;
  168. break;
  169. }
  170. /* XXX when can this fail? */
  171. ret = ib_post_recv(ic->i_cm_id->qp, &recv->r_wr, &failed_wr);
  172. rdsdebug("recv %p ibinc %p page %p addr %lu ret %d\n", recv,
  173. recv->r_ibinc, sg_page(&recv->r_frag->f_sg),
  174. (long) sg_dma_address(&recv->r_frag->f_sg), ret);
  175. if (ret) {
  176. rds_ib_conn_error(conn, "recv post on "
  177. "%pI4 returned %d, disconnecting and "
  178. "reconnecting\n", &conn->c_faddr,
  179. ret);
  180. ret = -1;
  181. break;
  182. }
  183. posted++;
  184. }
  185. /* We're doing flow control - update the window. */
  186. if (ic->i_flowctl && posted)
  187. rds_ib_advertise_credits(conn, posted);
  188. if (ret)
  189. rds_ib_ring_unalloc(&ic->i_recv_ring, 1);
  190. return ret;
  191. }
  192. static void rds_ib_inc_purge(struct rds_incoming *inc)
  193. {
  194. struct rds_ib_incoming *ibinc;
  195. struct rds_page_frag *frag;
  196. struct rds_page_frag *pos;
  197. ibinc = container_of(inc, struct rds_ib_incoming, ii_inc);
  198. rdsdebug("purging ibinc %p inc %p\n", ibinc, inc);
  199. list_for_each_entry_safe(frag, pos, &ibinc->ii_frags, f_item) {
  200. list_del_init(&frag->f_item);
  201. rds_ib_frag_free(frag);
  202. }
  203. }
  204. void rds_ib_inc_free(struct rds_incoming *inc)
  205. {
  206. struct rds_ib_incoming *ibinc;
  207. ibinc = container_of(inc, struct rds_ib_incoming, ii_inc);
  208. rds_ib_inc_purge(inc);
  209. rdsdebug("freeing ibinc %p inc %p\n", ibinc, inc);
  210. BUG_ON(!list_empty(&ibinc->ii_frags));
  211. kmem_cache_free(rds_ib_incoming_slab, ibinc);
  212. atomic_dec(&rds_ib_allocation);
  213. BUG_ON(atomic_read(&rds_ib_allocation) < 0);
  214. }
  215. int rds_ib_inc_copy_to_user(struct rds_incoming *inc, struct iovec *first_iov,
  216. size_t size)
  217. {
  218. struct rds_ib_incoming *ibinc;
  219. struct rds_page_frag *frag;
  220. struct iovec *iov = first_iov;
  221. unsigned long to_copy;
  222. unsigned long frag_off = 0;
  223. unsigned long iov_off = 0;
  224. int copied = 0;
  225. int ret;
  226. u32 len;
  227. ibinc = container_of(inc, struct rds_ib_incoming, ii_inc);
  228. frag = list_entry(ibinc->ii_frags.next, struct rds_page_frag, f_item);
  229. len = be32_to_cpu(inc->i_hdr.h_len);
  230. while (copied < size && copied < len) {
  231. if (frag_off == RDS_FRAG_SIZE) {
  232. frag = list_entry(frag->f_item.next,
  233. struct rds_page_frag, f_item);
  234. frag_off = 0;
  235. }
  236. while (iov_off == iov->iov_len) {
  237. iov_off = 0;
  238. iov++;
  239. }
  240. to_copy = min(iov->iov_len - iov_off, RDS_FRAG_SIZE - frag_off);
  241. to_copy = min_t(size_t, to_copy, size - copied);
  242. to_copy = min_t(unsigned long, to_copy, len - copied);
  243. rdsdebug("%lu bytes to user [%p, %zu] + %lu from frag "
  244. "[%p, %u] + %lu\n",
  245. to_copy, iov->iov_base, iov->iov_len, iov_off,
  246. sg_page(&frag->f_sg), frag->f_sg.offset, frag_off);
  247. /* XXX needs + offset for multiple recvs per page */
  248. ret = rds_page_copy_to_user(sg_page(&frag->f_sg),
  249. frag->f_sg.offset + frag_off,
  250. iov->iov_base + iov_off,
  251. to_copy);
  252. if (ret) {
  253. copied = ret;
  254. break;
  255. }
  256. iov_off += to_copy;
  257. frag_off += to_copy;
  258. copied += to_copy;
  259. }
  260. return copied;
  261. }
  262. /* ic starts out kzalloc()ed */
  263. void rds_ib_recv_init_ack(struct rds_ib_connection *ic)
  264. {
  265. struct ib_send_wr *wr = &ic->i_ack_wr;
  266. struct ib_sge *sge = &ic->i_ack_sge;
  267. sge->addr = ic->i_ack_dma;
  268. sge->length = sizeof(struct rds_header);
  269. sge->lkey = ic->i_mr->lkey;
  270. wr->sg_list = sge;
  271. wr->num_sge = 1;
  272. wr->opcode = IB_WR_SEND;
  273. wr->wr_id = RDS_IB_ACK_WR_ID;
  274. wr->send_flags = IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  275. }
  276. /*
  277. * You'd think that with reliable IB connections you wouldn't need to ack
  278. * messages that have been received. The problem is that IB hardware generates
  279. * an ack message before it has DMAed the message into memory. This creates a
  280. * potential message loss if the HCA is disabled for any reason between when it
  281. * sends the ack and before the message is DMAed and processed. This is only a
  282. * potential issue if another HCA is available for fail-over.
  283. *
  284. * When the remote host receives our ack they'll free the sent message from
  285. * their send queue. To decrease the latency of this we always send an ack
  286. * immediately after we've received messages.
  287. *
  288. * For simplicity, we only have one ack in flight at a time. This puts
  289. * pressure on senders to have deep enough send queues to absorb the latency of
  290. * a single ack frame being in flight. This might not be good enough.
  291. *
  292. * This is implemented by have a long-lived send_wr and sge which point to a
  293. * statically allocated ack frame. This ack wr does not fall under the ring
  294. * accounting that the tx and rx wrs do. The QP attribute specifically makes
  295. * room for it beyond the ring size. Send completion notices its special
  296. * wr_id and avoids working with the ring in that case.
  297. */
  298. #ifndef KERNEL_HAS_ATOMIC64
  299. static void rds_ib_set_ack(struct rds_ib_connection *ic, u64 seq,
  300. int ack_required)
  301. {
  302. unsigned long flags;
  303. spin_lock_irqsave(&ic->i_ack_lock, flags);
  304. ic->i_ack_next = seq;
  305. if (ack_required)
  306. set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  307. spin_unlock_irqrestore(&ic->i_ack_lock, flags);
  308. }
  309. static u64 rds_ib_get_ack(struct rds_ib_connection *ic)
  310. {
  311. unsigned long flags;
  312. u64 seq;
  313. clear_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  314. spin_lock_irqsave(&ic->i_ack_lock, flags);
  315. seq = ic->i_ack_next;
  316. spin_unlock_irqrestore(&ic->i_ack_lock, flags);
  317. return seq;
  318. }
  319. #else
  320. static void rds_ib_set_ack(struct rds_ib_connection *ic, u64 seq,
  321. int ack_required)
  322. {
  323. atomic64_set(&ic->i_ack_next, seq);
  324. if (ack_required) {
  325. smp_mb__before_clear_bit();
  326. set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  327. }
  328. }
  329. static u64 rds_ib_get_ack(struct rds_ib_connection *ic)
  330. {
  331. clear_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  332. smp_mb__after_clear_bit();
  333. return atomic64_read(&ic->i_ack_next);
  334. }
  335. #endif
  336. static void rds_ib_send_ack(struct rds_ib_connection *ic, unsigned int adv_credits)
  337. {
  338. struct rds_header *hdr = ic->i_ack;
  339. struct ib_send_wr *failed_wr;
  340. u64 seq;
  341. int ret;
  342. seq = rds_ib_get_ack(ic);
  343. rdsdebug("send_ack: ic %p ack %llu\n", ic, (unsigned long long) seq);
  344. rds_message_populate_header(hdr, 0, 0, 0);
  345. hdr->h_ack = cpu_to_be64(seq);
  346. hdr->h_credit = adv_credits;
  347. rds_message_make_checksum(hdr);
  348. ic->i_ack_queued = jiffies;
  349. ret = ib_post_send(ic->i_cm_id->qp, &ic->i_ack_wr, &failed_wr);
  350. if (unlikely(ret)) {
  351. /* Failed to send. Release the WR, and
  352. * force another ACK.
  353. */
  354. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  355. set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  356. rds_ib_stats_inc(s_ib_ack_send_failure);
  357. rds_ib_conn_error(ic->conn, "sending ack failed\n");
  358. } else
  359. rds_ib_stats_inc(s_ib_ack_sent);
  360. }
  361. /*
  362. * There are 3 ways of getting acknowledgements to the peer:
  363. * 1. We call rds_ib_attempt_ack from the recv completion handler
  364. * to send an ACK-only frame.
  365. * However, there can be only one such frame in the send queue
  366. * at any time, so we may have to postpone it.
  367. * 2. When another (data) packet is transmitted while there's
  368. * an ACK in the queue, we piggyback the ACK sequence number
  369. * on the data packet.
  370. * 3. If the ACK WR is done sending, we get called from the
  371. * send queue completion handler, and check whether there's
  372. * another ACK pending (postponed because the WR was on the
  373. * queue). If so, we transmit it.
  374. *
  375. * We maintain 2 variables:
  376. * - i_ack_flags, which keeps track of whether the ACK WR
  377. * is currently in the send queue or not (IB_ACK_IN_FLIGHT)
  378. * - i_ack_next, which is the last sequence number we received
  379. *
  380. * Potentially, send queue and receive queue handlers can run concurrently.
  381. * It would be nice to not have to use a spinlock to synchronize things,
  382. * but the one problem that rules this out is that 64bit updates are
  383. * not atomic on all platforms. Things would be a lot simpler if
  384. * we had atomic64 or maybe cmpxchg64 everywhere.
  385. *
  386. * Reconnecting complicates this picture just slightly. When we
  387. * reconnect, we may be seeing duplicate packets. The peer
  388. * is retransmitting them, because it hasn't seen an ACK for
  389. * them. It is important that we ACK these.
  390. *
  391. * ACK mitigation adds a header flag "ACK_REQUIRED"; any packet with
  392. * this flag set *MUST* be acknowledged immediately.
  393. */
  394. /*
  395. * When we get here, we're called from the recv queue handler.
  396. * Check whether we ought to transmit an ACK.
  397. */
  398. void rds_ib_attempt_ack(struct rds_ib_connection *ic)
  399. {
  400. unsigned int adv_credits;
  401. if (!test_bit(IB_ACK_REQUESTED, &ic->i_ack_flags))
  402. return;
  403. if (test_and_set_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags)) {
  404. rds_ib_stats_inc(s_ib_ack_send_delayed);
  405. return;
  406. }
  407. /* Can we get a send credit? */
  408. if (!rds_ib_send_grab_credits(ic, 1, &adv_credits, 0, RDS_MAX_ADV_CREDIT)) {
  409. rds_ib_stats_inc(s_ib_tx_throttle);
  410. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  411. return;
  412. }
  413. clear_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  414. rds_ib_send_ack(ic, adv_credits);
  415. }
  416. /*
  417. * We get here from the send completion handler, when the
  418. * adapter tells us the ACK frame was sent.
  419. */
  420. void rds_ib_ack_send_complete(struct rds_ib_connection *ic)
  421. {
  422. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  423. rds_ib_attempt_ack(ic);
  424. }
  425. /*
  426. * This is called by the regular xmit code when it wants to piggyback
  427. * an ACK on an outgoing frame.
  428. */
  429. u64 rds_ib_piggyb_ack(struct rds_ib_connection *ic)
  430. {
  431. if (test_and_clear_bit(IB_ACK_REQUESTED, &ic->i_ack_flags))
  432. rds_ib_stats_inc(s_ib_ack_send_piggybacked);
  433. return rds_ib_get_ack(ic);
  434. }
  435. /*
  436. * It's kind of lame that we're copying from the posted receive pages into
  437. * long-lived bitmaps. We could have posted the bitmaps and rdma written into
  438. * them. But receiving new congestion bitmaps should be a *rare* event, so
  439. * hopefully we won't need to invest that complexity in making it more
  440. * efficient. By copying we can share a simpler core with TCP which has to
  441. * copy.
  442. */
  443. static void rds_ib_cong_recv(struct rds_connection *conn,
  444. struct rds_ib_incoming *ibinc)
  445. {
  446. struct rds_cong_map *map;
  447. unsigned int map_off;
  448. unsigned int map_page;
  449. struct rds_page_frag *frag;
  450. unsigned long frag_off;
  451. unsigned long to_copy;
  452. unsigned long copied;
  453. uint64_t uncongested = 0;
  454. void *addr;
  455. /* catch completely corrupt packets */
  456. if (be32_to_cpu(ibinc->ii_inc.i_hdr.h_len) != RDS_CONG_MAP_BYTES)
  457. return;
  458. map = conn->c_fcong;
  459. map_page = 0;
  460. map_off = 0;
  461. frag = list_entry(ibinc->ii_frags.next, struct rds_page_frag, f_item);
  462. frag_off = 0;
  463. copied = 0;
  464. while (copied < RDS_CONG_MAP_BYTES) {
  465. uint64_t *src, *dst;
  466. unsigned int k;
  467. to_copy = min(RDS_FRAG_SIZE - frag_off, PAGE_SIZE - map_off);
  468. BUG_ON(to_copy & 7); /* Must be 64bit aligned. */
  469. addr = kmap_atomic(sg_page(&frag->f_sg), KM_SOFTIRQ0);
  470. src = addr + frag_off;
  471. dst = (void *)map->m_page_addrs[map_page] + map_off;
  472. for (k = 0; k < to_copy; k += 8) {
  473. /* Record ports that became uncongested, ie
  474. * bits that changed from 0 to 1. */
  475. uncongested |= ~(*src) & *dst;
  476. *dst++ = *src++;
  477. }
  478. kunmap_atomic(addr, KM_SOFTIRQ0);
  479. copied += to_copy;
  480. map_off += to_copy;
  481. if (map_off == PAGE_SIZE) {
  482. map_off = 0;
  483. map_page++;
  484. }
  485. frag_off += to_copy;
  486. if (frag_off == RDS_FRAG_SIZE) {
  487. frag = list_entry(frag->f_item.next,
  488. struct rds_page_frag, f_item);
  489. frag_off = 0;
  490. }
  491. }
  492. /* the congestion map is in little endian order */
  493. uncongested = le64_to_cpu(uncongested);
  494. rds_cong_map_updated(map, uncongested);
  495. }
  496. /*
  497. * Rings are posted with all the allocations they'll need to queue the
  498. * incoming message to the receiving socket so this can't fail.
  499. * All fragments start with a header, so we can make sure we're not receiving
  500. * garbage, and we can tell a small 8 byte fragment from an ACK frame.
  501. */
  502. struct rds_ib_ack_state {
  503. u64 ack_next;
  504. u64 ack_recv;
  505. unsigned int ack_required:1;
  506. unsigned int ack_next_valid:1;
  507. unsigned int ack_recv_valid:1;
  508. };
  509. static void rds_ib_process_recv(struct rds_connection *conn,
  510. struct rds_ib_recv_work *recv, u32 data_len,
  511. struct rds_ib_ack_state *state)
  512. {
  513. struct rds_ib_connection *ic = conn->c_transport_data;
  514. struct rds_ib_incoming *ibinc = ic->i_ibinc;
  515. struct rds_header *ihdr, *hdr;
  516. /* XXX shut down the connection if port 0,0 are seen? */
  517. rdsdebug("ic %p ibinc %p recv %p byte len %u\n", ic, ibinc, recv,
  518. data_len);
  519. if (data_len < sizeof(struct rds_header)) {
  520. rds_ib_conn_error(conn, "incoming message "
  521. "from %pI4 didn't inclue a "
  522. "header, disconnecting and "
  523. "reconnecting\n",
  524. &conn->c_faddr);
  525. return;
  526. }
  527. data_len -= sizeof(struct rds_header);
  528. ihdr = &ic->i_recv_hdrs[recv - ic->i_recvs];
  529. /* Validate the checksum. */
  530. if (!rds_message_verify_checksum(ihdr)) {
  531. rds_ib_conn_error(conn, "incoming message "
  532. "from %pI4 has corrupted header - "
  533. "forcing a reconnect\n",
  534. &conn->c_faddr);
  535. rds_stats_inc(s_recv_drop_bad_checksum);
  536. return;
  537. }
  538. /* Process the ACK sequence which comes with every packet */
  539. state->ack_recv = be64_to_cpu(ihdr->h_ack);
  540. state->ack_recv_valid = 1;
  541. /* Process the credits update if there was one */
  542. if (ihdr->h_credit)
  543. rds_ib_send_add_credits(conn, ihdr->h_credit);
  544. if (ihdr->h_sport == 0 && ihdr->h_dport == 0 && data_len == 0) {
  545. /* This is an ACK-only packet. The fact that it gets
  546. * special treatment here is that historically, ACKs
  547. * were rather special beasts.
  548. */
  549. rds_ib_stats_inc(s_ib_ack_received);
  550. /*
  551. * Usually the frags make their way on to incs and are then freed as
  552. * the inc is freed. We don't go that route, so we have to drop the
  553. * page ref ourselves. We can't just leave the page on the recv
  554. * because that confuses the dma mapping of pages and each recv's use
  555. * of a partial page.
  556. *
  557. * FIXME: Fold this into the code path below.
  558. */
  559. rds_ib_frag_free(recv->r_frag);
  560. recv->r_frag = NULL;
  561. return;
  562. }
  563. /*
  564. * If we don't already have an inc on the connection then this
  565. * fragment has a header and starts a message.. copy its header
  566. * into the inc and save the inc so we can hang upcoming fragments
  567. * off its list.
  568. */
  569. if (!ibinc) {
  570. ibinc = recv->r_ibinc;
  571. recv->r_ibinc = NULL;
  572. ic->i_ibinc = ibinc;
  573. hdr = &ibinc->ii_inc.i_hdr;
  574. memcpy(hdr, ihdr, sizeof(*hdr));
  575. ic->i_recv_data_rem = be32_to_cpu(hdr->h_len);
  576. rdsdebug("ic %p ibinc %p rem %u flag 0x%x\n", ic, ibinc,
  577. ic->i_recv_data_rem, hdr->h_flags);
  578. } else {
  579. hdr = &ibinc->ii_inc.i_hdr;
  580. /* We can't just use memcmp here; fragments of a
  581. * single message may carry different ACKs */
  582. if (hdr->h_sequence != ihdr->h_sequence ||
  583. hdr->h_len != ihdr->h_len ||
  584. hdr->h_sport != ihdr->h_sport ||
  585. hdr->h_dport != ihdr->h_dport) {
  586. rds_ib_conn_error(conn,
  587. "fragment header mismatch; forcing reconnect\n");
  588. return;
  589. }
  590. }
  591. list_add_tail(&recv->r_frag->f_item, &ibinc->ii_frags);
  592. recv->r_frag = NULL;
  593. if (ic->i_recv_data_rem > RDS_FRAG_SIZE)
  594. ic->i_recv_data_rem -= RDS_FRAG_SIZE;
  595. else {
  596. ic->i_recv_data_rem = 0;
  597. ic->i_ibinc = NULL;
  598. if (ibinc->ii_inc.i_hdr.h_flags == RDS_FLAG_CONG_BITMAP)
  599. rds_ib_cong_recv(conn, ibinc);
  600. else {
  601. rds_recv_incoming(conn, conn->c_faddr, conn->c_laddr,
  602. &ibinc->ii_inc, GFP_ATOMIC,
  603. KM_SOFTIRQ0);
  604. state->ack_next = be64_to_cpu(hdr->h_sequence);
  605. state->ack_next_valid = 1;
  606. }
  607. /* Evaluate the ACK_REQUIRED flag *after* we received
  608. * the complete frame, and after bumping the next_rx
  609. * sequence. */
  610. if (hdr->h_flags & RDS_FLAG_ACK_REQUIRED) {
  611. rds_stats_inc(s_recv_ack_required);
  612. state->ack_required = 1;
  613. }
  614. rds_inc_put(&ibinc->ii_inc);
  615. }
  616. }
  617. /*
  618. * Plucking the oldest entry from the ring can be done concurrently with
  619. * the thread refilling the ring. Each ring operation is protected by
  620. * spinlocks and the transient state of refilling doesn't change the
  621. * recording of which entry is oldest.
  622. *
  623. * This relies on IB only calling one cq comp_handler for each cq so that
  624. * there will only be one caller of rds_recv_incoming() per RDS connection.
  625. */
  626. void rds_ib_recv_cq_comp_handler(struct ib_cq *cq, void *context)
  627. {
  628. struct rds_connection *conn = context;
  629. struct rds_ib_connection *ic = conn->c_transport_data;
  630. rdsdebug("conn %p cq %p\n", conn, cq);
  631. rds_ib_stats_inc(s_ib_rx_cq_call);
  632. tasklet_schedule(&ic->i_recv_tasklet);
  633. }
  634. static inline void rds_poll_cq(struct rds_ib_connection *ic,
  635. struct rds_ib_ack_state *state)
  636. {
  637. struct rds_connection *conn = ic->conn;
  638. struct ib_wc wc;
  639. struct rds_ib_recv_work *recv;
  640. while (ib_poll_cq(ic->i_recv_cq, 1, &wc) > 0) {
  641. rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n",
  642. (unsigned long long)wc.wr_id, wc.status, wc.byte_len,
  643. be32_to_cpu(wc.ex.imm_data));
  644. rds_ib_stats_inc(s_ib_rx_cq_event);
  645. recv = &ic->i_recvs[rds_ib_ring_oldest(&ic->i_recv_ring)];
  646. ib_dma_unmap_sg(ic->i_cm_id->device, &recv->r_frag->f_sg, 1, DMA_FROM_DEVICE);
  647. /*
  648. * Also process recvs in connecting state because it is possible
  649. * to get a recv completion _before_ the rdmacm ESTABLISHED
  650. * event is processed.
  651. */
  652. if (rds_conn_up(conn) || rds_conn_connecting(conn)) {
  653. /* We expect errors as the qp is drained during shutdown */
  654. if (wc.status == IB_WC_SUCCESS) {
  655. rds_ib_process_recv(conn, recv, wc.byte_len, state);
  656. } else {
  657. rds_ib_conn_error(conn, "recv completion on "
  658. "%pI4 had status %u, disconnecting and "
  659. "reconnecting\n", &conn->c_faddr,
  660. wc.status);
  661. }
  662. }
  663. rds_ib_ring_free(&ic->i_recv_ring, 1);
  664. }
  665. }
  666. void rds_ib_recv_tasklet_fn(unsigned long data)
  667. {
  668. struct rds_ib_connection *ic = (struct rds_ib_connection *) data;
  669. struct rds_connection *conn = ic->conn;
  670. struct rds_ib_ack_state state = { 0, };
  671. rds_poll_cq(ic, &state);
  672. ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED);
  673. rds_poll_cq(ic, &state);
  674. if (state.ack_next_valid)
  675. rds_ib_set_ack(ic, state.ack_next, state.ack_required);
  676. if (state.ack_recv_valid && state.ack_recv > ic->i_ack_recv) {
  677. rds_send_drop_acked(conn, state.ack_recv, NULL);
  678. ic->i_ack_recv = state.ack_recv;
  679. }
  680. if (rds_conn_up(conn))
  681. rds_ib_attempt_ack(ic);
  682. /* If we ever end up with a really empty receive ring, we're
  683. * in deep trouble, as the sender will definitely see RNR
  684. * timeouts. */
  685. if (rds_ib_ring_empty(&ic->i_recv_ring))
  686. rds_ib_stats_inc(s_ib_rx_ring_empty);
  687. if (rds_ib_ring_low(&ic->i_recv_ring))
  688. rds_ib_recv_refill(conn, 0);
  689. }
  690. int rds_ib_recv(struct rds_connection *conn)
  691. {
  692. struct rds_ib_connection *ic = conn->c_transport_data;
  693. int ret = 0;
  694. rdsdebug("conn %p\n", conn);
  695. if (rds_conn_up(conn))
  696. rds_ib_attempt_ack(ic);
  697. return ret;
  698. }
  699. int __init rds_ib_recv_init(void)
  700. {
  701. struct sysinfo si;
  702. int ret = -ENOMEM;
  703. /* Default to 30% of all available RAM for recv memory */
  704. si_meminfo(&si);
  705. rds_ib_sysctl_max_recv_allocation = si.totalram / 3 * PAGE_SIZE / RDS_FRAG_SIZE;
  706. rds_ib_incoming_slab = kmem_cache_create("rds_ib_incoming",
  707. sizeof(struct rds_ib_incoming),
  708. 0, 0, NULL);
  709. if (!rds_ib_incoming_slab)
  710. goto out;
  711. rds_ib_frag_slab = kmem_cache_create("rds_ib_frag",
  712. sizeof(struct rds_page_frag),
  713. 0, 0, NULL);
  714. if (!rds_ib_frag_slab)
  715. kmem_cache_destroy(rds_ib_incoming_slab);
  716. else
  717. ret = 0;
  718. out:
  719. return ret;
  720. }
  721. void rds_ib_recv_exit(void)
  722. {
  723. kmem_cache_destroy(rds_ib_incoming_slab);
  724. kmem_cache_destroy(rds_ib_frag_slab);
  725. }