1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2014-2016 Broadcom Corporation
4 * Copyright (c) 2016-2017 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/module.h>
12 #include <linux/pci.h>
13 #include <linux/netdevice.h>
14 #include <linux/if_vlan.h>
15 #include <linux/interrupt.h>
16 #include <linux/etherdevice.h>
20 #include "bnxt_sriov.h"
22 #include "bnxt_ethtool.h"
24 #ifdef CONFIG_BNXT_SRIOV
25 static int bnxt_hwrm_fwd_async_event_cmpl(struct bnxt *bp,
26 struct bnxt_vf_info *vf, u16 event_id)
28 struct hwrm_fwd_async_event_cmpl_output *resp = bp->hwrm_cmd_resp_addr;
29 struct hwrm_fwd_async_event_cmpl_input req = {0};
30 struct hwrm_async_event_cmpl *async_cmpl;
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 mutex_lock(&bp->hwrm_cmd_lock);
44 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
47 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl failed. rc:%d\n",
49 goto fwd_async_event_cmpl_exit;
52 if (resp->error_code) {
53 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl error %d\n",
58 fwd_async_event_cmpl_exit:
59 mutex_unlock(&bp->hwrm_cmd_lock);
63 static int bnxt_vf_ndo_prep(struct bnxt *bp, int vf_id)
65 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
66 netdev_err(bp->dev, "vf ndo called though PF is down\n");
69 if (!bp->pf.active_vfs) {
70 netdev_err(bp->dev, "vf ndo called though sriov is disabled\n");
73 if (vf_id >= bp->pf.max_vfs) {
74 netdev_err(bp->dev, "Invalid VF id %d\n", vf_id);
80 int bnxt_set_vf_spoofchk(struct net_device *dev, int vf_id, bool setting)
82 struct hwrm_func_cfg_input req = {0};
83 struct bnxt *bp = netdev_priv(dev);
84 struct bnxt_vf_info *vf;
85 bool old_setting = false;
89 if (bp->hwrm_spec_code < 0x10701)
92 rc = bnxt_vf_ndo_prep(bp, vf_id);
96 vf = &bp->pf.vf[vf_id];
97 if (vf->flags & BNXT_VF_SPOOFCHK)
99 if (old_setting == setting)
102 func_flags = vf->func_flags;
104 func_flags |= FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK_ENABLE;
106 func_flags |= FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK_DISABLE;
107 /*TODO: if the driver supports VLAN filter on guest VLAN,
108 * the spoof check should also include vlan anti-spoofing
110 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
111 req.fid = cpu_to_le16(vf->fw_fid);
112 req.flags = cpu_to_le32(func_flags);
113 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
115 vf->func_flags = func_flags;
117 vf->flags |= BNXT_VF_SPOOFCHK;
119 vf->flags &= ~BNXT_VF_SPOOFCHK;
124 int bnxt_get_vf_config(struct net_device *dev, int vf_id,
125 struct ifla_vf_info *ivi)
127 struct bnxt *bp = netdev_priv(dev);
128 struct bnxt_vf_info *vf;
131 rc = bnxt_vf_ndo_prep(bp, vf_id);
136 vf = &bp->pf.vf[vf_id];
138 memcpy(&ivi->mac, vf->mac_addr, ETH_ALEN);
139 ivi->max_tx_rate = vf->max_tx_rate;
140 ivi->min_tx_rate = vf->min_tx_rate;
141 ivi->vlan = vf->vlan;
142 if (vf->flags & BNXT_VF_QOS)
143 ivi->qos = vf->vlan >> VLAN_PRIO_SHIFT;
146 ivi->spoofchk = !!(vf->flags & BNXT_VF_SPOOFCHK);
147 if (!(vf->flags & BNXT_VF_LINK_FORCED))
148 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
149 else if (vf->flags & BNXT_VF_LINK_UP)
150 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
152 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
157 int bnxt_set_vf_mac(struct net_device *dev, int vf_id, u8 *mac)
159 struct hwrm_func_cfg_input req = {0};
160 struct bnxt *bp = netdev_priv(dev);
161 struct bnxt_vf_info *vf;
164 rc = bnxt_vf_ndo_prep(bp, vf_id);
167 /* reject bc or mc mac addr, zero mac addr means allow
168 * VF to use its own mac addr
170 if (is_multicast_ether_addr(mac)) {
171 netdev_err(dev, "Invalid VF ethernet address\n");
174 vf = &bp->pf.vf[vf_id];
176 memcpy(vf->mac_addr, mac, ETH_ALEN);
177 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
178 req.fid = cpu_to_le16(vf->fw_fid);
179 req.flags = cpu_to_le32(vf->func_flags);
180 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
181 memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
182 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
185 int bnxt_set_vf_vlan(struct net_device *dev, int vf_id, u16 vlan_id, u8 qos,
188 struct hwrm_func_cfg_input req = {0};
189 struct bnxt *bp = netdev_priv(dev);
190 struct bnxt_vf_info *vf;
194 if (bp->hwrm_spec_code < 0x10201)
197 if (vlan_proto != htons(ETH_P_8021Q))
198 return -EPROTONOSUPPORT;
200 rc = bnxt_vf_ndo_prep(bp, vf_id);
204 /* TODO: needed to implement proper handling of user priority,
205 * currently fail the command if there is valid priority
207 if (vlan_id > 4095 || qos)
210 vf = &bp->pf.vf[vf_id];
212 if (vlan_tag == vf->vlan)
215 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
216 req.fid = cpu_to_le16(vf->fw_fid);
217 req.flags = cpu_to_le32(vf->func_flags);
218 req.dflt_vlan = cpu_to_le16(vlan_tag);
219 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
220 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
226 int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate,
229 struct hwrm_func_cfg_input req = {0};
230 struct bnxt *bp = netdev_priv(dev);
231 struct bnxt_vf_info *vf;
235 rc = bnxt_vf_ndo_prep(bp, vf_id);
239 vf = &bp->pf.vf[vf_id];
240 pf_link_speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
241 if (max_tx_rate > pf_link_speed) {
242 netdev_info(bp->dev, "max tx rate %d exceed PF link speed for VF %d\n",
247 if (min_tx_rate > pf_link_speed || min_tx_rate > max_tx_rate) {
248 netdev_info(bp->dev, "min tx rate %d is invalid for VF %d\n",
252 if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate)
254 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
255 req.fid = cpu_to_le16(vf->fw_fid);
256 req.flags = cpu_to_le32(vf->func_flags);
257 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW);
258 req.max_bw = cpu_to_le32(max_tx_rate);
259 req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MIN_BW);
260 req.min_bw = cpu_to_le32(min_tx_rate);
261 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
263 vf->min_tx_rate = min_tx_rate;
264 vf->max_tx_rate = max_tx_rate;
269 int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link)
271 struct bnxt *bp = netdev_priv(dev);
272 struct bnxt_vf_info *vf;
275 rc = bnxt_vf_ndo_prep(bp, vf_id);
279 vf = &bp->pf.vf[vf_id];
281 vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED);
283 case IFLA_VF_LINK_STATE_AUTO:
284 vf->flags |= BNXT_VF_LINK_UP;
286 case IFLA_VF_LINK_STATE_DISABLE:
287 vf->flags |= BNXT_VF_LINK_FORCED;
289 case IFLA_VF_LINK_STATE_ENABLE:
290 vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED;
293 netdev_err(bp->dev, "Invalid link option\n");
297 if (vf->flags & (BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED))
298 rc = bnxt_hwrm_fwd_async_event_cmpl(bp, vf,
299 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE);
303 static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs)
306 struct bnxt_vf_info *vf;
308 for (i = 0; i < num_vfs; i++) {
310 memset(vf, 0, sizeof(*vf));
315 static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs)
318 struct bnxt_pf_info *pf = &bp->pf;
319 struct hwrm_func_vf_resc_free_input req = {0};
321 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESC_FREE, -1, -1);
323 mutex_lock(&bp->hwrm_cmd_lock);
324 for (i = pf->first_vf_id; i < pf->first_vf_id + num_vfs; i++) {
325 req.vf_id = cpu_to_le16(i);
326 rc = _hwrm_send_message(bp, &req, sizeof(req),
331 mutex_unlock(&bp->hwrm_cmd_lock);
335 static void bnxt_free_vf_resources(struct bnxt *bp)
337 struct pci_dev *pdev = bp->pdev;
340 kfree(bp->pf.vf_event_bmap);
341 bp->pf.vf_event_bmap = NULL;
343 for (i = 0; i < 4; i++) {
344 if (bp->pf.hwrm_cmd_req_addr[i]) {
345 dma_free_coherent(&pdev->dev, BNXT_PAGE_SIZE,
346 bp->pf.hwrm_cmd_req_addr[i],
347 bp->pf.hwrm_cmd_req_dma_addr[i]);
348 bp->pf.hwrm_cmd_req_addr[i] = NULL;
356 static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs)
358 struct pci_dev *pdev = bp->pdev;
359 u32 nr_pages, size, i, j, k = 0;
361 bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL);
365 bnxt_set_vf_attr(bp, num_vfs);
367 size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE;
368 nr_pages = size / BNXT_PAGE_SIZE;
369 if (size & (BNXT_PAGE_SIZE - 1))
372 for (i = 0; i < nr_pages; i++) {
373 bp->pf.hwrm_cmd_req_addr[i] =
374 dma_alloc_coherent(&pdev->dev, BNXT_PAGE_SIZE,
375 &bp->pf.hwrm_cmd_req_dma_addr[i],
378 if (!bp->pf.hwrm_cmd_req_addr[i])
381 for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) {
382 struct bnxt_vf_info *vf = &bp->pf.vf[k];
384 vf->hwrm_cmd_req_addr = bp->pf.hwrm_cmd_req_addr[i] +
385 j * BNXT_HWRM_REQ_MAX_SIZE;
386 vf->hwrm_cmd_req_dma_addr =
387 bp->pf.hwrm_cmd_req_dma_addr[i] + j *
388 BNXT_HWRM_REQ_MAX_SIZE;
394 bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL);
395 if (!bp->pf.vf_event_bmap)
398 bp->pf.hwrm_cmd_req_pages = nr_pages;
402 static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
404 struct hwrm_func_buf_rgtr_input req = {0};
406 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BUF_RGTR, -1, -1);
408 req.req_buf_num_pages = cpu_to_le16(bp->pf.hwrm_cmd_req_pages);
409 req.req_buf_page_size = cpu_to_le16(BNXT_PAGE_SHIFT);
410 req.req_buf_len = cpu_to_le16(BNXT_HWRM_REQ_MAX_SIZE);
411 req.req_buf_page_addr0 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[0]);
412 req.req_buf_page_addr1 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[1]);
413 req.req_buf_page_addr2 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[2]);
414 req.req_buf_page_addr3 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[3]);
416 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
419 /* only call by PF to reserve resources for VF */
420 static int bnxt_hwrm_func_cfg(struct bnxt *bp, int num_vfs)
423 u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics;
425 struct hwrm_func_cfg_input req = {0};
426 struct bnxt_pf_info *pf = &bp->pf;
427 int total_vf_tx_rings = 0;
429 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
431 /* Remaining rings are distributed equally amongs VF's for now */
432 vf_cp_rings = (pf->max_cp_rings - bp->cp_nr_rings) / num_vfs;
433 vf_stat_ctx = (pf->max_stat_ctxs - bp->num_stat_ctxs) / num_vfs;
434 if (bp->flags & BNXT_FLAG_AGG_RINGS)
435 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings * 2) /
438 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings) / num_vfs;
439 vf_ring_grps = (bp->pf.max_hw_ring_grps - bp->rx_nr_rings) / num_vfs;
440 vf_tx_rings = (pf->max_tx_rings - bp->tx_nr_rings) / num_vfs;
441 vf_vnics = (pf->max_vnics - bp->nr_vnics) / num_vfs;
442 vf_vnics = min_t(u16, vf_vnics, vf_rx_rings);
444 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MTU |
445 FUNC_CFG_REQ_ENABLES_MRU |
446 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
447 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
448 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
449 FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
450 FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
451 FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
452 FUNC_CFG_REQ_ENABLES_NUM_VNICS |
453 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);
455 mtu = bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
456 req.mru = cpu_to_le16(mtu);
457 req.mtu = cpu_to_le16(mtu);
459 req.num_rsscos_ctxs = cpu_to_le16(1);
460 req.num_cmpl_rings = cpu_to_le16(vf_cp_rings);
461 req.num_tx_rings = cpu_to_le16(vf_tx_rings);
462 req.num_rx_rings = cpu_to_le16(vf_rx_rings);
463 req.num_hw_ring_grps = cpu_to_le16(vf_ring_grps);
464 req.num_l2_ctxs = cpu_to_le16(4);
466 req.num_vnics = cpu_to_le16(vf_vnics);
467 /* FIXME spec currently uses 1 bit for stats ctx */
468 req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
470 mutex_lock(&bp->hwrm_cmd_lock);
471 for (i = 0; i < num_vfs; i++) {
472 int vf_tx_rsvd = vf_tx_rings;
474 req.fid = cpu_to_le16(pf->first_vf_id + i);
475 rc = _hwrm_send_message(bp, &req, sizeof(req),
479 pf->active_vfs = i + 1;
480 pf->vf[i].fw_fid = le16_to_cpu(req.fid);
481 rc = __bnxt_hwrm_get_tx_rings(bp, pf->vf[i].fw_fid,
485 total_vf_tx_rings += vf_tx_rsvd;
487 mutex_unlock(&bp->hwrm_cmd_lock);
489 pf->max_tx_rings -= total_vf_tx_rings;
490 pf->max_rx_rings -= vf_rx_rings * num_vfs;
491 pf->max_hw_ring_grps -= vf_ring_grps * num_vfs;
492 pf->max_cp_rings -= vf_cp_rings * num_vfs;
493 pf->max_rsscos_ctxs -= num_vfs;
494 pf->max_stat_ctxs -= vf_stat_ctx * num_vfs;
495 pf->max_vnics -= vf_vnics * num_vfs;
500 static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
502 int rc = 0, vfs_supported;
503 int min_rx_rings, min_tx_rings, min_rss_ctxs;
504 int tx_ok = 0, rx_ok = 0, rss_ok = 0;
505 int avail_cp, avail_stat;
507 /* Check if we can enable requested num of vf's. At a mininum
508 * we require 1 RX 1 TX rings for each VF. In this minimum conf
509 * features like TPA will not be available.
511 vfs_supported = *num_vfs;
513 avail_cp = bp->pf.max_cp_rings - bp->cp_nr_rings;
514 avail_stat = bp->pf.max_stat_ctxs - bp->num_stat_ctxs;
515 avail_cp = min_t(int, avail_cp, avail_stat);
517 while (vfs_supported) {
518 min_rx_rings = vfs_supported;
519 min_tx_rings = vfs_supported;
520 min_rss_ctxs = vfs_supported;
522 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
523 if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >=
527 if (bp->pf.max_rx_rings - bp->rx_nr_rings >=
531 if (bp->pf.max_vnics - bp->nr_vnics < min_rx_rings ||
532 avail_cp < min_rx_rings)
535 if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings &&
536 avail_cp >= min_tx_rings)
539 if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs)
542 if (tx_ok && rx_ok && rss_ok)
548 if (!vfs_supported) {
549 netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
553 if (vfs_supported != *num_vfs) {
554 netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
555 *num_vfs, vfs_supported);
556 *num_vfs = vfs_supported;
559 rc = bnxt_alloc_vf_resources(bp, *num_vfs);
563 /* Reserve resources for VFs */
564 rc = bnxt_hwrm_func_cfg(bp, *num_vfs);
568 /* Register buffers for VFs */
569 rc = bnxt_hwrm_func_buf_rgtr(bp);
573 bnxt_ulp_sriov_cfg(bp, *num_vfs);
575 rc = pci_enable_sriov(bp->pdev, *num_vfs);
582 /* Free the resources reserved for various VF's */
583 bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
586 bnxt_free_vf_resources(bp);
591 void bnxt_sriov_disable(struct bnxt *bp)
593 u16 num_vfs = pci_num_vf(bp->pdev);
598 /* synchronize VF and VF-rep create and destroy */
599 mutex_lock(&bp->sriov_lock);
600 bnxt_vf_reps_destroy(bp);
602 if (pci_vfs_assigned(bp->pdev)) {
603 bnxt_hwrm_fwd_async_event_cmpl(
604 bp, NULL, ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD);
605 netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
608 pci_disable_sriov(bp->pdev);
609 /* Free the HW resources reserved for various VF's */
610 bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
612 mutex_unlock(&bp->sriov_lock);
614 bnxt_free_vf_resources(bp);
616 bp->pf.active_vfs = 0;
617 /* Reclaim all resources for the PF. */
619 bnxt_restore_pf_fw_resources(bp);
622 bnxt_ulp_sriov_cfg(bp, 0);
625 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
627 struct net_device *dev = pci_get_drvdata(pdev);
628 struct bnxt *bp = netdev_priv(dev);
630 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
631 netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
636 if (!netif_running(dev)) {
637 netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
641 bp->sriov_cfg = true;
644 if (pci_vfs_assigned(bp->pdev)) {
645 netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
650 /* Check if enabled VFs is same as requested */
651 if (num_vfs && num_vfs == bp->pf.active_vfs)
654 /* if there are previous existing VFs, clean them up */
655 bnxt_sriov_disable(bp);
659 bnxt_sriov_enable(bp, &num_vfs);
662 bp->sriov_cfg = false;
663 wake_up(&bp->sriov_cfg_wait);
668 static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
669 void *encap_resp, __le64 encap_resp_addr,
670 __le16 encap_resp_cpr, u32 msg_size)
673 struct hwrm_fwd_resp_input req = {0};
674 struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
676 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
678 /* Set the new target id */
679 req.target_id = cpu_to_le16(vf->fw_fid);
680 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
681 req.encap_resp_len = cpu_to_le16(msg_size);
682 req.encap_resp_addr = encap_resp_addr;
683 req.encap_resp_cmpl_ring = encap_resp_cpr;
684 memcpy(req.encap_resp, encap_resp, 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_resp failed. rc:%d\n", rc);
694 if (resp->error_code) {
695 netdev_err(bp->dev, "hwrm_fwd_resp error %d\n",
701 mutex_unlock(&bp->hwrm_cmd_lock);
705 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
709 struct hwrm_reject_fwd_resp_input req = {0};
710 struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
712 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_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_fwd_err_resp failed. rc:%d\n", rc);
723 goto fwd_err_resp_exit;
726 if (resp->error_code) {
727 netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n",
733 mutex_unlock(&bp->hwrm_cmd_lock);
737 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
741 struct hwrm_exec_fwd_resp_input req = {0};
742 struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
744 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1);
745 /* Set the new target id */
746 req.target_id = cpu_to_le16(vf->fw_fid);
747 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
748 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
750 mutex_lock(&bp->hwrm_cmd_lock);
751 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
754 netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
755 goto exec_fwd_resp_exit;
758 if (resp->error_code) {
759 netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n",
765 mutex_unlock(&bp->hwrm_cmd_lock);
769 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
771 u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
772 struct hwrm_cfa_l2_filter_alloc_input *req =
773 (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
775 if (!is_valid_ether_addr(vf->mac_addr) ||
776 ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
777 return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
779 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
782 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
786 if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
788 rc = bnxt_hwrm_exec_fwd_resp(
789 bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
791 struct hwrm_port_phy_qcfg_output phy_qcfg_resp;
792 struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
795 (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
796 mutex_lock(&bp->hwrm_cmd_lock);
797 memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
798 sizeof(phy_qcfg_resp));
799 mutex_unlock(&bp->hwrm_cmd_lock);
800 phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
802 if (vf->flags & BNXT_VF_LINK_UP) {
803 /* if physical link is down, force link up on VF */
804 if (phy_qcfg_resp.link !=
805 PORT_PHY_QCFG_RESP_LINK_LINK) {
807 PORT_PHY_QCFG_RESP_LINK_LINK;
808 phy_qcfg_resp.link_speed = cpu_to_le16(
809 PORT_PHY_QCFG_RESP_LINK_SPEED_10GB);
810 phy_qcfg_resp.duplex_cfg =
811 PORT_PHY_QCFG_RESP_DUPLEX_CFG_FULL;
812 phy_qcfg_resp.duplex_state =
813 PORT_PHY_QCFG_RESP_DUPLEX_STATE_FULL;
814 phy_qcfg_resp.pause =
815 (PORT_PHY_QCFG_RESP_PAUSE_TX |
816 PORT_PHY_QCFG_RESP_PAUSE_RX);
819 /* force link down */
820 phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
821 phy_qcfg_resp.link_speed = 0;
822 phy_qcfg_resp.duplex_state =
823 PORT_PHY_QCFG_RESP_DUPLEX_STATE_HALF;
824 phy_qcfg_resp.pause = 0;
826 rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
827 phy_qcfg_req->resp_addr,
828 phy_qcfg_req->cmpl_ring,
829 sizeof(phy_qcfg_resp));
834 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
837 struct input *encap_req = vf->hwrm_cmd_req_addr;
838 u32 req_type = le16_to_cpu(encap_req->req_type);
841 case HWRM_CFA_L2_FILTER_ALLOC:
842 rc = bnxt_vf_validate_set_mac(bp, vf);
845 /* TODO Validate if VF is allowed to change mac address,
846 * mtu, num of rings etc
848 rc = bnxt_hwrm_exec_fwd_resp(
849 bp, vf, sizeof(struct hwrm_func_cfg_input));
851 case HWRM_PORT_PHY_QCFG:
852 rc = bnxt_vf_set_link(bp, vf);
860 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
862 u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
864 /* Scan through VF's and process commands */
866 vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
867 if (vf_id >= active_vfs)
870 clear_bit(vf_id, bp->pf.vf_event_bmap);
871 bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
876 void bnxt_update_vf_mac(struct bnxt *bp)
878 struct hwrm_func_qcaps_input req = {0};
879 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
881 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
882 req.fid = cpu_to_le16(0xffff);
884 mutex_lock(&bp->hwrm_cmd_lock);
885 if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
886 goto update_vf_mac_exit;
888 /* Store MAC address from the firmware. There are 2 cases:
889 * 1. MAC address is valid. It is assigned from the PF and we
890 * need to override the current VF MAC address with it.
891 * 2. MAC address is zero. The VF will use a random MAC address by
892 * default but the stored zero MAC will allow the VF user to change
893 * the random MAC address using ndo_set_mac_address() if he wants.
895 if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr))
896 memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN);
898 /* overwrite netdev dev_addr with admin VF MAC */
899 if (is_valid_ether_addr(bp->vf.mac_addr))
900 memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN);
902 mutex_unlock(&bp->hwrm_cmd_lock);
905 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
907 struct hwrm_func_vf_cfg_input req = {0};
913 if (bp->hwrm_spec_code < 0x10202) {
914 if (is_valid_ether_addr(bp->vf.mac_addr))
918 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
919 req.enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
920 memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
921 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
925 netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n",
932 void bnxt_sriov_disable(struct bnxt *bp)
936 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
938 netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
941 void bnxt_update_vf_mac(struct bnxt *bp)
945 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)