hamachi.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  1. /* hamachi.c: A Packet Engines GNIC-II Gigabit Ethernet driver for Linux. */
  2. /*
  3. Written 1998-2000 by Donald Becker.
  4. Updates 2000 by Keith Underwood.
  5. This software may be used and distributed according to the terms of
  6. the GNU General Public License (GPL), incorporated herein by reference.
  7. Drivers based on or derived from this code fall under the GPL and must
  8. retain the authorship, copyright and license notice. This file is not
  9. a complete program and may only be used when the entire operating
  10. system is licensed under the GPL.
  11. The author may be reached as becker@scyld.com, or C/O
  12. Scyld Computing Corporation
  13. 410 Severn Ave., Suite 210
  14. Annapolis MD 21403
  15. This driver is for the Packet Engines GNIC-II PCI Gigabit Ethernet
  16. adapter.
  17. Support and updates available at
  18. http://www.scyld.com/network/hamachi.html
  19. [link no longer provides useful info -jgarzik]
  20. or
  21. http://www.parl.clemson.edu/~keithu/hamachi.html
  22. */
  23. #define DRV_NAME "hamachi"
  24. #define DRV_VERSION "2.1"
  25. #define DRV_RELDATE "Sept 11, 2006"
  26. /* A few user-configurable values. */
  27. static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
  28. #define final_version
  29. #define hamachi_debug debug
  30. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  31. static int max_interrupt_work = 40;
  32. static int mtu;
  33. /* Default values selected by testing on a dual processor PIII-450 */
  34. /* These six interrupt control parameters may be set directly when loading the
  35. * module, or through the rx_params and tx_params variables
  36. */
  37. static int max_rx_latency = 0x11;
  38. static int max_rx_gap = 0x05;
  39. static int min_rx_pkt = 0x18;
  40. static int max_tx_latency = 0x00;
  41. static int max_tx_gap = 0x00;
  42. static int min_tx_pkt = 0x30;
  43. /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
  44. -Setting to > 1518 causes all frames to be copied
  45. -Setting to 0 disables copies
  46. */
  47. static int rx_copybreak;
  48. /* An override for the hardware detection of bus width.
  49. Set to 1 to force 32 bit PCI bus detection. Set to 4 to force 64 bit.
  50. Add 2 to disable parity detection.
  51. */
  52. static int force32;
  53. /* Used to pass the media type, etc.
  54. These exist for driver interoperability.
  55. No media types are currently defined.
  56. - The lower 4 bits are reserved for the media type.
  57. - The next three bits may be set to one of the following:
  58. 0x00000000 : Autodetect PCI bus
  59. 0x00000010 : Force 32 bit PCI bus
  60. 0x00000020 : Disable parity detection
  61. 0x00000040 : Force 64 bit PCI bus
  62. Default is autodetect
  63. - The next bit can be used to force half-duplex. This is a bad
  64. idea since no known implementations implement half-duplex, and,
  65. in general, half-duplex for gigabit ethernet is a bad idea.
  66. 0x00000080 : Force half-duplex
  67. Default is full-duplex.
  68. - In the original driver, the ninth bit could be used to force
  69. full-duplex. Maintain that for compatibility
  70. 0x00000200 : Force full-duplex
  71. */
  72. #define MAX_UNITS 8 /* More are supported, limit only on options */
  73. static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
  74. static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
  75. /* The Hamachi chipset supports 3 parameters each for Rx and Tx
  76. * interruput management. Parameters will be loaded as specified into
  77. * the TxIntControl and RxIntControl registers.
  78. *
  79. * The registers are arranged as follows:
  80. * 23 - 16 15 - 8 7 - 0
  81. * _________________________________
  82. * | min_pkt | max_gap | max_latency |
  83. * ---------------------------------
  84. * min_pkt : The minimum number of packets processed between
  85. * interrupts.
  86. * max_gap : The maximum inter-packet gap in units of 8.192 us
  87. * max_latency : The absolute time between interrupts in units of 8.192 us
  88. *
  89. */
  90. static int rx_params[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
  91. static int tx_params[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
  92. /* Operational parameters that are set at compile time. */
  93. /* Keep the ring sizes a power of two for compile efficiency.
  94. The compiler will convert <unsigned>'%'<2^N> into a bit mask.
  95. Making the Tx ring too large decreases the effectiveness of channel
  96. bonding and packet priority.
  97. There are no ill effects from too-large receive rings, except for
  98. excessive memory usage */
  99. /* Empirically it appears that the Tx ring needs to be a little bigger
  100. for these Gbit adapters or you get into an overrun condition really
  101. easily. Also, things appear to work a bit better in back-to-back
  102. configurations if the Rx ring is 8 times the size of the Tx ring
  103. */
  104. #define TX_RING_SIZE 64
  105. #define RX_RING_SIZE 512
  106. #define TX_TOTAL_SIZE TX_RING_SIZE*sizeof(struct hamachi_desc)
  107. #define RX_TOTAL_SIZE RX_RING_SIZE*sizeof(struct hamachi_desc)
  108. /*
  109. * Enable netdev_ioctl. Added interrupt coalescing parameter adjustment.
  110. * 2/19/99 Pete Wyckoff <wyckoff@ca.sandia.gov>
  111. */
  112. /* play with 64-bit addrlen; seems to be a teensy bit slower --pw */
  113. /* #define ADDRLEN 64 */
  114. /*
  115. * RX_CHECKSUM turns on card-generated receive checksum generation for
  116. * TCP and UDP packets. Otherwise the upper layers do the calculation.
  117. * 3/10/1999 Pete Wyckoff <wyckoff@ca.sandia.gov>
  118. */
  119. #define RX_CHECKSUM
  120. /* Operational parameters that usually are not changed. */
  121. /* Time in jiffies before concluding the transmitter is hung. */
  122. #define TX_TIMEOUT (5*HZ)
  123. #include <linux/capability.h>
  124. #include <linux/module.h>
  125. #include <linux/kernel.h>
  126. #include <linux/string.h>
  127. #include <linux/timer.h>
  128. #include <linux/time.h>
  129. #include <linux/errno.h>
  130. #include <linux/ioport.h>
  131. #include <linux/interrupt.h>
  132. #include <linux/pci.h>
  133. #include <linux/init.h>
  134. #include <linux/ethtool.h>
  135. #include <linux/mii.h>
  136. #include <linux/netdevice.h>
  137. #include <linux/etherdevice.h>
  138. #include <linux/skbuff.h>
  139. #include <linux/ip.h>
  140. #include <linux/delay.h>
  141. #include <linux/bitops.h>
  142. #include <asm/uaccess.h>
  143. #include <asm/processor.h> /* Processor type for cache alignment. */
  144. #include <asm/io.h>
  145. #include <asm/unaligned.h>
  146. #include <asm/cache.h>
  147. static const char version[] =
  148. KERN_INFO DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " Written by Donald Becker\n"
  149. " Some modifications by Eric kasten <kasten@nscl.msu.edu>\n"
  150. " Further modifications by Keith Underwood <keithu@parl.clemson.edu>\n";
  151. /* IP_MF appears to be only defined in <netinet/ip.h>, however,
  152. we need it for hardware checksumming support. FYI... some of
  153. the definitions in <netinet/ip.h> conflict/duplicate those in
  154. other linux headers causing many compiler warnings.
  155. */
  156. #ifndef IP_MF
  157. #define IP_MF 0x2000 /* IP more frags from <netinet/ip.h> */
  158. #endif
  159. /* Define IP_OFFSET to be IPOPT_OFFSET */
  160. #ifndef IP_OFFSET
  161. #ifdef IPOPT_OFFSET
  162. #define IP_OFFSET IPOPT_OFFSET
  163. #else
  164. #define IP_OFFSET 2
  165. #endif
  166. #endif
  167. #define RUN_AT(x) (jiffies + (x))
  168. #ifndef ADDRLEN
  169. #define ADDRLEN 32
  170. #endif
  171. /* Condensed bus+endian portability operations. */
  172. #if ADDRLEN == 64
  173. #define cpu_to_leXX(addr) cpu_to_le64(addr)
  174. #define leXX_to_cpu(addr) le64_to_cpu(addr)
  175. #else
  176. #define cpu_to_leXX(addr) cpu_to_le32(addr)
  177. #define leXX_to_cpu(addr) le32_to_cpu(addr)
  178. #endif
  179. /*
  180. Theory of Operation
  181. I. Board Compatibility
  182. This device driver is designed for the Packet Engines "Hamachi"
  183. Gigabit Ethernet chip. The only PCA currently supported is the GNIC-II 64-bit
  184. 66Mhz PCI card.
  185. II. Board-specific settings
  186. No jumpers exist on the board. The chip supports software correction of
  187. various motherboard wiring errors, however this driver does not support
  188. that feature.
  189. III. Driver operation
  190. IIIa. Ring buffers
  191. The Hamachi uses a typical descriptor based bus-master architecture.
  192. The descriptor list is similar to that used by the Digital Tulip.
  193. This driver uses two statically allocated fixed-size descriptor lists
  194. formed into rings by a branch from the final descriptor to the beginning of
  195. the list. The ring sizes are set at compile time by RX/TX_RING_SIZE.
  196. This driver uses a zero-copy receive and transmit scheme similar my other
  197. network drivers.
  198. The driver allocates full frame size skbuffs for the Rx ring buffers at
  199. open() time and passes the skb->data field to the Hamachi as receive data
  200. buffers. When an incoming frame is less than RX_COPYBREAK bytes long,
  201. a fresh skbuff is allocated and the frame is copied to the new skbuff.
  202. When the incoming frame is larger, the skbuff is passed directly up the
  203. protocol stack and replaced by a newly allocated skbuff.
  204. The RX_COPYBREAK value is chosen to trade-off the memory wasted by
  205. using a full-sized skbuff for small frames vs. the copying costs of larger
  206. frames. Gigabit cards are typically used on generously configured machines
  207. and the underfilled buffers have negligible impact compared to the benefit of
  208. a single allocation size, so the default value of zero results in never
  209. copying packets.
  210. IIIb/c. Transmit/Receive Structure
  211. The Rx and Tx descriptor structure are straight-forward, with no historical
  212. baggage that must be explained. Unlike the awkward DBDMA structure, there
  213. are no unused fields or option bits that had only one allowable setting.
  214. Two details should be noted about the descriptors: The chip supports both 32
  215. bit and 64 bit address structures, and the length field is overwritten on
  216. the receive descriptors. The descriptor length is set in the control word
  217. for each channel. The development driver uses 32 bit addresses only, however
  218. 64 bit addresses may be enabled for 64 bit architectures e.g. the Alpha.
  219. IIId. Synchronization
  220. This driver is very similar to my other network drivers.
  221. The driver runs as two independent, single-threaded flows of control. One
  222. is the send-packet routine, which enforces single-threaded use by the
  223. dev->tbusy flag. The other thread is the interrupt handler, which is single
  224. threaded by the hardware and other software.
  225. The send packet thread has partial control over the Tx ring and 'dev->tbusy'
  226. flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next
  227. queue slot is empty, it clears the tbusy flag when finished otherwise it sets
  228. the 'hmp->tx_full' flag.
  229. The interrupt handler has exclusive control over the Rx ring and records stats
  230. from the Tx ring. After reaping the stats, it marks the Tx queue entry as
  231. empty by incrementing the dirty_tx mark. Iff the 'hmp->tx_full' flag is set, it
  232. clears both the tx_full and tbusy flags.
  233. IV. Notes
  234. Thanks to Kim Stearns of Packet Engines for providing a pair of GNIC-II boards.
  235. IVb. References
  236. Hamachi Engineering Design Specification, 5/15/97
  237. (Note: This version was marked "Confidential".)
  238. IVc. Errata
  239. None noted.
  240. V. Recent Changes
  241. 01/15/1999 EPK Enlargement of the TX and RX ring sizes. This appears
  242. to help avoid some stall conditions -- this needs further research.
  243. 01/15/1999 EPK Creation of the hamachi_tx function. This function cleans
  244. the Tx ring and is called from hamachi_start_xmit (this used to be
  245. called from hamachi_interrupt but it tends to delay execution of the
  246. interrupt handler and thus reduce bandwidth by reducing the latency
  247. between hamachi_rx()'s). Notably, some modification has been made so
  248. that the cleaning loop checks only to make sure that the DescOwn bit
  249. isn't set in the status flag since the card is not required
  250. to set the entire flag to zero after processing.
  251. 01/15/1999 EPK In the hamachi_start_tx function, the Tx ring full flag is
  252. checked before attempting to add a buffer to the ring. If the ring is full
  253. an attempt is made to free any dirty buffers and thus find space for
  254. the new buffer or the function returns non-zero which should case the
  255. scheduler to reschedule the buffer later.
  256. 01/15/1999 EPK Some adjustments were made to the chip initialization.
  257. End-to-end flow control should now be fully active and the interrupt
  258. algorithm vars have been changed. These could probably use further tuning.
  259. 01/15/1999 EPK Added the max_{rx,tx}_latency options. These are used to
  260. set the rx and tx latencies for the Hamachi interrupts. If you're having
  261. problems with network stalls, try setting these to higher values.
  262. Valid values are 0x00 through 0xff.
  263. 01/15/1999 EPK In general, the overall bandwidth has increased and
  264. latencies are better (sometimes by a factor of 2). Stalls are rare at
  265. this point, however there still appears to be a bug somewhere between the
  266. hardware and driver. TCP checksum errors under load also appear to be
  267. eliminated at this point.
  268. 01/18/1999 EPK Ensured that the DescEndRing bit was being set on both the
  269. Rx and Tx rings. This appears to have been affecting whether a particular
  270. peer-to-peer connection would hang under high load. I believe the Rx
  271. rings was typically getting set correctly, but the Tx ring wasn't getting
  272. the DescEndRing bit set during initialization. ??? Does this mean the
  273. hamachi card is using the DescEndRing in processing even if a particular
  274. slot isn't in use -- hypothetically, the card might be searching the
  275. entire Tx ring for slots with the DescOwn bit set and then processing
  276. them. If the DescEndRing bit isn't set, then it might just wander off
  277. through memory until it hits a chunk of data with that bit set
  278. and then looping back.
  279. 02/09/1999 EPK Added Michel Mueller's TxDMA Interrupt and Tx-timeout
  280. problem (TxCmd and RxCmd need only to be set when idle or stopped.
  281. 02/09/1999 EPK Added code to check/reset dev->tbusy in hamachi_interrupt.
  282. (Michel Mueller pointed out the ``permanently busy'' potential
  283. problem here).
  284. 02/22/1999 EPK Added Pete Wyckoff's ioctl to control the Tx/Rx latencies.
  285. 02/23/1999 EPK Verified that the interrupt status field bits for Tx were
  286. incorrectly defined and corrected (as per Michel Mueller).
  287. 02/23/1999 EPK Corrected the Tx full check to check that at least 4 slots
  288. were available before resetting the tbusy and tx_full flags
  289. (as per Michel Mueller).
  290. 03/11/1999 EPK Added Pete Wyckoff's hardware checksumming support.
  291. 12/31/1999 KDU Cleaned up assorted things and added Don's code to force
  292. 32 bit.
  293. 02/20/2000 KDU Some of the control was just plain odd. Cleaned up the
  294. hamachi_start_xmit() and hamachi_interrupt() code. There is still some
  295. re-structuring I would like to do.
  296. 03/01/2000 KDU Experimenting with a WIDE range of interrupt mitigation
  297. parameters on a dual P3-450 setup yielded the new default interrupt
  298. mitigation parameters. Tx should interrupt VERY infrequently due to
  299. Eric's scheme. Rx should be more often...
  300. 03/13/2000 KDU Added a patch to make the Rx Checksum code interact
  301. nicely with non-linux machines.
  302. 03/13/2000 KDU Experimented with some of the configuration values:
  303. -It seems that enabling PCI performance commands for descriptors
  304. (changing RxDMACtrl and TxDMACtrl lower nibble from 5 to D) has minimal
  305. performance impact for any of my tests. (ttcp, netpipe, netperf) I will
  306. leave them that way until I hear further feedback.
  307. -Increasing the PCI_LATENCY_TIMER to 130
  308. (2 + (burst size of 128 * (0 wait states + 1))) seems to slightly
  309. degrade performance. Leaving default at 64 pending further information.
  310. 03/14/2000 KDU Further tuning:
  311. -adjusted boguscnt in hamachi_rx() to depend on interrupt
  312. mitigation parameters chosen.
  313. -Selected a set of interrupt parameters based on some extensive testing.
  314. These may change with more testing.
  315. TO DO:
  316. -Consider borrowing from the acenic driver code to check PCI_COMMAND for
  317. PCI_COMMAND_INVALIDATE. Set maximum burst size to cache line size in
  318. that case.
  319. -fix the reset procedure. It doesn't quite work.
  320. */
  321. /* A few values that may be tweaked. */
  322. /* Size of each temporary Rx buffer, calculated as:
  323. * 1518 bytes (ethernet packet) + 2 bytes (to get 8 byte alignment for
  324. * the card) + 8 bytes of status info + 8 bytes for the Rx Checksum
  325. */
  326. #define PKT_BUF_SZ 1536
  327. /* For now, this is going to be set to the maximum size of an ethernet
  328. * packet. Eventually, we may want to make it a variable that is
  329. * related to the MTU
  330. */
  331. #define MAX_FRAME_SIZE 1518
  332. /* The rest of these values should never change. */
  333. static void hamachi_timer(unsigned long data);
  334. enum capability_flags {CanHaveMII=1, };
  335. static const struct chip_info {
  336. u16 vendor_id, device_id, device_id_mask, pad;
  337. const char *name;
  338. void (*media_timer)(unsigned long data);
  339. int flags;
  340. } chip_tbl[] = {
  341. {0x1318, 0x0911, 0xffff, 0, "Hamachi GNIC-II", hamachi_timer, 0},
  342. {0,},
  343. };
  344. /* Offsets to the Hamachi registers. Various sizes. */
  345. enum hamachi_offsets {
  346. TxDMACtrl=0x00, TxCmd=0x04, TxStatus=0x06, TxPtr=0x08, TxCurPtr=0x10,
  347. RxDMACtrl=0x20, RxCmd=0x24, RxStatus=0x26, RxPtr=0x28, RxCurPtr=0x30,
  348. PCIClkMeas=0x060, MiscStatus=0x066, ChipRev=0x68, ChipReset=0x06B,
  349. LEDCtrl=0x06C, VirtualJumpers=0x06D, GPIO=0x6E,
  350. TxChecksum=0x074, RxChecksum=0x076,
  351. TxIntrCtrl=0x078, RxIntrCtrl=0x07C,
  352. InterruptEnable=0x080, InterruptClear=0x084, IntrStatus=0x088,
  353. EventStatus=0x08C,
  354. MACCnfg=0x0A0, FrameGap0=0x0A2, FrameGap1=0x0A4,
  355. /* See enum MII_offsets below. */
  356. MACCnfg2=0x0B0, RxDepth=0x0B8, FlowCtrl=0x0BC, MaxFrameSize=0x0CE,
  357. AddrMode=0x0D0, StationAddr=0x0D2,
  358. /* Gigabit AutoNegotiation. */
  359. ANCtrl=0x0E0, ANStatus=0x0E2, ANXchngCtrl=0x0E4, ANAdvertise=0x0E8,
  360. ANLinkPartnerAbility=0x0EA,
  361. EECmdStatus=0x0F0, EEData=0x0F1, EEAddr=0x0F2,
  362. FIFOcfg=0x0F8,
  363. };
  364. /* Offsets to the MII-mode registers. */
  365. enum MII_offsets {
  366. MII_Cmd=0xA6, MII_Addr=0xA8, MII_Wr_Data=0xAA, MII_Rd_Data=0xAC,
  367. MII_Status=0xAE,
  368. };
  369. /* Bits in the interrupt status/mask registers. */
  370. enum intr_status_bits {
  371. IntrRxDone=0x01, IntrRxPCIFault=0x02, IntrRxPCIErr=0x04,
  372. IntrTxDone=0x100, IntrTxPCIFault=0x200, IntrTxPCIErr=0x400,
  373. LinkChange=0x10000, NegotiationChange=0x20000, StatsMax=0x40000, };
  374. /* The Hamachi Rx and Tx buffer descriptors. */
  375. struct hamachi_desc {
  376. __le32 status_n_length;
  377. #if ADDRLEN == 64
  378. u32 pad;
  379. __le64 addr;
  380. #else
  381. __le32 addr;
  382. #endif
  383. };
  384. /* Bits in hamachi_desc.status_n_length */
  385. enum desc_status_bits {
  386. DescOwn=0x80000000, DescEndPacket=0x40000000, DescEndRing=0x20000000,
  387. DescIntr=0x10000000,
  388. };
  389. #define PRIV_ALIGN 15 /* Required alignment mask */
  390. #define MII_CNT 4
  391. struct hamachi_private {
  392. /* Descriptor rings first for alignment. Tx requires a second descriptor
  393. for status. */
  394. struct hamachi_desc *rx_ring;
  395. struct hamachi_desc *tx_ring;
  396. struct sk_buff* rx_skbuff[RX_RING_SIZE];
  397. struct sk_buff* tx_skbuff[TX_RING_SIZE];
  398. dma_addr_t tx_ring_dma;
  399. dma_addr_t rx_ring_dma;
  400. struct timer_list timer; /* Media selection timer. */
  401. /* Frequently used and paired value: keep adjacent for cache effect. */
  402. spinlock_t lock;
  403. int chip_id;
  404. unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */
  405. unsigned int cur_tx, dirty_tx;
  406. unsigned int rx_buf_sz; /* Based on MTU+slack. */
  407. unsigned int tx_full:1; /* The Tx queue is full. */
  408. unsigned int duplex_lock:1;
  409. unsigned int default_port:4; /* Last dev->if_port value. */
  410. /* MII transceiver section. */
  411. int mii_cnt; /* MII device addresses. */
  412. struct mii_if_info mii_if; /* MII lib hooks/info */
  413. unsigned char phys[MII_CNT]; /* MII device addresses, only first one used. */
  414. u32 rx_int_var, tx_int_var; /* interrupt control variables */
  415. u32 option; /* Hold on to a copy of the options */
  416. struct pci_dev *pci_dev;
  417. void __iomem *base;
  418. };
  419. MODULE_AUTHOR("Donald Becker <becker@scyld.com>, Eric Kasten <kasten@nscl.msu.edu>, Keith Underwood <keithu@parl.clemson.edu>");
  420. MODULE_DESCRIPTION("Packet Engines 'Hamachi' GNIC-II Gigabit Ethernet driver");
  421. MODULE_LICENSE("GPL");
  422. module_param(max_interrupt_work, int, 0);
  423. module_param(mtu, int, 0);
  424. module_param(debug, int, 0);
  425. module_param(min_rx_pkt, int, 0);
  426. module_param(max_rx_gap, int, 0);
  427. module_param(max_rx_latency, int, 0);
  428. module_param(min_tx_pkt, int, 0);
  429. module_param(max_tx_gap, int, 0);
  430. module_param(max_tx_latency, int, 0);
  431. module_param(rx_copybreak, int, 0);
  432. module_param_array(rx_params, int, NULL, 0);
  433. module_param_array(tx_params, int, NULL, 0);
  434. module_param_array(options, int, NULL, 0);
  435. module_param_array(full_duplex, int, NULL, 0);
  436. module_param(force32, int, 0);
  437. MODULE_PARM_DESC(max_interrupt_work, "GNIC-II maximum events handled per interrupt");
  438. MODULE_PARM_DESC(mtu, "GNIC-II MTU (all boards)");
  439. MODULE_PARM_DESC(debug, "GNIC-II debug level (0-7)");
  440. MODULE_PARM_DESC(min_rx_pkt, "GNIC-II minimum Rx packets processed between interrupts");
  441. MODULE_PARM_DESC(max_rx_gap, "GNIC-II maximum Rx inter-packet gap in 8.192 microsecond units");
  442. MODULE_PARM_DESC(max_rx_latency, "GNIC-II time between Rx interrupts in 8.192 microsecond units");
  443. MODULE_PARM_DESC(min_tx_pkt, "GNIC-II minimum Tx packets processed between interrupts");
  444. MODULE_PARM_DESC(max_tx_gap, "GNIC-II maximum Tx inter-packet gap in 8.192 microsecond units");
  445. MODULE_PARM_DESC(max_tx_latency, "GNIC-II time between Tx interrupts in 8.192 microsecond units");
  446. MODULE_PARM_DESC(rx_copybreak, "GNIC-II copy breakpoint for copy-only-tiny-frames");
  447. MODULE_PARM_DESC(rx_params, "GNIC-II min_rx_pkt+max_rx_gap+max_rx_latency");
  448. MODULE_PARM_DESC(tx_params, "GNIC-II min_tx_pkt+max_tx_gap+max_tx_latency");
  449. MODULE_PARM_DESC(options, "GNIC-II Bits 0-3: media type, bits 4-6: as force32, bit 7: half duplex, bit 9 full duplex");
  450. MODULE_PARM_DESC(full_duplex, "GNIC-II full duplex setting(s) (1)");
  451. MODULE_PARM_DESC(force32, "GNIC-II: Bit 0: 32 bit PCI, bit 1: disable parity, bit 2: 64 bit PCI (all boards)");
  452. static int read_eeprom(void __iomem *ioaddr, int location);
  453. static int mdio_read(struct net_device *dev, int phy_id, int location);
  454. static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
  455. static int hamachi_open(struct net_device *dev);
  456. static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  457. static void hamachi_timer(unsigned long data);
  458. static void hamachi_tx_timeout(struct net_device *dev);
  459. static void hamachi_init_ring(struct net_device *dev);
  460. static netdev_tx_t hamachi_start_xmit(struct sk_buff *skb,
  461. struct net_device *dev);
  462. static irqreturn_t hamachi_interrupt(int irq, void *dev_instance);
  463. static int hamachi_rx(struct net_device *dev);
  464. static inline int hamachi_tx(struct net_device *dev);
  465. static void hamachi_error(struct net_device *dev, int intr_status);
  466. static int hamachi_close(struct net_device *dev);
  467. static struct net_device_stats *hamachi_get_stats(struct net_device *dev);
  468. static void set_rx_mode(struct net_device *dev);
  469. static const struct ethtool_ops ethtool_ops;
  470. static const struct ethtool_ops ethtool_ops_no_mii;
  471. static const struct net_device_ops hamachi_netdev_ops = {
  472. .ndo_open = hamachi_open,
  473. .ndo_stop = hamachi_close,
  474. .ndo_start_xmit = hamachi_start_xmit,
  475. .ndo_get_stats = hamachi_get_stats,
  476. .ndo_set_rx_mode = set_rx_mode,
  477. .ndo_change_mtu = eth_change_mtu,
  478. .ndo_validate_addr = eth_validate_addr,
  479. .ndo_set_mac_address = eth_mac_addr,
  480. .ndo_tx_timeout = hamachi_tx_timeout,
  481. .ndo_do_ioctl = netdev_ioctl,
  482. };
  483. static int hamachi_init_one(struct pci_dev *pdev,
  484. const struct pci_device_id *ent)
  485. {
  486. struct hamachi_private *hmp;
  487. int option, i, rx_int_var, tx_int_var, boguscnt;
  488. int chip_id = ent->driver_data;
  489. int irq;
  490. void __iomem *ioaddr;
  491. unsigned long base;
  492. static int card_idx;
  493. struct net_device *dev;
  494. void *ring_space;
  495. dma_addr_t ring_dma;
  496. int ret = -ENOMEM;
  497. /* when built into the kernel, we only print version if device is found */
  498. #ifndef MODULE
  499. static int printed_version;
  500. if (!printed_version++)
  501. printk(version);
  502. #endif
  503. if (pci_enable_device(pdev)) {
  504. ret = -EIO;
  505. goto err_out;
  506. }
  507. base = pci_resource_start(pdev, 0);
  508. #ifdef __alpha__ /* Really "64 bit addrs" */
  509. base |= (pci_resource_start(pdev, 1) << 32);
  510. #endif
  511. pci_set_master(pdev);
  512. i = pci_request_regions(pdev, DRV_NAME);
  513. if (i)
  514. return i;
  515. irq = pdev->irq;
  516. ioaddr = ioremap(base, 0x400);
  517. if (!ioaddr)
  518. goto err_out_release;
  519. dev = alloc_etherdev(sizeof(struct hamachi_private));
  520. if (!dev)
  521. goto err_out_iounmap;
  522. SET_NETDEV_DEV(dev, &pdev->dev);
  523. for (i = 0; i < 6; i++)
  524. dev->dev_addr[i] = 1 ? read_eeprom(ioaddr, 4 + i)
  525. : readb(ioaddr + StationAddr + i);
  526. #if ! defined(final_version)
  527. if (hamachi_debug > 4)
  528. for (i = 0; i < 0x10; i++)
  529. printk("%2.2x%s",
  530. read_eeprom(ioaddr, i), i % 16 != 15 ? " " : "\n");
  531. #endif
  532. hmp = netdev_priv(dev);
  533. spin_lock_init(&hmp->lock);
  534. hmp->mii_if.dev = dev;
  535. hmp->mii_if.mdio_read = mdio_read;
  536. hmp->mii_if.mdio_write = mdio_write;
  537. hmp->mii_if.phy_id_mask = 0x1f;
  538. hmp->mii_if.reg_num_mask = 0x1f;
  539. ring_space = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
  540. if (!ring_space)
  541. goto err_out_cleardev;
  542. hmp->tx_ring = ring_space;
  543. hmp->tx_ring_dma = ring_dma;
  544. ring_space = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
  545. if (!ring_space)
  546. goto err_out_unmap_tx;
  547. hmp->rx_ring = ring_space;
  548. hmp->rx_ring_dma = ring_dma;
  549. /* Check for options being passed in */
  550. option = card_idx < MAX_UNITS ? options[card_idx] : 0;
  551. if (dev->mem_start)
  552. option = dev->mem_start;
  553. /* If the bus size is misidentified, do the following. */
  554. force32 = force32 ? force32 :
  555. ((option >= 0) ? ((option & 0x00000070) >> 4) : 0 );
  556. if (force32)
  557. writeb(force32, ioaddr + VirtualJumpers);
  558. /* Hmmm, do we really need to reset the chip???. */
  559. writeb(0x01, ioaddr + ChipReset);
  560. /* After a reset, the clock speed measurement of the PCI bus will not
  561. * be valid for a moment. Wait for a little while until it is. If
  562. * it takes more than 10ms, forget it.
  563. */
  564. udelay(10);
  565. i = readb(ioaddr + PCIClkMeas);
  566. for (boguscnt = 0; (!(i & 0x080)) && boguscnt < 1000; boguscnt++){
  567. udelay(10);
  568. i = readb(ioaddr + PCIClkMeas);
  569. }
  570. hmp->base = ioaddr;
  571. pci_set_drvdata(pdev, dev);
  572. hmp->chip_id = chip_id;
  573. hmp->pci_dev = pdev;
  574. /* The lower four bits are the media type. */
  575. if (option > 0) {
  576. hmp->option = option;
  577. if (option & 0x200)
  578. hmp->mii_if.full_duplex = 1;
  579. else if (option & 0x080)
  580. hmp->mii_if.full_duplex = 0;
  581. hmp->default_port = option & 15;
  582. if (hmp->default_port)
  583. hmp->mii_if.force_media = 1;
  584. }
  585. if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
  586. hmp->mii_if.full_duplex = 1;
  587. /* lock the duplex mode if someone specified a value */
  588. if (hmp->mii_if.full_duplex || (option & 0x080))
  589. hmp->duplex_lock = 1;
  590. /* Set interrupt tuning parameters */
  591. max_rx_latency = max_rx_latency & 0x00ff;
  592. max_rx_gap = max_rx_gap & 0x00ff;
  593. min_rx_pkt = min_rx_pkt & 0x00ff;
  594. max_tx_latency = max_tx_latency & 0x00ff;
  595. max_tx_gap = max_tx_gap & 0x00ff;
  596. min_tx_pkt = min_tx_pkt & 0x00ff;
  597. rx_int_var = card_idx < MAX_UNITS ? rx_params[card_idx] : -1;
  598. tx_int_var = card_idx < MAX_UNITS ? tx_params[card_idx] : -1;
  599. hmp->rx_int_var = rx_int_var >= 0 ? rx_int_var :
  600. (min_rx_pkt << 16 | max_rx_gap << 8 | max_rx_latency);
  601. hmp->tx_int_var = tx_int_var >= 0 ? tx_int_var :
  602. (min_tx_pkt << 16 | max_tx_gap << 8 | max_tx_latency);
  603. /* The Hamachi-specific entries in the device structure. */
  604. dev->netdev_ops = &hamachi_netdev_ops;
  605. dev->ethtool_ops = (chip_tbl[hmp->chip_id].flags & CanHaveMII) ?
  606. &ethtool_ops : &ethtool_ops_no_mii;
  607. dev->watchdog_timeo = TX_TIMEOUT;
  608. if (mtu)
  609. dev->mtu = mtu;
  610. i = register_netdev(dev);
  611. if (i) {
  612. ret = i;
  613. goto err_out_unmap_rx;
  614. }
  615. printk(KERN_INFO "%s: %s type %x at %p, %pM, IRQ %d.\n",
  616. dev->name, chip_tbl[chip_id].name, readl(ioaddr + ChipRev),
  617. ioaddr, dev->dev_addr, irq);
  618. i = readb(ioaddr + PCIClkMeas);
  619. printk(KERN_INFO "%s: %d-bit %d Mhz PCI bus (%d), Virtual Jumpers "
  620. "%2.2x, LPA %4.4x.\n",
  621. dev->name, readw(ioaddr + MiscStatus) & 1 ? 64 : 32,
  622. i ? 2000/(i&0x7f) : 0, i&0x7f, (int)readb(ioaddr + VirtualJumpers),
  623. readw(ioaddr + ANLinkPartnerAbility));
  624. if (chip_tbl[hmp->chip_id].flags & CanHaveMII) {
  625. int phy, phy_idx = 0;
  626. for (phy = 0; phy < 32 && phy_idx < MII_CNT; phy++) {
  627. int mii_status = mdio_read(dev, phy, MII_BMSR);
  628. if (mii_status != 0xffff &&
  629. mii_status != 0x0000) {
  630. hmp->phys[phy_idx++] = phy;
  631. hmp->mii_if.advertising = mdio_read(dev, phy, MII_ADVERTISE);
  632. printk(KERN_INFO "%s: MII PHY found at address %d, status "
  633. "0x%4.4x advertising %4.4x.\n",
  634. dev->name, phy, mii_status, hmp->mii_if.advertising);
  635. }
  636. }
  637. hmp->mii_cnt = phy_idx;
  638. if (hmp->mii_cnt > 0)
  639. hmp->mii_if.phy_id = hmp->phys[0];
  640. else
  641. memset(&hmp->mii_if, 0, sizeof(hmp->mii_if));
  642. }
  643. /* Configure gigabit autonegotiation. */
  644. writew(0x0400, ioaddr + ANXchngCtrl); /* Enable legacy links. */
  645. writew(0x08e0, ioaddr + ANAdvertise); /* Set our advertise word. */
  646. writew(0x1000, ioaddr + ANCtrl); /* Enable negotiation */
  647. card_idx++;
  648. return 0;
  649. err_out_unmap_rx:
  650. pci_free_consistent(pdev, RX_TOTAL_SIZE, hmp->rx_ring,
  651. hmp->rx_ring_dma);
  652. err_out_unmap_tx:
  653. pci_free_consistent(pdev, TX_TOTAL_SIZE, hmp->tx_ring,
  654. hmp->tx_ring_dma);
  655. err_out_cleardev:
  656. free_netdev (dev);
  657. err_out_iounmap:
  658. iounmap(ioaddr);
  659. err_out_release:
  660. pci_release_regions(pdev);
  661. err_out:
  662. return ret;
  663. }
  664. static int read_eeprom(void __iomem *ioaddr, int location)
  665. {
  666. int bogus_cnt = 1000;
  667. /* We should check busy first - per docs -KDU */
  668. while ((readb(ioaddr + EECmdStatus) & 0x40) && --bogus_cnt > 0);
  669. writew(location, ioaddr + EEAddr);
  670. writeb(0x02, ioaddr + EECmdStatus);
  671. bogus_cnt = 1000;
  672. while ((readb(ioaddr + EECmdStatus) & 0x40) && --bogus_cnt > 0);
  673. if (hamachi_debug > 5)
  674. printk(" EEPROM status is %2.2x after %d ticks.\n",
  675. (int)readb(ioaddr + EECmdStatus), 1000- bogus_cnt);
  676. return readb(ioaddr + EEData);
  677. }
  678. /* MII Managemen Data I/O accesses.
  679. These routines assume the MDIO controller is idle, and do not exit until
  680. the command is finished. */
  681. static int mdio_read(struct net_device *dev, int phy_id, int location)
  682. {
  683. struct hamachi_private *hmp = netdev_priv(dev);
  684. void __iomem *ioaddr = hmp->base;
  685. int i;
  686. /* We should check busy first - per docs -KDU */
  687. for (i = 10000; i >= 0; i--)
  688. if ((readw(ioaddr + MII_Status) & 1) == 0)
  689. break;
  690. writew((phy_id<<8) + location, ioaddr + MII_Addr);
  691. writew(0x0001, ioaddr + MII_Cmd);
  692. for (i = 10000; i >= 0; i--)
  693. if ((readw(ioaddr + MII_Status) & 1) == 0)
  694. break;
  695. return readw(ioaddr + MII_Rd_Data);
  696. }
  697. static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
  698. {
  699. struct hamachi_private *hmp = netdev_priv(dev);
  700. void __iomem *ioaddr = hmp->base;
  701. int i;
  702. /* We should check busy first - per docs -KDU */
  703. for (i = 10000; i >= 0; i--)
  704. if ((readw(ioaddr + MII_Status) & 1) == 0)
  705. break;
  706. writew((phy_id<<8) + location, ioaddr + MII_Addr);
  707. writew(value, ioaddr + MII_Wr_Data);
  708. /* Wait for the command to finish. */
  709. for (i = 10000; i >= 0; i--)
  710. if ((readw(ioaddr + MII_Status) & 1) == 0)
  711. break;
  712. }
  713. static int hamachi_open(struct net_device *dev)
  714. {
  715. struct hamachi_private *hmp = netdev_priv(dev);
  716. void __iomem *ioaddr = hmp->base;
  717. int i;
  718. u32 rx_int_var, tx_int_var;
  719. u16 fifo_info;
  720. i = request_irq(hmp->pci_dev->irq, hamachi_interrupt, IRQF_SHARED,
  721. dev->name, dev);
  722. if (i)
  723. return i;
  724. hamachi_init_ring(dev);
  725. #if ADDRLEN == 64
  726. /* writellll anyone ? */
  727. writel(hmp->rx_ring_dma, ioaddr + RxPtr);
  728. writel(hmp->rx_ring_dma >> 32, ioaddr + RxPtr + 4);
  729. writel(hmp->tx_ring_dma, ioaddr + TxPtr);
  730. writel(hmp->tx_ring_dma >> 32, ioaddr + TxPtr + 4);
  731. #else
  732. writel(hmp->rx_ring_dma, ioaddr + RxPtr);
  733. writel(hmp->tx_ring_dma, ioaddr + TxPtr);
  734. #endif
  735. /* TODO: It would make sense to organize this as words since the card
  736. * documentation does. -KDU
  737. */
  738. for (i = 0; i < 6; i++)
  739. writeb(dev->dev_addr[i], ioaddr + StationAddr + i);
  740. /* Initialize other registers: with so many this eventually this will
  741. converted to an offset/value list. */
  742. /* Configure the FIFO */
  743. fifo_info = (readw(ioaddr + GPIO) & 0x00C0) >> 6;
  744. switch (fifo_info){
  745. case 0 :
  746. /* No FIFO */
  747. writew(0x0000, ioaddr + FIFOcfg);
  748. break;
  749. case 1 :
  750. /* Configure the FIFO for 512K external, 16K used for Tx. */
  751. writew(0x0028, ioaddr + FIFOcfg);
  752. break;
  753. case 2 :
  754. /* Configure the FIFO for 1024 external, 32K used for Tx. */
  755. writew(0x004C, ioaddr + FIFOcfg);
  756. break;
  757. case 3 :
  758. /* Configure the FIFO for 2048 external, 32K used for Tx. */
  759. writew(0x006C, ioaddr + FIFOcfg);
  760. break;
  761. default :
  762. printk(KERN_WARNING "%s: Unsupported external memory config!\n",
  763. dev->name);
  764. /* Default to no FIFO */
  765. writew(0x0000, ioaddr + FIFOcfg);
  766. break;
  767. }
  768. if (dev->if_port == 0)
  769. dev->if_port = hmp->default_port;
  770. /* Setting the Rx mode will start the Rx process. */
  771. /* If someone didn't choose a duplex, default to full-duplex */
  772. if (hmp->duplex_lock != 1)
  773. hmp->mii_if.full_duplex = 1;
  774. /* always 1, takes no more time to do it */
  775. writew(0x0001, ioaddr + RxChecksum);
  776. writew(0x0000, ioaddr + TxChecksum);
  777. writew(0x8000, ioaddr + MACCnfg); /* Soft reset the MAC */
  778. writew(0x215F, ioaddr + MACCnfg);
  779. writew(0x000C, ioaddr + FrameGap0);
  780. /* WHAT?!?!? Why isn't this documented somewhere? -KDU */
  781. writew(0x1018, ioaddr + FrameGap1);
  782. /* Why do we enable receives/transmits here? -KDU */
  783. writew(0x0780, ioaddr + MACCnfg2); /* Upper 16 bits control LEDs. */
  784. /* Enable automatic generation of flow control frames, period 0xffff. */
  785. writel(0x0030FFFF, ioaddr + FlowCtrl);
  786. writew(MAX_FRAME_SIZE, ioaddr + MaxFrameSize); /* dev->mtu+14 ??? */
  787. /* Enable legacy links. */
  788. writew(0x0400, ioaddr + ANXchngCtrl); /* Enable legacy links. */
  789. /* Initial Link LED to blinking red. */
  790. writeb(0x03, ioaddr + LEDCtrl);
  791. /* Configure interrupt mitigation. This has a great effect on
  792. performance, so systems tuning should start here!. */
  793. rx_int_var = hmp->rx_int_var;
  794. tx_int_var = hmp->tx_int_var;
  795. if (hamachi_debug > 1) {
  796. printk("max_tx_latency: %d, max_tx_gap: %d, min_tx_pkt: %d\n",
  797. tx_int_var & 0x00ff, (tx_int_var & 0x00ff00) >> 8,
  798. (tx_int_var & 0x00ff0000) >> 16);
  799. printk("max_rx_latency: %d, max_rx_gap: %d, min_rx_pkt: %d\n",
  800. rx_int_var & 0x00ff, (rx_int_var & 0x00ff00) >> 8,
  801. (rx_int_var & 0x00ff0000) >> 16);
  802. printk("rx_int_var: %x, tx_int_var: %x\n", rx_int_var, tx_int_var);
  803. }
  804. writel(tx_int_var, ioaddr + TxIntrCtrl);
  805. writel(rx_int_var, ioaddr + RxIntrCtrl);
  806. set_rx_mode(dev);
  807. netif_start_queue(dev);
  808. /* Enable interrupts by setting the interrupt mask. */
  809. writel(0x80878787, ioaddr + InterruptEnable);
  810. writew(0x0000, ioaddr + EventStatus); /* Clear non-interrupting events */
  811. /* Configure and start the DMA channels. */
  812. /* Burst sizes are in the low three bits: size = 4<<(val&7) */
  813. #if ADDRLEN == 64
  814. writew(0x005D, ioaddr + RxDMACtrl); /* 128 dword bursts */
  815. writew(0x005D, ioaddr + TxDMACtrl);
  816. #else
  817. writew(0x001D, ioaddr + RxDMACtrl);
  818. writew(0x001D, ioaddr + TxDMACtrl);
  819. #endif
  820. writew(0x0001, ioaddr + RxCmd);
  821. if (hamachi_debug > 2) {
  822. printk(KERN_DEBUG "%s: Done hamachi_open(), status: Rx %x Tx %x.\n",
  823. dev->name, readw(ioaddr + RxStatus), readw(ioaddr + TxStatus));
  824. }
  825. /* Set the timer to check for link beat. */
  826. init_timer(&hmp->timer);
  827. hmp->timer.expires = RUN_AT((24*HZ)/10); /* 2.4 sec. */
  828. hmp->timer.data = (unsigned long)dev;
  829. hmp->timer.function = hamachi_timer; /* timer handler */
  830. add_timer(&hmp->timer);
  831. return 0;
  832. }
  833. static inline int hamachi_tx(struct net_device *dev)
  834. {
  835. struct hamachi_private *hmp = netdev_priv(dev);
  836. /* Update the dirty pointer until we find an entry that is
  837. still owned by the card */
  838. for (; hmp->cur_tx - hmp->dirty_tx > 0; hmp->dirty_tx++) {
  839. int entry = hmp->dirty_tx % TX_RING_SIZE;
  840. struct sk_buff *skb;
  841. if (hmp->tx_ring[entry].status_n_length & cpu_to_le32(DescOwn))
  842. break;
  843. /* Free the original skb. */
  844. skb = hmp->tx_skbuff[entry];
  845. if (skb) {
  846. pci_unmap_single(hmp->pci_dev,
  847. leXX_to_cpu(hmp->tx_ring[entry].addr),
  848. skb->len, PCI_DMA_TODEVICE);
  849. dev_kfree_skb(skb);
  850. hmp->tx_skbuff[entry] = NULL;
  851. }
  852. hmp->tx_ring[entry].status_n_length = 0;
  853. if (entry >= TX_RING_SIZE-1)
  854. hmp->tx_ring[TX_RING_SIZE-1].status_n_length |=
  855. cpu_to_le32(DescEndRing);
  856. dev->stats.tx_packets++;
  857. }
  858. return 0;
  859. }
  860. static void hamachi_timer(unsigned long data)
  861. {
  862. struct net_device *dev = (struct net_device *)data;
  863. struct hamachi_private *hmp = netdev_priv(dev);
  864. void __iomem *ioaddr = hmp->base;
  865. int next_tick = 10*HZ;
  866. if (hamachi_debug > 2) {
  867. printk(KERN_INFO "%s: Hamachi Autonegotiation status %4.4x, LPA "
  868. "%4.4x.\n", dev->name, readw(ioaddr + ANStatus),
  869. readw(ioaddr + ANLinkPartnerAbility));
  870. printk(KERN_INFO "%s: Autonegotiation regs %4.4x %4.4x %4.4x "
  871. "%4.4x %4.4x %4.4x.\n", dev->name,
  872. readw(ioaddr + 0x0e0),
  873. readw(ioaddr + 0x0e2),
  874. readw(ioaddr + 0x0e4),
  875. readw(ioaddr + 0x0e6),
  876. readw(ioaddr + 0x0e8),
  877. readw(ioaddr + 0x0eA));
  878. }
  879. /* We could do something here... nah. */
  880. hmp->timer.expires = RUN_AT(next_tick);
  881. add_timer(&hmp->timer);
  882. }
  883. static void hamachi_tx_timeout(struct net_device *dev)
  884. {
  885. int i;
  886. struct hamachi_private *hmp = netdev_priv(dev);
  887. void __iomem *ioaddr = hmp->base;
  888. printk(KERN_WARNING "%s: Hamachi transmit timed out, status %8.8x,"
  889. " resetting...\n", dev->name, (int)readw(ioaddr + TxStatus));
  890. {
  891. printk(KERN_DEBUG " Rx ring %p: ", hmp->rx_ring);
  892. for (i = 0; i < RX_RING_SIZE; i++)
  893. printk(KERN_CONT " %8.8x",
  894. le32_to_cpu(hmp->rx_ring[i].status_n_length));
  895. printk(KERN_CONT "\n");
  896. printk(KERN_DEBUG" Tx ring %p: ", hmp->tx_ring);
  897. for (i = 0; i < TX_RING_SIZE; i++)
  898. printk(KERN_CONT " %4.4x",
  899. le32_to_cpu(hmp->tx_ring[i].status_n_length));
  900. printk(KERN_CONT "\n");
  901. }
  902. /* Reinit the hardware and make sure the Rx and Tx processes
  903. are up and running.
  904. */
  905. dev->if_port = 0;
  906. /* The right way to do Reset. -KDU
  907. * -Clear OWN bit in all Rx/Tx descriptors
  908. * -Wait 50 uS for channels to go idle
  909. * -Turn off MAC receiver
  910. * -Issue Reset
  911. */
  912. for (i = 0; i < RX_RING_SIZE; i++)
  913. hmp->rx_ring[i].status_n_length &= cpu_to_le32(~DescOwn);
  914. /* Presume that all packets in the Tx queue are gone if we have to
  915. * re-init the hardware.
  916. */
  917. for (i = 0; i < TX_RING_SIZE; i++){
  918. struct sk_buff *skb;
  919. if (i >= TX_RING_SIZE - 1)
  920. hmp->tx_ring[i].status_n_length =
  921. cpu_to_le32(DescEndRing) |
  922. (hmp->tx_ring[i].status_n_length &
  923. cpu_to_le32(0x0000ffff));
  924. else
  925. hmp->tx_ring[i].status_n_length &= cpu_to_le32(0x0000ffff);
  926. skb = hmp->tx_skbuff[i];
  927. if (skb){
  928. pci_unmap_single(hmp->pci_dev, leXX_to_cpu(hmp->tx_ring[i].addr),
  929. skb->len, PCI_DMA_TODEVICE);
  930. dev_kfree_skb(skb);
  931. hmp->tx_skbuff[i] = NULL;
  932. }
  933. }
  934. udelay(60); /* Sleep 60 us just for safety sake */
  935. writew(0x0002, ioaddr + RxCmd); /* STOP Rx */
  936. writeb(0x01, ioaddr + ChipReset); /* Reinit the hardware */
  937. hmp->tx_full = 0;
  938. hmp->cur_rx = hmp->cur_tx = 0;
  939. hmp->dirty_rx = hmp->dirty_tx = 0;
  940. /* Rx packets are also presumed lost; however, we need to make sure a
  941. * ring of buffers is in tact. -KDU
  942. */
  943. for (i = 0; i < RX_RING_SIZE; i++){
  944. struct sk_buff *skb = hmp->rx_skbuff[i];
  945. if (skb){
  946. pci_unmap_single(hmp->pci_dev,
  947. leXX_to_cpu(hmp->rx_ring[i].addr),
  948. hmp->rx_buf_sz, PCI_DMA_FROMDEVICE);
  949. dev_kfree_skb(skb);
  950. hmp->rx_skbuff[i] = NULL;
  951. }
  952. }
  953. /* Fill in the Rx buffers. Handle allocation failure gracefully. */
  954. for (i = 0; i < RX_RING_SIZE; i++) {
  955. struct sk_buff *skb;
  956. skb = netdev_alloc_skb_ip_align(dev, hmp->rx_buf_sz);
  957. hmp->rx_skbuff[i] = skb;
  958. if (skb == NULL)
  959. break;
  960. hmp->rx_ring[i].addr = cpu_to_leXX(pci_map_single(hmp->pci_dev,
  961. skb->data, hmp->rx_buf_sz, PCI_DMA_FROMDEVICE));
  962. hmp->rx_ring[i].status_n_length = cpu_to_le32(DescOwn |
  963. DescEndPacket | DescIntr | (hmp->rx_buf_sz - 2));
  964. }
  965. hmp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
  966. /* Mark the last entry as wrapping the ring. */
  967. hmp->rx_ring[RX_RING_SIZE-1].status_n_length |= cpu_to_le32(DescEndRing);
  968. /* Trigger an immediate transmit demand. */
  969. netif_trans_update(dev); /* prevent tx timeout */
  970. dev->stats.tx_errors++;
  971. /* Restart the chip's Tx/Rx processes . */
  972. writew(0x0002, ioaddr + TxCmd); /* STOP Tx */
  973. writew(0x0001, ioaddr + TxCmd); /* START Tx */
  974. writew(0x0001, ioaddr + RxCmd); /* START Rx */
  975. netif_wake_queue(dev);
  976. }
  977. /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  978. static void hamachi_init_ring(struct net_device *dev)
  979. {
  980. struct hamachi_private *hmp = netdev_priv(dev);
  981. int i;
  982. hmp->tx_full = 0;
  983. hmp->cur_rx = hmp->cur_tx = 0;
  984. hmp->dirty_rx = hmp->dirty_tx = 0;
  985. /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
  986. * card needs room to do 8 byte alignment, +2 so we can reserve
  987. * the first 2 bytes, and +16 gets room for the status word from the
  988. * card. -KDU
  989. */
  990. hmp->rx_buf_sz = (dev->mtu <= 1492 ? PKT_BUF_SZ :
  991. (((dev->mtu+26+7) & ~7) + 16));
  992. /* Initialize all Rx descriptors. */
  993. for (i = 0; i < RX_RING_SIZE; i++) {
  994. hmp->rx_ring[i].status_n_length = 0;
  995. hmp->rx_skbuff[i] = NULL;
  996. }
  997. /* Fill in the Rx buffers. Handle allocation failure gracefully. */
  998. for (i = 0; i < RX_RING_SIZE; i++) {
  999. struct sk_buff *skb = netdev_alloc_skb(dev, hmp->rx_buf_sz + 2);
  1000. hmp->rx_skbuff[i] = skb;
  1001. if (skb == NULL)
  1002. break;
  1003. skb_reserve(skb, 2); /* 16 byte align the IP header. */
  1004. hmp->rx_ring[i].addr = cpu_to_leXX(pci_map_single(hmp->pci_dev,
  1005. skb->data, hmp->rx_buf_sz, PCI_DMA_FROMDEVICE));
  1006. /* -2 because it doesn't REALLY have that first 2 bytes -KDU */
  1007. hmp->rx_ring[i].status_n_length = cpu_to_le32(DescOwn |
  1008. DescEndPacket | DescIntr | (hmp->rx_buf_sz -2));
  1009. }
  1010. hmp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
  1011. hmp->rx_ring[RX_RING_SIZE-1].status_n_length |= cpu_to_le32(DescEndRing);
  1012. for (i = 0; i < TX_RING_SIZE; i++) {
  1013. hmp->tx_skbuff[i] = NULL;
  1014. hmp->tx_ring[i].status_n_length = 0;
  1015. }
  1016. /* Mark the last entry of the ring */
  1017. hmp->tx_ring[TX_RING_SIZE-1].status_n_length |= cpu_to_le32(DescEndRing);
  1018. }
  1019. static netdev_tx_t hamachi_start_xmit(struct sk_buff *skb,
  1020. struct net_device *dev)
  1021. {
  1022. struct hamachi_private *hmp = netdev_priv(dev);
  1023. unsigned entry;
  1024. u16 status;
  1025. /* Ok, now make sure that the queue has space before trying to
  1026. add another skbuff. if we return non-zero the scheduler
  1027. should interpret this as a queue full and requeue the buffer
  1028. for later.
  1029. */
  1030. if (hmp->tx_full) {
  1031. /* We should NEVER reach this point -KDU */
  1032. printk(KERN_WARNING "%s: Hamachi transmit queue full at slot %d.\n",dev->name, hmp->cur_tx);
  1033. /* Wake the potentially-idle transmit channel. */
  1034. /* If we don't need to read status, DON'T -KDU */
  1035. status=readw(hmp->base + TxStatus);
  1036. if( !(status & 0x0001) || (status & 0x0002))
  1037. writew(0x0001, hmp->base + TxCmd);
  1038. return NETDEV_TX_BUSY;
  1039. }
  1040. /* Caution: the write order is important here, set the field
  1041. with the "ownership" bits last. */
  1042. /* Calculate the next Tx descriptor entry. */
  1043. entry = hmp->cur_tx % TX_RING_SIZE;
  1044. hmp->tx_skbuff[entry] = skb;
  1045. hmp->tx_ring[entry].addr = cpu_to_leXX(pci_map_single(hmp->pci_dev,
  1046. skb->data, skb->len, PCI_DMA_TODEVICE));
  1047. /* Hmmmm, could probably put a DescIntr on these, but the way
  1048. the driver is currently coded makes Tx interrupts unnecessary
  1049. since the clearing of the Tx ring is handled by the start_xmit
  1050. routine. This organization helps mitigate the interrupts a
  1051. bit and probably renders the max_tx_latency param useless.
  1052. Update: Putting a DescIntr bit on all of the descriptors and
  1053. mitigating interrupt frequency with the tx_min_pkt parameter. -KDU
  1054. */
  1055. if (entry >= TX_RING_SIZE-1) /* Wrap ring */
  1056. hmp->tx_ring[entry].status_n_length = cpu_to_le32(DescOwn |
  1057. DescEndPacket | DescEndRing | DescIntr | skb->len);
  1058. else
  1059. hmp->tx_ring[entry].status_n_length = cpu_to_le32(DescOwn |
  1060. DescEndPacket | DescIntr | skb->len);
  1061. hmp->cur_tx++;
  1062. /* Non-x86 Todo: explicitly flush cache lines here. */
  1063. /* Wake the potentially-idle transmit channel. */
  1064. /* If we don't need to read status, DON'T -KDU */
  1065. status=readw(hmp->base + TxStatus);
  1066. if( !(status & 0x0001) || (status & 0x0002))
  1067. writew(0x0001, hmp->base + TxCmd);
  1068. /* Immediately before returning, let's clear as many entries as we can. */
  1069. hamachi_tx(dev);
  1070. /* We should kick the bottom half here, since we are not accepting
  1071. * interrupts with every packet. i.e. realize that Gigabit ethernet
  1072. * can transmit faster than ordinary machines can load packets;
  1073. * hence, any packet that got put off because we were in the transmit
  1074. * routine should IMMEDIATELY get a chance to be re-queued. -KDU
  1075. */
  1076. if ((hmp->cur_tx - hmp->dirty_tx) < (TX_RING_SIZE - 4))
  1077. netif_wake_queue(dev); /* Typical path */
  1078. else {
  1079. hmp->tx_full = 1;
  1080. netif_stop_queue(dev);
  1081. }
  1082. if (hamachi_debug > 4) {
  1083. printk(KERN_DEBUG "%s: Hamachi transmit frame #%d queued in slot %d.\n",
  1084. dev->name, hmp->cur_tx, entry);
  1085. }
  1086. return NETDEV_TX_OK;
  1087. }
  1088. /* The interrupt handler does all of the Rx thread work and cleans up
  1089. after the Tx thread. */
  1090. static irqreturn_t hamachi_interrupt(int irq, void *dev_instance)
  1091. {
  1092. struct net_device *dev = dev_instance;
  1093. struct hamachi_private *hmp = netdev_priv(dev);
  1094. void __iomem *ioaddr = hmp->base;
  1095. long boguscnt = max_interrupt_work;
  1096. int handled = 0;
  1097. #ifndef final_version /* Can never occur. */
  1098. if (dev == NULL) {
  1099. printk (KERN_ERR "hamachi_interrupt(): irq %d for unknown device.\n", irq);
  1100. return IRQ_NONE;
  1101. }
  1102. #endif
  1103. spin_lock(&hmp->lock);
  1104. do {
  1105. u32 intr_status = readl(ioaddr + InterruptClear);
  1106. if (hamachi_debug > 4)
  1107. printk(KERN_DEBUG "%s: Hamachi interrupt, status %4.4x.\n",
  1108. dev->name, intr_status);
  1109. if (intr_status == 0)
  1110. break;
  1111. handled = 1;
  1112. if (intr_status & IntrRxDone)
  1113. hamachi_rx(dev);
  1114. if (intr_status & IntrTxDone){
  1115. /* This code should RARELY need to execute. After all, this is
  1116. * a gigabit link, it should consume packets as fast as we put
  1117. * them in AND we clear the Tx ring in hamachi_start_xmit().
  1118. */
  1119. if (hmp->tx_full){
  1120. for (; hmp->cur_tx - hmp->dirty_tx > 0; hmp->dirty_tx++){
  1121. int entry = hmp->dirty_tx % TX_RING_SIZE;
  1122. struct sk_buff *skb;
  1123. if (hmp->tx_ring[entry].status_n_length & cpu_to_le32(DescOwn))
  1124. break;
  1125. skb = hmp->tx_skbuff[entry];
  1126. /* Free the original skb. */
  1127. if (skb){
  1128. pci_unmap_single(hmp->pci_dev,
  1129. leXX_to_cpu(hmp->tx_ring[entry].addr),
  1130. skb->len,
  1131. PCI_DMA_TODEVICE);
  1132. dev_kfree_skb_irq(skb);
  1133. hmp->tx_skbuff[entry] = NULL;
  1134. }
  1135. hmp->tx_ring[entry].status_n_length = 0;
  1136. if (entry >= TX_RING_SIZE-1)
  1137. hmp->tx_ring[TX_RING_SIZE-1].status_n_length |=
  1138. cpu_to_le32(DescEndRing);
  1139. dev->stats.tx_packets++;
  1140. }
  1141. if (hmp->cur_tx - hmp->dirty_tx < TX_RING_SIZE - 4){
  1142. /* The ring is no longer full */
  1143. hmp->tx_full = 0;
  1144. netif_wake_queue(dev);
  1145. }
  1146. } else {
  1147. netif_wake_queue(dev);
  1148. }
  1149. }
  1150. /* Abnormal error summary/uncommon events handlers. */
  1151. if (intr_status &
  1152. (IntrTxPCIFault | IntrTxPCIErr | IntrRxPCIFault | IntrRxPCIErr |
  1153. LinkChange | NegotiationChange | StatsMax))
  1154. hamachi_error(dev, intr_status);
  1155. if (--boguscnt < 0) {
  1156. printk(KERN_WARNING "%s: Too much work at interrupt, status=0x%4.4x.\n",
  1157. dev->name, intr_status);
  1158. break;
  1159. }
  1160. } while (1);
  1161. if (hamachi_debug > 3)
  1162. printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
  1163. dev->name, readl(ioaddr + IntrStatus));
  1164. #ifndef final_version
  1165. /* Code that should never be run! Perhaps remove after testing.. */
  1166. {
  1167. static int stopit = 10;
  1168. if (dev->start == 0 && --stopit < 0) {
  1169. printk(KERN_ERR "%s: Emergency stop, looping startup interrupt.\n",
  1170. dev->name);
  1171. free_irq(irq, dev);
  1172. }
  1173. }
  1174. #endif
  1175. spin_unlock(&hmp->lock);
  1176. return IRQ_RETVAL(handled);
  1177. }
  1178. /* This routine is logically part of the interrupt handler, but separated
  1179. for clarity and better register allocation. */
  1180. static int hamachi_rx(struct net_device *dev)
  1181. {
  1182. struct hamachi_private *hmp = netdev_priv(dev);
  1183. int entry = hmp->cur_rx % RX_RING_SIZE;
  1184. int boguscnt = (hmp->dirty_rx + RX_RING_SIZE) - hmp->cur_rx;
  1185. if (hamachi_debug > 4) {
  1186. printk(KERN_DEBUG " In hamachi_rx(), entry %d status %4.4x.\n",
  1187. entry, hmp->rx_ring[entry].status_n_length);
  1188. }
  1189. /* If EOP is set on the next entry, it's a new packet. Send it up. */
  1190. while (1) {
  1191. struct hamachi_desc *desc = &(hmp->rx_ring[entry]);
  1192. u32 desc_status = le32_to_cpu(desc->status_n_length);
  1193. u16 data_size = desc_status; /* Implicit truncate */
  1194. u8 *buf_addr;
  1195. s32 frame_status;
  1196. if (desc_status & DescOwn)
  1197. break;
  1198. pci_dma_sync_single_for_cpu(hmp->pci_dev,
  1199. leXX_to_cpu(desc->addr),
  1200. hmp->rx_buf_sz,
  1201. PCI_DMA_FROMDEVICE);
  1202. buf_addr = (u8 *) hmp->rx_skbuff[entry]->data;
  1203. frame_status = get_unaligned_le32(&(buf_addr[data_size - 12]));
  1204. if (hamachi_debug > 4)
  1205. printk(KERN_DEBUG " hamachi_rx() status was %8.8x.\n",
  1206. frame_status);
  1207. if (--boguscnt < 0)
  1208. break;
  1209. if ( ! (desc_status & DescEndPacket)) {
  1210. printk(KERN_WARNING "%s: Oversized Ethernet frame spanned "
  1211. "multiple buffers, entry %#x length %d status %4.4x!\n",
  1212. dev->name, hmp->cur_rx, data_size, desc_status);
  1213. printk(KERN_WARNING "%s: Oversized Ethernet frame %p vs %p.\n",
  1214. dev->name, desc, &hmp->rx_ring[hmp->cur_rx % RX_RING_SIZE]);
  1215. printk(KERN_WARNING "%s: Oversized Ethernet frame -- next status %x/%x last status %x.\n",
  1216. dev->name,
  1217. le32_to_cpu(hmp->rx_ring[(hmp->cur_rx+1) % RX_RING_SIZE].status_n_length) & 0xffff0000,
  1218. le32_to_cpu(hmp->rx_ring[(hmp->cur_rx+1) % RX_RING_SIZE].status_n_length) & 0x0000ffff,
  1219. le32_to_cpu(hmp->rx_ring[(hmp->cur_rx-1) % RX_RING_SIZE].status_n_length));
  1220. dev->stats.rx_length_errors++;
  1221. } /* else Omit for prototype errata??? */
  1222. if (frame_status & 0x00380000) {
  1223. /* There was an error. */
  1224. if (hamachi_debug > 2)
  1225. printk(KERN_DEBUG " hamachi_rx() Rx error was %8.8x.\n",
  1226. frame_status);
  1227. dev->stats.rx_errors++;
  1228. if (frame_status & 0x00600000)
  1229. dev->stats.rx_length_errors++;
  1230. if (frame_status & 0x00080000)
  1231. dev->stats.rx_frame_errors++;
  1232. if (frame_status & 0x00100000)
  1233. dev->stats.rx_crc_errors++;
  1234. if (frame_status < 0)
  1235. dev->stats.rx_dropped++;
  1236. } else {
  1237. struct sk_buff *skb;
  1238. /* Omit CRC */
  1239. u16 pkt_len = (frame_status & 0x07ff) - 4;
  1240. #ifdef RX_CHECKSUM
  1241. u32 pfck = *(u32 *) &buf_addr[data_size - 8];
  1242. #endif
  1243. #ifndef final_version
  1244. if (hamachi_debug > 4)
  1245. printk(KERN_DEBUG " hamachi_rx() normal Rx pkt length %d"
  1246. " of %d, bogus_cnt %d.\n",
  1247. pkt_len, data_size, boguscnt);
  1248. if (hamachi_debug > 5)
  1249. printk(KERN_DEBUG"%s: rx status %8.8x %8.8x %8.8x %8.8x %8.8x.\n",
  1250. dev->name,
  1251. *(s32*)&(buf_addr[data_size - 20]),
  1252. *(s32*)&(buf_addr[data_size - 16]),
  1253. *(s32*)&(buf_addr[data_size - 12]),
  1254. *(s32*)&(buf_addr[data_size - 8]),
  1255. *(s32*)&(buf_addr[data_size - 4]));
  1256. #endif
  1257. /* Check if the packet is long enough to accept without copying
  1258. to a minimally-sized skbuff. */
  1259. if (pkt_len < rx_copybreak &&
  1260. (skb = netdev_alloc_skb(dev, pkt_len + 2)) != NULL) {
  1261. #ifdef RX_CHECKSUM
  1262. printk(KERN_ERR "%s: rx_copybreak non-zero "
  1263. "not good with RX_CHECKSUM\n", dev->name);
  1264. #endif
  1265. skb_reserve(skb, 2); /* 16 byte align the IP header */
  1266. pci_dma_sync_single_for_cpu(hmp->pci_dev,
  1267. leXX_to_cpu(hmp->rx_ring[entry].addr),
  1268. hmp->rx_buf_sz,
  1269. PCI_DMA_FROMDEVICE);
  1270. /* Call copy + cksum if available. */
  1271. #if 1 || USE_IP_COPYSUM
  1272. skb_copy_to_linear_data(skb,
  1273. hmp->rx_skbuff[entry]->data, pkt_len);
  1274. skb_put(skb, pkt_len);
  1275. #else
  1276. memcpy(skb_put(skb, pkt_len), hmp->rx_ring_dma
  1277. + entry*sizeof(*desc), pkt_len);
  1278. #endif
  1279. pci_dma_sync_single_for_device(hmp->pci_dev,
  1280. leXX_to_cpu(hmp->rx_ring[entry].addr),
  1281. hmp->rx_buf_sz,
  1282. PCI_DMA_FROMDEVICE);
  1283. } else {
  1284. pci_unmap_single(hmp->pci_dev,
  1285. leXX_to_cpu(hmp->rx_ring[entry].addr),
  1286. hmp->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1287. skb_put(skb = hmp->rx_skbuff[entry], pkt_len);
  1288. hmp->rx_skbuff[entry] = NULL;
  1289. }
  1290. skb->protocol = eth_type_trans(skb, dev);
  1291. #ifdef RX_CHECKSUM
  1292. /* TCP or UDP on ipv4, DIX encoding */
  1293. if (pfck>>24 == 0x91 || pfck>>24 == 0x51) {
  1294. struct iphdr *ih = (struct iphdr *) skb->data;
  1295. /* Check that IP packet is at least 46 bytes, otherwise,
  1296. * there may be pad bytes included in the hardware checksum.
  1297. * This wouldn't happen if everyone padded with 0.
  1298. */
  1299. if (ntohs(ih->tot_len) >= 46){
  1300. /* don't worry about frags */
  1301. if (!(ih->frag_off & cpu_to_be16(IP_MF|IP_OFFSET))) {
  1302. u32 inv = *(u32 *) &buf_addr[data_size - 16];
  1303. u32 *p = (u32 *) &buf_addr[data_size - 20];
  1304. register u32 crc, p_r, p_r1;
  1305. if (inv & 4) {
  1306. inv &= ~4;
  1307. --p;
  1308. }
  1309. p_r = *p;
  1310. p_r1 = *(p-1);
  1311. switch (inv) {
  1312. case 0:
  1313. crc = (p_r & 0xffff) + (p_r >> 16);
  1314. break;
  1315. case 1:
  1316. crc = (p_r >> 16) + (p_r & 0xffff)
  1317. + (p_r1 >> 16 & 0xff00);
  1318. break;
  1319. case 2:
  1320. crc = p_r + (p_r1 >> 16);
  1321. break;
  1322. case 3:
  1323. crc = p_r + (p_r1 & 0xff00) + (p_r1 >> 16);
  1324. break;
  1325. default: /*NOTREACHED*/ crc = 0;
  1326. }
  1327. if (crc & 0xffff0000) {
  1328. crc &= 0xffff;
  1329. ++crc;
  1330. }
  1331. /* tcp/udp will add in pseudo */
  1332. skb->csum = ntohs(pfck & 0xffff);
  1333. if (skb->csum > crc)
  1334. skb->csum -= crc;
  1335. else
  1336. skb->csum += (~crc & 0xffff);
  1337. /*
  1338. * could do the pseudo myself and return
  1339. * CHECKSUM_UNNECESSARY
  1340. */
  1341. skb->ip_summed = CHECKSUM_COMPLETE;
  1342. }
  1343. }
  1344. }
  1345. #endif /* RX_CHECKSUM */
  1346. netif_rx(skb);
  1347. dev->stats.rx_packets++;
  1348. }
  1349. entry = (++hmp->cur_rx) % RX_RING_SIZE;
  1350. }
  1351. /* Refill the Rx ring buffers. */
  1352. for (; hmp->cur_rx - hmp->dirty_rx > 0; hmp->dirty_rx++) {
  1353. struct hamachi_desc *desc;
  1354. entry = hmp->dirty_rx % RX_RING_SIZE;
  1355. desc = &(hmp->rx_ring[entry]);
  1356. if (hmp->rx_skbuff[entry] == NULL) {
  1357. struct sk_buff *skb = netdev_alloc_skb(dev, hmp->rx_buf_sz + 2);
  1358. hmp->rx_skbuff[entry] = skb;
  1359. if (skb == NULL)
  1360. break; /* Better luck next round. */
  1361. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  1362. desc->addr = cpu_to_leXX(pci_map_single(hmp->pci_dev,
  1363. skb->data, hmp->rx_buf_sz, PCI_DMA_FROMDEVICE));
  1364. }
  1365. desc->status_n_length = cpu_to_le32(hmp->rx_buf_sz);
  1366. if (entry >= RX_RING_SIZE-1)
  1367. desc->status_n_length |= cpu_to_le32(DescOwn |
  1368. DescEndPacket | DescEndRing | DescIntr);
  1369. else
  1370. desc->status_n_length |= cpu_to_le32(DescOwn |
  1371. DescEndPacket | DescIntr);
  1372. }
  1373. /* Restart Rx engine if stopped. */
  1374. /* If we don't need to check status, don't. -KDU */
  1375. if (readw(hmp->base + RxStatus) & 0x0002)
  1376. writew(0x0001, hmp->base + RxCmd);
  1377. return 0;
  1378. }
  1379. /* This is more properly named "uncommon interrupt events", as it covers more
  1380. than just errors. */
  1381. static void hamachi_error(struct net_device *dev, int intr_status)
  1382. {
  1383. struct hamachi_private *hmp = netdev_priv(dev);
  1384. void __iomem *ioaddr = hmp->base;
  1385. if (intr_status & (LinkChange|NegotiationChange)) {
  1386. if (hamachi_debug > 1)
  1387. printk(KERN_INFO "%s: Link changed: AutoNegotiation Ctrl"
  1388. " %4.4x, Status %4.4x %4.4x Intr status %4.4x.\n",
  1389. dev->name, readw(ioaddr + 0x0E0), readw(ioaddr + 0x0E2),
  1390. readw(ioaddr + ANLinkPartnerAbility),
  1391. readl(ioaddr + IntrStatus));
  1392. if (readw(ioaddr + ANStatus) & 0x20)
  1393. writeb(0x01, ioaddr + LEDCtrl);
  1394. else
  1395. writeb(0x03, ioaddr + LEDCtrl);
  1396. }
  1397. if (intr_status & StatsMax) {
  1398. hamachi_get_stats(dev);
  1399. /* Read the overflow bits to clear. */
  1400. readl(ioaddr + 0x370);
  1401. readl(ioaddr + 0x3F0);
  1402. }
  1403. if ((intr_status & ~(LinkChange|StatsMax|NegotiationChange|IntrRxDone|IntrTxDone)) &&
  1404. hamachi_debug)
  1405. printk(KERN_ERR "%s: Something Wicked happened! %4.4x.\n",
  1406. dev->name, intr_status);
  1407. /* Hmmmmm, it's not clear how to recover from PCI faults. */
  1408. if (intr_status & (IntrTxPCIErr | IntrTxPCIFault))
  1409. dev->stats.tx_fifo_errors++;
  1410. if (intr_status & (IntrRxPCIErr | IntrRxPCIFault))
  1411. dev->stats.rx_fifo_errors++;
  1412. }
  1413. static int hamachi_close(struct net_device *dev)
  1414. {
  1415. struct hamachi_private *hmp = netdev_priv(dev);
  1416. void __iomem *ioaddr = hmp->base;
  1417. struct sk_buff *skb;
  1418. int i;
  1419. netif_stop_queue(dev);
  1420. if (hamachi_debug > 1) {
  1421. printk(KERN_DEBUG "%s: Shutting down ethercard, status was Tx %4.4x Rx %4.4x Int %2.2x.\n",
  1422. dev->name, readw(ioaddr + TxStatus),
  1423. readw(ioaddr + RxStatus), readl(ioaddr + IntrStatus));
  1424. printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
  1425. dev->name, hmp->cur_tx, hmp->dirty_tx, hmp->cur_rx, hmp->dirty_rx);
  1426. }
  1427. /* Disable interrupts by clearing the interrupt mask. */
  1428. writel(0x0000, ioaddr + InterruptEnable);
  1429. /* Stop the chip's Tx and Rx processes. */
  1430. writel(2, ioaddr + RxCmd);
  1431. writew(2, ioaddr + TxCmd);
  1432. #ifdef __i386__
  1433. if (hamachi_debug > 2) {
  1434. printk(KERN_DEBUG " Tx ring at %8.8x:\n",
  1435. (int)hmp->tx_ring_dma);
  1436. for (i = 0; i < TX_RING_SIZE; i++)
  1437. printk(KERN_DEBUG " %c #%d desc. %8.8x %8.8x.\n",
  1438. readl(ioaddr + TxCurPtr) == (long)&hmp->tx_ring[i] ? '>' : ' ',
  1439. i, hmp->tx_ring[i].status_n_length, hmp->tx_ring[i].addr);
  1440. printk(KERN_DEBUG " Rx ring %8.8x:\n",
  1441. (int)hmp->rx_ring_dma);
  1442. for (i = 0; i < RX_RING_SIZE; i++) {
  1443. printk(KERN_DEBUG " %c #%d desc. %4.4x %8.8x\n",
  1444. readl(ioaddr + RxCurPtr) == (long)&hmp->rx_ring[i] ? '>' : ' ',
  1445. i, hmp->rx_ring[i].status_n_length, hmp->rx_ring[i].addr);
  1446. if (hamachi_debug > 6) {
  1447. if (*(u8*)hmp->rx_skbuff[i]->data != 0x69) {
  1448. u16 *addr = (u16 *)
  1449. hmp->rx_skbuff[i]->data;
  1450. int j;
  1451. printk(KERN_DEBUG "Addr: ");
  1452. for (j = 0; j < 0x50; j++)
  1453. printk(" %4.4x", addr[j]);
  1454. printk("\n");
  1455. }
  1456. }
  1457. }
  1458. }
  1459. #endif /* __i386__ debugging only */
  1460. free_irq(hmp->pci_dev->irq, dev);
  1461. del_timer_sync(&hmp->timer);
  1462. /* Free all the skbuffs in the Rx queue. */
  1463. for (i = 0; i < RX_RING_SIZE; i++) {
  1464. skb = hmp->rx_skbuff[i];
  1465. hmp->rx_ring[i].status_n_length = 0;
  1466. if (skb) {
  1467. pci_unmap_single(hmp->pci_dev,
  1468. leXX_to_cpu(hmp->rx_ring[i].addr),
  1469. hmp->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1470. dev_kfree_skb(skb);
  1471. hmp->rx_skbuff[i] = NULL;
  1472. }
  1473. hmp->rx_ring[i].addr = cpu_to_leXX(0xBADF00D0); /* An invalid address. */
  1474. }
  1475. for (i = 0; i < TX_RING_SIZE; i++) {
  1476. skb = hmp->tx_skbuff[i];
  1477. if (skb) {
  1478. pci_unmap_single(hmp->pci_dev,
  1479. leXX_to_cpu(hmp->tx_ring[i].addr),
  1480. skb->len, PCI_DMA_TODEVICE);
  1481. dev_kfree_skb(skb);
  1482. hmp->tx_skbuff[i] = NULL;
  1483. }
  1484. }
  1485. writeb(0x00, ioaddr + LEDCtrl);
  1486. return 0;
  1487. }
  1488. static struct net_device_stats *hamachi_get_stats(struct net_device *dev)
  1489. {
  1490. struct hamachi_private *hmp = netdev_priv(dev);
  1491. void __iomem *ioaddr = hmp->base;
  1492. /* We should lock this segment of code for SMP eventually, although
  1493. the vulnerability window is very small and statistics are
  1494. non-critical. */
  1495. /* Ok, what goes here? This appears to be stuck at 21 packets
  1496. according to ifconfig. It does get incremented in hamachi_tx(),
  1497. so I think I'll comment it out here and see if better things
  1498. happen.
  1499. */
  1500. /* dev->stats.tx_packets = readl(ioaddr + 0x000); */
  1501. /* Total Uni+Brd+Multi */
  1502. dev->stats.rx_bytes = readl(ioaddr + 0x330);
  1503. /* Total Uni+Brd+Multi */
  1504. dev->stats.tx_bytes = readl(ioaddr + 0x3B0);
  1505. /* Multicast Rx */
  1506. dev->stats.multicast = readl(ioaddr + 0x320);
  1507. /* Over+Undersized */
  1508. dev->stats.rx_length_errors = readl(ioaddr + 0x368);
  1509. /* Jabber */
  1510. dev->stats.rx_over_errors = readl(ioaddr + 0x35C);
  1511. /* Jabber */
  1512. dev->stats.rx_crc_errors = readl(ioaddr + 0x360);
  1513. /* Symbol Errs */
  1514. dev->stats.rx_frame_errors = readl(ioaddr + 0x364);
  1515. /* Dropped */
  1516. dev->stats.rx_missed_errors = readl(ioaddr + 0x36C);
  1517. return &dev->stats;
  1518. }
  1519. static void set_rx_mode(struct net_device *dev)
  1520. {
  1521. struct hamachi_private *hmp = netdev_priv(dev);
  1522. void __iomem *ioaddr = hmp->base;
  1523. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1524. writew(0x000F, ioaddr + AddrMode);
  1525. } else if ((netdev_mc_count(dev) > 63) || (dev->flags & IFF_ALLMULTI)) {
  1526. /* Too many to match, or accept all multicasts. */
  1527. writew(0x000B, ioaddr + AddrMode);
  1528. } else if (!netdev_mc_empty(dev)) { /* Must use the CAM filter. */
  1529. struct netdev_hw_addr *ha;
  1530. int i = 0;
  1531. netdev_for_each_mc_addr(ha, dev) {
  1532. writel(*(u32 *)(ha->addr), ioaddr + 0x100 + i*8);
  1533. writel(0x20000 | (*(u16 *)&ha->addr[4]),
  1534. ioaddr + 0x104 + i*8);
  1535. i++;
  1536. }
  1537. /* Clear remaining entries. */
  1538. for (; i < 64; i++)
  1539. writel(0, ioaddr + 0x104 + i*8);
  1540. writew(0x0003, ioaddr + AddrMode);
  1541. } else { /* Normal, unicast/broadcast-only mode. */
  1542. writew(0x0001, ioaddr + AddrMode);
  1543. }
  1544. }
  1545. static int check_if_running(struct net_device *dev)
  1546. {
  1547. if (!netif_running(dev))
  1548. return -EINVAL;
  1549. return 0;
  1550. }
  1551. static void hamachi_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1552. {
  1553. struct hamachi_private *np = netdev_priv(dev);
  1554. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1555. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1556. strlcpy(info->bus_info, pci_name(np->pci_dev), sizeof(info->bus_info));
  1557. }
  1558. static int hamachi_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  1559. {
  1560. struct hamachi_private *np = netdev_priv(dev);
  1561. spin_lock_irq(&np->lock);
  1562. mii_ethtool_gset(&np->mii_if, ecmd);
  1563. spin_unlock_irq(&np->lock);
  1564. return 0;
  1565. }
  1566. static int hamachi_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  1567. {
  1568. struct hamachi_private *np = netdev_priv(dev);
  1569. int res;
  1570. spin_lock_irq(&np->lock);
  1571. res = mii_ethtool_sset(&np->mii_if, ecmd);
  1572. spin_unlock_irq(&np->lock);
  1573. return res;
  1574. }
  1575. static int hamachi_nway_reset(struct net_device *dev)
  1576. {
  1577. struct hamachi_private *np = netdev_priv(dev);
  1578. return mii_nway_restart(&np->mii_if);
  1579. }
  1580. static u32 hamachi_get_link(struct net_device *dev)
  1581. {
  1582. struct hamachi_private *np = netdev_priv(dev);
  1583. return mii_link_ok(&np->mii_if);
  1584. }
  1585. static const struct ethtool_ops ethtool_ops = {
  1586. .begin = check_if_running,
  1587. .get_drvinfo = hamachi_get_drvinfo,
  1588. .get_settings = hamachi_get_settings,
  1589. .set_settings = hamachi_set_settings,
  1590. .nway_reset = hamachi_nway_reset,
  1591. .get_link = hamachi_get_link,
  1592. };
  1593. static const struct ethtool_ops ethtool_ops_no_mii = {
  1594. .begin = check_if_running,
  1595. .get_drvinfo = hamachi_get_drvinfo,
  1596. };
  1597. static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1598. {
  1599. struct hamachi_private *np = netdev_priv(dev);
  1600. struct mii_ioctl_data *data = if_mii(rq);
  1601. int rc;
  1602. if (!netif_running(dev))
  1603. return -EINVAL;
  1604. if (cmd == (SIOCDEVPRIVATE+3)) { /* set rx,tx intr params */
  1605. u32 *d = (u32 *)&rq->ifr_ifru;
  1606. /* Should add this check here or an ordinary user can do nasty
  1607. * things. -KDU
  1608. *
  1609. * TODO: Shut down the Rx and Tx engines while doing this.
  1610. */
  1611. if (!capable(CAP_NET_ADMIN))
  1612. return -EPERM;
  1613. writel(d[0], np->base + TxIntrCtrl);
  1614. writel(d[1], np->base + RxIntrCtrl);
  1615. printk(KERN_NOTICE "%s: tx %08x, rx %08x intr\n", dev->name,
  1616. (u32) readl(np->base + TxIntrCtrl),
  1617. (u32) readl(np->base + RxIntrCtrl));
  1618. rc = 0;
  1619. }
  1620. else {
  1621. spin_lock_irq(&np->lock);
  1622. rc = generic_mii_ioctl(&np->mii_if, data, cmd, NULL);
  1623. spin_unlock_irq(&np->lock);
  1624. }
  1625. return rc;
  1626. }
  1627. static void hamachi_remove_one(struct pci_dev *pdev)
  1628. {
  1629. struct net_device *dev = pci_get_drvdata(pdev);
  1630. if (dev) {
  1631. struct hamachi_private *hmp = netdev_priv(dev);
  1632. pci_free_consistent(pdev, RX_TOTAL_SIZE, hmp->rx_ring,
  1633. hmp->rx_ring_dma);
  1634. pci_free_consistent(pdev, TX_TOTAL_SIZE, hmp->tx_ring,
  1635. hmp->tx_ring_dma);
  1636. unregister_netdev(dev);
  1637. iounmap(hmp->base);
  1638. free_netdev(dev);
  1639. pci_release_regions(pdev);
  1640. }
  1641. }
  1642. static const struct pci_device_id hamachi_pci_tbl[] = {
  1643. { 0x1318, 0x0911, PCI_ANY_ID, PCI_ANY_ID, },
  1644. { 0, }
  1645. };
  1646. MODULE_DEVICE_TABLE(pci, hamachi_pci_tbl);
  1647. static struct pci_driver hamachi_driver = {
  1648. .name = DRV_NAME,
  1649. .id_table = hamachi_pci_tbl,
  1650. .probe = hamachi_init_one,
  1651. .remove = hamachi_remove_one,
  1652. };
  1653. static int __init hamachi_init (void)
  1654. {
  1655. /* when a module, this is printed whether or not devices are found in probe */
  1656. #ifdef MODULE
  1657. printk(version);
  1658. #endif
  1659. return pci_register_driver(&hamachi_driver);
  1660. }
  1661. static void __exit hamachi_exit (void)
  1662. {
  1663. pci_unregister_driver(&hamachi_driver);
  1664. }
  1665. module_init(hamachi_init);
  1666. module_exit(hamachi_exit);