amd8111e.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Advanced Micro Devices Inc. AMD8111E Linux Network Driver
  3. * Copyright (C) 2004 Advanced Micro Devices
  4. *
  5. * Copyright 2001,2002 Jeff Garzik <[email protected]> [ 8139cp.c,tg3.c ]
  6. * Copyright (C) 2001, 2002 David S. Miller ([email protected])[ tg3.c]
  7. * Copyright 1996-1999 Thomas Bogendoerfer [ pcnet32.c ]
  8. * Derived from the lance driver written 1993,1994,1995 by Donald Becker.
  9. * Copyright 1993 United States Government as represented by the
  10. * Director, National Security Agency.[ pcnet32.c ]
  11. * Carsten Langgaard, [email protected] [ pcnet32.c ]
  12. * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
  13. *
  14. Module Name:
  15. amd8111e.c
  16. Abstract:
  17. AMD8111 based 10/100 Ethernet Controller Driver.
  18. Environment:
  19. Kernel Mode
  20. Revision History:
  21. 3.0.0
  22. Initial Revision.
  23. 3.0.1
  24. 1. Dynamic interrupt coalescing.
  25. 2. Removed prev_stats.
  26. 3. MII support.
  27. 4. Dynamic IPG support
  28. 3.0.2 05/29/2003
  29. 1. Bug fix: Fixed failure to send jumbo packets larger than 4k.
  30. 2. Bug fix: Fixed VLAN support failure.
  31. 3. Bug fix: Fixed receive interrupt coalescing bug.
  32. 4. Dynamic IPG support is disabled by default.
  33. 3.0.3 06/05/2003
  34. 1. Bug fix: Fixed failure to close the interface if SMP is enabled.
  35. 3.0.4 12/09/2003
  36. 1. Added set_mac_address routine for bonding driver support.
  37. 2. Tested the driver for bonding support
  38. 3. Bug fix: Fixed mismach in actual receive buffer length and length
  39. indicated to the h/w.
  40. 4. Modified amd8111e_rx() routine to receive all the received packets
  41. in the first interrupt.
  42. 5. Bug fix: Corrected rx_errors reported in get_stats() function.
  43. 3.0.5 03/22/2004
  44. 1. Added NAPI support
  45. */
  46. #include <linux/module.h>
  47. #include <linux/kernel.h>
  48. #include <linux/types.h>
  49. #include <linux/compiler.h>
  50. #include <linux/delay.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/ioport.h>
  53. #include <linux/pci.h>
  54. #include <linux/netdevice.h>
  55. #include <linux/etherdevice.h>
  56. #include <linux/skbuff.h>
  57. #include <linux/ethtool.h>
  58. #include <linux/mii.h>
  59. #include <linux/if_vlan.h>
  60. #include <linux/ctype.h>
  61. #include <linux/crc32.h>
  62. #include <linux/dma-mapping.h>
  63. #include <asm/io.h>
  64. #include <asm/byteorder.h>
  65. #include <linux/uaccess.h>
  66. #if IS_ENABLED(CONFIG_VLAN_8021Q)
  67. #define AMD8111E_VLAN_TAG_USED 1
  68. #else
  69. #define AMD8111E_VLAN_TAG_USED 0
  70. #endif
  71. #include "amd8111e.h"
  72. #define MODULE_NAME "amd8111e"
  73. MODULE_AUTHOR("Advanced Micro Devices, Inc.");
  74. MODULE_DESCRIPTION("AMD8111 based 10/100 Ethernet Controller.");
  75. MODULE_LICENSE("GPL");
  76. module_param_array(speed_duplex, int, NULL, 0);
  77. MODULE_PARM_DESC(speed_duplex, "Set device speed and duplex modes, 0: Auto Negotiate, 1: 10Mbps Half Duplex, 2: 10Mbps Full Duplex, 3: 100Mbps Half Duplex, 4: 100Mbps Full Duplex");
  78. module_param_array(coalesce, bool, NULL, 0);
  79. MODULE_PARM_DESC(coalesce, "Enable or Disable interrupt coalescing, 1: Enable, 0: Disable");
  80. module_param_array(dynamic_ipg, bool, NULL, 0);
  81. MODULE_PARM_DESC(dynamic_ipg, "Enable or Disable dynamic IPG, 1: Enable, 0: Disable");
  82. /* This function will read the PHY registers. */
  83. static int amd8111e_read_phy(struct amd8111e_priv *lp,
  84. int phy_id, int reg, u32 *val)
  85. {
  86. void __iomem *mmio = lp->mmio;
  87. unsigned int reg_val;
  88. unsigned int repeat = REPEAT_CNT;
  89. reg_val = readl(mmio + PHY_ACCESS);
  90. while (reg_val & PHY_CMD_ACTIVE)
  91. reg_val = readl(mmio + PHY_ACCESS);
  92. writel(PHY_RD_CMD | ((phy_id & 0x1f) << 21) |
  93. ((reg & 0x1f) << 16), mmio + PHY_ACCESS);
  94. do {
  95. reg_val = readl(mmio + PHY_ACCESS);
  96. udelay(30); /* It takes 30 us to read/write data */
  97. } while (--repeat && (reg_val & PHY_CMD_ACTIVE));
  98. if (reg_val & PHY_RD_ERR)
  99. goto err_phy_read;
  100. *val = reg_val & 0xffff;
  101. return 0;
  102. err_phy_read:
  103. *val = 0;
  104. return -EINVAL;
  105. }
  106. /* This function will write into PHY registers. */
  107. static int amd8111e_write_phy(struct amd8111e_priv *lp,
  108. int phy_id, int reg, u32 val)
  109. {
  110. unsigned int repeat = REPEAT_CNT;
  111. void __iomem *mmio = lp->mmio;
  112. unsigned int reg_val;
  113. reg_val = readl(mmio + PHY_ACCESS);
  114. while (reg_val & PHY_CMD_ACTIVE)
  115. reg_val = readl(mmio + PHY_ACCESS);
  116. writel(PHY_WR_CMD | ((phy_id & 0x1f) << 21) |
  117. ((reg & 0x1f) << 16)|val, mmio + PHY_ACCESS);
  118. do {
  119. reg_val = readl(mmio + PHY_ACCESS);
  120. udelay(30); /* It takes 30 us to read/write the data */
  121. } while (--repeat && (reg_val & PHY_CMD_ACTIVE));
  122. if (reg_val & PHY_RD_ERR)
  123. goto err_phy_write;
  124. return 0;
  125. err_phy_write:
  126. return -EINVAL;
  127. }
  128. /* This is the mii register read function provided to the mii interface. */
  129. static int amd8111e_mdio_read(struct net_device *dev, int phy_id, int reg_num)
  130. {
  131. struct amd8111e_priv *lp = netdev_priv(dev);
  132. unsigned int reg_val;
  133. amd8111e_read_phy(lp, phy_id, reg_num, &reg_val);
  134. return reg_val;
  135. }
  136. /* This is the mii register write function provided to the mii interface. */
  137. static void amd8111e_mdio_write(struct net_device *dev,
  138. int phy_id, int reg_num, int val)
  139. {
  140. struct amd8111e_priv *lp = netdev_priv(dev);
  141. amd8111e_write_phy(lp, phy_id, reg_num, val);
  142. }
  143. /* This function will set PHY speed. During initialization sets
  144. * the original speed to 100 full
  145. */
  146. static void amd8111e_set_ext_phy(struct net_device *dev)
  147. {
  148. struct amd8111e_priv *lp = netdev_priv(dev);
  149. u32 bmcr, advert, tmp;
  150. /* Determine mii register values to set the speed */
  151. advert = amd8111e_mdio_read(dev, lp->ext_phy_addr, MII_ADVERTISE);
  152. tmp = advert & ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
  153. switch (lp->ext_phy_option) {
  154. default:
  155. case SPEED_AUTONEG: /* advertise all values */
  156. tmp |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
  157. ADVERTISE_100HALF | ADVERTISE_100FULL);
  158. break;
  159. case SPEED10_HALF:
  160. tmp |= ADVERTISE_10HALF;
  161. break;
  162. case SPEED10_FULL:
  163. tmp |= ADVERTISE_10FULL;
  164. break;
  165. case SPEED100_HALF:
  166. tmp |= ADVERTISE_100HALF;
  167. break;
  168. case SPEED100_FULL:
  169. tmp |= ADVERTISE_100FULL;
  170. break;
  171. }
  172. if(advert != tmp)
  173. amd8111e_mdio_write(dev, lp->ext_phy_addr, MII_ADVERTISE, tmp);
  174. /* Restart auto negotiation */
  175. bmcr = amd8111e_mdio_read(dev, lp->ext_phy_addr, MII_BMCR);
  176. bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
  177. amd8111e_mdio_write(dev, lp->ext_phy_addr, MII_BMCR, bmcr);
  178. }
  179. /* This function will unmap skb->data space and will free
  180. * all transmit and receive skbuffs.
  181. */
  182. static int amd8111e_free_skbs(struct net_device *dev)
  183. {
  184. struct amd8111e_priv *lp = netdev_priv(dev);
  185. struct sk_buff *rx_skbuff;
  186. int i;
  187. /* Freeing transmit skbs */
  188. for (i = 0; i < NUM_TX_BUFFERS; i++) {
  189. if (lp->tx_skbuff[i]) {
  190. dma_unmap_single(&lp->pci_dev->dev,
  191. lp->tx_dma_addr[i],
  192. lp->tx_skbuff[i]->len, DMA_TO_DEVICE);
  193. dev_kfree_skb(lp->tx_skbuff[i]);
  194. lp->tx_skbuff[i] = NULL;
  195. lp->tx_dma_addr[i] = 0;
  196. }
  197. }
  198. /* Freeing previously allocated receive buffers */
  199. for (i = 0; i < NUM_RX_BUFFERS; i++) {
  200. rx_skbuff = lp->rx_skbuff[i];
  201. if (rx_skbuff) {
  202. dma_unmap_single(&lp->pci_dev->dev,
  203. lp->rx_dma_addr[i],
  204. lp->rx_buff_len - 2, DMA_FROM_DEVICE);
  205. dev_kfree_skb(lp->rx_skbuff[i]);
  206. lp->rx_skbuff[i] = NULL;
  207. lp->rx_dma_addr[i] = 0;
  208. }
  209. }
  210. return 0;
  211. }
  212. /* This will set the receive buffer length corresponding
  213. * to the mtu size of networkinterface.
  214. */
  215. static inline void amd8111e_set_rx_buff_len(struct net_device *dev)
  216. {
  217. struct amd8111e_priv *lp = netdev_priv(dev);
  218. unsigned int mtu = dev->mtu;
  219. if (mtu > ETH_DATA_LEN) {
  220. /* MTU + ethernet header + FCS
  221. * + optional VLAN tag + skb reserve space 2
  222. */
  223. lp->rx_buff_len = mtu + ETH_HLEN + 10;
  224. lp->options |= OPTION_JUMBO_ENABLE;
  225. } else {
  226. lp->rx_buff_len = PKT_BUFF_SZ;
  227. lp->options &= ~OPTION_JUMBO_ENABLE;
  228. }
  229. }
  230. /* This function will free all the previously allocated buffers,
  231. * determine new receive buffer length and will allocate new receive buffers.
  232. * This function also allocates and initializes both the transmitter
  233. * and receive hardware descriptors.
  234. */
  235. static int amd8111e_init_ring(struct net_device *dev)
  236. {
  237. struct amd8111e_priv *lp = netdev_priv(dev);
  238. int i;
  239. lp->rx_idx = lp->tx_idx = 0;
  240. lp->tx_complete_idx = 0;
  241. lp->tx_ring_idx = 0;
  242. if (lp->opened)
  243. /* Free previously allocated transmit and receive skbs */
  244. amd8111e_free_skbs(dev);
  245. else {
  246. /* allocate the tx and rx descriptors */
  247. lp->tx_ring = dma_alloc_coherent(&lp->pci_dev->dev,
  248. sizeof(struct amd8111e_tx_dr) * NUM_TX_RING_DR,
  249. &lp->tx_ring_dma_addr, GFP_ATOMIC);
  250. if (!lp->tx_ring)
  251. goto err_no_mem;
  252. lp->rx_ring = dma_alloc_coherent(&lp->pci_dev->dev,
  253. sizeof(struct amd8111e_rx_dr) * NUM_RX_RING_DR,
  254. &lp->rx_ring_dma_addr, GFP_ATOMIC);
  255. if (!lp->rx_ring)
  256. goto err_free_tx_ring;
  257. }
  258. /* Set new receive buff size */
  259. amd8111e_set_rx_buff_len(dev);
  260. /* Allocating receive skbs */
  261. for (i = 0; i < NUM_RX_BUFFERS; i++) {
  262. lp->rx_skbuff[i] = netdev_alloc_skb(dev, lp->rx_buff_len);
  263. if (!lp->rx_skbuff[i]) {
  264. /* Release previos allocated skbs */
  265. for (--i; i >= 0; i--)
  266. dev_kfree_skb(lp->rx_skbuff[i]);
  267. goto err_free_rx_ring;
  268. }
  269. skb_reserve(lp->rx_skbuff[i], 2);
  270. }
  271. /* Initilaizing receive descriptors */
  272. for (i = 0; i < NUM_RX_BUFFERS; i++) {
  273. lp->rx_dma_addr[i] = dma_map_single(&lp->pci_dev->dev,
  274. lp->rx_skbuff[i]->data,
  275. lp->rx_buff_len - 2,
  276. DMA_FROM_DEVICE);
  277. lp->rx_ring[i].buff_phy_addr = cpu_to_le32(lp->rx_dma_addr[i]);
  278. lp->rx_ring[i].buff_count = cpu_to_le16(lp->rx_buff_len-2);
  279. wmb();
  280. lp->rx_ring[i].rx_flags = cpu_to_le16(OWN_BIT);
  281. }
  282. /* Initializing transmit descriptors */
  283. for (i = 0; i < NUM_TX_RING_DR; i++) {
  284. lp->tx_ring[i].buff_phy_addr = 0;
  285. lp->tx_ring[i].tx_flags = 0;
  286. lp->tx_ring[i].buff_count = 0;
  287. }
  288. return 0;
  289. err_free_rx_ring:
  290. dma_free_coherent(&lp->pci_dev->dev,
  291. sizeof(struct amd8111e_rx_dr) * NUM_RX_RING_DR,
  292. lp->rx_ring, lp->rx_ring_dma_addr);
  293. err_free_tx_ring:
  294. dma_free_coherent(&lp->pci_dev->dev,
  295. sizeof(struct amd8111e_tx_dr) * NUM_TX_RING_DR,
  296. lp->tx_ring, lp->tx_ring_dma_addr);
  297. err_no_mem:
  298. return -ENOMEM;
  299. }
  300. /* This function will set the interrupt coalescing according
  301. * to the input arguments
  302. */
  303. static int amd8111e_set_coalesce(struct net_device *dev, enum coal_mode cmod)
  304. {
  305. unsigned int timeout;
  306. unsigned int event_count;
  307. struct amd8111e_priv *lp = netdev_priv(dev);
  308. void __iomem *mmio = lp->mmio;
  309. struct amd8111e_coalesce_conf *coal_conf = &lp->coal_conf;
  310. switch(cmod)
  311. {
  312. case RX_INTR_COAL :
  313. timeout = coal_conf->rx_timeout;
  314. event_count = coal_conf->rx_event_count;
  315. if (timeout > MAX_TIMEOUT ||
  316. event_count > MAX_EVENT_COUNT)
  317. return -EINVAL;
  318. timeout = timeout * DELAY_TIMER_CONV;
  319. writel(VAL0|STINTEN, mmio+INTEN0);
  320. writel((u32)DLY_INT_A_R0 | (event_count << 16) |
  321. timeout, mmio + DLY_INT_A);
  322. break;
  323. case TX_INTR_COAL:
  324. timeout = coal_conf->tx_timeout;
  325. event_count = coal_conf->tx_event_count;
  326. if (timeout > MAX_TIMEOUT ||
  327. event_count > MAX_EVENT_COUNT)
  328. return -EINVAL;
  329. timeout = timeout * DELAY_TIMER_CONV;
  330. writel(VAL0 | STINTEN, mmio + INTEN0);
  331. writel((u32)DLY_INT_B_T0 | (event_count << 16) |
  332. timeout, mmio + DLY_INT_B);
  333. break;
  334. case DISABLE_COAL:
  335. writel(0, mmio + STVAL);
  336. writel(STINTEN, mmio + INTEN0);
  337. writel(0, mmio + DLY_INT_B);
  338. writel(0, mmio + DLY_INT_A);
  339. break;
  340. case ENABLE_COAL:
  341. /* Start the timer */
  342. writel((u32)SOFT_TIMER_FREQ, mmio + STVAL); /* 0.5 sec */
  343. writel(VAL0 | STINTEN, mmio + INTEN0);
  344. break;
  345. default:
  346. break;
  347. }
  348. return 0;
  349. }
  350. /* This function initializes the device registers and starts the device. */
  351. static int amd8111e_restart(struct net_device *dev)
  352. {
  353. struct amd8111e_priv *lp = netdev_priv(dev);
  354. void __iomem *mmio = lp->mmio;
  355. int i, reg_val;
  356. /* stop the chip */
  357. writel(RUN, mmio + CMD0);
  358. if (amd8111e_init_ring(dev))
  359. return -ENOMEM;
  360. /* enable the port manager and set auto negotiation always */
  361. writel((u32)VAL1 | EN_PMGR, mmio + CMD3);
  362. writel((u32)XPHYANE | XPHYRST, mmio + CTRL2);
  363. amd8111e_set_ext_phy(dev);
  364. /* set control registers */
  365. reg_val = readl(mmio + CTRL1);
  366. reg_val &= ~XMTSP_MASK;
  367. writel(reg_val | XMTSP_128 | CACHE_ALIGN, mmio + CTRL1);
  368. /* enable interrupt */
  369. writel(APINT5EN | APINT4EN | APINT3EN | APINT2EN | APINT1EN |
  370. APINT0EN | MIIPDTINTEN | MCCIINTEN | MCCINTEN | MREINTEN |
  371. SPNDINTEN | MPINTEN | SINTEN | STINTEN, mmio + INTEN0);
  372. writel(VAL3 | LCINTEN | VAL1 | TINTEN0 | VAL0 | RINTEN0, mmio + INTEN0);
  373. /* initialize tx and rx ring base addresses */
  374. writel((u32)lp->tx_ring_dma_addr, mmio + XMT_RING_BASE_ADDR0);
  375. writel((u32)lp->rx_ring_dma_addr, mmio + RCV_RING_BASE_ADDR0);
  376. writew((u32)NUM_TX_RING_DR, mmio + XMT_RING_LEN0);
  377. writew((u16)NUM_RX_RING_DR, mmio + RCV_RING_LEN0);
  378. /* set default IPG to 96 */
  379. writew((u32)DEFAULT_IPG, mmio + IPG);
  380. writew((u32)(DEFAULT_IPG-IFS1_DELTA), mmio + IFS1);
  381. if (lp->options & OPTION_JUMBO_ENABLE) {
  382. writel((u32)VAL2|JUMBO, mmio + CMD3);
  383. /* Reset REX_UFLO */
  384. writel(REX_UFLO, mmio + CMD2);
  385. /* Should not set REX_UFLO for jumbo frames */
  386. writel(VAL0 | APAD_XMT | REX_RTRY, mmio + CMD2);
  387. } else {
  388. writel(VAL0 | APAD_XMT | REX_RTRY | REX_UFLO, mmio + CMD2);
  389. writel((u32)JUMBO, mmio + CMD3);
  390. }
  391. #if AMD8111E_VLAN_TAG_USED
  392. writel((u32)VAL2 | VSIZE | VL_TAG_DEL, mmio + CMD3);
  393. #endif
  394. writel(VAL0 | APAD_XMT | REX_RTRY, mmio + CMD2);
  395. /* Setting the MAC address to the device */
  396. for (i = 0; i < ETH_ALEN; i++)
  397. writeb(dev->dev_addr[i], mmio + PADR + i);
  398. /* Enable interrupt coalesce */
  399. if (lp->options & OPTION_INTR_COAL_ENABLE) {
  400. netdev_info(dev, "Interrupt Coalescing Enabled.\n");
  401. amd8111e_set_coalesce(dev, ENABLE_COAL);
  402. }
  403. /* set RUN bit to start the chip */
  404. writel(VAL2 | RDMD0, mmio + CMD0);
  405. writel(VAL0 | INTREN | RUN, mmio + CMD0);
  406. /* To avoid PCI posting bug */
  407. readl(mmio+CMD0);
  408. return 0;
  409. }
  410. /* This function clears necessary the device registers. */
  411. static void amd8111e_init_hw_default(struct amd8111e_priv *lp)
  412. {
  413. unsigned int reg_val;
  414. unsigned int logic_filter[2] = {0,};
  415. void __iomem *mmio = lp->mmio;
  416. /* stop the chip */
  417. writel(RUN, mmio + CMD0);
  418. /* AUTOPOLL0 Register *//*TBD default value is 8100 in FPS */
  419. writew( 0x8100 | lp->ext_phy_addr, mmio + AUTOPOLL0);
  420. /* Clear RCV_RING_BASE_ADDR */
  421. writel(0, mmio + RCV_RING_BASE_ADDR0);
  422. /* Clear XMT_RING_BASE_ADDR */
  423. writel(0, mmio + XMT_RING_BASE_ADDR0);
  424. writel(0, mmio + XMT_RING_BASE_ADDR1);
  425. writel(0, mmio + XMT_RING_BASE_ADDR2);
  426. writel(0, mmio + XMT_RING_BASE_ADDR3);
  427. /* Clear CMD0 */
  428. writel(CMD0_CLEAR, mmio + CMD0);
  429. /* Clear CMD2 */
  430. writel(CMD2_CLEAR, mmio + CMD2);
  431. /* Clear CMD7 */
  432. writel(CMD7_CLEAR, mmio + CMD7);
  433. /* Clear DLY_INT_A and DLY_INT_B */
  434. writel(0x0, mmio + DLY_INT_A);
  435. writel(0x0, mmio + DLY_INT_B);
  436. /* Clear FLOW_CONTROL */
  437. writel(0x0, mmio + FLOW_CONTROL);
  438. /* Clear INT0 write 1 to clear register */
  439. reg_val = readl(mmio + INT0);
  440. writel(reg_val, mmio + INT0);
  441. /* Clear STVAL */
  442. writel(0x0, mmio + STVAL);
  443. /* Clear INTEN0 */
  444. writel(INTEN0_CLEAR, mmio + INTEN0);
  445. /* Clear LADRF */
  446. writel(0x0, mmio + LADRF);
  447. /* Set SRAM_SIZE & SRAM_BOUNDARY registers */
  448. writel(0x80010, mmio + SRAM_SIZE);
  449. /* Clear RCV_RING0_LEN */
  450. writel(0x0, mmio + RCV_RING_LEN0);
  451. /* Clear XMT_RING0/1/2/3_LEN */
  452. writel(0x0, mmio + XMT_RING_LEN0);
  453. writel(0x0, mmio + XMT_RING_LEN1);
  454. writel(0x0, mmio + XMT_RING_LEN2);
  455. writel(0x0, mmio + XMT_RING_LEN3);
  456. /* Clear XMT_RING_LIMIT */
  457. writel(0x0, mmio + XMT_RING_LIMIT);
  458. /* Clear MIB */
  459. writew(MIB_CLEAR, mmio + MIB_ADDR);
  460. /* Clear LARF */
  461. amd8111e_writeq(*(u64 *)logic_filter, mmio + LADRF);
  462. /* SRAM_SIZE register */
  463. reg_val = readl(mmio + SRAM_SIZE);
  464. if (lp->options & OPTION_JUMBO_ENABLE)
  465. writel(VAL2 | JUMBO, mmio + CMD3);
  466. #if AMD8111E_VLAN_TAG_USED
  467. writel(VAL2 | VSIZE | VL_TAG_DEL, mmio + CMD3);
  468. #endif
  469. /* Set default value to CTRL1 Register */
  470. writel(CTRL1_DEFAULT, mmio + CTRL1);
  471. /* To avoid PCI posting bug */
  472. readl(mmio + CMD2);
  473. }
  474. /* This function disables the interrupt and clears all the pending
  475. * interrupts in INT0
  476. */
  477. static void amd8111e_disable_interrupt(struct amd8111e_priv *lp)
  478. {
  479. u32 intr0;
  480. /* Disable interrupt */
  481. writel(INTREN, lp->mmio + CMD0);
  482. /* Clear INT0 */
  483. intr0 = readl(lp->mmio + INT0);
  484. writel(intr0, lp->mmio + INT0);
  485. /* To avoid PCI posting bug */
  486. readl(lp->mmio + INT0);
  487. }
  488. /* This function stops the chip. */
  489. static void amd8111e_stop_chip(struct amd8111e_priv *lp)
  490. {
  491. writel(RUN, lp->mmio + CMD0);
  492. /* To avoid PCI posting bug */
  493. readl(lp->mmio + CMD0);
  494. }
  495. /* This function frees the transmiter and receiver descriptor rings. */
  496. static void amd8111e_free_ring(struct amd8111e_priv *lp)
  497. {
  498. /* Free transmit and receive descriptor rings */
  499. if (lp->rx_ring) {
  500. dma_free_coherent(&lp->pci_dev->dev,
  501. sizeof(struct amd8111e_rx_dr) * NUM_RX_RING_DR,
  502. lp->rx_ring, lp->rx_ring_dma_addr);
  503. lp->rx_ring = NULL;
  504. }
  505. if (lp->tx_ring) {
  506. dma_free_coherent(&lp->pci_dev->dev,
  507. sizeof(struct amd8111e_tx_dr) * NUM_TX_RING_DR,
  508. lp->tx_ring, lp->tx_ring_dma_addr);
  509. lp->tx_ring = NULL;
  510. }
  511. }
  512. /* This function will free all the transmit skbs that are actually
  513. * transmitted by the device. It will check the ownership of the
  514. * skb before freeing the skb.
  515. */
  516. static int amd8111e_tx(struct net_device *dev)
  517. {
  518. struct amd8111e_priv *lp = netdev_priv(dev);
  519. int tx_index;
  520. int status;
  521. /* Complete all the transmit packet */
  522. while (lp->tx_complete_idx != lp->tx_idx) {
  523. tx_index = lp->tx_complete_idx & TX_RING_DR_MOD_MASK;
  524. status = le16_to_cpu(lp->tx_ring[tx_index].tx_flags);
  525. if (status & OWN_BIT)
  526. break; /* It still hasn't been Txed */
  527. lp->tx_ring[tx_index].buff_phy_addr = 0;
  528. /* We must free the original skb */
  529. if (lp->tx_skbuff[tx_index]) {
  530. dma_unmap_single(&lp->pci_dev->dev,
  531. lp->tx_dma_addr[tx_index],
  532. lp->tx_skbuff[tx_index]->len,
  533. DMA_TO_DEVICE);
  534. dev_consume_skb_irq(lp->tx_skbuff[tx_index]);
  535. lp->tx_skbuff[tx_index] = NULL;
  536. lp->tx_dma_addr[tx_index] = 0;
  537. }
  538. lp->tx_complete_idx++;
  539. /*COAL update tx coalescing parameters */
  540. lp->coal_conf.tx_packets++;
  541. lp->coal_conf.tx_bytes +=
  542. le16_to_cpu(lp->tx_ring[tx_index].buff_count);
  543. if (netif_queue_stopped(dev) &&
  544. lp->tx_complete_idx > lp->tx_idx - NUM_TX_BUFFERS + 2) {
  545. /* The ring is no longer full, clear tbusy. */
  546. /* lp->tx_full = 0; */
  547. netif_wake_queue(dev);
  548. }
  549. }
  550. return 0;
  551. }
  552. /* This function handles the driver receive operation in polling mode */
  553. static int amd8111e_rx_poll(struct napi_struct *napi, int budget)
  554. {
  555. struct amd8111e_priv *lp = container_of(napi, struct amd8111e_priv, napi);
  556. struct net_device *dev = lp->amd8111e_net_dev;
  557. int rx_index = lp->rx_idx & RX_RING_DR_MOD_MASK;
  558. void __iomem *mmio = lp->mmio;
  559. struct sk_buff *skb, *new_skb;
  560. int min_pkt_len, status;
  561. int num_rx_pkt = 0;
  562. short pkt_len;
  563. #if AMD8111E_VLAN_TAG_USED
  564. short vtag;
  565. #endif
  566. while (num_rx_pkt < budget) {
  567. status = le16_to_cpu(lp->rx_ring[rx_index].rx_flags);
  568. if (status & OWN_BIT)
  569. break;
  570. /* There is a tricky error noted by John Murphy,
  571. * <[email protected]> to Russ Nelson: Even with
  572. * full-sized * buffers it's possible for a
  573. * jabber packet to use two buffers, with only
  574. * the last correctly noting the error.
  575. */
  576. if (status & ERR_BIT) {
  577. /* resetting flags */
  578. lp->rx_ring[rx_index].rx_flags &= RESET_RX_FLAGS;
  579. goto err_next_pkt;
  580. }
  581. /* check for STP and ENP */
  582. if (!((status & STP_BIT) && (status & ENP_BIT))) {
  583. /* resetting flags */
  584. lp->rx_ring[rx_index].rx_flags &= RESET_RX_FLAGS;
  585. goto err_next_pkt;
  586. }
  587. pkt_len = le16_to_cpu(lp->rx_ring[rx_index].msg_count) - 4;
  588. #if AMD8111E_VLAN_TAG_USED
  589. vtag = status & TT_MASK;
  590. /* MAC will strip vlan tag */
  591. if (vtag != 0)
  592. min_pkt_len = MIN_PKT_LEN - 4;
  593. else
  594. #endif
  595. min_pkt_len = MIN_PKT_LEN;
  596. if (pkt_len < min_pkt_len) {
  597. lp->rx_ring[rx_index].rx_flags &= RESET_RX_FLAGS;
  598. lp->drv_rx_errors++;
  599. goto err_next_pkt;
  600. }
  601. new_skb = netdev_alloc_skb(dev, lp->rx_buff_len);
  602. if (!new_skb) {
  603. /* if allocation fail,
  604. * ignore that pkt and go to next one
  605. */
  606. lp->rx_ring[rx_index].rx_flags &= RESET_RX_FLAGS;
  607. lp->drv_rx_errors++;
  608. goto err_next_pkt;
  609. }
  610. skb_reserve(new_skb, 2);
  611. skb = lp->rx_skbuff[rx_index];
  612. dma_unmap_single(&lp->pci_dev->dev, lp->rx_dma_addr[rx_index],
  613. lp->rx_buff_len - 2, DMA_FROM_DEVICE);
  614. skb_put(skb, pkt_len);
  615. lp->rx_skbuff[rx_index] = new_skb;
  616. lp->rx_dma_addr[rx_index] = dma_map_single(&lp->pci_dev->dev,
  617. new_skb->data,
  618. lp->rx_buff_len - 2,
  619. DMA_FROM_DEVICE);
  620. skb->protocol = eth_type_trans(skb, dev);
  621. #if AMD8111E_VLAN_TAG_USED
  622. if (vtag == TT_VLAN_TAGGED) {
  623. u16 vlan_tag = le16_to_cpu(lp->rx_ring[rx_index].tag_ctrl_info);
  624. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  625. }
  626. #endif
  627. napi_gro_receive(napi, skb);
  628. /* COAL update rx coalescing parameters */
  629. lp->coal_conf.rx_packets++;
  630. lp->coal_conf.rx_bytes += pkt_len;
  631. num_rx_pkt++;
  632. err_next_pkt:
  633. lp->rx_ring[rx_index].buff_phy_addr
  634. = cpu_to_le32(lp->rx_dma_addr[rx_index]);
  635. lp->rx_ring[rx_index].buff_count =
  636. cpu_to_le16(lp->rx_buff_len-2);
  637. wmb();
  638. lp->rx_ring[rx_index].rx_flags |= cpu_to_le16(OWN_BIT);
  639. rx_index = (++lp->rx_idx) & RX_RING_DR_MOD_MASK;
  640. }
  641. if (num_rx_pkt < budget && napi_complete_done(napi, num_rx_pkt)) {
  642. unsigned long flags;
  643. /* Receive descriptor is empty now */
  644. spin_lock_irqsave(&lp->lock, flags);
  645. writel(VAL0|RINTEN0, mmio + INTEN0);
  646. writel(VAL2 | RDMD0, mmio + CMD0);
  647. spin_unlock_irqrestore(&lp->lock, flags);
  648. }
  649. return num_rx_pkt;
  650. }
  651. /* This function will indicate the link status to the kernel. */
  652. static int amd8111e_link_change(struct net_device *dev)
  653. {
  654. struct amd8111e_priv *lp = netdev_priv(dev);
  655. int status0, speed;
  656. /* read the link change */
  657. status0 = readl(lp->mmio + STAT0);
  658. if (status0 & LINK_STATS) {
  659. if (status0 & AUTONEG_COMPLETE)
  660. lp->link_config.autoneg = AUTONEG_ENABLE;
  661. else
  662. lp->link_config.autoneg = AUTONEG_DISABLE;
  663. if (status0 & FULL_DPLX)
  664. lp->link_config.duplex = DUPLEX_FULL;
  665. else
  666. lp->link_config.duplex = DUPLEX_HALF;
  667. speed = (status0 & SPEED_MASK) >> 7;
  668. if (speed == PHY_SPEED_10)
  669. lp->link_config.speed = SPEED_10;
  670. else if (speed == PHY_SPEED_100)
  671. lp->link_config.speed = SPEED_100;
  672. netdev_info(dev, "Link is Up. Speed is %s Mbps %s Duplex\n",
  673. (lp->link_config.speed == SPEED_100) ?
  674. "100" : "10",
  675. (lp->link_config.duplex == DUPLEX_FULL) ?
  676. "Full" : "Half");
  677. netif_carrier_on(dev);
  678. } else {
  679. lp->link_config.speed = SPEED_INVALID;
  680. lp->link_config.duplex = DUPLEX_INVALID;
  681. lp->link_config.autoneg = AUTONEG_INVALID;
  682. netdev_info(dev, "Link is Down.\n");
  683. netif_carrier_off(dev);
  684. }
  685. return 0;
  686. }
  687. /* This function reads the mib counters. */
  688. static int amd8111e_read_mib(void __iomem *mmio, u8 MIB_COUNTER)
  689. {
  690. unsigned int status;
  691. unsigned int data;
  692. unsigned int repeat = REPEAT_CNT;
  693. writew(MIB_RD_CMD | MIB_COUNTER, mmio + MIB_ADDR);
  694. do {
  695. status = readw(mmio + MIB_ADDR);
  696. udelay(2); /* controller takes MAX 2 us to get mib data */
  697. }
  698. while (--repeat && (status & MIB_CMD_ACTIVE));
  699. data = readl(mmio + MIB_DATA);
  700. return data;
  701. }
  702. /* This function reads the mib registers and returns the hardware statistics.
  703. * It updates previous internal driver statistics with new values.
  704. */
  705. static struct net_device_stats *amd8111e_get_stats(struct net_device *dev)
  706. {
  707. struct amd8111e_priv *lp = netdev_priv(dev);
  708. void __iomem *mmio = lp->mmio;
  709. unsigned long flags;
  710. struct net_device_stats *new_stats = &dev->stats;
  711. if (!lp->opened)
  712. return new_stats;
  713. spin_lock_irqsave(&lp->lock, flags);
  714. /* stats.rx_packets */
  715. new_stats->rx_packets = amd8111e_read_mib(mmio, rcv_broadcast_pkts)+
  716. amd8111e_read_mib(mmio, rcv_multicast_pkts)+
  717. amd8111e_read_mib(mmio, rcv_unicast_pkts);
  718. /* stats.tx_packets */
  719. new_stats->tx_packets = amd8111e_read_mib(mmio, xmt_packets);
  720. /*stats.rx_bytes */
  721. new_stats->rx_bytes = amd8111e_read_mib(mmio, rcv_octets);
  722. /* stats.tx_bytes */
  723. new_stats->tx_bytes = amd8111e_read_mib(mmio, xmt_octets);
  724. /* stats.rx_errors */
  725. /* hw errors + errors driver reported */
  726. new_stats->rx_errors = amd8111e_read_mib(mmio, rcv_undersize_pkts)+
  727. amd8111e_read_mib(mmio, rcv_fragments)+
  728. amd8111e_read_mib(mmio, rcv_jabbers)+
  729. amd8111e_read_mib(mmio, rcv_alignment_errors)+
  730. amd8111e_read_mib(mmio, rcv_fcs_errors)+
  731. amd8111e_read_mib(mmio, rcv_miss_pkts)+
  732. lp->drv_rx_errors;
  733. /* stats.tx_errors */
  734. new_stats->tx_errors = amd8111e_read_mib(mmio, xmt_underrun_pkts);
  735. /* stats.rx_dropped*/
  736. new_stats->rx_dropped = amd8111e_read_mib(mmio, rcv_miss_pkts);
  737. /* stats.tx_dropped*/
  738. new_stats->tx_dropped = amd8111e_read_mib(mmio, xmt_underrun_pkts);
  739. /* stats.multicast*/
  740. new_stats->multicast = amd8111e_read_mib(mmio, rcv_multicast_pkts);
  741. /* stats.collisions*/
  742. new_stats->collisions = amd8111e_read_mib(mmio, xmt_collisions);
  743. /* stats.rx_length_errors*/
  744. new_stats->rx_length_errors =
  745. amd8111e_read_mib(mmio, rcv_undersize_pkts)+
  746. amd8111e_read_mib(mmio, rcv_oversize_pkts);
  747. /* stats.rx_over_errors*/
  748. new_stats->rx_over_errors = amd8111e_read_mib(mmio, rcv_miss_pkts);
  749. /* stats.rx_crc_errors*/
  750. new_stats->rx_crc_errors = amd8111e_read_mib(mmio, rcv_fcs_errors);
  751. /* stats.rx_frame_errors*/
  752. new_stats->rx_frame_errors =
  753. amd8111e_read_mib(mmio, rcv_alignment_errors);
  754. /* stats.rx_fifo_errors */
  755. new_stats->rx_fifo_errors = amd8111e_read_mib(mmio, rcv_miss_pkts);
  756. /* stats.rx_missed_errors */
  757. new_stats->rx_missed_errors = amd8111e_read_mib(mmio, rcv_miss_pkts);
  758. /* stats.tx_aborted_errors*/
  759. new_stats->tx_aborted_errors =
  760. amd8111e_read_mib(mmio, xmt_excessive_collision);
  761. /* stats.tx_carrier_errors*/
  762. new_stats->tx_carrier_errors =
  763. amd8111e_read_mib(mmio, xmt_loss_carrier);
  764. /* stats.tx_fifo_errors*/
  765. new_stats->tx_fifo_errors = amd8111e_read_mib(mmio, xmt_underrun_pkts);
  766. /* stats.tx_window_errors*/
  767. new_stats->tx_window_errors =
  768. amd8111e_read_mib(mmio, xmt_late_collision);
  769. /* Reset the mibs for collecting new statistics */
  770. /* writew(MIB_CLEAR, mmio + MIB_ADDR);*/
  771. spin_unlock_irqrestore(&lp->lock, flags);
  772. return new_stats;
  773. }
  774. /* This function recalculate the interrupt coalescing mode on every interrupt
  775. * according to the datarate and the packet rate.
  776. */
  777. static int amd8111e_calc_coalesce(struct net_device *dev)
  778. {
  779. struct amd8111e_priv *lp = netdev_priv(dev);
  780. struct amd8111e_coalesce_conf *coal_conf = &lp->coal_conf;
  781. int tx_pkt_rate;
  782. int rx_pkt_rate;
  783. int tx_data_rate;
  784. int rx_data_rate;
  785. int rx_pkt_size;
  786. int tx_pkt_size;
  787. tx_pkt_rate = coal_conf->tx_packets - coal_conf->tx_prev_packets;
  788. coal_conf->tx_prev_packets = coal_conf->tx_packets;
  789. tx_data_rate = coal_conf->tx_bytes - coal_conf->tx_prev_bytes;
  790. coal_conf->tx_prev_bytes = coal_conf->tx_bytes;
  791. rx_pkt_rate = coal_conf->rx_packets - coal_conf->rx_prev_packets;
  792. coal_conf->rx_prev_packets = coal_conf->rx_packets;
  793. rx_data_rate = coal_conf->rx_bytes - coal_conf->rx_prev_bytes;
  794. coal_conf->rx_prev_bytes = coal_conf->rx_bytes;
  795. if (rx_pkt_rate < 800) {
  796. if (coal_conf->rx_coal_type != NO_COALESCE) {
  797. coal_conf->rx_timeout = 0x0;
  798. coal_conf->rx_event_count = 0;
  799. amd8111e_set_coalesce(dev, RX_INTR_COAL);
  800. coal_conf->rx_coal_type = NO_COALESCE;
  801. }
  802. } else {
  803. rx_pkt_size = rx_data_rate/rx_pkt_rate;
  804. if (rx_pkt_size < 128) {
  805. if (coal_conf->rx_coal_type != NO_COALESCE) {
  806. coal_conf->rx_timeout = 0;
  807. coal_conf->rx_event_count = 0;
  808. amd8111e_set_coalesce(dev, RX_INTR_COAL);
  809. coal_conf->rx_coal_type = NO_COALESCE;
  810. }
  811. } else if ((rx_pkt_size >= 128) && (rx_pkt_size < 512)) {
  812. if (coal_conf->rx_coal_type != LOW_COALESCE) {
  813. coal_conf->rx_timeout = 1;
  814. coal_conf->rx_event_count = 4;
  815. amd8111e_set_coalesce(dev, RX_INTR_COAL);
  816. coal_conf->rx_coal_type = LOW_COALESCE;
  817. }
  818. } else if ((rx_pkt_size >= 512) && (rx_pkt_size < 1024)) {
  819. if (coal_conf->rx_coal_type != MEDIUM_COALESCE) {
  820. coal_conf->rx_timeout = 1;
  821. coal_conf->rx_event_count = 4;
  822. amd8111e_set_coalesce(dev, RX_INTR_COAL);
  823. coal_conf->rx_coal_type = MEDIUM_COALESCE;
  824. }
  825. } else if (rx_pkt_size >= 1024) {
  826. if (coal_conf->rx_coal_type != HIGH_COALESCE) {
  827. coal_conf->rx_timeout = 2;
  828. coal_conf->rx_event_count = 3;
  829. amd8111e_set_coalesce(dev, RX_INTR_COAL);
  830. coal_conf->rx_coal_type = HIGH_COALESCE;
  831. }
  832. }
  833. }
  834. /* NOW FOR TX INTR COALESC */
  835. if (tx_pkt_rate < 800) {
  836. if (coal_conf->tx_coal_type != NO_COALESCE) {
  837. coal_conf->tx_timeout = 0x0;
  838. coal_conf->tx_event_count = 0;
  839. amd8111e_set_coalesce(dev, TX_INTR_COAL);
  840. coal_conf->tx_coal_type = NO_COALESCE;
  841. }
  842. } else {
  843. tx_pkt_size = tx_data_rate/tx_pkt_rate;
  844. if (tx_pkt_size < 128) {
  845. if (coal_conf->tx_coal_type != NO_COALESCE) {
  846. coal_conf->tx_timeout = 0;
  847. coal_conf->tx_event_count = 0;
  848. amd8111e_set_coalesce(dev, TX_INTR_COAL);
  849. coal_conf->tx_coal_type = NO_COALESCE;
  850. }
  851. } else if ((tx_pkt_size >= 128) && (tx_pkt_size < 512)) {
  852. if (coal_conf->tx_coal_type != LOW_COALESCE) {
  853. coal_conf->tx_timeout = 1;
  854. coal_conf->tx_event_count = 2;
  855. amd8111e_set_coalesce(dev, TX_INTR_COAL);
  856. coal_conf->tx_coal_type = LOW_COALESCE;
  857. }
  858. } else if ((tx_pkt_size >= 512) && (tx_pkt_size < 1024)) {
  859. if (coal_conf->tx_coal_type != MEDIUM_COALESCE) {
  860. coal_conf->tx_timeout = 2;
  861. coal_conf->tx_event_count = 5;
  862. amd8111e_set_coalesce(dev, TX_INTR_COAL);
  863. coal_conf->tx_coal_type = MEDIUM_COALESCE;
  864. }
  865. } else if (tx_pkt_size >= 1024) {
  866. if (coal_conf->tx_coal_type != HIGH_COALESCE) {
  867. coal_conf->tx_timeout = 4;
  868. coal_conf->tx_event_count = 8;
  869. amd8111e_set_coalesce(dev, TX_INTR_COAL);
  870. coal_conf->tx_coal_type = HIGH_COALESCE;
  871. }
  872. }
  873. }
  874. return 0;
  875. }
  876. /* This is device interrupt function. It handles transmit,
  877. * receive,link change and hardware timer interrupts.
  878. */
  879. static irqreturn_t amd8111e_interrupt(int irq, void *dev_id)
  880. {
  881. struct net_device *dev = (struct net_device *)dev_id;
  882. struct amd8111e_priv *lp = netdev_priv(dev);
  883. void __iomem *mmio = lp->mmio;
  884. unsigned int intr0, intren0;
  885. unsigned int handled = 1;
  886. if (unlikely(!dev))
  887. return IRQ_NONE;
  888. spin_lock(&lp->lock);
  889. /* disabling interrupt */
  890. writel(INTREN, mmio + CMD0);
  891. /* Read interrupt status */
  892. intr0 = readl(mmio + INT0);
  893. intren0 = readl(mmio + INTEN0);
  894. /* Process all the INT event until INTR bit is clear. */
  895. if (!(intr0 & INTR)) {
  896. handled = 0;
  897. goto err_no_interrupt;
  898. }
  899. /* Current driver processes 4 interrupts : RINT,TINT,LCINT,STINT */
  900. writel(intr0, mmio + INT0);
  901. /* Check if Receive Interrupt has occurred. */
  902. if (intr0 & RINT0) {
  903. if (napi_schedule_prep(&lp->napi)) {
  904. /* Disable receive interrupts */
  905. writel(RINTEN0, mmio + INTEN0);
  906. /* Schedule a polling routine */
  907. __napi_schedule(&lp->napi);
  908. } else if (intren0 & RINTEN0) {
  909. netdev_dbg(dev, "************Driver bug! interrupt while in poll\n");
  910. /* Fix by disable receive interrupts */
  911. writel(RINTEN0, mmio + INTEN0);
  912. }
  913. }
  914. /* Check if Transmit Interrupt has occurred. */
  915. if (intr0 & TINT0)
  916. amd8111e_tx(dev);
  917. /* Check if Link Change Interrupt has occurred. */
  918. if (intr0 & LCINT)
  919. amd8111e_link_change(dev);
  920. /* Check if Hardware Timer Interrupt has occurred. */
  921. if (intr0 & STINT)
  922. amd8111e_calc_coalesce(dev);
  923. err_no_interrupt:
  924. writel(VAL0 | INTREN, mmio + CMD0);
  925. spin_unlock(&lp->lock);
  926. return IRQ_RETVAL(handled);
  927. }
  928. #ifdef CONFIG_NET_POLL_CONTROLLER
  929. static void amd8111e_poll(struct net_device *dev)
  930. {
  931. unsigned long flags;
  932. local_irq_save(flags);
  933. amd8111e_interrupt(0, dev);
  934. local_irq_restore(flags);
  935. }
  936. #endif
  937. /* This function closes the network interface and updates
  938. * the statistics so that most recent statistics will be
  939. * available after the interface is down.
  940. */
  941. static int amd8111e_close(struct net_device *dev)
  942. {
  943. struct amd8111e_priv *lp = netdev_priv(dev);
  944. netif_stop_queue(dev);
  945. napi_disable(&lp->napi);
  946. spin_lock_irq(&lp->lock);
  947. amd8111e_disable_interrupt(lp);
  948. amd8111e_stop_chip(lp);
  949. /* Free transmit and receive skbs */
  950. amd8111e_free_skbs(lp->amd8111e_net_dev);
  951. netif_carrier_off(lp->amd8111e_net_dev);
  952. /* Delete ipg timer */
  953. if (lp->options & OPTION_DYN_IPG_ENABLE)
  954. del_timer_sync(&lp->ipg_data.ipg_timer);
  955. spin_unlock_irq(&lp->lock);
  956. free_irq(dev->irq, dev);
  957. amd8111e_free_ring(lp);
  958. /* Update the statistics before closing */
  959. amd8111e_get_stats(dev);
  960. lp->opened = 0;
  961. return 0;
  962. }
  963. /* This function opens new interface.It requests irq for the device,
  964. * initializes the device,buffers and descriptors, and starts the device.
  965. */
  966. static int amd8111e_open(struct net_device *dev)
  967. {
  968. struct amd8111e_priv *lp = netdev_priv(dev);
  969. if (dev->irq == 0 || request_irq(dev->irq, amd8111e_interrupt,
  970. IRQF_SHARED, dev->name, dev))
  971. return -EAGAIN;
  972. napi_enable(&lp->napi);
  973. spin_lock_irq(&lp->lock);
  974. amd8111e_init_hw_default(lp);
  975. if (amd8111e_restart(dev)) {
  976. spin_unlock_irq(&lp->lock);
  977. napi_disable(&lp->napi);
  978. if (dev->irq)
  979. free_irq(dev->irq, dev);
  980. return -ENOMEM;
  981. }
  982. /* Start ipg timer */
  983. if (lp->options & OPTION_DYN_IPG_ENABLE) {
  984. add_timer(&lp->ipg_data.ipg_timer);
  985. netdev_info(dev, "Dynamic IPG Enabled\n");
  986. }
  987. lp->opened = 1;
  988. spin_unlock_irq(&lp->lock);
  989. netif_start_queue(dev);
  990. return 0;
  991. }
  992. /* This function checks if there is any transmit descriptors
  993. * available to queue more packet.
  994. */
  995. static int amd8111e_tx_queue_avail(struct amd8111e_priv *lp)
  996. {
  997. int tx_index = lp->tx_idx & TX_BUFF_MOD_MASK;
  998. if (lp->tx_skbuff[tx_index])
  999. return -1;
  1000. else
  1001. return 0;
  1002. }
  1003. /* This function will queue the transmit packets to the
  1004. * descriptors and will trigger the send operation. It also
  1005. * initializes the transmit descriptors with buffer physical address,
  1006. * byte count, ownership to hardware etc.
  1007. */
  1008. static netdev_tx_t amd8111e_start_xmit(struct sk_buff *skb,
  1009. struct net_device *dev)
  1010. {
  1011. struct amd8111e_priv *lp = netdev_priv(dev);
  1012. int tx_index;
  1013. unsigned long flags;
  1014. spin_lock_irqsave(&lp->lock, flags);
  1015. tx_index = lp->tx_idx & TX_RING_DR_MOD_MASK;
  1016. lp->tx_ring[tx_index].buff_count = cpu_to_le16(skb->len);
  1017. lp->tx_skbuff[tx_index] = skb;
  1018. lp->tx_ring[tx_index].tx_flags = 0;
  1019. #if AMD8111E_VLAN_TAG_USED
  1020. if (skb_vlan_tag_present(skb)) {
  1021. lp->tx_ring[tx_index].tag_ctrl_cmd |=
  1022. cpu_to_le16(TCC_VLAN_INSERT);
  1023. lp->tx_ring[tx_index].tag_ctrl_info =
  1024. cpu_to_le16(skb_vlan_tag_get(skb));
  1025. }
  1026. #endif
  1027. lp->tx_dma_addr[tx_index] =
  1028. dma_map_single(&lp->pci_dev->dev, skb->data, skb->len,
  1029. DMA_TO_DEVICE);
  1030. lp->tx_ring[tx_index].buff_phy_addr =
  1031. cpu_to_le32(lp->tx_dma_addr[tx_index]);
  1032. /* Set FCS and LTINT bits */
  1033. wmb();
  1034. lp->tx_ring[tx_index].tx_flags |=
  1035. cpu_to_le16(OWN_BIT | STP_BIT | ENP_BIT|ADD_FCS_BIT|LTINT_BIT);
  1036. lp->tx_idx++;
  1037. /* Trigger an immediate send poll. */
  1038. writel(VAL1 | TDMD0, lp->mmio + CMD0);
  1039. writel(VAL2 | RDMD0, lp->mmio + CMD0);
  1040. if (amd8111e_tx_queue_avail(lp) < 0) {
  1041. netif_stop_queue(dev);
  1042. }
  1043. spin_unlock_irqrestore(&lp->lock, flags);
  1044. return NETDEV_TX_OK;
  1045. }
  1046. /* This function returns all the memory mapped registers of the device. */
  1047. static void amd8111e_read_regs(struct amd8111e_priv *lp, u32 *buf)
  1048. {
  1049. void __iomem *mmio = lp->mmio;
  1050. /* Read only necessary registers */
  1051. buf[0] = readl(mmio + XMT_RING_BASE_ADDR0);
  1052. buf[1] = readl(mmio + XMT_RING_LEN0);
  1053. buf[2] = readl(mmio + RCV_RING_BASE_ADDR0);
  1054. buf[3] = readl(mmio + RCV_RING_LEN0);
  1055. buf[4] = readl(mmio + CMD0);
  1056. buf[5] = readl(mmio + CMD2);
  1057. buf[6] = readl(mmio + CMD3);
  1058. buf[7] = readl(mmio + CMD7);
  1059. buf[8] = readl(mmio + INT0);
  1060. buf[9] = readl(mmio + INTEN0);
  1061. buf[10] = readl(mmio + LADRF);
  1062. buf[11] = readl(mmio + LADRF+4);
  1063. buf[12] = readl(mmio + STAT0);
  1064. }
  1065. /* This function sets promiscuos mode, all-multi mode or the multicast address
  1066. * list to the device.
  1067. */
  1068. static void amd8111e_set_multicast_list(struct net_device *dev)
  1069. {
  1070. struct netdev_hw_addr *ha;
  1071. struct amd8111e_priv *lp = netdev_priv(dev);
  1072. u32 mc_filter[2];
  1073. int bit_num;
  1074. if (dev->flags & IFF_PROMISC) {
  1075. writel(VAL2 | PROM, lp->mmio + CMD2);
  1076. return;
  1077. }
  1078. else
  1079. writel(PROM, lp->mmio + CMD2);
  1080. if (dev->flags & IFF_ALLMULTI ||
  1081. netdev_mc_count(dev) > MAX_FILTER_SIZE) {
  1082. /* get all multicast packet */
  1083. mc_filter[1] = mc_filter[0] = 0xffffffff;
  1084. lp->options |= OPTION_MULTICAST_ENABLE;
  1085. amd8111e_writeq(*(u64 *)mc_filter, lp->mmio + LADRF);
  1086. return;
  1087. }
  1088. if (netdev_mc_empty(dev)) {
  1089. /* get only own packets */
  1090. mc_filter[1] = mc_filter[0] = 0;
  1091. lp->options &= ~OPTION_MULTICAST_ENABLE;
  1092. amd8111e_writeq(*(u64 *)mc_filter, lp->mmio + LADRF);
  1093. /* disable promiscuous mode */
  1094. writel(PROM, lp->mmio + CMD2);
  1095. return;
  1096. }
  1097. /* load all the multicast addresses in the logic filter */
  1098. lp->options |= OPTION_MULTICAST_ENABLE;
  1099. mc_filter[1] = mc_filter[0] = 0;
  1100. netdev_for_each_mc_addr(ha, dev) {
  1101. bit_num = (ether_crc_le(ETH_ALEN, ha->addr) >> 26) & 0x3f;
  1102. mc_filter[bit_num >> 5] |= 1 << (bit_num & 31);
  1103. }
  1104. amd8111e_writeq(*(u64 *)mc_filter, lp->mmio + LADRF);
  1105. /* To eliminate PCI posting bug */
  1106. readl(lp->mmio + CMD2);
  1107. }
  1108. static void amd8111e_get_drvinfo(struct net_device *dev,
  1109. struct ethtool_drvinfo *info)
  1110. {
  1111. struct amd8111e_priv *lp = netdev_priv(dev);
  1112. struct pci_dev *pci_dev = lp->pci_dev;
  1113. strscpy(info->driver, MODULE_NAME, sizeof(info->driver));
  1114. snprintf(info->fw_version, sizeof(info->fw_version),
  1115. "%u", chip_version);
  1116. strscpy(info->bus_info, pci_name(pci_dev), sizeof(info->bus_info));
  1117. }
  1118. static int amd8111e_get_regs_len(struct net_device *dev)
  1119. {
  1120. return AMD8111E_REG_DUMP_LEN;
  1121. }
  1122. static void amd8111e_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *buf)
  1123. {
  1124. struct amd8111e_priv *lp = netdev_priv(dev);
  1125. regs->version = 0;
  1126. amd8111e_read_regs(lp, buf);
  1127. }
  1128. static int amd8111e_get_link_ksettings(struct net_device *dev,
  1129. struct ethtool_link_ksettings *cmd)
  1130. {
  1131. struct amd8111e_priv *lp = netdev_priv(dev);
  1132. spin_lock_irq(&lp->lock);
  1133. mii_ethtool_get_link_ksettings(&lp->mii_if, cmd);
  1134. spin_unlock_irq(&lp->lock);
  1135. return 0;
  1136. }
  1137. static int amd8111e_set_link_ksettings(struct net_device *dev,
  1138. const struct ethtool_link_ksettings *cmd)
  1139. {
  1140. struct amd8111e_priv *lp = netdev_priv(dev);
  1141. int res;
  1142. spin_lock_irq(&lp->lock);
  1143. res = mii_ethtool_set_link_ksettings(&lp->mii_if, cmd);
  1144. spin_unlock_irq(&lp->lock);
  1145. return res;
  1146. }
  1147. static int amd8111e_nway_reset(struct net_device *dev)
  1148. {
  1149. struct amd8111e_priv *lp = netdev_priv(dev);
  1150. return mii_nway_restart(&lp->mii_if);
  1151. }
  1152. static u32 amd8111e_get_link(struct net_device *dev)
  1153. {
  1154. struct amd8111e_priv *lp = netdev_priv(dev);
  1155. return mii_link_ok(&lp->mii_if);
  1156. }
  1157. static void amd8111e_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol_info)
  1158. {
  1159. struct amd8111e_priv *lp = netdev_priv(dev);
  1160. wol_info->supported = WAKE_MAGIC|WAKE_PHY;
  1161. if (lp->options & OPTION_WOL_ENABLE)
  1162. wol_info->wolopts = WAKE_MAGIC;
  1163. }
  1164. static int amd8111e_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol_info)
  1165. {
  1166. struct amd8111e_priv *lp = netdev_priv(dev);
  1167. if (wol_info->wolopts & ~(WAKE_MAGIC|WAKE_PHY))
  1168. return -EINVAL;
  1169. spin_lock_irq(&lp->lock);
  1170. if (wol_info->wolopts & WAKE_MAGIC)
  1171. lp->options |=
  1172. (OPTION_WOL_ENABLE | OPTION_WAKE_MAGIC_ENABLE);
  1173. else if (wol_info->wolopts & WAKE_PHY)
  1174. lp->options |=
  1175. (OPTION_WOL_ENABLE | OPTION_WAKE_PHY_ENABLE);
  1176. else
  1177. lp->options &= ~OPTION_WOL_ENABLE;
  1178. spin_unlock_irq(&lp->lock);
  1179. return 0;
  1180. }
  1181. static const struct ethtool_ops ops = {
  1182. .get_drvinfo = amd8111e_get_drvinfo,
  1183. .get_regs_len = amd8111e_get_regs_len,
  1184. .get_regs = amd8111e_get_regs,
  1185. .nway_reset = amd8111e_nway_reset,
  1186. .get_link = amd8111e_get_link,
  1187. .get_wol = amd8111e_get_wol,
  1188. .set_wol = amd8111e_set_wol,
  1189. .get_link_ksettings = amd8111e_get_link_ksettings,
  1190. .set_link_ksettings = amd8111e_set_link_ksettings,
  1191. };
  1192. /* This function handles all the ethtool ioctls. It gives driver info,
  1193. * gets/sets driver speed, gets memory mapped register values, forces
  1194. * auto negotiation, sets/gets WOL options for ethtool application.
  1195. */
  1196. static int amd8111e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1197. {
  1198. struct mii_ioctl_data *data = if_mii(ifr);
  1199. struct amd8111e_priv *lp = netdev_priv(dev);
  1200. int err;
  1201. u32 mii_regval;
  1202. switch (cmd) {
  1203. case SIOCGMIIPHY:
  1204. data->phy_id = lp->ext_phy_addr;
  1205. fallthrough;
  1206. case SIOCGMIIREG:
  1207. spin_lock_irq(&lp->lock);
  1208. err = amd8111e_read_phy(lp, data->phy_id,
  1209. data->reg_num & PHY_REG_ADDR_MASK, &mii_regval);
  1210. spin_unlock_irq(&lp->lock);
  1211. data->val_out = mii_regval;
  1212. return err;
  1213. case SIOCSMIIREG:
  1214. spin_lock_irq(&lp->lock);
  1215. err = amd8111e_write_phy(lp, data->phy_id,
  1216. data->reg_num & PHY_REG_ADDR_MASK, data->val_in);
  1217. spin_unlock_irq(&lp->lock);
  1218. return err;
  1219. default:
  1220. /* do nothing */
  1221. break;
  1222. }
  1223. return -EOPNOTSUPP;
  1224. }
  1225. static int amd8111e_set_mac_address(struct net_device *dev, void *p)
  1226. {
  1227. struct amd8111e_priv *lp = netdev_priv(dev);
  1228. int i;
  1229. struct sockaddr *addr = p;
  1230. eth_hw_addr_set(dev, addr->sa_data);
  1231. spin_lock_irq(&lp->lock);
  1232. /* Setting the MAC address to the device */
  1233. for (i = 0; i < ETH_ALEN; i++)
  1234. writeb(dev->dev_addr[i], lp->mmio + PADR + i);
  1235. spin_unlock_irq(&lp->lock);
  1236. return 0;
  1237. }
  1238. /* This function changes the mtu of the device. It restarts the device to
  1239. * initialize the descriptor with new receive buffers.
  1240. */
  1241. static int amd8111e_change_mtu(struct net_device *dev, int new_mtu)
  1242. {
  1243. struct amd8111e_priv *lp = netdev_priv(dev);
  1244. int err;
  1245. if (!netif_running(dev)) {
  1246. /* new_mtu will be used
  1247. * when device starts netxt time
  1248. */
  1249. dev->mtu = new_mtu;
  1250. return 0;
  1251. }
  1252. spin_lock_irq(&lp->lock);
  1253. /* stop the chip */
  1254. writel(RUN, lp->mmio + CMD0);
  1255. dev->mtu = new_mtu;
  1256. err = amd8111e_restart(dev);
  1257. spin_unlock_irq(&lp->lock);
  1258. if (!err)
  1259. netif_start_queue(dev);
  1260. return err;
  1261. }
  1262. static int amd8111e_enable_magicpkt(struct amd8111e_priv *lp)
  1263. {
  1264. writel(VAL1 | MPPLBA, lp->mmio + CMD3);
  1265. writel(VAL0 | MPEN_SW, lp->mmio + CMD7);
  1266. /* To eliminate PCI posting bug */
  1267. readl(lp->mmio + CMD7);
  1268. return 0;
  1269. }
  1270. static int amd8111e_enable_link_change(struct amd8111e_priv *lp)
  1271. {
  1272. /* Adapter is already stopped/suspended/interrupt-disabled */
  1273. writel(VAL0 | LCMODE_SW, lp->mmio + CMD7);
  1274. /* To eliminate PCI posting bug */
  1275. readl(lp->mmio + CMD7);
  1276. return 0;
  1277. }
  1278. /* This function is called when a packet transmission fails to complete
  1279. * within a reasonable period, on the assumption that an interrupt have
  1280. * failed or the interface is locked up. This function will reinitialize
  1281. * the hardware.
  1282. */
  1283. static void amd8111e_tx_timeout(struct net_device *dev, unsigned int txqueue)
  1284. {
  1285. struct amd8111e_priv *lp = netdev_priv(dev);
  1286. int err;
  1287. netdev_err(dev, "transmit timed out, resetting\n");
  1288. spin_lock_irq(&lp->lock);
  1289. err = amd8111e_restart(dev);
  1290. spin_unlock_irq(&lp->lock);
  1291. if (!err)
  1292. netif_wake_queue(dev);
  1293. }
  1294. static int __maybe_unused amd8111e_suspend(struct device *dev_d)
  1295. {
  1296. struct net_device *dev = dev_get_drvdata(dev_d);
  1297. struct amd8111e_priv *lp = netdev_priv(dev);
  1298. if (!netif_running(dev))
  1299. return 0;
  1300. /* disable the interrupt */
  1301. spin_lock_irq(&lp->lock);
  1302. amd8111e_disable_interrupt(lp);
  1303. spin_unlock_irq(&lp->lock);
  1304. netif_device_detach(dev);
  1305. /* stop chip */
  1306. spin_lock_irq(&lp->lock);
  1307. if (lp->options & OPTION_DYN_IPG_ENABLE)
  1308. del_timer_sync(&lp->ipg_data.ipg_timer);
  1309. amd8111e_stop_chip(lp);
  1310. spin_unlock_irq(&lp->lock);
  1311. if (lp->options & OPTION_WOL_ENABLE) {
  1312. /* enable wol */
  1313. if (lp->options & OPTION_WAKE_MAGIC_ENABLE)
  1314. amd8111e_enable_magicpkt(lp);
  1315. if (lp->options & OPTION_WAKE_PHY_ENABLE)
  1316. amd8111e_enable_link_change(lp);
  1317. device_set_wakeup_enable(dev_d, 1);
  1318. } else {
  1319. device_set_wakeup_enable(dev_d, 0);
  1320. }
  1321. return 0;
  1322. }
  1323. static int __maybe_unused amd8111e_resume(struct device *dev_d)
  1324. {
  1325. struct net_device *dev = dev_get_drvdata(dev_d);
  1326. struct amd8111e_priv *lp = netdev_priv(dev);
  1327. if (!netif_running(dev))
  1328. return 0;
  1329. netif_device_attach(dev);
  1330. spin_lock_irq(&lp->lock);
  1331. amd8111e_restart(dev);
  1332. /* Restart ipg timer */
  1333. if (lp->options & OPTION_DYN_IPG_ENABLE)
  1334. mod_timer(&lp->ipg_data.ipg_timer,
  1335. jiffies + IPG_CONVERGE_JIFFIES);
  1336. spin_unlock_irq(&lp->lock);
  1337. return 0;
  1338. }
  1339. static void amd8111e_config_ipg(struct timer_list *t)
  1340. {
  1341. struct amd8111e_priv *lp = from_timer(lp, t, ipg_data.ipg_timer);
  1342. struct ipg_info *ipg_data = &lp->ipg_data;
  1343. void __iomem *mmio = lp->mmio;
  1344. unsigned int prev_col_cnt = ipg_data->col_cnt;
  1345. unsigned int total_col_cnt;
  1346. unsigned int tmp_ipg;
  1347. if (lp->link_config.duplex == DUPLEX_FULL) {
  1348. ipg_data->ipg = DEFAULT_IPG;
  1349. return;
  1350. }
  1351. if (ipg_data->ipg_state == SSTATE) {
  1352. if (ipg_data->timer_tick == IPG_STABLE_TIME) {
  1353. ipg_data->timer_tick = 0;
  1354. ipg_data->ipg = MIN_IPG - IPG_STEP;
  1355. ipg_data->current_ipg = MIN_IPG;
  1356. ipg_data->diff_col_cnt = 0xFFFFFFFF;
  1357. ipg_data->ipg_state = CSTATE;
  1358. }
  1359. else
  1360. ipg_data->timer_tick++;
  1361. }
  1362. if (ipg_data->ipg_state == CSTATE) {
  1363. /* Get the current collision count */
  1364. total_col_cnt = ipg_data->col_cnt =
  1365. amd8111e_read_mib(mmio, xmt_collisions);
  1366. if ((total_col_cnt - prev_col_cnt) <
  1367. (ipg_data->diff_col_cnt)) {
  1368. ipg_data->diff_col_cnt =
  1369. total_col_cnt - prev_col_cnt;
  1370. ipg_data->ipg = ipg_data->current_ipg;
  1371. }
  1372. ipg_data->current_ipg += IPG_STEP;
  1373. if (ipg_data->current_ipg <= MAX_IPG)
  1374. tmp_ipg = ipg_data->current_ipg;
  1375. else {
  1376. tmp_ipg = ipg_data->ipg;
  1377. ipg_data->ipg_state = SSTATE;
  1378. }
  1379. writew((u32)tmp_ipg, mmio + IPG);
  1380. writew((u32)(tmp_ipg - IFS1_DELTA), mmio + IFS1);
  1381. }
  1382. mod_timer(&lp->ipg_data.ipg_timer, jiffies + IPG_CONVERGE_JIFFIES);
  1383. return;
  1384. }
  1385. static void amd8111e_probe_ext_phy(struct net_device *dev)
  1386. {
  1387. struct amd8111e_priv *lp = netdev_priv(dev);
  1388. int i;
  1389. for (i = 0x1e; i >= 0; i--) {
  1390. u32 id1, id2;
  1391. if (amd8111e_read_phy(lp, i, MII_PHYSID1, &id1))
  1392. continue;
  1393. if (amd8111e_read_phy(lp, i, MII_PHYSID2, &id2))
  1394. continue;
  1395. lp->ext_phy_id = (id1 << 16) | id2;
  1396. lp->ext_phy_addr = i;
  1397. return;
  1398. }
  1399. lp->ext_phy_id = 0;
  1400. lp->ext_phy_addr = 1;
  1401. }
  1402. static const struct net_device_ops amd8111e_netdev_ops = {
  1403. .ndo_open = amd8111e_open,
  1404. .ndo_stop = amd8111e_close,
  1405. .ndo_start_xmit = amd8111e_start_xmit,
  1406. .ndo_tx_timeout = amd8111e_tx_timeout,
  1407. .ndo_get_stats = amd8111e_get_stats,
  1408. .ndo_set_rx_mode = amd8111e_set_multicast_list,
  1409. .ndo_validate_addr = eth_validate_addr,
  1410. .ndo_set_mac_address = amd8111e_set_mac_address,
  1411. .ndo_eth_ioctl = amd8111e_ioctl,
  1412. .ndo_change_mtu = amd8111e_change_mtu,
  1413. #ifdef CONFIG_NET_POLL_CONTROLLER
  1414. .ndo_poll_controller = amd8111e_poll,
  1415. #endif
  1416. };
  1417. static int amd8111e_probe_one(struct pci_dev *pdev,
  1418. const struct pci_device_id *ent)
  1419. {
  1420. int err, i;
  1421. unsigned long reg_addr, reg_len;
  1422. struct amd8111e_priv *lp;
  1423. struct net_device *dev;
  1424. u8 addr[ETH_ALEN];
  1425. err = pci_enable_device(pdev);
  1426. if (err) {
  1427. dev_err(&pdev->dev, "Cannot enable new PCI device\n");
  1428. return err;
  1429. }
  1430. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  1431. dev_err(&pdev->dev, "Cannot find PCI base address\n");
  1432. err = -ENODEV;
  1433. goto err_disable_pdev;
  1434. }
  1435. err = pci_request_regions(pdev, MODULE_NAME);
  1436. if (err) {
  1437. dev_err(&pdev->dev, "Cannot obtain PCI resources\n");
  1438. goto err_disable_pdev;
  1439. }
  1440. pci_set_master(pdev);
  1441. /* Find power-management capability. */
  1442. if (!pdev->pm_cap) {
  1443. dev_err(&pdev->dev, "No Power Management capability\n");
  1444. err = -ENODEV;
  1445. goto err_free_reg;
  1446. }
  1447. /* Initialize DMA */
  1448. if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)) < 0) {
  1449. dev_err(&pdev->dev, "DMA not supported\n");
  1450. err = -ENODEV;
  1451. goto err_free_reg;
  1452. }
  1453. reg_addr = pci_resource_start(pdev, 0);
  1454. reg_len = pci_resource_len(pdev, 0);
  1455. dev = alloc_etherdev(sizeof(struct amd8111e_priv));
  1456. if (!dev) {
  1457. err = -ENOMEM;
  1458. goto err_free_reg;
  1459. }
  1460. SET_NETDEV_DEV(dev, &pdev->dev);
  1461. #if AMD8111E_VLAN_TAG_USED
  1462. dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  1463. #endif
  1464. lp = netdev_priv(dev);
  1465. lp->pci_dev = pdev;
  1466. lp->amd8111e_net_dev = dev;
  1467. lp->pm_cap = pdev->pm_cap;
  1468. spin_lock_init(&lp->lock);
  1469. lp->mmio = devm_ioremap(&pdev->dev, reg_addr, reg_len);
  1470. if (!lp->mmio) {
  1471. dev_err(&pdev->dev, "Cannot map device registers\n");
  1472. err = -ENOMEM;
  1473. goto err_free_dev;
  1474. }
  1475. /* Initializing MAC address */
  1476. for (i = 0; i < ETH_ALEN; i++)
  1477. addr[i] = readb(lp->mmio + PADR + i);
  1478. eth_hw_addr_set(dev, addr);
  1479. /* Setting user defined parametrs */
  1480. lp->ext_phy_option = speed_duplex[card_idx];
  1481. if (coalesce[card_idx])
  1482. lp->options |= OPTION_INTR_COAL_ENABLE;
  1483. if (dynamic_ipg[card_idx++])
  1484. lp->options |= OPTION_DYN_IPG_ENABLE;
  1485. /* Initialize driver entry points */
  1486. dev->netdev_ops = &amd8111e_netdev_ops;
  1487. dev->ethtool_ops = &ops;
  1488. dev->irq = pdev->irq;
  1489. dev->watchdog_timeo = AMD8111E_TX_TIMEOUT;
  1490. dev->min_mtu = AMD8111E_MIN_MTU;
  1491. dev->max_mtu = AMD8111E_MAX_MTU;
  1492. netif_napi_add_weight(dev, &lp->napi, amd8111e_rx_poll, 32);
  1493. /* Probe the external PHY */
  1494. amd8111e_probe_ext_phy(dev);
  1495. /* setting mii default values */
  1496. lp->mii_if.dev = dev;
  1497. lp->mii_if.mdio_read = amd8111e_mdio_read;
  1498. lp->mii_if.mdio_write = amd8111e_mdio_write;
  1499. lp->mii_if.phy_id = lp->ext_phy_addr;
  1500. /* Set receive buffer length and set jumbo option*/
  1501. amd8111e_set_rx_buff_len(dev);
  1502. err = register_netdev(dev);
  1503. if (err) {
  1504. dev_err(&pdev->dev, "Cannot register net device\n");
  1505. goto err_free_dev;
  1506. }
  1507. pci_set_drvdata(pdev, dev);
  1508. /* Initialize software ipg timer */
  1509. if (lp->options & OPTION_DYN_IPG_ENABLE) {
  1510. timer_setup(&lp->ipg_data.ipg_timer, amd8111e_config_ipg, 0);
  1511. lp->ipg_data.ipg_timer.expires = jiffies +
  1512. IPG_CONVERGE_JIFFIES;
  1513. lp->ipg_data.ipg = DEFAULT_IPG;
  1514. lp->ipg_data.ipg_state = CSTATE;
  1515. }
  1516. /* display driver and device information */
  1517. chip_version = (readl(lp->mmio + CHIPID) & 0xf0000000) >> 28;
  1518. dev_info(&pdev->dev, "[ Rev %x ] PCI 10/100BaseT Ethernet %pM\n",
  1519. chip_version, dev->dev_addr);
  1520. if (lp->ext_phy_id)
  1521. dev_info(&pdev->dev, "Found MII PHY ID 0x%08x at address 0x%02x\n",
  1522. lp->ext_phy_id, lp->ext_phy_addr);
  1523. else
  1524. dev_info(&pdev->dev, "Couldn't detect MII PHY, assuming address 0x01\n");
  1525. return 0;
  1526. err_free_dev:
  1527. free_netdev(dev);
  1528. err_free_reg:
  1529. pci_release_regions(pdev);
  1530. err_disable_pdev:
  1531. pci_disable_device(pdev);
  1532. return err;
  1533. }
  1534. static void amd8111e_remove_one(struct pci_dev *pdev)
  1535. {
  1536. struct net_device *dev = pci_get_drvdata(pdev);
  1537. if (dev) {
  1538. unregister_netdev(dev);
  1539. free_netdev(dev);
  1540. pci_release_regions(pdev);
  1541. pci_disable_device(pdev);
  1542. }
  1543. }
  1544. static const struct pci_device_id amd8111e_pci_tbl[] = {
  1545. {
  1546. .vendor = PCI_VENDOR_ID_AMD,
  1547. .device = PCI_DEVICE_ID_AMD8111E_7462,
  1548. },
  1549. {
  1550. .vendor = 0,
  1551. }
  1552. };
  1553. MODULE_DEVICE_TABLE(pci, amd8111e_pci_tbl);
  1554. static SIMPLE_DEV_PM_OPS(amd8111e_pm_ops, amd8111e_suspend, amd8111e_resume);
  1555. static struct pci_driver amd8111e_driver = {
  1556. .name = MODULE_NAME,
  1557. .id_table = amd8111e_pci_tbl,
  1558. .probe = amd8111e_probe_one,
  1559. .remove = amd8111e_remove_one,
  1560. .driver.pm = &amd8111e_pm_ops
  1561. };
  1562. module_pci_driver(amd8111e_driver);