iwch_qp.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /*
  2. * Copyright (c) 2006 Chelsio, 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 <linux/sched.h>
  33. #include "iwch_provider.h"
  34. #include "iwch.h"
  35. #include "iwch_cm.h"
  36. #include "cxio_hal.h"
  37. #include "cxio_resource.h"
  38. #define NO_SUPPORT -1
  39. static int build_rdma_send(union t3_wr *wqe, struct ib_send_wr *wr,
  40. u8 * flit_cnt)
  41. {
  42. int i;
  43. u32 plen;
  44. switch (wr->opcode) {
  45. case IB_WR_SEND:
  46. if (wr->send_flags & IB_SEND_SOLICITED)
  47. wqe->send.rdmaop = T3_SEND_WITH_SE;
  48. else
  49. wqe->send.rdmaop = T3_SEND;
  50. wqe->send.rem_stag = 0;
  51. break;
  52. case IB_WR_SEND_WITH_INV:
  53. if (wr->send_flags & IB_SEND_SOLICITED)
  54. wqe->send.rdmaop = T3_SEND_WITH_SE_INV;
  55. else
  56. wqe->send.rdmaop = T3_SEND_WITH_INV;
  57. wqe->send.rem_stag = cpu_to_be32(wr->ex.invalidate_rkey);
  58. break;
  59. default:
  60. return -EINVAL;
  61. }
  62. if (wr->num_sge > T3_MAX_SGE)
  63. return -EINVAL;
  64. wqe->send.reserved[0] = 0;
  65. wqe->send.reserved[1] = 0;
  66. wqe->send.reserved[2] = 0;
  67. plen = 0;
  68. for (i = 0; i < wr->num_sge; i++) {
  69. if ((plen + wr->sg_list[i].length) < plen)
  70. return -EMSGSIZE;
  71. plen += wr->sg_list[i].length;
  72. wqe->send.sgl[i].stag = cpu_to_be32(wr->sg_list[i].lkey);
  73. wqe->send.sgl[i].len = cpu_to_be32(wr->sg_list[i].length);
  74. wqe->send.sgl[i].to = cpu_to_be64(wr->sg_list[i].addr);
  75. }
  76. wqe->send.num_sgle = cpu_to_be32(wr->num_sge);
  77. *flit_cnt = 4 + ((wr->num_sge) << 1);
  78. wqe->send.plen = cpu_to_be32(plen);
  79. return 0;
  80. }
  81. static int build_rdma_write(union t3_wr *wqe, struct ib_send_wr *wr,
  82. u8 *flit_cnt)
  83. {
  84. int i;
  85. u32 plen;
  86. if (wr->num_sge > T3_MAX_SGE)
  87. return -EINVAL;
  88. wqe->write.rdmaop = T3_RDMA_WRITE;
  89. wqe->write.reserved[0] = 0;
  90. wqe->write.reserved[1] = 0;
  91. wqe->write.reserved[2] = 0;
  92. wqe->write.stag_sink = cpu_to_be32(wr->wr.rdma.rkey);
  93. wqe->write.to_sink = cpu_to_be64(wr->wr.rdma.remote_addr);
  94. if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
  95. plen = 4;
  96. wqe->write.sgl[0].stag = wr->ex.imm_data;
  97. wqe->write.sgl[0].len = cpu_to_be32(0);
  98. wqe->write.num_sgle = cpu_to_be32(0);
  99. *flit_cnt = 6;
  100. } else {
  101. plen = 0;
  102. for (i = 0; i < wr->num_sge; i++) {
  103. if ((plen + wr->sg_list[i].length) < plen) {
  104. return -EMSGSIZE;
  105. }
  106. plen += wr->sg_list[i].length;
  107. wqe->write.sgl[i].stag =
  108. cpu_to_be32(wr->sg_list[i].lkey);
  109. wqe->write.sgl[i].len =
  110. cpu_to_be32(wr->sg_list[i].length);
  111. wqe->write.sgl[i].to =
  112. cpu_to_be64(wr->sg_list[i].addr);
  113. }
  114. wqe->write.num_sgle = cpu_to_be32(wr->num_sge);
  115. *flit_cnt = 5 + ((wr->num_sge) << 1);
  116. }
  117. wqe->write.plen = cpu_to_be32(plen);
  118. return 0;
  119. }
  120. static int build_rdma_read(union t3_wr *wqe, struct ib_send_wr *wr,
  121. u8 *flit_cnt)
  122. {
  123. if (wr->num_sge > 1)
  124. return -EINVAL;
  125. wqe->read.rdmaop = T3_READ_REQ;
  126. if (wr->opcode == IB_WR_RDMA_READ_WITH_INV)
  127. wqe->read.local_inv = 1;
  128. else
  129. wqe->read.local_inv = 0;
  130. wqe->read.reserved[0] = 0;
  131. wqe->read.reserved[1] = 0;
  132. wqe->read.rem_stag = cpu_to_be32(wr->wr.rdma.rkey);
  133. wqe->read.rem_to = cpu_to_be64(wr->wr.rdma.remote_addr);
  134. wqe->read.local_stag = cpu_to_be32(wr->sg_list[0].lkey);
  135. wqe->read.local_len = cpu_to_be32(wr->sg_list[0].length);
  136. wqe->read.local_to = cpu_to_be64(wr->sg_list[0].addr);
  137. *flit_cnt = sizeof(struct t3_rdma_read_wr) >> 3;
  138. return 0;
  139. }
  140. static int build_fastreg(union t3_wr *wqe, struct ib_send_wr *wr,
  141. u8 *flit_cnt, int *wr_cnt, struct t3_wq *wq)
  142. {
  143. int i;
  144. __be64 *p;
  145. if (wr->wr.fast_reg.page_list_len > T3_MAX_FASTREG_DEPTH)
  146. return -EINVAL;
  147. *wr_cnt = 1;
  148. wqe->fastreg.stag = cpu_to_be32(wr->wr.fast_reg.rkey);
  149. wqe->fastreg.len = cpu_to_be32(wr->wr.fast_reg.length);
  150. wqe->fastreg.va_base_hi = cpu_to_be32(wr->wr.fast_reg.iova_start >> 32);
  151. wqe->fastreg.va_base_lo_fbo =
  152. cpu_to_be32(wr->wr.fast_reg.iova_start & 0xffffffff);
  153. wqe->fastreg.page_type_perms = cpu_to_be32(
  154. V_FR_PAGE_COUNT(wr->wr.fast_reg.page_list_len) |
  155. V_FR_PAGE_SIZE(wr->wr.fast_reg.page_shift-12) |
  156. V_FR_TYPE(TPT_VATO) |
  157. V_FR_PERMS(iwch_ib_to_tpt_access(wr->wr.fast_reg.access_flags)));
  158. p = &wqe->fastreg.pbl_addrs[0];
  159. for (i = 0; i < wr->wr.fast_reg.page_list_len; i++, p++) {
  160. /* If we need a 2nd WR, then set it up */
  161. if (i == T3_MAX_FASTREG_FRAG) {
  162. *wr_cnt = 2;
  163. wqe = (union t3_wr *)(wq->queue +
  164. Q_PTR2IDX((wq->wptr+1), wq->size_log2));
  165. build_fw_riwrh((void *)wqe, T3_WR_FASTREG, 0,
  166. Q_GENBIT(wq->wptr + 1, wq->size_log2),
  167. 0, 1 + wr->wr.fast_reg.page_list_len - T3_MAX_FASTREG_FRAG,
  168. T3_EOP);
  169. p = &wqe->pbl_frag.pbl_addrs[0];
  170. }
  171. *p = cpu_to_be64((u64)wr->wr.fast_reg.page_list->page_list[i]);
  172. }
  173. *flit_cnt = 5 + wr->wr.fast_reg.page_list_len;
  174. if (*flit_cnt > 15)
  175. *flit_cnt = 15;
  176. return 0;
  177. }
  178. static int build_inv_stag(union t3_wr *wqe, struct ib_send_wr *wr,
  179. u8 *flit_cnt)
  180. {
  181. wqe->local_inv.stag = cpu_to_be32(wr->ex.invalidate_rkey);
  182. wqe->local_inv.reserved = 0;
  183. *flit_cnt = sizeof(struct t3_local_inv_wr) >> 3;
  184. return 0;
  185. }
  186. static int iwch_sgl2pbl_map(struct iwch_dev *rhp, struct ib_sge *sg_list,
  187. u32 num_sgle, u32 * pbl_addr, u8 * page_size)
  188. {
  189. int i;
  190. struct iwch_mr *mhp;
  191. u64 offset;
  192. for (i = 0; i < num_sgle; i++) {
  193. mhp = get_mhp(rhp, (sg_list[i].lkey) >> 8);
  194. if (!mhp) {
  195. PDBG("%s %d\n", __func__, __LINE__);
  196. return -EIO;
  197. }
  198. if (!mhp->attr.state) {
  199. PDBG("%s %d\n", __func__, __LINE__);
  200. return -EIO;
  201. }
  202. if (mhp->attr.zbva) {
  203. PDBG("%s %d\n", __func__, __LINE__);
  204. return -EIO;
  205. }
  206. if (sg_list[i].addr < mhp->attr.va_fbo) {
  207. PDBG("%s %d\n", __func__, __LINE__);
  208. return -EINVAL;
  209. }
  210. if (sg_list[i].addr + ((u64) sg_list[i].length) <
  211. sg_list[i].addr) {
  212. PDBG("%s %d\n", __func__, __LINE__);
  213. return -EINVAL;
  214. }
  215. if (sg_list[i].addr + ((u64) sg_list[i].length) >
  216. mhp->attr.va_fbo + ((u64) mhp->attr.len)) {
  217. PDBG("%s %d\n", __func__, __LINE__);
  218. return -EINVAL;
  219. }
  220. offset = sg_list[i].addr - mhp->attr.va_fbo;
  221. offset += mhp->attr.va_fbo &
  222. ((1UL << (12 + mhp->attr.page_size)) - 1);
  223. pbl_addr[i] = ((mhp->attr.pbl_addr -
  224. rhp->rdev.rnic_info.pbl_base) >> 3) +
  225. (offset >> (12 + mhp->attr.page_size));
  226. page_size[i] = mhp->attr.page_size;
  227. }
  228. return 0;
  229. }
  230. static int build_rdma_recv(struct iwch_qp *qhp, union t3_wr *wqe,
  231. struct ib_recv_wr *wr)
  232. {
  233. int i, err = 0;
  234. u32 pbl_addr[T3_MAX_SGE];
  235. u8 page_size[T3_MAX_SGE];
  236. err = iwch_sgl2pbl_map(qhp->rhp, wr->sg_list, wr->num_sge, pbl_addr,
  237. page_size);
  238. if (err)
  239. return err;
  240. wqe->recv.pagesz[0] = page_size[0];
  241. wqe->recv.pagesz[1] = page_size[1];
  242. wqe->recv.pagesz[2] = page_size[2];
  243. wqe->recv.pagesz[3] = page_size[3];
  244. wqe->recv.num_sgle = cpu_to_be32(wr->num_sge);
  245. for (i = 0; i < wr->num_sge; i++) {
  246. wqe->recv.sgl[i].stag = cpu_to_be32(wr->sg_list[i].lkey);
  247. wqe->recv.sgl[i].len = cpu_to_be32(wr->sg_list[i].length);
  248. /* to in the WQE == the offset into the page */
  249. wqe->recv.sgl[i].to = cpu_to_be64(((u32)wr->sg_list[i].addr) &
  250. ((1UL << (12 + page_size[i])) - 1));
  251. /* pbl_addr is the adapters address in the PBL */
  252. wqe->recv.pbl_addr[i] = cpu_to_be32(pbl_addr[i]);
  253. }
  254. for (; i < T3_MAX_SGE; i++) {
  255. wqe->recv.sgl[i].stag = 0;
  256. wqe->recv.sgl[i].len = 0;
  257. wqe->recv.sgl[i].to = 0;
  258. wqe->recv.pbl_addr[i] = 0;
  259. }
  260. qhp->wq.rq[Q_PTR2IDX(qhp->wq.rq_wptr,
  261. qhp->wq.rq_size_log2)].wr_id = wr->wr_id;
  262. qhp->wq.rq[Q_PTR2IDX(qhp->wq.rq_wptr,
  263. qhp->wq.rq_size_log2)].pbl_addr = 0;
  264. return 0;
  265. }
  266. static int build_zero_stag_recv(struct iwch_qp *qhp, union t3_wr *wqe,
  267. struct ib_recv_wr *wr)
  268. {
  269. int i;
  270. u32 pbl_addr;
  271. u32 pbl_offset;
  272. /*
  273. * The T3 HW requires the PBL in the HW recv descriptor to reference
  274. * a PBL entry. So we allocate the max needed PBL memory here and pass
  275. * it to the uP in the recv WR. The uP will build the PBL and setup
  276. * the HW recv descriptor.
  277. */
  278. pbl_addr = cxio_hal_pblpool_alloc(&qhp->rhp->rdev, T3_STAG0_PBL_SIZE);
  279. if (!pbl_addr)
  280. return -ENOMEM;
  281. /*
  282. * Compute the 8B aligned offset.
  283. */
  284. pbl_offset = (pbl_addr - qhp->rhp->rdev.rnic_info.pbl_base) >> 3;
  285. wqe->recv.num_sgle = cpu_to_be32(wr->num_sge);
  286. for (i = 0; i < wr->num_sge; i++) {
  287. /*
  288. * Use a 128MB page size. This and an imposed 128MB
  289. * sge length limit allows us to require only a 2-entry HW
  290. * PBL for each SGE. This restriction is acceptable since
  291. * since it is not possible to allocate 128MB of contiguous
  292. * DMA coherent memory!
  293. */
  294. if (wr->sg_list[i].length > T3_STAG0_MAX_PBE_LEN)
  295. return -EINVAL;
  296. wqe->recv.pagesz[i] = T3_STAG0_PAGE_SHIFT;
  297. /*
  298. * T3 restricts a recv to all zero-stag or all non-zero-stag.
  299. */
  300. if (wr->sg_list[i].lkey != 0)
  301. return -EINVAL;
  302. wqe->recv.sgl[i].stag = 0;
  303. wqe->recv.sgl[i].len = cpu_to_be32(wr->sg_list[i].length);
  304. wqe->recv.sgl[i].to = cpu_to_be64(wr->sg_list[i].addr);
  305. wqe->recv.pbl_addr[i] = cpu_to_be32(pbl_offset);
  306. pbl_offset += 2;
  307. }
  308. for (; i < T3_MAX_SGE; i++) {
  309. wqe->recv.pagesz[i] = 0;
  310. wqe->recv.sgl[i].stag = 0;
  311. wqe->recv.sgl[i].len = 0;
  312. wqe->recv.sgl[i].to = 0;
  313. wqe->recv.pbl_addr[i] = 0;
  314. }
  315. qhp->wq.rq[Q_PTR2IDX(qhp->wq.rq_wptr,
  316. qhp->wq.rq_size_log2)].wr_id = wr->wr_id;
  317. qhp->wq.rq[Q_PTR2IDX(qhp->wq.rq_wptr,
  318. qhp->wq.rq_size_log2)].pbl_addr = pbl_addr;
  319. return 0;
  320. }
  321. int iwch_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  322. struct ib_send_wr **bad_wr)
  323. {
  324. int err = 0;
  325. u8 uninitialized_var(t3_wr_flit_cnt);
  326. enum t3_wr_opcode t3_wr_opcode = 0;
  327. enum t3_wr_flags t3_wr_flags;
  328. struct iwch_qp *qhp;
  329. u32 idx;
  330. union t3_wr *wqe;
  331. u32 num_wrs;
  332. unsigned long flag;
  333. struct t3_swsq *sqp;
  334. int wr_cnt = 1;
  335. qhp = to_iwch_qp(ibqp);
  336. spin_lock_irqsave(&qhp->lock, flag);
  337. if (qhp->attr.state > IWCH_QP_STATE_RTS) {
  338. spin_unlock_irqrestore(&qhp->lock, flag);
  339. return -EINVAL;
  340. }
  341. num_wrs = Q_FREECNT(qhp->wq.sq_rptr, qhp->wq.sq_wptr,
  342. qhp->wq.sq_size_log2);
  343. if (num_wrs <= 0) {
  344. spin_unlock_irqrestore(&qhp->lock, flag);
  345. return -ENOMEM;
  346. }
  347. while (wr) {
  348. if (num_wrs == 0) {
  349. err = -ENOMEM;
  350. *bad_wr = wr;
  351. break;
  352. }
  353. idx = Q_PTR2IDX(qhp->wq.wptr, qhp->wq.size_log2);
  354. wqe = (union t3_wr *) (qhp->wq.queue + idx);
  355. t3_wr_flags = 0;
  356. if (wr->send_flags & IB_SEND_SOLICITED)
  357. t3_wr_flags |= T3_SOLICITED_EVENT_FLAG;
  358. if (wr->send_flags & IB_SEND_SIGNALED)
  359. t3_wr_flags |= T3_COMPLETION_FLAG;
  360. sqp = qhp->wq.sq +
  361. Q_PTR2IDX(qhp->wq.sq_wptr, qhp->wq.sq_size_log2);
  362. switch (wr->opcode) {
  363. case IB_WR_SEND:
  364. case IB_WR_SEND_WITH_INV:
  365. if (wr->send_flags & IB_SEND_FENCE)
  366. t3_wr_flags |= T3_READ_FENCE_FLAG;
  367. t3_wr_opcode = T3_WR_SEND;
  368. err = build_rdma_send(wqe, wr, &t3_wr_flit_cnt);
  369. break;
  370. case IB_WR_RDMA_WRITE:
  371. case IB_WR_RDMA_WRITE_WITH_IMM:
  372. t3_wr_opcode = T3_WR_WRITE;
  373. err = build_rdma_write(wqe, wr, &t3_wr_flit_cnt);
  374. break;
  375. case IB_WR_RDMA_READ:
  376. case IB_WR_RDMA_READ_WITH_INV:
  377. t3_wr_opcode = T3_WR_READ;
  378. t3_wr_flags = 0; /* T3 reads are always signaled */
  379. err = build_rdma_read(wqe, wr, &t3_wr_flit_cnt);
  380. if (err)
  381. break;
  382. sqp->read_len = wqe->read.local_len;
  383. if (!qhp->wq.oldest_read)
  384. qhp->wq.oldest_read = sqp;
  385. break;
  386. case IB_WR_FAST_REG_MR:
  387. t3_wr_opcode = T3_WR_FASTREG;
  388. err = build_fastreg(wqe, wr, &t3_wr_flit_cnt,
  389. &wr_cnt, &qhp->wq);
  390. break;
  391. case IB_WR_LOCAL_INV:
  392. if (wr->send_flags & IB_SEND_FENCE)
  393. t3_wr_flags |= T3_LOCAL_FENCE_FLAG;
  394. t3_wr_opcode = T3_WR_INV_STAG;
  395. err = build_inv_stag(wqe, wr, &t3_wr_flit_cnt);
  396. break;
  397. default:
  398. PDBG("%s post of type=%d TBD!\n", __func__,
  399. wr->opcode);
  400. err = -EINVAL;
  401. }
  402. if (err) {
  403. *bad_wr = wr;
  404. break;
  405. }
  406. wqe->send.wrid.id0.hi = qhp->wq.sq_wptr;
  407. sqp->wr_id = wr->wr_id;
  408. sqp->opcode = wr2opcode(t3_wr_opcode);
  409. sqp->sq_wptr = qhp->wq.sq_wptr;
  410. sqp->complete = 0;
  411. sqp->signaled = (wr->send_flags & IB_SEND_SIGNALED);
  412. build_fw_riwrh((void *) wqe, t3_wr_opcode, t3_wr_flags,
  413. Q_GENBIT(qhp->wq.wptr, qhp->wq.size_log2),
  414. 0, t3_wr_flit_cnt,
  415. (wr_cnt == 1) ? T3_SOPEOP : T3_SOP);
  416. PDBG("%s cookie 0x%llx wq idx 0x%x swsq idx %ld opcode %d\n",
  417. __func__, (unsigned long long) wr->wr_id, idx,
  418. Q_PTR2IDX(qhp->wq.sq_wptr, qhp->wq.sq_size_log2),
  419. sqp->opcode);
  420. wr = wr->next;
  421. num_wrs--;
  422. qhp->wq.wptr += wr_cnt;
  423. ++(qhp->wq.sq_wptr);
  424. }
  425. spin_unlock_irqrestore(&qhp->lock, flag);
  426. ring_doorbell(qhp->wq.doorbell, qhp->wq.qpid);
  427. return err;
  428. }
  429. int iwch_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  430. struct ib_recv_wr **bad_wr)
  431. {
  432. int err = 0;
  433. struct iwch_qp *qhp;
  434. u32 idx;
  435. union t3_wr *wqe;
  436. u32 num_wrs;
  437. unsigned long flag;
  438. qhp = to_iwch_qp(ibqp);
  439. spin_lock_irqsave(&qhp->lock, flag);
  440. if (qhp->attr.state > IWCH_QP_STATE_RTS) {
  441. spin_unlock_irqrestore(&qhp->lock, flag);
  442. return -EINVAL;
  443. }
  444. num_wrs = Q_FREECNT(qhp->wq.rq_rptr, qhp->wq.rq_wptr,
  445. qhp->wq.rq_size_log2) - 1;
  446. if (!wr) {
  447. spin_unlock_irqrestore(&qhp->lock, flag);
  448. return -EINVAL;
  449. }
  450. while (wr) {
  451. if (wr->num_sge > T3_MAX_SGE) {
  452. err = -EINVAL;
  453. *bad_wr = wr;
  454. break;
  455. }
  456. idx = Q_PTR2IDX(qhp->wq.wptr, qhp->wq.size_log2);
  457. wqe = (union t3_wr *) (qhp->wq.queue + idx);
  458. if (num_wrs)
  459. if (wr->sg_list[0].lkey)
  460. err = build_rdma_recv(qhp, wqe, wr);
  461. else
  462. err = build_zero_stag_recv(qhp, wqe, wr);
  463. else
  464. err = -ENOMEM;
  465. if (err) {
  466. *bad_wr = wr;
  467. break;
  468. }
  469. build_fw_riwrh((void *) wqe, T3_WR_RCV, T3_COMPLETION_FLAG,
  470. Q_GENBIT(qhp->wq.wptr, qhp->wq.size_log2),
  471. 0, sizeof(struct t3_receive_wr) >> 3, T3_SOPEOP);
  472. PDBG("%s cookie 0x%llx idx 0x%x rq_wptr 0x%x rw_rptr 0x%x "
  473. "wqe %p \n", __func__, (unsigned long long) wr->wr_id,
  474. idx, qhp->wq.rq_wptr, qhp->wq.rq_rptr, wqe);
  475. ++(qhp->wq.rq_wptr);
  476. ++(qhp->wq.wptr);
  477. wr = wr->next;
  478. num_wrs--;
  479. }
  480. spin_unlock_irqrestore(&qhp->lock, flag);
  481. ring_doorbell(qhp->wq.doorbell, qhp->wq.qpid);
  482. return err;
  483. }
  484. int iwch_bind_mw(struct ib_qp *qp,
  485. struct ib_mw *mw,
  486. struct ib_mw_bind *mw_bind)
  487. {
  488. struct iwch_dev *rhp;
  489. struct iwch_mw *mhp;
  490. struct iwch_qp *qhp;
  491. union t3_wr *wqe;
  492. u32 pbl_addr;
  493. u8 page_size;
  494. u32 num_wrs;
  495. unsigned long flag;
  496. struct ib_sge sgl;
  497. int err=0;
  498. enum t3_wr_flags t3_wr_flags;
  499. u32 idx;
  500. struct t3_swsq *sqp;
  501. qhp = to_iwch_qp(qp);
  502. mhp = to_iwch_mw(mw);
  503. rhp = qhp->rhp;
  504. spin_lock_irqsave(&qhp->lock, flag);
  505. if (qhp->attr.state > IWCH_QP_STATE_RTS) {
  506. spin_unlock_irqrestore(&qhp->lock, flag);
  507. return -EINVAL;
  508. }
  509. num_wrs = Q_FREECNT(qhp->wq.sq_rptr, qhp->wq.sq_wptr,
  510. qhp->wq.sq_size_log2);
  511. if ((num_wrs) <= 0) {
  512. spin_unlock_irqrestore(&qhp->lock, flag);
  513. return -ENOMEM;
  514. }
  515. idx = Q_PTR2IDX(qhp->wq.wptr, qhp->wq.size_log2);
  516. PDBG("%s: idx 0x%0x, mw 0x%p, mw_bind 0x%p\n", __func__, idx,
  517. mw, mw_bind);
  518. wqe = (union t3_wr *) (qhp->wq.queue + idx);
  519. t3_wr_flags = 0;
  520. if (mw_bind->send_flags & IB_SEND_SIGNALED)
  521. t3_wr_flags = T3_COMPLETION_FLAG;
  522. sgl.addr = mw_bind->addr;
  523. sgl.lkey = mw_bind->mr->lkey;
  524. sgl.length = mw_bind->length;
  525. wqe->bind.reserved = 0;
  526. wqe->bind.type = TPT_VATO;
  527. /* TBD: check perms */
  528. wqe->bind.perms = iwch_ib_to_tpt_bind_access(mw_bind->mw_access_flags);
  529. wqe->bind.mr_stag = cpu_to_be32(mw_bind->mr->lkey);
  530. wqe->bind.mw_stag = cpu_to_be32(mw->rkey);
  531. wqe->bind.mw_len = cpu_to_be32(mw_bind->length);
  532. wqe->bind.mw_va = cpu_to_be64(mw_bind->addr);
  533. err = iwch_sgl2pbl_map(rhp, &sgl, 1, &pbl_addr, &page_size);
  534. if (err) {
  535. spin_unlock_irqrestore(&qhp->lock, flag);
  536. return err;
  537. }
  538. wqe->send.wrid.id0.hi = qhp->wq.sq_wptr;
  539. sqp = qhp->wq.sq + Q_PTR2IDX(qhp->wq.sq_wptr, qhp->wq.sq_size_log2);
  540. sqp->wr_id = mw_bind->wr_id;
  541. sqp->opcode = T3_BIND_MW;
  542. sqp->sq_wptr = qhp->wq.sq_wptr;
  543. sqp->complete = 0;
  544. sqp->signaled = (mw_bind->send_flags & IB_SEND_SIGNALED);
  545. wqe->bind.mr_pbl_addr = cpu_to_be32(pbl_addr);
  546. wqe->bind.mr_pagesz = page_size;
  547. build_fw_riwrh((void *)wqe, T3_WR_BIND, t3_wr_flags,
  548. Q_GENBIT(qhp->wq.wptr, qhp->wq.size_log2), 0,
  549. sizeof(struct t3_bind_mw_wr) >> 3, T3_SOPEOP);
  550. ++(qhp->wq.wptr);
  551. ++(qhp->wq.sq_wptr);
  552. spin_unlock_irqrestore(&qhp->lock, flag);
  553. ring_doorbell(qhp->wq.doorbell, qhp->wq.qpid);
  554. return err;
  555. }
  556. static inline void build_term_codes(struct respQ_msg_t *rsp_msg,
  557. u8 *layer_type, u8 *ecode)
  558. {
  559. int status = TPT_ERR_INTERNAL_ERR;
  560. int tagged = 0;
  561. int opcode = -1;
  562. int rqtype = 0;
  563. int send_inv = 0;
  564. if (rsp_msg) {
  565. status = CQE_STATUS(rsp_msg->cqe);
  566. opcode = CQE_OPCODE(rsp_msg->cqe);
  567. rqtype = RQ_TYPE(rsp_msg->cqe);
  568. send_inv = (opcode == T3_SEND_WITH_INV) ||
  569. (opcode == T3_SEND_WITH_SE_INV);
  570. tagged = (opcode == T3_RDMA_WRITE) ||
  571. (rqtype && (opcode == T3_READ_RESP));
  572. }
  573. switch (status) {
  574. case TPT_ERR_STAG:
  575. if (send_inv) {
  576. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  577. *ecode = RDMAP_CANT_INV_STAG;
  578. } else {
  579. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  580. *ecode = RDMAP_INV_STAG;
  581. }
  582. break;
  583. case TPT_ERR_PDID:
  584. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  585. if ((opcode == T3_SEND_WITH_INV) ||
  586. (opcode == T3_SEND_WITH_SE_INV))
  587. *ecode = RDMAP_CANT_INV_STAG;
  588. else
  589. *ecode = RDMAP_STAG_NOT_ASSOC;
  590. break;
  591. case TPT_ERR_QPID:
  592. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  593. *ecode = RDMAP_STAG_NOT_ASSOC;
  594. break;
  595. case TPT_ERR_ACCESS:
  596. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  597. *ecode = RDMAP_ACC_VIOL;
  598. break;
  599. case TPT_ERR_WRAP:
  600. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  601. *ecode = RDMAP_TO_WRAP;
  602. break;
  603. case TPT_ERR_BOUND:
  604. if (tagged) {
  605. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  606. *ecode = DDPT_BASE_BOUNDS;
  607. } else {
  608. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  609. *ecode = RDMAP_BASE_BOUNDS;
  610. }
  611. break;
  612. case TPT_ERR_INVALIDATE_SHARED_MR:
  613. case TPT_ERR_INVALIDATE_MR_WITH_MW_BOUND:
  614. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  615. *ecode = RDMAP_CANT_INV_STAG;
  616. break;
  617. case TPT_ERR_ECC:
  618. case TPT_ERR_ECC_PSTAG:
  619. case TPT_ERR_INTERNAL_ERR:
  620. *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
  621. *ecode = 0;
  622. break;
  623. case TPT_ERR_OUT_OF_RQE:
  624. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  625. *ecode = DDPU_INV_MSN_NOBUF;
  626. break;
  627. case TPT_ERR_PBL_ADDR_BOUND:
  628. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  629. *ecode = DDPT_BASE_BOUNDS;
  630. break;
  631. case TPT_ERR_CRC:
  632. *layer_type = LAYER_MPA|DDP_LLP;
  633. *ecode = MPA_CRC_ERR;
  634. break;
  635. case TPT_ERR_MARKER:
  636. *layer_type = LAYER_MPA|DDP_LLP;
  637. *ecode = MPA_MARKER_ERR;
  638. break;
  639. case TPT_ERR_PDU_LEN_ERR:
  640. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  641. *ecode = DDPU_MSG_TOOBIG;
  642. break;
  643. case TPT_ERR_DDP_VERSION:
  644. if (tagged) {
  645. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  646. *ecode = DDPT_INV_VERS;
  647. } else {
  648. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  649. *ecode = DDPU_INV_VERS;
  650. }
  651. break;
  652. case TPT_ERR_RDMA_VERSION:
  653. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  654. *ecode = RDMAP_INV_VERS;
  655. break;
  656. case TPT_ERR_OPCODE:
  657. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  658. *ecode = RDMAP_INV_OPCODE;
  659. break;
  660. case TPT_ERR_DDP_QUEUE_NUM:
  661. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  662. *ecode = DDPU_INV_QN;
  663. break;
  664. case TPT_ERR_MSN:
  665. case TPT_ERR_MSN_GAP:
  666. case TPT_ERR_MSN_RANGE:
  667. case TPT_ERR_IRD_OVERFLOW:
  668. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  669. *ecode = DDPU_INV_MSN_RANGE;
  670. break;
  671. case TPT_ERR_TBIT:
  672. *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
  673. *ecode = 0;
  674. break;
  675. case TPT_ERR_MO:
  676. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  677. *ecode = DDPU_INV_MO;
  678. break;
  679. default:
  680. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  681. *ecode = 0;
  682. break;
  683. }
  684. }
  685. int iwch_post_zb_read(struct iwch_qp *qhp)
  686. {
  687. union t3_wr *wqe;
  688. struct sk_buff *skb;
  689. u8 flit_cnt = sizeof(struct t3_rdma_read_wr) >> 3;
  690. PDBG("%s enter\n", __func__);
  691. skb = alloc_skb(40, GFP_KERNEL);
  692. if (!skb) {
  693. printk(KERN_ERR "%s cannot send zb_read!!\n", __func__);
  694. return -ENOMEM;
  695. }
  696. wqe = (union t3_wr *)skb_put(skb, sizeof(struct t3_rdma_read_wr));
  697. memset(wqe, 0, sizeof(struct t3_rdma_read_wr));
  698. wqe->read.rdmaop = T3_READ_REQ;
  699. wqe->read.reserved[0] = 0;
  700. wqe->read.reserved[1] = 0;
  701. wqe->read.rem_stag = cpu_to_be32(1);
  702. wqe->read.rem_to = cpu_to_be64(1);
  703. wqe->read.local_stag = cpu_to_be32(1);
  704. wqe->read.local_len = cpu_to_be32(0);
  705. wqe->read.local_to = cpu_to_be64(1);
  706. wqe->send.wrh.op_seop_flags = cpu_to_be32(V_FW_RIWR_OP(T3_WR_READ));
  707. wqe->send.wrh.gen_tid_len = cpu_to_be32(V_FW_RIWR_TID(qhp->ep->hwtid)|
  708. V_FW_RIWR_LEN(flit_cnt));
  709. skb->priority = CPL_PRIORITY_DATA;
  710. return iwch_cxgb3_ofld_send(qhp->rhp->rdev.t3cdev_p, skb);
  711. }
  712. /*
  713. * This posts a TERMINATE with layer=RDMA, type=catastrophic.
  714. */
  715. int iwch_post_terminate(struct iwch_qp *qhp, struct respQ_msg_t *rsp_msg)
  716. {
  717. union t3_wr *wqe;
  718. struct terminate_message *term;
  719. struct sk_buff *skb;
  720. PDBG("%s %d\n", __func__, __LINE__);
  721. skb = alloc_skb(40, GFP_ATOMIC);
  722. if (!skb) {
  723. printk(KERN_ERR "%s cannot send TERMINATE!\n", __func__);
  724. return -ENOMEM;
  725. }
  726. wqe = (union t3_wr *)skb_put(skb, 40);
  727. memset(wqe, 0, 40);
  728. wqe->send.rdmaop = T3_TERMINATE;
  729. /* immediate data length */
  730. wqe->send.plen = htonl(4);
  731. /* immediate data starts here. */
  732. term = (struct terminate_message *)wqe->send.sgl;
  733. build_term_codes(rsp_msg, &term->layer_etype, &term->ecode);
  734. wqe->send.wrh.op_seop_flags = cpu_to_be32(V_FW_RIWR_OP(T3_WR_SEND) |
  735. V_FW_RIWR_FLAGS(T3_COMPLETION_FLAG | T3_NOTIFY_FLAG));
  736. wqe->send.wrh.gen_tid_len = cpu_to_be32(V_FW_RIWR_TID(qhp->ep->hwtid));
  737. skb->priority = CPL_PRIORITY_DATA;
  738. return iwch_cxgb3_ofld_send(qhp->rhp->rdev.t3cdev_p, skb);
  739. }
  740. /*
  741. * Assumes qhp lock is held.
  742. */
  743. static void __flush_qp(struct iwch_qp *qhp, unsigned long *flag)
  744. {
  745. struct iwch_cq *rchp, *schp;
  746. int count;
  747. int flushed;
  748. rchp = get_chp(qhp->rhp, qhp->attr.rcq);
  749. schp = get_chp(qhp->rhp, qhp->attr.scq);
  750. PDBG("%s qhp %p rchp %p schp %p\n", __func__, qhp, rchp, schp);
  751. /* take a ref on the qhp since we must release the lock */
  752. atomic_inc(&qhp->refcnt);
  753. spin_unlock_irqrestore(&qhp->lock, *flag);
  754. /* locking heirarchy: cq lock first, then qp lock. */
  755. spin_lock_irqsave(&rchp->lock, *flag);
  756. spin_lock(&qhp->lock);
  757. cxio_flush_hw_cq(&rchp->cq);
  758. cxio_count_rcqes(&rchp->cq, &qhp->wq, &count);
  759. flushed = cxio_flush_rq(&qhp->wq, &rchp->cq, count);
  760. spin_unlock(&qhp->lock);
  761. spin_unlock_irqrestore(&rchp->lock, *flag);
  762. if (flushed)
  763. (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
  764. /* locking heirarchy: cq lock first, then qp lock. */
  765. spin_lock_irqsave(&schp->lock, *flag);
  766. spin_lock(&qhp->lock);
  767. cxio_flush_hw_cq(&schp->cq);
  768. cxio_count_scqes(&schp->cq, &qhp->wq, &count);
  769. flushed = cxio_flush_sq(&qhp->wq, &schp->cq, count);
  770. spin_unlock(&qhp->lock);
  771. spin_unlock_irqrestore(&schp->lock, *flag);
  772. if (flushed)
  773. (*schp->ibcq.comp_handler)(&schp->ibcq, schp->ibcq.cq_context);
  774. /* deref */
  775. if (atomic_dec_and_test(&qhp->refcnt))
  776. wake_up(&qhp->wait);
  777. spin_lock_irqsave(&qhp->lock, *flag);
  778. }
  779. static void flush_qp(struct iwch_qp *qhp, unsigned long *flag)
  780. {
  781. if (qhp->ibqp.uobject)
  782. cxio_set_wq_in_error(&qhp->wq);
  783. else
  784. __flush_qp(qhp, flag);
  785. }
  786. /*
  787. * Return count of RECV WRs posted
  788. */
  789. u16 iwch_rqes_posted(struct iwch_qp *qhp)
  790. {
  791. union t3_wr *wqe = qhp->wq.queue;
  792. u16 count = 0;
  793. while ((count+1) != 0 && fw_riwrh_opcode((struct fw_riwrh *)wqe) == T3_WR_RCV) {
  794. count++;
  795. wqe++;
  796. }
  797. PDBG("%s qhp %p count %u\n", __func__, qhp, count);
  798. return count;
  799. }
  800. static int rdma_init(struct iwch_dev *rhp, struct iwch_qp *qhp,
  801. enum iwch_qp_attr_mask mask,
  802. struct iwch_qp_attributes *attrs)
  803. {
  804. struct t3_rdma_init_attr init_attr;
  805. int ret;
  806. init_attr.tid = qhp->ep->hwtid;
  807. init_attr.qpid = qhp->wq.qpid;
  808. init_attr.pdid = qhp->attr.pd;
  809. init_attr.scqid = qhp->attr.scq;
  810. init_attr.rcqid = qhp->attr.rcq;
  811. init_attr.rq_addr = qhp->wq.rq_addr;
  812. init_attr.rq_size = 1 << qhp->wq.rq_size_log2;
  813. init_attr.mpaattrs = uP_RI_MPA_IETF_ENABLE |
  814. qhp->attr.mpa_attr.recv_marker_enabled |
  815. (qhp->attr.mpa_attr.xmit_marker_enabled << 1) |
  816. (qhp->attr.mpa_attr.crc_enabled << 2);
  817. init_attr.qpcaps = uP_RI_QP_RDMA_READ_ENABLE |
  818. uP_RI_QP_RDMA_WRITE_ENABLE |
  819. uP_RI_QP_BIND_ENABLE;
  820. if (!qhp->ibqp.uobject)
  821. init_attr.qpcaps |= uP_RI_QP_STAG0_ENABLE |
  822. uP_RI_QP_FAST_REGISTER_ENABLE;
  823. init_attr.tcp_emss = qhp->ep->emss;
  824. init_attr.ord = qhp->attr.max_ord;
  825. init_attr.ird = qhp->attr.max_ird;
  826. init_attr.qp_dma_addr = qhp->wq.dma_addr;
  827. init_attr.qp_dma_size = (1UL << qhp->wq.size_log2);
  828. init_attr.rqe_count = iwch_rqes_posted(qhp);
  829. init_attr.flags = qhp->attr.mpa_attr.initiator ? MPA_INITIATOR : 0;
  830. init_attr.chan = qhp->ep->l2t->smt_idx;
  831. if (peer2peer) {
  832. init_attr.rtr_type = RTR_READ;
  833. if (init_attr.ord == 0 && qhp->attr.mpa_attr.initiator)
  834. init_attr.ord = 1;
  835. if (init_attr.ird == 0 && !qhp->attr.mpa_attr.initiator)
  836. init_attr.ird = 1;
  837. } else
  838. init_attr.rtr_type = 0;
  839. init_attr.irs = qhp->ep->rcv_seq;
  840. PDBG("%s init_attr.rq_addr 0x%x init_attr.rq_size = %d "
  841. "flags 0x%x qpcaps 0x%x\n", __func__,
  842. init_attr.rq_addr, init_attr.rq_size,
  843. init_attr.flags, init_attr.qpcaps);
  844. ret = cxio_rdma_init(&rhp->rdev, &init_attr);
  845. PDBG("%s ret %d\n", __func__, ret);
  846. return ret;
  847. }
  848. int iwch_modify_qp(struct iwch_dev *rhp, struct iwch_qp *qhp,
  849. enum iwch_qp_attr_mask mask,
  850. struct iwch_qp_attributes *attrs,
  851. int internal)
  852. {
  853. int ret = 0;
  854. struct iwch_qp_attributes newattr = qhp->attr;
  855. unsigned long flag;
  856. int disconnect = 0;
  857. int terminate = 0;
  858. int abort = 0;
  859. int free = 0;
  860. struct iwch_ep *ep = NULL;
  861. PDBG("%s qhp %p qpid 0x%x ep %p state %d -> %d\n", __func__,
  862. qhp, qhp->wq.qpid, qhp->ep, qhp->attr.state,
  863. (mask & IWCH_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
  864. spin_lock_irqsave(&qhp->lock, flag);
  865. /* Process attr changes if in IDLE */
  866. if (mask & IWCH_QP_ATTR_VALID_MODIFY) {
  867. if (qhp->attr.state != IWCH_QP_STATE_IDLE) {
  868. ret = -EIO;
  869. goto out;
  870. }
  871. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_READ)
  872. newattr.enable_rdma_read = attrs->enable_rdma_read;
  873. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_WRITE)
  874. newattr.enable_rdma_write = attrs->enable_rdma_write;
  875. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_BIND)
  876. newattr.enable_bind = attrs->enable_bind;
  877. if (mask & IWCH_QP_ATTR_MAX_ORD) {
  878. if (attrs->max_ord >
  879. rhp->attr.max_rdma_read_qp_depth) {
  880. ret = -EINVAL;
  881. goto out;
  882. }
  883. newattr.max_ord = attrs->max_ord;
  884. }
  885. if (mask & IWCH_QP_ATTR_MAX_IRD) {
  886. if (attrs->max_ird >
  887. rhp->attr.max_rdma_reads_per_qp) {
  888. ret = -EINVAL;
  889. goto out;
  890. }
  891. newattr.max_ird = attrs->max_ird;
  892. }
  893. qhp->attr = newattr;
  894. }
  895. if (!(mask & IWCH_QP_ATTR_NEXT_STATE))
  896. goto out;
  897. if (qhp->attr.state == attrs->next_state)
  898. goto out;
  899. switch (qhp->attr.state) {
  900. case IWCH_QP_STATE_IDLE:
  901. switch (attrs->next_state) {
  902. case IWCH_QP_STATE_RTS:
  903. if (!(mask & IWCH_QP_ATTR_LLP_STREAM_HANDLE)) {
  904. ret = -EINVAL;
  905. goto out;
  906. }
  907. if (!(mask & IWCH_QP_ATTR_MPA_ATTR)) {
  908. ret = -EINVAL;
  909. goto out;
  910. }
  911. qhp->attr.mpa_attr = attrs->mpa_attr;
  912. qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
  913. qhp->ep = qhp->attr.llp_stream_handle;
  914. qhp->attr.state = IWCH_QP_STATE_RTS;
  915. /*
  916. * Ref the endpoint here and deref when we
  917. * disassociate the endpoint from the QP. This
  918. * happens in CLOSING->IDLE transition or *->ERROR
  919. * transition.
  920. */
  921. get_ep(&qhp->ep->com);
  922. spin_unlock_irqrestore(&qhp->lock, flag);
  923. ret = rdma_init(rhp, qhp, mask, attrs);
  924. spin_lock_irqsave(&qhp->lock, flag);
  925. if (ret)
  926. goto err;
  927. break;
  928. case IWCH_QP_STATE_ERROR:
  929. qhp->attr.state = IWCH_QP_STATE_ERROR;
  930. flush_qp(qhp, &flag);
  931. break;
  932. default:
  933. ret = -EINVAL;
  934. goto out;
  935. }
  936. break;
  937. case IWCH_QP_STATE_RTS:
  938. switch (attrs->next_state) {
  939. case IWCH_QP_STATE_CLOSING:
  940. BUG_ON(atomic_read(&qhp->ep->com.kref.refcount) < 2);
  941. qhp->attr.state = IWCH_QP_STATE_CLOSING;
  942. if (!internal) {
  943. abort=0;
  944. disconnect = 1;
  945. ep = qhp->ep;
  946. get_ep(&ep->com);
  947. }
  948. break;
  949. case IWCH_QP_STATE_TERMINATE:
  950. qhp->attr.state = IWCH_QP_STATE_TERMINATE;
  951. if (qhp->ibqp.uobject)
  952. cxio_set_wq_in_error(&qhp->wq);
  953. if (!internal)
  954. terminate = 1;
  955. break;
  956. case IWCH_QP_STATE_ERROR:
  957. qhp->attr.state = IWCH_QP_STATE_ERROR;
  958. if (!internal) {
  959. abort=1;
  960. disconnect = 1;
  961. ep = qhp->ep;
  962. get_ep(&ep->com);
  963. }
  964. goto err;
  965. break;
  966. default:
  967. ret = -EINVAL;
  968. goto out;
  969. }
  970. break;
  971. case IWCH_QP_STATE_CLOSING:
  972. if (!internal) {
  973. ret = -EINVAL;
  974. goto out;
  975. }
  976. switch (attrs->next_state) {
  977. case IWCH_QP_STATE_IDLE:
  978. flush_qp(qhp, &flag);
  979. qhp->attr.state = IWCH_QP_STATE_IDLE;
  980. qhp->attr.llp_stream_handle = NULL;
  981. put_ep(&qhp->ep->com);
  982. qhp->ep = NULL;
  983. wake_up(&qhp->wait);
  984. break;
  985. case IWCH_QP_STATE_ERROR:
  986. goto err;
  987. default:
  988. ret = -EINVAL;
  989. goto err;
  990. }
  991. break;
  992. case IWCH_QP_STATE_ERROR:
  993. if (attrs->next_state != IWCH_QP_STATE_IDLE) {
  994. ret = -EINVAL;
  995. goto out;
  996. }
  997. if (!Q_EMPTY(qhp->wq.sq_rptr, qhp->wq.sq_wptr) ||
  998. !Q_EMPTY(qhp->wq.rq_rptr, qhp->wq.rq_wptr)) {
  999. ret = -EINVAL;
  1000. goto out;
  1001. }
  1002. qhp->attr.state = IWCH_QP_STATE_IDLE;
  1003. break;
  1004. case IWCH_QP_STATE_TERMINATE:
  1005. if (!internal) {
  1006. ret = -EINVAL;
  1007. goto out;
  1008. }
  1009. goto err;
  1010. break;
  1011. default:
  1012. printk(KERN_ERR "%s in a bad state %d\n",
  1013. __func__, qhp->attr.state);
  1014. ret = -EINVAL;
  1015. goto err;
  1016. break;
  1017. }
  1018. goto out;
  1019. err:
  1020. PDBG("%s disassociating ep %p qpid 0x%x\n", __func__, qhp->ep,
  1021. qhp->wq.qpid);
  1022. /* disassociate the LLP connection */
  1023. qhp->attr.llp_stream_handle = NULL;
  1024. ep = qhp->ep;
  1025. qhp->ep = NULL;
  1026. qhp->attr.state = IWCH_QP_STATE_ERROR;
  1027. free=1;
  1028. wake_up(&qhp->wait);
  1029. BUG_ON(!ep);
  1030. flush_qp(qhp, &flag);
  1031. out:
  1032. spin_unlock_irqrestore(&qhp->lock, flag);
  1033. if (terminate)
  1034. iwch_post_terminate(qhp, NULL);
  1035. /*
  1036. * If disconnect is 1, then we need to initiate a disconnect
  1037. * on the EP. This can be a normal close (RTS->CLOSING) or
  1038. * an abnormal close (RTS/CLOSING->ERROR).
  1039. */
  1040. if (disconnect) {
  1041. iwch_ep_disconnect(ep, abort, GFP_KERNEL);
  1042. put_ep(&ep->com);
  1043. }
  1044. /*
  1045. * If free is 1, then we've disassociated the EP from the QP
  1046. * and we need to dereference the EP.
  1047. */
  1048. if (free)
  1049. put_ep(&ep->com);
  1050. PDBG("%s exit state %d\n", __func__, qhp->attr.state);
  1051. return ret;
  1052. }
  1053. static int quiesce_qp(struct iwch_qp *qhp)
  1054. {
  1055. spin_lock_irq(&qhp->lock);
  1056. iwch_quiesce_tid(qhp->ep);
  1057. qhp->flags |= QP_QUIESCED;
  1058. spin_unlock_irq(&qhp->lock);
  1059. return 0;
  1060. }
  1061. static int resume_qp(struct iwch_qp *qhp)
  1062. {
  1063. spin_lock_irq(&qhp->lock);
  1064. iwch_resume_tid(qhp->ep);
  1065. qhp->flags &= ~QP_QUIESCED;
  1066. spin_unlock_irq(&qhp->lock);
  1067. return 0;
  1068. }
  1069. int iwch_quiesce_qps(struct iwch_cq *chp)
  1070. {
  1071. int i;
  1072. struct iwch_qp *qhp;
  1073. for (i=0; i < T3_MAX_NUM_QP; i++) {
  1074. qhp = get_qhp(chp->rhp, i);
  1075. if (!qhp)
  1076. continue;
  1077. if ((qhp->attr.rcq == chp->cq.cqid) && !qp_quiesced(qhp)) {
  1078. quiesce_qp(qhp);
  1079. continue;
  1080. }
  1081. if ((qhp->attr.scq == chp->cq.cqid) && !qp_quiesced(qhp))
  1082. quiesce_qp(qhp);
  1083. }
  1084. return 0;
  1085. }
  1086. int iwch_resume_qps(struct iwch_cq *chp)
  1087. {
  1088. int i;
  1089. struct iwch_qp *qhp;
  1090. for (i=0; i < T3_MAX_NUM_QP; i++) {
  1091. qhp = get_qhp(chp->rhp, i);
  1092. if (!qhp)
  1093. continue;
  1094. if ((qhp->attr.rcq == chp->cq.cqid) && qp_quiesced(qhp)) {
  1095. resume_qp(qhp);
  1096. continue;
  1097. }
  1098. if ((qhp->attr.scq == chp->cq.cqid) && qp_quiesced(qhp))
  1099. resume_qp(qhp);
  1100. }
  1101. return 0;
  1102. }