ipath_rc.c 51 KB

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