ar8216.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  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. #include <linux/if.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/if_ether.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/netlink.h>
  25. #include <linux/bitops.h>
  26. #include <net/genetlink.h>
  27. #include <linux/switch.h>
  28. #include <linux/delay.h>
  29. #include <linux/phy.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/lockdep.h>
  33. #include <linux/ar8216_platform.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/version.h>
  36. #include "ar8216.h"
  37. extern const struct ar8xxx_chip ar8327_chip;
  38. extern const struct ar8xxx_chip ar8337_chip;
  39. #define AR8XXX_MIB_WORK_DELAY 2000 /* msecs */
  40. #define MIB_DESC(_s , _o, _n) \
  41. { \
  42. .size = (_s), \
  43. .offset = (_o), \
  44. .name = (_n), \
  45. }
  46. static const struct ar8xxx_mib_desc ar8216_mibs[] = {
  47. MIB_DESC(1, AR8216_STATS_RXBROAD, "RxBroad"),
  48. MIB_DESC(1, AR8216_STATS_RXPAUSE, "RxPause"),
  49. MIB_DESC(1, AR8216_STATS_RXMULTI, "RxMulti"),
  50. MIB_DESC(1, AR8216_STATS_RXFCSERR, "RxFcsErr"),
  51. MIB_DESC(1, AR8216_STATS_RXALIGNERR, "RxAlignErr"),
  52. MIB_DESC(1, AR8216_STATS_RXRUNT, "RxRunt"),
  53. MIB_DESC(1, AR8216_STATS_RXFRAGMENT, "RxFragment"),
  54. MIB_DESC(1, AR8216_STATS_RX64BYTE, "Rx64Byte"),
  55. MIB_DESC(1, AR8216_STATS_RX128BYTE, "Rx128Byte"),
  56. MIB_DESC(1, AR8216_STATS_RX256BYTE, "Rx256Byte"),
  57. MIB_DESC(1, AR8216_STATS_RX512BYTE, "Rx512Byte"),
  58. MIB_DESC(1, AR8216_STATS_RX1024BYTE, "Rx1024Byte"),
  59. MIB_DESC(1, AR8216_STATS_RXMAXBYTE, "RxMaxByte"),
  60. MIB_DESC(1, AR8216_STATS_RXTOOLONG, "RxTooLong"),
  61. MIB_DESC(2, AR8216_STATS_RXGOODBYTE, "RxGoodByte"),
  62. MIB_DESC(2, AR8216_STATS_RXBADBYTE, "RxBadByte"),
  63. MIB_DESC(1, AR8216_STATS_RXOVERFLOW, "RxOverFlow"),
  64. MIB_DESC(1, AR8216_STATS_FILTERED, "Filtered"),
  65. MIB_DESC(1, AR8216_STATS_TXBROAD, "TxBroad"),
  66. MIB_DESC(1, AR8216_STATS_TXPAUSE, "TxPause"),
  67. MIB_DESC(1, AR8216_STATS_TXMULTI, "TxMulti"),
  68. MIB_DESC(1, AR8216_STATS_TXUNDERRUN, "TxUnderRun"),
  69. MIB_DESC(1, AR8216_STATS_TX64BYTE, "Tx64Byte"),
  70. MIB_DESC(1, AR8216_STATS_TX128BYTE, "Tx128Byte"),
  71. MIB_DESC(1, AR8216_STATS_TX256BYTE, "Tx256Byte"),
  72. MIB_DESC(1, AR8216_STATS_TX512BYTE, "Tx512Byte"),
  73. MIB_DESC(1, AR8216_STATS_TX1024BYTE, "Tx1024Byte"),
  74. MIB_DESC(1, AR8216_STATS_TXMAXBYTE, "TxMaxByte"),
  75. MIB_DESC(1, AR8216_STATS_TXOVERSIZE, "TxOverSize"),
  76. MIB_DESC(2, AR8216_STATS_TXBYTE, "TxByte"),
  77. MIB_DESC(1, AR8216_STATS_TXCOLLISION, "TxCollision"),
  78. MIB_DESC(1, AR8216_STATS_TXABORTCOL, "TxAbortCol"),
  79. MIB_DESC(1, AR8216_STATS_TXMULTICOL, "TxMultiCol"),
  80. MIB_DESC(1, AR8216_STATS_TXSINGLECOL, "TxSingleCol"),
  81. MIB_DESC(1, AR8216_STATS_TXEXCDEFER, "TxExcDefer"),
  82. MIB_DESC(1, AR8216_STATS_TXDEFER, "TxDefer"),
  83. MIB_DESC(1, AR8216_STATS_TXLATECOL, "TxLateCol"),
  84. };
  85. const struct ar8xxx_mib_desc ar8236_mibs[39] = {
  86. MIB_DESC(1, AR8236_STATS_RXBROAD, "RxBroad"),
  87. MIB_DESC(1, AR8236_STATS_RXPAUSE, "RxPause"),
  88. MIB_DESC(1, AR8236_STATS_RXMULTI, "RxMulti"),
  89. MIB_DESC(1, AR8236_STATS_RXFCSERR, "RxFcsErr"),
  90. MIB_DESC(1, AR8236_STATS_RXALIGNERR, "RxAlignErr"),
  91. MIB_DESC(1, AR8236_STATS_RXRUNT, "RxRunt"),
  92. MIB_DESC(1, AR8236_STATS_RXFRAGMENT, "RxFragment"),
  93. MIB_DESC(1, AR8236_STATS_RX64BYTE, "Rx64Byte"),
  94. MIB_DESC(1, AR8236_STATS_RX128BYTE, "Rx128Byte"),
  95. MIB_DESC(1, AR8236_STATS_RX256BYTE, "Rx256Byte"),
  96. MIB_DESC(1, AR8236_STATS_RX512BYTE, "Rx512Byte"),
  97. MIB_DESC(1, AR8236_STATS_RX1024BYTE, "Rx1024Byte"),
  98. MIB_DESC(1, AR8236_STATS_RX1518BYTE, "Rx1518Byte"),
  99. MIB_DESC(1, AR8236_STATS_RXMAXBYTE, "RxMaxByte"),
  100. MIB_DESC(1, AR8236_STATS_RXTOOLONG, "RxTooLong"),
  101. MIB_DESC(2, AR8236_STATS_RXGOODBYTE, "RxGoodByte"),
  102. MIB_DESC(2, AR8236_STATS_RXBADBYTE, "RxBadByte"),
  103. MIB_DESC(1, AR8236_STATS_RXOVERFLOW, "RxOverFlow"),
  104. MIB_DESC(1, AR8236_STATS_FILTERED, "Filtered"),
  105. MIB_DESC(1, AR8236_STATS_TXBROAD, "TxBroad"),
  106. MIB_DESC(1, AR8236_STATS_TXPAUSE, "TxPause"),
  107. MIB_DESC(1, AR8236_STATS_TXMULTI, "TxMulti"),
  108. MIB_DESC(1, AR8236_STATS_TXUNDERRUN, "TxUnderRun"),
  109. MIB_DESC(1, AR8236_STATS_TX64BYTE, "Tx64Byte"),
  110. MIB_DESC(1, AR8236_STATS_TX128BYTE, "Tx128Byte"),
  111. MIB_DESC(1, AR8236_STATS_TX256BYTE, "Tx256Byte"),
  112. MIB_DESC(1, AR8236_STATS_TX512BYTE, "Tx512Byte"),
  113. MIB_DESC(1, AR8236_STATS_TX1024BYTE, "Tx1024Byte"),
  114. MIB_DESC(1, AR8236_STATS_TX1518BYTE, "Tx1518Byte"),
  115. MIB_DESC(1, AR8236_STATS_TXMAXBYTE, "TxMaxByte"),
  116. MIB_DESC(1, AR8236_STATS_TXOVERSIZE, "TxOverSize"),
  117. MIB_DESC(2, AR8236_STATS_TXBYTE, "TxByte"),
  118. MIB_DESC(1, AR8236_STATS_TXCOLLISION, "TxCollision"),
  119. MIB_DESC(1, AR8236_STATS_TXABORTCOL, "TxAbortCol"),
  120. MIB_DESC(1, AR8236_STATS_TXMULTICOL, "TxMultiCol"),
  121. MIB_DESC(1, AR8236_STATS_TXSINGLECOL, "TxSingleCol"),
  122. MIB_DESC(1, AR8236_STATS_TXEXCDEFER, "TxExcDefer"),
  123. MIB_DESC(1, AR8236_STATS_TXDEFER, "TxDefer"),
  124. MIB_DESC(1, AR8236_STATS_TXLATECOL, "TxLateCol"),
  125. };
  126. static DEFINE_MUTEX(ar8xxx_dev_list_lock);
  127. static LIST_HEAD(ar8xxx_dev_list);
  128. /* inspired by phy_poll_reset in drivers/net/phy/phy_device.c */
  129. static int
  130. ar8xxx_phy_poll_reset(struct mii_bus *bus)
  131. {
  132. unsigned int sleep_msecs = 20;
  133. int ret, elapsed, i;
  134. for (elapsed = sleep_msecs; elapsed <= 600;
  135. elapsed += sleep_msecs) {
  136. msleep(sleep_msecs);
  137. for (i = 0; i < AR8XXX_NUM_PHYS; i++) {
  138. ret = mdiobus_read(bus, i, MII_BMCR);
  139. if (ret < 0)
  140. return ret;
  141. if (ret & BMCR_RESET)
  142. break;
  143. if (i == AR8XXX_NUM_PHYS - 1) {
  144. usleep_range(1000, 2000);
  145. return 0;
  146. }
  147. }
  148. }
  149. return -ETIMEDOUT;
  150. }
  151. static int
  152. ar8xxx_phy_check_aneg(struct phy_device *phydev)
  153. {
  154. int ret;
  155. if (phydev->autoneg != AUTONEG_ENABLE)
  156. return 0;
  157. /*
  158. * BMCR_ANENABLE might have been cleared
  159. * by phy_init_hw in certain kernel versions
  160. * therefore check for it
  161. */
  162. ret = phy_read(phydev, MII_BMCR);
  163. if (ret < 0)
  164. return ret;
  165. if (ret & BMCR_ANENABLE)
  166. return 0;
  167. dev_info(&phydev->dev, "ANEG disabled, re-enabling ...\n");
  168. ret |= BMCR_ANENABLE | BMCR_ANRESTART;
  169. return phy_write(phydev, MII_BMCR, ret);
  170. }
  171. void
  172. ar8xxx_phy_init(struct ar8xxx_priv *priv)
  173. {
  174. int i;
  175. struct mii_bus *bus;
  176. bus = priv->mii_bus;
  177. for (i = 0; i < AR8XXX_NUM_PHYS; i++) {
  178. if (priv->chip->phy_fixup)
  179. priv->chip->phy_fixup(priv, i);
  180. /* initialize the port itself */
  181. mdiobus_write(bus, i, MII_ADVERTISE,
  182. ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  183. if (ar8xxx_has_gige(priv))
  184. mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
  185. mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  186. }
  187. ar8xxx_phy_poll_reset(bus);
  188. }
  189. u32
  190. ar8xxx_mii_read32(struct ar8xxx_priv *priv, int phy_id, int regnum)
  191. {
  192. struct mii_bus *bus = priv->mii_bus;
  193. u16 lo, hi;
  194. lo = bus->read(bus, phy_id, regnum);
  195. hi = bus->read(bus, phy_id, regnum + 1);
  196. return (hi << 16) | lo;
  197. }
  198. void
  199. ar8xxx_mii_write32(struct ar8xxx_priv *priv, int phy_id, int regnum, u32 val)
  200. {
  201. struct mii_bus *bus = priv->mii_bus;
  202. u16 lo, hi;
  203. lo = val & 0xffff;
  204. hi = (u16) (val >> 16);
  205. if (priv->chip->mii_lo_first)
  206. {
  207. bus->write(bus, phy_id, regnum, lo);
  208. bus->write(bus, phy_id, regnum + 1, hi);
  209. } else {
  210. bus->write(bus, phy_id, regnum + 1, hi);
  211. bus->write(bus, phy_id, regnum, lo);
  212. }
  213. }
  214. u32
  215. ar8xxx_read(struct ar8xxx_priv *priv, int reg)
  216. {
  217. struct mii_bus *bus = priv->mii_bus;
  218. u16 r1, r2, page;
  219. u32 val;
  220. split_addr((u32) reg, &r1, &r2, &page);
  221. mutex_lock(&bus->mdio_lock);
  222. bus->write(bus, 0x18, 0, page);
  223. wait_for_page_switch();
  224. val = ar8xxx_mii_read32(priv, 0x10 | r2, r1);
  225. mutex_unlock(&bus->mdio_lock);
  226. return val;
  227. }
  228. void
  229. ar8xxx_write(struct ar8xxx_priv *priv, int reg, u32 val)
  230. {
  231. struct mii_bus *bus = priv->mii_bus;
  232. u16 r1, r2, page;
  233. split_addr((u32) reg, &r1, &r2, &page);
  234. mutex_lock(&bus->mdio_lock);
  235. bus->write(bus, 0x18, 0, page);
  236. wait_for_page_switch();
  237. ar8xxx_mii_write32(priv, 0x10 | r2, r1, val);
  238. mutex_unlock(&bus->mdio_lock);
  239. }
  240. u32
  241. ar8xxx_rmw(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  242. {
  243. struct mii_bus *bus = priv->mii_bus;
  244. u16 r1, r2, page;
  245. u32 ret;
  246. split_addr((u32) reg, &r1, &r2, &page);
  247. mutex_lock(&bus->mdio_lock);
  248. bus->write(bus, 0x18, 0, page);
  249. wait_for_page_switch();
  250. ret = ar8xxx_mii_read32(priv, 0x10 | r2, r1);
  251. ret &= ~mask;
  252. ret |= val;
  253. ar8xxx_mii_write32(priv, 0x10 | r2, r1, ret);
  254. mutex_unlock(&bus->mdio_lock);
  255. return ret;
  256. }
  257. void
  258. ar8xxx_phy_dbg_write(struct ar8xxx_priv *priv, int phy_addr,
  259. u16 dbg_addr, u16 dbg_data)
  260. {
  261. struct mii_bus *bus = priv->mii_bus;
  262. mutex_lock(&bus->mdio_lock);
  263. bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
  264. bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
  265. mutex_unlock(&bus->mdio_lock);
  266. }
  267. void
  268. ar8xxx_phy_mmd_write(struct ar8xxx_priv *priv, int phy_addr, u16 addr, u16 data)
  269. {
  270. struct mii_bus *bus = priv->mii_bus;
  271. mutex_lock(&bus->mdio_lock);
  272. bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr);
  273. bus->write(bus, phy_addr, MII_ATH_MMD_DATA, data);
  274. mutex_unlock(&bus->mdio_lock);
  275. }
  276. u16
  277. ar8xxx_phy_mmd_read(struct ar8xxx_priv *priv, int phy_addr, u16 addr)
  278. {
  279. struct mii_bus *bus = priv->mii_bus;
  280. u16 data;
  281. mutex_lock(&bus->mdio_lock);
  282. bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr);
  283. data = bus->read(bus, phy_addr, MII_ATH_MMD_DATA);
  284. mutex_unlock(&bus->mdio_lock);
  285. return data;
  286. }
  287. static int
  288. ar8xxx_reg_wait(struct ar8xxx_priv *priv, u32 reg, u32 mask, u32 val,
  289. unsigned timeout)
  290. {
  291. int i;
  292. for (i = 0; i < timeout; i++) {
  293. u32 t;
  294. t = ar8xxx_read(priv, reg);
  295. if ((t & mask) == val)
  296. return 0;
  297. usleep_range(1000, 2000);
  298. }
  299. return -ETIMEDOUT;
  300. }
  301. static int
  302. ar8xxx_mib_op(struct ar8xxx_priv *priv, u32 op)
  303. {
  304. unsigned mib_func = priv->chip->mib_func;
  305. int ret;
  306. lockdep_assert_held(&priv->mib_lock);
  307. /* Capture the hardware statistics for all ports */
  308. ar8xxx_rmw(priv, mib_func, AR8216_MIB_FUNC, (op << AR8216_MIB_FUNC_S));
  309. /* Wait for the capturing to complete. */
  310. ret = ar8xxx_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
  311. if (ret)
  312. goto out;
  313. ret = 0;
  314. out:
  315. return ret;
  316. }
  317. static int
  318. ar8xxx_mib_capture(struct ar8xxx_priv *priv)
  319. {
  320. return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_CAPTURE);
  321. }
  322. static int
  323. ar8xxx_mib_flush(struct ar8xxx_priv *priv)
  324. {
  325. return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_FLUSH);
  326. }
  327. static void
  328. ar8xxx_mib_fetch_port_stat(struct ar8xxx_priv *priv, int port, bool flush)
  329. {
  330. unsigned int base;
  331. u64 *mib_stats;
  332. int i;
  333. WARN_ON(port >= priv->dev.ports);
  334. lockdep_assert_held(&priv->mib_lock);
  335. base = priv->chip->reg_port_stats_start +
  336. priv->chip->reg_port_stats_length * port;
  337. mib_stats = &priv->mib_stats[port * priv->chip->num_mibs];
  338. for (i = 0; i < priv->chip->num_mibs; i++) {
  339. const struct ar8xxx_mib_desc *mib;
  340. u64 t;
  341. mib = &priv->chip->mib_decs[i];
  342. t = ar8xxx_read(priv, base + mib->offset);
  343. if (mib->size == 2) {
  344. u64 hi;
  345. hi = ar8xxx_read(priv, base + mib->offset + 4);
  346. t |= hi << 32;
  347. }
  348. if (flush)
  349. mib_stats[i] = 0;
  350. else
  351. mib_stats[i] += t;
  352. }
  353. }
  354. static void
  355. ar8216_read_port_link(struct ar8xxx_priv *priv, int port,
  356. struct switch_port_link *link)
  357. {
  358. u32 status;
  359. u32 speed;
  360. memset(link, '\0', sizeof(*link));
  361. status = priv->chip->read_port_status(priv, port);
  362. link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
  363. if (link->aneg) {
  364. link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
  365. } else {
  366. link->link = true;
  367. if (priv->get_port_link) {
  368. int err;
  369. err = priv->get_port_link(port);
  370. if (err >= 0)
  371. link->link = !!err;
  372. }
  373. }
  374. if (!link->link)
  375. return;
  376. link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
  377. link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
  378. link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
  379. if (link->aneg && link->duplex && priv->chip->read_port_eee_status)
  380. link->eee = priv->chip->read_port_eee_status(priv, port);
  381. speed = (status & AR8216_PORT_STATUS_SPEED) >>
  382. AR8216_PORT_STATUS_SPEED_S;
  383. switch (speed) {
  384. case AR8216_PORT_SPEED_10M:
  385. link->speed = SWITCH_PORT_SPEED_10;
  386. break;
  387. case AR8216_PORT_SPEED_100M:
  388. link->speed = SWITCH_PORT_SPEED_100;
  389. break;
  390. case AR8216_PORT_SPEED_1000M:
  391. link->speed = SWITCH_PORT_SPEED_1000;
  392. break;
  393. default:
  394. link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  395. break;
  396. }
  397. }
  398. static struct sk_buff *
  399. ar8216_mangle_tx(struct net_device *dev, struct sk_buff *skb)
  400. {
  401. struct ar8xxx_priv *priv = dev->phy_ptr;
  402. unsigned char *buf;
  403. if (unlikely(!priv))
  404. goto error;
  405. if (!priv->vlan)
  406. goto send;
  407. if (unlikely(skb_headroom(skb) < 2)) {
  408. if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
  409. goto error;
  410. }
  411. buf = skb_push(skb, 2);
  412. buf[0] = 0x10;
  413. buf[1] = 0x80;
  414. send:
  415. return skb;
  416. error:
  417. dev_kfree_skb_any(skb);
  418. return NULL;
  419. }
  420. static void
  421. ar8216_mangle_rx(struct net_device *dev, struct sk_buff *skb)
  422. {
  423. struct ar8xxx_priv *priv;
  424. unsigned char *buf;
  425. int port, vlan;
  426. priv = dev->phy_ptr;
  427. if (!priv)
  428. return;
  429. /* don't strip the header if vlan mode is disabled */
  430. if (!priv->vlan)
  431. return;
  432. /* strip header, get vlan id */
  433. buf = skb->data;
  434. skb_pull(skb, 2);
  435. /* check for vlan header presence */
  436. if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
  437. return;
  438. port = buf[0] & 0xf;
  439. /* no need to fix up packets coming from a tagged source */
  440. if (priv->vlan_tagged & (1 << port))
  441. return;
  442. /* lookup port vid from local table, the switch passes an invalid vlan id */
  443. vlan = priv->vlan_id[priv->pvid[port]];
  444. buf[14 + 2] &= 0xf0;
  445. buf[14 + 2] |= vlan >> 8;
  446. buf[15 + 2] = vlan & 0xff;
  447. }
  448. int
  449. ar8216_wait_bit(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  450. {
  451. int timeout = 20;
  452. u32 t = 0;
  453. while (1) {
  454. t = ar8xxx_read(priv, reg);
  455. if ((t & mask) == val)
  456. return 0;
  457. if (timeout-- <= 0)
  458. break;
  459. udelay(10);
  460. }
  461. pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
  462. (unsigned int) reg, t, mask, val);
  463. return -ETIMEDOUT;
  464. }
  465. static void
  466. ar8216_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
  467. {
  468. if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
  469. return;
  470. if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
  471. val &= AR8216_VTUDATA_MEMBER;
  472. val |= AR8216_VTUDATA_VALID;
  473. ar8xxx_write(priv, AR8216_REG_VTU_DATA, val);
  474. }
  475. op |= AR8216_VTU_ACTIVE;
  476. ar8xxx_write(priv, AR8216_REG_VTU, op);
  477. }
  478. static void
  479. ar8216_vtu_flush(struct ar8xxx_priv *priv)
  480. {
  481. ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
  482. }
  483. static void
  484. ar8216_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
  485. {
  486. u32 op;
  487. op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
  488. ar8216_vtu_op(priv, op, port_mask);
  489. }
  490. static int
  491. ar8216_atu_flush(struct ar8xxx_priv *priv)
  492. {
  493. int ret;
  494. ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
  495. if (!ret)
  496. ar8xxx_write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH |
  497. AR8216_ATU_ACTIVE);
  498. return ret;
  499. }
  500. static u32
  501. ar8216_read_port_status(struct ar8xxx_priv *priv, int port)
  502. {
  503. return ar8xxx_read(priv, AR8216_REG_PORT_STATUS(port));
  504. }
  505. static void
  506. ar8216_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
  507. {
  508. u32 header;
  509. u32 egress, ingress;
  510. u32 pvid;
  511. if (priv->vlan) {
  512. pvid = priv->vlan_id[priv->pvid[port]];
  513. if (priv->vlan_tagged & (1 << port))
  514. egress = AR8216_OUT_ADD_VLAN;
  515. else
  516. egress = AR8216_OUT_STRIP_VLAN;
  517. ingress = AR8216_IN_SECURE;
  518. } else {
  519. pvid = port;
  520. egress = AR8216_OUT_KEEP;
  521. ingress = AR8216_IN_PORT_ONLY;
  522. }
  523. if (chip_is_ar8216(priv) && priv->vlan && port == AR8216_PORT_CPU)
  524. header = AR8216_PORT_CTRL_HEADER;
  525. else
  526. header = 0;
  527. ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  528. AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  529. AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  530. AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  531. AR8216_PORT_CTRL_LEARN | header |
  532. (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  533. (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  534. ar8xxx_rmw(priv, AR8216_REG_PORT_VLAN(port),
  535. AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
  536. AR8216_PORT_VLAN_DEFAULT_ID,
  537. (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
  538. (ingress << AR8216_PORT_VLAN_MODE_S) |
  539. (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
  540. }
  541. static int
  542. ar8216_hw_init(struct ar8xxx_priv *priv)
  543. {
  544. if (priv->initialized)
  545. return 0;
  546. ar8xxx_phy_init(priv);
  547. priv->initialized = true;
  548. return 0;
  549. }
  550. static void
  551. ar8216_init_globals(struct ar8xxx_priv *priv)
  552. {
  553. /* standard atheros magic */
  554. ar8xxx_write(priv, 0x38, 0xc000050e);
  555. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  556. AR8216_GCTRL_MTU, 1518 + 8 + 2);
  557. }
  558. static void
  559. ar8216_init_port(struct ar8xxx_priv *priv, int port)
  560. {
  561. /* Enable port learning and tx */
  562. ar8xxx_write(priv, AR8216_REG_PORT_CTRL(port),
  563. AR8216_PORT_CTRL_LEARN |
  564. (4 << AR8216_PORT_CTRL_STATE_S));
  565. ar8xxx_write(priv, AR8216_REG_PORT_VLAN(port), 0);
  566. if (port == AR8216_PORT_CPU) {
  567. ar8xxx_write(priv, AR8216_REG_PORT_STATUS(port),
  568. AR8216_PORT_STATUS_LINK_UP |
  569. (ar8xxx_has_gige(priv) ?
  570. AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
  571. AR8216_PORT_STATUS_TXMAC |
  572. AR8216_PORT_STATUS_RXMAC |
  573. (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_RXFLOW : 0) |
  574. (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_TXFLOW : 0) |
  575. AR8216_PORT_STATUS_DUPLEX);
  576. } else {
  577. ar8xxx_write(priv, AR8216_REG_PORT_STATUS(port),
  578. AR8216_PORT_STATUS_LINK_AUTO);
  579. }
  580. }
  581. static void
  582. ar8236_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
  583. {
  584. u32 egress, ingress;
  585. u32 pvid;
  586. if (priv->vlan) {
  587. pvid = priv->vlan_id[priv->pvid[port]];
  588. if (priv->vlan_tagged & (1 << port))
  589. egress = AR8216_OUT_ADD_VLAN;
  590. else
  591. egress = AR8216_OUT_STRIP_VLAN;
  592. ingress = AR8216_IN_SECURE;
  593. } else {
  594. pvid = port;
  595. egress = AR8216_OUT_KEEP;
  596. ingress = AR8216_IN_PORT_ONLY;
  597. }
  598. ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  599. AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  600. AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  601. AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  602. AR8216_PORT_CTRL_LEARN |
  603. (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  604. (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  605. ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN(port),
  606. AR8236_PORT_VLAN_DEFAULT_ID,
  607. (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
  608. ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN2(port),
  609. AR8236_PORT_VLAN2_VLAN_MODE |
  610. AR8236_PORT_VLAN2_MEMBER,
  611. (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
  612. (members << AR8236_PORT_VLAN2_MEMBER_S));
  613. }
  614. static void
  615. ar8236_init_globals(struct ar8xxx_priv *priv)
  616. {
  617. /* enable jumbo frames */
  618. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  619. AR8316_GCTRL_MTU, 9018 + 8 + 2);
  620. /* enable cpu port to receive arp frames */
  621. ar8xxx_reg_set(priv, AR8216_REG_ATU_CTRL,
  622. AR8236_ATU_CTRL_RES);
  623. /* enable cpu port to receive multicast and broadcast frames */
  624. ar8xxx_reg_set(priv, AR8216_REG_FLOOD_MASK,
  625. AR8236_FM_CPU_BROADCAST_EN | AR8236_FM_CPU_BCAST_FWD_EN);
  626. /* Enable MIB counters */
  627. ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
  628. (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
  629. AR8236_MIB_EN);
  630. }
  631. static int
  632. ar8316_hw_init(struct ar8xxx_priv *priv)
  633. {
  634. u32 val, newval;
  635. val = ar8xxx_read(priv, AR8316_REG_POSTRIP);
  636. if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  637. if (priv->port4_phy) {
  638. /* value taken from Ubiquiti RouterStation Pro */
  639. newval = 0x81461bea;
  640. pr_info("ar8316: Using port 4 as PHY\n");
  641. } else {
  642. newval = 0x01261be2;
  643. pr_info("ar8316: Using port 4 as switch port\n");
  644. }
  645. } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
  646. /* value taken from AVM Fritz!Box 7390 sources */
  647. newval = 0x010e5b71;
  648. } else {
  649. /* no known value for phy interface */
  650. pr_err("ar8316: unsupported mii mode: %d.\n",
  651. priv->phy->interface);
  652. return -EINVAL;
  653. }
  654. if (val == newval)
  655. goto out;
  656. ar8xxx_write(priv, AR8316_REG_POSTRIP, newval);
  657. if (priv->port4_phy &&
  658. priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  659. /* work around for phy4 rgmii mode */
  660. ar8xxx_phy_dbg_write(priv, 4, 0x12, 0x480c);
  661. /* rx delay */
  662. ar8xxx_phy_dbg_write(priv, 4, 0x0, 0x824e);
  663. /* tx delay */
  664. ar8xxx_phy_dbg_write(priv, 4, 0x5, 0x3d47);
  665. msleep(1000);
  666. }
  667. ar8xxx_phy_init(priv);
  668. out:
  669. priv->initialized = true;
  670. return 0;
  671. }
  672. static void
  673. ar8316_init_globals(struct ar8xxx_priv *priv)
  674. {
  675. /* standard atheros magic */
  676. ar8xxx_write(priv, 0x38, 0xc000050e);
  677. /* enable cpu port to receive multicast and broadcast frames */
  678. ar8xxx_write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
  679. /* enable jumbo frames */
  680. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  681. AR8316_GCTRL_MTU, 9018 + 8 + 2);
  682. /* Enable MIB counters */
  683. ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
  684. (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
  685. AR8236_MIB_EN);
  686. }
  687. int
  688. ar8xxx_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  689. struct switch_val *val)
  690. {
  691. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  692. priv->vlan = !!val->value.i;
  693. return 0;
  694. }
  695. int
  696. ar8xxx_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  697. struct switch_val *val)
  698. {
  699. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  700. val->value.i = priv->vlan;
  701. return 0;
  702. }
  703. int
  704. ar8xxx_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
  705. {
  706. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  707. /* make sure no invalid PVIDs get set */
  708. if (vlan >= dev->vlans)
  709. return -EINVAL;
  710. priv->pvid[port] = vlan;
  711. return 0;
  712. }
  713. int
  714. ar8xxx_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
  715. {
  716. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  717. *vlan = priv->pvid[port];
  718. return 0;
  719. }
  720. static int
  721. ar8xxx_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  722. struct switch_val *val)
  723. {
  724. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  725. priv->vlan_id[val->port_vlan] = val->value.i;
  726. return 0;
  727. }
  728. static int
  729. ar8xxx_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  730. struct switch_val *val)
  731. {
  732. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  733. val->value.i = priv->vlan_id[val->port_vlan];
  734. return 0;
  735. }
  736. int
  737. ar8xxx_sw_get_port_link(struct switch_dev *dev, int port,
  738. struct switch_port_link *link)
  739. {
  740. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  741. ar8216_read_port_link(priv, port, link);
  742. return 0;
  743. }
  744. static int
  745. ar8xxx_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
  746. {
  747. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  748. u8 ports = priv->vlan_table[val->port_vlan];
  749. int i;
  750. val->len = 0;
  751. for (i = 0; i < dev->ports; i++) {
  752. struct switch_port *p;
  753. if (!(ports & (1 << i)))
  754. continue;
  755. p = &val->value.ports[val->len++];
  756. p->id = i;
  757. if (priv->vlan_tagged & (1 << i))
  758. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  759. else
  760. p->flags = 0;
  761. }
  762. return 0;
  763. }
  764. static int
  765. ar8xxx_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
  766. {
  767. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  768. u8 *vt = &priv->vlan_table[val->port_vlan];
  769. int i, j;
  770. *vt = 0;
  771. for (i = 0; i < val->len; i++) {
  772. struct switch_port *p = &val->value.ports[i];
  773. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  774. priv->vlan_tagged |= (1 << p->id);
  775. } else {
  776. priv->vlan_tagged &= ~(1 << p->id);
  777. priv->pvid[p->id] = val->port_vlan;
  778. /* make sure that an untagged port does not
  779. * appear in other vlans */
  780. for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  781. if (j == val->port_vlan)
  782. continue;
  783. priv->vlan_table[j] &= ~(1 << p->id);
  784. }
  785. }
  786. *vt |= 1 << p->id;
  787. }
  788. return 0;
  789. }
  790. static void
  791. ar8216_set_mirror_regs(struct ar8xxx_priv *priv)
  792. {
  793. int port;
  794. /* reset all mirror registers */
  795. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
  796. AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
  797. (0xF << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  798. for (port = 0; port < AR8216_NUM_PORTS; port++) {
  799. ar8xxx_reg_clear(priv, AR8216_REG_PORT_CTRL(port),
  800. AR8216_PORT_CTRL_MIRROR_RX);
  801. ar8xxx_reg_clear(priv, AR8216_REG_PORT_CTRL(port),
  802. AR8216_PORT_CTRL_MIRROR_TX);
  803. }
  804. /* now enable mirroring if necessary */
  805. if (priv->source_port >= AR8216_NUM_PORTS ||
  806. priv->monitor_port >= AR8216_NUM_PORTS ||
  807. priv->source_port == priv->monitor_port) {
  808. return;
  809. }
  810. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
  811. AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
  812. (priv->monitor_port << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  813. if (priv->mirror_rx)
  814. ar8xxx_reg_set(priv, AR8216_REG_PORT_CTRL(priv->source_port),
  815. AR8216_PORT_CTRL_MIRROR_RX);
  816. if (priv->mirror_tx)
  817. ar8xxx_reg_set(priv, AR8216_REG_PORT_CTRL(priv->source_port),
  818. AR8216_PORT_CTRL_MIRROR_TX);
  819. }
  820. int
  821. ar8xxx_sw_hw_apply(struct switch_dev *dev)
  822. {
  823. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  824. u8 portmask[AR8X16_MAX_PORTS];
  825. int i, j;
  826. mutex_lock(&priv->reg_mutex);
  827. /* flush all vlan translation unit entries */
  828. priv->chip->vtu_flush(priv);
  829. memset(portmask, 0, sizeof(portmask));
  830. if (!priv->init) {
  831. /* calculate the port destination masks and load vlans
  832. * into the vlan translation unit */
  833. for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  834. u8 vp = priv->vlan_table[j];
  835. if (!vp)
  836. continue;
  837. for (i = 0; i < dev->ports; i++) {
  838. u8 mask = (1 << i);
  839. if (vp & mask)
  840. portmask[i] |= vp & ~mask;
  841. }
  842. priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
  843. priv->vlan_table[j]);
  844. }
  845. } else {
  846. /* vlan disabled:
  847. * isolate all ports, but connect them to the cpu port */
  848. for (i = 0; i < dev->ports; i++) {
  849. if (i == AR8216_PORT_CPU)
  850. continue;
  851. portmask[i] = 1 << AR8216_PORT_CPU;
  852. portmask[AR8216_PORT_CPU] |= (1 << i);
  853. }
  854. }
  855. /* update the port destination mask registers and tag settings */
  856. for (i = 0; i < dev->ports; i++) {
  857. priv->chip->setup_port(priv, i, portmask[i]);
  858. }
  859. priv->chip->set_mirror_regs(priv);
  860. mutex_unlock(&priv->reg_mutex);
  861. return 0;
  862. }
  863. int
  864. ar8xxx_sw_reset_switch(struct switch_dev *dev)
  865. {
  866. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  867. const struct ar8xxx_chip *chip = priv->chip;
  868. int i;
  869. mutex_lock(&priv->reg_mutex);
  870. memset(&priv->vlan, 0, sizeof(struct ar8xxx_priv) -
  871. offsetof(struct ar8xxx_priv, vlan));
  872. for (i = 0; i < AR8X16_MAX_VLANS; i++)
  873. priv->vlan_id[i] = i;
  874. /* Configure all ports */
  875. for (i = 0; i < dev->ports; i++)
  876. chip->init_port(priv, i);
  877. priv->mirror_rx = false;
  878. priv->mirror_tx = false;
  879. priv->source_port = 0;
  880. priv->monitor_port = 0;
  881. chip->init_globals(priv);
  882. mutex_unlock(&priv->reg_mutex);
  883. return chip->sw_hw_apply(dev);
  884. }
  885. int
  886. ar8xxx_sw_set_reset_mibs(struct switch_dev *dev,
  887. const struct switch_attr *attr,
  888. struct switch_val *val)
  889. {
  890. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  891. unsigned int len;
  892. int ret;
  893. if (!ar8xxx_has_mib_counters(priv))
  894. return -EOPNOTSUPP;
  895. mutex_lock(&priv->mib_lock);
  896. len = priv->dev.ports * priv->chip->num_mibs *
  897. sizeof(*priv->mib_stats);
  898. memset(priv->mib_stats, '\0', len);
  899. ret = ar8xxx_mib_flush(priv);
  900. if (ret)
  901. goto unlock;
  902. ret = 0;
  903. unlock:
  904. mutex_unlock(&priv->mib_lock);
  905. return ret;
  906. }
  907. int
  908. ar8xxx_sw_set_mirror_rx_enable(struct switch_dev *dev,
  909. const struct switch_attr *attr,
  910. struct switch_val *val)
  911. {
  912. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  913. mutex_lock(&priv->reg_mutex);
  914. priv->mirror_rx = !!val->value.i;
  915. priv->chip->set_mirror_regs(priv);
  916. mutex_unlock(&priv->reg_mutex);
  917. return 0;
  918. }
  919. int
  920. ar8xxx_sw_get_mirror_rx_enable(struct switch_dev *dev,
  921. const struct switch_attr *attr,
  922. struct switch_val *val)
  923. {
  924. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  925. val->value.i = priv->mirror_rx;
  926. return 0;
  927. }
  928. int
  929. ar8xxx_sw_set_mirror_tx_enable(struct switch_dev *dev,
  930. const struct switch_attr *attr,
  931. struct switch_val *val)
  932. {
  933. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  934. mutex_lock(&priv->reg_mutex);
  935. priv->mirror_tx = !!val->value.i;
  936. priv->chip->set_mirror_regs(priv);
  937. mutex_unlock(&priv->reg_mutex);
  938. return 0;
  939. }
  940. int
  941. ar8xxx_sw_get_mirror_tx_enable(struct switch_dev *dev,
  942. const struct switch_attr *attr,
  943. struct switch_val *val)
  944. {
  945. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  946. val->value.i = priv->mirror_tx;
  947. return 0;
  948. }
  949. int
  950. ar8xxx_sw_set_mirror_monitor_port(struct switch_dev *dev,
  951. const struct switch_attr *attr,
  952. struct switch_val *val)
  953. {
  954. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  955. mutex_lock(&priv->reg_mutex);
  956. priv->monitor_port = val->value.i;
  957. priv->chip->set_mirror_regs(priv);
  958. mutex_unlock(&priv->reg_mutex);
  959. return 0;
  960. }
  961. int
  962. ar8xxx_sw_get_mirror_monitor_port(struct switch_dev *dev,
  963. const struct switch_attr *attr,
  964. struct switch_val *val)
  965. {
  966. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  967. val->value.i = priv->monitor_port;
  968. return 0;
  969. }
  970. int
  971. ar8xxx_sw_set_mirror_source_port(struct switch_dev *dev,
  972. const struct switch_attr *attr,
  973. struct switch_val *val)
  974. {
  975. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  976. mutex_lock(&priv->reg_mutex);
  977. priv->source_port = val->value.i;
  978. priv->chip->set_mirror_regs(priv);
  979. mutex_unlock(&priv->reg_mutex);
  980. return 0;
  981. }
  982. int
  983. ar8xxx_sw_get_mirror_source_port(struct switch_dev *dev,
  984. const struct switch_attr *attr,
  985. struct switch_val *val)
  986. {
  987. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  988. val->value.i = priv->source_port;
  989. return 0;
  990. }
  991. int
  992. ar8xxx_sw_set_port_reset_mib(struct switch_dev *dev,
  993. const struct switch_attr *attr,
  994. struct switch_val *val)
  995. {
  996. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  997. int port;
  998. int ret;
  999. if (!ar8xxx_has_mib_counters(priv))
  1000. return -EOPNOTSUPP;
  1001. port = val->port_vlan;
  1002. if (port >= dev->ports)
  1003. return -EINVAL;
  1004. mutex_lock(&priv->mib_lock);
  1005. ret = ar8xxx_mib_capture(priv);
  1006. if (ret)
  1007. goto unlock;
  1008. ar8xxx_mib_fetch_port_stat(priv, port, true);
  1009. ret = 0;
  1010. unlock:
  1011. mutex_unlock(&priv->mib_lock);
  1012. return ret;
  1013. }
  1014. int
  1015. ar8xxx_sw_get_port_mib(struct switch_dev *dev,
  1016. const struct switch_attr *attr,
  1017. struct switch_val *val)
  1018. {
  1019. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1020. const struct ar8xxx_chip *chip = priv->chip;
  1021. u64 *mib_stats;
  1022. int port;
  1023. int ret;
  1024. char *buf = priv->buf;
  1025. int i, len = 0;
  1026. if (!ar8xxx_has_mib_counters(priv))
  1027. return -EOPNOTSUPP;
  1028. port = val->port_vlan;
  1029. if (port >= dev->ports)
  1030. return -EINVAL;
  1031. mutex_lock(&priv->mib_lock);
  1032. ret = ar8xxx_mib_capture(priv);
  1033. if (ret)
  1034. goto unlock;
  1035. ar8xxx_mib_fetch_port_stat(priv, port, false);
  1036. len += snprintf(buf + len, sizeof(priv->buf) - len,
  1037. "Port %d MIB counters\n",
  1038. port);
  1039. mib_stats = &priv->mib_stats[port * chip->num_mibs];
  1040. for (i = 0; i < chip->num_mibs; i++)
  1041. len += snprintf(buf + len, sizeof(priv->buf) - len,
  1042. "%-12s: %llu\n",
  1043. chip->mib_decs[i].name,
  1044. mib_stats[i]);
  1045. val->value.s = buf;
  1046. val->len = len;
  1047. ret = 0;
  1048. unlock:
  1049. mutex_unlock(&priv->mib_lock);
  1050. return ret;
  1051. }
  1052. int
  1053. ar8xxx_sw_get_arl_table(struct switch_dev *dev,
  1054. const struct switch_attr *attr,
  1055. struct switch_val *val)
  1056. {
  1057. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1058. struct mii_bus *bus = priv->mii_bus;
  1059. const struct ar8xxx_chip *chip = priv->chip;
  1060. char *buf = priv->arl_buf;
  1061. int i, j, k, len = 0;
  1062. struct arl_entry *a, *a1;
  1063. u32 status;
  1064. if (!chip->get_arl_entry)
  1065. return -EOPNOTSUPP;
  1066. mutex_lock(&priv->reg_mutex);
  1067. mutex_lock(&bus->mdio_lock);
  1068. chip->get_arl_entry(priv, NULL, NULL, AR8XXX_ARL_INITIALIZE);
  1069. for(i = 0; i < AR8XXX_NUM_ARL_RECORDS; ++i) {
  1070. a = &priv->arl_table[i];
  1071. duplicate:
  1072. chip->get_arl_entry(priv, a, &status, AR8XXX_ARL_GET_NEXT);
  1073. if (!status)
  1074. break;
  1075. /* avoid duplicates
  1076. * ARL table can include multiple valid entries
  1077. * per MAC, just with differing status codes
  1078. */
  1079. for (j = 0; j < i; ++j) {
  1080. a1 = &priv->arl_table[j];
  1081. if (a->port == a1->port && !memcmp(a->mac, a1->mac, sizeof(a->mac)))
  1082. goto duplicate;
  1083. }
  1084. }
  1085. mutex_unlock(&bus->mdio_lock);
  1086. len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
  1087. "address resolution table\n");
  1088. if (i == AR8XXX_NUM_ARL_RECORDS)
  1089. len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
  1090. "Too many entries found, displaying the first %d only!\n",
  1091. AR8XXX_NUM_ARL_RECORDS);
  1092. for (j = 0; j < priv->dev.ports; ++j) {
  1093. for (k = 0; k < i; ++k) {
  1094. a = &priv->arl_table[k];
  1095. if (a->port != j)
  1096. continue;
  1097. len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
  1098. "Port %d: MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  1099. j,
  1100. a->mac[5], a->mac[4], a->mac[3],
  1101. a->mac[2], a->mac[1], a->mac[0]);
  1102. }
  1103. }
  1104. val->value.s = buf;
  1105. val->len = len;
  1106. mutex_unlock(&priv->reg_mutex);
  1107. return 0;
  1108. }
  1109. static const struct switch_attr ar8xxx_sw_attr_globals[] = {
  1110. {
  1111. .type = SWITCH_TYPE_INT,
  1112. .name = "enable_vlan",
  1113. .description = "Enable VLAN mode",
  1114. .set = ar8xxx_sw_set_vlan,
  1115. .get = ar8xxx_sw_get_vlan,
  1116. .max = 1
  1117. },
  1118. {
  1119. .type = SWITCH_TYPE_NOVAL,
  1120. .name = "reset_mibs",
  1121. .description = "Reset all MIB counters",
  1122. .set = ar8xxx_sw_set_reset_mibs,
  1123. },
  1124. {
  1125. .type = SWITCH_TYPE_INT,
  1126. .name = "enable_mirror_rx",
  1127. .description = "Enable mirroring of RX packets",
  1128. .set = ar8xxx_sw_set_mirror_rx_enable,
  1129. .get = ar8xxx_sw_get_mirror_rx_enable,
  1130. .max = 1
  1131. },
  1132. {
  1133. .type = SWITCH_TYPE_INT,
  1134. .name = "enable_mirror_tx",
  1135. .description = "Enable mirroring of TX packets",
  1136. .set = ar8xxx_sw_set_mirror_tx_enable,
  1137. .get = ar8xxx_sw_get_mirror_tx_enable,
  1138. .max = 1
  1139. },
  1140. {
  1141. .type = SWITCH_TYPE_INT,
  1142. .name = "mirror_monitor_port",
  1143. .description = "Mirror monitor port",
  1144. .set = ar8xxx_sw_set_mirror_monitor_port,
  1145. .get = ar8xxx_sw_get_mirror_monitor_port,
  1146. .max = AR8216_NUM_PORTS - 1
  1147. },
  1148. {
  1149. .type = SWITCH_TYPE_INT,
  1150. .name = "mirror_source_port",
  1151. .description = "Mirror source port",
  1152. .set = ar8xxx_sw_set_mirror_source_port,
  1153. .get = ar8xxx_sw_get_mirror_source_port,
  1154. .max = AR8216_NUM_PORTS - 1
  1155. },
  1156. {
  1157. .type = SWITCH_TYPE_STRING,
  1158. .name = "arl_table",
  1159. .description = "Get ARL table",
  1160. .set = NULL,
  1161. .get = ar8xxx_sw_get_arl_table,
  1162. },
  1163. };
  1164. const struct switch_attr ar8xxx_sw_attr_port[2] = {
  1165. {
  1166. .type = SWITCH_TYPE_NOVAL,
  1167. .name = "reset_mib",
  1168. .description = "Reset single port MIB counters",
  1169. .set = ar8xxx_sw_set_port_reset_mib,
  1170. },
  1171. {
  1172. .type = SWITCH_TYPE_STRING,
  1173. .name = "mib",
  1174. .description = "Get port's MIB counters",
  1175. .set = NULL,
  1176. .get = ar8xxx_sw_get_port_mib,
  1177. },
  1178. };
  1179. const struct switch_attr ar8xxx_sw_attr_vlan[1] = {
  1180. {
  1181. .type = SWITCH_TYPE_INT,
  1182. .name = "vid",
  1183. .description = "VLAN ID (0-4094)",
  1184. .set = ar8xxx_sw_set_vid,
  1185. .get = ar8xxx_sw_get_vid,
  1186. .max = 4094,
  1187. },
  1188. };
  1189. static const struct switch_dev_ops ar8xxx_sw_ops = {
  1190. .attr_global = {
  1191. .attr = ar8xxx_sw_attr_globals,
  1192. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_globals),
  1193. },
  1194. .attr_port = {
  1195. .attr = ar8xxx_sw_attr_port,
  1196. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
  1197. },
  1198. .attr_vlan = {
  1199. .attr = ar8xxx_sw_attr_vlan,
  1200. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  1201. },
  1202. .get_port_pvid = ar8xxx_sw_get_pvid,
  1203. .set_port_pvid = ar8xxx_sw_set_pvid,
  1204. .get_vlan_ports = ar8xxx_sw_get_ports,
  1205. .set_vlan_ports = ar8xxx_sw_set_ports,
  1206. .apply_config = ar8xxx_sw_hw_apply,
  1207. .reset_switch = ar8xxx_sw_reset_switch,
  1208. .get_port_link = ar8xxx_sw_get_port_link,
  1209. };
  1210. static const struct ar8xxx_chip ar8216_chip = {
  1211. .caps = AR8XXX_CAP_MIB_COUNTERS,
  1212. .reg_port_stats_start = 0x19000,
  1213. .reg_port_stats_length = 0xa0,
  1214. .name = "Atheros AR8216",
  1215. .ports = AR8216_NUM_PORTS,
  1216. .vlans = AR8216_NUM_VLANS,
  1217. .swops = &ar8xxx_sw_ops,
  1218. .hw_init = ar8216_hw_init,
  1219. .init_globals = ar8216_init_globals,
  1220. .init_port = ar8216_init_port,
  1221. .setup_port = ar8216_setup_port,
  1222. .read_port_status = ar8216_read_port_status,
  1223. .atu_flush = ar8216_atu_flush,
  1224. .vtu_flush = ar8216_vtu_flush,
  1225. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1226. .set_mirror_regs = ar8216_set_mirror_regs,
  1227. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1228. .num_mibs = ARRAY_SIZE(ar8216_mibs),
  1229. .mib_decs = ar8216_mibs,
  1230. .mib_func = AR8216_REG_MIB_FUNC
  1231. };
  1232. static const struct ar8xxx_chip ar8236_chip = {
  1233. .caps = AR8XXX_CAP_MIB_COUNTERS,
  1234. .reg_port_stats_start = 0x20000,
  1235. .reg_port_stats_length = 0x100,
  1236. .name = "Atheros AR8236",
  1237. .ports = AR8216_NUM_PORTS,
  1238. .vlans = AR8216_NUM_VLANS,
  1239. .swops = &ar8xxx_sw_ops,
  1240. .hw_init = ar8216_hw_init,
  1241. .init_globals = ar8236_init_globals,
  1242. .init_port = ar8216_init_port,
  1243. .setup_port = ar8236_setup_port,
  1244. .read_port_status = ar8216_read_port_status,
  1245. .atu_flush = ar8216_atu_flush,
  1246. .vtu_flush = ar8216_vtu_flush,
  1247. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1248. .set_mirror_regs = ar8216_set_mirror_regs,
  1249. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1250. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1251. .mib_decs = ar8236_mibs,
  1252. .mib_func = AR8216_REG_MIB_FUNC
  1253. };
  1254. static const struct ar8xxx_chip ar8316_chip = {
  1255. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1256. .reg_port_stats_start = 0x20000,
  1257. .reg_port_stats_length = 0x100,
  1258. .name = "Atheros AR8316",
  1259. .ports = AR8216_NUM_PORTS,
  1260. .vlans = AR8X16_MAX_VLANS,
  1261. .swops = &ar8xxx_sw_ops,
  1262. .hw_init = ar8316_hw_init,
  1263. .init_globals = ar8316_init_globals,
  1264. .init_port = ar8216_init_port,
  1265. .setup_port = ar8216_setup_port,
  1266. .read_port_status = ar8216_read_port_status,
  1267. .atu_flush = ar8216_atu_flush,
  1268. .vtu_flush = ar8216_vtu_flush,
  1269. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1270. .set_mirror_regs = ar8216_set_mirror_regs,
  1271. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1272. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1273. .mib_decs = ar8236_mibs,
  1274. .mib_func = AR8216_REG_MIB_FUNC
  1275. };
  1276. static int
  1277. ar8xxx_id_chip(struct ar8xxx_priv *priv)
  1278. {
  1279. u32 val;
  1280. u16 id;
  1281. int i;
  1282. val = ar8xxx_read(priv, AR8216_REG_CTRL);
  1283. if (val == ~0)
  1284. return -ENODEV;
  1285. id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  1286. for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
  1287. u16 t;
  1288. val = ar8xxx_read(priv, AR8216_REG_CTRL);
  1289. if (val == ~0)
  1290. return -ENODEV;
  1291. t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  1292. if (t != id)
  1293. return -ENODEV;
  1294. }
  1295. priv->chip_ver = (id & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
  1296. priv->chip_rev = (id & AR8216_CTRL_REVISION);
  1297. switch (priv->chip_ver) {
  1298. case AR8XXX_VER_AR8216:
  1299. priv->chip = &ar8216_chip;
  1300. break;
  1301. case AR8XXX_VER_AR8236:
  1302. priv->chip = &ar8236_chip;
  1303. break;
  1304. case AR8XXX_VER_AR8316:
  1305. priv->chip = &ar8316_chip;
  1306. break;
  1307. case AR8XXX_VER_AR8327:
  1308. priv->chip = &ar8327_chip;
  1309. break;
  1310. case AR8XXX_VER_AR8337:
  1311. priv->chip = &ar8337_chip;
  1312. break;
  1313. default:
  1314. pr_err("ar8216: Unknown Atheros device [ver=%d, rev=%d]\n",
  1315. priv->chip_ver, priv->chip_rev);
  1316. return -ENODEV;
  1317. }
  1318. return 0;
  1319. }
  1320. static void
  1321. ar8xxx_mib_work_func(struct work_struct *work)
  1322. {
  1323. struct ar8xxx_priv *priv;
  1324. int err;
  1325. priv = container_of(work, struct ar8xxx_priv, mib_work.work);
  1326. mutex_lock(&priv->mib_lock);
  1327. err = ar8xxx_mib_capture(priv);
  1328. if (err)
  1329. goto next_port;
  1330. ar8xxx_mib_fetch_port_stat(priv, priv->mib_next_port, false);
  1331. next_port:
  1332. priv->mib_next_port++;
  1333. if (priv->mib_next_port >= priv->dev.ports)
  1334. priv->mib_next_port = 0;
  1335. mutex_unlock(&priv->mib_lock);
  1336. schedule_delayed_work(&priv->mib_work,
  1337. msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
  1338. }
  1339. static int
  1340. ar8xxx_mib_init(struct ar8xxx_priv *priv)
  1341. {
  1342. unsigned int len;
  1343. if (!ar8xxx_has_mib_counters(priv))
  1344. return 0;
  1345. BUG_ON(!priv->chip->mib_decs || !priv->chip->num_mibs);
  1346. len = priv->dev.ports * priv->chip->num_mibs *
  1347. sizeof(*priv->mib_stats);
  1348. priv->mib_stats = kzalloc(len, GFP_KERNEL);
  1349. if (!priv->mib_stats)
  1350. return -ENOMEM;
  1351. return 0;
  1352. }
  1353. static void
  1354. ar8xxx_mib_start(struct ar8xxx_priv *priv)
  1355. {
  1356. if (!ar8xxx_has_mib_counters(priv))
  1357. return;
  1358. schedule_delayed_work(&priv->mib_work,
  1359. msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
  1360. }
  1361. static void
  1362. ar8xxx_mib_stop(struct ar8xxx_priv *priv)
  1363. {
  1364. if (!ar8xxx_has_mib_counters(priv))
  1365. return;
  1366. cancel_delayed_work(&priv->mib_work);
  1367. }
  1368. static struct ar8xxx_priv *
  1369. ar8xxx_create(void)
  1370. {
  1371. struct ar8xxx_priv *priv;
  1372. priv = kzalloc(sizeof(struct ar8xxx_priv), GFP_KERNEL);
  1373. if (priv == NULL)
  1374. return NULL;
  1375. mutex_init(&priv->reg_mutex);
  1376. mutex_init(&priv->mib_lock);
  1377. INIT_DELAYED_WORK(&priv->mib_work, ar8xxx_mib_work_func);
  1378. return priv;
  1379. }
  1380. static void
  1381. ar8xxx_free(struct ar8xxx_priv *priv)
  1382. {
  1383. if (priv->chip && priv->chip->cleanup)
  1384. priv->chip->cleanup(priv);
  1385. kfree(priv->chip_data);
  1386. kfree(priv->mib_stats);
  1387. kfree(priv);
  1388. }
  1389. static int
  1390. ar8xxx_probe_switch(struct ar8xxx_priv *priv)
  1391. {
  1392. const struct ar8xxx_chip *chip;
  1393. struct switch_dev *swdev;
  1394. int ret;
  1395. ret = ar8xxx_id_chip(priv);
  1396. if (ret)
  1397. return ret;
  1398. chip = priv->chip;
  1399. swdev = &priv->dev;
  1400. swdev->cpu_port = AR8216_PORT_CPU;
  1401. swdev->name = chip->name;
  1402. swdev->vlans = chip->vlans;
  1403. swdev->ports = chip->ports;
  1404. swdev->ops = chip->swops;
  1405. ret = ar8xxx_mib_init(priv);
  1406. if (ret)
  1407. return ret;
  1408. return 0;
  1409. }
  1410. static int
  1411. ar8xxx_start(struct ar8xxx_priv *priv)
  1412. {
  1413. int ret;
  1414. priv->init = true;
  1415. ret = priv->chip->hw_init(priv);
  1416. if (ret)
  1417. return ret;
  1418. ret = ar8xxx_sw_reset_switch(&priv->dev);
  1419. if (ret)
  1420. return ret;
  1421. priv->init = false;
  1422. ar8xxx_mib_start(priv);
  1423. return 0;
  1424. }
  1425. static int
  1426. ar8xxx_phy_config_init(struct phy_device *phydev)
  1427. {
  1428. struct ar8xxx_priv *priv = phydev->priv;
  1429. struct net_device *dev = phydev->attached_dev;
  1430. int ret;
  1431. if (WARN_ON(!priv))
  1432. return -ENODEV;
  1433. if (priv->chip->config_at_probe)
  1434. return ar8xxx_phy_check_aneg(phydev);
  1435. priv->phy = phydev;
  1436. if (phydev->addr != 0) {
  1437. if (chip_is_ar8316(priv)) {
  1438. /* switch device has been initialized, reinit */
  1439. priv->dev.ports = (AR8216_NUM_PORTS - 1);
  1440. priv->initialized = false;
  1441. priv->port4_phy = true;
  1442. ar8316_hw_init(priv);
  1443. return 0;
  1444. }
  1445. return 0;
  1446. }
  1447. ret = ar8xxx_start(priv);
  1448. if (ret)
  1449. return ret;
  1450. /* VID fixup only needed on ar8216 */
  1451. if (chip_is_ar8216(priv)) {
  1452. dev->phy_ptr = priv;
  1453. dev->priv_flags |= IFF_NO_IP_ALIGN;
  1454. dev->eth_mangle_rx = ar8216_mangle_rx;
  1455. dev->eth_mangle_tx = ar8216_mangle_tx;
  1456. }
  1457. return 0;
  1458. }
  1459. static bool
  1460. ar8xxx_check_link_states(struct ar8xxx_priv *priv)
  1461. {
  1462. bool link_new, changed = false;
  1463. u32 status;
  1464. int i;
  1465. mutex_lock(&priv->reg_mutex);
  1466. for (i = 0; i < priv->dev.ports; i++) {
  1467. status = priv->chip->read_port_status(priv, i);
  1468. link_new = !!(status & AR8216_PORT_STATUS_LINK_UP);
  1469. if (link_new == priv->link_up[i])
  1470. continue;
  1471. priv->link_up[i] = link_new;
  1472. changed = true;
  1473. dev_info(&priv->phy->dev, "Port %d is %s\n",
  1474. i, link_new ? "up" : "down");
  1475. }
  1476. if (changed)
  1477. priv->chip->atu_flush(priv);
  1478. mutex_unlock(&priv->reg_mutex);
  1479. return changed;
  1480. }
  1481. static int
  1482. ar8xxx_phy_read_status(struct phy_device *phydev)
  1483. {
  1484. struct ar8xxx_priv *priv = phydev->priv;
  1485. struct switch_port_link link;
  1486. /* check for link changes and flush ATU
  1487. * if a change was detected
  1488. */
  1489. if (phydev->state == PHY_CHANGELINK)
  1490. ar8xxx_check_link_states(priv);
  1491. if (phydev->addr != 0)
  1492. return genphy_read_status(phydev);
  1493. ar8216_read_port_link(priv, phydev->addr, &link);
  1494. phydev->link = !!link.link;
  1495. if (!phydev->link)
  1496. return 0;
  1497. switch (link.speed) {
  1498. case SWITCH_PORT_SPEED_10:
  1499. phydev->speed = SPEED_10;
  1500. break;
  1501. case SWITCH_PORT_SPEED_100:
  1502. phydev->speed = SPEED_100;
  1503. break;
  1504. case SWITCH_PORT_SPEED_1000:
  1505. phydev->speed = SPEED_1000;
  1506. break;
  1507. default:
  1508. phydev->speed = 0;
  1509. }
  1510. phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1511. phydev->state = PHY_RUNNING;
  1512. netif_carrier_on(phydev->attached_dev);
  1513. phydev->adjust_link(phydev->attached_dev);
  1514. return 0;
  1515. }
  1516. static int
  1517. ar8xxx_phy_config_aneg(struct phy_device *phydev)
  1518. {
  1519. if (phydev->addr == 0)
  1520. return 0;
  1521. return genphy_config_aneg(phydev);
  1522. }
  1523. static const u32 ar8xxx_phy_ids[] = {
  1524. 0x004dd033,
  1525. 0x004dd034, /* AR8327 */
  1526. 0x004dd036, /* AR8337 */
  1527. 0x004dd041,
  1528. 0x004dd042,
  1529. 0x004dd043, /* AR8236 */
  1530. };
  1531. static bool
  1532. ar8xxx_phy_match(u32 phy_id)
  1533. {
  1534. int i;
  1535. for (i = 0; i < ARRAY_SIZE(ar8xxx_phy_ids); i++)
  1536. if (phy_id == ar8xxx_phy_ids[i])
  1537. return true;
  1538. return false;
  1539. }
  1540. static bool
  1541. ar8xxx_is_possible(struct mii_bus *bus)
  1542. {
  1543. unsigned i;
  1544. for (i = 0; i < 4; i++) {
  1545. u32 phy_id;
  1546. phy_id = mdiobus_read(bus, i, MII_PHYSID1) << 16;
  1547. phy_id |= mdiobus_read(bus, i, MII_PHYSID2);
  1548. if (!ar8xxx_phy_match(phy_id)) {
  1549. pr_debug("ar8xxx: unknown PHY at %s:%02x id:%08x\n",
  1550. dev_name(&bus->dev), i, phy_id);
  1551. return false;
  1552. }
  1553. }
  1554. return true;
  1555. }
  1556. static int
  1557. ar8xxx_phy_probe(struct phy_device *phydev)
  1558. {
  1559. struct ar8xxx_priv *priv;
  1560. struct switch_dev *swdev;
  1561. int ret;
  1562. /* skip PHYs at unused adresses */
  1563. if (phydev->addr != 0 && phydev->addr != 4)
  1564. return -ENODEV;
  1565. if (!ar8xxx_is_possible(phydev->bus))
  1566. return -ENODEV;
  1567. mutex_lock(&ar8xxx_dev_list_lock);
  1568. list_for_each_entry(priv, &ar8xxx_dev_list, list)
  1569. if (priv->mii_bus == phydev->bus)
  1570. goto found;
  1571. priv = ar8xxx_create();
  1572. if (priv == NULL) {
  1573. ret = -ENOMEM;
  1574. goto unlock;
  1575. }
  1576. priv->mii_bus = phydev->bus;
  1577. ret = ar8xxx_probe_switch(priv);
  1578. if (ret)
  1579. goto free_priv;
  1580. swdev = &priv->dev;
  1581. swdev->alias = dev_name(&priv->mii_bus->dev);
  1582. ret = register_switch(swdev, NULL);
  1583. if (ret)
  1584. goto free_priv;
  1585. pr_info("%s: %s rev. %u switch registered on %s\n",
  1586. swdev->devname, swdev->name, priv->chip_rev,
  1587. dev_name(&priv->mii_bus->dev));
  1588. found:
  1589. priv->use_count++;
  1590. if (phydev->addr == 0) {
  1591. if (ar8xxx_has_gige(priv)) {
  1592. phydev->supported = SUPPORTED_1000baseT_Full;
  1593. phydev->advertising = ADVERTISED_1000baseT_Full;
  1594. } else {
  1595. phydev->supported = SUPPORTED_100baseT_Full;
  1596. phydev->advertising = ADVERTISED_100baseT_Full;
  1597. }
  1598. if (priv->chip->config_at_probe) {
  1599. priv->phy = phydev;
  1600. ret = ar8xxx_start(priv);
  1601. if (ret)
  1602. goto err_unregister_switch;
  1603. }
  1604. } else {
  1605. if (ar8xxx_has_gige(priv)) {
  1606. phydev->supported |= SUPPORTED_1000baseT_Full;
  1607. phydev->advertising |= ADVERTISED_1000baseT_Full;
  1608. }
  1609. }
  1610. phydev->priv = priv;
  1611. list_add(&priv->list, &ar8xxx_dev_list);
  1612. mutex_unlock(&ar8xxx_dev_list_lock);
  1613. return 0;
  1614. err_unregister_switch:
  1615. if (--priv->use_count)
  1616. goto unlock;
  1617. unregister_switch(&priv->dev);
  1618. free_priv:
  1619. ar8xxx_free(priv);
  1620. unlock:
  1621. mutex_unlock(&ar8xxx_dev_list_lock);
  1622. return ret;
  1623. }
  1624. static void
  1625. ar8xxx_phy_detach(struct phy_device *phydev)
  1626. {
  1627. struct net_device *dev = phydev->attached_dev;
  1628. if (!dev)
  1629. return;
  1630. dev->phy_ptr = NULL;
  1631. dev->priv_flags &= ~IFF_NO_IP_ALIGN;
  1632. dev->eth_mangle_rx = NULL;
  1633. dev->eth_mangle_tx = NULL;
  1634. }
  1635. static void
  1636. ar8xxx_phy_remove(struct phy_device *phydev)
  1637. {
  1638. struct ar8xxx_priv *priv = phydev->priv;
  1639. if (WARN_ON(!priv))
  1640. return;
  1641. phydev->priv = NULL;
  1642. if (--priv->use_count > 0)
  1643. return;
  1644. mutex_lock(&ar8xxx_dev_list_lock);
  1645. list_del(&priv->list);
  1646. mutex_unlock(&ar8xxx_dev_list_lock);
  1647. unregister_switch(&priv->dev);
  1648. ar8xxx_mib_stop(priv);
  1649. ar8xxx_free(priv);
  1650. }
  1651. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
  1652. static int
  1653. ar8xxx_phy_soft_reset(struct phy_device *phydev)
  1654. {
  1655. /* we don't need an extra reset */
  1656. return 0;
  1657. }
  1658. #endif
  1659. static struct phy_driver ar8xxx_phy_driver = {
  1660. .phy_id = 0x004d0000,
  1661. .name = "Atheros AR8216/AR8236/AR8316",
  1662. .phy_id_mask = 0xffff0000,
  1663. .features = PHY_BASIC_FEATURES,
  1664. .probe = ar8xxx_phy_probe,
  1665. .remove = ar8xxx_phy_remove,
  1666. .detach = ar8xxx_phy_detach,
  1667. .config_init = ar8xxx_phy_config_init,
  1668. .config_aneg = ar8xxx_phy_config_aneg,
  1669. .read_status = ar8xxx_phy_read_status,
  1670. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
  1671. .soft_reset = ar8xxx_phy_soft_reset,
  1672. #endif
  1673. .driver = { .owner = THIS_MODULE },
  1674. };
  1675. int __init
  1676. ar8xxx_init(void)
  1677. {
  1678. return phy_driver_register(&ar8xxx_phy_driver);
  1679. }
  1680. void __exit
  1681. ar8xxx_exit(void)
  1682. {
  1683. phy_driver_unregister(&ar8xxx_phy_driver);
  1684. }
  1685. module_init(ar8xxx_init);
  1686. module_exit(ar8xxx_exit);
  1687. MODULE_LICENSE("GPL");