ipath_rc.c 49 KB

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