iw_recv.c 26 KB

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