sxgbe_main.c 63 KB

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