qp.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. /*
  2. * Copyright (c) 2009-2010 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 "iw_cxgb4.h"
  33. static int destroy_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
  34. struct c4iw_dev_ucontext *uctx)
  35. {
  36. /*
  37. * uP clears EQ contexts when the connection exits rdma mode,
  38. * so no need to post a RESET WR for these EQs.
  39. */
  40. dma_free_coherent(&(rdev->lldi.pdev->dev),
  41. wq->rq.memsize, wq->rq.queue,
  42. dma_unmap_addr(&wq->rq, mapping));
  43. dma_free_coherent(&(rdev->lldi.pdev->dev),
  44. wq->sq.memsize, wq->sq.queue,
  45. dma_unmap_addr(&wq->sq, mapping));
  46. c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
  47. kfree(wq->rq.sw_rq);
  48. kfree(wq->sq.sw_sq);
  49. c4iw_put_qpid(rdev, wq->rq.qid, uctx);
  50. c4iw_put_qpid(rdev, wq->sq.qid, uctx);
  51. return 0;
  52. }
  53. static int create_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
  54. struct t4_cq *rcq, struct t4_cq *scq,
  55. struct c4iw_dev_ucontext *uctx)
  56. {
  57. int user = (uctx != &rdev->uctx);
  58. struct fw_ri_res_wr *res_wr;
  59. struct fw_ri_res *res;
  60. int wr_len;
  61. struct c4iw_wr_wait wr_wait;
  62. struct sk_buff *skb;
  63. int ret;
  64. int eqsize;
  65. wq->sq.qid = c4iw_get_qpid(rdev, uctx);
  66. if (!wq->sq.qid)
  67. return -ENOMEM;
  68. wq->rq.qid = c4iw_get_qpid(rdev, uctx);
  69. if (!wq->rq.qid)
  70. goto err1;
  71. if (!user) {
  72. wq->sq.sw_sq = kzalloc(wq->sq.size * sizeof *wq->sq.sw_sq,
  73. GFP_KERNEL);
  74. if (!wq->sq.sw_sq)
  75. goto err2;
  76. wq->rq.sw_rq = kzalloc(wq->rq.size * sizeof *wq->rq.sw_rq,
  77. GFP_KERNEL);
  78. if (!wq->rq.sw_rq)
  79. goto err3;
  80. }
  81. /*
  82. * RQT must be a power of 2.
  83. */
  84. wq->rq.rqt_size = roundup_pow_of_two(wq->rq.size);
  85. wq->rq.rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rq.rqt_size);
  86. if (!wq->rq.rqt_hwaddr)
  87. goto err4;
  88. wq->sq.queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev),
  89. wq->sq.memsize, &(wq->sq.dma_addr),
  90. GFP_KERNEL);
  91. if (!wq->sq.queue)
  92. goto err5;
  93. memset(wq->sq.queue, 0, wq->sq.memsize);
  94. dma_unmap_addr_set(&wq->sq, mapping, wq->sq.dma_addr);
  95. wq->rq.queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev),
  96. wq->rq.memsize, &(wq->rq.dma_addr),
  97. GFP_KERNEL);
  98. if (!wq->rq.queue)
  99. goto err6;
  100. PDBG("%s sq base va 0x%p pa 0x%llx rq base va 0x%p pa 0x%llx\n",
  101. __func__, wq->sq.queue,
  102. (unsigned long long)virt_to_phys(wq->sq.queue),
  103. wq->rq.queue,
  104. (unsigned long long)virt_to_phys(wq->rq.queue));
  105. memset(wq->rq.queue, 0, wq->rq.memsize);
  106. dma_unmap_addr_set(&wq->rq, mapping, wq->rq.dma_addr);
  107. wq->db = rdev->lldi.db_reg;
  108. wq->gts = rdev->lldi.gts_reg;
  109. if (user) {
  110. wq->sq.udb = (u64)pci_resource_start(rdev->lldi.pdev, 2) +
  111. (wq->sq.qid << rdev->qpshift);
  112. wq->sq.udb &= PAGE_MASK;
  113. wq->rq.udb = (u64)pci_resource_start(rdev->lldi.pdev, 2) +
  114. (wq->rq.qid << rdev->qpshift);
  115. wq->rq.udb &= PAGE_MASK;
  116. }
  117. wq->rdev = rdev;
  118. wq->rq.msn = 1;
  119. /* build fw_ri_res_wr */
  120. wr_len = sizeof *res_wr + 2 * sizeof *res;
  121. skb = alloc_skb(wr_len, GFP_KERNEL);
  122. if (!skb) {
  123. ret = -ENOMEM;
  124. goto err7;
  125. }
  126. set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
  127. res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
  128. memset(res_wr, 0, wr_len);
  129. res_wr->op_nres = cpu_to_be32(
  130. FW_WR_OP(FW_RI_RES_WR) |
  131. V_FW_RI_RES_WR_NRES(2) |
  132. FW_WR_COMPL(1));
  133. res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
  134. res_wr->cookie = (unsigned long) &wr_wait;
  135. res = res_wr->res;
  136. res->u.sqrq.restype = FW_RI_RES_TYPE_SQ;
  137. res->u.sqrq.op = FW_RI_RES_OP_WRITE;
  138. /*
  139. * eqsize is the number of 64B entries plus the status page size.
  140. */
  141. eqsize = wq->sq.size * T4_SQ_NUM_SLOTS + T4_EQ_STATUS_ENTRIES;
  142. res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
  143. V_FW_RI_RES_WR_HOSTFCMODE(0) | /* no host cidx updates */
  144. V_FW_RI_RES_WR_CPRIO(0) | /* don't keep in chip cache */
  145. V_FW_RI_RES_WR_PCIECHN(0) | /* set by uP at ri_init time */
  146. V_FW_RI_RES_WR_IQID(scq->cqid));
  147. res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
  148. V_FW_RI_RES_WR_DCAEN(0) |
  149. V_FW_RI_RES_WR_DCACPU(0) |
  150. V_FW_RI_RES_WR_FBMIN(2) |
  151. V_FW_RI_RES_WR_FBMAX(3) |
  152. V_FW_RI_RES_WR_CIDXFTHRESHO(0) |
  153. V_FW_RI_RES_WR_CIDXFTHRESH(0) |
  154. V_FW_RI_RES_WR_EQSIZE(eqsize));
  155. res->u.sqrq.eqid = cpu_to_be32(wq->sq.qid);
  156. res->u.sqrq.eqaddr = cpu_to_be64(wq->sq.dma_addr);
  157. res++;
  158. res->u.sqrq.restype = FW_RI_RES_TYPE_RQ;
  159. res->u.sqrq.op = FW_RI_RES_OP_WRITE;
  160. /*
  161. * eqsize is the number of 64B entries plus the status page size.
  162. */
  163. eqsize = wq->rq.size * T4_RQ_NUM_SLOTS + T4_EQ_STATUS_ENTRIES;
  164. res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
  165. V_FW_RI_RES_WR_HOSTFCMODE(0) | /* no host cidx updates */
  166. V_FW_RI_RES_WR_CPRIO(0) | /* don't keep in chip cache */
  167. V_FW_RI_RES_WR_PCIECHN(0) | /* set by uP at ri_init time */
  168. V_FW_RI_RES_WR_IQID(rcq->cqid));
  169. res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
  170. V_FW_RI_RES_WR_DCAEN(0) |
  171. V_FW_RI_RES_WR_DCACPU(0) |
  172. V_FW_RI_RES_WR_FBMIN(2) |
  173. V_FW_RI_RES_WR_FBMAX(3) |
  174. V_FW_RI_RES_WR_CIDXFTHRESHO(0) |
  175. V_FW_RI_RES_WR_CIDXFTHRESH(0) |
  176. V_FW_RI_RES_WR_EQSIZE(eqsize));
  177. res->u.sqrq.eqid = cpu_to_be32(wq->rq.qid);
  178. res->u.sqrq.eqaddr = cpu_to_be64(wq->rq.dma_addr);
  179. c4iw_init_wr_wait(&wr_wait);
  180. ret = c4iw_ofld_send(rdev, skb);
  181. if (ret)
  182. goto err7;
  183. wait_event_timeout(wr_wait.wait, wr_wait.done, C4IW_WR_TO);
  184. if (!wr_wait.done) {
  185. printk(KERN_ERR MOD "Device %s not responding!\n",
  186. pci_name(rdev->lldi.pdev));
  187. rdev->flags = T4_FATAL_ERROR;
  188. ret = -EIO;
  189. } else
  190. ret = wr_wait.ret;
  191. if (ret)
  192. goto err7;
  193. PDBG("%s sqid 0x%x rqid 0x%x kdb 0x%p squdb 0x%llx rqudb 0x%llx\n",
  194. __func__, wq->sq.qid, wq->rq.qid, wq->db,
  195. (unsigned long long)wq->sq.udb, (unsigned long long)wq->rq.udb);
  196. return 0;
  197. err7:
  198. dma_free_coherent(&(rdev->lldi.pdev->dev),
  199. wq->rq.memsize, wq->rq.queue,
  200. dma_unmap_addr(&wq->rq, mapping));
  201. err6:
  202. dma_free_coherent(&(rdev->lldi.pdev->dev),
  203. wq->sq.memsize, wq->sq.queue,
  204. dma_unmap_addr(&wq->sq, mapping));
  205. err5:
  206. c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
  207. err4:
  208. kfree(wq->rq.sw_rq);
  209. err3:
  210. kfree(wq->sq.sw_sq);
  211. err2:
  212. c4iw_put_qpid(rdev, wq->rq.qid, uctx);
  213. err1:
  214. c4iw_put_qpid(rdev, wq->sq.qid, uctx);
  215. return -ENOMEM;
  216. }
  217. static int build_immd(struct t4_sq *sq, struct fw_ri_immd *immdp,
  218. struct ib_send_wr *wr, int max, u32 *plenp)
  219. {
  220. u8 *dstp, *srcp;
  221. u32 plen = 0;
  222. int i;
  223. int rem, len;
  224. dstp = (u8 *)immdp->data;
  225. for (i = 0; i < wr->num_sge; i++) {
  226. if ((plen + wr->sg_list[i].length) > max)
  227. return -EMSGSIZE;
  228. srcp = (u8 *)(unsigned long)wr->sg_list[i].addr;
  229. plen += wr->sg_list[i].length;
  230. rem = wr->sg_list[i].length;
  231. while (rem) {
  232. if (dstp == (u8 *)&sq->queue[sq->size])
  233. dstp = (u8 *)sq->queue;
  234. if (rem <= (u8 *)&sq->queue[sq->size] - dstp)
  235. len = rem;
  236. else
  237. len = (u8 *)&sq->queue[sq->size] - dstp;
  238. memcpy(dstp, srcp, len);
  239. dstp += len;
  240. srcp += len;
  241. rem -= len;
  242. }
  243. }
  244. len = roundup(plen + sizeof *immdp, 16) - (plen + sizeof *immdp);
  245. if (len)
  246. memset(dstp, 0, len);
  247. immdp->op = FW_RI_DATA_IMMD;
  248. immdp->r1 = 0;
  249. immdp->r2 = 0;
  250. immdp->immdlen = cpu_to_be32(plen);
  251. *plenp = plen;
  252. return 0;
  253. }
  254. static int build_isgl(__be64 *queue_start, __be64 *queue_end,
  255. struct fw_ri_isgl *isglp, struct ib_sge *sg_list,
  256. int num_sge, u32 *plenp)
  257. {
  258. int i;
  259. u32 plen = 0;
  260. __be64 *flitp = (__be64 *)isglp->sge;
  261. for (i = 0; i < num_sge; i++) {
  262. if ((plen + sg_list[i].length) < plen)
  263. return -EMSGSIZE;
  264. plen += sg_list[i].length;
  265. *flitp = cpu_to_be64(((u64)sg_list[i].lkey << 32) |
  266. sg_list[i].length);
  267. if (++flitp == queue_end)
  268. flitp = queue_start;
  269. *flitp = cpu_to_be64(sg_list[i].addr);
  270. if (++flitp == queue_end)
  271. flitp = queue_start;
  272. }
  273. *flitp = (__force __be64)0;
  274. isglp->op = FW_RI_DATA_ISGL;
  275. isglp->r1 = 0;
  276. isglp->nsge = cpu_to_be16(num_sge);
  277. isglp->r2 = 0;
  278. if (plenp)
  279. *plenp = plen;
  280. return 0;
  281. }
  282. static int build_rdma_send(struct t4_sq *sq, union t4_wr *wqe,
  283. struct ib_send_wr *wr, u8 *len16)
  284. {
  285. u32 plen;
  286. int size;
  287. int ret;
  288. if (wr->num_sge > T4_MAX_SEND_SGE)
  289. return -EINVAL;
  290. switch (wr->opcode) {
  291. case IB_WR_SEND:
  292. if (wr->send_flags & IB_SEND_SOLICITED)
  293. wqe->send.sendop_pkd = cpu_to_be32(
  294. V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_SE));
  295. else
  296. wqe->send.sendop_pkd = cpu_to_be32(
  297. V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND));
  298. wqe->send.stag_inv = 0;
  299. break;
  300. case IB_WR_SEND_WITH_INV:
  301. if (wr->send_flags & IB_SEND_SOLICITED)
  302. wqe->send.sendop_pkd = cpu_to_be32(
  303. V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_SE_INV));
  304. else
  305. wqe->send.sendop_pkd = cpu_to_be32(
  306. V_FW_RI_SEND_WR_SENDOP(FW_RI_SEND_WITH_INV));
  307. wqe->send.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
  308. break;
  309. default:
  310. return -EINVAL;
  311. }
  312. plen = 0;
  313. if (wr->num_sge) {
  314. if (wr->send_flags & IB_SEND_INLINE) {
  315. ret = build_immd(sq, wqe->send.u.immd_src, wr,
  316. T4_MAX_SEND_INLINE, &plen);
  317. if (ret)
  318. return ret;
  319. size = sizeof wqe->send + sizeof(struct fw_ri_immd) +
  320. plen;
  321. } else {
  322. ret = build_isgl((__be64 *)sq->queue,
  323. (__be64 *)&sq->queue[sq->size],
  324. wqe->send.u.isgl_src,
  325. wr->sg_list, wr->num_sge, &plen);
  326. if (ret)
  327. return ret;
  328. size = sizeof wqe->send + sizeof(struct fw_ri_isgl) +
  329. wr->num_sge * sizeof(struct fw_ri_sge);
  330. }
  331. } else {
  332. wqe->send.u.immd_src[0].op = FW_RI_DATA_IMMD;
  333. wqe->send.u.immd_src[0].r1 = 0;
  334. wqe->send.u.immd_src[0].r2 = 0;
  335. wqe->send.u.immd_src[0].immdlen = 0;
  336. size = sizeof wqe->send + sizeof(struct fw_ri_immd);
  337. plen = 0;
  338. }
  339. *len16 = DIV_ROUND_UP(size, 16);
  340. wqe->send.plen = cpu_to_be32(plen);
  341. return 0;
  342. }
  343. static int build_rdma_write(struct t4_sq *sq, union t4_wr *wqe,
  344. struct ib_send_wr *wr, u8 *len16)
  345. {
  346. u32 plen;
  347. int size;
  348. int ret;
  349. if (wr->num_sge > T4_MAX_SEND_SGE)
  350. return -EINVAL;
  351. wqe->write.r2 = 0;
  352. wqe->write.stag_sink = cpu_to_be32(wr->wr.rdma.rkey);
  353. wqe->write.to_sink = cpu_to_be64(wr->wr.rdma.remote_addr);
  354. if (wr->num_sge) {
  355. if (wr->send_flags & IB_SEND_INLINE) {
  356. ret = build_immd(sq, wqe->write.u.immd_src, wr,
  357. T4_MAX_WRITE_INLINE, &plen);
  358. if (ret)
  359. return ret;
  360. size = sizeof wqe->write + sizeof(struct fw_ri_immd) +
  361. plen;
  362. } else {
  363. ret = build_isgl((__be64 *)sq->queue,
  364. (__be64 *)&sq->queue[sq->size],
  365. wqe->write.u.isgl_src,
  366. wr->sg_list, wr->num_sge, &plen);
  367. if (ret)
  368. return ret;
  369. size = sizeof wqe->write + sizeof(struct fw_ri_isgl) +
  370. wr->num_sge * sizeof(struct fw_ri_sge);
  371. }
  372. } else {
  373. wqe->write.u.immd_src[0].op = FW_RI_DATA_IMMD;
  374. wqe->write.u.immd_src[0].r1 = 0;
  375. wqe->write.u.immd_src[0].r2 = 0;
  376. wqe->write.u.immd_src[0].immdlen = 0;
  377. size = sizeof wqe->write + sizeof(struct fw_ri_immd);
  378. plen = 0;
  379. }
  380. *len16 = DIV_ROUND_UP(size, 16);
  381. wqe->write.plen = cpu_to_be32(plen);
  382. return 0;
  383. }
  384. static int build_rdma_read(union t4_wr *wqe, struct ib_send_wr *wr, u8 *len16)
  385. {
  386. if (wr->num_sge > 1)
  387. return -EINVAL;
  388. if (wr->num_sge) {
  389. wqe->read.stag_src = cpu_to_be32(wr->wr.rdma.rkey);
  390. wqe->read.to_src_hi = cpu_to_be32((u32)(wr->wr.rdma.remote_addr
  391. >> 32));
  392. wqe->read.to_src_lo = cpu_to_be32((u32)wr->wr.rdma.remote_addr);
  393. wqe->read.stag_sink = cpu_to_be32(wr->sg_list[0].lkey);
  394. wqe->read.plen = cpu_to_be32(wr->sg_list[0].length);
  395. wqe->read.to_sink_hi = cpu_to_be32((u32)(wr->sg_list[0].addr
  396. >> 32));
  397. wqe->read.to_sink_lo = cpu_to_be32((u32)(wr->sg_list[0].addr));
  398. } else {
  399. wqe->read.stag_src = cpu_to_be32(2);
  400. wqe->read.to_src_hi = 0;
  401. wqe->read.to_src_lo = 0;
  402. wqe->read.stag_sink = cpu_to_be32(2);
  403. wqe->read.plen = 0;
  404. wqe->read.to_sink_hi = 0;
  405. wqe->read.to_sink_lo = 0;
  406. }
  407. wqe->read.r2 = 0;
  408. wqe->read.r5 = 0;
  409. *len16 = DIV_ROUND_UP(sizeof wqe->read, 16);
  410. return 0;
  411. }
  412. static int build_rdma_recv(struct c4iw_qp *qhp, union t4_recv_wr *wqe,
  413. struct ib_recv_wr *wr, u8 *len16)
  414. {
  415. int ret;
  416. ret = build_isgl((__be64 *)qhp->wq.rq.queue,
  417. (__be64 *)&qhp->wq.rq.queue[qhp->wq.rq.size],
  418. &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
  419. if (ret)
  420. return ret;
  421. *len16 = DIV_ROUND_UP(sizeof wqe->recv +
  422. wr->num_sge * sizeof(struct fw_ri_sge), 16);
  423. return 0;
  424. }
  425. static int build_fastreg(union t4_wr *wqe, struct ib_send_wr *wr, u8 *len16)
  426. {
  427. struct fw_ri_immd *imdp;
  428. __be64 *p;
  429. int i;
  430. int pbllen = roundup(wr->wr.fast_reg.page_list_len * sizeof(u64), 32);
  431. if (wr->wr.fast_reg.page_list_len > T4_MAX_FR_DEPTH)
  432. return -EINVAL;
  433. wqe->fr.qpbinde_to_dcacpu = 0;
  434. wqe->fr.pgsz_shift = wr->wr.fast_reg.page_shift - 12;
  435. wqe->fr.addr_type = FW_RI_VA_BASED_TO;
  436. wqe->fr.mem_perms = c4iw_ib_to_tpt_access(wr->wr.fast_reg.access_flags);
  437. wqe->fr.len_hi = 0;
  438. wqe->fr.len_lo = cpu_to_be32(wr->wr.fast_reg.length);
  439. wqe->fr.stag = cpu_to_be32(wr->wr.fast_reg.rkey);
  440. wqe->fr.va_hi = cpu_to_be32(wr->wr.fast_reg.iova_start >> 32);
  441. wqe->fr.va_lo_fbo = cpu_to_be32(wr->wr.fast_reg.iova_start &
  442. 0xffffffff);
  443. if (pbllen > T4_MAX_FR_IMMD) {
  444. struct c4iw_fr_page_list *c4pl =
  445. to_c4iw_fr_page_list(wr->wr.fast_reg.page_list);
  446. struct fw_ri_dsgl *sglp;
  447. sglp = (struct fw_ri_dsgl *)(&wqe->fr + 1);
  448. sglp->op = FW_RI_DATA_DSGL;
  449. sglp->r1 = 0;
  450. sglp->nsge = cpu_to_be16(1);
  451. sglp->addr0 = cpu_to_be64(c4pl->dma_addr);
  452. sglp->len0 = cpu_to_be32(pbllen);
  453. *len16 = DIV_ROUND_UP(sizeof wqe->fr + sizeof *sglp, 16);
  454. } else {
  455. imdp = (struct fw_ri_immd *)(&wqe->fr + 1);
  456. imdp->op = FW_RI_DATA_IMMD;
  457. imdp->r1 = 0;
  458. imdp->r2 = 0;
  459. imdp->immdlen = cpu_to_be32(pbllen);
  460. p = (__be64 *)(imdp + 1);
  461. for (i = 0; i < wr->wr.fast_reg.page_list_len; i++, p++)
  462. *p = cpu_to_be64(
  463. (u64)wr->wr.fast_reg.page_list->page_list[i]);
  464. *len16 = DIV_ROUND_UP(sizeof wqe->fr + sizeof *imdp + pbllen,
  465. 16);
  466. }
  467. return 0;
  468. }
  469. static int build_inv_stag(union t4_wr *wqe, struct ib_send_wr *wr,
  470. u8 *len16)
  471. {
  472. wqe->inv.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
  473. wqe->inv.r2 = 0;
  474. *len16 = DIV_ROUND_UP(sizeof wqe->inv, 16);
  475. return 0;
  476. }
  477. void c4iw_qp_add_ref(struct ib_qp *qp)
  478. {
  479. PDBG("%s ib_qp %p\n", __func__, qp);
  480. atomic_inc(&(to_c4iw_qp(qp)->refcnt));
  481. }
  482. void c4iw_qp_rem_ref(struct ib_qp *qp)
  483. {
  484. PDBG("%s ib_qp %p\n", __func__, qp);
  485. if (atomic_dec_and_test(&(to_c4iw_qp(qp)->refcnt)))
  486. wake_up(&(to_c4iw_qp(qp)->wait));
  487. }
  488. int c4iw_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  489. struct ib_send_wr **bad_wr)
  490. {
  491. int err = 0;
  492. u8 len16 = 0;
  493. enum fw_wr_opcodes fw_opcode = 0;
  494. enum fw_ri_wr_flags fw_flags;
  495. struct c4iw_qp *qhp;
  496. union t4_wr *wqe;
  497. u32 num_wrs;
  498. struct t4_swsqe *swsqe;
  499. unsigned long flag;
  500. u16 idx = 0;
  501. qhp = to_c4iw_qp(ibqp);
  502. spin_lock_irqsave(&qhp->lock, flag);
  503. if (t4_wq_in_error(&qhp->wq)) {
  504. spin_unlock_irqrestore(&qhp->lock, flag);
  505. return -EINVAL;
  506. }
  507. num_wrs = t4_sq_avail(&qhp->wq);
  508. if (num_wrs == 0) {
  509. spin_unlock_irqrestore(&qhp->lock, flag);
  510. return -ENOMEM;
  511. }
  512. while (wr) {
  513. if (num_wrs == 0) {
  514. err = -ENOMEM;
  515. *bad_wr = wr;
  516. break;
  517. }
  518. wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
  519. qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
  520. fw_flags = 0;
  521. if (wr->send_flags & IB_SEND_SOLICITED)
  522. fw_flags |= FW_RI_SOLICITED_EVENT_FLAG;
  523. if (wr->send_flags & IB_SEND_SIGNALED)
  524. fw_flags |= FW_RI_COMPLETION_FLAG;
  525. swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
  526. switch (wr->opcode) {
  527. case IB_WR_SEND_WITH_INV:
  528. case IB_WR_SEND:
  529. if (wr->send_flags & IB_SEND_FENCE)
  530. fw_flags |= FW_RI_READ_FENCE_FLAG;
  531. fw_opcode = FW_RI_SEND_WR;
  532. if (wr->opcode == IB_WR_SEND)
  533. swsqe->opcode = FW_RI_SEND;
  534. else
  535. swsqe->opcode = FW_RI_SEND_WITH_INV;
  536. err = build_rdma_send(&qhp->wq.sq, wqe, wr, &len16);
  537. break;
  538. case IB_WR_RDMA_WRITE:
  539. fw_opcode = FW_RI_RDMA_WRITE_WR;
  540. swsqe->opcode = FW_RI_RDMA_WRITE;
  541. err = build_rdma_write(&qhp->wq.sq, wqe, wr, &len16);
  542. break;
  543. case IB_WR_RDMA_READ:
  544. case IB_WR_RDMA_READ_WITH_INV:
  545. fw_opcode = FW_RI_RDMA_READ_WR;
  546. swsqe->opcode = FW_RI_READ_REQ;
  547. if (wr->opcode == IB_WR_RDMA_READ_WITH_INV)
  548. fw_flags |= FW_RI_RDMA_READ_INVALIDATE;
  549. else
  550. fw_flags = 0;
  551. err = build_rdma_read(wqe, wr, &len16);
  552. if (err)
  553. break;
  554. swsqe->read_len = wr->sg_list[0].length;
  555. if (!qhp->wq.sq.oldest_read)
  556. qhp->wq.sq.oldest_read = swsqe;
  557. break;
  558. case IB_WR_FAST_REG_MR:
  559. fw_opcode = FW_RI_FR_NSMR_WR;
  560. swsqe->opcode = FW_RI_FAST_REGISTER;
  561. err = build_fastreg(wqe, wr, &len16);
  562. break;
  563. case IB_WR_LOCAL_INV:
  564. if (wr->send_flags & IB_SEND_FENCE)
  565. fw_flags |= FW_RI_LOCAL_FENCE_FLAG;
  566. fw_opcode = FW_RI_INV_LSTAG_WR;
  567. swsqe->opcode = FW_RI_LOCAL_INV;
  568. err = build_inv_stag(wqe, wr, &len16);
  569. break;
  570. default:
  571. PDBG("%s post of type=%d TBD!\n", __func__,
  572. wr->opcode);
  573. err = -EINVAL;
  574. }
  575. if (err) {
  576. *bad_wr = wr;
  577. break;
  578. }
  579. swsqe->idx = qhp->wq.sq.pidx;
  580. swsqe->complete = 0;
  581. swsqe->signaled = (wr->send_flags & IB_SEND_SIGNALED);
  582. swsqe->wr_id = wr->wr_id;
  583. init_wr_hdr(wqe, qhp->wq.sq.pidx, fw_opcode, fw_flags, len16);
  584. PDBG("%s cookie 0x%llx pidx 0x%x opcode 0x%x read_len %u\n",
  585. __func__, (unsigned long long)wr->wr_id, qhp->wq.sq.pidx,
  586. swsqe->opcode, swsqe->read_len);
  587. wr = wr->next;
  588. num_wrs--;
  589. t4_sq_produce(&qhp->wq, len16);
  590. idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
  591. }
  592. if (t4_wq_db_enabled(&qhp->wq))
  593. t4_ring_sq_db(&qhp->wq, idx);
  594. spin_unlock_irqrestore(&qhp->lock, flag);
  595. return err;
  596. }
  597. int c4iw_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  598. struct ib_recv_wr **bad_wr)
  599. {
  600. int err = 0;
  601. struct c4iw_qp *qhp;
  602. union t4_recv_wr *wqe;
  603. u32 num_wrs;
  604. u8 len16 = 0;
  605. unsigned long flag;
  606. u16 idx = 0;
  607. qhp = to_c4iw_qp(ibqp);
  608. spin_lock_irqsave(&qhp->lock, flag);
  609. if (t4_wq_in_error(&qhp->wq)) {
  610. spin_unlock_irqrestore(&qhp->lock, flag);
  611. return -EINVAL;
  612. }
  613. num_wrs = t4_rq_avail(&qhp->wq);
  614. if (num_wrs == 0) {
  615. spin_unlock_irqrestore(&qhp->lock, flag);
  616. return -ENOMEM;
  617. }
  618. while (wr) {
  619. if (wr->num_sge > T4_MAX_RECV_SGE) {
  620. err = -EINVAL;
  621. *bad_wr = wr;
  622. break;
  623. }
  624. wqe = (union t4_recv_wr *)((u8 *)qhp->wq.rq.queue +
  625. qhp->wq.rq.wq_pidx *
  626. T4_EQ_ENTRY_SIZE);
  627. if (num_wrs)
  628. err = build_rdma_recv(qhp, wqe, wr, &len16);
  629. else
  630. err = -ENOMEM;
  631. if (err) {
  632. *bad_wr = wr;
  633. break;
  634. }
  635. qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].wr_id = wr->wr_id;
  636. wqe->recv.opcode = FW_RI_RECV_WR;
  637. wqe->recv.r1 = 0;
  638. wqe->recv.wrid = qhp->wq.rq.pidx;
  639. wqe->recv.r2[0] = 0;
  640. wqe->recv.r2[1] = 0;
  641. wqe->recv.r2[2] = 0;
  642. wqe->recv.len16 = len16;
  643. PDBG("%s cookie 0x%llx pidx %u\n", __func__,
  644. (unsigned long long) wr->wr_id, qhp->wq.rq.pidx);
  645. t4_rq_produce(&qhp->wq, len16);
  646. idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
  647. wr = wr->next;
  648. num_wrs--;
  649. }
  650. if (t4_wq_db_enabled(&qhp->wq))
  651. t4_ring_rq_db(&qhp->wq, idx);
  652. spin_unlock_irqrestore(&qhp->lock, flag);
  653. return err;
  654. }
  655. int c4iw_bind_mw(struct ib_qp *qp, struct ib_mw *mw, struct ib_mw_bind *mw_bind)
  656. {
  657. return -ENOSYS;
  658. }
  659. static inline void build_term_codes(struct t4_cqe *err_cqe, u8 *layer_type,
  660. u8 *ecode)
  661. {
  662. int status;
  663. int tagged;
  664. int opcode;
  665. int rqtype;
  666. int send_inv;
  667. if (!err_cqe) {
  668. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  669. *ecode = 0;
  670. return;
  671. }
  672. status = CQE_STATUS(err_cqe);
  673. opcode = CQE_OPCODE(err_cqe);
  674. rqtype = RQ_TYPE(err_cqe);
  675. send_inv = (opcode == FW_RI_SEND_WITH_INV) ||
  676. (opcode == FW_RI_SEND_WITH_SE_INV);
  677. tagged = (opcode == FW_RI_RDMA_WRITE) ||
  678. (rqtype && (opcode == FW_RI_READ_RESP));
  679. switch (status) {
  680. case T4_ERR_STAG:
  681. if (send_inv) {
  682. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  683. *ecode = RDMAP_CANT_INV_STAG;
  684. } else {
  685. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  686. *ecode = RDMAP_INV_STAG;
  687. }
  688. break;
  689. case T4_ERR_PDID:
  690. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  691. if ((opcode == FW_RI_SEND_WITH_INV) ||
  692. (opcode == FW_RI_SEND_WITH_SE_INV))
  693. *ecode = RDMAP_CANT_INV_STAG;
  694. else
  695. *ecode = RDMAP_STAG_NOT_ASSOC;
  696. break;
  697. case T4_ERR_QPID:
  698. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  699. *ecode = RDMAP_STAG_NOT_ASSOC;
  700. break;
  701. case T4_ERR_ACCESS:
  702. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  703. *ecode = RDMAP_ACC_VIOL;
  704. break;
  705. case T4_ERR_WRAP:
  706. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  707. *ecode = RDMAP_TO_WRAP;
  708. break;
  709. case T4_ERR_BOUND:
  710. if (tagged) {
  711. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  712. *ecode = DDPT_BASE_BOUNDS;
  713. } else {
  714. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
  715. *ecode = RDMAP_BASE_BOUNDS;
  716. }
  717. break;
  718. case T4_ERR_INVALIDATE_SHARED_MR:
  719. case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
  720. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  721. *ecode = RDMAP_CANT_INV_STAG;
  722. break;
  723. case T4_ERR_ECC:
  724. case T4_ERR_ECC_PSTAG:
  725. case T4_ERR_INTERNAL_ERR:
  726. *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
  727. *ecode = 0;
  728. break;
  729. case T4_ERR_OUT_OF_RQE:
  730. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  731. *ecode = DDPU_INV_MSN_NOBUF;
  732. break;
  733. case T4_ERR_PBL_ADDR_BOUND:
  734. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  735. *ecode = DDPT_BASE_BOUNDS;
  736. break;
  737. case T4_ERR_CRC:
  738. *layer_type = LAYER_MPA|DDP_LLP;
  739. *ecode = MPA_CRC_ERR;
  740. break;
  741. case T4_ERR_MARKER:
  742. *layer_type = LAYER_MPA|DDP_LLP;
  743. *ecode = MPA_MARKER_ERR;
  744. break;
  745. case T4_ERR_PDU_LEN_ERR:
  746. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  747. *ecode = DDPU_MSG_TOOBIG;
  748. break;
  749. case T4_ERR_DDP_VERSION:
  750. if (tagged) {
  751. *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
  752. *ecode = DDPT_INV_VERS;
  753. } else {
  754. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  755. *ecode = DDPU_INV_VERS;
  756. }
  757. break;
  758. case T4_ERR_RDMA_VERSION:
  759. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  760. *ecode = RDMAP_INV_VERS;
  761. break;
  762. case T4_ERR_OPCODE:
  763. *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
  764. *ecode = RDMAP_INV_OPCODE;
  765. break;
  766. case T4_ERR_DDP_QUEUE_NUM:
  767. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  768. *ecode = DDPU_INV_QN;
  769. break;
  770. case T4_ERR_MSN:
  771. case T4_ERR_MSN_GAP:
  772. case T4_ERR_MSN_RANGE:
  773. case T4_ERR_IRD_OVERFLOW:
  774. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  775. *ecode = DDPU_INV_MSN_RANGE;
  776. break;
  777. case T4_ERR_TBIT:
  778. *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
  779. *ecode = 0;
  780. break;
  781. case T4_ERR_MO:
  782. *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
  783. *ecode = DDPU_INV_MO;
  784. break;
  785. default:
  786. *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
  787. *ecode = 0;
  788. break;
  789. }
  790. }
  791. int c4iw_post_zb_read(struct c4iw_qp *qhp)
  792. {
  793. union t4_wr *wqe;
  794. struct sk_buff *skb;
  795. u8 len16;
  796. PDBG("%s enter\n", __func__);
  797. skb = alloc_skb(40, GFP_KERNEL);
  798. if (!skb) {
  799. printk(KERN_ERR "%s cannot send zb_read!!\n", __func__);
  800. return -ENOMEM;
  801. }
  802. set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
  803. wqe = (union t4_wr *)skb_put(skb, sizeof wqe->read);
  804. memset(wqe, 0, sizeof wqe->read);
  805. wqe->read.r2 = cpu_to_be64(0);
  806. wqe->read.stag_sink = cpu_to_be32(1);
  807. wqe->read.to_sink_hi = cpu_to_be32(0);
  808. wqe->read.to_sink_lo = cpu_to_be32(1);
  809. wqe->read.stag_src = cpu_to_be32(1);
  810. wqe->read.plen = cpu_to_be32(0);
  811. wqe->read.to_src_hi = cpu_to_be32(0);
  812. wqe->read.to_src_lo = cpu_to_be32(1);
  813. len16 = DIV_ROUND_UP(sizeof wqe->read, 16);
  814. init_wr_hdr(wqe, 0, FW_RI_RDMA_READ_WR, FW_RI_COMPLETION_FLAG, len16);
  815. return c4iw_ofld_send(&qhp->rhp->rdev, skb);
  816. }
  817. static void post_terminate(struct c4iw_qp *qhp, struct t4_cqe *err_cqe,
  818. gfp_t gfp)
  819. {
  820. struct fw_ri_wr *wqe;
  821. struct sk_buff *skb;
  822. struct terminate_message *term;
  823. PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
  824. qhp->ep->hwtid);
  825. skb = alloc_skb(sizeof *wqe, gfp);
  826. if (!skb)
  827. return;
  828. set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
  829. wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
  830. memset(wqe, 0, sizeof *wqe);
  831. wqe->op_compl = cpu_to_be32(FW_WR_OP(FW_RI_INIT_WR));
  832. wqe->flowid_len16 = cpu_to_be32(
  833. FW_WR_FLOWID(qhp->ep->hwtid) |
  834. FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
  835. wqe->u.terminate.type = FW_RI_TYPE_TERMINATE;
  836. wqe->u.terminate.immdlen = cpu_to_be32(sizeof *term);
  837. term = (struct terminate_message *)wqe->u.terminate.termmsg;
  838. build_term_codes(err_cqe, &term->layer_etype, &term->ecode);
  839. c4iw_ofld_send(&qhp->rhp->rdev, skb);
  840. }
  841. /*
  842. * Assumes qhp lock is held.
  843. */
  844. static void __flush_qp(struct c4iw_qp *qhp, struct c4iw_cq *rchp,
  845. struct c4iw_cq *schp, unsigned long *flag)
  846. {
  847. int count;
  848. int flushed;
  849. PDBG("%s qhp %p rchp %p schp %p\n", __func__, qhp, rchp, schp);
  850. /* take a ref on the qhp since we must release the lock */
  851. atomic_inc(&qhp->refcnt);
  852. spin_unlock_irqrestore(&qhp->lock, *flag);
  853. /* locking hierarchy: cq lock first, then qp lock. */
  854. spin_lock_irqsave(&rchp->lock, *flag);
  855. spin_lock(&qhp->lock);
  856. c4iw_flush_hw_cq(&rchp->cq);
  857. c4iw_count_rcqes(&rchp->cq, &qhp->wq, &count);
  858. flushed = c4iw_flush_rq(&qhp->wq, &rchp->cq, count);
  859. spin_unlock(&qhp->lock);
  860. spin_unlock_irqrestore(&rchp->lock, *flag);
  861. if (flushed)
  862. (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
  863. /* locking hierarchy: cq lock first, then qp lock. */
  864. spin_lock_irqsave(&schp->lock, *flag);
  865. spin_lock(&qhp->lock);
  866. c4iw_flush_hw_cq(&schp->cq);
  867. c4iw_count_scqes(&schp->cq, &qhp->wq, &count);
  868. flushed = c4iw_flush_sq(&qhp->wq, &schp->cq, count);
  869. spin_unlock(&qhp->lock);
  870. spin_unlock_irqrestore(&schp->lock, *flag);
  871. if (flushed)
  872. (*schp->ibcq.comp_handler)(&schp->ibcq, schp->ibcq.cq_context);
  873. /* deref */
  874. if (atomic_dec_and_test(&qhp->refcnt))
  875. wake_up(&qhp->wait);
  876. spin_lock_irqsave(&qhp->lock, *flag);
  877. }
  878. static void flush_qp(struct c4iw_qp *qhp, unsigned long *flag)
  879. {
  880. struct c4iw_cq *rchp, *schp;
  881. rchp = get_chp(qhp->rhp, qhp->attr.rcq);
  882. schp = get_chp(qhp->rhp, qhp->attr.scq);
  883. if (qhp->ibqp.uobject) {
  884. t4_set_wq_in_error(&qhp->wq);
  885. t4_set_cq_in_error(&rchp->cq);
  886. if (schp != rchp)
  887. t4_set_cq_in_error(&schp->cq);
  888. return;
  889. }
  890. __flush_qp(qhp, rchp, schp, flag);
  891. }
  892. static int rdma_fini(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
  893. struct c4iw_ep *ep)
  894. {
  895. struct fw_ri_wr *wqe;
  896. int ret;
  897. struct c4iw_wr_wait wr_wait;
  898. struct sk_buff *skb;
  899. PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
  900. ep->hwtid);
  901. skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
  902. if (!skb)
  903. return -ENOMEM;
  904. set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
  905. wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
  906. memset(wqe, 0, sizeof *wqe);
  907. wqe->op_compl = cpu_to_be32(
  908. FW_WR_OP(FW_RI_INIT_WR) |
  909. FW_WR_COMPL(1));
  910. wqe->flowid_len16 = cpu_to_be32(
  911. FW_WR_FLOWID(ep->hwtid) |
  912. FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
  913. wqe->cookie = (unsigned long) &wr_wait;
  914. wqe->u.fini.type = FW_RI_TYPE_FINI;
  915. c4iw_init_wr_wait(&wr_wait);
  916. ret = c4iw_ofld_send(&rhp->rdev, skb);
  917. if (ret)
  918. goto out;
  919. wait_event_timeout(wr_wait.wait, wr_wait.done, C4IW_WR_TO);
  920. if (!wr_wait.done) {
  921. printk(KERN_ERR MOD "Device %s not responding!\n",
  922. pci_name(rhp->rdev.lldi.pdev));
  923. rhp->rdev.flags = T4_FATAL_ERROR;
  924. ret = -EIO;
  925. } else {
  926. ret = wr_wait.ret;
  927. if (ret)
  928. printk(KERN_WARNING MOD
  929. "%s: Abnormal close qpid %d ret %u\n",
  930. pci_name(rhp->rdev.lldi.pdev), qhp->wq.sq.qid,
  931. ret);
  932. }
  933. out:
  934. PDBG("%s ret %d\n", __func__, ret);
  935. return ret;
  936. }
  937. static void build_rtr_msg(u8 p2p_type, struct fw_ri_init *init)
  938. {
  939. memset(&init->u, 0, sizeof init->u);
  940. switch (p2p_type) {
  941. case FW_RI_INIT_P2PTYPE_RDMA_WRITE:
  942. init->u.write.opcode = FW_RI_RDMA_WRITE_WR;
  943. init->u.write.stag_sink = cpu_to_be32(1);
  944. init->u.write.to_sink = cpu_to_be64(1);
  945. init->u.write.u.immd_src[0].op = FW_RI_DATA_IMMD;
  946. init->u.write.len16 = DIV_ROUND_UP(sizeof init->u.write +
  947. sizeof(struct fw_ri_immd),
  948. 16);
  949. break;
  950. case FW_RI_INIT_P2PTYPE_READ_REQ:
  951. init->u.write.opcode = FW_RI_RDMA_READ_WR;
  952. init->u.read.stag_src = cpu_to_be32(1);
  953. init->u.read.to_src_lo = cpu_to_be32(1);
  954. init->u.read.stag_sink = cpu_to_be32(1);
  955. init->u.read.to_sink_lo = cpu_to_be32(1);
  956. init->u.read.len16 = DIV_ROUND_UP(sizeof init->u.read, 16);
  957. break;
  958. }
  959. }
  960. static int rdma_init(struct c4iw_dev *rhp, struct c4iw_qp *qhp)
  961. {
  962. struct fw_ri_wr *wqe;
  963. int ret;
  964. struct c4iw_wr_wait wr_wait;
  965. struct sk_buff *skb;
  966. PDBG("%s qhp %p qid 0x%x tid %u\n", __func__, qhp, qhp->wq.sq.qid,
  967. qhp->ep->hwtid);
  968. skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
  969. if (!skb)
  970. return -ENOMEM;
  971. set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
  972. wqe = (struct fw_ri_wr *)__skb_put(skb, sizeof(*wqe));
  973. memset(wqe, 0, sizeof *wqe);
  974. wqe->op_compl = cpu_to_be32(
  975. FW_WR_OP(FW_RI_INIT_WR) |
  976. FW_WR_COMPL(1));
  977. wqe->flowid_len16 = cpu_to_be32(
  978. FW_WR_FLOWID(qhp->ep->hwtid) |
  979. FW_WR_LEN16(DIV_ROUND_UP(sizeof *wqe, 16)));
  980. wqe->cookie = (unsigned long) &wr_wait;
  981. wqe->u.init.type = FW_RI_TYPE_INIT;
  982. wqe->u.init.mpareqbit_p2ptype =
  983. V_FW_RI_WR_MPAREQBIT(qhp->attr.mpa_attr.initiator) |
  984. V_FW_RI_WR_P2PTYPE(qhp->attr.mpa_attr.p2p_type);
  985. wqe->u.init.mpa_attrs = FW_RI_MPA_IETF_ENABLE;
  986. if (qhp->attr.mpa_attr.recv_marker_enabled)
  987. wqe->u.init.mpa_attrs |= FW_RI_MPA_RX_MARKER_ENABLE;
  988. if (qhp->attr.mpa_attr.xmit_marker_enabled)
  989. wqe->u.init.mpa_attrs |= FW_RI_MPA_TX_MARKER_ENABLE;
  990. if (qhp->attr.mpa_attr.crc_enabled)
  991. wqe->u.init.mpa_attrs |= FW_RI_MPA_CRC_ENABLE;
  992. wqe->u.init.qp_caps = FW_RI_QP_RDMA_READ_ENABLE |
  993. FW_RI_QP_RDMA_WRITE_ENABLE |
  994. FW_RI_QP_BIND_ENABLE;
  995. if (!qhp->ibqp.uobject)
  996. wqe->u.init.qp_caps |= FW_RI_QP_FAST_REGISTER_ENABLE |
  997. FW_RI_QP_STAG0_ENABLE;
  998. wqe->u.init.nrqe = cpu_to_be16(t4_rqes_posted(&qhp->wq));
  999. wqe->u.init.pdid = cpu_to_be32(qhp->attr.pd);
  1000. wqe->u.init.qpid = cpu_to_be32(qhp->wq.sq.qid);
  1001. wqe->u.init.sq_eqid = cpu_to_be32(qhp->wq.sq.qid);
  1002. wqe->u.init.rq_eqid = cpu_to_be32(qhp->wq.rq.qid);
  1003. wqe->u.init.scqid = cpu_to_be32(qhp->attr.scq);
  1004. wqe->u.init.rcqid = cpu_to_be32(qhp->attr.rcq);
  1005. wqe->u.init.ord_max = cpu_to_be32(qhp->attr.max_ord);
  1006. wqe->u.init.ird_max = cpu_to_be32(qhp->attr.max_ird);
  1007. wqe->u.init.iss = cpu_to_be32(qhp->ep->snd_seq);
  1008. wqe->u.init.irs = cpu_to_be32(qhp->ep->rcv_seq);
  1009. wqe->u.init.hwrqsize = cpu_to_be32(qhp->wq.rq.rqt_size);
  1010. wqe->u.init.hwrqaddr = cpu_to_be32(qhp->wq.rq.rqt_hwaddr -
  1011. rhp->rdev.lldi.vr->rq.start);
  1012. if (qhp->attr.mpa_attr.initiator)
  1013. build_rtr_msg(qhp->attr.mpa_attr.p2p_type, &wqe->u.init);
  1014. c4iw_init_wr_wait(&wr_wait);
  1015. ret = c4iw_ofld_send(&rhp->rdev, skb);
  1016. if (ret)
  1017. goto out;
  1018. wait_event_timeout(wr_wait.wait, wr_wait.done, C4IW_WR_TO);
  1019. if (!wr_wait.done) {
  1020. printk(KERN_ERR MOD "Device %s not responding!\n",
  1021. pci_name(rhp->rdev.lldi.pdev));
  1022. rhp->rdev.flags = T4_FATAL_ERROR;
  1023. ret = -EIO;
  1024. } else
  1025. ret = wr_wait.ret;
  1026. out:
  1027. PDBG("%s ret %d\n", __func__, ret);
  1028. return ret;
  1029. }
  1030. int c4iw_modify_qp(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
  1031. enum c4iw_qp_attr_mask mask,
  1032. struct c4iw_qp_attributes *attrs,
  1033. int internal)
  1034. {
  1035. int ret = 0;
  1036. struct c4iw_qp_attributes newattr = qhp->attr;
  1037. unsigned long flag;
  1038. int disconnect = 0;
  1039. int terminate = 0;
  1040. int abort = 0;
  1041. int free = 0;
  1042. struct c4iw_ep *ep = NULL;
  1043. PDBG("%s qhp %p sqid 0x%x rqid 0x%x ep %p state %d -> %d\n", __func__,
  1044. qhp, qhp->wq.sq.qid, qhp->wq.rq.qid, qhp->ep, qhp->attr.state,
  1045. (mask & C4IW_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
  1046. spin_lock_irqsave(&qhp->lock, flag);
  1047. /* Process attr changes if in IDLE */
  1048. if (mask & C4IW_QP_ATTR_VALID_MODIFY) {
  1049. if (qhp->attr.state != C4IW_QP_STATE_IDLE) {
  1050. ret = -EIO;
  1051. goto out;
  1052. }
  1053. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_READ)
  1054. newattr.enable_rdma_read = attrs->enable_rdma_read;
  1055. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_WRITE)
  1056. newattr.enable_rdma_write = attrs->enable_rdma_write;
  1057. if (mask & C4IW_QP_ATTR_ENABLE_RDMA_BIND)
  1058. newattr.enable_bind = attrs->enable_bind;
  1059. if (mask & C4IW_QP_ATTR_MAX_ORD) {
  1060. if (attrs->max_ord > c4iw_max_read_depth) {
  1061. ret = -EINVAL;
  1062. goto out;
  1063. }
  1064. newattr.max_ord = attrs->max_ord;
  1065. }
  1066. if (mask & C4IW_QP_ATTR_MAX_IRD) {
  1067. if (attrs->max_ird > c4iw_max_read_depth) {
  1068. ret = -EINVAL;
  1069. goto out;
  1070. }
  1071. newattr.max_ird = attrs->max_ird;
  1072. }
  1073. qhp->attr = newattr;
  1074. }
  1075. if (!(mask & C4IW_QP_ATTR_NEXT_STATE))
  1076. goto out;
  1077. if (qhp->attr.state == attrs->next_state)
  1078. goto out;
  1079. switch (qhp->attr.state) {
  1080. case C4IW_QP_STATE_IDLE:
  1081. switch (attrs->next_state) {
  1082. case C4IW_QP_STATE_RTS:
  1083. if (!(mask & C4IW_QP_ATTR_LLP_STREAM_HANDLE)) {
  1084. ret = -EINVAL;
  1085. goto out;
  1086. }
  1087. if (!(mask & C4IW_QP_ATTR_MPA_ATTR)) {
  1088. ret = -EINVAL;
  1089. goto out;
  1090. }
  1091. qhp->attr.mpa_attr = attrs->mpa_attr;
  1092. qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
  1093. qhp->ep = qhp->attr.llp_stream_handle;
  1094. qhp->attr.state = C4IW_QP_STATE_RTS;
  1095. /*
  1096. * Ref the endpoint here and deref when we
  1097. * disassociate the endpoint from the QP. This
  1098. * happens in CLOSING->IDLE transition or *->ERROR
  1099. * transition.
  1100. */
  1101. c4iw_get_ep(&qhp->ep->com);
  1102. spin_unlock_irqrestore(&qhp->lock, flag);
  1103. ret = rdma_init(rhp, qhp);
  1104. spin_lock_irqsave(&qhp->lock, flag);
  1105. if (ret)
  1106. goto err;
  1107. break;
  1108. case C4IW_QP_STATE_ERROR:
  1109. qhp->attr.state = C4IW_QP_STATE_ERROR;
  1110. flush_qp(qhp, &flag);
  1111. break;
  1112. default:
  1113. ret = -EINVAL;
  1114. goto out;
  1115. }
  1116. break;
  1117. case C4IW_QP_STATE_RTS:
  1118. switch (attrs->next_state) {
  1119. case C4IW_QP_STATE_CLOSING:
  1120. BUG_ON(atomic_read(&qhp->ep->com.kref.refcount) < 2);
  1121. qhp->attr.state = C4IW_QP_STATE_CLOSING;
  1122. ep = qhp->ep;
  1123. if (!internal) {
  1124. abort = 0;
  1125. disconnect = 1;
  1126. c4iw_get_ep(&ep->com);
  1127. }
  1128. spin_unlock_irqrestore(&qhp->lock, flag);
  1129. ret = rdma_fini(rhp, qhp, ep);
  1130. spin_lock_irqsave(&qhp->lock, flag);
  1131. if (ret) {
  1132. c4iw_get_ep(&ep->com);
  1133. disconnect = abort = 1;
  1134. goto err;
  1135. }
  1136. break;
  1137. case C4IW_QP_STATE_TERMINATE:
  1138. qhp->attr.state = C4IW_QP_STATE_TERMINATE;
  1139. if (qhp->ibqp.uobject)
  1140. t4_set_wq_in_error(&qhp->wq);
  1141. ep = qhp->ep;
  1142. c4iw_get_ep(&ep->com);
  1143. if (!internal)
  1144. terminate = 1;
  1145. disconnect = 1;
  1146. break;
  1147. case C4IW_QP_STATE_ERROR:
  1148. qhp->attr.state = C4IW_QP_STATE_ERROR;
  1149. if (!internal) {
  1150. abort = 1;
  1151. disconnect = 1;
  1152. ep = qhp->ep;
  1153. c4iw_get_ep(&ep->com);
  1154. }
  1155. goto err;
  1156. break;
  1157. default:
  1158. ret = -EINVAL;
  1159. goto out;
  1160. }
  1161. break;
  1162. case C4IW_QP_STATE_CLOSING:
  1163. if (!internal) {
  1164. ret = -EINVAL;
  1165. goto out;
  1166. }
  1167. switch (attrs->next_state) {
  1168. case C4IW_QP_STATE_IDLE:
  1169. flush_qp(qhp, &flag);
  1170. qhp->attr.state = C4IW_QP_STATE_IDLE;
  1171. qhp->attr.llp_stream_handle = NULL;
  1172. c4iw_put_ep(&qhp->ep->com);
  1173. qhp->ep = NULL;
  1174. wake_up(&qhp->wait);
  1175. break;
  1176. case C4IW_QP_STATE_ERROR:
  1177. goto err;
  1178. default:
  1179. ret = -EINVAL;
  1180. goto err;
  1181. }
  1182. break;
  1183. case C4IW_QP_STATE_ERROR:
  1184. if (attrs->next_state != C4IW_QP_STATE_IDLE) {
  1185. ret = -EINVAL;
  1186. goto out;
  1187. }
  1188. if (!t4_sq_empty(&qhp->wq) || !t4_rq_empty(&qhp->wq)) {
  1189. ret = -EINVAL;
  1190. goto out;
  1191. }
  1192. qhp->attr.state = C4IW_QP_STATE_IDLE;
  1193. break;
  1194. case C4IW_QP_STATE_TERMINATE:
  1195. if (!internal) {
  1196. ret = -EINVAL;
  1197. goto out;
  1198. }
  1199. goto err;
  1200. break;
  1201. default:
  1202. printk(KERN_ERR "%s in a bad state %d\n",
  1203. __func__, qhp->attr.state);
  1204. ret = -EINVAL;
  1205. goto err;
  1206. break;
  1207. }
  1208. goto out;
  1209. err:
  1210. PDBG("%s disassociating ep %p qpid 0x%x\n", __func__, qhp->ep,
  1211. qhp->wq.sq.qid);
  1212. /* disassociate the LLP connection */
  1213. qhp->attr.llp_stream_handle = NULL;
  1214. if (!ep)
  1215. ep = qhp->ep;
  1216. qhp->ep = NULL;
  1217. qhp->attr.state = C4IW_QP_STATE_ERROR;
  1218. free = 1;
  1219. wake_up(&qhp->wait);
  1220. BUG_ON(!ep);
  1221. flush_qp(qhp, &flag);
  1222. out:
  1223. spin_unlock_irqrestore(&qhp->lock, flag);
  1224. if (terminate)
  1225. post_terminate(qhp, NULL, internal ? GFP_ATOMIC : GFP_KERNEL);
  1226. /*
  1227. * If disconnect is 1, then we need to initiate a disconnect
  1228. * on the EP. This can be a normal close (RTS->CLOSING) or
  1229. * an abnormal close (RTS/CLOSING->ERROR).
  1230. */
  1231. if (disconnect) {
  1232. c4iw_ep_disconnect(ep, abort, internal ? GFP_ATOMIC :
  1233. GFP_KERNEL);
  1234. c4iw_put_ep(&ep->com);
  1235. }
  1236. /*
  1237. * If free is 1, then we've disassociated the EP from the QP
  1238. * and we need to dereference the EP.
  1239. */
  1240. if (free)
  1241. c4iw_put_ep(&ep->com);
  1242. PDBG("%s exit state %d\n", __func__, qhp->attr.state);
  1243. return ret;
  1244. }
  1245. int c4iw_destroy_qp(struct ib_qp *ib_qp)
  1246. {
  1247. struct c4iw_dev *rhp;
  1248. struct c4iw_qp *qhp;
  1249. struct c4iw_qp_attributes attrs;
  1250. struct c4iw_ucontext *ucontext;
  1251. qhp = to_c4iw_qp(ib_qp);
  1252. rhp = qhp->rhp;
  1253. attrs.next_state = C4IW_QP_STATE_ERROR;
  1254. c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
  1255. wait_event(qhp->wait, !qhp->ep);
  1256. remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
  1257. atomic_dec(&qhp->refcnt);
  1258. wait_event(qhp->wait, !atomic_read(&qhp->refcnt));
  1259. ucontext = ib_qp->uobject ?
  1260. to_c4iw_ucontext(ib_qp->uobject->context) : NULL;
  1261. destroy_qp(&rhp->rdev, &qhp->wq,
  1262. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  1263. PDBG("%s ib_qp %p qpid 0x%0x\n", __func__, ib_qp, qhp->wq.sq.qid);
  1264. kfree(qhp);
  1265. return 0;
  1266. }
  1267. struct ib_qp *c4iw_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
  1268. struct ib_udata *udata)
  1269. {
  1270. struct c4iw_dev *rhp;
  1271. struct c4iw_qp *qhp;
  1272. struct c4iw_pd *php;
  1273. struct c4iw_cq *schp;
  1274. struct c4iw_cq *rchp;
  1275. struct c4iw_create_qp_resp uresp;
  1276. int sqsize, rqsize;
  1277. struct c4iw_ucontext *ucontext;
  1278. int ret;
  1279. struct c4iw_mm_entry *mm1, *mm2, *mm3, *mm4;
  1280. PDBG("%s ib_pd %p\n", __func__, pd);
  1281. if (attrs->qp_type != IB_QPT_RC)
  1282. return ERR_PTR(-EINVAL);
  1283. php = to_c4iw_pd(pd);
  1284. rhp = php->rhp;
  1285. schp = get_chp(rhp, ((struct c4iw_cq *)attrs->send_cq)->cq.cqid);
  1286. rchp = get_chp(rhp, ((struct c4iw_cq *)attrs->recv_cq)->cq.cqid);
  1287. if (!schp || !rchp)
  1288. return ERR_PTR(-EINVAL);
  1289. if (attrs->cap.max_inline_data > T4_MAX_SEND_INLINE)
  1290. return ERR_PTR(-EINVAL);
  1291. rqsize = roundup(attrs->cap.max_recv_wr + 1, 16);
  1292. if (rqsize > T4_MAX_RQ_SIZE)
  1293. return ERR_PTR(-E2BIG);
  1294. sqsize = roundup(attrs->cap.max_send_wr + 1, 16);
  1295. if (sqsize > T4_MAX_SQ_SIZE)
  1296. return ERR_PTR(-E2BIG);
  1297. ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
  1298. qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
  1299. if (!qhp)
  1300. return ERR_PTR(-ENOMEM);
  1301. qhp->wq.sq.size = sqsize;
  1302. qhp->wq.sq.memsize = (sqsize + 1) * sizeof *qhp->wq.sq.queue;
  1303. qhp->wq.rq.size = rqsize;
  1304. qhp->wq.rq.memsize = (rqsize + 1) * sizeof *qhp->wq.rq.queue;
  1305. if (ucontext) {
  1306. qhp->wq.sq.memsize = roundup(qhp->wq.sq.memsize, PAGE_SIZE);
  1307. qhp->wq.rq.memsize = roundup(qhp->wq.rq.memsize, PAGE_SIZE);
  1308. }
  1309. PDBG("%s sqsize %u sqmemsize %zu rqsize %u rqmemsize %zu\n",
  1310. __func__, sqsize, qhp->wq.sq.memsize, rqsize, qhp->wq.rq.memsize);
  1311. ret = create_qp(&rhp->rdev, &qhp->wq, &schp->cq, &rchp->cq,
  1312. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  1313. if (ret)
  1314. goto err1;
  1315. attrs->cap.max_recv_wr = rqsize - 1;
  1316. attrs->cap.max_send_wr = sqsize - 1;
  1317. attrs->cap.max_inline_data = T4_MAX_SEND_INLINE;
  1318. qhp->rhp = rhp;
  1319. qhp->attr.pd = php->pdid;
  1320. qhp->attr.scq = ((struct c4iw_cq *) attrs->send_cq)->cq.cqid;
  1321. qhp->attr.rcq = ((struct c4iw_cq *) attrs->recv_cq)->cq.cqid;
  1322. qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
  1323. qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
  1324. qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
  1325. qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
  1326. qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
  1327. qhp->attr.state = C4IW_QP_STATE_IDLE;
  1328. qhp->attr.next_state = C4IW_QP_STATE_IDLE;
  1329. qhp->attr.enable_rdma_read = 1;
  1330. qhp->attr.enable_rdma_write = 1;
  1331. qhp->attr.enable_bind = 1;
  1332. qhp->attr.max_ord = 1;
  1333. qhp->attr.max_ird = 1;
  1334. spin_lock_init(&qhp->lock);
  1335. init_waitqueue_head(&qhp->wait);
  1336. atomic_set(&qhp->refcnt, 1);
  1337. ret = insert_handle(rhp, &rhp->qpidr, qhp, qhp->wq.sq.qid);
  1338. if (ret)
  1339. goto err2;
  1340. if (udata) {
  1341. mm1 = kmalloc(sizeof *mm1, GFP_KERNEL);
  1342. if (!mm1) {
  1343. ret = -ENOMEM;
  1344. goto err3;
  1345. }
  1346. mm2 = kmalloc(sizeof *mm2, GFP_KERNEL);
  1347. if (!mm2) {
  1348. ret = -ENOMEM;
  1349. goto err4;
  1350. }
  1351. mm3 = kmalloc(sizeof *mm3, GFP_KERNEL);
  1352. if (!mm3) {
  1353. ret = -ENOMEM;
  1354. goto err5;
  1355. }
  1356. mm4 = kmalloc(sizeof *mm4, GFP_KERNEL);
  1357. if (!mm4) {
  1358. ret = -ENOMEM;
  1359. goto err6;
  1360. }
  1361. uresp.qid_mask = rhp->rdev.qpmask;
  1362. uresp.sqid = qhp->wq.sq.qid;
  1363. uresp.sq_size = qhp->wq.sq.size;
  1364. uresp.sq_memsize = qhp->wq.sq.memsize;
  1365. uresp.rqid = qhp->wq.rq.qid;
  1366. uresp.rq_size = qhp->wq.rq.size;
  1367. uresp.rq_memsize = qhp->wq.rq.memsize;
  1368. spin_lock(&ucontext->mmap_lock);
  1369. uresp.sq_key = ucontext->key;
  1370. ucontext->key += PAGE_SIZE;
  1371. uresp.rq_key = ucontext->key;
  1372. ucontext->key += PAGE_SIZE;
  1373. uresp.sq_db_gts_key = ucontext->key;
  1374. ucontext->key += PAGE_SIZE;
  1375. uresp.rq_db_gts_key = ucontext->key;
  1376. ucontext->key += PAGE_SIZE;
  1377. spin_unlock(&ucontext->mmap_lock);
  1378. ret = ib_copy_to_udata(udata, &uresp, sizeof uresp);
  1379. if (ret)
  1380. goto err7;
  1381. mm1->key = uresp.sq_key;
  1382. mm1->addr = virt_to_phys(qhp->wq.sq.queue);
  1383. mm1->len = PAGE_ALIGN(qhp->wq.sq.memsize);
  1384. insert_mmap(ucontext, mm1);
  1385. mm2->key = uresp.rq_key;
  1386. mm2->addr = virt_to_phys(qhp->wq.rq.queue);
  1387. mm2->len = PAGE_ALIGN(qhp->wq.rq.memsize);
  1388. insert_mmap(ucontext, mm2);
  1389. mm3->key = uresp.sq_db_gts_key;
  1390. mm3->addr = qhp->wq.sq.udb;
  1391. mm3->len = PAGE_SIZE;
  1392. insert_mmap(ucontext, mm3);
  1393. mm4->key = uresp.rq_db_gts_key;
  1394. mm4->addr = qhp->wq.rq.udb;
  1395. mm4->len = PAGE_SIZE;
  1396. insert_mmap(ucontext, mm4);
  1397. }
  1398. qhp->ibqp.qp_num = qhp->wq.sq.qid;
  1399. init_timer(&(qhp->timer));
  1400. PDBG("%s qhp %p sq_num_entries %d, rq_num_entries %d qpid 0x%0x\n",
  1401. __func__, qhp, qhp->attr.sq_num_entries, qhp->attr.rq_num_entries,
  1402. qhp->wq.sq.qid);
  1403. return &qhp->ibqp;
  1404. err7:
  1405. kfree(mm4);
  1406. err6:
  1407. kfree(mm3);
  1408. err5:
  1409. kfree(mm2);
  1410. err4:
  1411. kfree(mm1);
  1412. err3:
  1413. remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
  1414. err2:
  1415. destroy_qp(&rhp->rdev, &qhp->wq,
  1416. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  1417. err1:
  1418. kfree(qhp);
  1419. return ERR_PTR(ret);
  1420. }
  1421. int c4iw_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1422. int attr_mask, struct ib_udata *udata)
  1423. {
  1424. struct c4iw_dev *rhp;
  1425. struct c4iw_qp *qhp;
  1426. enum c4iw_qp_attr_mask mask = 0;
  1427. struct c4iw_qp_attributes attrs;
  1428. PDBG("%s ib_qp %p\n", __func__, ibqp);
  1429. /* iwarp does not support the RTR state */
  1430. if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
  1431. attr_mask &= ~IB_QP_STATE;
  1432. /* Make sure we still have something left to do */
  1433. if (!attr_mask)
  1434. return 0;
  1435. memset(&attrs, 0, sizeof attrs);
  1436. qhp = to_c4iw_qp(ibqp);
  1437. rhp = qhp->rhp;
  1438. attrs.next_state = c4iw_convert_state(attr->qp_state);
  1439. attrs.enable_rdma_read = (attr->qp_access_flags &
  1440. IB_ACCESS_REMOTE_READ) ? 1 : 0;
  1441. attrs.enable_rdma_write = (attr->qp_access_flags &
  1442. IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  1443. attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
  1444. mask |= (attr_mask & IB_QP_STATE) ? C4IW_QP_ATTR_NEXT_STATE : 0;
  1445. mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
  1446. (C4IW_QP_ATTR_ENABLE_RDMA_READ |
  1447. C4IW_QP_ATTR_ENABLE_RDMA_WRITE |
  1448. C4IW_QP_ATTR_ENABLE_RDMA_BIND) : 0;
  1449. return c4iw_modify_qp(rhp, qhp, mask, &attrs, 0);
  1450. }
  1451. struct ib_qp *c4iw_get_qp(struct ib_device *dev, int qpn)
  1452. {
  1453. PDBG("%s ib_dev %p qpn 0x%x\n", __func__, dev, qpn);
  1454. return (struct ib_qp *)get_qhp(to_c4iw_dev(dev), qpn);
  1455. }