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>
36 #include <linux/math64.h>
38 #include <rdma/ib_verbs.h>
42 #define DRV_VERSION "0.1"
44 MODULE_AUTHOR("Steve Wise");
45 MODULE_DESCRIPTION("Chelsio T4/T5 RDMA Driver");
46 MODULE_LICENSE("Dual BSD/GPL");
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 module_param(c4iw_wr_log, int, 0444);
60 MODULE_PARM_DESC(c4iw_wr_log, "Enables logging of work request timing data.");
62 static int c4iw_wr_log_size_order = 12;
63 module_param(c4iw_wr_log_size_order, int, 0444);
64 MODULE_PARM_DESC(c4iw_wr_log_size_order,
65 "Number of entries (log2) in the work request timing log.");
67 static LIST_HEAD(uld_ctx_list);
68 static DEFINE_MUTEX(dev_mutex);
69 static struct workqueue_struct *reg_workq;
71 #define DB_FC_RESUME_SIZE 64
72 #define DB_FC_RESUME_DELAY 1
73 #define DB_FC_DRAIN_THRESH 0
75 static struct dentry *c4iw_debugfs_root;
77 struct c4iw_debugfs_data {
78 struct c4iw_dev *devp;
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 void c4iw_log_wr_stats(struct t4_wq *wq, struct t4_cqe *cqe)
94 struct wr_log_entry le;
97 if (!wq->rdev->wr_log)
100 idx = (atomic_inc_return(&wq->rdev->wr_log_idx) - 1) &
101 (wq->rdev->wr_log_size - 1);
102 le.poll_sge_ts = cxgb4_read_sge_timestamp(wq->rdev->lldi.ports[0]);
103 le.poll_host_time = ktime_get();
105 le.cqe_sge_ts = CQE_TS(cqe);
108 le.opcode = CQE_OPCODE(cqe);
109 le.post_host_time = wq->sq.sw_sq[wq->sq.cidx].host_time;
110 le.post_sge_ts = wq->sq.sw_sq[wq->sq.cidx].sge_ts;
111 le.wr_id = CQE_WRID_SQ_IDX(cqe);
114 le.opcode = FW_RI_RECEIVE;
115 le.post_host_time = wq->rq.sw_rq[wq->rq.cidx].host_time;
116 le.post_sge_ts = wq->rq.sw_rq[wq->rq.cidx].sge_ts;
117 le.wr_id = CQE_WRID_MSN(cqe);
119 wq->rdev->wr_log[idx] = le;
122 static int wr_log_show(struct seq_file *seq, void *v)
124 struct c4iw_dev *dev = seq->private;
126 struct wr_log_entry *lep;
127 int prev_time_set = 0;
130 #define ts2ns(ts) div64_u64((ts) * dev->rdev.lldi.cclk_ps, 1000)
132 idx = atomic_read(&dev->rdev.wr_log_idx) &
133 (dev->rdev.wr_log_size - 1);
136 end = dev->rdev.wr_log_size - 1;
137 lep = &dev->rdev.wr_log[idx];
140 if (!prev_time_set) {
142 prev_time = lep->poll_host_time;
144 seq_printf(seq, "%04u: nsec %llu qid %u opcode "
145 "%u %s 0x%x host_wr_delta nsec %llu "
146 "post_sge_ts 0x%llx cqe_sge_ts 0x%llx "
147 "poll_sge_ts 0x%llx post_poll_delta_ns %llu "
148 "cqe_poll_delta_ns %llu\n",
150 ktime_to_ns(ktime_sub(lep->poll_host_time,
152 lep->qid, lep->opcode,
153 lep->opcode == FW_RI_RECEIVE ?
156 ktime_to_ns(ktime_sub(lep->poll_host_time,
157 lep->post_host_time)),
158 lep->post_sge_ts, lep->cqe_sge_ts,
160 ts2ns(lep->poll_sge_ts - lep->post_sge_ts),
161 ts2ns(lep->poll_sge_ts - lep->cqe_sge_ts));
162 prev_time = lep->poll_host_time;
165 if (idx > (dev->rdev.wr_log_size - 1))
167 lep = &dev->rdev.wr_log[idx];
173 static int wr_log_open(struct inode *inode, struct file *file)
175 return single_open(file, wr_log_show, inode->i_private);
178 static ssize_t wr_log_clear(struct file *file, const char __user *buf,
179 size_t count, loff_t *pos)
181 struct c4iw_dev *dev = ((struct seq_file *)file->private_data)->private;
184 if (dev->rdev.wr_log)
185 for (i = 0; i < dev->rdev.wr_log_size; i++)
186 dev->rdev.wr_log[i].valid = 0;
190 static const struct file_operations wr_log_debugfs_fops = {
191 .owner = THIS_MODULE,
193 .release = single_release,
196 .write = wr_log_clear,
199 static struct sockaddr_in zero_sin = {
200 .sin_family = AF_INET,
203 static struct sockaddr_in6 zero_sin6 = {
204 .sin6_family = AF_INET6,
207 static void set_ep_sin_addrs(struct c4iw_ep *ep,
208 struct sockaddr_in **lsin,
209 struct sockaddr_in **rsin,
210 struct sockaddr_in **m_lsin,
211 struct sockaddr_in **m_rsin)
213 struct iw_cm_id *id = ep->com.cm_id;
215 *m_lsin = (struct sockaddr_in *)&ep->com.local_addr;
216 *m_rsin = (struct sockaddr_in *)&ep->com.remote_addr;
218 *lsin = (struct sockaddr_in *)&id->local_addr;
219 *rsin = (struct sockaddr_in *)&id->remote_addr;
226 static void set_ep_sin6_addrs(struct c4iw_ep *ep,
227 struct sockaddr_in6 **lsin6,
228 struct sockaddr_in6 **rsin6,
229 struct sockaddr_in6 **m_lsin6,
230 struct sockaddr_in6 **m_rsin6)
232 struct iw_cm_id *id = ep->com.cm_id;
234 *m_lsin6 = (struct sockaddr_in6 *)&ep->com.local_addr;
235 *m_rsin6 = (struct sockaddr_in6 *)&ep->com.remote_addr;
237 *lsin6 = (struct sockaddr_in6 *)&id->local_addr;
238 *rsin6 = (struct sockaddr_in6 *)&id->remote_addr;
245 static int dump_qp(struct c4iw_qp *qp, struct c4iw_debugfs_data *qpd)
250 space = qpd->bufsize - qpd->pos - 1;
255 struct c4iw_ep *ep = qp->ep;
257 if (ep->com.local_addr.ss_family == AF_INET) {
258 struct sockaddr_in *lsin;
259 struct sockaddr_in *rsin;
260 struct sockaddr_in *m_lsin;
261 struct sockaddr_in *m_rsin;
263 set_ep_sin_addrs(ep, &lsin, &rsin, &m_lsin, &m_rsin);
264 cc = snprintf(qpd->buf + qpd->pos, space,
265 "rc qp sq id %u %s id %u state %u "
266 "onchip %u ep tid %u state %u "
267 "%pI4:%u/%u->%pI4:%u/%u\n",
268 qp->wq.sq.qid, qp->srq ? "srq" : "rq",
269 qp->srq ? qp->srq->idx : qp->wq.rq.qid,
271 qp->wq.sq.flags & T4_SQ_ONCHIP,
272 ep->hwtid, (int)ep->com.state,
273 &lsin->sin_addr, ntohs(lsin->sin_port),
274 ntohs(m_lsin->sin_port),
275 &rsin->sin_addr, ntohs(rsin->sin_port),
276 ntohs(m_rsin->sin_port));
278 struct sockaddr_in6 *lsin6;
279 struct sockaddr_in6 *rsin6;
280 struct sockaddr_in6 *m_lsin6;
281 struct sockaddr_in6 *m_rsin6;
283 set_ep_sin6_addrs(ep, &lsin6, &rsin6, &m_lsin6,
285 cc = snprintf(qpd->buf + qpd->pos, space,
286 "rc qp sq id %u rq id %u state %u "
287 "onchip %u ep tid %u state %u "
288 "%pI6:%u/%u->%pI6:%u/%u\n",
289 qp->wq.sq.qid, qp->wq.rq.qid,
291 qp->wq.sq.flags & T4_SQ_ONCHIP,
292 ep->hwtid, (int)ep->com.state,
294 ntohs(lsin6->sin6_port),
295 ntohs(m_lsin6->sin6_port),
297 ntohs(rsin6->sin6_port),
298 ntohs(m_rsin6->sin6_port));
301 cc = snprintf(qpd->buf + qpd->pos, space,
302 "qp sq id %u rq id %u state %u onchip %u\n",
303 qp->wq.sq.qid, qp->wq.rq.qid,
305 qp->wq.sq.flags & T4_SQ_ONCHIP);
311 static int qp_release(struct inode *inode, struct file *file)
313 struct c4iw_debugfs_data *qpd = file->private_data;
315 pr_info("%s null qpd?\n", __func__);
323 static int qp_open(struct inode *inode, struct file *file)
326 struct c4iw_debugfs_data *qpd;
330 qpd = kmalloc(sizeof *qpd, GFP_KERNEL);
334 qpd->devp = inode->i_private;
338 * No need to lock; we drop the lock to call vmalloc so it's racy
339 * anyway. Someone who cares should switch this over to seq_file
341 xa_for_each(&qpd->devp->qps, index, qp)
344 qpd->bufsize = count * 180;
345 qpd->buf = vmalloc(qpd->bufsize);
351 xa_lock_irq(&qpd->devp->qps);
352 xa_for_each(&qpd->devp->qps, index, qp)
354 xa_unlock_irq(&qpd->devp->qps);
356 qpd->buf[qpd->pos++] = 0;
357 file->private_data = qpd;
361 static const struct file_operations qp_debugfs_fops = {
362 .owner = THIS_MODULE,
364 .release = qp_release,
365 .read = debugfs_read,
366 .llseek = default_llseek,
369 static int dump_stag(unsigned long id, struct c4iw_debugfs_data *stagd)
373 struct fw_ri_tpte tpte;
376 space = stagd->bufsize - stagd->pos - 1;
380 ret = cxgb4_read_tpte(stagd->devp->rdev.lldi.ports[0], (u32)id<<8,
383 dev_err(&stagd->devp->rdev.lldi.pdev->dev,
384 "%s cxgb4_read_tpte err %d\n", __func__, ret);
387 cc = snprintf(stagd->buf + stagd->pos, space,
388 "stag: idx 0x%x valid %d key 0x%x state %d pdid %d "
389 "perm 0x%x ps %d len 0x%llx va 0x%llx\n",
391 FW_RI_TPTE_VALID_G(ntohl(tpte.valid_to_pdid)),
392 FW_RI_TPTE_STAGKEY_G(ntohl(tpte.valid_to_pdid)),
393 FW_RI_TPTE_STAGSTATE_G(ntohl(tpte.valid_to_pdid)),
394 FW_RI_TPTE_PDID_G(ntohl(tpte.valid_to_pdid)),
395 FW_RI_TPTE_PERM_G(ntohl(tpte.locread_to_qpid)),
396 FW_RI_TPTE_PS_G(ntohl(tpte.locread_to_qpid)),
397 ((u64)ntohl(tpte.len_hi) << 32) | ntohl(tpte.len_lo),
398 ((u64)ntohl(tpte.va_hi) << 32) | ntohl(tpte.va_lo_fbo));
404 static int stag_release(struct inode *inode, struct file *file)
406 struct c4iw_debugfs_data *stagd = file->private_data;
408 pr_info("%s null stagd?\n", __func__);
416 static int stag_open(struct inode *inode, struct file *file)
418 struct c4iw_debugfs_data *stagd;
424 stagd = kmalloc(sizeof *stagd, GFP_KERNEL);
429 stagd->devp = inode->i_private;
432 xa_for_each(&stagd->devp->mrs, index, p)
435 stagd->bufsize = count * 256;
436 stagd->buf = vmalloc(stagd->bufsize);
442 xa_lock_irq(&stagd->devp->mrs);
443 xa_for_each(&stagd->devp->mrs, index, p)
444 dump_stag(index, stagd);
445 xa_unlock_irq(&stagd->devp->mrs);
447 stagd->buf[stagd->pos++] = 0;
448 file->private_data = stagd;
456 static const struct file_operations stag_debugfs_fops = {
457 .owner = THIS_MODULE,
459 .release = stag_release,
460 .read = debugfs_read,
461 .llseek = default_llseek,
464 static char *db_state_str[] = {"NORMAL", "FLOW_CONTROL", "RECOVERY", "STOPPED"};
466 static int stats_show(struct seq_file *seq, void *v)
468 struct c4iw_dev *dev = seq->private;
470 seq_printf(seq, " Object: %10s %10s %10s %10s\n", "Total", "Current",
472 seq_printf(seq, " PDID: %10llu %10llu %10llu %10llu\n",
473 dev->rdev.stats.pd.total, dev->rdev.stats.pd.cur,
474 dev->rdev.stats.pd.max, dev->rdev.stats.pd.fail);
475 seq_printf(seq, " QID: %10llu %10llu %10llu %10llu\n",
476 dev->rdev.stats.qid.total, dev->rdev.stats.qid.cur,
477 dev->rdev.stats.qid.max, dev->rdev.stats.qid.fail);
478 seq_printf(seq, " SRQS: %10llu %10llu %10llu %10llu\n",
479 dev->rdev.stats.srqt.total, dev->rdev.stats.srqt.cur,
480 dev->rdev.stats.srqt.max, dev->rdev.stats.srqt.fail);
481 seq_printf(seq, " TPTMEM: %10llu %10llu %10llu %10llu\n",
482 dev->rdev.stats.stag.total, dev->rdev.stats.stag.cur,
483 dev->rdev.stats.stag.max, dev->rdev.stats.stag.fail);
484 seq_printf(seq, " PBLMEM: %10llu %10llu %10llu %10llu\n",
485 dev->rdev.stats.pbl.total, dev->rdev.stats.pbl.cur,
486 dev->rdev.stats.pbl.max, dev->rdev.stats.pbl.fail);
487 seq_printf(seq, " RQTMEM: %10llu %10llu %10llu %10llu\n",
488 dev->rdev.stats.rqt.total, dev->rdev.stats.rqt.cur,
489 dev->rdev.stats.rqt.max, dev->rdev.stats.rqt.fail);
490 seq_printf(seq, " OCQPMEM: %10llu %10llu %10llu %10llu\n",
491 dev->rdev.stats.ocqp.total, dev->rdev.stats.ocqp.cur,
492 dev->rdev.stats.ocqp.max, dev->rdev.stats.ocqp.fail);
493 seq_printf(seq, " DB FULL: %10llu\n", dev->rdev.stats.db_full);
494 seq_printf(seq, " DB EMPTY: %10llu\n", dev->rdev.stats.db_empty);
495 seq_printf(seq, " DB DROP: %10llu\n", dev->rdev.stats.db_drop);
496 seq_printf(seq, " DB State: %s Transitions %llu FC Interruptions %llu\n",
497 db_state_str[dev->db_state],
498 dev->rdev.stats.db_state_transitions,
499 dev->rdev.stats.db_fc_interruptions);
500 seq_printf(seq, "TCAM_FULL: %10llu\n", dev->rdev.stats.tcam_full);
501 seq_printf(seq, "ACT_OFLD_CONN_FAILS: %10llu\n",
502 dev->rdev.stats.act_ofld_conn_fails);
503 seq_printf(seq, "PAS_OFLD_CONN_FAILS: %10llu\n",
504 dev->rdev.stats.pas_ofld_conn_fails);
505 seq_printf(seq, "NEG_ADV_RCVD: %10llu\n", dev->rdev.stats.neg_adv);
506 seq_printf(seq, "AVAILABLE IRD: %10u\n", dev->avail_ird);
510 static int stats_open(struct inode *inode, struct file *file)
512 return single_open(file, stats_show, inode->i_private);
515 static ssize_t stats_clear(struct file *file, const char __user *buf,
516 size_t count, loff_t *pos)
518 struct c4iw_dev *dev = ((struct seq_file *)file->private_data)->private;
520 mutex_lock(&dev->rdev.stats.lock);
521 dev->rdev.stats.pd.max = 0;
522 dev->rdev.stats.pd.fail = 0;
523 dev->rdev.stats.qid.max = 0;
524 dev->rdev.stats.qid.fail = 0;
525 dev->rdev.stats.stag.max = 0;
526 dev->rdev.stats.stag.fail = 0;
527 dev->rdev.stats.pbl.max = 0;
528 dev->rdev.stats.pbl.fail = 0;
529 dev->rdev.stats.rqt.max = 0;
530 dev->rdev.stats.rqt.fail = 0;
531 dev->rdev.stats.rqt.max = 0;
532 dev->rdev.stats.rqt.fail = 0;
533 dev->rdev.stats.ocqp.max = 0;
534 dev->rdev.stats.ocqp.fail = 0;
535 dev->rdev.stats.db_full = 0;
536 dev->rdev.stats.db_empty = 0;
537 dev->rdev.stats.db_drop = 0;
538 dev->rdev.stats.db_state_transitions = 0;
539 dev->rdev.stats.tcam_full = 0;
540 dev->rdev.stats.act_ofld_conn_fails = 0;
541 dev->rdev.stats.pas_ofld_conn_fails = 0;
542 mutex_unlock(&dev->rdev.stats.lock);
546 static const struct file_operations stats_debugfs_fops = {
547 .owner = THIS_MODULE,
549 .release = single_release,
552 .write = stats_clear,
555 static int dump_ep(struct c4iw_ep *ep, struct c4iw_debugfs_data *epd)
560 space = epd->bufsize - epd->pos - 1;
564 if (ep->com.local_addr.ss_family == AF_INET) {
565 struct sockaddr_in *lsin;
566 struct sockaddr_in *rsin;
567 struct sockaddr_in *m_lsin;
568 struct sockaddr_in *m_rsin;
570 set_ep_sin_addrs(ep, &lsin, &rsin, &m_lsin, &m_rsin);
571 cc = snprintf(epd->buf + epd->pos, space,
572 "ep %p cm_id %p qp %p state %d flags 0x%lx "
573 "history 0x%lx hwtid %d atid %d "
574 "conn_na %u abort_na %u "
575 "%pI4:%d/%d <-> %pI4:%d/%d\n",
576 ep, ep->com.cm_id, ep->com.qp,
577 (int)ep->com.state, ep->com.flags,
578 ep->com.history, ep->hwtid, ep->atid,
579 ep->stats.connect_neg_adv,
580 ep->stats.abort_neg_adv,
581 &lsin->sin_addr, ntohs(lsin->sin_port),
582 ntohs(m_lsin->sin_port),
583 &rsin->sin_addr, ntohs(rsin->sin_port),
584 ntohs(m_rsin->sin_port));
586 struct sockaddr_in6 *lsin6;
587 struct sockaddr_in6 *rsin6;
588 struct sockaddr_in6 *m_lsin6;
589 struct sockaddr_in6 *m_rsin6;
591 set_ep_sin6_addrs(ep, &lsin6, &rsin6, &m_lsin6, &m_rsin6);
592 cc = snprintf(epd->buf + epd->pos, space,
593 "ep %p cm_id %p qp %p state %d flags 0x%lx "
594 "history 0x%lx hwtid %d atid %d "
595 "conn_na %u abort_na %u "
596 "%pI6:%d/%d <-> %pI6:%d/%d\n",
597 ep, ep->com.cm_id, ep->com.qp,
598 (int)ep->com.state, ep->com.flags,
599 ep->com.history, ep->hwtid, ep->atid,
600 ep->stats.connect_neg_adv,
601 ep->stats.abort_neg_adv,
602 &lsin6->sin6_addr, ntohs(lsin6->sin6_port),
603 ntohs(m_lsin6->sin6_port),
604 &rsin6->sin6_addr, ntohs(rsin6->sin6_port),
605 ntohs(m_rsin6->sin6_port));
613 int dump_listen_ep(struct c4iw_listen_ep *ep, struct c4iw_debugfs_data *epd)
618 space = epd->bufsize - epd->pos - 1;
622 if (ep->com.local_addr.ss_family == AF_INET) {
623 struct sockaddr_in *lsin = (struct sockaddr_in *)
624 &ep->com.cm_id->local_addr;
625 struct sockaddr_in *m_lsin = (struct sockaddr_in *)
626 &ep->com.cm_id->m_local_addr;
628 cc = snprintf(epd->buf + epd->pos, space,
629 "ep %p cm_id %p state %d flags 0x%lx stid %d "
630 "backlog %d %pI4:%d/%d\n",
631 ep, ep->com.cm_id, (int)ep->com.state,
632 ep->com.flags, ep->stid, ep->backlog,
633 &lsin->sin_addr, ntohs(lsin->sin_port),
634 ntohs(m_lsin->sin_port));
636 struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)
637 &ep->com.cm_id->local_addr;
638 struct sockaddr_in6 *m_lsin6 = (struct sockaddr_in6 *)
639 &ep->com.cm_id->m_local_addr;
641 cc = snprintf(epd->buf + epd->pos, space,
642 "ep %p cm_id %p state %d flags 0x%lx stid %d "
643 "backlog %d %pI6:%d/%d\n",
644 ep, ep->com.cm_id, (int)ep->com.state,
645 ep->com.flags, ep->stid, ep->backlog,
646 &lsin6->sin6_addr, ntohs(lsin6->sin6_port),
647 ntohs(m_lsin6->sin6_port));
654 static int ep_release(struct inode *inode, struct file *file)
656 struct c4iw_debugfs_data *epd = file->private_data;
658 pr_info("%s null qpd?\n", __func__);
666 static int ep_open(struct inode *inode, struct file *file)
669 struct c4iw_listen_ep *lep;
671 struct c4iw_debugfs_data *epd;
675 epd = kmalloc(sizeof(*epd), GFP_KERNEL);
680 epd->devp = inode->i_private;
683 xa_for_each(&epd->devp->hwtids, index, ep)
685 xa_for_each(&epd->devp->atids, index, ep)
687 xa_for_each(&epd->devp->stids, index, lep)
690 epd->bufsize = count * 240;
691 epd->buf = vmalloc(epd->bufsize);
697 xa_lock_irq(&epd->devp->hwtids);
698 xa_for_each(&epd->devp->hwtids, index, ep)
700 xa_unlock_irq(&epd->devp->hwtids);
701 xa_lock_irq(&epd->devp->atids);
702 xa_for_each(&epd->devp->atids, index, ep)
704 xa_unlock_irq(&epd->devp->atids);
705 xa_lock_irq(&epd->devp->stids);
706 xa_for_each(&epd->devp->stids, index, lep)
707 dump_listen_ep(lep, epd);
708 xa_unlock_irq(&epd->devp->stids);
710 file->private_data = epd;
718 static const struct file_operations ep_debugfs_fops = {
719 .owner = THIS_MODULE,
721 .release = ep_release,
722 .read = debugfs_read,
725 static void setup_debugfs(struct c4iw_dev *devp)
727 debugfs_create_file_size("qps", S_IWUSR, devp->debugfs_root,
728 (void *)devp, &qp_debugfs_fops, 4096);
730 debugfs_create_file_size("stags", S_IWUSR, devp->debugfs_root,
731 (void *)devp, &stag_debugfs_fops, 4096);
733 debugfs_create_file_size("stats", S_IWUSR, devp->debugfs_root,
734 (void *)devp, &stats_debugfs_fops, 4096);
736 debugfs_create_file_size("eps", S_IWUSR, devp->debugfs_root,
737 (void *)devp, &ep_debugfs_fops, 4096);
740 debugfs_create_file_size("wr_log", S_IWUSR, devp->debugfs_root,
741 (void *)devp, &wr_log_debugfs_fops, 4096);
744 void c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
745 struct c4iw_dev_ucontext *uctx)
747 struct list_head *pos, *nxt;
748 struct c4iw_qid_list *entry;
750 mutex_lock(&uctx->lock);
751 list_for_each_safe(pos, nxt, &uctx->qpids) {
752 entry = list_entry(pos, struct c4iw_qid_list, entry);
753 list_del_init(&entry->entry);
754 if (!(entry->qid & rdev->qpmask)) {
755 c4iw_put_resource(&rdev->resource.qid_table,
757 mutex_lock(&rdev->stats.lock);
758 rdev->stats.qid.cur -= rdev->qpmask + 1;
759 mutex_unlock(&rdev->stats.lock);
764 list_for_each_safe(pos, nxt, &uctx->cqids) {
765 entry = list_entry(pos, struct c4iw_qid_list, entry);
766 list_del_init(&entry->entry);
769 mutex_unlock(&uctx->lock);
772 void c4iw_init_dev_ucontext(struct c4iw_rdev *rdev,
773 struct c4iw_dev_ucontext *uctx)
775 INIT_LIST_HEAD(&uctx->qpids);
776 INIT_LIST_HEAD(&uctx->cqids);
777 mutex_init(&uctx->lock);
780 /* Caller takes care of locking if needed */
781 static int c4iw_rdev_open(struct c4iw_rdev *rdev)
786 c4iw_init_dev_ucontext(rdev, &rdev->uctx);
789 * This implementation assumes udb_density == ucq_density! Eventually
790 * we might need to support this but for now fail the open. Also the
791 * cqid and qpid range must match for now.
793 if (rdev->lldi.udb_density != rdev->lldi.ucq_density) {
794 pr_err("%s: unsupported udb/ucq densities %u/%u\n",
795 pci_name(rdev->lldi.pdev), rdev->lldi.udb_density,
796 rdev->lldi.ucq_density);
799 if (rdev->lldi.vr->qp.start != rdev->lldi.vr->cq.start ||
800 rdev->lldi.vr->qp.size != rdev->lldi.vr->cq.size) {
801 pr_err("%s: unsupported qp and cq id ranges qp start %u size %u cq start %u size %u\n",
802 pci_name(rdev->lldi.pdev), rdev->lldi.vr->qp.start,
803 rdev->lldi.vr->qp.size, rdev->lldi.vr->cq.size,
804 rdev->lldi.vr->cq.size);
808 /* This implementation requires a sge_host_page_size <= PAGE_SIZE. */
809 if (rdev->lldi.sge_host_page_size > PAGE_SIZE) {
810 pr_err("%s: unsupported sge host page size %u\n",
811 pci_name(rdev->lldi.pdev),
812 rdev->lldi.sge_host_page_size);
816 factor = PAGE_SIZE / rdev->lldi.sge_host_page_size;
817 rdev->qpmask = (rdev->lldi.udb_density * factor) - 1;
818 rdev->cqmask = (rdev->lldi.ucq_density * factor) - 1;
820 pr_debug("dev %s stag start 0x%0x size 0x%0x num stags %d pbl start 0x%0x size 0x%0x rq start 0x%0x size 0x%0x qp qid start %u size %u cq qid start %u size %u srq size %u\n",
821 pci_name(rdev->lldi.pdev), rdev->lldi.vr->stag.start,
822 rdev->lldi.vr->stag.size, c4iw_num_stags(rdev),
823 rdev->lldi.vr->pbl.start,
824 rdev->lldi.vr->pbl.size, rdev->lldi.vr->rq.start,
825 rdev->lldi.vr->rq.size,
826 rdev->lldi.vr->qp.start,
827 rdev->lldi.vr->qp.size,
828 rdev->lldi.vr->cq.start,
829 rdev->lldi.vr->cq.size,
830 rdev->lldi.vr->srq.size);
831 pr_debug("udb %pR db_reg %p gts_reg %p qpmask 0x%x cqmask 0x%x\n",
832 &rdev->lldi.pdev->resource[2],
833 rdev->lldi.db_reg, rdev->lldi.gts_reg,
834 rdev->qpmask, rdev->cqmask);
836 if (c4iw_num_stags(rdev) == 0)
839 rdev->stats.pd.total = T4_MAX_NUM_PD;
840 rdev->stats.stag.total = rdev->lldi.vr->stag.size;
841 rdev->stats.pbl.total = rdev->lldi.vr->pbl.size;
842 rdev->stats.rqt.total = rdev->lldi.vr->rq.size;
843 rdev->stats.srqt.total = rdev->lldi.vr->srq.size;
844 rdev->stats.ocqp.total = rdev->lldi.vr->ocq.size;
845 rdev->stats.qid.total = rdev->lldi.vr->qp.size;
847 err = c4iw_init_resource(rdev, c4iw_num_stags(rdev),
848 T4_MAX_NUM_PD, rdev->lldi.vr->srq.size);
850 pr_err("error %d initializing resources\n", err);
853 err = c4iw_pblpool_create(rdev);
855 pr_err("error %d initializing pbl pool\n", err);
856 goto destroy_resource;
858 err = c4iw_rqtpool_create(rdev);
860 pr_err("error %d initializing rqt pool\n", err);
861 goto destroy_pblpool;
863 err = c4iw_ocqp_pool_create(rdev);
865 pr_err("error %d initializing ocqp pool\n", err);
866 goto destroy_rqtpool;
868 rdev->status_page = (struct t4_dev_status_page *)
869 __get_free_page(GFP_KERNEL);
870 if (!rdev->status_page) {
872 goto destroy_ocqp_pool;
874 rdev->status_page->qp_start = rdev->lldi.vr->qp.start;
875 rdev->status_page->qp_size = rdev->lldi.vr->qp.size;
876 rdev->status_page->cq_start = rdev->lldi.vr->cq.start;
877 rdev->status_page->cq_size = rdev->lldi.vr->cq.size;
878 rdev->status_page->write_cmpl_supported = rdev->lldi.write_cmpl_support;
881 rdev->wr_log = kcalloc(1 << c4iw_wr_log_size_order,
882 sizeof(*rdev->wr_log),
885 rdev->wr_log_size = 1 << c4iw_wr_log_size_order;
886 atomic_set(&rdev->wr_log_idx, 0);
890 rdev->free_workq = create_singlethread_workqueue("iw_cxgb4_free");
891 if (!rdev->free_workq) {
893 goto err_free_status_page_and_wr_log;
896 rdev->status_page->db_off = 0;
898 init_completion(&rdev->rqt_compl);
899 init_completion(&rdev->pbl_compl);
900 kref_init(&rdev->rqt_kref);
901 kref_init(&rdev->pbl_kref);
904 err_free_status_page_and_wr_log:
905 if (c4iw_wr_log && rdev->wr_log)
907 free_page((unsigned long)rdev->status_page);
909 c4iw_ocqp_pool_destroy(rdev);
911 c4iw_rqtpool_destroy(rdev);
913 c4iw_pblpool_destroy(rdev);
915 c4iw_destroy_resource(&rdev->resource);
919 static void c4iw_rdev_close(struct c4iw_rdev *rdev)
922 c4iw_release_dev_ucontext(rdev, &rdev->uctx);
923 free_page((unsigned long)rdev->status_page);
924 c4iw_pblpool_destroy(rdev);
925 c4iw_rqtpool_destroy(rdev);
926 wait_for_completion(&rdev->pbl_compl);
927 wait_for_completion(&rdev->rqt_compl);
928 c4iw_ocqp_pool_destroy(rdev);
929 destroy_workqueue(rdev->free_workq);
930 c4iw_destroy_resource(&rdev->resource);
933 void c4iw_dealloc(struct uld_ctx *ctx)
935 c4iw_rdev_close(&ctx->dev->rdev);
936 WARN_ON(!xa_empty(&ctx->dev->cqs));
937 WARN_ON(!xa_empty(&ctx->dev->qps));
938 WARN_ON(!xa_empty(&ctx->dev->mrs));
939 wait_event(ctx->dev->wait, xa_empty(&ctx->dev->hwtids));
940 WARN_ON(!xa_empty(&ctx->dev->stids));
941 WARN_ON(!xa_empty(&ctx->dev->atids));
942 if (ctx->dev->rdev.bar2_kva)
943 iounmap(ctx->dev->rdev.bar2_kva);
944 if (ctx->dev->rdev.oc_mw_kva)
945 iounmap(ctx->dev->rdev.oc_mw_kva);
946 ib_dealloc_device(&ctx->dev->ibdev);
950 static void c4iw_remove(struct uld_ctx *ctx)
952 pr_debug("c4iw_dev %p\n", ctx->dev);
953 c4iw_unregister_device(ctx->dev);
957 static int rdma_supported(const struct cxgb4_lld_info *infop)
959 return infop->vr->stag.size > 0 && infop->vr->pbl.size > 0 &&
960 infop->vr->rq.size > 0 && infop->vr->qp.size > 0 &&
961 infop->vr->cq.size > 0;
964 static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
966 struct c4iw_dev *devp;
969 if (!rdma_supported(infop)) {
970 pr_info("%s: RDMA not supported on this device\n",
971 pci_name(infop->pdev));
972 return ERR_PTR(-ENOSYS);
974 if (!ocqp_supported(infop))
975 pr_info("%s: On-Chip Queues not supported on this device\n",
976 pci_name(infop->pdev));
978 devp = ib_alloc_device(c4iw_dev, ibdev);
980 pr_err("Cannot allocate ib device\n");
981 return ERR_PTR(-ENOMEM);
983 devp->rdev.lldi = *infop;
985 /* init various hw-queue params based on lld info */
986 pr_debug("Ing. padding boundary is %d, egrsstatuspagesize = %d\n",
987 devp->rdev.lldi.sge_ingpadboundary,
988 devp->rdev.lldi.sge_egrstatuspagesize);
990 devp->rdev.hw_queue.t4_eq_status_entries =
991 devp->rdev.lldi.sge_egrstatuspagesize / 64;
992 devp->rdev.hw_queue.t4_max_eq_size = 65520;
993 devp->rdev.hw_queue.t4_max_iq_size = 65520;
994 devp->rdev.hw_queue.t4_max_rq_size = 8192 -
995 devp->rdev.hw_queue.t4_eq_status_entries - 1;
996 devp->rdev.hw_queue.t4_max_sq_size =
997 devp->rdev.hw_queue.t4_max_eq_size -
998 devp->rdev.hw_queue.t4_eq_status_entries - 1;
999 devp->rdev.hw_queue.t4_max_qp_depth =
1000 devp->rdev.hw_queue.t4_max_rq_size;
1001 devp->rdev.hw_queue.t4_max_cq_depth =
1002 devp->rdev.hw_queue.t4_max_iq_size - 2;
1003 devp->rdev.hw_queue.t4_stat_len =
1004 devp->rdev.lldi.sge_egrstatuspagesize;
1007 * For T5/T6 devices, we map all of BAR2 with WC.
1008 * For T4 devices with onchip qp mem, we map only that part
1011 devp->rdev.bar2_pa = pci_resource_start(devp->rdev.lldi.pdev, 2);
1012 if (!is_t4(devp->rdev.lldi.adapter_type)) {
1013 devp->rdev.bar2_kva = ioremap_wc(devp->rdev.bar2_pa,
1014 pci_resource_len(devp->rdev.lldi.pdev, 2));
1015 if (!devp->rdev.bar2_kva) {
1016 pr_err("Unable to ioremap BAR2\n");
1017 ib_dealloc_device(&devp->ibdev);
1018 return ERR_PTR(-EINVAL);
1020 } else if (ocqp_supported(infop)) {
1021 devp->rdev.oc_mw_pa =
1022 pci_resource_start(devp->rdev.lldi.pdev, 2) +
1023 pci_resource_len(devp->rdev.lldi.pdev, 2) -
1024 roundup_pow_of_two(devp->rdev.lldi.vr->ocq.size);
1025 devp->rdev.oc_mw_kva = ioremap_wc(devp->rdev.oc_mw_pa,
1026 devp->rdev.lldi.vr->ocq.size);
1027 if (!devp->rdev.oc_mw_kva) {
1028 pr_err("Unable to ioremap onchip mem\n");
1029 ib_dealloc_device(&devp->ibdev);
1030 return ERR_PTR(-EINVAL);
1034 pr_debug("ocq memory: hw_start 0x%x size %u mw_pa 0x%lx mw_kva %p\n",
1035 devp->rdev.lldi.vr->ocq.start, devp->rdev.lldi.vr->ocq.size,
1036 devp->rdev.oc_mw_pa, devp->rdev.oc_mw_kva);
1038 ret = c4iw_rdev_open(&devp->rdev);
1040 pr_err("Unable to open CXIO rdev err %d\n", ret);
1041 ib_dealloc_device(&devp->ibdev);
1042 return ERR_PTR(ret);
1045 xa_init_flags(&devp->cqs, XA_FLAGS_LOCK_IRQ);
1046 xa_init_flags(&devp->qps, XA_FLAGS_LOCK_IRQ);
1047 xa_init_flags(&devp->mrs, XA_FLAGS_LOCK_IRQ);
1048 xa_init_flags(&devp->hwtids, XA_FLAGS_LOCK_IRQ);
1049 xa_init_flags(&devp->atids, XA_FLAGS_LOCK_IRQ);
1050 xa_init_flags(&devp->stids, XA_FLAGS_LOCK_IRQ);
1051 mutex_init(&devp->rdev.stats.lock);
1052 mutex_init(&devp->db_mutex);
1053 INIT_LIST_HEAD(&devp->db_fc_list);
1054 init_waitqueue_head(&devp->wait);
1055 devp->avail_ird = devp->rdev.lldi.max_ird_adapter;
1057 if (c4iw_debugfs_root) {
1058 devp->debugfs_root = debugfs_create_dir(
1059 pci_name(devp->rdev.lldi.pdev),
1061 setup_debugfs(devp);
1068 static void *c4iw_uld_add(const struct cxgb4_lld_info *infop)
1070 struct uld_ctx *ctx;
1071 static int vers_printed;
1074 if (!vers_printed++)
1075 pr_info("Chelsio T4/T5 RDMA Driver - version %s\n",
1078 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1080 ctx = ERR_PTR(-ENOMEM);
1085 pr_debug("found device %s nchan %u nrxq %u ntxq %u nports %u\n",
1086 pci_name(ctx->lldi.pdev),
1087 ctx->lldi.nchan, ctx->lldi.nrxq,
1088 ctx->lldi.ntxq, ctx->lldi.nports);
1090 mutex_lock(&dev_mutex);
1091 list_add_tail(&ctx->entry, &uld_ctx_list);
1092 mutex_unlock(&dev_mutex);
1094 for (i = 0; i < ctx->lldi.nrxq; i++)
1095 pr_debug("rxqid[%u] %u\n", i, ctx->lldi.rxq_ids[i]);
1100 static inline struct sk_buff *copy_gl_to_skb_pkt(const struct pkt_gl *gl,
1104 struct sk_buff *skb;
1107 * Allocate space for cpl_pass_accept_req which will be synthesized by
1108 * driver. Once the driver synthesizes the request the skb will go
1109 * through the regular cpl_pass_accept_req processing.
1110 * The math here assumes sizeof cpl_pass_accept_req >= sizeof
1113 skb = alloc_skb(gl->tot_len + sizeof(struct cpl_pass_accept_req) +
1114 sizeof(struct rss_header) - pktshift, GFP_ATOMIC);
1118 __skb_put(skb, gl->tot_len + sizeof(struct cpl_pass_accept_req) +
1119 sizeof(struct rss_header) - pktshift);
1122 * This skb will contain:
1123 * rss_header from the rspq descriptor (1 flit)
1124 * cpl_rx_pkt struct from the rspq descriptor (2 flits)
1125 * space for the difference between the size of an
1126 * rx_pkt and pass_accept_req cpl (1 flit)
1127 * the packet data from the gl
1129 skb_copy_to_linear_data(skb, rsp, sizeof(struct cpl_pass_accept_req) +
1130 sizeof(struct rss_header));
1131 skb_copy_to_linear_data_offset(skb, sizeof(struct rss_header) +
1132 sizeof(struct cpl_pass_accept_req),
1134 gl->tot_len - pktshift);
1138 static inline int recv_rx_pkt(struct c4iw_dev *dev, const struct pkt_gl *gl,
1141 unsigned int opcode = *(u8 *)rsp;
1142 struct sk_buff *skb;
1144 if (opcode != CPL_RX_PKT)
1147 skb = copy_gl_to_skb_pkt(gl , rsp, dev->rdev.lldi.sge_pktshift);
1151 if (c4iw_handlers[opcode] == NULL) {
1152 pr_info("%s no handler opcode 0x%x...\n", __func__, opcode);
1156 c4iw_handlers[opcode](dev, skb);
1162 static int c4iw_uld_rx_handler(void *handle, const __be64 *rsp,
1163 const struct pkt_gl *gl)
1165 struct uld_ctx *ctx = handle;
1166 struct c4iw_dev *dev = ctx->dev;
1167 struct sk_buff *skb;
1171 /* omit RSS and rsp_ctrl at end of descriptor */
1172 unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;
1174 skb = alloc_skb(256, GFP_ATOMIC);
1177 __skb_put(skb, len);
1178 skb_copy_to_linear_data(skb, &rsp[1], len);
1179 } else if (gl == CXGB4_MSG_AN) {
1180 const struct rsp_ctrl *rc = (void *)rsp;
1182 u32 qid = be32_to_cpu(rc->pldbuflen_qid);
1183 c4iw_ev_handler(dev, qid);
1185 } else if (unlikely(*(u8 *)rsp != *(u8 *)gl->va)) {
1186 if (recv_rx_pkt(dev, gl, rsp))
1189 pr_info("%s: unexpected FL contents at %p, RSS %#llx, FL %#llx, len %u\n",
1190 pci_name(ctx->lldi.pdev), gl->va,
1192 be64_to_cpu(*(__force __be64 *)gl->va),
1197 skb = cxgb4_pktgl_to_skb(gl, 128, 128);
1202 opcode = *(u8 *)rsp;
1203 if (c4iw_handlers[opcode]) {
1204 c4iw_handlers[opcode](dev, skb);
1206 pr_info("%s no handler opcode 0x%x...\n", __func__, opcode);
1215 static int c4iw_uld_state_change(void *handle, enum cxgb4_state new_state)
1217 struct uld_ctx *ctx = handle;
1219 pr_debug("new_state %u\n", new_state);
1220 switch (new_state) {
1221 case CXGB4_STATE_UP:
1222 pr_info("%s: Up\n", pci_name(ctx->lldi.pdev));
1224 ctx->dev = c4iw_alloc(&ctx->lldi);
1225 if (IS_ERR(ctx->dev)) {
1226 pr_err("%s: initialization failed: %ld\n",
1227 pci_name(ctx->lldi.pdev),
1233 INIT_WORK(&ctx->reg_work, c4iw_register_device);
1234 queue_work(reg_workq, &ctx->reg_work);
1237 case CXGB4_STATE_DOWN:
1238 pr_info("%s: Down\n", pci_name(ctx->lldi.pdev));
1242 case CXGB4_STATE_FATAL_ERROR:
1243 case CXGB4_STATE_START_RECOVERY:
1244 pr_info("%s: Fatal Error\n", pci_name(ctx->lldi.pdev));
1246 struct ib_event event;
1248 ctx->dev->rdev.flags |= T4_FATAL_ERROR;
1249 memset(&event, 0, sizeof event);
1250 event.event = IB_EVENT_DEVICE_FATAL;
1251 event.device = &ctx->dev->ibdev;
1252 ib_dispatch_event(&event);
1256 case CXGB4_STATE_DETACH:
1257 pr_info("%s: Detach\n", pci_name(ctx->lldi.pdev));
1265 static void stop_queues(struct uld_ctx *ctx)
1268 unsigned long index, flags;
1270 xa_lock_irqsave(&ctx->dev->qps, flags);
1271 ctx->dev->rdev.stats.db_state_transitions++;
1272 ctx->dev->db_state = STOPPED;
1273 if (ctx->dev->rdev.flags & T4_STATUS_PAGE_DISABLED) {
1274 xa_for_each(&ctx->dev->qps, index, qp)
1275 t4_disable_wq_db(&qp->wq);
1277 ctx->dev->rdev.status_page->db_off = 1;
1279 xa_unlock_irqrestore(&ctx->dev->qps, flags);
1282 static void resume_rc_qp(struct c4iw_qp *qp)
1284 spin_lock(&qp->lock);
1285 t4_ring_sq_db(&qp->wq, qp->wq.sq.wq_pidx_inc, NULL);
1286 qp->wq.sq.wq_pidx_inc = 0;
1287 t4_ring_rq_db(&qp->wq, qp->wq.rq.wq_pidx_inc, NULL);
1288 qp->wq.rq.wq_pidx_inc = 0;
1289 spin_unlock(&qp->lock);
1292 static void resume_a_chunk(struct uld_ctx *ctx)
1297 for (i = 0; i < DB_FC_RESUME_SIZE; i++) {
1298 qp = list_first_entry(&ctx->dev->db_fc_list, struct c4iw_qp,
1300 list_del_init(&qp->db_fc_entry);
1302 if (list_empty(&ctx->dev->db_fc_list))
1307 static void resume_queues(struct uld_ctx *ctx)
1309 xa_lock_irq(&ctx->dev->qps);
1310 if (ctx->dev->db_state != STOPPED)
1312 ctx->dev->db_state = FLOW_CONTROL;
1314 if (list_empty(&ctx->dev->db_fc_list)) {
1316 unsigned long index;
1318 WARN_ON(ctx->dev->db_state != FLOW_CONTROL);
1319 ctx->dev->db_state = NORMAL;
1320 ctx->dev->rdev.stats.db_state_transitions++;
1321 if (ctx->dev->rdev.flags & T4_STATUS_PAGE_DISABLED) {
1322 xa_for_each(&ctx->dev->qps, index, qp)
1323 t4_enable_wq_db(&qp->wq);
1325 ctx->dev->rdev.status_page->db_off = 0;
1329 if (cxgb4_dbfifo_count(ctx->dev->rdev.lldi.ports[0], 1)
1330 < (ctx->dev->rdev.lldi.dbfifo_int_thresh <<
1331 DB_FC_DRAIN_THRESH)) {
1332 resume_a_chunk(ctx);
1334 if (!list_empty(&ctx->dev->db_fc_list)) {
1335 xa_unlock_irq(&ctx->dev->qps);
1336 if (DB_FC_RESUME_DELAY) {
1337 set_current_state(TASK_UNINTERRUPTIBLE);
1338 schedule_timeout(DB_FC_RESUME_DELAY);
1340 xa_lock_irq(&ctx->dev->qps);
1341 if (ctx->dev->db_state != FLOW_CONTROL)
1347 if (ctx->dev->db_state != NORMAL)
1348 ctx->dev->rdev.stats.db_fc_interruptions++;
1349 xa_unlock_irq(&ctx->dev->qps);
1354 struct c4iw_qp **qps;
1357 static void deref_qps(struct qp_list *qp_list)
1361 for (idx = 0; idx < qp_list->idx; idx++)
1362 c4iw_qp_rem_ref(&qp_list->qps[idx]->ibqp);
1365 static void recover_lost_dbs(struct uld_ctx *ctx, struct qp_list *qp_list)
1370 for (idx = 0; idx < qp_list->idx; idx++) {
1371 struct c4iw_qp *qp = qp_list->qps[idx];
1373 xa_lock_irq(&qp->rhp->qps);
1374 spin_lock(&qp->lock);
1375 ret = cxgb4_sync_txq_pidx(qp->rhp->rdev.lldi.ports[0],
1377 t4_sq_host_wq_pidx(&qp->wq),
1378 t4_sq_wq_size(&qp->wq));
1380 pr_err("%s: Fatal error - DB overflow recovery failed - error syncing SQ qid %u\n",
1381 pci_name(ctx->lldi.pdev), qp->wq.sq.qid);
1382 spin_unlock(&qp->lock);
1383 xa_unlock_irq(&qp->rhp->qps);
1386 qp->wq.sq.wq_pidx_inc = 0;
1388 ret = cxgb4_sync_txq_pidx(qp->rhp->rdev.lldi.ports[0],
1390 t4_rq_host_wq_pidx(&qp->wq),
1391 t4_rq_wq_size(&qp->wq));
1394 pr_err("%s: Fatal error - DB overflow recovery failed - error syncing RQ qid %u\n",
1395 pci_name(ctx->lldi.pdev), qp->wq.rq.qid);
1396 spin_unlock(&qp->lock);
1397 xa_unlock_irq(&qp->rhp->qps);
1400 qp->wq.rq.wq_pidx_inc = 0;
1401 spin_unlock(&qp->lock);
1402 xa_unlock_irq(&qp->rhp->qps);
1404 /* Wait for the dbfifo to drain */
1405 while (cxgb4_dbfifo_count(qp->rhp->rdev.lldi.ports[0], 1) > 0) {
1406 set_current_state(TASK_UNINTERRUPTIBLE);
1407 schedule_timeout(usecs_to_jiffies(10));
1412 static void recover_queues(struct uld_ctx *ctx)
1415 unsigned long index;
1417 struct qp_list qp_list;
1420 /* slow everybody down */
1421 set_current_state(TASK_UNINTERRUPTIBLE);
1422 schedule_timeout(usecs_to_jiffies(1000));
1424 /* flush the SGE contexts */
1425 ret = cxgb4_flush_eq_cache(ctx->dev->rdev.lldi.ports[0]);
1427 pr_err("%s: Fatal error - DB overflow recovery failed\n",
1428 pci_name(ctx->lldi.pdev));
1432 /* Count active queues so we can build a list of queues to recover */
1433 xa_lock_irq(&ctx->dev->qps);
1434 WARN_ON(ctx->dev->db_state != STOPPED);
1435 ctx->dev->db_state = RECOVERY;
1436 xa_for_each(&ctx->dev->qps, index, qp)
1439 qp_list.qps = kcalloc(count, sizeof(*qp_list.qps), GFP_ATOMIC);
1441 xa_unlock_irq(&ctx->dev->qps);
1446 /* add and ref each qp so it doesn't get freed */
1447 xa_for_each(&ctx->dev->qps, index, qp) {
1448 c4iw_qp_add_ref(&qp->ibqp);
1449 qp_list.qps[qp_list.idx++] = qp;
1452 xa_unlock_irq(&ctx->dev->qps);
1454 /* now traverse the list in a safe context to recover the db state*/
1455 recover_lost_dbs(ctx, &qp_list);
1457 /* we're almost done! deref the qps and clean up */
1458 deref_qps(&qp_list);
1461 xa_lock_irq(&ctx->dev->qps);
1462 WARN_ON(ctx->dev->db_state != RECOVERY);
1463 ctx->dev->db_state = STOPPED;
1464 xa_unlock_irq(&ctx->dev->qps);
1467 static int c4iw_uld_control(void *handle, enum cxgb4_control control, ...)
1469 struct uld_ctx *ctx = handle;
1472 case CXGB4_CONTROL_DB_FULL:
1474 ctx->dev->rdev.stats.db_full++;
1476 case CXGB4_CONTROL_DB_EMPTY:
1478 mutex_lock(&ctx->dev->rdev.stats.lock);
1479 ctx->dev->rdev.stats.db_empty++;
1480 mutex_unlock(&ctx->dev->rdev.stats.lock);
1482 case CXGB4_CONTROL_DB_DROP:
1483 recover_queues(ctx);
1484 mutex_lock(&ctx->dev->rdev.stats.lock);
1485 ctx->dev->rdev.stats.db_drop++;
1486 mutex_unlock(&ctx->dev->rdev.stats.lock);
1489 pr_warn("%s: unknown control cmd %u\n",
1490 pci_name(ctx->lldi.pdev), control);
1496 static struct cxgb4_uld_info c4iw_uld_info = {
1498 .nrxq = MAX_ULD_QSETS,
1499 .ntxq = MAX_ULD_QSETS,
1503 .add = c4iw_uld_add,
1504 .rx_handler = c4iw_uld_rx_handler,
1505 .state_change = c4iw_uld_state_change,
1506 .control = c4iw_uld_control,
1509 void _c4iw_free_wr_wait(struct kref *kref)
1511 struct c4iw_wr_wait *wr_waitp;
1513 wr_waitp = container_of(kref, struct c4iw_wr_wait, kref);
1514 pr_debug("Free wr_wait %p\n", wr_waitp);
1518 struct c4iw_wr_wait *c4iw_alloc_wr_wait(gfp_t gfp)
1520 struct c4iw_wr_wait *wr_waitp;
1522 wr_waitp = kzalloc(sizeof(*wr_waitp), gfp);
1524 kref_init(&wr_waitp->kref);
1525 pr_debug("wr_wait %p\n", wr_waitp);
1530 static int __init c4iw_init_module(void)
1534 err = c4iw_cm_init();
1538 c4iw_debugfs_root = debugfs_create_dir(DRV_NAME, NULL);
1540 reg_workq = create_singlethread_workqueue("Register_iWARP_device");
1542 pr_err("Failed creating workqueue to register iwarp device\n");
1546 cxgb4_register_uld(CXGB4_ULD_RDMA, &c4iw_uld_info);
1551 static void __exit c4iw_exit_module(void)
1553 struct uld_ctx *ctx, *tmp;
1555 mutex_lock(&dev_mutex);
1556 list_for_each_entry_safe(ctx, tmp, &uld_ctx_list, entry) {
1561 mutex_unlock(&dev_mutex);
1562 flush_workqueue(reg_workq);
1563 destroy_workqueue(reg_workq);
1564 cxgb4_unregister_uld(CXGB4_ULD_RDMA);
1566 debugfs_remove_recursive(c4iw_debugfs_root);
1569 module_init(c4iw_init_module);
1570 module_exit(c4iw_exit_module);