mvswitch.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Marvell 88E6060 switch driver
  3. * Copyright (c) 2008 Felix Fietkau <nbd@nbd.name>
  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. netdev_features_t orig_features;
  40. u8 vlans[16];
  41. };
  42. #define to_mvsw(_phy) ((struct mvswitch_priv *) (_phy)->priv)
  43. static inline u16
  44. r16(struct phy_device *phydev, int addr, int reg)
  45. {
  46. struct mii_bus *bus = phydev->mdio.bus;
  47. return bus->read(bus, addr, reg);
  48. }
  49. static inline void
  50. w16(struct phy_device *phydev, int addr, int reg, u16 val)
  51. {
  52. struct mii_bus *bus = phydev->mdio.bus;
  53. bus->write(bus, addr, reg, val);
  54. }
  55. static struct sk_buff *
  56. mvswitch_mangle_tx(struct net_device *dev, struct sk_buff *skb)
  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 skb;
  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 NULL;
  122. }
  123. static void
  124. mvswitch_mangle_rx(struct net_device *dev, struct sk_buff *skb)
  125. {
  126. struct mvswitch_priv *priv;
  127. unsigned char *buf;
  128. int vlan = -1;
  129. int i;
  130. priv = dev->phy_ptr;
  131. if (WARN_ON_ONCE(!priv))
  132. return;
  133. #ifdef HEADER_MODE
  134. buf = skb->data;
  135. skb_pull(skb, MV_HEADER_SIZE);
  136. #else
  137. buf = skb->data + skb->len - MV_TRAILER_SIZE;
  138. if (buf[0] != 0x80)
  139. return;
  140. #endif
  141. /* look for the vlan matching the incoming port */
  142. for (i = 0; i < ARRAY_SIZE(priv->vlans); i++) {
  143. if ((1 << buf[1]) & priv->vlans[i])
  144. vlan = i;
  145. }
  146. if (vlan == -1)
  147. return;
  148. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
  149. }
  150. static int
  151. mvswitch_wait_mask(struct phy_device *pdev, int addr, int reg, u16 mask, u16 val)
  152. {
  153. int i = 100;
  154. u16 r;
  155. do {
  156. r = r16(pdev, addr, reg) & mask;
  157. if (r == val)
  158. return 0;
  159. } while(--i > 0);
  160. return -ETIMEDOUT;
  161. }
  162. static int
  163. mvswitch_config_init(struct phy_device *pdev)
  164. {
  165. struct mvswitch_priv *priv = to_mvsw(pdev);
  166. struct net_device *dev = pdev->attached_dev;
  167. u8 vlmap = 0;
  168. int i;
  169. if (!dev)
  170. return -EINVAL;
  171. printk("%s: Marvell 88E6060 PHY driver attached.\n", dev->name);
  172. pdev->supported = ADVERTISED_100baseT_Full;
  173. pdev->advertising = ADVERTISED_100baseT_Full;
  174. dev->phy_ptr = priv;
  175. pdev->irq = PHY_POLL;
  176. #ifdef HEADER_MODE
  177. dev->flags |= IFF_PROMISC;
  178. #endif
  179. /* initialize default vlans */
  180. for (i = 0; i < MV_PORTS; i++)
  181. priv->vlans[(i == MV_WANPORT ? 2 : 1)] |= (1 << i);
  182. /* before entering reset, disable all ports */
  183. for (i = 0; i < MV_PORTS; i++)
  184. w16(pdev, MV_PORTREG(CONTROL, i), 0x00);
  185. msleep(2); /* wait for the status change to settle in */
  186. /* put the ATU in reset */
  187. w16(pdev, MV_SWITCHREG(ATU_CTRL), MV_ATUCTL_RESET);
  188. i = mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_CTRL), MV_ATUCTL_RESET, 0);
  189. if (i < 0) {
  190. printk("%s: Timeout waiting for the switch to reset.\n", dev->name);
  191. return i;
  192. }
  193. /* set the ATU flags */
  194. w16(pdev, MV_SWITCHREG(ATU_CTRL),
  195. MV_ATUCTL_NO_LEARN |
  196. MV_ATUCTL_ATU_1K |
  197. MV_ATUCTL_AGETIME(MV_ATUCTL_AGETIME_MIN) /* minimum without disabling ageing */
  198. );
  199. /* initialize the cpu port */
  200. w16(pdev, MV_PORTREG(CONTROL, MV_CPUPORT),
  201. #ifdef HEADER_MODE
  202. MV_PORTCTRL_HEADER |
  203. #else
  204. MV_PORTCTRL_RXTR |
  205. MV_PORTCTRL_TXTR |
  206. #endif
  207. MV_PORTCTRL_ENABLED
  208. );
  209. /* wait for the phy change to settle in */
  210. msleep(2);
  211. for (i = 0; i < MV_PORTS; i++) {
  212. u8 pvid = 0;
  213. int j;
  214. vlmap = 0;
  215. /* look for the matching vlan */
  216. for (j = 0; j < ARRAY_SIZE(priv->vlans); j++) {
  217. if (priv->vlans[j] & (1 << i)) {
  218. vlmap = priv->vlans[j];
  219. pvid = j;
  220. }
  221. }
  222. /* leave port unconfigured if it's not part of a vlan */
  223. if (!vlmap)
  224. continue;
  225. /* add the cpu port to the allowed destinations list */
  226. vlmap |= (1 << MV_CPUPORT);
  227. /* take port out of its own vlan destination map */
  228. vlmap &= ~(1 << i);
  229. /* apply vlan settings */
  230. w16(pdev, MV_PORTREG(VLANMAP, i),
  231. MV_PORTVLAN_PORTS(vlmap) |
  232. MV_PORTVLAN_ID(i)
  233. );
  234. /* re-enable port */
  235. w16(pdev, MV_PORTREG(CONTROL, i),
  236. MV_PORTCTRL_ENABLED
  237. );
  238. }
  239. w16(pdev, MV_PORTREG(VLANMAP, MV_CPUPORT),
  240. MV_PORTVLAN_ID(MV_CPUPORT)
  241. );
  242. /* set the port association vector */
  243. for (i = 0; i <= MV_PORTS; i++) {
  244. w16(pdev, MV_PORTREG(ASSOC, i),
  245. MV_PORTASSOC_PORTS(1 << i)
  246. );
  247. }
  248. /* init switch control */
  249. w16(pdev, MV_SWITCHREG(CTRL),
  250. MV_SWITCHCTL_MSIZE |
  251. MV_SWITCHCTL_DROP
  252. );
  253. dev->eth_mangle_rx = mvswitch_mangle_rx;
  254. dev->eth_mangle_tx = mvswitch_mangle_tx;
  255. priv->orig_features = dev->features;
  256. #ifdef HEADER_MODE
  257. dev->priv_flags |= IFF_NO_IP_ALIGN;
  258. dev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
  259. #else
  260. dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  261. #endif
  262. return 0;
  263. }
  264. static int
  265. mvswitch_read_status(struct phy_device *pdev)
  266. {
  267. pdev->speed = SPEED_100;
  268. pdev->duplex = DUPLEX_FULL;
  269. pdev->link = 1;
  270. /* XXX ugly workaround: we can't force the switch
  271. * to gracefully handle hosts moving from one port to another,
  272. * so we have to regularly clear the ATU database */
  273. /* wait for the ATU to become available */
  274. mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_OP), MV_ATUOP_INPROGRESS, 0);
  275. /* flush the ATU */
  276. w16(pdev, MV_SWITCHREG(ATU_OP),
  277. MV_ATUOP_INPROGRESS |
  278. MV_ATUOP_FLUSH_ALL
  279. );
  280. /* wait for operation to complete */
  281. mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_OP), MV_ATUOP_INPROGRESS, 0);
  282. return 0;
  283. }
  284. static int
  285. mvswitch_aneg_done(struct phy_device *phydev)
  286. {
  287. return 1; /* Return any positive value */
  288. }
  289. static int
  290. mvswitch_config_aneg(struct phy_device *phydev)
  291. {
  292. return 0;
  293. }
  294. static void
  295. mvswitch_detach(struct phy_device *pdev)
  296. {
  297. struct mvswitch_priv *priv = to_mvsw(pdev);
  298. struct net_device *dev = pdev->attached_dev;
  299. if (!dev)
  300. return;
  301. dev->phy_ptr = NULL;
  302. dev->eth_mangle_rx = NULL;
  303. dev->eth_mangle_tx = NULL;
  304. dev->features = priv->orig_features;
  305. dev->priv_flags &= ~IFF_NO_IP_ALIGN;
  306. }
  307. static void
  308. mvswitch_remove(struct phy_device *pdev)
  309. {
  310. struct mvswitch_priv *priv = to_mvsw(pdev);
  311. kfree(priv);
  312. }
  313. static int
  314. mvswitch_probe(struct phy_device *pdev)
  315. {
  316. struct mvswitch_priv *priv;
  317. priv = kzalloc(sizeof(struct mvswitch_priv), GFP_KERNEL);
  318. if (priv == NULL)
  319. return -ENOMEM;
  320. pdev->priv = priv;
  321. return 0;
  322. }
  323. static int
  324. mvswitch_fixup(struct phy_device *dev)
  325. {
  326. struct mii_bus *bus = dev->mdio.bus;
  327. u16 reg;
  328. if (dev->mdio.addr != 0x10)
  329. return 0;
  330. reg = bus->read(bus, MV_PORTREG(IDENT, 0)) & MV_IDENT_MASK;
  331. if (reg != MV_IDENT_VALUE)
  332. return 0;
  333. dev->phy_id = MVSWITCH_MAGIC;
  334. return 0;
  335. }
  336. static struct phy_driver mvswitch_driver = {
  337. .name = "Marvell 88E6060",
  338. .phy_id = MVSWITCH_MAGIC,
  339. .phy_id_mask = 0xffffffff,
  340. .features = PHY_BASIC_FEATURES,
  341. .probe = &mvswitch_probe,
  342. .remove = &mvswitch_remove,
  343. .detach = &mvswitch_detach,
  344. .config_init = &mvswitch_config_init,
  345. .config_aneg = &mvswitch_config_aneg,
  346. .aneg_done = &mvswitch_aneg_done,
  347. .read_status = &mvswitch_read_status,
  348. };
  349. static int __init
  350. mvswitch_init(void)
  351. {
  352. phy_register_fixup_for_id(PHY_ANY_ID, mvswitch_fixup);
  353. return phy_driver_register(&mvswitch_driver, THIS_MODULE);
  354. }
  355. static void __exit
  356. mvswitch_exit(void)
  357. {
  358. phy_driver_unregister(&mvswitch_driver);
  359. }
  360. module_init(mvswitch_init);
  361. module_exit(mvswitch_exit);