sxgbe_main.c 63 KB

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