qp.c 46 KB

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