ar8216.c 53 KB

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