GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / net / ethernet / broadcom / bnxt / bnxt_sriov.c
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2017 Broadcom Limited
5  *
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.
9  */
10
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>
17 #include "bnxt_hsi.h"
18 #include "bnxt.h"
19 #include "bnxt_ulp.h"
20 #include "bnxt_sriov.h"
21 #include "bnxt_vfr.h"
22 #include "bnxt_ethtool.h"
23
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)
27 {
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;
31         int rc = 0;
32
33         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_ASYNC_EVENT_CMPL, -1, -1);
34         if (vf)
35                 req.encap_async_event_target_id = cpu_to_le16(vf->fw_fid);
36         else
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);
42
43         mutex_lock(&bp->hwrm_cmd_lock);
44         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
45
46         if (rc) {
47                 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl failed. rc:%d\n",
48                            rc);
49                 goto fwd_async_event_cmpl_exit;
50         }
51
52         if (resp->error_code) {
53                 netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl error %d\n",
54                            resp->error_code);
55                 rc = -1;
56         }
57
58 fwd_async_event_cmpl_exit:
59         mutex_unlock(&bp->hwrm_cmd_lock);
60         return rc;
61 }
62
63 static int bnxt_vf_ndo_prep(struct bnxt *bp, int vf_id)
64 {
65         if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
66                 netdev_err(bp->dev, "vf ndo called though PF is down\n");
67                 return -EINVAL;
68         }
69         if (!bp->pf.active_vfs) {
70                 netdev_err(bp->dev, "vf ndo called though sriov is disabled\n");
71                 return -EINVAL;
72         }
73         if (vf_id >= bp->pf.active_vfs) {
74                 netdev_err(bp->dev, "Invalid VF id %d\n", vf_id);
75                 return -EINVAL;
76         }
77         return 0;
78 }
79
80 int bnxt_set_vf_spoofchk(struct net_device *dev, int vf_id, bool setting)
81 {
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;
86         u32 func_flags;
87         int rc;
88
89         if (bp->hwrm_spec_code < 0x10701)
90                 return -ENOTSUPP;
91
92         rc = bnxt_vf_ndo_prep(bp, vf_id);
93         if (rc)
94                 return rc;
95
96         vf = &bp->pf.vf[vf_id];
97         if (vf->flags & BNXT_VF_SPOOFCHK)
98                 old_setting = true;
99         if (old_setting == setting)
100                 return 0;
101
102         if (setting)
103                 func_flags = FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK_ENABLE;
104         else
105                 func_flags = FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK_DISABLE;
106         /*TODO: if the driver supports VLAN filter on guest VLAN,
107          * the spoof check should also include vlan anti-spoofing
108          */
109         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
110         req.fid = cpu_to_le16(vf->fw_fid);
111         req.flags = cpu_to_le32(func_flags);
112         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
113         if (!rc) {
114                 if (setting)
115                         vf->flags |= BNXT_VF_SPOOFCHK;
116                 else
117                         vf->flags &= ~BNXT_VF_SPOOFCHK;
118         }
119         return rc;
120 }
121
122 int bnxt_get_vf_config(struct net_device *dev, int vf_id,
123                        struct ifla_vf_info *ivi)
124 {
125         struct bnxt *bp = netdev_priv(dev);
126         struct bnxt_vf_info *vf;
127         int rc;
128
129         rc = bnxt_vf_ndo_prep(bp, vf_id);
130         if (rc)
131                 return rc;
132
133         ivi->vf = vf_id;
134         vf = &bp->pf.vf[vf_id];
135
136         memcpy(&ivi->mac, vf->mac_addr, ETH_ALEN);
137         ivi->max_tx_rate = vf->max_tx_rate;
138         ivi->min_tx_rate = vf->min_tx_rate;
139         ivi->vlan = vf->vlan;
140         if (vf->flags & BNXT_VF_QOS)
141                 ivi->qos = vf->vlan >> VLAN_PRIO_SHIFT;
142         else
143                 ivi->qos = 0;
144         ivi->spoofchk = !!(vf->flags & BNXT_VF_SPOOFCHK);
145         if (!(vf->flags & BNXT_VF_LINK_FORCED))
146                 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
147         else if (vf->flags & BNXT_VF_LINK_UP)
148                 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
149         else
150                 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
151
152         return 0;
153 }
154
155 int bnxt_set_vf_mac(struct net_device *dev, int vf_id, u8 *mac)
156 {
157         struct hwrm_func_cfg_input req = {0};
158         struct bnxt *bp = netdev_priv(dev);
159         struct bnxt_vf_info *vf;
160         int rc;
161
162         rc = bnxt_vf_ndo_prep(bp, vf_id);
163         if (rc)
164                 return rc;
165         /* reject bc or mc mac addr, zero mac addr means allow
166          * VF to use its own mac addr
167          */
168         if (is_multicast_ether_addr(mac)) {
169                 netdev_err(dev, "Invalid VF ethernet address\n");
170                 return -EINVAL;
171         }
172         vf = &bp->pf.vf[vf_id];
173
174         memcpy(vf->mac_addr, mac, ETH_ALEN);
175         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
176         req.fid = cpu_to_le16(vf->fw_fid);
177         req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
178         memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
179         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
180 }
181
182 int bnxt_set_vf_vlan(struct net_device *dev, int vf_id, u16 vlan_id, u8 qos,
183                      __be16 vlan_proto)
184 {
185         struct hwrm_func_cfg_input req = {0};
186         struct bnxt *bp = netdev_priv(dev);
187         struct bnxt_vf_info *vf;
188         u16 vlan_tag;
189         int rc;
190
191         if (bp->hwrm_spec_code < 0x10201)
192                 return -ENOTSUPP;
193
194         if (vlan_proto != htons(ETH_P_8021Q))
195                 return -EPROTONOSUPPORT;
196
197         rc = bnxt_vf_ndo_prep(bp, vf_id);
198         if (rc)
199                 return rc;
200
201         /* TODO: needed to implement proper handling of user priority,
202          * currently fail the command if there is valid priority
203          */
204         if (vlan_id > 4095 || qos)
205                 return -EINVAL;
206
207         vf = &bp->pf.vf[vf_id];
208         vlan_tag = vlan_id;
209         if (vlan_tag == vf->vlan)
210                 return 0;
211
212         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
213         req.fid = cpu_to_le16(vf->fw_fid);
214         req.dflt_vlan = cpu_to_le16(vlan_tag);
215         req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
216         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
217         if (!rc)
218                 vf->vlan = vlan_tag;
219         return rc;
220 }
221
222 int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate,
223                    int max_tx_rate)
224 {
225         struct hwrm_func_cfg_input req = {0};
226         struct bnxt *bp = netdev_priv(dev);
227         struct bnxt_vf_info *vf;
228         u32 pf_link_speed;
229         int rc;
230
231         rc = bnxt_vf_ndo_prep(bp, vf_id);
232         if (rc)
233                 return rc;
234
235         vf = &bp->pf.vf[vf_id];
236         pf_link_speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
237         if (max_tx_rate > pf_link_speed) {
238                 netdev_info(bp->dev, "max tx rate %d exceed PF link speed for VF %d\n",
239                             max_tx_rate, vf_id);
240                 return -EINVAL;
241         }
242
243         if (min_tx_rate > pf_link_speed || min_tx_rate > max_tx_rate) {
244                 netdev_info(bp->dev, "min tx rate %d is invalid for VF %d\n",
245                             min_tx_rate, vf_id);
246                 return -EINVAL;
247         }
248         if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate)
249                 return 0;
250         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
251         req.fid = cpu_to_le16(vf->fw_fid);
252         req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW);
253         req.max_bw = cpu_to_le32(max_tx_rate);
254         req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MIN_BW);
255         req.min_bw = cpu_to_le32(min_tx_rate);
256         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
257         if (!rc) {
258                 vf->min_tx_rate = min_tx_rate;
259                 vf->max_tx_rate = max_tx_rate;
260         }
261         return rc;
262 }
263
264 int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link)
265 {
266         struct bnxt *bp = netdev_priv(dev);
267         struct bnxt_vf_info *vf;
268         int rc;
269
270         rc = bnxt_vf_ndo_prep(bp, vf_id);
271         if (rc)
272                 return rc;
273
274         vf = &bp->pf.vf[vf_id];
275
276         vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED);
277         switch (link) {
278         case IFLA_VF_LINK_STATE_AUTO:
279                 vf->flags |= BNXT_VF_LINK_UP;
280                 break;
281         case IFLA_VF_LINK_STATE_DISABLE:
282                 vf->flags |= BNXT_VF_LINK_FORCED;
283                 break;
284         case IFLA_VF_LINK_STATE_ENABLE:
285                 vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED;
286                 break;
287         default:
288                 netdev_err(bp->dev, "Invalid link option\n");
289                 rc = -EINVAL;
290                 break;
291         }
292         if (vf->flags & (BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED))
293                 rc = bnxt_hwrm_fwd_async_event_cmpl(bp, vf,
294                         ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE);
295         return rc;
296 }
297
298 static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs)
299 {
300         int i;
301         struct bnxt_vf_info *vf;
302
303         for (i = 0; i < num_vfs; i++) {
304                 vf = &bp->pf.vf[i];
305                 memset(vf, 0, sizeof(*vf));
306         }
307         return 0;
308 }
309
310 static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs)
311 {
312         int i, rc = 0;
313         struct bnxt_pf_info *pf = &bp->pf;
314         struct hwrm_func_vf_resc_free_input req = {0};
315
316         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESC_FREE, -1, -1);
317
318         mutex_lock(&bp->hwrm_cmd_lock);
319         for (i = pf->first_vf_id; i < pf->first_vf_id + num_vfs; i++) {
320                 req.vf_id = cpu_to_le16(i);
321                 rc = _hwrm_send_message(bp, &req, sizeof(req),
322                                         HWRM_CMD_TIMEOUT);
323                 if (rc)
324                         break;
325         }
326         mutex_unlock(&bp->hwrm_cmd_lock);
327         return rc;
328 }
329
330 static void bnxt_free_vf_resources(struct bnxt *bp)
331 {
332         struct pci_dev *pdev = bp->pdev;
333         int i;
334
335         kfree(bp->pf.vf_event_bmap);
336         bp->pf.vf_event_bmap = NULL;
337
338         for (i = 0; i < 4; i++) {
339                 if (bp->pf.hwrm_cmd_req_addr[i]) {
340                         dma_free_coherent(&pdev->dev, BNXT_PAGE_SIZE,
341                                           bp->pf.hwrm_cmd_req_addr[i],
342                                           bp->pf.hwrm_cmd_req_dma_addr[i]);
343                         bp->pf.hwrm_cmd_req_addr[i] = NULL;
344                 }
345         }
346
347         bp->pf.active_vfs = 0;
348         kfree(bp->pf.vf);
349         bp->pf.vf = NULL;
350 }
351
352 static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs)
353 {
354         struct pci_dev *pdev = bp->pdev;
355         u32 nr_pages, size, i, j, k = 0;
356
357         bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL);
358         if (!bp->pf.vf)
359                 return -ENOMEM;
360
361         bnxt_set_vf_attr(bp, num_vfs);
362
363         size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE;
364         nr_pages = size / BNXT_PAGE_SIZE;
365         if (size & (BNXT_PAGE_SIZE - 1))
366                 nr_pages++;
367
368         for (i = 0; i < nr_pages; i++) {
369                 bp->pf.hwrm_cmd_req_addr[i] =
370                         dma_alloc_coherent(&pdev->dev, BNXT_PAGE_SIZE,
371                                            &bp->pf.hwrm_cmd_req_dma_addr[i],
372                                            GFP_KERNEL);
373
374                 if (!bp->pf.hwrm_cmd_req_addr[i])
375                         return -ENOMEM;
376
377                 for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) {
378                         struct bnxt_vf_info *vf = &bp->pf.vf[k];
379
380                         vf->hwrm_cmd_req_addr = bp->pf.hwrm_cmd_req_addr[i] +
381                                                 j * BNXT_HWRM_REQ_MAX_SIZE;
382                         vf->hwrm_cmd_req_dma_addr =
383                                 bp->pf.hwrm_cmd_req_dma_addr[i] + j *
384                                 BNXT_HWRM_REQ_MAX_SIZE;
385                         k++;
386                 }
387         }
388
389         /* Max 128 VF's */
390         bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL);
391         if (!bp->pf.vf_event_bmap)
392                 return -ENOMEM;
393
394         bp->pf.hwrm_cmd_req_pages = nr_pages;
395         return 0;
396 }
397
398 static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
399 {
400         struct hwrm_func_buf_rgtr_input req = {0};
401
402         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BUF_RGTR, -1, -1);
403
404         req.req_buf_num_pages = cpu_to_le16(bp->pf.hwrm_cmd_req_pages);
405         req.req_buf_page_size = cpu_to_le16(BNXT_PAGE_SHIFT);
406         req.req_buf_len = cpu_to_le16(BNXT_HWRM_REQ_MAX_SIZE);
407         req.req_buf_page_addr0 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[0]);
408         req.req_buf_page_addr1 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[1]);
409         req.req_buf_page_addr2 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[2]);
410         req.req_buf_page_addr3 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[3]);
411
412         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
413 }
414
415 /* only call by PF to reserve resources for VF */
416 static int bnxt_hwrm_func_cfg(struct bnxt *bp, int num_vfs)
417 {
418         u32 rc = 0, mtu, i;
419         u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics;
420         u16 vf_ring_grps;
421         struct hwrm_func_cfg_input req = {0};
422         struct bnxt_pf_info *pf = &bp->pf;
423         int total_vf_tx_rings = 0;
424
425         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
426
427         /* Remaining rings are distributed equally amongs VF's for now */
428         vf_cp_rings = (pf->max_cp_rings - bp->cp_nr_rings) / num_vfs;
429         vf_stat_ctx = (pf->max_stat_ctxs - bp->num_stat_ctxs) / num_vfs;
430         if (bp->flags & BNXT_FLAG_AGG_RINGS)
431                 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings * 2) /
432                               num_vfs;
433         else
434                 vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings) / num_vfs;
435         vf_ring_grps = (bp->pf.max_hw_ring_grps - bp->rx_nr_rings) / num_vfs;
436         vf_tx_rings = (pf->max_tx_rings - bp->tx_nr_rings) / num_vfs;
437         vf_vnics = (pf->max_vnics - bp->nr_vnics) / num_vfs;
438         vf_vnics = min_t(u16, vf_vnics, vf_rx_rings);
439
440         req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MTU |
441                                   FUNC_CFG_REQ_ENABLES_MRU |
442                                   FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
443                                   FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
444                                   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
445                                   FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
446                                   FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
447                                   FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
448                                   FUNC_CFG_REQ_ENABLES_NUM_VNICS |
449                                   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);
450
451         mtu = bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
452         req.mru = cpu_to_le16(mtu);
453         req.mtu = cpu_to_le16(mtu);
454
455         req.num_rsscos_ctxs = cpu_to_le16(1);
456         req.num_cmpl_rings = cpu_to_le16(vf_cp_rings);
457         req.num_tx_rings = cpu_to_le16(vf_tx_rings);
458         req.num_rx_rings = cpu_to_le16(vf_rx_rings);
459         req.num_hw_ring_grps = cpu_to_le16(vf_ring_grps);
460         req.num_l2_ctxs = cpu_to_le16(4);
461
462         req.num_vnics = cpu_to_le16(vf_vnics);
463         /* FIXME spec currently uses 1 bit for stats ctx */
464         req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
465
466         mutex_lock(&bp->hwrm_cmd_lock);
467         for (i = 0; i < num_vfs; i++) {
468                 int vf_tx_rsvd = vf_tx_rings;
469
470                 req.fid = cpu_to_le16(pf->first_vf_id + i);
471                 rc = _hwrm_send_message(bp, &req, sizeof(req),
472                                         HWRM_CMD_TIMEOUT);
473                 if (rc)
474                         break;
475                 pf->active_vfs = i + 1;
476                 pf->vf[i].fw_fid = le16_to_cpu(req.fid);
477                 rc = __bnxt_hwrm_get_tx_rings(bp, pf->vf[i].fw_fid,
478                                               &vf_tx_rsvd);
479                 if (rc)
480                         break;
481                 total_vf_tx_rings += vf_tx_rsvd;
482         }
483         mutex_unlock(&bp->hwrm_cmd_lock);
484         if (!rc) {
485                 pf->max_tx_rings -= total_vf_tx_rings;
486                 pf->max_rx_rings -= vf_rx_rings * num_vfs;
487                 pf->max_hw_ring_grps -= vf_ring_grps * num_vfs;
488                 pf->max_cp_rings -= vf_cp_rings * num_vfs;
489                 pf->max_rsscos_ctxs -= num_vfs;
490                 pf->max_stat_ctxs -= vf_stat_ctx * num_vfs;
491                 pf->max_vnics -= vf_vnics * num_vfs;
492         }
493         return rc;
494 }
495
496 static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
497 {
498         int rc = 0, vfs_supported;
499         int min_rx_rings, min_tx_rings, min_rss_ctxs;
500         int tx_ok = 0, rx_ok = 0, rss_ok = 0;
501         int avail_cp, avail_stat;
502
503         /* Check if we can enable requested num of vf's. At a mininum
504          * we require 1 RX 1 TX rings for each VF. In this minimum conf
505          * features like TPA will not be available.
506          */
507         vfs_supported = *num_vfs;
508
509         avail_cp = bp->pf.max_cp_rings - bp->cp_nr_rings;
510         avail_stat = bp->pf.max_stat_ctxs - bp->num_stat_ctxs;
511         avail_cp = min_t(int, avail_cp, avail_stat);
512
513         while (vfs_supported) {
514                 min_rx_rings = vfs_supported;
515                 min_tx_rings = vfs_supported;
516                 min_rss_ctxs = vfs_supported;
517
518                 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
519                         if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >=
520                             min_rx_rings)
521                                 rx_ok = 1;
522                 } else {
523                         if (bp->pf.max_rx_rings - bp->rx_nr_rings >=
524                             min_rx_rings)
525                                 rx_ok = 1;
526                 }
527                 if (bp->pf.max_vnics - bp->nr_vnics < min_rx_rings ||
528                     avail_cp < min_rx_rings)
529                         rx_ok = 0;
530
531                 if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings &&
532                     avail_cp >= min_tx_rings)
533                         tx_ok = 1;
534
535                 if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs)
536                         rss_ok = 1;
537
538                 if (tx_ok && rx_ok && rss_ok)
539                         break;
540
541                 vfs_supported--;
542         }
543
544         if (!vfs_supported) {
545                 netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
546                 return -EINVAL;
547         }
548
549         if (vfs_supported != *num_vfs) {
550                 netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
551                             *num_vfs, vfs_supported);
552                 *num_vfs = vfs_supported;
553         }
554
555         rc = bnxt_alloc_vf_resources(bp, *num_vfs);
556         if (rc)
557                 goto err_out1;
558
559         /* Reserve resources for VFs */
560         rc = bnxt_hwrm_func_cfg(bp, *num_vfs);
561         if (rc)
562                 goto err_out2;
563
564         /* Register buffers for VFs */
565         rc = bnxt_hwrm_func_buf_rgtr(bp);
566         if (rc)
567                 goto err_out2;
568
569         bnxt_ulp_sriov_cfg(bp, *num_vfs);
570
571         rc = pci_enable_sriov(bp->pdev, *num_vfs);
572         if (rc)
573                 goto err_out2;
574
575         return 0;
576
577 err_out2:
578         /* Free the resources reserved for various VF's */
579         bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
580
581 err_out1:
582         bnxt_free_vf_resources(bp);
583
584         return rc;
585 }
586
587 void bnxt_sriov_disable(struct bnxt *bp)
588 {
589         u16 num_vfs = pci_num_vf(bp->pdev);
590
591         if (!num_vfs)
592                 return;
593
594         /* synchronize VF and VF-rep create and destroy */
595         mutex_lock(&bp->sriov_lock);
596         bnxt_vf_reps_destroy(bp);
597
598         if (pci_vfs_assigned(bp->pdev)) {
599                 bnxt_hwrm_fwd_async_event_cmpl(
600                         bp, NULL, ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD);
601                 netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
602                             num_vfs);
603         } else {
604                 pci_disable_sriov(bp->pdev);
605                 /* Free the HW resources reserved for various VF's */
606                 bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
607         }
608         mutex_unlock(&bp->sriov_lock);
609
610         bnxt_free_vf_resources(bp);
611
612         /* Reclaim all resources for the PF. */
613         rtnl_lock();
614         bnxt_restore_pf_fw_resources(bp);
615         rtnl_unlock();
616
617         bnxt_ulp_sriov_cfg(bp, 0);
618 }
619
620 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
621 {
622         struct net_device *dev = pci_get_drvdata(pdev);
623         struct bnxt *bp = netdev_priv(dev);
624
625         if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
626                 netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
627                 return 0;
628         }
629
630         rtnl_lock();
631         if (!netif_running(dev)) {
632                 netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
633                 rtnl_unlock();
634                 return 0;
635         }
636         bp->sriov_cfg = true;
637         rtnl_unlock();
638
639         if (pci_vfs_assigned(bp->pdev)) {
640                 netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
641                 num_vfs = 0;
642                 goto sriov_cfg_exit;
643         }
644
645         /* Check if enabled VFs is same as requested */
646         if (num_vfs && num_vfs == bp->pf.active_vfs)
647                 goto sriov_cfg_exit;
648
649         /* if there are previous existing VFs, clean them up */
650         bnxt_sriov_disable(bp);
651         if (!num_vfs)
652                 goto sriov_cfg_exit;
653
654         bnxt_sriov_enable(bp, &num_vfs);
655
656 sriov_cfg_exit:
657         bp->sriov_cfg = false;
658         wake_up(&bp->sriov_cfg_wait);
659
660         return num_vfs;
661 }
662
663 static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
664                               void *encap_resp, __le64 encap_resp_addr,
665                               __le16 encap_resp_cpr, u32 msg_size)
666 {
667         int rc = 0;
668         struct hwrm_fwd_resp_input req = {0};
669         struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
670
671         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
672
673         /* Set the new target id */
674         req.target_id = cpu_to_le16(vf->fw_fid);
675         req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
676         req.encap_resp_len = cpu_to_le16(msg_size);
677         req.encap_resp_addr = encap_resp_addr;
678         req.encap_resp_cmpl_ring = encap_resp_cpr;
679         memcpy(req.encap_resp, encap_resp, msg_size);
680
681         mutex_lock(&bp->hwrm_cmd_lock);
682         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
683
684         if (rc) {
685                 netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
686                 goto fwd_resp_exit;
687         }
688
689         if (resp->error_code) {
690                 netdev_err(bp->dev, "hwrm_fwd_resp error %d\n",
691                            resp->error_code);
692                 rc = -1;
693         }
694
695 fwd_resp_exit:
696         mutex_unlock(&bp->hwrm_cmd_lock);
697         return rc;
698 }
699
700 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
701                                   u32 msg_size)
702 {
703         int rc = 0;
704         struct hwrm_reject_fwd_resp_input req = {0};
705         struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
706
707         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1);
708         /* Set the new target id */
709         req.target_id = cpu_to_le16(vf->fw_fid);
710         req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
711         memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
712
713         mutex_lock(&bp->hwrm_cmd_lock);
714         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
715
716         if (rc) {
717                 netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
718                 goto fwd_err_resp_exit;
719         }
720
721         if (resp->error_code) {
722                 netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n",
723                            resp->error_code);
724                 rc = -1;
725         }
726
727 fwd_err_resp_exit:
728         mutex_unlock(&bp->hwrm_cmd_lock);
729         return rc;
730 }
731
732 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
733                                    u32 msg_size)
734 {
735         int rc = 0;
736         struct hwrm_exec_fwd_resp_input req = {0};
737         struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
738
739         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1);
740         /* Set the new target id */
741         req.target_id = cpu_to_le16(vf->fw_fid);
742         req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
743         memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
744
745         mutex_lock(&bp->hwrm_cmd_lock);
746         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
747
748         if (rc) {
749                 netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
750                 goto exec_fwd_resp_exit;
751         }
752
753         if (resp->error_code) {
754                 netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n",
755                            resp->error_code);
756                 rc = -1;
757         }
758
759 exec_fwd_resp_exit:
760         mutex_unlock(&bp->hwrm_cmd_lock);
761         return rc;
762 }
763
764 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
765 {
766         u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
767         struct hwrm_cfa_l2_filter_alloc_input *req =
768                 (struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
769
770         if (!is_valid_ether_addr(vf->mac_addr) ||
771             ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
772                 return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
773         else
774                 return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
775 }
776
777 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
778 {
779         int rc = 0;
780
781         if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
782                 /* real link */
783                 rc = bnxt_hwrm_exec_fwd_resp(
784                         bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
785         } else {
786                 struct hwrm_port_phy_qcfg_output phy_qcfg_resp;
787                 struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
788
789                 phy_qcfg_req =
790                 (struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
791                 mutex_lock(&bp->hwrm_cmd_lock);
792                 memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
793                        sizeof(phy_qcfg_resp));
794                 mutex_unlock(&bp->hwrm_cmd_lock);
795                 phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
796
797                 if (vf->flags & BNXT_VF_LINK_UP) {
798                         /* if physical link is down, force link up on VF */
799                         if (phy_qcfg_resp.link !=
800                             PORT_PHY_QCFG_RESP_LINK_LINK) {
801                                 phy_qcfg_resp.link =
802                                         PORT_PHY_QCFG_RESP_LINK_LINK;
803                                 phy_qcfg_resp.link_speed = cpu_to_le16(
804                                         PORT_PHY_QCFG_RESP_LINK_SPEED_10GB);
805                                 phy_qcfg_resp.duplex_cfg =
806                                         PORT_PHY_QCFG_RESP_DUPLEX_CFG_FULL;
807                                 phy_qcfg_resp.duplex_state =
808                                         PORT_PHY_QCFG_RESP_DUPLEX_STATE_FULL;
809                                 phy_qcfg_resp.pause =
810                                         (PORT_PHY_QCFG_RESP_PAUSE_TX |
811                                          PORT_PHY_QCFG_RESP_PAUSE_RX);
812                         }
813                 } else {
814                         /* force link down */
815                         phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
816                         phy_qcfg_resp.link_speed = 0;
817                         phy_qcfg_resp.duplex_state =
818                                 PORT_PHY_QCFG_RESP_DUPLEX_STATE_HALF;
819                         phy_qcfg_resp.pause = 0;
820                 }
821                 rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
822                                         phy_qcfg_req->resp_addr,
823                                         phy_qcfg_req->cmpl_ring,
824                                         sizeof(phy_qcfg_resp));
825         }
826         return rc;
827 }
828
829 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
830 {
831         int rc = 0;
832         struct input *encap_req = vf->hwrm_cmd_req_addr;
833         u32 req_type = le16_to_cpu(encap_req->req_type);
834
835         switch (req_type) {
836         case HWRM_CFA_L2_FILTER_ALLOC:
837                 rc = bnxt_vf_validate_set_mac(bp, vf);
838                 break;
839         case HWRM_FUNC_CFG:
840                 /* TODO Validate if VF is allowed to change mac address,
841                  * mtu, num of rings etc
842                  */
843                 rc = bnxt_hwrm_exec_fwd_resp(
844                         bp, vf, sizeof(struct hwrm_func_cfg_input));
845                 break;
846         case HWRM_PORT_PHY_QCFG:
847                 rc = bnxt_vf_set_link(bp, vf);
848                 break;
849         default:
850                 break;
851         }
852         return rc;
853 }
854
855 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
856 {
857         u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
858
859         /* Scan through VF's and process commands */
860         while (1) {
861                 vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
862                 if (vf_id >= active_vfs)
863                         break;
864
865                 clear_bit(vf_id, bp->pf.vf_event_bmap);
866                 bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
867                 i = vf_id + 1;
868         }
869 }
870
871 void bnxt_update_vf_mac(struct bnxt *bp)
872 {
873         struct hwrm_func_qcaps_input req = {0};
874         struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
875
876         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
877         req.fid = cpu_to_le16(0xffff);
878
879         mutex_lock(&bp->hwrm_cmd_lock);
880         if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
881                 goto update_vf_mac_exit;
882
883         /* Store MAC address from the firmware.  There are 2 cases:
884          * 1. MAC address is valid.  It is assigned from the PF and we
885          *    need to override the current VF MAC address with it.
886          * 2. MAC address is zero.  The VF will use a random MAC address by
887          *    default but the stored zero MAC will allow the VF user to change
888          *    the random MAC address using ndo_set_mac_address() if he wants.
889          */
890         if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr))
891                 memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN);
892
893         /* overwrite netdev dev_addr with admin VF MAC */
894         if (is_valid_ether_addr(bp->vf.mac_addr))
895                 memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN);
896 update_vf_mac_exit:
897         mutex_unlock(&bp->hwrm_cmd_lock);
898 }
899
900 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
901 {
902         struct hwrm_func_vf_cfg_input req = {0};
903         int rc = 0;
904
905         if (!BNXT_VF(bp))
906                 return 0;
907
908         if (bp->hwrm_spec_code < 0x10202) {
909                 if (is_valid_ether_addr(bp->vf.mac_addr))
910                         rc = -EADDRNOTAVAIL;
911                 goto mac_done;
912         }
913         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
914         req.enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
915         memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
916         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
917 mac_done:
918         if (rc) {
919                 rc = -EADDRNOTAVAIL;
920                 netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n",
921                             mac);
922         }
923         return rc;
924 }
925 #else
926
927 void bnxt_sriov_disable(struct bnxt *bp)
928 {
929 }
930
931 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
932 {
933         netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
934 }
935
936 void bnxt_update_vf_mac(struct bnxt *bp)
937 {
938 }
939
940 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
941 {
942         return 0;
943 }
944 #endif