2 * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/debugfs.h>
35 #include <linux/vmalloc.h>
37 #include <rdma/ib_verbs.h>
41 #define DRV_VERSION "0.1"
43 MODULE_AUTHOR("Steve Wise");
44 MODULE_DESCRIPTION("Chelsio T4/T5 RDMA Driver");
45 MODULE_LICENSE("Dual BSD/GPL");
46 MODULE_VERSION(DRV_VERSION
);
48 static int allow_db_fc_on_t5
;
49 module_param(allow_db_fc_on_t5
, int, 0644);
50 MODULE_PARM_DESC(allow_db_fc_on_t5
,
51 "Allow DB Flow Control on T5 (default = 0)");
53 static int allow_db_coalescing_on_t5
;
54 module_param(allow_db_coalescing_on_t5
, int, 0644);
55 MODULE_PARM_DESC(allow_db_coalescing_on_t5
,
56 "Allow DB Coalescing on T5 (default = 0)");
59 struct list_head entry
;
60 struct cxgb4_lld_info lldi
;
64 static LIST_HEAD(uld_ctx_list
);
65 static DEFINE_MUTEX(dev_mutex
);
67 static struct dentry
*c4iw_debugfs_root
;
69 struct c4iw_debugfs_data
{
70 struct c4iw_dev
*devp
;
76 static int count_idrs(int id
, void *p
, void *data
)
80 *countp
= *countp
+ 1;
84 static ssize_t
debugfs_read(struct file
*file
, char __user
*buf
, size_t count
,
87 struct c4iw_debugfs_data
*d
= file
->private_data
;
89 return simple_read_from_buffer(buf
, count
, ppos
, d
->buf
, d
->pos
);
92 static int dump_qp(int id
, void *p
, void *data
)
94 struct c4iw_qp
*qp
= p
;
95 struct c4iw_debugfs_data
*qpd
= data
;
99 if (id
!= qp
->wq
.sq
.qid
)
102 space
= qpd
->bufsize
- qpd
->pos
- 1;
107 cc
= snprintf(qpd
->buf
+ qpd
->pos
, space
,
108 "qp sq id %u rq id %u state %u onchip %u "
109 "ep tid %u state %u %pI4:%u->%pI4:%u\n",
110 qp
->wq
.sq
.qid
, qp
->wq
.rq
.qid
, (int)qp
->attr
.state
,
111 qp
->wq
.sq
.flags
& T4_SQ_ONCHIP
,
112 qp
->ep
->hwtid
, (int)qp
->ep
->com
.state
,
113 &qp
->ep
->com
.local_addr
.sin_addr
.s_addr
,
114 ntohs(qp
->ep
->com
.local_addr
.sin_port
),
115 &qp
->ep
->com
.remote_addr
.sin_addr
.s_addr
,
116 ntohs(qp
->ep
->com
.remote_addr
.sin_port
));
118 cc
= snprintf(qpd
->buf
+ qpd
->pos
, space
,
119 "qp sq id %u rq id %u state %u onchip %u\n",
120 qp
->wq
.sq
.qid
, qp
->wq
.rq
.qid
,
122 qp
->wq
.sq
.flags
& T4_SQ_ONCHIP
);
128 static int qp_release(struct inode
*inode
, struct file
*file
)
130 struct c4iw_debugfs_data
*qpd
= file
->private_data
;
132 printk(KERN_INFO
"%s null qpd?\n", __func__
);
140 static int qp_open(struct inode
*inode
, struct file
*file
)
142 struct c4iw_debugfs_data
*qpd
;
146 qpd
= kmalloc(sizeof *qpd
, GFP_KERNEL
);
151 qpd
->devp
= inode
->i_private
;
154 spin_lock_irq(&qpd
->devp
->lock
);
155 idr_for_each(&qpd
->devp
->qpidr
, count_idrs
, &count
);
156 spin_unlock_irq(&qpd
->devp
->lock
);
158 qpd
->bufsize
= count
* 128;
159 qpd
->buf
= vmalloc(qpd
->bufsize
);
165 spin_lock_irq(&qpd
->devp
->lock
);
166 idr_for_each(&qpd
->devp
->qpidr
, dump_qp
, qpd
);
167 spin_unlock_irq(&qpd
->devp
->lock
);
169 qpd
->buf
[qpd
->pos
++] = 0;
170 file
->private_data
= qpd
;
178 static const struct file_operations qp_debugfs_fops
= {
179 .owner
= THIS_MODULE
,
181 .release
= qp_release
,
182 .read
= debugfs_read
,
183 .llseek
= default_llseek
,
186 static int dump_stag(int id
, void *p
, void *data
)
188 struct c4iw_debugfs_data
*stagd
= data
;
192 space
= stagd
->bufsize
- stagd
->pos
- 1;
196 cc
= snprintf(stagd
->buf
+ stagd
->pos
, space
, "0x%x\n", id
<<8);
202 static int stag_release(struct inode
*inode
, struct file
*file
)
204 struct c4iw_debugfs_data
*stagd
= file
->private_data
;
206 printk(KERN_INFO
"%s null stagd?\n", __func__
);
214 static int stag_open(struct inode
*inode
, struct file
*file
)
216 struct c4iw_debugfs_data
*stagd
;
220 stagd
= kmalloc(sizeof *stagd
, GFP_KERNEL
);
225 stagd
->devp
= inode
->i_private
;
228 spin_lock_irq(&stagd
->devp
->lock
);
229 idr_for_each(&stagd
->devp
->mmidr
, count_idrs
, &count
);
230 spin_unlock_irq(&stagd
->devp
->lock
);
232 stagd
->bufsize
= count
* sizeof("0x12345678\n");
233 stagd
->buf
= kmalloc(stagd
->bufsize
, GFP_KERNEL
);
239 spin_lock_irq(&stagd
->devp
->lock
);
240 idr_for_each(&stagd
->devp
->mmidr
, dump_stag
, stagd
);
241 spin_unlock_irq(&stagd
->devp
->lock
);
243 stagd
->buf
[stagd
->pos
++] = 0;
244 file
->private_data
= stagd
;
252 static const struct file_operations stag_debugfs_fops
= {
253 .owner
= THIS_MODULE
,
255 .release
= stag_release
,
256 .read
= debugfs_read
,
257 .llseek
= default_llseek
,
260 static char *db_state_str
[] = {"NORMAL", "FLOW_CONTROL", "RECOVERY"};
262 static int stats_show(struct seq_file
*seq
, void *v
)
264 struct c4iw_dev
*dev
= seq
->private;
266 seq_printf(seq
, " Object: %10s %10s %10s %10s\n", "Total", "Current",
268 seq_printf(seq
, " PDID: %10llu %10llu %10llu %10llu\n",
269 dev
->rdev
.stats
.pd
.total
, dev
->rdev
.stats
.pd
.cur
,
270 dev
->rdev
.stats
.pd
.max
, dev
->rdev
.stats
.pd
.fail
);
271 seq_printf(seq
, " QID: %10llu %10llu %10llu %10llu\n",
272 dev
->rdev
.stats
.qid
.total
, dev
->rdev
.stats
.qid
.cur
,
273 dev
->rdev
.stats
.qid
.max
, dev
->rdev
.stats
.qid
.fail
);
274 seq_printf(seq
, " TPTMEM: %10llu %10llu %10llu %10llu\n",
275 dev
->rdev
.stats
.stag
.total
, dev
->rdev
.stats
.stag
.cur
,
276 dev
->rdev
.stats
.stag
.max
, dev
->rdev
.stats
.stag
.fail
);
277 seq_printf(seq
, " PBLMEM: %10llu %10llu %10llu %10llu\n",
278 dev
->rdev
.stats
.pbl
.total
, dev
->rdev
.stats
.pbl
.cur
,
279 dev
->rdev
.stats
.pbl
.max
, dev
->rdev
.stats
.pbl
.fail
);
280 seq_printf(seq
, " RQTMEM: %10llu %10llu %10llu %10llu\n",
281 dev
->rdev
.stats
.rqt
.total
, dev
->rdev
.stats
.rqt
.cur
,
282 dev
->rdev
.stats
.rqt
.max
, dev
->rdev
.stats
.rqt
.fail
);
283 seq_printf(seq
, " OCQPMEM: %10llu %10llu %10llu %10llu\n",
284 dev
->rdev
.stats
.ocqp
.total
, dev
->rdev
.stats
.ocqp
.cur
,
285 dev
->rdev
.stats
.ocqp
.max
, dev
->rdev
.stats
.ocqp
.fail
);
286 seq_printf(seq
, " DB FULL: %10llu\n", dev
->rdev
.stats
.db_full
);
287 seq_printf(seq
, " DB EMPTY: %10llu\n", dev
->rdev
.stats
.db_empty
);
288 seq_printf(seq
, " DB DROP: %10llu\n", dev
->rdev
.stats
.db_drop
);
289 seq_printf(seq
, " DB State: %s Transitions %llu\n",
290 db_state_str
[dev
->db_state
],
291 dev
->rdev
.stats
.db_state_transitions
);
292 seq_printf(seq
, "TCAM_FULL: %10llu\n", dev
->rdev
.stats
.tcam_full
);
293 seq_printf(seq
, "ACT_OFLD_CONN_FAILS: %10llu\n",
294 dev
->rdev
.stats
.act_ofld_conn_fails
);
295 seq_printf(seq
, "PAS_OFLD_CONN_FAILS: %10llu\n",
296 dev
->rdev
.stats
.pas_ofld_conn_fails
);
300 static int stats_open(struct inode
*inode
, struct file
*file
)
302 return single_open(file
, stats_show
, inode
->i_private
);
305 static ssize_t
stats_clear(struct file
*file
, const char __user
*buf
,
306 size_t count
, loff_t
*pos
)
308 struct c4iw_dev
*dev
= ((struct seq_file
*)file
->private_data
)->private;
310 mutex_lock(&dev
->rdev
.stats
.lock
);
311 dev
->rdev
.stats
.pd
.max
= 0;
312 dev
->rdev
.stats
.pd
.fail
= 0;
313 dev
->rdev
.stats
.qid
.max
= 0;
314 dev
->rdev
.stats
.qid
.fail
= 0;
315 dev
->rdev
.stats
.stag
.max
= 0;
316 dev
->rdev
.stats
.stag
.fail
= 0;
317 dev
->rdev
.stats
.pbl
.max
= 0;
318 dev
->rdev
.stats
.pbl
.fail
= 0;
319 dev
->rdev
.stats
.rqt
.max
= 0;
320 dev
->rdev
.stats
.rqt
.fail
= 0;
321 dev
->rdev
.stats
.ocqp
.max
= 0;
322 dev
->rdev
.stats
.ocqp
.fail
= 0;
323 dev
->rdev
.stats
.db_full
= 0;
324 dev
->rdev
.stats
.db_empty
= 0;
325 dev
->rdev
.stats
.db_drop
= 0;
326 dev
->rdev
.stats
.db_state_transitions
= 0;
327 dev
->rdev
.stats
.tcam_full
= 0;
328 dev
->rdev
.stats
.act_ofld_conn_fails
= 0;
329 dev
->rdev
.stats
.pas_ofld_conn_fails
= 0;
330 mutex_unlock(&dev
->rdev
.stats
.lock
);
334 static const struct file_operations stats_debugfs_fops
= {
335 .owner
= THIS_MODULE
,
337 .release
= single_release
,
340 .write
= stats_clear
,
343 static int dump_ep(int id
, void *p
, void *data
)
345 struct c4iw_ep
*ep
= p
;
346 struct c4iw_debugfs_data
*epd
= data
;
350 space
= epd
->bufsize
- epd
->pos
- 1;
354 cc
= snprintf(epd
->buf
+ epd
->pos
, space
,
355 "ep %p cm_id %p qp %p state %d flags 0x%lx history 0x%lx "
356 "hwtid %d atid %d %pI4:%d <-> %pI4:%d\n",
357 ep
, ep
->com
.cm_id
, ep
->com
.qp
, (int)ep
->com
.state
,
358 ep
->com
.flags
, ep
->com
.history
, ep
->hwtid
, ep
->atid
,
359 &ep
->com
.local_addr
.sin_addr
.s_addr
,
360 ntohs(ep
->com
.local_addr
.sin_port
),
361 &ep
->com
.remote_addr
.sin_addr
.s_addr
,
362 ntohs(ep
->com
.remote_addr
.sin_port
));
368 static int dump_listen_ep(int id
, void *p
, void *data
)
370 struct c4iw_listen_ep
*ep
= p
;
371 struct c4iw_debugfs_data
*epd
= data
;
375 space
= epd
->bufsize
- epd
->pos
- 1;
379 cc
= snprintf(epd
->buf
+ epd
->pos
, space
,
380 "ep %p cm_id %p state %d flags 0x%lx stid %d backlog %d "
381 "%pI4:%d\n", ep
, ep
->com
.cm_id
, (int)ep
->com
.state
,
382 ep
->com
.flags
, ep
->stid
, ep
->backlog
,
383 &ep
->com
.local_addr
.sin_addr
.s_addr
,
384 ntohs(ep
->com
.local_addr
.sin_port
));
390 static int ep_release(struct inode
*inode
, struct file
*file
)
392 struct c4iw_debugfs_data
*epd
= file
->private_data
;
394 pr_info("%s null qpd?\n", __func__
);
402 static int ep_open(struct inode
*inode
, struct file
*file
)
404 struct c4iw_debugfs_data
*epd
;
408 epd
= kmalloc(sizeof(*epd
), GFP_KERNEL
);
413 epd
->devp
= inode
->i_private
;
416 spin_lock_irq(&epd
->devp
->lock
);
417 idr_for_each(&epd
->devp
->hwtid_idr
, count_idrs
, &count
);
418 idr_for_each(&epd
->devp
->atid_idr
, count_idrs
, &count
);
419 idr_for_each(&epd
->devp
->stid_idr
, count_idrs
, &count
);
420 spin_unlock_irq(&epd
->devp
->lock
);
422 epd
->bufsize
= count
* 160;
423 epd
->buf
= vmalloc(epd
->bufsize
);
429 spin_lock_irq(&epd
->devp
->lock
);
430 idr_for_each(&epd
->devp
->hwtid_idr
, dump_ep
, epd
);
431 idr_for_each(&epd
->devp
->atid_idr
, dump_ep
, epd
);
432 idr_for_each(&epd
->devp
->stid_idr
, dump_listen_ep
, epd
);
433 spin_unlock_irq(&epd
->devp
->lock
);
435 file
->private_data
= epd
;
443 static const struct file_operations ep_debugfs_fops
= {
444 .owner
= THIS_MODULE
,
446 .release
= ep_release
,
447 .read
= debugfs_read
,
450 static int setup_debugfs(struct c4iw_dev
*devp
)
454 if (!devp
->debugfs_root
)
457 de
= debugfs_create_file("qps", S_IWUSR
, devp
->debugfs_root
,
458 (void *)devp
, &qp_debugfs_fops
);
459 if (de
&& de
->d_inode
)
460 de
->d_inode
->i_size
= 4096;
462 de
= debugfs_create_file("stags", S_IWUSR
, devp
->debugfs_root
,
463 (void *)devp
, &stag_debugfs_fops
);
464 if (de
&& de
->d_inode
)
465 de
->d_inode
->i_size
= 4096;
467 de
= debugfs_create_file("stats", S_IWUSR
, devp
->debugfs_root
,
468 (void *)devp
, &stats_debugfs_fops
);
469 if (de
&& de
->d_inode
)
470 de
->d_inode
->i_size
= 4096;
472 de
= debugfs_create_file("eps", S_IWUSR
, devp
->debugfs_root
,
473 (void *)devp
, &ep_debugfs_fops
);
474 if (de
&& de
->d_inode
)
475 de
->d_inode
->i_size
= 4096;
480 void c4iw_release_dev_ucontext(struct c4iw_rdev
*rdev
,
481 struct c4iw_dev_ucontext
*uctx
)
483 struct list_head
*pos
, *nxt
;
484 struct c4iw_qid_list
*entry
;
486 mutex_lock(&uctx
->lock
);
487 list_for_each_safe(pos
, nxt
, &uctx
->qpids
) {
488 entry
= list_entry(pos
, struct c4iw_qid_list
, entry
);
489 list_del_init(&entry
->entry
);
490 if (!(entry
->qid
& rdev
->qpmask
)) {
491 c4iw_put_resource(&rdev
->resource
.qid_table
,
493 mutex_lock(&rdev
->stats
.lock
);
494 rdev
->stats
.qid
.cur
-= rdev
->qpmask
+ 1;
495 mutex_unlock(&rdev
->stats
.lock
);
500 list_for_each_safe(pos
, nxt
, &uctx
->qpids
) {
501 entry
= list_entry(pos
, struct c4iw_qid_list
, entry
);
502 list_del_init(&entry
->entry
);
505 mutex_unlock(&uctx
->lock
);
508 void c4iw_init_dev_ucontext(struct c4iw_rdev
*rdev
,
509 struct c4iw_dev_ucontext
*uctx
)
511 INIT_LIST_HEAD(&uctx
->qpids
);
512 INIT_LIST_HEAD(&uctx
->cqids
);
513 mutex_init(&uctx
->lock
);
516 /* Caller takes care of locking if needed */
517 static int c4iw_rdev_open(struct c4iw_rdev
*rdev
)
521 c4iw_init_dev_ucontext(rdev
, &rdev
->uctx
);
524 * qpshift is the number of bits to shift the qpid left in order
525 * to get the correct address of the doorbell for that qp.
527 rdev
->qpshift
= PAGE_SHIFT
- ilog2(rdev
->lldi
.udb_density
);
528 rdev
->qpmask
= rdev
->lldi
.udb_density
- 1;
529 rdev
->cqshift
= PAGE_SHIFT
- ilog2(rdev
->lldi
.ucq_density
);
530 rdev
->cqmask
= rdev
->lldi
.ucq_density
- 1;
531 PDBG("%s dev %s stag start 0x%0x size 0x%0x num stags %d "
532 "pbl start 0x%0x size 0x%0x rq start 0x%0x size 0x%0x "
533 "qp qid start %u size %u cq qid start %u size %u\n",
534 __func__
, pci_name(rdev
->lldi
.pdev
), rdev
->lldi
.vr
->stag
.start
,
535 rdev
->lldi
.vr
->stag
.size
, c4iw_num_stags(rdev
),
536 rdev
->lldi
.vr
->pbl
.start
,
537 rdev
->lldi
.vr
->pbl
.size
, rdev
->lldi
.vr
->rq
.start
,
538 rdev
->lldi
.vr
->rq
.size
,
539 rdev
->lldi
.vr
->qp
.start
,
540 rdev
->lldi
.vr
->qp
.size
,
541 rdev
->lldi
.vr
->cq
.start
,
542 rdev
->lldi
.vr
->cq
.size
);
543 PDBG("udb len 0x%x udb base %p db_reg %p gts_reg %p qpshift %lu "
544 "qpmask 0x%x cqshift %lu cqmask 0x%x\n",
545 (unsigned)pci_resource_len(rdev
->lldi
.pdev
, 2),
546 (void *)(unsigned long)pci_resource_start(rdev
->lldi
.pdev
, 2),
549 rdev
->qpshift
, rdev
->qpmask
,
550 rdev
->cqshift
, rdev
->cqmask
);
552 if (c4iw_num_stags(rdev
) == 0) {
557 rdev
->stats
.pd
.total
= T4_MAX_NUM_PD
;
558 rdev
->stats
.stag
.total
= rdev
->lldi
.vr
->stag
.size
;
559 rdev
->stats
.pbl
.total
= rdev
->lldi
.vr
->pbl
.size
;
560 rdev
->stats
.rqt
.total
= rdev
->lldi
.vr
->rq
.size
;
561 rdev
->stats
.ocqp
.total
= rdev
->lldi
.vr
->ocq
.size
;
562 rdev
->stats
.qid
.total
= rdev
->lldi
.vr
->qp
.size
;
564 err
= c4iw_init_resource(rdev
, c4iw_num_stags(rdev
), T4_MAX_NUM_PD
);
566 printk(KERN_ERR MOD
"error %d initializing resources\n", err
);
569 err
= c4iw_pblpool_create(rdev
);
571 printk(KERN_ERR MOD
"error %d initializing pbl pool\n", err
);
574 err
= c4iw_rqtpool_create(rdev
);
576 printk(KERN_ERR MOD
"error %d initializing rqt pool\n", err
);
579 err
= c4iw_ocqp_pool_create(rdev
);
581 printk(KERN_ERR MOD
"error %d initializing ocqp pool\n", err
);
586 c4iw_rqtpool_destroy(rdev
);
588 c4iw_pblpool_destroy(rdev
);
590 c4iw_destroy_resource(&rdev
->resource
);
595 static void c4iw_rdev_close(struct c4iw_rdev
*rdev
)
597 c4iw_pblpool_destroy(rdev
);
598 c4iw_rqtpool_destroy(rdev
);
599 c4iw_destroy_resource(&rdev
->resource
);
602 static void c4iw_dealloc(struct uld_ctx
*ctx
)
604 c4iw_rdev_close(&ctx
->dev
->rdev
);
605 idr_destroy(&ctx
->dev
->cqidr
);
606 idr_destroy(&ctx
->dev
->qpidr
);
607 idr_destroy(&ctx
->dev
->mmidr
);
608 idr_destroy(&ctx
->dev
->hwtid_idr
);
609 idr_destroy(&ctx
->dev
->stid_idr
);
610 idr_destroy(&ctx
->dev
->atid_idr
);
611 iounmap(ctx
->dev
->rdev
.oc_mw_kva
);
612 ib_dealloc_device(&ctx
->dev
->ibdev
);
616 static void c4iw_remove(struct uld_ctx
*ctx
)
618 PDBG("%s c4iw_dev %p\n", __func__
, ctx
->dev
);
619 c4iw_unregister_device(ctx
->dev
);
623 static int rdma_supported(const struct cxgb4_lld_info
*infop
)
625 return infop
->vr
->stag
.size
> 0 && infop
->vr
->pbl
.size
> 0 &&
626 infop
->vr
->rq
.size
> 0 && infop
->vr
->qp
.size
> 0 &&
627 infop
->vr
->cq
.size
> 0;
630 static struct c4iw_dev
*c4iw_alloc(const struct cxgb4_lld_info
*infop
)
632 struct c4iw_dev
*devp
;
635 if (!rdma_supported(infop
)) {
636 printk(KERN_INFO MOD
"%s: RDMA not supported on this device.\n",
637 pci_name(infop
->pdev
));
638 return ERR_PTR(-ENOSYS
);
640 if (!ocqp_supported(infop
))
641 pr_info("%s: On-Chip Queues not supported on this device.\n",
642 pci_name(infop
->pdev
));
644 if (!is_t4(infop
->adapter_type
)) {
645 if (!allow_db_fc_on_t5
) {
646 db_fc_threshold
= 100000;
647 pr_info("DB Flow Control Disabled.\n");
650 if (!allow_db_coalescing_on_t5
) {
651 db_coalescing_threshold
= -1;
652 pr_info("DB Coalescing Disabled.\n");
656 devp
= (struct c4iw_dev
*)ib_alloc_device(sizeof(*devp
));
658 printk(KERN_ERR MOD
"Cannot allocate ib device\n");
659 return ERR_PTR(-ENOMEM
);
661 devp
->rdev
.lldi
= *infop
;
663 devp
->rdev
.oc_mw_pa
= pci_resource_start(devp
->rdev
.lldi
.pdev
, 2) +
664 (pci_resource_len(devp
->rdev
.lldi
.pdev
, 2) -
665 roundup_pow_of_two(devp
->rdev
.lldi
.vr
->ocq
.size
));
666 devp
->rdev
.oc_mw_kva
= ioremap_wc(devp
->rdev
.oc_mw_pa
,
667 devp
->rdev
.lldi
.vr
->ocq
.size
);
669 PDBG(KERN_INFO MOD
"ocq memory: "
670 "hw_start 0x%x size %u mw_pa 0x%lx mw_kva %p\n",
671 devp
->rdev
.lldi
.vr
->ocq
.start
, devp
->rdev
.lldi
.vr
->ocq
.size
,
672 devp
->rdev
.oc_mw_pa
, devp
->rdev
.oc_mw_kva
);
674 ret
= c4iw_rdev_open(&devp
->rdev
);
676 printk(KERN_ERR MOD
"Unable to open CXIO rdev err %d\n", ret
);
677 ib_dealloc_device(&devp
->ibdev
);
681 idr_init(&devp
->cqidr
);
682 idr_init(&devp
->qpidr
);
683 idr_init(&devp
->mmidr
);
684 idr_init(&devp
->hwtid_idr
);
685 idr_init(&devp
->stid_idr
);
686 idr_init(&devp
->atid_idr
);
687 spin_lock_init(&devp
->lock
);
688 mutex_init(&devp
->rdev
.stats
.lock
);
689 mutex_init(&devp
->db_mutex
);
691 if (c4iw_debugfs_root
) {
692 devp
->debugfs_root
= debugfs_create_dir(
693 pci_name(devp
->rdev
.lldi
.pdev
),
700 static void *c4iw_uld_add(const struct cxgb4_lld_info
*infop
)
703 static int vers_printed
;
707 pr_info("Chelsio T4/T5 RDMA Driver - version %s\n",
710 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
712 ctx
= ERR_PTR(-ENOMEM
);
717 PDBG("%s found device %s nchan %u nrxq %u ntxq %u nports %u\n",
718 __func__
, pci_name(ctx
->lldi
.pdev
),
719 ctx
->lldi
.nchan
, ctx
->lldi
.nrxq
,
720 ctx
->lldi
.ntxq
, ctx
->lldi
.nports
);
722 mutex_lock(&dev_mutex
);
723 list_add_tail(&ctx
->entry
, &uld_ctx_list
);
724 mutex_unlock(&dev_mutex
);
726 for (i
= 0; i
< ctx
->lldi
.nrxq
; i
++)
727 PDBG("rxqid[%u] %u\n", i
, ctx
->lldi
.rxq_ids
[i
]);
732 static inline struct sk_buff
*copy_gl_to_skb_pkt(const struct pkt_gl
*gl
,
739 * Allocate space for cpl_pass_accept_req which will be synthesized by
740 * driver. Once the driver synthesizes the request the skb will go
741 * through the regular cpl_pass_accept_req processing.
742 * The math here assumes sizeof cpl_pass_accept_req >= sizeof
745 skb
= alloc_skb(gl
->tot_len
+ sizeof(struct cpl_pass_accept_req
) +
746 sizeof(struct rss_header
) - pktshift
, GFP_ATOMIC
);
750 __skb_put(skb
, gl
->tot_len
+ sizeof(struct cpl_pass_accept_req
) +
751 sizeof(struct rss_header
) - pktshift
);
754 * This skb will contain:
755 * rss_header from the rspq descriptor (1 flit)
756 * cpl_rx_pkt struct from the rspq descriptor (2 flits)
757 * space for the difference between the size of an
758 * rx_pkt and pass_accept_req cpl (1 flit)
759 * the packet data from the gl
761 skb_copy_to_linear_data(skb
, rsp
, sizeof(struct cpl_pass_accept_req
) +
762 sizeof(struct rss_header
));
763 skb_copy_to_linear_data_offset(skb
, sizeof(struct rss_header
) +
764 sizeof(struct cpl_pass_accept_req
),
766 gl
->tot_len
- pktshift
);
770 static inline int recv_rx_pkt(struct c4iw_dev
*dev
, const struct pkt_gl
*gl
,
773 unsigned int opcode
= *(u8
*)rsp
;
776 if (opcode
!= CPL_RX_PKT
)
779 skb
= copy_gl_to_skb_pkt(gl
, rsp
, dev
->rdev
.lldi
.sge_pktshift
);
783 if (c4iw_handlers
[opcode
] == NULL
) {
784 pr_info("%s no handler opcode 0x%x...\n", __func__
,
789 c4iw_handlers
[opcode
](dev
, skb
);
795 static int c4iw_uld_rx_handler(void *handle
, const __be64
*rsp
,
796 const struct pkt_gl
*gl
)
798 struct uld_ctx
*ctx
= handle
;
799 struct c4iw_dev
*dev
= ctx
->dev
;
804 /* omit RSS and rsp_ctrl at end of descriptor */
805 unsigned int len
= 64 - sizeof(struct rsp_ctrl
) - 8;
807 skb
= alloc_skb(256, GFP_ATOMIC
);
811 skb_copy_to_linear_data(skb
, &rsp
[1], len
);
812 } else if (gl
== CXGB4_MSG_AN
) {
813 const struct rsp_ctrl
*rc
= (void *)rsp
;
815 u32 qid
= be32_to_cpu(rc
->pldbuflen_qid
);
816 c4iw_ev_handler(dev
, qid
);
818 } else if (unlikely(*(u8
*)rsp
!= *(u8
*)gl
->va
)) {
819 if (recv_rx_pkt(dev
, gl
, rsp
))
822 pr_info("%s: unexpected FL contents at %p, " \
823 "RSS %#llx, FL %#llx, len %u\n",
824 pci_name(ctx
->lldi
.pdev
), gl
->va
,
825 (unsigned long long)be64_to_cpu(*rsp
),
826 (unsigned long long)be64_to_cpu(
827 *(__force __be64
*)gl
->va
),
832 skb
= cxgb4_pktgl_to_skb(gl
, 128, 128);
838 if (c4iw_handlers
[opcode
])
839 c4iw_handlers
[opcode
](dev
, skb
);
841 pr_info("%s no handler opcode 0x%x...\n", __func__
,
849 static int c4iw_uld_state_change(void *handle
, enum cxgb4_state new_state
)
851 struct uld_ctx
*ctx
= handle
;
853 PDBG("%s new_state %u\n", __func__
, new_state
);
856 printk(KERN_INFO MOD
"%s: Up\n", pci_name(ctx
->lldi
.pdev
));
860 ctx
->dev
= c4iw_alloc(&ctx
->lldi
);
861 if (IS_ERR(ctx
->dev
)) {
863 "%s: initialization failed: %ld\n",
864 pci_name(ctx
->lldi
.pdev
),
869 ret
= c4iw_register_device(ctx
->dev
);
872 "%s: RDMA registration failed: %d\n",
873 pci_name(ctx
->lldi
.pdev
), ret
);
878 case CXGB4_STATE_DOWN
:
879 printk(KERN_INFO MOD
"%s: Down\n",
880 pci_name(ctx
->lldi
.pdev
));
884 case CXGB4_STATE_START_RECOVERY
:
885 printk(KERN_INFO MOD
"%s: Fatal Error\n",
886 pci_name(ctx
->lldi
.pdev
));
888 struct ib_event event
;
890 ctx
->dev
->rdev
.flags
|= T4_FATAL_ERROR
;
891 memset(&event
, 0, sizeof event
);
892 event
.event
= IB_EVENT_DEVICE_FATAL
;
893 event
.device
= &ctx
->dev
->ibdev
;
894 ib_dispatch_event(&event
);
898 case CXGB4_STATE_DETACH
:
899 printk(KERN_INFO MOD
"%s: Detach\n",
900 pci_name(ctx
->lldi
.pdev
));
908 static int disable_qp_db(int id
, void *p
, void *data
)
910 struct c4iw_qp
*qp
= p
;
912 t4_disable_wq_db(&qp
->wq
);
916 static void stop_queues(struct uld_ctx
*ctx
)
918 spin_lock_irq(&ctx
->dev
->lock
);
919 if (ctx
->dev
->db_state
== NORMAL
) {
920 ctx
->dev
->rdev
.stats
.db_state_transitions
++;
921 ctx
->dev
->db_state
= FLOW_CONTROL
;
922 idr_for_each(&ctx
->dev
->qpidr
, disable_qp_db
, NULL
);
924 spin_unlock_irq(&ctx
->dev
->lock
);
927 static int enable_qp_db(int id
, void *p
, void *data
)
929 struct c4iw_qp
*qp
= p
;
931 t4_enable_wq_db(&qp
->wq
);
935 static void resume_queues(struct uld_ctx
*ctx
)
937 spin_lock_irq(&ctx
->dev
->lock
);
938 if (ctx
->dev
->qpcnt
<= db_fc_threshold
&&
939 ctx
->dev
->db_state
== FLOW_CONTROL
) {
940 ctx
->dev
->db_state
= NORMAL
;
941 ctx
->dev
->rdev
.stats
.db_state_transitions
++;
942 idr_for_each(&ctx
->dev
->qpidr
, enable_qp_db
, NULL
);
944 spin_unlock_irq(&ctx
->dev
->lock
);
949 struct c4iw_qp
**qps
;
952 static int add_and_ref_qp(int id
, void *p
, void *data
)
954 struct qp_list
*qp_listp
= data
;
955 struct c4iw_qp
*qp
= p
;
957 c4iw_qp_add_ref(&qp
->ibqp
);
958 qp_listp
->qps
[qp_listp
->idx
++] = qp
;
962 static int count_qps(int id
, void *p
, void *data
)
964 unsigned *countp
= data
;
969 static void deref_qps(struct qp_list qp_list
)
973 for (idx
= 0; idx
< qp_list
.idx
; idx
++)
974 c4iw_qp_rem_ref(&qp_list
.qps
[idx
]->ibqp
);
977 static void recover_lost_dbs(struct uld_ctx
*ctx
, struct qp_list
*qp_list
)
982 for (idx
= 0; idx
< qp_list
->idx
; idx
++) {
983 struct c4iw_qp
*qp
= qp_list
->qps
[idx
];
985 ret
= cxgb4_sync_txq_pidx(qp
->rhp
->rdev
.lldi
.ports
[0],
987 t4_sq_host_wq_pidx(&qp
->wq
),
988 t4_sq_wq_size(&qp
->wq
));
990 printk(KERN_ERR MOD
"%s: Fatal error - "
991 "DB overflow recovery failed - "
992 "error syncing SQ qid %u\n",
993 pci_name(ctx
->lldi
.pdev
), qp
->wq
.sq
.qid
);
997 ret
= cxgb4_sync_txq_pidx(qp
->rhp
->rdev
.lldi
.ports
[0],
999 t4_rq_host_wq_pidx(&qp
->wq
),
1000 t4_rq_wq_size(&qp
->wq
));
1003 printk(KERN_ERR MOD
"%s: Fatal error - "
1004 "DB overflow recovery failed - "
1005 "error syncing RQ qid %u\n",
1006 pci_name(ctx
->lldi
.pdev
), qp
->wq
.rq
.qid
);
1010 /* Wait for the dbfifo to drain */
1011 while (cxgb4_dbfifo_count(qp
->rhp
->rdev
.lldi
.ports
[0], 1) > 0) {
1012 set_current_state(TASK_UNINTERRUPTIBLE
);
1013 schedule_timeout(usecs_to_jiffies(10));
1018 static void recover_queues(struct uld_ctx
*ctx
)
1021 struct qp_list qp_list
;
1024 /* lock out kernel db ringers */
1025 mutex_lock(&ctx
->dev
->db_mutex
);
1027 /* put all queues in to recovery mode */
1028 spin_lock_irq(&ctx
->dev
->lock
);
1029 ctx
->dev
->db_state
= RECOVERY
;
1030 ctx
->dev
->rdev
.stats
.db_state_transitions
++;
1031 idr_for_each(&ctx
->dev
->qpidr
, disable_qp_db
, NULL
);
1032 spin_unlock_irq(&ctx
->dev
->lock
);
1034 /* slow everybody down */
1035 set_current_state(TASK_UNINTERRUPTIBLE
);
1036 schedule_timeout(usecs_to_jiffies(1000));
1038 /* Wait for the dbfifo to completely drain. */
1039 while (cxgb4_dbfifo_count(ctx
->dev
->rdev
.lldi
.ports
[0], 1) > 0) {
1040 set_current_state(TASK_UNINTERRUPTIBLE
);
1041 schedule_timeout(usecs_to_jiffies(10));
1044 /* flush the SGE contexts */
1045 ret
= cxgb4_flush_eq_cache(ctx
->dev
->rdev
.lldi
.ports
[0]);
1047 printk(KERN_ERR MOD
"%s: Fatal error - DB overflow recovery failed\n",
1048 pci_name(ctx
->lldi
.pdev
));
1052 /* Count active queues so we can build a list of queues to recover */
1053 spin_lock_irq(&ctx
->dev
->lock
);
1054 idr_for_each(&ctx
->dev
->qpidr
, count_qps
, &count
);
1056 qp_list
.qps
= kzalloc(count
* sizeof *qp_list
.qps
, GFP_ATOMIC
);
1058 printk(KERN_ERR MOD
"%s: Fatal error - DB overflow recovery failed\n",
1059 pci_name(ctx
->lldi
.pdev
));
1060 spin_unlock_irq(&ctx
->dev
->lock
);
1065 /* add and ref each qp so it doesn't get freed */
1066 idr_for_each(&ctx
->dev
->qpidr
, add_and_ref_qp
, &qp_list
);
1068 spin_unlock_irq(&ctx
->dev
->lock
);
1070 /* now traverse the list in a safe context to recover the db state*/
1071 recover_lost_dbs(ctx
, &qp_list
);
1073 /* we're almost done! deref the qps and clean up */
1077 /* Wait for the dbfifo to completely drain again */
1078 while (cxgb4_dbfifo_count(ctx
->dev
->rdev
.lldi
.ports
[0], 1) > 0) {
1079 set_current_state(TASK_UNINTERRUPTIBLE
);
1080 schedule_timeout(usecs_to_jiffies(10));
1083 /* resume the queues */
1084 spin_lock_irq(&ctx
->dev
->lock
);
1085 if (ctx
->dev
->qpcnt
> db_fc_threshold
)
1086 ctx
->dev
->db_state
= FLOW_CONTROL
;
1088 ctx
->dev
->db_state
= NORMAL
;
1089 idr_for_each(&ctx
->dev
->qpidr
, enable_qp_db
, NULL
);
1091 ctx
->dev
->rdev
.stats
.db_state_transitions
++;
1092 spin_unlock_irq(&ctx
->dev
->lock
);
1095 /* start up kernel db ringers again */
1096 mutex_unlock(&ctx
->dev
->db_mutex
);
1099 static int c4iw_uld_control(void *handle
, enum cxgb4_control control
, ...)
1101 struct uld_ctx
*ctx
= handle
;
1104 case CXGB4_CONTROL_DB_FULL
:
1106 mutex_lock(&ctx
->dev
->rdev
.stats
.lock
);
1107 ctx
->dev
->rdev
.stats
.db_full
++;
1108 mutex_unlock(&ctx
->dev
->rdev
.stats
.lock
);
1110 case CXGB4_CONTROL_DB_EMPTY
:
1112 mutex_lock(&ctx
->dev
->rdev
.stats
.lock
);
1113 ctx
->dev
->rdev
.stats
.db_empty
++;
1114 mutex_unlock(&ctx
->dev
->rdev
.stats
.lock
);
1116 case CXGB4_CONTROL_DB_DROP
:
1117 recover_queues(ctx
);
1118 mutex_lock(&ctx
->dev
->rdev
.stats
.lock
);
1119 ctx
->dev
->rdev
.stats
.db_drop
++;
1120 mutex_unlock(&ctx
->dev
->rdev
.stats
.lock
);
1123 printk(KERN_WARNING MOD
"%s: unknown control cmd %u\n",
1124 pci_name(ctx
->lldi
.pdev
), control
);
1130 static struct cxgb4_uld_info c4iw_uld_info
= {
1132 .add
= c4iw_uld_add
,
1133 .rx_handler
= c4iw_uld_rx_handler
,
1134 .state_change
= c4iw_uld_state_change
,
1135 .control
= c4iw_uld_control
,
1138 static int __init
c4iw_init_module(void)
1142 err
= c4iw_cm_init();
1146 c4iw_debugfs_root
= debugfs_create_dir(DRV_NAME
, NULL
);
1147 if (!c4iw_debugfs_root
)
1148 printk(KERN_WARNING MOD
1149 "could not create debugfs entry, continuing\n");
1151 cxgb4_register_uld(CXGB4_ULD_RDMA
, &c4iw_uld_info
);
1156 static void __exit
c4iw_exit_module(void)
1158 struct uld_ctx
*ctx
, *tmp
;
1160 mutex_lock(&dev_mutex
);
1161 list_for_each_entry_safe(ctx
, tmp
, &uld_ctx_list
, entry
) {
1166 mutex_unlock(&dev_mutex
);
1167 cxgb4_unregister_uld(CXGB4_ULD_RDMA
);
1169 debugfs_remove_recursive(c4iw_debugfs_root
);
1172 module_init(c4iw_init_module
);
1173 module_exit(c4iw_exit_module
);