stmmac_main.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611
  1. /*******************************************************************************
  2. This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
  3. ST Ethernet IPs are built around a Synopsys IP Core.
  4. Copyright(C) 2007-2011 STMicroelectronics Ltd
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms and conditions of the GNU General Public License,
  7. version 2, as published by the Free Software Foundation.
  8. This program is distributed in the hope it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. You should have received a copy of the GNU General Public License along with
  13. this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  15. The full GNU General Public License is included in this distribution in
  16. the file called "COPYING".
  17. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  18. Documentation available at:
  19. http://www.stlinux.com
  20. Support available at:
  21. https://bugzilla.stlinux.com/
  22. *******************************************************************************/
  23. #include <linux/clk.h>
  24. #include <linux/kernel.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/ip.h>
  27. #include <linux/tcp.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/if_ether.h>
  31. #include <linux/crc32.h>
  32. #include <linux/mii.h>
  33. #include <linux/if.h>
  34. #include <linux/if_vlan.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/slab.h>
  37. #include <linux/prefetch.h>
  38. #include <linux/pinctrl/consumer.h>
  39. #ifdef CONFIG_DEBUG_FS
  40. #include <linux/debugfs.h>
  41. #include <linux/seq_file.h>
  42. #endif /* CONFIG_DEBUG_FS */
  43. #include <linux/net_tstamp.h>
  44. #include "stmmac_ptp.h"
  45. #include "stmmac.h"
  46. #include <linux/reset.h>
  47. #include <linux/of_mdio.h>
  48. #include "dwmac1000.h"
  49. #define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
  50. #define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
  51. /* Module parameters */
  52. #define TX_TIMEO 5000
  53. static int watchdog = TX_TIMEO;
  54. module_param(watchdog, int, S_IRUGO | S_IWUSR);
  55. MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
  56. static int debug = -1;
  57. module_param(debug, int, S_IRUGO | S_IWUSR);
  58. MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
  59. static int phyaddr = -1;
  60. module_param(phyaddr, int, S_IRUGO);
  61. MODULE_PARM_DESC(phyaddr, "Physical device address");
  62. #define STMMAC_TX_THRESH (DMA_TX_SIZE / 4)
  63. #define STMMAC_RX_THRESH (DMA_RX_SIZE / 4)
  64. static int flow_ctrl = FLOW_OFF;
  65. module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
  66. MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
  67. static int pause = PAUSE_TIME;
  68. module_param(pause, int, S_IRUGO | S_IWUSR);
  69. MODULE_PARM_DESC(pause, "Flow Control Pause Time");
  70. #define TC_DEFAULT 64
  71. static int tc = TC_DEFAULT;
  72. module_param(tc, int, S_IRUGO | S_IWUSR);
  73. MODULE_PARM_DESC(tc, "DMA threshold control value");
  74. #define DEFAULT_BUFSIZE 1536
  75. static int buf_sz = DEFAULT_BUFSIZE;
  76. module_param(buf_sz, int, S_IRUGO | S_IWUSR);
  77. MODULE_PARM_DESC(buf_sz, "DMA buffer size");
  78. #define STMMAC_RX_COPYBREAK 256
  79. static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  80. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  81. NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
  82. #define STMMAC_DEFAULT_LPI_TIMER 1000
  83. static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  84. module_param(eee_timer, int, S_IRUGO | S_IWUSR);
  85. MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
  86. #define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
  87. /* By default the driver will use the ring mode to manage tx and rx descriptors
  88. * but passing this value so user can force to use the chain instead of the ring
  89. */
  90. static unsigned int chain_mode;
  91. module_param(chain_mode, int, S_IRUGO);
  92. MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
  93. static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
  94. #ifdef CONFIG_DEBUG_FS
  95. static int stmmac_init_fs(struct net_device *dev);
  96. static void stmmac_exit_fs(struct net_device *dev);
  97. #endif
  98. #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
  99. /**
  100. * stmmac_verify_args - verify the driver parameters.
  101. * Description: it checks the driver parameters and set a default in case of
  102. * errors.
  103. */
  104. static void stmmac_verify_args(void)
  105. {
  106. if (unlikely(watchdog < 0))
  107. watchdog = TX_TIMEO;
  108. if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
  109. buf_sz = DEFAULT_BUFSIZE;
  110. if (unlikely(flow_ctrl > 1))
  111. flow_ctrl = FLOW_AUTO;
  112. else if (likely(flow_ctrl < 0))
  113. flow_ctrl = FLOW_OFF;
  114. if (unlikely((pause < 0) || (pause > 0xffff)))
  115. pause = PAUSE_TIME;
  116. if (eee_timer < 0)
  117. eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  118. }
  119. /**
  120. * stmmac_clk_csr_set - dynamically set the MDC clock
  121. * @priv: driver private structure
  122. * Description: this is to dynamically set the MDC clock according to the csr
  123. * clock input.
  124. * Note:
  125. * If a specific clk_csr value is passed from the platform
  126. * this means that the CSR Clock Range selection cannot be
  127. * changed at run-time and it is fixed (as reported in the driver
  128. * documentation). Viceversa the driver will try to set the MDC
  129. * clock dynamically according to the actual clock input.
  130. */
  131. static void stmmac_clk_csr_set(struct stmmac_priv *priv)
  132. {
  133. u32 clk_rate;
  134. clk_rate = clk_get_rate(priv->stmmac_clk);
  135. /* Platform provided default clk_csr would be assumed valid
  136. * for all other cases except for the below mentioned ones.
  137. * For values higher than the IEEE 802.3 specified frequency
  138. * we can not estimate the proper divider as it is not known
  139. * the frequency of clk_csr_i. So we do not change the default
  140. * divider.
  141. */
  142. if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
  143. if (clk_rate < CSR_F_35M)
  144. priv->clk_csr = STMMAC_CSR_20_35M;
  145. else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
  146. priv->clk_csr = STMMAC_CSR_35_60M;
  147. else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
  148. priv->clk_csr = STMMAC_CSR_60_100M;
  149. else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
  150. priv->clk_csr = STMMAC_CSR_100_150M;
  151. else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
  152. priv->clk_csr = STMMAC_CSR_150_250M;
  153. else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
  154. priv->clk_csr = STMMAC_CSR_250_300M;
  155. }
  156. }
  157. static void print_pkt(unsigned char *buf, int len)
  158. {
  159. pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
  160. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
  161. }
  162. static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
  163. {
  164. unsigned avail;
  165. if (priv->dirty_tx > priv->cur_tx)
  166. avail = priv->dirty_tx - priv->cur_tx - 1;
  167. else
  168. avail = DMA_TX_SIZE - priv->cur_tx + priv->dirty_tx - 1;
  169. return avail;
  170. }
  171. static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv)
  172. {
  173. unsigned dirty;
  174. if (priv->dirty_rx <= priv->cur_rx)
  175. dirty = priv->cur_rx - priv->dirty_rx;
  176. else
  177. dirty = DMA_RX_SIZE - priv->dirty_rx + priv->cur_rx;
  178. return dirty;
  179. }
  180. /**
  181. * stmmac_hw_fix_mac_speed - callback for speed selection
  182. * @priv: driver private structure
  183. * Description: on some platforms (e.g. ST), some HW system configuraton
  184. * registers have to be set according to the link speed negotiated.
  185. */
  186. static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
  187. {
  188. struct phy_device *phydev = priv->phydev;
  189. if (likely(priv->plat->fix_mac_speed))
  190. priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
  191. }
  192. /**
  193. * stmmac_enable_eee_mode - check and enter in LPI mode
  194. * @priv: driver private structure
  195. * Description: this function is to verify and enter in LPI mode in case of
  196. * EEE.
  197. */
  198. static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
  199. {
  200. /* Check and enter in LPI mode */
  201. if ((priv->dirty_tx == priv->cur_tx) &&
  202. (priv->tx_path_in_lpi_mode == false))
  203. priv->hw->mac->set_eee_mode(priv->hw);
  204. }
  205. /**
  206. * stmmac_disable_eee_mode - disable and exit from LPI mode
  207. * @priv: driver private structure
  208. * Description: this function is to exit and disable EEE in case of
  209. * LPI state is true. This is called by the xmit.
  210. */
  211. void stmmac_disable_eee_mode(struct stmmac_priv *priv)
  212. {
  213. priv->hw->mac->reset_eee_mode(priv->hw);
  214. del_timer_sync(&priv->eee_ctrl_timer);
  215. priv->tx_path_in_lpi_mode = false;
  216. }
  217. /**
  218. * stmmac_eee_ctrl_timer - EEE TX SW timer.
  219. * @arg : data hook
  220. * Description:
  221. * if there is no data transfer and if we are not in LPI state,
  222. * then MAC Transmitter can be moved to LPI state.
  223. */
  224. static void stmmac_eee_ctrl_timer(unsigned long arg)
  225. {
  226. struct stmmac_priv *priv = (struct stmmac_priv *)arg;
  227. stmmac_enable_eee_mode(priv);
  228. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
  229. }
  230. /**
  231. * stmmac_eee_init - init EEE
  232. * @priv: driver private structure
  233. * Description:
  234. * if the GMAC supports the EEE (from the HW cap reg) and the phy device
  235. * can also manage EEE, this function enable the LPI state and start related
  236. * timer.
  237. */
  238. bool stmmac_eee_init(struct stmmac_priv *priv)
  239. {
  240. unsigned long flags;
  241. bool ret = false;
  242. /* Using PCS we cannot dial with the phy registers at this stage
  243. * so we do not support extra feature like EEE.
  244. */
  245. if ((priv->hw->pcs == STMMAC_PCS_RGMII) ||
  246. (priv->hw->pcs == STMMAC_PCS_TBI) ||
  247. (priv->hw->pcs == STMMAC_PCS_RTBI))
  248. goto out;
  249. /* MAC core supports the EEE feature. */
  250. if (priv->dma_cap.eee) {
  251. int tx_lpi_timer = priv->tx_lpi_timer;
  252. /* Check if the PHY supports EEE */
  253. if (phy_init_eee(priv->phydev, 1)) {
  254. /* To manage at run-time if the EEE cannot be supported
  255. * anymore (for example because the lp caps have been
  256. * changed).
  257. * In that case the driver disable own timers.
  258. */
  259. spin_lock_irqsave(&priv->lock, flags);
  260. if (priv->eee_active) {
  261. pr_debug("stmmac: disable EEE\n");
  262. del_timer_sync(&priv->eee_ctrl_timer);
  263. priv->hw->mac->set_eee_timer(priv->hw, 0,
  264. tx_lpi_timer);
  265. }
  266. priv->eee_active = 0;
  267. spin_unlock_irqrestore(&priv->lock, flags);
  268. goto out;
  269. }
  270. /* Activate the EEE and start timers */
  271. spin_lock_irqsave(&priv->lock, flags);
  272. if (!priv->eee_active) {
  273. priv->eee_active = 1;
  274. setup_timer(&priv->eee_ctrl_timer,
  275. stmmac_eee_ctrl_timer,
  276. (unsigned long)priv);
  277. mod_timer(&priv->eee_ctrl_timer,
  278. STMMAC_LPI_T(eee_timer));
  279. priv->hw->mac->set_eee_timer(priv->hw,
  280. STMMAC_DEFAULT_LIT_LS,
  281. tx_lpi_timer);
  282. }
  283. /* Set HW EEE according to the speed */
  284. priv->hw->mac->set_eee_pls(priv->hw, priv->phydev->link);
  285. ret = true;
  286. spin_unlock_irqrestore(&priv->lock, flags);
  287. pr_debug("stmmac: Energy-Efficient Ethernet initialized\n");
  288. }
  289. out:
  290. return ret;
  291. }
  292. /* stmmac_get_tx_hwtstamp - get HW TX timestamps
  293. * @priv: driver private structure
  294. * @entry : descriptor index to be used.
  295. * @skb : the socket buffer
  296. * Description :
  297. * This function will read timestamp from the descriptor & pass it to stack.
  298. * and also perform some sanity checks.
  299. */
  300. static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
  301. unsigned int entry, struct sk_buff *skb)
  302. {
  303. struct skb_shared_hwtstamps shhwtstamp;
  304. u64 ns;
  305. void *desc = NULL;
  306. if (!priv->hwts_tx_en)
  307. return;
  308. /* exit if skb doesn't support hw tstamp */
  309. if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
  310. return;
  311. if (priv->adv_ts)
  312. desc = (priv->dma_etx + entry);
  313. else
  314. desc = (priv->dma_tx + entry);
  315. /* check tx tstamp status */
  316. if (!priv->hw->desc->get_tx_timestamp_status((struct dma_desc *)desc))
  317. return;
  318. /* get the valid tstamp */
  319. ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
  320. memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
  321. shhwtstamp.hwtstamp = ns_to_ktime(ns);
  322. /* pass tstamp to stack */
  323. skb_tstamp_tx(skb, &shhwtstamp);
  324. return;
  325. }
  326. /* stmmac_get_rx_hwtstamp - get HW RX timestamps
  327. * @priv: driver private structure
  328. * @entry : descriptor index to be used.
  329. * @skb : the socket buffer
  330. * Description :
  331. * This function will read received packet's timestamp from the descriptor
  332. * and pass it to stack. It also perform some sanity checks.
  333. */
  334. static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv,
  335. unsigned int entry, struct sk_buff *skb)
  336. {
  337. struct skb_shared_hwtstamps *shhwtstamp = NULL;
  338. u64 ns;
  339. void *desc = NULL;
  340. if (!priv->hwts_rx_en)
  341. return;
  342. if (priv->adv_ts)
  343. desc = (priv->dma_erx + entry);
  344. else
  345. desc = (priv->dma_rx + entry);
  346. /* exit if rx tstamp is not valid */
  347. if (!priv->hw->desc->get_rx_timestamp_status(desc, priv->adv_ts))
  348. return;
  349. /* get valid tstamp */
  350. ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
  351. shhwtstamp = skb_hwtstamps(skb);
  352. memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
  353. shhwtstamp->hwtstamp = ns_to_ktime(ns);
  354. }
  355. /**
  356. * stmmac_hwtstamp_ioctl - control hardware timestamping.
  357. * @dev: device pointer.
  358. * @ifr: An IOCTL specefic structure, that can contain a pointer to
  359. * a proprietary structure used to pass information to the driver.
  360. * Description:
  361. * This function configures the MAC to enable/disable both outgoing(TX)
  362. * and incoming(RX) packets time stamping based on user input.
  363. * Return Value:
  364. * 0 on success and an appropriate -ve integer on failure.
  365. */
  366. static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  367. {
  368. struct stmmac_priv *priv = netdev_priv(dev);
  369. struct hwtstamp_config config;
  370. struct timespec64 now;
  371. u64 temp = 0;
  372. u32 ptp_v2 = 0;
  373. u32 tstamp_all = 0;
  374. u32 ptp_over_ipv4_udp = 0;
  375. u32 ptp_over_ipv6_udp = 0;
  376. u32 ptp_over_ethernet = 0;
  377. u32 snap_type_sel = 0;
  378. u32 ts_master_en = 0;
  379. u32 ts_event_en = 0;
  380. u32 value = 0;
  381. u32 sec_inc;
  382. if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
  383. netdev_alert(priv->dev, "No support for HW time stamping\n");
  384. priv->hwts_tx_en = 0;
  385. priv->hwts_rx_en = 0;
  386. return -EOPNOTSUPP;
  387. }
  388. if (copy_from_user(&config, ifr->ifr_data,
  389. sizeof(struct hwtstamp_config)))
  390. return -EFAULT;
  391. pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
  392. __func__, config.flags, config.tx_type, config.rx_filter);
  393. /* reserved for future extensions */
  394. if (config.flags)
  395. return -EINVAL;
  396. if (config.tx_type != HWTSTAMP_TX_OFF &&
  397. config.tx_type != HWTSTAMP_TX_ON)
  398. return -ERANGE;
  399. if (priv->adv_ts) {
  400. switch (config.rx_filter) {
  401. case HWTSTAMP_FILTER_NONE:
  402. /* time stamp no incoming packet at all */
  403. config.rx_filter = HWTSTAMP_FILTER_NONE;
  404. break;
  405. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  406. /* PTP v1, UDP, any kind of event packet */
  407. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  408. /* take time stamp for all event messages */
  409. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  410. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  411. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  412. break;
  413. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  414. /* PTP v1, UDP, Sync packet */
  415. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
  416. /* take time stamp for SYNC messages only */
  417. ts_event_en = PTP_TCR_TSEVNTENA;
  418. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  419. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  420. break;
  421. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  422. /* PTP v1, UDP, Delay_req packet */
  423. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
  424. /* take time stamp for Delay_Req messages only */
  425. ts_master_en = PTP_TCR_TSMSTRENA;
  426. ts_event_en = PTP_TCR_TSEVNTENA;
  427. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  428. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  429. break;
  430. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  431. /* PTP v2, UDP, any kind of event packet */
  432. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  433. ptp_v2 = PTP_TCR_TSVER2ENA;
  434. /* take time stamp for all event messages */
  435. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  436. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  437. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  438. break;
  439. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  440. /* PTP v2, UDP, Sync packet */
  441. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
  442. ptp_v2 = PTP_TCR_TSVER2ENA;
  443. /* take time stamp for SYNC messages only */
  444. ts_event_en = PTP_TCR_TSEVNTENA;
  445. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  446. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  447. break;
  448. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  449. /* PTP v2, UDP, Delay_req packet */
  450. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
  451. ptp_v2 = PTP_TCR_TSVER2ENA;
  452. /* take time stamp for Delay_Req messages only */
  453. ts_master_en = PTP_TCR_TSMSTRENA;
  454. ts_event_en = PTP_TCR_TSEVNTENA;
  455. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  456. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  457. break;
  458. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  459. /* PTP v2/802.AS1 any layer, any kind of event packet */
  460. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  461. ptp_v2 = PTP_TCR_TSVER2ENA;
  462. /* take time stamp for all event messages */
  463. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  464. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  465. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  466. ptp_over_ethernet = PTP_TCR_TSIPENA;
  467. break;
  468. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  469. /* PTP v2/802.AS1, any layer, Sync packet */
  470. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
  471. ptp_v2 = PTP_TCR_TSVER2ENA;
  472. /* take time stamp for SYNC messages only */
  473. ts_event_en = PTP_TCR_TSEVNTENA;
  474. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  475. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  476. ptp_over_ethernet = PTP_TCR_TSIPENA;
  477. break;
  478. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  479. /* PTP v2/802.AS1, any layer, Delay_req packet */
  480. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
  481. ptp_v2 = PTP_TCR_TSVER2ENA;
  482. /* take time stamp for Delay_Req messages only */
  483. ts_master_en = PTP_TCR_TSMSTRENA;
  484. ts_event_en = PTP_TCR_TSEVNTENA;
  485. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  486. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  487. ptp_over_ethernet = PTP_TCR_TSIPENA;
  488. break;
  489. case HWTSTAMP_FILTER_ALL:
  490. /* time stamp any incoming packet */
  491. config.rx_filter = HWTSTAMP_FILTER_ALL;
  492. tstamp_all = PTP_TCR_TSENALL;
  493. break;
  494. default:
  495. return -ERANGE;
  496. }
  497. } else {
  498. switch (config.rx_filter) {
  499. case HWTSTAMP_FILTER_NONE:
  500. config.rx_filter = HWTSTAMP_FILTER_NONE;
  501. break;
  502. default:
  503. /* PTP v1, UDP, any kind of event packet */
  504. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  505. break;
  506. }
  507. }
  508. priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
  509. priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
  510. if (!priv->hwts_tx_en && !priv->hwts_rx_en)
  511. priv->hw->ptp->config_hw_tstamping(priv->ioaddr, 0);
  512. else {
  513. value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
  514. tstamp_all | ptp_v2 | ptp_over_ethernet |
  515. ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
  516. ts_master_en | snap_type_sel);
  517. priv->hw->ptp->config_hw_tstamping(priv->ioaddr, value);
  518. /* program Sub Second Increment reg */
  519. sec_inc = priv->hw->ptp->config_sub_second_increment(
  520. priv->ioaddr, priv->clk_ptp_rate);
  521. temp = div_u64(1000000000ULL, sec_inc);
  522. /* calculate default added value:
  523. * formula is :
  524. * addend = (2^32)/freq_div_ratio;
  525. * where, freq_div_ratio = 1e9ns/sec_inc
  526. */
  527. temp = (u64)(temp << 32);
  528. priv->default_addend = div_u64(temp, priv->clk_ptp_rate);
  529. priv->hw->ptp->config_addend(priv->ioaddr,
  530. priv->default_addend);
  531. /* initialize system time */
  532. ktime_get_real_ts64(&now);
  533. /* lower 32 bits of tv_sec are safe until y2106 */
  534. priv->hw->ptp->init_systime(priv->ioaddr, (u32)now.tv_sec,
  535. now.tv_nsec);
  536. }
  537. return copy_to_user(ifr->ifr_data, &config,
  538. sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
  539. }
  540. /**
  541. * stmmac_init_ptp - init PTP
  542. * @priv: driver private structure
  543. * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
  544. * This is done by looking at the HW cap. register.
  545. * This function also registers the ptp driver.
  546. */
  547. static int stmmac_init_ptp(struct stmmac_priv *priv)
  548. {
  549. if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
  550. return -EOPNOTSUPP;
  551. /* Fall-back to main clock in case of no PTP ref is passed */
  552. priv->clk_ptp_ref = devm_clk_get(priv->device, "clk_ptp_ref");
  553. if (IS_ERR(priv->clk_ptp_ref)) {
  554. priv->clk_ptp_rate = clk_get_rate(priv->stmmac_clk);
  555. priv->clk_ptp_ref = NULL;
  556. } else {
  557. clk_prepare_enable(priv->clk_ptp_ref);
  558. priv->clk_ptp_rate = clk_get_rate(priv->clk_ptp_ref);
  559. }
  560. priv->adv_ts = 0;
  561. if (priv->dma_cap.atime_stamp && priv->extend_desc)
  562. priv->adv_ts = 1;
  563. if (netif_msg_hw(priv) && priv->dma_cap.time_stamp)
  564. pr_debug("IEEE 1588-2002 Time Stamp supported\n");
  565. if (netif_msg_hw(priv) && priv->adv_ts)
  566. pr_debug("IEEE 1588-2008 Advanced Time Stamp supported\n");
  567. priv->hw->ptp = &stmmac_ptp;
  568. priv->hwts_tx_en = 0;
  569. priv->hwts_rx_en = 0;
  570. return stmmac_ptp_register(priv);
  571. }
  572. static void stmmac_release_ptp(struct stmmac_priv *priv)
  573. {
  574. if (priv->clk_ptp_ref)
  575. clk_disable_unprepare(priv->clk_ptp_ref);
  576. stmmac_ptp_unregister(priv);
  577. }
  578. /**
  579. * stmmac_adjust_link - adjusts the link parameters
  580. * @dev: net device structure
  581. * Description: this is the helper called by the physical abstraction layer
  582. * drivers to communicate the phy link status. According the speed and duplex
  583. * this driver can invoke registered glue-logic as well.
  584. * It also invoke the eee initialization because it could happen when switch
  585. * on different networks (that are eee capable).
  586. */
  587. static void stmmac_adjust_link(struct net_device *dev)
  588. {
  589. struct stmmac_priv *priv = netdev_priv(dev);
  590. struct phy_device *phydev = priv->phydev;
  591. unsigned long flags;
  592. int new_state = 0;
  593. unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
  594. if (phydev == NULL)
  595. return;
  596. spin_lock_irqsave(&priv->lock, flags);
  597. if (phydev->link) {
  598. u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
  599. /* Now we make sure that we can be in full duplex mode.
  600. * If not, we operate in half-duplex mode. */
  601. if (phydev->duplex != priv->oldduplex) {
  602. new_state = 1;
  603. if (!(phydev->duplex))
  604. ctrl &= ~priv->hw->link.duplex;
  605. else
  606. ctrl |= priv->hw->link.duplex;
  607. priv->oldduplex = phydev->duplex;
  608. }
  609. /* Flow Control operation */
  610. if (phydev->pause)
  611. priv->hw->mac->flow_ctrl(priv->hw, phydev->duplex,
  612. fc, pause_time);
  613. if (phydev->speed != priv->speed) {
  614. new_state = 1;
  615. switch (phydev->speed) {
  616. case 1000:
  617. if (likely((priv->plat->has_gmac) ||
  618. (priv->plat->has_gmac4)))
  619. ctrl &= ~priv->hw->link.port;
  620. stmmac_hw_fix_mac_speed(priv);
  621. break;
  622. case 100:
  623. case 10:
  624. if (likely((priv->plat->has_gmac) ||
  625. (priv->plat->has_gmac4))) {
  626. ctrl |= priv->hw->link.port;
  627. if (phydev->speed == SPEED_100) {
  628. ctrl |= priv->hw->link.speed;
  629. } else {
  630. ctrl &= ~(priv->hw->link.speed);
  631. }
  632. } else {
  633. ctrl &= ~priv->hw->link.port;
  634. }
  635. stmmac_hw_fix_mac_speed(priv);
  636. break;
  637. default:
  638. if (netif_msg_link(priv))
  639. pr_warn("%s: Speed (%d) not 10/100\n",
  640. dev->name, phydev->speed);
  641. break;
  642. }
  643. priv->speed = phydev->speed;
  644. }
  645. writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
  646. if (!priv->oldlink) {
  647. new_state = 1;
  648. priv->oldlink = 1;
  649. }
  650. } else if (priv->oldlink) {
  651. new_state = 1;
  652. priv->oldlink = 0;
  653. priv->speed = 0;
  654. priv->oldduplex = -1;
  655. }
  656. if (new_state && netif_msg_link(priv))
  657. phy_print_status(phydev);
  658. spin_unlock_irqrestore(&priv->lock, flags);
  659. if (phydev->is_pseudo_fixed_link)
  660. /* Stop PHY layer to call the hook to adjust the link in case
  661. * of a switch is attached to the stmmac driver.
  662. */
  663. phydev->irq = PHY_IGNORE_INTERRUPT;
  664. else
  665. /* At this stage, init the EEE if supported.
  666. * Never called in case of fixed_link.
  667. */
  668. priv->eee_enabled = stmmac_eee_init(priv);
  669. }
  670. /**
  671. * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
  672. * @priv: driver private structure
  673. * Description: this is to verify if the HW supports the PCS.
  674. * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
  675. * configured for the TBI, RTBI, or SGMII PHY interface.
  676. */
  677. static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
  678. {
  679. int interface = priv->plat->interface;
  680. if (priv->dma_cap.pcs) {
  681. if ((interface == PHY_INTERFACE_MODE_RGMII) ||
  682. (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
  683. (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
  684. (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
  685. pr_debug("STMMAC: PCS RGMII support enable\n");
  686. priv->hw->pcs = STMMAC_PCS_RGMII;
  687. } else if (interface == PHY_INTERFACE_MODE_SGMII) {
  688. pr_debug("STMMAC: PCS SGMII support enable\n");
  689. priv->hw->pcs = STMMAC_PCS_SGMII;
  690. }
  691. }
  692. }
  693. /**
  694. * stmmac_init_phy - PHY initialization
  695. * @dev: net device structure
  696. * Description: it initializes the driver's PHY state, and attaches the PHY
  697. * to the mac driver.
  698. * Return value:
  699. * 0 on success
  700. */
  701. static int stmmac_init_phy(struct net_device *dev)
  702. {
  703. struct stmmac_priv *priv = netdev_priv(dev);
  704. struct phy_device *phydev;
  705. char phy_id_fmt[MII_BUS_ID_SIZE + 3];
  706. char bus_id[MII_BUS_ID_SIZE];
  707. int interface = priv->plat->interface;
  708. int max_speed = priv->plat->max_speed;
  709. priv->oldlink = 0;
  710. priv->speed = 0;
  711. priv->oldduplex = -1;
  712. if (priv->plat->phy_node) {
  713. phydev = of_phy_connect(dev, priv->plat->phy_node,
  714. &stmmac_adjust_link, 0, interface);
  715. } else {
  716. snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
  717. priv->plat->bus_id);
  718. snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
  719. priv->plat->phy_addr);
  720. pr_debug("stmmac_init_phy: trying to attach to %s\n",
  721. phy_id_fmt);
  722. phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
  723. interface);
  724. }
  725. if (IS_ERR_OR_NULL(phydev)) {
  726. pr_err("%s: Could not attach to PHY\n", dev->name);
  727. if (!phydev)
  728. return -ENODEV;
  729. return PTR_ERR(phydev);
  730. }
  731. /* Stop Advertising 1000BASE Capability if interface is not GMII */
  732. if ((interface == PHY_INTERFACE_MODE_MII) ||
  733. (interface == PHY_INTERFACE_MODE_RMII) ||
  734. (max_speed < 1000 && max_speed > 0))
  735. phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
  736. SUPPORTED_1000baseT_Full);
  737. /*
  738. * Broken HW is sometimes missing the pull-up resistor on the
  739. * MDIO line, which results in reads to non-existent devices returning
  740. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  741. * device as well.
  742. * Note: phydev->phy_id is the result of reading the UID PHY registers.
  743. */
  744. if (!priv->plat->phy_node && phydev->phy_id == 0) {
  745. phy_disconnect(phydev);
  746. return -ENODEV;
  747. }
  748. pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
  749. " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
  750. priv->phydev = phydev;
  751. return 0;
  752. }
  753. static void stmmac_display_rings(struct stmmac_priv *priv)
  754. {
  755. void *head_rx, *head_tx;
  756. if (priv->extend_desc) {
  757. head_rx = (void *)priv->dma_erx;
  758. head_tx = (void *)priv->dma_etx;
  759. } else {
  760. head_rx = (void *)priv->dma_rx;
  761. head_tx = (void *)priv->dma_tx;
  762. }
  763. /* Display Rx ring */
  764. priv->hw->desc->display_ring(head_rx, DMA_RX_SIZE, true);
  765. /* Display Tx ring */
  766. priv->hw->desc->display_ring(head_tx, DMA_TX_SIZE, false);
  767. }
  768. static int stmmac_set_bfsize(int mtu, int bufsize)
  769. {
  770. int ret = bufsize;
  771. if (mtu >= BUF_SIZE_4KiB)
  772. ret = BUF_SIZE_8KiB;
  773. else if (mtu >= BUF_SIZE_2KiB)
  774. ret = BUF_SIZE_4KiB;
  775. else if (mtu > DEFAULT_BUFSIZE)
  776. ret = BUF_SIZE_2KiB;
  777. else
  778. ret = DEFAULT_BUFSIZE;
  779. return ret;
  780. }
  781. /**
  782. * stmmac_clear_descriptors - clear descriptors
  783. * @priv: driver private structure
  784. * Description: this function is called to clear the tx and rx descriptors
  785. * in case of both basic and extended descriptors are used.
  786. */
  787. static void stmmac_clear_descriptors(struct stmmac_priv *priv)
  788. {
  789. int i;
  790. /* Clear the Rx/Tx descriptors */
  791. for (i = 0; i < DMA_RX_SIZE; i++)
  792. if (priv->extend_desc)
  793. priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
  794. priv->use_riwt, priv->mode,
  795. (i == DMA_RX_SIZE - 1));
  796. else
  797. priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
  798. priv->use_riwt, priv->mode,
  799. (i == DMA_RX_SIZE - 1));
  800. for (i = 0; i < DMA_TX_SIZE; i++)
  801. if (priv->extend_desc)
  802. priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
  803. priv->mode,
  804. (i == DMA_TX_SIZE - 1));
  805. else
  806. priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
  807. priv->mode,
  808. (i == DMA_TX_SIZE - 1));
  809. }
  810. /**
  811. * stmmac_init_rx_buffers - init the RX descriptor buffer.
  812. * @priv: driver private structure
  813. * @p: descriptor pointer
  814. * @i: descriptor index
  815. * @flags: gfp flag.
  816. * Description: this function is called to allocate a receive buffer, perform
  817. * the DMA mapping and init the descriptor.
  818. */
  819. static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
  820. int i, gfp_t flags)
  821. {
  822. struct sk_buff *skb;
  823. skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
  824. if (!skb) {
  825. pr_err("%s: Rx init fails; skb is NULL\n", __func__);
  826. return -ENOMEM;
  827. }
  828. priv->rx_skbuff[i] = skb;
  829. priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
  830. priv->dma_buf_sz,
  831. DMA_FROM_DEVICE);
  832. if (dma_mapping_error(priv->device, priv->rx_skbuff_dma[i])) {
  833. pr_err("%s: DMA mapping error\n", __func__);
  834. dev_kfree_skb_any(skb);
  835. return -EINVAL;
  836. }
  837. if (priv->synopsys_id >= DWMAC_CORE_4_00)
  838. p->des0 = priv->rx_skbuff_dma[i];
  839. else
  840. p->des2 = priv->rx_skbuff_dma[i];
  841. if ((priv->hw->mode->init_desc3) &&
  842. (priv->dma_buf_sz == BUF_SIZE_16KiB))
  843. priv->hw->mode->init_desc3(p);
  844. return 0;
  845. }
  846. static void stmmac_free_rx_buffers(struct stmmac_priv *priv, int i)
  847. {
  848. if (priv->rx_skbuff[i]) {
  849. dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
  850. priv->dma_buf_sz, DMA_FROM_DEVICE);
  851. dev_kfree_skb_any(priv->rx_skbuff[i]);
  852. }
  853. priv->rx_skbuff[i] = NULL;
  854. }
  855. /**
  856. * init_dma_desc_rings - init the RX/TX descriptor rings
  857. * @dev: net device structure
  858. * @flags: gfp flag.
  859. * Description: this function initializes the DMA RX/TX descriptors
  860. * and allocates the socket buffers. It suppors the chained and ring
  861. * modes.
  862. */
  863. static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
  864. {
  865. int i;
  866. struct stmmac_priv *priv = netdev_priv(dev);
  867. unsigned int bfsize = 0;
  868. int ret = -ENOMEM;
  869. if (priv->hw->mode->set_16kib_bfsize)
  870. bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
  871. if (bfsize < BUF_SIZE_16KiB)
  872. bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
  873. priv->dma_buf_sz = bfsize;
  874. if (netif_msg_probe(priv)) {
  875. pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
  876. (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
  877. /* RX INITIALIZATION */
  878. pr_debug("\tSKB addresses:\nskb\t\tskb data\tdma data\n");
  879. }
  880. for (i = 0; i < DMA_RX_SIZE; i++) {
  881. struct dma_desc *p;
  882. if (priv->extend_desc)
  883. p = &((priv->dma_erx + i)->basic);
  884. else
  885. p = priv->dma_rx + i;
  886. ret = stmmac_init_rx_buffers(priv, p, i, flags);
  887. if (ret)
  888. goto err_init_rx_buffers;
  889. if (netif_msg_probe(priv))
  890. pr_debug("[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
  891. priv->rx_skbuff[i]->data,
  892. (unsigned int)priv->rx_skbuff_dma[i]);
  893. }
  894. priv->cur_rx = 0;
  895. priv->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
  896. buf_sz = bfsize;
  897. /* Setup the chained descriptor addresses */
  898. if (priv->mode == STMMAC_CHAIN_MODE) {
  899. if (priv->extend_desc) {
  900. priv->hw->mode->init(priv->dma_erx, priv->dma_rx_phy,
  901. DMA_RX_SIZE, 1);
  902. priv->hw->mode->init(priv->dma_etx, priv->dma_tx_phy,
  903. DMA_TX_SIZE, 1);
  904. } else {
  905. priv->hw->mode->init(priv->dma_rx, priv->dma_rx_phy,
  906. DMA_RX_SIZE, 0);
  907. priv->hw->mode->init(priv->dma_tx, priv->dma_tx_phy,
  908. DMA_TX_SIZE, 0);
  909. }
  910. }
  911. /* TX INITIALIZATION */
  912. for (i = 0; i < DMA_TX_SIZE; i++) {
  913. struct dma_desc *p;
  914. if (priv->extend_desc)
  915. p = &((priv->dma_etx + i)->basic);
  916. else
  917. p = priv->dma_tx + i;
  918. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  919. p->des0 = 0;
  920. p->des1 = 0;
  921. p->des2 = 0;
  922. p->des3 = 0;
  923. } else {
  924. p->des2 = 0;
  925. }
  926. priv->tx_skbuff_dma[i].buf = 0;
  927. priv->tx_skbuff_dma[i].map_as_page = false;
  928. priv->tx_skbuff_dma[i].len = 0;
  929. priv->tx_skbuff_dma[i].last_segment = false;
  930. priv->tx_skbuff[i] = NULL;
  931. }
  932. priv->dirty_tx = 0;
  933. priv->cur_tx = 0;
  934. netdev_reset_queue(priv->dev);
  935. stmmac_clear_descriptors(priv);
  936. if (netif_msg_hw(priv))
  937. stmmac_display_rings(priv);
  938. return 0;
  939. err_init_rx_buffers:
  940. while (--i >= 0)
  941. stmmac_free_rx_buffers(priv, i);
  942. return ret;
  943. }
  944. static void dma_free_rx_skbufs(struct stmmac_priv *priv)
  945. {
  946. int i;
  947. for (i = 0; i < DMA_RX_SIZE; i++)
  948. stmmac_free_rx_buffers(priv, i);
  949. }
  950. static void dma_free_tx_skbufs(struct stmmac_priv *priv)
  951. {
  952. int i;
  953. for (i = 0; i < DMA_TX_SIZE; i++) {
  954. struct dma_desc *p;
  955. if (priv->extend_desc)
  956. p = &((priv->dma_etx + i)->basic);
  957. else
  958. p = priv->dma_tx + i;
  959. if (priv->tx_skbuff_dma[i].buf) {
  960. if (priv->tx_skbuff_dma[i].map_as_page)
  961. dma_unmap_page(priv->device,
  962. priv->tx_skbuff_dma[i].buf,
  963. priv->tx_skbuff_dma[i].len,
  964. DMA_TO_DEVICE);
  965. else
  966. dma_unmap_single(priv->device,
  967. priv->tx_skbuff_dma[i].buf,
  968. priv->tx_skbuff_dma[i].len,
  969. DMA_TO_DEVICE);
  970. }
  971. if (priv->tx_skbuff[i] != NULL) {
  972. dev_kfree_skb_any(priv->tx_skbuff[i]);
  973. priv->tx_skbuff[i] = NULL;
  974. priv->tx_skbuff_dma[i].buf = 0;
  975. priv->tx_skbuff_dma[i].map_as_page = false;
  976. }
  977. }
  978. }
  979. /**
  980. * alloc_dma_desc_resources - alloc TX/RX resources.
  981. * @priv: private structure
  982. * Description: according to which descriptor can be used (extend or basic)
  983. * this function allocates the resources for TX and RX paths. In case of
  984. * reception, for example, it pre-allocated the RX socket buffer in order to
  985. * allow zero-copy mechanism.
  986. */
  987. static int alloc_dma_desc_resources(struct stmmac_priv *priv)
  988. {
  989. int ret = -ENOMEM;
  990. priv->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE, sizeof(dma_addr_t),
  991. GFP_KERNEL);
  992. if (!priv->rx_skbuff_dma)
  993. return -ENOMEM;
  994. priv->rx_skbuff = kmalloc_array(DMA_RX_SIZE, sizeof(struct sk_buff *),
  995. GFP_KERNEL);
  996. if (!priv->rx_skbuff)
  997. goto err_rx_skbuff;
  998. priv->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
  999. sizeof(*priv->tx_skbuff_dma),
  1000. GFP_KERNEL);
  1001. if (!priv->tx_skbuff_dma)
  1002. goto err_tx_skbuff_dma;
  1003. priv->tx_skbuff = kmalloc_array(DMA_TX_SIZE, sizeof(struct sk_buff *),
  1004. GFP_KERNEL);
  1005. if (!priv->tx_skbuff)
  1006. goto err_tx_skbuff;
  1007. if (priv->extend_desc) {
  1008. priv->dma_erx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
  1009. sizeof(struct
  1010. dma_extended_desc),
  1011. &priv->dma_rx_phy,
  1012. GFP_KERNEL);
  1013. if (!priv->dma_erx)
  1014. goto err_dma;
  1015. priv->dma_etx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
  1016. sizeof(struct
  1017. dma_extended_desc),
  1018. &priv->dma_tx_phy,
  1019. GFP_KERNEL);
  1020. if (!priv->dma_etx) {
  1021. dma_free_coherent(priv->device, DMA_RX_SIZE *
  1022. sizeof(struct dma_extended_desc),
  1023. priv->dma_erx, priv->dma_rx_phy);
  1024. goto err_dma;
  1025. }
  1026. } else {
  1027. priv->dma_rx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
  1028. sizeof(struct dma_desc),
  1029. &priv->dma_rx_phy,
  1030. GFP_KERNEL);
  1031. if (!priv->dma_rx)
  1032. goto err_dma;
  1033. priv->dma_tx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
  1034. sizeof(struct dma_desc),
  1035. &priv->dma_tx_phy,
  1036. GFP_KERNEL);
  1037. if (!priv->dma_tx) {
  1038. dma_free_coherent(priv->device, DMA_RX_SIZE *
  1039. sizeof(struct dma_desc),
  1040. priv->dma_rx, priv->dma_rx_phy);
  1041. goto err_dma;
  1042. }
  1043. }
  1044. return 0;
  1045. err_dma:
  1046. kfree(priv->tx_skbuff);
  1047. err_tx_skbuff:
  1048. kfree(priv->tx_skbuff_dma);
  1049. err_tx_skbuff_dma:
  1050. kfree(priv->rx_skbuff);
  1051. err_rx_skbuff:
  1052. kfree(priv->rx_skbuff_dma);
  1053. return ret;
  1054. }
  1055. static void free_dma_desc_resources(struct stmmac_priv *priv)
  1056. {
  1057. /* Release the DMA TX/RX socket buffers */
  1058. dma_free_rx_skbufs(priv);
  1059. dma_free_tx_skbufs(priv);
  1060. /* Free DMA regions of consistent memory previously allocated */
  1061. if (!priv->extend_desc) {
  1062. dma_free_coherent(priv->device,
  1063. DMA_TX_SIZE * sizeof(struct dma_desc),
  1064. priv->dma_tx, priv->dma_tx_phy);
  1065. dma_free_coherent(priv->device,
  1066. DMA_RX_SIZE * sizeof(struct dma_desc),
  1067. priv->dma_rx, priv->dma_rx_phy);
  1068. } else {
  1069. dma_free_coherent(priv->device, DMA_TX_SIZE *
  1070. sizeof(struct dma_extended_desc),
  1071. priv->dma_etx, priv->dma_tx_phy);
  1072. dma_free_coherent(priv->device, DMA_RX_SIZE *
  1073. sizeof(struct dma_extended_desc),
  1074. priv->dma_erx, priv->dma_rx_phy);
  1075. }
  1076. kfree(priv->rx_skbuff_dma);
  1077. kfree(priv->rx_skbuff);
  1078. kfree(priv->tx_skbuff_dma);
  1079. kfree(priv->tx_skbuff);
  1080. }
  1081. /**
  1082. * stmmac_dma_operation_mode - HW DMA operation mode
  1083. * @priv: driver private structure
  1084. * Description: it is used for configuring the DMA operation mode register in
  1085. * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
  1086. */
  1087. static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
  1088. {
  1089. int rxfifosz = priv->plat->rx_fifo_size;
  1090. if (priv->plat->force_thresh_dma_mode)
  1091. priv->hw->dma->dma_mode(priv->ioaddr, tc, tc, rxfifosz);
  1092. else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
  1093. /*
  1094. * In case of GMAC, SF mode can be enabled
  1095. * to perform the TX COE in HW. This depends on:
  1096. * 1) TX COE if actually supported
  1097. * 2) There is no bugged Jumbo frame support
  1098. * that needs to not insert csum in the TDES.
  1099. */
  1100. priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE,
  1101. rxfifosz);
  1102. priv->xstats.threshold = SF_DMA_MODE;
  1103. } else
  1104. priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE,
  1105. rxfifosz);
  1106. }
  1107. /**
  1108. * stmmac_tx_clean - to manage the transmission completion
  1109. * @priv: driver private structure
  1110. * Description: it reclaims the transmit resources after transmission completes.
  1111. */
  1112. static void stmmac_tx_clean(struct stmmac_priv *priv)
  1113. {
  1114. unsigned int bytes_compl = 0, pkts_compl = 0;
  1115. unsigned int entry = priv->dirty_tx;
  1116. spin_lock(&priv->tx_lock);
  1117. priv->xstats.tx_clean++;
  1118. while (entry != priv->cur_tx) {
  1119. struct sk_buff *skb = priv->tx_skbuff[entry];
  1120. struct dma_desc *p;
  1121. int status;
  1122. if (priv->extend_desc)
  1123. p = (struct dma_desc *)(priv->dma_etx + entry);
  1124. else
  1125. p = priv->dma_tx + entry;
  1126. status = priv->hw->desc->tx_status(&priv->dev->stats,
  1127. &priv->xstats, p,
  1128. priv->ioaddr);
  1129. /* Check if the descriptor is owned by the DMA */
  1130. if (unlikely(status & tx_dma_own))
  1131. break;
  1132. /* Just consider the last segment and ...*/
  1133. if (likely(!(status & tx_not_ls))) {
  1134. /* ... verify the status error condition */
  1135. if (unlikely(status & tx_err)) {
  1136. priv->dev->stats.tx_errors++;
  1137. } else {
  1138. priv->dev->stats.tx_packets++;
  1139. priv->xstats.tx_pkt_n++;
  1140. }
  1141. stmmac_get_tx_hwtstamp(priv, entry, skb);
  1142. }
  1143. if (likely(priv->tx_skbuff_dma[entry].buf)) {
  1144. if (priv->tx_skbuff_dma[entry].map_as_page)
  1145. dma_unmap_page(priv->device,
  1146. priv->tx_skbuff_dma[entry].buf,
  1147. priv->tx_skbuff_dma[entry].len,
  1148. DMA_TO_DEVICE);
  1149. else
  1150. dma_unmap_single(priv->device,
  1151. priv->tx_skbuff_dma[entry].buf,
  1152. priv->tx_skbuff_dma[entry].len,
  1153. DMA_TO_DEVICE);
  1154. priv->tx_skbuff_dma[entry].buf = 0;
  1155. priv->tx_skbuff_dma[entry].len = 0;
  1156. priv->tx_skbuff_dma[entry].map_as_page = false;
  1157. }
  1158. if (priv->hw->mode->clean_desc3)
  1159. priv->hw->mode->clean_desc3(priv, p);
  1160. priv->tx_skbuff_dma[entry].last_segment = false;
  1161. priv->tx_skbuff_dma[entry].is_jumbo = false;
  1162. if (likely(skb != NULL)) {
  1163. pkts_compl++;
  1164. bytes_compl += skb->len;
  1165. dev_consume_skb_any(skb);
  1166. priv->tx_skbuff[entry] = NULL;
  1167. }
  1168. priv->hw->desc->release_tx_desc(p, priv->mode);
  1169. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  1170. }
  1171. priv->dirty_tx = entry;
  1172. netdev_completed_queue(priv->dev, pkts_compl, bytes_compl);
  1173. if (unlikely(netif_queue_stopped(priv->dev) &&
  1174. stmmac_tx_avail(priv) > STMMAC_TX_THRESH)) {
  1175. netif_tx_lock(priv->dev);
  1176. if (netif_queue_stopped(priv->dev) &&
  1177. stmmac_tx_avail(priv) > STMMAC_TX_THRESH) {
  1178. if (netif_msg_tx_done(priv))
  1179. pr_debug("%s: restart transmit\n", __func__);
  1180. netif_wake_queue(priv->dev);
  1181. }
  1182. netif_tx_unlock(priv->dev);
  1183. }
  1184. if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
  1185. stmmac_enable_eee_mode(priv);
  1186. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
  1187. }
  1188. spin_unlock(&priv->tx_lock);
  1189. }
  1190. static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
  1191. {
  1192. priv->hw->dma->enable_dma_irq(priv->ioaddr);
  1193. }
  1194. static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
  1195. {
  1196. priv->hw->dma->disable_dma_irq(priv->ioaddr);
  1197. }
  1198. /**
  1199. * stmmac_tx_err - to manage the tx error
  1200. * @priv: driver private structure
  1201. * Description: it cleans the descriptors and restarts the transmission
  1202. * in case of transmission errors.
  1203. */
  1204. static void stmmac_tx_err(struct stmmac_priv *priv)
  1205. {
  1206. int i;
  1207. netif_stop_queue(priv->dev);
  1208. priv->hw->dma->stop_tx(priv->ioaddr);
  1209. dma_free_tx_skbufs(priv);
  1210. for (i = 0; i < DMA_TX_SIZE; i++)
  1211. if (priv->extend_desc)
  1212. priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
  1213. priv->mode,
  1214. (i == DMA_TX_SIZE - 1));
  1215. else
  1216. priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
  1217. priv->mode,
  1218. (i == DMA_TX_SIZE - 1));
  1219. priv->dirty_tx = 0;
  1220. priv->cur_tx = 0;
  1221. netdev_reset_queue(priv->dev);
  1222. priv->hw->dma->start_tx(priv->ioaddr);
  1223. priv->dev->stats.tx_errors++;
  1224. netif_wake_queue(priv->dev);
  1225. }
  1226. /**
  1227. * stmmac_dma_interrupt - DMA ISR
  1228. * @priv: driver private structure
  1229. * Description: this is the DMA ISR. It is called by the main ISR.
  1230. * It calls the dwmac dma routine and schedule poll method in case of some
  1231. * work can be done.
  1232. */
  1233. static void stmmac_dma_interrupt(struct stmmac_priv *priv)
  1234. {
  1235. int status;
  1236. int rxfifosz = priv->plat->rx_fifo_size;
  1237. status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
  1238. if (likely((status & handle_rx)) || (status & handle_tx)) {
  1239. if (likely(napi_schedule_prep(&priv->napi))) {
  1240. stmmac_disable_dma_irq(priv);
  1241. __napi_schedule(&priv->napi);
  1242. }
  1243. }
  1244. if (unlikely(status & tx_hard_error_bump_tc)) {
  1245. /* Try to bump up the dma threshold on this failure */
  1246. if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
  1247. (tc <= 256)) {
  1248. tc += 64;
  1249. if (priv->plat->force_thresh_dma_mode)
  1250. priv->hw->dma->dma_mode(priv->ioaddr, tc, tc,
  1251. rxfifosz);
  1252. else
  1253. priv->hw->dma->dma_mode(priv->ioaddr, tc,
  1254. SF_DMA_MODE, rxfifosz);
  1255. priv->xstats.threshold = tc;
  1256. }
  1257. } else if (unlikely(status == tx_hard_error))
  1258. stmmac_tx_err(priv);
  1259. }
  1260. /**
  1261. * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
  1262. * @priv: driver private structure
  1263. * Description: this masks the MMC irq, in fact, the counters are managed in SW.
  1264. */
  1265. static void stmmac_mmc_setup(struct stmmac_priv *priv)
  1266. {
  1267. unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
  1268. MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
  1269. if (priv->synopsys_id >= DWMAC_CORE_4_00)
  1270. priv->mmcaddr = priv->ioaddr + MMC_GMAC4_OFFSET;
  1271. else
  1272. priv->mmcaddr = priv->ioaddr + MMC_GMAC3_X_OFFSET;
  1273. dwmac_mmc_intr_all_mask(priv->mmcaddr);
  1274. if (priv->dma_cap.rmon) {
  1275. dwmac_mmc_ctrl(priv->mmcaddr, mode);
  1276. memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
  1277. } else
  1278. pr_info(" No MAC Management Counters available\n");
  1279. }
  1280. /**
  1281. * stmmac_selec_desc_mode - to select among: normal/alternate/extend descriptors
  1282. * @priv: driver private structure
  1283. * Description: select the Enhanced/Alternate or Normal descriptors.
  1284. * In case of Enhanced/Alternate, it checks if the extended descriptors are
  1285. * supported by the HW capability register.
  1286. */
  1287. static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
  1288. {
  1289. if (priv->plat->enh_desc) {
  1290. pr_info(" Enhanced/Alternate descriptors\n");
  1291. /* GMAC older than 3.50 has no extended descriptors */
  1292. if (priv->synopsys_id >= DWMAC_CORE_3_50) {
  1293. pr_info("\tEnabled extended descriptors\n");
  1294. priv->extend_desc = 1;
  1295. } else
  1296. pr_warn("Extended descriptors not supported\n");
  1297. priv->hw->desc = &enh_desc_ops;
  1298. } else {
  1299. pr_info(" Normal descriptors\n");
  1300. priv->hw->desc = &ndesc_ops;
  1301. }
  1302. }
  1303. /**
  1304. * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
  1305. * @priv: driver private structure
  1306. * Description:
  1307. * new GMAC chip generations have a new register to indicate the
  1308. * presence of the optional feature/functions.
  1309. * This can be also used to override the value passed through the
  1310. * platform and necessary for old MAC10/100 and GMAC chips.
  1311. */
  1312. static int stmmac_get_hw_features(struct stmmac_priv *priv)
  1313. {
  1314. u32 ret = 0;
  1315. if (priv->hw->dma->get_hw_feature) {
  1316. priv->hw->dma->get_hw_feature(priv->ioaddr,
  1317. &priv->dma_cap);
  1318. ret = 1;
  1319. }
  1320. return ret;
  1321. }
  1322. /**
  1323. * stmmac_check_ether_addr - check if the MAC addr is valid
  1324. * @priv: driver private structure
  1325. * Description:
  1326. * it is to verify if the MAC address is valid, in case of failures it
  1327. * generates a random MAC address
  1328. */
  1329. static void stmmac_check_ether_addr(struct stmmac_priv *priv)
  1330. {
  1331. if (!is_valid_ether_addr(priv->dev->dev_addr)) {
  1332. priv->hw->mac->get_umac_addr(priv->hw,
  1333. priv->dev->dev_addr, 0);
  1334. if (!is_valid_ether_addr(priv->dev->dev_addr))
  1335. eth_hw_addr_random(priv->dev);
  1336. pr_info("%s: device MAC address %pM\n", priv->dev->name,
  1337. priv->dev->dev_addr);
  1338. }
  1339. }
  1340. /**
  1341. * stmmac_init_dma_engine - DMA init.
  1342. * @priv: driver private structure
  1343. * Description:
  1344. * It inits the DMA invoking the specific MAC/GMAC callback.
  1345. * Some DMA parameters can be passed from the platform;
  1346. * in case of these are not passed a default is kept for the MAC or GMAC.
  1347. */
  1348. static int stmmac_init_dma_engine(struct stmmac_priv *priv)
  1349. {
  1350. int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, aal = 0;
  1351. int mixed_burst = 0;
  1352. int atds = 0;
  1353. int ret = 0;
  1354. if (priv->plat->dma_cfg) {
  1355. pbl = priv->plat->dma_cfg->pbl;
  1356. fixed_burst = priv->plat->dma_cfg->fixed_burst;
  1357. mixed_burst = priv->plat->dma_cfg->mixed_burst;
  1358. aal = priv->plat->dma_cfg->aal;
  1359. }
  1360. if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
  1361. atds = 1;
  1362. ret = priv->hw->dma->reset(priv->ioaddr);
  1363. if (ret) {
  1364. dev_err(priv->device, "Failed to reset the dma\n");
  1365. return ret;
  1366. }
  1367. priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
  1368. aal, priv->dma_tx_phy, priv->dma_rx_phy, atds);
  1369. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  1370. priv->rx_tail_addr = priv->dma_rx_phy +
  1371. (DMA_RX_SIZE * sizeof(struct dma_desc));
  1372. priv->hw->dma->set_rx_tail_ptr(priv->ioaddr, priv->rx_tail_addr,
  1373. STMMAC_CHAN0);
  1374. priv->tx_tail_addr = priv->dma_tx_phy +
  1375. (DMA_TX_SIZE * sizeof(struct dma_desc));
  1376. priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
  1377. STMMAC_CHAN0);
  1378. }
  1379. if (priv->plat->axi && priv->hw->dma->axi)
  1380. priv->hw->dma->axi(priv->ioaddr, priv->plat->axi);
  1381. return ret;
  1382. }
  1383. /**
  1384. * stmmac_tx_timer - mitigation sw timer for tx.
  1385. * @data: data pointer
  1386. * Description:
  1387. * This is the timer handler to directly invoke the stmmac_tx_clean.
  1388. */
  1389. static void stmmac_tx_timer(unsigned long data)
  1390. {
  1391. struct stmmac_priv *priv = (struct stmmac_priv *)data;
  1392. stmmac_tx_clean(priv);
  1393. }
  1394. /**
  1395. * stmmac_init_tx_coalesce - init tx mitigation options.
  1396. * @priv: driver private structure
  1397. * Description:
  1398. * This inits the transmit coalesce parameters: i.e. timer rate,
  1399. * timer handler and default threshold used for enabling the
  1400. * interrupt on completion bit.
  1401. */
  1402. static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
  1403. {
  1404. priv->tx_coal_frames = STMMAC_TX_FRAMES;
  1405. priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
  1406. init_timer(&priv->txtimer);
  1407. priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
  1408. priv->txtimer.data = (unsigned long)priv;
  1409. priv->txtimer.function = stmmac_tx_timer;
  1410. add_timer(&priv->txtimer);
  1411. }
  1412. /**
  1413. * stmmac_hw_setup - setup mac in a usable state.
  1414. * @dev : pointer to the device structure.
  1415. * Description:
  1416. * this is the main function to setup the HW in a usable state because the
  1417. * dma engine is reset, the core registers are configured (e.g. AXI,
  1418. * Checksum features, timers). The DMA is ready to start receiving and
  1419. * transmitting.
  1420. * Return value:
  1421. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1422. * file on failure.
  1423. */
  1424. static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
  1425. {
  1426. struct stmmac_priv *priv = netdev_priv(dev);
  1427. int ret;
  1428. /* DMA initialization and SW reset */
  1429. ret = stmmac_init_dma_engine(priv);
  1430. if (ret < 0) {
  1431. pr_err("%s: DMA engine initialization failed\n", __func__);
  1432. return ret;
  1433. }
  1434. /* Copy the MAC addr into the HW */
  1435. priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
  1436. /* If required, perform hw setup of the bus. */
  1437. if (priv->plat->bus_setup)
  1438. priv->plat->bus_setup(priv->ioaddr);
  1439. /* PS and related bits will be programmed according to the speed */
  1440. if (priv->hw->pcs) {
  1441. int speed = priv->plat->mac_port_sel_speed;
  1442. if ((speed == SPEED_10) || (speed == SPEED_100) ||
  1443. (speed == SPEED_1000)) {
  1444. priv->hw->ps = speed;
  1445. } else {
  1446. dev_warn(priv->device, "invalid port speed\n");
  1447. priv->hw->ps = 0;
  1448. }
  1449. }
  1450. /* Initialize the MAC Core */
  1451. priv->hw->mac->core_init(priv->hw, dev->mtu);
  1452. ret = priv->hw->mac->rx_ipc(priv->hw);
  1453. if (!ret) {
  1454. pr_warn(" RX IPC Checksum Offload disabled\n");
  1455. priv->plat->rx_coe = STMMAC_RX_COE_NONE;
  1456. priv->hw->rx_csum = 0;
  1457. }
  1458. /* Enable the MAC Rx/Tx */
  1459. if (priv->synopsys_id >= DWMAC_CORE_4_00)
  1460. stmmac_dwmac4_set_mac(priv->ioaddr, true);
  1461. else
  1462. stmmac_set_mac(priv->ioaddr, true);
  1463. /* Set the HW DMA mode and the COE */
  1464. stmmac_dma_operation_mode(priv);
  1465. stmmac_mmc_setup(priv);
  1466. if (init_ptp) {
  1467. ret = stmmac_init_ptp(priv);
  1468. if (ret && ret != -EOPNOTSUPP)
  1469. pr_warn("%s: failed PTP initialisation\n", __func__);
  1470. }
  1471. #ifdef CONFIG_DEBUG_FS
  1472. ret = stmmac_init_fs(dev);
  1473. if (ret < 0)
  1474. pr_warn("%s: failed debugFS registration\n", __func__);
  1475. #endif
  1476. /* Start the ball rolling... */
  1477. pr_debug("%s: DMA RX/TX processes started...\n", dev->name);
  1478. priv->hw->dma->start_tx(priv->ioaddr);
  1479. priv->hw->dma->start_rx(priv->ioaddr);
  1480. /* Dump DMA/MAC registers */
  1481. if (netif_msg_hw(priv)) {
  1482. priv->hw->mac->dump_regs(priv->hw);
  1483. priv->hw->dma->dump_regs(priv->ioaddr);
  1484. }
  1485. priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
  1486. if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
  1487. priv->rx_riwt = MAX_DMA_RIWT;
  1488. priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
  1489. }
  1490. if (priv->hw->pcs && priv->hw->mac->pcs_ctrl_ane)
  1491. priv->hw->mac->pcs_ctrl_ane(priv->hw, 1, priv->hw->ps, 0);
  1492. /* set TX ring length */
  1493. if (priv->hw->dma->set_tx_ring_len)
  1494. priv->hw->dma->set_tx_ring_len(priv->ioaddr,
  1495. (DMA_TX_SIZE - 1));
  1496. /* set RX ring length */
  1497. if (priv->hw->dma->set_rx_ring_len)
  1498. priv->hw->dma->set_rx_ring_len(priv->ioaddr,
  1499. (DMA_RX_SIZE - 1));
  1500. /* Enable TSO */
  1501. if (priv->tso)
  1502. priv->hw->dma->enable_tso(priv->ioaddr, 1, STMMAC_CHAN0);
  1503. return 0;
  1504. }
  1505. /**
  1506. * stmmac_open - open entry point of the driver
  1507. * @dev : pointer to the device structure.
  1508. * Description:
  1509. * This function is the open entry point of the driver.
  1510. * Return value:
  1511. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1512. * file on failure.
  1513. */
  1514. static int stmmac_open(struct net_device *dev)
  1515. {
  1516. struct stmmac_priv *priv = netdev_priv(dev);
  1517. int ret;
  1518. stmmac_check_ether_addr(priv);
  1519. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  1520. priv->hw->pcs != STMMAC_PCS_TBI &&
  1521. priv->hw->pcs != STMMAC_PCS_RTBI) {
  1522. ret = stmmac_init_phy(dev);
  1523. if (ret) {
  1524. pr_err("%s: Cannot attach to PHY (error: %d)\n",
  1525. __func__, ret);
  1526. return ret;
  1527. }
  1528. }
  1529. /* Extra statistics */
  1530. memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
  1531. priv->xstats.threshold = tc;
  1532. priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
  1533. priv->rx_copybreak = STMMAC_RX_COPYBREAK;
  1534. ret = alloc_dma_desc_resources(priv);
  1535. if (ret < 0) {
  1536. pr_err("%s: DMA descriptors allocation failed\n", __func__);
  1537. goto dma_desc_error;
  1538. }
  1539. ret = init_dma_desc_rings(dev, GFP_KERNEL);
  1540. if (ret < 0) {
  1541. pr_err("%s: DMA descriptors initialization failed\n", __func__);
  1542. goto init_error;
  1543. }
  1544. ret = stmmac_hw_setup(dev, true);
  1545. if (ret < 0) {
  1546. pr_err("%s: Hw setup failed\n", __func__);
  1547. goto init_error;
  1548. }
  1549. stmmac_init_tx_coalesce(priv);
  1550. if (priv->phydev)
  1551. phy_start(priv->phydev);
  1552. /* Request the IRQ lines */
  1553. ret = request_irq(dev->irq, stmmac_interrupt,
  1554. IRQF_SHARED, dev->name, dev);
  1555. if (unlikely(ret < 0)) {
  1556. pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
  1557. __func__, dev->irq, ret);
  1558. goto init_error;
  1559. }
  1560. /* Request the Wake IRQ in case of another line is used for WoL */
  1561. if (priv->wol_irq != dev->irq) {
  1562. ret = request_irq(priv->wol_irq, stmmac_interrupt,
  1563. IRQF_SHARED, dev->name, dev);
  1564. if (unlikely(ret < 0)) {
  1565. pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
  1566. __func__, priv->wol_irq, ret);
  1567. goto wolirq_error;
  1568. }
  1569. }
  1570. /* Request the IRQ lines */
  1571. if (priv->lpi_irq > 0) {
  1572. ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
  1573. dev->name, dev);
  1574. if (unlikely(ret < 0)) {
  1575. pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
  1576. __func__, priv->lpi_irq, ret);
  1577. goto lpiirq_error;
  1578. }
  1579. }
  1580. napi_enable(&priv->napi);
  1581. netif_start_queue(dev);
  1582. return 0;
  1583. lpiirq_error:
  1584. if (priv->wol_irq != dev->irq)
  1585. free_irq(priv->wol_irq, dev);
  1586. wolirq_error:
  1587. free_irq(dev->irq, dev);
  1588. init_error:
  1589. free_dma_desc_resources(priv);
  1590. dma_desc_error:
  1591. if (priv->phydev)
  1592. phy_disconnect(priv->phydev);
  1593. return ret;
  1594. }
  1595. /**
  1596. * stmmac_release - close entry point of the driver
  1597. * @dev : device pointer.
  1598. * Description:
  1599. * This is the stop entry point of the driver.
  1600. */
  1601. static int stmmac_release(struct net_device *dev)
  1602. {
  1603. struct stmmac_priv *priv = netdev_priv(dev);
  1604. if (priv->eee_enabled)
  1605. del_timer_sync(&priv->eee_ctrl_timer);
  1606. /* Stop and disconnect the PHY */
  1607. if (priv->phydev) {
  1608. phy_stop(priv->phydev);
  1609. phy_disconnect(priv->phydev);
  1610. priv->phydev = NULL;
  1611. }
  1612. netif_stop_queue(dev);
  1613. napi_disable(&priv->napi);
  1614. del_timer_sync(&priv->txtimer);
  1615. /* Free the IRQ lines */
  1616. free_irq(dev->irq, dev);
  1617. if (priv->wol_irq != dev->irq)
  1618. free_irq(priv->wol_irq, dev);
  1619. if (priv->lpi_irq > 0)
  1620. free_irq(priv->lpi_irq, dev);
  1621. /* Stop TX/RX DMA and clear the descriptors */
  1622. priv->hw->dma->stop_tx(priv->ioaddr);
  1623. priv->hw->dma->stop_rx(priv->ioaddr);
  1624. /* Release and free the Rx/Tx resources */
  1625. free_dma_desc_resources(priv);
  1626. /* Disable the MAC Rx/Tx */
  1627. stmmac_set_mac(priv->ioaddr, false);
  1628. netif_carrier_off(dev);
  1629. #ifdef CONFIG_DEBUG_FS
  1630. stmmac_exit_fs(dev);
  1631. #endif
  1632. stmmac_release_ptp(priv);
  1633. return 0;
  1634. }
  1635. /**
  1636. * stmmac_tso_allocator - close entry point of the driver
  1637. * @priv: driver private structure
  1638. * @des: buffer start address
  1639. * @total_len: total length to fill in descriptors
  1640. * @last_segmant: condition for the last descriptor
  1641. * Description:
  1642. * This function fills descriptor and request new descriptors according to
  1643. * buffer length to fill
  1644. */
  1645. static void stmmac_tso_allocator(struct stmmac_priv *priv, unsigned int des,
  1646. int total_len, bool last_segment)
  1647. {
  1648. struct dma_desc *desc;
  1649. int tmp_len;
  1650. u32 buff_size;
  1651. tmp_len = total_len;
  1652. while (tmp_len > 0) {
  1653. priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
  1654. desc = priv->dma_tx + priv->cur_tx;
  1655. desc->des0 = des + (total_len - tmp_len);
  1656. buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
  1657. TSO_MAX_BUFF_SIZE : tmp_len;
  1658. priv->hw->desc->prepare_tso_tx_desc(desc, 0, buff_size,
  1659. 0, 1,
  1660. (last_segment) && (buff_size < TSO_MAX_BUFF_SIZE),
  1661. 0, 0);
  1662. tmp_len -= TSO_MAX_BUFF_SIZE;
  1663. }
  1664. }
  1665. /**
  1666. * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
  1667. * @skb : the socket buffer
  1668. * @dev : device pointer
  1669. * Description: this is the transmit function that is called on TSO frames
  1670. * (support available on GMAC4 and newer chips).
  1671. * Diagram below show the ring programming in case of TSO frames:
  1672. *
  1673. * First Descriptor
  1674. * --------
  1675. * | DES0 |---> buffer1 = L2/L3/L4 header
  1676. * | DES1 |---> TCP Payload (can continue on next descr...)
  1677. * | DES2 |---> buffer 1 and 2 len
  1678. * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
  1679. * --------
  1680. * |
  1681. * ...
  1682. * |
  1683. * --------
  1684. * | DES0 | --| Split TCP Payload on Buffers 1 and 2
  1685. * | DES1 | --|
  1686. * | DES2 | --> buffer 1 and 2 len
  1687. * | DES3 |
  1688. * --------
  1689. *
  1690. * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
  1691. */
  1692. static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
  1693. {
  1694. u32 pay_len, mss;
  1695. int tmp_pay_len = 0;
  1696. struct stmmac_priv *priv = netdev_priv(dev);
  1697. int nfrags = skb_shinfo(skb)->nr_frags;
  1698. unsigned int first_entry, des;
  1699. struct dma_desc *desc, *first, *mss_desc = NULL;
  1700. u8 proto_hdr_len;
  1701. int i;
  1702. spin_lock(&priv->tx_lock);
  1703. /* Compute header lengths */
  1704. proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1705. /* Desc availability based on threshold should be enough safe */
  1706. if (unlikely(stmmac_tx_avail(priv) <
  1707. (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
  1708. if (!netif_queue_stopped(dev)) {
  1709. netif_stop_queue(dev);
  1710. /* This is a hard error, log it. */
  1711. pr_err("%s: Tx Ring full when queue awake\n", __func__);
  1712. }
  1713. spin_unlock(&priv->tx_lock);
  1714. return NETDEV_TX_BUSY;
  1715. }
  1716. pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
  1717. mss = skb_shinfo(skb)->gso_size;
  1718. /* set new MSS value if needed */
  1719. if (mss != priv->mss) {
  1720. mss_desc = priv->dma_tx + priv->cur_tx;
  1721. priv->hw->desc->set_mss(mss_desc, mss);
  1722. priv->mss = mss;
  1723. priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
  1724. }
  1725. if (netif_msg_tx_queued(priv)) {
  1726. pr_info("%s: tcphdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
  1727. __func__, tcp_hdrlen(skb), proto_hdr_len, pay_len, mss);
  1728. pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
  1729. skb->data_len);
  1730. }
  1731. first_entry = priv->cur_tx;
  1732. desc = priv->dma_tx + first_entry;
  1733. first = desc;
  1734. /* first descriptor: fill Headers on Buf1 */
  1735. des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
  1736. DMA_TO_DEVICE);
  1737. if (dma_mapping_error(priv->device, des))
  1738. goto dma_map_err;
  1739. priv->tx_skbuff_dma[first_entry].buf = des;
  1740. priv->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
  1741. priv->tx_skbuff[first_entry] = skb;
  1742. first->des0 = des;
  1743. /* Fill start of payload in buff2 of first descriptor */
  1744. if (pay_len)
  1745. first->des1 = des + proto_hdr_len;
  1746. /* If needed take extra descriptors to fill the remaining payload */
  1747. tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
  1748. stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0));
  1749. /* Prepare fragments */
  1750. for (i = 0; i < nfrags; i++) {
  1751. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1752. des = skb_frag_dma_map(priv->device, frag, 0,
  1753. skb_frag_size(frag),
  1754. DMA_TO_DEVICE);
  1755. stmmac_tso_allocator(priv, des, skb_frag_size(frag),
  1756. (i == nfrags - 1));
  1757. priv->tx_skbuff_dma[priv->cur_tx].buf = des;
  1758. priv->tx_skbuff_dma[priv->cur_tx].len = skb_frag_size(frag);
  1759. priv->tx_skbuff[priv->cur_tx] = NULL;
  1760. priv->tx_skbuff_dma[priv->cur_tx].map_as_page = true;
  1761. }
  1762. priv->tx_skbuff_dma[priv->cur_tx].last_segment = true;
  1763. priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
  1764. if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
  1765. if (netif_msg_hw(priv))
  1766. pr_debug("%s: stop transmitted packets\n", __func__);
  1767. netif_stop_queue(dev);
  1768. }
  1769. dev->stats.tx_bytes += skb->len;
  1770. priv->xstats.tx_tso_frames++;
  1771. priv->xstats.tx_tso_nfrags += nfrags;
  1772. /* Manage tx mitigation */
  1773. priv->tx_count_frames += nfrags + 1;
  1774. if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
  1775. mod_timer(&priv->txtimer,
  1776. STMMAC_COAL_TIMER(priv->tx_coal_timer));
  1777. } else {
  1778. priv->tx_count_frames = 0;
  1779. priv->hw->desc->set_tx_ic(desc);
  1780. priv->xstats.tx_set_ic_bit++;
  1781. }
  1782. if (!priv->hwts_tx_en)
  1783. skb_tx_timestamp(skb);
  1784. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1785. priv->hwts_tx_en)) {
  1786. /* declare that device is doing timestamping */
  1787. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1788. priv->hw->desc->enable_tx_timestamp(first);
  1789. }
  1790. /* Complete the first descriptor before granting the DMA */
  1791. priv->hw->desc->prepare_tso_tx_desc(first, 1,
  1792. proto_hdr_len,
  1793. pay_len,
  1794. 1, priv->tx_skbuff_dma[first_entry].last_segment,
  1795. tcp_hdrlen(skb) / 4, (skb->len - proto_hdr_len));
  1796. /* If context desc is used to change MSS */
  1797. if (mss_desc)
  1798. priv->hw->desc->set_tx_owner(mss_desc);
  1799. /* The own bit must be the latest setting done when prepare the
  1800. * descriptor and then barrier is needed to make sure that
  1801. * all is coherent before granting the DMA engine.
  1802. */
  1803. smp_wmb();
  1804. if (netif_msg_pktdata(priv)) {
  1805. pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
  1806. __func__, priv->cur_tx, priv->dirty_tx, first_entry,
  1807. priv->cur_tx, first, nfrags);
  1808. priv->hw->desc->display_ring((void *)priv->dma_tx, DMA_TX_SIZE,
  1809. 0);
  1810. pr_info(">>> frame to be transmitted: ");
  1811. print_pkt(skb->data, skb_headlen(skb));
  1812. }
  1813. netdev_sent_queue(dev, skb->len);
  1814. priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
  1815. STMMAC_CHAN0);
  1816. spin_unlock(&priv->tx_lock);
  1817. return NETDEV_TX_OK;
  1818. dma_map_err:
  1819. spin_unlock(&priv->tx_lock);
  1820. dev_err(priv->device, "Tx dma map failed\n");
  1821. dev_kfree_skb(skb);
  1822. priv->dev->stats.tx_dropped++;
  1823. return NETDEV_TX_OK;
  1824. }
  1825. /**
  1826. * stmmac_xmit - Tx entry point of the driver
  1827. * @skb : the socket buffer
  1828. * @dev : device pointer
  1829. * Description : this is the tx entry point of the driver.
  1830. * It programs the chain or the ring and supports oversized frames
  1831. * and SG feature.
  1832. */
  1833. static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
  1834. {
  1835. struct stmmac_priv *priv = netdev_priv(dev);
  1836. unsigned int nopaged_len = skb_headlen(skb);
  1837. int i, csum_insertion = 0, is_jumbo = 0;
  1838. int nfrags = skb_shinfo(skb)->nr_frags;
  1839. unsigned int entry, first_entry;
  1840. struct dma_desc *desc, *first;
  1841. unsigned int enh_desc;
  1842. unsigned int des;
  1843. /* Manage oversized TCP frames for GMAC4 device */
  1844. if (skb_is_gso(skb) && priv->tso) {
  1845. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  1846. return stmmac_tso_xmit(skb, dev);
  1847. }
  1848. spin_lock(&priv->tx_lock);
  1849. if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
  1850. spin_unlock(&priv->tx_lock);
  1851. if (!netif_queue_stopped(dev)) {
  1852. netif_stop_queue(dev);
  1853. /* This is a hard error, log it. */
  1854. pr_err("%s: Tx Ring full when queue awake\n", __func__);
  1855. }
  1856. return NETDEV_TX_BUSY;
  1857. }
  1858. if (priv->tx_path_in_lpi_mode)
  1859. stmmac_disable_eee_mode(priv);
  1860. entry = priv->cur_tx;
  1861. first_entry = entry;
  1862. csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
  1863. if (likely(priv->extend_desc))
  1864. desc = (struct dma_desc *)(priv->dma_etx + entry);
  1865. else
  1866. desc = priv->dma_tx + entry;
  1867. first = desc;
  1868. priv->tx_skbuff[first_entry] = skb;
  1869. enh_desc = priv->plat->enh_desc;
  1870. /* To program the descriptors according to the size of the frame */
  1871. if (enh_desc)
  1872. is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
  1873. if (unlikely(is_jumbo) && likely(priv->synopsys_id <
  1874. DWMAC_CORE_4_00)) {
  1875. entry = priv->hw->mode->jumbo_frm(priv, skb, csum_insertion);
  1876. if (unlikely(entry < 0))
  1877. goto dma_map_err;
  1878. }
  1879. for (i = 0; i < nfrags; i++) {
  1880. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1881. int len = skb_frag_size(frag);
  1882. bool last_segment = (i == (nfrags - 1));
  1883. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  1884. if (likely(priv->extend_desc))
  1885. desc = (struct dma_desc *)(priv->dma_etx + entry);
  1886. else
  1887. desc = priv->dma_tx + entry;
  1888. des = skb_frag_dma_map(priv->device, frag, 0, len,
  1889. DMA_TO_DEVICE);
  1890. if (dma_mapping_error(priv->device, des))
  1891. goto dma_map_err; /* should reuse desc w/o issues */
  1892. priv->tx_skbuff[entry] = NULL;
  1893. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00)) {
  1894. desc->des0 = des;
  1895. priv->tx_skbuff_dma[entry].buf = desc->des0;
  1896. } else {
  1897. desc->des2 = des;
  1898. priv->tx_skbuff_dma[entry].buf = desc->des2;
  1899. }
  1900. priv->tx_skbuff_dma[entry].map_as_page = true;
  1901. priv->tx_skbuff_dma[entry].len = len;
  1902. priv->tx_skbuff_dma[entry].last_segment = last_segment;
  1903. /* Prepare the descriptor and set the own bit too */
  1904. priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
  1905. priv->mode, 1, last_segment);
  1906. }
  1907. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  1908. priv->cur_tx = entry;
  1909. if (netif_msg_pktdata(priv)) {
  1910. void *tx_head;
  1911. pr_debug("%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
  1912. __func__, priv->cur_tx, priv->dirty_tx, first_entry,
  1913. entry, first, nfrags);
  1914. if (priv->extend_desc)
  1915. tx_head = (void *)priv->dma_etx;
  1916. else
  1917. tx_head = (void *)priv->dma_tx;
  1918. priv->hw->desc->display_ring(tx_head, DMA_TX_SIZE, false);
  1919. pr_debug(">>> frame to be transmitted: ");
  1920. print_pkt(skb->data, skb->len);
  1921. }
  1922. if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
  1923. if (netif_msg_hw(priv))
  1924. pr_debug("%s: stop transmitted packets\n", __func__);
  1925. netif_stop_queue(dev);
  1926. }
  1927. dev->stats.tx_bytes += skb->len;
  1928. /* According to the coalesce parameter the IC bit for the latest
  1929. * segment is reset and the timer re-started to clean the tx status.
  1930. * This approach takes care about the fragments: desc is the first
  1931. * element in case of no SG.
  1932. */
  1933. priv->tx_count_frames += nfrags + 1;
  1934. if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
  1935. mod_timer(&priv->txtimer,
  1936. STMMAC_COAL_TIMER(priv->tx_coal_timer));
  1937. } else {
  1938. priv->tx_count_frames = 0;
  1939. priv->hw->desc->set_tx_ic(desc);
  1940. priv->xstats.tx_set_ic_bit++;
  1941. }
  1942. if (!priv->hwts_tx_en)
  1943. skb_tx_timestamp(skb);
  1944. /* Ready to fill the first descriptor and set the OWN bit w/o any
  1945. * problems because all the descriptors are actually ready to be
  1946. * passed to the DMA engine.
  1947. */
  1948. if (likely(!is_jumbo)) {
  1949. bool last_segment = (nfrags == 0);
  1950. des = dma_map_single(priv->device, skb->data,
  1951. nopaged_len, DMA_TO_DEVICE);
  1952. if (dma_mapping_error(priv->device, des))
  1953. goto dma_map_err;
  1954. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00)) {
  1955. first->des0 = des;
  1956. priv->tx_skbuff_dma[first_entry].buf = first->des0;
  1957. } else {
  1958. first->des2 = des;
  1959. priv->tx_skbuff_dma[first_entry].buf = first->des2;
  1960. }
  1961. priv->tx_skbuff_dma[first_entry].len = nopaged_len;
  1962. priv->tx_skbuff_dma[first_entry].last_segment = last_segment;
  1963. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1964. priv->hwts_tx_en)) {
  1965. /* declare that device is doing timestamping */
  1966. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1967. priv->hw->desc->enable_tx_timestamp(first);
  1968. }
  1969. /* Prepare the first descriptor setting the OWN bit too */
  1970. priv->hw->desc->prepare_tx_desc(first, 1, nopaged_len,
  1971. csum_insertion, priv->mode, 1,
  1972. last_segment);
  1973. /* The own bit must be the latest setting done when prepare the
  1974. * descriptor and then barrier is needed to make sure that
  1975. * all is coherent before granting the DMA engine.
  1976. */
  1977. smp_wmb();
  1978. }
  1979. netdev_sent_queue(dev, skb->len);
  1980. if (priv->synopsys_id < DWMAC_CORE_4_00)
  1981. priv->hw->dma->enable_dma_transmission(priv->ioaddr);
  1982. else
  1983. priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
  1984. STMMAC_CHAN0);
  1985. spin_unlock(&priv->tx_lock);
  1986. return NETDEV_TX_OK;
  1987. dma_map_err:
  1988. spin_unlock(&priv->tx_lock);
  1989. dev_err(priv->device, "Tx dma map failed\n");
  1990. dev_kfree_skb(skb);
  1991. priv->dev->stats.tx_dropped++;
  1992. return NETDEV_TX_OK;
  1993. }
  1994. static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
  1995. {
  1996. struct ethhdr *ehdr;
  1997. u16 vlanid;
  1998. if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
  1999. NETIF_F_HW_VLAN_CTAG_RX &&
  2000. !__vlan_get_tag(skb, &vlanid)) {
  2001. /* pop the vlan tag */
  2002. ehdr = (struct ethhdr *)skb->data;
  2003. memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
  2004. skb_pull(skb, VLAN_HLEN);
  2005. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
  2006. }
  2007. }
  2008. static inline int stmmac_rx_threshold_count(struct stmmac_priv *priv)
  2009. {
  2010. if (priv->rx_zeroc_thresh < STMMAC_RX_THRESH)
  2011. return 0;
  2012. return 1;
  2013. }
  2014. /**
  2015. * stmmac_rx_refill - refill used skb preallocated buffers
  2016. * @priv: driver private structure
  2017. * Description : this is to reallocate the skb for the reception process
  2018. * that is based on zero-copy.
  2019. */
  2020. static inline void stmmac_rx_refill(struct stmmac_priv *priv)
  2021. {
  2022. int bfsize = priv->dma_buf_sz;
  2023. unsigned int entry = priv->dirty_rx;
  2024. int dirty = stmmac_rx_dirty(priv);
  2025. while (dirty-- > 0) {
  2026. struct dma_desc *p;
  2027. if (priv->extend_desc)
  2028. p = (struct dma_desc *)(priv->dma_erx + entry);
  2029. else
  2030. p = priv->dma_rx + entry;
  2031. if (likely(priv->rx_skbuff[entry] == NULL)) {
  2032. struct sk_buff *skb;
  2033. skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
  2034. if (unlikely(!skb)) {
  2035. /* so for a while no zero-copy! */
  2036. priv->rx_zeroc_thresh = STMMAC_RX_THRESH;
  2037. if (unlikely(net_ratelimit()))
  2038. dev_err(priv->device,
  2039. "fail to alloc skb entry %d\n",
  2040. entry);
  2041. break;
  2042. }
  2043. priv->rx_skbuff[entry] = skb;
  2044. priv->rx_skbuff_dma[entry] =
  2045. dma_map_single(priv->device, skb->data, bfsize,
  2046. DMA_FROM_DEVICE);
  2047. if (dma_mapping_error(priv->device,
  2048. priv->rx_skbuff_dma[entry])) {
  2049. dev_err(priv->device, "Rx dma map failed\n");
  2050. dev_kfree_skb(skb);
  2051. break;
  2052. }
  2053. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00)) {
  2054. p->des0 = priv->rx_skbuff_dma[entry];
  2055. p->des1 = 0;
  2056. } else {
  2057. p->des2 = priv->rx_skbuff_dma[entry];
  2058. }
  2059. if (priv->hw->mode->refill_desc3)
  2060. priv->hw->mode->refill_desc3(priv, p);
  2061. if (priv->rx_zeroc_thresh > 0)
  2062. priv->rx_zeroc_thresh--;
  2063. if (netif_msg_rx_status(priv))
  2064. pr_debug("\trefill entry #%d\n", entry);
  2065. }
  2066. wmb();
  2067. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
  2068. priv->hw->desc->init_rx_desc(p, priv->use_riwt, 0, 0);
  2069. else
  2070. priv->hw->desc->set_rx_owner(p);
  2071. wmb();
  2072. entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
  2073. }
  2074. priv->dirty_rx = entry;
  2075. }
  2076. /**
  2077. * stmmac_rx - manage the receive process
  2078. * @priv: driver private structure
  2079. * @limit: napi bugget.
  2080. * Description : this the function called by the napi poll method.
  2081. * It gets all the frames inside the ring.
  2082. */
  2083. static int stmmac_rx(struct stmmac_priv *priv, int limit)
  2084. {
  2085. unsigned int entry = priv->cur_rx;
  2086. unsigned int next_entry;
  2087. unsigned int count = 0;
  2088. int coe = priv->hw->rx_csum;
  2089. if (netif_msg_rx_status(priv)) {
  2090. void *rx_head;
  2091. pr_debug("%s: descriptor ring:\n", __func__);
  2092. if (priv->extend_desc)
  2093. rx_head = (void *)priv->dma_erx;
  2094. else
  2095. rx_head = (void *)priv->dma_rx;
  2096. priv->hw->desc->display_ring(rx_head, DMA_RX_SIZE, true);
  2097. }
  2098. while (count < limit) {
  2099. int status;
  2100. struct dma_desc *p;
  2101. if (priv->extend_desc)
  2102. p = (struct dma_desc *)(priv->dma_erx + entry);
  2103. else
  2104. p = priv->dma_rx + entry;
  2105. /* read the status of the incoming frame */
  2106. status = priv->hw->desc->rx_status(&priv->dev->stats,
  2107. &priv->xstats, p);
  2108. /* check if managed by the DMA otherwise go ahead */
  2109. if (unlikely(status & dma_own))
  2110. break;
  2111. count++;
  2112. priv->cur_rx = STMMAC_GET_ENTRY(priv->cur_rx, DMA_RX_SIZE);
  2113. next_entry = priv->cur_rx;
  2114. if (priv->extend_desc)
  2115. prefetch(priv->dma_erx + next_entry);
  2116. else
  2117. prefetch(priv->dma_rx + next_entry);
  2118. if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
  2119. priv->hw->desc->rx_extended_status(&priv->dev->stats,
  2120. &priv->xstats,
  2121. priv->dma_erx +
  2122. entry);
  2123. if (unlikely(status == discard_frame)) {
  2124. priv->dev->stats.rx_errors++;
  2125. if (priv->hwts_rx_en && !priv->extend_desc) {
  2126. /* DESC2 & DESC3 will be overwitten by device
  2127. * with timestamp value, hence reinitialize
  2128. * them in stmmac_rx_refill() function so that
  2129. * device can reuse it.
  2130. */
  2131. priv->rx_skbuff[entry] = NULL;
  2132. dma_unmap_single(priv->device,
  2133. priv->rx_skbuff_dma[entry],
  2134. priv->dma_buf_sz,
  2135. DMA_FROM_DEVICE);
  2136. }
  2137. } else {
  2138. struct sk_buff *skb;
  2139. int frame_len;
  2140. unsigned int des;
  2141. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
  2142. des = p->des0;
  2143. else
  2144. des = p->des2;
  2145. frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
  2146. /* If frame length is greather than skb buffer size
  2147. * (preallocated during init) then the packet is
  2148. * ignored
  2149. */
  2150. if (frame_len > priv->dma_buf_sz) {
  2151. pr_err("%s: len %d larger than size (%d)\n",
  2152. priv->dev->name, frame_len,
  2153. priv->dma_buf_sz);
  2154. priv->dev->stats.rx_length_errors++;
  2155. break;
  2156. }
  2157. /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
  2158. * Type frames (LLC/LLC-SNAP)
  2159. */
  2160. if (unlikely(status != llc_snap))
  2161. frame_len -= ETH_FCS_LEN;
  2162. if (netif_msg_rx_status(priv)) {
  2163. pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
  2164. p, entry, des);
  2165. if (frame_len > ETH_FRAME_LEN)
  2166. pr_debug("\tframe size %d, COE: %d\n",
  2167. frame_len, status);
  2168. }
  2169. /* The zero-copy is always used for all the sizes
  2170. * in case of GMAC4 because it needs
  2171. * to refill the used descriptors, always.
  2172. */
  2173. if (unlikely(!priv->plat->has_gmac4 &&
  2174. ((frame_len < priv->rx_copybreak) ||
  2175. stmmac_rx_threshold_count(priv)))) {
  2176. skb = netdev_alloc_skb_ip_align(priv->dev,
  2177. frame_len);
  2178. if (unlikely(!skb)) {
  2179. if (net_ratelimit())
  2180. dev_warn(priv->device,
  2181. "packet dropped\n");
  2182. priv->dev->stats.rx_dropped++;
  2183. break;
  2184. }
  2185. dma_sync_single_for_cpu(priv->device,
  2186. priv->rx_skbuff_dma
  2187. [entry], frame_len,
  2188. DMA_FROM_DEVICE);
  2189. skb_copy_to_linear_data(skb,
  2190. priv->
  2191. rx_skbuff[entry]->data,
  2192. frame_len);
  2193. skb_put(skb, frame_len);
  2194. dma_sync_single_for_device(priv->device,
  2195. priv->rx_skbuff_dma
  2196. [entry], frame_len,
  2197. DMA_FROM_DEVICE);
  2198. } else {
  2199. skb = priv->rx_skbuff[entry];
  2200. if (unlikely(!skb)) {
  2201. pr_err("%s: Inconsistent Rx chain\n",
  2202. priv->dev->name);
  2203. priv->dev->stats.rx_dropped++;
  2204. break;
  2205. }
  2206. prefetch(skb->data - NET_IP_ALIGN);
  2207. priv->rx_skbuff[entry] = NULL;
  2208. priv->rx_zeroc_thresh++;
  2209. skb_put(skb, frame_len);
  2210. dma_unmap_single(priv->device,
  2211. priv->rx_skbuff_dma[entry],
  2212. priv->dma_buf_sz,
  2213. DMA_FROM_DEVICE);
  2214. }
  2215. stmmac_get_rx_hwtstamp(priv, entry, skb);
  2216. if (netif_msg_pktdata(priv)) {
  2217. pr_debug("frame received (%dbytes)", frame_len);
  2218. print_pkt(skb->data, frame_len);
  2219. }
  2220. stmmac_rx_vlan(priv->dev, skb);
  2221. skb->protocol = eth_type_trans(skb, priv->dev);
  2222. if (unlikely(!coe))
  2223. skb_checksum_none_assert(skb);
  2224. else
  2225. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2226. napi_gro_receive(&priv->napi, skb);
  2227. priv->dev->stats.rx_packets++;
  2228. priv->dev->stats.rx_bytes += frame_len;
  2229. }
  2230. entry = next_entry;
  2231. }
  2232. stmmac_rx_refill(priv);
  2233. priv->xstats.rx_pkt_n += count;
  2234. return count;
  2235. }
  2236. /**
  2237. * stmmac_poll - stmmac poll method (NAPI)
  2238. * @napi : pointer to the napi structure.
  2239. * @budget : maximum number of packets that the current CPU can receive from
  2240. * all interfaces.
  2241. * Description :
  2242. * To look at the incoming frames and clear the tx resources.
  2243. */
  2244. static int stmmac_poll(struct napi_struct *napi, int budget)
  2245. {
  2246. struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
  2247. int work_done = 0;
  2248. priv->xstats.napi_poll++;
  2249. stmmac_tx_clean(priv);
  2250. work_done = stmmac_rx(priv, budget);
  2251. if (work_done < budget) {
  2252. napi_complete(napi);
  2253. stmmac_enable_dma_irq(priv);
  2254. }
  2255. return work_done;
  2256. }
  2257. /**
  2258. * stmmac_tx_timeout
  2259. * @dev : Pointer to net device structure
  2260. * Description: this function is called when a packet transmission fails to
  2261. * complete within a reasonable time. The driver will mark the error in the
  2262. * netdev structure and arrange for the device to be reset to a sane state
  2263. * in order to transmit a new packet.
  2264. */
  2265. static void stmmac_tx_timeout(struct net_device *dev)
  2266. {
  2267. struct stmmac_priv *priv = netdev_priv(dev);
  2268. /* Clear Tx resources and restart transmitting again */
  2269. stmmac_tx_err(priv);
  2270. }
  2271. /**
  2272. * stmmac_set_rx_mode - entry point for multicast addressing
  2273. * @dev : pointer to the device structure
  2274. * Description:
  2275. * This function is a driver entry point which gets called by the kernel
  2276. * whenever multicast addresses must be enabled/disabled.
  2277. * Return value:
  2278. * void.
  2279. */
  2280. static void stmmac_set_rx_mode(struct net_device *dev)
  2281. {
  2282. struct stmmac_priv *priv = netdev_priv(dev);
  2283. priv->hw->mac->set_filter(priv->hw, dev);
  2284. }
  2285. /**
  2286. * stmmac_change_mtu - entry point to change MTU size for the device.
  2287. * @dev : device pointer.
  2288. * @new_mtu : the new MTU size for the device.
  2289. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  2290. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  2291. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  2292. * Return value:
  2293. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  2294. * file on failure.
  2295. */
  2296. static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
  2297. {
  2298. struct stmmac_priv *priv = netdev_priv(dev);
  2299. int max_mtu;
  2300. if (netif_running(dev)) {
  2301. pr_err("%s: must be stopped to change its MTU\n", dev->name);
  2302. return -EBUSY;
  2303. }
  2304. if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
  2305. max_mtu = JUMBO_LEN;
  2306. else
  2307. max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
  2308. if (priv->plat->maxmtu < max_mtu)
  2309. max_mtu = priv->plat->maxmtu;
  2310. if ((new_mtu < 46) || (new_mtu > max_mtu)) {
  2311. pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
  2312. return -EINVAL;
  2313. }
  2314. dev->mtu = new_mtu;
  2315. netdev_update_features(dev);
  2316. return 0;
  2317. }
  2318. static netdev_features_t stmmac_fix_features(struct net_device *dev,
  2319. netdev_features_t features)
  2320. {
  2321. struct stmmac_priv *priv = netdev_priv(dev);
  2322. if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
  2323. features &= ~NETIF_F_RXCSUM;
  2324. if (!priv->plat->tx_coe)
  2325. features &= ~NETIF_F_CSUM_MASK;
  2326. /* Some GMAC devices have a bugged Jumbo frame support that
  2327. * needs to have the Tx COE disabled for oversized frames
  2328. * (due to limited buffer sizes). In this case we disable
  2329. * the TX csum insertionin the TDES and not use SF.
  2330. */
  2331. if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
  2332. features &= ~NETIF_F_CSUM_MASK;
  2333. /* Disable tso if asked by ethtool */
  2334. if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
  2335. if (features & NETIF_F_TSO)
  2336. priv->tso = true;
  2337. else
  2338. priv->tso = false;
  2339. }
  2340. return features;
  2341. }
  2342. static int stmmac_set_features(struct net_device *netdev,
  2343. netdev_features_t features)
  2344. {
  2345. struct stmmac_priv *priv = netdev_priv(netdev);
  2346. /* Keep the COE Type in case of csum is supporting */
  2347. if (features & NETIF_F_RXCSUM)
  2348. priv->hw->rx_csum = priv->plat->rx_coe;
  2349. else
  2350. priv->hw->rx_csum = 0;
  2351. /* No check needed because rx_coe has been set before and it will be
  2352. * fixed in case of issue.
  2353. */
  2354. priv->hw->mac->rx_ipc(priv->hw);
  2355. return 0;
  2356. }
  2357. /**
  2358. * stmmac_interrupt - main ISR
  2359. * @irq: interrupt number.
  2360. * @dev_id: to pass the net device pointer.
  2361. * Description: this is the main driver interrupt service routine.
  2362. * It can call:
  2363. * o DMA service routine (to manage incoming frame reception and transmission
  2364. * status)
  2365. * o Core interrupts to manage: remote wake-up, management counter, LPI
  2366. * interrupts.
  2367. */
  2368. static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
  2369. {
  2370. struct net_device *dev = (struct net_device *)dev_id;
  2371. struct stmmac_priv *priv = netdev_priv(dev);
  2372. if (priv->irq_wake)
  2373. pm_wakeup_event(priv->device, 0);
  2374. if (unlikely(!dev)) {
  2375. pr_err("%s: invalid dev pointer\n", __func__);
  2376. return IRQ_NONE;
  2377. }
  2378. /* To handle GMAC own interrupts */
  2379. if ((priv->plat->has_gmac) || (priv->plat->has_gmac4)) {
  2380. int status = priv->hw->mac->host_irq_status(priv->hw,
  2381. &priv->xstats);
  2382. if (unlikely(status)) {
  2383. /* For LPI we need to save the tx status */
  2384. if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
  2385. priv->tx_path_in_lpi_mode = true;
  2386. if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
  2387. priv->tx_path_in_lpi_mode = false;
  2388. if (status & CORE_IRQ_MTL_RX_OVERFLOW && priv->hw->dma->set_rx_tail_ptr)
  2389. priv->hw->dma->set_rx_tail_ptr(priv->ioaddr,
  2390. priv->rx_tail_addr,
  2391. STMMAC_CHAN0);
  2392. }
  2393. /* PCS link status */
  2394. if (priv->hw->pcs) {
  2395. if (priv->xstats.pcs_link)
  2396. netif_carrier_on(dev);
  2397. else
  2398. netif_carrier_off(dev);
  2399. }
  2400. }
  2401. /* To handle DMA interrupts */
  2402. stmmac_dma_interrupt(priv);
  2403. return IRQ_HANDLED;
  2404. }
  2405. #ifdef CONFIG_NET_POLL_CONTROLLER
  2406. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  2407. * to allow network I/O with interrupts disabled.
  2408. */
  2409. static void stmmac_poll_controller(struct net_device *dev)
  2410. {
  2411. disable_irq(dev->irq);
  2412. stmmac_interrupt(dev->irq, dev);
  2413. enable_irq(dev->irq);
  2414. }
  2415. #endif
  2416. /**
  2417. * stmmac_ioctl - Entry point for the Ioctl
  2418. * @dev: Device pointer.
  2419. * @rq: An IOCTL specefic structure, that can contain a pointer to
  2420. * a proprietary structure used to pass information to the driver.
  2421. * @cmd: IOCTL command
  2422. * Description:
  2423. * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
  2424. */
  2425. static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2426. {
  2427. struct stmmac_priv *priv = netdev_priv(dev);
  2428. int ret = -EOPNOTSUPP;
  2429. if (!netif_running(dev))
  2430. return -EINVAL;
  2431. switch (cmd) {
  2432. case SIOCGMIIPHY:
  2433. case SIOCGMIIREG:
  2434. case SIOCSMIIREG:
  2435. if (!priv->phydev)
  2436. return -EINVAL;
  2437. ret = phy_mii_ioctl(priv->phydev, rq, cmd);
  2438. break;
  2439. case SIOCSHWTSTAMP:
  2440. ret = stmmac_hwtstamp_ioctl(dev, rq);
  2441. break;
  2442. default:
  2443. break;
  2444. }
  2445. return ret;
  2446. }
  2447. #ifdef CONFIG_DEBUG_FS
  2448. static struct dentry *stmmac_fs_dir;
  2449. static void sysfs_display_ring(void *head, int size, int extend_desc,
  2450. struct seq_file *seq)
  2451. {
  2452. int i;
  2453. struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
  2454. struct dma_desc *p = (struct dma_desc *)head;
  2455. for (i = 0; i < size; i++) {
  2456. u64 x;
  2457. if (extend_desc) {
  2458. x = *(u64 *) ep;
  2459. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  2460. i, (unsigned int)virt_to_phys(ep),
  2461. ep->basic.des0, ep->basic.des1,
  2462. ep->basic.des2, ep->basic.des3);
  2463. ep++;
  2464. } else {
  2465. x = *(u64 *) p;
  2466. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  2467. i, (unsigned int)virt_to_phys(ep),
  2468. p->des0, p->des1, p->des2, p->des3);
  2469. p++;
  2470. }
  2471. seq_printf(seq, "\n");
  2472. }
  2473. }
  2474. static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
  2475. {
  2476. struct net_device *dev = seq->private;
  2477. struct stmmac_priv *priv = netdev_priv(dev);
  2478. if (priv->extend_desc) {
  2479. seq_printf(seq, "Extended RX descriptor ring:\n");
  2480. sysfs_display_ring((void *)priv->dma_erx, DMA_RX_SIZE, 1, seq);
  2481. seq_printf(seq, "Extended TX descriptor ring:\n");
  2482. sysfs_display_ring((void *)priv->dma_etx, DMA_TX_SIZE, 1, seq);
  2483. } else {
  2484. seq_printf(seq, "RX descriptor ring:\n");
  2485. sysfs_display_ring((void *)priv->dma_rx, DMA_RX_SIZE, 0, seq);
  2486. seq_printf(seq, "TX descriptor ring:\n");
  2487. sysfs_display_ring((void *)priv->dma_tx, DMA_TX_SIZE, 0, seq);
  2488. }
  2489. return 0;
  2490. }
  2491. static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
  2492. {
  2493. return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
  2494. }
  2495. static const struct file_operations stmmac_rings_status_fops = {
  2496. .owner = THIS_MODULE,
  2497. .open = stmmac_sysfs_ring_open,
  2498. .read = seq_read,
  2499. .llseek = seq_lseek,
  2500. .release = single_release,
  2501. };
  2502. static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
  2503. {
  2504. struct net_device *dev = seq->private;
  2505. struct stmmac_priv *priv = netdev_priv(dev);
  2506. if (!priv->hw_cap_support) {
  2507. seq_printf(seq, "DMA HW features not supported\n");
  2508. return 0;
  2509. }
  2510. seq_printf(seq, "==============================\n");
  2511. seq_printf(seq, "\tDMA HW features\n");
  2512. seq_printf(seq, "==============================\n");
  2513. seq_printf(seq, "\t10/100 Mbps %s\n",
  2514. (priv->dma_cap.mbps_10_100) ? "Y" : "N");
  2515. seq_printf(seq, "\t1000 Mbps %s\n",
  2516. (priv->dma_cap.mbps_1000) ? "Y" : "N");
  2517. seq_printf(seq, "\tHalf duple %s\n",
  2518. (priv->dma_cap.half_duplex) ? "Y" : "N");
  2519. seq_printf(seq, "\tHash Filter: %s\n",
  2520. (priv->dma_cap.hash_filter) ? "Y" : "N");
  2521. seq_printf(seq, "\tMultiple MAC address registers: %s\n",
  2522. (priv->dma_cap.multi_addr) ? "Y" : "N");
  2523. seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
  2524. (priv->dma_cap.pcs) ? "Y" : "N");
  2525. seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
  2526. (priv->dma_cap.sma_mdio) ? "Y" : "N");
  2527. seq_printf(seq, "\tPMT Remote wake up: %s\n",
  2528. (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
  2529. seq_printf(seq, "\tPMT Magic Frame: %s\n",
  2530. (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
  2531. seq_printf(seq, "\tRMON module: %s\n",
  2532. (priv->dma_cap.rmon) ? "Y" : "N");
  2533. seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
  2534. (priv->dma_cap.time_stamp) ? "Y" : "N");
  2535. seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
  2536. (priv->dma_cap.atime_stamp) ? "Y" : "N");
  2537. seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
  2538. (priv->dma_cap.eee) ? "Y" : "N");
  2539. seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
  2540. seq_printf(seq, "\tChecksum Offload in TX: %s\n",
  2541. (priv->dma_cap.tx_coe) ? "Y" : "N");
  2542. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  2543. seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
  2544. (priv->dma_cap.rx_coe) ? "Y" : "N");
  2545. } else {
  2546. seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
  2547. (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
  2548. seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
  2549. (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
  2550. }
  2551. seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
  2552. (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
  2553. seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
  2554. priv->dma_cap.number_rx_channel);
  2555. seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
  2556. priv->dma_cap.number_tx_channel);
  2557. seq_printf(seq, "\tEnhanced descriptors: %s\n",
  2558. (priv->dma_cap.enh_desc) ? "Y" : "N");
  2559. return 0;
  2560. }
  2561. static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
  2562. {
  2563. return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
  2564. }
  2565. static const struct file_operations stmmac_dma_cap_fops = {
  2566. .owner = THIS_MODULE,
  2567. .open = stmmac_sysfs_dma_cap_open,
  2568. .read = seq_read,
  2569. .llseek = seq_lseek,
  2570. .release = single_release,
  2571. };
  2572. static int stmmac_init_fs(struct net_device *dev)
  2573. {
  2574. struct stmmac_priv *priv = netdev_priv(dev);
  2575. /* Create per netdev entries */
  2576. priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
  2577. if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
  2578. pr_err("ERROR %s/%s, debugfs create directory failed\n",
  2579. STMMAC_RESOURCE_NAME, dev->name);
  2580. return -ENOMEM;
  2581. }
  2582. /* Entry to report DMA RX/TX rings */
  2583. priv->dbgfs_rings_status =
  2584. debugfs_create_file("descriptors_status", S_IRUGO,
  2585. priv->dbgfs_dir, dev,
  2586. &stmmac_rings_status_fops);
  2587. if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
  2588. pr_info("ERROR creating stmmac ring debugfs file\n");
  2589. debugfs_remove_recursive(priv->dbgfs_dir);
  2590. return -ENOMEM;
  2591. }
  2592. /* Entry to report the DMA HW features */
  2593. priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", S_IRUGO,
  2594. priv->dbgfs_dir,
  2595. dev, &stmmac_dma_cap_fops);
  2596. if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
  2597. pr_info("ERROR creating stmmac MMC debugfs file\n");
  2598. debugfs_remove_recursive(priv->dbgfs_dir);
  2599. return -ENOMEM;
  2600. }
  2601. return 0;
  2602. }
  2603. static void stmmac_exit_fs(struct net_device *dev)
  2604. {
  2605. struct stmmac_priv *priv = netdev_priv(dev);
  2606. debugfs_remove_recursive(priv->dbgfs_dir);
  2607. }
  2608. #endif /* CONFIG_DEBUG_FS */
  2609. static const struct net_device_ops stmmac_netdev_ops = {
  2610. .ndo_open = stmmac_open,
  2611. .ndo_start_xmit = stmmac_xmit,
  2612. .ndo_stop = stmmac_release,
  2613. .ndo_change_mtu = stmmac_change_mtu,
  2614. .ndo_fix_features = stmmac_fix_features,
  2615. .ndo_set_features = stmmac_set_features,
  2616. .ndo_set_rx_mode = stmmac_set_rx_mode,
  2617. .ndo_tx_timeout = stmmac_tx_timeout,
  2618. .ndo_do_ioctl = stmmac_ioctl,
  2619. #ifdef CONFIG_NET_POLL_CONTROLLER
  2620. .ndo_poll_controller = stmmac_poll_controller,
  2621. #endif
  2622. .ndo_set_mac_address = eth_mac_addr,
  2623. };
  2624. /**
  2625. * stmmac_hw_init - Init the MAC device
  2626. * @priv: driver private structure
  2627. * Description: this function is to configure the MAC device according to
  2628. * some platform parameters or the HW capability register. It prepares the
  2629. * driver to use either ring or chain modes and to setup either enhanced or
  2630. * normal descriptors.
  2631. */
  2632. static int stmmac_hw_init(struct stmmac_priv *priv)
  2633. {
  2634. struct mac_device_info *mac;
  2635. /* Identify the MAC HW device */
  2636. if (priv->plat->has_gmac) {
  2637. priv->dev->priv_flags |= IFF_UNICAST_FLT;
  2638. mac = dwmac1000_setup(priv->ioaddr,
  2639. priv->plat->multicast_filter_bins,
  2640. priv->plat->unicast_filter_entries,
  2641. &priv->synopsys_id);
  2642. } else if (priv->plat->has_gmac4) {
  2643. priv->dev->priv_flags |= IFF_UNICAST_FLT;
  2644. mac = dwmac4_setup(priv->ioaddr,
  2645. priv->plat->multicast_filter_bins,
  2646. priv->plat->unicast_filter_entries,
  2647. &priv->synopsys_id);
  2648. } else {
  2649. mac = dwmac100_setup(priv->ioaddr, &priv->synopsys_id);
  2650. }
  2651. if (!mac)
  2652. return -ENOMEM;
  2653. priv->hw = mac;
  2654. /* To use the chained or ring mode */
  2655. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  2656. priv->hw->mode = &dwmac4_ring_mode_ops;
  2657. } else {
  2658. if (chain_mode) {
  2659. priv->hw->mode = &chain_mode_ops;
  2660. pr_info(" Chain mode enabled\n");
  2661. priv->mode = STMMAC_CHAIN_MODE;
  2662. } else {
  2663. priv->hw->mode = &ring_mode_ops;
  2664. pr_info(" Ring mode enabled\n");
  2665. priv->mode = STMMAC_RING_MODE;
  2666. }
  2667. }
  2668. /* Get the HW capability (new GMAC newer than 3.50a) */
  2669. priv->hw_cap_support = stmmac_get_hw_features(priv);
  2670. if (priv->hw_cap_support) {
  2671. pr_info(" DMA HW capability register supported");
  2672. /* We can override some gmac/dma configuration fields: e.g.
  2673. * enh_desc, tx_coe (e.g. that are passed through the
  2674. * platform) with the values from the HW capability
  2675. * register (if supported).
  2676. */
  2677. priv->plat->enh_desc = priv->dma_cap.enh_desc;
  2678. priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
  2679. priv->hw->pmt = priv->plat->pmt;
  2680. /* TXCOE doesn't work in thresh DMA mode */
  2681. if (priv->plat->force_thresh_dma_mode)
  2682. priv->plat->tx_coe = 0;
  2683. else
  2684. priv->plat->tx_coe = priv->dma_cap.tx_coe;
  2685. /* In case of GMAC4 rx_coe is from HW cap register. */
  2686. priv->plat->rx_coe = priv->dma_cap.rx_coe;
  2687. if (priv->dma_cap.rx_coe_type2)
  2688. priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
  2689. else if (priv->dma_cap.rx_coe_type1)
  2690. priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
  2691. } else
  2692. pr_info(" No HW DMA feature register supported");
  2693. /* To use alternate (extended), normal or GMAC4 descriptor structures */
  2694. if (priv->synopsys_id >= DWMAC_CORE_4_00)
  2695. priv->hw->desc = &dwmac4_desc_ops;
  2696. else
  2697. stmmac_selec_desc_mode(priv);
  2698. if (priv->plat->rx_coe) {
  2699. priv->hw->rx_csum = priv->plat->rx_coe;
  2700. pr_info(" RX Checksum Offload Engine supported\n");
  2701. if (priv->synopsys_id < DWMAC_CORE_4_00)
  2702. pr_info("\tCOE Type %d\n", priv->hw->rx_csum);
  2703. }
  2704. if (priv->plat->tx_coe)
  2705. pr_info(" TX Checksum insertion supported\n");
  2706. if (priv->plat->pmt) {
  2707. pr_info(" Wake-Up On Lan supported\n");
  2708. device_set_wakeup_capable(priv->device, 1);
  2709. }
  2710. if (priv->dma_cap.tsoen)
  2711. pr_info(" TSO supported\n");
  2712. return 0;
  2713. }
  2714. /**
  2715. * stmmac_dvr_probe
  2716. * @device: device pointer
  2717. * @plat_dat: platform data pointer
  2718. * @res: stmmac resource pointer
  2719. * Description: this is the main probe function used to
  2720. * call the alloc_etherdev, allocate the priv structure.
  2721. * Return:
  2722. * returns 0 on success, otherwise errno.
  2723. */
  2724. int stmmac_dvr_probe(struct device *device,
  2725. struct plat_stmmacenet_data *plat_dat,
  2726. struct stmmac_resources *res)
  2727. {
  2728. int ret = 0;
  2729. struct net_device *ndev = NULL;
  2730. struct stmmac_priv *priv;
  2731. ndev = alloc_etherdev(sizeof(struct stmmac_priv));
  2732. if (!ndev)
  2733. return -ENOMEM;
  2734. SET_NETDEV_DEV(ndev, device);
  2735. priv = netdev_priv(ndev);
  2736. priv->device = device;
  2737. priv->dev = ndev;
  2738. stmmac_set_ethtool_ops(ndev);
  2739. priv->pause = pause;
  2740. priv->plat = plat_dat;
  2741. priv->ioaddr = res->addr;
  2742. priv->dev->base_addr = (unsigned long)res->addr;
  2743. priv->dev->irq = res->irq;
  2744. priv->wol_irq = res->wol_irq;
  2745. priv->lpi_irq = res->lpi_irq;
  2746. if (res->mac)
  2747. memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
  2748. dev_set_drvdata(device, priv->dev);
  2749. /* Verify driver arguments */
  2750. stmmac_verify_args();
  2751. /* Override with kernel parameters if supplied XXX CRS XXX
  2752. * this needs to have multiple instances
  2753. */
  2754. if ((phyaddr >= 0) && (phyaddr <= 31))
  2755. priv->plat->phy_addr = phyaddr;
  2756. priv->stmmac_clk = devm_clk_get(priv->device, STMMAC_RESOURCE_NAME);
  2757. if (IS_ERR(priv->stmmac_clk)) {
  2758. dev_warn(priv->device, "%s: warning: cannot get CSR clock\n",
  2759. __func__);
  2760. /* If failed to obtain stmmac_clk and specific clk_csr value
  2761. * is NOT passed from the platform, probe fail.
  2762. */
  2763. if (!priv->plat->clk_csr) {
  2764. ret = PTR_ERR(priv->stmmac_clk);
  2765. goto error_clk_get;
  2766. } else {
  2767. priv->stmmac_clk = NULL;
  2768. }
  2769. }
  2770. clk_prepare_enable(priv->stmmac_clk);
  2771. priv->pclk = devm_clk_get(priv->device, "pclk");
  2772. if (IS_ERR(priv->pclk)) {
  2773. if (PTR_ERR(priv->pclk) == -EPROBE_DEFER) {
  2774. ret = -EPROBE_DEFER;
  2775. goto error_pclk_get;
  2776. }
  2777. priv->pclk = NULL;
  2778. }
  2779. clk_prepare_enable(priv->pclk);
  2780. priv->stmmac_rst = devm_reset_control_get(priv->device,
  2781. STMMAC_RESOURCE_NAME);
  2782. if (IS_ERR(priv->stmmac_rst)) {
  2783. if (PTR_ERR(priv->stmmac_rst) == -EPROBE_DEFER) {
  2784. ret = -EPROBE_DEFER;
  2785. goto error_hw_init;
  2786. }
  2787. dev_info(priv->device, "no reset control found\n");
  2788. priv->stmmac_rst = NULL;
  2789. }
  2790. if (priv->stmmac_rst)
  2791. reset_control_deassert(priv->stmmac_rst);
  2792. /* Init MAC and get the capabilities */
  2793. ret = stmmac_hw_init(priv);
  2794. if (ret)
  2795. goto error_hw_init;
  2796. ndev->netdev_ops = &stmmac_netdev_ops;
  2797. ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2798. NETIF_F_RXCSUM;
  2799. if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
  2800. ndev->hw_features |= NETIF_F_TSO;
  2801. priv->tso = true;
  2802. pr_info(" TSO feature enabled\n");
  2803. }
  2804. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  2805. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  2806. #ifdef STMMAC_VLAN_TAG_USED
  2807. /* Both mac100 and gmac support receive VLAN tag detection */
  2808. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  2809. #endif
  2810. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  2811. if (flow_ctrl)
  2812. priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
  2813. /* Rx Watchdog is available in the COREs newer than the 3.40.
  2814. * In some case, for example on bugged HW this feature
  2815. * has to be disable and this can be done by passing the
  2816. * riwt_off field from the platform.
  2817. */
  2818. if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
  2819. priv->use_riwt = 1;
  2820. pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
  2821. }
  2822. netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
  2823. spin_lock_init(&priv->lock);
  2824. spin_lock_init(&priv->tx_lock);
  2825. ret = register_netdev(ndev);
  2826. if (ret) {
  2827. pr_err("%s: ERROR %i registering the device\n", __func__, ret);
  2828. goto error_netdev_register;
  2829. }
  2830. /* If a specific clk_csr value is passed from the platform
  2831. * this means that the CSR Clock Range selection cannot be
  2832. * changed at run-time and it is fixed. Viceversa the driver'll try to
  2833. * set the MDC clock dynamically according to the csr actual
  2834. * clock input.
  2835. */
  2836. if (!priv->plat->clk_csr)
  2837. stmmac_clk_csr_set(priv);
  2838. else
  2839. priv->clk_csr = priv->plat->clk_csr;
  2840. stmmac_check_pcs_mode(priv);
  2841. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  2842. priv->hw->pcs != STMMAC_PCS_TBI &&
  2843. priv->hw->pcs != STMMAC_PCS_RTBI) {
  2844. /* MDIO bus Registration */
  2845. ret = stmmac_mdio_register(ndev);
  2846. if (ret < 0) {
  2847. pr_debug("%s: MDIO bus (id: %d) registration failed",
  2848. __func__, priv->plat->bus_id);
  2849. goto error_mdio_register;
  2850. }
  2851. }
  2852. return 0;
  2853. error_mdio_register:
  2854. unregister_netdev(ndev);
  2855. error_netdev_register:
  2856. netif_napi_del(&priv->napi);
  2857. error_hw_init:
  2858. clk_disable_unprepare(priv->pclk);
  2859. error_pclk_get:
  2860. clk_disable_unprepare(priv->stmmac_clk);
  2861. error_clk_get:
  2862. free_netdev(ndev);
  2863. return ret;
  2864. }
  2865. EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
  2866. /**
  2867. * stmmac_dvr_remove
  2868. * @dev: device pointer
  2869. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  2870. * changes the link status, releases the DMA descriptor rings.
  2871. */
  2872. int stmmac_dvr_remove(struct device *dev)
  2873. {
  2874. struct net_device *ndev = dev_get_drvdata(dev);
  2875. struct stmmac_priv *priv = netdev_priv(ndev);
  2876. pr_info("%s:\n\tremoving driver", __func__);
  2877. priv->hw->dma->stop_rx(priv->ioaddr);
  2878. priv->hw->dma->stop_tx(priv->ioaddr);
  2879. stmmac_set_mac(priv->ioaddr, false);
  2880. netif_carrier_off(ndev);
  2881. unregister_netdev(ndev);
  2882. of_node_put(priv->plat->phy_node);
  2883. if (priv->stmmac_rst)
  2884. reset_control_assert(priv->stmmac_rst);
  2885. clk_disable_unprepare(priv->pclk);
  2886. clk_disable_unprepare(priv->stmmac_clk);
  2887. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  2888. priv->hw->pcs != STMMAC_PCS_TBI &&
  2889. priv->hw->pcs != STMMAC_PCS_RTBI)
  2890. stmmac_mdio_unregister(ndev);
  2891. free_netdev(ndev);
  2892. return 0;
  2893. }
  2894. EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
  2895. /**
  2896. * stmmac_suspend - suspend callback
  2897. * @dev: device pointer
  2898. * Description: this is the function to suspend the device and it is called
  2899. * by the platform driver to stop the network queue, release the resources,
  2900. * program the PMT register (for WoL), clean and release driver resources.
  2901. */
  2902. int stmmac_suspend(struct device *dev)
  2903. {
  2904. struct net_device *ndev = dev_get_drvdata(dev);
  2905. struct stmmac_priv *priv = netdev_priv(ndev);
  2906. unsigned long flags;
  2907. if (!ndev || !netif_running(ndev))
  2908. return 0;
  2909. if (priv->phydev)
  2910. phy_stop(priv->phydev);
  2911. spin_lock_irqsave(&priv->lock, flags);
  2912. netif_device_detach(ndev);
  2913. netif_stop_queue(ndev);
  2914. napi_disable(&priv->napi);
  2915. /* Stop TX/RX DMA */
  2916. priv->hw->dma->stop_tx(priv->ioaddr);
  2917. priv->hw->dma->stop_rx(priv->ioaddr);
  2918. /* Enable Power down mode by programming the PMT regs */
  2919. if (device_may_wakeup(priv->device)) {
  2920. priv->hw->mac->pmt(priv->hw, priv->wolopts);
  2921. priv->irq_wake = 1;
  2922. } else {
  2923. stmmac_set_mac(priv->ioaddr, false);
  2924. pinctrl_pm_select_sleep_state(priv->device);
  2925. /* Disable clock in case of PWM is off */
  2926. clk_disable(priv->pclk);
  2927. clk_disable(priv->stmmac_clk);
  2928. }
  2929. spin_unlock_irqrestore(&priv->lock, flags);
  2930. priv->oldlink = 0;
  2931. priv->speed = 0;
  2932. priv->oldduplex = -1;
  2933. return 0;
  2934. }
  2935. EXPORT_SYMBOL_GPL(stmmac_suspend);
  2936. /**
  2937. * stmmac_resume - resume callback
  2938. * @dev: device pointer
  2939. * Description: when resume this function is invoked to setup the DMA and CORE
  2940. * in a usable state.
  2941. */
  2942. int stmmac_resume(struct device *dev)
  2943. {
  2944. struct net_device *ndev = dev_get_drvdata(dev);
  2945. struct stmmac_priv *priv = netdev_priv(ndev);
  2946. unsigned long flags;
  2947. if (!netif_running(ndev))
  2948. return 0;
  2949. /* Power Down bit, into the PM register, is cleared
  2950. * automatically as soon as a magic packet or a Wake-up frame
  2951. * is received. Anyway, it's better to manually clear
  2952. * this bit because it can generate problems while resuming
  2953. * from another devices (e.g. serial console).
  2954. */
  2955. if (device_may_wakeup(priv->device)) {
  2956. spin_lock_irqsave(&priv->lock, flags);
  2957. priv->hw->mac->pmt(priv->hw, 0);
  2958. spin_unlock_irqrestore(&priv->lock, flags);
  2959. priv->irq_wake = 0;
  2960. } else {
  2961. pinctrl_pm_select_default_state(priv->device);
  2962. /* enable the clk prevously disabled */
  2963. clk_enable(priv->stmmac_clk);
  2964. clk_enable(priv->pclk);
  2965. /* reset the phy so that it's ready */
  2966. if (priv->mii)
  2967. stmmac_mdio_reset(priv->mii);
  2968. }
  2969. netif_device_attach(ndev);
  2970. spin_lock_irqsave(&priv->lock, flags);
  2971. priv->cur_rx = 0;
  2972. priv->dirty_rx = 0;
  2973. priv->dirty_tx = 0;
  2974. priv->cur_tx = 0;
  2975. /* reset private mss value to force mss context settings at
  2976. * next tso xmit (only used for gmac4).
  2977. */
  2978. priv->mss = 0;
  2979. stmmac_clear_descriptors(priv);
  2980. stmmac_hw_setup(ndev, false);
  2981. stmmac_init_tx_coalesce(priv);
  2982. stmmac_set_rx_mode(ndev);
  2983. napi_enable(&priv->napi);
  2984. netif_start_queue(ndev);
  2985. spin_unlock_irqrestore(&priv->lock, flags);
  2986. if (priv->phydev)
  2987. phy_start(priv->phydev);
  2988. return 0;
  2989. }
  2990. EXPORT_SYMBOL_GPL(stmmac_resume);
  2991. #ifndef MODULE
  2992. static int __init stmmac_cmdline_opt(char *str)
  2993. {
  2994. char *opt;
  2995. if (!str || !*str)
  2996. return -EINVAL;
  2997. while ((opt = strsep(&str, ",")) != NULL) {
  2998. if (!strncmp(opt, "debug:", 6)) {
  2999. if (kstrtoint(opt + 6, 0, &debug))
  3000. goto err;
  3001. } else if (!strncmp(opt, "phyaddr:", 8)) {
  3002. if (kstrtoint(opt + 8, 0, &phyaddr))
  3003. goto err;
  3004. } else if (!strncmp(opt, "buf_sz:", 7)) {
  3005. if (kstrtoint(opt + 7, 0, &buf_sz))
  3006. goto err;
  3007. } else if (!strncmp(opt, "tc:", 3)) {
  3008. if (kstrtoint(opt + 3, 0, &tc))
  3009. goto err;
  3010. } else if (!strncmp(opt, "watchdog:", 9)) {
  3011. if (kstrtoint(opt + 9, 0, &watchdog))
  3012. goto err;
  3013. } else if (!strncmp(opt, "flow_ctrl:", 10)) {
  3014. if (kstrtoint(opt + 10, 0, &flow_ctrl))
  3015. goto err;
  3016. } else if (!strncmp(opt, "pause:", 6)) {
  3017. if (kstrtoint(opt + 6, 0, &pause))
  3018. goto err;
  3019. } else if (!strncmp(opt, "eee_timer:", 10)) {
  3020. if (kstrtoint(opt + 10, 0, &eee_timer))
  3021. goto err;
  3022. } else if (!strncmp(opt, "chain_mode:", 11)) {
  3023. if (kstrtoint(opt + 11, 0, &chain_mode))
  3024. goto err;
  3025. }
  3026. }
  3027. return 0;
  3028. err:
  3029. pr_err("%s: ERROR broken module parameter conversion", __func__);
  3030. return -EINVAL;
  3031. }
  3032. __setup("stmmaceth=", stmmac_cmdline_opt);
  3033. #endif /* MODULE */
  3034. static int __init stmmac_init(void)
  3035. {
  3036. #ifdef CONFIG_DEBUG_FS
  3037. /* Create debugfs main directory if it doesn't exist yet */
  3038. if (!stmmac_fs_dir) {
  3039. stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
  3040. if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
  3041. pr_err("ERROR %s, debugfs create directory failed\n",
  3042. STMMAC_RESOURCE_NAME);
  3043. return -ENOMEM;
  3044. }
  3045. }
  3046. #endif
  3047. return 0;
  3048. }
  3049. static void __exit stmmac_exit(void)
  3050. {
  3051. #ifdef CONFIG_DEBUG_FS
  3052. debugfs_remove_recursive(stmmac_fs_dir);
  3053. #endif
  3054. }
  3055. module_init(stmmac_init)
  3056. module_exit(stmmac_exit)
  3057. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
  3058. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  3059. MODULE_LICENSE("GPL");