summaryrefslogtreecommitdiff
path: root/minix/drivers/net/dpeth/3c501.c
blob: df2d881f1908741caf0382e9baa51f1338e577f1 (plain)
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
/*
**  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.
*/

#include <minix/drivers.h>
#include <minix/netdriver.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:	el1_getstats
**  Function:	Reads statistics counters from board.
**/
static void el1_getstats(dpeth_t * dep)
{

  /* Nothing to do */
}

/*
**  Name:	el1_reset
**  Function:	Reset function specific for Etherlink hardware.
*/
static void el1_reset(dpeth_t * dep)
{
  unsigned 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]);

  /* 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);
}

/*
**  Name:	el1_dumpstats
**  Function:	Dumps counter on screen (support for console display).
*/
static void el1_dumpstats(dpeth_t * UNUSED(dep))
{

}

/*
**  Name:	el1_mode_init
**  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);
}

/*
**  Name:	el1_recv
**  Function:	Receive function.  Called from interrupt handler to
**  		unload recv. buffer or from main (packet to client)
*/
static ssize_t el1_recv(dpeth_t *dep, struct netdriver_data *data, size_t max)
{
  buff_t *rxptr;
  size_t size;

  if ((rxptr = dep->de_recvq_head) == NULL)
	return SUSPEND;

  /* Remove buffer from queue and free buffer */
  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;

  /* Copy buffer to user area */
  size = MIN((size_t)rxptr->size, max);

  netdriver_copyout(data, 0, rxptr->buffer, size);

  /* Return buffer to the idle pool */
  free_buff(dep, rxptr);

  return size;
}

/*
**  Name:	el1_send
**  Function:	Send function.
*/
static int el1_send(dpeth_t *dep, struct netdriver_data *data, size_t size)
{
  buff_t *txbuff;
  clock_t now;

  if (dep->de_flags & DEF_XMIT_BUSY) {
	now = getticks();
	if ((now - dep->de_xmit_start) > 4) {
		/* Transmitter timed out */
		DEBUG(printf("3c501: transmitter timed out ... \n"));
		netdriver_stat_oerror(1);
		dep->de_flags &= NOT(DEF_XMIT_BUSY);
		/* Try sending anyway. */
	} else
		return SUSPEND;
  }

  /* Since we may have to retransmit, we need a local copy. */
  if ((txbuff = alloc_buff(dep, size + sizeof(buff_t))) == NULL)
	panic("out of memory");

  /* Fill transmit buffer from user area */
  txbuff->next = NULL;
  txbuff->size = size;

  netdriver_copyin(data, 0, txbuff->buffer, size);

  /* Save for retransmission */
  TxBuff = txbuff;
  dep->de_flags |= DEF_XMIT_BUSY;

  /* Setup board for packet loading */
  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 - size));
  outsb(dep->de_data_port, txbuff->buffer, size);
  /* Starts transmitter */
  outw_el1(dep, EL1_XMITPTR, (EL1_BFRSIZ - size));
  outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_XMIT);	/* There it goes... */

  dep->de_xmit_start = getticks();

  return OK;
}

/*
**  Name:	el1_stop
**  Function:	Stops board and disable interrupts.
*/
static void el1_stop(dpeth_t * dep)
{
  int ix;

  DEBUG(printf("%s: stopping Etherlink ....\n", netdriver_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 */
}

/*
**  Name:	el1_interrupt
**  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 */
			netdriver_stat_coll(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)) {
			netdriver_stat_oerror(1);
		} else if (isr & EXSR_UNDER) {
			netdriver_stat_oerror(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->bytes_Tx += (long) (TxBuff->size);
		free_buff(dep, TxBuff);
		dep->de_flags &= NOT(DEF_XMIT_BUSY);
		netdriver_send();
		if (dep->de_flags & DEF_XMIT_BUSY)
			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));
		netdriver_stat_ierror(1);

	} else if (pktsize < NDEV_ETH_PACKET_MIN ||
	    pktsize > NDEV_ETH_PACKET_MAX) {
		DEBUG(printf("Rx1 (ASR=0x%02X RSR=0x%02X size=%d)\n",
		    csr, isr, pktsize));
		netdriver_stat_ierror(1);

	} else if ((rxptr = alloc_buff(dep, pktsize + sizeof(buff_t))) == NULL) {
		/* Memory not available. Drop packet */
		netdriver_stat_ierror(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, rxptr->buffer, pktsize);
		rxptr->next = NULL;
		rxptr->size = pktsize;
		dep->bytes_Rx += (long) pktsize;
		/* Queue packet to receive queue */
		if (dep->de_recvq_head == NULL)
			dep->de_recvq_head = rxptr;
		else
			dep->de_recvq_tail->next = rxptr;
		dep->de_recvq_tail = rxptr;

		/* Reply to pending Receive requests, if any */
		netdriver_recv();
	}
  } 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);
}

/*
**  Name:	el1_init
**  Function:	Initalizes board hardware and driver data structures.
*/
static void el1_init(dpeth_t * dep)
{
  unsigned 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 - ",
         netdriver_name(), "3c501", dep->de_base_port, dep->de_irq);
  for (ix = 0; ix < SA_ADDR_LEN; ix += 1)
	printf("%02X%c", (dep->de_address.na_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;
}

/*
**  Name:	el1_probe
**  Function:	Checks for presence of the board.
*/
int el1_probe(dpeth_t * dep)
{
  unsigned 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 **/