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