1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Microchip switch driver main logic 4 * 5 * Copyright (C) 2017-2019 Microchip Technology Inc. 6 */ 7 8 #include <linux/delay.h> 9 #include <linux/export.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/platform_data/microchip-ksz.h> 14 #include <linux/phy.h> 15 #include <linux/etherdevice.h> 16 #include <linux/if_bridge.h> 17 #include <linux/of_net.h> 18 #include <net/dsa.h> 19 #include <net/switchdev.h> 20 21 #include "ksz_common.h" 22 23 void ksz_update_port_member(struct ksz_device *dev, int port) 24 { 25 struct ksz_port *p; 26 int i; 27 28 for (i = 0; i < dev->port_cnt; i++) { 29 if (i == port || i == dev->cpu_port) 30 continue; 31 p = &dev->ports[i]; 32 if (!(dev->member & (1 << i))) 33 continue; 34 35 /* Port is a member of the bridge and is forwarding. */ 36 if (p->stp_state == BR_STATE_FORWARDING && 37 p->member != dev->member) 38 dev->dev_ops->cfg_port_member(dev, i, dev->member); 39 } 40 } 41 EXPORT_SYMBOL_GPL(ksz_update_port_member); 42 43 static void port_r_cnt(struct ksz_device *dev, int port) 44 { 45 struct ksz_port_mib *mib = &dev->ports[port].mib; 46 u64 *dropped; 47 48 /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */ 49 while (mib->cnt_ptr < dev->reg_mib_cnt) { 50 dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr, 51 &mib->counters[mib->cnt_ptr]); 52 ++mib->cnt_ptr; 53 } 54 55 /* last one in storage */ 56 dropped = &mib->counters[dev->mib_cnt]; 57 58 /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */ 59 while (mib->cnt_ptr < dev->mib_cnt) { 60 dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr, 61 dropped, &mib->counters[mib->cnt_ptr]); 62 ++mib->cnt_ptr; 63 } 64 mib->cnt_ptr = 0; 65 } 66 67 static void ksz_mib_read_work(struct work_struct *work) 68 { 69 struct ksz_device *dev = container_of(work, struct ksz_device, 70 mib_read.work); 71 struct ksz_port_mib *mib; 72 struct ksz_port *p; 73 int i; 74 75 for (i = 0; i < dev->mib_port_cnt; i++) { 76 if (dsa_is_unused_port(dev->ds, i)) 77 continue; 78 79 p = &dev->ports[i]; 80 mib = &p->mib; 81 mutex_lock(&mib->cnt_mutex); 82 83 /* Only read MIB counters when the port is told to do. 84 * If not, read only dropped counters when link is not up. 85 */ 86 if (!p->read) { 87 const struct dsa_port *dp = dsa_to_port(dev->ds, i); 88 89 if (!netif_carrier_ok(dp->slave)) 90 mib->cnt_ptr = dev->reg_mib_cnt; 91 } 92 port_r_cnt(dev, i); 93 p->read = false; 94 mutex_unlock(&mib->cnt_mutex); 95 } 96 97 schedule_delayed_work(&dev->mib_read, dev->mib_read_interval); 98 } 99 100 void ksz_init_mib_timer(struct ksz_device *dev) 101 { 102 int i; 103 104 INIT_DELAYED_WORK(&dev->mib_read, ksz_mib_read_work); 105 106 /* Read MIB counters every 30 seconds to avoid overflow. */ 107 dev->mib_read_interval = msecs_to_jiffies(30000); 108 109 for (i = 0; i < dev->mib_port_cnt; i++) 110 dev->dev_ops->port_init_cnt(dev, i); 111 112 /* Start the timer 2 seconds later. */ 113 schedule_delayed_work(&dev->mib_read, msecs_to_jiffies(2000)); 114 } 115 EXPORT_SYMBOL_GPL(ksz_init_mib_timer); 116 117 int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg) 118 { 119 struct ksz_device *dev = ds->priv; 120 u16 val = 0xffff; 121 122 dev->dev_ops->r_phy(dev, addr, reg, &val); 123 124 return val; 125 } 126 EXPORT_SYMBOL_GPL(ksz_phy_read16); 127 128 int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val) 129 { 130 struct ksz_device *dev = ds->priv; 131 132 dev->dev_ops->w_phy(dev, addr, reg, val); 133 134 return 0; 135 } 136 EXPORT_SYMBOL_GPL(ksz_phy_write16); 137 138 void ksz_adjust_link(struct dsa_switch *ds, int port, 139 struct phy_device *phydev) 140 { 141 struct ksz_device *dev = ds->priv; 142 struct ksz_port *p = &dev->ports[port]; 143 144 /* Read all MIB counters when the link is going down. */ 145 if (!phydev->link) { 146 p->read = true; 147 schedule_delayed_work(&dev->mib_read, 0); 148 } 149 mutex_lock(&dev->dev_mutex); 150 if (!phydev->link) 151 dev->live_ports &= ~(1 << port); 152 else 153 /* Remember which port is connected and active. */ 154 dev->live_ports |= (1 << port) & dev->on_ports; 155 mutex_unlock(&dev->dev_mutex); 156 } 157 EXPORT_SYMBOL_GPL(ksz_adjust_link); 158 159 int ksz_sset_count(struct dsa_switch *ds, int port, int sset) 160 { 161 struct ksz_device *dev = ds->priv; 162 163 if (sset != ETH_SS_STATS) 164 return 0; 165 166 return dev->mib_cnt; 167 } 168 EXPORT_SYMBOL_GPL(ksz_sset_count); 169 170 void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf) 171 { 172 const struct dsa_port *dp = dsa_to_port(ds, port); 173 struct ksz_device *dev = ds->priv; 174 struct ksz_port_mib *mib; 175 176 mib = &dev->ports[port].mib; 177 mutex_lock(&mib->cnt_mutex); 178 179 /* Only read dropped counters if no link. */ 180 if (!netif_carrier_ok(dp->slave)) 181 mib->cnt_ptr = dev->reg_mib_cnt; 182 port_r_cnt(dev, port); 183 memcpy(buf, mib->counters, dev->mib_cnt * sizeof(u64)); 184 mutex_unlock(&mib->cnt_mutex); 185 } 186 EXPORT_SYMBOL_GPL(ksz_get_ethtool_stats); 187 188 int ksz_port_bridge_join(struct dsa_switch *ds, int port, 189 struct net_device *br) 190 { 191 struct ksz_device *dev = ds->priv; 192 193 mutex_lock(&dev->dev_mutex); 194 dev->br_member |= (1 << port); 195 mutex_unlock(&dev->dev_mutex); 196 197 /* port_stp_state_set() will be called after to put the port in 198 * appropriate state so there is no need to do anything. 199 */ 200 201 return 0; 202 } 203 EXPORT_SYMBOL_GPL(ksz_port_bridge_join); 204 205 void ksz_port_bridge_leave(struct dsa_switch *ds, int port, 206 struct net_device *br) 207 { 208 struct ksz_device *dev = ds->priv; 209 210 mutex_lock(&dev->dev_mutex); 211 dev->br_member &= ~(1 << port); 212 dev->member &= ~(1 << port); 213 mutex_unlock(&dev->dev_mutex); 214 215 /* port_stp_state_set() will be called after to put the port in 216 * forwarding state so there is no need to do anything. 217 */ 218 } 219 EXPORT_SYMBOL_GPL(ksz_port_bridge_leave); 220 221 void ksz_port_fast_age(struct dsa_switch *ds, int port) 222 { 223 struct ksz_device *dev = ds->priv; 224 225 dev->dev_ops->flush_dyn_mac_table(dev, port); 226 } 227 EXPORT_SYMBOL_GPL(ksz_port_fast_age); 228 229 int ksz_port_vlan_prepare(struct dsa_switch *ds, int port, 230 const struct switchdev_obj_port_vlan *vlan) 231 { 232 /* nothing needed */ 233 234 return 0; 235 } 236 EXPORT_SYMBOL_GPL(ksz_port_vlan_prepare); 237 238 int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb, 239 void *data) 240 { 241 struct ksz_device *dev = ds->priv; 242 int ret = 0; 243 u16 i = 0; 244 u16 entries = 0; 245 u8 timestamp = 0; 246 u8 fid; 247 u8 member; 248 struct alu_struct alu; 249 250 do { 251 alu.is_static = false; 252 ret = dev->dev_ops->r_dyn_mac_table(dev, i, alu.mac, &fid, 253 &member, ×tamp, 254 &entries); 255 if (!ret && (member & BIT(port))) { 256 ret = cb(alu.mac, alu.fid, alu.is_static, data); 257 if (ret) 258 break; 259 } 260 i++; 261 } while (i < entries); 262 if (i >= entries) 263 ret = 0; 264 265 return ret; 266 } 267 EXPORT_SYMBOL_GPL(ksz_port_fdb_dump); 268 269 int ksz_port_mdb_prepare(struct dsa_switch *ds, int port, 270 const struct switchdev_obj_port_mdb *mdb) 271 { 272 /* nothing to do */ 273 return 0; 274 } 275 EXPORT_SYMBOL_GPL(ksz_port_mdb_prepare); 276 277 void ksz_port_mdb_add(struct dsa_switch *ds, int port, 278 const struct switchdev_obj_port_mdb *mdb) 279 { 280 struct ksz_device *dev = ds->priv; 281 struct alu_struct alu; 282 int index; 283 int empty = 0; 284 285 alu.port_forward = 0; 286 for (index = 0; index < dev->num_statics; index++) { 287 if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) { 288 /* Found one already in static MAC table. */ 289 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) && 290 alu.fid == mdb->vid) 291 break; 292 /* Remember the first empty entry. */ 293 } else if (!empty) { 294 empty = index + 1; 295 } 296 } 297 298 /* no available entry */ 299 if (index == dev->num_statics && !empty) 300 return; 301 302 /* add entry */ 303 if (index == dev->num_statics) { 304 index = empty - 1; 305 memset(&alu, 0, sizeof(alu)); 306 memcpy(alu.mac, mdb->addr, ETH_ALEN); 307 alu.is_static = true; 308 } 309 alu.port_forward |= BIT(port); 310 if (mdb->vid) { 311 alu.is_use_fid = true; 312 313 /* Need a way to map VID to FID. */ 314 alu.fid = mdb->vid; 315 } 316 dev->dev_ops->w_sta_mac_table(dev, index, &alu); 317 } 318 EXPORT_SYMBOL_GPL(ksz_port_mdb_add); 319 320 int ksz_port_mdb_del(struct dsa_switch *ds, int port, 321 const struct switchdev_obj_port_mdb *mdb) 322 { 323 struct ksz_device *dev = ds->priv; 324 struct alu_struct alu; 325 int index; 326 int ret = 0; 327 328 for (index = 0; index < dev->num_statics; index++) { 329 if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) { 330 /* Found one already in static MAC table. */ 331 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) && 332 alu.fid == mdb->vid) 333 break; 334 } 335 } 336 337 /* no available entry */ 338 if (index == dev->num_statics) 339 goto exit; 340 341 /* clear port */ 342 alu.port_forward &= ~BIT(port); 343 if (!alu.port_forward) 344 alu.is_static = false; 345 dev->dev_ops->w_sta_mac_table(dev, index, &alu); 346 347 exit: 348 return ret; 349 } 350 EXPORT_SYMBOL_GPL(ksz_port_mdb_del); 351 352 int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy) 353 { 354 struct ksz_device *dev = ds->priv; 355 356 if (!dsa_is_user_port(ds, port)) 357 return 0; 358 359 /* setup slave port */ 360 dev->dev_ops->port_setup(dev, port, false); 361 362 /* port_stp_state_set() will be called after to enable the port so 363 * there is no need to do anything. 364 */ 365 366 return 0; 367 } 368 EXPORT_SYMBOL_GPL(ksz_enable_port); 369 370 void ksz_disable_port(struct dsa_switch *ds, int port) 371 { 372 struct ksz_device *dev = ds->priv; 373 374 if (!dsa_is_user_port(ds, port)) 375 return; 376 377 dev->on_ports &= ~(1 << port); 378 dev->live_ports &= ~(1 << port); 379 380 /* port_stp_state_set() will be called after to disable the port so 381 * there is no need to do anything. 382 */ 383 } 384 EXPORT_SYMBOL_GPL(ksz_disable_port); 385 386 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv) 387 { 388 struct dsa_switch *ds; 389 struct ksz_device *swdev; 390 391 ds = devm_kzalloc(base, sizeof(*ds), GFP_KERNEL); 392 if (!ds) 393 return NULL; 394 395 ds->dev = base; 396 ds->num_ports = DSA_MAX_PORTS; 397 398 swdev = devm_kzalloc(base, sizeof(*swdev), GFP_KERNEL); 399 if (!swdev) 400 return NULL; 401 402 ds->priv = swdev; 403 swdev->dev = base; 404 405 swdev->ds = ds; 406 swdev->priv = priv; 407 408 return swdev; 409 } 410 EXPORT_SYMBOL(ksz_switch_alloc); 411 412 int ksz_switch_register(struct ksz_device *dev, 413 const struct ksz_dev_ops *ops) 414 { 415 phy_interface_t interface; 416 int ret; 417 418 if (dev->pdata) 419 dev->chip_id = dev->pdata->chip_id; 420 421 dev->reset_gpio = devm_gpiod_get_optional(dev->dev, "reset", 422 GPIOD_OUT_LOW); 423 if (IS_ERR(dev->reset_gpio)) 424 return PTR_ERR(dev->reset_gpio); 425 426 if (dev->reset_gpio) { 427 gpiod_set_value_cansleep(dev->reset_gpio, 1); 428 mdelay(10); 429 gpiod_set_value_cansleep(dev->reset_gpio, 0); 430 } 431 432 mutex_init(&dev->dev_mutex); 433 mutex_init(&dev->regmap_mutex); 434 mutex_init(&dev->alu_mutex); 435 mutex_init(&dev->vlan_mutex); 436 437 dev->dev_ops = ops; 438 439 if (dev->dev_ops->detect(dev)) 440 return -EINVAL; 441 442 ret = dev->dev_ops->init(dev); 443 if (ret) 444 return ret; 445 446 /* Host port interface will be self detected, or specifically set in 447 * device tree. 448 */ 449 if (dev->dev->of_node) { 450 ret = of_get_phy_mode(dev->dev->of_node, &interface); 451 if (ret == 0) 452 dev->interface = interface; 453 dev->synclko_125 = of_property_read_bool(dev->dev->of_node, 454 "microchip,synclko-125"); 455 } 456 457 ret = dsa_register_switch(dev->ds); 458 if (ret) { 459 dev->dev_ops->exit(dev); 460 return ret; 461 } 462 463 return 0; 464 } 465 EXPORT_SYMBOL(ksz_switch_register); 466 467 void ksz_switch_remove(struct ksz_device *dev) 468 { 469 /* timer started */ 470 if (dev->mib_read_interval) 471 cancel_delayed_work_sync(&dev->mib_read); 472 473 dev->dev_ops->exit(dev); 474 dsa_unregister_switch(dev->ds); 475 476 if (dev->reset_gpio) 477 gpiod_set_value_cansleep(dev->reset_gpio, 1); 478 479 } 480 EXPORT_SYMBOL(ksz_switch_remove); 481 482 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>"); 483 MODULE_DESCRIPTION("Microchip KSZ Series Switch DSA Driver"); 484 MODULE_LICENSE("GPL"); 485