sxgbe_main.c 62 KB

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