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 }; 42 43 static const struct qeth_stats card_stats[] = { 44 QETH_CARD_STAT("rx0 IO buffers", rx_bufs), 45 QETH_CARD_STAT("rx0 HW csum skbs", rx_skb_csum), 46 QETH_CARD_STAT("rx0 SG skbs", rx_sg_skbs), 47 QETH_CARD_STAT("rx0 SG page frags", rx_sg_frags), 48 QETH_CARD_STAT("rx0 SG page allocs", rx_sg_alloc_page), 49 }; 50 51 #define TXQ_STATS_LEN ARRAY_SIZE(txq_stats) 52 #define CARD_STATS_LEN ARRAY_SIZE(card_stats) 53 54 static void qeth_add_stat_data(u64 **dst, void *src, 55 const struct qeth_stats stats[], 56 unsigned int size) 57 { 58 unsigned int i; 59 char *stat; 60 61 for (i = 0; i < size; i++) { 62 stat = (char *)src + stats[i].offset; 63 **dst = *(u64 *)stat; 64 (*dst)++; 65 } 66 } 67 68 static void qeth_add_stat_strings(u8 **data, const char *prefix, 69 const struct qeth_stats stats[], 70 unsigned int size) 71 { 72 unsigned int i; 73 74 for (i = 0; i < size; i++) { 75 snprintf(*data, ETH_GSTRING_LEN, "%s%s", prefix, stats[i].name); 76 *data += ETH_GSTRING_LEN; 77 } 78 } 79 80 static int qeth_get_sset_count(struct net_device *dev, int stringset) 81 { 82 struct qeth_card *card = dev->ml_priv; 83 84 switch (stringset) { 85 case ETH_SS_STATS: 86 return CARD_STATS_LEN + 87 card->qdio.no_out_queues * TXQ_STATS_LEN; 88 default: 89 return -EINVAL; 90 } 91 } 92 93 static void qeth_get_ethtool_stats(struct net_device *dev, 94 struct ethtool_stats *stats, u64 *data) 95 { 96 struct qeth_card *card = dev->ml_priv; 97 unsigned int i; 98 99 qeth_add_stat_data(&data, &card->stats, card_stats, CARD_STATS_LEN); 100 for (i = 0; i < card->qdio.no_out_queues; i++) 101 qeth_add_stat_data(&data, &card->qdio.out_qs[i]->stats, 102 txq_stats, TXQ_STATS_LEN); 103 } 104 105 static void qeth_get_ringparam(struct net_device *dev, 106 struct ethtool_ringparam *param) 107 { 108 struct qeth_card *card = dev->ml_priv; 109 110 param->rx_max_pending = QDIO_MAX_BUFFERS_PER_Q; 111 param->rx_mini_max_pending = 0; 112 param->rx_jumbo_max_pending = 0; 113 param->tx_max_pending = QDIO_MAX_BUFFERS_PER_Q; 114 115 param->rx_pending = card->qdio.in_buf_pool.buf_count; 116 param->rx_mini_pending = 0; 117 param->rx_jumbo_pending = 0; 118 param->tx_pending = QDIO_MAX_BUFFERS_PER_Q; 119 } 120 121 static void qeth_get_strings(struct net_device *dev, u32 stringset, u8 *data) 122 { 123 struct qeth_card *card = dev->ml_priv; 124 char prefix[ETH_GSTRING_LEN] = ""; 125 unsigned int i; 126 127 switch (stringset) { 128 case ETH_SS_STATS: 129 qeth_add_stat_strings(&data, prefix, card_stats, 130 CARD_STATS_LEN); 131 for (i = 0; i < card->qdio.no_out_queues; i++) { 132 snprintf(prefix, ETH_GSTRING_LEN, "tx%u ", i); 133 qeth_add_stat_strings(&data, prefix, txq_stats, 134 TXQ_STATS_LEN); 135 } 136 break; 137 default: 138 WARN_ON(1); 139 break; 140 } 141 } 142 143 static void qeth_get_drvinfo(struct net_device *dev, 144 struct ethtool_drvinfo *info) 145 { 146 struct qeth_card *card = dev->ml_priv; 147 148 strlcpy(info->driver, IS_LAYER2(card) ? "qeth_l2" : "qeth_l3", 149 sizeof(info->driver)); 150 strlcpy(info->version, "1.0", sizeof(info->version)); 151 strlcpy(info->fw_version, card->info.mcl_level, 152 sizeof(info->fw_version)); 153 snprintf(info->bus_info, sizeof(info->bus_info), "%s/%s/%s", 154 CARD_RDEV_ID(card), CARD_WDEV_ID(card), CARD_DDEV_ID(card)); 155 } 156 157 /* Helper function to fill 'advertising' and 'supported' which are the same. */ 158 /* Autoneg and full-duplex are supported and advertised unconditionally. */ 159 /* Always advertise and support all speeds up to specified, and only one */ 160 /* specified port type. */ 161 static void qeth_set_cmd_adv_sup(struct ethtool_link_ksettings *cmd, 162 int maxspeed, int porttype) 163 { 164 ethtool_link_ksettings_zero_link_mode(cmd, supported); 165 ethtool_link_ksettings_zero_link_mode(cmd, advertising); 166 ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising); 167 168 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg); 169 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg); 170 171 switch (porttype) { 172 case PORT_TP: 173 ethtool_link_ksettings_add_link_mode(cmd, supported, TP); 174 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP); 175 break; 176 case PORT_FIBRE: 177 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE); 178 ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE); 179 break; 180 default: 181 ethtool_link_ksettings_add_link_mode(cmd, supported, TP); 182 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP); 183 WARN_ON_ONCE(1); 184 } 185 186 /* partially does fall through, to also select lower speeds */ 187 switch (maxspeed) { 188 case SPEED_25000: 189 ethtool_link_ksettings_add_link_mode(cmd, supported, 190 25000baseSR_Full); 191 ethtool_link_ksettings_add_link_mode(cmd, advertising, 192 25000baseSR_Full); 193 break; 194 case SPEED_10000: 195 ethtool_link_ksettings_add_link_mode(cmd, supported, 196 10000baseT_Full); 197 ethtool_link_ksettings_add_link_mode(cmd, advertising, 198 10000baseT_Full); 199 /* fall through */ 200 case SPEED_1000: 201 ethtool_link_ksettings_add_link_mode(cmd, supported, 202 1000baseT_Full); 203 ethtool_link_ksettings_add_link_mode(cmd, advertising, 204 1000baseT_Full); 205 ethtool_link_ksettings_add_link_mode(cmd, supported, 206 1000baseT_Half); 207 ethtool_link_ksettings_add_link_mode(cmd, advertising, 208 1000baseT_Half); 209 /* fall through */ 210 case SPEED_100: 211 ethtool_link_ksettings_add_link_mode(cmd, supported, 212 100baseT_Full); 213 ethtool_link_ksettings_add_link_mode(cmd, advertising, 214 100baseT_Full); 215 ethtool_link_ksettings_add_link_mode(cmd, supported, 216 100baseT_Half); 217 ethtool_link_ksettings_add_link_mode(cmd, advertising, 218 100baseT_Half); 219 /* fall through */ 220 case SPEED_10: 221 ethtool_link_ksettings_add_link_mode(cmd, supported, 222 10baseT_Full); 223 ethtool_link_ksettings_add_link_mode(cmd, advertising, 224 10baseT_Full); 225 ethtool_link_ksettings_add_link_mode(cmd, supported, 226 10baseT_Half); 227 ethtool_link_ksettings_add_link_mode(cmd, advertising, 228 10baseT_Half); 229 break; 230 default: 231 ethtool_link_ksettings_add_link_mode(cmd, supported, 232 10baseT_Full); 233 ethtool_link_ksettings_add_link_mode(cmd, advertising, 234 10baseT_Full); 235 ethtool_link_ksettings_add_link_mode(cmd, supported, 236 10baseT_Half); 237 ethtool_link_ksettings_add_link_mode(cmd, advertising, 238 10baseT_Half); 239 WARN_ON_ONCE(1); 240 } 241 } 242 243 244 static int qeth_get_link_ksettings(struct net_device *netdev, 245 struct ethtool_link_ksettings *cmd) 246 { 247 struct qeth_card *card = netdev->ml_priv; 248 enum qeth_link_types link_type; 249 struct carrier_info carrier_info; 250 int rc; 251 252 if (IS_IQD(card) || IS_VM_NIC(card)) 253 link_type = QETH_LINK_TYPE_10GBIT_ETH; 254 else 255 link_type = card->info.link_type; 256 257 cmd->base.duplex = DUPLEX_FULL; 258 cmd->base.autoneg = AUTONEG_ENABLE; 259 cmd->base.phy_address = 0; 260 cmd->base.mdio_support = 0; 261 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID; 262 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID; 263 264 switch (link_type) { 265 case QETH_LINK_TYPE_FAST_ETH: 266 case QETH_LINK_TYPE_LANE_ETH100: 267 cmd->base.speed = SPEED_100; 268 cmd->base.port = PORT_TP; 269 break; 270 case QETH_LINK_TYPE_GBIT_ETH: 271 case QETH_LINK_TYPE_LANE_ETH1000: 272 cmd->base.speed = SPEED_1000; 273 cmd->base.port = PORT_FIBRE; 274 break; 275 case QETH_LINK_TYPE_10GBIT_ETH: 276 cmd->base.speed = SPEED_10000; 277 cmd->base.port = PORT_FIBRE; 278 break; 279 case QETH_LINK_TYPE_25GBIT_ETH: 280 cmd->base.speed = SPEED_25000; 281 cmd->base.port = PORT_FIBRE; 282 break; 283 default: 284 cmd->base.speed = SPEED_10; 285 cmd->base.port = PORT_TP; 286 } 287 qeth_set_cmd_adv_sup(cmd, cmd->base.speed, cmd->base.port); 288 289 /* Check if we can obtain more accurate information. */ 290 /* If QUERY_CARD_INFO command is not supported or fails, */ 291 /* just return the heuristics that was filled above. */ 292 rc = qeth_query_card_info(card, &carrier_info); 293 if (rc == -EOPNOTSUPP) /* for old hardware, return heuristic */ 294 return 0; 295 if (rc) /* report error from the hardware operation */ 296 return rc; 297 /* on success, fill in the information got from the hardware */ 298 299 netdev_dbg(netdev, 300 "card info: card_type=0x%02x, port_mode=0x%04x, port_speed=0x%08x\n", 301 carrier_info.card_type, 302 carrier_info.port_mode, 303 carrier_info.port_speed); 304 305 /* Update attributes for which we've obtained more authoritative */ 306 /* information, leave the rest the way they where filled above. */ 307 switch (carrier_info.card_type) { 308 case CARD_INFO_TYPE_1G_COPPER_A: 309 case CARD_INFO_TYPE_1G_COPPER_B: 310 cmd->base.port = PORT_TP; 311 qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port); 312 break; 313 case CARD_INFO_TYPE_1G_FIBRE_A: 314 case CARD_INFO_TYPE_1G_FIBRE_B: 315 cmd->base.port = PORT_FIBRE; 316 qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port); 317 break; 318 case CARD_INFO_TYPE_10G_FIBRE_A: 319 case CARD_INFO_TYPE_10G_FIBRE_B: 320 cmd->base.port = PORT_FIBRE; 321 qeth_set_cmd_adv_sup(cmd, SPEED_10000, cmd->base.port); 322 break; 323 } 324 325 switch (carrier_info.port_mode) { 326 case CARD_INFO_PORTM_FULLDUPLEX: 327 cmd->base.duplex = DUPLEX_FULL; 328 break; 329 case CARD_INFO_PORTM_HALFDUPLEX: 330 cmd->base.duplex = DUPLEX_HALF; 331 break; 332 } 333 334 switch (carrier_info.port_speed) { 335 case CARD_INFO_PORTS_10M: 336 cmd->base.speed = SPEED_10; 337 break; 338 case CARD_INFO_PORTS_100M: 339 cmd->base.speed = SPEED_100; 340 break; 341 case CARD_INFO_PORTS_1G: 342 cmd->base.speed = SPEED_1000; 343 break; 344 case CARD_INFO_PORTS_10G: 345 cmd->base.speed = SPEED_10000; 346 break; 347 case CARD_INFO_PORTS_25G: 348 cmd->base.speed = SPEED_25000; 349 break; 350 } 351 352 return 0; 353 } 354 355 const struct ethtool_ops qeth_ethtool_ops = { 356 .get_link = ethtool_op_get_link, 357 .get_ringparam = qeth_get_ringparam, 358 .get_strings = qeth_get_strings, 359 .get_ethtool_stats = qeth_get_ethtool_stats, 360 .get_sset_count = qeth_get_sset_count, 361 .get_drvinfo = qeth_get_drvinfo, 362 .get_link_ksettings = qeth_get_link_ksettings, 363 }; 364 365 const struct ethtool_ops qeth_osn_ethtool_ops = { 366 .get_strings = qeth_get_strings, 367 .get_ethtool_stats = qeth_get_ethtool_stats, 368 .get_sset_count = qeth_get_sset_count, 369 .get_drvinfo = qeth_get_drvinfo, 370 }; 371