net/mlx5: add VLAN push/pop DR commands to glue
[dpdk.git] / drivers / net / cxgbe / cxgbevf_main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Chelsio Communications.
3  * All rights reserved.
4  */
5
6 #include <rte_ethdev_driver.h>
7 #include <rte_ethdev_pci.h>
8 #include <rte_malloc.h>
9
10 #include "base/common.h"
11 #include "base/t4_regs.h"
12 #include "base/t4_msg.h"
13 #include "cxgbe.h"
14 #include "mps_tcam.h"
15
16 /*
17  * Figure out how many Ports and Queue Sets we can support.  This depends on
18  * knowing our Virtual Function Resources and may be called a second time if
19  * we fall back from MSI-X to MSI Interrupt Mode.
20  */
21 static void size_nports_qsets(struct adapter *adapter)
22 {
23         struct vf_resources *vfres = &adapter->params.vfres;
24         unsigned int pmask_nports;
25
26         /*
27          * The number of "ports" which we support is equal to the number of
28          * Virtual Interfaces with which we've been provisioned.
29          */
30         adapter->params.nports = vfres->nvi;
31         if (adapter->params.nports > MAX_NPORTS) {
32                 dev_warn(adapter->pdev_dev, "only using %d of %d maximum"
33                          " allowed virtual interfaces\n", MAX_NPORTS,
34                          adapter->params.nports);
35                 adapter->params.nports = MAX_NPORTS;
36         }
37
38         /*
39          * We may have been provisioned with more VIs than the number of
40          * ports we're allowed to access (our Port Access Rights Mask).
41          * This is obviously a configuration conflict but we don't want to
42          * do anything silly just because of that.
43          */
44         pmask_nports = hweight32(adapter->params.vfres.pmask);
45         if (pmask_nports < adapter->params.nports) {
46                 dev_warn(adapter->pdev_dev, "only using %d of %d provissioned"
47                          " virtual interfaces; limited by Port Access Rights"
48                          " mask %#x\n", pmask_nports, adapter->params.nports,
49                          adapter->params.vfres.pmask);
50                 adapter->params.nports = pmask_nports;
51         }
52
53         cxgbe_configure_max_ethqsets(adapter);
54         if (adapter->sge.max_ethqsets < adapter->params.nports) {
55                 dev_warn(adapter->pdev_dev, "only using %d of %d available"
56                          " virtual interfaces (too few Queue Sets)\n",
57                          adapter->sge.max_ethqsets, adapter->params.nports);
58                 adapter->params.nports = adapter->sge.max_ethqsets;
59         }
60 }
61
62 void cxgbevf_stats_get(struct port_info *pi, struct port_stats *stats)
63 {
64         t4vf_get_port_stats(pi->adapter, pi->pidx, stats);
65 }
66
67 static int adap_init0vf(struct adapter *adapter)
68 {
69         u32 param, val = 0;
70         int err;
71
72         err = t4vf_fw_reset(adapter);
73         if (err < 0) {
74                 dev_err(adapter->pdev_dev, "FW reset failed: err=%d\n", err);
75                 return err;
76         }
77
78         /*
79          * Grab basic operational parameters.  These will predominantly have
80          * been set up by the Physical Function Driver or will be hard coded
81          * into the adapter.  We just have to live with them ...  Note that
82          * we _must_ get our VPD parameters before our SGE parameters because
83          * we need to know the adapter's core clock from the VPD in order to
84          * properly decode the SGE Timer Values.
85          */
86         err = t4vf_get_dev_params(adapter);
87         if (err) {
88                 dev_err(adapter->pdev_dev, "unable to retrieve adapter"
89                         " device parameters: err=%d\n", err);
90                 return err;
91         }
92
93         err = t4vf_get_vpd_params(adapter);
94         if (err) {
95                 dev_err(adapter->pdev_dev, "unable to retrieve adapter"
96                         " VPD parameters: err=%d\n", err);
97                 return err;
98         }
99
100         adapter->pf = t4vf_get_pf_from_vf(adapter);
101         err = t4vf_sge_init(adapter);
102         if (err) {
103                 dev_err(adapter->pdev_dev, "error in sge init\n");
104                 return err;
105         }
106
107         err = t4vf_get_rss_glb_config(adapter);
108         if (err) {
109                 dev_err(adapter->pdev_dev, "unable to retrieve adapter"
110                         " RSS parameters: err=%d\n", err);
111                 return err;
112         }
113         if (adapter->params.rss.mode !=
114             FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL) {
115                 dev_err(adapter->pdev_dev, "unable to operate with global RSS"
116                         " mode %d\n", adapter->params.rss.mode);
117                 return -EINVAL;
118         }
119
120         /* If we're running on newer firmware, let it know that we're
121          * prepared to deal with encapsulated CPL messages.  Older
122          * firmware won't understand this and we'll just get
123          * unencapsulated messages ...
124          */
125         param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) |
126                 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_CPLFW4MSG_ENCAP);
127         val = 1;
128         t4vf_set_params(adapter, 1, &param, &val);
129
130         /*
131          * Grab our Virtual Interface resource allocation, extract the
132          * features that we're interested in and do a bit of sanity testing on
133          * what we discover.
134          */
135         err = t4vf_get_vfres(adapter);
136         if (err) {
137                 dev_err(adapter->pdev_dev, "unable to get virtual interface"
138                         " resources: err=%d\n", err);
139                 return err;
140         }
141
142         /*
143          * Check for various parameter sanity issues.
144          */
145         if (adapter->params.vfres.pmask == 0) {
146                 dev_err(adapter->pdev_dev, "no port access configured\n"
147                         "usable!\n");
148                 return -EINVAL;
149         }
150         if (adapter->params.vfres.nvi == 0) {
151                 dev_err(adapter->pdev_dev, "no virtual interfaces configured/"
152                         "usable!\n");
153                 return -EINVAL;
154         }
155
156         /*
157          * Initialize nports and max_ethqsets now that we have our Virtual
158          * Function Resources.
159          */
160         size_nports_qsets(adapter);
161         adapter->flags |= FW_OK;
162         return 0;
163 }
164
165 int cxgbevf_probe(struct adapter *adapter)
166 {
167         struct port_info *pi;
168         unsigned int pmask;
169         int err = 0;
170         int i;
171
172         t4_os_lock_init(&adapter->mbox_lock);
173         TAILQ_INIT(&adapter->mbox_list);
174         err = t4vf_prep_adapter(adapter);
175         if (err)
176                 return err;
177
178         if (!is_t4(adapter->params.chip)) {
179                 adapter->bar2 = (void *)adapter->pdev->mem_resource[2].addr;
180                 if (!adapter->bar2) {
181                         dev_err(adapter, "cannot map device bar2 region\n");
182                         err = -ENOMEM;
183                         return err;
184                 }
185         }
186
187         err = adap_init0vf(adapter);
188         if (err) {
189                 dev_err(adapter, "%s: Adapter initialization failed, error %d\n",
190                                 __func__, err);
191                 goto out_free;
192         }
193
194         pmask = adapter->params.vfres.pmask;
195         for_each_port(adapter, i) {
196                 const unsigned int numa_node = rte_socket_id();
197                 char name[RTE_ETH_NAME_MAX_LEN];
198                 struct rte_eth_dev *eth_dev;
199                 int port_id;
200
201                 if (pmask == 0)
202                         break;
203                 port_id = ffs(pmask) - 1;
204                 pmask &= ~(1 << port_id);
205
206                 snprintf(name, sizeof(name), "%s_%d",
207                          adapter->pdev->device.name, i);
208
209                 if (i == 0) {
210                         /* First port is already allocated by DPDK */
211                         eth_dev = adapter->eth_dev;
212                         goto allocate_mac;
213                 }
214
215                 /*
216                  * now do all data allocation - for eth_dev structure,
217                  * and internal (private) data for the remaining ports
218                  */
219
220                 /* reserve an ethdev entry */
221                 eth_dev = rte_eth_dev_allocate(name);
222                 if (!eth_dev) {
223                         err = -ENOMEM;
224                         goto out_free;
225                 }
226                 eth_dev->data->dev_private =
227                         rte_zmalloc_socket(name, sizeof(struct port_info),
228                                            RTE_CACHE_LINE_SIZE, numa_node);
229                 if (!eth_dev->data->dev_private)
230                         goto out_free;
231
232 allocate_mac:
233                 pi = eth_dev->data->dev_private;
234                 adapter->port[i] = pi;
235                 pi->eth_dev = eth_dev;
236                 pi->adapter = adapter;
237                 pi->xact_addr_filt = -1;
238                 pi->port_id = port_id;
239                 pi->pidx = i;
240
241                 pi->eth_dev->device = &adapter->pdev->device;
242                 pi->eth_dev->dev_ops = adapter->eth_dev->dev_ops;
243                 pi->eth_dev->tx_pkt_burst = adapter->eth_dev->tx_pkt_burst;
244                 pi->eth_dev->rx_pkt_burst = adapter->eth_dev->rx_pkt_burst;
245
246                 rte_eth_copy_pci_info(pi->eth_dev, adapter->pdev);
247                 pi->eth_dev->data->mac_addrs = rte_zmalloc(name,
248                                                         RTE_ETHER_ADDR_LEN, 0);
249                 if (!pi->eth_dev->data->mac_addrs) {
250                         dev_err(adapter, "%s: Mem allocation failed for storing mac addr, aborting\n",
251                                 __func__);
252                         err = -ENOMEM;
253                         goto out_free;
254                 }
255
256                 if (i > 0) {
257                         /* First port will be notified by upper layer */
258                         rte_eth_dev_probing_finish(eth_dev);
259                 }
260         }
261
262         if (adapter->flags & FW_OK) {
263                 err = t4vf_port_init(adapter);
264                 if (err) {
265                         dev_err(adapter, "%s: t4_port_init failed with err %d\n",
266                                 __func__, err);
267                         goto out_free;
268                 }
269         }
270
271         cxgbe_cfg_queues(adapter->eth_dev);
272         cxgbe_print_adapter_info(adapter);
273         cxgbe_print_port_info(adapter);
274
275         adapter->mpstcam = t4_init_mpstcam(adapter);
276         if (!adapter->mpstcam)
277                 dev_warn(adapter,
278                          "VF could not allocate mps tcam table. Continuing\n");
279
280         err = cxgbe_init_rss(adapter);
281         if (err)
282                 goto out_free;
283         return 0;
284
285 out_free:
286         for_each_port(adapter, i) {
287                 pi = adap2pinfo(adapter, i);
288                 if (pi->viid != 0)
289                         t4_free_vi(adapter, adapter->mbox, adapter->pf,
290                                    0, pi->viid);
291                 rte_eth_dev_release_port(pi->eth_dev);
292         }
293         return -err;
294 }