ipath_rc.c 46 KB

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