cxio_hal.c 37 KB

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