cxio_hal.c 36 KB

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