pcnet32.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764
  1. /* pcnet32.c: An AMD PCnet32 ethernet driver for linux. */
  2. /*
  3. * Copyright 1996-1999 Thomas Bogendoerfer
  4. *
  5. * Derived from the lance driver written 1993,1994,1995 by Donald Becker.
  6. *
  7. * Copyright 1993 United States Government as represented by the
  8. * Director, National Security Agency.
  9. *
  10. * This software may be used and distributed according to the terms
  11. * of the GNU General Public License, incorporated herein by reference.
  12. *
  13. * This driver is for PCnet32 and PCnetPCI based ethercards
  14. */
  15. /**************************************************************************
  16. * 23 Oct, 2000.
  17. * Fixed a few bugs, related to running the controller in 32bit mode.
  18. *
  19. * Carsten Langgaard, carstenl@mips.com
  20. * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
  21. *
  22. *************************************************************************/
  23. #define DRV_NAME "pcnet32"
  24. #define DRV_VERSION "1.32"
  25. #define DRV_RELDATE "18.Mar.2006"
  26. #define PFX DRV_NAME ": "
  27. static const char *const version =
  28. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " tsbogend@alpha.franken.de\n";
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/string.h>
  32. #include <linux/errno.h>
  33. #include <linux/ioport.h>
  34. #include <linux/slab.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/pci.h>
  37. #include <linux/delay.h>
  38. #include <linux/init.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/mii.h>
  41. #include <linux/crc32.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/etherdevice.h>
  44. #include <linux/skbuff.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/moduleparam.h>
  47. #include <linux/bitops.h>
  48. #include <asm/dma.h>
  49. #include <asm/io.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/irq.h>
  52. /*
  53. * PCI device identifiers for "new style" Linux PCI Device Drivers
  54. */
  55. static struct pci_device_id pcnet32_pci_tbl[] = {
  56. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE_HOME), },
  57. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE), },
  58. /*
  59. * Adapters that were sold with IBM's RS/6000 or pSeries hardware have
  60. * the incorrect vendor id.
  61. */
  62. { PCI_DEVICE(PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_AMD_LANCE),
  63. .class = (PCI_CLASS_NETWORK_ETHERNET << 8), .class_mask = 0xffff00, },
  64. { } /* terminate list */
  65. };
  66. MODULE_DEVICE_TABLE(pci, pcnet32_pci_tbl);
  67. static int cards_found;
  68. /*
  69. * VLB I/O addresses
  70. */
  71. static unsigned int pcnet32_portlist[] __initdata =
  72. { 0x300, 0x320, 0x340, 0x360, 0 };
  73. static int pcnet32_debug = 0;
  74. static int tx_start = 1; /* Mapping -- 0:20, 1:64, 2:128, 3:~220 (depends on chip vers) */
  75. static int pcnet32vlb; /* check for VLB cards ? */
  76. static struct net_device *pcnet32_dev;
  77. static int max_interrupt_work = 2;
  78. static int rx_copybreak = 200;
  79. #define PCNET32_PORT_AUI 0x00
  80. #define PCNET32_PORT_10BT 0x01
  81. #define PCNET32_PORT_GPSI 0x02
  82. #define PCNET32_PORT_MII 0x03
  83. #define PCNET32_PORT_PORTSEL 0x03
  84. #define PCNET32_PORT_ASEL 0x04
  85. #define PCNET32_PORT_100 0x40
  86. #define PCNET32_PORT_FD 0x80
  87. #define PCNET32_DMA_MASK 0xffffffff
  88. #define PCNET32_WATCHDOG_TIMEOUT (jiffies + (2 * HZ))
  89. #define PCNET32_BLINK_TIMEOUT (jiffies + (HZ/4))
  90. /*
  91. * table to translate option values from tulip
  92. * to internal options
  93. */
  94. static const unsigned char options_mapping[] = {
  95. PCNET32_PORT_ASEL, /* 0 Auto-select */
  96. PCNET32_PORT_AUI, /* 1 BNC/AUI */
  97. PCNET32_PORT_AUI, /* 2 AUI/BNC */
  98. PCNET32_PORT_ASEL, /* 3 not supported */
  99. PCNET32_PORT_10BT | PCNET32_PORT_FD, /* 4 10baseT-FD */
  100. PCNET32_PORT_ASEL, /* 5 not supported */
  101. PCNET32_PORT_ASEL, /* 6 not supported */
  102. PCNET32_PORT_ASEL, /* 7 not supported */
  103. PCNET32_PORT_ASEL, /* 8 not supported */
  104. PCNET32_PORT_MII, /* 9 MII 10baseT */
  105. PCNET32_PORT_MII | PCNET32_PORT_FD, /* 10 MII 10baseT-FD */
  106. PCNET32_PORT_MII, /* 11 MII (autosel) */
  107. PCNET32_PORT_10BT, /* 12 10BaseT */
  108. PCNET32_PORT_MII | PCNET32_PORT_100, /* 13 MII 100BaseTx */
  109. /* 14 MII 100BaseTx-FD */
  110. PCNET32_PORT_MII | PCNET32_PORT_100 | PCNET32_PORT_FD,
  111. PCNET32_PORT_ASEL /* 15 not supported */
  112. };
  113. static const char pcnet32_gstrings_test[][ETH_GSTRING_LEN] = {
  114. "Loopback test (offline)"
  115. };
  116. #define PCNET32_TEST_LEN (sizeof(pcnet32_gstrings_test) / ETH_GSTRING_LEN)
  117. #define PCNET32_NUM_REGS 136
  118. #define MAX_UNITS 8 /* More are supported, limit only on options */
  119. static int options[MAX_UNITS];
  120. static int full_duplex[MAX_UNITS];
  121. static int homepna[MAX_UNITS];
  122. /*
  123. * Theory of Operation
  124. *
  125. * This driver uses the same software structure as the normal lance
  126. * driver. So look for a verbose description in lance.c. The differences
  127. * to the normal lance driver is the use of the 32bit mode of PCnet32
  128. * and PCnetPCI chips. Because these chips are 32bit chips, there is no
  129. * 16MB limitation and we don't need bounce buffers.
  130. */
  131. /*
  132. * Set the number of Tx and Rx buffers, using Log_2(# buffers).
  133. * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
  134. * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
  135. */
  136. #ifndef PCNET32_LOG_TX_BUFFERS
  137. #define PCNET32_LOG_TX_BUFFERS 4
  138. #define PCNET32_LOG_RX_BUFFERS 5
  139. #define PCNET32_LOG_MAX_TX_BUFFERS 9 /* 2^9 == 512 */
  140. #define PCNET32_LOG_MAX_RX_BUFFERS 9
  141. #endif
  142. #define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
  143. #define TX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_TX_BUFFERS))
  144. #define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
  145. #define RX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_RX_BUFFERS))
  146. #define PKT_BUF_SZ 1544
  147. /* Offsets from base I/O address. */
  148. #define PCNET32_WIO_RDP 0x10
  149. #define PCNET32_WIO_RAP 0x12
  150. #define PCNET32_WIO_RESET 0x14
  151. #define PCNET32_WIO_BDP 0x16
  152. #define PCNET32_DWIO_RDP 0x10
  153. #define PCNET32_DWIO_RAP 0x14
  154. #define PCNET32_DWIO_RESET 0x18
  155. #define PCNET32_DWIO_BDP 0x1C
  156. #define PCNET32_TOTAL_SIZE 0x20
  157. /* The PCNET32 Rx and Tx ring descriptors. */
  158. struct pcnet32_rx_head {
  159. u32 base;
  160. s16 buf_length;
  161. s16 status;
  162. u32 msg_length;
  163. u32 reserved;
  164. };
  165. struct pcnet32_tx_head {
  166. u32 base;
  167. s16 length;
  168. s16 status;
  169. u32 misc;
  170. u32 reserved;
  171. };
  172. /* The PCNET32 32-Bit initialization block, described in databook. */
  173. struct pcnet32_init_block {
  174. u16 mode;
  175. u16 tlen_rlen;
  176. u8 phys_addr[6];
  177. u16 reserved;
  178. u32 filter[2];
  179. /* Receive and transmit ring base, along with extra bits. */
  180. u32 rx_ring;
  181. u32 tx_ring;
  182. };
  183. /* PCnet32 access functions */
  184. struct pcnet32_access {
  185. u16 (*read_csr) (unsigned long, int);
  186. void (*write_csr) (unsigned long, int, u16);
  187. u16 (*read_bcr) (unsigned long, int);
  188. void (*write_bcr) (unsigned long, int, u16);
  189. u16 (*read_rap) (unsigned long);
  190. void (*write_rap) (unsigned long, u16);
  191. void (*reset) (unsigned long);
  192. };
  193. /*
  194. * The first field of pcnet32_private is read by the ethernet device
  195. * so the structure should be allocated using pci_alloc_consistent().
  196. */
  197. struct pcnet32_private {
  198. struct pcnet32_init_block init_block;
  199. /* The Tx and Rx ring entries must be aligned on 16-byte boundaries in 32bit mode. */
  200. struct pcnet32_rx_head *rx_ring;
  201. struct pcnet32_tx_head *tx_ring;
  202. dma_addr_t dma_addr;/* DMA address of beginning of this
  203. object, returned by pci_alloc_consistent */
  204. struct pci_dev *pci_dev;
  205. const char *name;
  206. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  207. struct sk_buff **tx_skbuff;
  208. struct sk_buff **rx_skbuff;
  209. dma_addr_t *tx_dma_addr;
  210. dma_addr_t *rx_dma_addr;
  211. struct pcnet32_access a;
  212. spinlock_t lock; /* Guard lock */
  213. unsigned int cur_rx, cur_tx; /* The next free ring entry */
  214. unsigned int rx_ring_size; /* current rx ring size */
  215. unsigned int tx_ring_size; /* current tx ring size */
  216. unsigned int rx_mod_mask; /* rx ring modular mask */
  217. unsigned int tx_mod_mask; /* tx ring modular mask */
  218. unsigned short rx_len_bits;
  219. unsigned short tx_len_bits;
  220. dma_addr_t rx_ring_dma_addr;
  221. dma_addr_t tx_ring_dma_addr;
  222. unsigned int dirty_rx, /* ring entries to be freed. */
  223. dirty_tx;
  224. struct net_device_stats stats;
  225. char tx_full;
  226. char phycount; /* number of phys found */
  227. int options;
  228. unsigned int shared_irq:1, /* shared irq possible */
  229. dxsuflo:1, /* disable transmit stop on uflo */
  230. mii:1; /* mii port available */
  231. struct net_device *next;
  232. struct mii_if_info mii_if;
  233. struct timer_list watchdog_timer;
  234. struct timer_list blink_timer;
  235. u32 msg_enable; /* debug message level */
  236. /* each bit indicates an available PHY */
  237. u32 phymask;
  238. };
  239. static int pcnet32_probe_pci(struct pci_dev *, const struct pci_device_id *);
  240. static int pcnet32_probe1(unsigned long, int, struct pci_dev *);
  241. static int pcnet32_open(struct net_device *);
  242. static int pcnet32_init_ring(struct net_device *);
  243. static int pcnet32_start_xmit(struct sk_buff *, struct net_device *);
  244. static int pcnet32_rx(struct net_device *);
  245. static void pcnet32_tx_timeout(struct net_device *dev);
  246. static irqreturn_t pcnet32_interrupt(int, void *, struct pt_regs *);
  247. static int pcnet32_close(struct net_device *);
  248. static struct net_device_stats *pcnet32_get_stats(struct net_device *);
  249. static void pcnet32_load_multicast(struct net_device *dev);
  250. static void pcnet32_set_multicast_list(struct net_device *);
  251. static int pcnet32_ioctl(struct net_device *, struct ifreq *, int);
  252. static void pcnet32_watchdog(struct net_device *);
  253. static int mdio_read(struct net_device *dev, int phy_id, int reg_num);
  254. static void mdio_write(struct net_device *dev, int phy_id, int reg_num,
  255. int val);
  256. static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits);
  257. static void pcnet32_ethtool_test(struct net_device *dev,
  258. struct ethtool_test *eth_test, u64 * data);
  259. static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1);
  260. static int pcnet32_phys_id(struct net_device *dev, u32 data);
  261. static void pcnet32_led_blink_callback(struct net_device *dev);
  262. static int pcnet32_get_regs_len(struct net_device *dev);
  263. static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  264. void *ptr);
  265. static void pcnet32_purge_tx_ring(struct net_device *dev);
  266. static int pcnet32_alloc_ring(struct net_device *dev, char *name);
  267. static void pcnet32_free_ring(struct net_device *dev);
  268. static void pcnet32_check_media(struct net_device *dev, int verbose);
  269. static u16 pcnet32_wio_read_csr(unsigned long addr, int index)
  270. {
  271. outw(index, addr + PCNET32_WIO_RAP);
  272. return inw(addr + PCNET32_WIO_RDP);
  273. }
  274. static void pcnet32_wio_write_csr(unsigned long addr, int index, u16 val)
  275. {
  276. outw(index, addr + PCNET32_WIO_RAP);
  277. outw(val, addr + PCNET32_WIO_RDP);
  278. }
  279. static u16 pcnet32_wio_read_bcr(unsigned long addr, int index)
  280. {
  281. outw(index, addr + PCNET32_WIO_RAP);
  282. return inw(addr + PCNET32_WIO_BDP);
  283. }
  284. static void pcnet32_wio_write_bcr(unsigned long addr, int index, u16 val)
  285. {
  286. outw(index, addr + PCNET32_WIO_RAP);
  287. outw(val, addr + PCNET32_WIO_BDP);
  288. }
  289. static u16 pcnet32_wio_read_rap(unsigned long addr)
  290. {
  291. return inw(addr + PCNET32_WIO_RAP);
  292. }
  293. static void pcnet32_wio_write_rap(unsigned long addr, u16 val)
  294. {
  295. outw(val, addr + PCNET32_WIO_RAP);
  296. }
  297. static void pcnet32_wio_reset(unsigned long addr)
  298. {
  299. inw(addr + PCNET32_WIO_RESET);
  300. }
  301. static int pcnet32_wio_check(unsigned long addr)
  302. {
  303. outw(88, addr + PCNET32_WIO_RAP);
  304. return (inw(addr + PCNET32_WIO_RAP) == 88);
  305. }
  306. static struct pcnet32_access pcnet32_wio = {
  307. .read_csr = pcnet32_wio_read_csr,
  308. .write_csr = pcnet32_wio_write_csr,
  309. .read_bcr = pcnet32_wio_read_bcr,
  310. .write_bcr = pcnet32_wio_write_bcr,
  311. .read_rap = pcnet32_wio_read_rap,
  312. .write_rap = pcnet32_wio_write_rap,
  313. .reset = pcnet32_wio_reset
  314. };
  315. static u16 pcnet32_dwio_read_csr(unsigned long addr, int index)
  316. {
  317. outl(index, addr + PCNET32_DWIO_RAP);
  318. return (inl(addr + PCNET32_DWIO_RDP) & 0xffff);
  319. }
  320. static void pcnet32_dwio_write_csr(unsigned long addr, int index, u16 val)
  321. {
  322. outl(index, addr + PCNET32_DWIO_RAP);
  323. outl(val, addr + PCNET32_DWIO_RDP);
  324. }
  325. static u16 pcnet32_dwio_read_bcr(unsigned long addr, int index)
  326. {
  327. outl(index, addr + PCNET32_DWIO_RAP);
  328. return (inl(addr + PCNET32_DWIO_BDP) & 0xffff);
  329. }
  330. static void pcnet32_dwio_write_bcr(unsigned long addr, int index, u16 val)
  331. {
  332. outl(index, addr + PCNET32_DWIO_RAP);
  333. outl(val, addr + PCNET32_DWIO_BDP);
  334. }
  335. static u16 pcnet32_dwio_read_rap(unsigned long addr)
  336. {
  337. return (inl(addr + PCNET32_DWIO_RAP) & 0xffff);
  338. }
  339. static void pcnet32_dwio_write_rap(unsigned long addr, u16 val)
  340. {
  341. outl(val, addr + PCNET32_DWIO_RAP);
  342. }
  343. static void pcnet32_dwio_reset(unsigned long addr)
  344. {
  345. inl(addr + PCNET32_DWIO_RESET);
  346. }
  347. static int pcnet32_dwio_check(unsigned long addr)
  348. {
  349. outl(88, addr + PCNET32_DWIO_RAP);
  350. return ((inl(addr + PCNET32_DWIO_RAP) & 0xffff) == 88);
  351. }
  352. static struct pcnet32_access pcnet32_dwio = {
  353. .read_csr = pcnet32_dwio_read_csr,
  354. .write_csr = pcnet32_dwio_write_csr,
  355. .read_bcr = pcnet32_dwio_read_bcr,
  356. .write_bcr = pcnet32_dwio_write_bcr,
  357. .read_rap = pcnet32_dwio_read_rap,
  358. .write_rap = pcnet32_dwio_write_rap,
  359. .reset = pcnet32_dwio_reset
  360. };
  361. #ifdef CONFIG_NET_POLL_CONTROLLER
  362. static void pcnet32_poll_controller(struct net_device *dev)
  363. {
  364. disable_irq(dev->irq);
  365. pcnet32_interrupt(0, dev, NULL);
  366. enable_irq(dev->irq);
  367. }
  368. #endif
  369. static int pcnet32_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  370. {
  371. struct pcnet32_private *lp = dev->priv;
  372. unsigned long flags;
  373. int r = -EOPNOTSUPP;
  374. if (lp->mii) {
  375. spin_lock_irqsave(&lp->lock, flags);
  376. mii_ethtool_gset(&lp->mii_if, cmd);
  377. spin_unlock_irqrestore(&lp->lock, flags);
  378. r = 0;
  379. }
  380. return r;
  381. }
  382. static int pcnet32_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  383. {
  384. struct pcnet32_private *lp = dev->priv;
  385. unsigned long flags;
  386. int r = -EOPNOTSUPP;
  387. if (lp->mii) {
  388. spin_lock_irqsave(&lp->lock, flags);
  389. r = mii_ethtool_sset(&lp->mii_if, cmd);
  390. spin_unlock_irqrestore(&lp->lock, flags);
  391. }
  392. return r;
  393. }
  394. static void pcnet32_get_drvinfo(struct net_device *dev,
  395. struct ethtool_drvinfo *info)
  396. {
  397. struct pcnet32_private *lp = dev->priv;
  398. strcpy(info->driver, DRV_NAME);
  399. strcpy(info->version, DRV_VERSION);
  400. if (lp->pci_dev)
  401. strcpy(info->bus_info, pci_name(lp->pci_dev));
  402. else
  403. sprintf(info->bus_info, "VLB 0x%lx", dev->base_addr);
  404. }
  405. static u32 pcnet32_get_link(struct net_device *dev)
  406. {
  407. struct pcnet32_private *lp = dev->priv;
  408. unsigned long flags;
  409. int r;
  410. spin_lock_irqsave(&lp->lock, flags);
  411. if (lp->mii) {
  412. r = mii_link_ok(&lp->mii_if);
  413. } else {
  414. ulong ioaddr = dev->base_addr; /* card base I/O address */
  415. r = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
  416. }
  417. spin_unlock_irqrestore(&lp->lock, flags);
  418. return r;
  419. }
  420. static u32 pcnet32_get_msglevel(struct net_device *dev)
  421. {
  422. struct pcnet32_private *lp = dev->priv;
  423. return lp->msg_enable;
  424. }
  425. static void pcnet32_set_msglevel(struct net_device *dev, u32 value)
  426. {
  427. struct pcnet32_private *lp = dev->priv;
  428. lp->msg_enable = value;
  429. }
  430. static int pcnet32_nway_reset(struct net_device *dev)
  431. {
  432. struct pcnet32_private *lp = dev->priv;
  433. unsigned long flags;
  434. int r = -EOPNOTSUPP;
  435. if (lp->mii) {
  436. spin_lock_irqsave(&lp->lock, flags);
  437. r = mii_nway_restart(&lp->mii_if);
  438. spin_unlock_irqrestore(&lp->lock, flags);
  439. }
  440. return r;
  441. }
  442. static void pcnet32_get_ringparam(struct net_device *dev,
  443. struct ethtool_ringparam *ering)
  444. {
  445. struct pcnet32_private *lp = dev->priv;
  446. ering->tx_max_pending = TX_MAX_RING_SIZE - 1;
  447. ering->tx_pending = lp->tx_ring_size - 1;
  448. ering->rx_max_pending = RX_MAX_RING_SIZE - 1;
  449. ering->rx_pending = lp->rx_ring_size - 1;
  450. }
  451. static int pcnet32_set_ringparam(struct net_device *dev,
  452. struct ethtool_ringparam *ering)
  453. {
  454. struct pcnet32_private *lp = dev->priv;
  455. unsigned long flags;
  456. int i;
  457. if (ering->rx_mini_pending || ering->rx_jumbo_pending)
  458. return -EINVAL;
  459. if (netif_running(dev))
  460. pcnet32_close(dev);
  461. spin_lock_irqsave(&lp->lock, flags);
  462. pcnet32_free_ring(dev);
  463. lp->tx_ring_size =
  464. min(ering->tx_pending, (unsigned int)TX_MAX_RING_SIZE);
  465. lp->rx_ring_size =
  466. min(ering->rx_pending, (unsigned int)RX_MAX_RING_SIZE);
  467. /* set the minimum ring size to 4, to allow the loopback test to work
  468. * unchanged.
  469. */
  470. for (i = 2; i <= PCNET32_LOG_MAX_TX_BUFFERS; i++) {
  471. if (lp->tx_ring_size <= (1 << i))
  472. break;
  473. }
  474. lp->tx_ring_size = (1 << i);
  475. lp->tx_mod_mask = lp->tx_ring_size - 1;
  476. lp->tx_len_bits = (i << 12);
  477. for (i = 2; i <= PCNET32_LOG_MAX_RX_BUFFERS; i++) {
  478. if (lp->rx_ring_size <= (1 << i))
  479. break;
  480. }
  481. lp->rx_ring_size = (1 << i);
  482. lp->rx_mod_mask = lp->rx_ring_size - 1;
  483. lp->rx_len_bits = (i << 4);
  484. if (pcnet32_alloc_ring(dev, dev->name)) {
  485. pcnet32_free_ring(dev);
  486. spin_unlock_irqrestore(&lp->lock, flags);
  487. return -ENOMEM;
  488. }
  489. spin_unlock_irqrestore(&lp->lock, flags);
  490. if (pcnet32_debug & NETIF_MSG_DRV)
  491. printk(KERN_INFO PFX
  492. "%s: Ring Param Settings: RX: %d, TX: %d\n", dev->name,
  493. lp->rx_ring_size, lp->tx_ring_size);
  494. if (netif_running(dev))
  495. pcnet32_open(dev);
  496. return 0;
  497. }
  498. static void pcnet32_get_strings(struct net_device *dev, u32 stringset,
  499. u8 * data)
  500. {
  501. memcpy(data, pcnet32_gstrings_test, sizeof(pcnet32_gstrings_test));
  502. }
  503. static int pcnet32_self_test_count(struct net_device *dev)
  504. {
  505. return PCNET32_TEST_LEN;
  506. }
  507. static void pcnet32_ethtool_test(struct net_device *dev,
  508. struct ethtool_test *test, u64 * data)
  509. {
  510. struct pcnet32_private *lp = dev->priv;
  511. int rc;
  512. if (test->flags == ETH_TEST_FL_OFFLINE) {
  513. rc = pcnet32_loopback_test(dev, data);
  514. if (rc) {
  515. if (netif_msg_hw(lp))
  516. printk(KERN_DEBUG "%s: Loopback test failed.\n",
  517. dev->name);
  518. test->flags |= ETH_TEST_FL_FAILED;
  519. } else if (netif_msg_hw(lp))
  520. printk(KERN_DEBUG "%s: Loopback test passed.\n",
  521. dev->name);
  522. } else if (netif_msg_hw(lp))
  523. printk(KERN_DEBUG
  524. "%s: No tests to run (specify 'Offline' on ethtool).",
  525. dev->name);
  526. } /* end pcnet32_ethtool_test */
  527. static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1)
  528. {
  529. struct pcnet32_private *lp = dev->priv;
  530. struct pcnet32_access *a = &lp->a; /* access to registers */
  531. ulong ioaddr = dev->base_addr; /* card base I/O address */
  532. struct sk_buff *skb; /* sk buff */
  533. int x, i; /* counters */
  534. int numbuffs = 4; /* number of TX/RX buffers and descs */
  535. u16 status = 0x8300; /* TX ring status */
  536. u16 teststatus; /* test of ring status */
  537. int rc; /* return code */
  538. int size; /* size of packets */
  539. unsigned char *packet; /* source packet data */
  540. static const int data_len = 60; /* length of source packets */
  541. unsigned long flags;
  542. unsigned long ticks;
  543. *data1 = 1; /* status of test, default to fail */
  544. rc = 1; /* default to fail */
  545. if (netif_running(dev))
  546. pcnet32_close(dev);
  547. spin_lock_irqsave(&lp->lock, flags);
  548. /* Reset the PCNET32 */
  549. lp->a.reset(ioaddr);
  550. /* switch pcnet32 to 32bit mode */
  551. lp->a.write_bcr(ioaddr, 20, 2);
  552. lp->init_block.mode =
  553. le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  554. lp->init_block.filter[0] = 0;
  555. lp->init_block.filter[1] = 0;
  556. /* purge & init rings but don't actually restart */
  557. pcnet32_restart(dev, 0x0000);
  558. lp->a.write_csr(ioaddr, 0, 0x0004); /* Set STOP bit */
  559. /* Initialize Transmit buffers. */
  560. size = data_len + 15;
  561. for (x = 0; x < numbuffs; x++) {
  562. if (!(skb = dev_alloc_skb(size))) {
  563. if (netif_msg_hw(lp))
  564. printk(KERN_DEBUG
  565. "%s: Cannot allocate skb at line: %d!\n",
  566. dev->name, __LINE__);
  567. goto clean_up;
  568. } else {
  569. packet = skb->data;
  570. skb_put(skb, size); /* create space for data */
  571. lp->tx_skbuff[x] = skb;
  572. lp->tx_ring[x].length = le16_to_cpu(-skb->len);
  573. lp->tx_ring[x].misc = 0;
  574. /* put DA and SA into the skb */
  575. for (i = 0; i < 6; i++)
  576. *packet++ = dev->dev_addr[i];
  577. for (i = 0; i < 6; i++)
  578. *packet++ = dev->dev_addr[i];
  579. /* type */
  580. *packet++ = 0x08;
  581. *packet++ = 0x06;
  582. /* packet number */
  583. *packet++ = x;
  584. /* fill packet with data */
  585. for (i = 0; i < data_len; i++)
  586. *packet++ = i;
  587. lp->tx_dma_addr[x] =
  588. pci_map_single(lp->pci_dev, skb->data, skb->len,
  589. PCI_DMA_TODEVICE);
  590. lp->tx_ring[x].base =
  591. (u32) le32_to_cpu(lp->tx_dma_addr[x]);
  592. wmb(); /* Make sure owner changes after all others are visible */
  593. lp->tx_ring[x].status = le16_to_cpu(status);
  594. }
  595. }
  596. x = a->read_bcr(ioaddr, 32); /* set internal loopback in BSR32 */
  597. x = x | 0x0002;
  598. a->write_bcr(ioaddr, 32, x);
  599. lp->a.write_csr(ioaddr, 15, 0x0044); /* set int loopback in CSR15 */
  600. teststatus = le16_to_cpu(0x8000);
  601. lp->a.write_csr(ioaddr, 0, 0x0002); /* Set STRT bit */
  602. /* Check status of descriptors */
  603. for (x = 0; x < numbuffs; x++) {
  604. ticks = 0;
  605. rmb();
  606. while ((lp->rx_ring[x].status & teststatus) && (ticks < 200)) {
  607. spin_unlock_irqrestore(&lp->lock, flags);
  608. mdelay(1);
  609. spin_lock_irqsave(&lp->lock, flags);
  610. rmb();
  611. ticks++;
  612. }
  613. if (ticks == 200) {
  614. if (netif_msg_hw(lp))
  615. printk("%s: Desc %d failed to reset!\n",
  616. dev->name, x);
  617. break;
  618. }
  619. }
  620. lp->a.write_csr(ioaddr, 0, 0x0004); /* Set STOP bit */
  621. wmb();
  622. if (netif_msg_hw(lp) && netif_msg_pktdata(lp)) {
  623. printk(KERN_DEBUG "%s: RX loopback packets:\n", dev->name);
  624. for (x = 0; x < numbuffs; x++) {
  625. printk(KERN_DEBUG "%s: Packet %d:\n", dev->name, x);
  626. skb = lp->rx_skbuff[x];
  627. for (i = 0; i < size; i++) {
  628. printk("%02x ", *(skb->data + i));
  629. }
  630. printk("\n");
  631. }
  632. }
  633. x = 0;
  634. rc = 0;
  635. while (x < numbuffs && !rc) {
  636. skb = lp->rx_skbuff[x];
  637. packet = lp->tx_skbuff[x]->data;
  638. for (i = 0; i < size; i++) {
  639. if (*(skb->data + i) != packet[i]) {
  640. if (netif_msg_hw(lp))
  641. printk(KERN_DEBUG
  642. "%s: Error in compare! %2x - %02x %02x\n",
  643. dev->name, i, *(skb->data + i),
  644. packet[i]);
  645. rc = 1;
  646. break;
  647. }
  648. }
  649. x++;
  650. }
  651. if (!rc) {
  652. *data1 = 0;
  653. }
  654. clean_up:
  655. pcnet32_purge_tx_ring(dev);
  656. x = a->read_csr(ioaddr, 15) & 0xFFFF;
  657. a->write_csr(ioaddr, 15, (x & ~0x0044)); /* reset bits 6 and 2 */
  658. x = a->read_bcr(ioaddr, 32); /* reset internal loopback */
  659. x = x & ~0x0002;
  660. a->write_bcr(ioaddr, 32, x);
  661. spin_unlock_irqrestore(&lp->lock, flags);
  662. if (netif_running(dev)) {
  663. pcnet32_open(dev);
  664. } else {
  665. lp->a.write_bcr(ioaddr, 20, 4); /* return to 16bit mode */
  666. }
  667. return (rc);
  668. } /* end pcnet32_loopback_test */
  669. static void pcnet32_led_blink_callback(struct net_device *dev)
  670. {
  671. struct pcnet32_private *lp = dev->priv;
  672. struct pcnet32_access *a = &lp->a;
  673. ulong ioaddr = dev->base_addr;
  674. unsigned long flags;
  675. int i;
  676. spin_lock_irqsave(&lp->lock, flags);
  677. for (i = 4; i < 8; i++) {
  678. a->write_bcr(ioaddr, i, a->read_bcr(ioaddr, i) ^ 0x4000);
  679. }
  680. spin_unlock_irqrestore(&lp->lock, flags);
  681. mod_timer(&lp->blink_timer, PCNET32_BLINK_TIMEOUT);
  682. }
  683. static int pcnet32_phys_id(struct net_device *dev, u32 data)
  684. {
  685. struct pcnet32_private *lp = dev->priv;
  686. struct pcnet32_access *a = &lp->a;
  687. ulong ioaddr = dev->base_addr;
  688. unsigned long flags;
  689. int i, regs[4];
  690. if (!lp->blink_timer.function) {
  691. init_timer(&lp->blink_timer);
  692. lp->blink_timer.function = (void *)pcnet32_led_blink_callback;
  693. lp->blink_timer.data = (unsigned long)dev;
  694. }
  695. /* Save the current value of the bcrs */
  696. spin_lock_irqsave(&lp->lock, flags);
  697. for (i = 4; i < 8; i++) {
  698. regs[i - 4] = a->read_bcr(ioaddr, i);
  699. }
  700. spin_unlock_irqrestore(&lp->lock, flags);
  701. mod_timer(&lp->blink_timer, jiffies);
  702. set_current_state(TASK_INTERRUPTIBLE);
  703. if ((!data) || (data > (u32) (MAX_SCHEDULE_TIMEOUT / HZ)))
  704. data = (u32) (MAX_SCHEDULE_TIMEOUT / HZ);
  705. msleep_interruptible(data * 1000);
  706. del_timer_sync(&lp->blink_timer);
  707. /* Restore the original value of the bcrs */
  708. spin_lock_irqsave(&lp->lock, flags);
  709. for (i = 4; i < 8; i++) {
  710. a->write_bcr(ioaddr, i, regs[i - 4]);
  711. }
  712. spin_unlock_irqrestore(&lp->lock, flags);
  713. return 0;
  714. }
  715. #define PCNET32_REGS_PER_PHY 32
  716. #define PCNET32_MAX_PHYS 32
  717. static int pcnet32_get_regs_len(struct net_device *dev)
  718. {
  719. struct pcnet32_private *lp = dev->priv;
  720. int j = lp->phycount * PCNET32_REGS_PER_PHY;
  721. return ((PCNET32_NUM_REGS + j) * sizeof(u16));
  722. }
  723. static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  724. void *ptr)
  725. {
  726. int i, csr0;
  727. u16 *buff = ptr;
  728. struct pcnet32_private *lp = dev->priv;
  729. struct pcnet32_access *a = &lp->a;
  730. ulong ioaddr = dev->base_addr;
  731. int ticks;
  732. unsigned long flags;
  733. spin_lock_irqsave(&lp->lock, flags);
  734. csr0 = a->read_csr(ioaddr, 0);
  735. if (!(csr0 & 0x0004)) { /* If not stopped */
  736. /* set SUSPEND (SPND) - CSR5 bit 0 */
  737. a->write_csr(ioaddr, 5, 0x0001);
  738. /* poll waiting for bit to be set */
  739. ticks = 0;
  740. while (!(a->read_csr(ioaddr, 5) & 0x0001)) {
  741. spin_unlock_irqrestore(&lp->lock, flags);
  742. mdelay(1);
  743. spin_lock_irqsave(&lp->lock, flags);
  744. ticks++;
  745. if (ticks > 200) {
  746. if (netif_msg_hw(lp))
  747. printk(KERN_DEBUG
  748. "%s: Error getting into suspend!\n",
  749. dev->name);
  750. break;
  751. }
  752. }
  753. }
  754. /* read address PROM */
  755. for (i = 0; i < 16; i += 2)
  756. *buff++ = inw(ioaddr + i);
  757. /* read control and status registers */
  758. for (i = 0; i < 90; i++) {
  759. *buff++ = a->read_csr(ioaddr, i);
  760. }
  761. *buff++ = a->read_csr(ioaddr, 112);
  762. *buff++ = a->read_csr(ioaddr, 114);
  763. /* read bus configuration registers */
  764. for (i = 0; i < 30; i++) {
  765. *buff++ = a->read_bcr(ioaddr, i);
  766. }
  767. *buff++ = 0; /* skip bcr30 so as not to hang 79C976 */
  768. for (i = 31; i < 36; i++) {
  769. *buff++ = a->read_bcr(ioaddr, i);
  770. }
  771. /* read mii phy registers */
  772. if (lp->mii) {
  773. int j;
  774. for (j = 0; j < PCNET32_MAX_PHYS; j++) {
  775. if (lp->phymask & (1 << j)) {
  776. for (i = 0; i < PCNET32_REGS_PER_PHY; i++) {
  777. lp->a.write_bcr(ioaddr, 33,
  778. (j << 5) | i);
  779. *buff++ = lp->a.read_bcr(ioaddr, 34);
  780. }
  781. }
  782. }
  783. }
  784. if (!(csr0 & 0x0004)) { /* If not stopped */
  785. /* clear SUSPEND (SPND) - CSR5 bit 0 */
  786. a->write_csr(ioaddr, 5, 0x0000);
  787. }
  788. spin_unlock_irqrestore(&lp->lock, flags);
  789. }
  790. static struct ethtool_ops pcnet32_ethtool_ops = {
  791. .get_settings = pcnet32_get_settings,
  792. .set_settings = pcnet32_set_settings,
  793. .get_drvinfo = pcnet32_get_drvinfo,
  794. .get_msglevel = pcnet32_get_msglevel,
  795. .set_msglevel = pcnet32_set_msglevel,
  796. .nway_reset = pcnet32_nway_reset,
  797. .get_link = pcnet32_get_link,
  798. .get_ringparam = pcnet32_get_ringparam,
  799. .set_ringparam = pcnet32_set_ringparam,
  800. .get_tx_csum = ethtool_op_get_tx_csum,
  801. .get_sg = ethtool_op_get_sg,
  802. .get_tso = ethtool_op_get_tso,
  803. .get_strings = pcnet32_get_strings,
  804. .self_test_count = pcnet32_self_test_count,
  805. .self_test = pcnet32_ethtool_test,
  806. .phys_id = pcnet32_phys_id,
  807. .get_regs_len = pcnet32_get_regs_len,
  808. .get_regs = pcnet32_get_regs,
  809. .get_perm_addr = ethtool_op_get_perm_addr,
  810. };
  811. /* only probes for non-PCI devices, the rest are handled by
  812. * pci_register_driver via pcnet32_probe_pci */
  813. static void __devinit pcnet32_probe_vlbus(unsigned int *pcnet32_portlist)
  814. {
  815. unsigned int *port, ioaddr;
  816. /* search for PCnet32 VLB cards at known addresses */
  817. for (port = pcnet32_portlist; (ioaddr = *port); port++) {
  818. if (request_region
  819. (ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_vlbus")) {
  820. /* check if there is really a pcnet chip on that ioaddr */
  821. if ((inb(ioaddr + 14) == 0x57)
  822. && (inb(ioaddr + 15) == 0x57)) {
  823. pcnet32_probe1(ioaddr, 0, NULL);
  824. } else {
  825. release_region(ioaddr, PCNET32_TOTAL_SIZE);
  826. }
  827. }
  828. }
  829. }
  830. static int __devinit
  831. pcnet32_probe_pci(struct pci_dev *pdev, const struct pci_device_id *ent)
  832. {
  833. unsigned long ioaddr;
  834. int err;
  835. err = pci_enable_device(pdev);
  836. if (err < 0) {
  837. if (pcnet32_debug & NETIF_MSG_PROBE)
  838. printk(KERN_ERR PFX
  839. "failed to enable device -- err=%d\n", err);
  840. return err;
  841. }
  842. pci_set_master(pdev);
  843. ioaddr = pci_resource_start(pdev, 0);
  844. if (!ioaddr) {
  845. if (pcnet32_debug & NETIF_MSG_PROBE)
  846. printk(KERN_ERR PFX
  847. "card has no PCI IO resources, aborting\n");
  848. return -ENODEV;
  849. }
  850. if (!pci_dma_supported(pdev, PCNET32_DMA_MASK)) {
  851. if (pcnet32_debug & NETIF_MSG_PROBE)
  852. printk(KERN_ERR PFX
  853. "architecture does not support 32bit PCI busmaster DMA\n");
  854. return -ENODEV;
  855. }
  856. if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_pci") ==
  857. NULL) {
  858. if (pcnet32_debug & NETIF_MSG_PROBE)
  859. printk(KERN_ERR PFX
  860. "io address range already allocated\n");
  861. return -EBUSY;
  862. }
  863. err = pcnet32_probe1(ioaddr, 1, pdev);
  864. if (err < 0) {
  865. pci_disable_device(pdev);
  866. }
  867. return err;
  868. }
  869. /* pcnet32_probe1
  870. * Called from both pcnet32_probe_vlbus and pcnet_probe_pci.
  871. * pdev will be NULL when called from pcnet32_probe_vlbus.
  872. */
  873. static int __devinit
  874. pcnet32_probe1(unsigned long ioaddr, int shared, struct pci_dev *pdev)
  875. {
  876. struct pcnet32_private *lp;
  877. dma_addr_t lp_dma_addr;
  878. int i, media;
  879. int fdx, mii, fset, dxsuflo;
  880. int chip_version;
  881. char *chipname;
  882. struct net_device *dev;
  883. struct pcnet32_access *a = NULL;
  884. u8 promaddr[6];
  885. int ret = -ENODEV;
  886. /* reset the chip */
  887. pcnet32_wio_reset(ioaddr);
  888. /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
  889. if (pcnet32_wio_read_csr(ioaddr, 0) == 4 && pcnet32_wio_check(ioaddr)) {
  890. a = &pcnet32_wio;
  891. } else {
  892. pcnet32_dwio_reset(ioaddr);
  893. if (pcnet32_dwio_read_csr(ioaddr, 0) == 4
  894. && pcnet32_dwio_check(ioaddr)) {
  895. a = &pcnet32_dwio;
  896. } else
  897. goto err_release_region;
  898. }
  899. chip_version =
  900. a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr, 89) << 16);
  901. if ((pcnet32_debug & NETIF_MSG_PROBE) && (pcnet32_debug & NETIF_MSG_HW))
  902. printk(KERN_INFO " PCnet chip version is %#x.\n",
  903. chip_version);
  904. if ((chip_version & 0xfff) != 0x003) {
  905. if (pcnet32_debug & NETIF_MSG_PROBE)
  906. printk(KERN_INFO PFX "Unsupported chip version.\n");
  907. goto err_release_region;
  908. }
  909. /* initialize variables */
  910. fdx = mii = fset = dxsuflo = 0;
  911. chip_version = (chip_version >> 12) & 0xffff;
  912. switch (chip_version) {
  913. case 0x2420:
  914. chipname = "PCnet/PCI 79C970"; /* PCI */
  915. break;
  916. case 0x2430:
  917. if (shared)
  918. chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
  919. else
  920. chipname = "PCnet/32 79C965"; /* 486/VL bus */
  921. break;
  922. case 0x2621:
  923. chipname = "PCnet/PCI II 79C970A"; /* PCI */
  924. fdx = 1;
  925. break;
  926. case 0x2623:
  927. chipname = "PCnet/FAST 79C971"; /* PCI */
  928. fdx = 1;
  929. mii = 1;
  930. fset = 1;
  931. break;
  932. case 0x2624:
  933. chipname = "PCnet/FAST+ 79C972"; /* PCI */
  934. fdx = 1;
  935. mii = 1;
  936. fset = 1;
  937. break;
  938. case 0x2625:
  939. chipname = "PCnet/FAST III 79C973"; /* PCI */
  940. fdx = 1;
  941. mii = 1;
  942. break;
  943. case 0x2626:
  944. chipname = "PCnet/Home 79C978"; /* PCI */
  945. fdx = 1;
  946. /*
  947. * This is based on specs published at www.amd.com. This section
  948. * assumes that a card with a 79C978 wants to go into standard
  949. * ethernet mode. The 79C978 can also go into 1Mb HomePNA mode,
  950. * and the module option homepna=1 can select this instead.
  951. */
  952. media = a->read_bcr(ioaddr, 49);
  953. media &= ~3; /* default to 10Mb ethernet */
  954. if (cards_found < MAX_UNITS && homepna[cards_found])
  955. media |= 1; /* switch to home wiring mode */
  956. if (pcnet32_debug & NETIF_MSG_PROBE)
  957. printk(KERN_DEBUG PFX "media set to %sMbit mode.\n",
  958. (media & 1) ? "1" : "10");
  959. a->write_bcr(ioaddr, 49, media);
  960. break;
  961. case 0x2627:
  962. chipname = "PCnet/FAST III 79C975"; /* PCI */
  963. fdx = 1;
  964. mii = 1;
  965. break;
  966. case 0x2628:
  967. chipname = "PCnet/PRO 79C976";
  968. fdx = 1;
  969. mii = 1;
  970. break;
  971. default:
  972. if (pcnet32_debug & NETIF_MSG_PROBE)
  973. printk(KERN_INFO PFX
  974. "PCnet version %#x, no PCnet32 chip.\n",
  975. chip_version);
  976. goto err_release_region;
  977. }
  978. /*
  979. * On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
  980. * starting until the packet is loaded. Strike one for reliability, lose
  981. * one for latency - although on PCI this isnt a big loss. Older chips
  982. * have FIFO's smaller than a packet, so you can't do this.
  983. * Turn on BCR18:BurstRdEn and BCR18:BurstWrEn.
  984. */
  985. if (fset) {
  986. a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0860));
  987. a->write_csr(ioaddr, 80,
  988. (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
  989. dxsuflo = 1;
  990. }
  991. dev = alloc_etherdev(0);
  992. if (!dev) {
  993. if (pcnet32_debug & NETIF_MSG_PROBE)
  994. printk(KERN_ERR PFX "Memory allocation failed.\n");
  995. ret = -ENOMEM;
  996. goto err_release_region;
  997. }
  998. SET_NETDEV_DEV(dev, &pdev->dev);
  999. if (pcnet32_debug & NETIF_MSG_PROBE)
  1000. printk(KERN_INFO PFX "%s at %#3lx,", chipname, ioaddr);
  1001. /* In most chips, after a chip reset, the ethernet address is read from the
  1002. * station address PROM at the base address and programmed into the
  1003. * "Physical Address Registers" CSR12-14.
  1004. * As a precautionary measure, we read the PROM values and complain if
  1005. * they disagree with the CSRs. If they miscompare, and the PROM addr
  1006. * is valid, then the PROM addr is used.
  1007. */
  1008. for (i = 0; i < 3; i++) {
  1009. unsigned int val;
  1010. val = a->read_csr(ioaddr, i + 12) & 0x0ffff;
  1011. /* There may be endianness issues here. */
  1012. dev->dev_addr[2 * i] = val & 0x0ff;
  1013. dev->dev_addr[2 * i + 1] = (val >> 8) & 0x0ff;
  1014. }
  1015. /* read PROM address and compare with CSR address */
  1016. for (i = 0; i < 6; i++)
  1017. promaddr[i] = inb(ioaddr + i);
  1018. if (memcmp(promaddr, dev->dev_addr, 6)
  1019. || !is_valid_ether_addr(dev->dev_addr)) {
  1020. if (is_valid_ether_addr(promaddr)) {
  1021. if (pcnet32_debug & NETIF_MSG_PROBE) {
  1022. printk(" warning: CSR address invalid,\n");
  1023. printk(KERN_INFO
  1024. " using instead PROM address of");
  1025. }
  1026. memcpy(dev->dev_addr, promaddr, 6);
  1027. }
  1028. }
  1029. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  1030. /* if the ethernet address is not valid, force to 00:00:00:00:00:00 */
  1031. if (!is_valid_ether_addr(dev->perm_addr))
  1032. memset(dev->dev_addr, 0, sizeof(dev->dev_addr));
  1033. if (pcnet32_debug & NETIF_MSG_PROBE) {
  1034. for (i = 0; i < 6; i++)
  1035. printk(" %2.2x", dev->dev_addr[i]);
  1036. /* Version 0x2623 and 0x2624 */
  1037. if (((chip_version + 1) & 0xfffe) == 0x2624) {
  1038. i = a->read_csr(ioaddr, 80) & 0x0C00; /* Check tx_start_pt */
  1039. printk("\n" KERN_INFO " tx_start_pt(0x%04x):", i);
  1040. switch (i >> 10) {
  1041. case 0:
  1042. printk(" 20 bytes,");
  1043. break;
  1044. case 1:
  1045. printk(" 64 bytes,");
  1046. break;
  1047. case 2:
  1048. printk(" 128 bytes,");
  1049. break;
  1050. case 3:
  1051. printk("~220 bytes,");
  1052. break;
  1053. }
  1054. i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
  1055. printk(" BCR18(%x):", i & 0xffff);
  1056. if (i & (1 << 5))
  1057. printk("BurstWrEn ");
  1058. if (i & (1 << 6))
  1059. printk("BurstRdEn ");
  1060. if (i & (1 << 7))
  1061. printk("DWordIO ");
  1062. if (i & (1 << 11))
  1063. printk("NoUFlow ");
  1064. i = a->read_bcr(ioaddr, 25);
  1065. printk("\n" KERN_INFO " SRAMSIZE=0x%04x,", i << 8);
  1066. i = a->read_bcr(ioaddr, 26);
  1067. printk(" SRAM_BND=0x%04x,", i << 8);
  1068. i = a->read_bcr(ioaddr, 27);
  1069. if (i & (1 << 14))
  1070. printk("LowLatRx");
  1071. }
  1072. }
  1073. dev->base_addr = ioaddr;
  1074. /* pci_alloc_consistent returns page-aligned memory, so we do not have to check the alignment */
  1075. if ((lp =
  1076. pci_alloc_consistent(pdev, sizeof(*lp), &lp_dma_addr)) == NULL) {
  1077. if (pcnet32_debug & NETIF_MSG_PROBE)
  1078. printk(KERN_ERR PFX
  1079. "Consistent memory allocation failed.\n");
  1080. ret = -ENOMEM;
  1081. goto err_free_netdev;
  1082. }
  1083. memset(lp, 0, sizeof(*lp));
  1084. lp->dma_addr = lp_dma_addr;
  1085. lp->pci_dev = pdev;
  1086. spin_lock_init(&lp->lock);
  1087. SET_MODULE_OWNER(dev);
  1088. SET_NETDEV_DEV(dev, &pdev->dev);
  1089. dev->priv = lp;
  1090. lp->name = chipname;
  1091. lp->shared_irq = shared;
  1092. lp->tx_ring_size = TX_RING_SIZE; /* default tx ring size */
  1093. lp->rx_ring_size = RX_RING_SIZE; /* default rx ring size */
  1094. lp->tx_mod_mask = lp->tx_ring_size - 1;
  1095. lp->rx_mod_mask = lp->rx_ring_size - 1;
  1096. lp->tx_len_bits = (PCNET32_LOG_TX_BUFFERS << 12);
  1097. lp->rx_len_bits = (PCNET32_LOG_RX_BUFFERS << 4);
  1098. lp->mii_if.full_duplex = fdx;
  1099. lp->mii_if.phy_id_mask = 0x1f;
  1100. lp->mii_if.reg_num_mask = 0x1f;
  1101. lp->dxsuflo = dxsuflo;
  1102. lp->mii = mii;
  1103. lp->msg_enable = pcnet32_debug;
  1104. if ((cards_found >= MAX_UNITS)
  1105. || (options[cards_found] > sizeof(options_mapping)))
  1106. lp->options = PCNET32_PORT_ASEL;
  1107. else
  1108. lp->options = options_mapping[options[cards_found]];
  1109. lp->mii_if.dev = dev;
  1110. lp->mii_if.mdio_read = mdio_read;
  1111. lp->mii_if.mdio_write = mdio_write;
  1112. if (fdx && !(lp->options & PCNET32_PORT_ASEL) &&
  1113. ((cards_found >= MAX_UNITS) || full_duplex[cards_found]))
  1114. lp->options |= PCNET32_PORT_FD;
  1115. if (!a) {
  1116. if (pcnet32_debug & NETIF_MSG_PROBE)
  1117. printk(KERN_ERR PFX "No access methods\n");
  1118. ret = -ENODEV;
  1119. goto err_free_consistent;
  1120. }
  1121. lp->a = *a;
  1122. /* prior to register_netdev, dev->name is not yet correct */
  1123. if (pcnet32_alloc_ring(dev, pci_name(lp->pci_dev))) {
  1124. ret = -ENOMEM;
  1125. goto err_free_ring;
  1126. }
  1127. /* detect special T1/E1 WAN card by checking for MAC address */
  1128. if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0
  1129. && dev->dev_addr[2] == 0x75)
  1130. lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
  1131. lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
  1132. lp->init_block.tlen_rlen =
  1133. le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
  1134. for (i = 0; i < 6; i++)
  1135. lp->init_block.phys_addr[i] = dev->dev_addr[i];
  1136. lp->init_block.filter[0] = 0x00000000;
  1137. lp->init_block.filter[1] = 0x00000000;
  1138. lp->init_block.rx_ring = (u32) le32_to_cpu(lp->rx_ring_dma_addr);
  1139. lp->init_block.tx_ring = (u32) le32_to_cpu(lp->tx_ring_dma_addr);
  1140. /* switch pcnet32 to 32bit mode */
  1141. a->write_bcr(ioaddr, 20, 2);
  1142. a->write_csr(ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private,
  1143. init_block)) & 0xffff);
  1144. a->write_csr(ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private,
  1145. init_block)) >> 16);
  1146. if (pdev) { /* use the IRQ provided by PCI */
  1147. dev->irq = pdev->irq;
  1148. if (pcnet32_debug & NETIF_MSG_PROBE)
  1149. printk(" assigned IRQ %d.\n", dev->irq);
  1150. } else {
  1151. unsigned long irq_mask = probe_irq_on();
  1152. /*
  1153. * To auto-IRQ we enable the initialization-done and DMA error
  1154. * interrupts. For ISA boards we get a DMA error, but VLB and PCI
  1155. * boards will work.
  1156. */
  1157. /* Trigger an initialization just for the interrupt. */
  1158. a->write_csr(ioaddr, 0, 0x41);
  1159. mdelay(1);
  1160. dev->irq = probe_irq_off(irq_mask);
  1161. if (!dev->irq) {
  1162. if (pcnet32_debug & NETIF_MSG_PROBE)
  1163. printk(", failed to detect IRQ line.\n");
  1164. ret = -ENODEV;
  1165. goto err_free_ring;
  1166. }
  1167. if (pcnet32_debug & NETIF_MSG_PROBE)
  1168. printk(", probed IRQ %d.\n", dev->irq);
  1169. }
  1170. /* Set the mii phy_id so that we can query the link state */
  1171. if (lp->mii) {
  1172. /* lp->phycount and lp->phymask are set to 0 by memset above */
  1173. lp->mii_if.phy_id = ((lp->a.read_bcr(ioaddr, 33)) >> 5) & 0x1f;
  1174. /* scan for PHYs */
  1175. for (i = 0; i < PCNET32_MAX_PHYS; i++) {
  1176. unsigned short id1, id2;
  1177. id1 = mdio_read(dev, i, MII_PHYSID1);
  1178. if (id1 == 0xffff)
  1179. continue;
  1180. id2 = mdio_read(dev, i, MII_PHYSID2);
  1181. if (id2 == 0xffff)
  1182. continue;
  1183. if (i == 31 && ((chip_version + 1) & 0xfffe) == 0x2624)
  1184. continue; /* 79C971 & 79C972 have phantom phy at id 31 */
  1185. lp->phycount++;
  1186. lp->phymask |= (1 << i);
  1187. lp->mii_if.phy_id = i;
  1188. if (pcnet32_debug & NETIF_MSG_PROBE)
  1189. printk(KERN_INFO PFX
  1190. "Found PHY %04x:%04x at address %d.\n",
  1191. id1, id2, i);
  1192. }
  1193. lp->a.write_bcr(ioaddr, 33, (lp->mii_if.phy_id) << 5);
  1194. if (lp->phycount > 1) {
  1195. lp->options |= PCNET32_PORT_MII;
  1196. }
  1197. }
  1198. init_timer(&lp->watchdog_timer);
  1199. lp->watchdog_timer.data = (unsigned long)dev;
  1200. lp->watchdog_timer.function = (void *)&pcnet32_watchdog;
  1201. /* The PCNET32-specific entries in the device structure. */
  1202. dev->open = &pcnet32_open;
  1203. dev->hard_start_xmit = &pcnet32_start_xmit;
  1204. dev->stop = &pcnet32_close;
  1205. dev->get_stats = &pcnet32_get_stats;
  1206. dev->set_multicast_list = &pcnet32_set_multicast_list;
  1207. dev->do_ioctl = &pcnet32_ioctl;
  1208. dev->ethtool_ops = &pcnet32_ethtool_ops;
  1209. dev->tx_timeout = pcnet32_tx_timeout;
  1210. dev->watchdog_timeo = (5 * HZ);
  1211. #ifdef CONFIG_NET_POLL_CONTROLLER
  1212. dev->poll_controller = pcnet32_poll_controller;
  1213. #endif
  1214. /* Fill in the generic fields of the device structure. */
  1215. if (register_netdev(dev))
  1216. goto err_free_ring;
  1217. if (pdev) {
  1218. pci_set_drvdata(pdev, dev);
  1219. } else {
  1220. lp->next = pcnet32_dev;
  1221. pcnet32_dev = dev;
  1222. }
  1223. if (pcnet32_debug & NETIF_MSG_PROBE)
  1224. printk(KERN_INFO "%s: registered as %s\n", dev->name, lp->name);
  1225. cards_found++;
  1226. /* enable LED writes */
  1227. a->write_bcr(ioaddr, 2, a->read_bcr(ioaddr, 2) | 0x1000);
  1228. return 0;
  1229. err_free_ring:
  1230. pcnet32_free_ring(dev);
  1231. err_free_consistent:
  1232. pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
  1233. err_free_netdev:
  1234. free_netdev(dev);
  1235. err_release_region:
  1236. release_region(ioaddr, PCNET32_TOTAL_SIZE);
  1237. return ret;
  1238. }
  1239. /* if any allocation fails, caller must also call pcnet32_free_ring */
  1240. static int pcnet32_alloc_ring(struct net_device *dev, char *name)
  1241. {
  1242. struct pcnet32_private *lp = dev->priv;
  1243. lp->tx_ring = pci_alloc_consistent(lp->pci_dev,
  1244. sizeof(struct pcnet32_tx_head) *
  1245. lp->tx_ring_size,
  1246. &lp->tx_ring_dma_addr);
  1247. if (lp->tx_ring == NULL) {
  1248. if (pcnet32_debug & NETIF_MSG_DRV)
  1249. printk("\n" KERN_ERR PFX
  1250. "%s: Consistent memory allocation failed.\n",
  1251. name);
  1252. return -ENOMEM;
  1253. }
  1254. lp->rx_ring = pci_alloc_consistent(lp->pci_dev,
  1255. sizeof(struct pcnet32_rx_head) *
  1256. lp->rx_ring_size,
  1257. &lp->rx_ring_dma_addr);
  1258. if (lp->rx_ring == NULL) {
  1259. if (pcnet32_debug & NETIF_MSG_DRV)
  1260. printk("\n" KERN_ERR PFX
  1261. "%s: Consistent memory allocation failed.\n",
  1262. name);
  1263. return -ENOMEM;
  1264. }
  1265. lp->tx_dma_addr = kmalloc(sizeof(dma_addr_t) * lp->tx_ring_size,
  1266. GFP_ATOMIC);
  1267. if (!lp->tx_dma_addr) {
  1268. if (pcnet32_debug & NETIF_MSG_DRV)
  1269. printk("\n" KERN_ERR PFX
  1270. "%s: Memory allocation failed.\n", name);
  1271. return -ENOMEM;
  1272. }
  1273. memset(lp->tx_dma_addr, 0, sizeof(dma_addr_t) * lp->tx_ring_size);
  1274. lp->rx_dma_addr = kmalloc(sizeof(dma_addr_t) * lp->rx_ring_size,
  1275. GFP_ATOMIC);
  1276. if (!lp->rx_dma_addr) {
  1277. if (pcnet32_debug & NETIF_MSG_DRV)
  1278. printk("\n" KERN_ERR PFX
  1279. "%s: Memory allocation failed.\n", name);
  1280. return -ENOMEM;
  1281. }
  1282. memset(lp->rx_dma_addr, 0, sizeof(dma_addr_t) * lp->rx_ring_size);
  1283. lp->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * lp->tx_ring_size,
  1284. GFP_ATOMIC);
  1285. if (!lp->tx_skbuff) {
  1286. if (pcnet32_debug & NETIF_MSG_DRV)
  1287. printk("\n" KERN_ERR PFX
  1288. "%s: Memory allocation failed.\n", name);
  1289. return -ENOMEM;
  1290. }
  1291. memset(lp->tx_skbuff, 0, sizeof(struct sk_buff *) * lp->tx_ring_size);
  1292. lp->rx_skbuff = kmalloc(sizeof(struct sk_buff *) * lp->rx_ring_size,
  1293. GFP_ATOMIC);
  1294. if (!lp->rx_skbuff) {
  1295. if (pcnet32_debug & NETIF_MSG_DRV)
  1296. printk("\n" KERN_ERR PFX
  1297. "%s: Memory allocation failed.\n", name);
  1298. return -ENOMEM;
  1299. }
  1300. memset(lp->rx_skbuff, 0, sizeof(struct sk_buff *) * lp->rx_ring_size);
  1301. return 0;
  1302. }
  1303. static void pcnet32_free_ring(struct net_device *dev)
  1304. {
  1305. struct pcnet32_private *lp = dev->priv;
  1306. kfree(lp->tx_skbuff);
  1307. lp->tx_skbuff = NULL;
  1308. kfree(lp->rx_skbuff);
  1309. lp->rx_skbuff = NULL;
  1310. kfree(lp->tx_dma_addr);
  1311. lp->tx_dma_addr = NULL;
  1312. kfree(lp->rx_dma_addr);
  1313. lp->rx_dma_addr = NULL;
  1314. if (lp->tx_ring) {
  1315. pci_free_consistent(lp->pci_dev,
  1316. sizeof(struct pcnet32_tx_head) *
  1317. lp->tx_ring_size, lp->tx_ring,
  1318. lp->tx_ring_dma_addr);
  1319. lp->tx_ring = NULL;
  1320. }
  1321. if (lp->rx_ring) {
  1322. pci_free_consistent(lp->pci_dev,
  1323. sizeof(struct pcnet32_rx_head) *
  1324. lp->rx_ring_size, lp->rx_ring,
  1325. lp->rx_ring_dma_addr);
  1326. lp->rx_ring = NULL;
  1327. }
  1328. }
  1329. static int pcnet32_open(struct net_device *dev)
  1330. {
  1331. struct pcnet32_private *lp = dev->priv;
  1332. unsigned long ioaddr = dev->base_addr;
  1333. u16 val;
  1334. int i;
  1335. int rc;
  1336. unsigned long flags;
  1337. if (request_irq(dev->irq, &pcnet32_interrupt,
  1338. lp->shared_irq ? IRQF_SHARED : 0, dev->name,
  1339. (void *)dev)) {
  1340. return -EAGAIN;
  1341. }
  1342. spin_lock_irqsave(&lp->lock, flags);
  1343. /* Check for a valid station address */
  1344. if (!is_valid_ether_addr(dev->dev_addr)) {
  1345. rc = -EINVAL;
  1346. goto err_free_irq;
  1347. }
  1348. /* Reset the PCNET32 */
  1349. lp->a.reset(ioaddr);
  1350. /* switch pcnet32 to 32bit mode */
  1351. lp->a.write_bcr(ioaddr, 20, 2);
  1352. if (netif_msg_ifup(lp))
  1353. printk(KERN_DEBUG
  1354. "%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
  1355. dev->name, dev->irq, (u32) (lp->tx_ring_dma_addr),
  1356. (u32) (lp->rx_ring_dma_addr),
  1357. (u32) (lp->dma_addr +
  1358. offsetof(struct pcnet32_private, init_block)));
  1359. /* set/reset autoselect bit */
  1360. val = lp->a.read_bcr(ioaddr, 2) & ~2;
  1361. if (lp->options & PCNET32_PORT_ASEL)
  1362. val |= 2;
  1363. lp->a.write_bcr(ioaddr, 2, val);
  1364. /* handle full duplex setting */
  1365. if (lp->mii_if.full_duplex) {
  1366. val = lp->a.read_bcr(ioaddr, 9) & ~3;
  1367. if (lp->options & PCNET32_PORT_FD) {
  1368. val |= 1;
  1369. if (lp->options == (PCNET32_PORT_FD | PCNET32_PORT_AUI))
  1370. val |= 2;
  1371. } else if (lp->options & PCNET32_PORT_ASEL) {
  1372. /* workaround of xSeries250, turn on for 79C975 only */
  1373. i = ((lp->a.read_csr(ioaddr, 88) |
  1374. (lp->a.
  1375. read_csr(ioaddr, 89) << 16)) >> 12) & 0xffff;
  1376. if (i == 0x2627)
  1377. val |= 3;
  1378. }
  1379. lp->a.write_bcr(ioaddr, 9, val);
  1380. }
  1381. /* set/reset GPSI bit in test register */
  1382. val = lp->a.read_csr(ioaddr, 124) & ~0x10;
  1383. if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
  1384. val |= 0x10;
  1385. lp->a.write_csr(ioaddr, 124, val);
  1386. /* Allied Telesyn AT 2700/2701 FX are 100Mbit only and do not negotiate */
  1387. if (lp->pci_dev->subsystem_vendor == PCI_VENDOR_ID_AT &&
  1388. (lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2700FX ||
  1389. lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2701FX)) {
  1390. if (lp->options & PCNET32_PORT_ASEL) {
  1391. lp->options = PCNET32_PORT_FD | PCNET32_PORT_100;
  1392. if (netif_msg_link(lp))
  1393. printk(KERN_DEBUG
  1394. "%s: Setting 100Mb-Full Duplex.\n",
  1395. dev->name);
  1396. }
  1397. }
  1398. if (lp->phycount < 2) {
  1399. /*
  1400. * 24 Jun 2004 according AMD, in order to change the PHY,
  1401. * DANAS (or DISPM for 79C976) must be set; then select the speed,
  1402. * duplex, and/or enable auto negotiation, and clear DANAS
  1403. */
  1404. if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
  1405. lp->a.write_bcr(ioaddr, 32,
  1406. lp->a.read_bcr(ioaddr, 32) | 0x0080);
  1407. /* disable Auto Negotiation, set 10Mpbs, HD */
  1408. val = lp->a.read_bcr(ioaddr, 32) & ~0xb8;
  1409. if (lp->options & PCNET32_PORT_FD)
  1410. val |= 0x10;
  1411. if (lp->options & PCNET32_PORT_100)
  1412. val |= 0x08;
  1413. lp->a.write_bcr(ioaddr, 32, val);
  1414. } else {
  1415. if (lp->options & PCNET32_PORT_ASEL) {
  1416. lp->a.write_bcr(ioaddr, 32,
  1417. lp->a.read_bcr(ioaddr,
  1418. 32) | 0x0080);
  1419. /* enable auto negotiate, setup, disable fd */
  1420. val = lp->a.read_bcr(ioaddr, 32) & ~0x98;
  1421. val |= 0x20;
  1422. lp->a.write_bcr(ioaddr, 32, val);
  1423. }
  1424. }
  1425. } else {
  1426. int first_phy = -1;
  1427. u16 bmcr;
  1428. u32 bcr9;
  1429. struct ethtool_cmd ecmd;
  1430. /*
  1431. * There is really no good other way to handle multiple PHYs
  1432. * other than turning off all automatics
  1433. */
  1434. val = lp->a.read_bcr(ioaddr, 2);
  1435. lp->a.write_bcr(ioaddr, 2, val & ~2);
  1436. val = lp->a.read_bcr(ioaddr, 32);
  1437. lp->a.write_bcr(ioaddr, 32, val & ~(1 << 7)); /* stop MII manager */
  1438. if (!(lp->options & PCNET32_PORT_ASEL)) {
  1439. /* setup ecmd */
  1440. ecmd.port = PORT_MII;
  1441. ecmd.transceiver = XCVR_INTERNAL;
  1442. ecmd.autoneg = AUTONEG_DISABLE;
  1443. ecmd.speed =
  1444. lp->
  1445. options & PCNET32_PORT_100 ? SPEED_100 : SPEED_10;
  1446. bcr9 = lp->a.read_bcr(ioaddr, 9);
  1447. if (lp->options & PCNET32_PORT_FD) {
  1448. ecmd.duplex = DUPLEX_FULL;
  1449. bcr9 |= (1 << 0);
  1450. } else {
  1451. ecmd.duplex = DUPLEX_HALF;
  1452. bcr9 |= ~(1 << 0);
  1453. }
  1454. lp->a.write_bcr(ioaddr, 9, bcr9);
  1455. }
  1456. for (i = 0; i < PCNET32_MAX_PHYS; i++) {
  1457. if (lp->phymask & (1 << i)) {
  1458. /* isolate all but the first PHY */
  1459. bmcr = mdio_read(dev, i, MII_BMCR);
  1460. if (first_phy == -1) {
  1461. first_phy = i;
  1462. mdio_write(dev, i, MII_BMCR,
  1463. bmcr & ~BMCR_ISOLATE);
  1464. } else {
  1465. mdio_write(dev, i, MII_BMCR,
  1466. bmcr | BMCR_ISOLATE);
  1467. }
  1468. /* use mii_ethtool_sset to setup PHY */
  1469. lp->mii_if.phy_id = i;
  1470. ecmd.phy_address = i;
  1471. if (lp->options & PCNET32_PORT_ASEL) {
  1472. mii_ethtool_gset(&lp->mii_if, &ecmd);
  1473. ecmd.autoneg = AUTONEG_ENABLE;
  1474. }
  1475. mii_ethtool_sset(&lp->mii_if, &ecmd);
  1476. }
  1477. }
  1478. lp->mii_if.phy_id = first_phy;
  1479. if (netif_msg_link(lp))
  1480. printk(KERN_INFO "%s: Using PHY number %d.\n",
  1481. dev->name, first_phy);
  1482. }
  1483. #ifdef DO_DXSUFLO
  1484. if (lp->dxsuflo) { /* Disable transmit stop on underflow */
  1485. val = lp->a.read_csr(ioaddr, 3);
  1486. val |= 0x40;
  1487. lp->a.write_csr(ioaddr, 3, val);
  1488. }
  1489. #endif
  1490. lp->init_block.mode =
  1491. le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  1492. pcnet32_load_multicast(dev);
  1493. if (pcnet32_init_ring(dev)) {
  1494. rc = -ENOMEM;
  1495. goto err_free_ring;
  1496. }
  1497. /* Re-initialize the PCNET32, and start it when done. */
  1498. lp->a.write_csr(ioaddr, 1, (lp->dma_addr +
  1499. offsetof(struct pcnet32_private,
  1500. init_block)) & 0xffff);
  1501. lp->a.write_csr(ioaddr, 2,
  1502. (lp->dma_addr +
  1503. offsetof(struct pcnet32_private, init_block)) >> 16);
  1504. lp->a.write_csr(ioaddr, 4, 0x0915);
  1505. lp->a.write_csr(ioaddr, 0, 0x0001);
  1506. netif_start_queue(dev);
  1507. /* Print the link status and start the watchdog */
  1508. pcnet32_check_media(dev, 1);
  1509. mod_timer(&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
  1510. i = 0;
  1511. while (i++ < 100)
  1512. if (lp->a.read_csr(ioaddr, 0) & 0x0100)
  1513. break;
  1514. /*
  1515. * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
  1516. * reports that doing so triggers a bug in the '974.
  1517. */
  1518. lp->a.write_csr(ioaddr, 0, 0x0042);
  1519. if (netif_msg_ifup(lp))
  1520. printk(KERN_DEBUG
  1521. "%s: pcnet32 open after %d ticks, init block %#x csr0 %4.4x.\n",
  1522. dev->name, i,
  1523. (u32) (lp->dma_addr +
  1524. offsetof(struct pcnet32_private, init_block)),
  1525. lp->a.read_csr(ioaddr, 0));
  1526. spin_unlock_irqrestore(&lp->lock, flags);
  1527. return 0; /* Always succeed */
  1528. err_free_ring:
  1529. /* free any allocated skbuffs */
  1530. for (i = 0; i < lp->rx_ring_size; i++) {
  1531. lp->rx_ring[i].status = 0;
  1532. if (lp->rx_skbuff[i]) {
  1533. pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i],
  1534. PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
  1535. dev_kfree_skb(lp->rx_skbuff[i]);
  1536. }
  1537. lp->rx_skbuff[i] = NULL;
  1538. lp->rx_dma_addr[i] = 0;
  1539. }
  1540. /*
  1541. * Switch back to 16bit mode to avoid problems with dumb
  1542. * DOS packet driver after a warm reboot
  1543. */
  1544. lp->a.write_bcr(ioaddr, 20, 4);
  1545. err_free_irq:
  1546. spin_unlock_irqrestore(&lp->lock, flags);
  1547. free_irq(dev->irq, dev);
  1548. return rc;
  1549. }
  1550. /*
  1551. * The LANCE has been halted for one reason or another (busmaster memory
  1552. * arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
  1553. * etc.). Modern LANCE variants always reload their ring-buffer
  1554. * configuration when restarted, so we must reinitialize our ring
  1555. * context before restarting. As part of this reinitialization,
  1556. * find all packets still on the Tx ring and pretend that they had been
  1557. * sent (in effect, drop the packets on the floor) - the higher-level
  1558. * protocols will time out and retransmit. It'd be better to shuffle
  1559. * these skbs to a temp list and then actually re-Tx them after
  1560. * restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
  1561. */
  1562. static void pcnet32_purge_tx_ring(struct net_device *dev)
  1563. {
  1564. struct pcnet32_private *lp = dev->priv;
  1565. int i;
  1566. for (i = 0; i < lp->tx_ring_size; i++) {
  1567. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  1568. wmb(); /* Make sure adapter sees owner change */
  1569. if (lp->tx_skbuff[i]) {
  1570. pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i],
  1571. lp->tx_skbuff[i]->len,
  1572. PCI_DMA_TODEVICE);
  1573. dev_kfree_skb_any(lp->tx_skbuff[i]);
  1574. }
  1575. lp->tx_skbuff[i] = NULL;
  1576. lp->tx_dma_addr[i] = 0;
  1577. }
  1578. }
  1579. /* Initialize the PCNET32 Rx and Tx rings. */
  1580. static int pcnet32_init_ring(struct net_device *dev)
  1581. {
  1582. struct pcnet32_private *lp = dev->priv;
  1583. int i;
  1584. lp->tx_full = 0;
  1585. lp->cur_rx = lp->cur_tx = 0;
  1586. lp->dirty_rx = lp->dirty_tx = 0;
  1587. for (i = 0; i < lp->rx_ring_size; i++) {
  1588. struct sk_buff *rx_skbuff = lp->rx_skbuff[i];
  1589. if (rx_skbuff == NULL) {
  1590. if (!
  1591. (rx_skbuff = lp->rx_skbuff[i] =
  1592. dev_alloc_skb(PKT_BUF_SZ))) {
  1593. /* there is not much, we can do at this point */
  1594. if (pcnet32_debug & NETIF_MSG_DRV)
  1595. printk(KERN_ERR
  1596. "%s: pcnet32_init_ring dev_alloc_skb failed.\n",
  1597. dev->name);
  1598. return -1;
  1599. }
  1600. skb_reserve(rx_skbuff, 2);
  1601. }
  1602. rmb();
  1603. if (lp->rx_dma_addr[i] == 0)
  1604. lp->rx_dma_addr[i] =
  1605. pci_map_single(lp->pci_dev, rx_skbuff->data,
  1606. PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
  1607. lp->rx_ring[i].base = (u32) le32_to_cpu(lp->rx_dma_addr[i]);
  1608. lp->rx_ring[i].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
  1609. wmb(); /* Make sure owner changes after all others are visible */
  1610. lp->rx_ring[i].status = le16_to_cpu(0x8000);
  1611. }
  1612. /* The Tx buffer address is filled in as needed, but we do need to clear
  1613. * the upper ownership bit. */
  1614. for (i = 0; i < lp->tx_ring_size; i++) {
  1615. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  1616. wmb(); /* Make sure adapter sees owner change */
  1617. lp->tx_ring[i].base = 0;
  1618. lp->tx_dma_addr[i] = 0;
  1619. }
  1620. lp->init_block.tlen_rlen =
  1621. le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
  1622. for (i = 0; i < 6; i++)
  1623. lp->init_block.phys_addr[i] = dev->dev_addr[i];
  1624. lp->init_block.rx_ring = (u32) le32_to_cpu(lp->rx_ring_dma_addr);
  1625. lp->init_block.tx_ring = (u32) le32_to_cpu(lp->tx_ring_dma_addr);
  1626. wmb(); /* Make sure all changes are visible */
  1627. return 0;
  1628. }
  1629. /* the pcnet32 has been issued a stop or reset. Wait for the stop bit
  1630. * then flush the pending transmit operations, re-initialize the ring,
  1631. * and tell the chip to initialize.
  1632. */
  1633. static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
  1634. {
  1635. struct pcnet32_private *lp = dev->priv;
  1636. unsigned long ioaddr = dev->base_addr;
  1637. int i;
  1638. /* wait for stop */
  1639. for (i = 0; i < 100; i++)
  1640. if (lp->a.read_csr(ioaddr, 0) & 0x0004)
  1641. break;
  1642. if (i >= 100 && netif_msg_drv(lp))
  1643. printk(KERN_ERR
  1644. "%s: pcnet32_restart timed out waiting for stop.\n",
  1645. dev->name);
  1646. pcnet32_purge_tx_ring(dev);
  1647. if (pcnet32_init_ring(dev))
  1648. return;
  1649. /* ReInit Ring */
  1650. lp->a.write_csr(ioaddr, 0, 1);
  1651. i = 0;
  1652. while (i++ < 1000)
  1653. if (lp->a.read_csr(ioaddr, 0) & 0x0100)
  1654. break;
  1655. lp->a.write_csr(ioaddr, 0, csr0_bits);
  1656. }
  1657. static void pcnet32_tx_timeout(struct net_device *dev)
  1658. {
  1659. struct pcnet32_private *lp = dev->priv;
  1660. unsigned long ioaddr = dev->base_addr, flags;
  1661. spin_lock_irqsave(&lp->lock, flags);
  1662. /* Transmitter timeout, serious problems. */
  1663. if (pcnet32_debug & NETIF_MSG_DRV)
  1664. printk(KERN_ERR
  1665. "%s: transmit timed out, status %4.4x, resetting.\n",
  1666. dev->name, lp->a.read_csr(ioaddr, 0));
  1667. lp->a.write_csr(ioaddr, 0, 0x0004);
  1668. lp->stats.tx_errors++;
  1669. if (netif_msg_tx_err(lp)) {
  1670. int i;
  1671. printk(KERN_DEBUG
  1672. " Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
  1673. lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
  1674. lp->cur_rx);
  1675. for (i = 0; i < lp->rx_ring_size; i++)
  1676. printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
  1677. le32_to_cpu(lp->rx_ring[i].base),
  1678. (-le16_to_cpu(lp->rx_ring[i].buf_length)) &
  1679. 0xffff, le32_to_cpu(lp->rx_ring[i].msg_length),
  1680. le16_to_cpu(lp->rx_ring[i].status));
  1681. for (i = 0; i < lp->tx_ring_size; i++)
  1682. printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
  1683. le32_to_cpu(lp->tx_ring[i].base),
  1684. (-le16_to_cpu(lp->tx_ring[i].length)) & 0xffff,
  1685. le32_to_cpu(lp->tx_ring[i].misc),
  1686. le16_to_cpu(lp->tx_ring[i].status));
  1687. printk("\n");
  1688. }
  1689. pcnet32_restart(dev, 0x0042);
  1690. dev->trans_start = jiffies;
  1691. netif_wake_queue(dev);
  1692. spin_unlock_irqrestore(&lp->lock, flags);
  1693. }
  1694. static int pcnet32_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1695. {
  1696. struct pcnet32_private *lp = dev->priv;
  1697. unsigned long ioaddr = dev->base_addr;
  1698. u16 status;
  1699. int entry;
  1700. unsigned long flags;
  1701. spin_lock_irqsave(&lp->lock, flags);
  1702. if (netif_msg_tx_queued(lp)) {
  1703. printk(KERN_DEBUG
  1704. "%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
  1705. dev->name, lp->a.read_csr(ioaddr, 0));
  1706. }
  1707. /* Default status -- will not enable Successful-TxDone
  1708. * interrupt when that option is available to us.
  1709. */
  1710. status = 0x8300;
  1711. /* Fill in a Tx ring entry */
  1712. /* Mask to ring buffer boundary. */
  1713. entry = lp->cur_tx & lp->tx_mod_mask;
  1714. /* Caution: the write order is important here, set the status
  1715. * with the "ownership" bits last. */
  1716. lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
  1717. lp->tx_ring[entry].misc = 0x00000000;
  1718. lp->tx_skbuff[entry] = skb;
  1719. lp->tx_dma_addr[entry] =
  1720. pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
  1721. lp->tx_ring[entry].base = (u32) le32_to_cpu(lp->tx_dma_addr[entry]);
  1722. wmb(); /* Make sure owner changes after all others are visible */
  1723. lp->tx_ring[entry].status = le16_to_cpu(status);
  1724. lp->cur_tx++;
  1725. lp->stats.tx_bytes += skb->len;
  1726. /* Trigger an immediate send poll. */
  1727. lp->a.write_csr(ioaddr, 0, 0x0048);
  1728. dev->trans_start = jiffies;
  1729. if (lp->tx_ring[(entry + 1) & lp->tx_mod_mask].base != 0) {
  1730. lp->tx_full = 1;
  1731. netif_stop_queue(dev);
  1732. }
  1733. spin_unlock_irqrestore(&lp->lock, flags);
  1734. return 0;
  1735. }
  1736. /* The PCNET32 interrupt handler. */
  1737. static irqreturn_t
  1738. pcnet32_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1739. {
  1740. struct net_device *dev = dev_id;
  1741. struct pcnet32_private *lp;
  1742. unsigned long ioaddr;
  1743. u16 csr0, rap;
  1744. int boguscnt = max_interrupt_work;
  1745. int must_restart;
  1746. if (!dev) {
  1747. if (pcnet32_debug & NETIF_MSG_INTR)
  1748. printk(KERN_DEBUG "%s(): irq %d for unknown device\n",
  1749. __FUNCTION__, irq);
  1750. return IRQ_NONE;
  1751. }
  1752. ioaddr = dev->base_addr;
  1753. lp = dev->priv;
  1754. spin_lock(&lp->lock);
  1755. rap = lp->a.read_rap(ioaddr);
  1756. while ((csr0 = lp->a.read_csr(ioaddr, 0)) & 0x8f00 && --boguscnt >= 0) {
  1757. if (csr0 == 0xffff) {
  1758. break; /* PCMCIA remove happened */
  1759. }
  1760. /* Acknowledge all of the current interrupt sources ASAP. */
  1761. lp->a.write_csr(ioaddr, 0, csr0 & ~0x004f);
  1762. must_restart = 0;
  1763. if (netif_msg_intr(lp))
  1764. printk(KERN_DEBUG
  1765. "%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
  1766. dev->name, csr0, lp->a.read_csr(ioaddr, 0));
  1767. if (csr0 & 0x0400) /* Rx interrupt */
  1768. pcnet32_rx(dev);
  1769. if (csr0 & 0x0200) { /* Tx-done interrupt */
  1770. unsigned int dirty_tx = lp->dirty_tx;
  1771. int delta;
  1772. while (dirty_tx != lp->cur_tx) {
  1773. int entry = dirty_tx & lp->tx_mod_mask;
  1774. int status =
  1775. (short)le16_to_cpu(lp->tx_ring[entry].
  1776. status);
  1777. if (status < 0)
  1778. break; /* It still hasn't been Txed */
  1779. lp->tx_ring[entry].base = 0;
  1780. if (status & 0x4000) {
  1781. /* There was an major error, log it. */
  1782. int err_status =
  1783. le32_to_cpu(lp->tx_ring[entry].
  1784. misc);
  1785. lp->stats.tx_errors++;
  1786. if (netif_msg_tx_err(lp))
  1787. printk(KERN_ERR
  1788. "%s: Tx error status=%04x err_status=%08x\n",
  1789. dev->name, status,
  1790. err_status);
  1791. if (err_status & 0x04000000)
  1792. lp->stats.tx_aborted_errors++;
  1793. if (err_status & 0x08000000)
  1794. lp->stats.tx_carrier_errors++;
  1795. if (err_status & 0x10000000)
  1796. lp->stats.tx_window_errors++;
  1797. #ifndef DO_DXSUFLO
  1798. if (err_status & 0x40000000) {
  1799. lp->stats.tx_fifo_errors++;
  1800. /* Ackk! On FIFO errors the Tx unit is turned off! */
  1801. /* Remove this verbosity later! */
  1802. if (netif_msg_tx_err(lp))
  1803. printk(KERN_ERR
  1804. "%s: Tx FIFO error! CSR0=%4.4x\n",
  1805. dev->name, csr0);
  1806. must_restart = 1;
  1807. }
  1808. #else
  1809. if (err_status & 0x40000000) {
  1810. lp->stats.tx_fifo_errors++;
  1811. if (!lp->dxsuflo) { /* If controller doesn't recover ... */
  1812. /* Ackk! On FIFO errors the Tx unit is turned off! */
  1813. /* Remove this verbosity later! */
  1814. if (netif_msg_tx_err
  1815. (lp))
  1816. printk(KERN_ERR
  1817. "%s: Tx FIFO error! CSR0=%4.4x\n",
  1818. dev->
  1819. name,
  1820. csr0);
  1821. must_restart = 1;
  1822. }
  1823. }
  1824. #endif
  1825. } else {
  1826. if (status & 0x1800)
  1827. lp->stats.collisions++;
  1828. lp->stats.tx_packets++;
  1829. }
  1830. /* We must free the original skb */
  1831. if (lp->tx_skbuff[entry]) {
  1832. pci_unmap_single(lp->pci_dev,
  1833. lp->tx_dma_addr[entry],
  1834. lp->tx_skbuff[entry]->
  1835. len, PCI_DMA_TODEVICE);
  1836. dev_kfree_skb_irq(lp->tx_skbuff[entry]);
  1837. lp->tx_skbuff[entry] = NULL;
  1838. lp->tx_dma_addr[entry] = 0;
  1839. }
  1840. dirty_tx++;
  1841. }
  1842. delta =
  1843. (lp->cur_tx - dirty_tx) & (lp->tx_mod_mask +
  1844. lp->tx_ring_size);
  1845. if (delta > lp->tx_ring_size) {
  1846. if (netif_msg_drv(lp))
  1847. printk(KERN_ERR
  1848. "%s: out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
  1849. dev->name, dirty_tx, lp->cur_tx,
  1850. lp->tx_full);
  1851. dirty_tx += lp->tx_ring_size;
  1852. delta -= lp->tx_ring_size;
  1853. }
  1854. if (lp->tx_full &&
  1855. netif_queue_stopped(dev) &&
  1856. delta < lp->tx_ring_size - 2) {
  1857. /* The ring is no longer full, clear tbusy. */
  1858. lp->tx_full = 0;
  1859. netif_wake_queue(dev);
  1860. }
  1861. lp->dirty_tx = dirty_tx;
  1862. }
  1863. /* Log misc errors. */
  1864. if (csr0 & 0x4000)
  1865. lp->stats.tx_errors++; /* Tx babble. */
  1866. if (csr0 & 0x1000) {
  1867. /*
  1868. * this happens when our receive ring is full. This shouldn't
  1869. * be a problem as we will see normal rx interrupts for the frames
  1870. * in the receive ring. But there are some PCI chipsets (I can
  1871. * reproduce this on SP3G with Intel saturn chipset) which have
  1872. * sometimes problems and will fill up the receive ring with
  1873. * error descriptors. In this situation we don't get a rx
  1874. * interrupt, but a missed frame interrupt sooner or later.
  1875. * So we try to clean up our receive ring here.
  1876. */
  1877. pcnet32_rx(dev);
  1878. lp->stats.rx_errors++; /* Missed a Rx frame. */
  1879. }
  1880. if (csr0 & 0x0800) {
  1881. if (netif_msg_drv(lp))
  1882. printk(KERN_ERR
  1883. "%s: Bus master arbitration failure, status %4.4x.\n",
  1884. dev->name, csr0);
  1885. /* unlike for the lance, there is no restart needed */
  1886. }
  1887. if (must_restart) {
  1888. /* reset the chip to clear the error condition, then restart */
  1889. lp->a.reset(ioaddr);
  1890. lp->a.write_csr(ioaddr, 4, 0x0915);
  1891. pcnet32_restart(dev, 0x0002);
  1892. netif_wake_queue(dev);
  1893. }
  1894. }
  1895. /* Set interrupt enable. */
  1896. lp->a.write_csr(ioaddr, 0, 0x0040);
  1897. lp->a.write_rap(ioaddr, rap);
  1898. if (netif_msg_intr(lp))
  1899. printk(KERN_DEBUG "%s: exiting interrupt, csr0=%#4.4x.\n",
  1900. dev->name, lp->a.read_csr(ioaddr, 0));
  1901. spin_unlock(&lp->lock);
  1902. return IRQ_HANDLED;
  1903. }
  1904. static int pcnet32_rx(struct net_device *dev)
  1905. {
  1906. struct pcnet32_private *lp = dev->priv;
  1907. int entry = lp->cur_rx & lp->rx_mod_mask;
  1908. int boguscnt = lp->rx_ring_size / 2;
  1909. /* If we own the next entry, it's a new packet. Send it up. */
  1910. while ((short)le16_to_cpu(lp->rx_ring[entry].status) >= 0) {
  1911. int status = (short)le16_to_cpu(lp->rx_ring[entry].status) >> 8;
  1912. if (status != 0x03) { /* There was an error. */
  1913. /*
  1914. * There is a tricky error noted by John Murphy,
  1915. * <murf@perftech.com> to Russ Nelson: Even with full-sized
  1916. * buffers it's possible for a jabber packet to use two
  1917. * buffers, with only the last correctly noting the error.
  1918. */
  1919. if (status & 0x01) /* Only count a general error at the */
  1920. lp->stats.rx_errors++; /* end of a packet. */
  1921. if (status & 0x20)
  1922. lp->stats.rx_frame_errors++;
  1923. if (status & 0x10)
  1924. lp->stats.rx_over_errors++;
  1925. if (status & 0x08)
  1926. lp->stats.rx_crc_errors++;
  1927. if (status & 0x04)
  1928. lp->stats.rx_fifo_errors++;
  1929. lp->rx_ring[entry].status &= le16_to_cpu(0x03ff);
  1930. } else {
  1931. /* Malloc up new buffer, compatible with net-2e. */
  1932. short pkt_len =
  1933. (le32_to_cpu(lp->rx_ring[entry].msg_length) & 0xfff)
  1934. - 4;
  1935. struct sk_buff *skb;
  1936. /* Discard oversize frames. */
  1937. if (unlikely(pkt_len > PKT_BUF_SZ - 2)) {
  1938. if (netif_msg_drv(lp))
  1939. printk(KERN_ERR
  1940. "%s: Impossible packet size %d!\n",
  1941. dev->name, pkt_len);
  1942. lp->stats.rx_errors++;
  1943. } else if (pkt_len < 60) {
  1944. if (netif_msg_rx_err(lp))
  1945. printk(KERN_ERR "%s: Runt packet!\n",
  1946. dev->name);
  1947. lp->stats.rx_errors++;
  1948. } else {
  1949. int rx_in_place = 0;
  1950. if (pkt_len > rx_copybreak) {
  1951. struct sk_buff *newskb;
  1952. if ((newskb =
  1953. dev_alloc_skb(PKT_BUF_SZ))) {
  1954. skb_reserve(newskb, 2);
  1955. skb = lp->rx_skbuff[entry];
  1956. pci_unmap_single(lp->pci_dev,
  1957. lp->
  1958. rx_dma_addr
  1959. [entry],
  1960. PKT_BUF_SZ - 2,
  1961. PCI_DMA_FROMDEVICE);
  1962. skb_put(skb, pkt_len);
  1963. lp->rx_skbuff[entry] = newskb;
  1964. newskb->dev = dev;
  1965. lp->rx_dma_addr[entry] =
  1966. pci_map_single(lp->pci_dev,
  1967. newskb->data,
  1968. PKT_BUF_SZ -
  1969. 2,
  1970. PCI_DMA_FROMDEVICE);
  1971. lp->rx_ring[entry].base =
  1972. le32_to_cpu(lp->
  1973. rx_dma_addr
  1974. [entry]);
  1975. rx_in_place = 1;
  1976. } else
  1977. skb = NULL;
  1978. } else {
  1979. skb = dev_alloc_skb(pkt_len + 2);
  1980. }
  1981. if (skb == NULL) {
  1982. int i;
  1983. if (netif_msg_drv(lp))
  1984. printk(KERN_ERR
  1985. "%s: Memory squeeze, deferring packet.\n",
  1986. dev->name);
  1987. for (i = 0; i < lp->rx_ring_size; i++)
  1988. if ((short)
  1989. le16_to_cpu(lp->
  1990. rx_ring[(entry +
  1991. i)
  1992. & lp->
  1993. rx_mod_mask].
  1994. status) < 0)
  1995. break;
  1996. if (i > lp->rx_ring_size - 2) {
  1997. lp->stats.rx_dropped++;
  1998. lp->rx_ring[entry].status |=
  1999. le16_to_cpu(0x8000);
  2000. wmb(); /* Make sure adapter sees owner change */
  2001. lp->cur_rx++;
  2002. }
  2003. break;
  2004. }
  2005. skb->dev = dev;
  2006. if (!rx_in_place) {
  2007. skb_reserve(skb, 2); /* 16 byte align */
  2008. skb_put(skb, pkt_len); /* Make room */
  2009. pci_dma_sync_single_for_cpu(lp->pci_dev,
  2010. lp->
  2011. rx_dma_addr
  2012. [entry],
  2013. PKT_BUF_SZ -
  2014. 2,
  2015. PCI_DMA_FROMDEVICE);
  2016. eth_copy_and_sum(skb,
  2017. (unsigned char *)(lp->
  2018. rx_skbuff
  2019. [entry]->
  2020. data),
  2021. pkt_len, 0);
  2022. pci_dma_sync_single_for_device(lp->
  2023. pci_dev,
  2024. lp->
  2025. rx_dma_addr
  2026. [entry],
  2027. PKT_BUF_SZ
  2028. - 2,
  2029. PCI_DMA_FROMDEVICE);
  2030. }
  2031. lp->stats.rx_bytes += skb->len;
  2032. skb->protocol = eth_type_trans(skb, dev);
  2033. netif_rx(skb);
  2034. dev->last_rx = jiffies;
  2035. lp->stats.rx_packets++;
  2036. }
  2037. }
  2038. /*
  2039. * The docs say that the buffer length isn't touched, but Andrew Boyd
  2040. * of QNX reports that some revs of the 79C965 clear it.
  2041. */
  2042. lp->rx_ring[entry].buf_length = le16_to_cpu(2 - PKT_BUF_SZ);
  2043. wmb(); /* Make sure owner changes after all others are visible */
  2044. lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
  2045. entry = (++lp->cur_rx) & lp->rx_mod_mask;
  2046. if (--boguscnt <= 0)
  2047. break; /* don't stay in loop forever */
  2048. }
  2049. return 0;
  2050. }
  2051. static int pcnet32_close(struct net_device *dev)
  2052. {
  2053. unsigned long ioaddr = dev->base_addr;
  2054. struct pcnet32_private *lp = dev->priv;
  2055. int i;
  2056. unsigned long flags;
  2057. del_timer_sync(&lp->watchdog_timer);
  2058. netif_stop_queue(dev);
  2059. spin_lock_irqsave(&lp->lock, flags);
  2060. lp->stats.rx_missed_errors = lp->a.read_csr(ioaddr, 112);
  2061. if (netif_msg_ifdown(lp))
  2062. printk(KERN_DEBUG
  2063. "%s: Shutting down ethercard, status was %2.2x.\n",
  2064. dev->name, lp->a.read_csr(ioaddr, 0));
  2065. /* We stop the PCNET32 here -- it occasionally polls memory if we don't. */
  2066. lp->a.write_csr(ioaddr, 0, 0x0004);
  2067. /*
  2068. * Switch back to 16bit mode to avoid problems with dumb
  2069. * DOS packet driver after a warm reboot
  2070. */
  2071. lp->a.write_bcr(ioaddr, 20, 4);
  2072. spin_unlock_irqrestore(&lp->lock, flags);
  2073. free_irq(dev->irq, dev);
  2074. spin_lock_irqsave(&lp->lock, flags);
  2075. /* free all allocated skbuffs */
  2076. for (i = 0; i < lp->rx_ring_size; i++) {
  2077. lp->rx_ring[i].status = 0;
  2078. wmb(); /* Make sure adapter sees owner change */
  2079. if (lp->rx_skbuff[i]) {
  2080. pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i],
  2081. PKT_BUF_SZ - 2, PCI_DMA_FROMDEVICE);
  2082. dev_kfree_skb(lp->rx_skbuff[i]);
  2083. }
  2084. lp->rx_skbuff[i] = NULL;
  2085. lp->rx_dma_addr[i] = 0;
  2086. }
  2087. for (i = 0; i < lp->tx_ring_size; i++) {
  2088. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  2089. wmb(); /* Make sure adapter sees owner change */
  2090. if (lp->tx_skbuff[i]) {
  2091. pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i],
  2092. lp->tx_skbuff[i]->len,
  2093. PCI_DMA_TODEVICE);
  2094. dev_kfree_skb(lp->tx_skbuff[i]);
  2095. }
  2096. lp->tx_skbuff[i] = NULL;
  2097. lp->tx_dma_addr[i] = 0;
  2098. }
  2099. spin_unlock_irqrestore(&lp->lock, flags);
  2100. return 0;
  2101. }
  2102. static struct net_device_stats *pcnet32_get_stats(struct net_device *dev)
  2103. {
  2104. struct pcnet32_private *lp = dev->priv;
  2105. unsigned long ioaddr = dev->base_addr;
  2106. u16 saved_addr;
  2107. unsigned long flags;
  2108. spin_lock_irqsave(&lp->lock, flags);
  2109. saved_addr = lp->a.read_rap(ioaddr);
  2110. lp->stats.rx_missed_errors = lp->a.read_csr(ioaddr, 112);
  2111. lp->a.write_rap(ioaddr, saved_addr);
  2112. spin_unlock_irqrestore(&lp->lock, flags);
  2113. return &lp->stats;
  2114. }
  2115. /* taken from the sunlance driver, which it took from the depca driver */
  2116. static void pcnet32_load_multicast(struct net_device *dev)
  2117. {
  2118. struct pcnet32_private *lp = dev->priv;
  2119. volatile struct pcnet32_init_block *ib = &lp->init_block;
  2120. volatile u16 *mcast_table = (u16 *) & ib->filter;
  2121. struct dev_mc_list *dmi = dev->mc_list;
  2122. char *addrs;
  2123. int i;
  2124. u32 crc;
  2125. /* set all multicast bits */
  2126. if (dev->flags & IFF_ALLMULTI) {
  2127. ib->filter[0] = 0xffffffff;
  2128. ib->filter[1] = 0xffffffff;
  2129. return;
  2130. }
  2131. /* clear the multicast filter */
  2132. ib->filter[0] = 0;
  2133. ib->filter[1] = 0;
  2134. /* Add addresses */
  2135. for (i = 0; i < dev->mc_count; i++) {
  2136. addrs = dmi->dmi_addr;
  2137. dmi = dmi->next;
  2138. /* multicast address? */
  2139. if (!(*addrs & 1))
  2140. continue;
  2141. crc = ether_crc_le(6, addrs);
  2142. crc = crc >> 26;
  2143. mcast_table[crc >> 4] =
  2144. le16_to_cpu(le16_to_cpu(mcast_table[crc >> 4]) |
  2145. (1 << (crc & 0xf)));
  2146. }
  2147. return;
  2148. }
  2149. /*
  2150. * Set or clear the multicast filter for this adaptor.
  2151. */
  2152. static void pcnet32_set_multicast_list(struct net_device *dev)
  2153. {
  2154. unsigned long ioaddr = dev->base_addr, flags;
  2155. struct pcnet32_private *lp = dev->priv;
  2156. spin_lock_irqsave(&lp->lock, flags);
  2157. if (dev->flags & IFF_PROMISC) {
  2158. /* Log any net taps. */
  2159. if (netif_msg_hw(lp))
  2160. printk(KERN_INFO "%s: Promiscuous mode enabled.\n",
  2161. dev->name);
  2162. lp->init_block.mode =
  2163. le16_to_cpu(0x8000 | (lp->options & PCNET32_PORT_PORTSEL) <<
  2164. 7);
  2165. } else {
  2166. lp->init_block.mode =
  2167. le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  2168. pcnet32_load_multicast(dev);
  2169. }
  2170. lp->a.write_csr(ioaddr, 0, 0x0004); /* Temporarily stop the lance. */
  2171. pcnet32_restart(dev, 0x0042); /* Resume normal operation */
  2172. netif_wake_queue(dev);
  2173. spin_unlock_irqrestore(&lp->lock, flags);
  2174. }
  2175. /* This routine assumes that the lp->lock is held */
  2176. static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
  2177. {
  2178. struct pcnet32_private *lp = dev->priv;
  2179. unsigned long ioaddr = dev->base_addr;
  2180. u16 val_out;
  2181. if (!lp->mii)
  2182. return 0;
  2183. lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  2184. val_out = lp->a.read_bcr(ioaddr, 34);
  2185. return val_out;
  2186. }
  2187. /* This routine assumes that the lp->lock is held */
  2188. static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
  2189. {
  2190. struct pcnet32_private *lp = dev->priv;
  2191. unsigned long ioaddr = dev->base_addr;
  2192. if (!lp->mii)
  2193. return;
  2194. lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  2195. lp->a.write_bcr(ioaddr, 34, val);
  2196. }
  2197. static int pcnet32_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2198. {
  2199. struct pcnet32_private *lp = dev->priv;
  2200. int rc;
  2201. unsigned long flags;
  2202. /* SIOC[GS]MIIxxx ioctls */
  2203. if (lp->mii) {
  2204. spin_lock_irqsave(&lp->lock, flags);
  2205. rc = generic_mii_ioctl(&lp->mii_if, if_mii(rq), cmd, NULL);
  2206. spin_unlock_irqrestore(&lp->lock, flags);
  2207. } else {
  2208. rc = -EOPNOTSUPP;
  2209. }
  2210. return rc;
  2211. }
  2212. static int pcnet32_check_otherphy(struct net_device *dev)
  2213. {
  2214. struct pcnet32_private *lp = dev->priv;
  2215. struct mii_if_info mii = lp->mii_if;
  2216. u16 bmcr;
  2217. int i;
  2218. for (i = 0; i < PCNET32_MAX_PHYS; i++) {
  2219. if (i == lp->mii_if.phy_id)
  2220. continue; /* skip active phy */
  2221. if (lp->phymask & (1 << i)) {
  2222. mii.phy_id = i;
  2223. if (mii_link_ok(&mii)) {
  2224. /* found PHY with active link */
  2225. if (netif_msg_link(lp))
  2226. printk(KERN_INFO
  2227. "%s: Using PHY number %d.\n",
  2228. dev->name, i);
  2229. /* isolate inactive phy */
  2230. bmcr =
  2231. mdio_read(dev, lp->mii_if.phy_id, MII_BMCR);
  2232. mdio_write(dev, lp->mii_if.phy_id, MII_BMCR,
  2233. bmcr | BMCR_ISOLATE);
  2234. /* de-isolate new phy */
  2235. bmcr = mdio_read(dev, i, MII_BMCR);
  2236. mdio_write(dev, i, MII_BMCR,
  2237. bmcr & ~BMCR_ISOLATE);
  2238. /* set new phy address */
  2239. lp->mii_if.phy_id = i;
  2240. return 1;
  2241. }
  2242. }
  2243. }
  2244. return 0;
  2245. }
  2246. /*
  2247. * Show the status of the media. Similar to mii_check_media however it
  2248. * correctly shows the link speed for all (tested) pcnet32 variants.
  2249. * Devices with no mii just report link state without speed.
  2250. *
  2251. * Caller is assumed to hold and release the lp->lock.
  2252. */
  2253. static void pcnet32_check_media(struct net_device *dev, int verbose)
  2254. {
  2255. struct pcnet32_private *lp = dev->priv;
  2256. int curr_link;
  2257. int prev_link = netif_carrier_ok(dev) ? 1 : 0;
  2258. u32 bcr9;
  2259. if (lp->mii) {
  2260. curr_link = mii_link_ok(&lp->mii_if);
  2261. } else {
  2262. ulong ioaddr = dev->base_addr; /* card base I/O address */
  2263. curr_link = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
  2264. }
  2265. if (!curr_link) {
  2266. if (prev_link || verbose) {
  2267. netif_carrier_off(dev);
  2268. if (netif_msg_link(lp))
  2269. printk(KERN_INFO "%s: link down\n", dev->name);
  2270. }
  2271. if (lp->phycount > 1) {
  2272. curr_link = pcnet32_check_otherphy(dev);
  2273. prev_link = 0;
  2274. }
  2275. } else if (verbose || !prev_link) {
  2276. netif_carrier_on(dev);
  2277. if (lp->mii) {
  2278. if (netif_msg_link(lp)) {
  2279. struct ethtool_cmd ecmd;
  2280. mii_ethtool_gset(&lp->mii_if, &ecmd);
  2281. printk(KERN_INFO
  2282. "%s: link up, %sMbps, %s-duplex\n",
  2283. dev->name,
  2284. (ecmd.speed == SPEED_100) ? "100" : "10",
  2285. (ecmd.duplex ==
  2286. DUPLEX_FULL) ? "full" : "half");
  2287. }
  2288. bcr9 = lp->a.read_bcr(dev->base_addr, 9);
  2289. if ((bcr9 & (1 << 0)) != lp->mii_if.full_duplex) {
  2290. if (lp->mii_if.full_duplex)
  2291. bcr9 |= (1 << 0);
  2292. else
  2293. bcr9 &= ~(1 << 0);
  2294. lp->a.write_bcr(dev->base_addr, 9, bcr9);
  2295. }
  2296. } else {
  2297. if (netif_msg_link(lp))
  2298. printk(KERN_INFO "%s: link up\n", dev->name);
  2299. }
  2300. }
  2301. }
  2302. /*
  2303. * Check for loss of link and link establishment.
  2304. * Can not use mii_check_media because it does nothing if mode is forced.
  2305. */
  2306. static void pcnet32_watchdog(struct net_device *dev)
  2307. {
  2308. struct pcnet32_private *lp = dev->priv;
  2309. unsigned long flags;
  2310. /* Print the link status if it has changed */
  2311. spin_lock_irqsave(&lp->lock, flags);
  2312. pcnet32_check_media(dev, 0);
  2313. spin_unlock_irqrestore(&lp->lock, flags);
  2314. mod_timer(&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
  2315. }
  2316. static void __devexit pcnet32_remove_one(struct pci_dev *pdev)
  2317. {
  2318. struct net_device *dev = pci_get_drvdata(pdev);
  2319. if (dev) {
  2320. struct pcnet32_private *lp = dev->priv;
  2321. unregister_netdev(dev);
  2322. pcnet32_free_ring(dev);
  2323. release_region(dev->base_addr, PCNET32_TOTAL_SIZE);
  2324. pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
  2325. free_netdev(dev);
  2326. pci_disable_device(pdev);
  2327. pci_set_drvdata(pdev, NULL);
  2328. }
  2329. }
  2330. static struct pci_driver pcnet32_driver = {
  2331. .name = DRV_NAME,
  2332. .probe = pcnet32_probe_pci,
  2333. .remove = __devexit_p(pcnet32_remove_one),
  2334. .id_table = pcnet32_pci_tbl,
  2335. };
  2336. /* An additional parameter that may be passed in... */
  2337. static int debug = -1;
  2338. static int tx_start_pt = -1;
  2339. static int pcnet32_have_pci;
  2340. module_param(debug, int, 0);
  2341. MODULE_PARM_DESC(debug, DRV_NAME " debug level");
  2342. module_param(max_interrupt_work, int, 0);
  2343. MODULE_PARM_DESC(max_interrupt_work,
  2344. DRV_NAME " maximum events handled per interrupt");
  2345. module_param(rx_copybreak, int, 0);
  2346. MODULE_PARM_DESC(rx_copybreak,
  2347. DRV_NAME " copy breakpoint for copy-only-tiny-frames");
  2348. module_param(tx_start_pt, int, 0);
  2349. MODULE_PARM_DESC(tx_start_pt, DRV_NAME " transmit start point (0-3)");
  2350. module_param(pcnet32vlb, int, 0);
  2351. MODULE_PARM_DESC(pcnet32vlb, DRV_NAME " Vesa local bus (VLB) support (0/1)");
  2352. module_param_array(options, int, NULL, 0);
  2353. MODULE_PARM_DESC(options, DRV_NAME " initial option setting(s) (0-15)");
  2354. module_param_array(full_duplex, int, NULL, 0);
  2355. MODULE_PARM_DESC(full_duplex, DRV_NAME " full duplex setting(s) (1)");
  2356. /* Module Parameter for HomePNA cards added by Patrick Simmons, 2004 */
  2357. module_param_array(homepna, int, NULL, 0);
  2358. MODULE_PARM_DESC(homepna,
  2359. DRV_NAME
  2360. " mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet");
  2361. MODULE_AUTHOR("Thomas Bogendoerfer");
  2362. MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
  2363. MODULE_LICENSE("GPL");
  2364. #define PCNET32_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  2365. static int __init pcnet32_init_module(void)
  2366. {
  2367. printk(KERN_INFO "%s", version);
  2368. pcnet32_debug = netif_msg_init(debug, PCNET32_MSG_DEFAULT);
  2369. if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
  2370. tx_start = tx_start_pt;
  2371. /* find the PCI devices */
  2372. if (!pci_module_init(&pcnet32_driver))
  2373. pcnet32_have_pci = 1;
  2374. /* should we find any remaining VLbus devices ? */
  2375. if (pcnet32vlb)
  2376. pcnet32_probe_vlbus(pcnet32_portlist);
  2377. if (cards_found && (pcnet32_debug & NETIF_MSG_PROBE))
  2378. printk(KERN_INFO PFX "%d cards_found.\n", cards_found);
  2379. return (pcnet32_have_pci + cards_found) ? 0 : -ENODEV;
  2380. }
  2381. static void __exit pcnet32_cleanup_module(void)
  2382. {
  2383. struct net_device *next_dev;
  2384. while (pcnet32_dev) {
  2385. struct pcnet32_private *lp = pcnet32_dev->priv;
  2386. next_dev = lp->next;
  2387. unregister_netdev(pcnet32_dev);
  2388. pcnet32_free_ring(pcnet32_dev);
  2389. release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
  2390. pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
  2391. free_netdev(pcnet32_dev);
  2392. pcnet32_dev = next_dev;
  2393. }
  2394. if (pcnet32_have_pci)
  2395. pci_unregister_driver(&pcnet32_driver);
  2396. }
  2397. module_init(pcnet32_init_module);
  2398. module_exit(pcnet32_cleanup_module);
  2399. /*
  2400. * Local variables:
  2401. * c-indent-level: 4
  2402. * tab-width: 8
  2403. * End:
  2404. */