amd8111e.c 50 KB

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