amd8111e.c 50 KB

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