source: rtems/bsps/shared/net/elnk.c @ 762fa62

5
Last change on this file since 762fa62 was 27de4e1f, checked in by Sebastian Huber <sebastian.huber@…>, on 04/03/18 at 05:20:11

bsps: Move libchip to bsps

This patch is a part of the BSP source reorganization.

Update #3285.

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