cxio_hal.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /*
  2. * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
  3. * Copyright (c) 2006 Open Grid Computing, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <asm/delay.h>
  34. #include <linux/mutex.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/sched.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/pci.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 inline 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 inline struct cxio_rdev *cxio_hal_find_rdev_by_t3cdev(struct t3cdev
  54. *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. }
  105. return 0;
  106. }
  107. static inline 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. 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 inline 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 = 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. return 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. return -ENOMEM;
  462. }
  463. pci_unmap_addr_set(&rdev_p->ctrl_qp, mapping,
  464. rdev_p->ctrl_qp.dma_addr);
  465. rdev_p->ctrl_qp.doorbell = (void __iomem *)rdev_p->rnic_info.kdb_addr;
  466. memset(rdev_p->ctrl_qp.workq, 0,
  467. (1 << T3_CTRL_QP_SIZE_LOG2) * sizeof(union t3_wr));
  468. mutex_init(&rdev_p->ctrl_qp.lock);
  469. init_waitqueue_head(&rdev_p->ctrl_qp.waitq);
  470. /* update HW Ctrl QP context */
  471. base_addr = rdev_p->ctrl_qp.dma_addr;
  472. base_addr >>= 12;
  473. ctx0 = (V_EC_SIZE((1 << T3_CTRL_QP_SIZE_LOG2)) |
  474. V_EC_BASE_LO((u32) base_addr & 0xffff));
  475. ctx0 <<= 32;
  476. ctx0 |= V_EC_CREDITS(FW_WR_NUM);
  477. base_addr >>= 16;
  478. ctx1 = (u32) base_addr;
  479. base_addr >>= 32;
  480. ctx1 |= ((u64) (V_EC_BASE_HI((u32) base_addr & 0xf) | V_EC_RESPQ(0) |
  481. V_EC_TYPE(0) | V_EC_GEN(1) |
  482. V_EC_UP_TOKEN(T3_CTL_QP_TID) | F_EC_VALID)) << 32;
  483. wqe = (struct t3_modify_qp_wr *) skb_put(skb, sizeof(*wqe));
  484. memset(wqe, 0, sizeof(*wqe));
  485. build_fw_riwrh((struct fw_riwrh *) wqe, T3_WR_QP_MOD, 0, 1,
  486. T3_CTL_QP_TID, 7);
  487. wqe->flags = cpu_to_be32(MODQP_WRITE_EC);
  488. sge_cmd = (3ULL << 56) | FW_RI_SGEEC_START << 8 | 3;
  489. wqe->sge_cmd = cpu_to_be64(sge_cmd);
  490. wqe->ctx1 = cpu_to_be64(ctx1);
  491. wqe->ctx0 = cpu_to_be64(ctx0);
  492. PDBG("CtrlQP dma_addr 0x%llx workq %p size %d\n",
  493. (unsigned long long) rdev_p->ctrl_qp.dma_addr,
  494. rdev_p->ctrl_qp.workq, 1 << T3_CTRL_QP_SIZE_LOG2);
  495. skb->priority = CPL_PRIORITY_CONTROL;
  496. return (cxgb3_ofld_send(rdev_p->t3cdev_p, skb));
  497. }
  498. static int cxio_hal_destroy_ctrl_qp(struct cxio_rdev *rdev_p)
  499. {
  500. dma_free_coherent(&(rdev_p->rnic_info.pdev->dev),
  501. (1UL << T3_CTRL_QP_SIZE_LOG2)
  502. * sizeof(union t3_wr), rdev_p->ctrl_qp.workq,
  503. pci_unmap_addr(&rdev_p->ctrl_qp, mapping));
  504. return cxio_hal_clear_qp_ctx(rdev_p, T3_CTRL_QP_ID);
  505. }
  506. /* write len bytes of data into addr (32B aligned address)
  507. * If data is NULL, clear len byte of memory to zero.
  508. * caller aquires the ctrl_qp lock before the call
  509. */
  510. static int cxio_hal_ctrl_qp_write_mem(struct cxio_rdev *rdev_p, u32 addr,
  511. u32 len, void *data, int completion)
  512. {
  513. u32 i, nr_wqe, copy_len;
  514. u8 *copy_data;
  515. u8 wr_len, utx_len; /* lenght in 8 byte flit */
  516. enum t3_wr_flags flag;
  517. __be64 *wqe;
  518. u64 utx_cmd;
  519. addr &= 0x7FFFFFF;
  520. nr_wqe = len % 96 ? len / 96 + 1 : len / 96; /* 96B max per WQE */
  521. PDBG("%s wptr 0x%x rptr 0x%x len %d, nr_wqe %d data %p addr 0x%0x\n",
  522. __FUNCTION__, rdev_p->ctrl_qp.wptr, rdev_p->ctrl_qp.rptr, len,
  523. nr_wqe, data, addr);
  524. utx_len = 3; /* in 32B unit */
  525. for (i = 0; i < nr_wqe; i++) {
  526. if (Q_FULL(rdev_p->ctrl_qp.rptr, rdev_p->ctrl_qp.wptr,
  527. T3_CTRL_QP_SIZE_LOG2)) {
  528. PDBG("%s ctrl_qp full wtpr 0x%0x rptr 0x%0x, "
  529. "wait for more space i %d\n", __FUNCTION__,
  530. rdev_p->ctrl_qp.wptr, rdev_p->ctrl_qp.rptr, i);
  531. if (wait_event_interruptible(rdev_p->ctrl_qp.waitq,
  532. !Q_FULL(rdev_p->ctrl_qp.rptr,
  533. rdev_p->ctrl_qp.wptr,
  534. T3_CTRL_QP_SIZE_LOG2))) {
  535. PDBG("%s ctrl_qp workq interrupted\n",
  536. __FUNCTION__);
  537. return -ERESTARTSYS;
  538. }
  539. PDBG("%s ctrl_qp wakeup, continue posting work request "
  540. "i %d\n", __FUNCTION__, i);
  541. }
  542. wqe = (__be64 *)(rdev_p->ctrl_qp.workq + (rdev_p->ctrl_qp.wptr %
  543. (1 << T3_CTRL_QP_SIZE_LOG2)));
  544. flag = 0;
  545. if (i == (nr_wqe - 1)) {
  546. /* last WQE */
  547. flag = completion ? T3_COMPLETION_FLAG : 0;
  548. if (len % 32)
  549. utx_len = len / 32 + 1;
  550. else
  551. utx_len = len / 32;
  552. }
  553. /*
  554. * Force a CQE to return the credit to the workq in case
  555. * we posted more than half the max QP size of WRs
  556. */
  557. if ((i != 0) &&
  558. (i % (((1 << T3_CTRL_QP_SIZE_LOG2)) >> 1) == 0)) {
  559. flag = T3_COMPLETION_FLAG;
  560. PDBG("%s force completion at i %d\n", __FUNCTION__, i);
  561. }
  562. /* build the utx mem command */
  563. wqe += (sizeof(struct t3_bypass_wr) >> 3);
  564. utx_cmd = (T3_UTX_MEM_WRITE << 28) | (addr + i * 3);
  565. utx_cmd <<= 32;
  566. utx_cmd |= (utx_len << 28) | ((utx_len << 2) + 1);
  567. *wqe = cpu_to_be64(utx_cmd);
  568. wqe++;
  569. copy_data = (u8 *) data + i * 96;
  570. copy_len = len > 96 ? 96 : len;
  571. /* clear memory content if data is NULL */
  572. if (data)
  573. memcpy(wqe, copy_data, copy_len);
  574. else
  575. memset(wqe, 0, copy_len);
  576. if (copy_len % 32)
  577. memset(((u8 *) wqe) + copy_len, 0,
  578. 32 - (copy_len % 32));
  579. wr_len = ((sizeof(struct t3_bypass_wr)) >> 3) + 1 +
  580. (utx_len << 2);
  581. wqe = (__be64 *)(rdev_p->ctrl_qp.workq + (rdev_p->ctrl_qp.wptr %
  582. (1 << T3_CTRL_QP_SIZE_LOG2)));
  583. /* wptr in the WRID[31:0] */
  584. ((union t3_wrid *)(wqe+1))->id0.low = rdev_p->ctrl_qp.wptr;
  585. /*
  586. * This must be the last write with a memory barrier
  587. * for the genbit
  588. */
  589. build_fw_riwrh((struct fw_riwrh *) wqe, T3_WR_BP, flag,
  590. Q_GENBIT(rdev_p->ctrl_qp.wptr,
  591. T3_CTRL_QP_SIZE_LOG2), T3_CTRL_QP_ID,
  592. wr_len);
  593. if (flag == T3_COMPLETION_FLAG)
  594. ring_doorbell(rdev_p->ctrl_qp.doorbell, T3_CTRL_QP_ID);
  595. len -= 96;
  596. rdev_p->ctrl_qp.wptr++;
  597. }
  598. return 0;
  599. }
  600. /* IN: stag key, pdid, perm, zbva, to, len, page_size, pbl, and pbl_size
  601. * OUT: stag index, actual pbl_size, pbl_addr allocated.
  602. * TBD: shared memory region support
  603. */
  604. static int __cxio_tpt_op(struct cxio_rdev *rdev_p, u32 reset_tpt_entry,
  605. u32 *stag, u8 stag_state, u32 pdid,
  606. enum tpt_mem_type type, enum tpt_mem_perm perm,
  607. u32 zbva, u64 to, u32 len, u8 page_size, __be64 *pbl,
  608. u32 *pbl_size, u32 *pbl_addr)
  609. {
  610. int err;
  611. struct tpt_entry tpt;
  612. u32 stag_idx;
  613. u32 wptr;
  614. int rereg = (*stag != T3_STAG_UNSET);
  615. stag_state = stag_state > 0;
  616. stag_idx = (*stag) >> 8;
  617. if ((!reset_tpt_entry) && !(*stag != T3_STAG_UNSET)) {
  618. stag_idx = cxio_hal_get_stag(rdev_p->rscp);
  619. if (!stag_idx)
  620. return -ENOMEM;
  621. *stag = (stag_idx << 8) | ((*stag) & 0xFF);
  622. }
  623. PDBG("%s stag_state 0x%0x type 0x%0x pdid 0x%0x, stag_idx 0x%x\n",
  624. __FUNCTION__, stag_state, type, pdid, stag_idx);
  625. if (reset_tpt_entry)
  626. cxio_hal_pblpool_free(rdev_p, *pbl_addr, *pbl_size << 3);
  627. else if (!rereg) {
  628. *pbl_addr = cxio_hal_pblpool_alloc(rdev_p, *pbl_size << 3);
  629. if (!*pbl_addr) {
  630. return -ENOMEM;
  631. }
  632. }
  633. mutex_lock(&rdev_p->ctrl_qp.lock);
  634. /* write PBL first if any - update pbl only if pbl list exist */
  635. if (pbl) {
  636. PDBG("%s *pdb_addr 0x%x, pbl_base 0x%x, pbl_size %d\n",
  637. __FUNCTION__, *pbl_addr, rdev_p->rnic_info.pbl_base,
  638. *pbl_size);
  639. err = cxio_hal_ctrl_qp_write_mem(rdev_p,
  640. (*pbl_addr >> 5),
  641. (*pbl_size << 3), pbl, 0);
  642. if (err)
  643. goto ret;
  644. }
  645. /* write TPT entry */
  646. if (reset_tpt_entry)
  647. memset(&tpt, 0, sizeof(tpt));
  648. else {
  649. tpt.valid_stag_pdid = cpu_to_be32(F_TPT_VALID |
  650. V_TPT_STAG_KEY((*stag) & M_TPT_STAG_KEY) |
  651. V_TPT_STAG_STATE(stag_state) |
  652. V_TPT_STAG_TYPE(type) | V_TPT_PDID(pdid));
  653. BUG_ON(page_size >= 28);
  654. tpt.flags_pagesize_qpid = cpu_to_be32(V_TPT_PERM(perm) |
  655. F_TPT_MW_BIND_ENABLE |
  656. V_TPT_ADDR_TYPE((zbva ? TPT_ZBTO : TPT_VATO)) |
  657. V_TPT_PAGE_SIZE(page_size));
  658. tpt.rsvd_pbl_addr = reset_tpt_entry ? 0 :
  659. cpu_to_be32(V_TPT_PBL_ADDR(PBL_OFF(rdev_p, *pbl_addr)>>3));
  660. tpt.len = cpu_to_be32(len);
  661. tpt.va_hi = cpu_to_be32((u32) (to >> 32));
  662. tpt.va_low_or_fbo = cpu_to_be32((u32) (to & 0xFFFFFFFFULL));
  663. tpt.rsvd_bind_cnt_or_pstag = 0;
  664. tpt.rsvd_pbl_size = reset_tpt_entry ? 0 :
  665. cpu_to_be32(V_TPT_PBL_SIZE((*pbl_size) >> 2));
  666. }
  667. err = cxio_hal_ctrl_qp_write_mem(rdev_p,
  668. stag_idx +
  669. (rdev_p->rnic_info.tpt_base >> 5),
  670. sizeof(tpt), &tpt, 1);
  671. /* release the stag index to free pool */
  672. if (reset_tpt_entry)
  673. cxio_hal_put_stag(rdev_p->rscp, stag_idx);
  674. ret:
  675. wptr = rdev_p->ctrl_qp.wptr;
  676. mutex_unlock(&rdev_p->ctrl_qp.lock);
  677. if (!err)
  678. if (wait_event_interruptible(rdev_p->ctrl_qp.waitq,
  679. SEQ32_GE(rdev_p->ctrl_qp.rptr,
  680. wptr)))
  681. return -ERESTARTSYS;
  682. return err;
  683. }
  684. /* IN : stag key, pdid, pbl_size
  685. * Out: stag index, actaul pbl_size, and pbl_addr allocated.
  686. */
  687. int cxio_allocate_stag(struct cxio_rdev *rdev_p, u32 * stag, u32 pdid,
  688. enum tpt_mem_perm perm, u32 * pbl_size, u32 * pbl_addr)
  689. {
  690. *stag = T3_STAG_UNSET;
  691. return (__cxio_tpt_op(rdev_p, 0, stag, 0, pdid, TPT_NON_SHARED_MR,
  692. perm, 0, 0ULL, 0, 0, NULL, pbl_size, pbl_addr));
  693. }
  694. int cxio_register_phys_mem(struct cxio_rdev *rdev_p, u32 *stag, u32 pdid,
  695. enum tpt_mem_perm perm, u32 zbva, u64 to, u32 len,
  696. u8 page_size, __be64 *pbl, u32 *pbl_size,
  697. u32 *pbl_addr)
  698. {
  699. *stag = T3_STAG_UNSET;
  700. return __cxio_tpt_op(rdev_p, 0, stag, 1, pdid, TPT_NON_SHARED_MR, perm,
  701. zbva, to, len, page_size, pbl, pbl_size, pbl_addr);
  702. }
  703. int cxio_reregister_phys_mem(struct cxio_rdev *rdev_p, u32 *stag, u32 pdid,
  704. enum tpt_mem_perm perm, u32 zbva, u64 to, u32 len,
  705. u8 page_size, __be64 *pbl, u32 *pbl_size,
  706. u32 *pbl_addr)
  707. {
  708. return __cxio_tpt_op(rdev_p, 0, stag, 1, pdid, TPT_NON_SHARED_MR, perm,
  709. zbva, to, len, page_size, pbl, pbl_size, pbl_addr);
  710. }
  711. int cxio_dereg_mem(struct cxio_rdev *rdev_p, u32 stag, u32 pbl_size,
  712. u32 pbl_addr)
  713. {
  714. return __cxio_tpt_op(rdev_p, 1, &stag, 0, 0, 0, 0, 0, 0ULL, 0, 0, NULL,
  715. &pbl_size, &pbl_addr);
  716. }
  717. int cxio_allocate_window(struct cxio_rdev *rdev_p, u32 * stag, u32 pdid)
  718. {
  719. u32 pbl_size = 0;
  720. *stag = T3_STAG_UNSET;
  721. return __cxio_tpt_op(rdev_p, 0, stag, 0, pdid, TPT_MW, 0, 0, 0ULL, 0, 0,
  722. NULL, &pbl_size, NULL);
  723. }
  724. int cxio_deallocate_window(struct cxio_rdev *rdev_p, u32 stag)
  725. {
  726. return __cxio_tpt_op(rdev_p, 1, &stag, 0, 0, 0, 0, 0, 0ULL, 0, 0, NULL,
  727. NULL, NULL);
  728. }
  729. int cxio_rdma_init(struct cxio_rdev *rdev_p, struct t3_rdma_init_attr *attr)
  730. {
  731. struct t3_rdma_init_wr *wqe;
  732. struct sk_buff *skb = alloc_skb(sizeof(*wqe), GFP_ATOMIC);
  733. if (!skb)
  734. return -ENOMEM;
  735. PDBG("%s rdev_p %p\n", __FUNCTION__, rdev_p);
  736. wqe = (struct t3_rdma_init_wr *) __skb_put(skb, sizeof(*wqe));
  737. wqe->wrh.op_seop_flags = cpu_to_be32(V_FW_RIWR_OP(T3_WR_INIT));
  738. wqe->wrh.gen_tid_len = cpu_to_be32(V_FW_RIWR_TID(attr->tid) |
  739. V_FW_RIWR_LEN(sizeof(*wqe) >> 3));
  740. wqe->wrid.id1 = 0;
  741. wqe->qpid = cpu_to_be32(attr->qpid);
  742. wqe->pdid = cpu_to_be32(attr->pdid);
  743. wqe->scqid = cpu_to_be32(attr->scqid);
  744. wqe->rcqid = cpu_to_be32(attr->rcqid);
  745. wqe->rq_addr = cpu_to_be32(attr->rq_addr - rdev_p->rnic_info.rqt_base);
  746. wqe->rq_size = cpu_to_be32(attr->rq_size);
  747. wqe->mpaattrs = attr->mpaattrs;
  748. wqe->qpcaps = attr->qpcaps;
  749. wqe->ulpdu_size = cpu_to_be16(attr->tcp_emss);
  750. wqe->flags = cpu_to_be32(attr->flags);
  751. wqe->ord = cpu_to_be32(attr->ord);
  752. wqe->ird = cpu_to_be32(attr->ird);
  753. wqe->qp_dma_addr = cpu_to_be64(attr->qp_dma_addr);
  754. wqe->qp_dma_size = cpu_to_be32(attr->qp_dma_size);
  755. wqe->rsvd = 0;
  756. skb->priority = 0; /* 0=>ToeQ; 1=>CtrlQ */
  757. return (cxgb3_ofld_send(rdev_p->t3cdev_p, skb));
  758. }
  759. void cxio_register_ev_cb(cxio_hal_ev_callback_func_t ev_cb)
  760. {
  761. cxio_ev_cb = ev_cb;
  762. }
  763. void cxio_unregister_ev_cb(cxio_hal_ev_callback_func_t ev_cb)
  764. {
  765. cxio_ev_cb = NULL;
  766. }
  767. static int cxio_hal_ev_handler(struct t3cdev *t3cdev_p, struct sk_buff *skb)
  768. {
  769. static int cnt;
  770. struct cxio_rdev *rdev_p = NULL;
  771. struct respQ_msg_t *rsp_msg = (struct respQ_msg_t *) skb->data;
  772. PDBG("%d: %s cq_id 0x%x cq_ptr 0x%x genbit %0x overflow %0x an %0x"
  773. " se %0x notify %0x cqbranch %0x creditth %0x\n",
  774. cnt, __FUNCTION__, RSPQ_CQID(rsp_msg), RSPQ_CQPTR(rsp_msg),
  775. RSPQ_GENBIT(rsp_msg), RSPQ_OVERFLOW(rsp_msg), RSPQ_AN(rsp_msg),
  776. RSPQ_SE(rsp_msg), RSPQ_NOTIFY(rsp_msg), RSPQ_CQBRANCH(rsp_msg),
  777. RSPQ_CREDIT_THRESH(rsp_msg));
  778. PDBG("CQE: QPID 0x%0x genbit %0x type 0x%0x status 0x%0x opcode %d "
  779. "len 0x%0x wrid_hi_stag 0x%x wrid_low_msn 0x%x\n",
  780. CQE_QPID(rsp_msg->cqe), CQE_GENBIT(rsp_msg->cqe),
  781. CQE_TYPE(rsp_msg->cqe), CQE_STATUS(rsp_msg->cqe),
  782. CQE_OPCODE(rsp_msg->cqe), CQE_LEN(rsp_msg->cqe),
  783. CQE_WRID_HI(rsp_msg->cqe), CQE_WRID_LOW(rsp_msg->cqe));
  784. rdev_p = (struct cxio_rdev *)t3cdev_p->ulp;
  785. if (!rdev_p) {
  786. PDBG("%s called by t3cdev %p with null ulp\n", __FUNCTION__,
  787. t3cdev_p);
  788. return 0;
  789. }
  790. if (CQE_QPID(rsp_msg->cqe) == T3_CTRL_QP_ID) {
  791. rdev_p->ctrl_qp.rptr = CQE_WRID_LOW(rsp_msg->cqe) + 1;
  792. wake_up_interruptible(&rdev_p->ctrl_qp.waitq);
  793. dev_kfree_skb_irq(skb);
  794. } else if (CQE_QPID(rsp_msg->cqe) == 0xfff8)
  795. dev_kfree_skb_irq(skb);
  796. else if (cxio_ev_cb)
  797. (*cxio_ev_cb) (rdev_p, skb);
  798. else
  799. dev_kfree_skb_irq(skb);
  800. cnt++;
  801. return 0;
  802. }
  803. /* Caller takes care of locking if needed */
  804. int cxio_rdev_open(struct cxio_rdev *rdev_p)
  805. {
  806. struct net_device *netdev_p = NULL;
  807. int err = 0;
  808. if (strlen(rdev_p->dev_name)) {
  809. if (cxio_hal_find_rdev_by_name(rdev_p->dev_name)) {
  810. return -EBUSY;
  811. }
  812. netdev_p = dev_get_by_name(rdev_p->dev_name);
  813. if (!netdev_p) {
  814. return -EINVAL;
  815. }
  816. dev_put(netdev_p);
  817. } else if (rdev_p->t3cdev_p) {
  818. if (cxio_hal_find_rdev_by_t3cdev(rdev_p->t3cdev_p)) {
  819. return -EBUSY;
  820. }
  821. netdev_p = rdev_p->t3cdev_p->lldev;
  822. strncpy(rdev_p->dev_name, rdev_p->t3cdev_p->name,
  823. T3_MAX_DEV_NAME_LEN);
  824. } else {
  825. PDBG("%s t3cdev_p or dev_name must be set\n", __FUNCTION__);
  826. return -EINVAL;
  827. }
  828. list_add_tail(&rdev_p->entry, &rdev_list);
  829. PDBG("%s opening rnic dev %s\n", __FUNCTION__, rdev_p->dev_name);
  830. memset(&rdev_p->ctrl_qp, 0, sizeof(rdev_p->ctrl_qp));
  831. if (!rdev_p->t3cdev_p)
  832. rdev_p->t3cdev_p = T3CDEV(netdev_p);
  833. rdev_p->t3cdev_p->ulp = (void *) rdev_p;
  834. err = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_GET_PARAMS,
  835. &(rdev_p->rnic_info));
  836. if (err) {
  837. printk(KERN_ERR "%s t3cdev_p(%p)->ctl returned error %d.\n",
  838. __FUNCTION__, rdev_p->t3cdev_p, err);
  839. goto err1;
  840. }
  841. err = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, GET_PORTS,
  842. &(rdev_p->port_info));
  843. if (err) {
  844. printk(KERN_ERR "%s t3cdev_p(%p)->ctl returned error %d.\n",
  845. __FUNCTION__, rdev_p->t3cdev_p, err);
  846. goto err1;
  847. }
  848. /*
  849. * qpshift is the number of bits to shift the qpid left in order
  850. * to get the correct address of the doorbell for that qp.
  851. */
  852. cxio_init_ucontext(rdev_p, &rdev_p->uctx);
  853. rdev_p->qpshift = PAGE_SHIFT -
  854. ilog2(65536 >>
  855. ilog2(rdev_p->rnic_info.udbell_len >>
  856. PAGE_SHIFT));
  857. rdev_p->qpnr = rdev_p->rnic_info.udbell_len >> PAGE_SHIFT;
  858. rdev_p->qpmask = (65536 >> ilog2(rdev_p->qpnr)) - 1;
  859. PDBG("%s rnic %s info: tpt_base 0x%0x tpt_top 0x%0x num stags %d "
  860. "pbl_base 0x%0x pbl_top 0x%0x rqt_base 0x%0x, rqt_top 0x%0x\n",
  861. __FUNCTION__, rdev_p->dev_name, rdev_p->rnic_info.tpt_base,
  862. rdev_p->rnic_info.tpt_top, cxio_num_stags(rdev_p),
  863. rdev_p->rnic_info.pbl_base,
  864. rdev_p->rnic_info.pbl_top, rdev_p->rnic_info.rqt_base,
  865. rdev_p->rnic_info.rqt_top);
  866. PDBG("udbell_len 0x%0x udbell_physbase 0x%lx kdb_addr %p qpshift %lu "
  867. "qpnr %d qpmask 0x%x\n",
  868. rdev_p->rnic_info.udbell_len,
  869. rdev_p->rnic_info.udbell_physbase, rdev_p->rnic_info.kdb_addr,
  870. rdev_p->qpshift, rdev_p->qpnr, rdev_p->qpmask);
  871. err = cxio_hal_init_ctrl_qp(rdev_p);
  872. if (err) {
  873. printk(KERN_ERR "%s error %d initializing ctrl_qp.\n",
  874. __FUNCTION__, err);
  875. goto err1;
  876. }
  877. err = cxio_hal_init_resource(rdev_p, cxio_num_stags(rdev_p), 0,
  878. 0, T3_MAX_NUM_QP, T3_MAX_NUM_CQ,
  879. T3_MAX_NUM_PD);
  880. if (err) {
  881. printk(KERN_ERR "%s error %d initializing hal resources.\n",
  882. __FUNCTION__, err);
  883. goto err2;
  884. }
  885. err = cxio_hal_pblpool_create(rdev_p);
  886. if (err) {
  887. printk(KERN_ERR "%s error %d initializing pbl mem pool.\n",
  888. __FUNCTION__, err);
  889. goto err3;
  890. }
  891. err = cxio_hal_rqtpool_create(rdev_p);
  892. if (err) {
  893. printk(KERN_ERR "%s error %d initializing rqt mem pool.\n",
  894. __FUNCTION__, err);
  895. goto err4;
  896. }
  897. return 0;
  898. err4:
  899. cxio_hal_pblpool_destroy(rdev_p);
  900. err3:
  901. cxio_hal_destroy_resource(rdev_p->rscp);
  902. err2:
  903. cxio_hal_destroy_ctrl_qp(rdev_p);
  904. err1:
  905. list_del(&rdev_p->entry);
  906. return err;
  907. }
  908. void cxio_rdev_close(struct cxio_rdev *rdev_p)
  909. {
  910. if (rdev_p) {
  911. cxio_hal_pblpool_destroy(rdev_p);
  912. cxio_hal_rqtpool_destroy(rdev_p);
  913. list_del(&rdev_p->entry);
  914. rdev_p->t3cdev_p->ulp = NULL;
  915. cxio_hal_destroy_ctrl_qp(rdev_p);
  916. cxio_hal_destroy_resource(rdev_p->rscp);
  917. }
  918. }
  919. int __init cxio_hal_init(void)
  920. {
  921. if (cxio_hal_init_rhdl_resource(T3_MAX_NUM_RI))
  922. return -ENOMEM;
  923. t3_register_cpl_handler(CPL_ASYNC_NOTIF, cxio_hal_ev_handler);
  924. return 0;
  925. }
  926. void __exit cxio_hal_exit(void)
  927. {
  928. struct cxio_rdev *rdev, *tmp;
  929. t3_register_cpl_handler(CPL_ASYNC_NOTIF, NULL);
  930. list_for_each_entry_safe(rdev, tmp, &rdev_list, entry)
  931. cxio_rdev_close(rdev);
  932. cxio_hal_destroy_rhdl_resource();
  933. }
  934. static inline void flush_completed_wrs(struct t3_wq *wq, struct t3_cq *cq)
  935. {
  936. struct t3_swsq *sqp;
  937. __u32 ptr = wq->sq_rptr;
  938. int count = Q_COUNT(wq->sq_rptr, wq->sq_wptr);
  939. sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
  940. while (count--)
  941. if (!sqp->signaled) {
  942. ptr++;
  943. sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
  944. } else if (sqp->complete) {
  945. /*
  946. * Insert this completed cqe into the swcq.
  947. */
  948. PDBG("%s moving cqe into swcq sq idx %ld cq idx %ld\n",
  949. __FUNCTION__, Q_PTR2IDX(ptr, wq->sq_size_log2),
  950. Q_PTR2IDX(cq->sw_wptr, cq->size_log2));
  951. sqp->cqe.header |= htonl(V_CQE_SWCQE(1));
  952. *(cq->sw_queue + Q_PTR2IDX(cq->sw_wptr, cq->size_log2))
  953. = sqp->cqe;
  954. cq->sw_wptr++;
  955. sqp->signaled = 0;
  956. break;
  957. } else
  958. break;
  959. }
  960. static inline void create_read_req_cqe(struct t3_wq *wq,
  961. struct t3_cqe *hw_cqe,
  962. struct t3_cqe *read_cqe)
  963. {
  964. read_cqe->u.scqe.wrid_hi = wq->oldest_read->sq_wptr;
  965. read_cqe->len = wq->oldest_read->read_len;
  966. read_cqe->header = htonl(V_CQE_QPID(CQE_QPID(*hw_cqe)) |
  967. V_CQE_SWCQE(SW_CQE(*hw_cqe)) |
  968. V_CQE_OPCODE(T3_READ_REQ) |
  969. V_CQE_TYPE(1));
  970. }
  971. /*
  972. * Return a ptr to the next read wr in the SWSQ or NULL.
  973. */
  974. static inline void advance_oldest_read(struct t3_wq *wq)
  975. {
  976. u32 rptr = wq->oldest_read - wq->sq + 1;
  977. u32 wptr = Q_PTR2IDX(wq->sq_wptr, wq->sq_size_log2);
  978. while (Q_PTR2IDX(rptr, wq->sq_size_log2) != wptr) {
  979. wq->oldest_read = wq->sq + Q_PTR2IDX(rptr, wq->sq_size_log2);
  980. if (wq->oldest_read->opcode == T3_READ_REQ)
  981. return;
  982. rptr++;
  983. }
  984. wq->oldest_read = NULL;
  985. }
  986. /*
  987. * cxio_poll_cq
  988. *
  989. * Caller must:
  990. * check the validity of the first CQE,
  991. * supply the wq assicated with the qpid.
  992. *
  993. * credit: cq credit to return to sge.
  994. * cqe_flushed: 1 iff the CQE is flushed.
  995. * cqe: copy of the polled CQE.
  996. *
  997. * return value:
  998. * 0 CQE returned,
  999. * -1 CQE skipped, try again.
  1000. */
  1001. int cxio_poll_cq(struct t3_wq *wq, struct t3_cq *cq, struct t3_cqe *cqe,
  1002. u8 *cqe_flushed, u64 *cookie, u32 *credit)
  1003. {
  1004. int ret = 0;
  1005. struct t3_cqe *hw_cqe, read_cqe;
  1006. *cqe_flushed = 0;
  1007. *credit = 0;
  1008. hw_cqe = cxio_next_cqe(cq);
  1009. PDBG("%s CQE OOO %d qpid 0x%0x genbit %d type %d status 0x%0x"
  1010. " opcode 0x%0x len 0x%0x wrid_hi_stag 0x%x wrid_low_msn 0x%x\n",
  1011. __FUNCTION__, CQE_OOO(*hw_cqe), CQE_QPID(*hw_cqe),
  1012. CQE_GENBIT(*hw_cqe), CQE_TYPE(*hw_cqe), CQE_STATUS(*hw_cqe),
  1013. CQE_OPCODE(*hw_cqe), CQE_LEN(*hw_cqe), CQE_WRID_HI(*hw_cqe),
  1014. CQE_WRID_LOW(*hw_cqe));
  1015. /*
  1016. * skip cqe's not affiliated with a QP.
  1017. */
  1018. if (wq == NULL) {
  1019. ret = -1;
  1020. goto skip_cqe;
  1021. }
  1022. /*
  1023. * Gotta tweak READ completions:
  1024. * 1) the cqe doesn't contain the sq_wptr from the wr.
  1025. * 2) opcode not reflected from the wr.
  1026. * 3) read_len not reflected from the wr.
  1027. * 4) cq_type is RQ_TYPE not SQ_TYPE.
  1028. */
  1029. if (RQ_TYPE(*hw_cqe) && (CQE_OPCODE(*hw_cqe) == T3_READ_RESP)) {
  1030. /*
  1031. * Don't write to the HWCQ, so create a new read req CQE
  1032. * in local memory.
  1033. */
  1034. create_read_req_cqe(wq, hw_cqe, &read_cqe);
  1035. hw_cqe = &read_cqe;
  1036. advance_oldest_read(wq);
  1037. }
  1038. /*
  1039. * T3A: Discard TERMINATE CQEs.
  1040. */
  1041. if (CQE_OPCODE(*hw_cqe) == T3_TERMINATE) {
  1042. ret = -1;
  1043. wq->error = 1;
  1044. goto skip_cqe;
  1045. }
  1046. if (CQE_STATUS(*hw_cqe) || wq->error) {
  1047. *cqe_flushed = wq->error;
  1048. wq->error = 1;
  1049. /*
  1050. * T3A inserts errors into the CQE. We cannot return
  1051. * these as work completions.
  1052. */
  1053. /* incoming write failures */
  1054. if ((CQE_OPCODE(*hw_cqe) == T3_RDMA_WRITE)
  1055. && RQ_TYPE(*hw_cqe)) {
  1056. ret = -1;
  1057. goto skip_cqe;
  1058. }
  1059. /* incoming read request failures */
  1060. if ((CQE_OPCODE(*hw_cqe) == T3_READ_RESP) && SQ_TYPE(*hw_cqe)) {
  1061. ret = -1;
  1062. goto skip_cqe;
  1063. }
  1064. /* incoming SEND with no receive posted failures */
  1065. if ((CQE_OPCODE(*hw_cqe) == T3_SEND) && RQ_TYPE(*hw_cqe) &&
  1066. Q_EMPTY(wq->rq_rptr, wq->rq_wptr)) {
  1067. ret = -1;
  1068. goto skip_cqe;
  1069. }
  1070. goto proc_cqe;
  1071. }
  1072. /*
  1073. * RECV completion.
  1074. */
  1075. if (RQ_TYPE(*hw_cqe)) {
  1076. /*
  1077. * HW only validates 4 bits of MSN. So we must validate that
  1078. * the MSN in the SEND is the next expected MSN. If its not,
  1079. * then we complete this with TPT_ERR_MSN and mark the wq in
  1080. * error.
  1081. */
  1082. if (unlikely((CQE_WRID_MSN(*hw_cqe) != (wq->rq_rptr + 1)))) {
  1083. wq->error = 1;
  1084. hw_cqe->header |= htonl(V_CQE_STATUS(TPT_ERR_MSN));
  1085. goto proc_cqe;
  1086. }
  1087. goto proc_cqe;
  1088. }
  1089. /*
  1090. * If we get here its a send completion.
  1091. *
  1092. * Handle out of order completion. These get stuffed
  1093. * in the SW SQ. Then the SW SQ is walked to move any
  1094. * now in-order completions into the SW CQ. This handles
  1095. * 2 cases:
  1096. * 1) reaping unsignaled WRs when the first subsequent
  1097. * signaled WR is completed.
  1098. * 2) out of order read completions.
  1099. */
  1100. if (!SW_CQE(*hw_cqe) && (CQE_WRID_SQ_WPTR(*hw_cqe) != wq->sq_rptr)) {
  1101. struct t3_swsq *sqp;
  1102. PDBG("%s out of order completion going in swsq at idx %ld\n",
  1103. __FUNCTION__,
  1104. Q_PTR2IDX(CQE_WRID_SQ_WPTR(*hw_cqe), wq->sq_size_log2));
  1105. sqp = wq->sq +
  1106. Q_PTR2IDX(CQE_WRID_SQ_WPTR(*hw_cqe), wq->sq_size_log2);
  1107. sqp->cqe = *hw_cqe;
  1108. sqp->complete = 1;
  1109. ret = -1;
  1110. goto flush_wq;
  1111. }
  1112. proc_cqe:
  1113. *cqe = *hw_cqe;
  1114. /*
  1115. * Reap the associated WR(s) that are freed up with this
  1116. * completion.
  1117. */
  1118. if (SQ_TYPE(*hw_cqe)) {
  1119. wq->sq_rptr = CQE_WRID_SQ_WPTR(*hw_cqe);
  1120. PDBG("%s completing sq idx %ld\n", __FUNCTION__,
  1121. Q_PTR2IDX(wq->sq_rptr, wq->sq_size_log2));
  1122. *cookie = (wq->sq +
  1123. Q_PTR2IDX(wq->sq_rptr, wq->sq_size_log2))->wr_id;
  1124. wq->sq_rptr++;
  1125. } else {
  1126. PDBG("%s completing rq idx %ld\n", __FUNCTION__,
  1127. Q_PTR2IDX(wq->rq_rptr, wq->rq_size_log2));
  1128. *cookie = *(wq->rq + Q_PTR2IDX(wq->rq_rptr, wq->rq_size_log2));
  1129. wq->rq_rptr++;
  1130. }
  1131. flush_wq:
  1132. /*
  1133. * Flush any completed cqes that are now in-order.
  1134. */
  1135. flush_completed_wrs(wq, cq);
  1136. skip_cqe:
  1137. if (SW_CQE(*hw_cqe)) {
  1138. PDBG("%s cq %p cqid 0x%x skip sw cqe sw_rptr 0x%x\n",
  1139. __FUNCTION__, cq, cq->cqid, cq->sw_rptr);
  1140. ++cq->sw_rptr;
  1141. } else {
  1142. PDBG("%s cq %p cqid 0x%x skip hw cqe rptr 0x%x\n",
  1143. __FUNCTION__, cq, cq->cqid, cq->rptr);
  1144. ++cq->rptr;
  1145. /*
  1146. * T3A: compute credits.
  1147. */
  1148. if (((cq->rptr - cq->wptr) > (1 << (cq->size_log2 - 1)))
  1149. || ((cq->rptr - cq->wptr) >= 128)) {
  1150. *credit = cq->rptr - cq->wptr;
  1151. cq->wptr = cq->rptr;
  1152. }
  1153. }
  1154. return ret;
  1155. }