qp.c 47 KB

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