qib_rc.c 59 KB

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