cxio_hal.c 36 KB

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