cxio_hal.c 36 KB

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