stmmac_main.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960
  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. #ifdef CONFIG_STMMAC_DEBUG_FS
  39. #include <linux/debugfs.h>
  40. #include <linux/seq_file.h>
  41. #endif /* CONFIG_STMMAC_DEBUG_FS */
  42. #include <linux/net_tstamp.h>
  43. #include "stmmac_ptp.h"
  44. #include "stmmac.h"
  45. #undef STMMAC_DEBUG
  46. /*#define STMMAC_DEBUG*/
  47. #ifdef STMMAC_DEBUG
  48. #define DBG(nlevel, klevel, fmt, args...) \
  49. ((void)(netif_msg_##nlevel(priv) && \
  50. printk(KERN_##klevel fmt, ## args)))
  51. #else
  52. #define DBG(nlevel, klevel, fmt, args...) do { } while (0)
  53. #endif
  54. #undef STMMAC_RX_DEBUG
  55. /*#define STMMAC_RX_DEBUG*/
  56. #ifdef STMMAC_RX_DEBUG
  57. #define RX_DBG(fmt, args...) printk(fmt, ## args)
  58. #else
  59. #define RX_DBG(fmt, args...) do { } while (0)
  60. #endif
  61. #undef STMMAC_XMIT_DEBUG
  62. /*#define STMMAC_XMIT_DEBUG*/
  63. #ifdef STMMAC_XMIT_DEBUG
  64. #define TX_DBG(fmt, args...) printk(fmt, ## args)
  65. #else
  66. #define TX_DBG(fmt, args...) do { } while (0)
  67. #endif
  68. #define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
  69. #define JUMBO_LEN 9000
  70. /* Module parameters */
  71. #define TX_TIMEO 5000
  72. static int watchdog = TX_TIMEO;
  73. module_param(watchdog, int, S_IRUGO | S_IWUSR);
  74. MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
  75. static int debug = -1;
  76. module_param(debug, int, S_IRUGO | S_IWUSR);
  77. MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
  78. int phyaddr = -1;
  79. module_param(phyaddr, int, S_IRUGO);
  80. MODULE_PARM_DESC(phyaddr, "Physical device address");
  81. #define DMA_TX_SIZE 256
  82. static int dma_txsize = DMA_TX_SIZE;
  83. module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
  84. MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
  85. #define DMA_RX_SIZE 256
  86. static int dma_rxsize = DMA_RX_SIZE;
  87. module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
  88. MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
  89. static int flow_ctrl = FLOW_OFF;
  90. module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
  91. MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
  92. static int pause = PAUSE_TIME;
  93. module_param(pause, int, S_IRUGO | S_IWUSR);
  94. MODULE_PARM_DESC(pause, "Flow Control Pause Time");
  95. #define TC_DEFAULT 64
  96. static int tc = TC_DEFAULT;
  97. module_param(tc, int, S_IRUGO | S_IWUSR);
  98. MODULE_PARM_DESC(tc, "DMA threshold control value");
  99. #define DMA_BUFFER_SIZE BUF_SIZE_2KiB
  100. static int buf_sz = DMA_BUFFER_SIZE;
  101. module_param(buf_sz, int, S_IRUGO | S_IWUSR);
  102. MODULE_PARM_DESC(buf_sz, "DMA buffer size");
  103. static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  104. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  105. NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
  106. #define STMMAC_DEFAULT_LPI_TIMER 1000
  107. static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  108. module_param(eee_timer, int, S_IRUGO | S_IWUSR);
  109. MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
  110. #define STMMAC_LPI_TIMER(x) (jiffies + msecs_to_jiffies(x))
  111. /* By default the driver will use the ring mode to manage tx and rx descriptors
  112. * but passing this value so user can force to use the chain instead of the ring
  113. */
  114. static unsigned int chain_mode;
  115. module_param(chain_mode, int, S_IRUGO);
  116. MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
  117. static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
  118. #ifdef CONFIG_STMMAC_DEBUG_FS
  119. static int stmmac_init_fs(struct net_device *dev);
  120. static void stmmac_exit_fs(void);
  121. #endif
  122. #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
  123. /**
  124. * stmmac_verify_args - verify the driver parameters.
  125. * Description: it verifies if some wrong parameter is passed to the driver.
  126. * Note that wrong parameters are replaced with the default values.
  127. */
  128. static void stmmac_verify_args(void)
  129. {
  130. if (unlikely(watchdog < 0))
  131. watchdog = TX_TIMEO;
  132. if (unlikely(dma_rxsize < 0))
  133. dma_rxsize = DMA_RX_SIZE;
  134. if (unlikely(dma_txsize < 0))
  135. dma_txsize = DMA_TX_SIZE;
  136. if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
  137. buf_sz = DMA_BUFFER_SIZE;
  138. if (unlikely(flow_ctrl > 1))
  139. flow_ctrl = FLOW_AUTO;
  140. else if (likely(flow_ctrl < 0))
  141. flow_ctrl = FLOW_OFF;
  142. if (unlikely((pause < 0) || (pause > 0xffff)))
  143. pause = PAUSE_TIME;
  144. if (eee_timer < 0)
  145. eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  146. }
  147. /**
  148. * stmmac_clk_csr_set - dynamically set the MDC clock
  149. * @priv: driver private structure
  150. * Description: this is to dynamically set the MDC clock according to the csr
  151. * clock input.
  152. * Note:
  153. * If a specific clk_csr value is passed from the platform
  154. * this means that the CSR Clock Range selection cannot be
  155. * changed at run-time and it is fixed (as reported in the driver
  156. * documentation). Viceversa the driver will try to set the MDC
  157. * clock dynamically according to the actual clock input.
  158. */
  159. static void stmmac_clk_csr_set(struct stmmac_priv *priv)
  160. {
  161. u32 clk_rate;
  162. clk_rate = clk_get_rate(priv->stmmac_clk);
  163. /* Platform provided default clk_csr would be assumed valid
  164. * for all other cases except for the below mentioned ones.
  165. * For values higher than the IEEE 802.3 specified frequency
  166. * we can not estimate the proper divider as it is not known
  167. * the frequency of clk_csr_i. So we do not change the default
  168. * divider.
  169. */
  170. if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
  171. if (clk_rate < CSR_F_35M)
  172. priv->clk_csr = STMMAC_CSR_20_35M;
  173. else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
  174. priv->clk_csr = STMMAC_CSR_35_60M;
  175. else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
  176. priv->clk_csr = STMMAC_CSR_60_100M;
  177. else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
  178. priv->clk_csr = STMMAC_CSR_100_150M;
  179. else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
  180. priv->clk_csr = STMMAC_CSR_150_250M;
  181. else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
  182. priv->clk_csr = STMMAC_CSR_250_300M;
  183. }
  184. }
  185. #if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
  186. static void print_pkt(unsigned char *buf, int len)
  187. {
  188. int j;
  189. pr_info("len = %d byte, buf addr: 0x%p", len, buf);
  190. for (j = 0; j < len; j++) {
  191. if ((j % 16) == 0)
  192. pr_info("\n %03x:", j);
  193. pr_info(" %02x", buf[j]);
  194. }
  195. pr_info("\n");
  196. }
  197. #endif
  198. /* minimum number of free TX descriptors required to wake up TX process */
  199. #define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
  200. static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
  201. {
  202. return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
  203. }
  204. /**
  205. * stmmac_hw_fix_mac_speed: callback for speed selection
  206. * @priv: driver private structure
  207. * Description: on some platforms (e.g. ST), some HW system configuraton
  208. * registers have to be set according to the link speed negotiated.
  209. */
  210. static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
  211. {
  212. struct phy_device *phydev = priv->phydev;
  213. if (likely(priv->plat->fix_mac_speed))
  214. priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
  215. }
  216. /**
  217. * stmmac_enable_eee_mode: Check and enter in LPI mode
  218. * @priv: driver private structure
  219. * Description: this function is to verify and enter in LPI mode for EEE.
  220. */
  221. static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
  222. {
  223. /* Check and enter in LPI mode */
  224. if ((priv->dirty_tx == priv->cur_tx) &&
  225. (priv->tx_path_in_lpi_mode == false))
  226. priv->hw->mac->set_eee_mode(priv->ioaddr);
  227. }
  228. /**
  229. * stmmac_disable_eee_mode: disable/exit from EEE
  230. * @priv: driver private structure
  231. * Description: this function is to exit and disable EEE in case of
  232. * LPI state is true. This is called by the xmit.
  233. */
  234. void stmmac_disable_eee_mode(struct stmmac_priv *priv)
  235. {
  236. priv->hw->mac->reset_eee_mode(priv->ioaddr);
  237. del_timer_sync(&priv->eee_ctrl_timer);
  238. priv->tx_path_in_lpi_mode = false;
  239. }
  240. /**
  241. * stmmac_eee_ctrl_timer: EEE TX SW timer.
  242. * @arg : data hook
  243. * Description:
  244. * if there is no data transfer and if we are not in LPI state,
  245. * then MAC Transmitter can be moved to LPI state.
  246. */
  247. static void stmmac_eee_ctrl_timer(unsigned long arg)
  248. {
  249. struct stmmac_priv *priv = (struct stmmac_priv *)arg;
  250. stmmac_enable_eee_mode(priv);
  251. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
  252. }
  253. /**
  254. * stmmac_eee_init: init EEE
  255. * @priv: driver private structure
  256. * Description:
  257. * If the EEE support has been enabled while configuring the driver,
  258. * if the GMAC actually supports the EEE (from the HW cap reg) and the
  259. * phy can also manage EEE, so enable the LPI state and start the timer
  260. * to verify if the tx path can enter in LPI state.
  261. */
  262. bool stmmac_eee_init(struct stmmac_priv *priv)
  263. {
  264. bool ret = false;
  265. /* MAC core supports the EEE feature. */
  266. if (priv->dma_cap.eee) {
  267. /* Check if the PHY supports EEE */
  268. if (phy_init_eee(priv->phydev, 1))
  269. goto out;
  270. priv->eee_active = 1;
  271. init_timer(&priv->eee_ctrl_timer);
  272. priv->eee_ctrl_timer.function = stmmac_eee_ctrl_timer;
  273. priv->eee_ctrl_timer.data = (unsigned long)priv;
  274. priv->eee_ctrl_timer.expires = STMMAC_LPI_TIMER(eee_timer);
  275. add_timer(&priv->eee_ctrl_timer);
  276. priv->hw->mac->set_eee_timer(priv->ioaddr,
  277. STMMAC_DEFAULT_LIT_LS_TIMER,
  278. priv->tx_lpi_timer);
  279. pr_info("stmmac: Energy-Efficient Ethernet initialized\n");
  280. ret = true;
  281. }
  282. out:
  283. return ret;
  284. }
  285. /**
  286. * stmmac_eee_adjust: adjust HW EEE according to the speed
  287. * @priv: driver private structure
  288. * Description:
  289. * When the EEE has been already initialised we have to
  290. * modify the PLS bit in the LPI ctrl & status reg according
  291. * to the PHY link status. For this reason.
  292. */
  293. static void stmmac_eee_adjust(struct stmmac_priv *priv)
  294. {
  295. if (priv->eee_enabled)
  296. priv->hw->mac->set_eee_pls(priv->ioaddr, priv->phydev->link);
  297. }
  298. /* stmmac_get_tx_hwtstamp: get HW TX timestamps
  299. * @priv: driver private structure
  300. * @entry : descriptor index to be used.
  301. * @skb : the socket buffer
  302. * Description :
  303. * This function will read timestamp from the descriptor & pass it to stack.
  304. * and also perform some sanity checks.
  305. */
  306. static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
  307. unsigned int entry, struct sk_buff *skb)
  308. {
  309. struct skb_shared_hwtstamps shhwtstamp;
  310. u64 ns;
  311. void *desc = NULL;
  312. if (!priv->hwts_tx_en)
  313. return;
  314. /* exit if skb doesn't support hw tstamp */
  315. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
  316. return;
  317. if (priv->adv_ts)
  318. desc = (priv->dma_etx + entry);
  319. else
  320. desc = (priv->dma_tx + entry);
  321. /* check tx tstamp status */
  322. if (!priv->hw->desc->get_tx_timestamp_status((struct dma_desc *)desc))
  323. return;
  324. /* get the valid tstamp */
  325. ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
  326. memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
  327. shhwtstamp.hwtstamp = ns_to_ktime(ns);
  328. /* pass tstamp to stack */
  329. skb_tstamp_tx(skb, &shhwtstamp);
  330. return;
  331. }
  332. /* stmmac_get_rx_hwtstamp: get HW RX timestamps
  333. * @priv: driver private structure
  334. * @entry : descriptor index to be used.
  335. * @skb : the socket buffer
  336. * Description :
  337. * This function will read received packet's timestamp from the descriptor
  338. * and pass it to stack. It also perform some sanity checks.
  339. */
  340. static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv,
  341. unsigned int entry, struct sk_buff *skb)
  342. {
  343. struct skb_shared_hwtstamps *shhwtstamp = NULL;
  344. u64 ns;
  345. void *desc = NULL;
  346. if (!priv->hwts_rx_en)
  347. return;
  348. if (priv->adv_ts)
  349. desc = (priv->dma_erx + entry);
  350. else
  351. desc = (priv->dma_rx + entry);
  352. /* exit if rx tstamp is not valid */
  353. if (!priv->hw->desc->get_rx_timestamp_status(desc, priv->adv_ts))
  354. return;
  355. /* get valid tstamp */
  356. ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
  357. shhwtstamp = skb_hwtstamps(skb);
  358. memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
  359. shhwtstamp->hwtstamp = ns_to_ktime(ns);
  360. }
  361. /**
  362. * stmmac_hwtstamp_ioctl - control hardware timestamping.
  363. * @dev: device pointer.
  364. * @ifr: An IOCTL specefic structure, that can contain a pointer to
  365. * a proprietary structure used to pass information to the driver.
  366. * Description:
  367. * This function configures the MAC to enable/disable both outgoing(TX)
  368. * and incoming(RX) packets time stamping based on user input.
  369. * Return Value:
  370. * 0 on success and an appropriate -ve integer on failure.
  371. */
  372. static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  373. {
  374. struct stmmac_priv *priv = netdev_priv(dev);
  375. struct hwtstamp_config config;
  376. struct timespec now;
  377. u64 temp = 0;
  378. u32 ptp_v2 = 0;
  379. u32 tstamp_all = 0;
  380. u32 ptp_over_ipv4_udp = 0;
  381. u32 ptp_over_ipv6_udp = 0;
  382. u32 ptp_over_ethernet = 0;
  383. u32 snap_type_sel = 0;
  384. u32 ts_master_en = 0;
  385. u32 ts_event_en = 0;
  386. u32 value = 0;
  387. if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
  388. netdev_alert(priv->dev, "No support for HW time stamping\n");
  389. priv->hwts_tx_en = 0;
  390. priv->hwts_rx_en = 0;
  391. return -EOPNOTSUPP;
  392. }
  393. if (copy_from_user(&config, ifr->ifr_data,
  394. sizeof(struct hwtstamp_config)))
  395. return -EFAULT;
  396. pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
  397. __func__, config.flags, config.tx_type, config.rx_filter);
  398. /* reserved for future extensions */
  399. if (config.flags)
  400. return -EINVAL;
  401. switch (config.tx_type) {
  402. case HWTSTAMP_TX_OFF:
  403. priv->hwts_tx_en = 0;
  404. break;
  405. case HWTSTAMP_TX_ON:
  406. priv->hwts_tx_en = 1;
  407. break;
  408. default:
  409. return -ERANGE;
  410. }
  411. if (priv->adv_ts) {
  412. switch (config.rx_filter) {
  413. case HWTSTAMP_FILTER_NONE:
  414. /* time stamp no incoming packet at all */
  415. config.rx_filter = HWTSTAMP_FILTER_NONE;
  416. break;
  417. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  418. /* PTP v1, UDP, any kind of event packet */
  419. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  420. /* take time stamp for all event messages */
  421. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  422. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  423. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  424. break;
  425. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  426. /* PTP v1, UDP, Sync packet */
  427. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
  428. /* take time stamp for SYNC messages only */
  429. ts_event_en = PTP_TCR_TSEVNTENA;
  430. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  431. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  432. break;
  433. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  434. /* PTP v1, UDP, Delay_req packet */
  435. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
  436. /* take time stamp for Delay_Req messages only */
  437. ts_master_en = PTP_TCR_TSMSTRENA;
  438. ts_event_en = PTP_TCR_TSEVNTENA;
  439. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  440. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  441. break;
  442. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  443. /* PTP v2, UDP, any kind of event packet */
  444. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  445. ptp_v2 = PTP_TCR_TSVER2ENA;
  446. /* take time stamp for all event messages */
  447. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  448. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  449. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  450. break;
  451. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  452. /* PTP v2, UDP, Sync packet */
  453. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
  454. ptp_v2 = PTP_TCR_TSVER2ENA;
  455. /* take time stamp for SYNC messages only */
  456. ts_event_en = PTP_TCR_TSEVNTENA;
  457. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  458. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  459. break;
  460. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  461. /* PTP v2, UDP, Delay_req packet */
  462. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
  463. ptp_v2 = PTP_TCR_TSVER2ENA;
  464. /* take time stamp for Delay_Req messages only */
  465. ts_master_en = PTP_TCR_TSMSTRENA;
  466. ts_event_en = PTP_TCR_TSEVNTENA;
  467. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  468. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  469. break;
  470. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  471. /* PTP v2/802.AS1 any layer, any kind of event packet */
  472. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  473. ptp_v2 = PTP_TCR_TSVER2ENA;
  474. /* take time stamp for all event messages */
  475. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  476. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  477. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  478. ptp_over_ethernet = PTP_TCR_TSIPENA;
  479. break;
  480. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  481. /* PTP v2/802.AS1, any layer, Sync packet */
  482. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
  483. ptp_v2 = PTP_TCR_TSVER2ENA;
  484. /* take time stamp for SYNC messages only */
  485. ts_event_en = PTP_TCR_TSEVNTENA;
  486. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  487. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  488. ptp_over_ethernet = PTP_TCR_TSIPENA;
  489. break;
  490. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  491. /* PTP v2/802.AS1, any layer, Delay_req packet */
  492. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
  493. ptp_v2 = PTP_TCR_TSVER2ENA;
  494. /* take time stamp for Delay_Req messages only */
  495. ts_master_en = PTP_TCR_TSMSTRENA;
  496. ts_event_en = PTP_TCR_TSEVNTENA;
  497. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  498. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  499. ptp_over_ethernet = PTP_TCR_TSIPENA;
  500. break;
  501. case HWTSTAMP_FILTER_ALL:
  502. /* time stamp any incoming packet */
  503. config.rx_filter = HWTSTAMP_FILTER_ALL;
  504. tstamp_all = PTP_TCR_TSENALL;
  505. break;
  506. default:
  507. return -ERANGE;
  508. }
  509. } else {
  510. switch (config.rx_filter) {
  511. case HWTSTAMP_FILTER_NONE:
  512. config.rx_filter = HWTSTAMP_FILTER_NONE;
  513. break;
  514. default:
  515. /* PTP v1, UDP, any kind of event packet */
  516. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  517. break;
  518. }
  519. }
  520. priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
  521. if (!priv->hwts_tx_en && !priv->hwts_rx_en)
  522. priv->hw->ptp->config_hw_tstamping(priv->ioaddr, 0);
  523. else {
  524. value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
  525. tstamp_all | ptp_v2 | ptp_over_ethernet |
  526. ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
  527. ts_master_en | snap_type_sel);
  528. priv->hw->ptp->config_hw_tstamping(priv->ioaddr, value);
  529. /* program Sub Second Increment reg */
  530. priv->hw->ptp->config_sub_second_increment(priv->ioaddr);
  531. /* calculate default added value:
  532. * formula is :
  533. * addend = (2^32)/freq_div_ratio;
  534. * where, freq_div_ratio = STMMAC_SYSCLOCK/50MHz
  535. * hence, addend = ((2^32) * 50MHz)/STMMAC_SYSCLOCK;
  536. * NOTE: STMMAC_SYSCLOCK should be >= 50MHz to
  537. * achive 20ns accuracy.
  538. *
  539. * 2^x * y == (y << x), hence
  540. * 2^32 * 50000000 ==> (50000000 << 32)
  541. */
  542. temp = (u64) (50000000ULL << 32);
  543. priv->default_addend = div_u64(temp, STMMAC_SYSCLOCK);
  544. priv->hw->ptp->config_addend(priv->ioaddr,
  545. priv->default_addend);
  546. /* initialize system time */
  547. getnstimeofday(&now);
  548. priv->hw->ptp->init_systime(priv->ioaddr, now.tv_sec,
  549. now.tv_nsec);
  550. }
  551. return copy_to_user(ifr->ifr_data, &config,
  552. sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
  553. }
  554. /**
  555. * stmmac_init_ptp: init PTP
  556. * @priv: driver private structure
  557. * Description: this is to verify if the HW supports the PTPv1 or v2.
  558. * This is done by looking at the HW cap. register.
  559. * Also it registers the ptp driver.
  560. */
  561. static int stmmac_init_ptp(struct stmmac_priv *priv)
  562. {
  563. if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
  564. return -EOPNOTSUPP;
  565. if (netif_msg_hw(priv)) {
  566. if (priv->dma_cap.time_stamp) {
  567. pr_debug("IEEE 1588-2002 Time Stamp supported\n");
  568. priv->adv_ts = 0;
  569. }
  570. if (priv->dma_cap.atime_stamp && priv->extend_desc) {
  571. pr_debug
  572. ("IEEE 1588-2008 Advanced Time Stamp supported\n");
  573. priv->adv_ts = 1;
  574. }
  575. }
  576. priv->hw->ptp = &stmmac_ptp;
  577. priv->hwts_tx_en = 0;
  578. priv->hwts_rx_en = 0;
  579. return stmmac_ptp_register(priv);
  580. }
  581. static void stmmac_release_ptp(struct stmmac_priv *priv)
  582. {
  583. stmmac_ptp_unregister(priv);
  584. }
  585. /**
  586. * stmmac_adjust_link
  587. * @dev: net device structure
  588. * Description: it adjusts the link parameters.
  589. */
  590. static void stmmac_adjust_link(struct net_device *dev)
  591. {
  592. struct stmmac_priv *priv = netdev_priv(dev);
  593. struct phy_device *phydev = priv->phydev;
  594. unsigned long flags;
  595. int new_state = 0;
  596. unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
  597. if (phydev == NULL)
  598. return;
  599. DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
  600. phydev->addr, phydev->link);
  601. spin_lock_irqsave(&priv->lock, flags);
  602. if (phydev->link) {
  603. u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
  604. /* Now we make sure that we can be in full duplex mode.
  605. * If not, we operate in half-duplex mode. */
  606. if (phydev->duplex != priv->oldduplex) {
  607. new_state = 1;
  608. if (!(phydev->duplex))
  609. ctrl &= ~priv->hw->link.duplex;
  610. else
  611. ctrl |= priv->hw->link.duplex;
  612. priv->oldduplex = phydev->duplex;
  613. }
  614. /* Flow Control operation */
  615. if (phydev->pause)
  616. priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
  617. fc, pause_time);
  618. if (phydev->speed != priv->speed) {
  619. new_state = 1;
  620. switch (phydev->speed) {
  621. case 1000:
  622. if (likely(priv->plat->has_gmac))
  623. ctrl &= ~priv->hw->link.port;
  624. stmmac_hw_fix_mac_speed(priv);
  625. break;
  626. case 100:
  627. case 10:
  628. if (priv->plat->has_gmac) {
  629. ctrl |= priv->hw->link.port;
  630. if (phydev->speed == SPEED_100) {
  631. ctrl |= priv->hw->link.speed;
  632. } else {
  633. ctrl &= ~(priv->hw->link.speed);
  634. }
  635. } else {
  636. ctrl &= ~priv->hw->link.port;
  637. }
  638. stmmac_hw_fix_mac_speed(priv);
  639. break;
  640. default:
  641. if (netif_msg_link(priv))
  642. pr_warn("%s: Speed (%d) not 10/100\n",
  643. dev->name, phydev->speed);
  644. break;
  645. }
  646. priv->speed = phydev->speed;
  647. }
  648. writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
  649. if (!priv->oldlink) {
  650. new_state = 1;
  651. priv->oldlink = 1;
  652. }
  653. } else if (priv->oldlink) {
  654. new_state = 1;
  655. priv->oldlink = 0;
  656. priv->speed = 0;
  657. priv->oldduplex = -1;
  658. }
  659. if (new_state && netif_msg_link(priv))
  660. phy_print_status(phydev);
  661. stmmac_eee_adjust(priv);
  662. spin_unlock_irqrestore(&priv->lock, flags);
  663. DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
  664. }
  665. /**
  666. * stmmac_check_pcs_mode: verify if RGMII/SGMII is supported
  667. * @priv: driver private structure
  668. * Description: this is to verify if the HW supports the PCS.
  669. * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
  670. * configured for the TBI, RTBI, or SGMII PHY interface.
  671. */
  672. static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
  673. {
  674. int interface = priv->plat->interface;
  675. if (priv->dma_cap.pcs) {
  676. if ((interface & PHY_INTERFACE_MODE_RGMII) ||
  677. (interface & PHY_INTERFACE_MODE_RGMII_ID) ||
  678. (interface & PHY_INTERFACE_MODE_RGMII_RXID) ||
  679. (interface & PHY_INTERFACE_MODE_RGMII_TXID)) {
  680. pr_debug("STMMAC: PCS RGMII support enable\n");
  681. priv->pcs = STMMAC_PCS_RGMII;
  682. } else if (interface & PHY_INTERFACE_MODE_SGMII) {
  683. pr_debug("STMMAC: PCS SGMII support enable\n");
  684. priv->pcs = STMMAC_PCS_SGMII;
  685. }
  686. }
  687. }
  688. /**
  689. * stmmac_init_phy - PHY initialization
  690. * @dev: net device structure
  691. * Description: it initializes the driver's PHY state, and attaches the PHY
  692. * to the mac driver.
  693. * Return value:
  694. * 0 on success
  695. */
  696. static int stmmac_init_phy(struct net_device *dev)
  697. {
  698. struct stmmac_priv *priv = netdev_priv(dev);
  699. struct phy_device *phydev;
  700. char phy_id_fmt[MII_BUS_ID_SIZE + 3];
  701. char bus_id[MII_BUS_ID_SIZE];
  702. int interface = priv->plat->interface;
  703. priv->oldlink = 0;
  704. priv->speed = 0;
  705. priv->oldduplex = -1;
  706. if (priv->plat->phy_bus_name)
  707. snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
  708. priv->plat->phy_bus_name, priv->plat->bus_id);
  709. else
  710. snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
  711. priv->plat->bus_id);
  712. snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
  713. priv->plat->phy_addr);
  714. pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id_fmt);
  715. phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link, interface);
  716. if (IS_ERR(phydev)) {
  717. pr_err("%s: Could not attach to PHY\n", dev->name);
  718. return PTR_ERR(phydev);
  719. }
  720. /* Stop Advertising 1000BASE Capability if interface is not GMII */
  721. if ((interface == PHY_INTERFACE_MODE_MII) ||
  722. (interface == PHY_INTERFACE_MODE_RMII))
  723. phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
  724. SUPPORTED_1000baseT_Full);
  725. /*
  726. * Broken HW is sometimes missing the pull-up resistor on the
  727. * MDIO line, which results in reads to non-existent devices returning
  728. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  729. * device as well.
  730. * Note: phydev->phy_id is the result of reading the UID PHY registers.
  731. */
  732. if (phydev->phy_id == 0) {
  733. phy_disconnect(phydev);
  734. return -ENODEV;
  735. }
  736. pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
  737. " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
  738. priv->phydev = phydev;
  739. return 0;
  740. }
  741. /**
  742. * stmmac_display_ring: display ring
  743. * @head: pointer to the head of the ring passed.
  744. * @size: size of the ring.
  745. * @extend_desc: to verify if extended descriptors are used.
  746. * Description: display the control/status and buffer descriptors.
  747. */
  748. static void stmmac_display_ring(void *head, int size, int extend_desc)
  749. {
  750. int i;
  751. struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
  752. struct dma_desc *p = (struct dma_desc *)head;
  753. for (i = 0; i < size; i++) {
  754. u64 x;
  755. if (extend_desc) {
  756. x = *(u64 *) ep;
  757. pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  758. i, (unsigned int)virt_to_phys(ep),
  759. (unsigned int)x, (unsigned int)(x >> 32),
  760. ep->basic.des2, ep->basic.des3);
  761. ep++;
  762. } else {
  763. x = *(u64 *) p;
  764. pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
  765. i, (unsigned int)virt_to_phys(p),
  766. (unsigned int)x, (unsigned int)(x >> 32),
  767. p->des2, p->des3);
  768. p++;
  769. }
  770. pr_info("\n");
  771. }
  772. }
  773. static void stmmac_display_rings(struct stmmac_priv *priv)
  774. {
  775. unsigned int txsize = priv->dma_tx_size;
  776. unsigned int rxsize = priv->dma_rx_size;
  777. if (priv->extend_desc) {
  778. pr_info("Extended RX descriptor ring:\n");
  779. stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
  780. pr_info("Extended TX descriptor ring:\n");
  781. stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
  782. } else {
  783. pr_info("RX descriptor ring:\n");
  784. stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
  785. pr_info("TX descriptor ring:\n");
  786. stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
  787. }
  788. }
  789. static int stmmac_set_bfsize(int mtu, int bufsize)
  790. {
  791. int ret = bufsize;
  792. if (mtu >= BUF_SIZE_4KiB)
  793. ret = BUF_SIZE_8KiB;
  794. else if (mtu >= BUF_SIZE_2KiB)
  795. ret = BUF_SIZE_4KiB;
  796. else if (mtu >= DMA_BUFFER_SIZE)
  797. ret = BUF_SIZE_2KiB;
  798. else
  799. ret = DMA_BUFFER_SIZE;
  800. return ret;
  801. }
  802. /**
  803. * stmmac_clear_descriptors: clear descriptors
  804. * @priv: driver private structure
  805. * Description: this function is called to clear the tx and rx descriptors
  806. * in case of both basic and extended descriptors are used.
  807. */
  808. static void stmmac_clear_descriptors(struct stmmac_priv *priv)
  809. {
  810. int i;
  811. unsigned int txsize = priv->dma_tx_size;
  812. unsigned int rxsize = priv->dma_rx_size;
  813. /* Clear the Rx/Tx descriptors */
  814. for (i = 0; i < rxsize; i++)
  815. if (priv->extend_desc)
  816. priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
  817. priv->use_riwt, priv->mode,
  818. (i == rxsize - 1));
  819. else
  820. priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
  821. priv->use_riwt, priv->mode,
  822. (i == rxsize - 1));
  823. for (i = 0; i < txsize; i++)
  824. if (priv->extend_desc)
  825. priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
  826. priv->mode,
  827. (i == txsize - 1));
  828. else
  829. priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
  830. priv->mode,
  831. (i == txsize - 1));
  832. }
  833. static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
  834. int i)
  835. {
  836. struct sk_buff *skb;
  837. skb = __netdev_alloc_skb(priv->dev, priv->dma_buf_sz + NET_IP_ALIGN,
  838. GFP_KERNEL);
  839. if (unlikely(skb == NULL)) {
  840. pr_err("%s: Rx init fails; skb is NULL\n", __func__);
  841. return 1;
  842. }
  843. skb_reserve(skb, NET_IP_ALIGN);
  844. priv->rx_skbuff[i] = skb;
  845. priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
  846. priv->dma_buf_sz,
  847. DMA_FROM_DEVICE);
  848. p->des2 = priv->rx_skbuff_dma[i];
  849. if ((priv->mode == STMMAC_RING_MODE) &&
  850. (priv->dma_buf_sz == BUF_SIZE_16KiB))
  851. priv->hw->ring->init_desc3(p);
  852. return 0;
  853. }
  854. /**
  855. * init_dma_desc_rings - init the RX/TX descriptor rings
  856. * @dev: net device structure
  857. * Description: this function initializes the DMA RX/TX descriptors
  858. * and allocates the socket buffers. It suppors the chained and ring
  859. * modes.
  860. */
  861. static void init_dma_desc_rings(struct net_device *dev)
  862. {
  863. int i;
  864. struct stmmac_priv *priv = netdev_priv(dev);
  865. unsigned int txsize = priv->dma_tx_size;
  866. unsigned int rxsize = priv->dma_rx_size;
  867. unsigned int bfsize = 0;
  868. /* Set the max buffer size according to the DESC mode
  869. * and the MTU. Note that RING mode allows 16KiB bsize.
  870. */
  871. if (priv->mode == STMMAC_RING_MODE)
  872. bfsize = priv->hw->ring->set_16kib_bfsize(dev->mtu);
  873. if (bfsize < BUF_SIZE_16KiB)
  874. bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
  875. DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
  876. txsize, rxsize, bfsize);
  877. if (priv->extend_desc) {
  878. priv->dma_erx = dma_alloc_coherent(priv->device, rxsize *
  879. sizeof(struct
  880. dma_extended_desc),
  881. &priv->dma_rx_phy,
  882. GFP_KERNEL);
  883. priv->dma_etx = dma_alloc_coherent(priv->device, txsize *
  884. sizeof(struct
  885. dma_extended_desc),
  886. &priv->dma_tx_phy,
  887. GFP_KERNEL);
  888. if ((!priv->dma_erx) || (!priv->dma_etx))
  889. return;
  890. } else {
  891. priv->dma_rx = dma_alloc_coherent(priv->device, rxsize *
  892. sizeof(struct dma_desc),
  893. &priv->dma_rx_phy,
  894. GFP_KERNEL);
  895. priv->dma_tx = dma_alloc_coherent(priv->device, txsize *
  896. sizeof(struct dma_desc),
  897. &priv->dma_tx_phy,
  898. GFP_KERNEL);
  899. if ((!priv->dma_rx) || (!priv->dma_tx))
  900. return;
  901. }
  902. priv->rx_skbuff_dma = kmalloc_array(rxsize, sizeof(dma_addr_t),
  903. GFP_KERNEL);
  904. priv->rx_skbuff = kmalloc_array(rxsize, sizeof(struct sk_buff *),
  905. GFP_KERNEL);
  906. priv->tx_skbuff_dma = kmalloc_array(txsize, sizeof(dma_addr_t),
  907. GFP_KERNEL);
  908. priv->tx_skbuff = kmalloc_array(txsize, sizeof(struct sk_buff *),
  909. GFP_KERNEL);
  910. if (netif_msg_drv(priv))
  911. pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
  912. (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
  913. /* RX INITIALIZATION */
  914. DBG(probe, INFO, "stmmac: SKB addresses:\nskb\t\tskb data\tdma data\n");
  915. for (i = 0; i < rxsize; i++) {
  916. struct dma_desc *p;
  917. if (priv->extend_desc)
  918. p = &((priv->dma_erx + i)->basic);
  919. else
  920. p = priv->dma_rx + i;
  921. if (stmmac_init_rx_buffers(priv, p, i))
  922. break;
  923. DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
  924. priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
  925. }
  926. priv->cur_rx = 0;
  927. priv->dirty_rx = (unsigned int)(i - rxsize);
  928. priv->dma_buf_sz = bfsize;
  929. buf_sz = bfsize;
  930. /* Setup the chained descriptor addresses */
  931. if (priv->mode == STMMAC_CHAIN_MODE) {
  932. if (priv->extend_desc) {
  933. priv->hw->chain->init(priv->dma_erx, priv->dma_rx_phy,
  934. rxsize, 1);
  935. priv->hw->chain->init(priv->dma_etx, priv->dma_tx_phy,
  936. txsize, 1);
  937. } else {
  938. priv->hw->chain->init(priv->dma_rx, priv->dma_rx_phy,
  939. rxsize, 0);
  940. priv->hw->chain->init(priv->dma_tx, priv->dma_tx_phy,
  941. txsize, 0);
  942. }
  943. }
  944. /* TX INITIALIZATION */
  945. for (i = 0; i < txsize; i++) {
  946. struct dma_desc *p;
  947. if (priv->extend_desc)
  948. p = &((priv->dma_etx + i)->basic);
  949. else
  950. p = priv->dma_tx + i;
  951. p->des2 = 0;
  952. priv->tx_skbuff_dma[i] = 0;
  953. priv->tx_skbuff[i] = NULL;
  954. }
  955. priv->dirty_tx = 0;
  956. priv->cur_tx = 0;
  957. stmmac_clear_descriptors(priv);
  958. if (netif_msg_hw(priv))
  959. stmmac_display_rings(priv);
  960. }
  961. static void dma_free_rx_skbufs(struct stmmac_priv *priv)
  962. {
  963. int i;
  964. for (i = 0; i < priv->dma_rx_size; i++) {
  965. if (priv->rx_skbuff[i]) {
  966. dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
  967. priv->dma_buf_sz, DMA_FROM_DEVICE);
  968. dev_kfree_skb_any(priv->rx_skbuff[i]);
  969. }
  970. priv->rx_skbuff[i] = NULL;
  971. }
  972. }
  973. static void dma_free_tx_skbufs(struct stmmac_priv *priv)
  974. {
  975. int i;
  976. for (i = 0; i < priv->dma_tx_size; i++) {
  977. if (priv->tx_skbuff[i] != NULL) {
  978. struct dma_desc *p;
  979. if (priv->extend_desc)
  980. p = &((priv->dma_etx + i)->basic);
  981. else
  982. p = priv->dma_tx + i;
  983. if (priv->tx_skbuff_dma[i])
  984. dma_unmap_single(priv->device,
  985. priv->tx_skbuff_dma[i],
  986. priv->hw->desc->get_tx_len(p),
  987. DMA_TO_DEVICE);
  988. dev_kfree_skb_any(priv->tx_skbuff[i]);
  989. priv->tx_skbuff[i] = NULL;
  990. priv->tx_skbuff_dma[i] = 0;
  991. }
  992. }
  993. }
  994. static void free_dma_desc_resources(struct stmmac_priv *priv)
  995. {
  996. /* Release the DMA TX/RX socket buffers */
  997. dma_free_rx_skbufs(priv);
  998. dma_free_tx_skbufs(priv);
  999. /* Free DMA regions of consistent memory previously allocated */
  1000. if (!priv->extend_desc) {
  1001. dma_free_coherent(priv->device,
  1002. priv->dma_tx_size * sizeof(struct dma_desc),
  1003. priv->dma_tx, priv->dma_tx_phy);
  1004. dma_free_coherent(priv->device,
  1005. priv->dma_rx_size * sizeof(struct dma_desc),
  1006. priv->dma_rx, priv->dma_rx_phy);
  1007. } else {
  1008. dma_free_coherent(priv->device, priv->dma_tx_size *
  1009. sizeof(struct dma_extended_desc),
  1010. priv->dma_etx, priv->dma_tx_phy);
  1011. dma_free_coherent(priv->device, priv->dma_rx_size *
  1012. sizeof(struct dma_extended_desc),
  1013. priv->dma_erx, priv->dma_rx_phy);
  1014. }
  1015. kfree(priv->rx_skbuff_dma);
  1016. kfree(priv->rx_skbuff);
  1017. kfree(priv->tx_skbuff_dma);
  1018. kfree(priv->tx_skbuff);
  1019. }
  1020. /**
  1021. * stmmac_dma_operation_mode - HW DMA operation mode
  1022. * @priv: driver private structure
  1023. * Description: it sets the DMA operation mode: tx/rx DMA thresholds
  1024. * or Store-And-Forward capability.
  1025. */
  1026. static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
  1027. {
  1028. if (likely(priv->plat->force_sf_dma_mode ||
  1029. ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
  1030. /*
  1031. * In case of GMAC, SF mode can be enabled
  1032. * to perform the TX COE in HW. This depends on:
  1033. * 1) TX COE if actually supported
  1034. * 2) There is no bugged Jumbo frame support
  1035. * that needs to not insert csum in the TDES.
  1036. */
  1037. priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE);
  1038. tc = SF_DMA_MODE;
  1039. } else
  1040. priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
  1041. }
  1042. /**
  1043. * stmmac_tx_clean:
  1044. * @priv: driver private structure
  1045. * Description: it reclaims resources after transmission completes.
  1046. */
  1047. static void stmmac_tx_clean(struct stmmac_priv *priv)
  1048. {
  1049. unsigned int txsize = priv->dma_tx_size;
  1050. spin_lock(&priv->tx_lock);
  1051. priv->xstats.tx_clean++;
  1052. while (priv->dirty_tx != priv->cur_tx) {
  1053. int last;
  1054. unsigned int entry = priv->dirty_tx % txsize;
  1055. struct sk_buff *skb = priv->tx_skbuff[entry];
  1056. struct dma_desc *p;
  1057. if (priv->extend_desc)
  1058. p = (struct dma_desc *)(priv->dma_etx + entry);
  1059. else
  1060. p = priv->dma_tx + entry;
  1061. /* Check if the descriptor is owned by the DMA. */
  1062. if (priv->hw->desc->get_tx_owner(p))
  1063. break;
  1064. /* Verify tx error by looking at the last segment. */
  1065. last = priv->hw->desc->get_tx_ls(p);
  1066. if (likely(last)) {
  1067. int tx_error =
  1068. priv->hw->desc->tx_status(&priv->dev->stats,
  1069. &priv->xstats, p,
  1070. priv->ioaddr);
  1071. if (likely(tx_error == 0)) {
  1072. priv->dev->stats.tx_packets++;
  1073. priv->xstats.tx_pkt_n++;
  1074. } else
  1075. priv->dev->stats.tx_errors++;
  1076. stmmac_get_tx_hwtstamp(priv, entry, skb);
  1077. }
  1078. TX_DBG("%s: curr %d, dirty %d\n", __func__,
  1079. priv->cur_tx, priv->dirty_tx);
  1080. if (likely(priv->tx_skbuff_dma[entry])) {
  1081. dma_unmap_single(priv->device,
  1082. priv->tx_skbuff_dma[entry],
  1083. priv->hw->desc->get_tx_len(p),
  1084. DMA_TO_DEVICE);
  1085. priv->tx_skbuff_dma[entry] = 0;
  1086. }
  1087. priv->hw->ring->clean_desc3(priv, p);
  1088. if (likely(skb != NULL)) {
  1089. dev_kfree_skb(skb);
  1090. priv->tx_skbuff[entry] = NULL;
  1091. }
  1092. priv->hw->desc->release_tx_desc(p, priv->mode);
  1093. priv->dirty_tx++;
  1094. }
  1095. if (unlikely(netif_queue_stopped(priv->dev) &&
  1096. stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
  1097. netif_tx_lock(priv->dev);
  1098. if (netif_queue_stopped(priv->dev) &&
  1099. stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
  1100. TX_DBG("%s: restart transmit\n", __func__);
  1101. netif_wake_queue(priv->dev);
  1102. }
  1103. netif_tx_unlock(priv->dev);
  1104. }
  1105. if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
  1106. stmmac_enable_eee_mode(priv);
  1107. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
  1108. }
  1109. spin_unlock(&priv->tx_lock);
  1110. }
  1111. static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
  1112. {
  1113. priv->hw->dma->enable_dma_irq(priv->ioaddr);
  1114. }
  1115. static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
  1116. {
  1117. priv->hw->dma->disable_dma_irq(priv->ioaddr);
  1118. }
  1119. /**
  1120. * stmmac_tx_err: irq tx error mng function
  1121. * @priv: driver private structure
  1122. * Description: it cleans the descriptors and restarts the transmission
  1123. * in case of errors.
  1124. */
  1125. static void stmmac_tx_err(struct stmmac_priv *priv)
  1126. {
  1127. int i;
  1128. int txsize = priv->dma_tx_size;
  1129. netif_stop_queue(priv->dev);
  1130. priv->hw->dma->stop_tx(priv->ioaddr);
  1131. dma_free_tx_skbufs(priv);
  1132. for (i = 0; i < txsize; i++)
  1133. if (priv->extend_desc)
  1134. priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
  1135. priv->mode,
  1136. (i == txsize - 1));
  1137. else
  1138. priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
  1139. priv->mode,
  1140. (i == txsize - 1));
  1141. priv->dirty_tx = 0;
  1142. priv->cur_tx = 0;
  1143. priv->hw->dma->start_tx(priv->ioaddr);
  1144. priv->dev->stats.tx_errors++;
  1145. netif_wake_queue(priv->dev);
  1146. }
  1147. /**
  1148. * stmmac_dma_interrupt: DMA ISR
  1149. * @priv: driver private structure
  1150. * Description: this is the DMA ISR. It is called by the main ISR.
  1151. * It calls the dwmac dma routine to understand which type of interrupt
  1152. * happened. In case of there is a Normal interrupt and either TX or RX
  1153. * interrupt happened so the NAPI is scheduled.
  1154. */
  1155. static void stmmac_dma_interrupt(struct stmmac_priv *priv)
  1156. {
  1157. int status;
  1158. status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
  1159. if (likely((status & handle_rx)) || (status & handle_tx)) {
  1160. if (likely(napi_schedule_prep(&priv->napi))) {
  1161. stmmac_disable_dma_irq(priv);
  1162. __napi_schedule(&priv->napi);
  1163. }
  1164. }
  1165. if (unlikely(status & tx_hard_error_bump_tc)) {
  1166. /* Try to bump up the dma threshold on this failure */
  1167. if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
  1168. tc += 64;
  1169. priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
  1170. priv->xstats.threshold = tc;
  1171. }
  1172. } else if (unlikely(status == tx_hard_error))
  1173. stmmac_tx_err(priv);
  1174. }
  1175. /**
  1176. * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
  1177. * @priv: driver private structure
  1178. * Description: this masks the MMC irq, in fact, the counters are managed in SW.
  1179. */
  1180. static void stmmac_mmc_setup(struct stmmac_priv *priv)
  1181. {
  1182. unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
  1183. MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
  1184. dwmac_mmc_intr_all_mask(priv->ioaddr);
  1185. if (priv->dma_cap.rmon) {
  1186. dwmac_mmc_ctrl(priv->ioaddr, mode);
  1187. memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
  1188. } else
  1189. pr_info(" No MAC Management Counters available\n");
  1190. }
  1191. static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
  1192. {
  1193. u32 hwid = priv->hw->synopsys_uid;
  1194. /* Check Synopsys Id (not available on old chips) */
  1195. if (likely(hwid)) {
  1196. u32 uid = ((hwid & 0x0000ff00) >> 8);
  1197. u32 synid = (hwid & 0x000000ff);
  1198. pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
  1199. uid, synid);
  1200. return synid;
  1201. }
  1202. return 0;
  1203. }
  1204. /**
  1205. * stmmac_selec_desc_mode: to select among: normal/alternate/extend descriptors
  1206. * @priv: driver private structure
  1207. * Description: select the Enhanced/Alternate or Normal descriptors.
  1208. * In case of Enhanced/Alternate, it looks at the extended descriptors are
  1209. * supported by the HW cap. register.
  1210. */
  1211. static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
  1212. {
  1213. if (priv->plat->enh_desc) {
  1214. pr_info(" Enhanced/Alternate descriptors\n");
  1215. /* GMAC older than 3.50 has no extended descriptors */
  1216. if (priv->synopsys_id >= DWMAC_CORE_3_50) {
  1217. pr_info("\tEnabled extended descriptors\n");
  1218. priv->extend_desc = 1;
  1219. } else
  1220. pr_warn("Extended descriptors not supported\n");
  1221. priv->hw->desc = &enh_desc_ops;
  1222. } else {
  1223. pr_info(" Normal descriptors\n");
  1224. priv->hw->desc = &ndesc_ops;
  1225. }
  1226. }
  1227. /**
  1228. * stmmac_get_hw_features: get MAC capabilities from the HW cap. register.
  1229. * @priv: driver private structure
  1230. * Description:
  1231. * new GMAC chip generations have a new register to indicate the
  1232. * presence of the optional feature/functions.
  1233. * This can be also used to override the value passed through the
  1234. * platform and necessary for old MAC10/100 and GMAC chips.
  1235. */
  1236. static int stmmac_get_hw_features(struct stmmac_priv *priv)
  1237. {
  1238. u32 hw_cap = 0;
  1239. if (priv->hw->dma->get_hw_feature) {
  1240. hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
  1241. priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
  1242. priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
  1243. priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
  1244. priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
  1245. priv->dma_cap.multi_addr = (hw_cap & DMA_HW_FEAT_ADDMAC) >> 5;
  1246. priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
  1247. priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
  1248. priv->dma_cap.pmt_remote_wake_up =
  1249. (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
  1250. priv->dma_cap.pmt_magic_frame =
  1251. (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
  1252. /* MMC */
  1253. priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
  1254. /* IEEE 1588-2002 */
  1255. priv->dma_cap.time_stamp =
  1256. (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
  1257. /* IEEE 1588-2008 */
  1258. priv->dma_cap.atime_stamp =
  1259. (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
  1260. /* 802.3az - Energy-Efficient Ethernet (EEE) */
  1261. priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
  1262. priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
  1263. /* TX and RX csum */
  1264. priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
  1265. priv->dma_cap.rx_coe_type1 =
  1266. (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
  1267. priv->dma_cap.rx_coe_type2 =
  1268. (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
  1269. priv->dma_cap.rxfifo_over_2048 =
  1270. (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
  1271. /* TX and RX number of channels */
  1272. priv->dma_cap.number_rx_channel =
  1273. (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
  1274. priv->dma_cap.number_tx_channel =
  1275. (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
  1276. /* Alternate (enhanced) DESC mode */
  1277. priv->dma_cap.enh_desc = (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
  1278. }
  1279. return hw_cap;
  1280. }
  1281. /**
  1282. * stmmac_check_ether_addr: check if the MAC addr is valid
  1283. * @priv: driver private structure
  1284. * Description:
  1285. * it is to verify if the MAC address is valid, in case of failures it
  1286. * generates a random MAC address
  1287. */
  1288. static void stmmac_check_ether_addr(struct stmmac_priv *priv)
  1289. {
  1290. if (!is_valid_ether_addr(priv->dev->dev_addr)) {
  1291. priv->hw->mac->get_umac_addr((void __iomem *)
  1292. priv->dev->base_addr,
  1293. priv->dev->dev_addr, 0);
  1294. if (!is_valid_ether_addr(priv->dev->dev_addr))
  1295. eth_hw_addr_random(priv->dev);
  1296. }
  1297. pr_warn("%s: device MAC address %pM\n", priv->dev->name,
  1298. priv->dev->dev_addr);
  1299. }
  1300. /**
  1301. * stmmac_init_dma_engine: DMA init.
  1302. * @priv: driver private structure
  1303. * Description:
  1304. * It inits the DMA invoking the specific MAC/GMAC callback.
  1305. * Some DMA parameters can be passed from the platform;
  1306. * in case of these are not passed a default is kept for the MAC or GMAC.
  1307. */
  1308. static int stmmac_init_dma_engine(struct stmmac_priv *priv)
  1309. {
  1310. int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_len = 0;
  1311. int mixed_burst = 0;
  1312. int atds = 0;
  1313. if (priv->plat->dma_cfg) {
  1314. pbl = priv->plat->dma_cfg->pbl;
  1315. fixed_burst = priv->plat->dma_cfg->fixed_burst;
  1316. mixed_burst = priv->plat->dma_cfg->mixed_burst;
  1317. burst_len = priv->plat->dma_cfg->burst_len;
  1318. }
  1319. if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
  1320. atds = 1;
  1321. return priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
  1322. burst_len, priv->dma_tx_phy,
  1323. priv->dma_rx_phy, atds);
  1324. }
  1325. /**
  1326. * stmmac_tx_timer: mitigation sw timer for tx.
  1327. * @data: data pointer
  1328. * Description:
  1329. * This is the timer handler to directly invoke the stmmac_tx_clean.
  1330. */
  1331. static void stmmac_tx_timer(unsigned long data)
  1332. {
  1333. struct stmmac_priv *priv = (struct stmmac_priv *)data;
  1334. stmmac_tx_clean(priv);
  1335. }
  1336. /**
  1337. * stmmac_init_tx_coalesce: init tx mitigation options.
  1338. * @priv: driver private structure
  1339. * Description:
  1340. * This inits the transmit coalesce parameters: i.e. timer rate,
  1341. * timer handler and default threshold used for enabling the
  1342. * interrupt on completion bit.
  1343. */
  1344. static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
  1345. {
  1346. priv->tx_coal_frames = STMMAC_TX_FRAMES;
  1347. priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
  1348. init_timer(&priv->txtimer);
  1349. priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
  1350. priv->txtimer.data = (unsigned long)priv;
  1351. priv->txtimer.function = stmmac_tx_timer;
  1352. add_timer(&priv->txtimer);
  1353. }
  1354. /**
  1355. * stmmac_open - open entry point of the driver
  1356. * @dev : pointer to the device structure.
  1357. * Description:
  1358. * This function is the open entry point of the driver.
  1359. * Return value:
  1360. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1361. * file on failure.
  1362. */
  1363. static int stmmac_open(struct net_device *dev)
  1364. {
  1365. struct stmmac_priv *priv = netdev_priv(dev);
  1366. int ret;
  1367. clk_prepare_enable(priv->stmmac_clk);
  1368. stmmac_check_ether_addr(priv);
  1369. if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
  1370. priv->pcs != STMMAC_PCS_RTBI) {
  1371. ret = stmmac_init_phy(dev);
  1372. if (ret) {
  1373. pr_err("%s: Cannot attach to PHY (error: %d)\n",
  1374. __func__, ret);
  1375. goto open_error;
  1376. }
  1377. }
  1378. /* Create and initialize the TX/RX descriptors chains. */
  1379. priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
  1380. priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
  1381. priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
  1382. init_dma_desc_rings(dev);
  1383. /* DMA initialization and SW reset */
  1384. ret = stmmac_init_dma_engine(priv);
  1385. if (ret < 0) {
  1386. pr_err("%s: DMA initialization failed\n", __func__);
  1387. goto open_error;
  1388. }
  1389. /* Copy the MAC addr into the HW */
  1390. priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
  1391. /* If required, perform hw setup of the bus. */
  1392. if (priv->plat->bus_setup)
  1393. priv->plat->bus_setup(priv->ioaddr);
  1394. /* Initialize the MAC Core */
  1395. priv->hw->mac->core_init(priv->ioaddr);
  1396. /* Request the IRQ lines */
  1397. ret = request_irq(dev->irq, stmmac_interrupt,
  1398. IRQF_SHARED, dev->name, dev);
  1399. if (unlikely(ret < 0)) {
  1400. pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
  1401. __func__, dev->irq, ret);
  1402. goto open_error;
  1403. }
  1404. /* Request the Wake IRQ in case of another line is used for WoL */
  1405. if (priv->wol_irq != dev->irq) {
  1406. ret = request_irq(priv->wol_irq, stmmac_interrupt,
  1407. IRQF_SHARED, dev->name, dev);
  1408. if (unlikely(ret < 0)) {
  1409. pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
  1410. __func__, priv->wol_irq, ret);
  1411. goto open_error_wolirq;
  1412. }
  1413. }
  1414. /* Request the IRQ lines */
  1415. if (priv->lpi_irq != -ENXIO) {
  1416. ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
  1417. dev->name, dev);
  1418. if (unlikely(ret < 0)) {
  1419. pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
  1420. __func__, priv->lpi_irq, ret);
  1421. goto open_error_lpiirq;
  1422. }
  1423. }
  1424. /* Enable the MAC Rx/Tx */
  1425. stmmac_set_mac(priv->ioaddr, true);
  1426. /* Set the HW DMA mode and the COE */
  1427. stmmac_dma_operation_mode(priv);
  1428. /* Extra statistics */
  1429. memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
  1430. priv->xstats.threshold = tc;
  1431. stmmac_mmc_setup(priv);
  1432. ret = stmmac_init_ptp(priv);
  1433. if (ret)
  1434. pr_warn("%s: failed PTP initialisation\n", __func__);
  1435. #ifdef CONFIG_STMMAC_DEBUG_FS
  1436. ret = stmmac_init_fs(dev);
  1437. if (ret < 0)
  1438. pr_warn("%s: failed debugFS registration\n", __func__);
  1439. #endif
  1440. /* Start the ball rolling... */
  1441. DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
  1442. priv->hw->dma->start_tx(priv->ioaddr);
  1443. priv->hw->dma->start_rx(priv->ioaddr);
  1444. /* Dump DMA/MAC registers */
  1445. if (netif_msg_hw(priv)) {
  1446. priv->hw->mac->dump_regs(priv->ioaddr);
  1447. priv->hw->dma->dump_regs(priv->ioaddr);
  1448. }
  1449. if (priv->phydev)
  1450. phy_start(priv->phydev);
  1451. priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS_TIMER;
  1452. /* Using PCS we cannot dial with the phy registers at this stage
  1453. * so we do not support extra feature like EEE.
  1454. */
  1455. if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
  1456. priv->pcs != STMMAC_PCS_RTBI)
  1457. priv->eee_enabled = stmmac_eee_init(priv);
  1458. stmmac_init_tx_coalesce(priv);
  1459. if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
  1460. priv->rx_riwt = MAX_DMA_RIWT;
  1461. priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
  1462. }
  1463. if (priv->pcs && priv->hw->mac->ctrl_ane)
  1464. priv->hw->mac->ctrl_ane(priv->ioaddr, 0);
  1465. napi_enable(&priv->napi);
  1466. netif_start_queue(dev);
  1467. return 0;
  1468. open_error_lpiirq:
  1469. if (priv->wol_irq != dev->irq)
  1470. free_irq(priv->wol_irq, dev);
  1471. open_error_wolirq:
  1472. free_irq(dev->irq, dev);
  1473. open_error:
  1474. if (priv->phydev)
  1475. phy_disconnect(priv->phydev);
  1476. clk_disable_unprepare(priv->stmmac_clk);
  1477. return ret;
  1478. }
  1479. /**
  1480. * stmmac_release - close entry point of the driver
  1481. * @dev : device pointer.
  1482. * Description:
  1483. * This is the stop entry point of the driver.
  1484. */
  1485. static int stmmac_release(struct net_device *dev)
  1486. {
  1487. struct stmmac_priv *priv = netdev_priv(dev);
  1488. if (priv->eee_enabled)
  1489. del_timer_sync(&priv->eee_ctrl_timer);
  1490. /* Stop and disconnect the PHY */
  1491. if (priv->phydev) {
  1492. phy_stop(priv->phydev);
  1493. phy_disconnect(priv->phydev);
  1494. priv->phydev = NULL;
  1495. }
  1496. netif_stop_queue(dev);
  1497. napi_disable(&priv->napi);
  1498. del_timer_sync(&priv->txtimer);
  1499. /* Free the IRQ lines */
  1500. free_irq(dev->irq, dev);
  1501. if (priv->wol_irq != dev->irq)
  1502. free_irq(priv->wol_irq, dev);
  1503. if (priv->lpi_irq != -ENXIO)
  1504. free_irq(priv->lpi_irq, dev);
  1505. /* Stop TX/RX DMA and clear the descriptors */
  1506. priv->hw->dma->stop_tx(priv->ioaddr);
  1507. priv->hw->dma->stop_rx(priv->ioaddr);
  1508. /* Release and free the Rx/Tx resources */
  1509. free_dma_desc_resources(priv);
  1510. /* Disable the MAC Rx/Tx */
  1511. stmmac_set_mac(priv->ioaddr, false);
  1512. netif_carrier_off(dev);
  1513. #ifdef CONFIG_STMMAC_DEBUG_FS
  1514. stmmac_exit_fs();
  1515. #endif
  1516. clk_disable_unprepare(priv->stmmac_clk);
  1517. stmmac_release_ptp(priv);
  1518. return 0;
  1519. }
  1520. /**
  1521. * stmmac_xmit: Tx entry point of the driver
  1522. * @skb : the socket buffer
  1523. * @dev : device pointer
  1524. * Description : this is the tx entry point of the driver.
  1525. * It programs the chain or the ring and supports oversized frames
  1526. * and SG feature.
  1527. */
  1528. static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
  1529. {
  1530. struct stmmac_priv *priv = netdev_priv(dev);
  1531. unsigned int txsize = priv->dma_tx_size;
  1532. unsigned int entry;
  1533. int i, csum_insertion = 0, is_jumbo = 0;
  1534. int nfrags = skb_shinfo(skb)->nr_frags;
  1535. struct dma_desc *desc, *first;
  1536. unsigned int nopaged_len = skb_headlen(skb);
  1537. if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
  1538. if (!netif_queue_stopped(dev)) {
  1539. netif_stop_queue(dev);
  1540. /* This is a hard error, log it. */
  1541. pr_err("%s: Tx Ring full when queue awake\n", __func__);
  1542. }
  1543. return NETDEV_TX_BUSY;
  1544. }
  1545. spin_lock(&priv->tx_lock);
  1546. if (priv->tx_path_in_lpi_mode)
  1547. stmmac_disable_eee_mode(priv);
  1548. entry = priv->cur_tx % txsize;
  1549. #ifdef STMMAC_XMIT_DEBUG
  1550. if ((skb->len > ETH_FRAME_LEN) || nfrags)
  1551. pr_debug("%s: [entry %d]: skb addr %p len: %d nopagedlen: %d\n"
  1552. "\tn_frags: %d - ip_summed: %d - %s gso\n"
  1553. "\ttx_count_frames %d\n", __func__, entry,
  1554. skb, skb->len, nopaged_len, nfrags, skb->ip_summed,
  1555. !skb_is_gso(skb) ? "isn't" : "is",
  1556. priv->tx_count_frames);
  1557. #endif
  1558. csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
  1559. if (priv->extend_desc)
  1560. desc = (struct dma_desc *)(priv->dma_etx + entry);
  1561. else
  1562. desc = priv->dma_tx + entry;
  1563. first = desc;
  1564. #ifdef STMMAC_XMIT_DEBUG
  1565. if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
  1566. pr_debug("\tskb len: %d, nopaged_len: %d,\n"
  1567. "\t\tn_frags: %d, ip_summed: %d\n",
  1568. skb->len, nopaged_len, nfrags, skb->ip_summed);
  1569. #endif
  1570. priv->tx_skbuff[entry] = skb;
  1571. /* To program the descriptors according to the size of the frame */
  1572. if (priv->mode == STMMAC_RING_MODE) {
  1573. is_jumbo = priv->hw->ring->is_jumbo_frm(skb->len,
  1574. priv->plat->enh_desc);
  1575. if (unlikely(is_jumbo))
  1576. entry = priv->hw->ring->jumbo_frm(priv, skb,
  1577. csum_insertion);
  1578. } else {
  1579. is_jumbo = priv->hw->chain->is_jumbo_frm(skb->len,
  1580. priv->plat->enh_desc);
  1581. if (unlikely(is_jumbo))
  1582. entry = priv->hw->chain->jumbo_frm(priv, skb,
  1583. csum_insertion);
  1584. }
  1585. if (likely(!is_jumbo)) {
  1586. desc->des2 = dma_map_single(priv->device, skb->data,
  1587. nopaged_len, DMA_TO_DEVICE);
  1588. priv->tx_skbuff_dma[entry] = desc->des2;
  1589. priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
  1590. csum_insertion, priv->mode);
  1591. } else
  1592. desc = first;
  1593. for (i = 0; i < nfrags; i++) {
  1594. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1595. int len = skb_frag_size(frag);
  1596. entry = (++priv->cur_tx) % txsize;
  1597. if (priv->extend_desc)
  1598. desc = (struct dma_desc *)(priv->dma_etx + entry);
  1599. else
  1600. desc = priv->dma_tx + entry;
  1601. TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
  1602. desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
  1603. DMA_TO_DEVICE);
  1604. priv->tx_skbuff_dma[entry] = desc->des2;
  1605. priv->tx_skbuff[entry] = NULL;
  1606. priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
  1607. priv->mode);
  1608. wmb();
  1609. priv->hw->desc->set_tx_owner(desc);
  1610. wmb();
  1611. }
  1612. /* Finalize the latest segment. */
  1613. priv->hw->desc->close_tx_desc(desc);
  1614. wmb();
  1615. /* According to the coalesce parameter the IC bit for the latest
  1616. * segment could be reset and the timer re-started to invoke the
  1617. * stmmac_tx function. This approach takes care about the fragments.
  1618. */
  1619. priv->tx_count_frames += nfrags + 1;
  1620. if (priv->tx_coal_frames > priv->tx_count_frames) {
  1621. priv->hw->desc->clear_tx_ic(desc);
  1622. priv->xstats.tx_reset_ic_bit++;
  1623. TX_DBG("\t[entry %d]: tx_count_frames %d\n", entry,
  1624. priv->tx_count_frames);
  1625. mod_timer(&priv->txtimer,
  1626. STMMAC_COAL_TIMER(priv->tx_coal_timer));
  1627. } else
  1628. priv->tx_count_frames = 0;
  1629. /* To avoid raise condition */
  1630. priv->hw->desc->set_tx_owner(first);
  1631. wmb();
  1632. priv->cur_tx++;
  1633. #ifdef STMMAC_XMIT_DEBUG
  1634. if (netif_msg_pktdata(priv)) {
  1635. pr_info("%s: curr %d dirty=%d entry=%d, first=%p, nfrags=%d"
  1636. __func__, (priv->cur_tx % txsize),
  1637. (priv->dirty_tx % txsize), entry, first, nfrags);
  1638. if (priv->extend_desc)
  1639. stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
  1640. else
  1641. stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
  1642. pr_info(">>> frame to be transmitted: ");
  1643. print_pkt(skb->data, skb->len);
  1644. }
  1645. #endif
  1646. if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
  1647. TX_DBG("%s: stop transmitted packets\n", __func__);
  1648. netif_stop_queue(dev);
  1649. }
  1650. dev->stats.tx_bytes += skb->len;
  1651. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1652. priv->hwts_tx_en)) {
  1653. /* declare that device is doing timestamping */
  1654. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1655. priv->hw->desc->enable_tx_timestamp(first);
  1656. }
  1657. if (!priv->hwts_tx_en)
  1658. skb_tx_timestamp(skb);
  1659. priv->hw->dma->enable_dma_transmission(priv->ioaddr);
  1660. spin_unlock(&priv->tx_lock);
  1661. return NETDEV_TX_OK;
  1662. }
  1663. /**
  1664. * stmmac_rx_refill: refill used skb preallocated buffers
  1665. * @priv: driver private structure
  1666. * Description : this is to reallocate the skb for the reception process
  1667. * that is based on zero-copy.
  1668. */
  1669. static inline void stmmac_rx_refill(struct stmmac_priv *priv)
  1670. {
  1671. unsigned int rxsize = priv->dma_rx_size;
  1672. int bfsize = priv->dma_buf_sz;
  1673. for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
  1674. unsigned int entry = priv->dirty_rx % rxsize;
  1675. struct dma_desc *p;
  1676. if (priv->extend_desc)
  1677. p = (struct dma_desc *)(priv->dma_erx + entry);
  1678. else
  1679. p = priv->dma_rx + entry;
  1680. if (likely(priv->rx_skbuff[entry] == NULL)) {
  1681. struct sk_buff *skb;
  1682. skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
  1683. if (unlikely(skb == NULL))
  1684. break;
  1685. priv->rx_skbuff[entry] = skb;
  1686. priv->rx_skbuff_dma[entry] =
  1687. dma_map_single(priv->device, skb->data, bfsize,
  1688. DMA_FROM_DEVICE);
  1689. p->des2 = priv->rx_skbuff_dma[entry];
  1690. priv->hw->ring->refill_desc3(priv, p);
  1691. RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
  1692. }
  1693. wmb();
  1694. priv->hw->desc->set_rx_owner(p);
  1695. wmb();
  1696. }
  1697. }
  1698. /**
  1699. * stmmac_rx_refill: refill used skb preallocated buffers
  1700. * @priv: driver private structure
  1701. * @limit: napi bugget.
  1702. * Description : this the function called by the napi poll method.
  1703. * It gets all the frames inside the ring.
  1704. */
  1705. static int stmmac_rx(struct stmmac_priv *priv, int limit)
  1706. {
  1707. unsigned int rxsize = priv->dma_rx_size;
  1708. unsigned int entry = priv->cur_rx % rxsize;
  1709. unsigned int next_entry;
  1710. unsigned int count = 0;
  1711. int coe = priv->plat->rx_coe;
  1712. #ifdef STMMAC_RX_DEBUG
  1713. if (netif_msg_hw(priv)) {
  1714. pr_debug(">>> stmmac_rx: descriptor ring:\n");
  1715. if (priv->extend_desc)
  1716. stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
  1717. else
  1718. stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
  1719. }
  1720. #endif
  1721. while (count < limit) {
  1722. int status;
  1723. struct dma_desc *p;
  1724. if (priv->extend_desc)
  1725. p = (struct dma_desc *)(priv->dma_erx + entry);
  1726. else
  1727. p = priv->dma_rx + entry;
  1728. if (priv->hw->desc->get_rx_owner(p))
  1729. break;
  1730. count++;
  1731. next_entry = (++priv->cur_rx) % rxsize;
  1732. if (priv->extend_desc)
  1733. prefetch(priv->dma_erx + next_entry);
  1734. else
  1735. prefetch(priv->dma_rx + next_entry);
  1736. /* read the status of the incoming frame */
  1737. status = priv->hw->desc->rx_status(&priv->dev->stats,
  1738. &priv->xstats, p);
  1739. if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
  1740. priv->hw->desc->rx_extended_status(&priv->dev->stats,
  1741. &priv->xstats,
  1742. priv->dma_erx +
  1743. entry);
  1744. if (unlikely(status == discard_frame)) {
  1745. priv->dev->stats.rx_errors++;
  1746. if (priv->hwts_rx_en && !priv->extend_desc) {
  1747. /* DESC2 & DESC3 will be overwitten by device
  1748. * with timestamp value, hence reinitialize
  1749. * them in stmmac_rx_refill() function so that
  1750. * device can reuse it.
  1751. */
  1752. priv->rx_skbuff[entry] = NULL;
  1753. dma_unmap_single(priv->device,
  1754. priv->rx_skbuff_dma[entry],
  1755. priv->dma_buf_sz,
  1756. DMA_FROM_DEVICE);
  1757. }
  1758. } else {
  1759. struct sk_buff *skb;
  1760. int frame_len;
  1761. frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
  1762. /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
  1763. * Type frames (LLC/LLC-SNAP)
  1764. */
  1765. if (unlikely(status != llc_snap))
  1766. frame_len -= ETH_FCS_LEN;
  1767. #ifdef STMMAC_RX_DEBUG
  1768. if (frame_len > ETH_FRAME_LEN)
  1769. pr_debug("\tRX frame size %d, COE status: %d\n",
  1770. frame_len, status);
  1771. if (netif_msg_hw(priv))
  1772. pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
  1773. p, entry, p->des2);
  1774. #endif
  1775. skb = priv->rx_skbuff[entry];
  1776. if (unlikely(!skb)) {
  1777. pr_err("%s: Inconsistent Rx descriptor chain\n",
  1778. priv->dev->name);
  1779. priv->dev->stats.rx_dropped++;
  1780. break;
  1781. }
  1782. prefetch(skb->data - NET_IP_ALIGN);
  1783. priv->rx_skbuff[entry] = NULL;
  1784. stmmac_get_rx_hwtstamp(priv, entry, skb);
  1785. skb_put(skb, frame_len);
  1786. dma_unmap_single(priv->device,
  1787. priv->rx_skbuff_dma[entry],
  1788. priv->dma_buf_sz, DMA_FROM_DEVICE);
  1789. #ifdef STMMAC_RX_DEBUG
  1790. if (netif_msg_pktdata(priv)) {
  1791. pr_info(" frame received (%dbytes)", frame_len);
  1792. print_pkt(skb->data, frame_len);
  1793. }
  1794. #endif
  1795. skb->protocol = eth_type_trans(skb, priv->dev);
  1796. if (unlikely(!coe))
  1797. skb_checksum_none_assert(skb);
  1798. else
  1799. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1800. napi_gro_receive(&priv->napi, skb);
  1801. priv->dev->stats.rx_packets++;
  1802. priv->dev->stats.rx_bytes += frame_len;
  1803. }
  1804. entry = next_entry;
  1805. }
  1806. stmmac_rx_refill(priv);
  1807. priv->xstats.rx_pkt_n += count;
  1808. return count;
  1809. }
  1810. /**
  1811. * stmmac_poll - stmmac poll method (NAPI)
  1812. * @napi : pointer to the napi structure.
  1813. * @budget : maximum number of packets that the current CPU can receive from
  1814. * all interfaces.
  1815. * Description :
  1816. * To look at the incoming frames and clear the tx resources.
  1817. */
  1818. static int stmmac_poll(struct napi_struct *napi, int budget)
  1819. {
  1820. struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
  1821. int work_done = 0;
  1822. priv->xstats.napi_poll++;
  1823. stmmac_tx_clean(priv);
  1824. work_done = stmmac_rx(priv, budget);
  1825. if (work_done < budget) {
  1826. napi_complete(napi);
  1827. stmmac_enable_dma_irq(priv);
  1828. }
  1829. return work_done;
  1830. }
  1831. /**
  1832. * stmmac_tx_timeout
  1833. * @dev : Pointer to net device structure
  1834. * Description: this function is called when a packet transmission fails to
  1835. * complete within a reasonable time. The driver will mark the error in the
  1836. * netdev structure and arrange for the device to be reset to a sane state
  1837. * in order to transmit a new packet.
  1838. */
  1839. static void stmmac_tx_timeout(struct net_device *dev)
  1840. {
  1841. struct stmmac_priv *priv = netdev_priv(dev);
  1842. /* Clear Tx resources and restart transmitting again */
  1843. stmmac_tx_err(priv);
  1844. }
  1845. /* Configuration changes (passed on by ifconfig) */
  1846. static int stmmac_config(struct net_device *dev, struct ifmap *map)
  1847. {
  1848. if (dev->flags & IFF_UP) /* can't act on a running interface */
  1849. return -EBUSY;
  1850. /* Don't allow changing the I/O address */
  1851. if (map->base_addr != dev->base_addr) {
  1852. pr_warn("%s: can't change I/O address\n", dev->name);
  1853. return -EOPNOTSUPP;
  1854. }
  1855. /* Don't allow changing the IRQ */
  1856. if (map->irq != dev->irq) {
  1857. pr_warn("%s: not change IRQ number %d\n", dev->name, dev->irq);
  1858. return -EOPNOTSUPP;
  1859. }
  1860. return 0;
  1861. }
  1862. /**
  1863. * stmmac_set_rx_mode - entry point for multicast addressing
  1864. * @dev : pointer to the device structure
  1865. * Description:
  1866. * This function is a driver entry point which gets called by the kernel
  1867. * whenever multicast addresses must be enabled/disabled.
  1868. * Return value:
  1869. * void.
  1870. */
  1871. static void stmmac_set_rx_mode(struct net_device *dev)
  1872. {
  1873. struct stmmac_priv *priv = netdev_priv(dev);
  1874. spin_lock(&priv->lock);
  1875. priv->hw->mac->set_filter(dev, priv->synopsys_id);
  1876. spin_unlock(&priv->lock);
  1877. }
  1878. /**
  1879. * stmmac_change_mtu - entry point to change MTU size for the device.
  1880. * @dev : device pointer.
  1881. * @new_mtu : the new MTU size for the device.
  1882. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  1883. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  1884. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  1885. * Return value:
  1886. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1887. * file on failure.
  1888. */
  1889. static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
  1890. {
  1891. struct stmmac_priv *priv = netdev_priv(dev);
  1892. int max_mtu;
  1893. if (netif_running(dev)) {
  1894. pr_err("%s: must be stopped to change its MTU\n", dev->name);
  1895. return -EBUSY;
  1896. }
  1897. if (priv->plat->enh_desc)
  1898. max_mtu = JUMBO_LEN;
  1899. else
  1900. max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
  1901. if ((new_mtu < 46) || (new_mtu > max_mtu)) {
  1902. pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
  1903. return -EINVAL;
  1904. }
  1905. dev->mtu = new_mtu;
  1906. netdev_update_features(dev);
  1907. return 0;
  1908. }
  1909. static netdev_features_t stmmac_fix_features(struct net_device *dev,
  1910. netdev_features_t features)
  1911. {
  1912. struct stmmac_priv *priv = netdev_priv(dev);
  1913. if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
  1914. features &= ~NETIF_F_RXCSUM;
  1915. else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
  1916. features &= ~NETIF_F_IPV6_CSUM;
  1917. if (!priv->plat->tx_coe)
  1918. features &= ~NETIF_F_ALL_CSUM;
  1919. /* Some GMAC devices have a bugged Jumbo frame support that
  1920. * needs to have the Tx COE disabled for oversized frames
  1921. * (due to limited buffer sizes). In this case we disable
  1922. * the TX csum insertionin the TDES and not use SF.
  1923. */
  1924. if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
  1925. features &= ~NETIF_F_ALL_CSUM;
  1926. return features;
  1927. }
  1928. /**
  1929. * stmmac_interrupt - main ISR
  1930. * @irq: interrupt number.
  1931. * @dev_id: to pass the net device pointer.
  1932. * Description: this is the main driver interrupt service routine.
  1933. * It calls the DMA ISR and also the core ISR to manage PMT, MMC, LPI
  1934. * interrupts.
  1935. */
  1936. static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
  1937. {
  1938. struct net_device *dev = (struct net_device *)dev_id;
  1939. struct stmmac_priv *priv = netdev_priv(dev);
  1940. if (unlikely(!dev)) {
  1941. pr_err("%s: invalid dev pointer\n", __func__);
  1942. return IRQ_NONE;
  1943. }
  1944. /* To handle GMAC own interrupts */
  1945. if (priv->plat->has_gmac) {
  1946. int status = priv->hw->mac->host_irq_status((void __iomem *)
  1947. dev->base_addr,
  1948. &priv->xstats);
  1949. if (unlikely(status)) {
  1950. /* For LPI we need to save the tx status */
  1951. if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
  1952. priv->tx_path_in_lpi_mode = true;
  1953. if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
  1954. priv->tx_path_in_lpi_mode = false;
  1955. }
  1956. }
  1957. /* To handle DMA interrupts */
  1958. stmmac_dma_interrupt(priv);
  1959. return IRQ_HANDLED;
  1960. }
  1961. #ifdef CONFIG_NET_POLL_CONTROLLER
  1962. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  1963. * to allow network I/O with interrupts disabled.
  1964. */
  1965. static void stmmac_poll_controller(struct net_device *dev)
  1966. {
  1967. disable_irq(dev->irq);
  1968. stmmac_interrupt(dev->irq, dev);
  1969. enable_irq(dev->irq);
  1970. }
  1971. #endif
  1972. /**
  1973. * stmmac_ioctl - Entry point for the Ioctl
  1974. * @dev: Device pointer.
  1975. * @rq: An IOCTL specefic structure, that can contain a pointer to
  1976. * a proprietary structure used to pass information to the driver.
  1977. * @cmd: IOCTL command
  1978. * Description:
  1979. * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
  1980. */
  1981. static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1982. {
  1983. struct stmmac_priv *priv = netdev_priv(dev);
  1984. int ret = -EOPNOTSUPP;
  1985. if (!netif_running(dev))
  1986. return -EINVAL;
  1987. switch (cmd) {
  1988. case SIOCGMIIPHY:
  1989. case SIOCGMIIREG:
  1990. case SIOCSMIIREG:
  1991. if (!priv->phydev)
  1992. return -EINVAL;
  1993. ret = phy_mii_ioctl(priv->phydev, rq, cmd);
  1994. break;
  1995. case SIOCSHWTSTAMP:
  1996. ret = stmmac_hwtstamp_ioctl(dev, rq);
  1997. break;
  1998. default:
  1999. break;
  2000. }
  2001. return ret;
  2002. }
  2003. #ifdef CONFIG_STMMAC_DEBUG_FS
  2004. static struct dentry *stmmac_fs_dir;
  2005. static struct dentry *stmmac_rings_status;
  2006. static struct dentry *stmmac_dma_cap;
  2007. static void sysfs_display_ring(void *head, int size, int extend_desc,
  2008. struct seq_file *seq)
  2009. {
  2010. int i;
  2011. struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
  2012. struct dma_desc *p = (struct dma_desc *)head;
  2013. for (i = 0; i < size; i++) {
  2014. u64 x;
  2015. if (extend_desc) {
  2016. x = *(u64 *) ep;
  2017. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  2018. i, (unsigned int)virt_to_phys(ep),
  2019. (unsigned int)x, (unsigned int)(x >> 32),
  2020. ep->basic.des2, ep->basic.des3);
  2021. ep++;
  2022. } else {
  2023. x = *(u64 *) p;
  2024. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  2025. i, (unsigned int)virt_to_phys(ep),
  2026. (unsigned int)x, (unsigned int)(x >> 32),
  2027. p->des2, p->des3);
  2028. p++;
  2029. }
  2030. seq_printf(seq, "\n");
  2031. }
  2032. }
  2033. static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
  2034. {
  2035. struct net_device *dev = seq->private;
  2036. struct stmmac_priv *priv = netdev_priv(dev);
  2037. unsigned int txsize = priv->dma_tx_size;
  2038. unsigned int rxsize = priv->dma_rx_size;
  2039. if (priv->extend_desc) {
  2040. seq_printf(seq, "Extended RX descriptor ring:\n");
  2041. sysfs_display_ring((void *)priv->dma_erx, rxsize, 1, seq);
  2042. seq_printf(seq, "Extended TX descriptor ring:\n");
  2043. sysfs_display_ring((void *)priv->dma_etx, txsize, 1, seq);
  2044. } else {
  2045. seq_printf(seq, "RX descriptor ring:\n");
  2046. sysfs_display_ring((void *)priv->dma_rx, rxsize, 0, seq);
  2047. seq_printf(seq, "TX descriptor ring:\n");
  2048. sysfs_display_ring((void *)priv->dma_tx, txsize, 0, seq);
  2049. }
  2050. return 0;
  2051. }
  2052. static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
  2053. {
  2054. return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
  2055. }
  2056. static const struct file_operations stmmac_rings_status_fops = {
  2057. .owner = THIS_MODULE,
  2058. .open = stmmac_sysfs_ring_open,
  2059. .read = seq_read,
  2060. .llseek = seq_lseek,
  2061. .release = single_release,
  2062. };
  2063. static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
  2064. {
  2065. struct net_device *dev = seq->private;
  2066. struct stmmac_priv *priv = netdev_priv(dev);
  2067. if (!priv->hw_cap_support) {
  2068. seq_printf(seq, "DMA HW features not supported\n");
  2069. return 0;
  2070. }
  2071. seq_printf(seq, "==============================\n");
  2072. seq_printf(seq, "\tDMA HW features\n");
  2073. seq_printf(seq, "==============================\n");
  2074. seq_printf(seq, "\t10/100 Mbps %s\n",
  2075. (priv->dma_cap.mbps_10_100) ? "Y" : "N");
  2076. seq_printf(seq, "\t1000 Mbps %s\n",
  2077. (priv->dma_cap.mbps_1000) ? "Y" : "N");
  2078. seq_printf(seq, "\tHalf duple %s\n",
  2079. (priv->dma_cap.half_duplex) ? "Y" : "N");
  2080. seq_printf(seq, "\tHash Filter: %s\n",
  2081. (priv->dma_cap.hash_filter) ? "Y" : "N");
  2082. seq_printf(seq, "\tMultiple MAC address registers: %s\n",
  2083. (priv->dma_cap.multi_addr) ? "Y" : "N");
  2084. seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
  2085. (priv->dma_cap.pcs) ? "Y" : "N");
  2086. seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
  2087. (priv->dma_cap.sma_mdio) ? "Y" : "N");
  2088. seq_printf(seq, "\tPMT Remote wake up: %s\n",
  2089. (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
  2090. seq_printf(seq, "\tPMT Magic Frame: %s\n",
  2091. (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
  2092. seq_printf(seq, "\tRMON module: %s\n",
  2093. (priv->dma_cap.rmon) ? "Y" : "N");
  2094. seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
  2095. (priv->dma_cap.time_stamp) ? "Y" : "N");
  2096. seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
  2097. (priv->dma_cap.atime_stamp) ? "Y" : "N");
  2098. seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
  2099. (priv->dma_cap.eee) ? "Y" : "N");
  2100. seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
  2101. seq_printf(seq, "\tChecksum Offload in TX: %s\n",
  2102. (priv->dma_cap.tx_coe) ? "Y" : "N");
  2103. seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
  2104. (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
  2105. seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
  2106. (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
  2107. seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
  2108. (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
  2109. seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
  2110. priv->dma_cap.number_rx_channel);
  2111. seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
  2112. priv->dma_cap.number_tx_channel);
  2113. seq_printf(seq, "\tEnhanced descriptors: %s\n",
  2114. (priv->dma_cap.enh_desc) ? "Y" : "N");
  2115. return 0;
  2116. }
  2117. static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
  2118. {
  2119. return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
  2120. }
  2121. static const struct file_operations stmmac_dma_cap_fops = {
  2122. .owner = THIS_MODULE,
  2123. .open = stmmac_sysfs_dma_cap_open,
  2124. .read = seq_read,
  2125. .llseek = seq_lseek,
  2126. .release = single_release,
  2127. };
  2128. static int stmmac_init_fs(struct net_device *dev)
  2129. {
  2130. /* Create debugfs entries */
  2131. stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
  2132. if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
  2133. pr_err("ERROR %s, debugfs create directory failed\n",
  2134. STMMAC_RESOURCE_NAME);
  2135. return -ENOMEM;
  2136. }
  2137. /* Entry to report DMA RX/TX rings */
  2138. stmmac_rings_status = debugfs_create_file("descriptors_status",
  2139. S_IRUGO, stmmac_fs_dir, dev,
  2140. &stmmac_rings_status_fops);
  2141. if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
  2142. pr_info("ERROR creating stmmac ring debugfs file\n");
  2143. debugfs_remove(stmmac_fs_dir);
  2144. return -ENOMEM;
  2145. }
  2146. /* Entry to report the DMA HW features */
  2147. stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
  2148. dev, &stmmac_dma_cap_fops);
  2149. if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
  2150. pr_info("ERROR creating stmmac MMC debugfs file\n");
  2151. debugfs_remove(stmmac_rings_status);
  2152. debugfs_remove(stmmac_fs_dir);
  2153. return -ENOMEM;
  2154. }
  2155. return 0;
  2156. }
  2157. static void stmmac_exit_fs(void)
  2158. {
  2159. debugfs_remove(stmmac_rings_status);
  2160. debugfs_remove(stmmac_dma_cap);
  2161. debugfs_remove(stmmac_fs_dir);
  2162. }
  2163. #endif /* CONFIG_STMMAC_DEBUG_FS */
  2164. static const struct net_device_ops stmmac_netdev_ops = {
  2165. .ndo_open = stmmac_open,
  2166. .ndo_start_xmit = stmmac_xmit,
  2167. .ndo_stop = stmmac_release,
  2168. .ndo_change_mtu = stmmac_change_mtu,
  2169. .ndo_fix_features = stmmac_fix_features,
  2170. .ndo_set_rx_mode = stmmac_set_rx_mode,
  2171. .ndo_tx_timeout = stmmac_tx_timeout,
  2172. .ndo_do_ioctl = stmmac_ioctl,
  2173. .ndo_set_config = stmmac_config,
  2174. #ifdef CONFIG_NET_POLL_CONTROLLER
  2175. .ndo_poll_controller = stmmac_poll_controller,
  2176. #endif
  2177. .ndo_set_mac_address = eth_mac_addr,
  2178. };
  2179. /**
  2180. * stmmac_hw_init - Init the MAC device
  2181. * @priv: driver private structure
  2182. * Description: this function detects which MAC device
  2183. * (GMAC/MAC10-100) has to attached, checks the HW capability
  2184. * (if supported) and sets the driver's features (for example
  2185. * to use the ring or chaine mode or support the normal/enh
  2186. * descriptor structure).
  2187. */
  2188. static int stmmac_hw_init(struct stmmac_priv *priv)
  2189. {
  2190. int ret;
  2191. struct mac_device_info *mac;
  2192. /* Identify the MAC HW device */
  2193. if (priv->plat->has_gmac) {
  2194. priv->dev->priv_flags |= IFF_UNICAST_FLT;
  2195. mac = dwmac1000_setup(priv->ioaddr);
  2196. } else {
  2197. mac = dwmac100_setup(priv->ioaddr);
  2198. }
  2199. if (!mac)
  2200. return -ENOMEM;
  2201. priv->hw = mac;
  2202. /* Get and dump the chip ID */
  2203. priv->synopsys_id = stmmac_get_synopsys_id(priv);
  2204. /* To use alternate (extended) or normal descriptor structures */
  2205. stmmac_selec_desc_mode(priv);
  2206. /* To use the chained or ring mode */
  2207. if (chain_mode) {
  2208. priv->hw->chain = &chain_mode_ops;
  2209. pr_info(" Chain mode enabled\n");
  2210. priv->mode = STMMAC_CHAIN_MODE;
  2211. } else {
  2212. priv->hw->ring = &ring_mode_ops;
  2213. pr_info(" Ring mode enabled\n");
  2214. priv->mode = STMMAC_RING_MODE;
  2215. }
  2216. /* Get the HW capability (new GMAC newer than 3.50a) */
  2217. priv->hw_cap_support = stmmac_get_hw_features(priv);
  2218. if (priv->hw_cap_support) {
  2219. pr_info(" DMA HW capability register supported");
  2220. /* We can override some gmac/dma configuration fields: e.g.
  2221. * enh_desc, tx_coe (e.g. that are passed through the
  2222. * platform) with the values from the HW capability
  2223. * register (if supported).
  2224. */
  2225. priv->plat->enh_desc = priv->dma_cap.enh_desc;
  2226. priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
  2227. priv->plat->tx_coe = priv->dma_cap.tx_coe;
  2228. if (priv->dma_cap.rx_coe_type2)
  2229. priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
  2230. else if (priv->dma_cap.rx_coe_type1)
  2231. priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
  2232. } else
  2233. pr_info(" No HW DMA feature register supported");
  2234. ret = priv->hw->mac->rx_ipc(priv->ioaddr);
  2235. if (!ret) {
  2236. pr_warn(" RX IPC Checksum Offload not configured.\n");
  2237. priv->plat->rx_coe = STMMAC_RX_COE_NONE;
  2238. }
  2239. if (priv->plat->rx_coe)
  2240. pr_info(" RX Checksum Offload Engine supported (type %d)\n",
  2241. priv->plat->rx_coe);
  2242. if (priv->plat->tx_coe)
  2243. pr_info(" TX Checksum insertion supported\n");
  2244. if (priv->plat->pmt) {
  2245. pr_info(" Wake-Up On Lan supported\n");
  2246. device_set_wakeup_capable(priv->device, 1);
  2247. }
  2248. return 0;
  2249. }
  2250. /**
  2251. * stmmac_dvr_probe
  2252. * @device: device pointer
  2253. * @plat_dat: platform data pointer
  2254. * @addr: iobase memory address
  2255. * Description: this is the main probe function used to
  2256. * call the alloc_etherdev, allocate the priv structure.
  2257. */
  2258. struct stmmac_priv *stmmac_dvr_probe(struct device *device,
  2259. struct plat_stmmacenet_data *plat_dat,
  2260. void __iomem *addr)
  2261. {
  2262. int ret = 0;
  2263. struct net_device *ndev = NULL;
  2264. struct stmmac_priv *priv;
  2265. ndev = alloc_etherdev(sizeof(struct stmmac_priv));
  2266. if (!ndev)
  2267. return NULL;
  2268. SET_NETDEV_DEV(ndev, device);
  2269. priv = netdev_priv(ndev);
  2270. priv->device = device;
  2271. priv->dev = ndev;
  2272. ether_setup(ndev);
  2273. stmmac_set_ethtool_ops(ndev);
  2274. priv->pause = pause;
  2275. priv->plat = plat_dat;
  2276. priv->ioaddr = addr;
  2277. priv->dev->base_addr = (unsigned long)addr;
  2278. /* Verify driver arguments */
  2279. stmmac_verify_args();
  2280. /* Override with kernel parameters if supplied XXX CRS XXX
  2281. * this needs to have multiple instances
  2282. */
  2283. if ((phyaddr >= 0) && (phyaddr <= 31))
  2284. priv->plat->phy_addr = phyaddr;
  2285. /* Init MAC and get the capabilities */
  2286. ret = stmmac_hw_init(priv);
  2287. if (ret)
  2288. goto error_free_netdev;
  2289. ndev->netdev_ops = &stmmac_netdev_ops;
  2290. ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2291. NETIF_F_RXCSUM;
  2292. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  2293. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  2294. #ifdef STMMAC_VLAN_TAG_USED
  2295. /* Both mac100 and gmac support receive VLAN tag detection */
  2296. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  2297. #endif
  2298. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  2299. if (flow_ctrl)
  2300. priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
  2301. /* Rx Watchdog is available in the COREs newer than the 3.40.
  2302. * In some case, for example on bugged HW this feature
  2303. * has to be disable and this can be done by passing the
  2304. * riwt_off field from the platform.
  2305. */
  2306. if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
  2307. priv->use_riwt = 1;
  2308. pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
  2309. }
  2310. netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
  2311. spin_lock_init(&priv->lock);
  2312. spin_lock_init(&priv->tx_lock);
  2313. ret = register_netdev(ndev);
  2314. if (ret) {
  2315. pr_err("%s: ERROR %i registering the device\n", __func__, ret);
  2316. goto error_netdev_register;
  2317. }
  2318. priv->stmmac_clk = clk_get(priv->device, STMMAC_RESOURCE_NAME);
  2319. if (IS_ERR(priv->stmmac_clk)) {
  2320. pr_warn("%s: warning: cannot get CSR clock\n", __func__);
  2321. goto error_clk_get;
  2322. }
  2323. /* If a specific clk_csr value is passed from the platform
  2324. * this means that the CSR Clock Range selection cannot be
  2325. * changed at run-time and it is fixed. Viceversa the driver'll try to
  2326. * set the MDC clock dynamically according to the csr actual
  2327. * clock input.
  2328. */
  2329. if (!priv->plat->clk_csr)
  2330. stmmac_clk_csr_set(priv);
  2331. else
  2332. priv->clk_csr = priv->plat->clk_csr;
  2333. stmmac_check_pcs_mode(priv);
  2334. if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
  2335. priv->pcs != STMMAC_PCS_RTBI) {
  2336. /* MDIO bus Registration */
  2337. ret = stmmac_mdio_register(ndev);
  2338. if (ret < 0) {
  2339. pr_debug("%s: MDIO bus (id: %d) registration failed",
  2340. __func__, priv->plat->bus_id);
  2341. goto error_mdio_register;
  2342. }
  2343. }
  2344. return priv;
  2345. error_mdio_register:
  2346. clk_put(priv->stmmac_clk);
  2347. error_clk_get:
  2348. unregister_netdev(ndev);
  2349. error_netdev_register:
  2350. netif_napi_del(&priv->napi);
  2351. error_free_netdev:
  2352. free_netdev(ndev);
  2353. return NULL;
  2354. }
  2355. /**
  2356. * stmmac_dvr_remove
  2357. * @ndev: net device pointer
  2358. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  2359. * changes the link status, releases the DMA descriptor rings.
  2360. */
  2361. int stmmac_dvr_remove(struct net_device *ndev)
  2362. {
  2363. struct stmmac_priv *priv = netdev_priv(ndev);
  2364. pr_info("%s:\n\tremoving driver", __func__);
  2365. priv->hw->dma->stop_rx(priv->ioaddr);
  2366. priv->hw->dma->stop_tx(priv->ioaddr);
  2367. stmmac_set_mac(priv->ioaddr, false);
  2368. if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
  2369. priv->pcs != STMMAC_PCS_RTBI)
  2370. stmmac_mdio_unregister(ndev);
  2371. netif_carrier_off(ndev);
  2372. unregister_netdev(ndev);
  2373. free_netdev(ndev);
  2374. return 0;
  2375. }
  2376. #ifdef CONFIG_PM
  2377. int stmmac_suspend(struct net_device *ndev)
  2378. {
  2379. struct stmmac_priv *priv = netdev_priv(ndev);
  2380. unsigned long flags;
  2381. if (!ndev || !netif_running(ndev))
  2382. return 0;
  2383. if (priv->phydev)
  2384. phy_stop(priv->phydev);
  2385. spin_lock_irqsave(&priv->lock, flags);
  2386. netif_device_detach(ndev);
  2387. netif_stop_queue(ndev);
  2388. napi_disable(&priv->napi);
  2389. /* Stop TX/RX DMA */
  2390. priv->hw->dma->stop_tx(priv->ioaddr);
  2391. priv->hw->dma->stop_rx(priv->ioaddr);
  2392. stmmac_clear_descriptors(priv);
  2393. /* Enable Power down mode by programming the PMT regs */
  2394. if (device_may_wakeup(priv->device))
  2395. priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
  2396. else {
  2397. stmmac_set_mac(priv->ioaddr, false);
  2398. /* Disable clock in case of PWM is off */
  2399. clk_disable_unprepare(priv->stmmac_clk);
  2400. }
  2401. spin_unlock_irqrestore(&priv->lock, flags);
  2402. return 0;
  2403. }
  2404. int stmmac_resume(struct net_device *ndev)
  2405. {
  2406. struct stmmac_priv *priv = netdev_priv(ndev);
  2407. unsigned long flags;
  2408. if (!netif_running(ndev))
  2409. return 0;
  2410. spin_lock_irqsave(&priv->lock, flags);
  2411. /* Power Down bit, into the PM register, is cleared
  2412. * automatically as soon as a magic packet or a Wake-up frame
  2413. * is received. Anyway, it's better to manually clear
  2414. * this bit because it can generate problems while resuming
  2415. * from another devices (e.g. serial console).
  2416. */
  2417. if (device_may_wakeup(priv->device))
  2418. priv->hw->mac->pmt(priv->ioaddr, 0);
  2419. else
  2420. /* enable the clk prevously disabled */
  2421. clk_prepare_enable(priv->stmmac_clk);
  2422. netif_device_attach(ndev);
  2423. /* Enable the MAC and DMA */
  2424. stmmac_set_mac(priv->ioaddr, true);
  2425. priv->hw->dma->start_tx(priv->ioaddr);
  2426. priv->hw->dma->start_rx(priv->ioaddr);
  2427. napi_enable(&priv->napi);
  2428. netif_start_queue(ndev);
  2429. spin_unlock_irqrestore(&priv->lock, flags);
  2430. if (priv->phydev)
  2431. phy_start(priv->phydev);
  2432. return 0;
  2433. }
  2434. int stmmac_freeze(struct net_device *ndev)
  2435. {
  2436. if (!ndev || !netif_running(ndev))
  2437. return 0;
  2438. return stmmac_release(ndev);
  2439. }
  2440. int stmmac_restore(struct net_device *ndev)
  2441. {
  2442. if (!ndev || !netif_running(ndev))
  2443. return 0;
  2444. return stmmac_open(ndev);
  2445. }
  2446. #endif /* CONFIG_PM */
  2447. /* Driver can be configured w/ and w/ both PCI and Platf drivers
  2448. * depending on the configuration selected.
  2449. */
  2450. static int __init stmmac_init(void)
  2451. {
  2452. int ret;
  2453. ret = stmmac_register_platform();
  2454. if (ret)
  2455. goto err;
  2456. ret = stmmac_register_pci();
  2457. if (ret)
  2458. goto err_pci;
  2459. return 0;
  2460. err_pci:
  2461. stmmac_unregister_platform();
  2462. err:
  2463. pr_err("stmmac: driver registration failed\n");
  2464. return ret;
  2465. }
  2466. static void __exit stmmac_exit(void)
  2467. {
  2468. stmmac_unregister_platform();
  2469. stmmac_unregister_pci();
  2470. }
  2471. module_init(stmmac_init);
  2472. module_exit(stmmac_exit);
  2473. #ifndef MODULE
  2474. static int __init stmmac_cmdline_opt(char *str)
  2475. {
  2476. char *opt;
  2477. if (!str || !*str)
  2478. return -EINVAL;
  2479. while ((opt = strsep(&str, ",")) != NULL) {
  2480. if (!strncmp(opt, "debug:", 6)) {
  2481. if (kstrtoint(opt + 6, 0, &debug))
  2482. goto err;
  2483. } else if (!strncmp(opt, "phyaddr:", 8)) {
  2484. if (kstrtoint(opt + 8, 0, &phyaddr))
  2485. goto err;
  2486. } else if (!strncmp(opt, "dma_txsize:", 11)) {
  2487. if (kstrtoint(opt + 11, 0, &dma_txsize))
  2488. goto err;
  2489. } else if (!strncmp(opt, "dma_rxsize:", 11)) {
  2490. if (kstrtoint(opt + 11, 0, &dma_rxsize))
  2491. goto err;
  2492. } else if (!strncmp(opt, "buf_sz:", 7)) {
  2493. if (kstrtoint(opt + 7, 0, &buf_sz))
  2494. goto err;
  2495. } else if (!strncmp(opt, "tc:", 3)) {
  2496. if (kstrtoint(opt + 3, 0, &tc))
  2497. goto err;
  2498. } else if (!strncmp(opt, "watchdog:", 9)) {
  2499. if (kstrtoint(opt + 9, 0, &watchdog))
  2500. goto err;
  2501. } else if (!strncmp(opt, "flow_ctrl:", 10)) {
  2502. if (kstrtoint(opt + 10, 0, &flow_ctrl))
  2503. goto err;
  2504. } else if (!strncmp(opt, "pause:", 6)) {
  2505. if (kstrtoint(opt + 6, 0, &pause))
  2506. goto err;
  2507. } else if (!strncmp(opt, "eee_timer:", 10)) {
  2508. if (kstrtoint(opt + 10, 0, &eee_timer))
  2509. goto err;
  2510. } else if (!strncmp(opt, "chain_mode:", 11)) {
  2511. if (kstrtoint(opt + 11, 0, &chain_mode))
  2512. goto err;
  2513. }
  2514. }
  2515. return 0;
  2516. err:
  2517. pr_err("%s: ERROR broken module parameter conversion", __func__);
  2518. return -EINVAL;
  2519. }
  2520. __setup("stmmaceth=", stmmac_cmdline_opt);
  2521. #endif /* MODULE */
  2522. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
  2523. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  2524. MODULE_LICENSE("GPL");