1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright IBM Corp. 2018 4 */ 5 6 #define KMSG_COMPONENT "qeth" 7 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 8 9 #include <linux/ethtool.h> 10 #include "qeth_core.h" 11 12 13 #define QETH_TXQ_STAT(_name, _stat) { \ 14 .name = _name, \ 15 .offset = offsetof(struct qeth_out_q_stats, _stat) \ 16 } 17 18 #define QETH_CARD_STAT(_name, _stat) { \ 19 .name = _name, \ 20 .offset = offsetof(struct qeth_card_stats, _stat) \ 21 } 22 23 struct qeth_stats { 24 char name[ETH_GSTRING_LEN]; 25 unsigned int offset; 26 }; 27 28 static const struct qeth_stats txq_stats[] = { 29 QETH_TXQ_STAT("IO buffers", bufs), 30 QETH_TXQ_STAT("IO buffer elements", buf_elements), 31 QETH_TXQ_STAT("packed IO buffers", bufs_pack), 32 QETH_TXQ_STAT("skbs", tx_packets), 33 QETH_TXQ_STAT("packed skbs", skbs_pack), 34 QETH_TXQ_STAT("SG skbs", skbs_sg), 35 QETH_TXQ_STAT("HW csum skbs", skbs_csum), 36 QETH_TXQ_STAT("TSO skbs", skbs_tso), 37 QETH_TXQ_STAT("linearized skbs", skbs_linearized), 38 QETH_TXQ_STAT("linearized+error skbs", skbs_linearized_fail), 39 QETH_TXQ_STAT("TSO bytes", tso_bytes), 40 QETH_TXQ_STAT("Packing mode switches", packing_mode_switch), 41 QETH_TXQ_STAT("Queue stopped", stopped), 42 QETH_TXQ_STAT("Doorbell", doorbell), 43 QETH_TXQ_STAT("IRQ for frames", coal_frames), 44 QETH_TXQ_STAT("Completion yield", completion_yield), 45 QETH_TXQ_STAT("Completion timer", completion_timer), 46 }; 47 48 static const struct qeth_stats card_stats[] = { 49 QETH_CARD_STAT("rx0 IO buffers", rx_bufs), 50 QETH_CARD_STAT("rx0 HW csum skbs", rx_skb_csum), 51 QETH_CARD_STAT("rx0 SG skbs", rx_sg_skbs), 52 QETH_CARD_STAT("rx0 SG page frags", rx_sg_frags), 53 QETH_CARD_STAT("rx0 SG page allocs", rx_sg_alloc_page), 54 QETH_CARD_STAT("rx0 dropped, no memory", rx_dropped_nomem), 55 QETH_CARD_STAT("rx0 dropped, bad format", rx_dropped_notsupp), 56 QETH_CARD_STAT("rx0 dropped, runt", rx_dropped_runt), 57 }; 58 59 #define TXQ_STATS_LEN ARRAY_SIZE(txq_stats) 60 #define CARD_STATS_LEN ARRAY_SIZE(card_stats) 61 62 static void qeth_add_stat_data(u64 **dst, void *src, 63 const struct qeth_stats stats[], 64 unsigned int size) 65 { 66 unsigned int i; 67 char *stat; 68 69 for (i = 0; i < size; i++) { 70 stat = (char *)src + stats[i].offset; 71 **dst = *(u64 *)stat; 72 (*dst)++; 73 } 74 } 75 76 static void qeth_add_stat_strings(u8 **data, const char *prefix, 77 const struct qeth_stats stats[], 78 unsigned int size) 79 { 80 unsigned int i; 81 82 for (i = 0; i < size; i++) { 83 snprintf(*data, ETH_GSTRING_LEN, "%s%s", prefix, stats[i].name); 84 *data += ETH_GSTRING_LEN; 85 } 86 } 87 88 static int qeth_get_sset_count(struct net_device *dev, int stringset) 89 { 90 struct qeth_card *card = dev->ml_priv; 91 92 switch (stringset) { 93 case ETH_SS_STATS: 94 return CARD_STATS_LEN + 95 card->qdio.no_out_queues * TXQ_STATS_LEN; 96 default: 97 return -EINVAL; 98 } 99 } 100 101 static void qeth_get_ethtool_stats(struct net_device *dev, 102 struct ethtool_stats *stats, u64 *data) 103 { 104 struct qeth_card *card = dev->ml_priv; 105 unsigned int i; 106 107 qeth_add_stat_data(&data, &card->stats, card_stats, CARD_STATS_LEN); 108 for (i = 0; i < card->qdio.no_out_queues; i++) 109 qeth_add_stat_data(&data, &card->qdio.out_qs[i]->stats, 110 txq_stats, TXQ_STATS_LEN); 111 } 112 113 static void __qeth_set_coalesce(struct net_device *dev, 114 struct qeth_qdio_out_q *queue, 115 struct ethtool_coalesce *coal) 116 { 117 WRITE_ONCE(queue->coalesce_usecs, coal->tx_coalesce_usecs); 118 WRITE_ONCE(queue->max_coalesced_frames, coal->tx_max_coalesced_frames); 119 120 if (coal->tx_coalesce_usecs && 121 netif_running(dev) && 122 !qeth_out_queue_is_empty(queue)) 123 qeth_tx_arm_timer(queue, coal->tx_coalesce_usecs); 124 } 125 126 static int qeth_set_coalesce(struct net_device *dev, 127 struct ethtool_coalesce *coal) 128 { 129 struct qeth_card *card = dev->ml_priv; 130 struct qeth_qdio_out_q *queue; 131 unsigned int i; 132 133 if (!IS_IQD(card)) 134 return -EOPNOTSUPP; 135 136 if (!coal->tx_coalesce_usecs && !coal->tx_max_coalesced_frames) 137 return -EINVAL; 138 139 qeth_for_each_output_queue(card, queue, i) 140 __qeth_set_coalesce(dev, queue, coal); 141 142 return 0; 143 } 144 145 static void qeth_get_ringparam(struct net_device *dev, 146 struct ethtool_ringparam *param) 147 { 148 struct qeth_card *card = dev->ml_priv; 149 150 param->rx_max_pending = QDIO_MAX_BUFFERS_PER_Q; 151 param->rx_mini_max_pending = 0; 152 param->rx_jumbo_max_pending = 0; 153 param->tx_max_pending = QDIO_MAX_BUFFERS_PER_Q; 154 155 param->rx_pending = card->qdio.in_buf_pool.buf_count; 156 param->rx_mini_pending = 0; 157 param->rx_jumbo_pending = 0; 158 param->tx_pending = QDIO_MAX_BUFFERS_PER_Q; 159 } 160 161 static void qeth_get_strings(struct net_device *dev, u32 stringset, u8 *data) 162 { 163 struct qeth_card *card = dev->ml_priv; 164 char prefix[ETH_GSTRING_LEN] = ""; 165 unsigned int i; 166 167 switch (stringset) { 168 case ETH_SS_STATS: 169 qeth_add_stat_strings(&data, prefix, card_stats, 170 CARD_STATS_LEN); 171 for (i = 0; i < card->qdio.no_out_queues; i++) { 172 snprintf(prefix, ETH_GSTRING_LEN, "tx%u ", i); 173 qeth_add_stat_strings(&data, prefix, txq_stats, 174 TXQ_STATS_LEN); 175 } 176 break; 177 default: 178 WARN_ON(1); 179 break; 180 } 181 } 182 183 static void qeth_get_drvinfo(struct net_device *dev, 184 struct ethtool_drvinfo *info) 185 { 186 struct qeth_card *card = dev->ml_priv; 187 188 strlcpy(info->driver, IS_LAYER2(card) ? "qeth_l2" : "qeth_l3", 189 sizeof(info->driver)); 190 strlcpy(info->fw_version, card->info.mcl_level, 191 sizeof(info->fw_version)); 192 snprintf(info->bus_info, sizeof(info->bus_info), "%s/%s/%s", 193 CARD_RDEV_ID(card), CARD_WDEV_ID(card), CARD_DDEV_ID(card)); 194 } 195 196 static void qeth_get_channels(struct net_device *dev, 197 struct ethtool_channels *channels) 198 { 199 struct qeth_card *card = dev->ml_priv; 200 201 channels->max_rx = dev->num_rx_queues; 202 channels->max_tx = card->qdio.no_out_queues; 203 channels->max_other = 0; 204 channels->max_combined = 0; 205 channels->rx_count = dev->real_num_rx_queues; 206 channels->tx_count = dev->real_num_tx_queues; 207 channels->other_count = 0; 208 channels->combined_count = 0; 209 } 210 211 static int qeth_set_channels(struct net_device *dev, 212 struct ethtool_channels *channels) 213 { 214 struct qeth_priv *priv = netdev_priv(dev); 215 struct qeth_card *card = dev->ml_priv; 216 int rc; 217 218 if (channels->rx_count == 0 || channels->tx_count == 0) 219 return -EINVAL; 220 if (channels->tx_count > card->qdio.no_out_queues) 221 return -EINVAL; 222 223 /* Prio-queueing needs all TX queues: */ 224 if (qeth_uses_tx_prio_queueing(card)) 225 return -EPERM; 226 227 if (IS_IQD(card)) { 228 if (channels->tx_count < QETH_IQD_MIN_TXQ) 229 return -EINVAL; 230 231 /* Reject downgrade while running. It could push displaced 232 * ucast flows onto txq0, which is reserved for mcast. 233 */ 234 if (netif_running(dev) && 235 channels->tx_count < dev->real_num_tx_queues) 236 return -EPERM; 237 } 238 239 rc = qeth_set_real_num_tx_queues(card, channels->tx_count); 240 if (!rc) 241 priv->tx_wanted_queues = channels->tx_count; 242 243 return rc; 244 } 245 246 static int qeth_get_ts_info(struct net_device *dev, 247 struct ethtool_ts_info *info) 248 { 249 struct qeth_card *card = dev->ml_priv; 250 251 if (!IS_IQD(card)) 252 return -EOPNOTSUPP; 253 254 return ethtool_op_get_ts_info(dev, info); 255 } 256 257 static int qeth_get_tunable(struct net_device *dev, 258 const struct ethtool_tunable *tuna, void *data) 259 { 260 struct qeth_priv *priv = netdev_priv(dev); 261 262 switch (tuna->id) { 263 case ETHTOOL_RX_COPYBREAK: 264 *(u32 *)data = priv->rx_copybreak; 265 return 0; 266 default: 267 return -EOPNOTSUPP; 268 } 269 } 270 271 static int qeth_set_tunable(struct net_device *dev, 272 const struct ethtool_tunable *tuna, 273 const void *data) 274 { 275 struct qeth_priv *priv = netdev_priv(dev); 276 277 switch (tuna->id) { 278 case ETHTOOL_RX_COPYBREAK: 279 WRITE_ONCE(priv->rx_copybreak, *(u32 *)data); 280 return 0; 281 default: 282 return -EOPNOTSUPP; 283 } 284 } 285 286 static int qeth_get_per_queue_coalesce(struct net_device *dev, u32 __queue, 287 struct ethtool_coalesce *coal) 288 { 289 struct qeth_card *card = dev->ml_priv; 290 struct qeth_qdio_out_q *queue; 291 292 if (!IS_IQD(card)) 293 return -EOPNOTSUPP; 294 295 if (__queue >= card->qdio.no_out_queues) 296 return -EINVAL; 297 298 queue = card->qdio.out_qs[__queue]; 299 300 coal->tx_coalesce_usecs = queue->coalesce_usecs; 301 coal->tx_max_coalesced_frames = queue->max_coalesced_frames; 302 return 0; 303 } 304 305 static int qeth_set_per_queue_coalesce(struct net_device *dev, u32 queue, 306 struct ethtool_coalesce *coal) 307 { 308 struct qeth_card *card = dev->ml_priv; 309 310 if (!IS_IQD(card)) 311 return -EOPNOTSUPP; 312 313 if (queue >= card->qdio.no_out_queues) 314 return -EINVAL; 315 316 if (!coal->tx_coalesce_usecs && !coal->tx_max_coalesced_frames) 317 return -EINVAL; 318 319 __qeth_set_coalesce(dev, card->qdio.out_qs[queue], coal); 320 return 0; 321 } 322 323 /* Helper function to fill 'advertising' and 'supported' which are the same. */ 324 /* Autoneg and full-duplex are supported and advertised unconditionally. */ 325 /* Always advertise and support all speeds up to specified, and only one */ 326 /* specified port type. */ 327 static void qeth_set_cmd_adv_sup(struct ethtool_link_ksettings *cmd, 328 int maxspeed, int porttype) 329 { 330 ethtool_link_ksettings_zero_link_mode(cmd, supported); 331 ethtool_link_ksettings_zero_link_mode(cmd, advertising); 332 ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising); 333 334 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg); 335 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg); 336 337 switch (porttype) { 338 case PORT_TP: 339 ethtool_link_ksettings_add_link_mode(cmd, supported, TP); 340 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP); 341 break; 342 case PORT_FIBRE: 343 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE); 344 ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE); 345 break; 346 default: 347 ethtool_link_ksettings_add_link_mode(cmd, supported, TP); 348 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP); 349 WARN_ON_ONCE(1); 350 } 351 352 /* partially does fall through, to also select lower speeds */ 353 switch (maxspeed) { 354 case SPEED_25000: 355 ethtool_link_ksettings_add_link_mode(cmd, supported, 356 25000baseSR_Full); 357 ethtool_link_ksettings_add_link_mode(cmd, advertising, 358 25000baseSR_Full); 359 break; 360 case SPEED_10000: 361 ethtool_link_ksettings_add_link_mode(cmd, supported, 362 10000baseT_Full); 363 ethtool_link_ksettings_add_link_mode(cmd, advertising, 364 10000baseT_Full); 365 fallthrough; 366 case SPEED_1000: 367 ethtool_link_ksettings_add_link_mode(cmd, supported, 368 1000baseT_Full); 369 ethtool_link_ksettings_add_link_mode(cmd, advertising, 370 1000baseT_Full); 371 ethtool_link_ksettings_add_link_mode(cmd, supported, 372 1000baseT_Half); 373 ethtool_link_ksettings_add_link_mode(cmd, advertising, 374 1000baseT_Half); 375 fallthrough; 376 case SPEED_100: 377 ethtool_link_ksettings_add_link_mode(cmd, supported, 378 100baseT_Full); 379 ethtool_link_ksettings_add_link_mode(cmd, advertising, 380 100baseT_Full); 381 ethtool_link_ksettings_add_link_mode(cmd, supported, 382 100baseT_Half); 383 ethtool_link_ksettings_add_link_mode(cmd, advertising, 384 100baseT_Half); 385 fallthrough; 386 case SPEED_10: 387 ethtool_link_ksettings_add_link_mode(cmd, supported, 388 10baseT_Full); 389 ethtool_link_ksettings_add_link_mode(cmd, advertising, 390 10baseT_Full); 391 ethtool_link_ksettings_add_link_mode(cmd, supported, 392 10baseT_Half); 393 ethtool_link_ksettings_add_link_mode(cmd, advertising, 394 10baseT_Half); 395 break; 396 default: 397 ethtool_link_ksettings_add_link_mode(cmd, supported, 398 10baseT_Full); 399 ethtool_link_ksettings_add_link_mode(cmd, advertising, 400 10baseT_Full); 401 ethtool_link_ksettings_add_link_mode(cmd, supported, 402 10baseT_Half); 403 ethtool_link_ksettings_add_link_mode(cmd, advertising, 404 10baseT_Half); 405 WARN_ON_ONCE(1); 406 } 407 } 408 409 410 static int qeth_get_link_ksettings(struct net_device *netdev, 411 struct ethtool_link_ksettings *cmd) 412 { 413 struct qeth_card *card = netdev->ml_priv; 414 enum qeth_link_types link_type; 415 struct carrier_info carrier_info; 416 int rc; 417 418 if (IS_IQD(card) || IS_VM_NIC(card)) 419 link_type = QETH_LINK_TYPE_10GBIT_ETH; 420 else 421 link_type = card->info.link_type; 422 423 cmd->base.duplex = DUPLEX_FULL; 424 cmd->base.autoneg = AUTONEG_ENABLE; 425 cmd->base.phy_address = 0; 426 cmd->base.mdio_support = 0; 427 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID; 428 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID; 429 430 switch (link_type) { 431 case QETH_LINK_TYPE_FAST_ETH: 432 case QETH_LINK_TYPE_LANE_ETH100: 433 cmd->base.speed = SPEED_100; 434 cmd->base.port = PORT_TP; 435 break; 436 case QETH_LINK_TYPE_GBIT_ETH: 437 case QETH_LINK_TYPE_LANE_ETH1000: 438 cmd->base.speed = SPEED_1000; 439 cmd->base.port = PORT_FIBRE; 440 break; 441 case QETH_LINK_TYPE_10GBIT_ETH: 442 cmd->base.speed = SPEED_10000; 443 cmd->base.port = PORT_FIBRE; 444 break; 445 case QETH_LINK_TYPE_25GBIT_ETH: 446 cmd->base.speed = SPEED_25000; 447 cmd->base.port = PORT_FIBRE; 448 break; 449 default: 450 cmd->base.speed = SPEED_10; 451 cmd->base.port = PORT_TP; 452 } 453 qeth_set_cmd_adv_sup(cmd, cmd->base.speed, cmd->base.port); 454 455 /* Check if we can obtain more accurate information. */ 456 /* If QUERY_CARD_INFO command is not supported or fails, */ 457 /* just return the heuristics that was filled above. */ 458 rc = qeth_query_card_info(card, &carrier_info); 459 if (rc == -EOPNOTSUPP) /* for old hardware, return heuristic */ 460 return 0; 461 if (rc) /* report error from the hardware operation */ 462 return rc; 463 /* on success, fill in the information got from the hardware */ 464 465 netdev_dbg(netdev, 466 "card info: card_type=0x%02x, port_mode=0x%04x, port_speed=0x%08x\n", 467 carrier_info.card_type, 468 carrier_info.port_mode, 469 carrier_info.port_speed); 470 471 /* Update attributes for which we've obtained more authoritative */ 472 /* information, leave the rest the way they where filled above. */ 473 switch (carrier_info.card_type) { 474 case CARD_INFO_TYPE_1G_COPPER_A: 475 case CARD_INFO_TYPE_1G_COPPER_B: 476 cmd->base.port = PORT_TP; 477 qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port); 478 break; 479 case CARD_INFO_TYPE_1G_FIBRE_A: 480 case CARD_INFO_TYPE_1G_FIBRE_B: 481 cmd->base.port = PORT_FIBRE; 482 qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port); 483 break; 484 case CARD_INFO_TYPE_10G_FIBRE_A: 485 case CARD_INFO_TYPE_10G_FIBRE_B: 486 cmd->base.port = PORT_FIBRE; 487 qeth_set_cmd_adv_sup(cmd, SPEED_10000, cmd->base.port); 488 break; 489 } 490 491 switch (carrier_info.port_mode) { 492 case CARD_INFO_PORTM_FULLDUPLEX: 493 cmd->base.duplex = DUPLEX_FULL; 494 break; 495 case CARD_INFO_PORTM_HALFDUPLEX: 496 cmd->base.duplex = DUPLEX_HALF; 497 break; 498 } 499 500 switch (carrier_info.port_speed) { 501 case CARD_INFO_PORTS_10M: 502 cmd->base.speed = SPEED_10; 503 break; 504 case CARD_INFO_PORTS_100M: 505 cmd->base.speed = SPEED_100; 506 break; 507 case CARD_INFO_PORTS_1G: 508 cmd->base.speed = SPEED_1000; 509 break; 510 case CARD_INFO_PORTS_10G: 511 cmd->base.speed = SPEED_10000; 512 break; 513 case CARD_INFO_PORTS_25G: 514 cmd->base.speed = SPEED_25000; 515 break; 516 } 517 518 return 0; 519 } 520 521 const struct ethtool_ops qeth_ethtool_ops = { 522 .supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS | 523 ETHTOOL_COALESCE_TX_MAX_FRAMES, 524 .get_link = ethtool_op_get_link, 525 .set_coalesce = qeth_set_coalesce, 526 .get_ringparam = qeth_get_ringparam, 527 .get_strings = qeth_get_strings, 528 .get_ethtool_stats = qeth_get_ethtool_stats, 529 .get_sset_count = qeth_get_sset_count, 530 .get_drvinfo = qeth_get_drvinfo, 531 .get_channels = qeth_get_channels, 532 .set_channels = qeth_set_channels, 533 .get_ts_info = qeth_get_ts_info, 534 .get_tunable = qeth_get_tunable, 535 .set_tunable = qeth_set_tunable, 536 .get_per_queue_coalesce = qeth_get_per_queue_coalesce, 537 .set_per_queue_coalesce = qeth_set_per_queue_coalesce, 538 .get_link_ksettings = qeth_get_link_ksettings, 539 }; 540 541 const struct ethtool_ops qeth_osn_ethtool_ops = { 542 .get_strings = qeth_get_strings, 543 .get_ethtool_stats = qeth_get_ethtool_stats, 544 .get_sset_count = qeth_get_sset_count, 545 .get_drvinfo = qeth_get_drvinfo, 546 }; 547