pcnet32.c 81 KB

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