ar8216.c 68 KB

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