amd8111e.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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 defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  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);