pcnet32.c 83 KB

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