ipath_rc.c 45 KB

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