iwch_qp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. #define NO_SUPPORT -1
  37. static int iwch_build_rdma_send(union t3_wr *wqe, struct ib_send_wr *wr,
  38. u8 * flit_cnt)
  39. {
  40. int i;
  41. u32 plen;
  42. switch (wr->opcode) {
  43. case IB_WR_SEND:
  44. case IB_WR_SEND_WITH_IMM:
  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. #if 0 /* Not currently supported */
  52. case TYPE_SEND_INVALIDATE:
  53. case TYPE_SEND_INVALIDATE_IMMEDIATE:
  54. wqe->send.rdmaop = T3_SEND_WITH_INV;
  55. wqe->send.rem_stag = cpu_to_be32(wr->wr.rdma.rkey);
  56. break;
  57. case TYPE_SEND_SE_INVALIDATE:
  58. wqe->send.rdmaop = T3_SEND_WITH_SE_INV;
  59. wqe->send.rem_stag = cpu_to_be32(wr->wr.rdma.rkey);
  60. break;
  61. #endif
  62. default:
  63. break;
  64. }
  65. if (wr->num_sge > T3_MAX_SGE)
  66. return -EINVAL;
  67. wqe->send.reserved[0] = 0;
  68. wqe->send.reserved[1] = 0;
  69. wqe->send.reserved[2] = 0;
  70. if (wr->opcode == IB_WR_SEND_WITH_IMM) {
  71. plen = 4;
  72. wqe->send.sgl[0].stag = wr->imm_data;
  73. wqe->send.sgl[0].len = __constant_cpu_to_be32(0);
  74. wqe->send.num_sgle = __constant_cpu_to_be32(0);
  75. *flit_cnt = 5;
  76. } else {
  77. plen = 0;
  78. for (i = 0; i < wr->num_sge; i++) {
  79. if ((plen + wr->sg_list[i].length) < plen) {
  80. return -EMSGSIZE;
  81. }
  82. plen += wr->sg_list[i].length;
  83. wqe->send.sgl[i].stag =
  84. cpu_to_be32(wr->sg_list[i].lkey);
  85. wqe->send.sgl[i].len =
  86. cpu_to_be32(wr->sg_list[i].length);
  87. wqe->send.sgl[i].to = cpu_to_be64(wr->sg_list[i].addr);
  88. }
  89. wqe->send.num_sgle = cpu_to_be32(wr->num_sge);
  90. *flit_cnt = 4 + ((wr->num_sge) << 1);
  91. }
  92. wqe->send.plen = cpu_to_be32(plen);
  93. return 0;
  94. }
  95. static int iwch_build_rdma_write(union t3_wr *wqe, struct ib_send_wr *wr,
  96. u8 *flit_cnt)
  97. {
  98. int i;
  99. u32 plen;
  100. if (wr->num_sge > T3_MAX_SGE)
  101. return -EINVAL;
  102. wqe->write.rdmaop = T3_RDMA_WRITE;
  103. wqe->write.reserved[0] = 0;
  104. wqe->write.reserved[1] = 0;
  105. wqe->write.reserved[2] = 0;
  106. wqe->write.stag_sink = cpu_to_be32(wr->wr.rdma.rkey);
  107. wqe->write.to_sink = cpu_to_be64(wr->wr.rdma.remote_addr);
  108. if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
  109. plen = 4;
  110. wqe->write.sgl[0].stag = wr->imm_data;
  111. wqe->write.sgl[0].len = __constant_cpu_to_be32(0);
  112. wqe->write.num_sgle = __constant_cpu_to_be32(0);
  113. *flit_cnt = 6;
  114. } else {
  115. plen = 0;
  116. for (i = 0; i < wr->num_sge; i++) {
  117. if ((plen + wr->sg_list[i].length) < plen) {
  118. return -EMSGSIZE;
  119. }
  120. plen += wr->sg_list[i].length;
  121. wqe->write.sgl[i].stag =
  122. cpu_to_be32(wr->sg_list[i].lkey);
  123. wqe->write.sgl[i].len =
  124. cpu_to_be32(wr->sg_list[i].length);
  125. wqe->write.sgl[i].to =
  126. cpu_to_be64(wr->sg_list[i].addr);
  127. }
  128. wqe->write.num_sgle = cpu_to_be32(wr->num_sge);
  129. *flit_cnt = 5 + ((wr->num_sge) << 1);
  130. }
  131. wqe->write.plen = cpu_to_be32(plen);
  132. return 0;
  133. }
  134. static int iwch_build_rdma_read(union t3_wr *wqe, struct ib_send_wr *wr,
  135. u8 *flit_cnt)
  136. {
  137. if (wr->num_sge > 1)
  138. return -EINVAL;
  139. wqe->read.rdmaop = T3_READ_REQ;
  140. wqe->read.reserved[0] = 0;
  141. wqe->read.reserved[1] = 0;
  142. wqe->read.reserved[2] = 0;
  143. wqe->read.rem_stag = cpu_to_be32(wr->wr.rdma.rkey);
  144. wqe->read.rem_to = cpu_to_be64(wr->wr.rdma.remote_addr);
  145. wqe->read.local_stag = cpu_to_be32(wr->sg_list[0].lkey);
  146. wqe->read.local_len = cpu_to_be32(wr->sg_list[0].length);
  147. wqe->read.local_to = cpu_to_be64(wr->sg_list[0].addr);
  148. *flit_cnt = sizeof(struct t3_rdma_read_wr) >> 3;
  149. return 0;
  150. }
  151. /*
  152. * TBD: this is going to be moved to firmware. Missing pdid/qpid check for now.
  153. */
  154. static int iwch_sgl2pbl_map(struct iwch_dev *rhp, struct ib_sge *sg_list,
  155. u32 num_sgle, u32 * pbl_addr, u8 * page_size)
  156. {
  157. int i;
  158. struct iwch_mr *mhp;
  159. u32 offset;
  160. for (i = 0; i < num_sgle; i++) {
  161. mhp = get_mhp(rhp, (sg_list[i].lkey) >> 8);
  162. if (!mhp) {
  163. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  164. return -EIO;
  165. }
  166. if (!mhp->attr.state) {
  167. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  168. return -EIO;
  169. }
  170. if (mhp->attr.zbva) {
  171. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  172. return -EIO;
  173. }
  174. if (sg_list[i].addr < mhp->attr.va_fbo) {
  175. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  176. return -EINVAL;
  177. }
  178. if (sg_list[i].addr + ((u64) sg_list[i].length) <
  179. sg_list[i].addr) {
  180. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  181. return -EINVAL;
  182. }
  183. if (sg_list[i].addr + ((u64) sg_list[i].length) >
  184. mhp->attr.va_fbo + ((u64) mhp->attr.len)) {
  185. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  186. return -EINVAL;
  187. }
  188. offset = sg_list[i].addr - mhp->attr.va_fbo;
  189. offset += ((u32) mhp->attr.va_fbo) %
  190. (1UL << (12 + mhp->attr.page_size));
  191. pbl_addr[i] = ((mhp->attr.pbl_addr -
  192. rhp->rdev.rnic_info.pbl_base) >> 3) +
  193. (offset >> (12 + mhp->attr.page_size));
  194. page_size[i] = mhp->attr.page_size;
  195. }
  196. return 0;
  197. }
  198. static int iwch_build_rdma_recv(struct iwch_dev *rhp, union t3_wr *wqe,
  199. struct ib_recv_wr *wr)
  200. {
  201. int i;
  202. if (wr->num_sge > T3_MAX_SGE)
  203. return -EINVAL;
  204. wqe->recv.num_sgle = cpu_to_be32(wr->num_sge);
  205. for (i = 0; i < wr->num_sge; i++) {
  206. wqe->recv.sgl[i].stag = cpu_to_be32(wr->sg_list[i].lkey);
  207. wqe->recv.sgl[i].len = cpu_to_be32(wr->sg_list[i].length);
  208. wqe->recv.sgl[i].to = cpu_to_be64(wr->sg_list[i].addr);
  209. }
  210. for (; i < T3_MAX_SGE; i++) {
  211. wqe->recv.sgl[i].stag = 0;
  212. wqe->recv.sgl[i].len = 0;
  213. wqe->recv.sgl[i].to = 0;
  214. }
  215. return 0;
  216. }
  217. int iwch_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  218. struct ib_send_wr **bad_wr)
  219. {
  220. int err = 0;
  221. u8 t3_wr_flit_cnt;
  222. enum t3_wr_opcode t3_wr_opcode = 0;
  223. enum t3_wr_flags t3_wr_flags;
  224. struct iwch_qp *qhp;
  225. u32 idx;
  226. union t3_wr *wqe;
  227. u32 num_wrs;
  228. unsigned long flag;
  229. struct t3_swsq *sqp;
  230. qhp = to_iwch_qp(ibqp);
  231. spin_lock_irqsave(&qhp->lock, flag);
  232. if (qhp->attr.state > IWCH_QP_STATE_RTS) {
  233. spin_unlock_irqrestore(&qhp->lock, flag);
  234. return -EINVAL;
  235. }
  236. num_wrs = Q_FREECNT(qhp->wq.sq_rptr, qhp->wq.sq_wptr,
  237. qhp->wq.sq_size_log2);
  238. if (num_wrs <= 0) {
  239. spin_unlock_irqrestore(&qhp->lock, flag);
  240. return -ENOMEM;
  241. }
  242. while (wr) {
  243. if (num_wrs == 0) {
  244. err = -ENOMEM;
  245. *bad_wr = wr;
  246. break;
  247. }
  248. idx = Q_PTR2IDX(qhp->wq.wptr, qhp->wq.size_log2);
  249. wqe = (union t3_wr *) (qhp->wq.queue + idx);
  250. t3_wr_flags = 0;
  251. if (wr->send_flags & IB_SEND_SOLICITED)
  252. t3_wr_flags |= T3_SOLICITED_EVENT_FLAG;
  253. if (wr->send_flags & IB_SEND_FENCE)
  254. t3_wr_flags |= T3_READ_FENCE_FLAG;
  255. if (wr->send_flags & IB_SEND_SIGNALED)
  256. t3_wr_flags |= T3_COMPLETION_FLAG;
  257. sqp = qhp->wq.sq +
  258. Q_PTR2IDX(qhp->wq.sq_wptr, qhp->wq.sq_size_log2);
  259. switch (wr->opcode) {
  260. case IB_WR_SEND:
  261. case IB_WR_SEND_WITH_IMM:
  262. t3_wr_opcode = T3_WR_SEND;
  263. err = iwch_build_rdma_send(wqe, wr, &t3_wr_flit_cnt);
  264. break;
  265. case IB_WR_RDMA_WRITE:
  266. case IB_WR_RDMA_WRITE_WITH_IMM:
  267. t3_wr_opcode = T3_WR_WRITE;
  268. err = iwch_build_rdma_write(wqe, wr, &t3_wr_flit_cnt);
  269. break;
  270. case IB_WR_RDMA_READ:
  271. t3_wr_opcode = T3_WR_READ;
  272. t3_wr_flags = 0; /* T3 reads are always signaled */
  273. err = iwch_build_rdma_read(wqe, wr, &t3_wr_flit_cnt);
  274. if (err)
  275. break;
  276. sqp->read_len = wqe->read.local_len;
  277. if (!qhp->wq.oldest_read)
  278. qhp->wq.oldest_read = sqp;
  279. break;
  280. default:
  281. PDBG("%s post of type=%d TBD!\n", __FUNCTION__,
  282. wr->opcode);
  283. err = -EINVAL;
  284. }
  285. if (err) {
  286. *bad_wr = wr;
  287. break;
  288. }
  289. wqe->send.wrid.id0.hi = qhp->wq.sq_wptr;
  290. sqp->wr_id = wr->wr_id;
  291. sqp->opcode = wr2opcode(t3_wr_opcode);
  292. sqp->sq_wptr = qhp->wq.sq_wptr;
  293. sqp->complete = 0;
  294. sqp->signaled = (wr->send_flags & IB_SEND_SIGNALED);
  295. build_fw_riwrh((void *) wqe, t3_wr_opcode, t3_wr_flags,
  296. Q_GENBIT(qhp->wq.wptr, qhp->wq.size_log2),
  297. 0, t3_wr_flit_cnt);
  298. PDBG("%s cookie 0x%llx wq idx 0x%x swsq idx %ld opcode %d\n",
  299. __FUNCTION__, (unsigned long long) wr->wr_id, idx,
  300. Q_PTR2IDX(qhp->wq.sq_wptr, qhp->wq.sq_size_log2),
  301. sqp->opcode);
  302. wr = wr->next;
  303. num_wrs--;
  304. ++(qhp->wq.wptr);
  305. ++(qhp->wq.sq_wptr);
  306. }
  307. spin_unlock_irqrestore(&qhp->lock, flag);
  308. ring_doorbell(qhp->wq.doorbell, qhp->wq.qpid);
  309. return err;
  310. }
  311. int iwch_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  312. struct ib_recv_wr **bad_wr)
  313. {
  314. int err = 0;
  315. struct iwch_qp *qhp;
  316. u32 idx;
  317. union t3_wr *wqe;
  318. u32 num_wrs;
  319. unsigned long flag;
  320. qhp = to_iwch_qp(ibqp);
  321. spin_lock_irqsave(&qhp->lock, flag);
  322. if (qhp->attr.state > IWCH_QP_STATE_RTS) {
  323. spin_unlock_irqrestore(&qhp->lock, flag);
  324. return -EINVAL;
  325. }
  326. num_wrs = Q_FREECNT(qhp->wq.rq_rptr, qhp->wq.rq_wptr,
  327. qhp->wq.rq_size_log2) - 1;
  328. if (!wr) {
  329. spin_unlock_irqrestore(&qhp->lock, flag);
  330. return -EINVAL;
  331. }
  332. while (wr) {
  333. idx = Q_PTR2IDX(qhp->wq.wptr, qhp->wq.size_log2);
  334. wqe = (union t3_wr *) (qhp->wq.queue + idx);
  335. if (num_wrs)
  336. err = iwch_build_rdma_recv(qhp->rhp, wqe, wr);
  337. else
  338. err = -ENOMEM;
  339. if (err) {
  340. *bad_wr = wr;
  341. break;
  342. }
  343. qhp->wq.rq[Q_PTR2IDX(qhp->wq.rq_wptr, qhp->wq.rq_size_log2)] =
  344. wr->wr_id;
  345. build_fw_riwrh((void *) wqe, T3_WR_RCV, T3_COMPLETION_FLAG,
  346. Q_GENBIT(qhp->wq.wptr, qhp->wq.size_log2),
  347. 0, sizeof(struct t3_receive_wr) >> 3);
  348. PDBG("%s cookie 0x%llx idx 0x%x rq_wptr 0x%x rw_rptr 0x%x "
  349. "wqe %p \n", __FUNCTION__, (unsigned long long) wr->wr_id,
  350. idx, qhp->wq.rq_wptr, qhp->wq.rq_rptr, wqe);
  351. ++(qhp->wq.rq_wptr);
  352. ++(qhp->wq.wptr);
  353. wr = wr->next;
  354. num_wrs--;
  355. }
  356. spin_unlock_irqrestore(&qhp->lock, flag);
  357. ring_doorbell(qhp->wq.doorbell, qhp->wq.qpid);
  358. return err;
  359. }
  360. int iwch_bind_mw(struct ib_qp *qp,
  361. struct ib_mw *mw,
  362. struct ib_mw_bind *mw_bind)
  363. {
  364. struct iwch_dev *rhp;
  365. struct iwch_mw *mhp;
  366. struct iwch_qp *qhp;
  367. union t3_wr *wqe;
  368. u32 pbl_addr;
  369. u8 page_size;
  370. u32 num_wrs;
  371. unsigned long flag;
  372. struct ib_sge sgl;
  373. int err=0;
  374. enum t3_wr_flags t3_wr_flags;
  375. u32 idx;
  376. struct t3_swsq *sqp;
  377. qhp = to_iwch_qp(qp);
  378. mhp = to_iwch_mw(mw);
  379. rhp = qhp->rhp;
  380. spin_lock_irqsave(&qhp->lock, flag);
  381. if (qhp->attr.state > IWCH_QP_STATE_RTS) {
  382. spin_unlock_irqrestore(&qhp->lock, flag);
  383. return -EINVAL;
  384. }
  385. num_wrs = Q_FREECNT(qhp->wq.sq_rptr, qhp->wq.sq_wptr,
  386. qhp->wq.sq_size_log2);
  387. if ((num_wrs) <= 0) {
  388. spin_unlock_irqrestore(&qhp->lock, flag);
  389. return -ENOMEM;
  390. }
  391. idx = Q_PTR2IDX(qhp->wq.wptr, qhp->wq.size_log2);
  392. PDBG("%s: idx 0x%0x, mw 0x%p, mw_bind 0x%p\n", __FUNCTION__, idx,
  393. mw, mw_bind);
  394. wqe = (union t3_wr *) (qhp->wq.queue + idx);
  395. t3_wr_flags = 0;
  396. if (mw_bind->send_flags & IB_SEND_SIGNALED)
  397. t3_wr_flags = T3_COMPLETION_FLAG;
  398. sgl.addr = mw_bind->addr;
  399. sgl.lkey = mw_bind->mr->lkey;
  400. sgl.length = mw_bind->length;
  401. wqe->bind.reserved = 0;
  402. wqe->bind.type = T3_VA_BASED_TO;
  403. /* TBD: check perms */
  404. wqe->bind.perms = iwch_ib_to_mwbind_access(mw_bind->mw_access_flags);
  405. wqe->bind.mr_stag = cpu_to_be32(mw_bind->mr->lkey);
  406. wqe->bind.mw_stag = cpu_to_be32(mw->rkey);
  407. wqe->bind.mw_len = cpu_to_be32(mw_bind->length);
  408. wqe->bind.mw_va = cpu_to_be64(mw_bind->addr);
  409. err = iwch_sgl2pbl_map(rhp, &sgl, 1, &pbl_addr, &page_size);
  410. if (err) {
  411. spin_unlock_irqrestore(&qhp->lock, flag);
  412. return err;
  413. }
  414. wqe->send.wrid.id0.hi = qhp->wq.sq_wptr;
  415. sqp = qhp->wq.sq + Q_PTR2IDX(qhp->wq.sq_wptr, qhp->wq.sq_size_log2);
  416. sqp->wr_id = mw_bind->wr_id;
  417. sqp->opcode = T3_BIND_MW;
  418. sqp->sq_wptr = qhp->wq.sq_wptr;
  419. sqp->complete = 0;
  420. sqp->signaled = (mw_bind->send_flags & IB_SEND_SIGNALED);
  421. wqe->bind.mr_pbl_addr = cpu_to_be32(pbl_addr);
  422. wqe->bind.mr_pagesz = page_size;
  423. wqe->flit[T3_SQ_COOKIE_FLIT] = mw_bind->wr_id;
  424. build_fw_riwrh((void *)wqe, T3_WR_BIND, t3_wr_flags,
  425. Q_GENBIT(qhp->wq.wptr, qhp->wq.size_log2), 0,
  426. sizeof(struct t3_bind_mw_wr) >> 3);
  427. ++(qhp->wq.wptr);
  428. ++(qhp->wq.sq_wptr);
  429. spin_unlock_irqrestore(&qhp->lock, flag);
  430. ring_doorbell(qhp->wq.doorbell, qhp->wq.qpid);
  431. return err;
  432. }
  433. static inline void build_term_codes(struct respQ_msg_t *rsp_msg,
  434. u8 *layer_type, u8 *ecode)
  435. {
  436. int status = TPT_ERR_INTERNAL_ERR;
  437. int tagged = 0;
  438. int opcode = -1;
  439. int rqtype = 0;
  440. int send_inv = 0;
  441. if (rsp_msg) {
  442. status = CQE_STATUS(rsp_msg->cqe);
  443. opcode = CQE_OPCODE(rsp_msg->cqe);
  444. rqtype = RQ_TYPE(rsp_msg->cqe);
  445. send_inv = (opcode == T3_SEND_WITH_INV) ||
  446. (opcode == T3_SEND_WITH_SE_INV);
  447. tagged = (opcode == T3_RDMA_WRITE) ||
  448. (rqtype && (opcode == T3_READ_RESP));
  449. }
  450. switch (status) {
  451. case TPT_ERR_STAG:
  452. if (send_inv) {
  453. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  454. *ecode = RDMAP_CANT_INV_STAG;
  455. } else {
  456. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  457. *ecode = RDMAP_INV_STAG;
  458. }
  459. break;
  460. case TPT_ERR_PDID:
  461. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  462. if ((opcode == T3_SEND_WITH_INV) ||
  463. (opcode == T3_SEND_WITH_SE_INV))
  464. *ecode = RDMAP_CANT_INV_STAG;
  465. else
  466. *ecode = RDMAP_STAG_NOT_ASSOC;
  467. break;
  468. case TPT_ERR_QPID:
  469. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  470. *ecode = RDMAP_STAG_NOT_ASSOC;
  471. break;
  472. case TPT_ERR_ACCESS:
  473. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  474. *ecode = RDMAP_ACC_VIOL;
  475. break;
  476. case TPT_ERR_WRAP:
  477. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  478. *ecode = RDMAP_TO_WRAP;
  479. break;
  480. case TPT_ERR_BOUND:
  481. if (tagged) {
  482. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  483. *ecode = DDPT_BASE_BOUNDS;
  484. } else {
  485. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  486. *ecode = RDMAP_BASE_BOUNDS;
  487. }
  488. break;
  489. case TPT_ERR_INVALIDATE_SHARED_MR:
  490. case TPT_ERR_INVALIDATE_MR_WITH_MW_BOUND:
  491. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  492. *ecode = RDMAP_CANT_INV_STAG;
  493. break;
  494. case TPT_ERR_ECC:
  495. case TPT_ERR_ECC_PSTAG:
  496. case TPT_ERR_INTERNAL_ERR:
  497. *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
  498. *ecode = 0;
  499. break;
  500. case TPT_ERR_OUT_OF_RQE:
  501. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  502. *ecode = DDPU_INV_MSN_NOBUF;
  503. break;
  504. case TPT_ERR_PBL_ADDR_BOUND:
  505. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  506. *ecode = DDPT_BASE_BOUNDS;
  507. break;
  508. case TPT_ERR_CRC:
  509. *layer_type = LAYER_MPA|DDP_LLP;
  510. *ecode = MPA_CRC_ERR;
  511. break;
  512. case TPT_ERR_MARKER:
  513. *layer_type = LAYER_MPA|DDP_LLP;
  514. *ecode = MPA_MARKER_ERR;
  515. break;
  516. case TPT_ERR_PDU_LEN_ERR:
  517. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  518. *ecode = DDPU_MSG_TOOBIG;
  519. break;
  520. case TPT_ERR_DDP_VERSION:
  521. if (tagged) {
  522. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  523. *ecode = DDPT_INV_VERS;
  524. } else {
  525. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  526. *ecode = DDPU_INV_VERS;
  527. }
  528. break;
  529. case TPT_ERR_RDMA_VERSION:
  530. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  531. *ecode = RDMAP_INV_VERS;
  532. break;
  533. case TPT_ERR_OPCODE:
  534. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  535. *ecode = RDMAP_INV_OPCODE;
  536. break;
  537. case TPT_ERR_DDP_QUEUE_NUM:
  538. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  539. *ecode = DDPU_INV_QN;
  540. break;
  541. case TPT_ERR_MSN:
  542. case TPT_ERR_MSN_GAP:
  543. case TPT_ERR_MSN_RANGE:
  544. case TPT_ERR_IRD_OVERFLOW:
  545. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  546. *ecode = DDPU_INV_MSN_RANGE;
  547. break;
  548. case TPT_ERR_TBIT:
  549. *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
  550. *ecode = 0;
  551. break;
  552. case TPT_ERR_MO:
  553. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  554. *ecode = DDPU_INV_MO;
  555. break;
  556. default:
  557. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  558. *ecode = 0;
  559. break;
  560. }
  561. }
  562. /*
  563. * This posts a TERMINATE with layer=RDMA, type=catastrophic.
  564. */
  565. int iwch_post_terminate(struct iwch_qp *qhp, struct respQ_msg_t *rsp_msg)
  566. {
  567. union t3_wr *wqe;
  568. struct terminate_message *term;
  569. struct sk_buff *skb;
  570. PDBG("%s %d\n", __FUNCTION__, __LINE__);
  571. skb = alloc_skb(40, GFP_ATOMIC);
  572. if (!skb) {
  573. printk(KERN_ERR "%s cannot send TERMINATE!\n", __FUNCTION__);
  574. return -ENOMEM;
  575. }
  576. wqe = (union t3_wr *)skb_put(skb, 40);
  577. memset(wqe, 0, 40);
  578. wqe->send.rdmaop = T3_TERMINATE;
  579. /* immediate data length */
  580. wqe->send.plen = htonl(4);
  581. /* immediate data starts here. */
  582. term = (struct terminate_message *)wqe->send.sgl;
  583. build_term_codes(rsp_msg, &term->layer_etype, &term->ecode);
  584. wqe->send.wrh.op_seop_flags = cpu_to_be32(V_FW_RIWR_OP(T3_WR_SEND) |
  585. V_FW_RIWR_FLAGS(T3_COMPLETION_FLAG | T3_NOTIFY_FLAG));
  586. wqe->send.wrh.gen_tid_len = cpu_to_be32(V_FW_RIWR_TID(qhp->ep->hwtid));
  587. skb->priority = CPL_PRIORITY_DATA;
  588. return cxgb3_ofld_send(qhp->rhp->rdev.t3cdev_p, skb);
  589. }
  590. /*
  591. * Assumes qhp lock is held.
  592. */
  593. static void __flush_qp(struct iwch_qp *qhp, unsigned long *flag)
  594. {
  595. struct iwch_cq *rchp, *schp;
  596. int count;
  597. rchp = get_chp(qhp->rhp, qhp->attr.rcq);
  598. schp = get_chp(qhp->rhp, qhp->attr.scq);
  599. PDBG("%s qhp %p rchp %p schp %p\n", __FUNCTION__, qhp, rchp, schp);
  600. /* take a ref on the qhp since we must release the lock */
  601. atomic_inc(&qhp->refcnt);
  602. spin_unlock_irqrestore(&qhp->lock, *flag);
  603. /* locking heirarchy: cq lock first, then qp lock. */
  604. spin_lock_irqsave(&rchp->lock, *flag);
  605. spin_lock(&qhp->lock);
  606. cxio_flush_hw_cq(&rchp->cq);
  607. cxio_count_rcqes(&rchp->cq, &qhp->wq, &count);
  608. cxio_flush_rq(&qhp->wq, &rchp->cq, count);
  609. spin_unlock(&qhp->lock);
  610. spin_unlock_irqrestore(&rchp->lock, *flag);
  611. (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
  612. /* locking heirarchy: cq lock first, then qp lock. */
  613. spin_lock_irqsave(&schp->lock, *flag);
  614. spin_lock(&qhp->lock);
  615. cxio_flush_hw_cq(&schp->cq);
  616. cxio_count_scqes(&schp->cq, &qhp->wq, &count);
  617. cxio_flush_sq(&qhp->wq, &schp->cq, count);
  618. spin_unlock(&qhp->lock);
  619. spin_unlock_irqrestore(&schp->lock, *flag);
  620. (*schp->ibcq.comp_handler)(&schp->ibcq, schp->ibcq.cq_context);
  621. /* deref */
  622. if (atomic_dec_and_test(&qhp->refcnt))
  623. wake_up(&qhp->wait);
  624. spin_lock_irqsave(&qhp->lock, *flag);
  625. }
  626. static void flush_qp(struct iwch_qp *qhp, unsigned long *flag)
  627. {
  628. if (qhp->ibqp.uobject)
  629. cxio_set_wq_in_error(&qhp->wq);
  630. else
  631. __flush_qp(qhp, flag);
  632. }
  633. /*
  634. * Return non zero if at least one RECV was pre-posted.
  635. */
  636. static int rqes_posted(struct iwch_qp *qhp)
  637. {
  638. return fw_riwrh_opcode((struct fw_riwrh *)qhp->wq.queue) == T3_WR_RCV;
  639. }
  640. static int rdma_init(struct iwch_dev *rhp, struct iwch_qp *qhp,
  641. enum iwch_qp_attr_mask mask,
  642. struct iwch_qp_attributes *attrs)
  643. {
  644. struct t3_rdma_init_attr init_attr;
  645. int ret;
  646. init_attr.tid = qhp->ep->hwtid;
  647. init_attr.qpid = qhp->wq.qpid;
  648. init_attr.pdid = qhp->attr.pd;
  649. init_attr.scqid = qhp->attr.scq;
  650. init_attr.rcqid = qhp->attr.rcq;
  651. init_attr.rq_addr = qhp->wq.rq_addr;
  652. init_attr.rq_size = 1 << qhp->wq.rq_size_log2;
  653. init_attr.mpaattrs = uP_RI_MPA_IETF_ENABLE |
  654. qhp->attr.mpa_attr.recv_marker_enabled |
  655. (qhp->attr.mpa_attr.xmit_marker_enabled << 1) |
  656. (qhp->attr.mpa_attr.crc_enabled << 2);
  657. /*
  658. * XXX - The IWCM doesn't quite handle getting these
  659. * attrs set before going into RTS. For now, just turn
  660. * them on always...
  661. */
  662. #if 0
  663. init_attr.qpcaps = qhp->attr.enableRdmaRead |
  664. (qhp->attr.enableRdmaWrite << 1) |
  665. (qhp->attr.enableBind << 2) |
  666. (qhp->attr.enable_stag0_fastreg << 3) |
  667. (qhp->attr.enable_stag0_fastreg << 4);
  668. #else
  669. init_attr.qpcaps = 0x1f;
  670. #endif
  671. init_attr.tcp_emss = qhp->ep->emss;
  672. init_attr.ord = qhp->attr.max_ord;
  673. init_attr.ird = qhp->attr.max_ird;
  674. init_attr.qp_dma_addr = qhp->wq.dma_addr;
  675. init_attr.qp_dma_size = (1UL << qhp->wq.size_log2);
  676. init_attr.flags = rqes_posted(qhp) ? RECVS_POSTED : 0;
  677. init_attr.flags |= capable(CAP_NET_BIND_SERVICE) ? PRIV_QP : 0;
  678. init_attr.irs = qhp->ep->rcv_seq;
  679. PDBG("%s init_attr.rq_addr 0x%x init_attr.rq_size = %d "
  680. "flags 0x%x qpcaps 0x%x\n", __FUNCTION__,
  681. init_attr.rq_addr, init_attr.rq_size,
  682. init_attr.flags, init_attr.qpcaps);
  683. ret = cxio_rdma_init(&rhp->rdev, &init_attr);
  684. PDBG("%s ret %d\n", __FUNCTION__, ret);
  685. return ret;
  686. }
  687. int iwch_modify_qp(struct iwch_dev *rhp, struct iwch_qp *qhp,
  688. enum iwch_qp_attr_mask mask,
  689. struct iwch_qp_attributes *attrs,
  690. int internal)
  691. {
  692. int ret = 0;
  693. struct iwch_qp_attributes newattr = qhp->attr;
  694. unsigned long flag;
  695. int disconnect = 0;
  696. int terminate = 0;
  697. int abort = 0;
  698. int free = 0;
  699. struct iwch_ep *ep = NULL;
  700. PDBG("%s qhp %p qpid 0x%x ep %p state %d -> %d\n", __FUNCTION__,
  701. qhp, qhp->wq.qpid, qhp->ep, qhp->attr.state,
  702. (mask & IWCH_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
  703. spin_lock_irqsave(&qhp->lock, flag);
  704. /* Process attr changes if in IDLE */
  705. if (mask & IWCH_QP_ATTR_VALID_MODIFY) {
  706. if (qhp->attr.state != IWCH_QP_STATE_IDLE) {
  707. ret = -EIO;
  708. goto out;
  709. }
  710. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_READ)
  711. newattr.enable_rdma_read = attrs->enable_rdma_read;
  712. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_WRITE)
  713. newattr.enable_rdma_write = attrs->enable_rdma_write;
  714. if (mask & IWCH_QP_ATTR_ENABLE_RDMA_BIND)
  715. newattr.enable_bind = attrs->enable_bind;
  716. if (mask & IWCH_QP_ATTR_MAX_ORD) {
  717. if (attrs->max_ord >
  718. rhp->attr.max_rdma_read_qp_depth) {
  719. ret = -EINVAL;
  720. goto out;
  721. }
  722. newattr.max_ord = attrs->max_ord;
  723. }
  724. if (mask & IWCH_QP_ATTR_MAX_IRD) {
  725. if (attrs->max_ird >
  726. rhp->attr.max_rdma_reads_per_qp) {
  727. ret = -EINVAL;
  728. goto out;
  729. }
  730. newattr.max_ird = attrs->max_ird;
  731. }
  732. qhp->attr = newattr;
  733. }
  734. if (!(mask & IWCH_QP_ATTR_NEXT_STATE))
  735. goto out;
  736. if (qhp->attr.state == attrs->next_state)
  737. goto out;
  738. switch (qhp->attr.state) {
  739. case IWCH_QP_STATE_IDLE:
  740. switch (attrs->next_state) {
  741. case IWCH_QP_STATE_RTS:
  742. if (!(mask & IWCH_QP_ATTR_LLP_STREAM_HANDLE)) {
  743. ret = -EINVAL;
  744. goto out;
  745. }
  746. if (!(mask & IWCH_QP_ATTR_MPA_ATTR)) {
  747. ret = -EINVAL;
  748. goto out;
  749. }
  750. qhp->attr.mpa_attr = attrs->mpa_attr;
  751. qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
  752. qhp->ep = qhp->attr.llp_stream_handle;
  753. qhp->attr.state = IWCH_QP_STATE_RTS;
  754. /*
  755. * Ref the endpoint here and deref when we
  756. * disassociate the endpoint from the QP. This
  757. * happens in CLOSING->IDLE transition or *->ERROR
  758. * transition.
  759. */
  760. get_ep(&qhp->ep->com);
  761. spin_unlock_irqrestore(&qhp->lock, flag);
  762. ret = rdma_init(rhp, qhp, mask, attrs);
  763. spin_lock_irqsave(&qhp->lock, flag);
  764. if (ret)
  765. goto err;
  766. break;
  767. case IWCH_QP_STATE_ERROR:
  768. qhp->attr.state = IWCH_QP_STATE_ERROR;
  769. flush_qp(qhp, &flag);
  770. break;
  771. default:
  772. ret = -EINVAL;
  773. goto out;
  774. }
  775. break;
  776. case IWCH_QP_STATE_RTS:
  777. switch (attrs->next_state) {
  778. case IWCH_QP_STATE_CLOSING:
  779. BUG_ON(atomic_read(&qhp->ep->com.kref.refcount) < 2);
  780. qhp->attr.state = IWCH_QP_STATE_CLOSING;
  781. if (!internal) {
  782. abort=0;
  783. disconnect = 1;
  784. ep = qhp->ep;
  785. }
  786. flush_qp(qhp, &flag);
  787. break;
  788. case IWCH_QP_STATE_TERMINATE:
  789. qhp->attr.state = IWCH_QP_STATE_TERMINATE;
  790. if (qhp->ibqp.uobject)
  791. cxio_set_wq_in_error(&qhp->wq);
  792. if (!internal)
  793. terminate = 1;
  794. break;
  795. case IWCH_QP_STATE_ERROR:
  796. qhp->attr.state = IWCH_QP_STATE_ERROR;
  797. if (!internal) {
  798. abort=1;
  799. disconnect = 1;
  800. ep = qhp->ep;
  801. }
  802. goto err;
  803. break;
  804. default:
  805. ret = -EINVAL;
  806. goto out;
  807. }
  808. break;
  809. case IWCH_QP_STATE_CLOSING:
  810. if (!internal) {
  811. ret = -EINVAL;
  812. goto out;
  813. }
  814. switch (attrs->next_state) {
  815. case IWCH_QP_STATE_IDLE:
  816. qhp->attr.state = IWCH_QP_STATE_IDLE;
  817. qhp->attr.llp_stream_handle = NULL;
  818. put_ep(&qhp->ep->com);
  819. qhp->ep = NULL;
  820. wake_up(&qhp->wait);
  821. break;
  822. case IWCH_QP_STATE_ERROR:
  823. goto err;
  824. default:
  825. ret = -EINVAL;
  826. goto err;
  827. }
  828. break;
  829. case IWCH_QP_STATE_ERROR:
  830. if (attrs->next_state != IWCH_QP_STATE_IDLE) {
  831. ret = -EINVAL;
  832. goto out;
  833. }
  834. if (!Q_EMPTY(qhp->wq.sq_rptr, qhp->wq.sq_wptr) ||
  835. !Q_EMPTY(qhp->wq.rq_rptr, qhp->wq.rq_wptr)) {
  836. ret = -EINVAL;
  837. goto out;
  838. }
  839. qhp->attr.state = IWCH_QP_STATE_IDLE;
  840. memset(&qhp->attr, 0, sizeof(qhp->attr));
  841. break;
  842. case IWCH_QP_STATE_TERMINATE:
  843. if (!internal) {
  844. ret = -EINVAL;
  845. goto out;
  846. }
  847. goto err;
  848. break;
  849. default:
  850. printk(KERN_ERR "%s in a bad state %d\n",
  851. __FUNCTION__, qhp->attr.state);
  852. ret = -EINVAL;
  853. goto err;
  854. break;
  855. }
  856. goto out;
  857. err:
  858. PDBG("%s disassociating ep %p qpid 0x%x\n", __FUNCTION__, qhp->ep,
  859. qhp->wq.qpid);
  860. /* disassociate the LLP connection */
  861. qhp->attr.llp_stream_handle = NULL;
  862. ep = qhp->ep;
  863. qhp->ep = NULL;
  864. qhp->attr.state = IWCH_QP_STATE_ERROR;
  865. free=1;
  866. wake_up(&qhp->wait);
  867. BUG_ON(!ep);
  868. flush_qp(qhp, &flag);
  869. out:
  870. spin_unlock_irqrestore(&qhp->lock, flag);
  871. if (terminate)
  872. iwch_post_terminate(qhp, NULL);
  873. /*
  874. * If disconnect is 1, then we need to initiate a disconnect
  875. * on the EP. This can be a normal close (RTS->CLOSING) or
  876. * an abnormal close (RTS/CLOSING->ERROR).
  877. */
  878. if (disconnect)
  879. iwch_ep_disconnect(ep, abort, GFP_KERNEL);
  880. /*
  881. * If free is 1, then we've disassociated the EP from the QP
  882. * and we need to dereference the EP.
  883. */
  884. if (free)
  885. put_ep(&ep->com);
  886. PDBG("%s exit state %d\n", __FUNCTION__, qhp->attr.state);
  887. return ret;
  888. }
  889. static int quiesce_qp(struct iwch_qp *qhp)
  890. {
  891. spin_lock_irq(&qhp->lock);
  892. iwch_quiesce_tid(qhp->ep);
  893. qhp->flags |= QP_QUIESCED;
  894. spin_unlock_irq(&qhp->lock);
  895. return 0;
  896. }
  897. static int resume_qp(struct iwch_qp *qhp)
  898. {
  899. spin_lock_irq(&qhp->lock);
  900. iwch_resume_tid(qhp->ep);
  901. qhp->flags &= ~QP_QUIESCED;
  902. spin_unlock_irq(&qhp->lock);
  903. return 0;
  904. }
  905. int iwch_quiesce_qps(struct iwch_cq *chp)
  906. {
  907. int i;
  908. struct iwch_qp *qhp;
  909. for (i=0; i < T3_MAX_NUM_QP; i++) {
  910. qhp = get_qhp(chp->rhp, i);
  911. if (!qhp)
  912. continue;
  913. if ((qhp->attr.rcq == chp->cq.cqid) && !qp_quiesced(qhp)) {
  914. quiesce_qp(qhp);
  915. continue;
  916. }
  917. if ((qhp->attr.scq == chp->cq.cqid) && !qp_quiesced(qhp))
  918. quiesce_qp(qhp);
  919. }
  920. return 0;
  921. }
  922. int iwch_resume_qps(struct iwch_cq *chp)
  923. {
  924. int i;
  925. struct iwch_qp *qhp;
  926. for (i=0; i < T3_MAX_NUM_QP; i++) {
  927. qhp = get_qhp(chp->rhp, i);
  928. if (!qhp)
  929. continue;
  930. if ((qhp->attr.rcq == chp->cq.cqid) && qp_quiesced(qhp)) {
  931. resume_qp(qhp);
  932. continue;
  933. }
  934. if ((qhp->attr.scq == chp->cq.cqid) && qp_quiesced(qhp))
  935. resume_qp(qhp);
  936. }
  937. return 0;
  938. }