mthca_provider.c 35.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4
 * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
5 6
 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
L
Linus Torvalds 已提交
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
 *
 * 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.
 *
36
 * $Id: mthca_provider.c 4859 2006-01-09 21:55:10Z roland $
L
Linus Torvalds 已提交
37 38
 */

39
#include <rdma/ib_smi.h>
40
#include <rdma/ib_umem.h>
41
#include <rdma/ib_user_verbs.h>
42
#include <linux/mm.h>
L
Linus Torvalds 已提交
43 44 45

#include "mthca_dev.h"
#include "mthca_cmd.h"
46 47
#include "mthca_user.h"
#include "mthca_memfree.h"
L
Linus Torvalds 已提交
48

49 50 51 52 53 54 55 56
static void init_query_mad(struct ib_smp *mad)
{
	mad->base_version  = 1;
	mad->mgmt_class    = IB_MGMT_CLASS_SUBN_LID_ROUTED;
	mad->class_version = 1;
	mad->method    	   = IB_MGMT_METHOD_GET;
}

L
Linus Torvalds 已提交
57 58 59 60 61 62
static int mthca_query_device(struct ib_device *ibdev,
			      struct ib_device_attr *props)
{
	struct ib_smp *in_mad  = NULL;
	struct ib_smp *out_mad = NULL;
	int err = -ENOMEM;
R
Roland Dreier 已提交
63
	struct mthca_dev *mdev = to_mdev(ibdev);
L
Linus Torvalds 已提交
64 65 66

	u8 status;

R
Roland Dreier 已提交
67
	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
L
Linus Torvalds 已提交
68 69 70 71
	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
	if (!in_mad || !out_mad)
		goto out;

72
	memset(props, 0, sizeof *props);
73

L
Linus Torvalds 已提交
74 75
	props->fw_ver              = mdev->fw_ver;

76 77
	init_query_mad(in_mad);
	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86 87 88

	err = mthca_MAD_IFC(mdev, 1, 1,
			    1, NULL, NULL, in_mad, out_mad,
			    &status);
	if (err)
		goto out;
	if (status) {
		err = -EINVAL;
		goto out;
	}

89
	props->device_cap_flags    = mdev->device_cap_flags;
90
	props->vendor_id           = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
L
Linus Torvalds 已提交
91
		0xffffff;
92
	props->vendor_part_id      = be16_to_cpup((__be16 *) (out_mad->data + 30));
93
	props->hw_ver              = be32_to_cpup((__be32 *) (out_mad->data + 32));
L
Linus Torvalds 已提交
94 95
	memcpy(&props->sys_image_guid, out_mad->data +  4, 8);

96
	props->max_mr_size         = ~0ull;
97
	props->page_size_cap       = mdev->limits.page_size_cap;
98
	props->max_qp              = mdev->limits.num_qps - mdev->limits.reserved_qps;
99
	props->max_qp_wr           = mdev->limits.max_wqes;
100 101
	props->max_sge             = mdev->limits.max_sg;
	props->max_cq              = mdev->limits.num_cqs - mdev->limits.reserved_cqs;
102
	props->max_cqe             = mdev->limits.max_cqes;
103 104 105
	props->max_mr              = mdev->limits.num_mpts - mdev->limits.reserved_mrws;
	props->max_pd              = mdev->limits.num_pds - mdev->limits.reserved_pds;
	props->max_qp_rd_atom      = 1 << mdev->qp_table.rdb_shift;
106 107 108 109
	props->max_qp_init_rd_atom = mdev->limits.max_qp_init_rdma;
	props->max_res_rd_atom     = props->max_qp_rd_atom * props->max_qp;
	props->max_srq             = mdev->limits.num_srqs - mdev->limits.reserved_srqs;
	props->max_srq_wr          = mdev->limits.max_srq_wqes;
110
	props->max_srq_sge         = mdev->limits.max_srq_sge;
111
	props->local_ca_ack_delay  = mdev->limits.local_ca_ack_delay;
R
Roland Dreier 已提交
112
	props->atomic_cap          = mdev->limits.flags & DEV_LIM_FLAG_ATOMIC ?
113
					IB_ATOMIC_HCA : IB_ATOMIC_NONE;
114 115 116
	props->max_pkeys           = mdev->limits.pkey_table_len;
	props->max_mcast_grp       = mdev->limits.num_mgms + mdev->limits.num_amgms;
	props->max_mcast_qp_attach = MTHCA_QP_PER_MGM;
R
Roland Dreier 已提交
117
	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
118
					   props->max_mcast_grp;
119 120 121 122 123 124 125 126 127
	/*
	 * If Sinai memory key optimization is being used, then only
	 * the 8-bit key portion will change.  For other HCAs, the
	 * unused index bits will also be used for FMR remapping.
	 */
	if (mdev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
		props->max_map_per_fmr = 255;
	else
		props->max_map_per_fmr =
128
			(1 << (32 - ilog2(mdev->limits.num_mpts))) - 1;
129

L
Linus Torvalds 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	err = 0;
 out:
	kfree(in_mad);
	kfree(out_mad);
	return err;
}

static int mthca_query_port(struct ib_device *ibdev,
			    u8 port, struct ib_port_attr *props)
{
	struct ib_smp *in_mad  = NULL;
	struct ib_smp *out_mad = NULL;
	int err = -ENOMEM;
	u8 status;

R
Roland Dreier 已提交
145
	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
L
Linus Torvalds 已提交
146 147 148 149
	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
	if (!in_mad || !out_mad)
		goto out;

150 151
	memset(props, 0, sizeof *props);

152 153 154
	init_query_mad(in_mad);
	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
	in_mad->attr_mod = cpu_to_be32(port);
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163 164 165

	err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
			    port, NULL, NULL, in_mad, out_mad,
			    &status);
	if (err)
		goto out;
	if (status) {
		err = -EINVAL;
		goto out;
	}

166
	props->lid               = be16_to_cpup((__be16 *) (out_mad->data + 16));
L
Linus Torvalds 已提交
167
	props->lmc               = out_mad->data[34] & 0x7;
168
	props->sm_lid            = be16_to_cpup((__be16 *) (out_mad->data + 18));
L
Linus Torvalds 已提交
169 170 171
	props->sm_sl             = out_mad->data[36] & 0xf;
	props->state             = out_mad->data[32] & 0xf;
	props->phys_state        = out_mad->data[33] >> 4;
172
	props->port_cap_flags    = be32_to_cpup((__be32 *) (out_mad->data + 20));
L
Linus Torvalds 已提交
173
	props->gid_tbl_len       = to_mdev(ibdev)->limits.gid_table_len;
174
	props->max_msg_sz        = 0x80000000;
L
Linus Torvalds 已提交
175
	props->pkey_tbl_len      = to_mdev(ibdev)->limits.pkey_table_len;
176
	props->bad_pkey_cntr     = be16_to_cpup((__be16 *) (out_mad->data + 46));
177
	props->qkey_viol_cntr    = be16_to_cpup((__be16 *) (out_mad->data + 48));
L
Linus Torvalds 已提交
178 179
	props->active_width      = out_mad->data[31] & 0xf;
	props->active_speed      = out_mad->data[35] >> 4;
180 181 182
	props->max_mtu           = out_mad->data[41] & 0xf;
	props->active_mtu        = out_mad->data[36] >> 4;
	props->subnet_timeout    = out_mad->data[51] & 0x1f;
J
Jack Morgenstein 已提交
183 184
	props->max_vl_num        = out_mad->data[37] >> 4;
	props->init_type_reply   = out_mad->data[41] >> 4;
L
Linus Torvalds 已提交
185 186 187 188 189 190 191

 out:
	kfree(in_mad);
	kfree(out_mad);
	return err;
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static int mthca_modify_device(struct ib_device *ibdev,
			       int mask,
			       struct ib_device_modify *props)
{
	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
		return -EOPNOTSUPP;

	if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
		if (mutex_lock_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
			return -ERESTARTSYS;
		memcpy(ibdev->node_desc, props->node_desc, 64);
		mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
	}

	return 0;
}

L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217
static int mthca_modify_port(struct ib_device *ibdev,
			     u8 port, int port_modify_mask,
			     struct ib_port_modify *props)
{
	struct mthca_set_ib_param set_ib;
	struct ib_port_attr attr;
	int err;
	u8 status;

218
	if (mutex_lock_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
L
Linus Torvalds 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
		return -ERESTARTSYS;

	err = mthca_query_port(ibdev, port, &attr);
	if (err)
		goto out;

	set_ib.set_si_guid     = 0;
	set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);

	set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
		~props->clr_port_cap_mask;

	err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port, &status);
	if (err)
		goto out;
	if (status) {
		err = -EINVAL;
		goto out;
	}

out:
240
	mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
L
Linus Torvalds 已提交
241 242 243 244 245 246 247 248 249 250 251
	return err;
}

static int mthca_query_pkey(struct ib_device *ibdev,
			    u8 port, u16 index, u16 *pkey)
{
	struct ib_smp *in_mad  = NULL;
	struct ib_smp *out_mad = NULL;
	int err = -ENOMEM;
	u8 status;

R
Roland Dreier 已提交
252
	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
L
Linus Torvalds 已提交
253 254 255 256
	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
	if (!in_mad || !out_mad)
		goto out;

257 258 259
	init_query_mad(in_mad);
	in_mad->attr_id  = IB_SMP_ATTR_PKEY_TABLE;
	in_mad->attr_mod = cpu_to_be32(index / 32);
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269 270

	err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
			    port, NULL, NULL, in_mad, out_mad,
			    &status);
	if (err)
		goto out;
	if (status) {
		err = -EINVAL;
		goto out;
	}

271
	*pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

 out:
	kfree(in_mad);
	kfree(out_mad);
	return err;
}

static int mthca_query_gid(struct ib_device *ibdev, u8 port,
			   int index, union ib_gid *gid)
{
	struct ib_smp *in_mad  = NULL;
	struct ib_smp *out_mad = NULL;
	int err = -ENOMEM;
	u8 status;

R
Roland Dreier 已提交
287
	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
L
Linus Torvalds 已提交
288 289 290 291
	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
	if (!in_mad || !out_mad)
		goto out;

292 293 294
	init_query_mad(in_mad);
	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
	in_mad->attr_mod = cpu_to_be32(port);
L
Linus Torvalds 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307

	err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
			    port, NULL, NULL, in_mad, out_mad,
			    &status);
	if (err)
		goto out;
	if (status) {
		err = -EINVAL;
		goto out;
	}

	memcpy(gid->raw, out_mad->data + 8, 8);

308 309 310
	init_query_mad(in_mad);
	in_mad->attr_id  = IB_SMP_ATTR_GUID_INFO;
	in_mad->attr_mod = cpu_to_be32(index / 8);
L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318 319 320 321

	err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
			    port, NULL, NULL, in_mad, out_mad,
			    &status);
	if (err)
		goto out;
	if (status) {
		err = -EINVAL;
		goto out;
	}

322
	memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
L
Linus Torvalds 已提交
323 324 325 326 327 328 329

 out:
	kfree(in_mad);
	kfree(out_mad);
	return err;
}

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
static struct ib_ucontext *mthca_alloc_ucontext(struct ib_device *ibdev,
						struct ib_udata *udata)
{
	struct mthca_alloc_ucontext_resp uresp;
	struct mthca_ucontext           *context;
	int                              err;

	memset(&uresp, 0, sizeof uresp);

	uresp.qp_tab_size = to_mdev(ibdev)->limits.num_qps;
	if (mthca_is_memfree(to_mdev(ibdev)))
		uresp.uarc_size = to_mdev(ibdev)->uar_table.uarc_size;
	else
		uresp.uarc_size = 0;

	context = kmalloc(sizeof *context, GFP_KERNEL);
	if (!context)
		return ERR_PTR(-ENOMEM);

	err = mthca_uar_alloc(to_mdev(ibdev), &context->uar);
	if (err) {
		kfree(context);
		return ERR_PTR(err);
	}

	context->db_tab = mthca_init_user_db_tab(to_mdev(ibdev));
	if (IS_ERR(context->db_tab)) {
		err = PTR_ERR(context->db_tab);
		mthca_uar_free(to_mdev(ibdev), &context->uar);
		kfree(context);
		return ERR_PTR(err);
	}

	if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
		mthca_cleanup_user_db_tab(to_mdev(ibdev), &context->uar, context->db_tab);
		mthca_uar_free(to_mdev(ibdev), &context->uar);
		kfree(context);
		return ERR_PTR(-EFAULT);
	}

	return &context->ibucontext;
}

static int mthca_dealloc_ucontext(struct ib_ucontext *context)
{
	mthca_cleanup_user_db_tab(to_mdev(context->device), &to_mucontext(context)->uar,
				  to_mucontext(context)->db_tab);
	mthca_uar_free(to_mdev(context->device), &to_mucontext(context)->uar);
	kfree(to_mucontext(context));

	return 0;
}

383 384 385 386 387 388 389 390
static int mthca_mmap_uar(struct ib_ucontext *context,
			  struct vm_area_struct *vma)
{
	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
		return -EINVAL;

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);

391 392 393
	if (io_remap_pfn_range(vma, vma->vm_start,
			       to_mucontext(context)->uar.pfn,
			       PAGE_SIZE, vma->vm_page_prot))
394 395 396 397 398
		return -EAGAIN;

	return 0;
}

399 400 401
static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev,
				    struct ib_ucontext *context,
				    struct ib_udata *udata)
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409
{
	struct mthca_pd *pd;
	int err;

	pd = kmalloc(sizeof *pd, GFP_KERNEL);
	if (!pd)
		return ERR_PTR(-ENOMEM);

410
	err = mthca_pd_alloc(to_mdev(ibdev), !context, pd);
L
Linus Torvalds 已提交
411 412 413 414 415
	if (err) {
		kfree(pd);
		return ERR_PTR(err);
	}

416 417 418 419 420 421 422 423
	if (context) {
		if (ib_copy_to_udata(udata, &pd->pd_num, sizeof (__u32))) {
			mthca_pd_free(to_mdev(ibdev), pd);
			kfree(pd);
			return ERR_PTR(-EFAULT);
		}
	}

L
Linus Torvalds 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
	return &pd->ibpd;
}

static int mthca_dealloc_pd(struct ib_pd *pd)
{
	mthca_pd_free(to_mdev(pd->device), to_mpd(pd));
	kfree(pd);

	return 0;
}

static struct ib_ah *mthca_ah_create(struct ib_pd *pd,
				     struct ib_ah_attr *ah_attr)
{
	int err;
	struct mthca_ah *ah;

441
	ah = kmalloc(sizeof *ah, GFP_ATOMIC);
L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	if (!ah)
		return ERR_PTR(-ENOMEM);

	err = mthca_create_ah(to_mdev(pd->device), to_mpd(pd), ah_attr, ah);
	if (err) {
		kfree(ah);
		return ERR_PTR(err);
	}

	return &ah->ibah;
}

static int mthca_ah_destroy(struct ib_ah *ah)
{
	mthca_destroy_ah(to_mdev(ah->device), to_mah(ah));
	kfree(ah);

	return 0;
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
static struct ib_srq *mthca_create_srq(struct ib_pd *pd,
				       struct ib_srq_init_attr *init_attr,
				       struct ib_udata *udata)
{
	struct mthca_create_srq ucmd;
	struct mthca_ucontext *context = NULL;
	struct mthca_srq *srq;
	int err;

	srq = kmalloc(sizeof *srq, GFP_KERNEL);
	if (!srq)
		return ERR_PTR(-ENOMEM);

	if (pd->uobject) {
		context = to_mucontext(pd->uobject->context);

478 479 480 481
		if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
			err = -EFAULT;
			goto err_free;
		}
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

		err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
					context->db_tab, ucmd.db_index,
					ucmd.db_page);

		if (err)
			goto err_free;

		srq->mr.ibmr.lkey = ucmd.lkey;
		srq->db_index     = ucmd.db_index;
	}

	err = mthca_alloc_srq(to_mdev(pd->device), to_mpd(pd),
			      &init_attr->attr, srq);

	if (err && pd->uobject)
		mthca_unmap_user_db(to_mdev(pd->device), &context->uar,
				    context->db_tab, ucmd.db_index);

	if (err)
		goto err_free;

	if (context && ib_copy_to_udata(udata, &srq->srqn, sizeof (__u32))) {
		mthca_free_srq(to_mdev(pd->device), srq);
		err = -EFAULT;
		goto err_free;
	}

	return &srq->ibsrq;

err_free:
	kfree(srq);

	return ERR_PTR(err);
}

static int mthca_destroy_srq(struct ib_srq *srq)
{
	struct mthca_ucontext *context;

	if (srq->uobject) {
		context = to_mucontext(srq->uobject->context);

		mthca_unmap_user_db(to_mdev(srq->device), &context->uar,
				    context->db_tab, to_msrq(srq)->db_index);
	}

	mthca_free_srq(to_mdev(srq->device), to_msrq(srq));
	kfree(srq);

	return 0;
}

L
Linus Torvalds 已提交
535
static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
536 537
				     struct ib_qp_init_attr *init_attr,
				     struct ib_udata *udata)
L
Linus Torvalds 已提交
538
{
539
	struct mthca_create_qp ucmd;
L
Linus Torvalds 已提交
540 541 542
	struct mthca_qp *qp;
	int err;

543 544 545
	if (init_attr->create_flags)
		return ERR_PTR(-EINVAL);

L
Linus Torvalds 已提交
546 547 548 549 550
	switch (init_attr->qp_type) {
	case IB_QPT_RC:
	case IB_QPT_UC:
	case IB_QPT_UD:
	{
551 552
		struct mthca_ucontext *context;

L
Linus Torvalds 已提交
553 554 555 556
		qp = kmalloc(sizeof *qp, GFP_KERNEL);
		if (!qp)
			return ERR_PTR(-ENOMEM);

557 558 559
		if (pd->uobject) {
			context = to_mucontext(pd->uobject->context);

560 561
			if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
				kfree(qp);
562
				return ERR_PTR(-EFAULT);
563
			}
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

			err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
						context->db_tab,
						ucmd.sq_db_index, ucmd.sq_db_page);
			if (err) {
				kfree(qp);
				return ERR_PTR(err);
			}

			err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
						context->db_tab,
						ucmd.rq_db_index, ucmd.rq_db_page);
			if (err) {
				mthca_unmap_user_db(to_mdev(pd->device),
						    &context->uar,
						    context->db_tab,
						    ucmd.sq_db_index);
				kfree(qp);
				return ERR_PTR(err);
			}

			qp->mr.ibmr.lkey = ucmd.lkey;
			qp->sq.db_index  = ucmd.sq_db_index;
			qp->rq.db_index  = ucmd.rq_db_index;
		}
L
Linus Torvalds 已提交
589 590 591 592 593

		err = mthca_alloc_qp(to_mdev(pd->device), to_mpd(pd),
				     to_mcq(init_attr->send_cq),
				     to_mcq(init_attr->recv_cq),
				     init_attr->qp_type, init_attr->sq_sig_type,
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
				     &init_attr->cap, qp);

		if (err && pd->uobject) {
			context = to_mucontext(pd->uobject->context);

			mthca_unmap_user_db(to_mdev(pd->device),
					    &context->uar,
					    context->db_tab,
					    ucmd.sq_db_index);
			mthca_unmap_user_db(to_mdev(pd->device),
					    &context->uar,
					    context->db_tab,
					    ucmd.rq_db_index);
		}

L
Linus Torvalds 已提交
609 610 611 612 613 614
		qp->ibqp.qp_num = qp->qpn;
		break;
	}
	case IB_QPT_SMI:
	case IB_QPT_GSI:
	{
615 616 617 618
		/* Don't allow userspace to create special QPs */
		if (pd->uobject)
			return ERR_PTR(-EINVAL);

L
Linus Torvalds 已提交
619 620 621 622 623 624 625 626 627
		qp = kmalloc(sizeof (struct mthca_sqp), GFP_KERNEL);
		if (!qp)
			return ERR_PTR(-ENOMEM);

		qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;

		err = mthca_alloc_sqp(to_mdev(pd->device), to_mpd(pd),
				      to_mcq(init_attr->send_cq),
				      to_mcq(init_attr->recv_cq),
628
				      init_attr->sq_sig_type, &init_attr->cap,
L
Linus Torvalds 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642
				      qp->ibqp.qp_num, init_attr->port_num,
				      to_msqp(qp));
		break;
	}
	default:
		/* Don't support raw QPs */
		return ERR_PTR(-ENOSYS);
	}

	if (err) {
		kfree(qp);
		return ERR_PTR(err);
	}

643 644 645 646
	init_attr->cap.max_send_wr     = qp->sq.max;
	init_attr->cap.max_recv_wr     = qp->rq.max;
	init_attr->cap.max_send_sge    = qp->sq.max_gs;
	init_attr->cap.max_recv_sge    = qp->rq.max_gs;
647
	init_attr->cap.max_inline_data = qp->max_inline_data;
L
Linus Torvalds 已提交
648 649 650 651 652 653

	return &qp->ibqp;
}

static int mthca_destroy_qp(struct ib_qp *qp)
{
654 655 656 657 658 659 660 661 662 663
	if (qp->uobject) {
		mthca_unmap_user_db(to_mdev(qp->device),
				    &to_mucontext(qp->uobject->context)->uar,
				    to_mucontext(qp->uobject->context)->db_tab,
				    to_mqp(qp)->sq.db_index);
		mthca_unmap_user_db(to_mdev(qp->device),
				    &to_mucontext(qp->uobject->context)->uar,
				    to_mucontext(qp->uobject->context)->db_tab,
				    to_mqp(qp)->rq.db_index);
	}
L
Linus Torvalds 已提交
664 665 666 667 668
	mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
	kfree(qp);
	return 0;
}

669
static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
670
				     int comp_vector,
671 672
				     struct ib_ucontext *context,
				     struct ib_udata *udata)
L
Linus Torvalds 已提交
673
{
674
	struct mthca_create_cq ucmd;
L
Linus Torvalds 已提交
675 676 677 678
	struct mthca_cq *cq;
	int nent;
	int err;

679 680 681
	if (entries < 1 || entries > to_mdev(ibdev)->limits.max_cqes)
		return ERR_PTR(-EINVAL);

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
	if (context) {
		if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
			return ERR_PTR(-EFAULT);

		err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
					to_mucontext(context)->db_tab,
					ucmd.set_db_index, ucmd.set_db_page);
		if (err)
			return ERR_PTR(err);

		err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
					to_mucontext(context)->db_tab,
					ucmd.arm_db_index, ucmd.arm_db_page);
		if (err)
			goto err_unmap_set;
	}

L
Linus Torvalds 已提交
699
	cq = kmalloc(sizeof *cq, GFP_KERNEL);
700 701 702 703 704 705
	if (!cq) {
		err = -ENOMEM;
		goto err_unmap_arm;
	}

	if (context) {
706 707 708
		cq->buf.mr.ibmr.lkey = ucmd.lkey;
		cq->set_ci_db_index  = ucmd.set_db_index;
		cq->arm_db_index     = ucmd.arm_db_index;
709
	}
L
Linus Torvalds 已提交
710 711 712 713

	for (nent = 1; nent <= entries; nent <<= 1)
		; /* nothing */

714 715 716 717 718 719 720 721 722 723
	err = mthca_init_cq(to_mdev(ibdev), nent,
			    context ? to_mucontext(context) : NULL,
			    context ? ucmd.pdn : to_mdev(ibdev)->driver_pd.pd_num,
			    cq);
	if (err)
		goto err_free;

	if (context && ib_copy_to_udata(udata, &cq->cqn, sizeof (__u32))) {
		mthca_free_cq(to_mdev(ibdev), cq);
		goto err_free;
L
Linus Torvalds 已提交
724 725
	}

726 727
	cq->resize_buf = NULL;

L
Linus Torvalds 已提交
728
	return &cq->ibcq;
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

err_free:
	kfree(cq);

err_unmap_arm:
	if (context)
		mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
				    to_mucontext(context)->db_tab, ucmd.arm_db_index);

err_unmap_set:
	if (context)
		mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
				    to_mucontext(context)->db_tab, ucmd.set_db_index);

	return ERR_PTR(err);
L
Linus Torvalds 已提交
744 745
}

746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
static int mthca_alloc_resize_buf(struct mthca_dev *dev, struct mthca_cq *cq,
				  int entries)
{
	int ret;

	spin_lock_irq(&cq->lock);
	if (cq->resize_buf) {
		ret = -EBUSY;
		goto unlock;
	}

	cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
	if (!cq->resize_buf) {
		ret = -ENOMEM;
		goto unlock;
	}

	cq->resize_buf->state = CQ_RESIZE_ALLOC;

	ret = 0;

unlock:
	spin_unlock_irq(&cq->lock);

	if (ret)
		return ret;

	ret = mthca_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
	if (ret) {
		spin_lock_irq(&cq->lock);
		kfree(cq->resize_buf);
		cq->resize_buf = NULL;
		spin_unlock_irq(&cq->lock);
		return ret;
	}

	cq->resize_buf->cqe = entries - 1;

	spin_lock_irq(&cq->lock);
	cq->resize_buf->state = CQ_RESIZE_READY;
	spin_unlock_irq(&cq->lock);

	return 0;
}

static int mthca_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
{
	struct mthca_dev *dev = to_mdev(ibcq->device);
	struct mthca_cq *cq = to_mcq(ibcq);
	struct mthca_resize_cq ucmd;
	u32 lkey;
	u8 status;
	int ret;

	if (entries < 1 || entries > dev->limits.max_cqes)
		return -EINVAL;

803 804
	mutex_lock(&cq->mutex);

805
	entries = roundup_pow_of_two(entries + 1);
806 807 808 809
	if (entries == ibcq->cqe + 1) {
		ret = 0;
		goto out;
	}
810 811 812 813

	if (cq->is_kernel) {
		ret = mthca_alloc_resize_buf(dev, cq, entries);
		if (ret)
814
			goto out;
815 816
		lkey = cq->resize_buf->buf.mr.ibmr.lkey;
	} else {
817 818 819 820
		if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
			ret = -EFAULT;
			goto out;
		}
821 822 823
		lkey = ucmd.lkey;
	}

824
	ret = mthca_RESIZE_CQ(dev, cq->cqn, lkey, ilog2(entries), &status);
825 826 827 828 829 830 831 832 833 834 835 836
	if (status)
		ret = -EINVAL;

	if (ret) {
		if (cq->resize_buf) {
			mthca_free_cq_buf(dev, &cq->resize_buf->buf,
					  cq->resize_buf->cqe);
			kfree(cq->resize_buf);
			spin_lock_irq(&cq->lock);
			cq->resize_buf = NULL;
			spin_unlock_irq(&cq->lock);
		}
837
		goto out;
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
	}

	if (cq->is_kernel) {
		struct mthca_cq_buf tbuf;
		int tcqe;

		spin_lock_irq(&cq->lock);
		if (cq->resize_buf->state == CQ_RESIZE_READY) {
			mthca_cq_resize_copy_cqes(cq);
			tbuf         = cq->buf;
			tcqe         = cq->ibcq.cqe;
			cq->buf      = cq->resize_buf->buf;
			cq->ibcq.cqe = cq->resize_buf->cqe;
		} else {
			tbuf = cq->resize_buf->buf;
			tcqe = cq->resize_buf->cqe;
		}

		kfree(cq->resize_buf);
		cq->resize_buf = NULL;
		spin_unlock_irq(&cq->lock);

		mthca_free_cq_buf(dev, &tbuf, tcqe);
	} else
		ibcq->cqe = entries - 1;

864 865 866 867
out:
	mutex_unlock(&cq->mutex);

	return ret;
868 869
}

L
Linus Torvalds 已提交
870 871
static int mthca_destroy_cq(struct ib_cq *cq)
{
872 873 874 875 876 877 878 879 880 881
	if (cq->uobject) {
		mthca_unmap_user_db(to_mdev(cq->device),
				    &to_mucontext(cq->uobject->context)->uar,
				    to_mucontext(cq->uobject->context)->db_tab,
				    to_mcq(cq)->arm_db_index);
		mthca_unmap_user_db(to_mdev(cq->device),
				    &to_mucontext(cq->uobject->context)->uar,
				    to_mucontext(cq->uobject->context)->db_tab,
				    to_mcq(cq)->set_ci_db_index);
	}
L
Linus Torvalds 已提交
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
	mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
	kfree(cq);

	return 0;
}

static inline u32 convert_access(int acc)
{
	return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC       : 0) |
	       (acc & IB_ACCESS_REMOTE_WRITE  ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
	       (acc & IB_ACCESS_REMOTE_READ   ? MTHCA_MPT_FLAG_REMOTE_READ  : 0) |
	       (acc & IB_ACCESS_LOCAL_WRITE   ? MTHCA_MPT_FLAG_LOCAL_WRITE  : 0) |
	       MTHCA_MPT_FLAG_LOCAL_READ;
}

static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
{
	struct mthca_mr *mr;
	int err;

	mr = kmalloc(sizeof *mr, GFP_KERNEL);
	if (!mr)
		return ERR_PTR(-ENOMEM);

	err = mthca_mr_alloc_notrans(to_mdev(pd->device),
				     to_mpd(pd)->pd_num,
				     convert_access(acc), mr);

	if (err) {
		kfree(mr);
		return ERR_PTR(err);
	}

915 916
	mr->umem = NULL;

L
Linus Torvalds 已提交
917 918 919 920 921 922 923 924 925 926 927 928
	return &mr->ibmr;
}

static struct ib_mr *mthca_reg_phys_mr(struct ib_pd       *pd,
				       struct ib_phys_buf *buffer_list,
				       int                 num_phys_buf,
				       int                 acc,
				       u64                *iova_start)
{
	struct mthca_mr *mr;
	u64 *page_list;
	u64 total_size;
929
	unsigned long mask;
L
Linus Torvalds 已提交
930 931 932 933 934
	int shift;
	int npages;
	int err;
	int i, j, n;

935
	mask = buffer_list[0].addr ^ *iova_start;
L
Linus Torvalds 已提交
936 937
	total_size = 0;
	for (i = 0; i < num_phys_buf; ++i) {
938 939 940 941
		if (i != 0)
			mask |= buffer_list[i].addr;
		if (i != num_phys_buf - 1)
			mask |= buffer_list[i].addr + buffer_list[i].size;
L
Linus Torvalds 已提交
942 943 944 945

		total_size += buffer_list[i].size;
	}

946 947 948
	if (mask & ~PAGE_MASK)
		return ERR_PTR(-EINVAL);

949
	shift = __ffs(mask | 1 << 31);
L
Linus Torvalds 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991

	buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
	buffer_list[0].addr &= ~0ull << shift;

	mr = kmalloc(sizeof *mr, GFP_KERNEL);
	if (!mr)
		return ERR_PTR(-ENOMEM);

	npages = 0;
	for (i = 0; i < num_phys_buf; ++i)
		npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;

	if (!npages)
		return &mr->ibmr;

	page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
	if (!page_list) {
		kfree(mr);
		return ERR_PTR(-ENOMEM);
	}

	n = 0;
	for (i = 0; i < num_phys_buf; ++i)
		for (j = 0;
		     j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
		     ++j)
			page_list[n++] = buffer_list[i].addr + ((u64) j << shift);

	mthca_dbg(to_mdev(pd->device), "Registering memory at %llx (iova %llx) "
		  "in PD %x; shift %d, npages %d.\n",
		  (unsigned long long) buffer_list[0].addr,
		  (unsigned long long) *iova_start,
		  to_mpd(pd)->pd_num,
		  shift, npages);

	err = mthca_mr_alloc_phys(to_mdev(pd->device),
				  to_mpd(pd)->pd_num,
				  page_list, shift, npages,
				  *iova_start, total_size,
				  convert_access(acc), mr);

	if (err) {
992
		kfree(page_list);
L
Linus Torvalds 已提交
993 994 995 996 997
		kfree(mr);
		return ERR_PTR(err);
	}

	kfree(page_list);
998 999
	mr->umem = NULL;

L
Linus Torvalds 已提交
1000 1001 1002
	return &mr->ibmr;
}

1003 1004
static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
				       u64 virt, int acc, struct ib_udata *udata)
1005 1006 1007 1008
{
	struct mthca_dev *dev = to_mdev(pd->device);
	struct ib_umem_chunk *chunk;
	struct mthca_mr *mr;
1009
	struct mthca_reg_mr ucmd;
1010 1011 1012 1013
	u64 *pages;
	int shift, n, len;
	int i, j, k;
	int err = 0;
1014
	int write_mtt_size;
1015

1016 1017 1018
	if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
		return ERR_PTR(-EFAULT);

1019 1020 1021 1022
	mr = kmalloc(sizeof *mr, GFP_KERNEL);
	if (!mr)
		return ERR_PTR(-ENOMEM);

1023 1024 1025
	mr->umem = ib_umem_get(pd->uobject->context, start, length, acc,
			       ucmd.mr_attrs & MTHCA_MR_DMASYNC);

1026 1027 1028 1029 1030 1031 1032
	if (IS_ERR(mr->umem)) {
		err = PTR_ERR(mr->umem);
		goto err;
	}

	shift = ffs(mr->umem->page_size) - 1;

1033
	n = 0;
1034
	list_for_each_entry(chunk, &mr->umem->chunk_list, list)
1035 1036 1037 1038 1039
		n += chunk->nents;

	mr->mtt = mthca_alloc_mtt(dev, n);
	if (IS_ERR(mr->mtt)) {
		err = PTR_ERR(mr->mtt);
1040
		goto err_umem;
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	}

	pages = (u64 *) __get_free_page(GFP_KERNEL);
	if (!pages) {
		err = -ENOMEM;
		goto err_mtt;
	}

	i = n = 0;

1051 1052
	write_mtt_size = min(mthca_write_mtt_size(dev), (int) (PAGE_SIZE / sizeof *pages));

1053
	list_for_each_entry(chunk, &mr->umem->chunk_list, list)
1054 1055 1056 1057
		for (j = 0; j < chunk->nmap; ++j) {
			len = sg_dma_len(&chunk->page_list[j]) >> shift;
			for (k = 0; k < len; ++k) {
				pages[i++] = sg_dma_address(&chunk->page_list[j]) +
1058
					mr->umem->page_size * k;
1059
				/*
1060 1061
				 * Be friendly to write_mtt and pass it chunks
				 * of appropriate size.
1062
				 */
1063 1064
				if (i == write_mtt_size) {
					err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
					if (err)
						goto mtt_done;
					n += i;
					i = 0;
				}
			}
		}

	if (i)
		err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
mtt_done:
	free_page((unsigned long) pages);
	if (err)
		goto err_mtt;

1080 1081
	err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, shift, virt, length,
			     convert_access(acc), mr);
1082 1083 1084 1085 1086 1087 1088 1089 1090

	if (err)
		goto err_mtt;

	return &mr->ibmr;

err_mtt:
	mthca_free_mtt(dev, mr->mtt);

1091 1092 1093
err_umem:
	ib_umem_release(mr->umem);

1094 1095 1096 1097 1098
err:
	kfree(mr);
	return ERR_PTR(err);
}

L
Linus Torvalds 已提交
1099 1100
static int mthca_dereg_mr(struct ib_mr *mr)
{
1101
	struct mthca_mr *mmr = to_mmr(mr);
1102

1103
	mthca_free_mr(to_mdev(mr->device), mmr);
1104 1105
	if (mmr->umem)
		ib_umem_release(mmr->umem);
1106
	kfree(mmr);
1107

L
Linus Torvalds 已提交
1108 1109 1110
	return 0;
}

1111 1112 1113 1114 1115 1116
static struct ib_fmr *mthca_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
				      struct ib_fmr_attr *fmr_attr)
{
	struct mthca_fmr *fmr;
	int err;

1117
	fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
1118 1119 1120
	if (!fmr)
		return ERR_PTR(-ENOMEM);

1121
	memcpy(&fmr->attr, fmr_attr, sizeof *fmr_attr);
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 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
	err = mthca_fmr_alloc(to_mdev(pd->device), to_mpd(pd)->pd_num,
			     convert_access(mr_access_flags), fmr);

	if (err) {
		kfree(fmr);
		return ERR_PTR(err);
	}

	return &fmr->ibmr;
}

static int mthca_dealloc_fmr(struct ib_fmr *fmr)
{
	struct mthca_fmr *mfmr = to_mfmr(fmr);
	int err;

	err = mthca_free_fmr(to_mdev(fmr->device), mfmr);
	if (err)
		return err;

	kfree(mfmr);
	return 0;
}

static int mthca_unmap_fmr(struct list_head *fmr_list)
{
	struct ib_fmr *fmr;
	int err;
	u8 status;
	struct mthca_dev *mdev = NULL;

	list_for_each_entry(fmr, fmr_list, list) {
		if (mdev && to_mdev(fmr->device) != mdev)
			return -EINVAL;
		mdev = to_mdev(fmr->device);
	}

	if (!mdev)
		return 0;

1162
	if (mthca_is_memfree(mdev)) {
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
		list_for_each_entry(fmr, fmr_list, list)
			mthca_arbel_fmr_unmap(mdev, to_mfmr(fmr));

		wmb();
	} else
		list_for_each_entry(fmr, fmr_list, list)
			mthca_tavor_fmr_unmap(mdev, to_mfmr(fmr));

	err = mthca_SYNC_TPT(mdev, &status);
	if (err)
		return err;
	if (status)
		return -EINVAL;
	return 0;
}

1179 1180
static ssize_t show_rev(struct device *device, struct device_attribute *attr,
			char *buf)
L
Linus Torvalds 已提交
1181
{
1182 1183
	struct mthca_dev *dev =
		container_of(device, struct mthca_dev, ib_dev.dev);
L
Linus Torvalds 已提交
1184 1185 1186
	return sprintf(buf, "%x\n", dev->rev_id);
}

1187 1188
static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
			   char *buf)
L
Linus Torvalds 已提交
1189
{
1190 1191
	struct mthca_dev *dev =
		container_of(device, struct mthca_dev, ib_dev.dev);
1192
	return sprintf(buf, "%d.%d.%d\n", (int) (dev->fw_ver >> 32),
L
Linus Torvalds 已提交
1193 1194 1195 1196
		       (int) (dev->fw_ver >> 16) & 0xffff,
		       (int) dev->fw_ver & 0xffff);
}

1197 1198
static ssize_t show_hca(struct device *device, struct device_attribute *attr,
			char *buf)
L
Linus Torvalds 已提交
1199
{
1200 1201
	struct mthca_dev *dev =
		container_of(device, struct mthca_dev, ib_dev.dev);
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
	switch (dev->pdev->device) {
	case PCI_DEVICE_ID_MELLANOX_TAVOR:
		return sprintf(buf, "MT23108\n");
	case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
		return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
	case PCI_DEVICE_ID_MELLANOX_ARBEL:
		return sprintf(buf, "MT25208\n");
	case PCI_DEVICE_ID_MELLANOX_SINAI:
	case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
		return sprintf(buf, "MT25204\n");
	default:
		return sprintf(buf, "unknown\n");
L
Linus Torvalds 已提交
1214 1215 1216
	}
}

1217 1218
static ssize_t show_board(struct device *device, struct device_attribute *attr,
			  char *buf)
1219
{
1220 1221
	struct mthca_dev *dev =
		container_of(device, struct mthca_dev, ib_dev.dev);
1222 1223 1224
	return sprintf(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
}

1225 1226 1227 1228
static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
L
Linus Torvalds 已提交
1229

1230 1231 1232 1233 1234
static struct device_attribute *mthca_dev_attributes[] = {
	&dev_attr_hw_rev,
	&dev_attr_fw_ver,
	&dev_attr_hca_type,
	&dev_attr_board_id
L
Linus Torvalds 已提交
1235 1236
};

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
static int mthca_init_node_data(struct mthca_dev *dev)
{
	struct ib_smp *in_mad  = NULL;
	struct ib_smp *out_mad = NULL;
	int err = -ENOMEM;
	u8 status;

	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
	if (!in_mad || !out_mad)
		goto out;

	init_query_mad(in_mad);
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
	in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;

	err = mthca_MAD_IFC(dev, 1, 1,
			    1, NULL, NULL, in_mad, out_mad,
			    &status);
	if (err)
		goto out;
	if (status) {
		err = -EINVAL;
		goto out;
	}

	memcpy(dev->ib_dev.node_desc, out_mad->data, 64);

1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;

	err = mthca_MAD_IFC(dev, 1, 1,
			    1, NULL, NULL, in_mad, out_mad,
			    &status);
	if (err)
		goto out;
	if (status) {
		err = -EINVAL;
		goto out;
	}

1276 1277
	if (mthca_is_memfree(dev))
		dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
1278 1279 1280 1281 1282 1283 1284 1285
	memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);

out:
	kfree(in_mad);
	kfree(out_mad);
	return err;
}

L
Linus Torvalds 已提交
1286 1287 1288 1289 1290
int mthca_register_device(struct mthca_dev *dev)
{
	int ret;
	int i;

1291 1292 1293 1294
	ret = mthca_init_node_data(dev);
	if (ret)
		return ret;

L
Linus Torvalds 已提交
1295
	strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
1296 1297
	dev->ib_dev.owner                = THIS_MODULE;

1298
	dev->ib_dev.uverbs_abi_ver	 = MTHCA_UVERBS_ABI_VERSION;
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
	dev->ib_dev.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)		|
1309
		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
1310 1311
		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
1312
		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
1313 1314 1315
		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
1316
		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST);
T
Tom Tucker 已提交
1317
	dev->ib_dev.node_type            = RDMA_NODE_IB_CA;
L
Linus Torvalds 已提交
1318
	dev->ib_dev.phys_port_cnt        = dev->limits.num_ports;
1319
	dev->ib_dev.num_comp_vectors     = 1;
L
Linus Torvalds 已提交
1320 1321 1322
	dev->ib_dev.dma_device           = &dev->pdev->dev;
	dev->ib_dev.query_device         = mthca_query_device;
	dev->ib_dev.query_port           = mthca_query_port;
1323
	dev->ib_dev.modify_device        = mthca_modify_device;
L
Linus Torvalds 已提交
1324 1325 1326
	dev->ib_dev.modify_port          = mthca_modify_port;
	dev->ib_dev.query_pkey           = mthca_query_pkey;
	dev->ib_dev.query_gid            = mthca_query_gid;
1327 1328
	dev->ib_dev.alloc_ucontext       = mthca_alloc_ucontext;
	dev->ib_dev.dealloc_ucontext     = mthca_dealloc_ucontext;
1329
	dev->ib_dev.mmap                 = mthca_mmap_uar;
L
Linus Torvalds 已提交
1330 1331 1332
	dev->ib_dev.alloc_pd             = mthca_alloc_pd;
	dev->ib_dev.dealloc_pd           = mthca_dealloc_pd;
	dev->ib_dev.create_ah            = mthca_ah_create;
1333
	dev->ib_dev.query_ah             = mthca_ah_query;
L
Linus Torvalds 已提交
1334
	dev->ib_dev.destroy_ah           = mthca_ah_destroy;
1335 1336 1337

	if (dev->mthca_flags & MTHCA_FLAG_SRQ) {
		dev->ib_dev.create_srq           = mthca_create_srq;
1338 1339
		dev->ib_dev.modify_srq           = mthca_modify_srq;
		dev->ib_dev.query_srq            = mthca_query_srq;
1340
		dev->ib_dev.destroy_srq          = mthca_destroy_srq;
1341 1342 1343 1344 1345
		dev->ib_dev.uverbs_cmd_mask	|=
			(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
			(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
			(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
			(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
1346 1347 1348 1349 1350 1351 1352

		if (mthca_is_memfree(dev))
			dev->ib_dev.post_srq_recv = mthca_arbel_post_srq_recv;
		else
			dev->ib_dev.post_srq_recv = mthca_tavor_post_srq_recv;
	}

L
Linus Torvalds 已提交
1353 1354
	dev->ib_dev.create_qp            = mthca_create_qp;
	dev->ib_dev.modify_qp            = mthca_modify_qp;
1355
	dev->ib_dev.query_qp             = mthca_query_qp;
L
Linus Torvalds 已提交
1356 1357
	dev->ib_dev.destroy_qp           = mthca_destroy_qp;
	dev->ib_dev.create_cq            = mthca_create_cq;
1358
	dev->ib_dev.resize_cq            = mthca_resize_cq;
L
Linus Torvalds 已提交
1359 1360 1361 1362
	dev->ib_dev.destroy_cq           = mthca_destroy_cq;
	dev->ib_dev.poll_cq              = mthca_poll_cq;
	dev->ib_dev.get_dma_mr           = mthca_get_dma_mr;
	dev->ib_dev.reg_phys_mr          = mthca_reg_phys_mr;
1363
	dev->ib_dev.reg_user_mr          = mthca_reg_user_mr;
L
Linus Torvalds 已提交
1364
	dev->ib_dev.dereg_mr             = mthca_dereg_mr;
1365 1366 1367 1368 1369

	if (dev->mthca_flags & MTHCA_FLAG_FMR) {
		dev->ib_dev.alloc_fmr            = mthca_alloc_fmr;
		dev->ib_dev.unmap_fmr            = mthca_unmap_fmr;
		dev->ib_dev.dealloc_fmr          = mthca_dealloc_fmr;
1370
		if (mthca_is_memfree(dev))
1371 1372 1373 1374 1375
			dev->ib_dev.map_phys_fmr = mthca_arbel_map_phys_fmr;
		else
			dev->ib_dev.map_phys_fmr = mthca_tavor_map_phys_fmr;
	}

L
Linus Torvalds 已提交
1376 1377 1378 1379
	dev->ib_dev.attach_mcast         = mthca_multicast_attach;
	dev->ib_dev.detach_mcast         = mthca_multicast_detach;
	dev->ib_dev.process_mad          = mthca_process_mad;

1380
	if (mthca_is_memfree(dev)) {
L
Linus Torvalds 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389
		dev->ib_dev.req_notify_cq = mthca_arbel_arm_cq;
		dev->ib_dev.post_send     = mthca_arbel_post_send;
		dev->ib_dev.post_recv     = mthca_arbel_post_receive;
	} else {
		dev->ib_dev.req_notify_cq = mthca_tavor_arm_cq;
		dev->ib_dev.post_send     = mthca_tavor_post_send;
		dev->ib_dev.post_recv     = mthca_tavor_post_receive;
	}

1390
	mutex_init(&dev->cap_mask_mutex);
L
Linus Torvalds 已提交
1391 1392 1393 1394 1395

	ret = ib_register_device(&dev->ib_dev);
	if (ret)
		return ret;

1396 1397 1398
	for (i = 0; i < ARRAY_SIZE(mthca_dev_attributes); ++i) {
		ret = device_create_file(&dev->ib_dev.dev,
					 mthca_dev_attributes[i]);
L
Linus Torvalds 已提交
1399 1400 1401 1402 1403 1404
		if (ret) {
			ib_unregister_device(&dev->ib_dev);
			return ret;
		}
	}

1405 1406
	mthca_start_catas_poll(dev);

L
Linus Torvalds 已提交
1407 1408 1409 1410 1411
	return 0;
}

void mthca_unregister_device(struct mthca_dev *dev)
{
1412
	mthca_stop_catas_poll(dev);
L
Linus Torvalds 已提交
1413 1414
	ib_unregister_device(&dev->ib_dev);
}