OSDN Git Service

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