stmmac_main.c 82 KB

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