sxgbe_main.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  1. /* 10G controller driver for Samsung SoCs
  2. *
  3. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Author: Siva Reddy Kallam <siva.kallam@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/clk.h>
  14. #include <linux/crc32.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/if.h>
  19. #include <linux/if_ether.h>
  20. #include <linux/if_vlan.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ip.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mii.h>
  26. #include <linux/module.h>
  27. #include <linux/net_tstamp.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/phy.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/prefetch.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/slab.h>
  34. #include <linux/tcp.h>
  35. #include <linux/sxgbe_platform.h>
  36. #include "sxgbe_common.h"
  37. #include "sxgbe_desc.h"
  38. #include "sxgbe_dma.h"
  39. #include "sxgbe_mtl.h"
  40. #include "sxgbe_reg.h"
  41. #define SXGBE_ALIGN(x) L1_CACHE_ALIGN(x)
  42. #define JUMBO_LEN 9000
  43. /* Module parameters */
  44. #define TX_TIMEO 5000
  45. #define DMA_TX_SIZE 512
  46. #define DMA_RX_SIZE 1024
  47. #define TC_DEFAULT 64
  48. #define DMA_BUFFER_SIZE BUF_SIZE_2KiB
  49. /* The default timer value as per the sxgbe specification 1 sec(1000 ms) */
  50. #define SXGBE_DEFAULT_LPI_TIMER 1000
  51. static int debug = -1;
  52. static int eee_timer = SXGBE_DEFAULT_LPI_TIMER;
  53. module_param(eee_timer, int, S_IRUGO | S_IWUSR);
  54. module_param(debug, int, S_IRUGO | S_IWUSR);
  55. static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  56. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  57. NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
  58. static irqreturn_t sxgbe_common_interrupt(int irq, void *dev_id);
  59. static irqreturn_t sxgbe_tx_interrupt(int irq, void *dev_id);
  60. static irqreturn_t sxgbe_rx_interrupt(int irq, void *dev_id);
  61. #define SXGBE_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
  62. #define SXGBE_LPI_TIMER(x) (jiffies + msecs_to_jiffies(x))
  63. /**
  64. * sxgbe_verify_args - verify the driver parameters.
  65. * Description: it verifies if some wrong parameter is passed to the driver.
  66. * Note that wrong parameters are replaced with the default values.
  67. */
  68. static void sxgbe_verify_args(void)
  69. {
  70. if (unlikely(eee_timer < 0))
  71. eee_timer = SXGBE_DEFAULT_LPI_TIMER;
  72. }
  73. static void sxgbe_enable_eee_mode(const struct sxgbe_priv_data *priv)
  74. {
  75. /* Check and enter in LPI mode */
  76. if (!priv->tx_path_in_lpi_mode)
  77. priv->hw->mac->set_eee_mode(priv->ioaddr);
  78. }
  79. void sxgbe_disable_eee_mode(struct sxgbe_priv_data * const priv)
  80. {
  81. /* Exit and disable EEE in case of we are are in LPI state. */
  82. priv->hw->mac->reset_eee_mode(priv->ioaddr);
  83. del_timer_sync(&priv->eee_ctrl_timer);
  84. priv->tx_path_in_lpi_mode = false;
  85. }
  86. /**
  87. * sxgbe_eee_ctrl_timer
  88. * @arg : data hook
  89. * Description:
  90. * If there is no data transfer and if we are not in LPI state,
  91. * then MAC Transmitter can be moved to LPI state.
  92. */
  93. static void sxgbe_eee_ctrl_timer(unsigned long arg)
  94. {
  95. struct sxgbe_priv_data *priv = (struct sxgbe_priv_data *)arg;
  96. sxgbe_enable_eee_mode(priv);
  97. mod_timer(&priv->eee_ctrl_timer, SXGBE_LPI_TIMER(eee_timer));
  98. }
  99. /**
  100. * sxgbe_eee_init
  101. * @priv: private device pointer
  102. * Description:
  103. * If the EEE support has been enabled while configuring the driver,
  104. * if the GMAC actually supports the EEE (from the HW cap reg) and the
  105. * phy can also manage EEE, so enable the LPI state and start the timer
  106. * to verify if the tx path can enter in LPI state.
  107. */
  108. bool sxgbe_eee_init(struct sxgbe_priv_data * const priv)
  109. {
  110. bool ret = false;
  111. /* MAC core supports the EEE feature. */
  112. if (priv->hw_cap.eee) {
  113. /* Check if the PHY supports EEE */
  114. if (phy_init_eee(priv->phydev, 1))
  115. return false;
  116. priv->eee_active = 1;
  117. init_timer(&priv->eee_ctrl_timer);
  118. priv->eee_ctrl_timer.function = sxgbe_eee_ctrl_timer;
  119. priv->eee_ctrl_timer.data = (unsigned long)priv;
  120. priv->eee_ctrl_timer.expires = SXGBE_LPI_TIMER(eee_timer);
  121. add_timer(&priv->eee_ctrl_timer);
  122. priv->hw->mac->set_eee_timer(priv->ioaddr,
  123. SXGBE_DEFAULT_LPI_TIMER,
  124. priv->tx_lpi_timer);
  125. pr_info("Energy-Efficient Ethernet initialized\n");
  126. ret = true;
  127. }
  128. return ret;
  129. }
  130. static void sxgbe_eee_adjust(const struct sxgbe_priv_data *priv)
  131. {
  132. /* When the EEE has been already initialised we have to
  133. * modify the PLS bit in the LPI ctrl & status reg according
  134. * to the PHY link status. For this reason.
  135. */
  136. if (priv->eee_enabled)
  137. priv->hw->mac->set_eee_pls(priv->ioaddr, priv->phydev->link);
  138. }
  139. /**
  140. * sxgbe_clk_csr_set - dynamically set the MDC clock
  141. * @priv: driver private structure
  142. * Description: this is to dynamically set the MDC clock according to the csr
  143. * clock input.
  144. */
  145. static void sxgbe_clk_csr_set(struct sxgbe_priv_data *priv)
  146. {
  147. u32 clk_rate = clk_get_rate(priv->sxgbe_clk);
  148. /* assign the proper divider, this will be used during
  149. * mdio communication
  150. */
  151. if (clk_rate < SXGBE_CSR_F_150M)
  152. priv->clk_csr = SXGBE_CSR_100_150M;
  153. else if (clk_rate <= SXGBE_CSR_F_250M)
  154. priv->clk_csr = SXGBE_CSR_150_250M;
  155. else if (clk_rate <= SXGBE_CSR_F_300M)
  156. priv->clk_csr = SXGBE_CSR_250_300M;
  157. else if (clk_rate <= SXGBE_CSR_F_350M)
  158. priv->clk_csr = SXGBE_CSR_300_350M;
  159. else if (clk_rate <= SXGBE_CSR_F_400M)
  160. priv->clk_csr = SXGBE_CSR_350_400M;
  161. else if (clk_rate <= SXGBE_CSR_F_500M)
  162. priv->clk_csr = SXGBE_CSR_400_500M;
  163. }
  164. /* minimum number of free TX descriptors required to wake up TX process */
  165. #define SXGBE_TX_THRESH(x) (x->dma_tx_size/4)
  166. static inline u32 sxgbe_tx_avail(struct sxgbe_tx_queue *queue, int tx_qsize)
  167. {
  168. return queue->dirty_tx + tx_qsize - queue->cur_tx - 1;
  169. }
  170. /**
  171. * sxgbe_adjust_link
  172. * @dev: net device structure
  173. * Description: it adjusts the link parameters.
  174. */
  175. static void sxgbe_adjust_link(struct net_device *dev)
  176. {
  177. struct sxgbe_priv_data *priv = netdev_priv(dev);
  178. struct phy_device *phydev = priv->phydev;
  179. u8 new_state = 0;
  180. u8 speed = 0xff;
  181. if (!phydev)
  182. return;
  183. /* SXGBE is not supporting auto-negotiation and
  184. * half duplex mode. so, not handling duplex change
  185. * in this function. only handling speed and link status
  186. */
  187. if (phydev->link) {
  188. if (phydev->speed != priv->speed) {
  189. new_state = 1;
  190. switch (phydev->speed) {
  191. case SPEED_10000:
  192. speed = SXGBE_SPEED_10G;
  193. break;
  194. case SPEED_2500:
  195. speed = SXGBE_SPEED_2_5G;
  196. break;
  197. case SPEED_1000:
  198. speed = SXGBE_SPEED_1G;
  199. break;
  200. default:
  201. netif_err(priv, link, dev,
  202. "Speed (%d) not supported\n",
  203. phydev->speed);
  204. }
  205. priv->speed = phydev->speed;
  206. priv->hw->mac->set_speed(priv->ioaddr, speed);
  207. }
  208. if (!priv->oldlink) {
  209. new_state = 1;
  210. priv->oldlink = 1;
  211. }
  212. } else if (priv->oldlink) {
  213. new_state = 1;
  214. priv->oldlink = 0;
  215. priv->speed = SPEED_UNKNOWN;
  216. }
  217. if (new_state & netif_msg_link(priv))
  218. phy_print_status(phydev);
  219. /* Alter the MAC settings for EEE */
  220. sxgbe_eee_adjust(priv);
  221. }
  222. /**
  223. * sxgbe_init_phy - PHY initialization
  224. * @dev: net device structure
  225. * Description: it initializes the driver's PHY state, and attaches the PHY
  226. * to the mac driver.
  227. * Return value:
  228. * 0 on success
  229. */
  230. static int sxgbe_init_phy(struct net_device *ndev)
  231. {
  232. char phy_id_fmt[MII_BUS_ID_SIZE + 3];
  233. char bus_id[MII_BUS_ID_SIZE];
  234. struct phy_device *phydev;
  235. struct sxgbe_priv_data *priv = netdev_priv(ndev);
  236. int phy_iface = priv->plat->interface;
  237. /* assign default link status */
  238. priv->oldlink = 0;
  239. priv->speed = SPEED_UNKNOWN;
  240. priv->oldduplex = DUPLEX_UNKNOWN;
  241. if (priv->plat->phy_bus_name)
  242. snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
  243. priv->plat->phy_bus_name, priv->plat->bus_id);
  244. else
  245. snprintf(bus_id, MII_BUS_ID_SIZE, "sxgbe-%x",
  246. priv->plat->bus_id);
  247. snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
  248. priv->plat->phy_addr);
  249. netdev_dbg(ndev, "%s: trying to attach to %s\n", __func__, phy_id_fmt);
  250. phydev = phy_connect(ndev, phy_id_fmt, &sxgbe_adjust_link, phy_iface);
  251. if (IS_ERR(phydev)) {
  252. netdev_err(ndev, "Could not attach to PHY\n");
  253. return PTR_ERR(phydev);
  254. }
  255. /* Stop Advertising 1000BASE Capability if interface is not GMII */
  256. if ((phy_iface == PHY_INTERFACE_MODE_MII) ||
  257. (phy_iface == PHY_INTERFACE_MODE_RMII))
  258. phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
  259. SUPPORTED_1000baseT_Full);
  260. if (phydev->phy_id == 0) {
  261. phy_disconnect(phydev);
  262. return -ENODEV;
  263. }
  264. netdev_dbg(ndev, "%s: attached to PHY (UID 0x%x) Link = %d\n",
  265. __func__, phydev->phy_id, phydev->link);
  266. /* save phy device in private structure */
  267. priv->phydev = phydev;
  268. return 0;
  269. }
  270. /**
  271. * sxgbe_clear_descriptors: clear descriptors
  272. * @priv: driver private structure
  273. * Description: this function is called to clear the tx and rx descriptors
  274. * in case of both basic and extended descriptors are used.
  275. */
  276. static void sxgbe_clear_descriptors(struct sxgbe_priv_data *priv)
  277. {
  278. int i, j;
  279. unsigned int txsize = priv->dma_tx_size;
  280. unsigned int rxsize = priv->dma_rx_size;
  281. /* Clear the Rx/Tx descriptors */
  282. for (j = 0; j < SXGBE_RX_QUEUES; j++) {
  283. for (i = 0; i < rxsize; i++)
  284. priv->hw->desc->init_rx_desc(&priv->rxq[j]->dma_rx[i],
  285. priv->use_riwt, priv->mode,
  286. (i == rxsize - 1));
  287. }
  288. for (j = 0; j < SXGBE_TX_QUEUES; j++) {
  289. for (i = 0; i < txsize; i++)
  290. priv->hw->desc->init_tx_desc(&priv->txq[j]->dma_tx[i]);
  291. }
  292. }
  293. static int sxgbe_init_rx_buffers(struct net_device *dev,
  294. struct sxgbe_rx_norm_desc *p, int i,
  295. unsigned int dma_buf_sz,
  296. struct sxgbe_rx_queue *rx_ring)
  297. {
  298. struct sxgbe_priv_data *priv = netdev_priv(dev);
  299. struct sk_buff *skb;
  300. skb = __netdev_alloc_skb_ip_align(dev, dma_buf_sz, GFP_KERNEL);
  301. if (!skb)
  302. return -ENOMEM;
  303. rx_ring->rx_skbuff[i] = skb;
  304. rx_ring->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
  305. dma_buf_sz, DMA_FROM_DEVICE);
  306. if (dma_mapping_error(priv->device, rx_ring->rx_skbuff_dma[i])) {
  307. netdev_err(dev, "%s: DMA mapping error\n", __func__);
  308. dev_kfree_skb_any(skb);
  309. return -EINVAL;
  310. }
  311. p->rdes23.rx_rd_des23.buf2_addr = rx_ring->rx_skbuff_dma[i];
  312. return 0;
  313. }
  314. /**
  315. * init_tx_ring - init the TX descriptor ring
  316. * @dev: net device structure
  317. * @tx_ring: ring to be intialised
  318. * @tx_rsize: ring size
  319. * Description: this function initializes the DMA TX descriptor
  320. */
  321. static int init_tx_ring(struct device *dev, u8 queue_no,
  322. struct sxgbe_tx_queue *tx_ring, int tx_rsize)
  323. {
  324. /* TX ring is not allcoated */
  325. if (!tx_ring) {
  326. dev_err(dev, "No memory for TX queue of SXGBE\n");
  327. return -ENOMEM;
  328. }
  329. /* allocate memory for TX descriptors */
  330. tx_ring->dma_tx = dma_zalloc_coherent(dev,
  331. tx_rsize * sizeof(struct sxgbe_tx_norm_desc),
  332. &tx_ring->dma_tx_phy, GFP_KERNEL);
  333. if (!tx_ring->dma_tx)
  334. return -ENOMEM;
  335. /* allocate memory for TX skbuff array */
  336. tx_ring->tx_skbuff_dma = devm_kcalloc(dev, tx_rsize,
  337. sizeof(dma_addr_t), GFP_KERNEL);
  338. if (!tx_ring->tx_skbuff_dma)
  339. goto dmamem_err;
  340. tx_ring->tx_skbuff = devm_kcalloc(dev, tx_rsize,
  341. sizeof(struct sk_buff *), GFP_KERNEL);
  342. if (!tx_ring->tx_skbuff)
  343. goto dmamem_err;
  344. /* assign queue number */
  345. tx_ring->queue_no = queue_no;
  346. /* initalise counters */
  347. tx_ring->dirty_tx = 0;
  348. tx_ring->cur_tx = 0;
  349. /* initalise TX queue lock */
  350. spin_lock_init(&tx_ring->tx_lock);
  351. return 0;
  352. dmamem_err:
  353. dma_free_coherent(dev, tx_rsize * sizeof(struct sxgbe_tx_norm_desc),
  354. tx_ring->dma_tx, tx_ring->dma_tx_phy);
  355. return -ENOMEM;
  356. }
  357. /**
  358. * free_rx_ring - free the RX descriptor ring
  359. * @dev: net device structure
  360. * @rx_ring: ring to be intialised
  361. * @rx_rsize: ring size
  362. * Description: this function initializes the DMA RX descriptor
  363. */
  364. static void free_rx_ring(struct device *dev, struct sxgbe_rx_queue *rx_ring,
  365. int rx_rsize)
  366. {
  367. dma_free_coherent(dev, rx_rsize * sizeof(struct sxgbe_rx_norm_desc),
  368. rx_ring->dma_rx, rx_ring->dma_rx_phy);
  369. kfree(rx_ring->rx_skbuff_dma);
  370. kfree(rx_ring->rx_skbuff);
  371. }
  372. /**
  373. * init_rx_ring - init the RX descriptor ring
  374. * @dev: net device structure
  375. * @rx_ring: ring to be intialised
  376. * @rx_rsize: ring size
  377. * Description: this function initializes the DMA RX descriptor
  378. */
  379. static int init_rx_ring(struct net_device *dev, u8 queue_no,
  380. struct sxgbe_rx_queue *rx_ring, int rx_rsize)
  381. {
  382. struct sxgbe_priv_data *priv = netdev_priv(dev);
  383. int desc_index;
  384. unsigned int bfsize = 0;
  385. unsigned int ret = 0;
  386. /* Set the max buffer size according to the MTU. */
  387. bfsize = ALIGN(dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN, 8);
  388. netif_dbg(priv, probe, dev, "%s: bfsize %d\n", __func__, bfsize);
  389. /* RX ring is not allcoated */
  390. if (rx_ring == NULL) {
  391. netdev_err(dev, "No memory for RX queue\n");
  392. goto error;
  393. }
  394. /* assign queue number */
  395. rx_ring->queue_no = queue_no;
  396. /* allocate memory for RX descriptors */
  397. rx_ring->dma_rx = dma_zalloc_coherent(priv->device,
  398. rx_rsize * sizeof(struct sxgbe_rx_norm_desc),
  399. &rx_ring->dma_rx_phy, GFP_KERNEL);
  400. if (rx_ring->dma_rx == NULL)
  401. goto error;
  402. /* allocate memory for RX skbuff array */
  403. rx_ring->rx_skbuff_dma = kmalloc_array(rx_rsize,
  404. sizeof(dma_addr_t), GFP_KERNEL);
  405. if (rx_ring->rx_skbuff_dma == NULL)
  406. goto dmamem_err;
  407. rx_ring->rx_skbuff = kmalloc_array(rx_rsize,
  408. sizeof(struct sk_buff *), GFP_KERNEL);
  409. if (rx_ring->rx_skbuff == NULL)
  410. goto rxbuff_err;
  411. /* initialise the buffers */
  412. for (desc_index = 0; desc_index < rx_rsize; desc_index++) {
  413. struct sxgbe_rx_norm_desc *p;
  414. p = rx_ring->dma_rx + desc_index;
  415. ret = sxgbe_init_rx_buffers(dev, p, desc_index,
  416. bfsize, rx_ring);
  417. if (ret)
  418. goto err_init_rx_buffers;
  419. }
  420. /* initalise counters */
  421. rx_ring->cur_rx = 0;
  422. rx_ring->dirty_rx = (unsigned int)(desc_index - rx_rsize);
  423. priv->dma_buf_sz = bfsize;
  424. return 0;
  425. err_init_rx_buffers:
  426. while (--desc_index >= 0)
  427. free_rx_ring(priv->device, rx_ring, desc_index);
  428. kfree(rx_ring->rx_skbuff);
  429. rxbuff_err:
  430. kfree(rx_ring->rx_skbuff_dma);
  431. dmamem_err:
  432. dma_free_coherent(priv->device,
  433. rx_rsize * sizeof(struct sxgbe_rx_norm_desc),
  434. rx_ring->dma_rx, rx_ring->dma_rx_phy);
  435. error:
  436. return -ENOMEM;
  437. }
  438. /**
  439. * free_tx_ring - free the TX descriptor ring
  440. * @dev: net device structure
  441. * @tx_ring: ring to be intialised
  442. * @tx_rsize: ring size
  443. * Description: this function initializes the DMA TX descriptor
  444. */
  445. static void free_tx_ring(struct device *dev, struct sxgbe_tx_queue *tx_ring,
  446. int tx_rsize)
  447. {
  448. dma_free_coherent(dev, tx_rsize * sizeof(struct sxgbe_tx_norm_desc),
  449. tx_ring->dma_tx, tx_ring->dma_tx_phy);
  450. }
  451. /**
  452. * init_dma_desc_rings - init the RX/TX descriptor rings
  453. * @dev: net device structure
  454. * Description: this function initializes the DMA RX/TX descriptors
  455. * and allocates the socket buffers. It suppors the chained and ring
  456. * modes.
  457. */
  458. static int init_dma_desc_rings(struct net_device *netd)
  459. {
  460. int queue_num, ret;
  461. struct sxgbe_priv_data *priv = netdev_priv(netd);
  462. int tx_rsize = priv->dma_tx_size;
  463. int rx_rsize = priv->dma_rx_size;
  464. /* Allocate memory for queue structures and TX descs */
  465. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num) {
  466. ret = init_tx_ring(priv->device, queue_num,
  467. priv->txq[queue_num], tx_rsize);
  468. if (ret) {
  469. dev_err(&netd->dev, "TX DMA ring allocation failed!\n");
  470. goto txalloc_err;
  471. }
  472. /* save private pointer in each ring this
  473. * pointer is needed during cleaing TX queue
  474. */
  475. priv->txq[queue_num]->priv_ptr = priv;
  476. }
  477. /* Allocate memory for queue structures and RX descs */
  478. SXGBE_FOR_EACH_QUEUE(SXGBE_RX_QUEUES, queue_num) {
  479. ret = init_rx_ring(netd, queue_num,
  480. priv->rxq[queue_num], rx_rsize);
  481. if (ret) {
  482. netdev_err(netd, "RX DMA ring allocation failed!!\n");
  483. goto rxalloc_err;
  484. }
  485. /* save private pointer in each ring this
  486. * pointer is needed during cleaing TX queue
  487. */
  488. priv->rxq[queue_num]->priv_ptr = priv;
  489. }
  490. sxgbe_clear_descriptors(priv);
  491. return 0;
  492. txalloc_err:
  493. while (queue_num--)
  494. free_tx_ring(priv->device, priv->txq[queue_num], tx_rsize);
  495. return ret;
  496. rxalloc_err:
  497. while (queue_num--)
  498. free_rx_ring(priv->device, priv->rxq[queue_num], rx_rsize);
  499. return ret;
  500. }
  501. static void tx_free_ring_skbufs(struct sxgbe_tx_queue *txqueue)
  502. {
  503. int dma_desc;
  504. struct sxgbe_priv_data *priv = txqueue->priv_ptr;
  505. int tx_rsize = priv->dma_tx_size;
  506. for (dma_desc = 0; dma_desc < tx_rsize; dma_desc++) {
  507. struct sxgbe_tx_norm_desc *tdesc = txqueue->dma_tx + dma_desc;
  508. if (txqueue->tx_skbuff_dma[dma_desc])
  509. dma_unmap_single(priv->device,
  510. txqueue->tx_skbuff_dma[dma_desc],
  511. priv->hw->desc->get_tx_len(tdesc),
  512. DMA_TO_DEVICE);
  513. dev_kfree_skb_any(txqueue->tx_skbuff[dma_desc]);
  514. txqueue->tx_skbuff[dma_desc] = NULL;
  515. txqueue->tx_skbuff_dma[dma_desc] = 0;
  516. }
  517. }
  518. static void dma_free_tx_skbufs(struct sxgbe_priv_data *priv)
  519. {
  520. int queue_num;
  521. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num) {
  522. struct sxgbe_tx_queue *tqueue = priv->txq[queue_num];
  523. tx_free_ring_skbufs(tqueue);
  524. }
  525. }
  526. static void free_dma_desc_resources(struct sxgbe_priv_data *priv)
  527. {
  528. int queue_num;
  529. int tx_rsize = priv->dma_tx_size;
  530. int rx_rsize = priv->dma_rx_size;
  531. /* Release the DMA TX buffers */
  532. dma_free_tx_skbufs(priv);
  533. /* Release the TX ring memory also */
  534. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num) {
  535. free_tx_ring(priv->device, priv->txq[queue_num], tx_rsize);
  536. }
  537. /* Release the RX ring memory also */
  538. SXGBE_FOR_EACH_QUEUE(SXGBE_RX_QUEUES, queue_num) {
  539. free_rx_ring(priv->device, priv->rxq[queue_num], rx_rsize);
  540. }
  541. }
  542. static int txring_mem_alloc(struct sxgbe_priv_data *priv)
  543. {
  544. int queue_num;
  545. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num) {
  546. priv->txq[queue_num] = devm_kmalloc(priv->device,
  547. sizeof(struct sxgbe_tx_queue), GFP_KERNEL);
  548. if (!priv->txq[queue_num])
  549. return -ENOMEM;
  550. }
  551. return 0;
  552. }
  553. static int rxring_mem_alloc(struct sxgbe_priv_data *priv)
  554. {
  555. int queue_num;
  556. SXGBE_FOR_EACH_QUEUE(SXGBE_RX_QUEUES, queue_num) {
  557. priv->rxq[queue_num] = devm_kmalloc(priv->device,
  558. sizeof(struct sxgbe_rx_queue), GFP_KERNEL);
  559. if (!priv->rxq[queue_num])
  560. return -ENOMEM;
  561. }
  562. return 0;
  563. }
  564. /**
  565. * sxgbe_mtl_operation_mode - HW MTL operation mode
  566. * @priv: driver private structure
  567. * Description: it sets the MTL operation mode: tx/rx MTL thresholds
  568. * or Store-And-Forward capability.
  569. */
  570. static void sxgbe_mtl_operation_mode(struct sxgbe_priv_data *priv)
  571. {
  572. int queue_num;
  573. /* TX/RX threshold control */
  574. if (likely(priv->plat->force_sf_dma_mode)) {
  575. /* set TC mode for TX QUEUES */
  576. SXGBE_FOR_EACH_QUEUE(priv->hw_cap.tx_mtl_queues, queue_num)
  577. priv->hw->mtl->set_tx_mtl_mode(priv->ioaddr, queue_num,
  578. SXGBE_MTL_SFMODE);
  579. priv->tx_tc = SXGBE_MTL_SFMODE;
  580. /* set TC mode for RX QUEUES */
  581. SXGBE_FOR_EACH_QUEUE(priv->hw_cap.rx_mtl_queues, queue_num)
  582. priv->hw->mtl->set_rx_mtl_mode(priv->ioaddr, queue_num,
  583. SXGBE_MTL_SFMODE);
  584. priv->rx_tc = SXGBE_MTL_SFMODE;
  585. } else if (unlikely(priv->plat->force_thresh_dma_mode)) {
  586. /* set TC mode for TX QUEUES */
  587. SXGBE_FOR_EACH_QUEUE(priv->hw_cap.tx_mtl_queues, queue_num)
  588. priv->hw->mtl->set_tx_mtl_mode(priv->ioaddr, queue_num,
  589. priv->tx_tc);
  590. /* set TC mode for RX QUEUES */
  591. SXGBE_FOR_EACH_QUEUE(priv->hw_cap.rx_mtl_queues, queue_num)
  592. priv->hw->mtl->set_rx_mtl_mode(priv->ioaddr, queue_num,
  593. priv->rx_tc);
  594. } else {
  595. pr_err("ERROR: %s: Invalid TX threshold mode\n", __func__);
  596. }
  597. }
  598. /**
  599. * sxgbe_tx_queue_clean:
  600. * @priv: driver private structure
  601. * Description: it reclaims resources after transmission completes.
  602. */
  603. static void sxgbe_tx_queue_clean(struct sxgbe_tx_queue *tqueue)
  604. {
  605. struct sxgbe_priv_data *priv = tqueue->priv_ptr;
  606. unsigned int tx_rsize = priv->dma_tx_size;
  607. struct netdev_queue *dev_txq;
  608. u8 queue_no = tqueue->queue_no;
  609. dev_txq = netdev_get_tx_queue(priv->dev, queue_no);
  610. spin_lock(&tqueue->tx_lock);
  611. priv->xstats.tx_clean++;
  612. while (tqueue->dirty_tx != tqueue->cur_tx) {
  613. unsigned int entry = tqueue->dirty_tx % tx_rsize;
  614. struct sk_buff *skb = tqueue->tx_skbuff[entry];
  615. struct sxgbe_tx_norm_desc *p;
  616. p = tqueue->dma_tx + entry;
  617. /* Check if the descriptor is owned by the DMA. */
  618. if (priv->hw->desc->get_tx_owner(p))
  619. break;
  620. if (netif_msg_tx_done(priv))
  621. pr_debug("%s: curr %d, dirty %d\n",
  622. __func__, tqueue->cur_tx, tqueue->dirty_tx);
  623. if (likely(tqueue->tx_skbuff_dma[entry])) {
  624. dma_unmap_single(priv->device,
  625. tqueue->tx_skbuff_dma[entry],
  626. priv->hw->desc->get_tx_len(p),
  627. DMA_TO_DEVICE);
  628. tqueue->tx_skbuff_dma[entry] = 0;
  629. }
  630. if (likely(skb)) {
  631. dev_kfree_skb(skb);
  632. tqueue->tx_skbuff[entry] = NULL;
  633. }
  634. priv->hw->desc->release_tx_desc(p);
  635. tqueue->dirty_tx++;
  636. }
  637. /* wake up queue */
  638. if (unlikely(netif_tx_queue_stopped(dev_txq) &&
  639. sxgbe_tx_avail(tqueue, tx_rsize) > SXGBE_TX_THRESH(priv))) {
  640. netif_tx_lock(priv->dev);
  641. if (netif_tx_queue_stopped(dev_txq) &&
  642. sxgbe_tx_avail(tqueue, tx_rsize) > SXGBE_TX_THRESH(priv)) {
  643. if (netif_msg_tx_done(priv))
  644. pr_debug("%s: restart transmit\n", __func__);
  645. netif_tx_wake_queue(dev_txq);
  646. }
  647. netif_tx_unlock(priv->dev);
  648. }
  649. spin_unlock(&tqueue->tx_lock);
  650. }
  651. /**
  652. * sxgbe_tx_clean:
  653. * @priv: driver private structure
  654. * Description: it reclaims resources after transmission completes.
  655. */
  656. static void sxgbe_tx_all_clean(struct sxgbe_priv_data * const priv)
  657. {
  658. u8 queue_num;
  659. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num) {
  660. struct sxgbe_tx_queue *tqueue = priv->txq[queue_num];
  661. sxgbe_tx_queue_clean(tqueue);
  662. }
  663. if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
  664. sxgbe_enable_eee_mode(priv);
  665. mod_timer(&priv->eee_ctrl_timer, SXGBE_LPI_TIMER(eee_timer));
  666. }
  667. }
  668. /**
  669. * sxgbe_restart_tx_queue: irq tx error mng function
  670. * @priv: driver private structure
  671. * Description: it cleans the descriptors and restarts the transmission
  672. * in case of errors.
  673. */
  674. static void sxgbe_restart_tx_queue(struct sxgbe_priv_data *priv, int queue_num)
  675. {
  676. struct sxgbe_tx_queue *tx_ring = priv->txq[queue_num];
  677. struct netdev_queue *dev_txq = netdev_get_tx_queue(priv->dev,
  678. queue_num);
  679. /* stop the queue */
  680. netif_tx_stop_queue(dev_txq);
  681. /* stop the tx dma */
  682. priv->hw->dma->stop_tx_queue(priv->ioaddr, queue_num);
  683. /* free the skbuffs of the ring */
  684. tx_free_ring_skbufs(tx_ring);
  685. /* initalise counters */
  686. tx_ring->cur_tx = 0;
  687. tx_ring->dirty_tx = 0;
  688. /* start the tx dma */
  689. priv->hw->dma->start_tx_queue(priv->ioaddr, queue_num);
  690. priv->dev->stats.tx_errors++;
  691. /* wakeup the queue */
  692. netif_tx_wake_queue(dev_txq);
  693. }
  694. /**
  695. * sxgbe_reset_all_tx_queues: irq tx error mng function
  696. * @priv: driver private structure
  697. * Description: it cleans all the descriptors and
  698. * restarts the transmission on all queues in case of errors.
  699. */
  700. static void sxgbe_reset_all_tx_queues(struct sxgbe_priv_data *priv)
  701. {
  702. int queue_num;
  703. /* On TX timeout of net device, resetting of all queues
  704. * may not be proper way, revisit this later if needed
  705. */
  706. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num)
  707. sxgbe_restart_tx_queue(priv, queue_num);
  708. }
  709. /**
  710. * sxgbe_get_hw_features: get XMAC capabilities from the HW cap. register.
  711. * @priv: driver private structure
  712. * Description:
  713. * new GMAC chip generations have a new register to indicate the
  714. * presence of the optional feature/functions.
  715. * This can be also used to override the value passed through the
  716. * platform and necessary for old MAC10/100 and GMAC chips.
  717. */
  718. static int sxgbe_get_hw_features(struct sxgbe_priv_data * const priv)
  719. {
  720. int rval = 0;
  721. struct sxgbe_hw_features *features = &priv->hw_cap;
  722. /* Read First Capability Register CAP[0] */
  723. rval = priv->hw->mac->get_hw_feature(priv->ioaddr, 0);
  724. if (rval) {
  725. features->pmt_remote_wake_up =
  726. SXGBE_HW_FEAT_PMT_TEMOTE_WOP(rval);
  727. features->pmt_magic_frame = SXGBE_HW_FEAT_PMT_MAGIC_PKT(rval);
  728. features->atime_stamp = SXGBE_HW_FEAT_IEEE1500_2008(rval);
  729. features->tx_csum_offload =
  730. SXGBE_HW_FEAT_TX_CSUM_OFFLOAD(rval);
  731. features->rx_csum_offload =
  732. SXGBE_HW_FEAT_RX_CSUM_OFFLOAD(rval);
  733. features->multi_macaddr = SXGBE_HW_FEAT_MACADDR_COUNT(rval);
  734. features->tstamp_srcselect = SXGBE_HW_FEAT_TSTMAP_SRC(rval);
  735. features->sa_vlan_insert = SXGBE_HW_FEAT_SRCADDR_VLAN(rval);
  736. features->eee = SXGBE_HW_FEAT_EEE(rval);
  737. }
  738. /* Read First Capability Register CAP[1] */
  739. rval = priv->hw->mac->get_hw_feature(priv->ioaddr, 1);
  740. if (rval) {
  741. features->rxfifo_size = SXGBE_HW_FEAT_RX_FIFO_SIZE(rval);
  742. features->txfifo_size = SXGBE_HW_FEAT_TX_FIFO_SIZE(rval);
  743. features->atstmap_hword = SXGBE_HW_FEAT_TX_FIFO_SIZE(rval);
  744. features->dcb_enable = SXGBE_HW_FEAT_DCB(rval);
  745. features->splithead_enable = SXGBE_HW_FEAT_SPLIT_HDR(rval);
  746. features->tcpseg_offload = SXGBE_HW_FEAT_TSO(rval);
  747. features->debug_mem = SXGBE_HW_FEAT_DEBUG_MEM_IFACE(rval);
  748. features->rss_enable = SXGBE_HW_FEAT_RSS(rval);
  749. features->hash_tsize = SXGBE_HW_FEAT_HASH_TABLE_SIZE(rval);
  750. features->l3l4_filer_size = SXGBE_HW_FEAT_L3L4_FILTER_NUM(rval);
  751. }
  752. /* Read First Capability Register CAP[2] */
  753. rval = priv->hw->mac->get_hw_feature(priv->ioaddr, 2);
  754. if (rval) {
  755. features->rx_mtl_queues = SXGBE_HW_FEAT_RX_MTL_QUEUES(rval);
  756. features->tx_mtl_queues = SXGBE_HW_FEAT_TX_MTL_QUEUES(rval);
  757. features->rx_dma_channels = SXGBE_HW_FEAT_RX_DMA_CHANNELS(rval);
  758. features->tx_dma_channels = SXGBE_HW_FEAT_TX_DMA_CHANNELS(rval);
  759. features->pps_output_count = SXGBE_HW_FEAT_PPS_OUTPUTS(rval);
  760. features->aux_input_count = SXGBE_HW_FEAT_AUX_SNAPSHOTS(rval);
  761. }
  762. return rval;
  763. }
  764. /**
  765. * sxgbe_check_ether_addr: check if the MAC addr is valid
  766. * @priv: driver private structure
  767. * Description:
  768. * it is to verify if the MAC address is valid, in case of failures it
  769. * generates a random MAC address
  770. */
  771. static void sxgbe_check_ether_addr(struct sxgbe_priv_data *priv)
  772. {
  773. if (!is_valid_ether_addr(priv->dev->dev_addr)) {
  774. priv->hw->mac->get_umac_addr((void __iomem *)
  775. priv->ioaddr,
  776. priv->dev->dev_addr, 0);
  777. if (!is_valid_ether_addr(priv->dev->dev_addr))
  778. eth_hw_addr_random(priv->dev);
  779. }
  780. dev_info(priv->device, "device MAC address %pM\n",
  781. priv->dev->dev_addr);
  782. }
  783. /**
  784. * sxgbe_init_dma_engine: DMA init.
  785. * @priv: driver private structure
  786. * Description:
  787. * It inits the DMA invoking the specific SXGBE callback.
  788. * Some DMA parameters can be passed from the platform;
  789. * in case of these are not passed a default is kept for the MAC or GMAC.
  790. */
  791. static int sxgbe_init_dma_engine(struct sxgbe_priv_data *priv)
  792. {
  793. int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_map = 0;
  794. int queue_num;
  795. if (priv->plat->dma_cfg) {
  796. pbl = priv->plat->dma_cfg->pbl;
  797. fixed_burst = priv->plat->dma_cfg->fixed_burst;
  798. burst_map = priv->plat->dma_cfg->burst_map;
  799. }
  800. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num)
  801. priv->hw->dma->cha_init(priv->ioaddr, queue_num,
  802. fixed_burst, pbl,
  803. (priv->txq[queue_num])->dma_tx_phy,
  804. (priv->rxq[queue_num])->dma_rx_phy,
  805. priv->dma_tx_size, priv->dma_rx_size);
  806. return priv->hw->dma->init(priv->ioaddr, fixed_burst, burst_map);
  807. }
  808. /**
  809. * sxgbe_init_mtl_engine: MTL init.
  810. * @priv: driver private structure
  811. * Description:
  812. * It inits the MTL invoking the specific SXGBE callback.
  813. */
  814. static void sxgbe_init_mtl_engine(struct sxgbe_priv_data *priv)
  815. {
  816. int queue_num;
  817. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num) {
  818. priv->hw->mtl->mtl_set_txfifosize(priv->ioaddr, queue_num,
  819. priv->hw_cap.tx_mtl_qsize);
  820. priv->hw->mtl->mtl_enable_txqueue(priv->ioaddr, queue_num);
  821. }
  822. }
  823. /**
  824. * sxgbe_disable_mtl_engine: MTL disable.
  825. * @priv: driver private structure
  826. * Description:
  827. * It disables the MTL queues by invoking the specific SXGBE callback.
  828. */
  829. static void sxgbe_disable_mtl_engine(struct sxgbe_priv_data *priv)
  830. {
  831. int queue_num;
  832. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num)
  833. priv->hw->mtl->mtl_disable_txqueue(priv->ioaddr, queue_num);
  834. }
  835. /**
  836. * sxgbe_tx_timer: mitigation sw timer for tx.
  837. * @data: data pointer
  838. * Description:
  839. * This is the timer handler to directly invoke the sxgbe_tx_clean.
  840. */
  841. static void sxgbe_tx_timer(unsigned long data)
  842. {
  843. struct sxgbe_tx_queue *p = (struct sxgbe_tx_queue *)data;
  844. sxgbe_tx_queue_clean(p);
  845. }
  846. /**
  847. * sxgbe_init_tx_coalesce: init tx mitigation options.
  848. * @priv: driver private structure
  849. * Description:
  850. * This inits the transmit coalesce parameters: i.e. timer rate,
  851. * timer handler and default threshold used for enabling the
  852. * interrupt on completion bit.
  853. */
  854. static void sxgbe_tx_init_coalesce(struct sxgbe_priv_data *priv)
  855. {
  856. u8 queue_num;
  857. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num) {
  858. struct sxgbe_tx_queue *p = priv->txq[queue_num];
  859. p->tx_coal_frames = SXGBE_TX_FRAMES;
  860. p->tx_coal_timer = SXGBE_COAL_TX_TIMER;
  861. init_timer(&p->txtimer);
  862. p->txtimer.expires = SXGBE_COAL_TIMER(p->tx_coal_timer);
  863. p->txtimer.data = (unsigned long)&priv->txq[queue_num];
  864. p->txtimer.function = sxgbe_tx_timer;
  865. add_timer(&p->txtimer);
  866. }
  867. }
  868. static void sxgbe_tx_del_timer(struct sxgbe_priv_data *priv)
  869. {
  870. u8 queue_num;
  871. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num) {
  872. struct sxgbe_tx_queue *p = priv->txq[queue_num];
  873. del_timer_sync(&p->txtimer);
  874. }
  875. }
  876. /**
  877. * sxgbe_open - open entry point of the driver
  878. * @dev : pointer to the device structure.
  879. * Description:
  880. * This function is the open entry point of the driver.
  881. * Return value:
  882. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  883. * file on failure.
  884. */
  885. static int sxgbe_open(struct net_device *dev)
  886. {
  887. struct sxgbe_priv_data *priv = netdev_priv(dev);
  888. int ret, queue_num;
  889. clk_prepare_enable(priv->sxgbe_clk);
  890. sxgbe_check_ether_addr(priv);
  891. /* Init the phy */
  892. ret = sxgbe_init_phy(dev);
  893. if (ret) {
  894. netdev_err(dev, "%s: Cannot attach to PHY (error: %d)\n",
  895. __func__, ret);
  896. goto phy_error;
  897. }
  898. /* Create and initialize the TX/RX descriptors chains. */
  899. priv->dma_tx_size = SXGBE_ALIGN(DMA_TX_SIZE);
  900. priv->dma_rx_size = SXGBE_ALIGN(DMA_RX_SIZE);
  901. priv->dma_buf_sz = SXGBE_ALIGN(DMA_BUFFER_SIZE);
  902. priv->tx_tc = TC_DEFAULT;
  903. priv->rx_tc = TC_DEFAULT;
  904. init_dma_desc_rings(dev);
  905. /* DMA initialization and SW reset */
  906. ret = sxgbe_init_dma_engine(priv);
  907. if (ret < 0) {
  908. netdev_err(dev, "%s: DMA initialization failed\n", __func__);
  909. goto init_error;
  910. }
  911. /* MTL initialization */
  912. sxgbe_init_mtl_engine(priv);
  913. /* Copy the MAC addr into the HW */
  914. priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
  915. /* Initialize the MAC Core */
  916. priv->hw->mac->core_init(priv->ioaddr);
  917. SXGBE_FOR_EACH_QUEUE(SXGBE_RX_QUEUES, queue_num) {
  918. priv->hw->mac->enable_rxqueue(priv->ioaddr, queue_num);
  919. }
  920. /* Request the IRQ lines */
  921. ret = devm_request_irq(priv->device, priv->irq, sxgbe_common_interrupt,
  922. IRQF_SHARED, dev->name, dev);
  923. if (unlikely(ret < 0)) {
  924. netdev_err(dev, "%s: ERROR: allocating the IRQ %d (error: %d)\n",
  925. __func__, priv->irq, ret);
  926. goto init_error;
  927. }
  928. /* If the LPI irq is different from the mac irq
  929. * register a dedicated handler
  930. */
  931. if (priv->lpi_irq != dev->irq) {
  932. ret = devm_request_irq(priv->device, priv->lpi_irq,
  933. sxgbe_common_interrupt,
  934. IRQF_SHARED, dev->name, dev);
  935. if (unlikely(ret < 0)) {
  936. netdev_err(dev, "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
  937. __func__, priv->lpi_irq, ret);
  938. goto init_error;
  939. }
  940. }
  941. /* Request TX DMA irq lines */
  942. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num) {
  943. ret = devm_request_irq(priv->device,
  944. (priv->txq[queue_num])->irq_no,
  945. sxgbe_tx_interrupt, 0,
  946. dev->name, priv->txq[queue_num]);
  947. if (unlikely(ret < 0)) {
  948. netdev_err(dev, "%s: ERROR: allocating TX IRQ %d (error: %d)\n",
  949. __func__, priv->irq, ret);
  950. goto init_error;
  951. }
  952. }
  953. /* Request RX DMA irq lines */
  954. SXGBE_FOR_EACH_QUEUE(SXGBE_RX_QUEUES, queue_num) {
  955. ret = devm_request_irq(priv->device,
  956. (priv->rxq[queue_num])->irq_no,
  957. sxgbe_rx_interrupt, 0,
  958. dev->name, priv->rxq[queue_num]);
  959. if (unlikely(ret < 0)) {
  960. netdev_err(dev, "%s: ERROR: allocating TX IRQ %d (error: %d)\n",
  961. __func__, priv->irq, ret);
  962. goto init_error;
  963. }
  964. }
  965. /* Enable the MAC Rx/Tx */
  966. priv->hw->mac->enable_tx(priv->ioaddr, true);
  967. priv->hw->mac->enable_rx(priv->ioaddr, true);
  968. /* Set the HW DMA mode and the COE */
  969. sxgbe_mtl_operation_mode(priv);
  970. /* Extra statistics */
  971. memset(&priv->xstats, 0, sizeof(struct sxgbe_extra_stats));
  972. priv->xstats.tx_threshold = priv->tx_tc;
  973. priv->xstats.rx_threshold = priv->rx_tc;
  974. /* Start the ball rolling... */
  975. netdev_dbg(dev, "DMA RX/TX processes started...\n");
  976. priv->hw->dma->start_tx(priv->ioaddr, SXGBE_TX_QUEUES);
  977. priv->hw->dma->start_rx(priv->ioaddr, SXGBE_RX_QUEUES);
  978. if (priv->phydev)
  979. phy_start(priv->phydev);
  980. /* initalise TX coalesce parameters */
  981. sxgbe_tx_init_coalesce(priv);
  982. if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
  983. priv->rx_riwt = SXGBE_MAX_DMA_RIWT;
  984. priv->hw->dma->rx_watchdog(priv->ioaddr, SXGBE_MAX_DMA_RIWT);
  985. }
  986. priv->tx_lpi_timer = SXGBE_DEFAULT_LPI_TIMER;
  987. priv->eee_enabled = sxgbe_eee_init(priv);
  988. napi_enable(&priv->napi);
  989. netif_start_queue(dev);
  990. return 0;
  991. init_error:
  992. free_dma_desc_resources(priv);
  993. if (priv->phydev)
  994. phy_disconnect(priv->phydev);
  995. phy_error:
  996. clk_disable_unprepare(priv->sxgbe_clk);
  997. return ret;
  998. }
  999. /**
  1000. * sxgbe_release - close entry point of the driver
  1001. * @dev : device pointer.
  1002. * Description:
  1003. * This is the stop entry point of the driver.
  1004. */
  1005. static int sxgbe_release(struct net_device *dev)
  1006. {
  1007. struct sxgbe_priv_data *priv = netdev_priv(dev);
  1008. if (priv->eee_enabled)
  1009. del_timer_sync(&priv->eee_ctrl_timer);
  1010. /* Stop and disconnect the PHY */
  1011. if (priv->phydev) {
  1012. phy_stop(priv->phydev);
  1013. phy_disconnect(priv->phydev);
  1014. priv->phydev = NULL;
  1015. }
  1016. netif_tx_stop_all_queues(dev);
  1017. napi_disable(&priv->napi);
  1018. /* delete TX timers */
  1019. sxgbe_tx_del_timer(priv);
  1020. /* Stop TX/RX DMA and clear the descriptors */
  1021. priv->hw->dma->stop_tx(priv->ioaddr, SXGBE_TX_QUEUES);
  1022. priv->hw->dma->stop_rx(priv->ioaddr, SXGBE_RX_QUEUES);
  1023. /* disable MTL queue */
  1024. sxgbe_disable_mtl_engine(priv);
  1025. /* Release and free the Rx/Tx resources */
  1026. free_dma_desc_resources(priv);
  1027. /* Disable the MAC Rx/Tx */
  1028. priv->hw->mac->enable_tx(priv->ioaddr, false);
  1029. priv->hw->mac->enable_rx(priv->ioaddr, false);
  1030. clk_disable_unprepare(priv->sxgbe_clk);
  1031. return 0;
  1032. }
  1033. /* Prepare first Tx descriptor for doing TSO operation */
  1034. static void sxgbe_tso_prepare(struct sxgbe_priv_data *priv,
  1035. struct sxgbe_tx_norm_desc *first_desc,
  1036. struct sk_buff *skb)
  1037. {
  1038. unsigned int total_hdr_len, tcp_hdr_len;
  1039. /* Write first Tx descriptor with appropriate value */
  1040. tcp_hdr_len = tcp_hdrlen(skb);
  1041. total_hdr_len = skb_transport_offset(skb) + tcp_hdr_len;
  1042. first_desc->tdes01 = dma_map_single(priv->device, skb->data,
  1043. total_hdr_len, DMA_TO_DEVICE);
  1044. if (dma_mapping_error(priv->device, first_desc->tdes01))
  1045. pr_err("%s: TX dma mapping failed!!\n", __func__);
  1046. first_desc->tdes23.tx_rd_des23.first_desc = 1;
  1047. priv->hw->desc->tx_desc_enable_tse(first_desc, 1, total_hdr_len,
  1048. tcp_hdr_len,
  1049. skb->len - total_hdr_len);
  1050. }
  1051. /**
  1052. * sxgbe_xmit: Tx entry point of the driver
  1053. * @skb : the socket buffer
  1054. * @dev : device pointer
  1055. * Description : this is the tx entry point of the driver.
  1056. * It programs the chain or the ring and supports oversized frames
  1057. * and SG feature.
  1058. */
  1059. static netdev_tx_t sxgbe_xmit(struct sk_buff *skb, struct net_device *dev)
  1060. {
  1061. unsigned int entry, frag_num;
  1062. int cksum_flag = 0;
  1063. struct netdev_queue *dev_txq;
  1064. unsigned txq_index = skb_get_queue_mapping(skb);
  1065. struct sxgbe_priv_data *priv = netdev_priv(dev);
  1066. unsigned int tx_rsize = priv->dma_tx_size;
  1067. struct sxgbe_tx_queue *tqueue = priv->txq[txq_index];
  1068. struct sxgbe_tx_norm_desc *tx_desc, *first_desc;
  1069. struct sxgbe_tx_ctxt_desc *ctxt_desc = NULL;
  1070. int nr_frags = skb_shinfo(skb)->nr_frags;
  1071. int no_pagedlen = skb_headlen(skb);
  1072. int is_jumbo = 0;
  1073. u16 cur_mss = skb_shinfo(skb)->gso_size;
  1074. u32 ctxt_desc_req = 0;
  1075. /* get the TX queue handle */
  1076. dev_txq = netdev_get_tx_queue(dev, txq_index);
  1077. if (unlikely(skb_is_gso(skb) && tqueue->prev_mss != cur_mss))
  1078. ctxt_desc_req = 1;
  1079. if (unlikely(vlan_tx_tag_present(skb) ||
  1080. ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1081. tqueue->hwts_tx_en)))
  1082. ctxt_desc_req = 1;
  1083. /* get the spinlock */
  1084. spin_lock(&tqueue->tx_lock);
  1085. if (priv->tx_path_in_lpi_mode)
  1086. sxgbe_disable_eee_mode(priv);
  1087. if (unlikely(sxgbe_tx_avail(tqueue, tx_rsize) < nr_frags + 1)) {
  1088. if (!netif_tx_queue_stopped(dev_txq)) {
  1089. netif_tx_stop_queue(dev_txq);
  1090. netdev_err(dev, "%s: Tx Ring is full when %d queue is awake\n",
  1091. __func__, txq_index);
  1092. }
  1093. /* release the spin lock in case of BUSY */
  1094. spin_unlock(&tqueue->tx_lock);
  1095. return NETDEV_TX_BUSY;
  1096. }
  1097. entry = tqueue->cur_tx % tx_rsize;
  1098. tx_desc = tqueue->dma_tx + entry;
  1099. first_desc = tx_desc;
  1100. if (ctxt_desc_req)
  1101. ctxt_desc = (struct sxgbe_tx_ctxt_desc *)first_desc;
  1102. /* save the skb address */
  1103. tqueue->tx_skbuff[entry] = skb;
  1104. if (!is_jumbo) {
  1105. if (likely(skb_is_gso(skb))) {
  1106. /* TSO support */
  1107. if (unlikely(tqueue->prev_mss != cur_mss)) {
  1108. priv->hw->desc->tx_ctxt_desc_set_mss(
  1109. ctxt_desc, cur_mss);
  1110. priv->hw->desc->tx_ctxt_desc_set_tcmssv(
  1111. ctxt_desc);
  1112. priv->hw->desc->tx_ctxt_desc_reset_ostc(
  1113. ctxt_desc);
  1114. priv->hw->desc->tx_ctxt_desc_set_ctxt(
  1115. ctxt_desc);
  1116. priv->hw->desc->tx_ctxt_desc_set_owner(
  1117. ctxt_desc);
  1118. entry = (++tqueue->cur_tx) % tx_rsize;
  1119. first_desc = tqueue->dma_tx + entry;
  1120. tqueue->prev_mss = cur_mss;
  1121. }
  1122. sxgbe_tso_prepare(priv, first_desc, skb);
  1123. } else {
  1124. tx_desc->tdes01 = dma_map_single(priv->device,
  1125. skb->data, no_pagedlen, DMA_TO_DEVICE);
  1126. if (dma_mapping_error(priv->device, tx_desc->tdes01))
  1127. netdev_err(dev, "%s: TX dma mapping failed!!\n",
  1128. __func__);
  1129. priv->hw->desc->prepare_tx_desc(tx_desc, 1, no_pagedlen,
  1130. no_pagedlen, cksum_flag);
  1131. }
  1132. }
  1133. for (frag_num = 0; frag_num < nr_frags; frag_num++) {
  1134. const skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_num];
  1135. int len = skb_frag_size(frag);
  1136. entry = (++tqueue->cur_tx) % tx_rsize;
  1137. tx_desc = tqueue->dma_tx + entry;
  1138. tx_desc->tdes01 = skb_frag_dma_map(priv->device, frag, 0, len,
  1139. DMA_TO_DEVICE);
  1140. tqueue->tx_skbuff_dma[entry] = tx_desc->tdes01;
  1141. tqueue->tx_skbuff[entry] = NULL;
  1142. /* prepare the descriptor */
  1143. priv->hw->desc->prepare_tx_desc(tx_desc, 0, len,
  1144. len, cksum_flag);
  1145. /* memory barrier to flush descriptor */
  1146. wmb();
  1147. /* set the owner */
  1148. priv->hw->desc->set_tx_owner(tx_desc);
  1149. }
  1150. /* close the descriptors */
  1151. priv->hw->desc->close_tx_desc(tx_desc);
  1152. /* memory barrier to flush descriptor */
  1153. wmb();
  1154. tqueue->tx_count_frames += nr_frags + 1;
  1155. if (tqueue->tx_count_frames > tqueue->tx_coal_frames) {
  1156. priv->hw->desc->clear_tx_ic(tx_desc);
  1157. priv->xstats.tx_reset_ic_bit++;
  1158. mod_timer(&tqueue->txtimer,
  1159. SXGBE_COAL_TIMER(tqueue->tx_coal_timer));
  1160. } else {
  1161. tqueue->tx_count_frames = 0;
  1162. }
  1163. /* set owner for first desc */
  1164. priv->hw->desc->set_tx_owner(first_desc);
  1165. /* memory barrier to flush descriptor */
  1166. wmb();
  1167. tqueue->cur_tx++;
  1168. /* display current ring */
  1169. netif_dbg(priv, pktdata, dev, "%s: curr %d dirty=%d entry=%d, first=%p, nfrags=%d\n",
  1170. __func__, tqueue->cur_tx % tx_rsize,
  1171. tqueue->dirty_tx % tx_rsize, entry,
  1172. first_desc, nr_frags);
  1173. if (unlikely(sxgbe_tx_avail(tqueue, tx_rsize) <= (MAX_SKB_FRAGS + 1))) {
  1174. netif_dbg(priv, hw, dev, "%s: stop transmitted packets\n",
  1175. __func__);
  1176. netif_tx_stop_queue(dev_txq);
  1177. }
  1178. dev->stats.tx_bytes += skb->len;
  1179. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1180. tqueue->hwts_tx_en)) {
  1181. /* declare that device is doing timestamping */
  1182. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1183. priv->hw->desc->tx_enable_tstamp(first_desc);
  1184. }
  1185. if (!tqueue->hwts_tx_en)
  1186. skb_tx_timestamp(skb);
  1187. priv->hw->dma->enable_dma_transmission(priv->ioaddr, txq_index);
  1188. spin_unlock(&tqueue->tx_lock);
  1189. return NETDEV_TX_OK;
  1190. }
  1191. /**
  1192. * sxgbe_rx_refill: refill used skb preallocated buffers
  1193. * @priv: driver private structure
  1194. * Description : this is to reallocate the skb for the reception process
  1195. * that is based on zero-copy.
  1196. */
  1197. static void sxgbe_rx_refill(struct sxgbe_priv_data *priv)
  1198. {
  1199. unsigned int rxsize = priv->dma_rx_size;
  1200. int bfsize = priv->dma_buf_sz;
  1201. u8 qnum = priv->cur_rx_qnum;
  1202. for (; priv->rxq[qnum]->cur_rx - priv->rxq[qnum]->dirty_rx > 0;
  1203. priv->rxq[qnum]->dirty_rx++) {
  1204. unsigned int entry = priv->rxq[qnum]->dirty_rx % rxsize;
  1205. struct sxgbe_rx_norm_desc *p;
  1206. p = priv->rxq[qnum]->dma_rx + entry;
  1207. if (likely(priv->rxq[qnum]->rx_skbuff[entry] == NULL)) {
  1208. struct sk_buff *skb;
  1209. skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
  1210. if (unlikely(skb == NULL))
  1211. break;
  1212. priv->rxq[qnum]->rx_skbuff[entry] = skb;
  1213. priv->rxq[qnum]->rx_skbuff_dma[entry] =
  1214. dma_map_single(priv->device, skb->data, bfsize,
  1215. DMA_FROM_DEVICE);
  1216. p->rdes23.rx_rd_des23.buf2_addr =
  1217. priv->rxq[qnum]->rx_skbuff_dma[entry];
  1218. }
  1219. /* Added memory barrier for RX descriptor modification */
  1220. wmb();
  1221. priv->hw->desc->set_rx_owner(p);
  1222. priv->hw->desc->set_rx_int_on_com(p);
  1223. /* Added memory barrier for RX descriptor modification */
  1224. wmb();
  1225. }
  1226. }
  1227. /**
  1228. * sxgbe_rx: receive the frames from the remote host
  1229. * @priv: driver private structure
  1230. * @limit: napi bugget.
  1231. * Description : this the function called by the napi poll method.
  1232. * It gets all the frames inside the ring.
  1233. */
  1234. static int sxgbe_rx(struct sxgbe_priv_data *priv, int limit)
  1235. {
  1236. u8 qnum = priv->cur_rx_qnum;
  1237. unsigned int rxsize = priv->dma_rx_size;
  1238. unsigned int entry = priv->rxq[qnum]->cur_rx;
  1239. unsigned int next_entry = 0;
  1240. unsigned int count = 0;
  1241. int checksum;
  1242. int status;
  1243. while (count < limit) {
  1244. struct sxgbe_rx_norm_desc *p;
  1245. struct sk_buff *skb;
  1246. int frame_len;
  1247. p = priv->rxq[qnum]->dma_rx + entry;
  1248. if (priv->hw->desc->get_rx_owner(p))
  1249. break;
  1250. count++;
  1251. next_entry = (++priv->rxq[qnum]->cur_rx) % rxsize;
  1252. prefetch(priv->rxq[qnum]->dma_rx + next_entry);
  1253. /* Read the status of the incoming frame and also get checksum
  1254. * value based on whether it is enabled in SXGBE hardware or
  1255. * not.
  1256. */
  1257. status = priv->hw->desc->rx_wbstatus(p, &priv->xstats,
  1258. &checksum);
  1259. if (unlikely(status < 0)) {
  1260. entry = next_entry;
  1261. continue;
  1262. }
  1263. if (unlikely(!priv->rxcsum_insertion))
  1264. checksum = CHECKSUM_NONE;
  1265. skb = priv->rxq[qnum]->rx_skbuff[entry];
  1266. if (unlikely(!skb))
  1267. netdev_err(priv->dev, "rx descriptor is not consistent\n");
  1268. prefetch(skb->data - NET_IP_ALIGN);
  1269. priv->rxq[qnum]->rx_skbuff[entry] = NULL;
  1270. frame_len = priv->hw->desc->get_rx_frame_len(p);
  1271. skb_put(skb, frame_len);
  1272. skb->ip_summed = checksum;
  1273. if (checksum == CHECKSUM_NONE)
  1274. netif_receive_skb(skb);
  1275. else
  1276. napi_gro_receive(&priv->napi, skb);
  1277. entry = next_entry;
  1278. }
  1279. sxgbe_rx_refill(priv);
  1280. return count;
  1281. }
  1282. /**
  1283. * sxgbe_poll - sxgbe poll method (NAPI)
  1284. * @napi : pointer to the napi structure.
  1285. * @budget : maximum number of packets that the current CPU can receive from
  1286. * all interfaces.
  1287. * Description :
  1288. * To look at the incoming frames and clear the tx resources.
  1289. */
  1290. static int sxgbe_poll(struct napi_struct *napi, int budget)
  1291. {
  1292. struct sxgbe_priv_data *priv = container_of(napi,
  1293. struct sxgbe_priv_data, napi);
  1294. int work_done = 0;
  1295. u8 qnum = priv->cur_rx_qnum;
  1296. priv->xstats.napi_poll++;
  1297. /* first, clean the tx queues */
  1298. sxgbe_tx_all_clean(priv);
  1299. work_done = sxgbe_rx(priv, budget);
  1300. if (work_done < budget) {
  1301. napi_complete(napi);
  1302. priv->hw->dma->enable_dma_irq(priv->ioaddr, qnum);
  1303. }
  1304. return work_done;
  1305. }
  1306. /**
  1307. * sxgbe_tx_timeout
  1308. * @dev : Pointer to net device structure
  1309. * Description: this function is called when a packet transmission fails to
  1310. * complete within a reasonable time. The driver will mark the error in the
  1311. * netdev structure and arrange for the device to be reset to a sane state
  1312. * in order to transmit a new packet.
  1313. */
  1314. static void sxgbe_tx_timeout(struct net_device *dev)
  1315. {
  1316. struct sxgbe_priv_data *priv = netdev_priv(dev);
  1317. sxgbe_reset_all_tx_queues(priv);
  1318. }
  1319. /**
  1320. * sxgbe_common_interrupt - main ISR
  1321. * @irq: interrupt number.
  1322. * @dev_id: to pass the net device pointer.
  1323. * Description: this is the main driver interrupt service routine.
  1324. * It calls the DMA ISR and also the core ISR to manage PMT, MMC, LPI
  1325. * interrupts.
  1326. */
  1327. static irqreturn_t sxgbe_common_interrupt(int irq, void *dev_id)
  1328. {
  1329. struct net_device *netdev = (struct net_device *)dev_id;
  1330. struct sxgbe_priv_data *priv = netdev_priv(netdev);
  1331. int status;
  1332. status = priv->hw->mac->host_irq_status(priv->ioaddr, &priv->xstats);
  1333. /* For LPI we need to save the tx status */
  1334. if (status & TX_ENTRY_LPI_MODE) {
  1335. priv->xstats.tx_lpi_entry_n++;
  1336. priv->tx_path_in_lpi_mode = true;
  1337. }
  1338. if (status & TX_EXIT_LPI_MODE) {
  1339. priv->xstats.tx_lpi_exit_n++;
  1340. priv->tx_path_in_lpi_mode = false;
  1341. }
  1342. if (status & RX_ENTRY_LPI_MODE)
  1343. priv->xstats.rx_lpi_entry_n++;
  1344. if (status & RX_EXIT_LPI_MODE)
  1345. priv->xstats.rx_lpi_exit_n++;
  1346. return IRQ_HANDLED;
  1347. }
  1348. /**
  1349. * sxgbe_tx_interrupt - TX DMA ISR
  1350. * @irq: interrupt number.
  1351. * @dev_id: to pass the net device pointer.
  1352. * Description: this is the tx dma interrupt service routine.
  1353. */
  1354. static irqreturn_t sxgbe_tx_interrupt(int irq, void *dev_id)
  1355. {
  1356. int status;
  1357. struct sxgbe_tx_queue *txq = (struct sxgbe_tx_queue *)dev_id;
  1358. struct sxgbe_priv_data *priv = txq->priv_ptr;
  1359. /* get the channel status */
  1360. status = priv->hw->dma->tx_dma_int_status(priv->ioaddr, txq->queue_no,
  1361. &priv->xstats);
  1362. /* check for normal path */
  1363. if (likely((status & handle_tx)))
  1364. napi_schedule(&priv->napi);
  1365. /* check for unrecoverable error */
  1366. if (unlikely((status & tx_hard_error)))
  1367. sxgbe_restart_tx_queue(priv, txq->queue_no);
  1368. /* check for TC configuration change */
  1369. if (unlikely((status & tx_bump_tc) &&
  1370. (priv->tx_tc != SXGBE_MTL_SFMODE) &&
  1371. (priv->tx_tc < 512))) {
  1372. /* step of TX TC is 32 till 128, otherwise 64 */
  1373. priv->tx_tc += (priv->tx_tc < 128) ? 32 : 64;
  1374. priv->hw->mtl->set_tx_mtl_mode(priv->ioaddr,
  1375. txq->queue_no, priv->tx_tc);
  1376. priv->xstats.tx_threshold = priv->tx_tc;
  1377. }
  1378. return IRQ_HANDLED;
  1379. }
  1380. /**
  1381. * sxgbe_rx_interrupt - RX DMA ISR
  1382. * @irq: interrupt number.
  1383. * @dev_id: to pass the net device pointer.
  1384. * Description: this is the rx dma interrupt service routine.
  1385. */
  1386. static irqreturn_t sxgbe_rx_interrupt(int irq, void *dev_id)
  1387. {
  1388. int status;
  1389. struct sxgbe_rx_queue *rxq = (struct sxgbe_rx_queue *)dev_id;
  1390. struct sxgbe_priv_data *priv = rxq->priv_ptr;
  1391. /* get the channel status */
  1392. status = priv->hw->dma->rx_dma_int_status(priv->ioaddr, rxq->queue_no,
  1393. &priv->xstats);
  1394. if (likely((status & handle_rx) && (napi_schedule_prep(&priv->napi)))) {
  1395. priv->hw->dma->disable_dma_irq(priv->ioaddr, rxq->queue_no);
  1396. __napi_schedule(&priv->napi);
  1397. }
  1398. /* check for TC configuration change */
  1399. if (unlikely((status & rx_bump_tc) &&
  1400. (priv->rx_tc != SXGBE_MTL_SFMODE) &&
  1401. (priv->rx_tc < 128))) {
  1402. /* step of TC is 32 */
  1403. priv->rx_tc += 32;
  1404. priv->hw->mtl->set_rx_mtl_mode(priv->ioaddr,
  1405. rxq->queue_no, priv->rx_tc);
  1406. priv->xstats.rx_threshold = priv->rx_tc;
  1407. }
  1408. return IRQ_HANDLED;
  1409. }
  1410. static inline u64 sxgbe_get_stat64(void __iomem *ioaddr, int reg_lo, int reg_hi)
  1411. {
  1412. u64 val = readl(ioaddr + reg_lo);
  1413. val |= ((u64)readl(ioaddr + reg_hi)) << 32;
  1414. return val;
  1415. }
  1416. /* sxgbe_get_stats64 - entry point to see statistical information of device
  1417. * @dev : device pointer.
  1418. * @stats : pointer to hold all the statistical information of device.
  1419. * Description:
  1420. * This function is a driver entry point whenever ifconfig command gets
  1421. * executed to see device statistics. Statistics are number of
  1422. * bytes sent or received, errors occured etc.
  1423. * Return value:
  1424. * This function returns various statistical information of device.
  1425. */
  1426. static struct rtnl_link_stats64 *sxgbe_get_stats64(struct net_device *dev,
  1427. struct rtnl_link_stats64 *stats)
  1428. {
  1429. struct sxgbe_priv_data *priv = netdev_priv(dev);
  1430. void __iomem *ioaddr = priv->ioaddr;
  1431. u64 count;
  1432. spin_lock(&priv->stats_lock);
  1433. /* Freeze the counter registers before reading value otherwise it may
  1434. * get updated by hardware while we are reading them
  1435. */
  1436. writel(SXGBE_MMC_CTRL_CNT_FRZ, ioaddr + SXGBE_MMC_CTL_REG);
  1437. stats->rx_bytes = sxgbe_get_stat64(ioaddr,
  1438. SXGBE_MMC_RXOCTETLO_GCNT_REG,
  1439. SXGBE_MMC_RXOCTETHI_GCNT_REG);
  1440. stats->rx_packets = sxgbe_get_stat64(ioaddr,
  1441. SXGBE_MMC_RXFRAMELO_GBCNT_REG,
  1442. SXGBE_MMC_RXFRAMEHI_GBCNT_REG);
  1443. stats->multicast = sxgbe_get_stat64(ioaddr,
  1444. SXGBE_MMC_RXMULTILO_GCNT_REG,
  1445. SXGBE_MMC_RXMULTIHI_GCNT_REG);
  1446. stats->rx_crc_errors = sxgbe_get_stat64(ioaddr,
  1447. SXGBE_MMC_RXCRCERRLO_REG,
  1448. SXGBE_MMC_RXCRCERRHI_REG);
  1449. stats->rx_length_errors = sxgbe_get_stat64(ioaddr,
  1450. SXGBE_MMC_RXLENERRLO_REG,
  1451. SXGBE_MMC_RXLENERRHI_REG);
  1452. stats->rx_missed_errors = sxgbe_get_stat64(ioaddr,
  1453. SXGBE_MMC_RXFIFOOVERFLOWLO_GBCNT_REG,
  1454. SXGBE_MMC_RXFIFOOVERFLOWHI_GBCNT_REG);
  1455. stats->tx_bytes = sxgbe_get_stat64(ioaddr,
  1456. SXGBE_MMC_TXOCTETLO_GCNT_REG,
  1457. SXGBE_MMC_TXOCTETHI_GCNT_REG);
  1458. count = sxgbe_get_stat64(ioaddr, SXGBE_MMC_TXFRAMELO_GBCNT_REG,
  1459. SXGBE_MMC_TXFRAMEHI_GBCNT_REG);
  1460. stats->tx_errors = sxgbe_get_stat64(ioaddr, SXGBE_MMC_TXFRAMELO_GCNT_REG,
  1461. SXGBE_MMC_TXFRAMEHI_GCNT_REG);
  1462. stats->tx_errors = count - stats->tx_errors;
  1463. stats->tx_packets = count;
  1464. stats->tx_fifo_errors = sxgbe_get_stat64(ioaddr, SXGBE_MMC_TXUFLWLO_GBCNT_REG,
  1465. SXGBE_MMC_TXUFLWHI_GBCNT_REG);
  1466. writel(0, ioaddr + SXGBE_MMC_CTL_REG);
  1467. spin_unlock(&priv->stats_lock);
  1468. return stats;
  1469. }
  1470. /* sxgbe_set_features - entry point to set offload features of the device.
  1471. * @dev : device pointer.
  1472. * @features : features which are required to be set.
  1473. * Description:
  1474. * This function is a driver entry point and called by Linux kernel whenever
  1475. * any device features are set or reset by user.
  1476. * Return value:
  1477. * This function returns 0 after setting or resetting device features.
  1478. */
  1479. static int sxgbe_set_features(struct net_device *dev,
  1480. netdev_features_t features)
  1481. {
  1482. struct sxgbe_priv_data *priv = netdev_priv(dev);
  1483. netdev_features_t changed = dev->features ^ features;
  1484. if (changed & NETIF_F_RXCSUM) {
  1485. if (features & NETIF_F_RXCSUM) {
  1486. priv->hw->mac->enable_rx_csum(priv->ioaddr);
  1487. priv->rxcsum_insertion = true;
  1488. } else {
  1489. priv->hw->mac->disable_rx_csum(priv->ioaddr);
  1490. priv->rxcsum_insertion = false;
  1491. }
  1492. }
  1493. return 0;
  1494. }
  1495. /* sxgbe_change_mtu - entry point to change MTU size for the device.
  1496. * @dev : device pointer.
  1497. * @new_mtu : the new MTU size for the device.
  1498. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  1499. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  1500. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  1501. * Return value:
  1502. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1503. * file on failure.
  1504. */
  1505. static int sxgbe_change_mtu(struct net_device *dev, int new_mtu)
  1506. {
  1507. /* RFC 791, page 25, "Every internet module must be able to forward
  1508. * a datagram of 68 octets without further fragmentation."
  1509. */
  1510. if (new_mtu < MIN_MTU || (new_mtu > MAX_MTU)) {
  1511. netdev_err(dev, "invalid MTU, MTU should be in between %d and %d\n",
  1512. MIN_MTU, MAX_MTU);
  1513. return -EINVAL;
  1514. }
  1515. /* Return if the buffer sizes will not change */
  1516. if (dev->mtu == new_mtu)
  1517. return 0;
  1518. dev->mtu = new_mtu;
  1519. if (!netif_running(dev))
  1520. return 0;
  1521. /* Recevice ring buffer size is needed to be set based on MTU. If MTU is
  1522. * changed then reinitilisation of the receive ring buffers need to be
  1523. * done. Hence bring interface down and bring interface back up
  1524. */
  1525. sxgbe_release(dev);
  1526. return sxgbe_open(dev);
  1527. }
  1528. static void sxgbe_set_umac_addr(void __iomem *ioaddr, unsigned char *addr,
  1529. unsigned int reg_n)
  1530. {
  1531. unsigned long data;
  1532. data = (addr[5] << 8) | addr[4];
  1533. /* For MAC Addr registers se have to set the Address Enable (AE)
  1534. * bit that has no effect on the High Reg 0 where the bit 31 (MO)
  1535. * is RO.
  1536. */
  1537. writel(data | SXGBE_HI_REG_AE, ioaddr + SXGBE_ADDR_HIGH(reg_n));
  1538. data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  1539. writel(data, ioaddr + SXGBE_ADDR_LOW(reg_n));
  1540. }
  1541. /**
  1542. * sxgbe_set_rx_mode - entry point for setting different receive mode of
  1543. * a device. unicast, multicast addressing
  1544. * @dev : pointer to the device structure
  1545. * Description:
  1546. * This function is a driver entry point which gets called by the kernel
  1547. * whenever different receive mode like unicast, multicast and promiscuous
  1548. * must be enabled/disabled.
  1549. * Return value:
  1550. * void.
  1551. */
  1552. static void sxgbe_set_rx_mode(struct net_device *dev)
  1553. {
  1554. struct sxgbe_priv_data *priv = netdev_priv(dev);
  1555. void __iomem *ioaddr = (void __iomem *)priv->ioaddr;
  1556. unsigned int value = 0;
  1557. u32 mc_filter[2];
  1558. struct netdev_hw_addr *ha;
  1559. int reg = 1;
  1560. netdev_dbg(dev, "%s: # mcasts %d, # unicast %d\n",
  1561. __func__, netdev_mc_count(dev), netdev_uc_count(dev));
  1562. if (dev->flags & IFF_PROMISC) {
  1563. value = SXGBE_FRAME_FILTER_PR;
  1564. } else if ((netdev_mc_count(dev) > SXGBE_HASH_TABLE_SIZE) ||
  1565. (dev->flags & IFF_ALLMULTI)) {
  1566. value = SXGBE_FRAME_FILTER_PM; /* pass all multi */
  1567. writel(0xffffffff, ioaddr + SXGBE_HASH_HIGH);
  1568. writel(0xffffffff, ioaddr + SXGBE_HASH_LOW);
  1569. } else if (!netdev_mc_empty(dev)) {
  1570. /* Hash filter for multicast */
  1571. value = SXGBE_FRAME_FILTER_HMC;
  1572. memset(mc_filter, 0, sizeof(mc_filter));
  1573. netdev_for_each_mc_addr(ha, dev) {
  1574. /* The upper 6 bits of the calculated CRC are used to
  1575. * index the contens of the hash table
  1576. */
  1577. int bit_nr = bitrev32(~crc32_le(~0, ha->addr, 6)) >> 26;
  1578. /* The most significant bit determines the register to
  1579. * use (H/L) while the other 5 bits determine the bit
  1580. * within the register.
  1581. */
  1582. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1583. }
  1584. writel(mc_filter[0], ioaddr + SXGBE_HASH_LOW);
  1585. writel(mc_filter[1], ioaddr + SXGBE_HASH_HIGH);
  1586. }
  1587. /* Handle multiple unicast addresses (perfect filtering) */
  1588. if (netdev_uc_count(dev) > SXGBE_MAX_PERFECT_ADDRESSES)
  1589. /* Switch to promiscuous mode if more than 16 addrs
  1590. * are required
  1591. */
  1592. value |= SXGBE_FRAME_FILTER_PR;
  1593. else {
  1594. netdev_for_each_uc_addr(ha, dev) {
  1595. sxgbe_set_umac_addr(ioaddr, ha->addr, reg);
  1596. reg++;
  1597. }
  1598. }
  1599. #ifdef FRAME_FILTER_DEBUG
  1600. /* Enable Receive all mode (to debug filtering_fail errors) */
  1601. value |= SXGBE_FRAME_FILTER_RA;
  1602. #endif
  1603. writel(value, ioaddr + SXGBE_FRAME_FILTER);
  1604. netdev_dbg(dev, "Filter: 0x%08x\n\tHash: HI 0x%08x, LO 0x%08x\n",
  1605. readl(ioaddr + SXGBE_FRAME_FILTER),
  1606. readl(ioaddr + SXGBE_HASH_HIGH),
  1607. readl(ioaddr + SXGBE_HASH_LOW));
  1608. }
  1609. #ifdef CONFIG_NET_POLL_CONTROLLER
  1610. /**
  1611. * sxgbe_poll_controller - entry point for polling receive by device
  1612. * @dev : pointer to the device structure
  1613. * Description:
  1614. * This function is used by NETCONSOLE and other diagnostic tools
  1615. * to allow network I/O with interrupts disabled.
  1616. * Return value:
  1617. * Void.
  1618. */
  1619. static void sxgbe_poll_controller(struct net_device *dev)
  1620. {
  1621. struct sxgbe_priv_data *priv = netdev_priv(dev);
  1622. disable_irq(priv->irq);
  1623. sxgbe_rx_interrupt(priv->irq, dev);
  1624. enable_irq(priv->irq);
  1625. }
  1626. #endif
  1627. /* sxgbe_ioctl - Entry point for the Ioctl
  1628. * @dev: Device pointer.
  1629. * @rq: An IOCTL specefic structure, that can contain a pointer to
  1630. * a proprietary structure used to pass information to the driver.
  1631. * @cmd: IOCTL command
  1632. * Description:
  1633. * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
  1634. */
  1635. static int sxgbe_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1636. {
  1637. struct sxgbe_priv_data *priv = netdev_priv(dev);
  1638. int ret = -EOPNOTSUPP;
  1639. if (!netif_running(dev))
  1640. return -EINVAL;
  1641. switch (cmd) {
  1642. case SIOCGMIIPHY:
  1643. case SIOCGMIIREG:
  1644. case SIOCSMIIREG:
  1645. if (!priv->phydev)
  1646. return -EINVAL;
  1647. ret = phy_mii_ioctl(priv->phydev, rq, cmd);
  1648. break;
  1649. default:
  1650. break;
  1651. }
  1652. return ret;
  1653. }
  1654. static const struct net_device_ops sxgbe_netdev_ops = {
  1655. .ndo_open = sxgbe_open,
  1656. .ndo_start_xmit = sxgbe_xmit,
  1657. .ndo_stop = sxgbe_release,
  1658. .ndo_get_stats64 = sxgbe_get_stats64,
  1659. .ndo_change_mtu = sxgbe_change_mtu,
  1660. .ndo_set_features = sxgbe_set_features,
  1661. .ndo_set_rx_mode = sxgbe_set_rx_mode,
  1662. .ndo_tx_timeout = sxgbe_tx_timeout,
  1663. .ndo_do_ioctl = sxgbe_ioctl,
  1664. #ifdef CONFIG_NET_POLL_CONTROLLER
  1665. .ndo_poll_controller = sxgbe_poll_controller,
  1666. #endif
  1667. .ndo_set_mac_address = eth_mac_addr,
  1668. };
  1669. /* Get the hardware ops */
  1670. static void sxgbe_get_ops(struct sxgbe_ops * const ops_ptr)
  1671. {
  1672. ops_ptr->mac = sxgbe_get_core_ops();
  1673. ops_ptr->desc = sxgbe_get_desc_ops();
  1674. ops_ptr->dma = sxgbe_get_dma_ops();
  1675. ops_ptr->mtl = sxgbe_get_mtl_ops();
  1676. /* set the MDIO communication Address/Data regisers */
  1677. ops_ptr->mii.addr = SXGBE_MDIO_SCMD_ADD_REG;
  1678. ops_ptr->mii.data = SXGBE_MDIO_SCMD_DATA_REG;
  1679. /* Assigning the default link settings
  1680. * no SXGBE defined default values to be set in registers,
  1681. * so assigning as 0 for port and duplex
  1682. */
  1683. ops_ptr->link.port = 0;
  1684. ops_ptr->link.duplex = 0;
  1685. ops_ptr->link.speed = SXGBE_SPEED_10G;
  1686. }
  1687. /**
  1688. * sxgbe_hw_init - Init the GMAC device
  1689. * @priv: driver private structure
  1690. * Description: this function checks the HW capability
  1691. * (if supported) and sets the driver's features.
  1692. */
  1693. static int sxgbe_hw_init(struct sxgbe_priv_data * const priv)
  1694. {
  1695. u32 ctrl_ids;
  1696. priv->hw = kmalloc(sizeof(*priv->hw), GFP_KERNEL);
  1697. if(!priv->hw)
  1698. return -ENOMEM;
  1699. /* get the hardware ops */
  1700. sxgbe_get_ops(priv->hw);
  1701. /* get the controller id */
  1702. ctrl_ids = priv->hw->mac->get_controller_version(priv->ioaddr);
  1703. priv->hw->ctrl_uid = (ctrl_ids & 0x00ff0000) >> 16;
  1704. priv->hw->ctrl_id = (ctrl_ids & 0x000000ff);
  1705. pr_info("user ID: 0x%x, Controller ID: 0x%x\n",
  1706. priv->hw->ctrl_uid, priv->hw->ctrl_id);
  1707. /* get the H/W features */
  1708. if (!sxgbe_get_hw_features(priv))
  1709. pr_info("Hardware features not found\n");
  1710. if (priv->hw_cap.tx_csum_offload)
  1711. pr_info("TX Checksum offload supported\n");
  1712. if (priv->hw_cap.rx_csum_offload)
  1713. pr_info("RX Checksum offload supported\n");
  1714. return 0;
  1715. }
  1716. static int sxgbe_sw_reset(void __iomem *addr)
  1717. {
  1718. int retry_count = 10;
  1719. writel(SXGBE_DMA_SOFT_RESET, addr + SXGBE_DMA_MODE_REG);
  1720. while (retry_count--) {
  1721. if (!(readl(addr + SXGBE_DMA_MODE_REG) &
  1722. SXGBE_DMA_SOFT_RESET))
  1723. break;
  1724. mdelay(10);
  1725. }
  1726. if (retry_count < 0)
  1727. return -EBUSY;
  1728. return 0;
  1729. }
  1730. /**
  1731. * sxgbe_drv_probe
  1732. * @device: device pointer
  1733. * @plat_dat: platform data pointer
  1734. * @addr: iobase memory address
  1735. * Description: this is the main probe function used to
  1736. * call the alloc_etherdev, allocate the priv structure.
  1737. */
  1738. struct sxgbe_priv_data *sxgbe_drv_probe(struct device *device,
  1739. struct sxgbe_plat_data *plat_dat,
  1740. void __iomem *addr)
  1741. {
  1742. struct sxgbe_priv_data *priv;
  1743. struct net_device *ndev;
  1744. int ret;
  1745. u8 queue_num;
  1746. ndev = alloc_etherdev_mqs(sizeof(struct sxgbe_priv_data),
  1747. SXGBE_TX_QUEUES, SXGBE_RX_QUEUES);
  1748. if (!ndev)
  1749. return NULL;
  1750. SET_NETDEV_DEV(ndev, device);
  1751. priv = netdev_priv(ndev);
  1752. priv->device = device;
  1753. priv->dev = ndev;
  1754. sxgbe_set_ethtool_ops(ndev);
  1755. priv->plat = plat_dat;
  1756. priv->ioaddr = addr;
  1757. ret = sxgbe_sw_reset(priv->ioaddr);
  1758. if (ret)
  1759. goto error_free_netdev;
  1760. /* Verify driver arguments */
  1761. sxgbe_verify_args();
  1762. /* Init MAC and get the capabilities */
  1763. ret = sxgbe_hw_init(priv);
  1764. if (ret)
  1765. goto error_free_netdev;
  1766. /* allocate memory resources for Descriptor rings */
  1767. ret = txring_mem_alloc(priv);
  1768. if (ret)
  1769. goto error_free_hw;
  1770. ret = rxring_mem_alloc(priv);
  1771. if (ret)
  1772. goto error_free_hw;
  1773. ndev->netdev_ops = &sxgbe_netdev_ops;
  1774. ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1775. NETIF_F_RXCSUM | NETIF_F_TSO | NETIF_F_TSO6 |
  1776. NETIF_F_GRO;
  1777. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  1778. ndev->watchdog_timeo = msecs_to_jiffies(TX_TIMEO);
  1779. /* assign filtering support */
  1780. ndev->priv_flags |= IFF_UNICAST_FLT;
  1781. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  1782. /* Enable TCP segmentation offload for all DMA channels */
  1783. if (priv->hw_cap.tcpseg_offload) {
  1784. SXGBE_FOR_EACH_QUEUE(SXGBE_TX_QUEUES, queue_num) {
  1785. priv->hw->dma->enable_tso(priv->ioaddr, queue_num);
  1786. }
  1787. }
  1788. /* Enable Rx checksum offload */
  1789. if (priv->hw_cap.rx_csum_offload) {
  1790. priv->hw->mac->enable_rx_csum(priv->ioaddr);
  1791. priv->rxcsum_insertion = true;
  1792. }
  1793. /* Initialise pause frame settings */
  1794. priv->rx_pause = 1;
  1795. priv->tx_pause = 1;
  1796. /* Rx Watchdog is available, enable depend on platform data */
  1797. if (!priv->plat->riwt_off) {
  1798. priv->use_riwt = 1;
  1799. pr_info("Enable RX Mitigation via HW Watchdog Timer\n");
  1800. }
  1801. netif_napi_add(ndev, &priv->napi, sxgbe_poll, 64);
  1802. spin_lock_init(&priv->stats_lock);
  1803. priv->sxgbe_clk = clk_get(priv->device, SXGBE_RESOURCE_NAME);
  1804. if (IS_ERR(priv->sxgbe_clk)) {
  1805. netdev_warn(ndev, "%s: warning: cannot get CSR clock\n",
  1806. __func__);
  1807. goto error_napi_del;
  1808. }
  1809. /* If a specific clk_csr value is passed from the platform
  1810. * this means that the CSR Clock Range selection cannot be
  1811. * changed at run-time and it is fixed. Viceversa the driver'll try to
  1812. * set the MDC clock dynamically according to the csr actual
  1813. * clock input.
  1814. */
  1815. if (!priv->plat->clk_csr)
  1816. sxgbe_clk_csr_set(priv);
  1817. else
  1818. priv->clk_csr = priv->plat->clk_csr;
  1819. /* MDIO bus Registration */
  1820. ret = sxgbe_mdio_register(ndev);
  1821. if (ret < 0) {
  1822. netdev_dbg(ndev, "%s: MDIO bus (id: %d) registration failed\n",
  1823. __func__, priv->plat->bus_id);
  1824. goto error_clk_put;
  1825. }
  1826. ret = register_netdev(ndev);
  1827. if (ret) {
  1828. pr_err("%s: ERROR %i registering the device\n", __func__, ret);
  1829. goto error_mdio_unregister;
  1830. }
  1831. sxgbe_check_ether_addr(priv);
  1832. return priv;
  1833. error_mdio_unregister:
  1834. sxgbe_mdio_unregister(ndev);
  1835. error_clk_put:
  1836. clk_put(priv->sxgbe_clk);
  1837. error_napi_del:
  1838. netif_napi_del(&priv->napi);
  1839. error_free_hw:
  1840. kfree(priv->hw);
  1841. error_free_netdev:
  1842. free_netdev(ndev);
  1843. return NULL;
  1844. }
  1845. /**
  1846. * sxgbe_drv_remove
  1847. * @ndev: net device pointer
  1848. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  1849. * changes the link status, releases the DMA descriptor rings.
  1850. */
  1851. int sxgbe_drv_remove(struct net_device *ndev)
  1852. {
  1853. struct sxgbe_priv_data *priv = netdev_priv(ndev);
  1854. u8 queue_num;
  1855. netdev_info(ndev, "%s: removing driver\n", __func__);
  1856. SXGBE_FOR_EACH_QUEUE(SXGBE_RX_QUEUES, queue_num) {
  1857. priv->hw->mac->disable_rxqueue(priv->ioaddr, queue_num);
  1858. }
  1859. priv->hw->dma->stop_rx(priv->ioaddr, SXGBE_RX_QUEUES);
  1860. priv->hw->dma->stop_tx(priv->ioaddr, SXGBE_TX_QUEUES);
  1861. priv->hw->mac->enable_tx(priv->ioaddr, false);
  1862. priv->hw->mac->enable_rx(priv->ioaddr, false);
  1863. unregister_netdev(ndev);
  1864. sxgbe_mdio_unregister(ndev);
  1865. clk_put(priv->sxgbe_clk);
  1866. netif_napi_del(&priv->napi);
  1867. kfree(priv->hw);
  1868. free_netdev(ndev);
  1869. return 0;
  1870. }
  1871. #ifdef CONFIG_PM
  1872. int sxgbe_suspend(struct net_device *ndev)
  1873. {
  1874. return 0;
  1875. }
  1876. int sxgbe_resume(struct net_device *ndev)
  1877. {
  1878. return 0;
  1879. }
  1880. int sxgbe_freeze(struct net_device *ndev)
  1881. {
  1882. return -ENOSYS;
  1883. }
  1884. int sxgbe_restore(struct net_device *ndev)
  1885. {
  1886. return -ENOSYS;
  1887. }
  1888. #endif /* CONFIG_PM */
  1889. /* Driver is configured as Platform driver */
  1890. static int __init sxgbe_init(void)
  1891. {
  1892. int ret;
  1893. ret = sxgbe_register_platform();
  1894. if (ret)
  1895. goto err;
  1896. return 0;
  1897. err:
  1898. pr_err("driver registration failed\n");
  1899. return ret;
  1900. }
  1901. static void __exit sxgbe_exit(void)
  1902. {
  1903. sxgbe_unregister_platform();
  1904. }
  1905. module_init(sxgbe_init);
  1906. module_exit(sxgbe_exit);
  1907. #ifndef MODULE
  1908. static int __init sxgbe_cmdline_opt(char *str)
  1909. {
  1910. char *opt;
  1911. if (!str || !*str)
  1912. return -EINVAL;
  1913. while ((opt = strsep(&str, ",")) != NULL) {
  1914. if (!strncmp(opt, "eee_timer:", 6)) {
  1915. if (kstrtoint(opt + 10, 0, &eee_timer))
  1916. goto err;
  1917. }
  1918. }
  1919. return 0;
  1920. err:
  1921. pr_err("%s: ERROR broken module parameter conversion\n", __func__);
  1922. return -EINVAL;
  1923. }
  1924. __setup("sxgbeeth=", sxgbe_cmdline_opt);
  1925. #endif /* MODULE */
  1926. MODULE_DESCRIPTION("SAMSUNG 10G/2.5G/1G Ethernet PLATFORM driver");
  1927. MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
  1928. MODULE_PARM_DESC(eee_timer, "EEE-LPI Default LS timer value");
  1929. MODULE_AUTHOR("Siva Reddy Kallam <siva.kallam@samsung.com>");
  1930. MODULE_AUTHOR("ByungHo An <bh74.an@samsung.com>");
  1931. MODULE_AUTHOR("Girish K S <ks.giri@samsung.com>");
  1932. MODULE_AUTHOR("Vipul Pandya <vipul.pandya@samsung.com>");
  1933. MODULE_LICENSE("GPL");