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_vf_ndo_prep(struct bnxt *bp, int vf_id)
24 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
25 netdev_err(bp->dev, "vf ndo called though PF is down\n");
28 if (!bp->pf.active_vfs) {
29 netdev_err(bp->dev, "vf ndo called though sriov is disabled\n");
32 if (vf_id >= bp->pf.max_vfs) {
33 netdev_err(bp->dev, "Invalid VF id %d\n", vf_id);
39 int bnxt_set_vf_spoofchk(struct net_device *dev, int vf_id, bool setting)
41 struct hwrm_func_cfg_input req = {0};
42 struct bnxt *bp = netdev_priv(dev);
43 struct bnxt_vf_info *vf;
44 bool old_setting = false;
48 rc = bnxt_vf_ndo_prep(bp, vf_id);
52 vf = &bp->pf.vf[vf_id];
53 if (vf->flags & BNXT_VF_SPOOFCHK)
55 if (old_setting == setting)
58 func_flags = vf->func_flags;
60 func_flags |= FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK;
62 func_flags &= ~FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK;
63 /*TODO: if the driver supports VLAN filter on guest VLAN,
64 * the spoof check should also include vlan anti-spoofing
66 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
67 req.fid = cpu_to_le16(vf->fw_fid);
68 req.flags = cpu_to_le32(func_flags);
69 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
71 vf->func_flags = func_flags;
73 vf->flags |= BNXT_VF_SPOOFCHK;
75 vf->flags &= ~BNXT_VF_SPOOFCHK;
80 int bnxt_get_vf_config(struct net_device *dev, int vf_id,
81 struct ifla_vf_info *ivi)
83 struct bnxt *bp = netdev_priv(dev);
84 struct bnxt_vf_info *vf;
87 rc = bnxt_vf_ndo_prep(bp, vf_id);
92 vf = &bp->pf.vf[vf_id];
94 memcpy(&ivi->mac, vf->mac_addr, ETH_ALEN);
95 ivi->max_tx_rate = vf->max_tx_rate;
96 ivi->min_tx_rate = vf->min_tx_rate;
98 ivi->qos = vf->flags & BNXT_VF_QOS;
99 ivi->spoofchk = vf->flags & BNXT_VF_SPOOFCHK;
100 if (!(vf->flags & BNXT_VF_LINK_FORCED))
101 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
102 else if (vf->flags & BNXT_VF_LINK_UP)
103 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
105 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
110 int bnxt_set_vf_mac(struct net_device *dev, int vf_id, u8 *mac)
112 struct hwrm_func_cfg_input req = {0};
113 struct bnxt *bp = netdev_priv(dev);
114 struct bnxt_vf_info *vf;
117 rc = bnxt_vf_ndo_prep(bp, vf_id);
120 /* reject bc or mc mac addr, zero mac addr means allow
121 * VF to use its own mac addr
123 if (is_multicast_ether_addr(mac)) {
124 netdev_err(dev, "Invalid VF ethernet address\n");
127 vf = &bp->pf.vf[vf_id];
129 memcpy(vf->mac_addr, mac, ETH_ALEN);
130 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
131 req.fid = cpu_to_le16(vf->fw_fid);
132 req.flags = cpu_to_le32(vf->func_flags);
133 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
134 memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
135 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
138 int bnxt_set_vf_vlan(struct net_device *dev, int vf_id, u16 vlan_id, u8 qos)
140 struct hwrm_func_cfg_input req = {0};
141 struct bnxt *bp = netdev_priv(dev);
142 struct bnxt_vf_info *vf;
146 if (bp->hwrm_spec_code < 0x10201)
149 rc = bnxt_vf_ndo_prep(bp, vf_id);
153 /* TODO: needed to implement proper handling of user priority,
154 * currently fail the command if there is valid priority
156 if (vlan_id > 4095 || qos)
159 vf = &bp->pf.vf[vf_id];
161 if (vlan_tag == vf->vlan)
164 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
165 req.fid = cpu_to_le16(vf->fw_fid);
166 req.flags = cpu_to_le32(vf->func_flags);
167 req.dflt_vlan = cpu_to_le16(vlan_tag);
168 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
169 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
175 int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate,
178 struct hwrm_func_cfg_input req = {0};
179 struct bnxt *bp = netdev_priv(dev);
180 struct bnxt_vf_info *vf;
184 rc = bnxt_vf_ndo_prep(bp, vf_id);
188 vf = &bp->pf.vf[vf_id];
189 pf_link_speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
190 if (max_tx_rate > pf_link_speed) {
191 netdev_info(bp->dev, "max tx rate %d exceed PF link speed for VF %d\n",
196 if (min_tx_rate > pf_link_speed || min_tx_rate > max_tx_rate) {
197 netdev_info(bp->dev, "min tx rate %d is invalid for VF %d\n",
201 if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate)
203 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
204 req.fid = cpu_to_le16(vf->fw_fid);
205 req.flags = cpu_to_le32(vf->func_flags);
206 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW);
207 req.max_bw = cpu_to_le32(max_tx_rate);
208 req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MIN_BW);
209 req.min_bw = cpu_to_le32(min_tx_rate);
210 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
212 vf->min_tx_rate = min_tx_rate;
213 vf->max_tx_rate = max_tx_rate;
218 int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link)
220 struct bnxt *bp = netdev_priv(dev);
221 struct bnxt_vf_info *vf;
224 rc = bnxt_vf_ndo_prep(bp, vf_id);
228 vf = &bp->pf.vf[vf_id];
230 vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED);
232 case IFLA_VF_LINK_STATE_AUTO:
233 vf->flags |= BNXT_VF_LINK_UP;
235 case IFLA_VF_LINK_STATE_DISABLE:
236 vf->flags |= BNXT_VF_LINK_FORCED;
238 case IFLA_VF_LINK_STATE_ENABLE:
239 vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED;
242 netdev_err(bp->dev, "Invalid link option\n");
246 /* CHIMP TODO: send msg to VF to update new link state */
251 static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs)
254 struct bnxt_vf_info *vf;
256 for (i = 0; i < num_vfs; i++) {
258 memset(vf, 0, sizeof(*vf));
259 vf->flags = BNXT_VF_QOS | BNXT_VF_LINK_UP;
264 static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs)
267 struct bnxt_pf_info *pf = &bp->pf;
268 struct hwrm_func_vf_resc_free_input req = {0};
270 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESC_FREE, -1, -1);
272 mutex_lock(&bp->hwrm_cmd_lock);
273 for (i = pf->first_vf_id; i < pf->first_vf_id + num_vfs; i++) {
274 req.vf_id = cpu_to_le16(i);
275 rc = _hwrm_send_message(bp, &req, sizeof(req),
280 mutex_unlock(&bp->hwrm_cmd_lock);
284 static void bnxt_free_vf_resources(struct bnxt *bp)
286 struct pci_dev *pdev = bp->pdev;
289 kfree(bp->pf.vf_event_bmap);
290 bp->pf.vf_event_bmap = NULL;
292 for (i = 0; i < 4; i++) {
293 if (bp->pf.hwrm_cmd_req_addr[i]) {
294 dma_free_coherent(&pdev->dev, BNXT_PAGE_SIZE,
295 bp->pf.hwrm_cmd_req_addr[i],
296 bp->pf.hwrm_cmd_req_dma_addr[i]);
297 bp->pf.hwrm_cmd_req_addr[i] = NULL;
305 static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs)
307 struct pci_dev *pdev = bp->pdev;
308 u32 nr_pages, size, i, j, k = 0;
310 bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL);
314 bnxt_set_vf_attr(bp, num_vfs);
316 size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE;
317 nr_pages = size / BNXT_PAGE_SIZE;
318 if (size & (BNXT_PAGE_SIZE - 1))
321 for (i = 0; i < nr_pages; i++) {
322 bp->pf.hwrm_cmd_req_addr[i] =
323 dma_alloc_coherent(&pdev->dev, BNXT_PAGE_SIZE,
324 &bp->pf.hwrm_cmd_req_dma_addr[i],
327 if (!bp->pf.hwrm_cmd_req_addr[i])
330 for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) {
331 struct bnxt_vf_info *vf = &bp->pf.vf[k];
333 vf->hwrm_cmd_req_addr = bp->pf.hwrm_cmd_req_addr[i] +
334 j * BNXT_HWRM_REQ_MAX_SIZE;
335 vf->hwrm_cmd_req_dma_addr =
336 bp->pf.hwrm_cmd_req_dma_addr[i] + j *
337 BNXT_HWRM_REQ_MAX_SIZE;
343 bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL);
344 if (!bp->pf.vf_event_bmap)
347 bp->pf.hwrm_cmd_req_pages = nr_pages;
351 static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
353 struct hwrm_func_buf_rgtr_input req = {0};
355 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BUF_RGTR, -1, -1);
357 req.req_buf_num_pages = cpu_to_le16(bp->pf.hwrm_cmd_req_pages);
358 req.req_buf_page_size = cpu_to_le16(BNXT_PAGE_SHIFT);
359 req.req_buf_len = cpu_to_le16(BNXT_HWRM_REQ_MAX_SIZE);
360 req.req_buf_page_addr0 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[0]);
361 req.req_buf_page_addr1 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[1]);
362 req.req_buf_page_addr2 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[2]);
363 req.req_buf_page_addr3 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[3]);
365 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
368 /* only call by PF to reserve resources for VF */
369 static int bnxt_hwrm_func_cfg(struct bnxt *bp, int num_vfs)
372 u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics;
374 struct hwrm_func_cfg_input req = {0};
375 struct bnxt_pf_info *pf = &bp->pf;
377 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
379 /* Remaining rings are distributed equally amongs VF's for now */
380 /* TODO: the following workaroud is needed to restrict total number
381 * of vf_cp_rings not exceed number of HW ring groups. This WA should
382 * be removed once new HWRM provides HW ring groups capability in
385 vf_cp_rings = min_t(u16, pf->max_cp_rings, pf->max_stat_ctxs);
386 vf_cp_rings = (vf_cp_rings - bp->cp_nr_rings) / num_vfs;
387 /* TODO: restore this logic below once the WA above is removed */
388 /* vf_cp_rings = (pf->max_cp_rings - bp->cp_nr_rings) / num_vfs; */
389 vf_stat_ctx = (pf->max_stat_ctxs - bp->num_stat_ctxs) / num_vfs;
390 if (bp->flags & BNXT_FLAG_AGG_RINGS)
391 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings * 2) /
394 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings) / num_vfs;
395 vf_ring_grps = (bp->pf.max_hw_ring_grps - bp->rx_nr_rings) / num_vfs;
396 vf_tx_rings = (pf->max_tx_rings - bp->tx_nr_rings) / num_vfs;
398 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MTU |
399 FUNC_CFG_REQ_ENABLES_MRU |
400 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
401 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
402 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
403 FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
404 FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
405 FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
406 FUNC_CFG_REQ_ENABLES_NUM_VNICS |
407 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);
409 mtu = bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
410 req.mru = cpu_to_le16(mtu);
411 req.mtu = cpu_to_le16(mtu);
413 req.num_rsscos_ctxs = cpu_to_le16(1);
414 req.num_cmpl_rings = cpu_to_le16(vf_cp_rings);
415 req.num_tx_rings = cpu_to_le16(vf_tx_rings);
416 req.num_rx_rings = cpu_to_le16(vf_rx_rings);
417 req.num_hw_ring_grps = cpu_to_le16(vf_ring_grps);
418 req.num_l2_ctxs = cpu_to_le16(4);
421 req.num_vnics = cpu_to_le16(vf_vnics);
422 /* FIXME spec currently uses 1 bit for stats ctx */
423 req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
425 mutex_lock(&bp->hwrm_cmd_lock);
426 for (i = 0; i < num_vfs; i++) {
427 req.fid = cpu_to_le16(pf->first_vf_id + i);
428 rc = _hwrm_send_message(bp, &req, sizeof(req),
432 pf->active_vfs = i + 1;
433 pf->vf[i].fw_fid = le16_to_cpu(req.fid);
435 mutex_unlock(&bp->hwrm_cmd_lock);
437 pf->max_tx_rings -= vf_tx_rings * num_vfs;
438 pf->max_rx_rings -= vf_rx_rings * num_vfs;
439 pf->max_hw_ring_grps -= vf_ring_grps * num_vfs;
440 pf->max_cp_rings -= vf_cp_rings * num_vfs;
441 pf->max_rsscos_ctxs -= num_vfs;
442 pf->max_stat_ctxs -= vf_stat_ctx * num_vfs;
443 pf->max_vnics -= vf_vnics * num_vfs;
448 static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
450 int rc = 0, vfs_supported;
451 int min_rx_rings, min_tx_rings, min_rss_ctxs;
452 int tx_ok = 0, rx_ok = 0, rss_ok = 0;
454 /* Check if we can enable requested num of vf's. At a mininum
455 * we require 1 RX 1 TX rings for each VF. In this minimum conf
456 * features like TPA will not be available.
458 vfs_supported = *num_vfs;
460 while (vfs_supported) {
461 min_rx_rings = vfs_supported;
462 min_tx_rings = vfs_supported;
463 min_rss_ctxs = vfs_supported;
465 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
466 if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >=
470 if (bp->pf.max_rx_rings - bp->rx_nr_rings >=
475 if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings)
478 if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs)
481 if (tx_ok && rx_ok && rss_ok)
487 if (!vfs_supported) {
488 netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
492 if (vfs_supported != *num_vfs) {
493 netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
494 *num_vfs, vfs_supported);
495 *num_vfs = vfs_supported;
498 rc = bnxt_alloc_vf_resources(bp, *num_vfs);
502 /* Reserve resources for VFs */
503 rc = bnxt_hwrm_func_cfg(bp, *num_vfs);
507 /* Register buffers for VFs */
508 rc = bnxt_hwrm_func_buf_rgtr(bp);
512 rc = pci_enable_sriov(bp->pdev, *num_vfs);
519 /* Free the resources reserved for various VF's */
520 bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
523 bnxt_free_vf_resources(bp);
528 static int bnxt_hwrm_fwd_async_event_cmpl(struct bnxt *bp,
529 struct bnxt_vf_info *vf,
533 struct hwrm_fwd_async_event_cmpl_input req = {0};
534 struct hwrm_fwd_async_event_cmpl_output *resp = bp->hwrm_cmd_resp_addr;
535 struct hwrm_async_event_cmpl *async_cmpl;
537 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_ASYNC_EVENT_CMPL, -1, -1);
539 req.encap_async_event_target_id = cpu_to_le16(vf->fw_fid);
541 /* broadcast this async event to all VFs */
542 req.encap_async_event_target_id = cpu_to_le16(0xffff);
543 async_cmpl = (struct hwrm_async_event_cmpl *)req.encap_async_event_cmpl;
545 cpu_to_le16(HWRM_ASYNC_EVENT_CMPL_TYPE_HWRM_ASYNC_EVENT);
546 async_cmpl->event_id = cpu_to_le16(event_id);
548 mutex_lock(&bp->hwrm_cmd_lock);
549 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
552 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl failed. rc:%d\n",
554 goto fwd_async_event_cmpl_exit;
557 if (resp->error_code) {
558 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl error %d\n",
563 fwd_async_event_cmpl_exit:
564 mutex_unlock(&bp->hwrm_cmd_lock);
568 void bnxt_sriov_disable(struct bnxt *bp)
570 u16 num_vfs = pci_num_vf(bp->pdev);
575 if (pci_vfs_assigned(bp->pdev)) {
576 bnxt_hwrm_fwd_async_event_cmpl(
578 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD);
579 netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
582 pci_disable_sriov(bp->pdev);
583 /* Free the HW resources reserved for various VF's */
584 bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
587 bnxt_free_vf_resources(bp);
589 bp->pf.active_vfs = 0;
590 /* Reclaim all resources for the PF. */
591 bnxt_hwrm_func_qcaps(bp);
594 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
596 struct net_device *dev = pci_get_drvdata(pdev);
597 struct bnxt *bp = netdev_priv(dev);
599 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
600 netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
605 if (!netif_running(dev)) {
606 netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
610 bp->sriov_cfg = true;
613 if (pci_vfs_assigned(bp->pdev)) {
614 netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
619 /* Check if enabled VFs is same as requested */
620 if (num_vfs && num_vfs == bp->pf.active_vfs)
623 /* if there are previous existing VFs, clean them up */
624 bnxt_sriov_disable(bp);
628 bnxt_sriov_enable(bp, &num_vfs);
631 bp->sriov_cfg = false;
632 wake_up(&bp->sriov_cfg_wait);
637 static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
638 void *encap_resp, __le64 encap_resp_addr,
639 __le16 encap_resp_cpr, u32 msg_size)
642 struct hwrm_fwd_resp_input req = {0};
643 struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
645 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
647 /* Set the new target id */
648 req.target_id = cpu_to_le16(vf->fw_fid);
649 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
650 req.encap_resp_len = cpu_to_le16(msg_size);
651 req.encap_resp_addr = encap_resp_addr;
652 req.encap_resp_cmpl_ring = encap_resp_cpr;
653 memcpy(req.encap_resp, encap_resp, msg_size);
655 mutex_lock(&bp->hwrm_cmd_lock);
656 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
659 netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
663 if (resp->error_code) {
664 netdev_err(bp->dev, "hwrm_fwd_resp error %d\n",
670 mutex_unlock(&bp->hwrm_cmd_lock);
674 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
678 struct hwrm_reject_fwd_resp_input req = {0};
679 struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
681 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1);
682 /* Set the new target id */
683 req.target_id = cpu_to_le16(vf->fw_fid);
684 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
685 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
687 mutex_lock(&bp->hwrm_cmd_lock);
688 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
691 netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
692 goto fwd_err_resp_exit;
695 if (resp->error_code) {
696 netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n",
702 mutex_unlock(&bp->hwrm_cmd_lock);
706 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
710 struct hwrm_exec_fwd_resp_input req = {0};
711 struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
713 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1);
714 /* Set the new target id */
715 req.target_id = cpu_to_le16(vf->fw_fid);
716 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
717 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
719 mutex_lock(&bp->hwrm_cmd_lock);
720 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
723 netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
724 goto exec_fwd_resp_exit;
727 if (resp->error_code) {
728 netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n",
734 mutex_unlock(&bp->hwrm_cmd_lock);
738 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
740 u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
741 struct hwrm_cfa_l2_filter_alloc_input *req =
742 (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
744 if (!is_valid_ether_addr(vf->mac_addr) ||
745 ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
746 return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
748 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
751 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
755 if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
757 rc = bnxt_hwrm_exec_fwd_resp(
758 bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
760 struct hwrm_port_phy_qcfg_output phy_qcfg_resp;
761 struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
764 (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
765 mutex_lock(&bp->hwrm_cmd_lock);
766 memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
767 sizeof(phy_qcfg_resp));
768 mutex_unlock(&bp->hwrm_cmd_lock);
769 phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
771 if (vf->flags & BNXT_VF_LINK_UP) {
772 /* if physical link is down, force link up on VF */
773 if (phy_qcfg_resp.link ==
774 PORT_PHY_QCFG_RESP_LINK_NO_LINK) {
776 PORT_PHY_QCFG_RESP_LINK_LINK;
777 phy_qcfg_resp.link_speed = cpu_to_le16(
778 PORT_PHY_QCFG_RESP_LINK_SPEED_10GB);
779 phy_qcfg_resp.duplex =
780 PORT_PHY_QCFG_RESP_DUPLEX_FULL;
781 phy_qcfg_resp.pause =
782 (PORT_PHY_QCFG_RESP_PAUSE_TX |
783 PORT_PHY_QCFG_RESP_PAUSE_RX);
786 /* force link down */
787 phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
788 phy_qcfg_resp.link_speed = 0;
789 phy_qcfg_resp.duplex = PORT_PHY_QCFG_RESP_DUPLEX_HALF;
790 phy_qcfg_resp.pause = 0;
792 rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
793 phy_qcfg_req->resp_addr,
794 phy_qcfg_req->cmpl_ring,
795 sizeof(phy_qcfg_resp));
800 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
803 struct input *encap_req = vf->hwrm_cmd_req_addr;
804 u32 req_type = le16_to_cpu(encap_req->req_type);
807 case HWRM_CFA_L2_FILTER_ALLOC:
808 rc = bnxt_vf_validate_set_mac(bp, vf);
811 /* TODO Validate if VF is allowed to change mac address,
812 * mtu, num of rings etc
814 rc = bnxt_hwrm_exec_fwd_resp(
815 bp, vf, sizeof(struct hwrm_func_cfg_input));
817 case HWRM_PORT_PHY_QCFG:
818 rc = bnxt_vf_set_link(bp, vf);
826 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
828 u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
830 /* Scan through VF's and process commands */
832 vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
833 if (vf_id >= active_vfs)
836 clear_bit(vf_id, bp->pf.vf_event_bmap);
837 bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
842 void bnxt_update_vf_mac(struct bnxt *bp)
844 struct hwrm_func_qcaps_input req = {0};
845 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
847 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
848 req.fid = cpu_to_le16(0xffff);
850 mutex_lock(&bp->hwrm_cmd_lock);
851 if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
852 goto update_vf_mac_exit;
854 /* Store MAC address from the firmware. There are 2 cases:
855 * 1. MAC address is valid. It is assigned from the PF and we
856 * need to override the current VF MAC address with it.
857 * 2. MAC address is zero. The VF will use a random MAC address by
858 * default but the stored zero MAC will allow the VF user to change
859 * the random MAC address using ndo_set_mac_address() if he wants.
861 if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr))
862 memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN);
864 /* overwrite netdev dev_addr with admin VF MAC */
865 if (is_valid_ether_addr(bp->vf.mac_addr))
866 memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN);
868 mutex_unlock(&bp->hwrm_cmd_lock);
871 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
873 struct hwrm_func_vf_cfg_input req = {0};
879 if (bp->hwrm_spec_code < 0x10202) {
880 if (is_valid_ether_addr(bp->vf.mac_addr))
884 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
885 req.enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
886 memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
887 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
891 netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n",
898 void bnxt_sriov_disable(struct bnxt *bp)
902 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
904 netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
907 void bnxt_update_vf_mac(struct bnxt *bp)
911 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)