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 = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
  362. while (ptr != wq->sq_wptr) {
  363. insert_sq_cqe(wq, cq, sqp);
  364. ptr++;
  365. sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
  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)
  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 = T3_COMPLETION_FLAG;
  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_size and pbl_addr
  615. * OUT: stag index
  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,
  622. u32 pbl_size, u32 pbl_addr)
  623. {
  624. int err;
  625. struct tpt_entry tpt;
  626. u32 stag_idx;
  627. u32 wptr;
  628. stag_state = stag_state > 0;
  629. stag_idx = (*stag) >> 8;
  630. if ((!reset_tpt_entry) && !(*stag != T3_STAG_UNSET)) {
  631. stag_idx = cxio_hal_get_stag(rdev_p->rscp);
  632. if (!stag_idx)
  633. return -ENOMEM;
  634. *stag = (stag_idx << 8) | ((*stag) & 0xFF);
  635. }
  636. PDBG("%s stag_state 0x%0x type 0x%0x pdid 0x%0x, stag_idx 0x%x\n",
  637. __func__, stag_state, type, pdid, stag_idx);
  638. mutex_lock(&rdev_p->ctrl_qp.lock);
  639. /* write TPT entry */
  640. if (reset_tpt_entry)
  641. memset(&tpt, 0, sizeof(tpt));
  642. else {
  643. tpt.valid_stag_pdid = cpu_to_be32(F_TPT_VALID |
  644. V_TPT_STAG_KEY((*stag) & M_TPT_STAG_KEY) |
  645. V_TPT_STAG_STATE(stag_state) |
  646. V_TPT_STAG_TYPE(type) | V_TPT_PDID(pdid));
  647. BUG_ON(page_size >= 28);
  648. tpt.flags_pagesize_qpid = cpu_to_be32(V_TPT_PERM(perm) |
  649. F_TPT_MW_BIND_ENABLE |
  650. V_TPT_ADDR_TYPE((zbva ? TPT_ZBTO : TPT_VATO)) |
  651. V_TPT_PAGE_SIZE(page_size));
  652. tpt.rsvd_pbl_addr = reset_tpt_entry ? 0 :
  653. cpu_to_be32(V_TPT_PBL_ADDR(PBL_OFF(rdev_p, pbl_addr)>>3));
  654. tpt.len = cpu_to_be32(len);
  655. tpt.va_hi = cpu_to_be32((u32) (to >> 32));
  656. tpt.va_low_or_fbo = cpu_to_be32((u32) (to & 0xFFFFFFFFULL));
  657. tpt.rsvd_bind_cnt_or_pstag = 0;
  658. tpt.rsvd_pbl_size = reset_tpt_entry ? 0 :
  659. cpu_to_be32(V_TPT_PBL_SIZE(pbl_size >> 2));
  660. }
  661. err = cxio_hal_ctrl_qp_write_mem(rdev_p,
  662. stag_idx +
  663. (rdev_p->rnic_info.tpt_base >> 5),
  664. sizeof(tpt), &tpt);
  665. /* release the stag index to free pool */
  666. if (reset_tpt_entry)
  667. cxio_hal_put_stag(rdev_p->rscp, stag_idx);
  668. wptr = rdev_p->ctrl_qp.wptr;
  669. mutex_unlock(&rdev_p->ctrl_qp.lock);
  670. if (!err)
  671. if (wait_event_interruptible(rdev_p->ctrl_qp.waitq,
  672. SEQ32_GE(rdev_p->ctrl_qp.rptr,
  673. wptr)))
  674. return -ERESTARTSYS;
  675. return err;
  676. }
  677. int cxio_write_pbl(struct cxio_rdev *rdev_p, __be64 *pbl,
  678. u32 pbl_addr, u32 pbl_size)
  679. {
  680. u32 wptr;
  681. int err;
  682. PDBG("%s *pdb_addr 0x%x, pbl_base 0x%x, pbl_size %d\n",
  683. __func__, pbl_addr, rdev_p->rnic_info.pbl_base,
  684. pbl_size);
  685. mutex_lock(&rdev_p->ctrl_qp.lock);
  686. err = cxio_hal_ctrl_qp_write_mem(rdev_p, pbl_addr >> 5, pbl_size << 3,
  687. pbl);
  688. wptr = rdev_p->ctrl_qp.wptr;
  689. mutex_unlock(&rdev_p->ctrl_qp.lock);
  690. if (err)
  691. return 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 0;
  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, u32 pbl_size, u32 pbl_addr)
  701. {
  702. *stag = T3_STAG_UNSET;
  703. return __cxio_tpt_op(rdev_p, 0, stag, 1, pdid, TPT_NON_SHARED_MR, perm,
  704. zbva, to, len, page_size, pbl_size, pbl_addr);
  705. }
  706. int cxio_reregister_phys_mem(struct cxio_rdev *rdev_p, u32 *stag, u32 pdid,
  707. enum tpt_mem_perm perm, u32 zbva, u64 to, u32 len,
  708. u8 page_size, u32 pbl_size, u32 pbl_addr)
  709. {
  710. return __cxio_tpt_op(rdev_p, 0, stag, 1, pdid, TPT_NON_SHARED_MR, perm,
  711. zbva, to, len, page_size, pbl_size, pbl_addr);
  712. }
  713. int cxio_dereg_mem(struct cxio_rdev *rdev_p, u32 stag, u32 pbl_size,
  714. u32 pbl_addr)
  715. {
  716. return __cxio_tpt_op(rdev_p, 1, &stag, 0, 0, 0, 0, 0, 0ULL, 0, 0,
  717. pbl_size, pbl_addr);
  718. }
  719. int cxio_allocate_window(struct cxio_rdev *rdev_p, u32 * stag, u32 pdid)
  720. {
  721. *stag = T3_STAG_UNSET;
  722. return __cxio_tpt_op(rdev_p, 0, stag, 0, pdid, TPT_MW, 0, 0, 0ULL, 0, 0,
  723. 0, 0);
  724. }
  725. int cxio_deallocate_window(struct cxio_rdev *rdev_p, u32 stag)
  726. {
  727. return __cxio_tpt_op(rdev_p, 1, &stag, 0, 0, 0, 0, 0, 0ULL, 0, 0,
  728. 0, 0);
  729. }
  730. int cxio_rdma_init(struct cxio_rdev *rdev_p, struct t3_rdma_init_attr *attr)
  731. {
  732. struct t3_rdma_init_wr *wqe;
  733. struct sk_buff *skb = alloc_skb(sizeof(*wqe), GFP_ATOMIC);
  734. if (!skb)
  735. return -ENOMEM;
  736. PDBG("%s rdev_p %p\n", __func__, rdev_p);
  737. wqe = (struct t3_rdma_init_wr *) __skb_put(skb, sizeof(*wqe));
  738. wqe->wrh.op_seop_flags = cpu_to_be32(V_FW_RIWR_OP(T3_WR_INIT));
  739. wqe->wrh.gen_tid_len = cpu_to_be32(V_FW_RIWR_TID(attr->tid) |
  740. V_FW_RIWR_LEN(sizeof(*wqe) >> 3));
  741. wqe->wrid.id1 = 0;
  742. wqe->qpid = cpu_to_be32(attr->qpid);
  743. wqe->pdid = cpu_to_be32(attr->pdid);
  744. wqe->scqid = cpu_to_be32(attr->scqid);
  745. wqe->rcqid = cpu_to_be32(attr->rcqid);
  746. wqe->rq_addr = cpu_to_be32(attr->rq_addr - rdev_p->rnic_info.rqt_base);
  747. wqe->rq_size = cpu_to_be32(attr->rq_size);
  748. wqe->mpaattrs = attr->mpaattrs;
  749. wqe->qpcaps = attr->qpcaps;
  750. wqe->ulpdu_size = cpu_to_be16(attr->tcp_emss);
  751. wqe->rqe_count = cpu_to_be16(attr->rqe_count);
  752. wqe->flags_rtr_type = cpu_to_be16(attr->flags|V_RTR_TYPE(attr->rtr_type));
  753. wqe->ord = cpu_to_be32(attr->ord);
  754. wqe->ird = cpu_to_be32(attr->ird);
  755. wqe->qp_dma_addr = cpu_to_be64(attr->qp_dma_addr);
  756. wqe->qp_dma_size = cpu_to_be32(attr->qp_dma_size);
  757. wqe->irs = cpu_to_be32(attr->irs);
  758. skb->priority = 0; /* 0=>ToeQ; 1=>CtrlQ */
  759. return (cxgb3_ofld_send(rdev_p->t3cdev_p, skb));
  760. }
  761. void cxio_register_ev_cb(cxio_hal_ev_callback_func_t ev_cb)
  762. {
  763. cxio_ev_cb = ev_cb;
  764. }
  765. void cxio_unregister_ev_cb(cxio_hal_ev_callback_func_t ev_cb)
  766. {
  767. cxio_ev_cb = NULL;
  768. }
  769. static int cxio_hal_ev_handler(struct t3cdev *t3cdev_p, struct sk_buff *skb)
  770. {
  771. static int cnt;
  772. struct cxio_rdev *rdev_p = NULL;
  773. struct respQ_msg_t *rsp_msg = (struct respQ_msg_t *) skb->data;
  774. PDBG("%d: %s cq_id 0x%x cq_ptr 0x%x genbit %0x overflow %0x an %0x"
  775. " se %0x notify %0x cqbranch %0x creditth %0x\n",
  776. cnt, __func__, RSPQ_CQID(rsp_msg), RSPQ_CQPTR(rsp_msg),
  777. RSPQ_GENBIT(rsp_msg), RSPQ_OVERFLOW(rsp_msg), RSPQ_AN(rsp_msg),
  778. RSPQ_SE(rsp_msg), RSPQ_NOTIFY(rsp_msg), RSPQ_CQBRANCH(rsp_msg),
  779. RSPQ_CREDIT_THRESH(rsp_msg));
  780. PDBG("CQE: QPID 0x%0x genbit %0x type 0x%0x status 0x%0x opcode %d "
  781. "len 0x%0x wrid_hi_stag 0x%x wrid_low_msn 0x%x\n",
  782. CQE_QPID(rsp_msg->cqe), CQE_GENBIT(rsp_msg->cqe),
  783. CQE_TYPE(rsp_msg->cqe), CQE_STATUS(rsp_msg->cqe),
  784. CQE_OPCODE(rsp_msg->cqe), CQE_LEN(rsp_msg->cqe),
  785. CQE_WRID_HI(rsp_msg->cqe), CQE_WRID_LOW(rsp_msg->cqe));
  786. rdev_p = (struct cxio_rdev *)t3cdev_p->ulp;
  787. if (!rdev_p) {
  788. PDBG("%s called by t3cdev %p with null ulp\n", __func__,
  789. t3cdev_p);
  790. return 0;
  791. }
  792. if (CQE_QPID(rsp_msg->cqe) == T3_CTRL_QP_ID) {
  793. rdev_p->ctrl_qp.rptr = CQE_WRID_LOW(rsp_msg->cqe) + 1;
  794. wake_up_interruptible(&rdev_p->ctrl_qp.waitq);
  795. dev_kfree_skb_irq(skb);
  796. } else if (CQE_QPID(rsp_msg->cqe) == 0xfff8)
  797. dev_kfree_skb_irq(skb);
  798. else if (cxio_ev_cb)
  799. (*cxio_ev_cb) (rdev_p, skb);
  800. else
  801. dev_kfree_skb_irq(skb);
  802. cnt++;
  803. return 0;
  804. }
  805. /* Caller takes care of locking if needed */
  806. int cxio_rdev_open(struct cxio_rdev *rdev_p)
  807. {
  808. struct net_device *netdev_p = NULL;
  809. int err = 0;
  810. if (strlen(rdev_p->dev_name)) {
  811. if (cxio_hal_find_rdev_by_name(rdev_p->dev_name)) {
  812. return -EBUSY;
  813. }
  814. netdev_p = dev_get_by_name(&init_net, rdev_p->dev_name);
  815. if (!netdev_p) {
  816. return -EINVAL;
  817. }
  818. dev_put(netdev_p);
  819. } else if (rdev_p->t3cdev_p) {
  820. if (cxio_hal_find_rdev_by_t3cdev(rdev_p->t3cdev_p)) {
  821. return -EBUSY;
  822. }
  823. netdev_p = rdev_p->t3cdev_p->lldev;
  824. strncpy(rdev_p->dev_name, rdev_p->t3cdev_p->name,
  825. T3_MAX_DEV_NAME_LEN);
  826. } else {
  827. PDBG("%s t3cdev_p or dev_name must be set\n", __func__);
  828. return -EINVAL;
  829. }
  830. list_add_tail(&rdev_p->entry, &rdev_list);
  831. PDBG("%s opening rnic dev %s\n", __func__, rdev_p->dev_name);
  832. memset(&rdev_p->ctrl_qp, 0, sizeof(rdev_p->ctrl_qp));
  833. if (!rdev_p->t3cdev_p)
  834. rdev_p->t3cdev_p = dev2t3cdev(netdev_p);
  835. rdev_p->t3cdev_p->ulp = (void *) rdev_p;
  836. err = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_GET_PARAMS,
  837. &(rdev_p->rnic_info));
  838. if (err) {
  839. printk(KERN_ERR "%s t3cdev_p(%p)->ctl returned error %d.\n",
  840. __func__, rdev_p->t3cdev_p, err);
  841. goto err1;
  842. }
  843. err = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, GET_PORTS,
  844. &(rdev_p->port_info));
  845. if (err) {
  846. printk(KERN_ERR "%s t3cdev_p(%p)->ctl returned error %d.\n",
  847. __func__, rdev_p->t3cdev_p, err);
  848. goto err1;
  849. }
  850. /*
  851. * qpshift is the number of bits to shift the qpid left in order
  852. * to get the correct address of the doorbell for that qp.
  853. */
  854. cxio_init_ucontext(rdev_p, &rdev_p->uctx);
  855. rdev_p->qpshift = PAGE_SHIFT -
  856. ilog2(65536 >>
  857. ilog2(rdev_p->rnic_info.udbell_len >>
  858. PAGE_SHIFT));
  859. rdev_p->qpnr = rdev_p->rnic_info.udbell_len >> PAGE_SHIFT;
  860. rdev_p->qpmask = (65536 >> ilog2(rdev_p->qpnr)) - 1;
  861. PDBG("%s rnic %s info: tpt_base 0x%0x tpt_top 0x%0x num stags %d "
  862. "pbl_base 0x%0x pbl_top 0x%0x rqt_base 0x%0x, rqt_top 0x%0x\n",
  863. __func__, rdev_p->dev_name, rdev_p->rnic_info.tpt_base,
  864. rdev_p->rnic_info.tpt_top, cxio_num_stags(rdev_p),
  865. rdev_p->rnic_info.pbl_base,
  866. rdev_p->rnic_info.pbl_top, rdev_p->rnic_info.rqt_base,
  867. rdev_p->rnic_info.rqt_top);
  868. PDBG("udbell_len 0x%0x udbell_physbase 0x%lx kdb_addr %p qpshift %lu "
  869. "qpnr %d qpmask 0x%x\n",
  870. rdev_p->rnic_info.udbell_len,
  871. rdev_p->rnic_info.udbell_physbase, rdev_p->rnic_info.kdb_addr,
  872. rdev_p->qpshift, rdev_p->qpnr, rdev_p->qpmask);
  873. err = cxio_hal_init_ctrl_qp(rdev_p);
  874. if (err) {
  875. printk(KERN_ERR "%s error %d initializing ctrl_qp.\n",
  876. __func__, err);
  877. goto err1;
  878. }
  879. err = cxio_hal_init_resource(rdev_p, cxio_num_stags(rdev_p), 0,
  880. 0, T3_MAX_NUM_QP, T3_MAX_NUM_CQ,
  881. T3_MAX_NUM_PD);
  882. if (err) {
  883. printk(KERN_ERR "%s error %d initializing hal resources.\n",
  884. __func__, err);
  885. goto err2;
  886. }
  887. err = cxio_hal_pblpool_create(rdev_p);
  888. if (err) {
  889. printk(KERN_ERR "%s error %d initializing pbl mem pool.\n",
  890. __func__, err);
  891. goto err3;
  892. }
  893. err = cxio_hal_rqtpool_create(rdev_p);
  894. if (err) {
  895. printk(KERN_ERR "%s error %d initializing rqt mem pool.\n",
  896. __func__, err);
  897. goto err4;
  898. }
  899. return 0;
  900. err4:
  901. cxio_hal_pblpool_destroy(rdev_p);
  902. err3:
  903. cxio_hal_destroy_resource(rdev_p->rscp);
  904. err2:
  905. cxio_hal_destroy_ctrl_qp(rdev_p);
  906. err1:
  907. list_del(&rdev_p->entry);
  908. return err;
  909. }
  910. void cxio_rdev_close(struct cxio_rdev *rdev_p)
  911. {
  912. if (rdev_p) {
  913. cxio_hal_pblpool_destroy(rdev_p);
  914. cxio_hal_rqtpool_destroy(rdev_p);
  915. list_del(&rdev_p->entry);
  916. rdev_p->t3cdev_p->ulp = NULL;
  917. cxio_hal_destroy_ctrl_qp(rdev_p);
  918. cxio_hal_destroy_resource(rdev_p->rscp);
  919. }
  920. }
  921. int __init cxio_hal_init(void)
  922. {
  923. if (cxio_hal_init_rhdl_resource(T3_MAX_NUM_RI))
  924. return -ENOMEM;
  925. t3_register_cpl_handler(CPL_ASYNC_NOTIF, cxio_hal_ev_handler);
  926. return 0;
  927. }
  928. void __exit cxio_hal_exit(void)
  929. {
  930. struct cxio_rdev *rdev, *tmp;
  931. t3_register_cpl_handler(CPL_ASYNC_NOTIF, NULL);
  932. list_for_each_entry_safe(rdev, tmp, &rdev_list, entry)
  933. cxio_rdev_close(rdev);
  934. cxio_hal_destroy_rhdl_resource();
  935. }
  936. static void flush_completed_wrs(struct t3_wq *wq, struct t3_cq *cq)
  937. {
  938. struct t3_swsq *sqp;
  939. __u32 ptr = wq->sq_rptr;
  940. int count = Q_COUNT(wq->sq_rptr, wq->sq_wptr);
  941. sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
  942. while (count--)
  943. if (!sqp->signaled) {
  944. ptr++;
  945. sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
  946. } else if (sqp->complete) {
  947. /*
  948. * Insert this completed cqe into the swcq.
  949. */
  950. PDBG("%s moving cqe into swcq sq idx %ld cq idx %ld\n",
  951. __func__, Q_PTR2IDX(ptr, wq->sq_size_log2),
  952. Q_PTR2IDX(cq->sw_wptr, cq->size_log2));
  953. sqp->cqe.header |= htonl(V_CQE_SWCQE(1));
  954. *(cq->sw_queue + Q_PTR2IDX(cq->sw_wptr, cq->size_log2))
  955. = sqp->cqe;
  956. cq->sw_wptr++;
  957. sqp->signaled = 0;
  958. break;
  959. } else
  960. break;
  961. }
  962. static void create_read_req_cqe(struct t3_wq *wq, struct t3_cqe *hw_cqe,
  963. struct t3_cqe *read_cqe)
  964. {
  965. read_cqe->u.scqe.wrid_hi = wq->oldest_read->sq_wptr;
  966. read_cqe->len = wq->oldest_read->read_len;
  967. read_cqe->header = htonl(V_CQE_QPID(CQE_QPID(*hw_cqe)) |
  968. V_CQE_SWCQE(SW_CQE(*hw_cqe)) |
  969. V_CQE_OPCODE(T3_READ_REQ) |
  970. V_CQE_TYPE(1));
  971. }
  972. /*
  973. * Return a ptr to the next read wr in the SWSQ or NULL.
  974. */
  975. static void advance_oldest_read(struct t3_wq *wq)
  976. {
  977. u32 rptr = wq->oldest_read - wq->sq + 1;
  978. u32 wptr = Q_PTR2IDX(wq->sq_wptr, wq->sq_size_log2);
  979. while (Q_PTR2IDX(rptr, wq->sq_size_log2) != wptr) {
  980. wq->oldest_read = wq->sq + Q_PTR2IDX(rptr, wq->sq_size_log2);
  981. if (wq->oldest_read->opcode == T3_READ_REQ)
  982. return;
  983. rptr++;
  984. }
  985. wq->oldest_read = NULL;
  986. }
  987. /*
  988. * cxio_poll_cq
  989. *
  990. * Caller must:
  991. * check the validity of the first CQE,
  992. * supply the wq assicated with the qpid.
  993. *
  994. * credit: cq credit to return to sge.
  995. * cqe_flushed: 1 iff the CQE is flushed.
  996. * cqe: copy of the polled CQE.
  997. *
  998. * return value:
  999. * 0 CQE returned,
  1000. * -1 CQE skipped, try again.
  1001. */
  1002. int cxio_poll_cq(struct t3_wq *wq, struct t3_cq *cq, struct t3_cqe *cqe,
  1003. u8 *cqe_flushed, u64 *cookie, u32 *credit)
  1004. {
  1005. int ret = 0;
  1006. struct t3_cqe *hw_cqe, read_cqe;
  1007. *cqe_flushed = 0;
  1008. *credit = 0;
  1009. hw_cqe = cxio_next_cqe(cq);
  1010. PDBG("%s CQE OOO %d qpid 0x%0x genbit %d type %d status 0x%0x"
  1011. " opcode 0x%0x len 0x%0x wrid_hi_stag 0x%x wrid_low_msn 0x%x\n",
  1012. __func__, CQE_OOO(*hw_cqe), CQE_QPID(*hw_cqe),
  1013. CQE_GENBIT(*hw_cqe), CQE_TYPE(*hw_cqe), CQE_STATUS(*hw_cqe),
  1014. CQE_OPCODE(*hw_cqe), CQE_LEN(*hw_cqe), CQE_WRID_HI(*hw_cqe),
  1015. CQE_WRID_LOW(*hw_cqe));
  1016. /*
  1017. * skip cqe's not affiliated with a QP.
  1018. */
  1019. if (wq == NULL) {
  1020. ret = -1;
  1021. goto skip_cqe;
  1022. }
  1023. /*
  1024. * Gotta tweak READ completions:
  1025. * 1) the cqe doesn't contain the sq_wptr from the wr.
  1026. * 2) opcode not reflected from the wr.
  1027. * 3) read_len not reflected from the wr.
  1028. * 4) cq_type is RQ_TYPE not SQ_TYPE.
  1029. */
  1030. if (RQ_TYPE(*hw_cqe) && (CQE_OPCODE(*hw_cqe) == T3_READ_RESP)) {
  1031. /*
  1032. * If this is an unsolicited read response, then the read
  1033. * was generated by the kernel driver as part of peer-2-peer
  1034. * connection setup. So ignore the completion.
  1035. */
  1036. if (!wq->oldest_read) {
  1037. if (CQE_STATUS(*hw_cqe))
  1038. wq->error = 1;
  1039. ret = -1;
  1040. goto skip_cqe;
  1041. }
  1042. /*
  1043. * Don't write to the HWCQ, so create a new read req CQE
  1044. * in local memory.
  1045. */
  1046. create_read_req_cqe(wq, hw_cqe, &read_cqe);
  1047. hw_cqe = &read_cqe;
  1048. advance_oldest_read(wq);
  1049. }
  1050. /*
  1051. * T3A: Discard TERMINATE CQEs.
  1052. */
  1053. if (CQE_OPCODE(*hw_cqe) == T3_TERMINATE) {
  1054. ret = -1;
  1055. wq->error = 1;
  1056. goto skip_cqe;
  1057. }
  1058. if (CQE_STATUS(*hw_cqe) || wq->error) {
  1059. *cqe_flushed = wq->error;
  1060. wq->error = 1;
  1061. /*
  1062. * T3A inserts errors into the CQE. We cannot return
  1063. * these as work completions.
  1064. */
  1065. /* incoming write failures */
  1066. if ((CQE_OPCODE(*hw_cqe) == T3_RDMA_WRITE)
  1067. && RQ_TYPE(*hw_cqe)) {
  1068. ret = -1;
  1069. goto skip_cqe;
  1070. }
  1071. /* incoming read request failures */
  1072. if ((CQE_OPCODE(*hw_cqe) == T3_READ_RESP) && SQ_TYPE(*hw_cqe)) {
  1073. ret = -1;
  1074. goto skip_cqe;
  1075. }
  1076. /* incoming SEND with no receive posted failures */
  1077. if ((CQE_OPCODE(*hw_cqe) == T3_SEND) && RQ_TYPE(*hw_cqe) &&
  1078. Q_EMPTY(wq->rq_rptr, wq->rq_wptr)) {
  1079. ret = -1;
  1080. goto skip_cqe;
  1081. }
  1082. goto proc_cqe;
  1083. }
  1084. /*
  1085. * RECV completion.
  1086. */
  1087. if (RQ_TYPE(*hw_cqe)) {
  1088. /*
  1089. * HW only validates 4 bits of MSN. So we must validate that
  1090. * the MSN in the SEND is the next expected MSN. If its not,
  1091. * then we complete this with TPT_ERR_MSN and mark the wq in
  1092. * error.
  1093. */
  1094. if (unlikely((CQE_WRID_MSN(*hw_cqe) != (wq->rq_rptr + 1)))) {
  1095. wq->error = 1;
  1096. hw_cqe->header |= htonl(V_CQE_STATUS(TPT_ERR_MSN));
  1097. goto proc_cqe;
  1098. }
  1099. goto proc_cqe;
  1100. }
  1101. /*
  1102. * If we get here its a send completion.
  1103. *
  1104. * Handle out of order completion. These get stuffed
  1105. * in the SW SQ. Then the SW SQ is walked to move any
  1106. * now in-order completions into the SW CQ. This handles
  1107. * 2 cases:
  1108. * 1) reaping unsignaled WRs when the first subsequent
  1109. * signaled WR is completed.
  1110. * 2) out of order read completions.
  1111. */
  1112. if (!SW_CQE(*hw_cqe) && (CQE_WRID_SQ_WPTR(*hw_cqe) != wq->sq_rptr)) {
  1113. struct t3_swsq *sqp;
  1114. PDBG("%s out of order completion going in swsq at idx %ld\n",
  1115. __func__,
  1116. Q_PTR2IDX(CQE_WRID_SQ_WPTR(*hw_cqe), wq->sq_size_log2));
  1117. sqp = wq->sq +
  1118. Q_PTR2IDX(CQE_WRID_SQ_WPTR(*hw_cqe), wq->sq_size_log2);
  1119. sqp->cqe = *hw_cqe;
  1120. sqp->complete = 1;
  1121. ret = -1;
  1122. goto flush_wq;
  1123. }
  1124. proc_cqe:
  1125. *cqe = *hw_cqe;
  1126. /*
  1127. * Reap the associated WR(s) that are freed up with this
  1128. * completion.
  1129. */
  1130. if (SQ_TYPE(*hw_cqe)) {
  1131. wq->sq_rptr = CQE_WRID_SQ_WPTR(*hw_cqe);
  1132. PDBG("%s completing sq idx %ld\n", __func__,
  1133. Q_PTR2IDX(wq->sq_rptr, wq->sq_size_log2));
  1134. *cookie = (wq->sq +
  1135. Q_PTR2IDX(wq->sq_rptr, wq->sq_size_log2))->wr_id;
  1136. wq->sq_rptr++;
  1137. } else {
  1138. PDBG("%s completing rq idx %ld\n", __func__,
  1139. Q_PTR2IDX(wq->rq_rptr, wq->rq_size_log2));
  1140. *cookie = *(wq->rq + Q_PTR2IDX(wq->rq_rptr, wq->rq_size_log2));
  1141. wq->rq_rptr++;
  1142. }
  1143. flush_wq:
  1144. /*
  1145. * Flush any completed cqes that are now in-order.
  1146. */
  1147. flush_completed_wrs(wq, cq);
  1148. skip_cqe:
  1149. if (SW_CQE(*hw_cqe)) {
  1150. PDBG("%s cq %p cqid 0x%x skip sw cqe sw_rptr 0x%x\n",
  1151. __func__, cq, cq->cqid, cq->sw_rptr);
  1152. ++cq->sw_rptr;
  1153. } else {
  1154. PDBG("%s cq %p cqid 0x%x skip hw cqe rptr 0x%x\n",
  1155. __func__, cq, cq->cqid, cq->rptr);
  1156. ++cq->rptr;
  1157. /*
  1158. * T3A: compute credits.
  1159. */
  1160. if (((cq->rptr - cq->wptr) > (1 << (cq->size_log2 - 1)))
  1161. || ((cq->rptr - cq->wptr) >= 128)) {
  1162. *credit = cq->rptr - cq->wptr;
  1163. cq->wptr = cq->rptr;
  1164. }
  1165. }
  1166. return ret;
  1167. }