Upinder Malhi | e3cf00d | 2013-09-10 03:38:16 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you may redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 9 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 10 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 11 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 12 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 14 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 15 | * SOFTWARE. |
| 16 | * |
| 17 | * Author: Upinder Malhi <umalhi@cisco.com> |
| 18 | * Author: Anant Deepak <anadeepa@cisco.com> |
| 19 | * Author: Cesare Cantu' <cantuc@cisco.com> |
| 20 | * Author: Jeff Squyres <jsquyres@cisco.com> |
| 21 | * Author: Kiran Thirumalai <kithirum@cisco.com> |
| 22 | * Author: Xuyang Wang <xuywang@cisco.com> |
| 23 | * Author: Reese Faucette <rfaucett@cisco.com> |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/pci.h> |
| 32 | #include <linux/netdevice.h> |
| 33 | |
| 34 | #include <rdma/ib_user_verbs.h> |
| 35 | #include <rdma/ib_addr.h> |
| 36 | |
| 37 | #include "usnic_abi.h" |
| 38 | #include "usnic_common_util.h" |
| 39 | #include "usnic_ib.h" |
| 40 | #include "usnic_ib_qp_grp.h" |
| 41 | #include "usnic_log.h" |
| 42 | #include "usnic_fwd.h" |
| 43 | #include "usnic_debugfs.h" |
| 44 | #include "usnic_ib_verbs.h" |
| 45 | #include "usnic_transport.h" |
| 46 | #include "usnic_uiom.h" |
| 47 | #include "usnic_ib_sysfs.h" |
| 48 | |
| 49 | unsigned int usnic_log_lvl = USNIC_LOG_LVL_ERR; |
| 50 | unsigned int usnic_ib_share_vf = 1; |
| 51 | |
| 52 | static const char usnic_version[] = |
| 53 | DRV_NAME ": Cisco VIC (USNIC) Verbs Driver v" |
| 54 | DRV_VERSION " (" DRV_RELDATE ")\n"; |
| 55 | |
| 56 | static DEFINE_MUTEX(usnic_ib_ibdev_list_lock); |
| 57 | static LIST_HEAD(usnic_ib_ibdev_list); |
| 58 | |
| 59 | /* Callback dump funcs */ |
| 60 | static int usnic_ib_dump_vf_hdr(void *obj, char *buf, int buf_sz) |
| 61 | { |
| 62 | struct usnic_ib_vf *vf = obj; |
| 63 | return scnprintf(buf, buf_sz, "PF: %s ", vf->pf->ib_dev.name); |
| 64 | } |
| 65 | /* End callback dump funcs */ |
| 66 | |
| 67 | static void usnic_ib_dump_vf(struct usnic_ib_vf *vf, char *buf, int buf_sz) |
| 68 | { |
| 69 | usnic_vnic_dump(vf->vnic, buf, buf_sz, vf, |
| 70 | usnic_ib_dump_vf_hdr, |
| 71 | usnic_ib_qp_grp_dump_hdr, usnic_ib_qp_grp_dump_rows); |
| 72 | } |
| 73 | |
| 74 | void usnic_ib_log_vf(struct usnic_ib_vf *vf) |
| 75 | { |
| 76 | char buf[1000]; |
| 77 | usnic_ib_dump_vf(vf, buf, sizeof(buf)); |
| 78 | usnic_dbg("%s\n", buf); |
| 79 | } |
| 80 | |
| 81 | /* Start of netdev section */ |
| 82 | static inline const char *usnic_ib_netdev_event_to_string(unsigned long event) |
| 83 | { |
| 84 | const char *event2str[] = {"NETDEV_NONE", "NETDEV_UP", "NETDEV_DOWN", |
| 85 | "NETDEV_REBOOT", "NETDEV_CHANGE", |
| 86 | "NETDEV_REGISTER", "NETDEV_UNREGISTER", "NETDEV_CHANGEMTU", |
| 87 | "NETDEV_CHANGEADDR", "NETDEV_GOING_DOWN", "NETDEV_FEAT_CHANGE", |
| 88 | "NETDEV_BONDING_FAILOVER", "NETDEV_PRE_UP", |
| 89 | "NETDEV_PRE_TYPE_CHANGE", "NETDEV_POST_TYPE_CHANGE", |
| 90 | "NETDEV_POST_INT", "NETDEV_UNREGISTER_FINAL", "NETDEV_RELEASE", |
| 91 | "NETDEV_NOTIFY_PEERS", "NETDEV_JOIN" |
| 92 | }; |
| 93 | |
| 94 | if (event >= ARRAY_SIZE(event2str)) |
| 95 | return "UNKNOWN_NETDEV_EVENT"; |
| 96 | else |
| 97 | return event2str[event]; |
| 98 | } |
| 99 | |
| 100 | static void usnic_ib_qp_grp_modify_active_to_err(struct usnic_ib_dev *us_ibdev) |
| 101 | { |
| 102 | struct usnic_ib_ucontext *ctx; |
| 103 | struct usnic_ib_qp_grp *qp_grp; |
| 104 | enum ib_qp_state cur_state; |
| 105 | int status; |
| 106 | |
| 107 | BUG_ON(!mutex_is_locked(&us_ibdev->usdev_lock)); |
| 108 | |
| 109 | list_for_each_entry(ctx, &us_ibdev->ctx_list, link) { |
| 110 | list_for_each_entry(qp_grp, &ctx->qp_grp_list, link) { |
| 111 | cur_state = qp_grp->state; |
| 112 | if (cur_state == IB_QPS_INIT || |
| 113 | cur_state == IB_QPS_RTR || |
| 114 | cur_state == IB_QPS_RTS) { |
| 115 | status = usnic_ib_qp_grp_modify(qp_grp, |
| 116 | IB_QPS_ERR, |
| 117 | NULL); |
| 118 | if (status) { |
| 119 | usnic_err("Failed to transistion qp grp %u from %s to %s\n", |
| 120 | qp_grp->grp_id, |
| 121 | usnic_ib_qp_grp_state_to_string |
| 122 | (cur_state), |
| 123 | usnic_ib_qp_grp_state_to_string |
| 124 | (IB_QPS_ERR)); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void usnic_ib_handle_usdev_event(struct usnic_ib_dev *us_ibdev, |
| 132 | unsigned long event) |
| 133 | { |
| 134 | struct net_device *netdev; |
| 135 | struct ib_event ib_event; |
| 136 | |
| 137 | memset(&ib_event, 0, sizeof(ib_event)); |
| 138 | |
| 139 | mutex_lock(&us_ibdev->usdev_lock); |
| 140 | netdev = us_ibdev->netdev; |
| 141 | switch (event) { |
| 142 | case NETDEV_REBOOT: |
| 143 | usnic_info("PF Reset on %s\n", us_ibdev->ib_dev.name); |
| 144 | usnic_ib_qp_grp_modify_active_to_err(us_ibdev); |
| 145 | ib_event.event = IB_EVENT_PORT_ERR; |
| 146 | ib_event.device = &us_ibdev->ib_dev; |
| 147 | ib_event.element.port_num = 1; |
| 148 | ib_dispatch_event(&ib_event); |
| 149 | break; |
| 150 | case NETDEV_UP: |
| 151 | case NETDEV_DOWN: |
| 152 | case NETDEV_CHANGE: |
| 153 | if (!us_ibdev->link_up && netif_carrier_ok(netdev)) { |
| 154 | us_ibdev->link_up = true; |
| 155 | usnic_info("Link UP on %s\n", us_ibdev->ib_dev.name); |
| 156 | ib_event.event = IB_EVENT_PORT_ACTIVE; |
| 157 | ib_event.device = &us_ibdev->ib_dev; |
| 158 | ib_event.element.port_num = 1; |
| 159 | ib_dispatch_event(&ib_event); |
| 160 | } else if (us_ibdev->link_up && !netif_carrier_ok(netdev)) { |
| 161 | us_ibdev->link_up = false; |
| 162 | usnic_info("Link DOWN on %s\n", us_ibdev->ib_dev.name); |
| 163 | usnic_ib_qp_grp_modify_active_to_err(us_ibdev); |
| 164 | ib_event.event = IB_EVENT_PORT_ERR; |
| 165 | ib_event.device = &us_ibdev->ib_dev; |
| 166 | ib_event.element.port_num = 1; |
| 167 | ib_dispatch_event(&ib_event); |
| 168 | } else { |
| 169 | usnic_dbg("Ignorning %s on %s\n", |
| 170 | usnic_ib_netdev_event_to_string(event), |
| 171 | us_ibdev->ib_dev.name); |
| 172 | } |
| 173 | break; |
| 174 | case NETDEV_CHANGEADDR: |
| 175 | if (!memcmp(us_ibdev->mac, netdev->dev_addr, |
| 176 | sizeof(us_ibdev->mac))) { |
| 177 | usnic_dbg("Ignorning addr change on %s\n", |
| 178 | us_ibdev->ib_dev.name); |
| 179 | } else { |
| 180 | usnic_info(" %s old mac: %pM new mac: %pM\n", |
| 181 | us_ibdev->ib_dev.name, |
| 182 | us_ibdev->mac, |
| 183 | netdev->dev_addr); |
| 184 | memcpy(us_ibdev->mac, netdev->dev_addr, |
| 185 | sizeof(us_ibdev->mac)); |
| 186 | usnic_ib_qp_grp_modify_active_to_err(us_ibdev); |
| 187 | ib_event.event = IB_EVENT_GID_CHANGE; |
| 188 | ib_event.device = &us_ibdev->ib_dev; |
| 189 | ib_event.element.port_num = 1; |
| 190 | ib_dispatch_event(&ib_event); |
| 191 | } |
| 192 | |
| 193 | break; |
| 194 | case NETDEV_CHANGEMTU: |
| 195 | if (us_ibdev->mtu != netdev->mtu) { |
| 196 | usnic_info("MTU Change on %s old: %u new: %u\n", |
| 197 | us_ibdev->ib_dev.name, |
| 198 | us_ibdev->mtu, netdev->mtu); |
| 199 | us_ibdev->mtu = netdev->mtu; |
| 200 | usnic_ib_qp_grp_modify_active_to_err(us_ibdev); |
| 201 | } else { |
| 202 | usnic_dbg("Ignoring MTU change on %s\n", |
| 203 | us_ibdev->ib_dev.name); |
| 204 | } |
| 205 | break; |
| 206 | default: |
| 207 | usnic_dbg("Ignorning event %s on %s", |
| 208 | usnic_ib_netdev_event_to_string(event), |
| 209 | us_ibdev->ib_dev.name); |
| 210 | } |
| 211 | mutex_unlock(&us_ibdev->usdev_lock); |
| 212 | } |
| 213 | |
| 214 | static int usnic_ib_netdevice_event(struct notifier_block *notifier, |
| 215 | unsigned long event, void *ptr) |
| 216 | { |
| 217 | struct usnic_ib_dev *us_ibdev; |
| 218 | |
| 219 | struct net_device *netdev = netdev_notifier_info_to_dev(ptr); |
| 220 | |
| 221 | mutex_lock(&usnic_ib_ibdev_list_lock); |
| 222 | list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) { |
| 223 | if (us_ibdev->netdev == netdev) { |
| 224 | usnic_ib_handle_usdev_event(us_ibdev, event); |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | mutex_unlock(&usnic_ib_ibdev_list_lock); |
| 229 | |
| 230 | return NOTIFY_DONE; |
| 231 | } |
| 232 | |
| 233 | static struct notifier_block usnic_ib_netdevice_notifier = { |
| 234 | .notifier_call = usnic_ib_netdevice_event |
| 235 | }; |
| 236 | /* End of netdev section */ |
| 237 | |
| 238 | /* Start of PF discovery section */ |
| 239 | static void *usnic_ib_device_add(struct pci_dev *dev) |
| 240 | { |
| 241 | struct usnic_ib_dev *us_ibdev; |
| 242 | union ib_gid gid; |
| 243 | |
| 244 | usnic_dbg("\n"); |
| 245 | |
| 246 | us_ibdev = (struct usnic_ib_dev *)ib_alloc_device(sizeof(*us_ibdev)); |
| 247 | if (IS_ERR_OR_NULL(us_ibdev)) { |
| 248 | usnic_err("Device %s context alloc failed\n", |
| 249 | netdev_name(pci_get_drvdata(dev))); |
| 250 | return ERR_PTR(us_ibdev ? PTR_ERR(us_ibdev) : -EFAULT); |
| 251 | } |
| 252 | |
| 253 | us_ibdev->ufdev = usnic_fwd_dev_alloc(dev); |
| 254 | if (IS_ERR_OR_NULL(us_ibdev->ufdev)) { |
| 255 | usnic_err("Failed to alloc ufdev for %s with err %ld\n", |
| 256 | pci_name(dev), PTR_ERR(us_ibdev->ufdev)); |
| 257 | goto err_dealloc; |
| 258 | } |
| 259 | |
| 260 | mutex_init(&us_ibdev->usdev_lock); |
| 261 | INIT_LIST_HEAD(&us_ibdev->vf_dev_list); |
| 262 | INIT_LIST_HEAD(&us_ibdev->ctx_list); |
| 263 | |
| 264 | us_ibdev->pdev = dev; |
| 265 | us_ibdev->netdev = pci_get_drvdata(dev); |
| 266 | us_ibdev->ib_dev.owner = THIS_MODULE; |
| 267 | us_ibdev->ib_dev.node_type = RDMA_NODE_USNIC; |
| 268 | us_ibdev->ib_dev.phys_port_cnt = USNIC_IB_PORT_CNT; |
| 269 | us_ibdev->ib_dev.num_comp_vectors = USNIC_IB_NUM_COMP_VECTORS; |
| 270 | us_ibdev->ib_dev.dma_device = &dev->dev; |
| 271 | us_ibdev->ib_dev.uverbs_abi_ver = USNIC_UVERBS_ABI_VERSION; |
| 272 | strlcpy(us_ibdev->ib_dev.name, "usnic_%d", IB_DEVICE_NAME_MAX); |
| 273 | |
| 274 | us_ibdev->ib_dev.uverbs_cmd_mask = |
| 275 | (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | |
| 276 | (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | |
| 277 | (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | |
| 278 | (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | |
| 279 | (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | |
| 280 | (1ull << IB_USER_VERBS_CMD_REG_MR) | |
| 281 | (1ull << IB_USER_VERBS_CMD_DEREG_MR) | |
| 282 | (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | |
| 283 | (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | |
| 284 | (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | |
| 285 | (1ull << IB_USER_VERBS_CMD_CREATE_QP) | |
| 286 | (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | |
| 287 | (1ull << IB_USER_VERBS_CMD_QUERY_QP) | |
| 288 | (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | |
| 289 | (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | |
| 290 | (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | |
| 291 | (1ull << IB_USER_VERBS_CMD_OPEN_QP); |
| 292 | |
| 293 | us_ibdev->ib_dev.query_device = usnic_ib_query_device; |
| 294 | us_ibdev->ib_dev.query_port = usnic_ib_query_port; |
| 295 | us_ibdev->ib_dev.query_pkey = usnic_ib_query_pkey; |
| 296 | us_ibdev->ib_dev.query_gid = usnic_ib_query_gid; |
| 297 | us_ibdev->ib_dev.get_link_layer = usnic_ib_port_link_layer; |
| 298 | us_ibdev->ib_dev.alloc_pd = usnic_ib_alloc_pd; |
| 299 | us_ibdev->ib_dev.dealloc_pd = usnic_ib_dealloc_pd; |
| 300 | us_ibdev->ib_dev.create_qp = usnic_ib_create_qp; |
| 301 | us_ibdev->ib_dev.modify_qp = usnic_ib_modify_qp; |
| 302 | us_ibdev->ib_dev.query_qp = usnic_ib_query_qp; |
| 303 | us_ibdev->ib_dev.destroy_qp = usnic_ib_destroy_qp; |
| 304 | us_ibdev->ib_dev.create_cq = usnic_ib_create_cq; |
| 305 | us_ibdev->ib_dev.destroy_cq = usnic_ib_destroy_cq; |
| 306 | us_ibdev->ib_dev.reg_user_mr = usnic_ib_reg_mr; |
| 307 | us_ibdev->ib_dev.dereg_mr = usnic_ib_dereg_mr; |
| 308 | us_ibdev->ib_dev.alloc_ucontext = usnic_ib_alloc_ucontext; |
| 309 | us_ibdev->ib_dev.dealloc_ucontext = usnic_ib_dealloc_ucontext; |
| 310 | us_ibdev->ib_dev.mmap = usnic_ib_mmap; |
| 311 | us_ibdev->ib_dev.create_ah = usnic_ib_create_ah; |
| 312 | us_ibdev->ib_dev.destroy_ah = usnic_ib_destroy_ah; |
| 313 | us_ibdev->ib_dev.post_send = usnic_ib_post_send; |
| 314 | us_ibdev->ib_dev.post_recv = usnic_ib_post_recv; |
| 315 | us_ibdev->ib_dev.poll_cq = usnic_ib_poll_cq; |
| 316 | us_ibdev->ib_dev.req_notify_cq = usnic_ib_req_notify_cq; |
| 317 | us_ibdev->ib_dev.get_dma_mr = usnic_ib_get_dma_mr; |
| 318 | |
| 319 | |
| 320 | if (ib_register_device(&us_ibdev->ib_dev, NULL)) |
| 321 | goto err_fwd_dealloc; |
| 322 | |
| 323 | us_ibdev->link_up = netif_carrier_ok(us_ibdev->netdev); |
| 324 | us_ibdev->mtu = us_ibdev->netdev->mtu; |
| 325 | memcpy(&us_ibdev->mac, us_ibdev->netdev->dev_addr, |
| 326 | sizeof(us_ibdev->mac)); |
| 327 | usnic_mac_to_gid(us_ibdev->netdev->perm_addr, &gid.raw[0]); |
| 328 | memcpy(&us_ibdev->ib_dev.node_guid, &gid.global.interface_id, |
| 329 | sizeof(gid.global.interface_id)); |
| 330 | kref_init(&us_ibdev->vf_cnt); |
| 331 | |
| 332 | usnic_info("Added ibdev: %s netdev: %s with mac %pM Link: %u MTU: %u\n", |
| 333 | us_ibdev->ib_dev.name, netdev_name(us_ibdev->netdev), |
| 334 | us_ibdev->mac, us_ibdev->link_up, us_ibdev->mtu); |
| 335 | return us_ibdev; |
| 336 | |
| 337 | err_fwd_dealloc: |
| 338 | usnic_fwd_dev_free(us_ibdev->ufdev); |
| 339 | err_dealloc: |
| 340 | usnic_err("failed -- deallocing device\n"); |
| 341 | ib_dealloc_device(&us_ibdev->ib_dev); |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | static void usnic_ib_device_remove(struct usnic_ib_dev *us_ibdev) |
| 346 | { |
| 347 | usnic_info("Unregistering %s\n", us_ibdev->ib_dev.name); |
| 348 | usnic_ib_sysfs_unregister_usdev(us_ibdev); |
| 349 | usnic_fwd_dev_free(us_ibdev->ufdev); |
| 350 | ib_unregister_device(&us_ibdev->ib_dev); |
| 351 | ib_dealloc_device(&us_ibdev->ib_dev); |
| 352 | } |
| 353 | |
| 354 | static void usnic_ib_undiscover_pf(struct kref *kref) |
| 355 | { |
| 356 | struct usnic_ib_dev *us_ibdev, *tmp; |
| 357 | struct pci_dev *dev; |
| 358 | bool found = false; |
| 359 | |
| 360 | dev = container_of(kref, struct usnic_ib_dev, vf_cnt)->pdev; |
| 361 | mutex_lock(&usnic_ib_ibdev_list_lock); |
| 362 | list_for_each_entry_safe(us_ibdev, tmp, |
| 363 | &usnic_ib_ibdev_list, ib_dev_link) { |
| 364 | if (us_ibdev->pdev == dev) { |
| 365 | list_del(&us_ibdev->ib_dev_link); |
| 366 | usnic_ib_device_remove(us_ibdev); |
| 367 | found = true; |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | WARN(!found, "Failed to remove PF %s\n", pci_name(dev)); |
| 373 | |
| 374 | mutex_unlock(&usnic_ib_ibdev_list_lock); |
| 375 | } |
| 376 | |
| 377 | static struct usnic_ib_dev *usnic_ib_discover_pf(struct usnic_vnic *vnic) |
| 378 | { |
| 379 | struct usnic_ib_dev *us_ibdev; |
| 380 | struct pci_dev *parent_pci, *vf_pci; |
| 381 | int err; |
| 382 | |
| 383 | vf_pci = usnic_vnic_get_pdev(vnic); |
| 384 | parent_pci = pci_physfn(vf_pci); |
| 385 | |
| 386 | BUG_ON(!parent_pci); |
| 387 | |
| 388 | mutex_lock(&usnic_ib_ibdev_list_lock); |
| 389 | list_for_each_entry(us_ibdev, &usnic_ib_ibdev_list, ib_dev_link) { |
| 390 | if (us_ibdev->pdev == parent_pci) { |
| 391 | kref_get(&us_ibdev->vf_cnt); |
| 392 | goto out; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | us_ibdev = usnic_ib_device_add(parent_pci); |
| 397 | if (IS_ERR_OR_NULL(us_ibdev)) { |
| 398 | us_ibdev = (us_ibdev) ? us_ibdev : ERR_PTR(-EFAULT); |
| 399 | goto out; |
| 400 | } |
| 401 | |
| 402 | err = usnic_ib_sysfs_register_usdev(us_ibdev); |
| 403 | if (err) { |
| 404 | usnic_ib_device_remove(us_ibdev); |
| 405 | us_ibdev = ERR_PTR(err); |
| 406 | goto out; |
| 407 | } |
| 408 | |
| 409 | list_add(&us_ibdev->ib_dev_link, &usnic_ib_ibdev_list); |
| 410 | out: |
| 411 | mutex_unlock(&usnic_ib_ibdev_list_lock); |
| 412 | return us_ibdev; |
| 413 | } |
| 414 | /* End of PF discovery section */ |
| 415 | |
| 416 | /* Start of PCI section */ |
| 417 | |
| 418 | static DEFINE_PCI_DEVICE_TABLE(usnic_ib_pci_ids) = { |
| 419 | {PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC)}, |
| 420 | {0,} |
| 421 | }; |
| 422 | |
| 423 | static int usnic_ib_pci_probe(struct pci_dev *pdev, |
| 424 | const struct pci_device_id *id) |
| 425 | { |
| 426 | int err; |
| 427 | struct usnic_ib_dev *pf; |
| 428 | struct usnic_ib_vf *vf; |
| 429 | enum usnic_vnic_res_type res_type; |
| 430 | |
| 431 | vf = kzalloc(sizeof(*vf), GFP_KERNEL); |
| 432 | if (!vf) |
| 433 | return -ENOMEM; |
| 434 | |
| 435 | err = pci_enable_device(pdev); |
| 436 | if (err) { |
| 437 | usnic_err("Failed to enable %s with err %d\n", |
| 438 | pci_name(pdev), err); |
| 439 | goto out_clean_vf; |
| 440 | } |
| 441 | |
| 442 | err = pci_request_regions(pdev, DRV_NAME); |
| 443 | if (err) { |
| 444 | usnic_err("Failed to request region for %s with err %d\n", |
| 445 | pci_name(pdev), err); |
| 446 | goto out_disable_device; |
| 447 | } |
| 448 | |
| 449 | pci_set_master(pdev); |
| 450 | pci_set_drvdata(pdev, vf); |
| 451 | |
| 452 | vf->vnic = usnic_vnic_alloc(pdev); |
| 453 | if (IS_ERR_OR_NULL(vf->vnic)) { |
| 454 | err = (vf->vnic ? PTR_ERR(vf->vnic) : -ENOMEM); |
| 455 | usnic_err("Failed to alloc vnic for %s with err %d\n", |
| 456 | pci_name(pdev), err); |
| 457 | goto out_release_regions; |
| 458 | } |
| 459 | |
| 460 | pf = usnic_ib_discover_pf(vf->vnic); |
| 461 | if (IS_ERR_OR_NULL(pf)) { |
| 462 | usnic_err("Failed to discover pf of vnic %s with err%ld\n", |
| 463 | pci_name(pdev), PTR_ERR(pf)); |
| 464 | err = (pf ? PTR_ERR(pf) : -EFAULT); |
| 465 | goto out_clean_vnic; |
| 466 | } |
| 467 | |
| 468 | vf->pf = pf; |
| 469 | spin_lock_init(&vf->lock); |
| 470 | mutex_lock(&pf->usdev_lock); |
| 471 | list_add_tail(&vf->link, &pf->vf_dev_list); |
| 472 | /* |
| 473 | * Save max settings (will be same for each VF, easier to re-write than |
| 474 | * to say "if (!set) { set_values(); set=1; } |
| 475 | */ |
| 476 | for (res_type = USNIC_VNIC_RES_TYPE_EOL+1; |
| 477 | res_type < USNIC_VNIC_RES_TYPE_MAX; |
| 478 | res_type++) { |
| 479 | pf->vf_res_cnt[res_type] = usnic_vnic_res_cnt(vf->vnic, |
| 480 | res_type); |
| 481 | } |
| 482 | |
| 483 | mutex_unlock(&pf->usdev_lock); |
| 484 | |
| 485 | usnic_info("Registering usnic VF %s into PF %s\n", pci_name(pdev), |
| 486 | pf->ib_dev.name); |
| 487 | usnic_ib_log_vf(vf); |
| 488 | return 0; |
| 489 | |
| 490 | out_clean_vnic: |
| 491 | usnic_vnic_free(vf->vnic); |
| 492 | out_release_regions: |
| 493 | pci_set_drvdata(pdev, NULL); |
| 494 | pci_clear_master(pdev); |
| 495 | pci_release_regions(pdev); |
| 496 | out_disable_device: |
| 497 | pci_disable_device(pdev); |
| 498 | out_clean_vf: |
| 499 | kfree(vf); |
| 500 | return err; |
| 501 | } |
| 502 | |
| 503 | static void usnic_ib_pci_remove(struct pci_dev *pdev) |
| 504 | { |
| 505 | struct usnic_ib_vf *vf = pci_get_drvdata(pdev); |
| 506 | struct usnic_ib_dev *pf = vf->pf; |
| 507 | |
| 508 | mutex_lock(&pf->usdev_lock); |
| 509 | list_del(&vf->link); |
| 510 | mutex_unlock(&pf->usdev_lock); |
| 511 | |
| 512 | kref_put(&pf->vf_cnt, usnic_ib_undiscover_pf); |
| 513 | usnic_vnic_free(vf->vnic); |
| 514 | pci_set_drvdata(pdev, NULL); |
| 515 | pci_clear_master(pdev); |
| 516 | pci_release_regions(pdev); |
| 517 | pci_disable_device(pdev); |
| 518 | kfree(vf); |
| 519 | |
| 520 | usnic_info("Removed VF %s\n", pci_name(pdev)); |
| 521 | } |
| 522 | |
| 523 | /* PCI driver entry points */ |
| 524 | static struct pci_driver usnic_ib_pci_driver = { |
| 525 | .name = DRV_NAME, |
| 526 | .id_table = usnic_ib_pci_ids, |
| 527 | .probe = usnic_ib_pci_probe, |
| 528 | .remove = usnic_ib_pci_remove, |
| 529 | }; |
| 530 | /* End of PCI section */ |
| 531 | |
| 532 | /* Start of module section */ |
| 533 | static int __init usnic_ib_init(void) |
| 534 | { |
| 535 | int err; |
| 536 | |
| 537 | printk_once(KERN_INFO "%s", usnic_version); |
| 538 | |
| 539 | err = usnic_uiom_init(DRV_NAME); |
| 540 | if (err) { |
| 541 | usnic_err("Unable to initalize umem with err %d\n", err); |
| 542 | return err; |
| 543 | } |
| 544 | |
| 545 | if (pci_register_driver(&usnic_ib_pci_driver)) { |
| 546 | usnic_err("Unable to register with PCI\n"); |
| 547 | goto out_umem_fini; |
| 548 | } |
| 549 | |
| 550 | err = register_netdevice_notifier(&usnic_ib_netdevice_notifier); |
| 551 | if (err) { |
| 552 | usnic_err("Failed to register netdev notifier\n"); |
| 553 | goto out_pci_unreg; |
| 554 | } |
| 555 | |
| 556 | err = usnic_transport_init(); |
| 557 | if (err) { |
| 558 | usnic_err("Failed to initialize transport\n"); |
| 559 | goto out_unreg_netdev_notifier; |
| 560 | } |
| 561 | |
| 562 | usnic_debugfs_init(); |
| 563 | |
| 564 | return 0; |
| 565 | |
| 566 | out_unreg_netdev_notifier: |
| 567 | unregister_netdevice_notifier(&usnic_ib_netdevice_notifier); |
| 568 | out_pci_unreg: |
| 569 | pci_unregister_driver(&usnic_ib_pci_driver); |
| 570 | out_umem_fini: |
| 571 | usnic_uiom_fini(); |
| 572 | |
| 573 | return err; |
| 574 | } |
| 575 | |
| 576 | static void __exit usnic_ib_destroy(void) |
| 577 | { |
| 578 | usnic_dbg("\n"); |
| 579 | usnic_debugfs_exit(); |
| 580 | usnic_transport_fini(); |
| 581 | unregister_netdevice_notifier(&usnic_ib_netdevice_notifier); |
| 582 | pci_unregister_driver(&usnic_ib_pci_driver); |
| 583 | usnic_uiom_fini(); |
| 584 | } |
| 585 | |
| 586 | MODULE_DESCRIPTION("Cisco VIC (usNIC) Verbs Driver"); |
| 587 | MODULE_AUTHOR("Upinder Malhi <umalhi@cisco.com>"); |
| 588 | MODULE_LICENSE("Dual BSD/GPL"); |
| 589 | MODULE_VERSION(DRV_VERSION); |
| 590 | module_param(usnic_log_lvl, uint, S_IRUGO | S_IWUSR); |
| 591 | module_param(usnic_ib_share_vf, uint, S_IRUGO | S_IWUSR); |
| 592 | MODULE_PARM_DESC(usnic_log_lvl, " Off=0, Err=1, Info=2, Debug=3"); |
| 593 | MODULE_PARM_DESC(usnic_ib_share_vf, "Off=0, On=1 VF sharing amongst QPs"); |
| 594 | MODULE_DEVICE_TABLE(pci, usnic_ib_pci_ids); |
| 595 | |
| 596 | module_init(usnic_ib_init); |
| 597 | module_exit(usnic_ib_destroy); |
| 598 | /* End of module section */ |