stmmac_main.c 82 KB

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