iwch_qp.c 32 KB

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