mvswitch.c 9.9 KB

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