source: rtems/c/src/libchip/network/elnk.c @ 0b6ca8aa

4.104.115
Last change on this file since 0b6ca8aa was 0b6ca8aa, checked in by Joel Sherrill <joel.sherrill@…>, on 09/20/08 at 22:20:14

2008-09-20 Joel Sherrill <joel.sherrill@…>

  • libchip/network/dec21140.c, libchip/network/elnk.c: Detect when there is no PCI bus based upon the BSP not having a base address defined. This means this driver cannot be supported on that board.
  • Property mode set to 100644
File size: 98.7 KB
Line 
1/*
2 *  RTEMS driver for Etherlink based Ethernet Controllers
3 *
4 *  Copyright (C) 2003, Gregory Menke, NASA/GSFC
5 *
6 *  The license and distribution terms for this file may be
7 *  found in found in the file LICENSE in this distribution or at
8 *  http://www.rtems.com/license/LICENSE.
9 *
10 * elnk.c
11 *
12 *
13 */
14
15
16/*
17 * Portions of this driver are taken from the Freebsd if_xl.c driver,
18 * version "1.133 2003/03/19 01:48:14" and are covered by the license
19 * text included below that was taken verbatim from the original file.
20 * More particularly, all structures, variables, and #defines prefixed
21 * with XL_ or xl_, along with their associated comments were taken
22 * directly from the Freebsd driver and modified as required to suit the
23 * purposes of this one.  Additionally, much of the device setup &
24 * manipulation logic was also copied and modified to suit.  All types
25 * and functions beginning with elnk are either my own creations or were
26 * adapted from other RTEMS components, and regardless, are subject to
27 * the standard OAR licensing terms given in the comments at the top of
28 * this file.
29 *
30 * Greg Menke, 6/11/2003
31 */
32
33 /*
34 * Copyright (c) 1997, 1998, 1999
35 *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 *    must display the following acknowledgement:
47 *      This product includes software developed by Bill Paul.
48 * 4. Neither the name of the author nor the names of any co-contributors
49 *    may be used to endorse or promote products derived from this software
50 *    without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
62 * THE POSSIBILITY OF SUCH DAMAGE.
63 */
64
65#include <rtems.h>
66
67/*
68 *  This driver only supports architectures with the new style
69 *  exception processing.  The following checks try to keep this
70 *  from being compiled on systems which can't support this driver.
71 */
72
73#if defined(__i386__)
74#define ELNK_SUPPORTED
75#endif
76
77#if defined(__PPC__) && (defined(mpc604) || defined(mpc750) || defined(ppc603e))
78#define ELNK_SUPPORTED
79#endif
80
81#if !defined(PCI_DRAM_OFFSET)
82  #undef ELNK_SUPPORTED
83#endif
84
85/* #undef ELNK_SUPPORTED */
86
87
88#if defined(ELNK_SUPPORTED)
89#include <bsp.h>
90#include <rtems/pci.h>
91
92#if defined(__PPC__)
93#include <libcpu/byteorder.h>
94#include <libcpu/io.h>
95#endif
96
97#if defined(__i386__)
98#include <libcpu/byteorder.h>
99#endif
100
101#include <stdlib.h>
102#include <stdio.h>
103#include <stdarg.h>
104#include <string.h>
105#include <errno.h>
106#include <rtems/error.h>
107#include <rtems/bspIo.h>
108#include <rtems/rtems_bsdnet.h>
109
110#include <sys/param.h>
111#include <sys/mbuf.h>
112
113#include <sys/socket.h>
114#include <sys/sockio.h>
115#include <net/if.h>
116#include <netinet/in.h>
117#include <netinet/if_ether.h>
118
119#include <net/if_media.h>
120#include <dev/mii/mii.h>
121#include <bsp/irq.h>
122
123#if defined(__i386__)
124#define IO_MASK   0x3
125#define MEM_MASK  0xF
126#endif
127
128#ifdef malloc
129#undef malloc
130#endif
131#ifdef free
132#undef free
133#endif
134
135#define ELNK_DEBUG
136
137
138#define DRIVER_PREFIX   "elnk"
139
140
141
142
143
144/*
145* These buffers allocated for each unit, so ensure
146*
147*   rtems_bsdnet_config.mbuf_bytecount
148*   rtems_bsdnet_config.mbuf_cluster_bytecount
149*
150* are adequately sized to provide enough clusters and mbufs for all the
151* units.  The default bsdnet configuration is sufficient for one unit,
152* but will be nearing exhaustion with 2 or more.  Although a little
153* expensive in memory, the following configuration should eliminate all
154* mbuf/cluster issues;
155*
156*   rtems_bsdnet_config.mbuf_bytecount           = 128*1024;
157*   rtems_bsdnet_config.mbuf_cluster_bytecount   = 256*1024;
158*
159* The default size in buffers of the rx & tx rings are given below.
160* This driver honors the rtems_bsdnet_ifconfig fields 'rbuf_count' and
161* 'xbuf_count', allowing the user to specify something else.
162*/
163
164#define RX_RING_SIZE 16 /* default number of receive buffers */
165#define TX_RING_SIZE 16 /* default number of transmit buffers */
166
167
168/*
169 * Number of boards supported by this driver
170 */
171#define NUM_UNITS       8
172
173/*
174 * Receive buffer size -- Allow for a full ethernet packet including CRC
175 */
176#define XL_PACKET_SIZE  1540
177
178
179/*
180** Events, one per unit.  The event is sent to the rx task from the isr
181** or from the stack to the tx task whenever a unit needs service.  The
182** rx/tx tasks identify the requesting unit(s) by their particular
183** events so only requesting units are serviced.
184*/
185
186static rtems_event_set unit_signals[NUM_UNITS]= { RTEMS_EVENT_1,
187                                                  RTEMS_EVENT_2,
188                                                  RTEMS_EVENT_3,
189                                                  RTEMS_EVENT_4,
190                                                  RTEMS_EVENT_5,
191                                                  RTEMS_EVENT_6,
192                                                  RTEMS_EVENT_7,
193                                                  RTEMS_EVENT_8 };
194
195
196
197
198#if defined(__PPC__)
199#define phys_to_bus(address) ((unsigned int)((address)) + PCI_DRAM_OFFSET)
200#define bus_to_phys(address) ((unsigned int)((address)) - PCI_DRAM_OFFSET)
201#define CPU_CACHE_ALIGNMENT_FOR_BUFFER PPC_CACHE_ALIGNMENT
202#else
203extern void Wait_X_ms( unsigned int timeToWait );
204#define phys_to_bus(address) ((unsigned int) ((address)))
205#define bus_to_phys(address) ((unsigned int) ((address)))
206#define rtems_bsp_delay_in_bus_cycles(cycle) Wait_X_ms( cycle/100 )
207#define CPU_CACHE_ALIGNMENT_FOR_BUFFER PG_SIZE
208#endif
209
210/* the actual duration waited in DELAY is not especially predictable,
211 * though it will be consistent on a given host.  It should not be
212 * relied upon for specific timing given the vague per-bsp,
213 * per-architecture implementation of the actual delay function.  It
214 * would probably be helpful to make this more accurate at some point...
215 */
216#define DELAY(n)        rtems_bsp_delay_in_bus_cycles( n*20  )
217
218
219
220
221/*
222 * Register layouts.
223 */
224#define XL_COMMAND              0x0E
225#define XL_STATUS               0x0E
226
227#define XL_TX_STATUS            0x1B
228#define XL_TX_FREE              0x1C
229#define XL_DMACTL               0x20
230#define XL_DOWNLIST_PTR         0x24
231#define XL_DOWN_POLL            0x2D /* 3c90xB only */
232#define XL_TX_FREETHRESH        0x2F
233#define XL_UPLIST_PTR           0x38
234#define XL_UPLIST_STATUS        0x30
235#define XL_UP_POLL              0x3D /* 3c90xB only */
236
237#define XL_PKTSTAT_UP_STALLED           0x00002000
238#define XL_PKTSTAT_UP_ERROR             0x00004000
239#define XL_PKTSTAT_UP_CMPLT             0x00008000
240
241#define XL_DMACTL_DN_CMPLT_REQ          0x00000002
242#define XL_DMACTL_DOWN_STALLED          0x00000004
243#define XL_DMACTL_UP_CMPLT              0x00000008
244#define XL_DMACTL_DOWN_CMPLT            0x00000010
245#define XL_DMACTL_UP_RX_EARLY           0x00000020
246#define XL_DMACTL_ARM_COUNTDOWN         0x00000040
247#define XL_DMACTL_DOWN_INPROG           0x00000080
248#define XL_DMACTL_COUNTER_SPEED         0x00000100
249#define XL_DMACTL_DOWNDOWN_MODE         0x00000200
250#define XL_DMACTL_TARGET_ABORT          0x40000000
251#define XL_DMACTL_MASTER_ABORT          0x80000000
252
253/*
254 * Command codes. Some command codes require that we wait for
255 * the CMD_BUSY flag to clear. Those codes are marked as 'mustwait.'
256 */
257#define XL_CMD_RESET            0x0000  /* mustwait */
258#define XL_CMD_WINSEL           0x0800
259#define XL_CMD_COAX_START       0x1000
260#define XL_CMD_RX_DISABLE       0x1800
261#define XL_CMD_RX_ENABLE        0x2000
262#define XL_CMD_RX_RESET         0x2800  /* mustwait */
263#define XL_CMD_UP_STALL         0x3000  /* mustwait */
264#define XL_CMD_UP_UNSTALL       0x3001
265#define XL_CMD_DOWN_STALL       0x3002  /* mustwait */
266#define XL_CMD_DOWN_UNSTALL     0x3003
267#define XL_CMD_RX_DISCARD       0x4000
268#define XL_CMD_TX_ENABLE        0x4800
269#define XL_CMD_TX_DISABLE       0x5000
270#define XL_CMD_TX_RESET         0x5800  /* mustwait */
271#define XL_CMD_INTR_FAKE        0x6000
272#define XL_CMD_INTR_ACK         0x6800
273#define XL_CMD_INTR_ENB         0x7000
274#define XL_CMD_STAT_ENB         0x7800
275#define XL_CMD_RX_SET_FILT      0x8000
276#define XL_CMD_RX_SET_THRESH    0x8800
277#define XL_CMD_TX_SET_THRESH    0x9000
278#define XL_CMD_TX_SET_START     0x9800
279#define XL_CMD_DMA_UP           0xA000
280#define XL_CMD_DMA_STOP         0xA001
281#define XL_CMD_STATS_ENABLE     0xA800
282#define XL_CMD_STATS_DISABLE    0xB000
283#define XL_CMD_COAX_STOP        0xB800
284
285#define XL_CMD_SET_TX_RECLAIM   0xC000 /* 3c905B only */
286#define XL_CMD_RX_SET_HASH      0xC800 /* 3c905B only */
287
288#define XL_HASH_SET             0x0400
289#define XL_HASHFILT_SIZE        256
290
291/*
292 * status codes
293 * Note that bits 15 to 13 indicate the currently visible register window
294 * which may be anything from 0 to 7.
295 */
296#define XL_STAT_INTLATCH        0x0001  /* 0 */
297#define XL_STAT_ADFAIL          0x0002  /* 1 */
298#define XL_STAT_TX_COMPLETE     0x0004  /* 2 */
299#define XL_STAT_TX_AVAIL        0x0008  /* 3 first generation */
300#define XL_STAT_RX_COMPLETE     0x0010  /* 4 */
301#define XL_STAT_RX_EARLY        0x0020  /* 5 */
302#define XL_STAT_INTREQ          0x0040  /* 6 */
303#define XL_STAT_STATSOFLOW      0x0080  /* 7 */
304#define XL_STAT_DMADONE         0x0100  /* 8 first generation */
305#define XL_STAT_LINKSTAT        0x0100  /* 8 3c509B */
306#define XL_STAT_DOWN_COMPLETE   0x0200  /* 9 */
307#define XL_STAT_UP_COMPLETE     0x0400  /* 10 */
308#define XL_STAT_DMABUSY         0x0800  /* 11 first generation */
309#define XL_STAT_CMDBUSY         0x1000  /* 12 */
310
311/*
312 * Interrupts we normally want enabled.
313 */
314#define XL_INTRS        \
315                  (XL_STAT_UP_COMPLETE | XL_STAT_STATSOFLOW | XL_STAT_ADFAIL|   \
316                   XL_STAT_DOWN_COMPLETE | XL_STAT_TX_COMPLETE | XL_STAT_INTLATCH)
317
318
319
320/*
321 * General constants that are fun to know.
322 *
323 * 3Com PCI vendor ID
324 */
325#define TC_VENDORID             0x10B7
326
327/*
328 * 3Com chip device IDs.
329 */
330#define TC_DEVICEID_BOOMERANG_10BT              0x9000
331#define TC_DEVICEID_BOOMERANG_10BT_COMBO        0x9001
332#define TC_DEVICEID_BOOMERANG_10_100BT          0x9050
333#define TC_DEVICEID_BOOMERANG_100BT4            0x9051
334#define TC_DEVICEID_KRAKATOA_10BT               0x9004
335#define TC_DEVICEID_KRAKATOA_10BT_COMBO         0x9005
336#define TC_DEVICEID_KRAKATOA_10BT_TPC           0x9006
337#define TC_DEVICEID_CYCLONE_10FL                0x900A
338#define TC_DEVICEID_HURRICANE_10_100BT          0x9055
339#define TC_DEVICEID_CYCLONE_10_100BT4           0x9056
340#define TC_DEVICEID_CYCLONE_10_100_COMBO        0x9058
341#define TC_DEVICEID_CYCLONE_10_100FX            0x905A
342#define TC_DEVICEID_TORNADO_10_100BT            0x9200
343#define TC_DEVICEID_TORNADO_10_100BT_920B       0x9201
344#define TC_DEVICEID_HURRICANE_10_100BT_SERV     0x9800
345#define TC_DEVICEID_TORNADO_10_100BT_SERV       0x9805
346#define TC_DEVICEID_HURRICANE_SOHO100TX         0x7646
347#define TC_DEVICEID_TORNADO_HOMECONNECT         0x4500
348#define TC_DEVICEID_HURRICANE_555               0x5055
349#define TC_DEVICEID_HURRICANE_556               0x6055
350#define TC_DEVICEID_HURRICANE_556B              0x6056
351#define TC_DEVICEID_HURRICANE_575A              0x5057
352#define TC_DEVICEID_HURRICANE_575B              0x5157
353#define TC_DEVICEID_HURRICANE_575C              0x5257
354#define TC_DEVICEID_HURRICANE_656               0x6560
355#define TC_DEVICEID_HURRICANE_656B              0x6562
356#define TC_DEVICEID_TORNADO_656C                0x6564
357
358
359
360#define XL_RXSTAT_LENMASK       0x00001FFF
361#define XL_RXSTAT_UP_ERROR      0x00004000
362#define XL_RXSTAT_UP_CMPLT      0x00008000
363#define XL_RXSTAT_UP_OVERRUN    0x00010000
364#define XL_RXSTAT_RUNT          0x00020000
365#define XL_RXSTAT_ALIGN         0x00040000
366#define XL_RXSTAT_CRC           0x00080000
367#define XL_RXSTAT_OVERSIZE      0x00100000
368#define XL_RXSTAT_DRIBBLE       0x00800000
369#define XL_RXSTAT_UP_OFLOW      0x01000000
370#define XL_RXSTAT_IPCKERR       0x02000000      /* 3c905B only */
371#define XL_RXSTAT_TCPCKERR      0x04000000      /* 3c905B only */
372#define XL_RXSTAT_UDPCKERR      0x08000000      /* 3c905B only */
373#define XL_RXSTAT_BUFEN         0x10000000      /* 3c905B only */
374#define XL_RXSTAT_IPCKOK        0x20000000      /* 3c905B only */
375#define XL_RXSTAT_TCPCOK        0x40000000      /* 3c905B only */
376#define XL_RXSTAT_UDPCKOK       0x80000000      /* 3c905B only */
377
378#define XL_TXSTAT_LENMASK       0x00001FFF
379#define XL_TXSTAT_CRCDIS        0x00002000
380#define XL_TXSTAT_TX_INTR       0x00008000
381#define XL_TXSTAT_DL_COMPLETE   0x00010000
382#define XL_TXSTAT_IPCKSUM       0x02000000      /* 3c905B only */
383#define XL_TXSTAT_TCPCKSUM      0x04000000      /* 3c905B only */
384#define XL_TXSTAT_UDPCKSUM      0x08000000      /* 3c905B only */
385#define XL_TXSTAT_RND_DEFEAT    0x10000000      /* 3c905B only */
386#define XL_TXSTAT_EMPTY         0x20000000      /* 3c905B only */
387#define XL_TXSTAT_DL_INTR       0x80000000
388
389
390#define XL_FLAG_FUNCREG                 0x0001
391#define XL_FLAG_PHYOK                   0x0002
392#define XL_FLAG_EEPROM_OFFSET_30        0x0004
393#define XL_FLAG_WEIRDRESET              0x0008
394#define XL_FLAG_8BITROM                 0x0010
395#define XL_FLAG_INVERT_LED_PWR          0x0020
396#define XL_FLAG_INVERT_MII_PWR          0x0040
397#define XL_FLAG_NO_XCVR_PWR             0x0080
398#define XL_FLAG_USE_MMIO                0x0100
399
400
401
402#define XL_EE_READ      0x0080  /* read, 5 bit address */
403#define XL_EE_WRITE     0x0040  /* write, 5 bit address */
404#define XL_EE_ERASE     0x00c0  /* erase, 5 bit address */
405#define XL_EE_EWEN      0x0030  /* erase, no data needed */
406#define XL_EE_8BIT_READ 0x0200  /* read, 8 bit address */
407#define XL_EE_BUSY      0x8000
408
409#define XL_EE_EADDR0    0x00    /* station address, first word */
410#define XL_EE_EADDR1    0x01    /* station address, next word, */
411#define XL_EE_EADDR2    0x02    /* station address, last word */
412#define XL_EE_PRODID    0x03    /* product ID code */
413#define XL_EE_MDATA_DATE        0x04    /* manufacturing data, date */
414#define XL_EE_MDATA_DIV         0x05    /* manufacturing data, division */
415#define XL_EE_MDATA_PCODE       0x06    /* manufacturing data, product code */
416#define XL_EE_MFG_ID    0x07
417#define XL_EE_PCI_PARM  0x08
418#define XL_EE_ROM_ONFO  0x09
419#define XL_EE_OEM_ADR0  0x0A
420#define XL_EE_OEM_ADR1  0x0B
421#define XL_EE_OEM_ADR2  0x0C
422#define XL_EE_SOFTINFO1 0x0D
423#define XL_EE_COMPAT    0x0E
424#define XL_EE_SOFTINFO2 0x0F
425#define XL_EE_CAPS      0x10    /* capabilities word */
426#define XL_EE_RSVD0     0x11
427#define XL_EE_ICFG_0    0x12
428#define XL_EE_ICFG_1    0x13
429#define XL_EE_RSVD1     0x14
430#define XL_EE_SOFTINFO3 0x15
431#define XL_EE_RSVD_2    0x16
432
433/*
434 * Bits in the capabilities word
435 */
436#define XL_CAPS_PNP             0x0001
437#define XL_CAPS_FULL_DUPLEX     0x0002
438#define XL_CAPS_LARGE_PKTS      0x0004
439#define XL_CAPS_SLAVE_DMA       0x0008
440#define XL_CAPS_SECOND_DMA      0x0010
441#define XL_CAPS_FULL_BM         0x0020
442#define XL_CAPS_FRAG_BM         0x0040
443#define XL_CAPS_CRC_PASSTHRU    0x0080
444#define XL_CAPS_TXDONE          0x0100
445#define XL_CAPS_NO_TXLENGTH     0x0200
446#define XL_CAPS_RX_REPEAT       0x0400
447#define XL_CAPS_SNOOPING        0x0800
448#define XL_CAPS_100MBPS         0x1000
449#define XL_CAPS_PWRMGMT         0x2000
450
451
452
453/*
454 * Window 0 registers
455 */
456#define XL_W0_EE_DATA           0x0C
457#define XL_W0_EE_CMD            0x0A
458#define XL_W0_RSRC_CFG          0x08
459#define XL_W0_ADDR_CFG          0x06
460#define XL_W0_CFG_CTRL          0x04
461
462#define XL_W0_PROD_ID           0x02
463#define XL_W0_MFG_ID            0x00
464
465/*
466 * Window 1
467 */
468
469#define XL_W1_TX_FIFO           0x10
470
471#define XL_W1_FREE_TX           0x0C
472#define XL_W1_TX_STATUS         0x0B
473#define XL_W1_TX_TIMER          0x0A
474#define XL_W1_RX_STATUS         0x08
475#define XL_W1_RX_FIFO           0x00
476
477/*
478 * RX status codes
479 */
480#define XL_RXSTATUS_OVERRUN     0x01
481#define XL_RXSTATUS_RUNT        0x02
482#define XL_RXSTATUS_ALIGN       0x04
483#define XL_RXSTATUS_CRC         0x08
484#define XL_RXSTATUS_OVERSIZE    0x10
485#define XL_RXSTATUS_DRIBBLE     0x20
486
487/*
488 * TX status codes
489 */
490#define XL_TXSTATUS_RECLAIM     0x02 /* 3c905B only */
491#define XL_TXSTATUS_OVERFLOW    0x04
492#define XL_TXSTATUS_MAXCOLS     0x08
493#define XL_TXSTATUS_UNDERRUN    0x10
494#define XL_TXSTATUS_JABBER      0x20
495#define XL_TXSTATUS_INTREQ      0x40
496#define XL_TXSTATUS_COMPLETE    0x80
497
498/*
499 * Window 2
500 */
501#define XL_W2_RESET_OPTIONS     0x0C    /* 3c905B only */
502#define XL_W2_STATION_MASK_HI   0x0A
503#define XL_W2_STATION_MASK_MID  0x08
504#define XL_W2_STATION_MASK_LO   0x06
505#define XL_W2_STATION_ADDR_HI   0x04
506#define XL_W2_STATION_ADDR_MID  0x02
507#define XL_W2_STATION_ADDR_LO   0x00
508
509#define XL_RESETOPT_FEATUREMASK 0x0001|0x0002|0x004
510#define XL_RESETOPT_D3RESETDIS  0x0008
511#define XL_RESETOPT_DISADVFD    0x0010
512#define XL_RESETOPT_DISADV100   0x0020
513#define XL_RESETOPT_DISAUTONEG  0x0040
514#define XL_RESETOPT_DEBUGMODE   0x0080
515#define XL_RESETOPT_FASTAUTO    0x0100
516#define XL_RESETOPT_FASTEE      0x0200
517#define XL_RESETOPT_FORCEDCONF  0x0400
518#define XL_RESETOPT_TESTPDTPDR  0x0800
519#define XL_RESETOPT_TEST100TX   0x1000
520#define XL_RESETOPT_TEST100RX   0x2000
521
522#define XL_RESETOPT_INVERT_LED  0x0010
523#define XL_RESETOPT_INVERT_MII  0x4000
524
525/*
526 * Window 3 (fifo management)
527 */
528#define XL_W3_INTERNAL_CFG      0x00
529#define XL_W3_MAXPKTSIZE        0x04    /* 3c905B only */
530#define XL_W3_RESET_OPT         0x08
531#define XL_W3_FREE_TX           0x0C
532#define XL_W3_FREE_RX           0x0A
533#define XL_W3_MAC_CTRL          0x06
534
535#define XL_ICFG_CONNECTOR_MASK  0x00F00000
536#define XL_ICFG_CONNECTOR_BITS  20
537
538#define XL_ICFG_RAMSIZE_MASK    0x00000007
539#define XL_ICFG_RAMWIDTH        0x00000008
540#define XL_ICFG_ROMSIZE_MASK    (0x00000040|0x00000080)
541#define XL_ICFG_DISABLE_BASSD   0x00000100
542#define XL_ICFG_RAMLOC          0x00000200
543#define XL_ICFG_RAMPART         (0x00010000|0x00020000)
544#define XL_ICFG_XCVRSEL         (0x00100000|0x00200000|0x00400000)
545#define XL_ICFG_AUTOSEL         0x01000000
546
547#define XL_XCVR_10BT            0x00
548#define XL_XCVR_AUI             0x01
549#define XL_XCVR_RSVD_0          0x02
550#define XL_XCVR_COAX            0x03
551#define XL_XCVR_100BTX          0x04
552#define XL_XCVR_100BFX          0x05
553#define XL_XCVR_MII             0x06
554#define XL_XCVR_RSVD_1          0x07
555#define XL_XCVR_AUTO            0x08    /* 3c905B only */
556
557#define XL_MACCTRL_DEFER_EXT_END        0x0001
558#define XL_MACCTRL_DEFER_0              0x0002
559#define XL_MACCTRL_DEFER_1              0x0004
560#define XL_MACCTRL_DEFER_2              0x0008
561#define XL_MACCTRL_DEFER_3              0x0010
562#define XL_MACCTRL_DUPLEX               0x0020
563#define XL_MACCTRL_ALLOW_LARGE_PACK     0x0040
564#define XL_MACCTRL_EXTEND_AFTER_COL     0x0080 (3c905B only)
565#define XL_MACCTRL_FLOW_CONTROL_ENB     0x0100 (3c905B only)
566#define XL_MACCTRL_VLT_END              0x0200 (3c905B only)
567
568/*
569 * The 'reset options' register contains power-on reset values
570 * loaded from the EEPROM. This includes the supported media
571 * types on the card. It is also known as the media options register.
572 */
573#define XL_W3_MEDIA_OPT         0x08
574
575#define XL_MEDIAOPT_BT4         0x0001  /* MII */
576#define XL_MEDIAOPT_BTX         0x0002  /* on-chip */
577#define XL_MEDIAOPT_BFX         0x0004  /* on-chip */
578#define XL_MEDIAOPT_BT          0x0008  /* on-chip */
579#define XL_MEDIAOPT_BNC         0x0010  /* on-chip */
580#define XL_MEDIAOPT_AUI         0x0020  /* on-chip */
581#define XL_MEDIAOPT_MII         0x0040  /* MII */
582#define XL_MEDIAOPT_VCO         0x0100  /* 1st gen chip only */
583
584#define XL_MEDIAOPT_10FL        0x0100  /* 3x905B only, on-chip */
585#define XL_MEDIAOPT_MASK        0x01FF
586
587/*
588 * Window 4 (diagnostics)
589 */
590#define XL_W4_UPPERBYTESOK      0x0D
591#define XL_W4_BADSSD            0x0C
592#define XL_W4_MEDIA_STATUS      0x0A
593#define XL_W4_PHY_MGMT          0x08
594#define XL_W4_NET_DIAG          0x06
595#define XL_W4_FIFO_DIAG         0x04
596#define XL_W4_VCO_DIAG          0x02
597
598#define XL_W4_CTRLR_STAT        0x08
599#define XL_W4_TX_DIAG           0x00
600
601#define XL_MII_CLK              0x01
602#define XL_MII_DATA             0x02
603#define XL_MII_DIR              0x04
604
605#define XL_MEDIA_SQE            0x0008
606#define XL_MEDIA_10TP           0x00C0
607#define XL_MEDIA_LNK            0x0080
608#define XL_MEDIA_LNKBEAT        0x0800
609
610#define XL_MEDIASTAT_CRCSTRIP   0x0004
611#define XL_MEDIASTAT_SQEENB     0x0008
612#define XL_MEDIASTAT_COLDET     0x0010
613#define XL_MEDIASTAT_CARRIER    0x0020
614#define XL_MEDIASTAT_JABGUARD   0x0040
615#define XL_MEDIASTAT_LINKBEAT   0x0080
616#define XL_MEDIASTAT_JABDETECT  0x0200
617#define XL_MEDIASTAT_POLREVERS  0x0400
618#define XL_MEDIASTAT_LINKDETECT 0x0800
619#define XL_MEDIASTAT_TXINPROG   0x1000
620#define XL_MEDIASTAT_DCENB      0x4000
621#define XL_MEDIASTAT_AUIDIS     0x8000
622
623#define XL_NETDIAG_TEST_LOWVOLT         0x0001
624#define XL_NETDIAG_ASIC_REVMASK         (0x0002|0x0004|0x0008|0x0010|0x0020)
625#define XL_NETDIAG_UPPER_BYTES_ENABLE   0x0040
626#define XL_NETDIAG_STATS_ENABLED        0x0080
627#define XL_NETDIAG_TX_FATALERR          0x0100
628#define XL_NETDIAG_TRANSMITTING         0x0200
629#define XL_NETDIAG_RX_ENABLED           0x0400
630#define XL_NETDIAG_TX_ENABLED           0x0800
631#define XL_NETDIAG_FIFO_LOOPBACK        0x1000
632#define XL_NETDIAG_MAC_LOOPBACK         0x2000
633#define XL_NETDIAG_ENDEC_LOOPBACK       0x4000
634#define XL_NETDIAG_EXTERNAL_LOOP        0x8000
635
636/*
637 * Window 5
638 */
639#define XL_W5_STAT_ENB          0x0C
640#define XL_W5_INTR_ENB          0x0A
641#define XL_W5_RECLAIM_THRESH    0x09    /* 3c905B only */
642#define XL_W5_RX_FILTER         0x08
643#define XL_W5_RX_EARLYTHRESH    0x06
644#define XL_W5_TX_AVAILTHRESH    0x02
645#define XL_W5_TX_STARTTHRESH    0x00
646
647/*
648 * RX filter bits
649 */
650#define XL_RXFILTER_INDIVIDUAL  0x01
651#define XL_RXFILTER_ALLMULTI    0x02
652#define XL_RXFILTER_BROADCAST   0x04
653#define XL_RXFILTER_ALLFRAMES   0x08
654#define XL_RXFILTER_MULTIHASH   0x10 /* 3c905B only */
655
656/*
657 * Window 6 (stats)
658 */
659#define XL_W6_TX_BYTES_OK       0x0C
660#define XL_W6_RX_BYTES_OK       0x0A
661#define XL_W6_UPPER_FRAMES_OK   0x09
662#define XL_W6_DEFERRED          0x08
663#define XL_W6_RX_OK             0x07
664#define XL_W6_TX_OK             0x06
665#define XL_W6_RX_OVERRUN        0x05
666#define XL_W6_COL_LATE          0x04
667#define XL_W6_COL_SINGLE        0x03
668#define XL_W6_COL_MULTIPLE      0x02
669#define XL_W6_SQE_ERRORS        0x01
670#define XL_W6_CARRIER_LOST      0x00
671
672/*
673 * Window 7 (bus master control)
674 */
675#define XL_W7_BM_ADDR           0x00
676#define XL_W7_BM_LEN            0x06
677#define XL_W7_BM_STATUS         0x0B
678#define XL_W7_BM_TIMEr          0x0A
679
680/*
681 * bus master control registers
682 */
683#define XL_BM_PKTSTAT           0x20
684#define XL_BM_DOWNLISTPTR       0x24
685#define XL_BM_FRAGADDR          0x28
686#define XL_BM_FRAGLEN           0x2C
687#define XL_BM_TXFREETHRESH      0x2F
688#define XL_BM_UPPKTSTAT         0x30
689#define XL_BM_UPLISTPTR         0x38
690
691
692
693
694
695
696
697
698struct xl_mii_frame {
699        u_int8_t                mii_stdelim;
700        u_int8_t                mii_opcode;
701        u_int8_t                mii_phyaddr;
702        u_int8_t                mii_regaddr;
703        u_int8_t                mii_turnaround;
704        u_int16_t               mii_data;
705};
706
707/*
708 * MII constants
709 */
710#define XL_MII_STARTDELIM       0x01
711#define XL_MII_READOP           0x02
712#define XL_MII_WRITEOP          0x01
713#define XL_MII_TURNAROUND       0x02
714
715
716
717/*
718 * The 3C905B adapters implement a few features that we want to
719 * take advantage of, namely the multicast hash filter. With older
720 * chips, you only have the option of turning on reception of all
721 * multicast frames, which is kind of lame.
722 *
723 * We also use this to decide on a transmit strategy. For the 3c90xB
724 * cards, we can use polled descriptor mode, which reduces CPU overhead.
725 */
726#define XL_TYPE_905B    1
727#define XL_TYPE_90X     2
728
729#define XL_FLAG_FUNCREG                 0x0001
730#define XL_FLAG_PHYOK                   0x0002
731#define XL_FLAG_EEPROM_OFFSET_30        0x0004
732#define XL_FLAG_WEIRDRESET              0x0008
733#define XL_FLAG_8BITROM                 0x0010
734#define XL_FLAG_INVERT_LED_PWR          0x0020
735#define XL_FLAG_INVERT_MII_PWR          0x0040
736#define XL_FLAG_NO_XCVR_PWR             0x0080
737#define XL_FLAG_USE_MMIO                0x0100
738
739#define XL_NO_XCVR_PWR_MAGICBITS        0x0900
740
741
742#define XL_MIN_FRAMELEN         60
743
744#define XL_LAST_FRAG            0x80000000
745
746
747
748
749
750
751
752struct xl_stats
753{
754      /* accumulated stats */
755      u_int16_t         xl_carrier_lost;
756      u_int16_t         xl_sqe_errs;
757      u_int16_t         xl_tx_multi_collision;
758      u_int16_t         xl_tx_single_collision;
759      u_int16_t         xl_tx_late_collision;
760      u_int16_t         xl_rx_overrun;
761      u_int16_t         xl_tx_deferred;
762
763      u_int32_t         xl_rx_bytes_ok;
764      u_int32_t         xl_tx_bytes_ok;
765
766      u_int32_t         xl_tx_frames_ok;
767      u_int32_t         xl_rx_frames_ok;
768
769      u_int16_t         xl_badssd;
770
771      /* non-accumulated stats */
772      u_int16_t         intstatus;
773      u_int16_t         rxstatus;
774      u_int8_t          txstatus;
775      u_int16_t         mediastatus;
776
777      u_int32_t         txcomplete_ints;
778
779      u_int16_t         miianr, miipar, miistatus, miicmd;
780
781      u_int32_t         device_interrupts;
782      u_int32_t         internalconfig;
783      u_int16_t         mac_control;
784
785      u_int16_t         smbstatus;
786      u_int32_t         dmactl;
787      u_int16_t         txfree;
788};
789
790
791
792struct xl_type
793{
794      u_int16_t         xl_vid;
795      u_int16_t         xl_did;
796      char              *xl_name;
797};
798
799
800
801/*
802 * Various supported device vendors/types and their names.
803 */
804static struct xl_type xl_devs[] = {
805        { TC_VENDORID, TC_DEVICEID_BOOMERANG_10BT,
806                "3Com 3c900-TPO Etherlink XL" },
807        { TC_VENDORID, TC_DEVICEID_BOOMERANG_10BT_COMBO,
808                "3Com 3c900-COMBO Etherlink XL" },
809        { TC_VENDORID, TC_DEVICEID_BOOMERANG_10_100BT,
810                "3Com 3c905-TX Fast Etherlink XL" },
811        { TC_VENDORID, TC_DEVICEID_BOOMERANG_100BT4,
812                "3Com 3c905-T4 Fast Etherlink XL" },
813        { TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT,
814                "3Com 3c900B-TPO Etherlink XL" },
815        { TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT_COMBO,
816                "3Com 3c900B-COMBO Etherlink XL" },
817        { TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT_TPC,
818                "3Com 3c900B-TPC Etherlink XL" },
819        { TC_VENDORID, TC_DEVICEID_CYCLONE_10FL,
820                "3Com 3c900B-FL Etherlink XL" },
821        { TC_VENDORID, TC_DEVICEID_HURRICANE_10_100BT,
822                "3Com 3c905B-TX Fast Etherlink XL" },
823        { TC_VENDORID, TC_DEVICEID_CYCLONE_10_100BT4,
824                "3Com 3c905B-T4 Fast Etherlink XL" },
825        { TC_VENDORID, TC_DEVICEID_CYCLONE_10_100FX,
826                "3Com 3c905B-FX/SC Fast Etherlink XL" },
827        { TC_VENDORID, TC_DEVICEID_CYCLONE_10_100_COMBO,
828                "3Com 3c905B-COMBO Fast Etherlink XL" },
829        { TC_VENDORID, TC_DEVICEID_TORNADO_10_100BT,
830                "3Com 3c905C-TX Fast Etherlink XL" },
831        { TC_VENDORID, TC_DEVICEID_TORNADO_10_100BT_920B,
832                "3Com 3c920B-EMB Integrated Fast Etherlink XL" },
833        { TC_VENDORID, TC_DEVICEID_HURRICANE_10_100BT_SERV,
834                "3Com 3c980 Fast Etherlink XL" },
835        { TC_VENDORID, TC_DEVICEID_TORNADO_10_100BT_SERV,
836                "3Com 3c980C Fast Etherlink XL" },
837        { TC_VENDORID, TC_DEVICEID_HURRICANE_SOHO100TX,
838                "3Com 3cSOHO100-TX OfficeConnect" },
839        { TC_VENDORID, TC_DEVICEID_TORNADO_HOMECONNECT,
840                "3Com 3c450-TX HomeConnect" },
841        { TC_VENDORID, TC_DEVICEID_HURRICANE_555,
842                "3Com 3c555 Fast Etherlink XL" },
843        { TC_VENDORID, TC_DEVICEID_HURRICANE_556,
844                "3Com 3c556 Fast Etherlink XL" },
845        { TC_VENDORID, TC_DEVICEID_HURRICANE_556B,
846                "3Com 3c556B Fast Etherlink XL" },
847        { TC_VENDORID, TC_DEVICEID_HURRICANE_575A,
848                "3Com 3c575TX Fast Etherlink XL" },
849        { TC_VENDORID, TC_DEVICEID_HURRICANE_575B,
850                "3Com 3c575B Fast Etherlink XL" },
851        { TC_VENDORID, TC_DEVICEID_HURRICANE_575C,
852                "3Com 3c575C Fast Etherlink XL" },
853        { TC_VENDORID, TC_DEVICEID_HURRICANE_656,
854                "3Com 3c656 Fast Etherlink XL" },
855        { TC_VENDORID, TC_DEVICEID_HURRICANE_656B,
856                "3Com 3c656B Fast Etherlink XL" },
857        { TC_VENDORID, TC_DEVICEID_TORNADO_656C,
858                "3Com 3c656C Fast Etherlink XL" },
859        { 0, 0, NULL }
860};
861
862
863#define XL_TIMEOUT              1000
864
865
866
867
868
869
870/* rx message descriptor entry, ensure the struct is aligned to 8 bytes */
871struct RXMD
872{
873      /* used by hardware */
874      volatile uint32_t   next;
875      volatile uint32_t   status;
876      volatile uint32_t   addr;
877      volatile uint32_t   length;
878      /* used by software */
879      struct mbuf       *mbuf;        /* scratch variable used in the tx ring */
880      struct RXMD       *next_md;
881} __attribute__ ((aligned (8), packed));
882
883
884
885
886
887#define NUM_FRAGS       6
888
889/*
890 * tx message descriptor entry, ensure the struct is aligned to 8 bytes
891 */
892
893struct tfrag
894{
895      volatile uint32_t   addr;
896      volatile uint32_t   length;
897} __attribute__ ((packed));
898
899struct TXMD
900{
901      /* used by hardware */
902      volatile uint32_t   next;
903      volatile uint32_t   status;
904      struct tfrag        txfrags[NUM_FRAGS];
905      /* used by software */
906      struct mbuf       *mbuf;        /* scratch variable used in the tx ring */
907      struct TXMD       *next_md, *chainptr;
908} __attribute__ ((aligned (8), packed));
909
910
911
912
913
914#define NUM_CHAIN_LENGTHS       50
915
916
917
918/*
919 * Per-device data
920 */
921struct elnk_softc
922{
923      struct arpcom             arpcom;
924
925      rtems_irq_connect_data    irqInfo;
926      rtems_event_set           ioevent;
927      unsigned int              ioaddr;
928
929      unsigned char             *bufferBase, *ringBase;
930
931      struct RXMD      *rx_ring, *curr_rx_md;
932      struct TXMD      *tx_ring, *last_tx_md, *last_txchain_head;
933
934      rtems_id                  stat_timer_id;
935      uint32_t                  stats_update_ticks;
936
937      struct xl_stats           xl_stats;
938
939      u_int8_t                  xl_unit;        /* interface number */
940      u_int8_t                  xl_type;
941      int                       xl_flags;
942      u_int16_t                 xl_media;
943      u_int16_t                 xl_caps;
944      u_int32_t                 xl_xcvr;
945      u_int8_t                  xl_stats_no_timeout;
946      u_int16_t                 xl_tx_thresh;
947
948      int                       tx_idle;
949
950      short                     chain_lengths[NUM_CHAIN_LENGTHS];
951      int                       chlenIndex;
952
953      unsigned short            vendorID, deviceID;
954      int                       acceptBroadcast;
955      int                       numTxbuffers, numRxbuffers;
956};
957
958static struct elnk_softc elnk_softc[NUM_UNITS];
959static rtems_id rxDaemonTid;
960static rtems_id txDaemonTid;
961static rtems_id chainRecoveryQueue;
962
963
964
965
966
967
968
969#if defined(__i386__)
970
971#define CSR_WRITE_4(sc, reg, val)       i386_outport_long( sc->ioaddr + reg, val )
972#define CSR_WRITE_2(sc, reg, val)       i386_outport_word( sc->ioaddr + reg, val )
973#define CSR_WRITE_1(sc, reg, val)       i386_outport_byte( sc->ioaddr + reg, val )
974
975
976inline unsigned int CSR_READ_4( struct elnk_softc *sc, int reg)
977{
978   unsigned int myval;
979   i386_inport_long( sc->ioaddr + reg, myval );
980   return myval;
981}
982
983inline unsigned short  CSR_READ_2( struct elnk_softc *sc, int reg)
984{
985   unsigned short myval;
986   i386_inport_word( sc->ioaddr + reg, myval );
987   return myval;
988}
989
990inline unsigned char CSR_READ_1( struct elnk_softc *sc, int reg)
991{
992   unsigned char myval;
993   i386_inport_byte( sc->ioaddr + reg, myval );
994   return myval;
995}
996
997#endif
998
999#if defined(__PPC__)
1000
1001#define CSR_WRITE_4(sc, reg, val)       outl( val, sc->ioaddr + reg)
1002#define CSR_WRITE_2(sc, reg, val)       outw( val, sc->ioaddr + reg)
1003#define CSR_WRITE_1(sc, reg, val)       outb( val, sc->ioaddr + reg)
1004
1005#define CSR_READ_4(sc, reg)             inl(sc->ioaddr + reg)
1006#define CSR_READ_2(sc, reg)             inw(sc->ioaddr + reg)
1007#define CSR_READ_1(sc, reg)             inb(sc->ioaddr + reg)
1008
1009#endif
1010
1011
1012#define XL_SEL_WIN(x)                   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_WINSEL | x)
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024/*
1025 * Murphy's law says that it's possible the chip can wedge and
1026 * the 'command in progress' bit may never clear. Hence, we wait
1027 * only a finite amount of time to avoid getting caught in an
1028 * infinite loop. Normally this delay routine would be a macro,
1029 * but it isn't called during normal operation so we can afford
1030 * to make it a function.
1031 */
1032static void
1033xl_wait(struct elnk_softc       *sc)
1034{
1035   register int         i;
1036
1037   for(i = 0; i < XL_TIMEOUT; i++)
1038   {
1039      if (!(CSR_READ_2(sc, XL_STATUS) & XL_STAT_CMDBUSY))
1040         break;
1041   }
1042
1043   if (i == XL_TIMEOUT)
1044      printk("etherlink : unit elnk%d command never completed\n", sc->xl_unit );
1045   return;
1046}
1047
1048
1049
1050
1051
1052
1053/*
1054 * MII access routines are provided for adapters with external
1055 * PHYs (3c905-TX, 3c905-T4, 3c905B-T4) and those with built-in
1056 * autoneg logic that's faked up to look like a PHY (3c905B-TX).
1057 * Note: if you don't perform the MDIO operations just right,
1058 * it's possible to end up with code that works correctly with
1059 * some chips/CPUs/processor speeds/bus speeds/etc but not
1060 * with others.
1061 */
1062#define MII_SET(x)                                      \
1063        CSR_WRITE_2(sc, XL_W4_PHY_MGMT,                 \
1064                CSR_READ_2(sc, XL_W4_PHY_MGMT) | (x))
1065
1066#define MII_CLR(x)                                      \
1067        CSR_WRITE_2(sc, XL_W4_PHY_MGMT,                 \
1068                CSR_READ_2(sc, XL_W4_PHY_MGMT) & ~(x))
1069
1070/*
1071 * Sync the PHYs by setting data bit and strobing the clock 32 times.
1072 */
1073static void
1074xl_mii_sync(
1075   struct elnk_softc            *sc)
1076{
1077   register int         i;
1078
1079   XL_SEL_WIN(4);
1080   MII_SET(XL_MII_DIR|XL_MII_DATA);
1081
1082   for (i = 0; i < 32; i++) {
1083      MII_SET(XL_MII_CLK);
1084      MII_SET(XL_MII_DATA);
1085      MII_CLR(XL_MII_CLK);
1086      MII_SET(XL_MII_DATA);
1087   }
1088
1089   return;
1090}
1091
1092/*
1093 * Clock a series of bits through the MII.
1094 */
1095static void
1096xl_mii_send(
1097   struct elnk_softc            *sc,
1098   u_int32_t            bits,
1099   int                  cnt )
1100{
1101   int                  i;
1102
1103   XL_SEL_WIN(4);
1104   MII_CLR(XL_MII_CLK);
1105
1106   for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1107      if (bits & i) {
1108         MII_SET(XL_MII_DATA);
1109      } else {
1110         MII_CLR(XL_MII_DATA);
1111      }
1112      MII_CLR(XL_MII_CLK);
1113      MII_SET(XL_MII_CLK);
1114   }
1115}
1116
1117/*
1118 * Read an PHY register through the MII.
1119 */
1120static int
1121xl_mii_readreg(
1122   struct elnk_softc    *sc,
1123   struct xl_mii_frame  *frame )
1124{
1125   int                  i, ack;
1126
1127   /*
1128    * Set up frame for RX.
1129    */
1130   frame->mii_stdelim = XL_MII_STARTDELIM;
1131   frame->mii_opcode = XL_MII_READOP;
1132   frame->mii_turnaround = 0;
1133   frame->mii_data = 0;
1134
1135   /*
1136    * Select register window 4.
1137    */
1138
1139   XL_SEL_WIN(4);
1140
1141   CSR_WRITE_2(sc, XL_W4_PHY_MGMT, 0);
1142   /*
1143    * Turn on data xmit.
1144    */
1145   MII_SET(XL_MII_DIR);
1146
1147   xl_mii_sync(sc);
1148
1149   /*
1150    * Send command/address info.
1151    */
1152   xl_mii_send(sc, frame->mii_stdelim, 2);
1153   xl_mii_send(sc, frame->mii_opcode, 2);
1154   xl_mii_send(sc, frame->mii_phyaddr, 5);
1155   xl_mii_send(sc, frame->mii_regaddr, 5);
1156
1157   /* Idle bit */
1158   MII_CLR((XL_MII_CLK|XL_MII_DATA));
1159   MII_SET(XL_MII_CLK);
1160
1161   /* Turn off xmit. */
1162   MII_CLR(XL_MII_DIR);
1163
1164   /* Check for ack */
1165   MII_CLR(XL_MII_CLK);
1166   ack = CSR_READ_2(sc, XL_W4_PHY_MGMT) & XL_MII_DATA;
1167   MII_SET(XL_MII_CLK);
1168
1169   /*
1170    * Now try reading data bits. If the ack failed, we still
1171    * need to clock through 16 cycles to keep the PHY(s) in sync.
1172    */
1173   if (ack) {
1174      for(i = 0; i < 16; i++) {
1175         MII_CLR(XL_MII_CLK);
1176         MII_SET(XL_MII_CLK);
1177      }
1178      goto fail;
1179   }
1180
1181   for (i = 0x8000; i; i >>= 1) {
1182      MII_CLR(XL_MII_CLK);
1183      if (!ack) {
1184         if (CSR_READ_2(sc, XL_W4_PHY_MGMT) & XL_MII_DATA)
1185            frame->mii_data |= i;
1186      }
1187      MII_SET(XL_MII_CLK);
1188   }
1189
1190  fail:
1191
1192   MII_CLR(XL_MII_CLK);
1193   MII_SET(XL_MII_CLK);
1194
1195   if (ack)
1196      return(1);
1197   return(0);
1198}
1199
1200/*
1201 * Write to a PHY register through the MII.
1202 */
1203static int
1204xl_mii_writereg(
1205   struct elnk_softc    *sc,
1206   struct xl_mii_frame  *frame )
1207{
1208   /*
1209    * Set up frame for TX.
1210    */
1211
1212   frame->mii_stdelim = XL_MII_STARTDELIM;
1213   frame->mii_opcode = XL_MII_WRITEOP;
1214   frame->mii_turnaround = XL_MII_TURNAROUND;
1215
1216   /*
1217    * Select the window 4.
1218    */
1219   XL_SEL_WIN(4);
1220
1221   /*
1222    * Turn on data output.
1223    */
1224   MII_SET(XL_MII_DIR);
1225
1226   xl_mii_sync(sc);
1227
1228   xl_mii_send(sc, frame->mii_stdelim, 2);
1229   xl_mii_send(sc, frame->mii_opcode, 2);
1230   xl_mii_send(sc, frame->mii_phyaddr, 5);
1231   xl_mii_send(sc, frame->mii_regaddr, 5);
1232   xl_mii_send(sc, frame->mii_turnaround, 2);
1233   xl_mii_send(sc, frame->mii_data, 16);
1234
1235   /* Idle bit. */
1236   MII_SET(XL_MII_CLK);
1237   MII_CLR(XL_MII_CLK);
1238
1239   /*
1240    * Turn off xmit.
1241    */
1242   MII_CLR(XL_MII_DIR);
1243
1244   return(0);
1245}
1246
1247static int
1248xl_miibus_readreg(
1249   struct elnk_softc    *sc,
1250   int                  phy,
1251   int                  reg )
1252{
1253   struct xl_mii_frame  frame;
1254
1255   /*
1256    * Pretend that PHYs are only available at MII address 24.
1257    * This is to guard against problems with certain 3Com ASIC
1258    * revisions that incorrectly map the internal transceiver
1259    * control registers at all MII addresses. This can cause
1260    * the miibus code to attach the same PHY several times over.
1261    */
1262   if ((!(sc->xl_flags & XL_FLAG_PHYOK)) && phy != 24)
1263   {
1264      printk("etherlink : unit elnk%d xl_miibus_readreg returned\n", sc->xl_unit);
1265      return(0);
1266   }
1267
1268   memset((char *)&frame, 0, sizeof(frame));
1269
1270   frame.mii_phyaddr = phy;
1271   frame.mii_regaddr = reg;
1272   xl_mii_readreg(sc, &frame);
1273
1274   return(frame.mii_data);
1275}
1276
1277static int
1278xl_miibus_writereg(
1279   struct elnk_softc            *sc,
1280   int                  phy,
1281   int                  reg,
1282   int                  data )
1283{
1284   struct xl_mii_frame  frame;
1285
1286   if ((!(sc->xl_flags & XL_FLAG_PHYOK)) && phy != 24)
1287   {
1288      printk("etherlink : unit elnk%d xl_miibus_writereg returned\n", sc->xl_unit);
1289      return(0);
1290   }
1291
1292   memset((char *)&frame, 0, sizeof(frame));
1293
1294   frame.mii_phyaddr = phy;
1295   frame.mii_regaddr = reg;
1296   frame.mii_data = data;
1297
1298   xl_mii_writereg(sc, &frame);
1299
1300   return(0);
1301}
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311/*
1312 * The EEPROM is slow: give it time to come ready after issuing
1313 * it a command.
1314 */
1315static int
1316xl_eeprom_wait(struct elnk_softc *sc)
1317{
1318   int                  i;
1319
1320   for (i = 0; i < 100; i++) {
1321      if (CSR_READ_2(sc, XL_W0_EE_CMD) & XL_EE_BUSY)
1322         DELAY(162);
1323      else
1324         break;
1325   }
1326
1327   if (i == 100) {
1328      printk("etherlink : unit elnk%d eeprom failed to come ready\n", sc->xl_unit);
1329      return(1);
1330   }
1331
1332   return(0);
1333}
1334
1335/*
1336 * Read a sequence of words from the EEPROM. Note that ethernet address
1337 * data is stored in the EEPROM in network byte order.
1338 */
1339static int
1340xl_read_eeprom(
1341   struct elnk_softc            *sc,
1342   caddr_t                      dest,
1343   int                  off,
1344   int                  cnt,
1345   int                  swap)
1346{
1347   int                  err = 0, i;
1348   u_int16_t            word = 0, *ptr;
1349#define EEPROM_5BIT_OFFSET(A) ((((A) << 2) & 0x7F00) | ((A) & 0x003F))
1350#define EEPROM_8BIT_OFFSET(A) ((A) & 0x003F)
1351   /* WARNING! DANGER!
1352    * It's easy to accidentally overwrite the rom content!
1353    * Note: the 3c575 uses 8bit EEPROM offsets.
1354    */
1355   XL_SEL_WIN(0);
1356
1357   if (xl_eeprom_wait(sc))
1358      return(1);
1359
1360   if (sc->xl_flags & XL_FLAG_EEPROM_OFFSET_30)
1361      off += 0x30;
1362
1363   for (i = 0; i < cnt; i++) {
1364      if (sc->xl_flags & XL_FLAG_8BITROM)
1365         CSR_WRITE_2(sc, XL_W0_EE_CMD,
1366                     XL_EE_8BIT_READ | EEPROM_8BIT_OFFSET(off + i));
1367      else
1368         CSR_WRITE_2(sc, XL_W0_EE_CMD,
1369                     XL_EE_READ | EEPROM_5BIT_OFFSET(off + i));
1370      err = xl_eeprom_wait(sc);
1371      if (err)
1372         break;
1373      word = CSR_READ_2(sc, XL_W0_EE_DATA);
1374      ptr = (u_int16_t*)(dest + (i * 2));
1375      if (swap)
1376         *ptr = ntohs(word);
1377      else
1378         *ptr = word;
1379   }
1380
1381   return(err ? 1 : 0);
1382}
1383
1384
1385
1386
1387static void
1388xl_stats_update(
1389   rtems_id timerid,
1390   void *xsc)
1391{
1392   struct elnk_softc    *sc = (struct elnk_softc *)xsc;
1393   struct ifnet         *ifp = &sc->arpcom.ac_if;
1394   u_int32_t            t1;
1395
1396   sc->xl_stats.intstatus = CSR_READ_2(sc, XL_STATUS);
1397
1398   sc->xl_stats.miianr    = xl_miibus_readreg(sc, 0x18, MII_ANAR );
1399   sc->xl_stats.miipar    = xl_miibus_readreg(sc, 0x18, MII_ANLPAR );
1400   sc->xl_stats.miistatus = xl_miibus_readreg(sc, 0x18, MII_BMSR );
1401   sc->xl_stats.miicmd    = xl_miibus_readreg(sc, 0x18, MII_BMCR );
1402
1403   XL_SEL_WIN(1);
1404   sc->xl_stats.rxstatus  = CSR_READ_2(sc, XL_W1_RX_STATUS );
1405   sc->xl_stats.txstatus  = CSR_READ_1(sc, XL_W1_TX_STATUS );
1406   sc->xl_stats.smbstatus = CSR_READ_2(sc, 2 );
1407
1408   XL_SEL_WIN(3);
1409   sc->xl_stats.internalconfig = CSR_READ_4(sc, XL_W3_INTERNAL_CFG);
1410   sc->xl_stats.mac_control    = CSR_READ_2(sc, XL_W3_MAC_CTRL);
1411   sc->xl_stats.txfree         = CSR_READ_2(sc, XL_W3_FREE_TX );
1412
1413
1414   /* Read all the stats registers. */
1415   XL_SEL_WIN(6);
1416
1417   sc->xl_stats.xl_carrier_lost              += CSR_READ_1(sc, XL_W6_CARRIER_LOST);
1418   sc->xl_stats.xl_sqe_errs                  += CSR_READ_1(sc, XL_W6_SQE_ERRORS);
1419   sc->xl_stats.xl_tx_multi_collision        += CSR_READ_1(sc, XL_W6_COL_MULTIPLE);
1420   sc->xl_stats.xl_tx_single_collision       += CSR_READ_1(sc, XL_W6_COL_SINGLE);
1421   sc->xl_stats.xl_tx_late_collision         += CSR_READ_1(sc, XL_W6_COL_LATE);
1422   sc->xl_stats.xl_rx_overrun                += CSR_READ_1(sc, XL_W6_RX_OVERRUN);
1423   sc->xl_stats.xl_tx_deferred               += CSR_READ_1(sc, XL_W6_DEFERRED);
1424
1425   sc->xl_stats.xl_tx_frames_ok              += CSR_READ_1(sc, XL_W6_TX_OK);
1426   sc->xl_stats.xl_rx_frames_ok              += CSR_READ_1(sc, XL_W6_RX_OK);
1427
1428   sc->xl_stats.xl_rx_bytes_ok               += CSR_READ_2(sc, XL_W6_TX_BYTES_OK );
1429   sc->xl_stats.xl_tx_bytes_ok               += CSR_READ_2(sc, XL_W6_RX_BYTES_OK );
1430
1431   t1 = CSR_READ_1(sc, XL_W6_UPPER_FRAMES_OK);
1432   sc->xl_stats.xl_rx_frames_ok +=  ((t1 & 0x3) << 8);
1433   sc->xl_stats.xl_tx_frames_ok +=  (((t1 >> 4) & 0x3) << 8);
1434
1435
1436   ifp->if_ierrors += sc->xl_stats.xl_rx_overrun;
1437
1438   ifp->if_collisions += sc->xl_stats.xl_tx_multi_collision +
1439      sc->xl_stats.xl_tx_single_collision +
1440      sc->xl_stats.xl_tx_late_collision;
1441
1442   /*
1443    * Boomerang and cyclone chips have an extra stats counter
1444    * in window 4 (BadSSD). We have to read this too in order
1445    * to clear out all the stats registers and avoid a statsoflow
1446    * interrupt.
1447    */
1448   XL_SEL_WIN(4);
1449
1450   t1 = CSR_READ_1(sc, XL_W4_UPPERBYTESOK);
1451   sc->xl_stats.xl_rx_bytes_ok += ((t1 & 0xf) << 16);
1452   sc->xl_stats.xl_tx_bytes_ok += (((t1 >> 4) & 0xf) << 16);
1453
1454   sc->xl_stats.xl_badssd               += CSR_READ_1(sc, XL_W4_BADSSD);
1455
1456   sc->xl_stats.mediastatus             = CSR_READ_2(sc, XL_W4_MEDIA_STATUS );
1457   sc->xl_stats.dmactl                  = CSR_READ_4(sc, XL_DMACTL );
1458
1459
1460   XL_SEL_WIN(7);
1461
1462   if (!sc->xl_stats_no_timeout)
1463      rtems_timer_fire_after( sc->stat_timer_id, sc->stats_update_ticks, xl_stats_update, (void *)sc );
1464   return;
1465}
1466
1467
1468
1469
1470
1471
1472
1473static void
1474xl_reset(struct elnk_softc *sc)
1475{
1476   register int         i;
1477
1478   XL_SEL_WIN(0);
1479   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RESET |
1480               ((sc->xl_flags & XL_FLAG_WEIRDRESET) ?
1481                XL_RESETOPT_DISADVFD:0));
1482
1483   for (i = 0; i < XL_TIMEOUT; i++) {
1484      DELAY(10);
1485      if (!(CSR_READ_2(sc, XL_STATUS) & XL_STAT_CMDBUSY))
1486         break;
1487   }
1488
1489   if (i == XL_TIMEOUT)
1490      printk("etherlink : unit elnk%d reset didn't complete\n", sc->xl_unit);
1491
1492   /* Reset TX and RX. */
1493   /* Note: the RX reset takes an absurd amount of time
1494    * on newer versions of the Tornado chips such as those
1495    * on the 3c905CX and newer 3c908C cards. We wait an
1496    * extra amount of time so that xl_wait() doesn't complain
1497    * and annoy the users.
1498    */
1499   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_RESET);
1500   DELAY(100000);
1501   xl_wait(sc);
1502   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_RESET);
1503   xl_wait(sc);
1504
1505   if (sc->xl_flags & XL_FLAG_INVERT_LED_PWR ||
1506       sc->xl_flags & XL_FLAG_INVERT_MII_PWR)
1507   {
1508      XL_SEL_WIN(2);
1509      CSR_WRITE_2(sc, XL_W2_RESET_OPTIONS, CSR_READ_2(sc,
1510                                                      XL_W2_RESET_OPTIONS)
1511                  | ((sc->xl_flags & XL_FLAG_INVERT_LED_PWR)?XL_RESETOPT_INVERT_LED:0)
1512                  | ((sc->xl_flags & XL_FLAG_INVERT_MII_PWR)?XL_RESETOPT_INVERT_MII:0)
1513         );
1514   }
1515
1516   /* Wait a little while for the chip to get its brains in order. */
1517   DELAY(100000);
1518   return;
1519}
1520
1521
1522
1523
1524static void
1525xl_stop(struct elnk_softc *sc)
1526{
1527   struct ifnet         *ifp;
1528
1529   ifp = &sc->arpcom.ac_if;
1530   ifp->if_timer = 0;
1531
1532   rtems_timer_cancel( sc->stat_timer_id );
1533
1534   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_DISABLE);
1535   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STATS_DISABLE);
1536   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ENB);
1537   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_DISCARD);
1538   xl_wait(sc);
1539   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_DISABLE);
1540   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
1541   DELAY(800);
1542
1543   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ACK|XL_STAT_INTLATCH);
1544   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STAT_ENB|0);
1545   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ENB|0);
1546
1547   return;
1548}
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564static void
1565xl_setcfg(struct elnk_softc *sc)
1566{
1567   u_int32_t            icfg;
1568
1569   XL_SEL_WIN(3);
1570   icfg = CSR_READ_4(sc, XL_W3_INTERNAL_CFG);
1571
1572   icfg &= ~XL_ICFG_CONNECTOR_MASK;
1573
1574   if (sc->xl_media & XL_MEDIAOPT_MII || sc->xl_media & XL_MEDIAOPT_BT4)
1575      icfg |= (XL_XCVR_MII << XL_ICFG_CONNECTOR_BITS);
1576
1577   if (sc->xl_media & XL_MEDIAOPT_BTX)
1578      icfg |= (XL_XCVR_AUTO << XL_ICFG_CONNECTOR_BITS);
1579
1580   CSR_WRITE_4(sc, XL_W3_INTERNAL_CFG, icfg);
1581   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
1582
1583   XL_SEL_WIN(7);
1584   return;
1585}
1586
1587
1588
1589static void
1590xl_setmode(
1591   struct elnk_softc    *sc,
1592   int                  media)
1593{
1594   u_int32_t            icfg;
1595   u_int16_t            mediastat;
1596
1597   printk("etherlink : unit elnk%d selecting ", sc->xl_unit);
1598
1599   XL_SEL_WIN(4);
1600   mediastat = CSR_READ_2(sc, XL_W4_MEDIA_STATUS);
1601   XL_SEL_WIN(3);
1602   icfg = CSR_READ_4(sc, XL_W3_INTERNAL_CFG);
1603
1604   if (sc->xl_media & XL_MEDIAOPT_BT) {
1605      if (IFM_SUBTYPE(media) == IFM_10_T) {
1606         printk("10baseT transceiver, ");
1607         sc->xl_xcvr = XL_XCVR_10BT;
1608         icfg &= ~XL_ICFG_CONNECTOR_MASK;
1609         icfg |= (XL_XCVR_10BT << XL_ICFG_CONNECTOR_BITS);
1610         mediastat |= XL_MEDIASTAT_LINKBEAT|
1611            XL_MEDIASTAT_JABGUARD;
1612         mediastat &= ~XL_MEDIASTAT_SQEENB;
1613      }
1614   }
1615
1616   if (sc->xl_media & XL_MEDIAOPT_BFX) {
1617      if (IFM_SUBTYPE(media) == IFM_100_FX) {
1618         printk("100baseFX port, ");
1619         sc->xl_xcvr = XL_XCVR_100BFX;
1620         icfg &= ~XL_ICFG_CONNECTOR_MASK;
1621         icfg |= (XL_XCVR_100BFX << XL_ICFG_CONNECTOR_BITS);
1622         mediastat |= XL_MEDIASTAT_LINKBEAT;
1623         mediastat &= ~XL_MEDIASTAT_SQEENB;
1624      }
1625   }
1626
1627   if (sc->xl_media & (XL_MEDIAOPT_AUI|XL_MEDIAOPT_10FL)) {
1628      if (IFM_SUBTYPE(media) == IFM_10_5) {
1629         printk("AUI port, ");
1630         sc->xl_xcvr = XL_XCVR_AUI;
1631         icfg &= ~XL_ICFG_CONNECTOR_MASK;
1632         icfg |= (XL_XCVR_AUI << XL_ICFG_CONNECTOR_BITS);
1633         mediastat &= ~(XL_MEDIASTAT_LINKBEAT|
1634                        XL_MEDIASTAT_JABGUARD);
1635         mediastat |= ~XL_MEDIASTAT_SQEENB;
1636      }
1637      if (IFM_SUBTYPE(media) == IFM_10_FL) {
1638         printk("10baseFL transceiver, ");
1639         sc->xl_xcvr = XL_XCVR_AUI;
1640         icfg &= ~XL_ICFG_CONNECTOR_MASK;
1641         icfg |= (XL_XCVR_AUI << XL_ICFG_CONNECTOR_BITS);
1642         mediastat &= ~(XL_MEDIASTAT_LINKBEAT|
1643                        XL_MEDIASTAT_JABGUARD);
1644         mediastat |= ~XL_MEDIASTAT_SQEENB;
1645      }
1646   }
1647
1648   if (sc->xl_media & XL_MEDIAOPT_BNC) {
1649      if (IFM_SUBTYPE(media) == IFM_10_2) {
1650         printk("BNC port, ");
1651         sc->xl_xcvr = XL_XCVR_COAX;
1652         icfg &= ~XL_ICFG_CONNECTOR_MASK;
1653         icfg |= (XL_XCVR_COAX << XL_ICFG_CONNECTOR_BITS);
1654         mediastat &= ~(XL_MEDIASTAT_LINKBEAT|
1655                        XL_MEDIASTAT_JABGUARD|
1656                        XL_MEDIASTAT_SQEENB);
1657      }
1658   }
1659
1660   if ((media & IFM_GMASK) == IFM_FDX ||
1661       IFM_SUBTYPE(media) == IFM_100_FX) {
1662      printk("full duplex\n");
1663      XL_SEL_WIN(3);
1664      CSR_WRITE_1(sc, XL_W3_MAC_CTRL, XL_MACCTRL_DUPLEX);
1665   } else {
1666      printk("half duplex\n");
1667      XL_SEL_WIN(3);
1668      CSR_WRITE_1(sc, XL_W3_MAC_CTRL,
1669                  (CSR_READ_1(sc, XL_W3_MAC_CTRL) & ~XL_MACCTRL_DUPLEX));
1670   }
1671
1672   if (IFM_SUBTYPE(media) == IFM_10_2)
1673      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_START);
1674   else
1675      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
1676
1677   CSR_WRITE_4(sc, XL_W3_INTERNAL_CFG, icfg);
1678   XL_SEL_WIN(4);
1679   CSR_WRITE_2(sc, XL_W4_MEDIA_STATUS, mediastat);
1680   DELAY(800);
1681   XL_SEL_WIN(7);
1682
1683   return;
1684}
1685
1686
1687
1688
1689
1690
1691
1692static void
1693xl_choose_xcvr(
1694   struct elnk_softc    *sc,
1695   int                  verbose)
1696{
1697   u_int16_t            devid;
1698
1699   /*
1700    * Read the device ID from the EEPROM.
1701    * This is what's loaded into the PCI device ID register, so it has
1702    * to be correct otherwise we wouldn't have gotten this far.
1703    */
1704   xl_read_eeprom(sc, (caddr_t)&devid, XL_EE_PRODID, 1, 0);
1705
1706   switch(devid) {
1707      case TC_DEVICEID_BOOMERANG_10BT:  /* 3c900-TPO */
1708      case TC_DEVICEID_KRAKATOA_10BT:           /* 3c900B-TPO */
1709         sc->xl_media = XL_MEDIAOPT_BT;
1710         sc->xl_xcvr = XL_XCVR_10BT;
1711         if (verbose)
1712            printk("etherlink : unit elnk%d guessing 10BaseT "
1713                   "transceiver\n", sc->xl_unit);
1714         break;
1715      case TC_DEVICEID_BOOMERANG_10BT_COMBO:    /* 3c900-COMBO */
1716      case TC_DEVICEID_KRAKATOA_10BT_COMBO:     /* 3c900B-COMBO */
1717         sc->xl_media = XL_MEDIAOPT_BT|XL_MEDIAOPT_BNC|XL_MEDIAOPT_AUI;
1718         sc->xl_xcvr = XL_XCVR_10BT;
1719         if (verbose)
1720            printk("etherlink : unit elnk%d guessing COMBO "
1721                   "(AUI/BNC/TP)\n", sc->xl_unit);
1722         break;
1723      case TC_DEVICEID_KRAKATOA_10BT_TPC:       /* 3c900B-TPC */
1724         sc->xl_media = XL_MEDIAOPT_BT|XL_MEDIAOPT_BNC;
1725         sc->xl_xcvr = XL_XCVR_10BT;
1726         if (verbose)
1727            printk("etherlink : unit elnk%d guessing TPC (BNC/TP)\n", sc->xl_unit);
1728         break;
1729      case TC_DEVICEID_CYCLONE_10FL:            /* 3c900B-FL */
1730         sc->xl_media = XL_MEDIAOPT_10FL;
1731         sc->xl_xcvr = XL_XCVR_AUI;
1732         if (verbose)
1733            printk("etherlink : unit elnk%d guessing 10baseFL\n", sc->xl_unit);
1734         break;
1735      case TC_DEVICEID_BOOMERANG_10_100BT:      /* 3c905-TX */
1736      case TC_DEVICEID_HURRICANE_555:           /* 3c555 */
1737      case TC_DEVICEID_HURRICANE_556:           /* 3c556 */
1738      case TC_DEVICEID_HURRICANE_556B:  /* 3c556B */
1739      case TC_DEVICEID_HURRICANE_575A:  /* 3c575TX */
1740      case TC_DEVICEID_HURRICANE_575B:  /* 3c575B */
1741      case TC_DEVICEID_HURRICANE_575C:  /* 3c575C */
1742      case TC_DEVICEID_HURRICANE_656:           /* 3c656 */
1743      case TC_DEVICEID_HURRICANE_656B:  /* 3c656B */
1744      case TC_DEVICEID_TORNADO_656C:            /* 3c656C */
1745      case TC_DEVICEID_TORNADO_10_100BT_920B:   /* 3c920B-EMB */
1746         sc->xl_media = XL_MEDIAOPT_MII;
1747         sc->xl_xcvr = XL_XCVR_MII;
1748         if (verbose)
1749            printk("etherlink : unit elnk%d guessing MII\n", sc->xl_unit);
1750         break;
1751      case TC_DEVICEID_BOOMERANG_100BT4:        /* 3c905-T4 */
1752      case TC_DEVICEID_CYCLONE_10_100BT4:       /* 3c905B-T4 */
1753         sc->xl_media = XL_MEDIAOPT_BT4;
1754         sc->xl_xcvr = XL_XCVR_MII;
1755         if (verbose)
1756            printk("etherlink : unit elnk%d guessing 100BaseT4/MII\n", sc->xl_unit);
1757         break;
1758      case TC_DEVICEID_HURRICANE_10_100BT:      /* 3c905B-TX */
1759      case TC_DEVICEID_HURRICANE_10_100BT_SERV:/*3c980-TX */
1760      case TC_DEVICEID_TORNADO_10_100BT_SERV:   /* 3c980C-TX */
1761      case TC_DEVICEID_HURRICANE_SOHO100TX:     /* 3cSOHO100-TX */
1762      case TC_DEVICEID_TORNADO_10_100BT:        /* 3c905C-TX */
1763      case TC_DEVICEID_TORNADO_HOMECONNECT:     /* 3c450-TX */
1764         sc->xl_media = XL_MEDIAOPT_BTX;
1765         sc->xl_xcvr = XL_XCVR_AUTO;
1766         if (verbose)
1767            printk("etherlink : unit elnk%d guessing 10/100 internal\n", sc->xl_unit);
1768         break;
1769      case TC_DEVICEID_CYCLONE_10_100_COMBO:    /* 3c905B-COMBO */
1770         sc->xl_media = XL_MEDIAOPT_BTX|XL_MEDIAOPT_BNC|XL_MEDIAOPT_AUI;
1771         sc->xl_xcvr = XL_XCVR_AUTO;
1772         if (verbose)
1773            printk("etherlink : unit elnk%d guessing 10/100 "
1774                   "plus BNC/AUI\n", sc->xl_unit);
1775         break;
1776      default:
1777         printk("etherlink : unit elnk%d unknown device ID: %x -- "
1778                "defaulting to 10baseT\n", sc->xl_unit, devid);
1779         sc->xl_media = XL_MEDIAOPT_BT;
1780         break;
1781   }
1782
1783   return;
1784}
1785
1786
1787
1788
1789
1790
1791
1792/*
1793 * This routine is a kludge to work around possible hardware faults
1794 * or manufacturing defects that can cause the media options register
1795 * (or reset options register, as it's called for the first generation
1796 * 3c90x adapters) to return an incorrect result. I have encountered
1797 * one Dell Latitude laptop docking station with an integrated 3c905-TX
1798 * which doesn't have any of the 'mediaopt' bits set. This screws up
1799 * the attach routine pretty badly because it doesn't know what media
1800 * to look for. If we find ourselves in this predicament, this routine
1801 * will try to guess the media options values and warn the user of a
1802 * possible manufacturing defect with his adapter/system/whatever.
1803 */
1804static void
1805xl_mediacheck(struct elnk_softc         *sc)
1806{
1807
1808   xl_choose_xcvr(sc, 1);
1809
1810   /*
1811    * If some of the media options bits are set, assume they are
1812    * correct. If not, try to figure it out down below.
1813    * XXX I should check for 10baseFL, but I don't have an adapter
1814    * to test with.
1815    */
1816   if (sc->xl_media & (XL_MEDIAOPT_MASK & ~XL_MEDIAOPT_VCO)) {
1817      /*
1818       * Check the XCVR value. If it's not in the normal range
1819       * of values, we need to fake it up here.
1820       */
1821      if (sc->xl_xcvr <= XL_XCVR_AUTO)
1822         return;
1823      else {
1824         printk("etherlink : unit elnk%d bogus xcvr value "
1825                "in EEPROM (%x)\n", sc->xl_unit, sc->xl_xcvr);
1826         printk("etherlink : unit elnk%d choosing new default based "
1827                "on card type\n", sc->xl_unit);
1828      }
1829   } else {
1830      if (sc->xl_type == XL_TYPE_905B &&
1831          sc->xl_media & XL_MEDIAOPT_10FL)
1832         return;
1833      printk("etherlink : unit elnk%d WARNING: no media options bits set in "
1834             "the media options register!!\n", sc->xl_unit);
1835      printk("etherlink : unit elnk%d this could be a manufacturing defect in "
1836             "your adapter or system\n", sc->xl_unit);
1837      printk("etherlink : unit elnk%d attempting to guess media type; you "
1838             "should probably consult your vendor\n", sc->xl_unit);
1839   }
1840
1841   return;
1842}
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868static void no_op(const rtems_irq_connect_data* irq)
1869{
1870   return;
1871}
1872
1873
1874
1875
1876static int elnkIsOn(const rtems_irq_connect_data* irq)
1877{
1878  return BSP_irq_enabled_at_i8259s (irq->name);
1879}
1880
1881
1882
1883
1884
1885
1886static void
1887elnk_start_txchain( struct elnk_softc *sc, struct TXMD *chainhead )
1888{
1889   xl_wait(sc);
1890   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_STALL);
1891
1892   /* save the address of the TX list */
1893   sc->last_txchain_head = chainhead;
1894   sc->tx_idle = 0;
1895
1896   xl_wait(sc);
1897
1898   CSR_WRITE_4(sc, XL_DOWNLIST_PTR, phys_to_bus( sc->last_txchain_head ));
1899   CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_UNSTALL);
1900}
1901
1902
1903
1904
1905
1906/*
1907 * ELNK interrupt handler
1908 */
1909static rtems_isr
1910elnk_interrupt_handler ( struct elnk_softc *sc )
1911{
1912   struct ifnet         *ifp = &sc->arpcom.ac_if;
1913   u_int16_t            status;
1914
1915   while( ((status = CSR_READ_2(sc, XL_STATUS)) & XL_INTRS) && status != 0xFFFF)
1916   {
1917      sc->xl_stats.device_interrupts++;
1918
1919      CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ACK | (status & XL_INTRS));
1920
1921#if 0
1922      printk("etherlink : unit elnk%d intstatus %04x\n", sc->xl_unit, status  );
1923#endif
1924
1925      if (status & XL_STAT_UP_COMPLETE)
1926      {
1927#if 0
1928         printk("etherlink : unit elnk%d rx\n", sc->xl_unit );
1929#endif
1930         /* received packets */
1931         rtems_event_send(rxDaemonTid, sc->ioevent);
1932      }
1933
1934      if( (status & XL_STAT_DOWN_COMPLETE) || (status & XL_STAT_TX_COMPLETE) )
1935      {
1936         /* all packets uploaded to the device */
1937         struct TXMD *chaintailmd = NULL;
1938
1939
1940         if( status & XL_STAT_TX_COMPLETE )
1941         {
1942            /* if we got a tx complete error, count it, then reset the
1943               transmitter.  Consider the entire chain lost.. */
1944
1945            ifp->if_oerrors++;
1946            sc->xl_stats.txcomplete_ints++;
1947
1948            printk("etherlink : unit elnk%d transmit error\n", sc->xl_unit );
1949
1950            /* reset, re-enable fifo */
1951
1952            xl_wait(sc);
1953            CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_DISABLE);
1954
1955            xl_wait(sc);
1956            CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_RESET | 1 );
1957
1958            xl_wait(sc);
1959            CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_ENABLE);
1960
1961            xl_wait(sc);
1962         }
1963
1964
1965         /* send the chain head to the tx task which will recover the
1966            whole chain */
1967         rtems_message_queue_send( chainRecoveryQueue, &sc->last_txchain_head, sizeof(struct TXMD *));
1968
1969
1970         /* set up the next chain */
1971         if( sc->last_txchain_head->chainptr )
1972         {
1973            /* check the head of the chain of packets we just finished,
1974             * if != 0, either this is a chain of 2 or more packets or
1975             * its a single packet chain and another chain is ready to
1976             * send.
1977             */
1978            if( (int)sc->last_txchain_head->chainptr == -1 )
1979            {
1980               /*
1981               ** single packet was sent so no indirection to the last
1982               ** entry in the chain.  since chainptr is != 0, then
1983               ** another chain is ready starting from the packet AFTER
1984               ** the chain we just finished. - in this case the last
1985               ** chain's head == its tail
1986               */
1987               chaintailmd = sc->last_txchain_head;
1988            }
1989            else
1990            {
1991               /*
1992               ** otherwise, this is a pointer to the last packet in the
1993               ** chain of 2 or more packets.  If the chain's last
1994               ** packet's chainptr is != 0, then another chain is ready
1995               ** to send.
1996               */
1997               chaintailmd = sc->last_txchain_head->chainptr;
1998               if( !chaintailmd->chainptr ) chaintailmd = NULL;
1999            }
2000         }
2001
2002         if( chaintailmd )
2003         {
2004            /* the next MD is the start of another chain */
2005            elnk_start_txchain(sc, chaintailmd->next_md );
2006         }
2007         else
2008         {
2009            /* otherwise nothing to send, so go idle */
2010            sc->tx_idle = -1;
2011
2012            /* wake up the tx daemon once so we're sure this last chain
2013               will be freed */
2014            rtems_event_send( txDaemonTid, sc->ioevent );
2015#if 0
2016            printk("unit elnk%d tx done\n", sc->xl_unit );
2017#endif
2018         }
2019      }
2020
2021
2022      if (status & XL_STAT_ADFAIL)
2023      {
2024         printk("etherlink : unit elnk%d Catastrophic bus failure\n", sc->xl_unit );
2025      }
2026      if (status & XL_STAT_STATSOFLOW)
2027      {
2028         sc->xl_stats_no_timeout = 1;
2029         xl_stats_update(sc->stat_timer_id,sc);
2030         sc->xl_stats_no_timeout = 0;
2031      }
2032   }
2033
2034
2035#if 0
2036   {
2037      uint16_t   intstatus, intenable, indenable;
2038
2039      intstatus = CSR_READ_2(sc, XL_STATUS );
2040
2041      XL_SEL_WIN(5);
2042      intenable = CSR_READ_2(sc, XL_W5_INTR_ENB );
2043      indenable = CSR_READ_2(sc, XL_W5_STAT_ENB );
2044      XL_SEL_WIN(7);
2045      printk("etherlink : unit elnk%d istat %04x, ien %04x, ind %04x\n", sc->xl_unit, intstatus, intenable, indenable  );
2046   }
2047#endif
2048}
2049
2050
2051
2052
2053
2054static rtems_isr
2055elnk_interrupt_handler_entry(void)
2056{
2057   int i;
2058
2059   /*
2060   ** Check all the initialized units for interrupt service
2061   */
2062
2063   for(i=0; i< NUM_UNITS; i++ )
2064   {
2065      if( elnk_softc[i].ioaddr )
2066         elnk_interrupt_handler( &elnk_softc[i] );
2067   }
2068}
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080/*
2081 * Initialize the ethernet hardware
2082 */
2083static void
2084elnk_initialize_hardware (struct elnk_softc *sc)
2085{
2086   unsigned char *cp;
2087   int i, j, rxsize, txsize, ringsize;
2088
2089   /*
2090    * Init RX ring
2091    */
2092   cp = (unsigned char *)malloc( (ringsize = ((rxsize = (sc->numRxbuffers * sizeof(struct RXMD))) +
2093                                              (txsize = (sc->numTxbuffers * sizeof(struct TXMD)))) ) +
2094                                 + CPU_CACHE_ALIGNMENT_FOR_BUFFER);
2095   sc->bufferBase = cp;
2096   cp += (CPU_CACHE_ALIGNMENT_FOR_BUFFER - (int)cp) & (CPU_CACHE_ALIGNMENT_FOR_BUFFER - 1);
2097#if defined(__i386__)
2098#ifdef PCI_BRIDGE_DOES_NOT_ENSURE_CACHE_COHERENCY_FOR_DMA
2099   if (_CPU_is_paging_enabled())
2100      _CPU_change_memory_mapping_attribute
2101         (NULL, cp, ringsize, PTE_CACHE_DISABLE | PTE_WRITABLE);
2102#endif
2103#endif
2104   sc->ringBase = cp;
2105
2106   /* build tx and rx rings */
2107
2108   sc->rx_ring = (struct RXMD *)sc->ringBase;
2109   sc->tx_ring = (struct TXMD *)&sc->ringBase[ rxsize ];
2110
2111   {
2112      struct mbuf    *m;
2113      struct RXMD    *nxtmd;
2114      /*
2115       * The rx ring is easy as its just an array of RXMD structs.  New
2116       * mbuf entries are allocated from the stack whenever the rx
2117       * daemon forwards an incoming packet into it.  Here, we
2118       * pre-allocate the rx mbufs for the rx ring entries.
2119       */
2120      for(i=0 ; i<sc->numRxbuffers; i++)
2121      {
2122         if( ((uint32_t)&sc->rx_ring[i] & 0x7) )
2123         {
2124            rtems_panic ("etherlink : unit elnk%d rx ring entry %d not aligned to 8 bytes\n", sc->xl_unit, i );
2125         }
2126
2127         /* allocate an mbuf for each receive descriptor */
2128         MGETHDR (m, M_WAIT, MT_DATA);
2129         MCLGET (m, M_WAIT);
2130         m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
2131
2132         if( i == sc->numRxbuffers-1 )
2133            nxtmd = &sc->rx_ring[0];
2134         else
2135            nxtmd = &sc->rx_ring[i+1];
2136
2137         sc->rx_ring[i].next_md = nxtmd;
2138         sc->rx_ring[i].mbuf = m;
2139
2140         st_le32( &sc->rx_ring[i].status, 0);
2141         st_le32( &sc->rx_ring[i].next, (uint32_t)phys_to_bus( nxtmd ));
2142         st_le32( &sc->rx_ring[i].addr, (uint32_t)phys_to_bus( mtod(m, void *) ));
2143         st_le32( &sc->rx_ring[i].length, XL_LAST_FRAG | XL_PACKET_SIZE );
2144      }
2145      sc->curr_rx_md = &sc->rx_ring[0];
2146   }
2147
2148
2149   {
2150      struct TXMD *thismd, *nxtmd;
2151      /*
2152       * The tx ring is more complex. Each MD has an array of fragment
2153       * descriptors that are loaded from each packet as they arrive
2154       * from the stack.  Each packet gets one ring entry, this allows
2155       * the lanboard to efficiently assemble the piecemeal packets into
2156       * a contiguous unit at transmit time, rather than spending
2157       * cputime concatenating them first.  Although the next_md fields
2158       * form a ring, the DPD next is filled only when packets are added
2159       * to the tx chain, thus last entry of a series of packets has the
2160       * requisite dpd->next value == 0 to terminate the dma.  mbuf
2161       * holds the packet info so it can be freed once the packet has
2162       * been sent.  chainptr is used to link the head & tail of a chain
2163       * of 2 or more packets.  A chain is formed when the tx daemon
2164       * gets 2 or more packets from the stack's queue in a service
2165       * period, so higher outgoing loads are handled as efficiently as
2166       * possible.
2167       */
2168
2169      for(i=0 ; i<sc->numTxbuffers; i++)
2170      {
2171         if( ((uint32_t)&sc->tx_ring[i] & 0x7) )
2172         {
2173            rtems_panic ("etherlink : unit elnk%d tx ring entry %d not aligned to 8 bytes\n", sc->xl_unit, i );
2174         }
2175
2176         if( i == sc->numTxbuffers-1 )
2177            nxtmd = &sc->tx_ring[0];
2178         else
2179            nxtmd = &sc->tx_ring[i+1];
2180
2181         thismd = &sc->tx_ring[i];
2182
2183         thismd->next_md = nxtmd;
2184         thismd->chainptr = NULL;
2185         thismd->mbuf = NULL;
2186
2187         st_le32( &thismd->status, XL_TXSTAT_DL_COMPLETE );
2188         st_le32( &thismd->next, 0);
2189
2190         for(j=0; j< NUM_FRAGS; j++)
2191         {
2192            st_le32( &thismd->txfrags[j].addr, 0 );
2193            st_le32( &thismd->txfrags[j].length, 0 );
2194         }
2195      }
2196      sc->last_tx_md = &sc->tx_ring[0];
2197   }
2198
2199
2200
2201
2202#ifdef ELNK_DEBUG
2203   printk("etherlink : %02x:%02x:%02x:%02x:%02x:%02x   name 'elnk%d', io %x, int %d\n",
2204          sc->arpcom.ac_enaddr[0], sc->arpcom.ac_enaddr[1],
2205          sc->arpcom.ac_enaddr[2], sc->arpcom.ac_enaddr[3],
2206          sc->arpcom.ac_enaddr[4], sc->arpcom.ac_enaddr[5],
2207          sc->xl_unit,
2208          (unsigned)sc->ioaddr, sc->irqInfo.name );
2209#endif
2210
2211
2212   sc->irqInfo.hdl  = (rtems_irq_hdl)elnk_interrupt_handler_entry;
2213   sc->irqInfo.on   = no_op;
2214   sc->irqInfo.off  = no_op;
2215   sc->irqInfo.isOn = elnkIsOn;
2216
2217   if( sc->irqInfo.name != 255 )
2218   {
2219      int st;
2220
2221#ifdef BSP_SHARED_HANDLER_SUPPORT
2222      st = BSP_install_rtems_shared_irq_handler( &sc->irqInfo );
2223#else
2224      st = BSP_install_rtems_irq_handler( &sc->irqInfo );
2225#endif
2226      if (!st)
2227         rtems_panic ("etherlink : unit elnk%d Interrupt name %d already in use\n", sc->xl_unit, sc->irqInfo.name );
2228   }
2229   else
2230   {
2231      printk("etherlink : unit elnk%d Interrupt not specified by device\n", sc->xl_unit );
2232   }
2233}
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245static void
2246elnk_rxDaemon (void *arg)
2247{
2248   struct elnk_softc     *sc;
2249   struct ether_header   *eh;
2250   struct mbuf           *m;
2251   struct RXMD           *rmd;
2252   unsigned int          i,len, rxstat;
2253   rtems_event_set       events;
2254
2255   for (;;)
2256   {
2257
2258      rtems_bsdnet_event_receive( RTEMS_ALL_EVENTS,
2259                                  RTEMS_WAIT|RTEMS_EVENT_ANY,
2260                                  RTEMS_NO_TIMEOUT,
2261                                  &events);
2262
2263      for(;;)
2264      {
2265         for(i=0; i< NUM_UNITS; i++ )
2266         {
2267            sc = &elnk_softc[i];
2268            if( sc->ioaddr )
2269            {
2270               if( events & sc->ioevent )
2271               {
2272                  struct ifnet *ifp = &sc->arpcom.ac_if;
2273
2274                  rmd   = sc->curr_rx_md;
2275
2276                  /*
2277                  ** Read off all the packets we've received on this unit
2278                  */
2279                  while( (rxstat = ld_le32(&rmd->status)) )
2280                  {
2281                     if (rxstat & XL_RXSTAT_UP_ERROR)
2282                     {
2283                        printk("unit %i up error\n", sc->xl_unit );
2284                        ifp->if_ierrors++;
2285                     }
2286
2287                     if( (rxstat & XL_RXSTAT_UP_CMPLT) )
2288                     {
2289
2290#if 0
2291                        {
2292                           char *pkt, *delim;
2293                           int  i;
2294                           pkt = mtod(rmd->mbuf, char *);
2295                           printk("unit %i rx  pkt (%08x) ", sc->xl_unit, pkt );
2296                           for(delim="", i=0; i < sizeof(struct ether_header)+8; i++, delim=":")
2297                              printk("%s%02x", delim, (char) pkt[i] );
2298                           printk("\n");
2299                        }
2300#endif
2301
2302                        /* pass on the packet in the mbuf */
2303                        len = ( ld_le32(&rmd->status) & XL_RXSTAT_LENMASK);
2304                        m = rmd->mbuf;
2305                        m->m_len = m->m_pkthdr.len = len - sizeof(struct ether_header);
2306                        eh = mtod(m, struct ether_header *);
2307                        m->m_data += sizeof(struct ether_header);
2308
2309                        ether_input(ifp, eh, m);
2310
2311                        /* get a new mbuf */
2312                        MGETHDR (m, M_WAIT, MT_DATA);
2313                        MCLGET (m, M_WAIT);
2314                        m->m_pkthdr.rcvif = ifp;
2315                        rmd->mbuf   = m;
2316                        st_le32( &rmd->status, 0 );
2317                        st_le32( &rmd->addr, (uint32_t)phys_to_bus(mtod(m, void *)) );
2318                     }
2319                     else
2320                     {
2321                        /* some kind of packet failure */
2322                        printk("etherlink : unit elnk%d bad receive status -- packet dropped\n", sc->xl_unit);
2323                        ifp->if_ierrors++;
2324                     }
2325                     /* clear descriptor status */
2326                     rmd->status = 0;
2327
2328                     rmd = rmd->next_md;
2329                  }
2330
2331                  sc->curr_rx_md = rmd;
2332               }
2333            }
2334         }
2335
2336         /*
2337         ** If more events are pending, service them before we go back to sleep
2338         */
2339         if( rtems_event_receive( RTEMS_ALL_EVENTS,
2340                                  RTEMS_NO_WAIT | RTEMS_EVENT_ANY,
2341                                  0,
2342                                  &events ) == RTEMS_UNSATISFIED ) break;
2343      }
2344   }
2345}
2346
2347
2348
2349
2350
2351
2352
2353
2354/*
2355 * Driver transmit daemon
2356 */
2357void
2358elnk_txDaemon (void *arg)
2359{
2360   struct elnk_softc     *sc;
2361   struct ifnet          *ifp;
2362   struct mbuf           *m;
2363   struct TXMD           *lastmd, *nextmd, *firstmd;
2364   int                   chainCount,i;
2365   rtems_event_set       events;
2366
2367   for (;;)
2368   {
2369      /*
2370       * Wait for any unit's signal to wake us up
2371       */
2372      rtems_bsdnet_event_receive( RTEMS_ALL_EVENTS,
2373                                  RTEMS_EVENT_ANY | RTEMS_WAIT,
2374                                  RTEMS_NO_TIMEOUT, &events);
2375
2376      for(i=0; i< NUM_UNITS; i++ )
2377      {
2378         sc  = &elnk_softc[i];
2379         if( sc->ioaddr )
2380         {
2381            if( events & sc->ioevent )
2382            {
2383               ifp = &sc->arpcom.ac_if;
2384
2385               /*
2386                * Send packets till queue is empty or tx ring is full
2387                */
2388
2389               chainCount = 0;
2390               firstmd = NULL;
2391
2392               lastmd = sc->last_tx_md;
2393
2394               for(;;)
2395               {
2396                  /*
2397                  ** Check the chain recovery queue whenever the tx
2398                  ** daemon services the stack.  Note this routine does
2399                  ** not assume the context of one of the lanboard units
2400                  ** because used tx mbufs are no longer associated with
2401                  ** any unit.
2402                  */
2403                  {
2404                     struct TXMD *chainhead, *chaintail;
2405                     size_t esize;
2406
2407                     if( rtems_message_queue_receive( chainRecoveryQueue, &chainhead, &esize,
2408                                                      RTEMS_NO_WAIT, 0) == RTEMS_SUCCESSFUL )
2409                     {
2410                        /* get a pointer to the tail */
2411                        chaintail = chainhead->chainptr;
2412
2413                        /* if the tail points somewhere, free the entire
2414                           chain */
2415                        if( chaintail && (int)chaintail != -1 )
2416                        {
2417                           for(;;)
2418                           {
2419                              m_freem( chainhead->mbuf );
2420                              st_le32( &chainhead->status, XL_TXSTAT_DL_COMPLETE );
2421                              chainhead->mbuf = NULL;
2422
2423                              if(  chainhead == chaintail ) break;
2424                              chainhead = chainhead->next_md;
2425                           }
2426                        }
2427                        else
2428                        {
2429                           /* a single packet chain */
2430                           m_freem( chainhead->mbuf );
2431                           st_le32( &chainhead->status, XL_TXSTAT_DL_COMPLETE );
2432                           chainhead->mbuf = NULL;
2433                        }
2434                     }
2435                  }
2436
2437                  nextmd = lastmd->next_md;
2438
2439                  /* stop when ring is full */
2440                  if( ! (ld_le32(&nextmd->status) & XL_TXSTAT_DL_COMPLETE) )
2441                  {
2442                     printk("etherlink : unit elnk%d tx ring full!\n", sc->xl_unit);
2443                     break;
2444                  }
2445                  /* sanity check the next packet descriptor */
2446                  if( nextmd->mbuf )
2447                  {
2448                     printk("etherlink : unit elnk%d tx ring corrupt!\n", sc->xl_unit);
2449                     break;
2450                  }
2451
2452
2453
2454                  IF_DEQUEUE(&ifp->if_snd, m);
2455                  if( !m ) break;
2456
2457                  {
2458                     int i;
2459
2460                     nextmd->mbuf = m;
2461
2462                     for(i=0; i< NUM_FRAGS; i++)
2463                     {
2464                        st_le32( &nextmd->txfrags[i].length, ((m->m_next)?0:XL_LAST_FRAG) | ( m->m_len & XL_TXSTAT_LENMASK) );
2465                        st_le32( &nextmd->txfrags[i].addr, (uint32_t)phys_to_bus( m->m_data ) );
2466                        if ((m = m->m_next) == NULL)
2467                           break;
2468                     }
2469                     if( m )
2470                     {
2471                        printk("etherlink : unit elnk%d tx fragments exhausted, truncating packet!\n", sc->xl_unit);
2472                        st_le32( &nextmd->txfrags[NUM_FRAGS-1].length, XL_LAST_FRAG |
2473                                 ld_le32( &nextmd->txfrags[NUM_FRAGS-1].length) );
2474                     }
2475                  }
2476
2477#if 0
2478                  {
2479                     char *pkt = bus_to_phys( ld_le32( &nextmd->txfrags[i].addr )), *delim;
2480                     int  i;
2481                     printk("unit %d queued  pkt (%08x) ", sc->xl_unit, (uint32_t)pkt );
2482                     for(delim="", i=0; i < sizeof(struct ether_header); i++, delim=":")
2483                        printk("%s%02x", delim, (char) pkt[i] );
2484                     printk("\n");
2485                  }
2486#endif
2487
2488
2489                  /* this packet will be the new end of the list */
2490                  st_le32( &nextmd->next, 0);
2491                  st_le32( &nextmd->status, 0);
2492
2493                  if( !firstmd )
2494                  {
2495                     /* keep track of the first packet we add to the chain */
2496                     firstmd = nextmd;
2497
2498                     /*
2499                      ** use the chainbuf pointer of the last packet of
2500                      ** the previous chain as a flag so when a
2501                      ** dnComplete interrupt indicates the card is
2502                      ** finished downloading the chain, the isr can
2503                      ** immediately start the next which always begins
2504                      ** with the next packet in the ring.  Note several
2505                      ** chains of packets may be assembled this way.
2506                      */
2507                     lastmd->chainptr = (struct TXMD *)-1;
2508                  }
2509                  else
2510                  {
2511                     /* hook this packet to the previous one */
2512                     st_le32( &lastmd->next, (uint32_t)phys_to_bus( nextmd ));
2513                  }
2514
2515                  ++chainCount;
2516                  lastmd = nextmd;
2517               }
2518
2519
2520
2521
2522
2523               if( firstmd )
2524               {
2525                  /* only enter if we've queued one or more packets */
2526
2527                  /* save the last descriptor we set up in the chain */
2528                  sc->last_tx_md = lastmd;
2529
2530                  /*
2531                   * We've added one or more packets to a chain, flag
2532                   * the last packet so we get an dnComplete interrupt
2533                   * when the card finishes accepting the chain
2534                   */
2535                  st_le32( &lastmd->status, XL_TXSTAT_DL_INTR );
2536
2537                  /*
2538                   * point the chain head's chainptr to the tail so we
2539                   * can jump to the next chain to send inside the isr.
2540                   * If we're only sending one packet, then don't bother
2541                   * with the link, as the chainptr value will either be
2542                   * 0 if theres no next chain or -1 if there is.
2543                   */
2544                  if( chainCount > 1 )
2545                  {
2546                     firstmd->chainptr = lastmd;
2547
2548                     sc->chain_lengths[sc->chlenIndex]= (short)chainCount;
2549                     if( ++sc->chlenIndex == NUM_CHAIN_LENGTHS ) sc->chlenIndex = 0;
2550                  }
2551
2552                  /*
2553                  ** clear the last packet's chainptr flag.  If another
2554                  ** chain is added later but before this chain is
2555                  ** finished being sent, this flag on this packet will
2556                  ** be re-set to -1
2557                  */
2558                  lastmd->chainptr = NULL;
2559
2560#if 0
2561                  printk("unit %d queued %d pkts, lastpkt status %08X\n",
2562                         sc->xl_unit,
2563                         chainCount,
2564                         (uint32_t)ld_le32( &lastmd->status) );
2565#endif
2566
2567                  if( sc->tx_idle == 0 && CSR_READ_4(sc, XL_DOWNLIST_PTR) == 0 )
2568                  {
2569                     printk("etherlink : unit elnk%d tx forced!\n", sc->xl_unit);
2570                     sc->tx_idle = -1;
2571                  }
2572
2573                  /*
2574                  ** start sending this chain of packets if tx isn't
2575                  ** busy, else the dnComplete interrupt will see there
2576                  ** is another chain waiting and begin it immediately.
2577                  */
2578                  if( sc->tx_idle )
2579                  {
2580#if 0
2581                     printk("etherlink : unit elnk%d tx started %d packets\n", sc->xl_unit, chainCount );
2582#endif
2583                     elnk_start_txchain(sc, firstmd);
2584                  }
2585               }
2586
2587
2588               ifp->if_flags &= ~IFF_OACTIVE;
2589            }
2590         }
2591      }
2592   }
2593}
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605static void
2606elnk_start (struct ifnet *ifp)
2607{
2608   struct elnk_softc *sc = ifp->if_softc;
2609#if 0
2610   printk("unit %i tx signaled\n", sc->xl_unit );
2611#endif
2612   ifp->if_flags |= IFF_OACTIVE;
2613   rtems_event_send( txDaemonTid, sc->ioevent );
2614}
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630/*
2631 * Initialize and start the device
2632 */
2633static void
2634elnk_init (void *arg)
2635{
2636   int i;
2637   struct elnk_softc *sc = arg;
2638   struct ifnet *ifp = &sc->arpcom.ac_if;
2639
2640   if( !(ifp->if_flags & IFF_RUNNING) )
2641   {
2642      xl_stop(sc);
2643      xl_reset(sc);
2644      sc->tx_idle = -1;
2645
2646      {
2647         uint32_t   cr,sr;
2648
2649         xl_miibus_writereg(sc, 0x18, MII_BMCR, BMCR_RESET );
2650
2651         while( (cr = xl_miibus_readreg(sc, 0x18, MII_BMCR )) & BMCR_RESET )
2652         {
2653            DELAY(100000);
2654         }
2655
2656         xl_miibus_writereg(sc, 0x18, MII_ANAR, ANAR_10 | ANAR_TX | ANAR_10_FD | ANAR_TX_FD );  /*  ANAR_T4 */
2657         xl_miibus_writereg(sc, 0x18, MII_BMCR, BMCR_STARTNEG | BMCR_AUTOEN );
2658
2659         for (i=0; ((sr = xl_miibus_readreg(sc, 0x18, MII_BMSR)) & BMSR_ACOMP) == 0 && i < 20; i++)
2660            DELAY(10000);
2661      }
2662
2663
2664      /*
2665       * Set up hardware if its not already been done
2666       */
2667      if( !sc->irqInfo.hdl )
2668      {
2669         elnk_initialize_hardware(sc);
2670      }
2671
2672      /*
2673       * Enable the card
2674       */
2675      {
2676         u_int8_t               rxfilt;
2677
2678         /* Init our MAC address */
2679         XL_SEL_WIN(2);
2680         for (i = 0; i < ETHER_ADDR_LEN; i++)
2681         {
2682            CSR_WRITE_1(sc, XL_W2_STATION_ADDR_LO + i, sc->arpcom.ac_enaddr[i]);
2683         }
2684
2685         {
2686            int  media = IFM_ETHER|IFM_100_TX|IFM_FDX;
2687
2688            xl_mediacheck(sc);
2689
2690            /* Choose a default media. */
2691            switch(sc->xl_xcvr) {
2692               case XL_XCVR_10BT:
2693                  media = IFM_ETHER|IFM_10_T;
2694                  xl_setmode(sc, media);
2695                  break;
2696               case XL_XCVR_AUI:
2697                  if (sc->xl_type == XL_TYPE_905B &&
2698                      sc->xl_media == XL_MEDIAOPT_10FL) {
2699                     media = IFM_ETHER|IFM_10_FL;
2700                     xl_setmode(sc, media);
2701                  } else {
2702                     media = IFM_ETHER|IFM_10_5;
2703                     xl_setmode(sc, media);
2704                  }
2705                  break;
2706               case XL_XCVR_COAX:
2707                  media = IFM_ETHER|IFM_10_2;
2708                  xl_setmode(sc, media);
2709                  break;
2710               case XL_XCVR_AUTO:
2711               case XL_XCVR_100BTX:
2712                  xl_setcfg(sc);
2713                  break;
2714               case XL_XCVR_MII:
2715                  printk("etherlink : unit elnk%d MII media not supported!\n", sc->xl_unit);
2716                  break;
2717               case XL_XCVR_100BFX:
2718                  media = IFM_ETHER|IFM_100_FX;
2719                  break;
2720               default:
2721                  printk("etherlink : unit elnk%d unknown XCVR type: %d\n", sc->xl_unit, sc->xl_xcvr);
2722                  /*
2723                   * This will probably be wrong, but it prevents
2724                   * the ifmedia code from panicking.
2725                   */
2726                  media = IFM_ETHER|IFM_10_T;
2727                  break;
2728            }
2729
2730
2731            if (sc->xl_flags & XL_FLAG_NO_XCVR_PWR) {
2732               XL_SEL_WIN(0);
2733               CSR_WRITE_2(sc, XL_W0_MFG_ID, XL_NO_XCVR_PWR_MAGICBITS);
2734            }
2735         }
2736
2737
2738
2739         XL_SEL_WIN(2);
2740         /* Clear the station mask. */
2741         for (i = 0; i < 3; i++)
2742            CSR_WRITE_2(sc, XL_W2_STATION_MASK_LO + (i * 2), 0);
2743
2744         /*
2745          * Set the TX freethresh value.
2746          * Note that this has no effect on 3c905B "cyclone"
2747          * cards but is required for 3c900/3c905 "boomerang"
2748          * cards in order to enable the download engine.
2749          */
2750         CSR_WRITE_1(sc, XL_TX_FREETHRESH, XL_PACKET_SIZE >> 8);
2751
2752         /* Set the TX start threshold for best performance. */
2753         sc->xl_tx_thresh = XL_MIN_FRAMELEN;
2754         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_SET_START|sc->xl_tx_thresh);
2755
2756         /*
2757          * If this is a 3c905B, also set the tx reclaim threshold.
2758          * This helps cut down on the number of tx reclaim errors
2759          * that could happen on a busy network. The chip multiplies
2760          * the register value by 16 to obtain the actual threshold
2761          * in bytes, so we divide by 16 when setting the value here.
2762          * The existing threshold value can be examined by reading
2763          * the register at offset 9 in window 5.
2764          */
2765         if (sc->xl_type == XL_TYPE_905B) {
2766            CSR_WRITE_2(sc, XL_COMMAND,
2767                        XL_CMD_SET_TX_RECLAIM|(XL_PACKET_SIZE >> 4));
2768         }
2769
2770         /* Set RX filter bits. */
2771         XL_SEL_WIN(5);
2772         rxfilt = CSR_READ_1(sc, XL_W5_RX_FILTER);
2773
2774         /* Set the individual bit to receive frames for this host only. */
2775         rxfilt |= XL_RXFILTER_INDIVIDUAL;
2776
2777         /* If we want promiscuous mode, set the allframes bit. */
2778         if (ifp->if_flags & IFF_PROMISC) {
2779            rxfilt |= XL_RXFILTER_ALLFRAMES;
2780            CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2781         } else {
2782            rxfilt &= ~XL_RXFILTER_ALLFRAMES;
2783            CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2784         }
2785
2786         /*
2787          * Set capture broadcast bit to capture broadcast frames.
2788          */
2789         if (ifp->if_flags & IFF_BROADCAST) {
2790            rxfilt |= XL_RXFILTER_BROADCAST;
2791            CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2792         } else {
2793            rxfilt &= ~XL_RXFILTER_BROADCAST;
2794            CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2795         }
2796
2797#if 0
2798         /*
2799          * Program the multicast filter, if necessary.
2800          */
2801         if (sc->xl_type == XL_TYPE_905B)
2802            xl_setmulti_hash(sc);
2803         else
2804            xl_setmulti(sc);
2805#endif
2806         /*
2807          * Load the address of the RX list. We have to
2808          * stall the upload engine before we can manipulate
2809          * the uplist pointer register, then unstall it when
2810          * we're finished. We also have to wait for the
2811          * stall command to complete before proceeding.
2812          * Note that we have to do this after any RX resets
2813          * have completed since the uplist register is cleared
2814          * by a reset.
2815          */
2816         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_UP_STALL);
2817         xl_wait(sc);
2818         CSR_WRITE_4(sc, XL_UPLIST_PTR, phys_to_bus( sc->curr_rx_md ));
2819         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_UP_UNSTALL);
2820         xl_wait(sc);
2821
2822
2823#if 0
2824         if (sc->xl_type == XL_TYPE_905B) {
2825            /* Set polling interval */
2826            CSR_WRITE_1(sc, XL_DOWN_POLL, 64);
2827            xl_wait(sc);
2828            printk("etherlink : unit elnk%d tx polling enabled\n", sc->xl_unit );
2829         }
2830#endif
2831
2832         /*
2833          * If the coax transceiver is on, make sure to enable
2834          * the DC-DC converter.
2835          */
2836         XL_SEL_WIN(3);
2837         if (sc->xl_xcvr == XL_XCVR_COAX)
2838            CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_START);
2839         else
2840            CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
2841
2842         /* increase packet size to allow reception of 802.1q or ISL packets */
2843         if (sc->xl_type == XL_TYPE_905B)
2844            CSR_WRITE_2(sc, XL_W3_MAXPKTSIZE, XL_PACKET_SIZE);
2845         /* Clear out the stats counters. */
2846
2847         memset( &sc->xl_stats, 0, sizeof(struct xl_stats));
2848
2849         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STATS_DISABLE);
2850         sc->xl_stats_no_timeout = 1;
2851         xl_stats_update(sc->stat_timer_id,sc);
2852         sc->xl_stats_no_timeout = 0;
2853         XL_SEL_WIN(4);
2854         CSR_WRITE_2(sc, XL_W4_NET_DIAG, XL_NETDIAG_UPPER_BYTES_ENABLE);
2855         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STATS_ENABLE);
2856
2857
2858         /*
2859          * Enable interrupts.
2860          */
2861         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ACK|0xFF);
2862         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STAT_ENB|XL_INTRS);
2863         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ENB|XL_INTRS);
2864
2865         /* Set the RX early threshold */
2866         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_THRESH|(XL_PACKET_SIZE >>2));
2867         CSR_WRITE_4(sc, XL_DMACTL, XL_DMACTL_UP_RX_EARLY );
2868
2869         /* Enable receiver and transmitter. */
2870         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_ENABLE);
2871         xl_wait(sc);
2872         CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_ENABLE);
2873         xl_wait(sc);
2874
2875         /* Select window 7 for normal operations. */
2876         XL_SEL_WIN(7);
2877
2878         /* schedule the stats update timer */
2879         rtems_timer_fire_after( sc->stat_timer_id, sc->stats_update_ticks, xl_stats_update, (void *)sc );
2880      }
2881
2882      /*
2883       * Tell the world that we're running.
2884       */
2885      ifp->if_flags |= IFF_RUNNING;
2886   }
2887}
2888
2889
2890
2891
2892
2893
2894
2895/*
2896 * Stop the device
2897 */
2898static void
2899elnk_stop (struct elnk_softc *sc)
2900{
2901   struct ifnet *ifp = &sc->arpcom.ac_if;
2902   int i;
2903
2904   /*
2905    * Stop the transmitter
2906    */
2907   xl_stop(sc);
2908   xl_reset(sc);
2909   sc->tx_idle = -1;
2910
2911   ifp->if_flags &= ~IFF_RUNNING;
2912
2913   /*
2914   ** Clear out the rx & tx rings
2915   */
2916   {
2917      struct TXMD *chainhead;
2918      size_t esize;
2919
2920      while( rtems_message_queue_receive( chainRecoveryQueue, &chainhead, &esize,
2921                                          RTEMS_NO_WAIT, 0) == RTEMS_SUCCESSFUL );
2922   }
2923
2924   for(i=0 ; i<sc->numRxbuffers; i++)
2925   {
2926      st_le32( &sc->rx_ring[i].status, 0);
2927      st_le32( &sc->rx_ring[i].length, XL_LAST_FRAG | XL_PACKET_SIZE );
2928   }
2929
2930   for(i=0 ; i<sc->numTxbuffers; i++)
2931   {
2932      st_le32( &sc->tx_ring[i].status, XL_TXSTAT_DL_COMPLETE );
2933      st_le32( &sc->tx_ring[i].next, 0);
2934      if( sc->tx_ring[i].mbuf )
2935      {
2936         m_free( sc->tx_ring[i].mbuf );
2937         sc->tx_ring[i].mbuf = NULL;
2938      }
2939   }
2940}
2941
2942
2943
2944
2945/*
2946 * Show interface statistics
2947 */
2948static void
2949elnk_stats (struct elnk_softc *sc)
2950{
2951   printf("    MII PHY data { anr:%04x  lpar:%04x  stat:%04x  ctl:%04x }\n",
2952          sc->xl_stats.miianr,
2953          sc->xl_stats.miipar,
2954          sc->xl_stats.miistatus,
2955          sc->xl_stats.miicmd);
2956
2957   printf("         internalcfg:%08x            macctl:%04x      dmactl:%08x\n",
2958          sc->xl_stats.internalconfig,
2959          sc->xl_stats.mac_control,
2960          sc->xl_stats.dmactl);
2961
2962   printf("            rxstatus:%04x              txstatus:%02x       smbstat:%04x\n",
2963          sc->xl_stats.rxstatus,
2964          sc->xl_stats.txstatus,
2965          sc->xl_stats.smbstatus);
2966
2967   printf("              txfree:%04X             intstatus:%04x   mediastat:%04x\n",
2968          sc->xl_stats.txfree,
2969          sc->xl_stats.intstatus,
2970          sc->xl_stats.mediastatus);
2971
2972
2973   {
2974      int       i, totalLengths= 0, numLengths= 0;
2975
2976      for(i=0; i< NUM_CHAIN_LENGTHS; i++)
2977      {
2978         if( sc->chain_lengths[i] > -1 )
2979         {
2980            totalLengths += sc->chain_lengths[i];
2981            ++numLengths;
2982         }
2983      }
2984
2985      printf("          interrupts:%-9d       txcmp_ints:%-5d  avg_chain_len:%-4d\n",
2986             sc->xl_stats.device_interrupts,
2987             sc->xl_stats.txcomplete_ints,
2988             numLengths ? (totalLengths / numLengths) : -1 );
2989   }
2990
2991   printf("        carrier_lost:%-5d             sqe_errs:%-5d\n",
2992          sc->xl_stats.xl_carrier_lost,
2993          sc->xl_stats.xl_sqe_errs);
2994
2995   printf("  tx_multi_collision:%-5d  tx_single_collision:%-5d\n",
2996          sc->xl_stats.xl_tx_multi_collision,
2997          sc->xl_stats.xl_tx_single_collision);
2998
2999   printf("   tx_late_collision:%-5d           rx_overrun:%-5d\n",
3000          sc->xl_stats.xl_tx_late_collision,
3001          sc->xl_stats.xl_rx_overrun);
3002
3003   printf("         tx_deferred:%-5d               badssd:%-5d\n",
3004          sc->xl_stats.xl_tx_deferred,
3005          sc->xl_stats.xl_badssd);
3006
3007   printf("        rx_frames_ok:%-9d     tx_frames_ok:%-9d\n",
3008          sc->xl_stats.xl_rx_frames_ok,
3009          sc->xl_stats.xl_tx_frames_ok);
3010
3011   printf("         rx_bytes_ok:%-9d      tx_bytes_ok:%-9d\n",
3012          sc->xl_stats.xl_rx_bytes_ok,
3013          sc->xl_stats.xl_tx_bytes_ok );
3014}
3015
3016
3017
3018
3019
3020
3021
3022/*
3023 * Driver ioctl handler
3024 */
3025static int
3026elnk_ioctl (struct ifnet *ifp, ioctl_command_t command, caddr_t data)
3027{
3028   struct elnk_softc *sc = ifp->if_softc;
3029   int error = 0;
3030
3031   switch (command) {
3032      case SIOCGIFADDR:
3033      case SIOCSIFADDR:
3034         ether_ioctl (ifp, command, data);
3035         break;
3036
3037      case SIOCSIFFLAGS:
3038         switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
3039            case IFF_RUNNING:
3040               elnk_stop (sc);
3041               break;
3042
3043            case IFF_UP:
3044               elnk_init (sc);
3045               break;
3046
3047            case IFF_UP | IFF_RUNNING:
3048               elnk_stop (sc);
3049               elnk_init (sc);
3050               break;
3051
3052            default:
3053               break;
3054         }
3055         break;
3056
3057      case SIO_RTEMS_SHOW_STATS:
3058         elnk_stats (sc);
3059         break;
3060
3061         /*
3062          * FIXME: All sorts of multicast commands need to be added here!
3063          */
3064      default:
3065         error = EINVAL;
3066         break;
3067   }
3068
3069   return error;
3070}
3071
3072
3073
3074
3075
3076
3077
3078
3079#if 0
3080static int iftap(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m )
3081{
3082   int i;
3083   char *delim, *pkt;
3084
3085   printk("unit %i, src ", ifp->if_unit );
3086   for(delim= "", i=0; i< ETHER_ADDR_LEN; i++, delim=":")
3087      printk("%s%02x", delim, (char) eh->ether_shost[i] );
3088
3089   printk(" dest ");
3090
3091   for(delim= "", i=0; i< ETHER_ADDR_LEN; i++, delim=":")
3092      printk("%s%02x", delim, (char) eh->ether_dhost[i] );
3093   printk("  pkt ");
3094
3095   pkt = (char *)eh;
3096   for(delim="", i=0; i < sizeof(struct ether_header); i++, delim=":")
3097      printk("%s%02x", delim, (char) pkt[i] );
3098
3099   printk("\n");
3100   return 0;
3101}
3102#endif
3103
3104
3105
3106struct el_boards
3107{
3108      int pbus,pdev,pfun, vid, did, tindex;
3109};
3110
3111/*
3112 * Attach an ELNK driver to the system
3113 */
3114int
3115rtems_elnk_driver_attach (struct rtems_bsdnet_ifconfig *config, int attach)
3116{
3117   struct elnk_softc *sc;
3118   struct ifnet *ifp;
3119   char         *unitName;
3120   int          unitNumber;
3121   int          mtu, i;
3122   unsigned char cvalue;
3123   struct el_boards         sysboards[NUM_UNITS];
3124   int                      numFound = 0;
3125   int          pbus, pdev, pfun;
3126#if defined(__i386__)
3127   uint32_t     value;
3128   uint8_t      interrupt;
3129#endif
3130#if defined(__PPC__)
3131   uint32_t     lvalue;
3132#endif
3133
3134
3135   /*
3136    * Get the instance number for the board we're going to configure
3137    * from the user.
3138    */
3139   if( (unitNumber = rtems_bsdnet_parse_driver_name( config, &unitName)) == -1 )
3140   {
3141      return 0;
3142   }
3143
3144   if( strcmp(unitName, DRIVER_PREFIX) )
3145   {
3146      printk("etherlink : invalid unit name '%s'\n", unitName );
3147      return 0;
3148   }
3149
3150   if ((unitNumber < 1) || (unitNumber > NUM_UNITS))
3151   {
3152      printk("etherlink : unit %i is invalid, must be (1 <= n <= %d)\n", unitNumber, NUM_UNITS);
3153      return 0;
3154   }
3155
3156
3157   {
3158      int  done= 0, unum;
3159
3160      /*
3161       * Run thru the list of boards, finding all that are present in
3162       * the system.  Sort by slot,dev - and then use the unitNumber-1
3163       * to index the list and select the device.  Yucky.
3164       */
3165      for( i=0; !done && xl_devs[i].xl_vid; i++)
3166      {
3167         for(unum= 1; !done &&
3168                pci_find_device( xl_devs[i].xl_vid, xl_devs[i].xl_did, unum-1,
3169                                         &sysboards[numFound].pbus,
3170                                         &sysboards[numFound].pdev,
3171                                                  &sysboards[numFound].pfun)==0; unum++)
3172         {
3173            if( numFound == NUM_UNITS )
3174            {
3175               printk("etherlink : Maximum of %d units found, extra devices ignored.\n", NUM_UNITS );
3176               done=-1;
3177            }
3178            else
3179            {
3180               sysboards[numFound].vid = xl_devs[i].xl_vid;
3181               sysboards[numFound].did = xl_devs[i].xl_did;
3182               sysboards[numFound].tindex = i;
3183               ++numFound;
3184            }
3185         }
3186      }
3187
3188      if( ! numFound )
3189      {
3190         printk("etherlink : No Etherlink devices found\n");
3191         return 0;
3192      }
3193
3194      if( unitNumber-1 >= numFound )
3195      {
3196         printk("etherlink : device '%s' not found\n", config->name );
3197         return 0;
3198      }
3199
3200      /*
3201      * Got the list of etherlink boards in the system, now sort by
3202      * slot,device.  bubble sorts aren't all that wonderful, but this
3203      * is a short & infrequently sorted list.
3204      */
3205      if( numFound > 1 )
3206      {
3207         struct el_boards       tboard;
3208         int                    didsort;
3209
3210         do
3211         {
3212            didsort = 0;
3213
3214            for(i=1; i<numFound; i++)
3215            {
3216               if( sysboards[i-1].pbus > sysboards[i].pbus ||
3217                   (sysboards[i-1].pbus == sysboards[i].pbus && sysboards[i-1].pdev > sysboards[i].pdev) )
3218               {
3219                  memcpy(&tboard, &sysboards[i-1], sizeof(struct el_boards));
3220                  memcpy(&sysboards[i-1], &sysboards[i], sizeof(struct el_boards));
3221                  memcpy(&sysboards[i], &tboard, sizeof(struct el_boards));
3222                  didsort++;
3223               }
3224            }
3225         }
3226         while( didsort );
3227      }
3228
3229      /*
3230      ** board list is sorted, now select the unit
3231      */
3232
3233      pbus = sysboards[unitNumber-1].pbus;
3234      pdev = sysboards[unitNumber-1].pdev;
3235      pfun = sysboards[unitNumber-1].pfun;
3236   }
3237
3238   sc = &elnk_softc[unitNumber - 1];
3239   ifp = &sc->arpcom.ac_if;
3240   if (ifp->if_softc != NULL)
3241   {
3242      printk("etherlink : unit %i already in use.\n", unitNumber );
3243      return 0;
3244   }
3245
3246   /*
3247   ** Save various things
3248   */
3249   sc->xl_unit = unitNumber;
3250   sc->xl_type = sysboards[ unitNumber-1 ].tindex;
3251
3252   sc->vendorID = sysboards[numFound].vid;
3253   sc->deviceID = sysboards[numFound].did;
3254
3255   sc->numRxbuffers = (config->rbuf_count) ? config->rbuf_count : RX_RING_SIZE;
3256   sc->numTxbuffers = (config->xbuf_count) ? config->xbuf_count : TX_RING_SIZE;
3257
3258
3259   for(i=0; i< NUM_CHAIN_LENGTHS; i++) sc->chain_lengths[i]= -1;
3260   sc->chlenIndex = 0;
3261
3262
3263   if (config->mtu)
3264      mtu = config->mtu;
3265   else
3266      mtu = ETHERMTU;
3267
3268   sc->acceptBroadcast = !config->ignore_broadcast;
3269
3270
3271
3272#ifdef ELNK_DEBUG
3273   printk("etherlink : device '%s', name 'elnk%d', pci %02x:%02x.%02x, %d rx/%d tx buffers\n",
3274          xl_devs[sc->xl_type].xl_name, sc->xl_unit,
3275          pbus, pdev, pfun,
3276          sc->numRxbuffers, sc->numTxbuffers);
3277#endif
3278
3279
3280   /*
3281   ** Create this unit's stats timer
3282   */
3283   if( rtems_timer_create( rtems_build_name( 'X', 'L', 't', (char)(sc->xl_unit & 255)),
3284                           &sc->stat_timer_id ) != RTEMS_SUCCESSFUL )
3285   {
3286      printk("etherlink : unit elnk%d unable to create stats timer\n", sc->xl_unit );
3287      return 0;
3288   }
3289
3290   /* update stats 1 times/second if things aren't incrementing fast
3291    * enough to trigger stats interrupts
3292    */
3293   rtems_clock_get( RTEMS_CLOCK_GET_TICKS_PER_SECOND, &sc->stats_update_ticks );
3294
3295
3296   /*
3297   ** Get this unit's rx/tx event
3298   */
3299   sc->ioevent = unit_signals[unitNumber-1];
3300
3301
3302#if defined(__i386__)
3303   pci_read_config_dword(pbus, pdev, pfun, 16, &value);
3304   sc->ioaddr = value & ~IO_MASK;
3305
3306   pci_read_config_byte(pbus, pdev, pfun, 60, &interrupt);
3307   cvalue = interrupt;
3308#endif
3309#if defined(__PPC__)
3310   /*
3311   ** Prep the board
3312   */
3313   pci_write_config_word(pbus, pdev, pfun,
3314                         PCI_COMMAND,
3315                         (uint16_t)( PCI_COMMAND_IO |
3316                                       PCI_COMMAND_MASTER |
3317                                       PCI_COMMAND_INVALIDATE |
3318                                       PCI_COMMAND_WAIT ) );
3319   /*
3320    * Get the device's base address
3321    */
3322   pci_read_config_dword(pbus, pdev, pfun,
3323                         PCI_BASE_ADDRESS_0,
3324                         &lvalue);
3325
3326   sc->ioaddr = (uint32_t)lvalue & PCI_BASE_ADDRESS_IO_MASK;
3327   /*
3328   ** Store the interrupt name, we'll use it later when we initialize
3329   ** the board.
3330   */
3331   pci_read_config_byte(pbus, pdev, pfun,
3332                        PCI_INTERRUPT_LINE,
3333                        &cvalue);
3334#endif
3335
3336   memset(&sc->irqInfo,0,sizeof(rtems_irq_connect_data));
3337   sc->irqInfo.name = cvalue;
3338
3339
3340   /*
3341   ** Establish basic board config, set node address from config or
3342   ** board eeprom, do stuff with additional device properties
3343   */
3344
3345   {
3346      uint8_t   pci_latency;
3347      uint8_t   new_latency = 248;
3348
3349      /* Check the PCI latency value.  On the 3c590 series the latency timer
3350         must be set to the maximum value to avoid data corruption that occurs
3351         when the timer expires during a transfer.  This bug exists the Vortex
3352         chip only. */
3353#if defined(__i386__)
3354      pci_read_config_byte(pbus, pdev, pfun, 0x0d, &pci_latency);
3355#endif
3356#if defined(__PPC__)
3357      pci_read_config_byte(pbus,pdev,pfun, PCI_LATENCY_TIMER, &pci_latency);
3358#endif
3359      if (pci_latency < new_latency)
3360      {
3361         printk("etherlink : unit elnk%d Overriding PCI latency, timer (CFLT) setting of %d, new value is %d.\n", sc->xl_unit, pci_latency, new_latency );
3362#if defined(__i386__)
3363         pci_write_config_byte(pbus, pdev, pfun, 0x0d, new_latency);
3364#endif
3365#if defined(__PPC__)
3366         pci_write_config_byte(pbus,pdev,pfun, PCI_LATENCY_TIMER, new_latency);
3367#endif
3368      }
3369   }
3370
3371   /* Reset the adapter. */
3372   xl_reset(sc);
3373
3374
3375   {
3376      u_int16_t         xcvr[2];
3377      u_char            eaddr[ETHER_ADDR_LEN];
3378
3379      sc->xl_flags = 0;
3380      if (sc->deviceID == TC_DEVICEID_HURRICANE_555)
3381         sc->xl_flags |= XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_PHYOK;
3382      if (sc->deviceID == TC_DEVICEID_HURRICANE_556 ||
3383          sc->deviceID == TC_DEVICEID_HURRICANE_556B)
3384         sc->xl_flags |= XL_FLAG_FUNCREG | XL_FLAG_PHYOK |
3385            XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_WEIRDRESET |
3386            XL_FLAG_INVERT_LED_PWR | XL_FLAG_INVERT_MII_PWR;
3387      if (sc->deviceID == TC_DEVICEID_HURRICANE_555 ||
3388          sc->deviceID == TC_DEVICEID_HURRICANE_556)
3389         sc->xl_flags |= XL_FLAG_8BITROM;
3390      if (sc->deviceID == TC_DEVICEID_HURRICANE_556B)
3391         sc->xl_flags |= XL_FLAG_NO_XCVR_PWR;
3392
3393      if (sc->deviceID == TC_DEVICEID_HURRICANE_575A ||
3394          sc->deviceID == TC_DEVICEID_HURRICANE_575B ||
3395          sc->deviceID == TC_DEVICEID_HURRICANE_575C ||
3396          sc->deviceID == TC_DEVICEID_HURRICANE_656B ||
3397          sc->deviceID == TC_DEVICEID_TORNADO_656C)
3398         sc->xl_flags |= XL_FLAG_FUNCREG | XL_FLAG_PHYOK |
3399            XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_8BITROM;
3400      if (sc->deviceID == TC_DEVICEID_HURRICANE_656)
3401         sc->xl_flags |= XL_FLAG_FUNCREG | XL_FLAG_PHYOK;
3402      if (sc->deviceID == TC_DEVICEID_HURRICANE_575B)
3403         sc->xl_flags |= XL_FLAG_INVERT_LED_PWR;
3404      if (sc->deviceID == TC_DEVICEID_HURRICANE_575C)
3405         sc->xl_flags |= XL_FLAG_INVERT_MII_PWR;
3406      if (sc->deviceID == TC_DEVICEID_TORNADO_656C)
3407         sc->xl_flags |= XL_FLAG_INVERT_MII_PWR;
3408      if (sc->deviceID == TC_DEVICEID_HURRICANE_656 ||
3409          sc->deviceID == TC_DEVICEID_HURRICANE_656B)
3410         sc->xl_flags |= XL_FLAG_INVERT_MII_PWR |
3411            XL_FLAG_INVERT_LED_PWR;
3412      if (sc->deviceID == TC_DEVICEID_TORNADO_10_100BT_920B)
3413         sc->xl_flags |= XL_FLAG_PHYOK;
3414
3415
3416      if (config->hardware_address)
3417      {
3418         memcpy(sc->arpcom.ac_enaddr, config->hardware_address, ETHER_ADDR_LEN);
3419      }
3420      else
3421      {
3422         if (xl_read_eeprom(sc, (caddr_t)&eaddr, XL_EE_OEM_ADR0, 3, 1))
3423         {
3424            printk("etherlink : unit elnk%d Failed to read station address\n", sc->xl_unit );
3425            return 0;
3426         }
3427         memcpy((char *)&sc->arpcom.ac_enaddr, eaddr, ETHER_ADDR_LEN);
3428      }
3429
3430      /*
3431       * Figure out the card type. 3c905B adapters have the
3432       * 'supportsNoTxLength' bit set in the capabilities
3433       * word in the EEPROM.
3434       */
3435      xl_read_eeprom(sc, (caddr_t)&sc->xl_caps, XL_EE_CAPS, 1, 0);
3436      if (sc->xl_caps & XL_CAPS_NO_TXLENGTH)
3437         sc->xl_type = XL_TYPE_905B;
3438      else
3439         sc->xl_type = XL_TYPE_90X;
3440
3441
3442      /*
3443       * Now we have to see what sort of media we have.
3444       * This includes probing for an MII interace and a
3445       * possible PHY.
3446       */
3447      XL_SEL_WIN(3);
3448      sc->xl_media = CSR_READ_2(sc, XL_W3_MEDIA_OPT);
3449
3450      xl_read_eeprom(sc, (char *)&xcvr, XL_EE_ICFG_0, 2, 0);
3451      sc->xl_xcvr = xcvr[0] | xcvr[1] << 16;
3452      sc->xl_xcvr &= XL_ICFG_CONNECTOR_MASK;
3453      sc->xl_xcvr >>= XL_ICFG_CONNECTOR_BITS;
3454
3455#if 0
3456      printk("etherlink : unit elnk%d EEPROM set xcvr to 0x%x\n", sc->xl_unit, sc->xl_xcvr);
3457#endif
3458
3459      {
3460         char   msg[255];
3461         int    i;
3462
3463         struct _availmedia
3464         {
3465               int bit;
3466               char *name;
3467         } _am[]= {{ XL_MEDIAOPT_BT4, "100BaseT4" },
3468                   { XL_MEDIAOPT_BTX, "100BaseTX" },
3469                   { XL_MEDIAOPT_BFX, "100BaseFX" },
3470                   { XL_MEDIAOPT_BT,  "10BaseT" },
3471                   { XL_MEDIAOPT_BNC, "10Base2" },
3472                   { XL_MEDIAOPT_AUI, "10mbps AUI"},
3473                   { XL_MEDIAOPT_MII, "MII"},
3474                   { 0, NULL }};
3475
3476         msg[0]= 0;
3477         for( i=0; _am[i].bit; i++)
3478         {
3479            if( sc->xl_media & _am[i].bit )
3480               sprintf( &msg[strlen(msg)], ",%s", _am[i].name );
3481         }
3482         if( !strlen(msg) ) strcpy( &msg[1], "<no media bits>");
3483
3484         printk("etherlink : unit elnk%d available media : %s\n", sc->xl_unit, &msg[1]);
3485      }
3486
3487      XL_SEL_WIN(7);
3488   }
3489
3490
3491
3492   /*
3493    * Set up network interface
3494    */
3495   ifp->if_softc = sc;
3496   ifp->if_name = unitName;
3497   ifp->if_unit = sc->xl_unit;
3498   ifp->if_mtu = mtu;
3499   ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
3500   if (ifp->if_snd.ifq_maxlen == 0)
3501      ifp->if_snd.ifq_maxlen = ifqmaxlen;
3502   ifp->if_init = elnk_init;
3503   ifp->if_start = elnk_start;
3504   ifp->if_ioctl = elnk_ioctl;
3505   ifp->if_output = ether_output;
3506
3507#if 0
3508   ifp->if_tap = iftap;
3509#endif
3510
3511   /*
3512    * Attach the interface
3513    */
3514   if_attach (ifp);
3515   ether_ifattach (ifp);
3516
3517#ifdef ELNK_DEBUG
3518   printk( "etherlink : unit elnk%d driver attached\n", sc->xl_unit );
3519#endif
3520
3521   /*
3522    * Start driver tasks if this is the first unit initialized
3523    */
3524   if (txDaemonTid == 0)
3525   {
3526      if( rtems_message_queue_create( rtems_build_name('X','L','c','r'),
3527                                      sc->numTxbuffers+1,
3528                                      sizeof(struct TXMD *),
3529                                      RTEMS_FIFO | RTEMS_LOCAL,
3530                                      &chainRecoveryQueue ) != RTEMS_SUCCESSFUL )
3531      {
3532         rtems_panic( "etherlink : Unable to create TX buffer recovery queue\n" );
3533      }
3534
3535
3536      rxDaemonTid = rtems_bsdnet_newproc( "XLrx", 4096,
3537                                          elnk_rxDaemon, NULL);
3538
3539      txDaemonTid = rtems_bsdnet_newproc( "XLtx", 4096,
3540                                          elnk_txDaemon, NULL);
3541#ifdef ELNK_DEBUG
3542      printk( "etherlink : driver tasks created\n" );
3543#endif
3544   }
3545
3546   return 1;
3547};
3548
3549#endif /* ELNK_SUPPORTED */
3550
3551/* eof */
Note: See TracBrowser for help on using the repository browser.