iwch_qp.c 31 KB

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