hamachi.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  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 <linux/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(struct timer_list *t);
  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)(struct timer_list *t);
  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(struct timer_list *t);
  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_validate_addr = eth_validate_addr,
  478. .ndo_set_mac_address = eth_mac_addr,
  479. .ndo_tx_timeout = hamachi_tx_timeout,
  480. .ndo_do_ioctl = netdev_ioctl,
  481. };
  482. static int hamachi_init_one(struct pci_dev *pdev,
  483. const struct pci_device_id *ent)
  484. {
  485. struct hamachi_private *hmp;
  486. int option, i, rx_int_var, tx_int_var, boguscnt;
  487. int chip_id = ent->driver_data;
  488. int irq;
  489. void __iomem *ioaddr;
  490. unsigned long base;
  491. static int card_idx;
  492. struct net_device *dev;
  493. void *ring_space;
  494. dma_addr_t ring_dma;
  495. int ret = -ENOMEM;
  496. /* when built into the kernel, we only print version if device is found */
  497. #ifndef MODULE
  498. static int printed_version;
  499. if (!printed_version++)
  500. printk(version);
  501. #endif
  502. if (pci_enable_device(pdev)) {
  503. ret = -EIO;
  504. goto err_out;
  505. }
  506. base = pci_resource_start(pdev, 0);
  507. #ifdef __alpha__ /* Really "64 bit addrs" */
  508. base |= (pci_resource_start(pdev, 1) << 32);
  509. #endif
  510. pci_set_master(pdev);
  511. i = pci_request_regions(pdev, DRV_NAME);
  512. if (i)
  513. return i;
  514. irq = pdev->irq;
  515. ioaddr = ioremap(base, 0x400);
  516. if (!ioaddr)
  517. goto err_out_release;
  518. dev = alloc_etherdev(sizeof(struct hamachi_private));
  519. if (!dev)
  520. goto err_out_iounmap;
  521. SET_NETDEV_DEV(dev, &pdev->dev);
  522. for (i = 0; i < 6; i++)
  523. dev->dev_addr[i] = 1 ? read_eeprom(ioaddr, 4 + i)
  524. : readb(ioaddr + StationAddr + i);
  525. #if ! defined(final_version)
  526. if (hamachi_debug > 4)
  527. for (i = 0; i < 0x10; i++)
  528. printk("%2.2x%s",
  529. read_eeprom(ioaddr, i), i % 16 != 15 ? " " : "\n");
  530. #endif
  531. hmp = netdev_priv(dev);
  532. spin_lock_init(&hmp->lock);
  533. hmp->mii_if.dev = dev;
  534. hmp->mii_if.mdio_read = mdio_read;
  535. hmp->mii_if.mdio_write = mdio_write;
  536. hmp->mii_if.phy_id_mask = 0x1f;
  537. hmp->mii_if.reg_num_mask = 0x1f;
  538. ring_space = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
  539. if (!ring_space)
  540. goto err_out_cleardev;
  541. hmp->tx_ring = ring_space;
  542. hmp->tx_ring_dma = ring_dma;
  543. ring_space = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
  544. if (!ring_space)
  545. goto err_out_unmap_tx;
  546. hmp->rx_ring = ring_space;
  547. hmp->rx_ring_dma = ring_dma;
  548. /* Check for options being passed in */
  549. option = card_idx < MAX_UNITS ? options[card_idx] : 0;
  550. if (dev->mem_start)
  551. option = dev->mem_start;
  552. /* If the bus size is misidentified, do the following. */
  553. force32 = force32 ? force32 :
  554. ((option >= 0) ? ((option & 0x00000070) >> 4) : 0 );
  555. if (force32)
  556. writeb(force32, ioaddr + VirtualJumpers);
  557. /* Hmmm, do we really need to reset the chip???. */
  558. writeb(0x01, ioaddr + ChipReset);
  559. /* After a reset, the clock speed measurement of the PCI bus will not
  560. * be valid for a moment. Wait for a little while until it is. If
  561. * it takes more than 10ms, forget it.
  562. */
  563. udelay(10);
  564. i = readb(ioaddr + PCIClkMeas);
  565. for (boguscnt = 0; (!(i & 0x080)) && boguscnt < 1000; boguscnt++){
  566. udelay(10);
  567. i = readb(ioaddr + PCIClkMeas);
  568. }
  569. hmp->base = ioaddr;
  570. pci_set_drvdata(pdev, dev);
  571. hmp->chip_id = chip_id;
  572. hmp->pci_dev = pdev;
  573. /* The lower four bits are the media type. */
  574. if (option > 0) {
  575. hmp->option = option;
  576. if (option & 0x200)
  577. hmp->mii_if.full_duplex = 1;
  578. else if (option & 0x080)
  579. hmp->mii_if.full_duplex = 0;
  580. hmp->default_port = option & 15;
  581. if (hmp->default_port)
  582. hmp->mii_if.force_media = 1;
  583. }
  584. if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
  585. hmp->mii_if.full_duplex = 1;
  586. /* lock the duplex mode if someone specified a value */
  587. if (hmp->mii_if.full_duplex || (option & 0x080))
  588. hmp->duplex_lock = 1;
  589. /* Set interrupt tuning parameters */
  590. max_rx_latency = max_rx_latency & 0x00ff;
  591. max_rx_gap = max_rx_gap & 0x00ff;
  592. min_rx_pkt = min_rx_pkt & 0x00ff;
  593. max_tx_latency = max_tx_latency & 0x00ff;
  594. max_tx_gap = max_tx_gap & 0x00ff;
  595. min_tx_pkt = min_tx_pkt & 0x00ff;
  596. rx_int_var = card_idx < MAX_UNITS ? rx_params[card_idx] : -1;
  597. tx_int_var = card_idx < MAX_UNITS ? tx_params[card_idx] : -1;
  598. hmp->rx_int_var = rx_int_var >= 0 ? rx_int_var :
  599. (min_rx_pkt << 16 | max_rx_gap << 8 | max_rx_latency);
  600. hmp->tx_int_var = tx_int_var >= 0 ? tx_int_var :
  601. (min_tx_pkt << 16 | max_tx_gap << 8 | max_tx_latency);
  602. /* The Hamachi-specific entries in the device structure. */
  603. dev->netdev_ops = &hamachi_netdev_ops;
  604. dev->ethtool_ops = (chip_tbl[hmp->chip_id].flags & CanHaveMII) ?
  605. &ethtool_ops : &ethtool_ops_no_mii;
  606. dev->watchdog_timeo = TX_TIMEOUT;
  607. if (mtu)
  608. dev->mtu = mtu;
  609. i = register_netdev(dev);
  610. if (i) {
  611. ret = i;
  612. goto err_out_unmap_rx;
  613. }
  614. printk(KERN_INFO "%s: %s type %x at %p, %pM, IRQ %d.\n",
  615. dev->name, chip_tbl[chip_id].name, readl(ioaddr + ChipRev),
  616. ioaddr, dev->dev_addr, irq);
  617. i = readb(ioaddr + PCIClkMeas);
  618. printk(KERN_INFO "%s: %d-bit %d Mhz PCI bus (%d), Virtual Jumpers "
  619. "%2.2x, LPA %4.4x.\n",
  620. dev->name, readw(ioaddr + MiscStatus) & 1 ? 64 : 32,
  621. i ? 2000/(i&0x7f) : 0, i&0x7f, (int)readb(ioaddr + VirtualJumpers),
  622. readw(ioaddr + ANLinkPartnerAbility));
  623. if (chip_tbl[hmp->chip_id].flags & CanHaveMII) {
  624. int phy, phy_idx = 0;
  625. for (phy = 0; phy < 32 && phy_idx < MII_CNT; phy++) {
  626. int mii_status = mdio_read(dev, phy, MII_BMSR);
  627. if (mii_status != 0xffff &&
  628. mii_status != 0x0000) {
  629. hmp->phys[phy_idx++] = phy;
  630. hmp->mii_if.advertising = mdio_read(dev, phy, MII_ADVERTISE);
  631. printk(KERN_INFO "%s: MII PHY found at address %d, status "
  632. "0x%4.4x advertising %4.4x.\n",
  633. dev->name, phy, mii_status, hmp->mii_if.advertising);
  634. }
  635. }
  636. hmp->mii_cnt = phy_idx;
  637. if (hmp->mii_cnt > 0)
  638. hmp->mii_if.phy_id = hmp->phys[0];
  639. else
  640. memset(&hmp->mii_if, 0, sizeof(hmp->mii_if));
  641. }
  642. /* Configure gigabit autonegotiation. */
  643. writew(0x0400, ioaddr + ANXchngCtrl); /* Enable legacy links. */
  644. writew(0x08e0, ioaddr + ANAdvertise); /* Set our advertise word. */
  645. writew(0x1000, ioaddr + ANCtrl); /* Enable negotiation */
  646. card_idx++;
  647. return 0;
  648. err_out_unmap_rx:
  649. pci_free_consistent(pdev, RX_TOTAL_SIZE, hmp->rx_ring,
  650. hmp->rx_ring_dma);
  651. err_out_unmap_tx:
  652. pci_free_consistent(pdev, TX_TOTAL_SIZE, hmp->tx_ring,
  653. hmp->tx_ring_dma);
  654. err_out_cleardev:
  655. free_netdev (dev);
  656. err_out_iounmap:
  657. iounmap(ioaddr);
  658. err_out_release:
  659. pci_release_regions(pdev);
  660. err_out:
  661. return ret;
  662. }
  663. static int read_eeprom(void __iomem *ioaddr, int location)
  664. {
  665. int bogus_cnt = 1000;
  666. /* We should check busy first - per docs -KDU */
  667. while ((readb(ioaddr + EECmdStatus) & 0x40) && --bogus_cnt > 0);
  668. writew(location, ioaddr + EEAddr);
  669. writeb(0x02, ioaddr + EECmdStatus);
  670. bogus_cnt = 1000;
  671. while ((readb(ioaddr + EECmdStatus) & 0x40) && --bogus_cnt > 0);
  672. if (hamachi_debug > 5)
  673. printk(" EEPROM status is %2.2x after %d ticks.\n",
  674. (int)readb(ioaddr + EECmdStatus), 1000- bogus_cnt);
  675. return readb(ioaddr + EEData);
  676. }
  677. /* MII Managemen Data I/O accesses.
  678. These routines assume the MDIO controller is idle, and do not exit until
  679. the command is finished. */
  680. static int mdio_read(struct net_device *dev, int phy_id, int location)
  681. {
  682. struct hamachi_private *hmp = netdev_priv(dev);
  683. void __iomem *ioaddr = hmp->base;
  684. int i;
  685. /* We should check busy first - per docs -KDU */
  686. for (i = 10000; i >= 0; i--)
  687. if ((readw(ioaddr + MII_Status) & 1) == 0)
  688. break;
  689. writew((phy_id<<8) + location, ioaddr + MII_Addr);
  690. writew(0x0001, ioaddr + MII_Cmd);
  691. for (i = 10000; i >= 0; i--)
  692. if ((readw(ioaddr + MII_Status) & 1) == 0)
  693. break;
  694. return readw(ioaddr + MII_Rd_Data);
  695. }
  696. static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
  697. {
  698. struct hamachi_private *hmp = netdev_priv(dev);
  699. void __iomem *ioaddr = hmp->base;
  700. int i;
  701. /* We should check busy first - per docs -KDU */
  702. for (i = 10000; i >= 0; i--)
  703. if ((readw(ioaddr + MII_Status) & 1) == 0)
  704. break;
  705. writew((phy_id<<8) + location, ioaddr + MII_Addr);
  706. writew(value, ioaddr + MII_Wr_Data);
  707. /* Wait for the command to finish. */
  708. for (i = 10000; i >= 0; i--)
  709. if ((readw(ioaddr + MII_Status) & 1) == 0)
  710. break;
  711. }
  712. static int hamachi_open(struct net_device *dev)
  713. {
  714. struct hamachi_private *hmp = netdev_priv(dev);
  715. void __iomem *ioaddr = hmp->base;
  716. int i;
  717. u32 rx_int_var, tx_int_var;
  718. u16 fifo_info;
  719. i = request_irq(hmp->pci_dev->irq, hamachi_interrupt, IRQF_SHARED,
  720. dev->name, dev);
  721. if (i)
  722. return i;
  723. hamachi_init_ring(dev);
  724. #if ADDRLEN == 64
  725. /* writellll anyone ? */
  726. writel(hmp->rx_ring_dma, ioaddr + RxPtr);
  727. writel(hmp->rx_ring_dma >> 32, ioaddr + RxPtr + 4);
  728. writel(hmp->tx_ring_dma, ioaddr + TxPtr);
  729. writel(hmp->tx_ring_dma >> 32, ioaddr + TxPtr + 4);
  730. #else
  731. writel(hmp->rx_ring_dma, ioaddr + RxPtr);
  732. writel(hmp->tx_ring_dma, ioaddr + TxPtr);
  733. #endif
  734. /* TODO: It would make sense to organize this as words since the card
  735. * documentation does. -KDU
  736. */
  737. for (i = 0; i < 6; i++)
  738. writeb(dev->dev_addr[i], ioaddr + StationAddr + i);
  739. /* Initialize other registers: with so many this eventually this will
  740. converted to an offset/value list. */
  741. /* Configure the FIFO */
  742. fifo_info = (readw(ioaddr + GPIO) & 0x00C0) >> 6;
  743. switch (fifo_info){
  744. case 0 :
  745. /* No FIFO */
  746. writew(0x0000, ioaddr + FIFOcfg);
  747. break;
  748. case 1 :
  749. /* Configure the FIFO for 512K external, 16K used for Tx. */
  750. writew(0x0028, ioaddr + FIFOcfg);
  751. break;
  752. case 2 :
  753. /* Configure the FIFO for 1024 external, 32K used for Tx. */
  754. writew(0x004C, ioaddr + FIFOcfg);
  755. break;
  756. case 3 :
  757. /* Configure the FIFO for 2048 external, 32K used for Tx. */
  758. writew(0x006C, ioaddr + FIFOcfg);
  759. break;
  760. default :
  761. printk(KERN_WARNING "%s: Unsupported external memory config!\n",
  762. dev->name);
  763. /* Default to no FIFO */
  764. writew(0x0000, ioaddr + FIFOcfg);
  765. break;
  766. }
  767. if (dev->if_port == 0)
  768. dev->if_port = hmp->default_port;
  769. /* Setting the Rx mode will start the Rx process. */
  770. /* If someone didn't choose a duplex, default to full-duplex */
  771. if (hmp->duplex_lock != 1)
  772. hmp->mii_if.full_duplex = 1;
  773. /* always 1, takes no more time to do it */
  774. writew(0x0001, ioaddr + RxChecksum);
  775. writew(0x0000, ioaddr + TxChecksum);
  776. writew(0x8000, ioaddr + MACCnfg); /* Soft reset the MAC */
  777. writew(0x215F, ioaddr + MACCnfg);
  778. writew(0x000C, ioaddr + FrameGap0);
  779. /* WHAT?!?!? Why isn't this documented somewhere? -KDU */
  780. writew(0x1018, ioaddr + FrameGap1);
  781. /* Why do we enable receives/transmits here? -KDU */
  782. writew(0x0780, ioaddr + MACCnfg2); /* Upper 16 bits control LEDs. */
  783. /* Enable automatic generation of flow control frames, period 0xffff. */
  784. writel(0x0030FFFF, ioaddr + FlowCtrl);
  785. writew(MAX_FRAME_SIZE, ioaddr + MaxFrameSize); /* dev->mtu+14 ??? */
  786. /* Enable legacy links. */
  787. writew(0x0400, ioaddr + ANXchngCtrl); /* Enable legacy links. */
  788. /* Initial Link LED to blinking red. */
  789. writeb(0x03, ioaddr + LEDCtrl);
  790. /* Configure interrupt mitigation. This has a great effect on
  791. performance, so systems tuning should start here!. */
  792. rx_int_var = hmp->rx_int_var;
  793. tx_int_var = hmp->tx_int_var;
  794. if (hamachi_debug > 1) {
  795. printk("max_tx_latency: %d, max_tx_gap: %d, min_tx_pkt: %d\n",
  796. tx_int_var & 0x00ff, (tx_int_var & 0x00ff00) >> 8,
  797. (tx_int_var & 0x00ff0000) >> 16);
  798. printk("max_rx_latency: %d, max_rx_gap: %d, min_rx_pkt: %d\n",
  799. rx_int_var & 0x00ff, (rx_int_var & 0x00ff00) >> 8,
  800. (rx_int_var & 0x00ff0000) >> 16);
  801. printk("rx_int_var: %x, tx_int_var: %x\n", rx_int_var, tx_int_var);
  802. }
  803. writel(tx_int_var, ioaddr + TxIntrCtrl);
  804. writel(rx_int_var, ioaddr + RxIntrCtrl);
  805. set_rx_mode(dev);
  806. netif_start_queue(dev);
  807. /* Enable interrupts by setting the interrupt mask. */
  808. writel(0x80878787, ioaddr + InterruptEnable);
  809. writew(0x0000, ioaddr + EventStatus); /* Clear non-interrupting events */
  810. /* Configure and start the DMA channels. */
  811. /* Burst sizes are in the low three bits: size = 4<<(val&7) */
  812. #if ADDRLEN == 64
  813. writew(0x005D, ioaddr + RxDMACtrl); /* 128 dword bursts */
  814. writew(0x005D, ioaddr + TxDMACtrl);
  815. #else
  816. writew(0x001D, ioaddr + RxDMACtrl);
  817. writew(0x001D, ioaddr + TxDMACtrl);
  818. #endif
  819. writew(0x0001, ioaddr + RxCmd);
  820. if (hamachi_debug > 2) {
  821. printk(KERN_DEBUG "%s: Done hamachi_open(), status: Rx %x Tx %x.\n",
  822. dev->name, readw(ioaddr + RxStatus), readw(ioaddr + TxStatus));
  823. }
  824. /* Set the timer to check for link beat. */
  825. timer_setup(&hmp->timer, hamachi_timer, 0);
  826. hmp->timer.expires = RUN_AT((24*HZ)/10); /* 2.4 sec. */
  827. add_timer(&hmp->timer);
  828. return 0;
  829. }
  830. static inline int hamachi_tx(struct net_device *dev)
  831. {
  832. struct hamachi_private *hmp = netdev_priv(dev);
  833. /* Update the dirty pointer until we find an entry that is
  834. still owned by the card */
  835. for (; hmp->cur_tx - hmp->dirty_tx > 0; hmp->dirty_tx++) {
  836. int entry = hmp->dirty_tx % TX_RING_SIZE;
  837. struct sk_buff *skb;
  838. if (hmp->tx_ring[entry].status_n_length & cpu_to_le32(DescOwn))
  839. break;
  840. /* Free the original skb. */
  841. skb = hmp->tx_skbuff[entry];
  842. if (skb) {
  843. pci_unmap_single(hmp->pci_dev,
  844. leXX_to_cpu(hmp->tx_ring[entry].addr),
  845. skb->len, PCI_DMA_TODEVICE);
  846. dev_kfree_skb(skb);
  847. hmp->tx_skbuff[entry] = NULL;
  848. }
  849. hmp->tx_ring[entry].status_n_length = 0;
  850. if (entry >= TX_RING_SIZE-1)
  851. hmp->tx_ring[TX_RING_SIZE-1].status_n_length |=
  852. cpu_to_le32(DescEndRing);
  853. dev->stats.tx_packets++;
  854. }
  855. return 0;
  856. }
  857. static void hamachi_timer(struct timer_list *t)
  858. {
  859. struct hamachi_private *hmp = from_timer(hmp, t, timer);
  860. struct net_device *dev = hmp->mii_if.dev;
  861. void __iomem *ioaddr = hmp->base;
  862. int next_tick = 10*HZ;
  863. if (hamachi_debug > 2) {
  864. printk(KERN_INFO "%s: Hamachi Autonegotiation status %4.4x, LPA "
  865. "%4.4x.\n", dev->name, readw(ioaddr + ANStatus),
  866. readw(ioaddr + ANLinkPartnerAbility));
  867. printk(KERN_INFO "%s: Autonegotiation regs %4.4x %4.4x %4.4x "
  868. "%4.4x %4.4x %4.4x.\n", dev->name,
  869. readw(ioaddr + 0x0e0),
  870. readw(ioaddr + 0x0e2),
  871. readw(ioaddr + 0x0e4),
  872. readw(ioaddr + 0x0e6),
  873. readw(ioaddr + 0x0e8),
  874. readw(ioaddr + 0x0eA));
  875. }
  876. /* We could do something here... nah. */
  877. hmp->timer.expires = RUN_AT(next_tick);
  878. add_timer(&hmp->timer);
  879. }
  880. static void hamachi_tx_timeout(struct net_device *dev)
  881. {
  882. int i;
  883. struct hamachi_private *hmp = netdev_priv(dev);
  884. void __iomem *ioaddr = hmp->base;
  885. printk(KERN_WARNING "%s: Hamachi transmit timed out, status %8.8x,"
  886. " resetting...\n", dev->name, (int)readw(ioaddr + TxStatus));
  887. {
  888. printk(KERN_DEBUG " Rx ring %p: ", hmp->rx_ring);
  889. for (i = 0; i < RX_RING_SIZE; i++)
  890. printk(KERN_CONT " %8.8x",
  891. le32_to_cpu(hmp->rx_ring[i].status_n_length));
  892. printk(KERN_CONT "\n");
  893. printk(KERN_DEBUG" Tx ring %p: ", hmp->tx_ring);
  894. for (i = 0; i < TX_RING_SIZE; i++)
  895. printk(KERN_CONT " %4.4x",
  896. le32_to_cpu(hmp->tx_ring[i].status_n_length));
  897. printk(KERN_CONT "\n");
  898. }
  899. /* Reinit the hardware and make sure the Rx and Tx processes
  900. are up and running.
  901. */
  902. dev->if_port = 0;
  903. /* The right way to do Reset. -KDU
  904. * -Clear OWN bit in all Rx/Tx descriptors
  905. * -Wait 50 uS for channels to go idle
  906. * -Turn off MAC receiver
  907. * -Issue Reset
  908. */
  909. for (i = 0; i < RX_RING_SIZE; i++)
  910. hmp->rx_ring[i].status_n_length &= cpu_to_le32(~DescOwn);
  911. /* Presume that all packets in the Tx queue are gone if we have to
  912. * re-init the hardware.
  913. */
  914. for (i = 0; i < TX_RING_SIZE; i++){
  915. struct sk_buff *skb;
  916. if (i >= TX_RING_SIZE - 1)
  917. hmp->tx_ring[i].status_n_length =
  918. cpu_to_le32(DescEndRing) |
  919. (hmp->tx_ring[i].status_n_length &
  920. cpu_to_le32(0x0000ffff));
  921. else
  922. hmp->tx_ring[i].status_n_length &= cpu_to_le32(0x0000ffff);
  923. skb = hmp->tx_skbuff[i];
  924. if (skb){
  925. pci_unmap_single(hmp->pci_dev, leXX_to_cpu(hmp->tx_ring[i].addr),
  926. skb->len, PCI_DMA_TODEVICE);
  927. dev_kfree_skb(skb);
  928. hmp->tx_skbuff[i] = NULL;
  929. }
  930. }
  931. udelay(60); /* Sleep 60 us just for safety sake */
  932. writew(0x0002, ioaddr + RxCmd); /* STOP Rx */
  933. writeb(0x01, ioaddr + ChipReset); /* Reinit the hardware */
  934. hmp->tx_full = 0;
  935. hmp->cur_rx = hmp->cur_tx = 0;
  936. hmp->dirty_rx = hmp->dirty_tx = 0;
  937. /* Rx packets are also presumed lost; however, we need to make sure a
  938. * ring of buffers is in tact. -KDU
  939. */
  940. for (i = 0; i < RX_RING_SIZE; i++){
  941. struct sk_buff *skb = hmp->rx_skbuff[i];
  942. if (skb){
  943. pci_unmap_single(hmp->pci_dev,
  944. leXX_to_cpu(hmp->rx_ring[i].addr),
  945. hmp->rx_buf_sz, PCI_DMA_FROMDEVICE);
  946. dev_kfree_skb(skb);
  947. hmp->rx_skbuff[i] = NULL;
  948. }
  949. }
  950. /* Fill in the Rx buffers. Handle allocation failure gracefully. */
  951. for (i = 0; i < RX_RING_SIZE; i++) {
  952. struct sk_buff *skb;
  953. skb = netdev_alloc_skb_ip_align(dev, hmp->rx_buf_sz);
  954. hmp->rx_skbuff[i] = skb;
  955. if (skb == NULL)
  956. break;
  957. hmp->rx_ring[i].addr = cpu_to_leXX(pci_map_single(hmp->pci_dev,
  958. skb->data, hmp->rx_buf_sz, PCI_DMA_FROMDEVICE));
  959. hmp->rx_ring[i].status_n_length = cpu_to_le32(DescOwn |
  960. DescEndPacket | DescIntr | (hmp->rx_buf_sz - 2));
  961. }
  962. hmp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
  963. /* Mark the last entry as wrapping the ring. */
  964. hmp->rx_ring[RX_RING_SIZE-1].status_n_length |= cpu_to_le32(DescEndRing);
  965. /* Trigger an immediate transmit demand. */
  966. netif_trans_update(dev); /* prevent tx timeout */
  967. dev->stats.tx_errors++;
  968. /* Restart the chip's Tx/Rx processes . */
  969. writew(0x0002, ioaddr + TxCmd); /* STOP Tx */
  970. writew(0x0001, ioaddr + TxCmd); /* START Tx */
  971. writew(0x0001, ioaddr + RxCmd); /* START Rx */
  972. netif_wake_queue(dev);
  973. }
  974. /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  975. static void hamachi_init_ring(struct net_device *dev)
  976. {
  977. struct hamachi_private *hmp = netdev_priv(dev);
  978. int i;
  979. hmp->tx_full = 0;
  980. hmp->cur_rx = hmp->cur_tx = 0;
  981. hmp->dirty_rx = hmp->dirty_tx = 0;
  982. /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
  983. * card needs room to do 8 byte alignment, +2 so we can reserve
  984. * the first 2 bytes, and +16 gets room for the status word from the
  985. * card. -KDU
  986. */
  987. hmp->rx_buf_sz = (dev->mtu <= 1492 ? PKT_BUF_SZ :
  988. (((dev->mtu+26+7) & ~7) + 16));
  989. /* Initialize all Rx descriptors. */
  990. for (i = 0; i < RX_RING_SIZE; i++) {
  991. hmp->rx_ring[i].status_n_length = 0;
  992. hmp->rx_skbuff[i] = NULL;
  993. }
  994. /* Fill in the Rx buffers. Handle allocation failure gracefully. */
  995. for (i = 0; i < RX_RING_SIZE; i++) {
  996. struct sk_buff *skb = netdev_alloc_skb(dev, hmp->rx_buf_sz + 2);
  997. hmp->rx_skbuff[i] = skb;
  998. if (skb == NULL)
  999. break;
  1000. skb_reserve(skb, 2); /* 16 byte align the IP header. */
  1001. hmp->rx_ring[i].addr = cpu_to_leXX(pci_map_single(hmp->pci_dev,
  1002. skb->data, hmp->rx_buf_sz, PCI_DMA_FROMDEVICE));
  1003. /* -2 because it doesn't REALLY have that first 2 bytes -KDU */
  1004. hmp->rx_ring[i].status_n_length = cpu_to_le32(DescOwn |
  1005. DescEndPacket | DescIntr | (hmp->rx_buf_sz -2));
  1006. }
  1007. hmp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
  1008. hmp->rx_ring[RX_RING_SIZE-1].status_n_length |= cpu_to_le32(DescEndRing);
  1009. for (i = 0; i < TX_RING_SIZE; i++) {
  1010. hmp->tx_skbuff[i] = NULL;
  1011. hmp->tx_ring[i].status_n_length = 0;
  1012. }
  1013. /* Mark the last entry of the ring */
  1014. hmp->tx_ring[TX_RING_SIZE-1].status_n_length |= cpu_to_le32(DescEndRing);
  1015. }
  1016. static netdev_tx_t hamachi_start_xmit(struct sk_buff *skb,
  1017. struct net_device *dev)
  1018. {
  1019. struct hamachi_private *hmp = netdev_priv(dev);
  1020. unsigned entry;
  1021. u16 status;
  1022. /* Ok, now make sure that the queue has space before trying to
  1023. add another skbuff. if we return non-zero the scheduler
  1024. should interpret this as a queue full and requeue the buffer
  1025. for later.
  1026. */
  1027. if (hmp->tx_full) {
  1028. /* We should NEVER reach this point -KDU */
  1029. printk(KERN_WARNING "%s: Hamachi transmit queue full at slot %d.\n",dev->name, hmp->cur_tx);
  1030. /* Wake the potentially-idle transmit channel. */
  1031. /* If we don't need to read status, DON'T -KDU */
  1032. status=readw(hmp->base + TxStatus);
  1033. if( !(status & 0x0001) || (status & 0x0002))
  1034. writew(0x0001, hmp->base + TxCmd);
  1035. return NETDEV_TX_BUSY;
  1036. }
  1037. /* Caution: the write order is important here, set the field
  1038. with the "ownership" bits last. */
  1039. /* Calculate the next Tx descriptor entry. */
  1040. entry = hmp->cur_tx % TX_RING_SIZE;
  1041. hmp->tx_skbuff[entry] = skb;
  1042. hmp->tx_ring[entry].addr = cpu_to_leXX(pci_map_single(hmp->pci_dev,
  1043. skb->data, skb->len, PCI_DMA_TODEVICE));
  1044. /* Hmmmm, could probably put a DescIntr on these, but the way
  1045. the driver is currently coded makes Tx interrupts unnecessary
  1046. since the clearing of the Tx ring is handled by the start_xmit
  1047. routine. This organization helps mitigate the interrupts a
  1048. bit and probably renders the max_tx_latency param useless.
  1049. Update: Putting a DescIntr bit on all of the descriptors and
  1050. mitigating interrupt frequency with the tx_min_pkt parameter. -KDU
  1051. */
  1052. if (entry >= TX_RING_SIZE-1) /* Wrap ring */
  1053. hmp->tx_ring[entry].status_n_length = cpu_to_le32(DescOwn |
  1054. DescEndPacket | DescEndRing | DescIntr | skb->len);
  1055. else
  1056. hmp->tx_ring[entry].status_n_length = cpu_to_le32(DescOwn |
  1057. DescEndPacket | DescIntr | skb->len);
  1058. hmp->cur_tx++;
  1059. /* Non-x86 Todo: explicitly flush cache lines here. */
  1060. /* Wake the potentially-idle transmit channel. */
  1061. /* If we don't need to read status, DON'T -KDU */
  1062. status=readw(hmp->base + TxStatus);
  1063. if( !(status & 0x0001) || (status & 0x0002))
  1064. writew(0x0001, hmp->base + TxCmd);
  1065. /* Immediately before returning, let's clear as many entries as we can. */
  1066. hamachi_tx(dev);
  1067. /* We should kick the bottom half here, since we are not accepting
  1068. * interrupts with every packet. i.e. realize that Gigabit ethernet
  1069. * can transmit faster than ordinary machines can load packets;
  1070. * hence, any packet that got put off because we were in the transmit
  1071. * routine should IMMEDIATELY get a chance to be re-queued. -KDU
  1072. */
  1073. if ((hmp->cur_tx - hmp->dirty_tx) < (TX_RING_SIZE - 4))
  1074. netif_wake_queue(dev); /* Typical path */
  1075. else {
  1076. hmp->tx_full = 1;
  1077. netif_stop_queue(dev);
  1078. }
  1079. if (hamachi_debug > 4) {
  1080. printk(KERN_DEBUG "%s: Hamachi transmit frame #%d queued in slot %d.\n",
  1081. dev->name, hmp->cur_tx, entry);
  1082. }
  1083. return NETDEV_TX_OK;
  1084. }
  1085. /* The interrupt handler does all of the Rx thread work and cleans up
  1086. after the Tx thread. */
  1087. static irqreturn_t hamachi_interrupt(int irq, void *dev_instance)
  1088. {
  1089. struct net_device *dev = dev_instance;
  1090. struct hamachi_private *hmp = netdev_priv(dev);
  1091. void __iomem *ioaddr = hmp->base;
  1092. long boguscnt = max_interrupt_work;
  1093. int handled = 0;
  1094. #ifndef final_version /* Can never occur. */
  1095. if (dev == NULL) {
  1096. printk (KERN_ERR "hamachi_interrupt(): irq %d for unknown device.\n", irq);
  1097. return IRQ_NONE;
  1098. }
  1099. #endif
  1100. spin_lock(&hmp->lock);
  1101. do {
  1102. u32 intr_status = readl(ioaddr + InterruptClear);
  1103. if (hamachi_debug > 4)
  1104. printk(KERN_DEBUG "%s: Hamachi interrupt, status %4.4x.\n",
  1105. dev->name, intr_status);
  1106. if (intr_status == 0)
  1107. break;
  1108. handled = 1;
  1109. if (intr_status & IntrRxDone)
  1110. hamachi_rx(dev);
  1111. if (intr_status & IntrTxDone){
  1112. /* This code should RARELY need to execute. After all, this is
  1113. * a gigabit link, it should consume packets as fast as we put
  1114. * them in AND we clear the Tx ring in hamachi_start_xmit().
  1115. */
  1116. if (hmp->tx_full){
  1117. for (; hmp->cur_tx - hmp->dirty_tx > 0; hmp->dirty_tx++){
  1118. int entry = hmp->dirty_tx % TX_RING_SIZE;
  1119. struct sk_buff *skb;
  1120. if (hmp->tx_ring[entry].status_n_length & cpu_to_le32(DescOwn))
  1121. break;
  1122. skb = hmp->tx_skbuff[entry];
  1123. /* Free the original skb. */
  1124. if (skb){
  1125. pci_unmap_single(hmp->pci_dev,
  1126. leXX_to_cpu(hmp->tx_ring[entry].addr),
  1127. skb->len,
  1128. PCI_DMA_TODEVICE);
  1129. dev_consume_skb_irq(skb);
  1130. hmp->tx_skbuff[entry] = NULL;
  1131. }
  1132. hmp->tx_ring[entry].status_n_length = 0;
  1133. if (entry >= TX_RING_SIZE-1)
  1134. hmp->tx_ring[TX_RING_SIZE-1].status_n_length |=
  1135. cpu_to_le32(DescEndRing);
  1136. dev->stats.tx_packets++;
  1137. }
  1138. if (hmp->cur_tx - hmp->dirty_tx < TX_RING_SIZE - 4){
  1139. /* The ring is no longer full */
  1140. hmp->tx_full = 0;
  1141. netif_wake_queue(dev);
  1142. }
  1143. } else {
  1144. netif_wake_queue(dev);
  1145. }
  1146. }
  1147. /* Abnormal error summary/uncommon events handlers. */
  1148. if (intr_status &
  1149. (IntrTxPCIFault | IntrTxPCIErr | IntrRxPCIFault | IntrRxPCIErr |
  1150. LinkChange | NegotiationChange | StatsMax))
  1151. hamachi_error(dev, intr_status);
  1152. if (--boguscnt < 0) {
  1153. printk(KERN_WARNING "%s: Too much work at interrupt, status=0x%4.4x.\n",
  1154. dev->name, intr_status);
  1155. break;
  1156. }
  1157. } while (1);
  1158. if (hamachi_debug > 3)
  1159. printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
  1160. dev->name, readl(ioaddr + IntrStatus));
  1161. #ifndef final_version
  1162. /* Code that should never be run! Perhaps remove after testing.. */
  1163. {
  1164. static int stopit = 10;
  1165. if (dev->start == 0 && --stopit < 0) {
  1166. printk(KERN_ERR "%s: Emergency stop, looping startup interrupt.\n",
  1167. dev->name);
  1168. free_irq(irq, dev);
  1169. }
  1170. }
  1171. #endif
  1172. spin_unlock(&hmp->lock);
  1173. return IRQ_RETVAL(handled);
  1174. }
  1175. /* This routine is logically part of the interrupt handler, but separated
  1176. for clarity and better register allocation. */
  1177. static int hamachi_rx(struct net_device *dev)
  1178. {
  1179. struct hamachi_private *hmp = netdev_priv(dev);
  1180. int entry = hmp->cur_rx % RX_RING_SIZE;
  1181. int boguscnt = (hmp->dirty_rx + RX_RING_SIZE) - hmp->cur_rx;
  1182. if (hamachi_debug > 4) {
  1183. printk(KERN_DEBUG " In hamachi_rx(), entry %d status %4.4x.\n",
  1184. entry, hmp->rx_ring[entry].status_n_length);
  1185. }
  1186. /* If EOP is set on the next entry, it's a new packet. Send it up. */
  1187. while (1) {
  1188. struct hamachi_desc *desc = &(hmp->rx_ring[entry]);
  1189. u32 desc_status = le32_to_cpu(desc->status_n_length);
  1190. u16 data_size = desc_status; /* Implicit truncate */
  1191. u8 *buf_addr;
  1192. s32 frame_status;
  1193. if (desc_status & DescOwn)
  1194. break;
  1195. pci_dma_sync_single_for_cpu(hmp->pci_dev,
  1196. leXX_to_cpu(desc->addr),
  1197. hmp->rx_buf_sz,
  1198. PCI_DMA_FROMDEVICE);
  1199. buf_addr = (u8 *) hmp->rx_skbuff[entry]->data;
  1200. frame_status = get_unaligned_le32(&(buf_addr[data_size - 12]));
  1201. if (hamachi_debug > 4)
  1202. printk(KERN_DEBUG " hamachi_rx() status was %8.8x.\n",
  1203. frame_status);
  1204. if (--boguscnt < 0)
  1205. break;
  1206. if ( ! (desc_status & DescEndPacket)) {
  1207. printk(KERN_WARNING "%s: Oversized Ethernet frame spanned "
  1208. "multiple buffers, entry %#x length %d status %4.4x!\n",
  1209. dev->name, hmp->cur_rx, data_size, desc_status);
  1210. printk(KERN_WARNING "%s: Oversized Ethernet frame %p vs %p.\n",
  1211. dev->name, desc, &hmp->rx_ring[hmp->cur_rx % RX_RING_SIZE]);
  1212. printk(KERN_WARNING "%s: Oversized Ethernet frame -- next status %x/%x last status %x.\n",
  1213. dev->name,
  1214. le32_to_cpu(hmp->rx_ring[(hmp->cur_rx+1) % RX_RING_SIZE].status_n_length) & 0xffff0000,
  1215. le32_to_cpu(hmp->rx_ring[(hmp->cur_rx+1) % RX_RING_SIZE].status_n_length) & 0x0000ffff,
  1216. le32_to_cpu(hmp->rx_ring[(hmp->cur_rx-1) % RX_RING_SIZE].status_n_length));
  1217. dev->stats.rx_length_errors++;
  1218. } /* else Omit for prototype errata??? */
  1219. if (frame_status & 0x00380000) {
  1220. /* There was an error. */
  1221. if (hamachi_debug > 2)
  1222. printk(KERN_DEBUG " hamachi_rx() Rx error was %8.8x.\n",
  1223. frame_status);
  1224. dev->stats.rx_errors++;
  1225. if (frame_status & 0x00600000)
  1226. dev->stats.rx_length_errors++;
  1227. if (frame_status & 0x00080000)
  1228. dev->stats.rx_frame_errors++;
  1229. if (frame_status & 0x00100000)
  1230. dev->stats.rx_crc_errors++;
  1231. if (frame_status < 0)
  1232. dev->stats.rx_dropped++;
  1233. } else {
  1234. struct sk_buff *skb;
  1235. /* Omit CRC */
  1236. u16 pkt_len = (frame_status & 0x07ff) - 4;
  1237. #ifdef RX_CHECKSUM
  1238. u32 pfck = *(u32 *) &buf_addr[data_size - 8];
  1239. #endif
  1240. #ifndef final_version
  1241. if (hamachi_debug > 4)
  1242. printk(KERN_DEBUG " hamachi_rx() normal Rx pkt length %d"
  1243. " of %d, bogus_cnt %d.\n",
  1244. pkt_len, data_size, boguscnt);
  1245. if (hamachi_debug > 5)
  1246. printk(KERN_DEBUG"%s: rx status %8.8x %8.8x %8.8x %8.8x %8.8x.\n",
  1247. dev->name,
  1248. *(s32*)&(buf_addr[data_size - 20]),
  1249. *(s32*)&(buf_addr[data_size - 16]),
  1250. *(s32*)&(buf_addr[data_size - 12]),
  1251. *(s32*)&(buf_addr[data_size - 8]),
  1252. *(s32*)&(buf_addr[data_size - 4]));
  1253. #endif
  1254. /* Check if the packet is long enough to accept without copying
  1255. to a minimally-sized skbuff. */
  1256. if (pkt_len < rx_copybreak &&
  1257. (skb = netdev_alloc_skb(dev, pkt_len + 2)) != NULL) {
  1258. #ifdef RX_CHECKSUM
  1259. printk(KERN_ERR "%s: rx_copybreak non-zero "
  1260. "not good with RX_CHECKSUM\n", dev->name);
  1261. #endif
  1262. skb_reserve(skb, 2); /* 16 byte align the IP header */
  1263. pci_dma_sync_single_for_cpu(hmp->pci_dev,
  1264. leXX_to_cpu(hmp->rx_ring[entry].addr),
  1265. hmp->rx_buf_sz,
  1266. PCI_DMA_FROMDEVICE);
  1267. /* Call copy + cksum if available. */
  1268. #if 1 || USE_IP_COPYSUM
  1269. skb_copy_to_linear_data(skb,
  1270. hmp->rx_skbuff[entry]->data, pkt_len);
  1271. skb_put(skb, pkt_len);
  1272. #else
  1273. skb_put_data(skb, hmp->rx_ring_dma
  1274. + entry*sizeof(*desc), pkt_len);
  1275. #endif
  1276. pci_dma_sync_single_for_device(hmp->pci_dev,
  1277. leXX_to_cpu(hmp->rx_ring[entry].addr),
  1278. hmp->rx_buf_sz,
  1279. PCI_DMA_FROMDEVICE);
  1280. } else {
  1281. pci_unmap_single(hmp->pci_dev,
  1282. leXX_to_cpu(hmp->rx_ring[entry].addr),
  1283. hmp->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1284. skb_put(skb = hmp->rx_skbuff[entry], pkt_len);
  1285. hmp->rx_skbuff[entry] = NULL;
  1286. }
  1287. skb->protocol = eth_type_trans(skb, dev);
  1288. #ifdef RX_CHECKSUM
  1289. /* TCP or UDP on ipv4, DIX encoding */
  1290. if (pfck>>24 == 0x91 || pfck>>24 == 0x51) {
  1291. struct iphdr *ih = (struct iphdr *) skb->data;
  1292. /* Check that IP packet is at least 46 bytes, otherwise,
  1293. * there may be pad bytes included in the hardware checksum.
  1294. * This wouldn't happen if everyone padded with 0.
  1295. */
  1296. if (ntohs(ih->tot_len) >= 46){
  1297. /* don't worry about frags */
  1298. if (!(ih->frag_off & cpu_to_be16(IP_MF|IP_OFFSET))) {
  1299. u32 inv = *(u32 *) &buf_addr[data_size - 16];
  1300. u32 *p = (u32 *) &buf_addr[data_size - 20];
  1301. register u32 crc, p_r, p_r1;
  1302. if (inv & 4) {
  1303. inv &= ~4;
  1304. --p;
  1305. }
  1306. p_r = *p;
  1307. p_r1 = *(p-1);
  1308. switch (inv) {
  1309. case 0:
  1310. crc = (p_r & 0xffff) + (p_r >> 16);
  1311. break;
  1312. case 1:
  1313. crc = (p_r >> 16) + (p_r & 0xffff)
  1314. + (p_r1 >> 16 & 0xff00);
  1315. break;
  1316. case 2:
  1317. crc = p_r + (p_r1 >> 16);
  1318. break;
  1319. case 3:
  1320. crc = p_r + (p_r1 & 0xff00) + (p_r1 >> 16);
  1321. break;
  1322. default: /*NOTREACHED*/ crc = 0;
  1323. }
  1324. if (crc & 0xffff0000) {
  1325. crc &= 0xffff;
  1326. ++crc;
  1327. }
  1328. /* tcp/udp will add in pseudo */
  1329. skb->csum = ntohs(pfck & 0xffff);
  1330. if (skb->csum > crc)
  1331. skb->csum -= crc;
  1332. else
  1333. skb->csum += (~crc & 0xffff);
  1334. /*
  1335. * could do the pseudo myself and return
  1336. * CHECKSUM_UNNECESSARY
  1337. */
  1338. skb->ip_summed = CHECKSUM_COMPLETE;
  1339. }
  1340. }
  1341. }
  1342. #endif /* RX_CHECKSUM */
  1343. netif_rx(skb);
  1344. dev->stats.rx_packets++;
  1345. }
  1346. entry = (++hmp->cur_rx) % RX_RING_SIZE;
  1347. }
  1348. /* Refill the Rx ring buffers. */
  1349. for (; hmp->cur_rx - hmp->dirty_rx > 0; hmp->dirty_rx++) {
  1350. struct hamachi_desc *desc;
  1351. entry = hmp->dirty_rx % RX_RING_SIZE;
  1352. desc = &(hmp->rx_ring[entry]);
  1353. if (hmp->rx_skbuff[entry] == NULL) {
  1354. struct sk_buff *skb = netdev_alloc_skb(dev, hmp->rx_buf_sz + 2);
  1355. hmp->rx_skbuff[entry] = skb;
  1356. if (skb == NULL)
  1357. break; /* Better luck next round. */
  1358. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  1359. desc->addr = cpu_to_leXX(pci_map_single(hmp->pci_dev,
  1360. skb->data, hmp->rx_buf_sz, PCI_DMA_FROMDEVICE));
  1361. }
  1362. desc->status_n_length = cpu_to_le32(hmp->rx_buf_sz);
  1363. if (entry >= RX_RING_SIZE-1)
  1364. desc->status_n_length |= cpu_to_le32(DescOwn |
  1365. DescEndPacket | DescEndRing | DescIntr);
  1366. else
  1367. desc->status_n_length |= cpu_to_le32(DescOwn |
  1368. DescEndPacket | DescIntr);
  1369. }
  1370. /* Restart Rx engine if stopped. */
  1371. /* If we don't need to check status, don't. -KDU */
  1372. if (readw(hmp->base + RxStatus) & 0x0002)
  1373. writew(0x0001, hmp->base + RxCmd);
  1374. return 0;
  1375. }
  1376. /* This is more properly named "uncommon interrupt events", as it covers more
  1377. than just errors. */
  1378. static void hamachi_error(struct net_device *dev, int intr_status)
  1379. {
  1380. struct hamachi_private *hmp = netdev_priv(dev);
  1381. void __iomem *ioaddr = hmp->base;
  1382. if (intr_status & (LinkChange|NegotiationChange)) {
  1383. if (hamachi_debug > 1)
  1384. printk(KERN_INFO "%s: Link changed: AutoNegotiation Ctrl"
  1385. " %4.4x, Status %4.4x %4.4x Intr status %4.4x.\n",
  1386. dev->name, readw(ioaddr + 0x0E0), readw(ioaddr + 0x0E2),
  1387. readw(ioaddr + ANLinkPartnerAbility),
  1388. readl(ioaddr + IntrStatus));
  1389. if (readw(ioaddr + ANStatus) & 0x20)
  1390. writeb(0x01, ioaddr + LEDCtrl);
  1391. else
  1392. writeb(0x03, ioaddr + LEDCtrl);
  1393. }
  1394. if (intr_status & StatsMax) {
  1395. hamachi_get_stats(dev);
  1396. /* Read the overflow bits to clear. */
  1397. readl(ioaddr + 0x370);
  1398. readl(ioaddr + 0x3F0);
  1399. }
  1400. if ((intr_status & ~(LinkChange|StatsMax|NegotiationChange|IntrRxDone|IntrTxDone)) &&
  1401. hamachi_debug)
  1402. printk(KERN_ERR "%s: Something Wicked happened! %4.4x.\n",
  1403. dev->name, intr_status);
  1404. /* Hmmmmm, it's not clear how to recover from PCI faults. */
  1405. if (intr_status & (IntrTxPCIErr | IntrTxPCIFault))
  1406. dev->stats.tx_fifo_errors++;
  1407. if (intr_status & (IntrRxPCIErr | IntrRxPCIFault))
  1408. dev->stats.rx_fifo_errors++;
  1409. }
  1410. static int hamachi_close(struct net_device *dev)
  1411. {
  1412. struct hamachi_private *hmp = netdev_priv(dev);
  1413. void __iomem *ioaddr = hmp->base;
  1414. struct sk_buff *skb;
  1415. int i;
  1416. netif_stop_queue(dev);
  1417. if (hamachi_debug > 1) {
  1418. printk(KERN_DEBUG "%s: Shutting down ethercard, status was Tx %4.4x Rx %4.4x Int %2.2x.\n",
  1419. dev->name, readw(ioaddr + TxStatus),
  1420. readw(ioaddr + RxStatus), readl(ioaddr + IntrStatus));
  1421. printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
  1422. dev->name, hmp->cur_tx, hmp->dirty_tx, hmp->cur_rx, hmp->dirty_rx);
  1423. }
  1424. /* Disable interrupts by clearing the interrupt mask. */
  1425. writel(0x0000, ioaddr + InterruptEnable);
  1426. /* Stop the chip's Tx and Rx processes. */
  1427. writel(2, ioaddr + RxCmd);
  1428. writew(2, ioaddr + TxCmd);
  1429. #ifdef __i386__
  1430. if (hamachi_debug > 2) {
  1431. printk(KERN_DEBUG " Tx ring at %8.8x:\n",
  1432. (int)hmp->tx_ring_dma);
  1433. for (i = 0; i < TX_RING_SIZE; i++)
  1434. printk(KERN_DEBUG " %c #%d desc. %8.8x %8.8x.\n",
  1435. readl(ioaddr + TxCurPtr) == (long)&hmp->tx_ring[i] ? '>' : ' ',
  1436. i, hmp->tx_ring[i].status_n_length, hmp->tx_ring[i].addr);
  1437. printk(KERN_DEBUG " Rx ring %8.8x:\n",
  1438. (int)hmp->rx_ring_dma);
  1439. for (i = 0; i < RX_RING_SIZE; i++) {
  1440. printk(KERN_DEBUG " %c #%d desc. %4.4x %8.8x\n",
  1441. readl(ioaddr + RxCurPtr) == (long)&hmp->rx_ring[i] ? '>' : ' ',
  1442. i, hmp->rx_ring[i].status_n_length, hmp->rx_ring[i].addr);
  1443. if (hamachi_debug > 6) {
  1444. if (*(u8*)hmp->rx_skbuff[i]->data != 0x69) {
  1445. u16 *addr = (u16 *)
  1446. hmp->rx_skbuff[i]->data;
  1447. int j;
  1448. printk(KERN_DEBUG "Addr: ");
  1449. for (j = 0; j < 0x50; j++)
  1450. printk(" %4.4x", addr[j]);
  1451. printk("\n");
  1452. }
  1453. }
  1454. }
  1455. }
  1456. #endif /* __i386__ debugging only */
  1457. free_irq(hmp->pci_dev->irq, dev);
  1458. del_timer_sync(&hmp->timer);
  1459. /* Free all the skbuffs in the Rx queue. */
  1460. for (i = 0; i < RX_RING_SIZE; i++) {
  1461. skb = hmp->rx_skbuff[i];
  1462. hmp->rx_ring[i].status_n_length = 0;
  1463. if (skb) {
  1464. pci_unmap_single(hmp->pci_dev,
  1465. leXX_to_cpu(hmp->rx_ring[i].addr),
  1466. hmp->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1467. dev_kfree_skb(skb);
  1468. hmp->rx_skbuff[i] = NULL;
  1469. }
  1470. hmp->rx_ring[i].addr = cpu_to_leXX(0xBADF00D0); /* An invalid address. */
  1471. }
  1472. for (i = 0; i < TX_RING_SIZE; i++) {
  1473. skb = hmp->tx_skbuff[i];
  1474. if (skb) {
  1475. pci_unmap_single(hmp->pci_dev,
  1476. leXX_to_cpu(hmp->tx_ring[i].addr),
  1477. skb->len, PCI_DMA_TODEVICE);
  1478. dev_kfree_skb(skb);
  1479. hmp->tx_skbuff[i] = NULL;
  1480. }
  1481. }
  1482. writeb(0x00, ioaddr + LEDCtrl);
  1483. return 0;
  1484. }
  1485. static struct net_device_stats *hamachi_get_stats(struct net_device *dev)
  1486. {
  1487. struct hamachi_private *hmp = netdev_priv(dev);
  1488. void __iomem *ioaddr = hmp->base;
  1489. /* We should lock this segment of code for SMP eventually, although
  1490. the vulnerability window is very small and statistics are
  1491. non-critical. */
  1492. /* Ok, what goes here? This appears to be stuck at 21 packets
  1493. according to ifconfig. It does get incremented in hamachi_tx(),
  1494. so I think I'll comment it out here and see if better things
  1495. happen.
  1496. */
  1497. /* dev->stats.tx_packets = readl(ioaddr + 0x000); */
  1498. /* Total Uni+Brd+Multi */
  1499. dev->stats.rx_bytes = readl(ioaddr + 0x330);
  1500. /* Total Uni+Brd+Multi */
  1501. dev->stats.tx_bytes = readl(ioaddr + 0x3B0);
  1502. /* Multicast Rx */
  1503. dev->stats.multicast = readl(ioaddr + 0x320);
  1504. /* Over+Undersized */
  1505. dev->stats.rx_length_errors = readl(ioaddr + 0x368);
  1506. /* Jabber */
  1507. dev->stats.rx_over_errors = readl(ioaddr + 0x35C);
  1508. /* Jabber */
  1509. dev->stats.rx_crc_errors = readl(ioaddr + 0x360);
  1510. /* Symbol Errs */
  1511. dev->stats.rx_frame_errors = readl(ioaddr + 0x364);
  1512. /* Dropped */
  1513. dev->stats.rx_missed_errors = readl(ioaddr + 0x36C);
  1514. return &dev->stats;
  1515. }
  1516. static void set_rx_mode(struct net_device *dev)
  1517. {
  1518. struct hamachi_private *hmp = netdev_priv(dev);
  1519. void __iomem *ioaddr = hmp->base;
  1520. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1521. writew(0x000F, ioaddr + AddrMode);
  1522. } else if ((netdev_mc_count(dev) > 63) || (dev->flags & IFF_ALLMULTI)) {
  1523. /* Too many to match, or accept all multicasts. */
  1524. writew(0x000B, ioaddr + AddrMode);
  1525. } else if (!netdev_mc_empty(dev)) { /* Must use the CAM filter. */
  1526. struct netdev_hw_addr *ha;
  1527. int i = 0;
  1528. netdev_for_each_mc_addr(ha, dev) {
  1529. writel(*(u32 *)(ha->addr), ioaddr + 0x100 + i*8);
  1530. writel(0x20000 | (*(u16 *)&ha->addr[4]),
  1531. ioaddr + 0x104 + i*8);
  1532. i++;
  1533. }
  1534. /* Clear remaining entries. */
  1535. for (; i < 64; i++)
  1536. writel(0, ioaddr + 0x104 + i*8);
  1537. writew(0x0003, ioaddr + AddrMode);
  1538. } else { /* Normal, unicast/broadcast-only mode. */
  1539. writew(0x0001, ioaddr + AddrMode);
  1540. }
  1541. }
  1542. static int check_if_running(struct net_device *dev)
  1543. {
  1544. if (!netif_running(dev))
  1545. return -EINVAL;
  1546. return 0;
  1547. }
  1548. static void hamachi_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1549. {
  1550. struct hamachi_private *np = netdev_priv(dev);
  1551. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1552. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1553. strlcpy(info->bus_info, pci_name(np->pci_dev), sizeof(info->bus_info));
  1554. }
  1555. static int hamachi_get_link_ksettings(struct net_device *dev,
  1556. struct ethtool_link_ksettings *cmd)
  1557. {
  1558. struct hamachi_private *np = netdev_priv(dev);
  1559. spin_lock_irq(&np->lock);
  1560. mii_ethtool_get_link_ksettings(&np->mii_if, cmd);
  1561. spin_unlock_irq(&np->lock);
  1562. return 0;
  1563. }
  1564. static int hamachi_set_link_ksettings(struct net_device *dev,
  1565. const struct ethtool_link_ksettings *cmd)
  1566. {
  1567. struct hamachi_private *np = netdev_priv(dev);
  1568. int res;
  1569. spin_lock_irq(&np->lock);
  1570. res = mii_ethtool_set_link_ksettings(&np->mii_if, cmd);
  1571. spin_unlock_irq(&np->lock);
  1572. return res;
  1573. }
  1574. static int hamachi_nway_reset(struct net_device *dev)
  1575. {
  1576. struct hamachi_private *np = netdev_priv(dev);
  1577. return mii_nway_restart(&np->mii_if);
  1578. }
  1579. static u32 hamachi_get_link(struct net_device *dev)
  1580. {
  1581. struct hamachi_private *np = netdev_priv(dev);
  1582. return mii_link_ok(&np->mii_if);
  1583. }
  1584. static const struct ethtool_ops ethtool_ops = {
  1585. .begin = check_if_running,
  1586. .get_drvinfo = hamachi_get_drvinfo,
  1587. .nway_reset = hamachi_nway_reset,
  1588. .get_link = hamachi_get_link,
  1589. .get_link_ksettings = hamachi_get_link_ksettings,
  1590. .set_link_ksettings = hamachi_set_link_ksettings,
  1591. };
  1592. static const struct ethtool_ops ethtool_ops_no_mii = {
  1593. .begin = check_if_running,
  1594. .get_drvinfo = hamachi_get_drvinfo,
  1595. };
  1596. static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1597. {
  1598. struct hamachi_private *np = netdev_priv(dev);
  1599. struct mii_ioctl_data *data = if_mii(rq);
  1600. int rc;
  1601. if (!netif_running(dev))
  1602. return -EINVAL;
  1603. if (cmd == (SIOCDEVPRIVATE+3)) { /* set rx,tx intr params */
  1604. u32 *d = (u32 *)&rq->ifr_ifru;
  1605. /* Should add this check here or an ordinary user can do nasty
  1606. * things. -KDU
  1607. *
  1608. * TODO: Shut down the Rx and Tx engines while doing this.
  1609. */
  1610. if (!capable(CAP_NET_ADMIN))
  1611. return -EPERM;
  1612. writel(d[0], np->base + TxIntrCtrl);
  1613. writel(d[1], np->base + RxIntrCtrl);
  1614. printk(KERN_NOTICE "%s: tx %08x, rx %08x intr\n", dev->name,
  1615. (u32) readl(np->base + TxIntrCtrl),
  1616. (u32) readl(np->base + RxIntrCtrl));
  1617. rc = 0;
  1618. }
  1619. else {
  1620. spin_lock_irq(&np->lock);
  1621. rc = generic_mii_ioctl(&np->mii_if, data, cmd, NULL);
  1622. spin_unlock_irq(&np->lock);
  1623. }
  1624. return rc;
  1625. }
  1626. static void hamachi_remove_one(struct pci_dev *pdev)
  1627. {
  1628. struct net_device *dev = pci_get_drvdata(pdev);
  1629. if (dev) {
  1630. struct hamachi_private *hmp = netdev_priv(dev);
  1631. pci_free_consistent(pdev, RX_TOTAL_SIZE, hmp->rx_ring,
  1632. hmp->rx_ring_dma);
  1633. pci_free_consistent(pdev, TX_TOTAL_SIZE, hmp->tx_ring,
  1634. hmp->tx_ring_dma);
  1635. unregister_netdev(dev);
  1636. iounmap(hmp->base);
  1637. free_netdev(dev);
  1638. pci_release_regions(pdev);
  1639. }
  1640. }
  1641. static const struct pci_device_id hamachi_pci_tbl[] = {
  1642. { 0x1318, 0x0911, PCI_ANY_ID, PCI_ANY_ID, },
  1643. { 0, }
  1644. };
  1645. MODULE_DEVICE_TABLE(pci, hamachi_pci_tbl);
  1646. static struct pci_driver hamachi_driver = {
  1647. .name = DRV_NAME,
  1648. .id_table = hamachi_pci_tbl,
  1649. .probe = hamachi_init_one,
  1650. .remove = hamachi_remove_one,
  1651. };
  1652. static int __init hamachi_init (void)
  1653. {
  1654. /* when a module, this is printed whether or not devices are found in probe */
  1655. #ifdef MODULE
  1656. printk(version);
  1657. #endif
  1658. return pci_register_driver(&hamachi_driver);
  1659. }
  1660. static void __exit hamachi_exit (void)
  1661. {
  1662. pci_unregister_driver(&hamachi_driver);
  1663. }
  1664. module_init(hamachi_init);
  1665. module_exit(hamachi_exit);