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