blob: 340e4181c76108a86d8277d0c0e5c33a951e2e09 [file] [log] [blame]
Steve Wiseb038ced2007-02-12 16:16:18 -08001/*
2 * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
Steve Wiseb038ced2007-02-12 16:16:18 -08003 *
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
34#include <linux/mutex.h>
35#include <linux/netdevice.h>
36#include <linux/sched.h>
37#include <linux/spinlock.h>
38#include <linux/pci.h>
David Millerc3bb1092007-03-05 15:21:29 -080039#include <linux/dma-mapping.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070040#include <net/net_namespace.h>
Steve Wiseb038ced2007-02-12 16:16:18 -080041
42#include "cxio_resource.h"
43#include "cxio_hal.h"
44#include "cxgb3_offload.h"
45#include "sge_defs.h"
46
47static LIST_HEAD(rdev_list);
48static cxio_hal_ev_callback_func_t cxio_ev_cb = NULL;
49
Adrian Bunk2b540352007-02-21 11:52:49 +010050static struct cxio_rdev *cxio_hal_find_rdev_by_name(char *dev_name)
Steve Wiseb038ced2007-02-12 16:16:18 -080051{
52 struct cxio_rdev *rdev;
53
54 list_for_each_entry(rdev, &rdev_list, entry)
55 if (!strcmp(rdev->dev_name, dev_name))
56 return rdev;
57 return NULL;
58}
59
Adrian Bunk2b540352007-02-21 11:52:49 +010060static struct cxio_rdev *cxio_hal_find_rdev_by_t3cdev(struct t3cdev *tdev)
Steve Wiseb038ced2007-02-12 16:16:18 -080061{
62 struct cxio_rdev *rdev;
63
64 list_for_each_entry(rdev, &rdev_list, entry)
65 if (rdev->t3cdev_p == tdev)
66 return rdev;
67 return NULL;
68}
69
70int cxio_hal_cq_op(struct cxio_rdev *rdev_p, struct t3_cq *cq,
71 enum t3_cq_opcode op, u32 credit)
72{
73 int ret;
74 struct t3_cqe *cqe;
75 u32 rptr;
76
77 struct rdma_cq_op setup;
78 setup.id = cq->cqid;
79 setup.credits = (op == CQ_CREDIT_UPDATE) ? credit : 0;
80 setup.op = op;
81 ret = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_CQ_OP, &setup);
82
83 if ((ret < 0) || (op == CQ_CREDIT_UPDATE))
84 return ret;
85
86 /*
87 * If the rearm returned an index other than our current index,
88 * then there might be CQE's in flight (being DMA'd). We must wait
89 * here for them to complete or the consumer can miss a notification.
90 */
91 if (Q_PTR2IDX((cq->rptr), cq->size_log2) != ret) {
92 int i=0;
93
94 rptr = cq->rptr;
95
96 /*
97 * Keep the generation correct by bumping rptr until it
98 * matches the index returned by the rearm - 1.
99 */
100 while (Q_PTR2IDX((rptr+1), cq->size_log2) != ret)
101 rptr++;
102
103 /*
104 * Now rptr is the index for the (last) cqe that was
105 * in-flight at the time the HW rearmed the CQ. We
106 * spin until that CQE is valid.
107 */
108 cqe = cq->queue + Q_PTR2IDX(rptr, cq->size_log2);
109 while (!CQ_VLD_ENTRY(rptr, cq->size_log2, cqe)) {
110 udelay(1);
111 if (i++ > 1000000) {
112 BUG_ON(1);
113 printk(KERN_ERR "%s: stalled rnic\n",
114 rdev_p->dev_name);
115 return -EIO;
116 }
117 }
Roland Dreiered23a722007-05-06 21:02:48 -0700118
119 return 1;
Steve Wiseb038ced2007-02-12 16:16:18 -0800120 }
Roland Dreiered23a722007-05-06 21:02:48 -0700121
Steve Wiseb038ced2007-02-12 16:16:18 -0800122 return 0;
123}
124
Adrian Bunk2b540352007-02-21 11:52:49 +0100125static int cxio_hal_clear_cq_ctx(struct cxio_rdev *rdev_p, u32 cqid)
Steve Wiseb038ced2007-02-12 16:16:18 -0800126{
127 struct rdma_cq_setup setup;
128 setup.id = cqid;
129 setup.base_addr = 0; /* NULL address */
130 setup.size = 0; /* disaable the CQ */
131 setup.credits = 0;
132 setup.credit_thres = 0;
133 setup.ovfl_mode = 0;
134 return (rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_CQ_SETUP, &setup));
135}
136
Adrian Bunk2b540352007-02-21 11:52:49 +0100137static int cxio_hal_clear_qp_ctx(struct cxio_rdev *rdev_p, u32 qpid)
Steve Wiseb038ced2007-02-12 16:16:18 -0800138{
139 u64 sge_cmd;
140 struct t3_modify_qp_wr *wqe;
141 struct sk_buff *skb = alloc_skb(sizeof(*wqe), GFP_KERNEL);
142 if (!skb) {
Harvey Harrison33718362008-04-16 21:01:10 -0700143 PDBG("%s alloc_skb failed\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -0800144 return -ENOMEM;
145 }
146 wqe = (struct t3_modify_qp_wr *) skb_put(skb, sizeof(*wqe));
147 memset(wqe, 0, sizeof(*wqe));
Steve Wisee7e55822008-07-14 23:48:45 -0700148 build_fw_riwrh((struct fw_riwrh *) wqe, T3_WR_QP_MOD,
149 T3_COMPLETION_FLAG | T3_NOTIFY_FLAG, 0, qpid, 7,
150 T3_SOPEOP);
Steve Wiseb038ced2007-02-12 16:16:18 -0800151 wqe->flags = cpu_to_be32(MODQP_WRITE_EC);
152 sge_cmd = qpid << 8 | 3;
153 wqe->sge_cmd = cpu_to_be64(sge_cmd);
154 skb->priority = CPL_PRIORITY_CONTROL;
155 return (cxgb3_ofld_send(rdev_p->t3cdev_p, skb));
156}
157
158int cxio_create_cq(struct cxio_rdev *rdev_p, struct t3_cq *cq)
159{
160 struct rdma_cq_setup setup;
161 int size = (1UL << (cq->size_log2)) * sizeof(struct t3_cqe);
162
163 cq->cqid = cxio_hal_get_cqid(rdev_p->rscp);
164 if (!cq->cqid)
165 return -ENOMEM;
166 cq->sw_queue = kzalloc(size, GFP_KERNEL);
167 if (!cq->sw_queue)
168 return -ENOMEM;
169 cq->queue = dma_alloc_coherent(&(rdev_p->rnic_info.pdev->dev),
170 (1UL << (cq->size_log2)) *
171 sizeof(struct t3_cqe),
172 &(cq->dma_addr), GFP_KERNEL);
173 if (!cq->queue) {
174 kfree(cq->sw_queue);
175 return -ENOMEM;
176 }
177 pci_unmap_addr_set(cq, mapping, cq->dma_addr);
178 memset(cq->queue, 0, size);
179 setup.id = cq->cqid;
180 setup.base_addr = (u64) (cq->dma_addr);
181 setup.size = 1UL << cq->size_log2;
182 setup.credits = 65535;
183 setup.credit_thres = 1;
Steve Wise8176d292008-01-24 16:30:16 -0600184 if (rdev_p->t3cdev_p->type != T3A)
Steve Wiseb038ced2007-02-12 16:16:18 -0800185 setup.ovfl_mode = 0;
186 else
187 setup.ovfl_mode = 1;
188 return (rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_CQ_SETUP, &setup));
189}
190
191int cxio_resize_cq(struct cxio_rdev *rdev_p, struct t3_cq *cq)
192{
193 struct rdma_cq_setup setup;
194 setup.id = cq->cqid;
195 setup.base_addr = (u64) (cq->dma_addr);
196 setup.size = 1UL << cq->size_log2;
197 setup.credits = setup.size;
198 setup.credit_thres = setup.size; /* TBD: overflow recovery */
199 setup.ovfl_mode = 1;
200 return (rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_CQ_SETUP, &setup));
201}
202
203static u32 get_qpid(struct cxio_rdev *rdev_p, struct cxio_ucontext *uctx)
204{
205 struct cxio_qpid_list *entry;
206 u32 qpid;
207 int i;
208
209 mutex_lock(&uctx->lock);
210 if (!list_empty(&uctx->qpids)) {
211 entry = list_entry(uctx->qpids.next, struct cxio_qpid_list,
212 entry);
213 list_del(&entry->entry);
214 qpid = entry->qpid;
215 kfree(entry);
216 } else {
217 qpid = cxio_hal_get_qpid(rdev_p->rscp);
218 if (!qpid)
219 goto out;
220 for (i = qpid+1; i & rdev_p->qpmask; i++) {
221 entry = kmalloc(sizeof *entry, GFP_KERNEL);
222 if (!entry)
223 break;
224 entry->qpid = i;
225 list_add_tail(&entry->entry, &uctx->qpids);
226 }
227 }
228out:
229 mutex_unlock(&uctx->lock);
Harvey Harrison33718362008-04-16 21:01:10 -0700230 PDBG("%s qpid 0x%x\n", __func__, qpid);
Steve Wiseb038ced2007-02-12 16:16:18 -0800231 return qpid;
232}
233
234static void put_qpid(struct cxio_rdev *rdev_p, u32 qpid,
235 struct cxio_ucontext *uctx)
236{
237 struct cxio_qpid_list *entry;
238
239 entry = kmalloc(sizeof *entry, GFP_KERNEL);
240 if (!entry)
241 return;
Harvey Harrison33718362008-04-16 21:01:10 -0700242 PDBG("%s qpid 0x%x\n", __func__, qpid);
Steve Wiseb038ced2007-02-12 16:16:18 -0800243 entry->qpid = qpid;
244 mutex_lock(&uctx->lock);
245 list_add_tail(&entry->entry, &uctx->qpids);
246 mutex_unlock(&uctx->lock);
247}
248
249void cxio_release_ucontext(struct cxio_rdev *rdev_p, struct cxio_ucontext *uctx)
250{
251 struct list_head *pos, *nxt;
252 struct cxio_qpid_list *entry;
253
254 mutex_lock(&uctx->lock);
255 list_for_each_safe(pos, nxt, &uctx->qpids) {
256 entry = list_entry(pos, struct cxio_qpid_list, entry);
257 list_del_init(&entry->entry);
258 if (!(entry->qpid & rdev_p->qpmask))
259 cxio_hal_put_qpid(rdev_p->rscp, entry->qpid);
260 kfree(entry);
261 }
262 mutex_unlock(&uctx->lock);
263}
264
265void cxio_init_ucontext(struct cxio_rdev *rdev_p, struct cxio_ucontext *uctx)
266{
267 INIT_LIST_HEAD(&uctx->qpids);
268 mutex_init(&uctx->lock);
269}
270
271int cxio_create_qp(struct cxio_rdev *rdev_p, u32 kernel_domain,
272 struct t3_wq *wq, struct cxio_ucontext *uctx)
273{
274 int depth = 1UL << wq->size_log2;
275 int rqsize = 1UL << wq->rq_size_log2;
276
277 wq->qpid = get_qpid(rdev_p, uctx);
278 if (!wq->qpid)
279 return -ENOMEM;
280
281 wq->rq = kzalloc(depth * sizeof(u64), GFP_KERNEL);
282 if (!wq->rq)
283 goto err1;
284
285 wq->rq_addr = cxio_hal_rqtpool_alloc(rdev_p, rqsize);
286 if (!wq->rq_addr)
287 goto err2;
288
289 wq->sq = kzalloc(depth * sizeof(struct t3_swsq), GFP_KERNEL);
290 if (!wq->sq)
291 goto err3;
292
293 wq->queue = dma_alloc_coherent(&(rdev_p->rnic_info.pdev->dev),
294 depth * sizeof(union t3_wr),
295 &(wq->dma_addr), GFP_KERNEL);
296 if (!wq->queue)
297 goto err4;
298
299 memset(wq->queue, 0, depth * sizeof(union t3_wr));
300 pci_unmap_addr_set(wq, mapping, wq->dma_addr);
301 wq->doorbell = (void __iomem *)rdev_p->rnic_info.kdb_addr;
302 if (!kernel_domain)
303 wq->udb = (u64)rdev_p->rnic_info.udbell_physbase +
304 (wq->qpid << rdev_p->qpshift);
Harvey Harrison33718362008-04-16 21:01:10 -0700305 PDBG("%s qpid 0x%x doorbell 0x%p udb 0x%llx\n", __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -0800306 wq->qpid, wq->doorbell, (unsigned long long) wq->udb);
307 return 0;
308err4:
309 kfree(wq->sq);
310err3:
311 cxio_hal_rqtpool_free(rdev_p, wq->rq_addr, rqsize);
312err2:
313 kfree(wq->rq);
314err1:
315 put_qpid(rdev_p, wq->qpid, uctx);
316 return -ENOMEM;
317}
318
319int cxio_destroy_cq(struct cxio_rdev *rdev_p, struct t3_cq *cq)
320{
321 int err;
322 err = cxio_hal_clear_cq_ctx(rdev_p, cq->cqid);
323 kfree(cq->sw_queue);
324 dma_free_coherent(&(rdev_p->rnic_info.pdev->dev),
325 (1UL << (cq->size_log2))
326 * sizeof(struct t3_cqe), cq->queue,
327 pci_unmap_addr(cq, mapping));
328 cxio_hal_put_cqid(rdev_p->rscp, cq->cqid);
329 return err;
330}
331
332int cxio_destroy_qp(struct cxio_rdev *rdev_p, struct t3_wq *wq,
333 struct cxio_ucontext *uctx)
334{
335 dma_free_coherent(&(rdev_p->rnic_info.pdev->dev),
336 (1UL << (wq->size_log2))
337 * sizeof(union t3_wr), wq->queue,
338 pci_unmap_addr(wq, mapping));
339 kfree(wq->sq);
340 cxio_hal_rqtpool_free(rdev_p, wq->rq_addr, (1UL << wq->rq_size_log2));
341 kfree(wq->rq);
342 put_qpid(rdev_p, wq->qpid, uctx);
343 return 0;
344}
345
346static void insert_recv_cqe(struct t3_wq *wq, struct t3_cq *cq)
347{
348 struct t3_cqe cqe;
349
Harvey Harrison33718362008-04-16 21:01:10 -0700350 PDBG("%s wq %p cq %p sw_rptr 0x%x sw_wptr 0x%x\n", __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -0800351 wq, cq, cq->sw_rptr, cq->sw_wptr);
352 memset(&cqe, 0, sizeof(cqe));
353 cqe.header = cpu_to_be32(V_CQE_STATUS(TPT_ERR_SWFLUSH) |
354 V_CQE_OPCODE(T3_SEND) |
355 V_CQE_TYPE(0) |
356 V_CQE_SWCQE(1) |
357 V_CQE_QPID(wq->qpid) |
358 V_CQE_GENBIT(Q_GENBIT(cq->sw_wptr,
359 cq->size_log2)));
360 *(cq->sw_queue + Q_PTR2IDX(cq->sw_wptr, cq->size_log2)) = cqe;
361 cq->sw_wptr++;
362}
363
Steve Wisec8286942008-05-02 11:17:41 -0500364int cxio_flush_rq(struct t3_wq *wq, struct t3_cq *cq, int count)
Steve Wiseb038ced2007-02-12 16:16:18 -0800365{
366 u32 ptr;
Steve Wisec8286942008-05-02 11:17:41 -0500367 int flushed = 0;
Steve Wiseb038ced2007-02-12 16:16:18 -0800368
Harvey Harrison33718362008-04-16 21:01:10 -0700369 PDBG("%s wq %p cq %p\n", __func__, wq, cq);
Steve Wiseb038ced2007-02-12 16:16:18 -0800370
371 /* flush RQ */
Harvey Harrison33718362008-04-16 21:01:10 -0700372 PDBG("%s rq_rptr %u rq_wptr %u skip count %u\n", __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -0800373 wq->rq_rptr, wq->rq_wptr, count);
374 ptr = wq->rq_rptr + count;
Steve Wisec8286942008-05-02 11:17:41 -0500375 while (ptr++ != wq->rq_wptr) {
Steve Wiseb038ced2007-02-12 16:16:18 -0800376 insert_recv_cqe(wq, cq);
Steve Wisec8286942008-05-02 11:17:41 -0500377 flushed++;
378 }
379 return flushed;
Steve Wiseb038ced2007-02-12 16:16:18 -0800380}
381
382static void insert_sq_cqe(struct t3_wq *wq, struct t3_cq *cq,
383 struct t3_swsq *sqp)
384{
385 struct t3_cqe cqe;
386
Harvey Harrison33718362008-04-16 21:01:10 -0700387 PDBG("%s wq %p cq %p sw_rptr 0x%x sw_wptr 0x%x\n", __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -0800388 wq, cq, cq->sw_rptr, cq->sw_wptr);
389 memset(&cqe, 0, sizeof(cqe));
390 cqe.header = cpu_to_be32(V_CQE_STATUS(TPT_ERR_SWFLUSH) |
391 V_CQE_OPCODE(sqp->opcode) |
392 V_CQE_TYPE(1) |
393 V_CQE_SWCQE(1) |
394 V_CQE_QPID(wq->qpid) |
395 V_CQE_GENBIT(Q_GENBIT(cq->sw_wptr,
396 cq->size_log2)));
397 cqe.u.scqe.wrid_hi = sqp->sq_wptr;
398
399 *(cq->sw_queue + Q_PTR2IDX(cq->sw_wptr, cq->size_log2)) = cqe;
400 cq->sw_wptr++;
401}
402
Steve Wisec8286942008-05-02 11:17:41 -0500403int cxio_flush_sq(struct t3_wq *wq, struct t3_cq *cq, int count)
Steve Wiseb038ced2007-02-12 16:16:18 -0800404{
405 __u32 ptr;
Steve Wisec8286942008-05-02 11:17:41 -0500406 int flushed = 0;
Steve Wiseb038ced2007-02-12 16:16:18 -0800407 struct t3_swsq *sqp = wq->sq + Q_PTR2IDX(wq->sq_rptr, wq->sq_size_log2);
408
409 ptr = wq->sq_rptr + count;
Steve Wisea58e58f2008-05-13 11:52:55 -0700410 sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
Steve Wiseb038ced2007-02-12 16:16:18 -0800411 while (ptr != wq->sq_wptr) {
412 insert_sq_cqe(wq, cq, sqp);
Steve Wiseb038ced2007-02-12 16:16:18 -0800413 ptr++;
Steve Wisea58e58f2008-05-13 11:52:55 -0700414 sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
Steve Wisec8286942008-05-02 11:17:41 -0500415 flushed++;
Steve Wiseb038ced2007-02-12 16:16:18 -0800416 }
Steve Wisec8286942008-05-02 11:17:41 -0500417 return flushed;
Steve Wiseb038ced2007-02-12 16:16:18 -0800418}
419
420/*
421 * Move all CQEs from the HWCQ into the SWCQ.
422 */
423void cxio_flush_hw_cq(struct t3_cq *cq)
424{
425 struct t3_cqe *cqe, *swcqe;
426
Harvey Harrison33718362008-04-16 21:01:10 -0700427 PDBG("%s cq %p cqid 0x%x\n", __func__, cq, cq->cqid);
Steve Wiseb038ced2007-02-12 16:16:18 -0800428 cqe = cxio_next_hw_cqe(cq);
429 while (cqe) {
430 PDBG("%s flushing hwcq rptr 0x%x to swcq wptr 0x%x\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700431 __func__, cq->rptr, cq->sw_wptr);
Steve Wiseb038ced2007-02-12 16:16:18 -0800432 swcqe = cq->sw_queue + Q_PTR2IDX(cq->sw_wptr, cq->size_log2);
433 *swcqe = *cqe;
434 swcqe->header |= cpu_to_be32(V_CQE_SWCQE(1));
435 cq->sw_wptr++;
436 cq->rptr++;
437 cqe = cxio_next_hw_cqe(cq);
438 }
439}
440
Adrian Bunk2b540352007-02-21 11:52:49 +0100441static int cqe_completes_wr(struct t3_cqe *cqe, struct t3_wq *wq)
Steve Wiseb038ced2007-02-12 16:16:18 -0800442{
443 if (CQE_OPCODE(*cqe) == T3_TERMINATE)
444 return 0;
445
446 if ((CQE_OPCODE(*cqe) == T3_RDMA_WRITE) && RQ_TYPE(*cqe))
447 return 0;
448
449 if ((CQE_OPCODE(*cqe) == T3_READ_RESP) && SQ_TYPE(*cqe))
450 return 0;
451
452 if ((CQE_OPCODE(*cqe) == T3_SEND) && RQ_TYPE(*cqe) &&
453 Q_EMPTY(wq->rq_rptr, wq->rq_wptr))
454 return 0;
455
456 return 1;
457}
458
459void cxio_count_scqes(struct t3_cq *cq, struct t3_wq *wq, int *count)
460{
461 struct t3_cqe *cqe;
462 u32 ptr;
463
464 *count = 0;
465 ptr = cq->sw_rptr;
466 while (!Q_EMPTY(ptr, cq->sw_wptr)) {
467 cqe = cq->sw_queue + (Q_PTR2IDX(ptr, cq->size_log2));
Steve Wisef8b0dfd2008-04-29 13:46:52 -0700468 if ((SQ_TYPE(*cqe) ||
469 ((CQE_OPCODE(*cqe) == T3_READ_RESP) && wq->oldest_read)) &&
Steve Wiseb038ced2007-02-12 16:16:18 -0800470 (CQE_QPID(*cqe) == wq->qpid))
471 (*count)++;
472 ptr++;
473 }
Harvey Harrison33718362008-04-16 21:01:10 -0700474 PDBG("%s cq %p count %d\n", __func__, cq, *count);
Steve Wiseb038ced2007-02-12 16:16:18 -0800475}
476
477void cxio_count_rcqes(struct t3_cq *cq, struct t3_wq *wq, int *count)
478{
479 struct t3_cqe *cqe;
480 u32 ptr;
481
482 *count = 0;
Harvey Harrison33718362008-04-16 21:01:10 -0700483 PDBG("%s count zero %d\n", __func__, *count);
Steve Wiseb038ced2007-02-12 16:16:18 -0800484 ptr = cq->sw_rptr;
485 while (!Q_EMPTY(ptr, cq->sw_wptr)) {
486 cqe = cq->sw_queue + (Q_PTR2IDX(ptr, cq->size_log2));
487 if (RQ_TYPE(*cqe) && (CQE_OPCODE(*cqe) != T3_READ_RESP) &&
488 (CQE_QPID(*cqe) == wq->qpid) && cqe_completes_wr(cqe, wq))
489 (*count)++;
490 ptr++;
491 }
Harvey Harrison33718362008-04-16 21:01:10 -0700492 PDBG("%s cq %p count %d\n", __func__, cq, *count);
Steve Wiseb038ced2007-02-12 16:16:18 -0800493}
494
495static int cxio_hal_init_ctrl_cq(struct cxio_rdev *rdev_p)
496{
497 struct rdma_cq_setup setup;
498 setup.id = 0;
499 setup.base_addr = 0; /* NULL address */
500 setup.size = 1; /* enable the CQ */
501 setup.credits = 0;
502
503 /* force SGE to redirect to RspQ and interrupt */
504 setup.credit_thres = 0;
505 setup.ovfl_mode = 1;
506 return (rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_CQ_SETUP, &setup));
507}
508
509static int cxio_hal_init_ctrl_qp(struct cxio_rdev *rdev_p)
510{
511 int err;
512 u64 sge_cmd, ctx0, ctx1;
513 u64 base_addr;
514 struct t3_modify_qp_wr *wqe;
Steve Wiseed6ee512007-03-26 17:48:52 -0500515 struct sk_buff *skb;
Steve Wiseb038ced2007-02-12 16:16:18 -0800516
Steve Wiseed6ee512007-03-26 17:48:52 -0500517 skb = alloc_skb(sizeof(*wqe), GFP_KERNEL);
Steve Wiseb038ced2007-02-12 16:16:18 -0800518 if (!skb) {
Harvey Harrison33718362008-04-16 21:01:10 -0700519 PDBG("%s alloc_skb failed\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -0800520 return -ENOMEM;
521 }
522 err = cxio_hal_init_ctrl_cq(rdev_p);
523 if (err) {
Harvey Harrison33718362008-04-16 21:01:10 -0700524 PDBG("%s err %d initializing ctrl_cq\n", __func__, err);
Steve Wiseed6ee512007-03-26 17:48:52 -0500525 goto err;
Steve Wiseb038ced2007-02-12 16:16:18 -0800526 }
527 rdev_p->ctrl_qp.workq = dma_alloc_coherent(
528 &(rdev_p->rnic_info.pdev->dev),
529 (1 << T3_CTRL_QP_SIZE_LOG2) *
530 sizeof(union t3_wr),
531 &(rdev_p->ctrl_qp.dma_addr),
532 GFP_KERNEL);
533 if (!rdev_p->ctrl_qp.workq) {
Harvey Harrison33718362008-04-16 21:01:10 -0700534 PDBG("%s dma_alloc_coherent failed\n", __func__);
Steve Wiseed6ee512007-03-26 17:48:52 -0500535 err = -ENOMEM;
536 goto err;
Steve Wiseb038ced2007-02-12 16:16:18 -0800537 }
538 pci_unmap_addr_set(&rdev_p->ctrl_qp, mapping,
539 rdev_p->ctrl_qp.dma_addr);
540 rdev_p->ctrl_qp.doorbell = (void __iomem *)rdev_p->rnic_info.kdb_addr;
541 memset(rdev_p->ctrl_qp.workq, 0,
542 (1 << T3_CTRL_QP_SIZE_LOG2) * sizeof(union t3_wr));
543
544 mutex_init(&rdev_p->ctrl_qp.lock);
545 init_waitqueue_head(&rdev_p->ctrl_qp.waitq);
546
547 /* update HW Ctrl QP context */
548 base_addr = rdev_p->ctrl_qp.dma_addr;
549 base_addr >>= 12;
550 ctx0 = (V_EC_SIZE((1 << T3_CTRL_QP_SIZE_LOG2)) |
551 V_EC_BASE_LO((u32) base_addr & 0xffff));
552 ctx0 <<= 32;
553 ctx0 |= V_EC_CREDITS(FW_WR_NUM);
554 base_addr >>= 16;
555 ctx1 = (u32) base_addr;
556 base_addr >>= 32;
557 ctx1 |= ((u64) (V_EC_BASE_HI((u32) base_addr & 0xf) | V_EC_RESPQ(0) |
558 V_EC_TYPE(0) | V_EC_GEN(1) |
559 V_EC_UP_TOKEN(T3_CTL_QP_TID) | F_EC_VALID)) << 32;
560 wqe = (struct t3_modify_qp_wr *) skb_put(skb, sizeof(*wqe));
561 memset(wqe, 0, sizeof(*wqe));
Steve Wise6eda48d2007-06-19 09:27:48 -0500562 build_fw_riwrh((struct fw_riwrh *) wqe, T3_WR_QP_MOD, 0, 0,
Steve Wisee7e55822008-07-14 23:48:45 -0700563 T3_CTL_QP_TID, 7, T3_SOPEOP);
Steve Wiseb038ced2007-02-12 16:16:18 -0800564 wqe->flags = cpu_to_be32(MODQP_WRITE_EC);
565 sge_cmd = (3ULL << 56) | FW_RI_SGEEC_START << 8 | 3;
566 wqe->sge_cmd = cpu_to_be64(sge_cmd);
567 wqe->ctx1 = cpu_to_be64(ctx1);
568 wqe->ctx0 = cpu_to_be64(ctx0);
569 PDBG("CtrlQP dma_addr 0x%llx workq %p size %d\n",
570 (unsigned long long) rdev_p->ctrl_qp.dma_addr,
571 rdev_p->ctrl_qp.workq, 1 << T3_CTRL_QP_SIZE_LOG2);
572 skb->priority = CPL_PRIORITY_CONTROL;
573 return (cxgb3_ofld_send(rdev_p->t3cdev_p, skb));
Steve Wiseed6ee512007-03-26 17:48:52 -0500574err:
575 kfree_skb(skb);
576 return err;
Steve Wiseb038ced2007-02-12 16:16:18 -0800577}
578
579static int cxio_hal_destroy_ctrl_qp(struct cxio_rdev *rdev_p)
580{
581 dma_free_coherent(&(rdev_p->rnic_info.pdev->dev),
582 (1UL << T3_CTRL_QP_SIZE_LOG2)
583 * sizeof(union t3_wr), rdev_p->ctrl_qp.workq,
584 pci_unmap_addr(&rdev_p->ctrl_qp, mapping));
585 return cxio_hal_clear_qp_ctx(rdev_p, T3_CTRL_QP_ID);
586}
587
588/* write len bytes of data into addr (32B aligned address)
589 * If data is NULL, clear len byte of memory to zero.
590 * caller aquires the ctrl_qp lock before the call
591 */
592static int cxio_hal_ctrl_qp_write_mem(struct cxio_rdev *rdev_p, u32 addr,
Roland Dreier273748c2008-05-06 15:56:22 -0700593 u32 len, void *data)
Steve Wiseb038ced2007-02-12 16:16:18 -0800594{
595 u32 i, nr_wqe, copy_len;
596 u8 *copy_data;
Joe Perches94545e82007-12-17 11:30:36 -0800597 u8 wr_len, utx_len; /* length in 8 byte flit */
Steve Wiseb038ced2007-02-12 16:16:18 -0800598 enum t3_wr_flags flag;
599 __be64 *wqe;
600 u64 utx_cmd;
601 addr &= 0x7FFFFFF;
602 nr_wqe = len % 96 ? len / 96 + 1 : len / 96; /* 96B max per WQE */
603 PDBG("%s wptr 0x%x rptr 0x%x len %d, nr_wqe %d data %p addr 0x%0x\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700604 __func__, rdev_p->ctrl_qp.wptr, rdev_p->ctrl_qp.rptr, len,
Steve Wiseb038ced2007-02-12 16:16:18 -0800605 nr_wqe, data, addr);
606 utx_len = 3; /* in 32B unit */
607 for (i = 0; i < nr_wqe; i++) {
608 if (Q_FULL(rdev_p->ctrl_qp.rptr, rdev_p->ctrl_qp.wptr,
609 T3_CTRL_QP_SIZE_LOG2)) {
610 PDBG("%s ctrl_qp full wtpr 0x%0x rptr 0x%0x, "
Harvey Harrison33718362008-04-16 21:01:10 -0700611 "wait for more space i %d\n", __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -0800612 rdev_p->ctrl_qp.wptr, rdev_p->ctrl_qp.rptr, i);
613 if (wait_event_interruptible(rdev_p->ctrl_qp.waitq,
614 !Q_FULL(rdev_p->ctrl_qp.rptr,
615 rdev_p->ctrl_qp.wptr,
616 T3_CTRL_QP_SIZE_LOG2))) {
617 PDBG("%s ctrl_qp workq interrupted\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700618 __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -0800619 return -ERESTARTSYS;
620 }
621 PDBG("%s ctrl_qp wakeup, continue posting work request "
Harvey Harrison33718362008-04-16 21:01:10 -0700622 "i %d\n", __func__, i);
Steve Wiseb038ced2007-02-12 16:16:18 -0800623 }
624 wqe = (__be64 *)(rdev_p->ctrl_qp.workq + (rdev_p->ctrl_qp.wptr %
625 (1 << T3_CTRL_QP_SIZE_LOG2)));
626 flag = 0;
627 if (i == (nr_wqe - 1)) {
628 /* last WQE */
Roland Dreier273748c2008-05-06 15:56:22 -0700629 flag = T3_COMPLETION_FLAG;
Steve Wiseb038ced2007-02-12 16:16:18 -0800630 if (len % 32)
631 utx_len = len / 32 + 1;
632 else
633 utx_len = len / 32;
634 }
635
636 /*
637 * Force a CQE to return the credit to the workq in case
638 * we posted more than half the max QP size of WRs
639 */
640 if ((i != 0) &&
641 (i % (((1 << T3_CTRL_QP_SIZE_LOG2)) >> 1) == 0)) {
642 flag = T3_COMPLETION_FLAG;
Harvey Harrison33718362008-04-16 21:01:10 -0700643 PDBG("%s force completion at i %d\n", __func__, i);
Steve Wiseb038ced2007-02-12 16:16:18 -0800644 }
645
646 /* build the utx mem command */
647 wqe += (sizeof(struct t3_bypass_wr) >> 3);
648 utx_cmd = (T3_UTX_MEM_WRITE << 28) | (addr + i * 3);
649 utx_cmd <<= 32;
650 utx_cmd |= (utx_len << 28) | ((utx_len << 2) + 1);
651 *wqe = cpu_to_be64(utx_cmd);
652 wqe++;
653 copy_data = (u8 *) data + i * 96;
654 copy_len = len > 96 ? 96 : len;
655
656 /* clear memory content if data is NULL */
657 if (data)
658 memcpy(wqe, copy_data, copy_len);
659 else
660 memset(wqe, 0, copy_len);
661 if (copy_len % 32)
662 memset(((u8 *) wqe) + copy_len, 0,
663 32 - (copy_len % 32));
664 wr_len = ((sizeof(struct t3_bypass_wr)) >> 3) + 1 +
665 (utx_len << 2);
666 wqe = (__be64 *)(rdev_p->ctrl_qp.workq + (rdev_p->ctrl_qp.wptr %
667 (1 << T3_CTRL_QP_SIZE_LOG2)));
668
669 /* wptr in the WRID[31:0] */
670 ((union t3_wrid *)(wqe+1))->id0.low = rdev_p->ctrl_qp.wptr;
671
672 /*
673 * This must be the last write with a memory barrier
674 * for the genbit
675 */
676 build_fw_riwrh((struct fw_riwrh *) wqe, T3_WR_BP, flag,
677 Q_GENBIT(rdev_p->ctrl_qp.wptr,
678 T3_CTRL_QP_SIZE_LOG2), T3_CTRL_QP_ID,
Steve Wisee7e55822008-07-14 23:48:45 -0700679 wr_len, T3_SOPEOP);
Steve Wiseb038ced2007-02-12 16:16:18 -0800680 if (flag == T3_COMPLETION_FLAG)
681 ring_doorbell(rdev_p->ctrl_qp.doorbell, T3_CTRL_QP_ID);
682 len -= 96;
683 rdev_p->ctrl_qp.wptr++;
684 }
685 return 0;
686}
687
Roland Dreier273748c2008-05-06 15:56:22 -0700688/* IN: stag key, pdid, perm, zbva, to, len, page_size, pbl_size and pbl_addr
689 * OUT: stag index
Steve Wiseb038ced2007-02-12 16:16:18 -0800690 * TBD: shared memory region support
691 */
692static int __cxio_tpt_op(struct cxio_rdev *rdev_p, u32 reset_tpt_entry,
693 u32 *stag, u8 stag_state, u32 pdid,
694 enum tpt_mem_type type, enum tpt_mem_perm perm,
Roland Dreier273748c2008-05-06 15:56:22 -0700695 u32 zbva, u64 to, u32 len, u8 page_size,
696 u32 pbl_size, u32 pbl_addr)
Steve Wiseb038ced2007-02-12 16:16:18 -0800697{
698 int err;
699 struct tpt_entry tpt;
700 u32 stag_idx;
701 u32 wptr;
Steve Wiseb038ced2007-02-12 16:16:18 -0800702
703 stag_state = stag_state > 0;
704 stag_idx = (*stag) >> 8;
705
706 if ((!reset_tpt_entry) && !(*stag != T3_STAG_UNSET)) {
707 stag_idx = cxio_hal_get_stag(rdev_p->rscp);
708 if (!stag_idx)
709 return -ENOMEM;
710 *stag = (stag_idx << 8) | ((*stag) & 0xFF);
711 }
712 PDBG("%s stag_state 0x%0x type 0x%0x pdid 0x%0x, stag_idx 0x%x\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700713 __func__, stag_state, type, pdid, stag_idx);
Steve Wiseb038ced2007-02-12 16:16:18 -0800714
Steve Wiseb038ced2007-02-12 16:16:18 -0800715 mutex_lock(&rdev_p->ctrl_qp.lock);
716
Steve Wiseb038ced2007-02-12 16:16:18 -0800717 /* write TPT entry */
718 if (reset_tpt_entry)
719 memset(&tpt, 0, sizeof(tpt));
720 else {
721 tpt.valid_stag_pdid = cpu_to_be32(F_TPT_VALID |
722 V_TPT_STAG_KEY((*stag) & M_TPT_STAG_KEY) |
723 V_TPT_STAG_STATE(stag_state) |
724 V_TPT_STAG_TYPE(type) | V_TPT_PDID(pdid));
725 BUG_ON(page_size >= 28);
726 tpt.flags_pagesize_qpid = cpu_to_be32(V_TPT_PERM(perm) |
727 F_TPT_MW_BIND_ENABLE |
728 V_TPT_ADDR_TYPE((zbva ? TPT_ZBTO : TPT_VATO)) |
729 V_TPT_PAGE_SIZE(page_size));
730 tpt.rsvd_pbl_addr = reset_tpt_entry ? 0 :
Roland Dreier273748c2008-05-06 15:56:22 -0700731 cpu_to_be32(V_TPT_PBL_ADDR(PBL_OFF(rdev_p, pbl_addr)>>3));
Steve Wiseb038ced2007-02-12 16:16:18 -0800732 tpt.len = cpu_to_be32(len);
733 tpt.va_hi = cpu_to_be32((u32) (to >> 32));
734 tpt.va_low_or_fbo = cpu_to_be32((u32) (to & 0xFFFFFFFFULL));
735 tpt.rsvd_bind_cnt_or_pstag = 0;
736 tpt.rsvd_pbl_size = reset_tpt_entry ? 0 :
Roland Dreier273748c2008-05-06 15:56:22 -0700737 cpu_to_be32(V_TPT_PBL_SIZE(pbl_size >> 2));
Steve Wiseb038ced2007-02-12 16:16:18 -0800738 }
739 err = cxio_hal_ctrl_qp_write_mem(rdev_p,
740 stag_idx +
741 (rdev_p->rnic_info.tpt_base >> 5),
Roland Dreier273748c2008-05-06 15:56:22 -0700742 sizeof(tpt), &tpt);
Steve Wiseb038ced2007-02-12 16:16:18 -0800743
744 /* release the stag index to free pool */
745 if (reset_tpt_entry)
746 cxio_hal_put_stag(rdev_p->rscp, stag_idx);
Roland Dreier273748c2008-05-06 15:56:22 -0700747
Steve Wiseb038ced2007-02-12 16:16:18 -0800748 wptr = rdev_p->ctrl_qp.wptr;
749 mutex_unlock(&rdev_p->ctrl_qp.lock);
750 if (!err)
751 if (wait_event_interruptible(rdev_p->ctrl_qp.waitq,
752 SEQ32_GE(rdev_p->ctrl_qp.rptr,
753 wptr)))
754 return -ERESTARTSYS;
755 return err;
756}
757
Roland Dreier273748c2008-05-06 15:56:22 -0700758int cxio_write_pbl(struct cxio_rdev *rdev_p, __be64 *pbl,
759 u32 pbl_addr, u32 pbl_size)
760{
761 u32 wptr;
762 int err;
763
764 PDBG("%s *pdb_addr 0x%x, pbl_base 0x%x, pbl_size %d\n",
765 __func__, pbl_addr, rdev_p->rnic_info.pbl_base,
766 pbl_size);
767
768 mutex_lock(&rdev_p->ctrl_qp.lock);
769 err = cxio_hal_ctrl_qp_write_mem(rdev_p, pbl_addr >> 5, pbl_size << 3,
770 pbl);
771 wptr = rdev_p->ctrl_qp.wptr;
772 mutex_unlock(&rdev_p->ctrl_qp.lock);
773 if (err)
774 return err;
775
776 if (wait_event_interruptible(rdev_p->ctrl_qp.waitq,
777 SEQ32_GE(rdev_p->ctrl_qp.rptr,
778 wptr)))
779 return -ERESTARTSYS;
780
781 return 0;
782}
783
Steve Wiseb038ced2007-02-12 16:16:18 -0800784int cxio_register_phys_mem(struct cxio_rdev *rdev_p, u32 *stag, u32 pdid,
785 enum tpt_mem_perm perm, u32 zbva, u64 to, u32 len,
Roland Dreier273748c2008-05-06 15:56:22 -0700786 u8 page_size, u32 pbl_size, u32 pbl_addr)
Steve Wiseb038ced2007-02-12 16:16:18 -0800787{
788 *stag = T3_STAG_UNSET;
789 return __cxio_tpt_op(rdev_p, 0, stag, 1, pdid, TPT_NON_SHARED_MR, perm,
Roland Dreier273748c2008-05-06 15:56:22 -0700790 zbva, to, len, page_size, pbl_size, pbl_addr);
Steve Wiseb038ced2007-02-12 16:16:18 -0800791}
792
793int cxio_reregister_phys_mem(struct cxio_rdev *rdev_p, u32 *stag, u32 pdid,
794 enum tpt_mem_perm perm, u32 zbva, u64 to, u32 len,
Roland Dreier273748c2008-05-06 15:56:22 -0700795 u8 page_size, u32 pbl_size, u32 pbl_addr)
Steve Wiseb038ced2007-02-12 16:16:18 -0800796{
797 return __cxio_tpt_op(rdev_p, 0, stag, 1, pdid, TPT_NON_SHARED_MR, perm,
Roland Dreier273748c2008-05-06 15:56:22 -0700798 zbva, to, len, page_size, pbl_size, pbl_addr);
Steve Wiseb038ced2007-02-12 16:16:18 -0800799}
800
801int cxio_dereg_mem(struct cxio_rdev *rdev_p, u32 stag, u32 pbl_size,
802 u32 pbl_addr)
803{
Roland Dreier273748c2008-05-06 15:56:22 -0700804 return __cxio_tpt_op(rdev_p, 1, &stag, 0, 0, 0, 0, 0, 0ULL, 0, 0,
805 pbl_size, pbl_addr);
Steve Wiseb038ced2007-02-12 16:16:18 -0800806}
807
808int cxio_allocate_window(struct cxio_rdev *rdev_p, u32 * stag, u32 pdid)
809{
Steve Wiseb038ced2007-02-12 16:16:18 -0800810 *stag = T3_STAG_UNSET;
811 return __cxio_tpt_op(rdev_p, 0, stag, 0, pdid, TPT_MW, 0, 0, 0ULL, 0, 0,
Roland Dreier273748c2008-05-06 15:56:22 -0700812 0, 0);
Steve Wiseb038ced2007-02-12 16:16:18 -0800813}
814
815int cxio_deallocate_window(struct cxio_rdev *rdev_p, u32 stag)
816{
Roland Dreier273748c2008-05-06 15:56:22 -0700817 return __cxio_tpt_op(rdev_p, 1, &stag, 0, 0, 0, 0, 0, 0ULL, 0, 0,
818 0, 0);
Steve Wiseb038ced2007-02-12 16:16:18 -0800819}
820
Steve Wisee7e55822008-07-14 23:48:45 -0700821int cxio_allocate_stag(struct cxio_rdev *rdev_p, u32 *stag, u32 pdid, u32 pbl_size, u32 pbl_addr)
822{
823 *stag = T3_STAG_UNSET;
824 return __cxio_tpt_op(rdev_p, 0, stag, 0, pdid, TPT_NON_SHARED_MR,
825 0, 0, 0ULL, 0, 0, pbl_size, pbl_addr);
826}
827
Steve Wiseb038ced2007-02-12 16:16:18 -0800828int cxio_rdma_init(struct cxio_rdev *rdev_p, struct t3_rdma_init_attr *attr)
829{
830 struct t3_rdma_init_wr *wqe;
831 struct sk_buff *skb = alloc_skb(sizeof(*wqe), GFP_ATOMIC);
832 if (!skb)
833 return -ENOMEM;
Harvey Harrison33718362008-04-16 21:01:10 -0700834 PDBG("%s rdev_p %p\n", __func__, rdev_p);
Steve Wiseb038ced2007-02-12 16:16:18 -0800835 wqe = (struct t3_rdma_init_wr *) __skb_put(skb, sizeof(*wqe));
836 wqe->wrh.op_seop_flags = cpu_to_be32(V_FW_RIWR_OP(T3_WR_INIT));
837 wqe->wrh.gen_tid_len = cpu_to_be32(V_FW_RIWR_TID(attr->tid) |
838 V_FW_RIWR_LEN(sizeof(*wqe) >> 3));
839 wqe->wrid.id1 = 0;
840 wqe->qpid = cpu_to_be32(attr->qpid);
841 wqe->pdid = cpu_to_be32(attr->pdid);
842 wqe->scqid = cpu_to_be32(attr->scqid);
843 wqe->rcqid = cpu_to_be32(attr->rcqid);
844 wqe->rq_addr = cpu_to_be32(attr->rq_addr - rdev_p->rnic_info.rqt_base);
845 wqe->rq_size = cpu_to_be32(attr->rq_size);
846 wqe->mpaattrs = attr->mpaattrs;
847 wqe->qpcaps = attr->qpcaps;
848 wqe->ulpdu_size = cpu_to_be16(attr->tcp_emss);
Steve Wisef8b0dfd2008-04-29 13:46:52 -0700849 wqe->rqe_count = cpu_to_be16(attr->rqe_count);
850 wqe->flags_rtr_type = cpu_to_be16(attr->flags|V_RTR_TYPE(attr->rtr_type));
Steve Wiseb038ced2007-02-12 16:16:18 -0800851 wqe->ord = cpu_to_be32(attr->ord);
852 wqe->ird = cpu_to_be32(attr->ird);
853 wqe->qp_dma_addr = cpu_to_be64(attr->qp_dma_addr);
854 wqe->qp_dma_size = cpu_to_be32(attr->qp_dma_size);
Steve Wisede3d3532007-05-14 13:27:27 -0500855 wqe->irs = cpu_to_be32(attr->irs);
Steve Wiseb038ced2007-02-12 16:16:18 -0800856 skb->priority = 0; /* 0=>ToeQ; 1=>CtrlQ */
857 return (cxgb3_ofld_send(rdev_p->t3cdev_p, skb));
858}
859
860void cxio_register_ev_cb(cxio_hal_ev_callback_func_t ev_cb)
861{
862 cxio_ev_cb = ev_cb;
863}
864
865void cxio_unregister_ev_cb(cxio_hal_ev_callback_func_t ev_cb)
866{
867 cxio_ev_cb = NULL;
868}
869
870static int cxio_hal_ev_handler(struct t3cdev *t3cdev_p, struct sk_buff *skb)
871{
872 static int cnt;
873 struct cxio_rdev *rdev_p = NULL;
874 struct respQ_msg_t *rsp_msg = (struct respQ_msg_t *) skb->data;
875 PDBG("%d: %s cq_id 0x%x cq_ptr 0x%x genbit %0x overflow %0x an %0x"
876 " se %0x notify %0x cqbranch %0x creditth %0x\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700877 cnt, __func__, RSPQ_CQID(rsp_msg), RSPQ_CQPTR(rsp_msg),
Steve Wiseb038ced2007-02-12 16:16:18 -0800878 RSPQ_GENBIT(rsp_msg), RSPQ_OVERFLOW(rsp_msg), RSPQ_AN(rsp_msg),
879 RSPQ_SE(rsp_msg), RSPQ_NOTIFY(rsp_msg), RSPQ_CQBRANCH(rsp_msg),
880 RSPQ_CREDIT_THRESH(rsp_msg));
881 PDBG("CQE: QPID 0x%0x genbit %0x type 0x%0x status 0x%0x opcode %d "
882 "len 0x%0x wrid_hi_stag 0x%x wrid_low_msn 0x%x\n",
883 CQE_QPID(rsp_msg->cqe), CQE_GENBIT(rsp_msg->cqe),
884 CQE_TYPE(rsp_msg->cqe), CQE_STATUS(rsp_msg->cqe),
885 CQE_OPCODE(rsp_msg->cqe), CQE_LEN(rsp_msg->cqe),
886 CQE_WRID_HI(rsp_msg->cqe), CQE_WRID_LOW(rsp_msg->cqe));
887 rdev_p = (struct cxio_rdev *)t3cdev_p->ulp;
888 if (!rdev_p) {
Harvey Harrison33718362008-04-16 21:01:10 -0700889 PDBG("%s called by t3cdev %p with null ulp\n", __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -0800890 t3cdev_p);
891 return 0;
892 }
893 if (CQE_QPID(rsp_msg->cqe) == T3_CTRL_QP_ID) {
894 rdev_p->ctrl_qp.rptr = CQE_WRID_LOW(rsp_msg->cqe) + 1;
895 wake_up_interruptible(&rdev_p->ctrl_qp.waitq);
896 dev_kfree_skb_irq(skb);
897 } else if (CQE_QPID(rsp_msg->cqe) == 0xfff8)
898 dev_kfree_skb_irq(skb);
899 else if (cxio_ev_cb)
900 (*cxio_ev_cb) (rdev_p, skb);
901 else
902 dev_kfree_skb_irq(skb);
903 cnt++;
904 return 0;
905}
906
907/* Caller takes care of locking if needed */
908int cxio_rdev_open(struct cxio_rdev *rdev_p)
909{
910 struct net_device *netdev_p = NULL;
911 int err = 0;
912 if (strlen(rdev_p->dev_name)) {
913 if (cxio_hal_find_rdev_by_name(rdev_p->dev_name)) {
914 return -EBUSY;
915 }
Eric W. Biederman881d9662007-09-17 11:56:21 -0700916 netdev_p = dev_get_by_name(&init_net, rdev_p->dev_name);
Steve Wiseb038ced2007-02-12 16:16:18 -0800917 if (!netdev_p) {
918 return -EINVAL;
919 }
920 dev_put(netdev_p);
921 } else if (rdev_p->t3cdev_p) {
922 if (cxio_hal_find_rdev_by_t3cdev(rdev_p->t3cdev_p)) {
923 return -EBUSY;
924 }
925 netdev_p = rdev_p->t3cdev_p->lldev;
926 strncpy(rdev_p->dev_name, rdev_p->t3cdev_p->name,
927 T3_MAX_DEV_NAME_LEN);
928 } else {
Harvey Harrison33718362008-04-16 21:01:10 -0700929 PDBG("%s t3cdev_p or dev_name must be set\n", __func__);
Steve Wiseb038ced2007-02-12 16:16:18 -0800930 return -EINVAL;
931 }
932
933 list_add_tail(&rdev_p->entry, &rdev_list);
934
Harvey Harrison33718362008-04-16 21:01:10 -0700935 PDBG("%s opening rnic dev %s\n", __func__, rdev_p->dev_name);
Steve Wiseb038ced2007-02-12 16:16:18 -0800936 memset(&rdev_p->ctrl_qp, 0, sizeof(rdev_p->ctrl_qp));
937 if (!rdev_p->t3cdev_p)
Divy Le Ray5fbf8162007-08-29 19:15:47 -0700938 rdev_p->t3cdev_p = dev2t3cdev(netdev_p);
Steve Wiseb038ced2007-02-12 16:16:18 -0800939 rdev_p->t3cdev_p->ulp = (void *) rdev_p;
940 err = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, RDMA_GET_PARAMS,
941 &(rdev_p->rnic_info));
942 if (err) {
943 printk(KERN_ERR "%s t3cdev_p(%p)->ctl returned error %d.\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700944 __func__, rdev_p->t3cdev_p, err);
Steve Wiseb038ced2007-02-12 16:16:18 -0800945 goto err1;
946 }
947 err = rdev_p->t3cdev_p->ctl(rdev_p->t3cdev_p, GET_PORTS,
948 &(rdev_p->port_info));
949 if (err) {
950 printk(KERN_ERR "%s t3cdev_p(%p)->ctl returned error %d.\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700951 __func__, rdev_p->t3cdev_p, err);
Steve Wiseb038ced2007-02-12 16:16:18 -0800952 goto err1;
953 }
954
955 /*
956 * qpshift is the number of bits to shift the qpid left in order
957 * to get the correct address of the doorbell for that qp.
958 */
959 cxio_init_ucontext(rdev_p, &rdev_p->uctx);
960 rdev_p->qpshift = PAGE_SHIFT -
961 ilog2(65536 >>
962 ilog2(rdev_p->rnic_info.udbell_len >>
963 PAGE_SHIFT));
964 rdev_p->qpnr = rdev_p->rnic_info.udbell_len >> PAGE_SHIFT;
965 rdev_p->qpmask = (65536 >> ilog2(rdev_p->qpnr)) - 1;
966 PDBG("%s rnic %s info: tpt_base 0x%0x tpt_top 0x%0x num stags %d "
967 "pbl_base 0x%0x pbl_top 0x%0x rqt_base 0x%0x, rqt_top 0x%0x\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700968 __func__, rdev_p->dev_name, rdev_p->rnic_info.tpt_base,
Steve Wiseb038ced2007-02-12 16:16:18 -0800969 rdev_p->rnic_info.tpt_top, cxio_num_stags(rdev_p),
970 rdev_p->rnic_info.pbl_base,
971 rdev_p->rnic_info.pbl_top, rdev_p->rnic_info.rqt_base,
972 rdev_p->rnic_info.rqt_top);
973 PDBG("udbell_len 0x%0x udbell_physbase 0x%lx kdb_addr %p qpshift %lu "
974 "qpnr %d qpmask 0x%x\n",
975 rdev_p->rnic_info.udbell_len,
976 rdev_p->rnic_info.udbell_physbase, rdev_p->rnic_info.kdb_addr,
977 rdev_p->qpshift, rdev_p->qpnr, rdev_p->qpmask);
978
979 err = cxio_hal_init_ctrl_qp(rdev_p);
980 if (err) {
981 printk(KERN_ERR "%s error %d initializing ctrl_qp.\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700982 __func__, err);
Steve Wiseb038ced2007-02-12 16:16:18 -0800983 goto err1;
984 }
985 err = cxio_hal_init_resource(rdev_p, cxio_num_stags(rdev_p), 0,
986 0, T3_MAX_NUM_QP, T3_MAX_NUM_CQ,
987 T3_MAX_NUM_PD);
988 if (err) {
989 printk(KERN_ERR "%s error %d initializing hal resources.\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700990 __func__, err);
Steve Wiseb038ced2007-02-12 16:16:18 -0800991 goto err2;
992 }
993 err = cxio_hal_pblpool_create(rdev_p);
994 if (err) {
995 printk(KERN_ERR "%s error %d initializing pbl mem pool.\n",
Harvey Harrison33718362008-04-16 21:01:10 -0700996 __func__, err);
Steve Wiseb038ced2007-02-12 16:16:18 -0800997 goto err3;
998 }
999 err = cxio_hal_rqtpool_create(rdev_p);
1000 if (err) {
1001 printk(KERN_ERR "%s error %d initializing rqt mem pool.\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001002 __func__, err);
Steve Wiseb038ced2007-02-12 16:16:18 -08001003 goto err4;
1004 }
1005 return 0;
1006err4:
1007 cxio_hal_pblpool_destroy(rdev_p);
1008err3:
1009 cxio_hal_destroy_resource(rdev_p->rscp);
1010err2:
1011 cxio_hal_destroy_ctrl_qp(rdev_p);
1012err1:
1013 list_del(&rdev_p->entry);
1014 return err;
1015}
1016
1017void cxio_rdev_close(struct cxio_rdev *rdev_p)
1018{
1019 if (rdev_p) {
1020 cxio_hal_pblpool_destroy(rdev_p);
1021 cxio_hal_rqtpool_destroy(rdev_p);
1022 list_del(&rdev_p->entry);
1023 rdev_p->t3cdev_p->ulp = NULL;
1024 cxio_hal_destroy_ctrl_qp(rdev_p);
1025 cxio_hal_destroy_resource(rdev_p->rscp);
1026 }
1027}
1028
1029int __init cxio_hal_init(void)
1030{
1031 if (cxio_hal_init_rhdl_resource(T3_MAX_NUM_RI))
1032 return -ENOMEM;
1033 t3_register_cpl_handler(CPL_ASYNC_NOTIF, cxio_hal_ev_handler);
1034 return 0;
1035}
1036
1037void __exit cxio_hal_exit(void)
1038{
1039 struct cxio_rdev *rdev, *tmp;
1040
1041 t3_register_cpl_handler(CPL_ASYNC_NOTIF, NULL);
1042 list_for_each_entry_safe(rdev, tmp, &rdev_list, entry)
1043 cxio_rdev_close(rdev);
1044 cxio_hal_destroy_rhdl_resource();
1045}
1046
Adrian Bunk2b540352007-02-21 11:52:49 +01001047static void flush_completed_wrs(struct t3_wq *wq, struct t3_cq *cq)
Steve Wiseb038ced2007-02-12 16:16:18 -08001048{
1049 struct t3_swsq *sqp;
1050 __u32 ptr = wq->sq_rptr;
1051 int count = Q_COUNT(wq->sq_rptr, wq->sq_wptr);
1052
1053 sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
1054 while (count--)
1055 if (!sqp->signaled) {
1056 ptr++;
1057 sqp = wq->sq + Q_PTR2IDX(ptr, wq->sq_size_log2);
1058 } else if (sqp->complete) {
1059
1060 /*
1061 * Insert this completed cqe into the swcq.
1062 */
1063 PDBG("%s moving cqe into swcq sq idx %ld cq idx %ld\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001064 __func__, Q_PTR2IDX(ptr, wq->sq_size_log2),
Steve Wiseb038ced2007-02-12 16:16:18 -08001065 Q_PTR2IDX(cq->sw_wptr, cq->size_log2));
1066 sqp->cqe.header |= htonl(V_CQE_SWCQE(1));
1067 *(cq->sw_queue + Q_PTR2IDX(cq->sw_wptr, cq->size_log2))
1068 = sqp->cqe;
1069 cq->sw_wptr++;
1070 sqp->signaled = 0;
1071 break;
1072 } else
1073 break;
1074}
1075
Adrian Bunk2b540352007-02-21 11:52:49 +01001076static void create_read_req_cqe(struct t3_wq *wq, struct t3_cqe *hw_cqe,
1077 struct t3_cqe *read_cqe)
Steve Wiseb038ced2007-02-12 16:16:18 -08001078{
1079 read_cqe->u.scqe.wrid_hi = wq->oldest_read->sq_wptr;
1080 read_cqe->len = wq->oldest_read->read_len;
1081 read_cqe->header = htonl(V_CQE_QPID(CQE_QPID(*hw_cqe)) |
1082 V_CQE_SWCQE(SW_CQE(*hw_cqe)) |
1083 V_CQE_OPCODE(T3_READ_REQ) |
1084 V_CQE_TYPE(1));
1085}
1086
1087/*
1088 * Return a ptr to the next read wr in the SWSQ or NULL.
1089 */
Adrian Bunk2b540352007-02-21 11:52:49 +01001090static void advance_oldest_read(struct t3_wq *wq)
Steve Wiseb038ced2007-02-12 16:16:18 -08001091{
1092
1093 u32 rptr = wq->oldest_read - wq->sq + 1;
1094 u32 wptr = Q_PTR2IDX(wq->sq_wptr, wq->sq_size_log2);
1095
1096 while (Q_PTR2IDX(rptr, wq->sq_size_log2) != wptr) {
1097 wq->oldest_read = wq->sq + Q_PTR2IDX(rptr, wq->sq_size_log2);
1098
1099 if (wq->oldest_read->opcode == T3_READ_REQ)
1100 return;
1101 rptr++;
1102 }
1103 wq->oldest_read = NULL;
1104}
1105
1106/*
1107 * cxio_poll_cq
1108 *
1109 * Caller must:
1110 * check the validity of the first CQE,
1111 * supply the wq assicated with the qpid.
1112 *
1113 * credit: cq credit to return to sge.
1114 * cqe_flushed: 1 iff the CQE is flushed.
1115 * cqe: copy of the polled CQE.
1116 *
1117 * return value:
1118 * 0 CQE returned,
1119 * -1 CQE skipped, try again.
1120 */
1121int cxio_poll_cq(struct t3_wq *wq, struct t3_cq *cq, struct t3_cqe *cqe,
1122 u8 *cqe_flushed, u64 *cookie, u32 *credit)
1123{
1124 int ret = 0;
1125 struct t3_cqe *hw_cqe, read_cqe;
1126
1127 *cqe_flushed = 0;
1128 *credit = 0;
1129 hw_cqe = cxio_next_cqe(cq);
1130
1131 PDBG("%s CQE OOO %d qpid 0x%0x genbit %d type %d status 0x%0x"
1132 " opcode 0x%0x len 0x%0x wrid_hi_stag 0x%x wrid_low_msn 0x%x\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001133 __func__, CQE_OOO(*hw_cqe), CQE_QPID(*hw_cqe),
Steve Wiseb038ced2007-02-12 16:16:18 -08001134 CQE_GENBIT(*hw_cqe), CQE_TYPE(*hw_cqe), CQE_STATUS(*hw_cqe),
1135 CQE_OPCODE(*hw_cqe), CQE_LEN(*hw_cqe), CQE_WRID_HI(*hw_cqe),
1136 CQE_WRID_LOW(*hw_cqe));
1137
1138 /*
1139 * skip cqe's not affiliated with a QP.
1140 */
1141 if (wq == NULL) {
1142 ret = -1;
1143 goto skip_cqe;
1144 }
1145
1146 /*
1147 * Gotta tweak READ completions:
1148 * 1) the cqe doesn't contain the sq_wptr from the wr.
1149 * 2) opcode not reflected from the wr.
1150 * 3) read_len not reflected from the wr.
1151 * 4) cq_type is RQ_TYPE not SQ_TYPE.
1152 */
1153 if (RQ_TYPE(*hw_cqe) && (CQE_OPCODE(*hw_cqe) == T3_READ_RESP)) {
1154
1155 /*
Steve Wisef8b0dfd2008-04-29 13:46:52 -07001156 * If this is an unsolicited read response, then the read
1157 * was generated by the kernel driver as part of peer-2-peer
1158 * connection setup. So ignore the completion.
1159 */
1160 if (!wq->oldest_read) {
1161 if (CQE_STATUS(*hw_cqe))
1162 wq->error = 1;
1163 ret = -1;
1164 goto skip_cqe;
1165 }
1166
1167 /*
Steve Wiseb038ced2007-02-12 16:16:18 -08001168 * Don't write to the HWCQ, so create a new read req CQE
1169 * in local memory.
1170 */
1171 create_read_req_cqe(wq, hw_cqe, &read_cqe);
1172 hw_cqe = &read_cqe;
1173 advance_oldest_read(wq);
1174 }
1175
1176 /*
1177 * T3A: Discard TERMINATE CQEs.
1178 */
1179 if (CQE_OPCODE(*hw_cqe) == T3_TERMINATE) {
1180 ret = -1;
1181 wq->error = 1;
1182 goto skip_cqe;
1183 }
1184
1185 if (CQE_STATUS(*hw_cqe) || wq->error) {
1186 *cqe_flushed = wq->error;
1187 wq->error = 1;
1188
1189 /*
1190 * T3A inserts errors into the CQE. We cannot return
1191 * these as work completions.
1192 */
1193 /* incoming write failures */
1194 if ((CQE_OPCODE(*hw_cqe) == T3_RDMA_WRITE)
1195 && RQ_TYPE(*hw_cqe)) {
1196 ret = -1;
1197 goto skip_cqe;
1198 }
1199 /* incoming read request failures */
1200 if ((CQE_OPCODE(*hw_cqe) == T3_READ_RESP) && SQ_TYPE(*hw_cqe)) {
1201 ret = -1;
1202 goto skip_cqe;
1203 }
1204
1205 /* incoming SEND with no receive posted failures */
1206 if ((CQE_OPCODE(*hw_cqe) == T3_SEND) && RQ_TYPE(*hw_cqe) &&
1207 Q_EMPTY(wq->rq_rptr, wq->rq_wptr)) {
1208 ret = -1;
1209 goto skip_cqe;
1210 }
1211 goto proc_cqe;
1212 }
1213
1214 /*
1215 * RECV completion.
1216 */
1217 if (RQ_TYPE(*hw_cqe)) {
1218
1219 /*
1220 * HW only validates 4 bits of MSN. So we must validate that
1221 * the MSN in the SEND is the next expected MSN. If its not,
1222 * then we complete this with TPT_ERR_MSN and mark the wq in
1223 * error.
1224 */
1225 if (unlikely((CQE_WRID_MSN(*hw_cqe) != (wq->rq_rptr + 1)))) {
1226 wq->error = 1;
1227 hw_cqe->header |= htonl(V_CQE_STATUS(TPT_ERR_MSN));
1228 goto proc_cqe;
1229 }
1230 goto proc_cqe;
1231 }
1232
1233 /*
1234 * If we get here its a send completion.
1235 *
1236 * Handle out of order completion. These get stuffed
1237 * in the SW SQ. Then the SW SQ is walked to move any
1238 * now in-order completions into the SW CQ. This handles
1239 * 2 cases:
1240 * 1) reaping unsignaled WRs when the first subsequent
1241 * signaled WR is completed.
1242 * 2) out of order read completions.
1243 */
1244 if (!SW_CQE(*hw_cqe) && (CQE_WRID_SQ_WPTR(*hw_cqe) != wq->sq_rptr)) {
1245 struct t3_swsq *sqp;
1246
1247 PDBG("%s out of order completion going in swsq at idx %ld\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001248 __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -08001249 Q_PTR2IDX(CQE_WRID_SQ_WPTR(*hw_cqe), wq->sq_size_log2));
1250 sqp = wq->sq +
1251 Q_PTR2IDX(CQE_WRID_SQ_WPTR(*hw_cqe), wq->sq_size_log2);
1252 sqp->cqe = *hw_cqe;
1253 sqp->complete = 1;
1254 ret = -1;
1255 goto flush_wq;
1256 }
1257
1258proc_cqe:
1259 *cqe = *hw_cqe;
1260
1261 /*
1262 * Reap the associated WR(s) that are freed up with this
1263 * completion.
1264 */
1265 if (SQ_TYPE(*hw_cqe)) {
1266 wq->sq_rptr = CQE_WRID_SQ_WPTR(*hw_cqe);
Harvey Harrison33718362008-04-16 21:01:10 -07001267 PDBG("%s completing sq idx %ld\n", __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -08001268 Q_PTR2IDX(wq->sq_rptr, wq->sq_size_log2));
1269 *cookie = (wq->sq +
1270 Q_PTR2IDX(wq->sq_rptr, wq->sq_size_log2))->wr_id;
1271 wq->sq_rptr++;
1272 } else {
Harvey Harrison33718362008-04-16 21:01:10 -07001273 PDBG("%s completing rq idx %ld\n", __func__,
Steve Wiseb038ced2007-02-12 16:16:18 -08001274 Q_PTR2IDX(wq->rq_rptr, wq->rq_size_log2));
1275 *cookie = *(wq->rq + Q_PTR2IDX(wq->rq_rptr, wq->rq_size_log2));
1276 wq->rq_rptr++;
1277 }
1278
1279flush_wq:
1280 /*
1281 * Flush any completed cqes that are now in-order.
1282 */
1283 flush_completed_wrs(wq, cq);
1284
1285skip_cqe:
1286 if (SW_CQE(*hw_cqe)) {
1287 PDBG("%s cq %p cqid 0x%x skip sw cqe sw_rptr 0x%x\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001288 __func__, cq, cq->cqid, cq->sw_rptr);
Steve Wiseb038ced2007-02-12 16:16:18 -08001289 ++cq->sw_rptr;
1290 } else {
1291 PDBG("%s cq %p cqid 0x%x skip hw cqe rptr 0x%x\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001292 __func__, cq, cq->cqid, cq->rptr);
Steve Wiseb038ced2007-02-12 16:16:18 -08001293 ++cq->rptr;
1294
1295 /*
1296 * T3A: compute credits.
1297 */
1298 if (((cq->rptr - cq->wptr) > (1 << (cq->size_log2 - 1)))
1299 || ((cq->rptr - cq->wptr) >= 128)) {
1300 *credit = cq->rptr - cq->wptr;
1301 cq->wptr = cq->rptr;
1302 }
1303 }
1304 return ret;
1305}