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