| 1 | /* |
| 2 | * Marvell 88E6060 switch driver |
| 3 | * Copyright (c) 2008 Felix Fietkau <nbd@openwrt.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License v2 as published by the |
| 7 | * Free Software Foundation |
| 8 | */ |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/string.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/unistd.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/netdevice.h> |
| 18 | #include <linux/etherdevice.h> |
| 19 | #include <linux/skbuff.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/mii.h> |
| 24 | #include <linux/ethtool.h> |
| 25 | #include <linux/phy.h> |
| 26 | #include <linux/if_vlan.h> |
| 27 | |
| 28 | #include <asm/io.h> |
| 29 | #include <asm/irq.h> |
| 30 | #include <asm/uaccess.h> |
| 31 | #include "mvswitch.h" |
| 32 | |
| 33 | /* Undefine this to use trailer mode instead. |
| 34 | * I don't know if header mode works with all chips */ |
| 35 | #define HEADER_MODE 1 |
| 36 | |
| 37 | MODULE_DESCRIPTION("Marvell 88E6060 Switch driver"); |
| 38 | MODULE_AUTHOR("Felix Fietkau"); |
| 39 | MODULE_LICENSE("GPL"); |
| 40 | |
| 41 | #define MVSWITCH_MAGIC 0x88E6060 |
| 42 | |
| 43 | struct mvswitch_priv { |
| 44 | const struct net_device_ops *ndo_old; |
| 45 | struct net_device_ops ndo; |
| 46 | struct vlan_group *grp; |
| 47 | u8 vlans[16]; |
| 48 | }; |
| 49 | |
| 50 | #define to_mvsw(_phy) ((struct mvswitch_priv *) (_phy)->priv) |
| 51 | |
| 52 | static inline u16 |
| 53 | r16(struct phy_device *phydev, int addr, int reg) |
| 54 | { |
| 55 | return phydev->bus->read(phydev->bus, addr, reg); |
| 56 | } |
| 57 | |
| 58 | static inline void |
| 59 | w16(struct phy_device *phydev, int addr, int reg, u16 val) |
| 60 | { |
| 61 | phydev->bus->write(phydev->bus, addr, reg, val); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static int |
| 66 | mvswitch_mangle_tx(struct sk_buff *skb, struct net_device *dev) |
| 67 | { |
| 68 | struct mvswitch_priv *priv; |
| 69 | char *buf = NULL; |
| 70 | u16 vid; |
| 71 | |
| 72 | priv = dev->phy_ptr; |
| 73 | if (unlikely(!priv)) |
| 74 | goto error; |
| 75 | |
| 76 | if (unlikely(skb->len < 16)) |
| 77 | goto error; |
| 78 | |
| 79 | #ifdef HEADER_MODE |
| 80 | if (__vlan_hwaccel_get_tag(skb, &vid)) |
| 81 | goto error; |
| 82 | |
| 83 | if (skb_cloned(skb) || (skb->len <= 62) || (skb_headroom(skb) < MV_HEADER_SIZE)) { |
| 84 | if (pskb_expand_head(skb, MV_HEADER_SIZE, (skb->len < 62 ? 62 - skb->len : 0), GFP_ATOMIC)) |
| 85 | goto error_expand; |
| 86 | if (skb->len < 62) |
| 87 | skb->len = 62; |
| 88 | } |
| 89 | buf = skb_push(skb, MV_HEADER_SIZE); |
| 90 | #else |
| 91 | if (__vlan_get_tag(skb, &vid)) |
| 92 | goto error; |
| 93 | |
| 94 | if (unlikely((vid > 15 || !priv->vlans[vid]))) |
| 95 | goto error; |
| 96 | |
| 97 | if (skb->len <= 64) { |
| 98 | if (pskb_expand_head(skb, 0, 64 + MV_TRAILER_SIZE - skb->len, GFP_ATOMIC)) |
| 99 | goto error_expand; |
| 100 | |
| 101 | buf = skb->data + 64; |
| 102 | skb->len = 64 + MV_TRAILER_SIZE; |
| 103 | } else { |
| 104 | if (skb_cloned(skb) || unlikely(skb_tailroom(skb) < 4)) { |
| 105 | if (pskb_expand_head(skb, 0, 4, GFP_ATOMIC)) |
| 106 | goto error_expand; |
| 107 | } |
| 108 | buf = skb_put(skb, 4); |
| 109 | } |
| 110 | |
| 111 | /* move the ethernet header 4 bytes forward, overwriting the vlan tag */ |
| 112 | memmove(skb->data + 4, skb->data, 12); |
| 113 | skb->data += 4; |
| 114 | skb->len -= 4; |
| 115 | skb->mac_header += 4; |
| 116 | #endif |
| 117 | |
| 118 | if (!buf) |
| 119 | goto error; |
| 120 | |
| 121 | |
| 122 | #ifdef HEADER_MODE |
| 123 | /* prepend the tag */ |
| 124 | *((__be16 *) buf) = cpu_to_be16( |
| 125 | ((vid << MV_HEADER_VLAN_S) & MV_HEADER_VLAN_M) | |
| 126 | ((priv->vlans[vid] << MV_HEADER_PORTS_S) & MV_HEADER_PORTS_M) |
| 127 | ); |
| 128 | #else |
| 129 | /* append the tag */ |
| 130 | *((__be32 *) buf) = cpu_to_be32(( |
| 131 | (MV_TRAILER_OVERRIDE << MV_TRAILER_FLAGS_S) | |
| 132 | ((priv->vlans[vid] & MV_TRAILER_PORTS_M) << MV_TRAILER_PORTS_S) |
| 133 | )); |
| 134 | #endif |
| 135 | |
| 136 | return priv->ndo_old->ndo_start_xmit(skb, dev); |
| 137 | |
| 138 | error_expand: |
| 139 | if (net_ratelimit()) |
| 140 | printk("%s: failed to expand/update skb for the switch\n", dev->name); |
| 141 | |
| 142 | error: |
| 143 | /* any errors? drop the packet! */ |
| 144 | dev_kfree_skb_any(skb); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int |
| 149 | mvswitch_mangle_rx(struct sk_buff *skb, int napi) |
| 150 | { |
| 151 | struct mvswitch_priv *priv; |
| 152 | struct net_device *dev; |
| 153 | int vlan = -1; |
| 154 | unsigned char *buf; |
| 155 | int i; |
| 156 | |
| 157 | dev = skb->dev; |
| 158 | if (!dev) |
| 159 | goto error; |
| 160 | |
| 161 | priv = dev->phy_ptr; |
| 162 | if (!priv) |
| 163 | goto error; |
| 164 | |
| 165 | if (!priv->grp) |
| 166 | goto error; |
| 167 | |
| 168 | #ifdef HEADER_MODE |
| 169 | buf = skb->data; |
| 170 | skb_pull(skb, MV_HEADER_SIZE); |
| 171 | #else |
| 172 | buf = skb->data + skb->len - MV_TRAILER_SIZE; |
| 173 | if (buf[0] != 0x80) |
| 174 | goto error; |
| 175 | #endif |
| 176 | |
| 177 | /* look for the vlan matching the incoming port */ |
| 178 | for (i = 0; i < ARRAY_SIZE(priv->vlans); i++) { |
| 179 | if ((1 << buf[1]) & priv->vlans[i]) |
| 180 | vlan = i; |
| 181 | } |
| 182 | |
| 183 | if (vlan == -1) |
| 184 | goto error; |
| 185 | |
| 186 | skb->protocol = eth_type_trans(skb, skb->dev); |
| 187 | |
| 188 | if (napi) |
| 189 | return vlan_hwaccel_receive_skb(skb, priv->grp, vlan); |
| 190 | else |
| 191 | return vlan_hwaccel_rx(skb, priv->grp, vlan); |
| 192 | |
| 193 | error: |
| 194 | /* no vlan? eat the packet! */ |
| 195 | dev_kfree_skb_any(skb); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | static int |
| 201 | mvswitch_netif_rx(struct sk_buff *skb) |
| 202 | { |
| 203 | return mvswitch_mangle_rx(skb, 0); |
| 204 | } |
| 205 | |
| 206 | static int |
| 207 | mvswitch_netif_receive_skb(struct sk_buff *skb) |
| 208 | { |
| 209 | return mvswitch_mangle_rx(skb, 1); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | static void |
| 214 | mvswitch_vlan_rx_register(struct net_device *dev, struct vlan_group *grp) |
| 215 | { |
| 216 | struct mvswitch_priv *priv = dev->phy_ptr; |
| 217 | priv->grp = grp; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | static int |
| 222 | mvswitch_wait_mask(struct phy_device *pdev, int addr, int reg, u16 mask, u16 val) |
| 223 | { |
| 224 | int i = 100; |
| 225 | u16 r; |
| 226 | |
| 227 | do { |
| 228 | r = r16(pdev, addr, reg) & mask; |
| 229 | if (r == val) |
| 230 | return 0; |
| 231 | } while(--i > 0); |
| 232 | return -ETIMEDOUT; |
| 233 | } |
| 234 | |
| 235 | static int |
| 236 | mvswitch_config_init(struct phy_device *pdev) |
| 237 | { |
| 238 | struct mvswitch_priv *priv = to_mvsw(pdev); |
| 239 | struct net_device *dev = pdev->attached_dev; |
| 240 | u8 vlmap = 0; |
| 241 | int i; |
| 242 | |
| 243 | if (!dev) |
| 244 | return -EINVAL; |
| 245 | |
| 246 | printk("%s: Marvell 88E6060 PHY driver attached.\n", dev->name); |
| 247 | pdev->supported = ADVERTISED_100baseT_Full; |
| 248 | pdev->advertising = ADVERTISED_100baseT_Full; |
| 249 | dev->phy_ptr = priv; |
| 250 | dev->irq = PHY_POLL; |
| 251 | #ifdef HEADER_MODE |
| 252 | dev->flags |= IFF_PROMISC; |
| 253 | #endif |
| 254 | |
| 255 | /* initialize default vlans */ |
| 256 | for (i = 0; i < MV_PORTS; i++) |
| 257 | priv->vlans[(i == MV_WANPORT ? 2 : 1)] |= (1 << i); |
| 258 | |
| 259 | /* before entering reset, disable all ports */ |
| 260 | for (i = 0; i < MV_PORTS; i++) |
| 261 | w16(pdev, MV_PORTREG(CONTROL, i), 0x00); |
| 262 | |
| 263 | msleep(2); /* wait for the status change to settle in */ |
| 264 | |
| 265 | /* put the ATU in reset */ |
| 266 | w16(pdev, MV_SWITCHREG(ATU_CTRL), MV_ATUCTL_RESET); |
| 267 | |
| 268 | i = mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_CTRL), MV_ATUCTL_RESET, 0); |
| 269 | if (i < 0) { |
| 270 | printk("%s: Timeout waiting for the switch to reset.\n", dev->name); |
| 271 | return i; |
| 272 | } |
| 273 | |
| 274 | /* set the ATU flags */ |
| 275 | w16(pdev, MV_SWITCHREG(ATU_CTRL), |
| 276 | MV_ATUCTL_NO_LEARN | |
| 277 | MV_ATUCTL_ATU_1K | |
| 278 | MV_ATUCTL_AGETIME(MV_ATUCTL_AGETIME_MIN) /* minimum without disabling ageing */ |
| 279 | ); |
| 280 | |
| 281 | /* initialize the cpu port */ |
| 282 | w16(pdev, MV_PORTREG(CONTROL, MV_CPUPORT), |
| 283 | #ifdef HEADER_MODE |
| 284 | MV_PORTCTRL_HEADER | |
| 285 | #else |
| 286 | MV_PORTCTRL_RXTR | |
| 287 | MV_PORTCTRL_TXTR | |
| 288 | #endif |
| 289 | MV_PORTCTRL_ENABLED |
| 290 | ); |
| 291 | /* wait for the phy change to settle in */ |
| 292 | msleep(2); |
| 293 | for (i = 0; i < MV_PORTS; i++) { |
| 294 | u8 pvid = 0; |
| 295 | int j; |
| 296 | |
| 297 | vlmap = 0; |
| 298 | |
| 299 | /* look for the matching vlan */ |
| 300 | for (j = 0; j < ARRAY_SIZE(priv->vlans); j++) { |
| 301 | if (priv->vlans[j] & (1 << i)) { |
| 302 | vlmap = priv->vlans[j]; |
| 303 | pvid = j; |
| 304 | } |
| 305 | } |
| 306 | /* leave port unconfigured if it's not part of a vlan */ |
| 307 | if (!vlmap) |
| 308 | continue; |
| 309 | |
| 310 | /* add the cpu port to the allowed destinations list */ |
| 311 | vlmap |= (1 << MV_CPUPORT); |
| 312 | |
| 313 | /* take port out of its own vlan destination map */ |
| 314 | vlmap &= ~(1 << i); |
| 315 | |
| 316 | /* apply vlan settings */ |
| 317 | w16(pdev, MV_PORTREG(VLANMAP, i), |
| 318 | MV_PORTVLAN_PORTS(vlmap) | |
| 319 | MV_PORTVLAN_ID(i) |
| 320 | ); |
| 321 | |
| 322 | /* re-enable port */ |
| 323 | w16(pdev, MV_PORTREG(CONTROL, i), |
| 324 | MV_PORTCTRL_ENABLED |
| 325 | ); |
| 326 | } |
| 327 | |
| 328 | w16(pdev, MV_PORTREG(VLANMAP, MV_CPUPORT), |
| 329 | MV_PORTVLAN_ID(MV_CPUPORT) |
| 330 | ); |
| 331 | |
| 332 | /* set the port association vector */ |
| 333 | for (i = 0; i <= MV_PORTS; i++) { |
| 334 | w16(pdev, MV_PORTREG(ASSOC, i), |
| 335 | MV_PORTASSOC_PORTS(1 << i) |
| 336 | ); |
| 337 | } |
| 338 | |
| 339 | /* init switch control */ |
| 340 | w16(pdev, MV_SWITCHREG(CTRL), |
| 341 | MV_SWITCHCTL_MSIZE | |
| 342 | MV_SWITCHCTL_DROP |
| 343 | ); |
| 344 | |
| 345 | /* hook into the tx function */ |
| 346 | priv->ndo_old = dev->netdev_ops; |
| 347 | memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops)); |
| 348 | priv->ndo.ndo_start_xmit = mvswitch_mangle_tx; |
| 349 | priv->ndo.ndo_vlan_rx_register = mvswitch_vlan_rx_register; |
| 350 | dev->netdev_ops = &priv->ndo; |
| 351 | |
| 352 | pdev->pkt_align = 2; |
| 353 | pdev->netif_receive_skb = mvswitch_netif_receive_skb; |
| 354 | pdev->netif_rx = mvswitch_netif_rx; |
| 355 | #ifdef HEADER_MODE |
| 356 | dev->features |= NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX; |
| 357 | #else |
| 358 | dev->features |= NETIF_F_HW_VLAN_RX; |
| 359 | #endif |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int |
| 365 | mvswitch_read_status(struct phy_device *pdev) |
| 366 | { |
| 367 | pdev->speed = SPEED_100; |
| 368 | pdev->duplex = DUPLEX_FULL; |
| 369 | pdev->link = 1; |
| 370 | |
| 371 | /* XXX ugly workaround: we can't force the switch |
| 372 | * to gracefully handle hosts moving from one port to another, |
| 373 | * so we have to regularly clear the ATU database */ |
| 374 | |
| 375 | /* wait for the ATU to become available */ |
| 376 | mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_OP), MV_ATUOP_INPROGRESS, 0); |
| 377 | |
| 378 | /* flush the ATU */ |
| 379 | w16(pdev, MV_SWITCHREG(ATU_OP), |
| 380 | MV_ATUOP_INPROGRESS | |
| 381 | MV_ATUOP_FLUSH_ALL |
| 382 | ); |
| 383 | |
| 384 | /* wait for operation to complete */ |
| 385 | mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_OP), MV_ATUOP_INPROGRESS, 0); |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int |
| 391 | mvswitch_config_aneg(struct phy_device *phydev) |
| 392 | { |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static void |
| 397 | mvswitch_remove(struct phy_device *pdev) |
| 398 | { |
| 399 | struct mvswitch_priv *priv = to_mvsw(pdev); |
| 400 | struct net_device *dev = pdev->attached_dev; |
| 401 | |
| 402 | /* restore old netdev ops */ |
| 403 | if (priv->ndo_old && dev) |
| 404 | dev->netdev_ops = priv->ndo_old; |
| 405 | dev->phy_ptr = NULL; |
| 406 | dev->features &= ~NETIF_F_HW_VLAN_RX; |
| 407 | kfree(priv); |
| 408 | } |
| 409 | |
| 410 | static int |
| 411 | mvswitch_probe(struct phy_device *pdev) |
| 412 | { |
| 413 | struct mvswitch_priv *priv; |
| 414 | |
| 415 | priv = kzalloc(sizeof(struct mvswitch_priv), GFP_KERNEL); |
| 416 | if (priv == NULL) |
| 417 | return -ENOMEM; |
| 418 | |
| 419 | pdev->priv = priv; |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int |
| 425 | mvswitch_fixup(struct phy_device *dev) |
| 426 | { |
| 427 | u16 reg; |
| 428 | |
| 429 | if (dev->addr != 0x10) |
| 430 | return 0; |
| 431 | |
| 432 | reg = dev->bus->read(dev->bus, MV_PORTREG(IDENT, 0)) & MV_IDENT_MASK; |
| 433 | if (reg != MV_IDENT_VALUE) |
| 434 | return 0; |
| 435 | |
| 436 | dev->phy_id = MVSWITCH_MAGIC; |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | static struct phy_driver mvswitch_driver = { |
| 442 | .name = "Marvell 88E6060", |
| 443 | .phy_id = MVSWITCH_MAGIC, |
| 444 | .phy_id_mask = 0xffffffff, |
| 445 | .features = PHY_BASIC_FEATURES, |
| 446 | .probe = &mvswitch_probe, |
| 447 | .remove = &mvswitch_remove, |
| 448 | .config_init = &mvswitch_config_init, |
| 449 | .config_aneg = &mvswitch_config_aneg, |
| 450 | .read_status = &mvswitch_read_status, |
| 451 | .driver = { .owner = THIS_MODULE,}, |
| 452 | }; |
| 453 | |
| 454 | static int __init |
| 455 | mvswitch_init(void) |
| 456 | { |
| 457 | phy_register_fixup_for_id(PHY_ANY_ID, mvswitch_fixup); |
| 458 | return phy_driver_register(&mvswitch_driver); |
| 459 | } |
| 460 | |
| 461 | static void __exit |
| 462 | mvswitch_exit(void) |
| 463 | { |
| 464 | phy_driver_unregister(&mvswitch_driver); |
| 465 | } |
| 466 | |
| 467 | module_init(mvswitch_init); |
| 468 | module_exit(mvswitch_exit); |
| 469 | |