amd8111e.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  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 <asm/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 = lp->tx_complete_idx & TX_RING_DR_MOD_MASK;
  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. unsigned int intr0;
  568. int num_rx_pkt = 0;
  569. short pkt_len;
  570. #if AMD8111E_VLAN_TAG_USED
  571. short vtag;
  572. #endif
  573. int rx_pkt_limit = budget;
  574. unsigned long flags;
  575. if (rx_pkt_limit <= 0)
  576. goto rx_not_empty;
  577. do{
  578. /* process receive packets until we use the quota.
  579. * If we own the next entry, it's a new packet. Send it up.
  580. */
  581. while(1) {
  582. status = le16_to_cpu(lp->rx_ring[rx_index].rx_flags);
  583. if (status & OWN_BIT)
  584. break;
  585. /* There is a tricky error noted by John Murphy,
  586. * <murf@perftech.com> to Russ Nelson: Even with
  587. * full-sized * buffers it's possible for a
  588. * jabber packet to use two buffers, with only
  589. * the last correctly noting the error.
  590. */
  591. if(status & ERR_BIT) {
  592. /* resetting flags */
  593. lp->rx_ring[rx_index].rx_flags &= RESET_RX_FLAGS;
  594. goto err_next_pkt;
  595. }
  596. /* check for STP and ENP */
  597. if(!((status & STP_BIT) && (status & ENP_BIT))){
  598. /* resetting flags */
  599. lp->rx_ring[rx_index].rx_flags &= RESET_RX_FLAGS;
  600. goto err_next_pkt;
  601. }
  602. pkt_len = le16_to_cpu(lp->rx_ring[rx_index].msg_count) - 4;
  603. #if AMD8111E_VLAN_TAG_USED
  604. vtag = status & TT_MASK;
  605. /*MAC will strip vlan tag*/
  606. if (vtag != 0)
  607. min_pkt_len =MIN_PKT_LEN - 4;
  608. else
  609. #endif
  610. min_pkt_len =MIN_PKT_LEN;
  611. if (pkt_len < min_pkt_len) {
  612. lp->rx_ring[rx_index].rx_flags &= RESET_RX_FLAGS;
  613. lp->drv_rx_errors++;
  614. goto err_next_pkt;
  615. }
  616. if(--rx_pkt_limit < 0)
  617. goto rx_not_empty;
  618. new_skb = netdev_alloc_skb(dev, lp->rx_buff_len);
  619. if (!new_skb) {
  620. /* if allocation fail,
  621. * ignore that pkt and go to next one
  622. */
  623. lp->rx_ring[rx_index].rx_flags &= RESET_RX_FLAGS;
  624. lp->drv_rx_errors++;
  625. goto err_next_pkt;
  626. }
  627. skb_reserve(new_skb, 2);
  628. skb = lp->rx_skbuff[rx_index];
  629. pci_unmap_single(lp->pci_dev,lp->rx_dma_addr[rx_index],
  630. lp->rx_buff_len-2, PCI_DMA_FROMDEVICE);
  631. skb_put(skb, pkt_len);
  632. lp->rx_skbuff[rx_index] = new_skb;
  633. lp->rx_dma_addr[rx_index] = pci_map_single(lp->pci_dev,
  634. new_skb->data,
  635. lp->rx_buff_len-2,
  636. PCI_DMA_FROMDEVICE);
  637. skb->protocol = eth_type_trans(skb, dev);
  638. #if AMD8111E_VLAN_TAG_USED
  639. if (vtag == TT_VLAN_TAGGED){
  640. u16 vlan_tag = le16_to_cpu(lp->rx_ring[rx_index].tag_ctrl_info);
  641. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  642. }
  643. #endif
  644. netif_receive_skb(skb);
  645. /*COAL update rx coalescing parameters*/
  646. lp->coal_conf.rx_packets++;
  647. lp->coal_conf.rx_bytes += pkt_len;
  648. num_rx_pkt++;
  649. err_next_pkt:
  650. lp->rx_ring[rx_index].buff_phy_addr
  651. = cpu_to_le32(lp->rx_dma_addr[rx_index]);
  652. lp->rx_ring[rx_index].buff_count =
  653. cpu_to_le16(lp->rx_buff_len-2);
  654. wmb();
  655. lp->rx_ring[rx_index].rx_flags |= cpu_to_le16(OWN_BIT);
  656. rx_index = (++lp->rx_idx) & RX_RING_DR_MOD_MASK;
  657. }
  658. /* Check the interrupt status register for more packets in the
  659. * mean time. Process them since we have not used up our quota.
  660. */
  661. intr0 = readl(mmio + INT0);
  662. /*Ack receive packets */
  663. writel(intr0 & RINT0,mmio + INT0);
  664. } while(intr0 & RINT0);
  665. if (rx_pkt_limit > 0) {
  666. /* Receive descriptor is empty now */
  667. spin_lock_irqsave(&lp->lock, flags);
  668. __napi_complete(napi);
  669. writel(VAL0|RINTEN0, mmio + INTEN0);
  670. writel(VAL2 | RDMD0, mmio + CMD0);
  671. spin_unlock_irqrestore(&lp->lock, flags);
  672. }
  673. rx_not_empty:
  674. return num_rx_pkt;
  675. }
  676. /* This function will indicate the link status to the kernel. */
  677. static int amd8111e_link_change(struct net_device *dev)
  678. {
  679. struct amd8111e_priv *lp = netdev_priv(dev);
  680. int status0,speed;
  681. /* read the link change */
  682. status0 = readl(lp->mmio + STAT0);
  683. if(status0 & LINK_STATS){
  684. if(status0 & AUTONEG_COMPLETE)
  685. lp->link_config.autoneg = AUTONEG_ENABLE;
  686. else
  687. lp->link_config.autoneg = AUTONEG_DISABLE;
  688. if(status0 & FULL_DPLX)
  689. lp->link_config.duplex = DUPLEX_FULL;
  690. else
  691. lp->link_config.duplex = DUPLEX_HALF;
  692. speed = (status0 & SPEED_MASK) >> 7;
  693. if(speed == PHY_SPEED_10)
  694. lp->link_config.speed = SPEED_10;
  695. else if(speed == PHY_SPEED_100)
  696. lp->link_config.speed = SPEED_100;
  697. netdev_info(dev, "Link is Up. Speed is %s Mbps %s Duplex\n",
  698. (lp->link_config.speed == SPEED_100) ?
  699. "100" : "10",
  700. (lp->link_config.duplex == DUPLEX_FULL) ?
  701. "Full" : "Half");
  702. netif_carrier_on(dev);
  703. }
  704. else{
  705. lp->link_config.speed = SPEED_INVALID;
  706. lp->link_config.duplex = DUPLEX_INVALID;
  707. lp->link_config.autoneg = AUTONEG_INVALID;
  708. netdev_info(dev, "Link is Down.\n");
  709. netif_carrier_off(dev);
  710. }
  711. return 0;
  712. }
  713. /* This function reads the mib counters. */
  714. static int amd8111e_read_mib(void __iomem *mmio, u8 MIB_COUNTER)
  715. {
  716. unsigned int status;
  717. unsigned int data;
  718. unsigned int repeat = REPEAT_CNT;
  719. writew( MIB_RD_CMD | MIB_COUNTER, mmio + MIB_ADDR);
  720. do {
  721. status = readw(mmio + MIB_ADDR);
  722. udelay(2); /* controller takes MAX 2 us to get mib data */
  723. }
  724. while (--repeat && (status & MIB_CMD_ACTIVE));
  725. data = readl(mmio + MIB_DATA);
  726. return data;
  727. }
  728. /* This function reads the mib registers and returns the hardware statistics.
  729. * It updates previous internal driver statistics with new values.
  730. */
  731. static struct net_device_stats *amd8111e_get_stats(struct net_device *dev)
  732. {
  733. struct amd8111e_priv *lp = netdev_priv(dev);
  734. void __iomem *mmio = lp->mmio;
  735. unsigned long flags;
  736. struct net_device_stats *new_stats = &dev->stats;
  737. if (!lp->opened)
  738. return new_stats;
  739. spin_lock_irqsave (&lp->lock, flags);
  740. /* stats.rx_packets */
  741. new_stats->rx_packets = amd8111e_read_mib(mmio, rcv_broadcast_pkts)+
  742. amd8111e_read_mib(mmio, rcv_multicast_pkts)+
  743. amd8111e_read_mib(mmio, rcv_unicast_pkts);
  744. /* stats.tx_packets */
  745. new_stats->tx_packets = amd8111e_read_mib(mmio, xmt_packets);
  746. /*stats.rx_bytes */
  747. new_stats->rx_bytes = amd8111e_read_mib(mmio, rcv_octets);
  748. /* stats.tx_bytes */
  749. new_stats->tx_bytes = amd8111e_read_mib(mmio, xmt_octets);
  750. /* stats.rx_errors */
  751. /* hw errors + errors driver reported */
  752. new_stats->rx_errors = amd8111e_read_mib(mmio, rcv_undersize_pkts)+
  753. amd8111e_read_mib(mmio, rcv_fragments)+
  754. amd8111e_read_mib(mmio, rcv_jabbers)+
  755. amd8111e_read_mib(mmio, rcv_alignment_errors)+
  756. amd8111e_read_mib(mmio, rcv_fcs_errors)+
  757. amd8111e_read_mib(mmio, rcv_miss_pkts)+
  758. lp->drv_rx_errors;
  759. /* stats.tx_errors */
  760. new_stats->tx_errors = amd8111e_read_mib(mmio, xmt_underrun_pkts);
  761. /* stats.rx_dropped*/
  762. new_stats->rx_dropped = amd8111e_read_mib(mmio, rcv_miss_pkts);
  763. /* stats.tx_dropped*/
  764. new_stats->tx_dropped = amd8111e_read_mib(mmio, xmt_underrun_pkts);
  765. /* stats.multicast*/
  766. new_stats->multicast = amd8111e_read_mib(mmio, rcv_multicast_pkts);
  767. /* stats.collisions*/
  768. new_stats->collisions = amd8111e_read_mib(mmio, xmt_collisions);
  769. /* stats.rx_length_errors*/
  770. new_stats->rx_length_errors =
  771. amd8111e_read_mib(mmio, rcv_undersize_pkts)+
  772. amd8111e_read_mib(mmio, rcv_oversize_pkts);
  773. /* stats.rx_over_errors*/
  774. new_stats->rx_over_errors = amd8111e_read_mib(mmio, rcv_miss_pkts);
  775. /* stats.rx_crc_errors*/
  776. new_stats->rx_crc_errors = amd8111e_read_mib(mmio, rcv_fcs_errors);
  777. /* stats.rx_frame_errors*/
  778. new_stats->rx_frame_errors =
  779. amd8111e_read_mib(mmio, rcv_alignment_errors);
  780. /* stats.rx_fifo_errors */
  781. new_stats->rx_fifo_errors = amd8111e_read_mib(mmio, rcv_miss_pkts);
  782. /* stats.rx_missed_errors */
  783. new_stats->rx_missed_errors = amd8111e_read_mib(mmio, rcv_miss_pkts);
  784. /* stats.tx_aborted_errors*/
  785. new_stats->tx_aborted_errors =
  786. amd8111e_read_mib(mmio, xmt_excessive_collision);
  787. /* stats.tx_carrier_errors*/
  788. new_stats->tx_carrier_errors =
  789. amd8111e_read_mib(mmio, xmt_loss_carrier);
  790. /* stats.tx_fifo_errors*/
  791. new_stats->tx_fifo_errors = amd8111e_read_mib(mmio, xmt_underrun_pkts);
  792. /* stats.tx_window_errors*/
  793. new_stats->tx_window_errors =
  794. amd8111e_read_mib(mmio, xmt_late_collision);
  795. /* Reset the mibs for collecting new statistics */
  796. /* writew(MIB_CLEAR, mmio + MIB_ADDR);*/
  797. spin_unlock_irqrestore (&lp->lock, flags);
  798. return new_stats;
  799. }
  800. /* This function recalculate the interrupt coalescing mode on every interrupt
  801. * according to the datarate and the packet rate.
  802. */
  803. static int amd8111e_calc_coalesce(struct net_device *dev)
  804. {
  805. struct amd8111e_priv *lp = netdev_priv(dev);
  806. struct amd8111e_coalesce_conf *coal_conf = &lp->coal_conf;
  807. int tx_pkt_rate;
  808. int rx_pkt_rate;
  809. int tx_data_rate;
  810. int rx_data_rate;
  811. int rx_pkt_size;
  812. int tx_pkt_size;
  813. tx_pkt_rate = coal_conf->tx_packets - coal_conf->tx_prev_packets;
  814. coal_conf->tx_prev_packets = coal_conf->tx_packets;
  815. tx_data_rate = coal_conf->tx_bytes - coal_conf->tx_prev_bytes;
  816. coal_conf->tx_prev_bytes = coal_conf->tx_bytes;
  817. rx_pkt_rate = coal_conf->rx_packets - coal_conf->rx_prev_packets;
  818. coal_conf->rx_prev_packets = coal_conf->rx_packets;
  819. rx_data_rate = coal_conf->rx_bytes - coal_conf->rx_prev_bytes;
  820. coal_conf->rx_prev_bytes = coal_conf->rx_bytes;
  821. if(rx_pkt_rate < 800){
  822. if(coal_conf->rx_coal_type != NO_COALESCE){
  823. coal_conf->rx_timeout = 0x0;
  824. coal_conf->rx_event_count = 0;
  825. amd8111e_set_coalesce(dev,RX_INTR_COAL);
  826. coal_conf->rx_coal_type = NO_COALESCE;
  827. }
  828. }
  829. else{
  830. rx_pkt_size = rx_data_rate/rx_pkt_rate;
  831. if (rx_pkt_size < 128){
  832. if(coal_conf->rx_coal_type != NO_COALESCE){
  833. coal_conf->rx_timeout = 0;
  834. coal_conf->rx_event_count = 0;
  835. amd8111e_set_coalesce(dev,RX_INTR_COAL);
  836. coal_conf->rx_coal_type = NO_COALESCE;
  837. }
  838. }
  839. else if ( (rx_pkt_size >= 128) && (rx_pkt_size < 512) ){
  840. if(coal_conf->rx_coal_type != LOW_COALESCE){
  841. coal_conf->rx_timeout = 1;
  842. coal_conf->rx_event_count = 4;
  843. amd8111e_set_coalesce(dev,RX_INTR_COAL);
  844. coal_conf->rx_coal_type = LOW_COALESCE;
  845. }
  846. }
  847. else if ((rx_pkt_size >= 512) && (rx_pkt_size < 1024)){
  848. if(coal_conf->rx_coal_type != MEDIUM_COALESCE){
  849. coal_conf->rx_timeout = 1;
  850. coal_conf->rx_event_count = 4;
  851. amd8111e_set_coalesce(dev,RX_INTR_COAL);
  852. coal_conf->rx_coal_type = MEDIUM_COALESCE;
  853. }
  854. }
  855. else if(rx_pkt_size >= 1024){
  856. if(coal_conf->rx_coal_type != HIGH_COALESCE){
  857. coal_conf->rx_timeout = 2;
  858. coal_conf->rx_event_count = 3;
  859. amd8111e_set_coalesce(dev,RX_INTR_COAL);
  860. coal_conf->rx_coal_type = HIGH_COALESCE;
  861. }
  862. }
  863. }
  864. /* NOW FOR TX INTR COALESC */
  865. if(tx_pkt_rate < 800){
  866. if(coal_conf->tx_coal_type != NO_COALESCE){
  867. coal_conf->tx_timeout = 0x0;
  868. coal_conf->tx_event_count = 0;
  869. amd8111e_set_coalesce(dev,TX_INTR_COAL);
  870. coal_conf->tx_coal_type = NO_COALESCE;
  871. }
  872. }
  873. else{
  874. tx_pkt_size = tx_data_rate/tx_pkt_rate;
  875. if (tx_pkt_size < 128){
  876. if(coal_conf->tx_coal_type != NO_COALESCE){
  877. coal_conf->tx_timeout = 0;
  878. coal_conf->tx_event_count = 0;
  879. amd8111e_set_coalesce(dev,TX_INTR_COAL);
  880. coal_conf->tx_coal_type = NO_COALESCE;
  881. }
  882. }
  883. else if ( (tx_pkt_size >= 128) && (tx_pkt_size < 512) ){
  884. if(coal_conf->tx_coal_type != LOW_COALESCE){
  885. coal_conf->tx_timeout = 1;
  886. coal_conf->tx_event_count = 2;
  887. amd8111e_set_coalesce(dev,TX_INTR_COAL);
  888. coal_conf->tx_coal_type = LOW_COALESCE;
  889. }
  890. }
  891. else if ((tx_pkt_size >= 512) && (tx_pkt_size < 1024)){
  892. if(coal_conf->tx_coal_type != MEDIUM_COALESCE){
  893. coal_conf->tx_timeout = 2;
  894. coal_conf->tx_event_count = 5;
  895. amd8111e_set_coalesce(dev,TX_INTR_COAL);
  896. coal_conf->tx_coal_type = MEDIUM_COALESCE;
  897. }
  898. }
  899. else if(tx_pkt_size >= 1024){
  900. if (tx_pkt_size >= 1024){
  901. if(coal_conf->tx_coal_type != HIGH_COALESCE){
  902. coal_conf->tx_timeout = 4;
  903. coal_conf->tx_event_count = 8;
  904. amd8111e_set_coalesce(dev,TX_INTR_COAL);
  905. coal_conf->tx_coal_type = HIGH_COALESCE;
  906. }
  907. }
  908. }
  909. }
  910. return 0;
  911. }
  912. /* This is device interrupt function. It handles transmit,
  913. * receive,link change and hardware timer interrupts.
  914. */
  915. static irqreturn_t amd8111e_interrupt(int irq, void *dev_id)
  916. {
  917. struct net_device *dev = (struct net_device *)dev_id;
  918. struct amd8111e_priv *lp = netdev_priv(dev);
  919. void __iomem *mmio = lp->mmio;
  920. unsigned int intr0, intren0;
  921. unsigned int handled = 1;
  922. if(unlikely(dev == NULL))
  923. return IRQ_NONE;
  924. spin_lock(&lp->lock);
  925. /* disabling interrupt */
  926. writel(INTREN, mmio + CMD0);
  927. /* Read interrupt status */
  928. intr0 = readl(mmio + INT0);
  929. intren0 = readl(mmio + INTEN0);
  930. /* Process all the INT event until INTR bit is clear. */
  931. if (!(intr0 & INTR)){
  932. handled = 0;
  933. goto err_no_interrupt;
  934. }
  935. /* Current driver processes 4 interrupts : RINT,TINT,LCINT,STINT */
  936. writel(intr0, mmio + INT0);
  937. /* Check if Receive Interrupt has occurred. */
  938. if (intr0 & RINT0) {
  939. if (napi_schedule_prep(&lp->napi)) {
  940. /* Disable receive interupts */
  941. writel(RINTEN0, mmio + INTEN0);
  942. /* Schedule a polling routine */
  943. __napi_schedule(&lp->napi);
  944. } else if (intren0 & RINTEN0) {
  945. netdev_dbg(dev, "************Driver bug! interrupt while in poll\n");
  946. /* Fix by disable receive interrupts */
  947. writel(RINTEN0, mmio + INTEN0);
  948. }
  949. }
  950. /* Check if Transmit Interrupt has occurred. */
  951. if (intr0 & TINT0)
  952. amd8111e_tx(dev);
  953. /* Check if Link Change Interrupt has occurred. */
  954. if (intr0 & LCINT)
  955. amd8111e_link_change(dev);
  956. /* Check if Hardware Timer Interrupt has occurred. */
  957. if (intr0 & STINT)
  958. amd8111e_calc_coalesce(dev);
  959. err_no_interrupt:
  960. writel( VAL0 | INTREN,mmio + CMD0);
  961. spin_unlock(&lp->lock);
  962. return IRQ_RETVAL(handled);
  963. }
  964. #ifdef CONFIG_NET_POLL_CONTROLLER
  965. static void amd8111e_poll(struct net_device *dev)
  966. {
  967. unsigned long flags;
  968. local_irq_save(flags);
  969. amd8111e_interrupt(0, dev);
  970. local_irq_restore(flags);
  971. }
  972. #endif
  973. /* This function closes the network interface and updates
  974. * the statistics so that most recent statistics will be
  975. * available after the interface is down.
  976. */
  977. static int amd8111e_close(struct net_device *dev)
  978. {
  979. struct amd8111e_priv *lp = netdev_priv(dev);
  980. netif_stop_queue(dev);
  981. napi_disable(&lp->napi);
  982. spin_lock_irq(&lp->lock);
  983. amd8111e_disable_interrupt(lp);
  984. amd8111e_stop_chip(lp);
  985. /* Free transmit and receive skbs */
  986. amd8111e_free_skbs(lp->amd8111e_net_dev);
  987. netif_carrier_off(lp->amd8111e_net_dev);
  988. /* Delete ipg timer */
  989. if(lp->options & OPTION_DYN_IPG_ENABLE)
  990. del_timer_sync(&lp->ipg_data.ipg_timer);
  991. spin_unlock_irq(&lp->lock);
  992. free_irq(dev->irq, dev);
  993. amd8111e_free_ring(lp);
  994. /* Update the statistics before closing */
  995. amd8111e_get_stats(dev);
  996. lp->opened = 0;
  997. return 0;
  998. }
  999. /* This function opens new interface.It requests irq for the device,
  1000. * initializes the device,buffers and descriptors, and starts the device.
  1001. */
  1002. static int amd8111e_open(struct net_device *dev)
  1003. {
  1004. struct amd8111e_priv *lp = netdev_priv(dev);
  1005. if(dev->irq ==0 || request_irq(dev->irq, amd8111e_interrupt, IRQF_SHARED,
  1006. dev->name, dev))
  1007. return -EAGAIN;
  1008. napi_enable(&lp->napi);
  1009. spin_lock_irq(&lp->lock);
  1010. amd8111e_init_hw_default(lp);
  1011. if(amd8111e_restart(dev)){
  1012. spin_unlock_irq(&lp->lock);
  1013. napi_disable(&lp->napi);
  1014. if (dev->irq)
  1015. free_irq(dev->irq, dev);
  1016. return -ENOMEM;
  1017. }
  1018. /* Start ipg timer */
  1019. if(lp->options & OPTION_DYN_IPG_ENABLE){
  1020. add_timer(&lp->ipg_data.ipg_timer);
  1021. netdev_info(dev, "Dynamic IPG Enabled\n");
  1022. }
  1023. lp->opened = 1;
  1024. spin_unlock_irq(&lp->lock);
  1025. netif_start_queue(dev);
  1026. return 0;
  1027. }
  1028. /* This function checks if there is any transmit descriptors
  1029. * available to queue more packet.
  1030. */
  1031. static int amd8111e_tx_queue_avail(struct amd8111e_priv *lp)
  1032. {
  1033. int tx_index = lp->tx_idx & TX_BUFF_MOD_MASK;
  1034. if (lp->tx_skbuff[tx_index])
  1035. return -1;
  1036. else
  1037. return 0;
  1038. }
  1039. /* This function will queue the transmit packets to the
  1040. * descriptors and will trigger the send operation. It also
  1041. * initializes the transmit descriptors with buffer physical address,
  1042. * byte count, ownership to hardware etc.
  1043. */
  1044. static netdev_tx_t amd8111e_start_xmit(struct sk_buff *skb,
  1045. struct net_device *dev)
  1046. {
  1047. struct amd8111e_priv *lp = netdev_priv(dev);
  1048. int tx_index;
  1049. unsigned long flags;
  1050. spin_lock_irqsave(&lp->lock, flags);
  1051. tx_index = lp->tx_idx & TX_RING_DR_MOD_MASK;
  1052. lp->tx_ring[tx_index].buff_count = cpu_to_le16(skb->len);
  1053. lp->tx_skbuff[tx_index] = skb;
  1054. lp->tx_ring[tx_index].tx_flags = 0;
  1055. #if AMD8111E_VLAN_TAG_USED
  1056. if (skb_vlan_tag_present(skb)) {
  1057. lp->tx_ring[tx_index].tag_ctrl_cmd |=
  1058. cpu_to_le16(TCC_VLAN_INSERT);
  1059. lp->tx_ring[tx_index].tag_ctrl_info =
  1060. cpu_to_le16(skb_vlan_tag_get(skb));
  1061. }
  1062. #endif
  1063. lp->tx_dma_addr[tx_index] =
  1064. pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
  1065. lp->tx_ring[tx_index].buff_phy_addr =
  1066. cpu_to_le32(lp->tx_dma_addr[tx_index]);
  1067. /* Set FCS and LTINT bits */
  1068. wmb();
  1069. lp->tx_ring[tx_index].tx_flags |=
  1070. cpu_to_le16(OWN_BIT | STP_BIT | ENP_BIT|ADD_FCS_BIT|LTINT_BIT);
  1071. lp->tx_idx++;
  1072. /* Trigger an immediate send poll. */
  1073. writel( VAL1 | TDMD0, lp->mmio + CMD0);
  1074. writel( VAL2 | RDMD0,lp->mmio + CMD0);
  1075. if(amd8111e_tx_queue_avail(lp) < 0){
  1076. netif_stop_queue(dev);
  1077. }
  1078. spin_unlock_irqrestore(&lp->lock, flags);
  1079. return NETDEV_TX_OK;
  1080. }
  1081. /* This function returns all the memory mapped registers of the device. */
  1082. static void amd8111e_read_regs(struct amd8111e_priv *lp, u32 *buf)
  1083. {
  1084. void __iomem *mmio = lp->mmio;
  1085. /* Read only necessary registers */
  1086. buf[0] = readl(mmio + XMT_RING_BASE_ADDR0);
  1087. buf[1] = readl(mmio + XMT_RING_LEN0);
  1088. buf[2] = readl(mmio + RCV_RING_BASE_ADDR0);
  1089. buf[3] = readl(mmio + RCV_RING_LEN0);
  1090. buf[4] = readl(mmio + CMD0);
  1091. buf[5] = readl(mmio + CMD2);
  1092. buf[6] = readl(mmio + CMD3);
  1093. buf[7] = readl(mmio + CMD7);
  1094. buf[8] = readl(mmio + INT0);
  1095. buf[9] = readl(mmio + INTEN0);
  1096. buf[10] = readl(mmio + LADRF);
  1097. buf[11] = readl(mmio + LADRF+4);
  1098. buf[12] = readl(mmio + STAT0);
  1099. }
  1100. /* This function sets promiscuos mode, all-multi mode or the multicast address
  1101. * list to the device.
  1102. */
  1103. static void amd8111e_set_multicast_list(struct net_device *dev)
  1104. {
  1105. struct netdev_hw_addr *ha;
  1106. struct amd8111e_priv *lp = netdev_priv(dev);
  1107. u32 mc_filter[2] ;
  1108. int bit_num;
  1109. if(dev->flags & IFF_PROMISC){
  1110. writel( VAL2 | PROM, lp->mmio + CMD2);
  1111. return;
  1112. }
  1113. else
  1114. writel( PROM, lp->mmio + CMD2);
  1115. if (dev->flags & IFF_ALLMULTI ||
  1116. netdev_mc_count(dev) > MAX_FILTER_SIZE) {
  1117. /* get all multicast packet */
  1118. mc_filter[1] = mc_filter[0] = 0xffffffff;
  1119. lp->options |= OPTION_MULTICAST_ENABLE;
  1120. amd8111e_writeq(*(u64 *)mc_filter, lp->mmio + LADRF);
  1121. return;
  1122. }
  1123. if (netdev_mc_empty(dev)) {
  1124. /* get only own packets */
  1125. mc_filter[1] = mc_filter[0] = 0;
  1126. lp->options &= ~OPTION_MULTICAST_ENABLE;
  1127. amd8111e_writeq(*(u64 *)mc_filter, lp->mmio + LADRF);
  1128. /* disable promiscuous mode */
  1129. writel(PROM, lp->mmio + CMD2);
  1130. return;
  1131. }
  1132. /* load all the multicast addresses in the logic filter */
  1133. lp->options |= OPTION_MULTICAST_ENABLE;
  1134. mc_filter[1] = mc_filter[0] = 0;
  1135. netdev_for_each_mc_addr(ha, dev) {
  1136. bit_num = (ether_crc_le(ETH_ALEN, ha->addr) >> 26) & 0x3f;
  1137. mc_filter[bit_num >> 5] |= 1 << (bit_num & 31);
  1138. }
  1139. amd8111e_writeq(*(u64 *)mc_filter, lp->mmio + LADRF);
  1140. /* To eliminate PCI posting bug */
  1141. readl(lp->mmio + CMD2);
  1142. }
  1143. static void amd8111e_get_drvinfo(struct net_device *dev,
  1144. struct ethtool_drvinfo *info)
  1145. {
  1146. struct amd8111e_priv *lp = netdev_priv(dev);
  1147. struct pci_dev *pci_dev = lp->pci_dev;
  1148. strlcpy(info->driver, MODULE_NAME, sizeof(info->driver));
  1149. strlcpy(info->version, MODULE_VERS, sizeof(info->version));
  1150. snprintf(info->fw_version, sizeof(info->fw_version),
  1151. "%u", chip_version);
  1152. strlcpy(info->bus_info, pci_name(pci_dev), sizeof(info->bus_info));
  1153. }
  1154. static int amd8111e_get_regs_len(struct net_device *dev)
  1155. {
  1156. return AMD8111E_REG_DUMP_LEN;
  1157. }
  1158. static void amd8111e_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *buf)
  1159. {
  1160. struct amd8111e_priv *lp = netdev_priv(dev);
  1161. regs->version = 0;
  1162. amd8111e_read_regs(lp, buf);
  1163. }
  1164. static int amd8111e_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  1165. {
  1166. struct amd8111e_priv *lp = netdev_priv(dev);
  1167. spin_lock_irq(&lp->lock);
  1168. mii_ethtool_gset(&lp->mii_if, ecmd);
  1169. spin_unlock_irq(&lp->lock);
  1170. return 0;
  1171. }
  1172. static int amd8111e_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  1173. {
  1174. struct amd8111e_priv *lp = netdev_priv(dev);
  1175. int res;
  1176. spin_lock_irq(&lp->lock);
  1177. res = mii_ethtool_sset(&lp->mii_if, ecmd);
  1178. spin_unlock_irq(&lp->lock);
  1179. return res;
  1180. }
  1181. static int amd8111e_nway_reset(struct net_device *dev)
  1182. {
  1183. struct amd8111e_priv *lp = netdev_priv(dev);
  1184. return mii_nway_restart(&lp->mii_if);
  1185. }
  1186. static u32 amd8111e_get_link(struct net_device *dev)
  1187. {
  1188. struct amd8111e_priv *lp = netdev_priv(dev);
  1189. return mii_link_ok(&lp->mii_if);
  1190. }
  1191. static void amd8111e_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol_info)
  1192. {
  1193. struct amd8111e_priv *lp = netdev_priv(dev);
  1194. wol_info->supported = WAKE_MAGIC|WAKE_PHY;
  1195. if (lp->options & OPTION_WOL_ENABLE)
  1196. wol_info->wolopts = WAKE_MAGIC;
  1197. }
  1198. static int amd8111e_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol_info)
  1199. {
  1200. struct amd8111e_priv *lp = netdev_priv(dev);
  1201. if (wol_info->wolopts & ~(WAKE_MAGIC|WAKE_PHY))
  1202. return -EINVAL;
  1203. spin_lock_irq(&lp->lock);
  1204. if (wol_info->wolopts & WAKE_MAGIC)
  1205. lp->options |=
  1206. (OPTION_WOL_ENABLE | OPTION_WAKE_MAGIC_ENABLE);
  1207. else if(wol_info->wolopts & WAKE_PHY)
  1208. lp->options |=
  1209. (OPTION_WOL_ENABLE | OPTION_WAKE_PHY_ENABLE);
  1210. else
  1211. lp->options &= ~OPTION_WOL_ENABLE;
  1212. spin_unlock_irq(&lp->lock);
  1213. return 0;
  1214. }
  1215. static const struct ethtool_ops ops = {
  1216. .get_drvinfo = amd8111e_get_drvinfo,
  1217. .get_regs_len = amd8111e_get_regs_len,
  1218. .get_regs = amd8111e_get_regs,
  1219. .get_settings = amd8111e_get_settings,
  1220. .set_settings = amd8111e_set_settings,
  1221. .nway_reset = amd8111e_nway_reset,
  1222. .get_link = amd8111e_get_link,
  1223. .get_wol = amd8111e_get_wol,
  1224. .set_wol = amd8111e_set_wol,
  1225. };
  1226. /* This function handles all the ethtool ioctls. It gives driver info,
  1227. * gets/sets driver speed, gets memory mapped register values, forces
  1228. * auto negotiation, sets/gets WOL options for ethtool application.
  1229. */
  1230. static int amd8111e_ioctl(struct net_device *dev , struct ifreq *ifr, int cmd)
  1231. {
  1232. struct mii_ioctl_data *data = if_mii(ifr);
  1233. struct amd8111e_priv *lp = netdev_priv(dev);
  1234. int err;
  1235. u32 mii_regval;
  1236. switch(cmd) {
  1237. case SIOCGMIIPHY:
  1238. data->phy_id = lp->ext_phy_addr;
  1239. /* fallthru */
  1240. case SIOCGMIIREG:
  1241. spin_lock_irq(&lp->lock);
  1242. err = amd8111e_read_phy(lp, data->phy_id,
  1243. data->reg_num & PHY_REG_ADDR_MASK, &mii_regval);
  1244. spin_unlock_irq(&lp->lock);
  1245. data->val_out = mii_regval;
  1246. return err;
  1247. case SIOCSMIIREG:
  1248. spin_lock_irq(&lp->lock);
  1249. err = amd8111e_write_phy(lp, data->phy_id,
  1250. data->reg_num & PHY_REG_ADDR_MASK, data->val_in);
  1251. spin_unlock_irq(&lp->lock);
  1252. return err;
  1253. default:
  1254. /* do nothing */
  1255. break;
  1256. }
  1257. return -EOPNOTSUPP;
  1258. }
  1259. static int amd8111e_set_mac_address(struct net_device *dev, void *p)
  1260. {
  1261. struct amd8111e_priv *lp = netdev_priv(dev);
  1262. int i;
  1263. struct sockaddr *addr = p;
  1264. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1265. spin_lock_irq(&lp->lock);
  1266. /* Setting the MAC address to the device */
  1267. for (i = 0; i < ETH_ALEN; i++)
  1268. writeb( dev->dev_addr[i], lp->mmio + PADR + i );
  1269. spin_unlock_irq(&lp->lock);
  1270. return 0;
  1271. }
  1272. /* This function changes the mtu of the device. It restarts the device to
  1273. * initialize the descriptor with new receive buffers.
  1274. */
  1275. static int amd8111e_change_mtu(struct net_device *dev, int new_mtu)
  1276. {
  1277. struct amd8111e_priv *lp = netdev_priv(dev);
  1278. int err;
  1279. if ((new_mtu < AMD8111E_MIN_MTU) || (new_mtu > AMD8111E_MAX_MTU))
  1280. return -EINVAL;
  1281. if (!netif_running(dev)) {
  1282. /* new_mtu will be used
  1283. * when device starts netxt time
  1284. */
  1285. dev->mtu = new_mtu;
  1286. return 0;
  1287. }
  1288. spin_lock_irq(&lp->lock);
  1289. /* stop the chip */
  1290. writel(RUN, lp->mmio + CMD0);
  1291. dev->mtu = new_mtu;
  1292. err = amd8111e_restart(dev);
  1293. spin_unlock_irq(&lp->lock);
  1294. if(!err)
  1295. netif_start_queue(dev);
  1296. return err;
  1297. }
  1298. static int amd8111e_enable_magicpkt(struct amd8111e_priv *lp)
  1299. {
  1300. writel( VAL1|MPPLBA, lp->mmio + CMD3);
  1301. writel( VAL0|MPEN_SW, lp->mmio + CMD7);
  1302. /* To eliminate PCI posting bug */
  1303. readl(lp->mmio + CMD7);
  1304. return 0;
  1305. }
  1306. static int amd8111e_enable_link_change(struct amd8111e_priv *lp)
  1307. {
  1308. /* Adapter is already stoped/suspended/interrupt-disabled */
  1309. writel(VAL0|LCMODE_SW,lp->mmio + CMD7);
  1310. /* To eliminate PCI posting bug */
  1311. readl(lp->mmio + CMD7);
  1312. return 0;
  1313. }
  1314. /* This function is called when a packet transmission fails to complete
  1315. * within a reasonable period, on the assumption that an interrupt have
  1316. * failed or the interface is locked up. This function will reinitialize
  1317. * the hardware.
  1318. */
  1319. static void amd8111e_tx_timeout(struct net_device *dev)
  1320. {
  1321. struct amd8111e_priv *lp = netdev_priv(dev);
  1322. int err;
  1323. netdev_err(dev, "transmit timed out, resetting\n");
  1324. spin_lock_irq(&lp->lock);
  1325. err = amd8111e_restart(dev);
  1326. spin_unlock_irq(&lp->lock);
  1327. if(!err)
  1328. netif_wake_queue(dev);
  1329. }
  1330. static int amd8111e_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1331. {
  1332. struct net_device *dev = pci_get_drvdata(pci_dev);
  1333. struct amd8111e_priv *lp = netdev_priv(dev);
  1334. if (!netif_running(dev))
  1335. return 0;
  1336. /* disable the interrupt */
  1337. spin_lock_irq(&lp->lock);
  1338. amd8111e_disable_interrupt(lp);
  1339. spin_unlock_irq(&lp->lock);
  1340. netif_device_detach(dev);
  1341. /* stop chip */
  1342. spin_lock_irq(&lp->lock);
  1343. if(lp->options & OPTION_DYN_IPG_ENABLE)
  1344. del_timer_sync(&lp->ipg_data.ipg_timer);
  1345. amd8111e_stop_chip(lp);
  1346. spin_unlock_irq(&lp->lock);
  1347. if(lp->options & OPTION_WOL_ENABLE){
  1348. /* enable wol */
  1349. if(lp->options & OPTION_WAKE_MAGIC_ENABLE)
  1350. amd8111e_enable_magicpkt(lp);
  1351. if(lp->options & OPTION_WAKE_PHY_ENABLE)
  1352. amd8111e_enable_link_change(lp);
  1353. pci_enable_wake(pci_dev, PCI_D3hot, 1);
  1354. pci_enable_wake(pci_dev, PCI_D3cold, 1);
  1355. }
  1356. else{
  1357. pci_enable_wake(pci_dev, PCI_D3hot, 0);
  1358. pci_enable_wake(pci_dev, PCI_D3cold, 0);
  1359. }
  1360. pci_save_state(pci_dev);
  1361. pci_set_power_state(pci_dev, PCI_D3hot);
  1362. return 0;
  1363. }
  1364. static int amd8111e_resume(struct pci_dev *pci_dev)
  1365. {
  1366. struct net_device *dev = pci_get_drvdata(pci_dev);
  1367. struct amd8111e_priv *lp = netdev_priv(dev);
  1368. if (!netif_running(dev))
  1369. return 0;
  1370. pci_set_power_state(pci_dev, PCI_D0);
  1371. pci_restore_state(pci_dev);
  1372. pci_enable_wake(pci_dev, PCI_D3hot, 0);
  1373. pci_enable_wake(pci_dev, PCI_D3cold, 0); /* D3 cold */
  1374. netif_device_attach(dev);
  1375. spin_lock_irq(&lp->lock);
  1376. amd8111e_restart(dev);
  1377. /* Restart ipg timer */
  1378. if(lp->options & OPTION_DYN_IPG_ENABLE)
  1379. mod_timer(&lp->ipg_data.ipg_timer,
  1380. jiffies + IPG_CONVERGE_JIFFIES);
  1381. spin_unlock_irq(&lp->lock);
  1382. return 0;
  1383. }
  1384. static void amd8111e_config_ipg(struct net_device *dev)
  1385. {
  1386. struct amd8111e_priv *lp = netdev_priv(dev);
  1387. struct ipg_info *ipg_data = &lp->ipg_data;
  1388. void __iomem *mmio = lp->mmio;
  1389. unsigned int prev_col_cnt = ipg_data->col_cnt;
  1390. unsigned int total_col_cnt;
  1391. unsigned int tmp_ipg;
  1392. if(lp->link_config.duplex == DUPLEX_FULL){
  1393. ipg_data->ipg = DEFAULT_IPG;
  1394. return;
  1395. }
  1396. if(ipg_data->ipg_state == SSTATE){
  1397. if(ipg_data->timer_tick == IPG_STABLE_TIME){
  1398. ipg_data->timer_tick = 0;
  1399. ipg_data->ipg = MIN_IPG - IPG_STEP;
  1400. ipg_data->current_ipg = MIN_IPG;
  1401. ipg_data->diff_col_cnt = 0xFFFFFFFF;
  1402. ipg_data->ipg_state = CSTATE;
  1403. }
  1404. else
  1405. ipg_data->timer_tick++;
  1406. }
  1407. if(ipg_data->ipg_state == CSTATE){
  1408. /* Get the current collision count */
  1409. total_col_cnt = ipg_data->col_cnt =
  1410. amd8111e_read_mib(mmio, xmt_collisions);
  1411. if ((total_col_cnt - prev_col_cnt) <
  1412. (ipg_data->diff_col_cnt)){
  1413. ipg_data->diff_col_cnt =
  1414. total_col_cnt - prev_col_cnt ;
  1415. ipg_data->ipg = ipg_data->current_ipg;
  1416. }
  1417. ipg_data->current_ipg += IPG_STEP;
  1418. if (ipg_data->current_ipg <= MAX_IPG)
  1419. tmp_ipg = ipg_data->current_ipg;
  1420. else{
  1421. tmp_ipg = ipg_data->ipg;
  1422. ipg_data->ipg_state = SSTATE;
  1423. }
  1424. writew((u32)tmp_ipg, mmio + IPG);
  1425. writew((u32)(tmp_ipg - IFS1_DELTA), mmio + IFS1);
  1426. }
  1427. mod_timer(&lp->ipg_data.ipg_timer, jiffies + IPG_CONVERGE_JIFFIES);
  1428. return;
  1429. }
  1430. static void amd8111e_probe_ext_phy(struct net_device *dev)
  1431. {
  1432. struct amd8111e_priv *lp = netdev_priv(dev);
  1433. int i;
  1434. for (i = 0x1e; i >= 0; i--) {
  1435. u32 id1, id2;
  1436. if (amd8111e_read_phy(lp, i, MII_PHYSID1, &id1))
  1437. continue;
  1438. if (amd8111e_read_phy(lp, i, MII_PHYSID2, &id2))
  1439. continue;
  1440. lp->ext_phy_id = (id1 << 16) | id2;
  1441. lp->ext_phy_addr = i;
  1442. return;
  1443. }
  1444. lp->ext_phy_id = 0;
  1445. lp->ext_phy_addr = 1;
  1446. }
  1447. static const struct net_device_ops amd8111e_netdev_ops = {
  1448. .ndo_open = amd8111e_open,
  1449. .ndo_stop = amd8111e_close,
  1450. .ndo_start_xmit = amd8111e_start_xmit,
  1451. .ndo_tx_timeout = amd8111e_tx_timeout,
  1452. .ndo_get_stats = amd8111e_get_stats,
  1453. .ndo_set_rx_mode = amd8111e_set_multicast_list,
  1454. .ndo_validate_addr = eth_validate_addr,
  1455. .ndo_set_mac_address = amd8111e_set_mac_address,
  1456. .ndo_do_ioctl = amd8111e_ioctl,
  1457. .ndo_change_mtu = amd8111e_change_mtu,
  1458. #ifdef CONFIG_NET_POLL_CONTROLLER
  1459. .ndo_poll_controller = amd8111e_poll,
  1460. #endif
  1461. };
  1462. static int amd8111e_probe_one(struct pci_dev *pdev,
  1463. const struct pci_device_id *ent)
  1464. {
  1465. int err, i;
  1466. unsigned long reg_addr,reg_len;
  1467. struct amd8111e_priv *lp;
  1468. struct net_device *dev;
  1469. err = pci_enable_device(pdev);
  1470. if(err){
  1471. dev_err(&pdev->dev, "Cannot enable new PCI device\n");
  1472. return err;
  1473. }
  1474. if(!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)){
  1475. dev_err(&pdev->dev, "Cannot find PCI base address\n");
  1476. err = -ENODEV;
  1477. goto err_disable_pdev;
  1478. }
  1479. err = pci_request_regions(pdev, MODULE_NAME);
  1480. if(err){
  1481. dev_err(&pdev->dev, "Cannot obtain PCI resources\n");
  1482. goto err_disable_pdev;
  1483. }
  1484. pci_set_master(pdev);
  1485. /* Find power-management capability. */
  1486. if (!pdev->pm_cap) {
  1487. dev_err(&pdev->dev, "No Power Management capability\n");
  1488. err = -ENODEV;
  1489. goto err_free_reg;
  1490. }
  1491. /* Initialize DMA */
  1492. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) < 0) {
  1493. dev_err(&pdev->dev, "DMA not supported\n");
  1494. err = -ENODEV;
  1495. goto err_free_reg;
  1496. }
  1497. reg_addr = pci_resource_start(pdev, 0);
  1498. reg_len = pci_resource_len(pdev, 0);
  1499. dev = alloc_etherdev(sizeof(struct amd8111e_priv));
  1500. if (!dev) {
  1501. err = -ENOMEM;
  1502. goto err_free_reg;
  1503. }
  1504. SET_NETDEV_DEV(dev, &pdev->dev);
  1505. #if AMD8111E_VLAN_TAG_USED
  1506. dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX ;
  1507. #endif
  1508. lp = netdev_priv(dev);
  1509. lp->pci_dev = pdev;
  1510. lp->amd8111e_net_dev = dev;
  1511. lp->pm_cap = pdev->pm_cap;
  1512. spin_lock_init(&lp->lock);
  1513. lp->mmio = devm_ioremap(&pdev->dev, reg_addr, reg_len);
  1514. if (!lp->mmio) {
  1515. dev_err(&pdev->dev, "Cannot map device registers\n");
  1516. err = -ENOMEM;
  1517. goto err_free_dev;
  1518. }
  1519. /* Initializing MAC address */
  1520. for (i = 0; i < ETH_ALEN; i++)
  1521. dev->dev_addr[i] = readb(lp->mmio + PADR + i);
  1522. /* Setting user defined parametrs */
  1523. lp->ext_phy_option = speed_duplex[card_idx];
  1524. if(coalesce[card_idx])
  1525. lp->options |= OPTION_INTR_COAL_ENABLE;
  1526. if(dynamic_ipg[card_idx++])
  1527. lp->options |= OPTION_DYN_IPG_ENABLE;
  1528. /* Initialize driver entry points */
  1529. dev->netdev_ops = &amd8111e_netdev_ops;
  1530. dev->ethtool_ops = &ops;
  1531. dev->irq =pdev->irq;
  1532. dev->watchdog_timeo = AMD8111E_TX_TIMEOUT;
  1533. netif_napi_add(dev, &lp->napi, amd8111e_rx_poll, 32);
  1534. #if AMD8111E_VLAN_TAG_USED
  1535. dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  1536. #endif
  1537. /* Probe the external PHY */
  1538. amd8111e_probe_ext_phy(dev);
  1539. /* setting mii default values */
  1540. lp->mii_if.dev = dev;
  1541. lp->mii_if.mdio_read = amd8111e_mdio_read;
  1542. lp->mii_if.mdio_write = amd8111e_mdio_write;
  1543. lp->mii_if.phy_id = lp->ext_phy_addr;
  1544. /* Set receive buffer length and set jumbo option*/
  1545. amd8111e_set_rx_buff_len(dev);
  1546. err = register_netdev(dev);
  1547. if (err) {
  1548. dev_err(&pdev->dev, "Cannot register net device\n");
  1549. goto err_free_dev;
  1550. }
  1551. pci_set_drvdata(pdev, dev);
  1552. /* Initialize software ipg timer */
  1553. if(lp->options & OPTION_DYN_IPG_ENABLE){
  1554. init_timer(&lp->ipg_data.ipg_timer);
  1555. lp->ipg_data.ipg_timer.data = (unsigned long) dev;
  1556. lp->ipg_data.ipg_timer.function = (void *)&amd8111e_config_ipg;
  1557. lp->ipg_data.ipg_timer.expires = jiffies +
  1558. IPG_CONVERGE_JIFFIES;
  1559. lp->ipg_data.ipg = DEFAULT_IPG;
  1560. lp->ipg_data.ipg_state = CSTATE;
  1561. }
  1562. /* display driver and device information */
  1563. chip_version = (readl(lp->mmio + CHIPID) & 0xf0000000)>>28;
  1564. dev_info(&pdev->dev, "AMD-8111e Driver Version: %s\n", MODULE_VERS);
  1565. dev_info(&pdev->dev, "[ Rev %x ] PCI 10/100BaseT Ethernet %pM\n",
  1566. chip_version, dev->dev_addr);
  1567. if (lp->ext_phy_id)
  1568. dev_info(&pdev->dev, "Found MII PHY ID 0x%08x at address 0x%02x\n",
  1569. lp->ext_phy_id, lp->ext_phy_addr);
  1570. else
  1571. dev_info(&pdev->dev, "Couldn't detect MII PHY, assuming address 0x01\n");
  1572. return 0;
  1573. err_free_dev:
  1574. free_netdev(dev);
  1575. err_free_reg:
  1576. pci_release_regions(pdev);
  1577. err_disable_pdev:
  1578. pci_disable_device(pdev);
  1579. return err;
  1580. }
  1581. static void amd8111e_remove_one(struct pci_dev *pdev)
  1582. {
  1583. struct net_device *dev = pci_get_drvdata(pdev);
  1584. if (dev) {
  1585. unregister_netdev(dev);
  1586. free_netdev(dev);
  1587. pci_release_regions(pdev);
  1588. pci_disable_device(pdev);
  1589. }
  1590. }
  1591. static const struct pci_device_id amd8111e_pci_tbl[] = {
  1592. {
  1593. .vendor = PCI_VENDOR_ID_AMD,
  1594. .device = PCI_DEVICE_ID_AMD8111E_7462,
  1595. },
  1596. {
  1597. .vendor = 0,
  1598. }
  1599. };
  1600. MODULE_DEVICE_TABLE(pci, amd8111e_pci_tbl);
  1601. static struct pci_driver amd8111e_driver = {
  1602. .name = MODULE_NAME,
  1603. .id_table = amd8111e_pci_tbl,
  1604. .probe = amd8111e_probe_one,
  1605. .remove = amd8111e_remove_one,
  1606. .suspend = amd8111e_suspend,
  1607. .resume = amd8111e_resume
  1608. };
  1609. module_pci_driver(amd8111e_driver);