cxio_hal.c 36 KB

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