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