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