bcm63xx_enet.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for BCM963xx builtin Ethernet mac
  4. *
  5. * Copyright (C) 2008 Maxime Bizon <[email protected]>
  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. 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 int 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. return 0;
  1593. }
  1594. static struct platform_driver bcm63xx_enet_driver = {
  1595. .probe = bcm_enet_probe,
  1596. .remove = bcm_enet_remove,
  1597. .driver = {
  1598. .name = "bcm63xx_enet",
  1599. .owner = THIS_MODULE,
  1600. },
  1601. };
  1602. /*
  1603. * switch mii access callbacks
  1604. */
  1605. static int bcmenet_sw_mdio_read(struct bcm_enet_priv *priv,
  1606. int ext, int phy_id, int location)
  1607. {
  1608. u32 reg;
  1609. int ret;
  1610. spin_lock_bh(&priv->enetsw_mdio_lock);
  1611. enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
  1612. reg = ENETSW_MDIOC_RD_MASK |
  1613. (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
  1614. (location << ENETSW_MDIOC_REG_SHIFT);
  1615. if (ext)
  1616. reg |= ENETSW_MDIOC_EXT_MASK;
  1617. enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
  1618. udelay(50);
  1619. ret = enetsw_readw(priv, ENETSW_MDIOD_REG);
  1620. spin_unlock_bh(&priv->enetsw_mdio_lock);
  1621. return ret;
  1622. }
  1623. static void bcmenet_sw_mdio_write(struct bcm_enet_priv *priv,
  1624. int ext, int phy_id, int location,
  1625. uint16_t data)
  1626. {
  1627. u32 reg;
  1628. spin_lock_bh(&priv->enetsw_mdio_lock);
  1629. enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
  1630. reg = ENETSW_MDIOC_WR_MASK |
  1631. (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
  1632. (location << ENETSW_MDIOC_REG_SHIFT);
  1633. if (ext)
  1634. reg |= ENETSW_MDIOC_EXT_MASK;
  1635. reg |= data;
  1636. enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
  1637. udelay(50);
  1638. spin_unlock_bh(&priv->enetsw_mdio_lock);
  1639. }
  1640. static inline int bcm_enet_port_is_rgmii(int portid)
  1641. {
  1642. return portid >= ENETSW_RGMII_PORT0;
  1643. }
  1644. /*
  1645. * enet sw PHY polling
  1646. */
  1647. static void swphy_poll_timer(struct timer_list *t)
  1648. {
  1649. struct bcm_enet_priv *priv = from_timer(priv, t, swphy_poll);
  1650. unsigned int i;
  1651. for (i = 0; i < priv->num_ports; i++) {
  1652. struct bcm63xx_enetsw_port *port;
  1653. int val, j, up, advertise, lpa, speed, duplex, media;
  1654. int external_phy = bcm_enet_port_is_rgmii(i);
  1655. u8 override;
  1656. port = &priv->used_ports[i];
  1657. if (!port->used)
  1658. continue;
  1659. if (port->bypass_link)
  1660. continue;
  1661. /* dummy read to clear */
  1662. for (j = 0; j < 2; j++)
  1663. val = bcmenet_sw_mdio_read(priv, external_phy,
  1664. port->phy_id, MII_BMSR);
  1665. if (val == 0xffff)
  1666. continue;
  1667. up = (val & BMSR_LSTATUS) ? 1 : 0;
  1668. if (!(up ^ priv->sw_port_link[i]))
  1669. continue;
  1670. priv->sw_port_link[i] = up;
  1671. /* link changed */
  1672. if (!up) {
  1673. dev_info(&priv->pdev->dev, "link DOWN on %s\n",
  1674. port->name);
  1675. enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
  1676. ENETSW_PORTOV_REG(i));
  1677. enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
  1678. ENETSW_PTCTRL_TXDIS_MASK,
  1679. ENETSW_PTCTRL_REG(i));
  1680. continue;
  1681. }
  1682. advertise = bcmenet_sw_mdio_read(priv, external_phy,
  1683. port->phy_id, MII_ADVERTISE);
  1684. lpa = bcmenet_sw_mdio_read(priv, external_phy, port->phy_id,
  1685. MII_LPA);
  1686. /* figure out media and duplex from advertise and LPA values */
  1687. media = mii_nway_result(lpa & advertise);
  1688. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  1689. if (media & (ADVERTISE_100FULL | ADVERTISE_100HALF))
  1690. speed = 100;
  1691. else
  1692. speed = 10;
  1693. if (val & BMSR_ESTATEN) {
  1694. advertise = bcmenet_sw_mdio_read(priv, external_phy,
  1695. port->phy_id, MII_CTRL1000);
  1696. lpa = bcmenet_sw_mdio_read(priv, external_phy,
  1697. port->phy_id, MII_STAT1000);
  1698. if (advertise & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)
  1699. && lpa & (LPA_1000FULL | LPA_1000HALF)) {
  1700. speed = 1000;
  1701. duplex = (lpa & LPA_1000FULL);
  1702. }
  1703. }
  1704. dev_info(&priv->pdev->dev,
  1705. "link UP on %s, %dMbps, %s-duplex\n",
  1706. port->name, speed, duplex ? "full" : "half");
  1707. override = ENETSW_PORTOV_ENABLE_MASK |
  1708. ENETSW_PORTOV_LINKUP_MASK;
  1709. if (speed == 1000)
  1710. override |= ENETSW_IMPOV_1000_MASK;
  1711. else if (speed == 100)
  1712. override |= ENETSW_IMPOV_100_MASK;
  1713. if (duplex)
  1714. override |= ENETSW_IMPOV_FDX_MASK;
  1715. enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
  1716. enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
  1717. }
  1718. priv->swphy_poll.expires = jiffies + HZ;
  1719. add_timer(&priv->swphy_poll);
  1720. }
  1721. /*
  1722. * open callback, allocate dma rings & buffers and start rx operation
  1723. */
  1724. static int bcm_enetsw_open(struct net_device *dev)
  1725. {
  1726. struct bcm_enet_priv *priv;
  1727. struct device *kdev;
  1728. int i, ret;
  1729. unsigned int size;
  1730. void *p;
  1731. u32 val;
  1732. priv = netdev_priv(dev);
  1733. kdev = &priv->pdev->dev;
  1734. /* mask all interrupts and request them */
  1735. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  1736. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  1737. ret = request_irq(priv->irq_rx, bcm_enet_isr_dma,
  1738. 0, dev->name, dev);
  1739. if (ret)
  1740. goto out_freeirq;
  1741. if (priv->irq_tx != -1) {
  1742. ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
  1743. 0, dev->name, dev);
  1744. if (ret)
  1745. goto out_freeirq_rx;
  1746. }
  1747. /* allocate rx dma ring */
  1748. size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
  1749. p = dma_alloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
  1750. if (!p) {
  1751. dev_err(kdev, "cannot allocate rx ring %u\n", size);
  1752. ret = -ENOMEM;
  1753. goto out_freeirq_tx;
  1754. }
  1755. priv->rx_desc_alloc_size = size;
  1756. priv->rx_desc_cpu = p;
  1757. /* allocate tx dma ring */
  1758. size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
  1759. p = dma_alloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
  1760. if (!p) {
  1761. dev_err(kdev, "cannot allocate tx ring\n");
  1762. ret = -ENOMEM;
  1763. goto out_free_rx_ring;
  1764. }
  1765. priv->tx_desc_alloc_size = size;
  1766. priv->tx_desc_cpu = p;
  1767. priv->tx_skb = kcalloc(priv->tx_ring_size, sizeof(struct sk_buff *),
  1768. GFP_KERNEL);
  1769. if (!priv->tx_skb) {
  1770. dev_err(kdev, "cannot allocate tx skb queue\n");
  1771. ret = -ENOMEM;
  1772. goto out_free_tx_ring;
  1773. }
  1774. priv->tx_desc_count = priv->tx_ring_size;
  1775. priv->tx_dirty_desc = 0;
  1776. priv->tx_curr_desc = 0;
  1777. spin_lock_init(&priv->tx_lock);
  1778. /* init & fill rx ring with buffers */
  1779. priv->rx_buf = kcalloc(priv->rx_ring_size, sizeof(void *),
  1780. GFP_KERNEL);
  1781. if (!priv->rx_buf) {
  1782. dev_err(kdev, "cannot allocate rx buffer queue\n");
  1783. ret = -ENOMEM;
  1784. goto out_free_tx_skb;
  1785. }
  1786. priv->rx_desc_count = 0;
  1787. priv->rx_dirty_desc = 0;
  1788. priv->rx_curr_desc = 0;
  1789. /* disable all ports */
  1790. for (i = 0; i < priv->num_ports; i++) {
  1791. enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
  1792. ENETSW_PORTOV_REG(i));
  1793. enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
  1794. ENETSW_PTCTRL_TXDIS_MASK,
  1795. ENETSW_PTCTRL_REG(i));
  1796. priv->sw_port_link[i] = 0;
  1797. }
  1798. /* reset mib */
  1799. val = enetsw_readb(priv, ENETSW_GMCR_REG);
  1800. val |= ENETSW_GMCR_RST_MIB_MASK;
  1801. enetsw_writeb(priv, val, ENETSW_GMCR_REG);
  1802. mdelay(1);
  1803. val &= ~ENETSW_GMCR_RST_MIB_MASK;
  1804. enetsw_writeb(priv, val, ENETSW_GMCR_REG);
  1805. mdelay(1);
  1806. /* force CPU port state */
  1807. val = enetsw_readb(priv, ENETSW_IMPOV_REG);
  1808. val |= ENETSW_IMPOV_FORCE_MASK | ENETSW_IMPOV_LINKUP_MASK;
  1809. enetsw_writeb(priv, val, ENETSW_IMPOV_REG);
  1810. /* enable switch forward engine */
  1811. val = enetsw_readb(priv, ENETSW_SWMODE_REG);
  1812. val |= ENETSW_SWMODE_FWD_EN_MASK;
  1813. enetsw_writeb(priv, val, ENETSW_SWMODE_REG);
  1814. /* enable jumbo on all ports */
  1815. enetsw_writel(priv, 0x1ff, ENETSW_JMBCTL_PORT_REG);
  1816. enetsw_writew(priv, 9728, ENETSW_JMBCTL_MAXSIZE_REG);
  1817. /* initialize flow control buffer allocation */
  1818. enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
  1819. ENETDMA_BUFALLOC_REG(priv->rx_chan));
  1820. if (bcm_enet_refill_rx(dev, false)) {
  1821. dev_err(kdev, "cannot allocate rx buffer queue\n");
  1822. ret = -ENOMEM;
  1823. goto out;
  1824. }
  1825. /* write rx & tx ring addresses */
  1826. enet_dmas_writel(priv, priv->rx_desc_dma,
  1827. ENETDMAS_RSTART_REG, priv->rx_chan);
  1828. enet_dmas_writel(priv, priv->tx_desc_dma,
  1829. ENETDMAS_RSTART_REG, priv->tx_chan);
  1830. /* clear remaining state ram for rx & tx channel */
  1831. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
  1832. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
  1833. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
  1834. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
  1835. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
  1836. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
  1837. /* set dma maximum burst len */
  1838. enet_dmac_writel(priv, priv->dma_maxburst,
  1839. ENETDMAC_MAXBURST, priv->rx_chan);
  1840. enet_dmac_writel(priv, priv->dma_maxburst,
  1841. ENETDMAC_MAXBURST, priv->tx_chan);
  1842. /* set flow control low/high threshold to 1/3 / 2/3 */
  1843. val = priv->rx_ring_size / 3;
  1844. enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
  1845. val = (priv->rx_ring_size * 2) / 3;
  1846. enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
  1847. /* all set, enable mac and interrupts, start dma engine and
  1848. * kick rx dma channel
  1849. */
  1850. wmb();
  1851. enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
  1852. enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
  1853. ENETDMAC_CHANCFG, priv->rx_chan);
  1854. /* watch "packet transferred" interrupt in rx and tx */
  1855. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1856. ENETDMAC_IR, priv->rx_chan);
  1857. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1858. ENETDMAC_IR, priv->tx_chan);
  1859. /* make sure we enable napi before rx interrupt */
  1860. napi_enable(&priv->napi);
  1861. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1862. ENETDMAC_IRMASK, priv->rx_chan);
  1863. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1864. ENETDMAC_IRMASK, priv->tx_chan);
  1865. netif_carrier_on(dev);
  1866. netif_start_queue(dev);
  1867. /* apply override config for bypass_link ports here. */
  1868. for (i = 0; i < priv->num_ports; i++) {
  1869. struct bcm63xx_enetsw_port *port;
  1870. u8 override;
  1871. port = &priv->used_ports[i];
  1872. if (!port->used)
  1873. continue;
  1874. if (!port->bypass_link)
  1875. continue;
  1876. override = ENETSW_PORTOV_ENABLE_MASK |
  1877. ENETSW_PORTOV_LINKUP_MASK;
  1878. switch (port->force_speed) {
  1879. case 1000:
  1880. override |= ENETSW_IMPOV_1000_MASK;
  1881. break;
  1882. case 100:
  1883. override |= ENETSW_IMPOV_100_MASK;
  1884. break;
  1885. case 10:
  1886. break;
  1887. default:
  1888. pr_warn("invalid forced speed on port %s: assume 10\n",
  1889. port->name);
  1890. break;
  1891. }
  1892. if (port->force_duplex_full)
  1893. override |= ENETSW_IMPOV_FDX_MASK;
  1894. enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
  1895. enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
  1896. }
  1897. /* start phy polling timer */
  1898. timer_setup(&priv->swphy_poll, swphy_poll_timer, 0);
  1899. mod_timer(&priv->swphy_poll, jiffies);
  1900. return 0;
  1901. out:
  1902. bcm_enet_free_rx_buf_ring(kdev, priv);
  1903. out_free_tx_skb:
  1904. kfree(priv->tx_skb);
  1905. out_free_tx_ring:
  1906. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  1907. priv->tx_desc_cpu, priv->tx_desc_dma);
  1908. out_free_rx_ring:
  1909. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  1910. priv->rx_desc_cpu, priv->rx_desc_dma);
  1911. out_freeirq_tx:
  1912. if (priv->irq_tx != -1)
  1913. free_irq(priv->irq_tx, dev);
  1914. out_freeirq_rx:
  1915. free_irq(priv->irq_rx, dev);
  1916. out_freeirq:
  1917. return ret;
  1918. }
  1919. /* stop callback */
  1920. static int bcm_enetsw_stop(struct net_device *dev)
  1921. {
  1922. struct bcm_enet_priv *priv;
  1923. struct device *kdev;
  1924. priv = netdev_priv(dev);
  1925. kdev = &priv->pdev->dev;
  1926. del_timer_sync(&priv->swphy_poll);
  1927. netif_stop_queue(dev);
  1928. napi_disable(&priv->napi);
  1929. del_timer_sync(&priv->rx_timeout);
  1930. /* mask all interrupts */
  1931. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  1932. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  1933. /* disable dma & mac */
  1934. bcm_enet_disable_dma(priv, priv->tx_chan);
  1935. bcm_enet_disable_dma(priv, priv->rx_chan);
  1936. /* force reclaim of all tx buffers */
  1937. bcm_enet_tx_reclaim(dev, 1, 0);
  1938. /* free the rx buffer ring */
  1939. bcm_enet_free_rx_buf_ring(kdev, priv);
  1940. /* free remaining allocated memory */
  1941. kfree(priv->tx_skb);
  1942. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  1943. priv->rx_desc_cpu, priv->rx_desc_dma);
  1944. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  1945. priv->tx_desc_cpu, priv->tx_desc_dma);
  1946. if (priv->irq_tx != -1)
  1947. free_irq(priv->irq_tx, dev);
  1948. free_irq(priv->irq_rx, dev);
  1949. /* reset BQL after forced tx reclaim to prevent kernel panic */
  1950. netdev_reset_queue(dev);
  1951. return 0;
  1952. }
  1953. /* try to sort out phy external status by walking the used_port field
  1954. * in the bcm_enet_priv structure. in case the phy address is not
  1955. * assigned to any physical port on the switch, assume it is external
  1956. * (and yell at the user).
  1957. */
  1958. static int bcm_enetsw_phy_is_external(struct bcm_enet_priv *priv, int phy_id)
  1959. {
  1960. int i;
  1961. for (i = 0; i < priv->num_ports; ++i) {
  1962. if (!priv->used_ports[i].used)
  1963. continue;
  1964. if (priv->used_ports[i].phy_id == phy_id)
  1965. return bcm_enet_port_is_rgmii(i);
  1966. }
  1967. printk_once(KERN_WARNING "bcm63xx_enet: could not find a used port with phy_id %i, assuming phy is external\n",
  1968. phy_id);
  1969. return 1;
  1970. }
  1971. /* can't use bcmenet_sw_mdio_read directly as we need to sort out
  1972. * external/internal status of the given phy_id first.
  1973. */
  1974. static int bcm_enetsw_mii_mdio_read(struct net_device *dev, int phy_id,
  1975. int location)
  1976. {
  1977. struct bcm_enet_priv *priv;
  1978. priv = netdev_priv(dev);
  1979. return bcmenet_sw_mdio_read(priv,
  1980. bcm_enetsw_phy_is_external(priv, phy_id),
  1981. phy_id, location);
  1982. }
  1983. /* can't use bcmenet_sw_mdio_write directly as we need to sort out
  1984. * external/internal status of the given phy_id first.
  1985. */
  1986. static void bcm_enetsw_mii_mdio_write(struct net_device *dev, int phy_id,
  1987. int location,
  1988. int val)
  1989. {
  1990. struct bcm_enet_priv *priv;
  1991. priv = netdev_priv(dev);
  1992. bcmenet_sw_mdio_write(priv, bcm_enetsw_phy_is_external(priv, phy_id),
  1993. phy_id, location, val);
  1994. }
  1995. static int bcm_enetsw_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1996. {
  1997. struct mii_if_info mii;
  1998. mii.dev = dev;
  1999. mii.mdio_read = bcm_enetsw_mii_mdio_read;
  2000. mii.mdio_write = bcm_enetsw_mii_mdio_write;
  2001. mii.phy_id = 0;
  2002. mii.phy_id_mask = 0x3f;
  2003. mii.reg_num_mask = 0x1f;
  2004. return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
  2005. }
  2006. static const struct net_device_ops bcm_enetsw_ops = {
  2007. .ndo_open = bcm_enetsw_open,
  2008. .ndo_stop = bcm_enetsw_stop,
  2009. .ndo_start_xmit = bcm_enet_start_xmit,
  2010. .ndo_change_mtu = bcm_enet_change_mtu,
  2011. .ndo_eth_ioctl = bcm_enetsw_ioctl,
  2012. };
  2013. static const struct bcm_enet_stats bcm_enetsw_gstrings_stats[] = {
  2014. { "rx_packets", DEV_STAT(rx_packets), -1 },
  2015. { "tx_packets", DEV_STAT(tx_packets), -1 },
  2016. { "rx_bytes", DEV_STAT(rx_bytes), -1 },
  2017. { "tx_bytes", DEV_STAT(tx_bytes), -1 },
  2018. { "rx_errors", DEV_STAT(rx_errors), -1 },
  2019. { "tx_errors", DEV_STAT(tx_errors), -1 },
  2020. { "rx_dropped", DEV_STAT(rx_dropped), -1 },
  2021. { "tx_dropped", DEV_STAT(tx_dropped), -1 },
  2022. { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETHSW_MIB_RX_GD_OCT },
  2023. { "tx_unicast", GEN_STAT(mib.tx_unicast), ETHSW_MIB_RX_BRDCAST },
  2024. { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETHSW_MIB_RX_BRDCAST },
  2025. { "tx_multicast", GEN_STAT(mib.tx_mult), ETHSW_MIB_RX_MULT },
  2026. { "tx_64_octets", GEN_STAT(mib.tx_64), ETHSW_MIB_RX_64 },
  2027. { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETHSW_MIB_RX_65_127 },
  2028. { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETHSW_MIB_RX_128_255 },
  2029. { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETHSW_MIB_RX_256_511 },
  2030. { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETHSW_MIB_RX_512_1023},
  2031. { "tx_1024_1522_oct", GEN_STAT(mib.tx_1024_max),
  2032. ETHSW_MIB_RX_1024_1522 },
  2033. { "tx_1523_2047_oct", GEN_STAT(mib.tx_1523_2047),
  2034. ETHSW_MIB_RX_1523_2047 },
  2035. { "tx_2048_4095_oct", GEN_STAT(mib.tx_2048_4095),
  2036. ETHSW_MIB_RX_2048_4095 },
  2037. { "tx_4096_8191_oct", GEN_STAT(mib.tx_4096_8191),
  2038. ETHSW_MIB_RX_4096_8191 },
  2039. { "tx_8192_9728_oct", GEN_STAT(mib.tx_8192_9728),
  2040. ETHSW_MIB_RX_8192_9728 },
  2041. { "tx_oversize", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR },
  2042. { "tx_oversize_drop", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR_DISC },
  2043. { "tx_dropped", GEN_STAT(mib.tx_drop), ETHSW_MIB_RX_DROP },
  2044. { "tx_undersize", GEN_STAT(mib.tx_underrun), ETHSW_MIB_RX_UND },
  2045. { "tx_pause", GEN_STAT(mib.tx_pause), ETHSW_MIB_RX_PAUSE },
  2046. { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETHSW_MIB_TX_ALL_OCT },
  2047. { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETHSW_MIB_TX_BRDCAST },
  2048. { "rx_multicast", GEN_STAT(mib.rx_mult), ETHSW_MIB_TX_MULT },
  2049. { "rx_unicast", GEN_STAT(mib.rx_unicast), ETHSW_MIB_TX_MULT },
  2050. { "rx_pause", GEN_STAT(mib.rx_pause), ETHSW_MIB_TX_PAUSE },
  2051. { "rx_dropped", GEN_STAT(mib.rx_drop), ETHSW_MIB_TX_DROP_PKTS },
  2052. };
  2053. #define BCM_ENETSW_STATS_LEN \
  2054. (sizeof(bcm_enetsw_gstrings_stats) / sizeof(struct bcm_enet_stats))
  2055. static void bcm_enetsw_get_strings(struct net_device *netdev,
  2056. u32 stringset, u8 *data)
  2057. {
  2058. int i;
  2059. switch (stringset) {
  2060. case ETH_SS_STATS:
  2061. for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
  2062. memcpy(data + i * ETH_GSTRING_LEN,
  2063. bcm_enetsw_gstrings_stats[i].stat_string,
  2064. ETH_GSTRING_LEN);
  2065. }
  2066. break;
  2067. }
  2068. }
  2069. static int bcm_enetsw_get_sset_count(struct net_device *netdev,
  2070. int string_set)
  2071. {
  2072. switch (string_set) {
  2073. case ETH_SS_STATS:
  2074. return BCM_ENETSW_STATS_LEN;
  2075. default:
  2076. return -EINVAL;
  2077. }
  2078. }
  2079. static void bcm_enetsw_get_drvinfo(struct net_device *netdev,
  2080. struct ethtool_drvinfo *drvinfo)
  2081. {
  2082. strncpy(drvinfo->driver, bcm_enet_driver_name, sizeof(drvinfo->driver));
  2083. strncpy(drvinfo->bus_info, "bcm63xx", sizeof(drvinfo->bus_info));
  2084. }
  2085. static void bcm_enetsw_get_ethtool_stats(struct net_device *netdev,
  2086. struct ethtool_stats *stats,
  2087. u64 *data)
  2088. {
  2089. struct bcm_enet_priv *priv;
  2090. int i;
  2091. priv = netdev_priv(netdev);
  2092. for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
  2093. const struct bcm_enet_stats *s;
  2094. u32 lo, hi;
  2095. char *p;
  2096. int reg;
  2097. s = &bcm_enetsw_gstrings_stats[i];
  2098. reg = s->mib_reg;
  2099. if (reg == -1)
  2100. continue;
  2101. lo = enetsw_readl(priv, ENETSW_MIB_REG(reg));
  2102. p = (char *)priv + s->stat_offset;
  2103. if (s->sizeof_stat == sizeof(u64)) {
  2104. hi = enetsw_readl(priv, ENETSW_MIB_REG(reg + 1));
  2105. *(u64 *)p = ((u64)hi << 32 | lo);
  2106. } else {
  2107. *(u32 *)p = lo;
  2108. }
  2109. }
  2110. for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
  2111. const struct bcm_enet_stats *s;
  2112. char *p;
  2113. s = &bcm_enetsw_gstrings_stats[i];
  2114. if (s->mib_reg == -1)
  2115. p = (char *)&netdev->stats + s->stat_offset;
  2116. else
  2117. p = (char *)priv + s->stat_offset;
  2118. data[i] = (s->sizeof_stat == sizeof(u64)) ?
  2119. *(u64 *)p : *(u32 *)p;
  2120. }
  2121. }
  2122. static void
  2123. bcm_enetsw_get_ringparam(struct net_device *dev,
  2124. struct ethtool_ringparam *ering,
  2125. struct kernel_ethtool_ringparam *kernel_ering,
  2126. struct netlink_ext_ack *extack)
  2127. {
  2128. struct bcm_enet_priv *priv;
  2129. priv = netdev_priv(dev);
  2130. /* rx/tx ring is actually only limited by memory */
  2131. ering->rx_max_pending = 8192;
  2132. ering->tx_max_pending = 8192;
  2133. ering->rx_mini_max_pending = 0;
  2134. ering->rx_jumbo_max_pending = 0;
  2135. ering->rx_pending = priv->rx_ring_size;
  2136. ering->tx_pending = priv->tx_ring_size;
  2137. }
  2138. static int
  2139. bcm_enetsw_set_ringparam(struct net_device *dev,
  2140. struct ethtool_ringparam *ering,
  2141. struct kernel_ethtool_ringparam *kernel_ering,
  2142. struct netlink_ext_ack *extack)
  2143. {
  2144. struct bcm_enet_priv *priv;
  2145. int was_running;
  2146. priv = netdev_priv(dev);
  2147. was_running = 0;
  2148. if (netif_running(dev)) {
  2149. bcm_enetsw_stop(dev);
  2150. was_running = 1;
  2151. }
  2152. priv->rx_ring_size = ering->rx_pending;
  2153. priv->tx_ring_size = ering->tx_pending;
  2154. if (was_running) {
  2155. int err;
  2156. err = bcm_enetsw_open(dev);
  2157. if (err)
  2158. dev_close(dev);
  2159. }
  2160. return 0;
  2161. }
  2162. static const struct ethtool_ops bcm_enetsw_ethtool_ops = {
  2163. .get_strings = bcm_enetsw_get_strings,
  2164. .get_sset_count = bcm_enetsw_get_sset_count,
  2165. .get_ethtool_stats = bcm_enetsw_get_ethtool_stats,
  2166. .get_drvinfo = bcm_enetsw_get_drvinfo,
  2167. .get_ringparam = bcm_enetsw_get_ringparam,
  2168. .set_ringparam = bcm_enetsw_set_ringparam,
  2169. };
  2170. /* allocate netdevice, request register memory and register device. */
  2171. static int bcm_enetsw_probe(struct platform_device *pdev)
  2172. {
  2173. struct bcm_enet_priv *priv;
  2174. struct net_device *dev;
  2175. struct bcm63xx_enetsw_platform_data *pd;
  2176. struct resource *res_mem;
  2177. int ret, irq_rx, irq_tx;
  2178. if (!bcm_enet_shared_base[0])
  2179. return -EPROBE_DEFER;
  2180. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2181. irq_rx = platform_get_irq(pdev, 0);
  2182. irq_tx = platform_get_irq(pdev, 1);
  2183. if (!res_mem || irq_rx < 0)
  2184. return -ENODEV;
  2185. dev = alloc_etherdev(sizeof(*priv));
  2186. if (!dev)
  2187. return -ENOMEM;
  2188. priv = netdev_priv(dev);
  2189. /* initialize default and fetch platform data */
  2190. priv->enet_is_sw = true;
  2191. priv->irq_rx = irq_rx;
  2192. priv->irq_tx = irq_tx;
  2193. priv->rx_ring_size = BCMENET_DEF_RX_DESC;
  2194. priv->tx_ring_size = BCMENET_DEF_TX_DESC;
  2195. priv->dma_maxburst = BCMENETSW_DMA_MAXBURST;
  2196. priv->rx_buf_offset = NET_SKB_PAD + NET_IP_ALIGN;
  2197. pd = dev_get_platdata(&pdev->dev);
  2198. if (pd) {
  2199. eth_hw_addr_set(dev, pd->mac_addr);
  2200. memcpy(priv->used_ports, pd->used_ports,
  2201. sizeof(pd->used_ports));
  2202. priv->num_ports = pd->num_ports;
  2203. priv->dma_has_sram = pd->dma_has_sram;
  2204. priv->dma_chan_en_mask = pd->dma_chan_en_mask;
  2205. priv->dma_chan_int_mask = pd->dma_chan_int_mask;
  2206. priv->dma_chan_width = pd->dma_chan_width;
  2207. }
  2208. ret = bcm_enet_change_mtu(dev, dev->mtu);
  2209. if (ret)
  2210. goto out;
  2211. priv->base = devm_ioremap_resource(&pdev->dev, res_mem);
  2212. if (IS_ERR(priv->base)) {
  2213. ret = PTR_ERR(priv->base);
  2214. goto out;
  2215. }
  2216. priv->mac_clk = devm_clk_get(&pdev->dev, "enetsw");
  2217. if (IS_ERR(priv->mac_clk)) {
  2218. ret = PTR_ERR(priv->mac_clk);
  2219. goto out;
  2220. }
  2221. ret = clk_prepare_enable(priv->mac_clk);
  2222. if (ret)
  2223. goto out;
  2224. priv->rx_chan = 0;
  2225. priv->tx_chan = 1;
  2226. spin_lock_init(&priv->rx_lock);
  2227. /* init rx timeout (used for oom) */
  2228. timer_setup(&priv->rx_timeout, bcm_enet_refill_rx_timer, 0);
  2229. /* register netdevice */
  2230. dev->netdev_ops = &bcm_enetsw_ops;
  2231. netif_napi_add_weight(dev, &priv->napi, bcm_enet_poll, 16);
  2232. dev->ethtool_ops = &bcm_enetsw_ethtool_ops;
  2233. SET_NETDEV_DEV(dev, &pdev->dev);
  2234. spin_lock_init(&priv->enetsw_mdio_lock);
  2235. ret = register_netdev(dev);
  2236. if (ret)
  2237. goto out_disable_clk;
  2238. netif_carrier_off(dev);
  2239. platform_set_drvdata(pdev, dev);
  2240. priv->pdev = pdev;
  2241. priv->net_dev = dev;
  2242. return 0;
  2243. out_disable_clk:
  2244. clk_disable_unprepare(priv->mac_clk);
  2245. out:
  2246. free_netdev(dev);
  2247. return ret;
  2248. }
  2249. /* exit func, stops hardware and unregisters netdevice */
  2250. static int bcm_enetsw_remove(struct platform_device *pdev)
  2251. {
  2252. struct bcm_enet_priv *priv;
  2253. struct net_device *dev;
  2254. /* stop netdevice */
  2255. dev = platform_get_drvdata(pdev);
  2256. priv = netdev_priv(dev);
  2257. unregister_netdev(dev);
  2258. clk_disable_unprepare(priv->mac_clk);
  2259. free_netdev(dev);
  2260. return 0;
  2261. }
  2262. static struct platform_driver bcm63xx_enetsw_driver = {
  2263. .probe = bcm_enetsw_probe,
  2264. .remove = bcm_enetsw_remove,
  2265. .driver = {
  2266. .name = "bcm63xx_enetsw",
  2267. .owner = THIS_MODULE,
  2268. },
  2269. };
  2270. /* reserve & remap memory space shared between all macs */
  2271. static int bcm_enet_shared_probe(struct platform_device *pdev)
  2272. {
  2273. void __iomem *p[3];
  2274. unsigned int i;
  2275. memset(bcm_enet_shared_base, 0, sizeof(bcm_enet_shared_base));
  2276. for (i = 0; i < 3; i++) {
  2277. p[i] = devm_platform_ioremap_resource(pdev, i);
  2278. if (IS_ERR(p[i]))
  2279. return PTR_ERR(p[i]);
  2280. }
  2281. memcpy(bcm_enet_shared_base, p, sizeof(bcm_enet_shared_base));
  2282. return 0;
  2283. }
  2284. static int bcm_enet_shared_remove(struct platform_device *pdev)
  2285. {
  2286. return 0;
  2287. }
  2288. /* this "shared" driver is needed because both macs share a single
  2289. * address space
  2290. */
  2291. struct platform_driver bcm63xx_enet_shared_driver = {
  2292. .probe = bcm_enet_shared_probe,
  2293. .remove = bcm_enet_shared_remove,
  2294. .driver = {
  2295. .name = "bcm63xx_enet_shared",
  2296. .owner = THIS_MODULE,
  2297. },
  2298. };
  2299. static struct platform_driver * const drivers[] = {
  2300. &bcm63xx_enet_shared_driver,
  2301. &bcm63xx_enet_driver,
  2302. &bcm63xx_enetsw_driver,
  2303. };
  2304. /* entry point */
  2305. static int __init bcm_enet_init(void)
  2306. {
  2307. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  2308. }
  2309. static void __exit bcm_enet_exit(void)
  2310. {
  2311. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  2312. }
  2313. module_init(bcm_enet_init);
  2314. module_exit(bcm_enet_exit);
  2315. MODULE_DESCRIPTION("BCM63xx internal ethernet mac driver");
  2316. MODULE_AUTHOR("Maxime Bizon <[email protected]>");
  2317. MODULE_LICENSE("GPL");