1 /* 2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved. 3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved. 4 * 5 * This software is available to you under a choice of one of two 6 * licenses. You may choose to be licensed under the terms of the GNU 7 * General Public License (GPL) Version 2, available from the file 8 * COPYING in the main directory of this source tree, or the 9 * OpenIB.org BSD license below: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * - Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 */ 33 34 #include <linux/module.h> 35 #include <linux/init.h> 36 #include <linux/errno.h> 37 38 #include <rdma/ib_smi.h> 39 #include <rdma/ib_user_verbs.h> 40 41 #include <linux/mlx4/driver.h> 42 #include <linux/mlx4/cmd.h> 43 44 #include "mlx4_ib.h" 45 #include "user.h" 46 47 #define DRV_NAME "mlx4_ib" 48 #define DRV_VERSION "1.0" 49 #define DRV_RELDATE "April 4, 2008" 50 51 MODULE_AUTHOR("Roland Dreier"); 52 MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver"); 53 MODULE_LICENSE("Dual BSD/GPL"); 54 MODULE_VERSION(DRV_VERSION); 55 56 static const char mlx4_ib_version[] = 57 DRV_NAME ": Mellanox ConnectX InfiniBand driver v" 58 DRV_VERSION " (" DRV_RELDATE ")\n"; 59 60 static void init_query_mad(struct ib_smp *mad) 61 { 62 mad->base_version = 1; 63 mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED; 64 mad->class_version = 1; 65 mad->method = IB_MGMT_METHOD_GET; 66 } 67 68 static int mlx4_ib_query_device(struct ib_device *ibdev, 69 struct ib_device_attr *props) 70 { 71 struct mlx4_ib_dev *dev = to_mdev(ibdev); 72 struct ib_smp *in_mad = NULL; 73 struct ib_smp *out_mad = NULL; 74 int err = -ENOMEM; 75 76 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL); 77 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL); 78 if (!in_mad || !out_mad) 79 goto out; 80 81 init_query_mad(in_mad); 82 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO; 83 84 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad, out_mad); 85 if (err) 86 goto out; 87 88 memset(props, 0, sizeof *props); 89 90 props->fw_ver = dev->dev->caps.fw_ver; 91 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 92 IB_DEVICE_PORT_ACTIVE_EVENT | 93 IB_DEVICE_SYS_IMAGE_GUID | 94 IB_DEVICE_RC_RNR_NAK_GEN | 95 IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 96 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR) 97 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 98 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR) 99 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 100 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM) 101 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 102 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT) 103 props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE; 104 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM) 105 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 106 if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH) 107 props->device_cap_flags |= IB_DEVICE_UD_TSO; 108 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY) 109 props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY; 110 if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) && 111 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) && 112 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR)) 113 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 114 115 props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) & 116 0xffffff; 117 props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30)); 118 props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32)); 119 memcpy(&props->sys_image_guid, out_mad->data + 4, 8); 120 121 props->max_mr_size = ~0ull; 122 props->page_size_cap = dev->dev->caps.page_size_cap; 123 props->max_qp = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps; 124 props->max_qp_wr = dev->dev->caps.max_wqes; 125 props->max_sge = min(dev->dev->caps.max_sq_sg, 126 dev->dev->caps.max_rq_sg); 127 props->max_cq = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs; 128 props->max_cqe = dev->dev->caps.max_cqes; 129 props->max_mr = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws; 130 props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds; 131 props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma; 132 props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma; 133 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 134 props->max_srq = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs; 135 props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1; 136 props->max_srq_sge = dev->dev->caps.max_srq_sge; 137 props->max_fast_reg_page_list_len = PAGE_SIZE / sizeof (u64); 138 props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay; 139 props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ? 140 IB_ATOMIC_HCA : IB_ATOMIC_NONE; 141 props->max_pkeys = dev->dev->caps.pkey_table_len[1]; 142 props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms; 143 props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm; 144 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 145 props->max_mcast_grp; 146 props->max_map_per_fmr = (1 << (32 - ilog2(dev->dev->caps.num_mpts))) - 1; 147 148 out: 149 kfree(in_mad); 150 kfree(out_mad); 151 152 return err; 153 } 154 155 static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port, 156 struct ib_port_attr *props) 157 { 158 struct ib_smp *in_mad = NULL; 159 struct ib_smp *out_mad = NULL; 160 int err = -ENOMEM; 161 162 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL); 163 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL); 164 if (!in_mad || !out_mad) 165 goto out; 166 167 memset(props, 0, sizeof *props); 168 169 init_query_mad(in_mad); 170 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO; 171 in_mad->attr_mod = cpu_to_be32(port); 172 173 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad); 174 if (err) 175 goto out; 176 177 props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16)); 178 props->lmc = out_mad->data[34] & 0x7; 179 props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18)); 180 props->sm_sl = out_mad->data[36] & 0xf; 181 props->state = out_mad->data[32] & 0xf; 182 props->phys_state = out_mad->data[33] >> 4; 183 props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20)); 184 props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port]; 185 props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz; 186 props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port]; 187 props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46)); 188 props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48)); 189 props->active_width = out_mad->data[31] & 0xf; 190 props->active_speed = out_mad->data[35] >> 4; 191 props->max_mtu = out_mad->data[41] & 0xf; 192 props->active_mtu = out_mad->data[36] >> 4; 193 props->subnet_timeout = out_mad->data[51] & 0x1f; 194 props->max_vl_num = out_mad->data[37] >> 4; 195 props->init_type_reply = out_mad->data[41] >> 4; 196 197 out: 198 kfree(in_mad); 199 kfree(out_mad); 200 201 return err; 202 } 203 204 static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index, 205 union ib_gid *gid) 206 { 207 struct ib_smp *in_mad = NULL; 208 struct ib_smp *out_mad = NULL; 209 int err = -ENOMEM; 210 211 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL); 212 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL); 213 if (!in_mad || !out_mad) 214 goto out; 215 216 init_query_mad(in_mad); 217 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO; 218 in_mad->attr_mod = cpu_to_be32(port); 219 220 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad); 221 if (err) 222 goto out; 223 224 memcpy(gid->raw, out_mad->data + 8, 8); 225 226 init_query_mad(in_mad); 227 in_mad->attr_id = IB_SMP_ATTR_GUID_INFO; 228 in_mad->attr_mod = cpu_to_be32(index / 8); 229 230 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad); 231 if (err) 232 goto out; 233 234 memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8); 235 236 out: 237 kfree(in_mad); 238 kfree(out_mad); 239 return err; 240 } 241 242 static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 243 u16 *pkey) 244 { 245 struct ib_smp *in_mad = NULL; 246 struct ib_smp *out_mad = NULL; 247 int err = -ENOMEM; 248 249 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL); 250 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL); 251 if (!in_mad || !out_mad) 252 goto out; 253 254 init_query_mad(in_mad); 255 in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE; 256 in_mad->attr_mod = cpu_to_be32(index / 32); 257 258 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad); 259 if (err) 260 goto out; 261 262 *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]); 263 264 out: 265 kfree(in_mad); 266 kfree(out_mad); 267 return err; 268 } 269 270 static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask, 271 struct ib_device_modify *props) 272 { 273 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 274 return -EOPNOTSUPP; 275 276 if (mask & IB_DEVICE_MODIFY_NODE_DESC) { 277 spin_lock(&to_mdev(ibdev)->sm_lock); 278 memcpy(ibdev->node_desc, props->node_desc, 64); 279 spin_unlock(&to_mdev(ibdev)->sm_lock); 280 } 281 282 return 0; 283 } 284 285 static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols, 286 u32 cap_mask) 287 { 288 struct mlx4_cmd_mailbox *mailbox; 289 int err; 290 291 mailbox = mlx4_alloc_cmd_mailbox(dev->dev); 292 if (IS_ERR(mailbox)) 293 return PTR_ERR(mailbox); 294 295 memset(mailbox->buf, 0, 256); 296 297 if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) { 298 *(u8 *) mailbox->buf = !!reset_qkey_viols << 6; 299 ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask); 300 } else { 301 ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols; 302 ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask); 303 } 304 305 err = mlx4_cmd(dev->dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT, 306 MLX4_CMD_TIME_CLASS_B); 307 308 mlx4_free_cmd_mailbox(dev->dev, mailbox); 309 return err; 310 } 311 312 static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask, 313 struct ib_port_modify *props) 314 { 315 struct ib_port_attr attr; 316 u32 cap_mask; 317 int err; 318 319 mutex_lock(&to_mdev(ibdev)->cap_mask_mutex); 320 321 err = mlx4_ib_query_port(ibdev, port, &attr); 322 if (err) 323 goto out; 324 325 cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) & 326 ~props->clr_port_cap_mask; 327 328 err = mlx4_SET_PORT(to_mdev(ibdev), port, 329 !!(mask & IB_PORT_RESET_QKEY_CNTR), 330 cap_mask); 331 332 out: 333 mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex); 334 return err; 335 } 336 337 static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev, 338 struct ib_udata *udata) 339 { 340 struct mlx4_ib_dev *dev = to_mdev(ibdev); 341 struct mlx4_ib_ucontext *context; 342 struct mlx4_ib_alloc_ucontext_resp resp; 343 int err; 344 345 if (!dev->ib_active) 346 return ERR_PTR(-EAGAIN); 347 348 resp.qp_tab_size = dev->dev->caps.num_qps; 349 resp.bf_reg_size = dev->dev->caps.bf_reg_size; 350 resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page; 351 352 context = kmalloc(sizeof *context, GFP_KERNEL); 353 if (!context) 354 return ERR_PTR(-ENOMEM); 355 356 err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar); 357 if (err) { 358 kfree(context); 359 return ERR_PTR(err); 360 } 361 362 INIT_LIST_HEAD(&context->db_page_list); 363 mutex_init(&context->db_page_mutex); 364 365 err = ib_copy_to_udata(udata, &resp, sizeof resp); 366 if (err) { 367 mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar); 368 kfree(context); 369 return ERR_PTR(-EFAULT); 370 } 371 372 return &context->ibucontext; 373 } 374 375 static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 376 { 377 struct mlx4_ib_ucontext *context = to_mucontext(ibcontext); 378 379 mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar); 380 kfree(context); 381 382 return 0; 383 } 384 385 static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma) 386 { 387 struct mlx4_ib_dev *dev = to_mdev(context->device); 388 389 if (vma->vm_end - vma->vm_start != PAGE_SIZE) 390 return -EINVAL; 391 392 if (vma->vm_pgoff == 0) { 393 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 394 395 if (io_remap_pfn_range(vma, vma->vm_start, 396 to_mucontext(context)->uar.pfn, 397 PAGE_SIZE, vma->vm_page_prot)) 398 return -EAGAIN; 399 } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) { 400 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); 401 402 if (io_remap_pfn_range(vma, vma->vm_start, 403 to_mucontext(context)->uar.pfn + 404 dev->dev->caps.num_uars, 405 PAGE_SIZE, vma->vm_page_prot)) 406 return -EAGAIN; 407 } else 408 return -EINVAL; 409 410 return 0; 411 } 412 413 static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev, 414 struct ib_ucontext *context, 415 struct ib_udata *udata) 416 { 417 struct mlx4_ib_pd *pd; 418 int err; 419 420 pd = kmalloc(sizeof *pd, GFP_KERNEL); 421 if (!pd) 422 return ERR_PTR(-ENOMEM); 423 424 err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn); 425 if (err) { 426 kfree(pd); 427 return ERR_PTR(err); 428 } 429 430 if (context) 431 if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) { 432 mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn); 433 kfree(pd); 434 return ERR_PTR(-EFAULT); 435 } 436 437 return &pd->ibpd; 438 } 439 440 static int mlx4_ib_dealloc_pd(struct ib_pd *pd) 441 { 442 mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn); 443 kfree(pd); 444 445 return 0; 446 } 447 448 static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 449 { 450 return mlx4_multicast_attach(to_mdev(ibqp->device)->dev, 451 &to_mqp(ibqp)->mqp, gid->raw, 452 !!(to_mqp(ibqp)->flags & 453 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)); 454 } 455 456 static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 457 { 458 return mlx4_multicast_detach(to_mdev(ibqp->device)->dev, 459 &to_mqp(ibqp)->mqp, gid->raw); 460 } 461 462 static int init_node_data(struct mlx4_ib_dev *dev) 463 { 464 struct ib_smp *in_mad = NULL; 465 struct ib_smp *out_mad = NULL; 466 int err = -ENOMEM; 467 468 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL); 469 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL); 470 if (!in_mad || !out_mad) 471 goto out; 472 473 init_query_mad(in_mad); 474 in_mad->attr_id = IB_SMP_ATTR_NODE_DESC; 475 476 err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad); 477 if (err) 478 goto out; 479 480 memcpy(dev->ib_dev.node_desc, out_mad->data, 64); 481 482 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO; 483 484 err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad); 485 if (err) 486 goto out; 487 488 dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32)); 489 memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8); 490 491 out: 492 kfree(in_mad); 493 kfree(out_mad); 494 return err; 495 } 496 497 static ssize_t show_hca(struct device *device, struct device_attribute *attr, 498 char *buf) 499 { 500 struct mlx4_ib_dev *dev = 501 container_of(device, struct mlx4_ib_dev, ib_dev.dev); 502 return sprintf(buf, "MT%d\n", dev->dev->pdev->device); 503 } 504 505 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr, 506 char *buf) 507 { 508 struct mlx4_ib_dev *dev = 509 container_of(device, struct mlx4_ib_dev, ib_dev.dev); 510 return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32), 511 (int) (dev->dev->caps.fw_ver >> 16) & 0xffff, 512 (int) dev->dev->caps.fw_ver & 0xffff); 513 } 514 515 static ssize_t show_rev(struct device *device, struct device_attribute *attr, 516 char *buf) 517 { 518 struct mlx4_ib_dev *dev = 519 container_of(device, struct mlx4_ib_dev, ib_dev.dev); 520 return sprintf(buf, "%x\n", dev->dev->rev_id); 521 } 522 523 static ssize_t show_board(struct device *device, struct device_attribute *attr, 524 char *buf) 525 { 526 struct mlx4_ib_dev *dev = 527 container_of(device, struct mlx4_ib_dev, ib_dev.dev); 528 return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN, 529 dev->dev->board_id); 530 } 531 532 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 533 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL); 534 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); 535 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); 536 537 static struct device_attribute *mlx4_class_attributes[] = { 538 &dev_attr_hw_rev, 539 &dev_attr_fw_ver, 540 &dev_attr_hca_type, 541 &dev_attr_board_id 542 }; 543 544 static void *mlx4_ib_add(struct mlx4_dev *dev) 545 { 546 struct mlx4_ib_dev *ibdev; 547 int num_ports = 0; 548 int i; 549 550 printk_once(KERN_INFO "%s", mlx4_ib_version); 551 552 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB) 553 num_ports++; 554 555 /* No point in registering a device with no ports... */ 556 if (num_ports == 0) 557 return NULL; 558 559 ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev); 560 if (!ibdev) { 561 dev_err(&dev->pdev->dev, "Device struct alloc failed\n"); 562 return NULL; 563 } 564 565 if (mlx4_pd_alloc(dev, &ibdev->priv_pdn)) 566 goto err_dealloc; 567 568 if (mlx4_uar_alloc(dev, &ibdev->priv_uar)) 569 goto err_pd; 570 571 ibdev->uar_map = ioremap(ibdev->priv_uar.pfn << PAGE_SHIFT, PAGE_SIZE); 572 if (!ibdev->uar_map) 573 goto err_uar; 574 MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock); 575 576 ibdev->dev = dev; 577 578 strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX); 579 ibdev->ib_dev.owner = THIS_MODULE; 580 ibdev->ib_dev.node_type = RDMA_NODE_IB_CA; 581 ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey; 582 ibdev->num_ports = num_ports; 583 ibdev->ib_dev.phys_port_cnt = ibdev->num_ports; 584 ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors; 585 ibdev->ib_dev.dma_device = &dev->pdev->dev; 586 587 ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION; 588 ibdev->ib_dev.uverbs_cmd_mask = 589 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 590 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 591 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 592 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 593 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 594 (1ull << IB_USER_VERBS_CMD_REG_MR) | 595 (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 596 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 597 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 598 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 599 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 600 (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 601 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 602 (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 603 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 604 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 605 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 606 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 607 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 608 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 609 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ); 610 611 ibdev->ib_dev.query_device = mlx4_ib_query_device; 612 ibdev->ib_dev.query_port = mlx4_ib_query_port; 613 ibdev->ib_dev.query_gid = mlx4_ib_query_gid; 614 ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey; 615 ibdev->ib_dev.modify_device = mlx4_ib_modify_device; 616 ibdev->ib_dev.modify_port = mlx4_ib_modify_port; 617 ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext; 618 ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext; 619 ibdev->ib_dev.mmap = mlx4_ib_mmap; 620 ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd; 621 ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd; 622 ibdev->ib_dev.create_ah = mlx4_ib_create_ah; 623 ibdev->ib_dev.query_ah = mlx4_ib_query_ah; 624 ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah; 625 ibdev->ib_dev.create_srq = mlx4_ib_create_srq; 626 ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq; 627 ibdev->ib_dev.query_srq = mlx4_ib_query_srq; 628 ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq; 629 ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv; 630 ibdev->ib_dev.create_qp = mlx4_ib_create_qp; 631 ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp; 632 ibdev->ib_dev.query_qp = mlx4_ib_query_qp; 633 ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp; 634 ibdev->ib_dev.post_send = mlx4_ib_post_send; 635 ibdev->ib_dev.post_recv = mlx4_ib_post_recv; 636 ibdev->ib_dev.create_cq = mlx4_ib_create_cq; 637 ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq; 638 ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq; 639 ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq; 640 ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq; 641 ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq; 642 ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr; 643 ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr; 644 ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr; 645 ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr; 646 ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list; 647 ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list; 648 ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach; 649 ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach; 650 ibdev->ib_dev.process_mad = mlx4_ib_process_mad; 651 652 ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc; 653 ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr; 654 ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr; 655 ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc; 656 657 if (init_node_data(ibdev)) 658 goto err_map; 659 660 spin_lock_init(&ibdev->sm_lock); 661 mutex_init(&ibdev->cap_mask_mutex); 662 663 if (ib_register_device(&ibdev->ib_dev)) 664 goto err_map; 665 666 if (mlx4_ib_mad_init(ibdev)) 667 goto err_reg; 668 669 for (i = 0; i < ARRAY_SIZE(mlx4_class_attributes); ++i) { 670 if (device_create_file(&ibdev->ib_dev.dev, 671 mlx4_class_attributes[i])) 672 goto err_reg; 673 } 674 675 ibdev->ib_active = true; 676 677 return ibdev; 678 679 err_reg: 680 ib_unregister_device(&ibdev->ib_dev); 681 682 err_map: 683 iounmap(ibdev->uar_map); 684 685 err_uar: 686 mlx4_uar_free(dev, &ibdev->priv_uar); 687 688 err_pd: 689 mlx4_pd_free(dev, ibdev->priv_pdn); 690 691 err_dealloc: 692 ib_dealloc_device(&ibdev->ib_dev); 693 694 return NULL; 695 } 696 697 static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr) 698 { 699 struct mlx4_ib_dev *ibdev = ibdev_ptr; 700 int p; 701 702 mlx4_ib_mad_cleanup(ibdev); 703 ib_unregister_device(&ibdev->ib_dev); 704 705 for (p = 1; p <= ibdev->num_ports; ++p) 706 mlx4_CLOSE_PORT(dev, p); 707 708 iounmap(ibdev->uar_map); 709 mlx4_uar_free(dev, &ibdev->priv_uar); 710 mlx4_pd_free(dev, ibdev->priv_pdn); 711 ib_dealloc_device(&ibdev->ib_dev); 712 } 713 714 static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr, 715 enum mlx4_dev_event event, int port) 716 { 717 struct ib_event ibev; 718 struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr); 719 720 if (port > ibdev->num_ports) 721 return; 722 723 switch (event) { 724 case MLX4_DEV_EVENT_PORT_UP: 725 ibev.event = IB_EVENT_PORT_ACTIVE; 726 break; 727 728 case MLX4_DEV_EVENT_PORT_DOWN: 729 ibev.event = IB_EVENT_PORT_ERR; 730 break; 731 732 case MLX4_DEV_EVENT_CATASTROPHIC_ERROR: 733 ibdev->ib_active = false; 734 ibev.event = IB_EVENT_DEVICE_FATAL; 735 break; 736 737 default: 738 return; 739 } 740 741 ibev.device = ibdev_ptr; 742 ibev.element.port_num = port; 743 744 ib_dispatch_event(&ibev); 745 } 746 747 static struct mlx4_interface mlx4_ib_interface = { 748 .add = mlx4_ib_add, 749 .remove = mlx4_ib_remove, 750 .event = mlx4_ib_event 751 }; 752 753 static int __init mlx4_ib_init(void) 754 { 755 return mlx4_register_interface(&mlx4_ib_interface); 756 } 757 758 static void __exit mlx4_ib_cleanup(void) 759 { 760 mlx4_unregister_interface(&mlx4_ib_interface); 761 } 762 763 module_init(mlx4_ib_init); 764 module_exit(mlx4_ib_cleanup); 765