Devesh Sharma | 71ee673 | 2015-07-24 05:03:59 +0530 | [diff] [blame] | 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. |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 34 | * |
| 35 | * Contact Information: |
| 36 | * linux-drivers@emulex.com |
| 37 | * |
| 38 | * Emulex |
| 39 | * 3333 Susan Street |
| 40 | * Costa Mesa, CA 92626 |
Devesh Sharma | 71ee673 | 2015-07-24 05:03:59 +0530 | [diff] [blame] | 41 | */ |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 42 | |
| 43 | #include <linux/module.h> |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 44 | #include <linux/idr.h> |
| 45 | #include <rdma/ib_verbs.h> |
| 46 | #include <rdma/ib_user_verbs.h> |
| 47 | #include <rdma/ib_addr.h> |
Ira Weiny | 337877a | 2015-06-06 14:38:29 -0400 | [diff] [blame] | 48 | #include <rdma/ib_mad.h> |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 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" |
Selvin Xavier | a51f06e | 2014-02-04 11:57:07 +0530 | [diff] [blame] | 58 | #include "ocrdma_stats.h" |
Leon Romanovsky | a7fe738 | 2016-09-22 17:31:15 +0300 | [diff] [blame] | 59 | #include <rdma/ocrdma-abi.h> |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 60 | |
Devesh Sharma | 0154410 | 2014-02-04 11:57:00 +0530 | [diff] [blame] | 61 | MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION); |
| 62 | MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 63 | MODULE_AUTHOR("Emulex Corporation"); |
Devesh Sharma | b8f5595 | 2015-07-24 05:04:00 +0530 | [diff] [blame] | 64 | MODULE_LICENSE("Dual BSD/GPL"); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 65 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 66 | static DEFINE_IDR(ocrdma_dev_id); |
| 67 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 68 | void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid) |
| 69 | { |
| 70 | u8 mac_addr[6]; |
| 71 | |
| 72 | memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN); |
| 73 | guid[0] = mac_addr[0] ^ 2; |
| 74 | guid[1] = mac_addr[1]; |
| 75 | guid[2] = mac_addr[2]; |
| 76 | guid[3] = 0xff; |
| 77 | guid[4] = 0xfe; |
| 78 | guid[5] = mac_addr[3]; |
| 79 | guid[6] = mac_addr[4]; |
| 80 | guid[7] = mac_addr[5]; |
| 81 | } |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 82 | static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device, |
| 83 | u8 port_num) |
| 84 | { |
| 85 | return IB_LINK_LAYER_ETHERNET; |
| 86 | } |
| 87 | |
Ira Weiny | 7738613 | 2015-05-13 20:02:58 -0400 | [diff] [blame] | 88 | static int ocrdma_port_immutable(struct ib_device *ibdev, u8 port_num, |
| 89 | struct ib_port_immutable *immutable) |
| 90 | { |
| 91 | struct ib_port_attr attr; |
Somnath Kotur | e161486 | 2016-01-28 08:59:56 -0500 | [diff] [blame] | 92 | struct ocrdma_dev *dev; |
Ira Weiny | 7738613 | 2015-05-13 20:02:58 -0400 | [diff] [blame] | 93 | int err; |
| 94 | |
Somnath Kotur | e161486 | 2016-01-28 08:59:56 -0500 | [diff] [blame] | 95 | dev = get_ocrdma_dev(ibdev); |
Or Gerlitz | c4550c6 | 2017-01-24 13:02:39 +0200 | [diff] [blame] | 96 | immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE; |
| 97 | if (ocrdma_is_udp_encap_supported(dev)) |
| 98 | immutable->core_cap_flags |= RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP; |
| 99 | |
| 100 | err = ib_query_port(ibdev, port_num, &attr); |
Ira Weiny | 7738613 | 2015-05-13 20:02:58 -0400 | [diff] [blame] | 101 | if (err) |
| 102 | return err; |
| 103 | |
| 104 | immutable->pkey_tbl_len = attr.pkey_tbl_len; |
| 105 | immutable->gid_tbl_len = attr.gid_tbl_len; |
Ira Weiny | 337877a | 2015-06-06 14:38:29 -0400 | [diff] [blame] | 106 | immutable->max_mad_size = IB_MGMT_MAD_SIZE; |
Ira Weiny | 7738613 | 2015-05-13 20:02:58 -0400 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Ira Weiny | bd39500 | 2016-06-15 02:22:04 -0400 | [diff] [blame] | 111 | static void get_dev_fw_str(struct ib_device *device, char *str, |
| 112 | size_t str_len) |
| 113 | { |
| 114 | struct ocrdma_dev *dev = get_ocrdma_dev(device); |
| 115 | |
| 116 | snprintf(str, str_len, "%s", &dev->attr.fw_ver[0]); |
| 117 | } |
| 118 | |
Roland Dreier | abe3afa | 2012-04-16 11:36:29 -0700 | [diff] [blame] | 119 | static int ocrdma_register_device(struct ocrdma_dev *dev) |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 120 | { |
| 121 | strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX); |
| 122 | ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid); |
Yuval Shaia | bd99fde | 2016-08-25 10:57:07 -0700 | [diff] [blame] | 123 | BUILD_BUG_ON(sizeof(OCRDMA_NODE_DESC) > IB_DEVICE_NODE_DESC_MAX); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 124 | memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC, |
| 125 | sizeof(OCRDMA_NODE_DESC)); |
| 126 | dev->ibdev.owner = THIS_MODULE; |
Naresh Gottumukkala | 3875439 | 2013-08-26 15:27:49 +0530 | [diff] [blame] | 127 | dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 128 | dev->ibdev.uverbs_cmd_mask = |
| 129 | OCRDMA_UVERBS(GET_CONTEXT) | |
| 130 | OCRDMA_UVERBS(QUERY_DEVICE) | |
| 131 | OCRDMA_UVERBS(QUERY_PORT) | |
| 132 | OCRDMA_UVERBS(ALLOC_PD) | |
| 133 | OCRDMA_UVERBS(DEALLOC_PD) | |
| 134 | OCRDMA_UVERBS(REG_MR) | |
| 135 | OCRDMA_UVERBS(DEREG_MR) | |
| 136 | OCRDMA_UVERBS(CREATE_COMP_CHANNEL) | |
| 137 | OCRDMA_UVERBS(CREATE_CQ) | |
| 138 | OCRDMA_UVERBS(RESIZE_CQ) | |
| 139 | OCRDMA_UVERBS(DESTROY_CQ) | |
| 140 | OCRDMA_UVERBS(REQ_NOTIFY_CQ) | |
| 141 | OCRDMA_UVERBS(CREATE_QP) | |
| 142 | OCRDMA_UVERBS(MODIFY_QP) | |
| 143 | OCRDMA_UVERBS(QUERY_QP) | |
| 144 | OCRDMA_UVERBS(DESTROY_QP) | |
| 145 | OCRDMA_UVERBS(POLL_CQ) | |
| 146 | OCRDMA_UVERBS(POST_SEND) | |
| 147 | OCRDMA_UVERBS(POST_RECV); |
| 148 | |
| 149 | dev->ibdev.uverbs_cmd_mask |= |
| 150 | OCRDMA_UVERBS(CREATE_AH) | |
| 151 | OCRDMA_UVERBS(MODIFY_AH) | |
| 152 | OCRDMA_UVERBS(QUERY_AH) | |
| 153 | OCRDMA_UVERBS(DESTROY_AH); |
| 154 | |
| 155 | dev->ibdev.node_type = RDMA_NODE_IB_CA; |
| 156 | dev->ibdev.phys_port_cnt = 1; |
Devesh Sharma | 0c0eacd | 2014-12-18 14:12:58 +0530 | [diff] [blame] | 157 | dev->ibdev.num_comp_vectors = dev->eq_cnt; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 158 | |
| 159 | /* mandatory verbs. */ |
| 160 | dev->ibdev.query_device = ocrdma_query_device; |
| 161 | dev->ibdev.query_port = ocrdma_query_port; |
| 162 | dev->ibdev.modify_port = ocrdma_modify_port; |
| 163 | dev->ibdev.query_gid = ocrdma_query_gid; |
Somnath Kotur | cc36929 | 2015-07-30 18:33:31 +0300 | [diff] [blame] | 164 | dev->ibdev.get_netdev = ocrdma_get_netdev; |
| 165 | dev->ibdev.add_gid = ocrdma_add_gid; |
| 166 | dev->ibdev.del_gid = ocrdma_del_gid; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 167 | dev->ibdev.get_link_layer = ocrdma_link_layer; |
| 168 | dev->ibdev.alloc_pd = ocrdma_alloc_pd; |
| 169 | dev->ibdev.dealloc_pd = ocrdma_dealloc_pd; |
| 170 | |
| 171 | dev->ibdev.create_cq = ocrdma_create_cq; |
| 172 | dev->ibdev.destroy_cq = ocrdma_destroy_cq; |
| 173 | dev->ibdev.resize_cq = ocrdma_resize_cq; |
| 174 | |
| 175 | dev->ibdev.create_qp = ocrdma_create_qp; |
| 176 | dev->ibdev.modify_qp = ocrdma_modify_qp; |
| 177 | dev->ibdev.query_qp = ocrdma_query_qp; |
| 178 | dev->ibdev.destroy_qp = ocrdma_destroy_qp; |
| 179 | |
| 180 | dev->ibdev.query_pkey = ocrdma_query_pkey; |
| 181 | dev->ibdev.create_ah = ocrdma_create_ah; |
| 182 | dev->ibdev.destroy_ah = ocrdma_destroy_ah; |
| 183 | dev->ibdev.query_ah = ocrdma_query_ah; |
| 184 | dev->ibdev.modify_ah = ocrdma_modify_ah; |
| 185 | |
| 186 | dev->ibdev.poll_cq = ocrdma_poll_cq; |
| 187 | dev->ibdev.post_send = ocrdma_post_send; |
| 188 | dev->ibdev.post_recv = ocrdma_post_recv; |
| 189 | dev->ibdev.req_notify_cq = ocrdma_arm_cq; |
| 190 | |
| 191 | dev->ibdev.get_dma_mr = ocrdma_get_dma_mr; |
| 192 | dev->ibdev.dereg_mr = ocrdma_dereg_mr; |
| 193 | dev->ibdev.reg_user_mr = ocrdma_reg_user_mr; |
| 194 | |
Sagi Grimberg | cacb7d5 | 2015-07-30 10:32:43 +0300 | [diff] [blame] | 195 | dev->ibdev.alloc_mr = ocrdma_alloc_mr; |
Sagi Grimberg | 2eaa1c5 | 2015-10-13 19:11:28 +0300 | [diff] [blame] | 196 | dev->ibdev.map_mr_sg = ocrdma_map_mr_sg; |
Naresh Gottumukkala | 7c33880 | 2013-08-26 15:27:39 +0530 | [diff] [blame] | 197 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 198 | /* mandatory to support user space verbs consumer. */ |
| 199 | dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext; |
| 200 | dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext; |
| 201 | dev->ibdev.mmap = ocrdma_mmap; |
Bart Van Assche | e6a73f2 | 2017-01-20 13:04:24 -0800 | [diff] [blame] | 202 | dev->ibdev.dev.parent = &dev->nic_info.pdev->dev; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 203 | |
| 204 | dev->ibdev.process_mad = ocrdma_process_mad; |
Ira Weiny | 7738613 | 2015-05-13 20:02:58 -0400 | [diff] [blame] | 205 | dev->ibdev.get_port_immutable = ocrdma_port_immutable; |
Ira Weiny | bd39500 | 2016-06-15 02:22:04 -0400 | [diff] [blame] | 206 | dev->ibdev.get_dev_fw_str = get_dev_fw_str; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 207 | |
Devesh Sharma | 21c3391 | 2014-02-04 11:56:56 +0530 | [diff] [blame] | 208 | if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) { |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 209 | dev->ibdev.uverbs_cmd_mask |= |
| 210 | OCRDMA_UVERBS(CREATE_SRQ) | |
| 211 | OCRDMA_UVERBS(MODIFY_SRQ) | |
| 212 | OCRDMA_UVERBS(QUERY_SRQ) | |
| 213 | OCRDMA_UVERBS(DESTROY_SRQ) | |
| 214 | OCRDMA_UVERBS(POST_SRQ_RECV); |
| 215 | |
| 216 | dev->ibdev.create_srq = ocrdma_create_srq; |
| 217 | dev->ibdev.modify_srq = ocrdma_modify_srq; |
| 218 | dev->ibdev.query_srq = ocrdma_query_srq; |
| 219 | dev->ibdev.destroy_srq = ocrdma_destroy_srq; |
| 220 | dev->ibdev.post_srq_recv = ocrdma_post_srq_recv; |
| 221 | } |
| 222 | return ib_register_device(&dev->ibdev, NULL); |
| 223 | } |
| 224 | |
| 225 | static int ocrdma_alloc_resources(struct ocrdma_dev *dev) |
| 226 | { |
| 227 | mutex_init(&dev->dev_lock); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 228 | dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) * |
| 229 | OCRDMA_MAX_CQ, GFP_KERNEL); |
| 230 | if (!dev->cq_tbl) |
| 231 | goto alloc_err; |
| 232 | |
| 233 | if (dev->attr.max_qp) { |
| 234 | dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) * |
| 235 | OCRDMA_MAX_QP, GFP_KERNEL); |
| 236 | if (!dev->qp_tbl) |
| 237 | goto alloc_err; |
| 238 | } |
Selvin Xavier | 4f1df84 | 2014-06-10 19:32:24 +0530 | [diff] [blame] | 239 | |
| 240 | dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL); |
| 241 | if (dev->stag_arr == NULL) |
| 242 | goto alloc_err; |
| 243 | |
Mitesh Ahuja | 9ba1377 | 2014-12-18 14:12:57 +0530 | [diff] [blame] | 244 | ocrdma_alloc_pd_pool(dev); |
| 245 | |
Selvin Xavier | fd98d89 | 2016-02-05 20:06:37 +0530 | [diff] [blame] | 246 | if (!ocrdma_alloc_stats_resources(dev)) { |
| 247 | pr_err("%s: stats resource allocation failed\n", __func__); |
| 248 | goto alloc_err; |
| 249 | } |
| 250 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 251 | spin_lock_init(&dev->av_tbl.lock); |
| 252 | spin_lock_init(&dev->flush_q_lock); |
| 253 | return 0; |
| 254 | alloc_err: |
Naresh Gottumukkala | ef99c4c | 2013-06-10 04:42:39 +0000 | [diff] [blame] | 255 | pr_err("%s(%d) error.\n", __func__, dev->id); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 256 | return -ENOMEM; |
| 257 | } |
| 258 | |
| 259 | static void ocrdma_free_resources(struct ocrdma_dev *dev) |
| 260 | { |
Selvin Xavier | fd98d89 | 2016-02-05 20:06:37 +0530 | [diff] [blame] | 261 | ocrdma_release_stats_resources(dev); |
Selvin Xavier | 4f1df84 | 2014-06-10 19:32:24 +0530 | [diff] [blame] | 262 | kfree(dev->stag_arr); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 263 | kfree(dev->qp_tbl); |
| 264 | kfree(dev->cq_tbl); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 265 | } |
| 266 | |
Selvin Xavier | 334b8db | 2014-02-04 11:57:09 +0530 | [diff] [blame] | 267 | /* OCRDMA sysfs interface */ |
| 268 | static ssize_t show_rev(struct device *device, struct device_attribute *attr, |
| 269 | char *buf) |
| 270 | { |
| 271 | struct ocrdma_dev *dev = dev_get_drvdata(device); |
| 272 | |
| 273 | return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor); |
| 274 | } |
| 275 | |
Selvin Xavier | 4808b18 | 2014-06-10 19:32:14 +0530 | [diff] [blame] | 276 | static ssize_t show_hca_type(struct device *device, |
| 277 | struct device_attribute *attr, char *buf) |
| 278 | { |
| 279 | struct ocrdma_dev *dev = dev_get_drvdata(device); |
| 280 | |
| 281 | return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]); |
Selvin Xavier | 334b8db | 2014-02-04 11:57:09 +0530 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); |
Selvin Xavier | 4808b18 | 2014-06-10 19:32:14 +0530 | [diff] [blame] | 285 | static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL); |
Selvin Xavier | 334b8db | 2014-02-04 11:57:09 +0530 | [diff] [blame] | 286 | |
| 287 | static struct device_attribute *ocrdma_attributes[] = { |
| 288 | &dev_attr_hw_rev, |
Selvin Xavier | 4808b18 | 2014-06-10 19:32:14 +0530 | [diff] [blame] | 289 | &dev_attr_hca_type |
Selvin Xavier | 334b8db | 2014-02-04 11:57:09 +0530 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | static void ocrdma_remove_sysfiles(struct ocrdma_dev *dev) |
| 293 | { |
| 294 | int i; |
| 295 | |
| 296 | for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++) |
| 297 | device_remove_file(&dev->ibdev.dev, ocrdma_attributes[i]); |
| 298 | } |
| 299 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 300 | static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info) |
| 301 | { |
Selvin Xavier | 334b8db | 2014-02-04 11:57:09 +0530 | [diff] [blame] | 302 | int status = 0, i; |
Devesh Sharma | 3b1ea43 | 2015-12-24 13:14:07 -0500 | [diff] [blame] | 303 | u8 lstate = 0; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 304 | struct ocrdma_dev *dev; |
| 305 | |
| 306 | dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev)); |
| 307 | if (!dev) { |
Naresh Gottumukkala | ef99c4c | 2013-06-10 04:42:39 +0000 | [diff] [blame] | 308 | pr_err("Unable to allocate ib device\n"); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 309 | return NULL; |
| 310 | } |
| 311 | dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL); |
| 312 | if (!dev->mbx_cmd) |
| 313 | goto idr_err; |
| 314 | |
| 315 | memcpy(&dev->nic_info, dev_info, sizeof(*dev_info)); |
Tejun Heo | cffcd59 | 2013-02-27 17:04:24 -0800 | [diff] [blame] | 316 | dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 317 | if (dev->id < 0) |
| 318 | goto idr_err; |
| 319 | |
| 320 | status = ocrdma_init_hw(dev); |
| 321 | if (status) |
| 322 | goto init_err; |
| 323 | |
| 324 | status = ocrdma_alloc_resources(dev); |
| 325 | if (status) |
| 326 | goto alloc_err; |
| 327 | |
Selvin Xavier | 31dbdd9 | 2014-06-10 19:32:13 +0530 | [diff] [blame] | 328 | ocrdma_init_service_level(dev); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 329 | status = ocrdma_register_device(dev); |
| 330 | if (status) |
| 331 | goto alloc_err; |
| 332 | |
Devesh Sharma | 3b1ea43 | 2015-12-24 13:14:07 -0500 | [diff] [blame] | 333 | /* Query Link state and update */ |
| 334 | status = ocrdma_mbx_get_link_speed(dev, NULL, &lstate); |
| 335 | if (!status) |
| 336 | ocrdma_update_link_state(dev, lstate); |
| 337 | |
Selvin Xavier | 334b8db | 2014-02-04 11:57:09 +0530 | [diff] [blame] | 338 | for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++) |
| 339 | if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i])) |
| 340 | goto sysfs_err; |
Selvin Xavier | a51f06e | 2014-02-04 11:57:07 +0530 | [diff] [blame] | 341 | /* Init stats */ |
| 342 | ocrdma_add_port_stats(dev); |
Mitesh Ahuja | b4dbe8d | 2014-12-18 14:13:05 +0530 | [diff] [blame] | 343 | /* Interrupt Moderation */ |
| 344 | INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task); |
| 345 | schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000)); |
Selvin Xavier | a51f06e | 2014-02-04 11:57:07 +0530 | [diff] [blame] | 346 | |
| 347 | pr_info("%s %s: %s \"%s\" port %d\n", |
| 348 | dev_name(&dev->nic_info.pdev->dev), hca_name(dev), |
| 349 | port_speed_string(dev), dev->model_number, |
| 350 | dev->hba_port_num); |
| 351 | pr_info("%s ocrdma%d driver loaded successfully\n", |
| 352 | dev_name(&dev->nic_info.pdev->dev), dev->id); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 353 | return dev; |
| 354 | |
Selvin Xavier | 334b8db | 2014-02-04 11:57:09 +0530 | [diff] [blame] | 355 | sysfs_err: |
| 356 | ocrdma_remove_sysfiles(dev); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 357 | alloc_err: |
| 358 | ocrdma_free_resources(dev); |
| 359 | ocrdma_cleanup_hw(dev); |
| 360 | init_err: |
| 361 | idr_remove(&ocrdma_dev_id, dev->id); |
| 362 | idr_err: |
| 363 | kfree(dev->mbx_cmd); |
| 364 | ib_dealloc_device(&dev->ibdev); |
Naresh Gottumukkala | ef99c4c | 2013-06-10 04:42:39 +0000 | [diff] [blame] | 365 | pr_err("%s() leaving. ret=%d\n", __func__, status); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 366 | return NULL; |
| 367 | } |
| 368 | |
Selvin Xavier | 5a85f5e | 2015-10-20 14:17:56 +0530 | [diff] [blame] | 369 | static void ocrdma_remove_free(struct ocrdma_dev *dev) |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 370 | { |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 371 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 372 | idr_remove(&ocrdma_dev_id, dev->id); |
| 373 | kfree(dev->mbx_cmd); |
| 374 | ib_dealloc_device(&dev->ibdev); |
| 375 | } |
| 376 | |
Sasha Levin | 3e4d60a | 2012-04-28 07:40:01 +0200 | [diff] [blame] | 377 | static void ocrdma_remove(struct ocrdma_dev *dev) |
| 378 | { |
| 379 | /* first unregister with stack to stop all the active traffic |
| 380 | * of the registered clients. |
| 381 | */ |
Mitesh Ahuja | b4dbe8d | 2014-12-18 14:13:05 +0530 | [diff] [blame] | 382 | cancel_delayed_work_sync(&dev->eqd_work); |
Selvin Xavier | 334b8db | 2014-02-04 11:57:09 +0530 | [diff] [blame] | 383 | ocrdma_remove_sysfiles(dev); |
Sasha Levin | 3e4d60a | 2012-04-28 07:40:01 +0200 | [diff] [blame] | 384 | ib_unregister_device(&dev->ibdev); |
| 385 | |
Mitesh Ahuja | 4b8180a | 2014-12-18 14:13:01 +0530 | [diff] [blame] | 386 | ocrdma_rem_port_stats(dev); |
Naresh Gottumukkala | 1852d1d | 2013-09-06 15:02:47 +0530 | [diff] [blame] | 387 | ocrdma_free_resources(dev); |
| 388 | ocrdma_cleanup_hw(dev); |
Selvin Xavier | 5a85f5e | 2015-10-20 14:17:56 +0530 | [diff] [blame] | 389 | ocrdma_remove_free(dev); |
Sasha Levin | 3e4d60a | 2012-04-28 07:40:01 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Devesh Sharma | 3b1ea43 | 2015-12-24 13:14:07 -0500 | [diff] [blame] | 392 | static int ocrdma_dispatch_port_active(struct ocrdma_dev *dev) |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 393 | { |
| 394 | struct ib_event port_event; |
| 395 | |
| 396 | port_event.event = IB_EVENT_PORT_ACTIVE; |
| 397 | port_event.element.port_num = 1; |
| 398 | port_event.device = &dev->ibdev; |
| 399 | ib_dispatch_event(&port_event); |
| 400 | return 0; |
| 401 | } |
| 402 | |
Devesh Sharma | 3b1ea43 | 2015-12-24 13:14:07 -0500 | [diff] [blame] | 403 | static int ocrdma_dispatch_port_error(struct ocrdma_dev *dev) |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 404 | { |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 405 | struct ib_event err_event; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 406 | |
| 407 | err_event.event = IB_EVENT_PORT_ERR; |
| 408 | err_event.element.port_num = 1; |
| 409 | err_event.device = &dev->ibdev; |
| 410 | ib_dispatch_event(&err_event); |
| 411 | return 0; |
| 412 | } |
| 413 | |
Devesh Sharma | efe4593 | 2014-06-10 19:32:16 +0530 | [diff] [blame] | 414 | static void ocrdma_shutdown(struct ocrdma_dev *dev) |
| 415 | { |
Devesh Sharma | 3b1ea43 | 2015-12-24 13:14:07 -0500 | [diff] [blame] | 416 | ocrdma_dispatch_port_error(dev); |
Devesh Sharma | efe4593 | 2014-06-10 19:32:16 +0530 | [diff] [blame] | 417 | ocrdma_remove(dev); |
| 418 | } |
| 419 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 420 | /* event handling via NIC driver ensures that all the NIC specific |
| 421 | * initialization done before RoCE driver notifies |
| 422 | * event to stack. |
| 423 | */ |
| 424 | static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event) |
| 425 | { |
| 426 | switch (event) { |
Devesh Sharma | efe4593 | 2014-06-10 19:32:16 +0530 | [diff] [blame] | 427 | case BE_DEV_SHUTDOWN: |
| 428 | ocrdma_shutdown(dev); |
| 429 | break; |
Devesh Sharma | 3b1ea43 | 2015-12-24 13:14:07 -0500 | [diff] [blame] | 430 | default: |
| 431 | break; |
Joe Perches | 2b50176d | 2013-10-08 16:07:22 -0700 | [diff] [blame] | 432 | } |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 433 | } |
| 434 | |
Devesh Sharma | 3b1ea43 | 2015-12-24 13:14:07 -0500 | [diff] [blame] | 435 | void ocrdma_update_link_state(struct ocrdma_dev *dev, u8 lstate) |
| 436 | { |
| 437 | if (!(dev->flags & OCRDMA_FLAGS_LINK_STATUS_INIT)) { |
| 438 | dev->flags |= OCRDMA_FLAGS_LINK_STATUS_INIT; |
| 439 | if (!lstate) |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | if (!lstate) |
| 444 | ocrdma_dispatch_port_error(dev); |
| 445 | else |
| 446 | ocrdma_dispatch_port_active(dev); |
| 447 | } |
| 448 | |
Roland Dreier | abe3afa | 2012-04-16 11:36:29 -0700 | [diff] [blame] | 449 | static struct ocrdma_driver ocrdma_drv = { |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 450 | .name = "ocrdma_driver", |
| 451 | .add = ocrdma_add, |
| 452 | .remove = ocrdma_remove, |
| 453 | .state_change_handler = ocrdma_event_handler, |
Devesh Sharma | b6b87d2 | 2014-02-04 11:56:58 +0530 | [diff] [blame] | 454 | .be_abi_version = OCRDMA_BE_ROCE_ABI_VERSION, |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 455 | }; |
| 456 | |
| 457 | static int __init ocrdma_init_module(void) |
| 458 | { |
| 459 | int status; |
| 460 | |
Selvin Xavier | a51f06e | 2014-02-04 11:57:07 +0530 | [diff] [blame] | 461 | ocrdma_init_debugfs(); |
| 462 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 463 | status = be_roce_register_driver(&ocrdma_drv); |
| 464 | if (status) |
Selvin Xavier | 2d8f57d | 2014-03-18 14:54:56 +0530 | [diff] [blame] | 465 | goto err_be_reg; |
Roland Dreier | 3495566 | 2012-05-02 15:42:33 -0700 | [diff] [blame] | 466 | |
Selvin Xavier | 2d8f57d | 2014-03-18 14:54:56 +0530 | [diff] [blame] | 467 | return 0; |
| 468 | |
| 469 | err_be_reg: |
Somnath Kotur | cc36929 | 2015-07-30 18:33:31 +0300 | [diff] [blame] | 470 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 471 | return status; |
| 472 | } |
| 473 | |
| 474 | static void __exit ocrdma_exit_module(void) |
| 475 | { |
| 476 | be_roce_unregister_driver(&ocrdma_drv); |
Selvin Xavier | a51f06e | 2014-02-04 11:57:07 +0530 | [diff] [blame] | 477 | ocrdma_rem_debugfs(); |
Johannes Thumshirn | d8b2ba7 | 2015-07-08 17:23:00 +0200 | [diff] [blame] | 478 | idr_destroy(&ocrdma_dev_id); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | module_init(ocrdma_init_module); |
| 482 | module_exit(ocrdma_exit_module); |