ipath_rc.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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_kernel.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_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(dev->dd->ipath_lid);
  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 = dev->dd->ipath_rcvhdrentsize == 16;
  1243. if (header_in_data) {
  1244. psn = be32_to_cpu(((__be32 *) data)[0]);
  1245. data += sizeof(__be32);
  1246. } else
  1247. psn = be32_to_cpu(ohdr->bth[2]);
  1248. }
  1249. /*
  1250. * Process responses (ACKs) before anything else. Note that the
  1251. * packet sequence number will be for something in the send work
  1252. * queue rather than the expected receive packet sequence number.
  1253. * In other words, this QP is the requester.
  1254. */
  1255. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  1256. if (opcode >= OP(RDMA_READ_RESPONSE_FIRST) &&
  1257. opcode <= OP(ATOMIC_ACKNOWLEDGE)) {
  1258. ipath_rc_rcv_resp(dev, ohdr, data, tlen, qp, opcode, psn,
  1259. hdrsize, pmtu, header_in_data);
  1260. goto done;
  1261. }
  1262. /* Compute 24 bits worth of difference. */
  1263. diff = ipath_cmp24(psn, qp->r_psn);
  1264. if (unlikely(diff)) {
  1265. if (ipath_rc_rcv_error(dev, ohdr, data, qp, opcode,
  1266. psn, diff, header_in_data))
  1267. goto done;
  1268. goto send_ack;
  1269. }
  1270. /* Check for opcode sequence errors. */
  1271. switch (qp->r_state) {
  1272. case OP(SEND_FIRST):
  1273. case OP(SEND_MIDDLE):
  1274. if (opcode == OP(SEND_MIDDLE) ||
  1275. opcode == OP(SEND_LAST) ||
  1276. opcode == OP(SEND_LAST_WITH_IMMEDIATE))
  1277. break;
  1278. nack_inv:
  1279. /*
  1280. * A NAK will ACK earlier sends and RDMA writes.
  1281. * Don't queue the NAK if a RDMA read, atomic, or NAK
  1282. * is pending though.
  1283. */
  1284. if (qp->r_ack_state >= OP(COMPARE_SWAP))
  1285. goto send_ack;
  1286. /* XXX Flush WQEs */
  1287. qp->state = IB_QPS_ERR;
  1288. qp->r_ack_state = OP(SEND_ONLY);
  1289. qp->r_nak_state = IB_NAK_INVALID_REQUEST;
  1290. qp->r_ack_psn = qp->r_psn;
  1291. goto send_ack;
  1292. case OP(RDMA_WRITE_FIRST):
  1293. case OP(RDMA_WRITE_MIDDLE):
  1294. if (opcode == OP(RDMA_WRITE_MIDDLE) ||
  1295. opcode == OP(RDMA_WRITE_LAST) ||
  1296. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  1297. break;
  1298. goto nack_inv;
  1299. default:
  1300. if (opcode == OP(SEND_MIDDLE) ||
  1301. opcode == OP(SEND_LAST) ||
  1302. opcode == OP(SEND_LAST_WITH_IMMEDIATE) ||
  1303. opcode == OP(RDMA_WRITE_MIDDLE) ||
  1304. opcode == OP(RDMA_WRITE_LAST) ||
  1305. opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
  1306. goto nack_inv;
  1307. /*
  1308. * Note that it is up to the requester to not send a new
  1309. * RDMA read or atomic operation before receiving an ACK
  1310. * for the previous operation.
  1311. */
  1312. break;
  1313. }
  1314. wc.imm_data = 0;
  1315. wc.wc_flags = 0;
  1316. /* OK, process the packet. */
  1317. switch (opcode) {
  1318. case OP(SEND_FIRST):
  1319. if (!ipath_get_rwqe(qp, 0)) {
  1320. rnr_nak:
  1321. /*
  1322. * A RNR NAK will ACK earlier sends and RDMA writes.
  1323. * Don't queue the NAK if a RDMA read or atomic
  1324. * is pending though.
  1325. */
  1326. if (qp->r_ack_state >= OP(COMPARE_SWAP))
  1327. goto send_ack;
  1328. qp->r_ack_state = OP(SEND_ONLY);
  1329. qp->r_nak_state = IB_RNR_NAK | qp->r_min_rnr_timer;
  1330. qp->r_ack_psn = qp->r_psn;
  1331. goto send_ack;
  1332. }
  1333. qp->r_rcv_len = 0;
  1334. /* FALLTHROUGH */
  1335. case OP(SEND_MIDDLE):
  1336. case OP(RDMA_WRITE_MIDDLE):
  1337. send_middle:
  1338. /* Check for invalid length PMTU or posted rwqe len. */
  1339. if (unlikely(tlen != (hdrsize + pmtu + 4)))
  1340. goto nack_inv;
  1341. qp->r_rcv_len += pmtu;
  1342. if (unlikely(qp->r_rcv_len > qp->r_len))
  1343. goto nack_inv;
  1344. ipath_copy_sge(&qp->r_sge, data, pmtu);
  1345. break;
  1346. case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
  1347. /* consume RWQE */
  1348. if (!ipath_get_rwqe(qp, 1))
  1349. goto rnr_nak;
  1350. goto send_last_imm;
  1351. case OP(SEND_ONLY):
  1352. case OP(SEND_ONLY_WITH_IMMEDIATE):
  1353. if (!ipath_get_rwqe(qp, 0))
  1354. goto rnr_nak;
  1355. qp->r_rcv_len = 0;
  1356. if (opcode == OP(SEND_ONLY))
  1357. goto send_last;
  1358. /* FALLTHROUGH */
  1359. case OP(SEND_LAST_WITH_IMMEDIATE):
  1360. send_last_imm:
  1361. if (header_in_data) {
  1362. wc.imm_data = *(__be32 *) data;
  1363. data += sizeof(__be32);
  1364. } else {
  1365. /* Immediate data comes after BTH */
  1366. wc.imm_data = ohdr->u.imm_data;
  1367. }
  1368. hdrsize += 4;
  1369. wc.wc_flags = IB_WC_WITH_IMM;
  1370. /* FALLTHROUGH */
  1371. case OP(SEND_LAST):
  1372. case OP(RDMA_WRITE_LAST):
  1373. send_last:
  1374. /* Get the number of bytes the message was padded by. */
  1375. pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
  1376. /* Check for invalid length. */
  1377. /* XXX LAST len should be >= 1 */
  1378. if (unlikely(tlen < (hdrsize + pad + 4)))
  1379. goto nack_inv;
  1380. /* Don't count the CRC. */
  1381. tlen -= (hdrsize + pad + 4);
  1382. wc.byte_len = tlen + qp->r_rcv_len;
  1383. if (unlikely(wc.byte_len > qp->r_len))
  1384. goto nack_inv;
  1385. ipath_copy_sge(&qp->r_sge, data, tlen);
  1386. qp->r_msn++;
  1387. if (opcode == OP(RDMA_WRITE_LAST) ||
  1388. opcode == OP(RDMA_WRITE_ONLY))
  1389. break;
  1390. wc.wr_id = qp->r_wr_id;
  1391. wc.status = IB_WC_SUCCESS;
  1392. wc.opcode = IB_WC_RECV;
  1393. wc.vendor_err = 0;
  1394. wc.qp_num = qp->ibqp.qp_num;
  1395. wc.src_qp = qp->remote_qpn;
  1396. wc.pkey_index = 0;
  1397. wc.slid = qp->remote_ah_attr.dlid;
  1398. wc.sl = qp->remote_ah_attr.sl;
  1399. wc.dlid_path_bits = 0;
  1400. wc.port_num = 0;
  1401. /* Signal completion event if the solicited bit is set. */
  1402. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
  1403. (ohdr->bth[0] &
  1404. __constant_cpu_to_be32(1 << 23)) != 0);
  1405. break;
  1406. case OP(RDMA_WRITE_FIRST):
  1407. case OP(RDMA_WRITE_ONLY):
  1408. case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
  1409. /* consume RWQE */
  1410. /* RETH comes after BTH */
  1411. if (!header_in_data)
  1412. reth = &ohdr->u.rc.reth;
  1413. else {
  1414. reth = (struct ib_reth *)data;
  1415. data += sizeof(*reth);
  1416. }
  1417. hdrsize += sizeof(*reth);
  1418. qp->r_len = be32_to_cpu(reth->length);
  1419. qp->r_rcv_len = 0;
  1420. if (qp->r_len != 0) {
  1421. u32 rkey = be32_to_cpu(reth->rkey);
  1422. u64 vaddr = be64_to_cpu(reth->vaddr);
  1423. int ok;
  1424. /* Check rkey & NAK */
  1425. ok = ipath_rkey_ok(dev, &qp->r_sge,
  1426. qp->r_len, vaddr, rkey,
  1427. IB_ACCESS_REMOTE_WRITE);
  1428. if (unlikely(!ok))
  1429. goto nack_acc;
  1430. } else {
  1431. qp->r_sge.sg_list = NULL;
  1432. qp->r_sge.sge.mr = NULL;
  1433. qp->r_sge.sge.vaddr = NULL;
  1434. qp->r_sge.sge.length = 0;
  1435. qp->r_sge.sge.sge_length = 0;
  1436. }
  1437. if (unlikely(!(qp->qp_access_flags &
  1438. IB_ACCESS_REMOTE_WRITE)))
  1439. goto nack_acc;
  1440. if (opcode == OP(RDMA_WRITE_FIRST))
  1441. goto send_middle;
  1442. else if (opcode == OP(RDMA_WRITE_ONLY))
  1443. goto send_last;
  1444. if (!ipath_get_rwqe(qp, 1))
  1445. goto rnr_nak;
  1446. goto send_last_imm;
  1447. case OP(RDMA_READ_REQUEST):
  1448. /* RETH comes after BTH */
  1449. if (!header_in_data)
  1450. reth = &ohdr->u.rc.reth;
  1451. else {
  1452. reth = (struct ib_reth *)data;
  1453. data += sizeof(*reth);
  1454. }
  1455. if (unlikely(!(qp->qp_access_flags &
  1456. IB_ACCESS_REMOTE_READ)))
  1457. goto nack_acc;
  1458. spin_lock_irq(&qp->s_lock);
  1459. qp->s_rdma_len = be32_to_cpu(reth->length);
  1460. if (qp->s_rdma_len != 0) {
  1461. u32 rkey = be32_to_cpu(reth->rkey);
  1462. u64 vaddr = be64_to_cpu(reth->vaddr);
  1463. int ok;
  1464. /* Check rkey & NAK */
  1465. ok = ipath_rkey_ok(dev, &qp->s_rdma_sge,
  1466. qp->s_rdma_len, vaddr, rkey,
  1467. IB_ACCESS_REMOTE_READ);
  1468. if (unlikely(!ok)) {
  1469. spin_unlock_irq(&qp->s_lock);
  1470. goto nack_acc;
  1471. }
  1472. /*
  1473. * Update the next expected PSN. We add 1 later
  1474. * below, so only add the remainder here.
  1475. */
  1476. if (qp->s_rdma_len > pmtu)
  1477. qp->r_psn += (qp->s_rdma_len - 1) / pmtu;
  1478. } else {
  1479. qp->s_rdma_sge.sg_list = NULL;
  1480. qp->s_rdma_sge.num_sge = 0;
  1481. qp->s_rdma_sge.sge.mr = NULL;
  1482. qp->s_rdma_sge.sge.vaddr = NULL;
  1483. qp->s_rdma_sge.sge.length = 0;
  1484. qp->s_rdma_sge.sge.sge_length = 0;
  1485. }
  1486. /*
  1487. * We need to increment the MSN here instead of when we
  1488. * finish sending the result since a duplicate request would
  1489. * increment it more than once.
  1490. */
  1491. qp->r_msn++;
  1492. qp->s_ack_state = opcode;
  1493. qp->s_ack_psn = psn;
  1494. spin_unlock_irq(&qp->s_lock);
  1495. qp->r_psn++;
  1496. qp->r_state = opcode;
  1497. qp->r_nak_state = 0;
  1498. /* Call ipath_do_rc_send() in another thread. */
  1499. tasklet_hi_schedule(&qp->s_task);
  1500. goto done;
  1501. case OP(COMPARE_SWAP):
  1502. case OP(FETCH_ADD): {
  1503. struct ib_atomic_eth *ateth;
  1504. u64 vaddr;
  1505. u64 sdata;
  1506. u32 rkey;
  1507. if (!header_in_data)
  1508. ateth = &ohdr->u.atomic_eth;
  1509. else {
  1510. ateth = (struct ib_atomic_eth *)data;
  1511. data += sizeof(*ateth);
  1512. }
  1513. vaddr = be64_to_cpu(ateth->vaddr);
  1514. if (unlikely(vaddr & (sizeof(u64) - 1)))
  1515. goto nack_inv;
  1516. rkey = be32_to_cpu(ateth->rkey);
  1517. /* Check rkey & NAK */
  1518. if (unlikely(!ipath_rkey_ok(dev, &qp->r_sge,
  1519. sizeof(u64), vaddr, rkey,
  1520. IB_ACCESS_REMOTE_ATOMIC)))
  1521. goto nack_acc;
  1522. if (unlikely(!(qp->qp_access_flags &
  1523. IB_ACCESS_REMOTE_ATOMIC)))
  1524. goto nack_acc;
  1525. /* Perform atomic OP and save result. */
  1526. sdata = be64_to_cpu(ateth->swap_data);
  1527. spin_lock_irq(&dev->pending_lock);
  1528. qp->r_atomic_data = *(u64 *) qp->r_sge.sge.vaddr;
  1529. if (opcode == OP(FETCH_ADD))
  1530. *(u64 *) qp->r_sge.sge.vaddr =
  1531. qp->r_atomic_data + sdata;
  1532. else if (qp->r_atomic_data ==
  1533. be64_to_cpu(ateth->compare_data))
  1534. *(u64 *) qp->r_sge.sge.vaddr = sdata;
  1535. spin_unlock_irq(&dev->pending_lock);
  1536. qp->r_msn++;
  1537. qp->r_atomic_psn = psn & IPATH_PSN_MASK;
  1538. psn |= 1 << 31;
  1539. break;
  1540. }
  1541. default:
  1542. /* Drop packet for unknown opcodes. */
  1543. goto done;
  1544. }
  1545. qp->r_psn++;
  1546. qp->r_state = opcode;
  1547. qp->r_nak_state = 0;
  1548. /* Send an ACK if requested or required. */
  1549. if (psn & (1 << 31)) {
  1550. /*
  1551. * Coalesce ACKs unless there is a RDMA READ or
  1552. * ATOMIC pending.
  1553. */
  1554. if (qp->r_ack_state < OP(COMPARE_SWAP)) {
  1555. qp->r_ack_state = opcode;
  1556. qp->r_ack_psn = psn;
  1557. }
  1558. goto send_ack;
  1559. }
  1560. goto done;
  1561. nack_acc:
  1562. /*
  1563. * A NAK will ACK earlier sends and RDMA writes.
  1564. * Don't queue the NAK if a RDMA read, atomic, or NAK
  1565. * is pending though.
  1566. */
  1567. if (qp->r_ack_state < OP(COMPARE_SWAP)) {
  1568. /* XXX Flush WQEs */
  1569. qp->state = IB_QPS_ERR;
  1570. qp->r_ack_state = OP(RDMA_WRITE_ONLY);
  1571. qp->r_nak_state = IB_NAK_REMOTE_ACCESS_ERROR;
  1572. qp->r_ack_psn = qp->r_psn;
  1573. }
  1574. send_ack:
  1575. /* Send ACK right away unless the send tasklet has a pending ACK. */
  1576. if (qp->s_ack_state == OP(ACKNOWLEDGE))
  1577. send_rc_ack(qp);
  1578. done:
  1579. return;
  1580. }