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 /* calling with verbose false will not print error to log */ 37 int mlx5_access_reg(struct mlx5_core_dev *dev, void *data_in, int size_in, 38 void *data_out, int size_out, u16 reg_id, int arg, 39 int write, bool verbose) 40 { 41 int outlen = MLX5_ST_SZ_BYTES(access_register_out) + size_out; 42 int inlen = MLX5_ST_SZ_BYTES(access_register_in) + size_in; 43 int err = -ENOMEM; 44 u32 *out = NULL; 45 u32 *in = NULL; 46 void *data; 47 48 in = kvzalloc(inlen, GFP_KERNEL); 49 out = kvzalloc(outlen, GFP_KERNEL); 50 if (!in || !out) 51 goto out; 52 53 data = MLX5_ADDR_OF(access_register_in, in, register_data); 54 memcpy(data, data_in, size_in); 55 56 MLX5_SET(access_register_in, in, opcode, MLX5_CMD_OP_ACCESS_REG); 57 MLX5_SET(access_register_in, in, op_mod, !write); 58 MLX5_SET(access_register_in, in, argument, arg); 59 MLX5_SET(access_register_in, in, register_id, reg_id); 60 61 err = mlx5_cmd_do(dev, in, inlen, out, outlen); 62 if (verbose) 63 err = mlx5_cmd_check(dev, err, in, out); 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_access_reg); 76 77 int mlx5_core_access_reg(struct mlx5_core_dev *dev, void *data_in, 78 int size_in, void *data_out, int size_out, 79 u16 reg_id, int arg, int write) 80 { 81 return mlx5_access_reg(dev, data_in, size_in, data_out, size_out, 82 reg_id, arg, write, true); 83 } 84 EXPORT_SYMBOL_GPL(mlx5_core_access_reg); 85 86 int mlx5_query_pcam_reg(struct mlx5_core_dev *dev, u32 *pcam, u8 feature_group, 87 u8 access_reg_group) 88 { 89 u32 in[MLX5_ST_SZ_DW(pcam_reg)] = {0}; 90 int sz = MLX5_ST_SZ_BYTES(pcam_reg); 91 92 MLX5_SET(pcam_reg, in, feature_group, feature_group); 93 MLX5_SET(pcam_reg, in, access_reg_group, access_reg_group); 94 95 return mlx5_core_access_reg(dev, in, sz, pcam, sz, MLX5_REG_PCAM, 0, 0); 96 } 97 98 int mlx5_query_mcam_reg(struct mlx5_core_dev *dev, u32 *mcam, u8 feature_group, 99 u8 access_reg_group) 100 { 101 u32 in[MLX5_ST_SZ_DW(mcam_reg)] = {0}; 102 int sz = MLX5_ST_SZ_BYTES(mcam_reg); 103 104 MLX5_SET(mcam_reg, in, feature_group, feature_group); 105 MLX5_SET(mcam_reg, in, access_reg_group, access_reg_group); 106 107 return mlx5_core_access_reg(dev, in, sz, mcam, sz, MLX5_REG_MCAM, 0, 0); 108 } 109 110 int mlx5_query_qcam_reg(struct mlx5_core_dev *mdev, u32 *qcam, 111 u8 feature_group, u8 access_reg_group) 112 { 113 u32 in[MLX5_ST_SZ_DW(qcam_reg)] = {}; 114 int sz = MLX5_ST_SZ_BYTES(qcam_reg); 115 116 MLX5_SET(qcam_reg, in, feature_group, feature_group); 117 MLX5_SET(qcam_reg, in, access_reg_group, access_reg_group); 118 119 return mlx5_core_access_reg(mdev, in, sz, qcam, sz, MLX5_REG_QCAM, 0, 0); 120 } 121 122 struct mlx5_reg_pcap { 123 u8 rsvd0; 124 u8 port_num; 125 u8 rsvd1[2]; 126 __be32 caps_127_96; 127 __be32 caps_95_64; 128 __be32 caps_63_32; 129 __be32 caps_31_0; 130 }; 131 132 int mlx5_set_port_caps(struct mlx5_core_dev *dev, u8 port_num, u32 caps) 133 { 134 struct mlx5_reg_pcap in; 135 struct mlx5_reg_pcap out; 136 137 memset(&in, 0, sizeof(in)); 138 in.caps_127_96 = cpu_to_be32(caps); 139 in.port_num = port_num; 140 141 return mlx5_core_access_reg(dev, &in, sizeof(in), &out, 142 sizeof(out), MLX5_REG_PCAP, 0, 1); 143 } 144 EXPORT_SYMBOL_GPL(mlx5_set_port_caps); 145 146 int mlx5_query_port_ptys(struct mlx5_core_dev *dev, u32 *ptys, 147 int ptys_size, int proto_mask, 148 u8 local_port, u8 plane_index) 149 { 150 u32 in[MLX5_ST_SZ_DW(ptys_reg)] = {0}; 151 152 MLX5_SET(ptys_reg, in, local_port, local_port); 153 MLX5_SET(ptys_reg, in, plane_ind, plane_index); 154 MLX5_SET(ptys_reg, in, proto_mask, proto_mask); 155 return mlx5_core_access_reg(dev, in, sizeof(in), ptys, 156 ptys_size, MLX5_REG_PTYS, 0, 0); 157 } 158 EXPORT_SYMBOL_GPL(mlx5_query_port_ptys); 159 160 int mlx5_set_port_beacon(struct mlx5_core_dev *dev, u16 beacon_duration) 161 { 162 u32 in[MLX5_ST_SZ_DW(mlcr_reg)] = {0}; 163 u32 out[MLX5_ST_SZ_DW(mlcr_reg)]; 164 165 MLX5_SET(mlcr_reg, in, local_port, 1); 166 MLX5_SET(mlcr_reg, in, beacon_duration, beacon_duration); 167 return mlx5_core_access_reg(dev, in, sizeof(in), out, 168 sizeof(out), MLX5_REG_MLCR, 0, 1); 169 } 170 171 int mlx5_query_ib_port_oper(struct mlx5_core_dev *dev, u16 *link_width_oper, 172 u16 *proto_oper, u8 local_port, u8 plane_index) 173 { 174 u32 out[MLX5_ST_SZ_DW(ptys_reg)]; 175 int err; 176 177 err = mlx5_query_port_ptys(dev, out, sizeof(out), MLX5_PTYS_IB, 178 local_port, plane_index); 179 if (err) 180 return err; 181 182 *link_width_oper = MLX5_GET(ptys_reg, out, ib_link_width_oper); 183 *proto_oper = MLX5_GET(ptys_reg, out, ib_proto_oper); 184 185 return 0; 186 } 187 EXPORT_SYMBOL(mlx5_query_ib_port_oper); 188 189 /* This function should be used after setting a port register only */ 190 void mlx5_toggle_port_link(struct mlx5_core_dev *dev) 191 { 192 enum mlx5_port_status ps; 193 194 mlx5_query_port_admin_status(dev, &ps); 195 mlx5_set_port_admin_status(dev, MLX5_PORT_DOWN); 196 if (ps == MLX5_PORT_UP) 197 mlx5_set_port_admin_status(dev, MLX5_PORT_UP); 198 } 199 200 int mlx5_set_port_admin_status(struct mlx5_core_dev *dev, 201 enum mlx5_port_status status) 202 { 203 u32 in[MLX5_ST_SZ_DW(paos_reg)] = {0}; 204 u32 out[MLX5_ST_SZ_DW(paos_reg)]; 205 206 MLX5_SET(paos_reg, in, local_port, 1); 207 MLX5_SET(paos_reg, in, admin_status, status); 208 MLX5_SET(paos_reg, in, ase, 1); 209 return mlx5_core_access_reg(dev, in, sizeof(in), out, 210 sizeof(out), MLX5_REG_PAOS, 0, 1); 211 } 212 213 int mlx5_query_port_admin_status(struct mlx5_core_dev *dev, 214 enum mlx5_port_status *status) 215 { 216 u32 in[MLX5_ST_SZ_DW(paos_reg)] = {0}; 217 u32 out[MLX5_ST_SZ_DW(paos_reg)]; 218 int err; 219 220 MLX5_SET(paos_reg, in, local_port, 1); 221 err = mlx5_core_access_reg(dev, in, sizeof(in), out, 222 sizeof(out), MLX5_REG_PAOS, 0, 0); 223 if (err) 224 return err; 225 *status = MLX5_GET(paos_reg, out, admin_status); 226 return 0; 227 } 228 229 static void mlx5_query_port_mtu(struct mlx5_core_dev *dev, u16 *admin_mtu, 230 u16 *max_mtu, u16 *oper_mtu, u8 port) 231 { 232 u32 in[MLX5_ST_SZ_DW(pmtu_reg)] = {0}; 233 u32 out[MLX5_ST_SZ_DW(pmtu_reg)]; 234 235 MLX5_SET(pmtu_reg, in, local_port, port); 236 mlx5_core_access_reg(dev, in, sizeof(in), out, 237 sizeof(out), MLX5_REG_PMTU, 0, 0); 238 239 if (max_mtu) 240 *max_mtu = MLX5_GET(pmtu_reg, out, max_mtu); 241 if (oper_mtu) 242 *oper_mtu = MLX5_GET(pmtu_reg, out, oper_mtu); 243 if (admin_mtu) 244 *admin_mtu = MLX5_GET(pmtu_reg, out, admin_mtu); 245 } 246 247 int mlx5_set_port_mtu(struct mlx5_core_dev *dev, u16 mtu, u8 port) 248 { 249 u32 in[MLX5_ST_SZ_DW(pmtu_reg)] = {0}; 250 u32 out[MLX5_ST_SZ_DW(pmtu_reg)]; 251 252 MLX5_SET(pmtu_reg, in, admin_mtu, mtu); 253 MLX5_SET(pmtu_reg, in, local_port, port); 254 return mlx5_core_access_reg(dev, in, sizeof(in), out, 255 sizeof(out), MLX5_REG_PMTU, 0, 1); 256 } 257 258 void mlx5_query_port_max_mtu(struct mlx5_core_dev *dev, u16 *max_mtu, 259 u8 port) 260 { 261 mlx5_query_port_mtu(dev, NULL, max_mtu, NULL, port); 262 } 263 EXPORT_SYMBOL_GPL(mlx5_query_port_max_mtu); 264 265 void mlx5_query_port_oper_mtu(struct mlx5_core_dev *dev, u16 *oper_mtu, 266 u8 port) 267 { 268 mlx5_query_port_mtu(dev, NULL, NULL, oper_mtu, port); 269 } 270 EXPORT_SYMBOL_GPL(mlx5_query_port_oper_mtu); 271 272 int mlx5_query_module_num(struct mlx5_core_dev *dev, int *module_num) 273 { 274 u32 in[MLX5_ST_SZ_DW(pmlp_reg)] = {0}; 275 u32 out[MLX5_ST_SZ_DW(pmlp_reg)]; 276 int err; 277 278 MLX5_SET(pmlp_reg, in, local_port, 1); 279 err = mlx5_core_access_reg(dev, in, sizeof(in), out, sizeof(out), 280 MLX5_REG_PMLP, 0, 0); 281 if (err) 282 return err; 283 284 *module_num = MLX5_GET(lane_2_module_mapping, 285 MLX5_ADDR_OF(pmlp_reg, out, lane0_module_mapping), 286 module); 287 288 return 0; 289 } 290 291 static int mlx5_query_module_id(struct mlx5_core_dev *dev, int module_num, 292 u8 *module_id, u8 *status) 293 { 294 u32 in[MLX5_ST_SZ_DW(mcia_reg)] = {}; 295 u32 out[MLX5_ST_SZ_DW(mcia_reg)]; 296 int err; 297 u8 *ptr; 298 299 MLX5_SET(mcia_reg, in, i2c_device_address, MLX5_I2C_ADDR_LOW); 300 MLX5_SET(mcia_reg, in, module, module_num); 301 MLX5_SET(mcia_reg, in, device_address, 0); 302 MLX5_SET(mcia_reg, in, page_number, 0); 303 MLX5_SET(mcia_reg, in, size, 1); 304 MLX5_SET(mcia_reg, in, l, 0); 305 306 err = mlx5_core_access_reg(dev, in, sizeof(in), out, 307 sizeof(out), MLX5_REG_MCIA, 0, 0); 308 if (err) 309 return err; 310 311 if (MLX5_GET(mcia_reg, out, status)) { 312 if (status) 313 *status = MLX5_GET(mcia_reg, out, status); 314 return -EIO; 315 } 316 317 ptr = MLX5_ADDR_OF(mcia_reg, out, dword_0); 318 319 *module_id = ptr[0]; 320 321 return 0; 322 } 323 324 static int mlx5_qsfp_eeprom_page(u16 offset) 325 { 326 if (offset < MLX5_EEPROM_PAGE_LENGTH) 327 /* Addresses between 0-255 - page 00 */ 328 return 0; 329 330 /* Addresses between 256 - 639 belongs to pages 01, 02 and 03 331 * For example, offset = 400 belongs to page 02: 332 * 1 + ((400 - 256)/128) = 2 333 */ 334 return 1 + ((offset - MLX5_EEPROM_PAGE_LENGTH) / 335 MLX5_EEPROM_HIGH_PAGE_LENGTH); 336 } 337 338 static int mlx5_qsfp_eeprom_high_page_offset(int page_num) 339 { 340 if (!page_num) /* Page 0 always start from low page */ 341 return 0; 342 343 /* High page */ 344 return page_num * MLX5_EEPROM_HIGH_PAGE_LENGTH; 345 } 346 347 static void mlx5_qsfp_eeprom_params_set(u16 *i2c_addr, int *page_num, u16 *offset) 348 { 349 *i2c_addr = MLX5_I2C_ADDR_LOW; 350 *page_num = mlx5_qsfp_eeprom_page(*offset); 351 *offset -= mlx5_qsfp_eeprom_high_page_offset(*page_num); 352 } 353 354 static void mlx5_sfp_eeprom_params_set(u16 *i2c_addr, int *page_num, u16 *offset) 355 { 356 *i2c_addr = MLX5_I2C_ADDR_LOW; 357 *page_num = 0; 358 359 if (*offset < MLX5_EEPROM_PAGE_LENGTH) 360 return; 361 362 *i2c_addr = MLX5_I2C_ADDR_HIGH; 363 *offset -= MLX5_EEPROM_PAGE_LENGTH; 364 } 365 366 static int mlx5_mcia_max_bytes(struct mlx5_core_dev *dev) 367 { 368 /* mcia supports either 12 dwords or 32 dwords */ 369 return (MLX5_CAP_MCAM_FEATURE(dev, mcia_32dwords) ? 32 : 12) * sizeof(u32); 370 } 371 372 static int mlx5_query_mcia(struct mlx5_core_dev *dev, 373 struct mlx5_module_eeprom_query_params *params, 374 u8 *data, u8 *status) 375 { 376 u32 in[MLX5_ST_SZ_DW(mcia_reg)] = {}; 377 u32 out[MLX5_ST_SZ_DW(mcia_reg)]; 378 void *ptr; 379 u16 size; 380 int err; 381 382 size = min_t(int, params->size, mlx5_mcia_max_bytes(dev)); 383 384 MLX5_SET(mcia_reg, in, l, 0); 385 MLX5_SET(mcia_reg, in, size, size); 386 MLX5_SET(mcia_reg, in, module, params->module_number); 387 MLX5_SET(mcia_reg, in, device_address, params->offset); 388 MLX5_SET(mcia_reg, in, page_number, params->page); 389 MLX5_SET(mcia_reg, in, i2c_device_address, params->i2c_address); 390 391 err = mlx5_core_access_reg(dev, in, sizeof(in), out, 392 sizeof(out), MLX5_REG_MCIA, 0, 0); 393 if (err) 394 return err; 395 396 if (MLX5_GET(mcia_reg, out, status)) { 397 if (status) 398 *status = MLX5_GET(mcia_reg, out, status); 399 return -EIO; 400 } 401 402 ptr = MLX5_ADDR_OF(mcia_reg, out, dword_0); 403 memcpy(data, ptr, size); 404 405 return size; 406 } 407 408 int mlx5_query_module_eeprom(struct mlx5_core_dev *dev, 409 u16 offset, u16 size, u8 *data, u8 *status) 410 { 411 struct mlx5_module_eeprom_query_params query = {0}; 412 u8 module_id; 413 int err; 414 415 err = mlx5_query_module_num(dev, &query.module_number); 416 if (err) 417 return err; 418 419 err = mlx5_query_module_id(dev, query.module_number, &module_id, 420 status); 421 if (err) 422 return err; 423 424 switch (module_id) { 425 case MLX5_MODULE_ID_SFP: 426 mlx5_sfp_eeprom_params_set(&query.i2c_address, &query.page, &offset); 427 break; 428 case MLX5_MODULE_ID_QSFP: 429 case MLX5_MODULE_ID_QSFP_PLUS: 430 case MLX5_MODULE_ID_QSFP28: 431 mlx5_qsfp_eeprom_params_set(&query.i2c_address, &query.page, &offset); 432 break; 433 default: 434 mlx5_core_dbg(dev, "Module ID not recognized: 0x%x\n", 435 module_id); 436 return -EINVAL; 437 } 438 439 if (offset + size > MLX5_EEPROM_PAGE_LENGTH) 440 /* Cross pages read, read until offset 256 in low page */ 441 size = MLX5_EEPROM_PAGE_LENGTH - offset; 442 443 query.size = size; 444 query.offset = offset; 445 446 return mlx5_query_mcia(dev, &query, data, status); 447 } 448 449 int mlx5_query_module_eeprom_by_page(struct mlx5_core_dev *dev, 450 struct mlx5_module_eeprom_query_params *params, 451 u8 *data, u8 *status) 452 { 453 int err; 454 455 err = mlx5_query_module_num(dev, ¶ms->module_number); 456 if (err) 457 return err; 458 459 if (params->i2c_address != MLX5_I2C_ADDR_HIGH && 460 params->i2c_address != MLX5_I2C_ADDR_LOW) { 461 mlx5_core_err(dev, "I2C address not recognized: 0x%x\n", params->i2c_address); 462 return -EINVAL; 463 } 464 465 return mlx5_query_mcia(dev, params, data, status); 466 } 467 468 static int mlx5_query_port_pvlc(struct mlx5_core_dev *dev, u32 *pvlc, 469 int pvlc_size, u8 local_port) 470 { 471 u32 in[MLX5_ST_SZ_DW(pvlc_reg)] = {0}; 472 473 MLX5_SET(pvlc_reg, in, local_port, local_port); 474 return mlx5_core_access_reg(dev, in, sizeof(in), pvlc, 475 pvlc_size, MLX5_REG_PVLC, 0, 0); 476 } 477 478 int mlx5_query_port_vl_hw_cap(struct mlx5_core_dev *dev, 479 u8 *vl_hw_cap, u8 local_port) 480 { 481 u32 out[MLX5_ST_SZ_DW(pvlc_reg)]; 482 int err; 483 484 err = mlx5_query_port_pvlc(dev, out, sizeof(out), local_port); 485 if (err) 486 return err; 487 488 *vl_hw_cap = MLX5_GET(pvlc_reg, out, vl_hw_cap); 489 490 return 0; 491 } 492 EXPORT_SYMBOL_GPL(mlx5_query_port_vl_hw_cap); 493 494 static int mlx5_query_pfcc_reg(struct mlx5_core_dev *dev, u32 *out, 495 u32 out_size) 496 { 497 u32 in[MLX5_ST_SZ_DW(pfcc_reg)] = {0}; 498 499 MLX5_SET(pfcc_reg, in, local_port, 1); 500 501 return mlx5_core_access_reg(dev, in, sizeof(in), out, 502 out_size, MLX5_REG_PFCC, 0, 0); 503 } 504 505 int mlx5_set_port_pause(struct mlx5_core_dev *dev, u32 rx_pause, u32 tx_pause) 506 { 507 u32 in[MLX5_ST_SZ_DW(pfcc_reg)] = {0}; 508 u32 out[MLX5_ST_SZ_DW(pfcc_reg)]; 509 510 MLX5_SET(pfcc_reg, in, local_port, 1); 511 MLX5_SET(pfcc_reg, in, pptx, tx_pause); 512 MLX5_SET(pfcc_reg, in, pprx, rx_pause); 513 514 return mlx5_core_access_reg(dev, in, sizeof(in), out, 515 sizeof(out), MLX5_REG_PFCC, 0, 1); 516 } 517 518 int mlx5_query_port_pause(struct mlx5_core_dev *dev, 519 u32 *rx_pause, u32 *tx_pause) 520 { 521 u32 out[MLX5_ST_SZ_DW(pfcc_reg)]; 522 int err; 523 524 err = mlx5_query_pfcc_reg(dev, out, sizeof(out)); 525 if (err) 526 return err; 527 528 if (rx_pause) 529 *rx_pause = MLX5_GET(pfcc_reg, out, pprx); 530 531 if (tx_pause) 532 *tx_pause = MLX5_GET(pfcc_reg, out, pptx); 533 534 return 0; 535 } 536 537 int mlx5_set_port_stall_watermark(struct mlx5_core_dev *dev, 538 u16 stall_critical_watermark, 539 u16 stall_minor_watermark) 540 { 541 u32 in[MLX5_ST_SZ_DW(pfcc_reg)] = {0}; 542 u32 out[MLX5_ST_SZ_DW(pfcc_reg)]; 543 544 MLX5_SET(pfcc_reg, in, local_port, 1); 545 MLX5_SET(pfcc_reg, in, pptx_mask_n, 1); 546 MLX5_SET(pfcc_reg, in, pprx_mask_n, 1); 547 MLX5_SET(pfcc_reg, in, ppan_mask_n, 1); 548 MLX5_SET(pfcc_reg, in, critical_stall_mask, 1); 549 MLX5_SET(pfcc_reg, in, minor_stall_mask, 1); 550 MLX5_SET(pfcc_reg, in, device_stall_critical_watermark, 551 stall_critical_watermark); 552 MLX5_SET(pfcc_reg, in, device_stall_minor_watermark, stall_minor_watermark); 553 554 return mlx5_core_access_reg(dev, in, sizeof(in), out, 555 sizeof(out), MLX5_REG_PFCC, 0, 1); 556 } 557 558 int mlx5_query_port_stall_watermark(struct mlx5_core_dev *dev, 559 u16 *stall_critical_watermark, 560 u16 *stall_minor_watermark) 561 { 562 u32 out[MLX5_ST_SZ_DW(pfcc_reg)]; 563 int err; 564 565 err = mlx5_query_pfcc_reg(dev, out, sizeof(out)); 566 if (err) 567 return err; 568 569 if (stall_critical_watermark) 570 *stall_critical_watermark = MLX5_GET(pfcc_reg, out, 571 device_stall_critical_watermark); 572 573 if (stall_minor_watermark) 574 *stall_minor_watermark = MLX5_GET(pfcc_reg, out, 575 device_stall_minor_watermark); 576 577 return 0; 578 } 579 580 int mlx5_set_port_pfc(struct mlx5_core_dev *dev, u8 pfc_en_tx, u8 pfc_en_rx) 581 { 582 u32 in[MLX5_ST_SZ_DW(pfcc_reg)] = {0}; 583 u32 out[MLX5_ST_SZ_DW(pfcc_reg)]; 584 585 MLX5_SET(pfcc_reg, in, local_port, 1); 586 MLX5_SET(pfcc_reg, in, pfctx, pfc_en_tx); 587 MLX5_SET(pfcc_reg, in, pfcrx, pfc_en_rx); 588 MLX5_SET_TO_ONES(pfcc_reg, in, prio_mask_tx); 589 MLX5_SET_TO_ONES(pfcc_reg, in, prio_mask_rx); 590 591 return mlx5_core_access_reg(dev, in, sizeof(in), out, 592 sizeof(out), MLX5_REG_PFCC, 0, 1); 593 } 594 595 int mlx5_query_port_pfc(struct mlx5_core_dev *dev, u8 *pfc_en_tx, u8 *pfc_en_rx) 596 { 597 u32 out[MLX5_ST_SZ_DW(pfcc_reg)]; 598 int err; 599 600 err = mlx5_query_pfcc_reg(dev, out, sizeof(out)); 601 if (err) 602 return err; 603 604 if (pfc_en_tx) 605 *pfc_en_tx = MLX5_GET(pfcc_reg, out, pfctx); 606 607 if (pfc_en_rx) 608 *pfc_en_rx = MLX5_GET(pfcc_reg, out, pfcrx); 609 610 return 0; 611 } 612 613 int mlx5_max_tc(struct mlx5_core_dev *mdev) 614 { 615 u8 num_tc = MLX5_CAP_GEN(mdev, max_tc) ? : 8; 616 617 return num_tc - 1; 618 } 619 620 int mlx5_query_port_dcbx_param(struct mlx5_core_dev *mdev, u32 *out) 621 { 622 u32 in[MLX5_ST_SZ_DW(dcbx_param)] = {0}; 623 624 MLX5_SET(dcbx_param, in, port_number, 1); 625 626 return mlx5_core_access_reg(mdev, in, sizeof(in), out, 627 sizeof(in), MLX5_REG_DCBX_PARAM, 0, 0); 628 } 629 630 int mlx5_set_port_dcbx_param(struct mlx5_core_dev *mdev, u32 *in) 631 { 632 u32 out[MLX5_ST_SZ_DW(dcbx_param)]; 633 634 MLX5_SET(dcbx_param, in, port_number, 1); 635 636 return mlx5_core_access_reg(mdev, in, sizeof(out), out, 637 sizeof(out), MLX5_REG_DCBX_PARAM, 0, 1); 638 } 639 640 int mlx5_set_port_prio_tc(struct mlx5_core_dev *mdev, u8 *prio_tc) 641 { 642 u32 in[MLX5_ST_SZ_DW(qtct_reg)] = {0}; 643 u32 out[MLX5_ST_SZ_DW(qtct_reg)]; 644 int err; 645 int i; 646 647 for (i = 0; i < 8; i++) { 648 if (prio_tc[i] > mlx5_max_tc(mdev)) 649 return -EINVAL; 650 651 MLX5_SET(qtct_reg, in, prio, i); 652 MLX5_SET(qtct_reg, in, tclass, prio_tc[i]); 653 654 err = mlx5_core_access_reg(mdev, in, sizeof(in), out, 655 sizeof(out), MLX5_REG_QTCT, 0, 1); 656 if (err) 657 return err; 658 } 659 660 return 0; 661 } 662 663 int mlx5_query_port_prio_tc(struct mlx5_core_dev *mdev, 664 u8 prio, u8 *tc) 665 { 666 u32 in[MLX5_ST_SZ_DW(qtct_reg)]; 667 u32 out[MLX5_ST_SZ_DW(qtct_reg)]; 668 int err; 669 670 memset(in, 0, sizeof(in)); 671 memset(out, 0, sizeof(out)); 672 673 MLX5_SET(qtct_reg, in, port_number, 1); 674 MLX5_SET(qtct_reg, in, prio, prio); 675 676 err = mlx5_core_access_reg(mdev, in, sizeof(in), out, 677 sizeof(out), MLX5_REG_QTCT, 0, 0); 678 if (!err) 679 *tc = MLX5_GET(qtct_reg, out, tclass); 680 681 return err; 682 } 683 684 static int mlx5_set_port_qetcr_reg(struct mlx5_core_dev *mdev, u32 *in, 685 int inlen) 686 { 687 u32 out[MLX5_ST_SZ_DW(qetc_reg)]; 688 689 if (!MLX5_CAP_GEN(mdev, ets)) 690 return -EOPNOTSUPP; 691 692 return mlx5_core_access_reg(mdev, in, inlen, out, sizeof(out), 693 MLX5_REG_QETCR, 0, 1); 694 } 695 696 static int mlx5_query_port_qetcr_reg(struct mlx5_core_dev *mdev, u32 *out, 697 int outlen) 698 { 699 u32 in[MLX5_ST_SZ_DW(qetc_reg)]; 700 701 if (!MLX5_CAP_GEN(mdev, ets)) 702 return -EOPNOTSUPP; 703 704 memset(in, 0, sizeof(in)); 705 return mlx5_core_access_reg(mdev, in, sizeof(in), out, outlen, 706 MLX5_REG_QETCR, 0, 0); 707 } 708 709 int mlx5_set_port_tc_group(struct mlx5_core_dev *mdev, u8 *tc_group) 710 { 711 u32 in[MLX5_ST_SZ_DW(qetc_reg)] = {0}; 712 int i; 713 714 for (i = 0; i <= mlx5_max_tc(mdev); i++) { 715 MLX5_SET(qetc_reg, in, tc_configuration[i].g, 1); 716 MLX5_SET(qetc_reg, in, tc_configuration[i].group, tc_group[i]); 717 } 718 719 return mlx5_set_port_qetcr_reg(mdev, in, sizeof(in)); 720 } 721 722 int mlx5_query_port_tc_group(struct mlx5_core_dev *mdev, 723 u8 tc, u8 *tc_group) 724 { 725 u32 out[MLX5_ST_SZ_DW(qetc_reg)]; 726 void *ets_tcn_conf; 727 int err; 728 729 err = mlx5_query_port_qetcr_reg(mdev, out, sizeof(out)); 730 if (err) 731 return err; 732 733 ets_tcn_conf = MLX5_ADDR_OF(qetc_reg, out, 734 tc_configuration[tc]); 735 736 *tc_group = MLX5_GET(ets_tcn_config_reg, ets_tcn_conf, 737 group); 738 739 return 0; 740 } 741 742 int mlx5_set_port_tc_bw_alloc(struct mlx5_core_dev *mdev, u8 *tc_bw) 743 { 744 u32 in[MLX5_ST_SZ_DW(qetc_reg)] = {0}; 745 int i; 746 747 for (i = 0; i <= mlx5_max_tc(mdev); i++) { 748 MLX5_SET(qetc_reg, in, tc_configuration[i].b, 1); 749 MLX5_SET(qetc_reg, in, tc_configuration[i].bw_allocation, tc_bw[i]); 750 } 751 752 return mlx5_set_port_qetcr_reg(mdev, in, sizeof(in)); 753 } 754 755 int mlx5_query_port_tc_bw_alloc(struct mlx5_core_dev *mdev, 756 u8 tc, u8 *bw_pct) 757 { 758 u32 out[MLX5_ST_SZ_DW(qetc_reg)]; 759 void *ets_tcn_conf; 760 int err; 761 762 err = mlx5_query_port_qetcr_reg(mdev, out, sizeof(out)); 763 if (err) 764 return err; 765 766 ets_tcn_conf = MLX5_ADDR_OF(qetc_reg, out, 767 tc_configuration[tc]); 768 769 *bw_pct = MLX5_GET(ets_tcn_config_reg, ets_tcn_conf, 770 bw_allocation); 771 772 return 0; 773 } 774 775 int mlx5_modify_port_ets_rate_limit(struct mlx5_core_dev *mdev, 776 u16 *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 798 int mlx5_query_port_ets_rate_limit(struct mlx5_core_dev *mdev, 799 u16 *max_bw_value, 800 u8 *max_bw_units) 801 { 802 u32 out[MLX5_ST_SZ_DW(qetc_reg)]; 803 void *ets_tcn_conf; 804 int err; 805 int i; 806 807 err = mlx5_query_port_qetcr_reg(mdev, out, sizeof(out)); 808 if (err) 809 return err; 810 811 for (i = 0; i <= mlx5_max_tc(mdev); i++) { 812 ets_tcn_conf = MLX5_ADDR_OF(qetc_reg, out, tc_configuration[i]); 813 814 max_bw_value[i] = MLX5_GET(ets_tcn_config_reg, ets_tcn_conf, 815 max_bw_value); 816 max_bw_units[i] = MLX5_GET(ets_tcn_config_reg, ets_tcn_conf, 817 max_bw_units); 818 } 819 820 return 0; 821 } 822 823 int mlx5_set_port_wol(struct mlx5_core_dev *mdev, u8 wol_mode) 824 { 825 u32 in[MLX5_ST_SZ_DW(set_wol_rol_in)] = {}; 826 827 MLX5_SET(set_wol_rol_in, in, opcode, MLX5_CMD_OP_SET_WOL_ROL); 828 MLX5_SET(set_wol_rol_in, in, wol_mode_valid, 1); 829 MLX5_SET(set_wol_rol_in, in, wol_mode, wol_mode); 830 return mlx5_cmd_exec_in(mdev, set_wol_rol, in); 831 } 832 833 int mlx5_query_port_wol(struct mlx5_core_dev *mdev, u8 *wol_mode) 834 { 835 u32 out[MLX5_ST_SZ_DW(query_wol_rol_out)] = {}; 836 u32 in[MLX5_ST_SZ_DW(query_wol_rol_in)] = {}; 837 int err; 838 839 MLX5_SET(query_wol_rol_in, in, opcode, MLX5_CMD_OP_QUERY_WOL_ROL); 840 err = mlx5_cmd_exec_inout(mdev, query_wol_rol, in, out); 841 if (!err) 842 *wol_mode = MLX5_GET(query_wol_rol_out, out, wol_mode); 843 844 return err; 845 } 846 847 int mlx5_query_ports_check(struct mlx5_core_dev *mdev, u32 *out, int outlen) 848 { 849 u32 in[MLX5_ST_SZ_DW(pcmr_reg)] = {0}; 850 851 MLX5_SET(pcmr_reg, in, local_port, 1); 852 return mlx5_core_access_reg(mdev, in, sizeof(in), out, 853 outlen, MLX5_REG_PCMR, 0, 0); 854 } 855 856 int mlx5_set_ports_check(struct mlx5_core_dev *mdev, u32 *in, int inlen) 857 { 858 u32 out[MLX5_ST_SZ_DW(pcmr_reg)]; 859 860 return mlx5_core_access_reg(mdev, in, inlen, out, 861 sizeof(out), MLX5_REG_PCMR, 0, 1); 862 } 863 864 int mlx5_set_port_fcs(struct mlx5_core_dev *mdev, u8 enable) 865 { 866 u32 in[MLX5_ST_SZ_DW(pcmr_reg)] = {0}; 867 int err; 868 869 err = mlx5_query_ports_check(mdev, in, sizeof(in)); 870 if (err) 871 return err; 872 MLX5_SET(pcmr_reg, in, local_port, 1); 873 MLX5_SET(pcmr_reg, in, fcs_chk, enable); 874 return mlx5_set_ports_check(mdev, in, sizeof(in)); 875 } 876 877 void mlx5_query_port_fcs(struct mlx5_core_dev *mdev, bool *supported, 878 bool *enabled) 879 { 880 u32 out[MLX5_ST_SZ_DW(pcmr_reg)]; 881 /* Default values for FW which do not support MLX5_REG_PCMR */ 882 *supported = false; 883 *enabled = true; 884 885 if (!MLX5_CAP_GEN(mdev, ports_check)) 886 return; 887 888 if (mlx5_query_ports_check(mdev, out, sizeof(out))) 889 return; 890 891 *supported = !!(MLX5_GET(pcmr_reg, out, fcs_cap)); 892 *enabled = !!(MLX5_GET(pcmr_reg, out, fcs_chk)); 893 } 894 895 int mlx5_query_mtpps(struct mlx5_core_dev *mdev, u32 *mtpps, u32 mtpps_size) 896 { 897 u32 in[MLX5_ST_SZ_DW(mtpps_reg)] = {0}; 898 899 return mlx5_core_access_reg(mdev, in, sizeof(in), mtpps, 900 mtpps_size, MLX5_REG_MTPPS, 0, 0); 901 } 902 903 int mlx5_set_mtpps(struct mlx5_core_dev *mdev, u32 *mtpps, u32 mtpps_size) 904 { 905 u32 out[MLX5_ST_SZ_DW(mtpps_reg)] = {0}; 906 907 return mlx5_core_access_reg(mdev, mtpps, mtpps_size, out, 908 sizeof(out), MLX5_REG_MTPPS, 0, 1); 909 } 910 911 int mlx5_set_mtppse(struct mlx5_core_dev *mdev, u8 pin, u8 arm, u8 mode) 912 { 913 u32 out[MLX5_ST_SZ_DW(mtppse_reg)] = {0}; 914 u32 in[MLX5_ST_SZ_DW(mtppse_reg)] = {0}; 915 916 MLX5_SET(mtppse_reg, in, pin, pin); 917 MLX5_SET(mtppse_reg, in, event_arm, arm); 918 MLX5_SET(mtppse_reg, in, event_generation_mode, mode); 919 920 return mlx5_core_access_reg(mdev, in, sizeof(in), out, 921 sizeof(out), MLX5_REG_MTPPSE, 0, 1); 922 } 923 924 int mlx5_set_trust_state(struct mlx5_core_dev *mdev, u8 trust_state) 925 { 926 u32 out[MLX5_ST_SZ_DW(qpts_reg)] = {}; 927 u32 in[MLX5_ST_SZ_DW(qpts_reg)] = {}; 928 int err; 929 930 MLX5_SET(qpts_reg, in, local_port, 1); 931 MLX5_SET(qpts_reg, in, trust_state, trust_state); 932 933 err = mlx5_core_access_reg(mdev, in, sizeof(in), out, 934 sizeof(out), MLX5_REG_QPTS, 0, 1); 935 return err; 936 } 937 938 int mlx5_query_trust_state(struct mlx5_core_dev *mdev, u8 *trust_state) 939 { 940 u32 out[MLX5_ST_SZ_DW(qpts_reg)] = {}; 941 u32 in[MLX5_ST_SZ_DW(qpts_reg)] = {}; 942 int err; 943 944 MLX5_SET(qpts_reg, in, local_port, 1); 945 946 err = mlx5_core_access_reg(mdev, in, sizeof(in), out, 947 sizeof(out), MLX5_REG_QPTS, 0, 0); 948 if (!err) 949 *trust_state = MLX5_GET(qpts_reg, out, trust_state); 950 951 return err; 952 } 953 954 int mlx5_query_port_buffer_ownership(struct mlx5_core_dev *mdev, 955 u8 *buffer_ownership) 956 { 957 u32 out[MLX5_ST_SZ_DW(pfcc_reg)] = {}; 958 int err; 959 960 if (!MLX5_CAP_PCAM_FEATURE(mdev, buffer_ownership)) { 961 *buffer_ownership = MLX5_BUF_OWNERSHIP_UNKNOWN; 962 return 0; 963 } 964 965 err = mlx5_query_pfcc_reg(mdev, out, sizeof(out)); 966 if (err) 967 return err; 968 969 *buffer_ownership = MLX5_GET(pfcc_reg, out, buf_ownership); 970 971 return 0; 972 } 973 974 int mlx5_set_dscp2prio(struct mlx5_core_dev *mdev, u8 dscp, u8 prio) 975 { 976 int sz = MLX5_ST_SZ_BYTES(qpdpm_reg); 977 void *qpdpm_dscp; 978 void *out; 979 void *in; 980 int err; 981 982 in = kzalloc(sz, GFP_KERNEL); 983 out = kzalloc(sz, GFP_KERNEL); 984 if (!in || !out) { 985 err = -ENOMEM; 986 goto out; 987 } 988 989 MLX5_SET(qpdpm_reg, in, local_port, 1); 990 err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_QPDPM, 0, 0); 991 if (err) 992 goto out; 993 994 memcpy(in, out, sz); 995 MLX5_SET(qpdpm_reg, in, local_port, 1); 996 997 /* Update the corresponding dscp entry */ 998 qpdpm_dscp = MLX5_ADDR_OF(qpdpm_reg, in, dscp[dscp]); 999 MLX5_SET16(qpdpm_dscp_reg, qpdpm_dscp, prio, prio); 1000 MLX5_SET16(qpdpm_dscp_reg, qpdpm_dscp, e, 1); 1001 err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_QPDPM, 0, 1); 1002 1003 out: 1004 kfree(in); 1005 kfree(out); 1006 return err; 1007 } 1008 1009 /* dscp2prio[i]: priority that dscp i mapped to */ 1010 #define MLX5E_SUPPORTED_DSCP 64 1011 int mlx5_query_dscp2prio(struct mlx5_core_dev *mdev, u8 *dscp2prio) 1012 { 1013 int sz = MLX5_ST_SZ_BYTES(qpdpm_reg); 1014 void *qpdpm_dscp; 1015 void *out; 1016 void *in; 1017 int err; 1018 int i; 1019 1020 in = kzalloc(sz, GFP_KERNEL); 1021 out = kzalloc(sz, GFP_KERNEL); 1022 if (!in || !out) { 1023 err = -ENOMEM; 1024 goto out; 1025 } 1026 1027 MLX5_SET(qpdpm_reg, in, local_port, 1); 1028 err = mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_QPDPM, 0, 0); 1029 if (err) 1030 goto out; 1031 1032 for (i = 0; i < (MLX5E_SUPPORTED_DSCP); i++) { 1033 qpdpm_dscp = MLX5_ADDR_OF(qpdpm_reg, out, dscp[i]); 1034 dscp2prio[i] = MLX5_GET16(qpdpm_dscp_reg, qpdpm_dscp, prio); 1035 } 1036 1037 out: 1038 kfree(in); 1039 kfree(out); 1040 return err; 1041 } 1042 1043 /* speed in units of 1Mb */ 1044 static const struct mlx5_link_info mlx5e_link_info[MLX5E_LINK_MODES_NUMBER] = { 1045 [MLX5E_1000BASE_CX_SGMII] = {.speed = 1000, .lanes = 1}, 1046 [MLX5E_1000BASE_KX] = {.speed = 1000, .lanes = 1}, 1047 [MLX5E_10GBASE_CX4] = {.speed = 10000, .lanes = 4}, 1048 [MLX5E_10GBASE_KX4] = {.speed = 10000, .lanes = 4}, 1049 [MLX5E_10GBASE_KR] = {.speed = 10000, .lanes = 1}, 1050 [MLX5E_20GBASE_KR2] = {.speed = 20000, .lanes = 2}, 1051 [MLX5E_40GBASE_CR4] = {.speed = 40000, .lanes = 4}, 1052 [MLX5E_40GBASE_KR4] = {.speed = 40000, .lanes = 4}, 1053 [MLX5E_56GBASE_R4] = {.speed = 56000, .lanes = 4}, 1054 [MLX5E_10GBASE_CR] = {.speed = 10000, .lanes = 1}, 1055 [MLX5E_10GBASE_SR] = {.speed = 10000, .lanes = 1}, 1056 [MLX5E_10GBASE_ER] = {.speed = 10000, .lanes = 1}, 1057 [MLX5E_40GBASE_SR4] = {.speed = 40000, .lanes = 4}, 1058 [MLX5E_40GBASE_LR4] = {.speed = 40000, .lanes = 4}, 1059 [MLX5E_50GBASE_SR2] = {.speed = 50000, .lanes = 2}, 1060 [MLX5E_100GBASE_CR4] = {.speed = 100000, .lanes = 4}, 1061 [MLX5E_100GBASE_SR4] = {.speed = 100000, .lanes = 4}, 1062 [MLX5E_100GBASE_KR4] = {.speed = 100000, .lanes = 4}, 1063 [MLX5E_100GBASE_LR4] = {.speed = 100000, .lanes = 4}, 1064 [MLX5E_100BASE_TX] = {.speed = 100, .lanes = 1}, 1065 [MLX5E_1000BASE_T] = {.speed = 1000, .lanes = 1}, 1066 [MLX5E_10GBASE_T] = {.speed = 10000, .lanes = 1}, 1067 [MLX5E_25GBASE_CR] = {.speed = 25000, .lanes = 1}, 1068 [MLX5E_25GBASE_KR] = {.speed = 25000, .lanes = 1}, 1069 [MLX5E_25GBASE_SR] = {.speed = 25000, .lanes = 1}, 1070 [MLX5E_50GBASE_CR2] = {.speed = 50000, .lanes = 2}, 1071 [MLX5E_50GBASE_KR2] = {.speed = 50000, .lanes = 2}, 1072 }; 1073 1074 static const struct mlx5_link_info 1075 mlx5e_ext_link_info[MLX5E_EXT_LINK_MODES_NUMBER] = { 1076 [MLX5E_SGMII_100M] = {.speed = 100, .lanes = 1}, 1077 [MLX5E_1000BASE_X_SGMII] = {.speed = 1000, .lanes = 1}, 1078 [MLX5E_5GBASE_R] = {.speed = 5000, .lanes = 1}, 1079 [MLX5E_10GBASE_XFI_XAUI_1] = {.speed = 10000, .lanes = 1}, 1080 [MLX5E_40GBASE_XLAUI_4_XLPPI_4] = {.speed = 40000, .lanes = 4}, 1081 [MLX5E_25GAUI_1_25GBASE_CR_KR] = {.speed = 25000, .lanes = 1}, 1082 [MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2] = {.speed = 50000, .lanes = 2}, 1083 [MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR] = {.speed = 50000, .lanes = 1}, 1084 [MLX5E_CAUI_4_100GBASE_CR4_KR4] = {.speed = 100000, .lanes = 4}, 1085 [MLX5E_100GAUI_2_100GBASE_CR2_KR2] = {.speed = 100000, .lanes = 2}, 1086 [MLX5E_200GAUI_4_200GBASE_CR4_KR4] = {.speed = 200000, .lanes = 4}, 1087 [MLX5E_400GAUI_8_400GBASE_CR8] = {.speed = 400000, .lanes = 8}, 1088 [MLX5E_100GAUI_1_100GBASE_CR_KR] = {.speed = 100000, .lanes = 1}, 1089 [MLX5E_200GAUI_2_200GBASE_CR2_KR2] = {.speed = 200000, .lanes = 2}, 1090 [MLX5E_400GAUI_4_400GBASE_CR4_KR4] = {.speed = 400000, .lanes = 4}, 1091 [MLX5E_800GAUI_8_800GBASE_CR8_KR8] = {.speed = 800000, .lanes = 8}, 1092 [MLX5E_200GAUI_1_200GBASE_CR1_KR1] = {.speed = 200000, .lanes = 1}, 1093 [MLX5E_400GAUI_2_400GBASE_CR2_KR2] = {.speed = 400000, .lanes = 2}, 1094 [MLX5E_800GAUI_4_800GBASE_CR4_KR4] = {.speed = 800000, .lanes = 4}, 1095 [MLX5E_1600GAUI_8_1600GBASE_CR8_KR8] = {.speed = 1600000, .lanes = 8}, 1096 }; 1097 1098 int mlx5_port_query_eth_proto(struct mlx5_core_dev *dev, u8 port, bool ext, 1099 struct mlx5_port_eth_proto *eproto) 1100 { 1101 u32 out[MLX5_ST_SZ_DW(ptys_reg)]; 1102 int err; 1103 1104 if (!eproto) 1105 return -EINVAL; 1106 1107 err = mlx5_query_port_ptys(dev, out, sizeof(out), MLX5_PTYS_EN, port, 0); 1108 if (err) 1109 return err; 1110 1111 eproto->cap = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, 1112 eth_proto_capability); 1113 eproto->admin = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_admin); 1114 eproto->oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper); 1115 return 0; 1116 } 1117 1118 bool mlx5_ptys_ext_supported(struct mlx5_core_dev *mdev) 1119 { 1120 struct mlx5_port_eth_proto eproto; 1121 int err; 1122 1123 if (MLX5_CAP_PCAM_FEATURE(mdev, ptys_extended_ethernet)) 1124 return true; 1125 1126 err = mlx5_port_query_eth_proto(mdev, 1, true, &eproto); 1127 if (err) 1128 return false; 1129 1130 return !!eproto.cap; 1131 } 1132 1133 static void mlx5e_port_get_link_mode_info_arr(struct mlx5_core_dev *mdev, 1134 const struct mlx5_link_info **arr, 1135 u32 *size, 1136 bool force_legacy) 1137 { 1138 bool ext = force_legacy ? false : mlx5_ptys_ext_supported(mdev); 1139 1140 *size = ext ? ARRAY_SIZE(mlx5e_ext_link_info) : 1141 ARRAY_SIZE(mlx5e_link_info); 1142 *arr = ext ? mlx5e_ext_link_info : mlx5e_link_info; 1143 } 1144 1145 const struct mlx5_link_info *mlx5_port_ptys2info(struct mlx5_core_dev *mdev, 1146 u32 eth_proto_oper, 1147 bool force_legacy) 1148 { 1149 unsigned long temp = eth_proto_oper; 1150 const struct mlx5_link_info *table; 1151 u32 max_size; 1152 int i; 1153 1154 mlx5e_port_get_link_mode_info_arr(mdev, &table, &max_size, 1155 force_legacy); 1156 i = find_first_bit(&temp, max_size); 1157 1158 /* mlx5e_link_info has holes. Check speed 1159 * is not zero as indication of one. 1160 */ 1161 if (i < max_size && table[i].speed) 1162 return &table[i]; 1163 1164 return NULL; 1165 } 1166 1167 u32 mlx5_port_info2linkmodes(struct mlx5_core_dev *mdev, 1168 struct mlx5_link_info *info, 1169 bool force_legacy) 1170 { 1171 const struct mlx5_link_info *table; 1172 u32 link_modes = 0; 1173 u32 max_size; 1174 int i; 1175 1176 mlx5e_port_get_link_mode_info_arr(mdev, &table, &max_size, 1177 force_legacy); 1178 for (i = 0; i < max_size; ++i) { 1179 if (table[i].speed == info->speed) { 1180 if (!info->lanes || table[i].lanes == info->lanes) 1181 link_modes |= MLX5E_PROT_MASK(i); 1182 } 1183 } 1184 return link_modes; 1185 } 1186 1187 int mlx5_port_oper_linkspeed(struct mlx5_core_dev *mdev, u32 *speed) 1188 { 1189 const struct mlx5_link_info *table; 1190 struct mlx5_port_eth_proto eproto; 1191 u32 oper_speed = 0; 1192 u32 max_size; 1193 bool ext; 1194 int err; 1195 int i; 1196 1197 ext = mlx5_ptys_ext_supported(mdev); 1198 err = mlx5_port_query_eth_proto(mdev, 1, ext, &eproto); 1199 if (err) 1200 return err; 1201 1202 mlx5e_port_get_link_mode_info_arr(mdev, &table, &max_size, false); 1203 for (i = 0; i < max_size; ++i) 1204 if (eproto.oper & MLX5E_PROT_MASK(i)) 1205 oper_speed = max(oper_speed, table[i].speed); 1206 1207 *speed = oper_speed; 1208 return 0; 1209 } 1210 1211 int mlx5_port_max_linkspeed(struct mlx5_core_dev *mdev, u32 *speed) 1212 { 1213 const struct mlx5_link_info *table; 1214 struct mlx5_port_eth_proto eproto; 1215 u32 max_speed = 0; 1216 u32 max_size; 1217 bool ext; 1218 int err; 1219 int i; 1220 1221 ext = mlx5_ptys_ext_supported(mdev); 1222 err = mlx5_port_query_eth_proto(mdev, 1, ext, &eproto); 1223 if (err) 1224 return err; 1225 1226 mlx5e_port_get_link_mode_info_arr(mdev, &table, &max_size, false); 1227 for (i = 0; i < max_size; ++i) 1228 if (eproto.cap & MLX5E_PROT_MASK(i)) 1229 max_speed = max(max_speed, table[i].speed); 1230 1231 *speed = max_speed; 1232 return 0; 1233 } 1234 1235 int mlx5_query_mpir_reg(struct mlx5_core_dev *dev, u32 *mpir) 1236 { 1237 u32 in[MLX5_ST_SZ_DW(mpir_reg)] = {}; 1238 int sz = MLX5_ST_SZ_BYTES(mpir_reg); 1239 1240 MLX5_SET(mpir_reg, in, local_port, 1); 1241 1242 return mlx5_core_access_reg(dev, in, sz, mpir, sz, MLX5_REG_MPIR, 0, 0); 1243 } 1244