qp.c 47 KB

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