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 506 /* Check if we can enable requested num of vf's. At a mininum 507 * we require 1 RX 1 TX rings for each VF. In this minimum conf 508 * features like TPA will not be available. 509 */ 510 vfs_supported = *num_vfs; 511 512 while (vfs_supported) { 513 min_rx_rings = vfs_supported; 514 min_tx_rings = vfs_supported; 515 min_rss_ctxs = vfs_supported; 516 517 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 518 if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >= 519 min_rx_rings) 520 rx_ok = 1; 521 } else { 522 if (bp->pf.max_rx_rings - bp->rx_nr_rings >= 523 min_rx_rings) 524 rx_ok = 1; 525 } 526 if (bp->pf.max_vnics - bp->nr_vnics < min_rx_rings) 527 rx_ok = 0; 528 529 if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings) 530 tx_ok = 1; 531 532 if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs) 533 rss_ok = 1; 534 535 if (tx_ok && rx_ok && rss_ok) 536 break; 537 538 vfs_supported--; 539 } 540 541 if (!vfs_supported) { 542 netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n"); 543 return -EINVAL; 544 } 545 546 if (vfs_supported != *num_vfs) { 547 netdev_info(bp->dev, "Requested VFs %d, can enable %d\n", 548 *num_vfs, vfs_supported); 549 *num_vfs = vfs_supported; 550 } 551 552 rc = bnxt_alloc_vf_resources(bp, *num_vfs); 553 if (rc) 554 goto err_out1; 555 556 /* Reserve resources for VFs */ 557 rc = bnxt_hwrm_func_cfg(bp, *num_vfs); 558 if (rc) 559 goto err_out2; 560 561 /* Register buffers for VFs */ 562 rc = bnxt_hwrm_func_buf_rgtr(bp); 563 if (rc) 564 goto err_out2; 565 566 bnxt_ulp_sriov_cfg(bp, *num_vfs); 567 568 rc = pci_enable_sriov(bp->pdev, *num_vfs); 569 if (rc) 570 goto err_out2; 571 572 return 0; 573 574 err_out2: 575 /* Free the resources reserved for various VF's */ 576 bnxt_hwrm_func_vf_resource_free(bp, *num_vfs); 577 578 err_out1: 579 bnxt_free_vf_resources(bp); 580 581 return rc; 582 } 583 584 void bnxt_sriov_disable(struct bnxt *bp) 585 { 586 u16 num_vfs = pci_num_vf(bp->pdev); 587 588 if (!num_vfs) 589 return; 590 591 /* synchronize VF and VF-rep create and destroy */ 592 mutex_lock(&bp->sriov_lock); 593 bnxt_vf_reps_destroy(bp); 594 595 if (pci_vfs_assigned(bp->pdev)) { 596 bnxt_hwrm_fwd_async_event_cmpl( 597 bp, NULL, ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD); 598 netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n", 599 num_vfs); 600 } else { 601 pci_disable_sriov(bp->pdev); 602 /* Free the HW resources reserved for various VF's */ 603 bnxt_hwrm_func_vf_resource_free(bp, num_vfs); 604 } 605 mutex_unlock(&bp->sriov_lock); 606 607 bnxt_free_vf_resources(bp); 608 609 bp->pf.active_vfs = 0; 610 /* Reclaim all resources for the PF. */ 611 rtnl_lock(); 612 bnxt_restore_pf_fw_resources(bp); 613 rtnl_unlock(); 614 615 bnxt_ulp_sriov_cfg(bp, 0); 616 } 617 618 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs) 619 { 620 struct net_device *dev = pci_get_drvdata(pdev); 621 struct bnxt *bp = netdev_priv(dev); 622 623 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) { 624 netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n"); 625 return 0; 626 } 627 628 rtnl_lock(); 629 if (!netif_running(dev)) { 630 netdev_warn(dev, "Reject SRIOV config request since if is down!\n"); 631 rtnl_unlock(); 632 return 0; 633 } 634 bp->sriov_cfg = true; 635 rtnl_unlock(); 636 637 if (pci_vfs_assigned(bp->pdev)) { 638 netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n"); 639 num_vfs = 0; 640 goto sriov_cfg_exit; 641 } 642 643 /* Check if enabled VFs is same as requested */ 644 if (num_vfs && num_vfs == bp->pf.active_vfs) 645 goto sriov_cfg_exit; 646 647 /* if there are previous existing VFs, clean them up */ 648 bnxt_sriov_disable(bp); 649 if (!num_vfs) 650 goto sriov_cfg_exit; 651 652 bnxt_sriov_enable(bp, &num_vfs); 653 654 sriov_cfg_exit: 655 bp->sriov_cfg = false; 656 wake_up(&bp->sriov_cfg_wait); 657 658 return num_vfs; 659 } 660 661 static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf, 662 void *encap_resp, __le64 encap_resp_addr, 663 __le16 encap_resp_cpr, u32 msg_size) 664 { 665 int rc = 0; 666 struct hwrm_fwd_resp_input req = {0}; 667 struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr; 668 669 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1); 670 671 /* Set the new target id */ 672 req.target_id = cpu_to_le16(vf->fw_fid); 673 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid); 674 req.encap_resp_len = cpu_to_le16(msg_size); 675 req.encap_resp_addr = encap_resp_addr; 676 req.encap_resp_cmpl_ring = encap_resp_cpr; 677 memcpy(req.encap_resp, encap_resp, msg_size); 678 679 mutex_lock(&bp->hwrm_cmd_lock); 680 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 681 682 if (rc) { 683 netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc); 684 goto fwd_resp_exit; 685 } 686 687 if (resp->error_code) { 688 netdev_err(bp->dev, "hwrm_fwd_resp error %d\n", 689 resp->error_code); 690 rc = -1; 691 } 692 693 fwd_resp_exit: 694 mutex_unlock(&bp->hwrm_cmd_lock); 695 return rc; 696 } 697 698 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf, 699 u32 msg_size) 700 { 701 int rc = 0; 702 struct hwrm_reject_fwd_resp_input req = {0}; 703 struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr; 704 705 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1); 706 /* Set the new target id */ 707 req.target_id = cpu_to_le16(vf->fw_fid); 708 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid); 709 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size); 710 711 mutex_lock(&bp->hwrm_cmd_lock); 712 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 713 714 if (rc) { 715 netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc); 716 goto fwd_err_resp_exit; 717 } 718 719 if (resp->error_code) { 720 netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n", 721 resp->error_code); 722 rc = -1; 723 } 724 725 fwd_err_resp_exit: 726 mutex_unlock(&bp->hwrm_cmd_lock); 727 return rc; 728 } 729 730 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf, 731 u32 msg_size) 732 { 733 int rc = 0; 734 struct hwrm_exec_fwd_resp_input req = {0}; 735 struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr; 736 737 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1); 738 /* Set the new target id */ 739 req.target_id = cpu_to_le16(vf->fw_fid); 740 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid); 741 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size); 742 743 mutex_lock(&bp->hwrm_cmd_lock); 744 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 745 746 if (rc) { 747 netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc); 748 goto exec_fwd_resp_exit; 749 } 750 751 if (resp->error_code) { 752 netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n", 753 resp->error_code); 754 rc = -1; 755 } 756 757 exec_fwd_resp_exit: 758 mutex_unlock(&bp->hwrm_cmd_lock); 759 return rc; 760 } 761 762 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf) 763 { 764 u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input); 765 struct hwrm_cfa_l2_filter_alloc_input *req = 766 (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr; 767 768 if (!is_valid_ether_addr(vf->mac_addr) || 769 ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr)) 770 return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size); 771 else 772 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size); 773 } 774 775 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf) 776 { 777 int rc = 0; 778 779 if (!(vf->flags & BNXT_VF_LINK_FORCED)) { 780 /* real link */ 781 rc = bnxt_hwrm_exec_fwd_resp( 782 bp, vf, sizeof(struct hwrm_port_phy_qcfg_input)); 783 } else { 784 struct hwrm_port_phy_qcfg_output phy_qcfg_resp; 785 struct hwrm_port_phy_qcfg_input *phy_qcfg_req; 786 787 phy_qcfg_req = 788 (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr; 789 mutex_lock(&bp->hwrm_cmd_lock); 790 memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp, 791 sizeof(phy_qcfg_resp)); 792 mutex_unlock(&bp->hwrm_cmd_lock); 793 phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id; 794 795 if (vf->flags & BNXT_VF_LINK_UP) { 796 /* if physical link is down, force link up on VF */ 797 if (phy_qcfg_resp.link != 798 PORT_PHY_QCFG_RESP_LINK_LINK) { 799 phy_qcfg_resp.link = 800 PORT_PHY_QCFG_RESP_LINK_LINK; 801 phy_qcfg_resp.link_speed = cpu_to_le16( 802 PORT_PHY_QCFG_RESP_LINK_SPEED_10GB); 803 phy_qcfg_resp.duplex_cfg = 804 PORT_PHY_QCFG_RESP_DUPLEX_CFG_FULL; 805 phy_qcfg_resp.duplex_state = 806 PORT_PHY_QCFG_RESP_DUPLEX_STATE_FULL; 807 phy_qcfg_resp.pause = 808 (PORT_PHY_QCFG_RESP_PAUSE_TX | 809 PORT_PHY_QCFG_RESP_PAUSE_RX); 810 } 811 } else { 812 /* force link down */ 813 phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK; 814 phy_qcfg_resp.link_speed = 0; 815 phy_qcfg_resp.duplex_state = 816 PORT_PHY_QCFG_RESP_DUPLEX_STATE_HALF; 817 phy_qcfg_resp.pause = 0; 818 } 819 rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp, 820 phy_qcfg_req->resp_addr, 821 phy_qcfg_req->cmpl_ring, 822 sizeof(phy_qcfg_resp)); 823 } 824 return rc; 825 } 826 827 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf) 828 { 829 int rc = 0; 830 struct input *encap_req = vf->hwrm_cmd_req_addr; 831 u32 req_type = le16_to_cpu(encap_req->req_type); 832 833 switch (req_type) { 834 case HWRM_CFA_L2_FILTER_ALLOC: 835 rc = bnxt_vf_validate_set_mac(bp, vf); 836 break; 837 case HWRM_FUNC_CFG: 838 /* TODO Validate if VF is allowed to change mac address, 839 * mtu, num of rings etc 840 */ 841 rc = bnxt_hwrm_exec_fwd_resp( 842 bp, vf, sizeof(struct hwrm_func_cfg_input)); 843 break; 844 case HWRM_PORT_PHY_QCFG: 845 rc = bnxt_vf_set_link(bp, vf); 846 break; 847 default: 848 break; 849 } 850 return rc; 851 } 852 853 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp) 854 { 855 u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id; 856 857 /* Scan through VF's and process commands */ 858 while (1) { 859 vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i); 860 if (vf_id >= active_vfs) 861 break; 862 863 clear_bit(vf_id, bp->pf.vf_event_bmap); 864 bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]); 865 i = vf_id + 1; 866 } 867 } 868 869 void bnxt_update_vf_mac(struct bnxt *bp) 870 { 871 struct hwrm_func_qcaps_input req = {0}; 872 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr; 873 874 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1); 875 req.fid = cpu_to_le16(0xffff); 876 877 mutex_lock(&bp->hwrm_cmd_lock); 878 if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT)) 879 goto update_vf_mac_exit; 880 881 /* Store MAC address from the firmware. There are 2 cases: 882 * 1. MAC address is valid. It is assigned from the PF and we 883 * need to override the current VF MAC address with it. 884 * 2. MAC address is zero. The VF will use a random MAC address by 885 * default but the stored zero MAC will allow the VF user to change 886 * the random MAC address using ndo_set_mac_address() if he wants. 887 */ 888 if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr)) 889 memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN); 890 891 /* overwrite netdev dev_addr with admin VF MAC */ 892 if (is_valid_ether_addr(bp->vf.mac_addr)) 893 memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN); 894 update_vf_mac_exit: 895 mutex_unlock(&bp->hwrm_cmd_lock); 896 } 897 898 int bnxt_approve_mac(struct bnxt *bp, u8 *mac) 899 { 900 struct hwrm_func_vf_cfg_input req = {0}; 901 int rc = 0; 902 903 if (!BNXT_VF(bp)) 904 return 0; 905 906 if (bp->hwrm_spec_code < 0x10202) { 907 if (is_valid_ether_addr(bp->vf.mac_addr)) 908 rc = -EADDRNOTAVAIL; 909 goto mac_done; 910 } 911 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1); 912 req.enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR); 913 memcpy(req.dflt_mac_addr, mac, ETH_ALEN); 914 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 915 mac_done: 916 if (rc) { 917 rc = -EADDRNOTAVAIL; 918 netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n", 919 mac); 920 } 921 return rc; 922 } 923 #else 924 925 void bnxt_sriov_disable(struct bnxt *bp) 926 { 927 } 928 929 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp) 930 { 931 netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n"); 932 } 933 934 void bnxt_update_vf_mac(struct bnxt *bp) 935 { 936 } 937 938 int bnxt_approve_mac(struct bnxt *bp, u8 *mac) 939 { 940 return 0; 941 } 942 #endif 943