stmmac_main.c 101 KB

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