iwch_provider.c 37.4 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 35 36 37 38 39
/*
 * Copyright (c) 2006 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/device.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/list.h>
40
#include <linux/sched/mm.h>
41 42
#include <linux/spinlock.h>
#include <linux/ethtool.h>
43
#include <linux/rtnetlink.h>
44
#include <linux/inetdevice.h>
45
#include <linux/slab.h>
46 47 48 49 50 51 52 53

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/byteorder.h>

#include <rdma/iw_cm.h>
#include <rdma/ib_verbs.h>
#include <rdma/ib_smi.h>
54
#include <rdma/ib_umem.h>
55
#include <rdma/ib_user_verbs.h>
56
#include <rdma/uverbs_ioctl.h>
57 58 59 60 61

#include "cxio_hal.h"
#include "iwch.h"
#include "iwch_provider.h"
#include "iwch_cm.h"
62
#include <rdma/cxgb3-abi.h>
63
#include "common.h"
64

65
static void iwch_dealloc_ucontext(struct ib_ucontext *context)
66 67 68 69 70
{
	struct iwch_dev *rhp = to_iwch_dev(context->device);
	struct iwch_ucontext *ucontext = to_iwch_ucontext(context);
	struct iwch_mm_entry *mm, *tmp;

J
Joe Perches 已提交
71
	pr_debug("%s context %p\n", __func__, context);
72 73 74 75 76
	list_for_each_entry_safe(mm, tmp, &ucontext->mmaps, entry)
		kfree(mm);
	cxio_release_ucontext(&rhp->rdev, &ucontext->uctx);
}

77 78
static int iwch_alloc_ucontext(struct ib_ucontext *ucontext,
			       struct ib_udata *udata)
79
{
80 81
	struct ib_device *ibdev = ucontext->device;
	struct iwch_ucontext *context = to_iwch_ucontext(ucontext);
82 83
	struct iwch_dev *rhp = to_iwch_dev(ibdev);

J
Joe Perches 已提交
84
	pr_debug("%s ibdev %p\n", __func__, ibdev);
85 86 87
	cxio_init_ucontext(&rhp->rdev, &context->uctx);
	INIT_LIST_HEAD(&context->mmaps);
	spin_lock_init(&context->mmap_lock);
88
	return 0;
89 90 91 92 93 94
}

static int iwch_destroy_cq(struct ib_cq *ib_cq)
{
	struct iwch_cq *chp;

J
Joe Perches 已提交
95
	pr_debug("%s ib_cq %p\n", __func__, ib_cq);
96 97
	chp = to_iwch_cq(ib_cq);

M
Matthew Wilcox 已提交
98
	xa_erase_irq(&chp->rhp->cqs, chp->cq.cqid);
99 100 101 102 103 104 105 106
	atomic_dec(&chp->refcnt);
	wait_event(chp->wait, !atomic_read(&chp->refcnt));

	cxio_destroy_cq(&chp->rhp->rdev, &chp->cq);
	kfree(chp);
	return 0;
}

107 108 109 110
static struct ib_cq *iwch_create_cq(struct ib_device *ibdev,
				    const struct ib_cq_init_attr *attr,
				    struct ib_ucontext *ib_context,
				    struct ib_udata *udata)
111
{
112
	int entries = attr->cqe;
113 114 115 116 117
	struct iwch_dev *rhp;
	struct iwch_cq *chp;
	struct iwch_create_cq_resp uresp;
	struct iwch_create_cq_req ureq;
	struct iwch_ucontext *ucontext = NULL;
118 119
	static int warned;
	size_t resplen;
120

J
Joe Perches 已提交
121
	pr_debug("%s ib_dev %p entries %d\n", __func__, ibdev, entries);
122 123 124
	if (attr->flags)
		return ERR_PTR(-EINVAL);

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	rhp = to_iwch_dev(ibdev);
	chp = kzalloc(sizeof(*chp), GFP_KERNEL);
	if (!chp)
		return ERR_PTR(-ENOMEM);

	if (ib_context) {
		ucontext = to_iwch_ucontext(ib_context);
		if (!t3a_device(rhp)) {
			if (ib_copy_from_udata(&ureq, udata, sizeof (ureq))) {
				kfree(chp);
				return ERR_PTR(-EFAULT);
			}
			chp->user_rptr_addr = (u32 __user *)(unsigned long)ureq.user_rptr_addr;
		}
	}

	if (t3a_device(rhp)) {

		/*
		 * T3A: Add some fluff to handle extra CQEs inserted
		 * for various errors.
		 * Additional CQE possibilities:
		 *      TERMINATE,
		 *      incoming RDMA WRITE Failures
		 *      incoming RDMA READ REQUEST FAILUREs
		 * NOTE: We cannot ensure the CQ won't overflow.
		 */
		entries += 16;
	}
	entries = roundup_pow_of_two(entries);
	chp->cq.size_log2 = ilog2(entries);

157
	if (cxio_create_cq(&rhp->rdev, &chp->cq, !ucontext)) {
158 159 160 161
		kfree(chp);
		return ERR_PTR(-ENOMEM);
	}
	chp->rhp = rhp;
162
	chp->ibcq.cqe = 1 << chp->cq.size_log2;
163
	spin_lock_init(&chp->lock);
164
	spin_lock_init(&chp->comp_handler_lock);
165 166
	atomic_set(&chp->refcnt, 1);
	init_waitqueue_head(&chp->wait);
M
Matthew Wilcox 已提交
167
	if (xa_store_irq(&rhp->cqs, chp->cq.cqid, chp, GFP_KERNEL)) {
168 169 170 171
		cxio_destroy_cq(&chp->rhp->rdev, &chp->cq);
		kfree(chp);
		return ERR_PTR(-ENOMEM);
	}
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

	if (ucontext) {
		struct iwch_mm_entry *mm;

		mm = kmalloc(sizeof *mm, GFP_KERNEL);
		if (!mm) {
			iwch_destroy_cq(&chp->ibcq);
			return ERR_PTR(-ENOMEM);
		}
		uresp.cqid = chp->cq.cqid;
		uresp.size_log2 = chp->cq.size_log2;
		spin_lock(&ucontext->mmap_lock);
		uresp.key = ucontext->key;
		ucontext->key += PAGE_SIZE;
		spin_unlock(&ucontext->mmap_lock);
187 188 189 190
		mm->key = uresp.key;
		mm->addr = virt_to_phys(chp->cq.queue);
		if (udata->outlen < sizeof uresp) {
			if (!warned++)
191
				pr_warn("Warning - downlevel libcxgb3 (non-fatal)\n");
192 193 194 195 196 197 198
			mm->len = PAGE_ALIGN((1UL << uresp.size_log2) *
					     sizeof(struct t3_cqe));
			resplen = sizeof(struct iwch_create_cq_resp_v0);
		} else {
			mm->len = PAGE_ALIGN(((1UL << uresp.size_log2) + 1) *
					     sizeof(struct t3_cqe));
			uresp.memsize = mm->len;
199
			uresp.reserved = 0;
200 201 202
			resplen = sizeof uresp;
		}
		if (ib_copy_to_udata(udata, &uresp, resplen)) {
203 204 205 206 207 208
			kfree(mm);
			iwch_destroy_cq(&chp->ibcq);
			return ERR_PTR(-EFAULT);
		}
		insert_mmap(ucontext, mm);
	}
J
Joe Perches 已提交
209 210 211
	pr_debug("created cqid 0x%0x chp %p size 0x%0x, dma_addr 0x%0llx\n",
		 chp->cq.cqid, chp, (1 << chp->cq.size_log2),
		 (unsigned long long)chp->cq.dma_addr);
212 213 214 215 216 217 218 219 220 221
	return &chp->ibcq;
}

static int iwch_resize_cq(struct ib_cq *cq, int cqe, struct ib_udata *udata)
{
#ifdef notyet
	struct iwch_cq *chp = to_iwch_cq(cq);
	struct t3_cq oldcq, newcq;
	int ret;

J
Joe Perches 已提交
222
	pr_debug("%s ib_cq %p cqe %d\n", __func__, cq, cqe);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

	/* We don't downsize... */
	if (cqe <= cq->cqe)
		return 0;

	/* create new t3_cq with new size */
	cqe = roundup_pow_of_two(cqe+1);
	newcq.size_log2 = ilog2(cqe);

	/* Dont allow resize to less than the current wce count */
	if (cqe < Q_COUNT(chp->cq.rptr, chp->cq.wptr)) {
		return -ENOMEM;
	}

	/* Quiesce all QPs using this CQ */
	ret = iwch_quiesce_qps(chp);
	if (ret) {
		return ret;
	}

	ret = cxio_create_cq(&chp->rhp->rdev, &newcq);
	if (ret) {
		return ret;
	}

	/* copy CQEs */
	memcpy(newcq.queue, chp->cq.queue, (1 << chp->cq.size_log2) *
				        sizeof(struct t3_cqe));

	/* old iwch_qp gets new t3_cq but keeps old cqid */
	oldcq = chp->cq;
	chp->cq = newcq;
	chp->cq.cqid = oldcq.cqid;

	/* resize new t3_cq to update the HW context */
	ret = cxio_resize_cq(&chp->rhp->rdev, &chp->cq);
	if (ret) {
		chp->cq = oldcq;
		return ret;
	}
	chp->ibcq.cqe = (1<<chp->cq.size_log2) - 1;

	/* destroy old t3_cq */
	oldcq.cqid = newcq.cqid;
	ret = cxio_destroy_cq(&chp->rhp->rdev, &oldcq);
	if (ret) {
269
		pr_err("%s - cxio_destroy_cq failed %d\n", __func__, ret);
270 271 272 273 274 275 276 277 278 279 280 281
	}

	/* add user hooks here */

	/* resume qps */
	ret = iwch_resume_qps(chp);
	return ret;
#else
	return -ENOSYS;
#endif
}

282
static int iwch_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
283 284 285 286 287 288 289 290 291 292
{
	struct iwch_dev *rhp;
	struct iwch_cq *chp;
	enum t3_cq_opcode cq_op;
	int err;
	unsigned long flag;
	u32 rptr;

	chp = to_iwch_cq(ibcq);
	rhp = chp->rhp;
293
	if ((flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
294 295 296 297 298 299 300 301 302 303
		cq_op = CQ_ARM_SE;
	else
		cq_op = CQ_ARM_AN;
	if (chp->user_rptr_addr) {
		if (get_user(rptr, chp->user_rptr_addr))
			return -EFAULT;
		spin_lock_irqsave(&chp->lock, flag);
		chp->cq.rptr = rptr;
	} else
		spin_lock_irqsave(&chp->lock, flag);
J
Joe Perches 已提交
304
	pr_debug("%s rptr 0x%x\n", __func__, chp->cq.rptr);
305 306
	err = cxio_hal_cq_op(&rhp->rdev, &chp->cq, cq_op, 0);
	spin_unlock_irqrestore(&chp->lock, flag);
307
	if (err < 0)
308
		pr_err("Error %d rearming CQID 0x%x\n", err, chp->cq.cqid);
309 310
	if (err > 0 && !(flags & IB_CQ_REPORT_MISSED_EVENTS))
		err = 0;
311 312 313 314 315 316 317 318 319 320 321
	return err;
}

static int iwch_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
{
	int len = vma->vm_end - vma->vm_start;
	u32 key = vma->vm_pgoff << PAGE_SHIFT;
	struct cxio_rdev *rdev_p;
	int ret = 0;
	struct iwch_mm_entry *mm;
	struct iwch_ucontext *ucontext;
322
	u64 addr;
323

J
Joe Perches 已提交
324 325
	pr_debug("%s pgoff 0x%lx key 0x%x len %d\n", __func__, vma->vm_pgoff,
		 key, len);
326 327 328 329 330 331 332 333 334 335 336

	if (vma->vm_start & (PAGE_SIZE-1)) {
	        return -EINVAL;
	}

	rdev_p = &(to_iwch_dev(context->device)->rdev);
	ucontext = to_iwch_ucontext(context);

	mm = remove_mmap(ucontext, key, len);
	if (!mm)
		return -EINVAL;
337
	addr = mm->addr;
338 339
	kfree(mm);

340 341
	if ((addr >= rdev_p->rnic_info.udbell_physbase) &&
	    (addr < (rdev_p->rnic_info.udbell_physbase +
342 343 344 345 346 347 348 349 350 351 352 353 354
		       rdev_p->rnic_info.udbell_len))) {

		/*
		 * Map T3 DB register.
		 */
		if (vma->vm_flags & VM_READ) {
			return -EPERM;
		}

		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
		vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
		vma->vm_flags &= ~VM_MAYREAD;
		ret = io_remap_pfn_range(vma, vma->vm_start,
355
					 addr >> PAGE_SHIFT,
356 357 358 359 360 361 362
				         len, vma->vm_page_prot);
	} else {

		/*
		 * Map WQ or CQ contig dma memory...
		 */
		ret = remap_pfn_range(vma, vma->vm_start,
363
				      addr >> PAGE_SHIFT,
364 365 366 367 368 369
				      len, vma->vm_page_prot);
	}

	return ret;
}

370
static void iwch_deallocate_pd(struct ib_pd *pd)
371 372 373 374 375 376
{
	struct iwch_dev *rhp;
	struct iwch_pd *php;

	php = to_iwch_pd(pd);
	rhp = php->rhp;
J
Joe Perches 已提交
377
	pr_debug("%s ibpd %p pdid 0x%x\n", __func__, pd, php->pdid);
378 379 380
	cxio_hal_put_pdid(rhp->rdev.rscp, php->pdid);
}

381 382
static int iwch_allocate_pd(struct ib_pd *pd, struct ib_ucontext *context,
			    struct ib_udata *udata)
383
{
384 385
	struct iwch_pd *php = to_iwch_pd(pd);
	struct ib_device *ibdev = pd->device;
386 387 388
	u32 pdid;
	struct iwch_dev *rhp;

J
Joe Perches 已提交
389
	pr_debug("%s ibdev %p\n", __func__, ibdev);
390 391 392
	rhp = (struct iwch_dev *) ibdev;
	pdid = cxio_hal_get_pdid(rhp->rdev.rscp);
	if (!pdid)
393 394
		return -EINVAL;

395 396 397
	php->pdid = pdid;
	php->rhp = rhp;
	if (context) {
398 399 400
		struct iwch_alloc_pd_resp resp = {.pdid = php->pdid};

		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
401
			iwch_deallocate_pd(&php->ibpd);
402
			return -EFAULT;
403 404
		}
	}
J
Joe Perches 已提交
405
	pr_debug("%s pdid 0x%0x ptr 0x%p\n", __func__, pdid, php);
406
	return 0;
407 408 409 410 411 412 413 414
}

static int iwch_dereg_mr(struct ib_mr *ib_mr)
{
	struct iwch_dev *rhp;
	struct iwch_mr *mhp;
	u32 mmid;

J
Joe Perches 已提交
415
	pr_debug("%s ib_mr %p\n", __func__, ib_mr);
416 417

	mhp = to_iwch_mr(ib_mr);
418
	kfree(mhp->pages);
419 420 421 422
	rhp = mhp->rhp;
	mmid = mhp->attr.stag >> 8;
	cxio_dereg_mem(&rhp->rdev, mhp->attr.stag, mhp->attr.pbl_size,
		       mhp->attr.pbl_addr);
423
	iwch_free_pbl(mhp);
424 425 426
	remove_handle(rhp, &rhp->mmidr, mmid);
	if (mhp->kva)
		kfree((void *) (unsigned long) mhp->kva);
427 428
	if (mhp->umem)
		ib_umem_release(mhp->umem);
J
Joe Perches 已提交
429
	pr_debug("%s mmid 0x%x ptr %p\n", __func__, mmid, mhp);
430 431 432 433
	kfree(mhp);
	return 0;
}

434
static struct ib_mr *iwch_get_dma_mr(struct ib_pd *pd, int acc)
435
{
436 437 438 439
	const u64 total_size = 0xffffffff;
	const u64 mask = (total_size + PAGE_SIZE - 1) & PAGE_MASK;
	struct iwch_pd *php = to_iwch_pd(pd);
	struct iwch_dev *rhp = php->rhp;
440
	struct iwch_mr *mhp;
441 442
	__be64 *page_list;
	int shift = 26, npages, ret, i;
443

J
Joe Perches 已提交
444
	pr_debug("%s ib_pd %p\n", __func__, pd);
445 446 447 448 449

	/*
	 * T3 only supports 32 bits of size.
	 */
	if (sizeof(phys_addr_t) > 4) {
450
		pr_warn_once("Cannot support dma_mrs on this platform\n");
451 452
		return ERR_PTR(-ENOTSUPP);
	}
453 454 455 456 457

	mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
	if (!mhp)
		return ERR_PTR(-ENOMEM);

458 459
	mhp->rhp = rhp;

460 461
	npages = (total_size + (1ULL << shift) - 1) >> shift;
	if (!npages) {
462 463 464 465
		ret = -EINVAL;
		goto err;
	}

466 467 468
	page_list = kmalloc_array(npages, sizeof(u64), GFP_KERNEL);
	if (!page_list) {
		ret = -ENOMEM;
469 470 471
		goto err;
	}

472 473 474
	for (i = 0; i < npages; i++)
		page_list[i] = cpu_to_be64((u64)i << shift);

J
Joe Perches 已提交
475 476
	pr_debug("%s mask 0x%llx shift %d len %lld pbl_size %d\n",
		 __func__, mask, shift, total_size, npages);
477

478 479 480 481 482 483 484 485 486 487 488
	ret = iwch_alloc_pbl(mhp, npages);
	if (ret) {
		kfree(page_list);
		goto err_pbl;
	}

	ret = iwch_write_pbl(mhp, page_list, npages, 0);
	kfree(page_list);
	if (ret)
		goto err_pbl;

489 490 491
	mhp->attr.pdid = php->pdid;
	mhp->attr.zbva = 0;

492
	mhp->attr.perms = iwch_ib_to_tpt_access(acc);
493
	mhp->attr.va_fbo = 0;
494 495 496 497
	mhp->attr.page_size = shift - 12;

	mhp->attr.len = (u32) total_size;
	mhp->attr.pbl_size = npages;
498 499 500 501
	ret = iwch_register_mem(rhp, php, mhp, shift);
	if (ret)
		goto err_pbl;

502
	return &mhp->ibmr;
503 504 505 506

err_pbl:
	iwch_free_pbl(mhp);

507 508 509 510 511
err:
	kfree(mhp);
	return ERR_PTR(ret);
}

512 513
static struct ib_mr *iwch_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
				      u64 virt, int acc, struct ib_udata *udata)
514 515
{
	__be64 *pages;
516
	int shift, n, i;
517 518 519 520 521
	int err = 0;
	struct iwch_dev *rhp;
	struct iwch_pd *php;
	struct iwch_mr *mhp;
	struct iwch_reg_user_mr_resp uresp;
522
	struct sg_dma_page_iter sg_iter;
J
Joe Perches 已提交
523
	pr_debug("%s ib_pd %p\n", __func__, pd);
524 525 526 527 528 529 530

	php = to_iwch_pd(pd);
	rhp = php->rhp;
	mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
	if (!mhp)
		return ERR_PTR(-ENOMEM);

531 532
	mhp->rhp = rhp;

533
	mhp->umem = ib_umem_get(udata, start, length, acc, 0);
534 535 536 537 538 539
	if (IS_ERR(mhp->umem)) {
		err = PTR_ERR(mhp->umem);
		kfree(mhp);
		return ERR_PTR(err);
	}

540
	shift = PAGE_SHIFT;
541

542
	n = mhp->umem->nmap;
543

544 545 546 547 548
	err = iwch_alloc_pbl(mhp, n);
	if (err)
		goto err;

	pages = (__be64 *) __get_free_page(GFP_KERNEL);
549 550
	if (!pages) {
		err = -ENOMEM;
551
		goto err_pbl;
552 553 554 555
	}

	i = n = 0;

556 557 558 559 560 561 562 563 564
	for_each_sg_dma_page(mhp->umem->sg_head.sgl, &sg_iter, mhp->umem->nmap, 0) {
		pages[i++] = cpu_to_be64(sg_page_iter_dma_address(&sg_iter));
		if (i == PAGE_SIZE / sizeof *pages) {
			err = iwch_write_pbl(mhp, pages, i, n);
			if (err)
				goto pbl_done;
			n += i;
			i = 0;
		}
565
	}
566

567 568 569 570 571 572 573 574
	if (i)
		err = iwch_write_pbl(mhp, pages, i, n);

pbl_done:
	free_page((unsigned long) pages);
	if (err)
		goto err_pbl;

575 576
	mhp->attr.pdid = php->pdid;
	mhp->attr.zbva = 0;
577
	mhp->attr.perms = iwch_ib_to_tpt_access(acc);
578
	mhp->attr.va_fbo = virt;
579
	mhp->attr.page_size = shift - 12;
580
	mhp->attr.len = (u32) length;
581 582

	err = iwch_register_mem(rhp, php, mhp, shift);
583
	if (err)
584
		goto err_pbl;
585

586
	if (udata && !t3a_device(rhp)) {
587
		uresp.pbl_addr = (mhp->attr.pbl_addr -
588
				 rhp->rdev.rnic_info.pbl_base) >> 3;
J
Joe Perches 已提交
589 590
		pr_debug("%s user resp pbl_addr 0x%x\n", __func__,
			 uresp.pbl_addr);
591 592 593 594 595 596 597 598 599 600

		if (ib_copy_to_udata(udata, &uresp, sizeof (uresp))) {
			iwch_dereg_mr(&mhp->ibmr);
			err = -EFAULT;
			goto err;
		}
	}

	return &mhp->ibmr;

601 602 603
err_pbl:
	iwch_free_pbl(mhp);

604
err:
605
	ib_umem_release(mhp->umem);
606 607 608 609
	kfree(mhp);
	return ERR_PTR(err);
}

610 611
static struct ib_mw *iwch_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
				   struct ib_udata *udata)
612 613 614 615 616 617 618 619
{
	struct iwch_dev *rhp;
	struct iwch_pd *php;
	struct iwch_mw *mhp;
	u32 mmid;
	u32 stag = 0;
	int ret;

620 621 622
	if (type != IB_MW_TYPE_1)
		return ERR_PTR(-EINVAL);

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	php = to_iwch_pd(pd);
	rhp = php->rhp;
	mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
	if (!mhp)
		return ERR_PTR(-ENOMEM);
	ret = cxio_allocate_window(&rhp->rdev, &stag, php->pdid);
	if (ret) {
		kfree(mhp);
		return ERR_PTR(ret);
	}
	mhp->rhp = rhp;
	mhp->attr.pdid = php->pdid;
	mhp->attr.type = TPT_MW;
	mhp->attr.stag = stag;
	mmid = (stag) >> 8;
638
	mhp->ibmw.rkey = stag;
639 640 641 642 643
	if (insert_handle(rhp, &rhp->mmidr, mhp, mmid)) {
		cxio_deallocate_window(&rhp->rdev, mhp->attr.stag);
		kfree(mhp);
		return ERR_PTR(-ENOMEM);
	}
J
Joe Perches 已提交
644
	pr_debug("%s mmid 0x%x mhp %p stag 0x%x\n", __func__, mmid, mhp, stag);
645 646 647 648 649 650 651 652 653 654 655 656 657 658
	return &(mhp->ibmw);
}

static int iwch_dealloc_mw(struct ib_mw *mw)
{
	struct iwch_dev *rhp;
	struct iwch_mw *mhp;
	u32 mmid;

	mhp = to_iwch_mw(mw);
	rhp = mhp->rhp;
	mmid = (mw->rkey) >> 8;
	cxio_deallocate_window(&rhp->rdev, mhp->attr.stag);
	remove_handle(rhp, &rhp->mmidr, mmid);
J
Joe Perches 已提交
659
	pr_debug("%s ib_mw %p mmid 0x%x ptr %p\n", __func__, mw, mmid, mhp);
660
	kfree(mhp);
661 662 663
	return 0;
}

S
Sagi Grimberg 已提交
664 665 666
static struct ib_mr *iwch_alloc_mr(struct ib_pd *pd,
				   enum ib_mr_type mr_type,
				   u32 max_num_sg)
667 668 669 670 671 672
{
	struct iwch_dev *rhp;
	struct iwch_pd *php;
	struct iwch_mr *mhp;
	u32 mmid;
	u32 stag = 0;
673
	int ret = -ENOMEM;
674

S
Sagi Grimberg 已提交
675 676 677 678
	if (mr_type != IB_MR_TYPE_MEM_REG ||
	    max_num_sg > T3_MAX_FASTREG_DEPTH)
		return ERR_PTR(-EINVAL);

679 680 681 682
	php = to_iwch_pd(pd);
	rhp = php->rhp;
	mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
	if (!mhp)
683
		goto err;
684

685
	mhp->pages = kcalloc(max_num_sg, sizeof(u64), GFP_KERNEL);
686
	if (!mhp->pages)
687 688
		goto pl_err;

689
	mhp->rhp = rhp;
S
Sagi Grimberg 已提交
690
	ret = iwch_alloc_pbl(mhp, max_num_sg);
691 692
	if (ret)
		goto err1;
S
Sagi Grimberg 已提交
693
	mhp->attr.pbl_size = max_num_sg;
694 695
	ret = cxio_allocate_stag(&rhp->rdev, &stag, php->pdid,
				 mhp->attr.pbl_size, mhp->attr.pbl_addr);
696 697
	if (ret)
		goto err2;
698 699 700 701 702 703
	mhp->attr.pdid = php->pdid;
	mhp->attr.type = TPT_NON_SHARED_MR;
	mhp->attr.stag = stag;
	mhp->attr.state = 1;
	mmid = (stag) >> 8;
	mhp->ibmr.rkey = mhp->ibmr.lkey = stag;
704 705
	ret = insert_handle(rhp, &rhp->mmidr, mhp, mmid);
	if (ret)
706 707
		goto err3;

J
Joe Perches 已提交
708
	pr_debug("%s mmid 0x%x mhp %p stag 0x%x\n", __func__, mmid, mhp, stag);
709
	return &(mhp->ibmr);
710 711 712 713 714 715
err3:
	cxio_dereg_mem(&rhp->rdev, stag, mhp->attr.pbl_size,
		       mhp->attr.pbl_addr);
err2:
	iwch_free_pbl(mhp);
err1:
716 717
	kfree(mhp->pages);
pl_err:
718 719 720
	kfree(mhp);
err:
	return ERR_PTR(ret);
721 722
}

723 724 725 726 727 728 729 730 731 732 733 734
static int iwch_set_page(struct ib_mr *ibmr, u64 addr)
{
	struct iwch_mr *mhp = to_iwch_mr(ibmr);

	if (unlikely(mhp->npages == mhp->attr.pbl_size))
		return -ENOMEM;

	mhp->pages[mhp->npages++] = addr;

	return 0;
}

735
static int iwch_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
736
			  int sg_nents, unsigned int *sg_offset)
737 738 739 740 741
{
	struct iwch_mr *mhp = to_iwch_mr(ibmr);

	mhp->npages = 0;

742
	return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, iwch_set_page);
743 744
}

745 746 747 748 749 750 751 752 753 754
static int iwch_destroy_qp(struct ib_qp *ib_qp)
{
	struct iwch_dev *rhp;
	struct iwch_qp *qhp;
	struct iwch_qp_attributes attrs;
	struct iwch_ucontext *ucontext;

	qhp = to_iwch_qp(ib_qp);
	rhp = qhp->rhp;

755 756
	attrs.next_state = IWCH_QP_STATE_ERROR;
	iwch_modify_qp(rhp, qhp, IWCH_QP_ATTR_NEXT_STATE, &attrs, 0);
757 758
	wait_event(qhp->wait, !qhp->ep);

M
Matthew Wilcox 已提交
759
	xa_erase_irq(&rhp->qps, qhp->wq.qpid);
760 761 762 763 764 765 766 767 768

	atomic_dec(&qhp->refcnt);
	wait_event(qhp->wait, !atomic_read(&qhp->refcnt));

	ucontext = ib_qp->uobject ? to_iwch_ucontext(ib_qp->uobject->context)
				  : NULL;
	cxio_destroy_qp(&rhp->rdev, &qhp->wq,
			ucontext ? &ucontext->uctx : &rhp->rdev.uctx);

J
Joe Perches 已提交
769 770
	pr_debug("%s ib_qp %p qpid 0x%0x qhp %p\n", __func__,
		 ib_qp, qhp->wq.qpid, qhp);
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
	kfree(qhp);
	return 0;
}

static struct ib_qp *iwch_create_qp(struct ib_pd *pd,
			     struct ib_qp_init_attr *attrs,
			     struct ib_udata *udata)
{
	struct iwch_dev *rhp;
	struct iwch_qp *qhp;
	struct iwch_pd *php;
	struct iwch_cq *schp;
	struct iwch_cq *rchp;
	struct iwch_create_qp_resp uresp;
	int wqsize, sqsize, rqsize;
	struct iwch_ucontext *ucontext;

J
Joe Perches 已提交
788
	pr_debug("%s ib_pd %p\n", __func__, pd);
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
	if (attrs->qp_type != IB_QPT_RC)
		return ERR_PTR(-EINVAL);
	php = to_iwch_pd(pd);
	rhp = php->rhp;
	schp = get_chp(rhp, ((struct iwch_cq *) attrs->send_cq)->cq.cqid);
	rchp = get_chp(rhp, ((struct iwch_cq *) attrs->recv_cq)->cq.cqid);
	if (!schp || !rchp)
		return ERR_PTR(-EINVAL);

	/* The RQT size must be # of entries + 1 rounded up to a power of two */
	rqsize = roundup_pow_of_two(attrs->cap.max_recv_wr);
	if (rqsize == attrs->cap.max_recv_wr)
		rqsize = roundup_pow_of_two(attrs->cap.max_recv_wr+1);

	/* T3 doesn't support RQT depth < 16 */
	if (rqsize < 16)
		rqsize = 16;

	if (rqsize > T3_MAX_RQ_SIZE)
		return ERR_PTR(-EINVAL);

810 811 812
	if (attrs->cap.max_inline_data > T3_MAX_INLINE)
		return ERR_PTR(-EINVAL);

813 814 815 816 817 818 819
	/*
	 * NOTE: The SQ and total WQ sizes don't need to be
	 * a power of two.  However, all the code assumes
	 * they are. EG: Q_FREECNT() and friends.
	 */
	sqsize = roundup_pow_of_two(attrs->cap.max_send_wr);
	wqsize = roundup_pow_of_two(rqsize + sqsize);
820 821 822 823 824

	/*
	 * Kernel users need more wq space for fastreg WRs which can take
	 * 2 WR fragments.
	 */
825 826
	ucontext = rdma_udata_to_drv_context(udata, struct iwch_ucontext,
					     ibucontext);
827 828 829
	if (!ucontext && wqsize < (rqsize + (2 * sqsize)))
		wqsize = roundup_pow_of_two(rqsize +
				roundup_pow_of_two(attrs->cap.max_send_wr * 2));
J
Joe Perches 已提交
830 831
	pr_debug("%s wqsize %d sqsize %d rqsize %d\n", __func__,
		 wqsize, sqsize, rqsize);
832 833 834 835 836 837 838 839 840 841 842
	qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
	if (!qhp)
		return ERR_PTR(-ENOMEM);
	qhp->wq.size_log2 = ilog2(wqsize);
	qhp->wq.rq_size_log2 = ilog2(rqsize);
	qhp->wq.sq_size_log2 = ilog2(sqsize);
	if (cxio_create_qp(&rhp->rdev, !udata, &qhp->wq,
			   ucontext ? &ucontext->uctx : &rhp->rdev.uctx)) {
		kfree(qhp);
		return ERR_PTR(-ENOMEM);
	}
843

844 845
	attrs->cap.max_recv_wr = rqsize - 1;
	attrs->cap.max_send_wr = sqsize;
846 847
	attrs->cap.max_inline_data = T3_MAX_INLINE;

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
	qhp->rhp = rhp;
	qhp->attr.pd = php->pdid;
	qhp->attr.scq = ((struct iwch_cq *) attrs->send_cq)->cq.cqid;
	qhp->attr.rcq = ((struct iwch_cq *) attrs->recv_cq)->cq.cqid;
	qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
	qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
	qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
	qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
	qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
	qhp->attr.state = IWCH_QP_STATE_IDLE;
	qhp->attr.next_state = IWCH_QP_STATE_IDLE;

	/*
	 * XXX - These don't get passed in from the openib user
	 * at create time.  The CM sets them via a QP modify.
	 * Need to fix...  I think the CM should
	 */
	qhp->attr.enable_rdma_read = 1;
	qhp->attr.enable_rdma_write = 1;
	qhp->attr.enable_bind = 1;
	qhp->attr.max_ord = 1;
	qhp->attr.max_ird = 1;

	spin_lock_init(&qhp->lock);
	init_waitqueue_head(&qhp->wait);
	atomic_set(&qhp->refcnt, 1);
874

M
Matthew Wilcox 已提交
875
	if (xa_store_irq(&rhp->qps, qhp->wq.qpid, qhp, GFP_KERNEL)) {
876 877 878 879 880
		cxio_destroy_qp(&rhp->rdev, &qhp->wq,
			ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
		kfree(qhp);
		return ERR_PTR(-ENOMEM);
	}
881 882 883 884 885 886 887 888 889 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

	if (udata) {

		struct iwch_mm_entry *mm1, *mm2;

		mm1 = kmalloc(sizeof *mm1, GFP_KERNEL);
		if (!mm1) {
			iwch_destroy_qp(&qhp->ibqp);
			return ERR_PTR(-ENOMEM);
		}

		mm2 = kmalloc(sizeof *mm2, GFP_KERNEL);
		if (!mm2) {
			kfree(mm1);
			iwch_destroy_qp(&qhp->ibqp);
			return ERR_PTR(-ENOMEM);
		}

		uresp.qpid = qhp->wq.qpid;
		uresp.size_log2 = qhp->wq.size_log2;
		uresp.sq_size_log2 = qhp->wq.sq_size_log2;
		uresp.rq_size_log2 = qhp->wq.rq_size_log2;
		spin_lock(&ucontext->mmap_lock);
		uresp.key = ucontext->key;
		ucontext->key += PAGE_SIZE;
		uresp.db_key = ucontext->key;
		ucontext->key += PAGE_SIZE;
		spin_unlock(&ucontext->mmap_lock);
		if (ib_copy_to_udata(udata, &uresp, sizeof (uresp))) {
			kfree(mm1);
			kfree(mm2);
			iwch_destroy_qp(&qhp->ibqp);
			return ERR_PTR(-EFAULT);
		}
		mm1->key = uresp.key;
		mm1->addr = virt_to_phys(qhp->wq.queue);
		mm1->len = PAGE_ALIGN(wqsize * sizeof (union t3_wr));
		insert_mmap(ucontext, mm1);
		mm2->key = uresp.db_key;
		mm2->addr = qhp->wq.udb & PAGE_MASK;
		mm2->len = PAGE_SIZE;
		insert_mmap(ucontext, mm2);
	}
	qhp->ibqp.qp_num = qhp->wq.qpid;
J
Joe Perches 已提交
925 926 927 928
	pr_debug("%s sq_num_entries %d, rq_num_entries %d qpid 0x%0x qhp %p dma_addr 0x%llx size %d rq_addr 0x%x\n",
		 __func__, qhp->attr.sq_num_entries, qhp->attr.rq_num_entries,
		 qhp->wq.qpid, qhp, (unsigned long long)qhp->wq.dma_addr,
		 1 << qhp->wq.size_log2, qhp->wq.rq_addr);
929 930 931 932 933 934 935 936 937 938 939
	return &qhp->ibqp;
}

static int iwch_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
		      int attr_mask, struct ib_udata *udata)
{
	struct iwch_dev *rhp;
	struct iwch_qp *qhp;
	enum iwch_qp_attr_mask mask = 0;
	struct iwch_qp_attributes attrs;

J
Joe Perches 已提交
940
	pr_debug("%s ib_qp %p\n", __func__, ibqp);
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972

	/* iwarp does not support the RTR state */
	if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
		attr_mask &= ~IB_QP_STATE;

	/* Make sure we still have something left to do */
	if (!attr_mask)
		return 0;

	memset(&attrs, 0, sizeof attrs);
	qhp = to_iwch_qp(ibqp);
	rhp = qhp->rhp;

	attrs.next_state = iwch_convert_state(attr->qp_state);
	attrs.enable_rdma_read = (attr->qp_access_flags &
			       IB_ACCESS_REMOTE_READ) ?  1 : 0;
	attrs.enable_rdma_write = (attr->qp_access_flags &
				IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
	attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;


	mask |= (attr_mask & IB_QP_STATE) ? IWCH_QP_ATTR_NEXT_STATE : 0;
	mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
			(IWCH_QP_ATTR_ENABLE_RDMA_READ |
			 IWCH_QP_ATTR_ENABLE_RDMA_WRITE |
			 IWCH_QP_ATTR_ENABLE_RDMA_BIND) : 0;

	return iwch_modify_qp(rhp, qhp, mask, &attrs, 0);
}

void iwch_qp_add_ref(struct ib_qp *qp)
{
J
Joe Perches 已提交
973
	pr_debug("%s ib_qp %p\n", __func__, qp);
974 975 976 977 978
	atomic_inc(&(to_iwch_qp(qp)->refcnt));
}

void iwch_qp_rem_ref(struct ib_qp *qp)
{
J
Joe Perches 已提交
979
	pr_debug("%s ib_qp %p\n", __func__, qp);
980 981 982 983
	if (atomic_dec_and_test(&(to_iwch_qp(qp)->refcnt)))
	        wake_up(&(to_iwch_qp(qp)->wait));
}

A
Adrian Bunk 已提交
984
static struct ib_qp *iwch_get_qp(struct ib_device *dev, int qpn)
985
{
J
Joe Perches 已提交
986
	pr_debug("%s ib_dev %p qpn 0x%x\n", __func__, dev, qpn);
987 988 989 990 991 992 993
	return (struct ib_qp *)get_qhp(to_iwch_dev(dev), qpn);
}


static int iwch_query_pkey(struct ib_device *ibdev,
			   u8 port, u16 index, u16 * pkey)
{
J
Joe Perches 已提交
994
	pr_debug("%s ibdev %p\n", __func__, ibdev);
995 996 997 998 999 1000 1001 1002 1003
	*pkey = 0;
	return 0;
}

static int iwch_query_gid(struct ib_device *ibdev, u8 port,
			  int index, union ib_gid *gid)
{
	struct iwch_dev *dev;

J
Joe Perches 已提交
1004 1005
	pr_debug("%s ibdev %p, port %d, index %d, gid %p\n",
		 __func__, ibdev, port, index, gid);
1006 1007 1008 1009 1010 1011 1012
	dev = to_iwch_dev(ibdev);
	BUG_ON(port == 0 || port > 2);
	memset(&(gid->raw[0]), 0, sizeof(gid->raw));
	memcpy(&(gid->raw[0]), dev->rdev.port_info.lldevs[port-1]->dev_addr, 6);
	return 0;
}

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
static u64 fw_vers_string_to_u64(struct iwch_dev *iwch_dev)
{
	struct ethtool_drvinfo info;
	struct net_device *lldev = iwch_dev->rdev.t3cdev_p->lldev;
	char *cp, *next;
	unsigned fw_maj, fw_min, fw_mic;

	lldev->ethtool_ops->get_drvinfo(lldev, &info);

	next = info.fw_version + 1;
	cp = strsep(&next, ".");
	sscanf(cp, "%i", &fw_maj);
	cp = strsep(&next, ".");
	sscanf(cp, "%i", &fw_min);
	cp = strsep(&next, ".");
	sscanf(cp, "%i", &fw_mic);

	return (((u64)fw_maj & 0xffff) << 32) | ((fw_min & 0xffff) << 16) |
	       (fw_mic & 0xffff);
}

1034 1035
static int iwch_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
			     struct ib_udata *uhw)
1036 1037 1038
{

	struct iwch_dev *dev;
1039

J
Joe Perches 已提交
1040
	pr_debug("%s ibdev %p\n", __func__, ibdev);
1041

1042 1043 1044
	if (uhw->inlen || uhw->outlen)
		return -EINVAL;

1045 1046 1047
	dev = to_iwch_dev(ibdev);
	memset(props, 0, sizeof *props);
	memcpy(&props->sys_image_guid, dev->rdev.t3cdev_p->lldev->dev_addr, 6);
1048 1049
	props->hw_ver = dev->rdev.t3cdev_p->type;
	props->fw_ver = fw_vers_string_to_u64(dev);
1050
	props->device_cap_flags = dev->device_cap_flags;
1051
	props->page_size_cap = dev->attr.mem_pgsizes_bitmask;
1052 1053
	props->vendor_id = (u32)dev->rdev.rnic_info.pdev->vendor;
	props->vendor_part_id = (u32)dev->rdev.rnic_info.pdev->device;
1054
	props->max_mr_size = dev->attr.max_mr_size;
1055 1056
	props->max_qp = dev->attr.max_qps;
	props->max_qp_wr = dev->attr.max_wrs;
1057 1058
	props->max_send_sge = dev->attr.max_sge_per_wr;
	props->max_recv_sge = dev->attr.max_sge_per_wr;
1059 1060
	props->max_sge_rd = 1;
	props->max_qp_rd_atom = dev->attr.max_rdma_reads_per_qp;
1061
	props->max_qp_init_rd_atom = dev->attr.max_rdma_reads_per_qp;
1062 1063 1064 1065 1066
	props->max_cq = dev->attr.max_cqs;
	props->max_cqe = dev->attr.max_cqes_per_cq;
	props->max_mr = dev->attr.max_mem_regs;
	props->max_pd = dev->attr.max_pds;
	props->local_ca_ack_delay = 0;
1067
	props->max_fast_reg_page_list_len = T3_MAX_FASTREG_DEPTH;
1068 1069 1070 1071 1072 1073 1074

	return 0;
}

static int iwch_query_port(struct ib_device *ibdev,
			   u8 port, struct ib_port_attr *props)
{
1075 1076 1077 1078
	struct iwch_dev *dev;
	struct net_device *netdev;
	struct in_device *inetdev;

J
Joe Perches 已提交
1079
	pr_debug("%s ibdev %p\n", __func__, ibdev);
1080

1081 1082 1083
	dev = to_iwch_dev(ibdev);
	netdev = dev->rdev.port_info.lldevs[port-1];

1084
	/* props being zeroed by the caller, avoid zeroing it here */
1085
	props->max_mtu = IB_MTU_4096;
1086
	props->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
1087 1088 1089 1090 1091

	if (!netif_carrier_ok(netdev))
		props->state = IB_PORT_DOWN;
	else {
		inetdev = in_dev_get(netdev);
1092 1093 1094 1095 1096 1097 1098
		if (inetdev) {
			if (inetdev->ifa_list)
				props->state = IB_PORT_ACTIVE;
			else
				props->state = IB_PORT_INIT;
			in_dev_put(inetdev);
		} else
1099 1100 1101
			props->state = IB_PORT_INIT;
	}

1102 1103 1104 1105 1106 1107 1108 1109 1110
	props->port_cap_flags =
	    IB_PORT_CM_SUP |
	    IB_PORT_SNMP_TUNNEL_SUP |
	    IB_PORT_REINIT_SUP |
	    IB_PORT_DEVICE_MGMT_SUP |
	    IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
	props->gid_tbl_len = 1;
	props->pkey_tbl_len = 1;
	props->active_width = 2;
1111
	props->active_speed = IB_SPEED_DDR;
1112 1113 1114 1115 1116
	props->max_msg_sz = -1;

	return 0;
}

1117 1118
static ssize_t hw_rev_show(struct device *dev,
			   struct device_attribute *attr, char *buf)
1119
{
1120 1121 1122
	struct iwch_dev *iwch_dev =
			rdma_device_to_drv_device(dev, struct iwch_dev, ibdev);

J
Joe Perches 已提交
1123
	pr_debug("%s dev 0x%p\n", __func__, dev);
1124
	return sprintf(buf, "%d\n", iwch_dev->rdev.t3cdev_p->type);
1125
}
1126
static DEVICE_ATTR_RO(hw_rev);
1127

1128 1129
static ssize_t hca_type_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
1130
{
1131 1132
	struct iwch_dev *iwch_dev =
			rdma_device_to_drv_device(dev, struct iwch_dev, ibdev);
1133
	struct ethtool_drvinfo info;
1134
	struct net_device *lldev = iwch_dev->rdev.t3cdev_p->lldev;
1135

J
Joe Perches 已提交
1136
	pr_debug("%s dev 0x%p\n", __func__, dev);
1137 1138 1139
	lldev->ethtool_ops->get_drvinfo(lldev, &info);
	return sprintf(buf, "%s\n", info.driver);
}
1140
static DEVICE_ATTR_RO(hca_type);
1141

1142 1143
static ssize_t board_id_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
1144
{
1145 1146 1147
	struct iwch_dev *iwch_dev =
			rdma_device_to_drv_device(dev, struct iwch_dev, ibdev);

J
Joe Perches 已提交
1148
	pr_debug("%s dev 0x%p\n", __func__, dev);
1149 1150
	return sprintf(buf, "%x.%x\n", iwch_dev->rdev.rnic_info.pdev->vendor,
		       iwch_dev->rdev.rnic_info.pdev->device);
1151
}
1152
static DEVICE_ATTR_RO(board_id);
1153

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 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 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
enum counters {
	IPINRECEIVES,
	IPINHDRERRORS,
	IPINADDRERRORS,
	IPINUNKNOWNPROTOS,
	IPINDISCARDS,
	IPINDELIVERS,
	IPOUTREQUESTS,
	IPOUTDISCARDS,
	IPOUTNOROUTES,
	IPREASMTIMEOUT,
	IPREASMREQDS,
	IPREASMOKS,
	IPREASMFAILS,
	TCPACTIVEOPENS,
	TCPPASSIVEOPENS,
	TCPATTEMPTFAILS,
	TCPESTABRESETS,
	TCPCURRESTAB,
	TCPINSEGS,
	TCPOUTSEGS,
	TCPRETRANSSEGS,
	TCPINERRS,
	TCPOUTRSTS,
	TCPRTOMIN,
	TCPRTOMAX,
	NR_COUNTERS
};

static const char * const names[] = {
	[IPINRECEIVES] = "ipInReceives",
	[IPINHDRERRORS] = "ipInHdrErrors",
	[IPINADDRERRORS] = "ipInAddrErrors",
	[IPINUNKNOWNPROTOS] = "ipInUnknownProtos",
	[IPINDISCARDS] = "ipInDiscards",
	[IPINDELIVERS] = "ipInDelivers",
	[IPOUTREQUESTS] = "ipOutRequests",
	[IPOUTDISCARDS] = "ipOutDiscards",
	[IPOUTNOROUTES] = "ipOutNoRoutes",
	[IPREASMTIMEOUT] = "ipReasmTimeout",
	[IPREASMREQDS] = "ipReasmReqds",
	[IPREASMOKS] = "ipReasmOKs",
	[IPREASMFAILS] = "ipReasmFails",
	[TCPACTIVEOPENS] = "tcpActiveOpens",
	[TCPPASSIVEOPENS] = "tcpPassiveOpens",
	[TCPATTEMPTFAILS] = "tcpAttemptFails",
	[TCPESTABRESETS] = "tcpEstabResets",
	[TCPCURRESTAB] = "tcpCurrEstab",
	[TCPINSEGS] = "tcpInSegs",
	[TCPOUTSEGS] = "tcpOutSegs",
	[TCPRETRANSSEGS] = "tcpRetransSegs",
	[TCPINERRS] = "tcpInErrs",
	[TCPOUTRSTS] = "tcpOutRsts",
	[TCPRTOMIN] = "tcpRtoMin",
	[TCPRTOMAX] = "tcpRtoMax",
};

static struct rdma_hw_stats *iwch_alloc_stats(struct ib_device *ibdev,
					      u8 port_num)
{
	BUILD_BUG_ON(ARRAY_SIZE(names) != NR_COUNTERS);

	/* Our driver only supports device level stats */
	if (port_num != 0)
		return NULL;

	return rdma_alloc_hw_stats_struct(names, NR_COUNTERS,
					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
}

static int iwch_get_mib(struct ib_device *ibdev, struct rdma_hw_stats *stats,
			u8 port, int index)
1226 1227 1228 1229 1230
{
	struct iwch_dev *dev;
	struct tp_mib_stats m;
	int ret;

1231 1232 1233
	if (port != 0 || !stats)
		return -ENOSYS;

J
Joe Perches 已提交
1234
	pr_debug("%s ibdev %p\n", __func__, ibdev);
1235 1236 1237 1238 1239
	dev = to_iwch_dev(ibdev);
	ret = dev->rdev.t3cdev_p->ctl(dev->rdev.t3cdev_p, RDMA_GET_MIB, &m);
	if (ret)
		return -ENOSYS;

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	stats->value[IPINRECEIVES] = ((u64)m.ipInReceive_hi << 32) +	m.ipInReceive_lo;
	stats->value[IPINHDRERRORS] = ((u64)m.ipInHdrErrors_hi << 32) + m.ipInHdrErrors_lo;
	stats->value[IPINADDRERRORS] = ((u64)m.ipInAddrErrors_hi << 32) + m.ipInAddrErrors_lo;
	stats->value[IPINUNKNOWNPROTOS] = ((u64)m.ipInUnknownProtos_hi << 32) + m.ipInUnknownProtos_lo;
	stats->value[IPINDISCARDS] = ((u64)m.ipInDiscards_hi << 32) + m.ipInDiscards_lo;
	stats->value[IPINDELIVERS] = ((u64)m.ipInDelivers_hi << 32) + m.ipInDelivers_lo;
	stats->value[IPOUTREQUESTS] = ((u64)m.ipOutRequests_hi << 32) + m.ipOutRequests_lo;
	stats->value[IPOUTDISCARDS] = ((u64)m.ipOutDiscards_hi << 32) + m.ipOutDiscards_lo;
	stats->value[IPOUTNOROUTES] = ((u64)m.ipOutNoRoutes_hi << 32) + m.ipOutNoRoutes_lo;
	stats->value[IPREASMTIMEOUT] = 	m.ipReasmTimeout;
	stats->value[IPREASMREQDS] = m.ipReasmReqds;
	stats->value[IPREASMOKS] = m.ipReasmOKs;
	stats->value[IPREASMFAILS] = m.ipReasmFails;
	stats->value[TCPACTIVEOPENS] =	m.tcpActiveOpens;
	stats->value[TCPPASSIVEOPENS] =	m.tcpPassiveOpens;
	stats->value[TCPATTEMPTFAILS] = m.tcpAttemptFails;
	stats->value[TCPESTABRESETS] = m.tcpEstabResets;
	stats->value[TCPCURRESTAB] = m.tcpOutRsts;
	stats->value[TCPINSEGS] = m.tcpCurrEstab;
	stats->value[TCPOUTSEGS] = ((u64)m.tcpInSegs_hi << 32) + m.tcpInSegs_lo;
	stats->value[TCPRETRANSSEGS] = ((u64)m.tcpOutSegs_hi << 32) + m.tcpOutSegs_lo;
	stats->value[TCPINERRS] = ((u64)m.tcpRetransSeg_hi << 32) + m.tcpRetransSeg_lo,
	stats->value[TCPOUTRSTS] = ((u64)m.tcpInErrs_hi << 32) + m.tcpInErrs_lo;
	stats->value[TCPRTOMIN] = m.tcpRtoMin;
	stats->value[TCPRTOMAX] = m.tcpRtoMax;

	return stats->num_counters;
1267 1268
}

1269 1270 1271 1272 1273 1274
static struct attribute *iwch_class_attributes[] = {
	&dev_attr_hw_rev.attr,
	&dev_attr_hca_type.attr,
	&dev_attr_board_id.attr,
	NULL
};
1275

1276 1277
static const struct attribute_group iwch_attr_group = {
	.attrs = iwch_class_attributes,
1278 1279
};

1280 1281 1282 1283 1284 1285
static int iwch_port_immutable(struct ib_device *ibdev, u8 port_num,
			       struct ib_port_immutable *immutable)
{
	struct ib_port_attr attr;
	int err;

1286 1287 1288
	immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;

	err = ib_query_port(ibdev, port_num, &attr);
1289 1290 1291 1292 1293 1294 1295 1296 1297
	if (err)
		return err;

	immutable->pkey_tbl_len = attr.pkey_tbl_len;
	immutable->gid_tbl_len = attr.gid_tbl_len;

	return 0;
}

1298
static void get_dev_fw_ver_str(struct ib_device *ibdev, char *str)
1299 1300 1301 1302 1303
{
	struct iwch_dev *iwch_dev = to_iwch_dev(ibdev);
	struct ethtool_drvinfo info;
	struct net_device *lldev = iwch_dev->rdev.t3cdev_p->lldev;

J
Joe Perches 已提交
1304
	pr_debug("%s dev 0x%p\n", __func__, iwch_dev);
1305
	lldev->ethtool_ops->get_drvinfo(lldev, &info);
1306
	snprintf(str, IB_FW_VERSION_NAME_MAX, "%s", info.fw_version);
1307 1308
}

1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
static const struct ib_device_ops iwch_dev_ops = {
	.alloc_hw_stats	= iwch_alloc_stats,
	.alloc_mr = iwch_alloc_mr,
	.alloc_mw = iwch_alloc_mw,
	.alloc_pd = iwch_allocate_pd,
	.alloc_ucontext = iwch_alloc_ucontext,
	.create_cq = iwch_create_cq,
	.create_qp = iwch_create_qp,
	.dealloc_mw = iwch_dealloc_mw,
	.dealloc_pd = iwch_deallocate_pd,
	.dealloc_ucontext = iwch_dealloc_ucontext,
	.dereg_mr = iwch_dereg_mr,
	.destroy_cq = iwch_destroy_cq,
	.destroy_qp = iwch_destroy_qp,
	.get_dev_fw_str = get_dev_fw_ver_str,
	.get_dma_mr = iwch_get_dma_mr,
	.get_hw_stats = iwch_get_mib,
	.get_port_immutable = iwch_port_immutable,
	.map_mr_sg = iwch_map_mr_sg,
	.mmap = iwch_mmap,
	.modify_qp = iwch_ib_modify_qp,
	.poll_cq = iwch_poll_cq,
	.post_recv = iwch_post_receive,
	.post_send = iwch_post_send,
	.query_device = iwch_query_device,
	.query_gid = iwch_query_gid,
	.query_pkey = iwch_query_pkey,
	.query_port = iwch_query_port,
	.reg_user_mr = iwch_reg_user_mr,
	.req_notify_cq = iwch_arm_cq,
	.resize_cq = iwch_resize_cq,
1340
	INIT_RDMA_OBJ_SIZE(ib_pd, iwch_pd, ibpd),
1341
	INIT_RDMA_OBJ_SIZE(ib_ucontext, iwch_ucontext, ibucontext),
1342 1343
};

1344 1345 1346 1347
int iwch_register_device(struct iwch_dev *dev)
{
	int ret;

J
Joe Perches 已提交
1348
	pr_debug("%s iwch_dev %p\n", __func__, dev);
1349 1350 1351
	memset(&dev->ibdev.node_guid, 0, sizeof(dev->ibdev.node_guid));
	memcpy(&dev->ibdev.node_guid, dev->rdev.t3cdev_p->lldev->dev_addr, 6);
	dev->ibdev.owner = THIS_MODULE;
1352 1353 1354
	dev->device_cap_flags = IB_DEVICE_LOCAL_DMA_LKEY |
				IB_DEVICE_MEM_WINDOW |
				IB_DEVICE_MEM_MGT_EXTENSIONS;
1355 1356 1357

	/* cxgb3 supports STag 0. */
	dev->ibdev.local_dma_lkey = 0;
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377

	dev->ibdev.uverbs_cmd_mask =
	    (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
	    (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
	    (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
	    (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
	    (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
	    (1ull << IB_USER_VERBS_CMD_REG_MR) |
	    (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
	    (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
	    (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
	    (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
	    (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
	    (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
	    (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
	    (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
	    (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
	    (1ull << IB_USER_VERBS_CMD_POST_SEND) |
	    (1ull << IB_USER_VERBS_CMD_POST_RECV);
	dev->ibdev.node_type = RDMA_NODE_RNIC;
1378
	BUILD_BUG_ON(sizeof(IWCH_NODE_DESC) > IB_DEVICE_NODE_DESC_MAX);
1379 1380
	memcpy(dev->ibdev.node_desc, IWCH_NODE_DESC, sizeof(IWCH_NODE_DESC));
	dev->ibdev.phys_port_cnt = dev->rdev.port_info.nports;
1381
	dev->ibdev.num_comp_vectors = 1;
1382
	dev->ibdev.dev.parent = &dev->rdev.rnic_info.pdev->dev;
1383
	dev->ibdev.uverbs_abi_ver = IWCH_UVERBS_ABI_VERSION;
1384

1385
	dev->ibdev.iwcm = kzalloc(sizeof(struct iw_cm_verbs), GFP_KERNEL);
1386 1387 1388
	if (!dev->ibdev.iwcm)
		return -ENOMEM;

1389 1390 1391 1392 1393 1394 1395 1396
	dev->ibdev.iwcm->connect = iwch_connect;
	dev->ibdev.iwcm->accept = iwch_accept_cr;
	dev->ibdev.iwcm->reject = iwch_reject_cr;
	dev->ibdev.iwcm->create_listen = iwch_create_listen;
	dev->ibdev.iwcm->destroy_listen = iwch_destroy_listen;
	dev->ibdev.iwcm->add_ref = iwch_qp_add_ref;
	dev->ibdev.iwcm->rem_ref = iwch_qp_rem_ref;
	dev->ibdev.iwcm->get_qp = iwch_get_qp;
1397 1398
	memcpy(dev->ibdev.iwcm->ifname, dev->rdev.t3cdev_p->lldev->name,
	       sizeof(dev->ibdev.iwcm->ifname));
1399

1400
	dev->ibdev.driver_id = RDMA_DRIVER_CXGB3;
1401
	rdma_set_device_sysfs_group(&dev->ibdev, &iwch_attr_group);
1402
	ib_set_device_ops(&dev->ibdev, &iwch_dev_ops);
1403
	ret = ib_register_device(&dev->ibdev, "cxgb3_%d");
1404
	if (ret)
1405
		kfree(dev->ibdev.iwcm);
1406 1407 1408 1409 1410
	return ret;
}

void iwch_unregister_device(struct iwch_dev *dev)
{
J
Joe Perches 已提交
1411
	pr_debug("%s iwch_dev %p\n", __func__, dev);
1412
	ib_unregister_device(&dev->ibdev);
1413
	kfree(dev->ibdev.iwcm);
1414 1415
	return;
}