ar8216.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918
  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->ar8xxx_priv_volatile, 0, sizeof(priv->ar8xxx_priv_volatile));
  1168. for (i = 0; i < dev->vlans; i++)
  1169. priv->vlan_id[i] = i;
  1170. /* Configure all ports */
  1171. for (i = 0; i < dev->ports; i++)
  1172. chip->init_port(priv, i);
  1173. priv->mirror_rx = false;
  1174. priv->mirror_tx = false;
  1175. priv->source_port = 0;
  1176. priv->monitor_port = 0;
  1177. priv->arl_age_time = AR8XXX_DEFAULT_ARL_AGE_TIME;
  1178. chip->init_globals(priv);
  1179. chip->atu_flush(priv);
  1180. mutex_unlock(&priv->reg_mutex);
  1181. return chip->sw_hw_apply(dev);
  1182. }
  1183. int
  1184. ar8xxx_sw_set_reset_mibs(struct switch_dev *dev,
  1185. const struct switch_attr *attr,
  1186. struct switch_val *val)
  1187. {
  1188. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1189. unsigned int len;
  1190. int ret;
  1191. if (!ar8xxx_has_mib_counters(priv))
  1192. return -EOPNOTSUPP;
  1193. mutex_lock(&priv->mib_lock);
  1194. len = priv->dev.ports * priv->chip->num_mibs *
  1195. sizeof(*priv->mib_stats);
  1196. memset(priv->mib_stats, '\0', len);
  1197. ret = ar8xxx_mib_flush(priv);
  1198. if (ret)
  1199. goto unlock;
  1200. ret = 0;
  1201. unlock:
  1202. mutex_unlock(&priv->mib_lock);
  1203. return ret;
  1204. }
  1205. int
  1206. ar8xxx_sw_set_mib_poll_interval(struct switch_dev *dev,
  1207. const struct switch_attr *attr,
  1208. struct switch_val *val)
  1209. {
  1210. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1211. if (!ar8xxx_has_mib_counters(priv))
  1212. return -EOPNOTSUPP;
  1213. ar8xxx_mib_stop(priv);
  1214. priv->mib_poll_interval = val->value.i;
  1215. ar8xxx_mib_start(priv);
  1216. return 0;
  1217. }
  1218. int
  1219. ar8xxx_sw_get_mib_poll_interval(struct switch_dev *dev,
  1220. const struct switch_attr *attr,
  1221. struct switch_val *val)
  1222. {
  1223. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1224. if (!ar8xxx_has_mib_counters(priv))
  1225. return -EOPNOTSUPP;
  1226. val->value.i = priv->mib_poll_interval;
  1227. return 0;
  1228. }
  1229. int
  1230. ar8xxx_sw_set_mib_type(struct switch_dev *dev,
  1231. const struct switch_attr *attr,
  1232. struct switch_val *val)
  1233. {
  1234. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1235. if (!ar8xxx_has_mib_counters(priv))
  1236. return -EOPNOTSUPP;
  1237. priv->mib_type = val->value.i;
  1238. return 0;
  1239. }
  1240. int
  1241. ar8xxx_sw_get_mib_type(struct switch_dev *dev,
  1242. const struct switch_attr *attr,
  1243. struct switch_val *val)
  1244. {
  1245. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1246. if (!ar8xxx_has_mib_counters(priv))
  1247. return -EOPNOTSUPP;
  1248. val->value.i = priv->mib_type;
  1249. return 0;
  1250. }
  1251. int
  1252. ar8xxx_sw_set_mirror_rx_enable(struct switch_dev *dev,
  1253. const struct switch_attr *attr,
  1254. struct switch_val *val)
  1255. {
  1256. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1257. mutex_lock(&priv->reg_mutex);
  1258. priv->mirror_rx = !!val->value.i;
  1259. priv->chip->set_mirror_regs(priv);
  1260. mutex_unlock(&priv->reg_mutex);
  1261. return 0;
  1262. }
  1263. int
  1264. ar8xxx_sw_get_mirror_rx_enable(struct switch_dev *dev,
  1265. const struct switch_attr *attr,
  1266. struct switch_val *val)
  1267. {
  1268. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1269. val->value.i = priv->mirror_rx;
  1270. return 0;
  1271. }
  1272. int
  1273. ar8xxx_sw_set_mirror_tx_enable(struct switch_dev *dev,
  1274. const struct switch_attr *attr,
  1275. struct switch_val *val)
  1276. {
  1277. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1278. mutex_lock(&priv->reg_mutex);
  1279. priv->mirror_tx = !!val->value.i;
  1280. priv->chip->set_mirror_regs(priv);
  1281. mutex_unlock(&priv->reg_mutex);
  1282. return 0;
  1283. }
  1284. int
  1285. ar8xxx_sw_get_mirror_tx_enable(struct switch_dev *dev,
  1286. const struct switch_attr *attr,
  1287. struct switch_val *val)
  1288. {
  1289. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1290. val->value.i = priv->mirror_tx;
  1291. return 0;
  1292. }
  1293. int
  1294. ar8xxx_sw_set_mirror_monitor_port(struct switch_dev *dev,
  1295. const struct switch_attr *attr,
  1296. struct switch_val *val)
  1297. {
  1298. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1299. mutex_lock(&priv->reg_mutex);
  1300. priv->monitor_port = val->value.i;
  1301. priv->chip->set_mirror_regs(priv);
  1302. mutex_unlock(&priv->reg_mutex);
  1303. return 0;
  1304. }
  1305. int
  1306. ar8xxx_sw_get_mirror_monitor_port(struct switch_dev *dev,
  1307. const struct switch_attr *attr,
  1308. struct switch_val *val)
  1309. {
  1310. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1311. val->value.i = priv->monitor_port;
  1312. return 0;
  1313. }
  1314. int
  1315. ar8xxx_sw_set_mirror_source_port(struct switch_dev *dev,
  1316. const struct switch_attr *attr,
  1317. struct switch_val *val)
  1318. {
  1319. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1320. mutex_lock(&priv->reg_mutex);
  1321. priv->source_port = val->value.i;
  1322. priv->chip->set_mirror_regs(priv);
  1323. mutex_unlock(&priv->reg_mutex);
  1324. return 0;
  1325. }
  1326. int
  1327. ar8xxx_sw_get_mirror_source_port(struct switch_dev *dev,
  1328. const struct switch_attr *attr,
  1329. struct switch_val *val)
  1330. {
  1331. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1332. val->value.i = priv->source_port;
  1333. return 0;
  1334. }
  1335. int
  1336. ar8xxx_sw_set_port_reset_mib(struct switch_dev *dev,
  1337. const struct switch_attr *attr,
  1338. struct switch_val *val)
  1339. {
  1340. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1341. int port;
  1342. int ret;
  1343. if (!ar8xxx_has_mib_counters(priv))
  1344. return -EOPNOTSUPP;
  1345. port = val->port_vlan;
  1346. if (port >= dev->ports)
  1347. return -EINVAL;
  1348. mutex_lock(&priv->mib_lock);
  1349. ret = ar8xxx_mib_capture(priv);
  1350. if (ret)
  1351. goto unlock;
  1352. ar8xxx_mib_fetch_port_stat(priv, port, true);
  1353. ret = 0;
  1354. unlock:
  1355. mutex_unlock(&priv->mib_lock);
  1356. return ret;
  1357. }
  1358. static void
  1359. ar8xxx_byte_to_str(char *buf, int len, u64 byte)
  1360. {
  1361. unsigned long b;
  1362. const char *unit;
  1363. if (byte >= 0x40000000) { /* 1 GiB */
  1364. b = byte * 10 / 0x40000000;
  1365. unit = "GiB";
  1366. } else if (byte >= 0x100000) { /* 1 MiB */
  1367. b = byte * 10 / 0x100000;
  1368. unit = "MiB";
  1369. } else if (byte >= 0x400) { /* 1 KiB */
  1370. b = byte * 10 / 0x400;
  1371. unit = "KiB";
  1372. } else {
  1373. b = byte;
  1374. unit = "Byte";
  1375. }
  1376. if (strcmp(unit, "Byte"))
  1377. snprintf(buf, len, "%lu.%lu %s", b / 10, b % 10, unit);
  1378. else
  1379. snprintf(buf, len, "%lu %s", b, unit);
  1380. }
  1381. int
  1382. ar8xxx_sw_get_port_mib(struct switch_dev *dev,
  1383. const struct switch_attr *attr,
  1384. struct switch_val *val)
  1385. {
  1386. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1387. const struct ar8xxx_chip *chip = priv->chip;
  1388. u64 *mib_stats, mib_data;
  1389. unsigned int port;
  1390. int ret;
  1391. char *buf = priv->buf;
  1392. char buf1[64];
  1393. const char *mib_name;
  1394. int i, len = 0;
  1395. bool mib_stats_empty = true;
  1396. if (!ar8xxx_has_mib_counters(priv) || !priv->mib_poll_interval)
  1397. return -EOPNOTSUPP;
  1398. port = val->port_vlan;
  1399. if (port >= dev->ports)
  1400. return -EINVAL;
  1401. mutex_lock(&priv->mib_lock);
  1402. ret = ar8xxx_mib_capture(priv);
  1403. if (ret)
  1404. goto unlock;
  1405. ar8xxx_mib_fetch_port_stat(priv, port, false);
  1406. len += snprintf(buf + len, sizeof(priv->buf) - len,
  1407. "MIB counters\n");
  1408. mib_stats = &priv->mib_stats[port * chip->num_mibs];
  1409. for (i = 0; i < chip->num_mibs; i++) {
  1410. if (chip->mib_decs[i].type > priv->mib_type)
  1411. continue;
  1412. mib_name = chip->mib_decs[i].name;
  1413. mib_data = mib_stats[i];
  1414. len += snprintf(buf + len, sizeof(priv->buf) - len,
  1415. "%-12s: %llu\n", mib_name, mib_data);
  1416. if ((!strcmp(mib_name, "TxByte") ||
  1417. !strcmp(mib_name, "RxGoodByte")) &&
  1418. mib_data >= 1024) {
  1419. ar8xxx_byte_to_str(buf1, sizeof(buf1), mib_data);
  1420. --len; /* discard newline at the end of buf */
  1421. len += snprintf(buf + len, sizeof(priv->buf) - len,
  1422. " (%s)\n", buf1);
  1423. }
  1424. if (mib_stats_empty && mib_data)
  1425. mib_stats_empty = false;
  1426. }
  1427. if (mib_stats_empty)
  1428. len = snprintf(buf, sizeof(priv->buf), "No MIB data");
  1429. val->value.s = buf;
  1430. val->len = len;
  1431. ret = 0;
  1432. unlock:
  1433. mutex_unlock(&priv->mib_lock);
  1434. return ret;
  1435. }
  1436. int
  1437. ar8xxx_sw_set_arl_age_time(struct switch_dev *dev, const struct switch_attr *attr,
  1438. struct switch_val *val)
  1439. {
  1440. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1441. int age_time = val->value.i;
  1442. u32 age_time_val;
  1443. if (age_time < 0)
  1444. return -EINVAL;
  1445. age_time_val = ar8xxx_age_time_val(age_time);
  1446. if (age_time_val == 0 || age_time_val > 0xffff)
  1447. return -EINVAL;
  1448. priv->arl_age_time = age_time;
  1449. return 0;
  1450. }
  1451. int
  1452. ar8xxx_sw_get_arl_age_time(struct switch_dev *dev, const struct switch_attr *attr,
  1453. struct switch_val *val)
  1454. {
  1455. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1456. val->value.i = priv->arl_age_time;
  1457. return 0;
  1458. }
  1459. int
  1460. ar8xxx_sw_get_arl_table(struct switch_dev *dev,
  1461. const struct switch_attr *attr,
  1462. struct switch_val *val)
  1463. {
  1464. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1465. struct mii_bus *bus = priv->mii_bus;
  1466. const struct ar8xxx_chip *chip = priv->chip;
  1467. char *buf = priv->arl_buf;
  1468. int i, j, k, len = 0;
  1469. struct arl_entry *a, *a1;
  1470. u32 status;
  1471. if (!chip->get_arl_entry)
  1472. return -EOPNOTSUPP;
  1473. mutex_lock(&priv->reg_mutex);
  1474. mutex_lock(&bus->mdio_lock);
  1475. chip->get_arl_entry(priv, NULL, NULL, AR8XXX_ARL_INITIALIZE);
  1476. for(i = 0; i < AR8XXX_NUM_ARL_RECORDS; ++i) {
  1477. a = &priv->arl_table[i];
  1478. duplicate:
  1479. chip->get_arl_entry(priv, a, &status, AR8XXX_ARL_GET_NEXT);
  1480. if (!status)
  1481. break;
  1482. /* avoid duplicates
  1483. * ARL table can include multiple valid entries
  1484. * per MAC, just with differing status codes
  1485. */
  1486. for (j = 0; j < i; ++j) {
  1487. a1 = &priv->arl_table[j];
  1488. if (!memcmp(a->mac, a1->mac, sizeof(a->mac))) {
  1489. /* ignore ports already seen in former entry */
  1490. a->portmap &= ~a1->portmap;
  1491. if (!a->portmap)
  1492. goto duplicate;
  1493. }
  1494. }
  1495. }
  1496. mutex_unlock(&bus->mdio_lock);
  1497. len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
  1498. "address resolution table\n");
  1499. if (i == AR8XXX_NUM_ARL_RECORDS)
  1500. len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
  1501. "Too many entries found, displaying the first %d only!\n",
  1502. AR8XXX_NUM_ARL_RECORDS);
  1503. for (j = 0; j < priv->dev.ports; ++j) {
  1504. for (k = 0; k < i; ++k) {
  1505. a = &priv->arl_table[k];
  1506. if (!(a->portmap & BIT(j)))
  1507. continue;
  1508. len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
  1509. "Port %d: MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  1510. j,
  1511. a->mac[5], a->mac[4], a->mac[3],
  1512. a->mac[2], a->mac[1], a->mac[0]);
  1513. }
  1514. }
  1515. val->value.s = buf;
  1516. val->len = len;
  1517. mutex_unlock(&priv->reg_mutex);
  1518. return 0;
  1519. }
  1520. int
  1521. ar8xxx_sw_set_flush_arl_table(struct switch_dev *dev,
  1522. const struct switch_attr *attr,
  1523. struct switch_val *val)
  1524. {
  1525. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1526. int ret;
  1527. mutex_lock(&priv->reg_mutex);
  1528. ret = priv->chip->atu_flush(priv);
  1529. mutex_unlock(&priv->reg_mutex);
  1530. return ret;
  1531. }
  1532. int
  1533. ar8xxx_sw_set_flush_port_arl_table(struct switch_dev *dev,
  1534. const struct switch_attr *attr,
  1535. struct switch_val *val)
  1536. {
  1537. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1538. int port, ret;
  1539. port = val->port_vlan;
  1540. if (port >= dev->ports)
  1541. return -EINVAL;
  1542. mutex_lock(&priv->reg_mutex);
  1543. ret = priv->chip->atu_flush_port(priv, port);
  1544. mutex_unlock(&priv->reg_mutex);
  1545. return ret;
  1546. }
  1547. int
  1548. ar8xxx_sw_get_port_stats(struct switch_dev *dev, int port,
  1549. struct switch_port_stats *stats)
  1550. {
  1551. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1552. u64 *mib_stats;
  1553. if (!ar8xxx_has_mib_counters(priv) || !priv->mib_poll_interval)
  1554. return -EOPNOTSUPP;
  1555. if (!(priv->chip->mib_rxb_id || priv->chip->mib_txb_id))
  1556. return -EOPNOTSUPP;
  1557. if (port >= dev->ports)
  1558. return -EINVAL;
  1559. mutex_lock(&priv->mib_lock);
  1560. mib_stats = &priv->mib_stats[port * priv->chip->num_mibs];
  1561. stats->tx_bytes = mib_stats[priv->chip->mib_txb_id];
  1562. stats->rx_bytes = mib_stats[priv->chip->mib_rxb_id];
  1563. mutex_unlock(&priv->mib_lock);
  1564. return 0;
  1565. }
  1566. static int
  1567. ar8xxx_phy_read(struct mii_bus *bus, int phy_addr, int reg_addr)
  1568. {
  1569. struct ar8xxx_priv *priv = bus->priv;
  1570. return priv->chip->phy_read(priv, phy_addr, reg_addr);
  1571. }
  1572. static int
  1573. ar8xxx_phy_write(struct mii_bus *bus, int phy_addr, int reg_addr,
  1574. u16 reg_val)
  1575. {
  1576. struct ar8xxx_priv *priv = bus->priv;
  1577. return priv->chip->phy_write(priv, phy_addr, reg_addr, reg_val);
  1578. }
  1579. static const struct switch_attr ar8xxx_sw_attr_globals[] = {
  1580. {
  1581. .type = SWITCH_TYPE_INT,
  1582. .name = "enable_vlan",
  1583. .description = "Enable VLAN mode",
  1584. .set = ar8xxx_sw_set_vlan,
  1585. .get = ar8xxx_sw_get_vlan,
  1586. .max = 1
  1587. },
  1588. {
  1589. .type = SWITCH_TYPE_NOVAL,
  1590. .name = "reset_mibs",
  1591. .description = "Reset all MIB counters",
  1592. .set = ar8xxx_sw_set_reset_mibs,
  1593. },
  1594. {
  1595. .type = SWITCH_TYPE_INT,
  1596. .name = "ar8xxx_mib_poll_interval",
  1597. .description = "MIB polling interval in msecs (0 to disable)",
  1598. .set = ar8xxx_sw_set_mib_poll_interval,
  1599. .get = ar8xxx_sw_get_mib_poll_interval
  1600. },
  1601. {
  1602. .type = SWITCH_TYPE_INT,
  1603. .name = "ar8xxx_mib_type",
  1604. .description = "MIB type (0=basic 1=extended)",
  1605. .set = ar8xxx_sw_set_mib_type,
  1606. .get = ar8xxx_sw_get_mib_type
  1607. },
  1608. {
  1609. .type = SWITCH_TYPE_INT,
  1610. .name = "enable_mirror_rx",
  1611. .description = "Enable mirroring of RX packets",
  1612. .set = ar8xxx_sw_set_mirror_rx_enable,
  1613. .get = ar8xxx_sw_get_mirror_rx_enable,
  1614. .max = 1
  1615. },
  1616. {
  1617. .type = SWITCH_TYPE_INT,
  1618. .name = "enable_mirror_tx",
  1619. .description = "Enable mirroring of TX packets",
  1620. .set = ar8xxx_sw_set_mirror_tx_enable,
  1621. .get = ar8xxx_sw_get_mirror_tx_enable,
  1622. .max = 1
  1623. },
  1624. {
  1625. .type = SWITCH_TYPE_INT,
  1626. .name = "mirror_monitor_port",
  1627. .description = "Mirror monitor port",
  1628. .set = ar8xxx_sw_set_mirror_monitor_port,
  1629. .get = ar8xxx_sw_get_mirror_monitor_port,
  1630. .max = AR8216_NUM_PORTS - 1
  1631. },
  1632. {
  1633. .type = SWITCH_TYPE_INT,
  1634. .name = "mirror_source_port",
  1635. .description = "Mirror source port",
  1636. .set = ar8xxx_sw_set_mirror_source_port,
  1637. .get = ar8xxx_sw_get_mirror_source_port,
  1638. .max = AR8216_NUM_PORTS - 1
  1639. },
  1640. {
  1641. .type = SWITCH_TYPE_STRING,
  1642. .name = "arl_table",
  1643. .description = "Get ARL table",
  1644. .set = NULL,
  1645. .get = ar8xxx_sw_get_arl_table,
  1646. },
  1647. {
  1648. .type = SWITCH_TYPE_NOVAL,
  1649. .name = "flush_arl_table",
  1650. .description = "Flush ARL table",
  1651. .set = ar8xxx_sw_set_flush_arl_table,
  1652. },
  1653. };
  1654. const struct switch_attr ar8xxx_sw_attr_port[] = {
  1655. {
  1656. .type = SWITCH_TYPE_NOVAL,
  1657. .name = "reset_mib",
  1658. .description = "Reset single port MIB counters",
  1659. .set = ar8xxx_sw_set_port_reset_mib,
  1660. },
  1661. {
  1662. .type = SWITCH_TYPE_STRING,
  1663. .name = "mib",
  1664. .description = "Get port's MIB counters",
  1665. .set = NULL,
  1666. .get = ar8xxx_sw_get_port_mib,
  1667. },
  1668. {
  1669. .type = SWITCH_TYPE_NOVAL,
  1670. .name = "flush_arl_table",
  1671. .description = "Flush port's ARL table entries",
  1672. .set = ar8xxx_sw_set_flush_port_arl_table,
  1673. },
  1674. };
  1675. const struct switch_attr ar8xxx_sw_attr_vlan[1] = {
  1676. {
  1677. .type = SWITCH_TYPE_INT,
  1678. .name = "vid",
  1679. .description = "VLAN ID (0-4094)",
  1680. .set = ar8xxx_sw_set_vid,
  1681. .get = ar8xxx_sw_get_vid,
  1682. .max = 4094,
  1683. },
  1684. };
  1685. static const struct switch_dev_ops ar8xxx_sw_ops = {
  1686. .attr_global = {
  1687. .attr = ar8xxx_sw_attr_globals,
  1688. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_globals),
  1689. },
  1690. .attr_port = {
  1691. .attr = ar8xxx_sw_attr_port,
  1692. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
  1693. },
  1694. .attr_vlan = {
  1695. .attr = ar8xxx_sw_attr_vlan,
  1696. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  1697. },
  1698. .get_port_pvid = ar8xxx_sw_get_pvid,
  1699. .set_port_pvid = ar8xxx_sw_set_pvid,
  1700. .get_vlan_ports = ar8xxx_sw_get_ports,
  1701. .set_vlan_ports = ar8xxx_sw_set_ports,
  1702. .apply_config = ar8xxx_sw_hw_apply,
  1703. .reset_switch = ar8xxx_sw_reset_switch,
  1704. .get_port_link = ar8xxx_sw_get_port_link,
  1705. .get_port_stats = ar8xxx_sw_get_port_stats,
  1706. };
  1707. static const struct ar8xxx_chip ar7240sw_chip = {
  1708. .caps = AR8XXX_CAP_MIB_COUNTERS,
  1709. .reg_port_stats_start = 0x20000,
  1710. .reg_port_stats_length = 0x100,
  1711. .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
  1712. .name = "Atheros AR724X/AR933X built-in",
  1713. .ports = AR7240SW_NUM_PORTS,
  1714. .vlans = AR8216_NUM_VLANS,
  1715. .swops = &ar8xxx_sw_ops,
  1716. .hw_init = ar7240sw_hw_init,
  1717. .init_globals = ar7240sw_init_globals,
  1718. .init_port = ar8229_init_port,
  1719. .phy_read = ar8216_phy_read,
  1720. .phy_write = ar8216_phy_write,
  1721. .setup_port = ar7240sw_setup_port,
  1722. .read_port_status = ar8216_read_port_status,
  1723. .atu_flush = ar8216_atu_flush,
  1724. .atu_flush_port = ar8216_atu_flush_port,
  1725. .vtu_flush = ar8216_vtu_flush,
  1726. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1727. .set_mirror_regs = ar8216_set_mirror_regs,
  1728. .get_arl_entry = ar8216_get_arl_entry,
  1729. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1730. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1731. .mib_decs = ar8236_mibs,
  1732. .mib_func = AR8216_REG_MIB_FUNC,
  1733. .mib_rxb_id = AR8236_MIB_RXB_ID,
  1734. .mib_txb_id = AR8236_MIB_TXB_ID,
  1735. };
  1736. static const struct ar8xxx_chip ar8216_chip = {
  1737. .caps = AR8XXX_CAP_MIB_COUNTERS,
  1738. .reg_port_stats_start = 0x19000,
  1739. .reg_port_stats_length = 0xa0,
  1740. .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
  1741. .name = "Atheros AR8216",
  1742. .ports = AR8216_NUM_PORTS,
  1743. .vlans = AR8216_NUM_VLANS,
  1744. .swops = &ar8xxx_sw_ops,
  1745. .hw_init = ar8216_hw_init,
  1746. .init_globals = ar8216_init_globals,
  1747. .init_port = ar8216_init_port,
  1748. .setup_port = ar8216_setup_port,
  1749. .read_port_status = ar8216_read_port_status,
  1750. .atu_flush = ar8216_atu_flush,
  1751. .atu_flush_port = ar8216_atu_flush_port,
  1752. .vtu_flush = ar8216_vtu_flush,
  1753. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1754. .set_mirror_regs = ar8216_set_mirror_regs,
  1755. .get_arl_entry = ar8216_get_arl_entry,
  1756. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1757. .num_mibs = ARRAY_SIZE(ar8216_mibs),
  1758. .mib_decs = ar8216_mibs,
  1759. .mib_func = AR8216_REG_MIB_FUNC,
  1760. .mib_rxb_id = AR8216_MIB_RXB_ID,
  1761. .mib_txb_id = AR8216_MIB_TXB_ID,
  1762. };
  1763. static const struct ar8xxx_chip ar8229_chip = {
  1764. .caps = AR8XXX_CAP_MIB_COUNTERS,
  1765. .reg_port_stats_start = 0x20000,
  1766. .reg_port_stats_length = 0x100,
  1767. .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
  1768. .name = "Atheros AR8229",
  1769. .ports = AR8216_NUM_PORTS,
  1770. .vlans = AR8216_NUM_VLANS,
  1771. .swops = &ar8xxx_sw_ops,
  1772. .hw_init = ar8229_hw_init,
  1773. .init_globals = ar8229_init_globals,
  1774. .init_port = ar8229_init_port,
  1775. .phy_read = ar8216_phy_read,
  1776. .phy_write = ar8216_phy_write,
  1777. .setup_port = ar8236_setup_port,
  1778. .read_port_status = ar8216_read_port_status,
  1779. .atu_flush = ar8216_atu_flush,
  1780. .atu_flush_port = ar8216_atu_flush_port,
  1781. .vtu_flush = ar8216_vtu_flush,
  1782. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1783. .set_mirror_regs = ar8216_set_mirror_regs,
  1784. .get_arl_entry = ar8216_get_arl_entry,
  1785. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1786. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1787. .mib_decs = ar8236_mibs,
  1788. .mib_func = AR8216_REG_MIB_FUNC,
  1789. .mib_rxb_id = AR8236_MIB_RXB_ID,
  1790. .mib_txb_id = AR8236_MIB_TXB_ID,
  1791. };
  1792. static const struct ar8xxx_chip ar8236_chip = {
  1793. .caps = AR8XXX_CAP_MIB_COUNTERS,
  1794. .reg_port_stats_start = 0x20000,
  1795. .reg_port_stats_length = 0x100,
  1796. .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
  1797. .name = "Atheros AR8236",
  1798. .ports = AR8216_NUM_PORTS,
  1799. .vlans = AR8216_NUM_VLANS,
  1800. .swops = &ar8xxx_sw_ops,
  1801. .hw_init = ar8216_hw_init,
  1802. .init_globals = ar8236_init_globals,
  1803. .init_port = ar8216_init_port,
  1804. .setup_port = ar8236_setup_port,
  1805. .read_port_status = ar8216_read_port_status,
  1806. .atu_flush = ar8216_atu_flush,
  1807. .atu_flush_port = ar8216_atu_flush_port,
  1808. .vtu_flush = ar8216_vtu_flush,
  1809. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1810. .set_mirror_regs = ar8216_set_mirror_regs,
  1811. .get_arl_entry = ar8216_get_arl_entry,
  1812. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1813. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1814. .mib_decs = ar8236_mibs,
  1815. .mib_func = AR8216_REG_MIB_FUNC,
  1816. .mib_rxb_id = AR8236_MIB_RXB_ID,
  1817. .mib_txb_id = AR8236_MIB_TXB_ID,
  1818. };
  1819. static const struct ar8xxx_chip ar8316_chip = {
  1820. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1821. .reg_port_stats_start = 0x20000,
  1822. .reg_port_stats_length = 0x100,
  1823. .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
  1824. .name = "Atheros AR8316",
  1825. .ports = AR8216_NUM_PORTS,
  1826. .vlans = AR8X16_MAX_VLANS,
  1827. .swops = &ar8xxx_sw_ops,
  1828. .hw_init = ar8316_hw_init,
  1829. .init_globals = ar8316_init_globals,
  1830. .init_port = ar8216_init_port,
  1831. .setup_port = ar8216_setup_port,
  1832. .read_port_status = ar8216_read_port_status,
  1833. .atu_flush = ar8216_atu_flush,
  1834. .atu_flush_port = ar8216_atu_flush_port,
  1835. .vtu_flush = ar8216_vtu_flush,
  1836. .vtu_load_vlan = ar8216_vtu_load_vlan,
  1837. .set_mirror_regs = ar8216_set_mirror_regs,
  1838. .get_arl_entry = ar8216_get_arl_entry,
  1839. .sw_hw_apply = ar8xxx_sw_hw_apply,
  1840. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1841. .mib_decs = ar8236_mibs,
  1842. .mib_func = AR8216_REG_MIB_FUNC,
  1843. .mib_rxb_id = AR8236_MIB_RXB_ID,
  1844. .mib_txb_id = AR8236_MIB_TXB_ID,
  1845. };
  1846. static int
  1847. ar8xxx_read_id(struct ar8xxx_priv *priv)
  1848. {
  1849. u32 val;
  1850. u16 id;
  1851. int i;
  1852. val = ar8xxx_read(priv, AR8216_REG_CTRL);
  1853. if (val == ~0)
  1854. return -ENODEV;
  1855. id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  1856. for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
  1857. u16 t;
  1858. val = ar8xxx_read(priv, AR8216_REG_CTRL);
  1859. if (val == ~0)
  1860. return -ENODEV;
  1861. t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  1862. if (t != id)
  1863. return -ENODEV;
  1864. }
  1865. priv->chip_ver = (id & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
  1866. priv->chip_rev = (id & AR8216_CTRL_REVISION);
  1867. return 0;
  1868. }
  1869. static int
  1870. ar8xxx_id_chip(struct ar8xxx_priv *priv)
  1871. {
  1872. int ret;
  1873. ret = ar8xxx_read_id(priv);
  1874. if(ret)
  1875. return ret;
  1876. switch (priv->chip_ver) {
  1877. case AR8XXX_VER_AR8216:
  1878. priv->chip = &ar8216_chip;
  1879. break;
  1880. case AR8XXX_VER_AR8236:
  1881. priv->chip = &ar8236_chip;
  1882. break;
  1883. case AR8XXX_VER_AR8316:
  1884. priv->chip = &ar8316_chip;
  1885. break;
  1886. case AR8XXX_VER_AR8327:
  1887. priv->chip = &ar8327_chip;
  1888. break;
  1889. case AR8XXX_VER_AR8337:
  1890. priv->chip = &ar8337_chip;
  1891. break;
  1892. default:
  1893. pr_err("ar8216: Unknown Atheros device [ver=%d, rev=%d]\n",
  1894. priv->chip_ver, priv->chip_rev);
  1895. return -ENODEV;
  1896. }
  1897. return 0;
  1898. }
  1899. static void
  1900. ar8xxx_mib_work_func(struct work_struct *work)
  1901. {
  1902. struct ar8xxx_priv *priv;
  1903. int err, i;
  1904. priv = container_of(work, struct ar8xxx_priv, mib_work.work);
  1905. mutex_lock(&priv->mib_lock);
  1906. err = ar8xxx_mib_capture(priv);
  1907. if (err)
  1908. goto next_attempt;
  1909. for (i = 0; i < priv->dev.ports; i++)
  1910. ar8xxx_mib_fetch_port_stat(priv, i, false);
  1911. next_attempt:
  1912. mutex_unlock(&priv->mib_lock);
  1913. schedule_delayed_work(&priv->mib_work,
  1914. msecs_to_jiffies(priv->mib_poll_interval));
  1915. }
  1916. static int
  1917. ar8xxx_mib_init(struct ar8xxx_priv *priv)
  1918. {
  1919. unsigned int len;
  1920. if (!ar8xxx_has_mib_counters(priv))
  1921. return 0;
  1922. BUG_ON(!priv->chip->mib_decs || !priv->chip->num_mibs);
  1923. len = priv->dev.ports * priv->chip->num_mibs *
  1924. sizeof(*priv->mib_stats);
  1925. priv->mib_stats = kzalloc(len, GFP_KERNEL);
  1926. if (!priv->mib_stats)
  1927. return -ENOMEM;
  1928. return 0;
  1929. }
  1930. static void
  1931. ar8xxx_mib_start(struct ar8xxx_priv *priv)
  1932. {
  1933. if (!ar8xxx_has_mib_counters(priv) || !priv->mib_poll_interval)
  1934. return;
  1935. schedule_delayed_work(&priv->mib_work,
  1936. msecs_to_jiffies(priv->mib_poll_interval));
  1937. }
  1938. static void
  1939. ar8xxx_mib_stop(struct ar8xxx_priv *priv)
  1940. {
  1941. if (!ar8xxx_has_mib_counters(priv) || !priv->mib_poll_interval)
  1942. return;
  1943. cancel_delayed_work_sync(&priv->mib_work);
  1944. }
  1945. static struct ar8xxx_priv *
  1946. ar8xxx_create(void)
  1947. {
  1948. struct ar8xxx_priv *priv;
  1949. priv = kzalloc(sizeof(struct ar8xxx_priv), GFP_KERNEL);
  1950. if (priv == NULL)
  1951. return NULL;
  1952. mutex_init(&priv->reg_mutex);
  1953. mutex_init(&priv->mib_lock);
  1954. INIT_DELAYED_WORK(&priv->mib_work, ar8xxx_mib_work_func);
  1955. return priv;
  1956. }
  1957. static void
  1958. ar8xxx_free(struct ar8xxx_priv *priv)
  1959. {
  1960. if (priv->chip && priv->chip->cleanup)
  1961. priv->chip->cleanup(priv);
  1962. kfree(priv->chip_data);
  1963. kfree(priv->mib_stats);
  1964. kfree(priv);
  1965. }
  1966. static int
  1967. ar8xxx_probe_switch(struct ar8xxx_priv *priv)
  1968. {
  1969. const struct ar8xxx_chip *chip;
  1970. struct switch_dev *swdev;
  1971. int ret;
  1972. chip = priv->chip;
  1973. swdev = &priv->dev;
  1974. swdev->cpu_port = AR8216_PORT_CPU;
  1975. swdev->name = chip->name;
  1976. swdev->vlans = chip->vlans;
  1977. swdev->ports = chip->ports;
  1978. swdev->ops = chip->swops;
  1979. ret = ar8xxx_mib_init(priv);
  1980. if (ret)
  1981. return ret;
  1982. return 0;
  1983. }
  1984. static int
  1985. ar8xxx_start(struct ar8xxx_priv *priv)
  1986. {
  1987. int ret;
  1988. priv->init = true;
  1989. ret = priv->chip->hw_init(priv);
  1990. if (ret)
  1991. return ret;
  1992. ret = ar8xxx_sw_reset_switch(&priv->dev);
  1993. if (ret)
  1994. return ret;
  1995. priv->init = false;
  1996. ar8xxx_mib_start(priv);
  1997. return 0;
  1998. }
  1999. static int
  2000. ar8xxx_phy_config_init(struct phy_device *phydev)
  2001. {
  2002. struct ar8xxx_priv *priv = phydev->priv;
  2003. #ifdef CONFIG_ETHERNET_PACKET_MANGLE
  2004. struct net_device *dev = phydev->attached_dev;
  2005. #endif
  2006. int ret;
  2007. if (WARN_ON(!priv))
  2008. return -ENODEV;
  2009. if (priv->chip->config_at_probe)
  2010. return ar8xxx_phy_check_aneg(phydev);
  2011. priv->phy = phydev;
  2012. if (phydev->mdio.addr != 0) {
  2013. if (chip_is_ar8316(priv)) {
  2014. /* switch device has been initialized, reinit */
  2015. priv->dev.ports = (AR8216_NUM_PORTS - 1);
  2016. priv->initialized = false;
  2017. priv->port4_phy = true;
  2018. ar8316_hw_init(priv);
  2019. return 0;
  2020. }
  2021. return 0;
  2022. }
  2023. ret = ar8xxx_start(priv);
  2024. if (ret)
  2025. return ret;
  2026. #ifdef CONFIG_ETHERNET_PACKET_MANGLE
  2027. /* VID fixup only needed on ar8216 */
  2028. if (chip_is_ar8216(priv)) {
  2029. dev->phy_ptr = priv;
  2030. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6,1,0)
  2031. dev->priv_flags |= IFF_NO_IP_ALIGN;
  2032. #else
  2033. dev->extra_priv_flags |= IFF_NO_IP_ALIGN;
  2034. #endif
  2035. dev->eth_mangle_rx = ar8216_mangle_rx;
  2036. dev->eth_mangle_tx = ar8216_mangle_tx;
  2037. }
  2038. #endif
  2039. return 0;
  2040. }
  2041. static bool
  2042. ar8xxx_check_link_states(struct ar8xxx_priv *priv)
  2043. {
  2044. bool link_new, changed = false;
  2045. u32 status;
  2046. int i;
  2047. mutex_lock(&priv->reg_mutex);
  2048. for (i = 0; i < priv->dev.ports; i++) {
  2049. status = priv->chip->read_port_status(priv, i);
  2050. link_new = !!(status & AR8216_PORT_STATUS_LINK_UP);
  2051. if (link_new == priv->link_up[i])
  2052. continue;
  2053. priv->link_up[i] = link_new;
  2054. changed = true;
  2055. /* flush ARL entries for this port if it went down*/
  2056. if (!link_new)
  2057. priv->chip->atu_flush_port(priv, i);
  2058. dev_info(&priv->phy->mdio.dev, "Port %d is %s\n",
  2059. i, link_new ? "up" : "down");
  2060. }
  2061. mutex_unlock(&priv->reg_mutex);
  2062. return changed;
  2063. }
  2064. static int
  2065. ar8xxx_phy_read_status(struct phy_device *phydev)
  2066. {
  2067. struct ar8xxx_priv *priv = phydev->priv;
  2068. struct switch_port_link link;
  2069. /* check for switch port link changes */
  2070. ar8xxx_check_link_states(priv);
  2071. if (phydev->mdio.addr != 0)
  2072. return genphy_read_status(phydev);
  2073. ar8216_read_port_link(priv, phydev->mdio.addr, &link);
  2074. phydev->link = !!link.link;
  2075. if (!phydev->link)
  2076. return 0;
  2077. switch (link.speed) {
  2078. case SWITCH_PORT_SPEED_10:
  2079. phydev->speed = SPEED_10;
  2080. break;
  2081. case SWITCH_PORT_SPEED_100:
  2082. phydev->speed = SPEED_100;
  2083. break;
  2084. case SWITCH_PORT_SPEED_1000:
  2085. phydev->speed = SPEED_1000;
  2086. break;
  2087. default:
  2088. phydev->speed = 0;
  2089. }
  2090. phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
  2091. phydev->state = PHY_RUNNING;
  2092. netif_carrier_on(phydev->attached_dev);
  2093. if (phydev->adjust_link)
  2094. phydev->adjust_link(phydev->attached_dev);
  2095. return 0;
  2096. }
  2097. static int
  2098. ar8xxx_phy_config_aneg(struct phy_device *phydev)
  2099. {
  2100. if (phydev->mdio.addr == 0)
  2101. return 0;
  2102. return genphy_config_aneg(phydev);
  2103. }
  2104. static int
  2105. ar8xxx_get_features(struct phy_device *phydev)
  2106. {
  2107. struct ar8xxx_priv *priv = phydev->priv;
  2108. linkmode_copy(phydev->supported, PHY_BASIC_FEATURES);
  2109. if (ar8xxx_has_gige(priv))
  2110. linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, phydev->supported);
  2111. return 0;
  2112. }
  2113. static const u32 ar8xxx_phy_ids[] = {
  2114. 0x004dd033,
  2115. 0x004dd034, /* AR8327 */
  2116. 0x004dd036, /* AR8337 */
  2117. 0x004dd041,
  2118. 0x004dd042,
  2119. 0x004dd043, /* AR8236 */
  2120. };
  2121. static bool
  2122. ar8xxx_phy_match(u32 phy_id)
  2123. {
  2124. int i;
  2125. for (i = 0; i < ARRAY_SIZE(ar8xxx_phy_ids); i++)
  2126. if (phy_id == ar8xxx_phy_ids[i])
  2127. return true;
  2128. return false;
  2129. }
  2130. static bool
  2131. ar8xxx_is_possible(struct mii_bus *bus)
  2132. {
  2133. unsigned int i, found_phys = 0;
  2134. for (i = 0; i < 5; i++) {
  2135. u32 phy_id;
  2136. phy_id = mdiobus_read(bus, i, MII_PHYSID1) << 16;
  2137. phy_id |= mdiobus_read(bus, i, MII_PHYSID2);
  2138. if (ar8xxx_phy_match(phy_id)) {
  2139. found_phys++;
  2140. } else if (phy_id) {
  2141. pr_debug("ar8xxx: unknown PHY at %s:%02x id:%08x\n",
  2142. dev_name(&bus->dev), i, phy_id);
  2143. }
  2144. }
  2145. return !!found_phys;
  2146. }
  2147. static int
  2148. ar8xxx_phy_probe(struct phy_device *phydev)
  2149. {
  2150. struct ar8xxx_priv *priv;
  2151. struct switch_dev *swdev;
  2152. int ret;
  2153. /* skip PHYs at unused adresses */
  2154. if (phydev->mdio.addr != 0 && phydev->mdio.addr != 3 && phydev->mdio.addr != 4)
  2155. return -ENODEV;
  2156. if (!ar8xxx_is_possible(phydev->mdio.bus))
  2157. return -ENODEV;
  2158. mutex_lock(&ar8xxx_dev_list_lock);
  2159. list_for_each_entry(priv, &ar8xxx_dev_list, list)
  2160. if (priv->mii_bus == phydev->mdio.bus)
  2161. goto found;
  2162. priv = ar8xxx_create();
  2163. if (priv == NULL) {
  2164. ret = -ENOMEM;
  2165. goto unlock;
  2166. }
  2167. priv->mii_bus = phydev->mdio.bus;
  2168. priv->pdev = &phydev->mdio.dev;
  2169. ret = of_property_read_u32(priv->pdev->of_node, "qca,mib-poll-interval",
  2170. &priv->mib_poll_interval);
  2171. if (ret)
  2172. priv->mib_poll_interval = 0;
  2173. ret = ar8xxx_id_chip(priv);
  2174. if (ret)
  2175. goto free_priv;
  2176. ret = ar8xxx_probe_switch(priv);
  2177. if (ret)
  2178. goto free_priv;
  2179. swdev = &priv->dev;
  2180. swdev->alias = dev_name(&priv->mii_bus->dev);
  2181. ret = register_switch(swdev, NULL);
  2182. if (ret)
  2183. goto free_priv;
  2184. pr_info("%s: %s rev. %u switch registered on %s\n",
  2185. swdev->devname, swdev->name, priv->chip_rev,
  2186. dev_name(&priv->mii_bus->dev));
  2187. list_add(&priv->list, &ar8xxx_dev_list);
  2188. found:
  2189. priv->use_count++;
  2190. if (phydev->mdio.addr == 0 && priv->chip->config_at_probe) {
  2191. priv->phy = phydev;
  2192. ret = ar8xxx_start(priv);
  2193. if (ret)
  2194. goto err_unregister_switch;
  2195. } else if (priv->chip->phy_rgmii_set) {
  2196. priv->chip->phy_rgmii_set(priv, phydev);
  2197. }
  2198. phydev->priv = priv;
  2199. mutex_unlock(&ar8xxx_dev_list_lock);
  2200. return 0;
  2201. err_unregister_switch:
  2202. if (--priv->use_count)
  2203. goto unlock;
  2204. unregister_switch(&priv->dev);
  2205. free_priv:
  2206. ar8xxx_free(priv);
  2207. unlock:
  2208. mutex_unlock(&ar8xxx_dev_list_lock);
  2209. return ret;
  2210. }
  2211. static void
  2212. ar8xxx_phy_detach(struct phy_device *phydev)
  2213. {
  2214. struct net_device *dev = phydev->attached_dev;
  2215. if (!dev)
  2216. return;
  2217. #ifdef CONFIG_ETHERNET_PACKET_MANGLE
  2218. dev->phy_ptr = NULL;
  2219. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6,1,0)
  2220. dev->priv_flags &= ~IFF_NO_IP_ALIGN;
  2221. #else
  2222. dev->extra_priv_flags &= ~IFF_NO_IP_ALIGN;
  2223. #endif
  2224. dev->eth_mangle_rx = NULL;
  2225. dev->eth_mangle_tx = NULL;
  2226. #endif
  2227. }
  2228. static void
  2229. ar8xxx_phy_remove(struct phy_device *phydev)
  2230. {
  2231. struct ar8xxx_priv *priv = phydev->priv;
  2232. if (WARN_ON(!priv))
  2233. return;
  2234. phydev->priv = NULL;
  2235. mutex_lock(&ar8xxx_dev_list_lock);
  2236. if (--priv->use_count > 0) {
  2237. mutex_unlock(&ar8xxx_dev_list_lock);
  2238. return;
  2239. }
  2240. list_del(&priv->list);
  2241. mutex_unlock(&ar8xxx_dev_list_lock);
  2242. unregister_switch(&priv->dev);
  2243. ar8xxx_mib_stop(priv);
  2244. ar8xxx_free(priv);
  2245. }
  2246. static struct phy_driver ar8xxx_phy_driver[] = {
  2247. {
  2248. .phy_id = 0x004d0000,
  2249. .name = "Atheros AR8216/AR8236/AR8316",
  2250. .phy_id_mask = 0xffff0000,
  2251. .probe = ar8xxx_phy_probe,
  2252. .remove = ar8xxx_phy_remove,
  2253. .detach = ar8xxx_phy_detach,
  2254. .config_init = ar8xxx_phy_config_init,
  2255. .config_aneg = ar8xxx_phy_config_aneg,
  2256. .read_status = ar8xxx_phy_read_status,
  2257. .get_features = ar8xxx_get_features,
  2258. }
  2259. };
  2260. static const struct of_device_id ar8xxx_mdiodev_of_match[] = {
  2261. {
  2262. .compatible = "qca,ar7240sw",
  2263. .data = &ar7240sw_chip,
  2264. }, {
  2265. .compatible = "qca,ar8229",
  2266. .data = &ar8229_chip,
  2267. }, {
  2268. .compatible = "qca,ar8236",
  2269. .data = &ar8236_chip,
  2270. }, {
  2271. .compatible = "qca,ar8327",
  2272. .data = &ar8327_chip,
  2273. },
  2274. { /* sentinel */ },
  2275. };
  2276. static int
  2277. ar8xxx_mdiodev_probe(struct mdio_device *mdiodev)
  2278. {
  2279. const struct of_device_id *match;
  2280. struct ar8xxx_priv *priv;
  2281. struct switch_dev *swdev;
  2282. struct device_node *mdio_node;
  2283. int ret;
  2284. match = of_match_device(ar8xxx_mdiodev_of_match, &mdiodev->dev);
  2285. if (!match)
  2286. return -EINVAL;
  2287. priv = ar8xxx_create();
  2288. if (priv == NULL)
  2289. return -ENOMEM;
  2290. priv->mii_bus = mdiodev->bus;
  2291. priv->pdev = &mdiodev->dev;
  2292. priv->chip = (const struct ar8xxx_chip *) match->data;
  2293. ret = of_property_read_u32(priv->pdev->of_node, "qca,mib-poll-interval",
  2294. &priv->mib_poll_interval);
  2295. if (ret)
  2296. priv->mib_poll_interval = 0;
  2297. ret = ar8xxx_read_id(priv);
  2298. if (ret)
  2299. goto free_priv;
  2300. ret = ar8xxx_probe_switch(priv);
  2301. if (ret)
  2302. goto free_priv;
  2303. if (priv->chip->phy_read && priv->chip->phy_write) {
  2304. priv->sw_mii_bus = devm_mdiobus_alloc(&mdiodev->dev);
  2305. priv->sw_mii_bus->name = "ar8xxx-mdio";
  2306. priv->sw_mii_bus->read = ar8xxx_phy_read;
  2307. priv->sw_mii_bus->write = ar8xxx_phy_write;
  2308. priv->sw_mii_bus->priv = priv;
  2309. priv->sw_mii_bus->parent = &mdiodev->dev;
  2310. snprintf(priv->sw_mii_bus->id, MII_BUS_ID_SIZE, "%s",
  2311. dev_name(&mdiodev->dev));
  2312. mdio_node = of_get_child_by_name(priv->pdev->of_node, "mdio-bus");
  2313. ret = of_mdiobus_register(priv->sw_mii_bus, mdio_node);
  2314. if (ret)
  2315. goto free_priv;
  2316. }
  2317. swdev = &priv->dev;
  2318. swdev->alias = dev_name(&mdiodev->dev);
  2319. if (of_property_read_bool(priv->pdev->of_node, "qca,phy4-mii-enable")) {
  2320. priv->port4_phy = true;
  2321. swdev->ports--;
  2322. }
  2323. ret = register_switch(swdev, NULL);
  2324. if (ret)
  2325. goto free_priv;
  2326. pr_info("%s: %s rev. %u switch registered on %s\n",
  2327. swdev->devname, swdev->name, priv->chip_rev,
  2328. dev_name(&priv->mii_bus->dev));
  2329. mutex_lock(&ar8xxx_dev_list_lock);
  2330. list_add(&priv->list, &ar8xxx_dev_list);
  2331. mutex_unlock(&ar8xxx_dev_list_lock);
  2332. priv->use_count++;
  2333. ret = ar8xxx_start(priv);
  2334. if (ret)
  2335. goto err_unregister_switch;
  2336. dev_set_drvdata(&mdiodev->dev, priv);
  2337. return 0;
  2338. err_unregister_switch:
  2339. if (--priv->use_count)
  2340. return ret;
  2341. unregister_switch(&priv->dev);
  2342. free_priv:
  2343. ar8xxx_free(priv);
  2344. return ret;
  2345. }
  2346. static void
  2347. ar8xxx_mdiodev_remove(struct mdio_device *mdiodev)
  2348. {
  2349. struct ar8xxx_priv *priv = dev_get_drvdata(&mdiodev->dev);
  2350. if (WARN_ON(!priv))
  2351. return;
  2352. mutex_lock(&ar8xxx_dev_list_lock);
  2353. if (--priv->use_count > 0) {
  2354. mutex_unlock(&ar8xxx_dev_list_lock);
  2355. return;
  2356. }
  2357. list_del(&priv->list);
  2358. mutex_unlock(&ar8xxx_dev_list_lock);
  2359. unregister_switch(&priv->dev);
  2360. ar8xxx_mib_stop(priv);
  2361. if(priv->sw_mii_bus)
  2362. mdiobus_unregister(priv->sw_mii_bus);
  2363. ar8xxx_free(priv);
  2364. }
  2365. static struct mdio_driver ar8xxx_mdio_driver = {
  2366. .probe = ar8xxx_mdiodev_probe,
  2367. .remove = ar8xxx_mdiodev_remove,
  2368. .mdiodrv.driver = {
  2369. .name = "ar8xxx-switch",
  2370. .of_match_table = ar8xxx_mdiodev_of_match,
  2371. },
  2372. };
  2373. static int __init ar8216_init(void)
  2374. {
  2375. int ret;
  2376. ret = phy_drivers_register(ar8xxx_phy_driver,
  2377. ARRAY_SIZE(ar8xxx_phy_driver),
  2378. THIS_MODULE);
  2379. if (ret)
  2380. return ret;
  2381. ret = mdio_driver_register(&ar8xxx_mdio_driver);
  2382. if (ret)
  2383. phy_drivers_unregister(ar8xxx_phy_driver,
  2384. ARRAY_SIZE(ar8xxx_phy_driver));
  2385. return ret;
  2386. }
  2387. module_init(ar8216_init);
  2388. static void __exit ar8216_exit(void)
  2389. {
  2390. mdio_driver_unregister(&ar8xxx_mdio_driver);
  2391. phy_drivers_unregister(ar8xxx_phy_driver,
  2392. ARRAY_SIZE(ar8xxx_phy_driver));
  2393. }
  2394. module_exit(ar8216_exit);
  2395. MODULE_LICENSE("GPL");