blob: 3487c08828f72f8b54392ef0023fe6eb226d2b1b [file] [log] [blame]
Steve Wisecfdda9d2010-04-21 15:30:06 -07001/*
2 * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/debugfs.h>
Vipul Pandyae5725682012-05-21 17:31:13 +053035#include <linux/vmalloc.h>
Steve Wisecfdda9d2010-04-21 15:30:06 -070036
37#include <rdma/ib_verbs.h>
38
39#include "iw_cxgb4.h"
40
41#define DRV_VERSION "0.1"
42
43MODULE_AUTHOR("Steve Wise");
Vipul Pandyaf079af72013-03-14 05:08:58 +000044MODULE_DESCRIPTION("Chelsio T4/T5 RDMA Driver");
Steve Wisecfdda9d2010-04-21 15:30:06 -070045MODULE_LICENSE("Dual BSD/GPL");
46MODULE_VERSION(DRV_VERSION);
47
Vipul Pandya2c974782012-05-18 15:29:28 +053048struct uld_ctx {
49 struct list_head entry;
50 struct cxgb4_lld_info lldi;
51 struct c4iw_dev *dev;
52};
53
Steve Wise2f25e9a2011-05-09 22:06:23 -070054static LIST_HEAD(uld_ctx_list);
Steve Wisecfdda9d2010-04-21 15:30:06 -070055static DEFINE_MUTEX(dev_mutex);
56
57static struct dentry *c4iw_debugfs_root;
58
Steve Wise9e8d1fa32010-09-10 11:15:20 -050059struct c4iw_debugfs_data {
Steve Wisecfdda9d2010-04-21 15:30:06 -070060 struct c4iw_dev *devp;
61 char *buf;
62 int bufsize;
63 int pos;
64};
65
Steve Wise9e8d1fa32010-09-10 11:15:20 -050066static int count_idrs(int id, void *p, void *data)
Steve Wisecfdda9d2010-04-21 15:30:06 -070067{
Steve Wisecfdda9d2010-04-21 15:30:06 -070068 int *countp = data;
69
Steve Wisecfdda9d2010-04-21 15:30:06 -070070 *countp = *countp + 1;
71 return 0;
72}
73
Steve Wise9e8d1fa32010-09-10 11:15:20 -050074static ssize_t debugfs_read(struct file *file, char __user *buf, size_t count,
75 loff_t *ppos)
76{
77 struct c4iw_debugfs_data *d = file->private_data;
Steve Wise9e8d1fa32010-09-10 11:15:20 -050078
Steve Wise31609772010-09-29 18:21:33 +000079 return simple_read_from_buffer(buf, count, ppos, d->buf, d->pos);
Steve Wise9e8d1fa32010-09-10 11:15:20 -050080}
81
82static int dump_qp(int id, void *p, void *data)
Steve Wisecfdda9d2010-04-21 15:30:06 -070083{
84 struct c4iw_qp *qp = p;
Steve Wise9e8d1fa32010-09-10 11:15:20 -050085 struct c4iw_debugfs_data *qpd = data;
Steve Wisecfdda9d2010-04-21 15:30:06 -070086 int space;
87 int cc;
88
89 if (id != qp->wq.sq.qid)
90 return 0;
91
92 space = qpd->bufsize - qpd->pos - 1;
93 if (space == 0)
94 return 1;
95
96 if (qp->ep)
Steve Wisedb5d0402011-03-11 22:29:50 +000097 cc = snprintf(qpd->buf + qpd->pos, space,
98 "qp sq id %u rq id %u state %u onchip %u "
Steve Wisecfdda9d2010-04-21 15:30:06 -070099 "ep tid %u state %u %pI4:%u->%pI4:%u\n",
Steve Wisedb5d0402011-03-11 22:29:50 +0000100 qp->wq.sq.qid, qp->wq.rq.qid, (int)qp->attr.state,
101 qp->wq.sq.flags & T4_SQ_ONCHIP,
Steve Wisecfdda9d2010-04-21 15:30:06 -0700102 qp->ep->hwtid, (int)qp->ep->com.state,
103 &qp->ep->com.local_addr.sin_addr.s_addr,
104 ntohs(qp->ep->com.local_addr.sin_port),
105 &qp->ep->com.remote_addr.sin_addr.s_addr,
106 ntohs(qp->ep->com.remote_addr.sin_port));
107 else
Steve Wisedb5d0402011-03-11 22:29:50 +0000108 cc = snprintf(qpd->buf + qpd->pos, space,
109 "qp sq id %u rq id %u state %u onchip %u\n",
110 qp->wq.sq.qid, qp->wq.rq.qid,
111 (int)qp->attr.state,
112 qp->wq.sq.flags & T4_SQ_ONCHIP);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700113 if (cc < space)
114 qpd->pos += cc;
115 return 0;
116}
117
118static int qp_release(struct inode *inode, struct file *file)
119{
Steve Wise9e8d1fa32010-09-10 11:15:20 -0500120 struct c4iw_debugfs_data *qpd = file->private_data;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700121 if (!qpd) {
122 printk(KERN_INFO "%s null qpd?\n", __func__);
123 return 0;
124 }
Vipul Pandyad716a2a2012-05-18 15:29:31 +0530125 vfree(qpd->buf);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700126 kfree(qpd);
127 return 0;
128}
129
130static int qp_open(struct inode *inode, struct file *file)
131{
Steve Wise9e8d1fa32010-09-10 11:15:20 -0500132 struct c4iw_debugfs_data *qpd;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700133 int ret = 0;
134 int count = 1;
135
136 qpd = kmalloc(sizeof *qpd, GFP_KERNEL);
137 if (!qpd) {
138 ret = -ENOMEM;
139 goto out;
140 }
141 qpd->devp = inode->i_private;
142 qpd->pos = 0;
143
144 spin_lock_irq(&qpd->devp->lock);
Steve Wise9e8d1fa32010-09-10 11:15:20 -0500145 idr_for_each(&qpd->devp->qpidr, count_idrs, &count);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700146 spin_unlock_irq(&qpd->devp->lock);
147
148 qpd->bufsize = count * 128;
Vipul Pandyad716a2a2012-05-18 15:29:31 +0530149 qpd->buf = vmalloc(qpd->bufsize);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700150 if (!qpd->buf) {
151 ret = -ENOMEM;
152 goto err1;
153 }
154
155 spin_lock_irq(&qpd->devp->lock);
Steve Wise9e8d1fa32010-09-10 11:15:20 -0500156 idr_for_each(&qpd->devp->qpidr, dump_qp, qpd);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700157 spin_unlock_irq(&qpd->devp->lock);
158
159 qpd->buf[qpd->pos++] = 0;
160 file->private_data = qpd;
161 goto out;
162err1:
163 kfree(qpd);
164out:
165 return ret;
166}
167
Steve Wisecfdda9d2010-04-21 15:30:06 -0700168static const struct file_operations qp_debugfs_fops = {
169 .owner = THIS_MODULE,
170 .open = qp_open,
171 .release = qp_release,
Steve Wise9e8d1fa32010-09-10 11:15:20 -0500172 .read = debugfs_read,
Steve Wise8bbac892010-09-29 14:11:12 +0000173 .llseek = default_llseek,
Steve Wise9e8d1fa32010-09-10 11:15:20 -0500174};
175
176static int dump_stag(int id, void *p, void *data)
177{
178 struct c4iw_debugfs_data *stagd = data;
179 int space;
180 int cc;
181
182 space = stagd->bufsize - stagd->pos - 1;
183 if (space == 0)
184 return 1;
185
186 cc = snprintf(stagd->buf + stagd->pos, space, "0x%x\n", id<<8);
187 if (cc < space)
188 stagd->pos += cc;
189 return 0;
190}
191
192static int stag_release(struct inode *inode, struct file *file)
193{
194 struct c4iw_debugfs_data *stagd = file->private_data;
195 if (!stagd) {
196 printk(KERN_INFO "%s null stagd?\n", __func__);
197 return 0;
198 }
199 kfree(stagd->buf);
200 kfree(stagd);
201 return 0;
202}
203
204static int stag_open(struct inode *inode, struct file *file)
205{
206 struct c4iw_debugfs_data *stagd;
207 int ret = 0;
208 int count = 1;
209
210 stagd = kmalloc(sizeof *stagd, GFP_KERNEL);
211 if (!stagd) {
212 ret = -ENOMEM;
213 goto out;
214 }
215 stagd->devp = inode->i_private;
216 stagd->pos = 0;
217
218 spin_lock_irq(&stagd->devp->lock);
219 idr_for_each(&stagd->devp->mmidr, count_idrs, &count);
220 spin_unlock_irq(&stagd->devp->lock);
221
222 stagd->bufsize = count * sizeof("0x12345678\n");
223 stagd->buf = kmalloc(stagd->bufsize, GFP_KERNEL);
224 if (!stagd->buf) {
225 ret = -ENOMEM;
226 goto err1;
227 }
228
229 spin_lock_irq(&stagd->devp->lock);
230 idr_for_each(&stagd->devp->mmidr, dump_stag, stagd);
231 spin_unlock_irq(&stagd->devp->lock);
232
233 stagd->buf[stagd->pos++] = 0;
234 file->private_data = stagd;
235 goto out;
236err1:
237 kfree(stagd);
238out:
239 return ret;
240}
241
242static const struct file_operations stag_debugfs_fops = {
243 .owner = THIS_MODULE,
244 .open = stag_open,
245 .release = stag_release,
246 .read = debugfs_read,
Steve Wise8bbac892010-09-29 14:11:12 +0000247 .llseek = default_llseek,
Steve Wisecfdda9d2010-04-21 15:30:06 -0700248};
249
Vipul Pandya422eea02012-05-18 15:29:30 +0530250static char *db_state_str[] = {"NORMAL", "FLOW_CONTROL", "RECOVERY"};
251
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530252static int stats_show(struct seq_file *seq, void *v)
253{
254 struct c4iw_dev *dev = seq->private;
255
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530256 seq_printf(seq, " Object: %10s %10s %10s %10s\n", "Total", "Current",
257 "Max", "Fail");
258 seq_printf(seq, " PDID: %10llu %10llu %10llu %10llu\n",
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530259 dev->rdev.stats.pd.total, dev->rdev.stats.pd.cur,
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530260 dev->rdev.stats.pd.max, dev->rdev.stats.pd.fail);
261 seq_printf(seq, " QID: %10llu %10llu %10llu %10llu\n",
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530262 dev->rdev.stats.qid.total, dev->rdev.stats.qid.cur,
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530263 dev->rdev.stats.qid.max, dev->rdev.stats.qid.fail);
264 seq_printf(seq, " TPTMEM: %10llu %10llu %10llu %10llu\n",
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530265 dev->rdev.stats.stag.total, dev->rdev.stats.stag.cur,
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530266 dev->rdev.stats.stag.max, dev->rdev.stats.stag.fail);
267 seq_printf(seq, " PBLMEM: %10llu %10llu %10llu %10llu\n",
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530268 dev->rdev.stats.pbl.total, dev->rdev.stats.pbl.cur,
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530269 dev->rdev.stats.pbl.max, dev->rdev.stats.pbl.fail);
270 seq_printf(seq, " RQTMEM: %10llu %10llu %10llu %10llu\n",
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530271 dev->rdev.stats.rqt.total, dev->rdev.stats.rqt.cur,
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530272 dev->rdev.stats.rqt.max, dev->rdev.stats.rqt.fail);
273 seq_printf(seq, " OCQPMEM: %10llu %10llu %10llu %10llu\n",
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530274 dev->rdev.stats.ocqp.total, dev->rdev.stats.ocqp.cur,
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530275 dev->rdev.stats.ocqp.max, dev->rdev.stats.ocqp.fail);
Vipul Pandya2c974782012-05-18 15:29:28 +0530276 seq_printf(seq, " DB FULL: %10llu\n", dev->rdev.stats.db_full);
277 seq_printf(seq, " DB EMPTY: %10llu\n", dev->rdev.stats.db_empty);
278 seq_printf(seq, " DB DROP: %10llu\n", dev->rdev.stats.db_drop);
Vipul Pandya422eea02012-05-18 15:29:30 +0530279 seq_printf(seq, " DB State: %s Transitions %llu\n",
280 db_state_str[dev->db_state],
281 dev->rdev.stats.db_state_transitions);
Vipul Pandya1cab7752012-12-10 09:30:55 +0000282 seq_printf(seq, "TCAM_FULL: %10llu\n", dev->rdev.stats.tcam_full);
Vipul Pandya793dad92012-12-10 09:30:56 +0000283 seq_printf(seq, "ACT_OFLD_CONN_FAILS: %10llu\n",
284 dev->rdev.stats.act_ofld_conn_fails);
285 seq_printf(seq, "PAS_OFLD_CONN_FAILS: %10llu\n",
286 dev->rdev.stats.pas_ofld_conn_fails);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530287 return 0;
288}
289
290static int stats_open(struct inode *inode, struct file *file)
291{
292 return single_open(file, stats_show, inode->i_private);
293}
294
295static ssize_t stats_clear(struct file *file, const char __user *buf,
296 size_t count, loff_t *pos)
297{
298 struct c4iw_dev *dev = ((struct seq_file *)file->private_data)->private;
299
300 mutex_lock(&dev->rdev.stats.lock);
301 dev->rdev.stats.pd.max = 0;
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530302 dev->rdev.stats.pd.fail = 0;
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530303 dev->rdev.stats.qid.max = 0;
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530304 dev->rdev.stats.qid.fail = 0;
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530305 dev->rdev.stats.stag.max = 0;
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530306 dev->rdev.stats.stag.fail = 0;
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530307 dev->rdev.stats.pbl.max = 0;
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530308 dev->rdev.stats.pbl.fail = 0;
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530309 dev->rdev.stats.rqt.max = 0;
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530310 dev->rdev.stats.rqt.fail = 0;
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530311 dev->rdev.stats.ocqp.max = 0;
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530312 dev->rdev.stats.ocqp.fail = 0;
Vipul Pandya2c974782012-05-18 15:29:28 +0530313 dev->rdev.stats.db_full = 0;
314 dev->rdev.stats.db_empty = 0;
315 dev->rdev.stats.db_drop = 0;
Vipul Pandya422eea02012-05-18 15:29:30 +0530316 dev->rdev.stats.db_state_transitions = 0;
Vipul Pandya793dad92012-12-10 09:30:56 +0000317 dev->rdev.stats.tcam_full = 0;
318 dev->rdev.stats.act_ofld_conn_fails = 0;
319 dev->rdev.stats.pas_ofld_conn_fails = 0;
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530320 mutex_unlock(&dev->rdev.stats.lock);
321 return count;
322}
323
324static const struct file_operations stats_debugfs_fops = {
325 .owner = THIS_MODULE,
326 .open = stats_open,
327 .release = single_release,
328 .read = seq_read,
329 .llseek = seq_lseek,
330 .write = stats_clear,
331};
332
Vipul Pandya793dad92012-12-10 09:30:56 +0000333static int dump_ep(int id, void *p, void *data)
334{
335 struct c4iw_ep *ep = p;
336 struct c4iw_debugfs_data *epd = data;
337 int space;
338 int cc;
339
340 space = epd->bufsize - epd->pos - 1;
341 if (space == 0)
342 return 1;
343
344 cc = snprintf(epd->buf + epd->pos, space,
345 "ep %p cm_id %p qp %p state %d flags 0x%lx history 0x%lx "
346 "hwtid %d atid %d %pI4:%d <-> %pI4:%d\n",
347 ep, ep->com.cm_id, ep->com.qp, (int)ep->com.state,
348 ep->com.flags, ep->com.history, ep->hwtid, ep->atid,
349 &ep->com.local_addr.sin_addr.s_addr,
350 ntohs(ep->com.local_addr.sin_port),
351 &ep->com.remote_addr.sin_addr.s_addr,
352 ntohs(ep->com.remote_addr.sin_port));
353 if (cc < space)
354 epd->pos += cc;
355 return 0;
356}
357
358static int dump_listen_ep(int id, void *p, void *data)
359{
360 struct c4iw_listen_ep *ep = p;
361 struct c4iw_debugfs_data *epd = data;
362 int space;
363 int cc;
364
365 space = epd->bufsize - epd->pos - 1;
366 if (space == 0)
367 return 1;
368
369 cc = snprintf(epd->buf + epd->pos, space,
370 "ep %p cm_id %p state %d flags 0x%lx stid %d backlog %d "
371 "%pI4:%d\n", ep, ep->com.cm_id, (int)ep->com.state,
372 ep->com.flags, ep->stid, ep->backlog,
373 &ep->com.local_addr.sin_addr.s_addr,
374 ntohs(ep->com.local_addr.sin_port));
375 if (cc < space)
376 epd->pos += cc;
377 return 0;
378}
379
380static int ep_release(struct inode *inode, struct file *file)
381{
382 struct c4iw_debugfs_data *epd = file->private_data;
383 if (!epd) {
384 pr_info("%s null qpd?\n", __func__);
385 return 0;
386 }
387 vfree(epd->buf);
388 kfree(epd);
389 return 0;
390}
391
392static int ep_open(struct inode *inode, struct file *file)
393{
394 struct c4iw_debugfs_data *epd;
395 int ret = 0;
396 int count = 1;
397
398 epd = kmalloc(sizeof(*epd), GFP_KERNEL);
399 if (!epd) {
400 ret = -ENOMEM;
401 goto out;
402 }
403 epd->devp = inode->i_private;
404 epd->pos = 0;
405
406 spin_lock_irq(&epd->devp->lock);
407 idr_for_each(&epd->devp->hwtid_idr, count_idrs, &count);
408 idr_for_each(&epd->devp->atid_idr, count_idrs, &count);
409 idr_for_each(&epd->devp->stid_idr, count_idrs, &count);
410 spin_unlock_irq(&epd->devp->lock);
411
412 epd->bufsize = count * 160;
413 epd->buf = vmalloc(epd->bufsize);
414 if (!epd->buf) {
415 ret = -ENOMEM;
416 goto err1;
417 }
418
419 spin_lock_irq(&epd->devp->lock);
420 idr_for_each(&epd->devp->hwtid_idr, dump_ep, epd);
421 idr_for_each(&epd->devp->atid_idr, dump_ep, epd);
422 idr_for_each(&epd->devp->stid_idr, dump_listen_ep, epd);
423 spin_unlock_irq(&epd->devp->lock);
424
425 file->private_data = epd;
426 goto out;
427err1:
428 kfree(epd);
429out:
430 return ret;
431}
432
433static const struct file_operations ep_debugfs_fops = {
434 .owner = THIS_MODULE,
435 .open = ep_open,
436 .release = ep_release,
437 .read = debugfs_read,
438};
439
Steve Wisecfdda9d2010-04-21 15:30:06 -0700440static int setup_debugfs(struct c4iw_dev *devp)
441{
442 struct dentry *de;
443
444 if (!devp->debugfs_root)
445 return -1;
446
447 de = debugfs_create_file("qps", S_IWUSR, devp->debugfs_root,
448 (void *)devp, &qp_debugfs_fops);
449 if (de && de->d_inode)
450 de->d_inode->i_size = 4096;
Steve Wise9e8d1fa32010-09-10 11:15:20 -0500451
452 de = debugfs_create_file("stags", S_IWUSR, devp->debugfs_root,
453 (void *)devp, &stag_debugfs_fops);
454 if (de && de->d_inode)
455 de->d_inode->i_size = 4096;
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530456
457 de = debugfs_create_file("stats", S_IWUSR, devp->debugfs_root,
458 (void *)devp, &stats_debugfs_fops);
459 if (de && de->d_inode)
460 de->d_inode->i_size = 4096;
461
Vipul Pandya793dad92012-12-10 09:30:56 +0000462 de = debugfs_create_file("eps", S_IWUSR, devp->debugfs_root,
463 (void *)devp, &ep_debugfs_fops);
464 if (de && de->d_inode)
465 de->d_inode->i_size = 4096;
466
Steve Wisecfdda9d2010-04-21 15:30:06 -0700467 return 0;
468}
469
470void c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
471 struct c4iw_dev_ucontext *uctx)
472{
473 struct list_head *pos, *nxt;
474 struct c4iw_qid_list *entry;
475
476 mutex_lock(&uctx->lock);
477 list_for_each_safe(pos, nxt, &uctx->qpids) {
478 entry = list_entry(pos, struct c4iw_qid_list, entry);
479 list_del_init(&entry->entry);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530480 if (!(entry->qid & rdev->qpmask)) {
Vipul Pandyaec3eead2012-05-18 15:29:32 +0530481 c4iw_put_resource(&rdev->resource.qid_table,
482 entry->qid);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530483 mutex_lock(&rdev->stats.lock);
484 rdev->stats.qid.cur -= rdev->qpmask + 1;
485 mutex_unlock(&rdev->stats.lock);
486 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700487 kfree(entry);
488 }
489
490 list_for_each_safe(pos, nxt, &uctx->qpids) {
491 entry = list_entry(pos, struct c4iw_qid_list, entry);
492 list_del_init(&entry->entry);
493 kfree(entry);
494 }
495 mutex_unlock(&uctx->lock);
496}
497
498void c4iw_init_dev_ucontext(struct c4iw_rdev *rdev,
499 struct c4iw_dev_ucontext *uctx)
500{
501 INIT_LIST_HEAD(&uctx->qpids);
502 INIT_LIST_HEAD(&uctx->cqids);
503 mutex_init(&uctx->lock);
504}
505
506/* Caller takes care of locking if needed */
507static int c4iw_rdev_open(struct c4iw_rdev *rdev)
508{
509 int err;
510
511 c4iw_init_dev_ucontext(rdev, &rdev->uctx);
512
513 /*
514 * qpshift is the number of bits to shift the qpid left in order
515 * to get the correct address of the doorbell for that qp.
516 */
517 rdev->qpshift = PAGE_SHIFT - ilog2(rdev->lldi.udb_density);
518 rdev->qpmask = rdev->lldi.udb_density - 1;
519 rdev->cqshift = PAGE_SHIFT - ilog2(rdev->lldi.ucq_density);
520 rdev->cqmask = rdev->lldi.ucq_density - 1;
521 PDBG("%s dev %s stag start 0x%0x size 0x%0x num stags %d "
Steve Wise93fb72e2010-06-23 15:46:55 +0000522 "pbl start 0x%0x size 0x%0x rq start 0x%0x size 0x%0x "
523 "qp qid start %u size %u cq qid start %u size %u\n",
Steve Wisecfdda9d2010-04-21 15:30:06 -0700524 __func__, pci_name(rdev->lldi.pdev), rdev->lldi.vr->stag.start,
525 rdev->lldi.vr->stag.size, c4iw_num_stags(rdev),
526 rdev->lldi.vr->pbl.start,
527 rdev->lldi.vr->pbl.size, rdev->lldi.vr->rq.start,
Steve Wise93fb72e2010-06-23 15:46:55 +0000528 rdev->lldi.vr->rq.size,
529 rdev->lldi.vr->qp.start,
530 rdev->lldi.vr->qp.size,
531 rdev->lldi.vr->cq.start,
532 rdev->lldi.vr->cq.size);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700533 PDBG("udb len 0x%x udb base %p db_reg %p gts_reg %p qpshift %lu "
534 "qpmask 0x%x cqshift %lu cqmask 0x%x\n",
535 (unsigned)pci_resource_len(rdev->lldi.pdev, 2),
Stefan Haskob23523d2012-12-22 02:29:21 +0000536 (void *)(unsigned long)pci_resource_start(rdev->lldi.pdev, 2),
Steve Wisecfdda9d2010-04-21 15:30:06 -0700537 rdev->lldi.db_reg,
538 rdev->lldi.gts_reg,
539 rdev->qpshift, rdev->qpmask,
540 rdev->cqshift, rdev->cqmask);
541
542 if (c4iw_num_stags(rdev) == 0) {
543 err = -EINVAL;
544 goto err1;
545 }
546
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530547 rdev->stats.pd.total = T4_MAX_NUM_PD;
548 rdev->stats.stag.total = rdev->lldi.vr->stag.size;
549 rdev->stats.pbl.total = rdev->lldi.vr->pbl.size;
550 rdev->stats.rqt.total = rdev->lldi.vr->rq.size;
551 rdev->stats.ocqp.total = rdev->lldi.vr->ocq.size;
552 rdev->stats.qid.total = rdev->lldi.vr->qp.size;
553
Steve Wisecfdda9d2010-04-21 15:30:06 -0700554 err = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);
555 if (err) {
556 printk(KERN_ERR MOD "error %d initializing resources\n", err);
557 goto err1;
558 }
559 err = c4iw_pblpool_create(rdev);
560 if (err) {
561 printk(KERN_ERR MOD "error %d initializing pbl pool\n", err);
562 goto err2;
563 }
564 err = c4iw_rqtpool_create(rdev);
565 if (err) {
566 printk(KERN_ERR MOD "error %d initializing rqt pool\n", err);
567 goto err3;
568 }
Steve Wisec6d7b262010-09-13 11:23:57 -0500569 err = c4iw_ocqp_pool_create(rdev);
570 if (err) {
571 printk(KERN_ERR MOD "error %d initializing ocqp pool\n", err);
572 goto err4;
573 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700574 return 0;
Steve Wisec6d7b262010-09-13 11:23:57 -0500575err4:
576 c4iw_rqtpool_destroy(rdev);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700577err3:
578 c4iw_pblpool_destroy(rdev);
579err2:
580 c4iw_destroy_resource(&rdev->resource);
581err1:
582 return err;
583}
584
585static void c4iw_rdev_close(struct c4iw_rdev *rdev)
586{
587 c4iw_pblpool_destroy(rdev);
588 c4iw_rqtpool_destroy(rdev);
589 c4iw_destroy_resource(&rdev->resource);
590}
591
Steve Wise9efe10a2011-10-06 09:32:44 -0700592static void c4iw_dealloc(struct uld_ctx *ctx)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700593{
Steve Wise2f25e9a2011-05-09 22:06:23 -0700594 c4iw_rdev_close(&ctx->dev->rdev);
595 idr_destroy(&ctx->dev->cqidr);
596 idr_destroy(&ctx->dev->qpidr);
597 idr_destroy(&ctx->dev->mmidr);
Vipul Pandya793dad92012-12-10 09:30:56 +0000598 idr_destroy(&ctx->dev->hwtid_idr);
599 idr_destroy(&ctx->dev->stid_idr);
600 idr_destroy(&ctx->dev->atid_idr);
Steve Wise2f25e9a2011-05-09 22:06:23 -0700601 iounmap(ctx->dev->rdev.oc_mw_kva);
602 ib_dealloc_device(&ctx->dev->ibdev);
603 ctx->dev = NULL;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700604}
605
Steve Wise9efe10a2011-10-06 09:32:44 -0700606static void c4iw_remove(struct uld_ctx *ctx)
607{
608 PDBG("%s c4iw_dev %p\n", __func__, ctx->dev);
609 c4iw_unregister_device(ctx->dev);
610 c4iw_dealloc(ctx);
611}
612
613static int rdma_supported(const struct cxgb4_lld_info *infop)
614{
615 return infop->vr->stag.size > 0 && infop->vr->pbl.size > 0 &&
616 infop->vr->rq.size > 0 && infop->vr->qp.size > 0 &&
Vipul Pandyaf079af72013-03-14 05:08:58 +0000617 infop->vr->cq.size > 0;
Steve Wise9efe10a2011-10-06 09:32:44 -0700618}
619
Steve Wisecfdda9d2010-04-21 15:30:06 -0700620static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
621{
622 struct c4iw_dev *devp;
623 int ret;
624
Steve Wise9efe10a2011-10-06 09:32:44 -0700625 if (!rdma_supported(infop)) {
626 printk(KERN_INFO MOD "%s: RDMA not supported on this device.\n",
627 pci_name(infop->pdev));
628 return ERR_PTR(-ENOSYS);
629 }
Vipul Pandyaf079af72013-03-14 05:08:58 +0000630 if (!ocqp_supported(infop))
631 pr_info("%s: On-Chip Queues not supported on this device.\n",
632 pci_name(infop->pdev));
633 if (!is_t4(infop->adapter_type))
634 db_fc_threshold = 100000;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700635 devp = (struct c4iw_dev *)ib_alloc_device(sizeof(*devp));
636 if (!devp) {
637 printk(KERN_ERR MOD "Cannot allocate ib device\n");
Steve Wisebbe9a0a2011-05-09 22:06:22 -0700638 return ERR_PTR(-ENOMEM);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700639 }
640 devp->rdev.lldi = *infop;
641
Steve Wisec6d7b262010-09-13 11:23:57 -0500642 devp->rdev.oc_mw_pa = pci_resource_start(devp->rdev.lldi.pdev, 2) +
643 (pci_resource_len(devp->rdev.lldi.pdev, 2) -
644 roundup_pow_of_two(devp->rdev.lldi.vr->ocq.size));
645 devp->rdev.oc_mw_kva = ioremap_wc(devp->rdev.oc_mw_pa,
646 devp->rdev.lldi.vr->ocq.size);
647
Steve Wise2f25e9a2011-05-09 22:06:23 -0700648 PDBG(KERN_INFO MOD "ocq memory: "
Steve Wisec6d7b262010-09-13 11:23:57 -0500649 "hw_start 0x%x size %u mw_pa 0x%lx mw_kva %p\n",
650 devp->rdev.lldi.vr->ocq.start, devp->rdev.lldi.vr->ocq.size,
651 devp->rdev.oc_mw_pa, devp->rdev.oc_mw_kva);
652
Steve Wisecfdda9d2010-04-21 15:30:06 -0700653 ret = c4iw_rdev_open(&devp->rdev);
654 if (ret) {
Steve Wisecfdda9d2010-04-21 15:30:06 -0700655 printk(KERN_ERR MOD "Unable to open CXIO rdev err %d\n", ret);
656 ib_dealloc_device(&devp->ibdev);
Steve Wisebbe9a0a2011-05-09 22:06:22 -0700657 return ERR_PTR(ret);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700658 }
659
660 idr_init(&devp->cqidr);
661 idr_init(&devp->qpidr);
662 idr_init(&devp->mmidr);
Vipul Pandya793dad92012-12-10 09:30:56 +0000663 idr_init(&devp->hwtid_idr);
664 idr_init(&devp->stid_idr);
665 idr_init(&devp->atid_idr);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700666 spin_lock_init(&devp->lock);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530667 mutex_init(&devp->rdev.stats.lock);
Vipul Pandya2c974782012-05-18 15:29:28 +0530668 mutex_init(&devp->db_mutex);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700669
Steve Wisecfdda9d2010-04-21 15:30:06 -0700670 if (c4iw_debugfs_root) {
671 devp->debugfs_root = debugfs_create_dir(
672 pci_name(devp->rdev.lldi.pdev),
673 c4iw_debugfs_root);
674 setup_debugfs(devp);
675 }
676 return devp;
677}
678
679static void *c4iw_uld_add(const struct cxgb4_lld_info *infop)
680{
Steve Wise2f25e9a2011-05-09 22:06:23 -0700681 struct uld_ctx *ctx;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700682 static int vers_printed;
683 int i;
684
685 if (!vers_printed++)
Vipul Pandyaf079af72013-03-14 05:08:58 +0000686 pr_info("Chelsio T4/T5 RDMA Driver - version %s\n",
687 DRV_VERSION);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700688
Steve Wise2f25e9a2011-05-09 22:06:23 -0700689 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
690 if (!ctx) {
691 ctx = ERR_PTR(-ENOMEM);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700692 goto out;
Steve Wise2f25e9a2011-05-09 22:06:23 -0700693 }
694 ctx->lldi = *infop;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700695
696 PDBG("%s found device %s nchan %u nrxq %u ntxq %u nports %u\n",
Steve Wise2f25e9a2011-05-09 22:06:23 -0700697 __func__, pci_name(ctx->lldi.pdev),
698 ctx->lldi.nchan, ctx->lldi.nrxq,
699 ctx->lldi.ntxq, ctx->lldi.nports);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700700
Steve Wise2f25e9a2011-05-09 22:06:23 -0700701 mutex_lock(&dev_mutex);
702 list_add_tail(&ctx->entry, &uld_ctx_list);
703 mutex_unlock(&dev_mutex);
704
705 for (i = 0; i < ctx->lldi.nrxq; i++)
706 PDBG("rxqid[%u] %u\n", i, ctx->lldi.rxq_ids[i]);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700707out:
Steve Wise2f25e9a2011-05-09 22:06:23 -0700708 return ctx;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700709}
710
Vipul Pandya1cab7752012-12-10 09:30:55 +0000711static inline struct sk_buff *copy_gl_to_skb_pkt(const struct pkt_gl *gl,
712 const __be64 *rsp,
713 u32 pktshift)
714{
715 struct sk_buff *skb;
716
717 /*
718 * Allocate space for cpl_pass_accept_req which will be synthesized by
719 * driver. Once the driver synthesizes the request the skb will go
720 * through the regular cpl_pass_accept_req processing.
721 * The math here assumes sizeof cpl_pass_accept_req >= sizeof
722 * cpl_rx_pkt.
723 */
724 skb = alloc_skb(gl->tot_len + sizeof(struct cpl_pass_accept_req) +
725 sizeof(struct rss_header) - pktshift, GFP_ATOMIC);
726 if (unlikely(!skb))
727 return NULL;
728
729 __skb_put(skb, gl->tot_len + sizeof(struct cpl_pass_accept_req) +
730 sizeof(struct rss_header) - pktshift);
731
732 /*
733 * This skb will contain:
734 * rss_header from the rspq descriptor (1 flit)
735 * cpl_rx_pkt struct from the rspq descriptor (2 flits)
736 * space for the difference between the size of an
737 * rx_pkt and pass_accept_req cpl (1 flit)
738 * the packet data from the gl
739 */
740 skb_copy_to_linear_data(skb, rsp, sizeof(struct cpl_pass_accept_req) +
741 sizeof(struct rss_header));
742 skb_copy_to_linear_data_offset(skb, sizeof(struct rss_header) +
743 sizeof(struct cpl_pass_accept_req),
744 gl->va + pktshift,
745 gl->tot_len - pktshift);
746 return skb;
747}
748
749static inline int recv_rx_pkt(struct c4iw_dev *dev, const struct pkt_gl *gl,
750 const __be64 *rsp)
751{
752 unsigned int opcode = *(u8 *)rsp;
753 struct sk_buff *skb;
754
755 if (opcode != CPL_RX_PKT)
756 goto out;
757
758 skb = copy_gl_to_skb_pkt(gl , rsp, dev->rdev.lldi.sge_pktshift);
759 if (skb == NULL)
760 goto out;
761
762 if (c4iw_handlers[opcode] == NULL) {
763 pr_info("%s no handler opcode 0x%x...\n", __func__,
764 opcode);
765 kfree_skb(skb);
766 goto out;
767 }
768 c4iw_handlers[opcode](dev, skb);
769 return 1;
770out:
771 return 0;
772}
773
Steve Wisecfdda9d2010-04-21 15:30:06 -0700774static int c4iw_uld_rx_handler(void *handle, const __be64 *rsp,
775 const struct pkt_gl *gl)
776{
Steve Wise2f25e9a2011-05-09 22:06:23 -0700777 struct uld_ctx *ctx = handle;
778 struct c4iw_dev *dev = ctx->dev;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700779 struct sk_buff *skb;
Vipul Pandya1cab7752012-12-10 09:30:55 +0000780 u8 opcode;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700781
782 if (gl == NULL) {
783 /* omit RSS and rsp_ctrl at end of descriptor */
784 unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;
785
786 skb = alloc_skb(256, GFP_ATOMIC);
787 if (!skb)
788 goto nomem;
789 __skb_put(skb, len);
790 skb_copy_to_linear_data(skb, &rsp[1], len);
791 } else if (gl == CXGB4_MSG_AN) {
792 const struct rsp_ctrl *rc = (void *)rsp;
793
794 u32 qid = be32_to_cpu(rc->pldbuflen_qid);
795 c4iw_ev_handler(dev, qid);
796 return 0;
Vipul Pandya1cab7752012-12-10 09:30:55 +0000797 } else if (unlikely(*(u8 *)rsp != *(u8 *)gl->va)) {
798 if (recv_rx_pkt(dev, gl, rsp))
799 return 0;
800
801 pr_info("%s: unexpected FL contents at %p, " \
802 "RSS %#llx, FL %#llx, len %u\n",
803 pci_name(ctx->lldi.pdev), gl->va,
804 (unsigned long long)be64_to_cpu(*rsp),
Vipul Pandyaef5d6352013-01-07 13:12:00 +0000805 (unsigned long long)be64_to_cpu(
806 *(__force __be64 *)gl->va),
Vipul Pandya1cab7752012-12-10 09:30:55 +0000807 gl->tot_len);
808
809 return 0;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700810 } else {
Steve Wiseda411ba2010-10-18 15:16:45 +0000811 skb = cxgb4_pktgl_to_skb(gl, 128, 128);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700812 if (unlikely(!skb))
813 goto nomem;
814 }
815
Vipul Pandya1cab7752012-12-10 09:30:55 +0000816 opcode = *(u8 *)rsp;
Steve Wisecfdda9d2010-04-21 15:30:06 -0700817 if (c4iw_handlers[opcode])
818 c4iw_handlers[opcode](dev, skb);
819 else
Vipul Pandya1cab7752012-12-10 09:30:55 +0000820 pr_info("%s no handler opcode 0x%x...\n", __func__,
Steve Wisecfdda9d2010-04-21 15:30:06 -0700821 opcode);
822
823 return 0;
824nomem:
825 return -1;
826}
827
828static int c4iw_uld_state_change(void *handle, enum cxgb4_state new_state)
829{
Steve Wise2f25e9a2011-05-09 22:06:23 -0700830 struct uld_ctx *ctx = handle;
Steve Wise1c01c532010-05-20 16:57:32 -0500831
Steve Wisecfdda9d2010-04-21 15:30:06 -0700832 PDBG("%s new_state %u\n", __func__, new_state);
Steve Wise1c01c532010-05-20 16:57:32 -0500833 switch (new_state) {
834 case CXGB4_STATE_UP:
Steve Wise2f25e9a2011-05-09 22:06:23 -0700835 printk(KERN_INFO MOD "%s: Up\n", pci_name(ctx->lldi.pdev));
836 if (!ctx->dev) {
Steve Wise9efe10a2011-10-06 09:32:44 -0700837 int ret;
Steve Wise2f25e9a2011-05-09 22:06:23 -0700838
839 ctx->dev = c4iw_alloc(&ctx->lldi);
Steve Wise9efe10a2011-10-06 09:32:44 -0700840 if (IS_ERR(ctx->dev)) {
841 printk(KERN_ERR MOD
842 "%s: initialization failed: %ld\n",
843 pci_name(ctx->lldi.pdev),
844 PTR_ERR(ctx->dev));
845 ctx->dev = NULL;
846 break;
847 }
848 ret = c4iw_register_device(ctx->dev);
849 if (ret) {
Steve Wise1c01c532010-05-20 16:57:32 -0500850 printk(KERN_ERR MOD
851 "%s: RDMA registration failed: %d\n",
Steve Wise2f25e9a2011-05-09 22:06:23 -0700852 pci_name(ctx->lldi.pdev), ret);
Steve Wise9efe10a2011-10-06 09:32:44 -0700853 c4iw_dealloc(ctx);
854 }
Steve Wise1c01c532010-05-20 16:57:32 -0500855 }
856 break;
857 case CXGB4_STATE_DOWN:
858 printk(KERN_INFO MOD "%s: Down\n",
Steve Wise2f25e9a2011-05-09 22:06:23 -0700859 pci_name(ctx->lldi.pdev));
860 if (ctx->dev)
861 c4iw_remove(ctx);
Steve Wise1c01c532010-05-20 16:57:32 -0500862 break;
863 case CXGB4_STATE_START_RECOVERY:
864 printk(KERN_INFO MOD "%s: Fatal Error\n",
Steve Wise2f25e9a2011-05-09 22:06:23 -0700865 pci_name(ctx->lldi.pdev));
866 if (ctx->dev) {
Steve Wise767fbe82011-03-11 22:30:53 +0000867 struct ib_event event;
868
Steve Wise2f25e9a2011-05-09 22:06:23 -0700869 ctx->dev->rdev.flags |= T4_FATAL_ERROR;
Steve Wise767fbe82011-03-11 22:30:53 +0000870 memset(&event, 0, sizeof event);
871 event.event = IB_EVENT_DEVICE_FATAL;
Steve Wise2f25e9a2011-05-09 22:06:23 -0700872 event.device = &ctx->dev->ibdev;
Steve Wise767fbe82011-03-11 22:30:53 +0000873 ib_dispatch_event(&event);
Steve Wise2f25e9a2011-05-09 22:06:23 -0700874 c4iw_remove(ctx);
Steve Wise767fbe82011-03-11 22:30:53 +0000875 }
Steve Wise1c01c532010-05-20 16:57:32 -0500876 break;
877 case CXGB4_STATE_DETACH:
878 printk(KERN_INFO MOD "%s: Detach\n",
Steve Wise2f25e9a2011-05-09 22:06:23 -0700879 pci_name(ctx->lldi.pdev));
880 if (ctx->dev)
881 c4iw_remove(ctx);
Steve Wise1c01c532010-05-20 16:57:32 -0500882 break;
883 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700884 return 0;
885}
886
Vipul Pandya2c974782012-05-18 15:29:28 +0530887static int disable_qp_db(int id, void *p, void *data)
888{
889 struct c4iw_qp *qp = p;
890
891 t4_disable_wq_db(&qp->wq);
892 return 0;
893}
894
895static void stop_queues(struct uld_ctx *ctx)
896{
897 spin_lock_irq(&ctx->dev->lock);
Vipul Pandya422eea02012-05-18 15:29:30 +0530898 if (ctx->dev->db_state == NORMAL) {
899 ctx->dev->rdev.stats.db_state_transitions++;
900 ctx->dev->db_state = FLOW_CONTROL;
901 idr_for_each(&ctx->dev->qpidr, disable_qp_db, NULL);
902 }
Vipul Pandya2c974782012-05-18 15:29:28 +0530903 spin_unlock_irq(&ctx->dev->lock);
904}
905
906static int enable_qp_db(int id, void *p, void *data)
907{
908 struct c4iw_qp *qp = p;
909
910 t4_enable_wq_db(&qp->wq);
911 return 0;
912}
913
914static void resume_queues(struct uld_ctx *ctx)
915{
916 spin_lock_irq(&ctx->dev->lock);
Vipul Pandya422eea02012-05-18 15:29:30 +0530917 if (ctx->dev->qpcnt <= db_fc_threshold &&
918 ctx->dev->db_state == FLOW_CONTROL) {
919 ctx->dev->db_state = NORMAL;
920 ctx->dev->rdev.stats.db_state_transitions++;
921 idr_for_each(&ctx->dev->qpidr, enable_qp_db, NULL);
922 }
Vipul Pandya2c974782012-05-18 15:29:28 +0530923 spin_unlock_irq(&ctx->dev->lock);
924}
925
Vipul Pandya422eea02012-05-18 15:29:30 +0530926struct qp_list {
927 unsigned idx;
928 struct c4iw_qp **qps;
929};
930
931static int add_and_ref_qp(int id, void *p, void *data)
932{
933 struct qp_list *qp_listp = data;
934 struct c4iw_qp *qp = p;
935
936 c4iw_qp_add_ref(&qp->ibqp);
937 qp_listp->qps[qp_listp->idx++] = qp;
938 return 0;
939}
940
941static int count_qps(int id, void *p, void *data)
942{
943 unsigned *countp = data;
944 (*countp)++;
945 return 0;
946}
947
948static void deref_qps(struct qp_list qp_list)
949{
950 int idx;
951
952 for (idx = 0; idx < qp_list.idx; idx++)
953 c4iw_qp_rem_ref(&qp_list.qps[idx]->ibqp);
954}
955
956static void recover_lost_dbs(struct uld_ctx *ctx, struct qp_list *qp_list)
957{
958 int idx;
959 int ret;
960
961 for (idx = 0; idx < qp_list->idx; idx++) {
962 struct c4iw_qp *qp = qp_list->qps[idx];
963
964 ret = cxgb4_sync_txq_pidx(qp->rhp->rdev.lldi.ports[0],
965 qp->wq.sq.qid,
966 t4_sq_host_wq_pidx(&qp->wq),
967 t4_sq_wq_size(&qp->wq));
968 if (ret) {
969 printk(KERN_ERR MOD "%s: Fatal error - "
970 "DB overflow recovery failed - "
971 "error syncing SQ qid %u\n",
972 pci_name(ctx->lldi.pdev), qp->wq.sq.qid);
973 return;
974 }
975
976 ret = cxgb4_sync_txq_pidx(qp->rhp->rdev.lldi.ports[0],
977 qp->wq.rq.qid,
978 t4_rq_host_wq_pidx(&qp->wq),
979 t4_rq_wq_size(&qp->wq));
980
981 if (ret) {
982 printk(KERN_ERR MOD "%s: Fatal error - "
983 "DB overflow recovery failed - "
984 "error syncing RQ qid %u\n",
985 pci_name(ctx->lldi.pdev), qp->wq.rq.qid);
986 return;
987 }
988
989 /* Wait for the dbfifo to drain */
990 while (cxgb4_dbfifo_count(qp->rhp->rdev.lldi.ports[0], 1) > 0) {
991 set_current_state(TASK_UNINTERRUPTIBLE);
992 schedule_timeout(usecs_to_jiffies(10));
993 }
994 }
995}
996
997static void recover_queues(struct uld_ctx *ctx)
998{
999 int count = 0;
1000 struct qp_list qp_list;
1001 int ret;
1002
1003 /* lock out kernel db ringers */
1004 mutex_lock(&ctx->dev->db_mutex);
1005
1006 /* put all queues in to recovery mode */
1007 spin_lock_irq(&ctx->dev->lock);
1008 ctx->dev->db_state = RECOVERY;
1009 ctx->dev->rdev.stats.db_state_transitions++;
1010 idr_for_each(&ctx->dev->qpidr, disable_qp_db, NULL);
1011 spin_unlock_irq(&ctx->dev->lock);
1012
1013 /* slow everybody down */
1014 set_current_state(TASK_UNINTERRUPTIBLE);
1015 schedule_timeout(usecs_to_jiffies(1000));
1016
1017 /* Wait for the dbfifo to completely drain. */
1018 while (cxgb4_dbfifo_count(ctx->dev->rdev.lldi.ports[0], 1) > 0) {
1019 set_current_state(TASK_UNINTERRUPTIBLE);
1020 schedule_timeout(usecs_to_jiffies(10));
1021 }
1022
1023 /* flush the SGE contexts */
1024 ret = cxgb4_flush_eq_cache(ctx->dev->rdev.lldi.ports[0]);
1025 if (ret) {
1026 printk(KERN_ERR MOD "%s: Fatal error - DB overflow recovery failed\n",
1027 pci_name(ctx->lldi.pdev));
1028 goto out;
1029 }
1030
1031 /* Count active queues so we can build a list of queues to recover */
1032 spin_lock_irq(&ctx->dev->lock);
1033 idr_for_each(&ctx->dev->qpidr, count_qps, &count);
1034
1035 qp_list.qps = kzalloc(count * sizeof *qp_list.qps, GFP_ATOMIC);
1036 if (!qp_list.qps) {
1037 printk(KERN_ERR MOD "%s: Fatal error - DB overflow recovery failed\n",
1038 pci_name(ctx->lldi.pdev));
1039 spin_unlock_irq(&ctx->dev->lock);
1040 goto out;
1041 }
1042 qp_list.idx = 0;
1043
1044 /* add and ref each qp so it doesn't get freed */
1045 idr_for_each(&ctx->dev->qpidr, add_and_ref_qp, &qp_list);
1046
1047 spin_unlock_irq(&ctx->dev->lock);
1048
1049 /* now traverse the list in a safe context to recover the db state*/
1050 recover_lost_dbs(ctx, &qp_list);
1051
1052 /* we're almost done! deref the qps and clean up */
1053 deref_qps(qp_list);
1054 kfree(qp_list.qps);
1055
1056 /* Wait for the dbfifo to completely drain again */
1057 while (cxgb4_dbfifo_count(ctx->dev->rdev.lldi.ports[0], 1) > 0) {
1058 set_current_state(TASK_UNINTERRUPTIBLE);
1059 schedule_timeout(usecs_to_jiffies(10));
1060 }
1061
1062 /* resume the queues */
1063 spin_lock_irq(&ctx->dev->lock);
1064 if (ctx->dev->qpcnt > db_fc_threshold)
1065 ctx->dev->db_state = FLOW_CONTROL;
1066 else {
1067 ctx->dev->db_state = NORMAL;
1068 idr_for_each(&ctx->dev->qpidr, enable_qp_db, NULL);
1069 }
1070 ctx->dev->rdev.stats.db_state_transitions++;
1071 spin_unlock_irq(&ctx->dev->lock);
1072
1073out:
1074 /* start up kernel db ringers again */
1075 mutex_unlock(&ctx->dev->db_mutex);
1076}
1077
Vipul Pandya2c974782012-05-18 15:29:28 +05301078static int c4iw_uld_control(void *handle, enum cxgb4_control control, ...)
1079{
1080 struct uld_ctx *ctx = handle;
1081
1082 switch (control) {
1083 case CXGB4_CONTROL_DB_FULL:
1084 stop_queues(ctx);
1085 mutex_lock(&ctx->dev->rdev.stats.lock);
1086 ctx->dev->rdev.stats.db_full++;
1087 mutex_unlock(&ctx->dev->rdev.stats.lock);
1088 break;
1089 case CXGB4_CONTROL_DB_EMPTY:
1090 resume_queues(ctx);
1091 mutex_lock(&ctx->dev->rdev.stats.lock);
1092 ctx->dev->rdev.stats.db_empty++;
1093 mutex_unlock(&ctx->dev->rdev.stats.lock);
1094 break;
1095 case CXGB4_CONTROL_DB_DROP:
Vipul Pandya422eea02012-05-18 15:29:30 +05301096 recover_queues(ctx);
Vipul Pandya2c974782012-05-18 15:29:28 +05301097 mutex_lock(&ctx->dev->rdev.stats.lock);
1098 ctx->dev->rdev.stats.db_drop++;
1099 mutex_unlock(&ctx->dev->rdev.stats.lock);
1100 break;
1101 default:
1102 printk(KERN_WARNING MOD "%s: unknown control cmd %u\n",
1103 pci_name(ctx->lldi.pdev), control);
1104 break;
1105 }
1106 return 0;
1107}
1108
Steve Wisecfdda9d2010-04-21 15:30:06 -07001109static struct cxgb4_uld_info c4iw_uld_info = {
1110 .name = DRV_NAME,
1111 .add = c4iw_uld_add,
1112 .rx_handler = c4iw_uld_rx_handler,
1113 .state_change = c4iw_uld_state_change,
Vipul Pandya2c974782012-05-18 15:29:28 +05301114 .control = c4iw_uld_control,
Steve Wisecfdda9d2010-04-21 15:30:06 -07001115};
1116
1117static int __init c4iw_init_module(void)
1118{
1119 int err;
1120
1121 err = c4iw_cm_init();
1122 if (err)
1123 return err;
1124
1125 c4iw_debugfs_root = debugfs_create_dir(DRV_NAME, NULL);
1126 if (!c4iw_debugfs_root)
1127 printk(KERN_WARNING MOD
1128 "could not create debugfs entry, continuing\n");
1129
1130 cxgb4_register_uld(CXGB4_ULD_RDMA, &c4iw_uld_info);
1131
1132 return 0;
1133}
1134
1135static void __exit c4iw_exit_module(void)
1136{
Steve Wise2f25e9a2011-05-09 22:06:23 -07001137 struct uld_ctx *ctx, *tmp;
Steve Wisecfdda9d2010-04-21 15:30:06 -07001138
Steve Wisecfdda9d2010-04-21 15:30:06 -07001139 mutex_lock(&dev_mutex);
Steve Wise2f25e9a2011-05-09 22:06:23 -07001140 list_for_each_entry_safe(ctx, tmp, &uld_ctx_list, entry) {
1141 if (ctx->dev)
1142 c4iw_remove(ctx);
1143 kfree(ctx);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001144 }
1145 mutex_unlock(&dev_mutex);
Steve Wisefd388ce2010-05-20 16:57:27 -05001146 cxgb4_unregister_uld(CXGB4_ULD_RDMA);
Steve Wisecfdda9d2010-04-21 15:30:06 -07001147 c4iw_cm_term();
1148 debugfs_remove_recursive(c4iw_debugfs_root);
1149}
1150
1151module_init(c4iw_init_module);
1152module_exit(c4iw_exit_module);