mvswitch.c 9.7 KB

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