iwch_qp.c 27 KB

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