1 /* Broadcom NetXtreme-C/E network driver. 2 * 3 * Copyright (c) 2014-2016 Broadcom Corporation 4 * Copyright (c) 2016-2017 Broadcom Limited 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation. 9 */ 10 11 #include <linux/module.h> 12 #include <linux/pci.h> 13 #include <linux/netdevice.h> 14 #include <linux/if_vlan.h> 15 #include <linux/interrupt.h> 16 #include <linux/etherdevice.h> 17 #include "bnxt_hsi.h" 18 #include "bnxt.h" 19 #include "bnxt_ulp.h" 20 #include "bnxt_sriov.h" 21 #include "bnxt_ethtool.h" 22 23 #ifdef CONFIG_BNXT_SRIOV 24 static int bnxt_hwrm_fwd_async_event_cmpl(struct bnxt *bp, 25 struct bnxt_vf_info *vf, u16 event_id) 26 { 27 struct hwrm_fwd_async_event_cmpl_output *resp = bp->hwrm_cmd_resp_addr; 28 struct hwrm_fwd_async_event_cmpl_input req = {0}; 29 struct hwrm_async_event_cmpl *async_cmpl; 30 int rc = 0; 31 32 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_ASYNC_EVENT_CMPL, -1, -1); 33 if (vf) 34 req.encap_async_event_target_id = cpu_to_le16(vf->fw_fid); 35 else 36 /* broadcast this async event to all VFs */ 37 req.encap_async_event_target_id = cpu_to_le16(0xffff); 38 async_cmpl = (struct hwrm_async_event_cmpl *)req.encap_async_event_cmpl; 39 async_cmpl->type = cpu_to_le16(ASYNC_EVENT_CMPL_TYPE_HWRM_ASYNC_EVENT); 40 async_cmpl->event_id = cpu_to_le16(event_id); 41 42 mutex_lock(&bp->hwrm_cmd_lock); 43 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 44 45 if (rc) { 46 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl failed. rc:%d\n", 47 rc); 48 goto fwd_async_event_cmpl_exit; 49 } 50 51 if (resp->error_code) { 52 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl error %d\n", 53 resp->error_code); 54 rc = -1; 55 } 56 57 fwd_async_event_cmpl_exit: 58 mutex_unlock(&bp->hwrm_cmd_lock); 59 return rc; 60 } 61 62 static int bnxt_vf_ndo_prep(struct bnxt *bp, int vf_id) 63 { 64 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 65 netdev_err(bp->dev, "vf ndo called though PF is down\n"); 66 return -EINVAL; 67 } 68 if (!bp->pf.active_vfs) { 69 netdev_err(bp->dev, "vf ndo called though sriov is disabled\n"); 70 return -EINVAL; 71 } 72 if (vf_id >= bp->pf.max_vfs) { 73 netdev_err(bp->dev, "Invalid VF id %d\n", vf_id); 74 return -EINVAL; 75 } 76 return 0; 77 } 78 79 int bnxt_set_vf_spoofchk(struct net_device *dev, int vf_id, bool setting) 80 { 81 struct hwrm_func_cfg_input req = {0}; 82 struct bnxt *bp = netdev_priv(dev); 83 struct bnxt_vf_info *vf; 84 bool old_setting = false; 85 u32 func_flags; 86 int rc; 87 88 if (bp->hwrm_spec_code < 0x10701) 89 return -ENOTSUPP; 90 91 rc = bnxt_vf_ndo_prep(bp, vf_id); 92 if (rc) 93 return rc; 94 95 vf = &bp->pf.vf[vf_id]; 96 if (vf->flags & BNXT_VF_SPOOFCHK) 97 old_setting = true; 98 if (old_setting == setting) 99 return 0; 100 101 func_flags = vf->func_flags; 102 if (setting) 103 func_flags |= FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK_ENABLE; 104 else 105 func_flags |= FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK_DISABLE; 106 /*TODO: if the driver supports VLAN filter on guest VLAN, 107 * the spoof check should also include vlan anti-spoofing 108 */ 109 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1); 110 req.fid = cpu_to_le16(vf->fw_fid); 111 req.flags = cpu_to_le32(func_flags); 112 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 113 if (!rc) { 114 vf->func_flags = func_flags; 115 if (setting) 116 vf->flags |= BNXT_VF_SPOOFCHK; 117 else 118 vf->flags &= ~BNXT_VF_SPOOFCHK; 119 } 120 return rc; 121 } 122 123 int bnxt_get_vf_config(struct net_device *dev, int vf_id, 124 struct ifla_vf_info *ivi) 125 { 126 struct bnxt *bp = netdev_priv(dev); 127 struct bnxt_vf_info *vf; 128 int rc; 129 130 rc = bnxt_vf_ndo_prep(bp, vf_id); 131 if (rc) 132 return rc; 133 134 ivi->vf = vf_id; 135 vf = &bp->pf.vf[vf_id]; 136 137 memcpy(&ivi->mac, vf->mac_addr, ETH_ALEN); 138 ivi->max_tx_rate = vf->max_tx_rate; 139 ivi->min_tx_rate = vf->min_tx_rate; 140 ivi->vlan = vf->vlan; 141 if (vf->flags & BNXT_VF_QOS) 142 ivi->qos = vf->vlan >> VLAN_PRIO_SHIFT; 143 else 144 ivi->qos = 0; 145 ivi->spoofchk = !!(vf->flags & BNXT_VF_SPOOFCHK); 146 if (!(vf->flags & BNXT_VF_LINK_FORCED)) 147 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO; 148 else if (vf->flags & BNXT_VF_LINK_UP) 149 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE; 150 else 151 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE; 152 153 return 0; 154 } 155 156 int bnxt_set_vf_mac(struct net_device *dev, int vf_id, u8 *mac) 157 { 158 struct hwrm_func_cfg_input req = {0}; 159 struct bnxt *bp = netdev_priv(dev); 160 struct bnxt_vf_info *vf; 161 int rc; 162 163 rc = bnxt_vf_ndo_prep(bp, vf_id); 164 if (rc) 165 return rc; 166 /* reject bc or mc mac addr, zero mac addr means allow 167 * VF to use its own mac addr 168 */ 169 if (is_multicast_ether_addr(mac)) { 170 netdev_err(dev, "Invalid VF ethernet address\n"); 171 return -EINVAL; 172 } 173 vf = &bp->pf.vf[vf_id]; 174 175 memcpy(vf->mac_addr, mac, ETH_ALEN); 176 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1); 177 req.fid = cpu_to_le16(vf->fw_fid); 178 req.flags = cpu_to_le32(vf->func_flags); 179 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR); 180 memcpy(req.dflt_mac_addr, mac, ETH_ALEN); 181 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 182 } 183 184 int bnxt_set_vf_vlan(struct net_device *dev, int vf_id, u16 vlan_id, u8 qos, 185 __be16 vlan_proto) 186 { 187 struct hwrm_func_cfg_input req = {0}; 188 struct bnxt *bp = netdev_priv(dev); 189 struct bnxt_vf_info *vf; 190 u16 vlan_tag; 191 int rc; 192 193 if (bp->hwrm_spec_code < 0x10201) 194 return -ENOTSUPP; 195 196 if (vlan_proto != htons(ETH_P_8021Q)) 197 return -EPROTONOSUPPORT; 198 199 rc = bnxt_vf_ndo_prep(bp, vf_id); 200 if (rc) 201 return rc; 202 203 /* TODO: needed to implement proper handling of user priority, 204 * currently fail the command if there is valid priority 205 */ 206 if (vlan_id > 4095 || qos) 207 return -EINVAL; 208 209 vf = &bp->pf.vf[vf_id]; 210 vlan_tag = vlan_id; 211 if (vlan_tag == vf->vlan) 212 return 0; 213 214 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1); 215 req.fid = cpu_to_le16(vf->fw_fid); 216 req.flags = cpu_to_le32(vf->func_flags); 217 req.dflt_vlan = cpu_to_le16(vlan_tag); 218 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN); 219 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 220 if (!rc) 221 vf->vlan = vlan_tag; 222 return rc; 223 } 224 225 int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate, 226 int max_tx_rate) 227 { 228 struct hwrm_func_cfg_input req = {0}; 229 struct bnxt *bp = netdev_priv(dev); 230 struct bnxt_vf_info *vf; 231 u32 pf_link_speed; 232 int rc; 233 234 rc = bnxt_vf_ndo_prep(bp, vf_id); 235 if (rc) 236 return rc; 237 238 vf = &bp->pf.vf[vf_id]; 239 pf_link_speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 240 if (max_tx_rate > pf_link_speed) { 241 netdev_info(bp->dev, "max tx rate %d exceed PF link speed for VF %d\n", 242 max_tx_rate, vf_id); 243 return -EINVAL; 244 } 245 246 if (min_tx_rate > pf_link_speed || min_tx_rate > max_tx_rate) { 247 netdev_info(bp->dev, "min tx rate %d is invalid for VF %d\n", 248 min_tx_rate, vf_id); 249 return -EINVAL; 250 } 251 if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate) 252 return 0; 253 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1); 254 req.fid = cpu_to_le16(vf->fw_fid); 255 req.flags = cpu_to_le32(vf->func_flags); 256 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW); 257 req.max_bw = cpu_to_le32(max_tx_rate); 258 req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MIN_BW); 259 req.min_bw = cpu_to_le32(min_tx_rate); 260 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 261 if (!rc) { 262 vf->min_tx_rate = min_tx_rate; 263 vf->max_tx_rate = max_tx_rate; 264 } 265 return rc; 266 } 267 268 int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link) 269 { 270 struct bnxt *bp = netdev_priv(dev); 271 struct bnxt_vf_info *vf; 272 int rc; 273 274 rc = bnxt_vf_ndo_prep(bp, vf_id); 275 if (rc) 276 return rc; 277 278 vf = &bp->pf.vf[vf_id]; 279 280 vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED); 281 switch (link) { 282 case IFLA_VF_LINK_STATE_AUTO: 283 vf->flags |= BNXT_VF_LINK_UP; 284 break; 285 case IFLA_VF_LINK_STATE_DISABLE: 286 vf->flags |= BNXT_VF_LINK_FORCED; 287 break; 288 case IFLA_VF_LINK_STATE_ENABLE: 289 vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED; 290 break; 291 default: 292 netdev_err(bp->dev, "Invalid link option\n"); 293 rc = -EINVAL; 294 break; 295 } 296 if (vf->flags & (BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED)) 297 rc = bnxt_hwrm_fwd_async_event_cmpl(bp, vf, 298 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE); 299 return rc; 300 } 301 302 static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs) 303 { 304 int i; 305 struct bnxt_vf_info *vf; 306 307 for (i = 0; i < num_vfs; i++) { 308 vf = &bp->pf.vf[i]; 309 memset(vf, 0, sizeof(*vf)); 310 } 311 return 0; 312 } 313 314 static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs) 315 { 316 int i, rc = 0; 317 struct bnxt_pf_info *pf = &bp->pf; 318 struct hwrm_func_vf_resc_free_input req = {0}; 319 320 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESC_FREE, -1, -1); 321 322 mutex_lock(&bp->hwrm_cmd_lock); 323 for (i = pf->first_vf_id; i < pf->first_vf_id + num_vfs; i++) { 324 req.vf_id = cpu_to_le16(i); 325 rc = _hwrm_send_message(bp, &req, sizeof(req), 326 HWRM_CMD_TIMEOUT); 327 if (rc) 328 break; 329 } 330 mutex_unlock(&bp->hwrm_cmd_lock); 331 return rc; 332 } 333 334 static void bnxt_free_vf_resources(struct bnxt *bp) 335 { 336 struct pci_dev *pdev = bp->pdev; 337 int i; 338 339 kfree(bp->pf.vf_event_bmap); 340 bp->pf.vf_event_bmap = NULL; 341 342 for (i = 0; i < 4; i++) { 343 if (bp->pf.hwrm_cmd_req_addr[i]) { 344 dma_free_coherent(&pdev->dev, BNXT_PAGE_SIZE, 345 bp->pf.hwrm_cmd_req_addr[i], 346 bp->pf.hwrm_cmd_req_dma_addr[i]); 347 bp->pf.hwrm_cmd_req_addr[i] = NULL; 348 } 349 } 350 351 kfree(bp->pf.vf); 352 bp->pf.vf = NULL; 353 } 354 355 static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs) 356 { 357 struct pci_dev *pdev = bp->pdev; 358 u32 nr_pages, size, i, j, k = 0; 359 360 bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL); 361 if (!bp->pf.vf) 362 return -ENOMEM; 363 364 bnxt_set_vf_attr(bp, num_vfs); 365 366 size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE; 367 nr_pages = size / BNXT_PAGE_SIZE; 368 if (size & (BNXT_PAGE_SIZE - 1)) 369 nr_pages++; 370 371 for (i = 0; i < nr_pages; i++) { 372 bp->pf.hwrm_cmd_req_addr[i] = 373 dma_alloc_coherent(&pdev->dev, BNXT_PAGE_SIZE, 374 &bp->pf.hwrm_cmd_req_dma_addr[i], 375 GFP_KERNEL); 376 377 if (!bp->pf.hwrm_cmd_req_addr[i]) 378 return -ENOMEM; 379 380 for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) { 381 struct bnxt_vf_info *vf = &bp->pf.vf[k]; 382 383 vf->hwrm_cmd_req_addr = bp->pf.hwrm_cmd_req_addr[i] + 384 j * BNXT_HWRM_REQ_MAX_SIZE; 385 vf->hwrm_cmd_req_dma_addr = 386 bp->pf.hwrm_cmd_req_dma_addr[i] + j * 387 BNXT_HWRM_REQ_MAX_SIZE; 388 k++; 389 } 390 } 391 392 /* Max 128 VF's */ 393 bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL); 394 if (!bp->pf.vf_event_bmap) 395 return -ENOMEM; 396 397 bp->pf.hwrm_cmd_req_pages = nr_pages; 398 return 0; 399 } 400 401 static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp) 402 { 403 struct hwrm_func_buf_rgtr_input req = {0}; 404 405 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BUF_RGTR, -1, -1); 406 407 req.req_buf_num_pages = cpu_to_le16(bp->pf.hwrm_cmd_req_pages); 408 req.req_buf_page_size = cpu_to_le16(BNXT_PAGE_SHIFT); 409 req.req_buf_len = cpu_to_le16(BNXT_HWRM_REQ_MAX_SIZE); 410 req.req_buf_page_addr0 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[0]); 411 req.req_buf_page_addr1 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[1]); 412 req.req_buf_page_addr2 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[2]); 413 req.req_buf_page_addr3 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[3]); 414 415 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 416 } 417 418 /* only call by PF to reserve resources for VF */ 419 static int bnxt_hwrm_func_cfg(struct bnxt *bp, int num_vfs) 420 { 421 u32 rc = 0, mtu, i; 422 u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics; 423 u16 vf_ring_grps; 424 struct hwrm_func_cfg_input req = {0}; 425 struct bnxt_pf_info *pf = &bp->pf; 426 int total_vf_tx_rings = 0; 427 428 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1); 429 430 /* Remaining rings are distributed equally amongs VF's for now */ 431 vf_cp_rings = (pf->max_cp_rings - bp->cp_nr_rings) / num_vfs; 432 vf_stat_ctx = (pf->max_stat_ctxs - bp->num_stat_ctxs) / num_vfs; 433 if (bp->flags & BNXT_FLAG_AGG_RINGS) 434 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings * 2) / 435 num_vfs; 436 else 437 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings) / num_vfs; 438 vf_ring_grps = (bp->pf.max_hw_ring_grps - bp->rx_nr_rings) / num_vfs; 439 vf_tx_rings = (pf->max_tx_rings - bp->tx_nr_rings) / num_vfs; 440 vf_vnics = (pf->max_vnics - bp->nr_vnics) / num_vfs; 441 vf_vnics = min_t(u16, vf_vnics, vf_rx_rings); 442 443 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MTU | 444 FUNC_CFG_REQ_ENABLES_MRU | 445 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS | 446 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS | 447 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS | 448 FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS | 449 FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS | 450 FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS | 451 FUNC_CFG_REQ_ENABLES_NUM_VNICS | 452 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS); 453 454 mtu = bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN; 455 req.mru = cpu_to_le16(mtu); 456 req.mtu = cpu_to_le16(mtu); 457 458 req.num_rsscos_ctxs = cpu_to_le16(1); 459 req.num_cmpl_rings = cpu_to_le16(vf_cp_rings); 460 req.num_tx_rings = cpu_to_le16(vf_tx_rings); 461 req.num_rx_rings = cpu_to_le16(vf_rx_rings); 462 req.num_hw_ring_grps = cpu_to_le16(vf_ring_grps); 463 req.num_l2_ctxs = cpu_to_le16(4); 464 465 req.num_vnics = cpu_to_le16(vf_vnics); 466 /* FIXME spec currently uses 1 bit for stats ctx */ 467 req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx); 468 469 mutex_lock(&bp->hwrm_cmd_lock); 470 for (i = 0; i < num_vfs; i++) { 471 int vf_tx_rsvd = vf_tx_rings; 472 473 req.fid = cpu_to_le16(pf->first_vf_id + i); 474 rc = _hwrm_send_message(bp, &req, sizeof(req), 475 HWRM_CMD_TIMEOUT); 476 if (rc) 477 break; 478 pf->active_vfs = i + 1; 479 pf->vf[i].fw_fid = le16_to_cpu(req.fid); 480 rc = __bnxt_hwrm_get_tx_rings(bp, pf->vf[i].fw_fid, 481 &vf_tx_rsvd); 482 if (rc) 483 break; 484 total_vf_tx_rings += vf_tx_rsvd; 485 } 486 mutex_unlock(&bp->hwrm_cmd_lock); 487 if (!rc) { 488 pf->max_tx_rings -= total_vf_tx_rings; 489 pf->max_rx_rings -= vf_rx_rings * num_vfs; 490 pf->max_hw_ring_grps -= vf_ring_grps * num_vfs; 491 pf->max_cp_rings -= vf_cp_rings * num_vfs; 492 pf->max_rsscos_ctxs -= num_vfs; 493 pf->max_stat_ctxs -= vf_stat_ctx * num_vfs; 494 pf->max_vnics -= vf_vnics * num_vfs; 495 } 496 return rc; 497 } 498 499 static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs) 500 { 501 int rc = 0, vfs_supported; 502 int min_rx_rings, min_tx_rings, min_rss_ctxs; 503 int tx_ok = 0, rx_ok = 0, rss_ok = 0; 504 505 /* Check if we can enable requested num of vf's. At a mininum 506 * we require 1 RX 1 TX rings for each VF. In this minimum conf 507 * features like TPA will not be available. 508 */ 509 vfs_supported = *num_vfs; 510 511 while (vfs_supported) { 512 min_rx_rings = vfs_supported; 513 min_tx_rings = vfs_supported; 514 min_rss_ctxs = vfs_supported; 515 516 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 517 if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >= 518 min_rx_rings) 519 rx_ok = 1; 520 } else { 521 if (bp->pf.max_rx_rings - bp->rx_nr_rings >= 522 min_rx_rings) 523 rx_ok = 1; 524 } 525 if (bp->pf.max_vnics - bp->nr_vnics < min_rx_rings) 526 rx_ok = 0; 527 528 if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings) 529 tx_ok = 1; 530 531 if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs) 532 rss_ok = 1; 533 534 if (tx_ok && rx_ok && rss_ok) 535 break; 536 537 vfs_supported--; 538 } 539 540 if (!vfs_supported) { 541 netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n"); 542 return -EINVAL; 543 } 544 545 if (vfs_supported != *num_vfs) { 546 netdev_info(bp->dev, "Requested VFs %d, can enable %d\n", 547 *num_vfs, vfs_supported); 548 *num_vfs = vfs_supported; 549 } 550 551 rc = bnxt_alloc_vf_resources(bp, *num_vfs); 552 if (rc) 553 goto err_out1; 554 555 /* Reserve resources for VFs */ 556 rc = bnxt_hwrm_func_cfg(bp, *num_vfs); 557 if (rc) 558 goto err_out2; 559 560 /* Register buffers for VFs */ 561 rc = bnxt_hwrm_func_buf_rgtr(bp); 562 if (rc) 563 goto err_out2; 564 565 bnxt_ulp_sriov_cfg(bp, *num_vfs); 566 567 rc = pci_enable_sriov(bp->pdev, *num_vfs); 568 if (rc) 569 goto err_out2; 570 571 return 0; 572 573 err_out2: 574 /* Free the resources reserved for various VF's */ 575 bnxt_hwrm_func_vf_resource_free(bp, *num_vfs); 576 577 err_out1: 578 bnxt_free_vf_resources(bp); 579 580 return rc; 581 } 582 583 void bnxt_sriov_disable(struct bnxt *bp) 584 { 585 u16 num_vfs = pci_num_vf(bp->pdev); 586 587 if (!num_vfs) 588 return; 589 590 if (pci_vfs_assigned(bp->pdev)) { 591 bnxt_hwrm_fwd_async_event_cmpl( 592 bp, NULL, ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD); 593 netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n", 594 num_vfs); 595 } else { 596 pci_disable_sriov(bp->pdev); 597 /* Free the HW resources reserved for various VF's */ 598 bnxt_hwrm_func_vf_resource_free(bp, num_vfs); 599 } 600 601 bnxt_free_vf_resources(bp); 602 603 bp->pf.active_vfs = 0; 604 /* Reclaim all resources for the PF. */ 605 rtnl_lock(); 606 bnxt_restore_pf_fw_resources(bp); 607 rtnl_unlock(); 608 609 bnxt_ulp_sriov_cfg(bp, 0); 610 } 611 612 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs) 613 { 614 struct net_device *dev = pci_get_drvdata(pdev); 615 struct bnxt *bp = netdev_priv(dev); 616 617 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) { 618 netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n"); 619 return 0; 620 } 621 622 rtnl_lock(); 623 if (!netif_running(dev)) { 624 netdev_warn(dev, "Reject SRIOV config request since if is down!\n"); 625 rtnl_unlock(); 626 return 0; 627 } 628 bp->sriov_cfg = true; 629 rtnl_unlock(); 630 631 if (pci_vfs_assigned(bp->pdev)) { 632 netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n"); 633 num_vfs = 0; 634 goto sriov_cfg_exit; 635 } 636 637 /* Check if enabled VFs is same as requested */ 638 if (num_vfs && num_vfs == bp->pf.active_vfs) 639 goto sriov_cfg_exit; 640 641 /* if there are previous existing VFs, clean them up */ 642 bnxt_sriov_disable(bp); 643 if (!num_vfs) 644 goto sriov_cfg_exit; 645 646 bnxt_sriov_enable(bp, &num_vfs); 647 648 sriov_cfg_exit: 649 bp->sriov_cfg = false; 650 wake_up(&bp->sriov_cfg_wait); 651 652 return num_vfs; 653 } 654 655 static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf, 656 void *encap_resp, __le64 encap_resp_addr, 657 __le16 encap_resp_cpr, u32 msg_size) 658 { 659 int rc = 0; 660 struct hwrm_fwd_resp_input req = {0}; 661 struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr; 662 663 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1); 664 665 /* Set the new target id */ 666 req.target_id = cpu_to_le16(vf->fw_fid); 667 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid); 668 req.encap_resp_len = cpu_to_le16(msg_size); 669 req.encap_resp_addr = encap_resp_addr; 670 req.encap_resp_cmpl_ring = encap_resp_cpr; 671 memcpy(req.encap_resp, encap_resp, msg_size); 672 673 mutex_lock(&bp->hwrm_cmd_lock); 674 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 675 676 if (rc) { 677 netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc); 678 goto fwd_resp_exit; 679 } 680 681 if (resp->error_code) { 682 netdev_err(bp->dev, "hwrm_fwd_resp error %d\n", 683 resp->error_code); 684 rc = -1; 685 } 686 687 fwd_resp_exit: 688 mutex_unlock(&bp->hwrm_cmd_lock); 689 return rc; 690 } 691 692 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf, 693 u32 msg_size) 694 { 695 int rc = 0; 696 struct hwrm_reject_fwd_resp_input req = {0}; 697 struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr; 698 699 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1); 700 /* Set the new target id */ 701 req.target_id = cpu_to_le16(vf->fw_fid); 702 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid); 703 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size); 704 705 mutex_lock(&bp->hwrm_cmd_lock); 706 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 707 708 if (rc) { 709 netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc); 710 goto fwd_err_resp_exit; 711 } 712 713 if (resp->error_code) { 714 netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n", 715 resp->error_code); 716 rc = -1; 717 } 718 719 fwd_err_resp_exit: 720 mutex_unlock(&bp->hwrm_cmd_lock); 721 return rc; 722 } 723 724 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf, 725 u32 msg_size) 726 { 727 int rc = 0; 728 struct hwrm_exec_fwd_resp_input req = {0}; 729 struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr; 730 731 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1); 732 /* Set the new target id */ 733 req.target_id = cpu_to_le16(vf->fw_fid); 734 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid); 735 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size); 736 737 mutex_lock(&bp->hwrm_cmd_lock); 738 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 739 740 if (rc) { 741 netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc); 742 goto exec_fwd_resp_exit; 743 } 744 745 if (resp->error_code) { 746 netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n", 747 resp->error_code); 748 rc = -1; 749 } 750 751 exec_fwd_resp_exit: 752 mutex_unlock(&bp->hwrm_cmd_lock); 753 return rc; 754 } 755 756 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf) 757 { 758 u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input); 759 struct hwrm_cfa_l2_filter_alloc_input *req = 760 (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr; 761 762 if (!is_valid_ether_addr(vf->mac_addr) || 763 ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr)) 764 return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size); 765 else 766 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size); 767 } 768 769 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf) 770 { 771 int rc = 0; 772 773 if (!(vf->flags & BNXT_VF_LINK_FORCED)) { 774 /* real link */ 775 rc = bnxt_hwrm_exec_fwd_resp( 776 bp, vf, sizeof(struct hwrm_port_phy_qcfg_input)); 777 } else { 778 struct hwrm_port_phy_qcfg_output phy_qcfg_resp; 779 struct hwrm_port_phy_qcfg_input *phy_qcfg_req; 780 781 phy_qcfg_req = 782 (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr; 783 mutex_lock(&bp->hwrm_cmd_lock); 784 memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp, 785 sizeof(phy_qcfg_resp)); 786 mutex_unlock(&bp->hwrm_cmd_lock); 787 phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id; 788 789 if (vf->flags & BNXT_VF_LINK_UP) { 790 /* if physical link is down, force link up on VF */ 791 if (phy_qcfg_resp.link != 792 PORT_PHY_QCFG_RESP_LINK_LINK) { 793 phy_qcfg_resp.link = 794 PORT_PHY_QCFG_RESP_LINK_LINK; 795 phy_qcfg_resp.link_speed = cpu_to_le16( 796 PORT_PHY_QCFG_RESP_LINK_SPEED_10GB); 797 phy_qcfg_resp.duplex = 798 PORT_PHY_QCFG_RESP_DUPLEX_FULL; 799 phy_qcfg_resp.pause = 800 (PORT_PHY_QCFG_RESP_PAUSE_TX | 801 PORT_PHY_QCFG_RESP_PAUSE_RX); 802 } 803 } else { 804 /* force link down */ 805 phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK; 806 phy_qcfg_resp.link_speed = 0; 807 phy_qcfg_resp.duplex = PORT_PHY_QCFG_RESP_DUPLEX_HALF; 808 phy_qcfg_resp.pause = 0; 809 } 810 rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp, 811 phy_qcfg_req->resp_addr, 812 phy_qcfg_req->cmpl_ring, 813 sizeof(phy_qcfg_resp)); 814 } 815 return rc; 816 } 817 818 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf) 819 { 820 int rc = 0; 821 struct input *encap_req = vf->hwrm_cmd_req_addr; 822 u32 req_type = le16_to_cpu(encap_req->req_type); 823 824 switch (req_type) { 825 case HWRM_CFA_L2_FILTER_ALLOC: 826 rc = bnxt_vf_validate_set_mac(bp, vf); 827 break; 828 case HWRM_FUNC_CFG: 829 /* TODO Validate if VF is allowed to change mac address, 830 * mtu, num of rings etc 831 */ 832 rc = bnxt_hwrm_exec_fwd_resp( 833 bp, vf, sizeof(struct hwrm_func_cfg_input)); 834 break; 835 case HWRM_PORT_PHY_QCFG: 836 rc = bnxt_vf_set_link(bp, vf); 837 break; 838 default: 839 break; 840 } 841 return rc; 842 } 843 844 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp) 845 { 846 u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id; 847 848 /* Scan through VF's and process commands */ 849 while (1) { 850 vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i); 851 if (vf_id >= active_vfs) 852 break; 853 854 clear_bit(vf_id, bp->pf.vf_event_bmap); 855 bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]); 856 i = vf_id + 1; 857 } 858 } 859 860 void bnxt_update_vf_mac(struct bnxt *bp) 861 { 862 struct hwrm_func_qcaps_input req = {0}; 863 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr; 864 865 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1); 866 req.fid = cpu_to_le16(0xffff); 867 868 mutex_lock(&bp->hwrm_cmd_lock); 869 if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT)) 870 goto update_vf_mac_exit; 871 872 /* Store MAC address from the firmware. There are 2 cases: 873 * 1. MAC address is valid. It is assigned from the PF and we 874 * need to override the current VF MAC address with it. 875 * 2. MAC address is zero. The VF will use a random MAC address by 876 * default but the stored zero MAC will allow the VF user to change 877 * the random MAC address using ndo_set_mac_address() if he wants. 878 */ 879 if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr)) 880 memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN); 881 882 /* overwrite netdev dev_addr with admin VF MAC */ 883 if (is_valid_ether_addr(bp->vf.mac_addr)) 884 memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN); 885 update_vf_mac_exit: 886 mutex_unlock(&bp->hwrm_cmd_lock); 887 } 888 889 int bnxt_approve_mac(struct bnxt *bp, u8 *mac) 890 { 891 struct hwrm_func_vf_cfg_input req = {0}; 892 int rc = 0; 893 894 if (!BNXT_VF(bp)) 895 return 0; 896 897 if (bp->hwrm_spec_code < 0x10202) { 898 if (is_valid_ether_addr(bp->vf.mac_addr)) 899 rc = -EADDRNOTAVAIL; 900 goto mac_done; 901 } 902 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1); 903 req.enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR); 904 memcpy(req.dflt_mac_addr, mac, ETH_ALEN); 905 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 906 mac_done: 907 if (rc) { 908 rc = -EADDRNOTAVAIL; 909 netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n", 910 mac); 911 } 912 return rc; 913 } 914 #else 915 916 void bnxt_sriov_disable(struct bnxt *bp) 917 { 918 } 919 920 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp) 921 { 922 netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n"); 923 } 924 925 void bnxt_update_vf_mac(struct bnxt *bp) 926 { 927 } 928 929 int bnxt_approve_mac(struct bnxt *bp, u8 *mac) 930 { 931 return 0; 932 } 933 #endif 934