mvswitch.c 9.3 KB

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