qp.c 44 KB

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