Advertisement
Guest User

ar8216.c - test

a guest
Dec 29th, 2013
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 62.54 KB | None | 0 0
  1. /*
  2.  * ar8216.c: AR8216 switch driver
  3.  *
  4.  * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
  5.  * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version 2
  10.  * of the License, or (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  */
  17.  
  18. #include <linux/if.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/list.h>
  22. #include <linux/if_ether.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/netlink.h>
  26. #include <linux/bitops.h>
  27. #include <net/genetlink.h>
  28. #include <linux/switch.h>
  29. #include <linux/delay.h>
  30. #include <linux/phy.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/lockdep.h>
  34. #include <linux/ar8216_platform.h>
  35. #include <linux/workqueue.h>
  36. #include <linux/of_device.h>
  37.  
  38. #include "ar8216.h"
  39.  
  40. /* size of the vlan table */
  41. #define AR8X16_MAX_VLANS    128
  42. #define AR8X16_PROBE_RETRIES    10
  43. #define AR8X16_MAX_PORTS    8
  44.  
  45. #define AR8XXX_MIB_WORK_DELAY   2000 /* msecs */
  46.  
  47. struct ar8xxx_priv;
  48.  
  49. #define AR8XXX_CAP_GIGE         BIT(0)
  50. #define AR8XXX_CAP_MIB_COUNTERS     BIT(1)
  51.  
  52. enum {
  53.     AR8XXX_VER_AR8216 = 0x01,
  54.     AR8XXX_VER_AR8236 = 0x03,
  55.     AR8XXX_VER_AR8316 = 0x10,
  56.     AR8XXX_VER_AR8327 = 0x12,
  57.     AR8XXX_VER_AR8337 = 0x13,
  58. };
  59.  
  60. struct ar8xxx_mib_desc {
  61.     unsigned int size;
  62.     unsigned int offset;
  63.     const char *name;
  64. };
  65.  
  66. struct ar8xxx_chip {
  67.     unsigned long caps;
  68.  
  69.     int (*hw_init)(struct ar8xxx_priv *priv);
  70.     void (*init_globals)(struct ar8xxx_priv *priv);
  71.     void (*init_port)(struct ar8xxx_priv *priv, int port);
  72.     void (*setup_port)(struct ar8xxx_priv *priv, int port, u32 egress,
  73.                u32 ingress, u32 members, u32 pvid);
  74.     u32 (*read_port_status)(struct ar8xxx_priv *priv, int port);
  75.     int (*atu_flush)(struct ar8xxx_priv *priv);
  76.     void (*vtu_flush)(struct ar8xxx_priv *priv);
  77.     void (*vtu_load_vlan)(struct ar8xxx_priv *priv, u32 vid, u32 port_mask, u32 tagged);
  78.  
  79.     const struct ar8xxx_mib_desc *mib_decs;
  80.     unsigned num_mibs;
  81. };
  82.  
  83. struct ar8327_data {
  84.     u32 port0_status;
  85.     u32 port6_status;
  86. };
  87.  
  88. struct ar8xxx_priv {
  89.     struct switch_dev dev;
  90.     struct mii_bus *mii_bus;
  91.     struct phy_device *phy;
  92.  
  93.     u32 (*read)(struct ar8xxx_priv *priv, int reg);
  94.     void (*write)(struct ar8xxx_priv *priv, int reg, u32 val);
  95.  
  96.     int (*get_port_link)(unsigned port);
  97.  
  98.     const struct net_device_ops *ndo_old;
  99.     struct net_device_ops ndo;
  100.     struct mutex reg_mutex;
  101.     u8 chip_ver;
  102.     u8 chip_rev;
  103.     const struct ar8xxx_chip *chip;
  104.     union {
  105.         struct ar8327_data ar8327;
  106.     } chip_data;
  107.     bool initialized;
  108.     bool port4_phy;
  109.     char buf[2048];
  110.  
  111.     bool init;
  112.     bool mii_lo_first;
  113.  
  114.     struct mutex mib_lock;
  115.     struct delayed_work mib_work;
  116.     int mib_next_port;
  117.     u64 *mib_stats;
  118.  
  119.     struct list_head list;
  120.     unsigned int use_count;
  121.  
  122.     /* all fields below are cleared on reset */
  123.     bool vlan;
  124.     u16 vlan_id[AR8X16_MAX_VLANS];
  125.     u8 vlan_table[AR8X16_MAX_VLANS];
  126.     u8 port_tagged;
  127.     u16 pvid[AR8X16_MAX_PORTS];
  128.  
  129.     /* mirroring */
  130.     bool mirror_rx;
  131.     bool mirror_tx;
  132.     int source_port;
  133.     int monitor_port;
  134.    
  135.     u8 vlan_tagged[AR8X16_MAX_VLANS];
  136. };
  137.  
  138. #define MIB_DESC(_s , _o, _n)   \
  139.     {           \
  140.         .size = (_s),   \
  141.         .offset = (_o), \
  142.         .name = (_n),   \
  143.     }
  144.  
  145. static const struct ar8xxx_mib_desc ar8216_mibs[] = {
  146.     MIB_DESC(1, AR8216_STATS_RXBROAD, "RxBroad"),
  147.     MIB_DESC(1, AR8216_STATS_RXPAUSE, "RxPause"),
  148.     MIB_DESC(1, AR8216_STATS_RXMULTI, "RxMulti"),
  149.     MIB_DESC(1, AR8216_STATS_RXFCSERR, "RxFcsErr"),
  150.     MIB_DESC(1, AR8216_STATS_RXALIGNERR, "RxAlignErr"),
  151.     MIB_DESC(1, AR8216_STATS_RXRUNT, "RxRunt"),
  152.     MIB_DESC(1, AR8216_STATS_RXFRAGMENT, "RxFragment"),
  153.     MIB_DESC(1, AR8216_STATS_RX64BYTE, "Rx64Byte"),
  154.     MIB_DESC(1, AR8216_STATS_RX128BYTE, "Rx128Byte"),
  155.     MIB_DESC(1, AR8216_STATS_RX256BYTE, "Rx256Byte"),
  156.     MIB_DESC(1, AR8216_STATS_RX512BYTE, "Rx512Byte"),
  157.     MIB_DESC(1, AR8216_STATS_RX1024BYTE, "Rx1024Byte"),
  158.     MIB_DESC(1, AR8216_STATS_RXMAXBYTE, "RxMaxByte"),
  159.     MIB_DESC(1, AR8216_STATS_RXTOOLONG, "RxTooLong"),
  160.     MIB_DESC(2, AR8216_STATS_RXGOODBYTE, "RxGoodByte"),
  161.     MIB_DESC(2, AR8216_STATS_RXBADBYTE, "RxBadByte"),
  162.     MIB_DESC(1, AR8216_STATS_RXOVERFLOW, "RxOverFlow"),
  163.     MIB_DESC(1, AR8216_STATS_FILTERED, "Filtered"),
  164.     MIB_DESC(1, AR8216_STATS_TXBROAD, "TxBroad"),
  165.     MIB_DESC(1, AR8216_STATS_TXPAUSE, "TxPause"),
  166.     MIB_DESC(1, AR8216_STATS_TXMULTI, "TxMulti"),
  167.     MIB_DESC(1, AR8216_STATS_TXUNDERRUN, "TxUnderRun"),
  168.     MIB_DESC(1, AR8216_STATS_TX64BYTE, "Tx64Byte"),
  169.     MIB_DESC(1, AR8216_STATS_TX128BYTE, "Tx128Byte"),
  170.     MIB_DESC(1, AR8216_STATS_TX256BYTE, "Tx256Byte"),
  171.     MIB_DESC(1, AR8216_STATS_TX512BYTE, "Tx512Byte"),
  172.     MIB_DESC(1, AR8216_STATS_TX1024BYTE, "Tx1024Byte"),
  173.     MIB_DESC(1, AR8216_STATS_TXMAXBYTE, "TxMaxByte"),
  174.     MIB_DESC(1, AR8216_STATS_TXOVERSIZE, "TxOverSize"),
  175.     MIB_DESC(2, AR8216_STATS_TXBYTE, "TxByte"),
  176.     MIB_DESC(1, AR8216_STATS_TXCOLLISION, "TxCollision"),
  177.     MIB_DESC(1, AR8216_STATS_TXABORTCOL, "TxAbortCol"),
  178.     MIB_DESC(1, AR8216_STATS_TXMULTICOL, "TxMultiCol"),
  179.     MIB_DESC(1, AR8216_STATS_TXSINGLECOL, "TxSingleCol"),
  180.     MIB_DESC(1, AR8216_STATS_TXEXCDEFER, "TxExcDefer"),
  181.     MIB_DESC(1, AR8216_STATS_TXDEFER, "TxDefer"),
  182.     MIB_DESC(1, AR8216_STATS_TXLATECOL, "TxLateCol"),
  183. };
  184.  
  185. static const struct ar8xxx_mib_desc ar8236_mibs[] = {
  186.     MIB_DESC(1, AR8236_STATS_RXBROAD, "RxBroad"),
  187.     MIB_DESC(1, AR8236_STATS_RXPAUSE, "RxPause"),
  188.     MIB_DESC(1, AR8236_STATS_RXMULTI, "RxMulti"),
  189.     MIB_DESC(1, AR8236_STATS_RXFCSERR, "RxFcsErr"),
  190.     MIB_DESC(1, AR8236_STATS_RXALIGNERR, "RxAlignErr"),
  191.     MIB_DESC(1, AR8236_STATS_RXRUNT, "RxRunt"),
  192.     MIB_DESC(1, AR8236_STATS_RXFRAGMENT, "RxFragment"),
  193.     MIB_DESC(1, AR8236_STATS_RX64BYTE, "Rx64Byte"),
  194.     MIB_DESC(1, AR8236_STATS_RX128BYTE, "Rx128Byte"),
  195.     MIB_DESC(1, AR8236_STATS_RX256BYTE, "Rx256Byte"),
  196.     MIB_DESC(1, AR8236_STATS_RX512BYTE, "Rx512Byte"),
  197.     MIB_DESC(1, AR8236_STATS_RX1024BYTE, "Rx1024Byte"),
  198.     MIB_DESC(1, AR8236_STATS_RX1518BYTE, "Rx1518Byte"),
  199.     MIB_DESC(1, AR8236_STATS_RXMAXBYTE, "RxMaxByte"),
  200.     MIB_DESC(1, AR8236_STATS_RXTOOLONG, "RxTooLong"),
  201.     MIB_DESC(2, AR8236_STATS_RXGOODBYTE, "RxGoodByte"),
  202.     MIB_DESC(2, AR8236_STATS_RXBADBYTE, "RxBadByte"),
  203.     MIB_DESC(1, AR8236_STATS_RXOVERFLOW, "RxOverFlow"),
  204.     MIB_DESC(1, AR8236_STATS_FILTERED, "Filtered"),
  205.     MIB_DESC(1, AR8236_STATS_TXBROAD, "TxBroad"),
  206.     MIB_DESC(1, AR8236_STATS_TXPAUSE, "TxPause"),
  207.     MIB_DESC(1, AR8236_STATS_TXMULTI, "TxMulti"),
  208.     MIB_DESC(1, AR8236_STATS_TXUNDERRUN, "TxUnderRun"),
  209.     MIB_DESC(1, AR8236_STATS_TX64BYTE, "Tx64Byte"),
  210.     MIB_DESC(1, AR8236_STATS_TX128BYTE, "Tx128Byte"),
  211.     MIB_DESC(1, AR8236_STATS_TX256BYTE, "Tx256Byte"),
  212.     MIB_DESC(1, AR8236_STATS_TX512BYTE, "Tx512Byte"),
  213.     MIB_DESC(1, AR8236_STATS_TX1024BYTE, "Tx1024Byte"),
  214.     MIB_DESC(1, AR8236_STATS_TX1518BYTE, "Tx1518Byte"),
  215.     MIB_DESC(1, AR8236_STATS_TXMAXBYTE, "TxMaxByte"),
  216.     MIB_DESC(1, AR8236_STATS_TXOVERSIZE, "TxOverSize"),
  217.     MIB_DESC(2, AR8236_STATS_TXBYTE, "TxByte"),
  218.     MIB_DESC(1, AR8236_STATS_TXCOLLISION, "TxCollision"),
  219.     MIB_DESC(1, AR8236_STATS_TXABORTCOL, "TxAbortCol"),
  220.     MIB_DESC(1, AR8236_STATS_TXMULTICOL, "TxMultiCol"),
  221.     MIB_DESC(1, AR8236_STATS_TXSINGLECOL, "TxSingleCol"),
  222.     MIB_DESC(1, AR8236_STATS_TXEXCDEFER, "TxExcDefer"),
  223.     MIB_DESC(1, AR8236_STATS_TXDEFER, "TxDefer"),
  224.     MIB_DESC(1, AR8236_STATS_TXLATECOL, "TxLateCol"),
  225. };
  226.  
  227. static DEFINE_MUTEX(ar8xxx_dev_list_lock);
  228. static LIST_HEAD(ar8xxx_dev_list);
  229.  
  230. static inline struct ar8xxx_priv *
  231. swdev_to_ar8xxx(struct switch_dev *swdev)
  232. {
  233.     return container_of(swdev, struct ar8xxx_priv, dev);
  234. }
  235.  
  236. static inline bool ar8xxx_has_gige(struct ar8xxx_priv *priv)
  237. {
  238.     return priv->chip->caps & AR8XXX_CAP_GIGE;
  239. }
  240.  
  241. static inline bool ar8xxx_has_mib_counters(struct ar8xxx_priv *priv)
  242. {
  243.     return priv->chip->caps & AR8XXX_CAP_MIB_COUNTERS;
  244. }
  245.  
  246. static inline bool chip_is_ar8216(struct ar8xxx_priv *priv)
  247. {
  248.     return priv->chip_ver == AR8XXX_VER_AR8216;
  249. }
  250.  
  251. static inline bool chip_is_ar8236(struct ar8xxx_priv *priv)
  252. {
  253.     return priv->chip_ver == AR8XXX_VER_AR8236;
  254. }
  255.  
  256. static inline bool chip_is_ar8316(struct ar8xxx_priv *priv)
  257. {
  258.     return priv->chip_ver == AR8XXX_VER_AR8316;
  259. }
  260.  
  261. static inline bool chip_is_ar8327(struct ar8xxx_priv *priv)
  262. {
  263.     return priv->chip_ver == AR8XXX_VER_AR8327;
  264. }
  265.  
  266. static inline bool chip_is_ar8337(struct ar8xxx_priv *priv)
  267. {
  268.     return priv->chip_ver == AR8XXX_VER_AR8337;
  269. }
  270.  
  271. static inline void
  272. split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
  273. {
  274.     regaddr >>= 1;
  275.     *r1 = regaddr & 0x1e;
  276.  
  277.     regaddr >>= 5;
  278.     *r2 = regaddr & 0x7;
  279.  
  280.     regaddr >>= 3;
  281.     *page = regaddr & 0x1ff;
  282. }
  283.  
  284. static u32
  285. ar8xxx_mii_read(struct ar8xxx_priv *priv, int reg)
  286. {
  287.     struct mii_bus *bus = priv->mii_bus;
  288.     u16 r1, r2, page;
  289.     u16 lo, hi;
  290.  
  291.     split_addr((u32) reg, &r1, &r2, &page);
  292.  
  293.     mutex_lock(&bus->mdio_lock);
  294.  
  295.     bus->write(bus, 0x18, 0, page);
  296.     usleep_range(1000, 2000); /* wait for the page switch to propagate */
  297.     lo = bus->read(bus, 0x10 | r2, r1);
  298.     hi = bus->read(bus, 0x10 | r2, r1 + 1);
  299.  
  300.     mutex_unlock(&bus->mdio_lock);
  301.  
  302.     return (hi << 16) | lo;
  303. }
  304.  
  305. static void
  306. ar8xxx_mii_write(struct ar8xxx_priv *priv, int reg, u32 val)
  307. {
  308.     struct mii_bus *bus = priv->mii_bus;
  309.     u16 r1, r2, r3;
  310.     u16 lo, hi;
  311.  
  312.     split_addr((u32) reg, &r1, &r2, &r3);
  313.     lo = val & 0xffff;
  314.     hi = (u16) (val >> 16);
  315.  
  316.     mutex_lock(&bus->mdio_lock);
  317.  
  318.     bus->write(bus, 0x18, 0, r3);
  319.     usleep_range(1000, 2000); /* wait for the page switch to propagate */
  320.     if (priv->mii_lo_first) {
  321.         bus->write(bus, 0x10 | r2, r1, lo);
  322.         bus->write(bus, 0x10 | r2, r1 + 1, hi);
  323.     } else {
  324.         bus->write(bus, 0x10 | r2, r1 + 1, hi);
  325.         bus->write(bus, 0x10 | r2, r1, lo);
  326.     }
  327.  
  328.     mutex_unlock(&bus->mdio_lock);
  329. }
  330.  
  331. static void
  332. ar8xxx_phy_dbg_write(struct ar8xxx_priv *priv, int phy_addr,
  333.              u16 dbg_addr, u16 dbg_data)
  334. {
  335.     struct mii_bus *bus = priv->mii_bus;
  336.  
  337.     mutex_lock(&bus->mdio_lock);
  338.     bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
  339.     bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
  340.     mutex_unlock(&bus->mdio_lock);
  341. }
  342.  
  343. static void
  344. ar8xxx_phy_mmd_write(struct ar8xxx_priv *priv, int phy_addr, u16 addr, u16 data)
  345. {
  346.     struct mii_bus *bus = priv->mii_bus;
  347.  
  348.     mutex_lock(&bus->mdio_lock);
  349.     bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr);
  350.     bus->write(bus, phy_addr, MII_ATH_MMD_DATA, data);
  351.     mutex_unlock(&bus->mdio_lock);
  352. }
  353.  
  354. static u32
  355. ar8xxx_rmw(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  356. {
  357.     u32 v;
  358.  
  359.     lockdep_assert_held(&priv->reg_mutex);
  360.  
  361.     v = priv->read(priv, reg);
  362.     v &= ~mask;
  363.     v |= val;
  364.     priv->write(priv, reg, v);
  365.  
  366.     return v;
  367. }
  368.  
  369. static inline void
  370. ar8xxx_reg_set(struct ar8xxx_priv *priv, int reg, u32 val)
  371. {
  372.     u32 v;
  373.  
  374.     lockdep_assert_held(&priv->reg_mutex);
  375.  
  376.     v = priv->read(priv, reg);
  377.     v |= val;
  378.     priv->write(priv, reg, v);
  379. }
  380.  
  381. static int
  382. ar8xxx_reg_wait(struct ar8xxx_priv *priv, u32 reg, u32 mask, u32 val,
  383.         unsigned timeout)
  384. {
  385.     int i;
  386.  
  387.     for (i = 0; i < timeout; i++) {
  388.         u32 t;
  389.  
  390.         t = priv->read(priv, reg);
  391.         if ((t & mask) == val)
  392.             return 0;
  393.  
  394.         usleep_range(1000, 2000);
  395.     }
  396.  
  397.     return -ETIMEDOUT;
  398. }
  399.  
  400. static int
  401. ar8xxx_mib_op(struct ar8xxx_priv *priv, u32 op)
  402. {
  403.     unsigned mib_func;
  404.     int ret;
  405.  
  406.     lockdep_assert_held(&priv->mib_lock);
  407.  
  408.     if (chip_is_ar8327(priv) || chip_is_ar8337(priv))
  409.         mib_func = AR8327_REG_MIB_FUNC;
  410.     else
  411.         mib_func = AR8216_REG_MIB_FUNC;
  412.  
  413.     mutex_lock(&priv->reg_mutex);
  414.     /* Capture the hardware statistics for all ports */
  415.     ar8xxx_rmw(priv, mib_func, AR8216_MIB_FUNC, (op << AR8216_MIB_FUNC_S));
  416.     mutex_unlock(&priv->reg_mutex);
  417.  
  418.     /* Wait for the capturing to complete. */
  419.     ret = ar8xxx_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
  420.     if (ret)
  421.         goto out;
  422.  
  423.     ret = 0;
  424.  
  425. out:
  426.     return ret;
  427. }
  428.  
  429. static int
  430. ar8xxx_mib_capture(struct ar8xxx_priv *priv)
  431. {
  432.     return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_CAPTURE);
  433. }
  434.  
  435. static int
  436. ar8xxx_mib_flush(struct ar8xxx_priv *priv)
  437. {
  438.     return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_FLUSH);
  439. }
  440.  
  441. static void
  442. ar8xxx_mib_fetch_port_stat(struct ar8xxx_priv *priv, int port, bool flush)
  443. {
  444.     unsigned int base;
  445.     u64 *mib_stats;
  446.     int i;
  447.  
  448.     WARN_ON(port >= priv->dev.ports);
  449.  
  450.     lockdep_assert_held(&priv->mib_lock);
  451.  
  452.     if (chip_is_ar8327(priv) || chip_is_ar8337(priv))
  453.         base = AR8327_REG_PORT_STATS_BASE(port);
  454.     else if (chip_is_ar8236(priv) ||
  455.          chip_is_ar8316(priv))
  456.         base = AR8236_REG_PORT_STATS_BASE(port);
  457.     else
  458.         base = AR8216_REG_PORT_STATS_BASE(port);
  459.  
  460.     mib_stats = &priv->mib_stats[port * priv->chip->num_mibs];
  461.     for (i = 0; i < priv->chip->num_mibs; i++) {
  462.         const struct ar8xxx_mib_desc *mib;
  463.         u64 t;
  464.  
  465.         mib = &priv->chip->mib_decs[i];
  466.         t = priv->read(priv, base + mib->offset);
  467.         if (mib->size == 2) {
  468.             u64 hi;
  469.  
  470.             hi = priv->read(priv, base + mib->offset + 4);
  471.             t |= hi << 32;
  472.         }
  473.  
  474.         if (flush)
  475.             mib_stats[i] = 0;
  476.         else
  477.             mib_stats[i] += t;
  478.     }
  479. }
  480.  
  481. static void
  482. ar8216_read_port_link(struct ar8xxx_priv *priv, int port,
  483.               struct switch_port_link *link)
  484. {
  485.     u32 status;
  486.     u32 speed;
  487.  
  488.     memset(link, '\0', sizeof(*link));
  489.  
  490.     status = priv->chip->read_port_status(priv, port);
  491.  
  492.     link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
  493.     if (link->aneg) {
  494.         link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
  495.     } else {
  496.         link->link = true;
  497.  
  498.         if (priv->get_port_link) {
  499.             int err;
  500.  
  501.             err = priv->get_port_link(port);
  502.             if (err >= 0)
  503.                 link->link = !!err;
  504.         }
  505.     }
  506.  
  507.     if (!link->link)
  508.         return;
  509.  
  510.     link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
  511.     link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
  512.     link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
  513.  
  514.     speed = (status & AR8216_PORT_STATUS_SPEED) >>
  515.          AR8216_PORT_STATUS_SPEED_S;
  516.  
  517.     switch (speed) {
  518.     case AR8216_PORT_SPEED_10M:
  519.         link->speed = SWITCH_PORT_SPEED_10;
  520.         break;
  521.     case AR8216_PORT_SPEED_100M:
  522.         link->speed = SWITCH_PORT_SPEED_100;
  523.         break;
  524.     case AR8216_PORT_SPEED_1000M:
  525.         link->speed = SWITCH_PORT_SPEED_1000;
  526.         break;
  527.     default:
  528.         link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  529.         break;
  530.     }
  531. }
  532.  
  533. static struct sk_buff *
  534. ar8216_mangle_tx(struct net_device *dev, struct sk_buff *skb)
  535. {
  536.     struct ar8xxx_priv *priv = dev->phy_ptr;
  537.     unsigned char *buf;
  538.  
  539.     if (unlikely(!priv))
  540.         goto error;
  541.  
  542.     if (!priv->vlan)
  543.         goto send;
  544.  
  545.     if (unlikely(skb_headroom(skb) < 2)) {
  546.         if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
  547.             goto error;
  548.     }
  549.  
  550.     buf = skb_push(skb, 2);
  551.     buf[0] = 0x10;
  552.     buf[1] = 0x80;
  553.  
  554. send:
  555.     return skb;
  556.  
  557. error:
  558.     dev_kfree_skb_any(skb);
  559.     return NULL;
  560. }
  561.  
  562. static void
  563. ar8216_mangle_rx(struct net_device *dev, struct sk_buff *skb)
  564. {
  565.     struct ar8xxx_priv *priv;
  566.     unsigned char *buf;
  567.     int port, vlan;
  568.  
  569.     priv = dev->phy_ptr;
  570.     if (!priv)
  571.         return;
  572.  
  573.     /* don't strip the header if vlan mode is disabled */
  574.     if (!priv->vlan)
  575.         return;
  576.  
  577.     /* strip header, get vlan id */
  578.     buf = skb->data;
  579.     skb_pull(skb, 2);
  580.  
  581.     /* check for vlan header presence */
  582.     if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
  583.         return;
  584.  
  585.     port = buf[0] & 0xf;
  586.  
  587.     /* no need to fix up packets coming from a tagged source */
  588.     if (priv->port_tagged & BIT(port))
  589.         return;
  590.  
  591.     /* lookup port vid from local table, the switch passes an invalid vlan id */
  592.     vlan = priv->vlan_id[priv->pvid[port]];
  593.  
  594.     buf[14 + 2] &= 0xf0;
  595.     buf[14 + 2] |= vlan >> 8;
  596.     buf[15 + 2] = vlan & 0xff;
  597. }
  598.  
  599. static int
  600. ar8216_wait_bit(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  601. {
  602.     int timeout = 20;
  603.     u32 t = 0;
  604.  
  605.     while (1) {
  606.         t = priv->read(priv, reg);
  607.         if ((t & mask) == val)
  608.             return 0;
  609.  
  610.         if (timeout-- <= 0)
  611.             break;
  612.  
  613.         udelay(10);
  614.     }
  615.  
  616.     pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
  617.            (unsigned int) reg, t, mask, val);
  618.     return -ETIMEDOUT;
  619. }
  620.  
  621. static void
  622. ar8216_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
  623. {
  624.     if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
  625.         return;
  626.     if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
  627.         val &= AR8216_VTUDATA_MEMBER;
  628.         val |= AR8216_VTUDATA_VALID;
  629.         priv->write(priv, AR8216_REG_VTU_DATA, val);
  630.     }
  631.     op |= AR8216_VTU_ACTIVE;
  632.     priv->write(priv, AR8216_REG_VTU, op);
  633. }
  634.  
  635. static void
  636. ar8216_vtu_flush(struct ar8xxx_priv *priv)
  637. {
  638.     ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
  639. }
  640.  
  641. static void
  642. ar8216_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask, u32 tagged)
  643. {
  644.     u32 op;
  645.  
  646.     op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
  647.     ar8216_vtu_op(priv, op, port_mask);
  648. }
  649.  
  650. static int
  651. ar8216_atu_flush(struct ar8xxx_priv *priv)
  652. {
  653.     int ret;
  654.  
  655.     ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
  656.     if (!ret)
  657.         priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
  658.  
  659.     return ret;
  660. }
  661.  
  662. static u32
  663. ar8216_read_port_status(struct ar8xxx_priv *priv, int port)
  664. {
  665.     return priv->read(priv, AR8216_REG_PORT_STATUS(port));
  666. }
  667.  
  668. static void
  669. ar8216_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
  670.           u32 members, u32 pvid)
  671. {
  672.     u32 header;
  673.  
  674.     if (chip_is_ar8216(priv) && priv->vlan && port == AR8216_PORT_CPU)
  675.         header = AR8216_PORT_CTRL_HEADER;
  676.     else
  677.         header = 0;
  678.  
  679.     ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  680.            AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  681.            AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  682.            AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  683.            AR8216_PORT_CTRL_LEARN | header |
  684.            (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  685.            (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  686.  
  687.     ar8xxx_rmw(priv, AR8216_REG_PORT_VLAN(port),
  688.            AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
  689.            AR8216_PORT_VLAN_DEFAULT_ID,
  690.            (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
  691.            (ingress << AR8216_PORT_VLAN_MODE_S) |
  692.            (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
  693. }
  694.  
  695. static int
  696. ar8216_hw_init(struct ar8xxx_priv *priv)
  697. {
  698.     return 0;
  699. }
  700.  
  701. static void
  702. ar8216_init_globals(struct ar8xxx_priv *priv)
  703. {
  704.     /* standard atheros magic */
  705.     priv->write(priv, 0x38, 0xc000050e);
  706.  
  707.     ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  708.            AR8216_GCTRL_MTU, 1518 + 8 + 2);
  709. }
  710.  
  711. static void
  712. ar8216_init_port(struct ar8xxx_priv *priv, int port)
  713. {
  714.     /* Enable port learning and tx */
  715.     priv->write(priv, AR8216_REG_PORT_CTRL(port),
  716.         AR8216_PORT_CTRL_LEARN |
  717.         (4 << AR8216_PORT_CTRL_STATE_S));
  718.  
  719.     priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
  720.  
  721.     if (port == AR8216_PORT_CPU) {
  722.         priv->write(priv, AR8216_REG_PORT_STATUS(port),
  723.             AR8216_PORT_STATUS_LINK_UP |
  724.             (ar8xxx_has_gige(priv) ?
  725.                                 AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
  726.             AR8216_PORT_STATUS_TXMAC |
  727.             AR8216_PORT_STATUS_RXMAC |
  728.             (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_RXFLOW : 0) |
  729.             (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_TXFLOW : 0) |
  730.             AR8216_PORT_STATUS_DUPLEX);
  731.     } else {
  732.         priv->write(priv, AR8216_REG_PORT_STATUS(port),
  733.             AR8216_PORT_STATUS_LINK_AUTO);
  734.     }
  735. }
  736.  
  737. static const struct ar8xxx_chip ar8216_chip = {
  738.     .caps = AR8XXX_CAP_MIB_COUNTERS,
  739.  
  740.     .hw_init = ar8216_hw_init,
  741.     .init_globals = ar8216_init_globals,
  742.     .init_port = ar8216_init_port,
  743.     .setup_port = ar8216_setup_port,
  744.     .read_port_status = ar8216_read_port_status,
  745.     .atu_flush = ar8216_atu_flush,
  746.     .vtu_flush = ar8216_vtu_flush,
  747.     .vtu_load_vlan = ar8216_vtu_load_vlan,
  748.  
  749.     .num_mibs = ARRAY_SIZE(ar8216_mibs),
  750.     .mib_decs = ar8216_mibs,
  751. };
  752.  
  753. static void
  754. ar8236_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
  755.           u32 members, u32 pvid)
  756. {
  757.     ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  758.            AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  759.            AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  760.            AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  761.            AR8216_PORT_CTRL_LEARN |
  762.            (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  763.            (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  764.  
  765.     ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN(port),
  766.            AR8236_PORT_VLAN_DEFAULT_ID,
  767.            (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
  768.  
  769.     ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN2(port),
  770.            AR8236_PORT_VLAN2_VLAN_MODE |
  771.            AR8236_PORT_VLAN2_MEMBER,
  772.            (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
  773.            (members << AR8236_PORT_VLAN2_MEMBER_S));
  774. }
  775.  
  776. static int
  777. ar8236_hw_init(struct ar8xxx_priv *priv)
  778. {
  779.     int i;
  780.     struct mii_bus *bus;
  781.  
  782.     if (priv->initialized)
  783.         return 0;
  784.  
  785.     /* Initialize the PHYs */
  786.     bus = priv->mii_bus;
  787.     for (i = 0; i < 5; i++) {
  788.         mdiobus_write(bus, i, MII_ADVERTISE,
  789.                   ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
  790.                   ADVERTISE_PAUSE_ASYM);
  791.         mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  792.     }
  793.     msleep(1000);
  794.  
  795.     priv->initialized = true;
  796.     return 0;
  797. }
  798.  
  799. static void
  800. ar8236_init_globals(struct ar8xxx_priv *priv)
  801. {
  802.     /* enable jumbo frames */
  803.     ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  804.            AR8316_GCTRL_MTU, 9018 + 8 + 2);
  805.  
  806.     /* Enable MIB counters */
  807.     ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
  808.            (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
  809.            AR8236_MIB_EN);
  810. }
  811.  
  812. static const struct ar8xxx_chip ar8236_chip = {
  813.     .caps = AR8XXX_CAP_MIB_COUNTERS,
  814.     .hw_init = ar8236_hw_init,
  815.     .init_globals = ar8236_init_globals,
  816.     .init_port = ar8216_init_port,
  817.     .setup_port = ar8236_setup_port,
  818.     .read_port_status = ar8216_read_port_status,
  819.     .atu_flush = ar8216_atu_flush,
  820.     .vtu_flush = ar8216_vtu_flush,
  821.     .vtu_load_vlan = ar8216_vtu_load_vlan,
  822.  
  823.     .num_mibs = ARRAY_SIZE(ar8236_mibs),
  824.     .mib_decs = ar8236_mibs,
  825. };
  826.  
  827. static int
  828. ar8316_hw_init(struct ar8xxx_priv *priv)
  829. {
  830.     int i;
  831.     u32 val, newval;
  832.     struct mii_bus *bus;
  833.  
  834.     val = priv->read(priv, AR8316_REG_POSTRIP);
  835.  
  836.     if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  837.         if (priv->port4_phy) {
  838.             /* value taken from Ubiquiti RouterStation Pro */
  839.             newval = 0x81461bea;
  840.             pr_info("ar8316: Using port 4 as PHY\n");
  841.         } else {
  842.             newval = 0x01261be2;
  843.             pr_info("ar8316: Using port 4 as switch port\n");
  844.         }
  845.     } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
  846.         /* value taken from AVM Fritz!Box 7390 sources */
  847.         newval = 0x010e5b71;
  848.     } else {
  849.         /* no known value for phy interface */
  850.         pr_err("ar8316: unsupported mii mode: %d.\n",
  851.                priv->phy->interface);
  852.         return -EINVAL;
  853.     }
  854.  
  855.     if (val == newval)
  856.         goto out;
  857.  
  858.     priv->write(priv, AR8316_REG_POSTRIP, newval);
  859.  
  860.     if (priv->port4_phy &&
  861.         priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  862.         /* work around for phy4 rgmii mode */
  863.         ar8xxx_phy_dbg_write(priv, 4, 0x12, 0x480c);
  864.         /* rx delay */
  865.         ar8xxx_phy_dbg_write(priv, 4, 0x0, 0x824e);
  866.         /* tx delay */
  867.         ar8xxx_phy_dbg_write(priv, 4, 0x5, 0x3d47);
  868.         msleep(1000);
  869.     }
  870.  
  871.     /* Initialize the ports */
  872.     bus = priv->mii_bus;
  873.     for (i = 0; i < 5; i++) {
  874.         /* initialize the port itself */
  875.         mdiobus_write(bus, i, MII_ADVERTISE,
  876.             ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  877.         mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
  878.         mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  879.     }
  880.  
  881.     msleep(1000);
  882.  
  883. out:
  884.     priv->initialized = true;
  885.     return 0;
  886. }
  887.  
  888. static void
  889. ar8316_init_globals(struct ar8xxx_priv *priv)
  890. {
  891.     /* standard atheros magic */
  892.     priv->write(priv, 0x38, 0xc000050e);
  893.  
  894.     /* enable cpu port to receive multicast and broadcast frames */
  895.     priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
  896.  
  897.     /* enable jumbo frames */
  898.     ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  899.            AR8316_GCTRL_MTU, 9018 + 8 + 2);
  900.  
  901.     /* Enable MIB counters */
  902.     ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
  903.            (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
  904.            AR8236_MIB_EN);
  905. }
  906.  
  907. static const struct ar8xxx_chip ar8316_chip = {
  908.     .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  909.     .hw_init = ar8316_hw_init,
  910.     .init_globals = ar8316_init_globals,
  911.     .init_port = ar8216_init_port,
  912.     .setup_port = ar8216_setup_port,
  913.     .read_port_status = ar8216_read_port_status,
  914.     .atu_flush = ar8216_atu_flush,
  915.     .vtu_flush = ar8216_vtu_flush,
  916.     .vtu_load_vlan = ar8216_vtu_load_vlan,
  917.  
  918.     .num_mibs = ARRAY_SIZE(ar8236_mibs),
  919.     .mib_decs = ar8236_mibs,
  920. };
  921.  
  922. static u32
  923. ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
  924. {
  925.     u32 t;
  926.  
  927.     if (!cfg)
  928.         return 0;
  929.  
  930.     t = 0;
  931.     switch (cfg->mode) {
  932.     case AR8327_PAD_NC:
  933.         break;
  934.  
  935.     case AR8327_PAD_MAC2MAC_MII:
  936.         t = AR8327_PAD_MAC_MII_EN;
  937.         if (cfg->rxclk_sel)
  938.             t |= AR8327_PAD_MAC_MII_RXCLK_SEL;
  939.         if (cfg->txclk_sel)
  940.             t |= AR8327_PAD_MAC_MII_TXCLK_SEL;
  941.         break;
  942.  
  943.     case AR8327_PAD_MAC2MAC_GMII:
  944.         t = AR8327_PAD_MAC_GMII_EN;
  945.         if (cfg->rxclk_sel)
  946.             t |= AR8327_PAD_MAC_GMII_RXCLK_SEL;
  947.         if (cfg->txclk_sel)
  948.             t |= AR8327_PAD_MAC_GMII_TXCLK_SEL;
  949.         break;
  950.  
  951.     case AR8327_PAD_MAC_SGMII:
  952.         t = AR8327_PAD_SGMII_EN;
  953.  
  954.         /*
  955.          * WAR for the QUalcomm Atheros AP136 board.
  956.          * It seems that RGMII TX/RX delay settings needs to be
  957.          * applied for SGMII mode as well, The ethernet is not
  958.          * reliable without this.
  959.          */
  960.         t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  961.         t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  962.         if (cfg->rxclk_delay_en)
  963.             t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  964.         if (cfg->txclk_delay_en)
  965.             t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  966.  
  967.         if (cfg->sgmii_delay_en)
  968.             t |= AR8327_PAD_SGMII_DELAY_EN;
  969.  
  970.         break;
  971.  
  972.     case AR8327_PAD_MAC2PHY_MII:
  973.         t = AR8327_PAD_PHY_MII_EN;
  974.         if (cfg->rxclk_sel)
  975.             t |= AR8327_PAD_PHY_MII_RXCLK_SEL;
  976.         if (cfg->txclk_sel)
  977.             t |= AR8327_PAD_PHY_MII_TXCLK_SEL;
  978.         break;
  979.  
  980.     case AR8327_PAD_MAC2PHY_GMII:
  981.         t = AR8327_PAD_PHY_GMII_EN;
  982.         if (cfg->pipe_rxclk_sel)
  983.             t |= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL;
  984.         if (cfg->rxclk_sel)
  985.             t |= AR8327_PAD_PHY_GMII_RXCLK_SEL;
  986.         if (cfg->txclk_sel)
  987.             t |= AR8327_PAD_PHY_GMII_TXCLK_SEL;
  988.         break;
  989.  
  990.     case AR8327_PAD_MAC_RGMII:
  991.         t = AR8327_PAD_RGMII_EN;
  992.         t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  993.         t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  994.         if (cfg->rxclk_delay_en)
  995.             t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  996.         if (cfg->txclk_delay_en)
  997.             t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  998.         break;
  999.  
  1000.     case AR8327_PAD_PHY_GMII:
  1001.         t = AR8327_PAD_PHYX_GMII_EN;
  1002.         break;
  1003.  
  1004.     case AR8327_PAD_PHY_RGMII:
  1005.         t = AR8327_PAD_PHYX_RGMII_EN;
  1006.         break;
  1007.  
  1008.     case AR8327_PAD_PHY_MII:
  1009.         t = AR8327_PAD_PHYX_MII_EN;
  1010.         break;
  1011.     }
  1012.  
  1013.     return t;
  1014. }
  1015.  
  1016. static void
  1017. ar8327_phy_fixup(struct ar8xxx_priv *priv, int phy)
  1018. {
  1019.     switch (priv->chip_rev) {
  1020.     case 1:
  1021.         /* For 100M waveform */
  1022.         ar8xxx_phy_dbg_write(priv, phy, 0, 0x02ea);
  1023.         /* Turn on Gigabit clock */
  1024.         ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x68a0);
  1025.         break;
  1026.  
  1027.     case 2:
  1028.         ar8xxx_phy_mmd_write(priv, phy, 0x7, 0x3c);
  1029.         ar8xxx_phy_mmd_write(priv, phy, 0x4007, 0x0);
  1030.         /* fallthrough */
  1031.     case 4:
  1032.         ar8xxx_phy_mmd_write(priv, phy, 0x3, 0x800d);
  1033.         ar8xxx_phy_mmd_write(priv, phy, 0x4003, 0x803f);
  1034.  
  1035.         ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x6860);
  1036.         ar8xxx_phy_dbg_write(priv, phy, 0x5, 0x2c46);
  1037.         ar8xxx_phy_dbg_write(priv, phy, 0x3c, 0x6000);
  1038.         break;
  1039.     }
  1040. }
  1041.  
  1042. static u32
  1043. ar8327_get_port_init_status(struct ar8327_port_cfg *cfg)
  1044. {
  1045.     u32 t;
  1046.  
  1047.     if (!cfg->force_link)
  1048.         return AR8216_PORT_STATUS_LINK_AUTO;
  1049.  
  1050.     t = AR8216_PORT_STATUS_TXMAC | AR8216_PORT_STATUS_RXMAC;
  1051.     t |= cfg->duplex ? AR8216_PORT_STATUS_DUPLEX : 0;
  1052.     t |= cfg->rxpause ? AR8216_PORT_STATUS_RXFLOW : 0;
  1053.     t |= cfg->txpause ? AR8216_PORT_STATUS_TXFLOW : 0;
  1054.  
  1055.     switch (cfg->speed) {
  1056.     case AR8327_PORT_SPEED_10:
  1057.         t |= AR8216_PORT_SPEED_10M;
  1058.         break;
  1059.     case AR8327_PORT_SPEED_100:
  1060.         t |= AR8216_PORT_SPEED_100M;
  1061.         break;
  1062.     case AR8327_PORT_SPEED_1000:
  1063.         t |= AR8216_PORT_SPEED_1000M;
  1064.         break;
  1065.     }
  1066.  
  1067.     return t;
  1068. }
  1069.  
  1070. static int
  1071. ar8327_hw_config_pdata(struct ar8xxx_priv *priv,
  1072.                struct ar8327_platform_data *pdata)
  1073. {
  1074.     struct ar8327_led_cfg *led_cfg;
  1075.     struct ar8327_data *data;
  1076.     u32 pos, new_pos;
  1077.     u32 t;
  1078.  
  1079.     if (!pdata)
  1080.         return -EINVAL;
  1081.  
  1082.     priv->get_port_link = pdata->get_port_link;
  1083.  
  1084.     data = &priv->chip_data.ar8327;
  1085.  
  1086.     data->port0_status = ar8327_get_port_init_status(&pdata->port0_cfg);
  1087.     data->port6_status = ar8327_get_port_init_status(&pdata->port6_cfg);
  1088.  
  1089.     t = ar8327_get_pad_cfg(pdata->pad0_cfg);
  1090.     if (chip_is_ar8337(priv))
  1091.         t |= AR8337_PAD_MAC06_EXCHANGE_EN;
  1092.  
  1093.     priv->write(priv, AR8327_REG_PAD0_MODE, t);
  1094.     t = ar8327_get_pad_cfg(pdata->pad5_cfg);
  1095.     priv->write(priv, AR8327_REG_PAD5_MODE, t);
  1096.     t = ar8327_get_pad_cfg(pdata->pad6_cfg);
  1097.     priv->write(priv, AR8327_REG_PAD6_MODE, t);
  1098.  
  1099.     pos = priv->read(priv, AR8327_REG_POWER_ON_STRIP);
  1100.     new_pos = pos;
  1101.  
  1102.     led_cfg = pdata->led_cfg;
  1103.     if (led_cfg) {
  1104.         if (led_cfg->open_drain)
  1105.             new_pos |= AR8327_POWER_ON_STRIP_LED_OPEN_EN;
  1106.         else
  1107.             new_pos &= ~AR8327_POWER_ON_STRIP_LED_OPEN_EN;
  1108.  
  1109.         priv->write(priv, AR8327_REG_LED_CTRL0, led_cfg->led_ctrl0);
  1110.         priv->write(priv, AR8327_REG_LED_CTRL1, led_cfg->led_ctrl1);
  1111.         priv->write(priv, AR8327_REG_LED_CTRL2, led_cfg->led_ctrl2);
  1112.         priv->write(priv, AR8327_REG_LED_CTRL3, led_cfg->led_ctrl3);
  1113.  
  1114.         if (new_pos != pos)
  1115.             new_pos |= AR8327_POWER_ON_STRIP_POWER_ON_SEL;
  1116.     }
  1117.  
  1118.     if (pdata->sgmii_cfg) {
  1119.         t = pdata->sgmii_cfg->sgmii_ctrl;
  1120.         if (priv->chip_rev == 1)
  1121.             t |= AR8327_SGMII_CTRL_EN_PLL |
  1122.                  AR8327_SGMII_CTRL_EN_RX |
  1123.                  AR8327_SGMII_CTRL_EN_TX;
  1124.         else
  1125.             t &= ~(AR8327_SGMII_CTRL_EN_PLL |
  1126.                    AR8327_SGMII_CTRL_EN_RX |
  1127.                    AR8327_SGMII_CTRL_EN_TX);
  1128.  
  1129.         priv->write(priv, AR8327_REG_SGMII_CTRL, t);
  1130.  
  1131.         if (pdata->sgmii_cfg->serdes_aen)
  1132.             new_pos &= ~AR8327_POWER_ON_STRIP_SERDES_AEN;
  1133.         else
  1134.             new_pos |= AR8327_POWER_ON_STRIP_SERDES_AEN;
  1135.     }
  1136.  
  1137.     priv->write(priv, AR8327_REG_POWER_ON_STRIP, new_pos);
  1138.  
  1139.     return 0;
  1140. }
  1141.  
  1142. #ifdef CONFIG_OF
  1143. static int
  1144. ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
  1145. {
  1146.     const __be32 *paddr;
  1147.     int len;
  1148.     int i;
  1149.  
  1150.     paddr = of_get_property(np, "qca,ar8327-initvals", &len);
  1151.     if (!paddr || len < (2 * sizeof(*paddr)))
  1152.         return -EINVAL;
  1153.  
  1154.     len /= sizeof(*paddr);
  1155.  
  1156.     for (i = 0; i < len - 1; i += 2) {
  1157.         u32 reg;
  1158.         u32 val;
  1159.  
  1160.         reg = be32_to_cpup(paddr + i);
  1161.         val = be32_to_cpup(paddr + i + 1);
  1162.  
  1163.         switch (reg) {
  1164.         case AR8327_REG_PORT_STATUS(0):
  1165.             priv->chip_data.ar8327.port0_status = val;
  1166.             break;
  1167.         case AR8327_REG_PORT_STATUS(6):
  1168.             priv->chip_data.ar8327.port6_status = val;
  1169.             break;
  1170.         default:
  1171.             priv->write(priv, reg, val);
  1172.             break;
  1173.         }
  1174.     }
  1175.  
  1176.     return 0;
  1177. }
  1178. #else
  1179. static inline int
  1180. ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
  1181. {
  1182.     return -EINVAL;
  1183. }
  1184. #endif
  1185.  
  1186. static int
  1187. ar8327_hw_init(struct ar8xxx_priv *priv)
  1188. {
  1189.     struct mii_bus *bus;
  1190.     int ret;
  1191.     int i;
  1192.  
  1193.     if (priv->phy->dev.of_node)
  1194.         ret = ar8327_hw_config_of(priv, priv->phy->dev.of_node);
  1195.     else
  1196.         ret = ar8327_hw_config_pdata(priv,
  1197.                          priv->phy->dev.platform_data);
  1198.  
  1199.     if (ret)
  1200.         return ret;
  1201.  
  1202.     bus = priv->mii_bus;
  1203.     for (i = 0; i < AR8327_NUM_PHYS; i++) {
  1204.         ar8327_phy_fixup(priv, i);
  1205.  
  1206.         /* start aneg on the PHY */
  1207.         mdiobus_write(bus, i, MII_ADVERTISE, ADVERTISE_ALL |
  1208.                              ADVERTISE_PAUSE_CAP |
  1209.                              ADVERTISE_PAUSE_ASYM);
  1210.         mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
  1211.         mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  1212.     }
  1213.  
  1214.     msleep(1000);
  1215.  
  1216.     return 0;
  1217. }
  1218.  
  1219. static void
  1220. ar8327_init_globals(struct ar8xxx_priv *priv)
  1221. {
  1222.     u32 t;
  1223.  
  1224.     /* enable CPU port and disable mirror port */
  1225.     t = AR8327_FWD_CTRL0_CPU_PORT_EN |
  1226.         AR8327_FWD_CTRL0_MIRROR_PORT;
  1227.     priv->write(priv, AR8327_REG_FWD_CTRL0, t);
  1228.  
  1229.     /* forward multicast and broadcast frames to CPU */
  1230.     t = (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_UC_FLOOD_S) |
  1231.         (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_MC_FLOOD_S) |
  1232.         (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_BC_FLOOD_S);
  1233.     priv->write(priv, AR8327_REG_FWD_CTRL1, t);
  1234.  
  1235.     /* enable jumbo frames */
  1236.     ar8xxx_rmw(priv, AR8327_REG_MAX_FRAME_SIZE,
  1237.            AR8327_MAX_FRAME_SIZE_MTU, 9018 + 8 + 2);
  1238.  
  1239.     /* Enable MIB counters */
  1240.     ar8xxx_reg_set(priv, AR8327_REG_MODULE_EN,
  1241.                AR8327_MODULE_EN_MIB);
  1242. }
  1243.  
  1244. static void
  1245. ar8327_init_port(struct ar8xxx_priv *priv, int port)
  1246. {
  1247.     u32 t;
  1248.  
  1249.     if (port == AR8216_PORT_CPU)
  1250.         t = priv->chip_data.ar8327.port0_status;
  1251.     else if (port == 6)
  1252.         t = priv->chip_data.ar8327.port6_status;
  1253.     else
  1254.         t = AR8216_PORT_STATUS_LINK_AUTO;
  1255.  
  1256.     priv->write(priv, AR8327_REG_PORT_STATUS(port), t);
  1257.     priv->write(priv, AR8327_REG_PORT_HEADER(port), 0);
  1258.  
  1259.     t = 1 << AR8327_PORT_VLAN0_DEF_SVID_S;
  1260.     t |= 1 << AR8327_PORT_VLAN0_DEF_CVID_S;
  1261.     priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
  1262.  
  1263.     t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
  1264.     priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
  1265.  
  1266.     t = AR8327_PORT_LOOKUP_LEARN;
  1267.     t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  1268.     priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  1269. }
  1270.  
  1271. static u32
  1272. ar8327_read_port_status(struct ar8xxx_priv *priv, int port)
  1273. {
  1274.     return priv->read(priv, AR8327_REG_PORT_STATUS(port));
  1275. }
  1276.  
  1277. static int
  1278. ar8327_atu_flush(struct ar8xxx_priv *priv)
  1279. {
  1280.     int ret;
  1281.  
  1282.     ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
  1283.                   AR8327_ATU_FUNC_BUSY, 0);
  1284.     if (!ret)
  1285.         priv->write(priv, AR8327_REG_ATU_FUNC,
  1286.                 AR8327_ATU_FUNC_OP_FLUSH);
  1287.  
  1288.     return ret;
  1289. }
  1290.  
  1291. static void
  1292. ar8327_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
  1293. {
  1294.     if (ar8216_wait_bit(priv, AR8327_REG_VTU_FUNC1,
  1295.                 AR8327_VTU_FUNC1_BUSY, 0))
  1296.         return;
  1297.  
  1298.     if ((op & AR8327_VTU_FUNC1_OP) == AR8327_VTU_FUNC1_OP_LOAD)
  1299.         priv->write(priv, AR8327_REG_VTU_FUNC0, val);
  1300.  
  1301.     op |= AR8327_VTU_FUNC1_BUSY;
  1302.     priv->write(priv, AR8327_REG_VTU_FUNC1, op);
  1303. }
  1304.  
  1305. static void
  1306. ar8327_vtu_flush(struct ar8xxx_priv *priv)
  1307. {
  1308.     ar8327_vtu_op(priv, AR8327_VTU_FUNC1_OP_FLUSH, 0);
  1309. }
  1310.  
  1311. static void
  1312. ar8327_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask, u32 tagged)
  1313. {
  1314.     u32 op;
  1315.     u32 val;
  1316.     int i;
  1317.  
  1318.     op = AR8327_VTU_FUNC1_OP_LOAD | (vid << AR8327_VTU_FUNC1_VID_S);
  1319.     val = AR8327_VTU_FUNC0_VALID | AR8327_VTU_FUNC0_IVL;
  1320.     for (i = 0; i < AR8327_NUM_PORTS; i++) {
  1321.         u32 mode;
  1322.  
  1323.         if ((port_mask & BIT(i)) == 0)
  1324.             mode = AR8327_VTU_FUNC0_EG_MODE_NOT;
  1325.         else if (priv->vlan == 0)
  1326.             mode = AR8327_VTU_FUNC0_EG_MODE_KEEP;
  1327.         else if (tagged & BIT(i))
  1328.             mode = AR8327_VTU_FUNC0_EG_MODE_TAG;
  1329.         else
  1330.             mode = AR8327_VTU_FUNC0_EG_MODE_UNTAG;
  1331.  
  1332.         val |= mode << AR8327_VTU_FUNC0_EG_MODE_S(i);
  1333.     }
  1334.     ar8327_vtu_op(priv, op, val);
  1335. }
  1336.  
  1337. static void
  1338. ar8327_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
  1339.           u32 members, u32 pvid)
  1340. {
  1341.     u32 t;
  1342.     u32 mode;
  1343.  
  1344.     t = pvid << AR8327_PORT_VLAN0_DEF_SVID_S;
  1345.     t |= pvid << AR8327_PORT_VLAN0_DEF_CVID_S;
  1346.     priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
  1347.  
  1348.     mode = AR8327_PORT_VLAN1_OUT_MODE_UNMOD;
  1349.     switch (egress) {
  1350.     case AR8216_OUT_KEEP:
  1351.         mode = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH;
  1352.         break;
  1353.     //case AR8216_OUT_STRIP_VLAN:
  1354.     //  mode = AR8327_PORT_VLAN1_OUT_MODE_UNTAG;
  1355.     //  break;
  1356.     //case AR8216_OUT_ADD_VLAN:
  1357.     //  mode = AR8327_PORT_VLAN1_OUT_MODE_TAG;
  1358.     //  break;
  1359.     }
  1360.  
  1361.     t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
  1362.     t |= mode << AR8327_PORT_VLAN1_OUT_MODE_S;
  1363.     priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
  1364.  
  1365.     t = members;
  1366.     t |= AR8327_PORT_LOOKUP_LEARN;
  1367.     t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
  1368.     t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  1369.     priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  1370. }
  1371.  
  1372. static const struct ar8xxx_chip ar8327_chip = {
  1373.     .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1374.     .hw_init = ar8327_hw_init,
  1375.     .init_globals = ar8327_init_globals,
  1376.     .init_port = ar8327_init_port,
  1377.     .setup_port = ar8327_setup_port,
  1378.     .read_port_status = ar8327_read_port_status,
  1379.     .atu_flush = ar8327_atu_flush,
  1380.     .vtu_flush = ar8327_vtu_flush,
  1381.     .vtu_load_vlan = ar8327_vtu_load_vlan,
  1382.  
  1383.     .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1384.     .mib_decs = ar8236_mibs,
  1385. };
  1386.  
  1387. static int
  1388. ar8xxx_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  1389.            struct switch_val *val)
  1390. {
  1391.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1392.     priv->vlan = !!val->value.i;
  1393.     return 0;
  1394. }
  1395.  
  1396. static int
  1397. ar8xxx_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  1398.            struct switch_val *val)
  1399. {
  1400.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1401.     val->value.i = priv->vlan;
  1402.     return 0;
  1403. }
  1404.  
  1405.  
  1406. static int
  1407. ar8xxx_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
  1408. {
  1409.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1410.  
  1411.     /* make sure no invalid PVIDs get set */
  1412.  
  1413.     if (vlan >= dev->vlans)
  1414.         return -EINVAL;
  1415.  
  1416.     priv->pvid[port] = vlan;
  1417.     return 0;
  1418. }
  1419.  
  1420. static int
  1421. ar8xxx_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
  1422. {
  1423.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1424.     *vlan = priv->pvid[port];
  1425.     return 0;
  1426. }
  1427.  
  1428. static int
  1429. ar8xxx_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  1430.           struct switch_val *val)
  1431. {
  1432.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1433.     priv->vlan_id[val->port_vlan] = val->value.i;
  1434.     return 0;
  1435. }
  1436.  
  1437. static int
  1438. ar8xxx_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  1439.           struct switch_val *val)
  1440. {
  1441.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1442.     val->value.i = priv->vlan_id[val->port_vlan];
  1443.     return 0;
  1444. }
  1445.  
  1446. static int
  1447. ar8xxx_sw_get_port_link(struct switch_dev *dev, int port,
  1448.             struct switch_port_link *link)
  1449. {
  1450.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1451.  
  1452.     ar8216_read_port_link(priv, port, link);
  1453.     return 0;
  1454. }
  1455.  
  1456. static int
  1457. ar8xxx_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
  1458. {
  1459.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1460.     u8 ports = priv->vlan_table[val->port_vlan];
  1461.     u8 tagged = priv->vlan_tagged[val->port_vlan];
  1462.     int i;
  1463.  
  1464.     val->len = 0;
  1465.     for (i = 0; i < dev->ports; i++) {
  1466.         struct switch_port *p;
  1467.  
  1468.         if (!(ports & BIT(i)))
  1469.             continue;
  1470.  
  1471.         p = &val->value.ports[val->len++];
  1472.         p->id = i;
  1473.         if (tagged & BIT(i))
  1474.             p->flags = BIT(SWITCH_PORT_FLAG_TAGGED);
  1475.         else
  1476.             p->flags = 0;
  1477.     }
  1478.     return 0;
  1479. }
  1480.  
  1481. static int
  1482. ar8xxx_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
  1483. {
  1484.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1485.     u8 *vt = &priv->vlan_table[val->port_vlan];
  1486.     u8 *tagged = &priv->vlan_tagged[val->port_vlan];
  1487.     bool portTagging = !(chip_is_ar8327(priv) || chip_is_ar8337(priv));
  1488.    
  1489.     int i, j;
  1490.  
  1491.     *vt = 0;
  1492.     *tagged = 0;
  1493.     for (i = 0; i < val->len; i++) {
  1494.         struct switch_port *p = &val->value.ports[i];
  1495.  
  1496.         if (p->flags & BIT(SWITCH_PORT_FLAG_TAGGED)) {
  1497.             priv->port_tagged |= BIT(p->id);
  1498.             *tagged |= BIT(p->id);
  1499.            
  1500.            
  1501.             if(portTagging){
  1502.                 /* make sure that an tagged port does not
  1503.                  * appear in other vlans as untagged */
  1504.                 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  1505.                     if (j == val->port_vlan)
  1506.                         continue;
  1507.                    
  1508.                     if(priv->vlan_tagged[j] & BIT(SWITCH_PORT_FLAG_TAGGED))
  1509.                         continue;
  1510.                    
  1511.                     priv->vlan_table[j] &= ~(BIT(p->id));
  1512.                 }
  1513.             }
  1514.         } else {
  1515.             priv->port_tagged &= ~(BIT(p->id));
  1516.             priv->pvid[p->id] = val->port_vlan;
  1517.            
  1518.             /* make sure that an untagged port does not
  1519.             * appear in other vlans */
  1520.             for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  1521.                 if (j == val->port_vlan)
  1522.                     continue;
  1523.                
  1524.                 if(!portTagging && (priv->vlan_tagged[j] & BIT(SWITCH_PORT_FLAG_TAGGED)))
  1525.                     continue;
  1526.                
  1527.                 // remove port
  1528.                 priv->vlan_table[j] &= ~(BIT(p->id));
  1529.                 priv->vlan_tagged[j] &= ~(BIT(p->id));
  1530.             }
  1531.         }
  1532.  
  1533.         *vt |= BIT(p->id);
  1534.     }
  1535.     return 0;
  1536. }
  1537.  
  1538. static void
  1539. ar8327_set_mirror_regs(struct ar8xxx_priv *priv)
  1540. {
  1541.     int port;
  1542.  
  1543.     /* reset all mirror registers */
  1544.     ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  1545.            AR8327_FWD_CTRL0_MIRROR_PORT,
  1546.            (0xF << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  1547.     for (port = 0; port < AR8327_NUM_PORTS; port++) {
  1548.         ar8xxx_rmw(priv, AR8327_REG_PORT_LOOKUP(port),
  1549.                AR8327_PORT_LOOKUP_ING_MIRROR_EN,
  1550.                0);
  1551.  
  1552.         ar8xxx_rmw(priv, AR8327_REG_PORT_HOL_CTRL1(port),
  1553.                AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN,
  1554.                0);
  1555.     }
  1556.  
  1557.     /* now enable mirroring if necessary */
  1558.     if (priv->source_port >= AR8327_NUM_PORTS ||
  1559.         priv->monitor_port >= AR8327_NUM_PORTS ||
  1560.         priv->source_port == priv->monitor_port) {
  1561.         return;
  1562.     }
  1563.  
  1564.     ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  1565.            AR8327_FWD_CTRL0_MIRROR_PORT,
  1566.            (priv->monitor_port << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  1567.  
  1568.     if (priv->mirror_rx)
  1569.         ar8xxx_rmw(priv, AR8327_REG_PORT_LOOKUP(priv->source_port),
  1570.                AR8327_PORT_LOOKUP_ING_MIRROR_EN,
  1571.                AR8327_PORT_LOOKUP_ING_MIRROR_EN);
  1572.  
  1573.     if (priv->mirror_tx)
  1574.         ar8xxx_rmw(priv, AR8327_REG_PORT_HOL_CTRL1(priv->source_port),
  1575.                AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN,
  1576.                AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
  1577. }
  1578.  
  1579. static void
  1580. ar8216_set_mirror_regs(struct ar8xxx_priv *priv)
  1581. {
  1582.     int port;
  1583.  
  1584.     /* reset all mirror registers */
  1585.     ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
  1586.            AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
  1587.            (0xF << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  1588.     for (port = 0; port < AR8216_NUM_PORTS; port++) {
  1589.         ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  1590.                AR8216_PORT_CTRL_MIRROR_RX,
  1591.                0);
  1592.  
  1593.         ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  1594.                AR8216_PORT_CTRL_MIRROR_TX,
  1595.                0);
  1596.     }
  1597.  
  1598.     /* now enable mirroring if necessary */
  1599.     if (priv->source_port >= AR8216_NUM_PORTS ||
  1600.         priv->monitor_port >= AR8216_NUM_PORTS ||
  1601.         priv->source_port == priv->monitor_port) {
  1602.         return;
  1603.     }
  1604.  
  1605.     ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
  1606.            AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
  1607.            (priv->monitor_port << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  1608.  
  1609.     if (priv->mirror_rx)
  1610.         ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(priv->source_port),
  1611.                AR8216_PORT_CTRL_MIRROR_RX,
  1612.                AR8216_PORT_CTRL_MIRROR_RX);
  1613.  
  1614.     if (priv->mirror_tx)
  1615.         ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(priv->source_port),
  1616.                AR8216_PORT_CTRL_MIRROR_TX,
  1617.                AR8216_PORT_CTRL_MIRROR_TX);
  1618. }
  1619.  
  1620. static void
  1621. ar8xxx_set_mirror_regs(struct ar8xxx_priv *priv)
  1622. {
  1623.     if (chip_is_ar8327(priv) || chip_is_ar8337(priv)) {
  1624.         ar8327_set_mirror_regs(priv);
  1625.     } else {
  1626.         ar8216_set_mirror_regs(priv);
  1627.     }
  1628. }
  1629.  
  1630. static int
  1631. ar8xxx_sw_hw_apply(struct switch_dev *dev)
  1632. {
  1633.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1634.     u8 portmask[AR8X16_MAX_PORTS];
  1635.     int i, j;
  1636.  
  1637.     mutex_lock(&priv->reg_mutex);
  1638.     /* flush all vlan translation unit entries */
  1639.     priv->chip->vtu_flush(priv);
  1640.  
  1641.     memset(portmask, 0, sizeof(portmask));
  1642.     if (!priv->init) {
  1643.         /* calculate the port destination masks and load vlans
  1644.          * into the vlan translation unit */
  1645.         for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  1646.             u8 vp = priv->vlan_table[j];
  1647.  
  1648.             if (!vp)
  1649.                 continue;
  1650.  
  1651.             for (i = 0; i < dev->ports; i++) {
  1652.                 u8 mask = BIT(i);
  1653.                 if (vp & mask)
  1654.                     portmask[i] |= vp & ~mask;
  1655.             }
  1656.  
  1657.             priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
  1658.                          priv->vlan_table[j],
  1659.                          priv->vlan_tagged[j]
  1660.                         );
  1661.         }
  1662.     } else {
  1663.         /* vlan disabled:
  1664.          * isolate all ports, but connect them to the cpu port */
  1665.         for (i = 0; i < dev->ports; i++) {
  1666.             if (i == AR8216_PORT_CPU)
  1667.                 continue;
  1668.  
  1669.             portmask[i] = BIT(AR8216_PORT_CPU);
  1670.             portmask[AR8216_PORT_CPU] |= BIT(i);
  1671.         }
  1672.     }
  1673.  
  1674.     /* update the port destination mask registers and tag settings */
  1675.     for (i = 0; i < dev->ports; i++) {
  1676.         int egress, ingress;
  1677.         int pvid;
  1678.  
  1679.         if (priv->vlan) {
  1680.             pvid = priv->vlan_id[priv->pvid[i]];
  1681.             if (priv->port_tagged & BIT(i))
  1682.                 egress = AR8216_OUT_ADD_VLAN;
  1683.             else
  1684.                 egress = AR8216_OUT_STRIP_VLAN;
  1685.             ingress = AR8216_IN_SECURE;
  1686.         } else {
  1687.             pvid = i;
  1688.             egress = AR8216_OUT_KEEP;
  1689.             ingress = AR8216_IN_PORT_ONLY;
  1690.         }
  1691.  
  1692.         priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
  1693.                        pvid);
  1694.     }
  1695.  
  1696.     ar8xxx_set_mirror_regs(priv);
  1697.  
  1698.     mutex_unlock(&priv->reg_mutex);
  1699.     return 0;
  1700. }
  1701.  
  1702. static int
  1703. ar8xxx_sw_reset_switch(struct switch_dev *dev)
  1704. {
  1705.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1706.     int i;
  1707.  
  1708.     mutex_lock(&priv->reg_mutex);
  1709.     memset(&priv->vlan, 0, sizeof(struct ar8xxx_priv) -
  1710.         offsetof(struct ar8xxx_priv, vlan));
  1711.  
  1712.     for (i = 0; i < AR8X16_MAX_VLANS; i++)
  1713.         priv->vlan_id[i] = i;
  1714.  
  1715.     /* Configure all ports */
  1716.     for (i = 0; i < dev->ports; i++)
  1717.         priv->chip->init_port(priv, i);
  1718.  
  1719.     priv->mirror_rx = false;
  1720.     priv->mirror_tx = false;
  1721.     priv->source_port = 0;
  1722.     priv->monitor_port = 0;
  1723.  
  1724.     priv->chip->init_globals(priv);
  1725.  
  1726.     mutex_unlock(&priv->reg_mutex);
  1727.  
  1728.     return ar8xxx_sw_hw_apply(dev);
  1729. }
  1730.  
  1731. static int
  1732. ar8xxx_sw_set_reset_mibs(struct switch_dev *dev,
  1733.              const struct switch_attr *attr,
  1734.              struct switch_val *val)
  1735. {
  1736.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1737.     unsigned int len;
  1738.     int ret;
  1739.  
  1740.     if (!ar8xxx_has_mib_counters(priv))
  1741.         return -EOPNOTSUPP;
  1742.  
  1743.     mutex_lock(&priv->mib_lock);
  1744.  
  1745.     len = priv->dev.ports * priv->chip->num_mibs *
  1746.           sizeof(*priv->mib_stats);
  1747.     memset(priv->mib_stats, '\0', len);
  1748.     ret = ar8xxx_mib_flush(priv);
  1749.     if (ret)
  1750.         goto unlock;
  1751.  
  1752.     ret = 0;
  1753.  
  1754. unlock:
  1755.     mutex_unlock(&priv->mib_lock);
  1756.     return ret;
  1757. }
  1758.  
  1759. static int
  1760. ar8xxx_sw_set_mirror_rx_enable(struct switch_dev *dev,
  1761.                    const struct switch_attr *attr,
  1762.                    struct switch_val *val)
  1763. {
  1764.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1765.  
  1766.     mutex_lock(&priv->reg_mutex);
  1767.     priv->mirror_rx = !!val->value.i;
  1768.     ar8xxx_set_mirror_regs(priv);
  1769.     mutex_unlock(&priv->reg_mutex);
  1770.  
  1771.     return 0;
  1772. }
  1773.  
  1774. static int
  1775. ar8xxx_sw_get_mirror_rx_enable(struct switch_dev *dev,
  1776.                    const struct switch_attr *attr,
  1777.                    struct switch_val *val)
  1778. {
  1779.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1780.     val->value.i = priv->mirror_rx;
  1781.     return 0;
  1782. }
  1783.  
  1784. static int
  1785. ar8xxx_sw_set_mirror_tx_enable(struct switch_dev *dev,
  1786.                    const struct switch_attr *attr,
  1787.                    struct switch_val *val)
  1788. {
  1789.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1790.  
  1791.     mutex_lock(&priv->reg_mutex);
  1792.     priv->mirror_tx = !!val->value.i;
  1793.     ar8xxx_set_mirror_regs(priv);
  1794.     mutex_unlock(&priv->reg_mutex);
  1795.  
  1796.     return 0;
  1797. }
  1798.  
  1799. static int
  1800. ar8xxx_sw_get_mirror_tx_enable(struct switch_dev *dev,
  1801.                    const struct switch_attr *attr,
  1802.                    struct switch_val *val)
  1803. {
  1804.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1805.     val->value.i = priv->mirror_tx;
  1806.     return 0;
  1807. }
  1808.  
  1809. static int
  1810. ar8xxx_sw_set_mirror_monitor_port(struct switch_dev *dev,
  1811.                   const struct switch_attr *attr,
  1812.                   struct switch_val *val)
  1813. {
  1814.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1815.  
  1816.     mutex_lock(&priv->reg_mutex);
  1817.     priv->monitor_port = val->value.i;
  1818.     ar8xxx_set_mirror_regs(priv);
  1819.     mutex_unlock(&priv->reg_mutex);
  1820.  
  1821.     return 0;
  1822. }
  1823.  
  1824. static int
  1825. ar8xxx_sw_get_mirror_monitor_port(struct switch_dev *dev,
  1826.                   const struct switch_attr *attr,
  1827.                   struct switch_val *val)
  1828. {
  1829.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1830.     val->value.i = priv->monitor_port;
  1831.     return 0;
  1832. }
  1833.  
  1834. static int
  1835. ar8xxx_sw_set_mirror_source_port(struct switch_dev *dev,
  1836.                  const struct switch_attr *attr,
  1837.                  struct switch_val *val)
  1838. {
  1839.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1840.  
  1841.     mutex_lock(&priv->reg_mutex);
  1842.     priv->source_port = val->value.i;
  1843.     ar8xxx_set_mirror_regs(priv);
  1844.     mutex_unlock(&priv->reg_mutex);
  1845.  
  1846.     return 0;
  1847. }
  1848.  
  1849. static int
  1850. ar8xxx_sw_get_mirror_source_port(struct switch_dev *dev,
  1851.                  const struct switch_attr *attr,
  1852.                  struct switch_val *val)
  1853. {
  1854.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1855.     val->value.i = priv->source_port;
  1856.     return 0;
  1857. }
  1858.  
  1859. static int
  1860. ar8xxx_sw_set_port_reset_mib(struct switch_dev *dev,
  1861.                  const struct switch_attr *attr,
  1862.                  struct switch_val *val)
  1863. {
  1864.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1865.     int port;
  1866.     int ret;
  1867.  
  1868.     if (!ar8xxx_has_mib_counters(priv))
  1869.         return -EOPNOTSUPP;
  1870.  
  1871.     port = val->port_vlan;
  1872.     if (port >= dev->ports)
  1873.         return -EINVAL;
  1874.  
  1875.     mutex_lock(&priv->mib_lock);
  1876.     ret = ar8xxx_mib_capture(priv);
  1877.     if (ret)
  1878.         goto unlock;
  1879.  
  1880.     ar8xxx_mib_fetch_port_stat(priv, port, true);
  1881.  
  1882.     ret = 0;
  1883.  
  1884. unlock:
  1885.     mutex_unlock(&priv->mib_lock);
  1886.     return ret;
  1887. }
  1888.  
  1889. static int
  1890. ar8xxx_sw_get_port_mib(struct switch_dev *dev,
  1891.                const struct switch_attr *attr,
  1892.                struct switch_val *val)
  1893. {
  1894.     struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1895.     const struct ar8xxx_chip *chip = priv->chip;
  1896.     u64 *mib_stats;
  1897.     int port;
  1898.     int ret;
  1899.     char *buf = priv->buf;
  1900.     int i, len = 0;
  1901.  
  1902.     if (!ar8xxx_has_mib_counters(priv))
  1903.         return -EOPNOTSUPP;
  1904.  
  1905.     port = val->port_vlan;
  1906.     if (port >= dev->ports)
  1907.         return -EINVAL;
  1908.  
  1909.     mutex_lock(&priv->mib_lock);
  1910.     ret = ar8xxx_mib_capture(priv);
  1911.     if (ret)
  1912.         goto unlock;
  1913.  
  1914.     ar8xxx_mib_fetch_port_stat(priv, port, false);
  1915.  
  1916.     len += snprintf(buf + len, sizeof(priv->buf) - len,
  1917.             "Port %d MIB counters\n",
  1918.             port);
  1919.  
  1920.     mib_stats = &priv->mib_stats[port * chip->num_mibs];
  1921.     for (i = 0; i < chip->num_mibs; i++)
  1922.         len += snprintf(buf + len, sizeof(priv->buf) - len,
  1923.                 "%-12s: %llu\n",
  1924.                 chip->mib_decs[i].name,
  1925.                 mib_stats[i]);
  1926.  
  1927.     val->value.s = buf;
  1928.     val->len = len;
  1929.  
  1930.     ret = 0;
  1931.  
  1932. unlock:
  1933.     mutex_unlock(&priv->mib_lock);
  1934.     return ret;
  1935. }
  1936.  
  1937. static struct switch_attr ar8xxx_sw_attr_globals[] = {
  1938.     {
  1939.         .type = SWITCH_TYPE_INT,
  1940.         .name = "enable_vlan",
  1941.         .description = "Enable VLAN mode",
  1942.         .set = ar8xxx_sw_set_vlan,
  1943.         .get = ar8xxx_sw_get_vlan,
  1944.         .max = 1
  1945.     },
  1946.     {
  1947.         .type = SWITCH_TYPE_NOVAL,
  1948.         .name = "reset_mibs",
  1949.         .description = "Reset all MIB counters",
  1950.         .set = ar8xxx_sw_set_reset_mibs,
  1951.     },
  1952.     {
  1953.         .type = SWITCH_TYPE_INT,
  1954.         .name = "enable_mirror_rx",
  1955.         .description = "Enable mirroring of RX packets",
  1956.         .set = ar8xxx_sw_set_mirror_rx_enable,
  1957.         .get = ar8xxx_sw_get_mirror_rx_enable,
  1958.         .max = 1
  1959.     },
  1960.     {
  1961.         .type = SWITCH_TYPE_INT,
  1962.         .name = "enable_mirror_tx",
  1963.         .description = "Enable mirroring of TX packets",
  1964.         .set = ar8xxx_sw_set_mirror_tx_enable,
  1965.         .get = ar8xxx_sw_get_mirror_tx_enable,
  1966.         .max = 1
  1967.     },
  1968.     {
  1969.         .type = SWITCH_TYPE_INT,
  1970.         .name = "mirror_monitor_port",
  1971.         .description = "Mirror monitor port",
  1972.         .set = ar8xxx_sw_set_mirror_monitor_port,
  1973.         .get = ar8xxx_sw_get_mirror_monitor_port,
  1974.         .max = AR8216_NUM_PORTS - 1
  1975.     },
  1976.     {
  1977.         .type = SWITCH_TYPE_INT,
  1978.         .name = "mirror_source_port",
  1979.         .description = "Mirror source port",
  1980.         .set = ar8xxx_sw_set_mirror_source_port,
  1981.         .get = ar8xxx_sw_get_mirror_source_port,
  1982.         .max = AR8216_NUM_PORTS - 1
  1983.     },
  1984. };
  1985.  
  1986. static struct switch_attr ar8327_sw_attr_globals[] = {
  1987.     {
  1988.         .type = SWITCH_TYPE_INT,
  1989.         .name = "enable_vlan",
  1990.         .description = "Enable VLAN mode",
  1991.         .set = ar8xxx_sw_set_vlan,
  1992.         .get = ar8xxx_sw_get_vlan,
  1993.         .max = 1
  1994.     },
  1995.     {
  1996.         .type = SWITCH_TYPE_NOVAL,
  1997.         .name = "reset_mibs",
  1998.         .description = "Reset all MIB counters",
  1999.         .set = ar8xxx_sw_set_reset_mibs,
  2000.     },
  2001.     {
  2002.         .type = SWITCH_TYPE_INT,
  2003.         .name = "enable_mirror_rx",
  2004.         .description = "Enable mirroring of RX packets",
  2005.         .set = ar8xxx_sw_set_mirror_rx_enable,
  2006.         .get = ar8xxx_sw_get_mirror_rx_enable,
  2007.         .max = 1
  2008.     },
  2009.     {
  2010.         .type = SWITCH_TYPE_INT,
  2011.         .name = "enable_mirror_tx",
  2012.         .description = "Enable mirroring of TX packets",
  2013.         .set = ar8xxx_sw_set_mirror_tx_enable,
  2014.         .get = ar8xxx_sw_get_mirror_tx_enable,
  2015.         .max = 1
  2016.     },
  2017.     {
  2018.         .type = SWITCH_TYPE_INT,
  2019.         .name = "mirror_monitor_port",
  2020.         .description = "Mirror monitor port",
  2021.         .set = ar8xxx_sw_set_mirror_monitor_port,
  2022.         .get = ar8xxx_sw_get_mirror_monitor_port,
  2023.         .max = AR8327_NUM_PORTS - 1
  2024.     },
  2025.     {
  2026.         .type = SWITCH_TYPE_INT,
  2027.         .name = "mirror_source_port",
  2028.         .description = "Mirror source port",
  2029.         .set = ar8xxx_sw_set_mirror_source_port,
  2030.         .get = ar8xxx_sw_get_mirror_source_port,
  2031.         .max = AR8327_NUM_PORTS - 1
  2032.     },
  2033. };
  2034.  
  2035. static struct switch_attr ar8xxx_sw_attr_port[] = {
  2036.     {
  2037.         .type = SWITCH_TYPE_NOVAL,
  2038.         .name = "reset_mib",
  2039.         .description = "Reset single port MIB counters",
  2040.         .set = ar8xxx_sw_set_port_reset_mib,
  2041.     },
  2042.     {
  2043.         .type = SWITCH_TYPE_STRING,
  2044.         .name = "mib",
  2045.         .description = "Get port's MIB counters",
  2046.         .set = NULL,
  2047.         .get = ar8xxx_sw_get_port_mib,
  2048.     },
  2049. };
  2050.  
  2051. static struct switch_attr ar8xxx_sw_attr_vlan[] = {
  2052.     {
  2053.         .type = SWITCH_TYPE_INT,
  2054.         .name = "vid",
  2055.         .description = "VLAN ID (0-4094)",
  2056.         .set = ar8xxx_sw_set_vid,
  2057.         .get = ar8xxx_sw_get_vid,
  2058.         .max = 4094,
  2059.     },
  2060. };
  2061.  
  2062. static const struct switch_dev_ops ar8xxx_sw_ops = {
  2063.     .attr_global = {
  2064.         .attr = ar8xxx_sw_attr_globals,
  2065.         .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_globals),
  2066.     },
  2067.     .attr_port = {
  2068.         .attr = ar8xxx_sw_attr_port,
  2069.         .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
  2070.     },
  2071.     .attr_vlan = {
  2072.         .attr = ar8xxx_sw_attr_vlan,
  2073.         .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  2074.     },
  2075.     .get_port_pvid = ar8xxx_sw_get_pvid,
  2076.     .set_port_pvid = ar8xxx_sw_set_pvid,
  2077.     .get_vlan_ports = ar8xxx_sw_get_ports,
  2078.     .set_vlan_ports = ar8xxx_sw_set_ports,
  2079.     .apply_config = ar8xxx_sw_hw_apply,
  2080.     .reset_switch = ar8xxx_sw_reset_switch,
  2081.     .get_port_link = ar8xxx_sw_get_port_link,
  2082. };
  2083.  
  2084. static const struct switch_dev_ops ar8327_sw_ops = {
  2085.     .attr_global = {
  2086.         .attr = ar8327_sw_attr_globals,
  2087.         .n_attr = ARRAY_SIZE(ar8327_sw_attr_globals),
  2088.     },
  2089.     .attr_port = {
  2090.         .attr = ar8xxx_sw_attr_port,
  2091.         .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
  2092.     },
  2093.     .attr_vlan = {
  2094.         .attr = ar8xxx_sw_attr_vlan,
  2095.         .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  2096.     },
  2097.     .get_port_pvid = ar8xxx_sw_get_pvid,
  2098.     .set_port_pvid = ar8xxx_sw_set_pvid,
  2099.     .get_vlan_ports = ar8xxx_sw_get_ports,
  2100.     .set_vlan_ports = ar8xxx_sw_set_ports,
  2101.     .apply_config = ar8xxx_sw_hw_apply,
  2102.     .reset_switch = ar8xxx_sw_reset_switch,
  2103.     .get_port_link = ar8xxx_sw_get_port_link,
  2104. };
  2105.  
  2106. static int
  2107. ar8xxx_id_chip(struct ar8xxx_priv *priv)
  2108. {
  2109.     u32 val;
  2110.     u16 id;
  2111.     int i;
  2112.  
  2113.     val = priv->read(priv, AR8216_REG_CTRL);
  2114.     if (val == ~0)
  2115.         return -ENODEV;
  2116.  
  2117.     id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  2118.     for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
  2119.         u16 t;
  2120.  
  2121.         val = priv->read(priv, AR8216_REG_CTRL);
  2122.         if (val == ~0)
  2123.             return -ENODEV;
  2124.  
  2125.         t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  2126.         if (t != id)
  2127.             return -ENODEV;
  2128.     }
  2129.  
  2130.     priv->chip_ver = (id & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
  2131.     priv->chip_rev = (id & AR8216_CTRL_REVISION);
  2132.  
  2133.     switch (priv->chip_ver) {
  2134.     case AR8XXX_VER_AR8216:
  2135.         priv->chip = &ar8216_chip;
  2136.         break;
  2137.     case AR8XXX_VER_AR8236:
  2138.         priv->chip = &ar8236_chip;
  2139.         break;
  2140.     case AR8XXX_VER_AR8316:
  2141.         priv->chip = &ar8316_chip;
  2142.         break;
  2143.     case AR8XXX_VER_AR8327:
  2144.         priv->mii_lo_first = true;
  2145.         priv->chip = &ar8327_chip;
  2146.         break;
  2147.     case AR8XXX_VER_AR8337:
  2148.         priv->mii_lo_first = true;
  2149.         priv->chip = &ar8327_chip;
  2150.         break;
  2151.     default:
  2152.         pr_err("ar8216: Unknown Atheros device [ver=%d, rev=%d]\n",
  2153.                priv->chip_ver, priv->chip_rev);
  2154.  
  2155.         return -ENODEV;
  2156.     }
  2157.  
  2158.     return 0;
  2159. }
  2160.  
  2161. static void
  2162. ar8xxx_mib_work_func(struct work_struct *work)
  2163. {
  2164.     struct ar8xxx_priv *priv;
  2165.     int err;
  2166.  
  2167.     priv = container_of(work, struct ar8xxx_priv, mib_work.work);
  2168.  
  2169.     mutex_lock(&priv->mib_lock);
  2170.  
  2171.     err = ar8xxx_mib_capture(priv);
  2172.     if (err)
  2173.         goto next_port;
  2174.  
  2175.     ar8xxx_mib_fetch_port_stat(priv, priv->mib_next_port, false);
  2176.  
  2177. next_port:
  2178.     priv->mib_next_port++;
  2179.     if (priv->mib_next_port >= priv->dev.ports)
  2180.         priv->mib_next_port = 0;
  2181.  
  2182.     mutex_unlock(&priv->mib_lock);
  2183.     schedule_delayed_work(&priv->mib_work,
  2184.                   msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
  2185. }
  2186.  
  2187. static int
  2188. ar8xxx_mib_init(struct ar8xxx_priv *priv)
  2189. {
  2190.     unsigned int len;
  2191.  
  2192.     if (!ar8xxx_has_mib_counters(priv))
  2193.         return 0;
  2194.  
  2195.     BUG_ON(!priv->chip->mib_decs || !priv->chip->num_mibs);
  2196.  
  2197.     len = priv->dev.ports * priv->chip->num_mibs *
  2198.           sizeof(*priv->mib_stats);
  2199.     priv->mib_stats = kzalloc(len, GFP_KERNEL);
  2200.  
  2201.     if (!priv->mib_stats)
  2202.         return -ENOMEM;
  2203.  
  2204.     return 0;
  2205. }
  2206.  
  2207. static void
  2208. ar8xxx_mib_start(struct ar8xxx_priv *priv)
  2209. {
  2210.     if (!ar8xxx_has_mib_counters(priv))
  2211.         return;
  2212.  
  2213.     schedule_delayed_work(&priv->mib_work,
  2214.                   msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
  2215. }
  2216.  
  2217. static void
  2218. ar8xxx_mib_stop(struct ar8xxx_priv *priv)
  2219. {
  2220.     if (!ar8xxx_has_mib_counters(priv))
  2221.         return;
  2222.  
  2223.     cancel_delayed_work(&priv->mib_work);
  2224. }
  2225.  
  2226. static struct ar8xxx_priv *
  2227. ar8xxx_create(void)
  2228. {
  2229.     struct ar8xxx_priv *priv;
  2230.  
  2231.     priv = kzalloc(sizeof(struct ar8xxx_priv), GFP_KERNEL);
  2232.     if (priv == NULL)
  2233.         return NULL;
  2234.  
  2235.     mutex_init(&priv->reg_mutex);
  2236.     mutex_init(&priv->mib_lock);
  2237.     INIT_DELAYED_WORK(&priv->mib_work, ar8xxx_mib_work_func);
  2238.  
  2239.     return priv;
  2240. }
  2241.  
  2242. static void
  2243. ar8xxx_free(struct ar8xxx_priv *priv)
  2244. {
  2245.     kfree(priv->mib_stats);
  2246.     kfree(priv);
  2247. }
  2248.  
  2249. static struct ar8xxx_priv *
  2250. ar8xxx_create_mii(struct mii_bus *bus)
  2251. {
  2252.     struct ar8xxx_priv *priv;
  2253.  
  2254.     priv = ar8xxx_create();
  2255.     if (priv) {
  2256.         priv->mii_bus = bus;
  2257.         priv->read = ar8xxx_mii_read;
  2258.         priv->write = ar8xxx_mii_write;
  2259.     }
  2260.  
  2261.     return priv;
  2262. }
  2263.  
  2264. static int
  2265. ar8xxx_probe_switch(struct ar8xxx_priv *priv)
  2266. {
  2267.     struct switch_dev *swdev;
  2268.     int ret;
  2269.  
  2270.     ret = ar8xxx_id_chip(priv);
  2271.     if (ret)
  2272.         return ret;
  2273.  
  2274.     swdev = &priv->dev;
  2275.     swdev->cpu_port = AR8216_PORT_CPU;
  2276.     swdev->ops = &ar8xxx_sw_ops;
  2277.  
  2278.     if (chip_is_ar8316(priv)) {
  2279.         swdev->name = "Atheros AR8316";
  2280.         swdev->vlans = AR8X16_MAX_VLANS;
  2281.         swdev->ports = AR8216_NUM_PORTS;
  2282.     } else if (chip_is_ar8236(priv)) {
  2283.         swdev->name = "Atheros AR8236";
  2284.         swdev->vlans = AR8216_NUM_VLANS;
  2285.         swdev->ports = AR8216_NUM_PORTS;
  2286.     } else if (chip_is_ar8327(priv)) {
  2287.         swdev->name = "Atheros AR8327";
  2288.         swdev->vlans = AR8X16_MAX_VLANS;
  2289.         swdev->ports = AR8327_NUM_PORTS;
  2290.         swdev->ops = &ar8327_sw_ops;
  2291.     } else if (chip_is_ar8337(priv)) {
  2292.         swdev->name = "Atheros AR8337";
  2293.         swdev->vlans = AR8X16_MAX_VLANS;
  2294.         swdev->ports = AR8327_NUM_PORTS;
  2295.         swdev->ops = &ar8327_sw_ops;
  2296.     } else {
  2297.         swdev->name = "Atheros AR8216";
  2298.         swdev->vlans = AR8216_NUM_VLANS;
  2299.         swdev->ports = AR8216_NUM_PORTS;
  2300.     }
  2301.  
  2302.     ret = ar8xxx_mib_init(priv);
  2303.     if (ret)
  2304.         return ret;
  2305.  
  2306.     return 0;
  2307. }
  2308.  
  2309. static int
  2310. ar8xxx_start(struct ar8xxx_priv *priv)
  2311. {
  2312.     int ret;
  2313.  
  2314.     priv->init = true;
  2315.  
  2316.     ret = priv->chip->hw_init(priv);
  2317.     if (ret)
  2318.         return ret;
  2319.  
  2320.     ret = ar8xxx_sw_reset_switch(&priv->dev);
  2321.     if (ret)
  2322.         return ret;
  2323.  
  2324.     priv->init = false;
  2325.  
  2326.     ar8xxx_mib_start(priv);
  2327.  
  2328.     return 0;
  2329. }
  2330.  
  2331. static int
  2332. ar8xxx_phy_config_init(struct phy_device *phydev)
  2333. {
  2334.     struct ar8xxx_priv *priv = phydev->priv;
  2335.     struct net_device *dev = phydev->attached_dev;
  2336.     int ret;
  2337.  
  2338.     if (WARN_ON(!priv))
  2339.         return -ENODEV;
  2340.  
  2341.     if (chip_is_ar8327(priv) || chip_is_ar8337(priv))
  2342.         return 0;
  2343.  
  2344.     priv->phy = phydev;
  2345.  
  2346.     if (phydev->addr != 0) {
  2347.         if (chip_is_ar8316(priv)) {
  2348.             /* switch device has been initialized, reinit */
  2349.             priv->dev.ports = (AR8216_NUM_PORTS - 1);
  2350.             priv->initialized = false;
  2351.             priv->port4_phy = true;
  2352.             ar8316_hw_init(priv);
  2353.             return 0;
  2354.         }
  2355.  
  2356.         return 0;
  2357.     }
  2358.  
  2359.     ret = ar8xxx_start(priv);
  2360.     if (ret)
  2361.         return ret;
  2362.  
  2363.     /* VID fixup only needed on ar8216 */
  2364.     if (chip_is_ar8216(priv)) {
  2365.         dev->phy_ptr = priv;
  2366.         dev->priv_flags |= IFF_NO_IP_ALIGN;
  2367.         dev->eth_mangle_rx = ar8216_mangle_rx;
  2368.         dev->eth_mangle_tx = ar8216_mangle_tx;
  2369.     }
  2370.  
  2371.     return 0;
  2372. }
  2373.  
  2374. static int
  2375. ar8xxx_phy_read_status(struct phy_device *phydev)
  2376. {
  2377.     struct ar8xxx_priv *priv = phydev->priv;
  2378.     struct switch_port_link link;
  2379.     int ret;
  2380.  
  2381.     if (phydev->addr != 0)
  2382.         return genphy_read_status(phydev);
  2383.  
  2384.     ar8216_read_port_link(priv, phydev->addr, &link);
  2385.     phydev->link = !!link.link;
  2386.     if (!phydev->link)
  2387.         return 0;
  2388.  
  2389.     switch (link.speed) {
  2390.     case SWITCH_PORT_SPEED_10:
  2391.         phydev->speed = SPEED_10;
  2392.         break;
  2393.     case SWITCH_PORT_SPEED_100:
  2394.         phydev->speed = SPEED_100;
  2395.         break;
  2396.     case SWITCH_PORT_SPEED_1000:
  2397.         phydev->speed = SPEED_1000;
  2398.         break;
  2399.     default:
  2400.         phydev->speed = 0;
  2401.     }
  2402.     phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
  2403.  
  2404.     /* flush the address translation unit */
  2405.     mutex_lock(&priv->reg_mutex);
  2406.     ret = priv->chip->atu_flush(priv);
  2407.     mutex_unlock(&priv->reg_mutex);
  2408.  
  2409.     phydev->state = PHY_RUNNING;
  2410.     netif_carrier_on(phydev->attached_dev);
  2411.     phydev->adjust_link(phydev->attached_dev);
  2412.  
  2413.     return ret;
  2414. }
  2415.  
  2416. static int
  2417. ar8xxx_phy_config_aneg(struct phy_device *phydev)
  2418. {
  2419.     if (phydev->addr == 0)
  2420.         return 0;
  2421.  
  2422.     return genphy_config_aneg(phydev);
  2423. }
  2424.  
  2425. static const u32 ar8xxx_phy_ids[] = {
  2426.     0x004dd033,
  2427.     0x004dd034, /* AR8327 */
  2428.     0x004dd036, /* AR8337 */
  2429.     0x004dd041,
  2430.     0x004dd042,
  2431. };
  2432.  
  2433. static bool
  2434. ar8xxx_phy_match(u32 phy_id)
  2435. {
  2436.     int i;
  2437.  
  2438.     for (i = 0; i < ARRAY_SIZE(ar8xxx_phy_ids); i++)
  2439.         if (phy_id == ar8xxx_phy_ids[i])
  2440.             return true;
  2441.  
  2442.     return false;
  2443. }
  2444.  
  2445. static bool
  2446. ar8xxx_is_possible(struct mii_bus *bus)
  2447. {
  2448.     unsigned i;
  2449.  
  2450.     for (i = 0; i < 4; i++) {
  2451.         u32 phy_id;
  2452.  
  2453.         phy_id = mdiobus_read(bus, i, MII_PHYSID1) << 16;
  2454.         phy_id |= mdiobus_read(bus, i, MII_PHYSID2);
  2455.         if (!ar8xxx_phy_match(phy_id)) {
  2456.             pr_debug("ar8xxx: unknown PHY at %s:%02x id:%08x\n",
  2457.                  dev_name(&bus->dev), i, phy_id);
  2458.             return false;
  2459.         }
  2460.     }
  2461.  
  2462.     return true;
  2463. }
  2464.  
  2465. static int
  2466. ar8xxx_phy_probe(struct phy_device *phydev)
  2467. {
  2468.     struct ar8xxx_priv *priv;
  2469.     struct switch_dev *swdev;
  2470.     int ret;
  2471.  
  2472.     /* skip PHYs at unused adresses */
  2473.     if (phydev->addr != 0 && phydev->addr != 4)
  2474.         return -ENODEV;
  2475.  
  2476.     if (!ar8xxx_is_possible(phydev->bus))
  2477.         return -ENODEV;
  2478.  
  2479.     mutex_lock(&ar8xxx_dev_list_lock);
  2480.     list_for_each_entry(priv, &ar8xxx_dev_list, list)
  2481.         if (priv->mii_bus == phydev->bus)
  2482.             goto found;
  2483.  
  2484.     priv = ar8xxx_create_mii(phydev->bus);
  2485.     if (priv == NULL) {
  2486.         ret = -ENOMEM;
  2487.         goto unlock;
  2488.     }
  2489.  
  2490.     ret = ar8xxx_probe_switch(priv);
  2491.     if (ret)
  2492.         goto free_priv;
  2493.  
  2494.     swdev = &priv->dev;
  2495.     swdev->alias = dev_name(&priv->mii_bus->dev);
  2496.     ret = register_switch(swdev, NULL);
  2497.     if (ret)
  2498.         goto free_priv;
  2499.  
  2500.     pr_info("%s: %s rev. %u switch registered on %s\n",
  2501.         swdev->devname, swdev->name, priv->chip_rev,
  2502.         dev_name(&priv->mii_bus->dev));
  2503.  
  2504. found:
  2505.     priv->use_count++;
  2506.  
  2507.     if (phydev->addr == 0) {
  2508.         if (ar8xxx_has_gige(priv)) {
  2509.             phydev->supported = SUPPORTED_1000baseT_Full;
  2510.             phydev->advertising = ADVERTISED_1000baseT_Full;
  2511.         } else {
  2512.             phydev->supported = SUPPORTED_100baseT_Full;
  2513.             phydev->advertising = ADVERTISED_100baseT_Full;
  2514.         }
  2515.  
  2516.         if (chip_is_ar8327(priv) || chip_is_ar8337(priv)) {
  2517.             priv->phy = phydev;
  2518.  
  2519.             ret = ar8xxx_start(priv);
  2520.             if (ret)
  2521.                 goto err_unregister_switch;
  2522.         }
  2523.     } else {
  2524.         if (ar8xxx_has_gige(priv)) {
  2525.             phydev->supported |= SUPPORTED_1000baseT_Full;
  2526.             phydev->advertising |= ADVERTISED_1000baseT_Full;
  2527.         }
  2528.     }
  2529.  
  2530.     phydev->priv = priv;
  2531.  
  2532.     list_add(&priv->list, &ar8xxx_dev_list);
  2533.  
  2534.     mutex_unlock(&ar8xxx_dev_list_lock);
  2535.  
  2536.     return 0;
  2537.  
  2538. err_unregister_switch:
  2539.     if (--priv->use_count)
  2540.         goto unlock;
  2541.  
  2542.     unregister_switch(&priv->dev);
  2543.  
  2544. free_priv:
  2545.     ar8xxx_free(priv);
  2546. unlock:
  2547.     mutex_unlock(&ar8xxx_dev_list_lock);
  2548.     return ret;
  2549. }
  2550.  
  2551. static void
  2552. ar8xxx_phy_detach(struct phy_device *phydev)
  2553. {
  2554.     struct net_device *dev = phydev->attached_dev;
  2555.  
  2556.     if (!dev)
  2557.         return;
  2558.  
  2559.     dev->phy_ptr = NULL;
  2560.     dev->priv_flags &= ~IFF_NO_IP_ALIGN;
  2561.     dev->eth_mangle_rx = NULL;
  2562.     dev->eth_mangle_tx = NULL;
  2563. }
  2564.  
  2565. static void
  2566. ar8xxx_phy_remove(struct phy_device *phydev)
  2567. {
  2568.     struct ar8xxx_priv *priv = phydev->priv;
  2569.  
  2570.     if (WARN_ON(!priv))
  2571.         return;
  2572.  
  2573.     phydev->priv = NULL;
  2574.     if (--priv->use_count > 0)
  2575.         return;
  2576.  
  2577.     mutex_lock(&ar8xxx_dev_list_lock);
  2578.     list_del(&priv->list);
  2579.     mutex_unlock(&ar8xxx_dev_list_lock);
  2580.  
  2581.     unregister_switch(&priv->dev);
  2582.     ar8xxx_mib_stop(priv);
  2583.     ar8xxx_free(priv);
  2584. }
  2585.  
  2586. static struct phy_driver ar8xxx_phy_driver = {
  2587.     .phy_id     = 0x004d0000,
  2588.     .name       = "Atheros AR8216/AR8236/AR8316",
  2589.     .phy_id_mask    = 0xffff0000,
  2590.     .features   = PHY_BASIC_FEATURES,
  2591.     .probe      = ar8xxx_phy_probe,
  2592.     .remove     = ar8xxx_phy_remove,
  2593.     .detach     = ar8xxx_phy_detach,
  2594.     .config_init    = ar8xxx_phy_config_init,
  2595.     .config_aneg    = ar8xxx_phy_config_aneg,
  2596.     .read_status    = ar8xxx_phy_read_status,
  2597.     .driver     = { .owner = THIS_MODULE },
  2598. };
  2599.  
  2600. int __init
  2601. ar8xxx_init(void)
  2602. {
  2603.     return phy_driver_register(&ar8xxx_phy_driver);
  2604. }
  2605.  
  2606. void __exit
  2607. ar8xxx_exit(void)
  2608. {
  2609.     phy_driver_unregister(&ar8xxx_phy_driver);
  2610. }
  2611.  
  2612. module_init(ar8xxx_init);
  2613. module_exit(ar8xxx_exit);
  2614. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement