ar8216.c 68 KB

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