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>
19 #include "bnxt_sriov.h"
20 #include "bnxt_ethtool.h"
22 #ifdef CONFIG_BNXT_SRIOV
23 static int bnxt_hwrm_fwd_async_event_cmpl(struct bnxt *bp,
24 struct bnxt_vf_info *vf, u16 event_id)
26 struct hwrm_fwd_async_event_cmpl_output *resp = bp->hwrm_cmd_resp_addr;
27 struct hwrm_fwd_async_event_cmpl_input req = {0};
28 struct hwrm_async_event_cmpl *async_cmpl;
31 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_ASYNC_EVENT_CMPL, -1, -1);
33 req.encap_async_event_target_id = cpu_to_le16(vf->fw_fid);
35 /* broadcast this async event to all VFs */
36 req.encap_async_event_target_id = cpu_to_le16(0xffff);
37 async_cmpl = (struct hwrm_async_event_cmpl *)req.encap_async_event_cmpl;
38 async_cmpl->type = cpu_to_le16(ASYNC_EVENT_CMPL_TYPE_HWRM_ASYNC_EVENT);
39 async_cmpl->event_id = cpu_to_le16(event_id);
41 mutex_lock(&bp->hwrm_cmd_lock);
42 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",
47 goto fwd_async_event_cmpl_exit;
50 if (resp->error_code) {
51 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl error %d\n",
56 fwd_async_event_cmpl_exit:
57 mutex_unlock(&bp->hwrm_cmd_lock);
61 static int bnxt_vf_ndo_prep(struct bnxt *bp, int vf_id)
63 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
64 netdev_err(bp->dev, "vf ndo called though PF is down\n");
67 if (!bp->pf.active_vfs) {
68 netdev_err(bp->dev, "vf ndo called though sriov is disabled\n");
71 if (vf_id >= bp->pf.max_vfs) {
72 netdev_err(bp->dev, "Invalid VF id %d\n", vf_id);
78 int bnxt_set_vf_spoofchk(struct net_device *dev, int vf_id, bool setting)
80 struct hwrm_func_cfg_input req = {0};
81 struct bnxt *bp = netdev_priv(dev);
82 struct bnxt_vf_info *vf;
83 bool old_setting = false;
87 rc = bnxt_vf_ndo_prep(bp, vf_id);
91 vf = &bp->pf.vf[vf_id];
92 if (vf->flags & BNXT_VF_SPOOFCHK)
94 if (old_setting == setting)
97 func_flags = vf->func_flags;
99 func_flags |= FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK;
101 func_flags &= ~FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK;
102 /*TODO: if the driver supports VLAN filter on guest VLAN,
103 * the spoof check should also include vlan anti-spoofing
105 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
106 req.fid = cpu_to_le16(vf->fw_fid);
107 req.flags = cpu_to_le32(func_flags);
108 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
110 vf->func_flags = func_flags;
112 vf->flags |= BNXT_VF_SPOOFCHK;
114 vf->flags &= ~BNXT_VF_SPOOFCHK;
119 int bnxt_get_vf_config(struct net_device *dev, int vf_id,
120 struct ifla_vf_info *ivi)
122 struct bnxt *bp = netdev_priv(dev);
123 struct bnxt_vf_info *vf;
126 rc = bnxt_vf_ndo_prep(bp, vf_id);
131 vf = &bp->pf.vf[vf_id];
133 memcpy(&ivi->mac, vf->mac_addr, ETH_ALEN);
134 ivi->max_tx_rate = vf->max_tx_rate;
135 ivi->min_tx_rate = vf->min_tx_rate;
136 ivi->vlan = vf->vlan;
137 ivi->qos = vf->flags & BNXT_VF_QOS;
138 ivi->spoofchk = vf->flags & BNXT_VF_SPOOFCHK;
139 if (!(vf->flags & BNXT_VF_LINK_FORCED))
140 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
141 else if (vf->flags & BNXT_VF_LINK_UP)
142 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
144 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
149 int bnxt_set_vf_mac(struct net_device *dev, int vf_id, u8 *mac)
151 struct hwrm_func_cfg_input req = {0};
152 struct bnxt *bp = netdev_priv(dev);
153 struct bnxt_vf_info *vf;
156 rc = bnxt_vf_ndo_prep(bp, vf_id);
159 /* reject bc or mc mac addr, zero mac addr means allow
160 * VF to use its own mac addr
162 if (is_multicast_ether_addr(mac)) {
163 netdev_err(dev, "Invalid VF ethernet address\n");
166 vf = &bp->pf.vf[vf_id];
168 memcpy(vf->mac_addr, mac, ETH_ALEN);
169 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
170 req.fid = cpu_to_le16(vf->fw_fid);
171 req.flags = cpu_to_le32(vf->func_flags);
172 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
173 memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
174 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
177 int bnxt_set_vf_vlan(struct net_device *dev, int vf_id, u16 vlan_id, u8 qos,
180 struct hwrm_func_cfg_input req = {0};
181 struct bnxt *bp = netdev_priv(dev);
182 struct bnxt_vf_info *vf;
186 if (bp->hwrm_spec_code < 0x10201)
189 if (vlan_proto != htons(ETH_P_8021Q))
190 return -EPROTONOSUPPORT;
192 rc = bnxt_vf_ndo_prep(bp, vf_id);
196 /* TODO: needed to implement proper handling of user priority,
197 * currently fail the command if there is valid priority
199 if (vlan_id > 4095 || qos)
202 vf = &bp->pf.vf[vf_id];
204 if (vlan_tag == vf->vlan)
207 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
208 req.fid = cpu_to_le16(vf->fw_fid);
209 req.flags = cpu_to_le32(vf->func_flags);
210 req.dflt_vlan = cpu_to_le16(vlan_tag);
211 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
212 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
218 int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate,
221 struct hwrm_func_cfg_input req = {0};
222 struct bnxt *bp = netdev_priv(dev);
223 struct bnxt_vf_info *vf;
227 rc = bnxt_vf_ndo_prep(bp, vf_id);
231 vf = &bp->pf.vf[vf_id];
232 pf_link_speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
233 if (max_tx_rate > pf_link_speed) {
234 netdev_info(bp->dev, "max tx rate %d exceed PF link speed for VF %d\n",
239 if (min_tx_rate > pf_link_speed || min_tx_rate > max_tx_rate) {
240 netdev_info(bp->dev, "min tx rate %d is invalid for VF %d\n",
244 if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate)
246 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
247 req.fid = cpu_to_le16(vf->fw_fid);
248 req.flags = cpu_to_le32(vf->func_flags);
249 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW);
250 req.max_bw = cpu_to_le32(max_tx_rate);
251 req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MIN_BW);
252 req.min_bw = cpu_to_le32(min_tx_rate);
253 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
255 vf->min_tx_rate = min_tx_rate;
256 vf->max_tx_rate = max_tx_rate;
261 int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link)
263 struct bnxt *bp = netdev_priv(dev);
264 struct bnxt_vf_info *vf;
267 rc = bnxt_vf_ndo_prep(bp, vf_id);
271 vf = &bp->pf.vf[vf_id];
273 vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED);
275 case IFLA_VF_LINK_STATE_AUTO:
276 vf->flags |= BNXT_VF_LINK_UP;
278 case IFLA_VF_LINK_STATE_DISABLE:
279 vf->flags |= BNXT_VF_LINK_FORCED;
281 case IFLA_VF_LINK_STATE_ENABLE:
282 vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED;
285 netdev_err(bp->dev, "Invalid link option\n");
289 if (vf->flags & (BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED))
290 rc = bnxt_hwrm_fwd_async_event_cmpl(bp, vf,
291 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE);
295 static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs)
298 struct bnxt_vf_info *vf;
300 for (i = 0; i < num_vfs; i++) {
302 memset(vf, 0, sizeof(*vf));
303 vf->flags = BNXT_VF_QOS | BNXT_VF_LINK_UP;
308 static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs)
311 struct bnxt_pf_info *pf = &bp->pf;
312 struct hwrm_func_vf_resc_free_input req = {0};
314 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESC_FREE, -1, -1);
316 mutex_lock(&bp->hwrm_cmd_lock);
317 for (i = pf->first_vf_id; i < pf->first_vf_id + num_vfs; i++) {
318 req.vf_id = cpu_to_le16(i);
319 rc = _hwrm_send_message(bp, &req, sizeof(req),
324 mutex_unlock(&bp->hwrm_cmd_lock);
328 static void bnxt_free_vf_resources(struct bnxt *bp)
330 struct pci_dev *pdev = bp->pdev;
333 kfree(bp->pf.vf_event_bmap);
334 bp->pf.vf_event_bmap = NULL;
336 for (i = 0; i < 4; i++) {
337 if (bp->pf.hwrm_cmd_req_addr[i]) {
338 dma_free_coherent(&pdev->dev, BNXT_PAGE_SIZE,
339 bp->pf.hwrm_cmd_req_addr[i],
340 bp->pf.hwrm_cmd_req_dma_addr[i]);
341 bp->pf.hwrm_cmd_req_addr[i] = NULL;
349 static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs)
351 struct pci_dev *pdev = bp->pdev;
352 u32 nr_pages, size, i, j, k = 0;
354 bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL);
358 bnxt_set_vf_attr(bp, num_vfs);
360 size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE;
361 nr_pages = size / BNXT_PAGE_SIZE;
362 if (size & (BNXT_PAGE_SIZE - 1))
365 for (i = 0; i < nr_pages; i++) {
366 bp->pf.hwrm_cmd_req_addr[i] =
367 dma_alloc_coherent(&pdev->dev, BNXT_PAGE_SIZE,
368 &bp->pf.hwrm_cmd_req_dma_addr[i],
371 if (!bp->pf.hwrm_cmd_req_addr[i])
374 for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) {
375 struct bnxt_vf_info *vf = &bp->pf.vf[k];
377 vf->hwrm_cmd_req_addr = bp->pf.hwrm_cmd_req_addr[i] +
378 j * BNXT_HWRM_REQ_MAX_SIZE;
379 vf->hwrm_cmd_req_dma_addr =
380 bp->pf.hwrm_cmd_req_dma_addr[i] + j *
381 BNXT_HWRM_REQ_MAX_SIZE;
387 bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL);
388 if (!bp->pf.vf_event_bmap)
391 bp->pf.hwrm_cmd_req_pages = nr_pages;
395 static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
397 struct hwrm_func_buf_rgtr_input req = {0};
399 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BUF_RGTR, -1, -1);
401 req.req_buf_num_pages = cpu_to_le16(bp->pf.hwrm_cmd_req_pages);
402 req.req_buf_page_size = cpu_to_le16(BNXT_PAGE_SHIFT);
403 req.req_buf_len = cpu_to_le16(BNXT_HWRM_REQ_MAX_SIZE);
404 req.req_buf_page_addr0 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[0]);
405 req.req_buf_page_addr1 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[1]);
406 req.req_buf_page_addr2 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[2]);
407 req.req_buf_page_addr3 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[3]);
409 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
412 /* only call by PF to reserve resources for VF */
413 static int bnxt_hwrm_func_cfg(struct bnxt *bp, int num_vfs)
416 u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics;
418 struct hwrm_func_cfg_input req = {0};
419 struct bnxt_pf_info *pf = &bp->pf;
420 int total_vf_tx_rings = 0;
422 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
424 /* Remaining rings are distributed equally amongs VF's for now */
425 vf_cp_rings = (pf->max_cp_rings - bp->cp_nr_rings) / num_vfs;
426 vf_stat_ctx = (pf->max_stat_ctxs - bp->num_stat_ctxs) / num_vfs;
427 if (bp->flags & BNXT_FLAG_AGG_RINGS)
428 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings * 2) /
431 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings) / num_vfs;
432 vf_ring_grps = (bp->pf.max_hw_ring_grps - bp->rx_nr_rings) / num_vfs;
433 vf_tx_rings = (pf->max_tx_rings - bp->tx_nr_rings) / num_vfs;
434 vf_vnics = (pf->max_vnics - bp->nr_vnics) / num_vfs;
435 vf_vnics = min_t(u16, vf_vnics, vf_rx_rings);
437 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MTU |
438 FUNC_CFG_REQ_ENABLES_MRU |
439 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
440 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
441 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
442 FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
443 FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
444 FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
445 FUNC_CFG_REQ_ENABLES_NUM_VNICS |
446 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);
448 mtu = bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
449 req.mru = cpu_to_le16(mtu);
450 req.mtu = cpu_to_le16(mtu);
452 req.num_rsscos_ctxs = cpu_to_le16(1);
453 req.num_cmpl_rings = cpu_to_le16(vf_cp_rings);
454 req.num_tx_rings = cpu_to_le16(vf_tx_rings);
455 req.num_rx_rings = cpu_to_le16(vf_rx_rings);
456 req.num_hw_ring_grps = cpu_to_le16(vf_ring_grps);
457 req.num_l2_ctxs = cpu_to_le16(4);
459 req.num_vnics = cpu_to_le16(vf_vnics);
460 /* FIXME spec currently uses 1 bit for stats ctx */
461 req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
463 mutex_lock(&bp->hwrm_cmd_lock);
464 for (i = 0; i < num_vfs; i++) {
465 int vf_tx_rsvd = vf_tx_rings;
467 req.fid = cpu_to_le16(pf->first_vf_id + i);
468 rc = _hwrm_send_message(bp, &req, sizeof(req),
472 pf->active_vfs = i + 1;
473 pf->vf[i].fw_fid = le16_to_cpu(req.fid);
474 rc = __bnxt_hwrm_get_tx_rings(bp, pf->vf[i].fw_fid,
478 total_vf_tx_rings += vf_tx_rsvd;
480 mutex_unlock(&bp->hwrm_cmd_lock);
482 pf->max_tx_rings -= total_vf_tx_rings;
483 pf->max_rx_rings -= vf_rx_rings * num_vfs;
484 pf->max_hw_ring_grps -= vf_ring_grps * num_vfs;
485 pf->max_cp_rings -= vf_cp_rings * num_vfs;
486 pf->max_rsscos_ctxs -= num_vfs;
487 pf->max_stat_ctxs -= vf_stat_ctx * num_vfs;
488 pf->max_vnics -= vf_vnics * num_vfs;
493 static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
495 int rc = 0, vfs_supported;
496 int min_rx_rings, min_tx_rings, min_rss_ctxs;
497 int tx_ok = 0, rx_ok = 0, rss_ok = 0;
499 /* Check if we can enable requested num of vf's. At a mininum
500 * we require 1 RX 1 TX rings for each VF. In this minimum conf
501 * features like TPA will not be available.
503 vfs_supported = *num_vfs;
505 while (vfs_supported) {
506 min_rx_rings = vfs_supported;
507 min_tx_rings = vfs_supported;
508 min_rss_ctxs = vfs_supported;
510 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
511 if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >=
515 if (bp->pf.max_rx_rings - bp->rx_nr_rings >=
519 if (bp->pf.max_vnics - bp->nr_vnics < min_rx_rings)
522 if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings)
525 if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs)
528 if (tx_ok && rx_ok && rss_ok)
534 if (!vfs_supported) {
535 netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
539 if (vfs_supported != *num_vfs) {
540 netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
541 *num_vfs, vfs_supported);
542 *num_vfs = vfs_supported;
545 rc = bnxt_alloc_vf_resources(bp, *num_vfs);
549 /* Reserve resources for VFs */
550 rc = bnxt_hwrm_func_cfg(bp, *num_vfs);
554 /* Register buffers for VFs */
555 rc = bnxt_hwrm_func_buf_rgtr(bp);
559 bnxt_ulp_sriov_cfg(bp, *num_vfs);
561 rc = pci_enable_sriov(bp->pdev, *num_vfs);
568 /* Free the resources reserved for various VF's */
569 bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
572 bnxt_free_vf_resources(bp);
577 void bnxt_sriov_disable(struct bnxt *bp)
579 u16 num_vfs = pci_num_vf(bp->pdev);
584 if (pci_vfs_assigned(bp->pdev)) {
585 bnxt_hwrm_fwd_async_event_cmpl(
586 bp, NULL, ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD);
587 netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
590 pci_disable_sriov(bp->pdev);
591 /* Free the HW resources reserved for various VF's */
592 bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
595 bnxt_free_vf_resources(bp);
597 bp->pf.active_vfs = 0;
598 /* Reclaim all resources for the PF. */
600 bnxt_restore_pf_fw_resources(bp);
603 bnxt_ulp_sriov_cfg(bp, 0);
606 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
608 struct net_device *dev = pci_get_drvdata(pdev);
609 struct bnxt *bp = netdev_priv(dev);
611 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
612 netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
617 if (!netif_running(dev)) {
618 netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
622 bp->sriov_cfg = true;
625 if (pci_vfs_assigned(bp->pdev)) {
626 netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
631 /* Check if enabled VFs is same as requested */
632 if (num_vfs && num_vfs == bp->pf.active_vfs)
635 /* if there are previous existing VFs, clean them up */
636 bnxt_sriov_disable(bp);
640 bnxt_sriov_enable(bp, &num_vfs);
643 bp->sriov_cfg = false;
644 wake_up(&bp->sriov_cfg_wait);
649 static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
650 void *encap_resp, __le64 encap_resp_addr,
651 __le16 encap_resp_cpr, u32 msg_size)
654 struct hwrm_fwd_resp_input req = {0};
655 struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
657 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
659 /* Set the new target id */
660 req.target_id = cpu_to_le16(vf->fw_fid);
661 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
662 req.encap_resp_len = cpu_to_le16(msg_size);
663 req.encap_resp_addr = encap_resp_addr;
664 req.encap_resp_cmpl_ring = encap_resp_cpr;
665 memcpy(req.encap_resp, encap_resp, msg_size);
667 mutex_lock(&bp->hwrm_cmd_lock);
668 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
671 netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
675 if (resp->error_code) {
676 netdev_err(bp->dev, "hwrm_fwd_resp error %d\n",
682 mutex_unlock(&bp->hwrm_cmd_lock);
686 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
690 struct hwrm_reject_fwd_resp_input req = {0};
691 struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
693 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1);
694 /* Set the new target id */
695 req.target_id = cpu_to_le16(vf->fw_fid);
696 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
697 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
699 mutex_lock(&bp->hwrm_cmd_lock);
700 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
703 netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
704 goto fwd_err_resp_exit;
707 if (resp->error_code) {
708 netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n",
714 mutex_unlock(&bp->hwrm_cmd_lock);
718 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
722 struct hwrm_exec_fwd_resp_input req = {0};
723 struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
725 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1);
726 /* Set the new target id */
727 req.target_id = cpu_to_le16(vf->fw_fid);
728 req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
729 memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
731 mutex_lock(&bp->hwrm_cmd_lock);
732 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
735 netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
736 goto exec_fwd_resp_exit;
739 if (resp->error_code) {
740 netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n",
746 mutex_unlock(&bp->hwrm_cmd_lock);
750 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
752 u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
753 struct hwrm_cfa_l2_filter_alloc_input *req =
754 (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
756 if (!is_valid_ether_addr(vf->mac_addr) ||
757 ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
758 return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
760 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
763 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
767 if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
769 rc = bnxt_hwrm_exec_fwd_resp(
770 bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
772 struct hwrm_port_phy_qcfg_output phy_qcfg_resp;
773 struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
776 (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
777 mutex_lock(&bp->hwrm_cmd_lock);
778 memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
779 sizeof(phy_qcfg_resp));
780 mutex_unlock(&bp->hwrm_cmd_lock);
781 phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
783 if (vf->flags & BNXT_VF_LINK_UP) {
784 /* if physical link is down, force link up on VF */
785 if (phy_qcfg_resp.link !=
786 PORT_PHY_QCFG_RESP_LINK_LINK) {
788 PORT_PHY_QCFG_RESP_LINK_LINK;
789 phy_qcfg_resp.link_speed = cpu_to_le16(
790 PORT_PHY_QCFG_RESP_LINK_SPEED_10GB);
791 phy_qcfg_resp.duplex =
792 PORT_PHY_QCFG_RESP_DUPLEX_FULL;
793 phy_qcfg_resp.pause =
794 (PORT_PHY_QCFG_RESP_PAUSE_TX |
795 PORT_PHY_QCFG_RESP_PAUSE_RX);
798 /* force link down */
799 phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
800 phy_qcfg_resp.link_speed = 0;
801 phy_qcfg_resp.duplex = PORT_PHY_QCFG_RESP_DUPLEX_HALF;
802 phy_qcfg_resp.pause = 0;
804 rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
805 phy_qcfg_req->resp_addr,
806 phy_qcfg_req->cmpl_ring,
807 sizeof(phy_qcfg_resp));
812 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
815 struct input *encap_req = vf->hwrm_cmd_req_addr;
816 u32 req_type = le16_to_cpu(encap_req->req_type);
819 case HWRM_CFA_L2_FILTER_ALLOC:
820 rc = bnxt_vf_validate_set_mac(bp, vf);
823 /* TODO Validate if VF is allowed to change mac address,
824 * mtu, num of rings etc
826 rc = bnxt_hwrm_exec_fwd_resp(
827 bp, vf, sizeof(struct hwrm_func_cfg_input));
829 case HWRM_PORT_PHY_QCFG:
830 rc = bnxt_vf_set_link(bp, vf);
838 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
840 u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
842 /* Scan through VF's and process commands */
844 vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
845 if (vf_id >= active_vfs)
848 clear_bit(vf_id, bp->pf.vf_event_bmap);
849 bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
854 void bnxt_update_vf_mac(struct bnxt *bp)
856 struct hwrm_func_qcaps_input req = {0};
857 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
859 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
860 req.fid = cpu_to_le16(0xffff);
862 mutex_lock(&bp->hwrm_cmd_lock);
863 if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
864 goto update_vf_mac_exit;
866 /* Store MAC address from the firmware. There are 2 cases:
867 * 1. MAC address is valid. It is assigned from the PF and we
868 * need to override the current VF MAC address with it.
869 * 2. MAC address is zero. The VF will use a random MAC address by
870 * default but the stored zero MAC will allow the VF user to change
871 * the random MAC address using ndo_set_mac_address() if he wants.
873 if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr))
874 memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN);
876 /* overwrite netdev dev_addr with admin VF MAC */
877 if (is_valid_ether_addr(bp->vf.mac_addr))
878 memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN);
880 mutex_unlock(&bp->hwrm_cmd_lock);
883 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
885 struct hwrm_func_vf_cfg_input req = {0};
891 if (bp->hwrm_spec_code < 0x10202) {
892 if (is_valid_ether_addr(bp->vf.mac_addr))
896 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
897 req.enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
898 memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
899 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
903 netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n",
910 void bnxt_sriov_disable(struct bnxt *bp)
914 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
916 netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
919 void bnxt_update_vf_mac(struct bnxt *bp)
923 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)