source: rtems/c/src/libchip/network/elnk.c @ ccb670c

4.104.115
Last change on this file since ccb670c was ccb670c, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/06/09 at 04:44:39

2009-11-06 Ralf Corsépius <ralf.corsepius@…>

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