ipath_rc.c 52 KB

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