ar8216.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919
  1. /*
  2. * ar8216.c: AR8216 switch driver
  3. *
  4. * Copyright (C) 2009 Felix Fietkau <[email protected]>
  5. * Copyright (C) 2011-2012 Gabor Juhos <[email protected]>
  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/of_device.h>
  26. #include <linux/of_mdio.h>
  27. #include <linux/of_net.h>
  28. #include <linux/bitops.h>
  29. #include <net/genetlink.h>
  30. #include <linux/switch.h>
  31. #include <linux/delay.h>
  32. #include <linux/phy.h>
  33. #include <linux/etherdevice.h>
  34. #include <linux/lockdep.h>
  35. #include <linux/ar8216_platform.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/version.h>
  38. #include "ar8216.h"
  39. extern const struct ar8xxx_chip ar8327_chip;
  40. extern const struct ar8xxx_chip ar8337_chip;
  41. #define MIB_DESC_BASIC(_s , _o, _n) \
  42. { \
  43. .size = (_s), \
  44. .offset = (_o), \
  45. .name = (_n), \
  46. .type = AR8XXX_MIB_BASIC, \
  47. }
  48. #define MIB_DESC_EXT(_s , _o, _n) \
  49. { \
  50. .size = (_s), \
  51. .offset = (_o), \
  52. .name = (_n), \
  53. .type = AR8XXX_MIB_EXTENDED, \
  54. }
  55. static const struct ar8xxx_mib_desc ar8216_mibs[] = {
  56. MIB_DESC_EXT(1, AR8216_STATS_RXBROAD, "RxBroad"),
  57. MIB_DESC_EXT(1, AR8216_STATS_RXPAUSE, "RxPause"),
  58. MIB_DESC_EXT(1, AR8216_STATS_RXMULTI, "RxMulti"),
  59. MIB_DESC_EXT(1, AR8216_STATS_RXFCSERR, "RxFcsErr"),
  60. MIB_DESC_EXT(1, AR8216_STATS_RXALIGNERR, "RxAlignErr"),
  61. MIB_DESC_EXT(1, AR8216_STATS_RXRUNT, "RxRunt"),
  62. MIB_DESC_EXT(1, AR8216_STATS_RXFRAGMENT, "RxFragment"),
  63. MIB_DESC_EXT(1, AR8216_STATS_RX64BYTE, "Rx64Byte"),
  64. MIB_DESC_EXT(1, AR8216_STATS_RX128BYTE, "Rx128Byte"),
  65. MIB_DESC_EXT(1, AR8216_STATS_RX256BYTE, "Rx256Byte"),
  66. MIB_DESC_EXT(1, AR8216_STATS_RX512BYTE, "Rx512Byte"),
  67. MIB_DESC_EXT(1, AR8216_STATS_RX1024BYTE, "Rx1024Byte"),
  68. MIB_DESC_EXT(1, AR8216_STATS_RXMAXBYTE, "RxMaxByte"),
  69. MIB_DESC_EXT(1, AR8216_STATS_RXTOOLONG, "RxTooLong"),
  70. MIB_DESC_BASIC(2, AR8216_STATS_RXGOODBYTE, "RxGoodByte"),
  71. MIB_DESC_EXT(2, AR8216_STATS_RXBADBYTE, "RxBadByte"),
  72. MIB_DESC_EXT(1, AR8216_STATS_RXOVERFLOW, "RxOverFlow"),
  73. MIB_DESC_EXT(1, AR8216_STATS_FILTERED, "Filtered"),
  74. MIB_DESC_EXT(1, AR8216_STATS_TXBROAD, "TxBroad"),
  75. MIB_DESC_EXT(1, AR8216_STATS_TXPAUSE, "TxPause"),
  76. MIB_DESC_EXT(1, AR8216_STATS_TXMULTI, "TxMulti"),
  77. MIB_DESC_EXT(1, AR8216_STATS_TXUNDERRUN, "TxUnderRun"),
  78. MIB_DESC_EXT(1, AR8216_STATS_TX64BYTE, "Tx64Byte"),
  79. MIB_DESC_EXT(1, AR8216_STATS_TX128BYTE, "Tx128Byte"),
  80. MIB_DESC_EXT(1, AR8216_STATS_TX256BYTE, "Tx256Byte"),
  81. MIB_DESC_EXT(1, AR8216_STATS_TX512BYTE, "Tx512Byte"),
  82. MIB_DESC_EXT(1, AR8216_STATS_TX1024BYTE, "Tx1024Byte"),
  83. MIB_DESC_EXT(1, AR8216_STATS_TXMAXBYTE, "TxMaxByte"),
  84. MIB_DESC_EXT(1, AR8216_STATS_TXOVERSIZE, "TxOverSize"),
  85. MIB_DESC_BASIC(2, AR8216_STATS_TXBYTE, "TxByte"),
  86. MIB_DESC_EXT(1, AR8216_STATS_TXCOLLISION, "TxCollision"),
  87. MIB_DESC_EXT(1, AR8216_STATS_TXABORTCOL, "TxAbortCol"),
  88. MIB_DESC_EXT(1, AR8216_STATS_TXMULTICOL, "TxMultiCol"),
  89. MIB_DESC_EXT(1, AR8216_STATS_TXSINGLECOL, "TxSingleCol"),
  90. MIB_DESC_EXT(1, AR8216_STATS_TXEXCDEFER, "TxExcDefer"),
  91. MIB_DESC_EXT(1, AR8216_STATS_TXDEFER, "TxDefer"),
  92. MIB_DESC_EXT(1, AR8216_STATS_TXLATECOL, "TxLateCol"),
  93. };
  94. const struct ar8xxx_mib_desc ar8236_mibs[39] = {
  95. MIB_DESC_EXT(1, AR8236_STATS_RXBROAD, "RxBroad"),
  96. MIB_DESC_EXT(1, AR8236_STATS_RXPAUSE, "RxPause"),
  97. MIB_DESC_EXT(1, AR8236_STATS_RXMULTI, "RxMulti"),
  98. MIB_DESC_EXT(1, AR8236_STATS_RXFCSERR, "RxFcsErr"),
  99. MIB_DESC_EXT(1, AR8236_STATS_RXALIGNERR, "RxAlignErr"),
  100. MIB_DESC_EXT(1, AR8236_STATS_RXRUNT, "RxRunt"),
  101. MIB_DESC_EXT(1, AR8236_STATS_RXFRAGMENT, "RxFragment"),
  102. MIB_DESC_EXT(1, AR8236_STATS_RX64BYTE, "Rx64Byte"),
  103. MIB_DESC_EXT(1, AR8236_STATS_RX128BYTE, "Rx128Byte"),
  104. MIB_DESC_EXT(1, AR8236_STATS_RX256BYTE, "Rx256Byte"),
  105. MIB_DESC_EXT(1, AR8236_STATS_RX512BYTE, "Rx512Byte"),
  106. MIB_DESC_EXT(1, AR8236_STATS_RX1024BYTE, "Rx1024Byte"),
  107. MIB_DESC_EXT(1, AR8236_STATS_RX1518BYTE, "Rx1518Byte"),
  108. MIB_DESC_EXT(1, AR8236_STATS_RXMAXBYTE, "RxMaxByte"),
  109. MIB_DESC_EXT(1, AR8236_STATS_RXTOOLONG, "RxTooLong"),
  110. MIB_DESC_BASIC(2, AR8236_STATS_RXGOODBYTE, "RxGoodByte"),
  111. MIB_DESC_EXT(2, AR8236_STATS_RXBADBYTE, "RxBadByte"),
  112. MIB_DESC_EXT(1, AR8236_STATS_RXOVERFLOW, "RxOverFlow"),
  113. MIB_DESC_EXT(1, AR8236_STATS_FILTERED, "Filtered"),
  114. MIB_DESC_EXT(1, AR8236_STATS_TXBROAD, "TxBroad"),
  115. MIB_DESC_EXT(1, AR8236_STATS_TXPAUSE, "TxPause"),
  116. MIB_DESC_EXT(1, AR8236_STATS_TXMULTI, "TxMulti"),
  117. MIB_DESC_EXT(1, AR8236_STATS_TXUNDERRUN, "TxUnderRun"),
  118. MIB_DESC_EXT(1, AR8236_STATS_TX64BYTE, "Tx64Byte"),
  119. MIB_DESC_EXT(1, AR8236_STATS_TX128BYTE, "Tx128Byte"),
  120. MIB_DESC_EXT(1, AR8236_STATS_TX256BYTE, "Tx256Byte"),
  121. MIB_DESC_EXT(1, AR8236_STATS_TX512BYTE, "Tx512Byte"),
  122. MIB_DESC_EXT(1, AR8236_STATS_TX1024BYTE, "Tx1024Byte"),
  123. MIB_DESC_EXT(1, AR8236_STATS_TX1518BYTE, "Tx1518Byte"),
  124. MIB_DESC_EXT(1, AR8236_STATS_TXMAXBYTE, "TxMaxByte"),
  125. MIB_DESC_EXT(1, AR8236_STATS_TXOVERSIZE, "TxOverSize"),
  126. MIB_DESC_BASIC(2, AR8236_STATS_TXBYTE, "TxByte"),
  127. MIB_DESC_EXT(1, AR8236_STATS_TXCOLLISION, "TxCollision"),
  128. MIB_DESC_EXT(1, AR8236_STATS_TXABORTCOL, "TxAbortCol"),
  129. MIB_DESC_EXT(1, AR8236_STATS_TXMULTICOL, "TxMultiCol"),
  130. MIB_DESC_EXT(1, AR8236_STATS_TXSINGLECOL, "TxSingleCol"),
  131. MIB_DESC_EXT(1, AR8236_STATS_TXEXCDEFER, "TxExcDefer"),
  132. MIB_DESC_EXT(1, AR8236_STATS_TXDEFER, "TxDefer"),
  133. MIB_DESC_EXT(1, AR8236_STATS_TXLATECOL, "TxLateCol"),
  134. };
  135. static DEFINE_MUTEX(ar8xxx_dev_list_lock);
  136. static LIST_HEAD(ar8xxx_dev_list);
  137. static void
  138. ar8xxx_mib_start(struct ar8xxx_priv *priv);
  139. static void
  140. ar8xxx_mib_stop(struct ar8xxx_priv *priv);
  141. /* inspired by phy_poll_reset in drivers/net/phy/phy_device.c */
  142. static int
  143. ar8xxx_phy_poll_reset(struct mii_bus *bus)
  144. {
  145. unsigned int sleep_msecs = 20;
  146. int ret, elapsed, i;
  147. for (elapsed = sleep_msecs; elapsed <= 600;
  148. elapsed += sleep_msecs) {
  149. msleep(sleep_msecs);
  150. for (i = 0; i < AR8XXX_NUM_PHYS; i++) {
  151. ret = mdiobus_read(bus, i, MII_BMCR);
  152. if (ret < 0)
  153. return ret;
  154. if (ret & BMCR_RESET)
  155. break;
  156. if (i == AR8XXX_NUM_PHYS - 1) {
  157. usleep_range(1000, 2000);
  158. return 0;
  159. }
  160. }
  161. }
  162. return -ETIMEDOUT;
  163. }
  164. static int
  165. ar8xxx_phy_check_aneg(struct phy_device *phydev)
  166. {
  167. int ret;
  168. if (phydev->autoneg != AUTONEG_ENABLE)
  169. return 0;
  170. /*
  171. * BMCR_ANENABLE might have been cleared
  172. * by phy_init_hw in certain kernel versions
  173. * therefore check for it
  174. */
  175. ret = phy_read(phydev, MII_BMCR);
  176. if (ret < 0)
  177. return ret;
  178. if (ret & BMCR_ANENABLE)
  179. return 0;
  180. dev_info(&phydev->mdio.dev, "ANEG disabled, re-enabling ...\n");
  181. ret |= BMCR_ANENABLE | BMCR_ANRESTART;
  182. return phy_write(phydev, MII_BMCR, ret);
  183. }
  184. void
  185. ar8xxx_phy_init(struct ar8xxx_priv *priv)
  186. {
  187. int i;
  188. struct mii_bus *bus;
  189. bus = priv->sw_mii_bus ?: priv->mii_bus;
  190. for (i = 0; i < AR8XXX_NUM_PHYS; i++) {
  191. if (priv->chip->phy_fixup)
  192. priv->chip->phy_fixup(priv, i);
  193. /* initialize the port itself */
  194. mdiobus_write(bus, i, MII_ADVERTISE,
  195. ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  196. if (ar8xxx_has_gige(priv))
  197. mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
  198. mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  199. }
  200. ar8xxx_phy_poll_reset(bus);
  201. }
  202. u32
  203. ar8xxx_mii_read32(struct ar8xxx_priv *priv, int phy_id, int regnum)
  204. {
  205. struct mii_bus *bus = priv->mii_bus;
  206. u16 lo, hi;
  207. lo = bus->read(bus, phy_id, regnum);
  208. hi = bus->read(bus, phy_id, regnum + 1);
  209. return (hi << 16) | lo;
  210. }
  211. void
  212. ar8xxx_mii_write32(struct ar8xxx_priv *priv, int phy_id, int regnum, u32 val)
  213. {
  214. struct mii_bus *bus = priv->mii_bus;
  215. u16 lo, hi;
  216. lo = val & 0xffff;
  217. hi = (u16) (val >> 16);
  218. if (priv->chip->mii_lo_first)
  219. {
  220. bus->write(bus, phy_id, regnum, lo);
  221. bus->write(bus, phy_id, regnum + 1, hi);
  222. } else {
  223. bus->write(bus, phy_id, regnum + 1, hi);
  224. bus->write(bus, phy_id, regnum, lo);
  225. }
  226. }
  227. u32
  228. ar8xxx_read(struct ar8xxx_priv *priv, int reg)
  229. {
  230. struct mii_bus *bus = priv->mii_bus;
  231. u16 r1, r2, page;
  232. u32 val;
  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. val = ar8xxx_mii_read32(priv, 0x10 | r2, r1);
  238. mutex_unlock(&bus->mdio_lock);
  239. return val;
  240. }
  241. void
  242. ar8xxx_write(struct ar8xxx_priv *priv, int reg, u32 val)
  243. {
  244. struct mii_bus *bus = priv->mii_bus;
  245. u16 r1, r2, page;
  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. ar8xxx_mii_write32(priv, 0x10 | r2, r1, val);
  251. mutex_unlock(&bus->mdio_lock);
  252. }
  253. u32
  254. ar8xxx_rmw(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  255. {
  256. struct mii_bus *bus = priv->mii_bus;
  257. u16 r1, r2, page;
  258. u32 ret;
  259. split_addr((u32) reg, &r1, &r2, &page);
  260. mutex_lock(&bus->mdio_lock);
  261. bus->write(bus, 0x18, 0, page);
  262. wait_for_page_switch();
  263. ret = ar8xxx_mii_read32(priv, 0x10 | r2, r1);
  264. ret &= ~mask;
  265. ret |= val;
  266. ar8xxx_mii_write32(priv, 0x10 | r2, r1, ret);
  267. mutex_unlock(&bus->mdio_lock);
  268. return ret;
  269. }
  270. void
  271. ar8xxx_phy_dbg_read(struct ar8xxx_priv *priv, int phy_addr,
  272. u16 dbg_addr, u16 *dbg_data)
  273. {
  274. struct mii_bus *bus = priv->mii_bus;
  275. mutex_lock(&bus->mdio_lock);
  276. bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
  277. *dbg_data = bus->read(bus, phy_addr, MII_ATH_DBG_DATA);
  278. mutex_unlock(&bus->mdio_lock);
  279. }
  280. void
  281. ar8xxx_phy_dbg_write(struct ar8xxx_priv *priv, int phy_addr,
  282. u16 dbg_addr, u16 dbg_data)
  283. {
  284. struct mii_bus *bus = priv->mii_bus;
  285. mutex_lock(&bus->mdio_lock);
  286. bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
  287. bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
  288. mutex_unlock(&bus->mdio_lock);
  289. }
  290. static inline void
  291. ar8xxx_phy_mmd_prep(struct mii_bus *bus, int phy_addr, u16 addr, u16 reg)
  292. {
  293. bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr);
  294. bus->write(bus, phy_addr, MII_ATH_MMD_DATA, reg);
  295. bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr | 0x4000);
  296. }
  297. void
  298. ar8xxx_phy_mmd_write(struct ar8xxx_priv *priv, int phy_addr, u16 addr, u16 reg, u16 data)
  299. {
  300. struct mii_bus *bus = priv->mii_bus;
  301. mutex_lock(&bus->mdio_lock);
  302. ar8xxx_phy_mmd_prep(bus, phy_addr, addr, reg);
  303. bus->write(bus, phy_addr, MII_ATH_MMD_DATA, data);
  304. mutex_unlock(&bus->mdio_lock);
  305. }
  306. u16
  307. ar8xxx_phy_mmd_read(struct ar8xxx_priv *priv, int phy_addr, u16 addr, u16 reg)
  308. {
  309. struct mii_bus *bus = priv->mii_bus;
  310. u16 data;
  311. mutex_lock(&bus->mdio_lock);
  312. ar8xxx_phy_mmd_prep(bus, phy_addr, addr, reg);
  313. data = bus->read(bus, phy_addr, MII_ATH_MMD_DATA);
  314. mutex_unlock(&bus->mdio_lock);
  315. return data;
  316. }
  317. static int
  318. ar8xxx_reg_wait(struct ar8xxx_priv *priv, u32 reg, u32 mask, u32 val,
  319. unsigned timeout)
  320. {
  321. int i;
  322. for (i = 0; i < timeout; i++) {
  323. u32 t;
  324. t = ar8xxx_read(priv, reg);
  325. if ((t & mask) == val)
  326. return 0;
  327. usleep_range(1000, 2000);
  328. cond_resched();
  329. }
  330. return -ETIMEDOUT;
  331. }
  332. static int
  333. ar8xxx_mib_op(struct ar8xxx_priv *priv, u32 op)
  334. {
  335. unsigned mib_func = priv->chip->mib_func;
  336. int ret;
  337. lockdep_assert_held(&priv->mib_lock);
  338. /* Capture the hardware statistics for all ports */
  339. ar8xxx_rmw(priv, mib_func, AR8216_MIB_FUNC, (op << AR8216_MIB_FUNC_S));
  340. /* Wait for the capturing to complete. */
  341. ret = ar8xxx_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
  342. if (ret)
  343. goto out;
  344. ret = 0;
  345. out:
  346. return ret;
  347. }
  348. static int
  349. ar8xxx_mib_capture(struct ar8xxx_priv *priv)
  350. {
  351. return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_CAPTURE);
  352. }
  353. static int
  354. ar8xxx_mib_flush(struct ar8xxx_priv *priv)
  355. {
  356. return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_FLUSH);
  357. }
  358. static void
  359. ar8xxx_mib_fetch_port_stat(struct ar8xxx_priv *priv, int port, bool flush)
  360. {
  361. unsigned int base;
  362. u64 *mib_stats;
  363. int i;
  364. WARN_ON(port >= priv->dev.ports);
  365. lockdep_assert_held(&priv->mib_lock);
  366. base = priv->chip->reg_port_stats_start +
  367. priv->chip->reg_port_stats_length * port;
  368. mib_stats = &priv->mib_stats[port * priv->chip->num_mibs];
  369. for (i = 0; i < priv->chip->num_mibs; i++) {
  370. const struct ar8xxx_mib_desc *mib;
  371. u64 t;
  372. mib = &priv->chip->mib_decs[i];
  373. if (mib->type > priv->mib_type)
  374. continue;
  375. t = ar8xxx_read(priv, base + mib->offset);
  376. if (mib->size == 2) {
  377. u64 hi;
  378. hi = ar8xxx_read(priv, base + mib->offset + 4);
  379. t |= hi << 32;
  380. }
  381. if (flush)
  382. mib_stats[i] = 0;
  383. else
  384. mib_stats[i] += t;
  385. cond_resched();
  386. }
  387. }
  388. static void
  389. ar8216_read_port_link(struct ar8xxx_priv *priv, int port,
  390. struct switch_port_link *link)
  391. {
  392. u32 status;
  393. u32 speed;
  394. memset(link, '\0', sizeof(*link));
  395. status = priv->chip->read_port_status(priv, port);
  396. link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
  397. if (link->aneg) {
  398. link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
  399. } else {
  400. link->link = true;
  401. if (priv->get_port_link) {
  402. int err;
  403. err = priv->get_port_link(port);
  404. if (err >= 0)
  405. link->link = !!err;
  406. }
  407. }
  408. if (!link->link)
  409. return;
  410. link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
  411. link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
  412. link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
  413. if (link->aneg && link->duplex && priv->chip->read_port_eee_status)
  414. link->eee = priv->chip->read_port_eee_status(priv, port);
  415. speed = (status & AR8216_PORT_STATUS_SPEED) >>
  416. AR8216_PORT_STATUS_SPEED_S;
  417. switch (speed) {
  418. case AR8216_PORT_SPEED_10M:
  419. link->speed = SWITCH_PORT_SPEED_10;
  420. break;
  421. case AR8216_PORT_SPEED_100M:
  422. link->speed = SWITCH_PORT_SPEED_100;
  423. break;
  424. case AR8216_PORT_SPEED_1000M:
  425. link->speed = SWITCH_PORT_SPEED_1000;
  426. break;
  427. default:
  428. link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  429. break;
  430. }
  431. }
  432. #ifdef CONFIG_ETHERNET_PACKET_MANGLE
  433. static struct sk_buff *
  434. ar8216_mangle_tx(struct net_device *dev, struct sk_buff *skb)
  435. {
  436. struct ar8xxx_priv *priv = dev->phy_ptr;
  437. unsigned char *buf;
  438. if (unlikely(!priv))
  439. goto error;
  440. if (!priv->vlan)
  441. goto send;
  442. if (unlikely(skb_headroom(skb) < 2)) {
  443. if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
  444. goto error;
  445. }
  446. buf = skb_push(skb, 2);
  447. buf[0] = 0x10;
  448. buf[1] = 0x80;
  449. send:
  450. return skb;
  451. error:
  452. dev_kfree_skb_any(skb);
  453. return NULL;
  454. }
  455. static void
  456. ar8216_mangle_rx(struct net_device *dev, struct sk_buff *skb)
  457. {
  458. struct ar8xxx_priv *priv;
  459. unsigned char *buf;
  460. int port, vlan;
  461. priv = dev->phy_ptr;
  462. if (!priv)
  463. return;
  464. /* don't strip the header if vlan mode is disabled */
  465. if (!priv->vlan)
  466. return;
  467. /* strip header, get vlan id */
  468. buf = skb->data;
  469. skb_pull(skb, 2);
  470. /* check for vlan header presence */
  471. if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
  472. return;
  473. port = buf[0] & 0x7;
  474. /* no need to fix up packets coming from a tagged source */
  475. if (priv->vlan_tagged & (1 << port))
  476. return;
  477. /* lookup port vid from local table, the switch passes an invalid vlan id */
  478. vlan = priv->vlan_id[priv->pvid[port]];
  479. buf[14 + 2] &= 0xf0;
  480. buf[14 + 2] |= vlan >> 8;
  481. buf[15 + 2] = vlan & 0xff;
  482. }
  483. #endif
  484. int
  485. ar8216_wait_bit(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  486. {
  487. int timeout = 20;
  488. u32 t = 0;
  489. while (1) {
  490. t = ar8xxx_read(priv, reg);
  491. if ((t & mask) == val)
  492. return 0;
  493. if (timeout-- <= 0)
  494. break;
  495. udelay(10);
  496. cond_resched();
  497. }
  498. pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
  499. (unsigned int) reg, t, mask, val);
  500. return -ETIMEDOUT;
  501. }
  502. static void
  503. ar8216_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
  504. {
  505. if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
  506. return;
  507. if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
  508. val &= AR8216_VTUDATA_MEMBER;
  509. val |= AR8216_VTUDATA_VALID;
  510. ar8xxx_write(priv, AR8216_REG_VTU_DATA, val);
  511. }
  512. op |= AR8216_VTU_ACTIVE;
  513. ar8xxx_write(priv, AR8216_REG_VTU, op);
  514. }
  515. static void
  516. ar8216_vtu_flush(struct ar8xxx_priv *priv)
  517. {
  518. ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
  519. }
  520. static void
  521. ar8216_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
  522. {
  523. u32 op;
  524. op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
  525. ar8216_vtu_op(priv, op, port_mask);
  526. }
  527. static int
  528. ar8216_atu_flush(struct ar8xxx_priv *priv)
  529. {
  530. int ret;
  531. ret = ar8216_wait_bit(priv, AR8216_REG_ATU_FUNC0, AR8216_ATU_ACTIVE, 0);
  532. if (!ret)
  533. ar8xxx_write(priv, AR8216_REG_ATU_FUNC0, AR8216_ATU_OP_FLUSH |
  534. AR8216_ATU_ACTIVE);
  535. return ret;
  536. }
  537. static int
  538. ar8216_atu_flush_port(struct ar8xxx_priv *priv, int port)
  539. {
  540. u32 t;
  541. int ret;
  542. ret = ar8216_wait_bit(priv, AR8216_REG_ATU_FUNC0, AR8216_ATU_ACTIVE, 0);
  543. if (!ret) {
  544. t = (port << AR8216_ATU_PORT_NUM_S) | AR8216_ATU_OP_FLUSH_PORT;
  545. t |= AR8216_ATU_ACTIVE;
  546. ar8xxx_write(priv, AR8216_REG_ATU_FUNC0, t);
  547. }
  548. return ret;
  549. }
  550. static u32
  551. ar8216_read_port_status(struct ar8xxx_priv *priv, int port)
  552. {
  553. return ar8xxx_read(priv, AR8216_REG_PORT_STATUS(port));
  554. }
  555. static void
  556. __ar8216_setup_port(struct ar8xxx_priv *priv, int port, u32 members,
  557. bool ath_hdr_en)
  558. {
  559. u32 header;
  560. u32 egress, ingress;
  561. u32 pvid;
  562. if (priv->vlan) {
  563. pvid = priv->vlan_id[priv->pvid[port]];
  564. if (priv->vlan_tagged & (1 << port))
  565. egress = AR8216_OUT_ADD_VLAN;
  566. else
  567. egress = AR8216_OUT_STRIP_VLAN;
  568. ingress = AR8216_IN_SECURE;
  569. } else {
  570. pvid = port;
  571. egress = AR8216_OUT_KEEP;
  572. ingress = AR8216_IN_PORT_ONLY;
  573. }
  574. header = ath_hdr_en ? AR8216_PORT_CTRL_HEADER : 0;
  575. ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  576. AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  577. AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  578. AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  579. AR8216_PORT_CTRL_LEARN | header |
  580. (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  581. (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  582. ar8xxx_rmw(priv, AR8216_REG_PORT_VLAN(port),
  583. AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
  584. AR8216_PORT_VLAN_DEFAULT_ID,
  585. (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
  586. (ingress << AR8216_PORT_VLAN_MODE_S) |
  587. (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
  588. }
  589. static void
  590. ar8216_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
  591. {
  592. return __ar8216_setup_port(priv, port, members,
  593. chip_is_ar8216(priv) && priv->vlan &&
  594. port == AR8216_PORT_CPU);
  595. }
  596. static int
  597. ar8216_hw_init(struct ar8xxx_priv *priv)
  598. {
  599. if (priv->initialized)
  600. return 0;
  601. ar8xxx_write(priv, AR8216_REG_CTRL, AR8216_CTRL_RESET);
  602. ar8xxx_reg_wait(priv, AR8216_REG_CTRL, AR8216_CTRL_RESET, 0, 1000);
  603. ar8xxx_phy_init(priv);
  604. priv->initialized = true;
  605. return 0;
  606. }
  607. static void
  608. ar8216_init_globals(struct ar8xxx_priv *priv)
  609. {
  610. /* standard atheros magic */
  611. ar8xxx_write(priv, 0x38, 0xc000050e);
  612. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  613. AR8216_GCTRL_MTU, 1518 + 8 + 2);
  614. }
  615. static void
  616. __ar8216_init_port(struct ar8xxx_priv *priv, int port,
  617. bool cpu_ge, bool flow_en)
  618. {
  619. /* Enable port learning and tx */
  620. ar8xxx_write(priv, AR8216_REG_PORT_CTRL(port),
  621. AR8216_PORT_CTRL_LEARN |
  622. (4 << AR8216_PORT_CTRL_STATE_S));
  623. ar8xxx_write(priv, AR8216_REG_PORT_VLAN(port), 0);
  624. if (port == AR8216_PORT_CPU) {
  625. ar8xxx_write(priv, AR8216_REG_PORT_STATUS(port),
  626. AR8216_PORT_STATUS_LINK_UP |
  627. (cpu_ge ? AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
  628. AR8216_PORT_STATUS_TXMAC |
  629. AR8216_PORT_STATUS_RXMAC |
  630. (flow_en ? AR8216_PORT_STATUS_RXFLOW : 0) |
  631. (flow_en ? AR8216_PORT_STATUS_TXFLOW : 0) |
  632. AR8216_PORT_STATUS_DUPLEX);
  633. } else {
  634. ar8xxx_write(priv, AR8216_REG_PORT_STATUS(port),
  635. AR8216_PORT_STATUS_LINK_AUTO);
  636. }
  637. }
  638. static void
  639. ar8216_init_port(struct ar8xxx_priv *priv, int port)
  640. {
  641. __ar8216_init_port(priv, port, ar8xxx_has_gige(priv),
  642. chip_is_ar8316(priv));
  643. }
  644. static void
  645. ar8216_wait_atu_ready(struct ar8xxx_priv *priv, u16 r2, u16 r1)
  646. {
  647. int timeout = 20;
  648. while (ar8xxx_mii_read32(priv, r2, r1) & AR8216_ATU_ACTIVE && --timeout) {
  649. udelay(10);
  650. cond_resched();
  651. }
  652. if (!timeout)
  653. pr_err("ar8216: timeout waiting for atu to become ready\n");
  654. }
  655. static void ar8216_get_arl_entry(struct ar8xxx_priv *priv,
  656. struct arl_entry *a, u32 *status, enum arl_op op)
  657. {
  658. struct mii_bus *bus = priv->mii_bus;
  659. u16 r2, page;
  660. u16 r1_func0, r1_func1, r1_func2;
  661. u32 t, val0, val1, val2;
  662. split_addr(AR8216_REG_ATU_FUNC0, &r1_func0, &r2, &page);
  663. r2 |= 0x10;
  664. r1_func1 = (AR8216_REG_ATU_FUNC1 >> 1) & 0x1e;
  665. r1_func2 = (AR8216_REG_ATU_FUNC2 >> 1) & 0x1e;
  666. switch (op) {
  667. case AR8XXX_ARL_INITIALIZE:
  668. /* all ATU registers are on the same page
  669. * therefore set page only once
  670. */
  671. bus->write(bus, 0x18, 0, page);
  672. wait_for_page_switch();
  673. ar8216_wait_atu_ready(priv, r2, r1_func0);
  674. ar8xxx_mii_write32(priv, r2, r1_func0, AR8216_ATU_OP_GET_NEXT);
  675. ar8xxx_mii_write32(priv, r2, r1_func1, 0);
  676. ar8xxx_mii_write32(priv, r2, r1_func2, 0);
  677. break;
  678. case AR8XXX_ARL_GET_NEXT:
  679. t = ar8xxx_mii_read32(priv, r2, r1_func0);
  680. t |= AR8216_ATU_ACTIVE;
  681. ar8xxx_mii_write32(priv, r2, r1_func0, t);
  682. ar8216_wait_atu_ready(priv, r2, r1_func0);
  683. val0 = ar8xxx_mii_read32(priv, r2, r1_func0);
  684. val1 = ar8xxx_mii_read32(priv, r2, r1_func1);
  685. val2 = ar8xxx_mii_read32(priv, r2, r1_func2);
  686. *status = (val2 & AR8216_ATU_STATUS) >> AR8216_ATU_STATUS_S;
  687. if (!*status)
  688. break;
  689. a->portmap = (val2 & AR8216_ATU_PORTS) >> AR8216_ATU_PORTS_S;
  690. a->mac[0] = (val0 & AR8216_ATU_ADDR5) >> AR8216_ATU_ADDR5_S;
  691. a->mac[1] = (val0 & AR8216_ATU_ADDR4) >> AR8216_ATU_ADDR4_S;
  692. a->mac[2] = (val1 & AR8216_ATU_ADDR3) >> AR8216_ATU_ADDR3_S;
  693. a->mac[3] = (val1 & AR8216_ATU_ADDR2) >> AR8216_ATU_ADDR2_S;
  694. a->mac[4] = (val1 & AR8216_ATU_ADDR1) >> AR8216_ATU_ADDR1_S;
  695. a->mac[5] = (val1 & AR8216_ATU_ADDR0) >> AR8216_ATU_ADDR0_S;
  696. break;
  697. }
  698. }
  699. static int
  700. ar8216_phy_read(struct ar8xxx_priv *priv, int addr, int regnum)
  701. {
  702. u32 t, val = 0xffff;
  703. int err;
  704. if (addr >= AR8216_NUM_PORTS)
  705. return 0xffff;
  706. t = (regnum << AR8216_MDIO_CTRL_REG_ADDR_S) |
  707. (addr << AR8216_MDIO_CTRL_PHY_ADDR_S) |
  708. AR8216_MDIO_CTRL_MASTER_EN |
  709. AR8216_MDIO_CTRL_BUSY |
  710. AR8216_MDIO_CTRL_CMD_READ;
  711. ar8xxx_write(priv, AR8216_REG_MDIO_CTRL, t);
  712. err = ar8xxx_reg_wait(priv, AR8216_REG_MDIO_CTRL,
  713. AR8216_MDIO_CTRL_BUSY, 0, 5);
  714. if (!err)
  715. val = ar8xxx_read(priv, AR8216_REG_MDIO_CTRL);
  716. return val & AR8216_MDIO_CTRL_DATA_M;
  717. }
  718. static int
  719. ar8216_phy_write(struct ar8xxx_priv *priv, int addr, int regnum, u16 val)
  720. {
  721. u32 t;
  722. int ret;
  723. if (addr >= AR8216_NUM_PORTS)
  724. return -EINVAL;
  725. t = (addr << AR8216_MDIO_CTRL_PHY_ADDR_S) |
  726. (regnum << AR8216_MDIO_CTRL_REG_ADDR_S) |
  727. AR8216_MDIO_CTRL_MASTER_EN |
  728. AR8216_MDIO_CTRL_BUSY |
  729. AR8216_MDIO_CTRL_CMD_WRITE |
  730. val;
  731. ar8xxx_write(priv, AR8216_REG_MDIO_CTRL, t);
  732. ret = ar8xxx_reg_wait(priv, AR8216_REG_MDIO_CTRL,
  733. AR8216_MDIO_CTRL_BUSY, 0, 5);
  734. return ret;
  735. }
  736. static int
  737. ar8229_hw_init(struct ar8xxx_priv *priv)
  738. {
  739. phy_interface_t phy_if_mode;
  740. if (priv->initialized)
  741. return 0;
  742. ar8xxx_write(priv, AR8216_REG_CTRL, AR8216_CTRL_RESET);
  743. ar8xxx_reg_wait(priv, AR8216_REG_CTRL, AR8216_CTRL_RESET, 0, 1000);
  744. of_get_phy_mode(priv->pdev->of_node, &phy_if_mode);
  745. if (phy_if_mode == PHY_INTERFACE_MODE_GMII) {
  746. ar8xxx_write(priv, AR8229_REG_OPER_MODE0,
  747. AR8229_OPER_MODE0_MAC_GMII_EN);
  748. } else if (phy_if_mode == PHY_INTERFACE_MODE_MII) {
  749. ar8xxx_write(priv, AR8229_REG_OPER_MODE0,
  750. AR8229_OPER_MODE0_PHY_MII_EN);
  751. } else {
  752. pr_err("ar8229: unsupported mii mode\n");
  753. return -EINVAL;
  754. }
  755. if (priv->port4_phy) {
  756. ar8xxx_write(priv, AR8229_REG_OPER_MODE1,
  757. AR8229_REG_OPER_MODE1_PHY4_MII_EN);
  758. /* disable port5 to prevent mii conflict */
  759. ar8xxx_write(priv, AR8216_REG_PORT_STATUS(5), 0);
  760. }
  761. ar8xxx_phy_init(priv);
  762. priv->initialized = true;
  763. return 0;
  764. }
  765. static void
  766. ar8229_init_globals(struct ar8xxx_priv *priv)
  767. {
  768. /* Enable CPU port, and disable mirror port */
  769. ar8xxx_write(priv, AR8216_REG_GLOBAL_CPUPORT,
  770. AR8216_GLOBAL_CPUPORT_EN |
  771. (15 << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  772. /* Setup TAG priority mapping */
  773. ar8xxx_write(priv, AR8216_REG_TAG_PRIORITY, 0xfa50);
  774. /* Enable aging, MAC replacing */
  775. ar8xxx_write(priv, AR8216_REG_ATU_CTRL,
  776. 0x2b /* 5 min age time */ |
  777. AR8216_ATU_CTRL_AGE_EN |
  778. AR8216_ATU_CTRL_LEARN_CHANGE);
  779. /* Enable ARP frame acknowledge */
  780. ar8xxx_reg_set(priv, AR8229_REG_QM_CTRL,
  781. AR8229_QM_CTRL_ARP_EN);
  782. /*
  783. * Enable Broadcast/unknown multicast and unicast frames
  784. * transmitted to the CPU port.
  785. */
  786. ar8xxx_reg_set(priv, AR8216_REG_FLOOD_MASK,
  787. AR8229_FLOOD_MASK_BC_DP(0) |
  788. AR8229_FLOOD_MASK_MC_DP(0) |
  789. AR8229_FLOOD_MASK_UC_DP(0));
  790. /* setup MTU */
  791. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  792. AR8236_GCTRL_MTU, AR8236_GCTRL_MTU);
  793. /* Enable MIB counters */
  794. ar8xxx_reg_set(priv, AR8216_REG_MIB_FUNC,
  795. AR8236_MIB_EN);
  796. /* setup Service TAG */
  797. ar8xxx_rmw(priv, AR8216_REG_SERVICE_TAG, AR8216_SERVICE_TAG_M, 0);
  798. }
  799. static void
  800. ar8229_init_port(struct ar8xxx_priv *priv, int port)
  801. {
  802. __ar8216_init_port(priv, port, true, true);
  803. }
  804. static int
  805. ar7240sw_hw_init(struct ar8xxx_priv *priv)
  806. {
  807. if (priv->initialized)
  808. return 0;
  809. ar8xxx_write(priv, AR8216_REG_CTRL, AR8216_CTRL_RESET);
  810. ar8xxx_reg_wait(priv, AR8216_REG_CTRL, AR8216_CTRL_RESET, 0, 1000);
  811. priv->port4_phy = 1;
  812. /* disable port5 to prevent mii conflict */
  813. ar8xxx_write(priv, AR8216_REG_PORT_STATUS(5), 0);
  814. ar8xxx_phy_init(priv);
  815. priv->initialized = true;
  816. return 0;
  817. }
  818. static void
  819. ar7240sw_init_globals(struct ar8xxx_priv *priv)
  820. {
  821. /* Enable CPU port, and disable mirror port */
  822. ar8xxx_write(priv, AR8216_REG_GLOBAL_CPUPORT,
  823. AR8216_GLOBAL_CPUPORT_EN |
  824. (15 << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  825. /* Setup TAG priority mapping */
  826. ar8xxx_write(priv, AR8216_REG_TAG_PRIORITY, 0xfa50);
  827. /* Enable ARP frame acknowledge, aging, MAC replacing */
  828. ar8xxx_write(priv, AR8216_REG_ATU_CTRL,
  829. AR8216_ATU_CTRL_RESERVED |
  830. 0x2b /* 5 min age time */ |
  831. AR8216_ATU_CTRL_AGE_EN |
  832. AR8216_ATU_CTRL_ARP_EN |
  833. AR8216_ATU_CTRL_LEARN_CHANGE);
  834. /* Enable Broadcast frames transmitted to the CPU */
  835. ar8xxx_reg_set(priv, AR8216_REG_FLOOD_MASK,
  836. AR8216_FM_CPU_BROADCAST_EN);
  837. /* setup MTU */
  838. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  839. AR8216_GCTRL_MTU,
  840. AR8216_GCTRL_MTU);
  841. /* setup Service TAG */
  842. ar8xxx_rmw(priv, AR8216_REG_SERVICE_TAG, AR8216_SERVICE_TAG_M, 0);
  843. }
  844. static void
  845. ar7240sw_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
  846. {
  847. return __ar8216_setup_port(priv, port, members, false);
  848. }
  849. static void
  850. ar8236_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
  851. {
  852. u32 egress, ingress;
  853. u32 pvid;
  854. if (priv->vlan) {
  855. pvid = priv->vlan_id[priv->pvid[port]];
  856. if (priv->vlan_tagged & (1 << port))
  857. egress = AR8216_OUT_ADD_VLAN;
  858. else
  859. egress = AR8216_OUT_STRIP_VLAN;
  860. ingress = AR8216_IN_SECURE;
  861. } else {
  862. pvid = port;
  863. egress = AR8216_OUT_KEEP;
  864. ingress = AR8216_IN_PORT_ONLY;
  865. }
  866. ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  867. AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  868. AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  869. AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  870. AR8216_PORT_CTRL_LEARN |
  871. (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  872. (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  873. ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN(port),
  874. AR8236_PORT_VLAN_DEFAULT_ID,
  875. (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
  876. ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN2(port),
  877. AR8236_PORT_VLAN2_VLAN_MODE |
  878. AR8236_PORT_VLAN2_MEMBER,
  879. (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
  880. (members << AR8236_PORT_VLAN2_MEMBER_S));
  881. }
  882. static void
  883. ar8236_init_globals(struct ar8xxx_priv *priv)
  884. {
  885. /* enable jumbo frames */
  886. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  887. AR8316_GCTRL_MTU, 9018 + 8 + 2);
  888. /* enable cpu port to receive arp frames */
  889. ar8xxx_reg_set(priv, AR8216_REG_ATU_CTRL,
  890. AR8236_ATU_CTRL_RES);
  891. /*
  892. * Enable Broadcast/unknown multicast and unicast frames
  893. * transmitted to the CPU port.
  894. */
  895. ar8xxx_reg_set(priv, AR8216_REG_FLOOD_MASK,
  896. AR8229_FLOOD_MASK_BC_DP(0) |
  897. AR8229_FLOOD_MASK_MC_DP(0) |
  898. AR8229_FLOOD_MASK_UC_DP(0));
  899. /* Enable MIB counters */
  900. ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
  901. (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
  902. AR8236_MIB_EN);
  903. }
  904. static int
  905. ar8316_hw_init(struct ar8xxx_priv *priv)
  906. {
  907. u32 val, newval;
  908. val = ar8xxx_read(priv, AR8316_REG_POSTRIP);
  909. if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  910. if (priv->port4_phy) {
  911. /* value taken from Ubiquiti RouterStation Pro */
  912. newval = 0x81461bea;
  913. pr_info("ar8316: Using port 4 as PHY\n");
  914. } else {
  915. newval = 0x01261be2;
  916. pr_info("ar8316: Using port 4 as switch port\n");
  917. }
  918. } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
  919. /* value taken from AVM Fritz!Box 7390 sources */
  920. newval = 0x010e5b71;
  921. } else {
  922. /* no known value for phy interface */
  923. pr_err("ar8316: unsupported mii mode: %d.\n",
  924. priv->phy->interface);
  925. return -EINVAL;
  926. }
  927. if (val == newval)
  928. goto out;
  929. ar8xxx_write(priv, AR8316_REG_POSTRIP, newval);
  930. if (priv->port4_phy &&
  931. priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  932. /* work around for phy4 rgmii mode */
  933. ar8xxx_phy_dbg_write(priv, 4, 0x12, 0x480c);
  934. /* rx delay */
  935. ar8xxx_phy_dbg_write(priv, 4, 0x0, 0x824e);
  936. /* tx delay */
  937. ar8xxx_phy_dbg_write(priv, 4, 0x5, 0x3d47);
  938. msleep(1000);
  939. }
  940. ar8xxx_phy_init(priv);
  941. out:
  942. priv->initialized = true;
  943. return 0;
  944. }
  945. static void
  946. ar8316_init_globals(struct ar8xxx_priv *priv)
  947. {
  948. /* standard atheros magic */
  949. ar8xxx_write(priv, 0x38, 0xc000050e);
  950. /* enable cpu port to receive multicast and broadcast frames */
  951. ar8xxx_write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
  952. /* enable jumbo frames */
  953. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  954. AR8316_GCTRL_MTU, 9018 + 8 + 2);
  955. /* Enable MIB counters */
  956. ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
  957. (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
  958. AR8236_MIB_EN);
  959. }
  960. int
  961. ar8xxx_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  962. struct switch_val *val)
  963. {
  964. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  965. priv->vlan = !!val->value.i;
  966. return 0;
  967. }
  968. int
  969. ar8xxx_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  970. struct switch_val *val)
  971. {
  972. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  973. val->value.i = priv->vlan;
  974. return 0;
  975. }
  976. int
  977. ar8xxx_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
  978. {
  979. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  980. /* make sure no invalid PVIDs get set */
  981. if (vlan < 0 || vlan >= dev->vlans ||
  982. port < 0 || port >= AR8X16_MAX_PORTS)
  983. return -EINVAL;
  984. priv->pvid[port] = vlan;
  985. return 0;
  986. }
  987. int
  988. ar8xxx_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
  989. {
  990. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  991. if (port < 0 || port >= AR8X16_MAX_PORTS)
  992. return -EINVAL;
  993. *vlan = priv->pvid[port];
  994. return 0;
  995. }
  996. static int
  997. ar8xxx_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  998. struct switch_val *val)
  999. {
  1000. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1001. if (val->port_vlan >= dev->vlans)
  1002. return -EINVAL;
  1003. priv->vlan_id[val->port_vlan] = val->value.i;
  1004. return 0;
  1005. }
  1006. static int
  1007. ar8xxx_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  1008. struct switch_val *val)
  1009. {
  1010. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1011. val->value.i = priv->vlan_id[val->port_vlan];
  1012. return 0;
  1013. }
  1014. int
  1015. ar8xxx_sw_get_port_link(struct switch_dev *dev, int port,
  1016. struct switch_port_link *link)
  1017. {
  1018. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1019. ar8216_read_port_link(priv, port, link);
  1020. return 0;
  1021. }
  1022. static int
  1023. ar8xxx_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
  1024. {
  1025. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1026. u8 ports;
  1027. int i;
  1028. if (val->port_vlan >= dev->vlans)
  1029. return -EINVAL;
  1030. ports = priv->vlan_table[val->port_vlan];
  1031. val->len = 0;
  1032. for (i = 0; i < dev->ports; i++) {
  1033. struct switch_port *p;
  1034. if (!(ports & (1 << i)))
  1035. continue;
  1036. p = &val->value.ports[val->len++];
  1037. p->id = i;
  1038. if (priv->vlan_tagged & (1 << i))
  1039. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  1040. else
  1041. p->flags = 0;
  1042. }
  1043. return 0;
  1044. }
  1045. static int
  1046. ar8xxx_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
  1047. {
  1048. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1049. u8 *vt = &priv->vlan_table[val->port_vlan];
  1050. int i, j;
  1051. *vt = 0;
  1052. for (i = 0; i < val->len; i++) {
  1053. struct switch_port *p = &val->value.ports[i];
  1054. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  1055. priv->vlan_tagged |= (1 << p->id);
  1056. } else {
  1057. priv->vlan_tagged &= ~(1 << p->id);
  1058. priv->pvid[p->id] = val->port_vlan;
  1059. /* make sure that an untagged port does not
  1060. * appear in other vlans */
  1061. for (j = 0; j < dev->vlans; j++) {
  1062. if (j == val->port_vlan)
  1063. continue;
  1064. priv->vlan_table[j] &= ~(1 << p->id);
  1065. }
  1066. }
  1067. *vt |= 1 << p->id;
  1068. }
  1069. return 0;
  1070. }
  1071. static void
  1072. ar8216_set_mirror_regs(struct ar8xxx_priv *priv)
  1073. {
  1074. int port;
  1075. /* reset all mirror registers */
  1076. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
  1077. AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
  1078. (0xF << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  1079. for (port = 0; port < AR8216_NUM_PORTS; port++) {
  1080. ar8xxx_reg_clear(priv, AR8216_REG_PORT_CTRL(port),
  1081. AR8216_PORT_CTRL_MIRROR_RX);
  1082. ar8xxx_reg_clear(priv, AR8216_REG_PORT_CTRL(port),
  1083. AR8216_PORT_CTRL_MIRROR_TX);
  1084. }
  1085. /* now enable mirroring if necessary */
  1086. if (priv->source_port >= AR8216_NUM_PORTS ||
  1087. priv->monitor_port >= AR8216_NUM_PORTS ||
  1088. priv->source_port == priv->monitor_port) {
  1089. return;
  1090. }
  1091. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
  1092. AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
  1093. (priv->monitor_port << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  1094. if (priv->mirror_rx)
  1095. ar8xxx_reg_set(priv, AR8216_REG_PORT_CTRL(priv->source_port),
  1096. AR8216_PORT_CTRL_MIRROR_RX);
  1097. if (priv->mirror_tx)
  1098. ar8xxx_reg_set(priv, AR8216_REG_PORT_CTRL(priv->source_port),
  1099. AR8216_PORT_CTRL_MIRROR_TX);
  1100. }
  1101. static inline u32
  1102. ar8xxx_age_time_val(int age_time)
  1103. {
  1104. return (age_time + AR8XXX_REG_ARL_CTRL_AGE_TIME_SECS / 2) /
  1105. AR8XXX_REG_ARL_CTRL_AGE_TIME_SECS;
  1106. }
  1107. static inline void
  1108. ar8xxx_set_age_time(struct ar8xxx_priv *priv, int reg)
  1109. {
  1110. u32 age_time = ar8xxx_age_time_val(priv->arl_age_time);
  1111. ar8xxx_rmw(priv, reg, AR8216_ATU_CTRL_AGE_TIME, age_time << AR8216_ATU_CTRL_AGE_TIME_S);
  1112. }
  1113. int
  1114. ar8xxx_sw_hw_apply(struct switch_dev *dev)
  1115. {
  1116. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1117. const struct ar8xxx_chip *chip = priv->chip;
  1118. u8 portmask[AR8X16_MAX_PORTS];
  1119. int i, j;
  1120. mutex_lock(&priv->reg_mutex);
  1121. /* flush all vlan translation unit entries */
  1122. priv->chip->vtu_flush(priv);
  1123. memset(portmask, 0, sizeof(portmask));
  1124. if (!priv->init) {
  1125. /* calculate the port destination masks and load vlans
  1126. * into the vlan translation unit */
  1127. for (j = 0; j < dev->vlans; j++) {
  1128. u8 vp = priv->vlan_table[j];
  1129. if (!vp)
  1130. continue;
  1131. for (i = 0; i < dev->ports; i++) {
  1132. u8 mask = (1 << i);
  1133. if (vp & mask)
  1134. portmask[i] |= vp & ~mask;
  1135. }
  1136. chip->vtu_load_vlan(priv, priv->vlan_id[j],
  1137. priv->vlan_table[j]);
  1138. }
  1139. } else {
  1140. /* vlan disabled:
  1141. * isolate all ports, but connect them to the cpu port */
  1142. for (i = 0; i < dev->ports; i++) {
  1143. if (i == AR8216_PORT_CPU)
  1144. continue;
  1145. portmask[i] = 1 << AR8216_PORT_CPU;
  1146. portmask[AR8216_PORT_CPU] |= (1 << i);
  1147. }
  1148. }
  1149. /* update the port destination mask registers and tag settings */
  1150. for (i = 0; i < dev->ports; i++) {
  1151. chip->setup_port(priv, i, portmask[i]);
  1152. }
  1153. chip->set_mirror_regs(priv);
  1154. /* set age time */
  1155. if (chip->reg_arl_ctrl)
  1156. ar8xxx_set_age_time(priv, chip->reg_arl_ctrl);
  1157. mutex_unlock(&priv->reg_mutex);
  1158. return 0;
  1159. }
  1160. int
  1161. ar8xxx_sw_reset_switch(struct switch_dev *dev)
  1162. {
  1163. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1164. const struct ar8xxx_chip *chip = priv->chip;
  1165. int i;
  1166. mutex_lock(&priv->reg_mutex);
  1167. memset(&priv->vlan, 0, sizeof(struct ar8xxx_priv) -
  1168. offsetof(struct ar8xxx_priv, vlan));
  1169. for (i = 0; i < dev->vlans; i++)
  1170. priv->vlan_id[i] = i;
  1171. /* Configure all ports */
  1172. for (i = 0; i < dev->ports; i++)
  1173. chip->init_port(priv, i);
  1174. priv->mirror_rx = false;
  1175. priv->mirror_tx = false;
  1176. priv->source_port = 0;
  1177. priv->monitor_port = 0;
  1178. priv->arl_age_time = AR8XXX_DEFAULT_ARL_AGE_TIME;
  1179. chip->init_globals(priv);
  1180. chip->atu_flush(priv);
  1181. mutex_unlock(&priv->reg_mutex);
  1182. return chip->sw_hw_apply(dev);
  1183. }
  1184. int
  1185. ar8xxx_sw_set_reset_mibs(struct switch_dev *dev,
  1186. const struct switch_attr *attr,
  1187. struct switch_val *val)
  1188. {
  1189. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1190. unsigned int len;
  1191. int ret;
  1192. if (!ar8xxx_has_mib_counters(priv))
  1193. return -EOPNOTSUPP;
  1194. mutex_lock(&priv->mib_lock);
  1195. len = priv->dev.ports * priv->chip->num_mibs *
  1196. sizeof(*priv->mib_stats);
  1197. memset(priv->mib_stats, '\0', len);
  1198. ret = ar8xxx_mib_flush(priv);
  1199. if (ret)
  1200. goto unlock;
  1201. ret = 0;
  1202. unlock:
  1203. mutex_unlock(&priv->mib_lock);
  1204. return ret;
  1205. }
  1206. int
  1207. ar8xxx_sw_set_mib_poll_interval(struct switch_dev *dev,
  1208. const struct switch_attr *attr,
  1209. struct switch_val *val)
  1210. {
  1211. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1212. if (!ar8xxx_has_mib_counters(priv))
  1213. return -EOPNOTSUPP;
  1214. ar8xxx_mib_stop(priv);
  1215. priv->mib_poll_interval = val->value.i;
  1216. ar8xxx_mib_start(priv);
  1217. return 0;
  1218. }
  1219. int
  1220. ar8xxx_sw_get_mib_poll_interval(struct switch_dev *dev,
  1221. const struct switch_attr *attr,
  1222. struct switch_val *val)
  1223. {
  1224. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1225. if (!ar8xxx_has_mib_counters(priv))
  1226. return -EOPNOTSUPP;
  1227. val->value.i = priv->mib_poll_interval;
  1228. return 0;
  1229. }
  1230. int
  1231. ar8xxx_sw_set_mib_type(struct switch_dev *dev,
  1232. const struct switch_attr *attr,
  1233. struct switch_val *val)
  1234. {
  1235. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1236. if (!ar8xxx_has_mib_counters(priv))
  1237. return -EOPNOTSUPP;
  1238. priv->mib_type = val->value.i;
  1239. return 0;
  1240. }
  1241. int
  1242. ar8xxx_sw_get_mib_type(struct switch_dev *dev,
  1243. const struct switch_attr *attr,
  1244. struct switch_val *val)
  1245. {
  1246. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1247. if (!ar8xxx_has_mib_counters(priv))
  1248. return -EOPNOTSUPP;
  1249. val->value.i = priv->mib_type;
  1250. return 0;
  1251. }
  1252. int
  1253. ar8xxx_sw_set_mirror_rx_enable(struct switch_dev *dev,
  1254. const struct switch_attr *attr,
  1255. struct switch_val *val)
  1256. {
  1257. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1258. mutex_lock(&priv->reg_mutex);
  1259. priv->mirror_rx = !!val->value.i;
  1260. priv->chip->set_mirror_regs(priv);
  1261. mutex_unlock(&priv->reg_mutex);
  1262. return 0;
  1263. }
  1264. int
  1265. ar8xxx_sw_get_mirror_rx_enable(struct switch_dev *dev,
  1266. const struct switch_attr *attr,
  1267. struct switch_val *val)
  1268. {
  1269. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1270. val->value.i = priv->mirror_rx;
  1271. return 0;
  1272. }
  1273. int
  1274. ar8xxx_sw_set_mirror_tx_enable(struct switch_dev *dev,
  1275. const struct switch_attr *attr,
  1276. struct switch_val *val)
  1277. {
  1278. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1279. mutex_lock(&priv->reg_mutex);
  1280. priv->mirror_tx = !!val->value.i;
  1281. priv->chip->set_mirror_regs(priv);
  1282. mutex_unlock(&priv->reg_mutex);
  1283. return 0;
  1284. }
  1285. int
  1286. ar8xxx_sw_get_mirror_tx_enable(struct switch_dev *dev,
  1287. const struct switch_attr *attr,
  1288. struct switch_val *val)
  1289. {
  1290. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1291. val->value.i = priv->mirror_tx;
  1292. return 0;
  1293. }
  1294. int
  1295. ar8xxx_sw_set_mirror_monitor_port(struct switch_dev *dev,
  1296. const struct switch_attr *attr,
  1297. struct switch_val *val)
  1298. {
  1299. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1300. mutex_lock(&priv->reg_mutex);
  1301. priv->monitor_port = val->value.i;
  1302. priv->chip->set_mirror_regs(priv);
  1303. mutex_unlock(&priv->reg_mutex);
  1304. return 0;
  1305. }
  1306. int
  1307. ar8xxx_sw_get_mirror_monitor_port(struct switch_dev *dev,
  1308. const struct switch_attr *attr,
  1309. struct switch_val *val)
  1310. {
  1311. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1312. val->value.i = priv->monitor_port;
  1313. return 0;
  1314. }
  1315. int
  1316. ar8xxx_sw_set_mirror_source_port(struct switch_dev *dev,
  1317. const struct switch_attr *attr,
  1318. struct switch_val *val)
  1319. {
  1320. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1321. mutex_lock(&priv->reg_mutex);
  1322. priv->source_port = val->value.i;
  1323. priv->chip->set_mirror_regs(priv);
  1324. mutex_unlock(&priv->reg_mutex);
  1325. return 0;
  1326. }
  1327. int
  1328. ar8xxx_sw_get_mirror_source_port(struct switch_dev *dev,
  1329. const struct switch_attr *attr,
  1330. struct switch_val *val)
  1331. {
  1332. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1333. val->value.i = priv->source_port;
  1334. return 0;
  1335. }
  1336. int
  1337. ar8xxx_sw_set_port_reset_mib(struct switch_dev *dev,
  1338. const struct switch_attr *attr,
  1339. struct switch_val *val)
  1340. {
  1341. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1342. int port;
  1343. int ret;
  1344. if (!ar8xxx_has_mib_counters(priv))
  1345. return -EOPNOTSUPP;
  1346. port = val->port_vlan;
  1347. if (port >= dev->ports)
  1348. return -EINVAL;
  1349. mutex_lock(&priv->mib_lock);
  1350. ret = ar8xxx_mib_capture(priv);
  1351. if (ret)
  1352. goto unlock;
  1353. ar8xxx_mib_fetch_port_stat(priv, port, true);
  1354. ret = 0;
  1355. unlock:
  1356. mutex_unlock(&priv->mib_lock);
  1357. return ret;
  1358. }
  1359. static void
  1360. ar8xxx_byte_to_str(char *buf, int len, u64 byte)
  1361. {
  1362. unsigned long b;
  1363. const char *unit;
  1364. if (byte >= 0x40000000) { /* 1 GiB */
  1365. b = byte * 10 / 0x40000000;
  1366. unit = "GiB";
  1367. } else if (byte >= 0x100000) { /* 1 MiB */
  1368. b = byte * 10 / 0x100000;
  1369. unit = "MiB";
  1370. } else if (byte >= 0x400) { /* 1 KiB */
  1371. b = byte * 10 / 0x400;
  1372. unit = "KiB";
  1373. } else {
  1374. b = byte;
  1375. unit = "Byte";
  1376. }
  1377. if (strcmp(unit, "Byte"))
  1378. snprintf(buf, len, "%lu.%lu %s", b / 10, b % 10, unit);
  1379. else
  1380. snprintf(buf, len, "%lu %s", b, unit);
  1381. }
  1382. int
  1383. ar8xxx_sw_get_port_mib(struct switch_dev *dev,
  1384. const struct switch_attr *attr,
  1385. struct switch_val *val)
  1386. {
  1387. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1388. const struct ar8xxx_chip *chip = priv->chip;
  1389. u64 *mib_stats, mib_data;
  1390. unsigned int port;
  1391. int ret;
  1392. char *buf = priv->buf;
  1393. char buf1[64];
  1394. const char *mib_name;
  1395. int i, len = 0;
  1396. bool mib_stats_empty = true;
  1397. if (!ar8xxx_has_mib_counters(priv) || !priv->mib_poll_interval)
  1398. return -EOPNOTSUPP;
  1399. port = val->port_vlan;
  1400. if (port >= dev->ports)
  1401. return -EINVAL;
  1402. mutex_lock(&priv->mib_lock);
  1403. ret = ar8xxx_mib_capture(priv);
  1404. if (ret)
  1405. goto unlock;
  1406. ar8xxx_mib_fetch_port_stat(priv, port, false);
  1407. len += snprintf(buf + len, sizeof(priv->buf) - len,
  1408. "MIB counters\n");
  1409. mib_stats = &priv->mib_stats[port * chip->num_mibs];
  1410. for (i = 0; i < chip->num_mibs; i++) {
  1411. if (chip->mib_decs[i].type > priv->mib_type)
  1412. continue;
  1413. mib_name = chip->mib_decs[i].name;
  1414. mib_data = mib_stats[i];
  1415. len += snprintf(buf + len, sizeof(priv->buf) - len,
  1416. "%-12s: %llu\n", mib_name, mib_data);
  1417. if ((!strcmp(mib_name, "TxByte") ||
  1418. !strcmp(mib_name, "RxGoodByte")) &&
  1419. mib_data >= 1024) {
  1420. ar8xxx_byte_to_str(buf1, sizeof(buf1), mib_data);
  1421. --len; /* discard newline at the end of buf */
  1422. len += snprintf(buf + len, sizeof(priv->buf) - len,
  1423. " (%s)\n", buf1);
  1424. }
  1425. if (mib_stats_empty && mib_data)
  1426. mib_stats_empty = false;
  1427. }
  1428. if (mib_stats_empty)
  1429. len = snprintf(buf, sizeof(priv->buf), "No MIB data");
  1430. val->value.s = buf;
  1431. val->len = len;
  1432. ret = 0;
  1433. unlock:
  1434. mutex_unlock(&priv->mib_lock);
  1435. return ret;
  1436. }
  1437. int
  1438. ar8xxx_sw_set_arl_age_time(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. int age_time = val->value.i;
  1443. u32 age_time_val;
  1444. if (age_time < 0)
  1445. return -EINVAL;
  1446. age_time_val = ar8xxx_age_time_val(age_time);
  1447. if (age_time_val == 0 || age_time_val > 0xffff)
  1448. return -EINVAL;
  1449. priv->arl_age_time = age_time;
  1450. return 0;
  1451. }
  1452. int
  1453. ar8xxx_sw_get_arl_age_time(struct switch_dev *dev, const struct switch_attr *attr,
  1454. struct switch_val *val)
  1455. {
  1456. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1457. val->value.i = priv->arl_age_time;
  1458. return 0;
  1459. }
  1460. int
  1461. ar8xxx_sw_get_arl_table(struct switch_dev *dev,
  1462. const struct switch_attr *attr,
  1463. struct switch_val *val)
  1464. {
  1465. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1466. struct mii_bus *bus = priv->mii_bus;
  1467. const struct ar8xxx_chip *chip = priv->chip;
  1468. char *buf = priv->arl_buf;
  1469. int i, j, k, len = 0;
  1470. struct arl_entry *a, *a1;
  1471. u32 status;
  1472. if (!chip->get_arl_entry)
  1473. return -EOPNOTSUPP;
  1474. mutex_lock(&priv->reg_mutex);
  1475. mutex_lock(&bus->mdio_lock);
  1476. chip->get_arl_entry(priv, NULL, NULL, AR8XXX_ARL_INITIALIZE);
  1477. for(i = 0; i < AR8XXX_NUM_ARL_RECORDS; ++i) {
  1478. a = &priv->arl_table[i];
  1479. duplicate:
  1480. chip->get_arl_entry(priv, a, &status, AR8XXX_ARL_GET_NEXT);
  1481. if (!status)
  1482. break;
  1483. /* avoid duplicates
  1484. * ARL table can include multiple valid entries
  1485. * per MAC, just with differing status codes
  1486. */
  1487. for (j = 0; j < i; ++j) {
  1488. a1 = &priv->arl_table[j];
  1489. if (!memcmp(a->mac, a1->mac, sizeof(a->mac))) {
  1490. /* ignore ports already seen in former entry */
  1491. a->portmap &= ~a1->portmap;
  1492. if (!a->portmap)
  1493. goto duplicate;
  1494. }
  1495. }
  1496. }
  1497. mutex_unlock(&bus->mdio_lock);
  1498. len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
  1499. "address resolution table\n");
  1500. if (i == AR8XXX_NUM_ARL_RECORDS)
  1501. len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
  1502. "Too many entries found, displaying the first %d only!\n",
  1503. AR8XXX_NUM_ARL_RECORDS);
  1504. for (j = 0; j < priv->dev.ports; ++j) {
  1505. for (k = 0; k < i; ++k) {
  1506. a = &priv->arl_table[k];
  1507. if (!(a->portmap & BIT(j)))
  1508. continue;
  1509. len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
  1510. "Port %d: MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  1511. j,
  1512. a->mac[5], a->mac[4], a->mac[3],
  1513. a->mac[2], a->mac[1], a->mac[0]);
  1514. }
  1515. }
  1516. val->value.s = buf;
  1517. val->len = len;
  1518. mutex_unlock(&priv->reg_mutex);
  1519. return 0;
  1520. }
  1521. int
  1522. ar8xxx_sw_set_flush_arl_table(struct switch_dev *dev,
  1523. const struct switch_attr *attr,
  1524. struct switch_val *val)
  1525. {
  1526. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1527. int ret;
  1528. mutex_lock(&priv->reg_mutex);
  1529. ret = priv->chip->atu_flush(priv);
  1530. mutex_unlock(&priv->reg_mutex);
  1531. return ret;
  1532. }
  1533. int
  1534. ar8xxx_sw_set_flush_port_arl_table(struct switch_dev *dev,
  1535. const struct switch_attr *attr,
  1536. struct switch_val *val)
  1537. {
  1538. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1539. int port, ret;
  1540. port = val->port_vlan;
  1541. if (port >= dev->ports)
  1542. return -EINVAL;
  1543. mutex_lock(&priv->reg_mutex);
  1544. ret = priv->chip->atu_flush_port(priv, port);
  1545. mutex_unlock(&priv->reg_mutex);
  1546. return ret;
  1547. }
  1548. int
  1549. ar8xxx_sw_get_port_stats(struct switch_dev *dev, int port,
  1550. struct switch_port_stats *stats)
  1551. {
  1552. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1553. u64 *mib_stats;
  1554. if (!ar8xxx_has_mib_counters(priv) || !priv->mib_poll_interval)
  1555. return -EOPNOTSUPP;
  1556. if (!(priv->chip->mib_rxb_id || priv->chip->mib_txb_id))
  1557. return -EOPNOTSUPP;
  1558. if (port >= dev->ports)
  1559. return -EINVAL;
  1560. mutex_lock(&priv->mib_lock);
  1561. mib_stats = &priv->mib_stats[port * priv->chip->num_mibs];
  1562. stats->tx_bytes = mib_stats[priv->chip->mib_txb_id];
  1563. stats->rx_bytes = mib_stats[priv->chip->mib_rxb_id];
  1564. mutex_unlock(&priv->mib_lock);
  1565. return 0;
  1566. }
  1567. static int
  1568. ar8xxx_phy_read(struct mii_bus *bus, int phy_addr, int reg_addr)
  1569. {
  1570. struct ar8xxx_priv *priv = bus->priv;
  1571. return priv->chip->phy_read(priv, phy_addr, reg_addr);
  1572. }
  1573. static int
  1574. ar8xxx_phy_write(struct mii_bus *bus, int phy_addr, int reg_addr,
  1575. u16 reg_val)
  1576. {
  1577. struct ar8xxx_priv *priv = bus->priv;
  1578. return priv->chip->phy_write(priv, phy_addr, reg_addr, reg_val);
  1579. }
  1580. static const struct switch_attr ar8xxx_sw_attr_globals[] = {
  1581. {
  1582. .type = SWITCH_TYPE_INT,
  1583. .name = "enable_vlan",
  1584. .description = "Enable VLAN mode",
  1585. .set = ar8xxx_sw_set_vlan,
  1586. .get = ar8xxx_sw_get_vlan,
  1587. .max = 1
  1588. },
  1589. {
  1590. .type = SWITCH_TYPE_NOVAL,
  1591. .name = "reset_mibs",
  1592. .description = "Reset all MIB counters",
  1593. .set = ar8xxx_sw_set_reset_mibs,
  1594. },
  1595. {
  1596. .type = SWITCH_TYPE_INT,
  1597. .name = "ar8xxx_mib_poll_interval",
  1598. .description = "MIB polling interval in msecs (0 to disable)",
  1599. .set = ar8xxx_sw_set_mib_poll_interval,
  1600. .get = ar8xxx_sw_get_mib_poll_interval
  1601. },
  1602. {
  1603. .type = SWITCH_TYPE_INT,
  1604. .name = "ar8xxx_mib_type",
  1605. .description = "MIB type (0=basic 1=extended)",
  1606. .set = ar8xxx_sw_set_mib_type,
  1607. .get = ar8xxx_sw_get_mib_type
  1608. },
  1609. {
  1610. .type = SWITCH_TYPE_INT,
  1611. .name = "enable_mirror_rx",
  1612. .description = "Enable mirroring of RX packets",
  1613. .set = ar8xxx_sw_set_mirror_rx_enable,
  1614. .get = ar8xxx_sw_get_mirror_rx_enable,
  1615. .max = 1
  1616. },
  1617. {
  1618. .type = SWITCH_TYPE_INT,
  1619. .name = "enable_mirror_tx",
  1620. .description = "Enable mirroring of TX packets",
  1621. .set = ar8xxx_sw_set_mirror_tx_enable,
  1622. .get = ar8xxx_sw_get_mirror_tx_enable,
  1623. .max = 1
  1624. },
  1625. {
  1626. .type = SWITCH_TYPE_INT,
  1627. .name = "mirror_monitor_port",
  1628. .description = "Mirror monitor port",
  1629. .set = ar8xxx_sw_set_mirror_monitor_port,
  1630. .get = ar8xxx_sw_get_mirror_monitor_port,
  1631. .max = AR8216_NUM_PORTS - 1
  1632. },
  1633. {
  1634. .type = SWITCH_TYPE_INT,
  1635. .name = "mirror_source_port",
  1636. .description = "Mirror source port",
  1637. .set = ar8xxx_sw_set_mirror_source_port,
  1638. .get = ar8xxx_sw_get_mirror_source_port,
  1639. .max = AR8216_NUM_PORTS - 1
  1640. },
  1641. {
  1642. .type = SWITCH_TYPE_STRING,
  1643. .name = "arl_table",
  1644. .description = "Get ARL table",
  1645. .set = NULL,
  1646. .get = ar8xxx_sw_get_arl_table,
  1647. },
  1648. {
  1649. .type = SWITCH_TYPE_NOVAL,
  1650. .name = "flush_arl_table",
  1651. .description = "Flush ARL table",
  1652. .set = ar8xxx_sw_set_flush_arl_table,
  1653. },
  1654. };
  1655. const struct switch_attr ar8xxx_sw_attr_port[] = {
  1656. {
  1657. .type = SWITCH_TYPE_NOVAL,
  1658. .name = "reset_mib",
  1659. .description = "Reset single port MIB counters",
  1660. .set = ar8xxx_sw_set_port_reset_mib,
  1661. },
  1662. {
  1663. .type = SWITCH_TYPE_STRING,
  1664. .name = "mib",
  1665. .description = "Get port's MIB counters",
  1666. .set = NULL,
  1667. .get = ar8xxx_sw_get_port_mib,
  1668. },
  1669. {
  1670. .type = SWITCH_TYPE_NOVAL,
  1671. .name = "flush_arl_table",
  1672. .description = "Flush port's ARL table entries",
  1673. .set = ar8xxx_sw_set_flush_port_arl_table,
  1674. },
  1675. };
  1676. const struct switch_attr ar8xxx_sw_attr_vlan[1] = {
  1677. {
  1678. .type = SWITCH_TYPE_INT,
  1679. .name = "vid",
  1680. .description = "VLAN ID (0-4094)",
  1681. .set = ar8xxx_sw_set_vid,
  1682. .get = ar8xxx_sw_get_vid,
  1683. .max = 4094,
  1684. },
  1685. };
  1686. static const struct switch_dev_ops ar8xxx_sw_ops = {
  1687. .attr_global = {
  1688. .attr = ar8xxx_sw_attr_globals,
  1689. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_globals),
  1690. },
  1691. .attr_port = {
  1692. .attr = ar8xxx_sw_attr_port,
  1693. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
  1694. },
  1695. .attr_vlan = {
  1696. .attr = ar8xxx_sw_attr_vlan,
  1697. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  1698. },
  1699. .get_port_pvid = ar8xxx_sw_get_pvid,
  1700. .set_port_pvid = ar8xxx_sw_set_pvid,
  1701. .get_vlan_ports = ar8xxx_sw_get_ports,
  1702. .set_vlan_ports = ar8xxx_sw_set_ports,
  1703. .apply_config = ar8xxx_sw_hw_apply,
  1704. .reset_switch = ar8xxx_sw_reset_switch,
  1705. .get_port_link = ar8xxx_sw_get_port_link,
  1706. .get_port_stats = ar8xxx_sw_get_port_stats,
  1707. };
  1708. static const struct ar8xxx_chip ar7240sw_chip = {
  1709. .caps = AR8XXX_CAP_MIB_COUNTERS,
  1710. .reg_port_stats_start = 0x20000,
  1711. .reg_port_stats_length = 0x100,
  1712. .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
  1713. .name = "Atheros AR724X/AR933X built-in",
  1714. .ports = AR7240SW_NUM_PORTS,
  1715. .vlans = AR8216_NUM_VLANS,
  1716. .swops = &ar8xxx_sw_ops,
  1717. .hw_init = ar7240sw_hw_init,
  1718. .init_globals = ar7240sw_init_globals,
  1719. .init_port = ar8229_init_port,
  1720. .phy_read = ar8216_phy_read,
  1721. .phy_write = ar8216_phy_write,
  1722. .setup_port = ar7240sw_setup_port,
  1723. .read_port_status = ar8216_read_port_status,
  1724. .atu_flush = ar8216_atu_flush,
  1725. .atu_flush_port = ar8216_atu_flush_port,
  1726. .vtu_flush = ar8216_vtu_flush,
  1727. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1728. .set_mirror_regs = ar8216_set_mirror_regs,
  1729. .get_arl_entry = ar8216_get_arl_entry,
  1730. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1731. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1732. .mib_decs = ar8236_mibs,
  1733. .mib_func = AR8216_REG_MIB_FUNC,
  1734. .mib_rxb_id = AR8236_MIB_RXB_ID,
  1735. .mib_txb_id = AR8236_MIB_TXB_ID,
  1736. };
  1737. static const struct ar8xxx_chip ar8216_chip = {
  1738. .caps = AR8XXX_CAP_MIB_COUNTERS,
  1739. .reg_port_stats_start = 0x19000,
  1740. .reg_port_stats_length = 0xa0,
  1741. .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
  1742. .name = "Atheros AR8216",
  1743. .ports = AR8216_NUM_PORTS,
  1744. .vlans = AR8216_NUM_VLANS,
  1745. .swops = &ar8xxx_sw_ops,
  1746. .hw_init = ar8216_hw_init,
  1747. .init_globals = ar8216_init_globals,
  1748. .init_port = ar8216_init_port,
  1749. .setup_port = ar8216_setup_port,
  1750. .read_port_status = ar8216_read_port_status,
  1751. .atu_flush = ar8216_atu_flush,
  1752. .atu_flush_port = ar8216_atu_flush_port,
  1753. .vtu_flush = ar8216_vtu_flush,
  1754. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1755. .set_mirror_regs = ar8216_set_mirror_regs,
  1756. .get_arl_entry = ar8216_get_arl_entry,
  1757. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1758. .num_mibs = ARRAY_SIZE(ar8216_mibs),
  1759. .mib_decs = ar8216_mibs,
  1760. .mib_func = AR8216_REG_MIB_FUNC,
  1761. .mib_rxb_id = AR8216_MIB_RXB_ID,
  1762. .mib_txb_id = AR8216_MIB_TXB_ID,
  1763. };
  1764. static const struct ar8xxx_chip ar8229_chip = {
  1765. .caps = AR8XXX_CAP_MIB_COUNTERS,
  1766. .reg_port_stats_start = 0x20000,
  1767. .reg_port_stats_length = 0x100,
  1768. .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
  1769. .name = "Atheros AR8229",
  1770. .ports = AR8216_NUM_PORTS,
  1771. .vlans = AR8216_NUM_VLANS,
  1772. .swops = &ar8xxx_sw_ops,
  1773. .hw_init = ar8229_hw_init,
  1774. .init_globals = ar8229_init_globals,
  1775. .init_port = ar8229_init_port,
  1776. .phy_read = ar8216_phy_read,
  1777. .phy_write = ar8216_phy_write,
  1778. .setup_port = ar8236_setup_port,
  1779. .read_port_status = ar8216_read_port_status,
  1780. .atu_flush = ar8216_atu_flush,
  1781. .atu_flush_port = ar8216_atu_flush_port,
  1782. .vtu_flush = ar8216_vtu_flush,
  1783. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1784. .set_mirror_regs = ar8216_set_mirror_regs,
  1785. .get_arl_entry = ar8216_get_arl_entry,
  1786. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1787. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1788. .mib_decs = ar8236_mibs,
  1789. .mib_func = AR8216_REG_MIB_FUNC,
  1790. .mib_rxb_id = AR8236_MIB_RXB_ID,
  1791. .mib_txb_id = AR8236_MIB_TXB_ID,
  1792. };
  1793. static const struct ar8xxx_chip ar8236_chip = {
  1794. .caps = AR8XXX_CAP_MIB_COUNTERS,
  1795. .reg_port_stats_start = 0x20000,
  1796. .reg_port_stats_length = 0x100,
  1797. .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
  1798. .name = "Atheros AR8236",
  1799. .ports = AR8216_NUM_PORTS,
  1800. .vlans = AR8216_NUM_VLANS,
  1801. .swops = &ar8xxx_sw_ops,
  1802. .hw_init = ar8216_hw_init,
  1803. .init_globals = ar8236_init_globals,
  1804. .init_port = ar8216_init_port,
  1805. .setup_port = ar8236_setup_port,
  1806. .read_port_status = ar8216_read_port_status,
  1807. .atu_flush = ar8216_atu_flush,
  1808. .atu_flush_port = ar8216_atu_flush_port,
  1809. .vtu_flush = ar8216_vtu_flush,
  1810. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1811. .set_mirror_regs = ar8216_set_mirror_regs,
  1812. .get_arl_entry = ar8216_get_arl_entry,
  1813. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1814. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1815. .mib_decs = ar8236_mibs,
  1816. .mib_func = AR8216_REG_MIB_FUNC,
  1817. .mib_rxb_id = AR8236_MIB_RXB_ID,
  1818. .mib_txb_id = AR8236_MIB_TXB_ID,
  1819. };
  1820. static const struct ar8xxx_chip ar8316_chip = {
  1821. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1822. .reg_port_stats_start = 0x20000,
  1823. .reg_port_stats_length = 0x100,
  1824. .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
  1825. .name = "Atheros AR8316",
  1826. .ports = AR8216_NUM_PORTS,
  1827. .vlans = AR8X16_MAX_VLANS,
  1828. .swops = &ar8xxx_sw_ops,
  1829. .hw_init = ar8316_hw_init,
  1830. .init_globals = ar8316_init_globals,
  1831. .init_port = ar8216_init_port,
  1832. .setup_port = ar8216_setup_port,
  1833. .read_port_status = ar8216_read_port_status,
  1834. .atu_flush = ar8216_atu_flush,
  1835. .atu_flush_port = ar8216_atu_flush_port,
  1836. .vtu_flush = ar8216_vtu_flush,
  1837. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1838. .set_mirror_regs = ar8216_set_mirror_regs,
  1839. .get_arl_entry = ar8216_get_arl_entry,
  1840. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1841. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1842. .mib_decs = ar8236_mibs,
  1843. .mib_func = AR8216_REG_MIB_FUNC,
  1844. .mib_rxb_id = AR8236_MIB_RXB_ID,
  1845. .mib_txb_id = AR8236_MIB_TXB_ID,
  1846. };
  1847. static int
  1848. ar8xxx_read_id(struct ar8xxx_priv *priv)
  1849. {
  1850. u32 val;
  1851. u16 id;
  1852. int i;
  1853. val = ar8xxx_read(priv, AR8216_REG_CTRL);
  1854. if (val == ~0)
  1855. return -ENODEV;
  1856. id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  1857. for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
  1858. u16 t;
  1859. val = ar8xxx_read(priv, AR8216_REG_CTRL);
  1860. if (val == ~0)
  1861. return -ENODEV;
  1862. t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  1863. if (t != id)
  1864. return -ENODEV;
  1865. }
  1866. priv->chip_ver = (id & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
  1867. priv->chip_rev = (id & AR8216_CTRL_REVISION);
  1868. return 0;
  1869. }
  1870. static int
  1871. ar8xxx_id_chip(struct ar8xxx_priv *priv)
  1872. {
  1873. int ret;
  1874. ret = ar8xxx_read_id(priv);
  1875. if(ret)
  1876. return ret;
  1877. switch (priv->chip_ver) {
  1878. case AR8XXX_VER_AR8216:
  1879. priv->chip = &ar8216_chip;
  1880. break;
  1881. case AR8XXX_VER_AR8236:
  1882. priv->chip = &ar8236_chip;
  1883. break;
  1884. case AR8XXX_VER_AR8316:
  1885. priv->chip = &ar8316_chip;
  1886. break;
  1887. case AR8XXX_VER_AR8327:
  1888. priv->chip = &ar8327_chip;
  1889. break;
  1890. case AR8XXX_VER_AR8337:
  1891. priv->chip = &ar8337_chip;
  1892. break;
  1893. default:
  1894. pr_err("ar8216: Unknown Atheros device [ver=%d, rev=%d]\n",
  1895. priv->chip_ver, priv->chip_rev);
  1896. return -ENODEV;
  1897. }
  1898. return 0;
  1899. }
  1900. static void
  1901. ar8xxx_mib_work_func(struct work_struct *work)
  1902. {
  1903. struct ar8xxx_priv *priv;
  1904. int err, i;
  1905. priv = container_of(work, struct ar8xxx_priv, mib_work.work);
  1906. mutex_lock(&priv->mib_lock);
  1907. err = ar8xxx_mib_capture(priv);
  1908. if (err)
  1909. goto next_attempt;
  1910. for (i = 0; i < priv->dev.ports; i++)
  1911. ar8xxx_mib_fetch_port_stat(priv, i, false);
  1912. next_attempt:
  1913. mutex_unlock(&priv->mib_lock);
  1914. schedule_delayed_work(&priv->mib_work,
  1915. msecs_to_jiffies(priv->mib_poll_interval));
  1916. }
  1917. static int
  1918. ar8xxx_mib_init(struct ar8xxx_priv *priv)
  1919. {
  1920. unsigned int len;
  1921. if (!ar8xxx_has_mib_counters(priv))
  1922. return 0;
  1923. BUG_ON(!priv->chip->mib_decs || !priv->chip->num_mibs);
  1924. len = priv->dev.ports * priv->chip->num_mibs *
  1925. sizeof(*priv->mib_stats);
  1926. priv->mib_stats = kzalloc(len, GFP_KERNEL);
  1927. if (!priv->mib_stats)
  1928. return -ENOMEM;
  1929. return 0;
  1930. }
  1931. static void
  1932. ar8xxx_mib_start(struct ar8xxx_priv *priv)
  1933. {
  1934. if (!ar8xxx_has_mib_counters(priv) || !priv->mib_poll_interval)
  1935. return;
  1936. schedule_delayed_work(&priv->mib_work,
  1937. msecs_to_jiffies(priv->mib_poll_interval));
  1938. }
  1939. static void
  1940. ar8xxx_mib_stop(struct ar8xxx_priv *priv)
  1941. {
  1942. if (!ar8xxx_has_mib_counters(priv) || !priv->mib_poll_interval)
  1943. return;
  1944. cancel_delayed_work_sync(&priv->mib_work);
  1945. }
  1946. static struct ar8xxx_priv *
  1947. ar8xxx_create(void)
  1948. {
  1949. struct ar8xxx_priv *priv;
  1950. priv = kzalloc(sizeof(struct ar8xxx_priv), GFP_KERNEL);
  1951. if (priv == NULL)
  1952. return NULL;
  1953. mutex_init(&priv->reg_mutex);
  1954. mutex_init(&priv->mib_lock);
  1955. INIT_DELAYED_WORK(&priv->mib_work, ar8xxx_mib_work_func);
  1956. return priv;
  1957. }
  1958. static void
  1959. ar8xxx_free(struct ar8xxx_priv *priv)
  1960. {
  1961. if (priv->chip && priv->chip->cleanup)
  1962. priv->chip->cleanup(priv);
  1963. kfree(priv->chip_data);
  1964. kfree(priv->mib_stats);
  1965. kfree(priv);
  1966. }
  1967. static int
  1968. ar8xxx_probe_switch(struct ar8xxx_priv *priv)
  1969. {
  1970. const struct ar8xxx_chip *chip;
  1971. struct switch_dev *swdev;
  1972. int ret;
  1973. chip = priv->chip;
  1974. swdev = &priv->dev;
  1975. swdev->cpu_port = AR8216_PORT_CPU;
  1976. swdev->name = chip->name;
  1977. swdev->vlans = chip->vlans;
  1978. swdev->ports = chip->ports;
  1979. swdev->ops = chip->swops;
  1980. ret = ar8xxx_mib_init(priv);
  1981. if (ret)
  1982. return ret;
  1983. return 0;
  1984. }
  1985. static int
  1986. ar8xxx_start(struct ar8xxx_priv *priv)
  1987. {
  1988. int ret;
  1989. priv->init = true;
  1990. ret = priv->chip->hw_init(priv);
  1991. if (ret)
  1992. return ret;
  1993. ret = ar8xxx_sw_reset_switch(&priv->dev);
  1994. if (ret)
  1995. return ret;
  1996. priv->init = false;
  1997. ar8xxx_mib_start(priv);
  1998. return 0;
  1999. }
  2000. static int
  2001. ar8xxx_phy_config_init(struct phy_device *phydev)
  2002. {
  2003. struct ar8xxx_priv *priv = phydev->priv;
  2004. #ifdef CONFIG_ETHERNET_PACKET_MANGLE
  2005. struct net_device *dev = phydev->attached_dev;
  2006. #endif
  2007. int ret;
  2008. if (WARN_ON(!priv))
  2009. return -ENODEV;
  2010. if (priv->chip->config_at_probe)
  2011. return ar8xxx_phy_check_aneg(phydev);
  2012. priv->phy = phydev;
  2013. if (phydev->mdio.addr != 0) {
  2014. if (chip_is_ar8316(priv)) {
  2015. /* switch device has been initialized, reinit */
  2016. priv->dev.ports = (AR8216_NUM_PORTS - 1);
  2017. priv->initialized = false;
  2018. priv->port4_phy = true;
  2019. ar8316_hw_init(priv);
  2020. return 0;
  2021. }
  2022. return 0;
  2023. }
  2024. ret = ar8xxx_start(priv);
  2025. if (ret)
  2026. return ret;
  2027. #ifdef CONFIG_ETHERNET_PACKET_MANGLE
  2028. /* VID fixup only needed on ar8216 */
  2029. if (chip_is_ar8216(priv)) {
  2030. dev->phy_ptr = priv;
  2031. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6,1,0)
  2032. dev->priv_flags |= IFF_NO_IP_ALIGN;
  2033. #else LINUX_VERSION_CODE >= KERNEL_VERSION(5,15,0)
  2034. dev->extra_priv_flags |= IFF_NO_IP_ALIGN;
  2035. #endif
  2036. dev->eth_mangle_rx = ar8216_mangle_rx;
  2037. dev->eth_mangle_tx = ar8216_mangle_tx;
  2038. }
  2039. #endif
  2040. return 0;
  2041. }
  2042. static bool
  2043. ar8xxx_check_link_states(struct ar8xxx_priv *priv)
  2044. {
  2045. bool link_new, changed = false;
  2046. u32 status;
  2047. int i;
  2048. mutex_lock(&priv->reg_mutex);
  2049. for (i = 0; i < priv->dev.ports; i++) {
  2050. status = priv->chip->read_port_status(priv, i);
  2051. link_new = !!(status & AR8216_PORT_STATUS_LINK_UP);
  2052. if (link_new == priv->link_up[i])
  2053. continue;
  2054. priv->link_up[i] = link_new;
  2055. changed = true;
  2056. /* flush ARL entries for this port if it went down*/
  2057. if (!link_new)
  2058. priv->chip->atu_flush_port(priv, i);
  2059. dev_info(&priv->phy->mdio.dev, "Port %d is %s\n",
  2060. i, link_new ? "up" : "down");
  2061. }
  2062. mutex_unlock(&priv->reg_mutex);
  2063. return changed;
  2064. }
  2065. static int
  2066. ar8xxx_phy_read_status(struct phy_device *phydev)
  2067. {
  2068. struct ar8xxx_priv *priv = phydev->priv;
  2069. struct switch_port_link link;
  2070. /* check for switch port link changes */
  2071. ar8xxx_check_link_states(priv);
  2072. if (phydev->mdio.addr != 0)
  2073. return genphy_read_status(phydev);
  2074. ar8216_read_port_link(priv, phydev->mdio.addr, &link);
  2075. phydev->link = !!link.link;
  2076. if (!phydev->link)
  2077. return 0;
  2078. switch (link.speed) {
  2079. case SWITCH_PORT_SPEED_10:
  2080. phydev->speed = SPEED_10;
  2081. break;
  2082. case SWITCH_PORT_SPEED_100:
  2083. phydev->speed = SPEED_100;
  2084. break;
  2085. case SWITCH_PORT_SPEED_1000:
  2086. phydev->speed = SPEED_1000;
  2087. break;
  2088. default:
  2089. phydev->speed = 0;
  2090. }
  2091. phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
  2092. phydev->state = PHY_RUNNING;
  2093. netif_carrier_on(phydev->attached_dev);
  2094. if (phydev->adjust_link)
  2095. phydev->adjust_link(phydev->attached_dev);
  2096. return 0;
  2097. }
  2098. static int
  2099. ar8xxx_phy_config_aneg(struct phy_device *phydev)
  2100. {
  2101. if (phydev->mdio.addr == 0)
  2102. return 0;
  2103. return genphy_config_aneg(phydev);
  2104. }
  2105. static int
  2106. ar8xxx_get_features(struct phy_device *phydev)
  2107. {
  2108. struct ar8xxx_priv *priv = phydev->priv;
  2109. linkmode_copy(phydev->supported, PHY_BASIC_FEATURES);
  2110. if (ar8xxx_has_gige(priv))
  2111. linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, phydev->supported);
  2112. return 0;
  2113. }
  2114. static const u32 ar8xxx_phy_ids[] = {
  2115. 0x004dd033,
  2116. 0x004dd034, /* AR8327 */
  2117. 0x004dd036, /* AR8337 */
  2118. 0x004dd041,
  2119. 0x004dd042,
  2120. 0x004dd043, /* AR8236 */
  2121. };
  2122. static bool
  2123. ar8xxx_phy_match(u32 phy_id)
  2124. {
  2125. int i;
  2126. for (i = 0; i < ARRAY_SIZE(ar8xxx_phy_ids); i++)
  2127. if (phy_id == ar8xxx_phy_ids[i])
  2128. return true;
  2129. return false;
  2130. }
  2131. static bool
  2132. ar8xxx_is_possible(struct mii_bus *bus)
  2133. {
  2134. unsigned int i, found_phys = 0;
  2135. for (i = 0; i < 5; i++) {
  2136. u32 phy_id;
  2137. phy_id = mdiobus_read(bus, i, MII_PHYSID1) << 16;
  2138. phy_id |= mdiobus_read(bus, i, MII_PHYSID2);
  2139. if (ar8xxx_phy_match(phy_id)) {
  2140. found_phys++;
  2141. } else if (phy_id) {
  2142. pr_debug("ar8xxx: unknown PHY at %s:%02x id:%08x\n",
  2143. dev_name(&bus->dev), i, phy_id);
  2144. }
  2145. }
  2146. return !!found_phys;
  2147. }
  2148. static int
  2149. ar8xxx_phy_probe(struct phy_device *phydev)
  2150. {
  2151. struct ar8xxx_priv *priv;
  2152. struct switch_dev *swdev;
  2153. int ret;
  2154. /* skip PHYs at unused adresses */
  2155. if (phydev->mdio.addr != 0 && phydev->mdio.addr != 3 && phydev->mdio.addr != 4)
  2156. return -ENODEV;
  2157. if (!ar8xxx_is_possible(phydev->mdio.bus))
  2158. return -ENODEV;
  2159. mutex_lock(&ar8xxx_dev_list_lock);
  2160. list_for_each_entry(priv, &ar8xxx_dev_list, list)
  2161. if (priv->mii_bus == phydev->mdio.bus)
  2162. goto found;
  2163. priv = ar8xxx_create();
  2164. if (priv == NULL) {
  2165. ret = -ENOMEM;
  2166. goto unlock;
  2167. }
  2168. priv->mii_bus = phydev->mdio.bus;
  2169. priv->pdev = &phydev->mdio.dev;
  2170. ret = of_property_read_u32(priv->pdev->of_node, "qca,mib-poll-interval",
  2171. &priv->mib_poll_interval);
  2172. if (ret)
  2173. priv->mib_poll_interval = 0;
  2174. ret = ar8xxx_id_chip(priv);
  2175. if (ret)
  2176. goto free_priv;
  2177. ret = ar8xxx_probe_switch(priv);
  2178. if (ret)
  2179. goto free_priv;
  2180. swdev = &priv->dev;
  2181. swdev->alias = dev_name(&priv->mii_bus->dev);
  2182. ret = register_switch(swdev, NULL);
  2183. if (ret)
  2184. goto free_priv;
  2185. pr_info("%s: %s rev. %u switch registered on %s\n",
  2186. swdev->devname, swdev->name, priv->chip_rev,
  2187. dev_name(&priv->mii_bus->dev));
  2188. list_add(&priv->list, &ar8xxx_dev_list);
  2189. found:
  2190. priv->use_count++;
  2191. if (phydev->mdio.addr == 0 && priv->chip->config_at_probe) {
  2192. priv->phy = phydev;
  2193. ret = ar8xxx_start(priv);
  2194. if (ret)
  2195. goto err_unregister_switch;
  2196. } else if (priv->chip->phy_rgmii_set) {
  2197. priv->chip->phy_rgmii_set(priv, phydev);
  2198. }
  2199. phydev->priv = priv;
  2200. mutex_unlock(&ar8xxx_dev_list_lock);
  2201. return 0;
  2202. err_unregister_switch:
  2203. if (--priv->use_count)
  2204. goto unlock;
  2205. unregister_switch(&priv->dev);
  2206. free_priv:
  2207. ar8xxx_free(priv);
  2208. unlock:
  2209. mutex_unlock(&ar8xxx_dev_list_lock);
  2210. return ret;
  2211. }
  2212. static void
  2213. ar8xxx_phy_detach(struct phy_device *phydev)
  2214. {
  2215. struct net_device *dev = phydev->attached_dev;
  2216. if (!dev)
  2217. return;
  2218. #ifdef CONFIG_ETHERNET_PACKET_MANGLE
  2219. dev->phy_ptr = NULL;
  2220. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6,1,0)
  2221. dev->priv_flags &= ~IFF_NO_IP_ALIGN;
  2222. #else LINUX_VERSION_CODE >= KERNEL_VERSION(5,15,0)
  2223. dev->extra_priv_flags &= ~IFF_NO_IP_ALIGN;
  2224. #endif
  2225. dev->eth_mangle_rx = NULL;
  2226. dev->eth_mangle_tx = NULL;
  2227. #endif
  2228. }
  2229. static void
  2230. ar8xxx_phy_remove(struct phy_device *phydev)
  2231. {
  2232. struct ar8xxx_priv *priv = phydev->priv;
  2233. if (WARN_ON(!priv))
  2234. return;
  2235. phydev->priv = NULL;
  2236. mutex_lock(&ar8xxx_dev_list_lock);
  2237. if (--priv->use_count > 0) {
  2238. mutex_unlock(&ar8xxx_dev_list_lock);
  2239. return;
  2240. }
  2241. list_del(&priv->list);
  2242. mutex_unlock(&ar8xxx_dev_list_lock);
  2243. unregister_switch(&priv->dev);
  2244. ar8xxx_mib_stop(priv);
  2245. ar8xxx_free(priv);
  2246. }
  2247. static struct phy_driver ar8xxx_phy_driver[] = {
  2248. {
  2249. .phy_id = 0x004d0000,
  2250. .name = "Atheros AR8216/AR8236/AR8316",
  2251. .phy_id_mask = 0xffff0000,
  2252. .probe = ar8xxx_phy_probe,
  2253. .remove = ar8xxx_phy_remove,
  2254. .detach = ar8xxx_phy_detach,
  2255. .config_init = ar8xxx_phy_config_init,
  2256. .config_aneg = ar8xxx_phy_config_aneg,
  2257. .read_status = ar8xxx_phy_read_status,
  2258. .get_features = ar8xxx_get_features,
  2259. }
  2260. };
  2261. static const struct of_device_id ar8xxx_mdiodev_of_match[] = {
  2262. {
  2263. .compatible = "qca,ar7240sw",
  2264. .data = &ar7240sw_chip,
  2265. }, {
  2266. .compatible = "qca,ar8229",
  2267. .data = &ar8229_chip,
  2268. }, {
  2269. .compatible = "qca,ar8236",
  2270. .data = &ar8236_chip,
  2271. }, {
  2272. .compatible = "qca,ar8327",
  2273. .data = &ar8327_chip,
  2274. },
  2275. { /* sentinel */ },
  2276. };
  2277. static int
  2278. ar8xxx_mdiodev_probe(struct mdio_device *mdiodev)
  2279. {
  2280. const struct of_device_id *match;
  2281. struct ar8xxx_priv *priv;
  2282. struct switch_dev *swdev;
  2283. struct device_node *mdio_node;
  2284. int ret;
  2285. match = of_match_device(ar8xxx_mdiodev_of_match, &mdiodev->dev);
  2286. if (!match)
  2287. return -EINVAL;
  2288. priv = ar8xxx_create();
  2289. if (priv == NULL)
  2290. return -ENOMEM;
  2291. priv->mii_bus = mdiodev->bus;
  2292. priv->pdev = &mdiodev->dev;
  2293. priv->chip = (const struct ar8xxx_chip *) match->data;
  2294. ret = of_property_read_u32(priv->pdev->of_node, "qca,mib-poll-interval",
  2295. &priv->mib_poll_interval);
  2296. if (ret)
  2297. priv->mib_poll_interval = 0;
  2298. ret = ar8xxx_read_id(priv);
  2299. if (ret)
  2300. goto free_priv;
  2301. ret = ar8xxx_probe_switch(priv);
  2302. if (ret)
  2303. goto free_priv;
  2304. if (priv->chip->phy_read && priv->chip->phy_write) {
  2305. priv->sw_mii_bus = devm_mdiobus_alloc(&mdiodev->dev);
  2306. priv->sw_mii_bus->name = "ar8xxx-mdio";
  2307. priv->sw_mii_bus->read = ar8xxx_phy_read;
  2308. priv->sw_mii_bus->write = ar8xxx_phy_write;
  2309. priv->sw_mii_bus->priv = priv;
  2310. priv->sw_mii_bus->parent = &mdiodev->dev;
  2311. snprintf(priv->sw_mii_bus->id, MII_BUS_ID_SIZE, "%s",
  2312. dev_name(&mdiodev->dev));
  2313. mdio_node = of_get_child_by_name(priv->pdev->of_node, "mdio-bus");
  2314. ret = of_mdiobus_register(priv->sw_mii_bus, mdio_node);
  2315. if (ret)
  2316. goto free_priv;
  2317. }
  2318. swdev = &priv->dev;
  2319. swdev->alias = dev_name(&mdiodev->dev);
  2320. if (of_property_read_bool(priv->pdev->of_node, "qca,phy4-mii-enable")) {
  2321. priv->port4_phy = true;
  2322. swdev->ports--;
  2323. }
  2324. ret = register_switch(swdev, NULL);
  2325. if (ret)
  2326. goto free_priv;
  2327. pr_info("%s: %s rev. %u switch registered on %s\n",
  2328. swdev->devname, swdev->name, priv->chip_rev,
  2329. dev_name(&priv->mii_bus->dev));
  2330. mutex_lock(&ar8xxx_dev_list_lock);
  2331. list_add(&priv->list, &ar8xxx_dev_list);
  2332. mutex_unlock(&ar8xxx_dev_list_lock);
  2333. priv->use_count++;
  2334. ret = ar8xxx_start(priv);
  2335. if (ret)
  2336. goto err_unregister_switch;
  2337. dev_set_drvdata(&mdiodev->dev, priv);
  2338. return 0;
  2339. err_unregister_switch:
  2340. if (--priv->use_count)
  2341. return ret;
  2342. unregister_switch(&priv->dev);
  2343. free_priv:
  2344. ar8xxx_free(priv);
  2345. return ret;
  2346. }
  2347. static void
  2348. ar8xxx_mdiodev_remove(struct mdio_device *mdiodev)
  2349. {
  2350. struct ar8xxx_priv *priv = dev_get_drvdata(&mdiodev->dev);
  2351. if (WARN_ON(!priv))
  2352. return;
  2353. mutex_lock(&ar8xxx_dev_list_lock);
  2354. if (--priv->use_count > 0) {
  2355. mutex_unlock(&ar8xxx_dev_list_lock);
  2356. return;
  2357. }
  2358. list_del(&priv->list);
  2359. mutex_unlock(&ar8xxx_dev_list_lock);
  2360. unregister_switch(&priv->dev);
  2361. ar8xxx_mib_stop(priv);
  2362. if(priv->sw_mii_bus)
  2363. mdiobus_unregister(priv->sw_mii_bus);
  2364. ar8xxx_free(priv);
  2365. }
  2366. static struct mdio_driver ar8xxx_mdio_driver = {
  2367. .probe = ar8xxx_mdiodev_probe,
  2368. .remove = ar8xxx_mdiodev_remove,
  2369. .mdiodrv.driver = {
  2370. .name = "ar8xxx-switch",
  2371. .of_match_table = ar8xxx_mdiodev_of_match,
  2372. },
  2373. };
  2374. static int __init ar8216_init(void)
  2375. {
  2376. int ret;
  2377. ret = phy_drivers_register(ar8xxx_phy_driver,
  2378. ARRAY_SIZE(ar8xxx_phy_driver),
  2379. THIS_MODULE);
  2380. if (ret)
  2381. return ret;
  2382. ret = mdio_driver_register(&ar8xxx_mdio_driver);
  2383. if (ret)
  2384. phy_drivers_unregister(ar8xxx_phy_driver,
  2385. ARRAY_SIZE(ar8xxx_phy_driver));
  2386. return ret;
  2387. }
  2388. module_init(ar8216_init);
  2389. static void __exit ar8216_exit(void)
  2390. {
  2391. mdio_driver_unregister(&ar8xxx_mdio_driver);
  2392. phy_drivers_unregister(ar8xxx_phy_driver,
  2393. ARRAY_SIZE(ar8xxx_phy_driver));
  2394. }
  2395. module_exit(ar8216_exit);
  2396. MODULE_LICENSE("GPL");