cxio_hal.c 37 KB

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