cxio_hal.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. /*
  2. * Copyright (c) 2006 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 <asm/delay.h>
  33. #include <linux/mutex.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/sched.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/pci.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/slab.h>
  40. #include <net/net_namespace.h>
  41. #include "cxio_resource.h"
  42. #include "cxio_hal.h"
  43. #include "cxgb3_offload.h"
  44. #include "sge_defs.h"
  45. static LIST_HEAD(rdev_list);
  46. static cxio_hal_ev_callback_func_t cxio_ev_cb = NULL;
  47. static struct cxio_rdev *cxio_hal_find_rdev_by_name(char *dev_name)
  48. {
  49. struct cxio_rdev *rdev;
  50. list_for_each_entry(rdev, &rdev_list, entry)
  51. if (!strcmp(rdev->dev_name, dev_name))
  52. return rdev;
  53. return NULL;
  54. }
  55. static struct cxio_rdev *cxio_hal_find_rdev_by_t3cdev(struct t3cdev *tdev)
  56. {
  57. struct cxio_rdev *rdev;
  58. list_for_each_entry(rdev, &rdev_list, entry)
  59. if (rdev->t3cdev_p == tdev)
  60. return rdev;
  61. return NULL;
  62. }
  63. int cxio_hal_cq_op(struct cxio_rdev *rdev_p, struct t3_cq *cq,
  64. enum t3_cq_opcode op, u32 credit)
  65. {
  66. int ret;
  67. struct t3_cqe *cqe;
  68. u32 rptr;
  69. struct rdma_cq_op setup;
  70. setup.id = cq->cqid;
  71. setup.credits = (op == CQ_CREDIT_UPDATE) ? credit : 0;
  72. setup.op = op;
  73. ret = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_CQ_OP, &setup);
  74. if ((ret < 0) || (op == CQ_CREDIT_UPDATE))
  75. return ret;
  76. /*
  77. * If the rearm returned an index other than our current index,
  78. * then there might be CQE's in flight (being DMA'd). We must wait
  79. * here for them to complete or the consumer can miss a notification.
  80. */
  81. if (Q_PTR2IDX((cq->rptr), cq->size_log2) != ret) {
  82. int i=0;
  83. rptr = cq->rptr;
  84. /*
  85. * Keep the generation correct by bumping rptr until it
  86. * matches the index returned by the rearm - 1.
  87. */
  88. while (Q_PTR2IDX((rptr+1), cq->size_log2) != ret)
  89. rptr++;
  90. /*
  91. * Now rptr is the index for the (last) cqe that was
  92. * in-flight at the time the HW rearmed the CQ. We
  93. * spin until that CQE is valid.
  94. */
  95. cqe = cq->queue + Q_PTR2IDX(rptr, cq->size_log2);
  96. while (!CQ_VLD_ENTRY(rptr, cq->size_log2, cqe)) {
  97. udelay(1);
  98. if (i++ > 1000000) {
  99. pr_err("%s: stalled rnic\n", rdev_p->dev_name);
  100. return -EIO;
  101. }
  102. }
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. static int cxio_hal_clear_cq_ctx(struct cxio_rdev *rdev_p, u32 cqid)
  108. {
  109. struct rdma_cq_setup setup;
  110. setup.id = cqid;
  111. setup.base_addr = 0; /* NULL address */
  112. setup.size = 0; /* disaable the CQ */
  113. setup.credits = 0;
  114. setup.credit_thres = 0;
  115. setup.ovfl_mode = 0;
  116. return (rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_CQ_SETUP, &setup));
  117. }
  118. static int cxio_hal_clear_qp_ctx(struct cxio_rdev *rdev_p, u32 qpid)
  119. {
  120. u64 sge_cmd;
  121. struct t3_modify_qp_wr *wqe;
  122. struct sk_buff *skb = alloc_skb(sizeof(*wqe), GFP_KERNEL);
  123. if (!skb) {
  124. pr_debug("%s alloc_skb failed\n", __func__);
  125. return -ENOMEM;
  126. }
  127. wqe = skb_put_zero(skb, sizeof(*wqe));
  128. build_fw_riwrh((struct fw_riwrh *) wqe, T3_WR_QP_MOD,
  129. T3_COMPLETION_FLAG | T3_NOTIFY_FLAG, 0, qpid, 7,
  130. T3_SOPEOP);
  131. wqe->flags = cpu_to_be32(MODQP_WRITE_EC);
  132. sge_cmd = qpid << 8 | 3;
  133. wqe->sge_cmd = cpu_to_be64(sge_cmd);
  134. skb->priority = CPL_PRIORITY_CONTROL;
  135. return iwch_cxgb3_ofld_send(rdev_p->t3cdev_p, skb);
  136. }
  137. int cxio_create_cq(struct cxio_rdev *rdev_p, struct t3_cq *cq, int kernel)
  138. {
  139. struct rdma_cq_setup setup;
  140. int size = (1UL << (cq->size_log2)) * sizeof(struct t3_cqe);
  141. size += 1; /* one extra page for storing cq-in-err state */
  142. cq->cqid = cxio_hal_get_cqid(rdev_p->rscp);
  143. if (!cq->cqid)
  144. return -ENOMEM;
  145. if (kernel) {
  146. cq->sw_queue = kzalloc(size, GFP_KERNEL);
  147. if (!cq->sw_queue)
  148. return -ENOMEM;
  149. }
  150. cq->queue = dma_alloc_coherent(&(rdev_p->rnic_info.pdev->dev), size,
  151. &(cq->dma_addr), GFP_KERNEL);
  152. if (!cq->queue) {
  153. kfree(cq->sw_queue);
  154. return -ENOMEM;
  155. }
  156. dma_unmap_addr_set(cq, mapping, cq->dma_addr);
  157. memset(cq->queue, 0, size);
  158. setup.id = cq->cqid;
  159. setup.base_addr = (u64) (cq->dma_addr);
  160. setup.size = 1UL << cq->size_log2;
  161. setup.credits = 65535;
  162. setup.credit_thres = 1;
  163. if (rdev_p->t3cdev_p->type != T3A)
  164. setup.ovfl_mode = 0;
  165. else
  166. setup.ovfl_mode = 1;
  167. return (rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_CQ_SETUP, &setup));
  168. }
  169. #ifdef notyet
  170. int cxio_resize_cq(struct cxio_rdev *rdev_p, struct t3_cq *cq)
  171. {
  172. struct rdma_cq_setup setup;
  173. setup.id = cq->cqid;
  174. setup.base_addr = (u64) (cq->dma_addr);
  175. setup.size = 1UL << cq->size_log2;
  176. setup.credits = setup.size;
  177. setup.credit_thres = setup.size; /* TBD: overflow recovery */
  178. setup.ovfl_mode = 1;
  179. return (rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_CQ_SETUP, &setup));
  180. }
  181. #endif
  182. static u32 get_qpid(struct cxio_rdev *rdev_p, struct cxio_ucontext *uctx)
  183. {
  184. struct cxio_qpid_list *entry;
  185. u32 qpid;
  186. int i;
  187. mutex_lock(&uctx->lock);
  188. if (!list_empty(&uctx->qpids)) {
  189. entry = list_entry(uctx->qpids.next, struct cxio_qpid_list,
  190. entry);
  191. list_del(&entry->entry);
  192. qpid = entry->qpid;
  193. kfree(entry);
  194. } else {
  195. qpid = cxio_hal_get_qpid(rdev_p->rscp);
  196. if (!qpid)
  197. goto out;
  198. for (i = qpid+1; i & rdev_p->qpmask; i++) {
  199. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  200. if (!entry)
  201. break;
  202. entry->qpid = i;
  203. list_add_tail(&entry->entry, &uctx->qpids);
  204. }
  205. }
  206. out:
  207. mutex_unlock(&uctx->lock);
  208. pr_debug("%s qpid 0x%x\n", __func__, qpid);
  209. return qpid;
  210. }
  211. static void put_qpid(struct cxio_rdev *rdev_p, u32 qpid,
  212. struct cxio_ucontext *uctx)
  213. {
  214. struct cxio_qpid_list *entry;
  215. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  216. if (!entry)
  217. return;
  218. pr_debug("%s qpid 0x%x\n", __func__, qpid);
  219. entry->qpid = qpid;
  220. mutex_lock(&uctx->lock);
  221. list_add_tail(&entry->entry, &uctx->qpids);
  222. mutex_unlock(&uctx->lock);
  223. }
  224. void cxio_release_ucontext(struct cxio_rdev *rdev_p, struct cxio_ucontext *uctx)
  225. {
  226. struct list_head *pos, *nxt;
  227. struct cxio_qpid_list *entry;
  228. mutex_lock(&uctx->lock);
  229. list_for_each_safe(pos, nxt, &uctx->qpids) {
  230. entry = list_entry(pos, struct cxio_qpid_list, entry);
  231. list_del_init(&entry->entry);
  232. if (!(entry->qpid & rdev_p->qpmask))
  233. cxio_hal_put_qpid(rdev_p->rscp, entry->qpid);
  234. kfree(entry);
  235. }
  236. mutex_unlock(&uctx->lock);
  237. }
  238. void cxio_init_ucontext(struct cxio_rdev *rdev_p, struct cxio_ucontext *uctx)
  239. {
  240. INIT_LIST_HEAD(&uctx->qpids);
  241. mutex_init(&uctx->lock);
  242. }
  243. int cxio_create_qp(struct cxio_rdev *rdev_p, u32 kernel_domain,
  244. struct t3_wq *wq, struct cxio_ucontext *uctx)
  245. {
  246. int depth = 1UL << wq->size_log2;
  247. int rqsize = 1UL << wq->rq_size_log2;
  248. wq->qpid = get_qpid(rdev_p, uctx);
  249. if (!wq->qpid)
  250. return -ENOMEM;
  251. wq->rq = kcalloc(depth, sizeof(struct t3_swrq), GFP_KERNEL);
  252. if (!wq->rq)
  253. goto err1;
  254. wq->rq_addr = cxio_hal_rqtpool_alloc(rdev_p, rqsize);
  255. if (!wq->rq_addr)
  256. goto err2;
  257. wq->sq = kcalloc(depth, sizeof(struct t3_swsq), GFP_KERNEL);
  258. if (!wq->sq)
  259. goto err3;
  260. wq->queue = dma_alloc_coherent(&(rdev_p->rnic_info.pdev->dev),
  261. depth * sizeof(union t3_wr),
  262. &(wq->dma_addr), GFP_KERNEL);
  263. if (!wq->queue)
  264. goto err4;
  265. memset(wq->queue, 0, depth * sizeof(union t3_wr));
  266. dma_unmap_addr_set(wq, mapping, wq->dma_addr);
  267. wq->doorbell = (void __iomem *)rdev_p->rnic_info.kdb_addr;
  268. if (!kernel_domain)
  269. wq->udb = (u64)rdev_p->rnic_info.udbell_physbase +
  270. (wq->qpid << rdev_p->qpshift);
  271. wq->rdev = rdev_p;
  272. pr_debug("%s qpid 0x%x doorbell 0x%p udb 0x%llx\n",
  273. __func__, wq->qpid, wq->doorbell, (unsigned long long)wq->udb);
  274. return 0;
  275. err4:
  276. kfree(wq->sq);
  277. err3:
  278. cxio_hal_rqtpool_free(rdev_p, wq->rq_addr, rqsize);
  279. err2:
  280. kfree(wq->rq);
  281. err1:
  282. put_qpid(rdev_p, wq->qpid, uctx);
  283. return -ENOMEM;
  284. }
  285. int cxio_destroy_cq(struct cxio_rdev *rdev_p, struct t3_cq *cq)
  286. {
  287. int err;
  288. err = cxio_hal_clear_cq_ctx(rdev_p, cq->cqid);
  289. kfree(cq->sw_queue);
  290. dma_free_coherent(&(rdev_p->rnic_info.pdev->dev),
  291. (1UL << (cq->size_log2))
  292. * sizeof(struct t3_cqe) + 1, cq->queue,
  293. dma_unmap_addr(cq, mapping));
  294. cxio_hal_put_cqid(rdev_p->rscp, cq->cqid);
  295. return err;
  296. }
  297. int cxio_destroy_qp(struct cxio_rdev *rdev_p, struct t3_wq *wq,
  298. struct cxio_ucontext *uctx)
  299. {
  300. dma_free_coherent(&(rdev_p->rnic_info.pdev->dev),
  301. (1UL << (wq->size_log2))
  302. * sizeof(union t3_wr), wq->queue,
  303. dma_unmap_addr(wq, mapping));
  304. kfree(wq->sq);
  305. cxio_hal_rqtpool_free(rdev_p, wq->rq_addr, (1UL << wq->rq_size_log2));
  306. kfree(wq->rq);
  307. put_qpid(rdev_p, wq->qpid, uctx);
  308. return 0;
  309. }
  310. static void insert_recv_cqe(struct t3_wq *wq, struct t3_cq *cq)
  311. {
  312. struct t3_cqe cqe;
  313. pr_debug("%s wq %p cq %p sw_rptr 0x%x sw_wptr 0x%x\n", __func__,
  314. wq, cq, cq->sw_rptr, cq->sw_wptr);
  315. memset(&cqe, 0, sizeof(cqe));
  316. cqe.header = cpu_to_be32(V_CQE_STATUS(TPT_ERR_SWFLUSH) |
  317. V_CQE_OPCODE(T3_SEND) |
  318. V_CQE_TYPE(0) |
  319. V_CQE_SWCQE(1) |
  320. V_CQE_QPID(wq->qpid) |
  321. V_CQE_GENBIT(Q_GENBIT(cq->sw_wptr,
  322. cq->size_log2)));
  323. *(cq->sw_queue + Q_PTR2IDX(cq->sw_wptr, cq->size_log2)) = cqe;
  324. cq->sw_wptr++;
  325. }
  326. int cxio_flush_rq(struct t3_wq *wq, struct t3_cq *cq, int count)
  327. {
  328. u32 ptr;
  329. int flushed = 0;
  330. pr_debug("%s wq %p cq %p\n", __func__, wq, cq);
  331. /* flush RQ */
  332. pr_debug("%s rq_rptr %u rq_wptr %u skip count %u\n", __func__,
  333. wq->rq_rptr, wq->rq_wptr, count);
  334. ptr = wq->rq_rptr + count;
  335. while (ptr++ != wq->rq_wptr) {
  336. insert_recv_cqe(wq, cq);
  337. flushed++;
  338. }
  339. return flushed;
  340. }
  341. static void insert_sq_cqe(struct t3_wq *wq, struct t3_cq *cq,
  342. struct t3_swsq *sqp)
  343. {
  344. struct t3_cqe cqe;
  345. pr_debug("%s wq %p cq %p sw_rptr 0x%x sw_wptr 0x%x\n", __func__,
  346. wq, cq, cq->sw_rptr, cq->sw_wptr);
  347. memset(&cqe, 0, sizeof(cqe));
  348. cqe.header = cpu_to_be32(V_CQE_STATUS(TPT_ERR_SWFLUSH) |
  349. V_CQE_OPCODE(sqp->opcode) |
  350. V_CQE_TYPE(1) |
  351. V_CQE_SWCQE(1) |
  352. V_CQE_QPID(wq->qpid) |
  353. V_CQE_GENBIT(Q_GENBIT(cq->sw_wptr,
  354. cq->size_log2)));
  355. cqe.u.scqe.wrid_hi = sqp->sq_wptr;
  356. *(cq->sw_queue + Q_PTR2IDX(cq->sw_wptr, cq->size_log2)) = cqe;
  357. cq->sw_wptr++;
  358. }
  359. int cxio_flush_sq(struct t3_wq *wq, struct t3_cq *cq, int count)
  360. {
  361. __u32 ptr = wq->sq_rptr + count;
  362. int flushed = 0;
  363. struct t3_swsq *sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
  364. while (ptr != wq->sq_wptr) {
  365. sqp->signaled = 0;
  366. insert_sq_cqe(wq, cq, sqp);
  367. ptr++;
  368. sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
  369. flushed++;
  370. }
  371. return flushed;
  372. }
  373. /*
  374. * Move all CQEs from the HWCQ into the SWCQ.
  375. */
  376. void cxio_flush_hw_cq(struct t3_cq *cq)
  377. {
  378. struct t3_cqe *cqe, *swcqe;
  379. pr_debug("%s cq %p cqid 0x%x\n", __func__, cq, cq->cqid);
  380. cqe = cxio_next_hw_cqe(cq);
  381. while (cqe) {
  382. pr_debug("%s flushing hwcq rptr 0x%x to swcq wptr 0x%x\n",
  383. __func__, cq->rptr, cq->sw_wptr);
  384. swcqe = cq->sw_queue + Q_PTR2IDX(cq->sw_wptr, cq->size_log2);
  385. *swcqe = *cqe;
  386. swcqe->header |= cpu_to_be32(V_CQE_SWCQE(1));
  387. cq->sw_wptr++;
  388. cq->rptr++;
  389. cqe = cxio_next_hw_cqe(cq);
  390. }
  391. }
  392. static int cqe_completes_wr(struct t3_cqe *cqe, struct t3_wq *wq)
  393. {
  394. if (CQE_OPCODE(*cqe) == T3_TERMINATE)
  395. return 0;
  396. if ((CQE_OPCODE(*cqe) == T3_RDMA_WRITE) && RQ_TYPE(*cqe))
  397. return 0;
  398. if ((CQE_OPCODE(*cqe) == T3_READ_RESP) && SQ_TYPE(*cqe))
  399. return 0;
  400. if (CQE_SEND_OPCODE(*cqe) && RQ_TYPE(*cqe) &&
  401. Q_EMPTY(wq->rq_rptr, wq->rq_wptr))
  402. return 0;
  403. return 1;
  404. }
  405. void cxio_count_scqes(struct t3_cq *cq, struct t3_wq *wq, int *count)
  406. {
  407. struct t3_cqe *cqe;
  408. u32 ptr;
  409. *count = 0;
  410. ptr = cq->sw_rptr;
  411. while (!Q_EMPTY(ptr, cq->sw_wptr)) {
  412. cqe = cq->sw_queue + (Q_PTR2IDX(ptr, cq->size_log2));
  413. if ((SQ_TYPE(*cqe) ||
  414. ((CQE_OPCODE(*cqe) == T3_READ_RESP) && wq->oldest_read)) &&
  415. (CQE_QPID(*cqe) == wq->qpid))
  416. (*count)++;
  417. ptr++;
  418. }
  419. pr_debug("%s cq %p count %d\n", __func__, cq, *count);
  420. }
  421. void cxio_count_rcqes(struct t3_cq *cq, struct t3_wq *wq, int *count)
  422. {
  423. struct t3_cqe *cqe;
  424. u32 ptr;
  425. *count = 0;
  426. pr_debug("%s count zero %d\n", __func__, *count);
  427. ptr = cq->sw_rptr;
  428. while (!Q_EMPTY(ptr, cq->sw_wptr)) {
  429. cqe = cq->sw_queue + (Q_PTR2IDX(ptr, cq->size_log2));
  430. if (RQ_TYPE(*cqe) && (CQE_OPCODE(*cqe) != T3_READ_RESP) &&
  431. (CQE_QPID(*cqe) == wq->qpid) && cqe_completes_wr(cqe, wq))
  432. (*count)++;
  433. ptr++;
  434. }
  435. pr_debug("%s cq %p count %d\n", __func__, cq, *count);
  436. }
  437. static int cxio_hal_init_ctrl_cq(struct cxio_rdev *rdev_p)
  438. {
  439. struct rdma_cq_setup setup;
  440. setup.id = 0;
  441. setup.base_addr = 0; /* NULL address */
  442. setup.size = 1; /* enable the CQ */
  443. setup.credits = 0;
  444. /* force SGE to redirect to RspQ and interrupt */
  445. setup.credit_thres = 0;
  446. setup.ovfl_mode = 1;
  447. return (rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_CQ_SETUP, &setup));
  448. }
  449. static int cxio_hal_init_ctrl_qp(struct cxio_rdev *rdev_p)
  450. {
  451. int err;
  452. u64 sge_cmd, ctx0, ctx1;
  453. u64 base_addr;
  454. struct t3_modify_qp_wr *wqe;
  455. struct sk_buff *skb;
  456. skb = alloc_skb(sizeof(*wqe), GFP_KERNEL);
  457. if (!skb) {
  458. pr_debug("%s alloc_skb failed\n", __func__);
  459. return -ENOMEM;
  460. }
  461. err = cxio_hal_init_ctrl_cq(rdev_p);
  462. if (err) {
  463. pr_debug("%s err %d initializing ctrl_cq\n", __func__, err);
  464. goto err;
  465. }
  466. rdev_p->ctrl_qp.workq = dma_alloc_coherent(
  467. &(rdev_p->rnic_info.pdev->dev),
  468. (1 << T3_CTRL_QP_SIZE_LOG2) *
  469. sizeof(union t3_wr),
  470. &(rdev_p->ctrl_qp.dma_addr),
  471. GFP_KERNEL);
  472. if (!rdev_p->ctrl_qp.workq) {
  473. pr_debug("%s dma_alloc_coherent failed\n", __func__);
  474. err = -ENOMEM;
  475. goto err;
  476. }
  477. dma_unmap_addr_set(&rdev_p->ctrl_qp, mapping,
  478. rdev_p->ctrl_qp.dma_addr);
  479. rdev_p->ctrl_qp.doorbell = (void __iomem *)rdev_p->rnic_info.kdb_addr;
  480. memset(rdev_p->ctrl_qp.workq, 0,
  481. (1 << T3_CTRL_QP_SIZE_LOG2) * sizeof(union t3_wr));
  482. mutex_init(&rdev_p->ctrl_qp.lock);
  483. init_waitqueue_head(&rdev_p->ctrl_qp.waitq);
  484. /* update HW Ctrl QP context */
  485. base_addr = rdev_p->ctrl_qp.dma_addr;
  486. base_addr >>= 12;
  487. ctx0 = (V_EC_SIZE((1 << T3_CTRL_QP_SIZE_LOG2)) |
  488. V_EC_BASE_LO((u32) base_addr & 0xffff));
  489. ctx0 <<= 32;
  490. ctx0 |= V_EC_CREDITS(FW_WR_NUM);
  491. base_addr >>= 16;
  492. ctx1 = (u32) base_addr;
  493. base_addr >>= 32;
  494. ctx1 |= ((u64) (V_EC_BASE_HI((u32) base_addr & 0xf) | V_EC_RESPQ(0) |
  495. V_EC_TYPE(0) | V_EC_GEN(1) |
  496. V_EC_UP_TOKEN(T3_CTL_QP_TID) | F_EC_VALID)) << 32;
  497. wqe = skb_put_zero(skb, sizeof(*wqe));
  498. build_fw_riwrh((struct fw_riwrh *) wqe, T3_WR_QP_MOD, 0, 0,
  499. T3_CTL_QP_TID, 7, T3_SOPEOP);
  500. wqe->flags = cpu_to_be32(MODQP_WRITE_EC);
  501. sge_cmd = (3ULL << 56) | FW_RI_SGEEC_START << 8 | 3;
  502. wqe->sge_cmd = cpu_to_be64(sge_cmd);
  503. wqe->ctx1 = cpu_to_be64(ctx1);
  504. wqe->ctx0 = cpu_to_be64(ctx0);
  505. pr_debug("CtrlQP dma_addr 0x%llx workq %p size %d\n",
  506. (unsigned long long)rdev_p->ctrl_qp.dma_addr,
  507. rdev_p->ctrl_qp.workq, 1 << T3_CTRL_QP_SIZE_LOG2);
  508. skb->priority = CPL_PRIORITY_CONTROL;
  509. return iwch_cxgb3_ofld_send(rdev_p->t3cdev_p, skb);
  510. err:
  511. kfree_skb(skb);
  512. return err;
  513. }
  514. static int cxio_hal_destroy_ctrl_qp(struct cxio_rdev *rdev_p)
  515. {
  516. dma_free_coherent(&(rdev_p->rnic_info.pdev->dev),
  517. (1UL << T3_CTRL_QP_SIZE_LOG2)
  518. * sizeof(union t3_wr), rdev_p->ctrl_qp.workq,
  519. dma_unmap_addr(&rdev_p->ctrl_qp, mapping));
  520. return cxio_hal_clear_qp_ctx(rdev_p, T3_CTRL_QP_ID);
  521. }
  522. /* write len bytes of data into addr (32B aligned address)
  523. * If data is NULL, clear len byte of memory to zero.
  524. * caller acquires the ctrl_qp lock before the call
  525. */
  526. static int cxio_hal_ctrl_qp_write_mem(struct cxio_rdev *rdev_p, u32 addr,
  527. u32 len, void *data)
  528. {
  529. u32 i, nr_wqe, copy_len;
  530. u8 *copy_data;
  531. u8 wr_len, utx_len; /* length in 8 byte flit */
  532. enum t3_wr_flags flag;
  533. __be64 *wqe;
  534. u64 utx_cmd;
  535. addr &= 0x7FFFFFF;
  536. nr_wqe = len % 96 ? len / 96 + 1 : len / 96; /* 96B max per WQE */
  537. pr_debug("%s wptr 0x%x rptr 0x%x len %d, nr_wqe %d data %p addr 0x%0x\n",
  538. __func__, rdev_p->ctrl_qp.wptr, rdev_p->ctrl_qp.rptr, len,
  539. nr_wqe, data, addr);
  540. utx_len = 3; /* in 32B unit */
  541. for (i = 0; i < nr_wqe; i++) {
  542. if (Q_FULL(rdev_p->ctrl_qp.rptr, rdev_p->ctrl_qp.wptr,
  543. T3_CTRL_QP_SIZE_LOG2)) {
  544. pr_debug("%s ctrl_qp full wtpr 0x%0x rptr 0x%0x, wait for more space i %d\n",
  545. __func__,
  546. rdev_p->ctrl_qp.wptr, rdev_p->ctrl_qp.rptr, i);
  547. if (wait_event_interruptible(rdev_p->ctrl_qp.waitq,
  548. !Q_FULL(rdev_p->ctrl_qp.rptr,
  549. rdev_p->ctrl_qp.wptr,
  550. T3_CTRL_QP_SIZE_LOG2))) {
  551. pr_debug("%s ctrl_qp workq interrupted\n",
  552. __func__);
  553. return -ERESTARTSYS;
  554. }
  555. pr_debug("%s ctrl_qp wakeup, continue posting work request i %d\n",
  556. __func__, i);
  557. }
  558. wqe = (__be64 *)(rdev_p->ctrl_qp.workq + (rdev_p->ctrl_qp.wptr %
  559. (1 << T3_CTRL_QP_SIZE_LOG2)));
  560. flag = 0;
  561. if (i == (nr_wqe - 1)) {
  562. /* last WQE */
  563. flag = T3_COMPLETION_FLAG;
  564. if (len % 32)
  565. utx_len = len / 32 + 1;
  566. else
  567. utx_len = len / 32;
  568. }
  569. /*
  570. * Force a CQE to return the credit to the workq in case
  571. * we posted more than half the max QP size of WRs
  572. */
  573. if ((i != 0) &&
  574. (i % (((1 << T3_CTRL_QP_SIZE_LOG2)) >> 1) == 0)) {
  575. flag = T3_COMPLETION_FLAG;
  576. pr_debug("%s force completion at i %d\n", __func__, i);
  577. }
  578. /* build the utx mem command */
  579. wqe += (sizeof(struct t3_bypass_wr) >> 3);
  580. utx_cmd = (T3_UTX_MEM_WRITE << 28) | (addr + i * 3);
  581. utx_cmd <<= 32;
  582. utx_cmd |= (utx_len << 28) | ((utx_len << 2) + 1);
  583. *wqe = cpu_to_be64(utx_cmd);
  584. wqe++;
  585. copy_data = (u8 *) data + i * 96;
  586. copy_len = len > 96 ? 96 : len;
  587. /* clear memory content if data is NULL */
  588. if (data)
  589. memcpy(wqe, copy_data, copy_len);
  590. else
  591. memset(wqe, 0, copy_len);
  592. if (copy_len % 32)
  593. memset(((u8 *) wqe) + copy_len, 0,
  594. 32 - (copy_len % 32));
  595. wr_len = ((sizeof(struct t3_bypass_wr)) >> 3) + 1 +
  596. (utx_len << 2);
  597. wqe = (__be64 *)(rdev_p->ctrl_qp.workq + (rdev_p->ctrl_qp.wptr %
  598. (1 << T3_CTRL_QP_SIZE_LOG2)));
  599. /* wptr in the WRID[31:0] */
  600. ((union t3_wrid *)(wqe+1))->id0.low = rdev_p->ctrl_qp.wptr;
  601. /*
  602. * This must be the last write with a memory barrier
  603. * for the genbit
  604. */
  605. build_fw_riwrh((struct fw_riwrh *) wqe, T3_WR_BP, flag,
  606. Q_GENBIT(rdev_p->ctrl_qp.wptr,
  607. T3_CTRL_QP_SIZE_LOG2), T3_CTRL_QP_ID,
  608. wr_len, T3_SOPEOP);
  609. if (flag == T3_COMPLETION_FLAG)
  610. ring_doorbell(rdev_p->ctrl_qp.doorbell, T3_CTRL_QP_ID);
  611. len -= 96;
  612. rdev_p->ctrl_qp.wptr++;
  613. }
  614. return 0;
  615. }
  616. /* IN: stag key, pdid, perm, zbva, to, len, page_size, pbl_size and pbl_addr
  617. * OUT: stag index
  618. * TBD: shared memory region support
  619. */
  620. static int __cxio_tpt_op(struct cxio_rdev *rdev_p, u32 reset_tpt_entry,
  621. u32 *stag, u8 stag_state, u32 pdid,
  622. enum tpt_mem_type type, enum tpt_mem_perm perm,
  623. u32 zbva, u64 to, u32 len, u8 page_size,
  624. u32 pbl_size, u32 pbl_addr)
  625. {
  626. int err;
  627. struct tpt_entry tpt;
  628. u32 stag_idx;
  629. u32 wptr;
  630. if (cxio_fatal_error(rdev_p))
  631. return -EIO;
  632. stag_state = stag_state > 0;
  633. stag_idx = (*stag) >> 8;
  634. if ((!reset_tpt_entry) && !(*stag != T3_STAG_UNSET)) {
  635. stag_idx = cxio_hal_get_stag(rdev_p->rscp);
  636. if (!stag_idx)
  637. return -ENOMEM;
  638. *stag = (stag_idx << 8) | ((*stag) & 0xFF);
  639. }
  640. pr_debug("%s stag_state 0x%0x type 0x%0x pdid 0x%0x, stag_idx 0x%x\n",
  641. __func__, stag_state, type, pdid, stag_idx);
  642. mutex_lock(&rdev_p->ctrl_qp.lock);
  643. /* write TPT entry */
  644. if (reset_tpt_entry)
  645. memset(&tpt, 0, sizeof(tpt));
  646. else {
  647. tpt.valid_stag_pdid = cpu_to_be32(F_TPT_VALID |
  648. V_TPT_STAG_KEY((*stag) & M_TPT_STAG_KEY) |
  649. V_TPT_STAG_STATE(stag_state) |
  650. V_TPT_STAG_TYPE(type) | V_TPT_PDID(pdid));
  651. BUG_ON(page_size >= 28);
  652. tpt.flags_pagesize_qpid = cpu_to_be32(V_TPT_PERM(perm) |
  653. ((perm & TPT_MW_BIND) ? F_TPT_MW_BIND_ENABLE : 0) |
  654. V_TPT_ADDR_TYPE((zbva ? TPT_ZBTO : TPT_VATO)) |
  655. V_TPT_PAGE_SIZE(page_size));
  656. tpt.rsvd_pbl_addr = cpu_to_be32(V_TPT_PBL_ADDR(PBL_OFF(rdev_p, pbl_addr)>>3));
  657. tpt.len = cpu_to_be32(len);
  658. tpt.va_hi = cpu_to_be32((u32) (to >> 32));
  659. tpt.va_low_or_fbo = cpu_to_be32((u32) (to & 0xFFFFFFFFULL));
  660. tpt.rsvd_bind_cnt_or_pstag = 0;
  661. tpt.rsvd_pbl_size = cpu_to_be32(V_TPT_PBL_SIZE(pbl_size >> 2));
  662. }
  663. err = cxio_hal_ctrl_qp_write_mem(rdev_p,
  664. stag_idx +
  665. (rdev_p->rnic_info.tpt_base >> 5),
  666. sizeof(tpt), &tpt);
  667. /* release the stag index to free pool */
  668. if (reset_tpt_entry)
  669. cxio_hal_put_stag(rdev_p->rscp, stag_idx);
  670. wptr = rdev_p->ctrl_qp.wptr;
  671. mutex_unlock(&rdev_p->ctrl_qp.lock);
  672. if (!err)
  673. if (wait_event_interruptible(rdev_p->ctrl_qp.waitq,
  674. SEQ32_GE(rdev_p->ctrl_qp.rptr,
  675. wptr)))
  676. return -ERESTARTSYS;
  677. return err;
  678. }
  679. int cxio_write_pbl(struct cxio_rdev *rdev_p, __be64 *pbl,
  680. u32 pbl_addr, u32 pbl_size)
  681. {
  682. u32 wptr;
  683. int err;
  684. pr_debug("%s *pdb_addr 0x%x, pbl_base 0x%x, pbl_size %d\n",
  685. __func__, pbl_addr, rdev_p->rnic_info.pbl_base,
  686. pbl_size);
  687. mutex_lock(&rdev_p->ctrl_qp.lock);
  688. err = cxio_hal_ctrl_qp_write_mem(rdev_p, pbl_addr >> 5, pbl_size << 3,
  689. pbl);
  690. wptr = rdev_p->ctrl_qp.wptr;
  691. mutex_unlock(&rdev_p->ctrl_qp.lock);
  692. if (err)
  693. return err;
  694. if (wait_event_interruptible(rdev_p->ctrl_qp.waitq,
  695. SEQ32_GE(rdev_p->ctrl_qp.rptr,
  696. wptr)))
  697. return -ERESTARTSYS;
  698. return 0;
  699. }
  700. int cxio_register_phys_mem(struct cxio_rdev *rdev_p, u32 *stag, u32 pdid,
  701. enum tpt_mem_perm perm, u32 zbva, u64 to, u32 len,
  702. u8 page_size, u32 pbl_size, u32 pbl_addr)
  703. {
  704. *stag = T3_STAG_UNSET;
  705. return __cxio_tpt_op(rdev_p, 0, stag, 1, pdid, TPT_NON_SHARED_MR, perm,
  706. zbva, to, len, page_size, pbl_size, pbl_addr);
  707. }
  708. int cxio_reregister_phys_mem(struct cxio_rdev *rdev_p, u32 *stag, u32 pdid,
  709. enum tpt_mem_perm perm, u32 zbva, u64 to, u32 len,
  710. u8 page_size, u32 pbl_size, u32 pbl_addr)
  711. {
  712. return __cxio_tpt_op(rdev_p, 0, stag, 1, pdid, TPT_NON_SHARED_MR, perm,
  713. zbva, to, len, page_size, pbl_size, pbl_addr);
  714. }
  715. int cxio_dereg_mem(struct cxio_rdev *rdev_p, u32 stag, u32 pbl_size,
  716. u32 pbl_addr)
  717. {
  718. return __cxio_tpt_op(rdev_p, 1, &stag, 0, 0, 0, 0, 0, 0ULL, 0, 0,
  719. pbl_size, pbl_addr);
  720. }
  721. int cxio_allocate_window(struct cxio_rdev *rdev_p, u32 * stag, u32 pdid)
  722. {
  723. *stag = T3_STAG_UNSET;
  724. return __cxio_tpt_op(rdev_p, 0, stag, 0, pdid, TPT_MW, 0, 0, 0ULL, 0, 0,
  725. 0, 0);
  726. }
  727. int cxio_deallocate_window(struct cxio_rdev *rdev_p, u32 stag)
  728. {
  729. return __cxio_tpt_op(rdev_p, 1, &stag, 0, 0, 0, 0, 0, 0ULL, 0, 0,
  730. 0, 0);
  731. }
  732. int cxio_allocate_stag(struct cxio_rdev *rdev_p, u32 *stag, u32 pdid, u32 pbl_size, u32 pbl_addr)
  733. {
  734. *stag = T3_STAG_UNSET;
  735. return __cxio_tpt_op(rdev_p, 0, stag, 0, pdid, TPT_NON_SHARED_MR,
  736. 0, 0, 0ULL, 0, 0, pbl_size, pbl_addr);
  737. }
  738. int cxio_rdma_init(struct cxio_rdev *rdev_p, struct t3_rdma_init_attr *attr)
  739. {
  740. struct t3_rdma_init_wr *wqe;
  741. struct sk_buff *skb = alloc_skb(sizeof(*wqe), GFP_ATOMIC);
  742. if (!skb)
  743. return -ENOMEM;
  744. pr_debug("%s rdev_p %p\n", __func__, rdev_p);
  745. wqe = __skb_put(skb, sizeof(*wqe));
  746. wqe->wrh.op_seop_flags = cpu_to_be32(V_FW_RIWR_OP(T3_WR_INIT));
  747. wqe->wrh.gen_tid_len = cpu_to_be32(V_FW_RIWR_TID(attr->tid) |
  748. V_FW_RIWR_LEN(sizeof(*wqe) >> 3));
  749. wqe->wrid.id1 = 0;
  750. wqe->qpid = cpu_to_be32(attr->qpid);
  751. wqe->pdid = cpu_to_be32(attr->pdid);
  752. wqe->scqid = cpu_to_be32(attr->scqid);
  753. wqe->rcqid = cpu_to_be32(attr->rcqid);
  754. wqe->rq_addr = cpu_to_be32(attr->rq_addr - rdev_p->rnic_info.rqt_base);
  755. wqe->rq_size = cpu_to_be32(attr->rq_size);
  756. wqe->mpaattrs = attr->mpaattrs;
  757. wqe->qpcaps = attr->qpcaps;
  758. wqe->ulpdu_size = cpu_to_be16(attr->tcp_emss);
  759. wqe->rqe_count = cpu_to_be16(attr->rqe_count);
  760. wqe->flags_rtr_type = cpu_to_be16(attr->flags |
  761. V_RTR_TYPE(attr->rtr_type) |
  762. V_CHAN(attr->chan));
  763. wqe->ord = cpu_to_be32(attr->ord);
  764. wqe->ird = cpu_to_be32(attr->ird);
  765. wqe->qp_dma_addr = cpu_to_be64(attr->qp_dma_addr);
  766. wqe->qp_dma_size = cpu_to_be32(attr->qp_dma_size);
  767. wqe->irs = cpu_to_be32(attr->irs);
  768. skb->priority = 0; /* 0=>ToeQ; 1=>CtrlQ */
  769. return iwch_cxgb3_ofld_send(rdev_p->t3cdev_p, skb);
  770. }
  771. void cxio_register_ev_cb(cxio_hal_ev_callback_func_t ev_cb)
  772. {
  773. cxio_ev_cb = ev_cb;
  774. }
  775. void cxio_unregister_ev_cb(cxio_hal_ev_callback_func_t ev_cb)
  776. {
  777. cxio_ev_cb = NULL;
  778. }
  779. static int cxio_hal_ev_handler(struct t3cdev *t3cdev_p, struct sk_buff *skb)
  780. {
  781. static int cnt;
  782. struct cxio_rdev *rdev_p = NULL;
  783. struct respQ_msg_t *rsp_msg = (struct respQ_msg_t *) skb->data;
  784. pr_debug("%d: %s cq_id 0x%x cq_ptr 0x%x genbit %0x overflow %0x an %0x se %0x notify %0x cqbranch %0x creditth %0x\n",
  785. cnt, __func__, RSPQ_CQID(rsp_msg), RSPQ_CQPTR(rsp_msg),
  786. RSPQ_GENBIT(rsp_msg), RSPQ_OVERFLOW(rsp_msg), RSPQ_AN(rsp_msg),
  787. RSPQ_SE(rsp_msg), RSPQ_NOTIFY(rsp_msg), RSPQ_CQBRANCH(rsp_msg),
  788. RSPQ_CREDIT_THRESH(rsp_msg));
  789. pr_debug("CQE: QPID 0x%0x genbit %0x type 0x%0x status 0x%0x opcode %d len 0x%0x wrid_hi_stag 0x%x wrid_low_msn 0x%x\n",
  790. CQE_QPID(rsp_msg->cqe), CQE_GENBIT(rsp_msg->cqe),
  791. CQE_TYPE(rsp_msg->cqe), CQE_STATUS(rsp_msg->cqe),
  792. CQE_OPCODE(rsp_msg->cqe), CQE_LEN(rsp_msg->cqe),
  793. CQE_WRID_HI(rsp_msg->cqe), CQE_WRID_LOW(rsp_msg->cqe));
  794. rdev_p = (struct cxio_rdev *)t3cdev_p->ulp;
  795. if (!rdev_p) {
  796. pr_debug("%s called by t3cdev %p with null ulp\n", __func__,
  797. t3cdev_p);
  798. return 0;
  799. }
  800. if (CQE_QPID(rsp_msg->cqe) == T3_CTRL_QP_ID) {
  801. rdev_p->ctrl_qp.rptr = CQE_WRID_LOW(rsp_msg->cqe) + 1;
  802. wake_up_interruptible(&rdev_p->ctrl_qp.waitq);
  803. dev_kfree_skb_irq(skb);
  804. } else if (CQE_QPID(rsp_msg->cqe) == 0xfff8)
  805. dev_kfree_skb_irq(skb);
  806. else if (cxio_ev_cb)
  807. (*cxio_ev_cb) (rdev_p, skb);
  808. else
  809. dev_kfree_skb_irq(skb);
  810. cnt++;
  811. return 0;
  812. }
  813. /* Caller takes care of locking if needed */
  814. int cxio_rdev_open(struct cxio_rdev *rdev_p)
  815. {
  816. struct net_device *netdev_p = NULL;
  817. int err = 0;
  818. if (strlen(rdev_p->dev_name)) {
  819. if (cxio_hal_find_rdev_by_name(rdev_p->dev_name)) {
  820. return -EBUSY;
  821. }
  822. netdev_p = dev_get_by_name(&init_net, rdev_p->dev_name);
  823. if (!netdev_p) {
  824. return -EINVAL;
  825. }
  826. dev_put(netdev_p);
  827. } else if (rdev_p->t3cdev_p) {
  828. if (cxio_hal_find_rdev_by_t3cdev(rdev_p->t3cdev_p)) {
  829. return -EBUSY;
  830. }
  831. netdev_p = rdev_p->t3cdev_p->lldev;
  832. strncpy(rdev_p->dev_name, rdev_p->t3cdev_p->name,
  833. T3_MAX_DEV_NAME_LEN);
  834. } else {
  835. pr_debug("%s t3cdev_p or dev_name must be set\n", __func__);
  836. return -EINVAL;
  837. }
  838. list_add_tail(&rdev_p->entry, &rdev_list);
  839. pr_debug("%s opening rnic dev %s\n", __func__, rdev_p->dev_name);
  840. memset(&rdev_p->ctrl_qp, 0, sizeof(rdev_p->ctrl_qp));
  841. if (!rdev_p->t3cdev_p)
  842. rdev_p->t3cdev_p = dev2t3cdev(netdev_p);
  843. rdev_p->t3cdev_p->ulp = (void *) rdev_p;
  844. err = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, GET_EMBEDDED_INFO,
  845. &(rdev_p->fw_info));
  846. if (err) {
  847. pr_err("%s t3cdev_p(%p)->ctl returned error %d\n",
  848. __func__, rdev_p->t3cdev_p, err);
  849. goto err1;
  850. }
  851. if (G_FW_VERSION_MAJOR(rdev_p->fw_info.fw_vers) != CXIO_FW_MAJ) {
  852. pr_err("fatal firmware version mismatch: need version %u but adapter has version %u\n",
  853. CXIO_FW_MAJ,
  854. G_FW_VERSION_MAJOR(rdev_p->fw_info.fw_vers));
  855. err = -EINVAL;
  856. goto err1;
  857. }
  858. err = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_GET_PARAMS,
  859. &(rdev_p->rnic_info));
  860. if (err) {
  861. pr_err("%s t3cdev_p(%p)->ctl returned error %d\n",
  862. __func__, rdev_p->t3cdev_p, err);
  863. goto err1;
  864. }
  865. err = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, GET_PORTS,
  866. &(rdev_p->port_info));
  867. if (err) {
  868. pr_err("%s t3cdev_p(%p)->ctl returned error %d\n",
  869. __func__, rdev_p->t3cdev_p, err);
  870. goto err1;
  871. }
  872. /*
  873. * qpshift is the number of bits to shift the qpid left in order
  874. * to get the correct address of the doorbell for that qp.
  875. */
  876. cxio_init_ucontext(rdev_p, &rdev_p->uctx);
  877. rdev_p->qpshift = PAGE_SHIFT -
  878. ilog2(65536 >>
  879. ilog2(rdev_p->rnic_info.udbell_len >>
  880. PAGE_SHIFT));
  881. rdev_p->qpnr = rdev_p->rnic_info.udbell_len >> PAGE_SHIFT;
  882. rdev_p->qpmask = (65536 >> ilog2(rdev_p->qpnr)) - 1;
  883. pr_debug("%s rnic %s info: tpt_base 0x%0x tpt_top 0x%0x num stags %d pbl_base 0x%0x pbl_top 0x%0x rqt_base 0x%0x, rqt_top 0x%0x\n",
  884. __func__, rdev_p->dev_name, rdev_p->rnic_info.tpt_base,
  885. rdev_p->rnic_info.tpt_top, cxio_num_stags(rdev_p),
  886. rdev_p->rnic_info.pbl_base,
  887. rdev_p->rnic_info.pbl_top, rdev_p->rnic_info.rqt_base,
  888. rdev_p->rnic_info.rqt_top);
  889. pr_debug("udbell_len 0x%0x udbell_physbase 0x%lx kdb_addr %p qpshift %lu qpnr %d qpmask 0x%x\n",
  890. rdev_p->rnic_info.udbell_len,
  891. rdev_p->rnic_info.udbell_physbase, rdev_p->rnic_info.kdb_addr,
  892. rdev_p->qpshift, rdev_p->qpnr, rdev_p->qpmask);
  893. err = cxio_hal_init_ctrl_qp(rdev_p);
  894. if (err) {
  895. pr_err("%s error %d initializing ctrl_qp\n", __func__, err);
  896. goto err1;
  897. }
  898. err = cxio_hal_init_resource(rdev_p, cxio_num_stags(rdev_p), 0,
  899. 0, T3_MAX_NUM_QP, T3_MAX_NUM_CQ,
  900. T3_MAX_NUM_PD);
  901. if (err) {
  902. pr_err("%s error %d initializing hal resources\n",
  903. __func__, err);
  904. goto err2;
  905. }
  906. err = cxio_hal_pblpool_create(rdev_p);
  907. if (err) {
  908. pr_err("%s error %d initializing pbl mem pool\n",
  909. __func__, err);
  910. goto err3;
  911. }
  912. err = cxio_hal_rqtpool_create(rdev_p);
  913. if (err) {
  914. pr_err("%s error %d initializing rqt mem pool\n",
  915. __func__, err);
  916. goto err4;
  917. }
  918. return 0;
  919. err4:
  920. cxio_hal_pblpool_destroy(rdev_p);
  921. err3:
  922. cxio_hal_destroy_resource(rdev_p->rscp);
  923. err2:
  924. cxio_hal_destroy_ctrl_qp(rdev_p);
  925. err1:
  926. rdev_p->t3cdev_p->ulp = NULL;
  927. list_del(&rdev_p->entry);
  928. return err;
  929. }
  930. void cxio_rdev_close(struct cxio_rdev *rdev_p)
  931. {
  932. if (rdev_p) {
  933. cxio_hal_pblpool_destroy(rdev_p);
  934. cxio_hal_rqtpool_destroy(rdev_p);
  935. list_del(&rdev_p->entry);
  936. cxio_hal_destroy_ctrl_qp(rdev_p);
  937. cxio_hal_destroy_resource(rdev_p->rscp);
  938. rdev_p->t3cdev_p->ulp = NULL;
  939. }
  940. }
  941. int __init cxio_hal_init(void)
  942. {
  943. if (cxio_hal_init_rhdl_resource(T3_MAX_NUM_RI))
  944. return -ENOMEM;
  945. t3_register_cpl_handler(CPL_ASYNC_NOTIF, cxio_hal_ev_handler);
  946. return 0;
  947. }
  948. void __exit cxio_hal_exit(void)
  949. {
  950. struct cxio_rdev *rdev, *tmp;
  951. t3_register_cpl_handler(CPL_ASYNC_NOTIF, NULL);
  952. list_for_each_entry_safe(rdev, tmp, &rdev_list, entry)
  953. cxio_rdev_close(rdev);
  954. cxio_hal_destroy_rhdl_resource();
  955. }
  956. static void flush_completed_wrs(struct t3_wq *wq, struct t3_cq *cq)
  957. {
  958. struct t3_swsq *sqp;
  959. __u32 ptr = wq->sq_rptr;
  960. int count = Q_COUNT(wq->sq_rptr, wq->sq_wptr);
  961. sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
  962. while (count--)
  963. if (!sqp->signaled) {
  964. ptr++;
  965. sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
  966. } else if (sqp->complete) {
  967. /*
  968. * Insert this completed cqe into the swcq.
  969. */
  970. pr_debug("%s moving cqe into swcq sq idx %ld cq idx %ld\n",
  971. __func__, Q_PTR2IDX(ptr, wq->sq_size_log2),
  972. Q_PTR2IDX(cq->sw_wptr, cq->size_log2));
  973. sqp->cqe.header |= htonl(V_CQE_SWCQE(1));
  974. *(cq->sw_queue + Q_PTR2IDX(cq->sw_wptr, cq->size_log2))
  975. = sqp->cqe;
  976. cq->sw_wptr++;
  977. sqp->signaled = 0;
  978. break;
  979. } else
  980. break;
  981. }
  982. static void create_read_req_cqe(struct t3_wq *wq, struct t3_cqe *hw_cqe,
  983. struct t3_cqe *read_cqe)
  984. {
  985. read_cqe->u.scqe.wrid_hi = wq->oldest_read->sq_wptr;
  986. read_cqe->len = wq->oldest_read->read_len;
  987. read_cqe->header = htonl(V_CQE_QPID(CQE_QPID(*hw_cqe)) |
  988. V_CQE_SWCQE(SW_CQE(*hw_cqe)) |
  989. V_CQE_OPCODE(T3_READ_REQ) |
  990. V_CQE_TYPE(1));
  991. }
  992. /*
  993. * Return a ptr to the next read wr in the SWSQ or NULL.
  994. */
  995. static void advance_oldest_read(struct t3_wq *wq)
  996. {
  997. u32 rptr = wq->oldest_read - wq->sq + 1;
  998. u32 wptr = Q_PTR2IDX(wq->sq_wptr, wq->sq_size_log2);
  999. while (Q_PTR2IDX(rptr, wq->sq_size_log2) != wptr) {
  1000. wq->oldest_read = wq->sq + Q_PTR2IDX(rptr, wq->sq_size_log2);
  1001. if (wq->oldest_read->opcode == T3_READ_REQ)
  1002. return;
  1003. rptr++;
  1004. }
  1005. wq->oldest_read = NULL;
  1006. }
  1007. /*
  1008. * cxio_poll_cq
  1009. *
  1010. * Caller must:
  1011. * check the validity of the first CQE,
  1012. * supply the wq assicated with the qpid.
  1013. *
  1014. * credit: cq credit to return to sge.
  1015. * cqe_flushed: 1 iff the CQE is flushed.
  1016. * cqe: copy of the polled CQE.
  1017. *
  1018. * return value:
  1019. * 0 CQE returned,
  1020. * -1 CQE skipped, try again.
  1021. */
  1022. int cxio_poll_cq(struct t3_wq *wq, struct t3_cq *cq, struct t3_cqe *cqe,
  1023. u8 *cqe_flushed, u64 *cookie, u32 *credit)
  1024. {
  1025. int ret = 0;
  1026. struct t3_cqe *hw_cqe, read_cqe;
  1027. *cqe_flushed = 0;
  1028. *credit = 0;
  1029. hw_cqe = cxio_next_cqe(cq);
  1030. pr_debug("%s CQE OOO %d qpid 0x%0x genbit %d type %d status 0x%0x opcode 0x%0x len 0x%0x wrid_hi_stag 0x%x wrid_low_msn 0x%x\n",
  1031. __func__, CQE_OOO(*hw_cqe), CQE_QPID(*hw_cqe),
  1032. CQE_GENBIT(*hw_cqe), CQE_TYPE(*hw_cqe), CQE_STATUS(*hw_cqe),
  1033. CQE_OPCODE(*hw_cqe), CQE_LEN(*hw_cqe), CQE_WRID_HI(*hw_cqe),
  1034. CQE_WRID_LOW(*hw_cqe));
  1035. /*
  1036. * skip cqe's not affiliated with a QP.
  1037. */
  1038. if (wq == NULL) {
  1039. ret = -1;
  1040. goto skip_cqe;
  1041. }
  1042. /*
  1043. * Gotta tweak READ completions:
  1044. * 1) the cqe doesn't contain the sq_wptr from the wr.
  1045. * 2) opcode not reflected from the wr.
  1046. * 3) read_len not reflected from the wr.
  1047. * 4) cq_type is RQ_TYPE not SQ_TYPE.
  1048. */
  1049. if (RQ_TYPE(*hw_cqe) && (CQE_OPCODE(*hw_cqe) == T3_READ_RESP)) {
  1050. /*
  1051. * If this is an unsolicited read response, then the read
  1052. * was generated by the kernel driver as part of peer-2-peer
  1053. * connection setup. So ignore the completion.
  1054. */
  1055. if (!wq->oldest_read) {
  1056. if (CQE_STATUS(*hw_cqe))
  1057. wq->error = 1;
  1058. ret = -1;
  1059. goto skip_cqe;
  1060. }
  1061. /*
  1062. * Don't write to the HWCQ, so create a new read req CQE
  1063. * in local memory.
  1064. */
  1065. create_read_req_cqe(wq, hw_cqe, &read_cqe);
  1066. hw_cqe = &read_cqe;
  1067. advance_oldest_read(wq);
  1068. }
  1069. /*
  1070. * T3A: Discard TERMINATE CQEs.
  1071. */
  1072. if (CQE_OPCODE(*hw_cqe) == T3_TERMINATE) {
  1073. ret = -1;
  1074. wq->error = 1;
  1075. goto skip_cqe;
  1076. }
  1077. if (CQE_STATUS(*hw_cqe) || wq->error) {
  1078. *cqe_flushed = wq->error;
  1079. wq->error = 1;
  1080. /*
  1081. * T3A inserts errors into the CQE. We cannot return
  1082. * these as work completions.
  1083. */
  1084. /* incoming write failures */
  1085. if ((CQE_OPCODE(*hw_cqe) == T3_RDMA_WRITE)
  1086. && RQ_TYPE(*hw_cqe)) {
  1087. ret = -1;
  1088. goto skip_cqe;
  1089. }
  1090. /* incoming read request failures */
  1091. if ((CQE_OPCODE(*hw_cqe) == T3_READ_RESP) && SQ_TYPE(*hw_cqe)) {
  1092. ret = -1;
  1093. goto skip_cqe;
  1094. }
  1095. /* incoming SEND with no receive posted failures */
  1096. if (CQE_SEND_OPCODE(*hw_cqe) && RQ_TYPE(*hw_cqe) &&
  1097. Q_EMPTY(wq->rq_rptr, wq->rq_wptr)) {
  1098. ret = -1;
  1099. goto skip_cqe;
  1100. }
  1101. BUG_ON((*cqe_flushed == 0) && !SW_CQE(*hw_cqe));
  1102. goto proc_cqe;
  1103. }
  1104. /*
  1105. * RECV completion.
  1106. */
  1107. if (RQ_TYPE(*hw_cqe)) {
  1108. /*
  1109. * HW only validates 4 bits of MSN. So we must validate that
  1110. * the MSN in the SEND is the next expected MSN. If its not,
  1111. * then we complete this with TPT_ERR_MSN and mark the wq in
  1112. * error.
  1113. */
  1114. if (Q_EMPTY(wq->rq_rptr, wq->rq_wptr)) {
  1115. wq->error = 1;
  1116. ret = -1;
  1117. goto skip_cqe;
  1118. }
  1119. if (unlikely((CQE_WRID_MSN(*hw_cqe) != (wq->rq_rptr + 1)))) {
  1120. wq->error = 1;
  1121. hw_cqe->header |= htonl(V_CQE_STATUS(TPT_ERR_MSN));
  1122. goto proc_cqe;
  1123. }
  1124. goto proc_cqe;
  1125. }
  1126. /*
  1127. * If we get here its a send completion.
  1128. *
  1129. * Handle out of order completion. These get stuffed
  1130. * in the SW SQ. Then the SW SQ is walked to move any
  1131. * now in-order completions into the SW CQ. This handles
  1132. * 2 cases:
  1133. * 1) reaping unsignaled WRs when the first subsequent
  1134. * signaled WR is completed.
  1135. * 2) out of order read completions.
  1136. */
  1137. if (!SW_CQE(*hw_cqe) && (CQE_WRID_SQ_WPTR(*hw_cqe) != wq->sq_rptr)) {
  1138. struct t3_swsq *sqp;
  1139. pr_debug("%s out of order completion going in swsq at idx %ld\n",
  1140. __func__,
  1141. Q_PTR2IDX(CQE_WRID_SQ_WPTR(*hw_cqe),
  1142. wq->sq_size_log2));
  1143. sqp = wq->sq +
  1144. Q_PTR2IDX(CQE_WRID_SQ_WPTR(*hw_cqe), wq->sq_size_log2);
  1145. sqp->cqe = *hw_cqe;
  1146. sqp->complete = 1;
  1147. ret = -1;
  1148. goto flush_wq;
  1149. }
  1150. proc_cqe:
  1151. *cqe = *hw_cqe;
  1152. /*
  1153. * Reap the associated WR(s) that are freed up with this
  1154. * completion.
  1155. */
  1156. if (SQ_TYPE(*hw_cqe)) {
  1157. wq->sq_rptr = CQE_WRID_SQ_WPTR(*hw_cqe);
  1158. pr_debug("%s completing sq idx %ld\n", __func__,
  1159. Q_PTR2IDX(wq->sq_rptr, wq->sq_size_log2));
  1160. *cookie = wq->sq[Q_PTR2IDX(wq->sq_rptr, wq->sq_size_log2)].wr_id;
  1161. wq->sq_rptr++;
  1162. } else {
  1163. pr_debug("%s completing rq idx %ld\n", __func__,
  1164. Q_PTR2IDX(wq->rq_rptr, wq->rq_size_log2));
  1165. *cookie = wq->rq[Q_PTR2IDX(wq->rq_rptr, wq->rq_size_log2)].wr_id;
  1166. if (wq->rq[Q_PTR2IDX(wq->rq_rptr, wq->rq_size_log2)].pbl_addr)
  1167. cxio_hal_pblpool_free(wq->rdev,
  1168. wq->rq[Q_PTR2IDX(wq->rq_rptr,
  1169. wq->rq_size_log2)].pbl_addr, T3_STAG0_PBL_SIZE);
  1170. BUG_ON(Q_EMPTY(wq->rq_rptr, wq->rq_wptr));
  1171. wq->rq_rptr++;
  1172. }
  1173. flush_wq:
  1174. /*
  1175. * Flush any completed cqes that are now in-order.
  1176. */
  1177. flush_completed_wrs(wq, cq);
  1178. skip_cqe:
  1179. if (SW_CQE(*hw_cqe)) {
  1180. pr_debug("%s cq %p cqid 0x%x skip sw cqe sw_rptr 0x%x\n",
  1181. __func__, cq, cq->cqid, cq->sw_rptr);
  1182. ++cq->sw_rptr;
  1183. } else {
  1184. pr_debug("%s cq %p cqid 0x%x skip hw cqe rptr 0x%x\n",
  1185. __func__, cq, cq->cqid, cq->rptr);
  1186. ++cq->rptr;
  1187. /*
  1188. * T3A: compute credits.
  1189. */
  1190. if (((cq->rptr - cq->wptr) > (1 << (cq->size_log2 - 1)))
  1191. || ((cq->rptr - cq->wptr) >= 128)) {
  1192. *credit = cq->rptr - cq->wptr;
  1193. cq->wptr = cq->rptr;
  1194. }
  1195. }
  1196. return ret;
  1197. }