cxio_hal.c 37 KB

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