1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2014-2016 Broadcom Corporation
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.
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>
18 #include "bnxt_sriov.h"
19 #include "bnxt_ethtool.h"
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)
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;
30 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_ASYNC_EVENT_CMPL, -1, -1);
32 req.encap_async_event_target_id = cpu_to_le16(vf->fw_fid);
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);
40 mutex_lock(&bp->hwrm_cmd_lock);
41 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
44 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl failed. rc:%d\n",
46 goto fwd_async_event_cmpl_exit;
49 if (resp->error_code) {
50 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl error %d\n",
55 fwd_async_event_cmpl_exit:
56 mutex_unlock(&bp->hwrm_cmd_lock);
60 static int bnxt_vf_ndo_prep(struct bnxt *bp, int vf_id)
62 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
63 netdev_err(bp->dev, "vf ndo called though PF is down\n");
66 if (!bp->pf.active_vfs) {
67 netdev_err(bp->dev, "vf ndo called though sriov is disabled\n");
70 if (vf_id >= bp->pf.max_vfs) {
71 netdev_err(bp->dev, "Invalid VF id %d\n", vf_id);
77 int bnxt_set_vf_spoofchk(struct net_device *dev, int vf_id, bool setting)
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;
86 rc = bnxt_vf_ndo_prep(bp, vf_id);
90 vf = &bp->pf.vf[vf_id];
91 if (vf->flags & BNXT_VF_SPOOFCHK)
93 if (old_setting == setting)
96 func_flags = vf->func_flags;
98 func_flags |= FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK;
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
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);
109 vf->func_flags = func_flags;
111 vf->flags |= BNXT_VF_SPOOFCHK;
113 vf->flags &= ~BNXT_VF_SPOOFCHK;
118 int bnxt_get_vf_config(struct net_device *dev, int vf_id,
119 struct ifla_vf_info *ivi)
121 struct bnxt *bp = netdev_priv(dev);
122 struct bnxt_vf_info *vf;
125 rc = bnxt_vf_ndo_prep(bp, vf_id);
130 vf = &bp->pf.vf[vf_id];
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;
143 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
148 int bnxt_set_vf_mac(struct net_device *dev, int vf_id, u8 *mac)
150 struct hwrm_func_cfg_input req = {0};
151 struct bnxt *bp = netdev_priv(dev);
152 struct bnxt_vf_info *vf;
155 rc = bnxt_vf_ndo_prep(bp, vf_id);
158 /* reject bc or mc mac addr, zero mac addr means allow
159 * VF to use its own mac addr
161 if (is_multicast_ether_addr(mac)) {
162 netdev_err(dev, "Invalid VF ethernet address\n");
165 vf = &bp->pf.vf[vf_id];
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);
176 int bnxt_set_vf_vlan(struct net_device *dev, int vf_id, u16 vlan_id, u8 qos,
179 struct hwrm_func_cfg_input req = {0};
180 struct bnxt *bp = netdev_priv(dev);
181 struct bnxt_vf_info *vf;
185 if (bp->hwrm_spec_code < 0x10201)
188 if (vlan_proto != htons(ETH_P_8021Q))
189 return -EPROTONOSUPPORT;
191 rc = bnxt_vf_ndo_prep(bp, vf_id);
195 /* TODO: needed to implement proper handling of user priority,
196 * currently fail the command if there is valid priority
198 if (vlan_id > 4095 || qos)
201 vf = &bp->pf.vf[vf_id];
203 if (vlan_tag == vf->vlan)
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);
217 int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate,
220 struct hwrm_func_cfg_input req = {0};
221 struct bnxt *bp = netdev_priv(dev);
222 struct bnxt_vf_info *vf;
226 rc = bnxt_vf_ndo_prep(bp, vf_id);
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",
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",
243 if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate)
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);
254 vf->min_tx_rate = min_tx_rate;
255 vf->max_tx_rate = max_tx_rate;
260 int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link)
262 struct bnxt *bp = netdev_priv(dev);
263 struct bnxt_vf_info *vf;
266 rc = bnxt_vf_ndo_prep(bp, vf_id);
270 vf = &bp->pf.vf[vf_id];
272 vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED);
274 case IFLA_VF_LINK_STATE_AUTO:
275 vf->flags |= BNXT_VF_LINK_UP;
277 case IFLA_VF_LINK_STATE_DISABLE:
278 vf->flags |= BNXT_VF_LINK_FORCED;
280 case IFLA_VF_LINK_STATE_ENABLE:
281 vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED;
284 netdev_err(bp->dev, "Invalid link option\n");
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);
294 static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs)
297 struct bnxt_vf_info *vf;
299 for (i = 0; i < num_vfs; i++) {
301 memset(vf, 0, sizeof(*vf));
302 vf->flags = BNXT_VF_QOS | BNXT_VF_LINK_UP;
307 static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs)
310 struct bnxt_pf_info *pf = &bp->pf;
311 struct hwrm_func_vf_resc_free_input req = {0};
313 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESC_FREE, -1, -1);
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),
323 mutex_unlock(&bp->hwrm_cmd_lock);
327 static void bnxt_free_vf_resources(struct bnxt *bp)
329 struct pci_dev *pdev = bp->pdev;
332 kfree(bp->pf.vf_event_bmap);
333 bp->pf.vf_event_bmap = NULL;
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;
348 static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs)
350 struct pci_dev *pdev = bp->pdev;
351 u32 nr_pages, size, i, j, k = 0;
353 bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL);
357 bnxt_set_vf_attr(bp, num_vfs);
359 size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE;
360 nr_pages = size / BNXT_PAGE_SIZE;
361 if (size & (BNXT_PAGE_SIZE - 1))
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],
370 if (!bp->pf.hwrm_cmd_req_addr[i])
373 for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) {
374 struct bnxt_vf_info *vf = &bp->pf.vf[k];
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;
386 bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL);
387 if (!bp->pf.vf_event_bmap)
390 bp->pf.hwrm_cmd_req_pages = nr_pages;
394 static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
396 struct hwrm_func_buf_rgtr_input req = {0};
398 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BUF_RGTR, -1, -1);
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]);
408 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
411 /* only call by PF to reserve resources for VF */
412 static int bnxt_hwrm_func_cfg(struct bnxt *bp, int num_vfs)
415 u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics;
417 struct hwrm_func_cfg_input req = {0};
418 struct bnxt_pf_info *pf = &bp->pf;
420 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
422 /* Remaining rings are distributed equally amongs VF's for now */
423 vf_cp_rings = (pf->max_cp_rings - bp->cp_nr_rings) / num_vfs;
424 vf_stat_ctx = (pf->max_stat_ctxs - bp->num_stat_ctxs) / num_vfs;
425 if (bp->flags & BNXT_FLAG_AGG_RINGS)
426 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings * 2) /
429 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings) / num_vfs;
430 vf_ring_grps = (bp->pf.max_hw_ring_grps - bp->rx_nr_rings) / num_vfs;
431 vf_tx_rings = (pf->max_tx_rings - bp->tx_nr_rings) / num_vfs;
432 vf_vnics = (pf->max_vnics - bp->nr_vnics) / num_vfs;
433 vf_vnics = min_t(u16, vf_vnics, vf_rx_rings);
435 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MTU |
436 FUNC_CFG_REQ_ENABLES_MRU |
437 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
438 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
439 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
440 FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
441 FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
442 FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
443 FUNC_CFG_REQ_ENABLES_NUM_VNICS |
444 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);
446 mtu = bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
447 req.mru = cpu_to_le16(mtu);
448 req.mtu = cpu_to_le16(mtu);
450 req.num_rsscos_ctxs = cpu_to_le16(1);
451 req.num_cmpl_rings = cpu_to_le16(vf_cp_rings);
452 req.num_tx_rings = cpu_to_le16(vf_tx_rings);
453 req.num_rx_rings = cpu_to_le16(vf_rx_rings);
454 req.num_hw_ring_grps = cpu_to_le16(vf_ring_grps);
455 req.num_l2_ctxs = cpu_to_le16(4);
457 req.num_vnics = cpu_to_le16(vf_vnics);
458 /* FIXME spec currently uses 1 bit for stats ctx */
459 req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
461 mutex_lock(&bp->hwrm_cmd_lock);
462 for (i = 0; i < num_vfs; i++) {
463 req.fid = cpu_to_le16(pf->first_vf_id + i);
464 rc = _hwrm_send_message(bp, &req, sizeof(req),
468 pf->active_vfs = i + 1;
469 pf->vf[i].fw_fid = le16_to_cpu(req.fid);
471 mutex_unlock(&bp->hwrm_cmd_lock);
473 pf->max_tx_rings -= vf_tx_rings * num_vfs;
474 pf->max_rx_rings -= vf_rx_rings * num_vfs;
475 pf->max_hw_ring_grps -= vf_ring_grps * num_vfs;
476 pf->max_cp_rings -= vf_cp_rings * num_vfs;
477 pf->max_rsscos_ctxs -= num_vfs;
478 pf->max_stat_ctxs -= vf_stat_ctx * num_vfs;
479 pf->max_vnics -= vf_vnics * num_vfs;
484 static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
486 int rc = 0, vfs_supported;
487 int min_rx_rings, min_tx_rings, min_rss_ctxs;
488 int tx_ok = 0, rx_ok = 0, rss_ok = 0;
490 /* Check if we can enable requested num of vf's. At a mininum
491 * we require 1 RX 1 TX rings for each VF. In this minimum conf
492 * features like TPA will not be available.
494 vfs_supported = *num_vfs;
496 while (vfs_supported) {
497 min_rx_rings = vfs_supported;
498 min_tx_rings = vfs_supported;
499 min_rss_ctxs = vfs_supported;
501 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
502 if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >=
506 if (bp->pf.max_rx_rings - bp->rx_nr_rings >=
510 if (bp->pf.max_vnics - bp->nr_vnics < min_rx_rings)
513 if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings)
516 if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs)
519 if (tx_ok && rx_ok && rss_ok)
525 if (!vfs_supported) {
526 netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
530 if (vfs_supported != *num_vfs) {
531 netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
532 *num_vfs, vfs_supported);
533 *num_vfs = vfs_supported;
536 rc = bnxt_alloc_vf_resources(bp, *num_vfs);
540 /* Reserve resources for VFs */
541 rc = bnxt_hwrm_func_cfg(bp, *num_vfs);
545 /* Register buffers for VFs */
546 rc = bnxt_hwrm_func_buf_rgtr(bp);
550 rc = pci_enable_sriov(bp->pdev, *num_vfs);
557 /* Free the resources reserved for various VF's */
558 bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
561 bnxt_free_vf_resources(bp);
566 void bnxt_sriov_disable(struct bnxt *bp)
568 u16 num_vfs = pci_num_vf(bp->pdev);
573 if (pci_vfs_assigned(bp->pdev)) {
574 bnxt_hwrm_fwd_async_event_cmpl(
575 bp, NULL, ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD);
576 netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
579 pci_disable_sriov(bp->pdev);
580 /* Free the HW resources reserved for various VF's */
581 bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
584 bnxt_free_vf_resources(bp);
586 bp->pf.active_vfs = 0;
587 /* Reclaim all resources for the PF. */
589 bnxt_restore_pf_fw_resources(bp);
593 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
595 struct net_device *dev = pci_get_drvdata(pdev);
596 struct bnxt *bp = netdev_priv(dev);
598 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
599 netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
604 if (!netif_running(dev)) {
605 netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
609 bp->sriov_cfg = true;
612 if (pci_vfs_assigned(bp->pdev)) {
613 netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
618 /* Check if enabled VFs is same as requested */
619 if (num_vfs && num_vfs == bp->pf.active_vfs)
622 /* if there are previous existing VFs, clean them up */
623 bnxt_sriov_disable(bp);
627 bnxt_sriov_enable(bp, &num_vfs);
630 bp->sriov_cfg = false;
631 wake_up(&bp->sriov_cfg_wait);
636 static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
637 void *encap_resp, __le64 encap_resp_addr,
638 __le16 encap_resp_cpr, u32 msg_size)
641 struct hwrm_fwd_resp_input req = {0};
642 struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
644 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
646 /* Set the new target id */
647 req.target_id = cpu_to_le16(vf->fw_fid);
648 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
649 req.encap_resp_len = cpu_to_le16(msg_size);
650 req.encap_resp_addr = encap_resp_addr;
651 req.encap_resp_cmpl_ring = encap_resp_cpr;
652 memcpy(req.encap_resp, encap_resp, msg_size);
654 mutex_lock(&bp->hwrm_cmd_lock);
655 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
658 netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
662 if (resp->error_code) {
663 netdev_err(bp->dev, "hwrm_fwd_resp error %d\n",
669 mutex_unlock(&bp->hwrm_cmd_lock);
673 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
677 struct hwrm_reject_fwd_resp_input req = {0};
678 struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
680 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1);
681 /* Set the new target id */
682 req.target_id = cpu_to_le16(vf->fw_fid);
683 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
684 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
686 mutex_lock(&bp->hwrm_cmd_lock);
687 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
690 netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
691 goto fwd_err_resp_exit;
694 if (resp->error_code) {
695 netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n",
701 mutex_unlock(&bp->hwrm_cmd_lock);
705 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
709 struct hwrm_exec_fwd_resp_input req = {0};
710 struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
712 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_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);
718 mutex_lock(&bp->hwrm_cmd_lock);
719 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
722 netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
723 goto exec_fwd_resp_exit;
726 if (resp->error_code) {
727 netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n",
733 mutex_unlock(&bp->hwrm_cmd_lock);
737 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
739 u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
740 struct hwrm_cfa_l2_filter_alloc_input *req =
741 (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
743 if (!is_valid_ether_addr(vf->mac_addr) ||
744 ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
745 return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
747 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
750 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
754 if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
756 rc = bnxt_hwrm_exec_fwd_resp(
757 bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
759 struct hwrm_port_phy_qcfg_output phy_qcfg_resp;
760 struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
763 (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
764 mutex_lock(&bp->hwrm_cmd_lock);
765 memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
766 sizeof(phy_qcfg_resp));
767 mutex_unlock(&bp->hwrm_cmd_lock);
768 phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
770 if (vf->flags & BNXT_VF_LINK_UP) {
771 /* if physical link is down, force link up on VF */
772 if (phy_qcfg_resp.link !=
773 PORT_PHY_QCFG_RESP_LINK_LINK) {
775 PORT_PHY_QCFG_RESP_LINK_LINK;
776 phy_qcfg_resp.link_speed = cpu_to_le16(
777 PORT_PHY_QCFG_RESP_LINK_SPEED_10GB);
778 phy_qcfg_resp.duplex =
779 PORT_PHY_QCFG_RESP_DUPLEX_FULL;
780 phy_qcfg_resp.pause =
781 (PORT_PHY_QCFG_RESP_PAUSE_TX |
782 PORT_PHY_QCFG_RESP_PAUSE_RX);
785 /* force link down */
786 phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
787 phy_qcfg_resp.link_speed = 0;
788 phy_qcfg_resp.duplex = PORT_PHY_QCFG_RESP_DUPLEX_HALF;
789 phy_qcfg_resp.pause = 0;
791 rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
792 phy_qcfg_req->resp_addr,
793 phy_qcfg_req->cmpl_ring,
794 sizeof(phy_qcfg_resp));
799 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
802 struct input *encap_req = vf->hwrm_cmd_req_addr;
803 u32 req_type = le16_to_cpu(encap_req->req_type);
806 case HWRM_CFA_L2_FILTER_ALLOC:
807 rc = bnxt_vf_validate_set_mac(bp, vf);
810 /* TODO Validate if VF is allowed to change mac address,
811 * mtu, num of rings etc
813 rc = bnxt_hwrm_exec_fwd_resp(
814 bp, vf, sizeof(struct hwrm_func_cfg_input));
816 case HWRM_PORT_PHY_QCFG:
817 rc = bnxt_vf_set_link(bp, vf);
825 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
827 u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
829 /* Scan through VF's and process commands */
831 vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
832 if (vf_id >= active_vfs)
835 clear_bit(vf_id, bp->pf.vf_event_bmap);
836 bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
841 void bnxt_update_vf_mac(struct bnxt *bp)
843 struct hwrm_func_qcaps_input req = {0};
844 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
846 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
847 req.fid = cpu_to_le16(0xffff);
849 mutex_lock(&bp->hwrm_cmd_lock);
850 if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
851 goto update_vf_mac_exit;
853 /* Store MAC address from the firmware. There are 2 cases:
854 * 1. MAC address is valid. It is assigned from the PF and we
855 * need to override the current VF MAC address with it.
856 * 2. MAC address is zero. The VF will use a random MAC address by
857 * default but the stored zero MAC will allow the VF user to change
858 * the random MAC address using ndo_set_mac_address() if he wants.
860 if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr))
861 memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN);
863 /* overwrite netdev dev_addr with admin VF MAC */
864 if (is_valid_ether_addr(bp->vf.mac_addr))
865 memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN);
867 mutex_unlock(&bp->hwrm_cmd_lock);
870 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
872 struct hwrm_func_vf_cfg_input req = {0};
878 if (bp->hwrm_spec_code < 0x10202) {
879 if (is_valid_ether_addr(bp->vf.mac_addr))
883 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
884 req.enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
885 memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
886 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
890 netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n",
897 void bnxt_sriov_disable(struct bnxt *bp)
901 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
903 netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
906 void bnxt_update_vf_mac(struct bnxt *bp)
910 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)