qp.c 48 KB

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