bcm63xx_enet.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for BCM963xx builtin Ethernet mac
  4. *
  5. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/clk.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/slab.h>
  13. #include <linux/delay.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/crc32.h>
  16. #include <linux/err.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/if_vlan.h>
  20. #include <bcm63xx_dev_enet.h>
  21. #include "bcm63xx_enet.h"
  22. static char bcm_enet_driver_name[] = "bcm63xx_enet";
  23. static int copybreak __read_mostly = 128;
  24. module_param(copybreak, int, 0);
  25. MODULE_PARM_DESC(copybreak, "Receive copy threshold");
  26. /* io registers memory shared between all devices */
  27. static void __iomem *bcm_enet_shared_base[3];
  28. /*
  29. * io helpers to access mac registers
  30. */
  31. static inline u32 enet_readl(struct bcm_enet_priv *priv, u32 off)
  32. {
  33. return bcm_readl(priv->base + off);
  34. }
  35. static inline void enet_writel(struct bcm_enet_priv *priv,
  36. u32 val, u32 off)
  37. {
  38. bcm_writel(val, priv->base + off);
  39. }
  40. /*
  41. * io helpers to access switch registers
  42. */
  43. static inline u32 enetsw_readl(struct bcm_enet_priv *priv, u32 off)
  44. {
  45. return bcm_readl(priv->base + off);
  46. }
  47. static inline void enetsw_writel(struct bcm_enet_priv *priv,
  48. u32 val, u32 off)
  49. {
  50. bcm_writel(val, priv->base + off);
  51. }
  52. static inline u16 enetsw_readw(struct bcm_enet_priv *priv, u32 off)
  53. {
  54. return bcm_readw(priv->base + off);
  55. }
  56. static inline void enetsw_writew(struct bcm_enet_priv *priv,
  57. u16 val, u32 off)
  58. {
  59. bcm_writew(val, priv->base + off);
  60. }
  61. static inline u8 enetsw_readb(struct bcm_enet_priv *priv, u32 off)
  62. {
  63. return bcm_readb(priv->base + off);
  64. }
  65. static inline void enetsw_writeb(struct bcm_enet_priv *priv,
  66. u8 val, u32 off)
  67. {
  68. bcm_writeb(val, priv->base + off);
  69. }
  70. /* io helpers to access shared registers */
  71. static inline u32 enet_dma_readl(struct bcm_enet_priv *priv, u32 off)
  72. {
  73. return bcm_readl(bcm_enet_shared_base[0] + off);
  74. }
  75. static inline void enet_dma_writel(struct bcm_enet_priv *priv,
  76. u32 val, u32 off)
  77. {
  78. bcm_writel(val, bcm_enet_shared_base[0] + off);
  79. }
  80. static inline u32 enet_dmac_readl(struct bcm_enet_priv *priv, u32 off, int chan)
  81. {
  82. return bcm_readl(bcm_enet_shared_base[1] +
  83. bcm63xx_enetdmacreg(off) + chan * priv->dma_chan_width);
  84. }
  85. static inline void enet_dmac_writel(struct bcm_enet_priv *priv,
  86. u32 val, u32 off, int chan)
  87. {
  88. bcm_writel(val, bcm_enet_shared_base[1] +
  89. bcm63xx_enetdmacreg(off) + chan * priv->dma_chan_width);
  90. }
  91. static inline u32 enet_dmas_readl(struct bcm_enet_priv *priv, u32 off, int chan)
  92. {
  93. return bcm_readl(bcm_enet_shared_base[2] + off + chan * priv->dma_chan_width);
  94. }
  95. static inline void enet_dmas_writel(struct bcm_enet_priv *priv,
  96. u32 val, u32 off, int chan)
  97. {
  98. bcm_writel(val, bcm_enet_shared_base[2] + off + chan * priv->dma_chan_width);
  99. }
  100. /*
  101. * write given data into mii register and wait for transfer to end
  102. * with timeout (average measured transfer time is 25us)
  103. */
  104. static int do_mdio_op(struct bcm_enet_priv *priv, unsigned int data)
  105. {
  106. int limit;
  107. /* make sure mii interrupt status is cleared */
  108. enet_writel(priv, ENET_IR_MII, ENET_IR_REG);
  109. enet_writel(priv, data, ENET_MIIDATA_REG);
  110. wmb();
  111. /* busy wait on mii interrupt bit, with timeout */
  112. limit = 1000;
  113. do {
  114. if (enet_readl(priv, ENET_IR_REG) & ENET_IR_MII)
  115. break;
  116. udelay(1);
  117. } while (limit-- > 0);
  118. return (limit < 0) ? 1 : 0;
  119. }
  120. /*
  121. * MII internal read callback
  122. */
  123. static int bcm_enet_mdio_read(struct bcm_enet_priv *priv, int mii_id,
  124. int regnum)
  125. {
  126. u32 tmp, val;
  127. tmp = regnum << ENET_MIIDATA_REG_SHIFT;
  128. tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
  129. tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
  130. tmp |= ENET_MIIDATA_OP_READ_MASK;
  131. if (do_mdio_op(priv, tmp))
  132. return -1;
  133. val = enet_readl(priv, ENET_MIIDATA_REG);
  134. val &= 0xffff;
  135. return val;
  136. }
  137. /*
  138. * MII internal write callback
  139. */
  140. static int bcm_enet_mdio_write(struct bcm_enet_priv *priv, int mii_id,
  141. int regnum, u16 value)
  142. {
  143. u32 tmp;
  144. tmp = (value & 0xffff) << ENET_MIIDATA_DATA_SHIFT;
  145. tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
  146. tmp |= regnum << ENET_MIIDATA_REG_SHIFT;
  147. tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
  148. tmp |= ENET_MIIDATA_OP_WRITE_MASK;
  149. (void)do_mdio_op(priv, tmp);
  150. return 0;
  151. }
  152. /*
  153. * MII read callback from phylib
  154. */
  155. static int bcm_enet_mdio_read_phylib(struct mii_bus *bus, int mii_id,
  156. int regnum)
  157. {
  158. return bcm_enet_mdio_read(bus->priv, mii_id, regnum);
  159. }
  160. /*
  161. * MII write callback from phylib
  162. */
  163. static int bcm_enet_mdio_write_phylib(struct mii_bus *bus, int mii_id,
  164. int regnum, u16 value)
  165. {
  166. return bcm_enet_mdio_write(bus->priv, mii_id, regnum, value);
  167. }
  168. /*
  169. * MII read callback from mii core
  170. */
  171. static int bcm_enet_mdio_read_mii(struct net_device *dev, int mii_id,
  172. int regnum)
  173. {
  174. return bcm_enet_mdio_read(netdev_priv(dev), mii_id, regnum);
  175. }
  176. /*
  177. * MII write callback from mii core
  178. */
  179. static void bcm_enet_mdio_write_mii(struct net_device *dev, int mii_id,
  180. int regnum, int value)
  181. {
  182. bcm_enet_mdio_write(netdev_priv(dev), mii_id, regnum, value);
  183. }
  184. /*
  185. * refill rx queue
  186. */
  187. static int bcm_enet_refill_rx(struct net_device *dev, bool napi_mode)
  188. {
  189. struct bcm_enet_priv *priv;
  190. priv = netdev_priv(dev);
  191. while (priv->rx_desc_count < priv->rx_ring_size) {
  192. struct bcm_enet_desc *desc;
  193. int desc_idx;
  194. u32 len_stat;
  195. desc_idx = priv->rx_dirty_desc;
  196. desc = &priv->rx_desc_cpu[desc_idx];
  197. if (!priv->rx_buf[desc_idx]) {
  198. void *buf;
  199. if (likely(napi_mode))
  200. buf = napi_alloc_frag(priv->rx_frag_size);
  201. else
  202. buf = netdev_alloc_frag(priv->rx_frag_size);
  203. if (unlikely(!buf))
  204. break;
  205. priv->rx_buf[desc_idx] = buf;
  206. desc->address = dma_map_single(&priv->pdev->dev,
  207. buf + priv->rx_buf_offset,
  208. priv->rx_buf_size,
  209. DMA_FROM_DEVICE);
  210. }
  211. len_stat = priv->rx_buf_size << DMADESC_LENGTH_SHIFT;
  212. len_stat |= DMADESC_OWNER_MASK;
  213. if (priv->rx_dirty_desc == priv->rx_ring_size - 1) {
  214. len_stat |= (DMADESC_WRAP_MASK >> priv->dma_desc_shift);
  215. priv->rx_dirty_desc = 0;
  216. } else {
  217. priv->rx_dirty_desc++;
  218. }
  219. wmb();
  220. desc->len_stat = len_stat;
  221. priv->rx_desc_count++;
  222. /* tell dma engine we allocated one buffer */
  223. if (priv->dma_has_sram)
  224. enet_dma_writel(priv, 1, ENETDMA_BUFALLOC_REG(priv->rx_chan));
  225. else
  226. enet_dmac_writel(priv, 1, ENETDMAC_BUFALLOC, priv->rx_chan);
  227. }
  228. /* If rx ring is still empty, set a timer to try allocating
  229. * again at a later time. */
  230. if (priv->rx_desc_count == 0 && netif_running(dev)) {
  231. dev_warn(&priv->pdev->dev, "unable to refill rx ring\n");
  232. priv->rx_timeout.expires = jiffies + HZ;
  233. add_timer(&priv->rx_timeout);
  234. }
  235. return 0;
  236. }
  237. /*
  238. * timer callback to defer refill rx queue in case we're OOM
  239. */
  240. static void bcm_enet_refill_rx_timer(struct timer_list *t)
  241. {
  242. struct bcm_enet_priv *priv = from_timer(priv, t, rx_timeout);
  243. struct net_device *dev = priv->net_dev;
  244. spin_lock(&priv->rx_lock);
  245. bcm_enet_refill_rx(dev, false);
  246. spin_unlock(&priv->rx_lock);
  247. }
  248. /*
  249. * extract packet from rx queue
  250. */
  251. static int bcm_enet_receive_queue(struct net_device *dev, int budget)
  252. {
  253. struct bcm_enet_priv *priv;
  254. struct list_head rx_list;
  255. struct device *kdev;
  256. int processed;
  257. priv = netdev_priv(dev);
  258. INIT_LIST_HEAD(&rx_list);
  259. kdev = &priv->pdev->dev;
  260. processed = 0;
  261. /* don't scan ring further than number of refilled
  262. * descriptor */
  263. if (budget > priv->rx_desc_count)
  264. budget = priv->rx_desc_count;
  265. do {
  266. struct bcm_enet_desc *desc;
  267. struct sk_buff *skb;
  268. int desc_idx;
  269. u32 len_stat;
  270. unsigned int len;
  271. void *buf;
  272. desc_idx = priv->rx_curr_desc;
  273. desc = &priv->rx_desc_cpu[desc_idx];
  274. /* make sure we actually read the descriptor status at
  275. * each loop */
  276. rmb();
  277. len_stat = desc->len_stat;
  278. /* break if dma ownership belongs to hw */
  279. if (len_stat & DMADESC_OWNER_MASK)
  280. break;
  281. processed++;
  282. priv->rx_curr_desc++;
  283. if (priv->rx_curr_desc == priv->rx_ring_size)
  284. priv->rx_curr_desc = 0;
  285. /* if the packet does not have start of packet _and_
  286. * end of packet flag set, then just recycle it */
  287. if ((len_stat & (DMADESC_ESOP_MASK >> priv->dma_desc_shift)) !=
  288. (DMADESC_ESOP_MASK >> priv->dma_desc_shift)) {
  289. dev->stats.rx_dropped++;
  290. continue;
  291. }
  292. /* recycle packet if it's marked as bad */
  293. if (!priv->enet_is_sw &&
  294. unlikely(len_stat & DMADESC_ERR_MASK)) {
  295. dev->stats.rx_errors++;
  296. if (len_stat & DMADESC_OVSIZE_MASK)
  297. dev->stats.rx_length_errors++;
  298. if (len_stat & DMADESC_CRC_MASK)
  299. dev->stats.rx_crc_errors++;
  300. if (len_stat & DMADESC_UNDER_MASK)
  301. dev->stats.rx_frame_errors++;
  302. if (len_stat & DMADESC_OV_MASK)
  303. dev->stats.rx_fifo_errors++;
  304. continue;
  305. }
  306. /* valid packet */
  307. buf = priv->rx_buf[desc_idx];
  308. len = (len_stat & DMADESC_LENGTH_MASK) >> DMADESC_LENGTH_SHIFT;
  309. /* don't include FCS */
  310. len -= 4;
  311. if (len < copybreak) {
  312. skb = napi_alloc_skb(&priv->napi, len);
  313. if (unlikely(!skb)) {
  314. /* forget packet, just rearm desc */
  315. dev->stats.rx_dropped++;
  316. continue;
  317. }
  318. dma_sync_single_for_cpu(kdev, desc->address,
  319. len, DMA_FROM_DEVICE);
  320. memcpy(skb->data, buf + priv->rx_buf_offset, len);
  321. dma_sync_single_for_device(kdev, desc->address,
  322. len, DMA_FROM_DEVICE);
  323. } else {
  324. dma_unmap_single(kdev, desc->address,
  325. priv->rx_buf_size, DMA_FROM_DEVICE);
  326. priv->rx_buf[desc_idx] = NULL;
  327. skb = napi_build_skb(buf, priv->rx_frag_size);
  328. if (unlikely(!skb)) {
  329. skb_free_frag(buf);
  330. dev->stats.rx_dropped++;
  331. continue;
  332. }
  333. skb_reserve(skb, priv->rx_buf_offset);
  334. }
  335. skb_put(skb, len);
  336. skb->protocol = eth_type_trans(skb, dev);
  337. dev->stats.rx_packets++;
  338. dev->stats.rx_bytes += len;
  339. list_add_tail(&skb->list, &rx_list);
  340. } while (processed < budget);
  341. netif_receive_skb_list(&rx_list);
  342. priv->rx_desc_count -= processed;
  343. if (processed || !priv->rx_desc_count) {
  344. bcm_enet_refill_rx(dev, true);
  345. /* kick rx dma */
  346. enet_dmac_writel(priv, priv->dma_chan_en_mask,
  347. ENETDMAC_CHANCFG, priv->rx_chan);
  348. }
  349. return processed;
  350. }
  351. /*
  352. * try to or force reclaim of transmitted buffers
  353. */
  354. static int bcm_enet_tx_reclaim(struct net_device *dev, int force, int budget)
  355. {
  356. struct bcm_enet_priv *priv;
  357. unsigned int bytes;
  358. int released;
  359. priv = netdev_priv(dev);
  360. bytes = 0;
  361. released = 0;
  362. while (priv->tx_desc_count < priv->tx_ring_size) {
  363. struct bcm_enet_desc *desc;
  364. struct sk_buff *skb;
  365. /* We run in a bh and fight against start_xmit, which
  366. * is called with bh disabled */
  367. spin_lock(&priv->tx_lock);
  368. desc = &priv->tx_desc_cpu[priv->tx_dirty_desc];
  369. if (!force && (desc->len_stat & DMADESC_OWNER_MASK)) {
  370. spin_unlock(&priv->tx_lock);
  371. break;
  372. }
  373. /* ensure other field of the descriptor were not read
  374. * before we checked ownership */
  375. rmb();
  376. skb = priv->tx_skb[priv->tx_dirty_desc];
  377. priv->tx_skb[priv->tx_dirty_desc] = NULL;
  378. dma_unmap_single(&priv->pdev->dev, desc->address, skb->len,
  379. DMA_TO_DEVICE);
  380. priv->tx_dirty_desc++;
  381. if (priv->tx_dirty_desc == priv->tx_ring_size)
  382. priv->tx_dirty_desc = 0;
  383. priv->tx_desc_count++;
  384. spin_unlock(&priv->tx_lock);
  385. if (desc->len_stat & DMADESC_UNDER_MASK)
  386. dev->stats.tx_errors++;
  387. bytes += skb->len;
  388. napi_consume_skb(skb, budget);
  389. released++;
  390. }
  391. netdev_completed_queue(dev, released, bytes);
  392. if (netif_queue_stopped(dev) && released)
  393. netif_wake_queue(dev);
  394. return released;
  395. }
  396. /*
  397. * poll func, called by network core
  398. */
  399. static int bcm_enet_poll(struct napi_struct *napi, int budget)
  400. {
  401. struct bcm_enet_priv *priv;
  402. struct net_device *dev;
  403. int rx_work_done;
  404. priv = container_of(napi, struct bcm_enet_priv, napi);
  405. dev = priv->net_dev;
  406. /* ack interrupts */
  407. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  408. ENETDMAC_IR, priv->rx_chan);
  409. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  410. ENETDMAC_IR, priv->tx_chan);
  411. /* reclaim sent skb */
  412. bcm_enet_tx_reclaim(dev, 0, budget);
  413. spin_lock(&priv->rx_lock);
  414. rx_work_done = bcm_enet_receive_queue(dev, budget);
  415. spin_unlock(&priv->rx_lock);
  416. if (rx_work_done >= budget) {
  417. /* rx queue is not yet empty/clean */
  418. return rx_work_done;
  419. }
  420. /* no more packet in rx/tx queue, remove device from poll
  421. * queue */
  422. napi_complete_done(napi, rx_work_done);
  423. /* restore rx/tx interrupt */
  424. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  425. ENETDMAC_IRMASK, priv->rx_chan);
  426. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  427. ENETDMAC_IRMASK, priv->tx_chan);
  428. return rx_work_done;
  429. }
  430. /*
  431. * mac interrupt handler
  432. */
  433. static irqreturn_t bcm_enet_isr_mac(int irq, void *dev_id)
  434. {
  435. struct net_device *dev;
  436. struct bcm_enet_priv *priv;
  437. u32 stat;
  438. dev = dev_id;
  439. priv = netdev_priv(dev);
  440. stat = enet_readl(priv, ENET_IR_REG);
  441. if (!(stat & ENET_IR_MIB))
  442. return IRQ_NONE;
  443. /* clear & mask interrupt */
  444. enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
  445. enet_writel(priv, 0, ENET_IRMASK_REG);
  446. /* read mib registers in workqueue */
  447. schedule_work(&priv->mib_update_task);
  448. return IRQ_HANDLED;
  449. }
  450. /*
  451. * rx/tx dma interrupt handler
  452. */
  453. static irqreturn_t bcm_enet_isr_dma(int irq, void *dev_id)
  454. {
  455. struct net_device *dev;
  456. struct bcm_enet_priv *priv;
  457. dev = dev_id;
  458. priv = netdev_priv(dev);
  459. /* mask rx/tx interrupts */
  460. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  461. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  462. napi_schedule(&priv->napi);
  463. return IRQ_HANDLED;
  464. }
  465. /*
  466. * tx request callback
  467. */
  468. static netdev_tx_t
  469. bcm_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  470. {
  471. struct bcm_enet_priv *priv;
  472. struct bcm_enet_desc *desc;
  473. u32 len_stat;
  474. netdev_tx_t ret;
  475. priv = netdev_priv(dev);
  476. /* lock against tx reclaim */
  477. spin_lock(&priv->tx_lock);
  478. /* make sure the tx hw queue is not full, should not happen
  479. * since we stop queue before it's the case */
  480. if (unlikely(!priv->tx_desc_count)) {
  481. netif_stop_queue(dev);
  482. dev_err(&priv->pdev->dev, "xmit called with no tx desc "
  483. "available?\n");
  484. ret = NETDEV_TX_BUSY;
  485. goto out_unlock;
  486. }
  487. /* pad small packets sent on a switch device */
  488. if (priv->enet_is_sw && skb->len < 64) {
  489. int needed = 64 - skb->len;
  490. char *data;
  491. if (unlikely(skb_tailroom(skb) < needed)) {
  492. struct sk_buff *nskb;
  493. nskb = skb_copy_expand(skb, 0, needed, GFP_ATOMIC);
  494. if (!nskb) {
  495. ret = NETDEV_TX_BUSY;
  496. goto out_unlock;
  497. }
  498. dev_kfree_skb(skb);
  499. skb = nskb;
  500. }
  501. data = skb_put_zero(skb, needed);
  502. }
  503. /* point to the next available desc */
  504. desc = &priv->tx_desc_cpu[priv->tx_curr_desc];
  505. priv->tx_skb[priv->tx_curr_desc] = skb;
  506. /* fill descriptor */
  507. desc->address = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
  508. DMA_TO_DEVICE);
  509. len_stat = (skb->len << DMADESC_LENGTH_SHIFT) & DMADESC_LENGTH_MASK;
  510. len_stat |= (DMADESC_ESOP_MASK >> priv->dma_desc_shift) |
  511. DMADESC_APPEND_CRC |
  512. DMADESC_OWNER_MASK;
  513. priv->tx_curr_desc++;
  514. if (priv->tx_curr_desc == priv->tx_ring_size) {
  515. priv->tx_curr_desc = 0;
  516. len_stat |= (DMADESC_WRAP_MASK >> priv->dma_desc_shift);
  517. }
  518. priv->tx_desc_count--;
  519. /* dma might be already polling, make sure we update desc
  520. * fields in correct order */
  521. wmb();
  522. desc->len_stat = len_stat;
  523. wmb();
  524. netdev_sent_queue(dev, skb->len);
  525. /* kick tx dma */
  526. if (!netdev_xmit_more() || !priv->tx_desc_count)
  527. enet_dmac_writel(priv, priv->dma_chan_en_mask,
  528. ENETDMAC_CHANCFG, priv->tx_chan);
  529. /* stop queue if no more desc available */
  530. if (!priv->tx_desc_count)
  531. netif_stop_queue(dev);
  532. dev->stats.tx_bytes += skb->len;
  533. dev->stats.tx_packets++;
  534. ret = NETDEV_TX_OK;
  535. out_unlock:
  536. spin_unlock(&priv->tx_lock);
  537. return ret;
  538. }
  539. /*
  540. * Change the interface's mac address.
  541. */
  542. static int bcm_enet_set_mac_address(struct net_device *dev, void *p)
  543. {
  544. struct bcm_enet_priv *priv;
  545. struct sockaddr *addr = p;
  546. u32 val;
  547. priv = netdev_priv(dev);
  548. eth_hw_addr_set(dev, addr->sa_data);
  549. /* use perfect match register 0 to store my mac address */
  550. val = (dev->dev_addr[2] << 24) | (dev->dev_addr[3] << 16) |
  551. (dev->dev_addr[4] << 8) | dev->dev_addr[5];
  552. enet_writel(priv, val, ENET_PML_REG(0));
  553. val = (dev->dev_addr[0] << 8 | dev->dev_addr[1]);
  554. val |= ENET_PMH_DATAVALID_MASK;
  555. enet_writel(priv, val, ENET_PMH_REG(0));
  556. return 0;
  557. }
  558. /*
  559. * Change rx mode (promiscuous/allmulti) and update multicast list
  560. */
  561. static void bcm_enet_set_multicast_list(struct net_device *dev)
  562. {
  563. struct bcm_enet_priv *priv;
  564. struct netdev_hw_addr *ha;
  565. u32 val;
  566. int i;
  567. priv = netdev_priv(dev);
  568. val = enet_readl(priv, ENET_RXCFG_REG);
  569. if (dev->flags & IFF_PROMISC)
  570. val |= ENET_RXCFG_PROMISC_MASK;
  571. else
  572. val &= ~ENET_RXCFG_PROMISC_MASK;
  573. /* only 3 perfect match registers left, first one is used for
  574. * own mac address */
  575. if ((dev->flags & IFF_ALLMULTI) || netdev_mc_count(dev) > 3)
  576. val |= ENET_RXCFG_ALLMCAST_MASK;
  577. else
  578. val &= ~ENET_RXCFG_ALLMCAST_MASK;
  579. /* no need to set perfect match registers if we catch all
  580. * multicast */
  581. if (val & ENET_RXCFG_ALLMCAST_MASK) {
  582. enet_writel(priv, val, ENET_RXCFG_REG);
  583. return;
  584. }
  585. i = 0;
  586. netdev_for_each_mc_addr(ha, dev) {
  587. u8 *dmi_addr;
  588. u32 tmp;
  589. if (i == 3)
  590. break;
  591. /* update perfect match registers */
  592. dmi_addr = ha->addr;
  593. tmp = (dmi_addr[2] << 24) | (dmi_addr[3] << 16) |
  594. (dmi_addr[4] << 8) | dmi_addr[5];
  595. enet_writel(priv, tmp, ENET_PML_REG(i + 1));
  596. tmp = (dmi_addr[0] << 8 | dmi_addr[1]);
  597. tmp |= ENET_PMH_DATAVALID_MASK;
  598. enet_writel(priv, tmp, ENET_PMH_REG(i++ + 1));
  599. }
  600. for (; i < 3; i++) {
  601. enet_writel(priv, 0, ENET_PML_REG(i + 1));
  602. enet_writel(priv, 0, ENET_PMH_REG(i + 1));
  603. }
  604. enet_writel(priv, val, ENET_RXCFG_REG);
  605. }
  606. /*
  607. * set mac duplex parameters
  608. */
  609. static void bcm_enet_set_duplex(struct bcm_enet_priv *priv, int fullduplex)
  610. {
  611. u32 val;
  612. val = enet_readl(priv, ENET_TXCTL_REG);
  613. if (fullduplex)
  614. val |= ENET_TXCTL_FD_MASK;
  615. else
  616. val &= ~ENET_TXCTL_FD_MASK;
  617. enet_writel(priv, val, ENET_TXCTL_REG);
  618. }
  619. /*
  620. * set mac flow control parameters
  621. */
  622. static void bcm_enet_set_flow(struct bcm_enet_priv *priv, int rx_en, int tx_en)
  623. {
  624. u32 val;
  625. /* rx flow control (pause frame handling) */
  626. val = enet_readl(priv, ENET_RXCFG_REG);
  627. if (rx_en)
  628. val |= ENET_RXCFG_ENFLOW_MASK;
  629. else
  630. val &= ~ENET_RXCFG_ENFLOW_MASK;
  631. enet_writel(priv, val, ENET_RXCFG_REG);
  632. if (!priv->dma_has_sram)
  633. return;
  634. /* tx flow control (pause frame generation) */
  635. val = enet_dma_readl(priv, ENETDMA_CFG_REG);
  636. if (tx_en)
  637. val |= ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
  638. else
  639. val &= ~ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
  640. enet_dma_writel(priv, val, ENETDMA_CFG_REG);
  641. }
  642. /*
  643. * link changed callback (from phylib)
  644. */
  645. static void bcm_enet_adjust_phy_link(struct net_device *dev)
  646. {
  647. struct bcm_enet_priv *priv;
  648. struct phy_device *phydev;
  649. int status_changed;
  650. priv = netdev_priv(dev);
  651. phydev = dev->phydev;
  652. status_changed = 0;
  653. if (priv->old_link != phydev->link) {
  654. status_changed = 1;
  655. priv->old_link = phydev->link;
  656. }
  657. /* reflect duplex change in mac configuration */
  658. if (phydev->link && phydev->duplex != priv->old_duplex) {
  659. bcm_enet_set_duplex(priv,
  660. (phydev->duplex == DUPLEX_FULL) ? 1 : 0);
  661. status_changed = 1;
  662. priv->old_duplex = phydev->duplex;
  663. }
  664. /* enable flow control if remote advertise it (trust phylib to
  665. * check that duplex is full */
  666. if (phydev->link && phydev->pause != priv->old_pause) {
  667. int rx_pause_en, tx_pause_en;
  668. if (phydev->pause) {
  669. /* pause was advertised by lpa and us */
  670. rx_pause_en = 1;
  671. tx_pause_en = 1;
  672. } else if (!priv->pause_auto) {
  673. /* pause setting overridden by user */
  674. rx_pause_en = priv->pause_rx;
  675. tx_pause_en = priv->pause_tx;
  676. } else {
  677. rx_pause_en = 0;
  678. tx_pause_en = 0;
  679. }
  680. bcm_enet_set_flow(priv, rx_pause_en, tx_pause_en);
  681. status_changed = 1;
  682. priv->old_pause = phydev->pause;
  683. }
  684. if (status_changed) {
  685. pr_info("%s: link %s", dev->name, phydev->link ?
  686. "UP" : "DOWN");
  687. if (phydev->link)
  688. pr_cont(" - %d/%s - flow control %s", phydev->speed,
  689. DUPLEX_FULL == phydev->duplex ? "full" : "half",
  690. phydev->pause == 1 ? "rx&tx" : "off");
  691. pr_cont("\n");
  692. }
  693. }
  694. /*
  695. * link changed callback (if phylib is not used)
  696. */
  697. static void bcm_enet_adjust_link(struct net_device *dev)
  698. {
  699. struct bcm_enet_priv *priv;
  700. priv = netdev_priv(dev);
  701. bcm_enet_set_duplex(priv, priv->force_duplex_full);
  702. bcm_enet_set_flow(priv, priv->pause_rx, priv->pause_tx);
  703. netif_carrier_on(dev);
  704. pr_info("%s: link forced UP - %d/%s - flow control %s/%s\n",
  705. dev->name,
  706. priv->force_speed_100 ? 100 : 10,
  707. priv->force_duplex_full ? "full" : "half",
  708. priv->pause_rx ? "rx" : "off",
  709. priv->pause_tx ? "tx" : "off");
  710. }
  711. static void bcm_enet_free_rx_buf_ring(struct device *kdev, struct bcm_enet_priv *priv)
  712. {
  713. int i;
  714. for (i = 0; i < priv->rx_ring_size; i++) {
  715. struct bcm_enet_desc *desc;
  716. if (!priv->rx_buf[i])
  717. continue;
  718. desc = &priv->rx_desc_cpu[i];
  719. dma_unmap_single(kdev, desc->address, priv->rx_buf_size,
  720. DMA_FROM_DEVICE);
  721. skb_free_frag(priv->rx_buf[i]);
  722. }
  723. kfree(priv->rx_buf);
  724. }
  725. /*
  726. * open callback, allocate dma rings & buffers and start rx operation
  727. */
  728. static int bcm_enet_open(struct net_device *dev)
  729. {
  730. struct bcm_enet_priv *priv;
  731. struct sockaddr addr;
  732. struct device *kdev;
  733. struct phy_device *phydev;
  734. int i, ret;
  735. unsigned int size;
  736. char phy_id[MII_BUS_ID_SIZE + 3];
  737. void *p;
  738. u32 val;
  739. priv = netdev_priv(dev);
  740. kdev = &priv->pdev->dev;
  741. if (priv->has_phy) {
  742. /* connect to PHY */
  743. snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
  744. priv->mii_bus->id, priv->phy_id);
  745. phydev = phy_connect(dev, phy_id, bcm_enet_adjust_phy_link,
  746. PHY_INTERFACE_MODE_MII);
  747. if (IS_ERR(phydev)) {
  748. dev_err(kdev, "could not attach to PHY\n");
  749. return PTR_ERR(phydev);
  750. }
  751. /* mask with MAC supported features */
  752. phy_support_sym_pause(phydev);
  753. phy_set_max_speed(phydev, SPEED_100);
  754. phy_set_sym_pause(phydev, priv->pause_rx, priv->pause_rx,
  755. priv->pause_auto);
  756. phy_attached_info(phydev);
  757. priv->old_link = 0;
  758. priv->old_duplex = -1;
  759. priv->old_pause = -1;
  760. } else {
  761. phydev = NULL;
  762. }
  763. /* mask all interrupts and request them */
  764. enet_writel(priv, 0, ENET_IRMASK_REG);
  765. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  766. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  767. ret = request_irq(dev->irq, bcm_enet_isr_mac, 0, dev->name, dev);
  768. if (ret)
  769. goto out_phy_disconnect;
  770. ret = request_irq(priv->irq_rx, bcm_enet_isr_dma, 0,
  771. dev->name, dev);
  772. if (ret)
  773. goto out_freeirq;
  774. ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
  775. 0, dev->name, dev);
  776. if (ret)
  777. goto out_freeirq_rx;
  778. /* initialize perfect match registers */
  779. for (i = 0; i < 4; i++) {
  780. enet_writel(priv, 0, ENET_PML_REG(i));
  781. enet_writel(priv, 0, ENET_PMH_REG(i));
  782. }
  783. /* write device mac address */
  784. memcpy(addr.sa_data, dev->dev_addr, ETH_ALEN);
  785. bcm_enet_set_mac_address(dev, &addr);
  786. /* allocate rx dma ring */
  787. size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
  788. p = dma_alloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
  789. if (!p) {
  790. ret = -ENOMEM;
  791. goto out_freeirq_tx;
  792. }
  793. priv->rx_desc_alloc_size = size;
  794. priv->rx_desc_cpu = p;
  795. /* allocate tx dma ring */
  796. size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
  797. p = dma_alloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
  798. if (!p) {
  799. ret = -ENOMEM;
  800. goto out_free_rx_ring;
  801. }
  802. priv->tx_desc_alloc_size = size;
  803. priv->tx_desc_cpu = p;
  804. priv->tx_skb = kcalloc(priv->tx_ring_size, sizeof(struct sk_buff *),
  805. GFP_KERNEL);
  806. if (!priv->tx_skb) {
  807. ret = -ENOMEM;
  808. goto out_free_tx_ring;
  809. }
  810. priv->tx_desc_count = priv->tx_ring_size;
  811. priv->tx_dirty_desc = 0;
  812. priv->tx_curr_desc = 0;
  813. spin_lock_init(&priv->tx_lock);
  814. /* init & fill rx ring with buffers */
  815. priv->rx_buf = kcalloc(priv->rx_ring_size, sizeof(void *),
  816. GFP_KERNEL);
  817. if (!priv->rx_buf) {
  818. ret = -ENOMEM;
  819. goto out_free_tx_skb;
  820. }
  821. priv->rx_desc_count = 0;
  822. priv->rx_dirty_desc = 0;
  823. priv->rx_curr_desc = 0;
  824. /* initialize flow control buffer allocation */
  825. if (priv->dma_has_sram)
  826. enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
  827. ENETDMA_BUFALLOC_REG(priv->rx_chan));
  828. else
  829. enet_dmac_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
  830. ENETDMAC_BUFALLOC, priv->rx_chan);
  831. if (bcm_enet_refill_rx(dev, false)) {
  832. dev_err(kdev, "cannot allocate rx buffer queue\n");
  833. ret = -ENOMEM;
  834. goto out;
  835. }
  836. /* write rx & tx ring addresses */
  837. if (priv->dma_has_sram) {
  838. enet_dmas_writel(priv, priv->rx_desc_dma,
  839. ENETDMAS_RSTART_REG, priv->rx_chan);
  840. enet_dmas_writel(priv, priv->tx_desc_dma,
  841. ENETDMAS_RSTART_REG, priv->tx_chan);
  842. } else {
  843. enet_dmac_writel(priv, priv->rx_desc_dma,
  844. ENETDMAC_RSTART, priv->rx_chan);
  845. enet_dmac_writel(priv, priv->tx_desc_dma,
  846. ENETDMAC_RSTART, priv->tx_chan);
  847. }
  848. /* clear remaining state ram for rx & tx channel */
  849. if (priv->dma_has_sram) {
  850. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
  851. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
  852. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
  853. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
  854. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
  855. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
  856. } else {
  857. enet_dmac_writel(priv, 0, ENETDMAC_FC, priv->rx_chan);
  858. enet_dmac_writel(priv, 0, ENETDMAC_FC, priv->tx_chan);
  859. }
  860. /* set max rx/tx length */
  861. enet_writel(priv, priv->hw_mtu, ENET_RXMAXLEN_REG);
  862. enet_writel(priv, priv->hw_mtu, ENET_TXMAXLEN_REG);
  863. /* set dma maximum burst len */
  864. enet_dmac_writel(priv, priv->dma_maxburst,
  865. ENETDMAC_MAXBURST, priv->rx_chan);
  866. enet_dmac_writel(priv, priv->dma_maxburst,
  867. ENETDMAC_MAXBURST, priv->tx_chan);
  868. /* set correct transmit fifo watermark */
  869. enet_writel(priv, BCMENET_TX_FIFO_TRESH, ENET_TXWMARK_REG);
  870. /* set flow control low/high threshold to 1/3 / 2/3 */
  871. if (priv->dma_has_sram) {
  872. val = priv->rx_ring_size / 3;
  873. enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
  874. val = (priv->rx_ring_size * 2) / 3;
  875. enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
  876. } else {
  877. enet_dmac_writel(priv, 5, ENETDMAC_FC, priv->rx_chan);
  878. enet_dmac_writel(priv, priv->rx_ring_size, ENETDMAC_LEN, priv->rx_chan);
  879. enet_dmac_writel(priv, priv->tx_ring_size, ENETDMAC_LEN, priv->tx_chan);
  880. }
  881. /* all set, enable mac and interrupts, start dma engine and
  882. * kick rx dma channel */
  883. wmb();
  884. val = enet_readl(priv, ENET_CTL_REG);
  885. val |= ENET_CTL_ENABLE_MASK;
  886. enet_writel(priv, val, ENET_CTL_REG);
  887. if (priv->dma_has_sram)
  888. enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
  889. enet_dmac_writel(priv, priv->dma_chan_en_mask,
  890. ENETDMAC_CHANCFG, priv->rx_chan);
  891. /* watch "mib counters about to overflow" interrupt */
  892. enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
  893. enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
  894. /* watch "packet transferred" interrupt in rx and tx */
  895. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  896. ENETDMAC_IR, priv->rx_chan);
  897. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  898. ENETDMAC_IR, priv->tx_chan);
  899. /* make sure we enable napi before rx interrupt */
  900. napi_enable(&priv->napi);
  901. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  902. ENETDMAC_IRMASK, priv->rx_chan);
  903. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  904. ENETDMAC_IRMASK, priv->tx_chan);
  905. if (phydev)
  906. phy_start(phydev);
  907. else
  908. bcm_enet_adjust_link(dev);
  909. netif_start_queue(dev);
  910. return 0;
  911. out:
  912. bcm_enet_free_rx_buf_ring(kdev, priv);
  913. out_free_tx_skb:
  914. kfree(priv->tx_skb);
  915. out_free_tx_ring:
  916. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  917. priv->tx_desc_cpu, priv->tx_desc_dma);
  918. out_free_rx_ring:
  919. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  920. priv->rx_desc_cpu, priv->rx_desc_dma);
  921. out_freeirq_tx:
  922. free_irq(priv->irq_tx, dev);
  923. out_freeirq_rx:
  924. free_irq(priv->irq_rx, dev);
  925. out_freeirq:
  926. free_irq(dev->irq, dev);
  927. out_phy_disconnect:
  928. if (phydev)
  929. phy_disconnect(phydev);
  930. return ret;
  931. }
  932. /*
  933. * disable mac
  934. */
  935. static void bcm_enet_disable_mac(struct bcm_enet_priv *priv)
  936. {
  937. int limit;
  938. u32 val;
  939. val = enet_readl(priv, ENET_CTL_REG);
  940. val |= ENET_CTL_DISABLE_MASK;
  941. enet_writel(priv, val, ENET_CTL_REG);
  942. limit = 1000;
  943. do {
  944. u32 val;
  945. val = enet_readl(priv, ENET_CTL_REG);
  946. if (!(val & ENET_CTL_DISABLE_MASK))
  947. break;
  948. udelay(1);
  949. } while (limit--);
  950. }
  951. /*
  952. * disable dma in given channel
  953. */
  954. static void bcm_enet_disable_dma(struct bcm_enet_priv *priv, int chan)
  955. {
  956. int limit;
  957. enet_dmac_writel(priv, 0, ENETDMAC_CHANCFG, chan);
  958. limit = 1000;
  959. do {
  960. u32 val;
  961. val = enet_dmac_readl(priv, ENETDMAC_CHANCFG, chan);
  962. if (!(val & ENETDMAC_CHANCFG_EN_MASK))
  963. break;
  964. udelay(1);
  965. } while (limit--);
  966. }
  967. /*
  968. * stop callback
  969. */
  970. static int bcm_enet_stop(struct net_device *dev)
  971. {
  972. struct bcm_enet_priv *priv;
  973. struct device *kdev;
  974. priv = netdev_priv(dev);
  975. kdev = &priv->pdev->dev;
  976. netif_stop_queue(dev);
  977. napi_disable(&priv->napi);
  978. if (priv->has_phy)
  979. phy_stop(dev->phydev);
  980. del_timer_sync(&priv->rx_timeout);
  981. /* mask all interrupts */
  982. enet_writel(priv, 0, ENET_IRMASK_REG);
  983. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  984. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  985. /* make sure no mib update is scheduled */
  986. cancel_work_sync(&priv->mib_update_task);
  987. /* disable dma & mac */
  988. bcm_enet_disable_dma(priv, priv->tx_chan);
  989. bcm_enet_disable_dma(priv, priv->rx_chan);
  990. bcm_enet_disable_mac(priv);
  991. /* force reclaim of all tx buffers */
  992. bcm_enet_tx_reclaim(dev, 1, 0);
  993. /* free the rx buffer ring */
  994. bcm_enet_free_rx_buf_ring(kdev, priv);
  995. /* free remaining allocated memory */
  996. kfree(priv->tx_skb);
  997. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  998. priv->rx_desc_cpu, priv->rx_desc_dma);
  999. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  1000. priv->tx_desc_cpu, priv->tx_desc_dma);
  1001. free_irq(priv->irq_tx, dev);
  1002. free_irq(priv->irq_rx, dev);
  1003. free_irq(dev->irq, dev);
  1004. /* release phy */
  1005. if (priv->has_phy)
  1006. phy_disconnect(dev->phydev);
  1007. /* reset BQL after forced tx reclaim to prevent kernel panic */
  1008. netdev_reset_queue(dev);
  1009. return 0;
  1010. }
  1011. /*
  1012. * ethtool callbacks
  1013. */
  1014. struct bcm_enet_stats {
  1015. char stat_string[ETH_GSTRING_LEN];
  1016. int sizeof_stat;
  1017. int stat_offset;
  1018. int mib_reg;
  1019. };
  1020. #define GEN_STAT(m) sizeof(((struct bcm_enet_priv *)0)->m), \
  1021. offsetof(struct bcm_enet_priv, m)
  1022. #define DEV_STAT(m) sizeof(((struct net_device_stats *)0)->m), \
  1023. offsetof(struct net_device_stats, m)
  1024. static const struct bcm_enet_stats bcm_enet_gstrings_stats[] = {
  1025. { "rx_packets", DEV_STAT(rx_packets), -1 },
  1026. { "tx_packets", DEV_STAT(tx_packets), -1 },
  1027. { "rx_bytes", DEV_STAT(rx_bytes), -1 },
  1028. { "tx_bytes", DEV_STAT(tx_bytes), -1 },
  1029. { "rx_errors", DEV_STAT(rx_errors), -1 },
  1030. { "tx_errors", DEV_STAT(tx_errors), -1 },
  1031. { "rx_dropped", DEV_STAT(rx_dropped), -1 },
  1032. { "tx_dropped", DEV_STAT(tx_dropped), -1 },
  1033. { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETH_MIB_RX_GD_OCTETS},
  1034. { "rx_good_pkts", GEN_STAT(mib.rx_gd_pkts), ETH_MIB_RX_GD_PKTS },
  1035. { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETH_MIB_RX_BRDCAST },
  1036. { "rx_multicast", GEN_STAT(mib.rx_mult), ETH_MIB_RX_MULT },
  1037. { "rx_64_octets", GEN_STAT(mib.rx_64), ETH_MIB_RX_64 },
  1038. { "rx_65_127_oct", GEN_STAT(mib.rx_65_127), ETH_MIB_RX_65_127 },
  1039. { "rx_128_255_oct", GEN_STAT(mib.rx_128_255), ETH_MIB_RX_128_255 },
  1040. { "rx_256_511_oct", GEN_STAT(mib.rx_256_511), ETH_MIB_RX_256_511 },
  1041. { "rx_512_1023_oct", GEN_STAT(mib.rx_512_1023), ETH_MIB_RX_512_1023 },
  1042. { "rx_1024_max_oct", GEN_STAT(mib.rx_1024_max), ETH_MIB_RX_1024_MAX },
  1043. { "rx_jabber", GEN_STAT(mib.rx_jab), ETH_MIB_RX_JAB },
  1044. { "rx_oversize", GEN_STAT(mib.rx_ovr), ETH_MIB_RX_OVR },
  1045. { "rx_fragment", GEN_STAT(mib.rx_frag), ETH_MIB_RX_FRAG },
  1046. { "rx_dropped", GEN_STAT(mib.rx_drop), ETH_MIB_RX_DROP },
  1047. { "rx_crc_align", GEN_STAT(mib.rx_crc_align), ETH_MIB_RX_CRC_ALIGN },
  1048. { "rx_undersize", GEN_STAT(mib.rx_und), ETH_MIB_RX_UND },
  1049. { "rx_crc", GEN_STAT(mib.rx_crc), ETH_MIB_RX_CRC },
  1050. { "rx_align", GEN_STAT(mib.rx_align), ETH_MIB_RX_ALIGN },
  1051. { "rx_symbol_error", GEN_STAT(mib.rx_sym), ETH_MIB_RX_SYM },
  1052. { "rx_pause", GEN_STAT(mib.rx_pause), ETH_MIB_RX_PAUSE },
  1053. { "rx_control", GEN_STAT(mib.rx_cntrl), ETH_MIB_RX_CNTRL },
  1054. { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETH_MIB_TX_GD_OCTETS },
  1055. { "tx_good_pkts", GEN_STAT(mib.tx_gd_pkts), ETH_MIB_TX_GD_PKTS },
  1056. { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETH_MIB_TX_BRDCAST },
  1057. { "tx_multicast", GEN_STAT(mib.tx_mult), ETH_MIB_TX_MULT },
  1058. { "tx_64_oct", GEN_STAT(mib.tx_64), ETH_MIB_TX_64 },
  1059. { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETH_MIB_TX_65_127 },
  1060. { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETH_MIB_TX_128_255 },
  1061. { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETH_MIB_TX_256_511 },
  1062. { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETH_MIB_TX_512_1023},
  1063. { "tx_1024_max_oct", GEN_STAT(mib.tx_1024_max), ETH_MIB_TX_1024_MAX },
  1064. { "tx_jabber", GEN_STAT(mib.tx_jab), ETH_MIB_TX_JAB },
  1065. { "tx_oversize", GEN_STAT(mib.tx_ovr), ETH_MIB_TX_OVR },
  1066. { "tx_fragment", GEN_STAT(mib.tx_frag), ETH_MIB_TX_FRAG },
  1067. { "tx_underrun", GEN_STAT(mib.tx_underrun), ETH_MIB_TX_UNDERRUN },
  1068. { "tx_collisions", GEN_STAT(mib.tx_col), ETH_MIB_TX_COL },
  1069. { "tx_single_collision", GEN_STAT(mib.tx_1_col), ETH_MIB_TX_1_COL },
  1070. { "tx_multiple_collision", GEN_STAT(mib.tx_m_col), ETH_MIB_TX_M_COL },
  1071. { "tx_excess_collision", GEN_STAT(mib.tx_ex_col), ETH_MIB_TX_EX_COL },
  1072. { "tx_late_collision", GEN_STAT(mib.tx_late), ETH_MIB_TX_LATE },
  1073. { "tx_deferred", GEN_STAT(mib.tx_def), ETH_MIB_TX_DEF },
  1074. { "tx_carrier_sense", GEN_STAT(mib.tx_crs), ETH_MIB_TX_CRS },
  1075. { "tx_pause", GEN_STAT(mib.tx_pause), ETH_MIB_TX_PAUSE },
  1076. };
  1077. #define BCM_ENET_STATS_LEN ARRAY_SIZE(bcm_enet_gstrings_stats)
  1078. static const u32 unused_mib_regs[] = {
  1079. ETH_MIB_TX_ALL_OCTETS,
  1080. ETH_MIB_TX_ALL_PKTS,
  1081. ETH_MIB_RX_ALL_OCTETS,
  1082. ETH_MIB_RX_ALL_PKTS,
  1083. };
  1084. static void bcm_enet_get_drvinfo(struct net_device *netdev,
  1085. struct ethtool_drvinfo *drvinfo)
  1086. {
  1087. strscpy(drvinfo->driver, bcm_enet_driver_name, sizeof(drvinfo->driver));
  1088. strscpy(drvinfo->bus_info, "bcm63xx", sizeof(drvinfo->bus_info));
  1089. }
  1090. static int bcm_enet_get_sset_count(struct net_device *netdev,
  1091. int string_set)
  1092. {
  1093. switch (string_set) {
  1094. case ETH_SS_STATS:
  1095. return BCM_ENET_STATS_LEN;
  1096. default:
  1097. return -EINVAL;
  1098. }
  1099. }
  1100. static void bcm_enet_get_strings(struct net_device *netdev,
  1101. u32 stringset, u8 *data)
  1102. {
  1103. int i;
  1104. switch (stringset) {
  1105. case ETH_SS_STATS:
  1106. for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
  1107. memcpy(data + i * ETH_GSTRING_LEN,
  1108. bcm_enet_gstrings_stats[i].stat_string,
  1109. ETH_GSTRING_LEN);
  1110. }
  1111. break;
  1112. }
  1113. }
  1114. static void update_mib_counters(struct bcm_enet_priv *priv)
  1115. {
  1116. int i;
  1117. for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
  1118. const struct bcm_enet_stats *s;
  1119. u32 val;
  1120. char *p;
  1121. s = &bcm_enet_gstrings_stats[i];
  1122. if (s->mib_reg == -1)
  1123. continue;
  1124. val = enet_readl(priv, ENET_MIB_REG(s->mib_reg));
  1125. p = (char *)priv + s->stat_offset;
  1126. if (s->sizeof_stat == sizeof(u64))
  1127. *(u64 *)p += val;
  1128. else
  1129. *(u32 *)p += val;
  1130. }
  1131. /* also empty unused mib counters to make sure mib counter
  1132. * overflow interrupt is cleared */
  1133. for (i = 0; i < ARRAY_SIZE(unused_mib_regs); i++)
  1134. (void)enet_readl(priv, ENET_MIB_REG(unused_mib_regs[i]));
  1135. }
  1136. static void bcm_enet_update_mib_counters_defer(struct work_struct *t)
  1137. {
  1138. struct bcm_enet_priv *priv;
  1139. priv = container_of(t, struct bcm_enet_priv, mib_update_task);
  1140. mutex_lock(&priv->mib_update_lock);
  1141. update_mib_counters(priv);
  1142. mutex_unlock(&priv->mib_update_lock);
  1143. /* reenable mib interrupt */
  1144. if (netif_running(priv->net_dev))
  1145. enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
  1146. }
  1147. static void bcm_enet_get_ethtool_stats(struct net_device *netdev,
  1148. struct ethtool_stats *stats,
  1149. u64 *data)
  1150. {
  1151. struct bcm_enet_priv *priv;
  1152. int i;
  1153. priv = netdev_priv(netdev);
  1154. mutex_lock(&priv->mib_update_lock);
  1155. update_mib_counters(priv);
  1156. for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
  1157. const struct bcm_enet_stats *s;
  1158. char *p;
  1159. s = &bcm_enet_gstrings_stats[i];
  1160. if (s->mib_reg == -1)
  1161. p = (char *)&netdev->stats;
  1162. else
  1163. p = (char *)priv;
  1164. p += s->stat_offset;
  1165. data[i] = (s->sizeof_stat == sizeof(u64)) ?
  1166. *(u64 *)p : *(u32 *)p;
  1167. }
  1168. mutex_unlock(&priv->mib_update_lock);
  1169. }
  1170. static int bcm_enet_nway_reset(struct net_device *dev)
  1171. {
  1172. struct bcm_enet_priv *priv;
  1173. priv = netdev_priv(dev);
  1174. if (priv->has_phy)
  1175. return phy_ethtool_nway_reset(dev);
  1176. return -EOPNOTSUPP;
  1177. }
  1178. static int bcm_enet_get_link_ksettings(struct net_device *dev,
  1179. struct ethtool_link_ksettings *cmd)
  1180. {
  1181. struct bcm_enet_priv *priv;
  1182. u32 supported, advertising;
  1183. priv = netdev_priv(dev);
  1184. if (priv->has_phy) {
  1185. if (!dev->phydev)
  1186. return -ENODEV;
  1187. phy_ethtool_ksettings_get(dev->phydev, cmd);
  1188. return 0;
  1189. } else {
  1190. cmd->base.autoneg = 0;
  1191. cmd->base.speed = (priv->force_speed_100) ?
  1192. SPEED_100 : SPEED_10;
  1193. cmd->base.duplex = (priv->force_duplex_full) ?
  1194. DUPLEX_FULL : DUPLEX_HALF;
  1195. supported = ADVERTISED_10baseT_Half |
  1196. ADVERTISED_10baseT_Full |
  1197. ADVERTISED_100baseT_Half |
  1198. ADVERTISED_100baseT_Full;
  1199. advertising = 0;
  1200. ethtool_convert_legacy_u32_to_link_mode(
  1201. cmd->link_modes.supported, supported);
  1202. ethtool_convert_legacy_u32_to_link_mode(
  1203. cmd->link_modes.advertising, advertising);
  1204. cmd->base.port = PORT_MII;
  1205. }
  1206. return 0;
  1207. }
  1208. static int bcm_enet_set_link_ksettings(struct net_device *dev,
  1209. const struct ethtool_link_ksettings *cmd)
  1210. {
  1211. struct bcm_enet_priv *priv;
  1212. priv = netdev_priv(dev);
  1213. if (priv->has_phy) {
  1214. if (!dev->phydev)
  1215. return -ENODEV;
  1216. return phy_ethtool_ksettings_set(dev->phydev, cmd);
  1217. } else {
  1218. if (cmd->base.autoneg ||
  1219. (cmd->base.speed != SPEED_100 &&
  1220. cmd->base.speed != SPEED_10) ||
  1221. cmd->base.port != PORT_MII)
  1222. return -EINVAL;
  1223. priv->force_speed_100 =
  1224. (cmd->base.speed == SPEED_100) ? 1 : 0;
  1225. priv->force_duplex_full =
  1226. (cmd->base.duplex == DUPLEX_FULL) ? 1 : 0;
  1227. if (netif_running(dev))
  1228. bcm_enet_adjust_link(dev);
  1229. return 0;
  1230. }
  1231. }
  1232. static void
  1233. bcm_enet_get_ringparam(struct net_device *dev,
  1234. struct ethtool_ringparam *ering,
  1235. struct kernel_ethtool_ringparam *kernel_ering,
  1236. struct netlink_ext_ack *extack)
  1237. {
  1238. struct bcm_enet_priv *priv;
  1239. priv = netdev_priv(dev);
  1240. /* rx/tx ring is actually only limited by memory */
  1241. ering->rx_max_pending = 8192;
  1242. ering->tx_max_pending = 8192;
  1243. ering->rx_pending = priv->rx_ring_size;
  1244. ering->tx_pending = priv->tx_ring_size;
  1245. }
  1246. static int bcm_enet_set_ringparam(struct net_device *dev,
  1247. struct ethtool_ringparam *ering,
  1248. struct kernel_ethtool_ringparam *kernel_ering,
  1249. struct netlink_ext_ack *extack)
  1250. {
  1251. struct bcm_enet_priv *priv;
  1252. int was_running;
  1253. priv = netdev_priv(dev);
  1254. was_running = 0;
  1255. if (netif_running(dev)) {
  1256. bcm_enet_stop(dev);
  1257. was_running = 1;
  1258. }
  1259. priv->rx_ring_size = ering->rx_pending;
  1260. priv->tx_ring_size = ering->tx_pending;
  1261. if (was_running) {
  1262. int err;
  1263. err = bcm_enet_open(dev);
  1264. if (err)
  1265. dev_close(dev);
  1266. else
  1267. bcm_enet_set_multicast_list(dev);
  1268. }
  1269. return 0;
  1270. }
  1271. static void bcm_enet_get_pauseparam(struct net_device *dev,
  1272. struct ethtool_pauseparam *ecmd)
  1273. {
  1274. struct bcm_enet_priv *priv;
  1275. priv = netdev_priv(dev);
  1276. ecmd->autoneg = priv->pause_auto;
  1277. ecmd->rx_pause = priv->pause_rx;
  1278. ecmd->tx_pause = priv->pause_tx;
  1279. }
  1280. static int bcm_enet_set_pauseparam(struct net_device *dev,
  1281. struct ethtool_pauseparam *ecmd)
  1282. {
  1283. struct bcm_enet_priv *priv;
  1284. priv = netdev_priv(dev);
  1285. if (priv->has_phy) {
  1286. if (ecmd->autoneg && (ecmd->rx_pause != ecmd->tx_pause)) {
  1287. /* asymetric pause mode not supported,
  1288. * actually possible but integrated PHY has RO
  1289. * asym_pause bit */
  1290. return -EINVAL;
  1291. }
  1292. } else {
  1293. /* no pause autoneg on direct mii connection */
  1294. if (ecmd->autoneg)
  1295. return -EINVAL;
  1296. }
  1297. priv->pause_auto = ecmd->autoneg;
  1298. priv->pause_rx = ecmd->rx_pause;
  1299. priv->pause_tx = ecmd->tx_pause;
  1300. return 0;
  1301. }
  1302. static const struct ethtool_ops bcm_enet_ethtool_ops = {
  1303. .get_strings = bcm_enet_get_strings,
  1304. .get_sset_count = bcm_enet_get_sset_count,
  1305. .get_ethtool_stats = bcm_enet_get_ethtool_stats,
  1306. .nway_reset = bcm_enet_nway_reset,
  1307. .get_drvinfo = bcm_enet_get_drvinfo,
  1308. .get_link = ethtool_op_get_link,
  1309. .get_ringparam = bcm_enet_get_ringparam,
  1310. .set_ringparam = bcm_enet_set_ringparam,
  1311. .get_pauseparam = bcm_enet_get_pauseparam,
  1312. .set_pauseparam = bcm_enet_set_pauseparam,
  1313. .get_link_ksettings = bcm_enet_get_link_ksettings,
  1314. .set_link_ksettings = bcm_enet_set_link_ksettings,
  1315. };
  1316. static int bcm_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1317. {
  1318. struct bcm_enet_priv *priv;
  1319. priv = netdev_priv(dev);
  1320. if (priv->has_phy) {
  1321. if (!dev->phydev)
  1322. return -ENODEV;
  1323. return phy_mii_ioctl(dev->phydev, rq, cmd);
  1324. } else {
  1325. struct mii_if_info mii;
  1326. mii.dev = dev;
  1327. mii.mdio_read = bcm_enet_mdio_read_mii;
  1328. mii.mdio_write = bcm_enet_mdio_write_mii;
  1329. mii.phy_id = 0;
  1330. mii.phy_id_mask = 0x3f;
  1331. mii.reg_num_mask = 0x1f;
  1332. return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
  1333. }
  1334. }
  1335. /*
  1336. * adjust mtu, can't be called while device is running
  1337. */
  1338. static int bcm_enet_change_mtu(struct net_device *dev, int new_mtu)
  1339. {
  1340. struct bcm_enet_priv *priv = netdev_priv(dev);
  1341. int actual_mtu = new_mtu;
  1342. if (netif_running(dev))
  1343. return -EBUSY;
  1344. /* add ethernet header + vlan tag size */
  1345. actual_mtu += VLAN_ETH_HLEN;
  1346. /*
  1347. * setup maximum size before we get overflow mark in
  1348. * descriptor, note that this will not prevent reception of
  1349. * big frames, they will be split into multiple buffers
  1350. * anyway
  1351. */
  1352. priv->hw_mtu = actual_mtu;
  1353. /*
  1354. * align rx buffer size to dma burst len, account FCS since
  1355. * it's appended
  1356. */
  1357. priv->rx_buf_size = ALIGN(actual_mtu + ETH_FCS_LEN,
  1358. priv->dma_maxburst * 4);
  1359. priv->rx_frag_size = SKB_DATA_ALIGN(priv->rx_buf_offset + priv->rx_buf_size) +
  1360. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1361. WRITE_ONCE(dev->mtu, new_mtu);
  1362. return 0;
  1363. }
  1364. /*
  1365. * preinit hardware to allow mii operation while device is down
  1366. */
  1367. static void bcm_enet_hw_preinit(struct bcm_enet_priv *priv)
  1368. {
  1369. u32 val;
  1370. int limit;
  1371. /* make sure mac is disabled */
  1372. bcm_enet_disable_mac(priv);
  1373. /* soft reset mac */
  1374. val = ENET_CTL_SRESET_MASK;
  1375. enet_writel(priv, val, ENET_CTL_REG);
  1376. wmb();
  1377. limit = 1000;
  1378. do {
  1379. val = enet_readl(priv, ENET_CTL_REG);
  1380. if (!(val & ENET_CTL_SRESET_MASK))
  1381. break;
  1382. udelay(1);
  1383. } while (limit--);
  1384. /* select correct mii interface */
  1385. val = enet_readl(priv, ENET_CTL_REG);
  1386. if (priv->use_external_mii)
  1387. val |= ENET_CTL_EPHYSEL_MASK;
  1388. else
  1389. val &= ~ENET_CTL_EPHYSEL_MASK;
  1390. enet_writel(priv, val, ENET_CTL_REG);
  1391. /* turn on mdc clock */
  1392. enet_writel(priv, (0x1f << ENET_MIISC_MDCFREQDIV_SHIFT) |
  1393. ENET_MIISC_PREAMBLEEN_MASK, ENET_MIISC_REG);
  1394. /* set mib counters to self-clear when read */
  1395. val = enet_readl(priv, ENET_MIBCTL_REG);
  1396. val |= ENET_MIBCTL_RDCLEAR_MASK;
  1397. enet_writel(priv, val, ENET_MIBCTL_REG);
  1398. }
  1399. static const struct net_device_ops bcm_enet_ops = {
  1400. .ndo_open = bcm_enet_open,
  1401. .ndo_stop = bcm_enet_stop,
  1402. .ndo_start_xmit = bcm_enet_start_xmit,
  1403. .ndo_set_mac_address = bcm_enet_set_mac_address,
  1404. .ndo_set_rx_mode = bcm_enet_set_multicast_list,
  1405. .ndo_eth_ioctl = bcm_enet_ioctl,
  1406. .ndo_change_mtu = bcm_enet_change_mtu,
  1407. };
  1408. /*
  1409. * allocate netdevice, request register memory and register device.
  1410. */
  1411. static int bcm_enet_probe(struct platform_device *pdev)
  1412. {
  1413. struct bcm_enet_priv *priv;
  1414. struct net_device *dev;
  1415. struct bcm63xx_enet_platform_data *pd;
  1416. int irq, irq_rx, irq_tx;
  1417. struct mii_bus *bus;
  1418. int i, ret;
  1419. if (!bcm_enet_shared_base[0])
  1420. return -EPROBE_DEFER;
  1421. irq = platform_get_irq(pdev, 0);
  1422. irq_rx = platform_get_irq(pdev, 1);
  1423. irq_tx = platform_get_irq(pdev, 2);
  1424. if (irq < 0 || irq_rx < 0 || irq_tx < 0)
  1425. return -ENODEV;
  1426. dev = alloc_etherdev(sizeof(*priv));
  1427. if (!dev)
  1428. return -ENOMEM;
  1429. priv = netdev_priv(dev);
  1430. priv->enet_is_sw = false;
  1431. priv->dma_maxburst = BCMENET_DMA_MAXBURST;
  1432. priv->rx_buf_offset = NET_SKB_PAD;
  1433. ret = bcm_enet_change_mtu(dev, dev->mtu);
  1434. if (ret)
  1435. goto out;
  1436. priv->base = devm_platform_ioremap_resource(pdev, 0);
  1437. if (IS_ERR(priv->base)) {
  1438. ret = PTR_ERR(priv->base);
  1439. goto out;
  1440. }
  1441. dev->irq = priv->irq = irq;
  1442. priv->irq_rx = irq_rx;
  1443. priv->irq_tx = irq_tx;
  1444. priv->mac_clk = devm_clk_get(&pdev->dev, "enet");
  1445. if (IS_ERR(priv->mac_clk)) {
  1446. ret = PTR_ERR(priv->mac_clk);
  1447. goto out;
  1448. }
  1449. ret = clk_prepare_enable(priv->mac_clk);
  1450. if (ret)
  1451. goto out;
  1452. /* initialize default and fetch platform data */
  1453. priv->rx_ring_size = BCMENET_DEF_RX_DESC;
  1454. priv->tx_ring_size = BCMENET_DEF_TX_DESC;
  1455. pd = dev_get_platdata(&pdev->dev);
  1456. if (pd) {
  1457. eth_hw_addr_set(dev, pd->mac_addr);
  1458. priv->has_phy = pd->has_phy;
  1459. priv->phy_id = pd->phy_id;
  1460. priv->has_phy_interrupt = pd->has_phy_interrupt;
  1461. priv->phy_interrupt = pd->phy_interrupt;
  1462. priv->use_external_mii = !pd->use_internal_phy;
  1463. priv->pause_auto = pd->pause_auto;
  1464. priv->pause_rx = pd->pause_rx;
  1465. priv->pause_tx = pd->pause_tx;
  1466. priv->force_duplex_full = pd->force_duplex_full;
  1467. priv->force_speed_100 = pd->force_speed_100;
  1468. priv->dma_chan_en_mask = pd->dma_chan_en_mask;
  1469. priv->dma_chan_int_mask = pd->dma_chan_int_mask;
  1470. priv->dma_chan_width = pd->dma_chan_width;
  1471. priv->dma_has_sram = pd->dma_has_sram;
  1472. priv->dma_desc_shift = pd->dma_desc_shift;
  1473. priv->rx_chan = pd->rx_chan;
  1474. priv->tx_chan = pd->tx_chan;
  1475. }
  1476. if (priv->has_phy && !priv->use_external_mii) {
  1477. /* using internal PHY, enable clock */
  1478. priv->phy_clk = devm_clk_get(&pdev->dev, "ephy");
  1479. if (IS_ERR(priv->phy_clk)) {
  1480. ret = PTR_ERR(priv->phy_clk);
  1481. priv->phy_clk = NULL;
  1482. goto out_disable_clk_mac;
  1483. }
  1484. ret = clk_prepare_enable(priv->phy_clk);
  1485. if (ret)
  1486. goto out_disable_clk_mac;
  1487. }
  1488. /* do minimal hardware init to be able to probe mii bus */
  1489. bcm_enet_hw_preinit(priv);
  1490. /* MII bus registration */
  1491. if (priv->has_phy) {
  1492. priv->mii_bus = mdiobus_alloc();
  1493. if (!priv->mii_bus) {
  1494. ret = -ENOMEM;
  1495. goto out_uninit_hw;
  1496. }
  1497. bus = priv->mii_bus;
  1498. bus->name = "bcm63xx_enet MII bus";
  1499. bus->parent = &pdev->dev;
  1500. bus->priv = priv;
  1501. bus->read = bcm_enet_mdio_read_phylib;
  1502. bus->write = bcm_enet_mdio_write_phylib;
  1503. sprintf(bus->id, "%s-%d", pdev->name, pdev->id);
  1504. /* only probe bus where we think the PHY is, because
  1505. * the mdio read operation return 0 instead of 0xffff
  1506. * if a slave is not present on hw */
  1507. bus->phy_mask = ~(1 << priv->phy_id);
  1508. if (priv->has_phy_interrupt)
  1509. bus->irq[priv->phy_id] = priv->phy_interrupt;
  1510. ret = mdiobus_register(bus);
  1511. if (ret) {
  1512. dev_err(&pdev->dev, "unable to register mdio bus\n");
  1513. goto out_free_mdio;
  1514. }
  1515. } else {
  1516. /* run platform code to initialize PHY device */
  1517. if (pd && pd->mii_config &&
  1518. pd->mii_config(dev, 1, bcm_enet_mdio_read_mii,
  1519. bcm_enet_mdio_write_mii)) {
  1520. dev_err(&pdev->dev, "unable to configure mdio bus\n");
  1521. goto out_uninit_hw;
  1522. }
  1523. }
  1524. spin_lock_init(&priv->rx_lock);
  1525. /* init rx timeout (used for oom) */
  1526. timer_setup(&priv->rx_timeout, bcm_enet_refill_rx_timer, 0);
  1527. /* init the mib update lock&work */
  1528. mutex_init(&priv->mib_update_lock);
  1529. INIT_WORK(&priv->mib_update_task, bcm_enet_update_mib_counters_defer);
  1530. /* zero mib counters */
  1531. for (i = 0; i < ENET_MIB_REG_COUNT; i++)
  1532. enet_writel(priv, 0, ENET_MIB_REG(i));
  1533. /* register netdevice */
  1534. dev->netdev_ops = &bcm_enet_ops;
  1535. netif_napi_add_weight(dev, &priv->napi, bcm_enet_poll, 16);
  1536. dev->ethtool_ops = &bcm_enet_ethtool_ops;
  1537. /* MTU range: 46 - 2028 */
  1538. dev->min_mtu = ETH_ZLEN - ETH_HLEN;
  1539. dev->max_mtu = BCMENET_MAX_MTU - VLAN_ETH_HLEN;
  1540. SET_NETDEV_DEV(dev, &pdev->dev);
  1541. ret = register_netdev(dev);
  1542. if (ret)
  1543. goto out_unregister_mdio;
  1544. netif_carrier_off(dev);
  1545. platform_set_drvdata(pdev, dev);
  1546. priv->pdev = pdev;
  1547. priv->net_dev = dev;
  1548. return 0;
  1549. out_unregister_mdio:
  1550. if (priv->mii_bus)
  1551. mdiobus_unregister(priv->mii_bus);
  1552. out_free_mdio:
  1553. if (priv->mii_bus)
  1554. mdiobus_free(priv->mii_bus);
  1555. out_uninit_hw:
  1556. /* turn off mdc clock */
  1557. enet_writel(priv, 0, ENET_MIISC_REG);
  1558. clk_disable_unprepare(priv->phy_clk);
  1559. out_disable_clk_mac:
  1560. clk_disable_unprepare(priv->mac_clk);
  1561. out:
  1562. free_netdev(dev);
  1563. return ret;
  1564. }
  1565. /*
  1566. * exit func, stops hardware and unregisters netdevice
  1567. */
  1568. static void bcm_enet_remove(struct platform_device *pdev)
  1569. {
  1570. struct bcm_enet_priv *priv;
  1571. struct net_device *dev;
  1572. /* stop netdevice */
  1573. dev = platform_get_drvdata(pdev);
  1574. priv = netdev_priv(dev);
  1575. unregister_netdev(dev);
  1576. /* turn off mdc clock */
  1577. enet_writel(priv, 0, ENET_MIISC_REG);
  1578. if (priv->has_phy) {
  1579. mdiobus_unregister(priv->mii_bus);
  1580. mdiobus_free(priv->mii_bus);
  1581. } else {
  1582. struct bcm63xx_enet_platform_data *pd;
  1583. pd = dev_get_platdata(&pdev->dev);
  1584. if (pd && pd->mii_config)
  1585. pd->mii_config(dev, 0, bcm_enet_mdio_read_mii,
  1586. bcm_enet_mdio_write_mii);
  1587. }
  1588. /* disable hw block clocks */
  1589. clk_disable_unprepare(priv->phy_clk);
  1590. clk_disable_unprepare(priv->mac_clk);
  1591. free_netdev(dev);
  1592. }
  1593. static struct platform_driver bcm63xx_enet_driver = {
  1594. .probe = bcm_enet_probe,
  1595. .remove_new = bcm_enet_remove,
  1596. .driver = {
  1597. .name = "bcm63xx_enet",
  1598. },
  1599. };
  1600. /*
  1601. * switch mii access callbacks
  1602. */
  1603. static int bcmenet_sw_mdio_read(struct bcm_enet_priv *priv,
  1604. int ext, int phy_id, int location)
  1605. {
  1606. u32 reg;
  1607. int ret;
  1608. spin_lock_bh(&priv->enetsw_mdio_lock);
  1609. enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
  1610. reg = ENETSW_MDIOC_RD_MASK |
  1611. (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
  1612. (location << ENETSW_MDIOC_REG_SHIFT);
  1613. if (ext)
  1614. reg |= ENETSW_MDIOC_EXT_MASK;
  1615. enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
  1616. udelay(50);
  1617. ret = enetsw_readw(priv, ENETSW_MDIOD_REG);
  1618. spin_unlock_bh(&priv->enetsw_mdio_lock);
  1619. return ret;
  1620. }
  1621. static void bcmenet_sw_mdio_write(struct bcm_enet_priv *priv,
  1622. int ext, int phy_id, int location,
  1623. uint16_t data)
  1624. {
  1625. u32 reg;
  1626. spin_lock_bh(&priv->enetsw_mdio_lock);
  1627. enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
  1628. reg = ENETSW_MDIOC_WR_MASK |
  1629. (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
  1630. (location << ENETSW_MDIOC_REG_SHIFT);
  1631. if (ext)
  1632. reg |= ENETSW_MDIOC_EXT_MASK;
  1633. reg |= data;
  1634. enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
  1635. udelay(50);
  1636. spin_unlock_bh(&priv->enetsw_mdio_lock);
  1637. }
  1638. static inline int bcm_enet_port_is_rgmii(int portid)
  1639. {
  1640. return portid >= ENETSW_RGMII_PORT0;
  1641. }
  1642. /*
  1643. * enet sw PHY polling
  1644. */
  1645. static void swphy_poll_timer(struct timer_list *t)
  1646. {
  1647. struct bcm_enet_priv *priv = from_timer(priv, t, swphy_poll);
  1648. unsigned int i;
  1649. for (i = 0; i < priv->num_ports; i++) {
  1650. struct bcm63xx_enetsw_port *port;
  1651. int val, j, up, advertise, lpa, speed, duplex, media;
  1652. int external_phy = bcm_enet_port_is_rgmii(i);
  1653. u8 override;
  1654. port = &priv->used_ports[i];
  1655. if (!port->used)
  1656. continue;
  1657. if (port->bypass_link)
  1658. continue;
  1659. /* dummy read to clear */
  1660. for (j = 0; j < 2; j++)
  1661. val = bcmenet_sw_mdio_read(priv, external_phy,
  1662. port->phy_id, MII_BMSR);
  1663. if (val == 0xffff)
  1664. continue;
  1665. up = (val & BMSR_LSTATUS) ? 1 : 0;
  1666. if (!(up ^ priv->sw_port_link[i]))
  1667. continue;
  1668. priv->sw_port_link[i] = up;
  1669. /* link changed */
  1670. if (!up) {
  1671. dev_info(&priv->pdev->dev, "link DOWN on %s\n",
  1672. port->name);
  1673. enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
  1674. ENETSW_PORTOV_REG(i));
  1675. enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
  1676. ENETSW_PTCTRL_TXDIS_MASK,
  1677. ENETSW_PTCTRL_REG(i));
  1678. continue;
  1679. }
  1680. advertise = bcmenet_sw_mdio_read(priv, external_phy,
  1681. port->phy_id, MII_ADVERTISE);
  1682. lpa = bcmenet_sw_mdio_read(priv, external_phy, port->phy_id,
  1683. MII_LPA);
  1684. /* figure out media and duplex from advertise and LPA values */
  1685. media = mii_nway_result(lpa & advertise);
  1686. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  1687. if (media & (ADVERTISE_100FULL | ADVERTISE_100HALF))
  1688. speed = 100;
  1689. else
  1690. speed = 10;
  1691. if (val & BMSR_ESTATEN) {
  1692. advertise = bcmenet_sw_mdio_read(priv, external_phy,
  1693. port->phy_id, MII_CTRL1000);
  1694. lpa = bcmenet_sw_mdio_read(priv, external_phy,
  1695. port->phy_id, MII_STAT1000);
  1696. if (advertise & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)
  1697. && lpa & (LPA_1000FULL | LPA_1000HALF)) {
  1698. speed = 1000;
  1699. duplex = (lpa & LPA_1000FULL);
  1700. }
  1701. }
  1702. dev_info(&priv->pdev->dev,
  1703. "link UP on %s, %dMbps, %s-duplex\n",
  1704. port->name, speed, duplex ? "full" : "half");
  1705. override = ENETSW_PORTOV_ENABLE_MASK |
  1706. ENETSW_PORTOV_LINKUP_MASK;
  1707. if (speed == 1000)
  1708. override |= ENETSW_IMPOV_1000_MASK;
  1709. else if (speed == 100)
  1710. override |= ENETSW_IMPOV_100_MASK;
  1711. if (duplex)
  1712. override |= ENETSW_IMPOV_FDX_MASK;
  1713. enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
  1714. enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
  1715. }
  1716. priv->swphy_poll.expires = jiffies + HZ;
  1717. add_timer(&priv->swphy_poll);
  1718. }
  1719. /*
  1720. * open callback, allocate dma rings & buffers and start rx operation
  1721. */
  1722. static int bcm_enetsw_open(struct net_device *dev)
  1723. {
  1724. struct bcm_enet_priv *priv;
  1725. struct device *kdev;
  1726. int i, ret;
  1727. unsigned int size;
  1728. void *p;
  1729. u32 val;
  1730. priv = netdev_priv(dev);
  1731. kdev = &priv->pdev->dev;
  1732. /* mask all interrupts and request them */
  1733. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  1734. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  1735. ret = request_irq(priv->irq_rx, bcm_enet_isr_dma,
  1736. 0, dev->name, dev);
  1737. if (ret)
  1738. goto out_freeirq;
  1739. if (priv->irq_tx != -1) {
  1740. ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
  1741. 0, dev->name, dev);
  1742. if (ret)
  1743. goto out_freeirq_rx;
  1744. }
  1745. /* allocate rx dma ring */
  1746. size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
  1747. p = dma_alloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
  1748. if (!p) {
  1749. dev_err(kdev, "cannot allocate rx ring %u\n", size);
  1750. ret = -ENOMEM;
  1751. goto out_freeirq_tx;
  1752. }
  1753. priv->rx_desc_alloc_size = size;
  1754. priv->rx_desc_cpu = p;
  1755. /* allocate tx dma ring */
  1756. size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
  1757. p = dma_alloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
  1758. if (!p) {
  1759. dev_err(kdev, "cannot allocate tx ring\n");
  1760. ret = -ENOMEM;
  1761. goto out_free_rx_ring;
  1762. }
  1763. priv->tx_desc_alloc_size = size;
  1764. priv->tx_desc_cpu = p;
  1765. priv->tx_skb = kcalloc(priv->tx_ring_size, sizeof(struct sk_buff *),
  1766. GFP_KERNEL);
  1767. if (!priv->tx_skb) {
  1768. dev_err(kdev, "cannot allocate tx skb queue\n");
  1769. ret = -ENOMEM;
  1770. goto out_free_tx_ring;
  1771. }
  1772. priv->tx_desc_count = priv->tx_ring_size;
  1773. priv->tx_dirty_desc = 0;
  1774. priv->tx_curr_desc = 0;
  1775. spin_lock_init(&priv->tx_lock);
  1776. /* init & fill rx ring with buffers */
  1777. priv->rx_buf = kcalloc(priv->rx_ring_size, sizeof(void *),
  1778. GFP_KERNEL);
  1779. if (!priv->rx_buf) {
  1780. dev_err(kdev, "cannot allocate rx buffer queue\n");
  1781. ret = -ENOMEM;
  1782. goto out_free_tx_skb;
  1783. }
  1784. priv->rx_desc_count = 0;
  1785. priv->rx_dirty_desc = 0;
  1786. priv->rx_curr_desc = 0;
  1787. /* disable all ports */
  1788. for (i = 0; i < priv->num_ports; i++) {
  1789. enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
  1790. ENETSW_PORTOV_REG(i));
  1791. enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
  1792. ENETSW_PTCTRL_TXDIS_MASK,
  1793. ENETSW_PTCTRL_REG(i));
  1794. priv->sw_port_link[i] = 0;
  1795. }
  1796. /* reset mib */
  1797. val = enetsw_readb(priv, ENETSW_GMCR_REG);
  1798. val |= ENETSW_GMCR_RST_MIB_MASK;
  1799. enetsw_writeb(priv, val, ENETSW_GMCR_REG);
  1800. mdelay(1);
  1801. val &= ~ENETSW_GMCR_RST_MIB_MASK;
  1802. enetsw_writeb(priv, val, ENETSW_GMCR_REG);
  1803. mdelay(1);
  1804. /* force CPU port state */
  1805. val = enetsw_readb(priv, ENETSW_IMPOV_REG);
  1806. val |= ENETSW_IMPOV_FORCE_MASK | ENETSW_IMPOV_LINKUP_MASK;
  1807. enetsw_writeb(priv, val, ENETSW_IMPOV_REG);
  1808. /* enable switch forward engine */
  1809. val = enetsw_readb(priv, ENETSW_SWMODE_REG);
  1810. val |= ENETSW_SWMODE_FWD_EN_MASK;
  1811. enetsw_writeb(priv, val, ENETSW_SWMODE_REG);
  1812. /* enable jumbo on all ports */
  1813. enetsw_writel(priv, 0x1ff, ENETSW_JMBCTL_PORT_REG);
  1814. enetsw_writew(priv, 9728, ENETSW_JMBCTL_MAXSIZE_REG);
  1815. /* initialize flow control buffer allocation */
  1816. enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
  1817. ENETDMA_BUFALLOC_REG(priv->rx_chan));
  1818. if (bcm_enet_refill_rx(dev, false)) {
  1819. dev_err(kdev, "cannot allocate rx buffer queue\n");
  1820. ret = -ENOMEM;
  1821. goto out;
  1822. }
  1823. /* write rx & tx ring addresses */
  1824. enet_dmas_writel(priv, priv->rx_desc_dma,
  1825. ENETDMAS_RSTART_REG, priv->rx_chan);
  1826. enet_dmas_writel(priv, priv->tx_desc_dma,
  1827. ENETDMAS_RSTART_REG, priv->tx_chan);
  1828. /* clear remaining state ram for rx & tx channel */
  1829. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
  1830. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
  1831. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
  1832. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
  1833. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
  1834. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
  1835. /* set dma maximum burst len */
  1836. enet_dmac_writel(priv, priv->dma_maxburst,
  1837. ENETDMAC_MAXBURST, priv->rx_chan);
  1838. enet_dmac_writel(priv, priv->dma_maxburst,
  1839. ENETDMAC_MAXBURST, priv->tx_chan);
  1840. /* set flow control low/high threshold to 1/3 / 2/3 */
  1841. val = priv->rx_ring_size / 3;
  1842. enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
  1843. val = (priv->rx_ring_size * 2) / 3;
  1844. enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
  1845. /* all set, enable mac and interrupts, start dma engine and
  1846. * kick rx dma channel
  1847. */
  1848. wmb();
  1849. enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
  1850. enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
  1851. ENETDMAC_CHANCFG, priv->rx_chan);
  1852. /* watch "packet transferred" interrupt in rx and tx */
  1853. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1854. ENETDMAC_IR, priv->rx_chan);
  1855. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1856. ENETDMAC_IR, priv->tx_chan);
  1857. /* make sure we enable napi before rx interrupt */
  1858. napi_enable(&priv->napi);
  1859. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1860. ENETDMAC_IRMASK, priv->rx_chan);
  1861. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1862. ENETDMAC_IRMASK, priv->tx_chan);
  1863. netif_carrier_on(dev);
  1864. netif_start_queue(dev);
  1865. /* apply override config for bypass_link ports here. */
  1866. for (i = 0; i < priv->num_ports; i++) {
  1867. struct bcm63xx_enetsw_port *port;
  1868. u8 override;
  1869. port = &priv->used_ports[i];
  1870. if (!port->used)
  1871. continue;
  1872. if (!port->bypass_link)
  1873. continue;
  1874. override = ENETSW_PORTOV_ENABLE_MASK |
  1875. ENETSW_PORTOV_LINKUP_MASK;
  1876. switch (port->force_speed) {
  1877. case 1000:
  1878. override |= ENETSW_IMPOV_1000_MASK;
  1879. break;
  1880. case 100:
  1881. override |= ENETSW_IMPOV_100_MASK;
  1882. break;
  1883. case 10:
  1884. break;
  1885. default:
  1886. pr_warn("invalid forced speed on port %s: assume 10\n",
  1887. port->name);
  1888. break;
  1889. }
  1890. if (port->force_duplex_full)
  1891. override |= ENETSW_IMPOV_FDX_MASK;
  1892. enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
  1893. enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
  1894. }
  1895. /* start phy polling timer */
  1896. timer_setup(&priv->swphy_poll, swphy_poll_timer, 0);
  1897. mod_timer(&priv->swphy_poll, jiffies);
  1898. return 0;
  1899. out:
  1900. bcm_enet_free_rx_buf_ring(kdev, priv);
  1901. out_free_tx_skb:
  1902. kfree(priv->tx_skb);
  1903. out_free_tx_ring:
  1904. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  1905. priv->tx_desc_cpu, priv->tx_desc_dma);
  1906. out_free_rx_ring:
  1907. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  1908. priv->rx_desc_cpu, priv->rx_desc_dma);
  1909. out_freeirq_tx:
  1910. if (priv->irq_tx != -1)
  1911. free_irq(priv->irq_tx, dev);
  1912. out_freeirq_rx:
  1913. free_irq(priv->irq_rx, dev);
  1914. out_freeirq:
  1915. return ret;
  1916. }
  1917. /* stop callback */
  1918. static int bcm_enetsw_stop(struct net_device *dev)
  1919. {
  1920. struct bcm_enet_priv *priv;
  1921. struct device *kdev;
  1922. priv = netdev_priv(dev);
  1923. kdev = &priv->pdev->dev;
  1924. del_timer_sync(&priv->swphy_poll);
  1925. netif_stop_queue(dev);
  1926. napi_disable(&priv->napi);
  1927. del_timer_sync(&priv->rx_timeout);
  1928. /* mask all interrupts */
  1929. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  1930. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  1931. /* disable dma & mac */
  1932. bcm_enet_disable_dma(priv, priv->tx_chan);
  1933. bcm_enet_disable_dma(priv, priv->rx_chan);
  1934. /* force reclaim of all tx buffers */
  1935. bcm_enet_tx_reclaim(dev, 1, 0);
  1936. /* free the rx buffer ring */
  1937. bcm_enet_free_rx_buf_ring(kdev, priv);
  1938. /* free remaining allocated memory */
  1939. kfree(priv->tx_skb);
  1940. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  1941. priv->rx_desc_cpu, priv->rx_desc_dma);
  1942. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  1943. priv->tx_desc_cpu, priv->tx_desc_dma);
  1944. if (priv->irq_tx != -1)
  1945. free_irq(priv->irq_tx, dev);
  1946. free_irq(priv->irq_rx, dev);
  1947. /* reset BQL after forced tx reclaim to prevent kernel panic */
  1948. netdev_reset_queue(dev);
  1949. return 0;
  1950. }
  1951. /* try to sort out phy external status by walking the used_port field
  1952. * in the bcm_enet_priv structure. in case the phy address is not
  1953. * assigned to any physical port on the switch, assume it is external
  1954. * (and yell at the user).
  1955. */
  1956. static int bcm_enetsw_phy_is_external(struct bcm_enet_priv *priv, int phy_id)
  1957. {
  1958. int i;
  1959. for (i = 0; i < priv->num_ports; ++i) {
  1960. if (!priv->used_ports[i].used)
  1961. continue;
  1962. if (priv->used_ports[i].phy_id == phy_id)
  1963. return bcm_enet_port_is_rgmii(i);
  1964. }
  1965. printk_once(KERN_WARNING "bcm63xx_enet: could not find a used port with phy_id %i, assuming phy is external\n",
  1966. phy_id);
  1967. return 1;
  1968. }
  1969. /* can't use bcmenet_sw_mdio_read directly as we need to sort out
  1970. * external/internal status of the given phy_id first.
  1971. */
  1972. static int bcm_enetsw_mii_mdio_read(struct net_device *dev, int phy_id,
  1973. int location)
  1974. {
  1975. struct bcm_enet_priv *priv;
  1976. priv = netdev_priv(dev);
  1977. return bcmenet_sw_mdio_read(priv,
  1978. bcm_enetsw_phy_is_external(priv, phy_id),
  1979. phy_id, location);
  1980. }
  1981. /* can't use bcmenet_sw_mdio_write directly as we need to sort out
  1982. * external/internal status of the given phy_id first.
  1983. */
  1984. static void bcm_enetsw_mii_mdio_write(struct net_device *dev, int phy_id,
  1985. int location,
  1986. int val)
  1987. {
  1988. struct bcm_enet_priv *priv;
  1989. priv = netdev_priv(dev);
  1990. bcmenet_sw_mdio_write(priv, bcm_enetsw_phy_is_external(priv, phy_id),
  1991. phy_id, location, val);
  1992. }
  1993. static int bcm_enetsw_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1994. {
  1995. struct mii_if_info mii;
  1996. mii.dev = dev;
  1997. mii.mdio_read = bcm_enetsw_mii_mdio_read;
  1998. mii.mdio_write = bcm_enetsw_mii_mdio_write;
  1999. mii.phy_id = 0;
  2000. mii.phy_id_mask = 0x3f;
  2001. mii.reg_num_mask = 0x1f;
  2002. return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
  2003. }
  2004. static const struct net_device_ops bcm_enetsw_ops = {
  2005. .ndo_open = bcm_enetsw_open,
  2006. .ndo_stop = bcm_enetsw_stop,
  2007. .ndo_start_xmit = bcm_enet_start_xmit,
  2008. .ndo_change_mtu = bcm_enet_change_mtu,
  2009. .ndo_eth_ioctl = bcm_enetsw_ioctl,
  2010. };
  2011. static const struct bcm_enet_stats bcm_enetsw_gstrings_stats[] = {
  2012. { "rx_packets", DEV_STAT(rx_packets), -1 },
  2013. { "tx_packets", DEV_STAT(tx_packets), -1 },
  2014. { "rx_bytes", DEV_STAT(rx_bytes), -1 },
  2015. { "tx_bytes", DEV_STAT(tx_bytes), -1 },
  2016. { "rx_errors", DEV_STAT(rx_errors), -1 },
  2017. { "tx_errors", DEV_STAT(tx_errors), -1 },
  2018. { "rx_dropped", DEV_STAT(rx_dropped), -1 },
  2019. { "tx_dropped", DEV_STAT(tx_dropped), -1 },
  2020. { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETHSW_MIB_RX_GD_OCT },
  2021. { "tx_unicast", GEN_STAT(mib.tx_unicast), ETHSW_MIB_RX_BRDCAST },
  2022. { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETHSW_MIB_RX_BRDCAST },
  2023. { "tx_multicast", GEN_STAT(mib.tx_mult), ETHSW_MIB_RX_MULT },
  2024. { "tx_64_octets", GEN_STAT(mib.tx_64), ETHSW_MIB_RX_64 },
  2025. { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETHSW_MIB_RX_65_127 },
  2026. { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETHSW_MIB_RX_128_255 },
  2027. { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETHSW_MIB_RX_256_511 },
  2028. { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETHSW_MIB_RX_512_1023},
  2029. { "tx_1024_1522_oct", GEN_STAT(mib.tx_1024_max),
  2030. ETHSW_MIB_RX_1024_1522 },
  2031. { "tx_1523_2047_oct", GEN_STAT(mib.tx_1523_2047),
  2032. ETHSW_MIB_RX_1523_2047 },
  2033. { "tx_2048_4095_oct", GEN_STAT(mib.tx_2048_4095),
  2034. ETHSW_MIB_RX_2048_4095 },
  2035. { "tx_4096_8191_oct", GEN_STAT(mib.tx_4096_8191),
  2036. ETHSW_MIB_RX_4096_8191 },
  2037. { "tx_8192_9728_oct", GEN_STAT(mib.tx_8192_9728),
  2038. ETHSW_MIB_RX_8192_9728 },
  2039. { "tx_oversize", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR },
  2040. { "tx_oversize_drop", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR_DISC },
  2041. { "tx_dropped", GEN_STAT(mib.tx_drop), ETHSW_MIB_RX_DROP },
  2042. { "tx_undersize", GEN_STAT(mib.tx_underrun), ETHSW_MIB_RX_UND },
  2043. { "tx_pause", GEN_STAT(mib.tx_pause), ETHSW_MIB_RX_PAUSE },
  2044. { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETHSW_MIB_TX_ALL_OCT },
  2045. { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETHSW_MIB_TX_BRDCAST },
  2046. { "rx_multicast", GEN_STAT(mib.rx_mult), ETHSW_MIB_TX_MULT },
  2047. { "rx_unicast", GEN_STAT(mib.rx_unicast), ETHSW_MIB_TX_MULT },
  2048. { "rx_pause", GEN_STAT(mib.rx_pause), ETHSW_MIB_TX_PAUSE },
  2049. { "rx_dropped", GEN_STAT(mib.rx_drop), ETHSW_MIB_TX_DROP_PKTS },
  2050. };
  2051. #define BCM_ENETSW_STATS_LEN \
  2052. (sizeof(bcm_enetsw_gstrings_stats) / sizeof(struct bcm_enet_stats))
  2053. static void bcm_enetsw_get_strings(struct net_device *netdev,
  2054. u32 stringset, u8 *data)
  2055. {
  2056. int i;
  2057. switch (stringset) {
  2058. case ETH_SS_STATS:
  2059. for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
  2060. memcpy(data + i * ETH_GSTRING_LEN,
  2061. bcm_enetsw_gstrings_stats[i].stat_string,
  2062. ETH_GSTRING_LEN);
  2063. }
  2064. break;
  2065. }
  2066. }
  2067. static int bcm_enetsw_get_sset_count(struct net_device *netdev,
  2068. int string_set)
  2069. {
  2070. switch (string_set) {
  2071. case ETH_SS_STATS:
  2072. return BCM_ENETSW_STATS_LEN;
  2073. default:
  2074. return -EINVAL;
  2075. }
  2076. }
  2077. static void bcm_enetsw_get_drvinfo(struct net_device *netdev,
  2078. struct ethtool_drvinfo *drvinfo)
  2079. {
  2080. strscpy(drvinfo->driver, bcm_enet_driver_name, sizeof(drvinfo->driver));
  2081. strscpy(drvinfo->bus_info, "bcm63xx", sizeof(drvinfo->bus_info));
  2082. }
  2083. static void bcm_enetsw_get_ethtool_stats(struct net_device *netdev,
  2084. struct ethtool_stats *stats,
  2085. u64 *data)
  2086. {
  2087. struct bcm_enet_priv *priv;
  2088. int i;
  2089. priv = netdev_priv(netdev);
  2090. for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
  2091. const struct bcm_enet_stats *s;
  2092. u32 lo, hi;
  2093. char *p;
  2094. int reg;
  2095. s = &bcm_enetsw_gstrings_stats[i];
  2096. reg = s->mib_reg;
  2097. if (reg == -1)
  2098. continue;
  2099. lo = enetsw_readl(priv, ENETSW_MIB_REG(reg));
  2100. p = (char *)priv + s->stat_offset;
  2101. if (s->sizeof_stat == sizeof(u64)) {
  2102. hi = enetsw_readl(priv, ENETSW_MIB_REG(reg + 1));
  2103. *(u64 *)p = ((u64)hi << 32 | lo);
  2104. } else {
  2105. *(u32 *)p = lo;
  2106. }
  2107. }
  2108. for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
  2109. const struct bcm_enet_stats *s;
  2110. char *p;
  2111. s = &bcm_enetsw_gstrings_stats[i];
  2112. if (s->mib_reg == -1)
  2113. p = (char *)&netdev->stats + s->stat_offset;
  2114. else
  2115. p = (char *)priv + s->stat_offset;
  2116. data[i] = (s->sizeof_stat == sizeof(u64)) ?
  2117. *(u64 *)p : *(u32 *)p;
  2118. }
  2119. }
  2120. static void
  2121. bcm_enetsw_get_ringparam(struct net_device *dev,
  2122. struct ethtool_ringparam *ering,
  2123. struct kernel_ethtool_ringparam *kernel_ering,
  2124. struct netlink_ext_ack *extack)
  2125. {
  2126. struct bcm_enet_priv *priv;
  2127. priv = netdev_priv(dev);
  2128. /* rx/tx ring is actually only limited by memory */
  2129. ering->rx_max_pending = 8192;
  2130. ering->tx_max_pending = 8192;
  2131. ering->rx_mini_max_pending = 0;
  2132. ering->rx_jumbo_max_pending = 0;
  2133. ering->rx_pending = priv->rx_ring_size;
  2134. ering->tx_pending = priv->tx_ring_size;
  2135. }
  2136. static int
  2137. bcm_enetsw_set_ringparam(struct net_device *dev,
  2138. struct ethtool_ringparam *ering,
  2139. struct kernel_ethtool_ringparam *kernel_ering,
  2140. struct netlink_ext_ack *extack)
  2141. {
  2142. struct bcm_enet_priv *priv;
  2143. int was_running;
  2144. priv = netdev_priv(dev);
  2145. was_running = 0;
  2146. if (netif_running(dev)) {
  2147. bcm_enetsw_stop(dev);
  2148. was_running = 1;
  2149. }
  2150. priv->rx_ring_size = ering->rx_pending;
  2151. priv->tx_ring_size = ering->tx_pending;
  2152. if (was_running) {
  2153. int err;
  2154. err = bcm_enetsw_open(dev);
  2155. if (err)
  2156. dev_close(dev);
  2157. }
  2158. return 0;
  2159. }
  2160. static const struct ethtool_ops bcm_enetsw_ethtool_ops = {
  2161. .get_strings = bcm_enetsw_get_strings,
  2162. .get_sset_count = bcm_enetsw_get_sset_count,
  2163. .get_ethtool_stats = bcm_enetsw_get_ethtool_stats,
  2164. .get_drvinfo = bcm_enetsw_get_drvinfo,
  2165. .get_ringparam = bcm_enetsw_get_ringparam,
  2166. .set_ringparam = bcm_enetsw_set_ringparam,
  2167. };
  2168. /* allocate netdevice, request register memory and register device. */
  2169. static int bcm_enetsw_probe(struct platform_device *pdev)
  2170. {
  2171. struct bcm_enet_priv *priv;
  2172. struct net_device *dev;
  2173. struct bcm63xx_enetsw_platform_data *pd;
  2174. struct resource *res_mem;
  2175. int ret, irq_rx, irq_tx;
  2176. if (!bcm_enet_shared_base[0])
  2177. return -EPROBE_DEFER;
  2178. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2179. irq_rx = platform_get_irq(pdev, 0);
  2180. irq_tx = platform_get_irq(pdev, 1);
  2181. if (!res_mem || irq_rx < 0)
  2182. return -ENODEV;
  2183. dev = alloc_etherdev(sizeof(*priv));
  2184. if (!dev)
  2185. return -ENOMEM;
  2186. priv = netdev_priv(dev);
  2187. /* initialize default and fetch platform data */
  2188. priv->enet_is_sw = true;
  2189. priv->irq_rx = irq_rx;
  2190. priv->irq_tx = irq_tx;
  2191. priv->rx_ring_size = BCMENET_DEF_RX_DESC;
  2192. priv->tx_ring_size = BCMENET_DEF_TX_DESC;
  2193. priv->dma_maxburst = BCMENETSW_DMA_MAXBURST;
  2194. priv->rx_buf_offset = NET_SKB_PAD + NET_IP_ALIGN;
  2195. pd = dev_get_platdata(&pdev->dev);
  2196. if (pd) {
  2197. eth_hw_addr_set(dev, pd->mac_addr);
  2198. memcpy(priv->used_ports, pd->used_ports,
  2199. sizeof(pd->used_ports));
  2200. priv->num_ports = pd->num_ports;
  2201. priv->dma_has_sram = pd->dma_has_sram;
  2202. priv->dma_chan_en_mask = pd->dma_chan_en_mask;
  2203. priv->dma_chan_int_mask = pd->dma_chan_int_mask;
  2204. priv->dma_chan_width = pd->dma_chan_width;
  2205. }
  2206. ret = bcm_enet_change_mtu(dev, dev->mtu);
  2207. if (ret)
  2208. goto out;
  2209. priv->base = devm_ioremap_resource(&pdev->dev, res_mem);
  2210. if (IS_ERR(priv->base)) {
  2211. ret = PTR_ERR(priv->base);
  2212. goto out;
  2213. }
  2214. priv->mac_clk = devm_clk_get(&pdev->dev, "enetsw");
  2215. if (IS_ERR(priv->mac_clk)) {
  2216. ret = PTR_ERR(priv->mac_clk);
  2217. goto out;
  2218. }
  2219. ret = clk_prepare_enable(priv->mac_clk);
  2220. if (ret)
  2221. goto out;
  2222. priv->rx_chan = 0;
  2223. priv->tx_chan = 1;
  2224. spin_lock_init(&priv->rx_lock);
  2225. /* init rx timeout (used for oom) */
  2226. timer_setup(&priv->rx_timeout, bcm_enet_refill_rx_timer, 0);
  2227. /* register netdevice */
  2228. dev->netdev_ops = &bcm_enetsw_ops;
  2229. netif_napi_add_weight(dev, &priv->napi, bcm_enet_poll, 16);
  2230. dev->ethtool_ops = &bcm_enetsw_ethtool_ops;
  2231. SET_NETDEV_DEV(dev, &pdev->dev);
  2232. spin_lock_init(&priv->enetsw_mdio_lock);
  2233. ret = register_netdev(dev);
  2234. if (ret)
  2235. goto out_disable_clk;
  2236. netif_carrier_off(dev);
  2237. platform_set_drvdata(pdev, dev);
  2238. priv->pdev = pdev;
  2239. priv->net_dev = dev;
  2240. return 0;
  2241. out_disable_clk:
  2242. clk_disable_unprepare(priv->mac_clk);
  2243. out:
  2244. free_netdev(dev);
  2245. return ret;
  2246. }
  2247. /* exit func, stops hardware and unregisters netdevice */
  2248. static void bcm_enetsw_remove(struct platform_device *pdev)
  2249. {
  2250. struct bcm_enet_priv *priv;
  2251. struct net_device *dev;
  2252. /* stop netdevice */
  2253. dev = platform_get_drvdata(pdev);
  2254. priv = netdev_priv(dev);
  2255. unregister_netdev(dev);
  2256. clk_disable_unprepare(priv->mac_clk);
  2257. free_netdev(dev);
  2258. }
  2259. static struct platform_driver bcm63xx_enetsw_driver = {
  2260. .probe = bcm_enetsw_probe,
  2261. .remove_new = bcm_enetsw_remove,
  2262. .driver = {
  2263. .name = "bcm63xx_enetsw",
  2264. },
  2265. };
  2266. /* reserve & remap memory space shared between all macs */
  2267. static int bcm_enet_shared_probe(struct platform_device *pdev)
  2268. {
  2269. void __iomem *p[3];
  2270. unsigned int i;
  2271. memset(bcm_enet_shared_base, 0, sizeof(bcm_enet_shared_base));
  2272. for (i = 0; i < 3; i++) {
  2273. p[i] = devm_platform_ioremap_resource(pdev, i);
  2274. if (IS_ERR(p[i]))
  2275. return PTR_ERR(p[i]);
  2276. }
  2277. memcpy(bcm_enet_shared_base, p, sizeof(bcm_enet_shared_base));
  2278. return 0;
  2279. }
  2280. /* this "shared" driver is needed because both macs share a single
  2281. * address space
  2282. */
  2283. struct platform_driver bcm63xx_enet_shared_driver = {
  2284. .probe = bcm_enet_shared_probe,
  2285. .driver = {
  2286. .name = "bcm63xx_enet_shared",
  2287. },
  2288. };
  2289. static struct platform_driver * const drivers[] = {
  2290. &bcm63xx_enet_shared_driver,
  2291. &bcm63xx_enet_driver,
  2292. &bcm63xx_enetsw_driver,
  2293. };
  2294. /* entry point */
  2295. static int __init bcm_enet_init(void)
  2296. {
  2297. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  2298. }
  2299. static void __exit bcm_enet_exit(void)
  2300. {
  2301. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  2302. }
  2303. module_init(bcm_enet_init);
  2304. module_exit(bcm_enet_exit);
  2305. MODULE_DESCRIPTION("BCM63xx internal ethernet mac driver");
  2306. MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
  2307. MODULE_LICENSE("GPL");