cxio_hal.c 36 KB

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