Linux-libre 4.19.116-gnu
[librecmc/linux-libre.git] / drivers / net / ethernet / mellanox / mlx5 / core / port.c
1 /*
2  * Copyright (c) 2013-2015, 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 <linux/module.h>
34 #include <linux/mlx5/driver.h>
35 #include <linux/mlx5/port.h>
36 #include <linux/mlx5/cmd.h>
37 #include "mlx5_core.h"
38
39 int mlx5_core_access_reg(struct mlx5_core_dev *dev, void *data_in,
40                          int size_in, void *data_out, int size_out,
41                          u16 reg_id, int arg, int write)
42 {
43         int outlen = MLX5_ST_SZ_BYTES(access_register_out) + size_out;
44         int inlen = MLX5_ST_SZ_BYTES(access_register_in) + size_in;
45         int err = -ENOMEM;
46         u32 *out = NULL;
47         u32 *in = NULL;
48         void *data;
49
50         in = kvzalloc(inlen, GFP_KERNEL);
51         out = kvzalloc(outlen, GFP_KERNEL);
52         if (!in || !out)
53                 goto out;
54
55         data = MLX5_ADDR_OF(access_register_in, in, register_data);
56         memcpy(data, data_in, size_in);
57
58         MLX5_SET(access_register_in, in, opcode, MLX5_CMD_OP_ACCESS_REG);
59         MLX5_SET(access_register_in, in, op_mod, !write);
60         MLX5_SET(access_register_in, in, argument, arg);
61         MLX5_SET(access_register_in, in, register_id, reg_id);
62
63         err = mlx5_cmd_exec(dev, in, inlen, out, outlen);
64         if (err)
65                 goto out;
66
67         data = MLX5_ADDR_OF(access_register_out, out, register_data);
68         memcpy(data_out, data, size_out);
69
70 out:
71         kvfree(out);
72         kvfree(in);
73         return err;
74 }
75 EXPORT_SYMBOL_GPL(mlx5_core_access_reg);
76
77 int mlx5_query_pcam_reg(struct mlx5_core_dev *dev, u32 *pcam, u8 feature_group,
78                         u8 access_reg_group)
79 {
80         u32 in[MLX5_ST_SZ_DW(pcam_reg)] = {0};
81         int sz = MLX5_ST_SZ_BYTES(pcam_reg);
82
83         MLX5_SET(pcam_reg, in, feature_group, feature_group);
84         MLX5_SET(pcam_reg, in, access_reg_group, access_reg_group);
85
86         return mlx5_core_access_reg(dev, in, sz, pcam, sz, MLX5_REG_PCAM, 0, 0);
87 }
88
89 int mlx5_query_mcam_reg(struct mlx5_core_dev *dev, u32 *mcam, u8 feature_group,
90                         u8 access_reg_group)
91 {
92         u32 in[MLX5_ST_SZ_DW(mcam_reg)] = {0};
93         int sz = MLX5_ST_SZ_BYTES(mcam_reg);
94
95         MLX5_SET(mcam_reg, in, feature_group, feature_group);
96         MLX5_SET(mcam_reg, in, access_reg_group, access_reg_group);
97
98         return mlx5_core_access_reg(dev, in, sz, mcam, sz, MLX5_REG_MCAM, 0, 0);
99 }
100
101 int mlx5_query_qcam_reg(struct mlx5_core_dev *mdev, u32 *qcam,
102                         u8 feature_group, u8 access_reg_group)
103 {
104         u32 in[MLX5_ST_SZ_DW(qcam_reg)] = {};
105         int sz = MLX5_ST_SZ_BYTES(qcam_reg);
106
107         MLX5_SET(qcam_reg, in, feature_group, feature_group);
108         MLX5_SET(qcam_reg, in, access_reg_group, access_reg_group);
109
110         return mlx5_core_access_reg(mdev, in, sz, qcam, sz, MLX5_REG_QCAM, 0, 0);
111 }
112
113 struct mlx5_reg_pcap {
114         u8                      rsvd0;
115         u8                      port_num;
116         u8                      rsvd1[2];
117         __be32                  caps_127_96;
118         __be32                  caps_95_64;
119         __be32                  caps_63_32;
120         __be32                  caps_31_0;
121 };
122
123 int mlx5_set_port_caps(struct mlx5_core_dev *dev, u8 port_num, u32 caps)
124 {
125         struct mlx5_reg_pcap in;
126         struct mlx5_reg_pcap out;
127
128         memset(&in, 0, sizeof(in));
129         in.caps_127_96 = cpu_to_be32(caps);
130         in.port_num = port_num;
131
132         return mlx5_core_access_reg(dev, &in, sizeof(in), &out,
133                                     sizeof(out), MLX5_REG_PCAP, 0, 1);
134 }
135 EXPORT_SYMBOL_GPL(mlx5_set_port_caps);
136
137 int mlx5_query_port_ptys(struct mlx5_core_dev *dev, u32 *ptys,
138                          int ptys_size, int proto_mask, u8 local_port)
139 {
140         u32 in[MLX5_ST_SZ_DW(ptys_reg)] = {0};
141
142         MLX5_SET(ptys_reg, in, local_port, local_port);
143         MLX5_SET(ptys_reg, in, proto_mask, proto_mask);
144         return mlx5_core_access_reg(dev, in, sizeof(in), ptys,
145                                     ptys_size, MLX5_REG_PTYS, 0, 0);
146 }
147 EXPORT_SYMBOL_GPL(mlx5_query_port_ptys);
148
149 int mlx5_set_port_beacon(struct mlx5_core_dev *dev, u16 beacon_duration)
150 {
151         u32 in[MLX5_ST_SZ_DW(mlcr_reg)]  = {0};
152         u32 out[MLX5_ST_SZ_DW(mlcr_reg)];
153
154         MLX5_SET(mlcr_reg, in, local_port, 1);
155         MLX5_SET(mlcr_reg, in, beacon_duration, beacon_duration);
156         return mlx5_core_access_reg(dev, in, sizeof(in), out,
157                                     sizeof(out), MLX5_REG_MLCR, 0, 1);
158 }
159
160 int mlx5_query_port_proto_cap(struct mlx5_core_dev *dev,
161                               u32 *proto_cap, int proto_mask)
162 {
163         u32 out[MLX5_ST_SZ_DW(ptys_reg)];
164         int err;
165
166         err = mlx5_query_port_ptys(dev, out, sizeof(out), proto_mask, 1);
167         if (err)
168                 return err;
169
170         if (proto_mask == MLX5_PTYS_EN)
171                 *proto_cap = MLX5_GET(ptys_reg, out, eth_proto_capability);
172         else
173                 *proto_cap = MLX5_GET(ptys_reg, out, ib_proto_capability);
174
175         return 0;
176 }
177 EXPORT_SYMBOL_GPL(mlx5_query_port_proto_cap);
178
179 int mlx5_query_port_proto_admin(struct mlx5_core_dev *dev,
180                                 u32 *proto_admin, int proto_mask)
181 {
182         u32 out[MLX5_ST_SZ_DW(ptys_reg)];
183         int err;
184
185         err = mlx5_query_port_ptys(dev, out, sizeof(out), proto_mask, 1);
186         if (err)
187                 return err;
188
189         if (proto_mask == MLX5_PTYS_EN)
190                 *proto_admin = MLX5_GET(ptys_reg, out, eth_proto_admin);
191         else
192                 *proto_admin = MLX5_GET(ptys_reg, out, ib_proto_admin);
193
194         return 0;
195 }
196 EXPORT_SYMBOL_GPL(mlx5_query_port_proto_admin);
197
198 int mlx5_query_port_link_width_oper(struct mlx5_core_dev *dev,
199                                     u8 *link_width_oper, u8 local_port)
200 {
201         u32 out[MLX5_ST_SZ_DW(ptys_reg)];
202         int err;
203
204         err = mlx5_query_port_ptys(dev, out, sizeof(out), MLX5_PTYS_IB, local_port);
205         if (err)
206                 return err;
207
208         *link_width_oper = MLX5_GET(ptys_reg, out, ib_link_width_oper);
209
210         return 0;
211 }
212 EXPORT_SYMBOL_GPL(mlx5_query_port_link_width_oper);
213
214 int mlx5_query_port_eth_proto_oper(struct mlx5_core_dev *dev,
215                                    u32 *proto_oper, u8 local_port)
216 {
217         u32 out[MLX5_ST_SZ_DW(ptys_reg)];
218         int err;
219
220         err = mlx5_query_port_ptys(dev, out, sizeof(out), MLX5_PTYS_EN,
221                                    local_port);
222         if (err)
223                 return err;
224
225         *proto_oper = MLX5_GET(ptys_reg, out, eth_proto_oper);
226
227         return 0;
228 }
229 EXPORT_SYMBOL(mlx5_query_port_eth_proto_oper);
230
231 int mlx5_query_port_ib_proto_oper(struct mlx5_core_dev *dev,
232                                   u8 *proto_oper, u8 local_port)
233 {
234         u32 out[MLX5_ST_SZ_DW(ptys_reg)];
235         int err;
236
237         err = mlx5_query_port_ptys(dev, out, sizeof(out), MLX5_PTYS_IB,
238                                    local_port);
239         if (err)
240                 return err;
241
242         *proto_oper = MLX5_GET(ptys_reg, out, ib_proto_oper);
243
244         return 0;
245 }
246 EXPORT_SYMBOL(mlx5_query_port_ib_proto_oper);
247
248 int mlx5_set_port_ptys(struct mlx5_core_dev *dev, bool an_disable,
249                        u32 proto_admin, int proto_mask)
250 {
251         u32 out[MLX5_ST_SZ_DW(ptys_reg)];
252         u32 in[MLX5_ST_SZ_DW(ptys_reg)];
253         u8 an_disable_admin;
254         u8 an_disable_cap;
255         u8 an_status;
256
257         mlx5_query_port_autoneg(dev, proto_mask, &an_status,
258                                 &an_disable_cap, &an_disable_admin);
259         if (!an_disable_cap && an_disable)
260                 return -EPERM;
261
262         memset(in, 0, sizeof(in));
263
264         MLX5_SET(ptys_reg, in, local_port, 1);
265         MLX5_SET(ptys_reg, in, an_disable_admin, an_disable);
266         MLX5_SET(ptys_reg, in, proto_mask, proto_mask);
267         if (proto_mask == MLX5_PTYS_EN)
268                 MLX5_SET(ptys_reg, in, eth_proto_admin, proto_admin);
269         else
270                 MLX5_SET(ptys_reg, in, ib_proto_admin, proto_admin);
271
272         return mlx5_core_access_reg(dev, in, sizeof(in), out,
273                                     sizeof(out), MLX5_REG_PTYS, 0, 1);
274 }
275 EXPORT_SYMBOL_GPL(mlx5_set_port_ptys);
276
277 /* This function should be used after setting a port register only */
278 void mlx5_toggle_port_link(struct mlx5_core_dev *dev)
279 {
280         enum mlx5_port_status ps;
281
282         mlx5_query_port_admin_status(dev, &ps);
283         mlx5_set_port_admin_status(dev, MLX5_PORT_DOWN);
284         if (ps == MLX5_PORT_UP)
285                 mlx5_set_port_admin_status(dev, MLX5_PORT_UP);
286 }
287 EXPORT_SYMBOL_GPL(mlx5_toggle_port_link);
288
289 int mlx5_set_port_admin_status(struct mlx5_core_dev *dev,
290                                enum mlx5_port_status status)
291 {
292         u32 in[MLX5_ST_SZ_DW(paos_reg)] = {0};
293         u32 out[MLX5_ST_SZ_DW(paos_reg)];
294
295         MLX5_SET(paos_reg, in, local_port, 1);
296         MLX5_SET(paos_reg, in, admin_status, status);
297         MLX5_SET(paos_reg, in, ase, 1);
298         return mlx5_core_access_reg(dev, in, sizeof(in), out,
299                                     sizeof(out), MLX5_REG_PAOS, 0, 1);
300 }
301 EXPORT_SYMBOL_GPL(mlx5_set_port_admin_status);
302
303 int mlx5_query_port_admin_status(struct mlx5_core_dev *dev,
304                                  enum mlx5_port_status *status)
305 {
306         u32 in[MLX5_ST_SZ_DW(paos_reg)] = {0};
307         u32 out[MLX5_ST_SZ_DW(paos_reg)];
308         int err;
309
310         MLX5_SET(paos_reg, in, local_port, 1);
311         err = mlx5_core_access_reg(dev, in, sizeof(in), out,
312                                    sizeof(out), MLX5_REG_PAOS, 0, 0);
313         if (err)
314                 return err;
315         *status = MLX5_GET(paos_reg, out, admin_status);
316         return 0;
317 }
318 EXPORT_SYMBOL_GPL(mlx5_query_port_admin_status);
319
320 static void mlx5_query_port_mtu(struct mlx5_core_dev *dev, u16 *admin_mtu,
321                                 u16 *max_mtu, u16 *oper_mtu, u8 port)
322 {
323         u32 in[MLX5_ST_SZ_DW(pmtu_reg)] = {0};
324         u32 out[MLX5_ST_SZ_DW(pmtu_reg)];
325
326         MLX5_SET(pmtu_reg, in, local_port, port);
327         mlx5_core_access_reg(dev, in, sizeof(in), out,
328                              sizeof(out), MLX5_REG_PMTU, 0, 0);
329
330         if (max_mtu)
331                 *max_mtu  = MLX5_GET(pmtu_reg, out, max_mtu);
332         if (oper_mtu)
333                 *oper_mtu = MLX5_GET(pmtu_reg, out, oper_mtu);
334         if (admin_mtu)
335                 *admin_mtu = MLX5_GET(pmtu_reg, out, admin_mtu);
336 }
337
338 int mlx5_set_port_mtu(struct mlx5_core_dev *dev, u16 mtu, u8 port)
339 {
340         u32 in[MLX5_ST_SZ_DW(pmtu_reg)] = {0};
341         u32 out[MLX5_ST_SZ_DW(pmtu_reg)];
342
343         MLX5_SET(pmtu_reg, in, admin_mtu, mtu);
344         MLX5_SET(pmtu_reg, in, local_port, port);
345         return mlx5_core_access_reg(dev, in, sizeof(in), out,
346                                    sizeof(out), MLX5_REG_PMTU, 0, 1);
347 }
348 EXPORT_SYMBOL_GPL(mlx5_set_port_mtu);
349
350 void mlx5_query_port_max_mtu(struct mlx5_core_dev *dev, u16 *max_mtu,
351                              u8 port)
352 {
353         mlx5_query_port_mtu(dev, NULL, max_mtu, NULL, port);
354 }
355 EXPORT_SYMBOL_GPL(mlx5_query_port_max_mtu);
356
357 void mlx5_query_port_oper_mtu(struct mlx5_core_dev *dev, u16 *oper_mtu,
358                               u8 port)
359 {
360         mlx5_query_port_mtu(dev, NULL, NULL, oper_mtu, port);
361 }
362 EXPORT_SYMBOL_GPL(mlx5_query_port_oper_mtu);
363
364 static int mlx5_query_module_num(struct mlx5_core_dev *dev, int *module_num)
365 {
366         u32 in[MLX5_ST_SZ_DW(pmlp_reg)] = {0};
367         u32 out[MLX5_ST_SZ_DW(pmlp_reg)];
368         int module_mapping;
369         int err;
370
371         MLX5_SET(pmlp_reg, in, local_port, 1);
372         err = mlx5_core_access_reg(dev, in, sizeof(in), out, sizeof(out),
373                                    MLX5_REG_PMLP, 0, 0);
374         if (err)
375                 return err;
376
377         module_mapping = MLX5_GET(pmlp_reg, out, lane0_module_mapping);
378         *module_num = module_mapping & MLX5_EEPROM_IDENTIFIER_BYTE_MASK;
379
380         return 0;
381 }
382
383 int mlx5_query_module_eeprom(struct mlx5_core_dev *dev,
384                              u16 offset, u16 size, u8 *data)
385 {
386         u32 out[MLX5_ST_SZ_DW(mcia_reg)];
387         u32 in[MLX5_ST_SZ_DW(mcia_reg)];
388         int module_num;
389         u16 i2c_addr;
390         int status;
391         int err;
392         void *ptr = MLX5_ADDR_OF(mcia_reg, out, dword_0);
393
394         err = mlx5_query_module_num(dev, &module_num);
395         if (err)
396                 return err;
397
398         memset(in, 0, sizeof(in));
399         size = min_t(int, size, MLX5_EEPROM_MAX_BYTES);
400
401         if (offset < MLX5_EEPROM_PAGE_LENGTH &&
402             offset + size > MLX5_EEPROM_PAGE_LENGTH)
403                 /* Cross pages read, read until offset 256 in low page */
404                 size -= offset + size - MLX5_EEPROM_PAGE_LENGTH;
405
406         i2c_addr = MLX5_I2C_ADDR_LOW;
407
408         MLX5_SET(mcia_reg, in, l, 0);
409         MLX5_SET(mcia_reg, in, module, module_num);
410         MLX5_SET(mcia_reg, in, i2c_device_address, i2c_addr);
411         MLX5_SET(mcia_reg, in, page_number, 0);
412         MLX5_SET(mcia_reg, in, device_address, offset);
413         MLX5_SET(mcia_reg, in, size, size);
414
415         err = mlx5_core_access_reg(dev, in, sizeof(in), out,
416                                    sizeof(out), MLX5_REG_MCIA, 0, 0);
417         if (err)
418                 return err;
419
420         status = MLX5_GET(mcia_reg, out, status);
421         if (status) {
422                 mlx5_core_err(dev, "query_mcia_reg failed: status: 0x%x\n",
423                               status);
424                 return -EIO;
425         }
426
427         memcpy(data, ptr, size);
428
429         return size;
430 }
431 EXPORT_SYMBOL_GPL(mlx5_query_module_eeprom);
432
433 static int mlx5_query_port_pvlc(struct mlx5_core_dev *dev, u32 *pvlc,
434                                 int pvlc_size,  u8 local_port)
435 {
436         u32 in[MLX5_ST_SZ_DW(pvlc_reg)] = {0};
437
438         MLX5_SET(pvlc_reg, in, local_port, local_port);
439         return mlx5_core_access_reg(dev, in, sizeof(in), pvlc,
440                                     pvlc_size, MLX5_REG_PVLC, 0, 0);
441 }
442
443 int mlx5_query_port_vl_hw_cap(struct mlx5_core_dev *dev,
444                               u8 *vl_hw_cap, u8 local_port)
445 {
446         u32 out[MLX5_ST_SZ_DW(pvlc_reg)];
447         int err;
448
449         err = mlx5_query_port_pvlc(dev, out, sizeof(out), local_port);
450         if (err)
451                 return err;
452
453         *vl_hw_cap = MLX5_GET(pvlc_reg, out, vl_hw_cap);
454
455         return 0;
456 }
457 EXPORT_SYMBOL_GPL(mlx5_query_port_vl_hw_cap);
458
459 int mlx5_core_query_ib_ppcnt(struct mlx5_core_dev *dev,
460                              u8 port_num, void *out, size_t sz)
461 {
462         u32 *in;
463         int err;
464
465         in  = kvzalloc(sz, GFP_KERNEL);
466         if (!in) {
467                 err = -ENOMEM;
468                 return err;
469         }
470
471         MLX5_SET(ppcnt_reg, in, local_port, port_num);
472
473         MLX5_SET(ppcnt_reg, in, grp, MLX5_INFINIBAND_PORT_COUNTERS_GROUP);
474         err = mlx5_core_access_reg(dev, in, sz, out,
475                                    sz, MLX5_REG_PPCNT, 0, 0);
476
477         kvfree(in);
478         return err;
479 }
480 EXPORT_SYMBOL_GPL(mlx5_core_query_ib_ppcnt);
481
482 static int mlx5_query_pfcc_reg(struct mlx5_core_dev *dev, u32 *out,
483                                u32 out_size)
484 {
485         u32 in[MLX5_ST_SZ_DW(pfcc_reg)] = {0};
486
487         MLX5_SET(pfcc_reg, in, local_port, 1);
488
489         return mlx5_core_access_reg(dev, in, sizeof(in), out,
490                                     out_size, MLX5_REG_PFCC, 0, 0);
491 }
492
493 int mlx5_set_port_pause(struct mlx5_core_dev *dev, u32 rx_pause, u32 tx_pause)
494 {
495         u32 in[MLX5_ST_SZ_DW(pfcc_reg)] = {0};
496         u32 out[MLX5_ST_SZ_DW(pfcc_reg)];
497
498         MLX5_SET(pfcc_reg, in, local_port, 1);
499         MLX5_SET(pfcc_reg, in, pptx, tx_pause);
500         MLX5_SET(pfcc_reg, in, pprx, rx_pause);
501
502         return mlx5_core_access_reg(dev, in, sizeof(in), out,
503                                     sizeof(out), MLX5_REG_PFCC, 0, 1);
504 }
505 EXPORT_SYMBOL_GPL(mlx5_set_port_pause);
506
507 int mlx5_query_port_pause(struct mlx5_core_dev *dev,
508                           u32 *rx_pause, u32 *tx_pause)
509 {
510         u32 out[MLX5_ST_SZ_DW(pfcc_reg)];
511         int err;
512
513         err = mlx5_query_pfcc_reg(dev, out, sizeof(out));
514         if (err)
515                 return err;
516
517         if (rx_pause)
518                 *rx_pause = MLX5_GET(pfcc_reg, out, pprx);
519
520         if (tx_pause)
521                 *tx_pause = MLX5_GET(pfcc_reg, out, pptx);
522
523         return 0;
524 }
525 EXPORT_SYMBOL_GPL(mlx5_query_port_pause);
526
527 int mlx5_set_port_stall_watermark(struct mlx5_core_dev *dev,
528                                   u16 stall_critical_watermark,
529                                   u16 stall_minor_watermark)
530 {
531         u32 in[MLX5_ST_SZ_DW(pfcc_reg)] = {0};
532         u32 out[MLX5_ST_SZ_DW(pfcc_reg)];
533
534         MLX5_SET(pfcc_reg, in, local_port, 1);
535         MLX5_SET(pfcc_reg, in, pptx_mask_n, 1);
536         MLX5_SET(pfcc_reg, in, pprx_mask_n, 1);
537         MLX5_SET(pfcc_reg, in, ppan_mask_n, 1);
538         MLX5_SET(pfcc_reg, in, critical_stall_mask, 1);
539         MLX5_SET(pfcc_reg, in, minor_stall_mask, 1);
540         MLX5_SET(pfcc_reg, in, device_stall_critical_watermark,
541                  stall_critical_watermark);
542         MLX5_SET(pfcc_reg, in, device_stall_minor_watermark, stall_minor_watermark);
543
544         return mlx5_core_access_reg(dev, in, sizeof(in), out,
545                                     sizeof(out), MLX5_REG_PFCC, 0, 1);
546 }
547
548 int mlx5_query_port_stall_watermark(struct mlx5_core_dev *dev,
549                                     u16 *stall_critical_watermark,
550                                     u16 *stall_minor_watermark)
551 {
552         u32 out[MLX5_ST_SZ_DW(pfcc_reg)];
553         int err;
554
555         err = mlx5_query_pfcc_reg(dev, out, sizeof(out));
556         if (err)
557                 return err;
558
559         if (stall_critical_watermark)
560                 *stall_critical_watermark = MLX5_GET(pfcc_reg, out,
561                                                      device_stall_critical_watermark);
562
563         if (stall_minor_watermark)
564                 *stall_minor_watermark = MLX5_GET(pfcc_reg, out,
565                                                   device_stall_minor_watermark);
566
567         return 0;
568 }
569
570 int mlx5_set_port_pfc(struct mlx5_core_dev *dev, u8 pfc_en_tx, u8 pfc_en_rx)
571 {
572         u32 in[MLX5_ST_SZ_DW(pfcc_reg)] = {0};
573         u32 out[MLX5_ST_SZ_DW(pfcc_reg)];
574
575         MLX5_SET(pfcc_reg, in, local_port, 1);
576         MLX5_SET(pfcc_reg, in, pfctx, pfc_en_tx);
577         MLX5_SET(pfcc_reg, in, pfcrx, pfc_en_rx);
578         MLX5_SET_TO_ONES(pfcc_reg, in, prio_mask_tx);
579         MLX5_SET_TO_ONES(pfcc_reg, in, prio_mask_rx);
580
581         return mlx5_core_access_reg(dev, in, sizeof(in), out,
582                                     sizeof(out), MLX5_REG_PFCC, 0, 1);
583 }
584 EXPORT_SYMBOL_GPL(mlx5_set_port_pfc);
585
586 int mlx5_query_port_pfc(struct mlx5_core_dev *dev, u8 *pfc_en_tx, u8 *pfc_en_rx)
587 {
588         u32 out[MLX5_ST_SZ_DW(pfcc_reg)];
589         int err;
590
591         err = mlx5_query_pfcc_reg(dev, out, sizeof(out));
592         if (err)
593                 return err;
594
595         if (pfc_en_tx)
596                 *pfc_en_tx = MLX5_GET(pfcc_reg, out, pfctx);
597
598         if (pfc_en_rx)
599                 *pfc_en_rx = MLX5_GET(pfcc_reg, out, pfcrx);
600
601         return 0;
602 }
603 EXPORT_SYMBOL_GPL(mlx5_query_port_pfc);
604
605 void mlx5_query_port_autoneg(struct mlx5_core_dev *dev, int proto_mask,
606                              u8 *an_status,
607                              u8 *an_disable_cap, u8 *an_disable_admin)
608 {
609         u32 out[MLX5_ST_SZ_DW(ptys_reg)];
610
611         *an_status = 0;
612         *an_disable_cap = 0;
613         *an_disable_admin = 0;
614
615         if (mlx5_query_port_ptys(dev, out, sizeof(out), proto_mask, 1))
616                 return;
617
618         *an_status = MLX5_GET(ptys_reg, out, an_status);
619         *an_disable_cap = MLX5_GET(ptys_reg, out, an_disable_cap);
620         *an_disable_admin = MLX5_GET(ptys_reg, out, an_disable_admin);
621 }
622 EXPORT_SYMBOL_GPL(mlx5_query_port_autoneg);
623
624 int mlx5_max_tc(struct mlx5_core_dev *mdev)
625 {
626         u8 num_tc = MLX5_CAP_GEN(mdev, max_tc) ? : 8;
627
628         return num_tc - 1;
629 }
630
631 int mlx5_query_port_dcbx_param(struct mlx5_core_dev *mdev, u32 *out)
632 {
633         u32 in[MLX5_ST_SZ_DW(dcbx_param)] = {0};
634
635         MLX5_SET(dcbx_param, in, port_number, 1);
636
637         return  mlx5_core_access_reg(mdev, in, sizeof(in), out,
638                                     sizeof(in), MLX5_REG_DCBX_PARAM, 0, 0);
639 }
640
641 int mlx5_set_port_dcbx_param(struct mlx5_core_dev *mdev, u32 *in)
642 {
643         u32 out[MLX5_ST_SZ_DW(dcbx_param)];
644
645         MLX5_SET(dcbx_param, in, port_number, 1);
646
647         return mlx5_core_access_reg(mdev, in, sizeof(out), out,
648                                     sizeof(out), MLX5_REG_DCBX_PARAM, 0, 1);
649 }
650
651 int mlx5_set_port_prio_tc(struct mlx5_core_dev *mdev, u8 *prio_tc)
652 {
653         u32 in[MLX5_ST_SZ_DW(qtct_reg)] = {0};
654         u32 out[MLX5_ST_SZ_DW(qtct_reg)];
655         int err;
656         int i;
657
658         for (i = 0; i < 8; i++) {
659                 if (prio_tc[i] > mlx5_max_tc(mdev))
660                         return -EINVAL;
661
662                 MLX5_SET(qtct_reg, in, prio, i);
663                 MLX5_SET(qtct_reg, in, tclass, prio_tc[i]);
664
665                 err = mlx5_core_access_reg(mdev, in, sizeof(in), out,
666                                            sizeof(out), MLX5_REG_QTCT, 0, 1);
667                 if (err)
668                         return err;
669         }
670
671         return 0;
672 }
673 EXPORT_SYMBOL_GPL(mlx5_set_port_prio_tc);
674
675 int mlx5_query_port_prio_tc(struct mlx5_core_dev *mdev,
676                             u8 prio, u8 *tc)
677 {
678         u32 in[MLX5_ST_SZ_DW(qtct_reg)];
679         u32 out[MLX5_ST_SZ_DW(qtct_reg)];
680         int err;
681
682         memset(in, 0, sizeof(in));
683         memset(out, 0, sizeof(out));
684
685         MLX5_SET(qtct_reg, in, port_number, 1);
686         MLX5_SET(qtct_reg, in, prio, prio);
687
688         err = mlx5_core_access_reg(mdev, in, sizeof(in), out,
689                                    sizeof(out), MLX5_REG_QTCT, 0, 0);
690         if (!err)
691                 *tc = MLX5_GET(qtct_reg, out, tclass);
692
693         return err;
694 }
695 EXPORT_SYMBOL_GPL(mlx5_query_port_prio_tc);
696
697 static int mlx5_set_port_qetcr_reg(struct mlx5_core_dev *mdev, u32 *in,
698                                    int inlen)
699 {
700         u32 out[MLX5_ST_SZ_DW(qetc_reg)];
701
702         if (!MLX5_CAP_GEN(mdev, ets))
703                 return -EOPNOTSUPP;
704
705         return mlx5_core_access_reg(mdev, in, inlen, out, sizeof(out),
706                                     MLX5_REG_QETCR, 0, 1);
707 }
708
709 static int mlx5_query_port_qetcr_reg(struct mlx5_core_dev *mdev, u32 *out,
710                                      int outlen)
711 {
712         u32 in[MLX5_ST_SZ_DW(qetc_reg)];
713
714         if (!MLX5_CAP_GEN(mdev, ets))
715                 return -EOPNOTSUPP;
716
717         memset(in, 0, sizeof(in));
718         return mlx5_core_access_reg(mdev, in, sizeof(in), out, outlen,
719                                     MLX5_REG_QETCR, 0, 0);
720 }
721
722 int mlx5_set_port_tc_group(struct mlx5_core_dev *mdev, u8 *tc_group)
723 {
724         u32 in[MLX5_ST_SZ_DW(qetc_reg)] = {0};
725         int i;
726
727         for (i = 0; i <= mlx5_max_tc(mdev); i++) {
728                 MLX5_SET(qetc_reg, in, tc_configuration[i].g, 1);
729                 MLX5_SET(qetc_reg, in, tc_configuration[i].group, tc_group[i]);
730         }
731
732         return mlx5_set_port_qetcr_reg(mdev, in, sizeof(in));
733 }
734 EXPORT_SYMBOL_GPL(mlx5_set_port_tc_group);
735
736 int mlx5_query_port_tc_group(struct mlx5_core_dev *mdev,
737                              u8 tc, u8 *tc_group)
738 {
739         u32 out[MLX5_ST_SZ_DW(qetc_reg)];
740         void *ets_tcn_conf;
741         int err;
742
743         err = mlx5_query_port_qetcr_reg(mdev, out, sizeof(out));
744         if (err)
745                 return err;
746
747         ets_tcn_conf = MLX5_ADDR_OF(qetc_reg, out,
748                                     tc_configuration[tc]);
749
750         *tc_group = MLX5_GET(ets_tcn_config_reg, ets_tcn_conf,
751                              group);
752
753         return 0;
754 }
755 EXPORT_SYMBOL_GPL(mlx5_query_port_tc_group);
756
757 int mlx5_set_port_tc_bw_alloc(struct mlx5_core_dev *mdev, u8 *tc_bw)
758 {
759         u32 in[MLX5_ST_SZ_DW(qetc_reg)] = {0};
760         int i;
761
762         for (i = 0; i <= mlx5_max_tc(mdev); i++) {
763                 MLX5_SET(qetc_reg, in, tc_configuration[i].b, 1);
764                 MLX5_SET(qetc_reg, in, tc_configuration[i].bw_allocation, tc_bw[i]);
765         }
766
767         return mlx5_set_port_qetcr_reg(mdev, in, sizeof(in));
768 }
769 EXPORT_SYMBOL_GPL(mlx5_set_port_tc_bw_alloc);
770
771 int mlx5_query_port_tc_bw_alloc(struct mlx5_core_dev *mdev,
772                                 u8 tc, u8 *bw_pct)
773 {
774         u32 out[MLX5_ST_SZ_DW(qetc_reg)];
775         void *ets_tcn_conf;
776         int err;
777
778         err = mlx5_query_port_qetcr_reg(mdev, out, sizeof(out));
779         if (err)
780                 return err;
781
782         ets_tcn_conf = MLX5_ADDR_OF(qetc_reg, out,
783                                     tc_configuration[tc]);
784
785         *bw_pct = MLX5_GET(ets_tcn_config_reg, ets_tcn_conf,
786                            bw_allocation);
787
788         return 0;
789 }
790 EXPORT_SYMBOL_GPL(mlx5_query_port_tc_bw_alloc);
791
792 int mlx5_modify_port_ets_rate_limit(struct mlx5_core_dev *mdev,
793                                     u8 *max_bw_value,
794                                     u8 *max_bw_units)
795 {
796         u32 in[MLX5_ST_SZ_DW(qetc_reg)] = {0};
797         void *ets_tcn_conf;
798         int i;
799
800         MLX5_SET(qetc_reg, in, port_number, 1);
801
802         for (i = 0; i <= mlx5_max_tc(mdev); i++) {
803                 ets_tcn_conf = MLX5_ADDR_OF(qetc_reg, in, tc_configuration[i]);
804
805                 MLX5_SET(ets_tcn_config_reg, ets_tcn_conf, r, 1);
806                 MLX5_SET(ets_tcn_config_reg, ets_tcn_conf, max_bw_units,
807                          max_bw_units[i]);
808                 MLX5_SET(ets_tcn_config_reg, ets_tcn_conf, max_bw_value,
809                          max_bw_value[i]);
810         }
811
812         return mlx5_set_port_qetcr_reg(mdev, in, sizeof(in));
813 }
814 EXPORT_SYMBOL_GPL(mlx5_modify_port_ets_rate_limit);
815
816 int mlx5_query_port_ets_rate_limit(struct mlx5_core_dev *mdev,
817                                    u8 *max_bw_value,
818                                    u8 *max_bw_units)
819 {
820         u32 out[MLX5_ST_SZ_DW(qetc_reg)];
821         void *ets_tcn_conf;
822         int err;
823         int i;
824
825         err = mlx5_query_port_qetcr_reg(mdev, out, sizeof(out));
826         if (err)
827                 return err;
828
829         for (i = 0; i <= mlx5_max_tc(mdev); i++) {
830                 ets_tcn_conf = MLX5_ADDR_OF(qetc_reg, out, tc_configuration[i]);
831
832                 max_bw_value[i] = MLX5_GET(ets_tcn_config_reg, ets_tcn_conf,
833                                            max_bw_value);
834                 max_bw_units[i] = MLX5_GET(ets_tcn_config_reg, ets_tcn_conf,
835                                            max_bw_units);
836         }
837
838         return 0;
839 }
840 EXPORT_SYMBOL_GPL(mlx5_query_port_ets_rate_limit);
841
842 int mlx5_set_port_wol(struct mlx5_core_dev *mdev, u8 wol_mode)
843 {
844         u32 in[MLX5_ST_SZ_DW(set_wol_rol_in)]   = {0};
845         u32 out[MLX5_ST_SZ_DW(set_wol_rol_out)] = {0};
846
847         MLX5_SET(set_wol_rol_in, in, opcode, MLX5_CMD_OP_SET_WOL_ROL);
848         MLX5_SET(set_wol_rol_in, in, wol_mode_valid, 1);
849         MLX5_SET(set_wol_rol_in, in, wol_mode, wol_mode);
850         return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
851 }
852 EXPORT_SYMBOL_GPL(mlx5_set_port_wol);
853
854 int mlx5_query_port_wol(struct mlx5_core_dev *mdev, u8 *wol_mode)
855 {
856         u32 in[MLX5_ST_SZ_DW(query_wol_rol_in)]   = {0};
857         u32 out[MLX5_ST_SZ_DW(query_wol_rol_out)] = {0};
858         int err;
859
860         MLX5_SET(query_wol_rol_in, in, opcode, MLX5_CMD_OP_QUERY_WOL_ROL);
861         err = mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
862         if (!err)
863                 *wol_mode = MLX5_GET(query_wol_rol_out, out, wol_mode);
864
865         return err;
866 }
867 EXPORT_SYMBOL_GPL(mlx5_query_port_wol);
868
869 static int mlx5_query_ports_check(struct mlx5_core_dev *mdev, u32 *out,
870                                   int outlen)
871 {
872         u32 in[MLX5_ST_SZ_DW(pcmr_reg)] = {0};
873
874         MLX5_SET(pcmr_reg, in, local_port, 1);
875         return mlx5_core_access_reg(mdev, in, sizeof(in), out,
876                                     outlen, MLX5_REG_PCMR, 0, 0);
877 }
878
879 static int mlx5_set_ports_check(struct mlx5_core_dev *mdev, u32 *in, int inlen)
880 {
881         u32 out[MLX5_ST_SZ_DW(pcmr_reg)];
882
883         return mlx5_core_access_reg(mdev, in, inlen, out,
884                                     sizeof(out), MLX5_REG_PCMR, 0, 1);
885 }
886
887 int mlx5_set_port_fcs(struct mlx5_core_dev *mdev, u8 enable)
888 {
889         u32 in[MLX5_ST_SZ_DW(pcmr_reg)] = {0};
890
891         MLX5_SET(pcmr_reg, in, local_port, 1);
892         MLX5_SET(pcmr_reg, in, fcs_chk, enable);
893         return mlx5_set_ports_check(mdev, in, sizeof(in));
894 }
895
896 void mlx5_query_port_fcs(struct mlx5_core_dev *mdev, bool *supported,
897                          bool *enabled)
898 {
899         u32 out[MLX5_ST_SZ_DW(pcmr_reg)];
900         /* Default values for FW which do not support MLX5_REG_PCMR */
901         *supported = false;
902         *enabled = true;
903
904         if (!MLX5_CAP_GEN(mdev, ports_check))
905                 return;
906
907         if (mlx5_query_ports_check(mdev, out, sizeof(out)))
908                 return;
909
910         *supported = !!(MLX5_GET(pcmr_reg, out, fcs_cap));
911         *enabled = !!(MLX5_GET(pcmr_reg, out, fcs_chk));
912 }
913
914 static const char *mlx5_pme_status[MLX5_MODULE_STATUS_NUM] = {
915         "Cable plugged",   /* MLX5_MODULE_STATUS_PLUGGED    = 0x1 */
916         "Cable unplugged", /* MLX5_MODULE_STATUS_UNPLUGGED  = 0x2 */
917         "Cable error",     /* MLX5_MODULE_STATUS_ERROR      = 0x3 */
918 };
919
920 static const char *mlx5_pme_error[MLX5_MODULE_EVENT_ERROR_NUM] = {
921         "Power budget exceeded",
922         "Long Range for non MLNX cable",
923         "Bus stuck(I2C or data shorted)",
924         "No EEPROM/retry timeout",
925         "Enforce part number list",
926         "Unknown identifier",
927         "High Temperature",
928         "Bad or shorted cable/module",
929         "Unknown status",
930 };
931
932 void mlx5_port_module_event(struct mlx5_core_dev *dev, struct mlx5_eqe *eqe)
933 {
934         enum port_module_event_status_type module_status;
935         enum port_module_event_error_type error_type;
936         struct mlx5_eqe_port_module *module_event_eqe;
937         struct mlx5_priv *priv = &dev->priv;
938         u8 module_num;
939
940         module_event_eqe = &eqe->data.port_module;
941         module_num = module_event_eqe->module;
942         module_status = module_event_eqe->module_status &
943                         PORT_MODULE_EVENT_MODULE_STATUS_MASK;
944         error_type = module_event_eqe->error_type &
945                      PORT_MODULE_EVENT_ERROR_TYPE_MASK;
946
947         if (module_status < MLX5_MODULE_STATUS_ERROR) {
948                 priv->pme_stats.status_counters[module_status - 1]++;
949         } else if (module_status == MLX5_MODULE_STATUS_ERROR) {
950                 if (error_type >= MLX5_MODULE_EVENT_ERROR_UNKNOWN)
951                         /* Unknown error type */
952                         error_type = MLX5_MODULE_EVENT_ERROR_UNKNOWN;
953                 priv->pme_stats.error_counters[error_type]++;
954         }
955
956         if (!printk_ratelimit())
957                 return;
958
959         if (module_status < MLX5_MODULE_STATUS_ERROR)
960                 mlx5_core_info(dev,
961                                "Port module event: module %u, %s\n",
962                                module_num, mlx5_pme_status[module_status - 1]);
963
964         else if (module_status == MLX5_MODULE_STATUS_ERROR)
965                 mlx5_core_info(dev,
966                                "Port module event[error]: module %u, %s, %s\n",
967                                module_num, mlx5_pme_status[module_status - 1],
968                                mlx5_pme_error[error_type]);
969 }
970
971 int mlx5_query_mtpps(struct mlx5_core_dev *mdev, u32 *mtpps, u32 mtpps_size)
972 {
973         u32 in[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
974
975         return mlx5_core_access_reg(mdev, in, sizeof(in), mtpps,
976                                     mtpps_size, MLX5_REG_MTPPS, 0, 0);
977 }
978
979 int mlx5_set_mtpps(struct mlx5_core_dev *mdev, u32 *mtpps, u32 mtpps_size)
980 {
981         u32 out[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
982
983         return mlx5_core_access_reg(mdev, mtpps, mtpps_size, out,
984                                     sizeof(out), MLX5_REG_MTPPS, 0, 1);
985 }
986
987 int mlx5_query_mtppse(struct mlx5_core_dev *mdev, u8 pin, u8 *arm, u8 *mode)
988 {
989         u32 out[MLX5_ST_SZ_DW(mtppse_reg)] = {0};
990         u32 in[MLX5_ST_SZ_DW(mtppse_reg)] = {0};
991         int err = 0;
992
993         MLX5_SET(mtppse_reg, in, pin, pin);
994
995         err = mlx5_core_access_reg(mdev, in, sizeof(in), out,
996                                    sizeof(out), MLX5_REG_MTPPSE, 0, 0);
997         if (err)
998                 return err;
999
1000         *arm = MLX5_GET(mtppse_reg, in, event_arm);
1001         *mode = MLX5_GET(mtppse_reg, in, event_generation_mode);
1002
1003         return err;
1004 }
1005
1006 int mlx5_set_mtppse(struct mlx5_core_dev *mdev, u8 pin, u8 arm, u8 mode)
1007 {
1008         u32 out[MLX5_ST_SZ_DW(mtppse_reg)] = {0};
1009         u32 in[MLX5_ST_SZ_DW(mtppse_reg)] = {0};
1010
1011         MLX5_SET(mtppse_reg, in, pin, pin);
1012         MLX5_SET(mtppse_reg, in, event_arm, arm);
1013         MLX5_SET(mtppse_reg, in, event_generation_mode, mode);
1014
1015         return mlx5_core_access_reg(mdev, in, sizeof(in), out,
1016                                     sizeof(out), MLX5_REG_MTPPSE, 0, 1);
1017 }
1018
1019 int mlx5_set_trust_state(struct mlx5_core_dev *mdev, u8 trust_state)
1020 {
1021         u32 out[MLX5_ST_SZ_DW(qpts_reg)] = {};
1022         u32 in[MLX5_ST_SZ_DW(qpts_reg)] = {};
1023         int err;
1024
1025         MLX5_SET(qpts_reg, in, local_port, 1);
1026         MLX5_SET(qpts_reg, in, trust_state, trust_state);
1027
1028         err = mlx5_core_access_reg(mdev, in, sizeof(in), out,
1029                                    sizeof(out), MLX5_REG_QPTS, 0, 1);
1030         return err;
1031 }
1032
1033 int mlx5_query_trust_state(struct mlx5_core_dev *mdev, u8 *trust_state)
1034 {
1035         u32 out[MLX5_ST_SZ_DW(qpts_reg)] = {};
1036         u32 in[MLX5_ST_SZ_DW(qpts_reg)] = {};
1037         int err;
1038
1039         MLX5_SET(qpts_reg, in, local_port, 1);
1040
1041         err = mlx5_core_access_reg(mdev, in, sizeof(in), out,
1042                                    sizeof(out), MLX5_REG_QPTS, 0, 0);
1043         if (!err)
1044                 *trust_state = MLX5_GET(qpts_reg, out, trust_state);
1045
1046         return err;
1047 }
1048
1049 int mlx5_set_dscp2prio(struct mlx5_core_dev *mdev, u8 dscp, u8 prio)
1050 {
1051         int sz = MLX5_ST_SZ_BYTES(qpdpm_reg);
1052         void *qpdpm_dscp;
1053         void *out;
1054         void *in;
1055         int err;
1056
1057         in = kzalloc(sz, GFP_KERNEL);
1058         out = kzalloc(sz, GFP_KERNEL);
1059         if (!in || !out) {
1060                 err = -ENOMEM;
1061                 goto out;
1062         }
1063
1064         MLX5_SET(qpdpm_reg, in, local_port, 1);
1065         err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_QPDPM, 0, 0);
1066         if (err)
1067                 goto out;
1068
1069         memcpy(in, out, sz);
1070         MLX5_SET(qpdpm_reg, in, local_port, 1);
1071
1072         /* Update the corresponding dscp entry */
1073         qpdpm_dscp = MLX5_ADDR_OF(qpdpm_reg, in, dscp[dscp]);
1074         MLX5_SET16(qpdpm_dscp_reg, qpdpm_dscp, prio, prio);
1075         MLX5_SET16(qpdpm_dscp_reg, qpdpm_dscp, e, 1);
1076         err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_QPDPM, 0, 1);
1077
1078 out:
1079         kfree(in);
1080         kfree(out);
1081         return err;
1082 }
1083
1084 /* dscp2prio[i]: priority that dscp i mapped to */
1085 #define MLX5E_SUPPORTED_DSCP 64
1086 int mlx5_query_dscp2prio(struct mlx5_core_dev *mdev, u8 *dscp2prio)
1087 {
1088         int sz = MLX5_ST_SZ_BYTES(qpdpm_reg);
1089         void *qpdpm_dscp;
1090         void *out;
1091         void *in;
1092         int err;
1093         int i;
1094
1095         in = kzalloc(sz, GFP_KERNEL);
1096         out = kzalloc(sz, GFP_KERNEL);
1097         if (!in || !out) {
1098                 err = -ENOMEM;
1099                 goto out;
1100         }
1101
1102         MLX5_SET(qpdpm_reg, in, local_port, 1);
1103         err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_QPDPM, 0, 0);
1104         if (err)
1105                 goto out;
1106
1107         for (i = 0; i < (MLX5E_SUPPORTED_DSCP); i++) {
1108                 qpdpm_dscp = MLX5_ADDR_OF(qpdpm_reg, out, dscp[i]);
1109                 dscp2prio[i] = MLX5_GET16(qpdpm_dscp_reg, qpdpm_dscp, prio);
1110         }
1111
1112 out:
1113         kfree(in);
1114         kfree(out);
1115         return err;
1116 }