qib_rc.c 61 KB

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