Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1 | /******************************************************************* |
| 2 | * This file is part of the Emulex RoCE Device Driver for * |
| 3 | * RoCE (RDMA over Converged Ethernet) adapters. * |
| 4 | * Copyright (C) 2008-2012 Emulex. All rights reserved. * |
| 5 | * EMULEX and SLI are trademarks of Emulex. * |
| 6 | * www.emulex.com * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or * |
| 9 | * modify it under the terms of version 2 of the GNU General * |
| 10 | * Public License as published by the Free Software Foundation. * |
| 11 | * This program is distributed in the hope that it will be useful. * |
| 12 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * |
| 13 | * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE * |
| 15 | * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD * |
| 16 | * TO BE LEGALLY INVALID. See the GNU General Public License for * |
| 17 | * more details, a copy of which can be found in the file COPYING * |
| 18 | * included with this package. * |
| 19 | * |
| 20 | * Contact Information: |
| 21 | * linux-drivers@emulex.com |
| 22 | * |
| 23 | * Emulex |
| 24 | * 3333 Susan Street |
| 25 | * Costa Mesa, CA 92626 |
| 26 | *******************************************************************/ |
| 27 | |
| 28 | #include <linux/module.h> |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 29 | #include <linux/idr.h> |
| 30 | #include <rdma/ib_verbs.h> |
| 31 | #include <rdma/ib_user_verbs.h> |
| 32 | #include <rdma/ib_addr.h> |
| 33 | |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <net/addrconf.h> |
| 36 | |
| 37 | #include "ocrdma.h" |
| 38 | #include "ocrdma_verbs.h" |
| 39 | #include "ocrdma_ah.h" |
| 40 | #include "be_roce.h" |
| 41 | #include "ocrdma_hw.h" |
| 42 | |
| 43 | MODULE_VERSION(OCRDMA_ROCE_DEV_VERSION); |
| 44 | MODULE_DESCRIPTION("Emulex RoCE HCA Driver"); |
| 45 | MODULE_AUTHOR("Emulex Corporation"); |
| 46 | MODULE_LICENSE("GPL"); |
| 47 | |
| 48 | static LIST_HEAD(ocrdma_dev_list); |
Sasha Levin | 3e4d60a | 2012-04-28 07:40:01 +0200 | [diff] [blame] | 49 | static DEFINE_SPINLOCK(ocrdma_devlist_lock); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 50 | static DEFINE_IDR(ocrdma_dev_id); |
| 51 | |
| 52 | static union ib_gid ocrdma_zero_sgid; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 53 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 54 | void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid) |
| 55 | { |
| 56 | u8 mac_addr[6]; |
| 57 | |
| 58 | memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN); |
| 59 | guid[0] = mac_addr[0] ^ 2; |
| 60 | guid[1] = mac_addr[1]; |
| 61 | guid[2] = mac_addr[2]; |
| 62 | guid[3] = 0xff; |
| 63 | guid[4] = 0xfe; |
| 64 | guid[5] = mac_addr[3]; |
| 65 | guid[6] = mac_addr[4]; |
| 66 | guid[7] = mac_addr[5]; |
| 67 | } |
| 68 | |
| 69 | static void ocrdma_build_sgid_mac(union ib_gid *sgid, unsigned char *mac_addr, |
| 70 | bool is_vlan, u16 vlan_id) |
| 71 | { |
| 72 | sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL); |
| 73 | sgid->raw[8] = mac_addr[0] ^ 2; |
| 74 | sgid->raw[9] = mac_addr[1]; |
| 75 | sgid->raw[10] = mac_addr[2]; |
| 76 | if (is_vlan) { |
| 77 | sgid->raw[11] = vlan_id >> 8; |
| 78 | sgid->raw[12] = vlan_id & 0xff; |
| 79 | } else { |
| 80 | sgid->raw[11] = 0xff; |
| 81 | sgid->raw[12] = 0xfe; |
| 82 | } |
| 83 | sgid->raw[13] = mac_addr[3]; |
| 84 | sgid->raw[14] = mac_addr[4]; |
| 85 | sgid->raw[15] = mac_addr[5]; |
| 86 | } |
| 87 | |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 88 | static bool ocrdma_add_sgid(struct ocrdma_dev *dev, unsigned char *mac_addr, |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 89 | bool is_vlan, u16 vlan_id) |
| 90 | { |
| 91 | int i; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 92 | union ib_gid new_sgid; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 93 | unsigned long flags; |
| 94 | |
| 95 | memset(&ocrdma_zero_sgid, 0, sizeof(union ib_gid)); |
| 96 | |
| 97 | ocrdma_build_sgid_mac(&new_sgid, mac_addr, is_vlan, vlan_id); |
| 98 | |
| 99 | spin_lock_irqsave(&dev->sgid_lock, flags); |
| 100 | for (i = 0; i < OCRDMA_MAX_SGID; i++) { |
| 101 | if (!memcmp(&dev->sgid_tbl[i], &ocrdma_zero_sgid, |
| 102 | sizeof(union ib_gid))) { |
| 103 | /* found free entry */ |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 104 | memcpy(&dev->sgid_tbl[i], &new_sgid, |
| 105 | sizeof(union ib_gid)); |
| 106 | spin_unlock_irqrestore(&dev->sgid_lock, flags); |
| 107 | return true; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 108 | } else if (!memcmp(&dev->sgid_tbl[i], &new_sgid, |
| 109 | sizeof(union ib_gid))) { |
| 110 | /* entry already present, no addition is required. */ |
| 111 | spin_unlock_irqrestore(&dev->sgid_lock, flags); |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 112 | return false; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 113 | } |
| 114 | } |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 115 | spin_unlock_irqrestore(&dev->sgid_lock, flags); |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 116 | return false; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static bool ocrdma_del_sgid(struct ocrdma_dev *dev, unsigned char *mac_addr, |
| 120 | bool is_vlan, u16 vlan_id) |
| 121 | { |
| 122 | int found = false; |
| 123 | int i; |
| 124 | union ib_gid sgid; |
| 125 | unsigned long flags; |
| 126 | |
| 127 | ocrdma_build_sgid_mac(&sgid, mac_addr, is_vlan, vlan_id); |
| 128 | |
| 129 | spin_lock_irqsave(&dev->sgid_lock, flags); |
| 130 | /* first is default sgid, which cannot be deleted. */ |
| 131 | for (i = 1; i < OCRDMA_MAX_SGID; i++) { |
| 132 | if (!memcmp(&dev->sgid_tbl[i], &sgid, sizeof(union ib_gid))) { |
| 133 | /* found matching entry */ |
| 134 | memset(&dev->sgid_tbl[i], 0, sizeof(union ib_gid)); |
| 135 | found = true; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | spin_unlock_irqrestore(&dev->sgid_lock, flags); |
| 140 | return found; |
| 141 | } |
| 142 | |
| 143 | static void ocrdma_add_default_sgid(struct ocrdma_dev *dev) |
| 144 | { |
| 145 | /* GID Index 0 - Invariant manufacturer-assigned EUI-64 */ |
| 146 | union ib_gid *sgid = &dev->sgid_tbl[0]; |
| 147 | |
| 148 | sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL); |
| 149 | ocrdma_get_guid(dev, &sgid->raw[8]); |
| 150 | } |
| 151 | |
Roland Dreier | d549f55 | 2012-08-10 16:52:13 -0700 | [diff] [blame] | 152 | #if IS_ENABLED(CONFIG_VLAN_8021Q) |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 153 | static void ocrdma_add_vlan_sgids(struct ocrdma_dev *dev) |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 154 | { |
| 155 | struct net_device *netdev, *tmp; |
| 156 | u16 vlan_id; |
| 157 | bool is_vlan; |
| 158 | |
| 159 | netdev = dev->nic_info.netdev; |
| 160 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 161 | rcu_read_lock(); |
| 162 | for_each_netdev_rcu(&init_net, tmp) { |
| 163 | if (netdev == tmp || vlan_dev_real_dev(tmp) == netdev) { |
| 164 | if (!netif_running(tmp) || !netif_oper_up(tmp)) |
| 165 | continue; |
| 166 | if (netdev != tmp) { |
| 167 | vlan_id = vlan_dev_vlan_id(tmp); |
| 168 | is_vlan = true; |
| 169 | } else { |
| 170 | is_vlan = false; |
| 171 | vlan_id = 0; |
| 172 | tmp = netdev; |
| 173 | } |
| 174 | ocrdma_add_sgid(dev, tmp->dev_addr, is_vlan, vlan_id); |
| 175 | } |
| 176 | } |
| 177 | rcu_read_unlock(); |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 178 | } |
| 179 | #else |
| 180 | static void ocrdma_add_vlan_sgids(struct ocrdma_dev *dev) |
| 181 | { |
| 182 | |
| 183 | } |
| 184 | #endif /* VLAN */ |
| 185 | |
| 186 | static int ocrdma_build_sgid_tbl(struct ocrdma_dev *dev) |
| 187 | { |
| 188 | ocrdma_add_default_sgid(dev); |
| 189 | ocrdma_add_vlan_sgids(dev); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
Roland Dreier | d549f55 | 2012-08-10 16:52:13 -0700 | [diff] [blame] | 193 | #if IS_ENABLED(CONFIG_IPV6) |
Roland Dreier | 3495566 | 2012-05-02 15:42:33 -0700 | [diff] [blame] | 194 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 195 | static int ocrdma_inet6addr_event(struct notifier_block *notifier, |
| 196 | unsigned long event, void *ptr) |
| 197 | { |
| 198 | struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr; |
Roland Dreier | d549f55 | 2012-08-10 16:52:13 -0700 | [diff] [blame] | 199 | struct net_device *netdev = ifa->idev->dev; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 200 | struct ib_event gid_event; |
| 201 | struct ocrdma_dev *dev; |
| 202 | bool found = false; |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 203 | bool updated = false; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 204 | bool is_vlan = false; |
| 205 | u16 vid = 0; |
| 206 | |
Roland Dreier | d549f55 | 2012-08-10 16:52:13 -0700 | [diff] [blame] | 207 | is_vlan = netdev->priv_flags & IFF_802_1Q_VLAN; |
| 208 | if (is_vlan) { |
| 209 | vid = vlan_dev_vlan_id(netdev); |
| 210 | netdev = vlan_dev_real_dev(netdev); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 211 | } |
Roland Dreier | d549f55 | 2012-08-10 16:52:13 -0700 | [diff] [blame] | 212 | |
Sasha Levin | 3e4d60a | 2012-04-28 07:40:01 +0200 | [diff] [blame] | 213 | rcu_read_lock(); |
| 214 | list_for_each_entry_rcu(dev, &ocrdma_dev_list, entry) { |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 215 | if (dev->nic_info.netdev == netdev) { |
| 216 | found = true; |
| 217 | break; |
| 218 | } |
| 219 | } |
Sasha Levin | 3e4d60a | 2012-04-28 07:40:01 +0200 | [diff] [blame] | 220 | rcu_read_unlock(); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 221 | |
| 222 | if (!found) |
| 223 | return NOTIFY_DONE; |
| 224 | if (!rdma_link_local_addr((struct in6_addr *)&ifa->addr)) |
| 225 | return NOTIFY_DONE; |
| 226 | |
| 227 | mutex_lock(&dev->dev_lock); |
| 228 | switch (event) { |
| 229 | case NETDEV_UP: |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 230 | updated = ocrdma_add_sgid(dev, netdev->dev_addr, is_vlan, vid); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 231 | break; |
| 232 | case NETDEV_DOWN: |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 233 | updated = ocrdma_del_sgid(dev, netdev->dev_addr, is_vlan, vid); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 234 | break; |
| 235 | default: |
| 236 | break; |
| 237 | } |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 238 | if (updated) { |
| 239 | /* GID table updated, notify the consumers about it */ |
| 240 | gid_event.device = &dev->ibdev; |
| 241 | gid_event.element.port_num = 1; |
| 242 | gid_event.event = IB_EVENT_GID_CHANGE; |
| 243 | ib_dispatch_event(&gid_event); |
| 244 | } |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 245 | mutex_unlock(&dev->dev_lock); |
| 246 | return NOTIFY_OK; |
| 247 | } |
| 248 | |
Roland Dreier | 3495566 | 2012-05-02 15:42:33 -0700 | [diff] [blame] | 249 | static struct notifier_block ocrdma_inet6addr_notifier = { |
| 250 | .notifier_call = ocrdma_inet6addr_event |
| 251 | }; |
| 252 | |
Parav Pandit | 6ab6827 | 2012-06-08 21:25:25 +0530 | [diff] [blame] | 253 | #endif /* IPV6 and VLAN */ |
Roland Dreier | 3495566 | 2012-05-02 15:42:33 -0700 | [diff] [blame] | 254 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 255 | static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device, |
| 256 | u8 port_num) |
| 257 | { |
| 258 | return IB_LINK_LAYER_ETHERNET; |
| 259 | } |
| 260 | |
Roland Dreier | abe3afa | 2012-04-16 11:36:29 -0700 | [diff] [blame] | 261 | static int ocrdma_register_device(struct ocrdma_dev *dev) |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 262 | { |
| 263 | strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX); |
| 264 | ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid); |
| 265 | memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC, |
| 266 | sizeof(OCRDMA_NODE_DESC)); |
| 267 | dev->ibdev.owner = THIS_MODULE; |
| 268 | dev->ibdev.uverbs_cmd_mask = |
| 269 | OCRDMA_UVERBS(GET_CONTEXT) | |
| 270 | OCRDMA_UVERBS(QUERY_DEVICE) | |
| 271 | OCRDMA_UVERBS(QUERY_PORT) | |
| 272 | OCRDMA_UVERBS(ALLOC_PD) | |
| 273 | OCRDMA_UVERBS(DEALLOC_PD) | |
| 274 | OCRDMA_UVERBS(REG_MR) | |
| 275 | OCRDMA_UVERBS(DEREG_MR) | |
| 276 | OCRDMA_UVERBS(CREATE_COMP_CHANNEL) | |
| 277 | OCRDMA_UVERBS(CREATE_CQ) | |
| 278 | OCRDMA_UVERBS(RESIZE_CQ) | |
| 279 | OCRDMA_UVERBS(DESTROY_CQ) | |
| 280 | OCRDMA_UVERBS(REQ_NOTIFY_CQ) | |
| 281 | OCRDMA_UVERBS(CREATE_QP) | |
| 282 | OCRDMA_UVERBS(MODIFY_QP) | |
| 283 | OCRDMA_UVERBS(QUERY_QP) | |
| 284 | OCRDMA_UVERBS(DESTROY_QP) | |
| 285 | OCRDMA_UVERBS(POLL_CQ) | |
| 286 | OCRDMA_UVERBS(POST_SEND) | |
| 287 | OCRDMA_UVERBS(POST_RECV); |
| 288 | |
| 289 | dev->ibdev.uverbs_cmd_mask |= |
| 290 | OCRDMA_UVERBS(CREATE_AH) | |
| 291 | OCRDMA_UVERBS(MODIFY_AH) | |
| 292 | OCRDMA_UVERBS(QUERY_AH) | |
| 293 | OCRDMA_UVERBS(DESTROY_AH); |
| 294 | |
| 295 | dev->ibdev.node_type = RDMA_NODE_IB_CA; |
| 296 | dev->ibdev.phys_port_cnt = 1; |
| 297 | dev->ibdev.num_comp_vectors = 1; |
| 298 | |
| 299 | /* mandatory verbs. */ |
| 300 | dev->ibdev.query_device = ocrdma_query_device; |
| 301 | dev->ibdev.query_port = ocrdma_query_port; |
| 302 | dev->ibdev.modify_port = ocrdma_modify_port; |
| 303 | dev->ibdev.query_gid = ocrdma_query_gid; |
| 304 | dev->ibdev.get_link_layer = ocrdma_link_layer; |
| 305 | dev->ibdev.alloc_pd = ocrdma_alloc_pd; |
| 306 | dev->ibdev.dealloc_pd = ocrdma_dealloc_pd; |
| 307 | |
| 308 | dev->ibdev.create_cq = ocrdma_create_cq; |
| 309 | dev->ibdev.destroy_cq = ocrdma_destroy_cq; |
| 310 | dev->ibdev.resize_cq = ocrdma_resize_cq; |
| 311 | |
| 312 | dev->ibdev.create_qp = ocrdma_create_qp; |
| 313 | dev->ibdev.modify_qp = ocrdma_modify_qp; |
| 314 | dev->ibdev.query_qp = ocrdma_query_qp; |
| 315 | dev->ibdev.destroy_qp = ocrdma_destroy_qp; |
| 316 | |
| 317 | dev->ibdev.query_pkey = ocrdma_query_pkey; |
| 318 | dev->ibdev.create_ah = ocrdma_create_ah; |
| 319 | dev->ibdev.destroy_ah = ocrdma_destroy_ah; |
| 320 | dev->ibdev.query_ah = ocrdma_query_ah; |
| 321 | dev->ibdev.modify_ah = ocrdma_modify_ah; |
| 322 | |
| 323 | dev->ibdev.poll_cq = ocrdma_poll_cq; |
| 324 | dev->ibdev.post_send = ocrdma_post_send; |
| 325 | dev->ibdev.post_recv = ocrdma_post_recv; |
| 326 | dev->ibdev.req_notify_cq = ocrdma_arm_cq; |
| 327 | |
| 328 | dev->ibdev.get_dma_mr = ocrdma_get_dma_mr; |
| 329 | dev->ibdev.dereg_mr = ocrdma_dereg_mr; |
| 330 | dev->ibdev.reg_user_mr = ocrdma_reg_user_mr; |
| 331 | |
Naresh Gottumukkala | 7c33880 | 2013-08-26 15:27:39 +0530 | [diff] [blame^] | 332 | dev->ibdev.alloc_fast_reg_mr = ocrdma_alloc_frmr; |
| 333 | dev->ibdev.alloc_fast_reg_page_list = ocrdma_alloc_frmr_page_list; |
| 334 | dev->ibdev.free_fast_reg_page_list = ocrdma_free_frmr_page_list; |
| 335 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 336 | /* mandatory to support user space verbs consumer. */ |
| 337 | dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext; |
| 338 | dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext; |
| 339 | dev->ibdev.mmap = ocrdma_mmap; |
| 340 | dev->ibdev.dma_device = &dev->nic_info.pdev->dev; |
| 341 | |
| 342 | dev->ibdev.process_mad = ocrdma_process_mad; |
| 343 | |
| 344 | if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) { |
| 345 | dev->ibdev.uverbs_cmd_mask |= |
| 346 | OCRDMA_UVERBS(CREATE_SRQ) | |
| 347 | OCRDMA_UVERBS(MODIFY_SRQ) | |
| 348 | OCRDMA_UVERBS(QUERY_SRQ) | |
| 349 | OCRDMA_UVERBS(DESTROY_SRQ) | |
| 350 | OCRDMA_UVERBS(POST_SRQ_RECV); |
| 351 | |
| 352 | dev->ibdev.create_srq = ocrdma_create_srq; |
| 353 | dev->ibdev.modify_srq = ocrdma_modify_srq; |
| 354 | dev->ibdev.query_srq = ocrdma_query_srq; |
| 355 | dev->ibdev.destroy_srq = ocrdma_destroy_srq; |
| 356 | dev->ibdev.post_srq_recv = ocrdma_post_srq_recv; |
| 357 | } |
| 358 | return ib_register_device(&dev->ibdev, NULL); |
| 359 | } |
| 360 | |
| 361 | static int ocrdma_alloc_resources(struct ocrdma_dev *dev) |
| 362 | { |
| 363 | mutex_init(&dev->dev_lock); |
| 364 | dev->sgid_tbl = kzalloc(sizeof(union ib_gid) * |
| 365 | OCRDMA_MAX_SGID, GFP_KERNEL); |
| 366 | if (!dev->sgid_tbl) |
| 367 | goto alloc_err; |
| 368 | spin_lock_init(&dev->sgid_lock); |
| 369 | |
| 370 | dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) * |
| 371 | OCRDMA_MAX_CQ, GFP_KERNEL); |
| 372 | if (!dev->cq_tbl) |
| 373 | goto alloc_err; |
| 374 | |
| 375 | if (dev->attr.max_qp) { |
| 376 | dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) * |
| 377 | OCRDMA_MAX_QP, GFP_KERNEL); |
| 378 | if (!dev->qp_tbl) |
| 379 | goto alloc_err; |
| 380 | } |
| 381 | spin_lock_init(&dev->av_tbl.lock); |
| 382 | spin_lock_init(&dev->flush_q_lock); |
| 383 | return 0; |
| 384 | alloc_err: |
Naresh Gottumukkala | ef99c4c | 2013-06-10 04:42:39 +0000 | [diff] [blame] | 385 | pr_err("%s(%d) error.\n", __func__, dev->id); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 386 | return -ENOMEM; |
| 387 | } |
| 388 | |
| 389 | static void ocrdma_free_resources(struct ocrdma_dev *dev) |
| 390 | { |
| 391 | kfree(dev->qp_tbl); |
| 392 | kfree(dev->cq_tbl); |
| 393 | kfree(dev->sgid_tbl); |
| 394 | } |
| 395 | |
| 396 | static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info) |
| 397 | { |
| 398 | int status = 0; |
| 399 | struct ocrdma_dev *dev; |
| 400 | |
| 401 | dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev)); |
| 402 | if (!dev) { |
Naresh Gottumukkala | ef99c4c | 2013-06-10 04:42:39 +0000 | [diff] [blame] | 403 | pr_err("Unable to allocate ib device\n"); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 404 | return NULL; |
| 405 | } |
| 406 | dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL); |
| 407 | if (!dev->mbx_cmd) |
| 408 | goto idr_err; |
| 409 | |
| 410 | memcpy(&dev->nic_info, dev_info, sizeof(*dev_info)); |
Tejun Heo | cffcd59 | 2013-02-27 17:04:24 -0800 | [diff] [blame] | 411 | dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 412 | if (dev->id < 0) |
| 413 | goto idr_err; |
| 414 | |
| 415 | status = ocrdma_init_hw(dev); |
| 416 | if (status) |
| 417 | goto init_err; |
| 418 | |
| 419 | status = ocrdma_alloc_resources(dev); |
| 420 | if (status) |
| 421 | goto alloc_err; |
| 422 | |
| 423 | status = ocrdma_build_sgid_tbl(dev); |
| 424 | if (status) |
| 425 | goto alloc_err; |
| 426 | |
| 427 | status = ocrdma_register_device(dev); |
| 428 | if (status) |
| 429 | goto alloc_err; |
| 430 | |
Sasha Levin | 3e4d60a | 2012-04-28 07:40:01 +0200 | [diff] [blame] | 431 | spin_lock(&ocrdma_devlist_lock); |
| 432 | list_add_tail_rcu(&dev->entry, &ocrdma_dev_list); |
| 433 | spin_unlock(&ocrdma_devlist_lock); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 434 | return dev; |
| 435 | |
| 436 | alloc_err: |
| 437 | ocrdma_free_resources(dev); |
| 438 | ocrdma_cleanup_hw(dev); |
| 439 | init_err: |
| 440 | idr_remove(&ocrdma_dev_id, dev->id); |
| 441 | idr_err: |
| 442 | kfree(dev->mbx_cmd); |
| 443 | ib_dealloc_device(&dev->ibdev); |
Naresh Gottumukkala | ef99c4c | 2013-06-10 04:42:39 +0000 | [diff] [blame] | 444 | pr_err("%s() leaving. ret=%d\n", __func__, status); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 445 | return NULL; |
| 446 | } |
| 447 | |
Sasha Levin | 3e4d60a | 2012-04-28 07:40:01 +0200 | [diff] [blame] | 448 | static void ocrdma_remove_free(struct rcu_head *rcu) |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 449 | { |
Sasha Levin | 3e4d60a | 2012-04-28 07:40:01 +0200 | [diff] [blame] | 450 | struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 451 | |
| 452 | ocrdma_free_resources(dev); |
| 453 | ocrdma_cleanup_hw(dev); |
| 454 | |
| 455 | idr_remove(&ocrdma_dev_id, dev->id); |
| 456 | kfree(dev->mbx_cmd); |
| 457 | ib_dealloc_device(&dev->ibdev); |
| 458 | } |
| 459 | |
Sasha Levin | 3e4d60a | 2012-04-28 07:40:01 +0200 | [diff] [blame] | 460 | static void ocrdma_remove(struct ocrdma_dev *dev) |
| 461 | { |
| 462 | /* first unregister with stack to stop all the active traffic |
| 463 | * of the registered clients. |
| 464 | */ |
| 465 | ib_unregister_device(&dev->ibdev); |
| 466 | |
| 467 | spin_lock(&ocrdma_devlist_lock); |
| 468 | list_del_rcu(&dev->entry); |
| 469 | spin_unlock(&ocrdma_devlist_lock); |
| 470 | call_rcu(&dev->rcu, ocrdma_remove_free); |
| 471 | } |
| 472 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 473 | static int ocrdma_open(struct ocrdma_dev *dev) |
| 474 | { |
| 475 | struct ib_event port_event; |
| 476 | |
| 477 | port_event.event = IB_EVENT_PORT_ACTIVE; |
| 478 | port_event.element.port_num = 1; |
| 479 | port_event.device = &dev->ibdev; |
| 480 | ib_dispatch_event(&port_event); |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static int ocrdma_close(struct ocrdma_dev *dev) |
| 485 | { |
| 486 | int i; |
| 487 | struct ocrdma_qp *qp, **cur_qp; |
| 488 | struct ib_event err_event; |
| 489 | struct ib_qp_attr attrs; |
| 490 | int attr_mask = IB_QP_STATE; |
| 491 | |
| 492 | attrs.qp_state = IB_QPS_ERR; |
| 493 | mutex_lock(&dev->dev_lock); |
| 494 | if (dev->qp_tbl) { |
| 495 | cur_qp = dev->qp_tbl; |
| 496 | for (i = 0; i < OCRDMA_MAX_QP; i++) { |
| 497 | qp = cur_qp[i]; |
| 498 | if (qp) { |
| 499 | /* change the QP state to ERROR */ |
| 500 | _ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask); |
| 501 | |
| 502 | err_event.event = IB_EVENT_QP_FATAL; |
| 503 | err_event.element.qp = &qp->ibqp; |
| 504 | err_event.device = &dev->ibdev; |
| 505 | ib_dispatch_event(&err_event); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | mutex_unlock(&dev->dev_lock); |
| 510 | |
| 511 | err_event.event = IB_EVENT_PORT_ERR; |
| 512 | err_event.element.port_num = 1; |
| 513 | err_event.device = &dev->ibdev; |
| 514 | ib_dispatch_event(&err_event); |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | /* event handling via NIC driver ensures that all the NIC specific |
| 519 | * initialization done before RoCE driver notifies |
| 520 | * event to stack. |
| 521 | */ |
| 522 | static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event) |
| 523 | { |
| 524 | switch (event) { |
| 525 | case BE_DEV_UP: |
| 526 | ocrdma_open(dev); |
| 527 | break; |
| 528 | case BE_DEV_DOWN: |
| 529 | ocrdma_close(dev); |
| 530 | break; |
| 531 | }; |
| 532 | } |
| 533 | |
Roland Dreier | abe3afa | 2012-04-16 11:36:29 -0700 | [diff] [blame] | 534 | static struct ocrdma_driver ocrdma_drv = { |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 535 | .name = "ocrdma_driver", |
| 536 | .add = ocrdma_add, |
| 537 | .remove = ocrdma_remove, |
| 538 | .state_change_handler = ocrdma_event_handler, |
| 539 | }; |
| 540 | |
Roland Dreier | 3495566 | 2012-05-02 15:42:33 -0700 | [diff] [blame] | 541 | static void ocrdma_unregister_inet6addr_notifier(void) |
| 542 | { |
Roland Dreier | d90f9b3 | 2012-07-05 22:39:34 -0700 | [diff] [blame] | 543 | #if IS_ENABLED(CONFIG_IPV6) |
Roland Dreier | 3495566 | 2012-05-02 15:42:33 -0700 | [diff] [blame] | 544 | unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier); |
| 545 | #endif |
| 546 | } |
| 547 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 548 | static int __init ocrdma_init_module(void) |
| 549 | { |
| 550 | int status; |
| 551 | |
Roland Dreier | d90f9b3 | 2012-07-05 22:39:34 -0700 | [diff] [blame] | 552 | #if IS_ENABLED(CONFIG_IPV6) |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 553 | status = register_inet6addr_notifier(&ocrdma_inet6addr_notifier); |
| 554 | if (status) |
| 555 | return status; |
Roland Dreier | 3495566 | 2012-05-02 15:42:33 -0700 | [diff] [blame] | 556 | #endif |
| 557 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 558 | status = be_roce_register_driver(&ocrdma_drv); |
| 559 | if (status) |
Roland Dreier | 3495566 | 2012-05-02 15:42:33 -0700 | [diff] [blame] | 560 | ocrdma_unregister_inet6addr_notifier(); |
| 561 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 562 | return status; |
| 563 | } |
| 564 | |
| 565 | static void __exit ocrdma_exit_module(void) |
| 566 | { |
| 567 | be_roce_unregister_driver(&ocrdma_drv); |
Roland Dreier | 3495566 | 2012-05-02 15:42:33 -0700 | [diff] [blame] | 568 | ocrdma_unregister_inet6addr_notifier(); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | module_init(ocrdma_init_module); |
| 572 | module_exit(ocrdma_exit_module); |