mvswitch.c 10 KB

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