cxio_hal.c 36 KB

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