qp.c 51 KB

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