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