stmmac_main.c 79 KB

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