Repository navigation
Expand file tree
/
Copy path3c501.c
More file actions
420 lines (354 loc) · 11.4 KB
/
Copy path3c501.c
File metadata and controls
420 lines (354 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
** File: 3c501.c Jan. 14, 1997
**
** Author: Giovanni Falzoni <gfalzoni@inwind.it>
**
** This file contains specific implementation of the ethernet
** device driver for 3Com Etherlink (3c501) boards. This is a
** very old board and its performances are very poor for today
** network environments.
**
** $Id$
*/
#include "drivers.h"
#include <minix/com.h>
#include <net/hton.h>
#include <net/gen/ether.h>
#include <net/gen/eth_io.h>
#include "dp.h"
#if (ENABLE_3C501 == 1)
#include "3c501.h"
static unsigned char StationAddress[SA_ADDR_LEN] = {0, 0, 0, 0, 0, 0,};
static buff_t *TxBuff = NULL;
/*
** Name: void el1_getstats(dpeth_t *dep)
** Function: Reads statistics counters from board.
**/
static void el1_getstats(dpeth_t * dep)
{
return; /* Nothing to do */
}
/*
** Name: void el1_reset(dpeth_t *dep)
** Function: Reset function specific for Etherlink hardware.
*/
static void el1_reset(dpeth_t * dep)
{
int ix;
for (ix = 0; ix < 8; ix += 1) /* Resets the board */
outb_el1(dep, EL1_CSR, ECSR_RESET);
outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_SYS);
/* Set Ethernet Address on controller */
outb_el1(dep, EL1_CSR, ECSR_LOOP); /* Loopback mode */
for (ix = EL1_ADDRESS; ix < SA_ADDR_LEN; ix += 1)
outb_el1(dep, ix, StationAddress[ix]);
lock();
/* Enable DMA/Interrupt, gain control of Buffer */
outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_SYS);
/* Clear RX packet area */
outw_el1(dep, EL1_RECVPTR, 0);
/* Enable transmit/receive configuration and flush pending interrupts */
outb_el1(dep, EL1_XMIT, EXSR_IDLE | EXSR_16JAM | EXSR_JAM | EXSR_UNDER);
outb_el1(dep, EL1_RECV, dep->de_recv_mode);
inb_el1(dep, EL1_RECV);
inb_el1(dep, EL1_XMIT);
dep->de_flags &= NOT(DEF_XMIT_BUSY);
unlock();
return; /* Done */
}
/*
** Name: void el1_dumpstats(dpeth_t *dep, int port, vir_bytes size)
** Function: Dumps counter on screen (support for console display).
*/
static void el1_dumpstats(dpeth_t * dep)
{
return;
}
/*
** Name: void el1_mode_init(dpeth_t *dep)
** Function: Initializes receicer mode
*/
static void el1_mode_init(dpeth_t * dep)
{
if (dep->de_flags & DEF_BROAD) {
dep->de_recv_mode = ERSR_BROAD | ERSR_RMASK;
} else if (dep->de_flags & DEF_PROMISC) {
dep->de_recv_mode = ERSR_ALL | ERSR_RMASK;
} else if (dep->de_flags & DEF_MULTI) {
dep->de_recv_mode = ERSR_MULTI | ERSR_RMASK;
} else {
dep->de_recv_mode = ERSR_NONE | ERSR_RMASK;
}
outb_el1(dep, EL1_RECV, dep->de_recv_mode);
inb_el1(dep, EL1_RECV);
return;
}
/*
** Name: void el1_recv(dpeth_t *dep, int from, int size)
** Function: Receive function. Called from interrupt handler to
** unload recv. buffer or from main (packet to client)
*/
static void el1_recv(dpeth_t * dep, int from, int size)
{
buff_t *rxptr;
while ((dep->de_flags & DEF_READING) && (rxptr = dep->de_recvq_head)) {
/* Remove buffer from queue and free buffer */
lock();
if (dep->de_recvq_tail == dep->de_recvq_head)
dep->de_recvq_head = dep->de_recvq_tail = NULL;
else
dep->de_recvq_head = rxptr->next;
unlock();
/* Copy buffer to user area */
mem2user(dep, rxptr);
/* Reply information */
dep->de_read_s = rxptr->size;
dep->de_flags |= DEF_ACK_RECV;
dep->de_flags &= NOT(DEF_READING);
/* Return buffer to the idle pool */
free_buff(dep, rxptr);
}
return;
}
/*
** Name: void el1_send(dpeth_t *dep, int from_int, int pktsize)
** Function: Send function. Called from main to transit a packet or
** from interrupt handler when a new packet was queued.
*/
static void el1_send(dpeth_t * dep, int from_int, int pktsize)
{
buff_t *txbuff;
clock_t now;
if (from_int == FALSE) {
if ((txbuff = alloc_buff(dep, pktsize + sizeof(buff_t))) != NULL) {
/* Fill transmit buffer from user area */
txbuff->next = NULL;
txbuff->size = pktsize;
txbuff->client = dep->de_client;
user2mem(dep, txbuff);
} else
panic(dep->de_name, "out of memory for Tx", NO_NUM);
} else if ((txbuff = dep->de_xmitq_head) != NULL) {
/* Get first packet in queue */
lock();
if (dep->de_xmitq_tail == dep->de_xmitq_head)
dep->de_xmitq_head = dep->de_xmitq_tail = NULL;
else
dep->de_xmitq_head = txbuff->next;
unlock();
pktsize = txbuff->size;
} else
panic(dep->de_name, "should not be sending ", NO_NUM);
if ((dep->de_flags & DEF_XMIT_BUSY)) {
if (from_int) panic(dep->de_name, "should not be sending ", NO_NUM);
getuptime(&now);
if ((now - dep->de_xmit_start) > 4) {
/* Transmitter timed out */
DEBUG(printf("3c501: transmitter timed out ... \n"));
dep->de_stat.ets_sendErr += 1;
dep->de_flags &= NOT(DEF_XMIT_BUSY);
el1_reset(dep);
}
/* Queue packet */
lock(); /* Queue packet to receive queue */
if (dep->de_xmitq_head == NULL)
dep->de_xmitq_head = txbuff;
else
dep->de_xmitq_tail->next = txbuff;
dep->de_xmitq_tail = txbuff;
unlock();
} else {
/* Save for retransmission */
TxBuff = txbuff;
dep->de_flags |= (DEF_XMIT_BUSY | DEF_ACK_SEND);
/* Setup board for packet loading */
lock(); /* Buffer to processor */
outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_SYS);
inb_el1(dep, EL1_RECV); /* Clears any spurious interrupt */
inb_el1(dep, EL1_XMIT);
outw_el1(dep, EL1_RECVPTR, 0); /* Clears RX packet area */
/* Loads packet */
outw_el1(dep, EL1_XMITPTR, (EL1_BFRSIZ - pktsize));
outsb(dep->de_data_port, SELF, txbuff->buffer, pktsize);
/* Starts transmitter */
outw_el1(dep, EL1_XMITPTR, (EL1_BFRSIZ - pktsize));
outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_XMIT); /* There it goes... */
unlock();
getuptime(&dep->de_xmit_start);
dep->de_flags &= NOT(DEF_SENDING);
}
return;
}
/*
** Name: void el1_stop(dpeth_t *dep)
** Function: Stops board and disable interrupts.
*/
static void el1_stop(dpeth_t * dep)
{
int ix;
DEBUG(printf("%s: stopping Etherlink ....\n", dep->de_name));
for (ix = 0; ix < 8; ix += 1) /* Reset board */
outb_el1(dep, EL1_CSR, ECSR_RESET);
outb_el1(dep, EL1_CSR, ECSR_SYS);
sys_irqdisable(&dep->de_hook); /* Disable interrupt */
return;
}
/*
** Name: void el1_interrupt(dpeth_t *dep)
** Function: Interrupt handler. Acknwledges transmit interrupts
** or unloads receive buffer to memory queue.
*/
static void el1_interrupt(dpeth_t * dep)
{
u16_t csr, isr;
int pktsize;
buff_t *rxptr;
csr = inb_el1(dep, EL1_CSR);
if ((csr & ECSR_XMIT) && (dep->de_flags & DEF_XMIT_BUSY)) {
/* Got a transmit interrupt */
isr = inb_el1(dep, EL1_XMIT);
if ((isr & (EXSR_16JAM | EXSR_UNDER | EXSR_JAM)) || !(isr & EXSR_IDLE)) {
DEBUG(printf("3c501: got xmit interrupt (ASR=0x%02X XSR=0x%02X)\n", csr, isr));
if (isr & EXSR_JAM) {
/* Sending, packet got a collision */
dep->de_stat.ets_collision += 1;
/* Put pointer back to beginning of packet */
outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_SYS);
outw_el1(dep, EL1_XMITPTR, (EL1_BFRSIZ - TxBuff->size));
/* And retrigger transmission */
outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_XMIT);
return;
} else if ((isr & EXSR_16JAM) || !(isr & EXSR_IDLE)) {
dep->de_stat.ets_sendErr += 1;
} else if (isr & EXSR_UNDER) {
dep->de_stat.ets_fifoUnder += 1;
}
DEBUG(printf("3c501: got xmit interrupt (0x%02X)\n", isr));
el1_reset(dep);
} else {
/** if (inw_el1(dep, EL1_XMITPTR) == EL1_BFRSIZ) **/
/* Packet transmitted successfully */
dep->de_stat.ets_packetT += 1;
dep->bytes_Tx += (long) (TxBuff->size);
free_buff(dep, TxBuff);
dep->de_flags &= NOT(DEF_XMIT_BUSY);
if ((dep->de_flags & DEF_SENDING) && dep->de_xmitq_head) {
/* Pending transmit request available in queue */
el1_send(dep, TRUE, 0);
if (dep->de_flags & (DEF_XMIT_BUSY | DEF_ACK_SEND))
return;
}
}
} else if ((csr & (ECSR_RECV | ECSR_XMTBSY)) == (ECSR_RECV | ECSR_XMTBSY)) {
/* Got a receive interrupt */
isr = inb_el1(dep, EL1_RECV);
pktsize = inw_el1(dep, EL1_RECVPTR);
if ((isr & ERSR_RERROR) || (isr & ERSR_STALE)) {
DEBUG(printf("Rx0 (ASR=0x%02X RSR=0x%02X size=%d)\n", csr, isr, pktsize));
dep->de_stat.ets_recvErr += 1;
} else if (pktsize < ETH_MIN_PACK_SIZE || pktsize > ETH_MAX_PACK_SIZE) {
DEBUG(printf("Rx1 (ASR=0x%02X RSR=0x%02X size=%d)\n", csr, isr, pktsize));
dep->de_stat.ets_recvErr += 1;
} else if ((rxptr = alloc_buff(dep, pktsize + sizeof(buff_t))) == NULL) {
/* Memory not available. Drop packet */
dep->de_stat.ets_fifoOver += 1;
} else if (isr & (ERSR_GOOD | ERSR_ANY)) {
/* Got a good packet. Read it from buffer */
outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_SYS);
outw_el1(dep, EL1_XMITPTR, 0);
insb(dep->de_data_port, SELF, rxptr->buffer, pktsize);
rxptr->next = NULL;
rxptr->size = pktsize;
dep->de_stat.ets_packetR += 1;
dep->bytes_Rx += (long) pktsize;
lock(); /* Queue packet to receive queue */
96EA
if (dep->de_recvq_head == NULL)
dep->de_recvq_head = rxptr;
else
dep->de_recvq_tail->next = rxptr;
dep->de_recvq_tail = rxptr;
unlock();
/* Reply to pending Receive requests, if any */
el1_recv(dep, TRUE, 0);
}
} else { /* Nasty condition, should never happen */
DEBUG(
printf("3c501: got interrupt with status 0x%02X\n"
" de_flags=0x%04X XSR=0x%02X RSR=0x%02X \n"
" xmit buffer = 0x%4X recv buffer = 0x%4X\n",
csr, dep->de_flags,
inb_el1(dep, EL1_RECV),
inb_el1(dep, EL1_XMIT),
inw_el1(dep, EL1_XMITPTR),
inw_el1(dep, EL1_RECVPTR))
);
el1_reset(dep);
}
/* Move into receive mode */
outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_RECV);
outw_el1(dep, EL1_RECVPTR, 0);
/* Be sure that interrupts are cleared */
inb_el1(dep, EL1_RECV);
inb_el1(dep, EL1_XMIT);
return;
}
/*
** Name: void el1_init(dpeth_t *dep)
** Function: Initalizes board hardware and driver data structures.
*/
static void el1_init(dpeth_t * dep)
{
int ix;
dep->de_irq &= NOT(DEI_DEFAULT); /* Strip the default flag. */
dep->de_offset_page = 0;
dep->de_data_port = dep->de_base_port + EL1_DATAPORT;
el1_reset(dep); /* Reset and initialize board */
/* Start receiver (default mode) */
outw_el1(dep, EL1_RECVPTR, 0);
outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_RECV);
/* Initializes buffer pool */
init_buff(dep, NULL);
el1_mode_init(dep);
printf("%s: Etherlink (%s) at %X:%d - ",
dep->de_name, "3c501", dep->de_base_port, dep->de_irq);
for (ix = 0; ix < SA_ADDR_LEN; ix += 1)
printf("%02X%c", (dep->de_address.ea_addr[ix] = StationAddress[ix]),
ix < SA_ADDR_LEN - 1 ? ':' : '\n');
/* Device specific functions */
dep->de_recvf = el1_recv;
dep->de_sendf = el1_send;
dep->de_flagsf = el1_mode_init;
dep->de_resetf = el1_reset;
dep->de_getstatsf = el1_getstats;
dep->de_dumpstatsf = el1_dumpstats;
dep->de_interruptf = el1_interrupt;
return; /* Done */
}
/*
** Name: int el1_probe(dpeth_t *dep)
** Function: Checks for presence of the board.
*/
PUBLIC int el1_probe(dpeth_t * dep)
{
int ix;
for (ix = 0; ix < 8; ix += 1) /* Reset the board */
outb_el1(dep, EL1_CSR, ECSR_RESET);
outb_el1(dep, EL1_CSR, ECSR_SYS); /* Leaves buffer to system */
/* Check station address */
for (ix = 0; ix < SA_ADDR_LEN; ix += 1) {
outw_el1(dep, EL1_XMITPTR, ix);
StationAddress[ix] = inb_el1(dep, EL1_SAPROM);
}
if (StationAddress[0] != 0x02 || /* Etherlink Station address */
StationAddress[1] != 0x60 || /* MUST be 02:60:8c:xx:xx:xx */
StationAddress[2] != 0x8C)
return FALSE; /* No Etherlink board at this address */
dep->de_ramsize = 0; /* RAM size is meaningless */
dep->de_linmem = 0L; /* Access is via I/O port */
/* Device specific functions */
dep->de_initf = el1_init;
dep->de_stopf = el1_stop;
return TRUE; /* Etherlink board found */
}
#endif /* ENABLE_3C501 */
/** 3c501.c **/