rc.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/io.h>
  48. #include <rdma/rdma_vt.h>
  49. #include <rdma/rdmavt_qp.h>
  50. #include "hfi.h"
  51. #include "qp.h"
  52. #include "verbs_txreq.h"
  53. #include "trace.h"
  54. /* cut down ridiculously long IB macro names */
  55. #define OP(x) RC_OP(x)
  56. static u32 restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe,
  57. u32 psn, u32 pmtu)
  58. {
  59. u32 len;
  60. len = delta_psn(psn, wqe->psn) * pmtu;
  61. ss->sge = wqe->sg_list[0];
  62. ss->sg_list = wqe->sg_list + 1;
  63. ss->num_sge = wqe->wr.num_sge;
  64. ss->total_len = wqe->length;
  65. rvt_skip_sge(ss, len, false);
  66. return wqe->length - len;
  67. }
  68. /**
  69. * make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
  70. * @dev: the device for this QP
  71. * @qp: a pointer to the QP
  72. * @ohdr: a pointer to the IB header being constructed
  73. * @ps: the xmit packet state
  74. *
  75. * Return 1 if constructed; otherwise, return 0.
  76. * Note that we are in the responder's side of the QP context.
  77. * Note the QP s_lock must be held.
  78. */
  79. static int make_rc_ack(struct hfi1_ibdev *dev, struct rvt_qp *qp,
  80. struct ib_other_headers *ohdr,
  81. struct hfi1_pkt_state *ps)
  82. {
  83. struct rvt_ack_entry *e;
  84. u32 hwords;
  85. u32 len;
  86. u32 bth0;
  87. u32 bth2;
  88. int middle = 0;
  89. u32 pmtu = qp->pmtu;
  90. struct hfi1_qp_priv *priv = qp->priv;
  91. lockdep_assert_held(&qp->s_lock);
  92. /* Don't send an ACK if we aren't supposed to. */
  93. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
  94. goto bail;
  95. /* header size in 32-bit words LRH+BTH = (8+12)/4. */
  96. hwords = 5;
  97. switch (qp->s_ack_state) {
  98. case OP(RDMA_READ_RESPONSE_LAST):
  99. case OP(RDMA_READ_RESPONSE_ONLY):
  100. e = &qp->s_ack_queue[qp->s_tail_ack_queue];
  101. if (e->rdma_sge.mr) {
  102. rvt_put_mr(e->rdma_sge.mr);
  103. e->rdma_sge.mr = NULL;
  104. }
  105. /* FALLTHROUGH */
  106. case OP(ATOMIC_ACKNOWLEDGE):
  107. /*
  108. * We can increment the tail pointer now that the last
  109. * response has been sent instead of only being
  110. * constructed.
  111. */
  112. if (++qp->s_tail_ack_queue > HFI1_MAX_RDMA_ATOMIC)
  113. qp->s_tail_ack_queue = 0;
  114. /* FALLTHROUGH */
  115. case OP(SEND_ONLY):
  116. case OP(ACKNOWLEDGE):
  117. /* Check for no next entry in the queue. */
  118. if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
  119. if (qp->s_flags & RVT_S_ACK_PENDING)
  120. goto normal;
  121. goto bail;
  122. }
  123. e = &qp->s_ack_queue[qp->s_tail_ack_queue];
  124. if (e->opcode == OP(RDMA_READ_REQUEST)) {
  125. /*
  126. * If a RDMA read response is being resent and
  127. * we haven't seen the duplicate request yet,
  128. * then stop sending the remaining responses the
  129. * responder has seen until the requester re-sends it.
  130. */
  131. len = e->rdma_sge.sge_length;
  132. if (len && !e->rdma_sge.mr) {
  133. qp->s_tail_ack_queue = qp->r_head_ack_queue;
  134. goto bail;
  135. }
  136. /* Copy SGE state in case we need to resend */
  137. ps->s_txreq->mr = e->rdma_sge.mr;
  138. if (ps->s_txreq->mr)
  139. rvt_get_mr(ps->s_txreq->mr);
  140. qp->s_ack_rdma_sge.sge = e->rdma_sge;
  141. qp->s_ack_rdma_sge.num_sge = 1;
  142. ps->s_txreq->ss = &qp->s_ack_rdma_sge;
  143. if (len > pmtu) {
  144. len = pmtu;
  145. qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
  146. } else {
  147. qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
  148. e->sent = 1;
  149. }
  150. ohdr->u.aeth = rvt_compute_aeth(qp);
  151. hwords++;
  152. qp->s_ack_rdma_psn = e->psn;
  153. bth2 = mask_psn(qp->s_ack_rdma_psn++);
  154. } else {
  155. /* COMPARE_SWAP or FETCH_ADD */
  156. ps->s_txreq->ss = NULL;
  157. len = 0;
  158. qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
  159. ohdr->u.at.aeth = rvt_compute_aeth(qp);
  160. ib_u64_put(e->atomic_data, &ohdr->u.at.atomic_ack_eth);
  161. hwords += sizeof(ohdr->u.at) / sizeof(u32);
  162. bth2 = mask_psn(e->psn);
  163. e->sent = 1;
  164. }
  165. bth0 = qp->s_ack_state << 24;
  166. break;
  167. case OP(RDMA_READ_RESPONSE_FIRST):
  168. qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
  169. /* FALLTHROUGH */
  170. case OP(RDMA_READ_RESPONSE_MIDDLE):
  171. ps->s_txreq->ss = &qp->s_ack_rdma_sge;
  172. ps->s_txreq->mr = qp->s_ack_rdma_sge.sge.mr;
  173. if (ps->s_txreq->mr)
  174. rvt_get_mr(ps->s_txreq->mr);
  175. len = qp->s_ack_rdma_sge.sge.sge_length;
  176. if (len > pmtu) {
  177. len = pmtu;
  178. middle = HFI1_CAP_IS_KSET(SDMA_AHG);
  179. } else {
  180. ohdr->u.aeth = rvt_compute_aeth(qp);
  181. hwords++;
  182. qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
  183. e = &qp->s_ack_queue[qp->s_tail_ack_queue];
  184. e->sent = 1;
  185. }
  186. bth0 = qp->s_ack_state << 24;
  187. bth2 = mask_psn(qp->s_ack_rdma_psn++);
  188. break;
  189. default:
  190. normal:
  191. /*
  192. * Send a regular ACK.
  193. * Set the s_ack_state so we wait until after sending
  194. * the ACK before setting s_ack_state to ACKNOWLEDGE
  195. * (see above).
  196. */
  197. qp->s_ack_state = OP(SEND_ONLY);
  198. qp->s_flags &= ~RVT_S_ACK_PENDING;
  199. ps->s_txreq->ss = NULL;
  200. if (qp->s_nak_state)
  201. ohdr->u.aeth =
  202. cpu_to_be32((qp->r_msn & IB_MSN_MASK) |
  203. (qp->s_nak_state <<
  204. IB_AETH_CREDIT_SHIFT));
  205. else
  206. ohdr->u.aeth = rvt_compute_aeth(qp);
  207. hwords++;
  208. len = 0;
  209. bth0 = OP(ACKNOWLEDGE) << 24;
  210. bth2 = mask_psn(qp->s_ack_psn);
  211. }
  212. qp->s_rdma_ack_cnt++;
  213. qp->s_hdrwords = hwords;
  214. ps->s_txreq->sde = priv->s_sde;
  215. ps->s_txreq->s_cur_size = len;
  216. hfi1_make_ruc_header(qp, ohdr, bth0, bth2, middle, ps);
  217. /* pbc */
  218. ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
  219. return 1;
  220. bail:
  221. qp->s_ack_state = OP(ACKNOWLEDGE);
  222. /*
  223. * Ensure s_rdma_ack_cnt changes are committed prior to resetting
  224. * RVT_S_RESP_PENDING
  225. */
  226. smp_wmb();
  227. qp->s_flags &= ~(RVT_S_RESP_PENDING
  228. | RVT_S_ACK_PENDING
  229. | RVT_S_AHG_VALID);
  230. return 0;
  231. }
  232. /**
  233. * hfi1_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
  234. * @qp: a pointer to the QP
  235. *
  236. * Assumes s_lock is held.
  237. *
  238. * Return 1 if constructed; otherwise, return 0.
  239. */
  240. int hfi1_make_rc_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
  241. {
  242. struct hfi1_qp_priv *priv = qp->priv;
  243. struct hfi1_ibdev *dev = to_idev(qp->ibqp.device);
  244. struct ib_other_headers *ohdr;
  245. struct rvt_sge_state *ss;
  246. struct rvt_swqe *wqe;
  247. /* header size in 32-bit words LRH+BTH = (8+12)/4. */
  248. u32 hwords = 5;
  249. u32 len;
  250. u32 bth0 = 0;
  251. u32 bth2;
  252. u32 pmtu = qp->pmtu;
  253. char newreq;
  254. int middle = 0;
  255. int delta;
  256. lockdep_assert_held(&qp->s_lock);
  257. ps->s_txreq = get_txreq(ps->dev, qp);
  258. if (IS_ERR(ps->s_txreq))
  259. goto bail_no_tx;
  260. ohdr = &ps->s_txreq->phdr.hdr.u.oth;
  261. if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)
  262. ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
  263. /* Sending responses has higher priority over sending requests. */
  264. if ((qp->s_flags & RVT_S_RESP_PENDING) &&
  265. make_rc_ack(dev, qp, ohdr, ps))
  266. return 1;
  267. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
  268. if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
  269. goto bail;
  270. /* We are in the error state, flush the work request. */
  271. smp_read_barrier_depends(); /* see post_one_send() */
  272. if (qp->s_last == READ_ONCE(qp->s_head))
  273. goto bail;
  274. /* If DMAs are in progress, we can't flush immediately. */
  275. if (iowait_sdma_pending(&priv->s_iowait)) {
  276. qp->s_flags |= RVT_S_WAIT_DMA;
  277. goto bail;
  278. }
  279. clear_ahg(qp);
  280. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  281. hfi1_send_complete(qp, wqe, qp->s_last != qp->s_acked ?
  282. IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR);
  283. /* will get called again */
  284. goto done_free_tx;
  285. }
  286. if (qp->s_flags & (RVT_S_WAIT_RNR | RVT_S_WAIT_ACK))
  287. goto bail;
  288. if (cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) {
  289. if (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0) {
  290. qp->s_flags |= RVT_S_WAIT_PSN;
  291. goto bail;
  292. }
  293. qp->s_sending_psn = qp->s_psn;
  294. qp->s_sending_hpsn = qp->s_psn - 1;
  295. }
  296. /* Send a request. */
  297. wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
  298. switch (qp->s_state) {
  299. default:
  300. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK))
  301. goto bail;
  302. /*
  303. * Resend an old request or start a new one.
  304. *
  305. * We keep track of the current SWQE so that
  306. * we don't reset the "furthest progress" state
  307. * if we need to back up.
  308. */
  309. newreq = 0;
  310. if (qp->s_cur == qp->s_tail) {
  311. /* Check if send work queue is empty. */
  312. smp_read_barrier_depends(); /* see post_one_send() */
  313. if (qp->s_tail == READ_ONCE(qp->s_head)) {
  314. clear_ahg(qp);
  315. goto bail;
  316. }
  317. /*
  318. * If a fence is requested, wait for previous
  319. * RDMA read and atomic operations to finish.
  320. */
  321. if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
  322. qp->s_num_rd_atomic) {
  323. qp->s_flags |= RVT_S_WAIT_FENCE;
  324. goto bail;
  325. }
  326. /*
  327. * Local operations are processed immediately
  328. * after all prior requests have completed
  329. */
  330. if (wqe->wr.opcode == IB_WR_REG_MR ||
  331. wqe->wr.opcode == IB_WR_LOCAL_INV) {
  332. int local_ops = 0;
  333. int err = 0;
  334. if (qp->s_last != qp->s_cur)
  335. goto bail;
  336. if (++qp->s_cur == qp->s_size)
  337. qp->s_cur = 0;
  338. if (++qp->s_tail == qp->s_size)
  339. qp->s_tail = 0;
  340. if (!(wqe->wr.send_flags &
  341. RVT_SEND_COMPLETION_ONLY)) {
  342. err = rvt_invalidate_rkey(
  343. qp,
  344. wqe->wr.ex.invalidate_rkey);
  345. local_ops = 1;
  346. }
  347. hfi1_send_complete(qp, wqe,
  348. err ? IB_WC_LOC_PROT_ERR
  349. : IB_WC_SUCCESS);
  350. if (local_ops)
  351. atomic_dec(&qp->local_ops_pending);
  352. qp->s_hdrwords = 0;
  353. goto done_free_tx;
  354. }
  355. newreq = 1;
  356. qp->s_psn = wqe->psn;
  357. }
  358. /*
  359. * Note that we have to be careful not to modify the
  360. * original work request since we may need to resend
  361. * it.
  362. */
  363. len = wqe->length;
  364. ss = &qp->s_sge;
  365. bth2 = mask_psn(qp->s_psn);
  366. switch (wqe->wr.opcode) {
  367. case IB_WR_SEND:
  368. case IB_WR_SEND_WITH_IMM:
  369. case IB_WR_SEND_WITH_INV:
  370. /* If no credit, return. */
  371. if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
  372. rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
  373. qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
  374. goto bail;
  375. }
  376. if (len > pmtu) {
  377. qp->s_state = OP(SEND_FIRST);
  378. len = pmtu;
  379. break;
  380. }
  381. if (wqe->wr.opcode == IB_WR_SEND) {
  382. qp->s_state = OP(SEND_ONLY);
  383. } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
  384. qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
  385. /* Immediate data comes after the BTH */
  386. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  387. hwords += 1;
  388. } else {
  389. qp->s_state = OP(SEND_ONLY_WITH_INVALIDATE);
  390. /* Invalidate rkey comes after the BTH */
  391. ohdr->u.ieth = cpu_to_be32(
  392. wqe->wr.ex.invalidate_rkey);
  393. hwords += 1;
  394. }
  395. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  396. bth0 |= IB_BTH_SOLICITED;
  397. bth2 |= IB_BTH_REQ_ACK;
  398. if (++qp->s_cur == qp->s_size)
  399. qp->s_cur = 0;
  400. break;
  401. case IB_WR_RDMA_WRITE:
  402. if (newreq && !(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
  403. qp->s_lsn++;
  404. /* FALLTHROUGH */
  405. case IB_WR_RDMA_WRITE_WITH_IMM:
  406. /* If no credit, return. */
  407. if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
  408. rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
  409. qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
  410. goto bail;
  411. }
  412. put_ib_reth_vaddr(
  413. wqe->rdma_wr.remote_addr,
  414. &ohdr->u.rc.reth);
  415. ohdr->u.rc.reth.rkey =
  416. cpu_to_be32(wqe->rdma_wr.rkey);
  417. ohdr->u.rc.reth.length = cpu_to_be32(len);
  418. hwords += sizeof(struct ib_reth) / sizeof(u32);
  419. if (len > pmtu) {
  420. qp->s_state = OP(RDMA_WRITE_FIRST);
  421. len = pmtu;
  422. break;
  423. }
  424. if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
  425. qp->s_state = OP(RDMA_WRITE_ONLY);
  426. } else {
  427. qp->s_state =
  428. OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
  429. /* Immediate data comes after RETH */
  430. ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
  431. hwords += 1;
  432. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  433. bth0 |= IB_BTH_SOLICITED;
  434. }
  435. bth2 |= IB_BTH_REQ_ACK;
  436. if (++qp->s_cur == qp->s_size)
  437. qp->s_cur = 0;
  438. break;
  439. case IB_WR_RDMA_READ:
  440. /*
  441. * Don't allow more operations to be started
  442. * than the QP limits allow.
  443. */
  444. if (newreq) {
  445. if (qp->s_num_rd_atomic >=
  446. qp->s_max_rd_atomic) {
  447. qp->s_flags |= RVT_S_WAIT_RDMAR;
  448. goto bail;
  449. }
  450. qp->s_num_rd_atomic++;
  451. if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
  452. qp->s_lsn++;
  453. }
  454. put_ib_reth_vaddr(
  455. wqe->rdma_wr.remote_addr,
  456. &ohdr->u.rc.reth);
  457. ohdr->u.rc.reth.rkey =
  458. cpu_to_be32(wqe->rdma_wr.rkey);
  459. ohdr->u.rc.reth.length = cpu_to_be32(len);
  460. qp->s_state = OP(RDMA_READ_REQUEST);
  461. hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
  462. ss = NULL;
  463. len = 0;
  464. bth2 |= IB_BTH_REQ_ACK;
  465. if (++qp->s_cur == qp->s_size)
  466. qp->s_cur = 0;
  467. break;
  468. case IB_WR_ATOMIC_CMP_AND_SWP:
  469. case IB_WR_ATOMIC_FETCH_AND_ADD:
  470. /*
  471. * Don't allow more operations to be started
  472. * than the QP limits allow.
  473. */
  474. if (newreq) {
  475. if (qp->s_num_rd_atomic >=
  476. qp->s_max_rd_atomic) {
  477. qp->s_flags |= RVT_S_WAIT_RDMAR;
  478. goto bail;
  479. }
  480. qp->s_num_rd_atomic++;
  481. if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT))
  482. qp->s_lsn++;
  483. }
  484. if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  485. qp->s_state = OP(COMPARE_SWAP);
  486. put_ib_ateth_swap(wqe->atomic_wr.swap,
  487. &ohdr->u.atomic_eth);
  488. put_ib_ateth_compare(wqe->atomic_wr.compare_add,
  489. &ohdr->u.atomic_eth);
  490. } else {
  491. qp->s_state = OP(FETCH_ADD);
  492. put_ib_ateth_swap(wqe->atomic_wr.compare_add,
  493. &ohdr->u.atomic_eth);
  494. put_ib_ateth_compare(0, &ohdr->u.atomic_eth);
  495. }
  496. put_ib_ateth_vaddr(wqe->atomic_wr.remote_addr,
  497. &ohdr->u.atomic_eth);
  498. ohdr->u.atomic_eth.rkey = cpu_to_be32(
  499. wqe->atomic_wr.rkey);
  500. hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
  501. ss = NULL;
  502. len = 0;
  503. bth2 |= IB_BTH_REQ_ACK;
  504. if (++qp->s_cur == qp->s_size)
  505. qp->s_cur = 0;
  506. break;
  507. default:
  508. goto bail;
  509. }
  510. qp->s_sge.sge = wqe->sg_list[0];
  511. qp->s_sge.sg_list = wqe->sg_list + 1;
  512. qp->s_sge.num_sge = wqe->wr.num_sge;
  513. qp->s_sge.total_len = wqe->length;
  514. qp->s_len = wqe->length;
  515. if (newreq) {
  516. qp->s_tail++;
  517. if (qp->s_tail >= qp->s_size)
  518. qp->s_tail = 0;
  519. }
  520. if (wqe->wr.opcode == IB_WR_RDMA_READ)
  521. qp->s_psn = wqe->lpsn + 1;
  522. else
  523. qp->s_psn++;
  524. break;
  525. case OP(RDMA_READ_RESPONSE_FIRST):
  526. /*
  527. * qp->s_state is normally set to the opcode of the
  528. * last packet constructed for new requests and therefore
  529. * is never set to RDMA read response.
  530. * RDMA_READ_RESPONSE_FIRST is used by the ACK processing
  531. * thread to indicate a SEND needs to be restarted from an
  532. * earlier PSN without interfering with the sending thread.
  533. * See restart_rc().
  534. */
  535. qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
  536. /* FALLTHROUGH */
  537. case OP(SEND_FIRST):
  538. qp->s_state = OP(SEND_MIDDLE);
  539. /* FALLTHROUGH */
  540. case OP(SEND_MIDDLE):
  541. bth2 = mask_psn(qp->s_psn++);
  542. ss = &qp->s_sge;
  543. len = qp->s_len;
  544. if (len > pmtu) {
  545. len = pmtu;
  546. middle = HFI1_CAP_IS_KSET(SDMA_AHG);
  547. break;
  548. }
  549. if (wqe->wr.opcode == IB_WR_SEND) {
  550. qp->s_state = OP(SEND_LAST);
  551. } else if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
  552. qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
  553. /* Immediate data comes after the BTH */
  554. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  555. hwords += 1;
  556. } else {
  557. qp->s_state = OP(SEND_LAST_WITH_INVALIDATE);
  558. /* invalidate data comes after the BTH */
  559. ohdr->u.ieth = cpu_to_be32(wqe->wr.ex.invalidate_rkey);
  560. hwords += 1;
  561. }
  562. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  563. bth0 |= IB_BTH_SOLICITED;
  564. bth2 |= IB_BTH_REQ_ACK;
  565. qp->s_cur++;
  566. if (qp->s_cur >= qp->s_size)
  567. qp->s_cur = 0;
  568. break;
  569. case OP(RDMA_READ_RESPONSE_LAST):
  570. /*
  571. * qp->s_state is normally set to the opcode of the
  572. * last packet constructed for new requests and therefore
  573. * is never set to RDMA read response.
  574. * RDMA_READ_RESPONSE_LAST is used by the ACK processing
  575. * thread to indicate a RDMA write needs to be restarted from
  576. * an earlier PSN without interfering with the sending thread.
  577. * See restart_rc().
  578. */
  579. qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn, pmtu);
  580. /* FALLTHROUGH */
  581. case OP(RDMA_WRITE_FIRST):
  582. qp->s_state = OP(RDMA_WRITE_MIDDLE);
  583. /* FALLTHROUGH */
  584. case OP(RDMA_WRITE_MIDDLE):
  585. bth2 = mask_psn(qp->s_psn++);
  586. ss = &qp->s_sge;
  587. len = qp->s_len;
  588. if (len > pmtu) {
  589. len = pmtu;
  590. middle = HFI1_CAP_IS_KSET(SDMA_AHG);
  591. break;
  592. }
  593. if (wqe->wr.opcode == IB_WR_RDMA_WRITE) {
  594. qp->s_state = OP(RDMA_WRITE_LAST);
  595. } else {
  596. qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
  597. /* Immediate data comes after the BTH */
  598. ohdr->u.imm_data = wqe->wr.ex.imm_data;
  599. hwords += 1;
  600. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  601. bth0 |= IB_BTH_SOLICITED;
  602. }
  603. bth2 |= IB_BTH_REQ_ACK;
  604. qp->s_cur++;
  605. if (qp->s_cur >= qp->s_size)
  606. qp->s_cur = 0;
  607. break;
  608. case OP(RDMA_READ_RESPONSE_MIDDLE):
  609. /*
  610. * qp->s_state is normally set to the opcode of the
  611. * last packet constructed for new requests and therefore
  612. * is never set to RDMA read response.
  613. * RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing
  614. * thread to indicate a RDMA read needs to be restarted from
  615. * an earlier PSN without interfering with the sending thread.
  616. * See restart_rc().
  617. */
  618. len = (delta_psn(qp->s_psn, wqe->psn)) * pmtu;
  619. put_ib_reth_vaddr(
  620. wqe->rdma_wr.remote_addr + len,
  621. &ohdr->u.rc.reth);
  622. ohdr->u.rc.reth.rkey =
  623. cpu_to_be32(wqe->rdma_wr.rkey);
  624. ohdr->u.rc.reth.length = cpu_to_be32(wqe->length - len);
  625. qp->s_state = OP(RDMA_READ_REQUEST);
  626. hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
  627. bth2 = mask_psn(qp->s_psn) | IB_BTH_REQ_ACK;
  628. qp->s_psn = wqe->lpsn + 1;
  629. ss = NULL;
  630. len = 0;
  631. qp->s_cur++;
  632. if (qp->s_cur == qp->s_size)
  633. qp->s_cur = 0;
  634. break;
  635. }
  636. qp->s_sending_hpsn = bth2;
  637. delta = delta_psn(bth2, wqe->psn);
  638. if (delta && delta % HFI1_PSN_CREDIT == 0)
  639. bth2 |= IB_BTH_REQ_ACK;
  640. if (qp->s_flags & RVT_S_SEND_ONE) {
  641. qp->s_flags &= ~RVT_S_SEND_ONE;
  642. qp->s_flags |= RVT_S_WAIT_ACK;
  643. bth2 |= IB_BTH_REQ_ACK;
  644. }
  645. qp->s_len -= len;
  646. qp->s_hdrwords = hwords;
  647. ps->s_txreq->sde = priv->s_sde;
  648. ps->s_txreq->ss = ss;
  649. ps->s_txreq->s_cur_size = len;
  650. hfi1_make_ruc_header(
  651. qp,
  652. ohdr,
  653. bth0 | (qp->s_state << 24),
  654. bth2,
  655. middle,
  656. ps);
  657. /* pbc */
  658. ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
  659. return 1;
  660. done_free_tx:
  661. hfi1_put_txreq(ps->s_txreq);
  662. ps->s_txreq = NULL;
  663. return 1;
  664. bail:
  665. hfi1_put_txreq(ps->s_txreq);
  666. bail_no_tx:
  667. ps->s_txreq = NULL;
  668. qp->s_flags &= ~RVT_S_BUSY;
  669. qp->s_hdrwords = 0;
  670. return 0;
  671. }
  672. /**
  673. * hfi1_send_rc_ack - Construct an ACK packet and send it
  674. * @qp: a pointer to the QP
  675. *
  676. * This is called from hfi1_rc_rcv() and handle_receive_interrupt().
  677. * Note that RDMA reads and atomics are handled in the
  678. * send side QP state and send engine.
  679. */
  680. void hfi1_send_rc_ack(struct hfi1_ctxtdata *rcd, struct rvt_qp *qp,
  681. int is_fecn)
  682. {
  683. struct hfi1_ibport *ibp = rcd_to_iport(rcd);
  684. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  685. u64 pbc, pbc_flags = 0;
  686. u16 lrh0;
  687. u16 sc5;
  688. u32 bth0;
  689. u32 hwords;
  690. u32 vl, plen;
  691. struct send_context *sc;
  692. struct pio_buf *pbuf;
  693. struct ib_header hdr;
  694. struct ib_other_headers *ohdr;
  695. unsigned long flags;
  696. /* clear the defer count */
  697. qp->r_adefered = 0;
  698. /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
  699. if (qp->s_flags & RVT_S_RESP_PENDING)
  700. goto queue_ack;
  701. /* Ensure s_rdma_ack_cnt changes are committed */
  702. smp_read_barrier_depends();
  703. if (qp->s_rdma_ack_cnt)
  704. goto queue_ack;
  705. /* Construct the header */
  706. /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4 */
  707. hwords = 6;
  708. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
  709. hwords += hfi1_make_grh(ibp, &hdr.u.l.grh,
  710. rdma_ah_read_grh(&qp->remote_ah_attr),
  711. hwords, 0);
  712. ohdr = &hdr.u.l.oth;
  713. lrh0 = HFI1_LRH_GRH;
  714. } else {
  715. ohdr = &hdr.u.oth;
  716. lrh0 = HFI1_LRH_BTH;
  717. }
  718. /* read pkey_index w/o lock (its atomic) */
  719. bth0 = hfi1_get_pkey(ibp, qp->s_pkey_index) | (OP(ACKNOWLEDGE) << 24);
  720. if (qp->s_mig_state == IB_MIG_MIGRATED)
  721. bth0 |= IB_BTH_MIG_REQ;
  722. if (qp->r_nak_state)
  723. ohdr->u.aeth = cpu_to_be32((qp->r_msn & IB_MSN_MASK) |
  724. (qp->r_nak_state <<
  725. IB_AETH_CREDIT_SHIFT));
  726. else
  727. ohdr->u.aeth = rvt_compute_aeth(qp);
  728. sc5 = ibp->sl_to_sc[rdma_ah_get_sl(&qp->remote_ah_attr)];
  729. /* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
  730. pbc_flags |= ((!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT);
  731. lrh0 |= (sc5 & 0xf) << 12 | (rdma_ah_get_sl(&qp->remote_ah_attr)
  732. & 0xf) << 4;
  733. hdr.lrh[0] = cpu_to_be16(lrh0);
  734. hdr.lrh[1] = cpu_to_be16(rdma_ah_get_dlid(&qp->remote_ah_attr));
  735. hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
  736. hdr.lrh[3] = cpu_to_be16(ppd->lid |
  737. rdma_ah_get_path_bits(&qp->remote_ah_attr));
  738. ohdr->bth[0] = cpu_to_be32(bth0);
  739. ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
  740. ohdr->bth[1] |= cpu_to_be32((!!is_fecn) << IB_BECN_SHIFT);
  741. ohdr->bth[2] = cpu_to_be32(mask_psn(qp->r_ack_psn));
  742. /* Don't try to send ACKs if the link isn't ACTIVE */
  743. if (driver_lstate(ppd) != IB_PORT_ACTIVE)
  744. return;
  745. sc = rcd->sc;
  746. plen = 2 /* PBC */ + hwords;
  747. vl = sc_to_vlt(ppd->dd, sc5);
  748. pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
  749. pbuf = sc_buffer_alloc(sc, plen, NULL, NULL);
  750. if (!pbuf) {
  751. /*
  752. * We have no room to send at the moment. Pass
  753. * responsibility for sending the ACK to the send engine
  754. * so that when enough buffer space becomes available,
  755. * the ACK is sent ahead of other outgoing packets.
  756. */
  757. goto queue_ack;
  758. }
  759. trace_ack_output_ibhdr(dd_from_ibdev(qp->ibqp.device), &hdr);
  760. /* write the pbc and data */
  761. ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc, &hdr, hwords);
  762. return;
  763. queue_ack:
  764. spin_lock_irqsave(&qp->s_lock, flags);
  765. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
  766. goto unlock;
  767. this_cpu_inc(*ibp->rvp.rc_qacks);
  768. qp->s_flags |= RVT_S_ACK_PENDING | RVT_S_RESP_PENDING;
  769. qp->s_nak_state = qp->r_nak_state;
  770. qp->s_ack_psn = qp->r_ack_psn;
  771. if (is_fecn)
  772. qp->s_flags |= RVT_S_ECN;
  773. /* Schedule the send engine. */
  774. hfi1_schedule_send(qp);
  775. unlock:
  776. spin_unlock_irqrestore(&qp->s_lock, flags);
  777. }
  778. /**
  779. * reset_psn - reset the QP state to send starting from PSN
  780. * @qp: the QP
  781. * @psn: the packet sequence number to restart at
  782. *
  783. * This is called from hfi1_rc_rcv() to process an incoming RC ACK
  784. * for the given QP.
  785. * Called at interrupt level with the QP s_lock held.
  786. */
  787. static void reset_psn(struct rvt_qp *qp, u32 psn)
  788. {
  789. u32 n = qp->s_acked;
  790. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, n);
  791. u32 opcode;
  792. lockdep_assert_held(&qp->s_lock);
  793. qp->s_cur = n;
  794. /*
  795. * If we are starting the request from the beginning,
  796. * let the normal send code handle initialization.
  797. */
  798. if (cmp_psn(psn, wqe->psn) <= 0) {
  799. qp->s_state = OP(SEND_LAST);
  800. goto done;
  801. }
  802. /* Find the work request opcode corresponding to the given PSN. */
  803. opcode = wqe->wr.opcode;
  804. for (;;) {
  805. int diff;
  806. if (++n == qp->s_size)
  807. n = 0;
  808. if (n == qp->s_tail)
  809. break;
  810. wqe = rvt_get_swqe_ptr(qp, n);
  811. diff = cmp_psn(psn, wqe->psn);
  812. if (diff < 0)
  813. break;
  814. qp->s_cur = n;
  815. /*
  816. * If we are starting the request from the beginning,
  817. * let the normal send code handle initialization.
  818. */
  819. if (diff == 0) {
  820. qp->s_state = OP(SEND_LAST);
  821. goto done;
  822. }
  823. opcode = wqe->wr.opcode;
  824. }
  825. /*
  826. * Set the state to restart in the middle of a request.
  827. * Don't change the s_sge, s_cur_sge, or s_cur_size.
  828. * See hfi1_make_rc_req().
  829. */
  830. switch (opcode) {
  831. case IB_WR_SEND:
  832. case IB_WR_SEND_WITH_IMM:
  833. qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
  834. break;
  835. case IB_WR_RDMA_WRITE:
  836. case IB_WR_RDMA_WRITE_WITH_IMM:
  837. qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
  838. break;
  839. case IB_WR_RDMA_READ:
  840. qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
  841. break;
  842. default:
  843. /*
  844. * This case shouldn't happen since its only
  845. * one PSN per req.
  846. */
  847. qp->s_state = OP(SEND_LAST);
  848. }
  849. done:
  850. qp->s_psn = psn;
  851. /*
  852. * Set RVT_S_WAIT_PSN as rc_complete() may start the timer
  853. * asynchronously before the send engine can get scheduled.
  854. * Doing it in hfi1_make_rc_req() is too late.
  855. */
  856. if ((cmp_psn(qp->s_psn, qp->s_sending_hpsn) <= 0) &&
  857. (cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0))
  858. qp->s_flags |= RVT_S_WAIT_PSN;
  859. qp->s_flags &= ~RVT_S_AHG_VALID;
  860. }
  861. /*
  862. * Back up requester to resend the last un-ACKed request.
  863. * The QP r_lock and s_lock should be held and interrupts disabled.
  864. */
  865. void hfi1_restart_rc(struct rvt_qp *qp, u32 psn, int wait)
  866. {
  867. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  868. struct hfi1_ibport *ibp;
  869. lockdep_assert_held(&qp->r_lock);
  870. lockdep_assert_held(&qp->s_lock);
  871. if (qp->s_retry == 0) {
  872. if (qp->s_mig_state == IB_MIG_ARMED) {
  873. hfi1_migrate_qp(qp);
  874. qp->s_retry = qp->s_retry_cnt;
  875. } else if (qp->s_last == qp->s_acked) {
  876. hfi1_send_complete(qp, wqe, IB_WC_RETRY_EXC_ERR);
  877. rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  878. return;
  879. } else { /* need to handle delayed completion */
  880. return;
  881. }
  882. } else {
  883. qp->s_retry--;
  884. }
  885. ibp = to_iport(qp->ibqp.device, qp->port_num);
  886. if (wqe->wr.opcode == IB_WR_RDMA_READ)
  887. ibp->rvp.n_rc_resends++;
  888. else
  889. ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
  890. qp->s_flags &= ~(RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR |
  891. RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_PSN |
  892. RVT_S_WAIT_ACK);
  893. if (wait)
  894. qp->s_flags |= RVT_S_SEND_ONE;
  895. reset_psn(qp, psn);
  896. }
  897. /*
  898. * Set qp->s_sending_psn to the next PSN after the given one.
  899. * This would be psn+1 except when RDMA reads are present.
  900. */
  901. static void reset_sending_psn(struct rvt_qp *qp, u32 psn)
  902. {
  903. struct rvt_swqe *wqe;
  904. u32 n = qp->s_last;
  905. lockdep_assert_held(&qp->s_lock);
  906. /* Find the work request corresponding to the given PSN. */
  907. for (;;) {
  908. wqe = rvt_get_swqe_ptr(qp, n);
  909. if (cmp_psn(psn, wqe->lpsn) <= 0) {
  910. if (wqe->wr.opcode == IB_WR_RDMA_READ)
  911. qp->s_sending_psn = wqe->lpsn + 1;
  912. else
  913. qp->s_sending_psn = psn + 1;
  914. break;
  915. }
  916. if (++n == qp->s_size)
  917. n = 0;
  918. if (n == qp->s_tail)
  919. break;
  920. }
  921. }
  922. /*
  923. * This should be called with the QP s_lock held and interrupts disabled.
  924. */
  925. void hfi1_rc_send_complete(struct rvt_qp *qp, struct ib_header *hdr)
  926. {
  927. struct ib_other_headers *ohdr;
  928. struct rvt_swqe *wqe;
  929. u32 opcode;
  930. u32 psn;
  931. lockdep_assert_held(&qp->s_lock);
  932. if (!(ib_rvt_state_ops[qp->state] & RVT_SEND_OR_FLUSH_OR_RECV_OK))
  933. return;
  934. /* Find out where the BTH is */
  935. if (ib_get_lnh(hdr) == HFI1_LRH_BTH)
  936. ohdr = &hdr->u.oth;
  937. else
  938. ohdr = &hdr->u.l.oth;
  939. opcode = ib_bth_get_opcode(ohdr);
  940. if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
  941. opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
  942. WARN_ON(!qp->s_rdma_ack_cnt);
  943. qp->s_rdma_ack_cnt--;
  944. return;
  945. }
  946. psn = be32_to_cpu(ohdr->bth[2]);
  947. reset_sending_psn(qp, psn);
  948. /*
  949. * Start timer after a packet requesting an ACK has been sent and
  950. * there are still requests that haven't been acked.
  951. */
  952. if ((psn & IB_BTH_REQ_ACK) && qp->s_acked != qp->s_tail &&
  953. !(qp->s_flags &
  954. (RVT_S_TIMER | RVT_S_WAIT_RNR | RVT_S_WAIT_PSN)) &&
  955. (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK))
  956. rvt_add_retry_timer(qp);
  957. while (qp->s_last != qp->s_acked) {
  958. u32 s_last;
  959. wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  960. if (cmp_psn(wqe->lpsn, qp->s_sending_psn) >= 0 &&
  961. cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) <= 0)
  962. break;
  963. s_last = qp->s_last;
  964. trace_hfi1_qp_send_completion(qp, wqe, s_last);
  965. if (++s_last >= qp->s_size)
  966. s_last = 0;
  967. qp->s_last = s_last;
  968. /* see post_send() */
  969. barrier();
  970. rvt_put_swqe(wqe);
  971. rvt_qp_swqe_complete(qp,
  972. wqe,
  973. ib_hfi1_wc_opcode[wqe->wr.opcode],
  974. IB_WC_SUCCESS);
  975. }
  976. /*
  977. * If we were waiting for sends to complete before re-sending,
  978. * and they are now complete, restart sending.
  979. */
  980. trace_hfi1_sendcomplete(qp, psn);
  981. if (qp->s_flags & RVT_S_WAIT_PSN &&
  982. cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
  983. qp->s_flags &= ~RVT_S_WAIT_PSN;
  984. qp->s_sending_psn = qp->s_psn;
  985. qp->s_sending_hpsn = qp->s_psn - 1;
  986. hfi1_schedule_send(qp);
  987. }
  988. }
  989. static inline void update_last_psn(struct rvt_qp *qp, u32 psn)
  990. {
  991. qp->s_last_psn = psn;
  992. }
  993. /*
  994. * Generate a SWQE completion.
  995. * This is similar to hfi1_send_complete but has to check to be sure
  996. * that the SGEs are not being referenced if the SWQE is being resent.
  997. */
  998. static struct rvt_swqe *do_rc_completion(struct rvt_qp *qp,
  999. struct rvt_swqe *wqe,
  1000. struct hfi1_ibport *ibp)
  1001. {
  1002. lockdep_assert_held(&qp->s_lock);
  1003. /*
  1004. * Don't decrement refcount and don't generate a
  1005. * completion if the SWQE is being resent until the send
  1006. * is finished.
  1007. */
  1008. if (cmp_psn(wqe->lpsn, qp->s_sending_psn) < 0 ||
  1009. cmp_psn(qp->s_sending_psn, qp->s_sending_hpsn) > 0) {
  1010. u32 s_last;
  1011. rvt_put_swqe(wqe);
  1012. s_last = qp->s_last;
  1013. trace_hfi1_qp_send_completion(qp, wqe, s_last);
  1014. if (++s_last >= qp->s_size)
  1015. s_last = 0;
  1016. qp->s_last = s_last;
  1017. /* see post_send() */
  1018. barrier();
  1019. rvt_qp_swqe_complete(qp,
  1020. wqe,
  1021. ib_hfi1_wc_opcode[wqe->wr.opcode],
  1022. IB_WC_SUCCESS);
  1023. } else {
  1024. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  1025. this_cpu_inc(*ibp->rvp.rc_delayed_comp);
  1026. /*
  1027. * If send progress not running attempt to progress
  1028. * SDMA queue.
  1029. */
  1030. if (ppd->dd->flags & HFI1_HAS_SEND_DMA) {
  1031. struct sdma_engine *engine;
  1032. u8 sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  1033. u8 sc5;
  1034. /* For now use sc to find engine */
  1035. sc5 = ibp->sl_to_sc[sl];
  1036. engine = qp_to_sdma_engine(qp, sc5);
  1037. sdma_engine_progress_schedule(engine);
  1038. }
  1039. }
  1040. qp->s_retry = qp->s_retry_cnt;
  1041. update_last_psn(qp, wqe->lpsn);
  1042. /*
  1043. * If we are completing a request which is in the process of
  1044. * being resent, we can stop re-sending it since we know the
  1045. * responder has already seen it.
  1046. */
  1047. if (qp->s_acked == qp->s_cur) {
  1048. if (++qp->s_cur >= qp->s_size)
  1049. qp->s_cur = 0;
  1050. qp->s_acked = qp->s_cur;
  1051. wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
  1052. if (qp->s_acked != qp->s_tail) {
  1053. qp->s_state = OP(SEND_LAST);
  1054. qp->s_psn = wqe->psn;
  1055. }
  1056. } else {
  1057. if (++qp->s_acked >= qp->s_size)
  1058. qp->s_acked = 0;
  1059. if (qp->state == IB_QPS_SQD && qp->s_acked == qp->s_cur)
  1060. qp->s_draining = 0;
  1061. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  1062. }
  1063. return wqe;
  1064. }
  1065. /**
  1066. * do_rc_ack - process an incoming RC ACK
  1067. * @qp: the QP the ACK came in on
  1068. * @psn: the packet sequence number of the ACK
  1069. * @opcode: the opcode of the request that resulted in the ACK
  1070. *
  1071. * This is called from rc_rcv_resp() to process an incoming RC ACK
  1072. * for the given QP.
  1073. * May be called at interrupt level, with the QP s_lock held.
  1074. * Returns 1 if OK, 0 if current operation should be aborted (NAK).
  1075. */
  1076. static int do_rc_ack(struct rvt_qp *qp, u32 aeth, u32 psn, int opcode,
  1077. u64 val, struct hfi1_ctxtdata *rcd)
  1078. {
  1079. struct hfi1_ibport *ibp;
  1080. enum ib_wc_status status;
  1081. struct rvt_swqe *wqe;
  1082. int ret = 0;
  1083. u32 ack_psn;
  1084. int diff;
  1085. lockdep_assert_held(&qp->s_lock);
  1086. /*
  1087. * Note that NAKs implicitly ACK outstanding SEND and RDMA write
  1088. * requests and implicitly NAK RDMA read and atomic requests issued
  1089. * before the NAK'ed request. The MSN won't include the NAK'ed
  1090. * request but will include an ACK'ed request(s).
  1091. */
  1092. ack_psn = psn;
  1093. if (aeth >> IB_AETH_NAK_SHIFT)
  1094. ack_psn--;
  1095. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  1096. ibp = rcd_to_iport(rcd);
  1097. /*
  1098. * The MSN might be for a later WQE than the PSN indicates so
  1099. * only complete WQEs that the PSN finishes.
  1100. */
  1101. while ((diff = delta_psn(ack_psn, wqe->lpsn)) >= 0) {
  1102. /*
  1103. * RDMA_READ_RESPONSE_ONLY is a special case since
  1104. * we want to generate completion events for everything
  1105. * before the RDMA read, copy the data, then generate
  1106. * the completion for the read.
  1107. */
  1108. if (wqe->wr.opcode == IB_WR_RDMA_READ &&
  1109. opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
  1110. diff == 0) {
  1111. ret = 1;
  1112. goto bail_stop;
  1113. }
  1114. /*
  1115. * If this request is a RDMA read or atomic, and the ACK is
  1116. * for a later operation, this ACK NAKs the RDMA read or
  1117. * atomic. In other words, only a RDMA_READ_LAST or ONLY
  1118. * can ACK a RDMA read and likewise for atomic ops. Note
  1119. * that the NAK case can only happen if relaxed ordering is
  1120. * used and requests are sent after an RDMA read or atomic
  1121. * is sent but before the response is received.
  1122. */
  1123. if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
  1124. (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
  1125. ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  1126. wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
  1127. (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
  1128. /* Retry this request. */
  1129. if (!(qp->r_flags & RVT_R_RDMAR_SEQ)) {
  1130. qp->r_flags |= RVT_R_RDMAR_SEQ;
  1131. hfi1_restart_rc(qp, qp->s_last_psn + 1, 0);
  1132. if (list_empty(&qp->rspwait)) {
  1133. qp->r_flags |= RVT_R_RSP_SEND;
  1134. rvt_get_qp(qp);
  1135. list_add_tail(&qp->rspwait,
  1136. &rcd->qp_wait_list);
  1137. }
  1138. }
  1139. /*
  1140. * No need to process the ACK/NAK since we are
  1141. * restarting an earlier request.
  1142. */
  1143. goto bail_stop;
  1144. }
  1145. if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  1146. wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
  1147. u64 *vaddr = wqe->sg_list[0].vaddr;
  1148. *vaddr = val;
  1149. }
  1150. if (qp->s_num_rd_atomic &&
  1151. (wqe->wr.opcode == IB_WR_RDMA_READ ||
  1152. wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  1153. wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
  1154. qp->s_num_rd_atomic--;
  1155. /* Restart sending task if fence is complete */
  1156. if ((qp->s_flags & RVT_S_WAIT_FENCE) &&
  1157. !qp->s_num_rd_atomic) {
  1158. qp->s_flags &= ~(RVT_S_WAIT_FENCE |
  1159. RVT_S_WAIT_ACK);
  1160. hfi1_schedule_send(qp);
  1161. } else if (qp->s_flags & RVT_S_WAIT_RDMAR) {
  1162. qp->s_flags &= ~(RVT_S_WAIT_RDMAR |
  1163. RVT_S_WAIT_ACK);
  1164. hfi1_schedule_send(qp);
  1165. }
  1166. }
  1167. wqe = do_rc_completion(qp, wqe, ibp);
  1168. if (qp->s_acked == qp->s_tail)
  1169. break;
  1170. }
  1171. switch (aeth >> IB_AETH_NAK_SHIFT) {
  1172. case 0: /* ACK */
  1173. this_cpu_inc(*ibp->rvp.rc_acks);
  1174. if (qp->s_acked != qp->s_tail) {
  1175. /*
  1176. * We are expecting more ACKs so
  1177. * mod the retry timer.
  1178. */
  1179. rvt_mod_retry_timer(qp);
  1180. /*
  1181. * We can stop re-sending the earlier packets and
  1182. * continue with the next packet the receiver wants.
  1183. */
  1184. if (cmp_psn(qp->s_psn, psn) <= 0)
  1185. reset_psn(qp, psn + 1);
  1186. } else {
  1187. /* No more acks - kill all timers */
  1188. rvt_stop_rc_timers(qp);
  1189. if (cmp_psn(qp->s_psn, psn) <= 0) {
  1190. qp->s_state = OP(SEND_LAST);
  1191. qp->s_psn = psn + 1;
  1192. }
  1193. }
  1194. if (qp->s_flags & RVT_S_WAIT_ACK) {
  1195. qp->s_flags &= ~RVT_S_WAIT_ACK;
  1196. hfi1_schedule_send(qp);
  1197. }
  1198. rvt_get_credit(qp, aeth);
  1199. qp->s_rnr_retry = qp->s_rnr_retry_cnt;
  1200. qp->s_retry = qp->s_retry_cnt;
  1201. update_last_psn(qp, psn);
  1202. return 1;
  1203. case 1: /* RNR NAK */
  1204. ibp->rvp.n_rnr_naks++;
  1205. if (qp->s_acked == qp->s_tail)
  1206. goto bail_stop;
  1207. if (qp->s_flags & RVT_S_WAIT_RNR)
  1208. goto bail_stop;
  1209. if (qp->s_rnr_retry == 0) {
  1210. status = IB_WC_RNR_RETRY_EXC_ERR;
  1211. goto class_b;
  1212. }
  1213. if (qp->s_rnr_retry_cnt < 7)
  1214. qp->s_rnr_retry--;
  1215. /* The last valid PSN is the previous PSN. */
  1216. update_last_psn(qp, psn - 1);
  1217. ibp->rvp.n_rc_resends += delta_psn(qp->s_psn, psn);
  1218. reset_psn(qp, psn);
  1219. qp->s_flags &= ~(RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_ACK);
  1220. rvt_stop_rc_timers(qp);
  1221. rvt_add_rnr_timer(qp, aeth);
  1222. return 0;
  1223. case 3: /* NAK */
  1224. if (qp->s_acked == qp->s_tail)
  1225. goto bail_stop;
  1226. /* The last valid PSN is the previous PSN. */
  1227. update_last_psn(qp, psn - 1);
  1228. switch ((aeth >> IB_AETH_CREDIT_SHIFT) &
  1229. IB_AETH_CREDIT_MASK) {
  1230. case 0: /* PSN sequence error */
  1231. ibp->rvp.n_seq_naks++;
  1232. /*
  1233. * Back up to the responder's expected PSN.
  1234. * Note that we might get a NAK in the middle of an
  1235. * RDMA READ response which terminates the RDMA
  1236. * READ.
  1237. */
  1238. hfi1_restart_rc(qp, psn, 0);
  1239. hfi1_schedule_send(qp);
  1240. break;
  1241. case 1: /* Invalid Request */
  1242. status = IB_WC_REM_INV_REQ_ERR;
  1243. ibp->rvp.n_other_naks++;
  1244. goto class_b;
  1245. case 2: /* Remote Access Error */
  1246. status = IB_WC_REM_ACCESS_ERR;
  1247. ibp->rvp.n_other_naks++;
  1248. goto class_b;
  1249. case 3: /* Remote Operation Error */
  1250. status = IB_WC_REM_OP_ERR;
  1251. ibp->rvp.n_other_naks++;
  1252. class_b:
  1253. if (qp->s_last == qp->s_acked) {
  1254. hfi1_send_complete(qp, wqe, status);
  1255. rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  1256. }
  1257. break;
  1258. default:
  1259. /* Ignore other reserved NAK error codes */
  1260. goto reserved;
  1261. }
  1262. qp->s_retry = qp->s_retry_cnt;
  1263. qp->s_rnr_retry = qp->s_rnr_retry_cnt;
  1264. goto bail_stop;
  1265. default: /* 2: reserved */
  1266. reserved:
  1267. /* Ignore reserved NAK codes. */
  1268. goto bail_stop;
  1269. }
  1270. /* cannot be reached */
  1271. bail_stop:
  1272. rvt_stop_rc_timers(qp);
  1273. return ret;
  1274. }
  1275. /*
  1276. * We have seen an out of sequence RDMA read middle or last packet.
  1277. * This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE.
  1278. */
  1279. static void rdma_seq_err(struct rvt_qp *qp, struct hfi1_ibport *ibp, u32 psn,
  1280. struct hfi1_ctxtdata *rcd)
  1281. {
  1282. struct rvt_swqe *wqe;
  1283. lockdep_assert_held(&qp->s_lock);
  1284. /* Remove QP from retry timer */
  1285. rvt_stop_rc_timers(qp);
  1286. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  1287. while (cmp_psn(psn, wqe->lpsn) > 0) {
  1288. if (wqe->wr.opcode == IB_WR_RDMA_READ ||
  1289. wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  1290. wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
  1291. break;
  1292. wqe = do_rc_completion(qp, wqe, ibp);
  1293. }
  1294. ibp->rvp.n_rdma_seq++;
  1295. qp->r_flags |= RVT_R_RDMAR_SEQ;
  1296. hfi1_restart_rc(qp, qp->s_last_psn + 1, 0);
  1297. if (list_empty(&qp->rspwait)) {
  1298. qp->r_flags |= RVT_R_RSP_SEND;
  1299. rvt_get_qp(qp);
  1300. list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
  1301. }
  1302. }
  1303. /**
  1304. * rc_rcv_resp - process an incoming RC response packet
  1305. * @ibp: the port this packet came in on
  1306. * @ohdr: the other headers for this packet
  1307. * @data: the packet data
  1308. * @tlen: the packet length
  1309. * @qp: the QP for this packet
  1310. * @opcode: the opcode for this packet
  1311. * @psn: the packet sequence number for this packet
  1312. * @hdrsize: the header length
  1313. * @pmtu: the path MTU
  1314. *
  1315. * This is called from hfi1_rc_rcv() to process an incoming RC response
  1316. * packet for the given QP.
  1317. * Called at interrupt level.
  1318. */
  1319. static void rc_rcv_resp(struct hfi1_ibport *ibp,
  1320. struct ib_other_headers *ohdr,
  1321. void *data, u32 tlen, struct rvt_qp *qp,
  1322. u32 opcode, u32 psn, u32 hdrsize, u32 pmtu,
  1323. struct hfi1_ctxtdata *rcd)
  1324. {
  1325. struct rvt_swqe *wqe;
  1326. enum ib_wc_status status;
  1327. unsigned long flags;
  1328. int diff;
  1329. u32 pad;
  1330. u32 aeth;
  1331. u64 val;
  1332. spin_lock_irqsave(&qp->s_lock, flags);
  1333. trace_hfi1_ack(qp, psn);
  1334. /* Ignore invalid responses. */
  1335. smp_read_barrier_depends(); /* see post_one_send */
  1336. if (cmp_psn(psn, READ_ONCE(qp->s_next_psn)) >= 0)
  1337. goto ack_done;
  1338. /* Ignore duplicate responses. */
  1339. diff = cmp_psn(psn, qp->s_last_psn);
  1340. if (unlikely(diff <= 0)) {
  1341. /* Update credits for "ghost" ACKs */
  1342. if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
  1343. aeth = be32_to_cpu(ohdr->u.aeth);
  1344. if ((aeth >> IB_AETH_NAK_SHIFT) == 0)
  1345. rvt_get_credit(qp, aeth);
  1346. }
  1347. goto ack_done;
  1348. }
  1349. /*
  1350. * Skip everything other than the PSN we expect, if we are waiting
  1351. * for a reply to a restarted RDMA read or atomic op.
  1352. */
  1353. if (qp->r_flags & RVT_R_RDMAR_SEQ) {
  1354. if (cmp_psn(psn, qp->s_last_psn + 1) != 0)
  1355. goto ack_done;
  1356. qp->r_flags &= ~RVT_R_RDMAR_SEQ;
  1357. }
  1358. if (unlikely(qp->s_acked == qp->s_tail))
  1359. goto ack_done;
  1360. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  1361. status = IB_WC_SUCCESS;
  1362. switch (opcode) {
  1363. case OP(ACKNOWLEDGE):
  1364. case OP(ATOMIC_ACKNOWLEDGE):
  1365. case OP(RDMA_READ_RESPONSE_FIRST):
  1366. aeth = be32_to_cpu(ohdr->u.aeth);
  1367. if (opcode == OP(ATOMIC_ACKNOWLEDGE))
  1368. val = ib_u64_get(&ohdr->u.at.atomic_ack_eth);
  1369. else
  1370. val = 0;
  1371. if (!do_rc_ack(qp, aeth, psn, opcode, val, rcd) ||
  1372. opcode != OP(RDMA_READ_RESPONSE_FIRST))
  1373. goto ack_done;
  1374. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  1375. if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
  1376. goto ack_op_err;
  1377. /*
  1378. * If this is a response to a resent RDMA read, we
  1379. * have to be careful to copy the data to the right
  1380. * location.
  1381. */
  1382. qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
  1383. wqe, psn, pmtu);
  1384. goto read_middle;
  1385. case OP(RDMA_READ_RESPONSE_MIDDLE):
  1386. /* no AETH, no ACK */
  1387. if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
  1388. goto ack_seq_err;
  1389. if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
  1390. goto ack_op_err;
  1391. read_middle:
  1392. if (unlikely(tlen != (hdrsize + pmtu + 4)))
  1393. goto ack_len_err;
  1394. if (unlikely(pmtu >= qp->s_rdma_read_len))
  1395. goto ack_len_err;
  1396. /*
  1397. * We got a response so update the timeout.
  1398. * 4.096 usec. * (1 << qp->timeout)
  1399. */
  1400. rvt_mod_retry_timer(qp);
  1401. if (qp->s_flags & RVT_S_WAIT_ACK) {
  1402. qp->s_flags &= ~RVT_S_WAIT_ACK;
  1403. hfi1_schedule_send(qp);
  1404. }
  1405. if (opcode == OP(RDMA_READ_RESPONSE_MIDDLE))
  1406. qp->s_retry = qp->s_retry_cnt;
  1407. /*
  1408. * Update the RDMA receive state but do the copy w/o
  1409. * holding the locks and blocking interrupts.
  1410. */
  1411. qp->s_rdma_read_len -= pmtu;
  1412. update_last_psn(qp, psn);
  1413. spin_unlock_irqrestore(&qp->s_lock, flags);
  1414. hfi1_copy_sge(&qp->s_rdma_read_sge, data, pmtu, false, false);
  1415. goto bail;
  1416. case OP(RDMA_READ_RESPONSE_ONLY):
  1417. aeth = be32_to_cpu(ohdr->u.aeth);
  1418. if (!do_rc_ack(qp, aeth, psn, opcode, 0, rcd))
  1419. goto ack_done;
  1420. /* Get the number of bytes the message was padded by. */
  1421. pad = ib_bth_get_pad(ohdr);
  1422. /*
  1423. * Check that the data size is >= 0 && <= pmtu.
  1424. * Remember to account for ICRC (4).
  1425. */
  1426. if (unlikely(tlen < (hdrsize + pad + 4)))
  1427. goto ack_len_err;
  1428. /*
  1429. * If this is a response to a resent RDMA read, we
  1430. * have to be careful to copy the data to the right
  1431. * location.
  1432. */
  1433. wqe = rvt_get_swqe_ptr(qp, qp->s_acked);
  1434. qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
  1435. wqe, psn, pmtu);
  1436. goto read_last;
  1437. case OP(RDMA_READ_RESPONSE_LAST):
  1438. /* ACKs READ req. */
  1439. if (unlikely(cmp_psn(psn, qp->s_last_psn + 1)))
  1440. goto ack_seq_err;
  1441. if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
  1442. goto ack_op_err;
  1443. /* Get the number of bytes the message was padded by. */
  1444. pad = ib_bth_get_pad(ohdr);
  1445. /*
  1446. * Check that the data size is >= 1 && <= pmtu.
  1447. * Remember to account for ICRC (4).
  1448. */
  1449. if (unlikely(tlen <= (hdrsize + pad + 4)))
  1450. goto ack_len_err;
  1451. read_last:
  1452. tlen -= hdrsize + pad + 4;
  1453. if (unlikely(tlen != qp->s_rdma_read_len))
  1454. goto ack_len_err;
  1455. aeth = be32_to_cpu(ohdr->u.aeth);
  1456. hfi1_copy_sge(&qp->s_rdma_read_sge, data, tlen, false, false);
  1457. WARN_ON(qp->s_rdma_read_sge.num_sge);
  1458. (void)do_rc_ack(qp, aeth, psn,
  1459. OP(RDMA_READ_RESPONSE_LAST), 0, rcd);
  1460. goto ack_done;
  1461. }
  1462. ack_op_err:
  1463. status = IB_WC_LOC_QP_OP_ERR;
  1464. goto ack_err;
  1465. ack_seq_err:
  1466. rdma_seq_err(qp, ibp, psn, rcd);
  1467. goto ack_done;
  1468. ack_len_err:
  1469. status = IB_WC_LOC_LEN_ERR;
  1470. ack_err:
  1471. if (qp->s_last == qp->s_acked) {
  1472. hfi1_send_complete(qp, wqe, status);
  1473. rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  1474. }
  1475. ack_done:
  1476. spin_unlock_irqrestore(&qp->s_lock, flags);
  1477. bail:
  1478. return;
  1479. }
  1480. static inline void rc_defered_ack(struct hfi1_ctxtdata *rcd,
  1481. struct rvt_qp *qp)
  1482. {
  1483. if (list_empty(&qp->rspwait)) {
  1484. qp->r_flags |= RVT_R_RSP_NAK;
  1485. rvt_get_qp(qp);
  1486. list_add_tail(&qp->rspwait, &rcd->qp_wait_list);
  1487. }
  1488. }
  1489. static inline void rc_cancel_ack(struct rvt_qp *qp)
  1490. {
  1491. qp->r_adefered = 0;
  1492. if (list_empty(&qp->rspwait))
  1493. return;
  1494. list_del_init(&qp->rspwait);
  1495. qp->r_flags &= ~RVT_R_RSP_NAK;
  1496. rvt_put_qp(qp);
  1497. }
  1498. /**
  1499. * rc_rcv_error - process an incoming duplicate or error RC packet
  1500. * @ohdr: the other headers for this packet
  1501. * @data: the packet data
  1502. * @qp: the QP for this packet
  1503. * @opcode: the opcode for this packet
  1504. * @psn: the packet sequence number for this packet
  1505. * @diff: the difference between the PSN and the expected PSN
  1506. *
  1507. * This is called from hfi1_rc_rcv() to process an unexpected
  1508. * incoming RC packet for the given QP.
  1509. * Called at interrupt level.
  1510. * Return 1 if no more processing is needed; otherwise return 0 to
  1511. * schedule a response to be sent.
  1512. */
  1513. static noinline int rc_rcv_error(struct ib_other_headers *ohdr, void *data,
  1514. struct rvt_qp *qp, u32 opcode, u32 psn,
  1515. int diff, struct hfi1_ctxtdata *rcd)
  1516. {
  1517. struct hfi1_ibport *ibp = rcd_to_iport(rcd);
  1518. struct rvt_ack_entry *e;
  1519. unsigned long flags;
  1520. u8 i, prev;
  1521. int old_req;
  1522. trace_hfi1_rcv_error(qp, psn);
  1523. if (diff > 0) {
  1524. /*
  1525. * Packet sequence error.
  1526. * A NAK will ACK earlier sends and RDMA writes.
  1527. * Don't queue the NAK if we already sent one.
  1528. */
  1529. if (!qp->r_nak_state) {
  1530. ibp->rvp.n_rc_seqnak++;
  1531. qp->r_nak_state = IB_NAK_PSN_ERROR;
  1532. /* Use the expected PSN. */
  1533. qp->r_ack_psn = qp->r_psn;
  1534. /*
  1535. * Wait to send the sequence NAK until all packets
  1536. * in the receive queue have been processed.
  1537. * Otherwise, we end up propagating congestion.
  1538. */
  1539. rc_defered_ack(rcd, qp);
  1540. }
  1541. goto done;
  1542. }
  1543. /*
  1544. * Handle a duplicate request. Don't re-execute SEND, RDMA
  1545. * write or atomic op. Don't NAK errors, just silently drop
  1546. * the duplicate request. Note that r_sge, r_len, and
  1547. * r_rcv_len may be in use so don't modify them.
  1548. *
  1549. * We are supposed to ACK the earliest duplicate PSN but we
  1550. * can coalesce an outstanding duplicate ACK. We have to
  1551. * send the earliest so that RDMA reads can be restarted at
  1552. * the requester's expected PSN.
  1553. *
  1554. * First, find where this duplicate PSN falls within the
  1555. * ACKs previously sent.
  1556. * old_req is true if there is an older response that is scheduled
  1557. * to be sent before sending this one.
  1558. */
  1559. e = NULL;
  1560. old_req = 1;
  1561. ibp->rvp.n_rc_dupreq++;
  1562. spin_lock_irqsave(&qp->s_lock, flags);
  1563. for (i = qp->r_head_ack_queue; ; i = prev) {
  1564. if (i == qp->s_tail_ack_queue)
  1565. old_req = 0;
  1566. if (i)
  1567. prev = i - 1;
  1568. else
  1569. prev = HFI1_MAX_RDMA_ATOMIC;
  1570. if (prev == qp->r_head_ack_queue) {
  1571. e = NULL;
  1572. break;
  1573. }
  1574. e = &qp->s_ack_queue[prev];
  1575. if (!e->opcode) {
  1576. e = NULL;
  1577. break;
  1578. }
  1579. if (cmp_psn(psn, e->psn) >= 0) {
  1580. if (prev == qp->s_tail_ack_queue &&
  1581. cmp_psn(psn, e->lpsn) <= 0)
  1582. old_req = 0;
  1583. break;
  1584. }
  1585. }
  1586. switch (opcode) {
  1587. case OP(RDMA_READ_REQUEST): {
  1588. struct ib_reth *reth;
  1589. u32 offset;
  1590. u32 len;
  1591. /*
  1592. * If we didn't find the RDMA read request in the ack queue,
  1593. * we can ignore this request.
  1594. */
  1595. if (!e || e->opcode != OP(RDMA_READ_REQUEST))
  1596. goto unlock_done;
  1597. /* RETH comes after BTH */
  1598. reth = &ohdr->u.rc.reth;
  1599. /*
  1600. * Address range must be a subset of the original
  1601. * request and start on pmtu boundaries.
  1602. * We reuse the old ack_queue slot since the requester
  1603. * should not back up and request an earlier PSN for the
  1604. * same request.
  1605. */
  1606. offset = delta_psn(psn, e->psn) * qp->pmtu;
  1607. len = be32_to_cpu(reth->length);
  1608. if (unlikely(offset + len != e->rdma_sge.sge_length))
  1609. goto unlock_done;
  1610. if (e->rdma_sge.mr) {
  1611. rvt_put_mr(e->rdma_sge.mr);
  1612. e->rdma_sge.mr = NULL;
  1613. }
  1614. if (len != 0) {
  1615. u32 rkey = be32_to_cpu(reth->rkey);
  1616. u64 vaddr = get_ib_reth_vaddr(reth);
  1617. int ok;
  1618. ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr, rkey,
  1619. IB_ACCESS_REMOTE_READ);
  1620. if (unlikely(!ok))
  1621. goto unlock_done;
  1622. } else {
  1623. e->rdma_sge.vaddr = NULL;
  1624. e->rdma_sge.length = 0;
  1625. e->rdma_sge.sge_length = 0;
  1626. }
  1627. e->psn = psn;
  1628. if (old_req)
  1629. goto unlock_done;
  1630. qp->s_tail_ack_queue = prev;
  1631. break;
  1632. }
  1633. case OP(COMPARE_SWAP):
  1634. case OP(FETCH_ADD): {
  1635. /*
  1636. * If we didn't find the atomic request in the ack queue
  1637. * or the send engine is already backed up to send an
  1638. * earlier entry, we can ignore this request.
  1639. */
  1640. if (!e || e->opcode != (u8)opcode || old_req)
  1641. goto unlock_done;
  1642. qp->s_tail_ack_queue = prev;
  1643. break;
  1644. }
  1645. default:
  1646. /*
  1647. * Ignore this operation if it doesn't request an ACK
  1648. * or an earlier RDMA read or atomic is going to be resent.
  1649. */
  1650. if (!(psn & IB_BTH_REQ_ACK) || old_req)
  1651. goto unlock_done;
  1652. /*
  1653. * Resend the most recent ACK if this request is
  1654. * after all the previous RDMA reads and atomics.
  1655. */
  1656. if (i == qp->r_head_ack_queue) {
  1657. spin_unlock_irqrestore(&qp->s_lock, flags);
  1658. qp->r_nak_state = 0;
  1659. qp->r_ack_psn = qp->r_psn - 1;
  1660. goto send_ack;
  1661. }
  1662. /*
  1663. * Resend the RDMA read or atomic op which
  1664. * ACKs this duplicate request.
  1665. */
  1666. qp->s_tail_ack_queue = i;
  1667. break;
  1668. }
  1669. qp->s_ack_state = OP(ACKNOWLEDGE);
  1670. qp->s_flags |= RVT_S_RESP_PENDING;
  1671. qp->r_nak_state = 0;
  1672. hfi1_schedule_send(qp);
  1673. unlock_done:
  1674. spin_unlock_irqrestore(&qp->s_lock, flags);
  1675. done:
  1676. return 1;
  1677. send_ack:
  1678. return 0;
  1679. }
  1680. static inline void update_ack_queue(struct rvt_qp *qp, unsigned n)
  1681. {
  1682. unsigned next;
  1683. next = n + 1;
  1684. if (next > HFI1_MAX_RDMA_ATOMIC)
  1685. next = 0;
  1686. qp->s_tail_ack_queue = next;
  1687. qp->s_ack_state = OP(ACKNOWLEDGE);
  1688. }
  1689. static void log_cca_event(struct hfi1_pportdata *ppd, u8 sl, u32 rlid,
  1690. u32 lqpn, u32 rqpn, u8 svc_type)
  1691. {
  1692. struct opa_hfi1_cong_log_event_internal *cc_event;
  1693. unsigned long flags;
  1694. if (sl >= OPA_MAX_SLS)
  1695. return;
  1696. spin_lock_irqsave(&ppd->cc_log_lock, flags);
  1697. ppd->threshold_cong_event_map[sl / 8] |= 1 << (sl % 8);
  1698. ppd->threshold_event_counter++;
  1699. cc_event = &ppd->cc_events[ppd->cc_log_idx++];
  1700. if (ppd->cc_log_idx == OPA_CONG_LOG_ELEMS)
  1701. ppd->cc_log_idx = 0;
  1702. cc_event->lqpn = lqpn & RVT_QPN_MASK;
  1703. cc_event->rqpn = rqpn & RVT_QPN_MASK;
  1704. cc_event->sl = sl;
  1705. cc_event->svc_type = svc_type;
  1706. cc_event->rlid = rlid;
  1707. /* keep timestamp in units of 1.024 usec */
  1708. cc_event->timestamp = ktime_to_ns(ktime_get()) / 1024;
  1709. spin_unlock_irqrestore(&ppd->cc_log_lock, flags);
  1710. }
  1711. void process_becn(struct hfi1_pportdata *ppd, u8 sl, u16 rlid, u32 lqpn,
  1712. u32 rqpn, u8 svc_type)
  1713. {
  1714. struct cca_timer *cca_timer;
  1715. u16 ccti, ccti_incr, ccti_timer, ccti_limit;
  1716. u8 trigger_threshold;
  1717. struct cc_state *cc_state;
  1718. unsigned long flags;
  1719. if (sl >= OPA_MAX_SLS)
  1720. return;
  1721. cc_state = get_cc_state(ppd);
  1722. if (!cc_state)
  1723. return;
  1724. /*
  1725. * 1) increase CCTI (for this SL)
  1726. * 2) select IPG (i.e., call set_link_ipg())
  1727. * 3) start timer
  1728. */
  1729. ccti_limit = cc_state->cct.ccti_limit;
  1730. ccti_incr = cc_state->cong_setting.entries[sl].ccti_increase;
  1731. ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
  1732. trigger_threshold =
  1733. cc_state->cong_setting.entries[sl].trigger_threshold;
  1734. spin_lock_irqsave(&ppd->cca_timer_lock, flags);
  1735. cca_timer = &ppd->cca_timer[sl];
  1736. if (cca_timer->ccti < ccti_limit) {
  1737. if (cca_timer->ccti + ccti_incr <= ccti_limit)
  1738. cca_timer->ccti += ccti_incr;
  1739. else
  1740. cca_timer->ccti = ccti_limit;
  1741. set_link_ipg(ppd);
  1742. }
  1743. ccti = cca_timer->ccti;
  1744. if (!hrtimer_active(&cca_timer->hrtimer)) {
  1745. /* ccti_timer is in units of 1.024 usec */
  1746. unsigned long nsec = 1024 * ccti_timer;
  1747. hrtimer_start(&cca_timer->hrtimer, ns_to_ktime(nsec),
  1748. HRTIMER_MODE_REL);
  1749. }
  1750. spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
  1751. if ((trigger_threshold != 0) && (ccti >= trigger_threshold))
  1752. log_cca_event(ppd, sl, rlid, lqpn, rqpn, svc_type);
  1753. }
  1754. /**
  1755. * hfi1_rc_rcv - process an incoming RC packet
  1756. * @rcd: the context pointer
  1757. * @hdr: the header of this packet
  1758. * @rcv_flags: flags relevant to rcv processing
  1759. * @data: the packet data
  1760. * @tlen: the packet length
  1761. * @qp: the QP for this packet
  1762. *
  1763. * This is called from qp_rcv() to process an incoming RC packet
  1764. * for the given QP.
  1765. * May be called at interrupt level.
  1766. */
  1767. void hfi1_rc_rcv(struct hfi1_packet *packet)
  1768. {
  1769. struct hfi1_ctxtdata *rcd = packet->rcd;
  1770. struct ib_header *hdr = packet->hdr;
  1771. u32 rcv_flags = packet->rcv_flags;
  1772. void *data = packet->ebuf;
  1773. u32 tlen = packet->tlen;
  1774. struct rvt_qp *qp = packet->qp;
  1775. struct hfi1_ibport *ibp = rcd_to_iport(rcd);
  1776. struct ib_other_headers *ohdr = packet->ohdr;
  1777. u32 bth0, opcode;
  1778. u32 hdrsize = packet->hlen;
  1779. u32 psn;
  1780. u32 pad;
  1781. struct ib_wc wc;
  1782. u32 pmtu = qp->pmtu;
  1783. int diff;
  1784. struct ib_reth *reth;
  1785. unsigned long flags;
  1786. int ret;
  1787. bool is_fecn = false;
  1788. bool copy_last = false;
  1789. u32 rkey;
  1790. lockdep_assert_held(&qp->r_lock);
  1791. bth0 = be32_to_cpu(ohdr->bth[0]);
  1792. if (hfi1_ruc_check_hdr(ibp, hdr, rcv_flags & HFI1_HAS_GRH, qp, bth0))
  1793. return;
  1794. is_fecn = process_ecn(qp, packet, false);
  1795. psn = be32_to_cpu(ohdr->bth[2]);
  1796. opcode = ib_bth_get_opcode(ohdr);
  1797. /*
  1798. * Process responses (ACKs) before anything else. Note that the
  1799. * packet sequence number will be for something in the send work
  1800. * queue rather than the expected receive packet sequence number.
  1801. * In other words, this QP is the requester.
  1802. */
  1803. if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
  1804. opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
  1805. rc_rcv_resp(ibp, ohdr, data, tlen, qp, opcode, psn,
  1806. hdrsize, pmtu, rcd);
  1807. if (is_fecn)
  1808. goto send_ack;
  1809. return;
  1810. }
  1811. /* Compute 24 bits worth of difference. */
  1812. diff = delta_psn(psn, qp->r_psn);
  1813. if (unlikely(diff)) {
  1814. if (rc_rcv_error(ohdr, data, qp, opcode, psn, diff, rcd))
  1815. return;
  1816. goto send_ack;
  1817. }
  1818. /* Check for opcode sequence errors. */
  1819. switch (qp->r_state) {
  1820. case OP(SEND_FIRST):
  1821. case OP(SEND_MIDDLE):
  1822. if (opcode == OP(SEND_MIDDLE) ||
  1823. opcode == OP(SEND_LAST) ||
  1824. opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
  1825. opcode == OP(SEND_LAST_WITH_INVALIDATE))
  1826. break;
  1827. goto nack_inv;
  1828. case OP(RDMA_WRITE_FIRST):
  1829. case OP(RDMA_WRITE_MIDDLE):
  1830. if (opcode == OP(RDMA_WRITE_MIDDLE) ||
  1831. opcode == OP(RDMA_WRITE_LAST) ||
  1832. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  1833. break;
  1834. goto nack_inv;
  1835. default:
  1836. if (opcode == OP(SEND_MIDDLE) ||
  1837. opcode == OP(SEND_LAST) ||
  1838. opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
  1839. opcode == OP(SEND_LAST_WITH_INVALIDATE) ||
  1840. opcode == OP(RDMA_WRITE_MIDDLE) ||
  1841. opcode == OP(RDMA_WRITE_LAST) ||
  1842. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  1843. goto nack_inv;
  1844. /*
  1845. * Note that it is up to the requester to not send a new
  1846. * RDMA read or atomic operation before receiving an ACK
  1847. * for the previous operation.
  1848. */
  1849. break;
  1850. }
  1851. if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
  1852. rvt_comm_est(qp);
  1853. /* OK, process the packet. */
  1854. switch (opcode) {
  1855. case OP(SEND_FIRST):
  1856. ret = hfi1_rvt_get_rwqe(qp, 0);
  1857. if (ret < 0)
  1858. goto nack_op_err;
  1859. if (!ret)
  1860. goto rnr_nak;
  1861. qp->r_rcv_len = 0;
  1862. /* FALLTHROUGH */
  1863. case OP(SEND_MIDDLE):
  1864. case OP(RDMA_WRITE_MIDDLE):
  1865. send_middle:
  1866. /* Check for invalid length PMTU or posted rwqe len. */
  1867. if (unlikely(tlen != (hdrsize + pmtu + 4)))
  1868. goto nack_inv;
  1869. qp->r_rcv_len += pmtu;
  1870. if (unlikely(qp->r_rcv_len > qp->r_len))
  1871. goto nack_inv;
  1872. hfi1_copy_sge(&qp->r_sge, data, pmtu, true, false);
  1873. break;
  1874. case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
  1875. /* consume RWQE */
  1876. ret = hfi1_rvt_get_rwqe(qp, 1);
  1877. if (ret < 0)
  1878. goto nack_op_err;
  1879. if (!ret)
  1880. goto rnr_nak;
  1881. goto send_last_imm;
  1882. case OP(SEND_ONLY):
  1883. case OP(SEND_ONLY_WITH_IMMEDIATE):
  1884. case OP(SEND_ONLY_WITH_INVALIDATE):
  1885. ret = hfi1_rvt_get_rwqe(qp, 0);
  1886. if (ret < 0)
  1887. goto nack_op_err;
  1888. if (!ret)
  1889. goto rnr_nak;
  1890. qp->r_rcv_len = 0;
  1891. if (opcode == OP(SEND_ONLY))
  1892. goto no_immediate_data;
  1893. if (opcode == OP(SEND_ONLY_WITH_INVALIDATE))
  1894. goto send_last_inv;
  1895. /* FALLTHROUGH for SEND_ONLY_WITH_IMMEDIATE */
  1896. case OP(SEND_LAST_WITH_IMMEDIATE):
  1897. send_last_imm:
  1898. wc.ex.imm_data = ohdr->u.imm_data;
  1899. wc.wc_flags = IB_WC_WITH_IMM;
  1900. goto send_last;
  1901. case OP(SEND_LAST_WITH_INVALIDATE):
  1902. send_last_inv:
  1903. rkey = be32_to_cpu(ohdr->u.ieth);
  1904. if (rvt_invalidate_rkey(qp, rkey))
  1905. goto no_immediate_data;
  1906. wc.ex.invalidate_rkey = rkey;
  1907. wc.wc_flags = IB_WC_WITH_INVALIDATE;
  1908. goto send_last;
  1909. case OP(RDMA_WRITE_LAST):
  1910. copy_last = rvt_is_user_qp(qp);
  1911. /* fall through */
  1912. case OP(SEND_LAST):
  1913. no_immediate_data:
  1914. wc.wc_flags = 0;
  1915. wc.ex.imm_data = 0;
  1916. send_last:
  1917. /* Get the number of bytes the message was padded by. */
  1918. pad = ib_bth_get_pad(ohdr);
  1919. /* Check for invalid length. */
  1920. /* LAST len should be >= 1 */
  1921. if (unlikely(tlen < (hdrsize + pad + 4)))
  1922. goto nack_inv;
  1923. /* Don't count the CRC. */
  1924. tlen -= (hdrsize + pad + 4);
  1925. wc.byte_len = tlen + qp->r_rcv_len;
  1926. if (unlikely(wc.byte_len > qp->r_len))
  1927. goto nack_inv;
  1928. hfi1_copy_sge(&qp->r_sge, data, tlen, true, copy_last);
  1929. rvt_put_ss(&qp->r_sge);
  1930. qp->r_msn++;
  1931. if (!__test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
  1932. break;
  1933. wc.wr_id = qp->r_wr_id;
  1934. wc.status = IB_WC_SUCCESS;
  1935. if (opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE) ||
  1936. opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
  1937. wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
  1938. else
  1939. wc.opcode = IB_WC_RECV;
  1940. wc.qp = &qp->ibqp;
  1941. wc.src_qp = qp->remote_qpn;
  1942. wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr);
  1943. /*
  1944. * It seems that IB mandates the presence of an SL in a
  1945. * work completion only for the UD transport (see section
  1946. * 11.4.2 of IBTA Vol. 1).
  1947. *
  1948. * However, the way the SL is chosen below is consistent
  1949. * with the way that IB/qib works and is trying avoid
  1950. * introducing incompatibilities.
  1951. *
  1952. * See also OPA Vol. 1, section 9.7.6, and table 9-17.
  1953. */
  1954. wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
  1955. /* zero fields that are N/A */
  1956. wc.vendor_err = 0;
  1957. wc.pkey_index = 0;
  1958. wc.dlid_path_bits = 0;
  1959. wc.port_num = 0;
  1960. /* Signal completion event if the solicited bit is set. */
  1961. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
  1962. (bth0 & IB_BTH_SOLICITED) != 0);
  1963. break;
  1964. case OP(RDMA_WRITE_ONLY):
  1965. copy_last = rvt_is_user_qp(qp);
  1966. /* fall through */
  1967. case OP(RDMA_WRITE_FIRST):
  1968. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
  1969. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
  1970. goto nack_inv;
  1971. /* consume RWQE */
  1972. reth = &ohdr->u.rc.reth;
  1973. qp->r_len = be32_to_cpu(reth->length);
  1974. qp->r_rcv_len = 0;
  1975. qp->r_sge.sg_list = NULL;
  1976. if (qp->r_len != 0) {
  1977. u32 rkey = be32_to_cpu(reth->rkey);
  1978. u64 vaddr = get_ib_reth_vaddr(reth);
  1979. int ok;
  1980. /* Check rkey & NAK */
  1981. ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len, vaddr,
  1982. rkey, IB_ACCESS_REMOTE_WRITE);
  1983. if (unlikely(!ok))
  1984. goto nack_acc;
  1985. qp->r_sge.num_sge = 1;
  1986. } else {
  1987. qp->r_sge.num_sge = 0;
  1988. qp->r_sge.sge.mr = NULL;
  1989. qp->r_sge.sge.vaddr = NULL;
  1990. qp->r_sge.sge.length = 0;
  1991. qp->r_sge.sge.sge_length = 0;
  1992. }
  1993. if (opcode == OP(RDMA_WRITE_FIRST))
  1994. goto send_middle;
  1995. else if (opcode == OP(RDMA_WRITE_ONLY))
  1996. goto no_immediate_data;
  1997. ret = hfi1_rvt_get_rwqe(qp, 1);
  1998. if (ret < 0)
  1999. goto nack_op_err;
  2000. if (!ret) {
  2001. /* peer will send again */
  2002. rvt_put_ss(&qp->r_sge);
  2003. goto rnr_nak;
  2004. }
  2005. wc.ex.imm_data = ohdr->u.rc.imm_data;
  2006. wc.wc_flags = IB_WC_WITH_IMM;
  2007. goto send_last;
  2008. case OP(RDMA_READ_REQUEST): {
  2009. struct rvt_ack_entry *e;
  2010. u32 len;
  2011. u8 next;
  2012. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
  2013. goto nack_inv;
  2014. next = qp->r_head_ack_queue + 1;
  2015. /* s_ack_queue is size HFI1_MAX_RDMA_ATOMIC+1 so use > not >= */
  2016. if (next > HFI1_MAX_RDMA_ATOMIC)
  2017. next = 0;
  2018. spin_lock_irqsave(&qp->s_lock, flags);
  2019. if (unlikely(next == qp->s_tail_ack_queue)) {
  2020. if (!qp->s_ack_queue[next].sent)
  2021. goto nack_inv_unlck;
  2022. update_ack_queue(qp, next);
  2023. }
  2024. e = &qp->s_ack_queue[qp->r_head_ack_queue];
  2025. if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
  2026. rvt_put_mr(e->rdma_sge.mr);
  2027. e->rdma_sge.mr = NULL;
  2028. }
  2029. reth = &ohdr->u.rc.reth;
  2030. len = be32_to_cpu(reth->length);
  2031. if (len) {
  2032. u32 rkey = be32_to_cpu(reth->rkey);
  2033. u64 vaddr = get_ib_reth_vaddr(reth);
  2034. int ok;
  2035. /* Check rkey & NAK */
  2036. ok = rvt_rkey_ok(qp, &e->rdma_sge, len, vaddr,
  2037. rkey, IB_ACCESS_REMOTE_READ);
  2038. if (unlikely(!ok))
  2039. goto nack_acc_unlck;
  2040. /*
  2041. * Update the next expected PSN. We add 1 later
  2042. * below, so only add the remainder here.
  2043. */
  2044. qp->r_psn += rvt_div_mtu(qp, len - 1);
  2045. } else {
  2046. e->rdma_sge.mr = NULL;
  2047. e->rdma_sge.vaddr = NULL;
  2048. e->rdma_sge.length = 0;
  2049. e->rdma_sge.sge_length = 0;
  2050. }
  2051. e->opcode = opcode;
  2052. e->sent = 0;
  2053. e->psn = psn;
  2054. e->lpsn = qp->r_psn;
  2055. /*
  2056. * We need to increment the MSN here instead of when we
  2057. * finish sending the result since a duplicate request would
  2058. * increment it more than once.
  2059. */
  2060. qp->r_msn++;
  2061. qp->r_psn++;
  2062. qp->r_state = opcode;
  2063. qp->r_nak_state = 0;
  2064. qp->r_head_ack_queue = next;
  2065. /* Schedule the send engine. */
  2066. qp->s_flags |= RVT_S_RESP_PENDING;
  2067. hfi1_schedule_send(qp);
  2068. spin_unlock_irqrestore(&qp->s_lock, flags);
  2069. if (is_fecn)
  2070. goto send_ack;
  2071. return;
  2072. }
  2073. case OP(COMPARE_SWAP):
  2074. case OP(FETCH_ADD): {
  2075. struct ib_atomic_eth *ateth;
  2076. struct rvt_ack_entry *e;
  2077. u64 vaddr;
  2078. atomic64_t *maddr;
  2079. u64 sdata;
  2080. u32 rkey;
  2081. u8 next;
  2082. if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
  2083. goto nack_inv;
  2084. next = qp->r_head_ack_queue + 1;
  2085. if (next > HFI1_MAX_RDMA_ATOMIC)
  2086. next = 0;
  2087. spin_lock_irqsave(&qp->s_lock, flags);
  2088. if (unlikely(next == qp->s_tail_ack_queue)) {
  2089. if (!qp->s_ack_queue[next].sent)
  2090. goto nack_inv_unlck;
  2091. update_ack_queue(qp, next);
  2092. }
  2093. e = &qp->s_ack_queue[qp->r_head_ack_queue];
  2094. if (e->opcode == OP(RDMA_READ_REQUEST) && e->rdma_sge.mr) {
  2095. rvt_put_mr(e->rdma_sge.mr);
  2096. e->rdma_sge.mr = NULL;
  2097. }
  2098. ateth = &ohdr->u.atomic_eth;
  2099. vaddr = get_ib_ateth_vaddr(ateth);
  2100. if (unlikely(vaddr & (sizeof(u64) - 1)))
  2101. goto nack_inv_unlck;
  2102. rkey = be32_to_cpu(ateth->rkey);
  2103. /* Check rkey & NAK */
  2104. if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
  2105. vaddr, rkey,
  2106. IB_ACCESS_REMOTE_ATOMIC)))
  2107. goto nack_acc_unlck;
  2108. /* Perform atomic OP and save result. */
  2109. maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
  2110. sdata = get_ib_ateth_swap(ateth);
  2111. e->atomic_data = (opcode == OP(FETCH_ADD)) ?
  2112. (u64)atomic64_add_return(sdata, maddr) - sdata :
  2113. (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
  2114. get_ib_ateth_compare(ateth),
  2115. sdata);
  2116. rvt_put_mr(qp->r_sge.sge.mr);
  2117. qp->r_sge.num_sge = 0;
  2118. e->opcode = opcode;
  2119. e->sent = 0;
  2120. e->psn = psn;
  2121. e->lpsn = psn;
  2122. qp->r_msn++;
  2123. qp->r_psn++;
  2124. qp->r_state = opcode;
  2125. qp->r_nak_state = 0;
  2126. qp->r_head_ack_queue = next;
  2127. /* Schedule the send engine. */
  2128. qp->s_flags |= RVT_S_RESP_PENDING;
  2129. hfi1_schedule_send(qp);
  2130. spin_unlock_irqrestore(&qp->s_lock, flags);
  2131. if (is_fecn)
  2132. goto send_ack;
  2133. return;
  2134. }
  2135. default:
  2136. /* NAK unknown opcodes. */
  2137. goto nack_inv;
  2138. }
  2139. qp->r_psn++;
  2140. qp->r_state = opcode;
  2141. qp->r_ack_psn = psn;
  2142. qp->r_nak_state = 0;
  2143. /* Send an ACK if requested or required. */
  2144. if (psn & IB_BTH_REQ_ACK) {
  2145. if (packet->numpkt == 0) {
  2146. rc_cancel_ack(qp);
  2147. goto send_ack;
  2148. }
  2149. if (qp->r_adefered >= HFI1_PSN_CREDIT) {
  2150. rc_cancel_ack(qp);
  2151. goto send_ack;
  2152. }
  2153. if (unlikely(is_fecn)) {
  2154. rc_cancel_ack(qp);
  2155. goto send_ack;
  2156. }
  2157. qp->r_adefered++;
  2158. rc_defered_ack(rcd, qp);
  2159. }
  2160. return;
  2161. rnr_nak:
  2162. qp->r_nak_state = qp->r_min_rnr_timer | IB_RNR_NAK;
  2163. qp->r_ack_psn = qp->r_psn;
  2164. /* Queue RNR NAK for later */
  2165. rc_defered_ack(rcd, qp);
  2166. return;
  2167. nack_op_err:
  2168. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  2169. qp->r_nak_state = IB_NAK_REMOTE_OPERATIONAL_ERROR;
  2170. qp->r_ack_psn = qp->r_psn;
  2171. /* Queue NAK for later */
  2172. rc_defered_ack(rcd, qp);
  2173. return;
  2174. nack_inv_unlck:
  2175. spin_unlock_irqrestore(&qp->s_lock, flags);
  2176. nack_inv:
  2177. rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
  2178. qp->r_nak_state = IB_NAK_INVALID_REQUEST;
  2179. qp->r_ack_psn = qp->r_psn;
  2180. /* Queue NAK for later */
  2181. rc_defered_ack(rcd, qp);
  2182. return;
  2183. nack_acc_unlck:
  2184. spin_unlock_irqrestore(&qp->s_lock, flags);
  2185. nack_acc:
  2186. rvt_rc_error(qp, IB_WC_LOC_PROT_ERR);
  2187. qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
  2188. qp->r_ack_psn = qp->r_psn;
  2189. send_ack:
  2190. hfi1_send_rc_ack(rcd, qp, is_fecn);
  2191. }
  2192. void hfi1_rc_hdrerr(
  2193. struct hfi1_ctxtdata *rcd,
  2194. struct ib_header *hdr,
  2195. u32 rcv_flags,
  2196. struct rvt_qp *qp)
  2197. {
  2198. int has_grh = rcv_flags & HFI1_HAS_GRH;
  2199. struct ib_other_headers *ohdr;
  2200. struct hfi1_ibport *ibp = rcd_to_iport(rcd);
  2201. int diff;
  2202. u32 opcode;
  2203. u32 psn, bth0;
  2204. /* Check for GRH */
  2205. ohdr = &hdr->u.oth;
  2206. if (has_grh)
  2207. ohdr = &hdr->u.l.oth;
  2208. bth0 = be32_to_cpu(ohdr->bth[0]);
  2209. if (hfi1_ruc_check_hdr(ibp, hdr, has_grh, qp, bth0))
  2210. return;
  2211. psn = be32_to_cpu(ohdr->bth[2]);
  2212. opcode = ib_bth_get_opcode(ohdr);
  2213. /* Only deal with RDMA Writes for now */
  2214. if (opcode < IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) {
  2215. diff = delta_psn(psn, qp->r_psn);
  2216. if (!qp->r_nak_state && diff >= 0) {
  2217. ibp->rvp.n_rc_seqnak++;
  2218. qp->r_nak_state = IB_NAK_PSN_ERROR;
  2219. /* Use the expected PSN. */
  2220. qp->r_ack_psn = qp->r_psn;
  2221. /*
  2222. * Wait to send the sequence
  2223. * NAK until all packets
  2224. * in the receive queue have
  2225. * been processed.
  2226. * Otherwise, we end up
  2227. * propagating congestion.
  2228. */
  2229. rc_defered_ack(rcd, qp);
  2230. } /* Out of sequence NAK */
  2231. } /* QP Request NAKs */
  2232. }