1 /* This file is part of the Emulex RoCE Device Driver for 2 * RoCE (RDMA over Converged Ethernet) adapters. 3 * Copyright (C) 2012-2015 Emulex. All rights reserved. 4 * EMULEX and SLI are trademarks of Emulex. 5 * www.emulex.com 6 * 7 * This software is available to you under a choice of one of two licenses. 8 * You may choose to be licensed under the terms of the GNU General Public 9 * License (GPL) Version 2, available from the file COPYING in the main 10 * directory of this source tree, or the BSD license below: 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * - Redistributions of source code must retain the above copyright notice, 17 * this list of conditions and the following disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * Contact Information: 36 * linux-drivers@emulex.com 37 * 38 * Emulex 39 * 3333 Susan Street 40 * Costa Mesa, CA 92626 41 */ 42 43 #include <linux/module.h> 44 #include <linux/idr.h> 45 #include <rdma/ib_verbs.h> 46 #include <rdma/ib_user_verbs.h> 47 #include <rdma/ib_addr.h> 48 #include <rdma/ib_mad.h> 49 50 #include <linux/netdevice.h> 51 #include <net/addrconf.h> 52 53 #include "ocrdma.h" 54 #include "ocrdma_verbs.h" 55 #include "ocrdma_ah.h" 56 #include "be_roce.h" 57 #include "ocrdma_hw.h" 58 #include "ocrdma_stats.h" 59 #include <rdma/ocrdma-abi.h> 60 61 MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION); 62 MODULE_AUTHOR("Emulex Corporation"); 63 MODULE_LICENSE("Dual BSD/GPL"); 64 65 static DEFINE_IDR(ocrdma_dev_id); 66 67 void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid) 68 { 69 u8 mac_addr[6]; 70 71 memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN); 72 guid[0] = mac_addr[0] ^ 2; 73 guid[1] = mac_addr[1]; 74 guid[2] = mac_addr[2]; 75 guid[3] = 0xff; 76 guid[4] = 0xfe; 77 guid[5] = mac_addr[3]; 78 guid[6] = mac_addr[4]; 79 guid[7] = mac_addr[5]; 80 } 81 static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device, 82 u8 port_num) 83 { 84 return IB_LINK_LAYER_ETHERNET; 85 } 86 87 static int ocrdma_port_immutable(struct ib_device *ibdev, u8 port_num, 88 struct ib_port_immutable *immutable) 89 { 90 struct ib_port_attr attr; 91 struct ocrdma_dev *dev; 92 int err; 93 94 dev = get_ocrdma_dev(ibdev); 95 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE; 96 if (ocrdma_is_udp_encap_supported(dev)) 97 immutable->core_cap_flags |= RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP; 98 99 err = ib_query_port(ibdev, port_num, &attr); 100 if (err) 101 return err; 102 103 immutable->pkey_tbl_len = attr.pkey_tbl_len; 104 immutable->gid_tbl_len = attr.gid_tbl_len; 105 immutable->max_mad_size = IB_MGMT_MAD_SIZE; 106 107 return 0; 108 } 109 110 static void get_dev_fw_str(struct ib_device *device, char *str) 111 { 112 struct ocrdma_dev *dev = get_ocrdma_dev(device); 113 114 snprintf(str, IB_FW_VERSION_NAME_MAX, "%s", &dev->attr.fw_ver[0]); 115 } 116 117 /* OCRDMA sysfs interface */ 118 static ssize_t hw_rev_show(struct device *device, 119 struct device_attribute *attr, char *buf) 120 { 121 struct ocrdma_dev *dev = 122 rdma_device_to_drv_device(device, struct ocrdma_dev, ibdev); 123 124 return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor); 125 } 126 static DEVICE_ATTR_RO(hw_rev); 127 128 static ssize_t hca_type_show(struct device *device, 129 struct device_attribute *attr, char *buf) 130 { 131 struct ocrdma_dev *dev = 132 rdma_device_to_drv_device(device, struct ocrdma_dev, ibdev); 133 134 return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]); 135 } 136 static DEVICE_ATTR_RO(hca_type); 137 138 static struct attribute *ocrdma_attributes[] = { 139 &dev_attr_hw_rev.attr, 140 &dev_attr_hca_type.attr, 141 NULL 142 }; 143 144 static const struct attribute_group ocrdma_attr_group = { 145 .attrs = ocrdma_attributes, 146 }; 147 148 static const struct ib_device_ops ocrdma_dev_ops = { 149 .alloc_mr = ocrdma_alloc_mr, 150 .alloc_pd = ocrdma_alloc_pd, 151 .alloc_ucontext = ocrdma_alloc_ucontext, 152 .create_ah = ocrdma_create_ah, 153 .create_cq = ocrdma_create_cq, 154 .create_qp = ocrdma_create_qp, 155 .dealloc_pd = ocrdma_dealloc_pd, 156 .dealloc_ucontext = ocrdma_dealloc_ucontext, 157 .dereg_mr = ocrdma_dereg_mr, 158 .destroy_ah = ocrdma_destroy_ah, 159 .destroy_cq = ocrdma_destroy_cq, 160 .destroy_qp = ocrdma_destroy_qp, 161 .get_dev_fw_str = get_dev_fw_str, 162 .get_dma_mr = ocrdma_get_dma_mr, 163 .get_link_layer = ocrdma_link_layer, 164 .get_netdev = ocrdma_get_netdev, 165 .get_port_immutable = ocrdma_port_immutable, 166 .map_mr_sg = ocrdma_map_mr_sg, 167 .mmap = ocrdma_mmap, 168 .modify_port = ocrdma_modify_port, 169 .modify_qp = ocrdma_modify_qp, 170 .poll_cq = ocrdma_poll_cq, 171 .post_recv = ocrdma_post_recv, 172 .post_send = ocrdma_post_send, 173 .process_mad = ocrdma_process_mad, 174 .query_ah = ocrdma_query_ah, 175 .query_device = ocrdma_query_device, 176 .query_pkey = ocrdma_query_pkey, 177 .query_port = ocrdma_query_port, 178 .query_qp = ocrdma_query_qp, 179 .reg_user_mr = ocrdma_reg_user_mr, 180 .req_notify_cq = ocrdma_arm_cq, 181 .resize_cq = ocrdma_resize_cq, 182 INIT_RDMA_OBJ_SIZE(ib_pd, ocrdma_pd, ibpd), 183 INIT_RDMA_OBJ_SIZE(ib_ucontext, ocrdma_ucontext, ibucontext), 184 }; 185 186 static const struct ib_device_ops ocrdma_dev_srq_ops = { 187 .create_srq = ocrdma_create_srq, 188 .destroy_srq = ocrdma_destroy_srq, 189 .modify_srq = ocrdma_modify_srq, 190 .post_srq_recv = ocrdma_post_srq_recv, 191 .query_srq = ocrdma_query_srq, 192 }; 193 194 static int ocrdma_register_device(struct ocrdma_dev *dev) 195 { 196 ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid); 197 BUILD_BUG_ON(sizeof(OCRDMA_NODE_DESC) > IB_DEVICE_NODE_DESC_MAX); 198 memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC, 199 sizeof(OCRDMA_NODE_DESC)); 200 dev->ibdev.owner = THIS_MODULE; 201 dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION; 202 dev->ibdev.uverbs_cmd_mask = 203 OCRDMA_UVERBS(GET_CONTEXT) | 204 OCRDMA_UVERBS(QUERY_DEVICE) | 205 OCRDMA_UVERBS(QUERY_PORT) | 206 OCRDMA_UVERBS(ALLOC_PD) | 207 OCRDMA_UVERBS(DEALLOC_PD) | 208 OCRDMA_UVERBS(REG_MR) | 209 OCRDMA_UVERBS(DEREG_MR) | 210 OCRDMA_UVERBS(CREATE_COMP_CHANNEL) | 211 OCRDMA_UVERBS(CREATE_CQ) | 212 OCRDMA_UVERBS(RESIZE_CQ) | 213 OCRDMA_UVERBS(DESTROY_CQ) | 214 OCRDMA_UVERBS(REQ_NOTIFY_CQ) | 215 OCRDMA_UVERBS(CREATE_QP) | 216 OCRDMA_UVERBS(MODIFY_QP) | 217 OCRDMA_UVERBS(QUERY_QP) | 218 OCRDMA_UVERBS(DESTROY_QP) | 219 OCRDMA_UVERBS(POLL_CQ) | 220 OCRDMA_UVERBS(POST_SEND) | 221 OCRDMA_UVERBS(POST_RECV); 222 223 dev->ibdev.uverbs_cmd_mask |= 224 OCRDMA_UVERBS(CREATE_AH) | 225 OCRDMA_UVERBS(MODIFY_AH) | 226 OCRDMA_UVERBS(QUERY_AH) | 227 OCRDMA_UVERBS(DESTROY_AH); 228 229 dev->ibdev.node_type = RDMA_NODE_IB_CA; 230 dev->ibdev.phys_port_cnt = 1; 231 dev->ibdev.num_comp_vectors = dev->eq_cnt; 232 233 /* mandatory to support user space verbs consumer. */ 234 dev->ibdev.dev.parent = &dev->nic_info.pdev->dev; 235 236 ib_set_device_ops(&dev->ibdev, &ocrdma_dev_ops); 237 238 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) { 239 dev->ibdev.uverbs_cmd_mask |= 240 OCRDMA_UVERBS(CREATE_SRQ) | 241 OCRDMA_UVERBS(MODIFY_SRQ) | 242 OCRDMA_UVERBS(QUERY_SRQ) | 243 OCRDMA_UVERBS(DESTROY_SRQ) | 244 OCRDMA_UVERBS(POST_SRQ_RECV); 245 246 ib_set_device_ops(&dev->ibdev, &ocrdma_dev_srq_ops); 247 } 248 rdma_set_device_sysfs_group(&dev->ibdev, &ocrdma_attr_group); 249 dev->ibdev.driver_id = RDMA_DRIVER_OCRDMA; 250 return ib_register_device(&dev->ibdev, "ocrdma%d"); 251 } 252 253 static int ocrdma_alloc_resources(struct ocrdma_dev *dev) 254 { 255 mutex_init(&dev->dev_lock); 256 dev->cq_tbl = kcalloc(OCRDMA_MAX_CQ, sizeof(struct ocrdma_cq *), 257 GFP_KERNEL); 258 if (!dev->cq_tbl) 259 goto alloc_err; 260 261 if (dev->attr.max_qp) { 262 dev->qp_tbl = kcalloc(OCRDMA_MAX_QP, 263 sizeof(struct ocrdma_qp *), 264 GFP_KERNEL); 265 if (!dev->qp_tbl) 266 goto alloc_err; 267 } 268 269 dev->stag_arr = kcalloc(OCRDMA_MAX_STAG, sizeof(u64), GFP_KERNEL); 270 if (dev->stag_arr == NULL) 271 goto alloc_err; 272 273 ocrdma_alloc_pd_pool(dev); 274 275 if (!ocrdma_alloc_stats_resources(dev)) { 276 pr_err("%s: stats resource allocation failed\n", __func__); 277 goto alloc_err; 278 } 279 280 spin_lock_init(&dev->av_tbl.lock); 281 spin_lock_init(&dev->flush_q_lock); 282 return 0; 283 alloc_err: 284 pr_err("%s(%d) error.\n", __func__, dev->id); 285 return -ENOMEM; 286 } 287 288 static void ocrdma_free_resources(struct ocrdma_dev *dev) 289 { 290 ocrdma_release_stats_resources(dev); 291 kfree(dev->stag_arr); 292 kfree(dev->qp_tbl); 293 kfree(dev->cq_tbl); 294 } 295 296 static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info) 297 { 298 int status = 0; 299 u8 lstate = 0; 300 struct ocrdma_dev *dev; 301 302 dev = ib_alloc_device(ocrdma_dev, ibdev); 303 if (!dev) { 304 pr_err("Unable to allocate ib device\n"); 305 return NULL; 306 } 307 dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL); 308 if (!dev->mbx_cmd) 309 goto idr_err; 310 311 memcpy(&dev->nic_info, dev_info, sizeof(*dev_info)); 312 dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL); 313 if (dev->id < 0) 314 goto idr_err; 315 316 status = ocrdma_init_hw(dev); 317 if (status) 318 goto init_err; 319 320 status = ocrdma_alloc_resources(dev); 321 if (status) 322 goto alloc_err; 323 324 ocrdma_init_service_level(dev); 325 status = ocrdma_register_device(dev); 326 if (status) 327 goto alloc_err; 328 329 /* Query Link state and update */ 330 status = ocrdma_mbx_get_link_speed(dev, NULL, &lstate); 331 if (!status) 332 ocrdma_update_link_state(dev, lstate); 333 334 /* Init stats */ 335 ocrdma_add_port_stats(dev); 336 /* Interrupt Moderation */ 337 INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task); 338 schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000)); 339 340 pr_info("%s %s: %s \"%s\" port %d\n", 341 dev_name(&dev->nic_info.pdev->dev), hca_name(dev), 342 port_speed_string(dev), dev->model_number, 343 dev->hba_port_num); 344 pr_info("%s ocrdma%d driver loaded successfully\n", 345 dev_name(&dev->nic_info.pdev->dev), dev->id); 346 return dev; 347 348 alloc_err: 349 ocrdma_free_resources(dev); 350 ocrdma_cleanup_hw(dev); 351 init_err: 352 idr_remove(&ocrdma_dev_id, dev->id); 353 idr_err: 354 kfree(dev->mbx_cmd); 355 ib_dealloc_device(&dev->ibdev); 356 pr_err("%s() leaving. ret=%d\n", __func__, status); 357 return NULL; 358 } 359 360 static void ocrdma_remove_free(struct ocrdma_dev *dev) 361 { 362 363 idr_remove(&ocrdma_dev_id, dev->id); 364 kfree(dev->mbx_cmd); 365 ib_dealloc_device(&dev->ibdev); 366 } 367 368 static void ocrdma_remove(struct ocrdma_dev *dev) 369 { 370 /* first unregister with stack to stop all the active traffic 371 * of the registered clients. 372 */ 373 cancel_delayed_work_sync(&dev->eqd_work); 374 ib_unregister_device(&dev->ibdev); 375 376 ocrdma_rem_port_stats(dev); 377 ocrdma_free_resources(dev); 378 ocrdma_cleanup_hw(dev); 379 ocrdma_remove_free(dev); 380 } 381 382 static int ocrdma_dispatch_port_active(struct ocrdma_dev *dev) 383 { 384 struct ib_event port_event; 385 386 port_event.event = IB_EVENT_PORT_ACTIVE; 387 port_event.element.port_num = 1; 388 port_event.device = &dev->ibdev; 389 ib_dispatch_event(&port_event); 390 return 0; 391 } 392 393 static int ocrdma_dispatch_port_error(struct ocrdma_dev *dev) 394 { 395 struct ib_event err_event; 396 397 err_event.event = IB_EVENT_PORT_ERR; 398 err_event.element.port_num = 1; 399 err_event.device = &dev->ibdev; 400 ib_dispatch_event(&err_event); 401 return 0; 402 } 403 404 static void ocrdma_shutdown(struct ocrdma_dev *dev) 405 { 406 ocrdma_dispatch_port_error(dev); 407 ocrdma_remove(dev); 408 } 409 410 /* event handling via NIC driver ensures that all the NIC specific 411 * initialization done before RoCE driver notifies 412 * event to stack. 413 */ 414 static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event) 415 { 416 switch (event) { 417 case BE_DEV_SHUTDOWN: 418 ocrdma_shutdown(dev); 419 break; 420 default: 421 break; 422 } 423 } 424 425 void ocrdma_update_link_state(struct ocrdma_dev *dev, u8 lstate) 426 { 427 if (!(dev->flags & OCRDMA_FLAGS_LINK_STATUS_INIT)) { 428 dev->flags |= OCRDMA_FLAGS_LINK_STATUS_INIT; 429 if (!lstate) 430 return; 431 } 432 433 if (!lstate) 434 ocrdma_dispatch_port_error(dev); 435 else 436 ocrdma_dispatch_port_active(dev); 437 } 438 439 static struct ocrdma_driver ocrdma_drv = { 440 .name = "ocrdma_driver", 441 .add = ocrdma_add, 442 .remove = ocrdma_remove, 443 .state_change_handler = ocrdma_event_handler, 444 .be_abi_version = OCRDMA_BE_ROCE_ABI_VERSION, 445 }; 446 447 static int __init ocrdma_init_module(void) 448 { 449 int status; 450 451 ocrdma_init_debugfs(); 452 453 status = be_roce_register_driver(&ocrdma_drv); 454 if (status) 455 goto err_be_reg; 456 457 return 0; 458 459 err_be_reg: 460 461 return status; 462 } 463 464 static void __exit ocrdma_exit_module(void) 465 { 466 be_roce_unregister_driver(&ocrdma_drv); 467 ocrdma_rem_debugfs(); 468 idr_destroy(&ocrdma_dev_id); 469 } 470 471 module_init(ocrdma_init_module); 472 module_exit(ocrdma_exit_module); 473