ipath_rc.c 49 KB

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