Merge tag 'drm-intel-next-2018-12-04' of git://anongit.freedesktop.org/drm/drm-intel...
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / en / port.c
1 /*
2  * Copyright (c) 2018, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include "port.h"
34
35 /* speed in units of 1Mb */
36 static const u32 mlx5e_link_speed[MLX5E_LINK_MODES_NUMBER] = {
37         [MLX5E_1000BASE_CX_SGMII] = 1000,
38         [MLX5E_1000BASE_KX]       = 1000,
39         [MLX5E_10GBASE_CX4]       = 10000,
40         [MLX5E_10GBASE_KX4]       = 10000,
41         [MLX5E_10GBASE_KR]        = 10000,
42         [MLX5E_20GBASE_KR2]       = 20000,
43         [MLX5E_40GBASE_CR4]       = 40000,
44         [MLX5E_40GBASE_KR4]       = 40000,
45         [MLX5E_56GBASE_R4]        = 56000,
46         [MLX5E_10GBASE_CR]        = 10000,
47         [MLX5E_10GBASE_SR]        = 10000,
48         [MLX5E_10GBASE_ER]        = 10000,
49         [MLX5E_40GBASE_SR4]       = 40000,
50         [MLX5E_40GBASE_LR4]       = 40000,
51         [MLX5E_50GBASE_SR2]       = 50000,
52         [MLX5E_100GBASE_CR4]      = 100000,
53         [MLX5E_100GBASE_SR4]      = 100000,
54         [MLX5E_100GBASE_KR4]      = 100000,
55         [MLX5E_100GBASE_LR4]      = 100000,
56         [MLX5E_100BASE_TX]        = 100,
57         [MLX5E_1000BASE_T]        = 1000,
58         [MLX5E_10GBASE_T]         = 10000,
59         [MLX5E_25GBASE_CR]        = 25000,
60         [MLX5E_25GBASE_KR]        = 25000,
61         [MLX5E_25GBASE_SR]        = 25000,
62         [MLX5E_50GBASE_CR2]       = 50000,
63         [MLX5E_50GBASE_KR2]       = 50000,
64 };
65
66 u32 mlx5e_port_ptys2speed(u32 eth_proto_oper)
67 {
68         unsigned long temp = eth_proto_oper;
69         u32 speed = 0;
70         int i;
71
72         i = find_first_bit(&temp, MLX5E_LINK_MODES_NUMBER);
73         if (i < MLX5E_LINK_MODES_NUMBER)
74                 speed = mlx5e_link_speed[i];
75
76         return speed;
77 }
78
79 int mlx5e_port_linkspeed(struct mlx5_core_dev *mdev, u32 *speed)
80 {
81         u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {};
82         u32 eth_proto_oper;
83         int err;
84
85         err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 1);
86         if (err)
87                 return err;
88
89         eth_proto_oper = MLX5_GET(ptys_reg, out, eth_proto_oper);
90         *speed = mlx5e_port_ptys2speed(eth_proto_oper);
91         if (!(*speed))
92                 err = -EINVAL;
93
94         return err;
95 }
96
97 int mlx5e_port_max_linkspeed(struct mlx5_core_dev *mdev, u32 *speed)
98 {
99         u32 max_speed = 0;
100         u32 proto_cap;
101         int err;
102         int i;
103
104         err = mlx5_query_port_proto_cap(mdev, &proto_cap, MLX5_PTYS_EN);
105         if (err)
106                 return err;
107
108         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i)
109                 if (proto_cap & MLX5E_PROT_MASK(i))
110                         max_speed = max(max_speed, mlx5e_link_speed[i]);
111
112         *speed = max_speed;
113         return 0;
114 }
115
116 u32 mlx5e_port_speed2linkmodes(u32 speed)
117 {
118         u32 link_modes = 0;
119         int i;
120
121         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
122                 if (mlx5e_link_speed[i] == speed)
123                         link_modes |= MLX5E_PROT_MASK(i);
124         }
125
126         return link_modes;
127 }
128
129 int mlx5e_port_query_pbmc(struct mlx5_core_dev *mdev, void *out)
130 {
131         int sz = MLX5_ST_SZ_BYTES(pbmc_reg);
132         void *in;
133         int err;
134
135         in = kzalloc(sz, GFP_KERNEL);
136         if (!in)
137                 return -ENOMEM;
138
139         MLX5_SET(pbmc_reg, in, local_port, 1);
140         err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PBMC, 0, 0);
141
142         kfree(in);
143         return err;
144 }
145
146 int mlx5e_port_set_pbmc(struct mlx5_core_dev *mdev, void *in)
147 {
148         int sz = MLX5_ST_SZ_BYTES(pbmc_reg);
149         void *out;
150         int err;
151
152         out = kzalloc(sz, GFP_KERNEL);
153         if (!out)
154                 return -ENOMEM;
155
156         MLX5_SET(pbmc_reg, in, local_port, 1);
157         err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PBMC, 0, 1);
158
159         kfree(out);
160         return err;
161 }
162
163 /* buffer[i]: buffer that priority i mapped to */
164 int mlx5e_port_query_priority2buffer(struct mlx5_core_dev *mdev, u8 *buffer)
165 {
166         int sz = MLX5_ST_SZ_BYTES(pptb_reg);
167         u32 prio_x_buff;
168         void *out;
169         void *in;
170         int prio;
171         int err;
172
173         in = kzalloc(sz, GFP_KERNEL);
174         out = kzalloc(sz, GFP_KERNEL);
175         if (!in || !out) {
176                 err = -ENOMEM;
177                 goto out;
178         }
179
180         MLX5_SET(pptb_reg, in, local_port, 1);
181         err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPTB, 0, 0);
182         if (err)
183                 goto out;
184
185         prio_x_buff = MLX5_GET(pptb_reg, out, prio_x_buff);
186         for (prio = 0; prio < 8; prio++) {
187                 buffer[prio] = (u8)(prio_x_buff >> (4 * prio)) & 0xF;
188                 mlx5_core_dbg(mdev, "prio %d, buffer %d\n", prio, buffer[prio]);
189         }
190 out:
191         kfree(in);
192         kfree(out);
193         return err;
194 }
195
196 int mlx5e_port_set_priority2buffer(struct mlx5_core_dev *mdev, u8 *buffer)
197 {
198         int sz = MLX5_ST_SZ_BYTES(pptb_reg);
199         u32 prio_x_buff;
200         void *out;
201         void *in;
202         int prio;
203         int err;
204
205         in = kzalloc(sz, GFP_KERNEL);
206         out = kzalloc(sz, GFP_KERNEL);
207         if (!in || !out) {
208                 err = -ENOMEM;
209                 goto out;
210         }
211
212         /* First query the pptb register */
213         MLX5_SET(pptb_reg, in, local_port, 1);
214         err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPTB, 0, 0);
215         if (err)
216                 goto out;
217
218         memcpy(in, out, sz);
219         MLX5_SET(pptb_reg, in, local_port, 1);
220
221         /* Update the pm and prio_x_buff */
222         MLX5_SET(pptb_reg, in, pm, 0xFF);
223
224         prio_x_buff = 0;
225         for (prio = 0; prio < 8; prio++)
226                 prio_x_buff |= (buffer[prio] << (4 * prio));
227         MLX5_SET(pptb_reg, in, prio_x_buff, prio_x_buff);
228
229         err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPTB, 0, 1);
230
231 out:
232         kfree(in);
233         kfree(out);
234         return err;
235 }
236
237 static u32 fec_supported_speeds[] = {
238         10000,
239         40000,
240         25000,
241         50000,
242         56000,
243         100000
244 };
245
246 #define MLX5E_FEC_SUPPORTED_SPEEDS ARRAY_SIZE(fec_supported_speeds)
247
248 /* get/set FEC admin field for a given speed */
249 static int mlx5e_fec_admin_field(u32 *pplm,
250                                  u8 *fec_policy,
251                                  bool write,
252                                  u32 speed)
253 {
254         switch (speed) {
255         case 10000:
256         case 40000:
257                 if (!write)
258                         *fec_policy = MLX5_GET(pplm_reg, pplm,
259                                                fec_override_admin_10g_40g);
260                 else
261                         MLX5_SET(pplm_reg, pplm,
262                                  fec_override_admin_10g_40g, *fec_policy);
263                 break;
264         case 25000:
265                 if (!write)
266                         *fec_policy = MLX5_GET(pplm_reg, pplm,
267                                                fec_override_admin_25g);
268                 else
269                         MLX5_SET(pplm_reg, pplm,
270                                  fec_override_admin_25g, *fec_policy);
271                 break;
272         case 50000:
273                 if (!write)
274                         *fec_policy = MLX5_GET(pplm_reg, pplm,
275                                                fec_override_admin_50g);
276                 else
277                         MLX5_SET(pplm_reg, pplm,
278                                  fec_override_admin_50g, *fec_policy);
279                 break;
280         case 56000:
281                 if (!write)
282                         *fec_policy = MLX5_GET(pplm_reg, pplm,
283                                                fec_override_admin_56g);
284                 else
285                         MLX5_SET(pplm_reg, pplm,
286                                  fec_override_admin_56g, *fec_policy);
287                 break;
288         case 100000:
289                 if (!write)
290                         *fec_policy = MLX5_GET(pplm_reg, pplm,
291                                                fec_override_admin_100g);
292                 else
293                         MLX5_SET(pplm_reg, pplm,
294                                  fec_override_admin_100g, *fec_policy);
295                 break;
296         default:
297                 return -EINVAL;
298         }
299         return 0;
300 }
301
302 /* returns FEC capabilities for a given speed */
303 static int mlx5e_get_fec_cap_field(u32 *pplm,
304                                    u8 *fec_cap,
305                                    u32 speed)
306 {
307         switch (speed) {
308         case 10000:
309         case 40000:
310                 *fec_cap = MLX5_GET(pplm_reg, pplm,
311                                     fec_override_cap_10g_40g);
312                 break;
313         case 25000:
314                 *fec_cap = MLX5_GET(pplm_reg, pplm,
315                                     fec_override_cap_25g);
316                 break;
317         case 50000:
318                 *fec_cap = MLX5_GET(pplm_reg, pplm,
319                                     fec_override_cap_50g);
320                 break;
321         case 56000:
322                 *fec_cap = MLX5_GET(pplm_reg, pplm,
323                                     fec_override_cap_56g);
324                 break;
325         case 100000:
326                 *fec_cap = MLX5_GET(pplm_reg, pplm,
327                                     fec_override_cap_100g);
328                 break;
329         default:
330                 return -EINVAL;
331         }
332         return 0;
333 }
334
335 int mlx5e_get_fec_caps(struct mlx5_core_dev *dev, u8 *fec_caps)
336 {
337         u32 out[MLX5_ST_SZ_DW(pplm_reg)] = {};
338         u32 in[MLX5_ST_SZ_DW(pplm_reg)] = {};
339         int sz = MLX5_ST_SZ_BYTES(pplm_reg);
340         u32 current_fec_speed;
341         int err;
342
343         if (!MLX5_CAP_GEN(dev, pcam_reg))
344                 return -EOPNOTSUPP;
345
346         if (!MLX5_CAP_PCAM_REG(dev, pplm))
347                 return -EOPNOTSUPP;
348
349         MLX5_SET(pplm_reg, in, local_port, 1);
350         err =  mlx5_core_access_reg(dev, in, sz, out, sz, MLX5_REG_PPLM, 0, 0);
351         if (err)
352                 return err;
353
354         err = mlx5e_port_linkspeed(dev, &current_fec_speed);
355         if (err)
356                 return err;
357
358         return mlx5e_get_fec_cap_field(out, fec_caps, current_fec_speed);
359 }
360
361 int mlx5e_get_fec_mode(struct mlx5_core_dev *dev, u32 *fec_mode_active,
362                        u8 *fec_configured_mode)
363 {
364         u32 out[MLX5_ST_SZ_DW(pplm_reg)] = {};
365         u32 in[MLX5_ST_SZ_DW(pplm_reg)] = {};
366         int sz = MLX5_ST_SZ_BYTES(pplm_reg);
367         u32 link_speed;
368         int err;
369
370         if (!MLX5_CAP_GEN(dev, pcam_reg))
371                 return -EOPNOTSUPP;
372
373         if (!MLX5_CAP_PCAM_REG(dev, pplm))
374                 return -EOPNOTSUPP;
375
376         MLX5_SET(pplm_reg, in, local_port, 1);
377         err =  mlx5_core_access_reg(dev, in, sz, out, sz, MLX5_REG_PPLM, 0, 0);
378         if (err)
379                 return err;
380
381         *fec_mode_active = MLX5_GET(pplm_reg, out, fec_mode_active);
382
383         if (!fec_configured_mode)
384                 return 0;
385
386         err = mlx5e_port_linkspeed(dev, &link_speed);
387         if (err)
388                 return err;
389
390         return mlx5e_fec_admin_field(out, fec_configured_mode, 0, link_speed);
391 }
392
393 int mlx5e_set_fec_mode(struct mlx5_core_dev *dev, u8 fec_policy)
394 {
395         u8 fec_policy_nofec = BIT(MLX5E_FEC_NOFEC);
396         bool fec_mode_not_supp_in_speed = false;
397         u32 out[MLX5_ST_SZ_DW(pplm_reg)] = {};
398         u32 in[MLX5_ST_SZ_DW(pplm_reg)] = {};
399         int sz = MLX5_ST_SZ_BYTES(pplm_reg);
400         u8 fec_policy_auto = 0;
401         u8 fec_caps = 0;
402         int err;
403         int i;
404
405         if (!MLX5_CAP_GEN(dev, pcam_reg))
406                 return -EOPNOTSUPP;
407
408         if (!MLX5_CAP_PCAM_REG(dev, pplm))
409                 return -EOPNOTSUPP;
410
411         MLX5_SET(pplm_reg, in, local_port, 1);
412         err = mlx5_core_access_reg(dev, in, sz, out, sz, MLX5_REG_PPLM, 0, 0);
413         if (err)
414                 return err;
415
416         MLX5_SET(pplm_reg, out, local_port, 1);
417
418         for (i = 0; i < MLX5E_FEC_SUPPORTED_SPEEDS; i++) {
419                 mlx5e_get_fec_cap_field(out, &fec_caps, fec_supported_speeds[i]);
420                 /* policy supported for link speed, or policy is auto */
421                 if (fec_caps & fec_policy || fec_policy == fec_policy_auto) {
422                         mlx5e_fec_admin_field(out, &fec_policy, 1,
423                                               fec_supported_speeds[i]);
424                 } else {
425                         /* turn off FEC if supported. Else, leave it the same */
426                         if (fec_caps & fec_policy_nofec)
427                                 mlx5e_fec_admin_field(out, &fec_policy_nofec, 1,
428                                                       fec_supported_speeds[i]);
429                         fec_mode_not_supp_in_speed = true;
430                 }
431         }
432
433         if (fec_mode_not_supp_in_speed)
434                 mlx5_core_dbg(dev,
435                               "FEC policy 0x%x is not supported for some speeds",
436                               fec_policy);
437
438         return mlx5_core_access_reg(dev, out, sz, out, sz, MLX5_REG_PPLM, 0, 1);
439 }