device.c 42.8 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
#include <linux/math64.h>
37 38 39 40 41 42 43 44

#include <rdma/ib_verbs.h>

#include "iw_cxgb4.h"

#define DRV_VERSION "0.1"

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

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
int c4iw_wr_log = 0;
module_param(c4iw_wr_log, int, 0444);
MODULE_PARM_DESC(c4iw_wr_log, "Enables logging of work request timing data.");

62
static int c4iw_wr_log_size_order = 12;
63 64 65 66
module_param(c4iw_wr_log_size_order, int, 0444);
MODULE_PARM_DESC(c4iw_wr_log_size_order,
		 "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;
70

71 72 73 74
#define DB_FC_RESUME_SIZE 64
#define DB_FC_RESUME_DELAY 1
#define DB_FC_DRAIN_THRESH 0

75 76
static struct dentry *c4iw_debugfs_root;

77
struct c4iw_debugfs_data {
78 79 80 81 82 83
	struct c4iw_dev *devp;
	char *buf;
	int bufsize;
	int pos;
};

84
static int count_idrs(int id, void *p, void *data)
85 86 87 88 89 90 91
{
	int *countp = data;

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

92 93 94 95 96
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;

97
	return simple_read_from_buffer(buf, count, ppos, d->buf, d->pos);
98 99
}

100 101 102 103 104 105 106 107 108 109 110
void c4iw_log_wr_stats(struct t4_wq *wq, struct t4_cqe *cqe)
{
	struct wr_log_entry le;
	int idx;

	if (!wq->rdev->wr_log)
		return;

	idx = (atomic_inc_return(&wq->rdev->wr_log_idx) - 1) &
		(wq->rdev->wr_log_size - 1);
	le.poll_sge_ts = cxgb4_read_sge_timestamp(wq->rdev->lldi.ports[0]);
111
	le.poll_host_time = ktime_get();
112 113 114 115 116
	le.valid = 1;
	le.cqe_sge_ts = CQE_TS(cqe);
	if (SQ_TYPE(cqe)) {
		le.qid = wq->sq.qid;
		le.opcode = CQE_OPCODE(cqe);
117
		le.post_host_time = wq->sq.sw_sq[wq->sq.cidx].host_time;
118 119 120 121 122
		le.post_sge_ts = wq->sq.sw_sq[wq->sq.cidx].sge_ts;
		le.wr_id = CQE_WRID_SQ_IDX(cqe);
	} else {
		le.qid = wq->rq.qid;
		le.opcode = FW_RI_RECEIVE;
123
		le.post_host_time = wq->rq.sw_rq[wq->rq.cidx].host_time;
124 125 126 127 128 129 130 131 132
		le.post_sge_ts = wq->rq.sw_rq[wq->rq.cidx].sge_ts;
		le.wr_id = CQE_WRID_MSN(cqe);
	}
	wq->rdev->wr_log[idx] = le;
}

static int wr_log_show(struct seq_file *seq, void *v)
{
	struct c4iw_dev *dev = seq->private;
133
	ktime_t prev_time;
134
	struct wr_log_entry *lep;
135
	int prev_time_set = 0;
136 137
	int idx, end;

H
Hariprasad S 已提交
138
#define ts2ns(ts) div64_u64((ts) * dev->rdev.lldi.cclk_ps, 1000)
139 140 141 142 143 144 145 146 147

	idx = atomic_read(&dev->rdev.wr_log_idx) &
		(dev->rdev.wr_log_size - 1);
	end = idx - 1;
	if (end < 0)
		end = dev->rdev.wr_log_size - 1;
	lep = &dev->rdev.wr_log[idx];
	while (idx != end) {
		if (lep->valid) {
148 149 150
			if (!prev_time_set) {
				prev_time_set = 1;
				prev_time = lep->poll_host_time;
151
			}
152 153
			seq_printf(seq, "%04u: nsec %llu qid %u opcode "
				   "%u %s 0x%x host_wr_delta nsec %llu "
154 155 156 157
				   "post_sge_ts 0x%llx cqe_sge_ts 0x%llx "
				   "poll_sge_ts 0x%llx post_poll_delta_ns %llu "
				   "cqe_poll_delta_ns %llu\n",
				   idx,
158 159
				   ktime_to_ns(ktime_sub(lep->poll_host_time,
							 prev_time)),
160 161 162 163
				   lep->qid, lep->opcode,
				   lep->opcode == FW_RI_RECEIVE ?
							"msn" : "wrid",
				   lep->wr_id,
164 165
				   ktime_to_ns(ktime_sub(lep->poll_host_time,
							 lep->post_host_time)),
166 167 168 169
				   lep->post_sge_ts, lep->cqe_sge_ts,
				   lep->poll_sge_ts,
				   ts2ns(lep->poll_sge_ts - lep->post_sge_ts),
				   ts2ns(lep->poll_sge_ts - lep->cqe_sge_ts));
170
			prev_time = lep->poll_host_time;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		}
		idx++;
		if (idx > (dev->rdev.wr_log_size - 1))
			idx = 0;
		lep = &dev->rdev.wr_log[idx];
	}
#undef ts2ns
	return 0;
}

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

static ssize_t wr_log_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;
	int i;

	if (dev->rdev.wr_log)
		for (i = 0; i < dev->rdev.wr_log_size; i++)
			dev->rdev.wr_log[i].valid = 0;
	return count;
}

static const struct file_operations wr_log_debugfs_fops = {
	.owner   = THIS_MODULE,
	.open    = wr_log_open,
	.release = single_release,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.write   = wr_log_clear,
};

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
static struct sockaddr_in zero_sin = {
	.sin_family = AF_INET,
};

static struct sockaddr_in6 zero_sin6 = {
	.sin6_family = AF_INET6,
};

static void set_ep_sin_addrs(struct c4iw_ep *ep,
			     struct sockaddr_in **lsin,
			     struct sockaddr_in **rsin,
			     struct sockaddr_in **m_lsin,
			     struct sockaddr_in **m_rsin)
{
	struct iw_cm_id *id = ep->com.cm_id;

223 224
	*m_lsin = (struct sockaddr_in *)&ep->com.local_addr;
	*m_rsin = (struct sockaddr_in *)&ep->com.remote_addr;
225
	if (id) {
226 227
		*lsin = (struct sockaddr_in *)&id->local_addr;
		*rsin = (struct sockaddr_in *)&id->remote_addr;
228
	} else {
229 230
		*lsin = &zero_sin;
		*rsin = &zero_sin;
231 232 233 234 235 236 237 238 239 240 241
	}
}

static void set_ep_sin6_addrs(struct c4iw_ep *ep,
			      struct sockaddr_in6 **lsin6,
			      struct sockaddr_in6 **rsin6,
			      struct sockaddr_in6 **m_lsin6,
			      struct sockaddr_in6 **m_rsin6)
{
	struct iw_cm_id *id = ep->com.cm_id;

242 243
	*m_lsin6 = (struct sockaddr_in6 *)&ep->com.local_addr;
	*m_rsin6 = (struct sockaddr_in6 *)&ep->com.remote_addr;
244
	if (id) {
245 246
		*lsin6 = (struct sockaddr_in6 *)&id->local_addr;
		*rsin6 = (struct sockaddr_in6 *)&id->remote_addr;
247
	} else {
248 249
		*lsin6 = &zero_sin6;
		*rsin6 = &zero_sin6;
250 251 252
	}
}

253
static int dump_qp(int id, void *p, void *data)
254 255
{
	struct c4iw_qp *qp = p;
256
	struct c4iw_debugfs_data *qpd = data;
257 258 259 260 261 262 263 264 265 266
	int space;
	int cc;

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

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

267
	if (qp->ep) {
268 269 270 271 272 273 274
		struct c4iw_ep *ep = qp->ep;

		if (ep->com.local_addr.ss_family == AF_INET) {
			struct sockaddr_in *lsin;
			struct sockaddr_in *rsin;
			struct sockaddr_in *m_lsin;
			struct sockaddr_in *m_rsin;
275

276
			set_ep_sin_addrs(ep, &lsin, &rsin, &m_lsin, &m_rsin);
277
			cc = snprintf(qpd->buf + qpd->pos, space,
278
				      "rc qp sq id %u %s id %u state %u "
279
				      "onchip %u ep tid %u state %u "
280
				      "%pI4:%u/%u->%pI4:%u/%u\n",
281 282
				      qp->wq.sq.qid, qp->srq ? "srq" : "rq",
				      qp->srq ? qp->srq->idx : qp->wq.rq.qid,
283 284
				      (int)qp->attr.state,
				      qp->wq.sq.flags & T4_SQ_ONCHIP,
285
				      ep->hwtid, (int)ep->com.state,
286
				      &lsin->sin_addr, ntohs(lsin->sin_port),
287
				      ntohs(m_lsin->sin_port),
288
				      &rsin->sin_addr, ntohs(rsin->sin_port),
289
				      ntohs(m_rsin->sin_port));
290
		} else {
291 292 293 294
			struct sockaddr_in6 *lsin6;
			struct sockaddr_in6 *rsin6;
			struct sockaddr_in6 *m_lsin6;
			struct sockaddr_in6 *m_rsin6;
295

296 297
			set_ep_sin6_addrs(ep, &lsin6, &rsin6, &m_lsin6,
					  &m_rsin6);
298 299 300
			cc = snprintf(qpd->buf + qpd->pos, space,
				      "rc qp sq id %u rq id %u state %u "
				      "onchip %u ep tid %u state %u "
301
				      "%pI6:%u/%u->%pI6:%u/%u\n",
302 303 304
				      qp->wq.sq.qid, qp->wq.rq.qid,
				      (int)qp->attr.state,
				      qp->wq.sq.flags & T4_SQ_ONCHIP,
305
				      ep->hwtid, (int)ep->com.state,
306 307
				      &lsin6->sin6_addr,
				      ntohs(lsin6->sin6_port),
308
				      ntohs(m_lsin6->sin6_port),
309
				      &rsin6->sin6_addr,
310
				      ntohs(rsin6->sin6_port),
311
				      ntohs(m_rsin6->sin6_port));
312 313
		}
	} else
314 315 316 317 318
		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);
319 320 321 322 323 324 325
	if (cc < space)
		qpd->pos += cc;
	return 0;
}

static int qp_release(struct inode *inode, struct file *file)
{
326
	struct c4iw_debugfs_data *qpd = file->private_data;
327
	if (!qpd) {
328
		pr_info("%s null qpd?\n", __func__);
329 330
		return 0;
	}
331
	vfree(qpd->buf);
332 333 334 335 336 337
	kfree(qpd);
	return 0;
}

static int qp_open(struct inode *inode, struct file *file)
{
338
	struct c4iw_debugfs_data *qpd;
339 340 341
	int count = 1;

	qpd = kmalloc(sizeof *qpd, GFP_KERNEL);
342 343 344
	if (!qpd)
		return -ENOMEM;

345 346 347 348
	qpd->devp = inode->i_private;
	qpd->pos = 0;

	spin_lock_irq(&qpd->devp->lock);
349
	idr_for_each(&qpd->devp->qpidr, count_idrs, &count);
350 351
	spin_unlock_irq(&qpd->devp->lock);

352
	qpd->bufsize = count * 180;
353
	qpd->buf = vmalloc(qpd->bufsize);
354
	if (!qpd->buf) {
355 356
		kfree(qpd);
		return -ENOMEM;
357 358 359
	}

	spin_lock_irq(&qpd->devp->lock);
360
	idr_for_each(&qpd->devp->qpidr, dump_qp, qpd);
361 362 363 364
	spin_unlock_irq(&qpd->devp->lock);

	qpd->buf[qpd->pos++] = 0;
	file->private_data = qpd;
365
	return 0;
366 367
}

368 369 370 371 372
static const struct file_operations qp_debugfs_fops = {
	.owner   = THIS_MODULE,
	.open    = qp_open,
	.release = qp_release,
	.read    = debugfs_read,
373
	.llseek  = default_llseek,
374 375 376
};

static int dump_stag(int id, void *p, void *data)
377
{
378 379 380
	struct c4iw_debugfs_data *stagd = data;
	int space;
	int cc;
381 382
	struct fw_ri_tpte tpte;
	int ret;
383

384 385 386 387
	space = stagd->bufsize - stagd->pos - 1;
	if (space == 0)
		return 1;

388 389 390 391 392 393 394 395 396 397 398
	ret = cxgb4_read_tpte(stagd->devp->rdev.lldi.ports[0], (u32)id<<8,
			      (__be32 *)&tpte);
	if (ret) {
		dev_err(&stagd->devp->rdev.lldi.pdev->dev,
			"%s cxgb4_read_tpte err %d\n", __func__, ret);
		return ret;
	}
	cc = snprintf(stagd->buf + stagd->pos, space,
		      "stag: idx 0x%x valid %d key 0x%x state %d pdid %d "
		      "perm 0x%x ps %d len 0x%llx va 0x%llx\n",
		      (u32)id<<8,
399 400 401 402 403 404
		      FW_RI_TPTE_VALID_G(ntohl(tpte.valid_to_pdid)),
		      FW_RI_TPTE_STAGKEY_G(ntohl(tpte.valid_to_pdid)),
		      FW_RI_TPTE_STAGSTATE_G(ntohl(tpte.valid_to_pdid)),
		      FW_RI_TPTE_PDID_G(ntohl(tpte.valid_to_pdid)),
		      FW_RI_TPTE_PERM_G(ntohl(tpte.locread_to_qpid)),
		      FW_RI_TPTE_PS_G(ntohl(tpte.locread_to_qpid)),
405 406
		      ((u64)ntohl(tpte.len_hi) << 32) | ntohl(tpte.len_lo),
		      ((u64)ntohl(tpte.va_hi) << 32) | ntohl(tpte.va_lo_fbo));
407 408 409 410 411 412 413 414 415
	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) {
416
		pr_info("%s null stagd?\n", __func__);
417
		return 0;
418
	}
419
	vfree(stagd->buf);
420 421 422
	kfree(stagd);
	return 0;
}
423

424 425 426 427 428
static int stag_open(struct inode *inode, struct file *file)
{
	struct c4iw_debugfs_data *stagd;
	int ret = 0;
	int count = 1;
429

430 431 432 433 434 435 436
	stagd = kmalloc(sizeof *stagd, GFP_KERNEL);
	if (!stagd) {
		ret = -ENOMEM;
		goto out;
	}
	stagd->devp = inode->i_private;
	stagd->pos = 0;
437

438 439 440 441
	spin_lock_irq(&stagd->devp->lock);
	idr_for_each(&stagd->devp->mmidr, count_idrs, &count);
	spin_unlock_irq(&stagd->devp->lock);

442 443
	stagd->bufsize = count * 256;
	stagd->buf = vmalloc(stagd->bufsize);
444 445 446
	if (!stagd->buf) {
		ret = -ENOMEM;
		goto err1;
447
	}
448 449 450 451 452 453 454 455 456 457 458 459

	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;
460 461
}

462
static const struct file_operations stag_debugfs_fops = {
463
	.owner   = THIS_MODULE,
464 465 466
	.open    = stag_open,
	.release = stag_release,
	.read    = debugfs_read,
467
	.llseek  = default_llseek,
468 469
};

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

472 473 474 475
static int stats_show(struct seq_file *seq, void *v)
{
	struct c4iw_dev *dev = seq->private;

V
Vipul Pandya 已提交
476 477 478
	seq_printf(seq, "   Object: %10s %10s %10s %10s\n", "Total", "Current",
		   "Max", "Fail");
	seq_printf(seq, "     PDID: %10llu %10llu %10llu %10llu\n",
479
			dev->rdev.stats.pd.total, dev->rdev.stats.pd.cur,
V
Vipul Pandya 已提交
480 481
			dev->rdev.stats.pd.max, dev->rdev.stats.pd.fail);
	seq_printf(seq, "      QID: %10llu %10llu %10llu %10llu\n",
482
			dev->rdev.stats.qid.total, dev->rdev.stats.qid.cur,
V
Vipul Pandya 已提交
483
			dev->rdev.stats.qid.max, dev->rdev.stats.qid.fail);
484 485 486
	seq_printf(seq, "     SRQS: %10llu %10llu %10llu %10llu\n",
		   dev->rdev.stats.srqt.total, dev->rdev.stats.srqt.cur,
			dev->rdev.stats.srqt.max, dev->rdev.stats.srqt.fail);
V
Vipul Pandya 已提交
487
	seq_printf(seq, "   TPTMEM: %10llu %10llu %10llu %10llu\n",
488
			dev->rdev.stats.stag.total, dev->rdev.stats.stag.cur,
V
Vipul Pandya 已提交
489 490
			dev->rdev.stats.stag.max, dev->rdev.stats.stag.fail);
	seq_printf(seq, "   PBLMEM: %10llu %10llu %10llu %10llu\n",
491
			dev->rdev.stats.pbl.total, dev->rdev.stats.pbl.cur,
V
Vipul Pandya 已提交
492 493
			dev->rdev.stats.pbl.max, dev->rdev.stats.pbl.fail);
	seq_printf(seq, "   RQTMEM: %10llu %10llu %10llu %10llu\n",
494
			dev->rdev.stats.rqt.total, dev->rdev.stats.rqt.cur,
V
Vipul Pandya 已提交
495 496
			dev->rdev.stats.rqt.max, dev->rdev.stats.rqt.fail);
	seq_printf(seq, "  OCQPMEM: %10llu %10llu %10llu %10llu\n",
497
			dev->rdev.stats.ocqp.total, dev->rdev.stats.ocqp.cur,
V
Vipul Pandya 已提交
498
			dev->rdev.stats.ocqp.max, dev->rdev.stats.ocqp.fail);
499 500 501
	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);
502
	seq_printf(seq, " DB State: %s Transitions %llu FC Interruptions %llu\n",
503
		   db_state_str[dev->db_state],
504 505
		   dev->rdev.stats.db_state_transitions,
		   dev->rdev.stats.db_fc_interruptions);
506
	seq_printf(seq, "TCAM_FULL: %10llu\n", dev->rdev.stats.tcam_full);
507 508 509 510
	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);
511
	seq_printf(seq, "NEG_ADV_RCVD: %10llu\n", dev->rdev.stats.neg_adv);
512
	seq_printf(seq, "AVAILABLE IRD: %10u\n", dev->avail_ird);
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	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 已提交
528
	dev->rdev.stats.pd.fail = 0;
529
	dev->rdev.stats.qid.max = 0;
V
Vipul Pandya 已提交
530
	dev->rdev.stats.qid.fail = 0;
531
	dev->rdev.stats.stag.max = 0;
V
Vipul Pandya 已提交
532
	dev->rdev.stats.stag.fail = 0;
533
	dev->rdev.stats.pbl.max = 0;
V
Vipul Pandya 已提交
534
	dev->rdev.stats.pbl.fail = 0;
535
	dev->rdev.stats.rqt.max = 0;
V
Vipul Pandya 已提交
536
	dev->rdev.stats.rqt.fail = 0;
537 538
	dev->rdev.stats.rqt.max = 0;
	dev->rdev.stats.rqt.fail = 0;
539
	dev->rdev.stats.ocqp.max = 0;
V
Vipul Pandya 已提交
540
	dev->rdev.stats.ocqp.fail = 0;
541 542 543
	dev->rdev.stats.db_full = 0;
	dev->rdev.stats.db_empty = 0;
	dev->rdev.stats.db_drop = 0;
544
	dev->rdev.stats.db_state_transitions = 0;
545 546 547
	dev->rdev.stats.tcam_full = 0;
	dev->rdev.stats.act_ofld_conn_fails = 0;
	dev->rdev.stats.pas_ofld_conn_fails = 0;
548 549 550 551 552 553 554 555 556 557 558 559 560
	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,
};

561 562 563 564 565 566 567 568 569 570 571
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;

572
	if (ep->com.local_addr.ss_family == AF_INET) {
573 574 575 576
		struct sockaddr_in *lsin;
		struct sockaddr_in *rsin;
		struct sockaddr_in *m_lsin;
		struct sockaddr_in *m_rsin;
577

578
		set_ep_sin_addrs(ep, &lsin, &rsin, &m_lsin, &m_rsin);
579 580 581
		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 "
582
			      "conn_na %u abort_na %u "
583
			      "%pI4:%d/%d <-> %pI4:%d/%d\n",
584 585 586
			      ep, ep->com.cm_id, ep->com.qp,
			      (int)ep->com.state, ep->com.flags,
			      ep->com.history, ep->hwtid, ep->atid,
587 588
			      ep->stats.connect_neg_adv,
			      ep->stats.abort_neg_adv,
589
			      &lsin->sin_addr, ntohs(lsin->sin_port),
590
			      ntohs(m_lsin->sin_port),
591
			      &rsin->sin_addr, ntohs(rsin->sin_port),
592
			      ntohs(m_rsin->sin_port));
593
	} else {
594 595 596 597
		struct sockaddr_in6 *lsin6;
		struct sockaddr_in6 *rsin6;
		struct sockaddr_in6 *m_lsin6;
		struct sockaddr_in6 *m_rsin6;
598

599
		set_ep_sin6_addrs(ep, &lsin6, &rsin6, &m_lsin6, &m_rsin6);
600 601 602
		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 "
603
			      "conn_na %u abort_na %u "
604
			      "%pI6:%d/%d <-> %pI6:%d/%d\n",
605 606 607
			      ep, ep->com.cm_id, ep->com.qp,
			      (int)ep->com.state, ep->com.flags,
			      ep->com.history, ep->hwtid, ep->atid,
608 609
			      ep->stats.connect_neg_adv,
			      ep->stats.abort_neg_adv,
610
			      &lsin6->sin6_addr, ntohs(lsin6->sin6_port),
611
			      ntohs(m_lsin6->sin6_port),
612
			      &rsin6->sin6_addr, ntohs(rsin6->sin6_port),
613
			      ntohs(m_rsin6->sin6_port));
614
	}
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	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;

631 632
	if (ep->com.local_addr.ss_family == AF_INET) {
		struct sockaddr_in *lsin = (struct sockaddr_in *)
633
			&ep->com.cm_id->local_addr;
634
		struct sockaddr_in *m_lsin = (struct sockaddr_in *)
635
			&ep->com.cm_id->m_local_addr;
636 637 638

		cc = snprintf(epd->buf + epd->pos, space,
			      "ep %p cm_id %p state %d flags 0x%lx stid %d "
639
			      "backlog %d %pI4:%d/%d\n",
640 641
			      ep, ep->com.cm_id, (int)ep->com.state,
			      ep->com.flags, ep->stid, ep->backlog,
642
			      &lsin->sin_addr, ntohs(lsin->sin_port),
643
			      ntohs(m_lsin->sin_port));
644 645
	} else {
		struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)
646
			&ep->com.cm_id->local_addr;
647
		struct sockaddr_in6 *m_lsin6 = (struct sockaddr_in6 *)
648
			&ep->com.cm_id->m_local_addr;
649 650 651

		cc = snprintf(epd->buf + epd->pos, space,
			      "ep %p cm_id %p state %d flags 0x%lx stid %d "
652
			      "backlog %d %pI6:%d/%d\n",
653 654
			      ep, ep->com.cm_id, (int)ep->com.state,
			      ep->com.flags, ep->stid, ep->backlog,
655
			      &lsin6->sin6_addr, ntohs(lsin6->sin6_port),
656
			      ntohs(m_lsin6->sin6_port));
657
	}
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
	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);

695
	epd->bufsize = count * 240;
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	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,
};

723 724 725 726 727
static int setup_debugfs(struct c4iw_dev *devp)
{
	if (!devp->debugfs_root)
		return -1;

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
	debugfs_create_file_size("qps", S_IWUSR, devp->debugfs_root,
				 (void *)devp, &qp_debugfs_fops, 4096);

	debugfs_create_file_size("stags", S_IWUSR, devp->debugfs_root,
				 (void *)devp, &stag_debugfs_fops, 4096);

	debugfs_create_file_size("stats", S_IWUSR, devp->debugfs_root,
				 (void *)devp, &stats_debugfs_fops, 4096);

	debugfs_create_file_size("eps", S_IWUSR, devp->debugfs_root,
				 (void *)devp, &ep_debugfs_fops, 4096);

	if (c4iw_wr_log)
		debugfs_create_file_size("wr_log", S_IWUSR, devp->debugfs_root,
					 (void *)devp, &wr_log_debugfs_fops, 4096);
743 744 745 746 747 748 749 750 751 752 753 754 755
	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);
756
		if (!(entry->qid & rdev->qpmask)) {
V
Vipul Pandya 已提交
757 758
			c4iw_put_resource(&rdev->resource.qid_table,
					  entry->qid);
759 760 761 762
			mutex_lock(&rdev->stats.lock);
			rdev->stats.qid.cur -= rdev->qpmask + 1;
			mutex_unlock(&rdev->stats.lock);
		}
763 764 765
		kfree(entry);
	}

766
	list_for_each_safe(pos, nxt, &uctx->cqids) {
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
		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);

789 790 791 792 793 794
	/*
	 * This implementation assumes udb_density == ucq_density!  Eventually
	 * we might need to support this but for now fail the open. Also the
	 * cqid and qpid range must match for now.
	 */
	if (rdev->lldi.udb_density != rdev->lldi.ucq_density) {
795
		pr_err("%s: unsupported udb/ucq densities %u/%u\n",
796 797
		       pci_name(rdev->lldi.pdev), rdev->lldi.udb_density,
		       rdev->lldi.ucq_density);
798
		return -EINVAL;
799 800 801
	}
	if (rdev->lldi.vr->qp.start != rdev->lldi.vr->cq.start ||
	    rdev->lldi.vr->qp.size != rdev->lldi.vr->cq.size) {
802
		pr_err("%s: unsupported qp and cq id ranges qp start %u size %u cq start %u size %u\n",
803 804 805
		       pci_name(rdev->lldi.pdev), rdev->lldi.vr->qp.start,
		       rdev->lldi.vr->qp.size, rdev->lldi.vr->cq.size,
		       rdev->lldi.vr->cq.size);
806
		return -EINVAL;
807 808
	}

809 810
	rdev->qpmask = rdev->lldi.udb_density - 1;
	rdev->cqmask = rdev->lldi.ucq_density - 1;
811
	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",
812
		 pci_name(rdev->lldi.pdev), rdev->lldi.vr->stag.start,
J
Joe Perches 已提交
813 814 815 816 817 818 819
		 rdev->lldi.vr->stag.size, c4iw_num_stags(rdev),
		 rdev->lldi.vr->pbl.start,
		 rdev->lldi.vr->pbl.size, rdev->lldi.vr->rq.start,
		 rdev->lldi.vr->rq.size,
		 rdev->lldi.vr->qp.start,
		 rdev->lldi.vr->qp.size,
		 rdev->lldi.vr->cq.start,
820 821
		 rdev->lldi.vr->cq.size,
		 rdev->lldi.vr->srq.size);
J
Joe Perches 已提交
822 823 824 825
	pr_debug("udb %pR db_reg %p gts_reg %p qpmask 0x%x cqmask 0x%x\n",
		 &rdev->lldi.pdev->resource[2],
		 rdev->lldi.db_reg, rdev->lldi.gts_reg,
		 rdev->qpmask, rdev->cqmask);
826

827 828
	if (c4iw_num_stags(rdev) == 0)
		return -EINVAL;
829

830 831 832 833
	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;
834
	rdev->stats.srqt.total = rdev->lldi.vr->srq.size;
835 836 837
	rdev->stats.ocqp.total = rdev->lldi.vr->ocq.size;
	rdev->stats.qid.total = rdev->lldi.vr->qp.size;

838 839
	err = c4iw_init_resource(rdev, c4iw_num_stags(rdev),
				 T4_MAX_NUM_PD, rdev->lldi.vr->srq.size);
840
	if (err) {
841
		pr_err("error %d initializing resources\n", err);
842
		return err;
843 844 845
	}
	err = c4iw_pblpool_create(rdev);
	if (err) {
846
		pr_err("error %d initializing pbl pool\n", err);
847
		goto destroy_resource;
848 849 850
	}
	err = c4iw_rqtpool_create(rdev);
	if (err) {
851
		pr_err("error %d initializing rqt pool\n", err);
852
		goto destroy_pblpool;
853
	}
S
Steve Wise 已提交
854 855
	err = c4iw_ocqp_pool_create(rdev);
	if (err) {
856
		pr_err("error %d initializing ocqp pool\n", err);
857
		goto destroy_rqtpool;
S
Steve Wise 已提交
858
	}
859 860
	rdev->status_page = (struct t4_dev_status_page *)
			    __get_free_page(GFP_KERNEL);
861 862
	if (!rdev->status_page) {
		err = -ENOMEM;
863
		goto destroy_ocqp_pool;
864
	}
865 866 867 868
	rdev->status_page->qp_start = rdev->lldi.vr->qp.start;
	rdev->status_page->qp_size = rdev->lldi.vr->qp.size;
	rdev->status_page->cq_start = rdev->lldi.vr->cq.start;
	rdev->status_page->cq_size = rdev->lldi.vr->cq.size;
869

870
	if (c4iw_wr_log) {
K
Kees Cook 已提交
871 872 873
		rdev->wr_log = kcalloc(1 << c4iw_wr_log_size_order,
				       sizeof(*rdev->wr_log),
				       GFP_KERNEL);
874 875 876 877 878
		if (rdev->wr_log) {
			rdev->wr_log_size = 1 << c4iw_wr_log_size_order;
			atomic_set(&rdev->wr_log_idx, 0);
		}
	}
879

880 881 882
	rdev->free_workq = create_singlethread_workqueue("iw_cxgb4_free");
	if (!rdev->free_workq) {
		err = -ENOMEM;
883
		goto err_free_status_page_and_wr_log;
884 885
	}

886
	rdev->status_page->db_off = 0;
887

888 889 890 891 892
	init_completion(&rdev->rqt_compl);
	init_completion(&rdev->pbl_compl);
	kref_init(&rdev->rqt_kref);
	kref_init(&rdev->pbl_kref);

893
	return 0;
894 895 896
err_free_status_page_and_wr_log:
	if (c4iw_wr_log && rdev->wr_log)
		kfree(rdev->wr_log);
897
	free_page((unsigned long)rdev->status_page);
898 899
destroy_ocqp_pool:
	c4iw_ocqp_pool_destroy(rdev);
900
destroy_rqtpool:
S
Steve Wise 已提交
901
	c4iw_rqtpool_destroy(rdev);
902
destroy_pblpool:
903
	c4iw_pblpool_destroy(rdev);
904
destroy_resource:
905 906 907 908 909 910
	c4iw_destroy_resource(&rdev->resource);
	return err;
}

static void c4iw_rdev_close(struct c4iw_rdev *rdev)
{
911
	kfree(rdev->wr_log);
912
	c4iw_release_dev_ucontext(rdev, &rdev->uctx);
913
	free_page((unsigned long)rdev->status_page);
914 915
	c4iw_pblpool_destroy(rdev);
	c4iw_rqtpool_destroy(rdev);
916 917
	wait_for_completion(&rdev->pbl_compl);
	wait_for_completion(&rdev->rqt_compl);
918
	c4iw_ocqp_pool_destroy(rdev);
919
	destroy_workqueue(rdev->free_workq);
920 921 922
	c4iw_destroy_resource(&rdev->resource);
}

923
void c4iw_dealloc(struct uld_ctx *ctx)
924
{
925
	c4iw_rdev_close(&ctx->dev->rdev);
926
	WARN_ON_ONCE(!idr_is_empty(&ctx->dev->cqidr));
927
	idr_destroy(&ctx->dev->cqidr);
928
	WARN_ON_ONCE(!idr_is_empty(&ctx->dev->qpidr));
929
	idr_destroy(&ctx->dev->qpidr);
930
	WARN_ON_ONCE(!idr_is_empty(&ctx->dev->mmidr));
931
	idr_destroy(&ctx->dev->mmidr);
932
	wait_event(ctx->dev->wait, idr_is_empty(&ctx->dev->hwtid_idr));
933 934 935
	idr_destroy(&ctx->dev->hwtid_idr);
	idr_destroy(&ctx->dev->stid_idr);
	idr_destroy(&ctx->dev->atid_idr);
936 937 938 939
	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);
940 941
	ib_dealloc_device(&ctx->dev->ibdev);
	ctx->dev = NULL;
942 943
}

944 945
static void c4iw_remove(struct uld_ctx *ctx)
{
946
	pr_debug("c4iw_dev %p\n", ctx->dev);
947 948 949 950 951 952 953 954
	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 &&
955
	       infop->vr->cq.size > 0;
956 957
}

958 959 960 961 962
static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
{
	struct c4iw_dev *devp;
	int ret;

963
	if (!rdma_supported(infop)) {
964 965
		pr_info("%s: RDMA not supported on this device\n",
			pci_name(infop->pdev));
966 967
		return ERR_PTR(-ENOSYS);
	}
968
	if (!ocqp_supported(infop))
969
		pr_info("%s: On-Chip Queues not supported on this device\n",
970
			pci_name(infop->pdev));
971

972 973
	devp = (struct c4iw_dev *)ib_alloc_device(sizeof(*devp));
	if (!devp) {
974
		pr_err("Cannot allocate ib device\n");
975
		return ERR_PTR(-ENOMEM);
976 977 978
	}
	devp->rdev.lldi = *infop;

979
	/* init various hw-queue params based on lld info */
980 981
	pr_debug("Ing. padding boundary is %d, egrsstatuspagesize = %d\n",
		 devp->rdev.lldi.sge_ingpadboundary,
J
Joe Perches 已提交
982
		 devp->rdev.lldi.sge_egrstatuspagesize);
983 984

	devp->rdev.hw_queue.t4_eq_status_entries =
985
		devp->rdev.lldi.sge_egrstatuspagesize / 64;
986 987 988 989
	devp->rdev.hw_queue.t4_max_eq_size = 65520;
	devp->rdev.hw_queue.t4_max_iq_size = 65520;
	devp->rdev.hw_queue.t4_max_rq_size = 8192 -
		devp->rdev.hw_queue.t4_eq_status_entries - 1;
990
	devp->rdev.hw_queue.t4_max_sq_size =
991 992
		devp->rdev.hw_queue.t4_max_eq_size -
		devp->rdev.hw_queue.t4_eq_status_entries - 1;
993
	devp->rdev.hw_queue.t4_max_qp_depth =
994
		devp->rdev.hw_queue.t4_max_rq_size;
995
	devp->rdev.hw_queue.t4_max_cq_depth =
996
		devp->rdev.hw_queue.t4_max_iq_size - 2;
997 998 999
	devp->rdev.hw_queue.t4_stat_len =
		devp->rdev.lldi.sge_egrstatuspagesize;

1000
	/*
1001
	 * For T5/T6 devices, we map all of BAR2 with WC.
1002 1003 1004 1005
	 * 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);
1006
	if (!is_t4(devp->rdev.lldi.adapter_type)) {
1007 1008 1009
		devp->rdev.bar2_kva = ioremap_wc(devp->rdev.bar2_pa,
			pci_resource_len(devp->rdev.lldi.pdev, 2));
		if (!devp->rdev.bar2_kva) {
1010
			pr_err("Unable to ioremap BAR2\n");
1011
			ib_dealloc_device(&devp->ibdev);
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
			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) {
1022
			pr_err("Unable to ioremap onchip mem\n");
1023
			ib_dealloc_device(&devp->ibdev);
1024 1025 1026
			return ERR_PTR(-EINVAL);
		}
	}
S
Steve Wise 已提交
1027

J
Joe Perches 已提交
1028 1029 1030
	pr_debug("ocq memory: 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);
S
Steve Wise 已提交
1031

1032 1033
	ret = c4iw_rdev_open(&devp->rdev);
	if (ret) {
1034
		pr_err("Unable to open CXIO rdev err %d\n", ret);
1035
		ib_dealloc_device(&devp->ibdev);
1036
		return ERR_PTR(ret);
1037 1038 1039 1040 1041
	}

	idr_init(&devp->cqidr);
	idr_init(&devp->qpidr);
	idr_init(&devp->mmidr);
1042 1043 1044
	idr_init(&devp->hwtid_idr);
	idr_init(&devp->stid_idr);
	idr_init(&devp->atid_idr);
1045
	spin_lock_init(&devp->lock);
1046
	mutex_init(&devp->rdev.stats.lock);
1047
	mutex_init(&devp->db_mutex);
1048
	INIT_LIST_HEAD(&devp->db_fc_list);
1049
	init_waitqueue_head(&devp->wait);
1050
	devp->avail_ird = devp->rdev.lldi.max_ird_adapter;
1051 1052 1053 1054 1055 1056 1057

	if (c4iw_debugfs_root) {
		devp->debugfs_root = debugfs_create_dir(
					pci_name(devp->rdev.lldi.pdev),
					c4iw_debugfs_root);
		setup_debugfs(devp);
	}
1058 1059


1060 1061 1062 1063 1064
	return devp;
}

static void *c4iw_uld_add(const struct cxgb4_lld_info *infop)
{
1065
	struct uld_ctx *ctx;
1066 1067 1068 1069
	static int vers_printed;
	int i;

	if (!vers_printed++)
1070 1071
		pr_info("Chelsio T4/T5 RDMA Driver - version %s\n",
			DRV_VERSION);
1072

1073 1074 1075
	ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
	if (!ctx) {
		ctx = ERR_PTR(-ENOMEM);
1076
		goto out;
1077 1078
	}
	ctx->lldi = *infop;
1079

1080 1081
	pr_debug("found device %s nchan %u nrxq %u ntxq %u nports %u\n",
		 pci_name(ctx->lldi.pdev),
J
Joe Perches 已提交
1082 1083
		 ctx->lldi.nchan, ctx->lldi.nrxq,
		 ctx->lldi.ntxq, ctx->lldi.nports);
1084 1085 1086 1087

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

1089
	for (i = 0; i < ctx->lldi.nrxq; i++)
J
Joe Perches 已提交
1090
		pr_debug("rxqid[%u] %u\n", i, ctx->lldi.rxq_ids[i]);
1091
out:
1092
	return ctx;
1093 1094
}

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
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;

B
Bart Van Assche 已提交
1113 1114
	__skb_put(skb, gl->tot_len + sizeof(struct cpl_pass_accept_req) +
		  sizeof(struct rss_header) - pktshift);
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146

	/*
	 * 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) {
1147
		pr_info("%s no handler opcode 0x%x...\n", __func__, opcode);
1148 1149 1150 1151 1152 1153 1154 1155 1156
		kfree_skb(skb);
		goto out;
	}
	c4iw_handlers[opcode](dev, skb);
	return 1;
out:
	return 0;
}

1157 1158 1159
static int c4iw_uld_rx_handler(void *handle, const __be64 *rsp,
			const struct pkt_gl *gl)
{
1160 1161
	struct uld_ctx *ctx = handle;
	struct c4iw_dev *dev = ctx->dev;
1162
	struct sk_buff *skb;
1163
	u8 opcode;
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178

	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);
1179 1180 1181 1182 1183
		return 0;
	} else if (unlikely(*(u8 *)rsp != *(u8 *)gl->va)) {
		if (recv_rx_pkt(dev, gl, rsp))
			return 0;

1184 1185 1186 1187 1188
		pr_info("%s: unexpected FL contents at %p, RSS %#llx, FL %#llx, len %u\n",
			pci_name(ctx->lldi.pdev), gl->va,
			be64_to_cpu(*rsp),
			be64_to_cpu(*(__force __be64 *)gl->va),
			gl->tot_len);
1189

1190 1191
		return 0;
	} else {
1192
		skb = cxgb4_pktgl_to_skb(gl, 128, 128);
1193 1194 1195 1196
		if (unlikely(!skb))
			goto nomem;
	}

1197
	opcode = *(u8 *)rsp;
1198
	if (c4iw_handlers[opcode]) {
1199
		c4iw_handlers[opcode](dev, skb);
1200
	} else {
1201
		pr_info("%s no handler opcode 0x%x...\n", __func__, opcode);
1202 1203
		kfree_skb(skb);
	}
1204 1205 1206 1207 1208 1209 1210 1211

	return 0;
nomem:
	return -1;
}

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

1214
	pr_debug("new_state %u\n", new_state);
1215 1216
	switch (new_state) {
	case CXGB4_STATE_UP:
1217
		pr_info("%s: Up\n", pci_name(ctx->lldi.pdev));
1218 1219
		if (!ctx->dev) {
			ctx->dev = c4iw_alloc(&ctx->lldi);
1220
			if (IS_ERR(ctx->dev)) {
1221
				pr_err("%s: initialization failed: %ld\n",
1222 1223 1224 1225 1226
				       pci_name(ctx->lldi.pdev),
				       PTR_ERR(ctx->dev));
				ctx->dev = NULL;
				break;
			}
1227 1228 1229

			INIT_WORK(&ctx->reg_work, c4iw_register_device);
			queue_work(reg_workq, &ctx->reg_work);
1230 1231 1232
		}
		break;
	case CXGB4_STATE_DOWN:
1233
		pr_info("%s: Down\n", pci_name(ctx->lldi.pdev));
1234 1235
		if (ctx->dev)
			c4iw_remove(ctx);
1236
		break;
1237
	case CXGB4_STATE_FATAL_ERROR:
1238
	case CXGB4_STATE_START_RECOVERY:
1239
		pr_info("%s: Fatal Error\n", pci_name(ctx->lldi.pdev));
1240
		if (ctx->dev) {
1241 1242
			struct ib_event event;

1243
			ctx->dev->rdev.flags |= T4_FATAL_ERROR;
1244 1245
			memset(&event, 0, sizeof event);
			event.event  = IB_EVENT_DEVICE_FATAL;
1246
			event.device = &ctx->dev->ibdev;
1247
			ib_dispatch_event(&event);
1248
			c4iw_remove(ctx);
1249
		}
1250 1251
		break;
	case CXGB4_STATE_DETACH:
1252
		pr_info("%s: Detach\n", pci_name(ctx->lldi.pdev));
1253 1254
		if (ctx->dev)
			c4iw_remove(ctx);
1255 1256
		break;
	}
1257 1258 1259
	return 0;
}

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
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)
{
1270 1271 1272 1273 1274 1275
	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)
1276
		idr_for_each(&ctx->dev->qpidr, disable_qp_db, NULL);
1277 1278 1279
	else
		ctx->dev->rdev.status_page->db_off = 1;
	spin_unlock_irqrestore(&ctx->dev->lock, flags);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
}

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

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

1290 1291 1292
static void resume_rc_qp(struct c4iw_qp *qp)
{
	spin_lock(&qp->lock);
1293
	t4_ring_sq_db(&qp->wq, qp->wq.sq.wq_pidx_inc, NULL);
1294
	qp->wq.sq.wq_pidx_inc = 0;
1295
	t4_ring_rq_db(&qp->wq, qp->wq.rq.wq_pidx_inc, NULL);
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	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;
	}
}

1315 1316 1317
static void resume_queues(struct uld_ctx *ctx)
{
	spin_lock_irq(&ctx->dev->lock);
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
	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;
			}
		}
1350
	}
1351 1352 1353
out:
	if (ctx->dev->db_state != NORMAL)
		ctx->dev->rdev.stats.db_fc_interruptions++;
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
	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;
}

1379
static void deref_qps(struct qp_list *qp_list)
1380 1381 1382
{
	int idx;

1383 1384
	for (idx = 0; idx < qp_list->idx; idx++)
		c4iw_qp_rem_ref(&qp_list->qps[idx]->ibqp);
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
}

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];

1395 1396
		spin_lock_irq(&qp->rhp->lock);
		spin_lock(&qp->lock);
1397 1398 1399 1400 1401
		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) {
1402
			pr_err("%s: Fatal error - DB overflow recovery failed - error syncing SQ qid %u\n",
1403
			       pci_name(ctx->lldi.pdev), qp->wq.sq.qid);
1404 1405
			spin_unlock(&qp->lock);
			spin_unlock_irq(&qp->rhp->lock);
1406 1407
			return;
		}
1408
		qp->wq.sq.wq_pidx_inc = 0;
1409 1410 1411 1412 1413 1414 1415

		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) {
1416
			pr_err("%s: Fatal error - DB overflow recovery failed - error syncing RQ qid %u\n",
1417
			       pci_name(ctx->lldi.pdev), qp->wq.rq.qid);
1418 1419
			spin_unlock(&qp->lock);
			spin_unlock_irq(&qp->rhp->lock);
1420 1421
			return;
		}
1422 1423 1424
		qp->wq.rq.wq_pidx_inc = 0;
		spin_unlock(&qp->lock);
		spin_unlock_irq(&qp->rhp->lock);
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446

		/* 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) {
1447
		pr_err("%s: Fatal error - DB overflow recovery failed\n",
1448
		       pci_name(ctx->lldi.pdev));
1449
		return;
1450 1451 1452 1453
	}

	/* Count active queues so we can build a list of queues to recover */
	spin_lock_irq(&ctx->dev->lock);
1454 1455
	WARN_ON(ctx->dev->db_state != STOPPED);
	ctx->dev->db_state = RECOVERY;
1456 1457
	idr_for_each(&ctx->dev->qpidr, count_qps, &count);

K
Kees Cook 已提交
1458
	qp_list.qps = kcalloc(count, sizeof(*qp_list.qps), GFP_ATOMIC);
1459 1460
	if (!qp_list.qps) {
		spin_unlock_irq(&ctx->dev->lock);
1461
		return;
1462 1463 1464 1465 1466 1467
	}
	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);

1468
	spin_unlock_irq(&ctx->dev->lock);
1469 1470 1471 1472 1473

	/* 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 */
1474
	deref_qps(&qp_list);
1475 1476 1477
	kfree(qp_list.qps);

	spin_lock_irq(&ctx->dev->lock);
1478 1479
	WARN_ON(ctx->dev->db_state != RECOVERY);
	ctx->dev->db_state = STOPPED;
1480
	spin_unlock_irq(&ctx->dev->lock);
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
}

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:
1499
		recover_queues(ctx);
1500 1501 1502 1503 1504
		mutex_lock(&ctx->dev->rdev.stats.lock);
		ctx->dev->rdev.stats.db_drop++;
		mutex_unlock(&ctx->dev->rdev.stats.lock);
		break;
	default:
1505 1506
		pr_warn("%s: unknown control cmd %u\n",
			pci_name(ctx->lldi.pdev), control);
1507 1508 1509 1510 1511
		break;
	}
	return 0;
}

1512 1513
static struct cxgb4_uld_info c4iw_uld_info = {
	.name = DRV_NAME,
1514
	.nrxq = MAX_ULD_QSETS,
1515
	.ntxq = MAX_ULD_QSETS,
1516 1517 1518
	.rxq_size = 511,
	.ciq = true,
	.lro = false,
1519 1520 1521
	.add = c4iw_uld_add,
	.rx_handler = c4iw_uld_rx_handler,
	.state_change = c4iw_uld_state_change,
1522
	.control = c4iw_uld_control,
1523 1524
};

1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
void _c4iw_free_wr_wait(struct kref *kref)
{
	struct c4iw_wr_wait *wr_waitp;

	wr_waitp = container_of(kref, struct c4iw_wr_wait, kref);
	pr_debug("Free wr_wait %p\n", wr_waitp);
	kfree(wr_waitp);
}

struct c4iw_wr_wait *c4iw_alloc_wr_wait(gfp_t gfp)
{
	struct c4iw_wr_wait *wr_waitp;

	wr_waitp = kzalloc(sizeof(*wr_waitp), gfp);
	if (wr_waitp) {
		kref_init(&wr_waitp->kref);
		pr_debug("wr_wait %p\n", wr_waitp);
	}
	return wr_waitp;
}

1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
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)
1556
		pr_warn("could not create debugfs entry, continuing\n");
1557

1558 1559 1560 1561 1562 1563
	reg_workq = create_singlethread_workqueue("Register_iWARP_device");
	if (!reg_workq) {
		pr_err("Failed creating workqueue to register iwarp device\n");
		return -ENOMEM;
	}

1564 1565 1566 1567 1568 1569 1570
	cxgb4_register_uld(CXGB4_ULD_RDMA, &c4iw_uld_info);

	return 0;
}

static void __exit c4iw_exit_module(void)
{
1571
	struct uld_ctx *ctx, *tmp;
1572 1573

	mutex_lock(&dev_mutex);
1574 1575 1576 1577
	list_for_each_entry_safe(ctx, tmp, &uld_ctx_list, entry) {
		if (ctx->dev)
			c4iw_remove(ctx);
		kfree(ctx);
1578 1579
	}
	mutex_unlock(&dev_mutex);
1580 1581
	flush_workqueue(reg_workq);
	destroy_workqueue(reg_workq);
1582
	cxgb4_unregister_uld(CXGB4_ULD_RDMA);
1583 1584 1585 1586 1587 1588
	c4iw_cm_term();
	debugfs_remove_recursive(c4iw_debugfs_root);
}

module_init(c4iw_init_module);
module_exit(c4iw_exit_module);