ib_recv.c 25 KB

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