device.c 37.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *	  copyright notice, this list of conditions and the following
 *	  disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *	  copyright notice, this list of conditions and the following
 *	  disclaimer in the documentation and/or other materials
 *	  provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/debugfs.h>
35
#include <linux/vmalloc.h>
36 37 38 39 40 41 42 43

#include <rdma/ib_verbs.h>

#include "iw_cxgb4.h"

#define DRV_VERSION "0.1"

MODULE_AUTHOR("Steve Wise");
44
MODULE_DESCRIPTION("Chelsio T4/T5 RDMA Driver");
45 46 47
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION(DRV_VERSION);

48 49 50 51 52 53 54 55 56 57
static int allow_db_fc_on_t5;
module_param(allow_db_fc_on_t5, int, 0644);
MODULE_PARM_DESC(allow_db_fc_on_t5,
		 "Allow DB Flow Control on T5 (default = 0)");

static int allow_db_coalescing_on_t5;
module_param(allow_db_coalescing_on_t5, int, 0644);
MODULE_PARM_DESC(allow_db_coalescing_on_t5,
		 "Allow DB Coalescing on T5 (default = 0)");

58 59 60 61 62 63
struct uld_ctx {
	struct list_head entry;
	struct cxgb4_lld_info lldi;
	struct c4iw_dev *dev;
};

64
static LIST_HEAD(uld_ctx_list);
65 66
static DEFINE_MUTEX(dev_mutex);

67 68 69 70
#define DB_FC_RESUME_SIZE 64
#define DB_FC_RESUME_DELAY 1
#define DB_FC_DRAIN_THRESH 0

71 72
static struct dentry *c4iw_debugfs_root;

73
struct c4iw_debugfs_data {
74 75 76 77 78 79
	struct c4iw_dev *devp;
	char *buf;
	int bufsize;
	int pos;
};

80 81 82 83 84 85 86 87 88 89
/* registered cxgb4 netlink callbacks */
static struct ibnl_client_cbs c4iw_nl_cb_table[] = {
	[RDMA_NL_IWPM_REG_PID] = {.dump = iwpm_register_pid_cb},
	[RDMA_NL_IWPM_ADD_MAPPING] = {.dump = iwpm_add_mapping_cb},
	[RDMA_NL_IWPM_QUERY_MAPPING] = {.dump = iwpm_add_and_query_mapping_cb},
	[RDMA_NL_IWPM_HANDLE_ERR] = {.dump = iwpm_mapping_error_cb},
	[RDMA_NL_IWPM_MAPINFO] = {.dump = iwpm_mapping_info_cb},
	[RDMA_NL_IWPM_MAPINFO_NUM] = {.dump = iwpm_ack_mapping_info_cb}
};

90
static int count_idrs(int id, void *p, void *data)
91 92 93 94 95 96 97
{
	int *countp = data;

	*countp = *countp + 1;
	return 0;
}

98 99 100 101 102
static ssize_t debugfs_read(struct file *file, char __user *buf, size_t count,
			    loff_t *ppos)
{
	struct c4iw_debugfs_data *d = file->private_data;

103
	return simple_read_from_buffer(buf, count, ppos, d->buf, d->pos);
104 105 106
}

static int dump_qp(int id, void *p, void *data)
107 108
{
	struct c4iw_qp *qp = p;
109
	struct c4iw_debugfs_data *qpd = data;
110 111 112 113 114 115 116 117 118 119
	int space;
	int cc;

	if (id != qp->wq.sq.qid)
		return 0;

	space = qpd->bufsize - qpd->pos - 1;
	if (space == 0)
		return 1;

120 121 122 123 124 125
	if (qp->ep) {
		if (qp->ep->com.local_addr.ss_family == AF_INET) {
			struct sockaddr_in *lsin = (struct sockaddr_in *)
				&qp->ep->com.local_addr;
			struct sockaddr_in *rsin = (struct sockaddr_in *)
				&qp->ep->com.remote_addr;
126 127 128 129
			struct sockaddr_in *mapped_lsin = (struct sockaddr_in *)
				&qp->ep->com.mapped_local_addr;
			struct sockaddr_in *mapped_rsin = (struct sockaddr_in *)
				&qp->ep->com.mapped_remote_addr;
130 131 132 133

			cc = snprintf(qpd->buf + qpd->pos, space,
				      "rc qp sq id %u rq id %u state %u "
				      "onchip %u ep tid %u state %u "
134
				      "%pI4:%u/%u->%pI4:%u/%u\n",
135 136 137 138 139
				      qp->wq.sq.qid, qp->wq.rq.qid,
				      (int)qp->attr.state,
				      qp->wq.sq.flags & T4_SQ_ONCHIP,
				      qp->ep->hwtid, (int)qp->ep->com.state,
				      &lsin->sin_addr, ntohs(lsin->sin_port),
140 141 142
				      ntohs(mapped_lsin->sin_port),
				      &rsin->sin_addr, ntohs(rsin->sin_port),
				      ntohs(mapped_rsin->sin_port));
143 144 145 146 147
		} else {
			struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)
				&qp->ep->com.local_addr;
			struct sockaddr_in6 *rsin6 = (struct sockaddr_in6 *)
				&qp->ep->com.remote_addr;
148 149 150 151 152 153
			struct sockaddr_in6 *mapped_lsin6 =
				(struct sockaddr_in6 *)
				&qp->ep->com.mapped_local_addr;
			struct sockaddr_in6 *mapped_rsin6 =
				(struct sockaddr_in6 *)
				&qp->ep->com.mapped_remote_addr;
154 155 156 157

			cc = snprintf(qpd->buf + qpd->pos, space,
				      "rc qp sq id %u rq id %u state %u "
				      "onchip %u ep tid %u state %u "
158
				      "%pI6:%u/%u->%pI6:%u/%u\n",
159 160 161 162 163 164
				      qp->wq.sq.qid, qp->wq.rq.qid,
				      (int)qp->attr.state,
				      qp->wq.sq.flags & T4_SQ_ONCHIP,
				      qp->ep->hwtid, (int)qp->ep->com.state,
				      &lsin6->sin6_addr,
				      ntohs(lsin6->sin6_port),
165
				      ntohs(mapped_lsin6->sin6_port),
166
				      &rsin6->sin6_addr,
167 168
				      ntohs(rsin6->sin6_port),
				      ntohs(mapped_rsin6->sin6_port));
169 170
		}
	} else
171 172 173 174 175
		cc = snprintf(qpd->buf + qpd->pos, space,
			     "qp sq id %u rq id %u state %u onchip %u\n",
			      qp->wq.sq.qid, qp->wq.rq.qid,
			      (int)qp->attr.state,
			      qp->wq.sq.flags & T4_SQ_ONCHIP);
176 177 178 179 180 181 182
	if (cc < space)
		qpd->pos += cc;
	return 0;
}

static int qp_release(struct inode *inode, struct file *file)
{
183
	struct c4iw_debugfs_data *qpd = file->private_data;
184 185 186 187
	if (!qpd) {
		printk(KERN_INFO "%s null qpd?\n", __func__);
		return 0;
	}
188
	vfree(qpd->buf);
189 190 191 192 193 194
	kfree(qpd);
	return 0;
}

static int qp_open(struct inode *inode, struct file *file)
{
195
	struct c4iw_debugfs_data *qpd;
196 197 198 199 200 201 202 203 204 205 206 207
	int ret = 0;
	int count = 1;

	qpd = kmalloc(sizeof *qpd, GFP_KERNEL);
	if (!qpd) {
		ret = -ENOMEM;
		goto out;
	}
	qpd->devp = inode->i_private;
	qpd->pos = 0;

	spin_lock_irq(&qpd->devp->lock);
208
	idr_for_each(&qpd->devp->qpidr, count_idrs, &count);
209 210 211
	spin_unlock_irq(&qpd->devp->lock);

	qpd->bufsize = count * 128;
212
	qpd->buf = vmalloc(qpd->bufsize);
213 214 215 216 217 218
	if (!qpd->buf) {
		ret = -ENOMEM;
		goto err1;
	}

	spin_lock_irq(&qpd->devp->lock);
219
	idr_for_each(&qpd->devp->qpidr, dump_qp, qpd);
220 221 222 223 224 225 226 227 228 229 230
	spin_unlock_irq(&qpd->devp->lock);

	qpd->buf[qpd->pos++] = 0;
	file->private_data = qpd;
	goto out;
err1:
	kfree(qpd);
out:
	return ret;
}

231 232 233 234 235
static const struct file_operations qp_debugfs_fops = {
	.owner   = THIS_MODULE,
	.open    = qp_open,
	.release = qp_release,
	.read    = debugfs_read,
236
	.llseek  = default_llseek,
237 238 239
};

static int dump_stag(int id, void *p, void *data)
240
{
241 242 243
	struct c4iw_debugfs_data *stagd = data;
	int space;
	int cc;
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	space = stagd->bufsize - stagd->pos - 1;
	if (space == 0)
		return 1;

	cc = snprintf(stagd->buf + stagd->pos, space, "0x%x\n", id<<8);
	if (cc < space)
		stagd->pos += cc;
	return 0;
}

static int stag_release(struct inode *inode, struct file *file)
{
	struct c4iw_debugfs_data *stagd = file->private_data;
	if (!stagd) {
		printk(KERN_INFO "%s null stagd?\n", __func__);
260
		return 0;
261 262 263 264 265
	}
	kfree(stagd->buf);
	kfree(stagd);
	return 0;
}
266

267 268 269 270 271
static int stag_open(struct inode *inode, struct file *file)
{
	struct c4iw_debugfs_data *stagd;
	int ret = 0;
	int count = 1;
272

273 274 275 276 277 278 279
	stagd = kmalloc(sizeof *stagd, GFP_KERNEL);
	if (!stagd) {
		ret = -ENOMEM;
		goto out;
	}
	stagd->devp = inode->i_private;
	stagd->pos = 0;
280

281 282 283 284 285 286 287 288 289
	spin_lock_irq(&stagd->devp->lock);
	idr_for_each(&stagd->devp->mmidr, count_idrs, &count);
	spin_unlock_irq(&stagd->devp->lock);

	stagd->bufsize = count * sizeof("0x12345678\n");
	stagd->buf = kmalloc(stagd->bufsize, GFP_KERNEL);
	if (!stagd->buf) {
		ret = -ENOMEM;
		goto err1;
290
	}
291 292 293 294 295 296 297 298 299 300 301 302

	spin_lock_irq(&stagd->devp->lock);
	idr_for_each(&stagd->devp->mmidr, dump_stag, stagd);
	spin_unlock_irq(&stagd->devp->lock);

	stagd->buf[stagd->pos++] = 0;
	file->private_data = stagd;
	goto out;
err1:
	kfree(stagd);
out:
	return ret;
303 304
}

305
static const struct file_operations stag_debugfs_fops = {
306
	.owner   = THIS_MODULE,
307 308 309
	.open    = stag_open,
	.release = stag_release,
	.read    = debugfs_read,
310
	.llseek  = default_llseek,
311 312
};

313
static char *db_state_str[] = {"NORMAL", "FLOW_CONTROL", "RECOVERY", "STOPPED"};
314

315 316 317 318
static int stats_show(struct seq_file *seq, void *v)
{
	struct c4iw_dev *dev = seq->private;

V
Vipul Pandya 已提交
319 320 321
	seq_printf(seq, "   Object: %10s %10s %10s %10s\n", "Total", "Current",
		   "Max", "Fail");
	seq_printf(seq, "     PDID: %10llu %10llu %10llu %10llu\n",
322
			dev->rdev.stats.pd.total, dev->rdev.stats.pd.cur,
V
Vipul Pandya 已提交
323 324
			dev->rdev.stats.pd.max, dev->rdev.stats.pd.fail);
	seq_printf(seq, "      QID: %10llu %10llu %10llu %10llu\n",
325
			dev->rdev.stats.qid.total, dev->rdev.stats.qid.cur,
V
Vipul Pandya 已提交
326 327
			dev->rdev.stats.qid.max, dev->rdev.stats.qid.fail);
	seq_printf(seq, "   TPTMEM: %10llu %10llu %10llu %10llu\n",
328
			dev->rdev.stats.stag.total, dev->rdev.stats.stag.cur,
V
Vipul Pandya 已提交
329 330
			dev->rdev.stats.stag.max, dev->rdev.stats.stag.fail);
	seq_printf(seq, "   PBLMEM: %10llu %10llu %10llu %10llu\n",
331
			dev->rdev.stats.pbl.total, dev->rdev.stats.pbl.cur,
V
Vipul Pandya 已提交
332 333
			dev->rdev.stats.pbl.max, dev->rdev.stats.pbl.fail);
	seq_printf(seq, "   RQTMEM: %10llu %10llu %10llu %10llu\n",
334
			dev->rdev.stats.rqt.total, dev->rdev.stats.rqt.cur,
V
Vipul Pandya 已提交
335 336
			dev->rdev.stats.rqt.max, dev->rdev.stats.rqt.fail);
	seq_printf(seq, "  OCQPMEM: %10llu %10llu %10llu %10llu\n",
337
			dev->rdev.stats.ocqp.total, dev->rdev.stats.ocqp.cur,
V
Vipul Pandya 已提交
338
			dev->rdev.stats.ocqp.max, dev->rdev.stats.ocqp.fail);
339 340 341
	seq_printf(seq, "  DB FULL: %10llu\n", dev->rdev.stats.db_full);
	seq_printf(seq, " DB EMPTY: %10llu\n", dev->rdev.stats.db_empty);
	seq_printf(seq, "  DB DROP: %10llu\n", dev->rdev.stats.db_drop);
342
	seq_printf(seq, " DB State: %s Transitions %llu FC Interruptions %llu\n",
343
		   db_state_str[dev->db_state],
344 345
		   dev->rdev.stats.db_state_transitions,
		   dev->rdev.stats.db_fc_interruptions);
346
	seq_printf(seq, "TCAM_FULL: %10llu\n", dev->rdev.stats.tcam_full);
347 348 349 350
	seq_printf(seq, "ACT_OFLD_CONN_FAILS: %10llu\n",
		   dev->rdev.stats.act_ofld_conn_fails);
	seq_printf(seq, "PAS_OFLD_CONN_FAILS: %10llu\n",
		   dev->rdev.stats.pas_ofld_conn_fails);
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	return 0;
}

static int stats_open(struct inode *inode, struct file *file)
{
	return single_open(file, stats_show, inode->i_private);
}

static ssize_t stats_clear(struct file *file, const char __user *buf,
		size_t count, loff_t *pos)
{
	struct c4iw_dev *dev = ((struct seq_file *)file->private_data)->private;

	mutex_lock(&dev->rdev.stats.lock);
	dev->rdev.stats.pd.max = 0;
V
Vipul Pandya 已提交
366
	dev->rdev.stats.pd.fail = 0;
367
	dev->rdev.stats.qid.max = 0;
V
Vipul Pandya 已提交
368
	dev->rdev.stats.qid.fail = 0;
369
	dev->rdev.stats.stag.max = 0;
V
Vipul Pandya 已提交
370
	dev->rdev.stats.stag.fail = 0;
371
	dev->rdev.stats.pbl.max = 0;
V
Vipul Pandya 已提交
372
	dev->rdev.stats.pbl.fail = 0;
373
	dev->rdev.stats.rqt.max = 0;
V
Vipul Pandya 已提交
374
	dev->rdev.stats.rqt.fail = 0;
375
	dev->rdev.stats.ocqp.max = 0;
V
Vipul Pandya 已提交
376
	dev->rdev.stats.ocqp.fail = 0;
377 378 379
	dev->rdev.stats.db_full = 0;
	dev->rdev.stats.db_empty = 0;
	dev->rdev.stats.db_drop = 0;
380
	dev->rdev.stats.db_state_transitions = 0;
381 382 383
	dev->rdev.stats.tcam_full = 0;
	dev->rdev.stats.act_ofld_conn_fails = 0;
	dev->rdev.stats.pas_ofld_conn_fails = 0;
384 385 386 387 388 389 390 391 392 393 394 395 396
	mutex_unlock(&dev->rdev.stats.lock);
	return count;
}

static const struct file_operations stats_debugfs_fops = {
	.owner   = THIS_MODULE,
	.open    = stats_open,
	.release = single_release,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.write   = stats_clear,
};

397 398 399 400 401 402 403 404 405 406 407
static int dump_ep(int id, void *p, void *data)
{
	struct c4iw_ep *ep = p;
	struct c4iw_debugfs_data *epd = data;
	int space;
	int cc;

	space = epd->bufsize - epd->pos - 1;
	if (space == 0)
		return 1;

408 409 410 411 412
	if (ep->com.local_addr.ss_family == AF_INET) {
		struct sockaddr_in *lsin = (struct sockaddr_in *)
			&ep->com.local_addr;
		struct sockaddr_in *rsin = (struct sockaddr_in *)
			&ep->com.remote_addr;
413 414 415 416
		struct sockaddr_in *mapped_lsin = (struct sockaddr_in *)
			&ep->com.mapped_local_addr;
		struct sockaddr_in *mapped_rsin = (struct sockaddr_in *)
			&ep->com.mapped_remote_addr;
417 418 419 420

		cc = snprintf(epd->buf + epd->pos, space,
			      "ep %p cm_id %p qp %p state %d flags 0x%lx "
			      "history 0x%lx hwtid %d atid %d "
421
			      "%pI4:%d/%d <-> %pI4:%d/%d\n",
422 423 424 425
			      ep, ep->com.cm_id, ep->com.qp,
			      (int)ep->com.state, ep->com.flags,
			      ep->com.history, ep->hwtid, ep->atid,
			      &lsin->sin_addr, ntohs(lsin->sin_port),
426 427 428
			      ntohs(mapped_lsin->sin_port),
			      &rsin->sin_addr, ntohs(rsin->sin_port),
			      ntohs(mapped_rsin->sin_port));
429 430 431 432 433
	} else {
		struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)
			&ep->com.local_addr;
		struct sockaddr_in6 *rsin6 = (struct sockaddr_in6 *)
			&ep->com.remote_addr;
434 435 436 437
		struct sockaddr_in6 *mapped_lsin6 = (struct sockaddr_in6 *)
			&ep->com.mapped_local_addr;
		struct sockaddr_in6 *mapped_rsin6 = (struct sockaddr_in6 *)
			&ep->com.mapped_remote_addr;
438 439 440 441

		cc = snprintf(epd->buf + epd->pos, space,
			      "ep %p cm_id %p qp %p state %d flags 0x%lx "
			      "history 0x%lx hwtid %d atid %d "
442
			      "%pI6:%d/%d <-> %pI6:%d/%d\n",
443 444 445 446
			      ep, ep->com.cm_id, ep->com.qp,
			      (int)ep->com.state, ep->com.flags,
			      ep->com.history, ep->hwtid, ep->atid,
			      &lsin6->sin6_addr, ntohs(lsin6->sin6_port),
447 448 449
			      ntohs(mapped_lsin6->sin6_port),
			      &rsin6->sin6_addr, ntohs(rsin6->sin6_port),
			      ntohs(mapped_rsin6->sin6_port));
450
	}
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	if (cc < space)
		epd->pos += cc;
	return 0;
}

static int dump_listen_ep(int id, void *p, void *data)
{
	struct c4iw_listen_ep *ep = p;
	struct c4iw_debugfs_data *epd = data;
	int space;
	int cc;

	space = epd->bufsize - epd->pos - 1;
	if (space == 0)
		return 1;

467 468 469
	if (ep->com.local_addr.ss_family == AF_INET) {
		struct sockaddr_in *lsin = (struct sockaddr_in *)
			&ep->com.local_addr;
470 471
		struct sockaddr_in *mapped_lsin = (struct sockaddr_in *)
			&ep->com.mapped_local_addr;
472 473 474

		cc = snprintf(epd->buf + epd->pos, space,
			      "ep %p cm_id %p state %d flags 0x%lx stid %d "
475
			      "backlog %d %pI4:%d/%d\n",
476 477
			      ep, ep->com.cm_id, (int)ep->com.state,
			      ep->com.flags, ep->stid, ep->backlog,
478 479
			      &lsin->sin_addr, ntohs(lsin->sin_port),
			      ntohs(mapped_lsin->sin_port));
480 481 482
	} else {
		struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)
			&ep->com.local_addr;
483 484
		struct sockaddr_in6 *mapped_lsin6 = (struct sockaddr_in6 *)
			&ep->com.mapped_local_addr;
485 486 487

		cc = snprintf(epd->buf + epd->pos, space,
			      "ep %p cm_id %p state %d flags 0x%lx stid %d "
488
			      "backlog %d %pI6:%d/%d\n",
489 490
			      ep, ep->com.cm_id, (int)ep->com.state,
			      ep->com.flags, ep->stid, ep->backlog,
491 492
			      &lsin6->sin6_addr, ntohs(lsin6->sin6_port),
			      ntohs(mapped_lsin6->sin6_port));
493
	}
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
	if (cc < space)
		epd->pos += cc;
	return 0;
}

static int ep_release(struct inode *inode, struct file *file)
{
	struct c4iw_debugfs_data *epd = file->private_data;
	if (!epd) {
		pr_info("%s null qpd?\n", __func__);
		return 0;
	}
	vfree(epd->buf);
	kfree(epd);
	return 0;
}

static int ep_open(struct inode *inode, struct file *file)
{
	struct c4iw_debugfs_data *epd;
	int ret = 0;
	int count = 1;

	epd = kmalloc(sizeof(*epd), GFP_KERNEL);
	if (!epd) {
		ret = -ENOMEM;
		goto out;
	}
	epd->devp = inode->i_private;
	epd->pos = 0;

	spin_lock_irq(&epd->devp->lock);
	idr_for_each(&epd->devp->hwtid_idr, count_idrs, &count);
	idr_for_each(&epd->devp->atid_idr, count_idrs, &count);
	idr_for_each(&epd->devp->stid_idr, count_idrs, &count);
	spin_unlock_irq(&epd->devp->lock);

	epd->bufsize = count * 160;
	epd->buf = vmalloc(epd->bufsize);
	if (!epd->buf) {
		ret = -ENOMEM;
		goto err1;
	}

	spin_lock_irq(&epd->devp->lock);
	idr_for_each(&epd->devp->hwtid_idr, dump_ep, epd);
	idr_for_each(&epd->devp->atid_idr, dump_ep, epd);
	idr_for_each(&epd->devp->stid_idr, dump_listen_ep, epd);
	spin_unlock_irq(&epd->devp->lock);

	file->private_data = epd;
	goto out;
err1:
	kfree(epd);
out:
	return ret;
}

static const struct file_operations ep_debugfs_fops = {
	.owner   = THIS_MODULE,
	.open    = ep_open,
	.release = ep_release,
	.read    = debugfs_read,
};

559 560 561 562 563 564 565 566 567 568 569
static int setup_debugfs(struct c4iw_dev *devp)
{
	struct dentry *de;

	if (!devp->debugfs_root)
		return -1;

	de = debugfs_create_file("qps", S_IWUSR, devp->debugfs_root,
				 (void *)devp, &qp_debugfs_fops);
	if (de && de->d_inode)
		de->d_inode->i_size = 4096;
570 571 572 573 574

	de = debugfs_create_file("stags", S_IWUSR, devp->debugfs_root,
				 (void *)devp, &stag_debugfs_fops);
	if (de && de->d_inode)
		de->d_inode->i_size = 4096;
575 576 577 578 579 580

	de = debugfs_create_file("stats", S_IWUSR, devp->debugfs_root,
			(void *)devp, &stats_debugfs_fops);
	if (de && de->d_inode)
		de->d_inode->i_size = 4096;

581 582 583 584 585
	de = debugfs_create_file("eps", S_IWUSR, devp->debugfs_root,
			(void *)devp, &ep_debugfs_fops);
	if (de && de->d_inode)
		de->d_inode->i_size = 4096;

586 587 588 589 590 591 592 593 594 595 596 597 598
	return 0;
}

void c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
			       struct c4iw_dev_ucontext *uctx)
{
	struct list_head *pos, *nxt;
	struct c4iw_qid_list *entry;

	mutex_lock(&uctx->lock);
	list_for_each_safe(pos, nxt, &uctx->qpids) {
		entry = list_entry(pos, struct c4iw_qid_list, entry);
		list_del_init(&entry->entry);
599
		if (!(entry->qid & rdev->qpmask)) {
V
Vipul Pandya 已提交
600 601
			c4iw_put_resource(&rdev->resource.qid_table,
					  entry->qid);
602 603 604 605
			mutex_lock(&rdev->stats.lock);
			rdev->stats.qid.cur -= rdev->qpmask + 1;
			mutex_unlock(&rdev->stats.lock);
		}
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
		kfree(entry);
	}

	list_for_each_safe(pos, nxt, &uctx->qpids) {
		entry = list_entry(pos, struct c4iw_qid_list, entry);
		list_del_init(&entry->entry);
		kfree(entry);
	}
	mutex_unlock(&uctx->lock);
}

void c4iw_init_dev_ucontext(struct c4iw_rdev *rdev,
			    struct c4iw_dev_ucontext *uctx)
{
	INIT_LIST_HEAD(&uctx->qpids);
	INIT_LIST_HEAD(&uctx->cqids);
	mutex_init(&uctx->lock);
}

/* Caller takes care of locking if needed */
static int c4iw_rdev_open(struct c4iw_rdev *rdev)
{
	int err;

	c4iw_init_dev_ucontext(rdev, &rdev->uctx);

	/*
	 * qpshift is the number of bits to shift the qpid left in order
	 * to get the correct address of the doorbell for that qp.
	 */
	rdev->qpshift = PAGE_SHIFT - ilog2(rdev->lldi.udb_density);
	rdev->qpmask = rdev->lldi.udb_density - 1;
	rdev->cqshift = PAGE_SHIFT - ilog2(rdev->lldi.ucq_density);
	rdev->cqmask = rdev->lldi.ucq_density - 1;
	PDBG("%s dev %s stag start 0x%0x size 0x%0x num stags %d "
641 642
	     "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\n",
643 644 645 646
	     __func__, pci_name(rdev->lldi.pdev), rdev->lldi.vr->stag.start,
	     rdev->lldi.vr->stag.size, c4iw_num_stags(rdev),
	     rdev->lldi.vr->pbl.start,
	     rdev->lldi.vr->pbl.size, rdev->lldi.vr->rq.start,
647 648 649 650 651
	     rdev->lldi.vr->rq.size,
	     rdev->lldi.vr->qp.start,
	     rdev->lldi.vr->qp.size,
	     rdev->lldi.vr->cq.start,
	     rdev->lldi.vr->cq.size);
652
	PDBG("udb len 0x%x udb base %llx db_reg %p gts_reg %p qpshift %lu "
653 654
	     "qpmask 0x%x cqshift %lu cqmask 0x%x\n",
	     (unsigned)pci_resource_len(rdev->lldi.pdev, 2),
655
	     (u64)pci_resource_start(rdev->lldi.pdev, 2),
656 657 658 659 660 661 662 663 664 665
	     rdev->lldi.db_reg,
	     rdev->lldi.gts_reg,
	     rdev->qpshift, rdev->qpmask,
	     rdev->cqshift, rdev->cqmask);

	if (c4iw_num_stags(rdev) == 0) {
		err = -EINVAL;
		goto err1;
	}

666 667 668 669 670 671 672
	rdev->stats.pd.total = T4_MAX_NUM_PD;
	rdev->stats.stag.total = rdev->lldi.vr->stag.size;
	rdev->stats.pbl.total = rdev->lldi.vr->pbl.size;
	rdev->stats.rqt.total = rdev->lldi.vr->rq.size;
	rdev->stats.ocqp.total = rdev->lldi.vr->ocq.size;
	rdev->stats.qid.total = rdev->lldi.vr->qp.size;

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
	err = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);
	if (err) {
		printk(KERN_ERR MOD "error %d initializing resources\n", err);
		goto err1;
	}
	err = c4iw_pblpool_create(rdev);
	if (err) {
		printk(KERN_ERR MOD "error %d initializing pbl pool\n", err);
		goto err2;
	}
	err = c4iw_rqtpool_create(rdev);
	if (err) {
		printk(KERN_ERR MOD "error %d initializing rqt pool\n", err);
		goto err3;
	}
S
Steve Wise 已提交
688 689 690 691 692
	err = c4iw_ocqp_pool_create(rdev);
	if (err) {
		printk(KERN_ERR MOD "error %d initializing ocqp pool\n", err);
		goto err4;
	}
693 694 695 696 697 698
	rdev->status_page = (struct t4_dev_status_page *)
			    __get_free_page(GFP_KERNEL);
	if (!rdev->status_page) {
		pr_err(MOD "error allocating status page\n");
		goto err4;
	}
699
	return 0;
S
Steve Wise 已提交
700 701
err4:
	c4iw_rqtpool_destroy(rdev);
702 703 704 705 706 707 708 709 710 711
err3:
	c4iw_pblpool_destroy(rdev);
err2:
	c4iw_destroy_resource(&rdev->resource);
err1:
	return err;
}

static void c4iw_rdev_close(struct c4iw_rdev *rdev)
{
712
	free_page((unsigned long)rdev->status_page);
713 714 715 716 717
	c4iw_pblpool_destroy(rdev);
	c4iw_rqtpool_destroy(rdev);
	c4iw_destroy_resource(&rdev->resource);
}

718
static void c4iw_dealloc(struct uld_ctx *ctx)
719
{
720 721 722 723
	c4iw_rdev_close(&ctx->dev->rdev);
	idr_destroy(&ctx->dev->cqidr);
	idr_destroy(&ctx->dev->qpidr);
	idr_destroy(&ctx->dev->mmidr);
724 725 726
	idr_destroy(&ctx->dev->hwtid_idr);
	idr_destroy(&ctx->dev->stid_idr);
	idr_destroy(&ctx->dev->atid_idr);
727 728 729 730
	if (ctx->dev->rdev.bar2_kva)
		iounmap(ctx->dev->rdev.bar2_kva);
	if (ctx->dev->rdev.oc_mw_kva)
		iounmap(ctx->dev->rdev.oc_mw_kva);
731
	ib_dealloc_device(&ctx->dev->ibdev);
732
	iwpm_exit(RDMA_NL_C4IW);
733
	ctx->dev = NULL;
734 735
}

736 737 738 739 740 741 742 743 744 745 746
static void c4iw_remove(struct uld_ctx *ctx)
{
	PDBG("%s c4iw_dev %p\n", __func__,  ctx->dev);
	c4iw_unregister_device(ctx->dev);
	c4iw_dealloc(ctx);
}

static int rdma_supported(const struct cxgb4_lld_info *infop)
{
	return infop->vr->stag.size > 0 && infop->vr->pbl.size > 0 &&
	       infop->vr->rq.size > 0 && infop->vr->qp.size > 0 &&
747
	       infop->vr->cq.size > 0;
748 749
}

750 751 752 753 754
static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
{
	struct c4iw_dev *devp;
	int ret;

755 756 757 758 759
	if (!rdma_supported(infop)) {
		printk(KERN_INFO MOD "%s: RDMA not supported on this device.\n",
		       pci_name(infop->pdev));
		return ERR_PTR(-ENOSYS);
	}
760 761 762
	if (!ocqp_supported(infop))
		pr_info("%s: On-Chip Queues not supported on this device.\n",
			pci_name(infop->pdev));
763

764 765 766
	devp = (struct c4iw_dev *)ib_alloc_device(sizeof(*devp));
	if (!devp) {
		printk(KERN_ERR MOD "Cannot allocate ib device\n");
767
		return ERR_PTR(-ENOMEM);
768 769 770
	}
	devp->rdev.lldi = *infop;

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	/* init various hw-queue params based on lld info */
	PDBG("%s: Ing. padding boundary is %d, egrsstatuspagesize = %d\n",
	     __func__, devp->rdev.lldi.sge_ingpadboundary,
	     devp->rdev.lldi.sge_egrstatuspagesize);

	devp->rdev.hw_queue.t4_eq_status_entries =
		devp->rdev.lldi.sge_ingpadboundary > 64 ? 2 : 1;
	devp->rdev.hw_queue.t4_max_eq_size =
		65520 - devp->rdev.hw_queue.t4_eq_status_entries;
	devp->rdev.hw_queue.t4_max_iq_size = 65520 - 1;
	devp->rdev.hw_queue.t4_max_rq_size =
		8192 - devp->rdev.hw_queue.t4_eq_status_entries;
	devp->rdev.hw_queue.t4_max_sq_size =
		devp->rdev.hw_queue.t4_max_eq_size - 1;
	devp->rdev.hw_queue.t4_max_qp_depth =
		devp->rdev.hw_queue.t4_max_rq_size - 1;
	devp->rdev.hw_queue.t4_max_cq_depth =
		devp->rdev.hw_queue.t4_max_iq_size - 1;
	devp->rdev.hw_queue.t4_stat_len =
		devp->rdev.lldi.sge_egrstatuspagesize;

792 793 794 795 796 797 798 799 800 801 802
	/*
	 * For T5 devices, we map all of BAR2 with WC.
	 * For T4 devices with onchip qp mem, we map only that part
	 * of BAR2 with WC.
	 */
	devp->rdev.bar2_pa = pci_resource_start(devp->rdev.lldi.pdev, 2);
	if (is_t5(devp->rdev.lldi.adapter_type)) {
		devp->rdev.bar2_kva = ioremap_wc(devp->rdev.bar2_pa,
			pci_resource_len(devp->rdev.lldi.pdev, 2));
		if (!devp->rdev.bar2_kva) {
			pr_err(MOD "Unable to ioremap BAR2\n");
803
			ib_dealloc_device(&devp->ibdev);
804 805 806 807 808 809 810 811 812 813 814
			return ERR_PTR(-EINVAL);
		}
	} else if (ocqp_supported(infop)) {
		devp->rdev.oc_mw_pa =
			pci_resource_start(devp->rdev.lldi.pdev, 2) +
			pci_resource_len(devp->rdev.lldi.pdev, 2) -
			roundup_pow_of_two(devp->rdev.lldi.vr->ocq.size);
		devp->rdev.oc_mw_kva = ioremap_wc(devp->rdev.oc_mw_pa,
			devp->rdev.lldi.vr->ocq.size);
		if (!devp->rdev.oc_mw_kva) {
			pr_err(MOD "Unable to ioremap onchip mem\n");
815
			ib_dealloc_device(&devp->ibdev);
816 817 818
			return ERR_PTR(-EINVAL);
		}
	}
S
Steve Wise 已提交
819

820
	PDBG(KERN_INFO MOD "ocq memory: "
S
Steve Wise 已提交
821 822 823 824
	       "hw_start 0x%x size %u mw_pa 0x%lx mw_kva %p\n",
	       devp->rdev.lldi.vr->ocq.start, devp->rdev.lldi.vr->ocq.size,
	       devp->rdev.oc_mw_pa, devp->rdev.oc_mw_kva);

825 826 827 828
	ret = c4iw_rdev_open(&devp->rdev);
	if (ret) {
		printk(KERN_ERR MOD "Unable to open CXIO rdev err %d\n", ret);
		ib_dealloc_device(&devp->ibdev);
829
		return ERR_PTR(ret);
830 831 832 833 834
	}

	idr_init(&devp->cqidr);
	idr_init(&devp->qpidr);
	idr_init(&devp->mmidr);
835 836 837
	idr_init(&devp->hwtid_idr);
	idr_init(&devp->stid_idr);
	idr_init(&devp->atid_idr);
838
	spin_lock_init(&devp->lock);
839
	mutex_init(&devp->rdev.stats.lock);
840
	mutex_init(&devp->db_mutex);
841
	INIT_LIST_HEAD(&devp->db_fc_list);
842 843 844 845 846 847 848

	if (c4iw_debugfs_root) {
		devp->debugfs_root = debugfs_create_dir(
					pci_name(devp->rdev.lldi.pdev),
					c4iw_debugfs_root);
		setup_debugfs(devp);
	}
849 850 851 852 853 854 855 856

	ret = iwpm_init(RDMA_NL_C4IW);
	if (ret) {
		pr_err("port mapper initialization failed with %d\n", ret);
		ib_dealloc_device(&devp->ibdev);
		return ERR_PTR(ret);
	}

857 858 859 860 861
	return devp;
}

static void *c4iw_uld_add(const struct cxgb4_lld_info *infop)
{
862
	struct uld_ctx *ctx;
863 864 865 866
	static int vers_printed;
	int i;

	if (!vers_printed++)
867 868
		pr_info("Chelsio T4/T5 RDMA Driver - version %s\n",
			DRV_VERSION);
869

870 871 872
	ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
	if (!ctx) {
		ctx = ERR_PTR(-ENOMEM);
873
		goto out;
874 875
	}
	ctx->lldi = *infop;
876 877

	PDBG("%s found device %s nchan %u nrxq %u ntxq %u nports %u\n",
878 879 880 881 882 883 884
	     __func__, pci_name(ctx->lldi.pdev),
	     ctx->lldi.nchan, ctx->lldi.nrxq,
	     ctx->lldi.ntxq, ctx->lldi.nports);

	mutex_lock(&dev_mutex);
	list_add_tail(&ctx->entry, &uld_ctx_list);
	mutex_unlock(&dev_mutex);
885

886 887
	for (i = 0; i < ctx->lldi.nrxq; i++)
		PDBG("rxqid[%u] %u\n", i, ctx->lldi.rxq_ids[i]);
888
out:
889
	return ctx;
890 891
}

892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
static inline struct sk_buff *copy_gl_to_skb_pkt(const struct pkt_gl *gl,
						 const __be64 *rsp,
						 u32 pktshift)
{
	struct sk_buff *skb;

	/*
	 * Allocate space for cpl_pass_accept_req which will be synthesized by
	 * driver. Once the driver synthesizes the request the skb will go
	 * through the regular cpl_pass_accept_req processing.
	 * The math here assumes sizeof cpl_pass_accept_req >= sizeof
	 * cpl_rx_pkt.
	 */
	skb = alloc_skb(gl->tot_len + sizeof(struct cpl_pass_accept_req) +
			sizeof(struct rss_header) - pktshift, GFP_ATOMIC);
	if (unlikely(!skb))
		return NULL;

	 __skb_put(skb, gl->tot_len + sizeof(struct cpl_pass_accept_req) +
		   sizeof(struct rss_header) - pktshift);

	/*
	 * This skb will contain:
	 *   rss_header from the rspq descriptor (1 flit)
	 *   cpl_rx_pkt struct from the rspq descriptor (2 flits)
	 *   space for the difference between the size of an
	 *      rx_pkt and pass_accept_req cpl (1 flit)
	 *   the packet data from the gl
	 */
	skb_copy_to_linear_data(skb, rsp, sizeof(struct cpl_pass_accept_req) +
				sizeof(struct rss_header));
	skb_copy_to_linear_data_offset(skb, sizeof(struct rss_header) +
				       sizeof(struct cpl_pass_accept_req),
				       gl->va + pktshift,
				       gl->tot_len - pktshift);
	return skb;
}

static inline int recv_rx_pkt(struct c4iw_dev *dev, const struct pkt_gl *gl,
			   const __be64 *rsp)
{
	unsigned int opcode = *(u8 *)rsp;
	struct sk_buff *skb;

	if (opcode != CPL_RX_PKT)
		goto out;

	skb = copy_gl_to_skb_pkt(gl , rsp, dev->rdev.lldi.sge_pktshift);
	if (skb == NULL)
		goto out;

	if (c4iw_handlers[opcode] == NULL) {
		pr_info("%s no handler opcode 0x%x...\n", __func__,
		       opcode);
		kfree_skb(skb);
		goto out;
	}
	c4iw_handlers[opcode](dev, skb);
	return 1;
out:
	return 0;
}

955 956 957
static int c4iw_uld_rx_handler(void *handle, const __be64 *rsp,
			const struct pkt_gl *gl)
{
958 959
	struct uld_ctx *ctx = handle;
	struct c4iw_dev *dev = ctx->dev;
960
	struct sk_buff *skb;
961
	u8 opcode;
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976

	if (gl == NULL) {
		/* omit RSS and rsp_ctrl at end of descriptor */
		unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;

		skb = alloc_skb(256, GFP_ATOMIC);
		if (!skb)
			goto nomem;
		__skb_put(skb, len);
		skb_copy_to_linear_data(skb, &rsp[1], len);
	} else if (gl == CXGB4_MSG_AN) {
		const struct rsp_ctrl *rc = (void *)rsp;

		u32 qid = be32_to_cpu(rc->pldbuflen_qid);
		c4iw_ev_handler(dev, qid);
977 978 979 980 981 982 983 984 985
		return 0;
	} else if (unlikely(*(u8 *)rsp != *(u8 *)gl->va)) {
		if (recv_rx_pkt(dev, gl, rsp))
			return 0;

		pr_info("%s: unexpected FL contents at %p, " \
		       "RSS %#llx, FL %#llx, len %u\n",
		       pci_name(ctx->lldi.pdev), gl->va,
		       (unsigned long long)be64_to_cpu(*rsp),
986 987
		       (unsigned long long)be64_to_cpu(
		       *(__force __be64 *)gl->va),
988 989
		       gl->tot_len);

990 991
		return 0;
	} else {
992
		skb = cxgb4_pktgl_to_skb(gl, 128, 128);
993 994 995 996
		if (unlikely(!skb))
			goto nomem;
	}

997
	opcode = *(u8 *)rsp;
998
	if (c4iw_handlers[opcode]) {
999
		c4iw_handlers[opcode](dev, skb);
1000
	} else {
1001
		pr_info("%s no handler opcode 0x%x...\n", __func__,
1002
		       opcode);
1003 1004
		kfree_skb(skb);
	}
1005 1006 1007 1008 1009 1010 1011 1012

	return 0;
nomem:
	return -1;
}

static int c4iw_uld_state_change(void *handle, enum cxgb4_state new_state)
{
1013
	struct uld_ctx *ctx = handle;
1014

1015
	PDBG("%s new_state %u\n", __func__, new_state);
1016 1017
	switch (new_state) {
	case CXGB4_STATE_UP:
1018 1019
		printk(KERN_INFO MOD "%s: Up\n", pci_name(ctx->lldi.pdev));
		if (!ctx->dev) {
1020
			int ret;
1021 1022

			ctx->dev = c4iw_alloc(&ctx->lldi);
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
			if (IS_ERR(ctx->dev)) {
				printk(KERN_ERR MOD
				       "%s: initialization failed: %ld\n",
				       pci_name(ctx->lldi.pdev),
				       PTR_ERR(ctx->dev));
				ctx->dev = NULL;
				break;
			}
			ret = c4iw_register_device(ctx->dev);
			if (ret) {
1033 1034
				printk(KERN_ERR MOD
				       "%s: RDMA registration failed: %d\n",
1035
				       pci_name(ctx->lldi.pdev), ret);
1036 1037
				c4iw_dealloc(ctx);
			}
1038 1039 1040 1041
		}
		break;
	case CXGB4_STATE_DOWN:
		printk(KERN_INFO MOD "%s: Down\n",
1042 1043 1044
		       pci_name(ctx->lldi.pdev));
		if (ctx->dev)
			c4iw_remove(ctx);
1045 1046 1047
		break;
	case CXGB4_STATE_START_RECOVERY:
		printk(KERN_INFO MOD "%s: Fatal Error\n",
1048 1049
		       pci_name(ctx->lldi.pdev));
		if (ctx->dev) {
1050 1051
			struct ib_event event;

1052
			ctx->dev->rdev.flags |= T4_FATAL_ERROR;
1053 1054
			memset(&event, 0, sizeof event);
			event.event  = IB_EVENT_DEVICE_FATAL;
1055
			event.device = &ctx->dev->ibdev;
1056
			ib_dispatch_event(&event);
1057
			c4iw_remove(ctx);
1058
		}
1059 1060 1061
		break;
	case CXGB4_STATE_DETACH:
		printk(KERN_INFO MOD "%s: Detach\n",
1062 1063 1064
		       pci_name(ctx->lldi.pdev));
		if (ctx->dev)
			c4iw_remove(ctx);
1065 1066
		break;
	}
1067 1068 1069
	return 0;
}

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
static int disable_qp_db(int id, void *p, void *data)
{
	struct c4iw_qp *qp = p;

	t4_disable_wq_db(&qp->wq);
	return 0;
}

static void stop_queues(struct uld_ctx *ctx)
{
1080 1081 1082 1083 1084 1085
	unsigned long flags;

	spin_lock_irqsave(&ctx->dev->lock, flags);
	ctx->dev->rdev.stats.db_state_transitions++;
	ctx->dev->db_state = STOPPED;
	if (ctx->dev->rdev.flags & T4_STATUS_PAGE_DISABLED)
1086
		idr_for_each(&ctx->dev->qpidr, disable_qp_db, NULL);
1087 1088 1089
	else
		ctx->dev->rdev.status_page->db_off = 1;
	spin_unlock_irqrestore(&ctx->dev->lock, flags);
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
}

static int enable_qp_db(int id, void *p, void *data)
{
	struct c4iw_qp *qp = p;

	t4_enable_wq_db(&qp->wq);
	return 0;
}

1100 1101 1102
static void resume_rc_qp(struct c4iw_qp *qp)
{
	spin_lock(&qp->lock);
1103 1104
	t4_ring_sq_db(&qp->wq, qp->wq.sq.wq_pidx_inc,
		      is_t5(qp->rhp->rdev.lldi.adapter_type), NULL);
1105
	qp->wq.sq.wq_pidx_inc = 0;
1106 1107
	t4_ring_rq_db(&qp->wq, qp->wq.rq.wq_pidx_inc,
		      is_t5(qp->rhp->rdev.lldi.adapter_type), NULL);
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
	qp->wq.rq.wq_pidx_inc = 0;
	spin_unlock(&qp->lock);
}

static void resume_a_chunk(struct uld_ctx *ctx)
{
	int i;
	struct c4iw_qp *qp;

	for (i = 0; i < DB_FC_RESUME_SIZE; i++) {
		qp = list_first_entry(&ctx->dev->db_fc_list, struct c4iw_qp,
				      db_fc_entry);
		list_del_init(&qp->db_fc_entry);
		resume_rc_qp(qp);
		if (list_empty(&ctx->dev->db_fc_list))
			break;
	}
}

1127 1128 1129
static void resume_queues(struct uld_ctx *ctx)
{
	spin_lock_irq(&ctx->dev->lock);
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
	if (ctx->dev->db_state != STOPPED)
		goto out;
	ctx->dev->db_state = FLOW_CONTROL;
	while (1) {
		if (list_empty(&ctx->dev->db_fc_list)) {
			WARN_ON(ctx->dev->db_state != FLOW_CONTROL);
			ctx->dev->db_state = NORMAL;
			ctx->dev->rdev.stats.db_state_transitions++;
			if (ctx->dev->rdev.flags & T4_STATUS_PAGE_DISABLED) {
				idr_for_each(&ctx->dev->qpidr, enable_qp_db,
					     NULL);
			} else {
				ctx->dev->rdev.status_page->db_off = 0;
			}
			break;
		} else {
			if (cxgb4_dbfifo_count(ctx->dev->rdev.lldi.ports[0], 1)
			    < (ctx->dev->rdev.lldi.dbfifo_int_thresh <<
			       DB_FC_DRAIN_THRESH)) {
				resume_a_chunk(ctx);
			}
			if (!list_empty(&ctx->dev->db_fc_list)) {
				spin_unlock_irq(&ctx->dev->lock);
				if (DB_FC_RESUME_DELAY) {
					set_current_state(TASK_UNINTERRUPTIBLE);
					schedule_timeout(DB_FC_RESUME_DELAY);
				}
				spin_lock_irq(&ctx->dev->lock);
				if (ctx->dev->db_state != FLOW_CONTROL)
					break;
			}
		}
1162
	}
1163 1164 1165
out:
	if (ctx->dev->db_state != NORMAL)
		ctx->dev->rdev.stats.db_fc_interruptions++;
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
	spin_unlock_irq(&ctx->dev->lock);
}

struct qp_list {
	unsigned idx;
	struct c4iw_qp **qps;
};

static int add_and_ref_qp(int id, void *p, void *data)
{
	struct qp_list *qp_listp = data;
	struct c4iw_qp *qp = p;

	c4iw_qp_add_ref(&qp->ibqp);
	qp_listp->qps[qp_listp->idx++] = qp;
	return 0;
}

static int count_qps(int id, void *p, void *data)
{
	unsigned *countp = data;
	(*countp)++;
	return 0;
}

1191
static void deref_qps(struct qp_list *qp_list)
1192 1193 1194
{
	int idx;

1195 1196
	for (idx = 0; idx < qp_list->idx; idx++)
		c4iw_qp_rem_ref(&qp_list->qps[idx]->ibqp);
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
}

static void recover_lost_dbs(struct uld_ctx *ctx, struct qp_list *qp_list)
{
	int idx;
	int ret;

	for (idx = 0; idx < qp_list->idx; idx++) {
		struct c4iw_qp *qp = qp_list->qps[idx];

1207 1208
		spin_lock_irq(&qp->rhp->lock);
		spin_lock(&qp->lock);
1209 1210 1211 1212 1213
		ret = cxgb4_sync_txq_pidx(qp->rhp->rdev.lldi.ports[0],
					  qp->wq.sq.qid,
					  t4_sq_host_wq_pidx(&qp->wq),
					  t4_sq_wq_size(&qp->wq));
		if (ret) {
1214
			pr_err(KERN_ERR MOD "%s: Fatal error - "
1215 1216 1217
			       "DB overflow recovery failed - "
			       "error syncing SQ qid %u\n",
			       pci_name(ctx->lldi.pdev), qp->wq.sq.qid);
1218 1219
			spin_unlock(&qp->lock);
			spin_unlock_irq(&qp->rhp->lock);
1220 1221
			return;
		}
1222
		qp->wq.sq.wq_pidx_inc = 0;
1223 1224 1225 1226 1227 1228 1229

		ret = cxgb4_sync_txq_pidx(qp->rhp->rdev.lldi.ports[0],
					  qp->wq.rq.qid,
					  t4_rq_host_wq_pidx(&qp->wq),
					  t4_rq_wq_size(&qp->wq));

		if (ret) {
1230
			pr_err(KERN_ERR MOD "%s: Fatal error - "
1231 1232 1233
			       "DB overflow recovery failed - "
			       "error syncing RQ qid %u\n",
			       pci_name(ctx->lldi.pdev), qp->wq.rq.qid);
1234 1235
			spin_unlock(&qp->lock);
			spin_unlock_irq(&qp->rhp->lock);
1236 1237
			return;
		}
1238 1239 1240
		qp->wq.rq.wq_pidx_inc = 0;
		spin_unlock(&qp->lock);
		spin_unlock_irq(&qp->rhp->lock);
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

		/* Wait for the dbfifo to drain */
		while (cxgb4_dbfifo_count(qp->rhp->rdev.lldi.ports[0], 1) > 0) {
			set_current_state(TASK_UNINTERRUPTIBLE);
			schedule_timeout(usecs_to_jiffies(10));
		}
	}
}

static void recover_queues(struct uld_ctx *ctx)
{
	int count = 0;
	struct qp_list qp_list;
	int ret;

	/* slow everybody down */
	set_current_state(TASK_UNINTERRUPTIBLE);
	schedule_timeout(usecs_to_jiffies(1000));

	/* flush the SGE contexts */
	ret = cxgb4_flush_eq_cache(ctx->dev->rdev.lldi.ports[0]);
	if (ret) {
		printk(KERN_ERR MOD "%s: Fatal error - DB overflow recovery failed\n",
		       pci_name(ctx->lldi.pdev));
1265
		return;
1266 1267 1268 1269
	}

	/* Count active queues so we can build a list of queues to recover */
	spin_lock_irq(&ctx->dev->lock);
1270 1271
	WARN_ON(ctx->dev->db_state != STOPPED);
	ctx->dev->db_state = RECOVERY;
1272 1273 1274 1275 1276 1277 1278
	idr_for_each(&ctx->dev->qpidr, count_qps, &count);

	qp_list.qps = kzalloc(count * sizeof *qp_list.qps, GFP_ATOMIC);
	if (!qp_list.qps) {
		printk(KERN_ERR MOD "%s: Fatal error - DB overflow recovery failed\n",
		       pci_name(ctx->lldi.pdev));
		spin_unlock_irq(&ctx->dev->lock);
1279
		return;
1280 1281 1282 1283 1284 1285
	}
	qp_list.idx = 0;

	/* add and ref each qp so it doesn't get freed */
	idr_for_each(&ctx->dev->qpidr, add_and_ref_qp, &qp_list);

1286
	spin_unlock_irq(&ctx->dev->lock);
1287 1288 1289 1290 1291

	/* now traverse the list in a safe context to recover the db state*/
	recover_lost_dbs(ctx, &qp_list);

	/* we're almost done!  deref the qps and clean up */
1292
	deref_qps(&qp_list);
1293 1294 1295
	kfree(qp_list.qps);

	spin_lock_irq(&ctx->dev->lock);
1296 1297
	WARN_ON(ctx->dev->db_state != RECOVERY);
	ctx->dev->db_state = STOPPED;
1298
	spin_unlock_irq(&ctx->dev->lock);
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
}

static int c4iw_uld_control(void *handle, enum cxgb4_control control, ...)
{
	struct uld_ctx *ctx = handle;

	switch (control) {
	case CXGB4_CONTROL_DB_FULL:
		stop_queues(ctx);
		ctx->dev->rdev.stats.db_full++;
		break;
	case CXGB4_CONTROL_DB_EMPTY:
		resume_queues(ctx);
		mutex_lock(&ctx->dev->rdev.stats.lock);
		ctx->dev->rdev.stats.db_empty++;
		mutex_unlock(&ctx->dev->rdev.stats.lock);
		break;
	case CXGB4_CONTROL_DB_DROP:
1317
		recover_queues(ctx);
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
		mutex_lock(&ctx->dev->rdev.stats.lock);
		ctx->dev->rdev.stats.db_drop++;
		mutex_unlock(&ctx->dev->rdev.stats.lock);
		break;
	default:
		printk(KERN_WARNING MOD "%s: unknown control cmd %u\n",
		       pci_name(ctx->lldi.pdev), control);
		break;
	}
	return 0;
}

1330 1331 1332 1333 1334
static struct cxgb4_uld_info c4iw_uld_info = {
	.name = DRV_NAME,
	.add = c4iw_uld_add,
	.rx_handler = c4iw_uld_rx_handler,
	.state_change = c4iw_uld_state_change,
1335
	.control = c4iw_uld_control,
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
};

static int __init c4iw_init_module(void)
{
	int err;

	err = c4iw_cm_init();
	if (err)
		return err;

	c4iw_debugfs_root = debugfs_create_dir(DRV_NAME, NULL);
	if (!c4iw_debugfs_root)
		printk(KERN_WARNING MOD
		       "could not create debugfs entry, continuing\n");

1351 1352 1353 1354 1355
	if (ibnl_add_client(RDMA_NL_C4IW, RDMA_NL_IWPM_NUM_OPS,
			    c4iw_nl_cb_table))
		pr_err("%s[%u]: Failed to add netlink callback\n"
		       , __func__, __LINE__);

1356 1357 1358 1359 1360 1361 1362
	cxgb4_register_uld(CXGB4_ULD_RDMA, &c4iw_uld_info);

	return 0;
}

static void __exit c4iw_exit_module(void)
{
1363
	struct uld_ctx *ctx, *tmp;
1364 1365

	mutex_lock(&dev_mutex);
1366 1367 1368 1369
	list_for_each_entry_safe(ctx, tmp, &uld_ctx_list, entry) {
		if (ctx->dev)
			c4iw_remove(ctx);
		kfree(ctx);
1370 1371
	}
	mutex_unlock(&dev_mutex);
1372
	cxgb4_unregister_uld(CXGB4_ULD_RDMA);
1373
	ibnl_remove_client(RDMA_NL_C4IW);
1374 1375 1376 1377 1378 1379
	c4iw_cm_term();
	debugfs_remove_recursive(c4iw_debugfs_root);
}

module_init(c4iw_init_module);
module_exit(c4iw_exit_module);