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 IRQ", completion_irq), 45 QETH_TXQ_STAT("Completion yield", completion_yield), 46 QETH_TXQ_STAT("Completion timer", completion_timer), 47 }; 48 49 static const struct qeth_stats card_stats[] = { 50 QETH_CARD_STAT("rx0 IO buffers", rx_bufs), 51 QETH_CARD_STAT("rx0 HW csum skbs", rx_skb_csum), 52 QETH_CARD_STAT("rx0 SG skbs", rx_sg_skbs), 53 QETH_CARD_STAT("rx0 SG page frags", rx_sg_frags), 54 QETH_CARD_STAT("rx0 SG page allocs", rx_sg_alloc_page), 55 QETH_CARD_STAT("rx0 dropped, no memory", rx_dropped_nomem), 56 QETH_CARD_STAT("rx0 dropped, bad format", rx_dropped_notsupp), 57 QETH_CARD_STAT("rx0 dropped, runt", rx_dropped_runt), 58 }; 59 60 #define TXQ_STATS_LEN ARRAY_SIZE(txq_stats) 61 #define CARD_STATS_LEN ARRAY_SIZE(card_stats) 62 63 static void qeth_add_stat_data(u64 **dst, void *src, 64 const struct qeth_stats stats[], 65 unsigned int size) 66 { 67 unsigned int i; 68 char *stat; 69 70 for (i = 0; i < size; i++) { 71 stat = (char *)src + stats[i].offset; 72 **dst = *(u64 *)stat; 73 (*dst)++; 74 } 75 } 76 77 static void qeth_add_stat_strings(u8 **data, const char *prefix, 78 const struct qeth_stats stats[], 79 unsigned int size) 80 { 81 unsigned int i; 82 83 for (i = 0; i < size; i++) 84 ethtool_sprintf(data, "%s%s", prefix, stats[i].name); 85 } 86 87 static int qeth_get_sset_count(struct net_device *dev, int stringset) 88 { 89 struct qeth_card *card = dev->ml_priv; 90 91 switch (stringset) { 92 case ETH_SS_STATS: 93 return CARD_STATS_LEN + 94 card->qdio.no_out_queues * TXQ_STATS_LEN; 95 default: 96 return -EINVAL; 97 } 98 } 99 100 static void qeth_get_ethtool_stats(struct net_device *dev, 101 struct ethtool_stats *stats, u64 *data) 102 { 103 struct qeth_card *card = dev->ml_priv; 104 unsigned int i; 105 106 qeth_add_stat_data(&data, &card->stats, card_stats, CARD_STATS_LEN); 107 for (i = 0; i < card->qdio.no_out_queues; i++) 108 qeth_add_stat_data(&data, &card->qdio.out_qs[i]->stats, 109 txq_stats, TXQ_STATS_LEN); 110 } 111 112 static void __qeth_set_coalesce(struct net_device *dev, 113 struct qeth_qdio_out_q *queue, 114 struct ethtool_coalesce *coal) 115 { 116 WRITE_ONCE(queue->coalesce_usecs, coal->tx_coalesce_usecs); 117 WRITE_ONCE(queue->max_coalesced_frames, coal->tx_max_coalesced_frames); 118 119 if (coal->tx_coalesce_usecs && 120 netif_running(dev) && 121 !qeth_out_queue_is_empty(queue)) 122 qeth_tx_arm_timer(queue, coal->tx_coalesce_usecs); 123 } 124 125 static int qeth_set_coalesce(struct net_device *dev, 126 struct ethtool_coalesce *coal) 127 { 128 struct qeth_card *card = dev->ml_priv; 129 struct qeth_qdio_out_q *queue; 130 unsigned int i; 131 132 if (!IS_IQD(card)) 133 return -EOPNOTSUPP; 134 135 if (!coal->tx_coalesce_usecs && !coal->tx_max_coalesced_frames) 136 return -EINVAL; 137 138 qeth_for_each_output_queue(card, queue, i) 139 __qeth_set_coalesce(dev, queue, coal); 140 141 return 0; 142 } 143 144 static void qeth_get_ringparam(struct net_device *dev, 145 struct ethtool_ringparam *param) 146 { 147 struct qeth_card *card = dev->ml_priv; 148 149 param->rx_max_pending = QDIO_MAX_BUFFERS_PER_Q; 150 param->rx_mini_max_pending = 0; 151 param->rx_jumbo_max_pending = 0; 152 param->tx_max_pending = QDIO_MAX_BUFFERS_PER_Q; 153 154 param->rx_pending = card->qdio.in_buf_pool.buf_count; 155 param->rx_mini_pending = 0; 156 param->rx_jumbo_pending = 0; 157 param->tx_pending = QDIO_MAX_BUFFERS_PER_Q; 158 } 159 160 static void qeth_get_strings(struct net_device *dev, u32 stringset, u8 *data) 161 { 162 struct qeth_card *card = dev->ml_priv; 163 char prefix[ETH_GSTRING_LEN] = ""; 164 unsigned int i; 165 166 switch (stringset) { 167 case ETH_SS_STATS: 168 qeth_add_stat_strings(&data, prefix, card_stats, 169 CARD_STATS_LEN); 170 for (i = 0; i < card->qdio.no_out_queues; i++) { 171 snprintf(prefix, ETH_GSTRING_LEN, "tx%u ", i); 172 qeth_add_stat_strings(&data, prefix, txq_stats, 173 TXQ_STATS_LEN); 174 } 175 break; 176 default: 177 WARN_ON(1); 178 break; 179 } 180 } 181 182 static void qeth_get_drvinfo(struct net_device *dev, 183 struct ethtool_drvinfo *info) 184 { 185 struct qeth_card *card = dev->ml_priv; 186 187 strlcpy(info->driver, IS_LAYER2(card) ? "qeth_l2" : "qeth_l3", 188 sizeof(info->driver)); 189 strlcpy(info->fw_version, card->info.mcl_level, 190 sizeof(info->fw_version)); 191 snprintf(info->bus_info, sizeof(info->bus_info), "%s/%s/%s", 192 CARD_RDEV_ID(card), CARD_WDEV_ID(card), CARD_DDEV_ID(card)); 193 } 194 195 static void qeth_get_channels(struct net_device *dev, 196 struct ethtool_channels *channels) 197 { 198 struct qeth_card *card = dev->ml_priv; 199 200 channels->max_rx = dev->num_rx_queues; 201 channels->max_tx = card->qdio.no_out_queues; 202 channels->max_other = 0; 203 channels->max_combined = 0; 204 channels->rx_count = dev->real_num_rx_queues; 205 channels->tx_count = dev->real_num_tx_queues; 206 channels->other_count = 0; 207 channels->combined_count = 0; 208 } 209 210 static int qeth_set_channels(struct net_device *dev, 211 struct ethtool_channels *channels) 212 { 213 struct qeth_priv *priv = netdev_priv(dev); 214 struct qeth_card *card = dev->ml_priv; 215 int rc; 216 217 if (channels->rx_count == 0 || channels->tx_count == 0) 218 return -EINVAL; 219 if (channels->tx_count > card->qdio.no_out_queues) 220 return -EINVAL; 221 222 /* Prio-queueing needs all TX queues: */ 223 if (qeth_uses_tx_prio_queueing(card)) 224 return -EPERM; 225 226 if (IS_IQD(card)) { 227 if (channels->tx_count < QETH_IQD_MIN_TXQ) 228 return -EINVAL; 229 230 /* Reject downgrade while running. It could push displaced 231 * ucast flows onto txq0, which is reserved for mcast. 232 */ 233 if (netif_running(dev) && 234 channels->tx_count < dev->real_num_tx_queues) 235 return -EPERM; 236 } 237 238 rc = qeth_set_real_num_tx_queues(card, channels->tx_count); 239 if (!rc) 240 priv->tx_wanted_queues = channels->tx_count; 241 242 return rc; 243 } 244 245 static int qeth_get_ts_info(struct net_device *dev, 246 struct ethtool_ts_info *info) 247 { 248 struct qeth_card *card = dev->ml_priv; 249 250 if (!IS_IQD(card)) 251 return -EOPNOTSUPP; 252 253 return ethtool_op_get_ts_info(dev, info); 254 } 255 256 static int qeth_get_tunable(struct net_device *dev, 257 const struct ethtool_tunable *tuna, void *data) 258 { 259 struct qeth_priv *priv = netdev_priv(dev); 260 261 switch (tuna->id) { 262 case ETHTOOL_RX_COPYBREAK: 263 *(u32 *)data = priv->rx_copybreak; 264 return 0; 265 default: 266 return -EOPNOTSUPP; 267 } 268 } 269 270 static int qeth_set_tunable(struct net_device *dev, 271 const struct ethtool_tunable *tuna, 272 const void *data) 273 { 274 struct qeth_priv *priv = netdev_priv(dev); 275 276 switch (tuna->id) { 277 case ETHTOOL_RX_COPYBREAK: 278 WRITE_ONCE(priv->rx_copybreak, *(u32 *)data); 279 return 0; 280 default: 281 return -EOPNOTSUPP; 282 } 283 } 284 285 static int qeth_get_per_queue_coalesce(struct net_device *dev, u32 __queue, 286 struct ethtool_coalesce *coal) 287 { 288 struct qeth_card *card = dev->ml_priv; 289 struct qeth_qdio_out_q *queue; 290 291 if (!IS_IQD(card)) 292 return -EOPNOTSUPP; 293 294 if (__queue >= card->qdio.no_out_queues) 295 return -EINVAL; 296 297 queue = card->qdio.out_qs[__queue]; 298 299 coal->tx_coalesce_usecs = queue->coalesce_usecs; 300 coal->tx_max_coalesced_frames = queue->max_coalesced_frames; 301 return 0; 302 } 303 304 static int qeth_set_per_queue_coalesce(struct net_device *dev, u32 queue, 305 struct ethtool_coalesce *coal) 306 { 307 struct qeth_card *card = dev->ml_priv; 308 309 if (!IS_IQD(card)) 310 return -EOPNOTSUPP; 311 312 if (queue >= card->qdio.no_out_queues) 313 return -EINVAL; 314 315 if (!coal->tx_coalesce_usecs && !coal->tx_max_coalesced_frames) 316 return -EINVAL; 317 318 __qeth_set_coalesce(dev, card->qdio.out_qs[queue], coal); 319 return 0; 320 } 321 322 /* Helper function to fill 'advertising' and 'supported' which are the same. */ 323 /* Autoneg and full-duplex are supported and advertised unconditionally. */ 324 /* Always advertise and support all speeds up to specified, and only one */ 325 /* specified port type. */ 326 static void qeth_set_ethtool_link_modes(struct ethtool_link_ksettings *cmd, 327 enum qeth_link_mode link_mode) 328 { 329 ethtool_link_ksettings_zero_link_mode(cmd, supported); 330 ethtool_link_ksettings_zero_link_mode(cmd, advertising); 331 ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising); 332 333 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg); 334 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg); 335 336 switch (cmd->base.port) { 337 case PORT_TP: 338 ethtool_link_ksettings_add_link_mode(cmd, supported, TP); 339 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP); 340 341 switch (cmd->base.speed) { 342 case SPEED_10000: 343 ethtool_link_ksettings_add_link_mode(cmd, supported, 344 10000baseT_Full); 345 ethtool_link_ksettings_add_link_mode(cmd, advertising, 346 10000baseT_Full); 347 fallthrough; 348 case SPEED_1000: 349 ethtool_link_ksettings_add_link_mode(cmd, supported, 350 1000baseT_Full); 351 ethtool_link_ksettings_add_link_mode(cmd, advertising, 352 1000baseT_Full); 353 ethtool_link_ksettings_add_link_mode(cmd, supported, 354 1000baseT_Half); 355 ethtool_link_ksettings_add_link_mode(cmd, advertising, 356 1000baseT_Half); 357 fallthrough; 358 case SPEED_100: 359 ethtool_link_ksettings_add_link_mode(cmd, supported, 360 100baseT_Full); 361 ethtool_link_ksettings_add_link_mode(cmd, advertising, 362 100baseT_Full); 363 ethtool_link_ksettings_add_link_mode(cmd, supported, 364 100baseT_Half); 365 ethtool_link_ksettings_add_link_mode(cmd, advertising, 366 100baseT_Half); 367 fallthrough; 368 case SPEED_10: 369 ethtool_link_ksettings_add_link_mode(cmd, supported, 370 10baseT_Full); 371 ethtool_link_ksettings_add_link_mode(cmd, advertising, 372 10baseT_Full); 373 ethtool_link_ksettings_add_link_mode(cmd, supported, 374 10baseT_Half); 375 ethtool_link_ksettings_add_link_mode(cmd, advertising, 376 10baseT_Half); 377 break; 378 default: 379 break; 380 } 381 382 break; 383 case PORT_FIBRE: 384 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE); 385 ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE); 386 387 switch (cmd->base.speed) { 388 case SPEED_25000: 389 ethtool_link_ksettings_add_link_mode(cmd, supported, 390 25000baseSR_Full); 391 ethtool_link_ksettings_add_link_mode(cmd, advertising, 392 25000baseSR_Full); 393 break; 394 case SPEED_10000: 395 if (link_mode == QETH_LINK_MODE_FIBRE_LONG) { 396 ethtool_link_ksettings_add_link_mode(cmd, supported, 397 10000baseLR_Full); 398 ethtool_link_ksettings_add_link_mode(cmd, advertising, 399 10000baseLR_Full); 400 } else if (link_mode == QETH_LINK_MODE_FIBRE_SHORT) { 401 ethtool_link_ksettings_add_link_mode(cmd, supported, 402 10000baseSR_Full); 403 ethtool_link_ksettings_add_link_mode(cmd, advertising, 404 10000baseSR_Full); 405 } 406 break; 407 case SPEED_1000: 408 ethtool_link_ksettings_add_link_mode(cmd, supported, 409 1000baseX_Full); 410 ethtool_link_ksettings_add_link_mode(cmd, advertising, 411 1000baseX_Full); 412 break; 413 default: 414 break; 415 } 416 417 break; 418 default: 419 break; 420 } 421 } 422 423 static int qeth_get_link_ksettings(struct net_device *netdev, 424 struct ethtool_link_ksettings *cmd) 425 { 426 struct qeth_card *card = netdev->ml_priv; 427 struct qeth_link_info link_info; 428 429 cmd->base.speed = card->info.link_info.speed; 430 cmd->base.duplex = card->info.link_info.duplex; 431 cmd->base.port = card->info.link_info.port; 432 cmd->base.autoneg = AUTONEG_ENABLE; 433 cmd->base.phy_address = 0; 434 cmd->base.mdio_support = 0; 435 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID; 436 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID; 437 438 /* Check if we can obtain more accurate information. */ 439 if (!qeth_query_card_info(card, &link_info)) { 440 if (link_info.speed != SPEED_UNKNOWN) 441 cmd->base.speed = link_info.speed; 442 if (link_info.duplex != DUPLEX_UNKNOWN) 443 cmd->base.duplex = link_info.duplex; 444 if (link_info.port != PORT_OTHER) 445 cmd->base.port = link_info.port; 446 } 447 448 qeth_set_ethtool_link_modes(cmd, card->info.link_info.link_mode); 449 450 return 0; 451 } 452 453 const struct ethtool_ops qeth_ethtool_ops = { 454 .supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS | 455 ETHTOOL_COALESCE_TX_MAX_FRAMES, 456 .get_link = ethtool_op_get_link, 457 .set_coalesce = qeth_set_coalesce, 458 .get_ringparam = qeth_get_ringparam, 459 .get_strings = qeth_get_strings, 460 .get_ethtool_stats = qeth_get_ethtool_stats, 461 .get_sset_count = qeth_get_sset_count, 462 .get_drvinfo = qeth_get_drvinfo, 463 .get_channels = qeth_get_channels, 464 .set_channels = qeth_set_channels, 465 .get_ts_info = qeth_get_ts_info, 466 .get_tunable = qeth_get_tunable, 467 .set_tunable = qeth_set_tunable, 468 .get_per_queue_coalesce = qeth_get_per_queue_coalesce, 469 .set_per_queue_coalesce = qeth_set_per_queue_coalesce, 470 .get_link_ksettings = qeth_get_link_ksettings, 471 }; 472 473 const struct ethtool_ops qeth_osn_ethtool_ops = { 474 .get_strings = qeth_get_strings, 475 .get_ethtool_stats = qeth_get_ethtool_stats, 476 .get_sset_count = qeth_get_sset_count, 477 .get_drvinfo = qeth_get_drvinfo, 478 }; 479