cxio_hal.c 36 KB

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