ipath_rc.c 51 KB

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