pcnet32.c 82 KB

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