1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2014-2016 Broadcom Corporation
4 * Copyright (c) 2016-2018 Broadcom Limited
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.
11 #include <linux/ethtool.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/if_vlan.h>
16 #include <linux/interrupt.h>
17 #include <linux/etherdevice.h>
21 #include "bnxt_sriov.h"
23 #include "bnxt_ethtool.h"
25 #ifdef CONFIG_BNXT_SRIOV
26 static int bnxt_hwrm_fwd_async_event_cmpl(struct bnxt *bp,
27 struct bnxt_vf_info *vf, u16 event_id)
29 struct hwrm_fwd_async_event_cmpl_input req = {0};
30 struct hwrm_async_event_cmpl *async_cmpl;
33 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_ASYNC_EVENT_CMPL, -1, -1);
35 req.encap_async_event_target_id = cpu_to_le16(vf->fw_fid);
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);
43 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
45 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl failed. rc:%d\n",
50 static int bnxt_vf_ndo_prep(struct bnxt *bp, int vf_id)
52 if (!bp->pf.active_vfs) {
53 netdev_err(bp->dev, "vf ndo called though sriov is disabled\n");
56 if (vf_id >= bp->pf.active_vfs) {
57 netdev_err(bp->dev, "Invalid VF id %d\n", vf_id);
63 int bnxt_set_vf_spoofchk(struct net_device *dev, int vf_id, bool setting)
65 struct hwrm_func_cfg_input req = {0};
66 struct bnxt *bp = netdev_priv(dev);
67 struct bnxt_vf_info *vf;
68 bool old_setting = false;
72 if (bp->hwrm_spec_code < 0x10701)
75 rc = bnxt_vf_ndo_prep(bp, vf_id);
79 vf = &bp->pf.vf[vf_id];
80 if (vf->flags & BNXT_VF_SPOOFCHK)
82 if (old_setting == setting)
86 func_flags = FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK_ENABLE;
88 func_flags = FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK_DISABLE;
89 /*TODO: if the driver supports VLAN filter on guest VLAN,
90 * the spoof check should also include vlan anti-spoofing
92 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
93 req.fid = cpu_to_le16(vf->fw_fid);
94 req.flags = cpu_to_le32(func_flags);
95 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
98 vf->flags |= BNXT_VF_SPOOFCHK;
100 vf->flags &= ~BNXT_VF_SPOOFCHK;
105 static int bnxt_hwrm_func_qcfg_flags(struct bnxt *bp, struct bnxt_vf_info *vf)
107 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
108 struct hwrm_func_qcfg_input req = {0};
111 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
112 req.fid = cpu_to_le16(BNXT_PF(bp) ? vf->fw_fid : 0xffff);
113 mutex_lock(&bp->hwrm_cmd_lock);
114 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
116 mutex_unlock(&bp->hwrm_cmd_lock);
119 vf->func_qcfg_flags = le16_to_cpu(resp->flags);
120 mutex_unlock(&bp->hwrm_cmd_lock);
124 bool bnxt_is_trusted_vf(struct bnxt *bp, struct bnxt_vf_info *vf)
126 if (BNXT_PF(bp) && !(bp->fw_cap & BNXT_FW_CAP_TRUSTED_VF))
127 return !!(vf->flags & BNXT_VF_TRUST);
129 bnxt_hwrm_func_qcfg_flags(bp, vf);
130 return !!(vf->func_qcfg_flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF);
133 static int bnxt_hwrm_set_trusted_vf(struct bnxt *bp, struct bnxt_vf_info *vf)
135 struct hwrm_func_cfg_input req = {0};
137 if (!(bp->fw_cap & BNXT_FW_CAP_TRUSTED_VF))
140 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
141 req.fid = cpu_to_le16(vf->fw_fid);
142 if (vf->flags & BNXT_VF_TRUST)
143 req.flags = cpu_to_le32(FUNC_CFG_REQ_FLAGS_TRUSTED_VF_ENABLE);
145 req.flags = cpu_to_le32(FUNC_CFG_REQ_FLAGS_TRUSTED_VF_DISABLE);
146 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
149 int bnxt_set_vf_trust(struct net_device *dev, int vf_id, bool trusted)
151 struct bnxt *bp = netdev_priv(dev);
152 struct bnxt_vf_info *vf;
154 if (bnxt_vf_ndo_prep(bp, vf_id))
157 vf = &bp->pf.vf[vf_id];
159 vf->flags |= BNXT_VF_TRUST;
161 vf->flags &= ~BNXT_VF_TRUST;
163 bnxt_hwrm_set_trusted_vf(bp, vf);
167 int bnxt_get_vf_config(struct net_device *dev, int vf_id,
168 struct ifla_vf_info *ivi)
170 struct bnxt *bp = netdev_priv(dev);
171 struct bnxt_vf_info *vf;
174 rc = bnxt_vf_ndo_prep(bp, vf_id);
179 vf = &bp->pf.vf[vf_id];
181 if (is_valid_ether_addr(vf->mac_addr))
182 memcpy(&ivi->mac, vf->mac_addr, ETH_ALEN);
184 memcpy(&ivi->mac, vf->vf_mac_addr, ETH_ALEN);
185 ivi->max_tx_rate = vf->max_tx_rate;
186 ivi->min_tx_rate = vf->min_tx_rate;
187 ivi->vlan = vf->vlan;
188 if (vf->flags & BNXT_VF_QOS)
189 ivi->qos = vf->vlan >> VLAN_PRIO_SHIFT;
192 ivi->spoofchk = !!(vf->flags & BNXT_VF_SPOOFCHK);
193 ivi->trusted = bnxt_is_trusted_vf(bp, vf);
194 if (!(vf->flags & BNXT_VF_LINK_FORCED))
195 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
196 else if (vf->flags & BNXT_VF_LINK_UP)
197 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
199 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
204 int bnxt_set_vf_mac(struct net_device *dev, int vf_id, u8 *mac)
206 struct hwrm_func_cfg_input req = {0};
207 struct bnxt *bp = netdev_priv(dev);
208 struct bnxt_vf_info *vf;
211 rc = bnxt_vf_ndo_prep(bp, vf_id);
214 /* reject bc or mc mac addr, zero mac addr means allow
215 * VF to use its own mac addr
217 if (is_multicast_ether_addr(mac)) {
218 netdev_err(dev, "Invalid VF ethernet address\n");
221 vf = &bp->pf.vf[vf_id];
223 memcpy(vf->mac_addr, mac, ETH_ALEN);
224 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
225 req.fid = cpu_to_le16(vf->fw_fid);
226 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
227 memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
228 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
231 int bnxt_set_vf_vlan(struct net_device *dev, int vf_id, u16 vlan_id, u8 qos,
234 struct hwrm_func_cfg_input req = {0};
235 struct bnxt *bp = netdev_priv(dev);
236 struct bnxt_vf_info *vf;
240 if (bp->hwrm_spec_code < 0x10201)
243 if (vlan_proto != htons(ETH_P_8021Q))
244 return -EPROTONOSUPPORT;
246 rc = bnxt_vf_ndo_prep(bp, vf_id);
250 /* TODO: needed to implement proper handling of user priority,
251 * currently fail the command if there is valid priority
253 if (vlan_id > 4095 || qos)
256 vf = &bp->pf.vf[vf_id];
258 if (vlan_tag == vf->vlan)
261 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
262 req.fid = cpu_to_le16(vf->fw_fid);
263 req.dflt_vlan = cpu_to_le16(vlan_tag);
264 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
265 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
271 int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate,
274 struct hwrm_func_cfg_input req = {0};
275 struct bnxt *bp = netdev_priv(dev);
276 struct bnxt_vf_info *vf;
280 rc = bnxt_vf_ndo_prep(bp, vf_id);
284 vf = &bp->pf.vf[vf_id];
285 pf_link_speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
286 if (max_tx_rate > pf_link_speed) {
287 netdev_info(bp->dev, "max tx rate %d exceed PF link speed for VF %d\n",
292 if (min_tx_rate > pf_link_speed || min_tx_rate > max_tx_rate) {
293 netdev_info(bp->dev, "min tx rate %d is invalid for VF %d\n",
297 if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate)
299 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
300 req.fid = cpu_to_le16(vf->fw_fid);
301 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW);
302 req.max_bw = cpu_to_le32(max_tx_rate);
303 req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MIN_BW);
304 req.min_bw = cpu_to_le32(min_tx_rate);
305 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
307 vf->min_tx_rate = min_tx_rate;
308 vf->max_tx_rate = max_tx_rate;
313 int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link)
315 struct bnxt *bp = netdev_priv(dev);
316 struct bnxt_vf_info *vf;
319 rc = bnxt_vf_ndo_prep(bp, vf_id);
323 vf = &bp->pf.vf[vf_id];
325 vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED);
327 case IFLA_VF_LINK_STATE_AUTO:
328 vf->flags |= BNXT_VF_LINK_UP;
330 case IFLA_VF_LINK_STATE_DISABLE:
331 vf->flags |= BNXT_VF_LINK_FORCED;
333 case IFLA_VF_LINK_STATE_ENABLE:
334 vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED;
337 netdev_err(bp->dev, "Invalid link option\n");
341 if (vf->flags & (BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED))
342 rc = bnxt_hwrm_fwd_async_event_cmpl(bp, vf,
343 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE);
347 static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs)
350 struct bnxt_vf_info *vf;
352 for (i = 0; i < num_vfs; i++) {
354 memset(vf, 0, sizeof(*vf));
359 static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs)
362 struct bnxt_pf_info *pf = &bp->pf;
363 struct hwrm_func_vf_resc_free_input req = {0};
365 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESC_FREE, -1, -1);
367 mutex_lock(&bp->hwrm_cmd_lock);
368 for (i = pf->first_vf_id; i < pf->first_vf_id + num_vfs; i++) {
369 req.vf_id = cpu_to_le16(i);
370 rc = _hwrm_send_message(bp, &req, sizeof(req),
375 mutex_unlock(&bp->hwrm_cmd_lock);
379 static void bnxt_free_vf_resources(struct bnxt *bp)
381 struct pci_dev *pdev = bp->pdev;
384 kfree(bp->pf.vf_event_bmap);
385 bp->pf.vf_event_bmap = NULL;
387 for (i = 0; i < 4; i++) {
388 if (bp->pf.hwrm_cmd_req_addr[i]) {
389 dma_free_coherent(&pdev->dev, BNXT_PAGE_SIZE,
390 bp->pf.hwrm_cmd_req_addr[i],
391 bp->pf.hwrm_cmd_req_dma_addr[i]);
392 bp->pf.hwrm_cmd_req_addr[i] = NULL;
396 bp->pf.active_vfs = 0;
401 static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs)
403 struct pci_dev *pdev = bp->pdev;
404 u32 nr_pages, size, i, j, k = 0;
406 bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL);
410 bnxt_set_vf_attr(bp, num_vfs);
412 size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE;
413 nr_pages = size / BNXT_PAGE_SIZE;
414 if (size & (BNXT_PAGE_SIZE - 1))
417 for (i = 0; i < nr_pages; i++) {
418 bp->pf.hwrm_cmd_req_addr[i] =
419 dma_alloc_coherent(&pdev->dev, BNXT_PAGE_SIZE,
420 &bp->pf.hwrm_cmd_req_dma_addr[i],
423 if (!bp->pf.hwrm_cmd_req_addr[i])
426 for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) {
427 struct bnxt_vf_info *vf = &bp->pf.vf[k];
429 vf->hwrm_cmd_req_addr = bp->pf.hwrm_cmd_req_addr[i] +
430 j * BNXT_HWRM_REQ_MAX_SIZE;
431 vf->hwrm_cmd_req_dma_addr =
432 bp->pf.hwrm_cmd_req_dma_addr[i] + j *
433 BNXT_HWRM_REQ_MAX_SIZE;
439 bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL);
440 if (!bp->pf.vf_event_bmap)
443 bp->pf.hwrm_cmd_req_pages = nr_pages;
447 static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
449 struct hwrm_func_buf_rgtr_input req = {0};
451 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BUF_RGTR, -1, -1);
453 req.req_buf_num_pages = cpu_to_le16(bp->pf.hwrm_cmd_req_pages);
454 req.req_buf_page_size = cpu_to_le16(BNXT_PAGE_SHIFT);
455 req.req_buf_len = cpu_to_le16(BNXT_HWRM_REQ_MAX_SIZE);
456 req.req_buf_page_addr0 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[0]);
457 req.req_buf_page_addr1 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[1]);
458 req.req_buf_page_addr2 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[2]);
459 req.req_buf_page_addr3 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[3]);
461 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
464 /* Caller holds bp->hwrm_cmd_lock mutex lock */
465 static void __bnxt_set_vf_params(struct bnxt *bp, int vf_id)
467 struct hwrm_func_cfg_input req = {0};
468 struct bnxt_vf_info *vf;
470 vf = &bp->pf.vf[vf_id];
471 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
472 req.fid = cpu_to_le16(vf->fw_fid);
474 if (is_valid_ether_addr(vf->mac_addr)) {
475 req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
476 memcpy(req.dflt_mac_addr, vf->mac_addr, ETH_ALEN);
479 req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
480 req.dflt_vlan = cpu_to_le16(vf->vlan);
482 if (vf->max_tx_rate) {
483 req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW);
484 req.max_bw = cpu_to_le32(vf->max_tx_rate);
485 #ifdef HAVE_IFLA_TX_RATE
486 req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MIN_BW);
487 req.min_bw = cpu_to_le32(vf->min_tx_rate);
490 if (vf->flags & BNXT_VF_TRUST)
491 req.flags |= cpu_to_le32(FUNC_CFG_REQ_FLAGS_TRUSTED_VF_ENABLE);
493 _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
496 /* Only called by PF to reserve resources for VFs, returns actual number of
497 * VFs configured, or < 0 on error.
499 static int bnxt_hwrm_func_vf_resc_cfg(struct bnxt *bp, int num_vfs, bool reset)
501 struct hwrm_func_vf_resource_cfg_input req = {0};
502 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
503 u16 vf_tx_rings, vf_rx_rings, vf_cp_rings;
504 u16 vf_stat_ctx, vf_vnics, vf_ring_grps;
505 struct bnxt_pf_info *pf = &bp->pf;
506 int i, rc = 0, min = 1;
510 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESOURCE_CFG, -1, -1);
512 if (bp->flags & BNXT_FLAG_CHIP_P5) {
513 vf_msix = hw_resc->max_nqs - bnxt_nq_rings_in_use(bp);
516 vf_ring_grps = hw_resc->max_hw_ring_grps - bp->rx_nr_rings;
518 vf_cp_rings = bnxt_get_avail_cp_rings_for_en(bp);
519 vf_stat_ctx = bnxt_get_avail_stat_ctxs_for_en(bp);
520 if (bp->flags & BNXT_FLAG_AGG_RINGS)
521 vf_rx_rings = hw_resc->max_rx_rings - bp->rx_nr_rings * 2;
523 vf_rx_rings = hw_resc->max_rx_rings - bp->rx_nr_rings;
524 vf_tx_rings = hw_resc->max_tx_rings - bp->tx_nr_rings;
525 vf_vnics = hw_resc->max_vnics - bp->nr_vnics;
526 vf_vnics = min_t(u16, vf_vnics, vf_rx_rings);
527 vf_rss = hw_resc->max_rsscos_ctxs - bp->rsscos_nr_ctxs;
529 req.min_rsscos_ctx = cpu_to_le16(BNXT_VF_MIN_RSS_CTX);
530 if (pf->vf_resv_strategy == BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) {
532 req.min_rsscos_ctx = cpu_to_le16(min);
534 if (pf->vf_resv_strategy == BNXT_VF_RESV_STRATEGY_MINIMAL ||
535 pf->vf_resv_strategy == BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) {
536 req.min_cmpl_rings = cpu_to_le16(min);
537 req.min_tx_rings = cpu_to_le16(min);
538 req.min_rx_rings = cpu_to_le16(min);
539 req.min_l2_ctxs = cpu_to_le16(min);
540 req.min_vnics = cpu_to_le16(min);
541 req.min_stat_ctx = cpu_to_le16(min);
542 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
543 req.min_hw_ring_grps = cpu_to_le16(min);
545 vf_cp_rings /= num_vfs;
546 vf_tx_rings /= num_vfs;
547 vf_rx_rings /= num_vfs;
549 vf_stat_ctx /= num_vfs;
550 vf_ring_grps /= num_vfs;
553 req.min_cmpl_rings = cpu_to_le16(vf_cp_rings);
554 req.min_tx_rings = cpu_to_le16(vf_tx_rings);
555 req.min_rx_rings = cpu_to_le16(vf_rx_rings);
556 req.min_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
557 req.min_vnics = cpu_to_le16(vf_vnics);
558 req.min_stat_ctx = cpu_to_le16(vf_stat_ctx);
559 req.min_hw_ring_grps = cpu_to_le16(vf_ring_grps);
560 req.min_rsscos_ctx = cpu_to_le16(vf_rss);
562 req.max_cmpl_rings = cpu_to_le16(vf_cp_rings);
563 req.max_tx_rings = cpu_to_le16(vf_tx_rings);
564 req.max_rx_rings = cpu_to_le16(vf_rx_rings);
565 req.max_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
566 req.max_vnics = cpu_to_le16(vf_vnics);
567 req.max_stat_ctx = cpu_to_le16(vf_stat_ctx);
568 req.max_hw_ring_grps = cpu_to_le16(vf_ring_grps);
569 req.max_rsscos_ctx = cpu_to_le16(vf_rss);
570 if (bp->flags & BNXT_FLAG_CHIP_P5)
571 req.max_msix = cpu_to_le16(vf_msix / num_vfs);
573 mutex_lock(&bp->hwrm_cmd_lock);
574 for (i = 0; i < num_vfs; i++) {
576 __bnxt_set_vf_params(bp, i);
578 req.vf_id = cpu_to_le16(pf->first_vf_id + i);
579 rc = _hwrm_send_message(bp, &req, sizeof(req),
583 pf->active_vfs = i + 1;
584 pf->vf[i].fw_fid = pf->first_vf_id + i;
586 mutex_unlock(&bp->hwrm_cmd_lock);
587 if (pf->active_vfs) {
588 u16 n = pf->active_vfs;
590 hw_resc->max_tx_rings -= le16_to_cpu(req.min_tx_rings) * n;
591 hw_resc->max_rx_rings -= le16_to_cpu(req.min_rx_rings) * n;
592 hw_resc->max_hw_ring_grps -= le16_to_cpu(req.min_hw_ring_grps) *
594 hw_resc->max_cp_rings -= le16_to_cpu(req.min_cmpl_rings) * n;
595 hw_resc->max_rsscos_ctxs -= le16_to_cpu(req.min_rsscos_ctx) * n;
596 hw_resc->max_stat_ctxs -= le16_to_cpu(req.min_stat_ctx) * n;
597 hw_resc->max_vnics -= le16_to_cpu(req.min_vnics) * n;
598 if (bp->flags & BNXT_FLAG_CHIP_P5)
599 hw_resc->max_irqs -= vf_msix * n;
606 /* Only called by PF to reserve resources for VFs, returns actual number of
607 * VFs configured, or < 0 on error.
609 static int bnxt_hwrm_func_cfg(struct bnxt *bp, int num_vfs)
612 u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics;
613 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
614 struct hwrm_func_cfg_input req = {0};
615 struct bnxt_pf_info *pf = &bp->pf;
616 int total_vf_tx_rings = 0;
619 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
621 /* Remaining rings are distributed equally amongs VF's for now */
622 vf_cp_rings = bnxt_get_avail_cp_rings_for_en(bp) / num_vfs;
623 vf_stat_ctx = bnxt_get_avail_stat_ctxs_for_en(bp) / num_vfs;
624 if (bp->flags & BNXT_FLAG_AGG_RINGS)
625 vf_rx_rings = (hw_resc->max_rx_rings - bp->rx_nr_rings * 2) /
628 vf_rx_rings = (hw_resc->max_rx_rings - bp->rx_nr_rings) /
630 vf_ring_grps = (hw_resc->max_hw_ring_grps - bp->rx_nr_rings) / num_vfs;
631 vf_tx_rings = (hw_resc->max_tx_rings - bp->tx_nr_rings) / num_vfs;
632 vf_vnics = (hw_resc->max_vnics - bp->nr_vnics) / num_vfs;
633 vf_vnics = min_t(u16, vf_vnics, vf_rx_rings);
635 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ADMIN_MTU |
636 FUNC_CFG_REQ_ENABLES_MRU |
637 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
638 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
639 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
640 FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
641 FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
642 FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
643 FUNC_CFG_REQ_ENABLES_NUM_VNICS |
644 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);
646 mtu = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
647 req.mru = cpu_to_le16(mtu);
648 req.admin_mtu = cpu_to_le16(mtu);
650 req.num_rsscos_ctxs = cpu_to_le16(1);
651 req.num_cmpl_rings = cpu_to_le16(vf_cp_rings);
652 req.num_tx_rings = cpu_to_le16(vf_tx_rings);
653 req.num_rx_rings = cpu_to_le16(vf_rx_rings);
654 req.num_hw_ring_grps = cpu_to_le16(vf_ring_grps);
655 req.num_l2_ctxs = cpu_to_le16(4);
657 req.num_vnics = cpu_to_le16(vf_vnics);
658 /* FIXME spec currently uses 1 bit for stats ctx */
659 req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
661 mutex_lock(&bp->hwrm_cmd_lock);
662 for (i = 0; i < num_vfs; i++) {
663 int vf_tx_rsvd = vf_tx_rings;
665 req.fid = cpu_to_le16(pf->first_vf_id + i);
666 rc = _hwrm_send_message(bp, &req, sizeof(req),
670 pf->active_vfs = i + 1;
671 pf->vf[i].fw_fid = le16_to_cpu(req.fid);
672 rc = __bnxt_hwrm_get_tx_rings(bp, pf->vf[i].fw_fid,
676 total_vf_tx_rings += vf_tx_rsvd;
678 mutex_unlock(&bp->hwrm_cmd_lock);
679 if (pf->active_vfs) {
680 hw_resc->max_tx_rings -= total_vf_tx_rings;
681 hw_resc->max_rx_rings -= vf_rx_rings * num_vfs;
682 hw_resc->max_hw_ring_grps -= vf_ring_grps * num_vfs;
683 hw_resc->max_cp_rings -= vf_cp_rings * num_vfs;
684 hw_resc->max_rsscos_ctxs -= num_vfs;
685 hw_resc->max_stat_ctxs -= vf_stat_ctx * num_vfs;
686 hw_resc->max_vnics -= vf_vnics * num_vfs;
692 static int bnxt_func_cfg(struct bnxt *bp, int num_vfs, bool reset)
695 return bnxt_hwrm_func_vf_resc_cfg(bp, num_vfs, reset);
697 return bnxt_hwrm_func_cfg(bp, num_vfs);
700 int bnxt_cfg_hw_sriov(struct bnxt *bp, int *num_vfs, bool reset)
704 /* Register buffers for VFs */
705 rc = bnxt_hwrm_func_buf_rgtr(bp);
709 /* Reserve resources for VFs */
710 rc = bnxt_func_cfg(bp, *num_vfs, reset);
711 if (rc != *num_vfs) {
713 netdev_warn(bp->dev, "Unable to reserve resources for SRIOV.\n");
717 netdev_warn(bp->dev, "Only able to reserve resources for %d VFs.\n",
722 bnxt_ulp_sriov_cfg(bp, *num_vfs);
726 static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
728 int rc = 0, vfs_supported;
729 int min_rx_rings, min_tx_rings, min_rss_ctxs;
730 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
731 int tx_ok = 0, rx_ok = 0, rss_ok = 0;
732 int avail_cp, avail_stat;
734 /* Check if we can enable requested num of vf's. At a mininum
735 * we require 1 RX 1 TX rings for each VF. In this minimum conf
736 * features like TPA will not be available.
738 vfs_supported = *num_vfs;
740 avail_cp = bnxt_get_avail_cp_rings_for_en(bp);
741 avail_stat = bnxt_get_avail_stat_ctxs_for_en(bp);
742 avail_cp = min_t(int, avail_cp, avail_stat);
744 while (vfs_supported) {
745 min_rx_rings = vfs_supported;
746 min_tx_rings = vfs_supported;
747 min_rss_ctxs = vfs_supported;
749 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
750 if (hw_resc->max_rx_rings - bp->rx_nr_rings * 2 >=
754 if (hw_resc->max_rx_rings - bp->rx_nr_rings >=
758 if (hw_resc->max_vnics - bp->nr_vnics < min_rx_rings ||
759 avail_cp < min_rx_rings)
762 if (hw_resc->max_tx_rings - bp->tx_nr_rings >= min_tx_rings &&
763 avail_cp >= min_tx_rings)
766 if (hw_resc->max_rsscos_ctxs - bp->rsscos_nr_ctxs >=
770 if (tx_ok && rx_ok && rss_ok)
776 if (!vfs_supported) {
777 netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
781 if (vfs_supported != *num_vfs) {
782 netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
783 *num_vfs, vfs_supported);
784 *num_vfs = vfs_supported;
787 rc = bnxt_alloc_vf_resources(bp, *num_vfs);
791 rc = bnxt_cfg_hw_sriov(bp, num_vfs, false);
795 rc = pci_enable_sriov(bp->pdev, *num_vfs);
802 /* Free the resources reserved for various VF's */
803 bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
806 bnxt_free_vf_resources(bp);
811 void bnxt_sriov_disable(struct bnxt *bp)
813 u16 num_vfs = pci_num_vf(bp->pdev);
818 /* synchronize VF and VF-rep create and destroy */
819 mutex_lock(&bp->sriov_lock);
820 bnxt_vf_reps_destroy(bp);
822 if (pci_vfs_assigned(bp->pdev)) {
823 bnxt_hwrm_fwd_async_event_cmpl(
824 bp, NULL, ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD);
825 netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
828 pci_disable_sriov(bp->pdev);
829 /* Free the HW resources reserved for various VF's */
830 bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
832 mutex_unlock(&bp->sriov_lock);
834 bnxt_free_vf_resources(bp);
836 /* Reclaim all resources for the PF. */
838 bnxt_restore_pf_fw_resources(bp);
841 bnxt_ulp_sriov_cfg(bp, 0);
844 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
846 struct net_device *dev = pci_get_drvdata(pdev);
847 struct bnxt *bp = netdev_priv(dev);
849 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
850 netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
855 if (!netif_running(dev)) {
856 netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
860 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
861 netdev_warn(dev, "Reject SRIOV config request when FW reset is in progress\n");
865 bp->sriov_cfg = true;
868 if (pci_vfs_assigned(bp->pdev)) {
869 netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
874 /* Check if enabled VFs is same as requested */
875 if (num_vfs && num_vfs == bp->pf.active_vfs)
878 /* if there are previous existing VFs, clean them up */
879 bnxt_sriov_disable(bp);
883 bnxt_sriov_enable(bp, &num_vfs);
886 bp->sriov_cfg = false;
887 wake_up(&bp->sriov_cfg_wait);
892 static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
893 void *encap_resp, __le64 encap_resp_addr,
894 __le16 encap_resp_cpr, u32 msg_size)
897 struct hwrm_fwd_resp_input req = {0};
899 if (BNXT_FWD_RESP_SIZE_ERR(msg_size))
902 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
904 /* Set the new target id */
905 req.target_id = cpu_to_le16(vf->fw_fid);
906 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
907 req.encap_resp_len = cpu_to_le16(msg_size);
908 req.encap_resp_addr = encap_resp_addr;
909 req.encap_resp_cmpl_ring = encap_resp_cpr;
910 memcpy(req.encap_resp, encap_resp, msg_size);
912 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
914 netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
918 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
922 struct hwrm_reject_fwd_resp_input req = {0};
924 if (BNXT_REJ_FWD_RESP_SIZE_ERR(msg_size))
927 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1);
928 /* Set the new target id */
929 req.target_id = cpu_to_le16(vf->fw_fid);
930 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
931 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
933 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
935 netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
939 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
943 struct hwrm_exec_fwd_resp_input req = {0};
945 if (BNXT_EXEC_FWD_RESP_SIZE_ERR(msg_size))
948 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1);
949 /* Set the new target id */
950 req.target_id = cpu_to_le16(vf->fw_fid);
951 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
952 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
954 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
956 netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
960 static int bnxt_vf_configure_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
962 u32 msg_size = sizeof(struct hwrm_func_vf_cfg_input);
963 struct hwrm_func_vf_cfg_input *req =
964 (struct hwrm_func_vf_cfg_input *)vf->hwrm_cmd_req_addr;
966 /* Allow VF to set a valid MAC address, if trust is set to on or
967 * if the PF assigned MAC address is zero
969 if (req->enables & cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR)) {
970 bool trust = bnxt_is_trusted_vf(bp, vf);
972 if (is_valid_ether_addr(req->dflt_mac_addr) &&
973 (trust || !is_valid_ether_addr(vf->mac_addr) ||
974 ether_addr_equal(req->dflt_mac_addr, vf->mac_addr))) {
975 ether_addr_copy(vf->vf_mac_addr, req->dflt_mac_addr);
976 return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
978 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
980 return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
983 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
985 u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
986 struct hwrm_cfa_l2_filter_alloc_input *req =
987 (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
990 if (!is_valid_ether_addr((const u8 *)req->l2_addr))
991 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
993 /* Allow VF to set a valid MAC address, if trust is set to on.
994 * Or VF MAC address must first match MAC address in PF's context.
995 * Otherwise, it must match the VF MAC address if firmware spec >=
998 if (bnxt_is_trusted_vf(bp, vf)) {
1000 } else if (is_valid_ether_addr(vf->mac_addr)) {
1001 if (ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
1003 } else if (is_valid_ether_addr(vf->vf_mac_addr)) {
1004 if (ether_addr_equal((const u8 *)req->l2_addr, vf->vf_mac_addr))
1007 /* There are two cases:
1008 * 1.If firmware spec < 0x10202,VF MAC address is not forwarded
1009 * to the PF and so it doesn't have to match
1010 * 2.Allow VF to modify it's own MAC when PF has not assigned a
1011 * valid MAC address and firmware spec >= 0x10202
1016 return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
1017 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
1020 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
1024 if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
1026 rc = bnxt_hwrm_exec_fwd_resp(
1027 bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
1029 struct hwrm_port_phy_qcfg_output phy_qcfg_resp = {0};
1030 struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
1033 (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
1034 mutex_lock(&bp->hwrm_cmd_lock);
1035 memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
1036 sizeof(phy_qcfg_resp));
1037 mutex_unlock(&bp->hwrm_cmd_lock);
1038 phy_qcfg_resp.resp_len = cpu_to_le16(sizeof(phy_qcfg_resp));
1039 phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
1040 phy_qcfg_resp.valid = 1;
1042 if (vf->flags & BNXT_VF_LINK_UP) {
1043 /* if physical link is down, force link up on VF */
1044 if (phy_qcfg_resp.link !=
1045 PORT_PHY_QCFG_RESP_LINK_LINK) {
1046 phy_qcfg_resp.link =
1047 PORT_PHY_QCFG_RESP_LINK_LINK;
1048 phy_qcfg_resp.link_speed = cpu_to_le16(
1049 PORT_PHY_QCFG_RESP_LINK_SPEED_10GB);
1050 phy_qcfg_resp.duplex_cfg =
1051 PORT_PHY_QCFG_RESP_DUPLEX_CFG_FULL;
1052 phy_qcfg_resp.duplex_state =
1053 PORT_PHY_QCFG_RESP_DUPLEX_STATE_FULL;
1054 phy_qcfg_resp.pause =
1055 (PORT_PHY_QCFG_RESP_PAUSE_TX |
1056 PORT_PHY_QCFG_RESP_PAUSE_RX);
1059 /* force link down */
1060 phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
1061 phy_qcfg_resp.link_speed = 0;
1062 phy_qcfg_resp.duplex_state =
1063 PORT_PHY_QCFG_RESP_DUPLEX_STATE_HALF;
1064 phy_qcfg_resp.pause = 0;
1066 rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
1067 phy_qcfg_req->resp_addr,
1068 phy_qcfg_req->cmpl_ring,
1069 sizeof(phy_qcfg_resp));
1074 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
1077 struct input *encap_req = vf->hwrm_cmd_req_addr;
1078 u32 req_type = le16_to_cpu(encap_req->req_type);
1081 case HWRM_FUNC_VF_CFG:
1082 rc = bnxt_vf_configure_mac(bp, vf);
1084 case HWRM_CFA_L2_FILTER_ALLOC:
1085 rc = bnxt_vf_validate_set_mac(bp, vf);
1088 /* TODO Validate if VF is allowed to change mac address,
1089 * mtu, num of rings etc
1091 rc = bnxt_hwrm_exec_fwd_resp(
1092 bp, vf, sizeof(struct hwrm_func_cfg_input));
1094 case HWRM_PORT_PHY_QCFG:
1095 rc = bnxt_vf_set_link(bp, vf);
1103 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
1105 u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
1107 /* Scan through VF's and process commands */
1109 vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
1110 if (vf_id >= active_vfs)
1113 clear_bit(vf_id, bp->pf.vf_event_bmap);
1114 bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
1119 int bnxt_approve_mac(struct bnxt *bp, u8 *mac, bool strict)
1121 struct hwrm_func_vf_cfg_input req = {0};
1127 if (bp->hwrm_spec_code < 0x10202) {
1128 if (is_valid_ether_addr(bp->vf.mac_addr))
1129 rc = -EADDRNOTAVAIL;
1132 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
1133 req.enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
1134 memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
1135 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1138 rc = -EADDRNOTAVAIL;
1139 netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n",
1146 void bnxt_update_vf_mac(struct bnxt *bp)
1148 struct hwrm_func_qcaps_input req = {0};
1149 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
1150 bool inform_pf = false;
1152 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
1153 req.fid = cpu_to_le16(0xffff);
1155 mutex_lock(&bp->hwrm_cmd_lock);
1156 if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
1157 goto update_vf_mac_exit;
1159 /* Store MAC address from the firmware. There are 2 cases:
1160 * 1. MAC address is valid. It is assigned from the PF and we
1161 * need to override the current VF MAC address with it.
1162 * 2. MAC address is zero. The VF will use a random MAC address by
1163 * default but the stored zero MAC will allow the VF user to change
1164 * the random MAC address using ndo_set_mac_address() if he wants.
1166 if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr)) {
1167 memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN);
1168 /* This means we are now using our own MAC address, let
1169 * the PF know about this MAC address.
1171 if (!is_valid_ether_addr(bp->vf.mac_addr))
1175 /* overwrite netdev dev_addr with admin VF MAC */
1176 if (is_valid_ether_addr(bp->vf.mac_addr))
1177 memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN);
1179 mutex_unlock(&bp->hwrm_cmd_lock);
1181 bnxt_approve_mac(bp, bp->dev->dev_addr, false);
1186 int bnxt_cfg_hw_sriov(struct bnxt *bp, int *num_vfs, bool reset)
1193 void bnxt_sriov_disable(struct bnxt *bp)
1197 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
1199 netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
1202 void bnxt_update_vf_mac(struct bnxt *bp)
1206 int bnxt_approve_mac(struct bnxt *bp, u8 *mac, bool strict)