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;
419 int total_vf_tx_rings = 0;
421 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
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) /
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);
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);
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);
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);
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);
462 mutex_lock(&bp->hwrm_cmd_lock);
463 for (i = 0; i < num_vfs; i++) {
464 int vf_tx_rsvd = vf_tx_rings;
466 req.fid = cpu_to_le16(pf->first_vf_id + i);
467 rc = _hwrm_send_message(bp, &req, sizeof(req),
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,
477 total_vf_tx_rings += vf_tx_rsvd;
479 mutex_unlock(&bp->hwrm_cmd_lock);
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;
492 static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
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;
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.
502 vfs_supported = *num_vfs;
504 while (vfs_supported) {
505 min_rx_rings = vfs_supported;
506 min_tx_rings = vfs_supported;
507 min_rss_ctxs = vfs_supported;
509 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
510 if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >=
514 if (bp->pf.max_rx_rings - bp->rx_nr_rings >=
518 if (bp->pf.max_vnics - bp->nr_vnics < min_rx_rings)
521 if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings)
524 if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs)
527 if (tx_ok && rx_ok && rss_ok)
533 if (!vfs_supported) {
534 netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
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;
544 rc = bnxt_alloc_vf_resources(bp, *num_vfs);
548 /* Reserve resources for VFs */
549 rc = bnxt_hwrm_func_cfg(bp, *num_vfs);
553 /* Register buffers for VFs */
554 rc = bnxt_hwrm_func_buf_rgtr(bp);
558 rc = pci_enable_sriov(bp->pdev, *num_vfs);
565 /* Free the resources reserved for various VF's */
566 bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
569 bnxt_free_vf_resources(bp);
574 void bnxt_sriov_disable(struct bnxt *bp)
576 u16 num_vfs = pci_num_vf(bp->pdev);
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",
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);
592 bnxt_free_vf_resources(bp);
594 bp->pf.active_vfs = 0;
595 /* Reclaim all resources for the PF. */
597 bnxt_restore_pf_fw_resources(bp);
601 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
603 struct net_device *dev = pci_get_drvdata(pdev);
604 struct bnxt *bp = netdev_priv(dev);
606 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
607 netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
612 if (!netif_running(dev)) {
613 netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
617 bp->sriov_cfg = true;
620 if (pci_vfs_assigned(bp->pdev)) {
621 netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
626 /* Check if enabled VFs is same as requested */
627 if (num_vfs && num_vfs == bp->pf.active_vfs)
630 /* if there are previous existing VFs, clean them up */
631 bnxt_sriov_disable(bp);
635 bnxt_sriov_enable(bp, &num_vfs);
638 bp->sriov_cfg = false;
639 wake_up(&bp->sriov_cfg_wait);
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)
649 struct hwrm_fwd_resp_input req = {0};
650 struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
652 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
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);
662 mutex_lock(&bp->hwrm_cmd_lock);
663 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
666 netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
670 if (resp->error_code) {
671 netdev_err(bp->dev, "hwrm_fwd_resp error %d\n",
677 mutex_unlock(&bp->hwrm_cmd_lock);
681 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
685 struct hwrm_reject_fwd_resp_input req = {0};
686 struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
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);
694 mutex_lock(&bp->hwrm_cmd_lock);
695 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
698 netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
699 goto fwd_err_resp_exit;
702 if (resp->error_code) {
703 netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n",
709 mutex_unlock(&bp->hwrm_cmd_lock);
713 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
717 struct hwrm_exec_fwd_resp_input req = {0};
718 struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
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);
726 mutex_lock(&bp->hwrm_cmd_lock);
727 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
730 netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
731 goto exec_fwd_resp_exit;
734 if (resp->error_code) {
735 netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n",
741 mutex_unlock(&bp->hwrm_cmd_lock);
745 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
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;
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);
755 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
758 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
762 if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
764 rc = bnxt_hwrm_exec_fwd_resp(
765 bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
767 struct hwrm_port_phy_qcfg_output phy_qcfg_resp;
768 struct hwrm_port_phy_qcfg_input *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;
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) {
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);
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;
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));
807 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
810 struct input *encap_req = vf->hwrm_cmd_req_addr;
811 u32 req_type = le16_to_cpu(encap_req->req_type);
814 case HWRM_CFA_L2_FILTER_ALLOC:
815 rc = bnxt_vf_validate_set_mac(bp, vf);
818 /* TODO Validate if VF is allowed to change mac address,
819 * mtu, num of rings etc
821 rc = bnxt_hwrm_exec_fwd_resp(
822 bp, vf, sizeof(struct hwrm_func_cfg_input));
824 case HWRM_PORT_PHY_QCFG:
825 rc = bnxt_vf_set_link(bp, vf);
833 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
835 u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
837 /* Scan through VF's and process commands */
839 vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
840 if (vf_id >= active_vfs)
843 clear_bit(vf_id, bp->pf.vf_event_bmap);
844 bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
849 void bnxt_update_vf_mac(struct bnxt *bp)
851 struct hwrm_func_qcaps_input req = {0};
852 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
854 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
855 req.fid = cpu_to_le16(0xffff);
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;
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.
868 if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr))
869 memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN);
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);
875 mutex_unlock(&bp->hwrm_cmd_lock);
878 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
880 struct hwrm_func_vf_cfg_input req = {0};
886 if (bp->hwrm_spec_code < 0x10202) {
887 if (is_valid_ether_addr(bp->vf.mac_addr))
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);
898 netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n",
905 void bnxt_sriov_disable(struct bnxt *bp)
909 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
911 netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
914 void bnxt_update_vf_mac(struct bnxt *bp)
918 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)