ib_recv.c 29 KB

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