xilinx_axienet_main.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Xilinx Axi Ethernet device driver
  4. *
  5. * Copyright (c) 2008 Nissin Systems Co., Ltd., Yoshio Kashiwagi
  6. * Copyright (c) 2005-2008 DLA Systems, David H. Lynch Jr. <dhlii@dlasys.net>
  7. * Copyright (c) 2008-2009 Secret Lab Technologies Ltd.
  8. * Copyright (c) 2010 - 2011 Michal Simek <monstr@monstr.eu>
  9. * Copyright (c) 2010 - 2011 PetaLogix
  10. * Copyright (c) 2019 - 2022 Calian Advanced Technologies
  11. * Copyright (c) 2010 - 2012 Xilinx, Inc. All rights reserved.
  12. *
  13. * This is a driver for the Xilinx Axi Ethernet which is used in the Virtex6
  14. * and Spartan6.
  15. *
  16. * TODO:
  17. * - Add Axi Fifo support.
  18. * - Factor out Axi DMA code into separate driver.
  19. * - Test and fix basic multicast filtering.
  20. * - Add support for extended multicast filtering.
  21. * - Test basic VLAN support.
  22. * - Add support for extended VLAN support.
  23. */
  24. #include <linux/clk.h>
  25. #include <linux/delay.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/module.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/of.h>
  30. #include <linux/of_mdio.h>
  31. #include <linux/of_net.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/of_address.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/math64.h>
  37. #include <linux/phy.h>
  38. #include <linux/mii.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/dmaengine.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/dma/xilinx_dma.h>
  43. #include <linux/circ_buf.h>
  44. #include <net/netdev_queues.h>
  45. #include "xilinx_axienet.h"
  46. /* Descriptors defines for Tx and Rx DMA */
  47. #define TX_BD_NUM_DEFAULT 128
  48. #define RX_BD_NUM_DEFAULT 1024
  49. #define TX_BD_NUM_MIN (MAX_SKB_FRAGS + 1)
  50. #define TX_BD_NUM_MAX 4096
  51. #define RX_BD_NUM_MAX 4096
  52. #define DMA_NUM_APP_WORDS 5
  53. #define LEN_APP 4
  54. #define RX_BUF_NUM_DEFAULT 128
  55. /* Must be shorter than length of ethtool_drvinfo.driver field to fit */
  56. #define DRIVER_NAME "xaxienet"
  57. #define DRIVER_DESCRIPTION "Xilinx Axi Ethernet driver"
  58. #define DRIVER_VERSION "1.00a"
  59. #define AXIENET_REGS_N 40
  60. static void axienet_rx_submit_desc(struct net_device *ndev);
  61. /* Match table for of_platform binding */
  62. static const struct of_device_id axienet_of_match[] = {
  63. { .compatible = "xlnx,axi-ethernet-1.00.a", },
  64. { .compatible = "xlnx,axi-ethernet-1.01.a", },
  65. { .compatible = "xlnx,axi-ethernet-2.01.a", },
  66. {},
  67. };
  68. MODULE_DEVICE_TABLE(of, axienet_of_match);
  69. /* Option table for setting up Axi Ethernet hardware options */
  70. static struct axienet_option axienet_options[] = {
  71. /* Turn on jumbo packet support for both Rx and Tx */
  72. {
  73. .opt = XAE_OPTION_JUMBO,
  74. .reg = XAE_TC_OFFSET,
  75. .m_or = XAE_TC_JUM_MASK,
  76. }, {
  77. .opt = XAE_OPTION_JUMBO,
  78. .reg = XAE_RCW1_OFFSET,
  79. .m_or = XAE_RCW1_JUM_MASK,
  80. }, { /* Turn on VLAN packet support for both Rx and Tx */
  81. .opt = XAE_OPTION_VLAN,
  82. .reg = XAE_TC_OFFSET,
  83. .m_or = XAE_TC_VLAN_MASK,
  84. }, {
  85. .opt = XAE_OPTION_VLAN,
  86. .reg = XAE_RCW1_OFFSET,
  87. .m_or = XAE_RCW1_VLAN_MASK,
  88. }, { /* Turn on FCS stripping on receive packets */
  89. .opt = XAE_OPTION_FCS_STRIP,
  90. .reg = XAE_RCW1_OFFSET,
  91. .m_or = XAE_RCW1_FCS_MASK,
  92. }, { /* Turn on FCS insertion on transmit packets */
  93. .opt = XAE_OPTION_FCS_INSERT,
  94. .reg = XAE_TC_OFFSET,
  95. .m_or = XAE_TC_FCS_MASK,
  96. }, { /* Turn off length/type field checking on receive packets */
  97. .opt = XAE_OPTION_LENTYPE_ERR,
  98. .reg = XAE_RCW1_OFFSET,
  99. .m_or = XAE_RCW1_LT_DIS_MASK,
  100. }, { /* Turn on Rx flow control */
  101. .opt = XAE_OPTION_FLOW_CONTROL,
  102. .reg = XAE_FCC_OFFSET,
  103. .m_or = XAE_FCC_FCRX_MASK,
  104. }, { /* Turn on Tx flow control */
  105. .opt = XAE_OPTION_FLOW_CONTROL,
  106. .reg = XAE_FCC_OFFSET,
  107. .m_or = XAE_FCC_FCTX_MASK,
  108. }, { /* Turn on promiscuous frame filtering */
  109. .opt = XAE_OPTION_PROMISC,
  110. .reg = XAE_FMI_OFFSET,
  111. .m_or = XAE_FMI_PM_MASK,
  112. }, { /* Enable transmitter */
  113. .opt = XAE_OPTION_TXEN,
  114. .reg = XAE_TC_OFFSET,
  115. .m_or = XAE_TC_TX_MASK,
  116. }, { /* Enable receiver */
  117. .opt = XAE_OPTION_RXEN,
  118. .reg = XAE_RCW1_OFFSET,
  119. .m_or = XAE_RCW1_RX_MASK,
  120. },
  121. {}
  122. };
  123. static struct skbuf_dma_descriptor *axienet_get_rx_desc(struct axienet_local *lp, int i)
  124. {
  125. return lp->rx_skb_ring[i & (RX_BUF_NUM_DEFAULT - 1)];
  126. }
  127. static struct skbuf_dma_descriptor *axienet_get_tx_desc(struct axienet_local *lp, int i)
  128. {
  129. return lp->tx_skb_ring[i & (TX_BD_NUM_MAX - 1)];
  130. }
  131. /**
  132. * axienet_dma_in32 - Memory mapped Axi DMA register read
  133. * @lp: Pointer to axienet local structure
  134. * @reg: Address offset from the base address of the Axi DMA core
  135. *
  136. * Return: The contents of the Axi DMA register
  137. *
  138. * This function returns the contents of the corresponding Axi DMA register.
  139. */
  140. static inline u32 axienet_dma_in32(struct axienet_local *lp, off_t reg)
  141. {
  142. return ioread32(lp->dma_regs + reg);
  143. }
  144. static void desc_set_phys_addr(struct axienet_local *lp, dma_addr_t addr,
  145. struct axidma_bd *desc)
  146. {
  147. desc->phys = lower_32_bits(addr);
  148. if (lp->features & XAE_FEATURE_DMA_64BIT)
  149. desc->phys_msb = upper_32_bits(addr);
  150. }
  151. static dma_addr_t desc_get_phys_addr(struct axienet_local *lp,
  152. struct axidma_bd *desc)
  153. {
  154. dma_addr_t ret = desc->phys;
  155. if (lp->features & XAE_FEATURE_DMA_64BIT)
  156. ret |= ((dma_addr_t)desc->phys_msb << 16) << 16;
  157. return ret;
  158. }
  159. /**
  160. * axienet_dma_bd_release - Release buffer descriptor rings
  161. * @ndev: Pointer to the net_device structure
  162. *
  163. * This function is used to release the descriptors allocated in
  164. * axienet_dma_bd_init. axienet_dma_bd_release is called when Axi Ethernet
  165. * driver stop api is called.
  166. */
  167. static void axienet_dma_bd_release(struct net_device *ndev)
  168. {
  169. int i;
  170. struct axienet_local *lp = netdev_priv(ndev);
  171. /* If we end up here, tx_bd_v must have been DMA allocated. */
  172. dma_free_coherent(lp->dev,
  173. sizeof(*lp->tx_bd_v) * lp->tx_bd_num,
  174. lp->tx_bd_v,
  175. lp->tx_bd_p);
  176. if (!lp->rx_bd_v)
  177. return;
  178. for (i = 0; i < lp->rx_bd_num; i++) {
  179. dma_addr_t phys;
  180. /* A NULL skb means this descriptor has not been initialised
  181. * at all.
  182. */
  183. if (!lp->rx_bd_v[i].skb)
  184. break;
  185. dev_kfree_skb(lp->rx_bd_v[i].skb);
  186. /* For each descriptor, we programmed cntrl with the (non-zero)
  187. * descriptor size, after it had been successfully allocated.
  188. * So a non-zero value in there means we need to unmap it.
  189. */
  190. if (lp->rx_bd_v[i].cntrl) {
  191. phys = desc_get_phys_addr(lp, &lp->rx_bd_v[i]);
  192. dma_unmap_single(lp->dev, phys,
  193. lp->max_frm_size, DMA_FROM_DEVICE);
  194. }
  195. }
  196. dma_free_coherent(lp->dev,
  197. sizeof(*lp->rx_bd_v) * lp->rx_bd_num,
  198. lp->rx_bd_v,
  199. lp->rx_bd_p);
  200. }
  201. /**
  202. * axienet_usec_to_timer - Calculate IRQ delay timer value
  203. * @lp: Pointer to the axienet_local structure
  204. * @coalesce_usec: Microseconds to convert into timer value
  205. */
  206. static u32 axienet_usec_to_timer(struct axienet_local *lp, u32 coalesce_usec)
  207. {
  208. u32 result;
  209. u64 clk_rate = 125000000; /* arbitrary guess if no clock rate set */
  210. if (lp->axi_clk)
  211. clk_rate = clk_get_rate(lp->axi_clk);
  212. /* 1 Timeout Interval = 125 * (clock period of SG clock) */
  213. result = DIV64_U64_ROUND_CLOSEST((u64)coalesce_usec * clk_rate,
  214. (u64)125000000);
  215. if (result > 255)
  216. result = 255;
  217. return result;
  218. }
  219. /**
  220. * axienet_dma_start - Set up DMA registers and start DMA operation
  221. * @lp: Pointer to the axienet_local structure
  222. */
  223. static void axienet_dma_start(struct axienet_local *lp)
  224. {
  225. /* Start updating the Rx channel control register */
  226. lp->rx_dma_cr = (lp->coalesce_count_rx << XAXIDMA_COALESCE_SHIFT) |
  227. XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_ERROR_MASK;
  228. /* Only set interrupt delay timer if not generating an interrupt on
  229. * the first RX packet. Otherwise leave at 0 to disable delay interrupt.
  230. */
  231. if (lp->coalesce_count_rx > 1)
  232. lp->rx_dma_cr |= (axienet_usec_to_timer(lp, lp->coalesce_usec_rx)
  233. << XAXIDMA_DELAY_SHIFT) |
  234. XAXIDMA_IRQ_DELAY_MASK;
  235. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, lp->rx_dma_cr);
  236. /* Start updating the Tx channel control register */
  237. lp->tx_dma_cr = (lp->coalesce_count_tx << XAXIDMA_COALESCE_SHIFT) |
  238. XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_ERROR_MASK;
  239. /* Only set interrupt delay timer if not generating an interrupt on
  240. * the first TX packet. Otherwise leave at 0 to disable delay interrupt.
  241. */
  242. if (lp->coalesce_count_tx > 1)
  243. lp->tx_dma_cr |= (axienet_usec_to_timer(lp, lp->coalesce_usec_tx)
  244. << XAXIDMA_DELAY_SHIFT) |
  245. XAXIDMA_IRQ_DELAY_MASK;
  246. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, lp->tx_dma_cr);
  247. /* Populate the tail pointer and bring the Rx Axi DMA engine out of
  248. * halted state. This will make the Rx side ready for reception.
  249. */
  250. axienet_dma_out_addr(lp, XAXIDMA_RX_CDESC_OFFSET, lp->rx_bd_p);
  251. lp->rx_dma_cr |= XAXIDMA_CR_RUNSTOP_MASK;
  252. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, lp->rx_dma_cr);
  253. axienet_dma_out_addr(lp, XAXIDMA_RX_TDESC_OFFSET, lp->rx_bd_p +
  254. (sizeof(*lp->rx_bd_v) * (lp->rx_bd_num - 1)));
  255. /* Write to the RS (Run-stop) bit in the Tx channel control register.
  256. * Tx channel is now ready to run. But only after we write to the
  257. * tail pointer register that the Tx channel will start transmitting.
  258. */
  259. axienet_dma_out_addr(lp, XAXIDMA_TX_CDESC_OFFSET, lp->tx_bd_p);
  260. lp->tx_dma_cr |= XAXIDMA_CR_RUNSTOP_MASK;
  261. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, lp->tx_dma_cr);
  262. }
  263. /**
  264. * axienet_dma_bd_init - Setup buffer descriptor rings for Axi DMA
  265. * @ndev: Pointer to the net_device structure
  266. *
  267. * Return: 0, on success -ENOMEM, on failure
  268. *
  269. * This function is called to initialize the Rx and Tx DMA descriptor
  270. * rings. This initializes the descriptors with required default values
  271. * and is called when Axi Ethernet driver reset is called.
  272. */
  273. static int axienet_dma_bd_init(struct net_device *ndev)
  274. {
  275. int i;
  276. struct sk_buff *skb;
  277. struct axienet_local *lp = netdev_priv(ndev);
  278. /* Reset the indexes which are used for accessing the BDs */
  279. lp->tx_bd_ci = 0;
  280. lp->tx_bd_tail = 0;
  281. lp->rx_bd_ci = 0;
  282. /* Allocate the Tx and Rx buffer descriptors. */
  283. lp->tx_bd_v = dma_alloc_coherent(lp->dev,
  284. sizeof(*lp->tx_bd_v) * lp->tx_bd_num,
  285. &lp->tx_bd_p, GFP_KERNEL);
  286. if (!lp->tx_bd_v)
  287. return -ENOMEM;
  288. lp->rx_bd_v = dma_alloc_coherent(lp->dev,
  289. sizeof(*lp->rx_bd_v) * lp->rx_bd_num,
  290. &lp->rx_bd_p, GFP_KERNEL);
  291. if (!lp->rx_bd_v)
  292. goto out;
  293. for (i = 0; i < lp->tx_bd_num; i++) {
  294. dma_addr_t addr = lp->tx_bd_p +
  295. sizeof(*lp->tx_bd_v) *
  296. ((i + 1) % lp->tx_bd_num);
  297. lp->tx_bd_v[i].next = lower_32_bits(addr);
  298. if (lp->features & XAE_FEATURE_DMA_64BIT)
  299. lp->tx_bd_v[i].next_msb = upper_32_bits(addr);
  300. }
  301. for (i = 0; i < lp->rx_bd_num; i++) {
  302. dma_addr_t addr;
  303. addr = lp->rx_bd_p + sizeof(*lp->rx_bd_v) *
  304. ((i + 1) % lp->rx_bd_num);
  305. lp->rx_bd_v[i].next = lower_32_bits(addr);
  306. if (lp->features & XAE_FEATURE_DMA_64BIT)
  307. lp->rx_bd_v[i].next_msb = upper_32_bits(addr);
  308. skb = netdev_alloc_skb_ip_align(ndev, lp->max_frm_size);
  309. if (!skb)
  310. goto out;
  311. lp->rx_bd_v[i].skb = skb;
  312. addr = dma_map_single(lp->dev, skb->data,
  313. lp->max_frm_size, DMA_FROM_DEVICE);
  314. if (dma_mapping_error(lp->dev, addr)) {
  315. netdev_err(ndev, "DMA mapping error\n");
  316. goto out;
  317. }
  318. desc_set_phys_addr(lp, addr, &lp->rx_bd_v[i]);
  319. lp->rx_bd_v[i].cntrl = lp->max_frm_size;
  320. }
  321. axienet_dma_start(lp);
  322. return 0;
  323. out:
  324. axienet_dma_bd_release(ndev);
  325. return -ENOMEM;
  326. }
  327. /**
  328. * axienet_set_mac_address - Write the MAC address
  329. * @ndev: Pointer to the net_device structure
  330. * @address: 6 byte Address to be written as MAC address
  331. *
  332. * This function is called to initialize the MAC address of the Axi Ethernet
  333. * core. It writes to the UAW0 and UAW1 registers of the core.
  334. */
  335. static void axienet_set_mac_address(struct net_device *ndev,
  336. const void *address)
  337. {
  338. struct axienet_local *lp = netdev_priv(ndev);
  339. if (address)
  340. eth_hw_addr_set(ndev, address);
  341. if (!is_valid_ether_addr(ndev->dev_addr))
  342. eth_hw_addr_random(ndev);
  343. /* Set up unicast MAC address filter set its mac address */
  344. axienet_iow(lp, XAE_UAW0_OFFSET,
  345. (ndev->dev_addr[0]) |
  346. (ndev->dev_addr[1] << 8) |
  347. (ndev->dev_addr[2] << 16) |
  348. (ndev->dev_addr[3] << 24));
  349. axienet_iow(lp, XAE_UAW1_OFFSET,
  350. (((axienet_ior(lp, XAE_UAW1_OFFSET)) &
  351. ~XAE_UAW1_UNICASTADDR_MASK) |
  352. (ndev->dev_addr[4] |
  353. (ndev->dev_addr[5] << 8))));
  354. }
  355. /**
  356. * netdev_set_mac_address - Write the MAC address (from outside the driver)
  357. * @ndev: Pointer to the net_device structure
  358. * @p: 6 byte Address to be written as MAC address
  359. *
  360. * Return: 0 for all conditions. Presently, there is no failure case.
  361. *
  362. * This function is called to initialize the MAC address of the Axi Ethernet
  363. * core. It calls the core specific axienet_set_mac_address. This is the
  364. * function that goes into net_device_ops structure entry ndo_set_mac_address.
  365. */
  366. static int netdev_set_mac_address(struct net_device *ndev, void *p)
  367. {
  368. struct sockaddr *addr = p;
  369. axienet_set_mac_address(ndev, addr->sa_data);
  370. return 0;
  371. }
  372. /**
  373. * axienet_set_multicast_list - Prepare the multicast table
  374. * @ndev: Pointer to the net_device structure
  375. *
  376. * This function is called to initialize the multicast table during
  377. * initialization. The Axi Ethernet basic multicast support has a four-entry
  378. * multicast table which is initialized here. Additionally this function
  379. * goes into the net_device_ops structure entry ndo_set_multicast_list. This
  380. * means whenever the multicast table entries need to be updated this
  381. * function gets called.
  382. */
  383. static void axienet_set_multicast_list(struct net_device *ndev)
  384. {
  385. int i = 0;
  386. u32 reg, af0reg, af1reg;
  387. struct axienet_local *lp = netdev_priv(ndev);
  388. reg = axienet_ior(lp, XAE_FMI_OFFSET);
  389. reg &= ~XAE_FMI_PM_MASK;
  390. if (ndev->flags & IFF_PROMISC)
  391. reg |= XAE_FMI_PM_MASK;
  392. else
  393. reg &= ~XAE_FMI_PM_MASK;
  394. axienet_iow(lp, XAE_FMI_OFFSET, reg);
  395. if (ndev->flags & IFF_ALLMULTI ||
  396. netdev_mc_count(ndev) > XAE_MULTICAST_CAM_TABLE_NUM) {
  397. reg &= 0xFFFFFF00;
  398. axienet_iow(lp, XAE_FMI_OFFSET, reg);
  399. axienet_iow(lp, XAE_AF0_OFFSET, 1); /* Multicast bit */
  400. axienet_iow(lp, XAE_AF1_OFFSET, 0);
  401. axienet_iow(lp, XAE_AM0_OFFSET, 1); /* ditto */
  402. axienet_iow(lp, XAE_AM1_OFFSET, 0);
  403. axienet_iow(lp, XAE_FFE_OFFSET, 1);
  404. i = 1;
  405. } else if (!netdev_mc_empty(ndev)) {
  406. struct netdev_hw_addr *ha;
  407. netdev_for_each_mc_addr(ha, ndev) {
  408. if (i >= XAE_MULTICAST_CAM_TABLE_NUM)
  409. break;
  410. af0reg = (ha->addr[0]);
  411. af0reg |= (ha->addr[1] << 8);
  412. af0reg |= (ha->addr[2] << 16);
  413. af0reg |= (ha->addr[3] << 24);
  414. af1reg = (ha->addr[4]);
  415. af1reg |= (ha->addr[5] << 8);
  416. reg &= 0xFFFFFF00;
  417. reg |= i;
  418. axienet_iow(lp, XAE_FMI_OFFSET, reg);
  419. axienet_iow(lp, XAE_AF0_OFFSET, af0reg);
  420. axienet_iow(lp, XAE_AF1_OFFSET, af1reg);
  421. axienet_iow(lp, XAE_AM0_OFFSET, 0xffffffff);
  422. axienet_iow(lp, XAE_AM1_OFFSET, 0x0000ffff);
  423. axienet_iow(lp, XAE_FFE_OFFSET, 1);
  424. i++;
  425. }
  426. }
  427. for (; i < XAE_MULTICAST_CAM_TABLE_NUM; i++) {
  428. reg &= 0xFFFFFF00;
  429. reg |= i;
  430. axienet_iow(lp, XAE_FMI_OFFSET, reg);
  431. axienet_iow(lp, XAE_FFE_OFFSET, 0);
  432. }
  433. }
  434. /**
  435. * axienet_setoptions - Set an Axi Ethernet option
  436. * @ndev: Pointer to the net_device structure
  437. * @options: Option to be enabled/disabled
  438. *
  439. * The Axi Ethernet core has multiple features which can be selectively turned
  440. * on or off. The typical options could be jumbo frame option, basic VLAN
  441. * option, promiscuous mode option etc. This function is used to set or clear
  442. * these options in the Axi Ethernet hardware. This is done through
  443. * axienet_option structure .
  444. */
  445. static void axienet_setoptions(struct net_device *ndev, u32 options)
  446. {
  447. int reg;
  448. struct axienet_local *lp = netdev_priv(ndev);
  449. struct axienet_option *tp = &axienet_options[0];
  450. while (tp->opt) {
  451. reg = ((axienet_ior(lp, tp->reg)) & ~(tp->m_or));
  452. if (options & tp->opt)
  453. reg |= tp->m_or;
  454. axienet_iow(lp, tp->reg, reg);
  455. tp++;
  456. }
  457. lp->options |= options;
  458. }
  459. static u64 axienet_stat(struct axienet_local *lp, enum temac_stat stat)
  460. {
  461. u32 counter;
  462. if (lp->reset_in_progress)
  463. return lp->hw_stat_base[stat];
  464. counter = axienet_ior(lp, XAE_STATS_OFFSET + stat * 8);
  465. return lp->hw_stat_base[stat] + (counter - lp->hw_last_counter[stat]);
  466. }
  467. static void axienet_stats_update(struct axienet_local *lp, bool reset)
  468. {
  469. enum temac_stat stat;
  470. write_seqcount_begin(&lp->hw_stats_seqcount);
  471. lp->reset_in_progress = reset;
  472. for (stat = 0; stat < STAT_COUNT; stat++) {
  473. u32 counter = axienet_ior(lp, XAE_STATS_OFFSET + stat * 8);
  474. lp->hw_stat_base[stat] += counter - lp->hw_last_counter[stat];
  475. lp->hw_last_counter[stat] = counter;
  476. }
  477. write_seqcount_end(&lp->hw_stats_seqcount);
  478. }
  479. static void axienet_refresh_stats(struct work_struct *work)
  480. {
  481. struct axienet_local *lp = container_of(work, struct axienet_local,
  482. stats_work.work);
  483. mutex_lock(&lp->stats_lock);
  484. axienet_stats_update(lp, false);
  485. mutex_unlock(&lp->stats_lock);
  486. /* Just less than 2^32 bytes at 2.5 GBit/s */
  487. schedule_delayed_work(&lp->stats_work, 13 * HZ);
  488. }
  489. static int __axienet_device_reset(struct axienet_local *lp)
  490. {
  491. u32 value;
  492. int ret;
  493. /* Save statistics counters in case they will be reset */
  494. mutex_lock(&lp->stats_lock);
  495. if (lp->features & XAE_FEATURE_STATS)
  496. axienet_stats_update(lp, true);
  497. /* Reset Axi DMA. This would reset Axi Ethernet core as well. The reset
  498. * process of Axi DMA takes a while to complete as all pending
  499. * commands/transfers will be flushed or completed during this
  500. * reset process.
  501. * Note that even though both TX and RX have their own reset register,
  502. * they both reset the entire DMA core, so only one needs to be used.
  503. */
  504. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, XAXIDMA_CR_RESET_MASK);
  505. ret = read_poll_timeout(axienet_dma_in32, value,
  506. !(value & XAXIDMA_CR_RESET_MASK),
  507. DELAY_OF_ONE_MILLISEC, 50000, false, lp,
  508. XAXIDMA_TX_CR_OFFSET);
  509. if (ret) {
  510. dev_err(lp->dev, "%s: DMA reset timeout!\n", __func__);
  511. goto out;
  512. }
  513. /* Wait for PhyRstCmplt bit to be set, indicating the PHY reset has finished */
  514. ret = read_poll_timeout(axienet_ior, value,
  515. value & XAE_INT_PHYRSTCMPLT_MASK,
  516. DELAY_OF_ONE_MILLISEC, 50000, false, lp,
  517. XAE_IS_OFFSET);
  518. if (ret) {
  519. dev_err(lp->dev, "%s: timeout waiting for PhyRstCmplt\n", __func__);
  520. goto out;
  521. }
  522. /* Update statistics counters with new values */
  523. if (lp->features & XAE_FEATURE_STATS) {
  524. enum temac_stat stat;
  525. write_seqcount_begin(&lp->hw_stats_seqcount);
  526. lp->reset_in_progress = false;
  527. for (stat = 0; stat < STAT_COUNT; stat++) {
  528. u32 counter =
  529. axienet_ior(lp, XAE_STATS_OFFSET + stat * 8);
  530. lp->hw_stat_base[stat] +=
  531. lp->hw_last_counter[stat] - counter;
  532. lp->hw_last_counter[stat] = counter;
  533. }
  534. write_seqcount_end(&lp->hw_stats_seqcount);
  535. }
  536. out:
  537. mutex_unlock(&lp->stats_lock);
  538. return ret;
  539. }
  540. /**
  541. * axienet_dma_stop - Stop DMA operation
  542. * @lp: Pointer to the axienet_local structure
  543. */
  544. static void axienet_dma_stop(struct axienet_local *lp)
  545. {
  546. int count;
  547. u32 cr, sr;
  548. cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  549. cr &= ~(XAXIDMA_CR_RUNSTOP_MASK | XAXIDMA_IRQ_ALL_MASK);
  550. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
  551. synchronize_irq(lp->rx_irq);
  552. cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  553. cr &= ~(XAXIDMA_CR_RUNSTOP_MASK | XAXIDMA_IRQ_ALL_MASK);
  554. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
  555. synchronize_irq(lp->tx_irq);
  556. /* Give DMAs a chance to halt gracefully */
  557. sr = axienet_dma_in32(lp, XAXIDMA_RX_SR_OFFSET);
  558. for (count = 0; !(sr & XAXIDMA_SR_HALT_MASK) && count < 5; ++count) {
  559. msleep(20);
  560. sr = axienet_dma_in32(lp, XAXIDMA_RX_SR_OFFSET);
  561. }
  562. sr = axienet_dma_in32(lp, XAXIDMA_TX_SR_OFFSET);
  563. for (count = 0; !(sr & XAXIDMA_SR_HALT_MASK) && count < 5; ++count) {
  564. msleep(20);
  565. sr = axienet_dma_in32(lp, XAXIDMA_TX_SR_OFFSET);
  566. }
  567. /* Do a reset to ensure DMA is really stopped */
  568. axienet_lock_mii(lp);
  569. __axienet_device_reset(lp);
  570. axienet_unlock_mii(lp);
  571. }
  572. /**
  573. * axienet_device_reset - Reset and initialize the Axi Ethernet hardware.
  574. * @ndev: Pointer to the net_device structure
  575. *
  576. * This function is called to reset and initialize the Axi Ethernet core. This
  577. * is typically called during initialization. It does a reset of the Axi DMA
  578. * Rx/Tx channels and initializes the Axi DMA BDs. Since Axi DMA reset lines
  579. * are connected to Axi Ethernet reset lines, this in turn resets the Axi
  580. * Ethernet core. No separate hardware reset is done for the Axi Ethernet
  581. * core.
  582. * Returns 0 on success or a negative error number otherwise.
  583. */
  584. static int axienet_device_reset(struct net_device *ndev)
  585. {
  586. u32 axienet_status;
  587. struct axienet_local *lp = netdev_priv(ndev);
  588. int ret;
  589. lp->max_frm_size = XAE_MAX_VLAN_FRAME_SIZE;
  590. lp->options |= XAE_OPTION_VLAN;
  591. lp->options &= (~XAE_OPTION_JUMBO);
  592. if (ndev->mtu > XAE_MTU && ndev->mtu <= XAE_JUMBO_MTU) {
  593. lp->max_frm_size = ndev->mtu + VLAN_ETH_HLEN +
  594. XAE_TRL_SIZE;
  595. if (lp->max_frm_size <= lp->rxmem)
  596. lp->options |= XAE_OPTION_JUMBO;
  597. }
  598. if (!lp->use_dmaengine) {
  599. ret = __axienet_device_reset(lp);
  600. if (ret)
  601. return ret;
  602. ret = axienet_dma_bd_init(ndev);
  603. if (ret) {
  604. netdev_err(ndev, "%s: descriptor allocation failed\n",
  605. __func__);
  606. return ret;
  607. }
  608. }
  609. axienet_status = axienet_ior(lp, XAE_RCW1_OFFSET);
  610. axienet_status &= ~XAE_RCW1_RX_MASK;
  611. axienet_iow(lp, XAE_RCW1_OFFSET, axienet_status);
  612. axienet_status = axienet_ior(lp, XAE_IP_OFFSET);
  613. if (axienet_status & XAE_INT_RXRJECT_MASK)
  614. axienet_iow(lp, XAE_IS_OFFSET, XAE_INT_RXRJECT_MASK);
  615. axienet_iow(lp, XAE_IE_OFFSET, lp->eth_irq > 0 ?
  616. XAE_INT_RECV_ERROR_MASK : 0);
  617. axienet_iow(lp, XAE_FCC_OFFSET, XAE_FCC_FCRX_MASK);
  618. /* Sync default options with HW but leave receiver and
  619. * transmitter disabled.
  620. */
  621. axienet_setoptions(ndev, lp->options &
  622. ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
  623. axienet_set_mac_address(ndev, NULL);
  624. axienet_set_multicast_list(ndev);
  625. axienet_setoptions(ndev, lp->options);
  626. netif_trans_update(ndev);
  627. return 0;
  628. }
  629. /**
  630. * axienet_free_tx_chain - Clean up a series of linked TX descriptors.
  631. * @lp: Pointer to the axienet_local structure
  632. * @first_bd: Index of first descriptor to clean up
  633. * @nr_bds: Max number of descriptors to clean up
  634. * @force: Whether to clean descriptors even if not complete
  635. * @sizep: Pointer to a u32 filled with the total sum of all bytes
  636. * in all cleaned-up descriptors. Ignored if NULL.
  637. * @budget: NAPI budget (use 0 when not called from NAPI poll)
  638. *
  639. * Would either be called after a successful transmit operation, or after
  640. * there was an error when setting up the chain.
  641. * Returns the number of packets handled.
  642. */
  643. static int axienet_free_tx_chain(struct axienet_local *lp, u32 first_bd,
  644. int nr_bds, bool force, u32 *sizep, int budget)
  645. {
  646. struct axidma_bd *cur_p;
  647. unsigned int status;
  648. int i, packets = 0;
  649. dma_addr_t phys;
  650. for (i = 0; i < nr_bds; i++) {
  651. cur_p = &lp->tx_bd_v[(first_bd + i) % lp->tx_bd_num];
  652. status = cur_p->status;
  653. /* If force is not specified, clean up only descriptors
  654. * that have been completed by the MAC.
  655. */
  656. if (!force && !(status & XAXIDMA_BD_STS_COMPLETE_MASK))
  657. break;
  658. /* Ensure we see complete descriptor update */
  659. dma_rmb();
  660. phys = desc_get_phys_addr(lp, cur_p);
  661. dma_unmap_single(lp->dev, phys,
  662. (cur_p->cntrl & XAXIDMA_BD_CTRL_LENGTH_MASK),
  663. DMA_TO_DEVICE);
  664. if (cur_p->skb && (status & XAXIDMA_BD_STS_COMPLETE_MASK)) {
  665. napi_consume_skb(cur_p->skb, budget);
  666. packets++;
  667. }
  668. cur_p->app0 = 0;
  669. cur_p->app1 = 0;
  670. cur_p->app2 = 0;
  671. cur_p->app4 = 0;
  672. cur_p->skb = NULL;
  673. /* ensure our transmit path and device don't prematurely see status cleared */
  674. wmb();
  675. cur_p->cntrl = 0;
  676. cur_p->status = 0;
  677. if (sizep)
  678. *sizep += status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
  679. }
  680. if (!force) {
  681. lp->tx_bd_ci += i;
  682. if (lp->tx_bd_ci >= lp->tx_bd_num)
  683. lp->tx_bd_ci %= lp->tx_bd_num;
  684. }
  685. return packets;
  686. }
  687. /**
  688. * axienet_check_tx_bd_space - Checks if a BD/group of BDs are currently busy
  689. * @lp: Pointer to the axienet_local structure
  690. * @num_frag: The number of BDs to check for
  691. *
  692. * Return: 0, on success
  693. * NETDEV_TX_BUSY, if any of the descriptors are not free
  694. *
  695. * This function is invoked before BDs are allocated and transmission starts.
  696. * This function returns 0 if a BD or group of BDs can be allocated for
  697. * transmission. If the BD or any of the BDs are not free the function
  698. * returns a busy status.
  699. */
  700. static inline int axienet_check_tx_bd_space(struct axienet_local *lp,
  701. int num_frag)
  702. {
  703. struct axidma_bd *cur_p;
  704. /* Ensure we see all descriptor updates from device or TX polling */
  705. rmb();
  706. cur_p = &lp->tx_bd_v[(READ_ONCE(lp->tx_bd_tail) + num_frag) %
  707. lp->tx_bd_num];
  708. if (cur_p->cntrl)
  709. return NETDEV_TX_BUSY;
  710. return 0;
  711. }
  712. /**
  713. * axienet_dma_tx_cb - DMA engine callback for TX channel.
  714. * @data: Pointer to the axienet_local structure.
  715. * @result: error reporting through dmaengine_result.
  716. * This function is called by dmaengine driver for TX channel to notify
  717. * that the transmit is done.
  718. */
  719. static void axienet_dma_tx_cb(void *data, const struct dmaengine_result *result)
  720. {
  721. struct skbuf_dma_descriptor *skbuf_dma;
  722. struct axienet_local *lp = data;
  723. struct netdev_queue *txq;
  724. int len;
  725. skbuf_dma = axienet_get_tx_desc(lp, lp->tx_ring_tail++);
  726. len = skbuf_dma->skb->len;
  727. txq = skb_get_tx_queue(lp->ndev, skbuf_dma->skb);
  728. u64_stats_update_begin(&lp->tx_stat_sync);
  729. u64_stats_add(&lp->tx_bytes, len);
  730. u64_stats_add(&lp->tx_packets, 1);
  731. u64_stats_update_end(&lp->tx_stat_sync);
  732. dma_unmap_sg(lp->dev, skbuf_dma->sgl, skbuf_dma->sg_len, DMA_TO_DEVICE);
  733. dev_consume_skb_any(skbuf_dma->skb);
  734. netif_txq_completed_wake(txq, 1, len,
  735. CIRC_SPACE(lp->tx_ring_head, lp->tx_ring_tail, TX_BD_NUM_MAX),
  736. 2);
  737. }
  738. /**
  739. * axienet_start_xmit_dmaengine - Starts the transmission.
  740. * @skb: sk_buff pointer that contains data to be Txed.
  741. * @ndev: Pointer to net_device structure.
  742. *
  743. * Return: NETDEV_TX_OK on success or any non space errors.
  744. * NETDEV_TX_BUSY when free element in TX skb ring buffer
  745. * is not available.
  746. *
  747. * This function is invoked to initiate transmission. The
  748. * function sets the skbs, register dma callback API and submit
  749. * the dma transaction.
  750. * Additionally if checksum offloading is supported,
  751. * it populates AXI Stream Control fields with appropriate values.
  752. */
  753. static netdev_tx_t
  754. axienet_start_xmit_dmaengine(struct sk_buff *skb, struct net_device *ndev)
  755. {
  756. struct dma_async_tx_descriptor *dma_tx_desc = NULL;
  757. struct axienet_local *lp = netdev_priv(ndev);
  758. u32 app_metadata[DMA_NUM_APP_WORDS] = {0};
  759. struct skbuf_dma_descriptor *skbuf_dma;
  760. struct dma_device *dma_dev;
  761. struct netdev_queue *txq;
  762. u32 csum_start_off;
  763. u32 csum_index_off;
  764. int sg_len;
  765. int ret;
  766. dma_dev = lp->tx_chan->device;
  767. sg_len = skb_shinfo(skb)->nr_frags + 1;
  768. if (CIRC_SPACE(lp->tx_ring_head, lp->tx_ring_tail, TX_BD_NUM_MAX) <= 1) {
  769. netif_stop_queue(ndev);
  770. if (net_ratelimit())
  771. netdev_warn(ndev, "TX ring unexpectedly full\n");
  772. return NETDEV_TX_BUSY;
  773. }
  774. skbuf_dma = axienet_get_tx_desc(lp, lp->tx_ring_head);
  775. if (!skbuf_dma)
  776. goto xmit_error_drop_skb;
  777. lp->tx_ring_head++;
  778. sg_init_table(skbuf_dma->sgl, sg_len);
  779. ret = skb_to_sgvec(skb, skbuf_dma->sgl, 0, skb->len);
  780. if (ret < 0)
  781. goto xmit_error_drop_skb;
  782. ret = dma_map_sg(lp->dev, skbuf_dma->sgl, sg_len, DMA_TO_DEVICE);
  783. if (!ret)
  784. goto xmit_error_drop_skb;
  785. /* Fill up app fields for checksum */
  786. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  787. if (lp->features & XAE_FEATURE_FULL_TX_CSUM) {
  788. /* Tx Full Checksum Offload Enabled */
  789. app_metadata[0] |= 2;
  790. } else if (lp->features & XAE_FEATURE_PARTIAL_TX_CSUM) {
  791. csum_start_off = skb_transport_offset(skb);
  792. csum_index_off = csum_start_off + skb->csum_offset;
  793. /* Tx Partial Checksum Offload Enabled */
  794. app_metadata[0] |= 1;
  795. app_metadata[1] = (csum_start_off << 16) | csum_index_off;
  796. }
  797. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  798. app_metadata[0] |= 2; /* Tx Full Checksum Offload Enabled */
  799. }
  800. dma_tx_desc = dma_dev->device_prep_slave_sg(lp->tx_chan, skbuf_dma->sgl,
  801. sg_len, DMA_MEM_TO_DEV,
  802. DMA_PREP_INTERRUPT, (void *)app_metadata);
  803. if (!dma_tx_desc)
  804. goto xmit_error_unmap_sg;
  805. skbuf_dma->skb = skb;
  806. skbuf_dma->sg_len = sg_len;
  807. dma_tx_desc->callback_param = lp;
  808. dma_tx_desc->callback_result = axienet_dma_tx_cb;
  809. txq = skb_get_tx_queue(lp->ndev, skb);
  810. netdev_tx_sent_queue(txq, skb->len);
  811. netif_txq_maybe_stop(txq, CIRC_SPACE(lp->tx_ring_head, lp->tx_ring_tail, TX_BD_NUM_MAX),
  812. 1, 2);
  813. dmaengine_submit(dma_tx_desc);
  814. dma_async_issue_pending(lp->tx_chan);
  815. return NETDEV_TX_OK;
  816. xmit_error_unmap_sg:
  817. dma_unmap_sg(lp->dev, skbuf_dma->sgl, sg_len, DMA_TO_DEVICE);
  818. xmit_error_drop_skb:
  819. dev_kfree_skb_any(skb);
  820. return NETDEV_TX_OK;
  821. }
  822. /**
  823. * axienet_tx_poll - Invoked once a transmit is completed by the
  824. * Axi DMA Tx channel.
  825. * @napi: Pointer to NAPI structure.
  826. * @budget: Max number of TX packets to process.
  827. *
  828. * Return: Number of TX packets processed.
  829. *
  830. * This function is invoked from the NAPI processing to notify the completion
  831. * of transmit operation. It clears fields in the corresponding Tx BDs and
  832. * unmaps the corresponding buffer so that CPU can regain ownership of the
  833. * buffer. It finally invokes "netif_wake_queue" to restart transmission if
  834. * required.
  835. */
  836. static int axienet_tx_poll(struct napi_struct *napi, int budget)
  837. {
  838. struct axienet_local *lp = container_of(napi, struct axienet_local, napi_tx);
  839. struct net_device *ndev = lp->ndev;
  840. u32 size = 0;
  841. int packets;
  842. packets = axienet_free_tx_chain(lp, lp->tx_bd_ci, lp->tx_bd_num, false,
  843. &size, budget);
  844. if (packets) {
  845. u64_stats_update_begin(&lp->tx_stat_sync);
  846. u64_stats_add(&lp->tx_packets, packets);
  847. u64_stats_add(&lp->tx_bytes, size);
  848. u64_stats_update_end(&lp->tx_stat_sync);
  849. /* Matches barrier in axienet_start_xmit */
  850. smp_mb();
  851. if (!axienet_check_tx_bd_space(lp, MAX_SKB_FRAGS + 1))
  852. netif_wake_queue(ndev);
  853. }
  854. if (packets < budget && napi_complete_done(napi, packets)) {
  855. /* Re-enable TX completion interrupts. This should
  856. * cause an immediate interrupt if any TX packets are
  857. * already pending.
  858. */
  859. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, lp->tx_dma_cr);
  860. }
  861. return packets;
  862. }
  863. /**
  864. * axienet_start_xmit - Starts the transmission.
  865. * @skb: sk_buff pointer that contains data to be Txed.
  866. * @ndev: Pointer to net_device structure.
  867. *
  868. * Return: NETDEV_TX_OK, on success
  869. * NETDEV_TX_BUSY, if any of the descriptors are not free
  870. *
  871. * This function is invoked from upper layers to initiate transmission. The
  872. * function uses the next available free BDs and populates their fields to
  873. * start the transmission. Additionally if checksum offloading is supported,
  874. * it populates AXI Stream Control fields with appropriate values.
  875. */
  876. static netdev_tx_t
  877. axienet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  878. {
  879. u32 ii;
  880. u32 num_frag;
  881. u32 csum_start_off;
  882. u32 csum_index_off;
  883. skb_frag_t *frag;
  884. dma_addr_t tail_p, phys;
  885. u32 orig_tail_ptr, new_tail_ptr;
  886. struct axienet_local *lp = netdev_priv(ndev);
  887. struct axidma_bd *cur_p;
  888. orig_tail_ptr = lp->tx_bd_tail;
  889. new_tail_ptr = orig_tail_ptr;
  890. num_frag = skb_shinfo(skb)->nr_frags;
  891. cur_p = &lp->tx_bd_v[orig_tail_ptr];
  892. if (axienet_check_tx_bd_space(lp, num_frag + 1)) {
  893. /* Should not happen as last start_xmit call should have
  894. * checked for sufficient space and queue should only be
  895. * woken when sufficient space is available.
  896. */
  897. netif_stop_queue(ndev);
  898. if (net_ratelimit())
  899. netdev_warn(ndev, "TX ring unexpectedly full\n");
  900. return NETDEV_TX_BUSY;
  901. }
  902. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  903. if (lp->features & XAE_FEATURE_FULL_TX_CSUM) {
  904. /* Tx Full Checksum Offload Enabled */
  905. cur_p->app0 |= 2;
  906. } else if (lp->features & XAE_FEATURE_PARTIAL_TX_CSUM) {
  907. csum_start_off = skb_transport_offset(skb);
  908. csum_index_off = csum_start_off + skb->csum_offset;
  909. /* Tx Partial Checksum Offload Enabled */
  910. cur_p->app0 |= 1;
  911. cur_p->app1 = (csum_start_off << 16) | csum_index_off;
  912. }
  913. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  914. cur_p->app0 |= 2; /* Tx Full Checksum Offload Enabled */
  915. }
  916. phys = dma_map_single(lp->dev, skb->data,
  917. skb_headlen(skb), DMA_TO_DEVICE);
  918. if (unlikely(dma_mapping_error(lp->dev, phys))) {
  919. if (net_ratelimit())
  920. netdev_err(ndev, "TX DMA mapping error\n");
  921. ndev->stats.tx_dropped++;
  922. dev_kfree_skb_any(skb);
  923. return NETDEV_TX_OK;
  924. }
  925. desc_set_phys_addr(lp, phys, cur_p);
  926. cur_p->cntrl = skb_headlen(skb) | XAXIDMA_BD_CTRL_TXSOF_MASK;
  927. for (ii = 0; ii < num_frag; ii++) {
  928. if (++new_tail_ptr >= lp->tx_bd_num)
  929. new_tail_ptr = 0;
  930. cur_p = &lp->tx_bd_v[new_tail_ptr];
  931. frag = &skb_shinfo(skb)->frags[ii];
  932. phys = dma_map_single(lp->dev,
  933. skb_frag_address(frag),
  934. skb_frag_size(frag),
  935. DMA_TO_DEVICE);
  936. if (unlikely(dma_mapping_error(lp->dev, phys))) {
  937. if (net_ratelimit())
  938. netdev_err(ndev, "TX DMA mapping error\n");
  939. ndev->stats.tx_dropped++;
  940. axienet_free_tx_chain(lp, orig_tail_ptr, ii + 1,
  941. true, NULL, 0);
  942. dev_kfree_skb_any(skb);
  943. return NETDEV_TX_OK;
  944. }
  945. desc_set_phys_addr(lp, phys, cur_p);
  946. cur_p->cntrl = skb_frag_size(frag);
  947. }
  948. cur_p->cntrl |= XAXIDMA_BD_CTRL_TXEOF_MASK;
  949. cur_p->skb = skb;
  950. tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * new_tail_ptr;
  951. if (++new_tail_ptr >= lp->tx_bd_num)
  952. new_tail_ptr = 0;
  953. WRITE_ONCE(lp->tx_bd_tail, new_tail_ptr);
  954. /* Start the transfer */
  955. axienet_dma_out_addr(lp, XAXIDMA_TX_TDESC_OFFSET, tail_p);
  956. /* Stop queue if next transmit may not have space */
  957. if (axienet_check_tx_bd_space(lp, MAX_SKB_FRAGS + 1)) {
  958. netif_stop_queue(ndev);
  959. /* Matches barrier in axienet_tx_poll */
  960. smp_mb();
  961. /* Space might have just been freed - check again */
  962. if (!axienet_check_tx_bd_space(lp, MAX_SKB_FRAGS + 1))
  963. netif_wake_queue(ndev);
  964. }
  965. return NETDEV_TX_OK;
  966. }
  967. /**
  968. * axienet_dma_rx_cb - DMA engine callback for RX channel.
  969. * @data: Pointer to the skbuf_dma_descriptor structure.
  970. * @result: error reporting through dmaengine_result.
  971. * This function is called by dmaengine driver for RX channel to notify
  972. * that the packet is received.
  973. */
  974. static void axienet_dma_rx_cb(void *data, const struct dmaengine_result *result)
  975. {
  976. struct skbuf_dma_descriptor *skbuf_dma;
  977. size_t meta_len, meta_max_len, rx_len;
  978. struct axienet_local *lp = data;
  979. struct sk_buff *skb;
  980. u32 *app_metadata;
  981. int i;
  982. skbuf_dma = axienet_get_rx_desc(lp, lp->rx_ring_tail++);
  983. skb = skbuf_dma->skb;
  984. app_metadata = dmaengine_desc_get_metadata_ptr(skbuf_dma->desc, &meta_len,
  985. &meta_max_len);
  986. dma_unmap_single(lp->dev, skbuf_dma->dma_address, lp->max_frm_size,
  987. DMA_FROM_DEVICE);
  988. if (IS_ERR(app_metadata)) {
  989. if (net_ratelimit())
  990. netdev_err(lp->ndev, "Failed to get RX metadata pointer\n");
  991. dev_kfree_skb_any(skb);
  992. lp->ndev->stats.rx_dropped++;
  993. goto rx_submit;
  994. }
  995. /* TODO: Derive app word index programmatically */
  996. rx_len = (app_metadata[LEN_APP] & 0xFFFF);
  997. skb_put(skb, rx_len);
  998. skb->protocol = eth_type_trans(skb, lp->ndev);
  999. skb->ip_summed = CHECKSUM_NONE;
  1000. __netif_rx(skb);
  1001. u64_stats_update_begin(&lp->rx_stat_sync);
  1002. u64_stats_add(&lp->rx_packets, 1);
  1003. u64_stats_add(&lp->rx_bytes, rx_len);
  1004. u64_stats_update_end(&lp->rx_stat_sync);
  1005. rx_submit:
  1006. for (i = 0; i < CIRC_SPACE(lp->rx_ring_head, lp->rx_ring_tail,
  1007. RX_BUF_NUM_DEFAULT); i++)
  1008. axienet_rx_submit_desc(lp->ndev);
  1009. dma_async_issue_pending(lp->rx_chan);
  1010. }
  1011. /**
  1012. * axienet_rx_poll - Triggered by RX ISR to complete the BD processing.
  1013. * @napi: Pointer to NAPI structure.
  1014. * @budget: Max number of RX packets to process.
  1015. *
  1016. * Return: Number of RX packets processed.
  1017. */
  1018. static int axienet_rx_poll(struct napi_struct *napi, int budget)
  1019. {
  1020. u32 length;
  1021. u32 csumstatus;
  1022. u32 size = 0;
  1023. int packets = 0;
  1024. dma_addr_t tail_p = 0;
  1025. struct axidma_bd *cur_p;
  1026. struct sk_buff *skb, *new_skb;
  1027. struct axienet_local *lp = container_of(napi, struct axienet_local, napi_rx);
  1028. cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
  1029. while (packets < budget && (cur_p->status & XAXIDMA_BD_STS_COMPLETE_MASK)) {
  1030. dma_addr_t phys;
  1031. /* Ensure we see complete descriptor update */
  1032. dma_rmb();
  1033. skb = cur_p->skb;
  1034. cur_p->skb = NULL;
  1035. /* skb could be NULL if a previous pass already received the
  1036. * packet for this slot in the ring, but failed to refill it
  1037. * with a newly allocated buffer. In this case, don't try to
  1038. * receive it again.
  1039. */
  1040. if (likely(skb)) {
  1041. length = cur_p->app4 & 0x0000FFFF;
  1042. phys = desc_get_phys_addr(lp, cur_p);
  1043. dma_unmap_single(lp->dev, phys, lp->max_frm_size,
  1044. DMA_FROM_DEVICE);
  1045. skb_put(skb, length);
  1046. skb->protocol = eth_type_trans(skb, lp->ndev);
  1047. /*skb_checksum_none_assert(skb);*/
  1048. skb->ip_summed = CHECKSUM_NONE;
  1049. /* if we're doing Rx csum offload, set it up */
  1050. if (lp->features & XAE_FEATURE_FULL_RX_CSUM) {
  1051. csumstatus = (cur_p->app2 &
  1052. XAE_FULL_CSUM_STATUS_MASK) >> 3;
  1053. if (csumstatus == XAE_IP_TCP_CSUM_VALIDATED ||
  1054. csumstatus == XAE_IP_UDP_CSUM_VALIDATED) {
  1055. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1056. }
  1057. } else if (lp->features & XAE_FEATURE_PARTIAL_RX_CSUM) {
  1058. skb->csum = be32_to_cpu(cur_p->app3 & 0xFFFF);
  1059. skb->ip_summed = CHECKSUM_COMPLETE;
  1060. }
  1061. napi_gro_receive(napi, skb);
  1062. size += length;
  1063. packets++;
  1064. }
  1065. new_skb = napi_alloc_skb(napi, lp->max_frm_size);
  1066. if (!new_skb)
  1067. break;
  1068. phys = dma_map_single(lp->dev, new_skb->data,
  1069. lp->max_frm_size,
  1070. DMA_FROM_DEVICE);
  1071. if (unlikely(dma_mapping_error(lp->dev, phys))) {
  1072. if (net_ratelimit())
  1073. netdev_err(lp->ndev, "RX DMA mapping error\n");
  1074. dev_kfree_skb(new_skb);
  1075. break;
  1076. }
  1077. desc_set_phys_addr(lp, phys, cur_p);
  1078. cur_p->cntrl = lp->max_frm_size;
  1079. cur_p->status = 0;
  1080. cur_p->skb = new_skb;
  1081. /* Only update tail_p to mark this slot as usable after it has
  1082. * been successfully refilled.
  1083. */
  1084. tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
  1085. if (++lp->rx_bd_ci >= lp->rx_bd_num)
  1086. lp->rx_bd_ci = 0;
  1087. cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
  1088. }
  1089. u64_stats_update_begin(&lp->rx_stat_sync);
  1090. u64_stats_add(&lp->rx_packets, packets);
  1091. u64_stats_add(&lp->rx_bytes, size);
  1092. u64_stats_update_end(&lp->rx_stat_sync);
  1093. if (tail_p)
  1094. axienet_dma_out_addr(lp, XAXIDMA_RX_TDESC_OFFSET, tail_p);
  1095. if (packets < budget && napi_complete_done(napi, packets)) {
  1096. /* Re-enable RX completion interrupts. This should
  1097. * cause an immediate interrupt if any RX packets are
  1098. * already pending.
  1099. */
  1100. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, lp->rx_dma_cr);
  1101. }
  1102. return packets;
  1103. }
  1104. /**
  1105. * axienet_tx_irq - Tx Done Isr.
  1106. * @irq: irq number
  1107. * @_ndev: net_device pointer
  1108. *
  1109. * Return: IRQ_HANDLED if device generated a TX interrupt, IRQ_NONE otherwise.
  1110. *
  1111. * This is the Axi DMA Tx done Isr. It invokes NAPI polling to complete the
  1112. * TX BD processing.
  1113. */
  1114. static irqreturn_t axienet_tx_irq(int irq, void *_ndev)
  1115. {
  1116. unsigned int status;
  1117. struct net_device *ndev = _ndev;
  1118. struct axienet_local *lp = netdev_priv(ndev);
  1119. status = axienet_dma_in32(lp, XAXIDMA_TX_SR_OFFSET);
  1120. if (!(status & XAXIDMA_IRQ_ALL_MASK))
  1121. return IRQ_NONE;
  1122. axienet_dma_out32(lp, XAXIDMA_TX_SR_OFFSET, status);
  1123. if (unlikely(status & XAXIDMA_IRQ_ERROR_MASK)) {
  1124. netdev_err(ndev, "DMA Tx error 0x%x\n", status);
  1125. netdev_err(ndev, "Current BD is at: 0x%x%08x\n",
  1126. (lp->tx_bd_v[lp->tx_bd_ci]).phys_msb,
  1127. (lp->tx_bd_v[lp->tx_bd_ci]).phys);
  1128. schedule_work(&lp->dma_err_task);
  1129. } else {
  1130. /* Disable further TX completion interrupts and schedule
  1131. * NAPI to handle the completions.
  1132. */
  1133. u32 cr = lp->tx_dma_cr;
  1134. cr &= ~(XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK);
  1135. if (napi_schedule_prep(&lp->napi_tx)) {
  1136. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
  1137. __napi_schedule(&lp->napi_tx);
  1138. }
  1139. }
  1140. return IRQ_HANDLED;
  1141. }
  1142. /**
  1143. * axienet_rx_irq - Rx Isr.
  1144. * @irq: irq number
  1145. * @_ndev: net_device pointer
  1146. *
  1147. * Return: IRQ_HANDLED if device generated a RX interrupt, IRQ_NONE otherwise.
  1148. *
  1149. * This is the Axi DMA Rx Isr. It invokes NAPI polling to complete the RX BD
  1150. * processing.
  1151. */
  1152. static irqreturn_t axienet_rx_irq(int irq, void *_ndev)
  1153. {
  1154. unsigned int status;
  1155. struct net_device *ndev = _ndev;
  1156. struct axienet_local *lp = netdev_priv(ndev);
  1157. status = axienet_dma_in32(lp, XAXIDMA_RX_SR_OFFSET);
  1158. if (!(status & XAXIDMA_IRQ_ALL_MASK))
  1159. return IRQ_NONE;
  1160. axienet_dma_out32(lp, XAXIDMA_RX_SR_OFFSET, status);
  1161. if (unlikely(status & XAXIDMA_IRQ_ERROR_MASK)) {
  1162. netdev_err(ndev, "DMA Rx error 0x%x\n", status);
  1163. netdev_err(ndev, "Current BD is at: 0x%x%08x\n",
  1164. (lp->rx_bd_v[lp->rx_bd_ci]).phys_msb,
  1165. (lp->rx_bd_v[lp->rx_bd_ci]).phys);
  1166. schedule_work(&lp->dma_err_task);
  1167. } else {
  1168. /* Disable further RX completion interrupts and schedule
  1169. * NAPI receive.
  1170. */
  1171. u32 cr = lp->rx_dma_cr;
  1172. cr &= ~(XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK);
  1173. if (napi_schedule_prep(&lp->napi_rx)) {
  1174. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
  1175. __napi_schedule(&lp->napi_rx);
  1176. }
  1177. }
  1178. return IRQ_HANDLED;
  1179. }
  1180. /**
  1181. * axienet_eth_irq - Ethernet core Isr.
  1182. * @irq: irq number
  1183. * @_ndev: net_device pointer
  1184. *
  1185. * Return: IRQ_HANDLED if device generated a core interrupt, IRQ_NONE otherwise.
  1186. *
  1187. * Handle miscellaneous conditions indicated by Ethernet core IRQ.
  1188. */
  1189. static irqreturn_t axienet_eth_irq(int irq, void *_ndev)
  1190. {
  1191. struct net_device *ndev = _ndev;
  1192. struct axienet_local *lp = netdev_priv(ndev);
  1193. unsigned int pending;
  1194. pending = axienet_ior(lp, XAE_IP_OFFSET);
  1195. if (!pending)
  1196. return IRQ_NONE;
  1197. if (pending & XAE_INT_RXFIFOOVR_MASK)
  1198. ndev->stats.rx_missed_errors++;
  1199. if (pending & XAE_INT_RXRJECT_MASK)
  1200. ndev->stats.rx_dropped++;
  1201. axienet_iow(lp, XAE_IS_OFFSET, pending);
  1202. return IRQ_HANDLED;
  1203. }
  1204. static void axienet_dma_err_handler(struct work_struct *work);
  1205. /**
  1206. * axienet_rx_submit_desc - Submit the rx descriptors to dmaengine.
  1207. * allocate skbuff, map the scatterlist and obtain a descriptor
  1208. * and then add the callback information and submit descriptor.
  1209. *
  1210. * @ndev: net_device pointer
  1211. *
  1212. */
  1213. static void axienet_rx_submit_desc(struct net_device *ndev)
  1214. {
  1215. struct dma_async_tx_descriptor *dma_rx_desc = NULL;
  1216. struct axienet_local *lp = netdev_priv(ndev);
  1217. struct skbuf_dma_descriptor *skbuf_dma;
  1218. struct sk_buff *skb;
  1219. dma_addr_t addr;
  1220. skbuf_dma = axienet_get_rx_desc(lp, lp->rx_ring_head);
  1221. if (!skbuf_dma)
  1222. return;
  1223. skb = netdev_alloc_skb(ndev, lp->max_frm_size);
  1224. if (!skb)
  1225. return;
  1226. sg_init_table(skbuf_dma->sgl, 1);
  1227. addr = dma_map_single(lp->dev, skb->data, lp->max_frm_size, DMA_FROM_DEVICE);
  1228. if (unlikely(dma_mapping_error(lp->dev, addr))) {
  1229. if (net_ratelimit())
  1230. netdev_err(ndev, "DMA mapping error\n");
  1231. goto rx_submit_err_free_skb;
  1232. }
  1233. sg_dma_address(skbuf_dma->sgl) = addr;
  1234. sg_dma_len(skbuf_dma->sgl) = lp->max_frm_size;
  1235. dma_rx_desc = dmaengine_prep_slave_sg(lp->rx_chan, skbuf_dma->sgl,
  1236. 1, DMA_DEV_TO_MEM,
  1237. DMA_PREP_INTERRUPT);
  1238. if (!dma_rx_desc)
  1239. goto rx_submit_err_unmap_skb;
  1240. skbuf_dma->skb = skb;
  1241. skbuf_dma->dma_address = sg_dma_address(skbuf_dma->sgl);
  1242. skbuf_dma->desc = dma_rx_desc;
  1243. dma_rx_desc->callback_param = lp;
  1244. dma_rx_desc->callback_result = axienet_dma_rx_cb;
  1245. lp->rx_ring_head++;
  1246. dmaengine_submit(dma_rx_desc);
  1247. return;
  1248. rx_submit_err_unmap_skb:
  1249. dma_unmap_single(lp->dev, addr, lp->max_frm_size, DMA_FROM_DEVICE);
  1250. rx_submit_err_free_skb:
  1251. dev_kfree_skb(skb);
  1252. }
  1253. /**
  1254. * axienet_init_dmaengine - init the dmaengine code.
  1255. * @ndev: Pointer to net_device structure
  1256. *
  1257. * Return: 0, on success.
  1258. * non-zero error value on failure
  1259. *
  1260. * This is the dmaengine initialization code.
  1261. */
  1262. static int axienet_init_dmaengine(struct net_device *ndev)
  1263. {
  1264. struct axienet_local *lp = netdev_priv(ndev);
  1265. struct skbuf_dma_descriptor *skbuf_dma;
  1266. int i, ret;
  1267. lp->tx_chan = dma_request_chan(lp->dev, "tx_chan0");
  1268. if (IS_ERR(lp->tx_chan)) {
  1269. dev_err(lp->dev, "No Ethernet DMA (TX) channel found\n");
  1270. return PTR_ERR(lp->tx_chan);
  1271. }
  1272. lp->rx_chan = dma_request_chan(lp->dev, "rx_chan0");
  1273. if (IS_ERR(lp->rx_chan)) {
  1274. ret = PTR_ERR(lp->rx_chan);
  1275. dev_err(lp->dev, "No Ethernet DMA (RX) channel found\n");
  1276. goto err_dma_release_tx;
  1277. }
  1278. lp->tx_ring_tail = 0;
  1279. lp->tx_ring_head = 0;
  1280. lp->rx_ring_tail = 0;
  1281. lp->rx_ring_head = 0;
  1282. lp->tx_skb_ring = kcalloc(TX_BD_NUM_MAX, sizeof(*lp->tx_skb_ring),
  1283. GFP_KERNEL);
  1284. if (!lp->tx_skb_ring) {
  1285. ret = -ENOMEM;
  1286. goto err_dma_release_rx;
  1287. }
  1288. for (i = 0; i < TX_BD_NUM_MAX; i++) {
  1289. skbuf_dma = kzalloc(sizeof(*skbuf_dma), GFP_KERNEL);
  1290. if (!skbuf_dma) {
  1291. ret = -ENOMEM;
  1292. goto err_free_tx_skb_ring;
  1293. }
  1294. lp->tx_skb_ring[i] = skbuf_dma;
  1295. }
  1296. lp->rx_skb_ring = kcalloc(RX_BUF_NUM_DEFAULT, sizeof(*lp->rx_skb_ring),
  1297. GFP_KERNEL);
  1298. if (!lp->rx_skb_ring) {
  1299. ret = -ENOMEM;
  1300. goto err_free_tx_skb_ring;
  1301. }
  1302. for (i = 0; i < RX_BUF_NUM_DEFAULT; i++) {
  1303. skbuf_dma = kzalloc(sizeof(*skbuf_dma), GFP_KERNEL);
  1304. if (!skbuf_dma) {
  1305. ret = -ENOMEM;
  1306. goto err_free_rx_skb_ring;
  1307. }
  1308. lp->rx_skb_ring[i] = skbuf_dma;
  1309. }
  1310. /* TODO: Instead of BD_NUM_DEFAULT use runtime support */
  1311. for (i = 0; i < RX_BUF_NUM_DEFAULT; i++)
  1312. axienet_rx_submit_desc(ndev);
  1313. dma_async_issue_pending(lp->rx_chan);
  1314. return 0;
  1315. err_free_rx_skb_ring:
  1316. for (i = 0; i < RX_BUF_NUM_DEFAULT; i++)
  1317. kfree(lp->rx_skb_ring[i]);
  1318. kfree(lp->rx_skb_ring);
  1319. err_free_tx_skb_ring:
  1320. for (i = 0; i < TX_BD_NUM_MAX; i++)
  1321. kfree(lp->tx_skb_ring[i]);
  1322. kfree(lp->tx_skb_ring);
  1323. err_dma_release_rx:
  1324. dma_release_channel(lp->rx_chan);
  1325. err_dma_release_tx:
  1326. dma_release_channel(lp->tx_chan);
  1327. return ret;
  1328. }
  1329. /**
  1330. * axienet_init_legacy_dma - init the dma legacy code.
  1331. * @ndev: Pointer to net_device structure
  1332. *
  1333. * Return: 0, on success.
  1334. * non-zero error value on failure
  1335. *
  1336. * This is the dma initialization code. It also allocates interrupt
  1337. * service routines, enables the interrupt lines and ISR handling.
  1338. *
  1339. */
  1340. static int axienet_init_legacy_dma(struct net_device *ndev)
  1341. {
  1342. int ret;
  1343. struct axienet_local *lp = netdev_priv(ndev);
  1344. /* Enable worker thread for Axi DMA error handling */
  1345. lp->stopping = false;
  1346. INIT_WORK(&lp->dma_err_task, axienet_dma_err_handler);
  1347. napi_enable(&lp->napi_rx);
  1348. napi_enable(&lp->napi_tx);
  1349. /* Enable interrupts for Axi DMA Tx */
  1350. ret = request_irq(lp->tx_irq, axienet_tx_irq, IRQF_SHARED,
  1351. ndev->name, ndev);
  1352. if (ret)
  1353. goto err_tx_irq;
  1354. /* Enable interrupts for Axi DMA Rx */
  1355. ret = request_irq(lp->rx_irq, axienet_rx_irq, IRQF_SHARED,
  1356. ndev->name, ndev);
  1357. if (ret)
  1358. goto err_rx_irq;
  1359. /* Enable interrupts for Axi Ethernet core (if defined) */
  1360. if (lp->eth_irq > 0) {
  1361. ret = request_irq(lp->eth_irq, axienet_eth_irq, IRQF_SHARED,
  1362. ndev->name, ndev);
  1363. if (ret)
  1364. goto err_eth_irq;
  1365. }
  1366. return 0;
  1367. err_eth_irq:
  1368. free_irq(lp->rx_irq, ndev);
  1369. err_rx_irq:
  1370. free_irq(lp->tx_irq, ndev);
  1371. err_tx_irq:
  1372. napi_disable(&lp->napi_tx);
  1373. napi_disable(&lp->napi_rx);
  1374. cancel_work_sync(&lp->dma_err_task);
  1375. dev_err(lp->dev, "request_irq() failed\n");
  1376. return ret;
  1377. }
  1378. /**
  1379. * axienet_open - Driver open routine.
  1380. * @ndev: Pointer to net_device structure
  1381. *
  1382. * Return: 0, on success.
  1383. * non-zero error value on failure
  1384. *
  1385. * This is the driver open routine. It calls phylink_start to start the
  1386. * PHY device.
  1387. * It also allocates interrupt service routines, enables the interrupt lines
  1388. * and ISR handling. Axi Ethernet core is reset through Axi DMA core. Buffer
  1389. * descriptors are initialized.
  1390. */
  1391. static int axienet_open(struct net_device *ndev)
  1392. {
  1393. int ret;
  1394. struct axienet_local *lp = netdev_priv(ndev);
  1395. /* When we do an Axi Ethernet reset, it resets the complete core
  1396. * including the MDIO. MDIO must be disabled before resetting.
  1397. * Hold MDIO bus lock to avoid MDIO accesses during the reset.
  1398. */
  1399. axienet_lock_mii(lp);
  1400. ret = axienet_device_reset(ndev);
  1401. axienet_unlock_mii(lp);
  1402. ret = phylink_of_phy_connect(lp->phylink, lp->dev->of_node, 0);
  1403. if (ret) {
  1404. dev_err(lp->dev, "phylink_of_phy_connect() failed: %d\n", ret);
  1405. return ret;
  1406. }
  1407. phylink_start(lp->phylink);
  1408. /* Start the statistics refresh work */
  1409. schedule_delayed_work(&lp->stats_work, 0);
  1410. if (lp->use_dmaengine) {
  1411. /* Enable interrupts for Axi Ethernet core (if defined) */
  1412. if (lp->eth_irq > 0) {
  1413. ret = request_irq(lp->eth_irq, axienet_eth_irq, IRQF_SHARED,
  1414. ndev->name, ndev);
  1415. if (ret)
  1416. goto err_phy;
  1417. }
  1418. ret = axienet_init_dmaengine(ndev);
  1419. if (ret < 0)
  1420. goto err_free_eth_irq;
  1421. } else {
  1422. ret = axienet_init_legacy_dma(ndev);
  1423. if (ret)
  1424. goto err_phy;
  1425. }
  1426. return 0;
  1427. err_free_eth_irq:
  1428. if (lp->eth_irq > 0)
  1429. free_irq(lp->eth_irq, ndev);
  1430. err_phy:
  1431. cancel_delayed_work_sync(&lp->stats_work);
  1432. phylink_stop(lp->phylink);
  1433. phylink_disconnect_phy(lp->phylink);
  1434. return ret;
  1435. }
  1436. /**
  1437. * axienet_stop - Driver stop routine.
  1438. * @ndev: Pointer to net_device structure
  1439. *
  1440. * Return: 0, on success.
  1441. *
  1442. * This is the driver stop routine. It calls phylink_disconnect to stop the PHY
  1443. * device. It also removes the interrupt handlers and disables the interrupts.
  1444. * The Axi DMA Tx/Rx BDs are released.
  1445. */
  1446. static int axienet_stop(struct net_device *ndev)
  1447. {
  1448. struct axienet_local *lp = netdev_priv(ndev);
  1449. int i;
  1450. if (!lp->use_dmaengine) {
  1451. WRITE_ONCE(lp->stopping, true);
  1452. flush_work(&lp->dma_err_task);
  1453. napi_disable(&lp->napi_tx);
  1454. napi_disable(&lp->napi_rx);
  1455. }
  1456. cancel_delayed_work_sync(&lp->stats_work);
  1457. phylink_stop(lp->phylink);
  1458. phylink_disconnect_phy(lp->phylink);
  1459. axienet_setoptions(ndev, lp->options &
  1460. ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
  1461. if (!lp->use_dmaengine) {
  1462. axienet_dma_stop(lp);
  1463. cancel_work_sync(&lp->dma_err_task);
  1464. free_irq(lp->tx_irq, ndev);
  1465. free_irq(lp->rx_irq, ndev);
  1466. axienet_dma_bd_release(ndev);
  1467. } else {
  1468. dmaengine_terminate_sync(lp->tx_chan);
  1469. dmaengine_synchronize(lp->tx_chan);
  1470. dmaengine_terminate_sync(lp->rx_chan);
  1471. dmaengine_synchronize(lp->rx_chan);
  1472. for (i = 0; i < TX_BD_NUM_MAX; i++)
  1473. kfree(lp->tx_skb_ring[i]);
  1474. kfree(lp->tx_skb_ring);
  1475. for (i = 0; i < RX_BUF_NUM_DEFAULT; i++)
  1476. kfree(lp->rx_skb_ring[i]);
  1477. kfree(lp->rx_skb_ring);
  1478. dma_release_channel(lp->rx_chan);
  1479. dma_release_channel(lp->tx_chan);
  1480. }
  1481. axienet_iow(lp, XAE_IE_OFFSET, 0);
  1482. if (lp->eth_irq > 0)
  1483. free_irq(lp->eth_irq, ndev);
  1484. return 0;
  1485. }
  1486. /**
  1487. * axienet_change_mtu - Driver change mtu routine.
  1488. * @ndev: Pointer to net_device structure
  1489. * @new_mtu: New mtu value to be applied
  1490. *
  1491. * Return: Always returns 0 (success).
  1492. *
  1493. * This is the change mtu driver routine. It checks if the Axi Ethernet
  1494. * hardware supports jumbo frames before changing the mtu. This can be
  1495. * called only when the device is not up.
  1496. */
  1497. static int axienet_change_mtu(struct net_device *ndev, int new_mtu)
  1498. {
  1499. struct axienet_local *lp = netdev_priv(ndev);
  1500. if (netif_running(ndev))
  1501. return -EBUSY;
  1502. if ((new_mtu + VLAN_ETH_HLEN +
  1503. XAE_TRL_SIZE) > lp->rxmem)
  1504. return -EINVAL;
  1505. WRITE_ONCE(ndev->mtu, new_mtu);
  1506. return 0;
  1507. }
  1508. #ifdef CONFIG_NET_POLL_CONTROLLER
  1509. /**
  1510. * axienet_poll_controller - Axi Ethernet poll mechanism.
  1511. * @ndev: Pointer to net_device structure
  1512. *
  1513. * This implements Rx/Tx ISR poll mechanisms. The interrupts are disabled prior
  1514. * to polling the ISRs and are enabled back after the polling is done.
  1515. */
  1516. static void axienet_poll_controller(struct net_device *ndev)
  1517. {
  1518. struct axienet_local *lp = netdev_priv(ndev);
  1519. disable_irq(lp->tx_irq);
  1520. disable_irq(lp->rx_irq);
  1521. axienet_rx_irq(lp->tx_irq, ndev);
  1522. axienet_tx_irq(lp->rx_irq, ndev);
  1523. enable_irq(lp->tx_irq);
  1524. enable_irq(lp->rx_irq);
  1525. }
  1526. #endif
  1527. static int axienet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1528. {
  1529. struct axienet_local *lp = netdev_priv(dev);
  1530. if (!netif_running(dev))
  1531. return -EINVAL;
  1532. return phylink_mii_ioctl(lp->phylink, rq, cmd);
  1533. }
  1534. static void
  1535. axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1536. {
  1537. struct axienet_local *lp = netdev_priv(dev);
  1538. unsigned int start;
  1539. netdev_stats_to_stats64(stats, &dev->stats);
  1540. do {
  1541. start = u64_stats_fetch_begin(&lp->rx_stat_sync);
  1542. stats->rx_packets = u64_stats_read(&lp->rx_packets);
  1543. stats->rx_bytes = u64_stats_read(&lp->rx_bytes);
  1544. } while (u64_stats_fetch_retry(&lp->rx_stat_sync, start));
  1545. do {
  1546. start = u64_stats_fetch_begin(&lp->tx_stat_sync);
  1547. stats->tx_packets = u64_stats_read(&lp->tx_packets);
  1548. stats->tx_bytes = u64_stats_read(&lp->tx_bytes);
  1549. } while (u64_stats_fetch_retry(&lp->tx_stat_sync, start));
  1550. if (!(lp->features & XAE_FEATURE_STATS))
  1551. return;
  1552. do {
  1553. start = read_seqcount_begin(&lp->hw_stats_seqcount);
  1554. stats->rx_length_errors =
  1555. axienet_stat(lp, STAT_RX_LENGTH_ERRORS);
  1556. stats->rx_crc_errors = axienet_stat(lp, STAT_RX_FCS_ERRORS);
  1557. stats->rx_frame_errors =
  1558. axienet_stat(lp, STAT_RX_ALIGNMENT_ERRORS);
  1559. stats->rx_errors = axienet_stat(lp, STAT_UNDERSIZE_FRAMES) +
  1560. axienet_stat(lp, STAT_FRAGMENT_FRAMES) +
  1561. stats->rx_length_errors +
  1562. stats->rx_crc_errors +
  1563. stats->rx_frame_errors;
  1564. stats->multicast = axienet_stat(lp, STAT_RX_MULTICAST_FRAMES);
  1565. stats->tx_aborted_errors =
  1566. axienet_stat(lp, STAT_TX_EXCESS_COLLISIONS);
  1567. stats->tx_fifo_errors =
  1568. axienet_stat(lp, STAT_TX_UNDERRUN_ERRORS);
  1569. stats->tx_window_errors =
  1570. axienet_stat(lp, STAT_TX_LATE_COLLISIONS);
  1571. stats->tx_errors = axienet_stat(lp, STAT_TX_EXCESS_DEFERRAL) +
  1572. stats->tx_aborted_errors +
  1573. stats->tx_fifo_errors +
  1574. stats->tx_window_errors;
  1575. } while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
  1576. }
  1577. static const struct net_device_ops axienet_netdev_ops = {
  1578. .ndo_open = axienet_open,
  1579. .ndo_stop = axienet_stop,
  1580. .ndo_start_xmit = axienet_start_xmit,
  1581. .ndo_get_stats64 = axienet_get_stats64,
  1582. .ndo_change_mtu = axienet_change_mtu,
  1583. .ndo_set_mac_address = netdev_set_mac_address,
  1584. .ndo_validate_addr = eth_validate_addr,
  1585. .ndo_eth_ioctl = axienet_ioctl,
  1586. .ndo_set_rx_mode = axienet_set_multicast_list,
  1587. #ifdef CONFIG_NET_POLL_CONTROLLER
  1588. .ndo_poll_controller = axienet_poll_controller,
  1589. #endif
  1590. };
  1591. static const struct net_device_ops axienet_netdev_dmaengine_ops = {
  1592. .ndo_open = axienet_open,
  1593. .ndo_stop = axienet_stop,
  1594. .ndo_start_xmit = axienet_start_xmit_dmaengine,
  1595. .ndo_get_stats64 = axienet_get_stats64,
  1596. .ndo_change_mtu = axienet_change_mtu,
  1597. .ndo_set_mac_address = netdev_set_mac_address,
  1598. .ndo_validate_addr = eth_validate_addr,
  1599. .ndo_eth_ioctl = axienet_ioctl,
  1600. .ndo_set_rx_mode = axienet_set_multicast_list,
  1601. };
  1602. /**
  1603. * axienet_ethtools_get_drvinfo - Get various Axi Ethernet driver information.
  1604. * @ndev: Pointer to net_device structure
  1605. * @ed: Pointer to ethtool_drvinfo structure
  1606. *
  1607. * This implements ethtool command for getting the driver information.
  1608. * Issue "ethtool -i ethX" under linux prompt to execute this function.
  1609. */
  1610. static void axienet_ethtools_get_drvinfo(struct net_device *ndev,
  1611. struct ethtool_drvinfo *ed)
  1612. {
  1613. strscpy(ed->driver, DRIVER_NAME, sizeof(ed->driver));
  1614. strscpy(ed->version, DRIVER_VERSION, sizeof(ed->version));
  1615. }
  1616. /**
  1617. * axienet_ethtools_get_regs_len - Get the total regs length present in the
  1618. * AxiEthernet core.
  1619. * @ndev: Pointer to net_device structure
  1620. *
  1621. * This implements ethtool command for getting the total register length
  1622. * information.
  1623. *
  1624. * Return: the total regs length
  1625. */
  1626. static int axienet_ethtools_get_regs_len(struct net_device *ndev)
  1627. {
  1628. return sizeof(u32) * AXIENET_REGS_N;
  1629. }
  1630. /**
  1631. * axienet_ethtools_get_regs - Dump the contents of all registers present
  1632. * in AxiEthernet core.
  1633. * @ndev: Pointer to net_device structure
  1634. * @regs: Pointer to ethtool_regs structure
  1635. * @ret: Void pointer used to return the contents of the registers.
  1636. *
  1637. * This implements ethtool command for getting the Axi Ethernet register dump.
  1638. * Issue "ethtool -d ethX" to execute this function.
  1639. */
  1640. static void axienet_ethtools_get_regs(struct net_device *ndev,
  1641. struct ethtool_regs *regs, void *ret)
  1642. {
  1643. u32 *data = (u32 *)ret;
  1644. size_t len = sizeof(u32) * AXIENET_REGS_N;
  1645. struct axienet_local *lp = netdev_priv(ndev);
  1646. regs->version = 0;
  1647. regs->len = len;
  1648. memset(data, 0, len);
  1649. data[0] = axienet_ior(lp, XAE_RAF_OFFSET);
  1650. data[1] = axienet_ior(lp, XAE_TPF_OFFSET);
  1651. data[2] = axienet_ior(lp, XAE_IFGP_OFFSET);
  1652. data[3] = axienet_ior(lp, XAE_IS_OFFSET);
  1653. data[4] = axienet_ior(lp, XAE_IP_OFFSET);
  1654. data[5] = axienet_ior(lp, XAE_IE_OFFSET);
  1655. data[6] = axienet_ior(lp, XAE_TTAG_OFFSET);
  1656. data[7] = axienet_ior(lp, XAE_RTAG_OFFSET);
  1657. data[8] = axienet_ior(lp, XAE_UAWL_OFFSET);
  1658. data[9] = axienet_ior(lp, XAE_UAWU_OFFSET);
  1659. data[10] = axienet_ior(lp, XAE_TPID0_OFFSET);
  1660. data[11] = axienet_ior(lp, XAE_TPID1_OFFSET);
  1661. data[12] = axienet_ior(lp, XAE_PPST_OFFSET);
  1662. data[13] = axienet_ior(lp, XAE_RCW0_OFFSET);
  1663. data[14] = axienet_ior(lp, XAE_RCW1_OFFSET);
  1664. data[15] = axienet_ior(lp, XAE_TC_OFFSET);
  1665. data[16] = axienet_ior(lp, XAE_FCC_OFFSET);
  1666. data[17] = axienet_ior(lp, XAE_EMMC_OFFSET);
  1667. data[18] = axienet_ior(lp, XAE_PHYC_OFFSET);
  1668. data[19] = axienet_ior(lp, XAE_MDIO_MC_OFFSET);
  1669. data[20] = axienet_ior(lp, XAE_MDIO_MCR_OFFSET);
  1670. data[21] = axienet_ior(lp, XAE_MDIO_MWD_OFFSET);
  1671. data[22] = axienet_ior(lp, XAE_MDIO_MRD_OFFSET);
  1672. data[27] = axienet_ior(lp, XAE_UAW0_OFFSET);
  1673. data[28] = axienet_ior(lp, XAE_UAW1_OFFSET);
  1674. data[29] = axienet_ior(lp, XAE_FMI_OFFSET);
  1675. data[30] = axienet_ior(lp, XAE_AF0_OFFSET);
  1676. data[31] = axienet_ior(lp, XAE_AF1_OFFSET);
  1677. if (!lp->use_dmaengine) {
  1678. data[32] = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  1679. data[33] = axienet_dma_in32(lp, XAXIDMA_TX_SR_OFFSET);
  1680. data[34] = axienet_dma_in32(lp, XAXIDMA_TX_CDESC_OFFSET);
  1681. data[35] = axienet_dma_in32(lp, XAXIDMA_TX_TDESC_OFFSET);
  1682. data[36] = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  1683. data[37] = axienet_dma_in32(lp, XAXIDMA_RX_SR_OFFSET);
  1684. data[38] = axienet_dma_in32(lp, XAXIDMA_RX_CDESC_OFFSET);
  1685. data[39] = axienet_dma_in32(lp, XAXIDMA_RX_TDESC_OFFSET);
  1686. }
  1687. }
  1688. static void
  1689. axienet_ethtools_get_ringparam(struct net_device *ndev,
  1690. struct ethtool_ringparam *ering,
  1691. struct kernel_ethtool_ringparam *kernel_ering,
  1692. struct netlink_ext_ack *extack)
  1693. {
  1694. struct axienet_local *lp = netdev_priv(ndev);
  1695. ering->rx_max_pending = RX_BD_NUM_MAX;
  1696. ering->rx_mini_max_pending = 0;
  1697. ering->rx_jumbo_max_pending = 0;
  1698. ering->tx_max_pending = TX_BD_NUM_MAX;
  1699. ering->rx_pending = lp->rx_bd_num;
  1700. ering->rx_mini_pending = 0;
  1701. ering->rx_jumbo_pending = 0;
  1702. ering->tx_pending = lp->tx_bd_num;
  1703. }
  1704. static int
  1705. axienet_ethtools_set_ringparam(struct net_device *ndev,
  1706. struct ethtool_ringparam *ering,
  1707. struct kernel_ethtool_ringparam *kernel_ering,
  1708. struct netlink_ext_ack *extack)
  1709. {
  1710. struct axienet_local *lp = netdev_priv(ndev);
  1711. if (ering->rx_pending > RX_BD_NUM_MAX ||
  1712. ering->rx_mini_pending ||
  1713. ering->rx_jumbo_pending ||
  1714. ering->tx_pending < TX_BD_NUM_MIN ||
  1715. ering->tx_pending > TX_BD_NUM_MAX)
  1716. return -EINVAL;
  1717. if (netif_running(ndev))
  1718. return -EBUSY;
  1719. lp->rx_bd_num = ering->rx_pending;
  1720. lp->tx_bd_num = ering->tx_pending;
  1721. return 0;
  1722. }
  1723. /**
  1724. * axienet_ethtools_get_pauseparam - Get the pause parameter setting for
  1725. * Tx and Rx paths.
  1726. * @ndev: Pointer to net_device structure
  1727. * @epauseparm: Pointer to ethtool_pauseparam structure.
  1728. *
  1729. * This implements ethtool command for getting axi ethernet pause frame
  1730. * setting. Issue "ethtool -a ethX" to execute this function.
  1731. */
  1732. static void
  1733. axienet_ethtools_get_pauseparam(struct net_device *ndev,
  1734. struct ethtool_pauseparam *epauseparm)
  1735. {
  1736. struct axienet_local *lp = netdev_priv(ndev);
  1737. phylink_ethtool_get_pauseparam(lp->phylink, epauseparm);
  1738. }
  1739. /**
  1740. * axienet_ethtools_set_pauseparam - Set device pause parameter(flow control)
  1741. * settings.
  1742. * @ndev: Pointer to net_device structure
  1743. * @epauseparm:Pointer to ethtool_pauseparam structure
  1744. *
  1745. * This implements ethtool command for enabling flow control on Rx and Tx
  1746. * paths. Issue "ethtool -A ethX tx on|off" under linux prompt to execute this
  1747. * function.
  1748. *
  1749. * Return: 0 on success, -EFAULT if device is running
  1750. */
  1751. static int
  1752. axienet_ethtools_set_pauseparam(struct net_device *ndev,
  1753. struct ethtool_pauseparam *epauseparm)
  1754. {
  1755. struct axienet_local *lp = netdev_priv(ndev);
  1756. return phylink_ethtool_set_pauseparam(lp->phylink, epauseparm);
  1757. }
  1758. /**
  1759. * axienet_ethtools_get_coalesce - Get DMA interrupt coalescing count.
  1760. * @ndev: Pointer to net_device structure
  1761. * @ecoalesce: Pointer to ethtool_coalesce structure
  1762. * @kernel_coal: ethtool CQE mode setting structure
  1763. * @extack: extack for reporting error messages
  1764. *
  1765. * This implements ethtool command for getting the DMA interrupt coalescing
  1766. * count on Tx and Rx paths. Issue "ethtool -c ethX" under linux prompt to
  1767. * execute this function.
  1768. *
  1769. * Return: 0 always
  1770. */
  1771. static int
  1772. axienet_ethtools_get_coalesce(struct net_device *ndev,
  1773. struct ethtool_coalesce *ecoalesce,
  1774. struct kernel_ethtool_coalesce *kernel_coal,
  1775. struct netlink_ext_ack *extack)
  1776. {
  1777. struct axienet_local *lp = netdev_priv(ndev);
  1778. ecoalesce->rx_max_coalesced_frames = lp->coalesce_count_rx;
  1779. ecoalesce->rx_coalesce_usecs = lp->coalesce_usec_rx;
  1780. ecoalesce->tx_max_coalesced_frames = lp->coalesce_count_tx;
  1781. ecoalesce->tx_coalesce_usecs = lp->coalesce_usec_tx;
  1782. return 0;
  1783. }
  1784. /**
  1785. * axienet_ethtools_set_coalesce - Set DMA interrupt coalescing count.
  1786. * @ndev: Pointer to net_device structure
  1787. * @ecoalesce: Pointer to ethtool_coalesce structure
  1788. * @kernel_coal: ethtool CQE mode setting structure
  1789. * @extack: extack for reporting error messages
  1790. *
  1791. * This implements ethtool command for setting the DMA interrupt coalescing
  1792. * count on Tx and Rx paths. Issue "ethtool -C ethX rx-frames 5" under linux
  1793. * prompt to execute this function.
  1794. *
  1795. * Return: 0, on success, Non-zero error value on failure.
  1796. */
  1797. static int
  1798. axienet_ethtools_set_coalesce(struct net_device *ndev,
  1799. struct ethtool_coalesce *ecoalesce,
  1800. struct kernel_ethtool_coalesce *kernel_coal,
  1801. struct netlink_ext_ack *extack)
  1802. {
  1803. struct axienet_local *lp = netdev_priv(ndev);
  1804. if (netif_running(ndev)) {
  1805. NL_SET_ERR_MSG(extack,
  1806. "Please stop netif before applying configuration");
  1807. return -EBUSY;
  1808. }
  1809. if (ecoalesce->rx_max_coalesced_frames > 255 ||
  1810. ecoalesce->tx_max_coalesced_frames > 255) {
  1811. NL_SET_ERR_MSG(extack, "frames must be less than 256");
  1812. return -EINVAL;
  1813. }
  1814. if (ecoalesce->rx_max_coalesced_frames)
  1815. lp->coalesce_count_rx = ecoalesce->rx_max_coalesced_frames;
  1816. if (ecoalesce->rx_coalesce_usecs)
  1817. lp->coalesce_usec_rx = ecoalesce->rx_coalesce_usecs;
  1818. if (ecoalesce->tx_max_coalesced_frames)
  1819. lp->coalesce_count_tx = ecoalesce->tx_max_coalesced_frames;
  1820. if (ecoalesce->tx_coalesce_usecs)
  1821. lp->coalesce_usec_tx = ecoalesce->tx_coalesce_usecs;
  1822. return 0;
  1823. }
  1824. static int
  1825. axienet_ethtools_get_link_ksettings(struct net_device *ndev,
  1826. struct ethtool_link_ksettings *cmd)
  1827. {
  1828. struct axienet_local *lp = netdev_priv(ndev);
  1829. return phylink_ethtool_ksettings_get(lp->phylink, cmd);
  1830. }
  1831. static int
  1832. axienet_ethtools_set_link_ksettings(struct net_device *ndev,
  1833. const struct ethtool_link_ksettings *cmd)
  1834. {
  1835. struct axienet_local *lp = netdev_priv(ndev);
  1836. return phylink_ethtool_ksettings_set(lp->phylink, cmd);
  1837. }
  1838. static int axienet_ethtools_nway_reset(struct net_device *dev)
  1839. {
  1840. struct axienet_local *lp = netdev_priv(dev);
  1841. return phylink_ethtool_nway_reset(lp->phylink);
  1842. }
  1843. static void axienet_ethtools_get_ethtool_stats(struct net_device *dev,
  1844. struct ethtool_stats *stats,
  1845. u64 *data)
  1846. {
  1847. struct axienet_local *lp = netdev_priv(dev);
  1848. unsigned int start;
  1849. do {
  1850. start = read_seqcount_begin(&lp->hw_stats_seqcount);
  1851. data[0] = axienet_stat(lp, STAT_RX_BYTES);
  1852. data[1] = axienet_stat(lp, STAT_TX_BYTES);
  1853. data[2] = axienet_stat(lp, STAT_RX_VLAN_FRAMES);
  1854. data[3] = axienet_stat(lp, STAT_TX_VLAN_FRAMES);
  1855. data[6] = axienet_stat(lp, STAT_TX_PFC_FRAMES);
  1856. data[7] = axienet_stat(lp, STAT_RX_PFC_FRAMES);
  1857. data[8] = axienet_stat(lp, STAT_USER_DEFINED0);
  1858. data[9] = axienet_stat(lp, STAT_USER_DEFINED1);
  1859. data[10] = axienet_stat(lp, STAT_USER_DEFINED2);
  1860. } while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
  1861. }
  1862. static const char axienet_ethtool_stats_strings[][ETH_GSTRING_LEN] = {
  1863. "Received bytes",
  1864. "Transmitted bytes",
  1865. "RX Good VLAN Tagged Frames",
  1866. "TX Good VLAN Tagged Frames",
  1867. "TX Good PFC Frames",
  1868. "RX Good PFC Frames",
  1869. "User Defined Counter 0",
  1870. "User Defined Counter 1",
  1871. "User Defined Counter 2",
  1872. };
  1873. static void axienet_ethtools_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1874. {
  1875. switch (stringset) {
  1876. case ETH_SS_STATS:
  1877. memcpy(data, axienet_ethtool_stats_strings,
  1878. sizeof(axienet_ethtool_stats_strings));
  1879. break;
  1880. }
  1881. }
  1882. static int axienet_ethtools_get_sset_count(struct net_device *dev, int sset)
  1883. {
  1884. struct axienet_local *lp = netdev_priv(dev);
  1885. switch (sset) {
  1886. case ETH_SS_STATS:
  1887. if (lp->features & XAE_FEATURE_STATS)
  1888. return ARRAY_SIZE(axienet_ethtool_stats_strings);
  1889. fallthrough;
  1890. default:
  1891. return -EOPNOTSUPP;
  1892. }
  1893. }
  1894. static void
  1895. axienet_ethtools_get_pause_stats(struct net_device *dev,
  1896. struct ethtool_pause_stats *pause_stats)
  1897. {
  1898. struct axienet_local *lp = netdev_priv(dev);
  1899. unsigned int start;
  1900. if (!(lp->features & XAE_FEATURE_STATS))
  1901. return;
  1902. do {
  1903. start = read_seqcount_begin(&lp->hw_stats_seqcount);
  1904. pause_stats->tx_pause_frames =
  1905. axienet_stat(lp, STAT_TX_PAUSE_FRAMES);
  1906. pause_stats->rx_pause_frames =
  1907. axienet_stat(lp, STAT_RX_PAUSE_FRAMES);
  1908. } while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
  1909. }
  1910. static void
  1911. axienet_ethtool_get_eth_mac_stats(struct net_device *dev,
  1912. struct ethtool_eth_mac_stats *mac_stats)
  1913. {
  1914. struct axienet_local *lp = netdev_priv(dev);
  1915. unsigned int start;
  1916. if (!(lp->features & XAE_FEATURE_STATS))
  1917. return;
  1918. do {
  1919. start = read_seqcount_begin(&lp->hw_stats_seqcount);
  1920. mac_stats->FramesTransmittedOK =
  1921. axienet_stat(lp, STAT_TX_GOOD_FRAMES);
  1922. mac_stats->SingleCollisionFrames =
  1923. axienet_stat(lp, STAT_TX_SINGLE_COLLISION_FRAMES);
  1924. mac_stats->MultipleCollisionFrames =
  1925. axienet_stat(lp, STAT_TX_MULTIPLE_COLLISION_FRAMES);
  1926. mac_stats->FramesReceivedOK =
  1927. axienet_stat(lp, STAT_RX_GOOD_FRAMES);
  1928. mac_stats->FrameCheckSequenceErrors =
  1929. axienet_stat(lp, STAT_RX_FCS_ERRORS);
  1930. mac_stats->AlignmentErrors =
  1931. axienet_stat(lp, STAT_RX_ALIGNMENT_ERRORS);
  1932. mac_stats->FramesWithDeferredXmissions =
  1933. axienet_stat(lp, STAT_TX_DEFERRED_FRAMES);
  1934. mac_stats->LateCollisions =
  1935. axienet_stat(lp, STAT_TX_LATE_COLLISIONS);
  1936. mac_stats->FramesAbortedDueToXSColls =
  1937. axienet_stat(lp, STAT_TX_EXCESS_COLLISIONS);
  1938. mac_stats->MulticastFramesXmittedOK =
  1939. axienet_stat(lp, STAT_TX_MULTICAST_FRAMES);
  1940. mac_stats->BroadcastFramesXmittedOK =
  1941. axienet_stat(lp, STAT_TX_BROADCAST_FRAMES);
  1942. mac_stats->FramesWithExcessiveDeferral =
  1943. axienet_stat(lp, STAT_TX_EXCESS_DEFERRAL);
  1944. mac_stats->MulticastFramesReceivedOK =
  1945. axienet_stat(lp, STAT_RX_MULTICAST_FRAMES);
  1946. mac_stats->BroadcastFramesReceivedOK =
  1947. axienet_stat(lp, STAT_RX_BROADCAST_FRAMES);
  1948. mac_stats->InRangeLengthErrors =
  1949. axienet_stat(lp, STAT_RX_LENGTH_ERRORS);
  1950. } while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
  1951. }
  1952. static void
  1953. axienet_ethtool_get_eth_ctrl_stats(struct net_device *dev,
  1954. struct ethtool_eth_ctrl_stats *ctrl_stats)
  1955. {
  1956. struct axienet_local *lp = netdev_priv(dev);
  1957. unsigned int start;
  1958. if (!(lp->features & XAE_FEATURE_STATS))
  1959. return;
  1960. do {
  1961. start = read_seqcount_begin(&lp->hw_stats_seqcount);
  1962. ctrl_stats->MACControlFramesTransmitted =
  1963. axienet_stat(lp, STAT_TX_CONTROL_FRAMES);
  1964. ctrl_stats->MACControlFramesReceived =
  1965. axienet_stat(lp, STAT_RX_CONTROL_FRAMES);
  1966. ctrl_stats->UnsupportedOpcodesReceived =
  1967. axienet_stat(lp, STAT_RX_CONTROL_OPCODE_ERRORS);
  1968. } while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
  1969. }
  1970. static const struct ethtool_rmon_hist_range axienet_rmon_ranges[] = {
  1971. { 64, 64 },
  1972. { 65, 127 },
  1973. { 128, 255 },
  1974. { 256, 511 },
  1975. { 512, 1023 },
  1976. { 1024, 1518 },
  1977. { 1519, 16384 },
  1978. { },
  1979. };
  1980. static void
  1981. axienet_ethtool_get_rmon_stats(struct net_device *dev,
  1982. struct ethtool_rmon_stats *rmon_stats,
  1983. const struct ethtool_rmon_hist_range **ranges)
  1984. {
  1985. struct axienet_local *lp = netdev_priv(dev);
  1986. unsigned int start;
  1987. if (!(lp->features & XAE_FEATURE_STATS))
  1988. return;
  1989. do {
  1990. start = read_seqcount_begin(&lp->hw_stats_seqcount);
  1991. rmon_stats->undersize_pkts =
  1992. axienet_stat(lp, STAT_UNDERSIZE_FRAMES);
  1993. rmon_stats->oversize_pkts =
  1994. axienet_stat(lp, STAT_RX_OVERSIZE_FRAMES);
  1995. rmon_stats->fragments =
  1996. axienet_stat(lp, STAT_FRAGMENT_FRAMES);
  1997. rmon_stats->hist[0] =
  1998. axienet_stat(lp, STAT_RX_64_BYTE_FRAMES);
  1999. rmon_stats->hist[1] =
  2000. axienet_stat(lp, STAT_RX_65_127_BYTE_FRAMES);
  2001. rmon_stats->hist[2] =
  2002. axienet_stat(lp, STAT_RX_128_255_BYTE_FRAMES);
  2003. rmon_stats->hist[3] =
  2004. axienet_stat(lp, STAT_RX_256_511_BYTE_FRAMES);
  2005. rmon_stats->hist[4] =
  2006. axienet_stat(lp, STAT_RX_512_1023_BYTE_FRAMES);
  2007. rmon_stats->hist[5] =
  2008. axienet_stat(lp, STAT_RX_1024_MAX_BYTE_FRAMES);
  2009. rmon_stats->hist[6] =
  2010. rmon_stats->oversize_pkts;
  2011. rmon_stats->hist_tx[0] =
  2012. axienet_stat(lp, STAT_TX_64_BYTE_FRAMES);
  2013. rmon_stats->hist_tx[1] =
  2014. axienet_stat(lp, STAT_TX_65_127_BYTE_FRAMES);
  2015. rmon_stats->hist_tx[2] =
  2016. axienet_stat(lp, STAT_TX_128_255_BYTE_FRAMES);
  2017. rmon_stats->hist_tx[3] =
  2018. axienet_stat(lp, STAT_TX_256_511_BYTE_FRAMES);
  2019. rmon_stats->hist_tx[4] =
  2020. axienet_stat(lp, STAT_TX_512_1023_BYTE_FRAMES);
  2021. rmon_stats->hist_tx[5] =
  2022. axienet_stat(lp, STAT_TX_1024_MAX_BYTE_FRAMES);
  2023. rmon_stats->hist_tx[6] =
  2024. axienet_stat(lp, STAT_TX_OVERSIZE_FRAMES);
  2025. } while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
  2026. *ranges = axienet_rmon_ranges;
  2027. }
  2028. static const struct ethtool_ops axienet_ethtool_ops = {
  2029. .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
  2030. ETHTOOL_COALESCE_USECS,
  2031. .get_drvinfo = axienet_ethtools_get_drvinfo,
  2032. .get_regs_len = axienet_ethtools_get_regs_len,
  2033. .get_regs = axienet_ethtools_get_regs,
  2034. .get_link = ethtool_op_get_link,
  2035. .get_ringparam = axienet_ethtools_get_ringparam,
  2036. .set_ringparam = axienet_ethtools_set_ringparam,
  2037. .get_pauseparam = axienet_ethtools_get_pauseparam,
  2038. .set_pauseparam = axienet_ethtools_set_pauseparam,
  2039. .get_coalesce = axienet_ethtools_get_coalesce,
  2040. .set_coalesce = axienet_ethtools_set_coalesce,
  2041. .get_link_ksettings = axienet_ethtools_get_link_ksettings,
  2042. .set_link_ksettings = axienet_ethtools_set_link_ksettings,
  2043. .nway_reset = axienet_ethtools_nway_reset,
  2044. .get_ethtool_stats = axienet_ethtools_get_ethtool_stats,
  2045. .get_strings = axienet_ethtools_get_strings,
  2046. .get_sset_count = axienet_ethtools_get_sset_count,
  2047. .get_pause_stats = axienet_ethtools_get_pause_stats,
  2048. .get_eth_mac_stats = axienet_ethtool_get_eth_mac_stats,
  2049. .get_eth_ctrl_stats = axienet_ethtool_get_eth_ctrl_stats,
  2050. .get_rmon_stats = axienet_ethtool_get_rmon_stats,
  2051. };
  2052. static struct axienet_local *pcs_to_axienet_local(struct phylink_pcs *pcs)
  2053. {
  2054. return container_of(pcs, struct axienet_local, pcs);
  2055. }
  2056. static void axienet_pcs_get_state(struct phylink_pcs *pcs,
  2057. struct phylink_link_state *state)
  2058. {
  2059. struct mdio_device *pcs_phy = pcs_to_axienet_local(pcs)->pcs_phy;
  2060. phylink_mii_c22_pcs_get_state(pcs_phy, state);
  2061. }
  2062. static void axienet_pcs_an_restart(struct phylink_pcs *pcs)
  2063. {
  2064. struct mdio_device *pcs_phy = pcs_to_axienet_local(pcs)->pcs_phy;
  2065. phylink_mii_c22_pcs_an_restart(pcs_phy);
  2066. }
  2067. static int axienet_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
  2068. phy_interface_t interface,
  2069. const unsigned long *advertising,
  2070. bool permit_pause_to_mac)
  2071. {
  2072. struct mdio_device *pcs_phy = pcs_to_axienet_local(pcs)->pcs_phy;
  2073. struct net_device *ndev = pcs_to_axienet_local(pcs)->ndev;
  2074. struct axienet_local *lp = netdev_priv(ndev);
  2075. int ret;
  2076. if (lp->switch_x_sgmii) {
  2077. ret = mdiodev_write(pcs_phy, XLNX_MII_STD_SELECT_REG,
  2078. interface == PHY_INTERFACE_MODE_SGMII ?
  2079. XLNX_MII_STD_SELECT_SGMII : 0);
  2080. if (ret < 0) {
  2081. netdev_warn(ndev,
  2082. "Failed to switch PHY interface: %d\n",
  2083. ret);
  2084. return ret;
  2085. }
  2086. }
  2087. ret = phylink_mii_c22_pcs_config(pcs_phy, interface, advertising,
  2088. neg_mode);
  2089. if (ret < 0)
  2090. netdev_warn(ndev, "Failed to configure PCS: %d\n", ret);
  2091. return ret;
  2092. }
  2093. static const struct phylink_pcs_ops axienet_pcs_ops = {
  2094. .pcs_get_state = axienet_pcs_get_state,
  2095. .pcs_config = axienet_pcs_config,
  2096. .pcs_an_restart = axienet_pcs_an_restart,
  2097. };
  2098. static struct phylink_pcs *axienet_mac_select_pcs(struct phylink_config *config,
  2099. phy_interface_t interface)
  2100. {
  2101. struct net_device *ndev = to_net_dev(config->dev);
  2102. struct axienet_local *lp = netdev_priv(ndev);
  2103. if (interface == PHY_INTERFACE_MODE_1000BASEX ||
  2104. interface == PHY_INTERFACE_MODE_SGMII)
  2105. return &lp->pcs;
  2106. return NULL;
  2107. }
  2108. static void axienet_mac_config(struct phylink_config *config, unsigned int mode,
  2109. const struct phylink_link_state *state)
  2110. {
  2111. /* nothing meaningful to do */
  2112. }
  2113. static void axienet_mac_link_down(struct phylink_config *config,
  2114. unsigned int mode,
  2115. phy_interface_t interface)
  2116. {
  2117. /* nothing meaningful to do */
  2118. }
  2119. static void axienet_mac_link_up(struct phylink_config *config,
  2120. struct phy_device *phy,
  2121. unsigned int mode, phy_interface_t interface,
  2122. int speed, int duplex,
  2123. bool tx_pause, bool rx_pause)
  2124. {
  2125. struct net_device *ndev = to_net_dev(config->dev);
  2126. struct axienet_local *lp = netdev_priv(ndev);
  2127. u32 emmc_reg, fcc_reg;
  2128. emmc_reg = axienet_ior(lp, XAE_EMMC_OFFSET);
  2129. emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
  2130. switch (speed) {
  2131. case SPEED_1000:
  2132. emmc_reg |= XAE_EMMC_LINKSPD_1000;
  2133. break;
  2134. case SPEED_100:
  2135. emmc_reg |= XAE_EMMC_LINKSPD_100;
  2136. break;
  2137. case SPEED_10:
  2138. emmc_reg |= XAE_EMMC_LINKSPD_10;
  2139. break;
  2140. default:
  2141. dev_err(&ndev->dev,
  2142. "Speed other than 10, 100 or 1Gbps is not supported\n");
  2143. break;
  2144. }
  2145. axienet_iow(lp, XAE_EMMC_OFFSET, emmc_reg);
  2146. fcc_reg = axienet_ior(lp, XAE_FCC_OFFSET);
  2147. if (tx_pause)
  2148. fcc_reg |= XAE_FCC_FCTX_MASK;
  2149. else
  2150. fcc_reg &= ~XAE_FCC_FCTX_MASK;
  2151. if (rx_pause)
  2152. fcc_reg |= XAE_FCC_FCRX_MASK;
  2153. else
  2154. fcc_reg &= ~XAE_FCC_FCRX_MASK;
  2155. axienet_iow(lp, XAE_FCC_OFFSET, fcc_reg);
  2156. }
  2157. static const struct phylink_mac_ops axienet_phylink_ops = {
  2158. .mac_select_pcs = axienet_mac_select_pcs,
  2159. .mac_config = axienet_mac_config,
  2160. .mac_link_down = axienet_mac_link_down,
  2161. .mac_link_up = axienet_mac_link_up,
  2162. };
  2163. /**
  2164. * axienet_dma_err_handler - Work queue task for Axi DMA Error
  2165. * @work: pointer to work_struct
  2166. *
  2167. * Resets the Axi DMA and Axi Ethernet devices, and reconfigures the
  2168. * Tx/Rx BDs.
  2169. */
  2170. static void axienet_dma_err_handler(struct work_struct *work)
  2171. {
  2172. u32 i;
  2173. u32 axienet_status;
  2174. struct axidma_bd *cur_p;
  2175. struct axienet_local *lp = container_of(work, struct axienet_local,
  2176. dma_err_task);
  2177. struct net_device *ndev = lp->ndev;
  2178. /* Don't bother if we are going to stop anyway */
  2179. if (READ_ONCE(lp->stopping))
  2180. return;
  2181. napi_disable(&lp->napi_tx);
  2182. napi_disable(&lp->napi_rx);
  2183. axienet_setoptions(ndev, lp->options &
  2184. ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
  2185. axienet_dma_stop(lp);
  2186. for (i = 0; i < lp->tx_bd_num; i++) {
  2187. cur_p = &lp->tx_bd_v[i];
  2188. if (cur_p->cntrl) {
  2189. dma_addr_t addr = desc_get_phys_addr(lp, cur_p);
  2190. dma_unmap_single(lp->dev, addr,
  2191. (cur_p->cntrl &
  2192. XAXIDMA_BD_CTRL_LENGTH_MASK),
  2193. DMA_TO_DEVICE);
  2194. }
  2195. if (cur_p->skb)
  2196. dev_kfree_skb_irq(cur_p->skb);
  2197. cur_p->phys = 0;
  2198. cur_p->phys_msb = 0;
  2199. cur_p->cntrl = 0;
  2200. cur_p->status = 0;
  2201. cur_p->app0 = 0;
  2202. cur_p->app1 = 0;
  2203. cur_p->app2 = 0;
  2204. cur_p->app3 = 0;
  2205. cur_p->app4 = 0;
  2206. cur_p->skb = NULL;
  2207. }
  2208. for (i = 0; i < lp->rx_bd_num; i++) {
  2209. cur_p = &lp->rx_bd_v[i];
  2210. cur_p->status = 0;
  2211. cur_p->app0 = 0;
  2212. cur_p->app1 = 0;
  2213. cur_p->app2 = 0;
  2214. cur_p->app3 = 0;
  2215. cur_p->app4 = 0;
  2216. }
  2217. lp->tx_bd_ci = 0;
  2218. lp->tx_bd_tail = 0;
  2219. lp->rx_bd_ci = 0;
  2220. axienet_dma_start(lp);
  2221. axienet_status = axienet_ior(lp, XAE_RCW1_OFFSET);
  2222. axienet_status &= ~XAE_RCW1_RX_MASK;
  2223. axienet_iow(lp, XAE_RCW1_OFFSET, axienet_status);
  2224. axienet_status = axienet_ior(lp, XAE_IP_OFFSET);
  2225. if (axienet_status & XAE_INT_RXRJECT_MASK)
  2226. axienet_iow(lp, XAE_IS_OFFSET, XAE_INT_RXRJECT_MASK);
  2227. axienet_iow(lp, XAE_IE_OFFSET, lp->eth_irq > 0 ?
  2228. XAE_INT_RECV_ERROR_MASK : 0);
  2229. axienet_iow(lp, XAE_FCC_OFFSET, XAE_FCC_FCRX_MASK);
  2230. /* Sync default options with HW but leave receiver and
  2231. * transmitter disabled.
  2232. */
  2233. axienet_setoptions(ndev, lp->options &
  2234. ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
  2235. axienet_set_mac_address(ndev, NULL);
  2236. axienet_set_multicast_list(ndev);
  2237. napi_enable(&lp->napi_rx);
  2238. napi_enable(&lp->napi_tx);
  2239. axienet_setoptions(ndev, lp->options);
  2240. }
  2241. /**
  2242. * axienet_probe - Axi Ethernet probe function.
  2243. * @pdev: Pointer to platform device structure.
  2244. *
  2245. * Return: 0, on success
  2246. * Non-zero error value on failure.
  2247. *
  2248. * This is the probe routine for Axi Ethernet driver. This is called before
  2249. * any other driver routines are invoked. It allocates and sets up the Ethernet
  2250. * device. Parses through device tree and populates fields of
  2251. * axienet_local. It registers the Ethernet device.
  2252. */
  2253. static int axienet_probe(struct platform_device *pdev)
  2254. {
  2255. int ret;
  2256. struct device_node *np;
  2257. struct axienet_local *lp;
  2258. struct net_device *ndev;
  2259. struct resource *ethres;
  2260. u8 mac_addr[ETH_ALEN];
  2261. int addr_width = 32;
  2262. u32 value;
  2263. ndev = alloc_etherdev(sizeof(*lp));
  2264. if (!ndev)
  2265. return -ENOMEM;
  2266. platform_set_drvdata(pdev, ndev);
  2267. SET_NETDEV_DEV(ndev, &pdev->dev);
  2268. ndev->features = NETIF_F_SG;
  2269. ndev->ethtool_ops = &axienet_ethtool_ops;
  2270. /* MTU range: 64 - 9000 */
  2271. ndev->min_mtu = 64;
  2272. ndev->max_mtu = XAE_JUMBO_MTU;
  2273. lp = netdev_priv(ndev);
  2274. lp->ndev = ndev;
  2275. lp->dev = &pdev->dev;
  2276. lp->options = XAE_OPTION_DEFAULTS;
  2277. lp->rx_bd_num = RX_BD_NUM_DEFAULT;
  2278. lp->tx_bd_num = TX_BD_NUM_DEFAULT;
  2279. u64_stats_init(&lp->rx_stat_sync);
  2280. u64_stats_init(&lp->tx_stat_sync);
  2281. mutex_init(&lp->stats_lock);
  2282. seqcount_mutex_init(&lp->hw_stats_seqcount, &lp->stats_lock);
  2283. INIT_DEFERRABLE_WORK(&lp->stats_work, axienet_refresh_stats);
  2284. lp->axi_clk = devm_clk_get_optional(&pdev->dev, "s_axi_lite_clk");
  2285. if (!lp->axi_clk) {
  2286. /* For backward compatibility, if named AXI clock is not present,
  2287. * treat the first clock specified as the AXI clock.
  2288. */
  2289. lp->axi_clk = devm_clk_get_optional(&pdev->dev, NULL);
  2290. }
  2291. if (IS_ERR(lp->axi_clk)) {
  2292. ret = PTR_ERR(lp->axi_clk);
  2293. goto free_netdev;
  2294. }
  2295. ret = clk_prepare_enable(lp->axi_clk);
  2296. if (ret) {
  2297. dev_err(&pdev->dev, "Unable to enable AXI clock: %d\n", ret);
  2298. goto free_netdev;
  2299. }
  2300. lp->misc_clks[0].id = "axis_clk";
  2301. lp->misc_clks[1].id = "ref_clk";
  2302. lp->misc_clks[2].id = "mgt_clk";
  2303. ret = devm_clk_bulk_get_optional(&pdev->dev, XAE_NUM_MISC_CLOCKS, lp->misc_clks);
  2304. if (ret)
  2305. goto cleanup_clk;
  2306. ret = clk_bulk_prepare_enable(XAE_NUM_MISC_CLOCKS, lp->misc_clks);
  2307. if (ret)
  2308. goto cleanup_clk;
  2309. /* Map device registers */
  2310. lp->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &ethres);
  2311. if (IS_ERR(lp->regs)) {
  2312. ret = PTR_ERR(lp->regs);
  2313. goto cleanup_clk;
  2314. }
  2315. lp->regs_start = ethres->start;
  2316. /* Setup checksum offload, but default to off if not specified */
  2317. lp->features = 0;
  2318. if (axienet_ior(lp, XAE_ABILITY_OFFSET) & XAE_ABILITY_STATS)
  2319. lp->features |= XAE_FEATURE_STATS;
  2320. ret = of_property_read_u32(pdev->dev.of_node, "xlnx,txcsum", &value);
  2321. if (!ret) {
  2322. switch (value) {
  2323. case 1:
  2324. lp->features |= XAE_FEATURE_PARTIAL_TX_CSUM;
  2325. /* Can checksum any contiguous range */
  2326. ndev->features |= NETIF_F_HW_CSUM;
  2327. break;
  2328. case 2:
  2329. lp->features |= XAE_FEATURE_FULL_TX_CSUM;
  2330. /* Can checksum TCP/UDP over IPv4. */
  2331. ndev->features |= NETIF_F_IP_CSUM;
  2332. break;
  2333. }
  2334. }
  2335. ret = of_property_read_u32(pdev->dev.of_node, "xlnx,rxcsum", &value);
  2336. if (!ret) {
  2337. switch (value) {
  2338. case 1:
  2339. lp->features |= XAE_FEATURE_PARTIAL_RX_CSUM;
  2340. ndev->features |= NETIF_F_RXCSUM;
  2341. break;
  2342. case 2:
  2343. lp->features |= XAE_FEATURE_FULL_RX_CSUM;
  2344. ndev->features |= NETIF_F_RXCSUM;
  2345. break;
  2346. }
  2347. }
  2348. /* For supporting jumbo frames, the Axi Ethernet hardware must have
  2349. * a larger Rx/Tx Memory. Typically, the size must be large so that
  2350. * we can enable jumbo option and start supporting jumbo frames.
  2351. * Here we check for memory allocated for Rx/Tx in the hardware from
  2352. * the device-tree and accordingly set flags.
  2353. */
  2354. of_property_read_u32(pdev->dev.of_node, "xlnx,rxmem", &lp->rxmem);
  2355. lp->switch_x_sgmii = of_property_read_bool(pdev->dev.of_node,
  2356. "xlnx,switch-x-sgmii");
  2357. /* Start with the proprietary, and broken phy_type */
  2358. ret = of_property_read_u32(pdev->dev.of_node, "xlnx,phy-type", &value);
  2359. if (!ret) {
  2360. netdev_warn(ndev, "Please upgrade your device tree binary blob to use phy-mode");
  2361. switch (value) {
  2362. case XAE_PHY_TYPE_MII:
  2363. lp->phy_mode = PHY_INTERFACE_MODE_MII;
  2364. break;
  2365. case XAE_PHY_TYPE_GMII:
  2366. lp->phy_mode = PHY_INTERFACE_MODE_GMII;
  2367. break;
  2368. case XAE_PHY_TYPE_RGMII_2_0:
  2369. lp->phy_mode = PHY_INTERFACE_MODE_RGMII_ID;
  2370. break;
  2371. case XAE_PHY_TYPE_SGMII:
  2372. lp->phy_mode = PHY_INTERFACE_MODE_SGMII;
  2373. break;
  2374. case XAE_PHY_TYPE_1000BASE_X:
  2375. lp->phy_mode = PHY_INTERFACE_MODE_1000BASEX;
  2376. break;
  2377. default:
  2378. ret = -EINVAL;
  2379. goto cleanup_clk;
  2380. }
  2381. } else {
  2382. ret = of_get_phy_mode(pdev->dev.of_node, &lp->phy_mode);
  2383. if (ret)
  2384. goto cleanup_clk;
  2385. }
  2386. if (lp->switch_x_sgmii && lp->phy_mode != PHY_INTERFACE_MODE_SGMII &&
  2387. lp->phy_mode != PHY_INTERFACE_MODE_1000BASEX) {
  2388. dev_err(&pdev->dev, "xlnx,switch-x-sgmii only supported with SGMII or 1000BaseX\n");
  2389. ret = -EINVAL;
  2390. goto cleanup_clk;
  2391. }
  2392. if (!of_property_present(pdev->dev.of_node, "dmas")) {
  2393. /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
  2394. np = of_parse_phandle(pdev->dev.of_node, "axistream-connected", 0);
  2395. if (np) {
  2396. struct resource dmares;
  2397. ret = of_address_to_resource(np, 0, &dmares);
  2398. if (ret) {
  2399. dev_err(&pdev->dev,
  2400. "unable to get DMA resource\n");
  2401. of_node_put(np);
  2402. goto cleanup_clk;
  2403. }
  2404. lp->dma_regs = devm_ioremap_resource(&pdev->dev,
  2405. &dmares);
  2406. lp->rx_irq = irq_of_parse_and_map(np, 1);
  2407. lp->tx_irq = irq_of_parse_and_map(np, 0);
  2408. of_node_put(np);
  2409. lp->eth_irq = platform_get_irq_optional(pdev, 0);
  2410. } else {
  2411. /* Check for these resources directly on the Ethernet node. */
  2412. lp->dma_regs = devm_platform_get_and_ioremap_resource(pdev, 1, NULL);
  2413. lp->rx_irq = platform_get_irq(pdev, 1);
  2414. lp->tx_irq = platform_get_irq(pdev, 0);
  2415. lp->eth_irq = platform_get_irq_optional(pdev, 2);
  2416. }
  2417. if (IS_ERR(lp->dma_regs)) {
  2418. dev_err(&pdev->dev, "could not map DMA regs\n");
  2419. ret = PTR_ERR(lp->dma_regs);
  2420. goto cleanup_clk;
  2421. }
  2422. if (lp->rx_irq <= 0 || lp->tx_irq <= 0) {
  2423. dev_err(&pdev->dev, "could not determine irqs\n");
  2424. ret = -ENOMEM;
  2425. goto cleanup_clk;
  2426. }
  2427. /* Reset core now that clocks are enabled, prior to accessing MDIO */
  2428. ret = __axienet_device_reset(lp);
  2429. if (ret)
  2430. goto cleanup_clk;
  2431. /* Autodetect the need for 64-bit DMA pointers.
  2432. * When the IP is configured for a bus width bigger than 32 bits,
  2433. * writing the MSB registers is mandatory, even if they are all 0.
  2434. * We can detect this case by writing all 1's to one such register
  2435. * and see if that sticks: when the IP is configured for 32 bits
  2436. * only, those registers are RES0.
  2437. * Those MSB registers were introduced in IP v7.1, which we check first.
  2438. */
  2439. if ((axienet_ior(lp, XAE_ID_OFFSET) >> 24) >= 0x9) {
  2440. void __iomem *desc = lp->dma_regs + XAXIDMA_TX_CDESC_OFFSET + 4;
  2441. iowrite32(0x0, desc);
  2442. if (ioread32(desc) == 0) { /* sanity check */
  2443. iowrite32(0xffffffff, desc);
  2444. if (ioread32(desc) > 0) {
  2445. lp->features |= XAE_FEATURE_DMA_64BIT;
  2446. addr_width = 64;
  2447. dev_info(&pdev->dev,
  2448. "autodetected 64-bit DMA range\n");
  2449. }
  2450. iowrite32(0x0, desc);
  2451. }
  2452. }
  2453. if (!IS_ENABLED(CONFIG_64BIT) && lp->features & XAE_FEATURE_DMA_64BIT) {
  2454. dev_err(&pdev->dev, "64-bit addressable DMA is not compatible with 32-bit archecture\n");
  2455. ret = -EINVAL;
  2456. goto cleanup_clk;
  2457. }
  2458. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(addr_width));
  2459. if (ret) {
  2460. dev_err(&pdev->dev, "No suitable DMA available\n");
  2461. goto cleanup_clk;
  2462. }
  2463. netif_napi_add(ndev, &lp->napi_rx, axienet_rx_poll);
  2464. netif_napi_add(ndev, &lp->napi_tx, axienet_tx_poll);
  2465. } else {
  2466. struct xilinx_vdma_config cfg;
  2467. struct dma_chan *tx_chan;
  2468. lp->eth_irq = platform_get_irq_optional(pdev, 0);
  2469. if (lp->eth_irq < 0 && lp->eth_irq != -ENXIO) {
  2470. ret = lp->eth_irq;
  2471. goto cleanup_clk;
  2472. }
  2473. tx_chan = dma_request_chan(lp->dev, "tx_chan0");
  2474. if (IS_ERR(tx_chan)) {
  2475. ret = PTR_ERR(tx_chan);
  2476. dev_err_probe(lp->dev, ret, "No Ethernet DMA (TX) channel found\n");
  2477. goto cleanup_clk;
  2478. }
  2479. cfg.reset = 1;
  2480. /* As name says VDMA but it has support for DMA channel reset */
  2481. ret = xilinx_vdma_channel_set_config(tx_chan, &cfg);
  2482. if (ret < 0) {
  2483. dev_err(&pdev->dev, "Reset channel failed\n");
  2484. dma_release_channel(tx_chan);
  2485. goto cleanup_clk;
  2486. }
  2487. dma_release_channel(tx_chan);
  2488. lp->use_dmaengine = 1;
  2489. }
  2490. if (lp->use_dmaengine)
  2491. ndev->netdev_ops = &axienet_netdev_dmaengine_ops;
  2492. else
  2493. ndev->netdev_ops = &axienet_netdev_ops;
  2494. /* Check for Ethernet core IRQ (optional) */
  2495. if (lp->eth_irq <= 0)
  2496. dev_info(&pdev->dev, "Ethernet core IRQ not defined\n");
  2497. /* Retrieve the MAC address */
  2498. ret = of_get_mac_address(pdev->dev.of_node, mac_addr);
  2499. if (!ret) {
  2500. axienet_set_mac_address(ndev, mac_addr);
  2501. } else {
  2502. dev_warn(&pdev->dev, "could not find MAC address property: %d\n",
  2503. ret);
  2504. axienet_set_mac_address(ndev, NULL);
  2505. }
  2506. lp->coalesce_count_rx = XAXIDMA_DFT_RX_THRESHOLD;
  2507. lp->coalesce_count_tx = XAXIDMA_DFT_TX_THRESHOLD;
  2508. lp->coalesce_usec_rx = XAXIDMA_DFT_RX_USEC;
  2509. lp->coalesce_usec_tx = XAXIDMA_DFT_TX_USEC;
  2510. ret = axienet_mdio_setup(lp);
  2511. if (ret)
  2512. dev_warn(&pdev->dev,
  2513. "error registering MDIO bus: %d\n", ret);
  2514. if (lp->phy_mode == PHY_INTERFACE_MODE_SGMII ||
  2515. lp->phy_mode == PHY_INTERFACE_MODE_1000BASEX) {
  2516. np = of_parse_phandle(pdev->dev.of_node, "pcs-handle", 0);
  2517. if (!np) {
  2518. /* Deprecated: Always use "pcs-handle" for pcs_phy.
  2519. * Falling back to "phy-handle" here is only for
  2520. * backward compatibility with old device trees.
  2521. */
  2522. np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
  2523. }
  2524. if (!np) {
  2525. dev_err(&pdev->dev, "pcs-handle (preferred) or phy-handle required for 1000BaseX/SGMII\n");
  2526. ret = -EINVAL;
  2527. goto cleanup_mdio;
  2528. }
  2529. lp->pcs_phy = of_mdio_find_device(np);
  2530. if (!lp->pcs_phy) {
  2531. ret = -EPROBE_DEFER;
  2532. of_node_put(np);
  2533. goto cleanup_mdio;
  2534. }
  2535. of_node_put(np);
  2536. lp->pcs.ops = &axienet_pcs_ops;
  2537. lp->pcs.neg_mode = true;
  2538. lp->pcs.poll = true;
  2539. }
  2540. lp->phylink_config.dev = &ndev->dev;
  2541. lp->phylink_config.type = PHYLINK_NETDEV;
  2542. lp->phylink_config.mac_managed_pm = true;
  2543. lp->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE |
  2544. MAC_10FD | MAC_100FD | MAC_1000FD;
  2545. __set_bit(lp->phy_mode, lp->phylink_config.supported_interfaces);
  2546. if (lp->switch_x_sgmii) {
  2547. __set_bit(PHY_INTERFACE_MODE_1000BASEX,
  2548. lp->phylink_config.supported_interfaces);
  2549. __set_bit(PHY_INTERFACE_MODE_SGMII,
  2550. lp->phylink_config.supported_interfaces);
  2551. }
  2552. lp->phylink = phylink_create(&lp->phylink_config, pdev->dev.fwnode,
  2553. lp->phy_mode,
  2554. &axienet_phylink_ops);
  2555. if (IS_ERR(lp->phylink)) {
  2556. ret = PTR_ERR(lp->phylink);
  2557. dev_err(&pdev->dev, "phylink_create error (%i)\n", ret);
  2558. goto cleanup_mdio;
  2559. }
  2560. ret = register_netdev(lp->ndev);
  2561. if (ret) {
  2562. dev_err(lp->dev, "register_netdev() error (%i)\n", ret);
  2563. goto cleanup_phylink;
  2564. }
  2565. return 0;
  2566. cleanup_phylink:
  2567. phylink_destroy(lp->phylink);
  2568. cleanup_mdio:
  2569. if (lp->pcs_phy)
  2570. put_device(&lp->pcs_phy->dev);
  2571. if (lp->mii_bus)
  2572. axienet_mdio_teardown(lp);
  2573. cleanup_clk:
  2574. clk_bulk_disable_unprepare(XAE_NUM_MISC_CLOCKS, lp->misc_clks);
  2575. clk_disable_unprepare(lp->axi_clk);
  2576. free_netdev:
  2577. free_netdev(ndev);
  2578. return ret;
  2579. }
  2580. static void axienet_remove(struct platform_device *pdev)
  2581. {
  2582. struct net_device *ndev = platform_get_drvdata(pdev);
  2583. struct axienet_local *lp = netdev_priv(ndev);
  2584. unregister_netdev(ndev);
  2585. if (lp->phylink)
  2586. phylink_destroy(lp->phylink);
  2587. if (lp->pcs_phy)
  2588. put_device(&lp->pcs_phy->dev);
  2589. axienet_mdio_teardown(lp);
  2590. clk_bulk_disable_unprepare(XAE_NUM_MISC_CLOCKS, lp->misc_clks);
  2591. clk_disable_unprepare(lp->axi_clk);
  2592. free_netdev(ndev);
  2593. }
  2594. static void axienet_shutdown(struct platform_device *pdev)
  2595. {
  2596. struct net_device *ndev = platform_get_drvdata(pdev);
  2597. rtnl_lock();
  2598. netif_device_detach(ndev);
  2599. if (netif_running(ndev))
  2600. dev_close(ndev);
  2601. rtnl_unlock();
  2602. }
  2603. static int axienet_suspend(struct device *dev)
  2604. {
  2605. struct net_device *ndev = dev_get_drvdata(dev);
  2606. if (!netif_running(ndev))
  2607. return 0;
  2608. netif_device_detach(ndev);
  2609. rtnl_lock();
  2610. axienet_stop(ndev);
  2611. rtnl_unlock();
  2612. return 0;
  2613. }
  2614. static int axienet_resume(struct device *dev)
  2615. {
  2616. struct net_device *ndev = dev_get_drvdata(dev);
  2617. if (!netif_running(ndev))
  2618. return 0;
  2619. rtnl_lock();
  2620. axienet_open(ndev);
  2621. rtnl_unlock();
  2622. netif_device_attach(ndev);
  2623. return 0;
  2624. }
  2625. static DEFINE_SIMPLE_DEV_PM_OPS(axienet_pm_ops,
  2626. axienet_suspend, axienet_resume);
  2627. static struct platform_driver axienet_driver = {
  2628. .probe = axienet_probe,
  2629. .remove_new = axienet_remove,
  2630. .shutdown = axienet_shutdown,
  2631. .driver = {
  2632. .name = "xilinx_axienet",
  2633. .pm = &axienet_pm_ops,
  2634. .of_match_table = axienet_of_match,
  2635. },
  2636. };
  2637. module_platform_driver(axienet_driver);
  2638. MODULE_DESCRIPTION("Xilinx Axi Ethernet driver");
  2639. MODULE_AUTHOR("Xilinx");
  2640. MODULE_LICENSE("GPL");