ipath_rc.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  1. /*
  2. * Copyright (c) 2006, 2007 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 "ipath_verbs.h"
  34. #include "ipath_kernel.h"
  35. /* cut down ridiculously long IB macro names */
  36. #define OP(x) IB_OPCODE_RC_##x
  37. static u32 restart_sge(struct ipath_sge_state *ss, struct ipath_swqe *wqe,
  38. u32 psn, u32 pmtu)
  39. {
  40. u32 len;
  41. len = ((psn - wqe->psn) & IPATH_PSN_MASK) * pmtu;
  42. ss->sge = wqe->sg_list[0];
  43. ss->sg_list = wqe->sg_list + 1;
  44. ss->num_sge = wqe->wr.num_sge;
  45. ipath_skip_sge(ss, len);
  46. return wqe->length - len;
  47. }
  48. /**
  49. * ipath_init_restart- initialize the qp->s_sge after a restart
  50. * @qp: the QP who's SGE we're restarting
  51. * @wqe: the work queue to initialize the QP's SGE from
  52. *
  53. * The QP s_lock should be held and interrupts disabled.
  54. */
  55. static void ipath_init_restart(struct ipath_qp *qp, struct ipath_swqe *wqe)
  56. {
  57. struct ipath_ibdev *dev;
  58. qp->s_len = restart_sge(&qp->s_sge, wqe, qp->s_psn,
  59. ib_mtu_enum_to_int(qp->path_mtu));
  60. dev = to_idev(qp->ibqp.device);
  61. spin_lock(&dev->pending_lock);
  62. if (list_empty(&qp->timerwait))
  63. list_add_tail(&qp->timerwait,
  64. &dev->pending[dev->pending_index]);
  65. spin_unlock(&dev->pending_lock);
  66. }
  67. /**
  68. * ipath_make_rc_ack - construct a response packet (ACK, NAK, or RDMA read)
  69. * @qp: a pointer to the QP
  70. * @ohdr: a pointer to the IB header being constructed
  71. * @pmtu: the path MTU
  72. *
  73. * Return 1 if constructed; otherwise, return 0.
  74. * Note that we are in the responder's side of the QP context.
  75. * Note the QP s_lock must be held.
  76. */
  77. static int ipath_make_rc_ack(struct ipath_ibdev *dev, struct ipath_qp *qp,
  78. struct ipath_other_headers *ohdr, u32 pmtu)
  79. {
  80. struct ipath_ack_entry *e;
  81. u32 hwords;
  82. u32 len;
  83. u32 bth0;
  84. u32 bth2;
  85. /* header size in 32-bit words LRH+BTH = (8+12)/4. */
  86. hwords = 5;
  87. switch (qp->s_ack_state) {
  88. case OP(RDMA_READ_RESPONSE_LAST):
  89. case OP(RDMA_READ_RESPONSE_ONLY):
  90. case OP(ATOMIC_ACKNOWLEDGE):
  91. /*
  92. * We can increment the tail pointer now that the last
  93. * response has been sent instead of only being
  94. * constructed.
  95. */
  96. if (++qp->s_tail_ack_queue > IPATH_MAX_RDMA_ATOMIC)
  97. qp->s_tail_ack_queue = 0;
  98. /* FALLTHROUGH */
  99. case OP(SEND_ONLY):
  100. case OP(ACKNOWLEDGE):
  101. /* Check for no next entry in the queue. */
  102. if (qp->r_head_ack_queue == qp->s_tail_ack_queue) {
  103. if (qp->s_flags & IPATH_S_ACK_PENDING)
  104. goto normal;
  105. qp->s_ack_state = OP(ACKNOWLEDGE);
  106. goto bail;
  107. }
  108. e = &qp->s_ack_queue[qp->s_tail_ack_queue];
  109. if (e->opcode == OP(RDMA_READ_REQUEST)) {
  110. /* Copy SGE state in case we need to resend */
  111. qp->s_ack_rdma_sge = e->rdma_sge;
  112. qp->s_cur_sge = &qp->s_ack_rdma_sge;
  113. len = e->rdma_sge.sge.sge_length;
  114. if (len > pmtu) {
  115. len = pmtu;
  116. qp->s_ack_state = OP(RDMA_READ_RESPONSE_FIRST);
  117. } else {
  118. qp->s_ack_state = OP(RDMA_READ_RESPONSE_ONLY);
  119. e->sent = 1;
  120. }
  121. ohdr->u.aeth = ipath_compute_aeth(qp);
  122. hwords++;
  123. qp->s_ack_rdma_psn = e->psn;
  124. bth2 = qp->s_ack_rdma_psn++ & IPATH_PSN_MASK;
  125. } else {
  126. /* COMPARE_SWAP or FETCH_ADD */
  127. qp->s_cur_sge = NULL;
  128. len = 0;
  129. qp->s_ack_state = OP(ATOMIC_ACKNOWLEDGE);
  130. ohdr->u.at.aeth = ipath_compute_aeth(qp);
  131. ohdr->u.at.atomic_ack_eth[0] =
  132. cpu_to_be32(e->atomic_data >> 32);
  133. ohdr->u.at.atomic_ack_eth[1] =
  134. cpu_to_be32(e->atomic_data);
  135. hwords += sizeof(ohdr->u.at) / sizeof(u32);
  136. bth2 = e->psn;
  137. e->sent = 1;
  138. }
  139. bth0 = qp->s_ack_state << 24;
  140. break;
  141. case OP(RDMA_READ_RESPONSE_FIRST):
  142. qp->s_ack_state = OP(RDMA_READ_RESPONSE_MIDDLE);
  143. /* FALLTHROUGH */
  144. case OP(RDMA_READ_RESPONSE_MIDDLE):
  145. len = qp->s_ack_rdma_sge.sge.sge_length;
  146. if (len > pmtu)
  147. len = pmtu;
  148. else {
  149. ohdr->u.aeth = ipath_compute_aeth(qp);
  150. hwords++;
  151. qp->s_ack_state = OP(RDMA_READ_RESPONSE_LAST);
  152. qp->s_ack_queue[qp->s_tail_ack_queue].sent = 1;
  153. }
  154. bth0 = qp->s_ack_state << 24;
  155. bth2 = qp->s_ack_rdma_psn++ & IPATH_PSN_MASK;
  156. break;
  157. default:
  158. normal:
  159. /*
  160. * Send a regular ACK.
  161. * Set the s_ack_state so we wait until after sending
  162. * the ACK before setting s_ack_state to ACKNOWLEDGE
  163. * (see above).
  164. */
  165. qp->s_ack_state = OP(SEND_ONLY);
  166. qp->s_flags &= ~IPATH_S_ACK_PENDING;
  167. qp->s_cur_sge = NULL;
  168. if (qp->s_nak_state)
  169. ohdr->u.aeth =
  170. cpu_to_be32((qp->r_msn & IPATH_MSN_MASK) |
  171. (qp->s_nak_state <<
  172. IPATH_AETH_CREDIT_SHIFT));
  173. else
  174. ohdr->u.aeth = ipath_compute_aeth(qp);
  175. hwords++;
  176. len = 0;
  177. bth0 = OP(ACKNOWLEDGE) << 24;
  178. bth2 = qp->s_ack_psn & IPATH_PSN_MASK;
  179. }
  180. qp->s_hdrwords = hwords;
  181. qp->s_cur_size = len;
  182. ipath_make_ruc_header(dev, qp, ohdr, bth0, bth2);
  183. return 1;
  184. bail:
  185. return 0;
  186. }
  187. /**
  188. * ipath_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC)
  189. * @qp: a pointer to the QP
  190. *
  191. * Return 1 if constructed; otherwise, return 0.
  192. */
  193. int ipath_make_rc_req(struct ipath_qp *qp)
  194. {
  195. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  196. struct ipath_other_headers *ohdr;
  197. struct ipath_sge_state *ss;
  198. struct ipath_swqe *wqe;
  199. u32 hwords;
  200. u32 len;
  201. u32 bth0;
  202. u32 bth2;
  203. u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
  204. char newreq;
  205. unsigned long flags;
  206. int ret = 0;
  207. ohdr = &qp->s_hdr.u.oth;
  208. if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
  209. ohdr = &qp->s_hdr.u.l.oth;
  210. /*
  211. * The lock is needed to synchronize between the sending tasklet,
  212. * the receive interrupt handler, and timeout resends.
  213. */
  214. spin_lock_irqsave(&qp->s_lock, flags);
  215. /* Sending responses has higher priority over sending requests. */
  216. if ((qp->r_head_ack_queue != qp->s_tail_ack_queue ||
  217. (qp->s_flags & IPATH_S_ACK_PENDING) ||
  218. qp->s_ack_state != OP(ACKNOWLEDGE)) &&
  219. ipath_make_rc_ack(dev, qp, ohdr, pmtu))
  220. goto done;
  221. if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK) ||
  222. qp->s_rnr_timeout || qp->s_wait_credit)
  223. goto bail;
  224. /* Limit the number of packets sent without an ACK. */
  225. if (ipath_cmp24(qp->s_psn, qp->s_last_psn + IPATH_PSN_CREDIT) > 0) {
  226. qp->s_wait_credit = 1;
  227. dev->n_rc_stalls++;
  228. goto bail;
  229. }
  230. /* header size in 32-bit words LRH+BTH = (8+12)/4. */
  231. hwords = 5;
  232. bth0 = 1 << 22; /* Set M bit */
  233. /* Send a request. */
  234. wqe = get_swqe_ptr(qp, qp->s_cur);
  235. switch (qp->s_state) {
  236. default:
  237. /*
  238. * Resend an old request or start a new one.
  239. *
  240. * We keep track of the current SWQE so that
  241. * we don't reset the "furthest progress" state
  242. * if we need to back up.
  243. */
  244. newreq = 0;
  245. if (qp->s_cur == qp->s_tail) {
  246. /* Check if send work queue is empty. */
  247. if (qp->s_tail == qp->s_head)
  248. goto bail;
  249. /*
  250. * If a fence is requested, wait for previous
  251. * RDMA read and atomic operations to finish.
  252. */
  253. if ((wqe->wr.send_flags & IB_SEND_FENCE) &&
  254. qp->s_num_rd_atomic) {
  255. qp->s_flags |= IPATH_S_FENCE_PENDING;
  256. goto bail;
  257. }
  258. wqe->psn = qp->s_next_psn;
  259. newreq = 1;
  260. }
  261. /*
  262. * Note that we have to be careful not to modify the
  263. * original work request since we may need to resend
  264. * it.
  265. */
  266. len = wqe->length;
  267. ss = &qp->s_sge;
  268. bth2 = 0;
  269. switch (wqe->wr.opcode) {
  270. case IB_WR_SEND:
  271. case IB_WR_SEND_WITH_IMM:
  272. /* If no credit, return. */
  273. if (qp->s_lsn != (u32) -1 &&
  274. ipath_cmp24(wqe->ssn, qp->s_lsn + 1) > 0)
  275. goto bail;
  276. wqe->lpsn = wqe->psn;
  277. if (len > pmtu) {
  278. wqe->lpsn += (len - 1) / pmtu;
  279. qp->s_state = OP(SEND_FIRST);
  280. len = pmtu;
  281. break;
  282. }
  283. if (wqe->wr.opcode == IB_WR_SEND)
  284. qp->s_state = OP(SEND_ONLY);
  285. else {
  286. qp->s_state = OP(SEND_ONLY_WITH_IMMEDIATE);
  287. /* Immediate data comes after the BTH */
  288. ohdr->u.imm_data = wqe->wr.imm_data;
  289. hwords += 1;
  290. }
  291. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  292. bth0 |= 1 << 23;
  293. bth2 = 1 << 31; /* Request ACK. */
  294. if (++qp->s_cur == qp->s_size)
  295. qp->s_cur = 0;
  296. break;
  297. case IB_WR_RDMA_WRITE:
  298. if (newreq && qp->s_lsn != (u32) -1)
  299. qp->s_lsn++;
  300. /* FALLTHROUGH */
  301. case IB_WR_RDMA_WRITE_WITH_IMM:
  302. /* If no credit, return. */
  303. if (qp->s_lsn != (u32) -1 &&
  304. ipath_cmp24(wqe->ssn, qp->s_lsn + 1) > 0)
  305. goto bail;
  306. ohdr->u.rc.reth.vaddr =
  307. cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
  308. ohdr->u.rc.reth.rkey =
  309. cpu_to_be32(wqe->wr.wr.rdma.rkey);
  310. ohdr->u.rc.reth.length = cpu_to_be32(len);
  311. hwords += sizeof(struct ib_reth) / sizeof(u32);
  312. wqe->lpsn = wqe->psn;
  313. if (len > pmtu) {
  314. wqe->lpsn += (len - 1) / pmtu;
  315. qp->s_state = OP(RDMA_WRITE_FIRST);
  316. len = pmtu;
  317. break;
  318. }
  319. if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
  320. qp->s_state = OP(RDMA_WRITE_ONLY);
  321. else {
  322. qp->s_state =
  323. OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
  324. /* Immediate data comes after RETH */
  325. ohdr->u.rc.imm_data = wqe->wr.imm_data;
  326. hwords += 1;
  327. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  328. bth0 |= 1 << 23;
  329. }
  330. bth2 = 1 << 31; /* Request ACK. */
  331. if (++qp->s_cur == qp->s_size)
  332. qp->s_cur = 0;
  333. break;
  334. case IB_WR_RDMA_READ:
  335. /*
  336. * Don't allow more operations to be started
  337. * than the QP limits allow.
  338. */
  339. if (newreq) {
  340. if (qp->s_num_rd_atomic >=
  341. qp->s_max_rd_atomic) {
  342. qp->s_flags |= IPATH_S_RDMAR_PENDING;
  343. goto bail;
  344. }
  345. qp->s_num_rd_atomic++;
  346. if (qp->s_lsn != (u32) -1)
  347. qp->s_lsn++;
  348. /*
  349. * Adjust s_next_psn to count the
  350. * expected number of responses.
  351. */
  352. if (len > pmtu)
  353. qp->s_next_psn += (len - 1) / pmtu;
  354. wqe->lpsn = qp->s_next_psn++;
  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. qp->s_state = OP(RDMA_READ_REQUEST);
  362. hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
  363. ss = NULL;
  364. len = 0;
  365. if (++qp->s_cur == qp->s_size)
  366. qp->s_cur = 0;
  367. break;
  368. case IB_WR_ATOMIC_CMP_AND_SWP:
  369. case IB_WR_ATOMIC_FETCH_AND_ADD:
  370. /*
  371. * Don't allow more operations to be started
  372. * than the QP limits allow.
  373. */
  374. if (newreq) {
  375. if (qp->s_num_rd_atomic >=
  376. qp->s_max_rd_atomic) {
  377. qp->s_flags |= IPATH_S_RDMAR_PENDING;
  378. goto bail;
  379. }
  380. qp->s_num_rd_atomic++;
  381. if (qp->s_lsn != (u32) -1)
  382. qp->s_lsn++;
  383. wqe->lpsn = wqe->psn;
  384. }
  385. if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  386. qp->s_state = OP(COMPARE_SWAP);
  387. ohdr->u.atomic_eth.swap_data = cpu_to_be64(
  388. wqe->wr.wr.atomic.swap);
  389. ohdr->u.atomic_eth.compare_data = cpu_to_be64(
  390. wqe->wr.wr.atomic.compare_add);
  391. } else {
  392. qp->s_state = OP(FETCH_ADD);
  393. ohdr->u.atomic_eth.swap_data = cpu_to_be64(
  394. wqe->wr.wr.atomic.compare_add);
  395. ohdr->u.atomic_eth.compare_data = 0;
  396. }
  397. ohdr->u.atomic_eth.vaddr[0] = cpu_to_be32(
  398. wqe->wr.wr.atomic.remote_addr >> 32);
  399. ohdr->u.atomic_eth.vaddr[1] = cpu_to_be32(
  400. wqe->wr.wr.atomic.remote_addr);
  401. ohdr->u.atomic_eth.rkey = cpu_to_be32(
  402. wqe->wr.wr.atomic.rkey);
  403. hwords += sizeof(struct ib_atomic_eth) / sizeof(u32);
  404. ss = NULL;
  405. len = 0;
  406. if (++qp->s_cur == qp->s_size)
  407. qp->s_cur = 0;
  408. break;
  409. default:
  410. goto bail;
  411. }
  412. qp->s_sge.sge = wqe->sg_list[0];
  413. qp->s_sge.sg_list = wqe->sg_list + 1;
  414. qp->s_sge.num_sge = wqe->wr.num_sge;
  415. qp->s_len = wqe->length;
  416. if (newreq) {
  417. qp->s_tail++;
  418. if (qp->s_tail >= qp->s_size)
  419. qp->s_tail = 0;
  420. }
  421. bth2 |= qp->s_psn & IPATH_PSN_MASK;
  422. if (wqe->wr.opcode == IB_WR_RDMA_READ)
  423. qp->s_psn = wqe->lpsn + 1;
  424. else {
  425. qp->s_psn++;
  426. if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
  427. qp->s_next_psn = qp->s_psn;
  428. }
  429. /*
  430. * Put the QP on the pending list so lost ACKs will cause
  431. * a retry. More than one request can be pending so the
  432. * QP may already be on the dev->pending list.
  433. */
  434. spin_lock(&dev->pending_lock);
  435. if (list_empty(&qp->timerwait))
  436. list_add_tail(&qp->timerwait,
  437. &dev->pending[dev->pending_index]);
  438. spin_unlock(&dev->pending_lock);
  439. break;
  440. case OP(RDMA_READ_RESPONSE_FIRST):
  441. /*
  442. * This case can only happen if a send is restarted.
  443. * See ipath_restart_rc().
  444. */
  445. ipath_init_restart(qp, wqe);
  446. /* FALLTHROUGH */
  447. case OP(SEND_FIRST):
  448. qp->s_state = OP(SEND_MIDDLE);
  449. /* FALLTHROUGH */
  450. case OP(SEND_MIDDLE):
  451. bth2 = qp->s_psn++ & IPATH_PSN_MASK;
  452. if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
  453. qp->s_next_psn = qp->s_psn;
  454. ss = &qp->s_sge;
  455. len = qp->s_len;
  456. if (len > pmtu) {
  457. len = pmtu;
  458. break;
  459. }
  460. if (wqe->wr.opcode == IB_WR_SEND)
  461. qp->s_state = OP(SEND_LAST);
  462. else {
  463. qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
  464. /* Immediate data comes after the BTH */
  465. ohdr->u.imm_data = wqe->wr.imm_data;
  466. hwords += 1;
  467. }
  468. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  469. bth0 |= 1 << 23;
  470. bth2 |= 1 << 31; /* Request ACK. */
  471. qp->s_cur++;
  472. if (qp->s_cur >= qp->s_size)
  473. qp->s_cur = 0;
  474. break;
  475. case OP(RDMA_READ_RESPONSE_LAST):
  476. /*
  477. * This case can only happen if a RDMA write is restarted.
  478. * See ipath_restart_rc().
  479. */
  480. ipath_init_restart(qp, wqe);
  481. /* FALLTHROUGH */
  482. case OP(RDMA_WRITE_FIRST):
  483. qp->s_state = OP(RDMA_WRITE_MIDDLE);
  484. /* FALLTHROUGH */
  485. case OP(RDMA_WRITE_MIDDLE):
  486. bth2 = qp->s_psn++ & IPATH_PSN_MASK;
  487. if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
  488. qp->s_next_psn = qp->s_psn;
  489. ss = &qp->s_sge;
  490. len = qp->s_len;
  491. if (len > pmtu) {
  492. len = pmtu;
  493. break;
  494. }
  495. if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
  496. qp->s_state = OP(RDMA_WRITE_LAST);
  497. else {
  498. qp->s_state = OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
  499. /* Immediate data comes after the BTH */
  500. ohdr->u.imm_data = wqe->wr.imm_data;
  501. hwords += 1;
  502. if (wqe->wr.send_flags & IB_SEND_SOLICITED)
  503. bth0 |= 1 << 23;
  504. }
  505. bth2 |= 1 << 31; /* Request ACK. */
  506. qp->s_cur++;
  507. if (qp->s_cur >= qp->s_size)
  508. qp->s_cur = 0;
  509. break;
  510. case OP(RDMA_READ_RESPONSE_MIDDLE):
  511. /*
  512. * This case can only happen if a RDMA read is restarted.
  513. * See ipath_restart_rc().
  514. */
  515. ipath_init_restart(qp, wqe);
  516. len = ((qp->s_psn - wqe->psn) & IPATH_PSN_MASK) * pmtu;
  517. ohdr->u.rc.reth.vaddr =
  518. cpu_to_be64(wqe->wr.wr.rdma.remote_addr + len);
  519. ohdr->u.rc.reth.rkey =
  520. cpu_to_be32(wqe->wr.wr.rdma.rkey);
  521. ohdr->u.rc.reth.length = cpu_to_be32(qp->s_len);
  522. qp->s_state = OP(RDMA_READ_REQUEST);
  523. hwords += sizeof(ohdr->u.rc.reth) / sizeof(u32);
  524. bth2 = qp->s_psn++ & IPATH_PSN_MASK;
  525. if (ipath_cmp24(qp->s_psn, qp->s_next_psn) > 0)
  526. qp->s_next_psn = qp->s_psn;
  527. ss = NULL;
  528. len = 0;
  529. qp->s_cur++;
  530. if (qp->s_cur == qp->s_size)
  531. qp->s_cur = 0;
  532. break;
  533. }
  534. if (ipath_cmp24(qp->s_psn, qp->s_last_psn + IPATH_PSN_CREDIT - 1) >= 0)
  535. bth2 |= 1 << 31; /* Request ACK. */
  536. qp->s_len -= len;
  537. qp->s_hdrwords = hwords;
  538. qp->s_cur_sge = ss;
  539. qp->s_cur_size = len;
  540. ipath_make_ruc_header(dev, qp, ohdr, bth0 | (qp->s_state << 24), bth2);
  541. done:
  542. ret = 1;
  543. bail:
  544. spin_unlock_irqrestore(&qp->s_lock, flags);
  545. return ret;
  546. }
  547. /**
  548. * send_rc_ack - Construct an ACK packet and send it
  549. * @qp: a pointer to the QP
  550. *
  551. * This is called from ipath_rc_rcv() and only uses the receive
  552. * side QP state.
  553. * Note that RDMA reads and atomics are handled in the
  554. * send side QP state and tasklet.
  555. */
  556. static void send_rc_ack(struct ipath_qp *qp)
  557. {
  558. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  559. u16 lrh0;
  560. u32 bth0;
  561. u32 hwords;
  562. struct ipath_ib_header hdr;
  563. struct ipath_other_headers *ohdr;
  564. unsigned long flags;
  565. /* Don't send ACK or NAK if a RDMA read or atomic is pending. */
  566. if (qp->r_head_ack_queue != qp->s_tail_ack_queue ||
  567. (qp->s_flags & IPATH_S_ACK_PENDING) ||
  568. qp->s_ack_state != OP(ACKNOWLEDGE))
  569. goto queue_ack;
  570. /* Construct the header. */
  571. ohdr = &hdr.u.oth;
  572. lrh0 = IPATH_LRH_BTH;
  573. /* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4. */
  574. hwords = 6;
  575. if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
  576. hwords += ipath_make_grh(dev, &hdr.u.l.grh,
  577. &qp->remote_ah_attr.grh,
  578. hwords, 0);
  579. ohdr = &hdr.u.l.oth;
  580. lrh0 = IPATH_LRH_GRH;
  581. }
  582. /* read pkey_index w/o lock (its atomic) */
  583. bth0 = ipath_get_pkey(dev->dd, qp->s_pkey_index) |
  584. (OP(ACKNOWLEDGE) << 24) | (1 << 22);
  585. if (qp->r_nak_state)
  586. ohdr->u.aeth = cpu_to_be32((qp->r_msn & IPATH_MSN_MASK) |
  587. (qp->r_nak_state <<
  588. IPATH_AETH_CREDIT_SHIFT));
  589. else
  590. ohdr->u.aeth = ipath_compute_aeth(qp);
  591. lrh0 |= qp->remote_ah_attr.sl << 4;
  592. hdr.lrh[0] = cpu_to_be16(lrh0);
  593. hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
  594. hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
  595. hdr.lrh[3] = cpu_to_be16(dev->dd->ipath_lid);
  596. ohdr->bth[0] = cpu_to_be32(bth0);
  597. ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
  598. ohdr->bth[2] = cpu_to_be32(qp->r_ack_psn & IPATH_PSN_MASK);
  599. /*
  600. * If we can send the ACK, clear the ACK state.
  601. */
  602. if (ipath_verbs_send(qp, &hdr, hwords, NULL, 0) == 0) {
  603. dev->n_unicast_xmit++;
  604. goto done;
  605. }
  606. /*
  607. * We are out of PIO buffers at the moment.
  608. * Pass responsibility for sending the ACK to the
  609. * send tasklet so that when a PIO buffer becomes
  610. * available, the ACK is sent ahead of other outgoing
  611. * packets.
  612. */
  613. dev->n_rc_qacks++;
  614. queue_ack:
  615. spin_lock_irqsave(&qp->s_lock, flags);
  616. qp->s_flags |= IPATH_S_ACK_PENDING;
  617. qp->s_nak_state = qp->r_nak_state;
  618. qp->s_ack_psn = qp->r_ack_psn;
  619. spin_unlock_irqrestore(&qp->s_lock, flags);
  620. /* Call ipath_do_rc_send() in another thread. */
  621. tasklet_hi_schedule(&qp->s_task);
  622. done:
  623. return;
  624. }
  625. /**
  626. * reset_psn - reset the QP state to send starting from PSN
  627. * @qp: the QP
  628. * @psn: the packet sequence number to restart at
  629. *
  630. * This is called from ipath_rc_rcv() to process an incoming RC ACK
  631. * for the given QP.
  632. * Called at interrupt level with the QP s_lock held.
  633. */
  634. static void reset_psn(struct ipath_qp *qp, u32 psn)
  635. {
  636. u32 n = qp->s_last;
  637. struct ipath_swqe *wqe = get_swqe_ptr(qp, n);
  638. u32 opcode;
  639. qp->s_cur = n;
  640. /*
  641. * If we are starting the request from the beginning,
  642. * let the normal send code handle initialization.
  643. */
  644. if (ipath_cmp24(psn, wqe->psn) <= 0) {
  645. qp->s_state = OP(SEND_LAST);
  646. goto done;
  647. }
  648. /* Find the work request opcode corresponding to the given PSN. */
  649. opcode = wqe->wr.opcode;
  650. for (;;) {
  651. int diff;
  652. if (++n == qp->s_size)
  653. n = 0;
  654. if (n == qp->s_tail)
  655. break;
  656. wqe = get_swqe_ptr(qp, n);
  657. diff = ipath_cmp24(psn, wqe->psn);
  658. if (diff < 0)
  659. break;
  660. qp->s_cur = n;
  661. /*
  662. * If we are starting the request from the beginning,
  663. * let the normal send code handle initialization.
  664. */
  665. if (diff == 0) {
  666. qp->s_state = OP(SEND_LAST);
  667. goto done;
  668. }
  669. opcode = wqe->wr.opcode;
  670. }
  671. /*
  672. * Set the state to restart in the middle of a request.
  673. * Don't change the s_sge, s_cur_sge, or s_cur_size.
  674. * See ipath_do_rc_send().
  675. */
  676. switch (opcode) {
  677. case IB_WR_SEND:
  678. case IB_WR_SEND_WITH_IMM:
  679. qp->s_state = OP(RDMA_READ_RESPONSE_FIRST);
  680. break;
  681. case IB_WR_RDMA_WRITE:
  682. case IB_WR_RDMA_WRITE_WITH_IMM:
  683. qp->s_state = OP(RDMA_READ_RESPONSE_LAST);
  684. break;
  685. case IB_WR_RDMA_READ:
  686. qp->s_state = OP(RDMA_READ_RESPONSE_MIDDLE);
  687. break;
  688. default:
  689. /*
  690. * This case shouldn't happen since its only
  691. * one PSN per req.
  692. */
  693. qp->s_state = OP(SEND_LAST);
  694. }
  695. done:
  696. qp->s_psn = psn;
  697. }
  698. /**
  699. * ipath_restart_rc - back up requester to resend the last un-ACKed request
  700. * @qp: the QP to restart
  701. * @psn: packet sequence number for the request
  702. * @wc: the work completion request
  703. *
  704. * The QP s_lock should be held and interrupts disabled.
  705. */
  706. void ipath_restart_rc(struct ipath_qp *qp, u32 psn, struct ib_wc *wc)
  707. {
  708. struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  709. struct ipath_ibdev *dev;
  710. if (qp->s_retry == 0) {
  711. wc->wr_id = wqe->wr.wr_id;
  712. wc->status = IB_WC_RETRY_EXC_ERR;
  713. wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  714. wc->vendor_err = 0;
  715. wc->byte_len = 0;
  716. wc->qp = &qp->ibqp;
  717. wc->imm_data = 0;
  718. wc->src_qp = qp->remote_qpn;
  719. wc->wc_flags = 0;
  720. wc->pkey_index = 0;
  721. wc->slid = qp->remote_ah_attr.dlid;
  722. wc->sl = qp->remote_ah_attr.sl;
  723. wc->dlid_path_bits = 0;
  724. wc->port_num = 0;
  725. ipath_sqerror_qp(qp, wc);
  726. goto bail;
  727. }
  728. qp->s_retry--;
  729. /*
  730. * Remove the QP from the timeout queue.
  731. * Note: it may already have been removed by ipath_ib_timer().
  732. */
  733. dev = to_idev(qp->ibqp.device);
  734. spin_lock(&dev->pending_lock);
  735. if (!list_empty(&qp->timerwait))
  736. list_del_init(&qp->timerwait);
  737. spin_unlock(&dev->pending_lock);
  738. if (wqe->wr.opcode == IB_WR_RDMA_READ)
  739. dev->n_rc_resends++;
  740. else
  741. dev->n_rc_resends += (qp->s_psn - psn) & IPATH_PSN_MASK;
  742. reset_psn(qp, psn);
  743. tasklet_hi_schedule(&qp->s_task);
  744. bail:
  745. return;
  746. }
  747. static inline void update_last_psn(struct ipath_qp *qp, u32 psn)
  748. {
  749. if (qp->s_wait_credit) {
  750. qp->s_wait_credit = 0;
  751. tasklet_hi_schedule(&qp->s_task);
  752. }
  753. qp->s_last_psn = psn;
  754. }
  755. /**
  756. * do_rc_ack - process an incoming RC ACK
  757. * @qp: the QP the ACK came in on
  758. * @psn: the packet sequence number of the ACK
  759. * @opcode: the opcode of the request that resulted in the ACK
  760. *
  761. * This is called from ipath_rc_rcv_resp() to process an incoming RC ACK
  762. * for the given QP.
  763. * Called at interrupt level with the QP s_lock held and interrupts disabled.
  764. * Returns 1 if OK, 0 if current operation should be aborted (NAK).
  765. */
  766. static int do_rc_ack(struct ipath_qp *qp, u32 aeth, u32 psn, int opcode,
  767. u64 val)
  768. {
  769. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  770. struct ib_wc wc;
  771. struct ipath_swqe *wqe;
  772. int ret = 0;
  773. u32 ack_psn;
  774. int diff;
  775. /*
  776. * Remove the QP from the timeout queue (or RNR timeout queue).
  777. * If ipath_ib_timer() has already removed it,
  778. * it's OK since we hold the QP s_lock and ipath_restart_rc()
  779. * just won't find anything to restart if we ACK everything.
  780. */
  781. spin_lock(&dev->pending_lock);
  782. if (!list_empty(&qp->timerwait))
  783. list_del_init(&qp->timerwait);
  784. spin_unlock(&dev->pending_lock);
  785. /*
  786. * Note that NAKs implicitly ACK outstanding SEND and RDMA write
  787. * requests and implicitly NAK RDMA read and atomic requests issued
  788. * before the NAK'ed request. The MSN won't include the NAK'ed
  789. * request but will include an ACK'ed request(s).
  790. */
  791. ack_psn = psn;
  792. if (aeth >> 29)
  793. ack_psn--;
  794. wqe = get_swqe_ptr(qp, qp->s_last);
  795. /*
  796. * The MSN might be for a later WQE than the PSN indicates so
  797. * only complete WQEs that the PSN finishes.
  798. */
  799. while ((diff = ipath_cmp24(ack_psn, wqe->lpsn)) >= 0) {
  800. /*
  801. * RDMA_READ_RESPONSE_ONLY is a special case since
  802. * we want to generate completion events for everything
  803. * before the RDMA read, copy the data, then generate
  804. * the completion for the read.
  805. */
  806. if (wqe->wr.opcode == IB_WR_RDMA_READ &&
  807. opcode == OP(RDMA_READ_RESPONSE_ONLY) &&
  808. diff == 0) {
  809. ret = 1;
  810. goto bail;
  811. }
  812. /*
  813. * If this request is a RDMA read or atomic, and the ACK is
  814. * for a later operation, this ACK NAKs the RDMA read or
  815. * atomic. In other words, only a RDMA_READ_LAST or ONLY
  816. * can ACK a RDMA read and likewise for atomic ops. Note
  817. * that the NAK case can only happen if relaxed ordering is
  818. * used and requests are sent after an RDMA read or atomic
  819. * is sent but before the response is received.
  820. */
  821. if ((wqe->wr.opcode == IB_WR_RDMA_READ &&
  822. (opcode != OP(RDMA_READ_RESPONSE_LAST) || diff != 0)) ||
  823. ((wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  824. wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) &&
  825. (opcode != OP(ATOMIC_ACKNOWLEDGE) || diff != 0))) {
  826. /*
  827. * The last valid PSN seen is the previous
  828. * request's.
  829. */
  830. update_last_psn(qp, wqe->psn - 1);
  831. /* Retry this request. */
  832. ipath_restart_rc(qp, wqe->psn, &wc);
  833. /*
  834. * No need to process the ACK/NAK since we are
  835. * restarting an earlier request.
  836. */
  837. goto bail;
  838. }
  839. if (wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  840. wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
  841. *(u64 *) wqe->sg_list[0].vaddr = val;
  842. if (qp->s_num_rd_atomic &&
  843. (wqe->wr.opcode == IB_WR_RDMA_READ ||
  844. wqe->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  845. wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD)) {
  846. qp->s_num_rd_atomic--;
  847. /* Restart sending task if fence is complete */
  848. if ((qp->s_flags & IPATH_S_FENCE_PENDING) &&
  849. !qp->s_num_rd_atomic) {
  850. qp->s_flags &= ~IPATH_S_FENCE_PENDING;
  851. tasklet_hi_schedule(&qp->s_task);
  852. } else if (qp->s_flags & IPATH_S_RDMAR_PENDING) {
  853. qp->s_flags &= ~IPATH_S_RDMAR_PENDING;
  854. tasklet_hi_schedule(&qp->s_task);
  855. }
  856. }
  857. /* Post a send completion queue entry if requested. */
  858. if (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
  859. (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
  860. wc.wr_id = wqe->wr.wr_id;
  861. wc.status = IB_WC_SUCCESS;
  862. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  863. wc.vendor_err = 0;
  864. wc.byte_len = wqe->length;
  865. wc.imm_data = 0;
  866. wc.qp = &qp->ibqp;
  867. wc.src_qp = qp->remote_qpn;
  868. wc.wc_flags = 0;
  869. wc.pkey_index = 0;
  870. wc.slid = qp->remote_ah_attr.dlid;
  871. wc.sl = qp->remote_ah_attr.sl;
  872. wc.dlid_path_bits = 0;
  873. wc.port_num = 0;
  874. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
  875. }
  876. qp->s_retry = qp->s_retry_cnt;
  877. /*
  878. * If we are completing a request which is in the process of
  879. * being resent, we can stop resending it since we know the
  880. * responder has already seen it.
  881. */
  882. if (qp->s_last == qp->s_cur) {
  883. if (++qp->s_cur >= qp->s_size)
  884. qp->s_cur = 0;
  885. qp->s_last = qp->s_cur;
  886. if (qp->s_last == qp->s_tail)
  887. break;
  888. wqe = get_swqe_ptr(qp, qp->s_cur);
  889. qp->s_state = OP(SEND_LAST);
  890. qp->s_psn = wqe->psn;
  891. } else {
  892. if (++qp->s_last >= qp->s_size)
  893. qp->s_last = 0;
  894. if (qp->s_last == qp->s_tail)
  895. break;
  896. wqe = get_swqe_ptr(qp, qp->s_last);
  897. }
  898. }
  899. switch (aeth >> 29) {
  900. case 0: /* ACK */
  901. dev->n_rc_acks++;
  902. /* If this is a partial ACK, reset the retransmit timer. */
  903. if (qp->s_last != qp->s_tail) {
  904. spin_lock(&dev->pending_lock);
  905. if (list_empty(&qp->timerwait))
  906. list_add_tail(&qp->timerwait,
  907. &dev->pending[dev->pending_index]);
  908. spin_unlock(&dev->pending_lock);
  909. /*
  910. * If we get a partial ACK for a resent operation,
  911. * we can stop resending the earlier packets and
  912. * continue with the next packet the receiver wants.
  913. */
  914. if (ipath_cmp24(qp->s_psn, psn) <= 0) {
  915. reset_psn(qp, psn + 1);
  916. tasklet_hi_schedule(&qp->s_task);
  917. }
  918. } else if (ipath_cmp24(qp->s_psn, psn) <= 0) {
  919. qp->s_state = OP(SEND_LAST);
  920. qp->s_psn = psn + 1;
  921. }
  922. ipath_get_credit(qp, aeth);
  923. qp->s_rnr_retry = qp->s_rnr_retry_cnt;
  924. qp->s_retry = qp->s_retry_cnt;
  925. update_last_psn(qp, psn);
  926. ret = 1;
  927. goto bail;
  928. case 1: /* RNR NAK */
  929. dev->n_rnr_naks++;
  930. if (qp->s_last == qp->s_tail)
  931. goto bail;
  932. if (qp->s_rnr_retry == 0) {
  933. wc.status = IB_WC_RNR_RETRY_EXC_ERR;
  934. goto class_b;
  935. }
  936. if (qp->s_rnr_retry_cnt < 7)
  937. qp->s_rnr_retry--;
  938. /* The last valid PSN is the previous PSN. */
  939. update_last_psn(qp, psn - 1);
  940. if (wqe->wr.opcode == IB_WR_RDMA_READ)
  941. dev->n_rc_resends++;
  942. else
  943. dev->n_rc_resends +=
  944. (qp->s_psn - psn) & IPATH_PSN_MASK;
  945. reset_psn(qp, psn);
  946. qp->s_rnr_timeout =
  947. ib_ipath_rnr_table[(aeth >> IPATH_AETH_CREDIT_SHIFT) &
  948. IPATH_AETH_CREDIT_MASK];
  949. ipath_insert_rnr_queue(qp);
  950. goto bail;
  951. case 3: /* NAK */
  952. if (qp->s_last == qp->s_tail)
  953. goto bail;
  954. /* The last valid PSN is the previous PSN. */
  955. update_last_psn(qp, psn - 1);
  956. switch ((aeth >> IPATH_AETH_CREDIT_SHIFT) &
  957. IPATH_AETH_CREDIT_MASK) {
  958. case 0: /* PSN sequence error */
  959. dev->n_seq_naks++;
  960. /*
  961. * Back up to the responder's expected PSN.
  962. * Note that we might get a NAK in the middle of an
  963. * RDMA READ response which terminates the RDMA
  964. * READ.
  965. */
  966. ipath_restart_rc(qp, psn, &wc);
  967. break;
  968. case 1: /* Invalid Request */
  969. wc.status = IB_WC_REM_INV_REQ_ERR;
  970. dev->n_other_naks++;
  971. goto class_b;
  972. case 2: /* Remote Access Error */
  973. wc.status = IB_WC_REM_ACCESS_ERR;
  974. dev->n_other_naks++;
  975. goto class_b;
  976. case 3: /* Remote Operation Error */
  977. wc.status = IB_WC_REM_OP_ERR;
  978. dev->n_other_naks++;
  979. class_b:
  980. wc.wr_id = wqe->wr.wr_id;
  981. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  982. wc.vendor_err = 0;
  983. wc.byte_len = 0;
  984. wc.qp = &qp->ibqp;
  985. wc.imm_data = 0;
  986. wc.src_qp = qp->remote_qpn;
  987. wc.wc_flags = 0;
  988. wc.pkey_index = 0;
  989. wc.slid = qp->remote_ah_attr.dlid;
  990. wc.sl = qp->remote_ah_attr.sl;
  991. wc.dlid_path_bits = 0;
  992. wc.port_num = 0;
  993. ipath_sqerror_qp(qp, &wc);
  994. break;
  995. default:
  996. /* Ignore other reserved NAK error codes */
  997. goto reserved;
  998. }
  999. qp->s_rnr_retry = qp->s_rnr_retry_cnt;
  1000. goto bail;
  1001. default: /* 2: reserved */
  1002. reserved:
  1003. /* Ignore reserved NAK codes. */
  1004. goto bail;
  1005. }
  1006. bail:
  1007. return ret;
  1008. }
  1009. /**
  1010. * ipath_rc_rcv_resp - process an incoming RC response packet
  1011. * @dev: the device this packet came in on
  1012. * @ohdr: the other headers for this packet
  1013. * @data: the packet data
  1014. * @tlen: the packet length
  1015. * @qp: the QP for this packet
  1016. * @opcode: the opcode for this packet
  1017. * @psn: the packet sequence number for this packet
  1018. * @hdrsize: the header length
  1019. * @pmtu: the path MTU
  1020. * @header_in_data: true if part of the header data is in the data buffer
  1021. *
  1022. * This is called from ipath_rc_rcv() to process an incoming RC response
  1023. * packet for the given QP.
  1024. * Called at interrupt level.
  1025. */
  1026. static inline void ipath_rc_rcv_resp(struct ipath_ibdev *dev,
  1027. struct ipath_other_headers *ohdr,
  1028. void *data, u32 tlen,
  1029. struct ipath_qp *qp,
  1030. u32 opcode,
  1031. u32 psn, u32 hdrsize, u32 pmtu,
  1032. int header_in_data)
  1033. {
  1034. struct ipath_swqe *wqe;
  1035. unsigned long flags;
  1036. struct ib_wc wc;
  1037. int diff;
  1038. u32 pad;
  1039. u32 aeth;
  1040. u64 val;
  1041. spin_lock_irqsave(&qp->s_lock, flags);
  1042. /* Ignore invalid responses. */
  1043. if (ipath_cmp24(psn, qp->s_next_psn) >= 0)
  1044. goto ack_done;
  1045. /* Ignore duplicate responses. */
  1046. diff = ipath_cmp24(psn, qp->s_last_psn);
  1047. if (unlikely(diff <= 0)) {
  1048. /* Update credits for "ghost" ACKs */
  1049. if (diff == 0 && opcode == OP(ACKNOWLEDGE)) {
  1050. if (!header_in_data)
  1051. aeth = be32_to_cpu(ohdr->u.aeth);
  1052. else {
  1053. aeth = be32_to_cpu(((__be32 *) data)[0]);
  1054. data += sizeof(__be32);
  1055. }
  1056. if ((aeth >> 29) == 0)
  1057. ipath_get_credit(qp, aeth);
  1058. }
  1059. goto ack_done;
  1060. }
  1061. if (unlikely(qp->s_last == qp->s_tail))
  1062. goto ack_done;
  1063. wqe = get_swqe_ptr(qp, qp->s_last);
  1064. switch (opcode) {
  1065. case OP(ACKNOWLEDGE):
  1066. case OP(ATOMIC_ACKNOWLEDGE):
  1067. case OP(RDMA_READ_RESPONSE_FIRST):
  1068. if (!header_in_data)
  1069. aeth = be32_to_cpu(ohdr->u.aeth);
  1070. else {
  1071. aeth = be32_to_cpu(((__be32 *) data)[0]);
  1072. data += sizeof(__be32);
  1073. }
  1074. if (opcode == OP(ATOMIC_ACKNOWLEDGE)) {
  1075. if (!header_in_data) {
  1076. __be32 *p = ohdr->u.at.atomic_ack_eth;
  1077. val = ((u64) be32_to_cpu(p[0]) << 32) |
  1078. be32_to_cpu(p[1]);
  1079. } else
  1080. val = be64_to_cpu(((__be64 *) data)[0]);
  1081. } else
  1082. val = 0;
  1083. if (!do_rc_ack(qp, aeth, psn, opcode, val) ||
  1084. opcode != OP(RDMA_READ_RESPONSE_FIRST))
  1085. goto ack_done;
  1086. hdrsize += 4;
  1087. wqe = get_swqe_ptr(qp, qp->s_last);
  1088. if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
  1089. goto ack_op_err;
  1090. /*
  1091. * If this is a response to a resent RDMA read, we
  1092. * have to be careful to copy the data to the right
  1093. * location.
  1094. */
  1095. qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
  1096. wqe, psn, pmtu);
  1097. goto read_middle;
  1098. case OP(RDMA_READ_RESPONSE_MIDDLE):
  1099. /* no AETH, no ACK */
  1100. if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
  1101. dev->n_rdma_seq++;
  1102. ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
  1103. goto ack_done;
  1104. }
  1105. if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
  1106. goto ack_op_err;
  1107. read_middle:
  1108. if (unlikely(tlen != (hdrsize + pmtu + 4)))
  1109. goto ack_len_err;
  1110. if (unlikely(pmtu >= qp->s_rdma_read_len))
  1111. goto ack_len_err;
  1112. /* We got a response so update the timeout. */
  1113. spin_lock(&dev->pending_lock);
  1114. if (qp->s_rnr_timeout == 0 && !list_empty(&qp->timerwait))
  1115. list_move_tail(&qp->timerwait,
  1116. &dev->pending[dev->pending_index]);
  1117. spin_unlock(&dev->pending_lock);
  1118. /*
  1119. * Update the RDMA receive state but do the copy w/o
  1120. * holding the locks and blocking interrupts.
  1121. */
  1122. qp->s_rdma_read_len -= pmtu;
  1123. update_last_psn(qp, psn);
  1124. spin_unlock_irqrestore(&qp->s_lock, flags);
  1125. ipath_copy_sge(&qp->s_rdma_read_sge, data, pmtu);
  1126. goto bail;
  1127. case OP(RDMA_READ_RESPONSE_ONLY):
  1128. if (!header_in_data)
  1129. aeth = be32_to_cpu(ohdr->u.aeth);
  1130. else
  1131. aeth = be32_to_cpu(((__be32 *) data)[0]);
  1132. if (!do_rc_ack(qp, aeth, psn, opcode, 0))
  1133. goto ack_done;
  1134. /* Get the number of bytes the message was padded by. */
  1135. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  1136. /*
  1137. * Check that the data size is >= 0 && <= pmtu.
  1138. * Remember to account for the AETH header (4) and
  1139. * ICRC (4).
  1140. */
  1141. if (unlikely(tlen < (hdrsize + pad + 8)))
  1142. goto ack_len_err;
  1143. /*
  1144. * If this is a response to a resent RDMA read, we
  1145. * have to be careful to copy the data to the right
  1146. * location.
  1147. */
  1148. wqe = get_swqe_ptr(qp, qp->s_last);
  1149. qp->s_rdma_read_len = restart_sge(&qp->s_rdma_read_sge,
  1150. wqe, psn, pmtu);
  1151. goto read_last;
  1152. case OP(RDMA_READ_RESPONSE_LAST):
  1153. /* ACKs READ req. */
  1154. if (unlikely(ipath_cmp24(psn, qp->s_last_psn + 1))) {
  1155. dev->n_rdma_seq++;
  1156. ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
  1157. goto ack_done;
  1158. }
  1159. if (unlikely(wqe->wr.opcode != IB_WR_RDMA_READ))
  1160. goto ack_op_err;
  1161. /* Get the number of bytes the message was padded by. */
  1162. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  1163. /*
  1164. * Check that the data size is >= 1 && <= pmtu.
  1165. * Remember to account for the AETH header (4) and
  1166. * ICRC (4).
  1167. */
  1168. if (unlikely(tlen <= (hdrsize + pad + 8)))
  1169. goto ack_len_err;
  1170. read_last:
  1171. tlen -= hdrsize + pad + 8;
  1172. if (unlikely(tlen != qp->s_rdma_read_len))
  1173. goto ack_len_err;
  1174. if (!header_in_data)
  1175. aeth = be32_to_cpu(ohdr->u.aeth);
  1176. else {
  1177. aeth = be32_to_cpu(((__be32 *) data)[0]);
  1178. data += sizeof(__be32);
  1179. }
  1180. ipath_copy_sge(&qp->s_rdma_read_sge, data, tlen);
  1181. (void) do_rc_ack(qp, aeth, psn,
  1182. OP(RDMA_READ_RESPONSE_LAST), 0);
  1183. goto ack_done;
  1184. }
  1185. ack_done:
  1186. spin_unlock_irqrestore(&qp->s_lock, flags);
  1187. goto bail;
  1188. ack_op_err:
  1189. wc.status = IB_WC_LOC_QP_OP_ERR;
  1190. goto ack_err;
  1191. ack_len_err:
  1192. wc.status = IB_WC_LOC_LEN_ERR;
  1193. ack_err:
  1194. wc.wr_id = wqe->wr.wr_id;
  1195. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  1196. wc.vendor_err = 0;
  1197. wc.byte_len = 0;
  1198. wc.imm_data = 0;
  1199. wc.qp = &qp->ibqp;
  1200. wc.src_qp = qp->remote_qpn;
  1201. wc.wc_flags = 0;
  1202. wc.pkey_index = 0;
  1203. wc.slid = qp->remote_ah_attr.dlid;
  1204. wc.sl = qp->remote_ah_attr.sl;
  1205. wc.dlid_path_bits = 0;
  1206. wc.port_num = 0;
  1207. ipath_sqerror_qp(qp, &wc);
  1208. spin_unlock_irqrestore(&qp->s_lock, flags);
  1209. bail:
  1210. return;
  1211. }
  1212. /**
  1213. * ipath_rc_rcv_error - process an incoming duplicate or error RC packet
  1214. * @dev: the device this packet came in on
  1215. * @ohdr: the other headers for this packet
  1216. * @data: the packet data
  1217. * @qp: the QP for this packet
  1218. * @opcode: the opcode for this packet
  1219. * @psn: the packet sequence number for this packet
  1220. * @diff: the difference between the PSN and the expected PSN
  1221. * @header_in_data: true if part of the header data is in the data buffer
  1222. *
  1223. * This is called from ipath_rc_rcv() to process an unexpected
  1224. * incoming RC packet for the given QP.
  1225. * Called at interrupt level.
  1226. * Return 1 if no more processing is needed; otherwise return 0 to
  1227. * schedule a response to be sent.
  1228. */
  1229. static inline int ipath_rc_rcv_error(struct ipath_ibdev *dev,
  1230. struct ipath_other_headers *ohdr,
  1231. void *data,
  1232. struct ipath_qp *qp,
  1233. u32 opcode,
  1234. u32 psn,
  1235. int diff,
  1236. int header_in_data)
  1237. {
  1238. struct ipath_ack_entry *e;
  1239. u8 i, prev;
  1240. int old_req;
  1241. unsigned long flags;
  1242. if (diff > 0) {
  1243. /*
  1244. * Packet sequence error.
  1245. * A NAK will ACK earlier sends and RDMA writes.
  1246. * Don't queue the NAK if we already sent one.
  1247. */
  1248. if (!qp->r_nak_state) {
  1249. qp->r_nak_state = IB_NAK_PSN_ERROR;
  1250. /* Use the expected PSN. */
  1251. qp->r_ack_psn = qp->r_psn;
  1252. goto send_ack;
  1253. }
  1254. goto done;
  1255. }
  1256. /*
  1257. * Handle a duplicate request. Don't re-execute SEND, RDMA
  1258. * write or atomic op. Don't NAK errors, just silently drop
  1259. * the duplicate request. Note that r_sge, r_len, and
  1260. * r_rcv_len may be in use so don't modify them.
  1261. *
  1262. * We are supposed to ACK the earliest duplicate PSN but we
  1263. * can coalesce an outstanding duplicate ACK. We have to
  1264. * send the earliest so that RDMA reads can be restarted at
  1265. * the requester's expected PSN.
  1266. *
  1267. * First, find where this duplicate PSN falls within the
  1268. * ACKs previously sent.
  1269. */
  1270. psn &= IPATH_PSN_MASK;
  1271. e = NULL;
  1272. old_req = 1;
  1273. spin_lock_irqsave(&qp->s_lock, flags);
  1274. for (i = qp->r_head_ack_queue; ; i = prev) {
  1275. if (i == qp->s_tail_ack_queue)
  1276. old_req = 0;
  1277. if (i)
  1278. prev = i - 1;
  1279. else
  1280. prev = IPATH_MAX_RDMA_ATOMIC;
  1281. if (prev == qp->r_head_ack_queue) {
  1282. e = NULL;
  1283. break;
  1284. }
  1285. e = &qp->s_ack_queue[prev];
  1286. if (!e->opcode) {
  1287. e = NULL;
  1288. break;
  1289. }
  1290. if (ipath_cmp24(psn, e->psn) >= 0) {
  1291. if (prev == qp->s_tail_ack_queue)
  1292. old_req = 0;
  1293. break;
  1294. }
  1295. }
  1296. switch (opcode) {
  1297. case OP(RDMA_READ_REQUEST): {
  1298. struct ib_reth *reth;
  1299. u32 offset;
  1300. u32 len;
  1301. /*
  1302. * If we didn't find the RDMA read request in the ack queue,
  1303. * or the send tasklet is already backed up to send an
  1304. * earlier entry, we can ignore this request.
  1305. */
  1306. if (!e || e->opcode != OP(RDMA_READ_REQUEST) || old_req)
  1307. goto unlock_done;
  1308. /* RETH comes after BTH */
  1309. if (!header_in_data)
  1310. reth = &ohdr->u.rc.reth;
  1311. else {
  1312. reth = (struct ib_reth *)data;
  1313. data += sizeof(*reth);
  1314. }
  1315. /*
  1316. * Address range must be a subset of the original
  1317. * request and start on pmtu boundaries.
  1318. * We reuse the old ack_queue slot since the requester
  1319. * should not back up and request an earlier PSN for the
  1320. * same request.
  1321. */
  1322. offset = ((psn - e->psn) & IPATH_PSN_MASK) *
  1323. ib_mtu_enum_to_int(qp->path_mtu);
  1324. len = be32_to_cpu(reth->length);
  1325. if (unlikely(offset + len > e->rdma_sge.sge.sge_length))
  1326. goto unlock_done;
  1327. if (len != 0) {
  1328. u32 rkey = be32_to_cpu(reth->rkey);
  1329. u64 vaddr = be64_to_cpu(reth->vaddr);
  1330. int ok;
  1331. ok = ipath_rkey_ok(qp, &e->rdma_sge,
  1332. len, vaddr, rkey,
  1333. IB_ACCESS_REMOTE_READ);
  1334. if (unlikely(!ok))
  1335. goto unlock_done;
  1336. } else {
  1337. e->rdma_sge.sg_list = NULL;
  1338. e->rdma_sge.num_sge = 0;
  1339. e->rdma_sge.sge.mr = NULL;
  1340. e->rdma_sge.sge.vaddr = NULL;
  1341. e->rdma_sge.sge.length = 0;
  1342. e->rdma_sge.sge.sge_length = 0;
  1343. }
  1344. e->psn = psn;
  1345. qp->s_ack_state = OP(ACKNOWLEDGE);
  1346. qp->s_tail_ack_queue = prev;
  1347. break;
  1348. }
  1349. case OP(COMPARE_SWAP):
  1350. case OP(FETCH_ADD): {
  1351. /*
  1352. * If we didn't find the atomic request in the ack queue
  1353. * or the send tasklet is already backed up to send an
  1354. * earlier entry, we can ignore this request.
  1355. */
  1356. if (!e || e->opcode != (u8) opcode || old_req)
  1357. goto unlock_done;
  1358. qp->s_ack_state = OP(ACKNOWLEDGE);
  1359. qp->s_tail_ack_queue = prev;
  1360. break;
  1361. }
  1362. default:
  1363. if (old_req)
  1364. goto unlock_done;
  1365. /*
  1366. * Resend the most recent ACK if this request is
  1367. * after all the previous RDMA reads and atomics.
  1368. */
  1369. if (i == qp->r_head_ack_queue) {
  1370. spin_unlock_irqrestore(&qp->s_lock, flags);
  1371. qp->r_nak_state = 0;
  1372. qp->r_ack_psn = qp->r_psn - 1;
  1373. goto send_ack;
  1374. }
  1375. /*
  1376. * Try to send a simple ACK to work around a Mellanox bug
  1377. * which doesn't accept a RDMA read response or atomic
  1378. * response as an ACK for earlier SENDs or RDMA writes.
  1379. */
  1380. if (qp->r_head_ack_queue == qp->s_tail_ack_queue &&
  1381. !(qp->s_flags & IPATH_S_ACK_PENDING) &&
  1382. qp->s_ack_state == OP(ACKNOWLEDGE)) {
  1383. spin_unlock_irqrestore(&qp->s_lock, flags);
  1384. qp->r_nak_state = 0;
  1385. qp->r_ack_psn = qp->s_ack_queue[i].psn - 1;
  1386. goto send_ack;
  1387. }
  1388. /*
  1389. * Resend the RDMA read or atomic op which
  1390. * ACKs this duplicate request.
  1391. */
  1392. qp->s_ack_state = OP(ACKNOWLEDGE);
  1393. qp->s_tail_ack_queue = i;
  1394. break;
  1395. }
  1396. qp->r_nak_state = 0;
  1397. tasklet_hi_schedule(&qp->s_task);
  1398. unlock_done:
  1399. spin_unlock_irqrestore(&qp->s_lock, flags);
  1400. done:
  1401. return 1;
  1402. send_ack:
  1403. return 0;
  1404. }
  1405. static void ipath_rc_error(struct ipath_qp *qp, enum ib_wc_status err)
  1406. {
  1407. unsigned long flags;
  1408. int lastwqe;
  1409. spin_lock_irqsave(&qp->s_lock, flags);
  1410. qp->state = IB_QPS_ERR;
  1411. lastwqe = ipath_error_qp(qp, err);
  1412. spin_unlock_irqrestore(&qp->s_lock, flags);
  1413. if (lastwqe) {
  1414. struct ib_event ev;
  1415. ev.device = qp->ibqp.device;
  1416. ev.element.qp = &qp->ibqp;
  1417. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1418. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1419. }
  1420. }
  1421. static inline void ipath_update_ack_queue(struct ipath_qp *qp, unsigned n)
  1422. {
  1423. unsigned long flags;
  1424. unsigned next;
  1425. next = n + 1;
  1426. if (next > IPATH_MAX_RDMA_ATOMIC)
  1427. next = 0;
  1428. spin_lock_irqsave(&qp->s_lock, flags);
  1429. if (n == qp->s_tail_ack_queue) {
  1430. qp->s_tail_ack_queue = next;
  1431. qp->s_ack_state = OP(ACKNOWLEDGE);
  1432. }
  1433. spin_unlock_irqrestore(&qp->s_lock, flags);
  1434. }
  1435. /**
  1436. * ipath_rc_rcv - process an incoming RC packet
  1437. * @dev: the device this packet came in on
  1438. * @hdr: the header of this packet
  1439. * @has_grh: true if the header has a GRH
  1440. * @data: the packet data
  1441. * @tlen: the packet length
  1442. * @qp: the QP for this packet
  1443. *
  1444. * This is called from ipath_qp_rcv() to process an incoming RC packet
  1445. * for the given QP.
  1446. * Called at interrupt level.
  1447. */
  1448. void ipath_rc_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
  1449. int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
  1450. {
  1451. struct ipath_other_headers *ohdr;
  1452. u32 opcode;
  1453. u32 hdrsize;
  1454. u32 psn;
  1455. u32 pad;
  1456. struct ib_wc wc;
  1457. u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
  1458. int diff;
  1459. struct ib_reth *reth;
  1460. int header_in_data;
  1461. /* Validate the SLID. See Ch. 9.6.1.5 */
  1462. if (unlikely(be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid))
  1463. goto done;
  1464. /* Check for GRH */
  1465. if (!has_grh) {
  1466. ohdr = &hdr->u.oth;
  1467. hdrsize = 8 + 12; /* LRH + BTH */
  1468. psn = be32_to_cpu(ohdr->bth[2]);
  1469. header_in_data = 0;
  1470. } else {
  1471. ohdr = &hdr->u.l.oth;
  1472. hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
  1473. /*
  1474. * The header with GRH is 60 bytes and the core driver sets
  1475. * the eager header buffer size to 56 bytes so the last 4
  1476. * bytes of the BTH header (PSN) is in the data buffer.
  1477. */
  1478. header_in_data = dev->dd->ipath_rcvhdrentsize == 16;
  1479. if (header_in_data) {
  1480. psn = be32_to_cpu(((__be32 *) data)[0]);
  1481. data += sizeof(__be32);
  1482. } else
  1483. psn = be32_to_cpu(ohdr->bth[2]);
  1484. }
  1485. /*
  1486. * Process responses (ACKs) before anything else. Note that the
  1487. * packet sequence number will be for something in the send work
  1488. * queue rather than the expected receive packet sequence number.
  1489. * In other words, this QP is the requester.
  1490. */
  1491. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  1492. if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
  1493. opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
  1494. ipath_rc_rcv_resp(dev, ohdr, data, tlen, qp, opcode, psn,
  1495. hdrsize, pmtu, header_in_data);
  1496. goto done;
  1497. }
  1498. /* Compute 24 bits worth of difference. */
  1499. diff = ipath_cmp24(psn, qp->r_psn);
  1500. if (unlikely(diff)) {
  1501. if (ipath_rc_rcv_error(dev, ohdr, data, qp, opcode,
  1502. psn, diff, header_in_data))
  1503. goto done;
  1504. goto send_ack;
  1505. }
  1506. /* Check for opcode sequence errors. */
  1507. switch (qp->r_state) {
  1508. case OP(SEND_FIRST):
  1509. case OP(SEND_MIDDLE):
  1510. if (opcode == OP(SEND_MIDDLE) ||
  1511. opcode == OP(SEND_LAST) ||
  1512. opcode == OP(SEND_LAST_WITH_IMMEDIATE))
  1513. break;
  1514. nack_inv:
  1515. ipath_rc_error(qp, IB_WC_REM_INV_REQ_ERR);
  1516. qp->r_nak_state = IB_NAK_INVALID_REQUEST;
  1517. qp->r_ack_psn = qp->r_psn;
  1518. goto send_ack;
  1519. case OP(RDMA_WRITE_FIRST):
  1520. case OP(RDMA_WRITE_MIDDLE):
  1521. if (opcode == OP(RDMA_WRITE_MIDDLE) ||
  1522. opcode == OP(RDMA_WRITE_LAST) ||
  1523. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  1524. break;
  1525. goto nack_inv;
  1526. default:
  1527. if (opcode == OP(SEND_MIDDLE) ||
  1528. opcode == OP(SEND_LAST) ||
  1529. opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
  1530. opcode == OP(RDMA_WRITE_MIDDLE) ||
  1531. opcode == OP(RDMA_WRITE_LAST) ||
  1532. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  1533. goto nack_inv;
  1534. /*
  1535. * Note that it is up to the requester to not send a new
  1536. * RDMA read or atomic operation before receiving an ACK
  1537. * for the previous operation.
  1538. */
  1539. break;
  1540. }
  1541. wc.imm_data = 0;
  1542. wc.wc_flags = 0;
  1543. /* OK, process the packet. */
  1544. switch (opcode) {
  1545. case OP(SEND_FIRST):
  1546. if (!ipath_get_rwqe(qp, 0)) {
  1547. rnr_nak:
  1548. /*
  1549. * A RNR NAK will ACK earlier sends and RDMA writes.
  1550. * Don't queue the NAK if a RDMA read or atomic
  1551. * is pending though.
  1552. */
  1553. if (qp->r_nak_state)
  1554. goto done;
  1555. qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
  1556. qp->r_ack_psn = qp->r_psn;
  1557. goto send_ack;
  1558. }
  1559. qp->r_rcv_len = 0;
  1560. /* FALLTHROUGH */
  1561. case OP(SEND_MIDDLE):
  1562. case OP(RDMA_WRITE_MIDDLE):
  1563. send_middle:
  1564. /* Check for invalid length PMTU or posted rwqe len. */
  1565. if (unlikely(tlen != (hdrsize + pmtu + 4)))
  1566. goto nack_inv;
  1567. qp->r_rcv_len += pmtu;
  1568. if (unlikely(qp->r_rcv_len > qp->r_len))
  1569. goto nack_inv;
  1570. ipath_copy_sge(&qp->r_sge, data, pmtu);
  1571. break;
  1572. case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
  1573. /* consume RWQE */
  1574. if (!ipath_get_rwqe(qp, 1))
  1575. goto rnr_nak;
  1576. goto send_last_imm;
  1577. case OP(SEND_ONLY):
  1578. case OP(SEND_ONLY_WITH_IMMEDIATE):
  1579. if (!ipath_get_rwqe(qp, 0))
  1580. goto rnr_nak;
  1581. qp->r_rcv_len = 0;
  1582. if (opcode == OP(SEND_ONLY))
  1583. goto send_last;
  1584. /* FALLTHROUGH */
  1585. case OP(SEND_LAST_WITH_IMMEDIATE):
  1586. send_last_imm:
  1587. if (header_in_data) {
  1588. wc.imm_data = *(__be32 *) data;
  1589. data += sizeof(__be32);
  1590. } else {
  1591. /* Immediate data comes after BTH */
  1592. wc.imm_data = ohdr->u.imm_data;
  1593. }
  1594. hdrsize += 4;
  1595. wc.wc_flags = IB_WC_WITH_IMM;
  1596. /* FALLTHROUGH */
  1597. case OP(SEND_LAST):
  1598. case OP(RDMA_WRITE_LAST):
  1599. send_last:
  1600. /* Get the number of bytes the message was padded by. */
  1601. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  1602. /* Check for invalid length. */
  1603. /* XXX LAST len should be >= 1 */
  1604. if (unlikely(tlen < (hdrsize + pad + 4)))
  1605. goto nack_inv;
  1606. /* Don't count the CRC. */
  1607. tlen -= (hdrsize + pad + 4);
  1608. wc.byte_len = tlen + qp->r_rcv_len;
  1609. if (unlikely(wc.byte_len > qp->r_len))
  1610. goto nack_inv;
  1611. ipath_copy_sge(&qp->r_sge, data, tlen);
  1612. qp->r_msn++;
  1613. if (!qp->r_wrid_valid)
  1614. break;
  1615. qp->r_wrid_valid = 0;
  1616. wc.wr_id = qp->r_wr_id;
  1617. wc.status = IB_WC_SUCCESS;
  1618. wc.opcode = IB_WC_RECV;
  1619. wc.vendor_err = 0;
  1620. wc.qp = &qp->ibqp;
  1621. wc.src_qp = qp->remote_qpn;
  1622. wc.pkey_index = 0;
  1623. wc.slid = qp->remote_ah_attr.dlid;
  1624. wc.sl = qp->remote_ah_attr.sl;
  1625. wc.dlid_path_bits = 0;
  1626. wc.port_num = 0;
  1627. /* Signal completion event if the solicited bit is set. */
  1628. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  1629. (ohdr->bth[0] &
  1630. __constant_cpu_to_be32(1 << 23)) != 0);
  1631. break;
  1632. case OP(RDMA_WRITE_FIRST):
  1633. case OP(RDMA_WRITE_ONLY):
  1634. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
  1635. if (unlikely(!(qp->qp_access_flags &
  1636. IB_ACCESS_REMOTE_WRITE)))
  1637. goto nack_inv;
  1638. /* consume RWQE */
  1639. /* RETH comes after BTH */
  1640. if (!header_in_data)
  1641. reth = &ohdr->u.rc.reth;
  1642. else {
  1643. reth = (struct ib_reth *)data;
  1644. data += sizeof(*reth);
  1645. }
  1646. hdrsize += sizeof(*reth);
  1647. qp->r_len = be32_to_cpu(reth->length);
  1648. qp->r_rcv_len = 0;
  1649. if (qp->r_len != 0) {
  1650. u32 rkey = be32_to_cpu(reth->rkey);
  1651. u64 vaddr = be64_to_cpu(reth->vaddr);
  1652. int ok;
  1653. /* Check rkey & NAK */
  1654. ok = ipath_rkey_ok(qp, &qp->r_sge,
  1655. qp->r_len, vaddr, rkey,
  1656. IB_ACCESS_REMOTE_WRITE);
  1657. if (unlikely(!ok))
  1658. goto nack_acc;
  1659. } else {
  1660. qp->r_sge.sg_list = NULL;
  1661. qp->r_sge.sge.mr = NULL;
  1662. qp->r_sge.sge.vaddr = NULL;
  1663. qp->r_sge.sge.length = 0;
  1664. qp->r_sge.sge.sge_length = 0;
  1665. }
  1666. if (opcode == OP(RDMA_WRITE_FIRST))
  1667. goto send_middle;
  1668. else if (opcode == OP(RDMA_WRITE_ONLY))
  1669. goto send_last;
  1670. if (!ipath_get_rwqe(qp, 1))
  1671. goto rnr_nak;
  1672. goto send_last_imm;
  1673. case OP(RDMA_READ_REQUEST): {
  1674. struct ipath_ack_entry *e;
  1675. u32 len;
  1676. u8 next;
  1677. if (unlikely(!(qp->qp_access_flags &
  1678. IB_ACCESS_REMOTE_READ)))
  1679. goto nack_inv;
  1680. next = qp->r_head_ack_queue + 1;
  1681. if (next > IPATH_MAX_RDMA_ATOMIC)
  1682. next = 0;
  1683. if (unlikely(next == qp->s_tail_ack_queue)) {
  1684. if (!qp->s_ack_queue[next].sent)
  1685. goto nack_inv;
  1686. ipath_update_ack_queue(qp, next);
  1687. }
  1688. e = &qp->s_ack_queue[qp->r_head_ack_queue];
  1689. /* RETH comes after BTH */
  1690. if (!header_in_data)
  1691. reth = &ohdr->u.rc.reth;
  1692. else {
  1693. reth = (struct ib_reth *)data;
  1694. data += sizeof(*reth);
  1695. }
  1696. len = be32_to_cpu(reth->length);
  1697. if (len) {
  1698. u32 rkey = be32_to_cpu(reth->rkey);
  1699. u64 vaddr = be64_to_cpu(reth->vaddr);
  1700. int ok;
  1701. /* Check rkey & NAK */
  1702. ok = ipath_rkey_ok(qp, &e->rdma_sge, len, vaddr,
  1703. rkey, IB_ACCESS_REMOTE_READ);
  1704. if (unlikely(!ok))
  1705. goto nack_acc;
  1706. /*
  1707. * Update the next expected PSN. We add 1 later
  1708. * below, so only add the remainder here.
  1709. */
  1710. if (len > pmtu)
  1711. qp->r_psn += (len - 1) / pmtu;
  1712. } else {
  1713. e->rdma_sge.sg_list = NULL;
  1714. e->rdma_sge.num_sge = 0;
  1715. e->rdma_sge.sge.mr = NULL;
  1716. e->rdma_sge.sge.vaddr = NULL;
  1717. e->rdma_sge.sge.length = 0;
  1718. e->rdma_sge.sge.sge_length = 0;
  1719. }
  1720. e->opcode = opcode;
  1721. e->sent = 0;
  1722. e->psn = psn;
  1723. /*
  1724. * We need to increment the MSN here instead of when we
  1725. * finish sending the result since a duplicate request would
  1726. * increment it more than once.
  1727. */
  1728. qp->r_msn++;
  1729. qp->r_psn++;
  1730. qp->r_state = opcode;
  1731. qp->r_nak_state = 0;
  1732. barrier();
  1733. qp->r_head_ack_queue = next;
  1734. /* Call ipath_do_rc_send() in another thread. */
  1735. tasklet_hi_schedule(&qp->s_task);
  1736. goto done;
  1737. }
  1738. case OP(COMPARE_SWAP):
  1739. case OP(FETCH_ADD): {
  1740. struct ib_atomic_eth *ateth;
  1741. struct ipath_ack_entry *e;
  1742. u64 vaddr;
  1743. atomic64_t *maddr;
  1744. u64 sdata;
  1745. u32 rkey;
  1746. u8 next;
  1747. if (unlikely(!(qp->qp_access_flags &
  1748. IB_ACCESS_REMOTE_ATOMIC)))
  1749. goto nack_inv;
  1750. next = qp->r_head_ack_queue + 1;
  1751. if (next > IPATH_MAX_RDMA_ATOMIC)
  1752. next = 0;
  1753. if (unlikely(next == qp->s_tail_ack_queue)) {
  1754. if (!qp->s_ack_queue[next].sent)
  1755. goto nack_inv;
  1756. ipath_update_ack_queue(qp, next);
  1757. }
  1758. if (!header_in_data)
  1759. ateth = &ohdr->u.atomic_eth;
  1760. else
  1761. ateth = (struct ib_atomic_eth *)data;
  1762. vaddr = ((u64) be32_to_cpu(ateth->vaddr[0]) << 32) |
  1763. be32_to_cpu(ateth->vaddr[1]);
  1764. if (unlikely(vaddr & (sizeof(u64) - 1)))
  1765. goto nack_inv;
  1766. rkey = be32_to_cpu(ateth->rkey);
  1767. /* Check rkey & NAK */
  1768. if (unlikely(!ipath_rkey_ok(qp, &qp->r_sge,
  1769. sizeof(u64), vaddr, rkey,
  1770. IB_ACCESS_REMOTE_ATOMIC)))
  1771. goto nack_acc;
  1772. /* Perform atomic OP and save result. */
  1773. maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
  1774. sdata = be64_to_cpu(ateth->swap_data);
  1775. e = &qp->s_ack_queue[qp->r_head_ack_queue];
  1776. e->atomic_data = (opcode == OP(FETCH_ADD)) ?
  1777. (u64) atomic64_add_return(sdata, maddr) - sdata :
  1778. (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
  1779. be64_to_cpu(ateth->compare_data),
  1780. sdata);
  1781. e->opcode = opcode;
  1782. e->sent = 0;
  1783. e->psn = psn & IPATH_PSN_MASK;
  1784. qp->r_msn++;
  1785. qp->r_psn++;
  1786. qp->r_state = opcode;
  1787. qp->r_nak_state = 0;
  1788. barrier();
  1789. qp->r_head_ack_queue = next;
  1790. /* Call ipath_do_rc_send() in another thread. */
  1791. tasklet_hi_schedule(&qp->s_task);
  1792. goto done;
  1793. }
  1794. default:
  1795. /* NAK unknown opcodes. */
  1796. goto nack_inv;
  1797. }
  1798. qp->r_psn++;
  1799. qp->r_state = opcode;
  1800. qp->r_ack_psn = psn;
  1801. qp->r_nak_state = 0;
  1802. /* Send an ACK if requested or required. */
  1803. if (psn & (1 << 31))
  1804. goto send_ack;
  1805. goto done;
  1806. nack_acc:
  1807. ipath_rc_error(qp, IB_WC_REM_ACCESS_ERR);
  1808. qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
  1809. qp->r_ack_psn = qp->r_psn;
  1810. send_ack:
  1811. send_rc_ack(qp);
  1812. done:
  1813. return;
  1814. }