summaryrefslogtreecommitdiff
path: root/sys/arch/vax/stand/boot/if_de.c
blob: 3dd71bb6aa2975cade610a5e5b6bdc7a8f650d3d (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
/*	$OpenBSD: if_de.c,v 1.3 2011/07/06 18:32:59 miod Exp $ */
/*	$NetBSD: if_de.c,v 1.2 2002/05/24 21:41:40 ragge Exp $	*/

/*
 * Copyright (c) 2000 Ludd, University of Lule}, Sweden. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed at Ludd, University of
 *      Lule}, Sweden and its contributors.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *	Standalone routine for the DEUNA Ethernet controller.
 */

#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>

#include <net/if.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/if_ether.h>

#include <lib/libsa/netif.h>
#include <lib/libsa/stand.h>

#include <arch/vax/if/if_dereg.h>

#define	DE_PCSR0	0
#define	DE_PCSR1	2
#define	DE_PCSR2	4
#define	DE_PCSR3	6

#include "arch/vax/include/sid.h"
#include "arch/vax/include/rpb.h"
#include "arch/vax/include/pte.h"

#include "vaxstand.h"

static int de_get(struct iodesc *, void *, size_t, time_t);
static int de_put(struct iodesc *, void *, size_t);
static void dewait(char *);

struct netif_driver de_driver = {
	0, 0, 0, 0, de_get, de_put,
};

#define NRCV 8		/* allocate 8 receive descriptors */
#define NXMT 4		/* and 4 transmit - must be >1 */

struct de_cdata {
	/* the following structures are always mapped in */
        struct  de_pcbb dc_pcbb;        /* port control block */
        struct  de_ring dc_xrent[NXMT]; /* transmit ring entries */
        struct  de_ring dc_rrent[NRCV]; /* receive ring entries */
        struct  de_udbbuf dc_udbbuf;    /* UNIBUS data buffer */
	char	dc_rbuf[NRCV][ETHER_MAX_LEN];
	char	dc_xbuf[NXMT][ETHER_MAX_LEN];
        /* end mapped area */
};

static volatile struct de_cdata *dc, *pdc;
static volatile char *addr;
static int crx, ctx;
#define DE_WCSR(csr, val) *(volatile u_short *)(addr + (csr)) = (val)
#define DE_WLOW(val) *(volatile u_char *)(addr + DE_PCSR0) = (val)
#define DE_WHIGH(val) *(volatile u_char *)(addr + DE_PCSR0 + 1) = (val)
#define DE_RCSR(csr) *(volatile u_short *)(addr + (csr))
#define LOWORD(x)       ((u_int)(x) & 0xffff)
#define HIWORD(x)       (((u_int)(x) >> 16) & 0x3)
#define	dereg(x)	((x) & 017777)

int
deopen(struct open_file *f, int adapt, int ctlr, int unit, int part)
{
	int i, cdata, *map, npgs;
	char eaddr[6];

	/* point to the device in memory */
	if (askname == 0) /* Override if autoboot */
		addr = (char *)bootrpb.csrphy;
	else {
		addr = (char *)csrbase + dereg(0174510);
		bootrpb.csrphy = (int)addr;
	}
#ifdef DEV_DEBUG
	printf("deopen: csrbase %x addr %p nexaddr %x\n", 
	    csrbase, addr, nexaddr);
#endif
	/* reset the device and wait for completion */
	DE_WCSR(DE_PCSR0, 0);
	{volatile int j = 100; while (--j);}
	DE_WCSR(DE_PCSR0, PCSR0_RSET);
	dewait("reset");

	/* Map in the control structures and buffers */
	dc = alloc(sizeof(struct de_cdata));
	(int)pdc = (int)dc & VAX_PGOFSET;
	map = (int *)nexaddr + 512;
	npgs = (sizeof(struct de_cdata) >> VAX_PGSHIFT) + 1;
	cdata = (int)dc >> VAX_PGSHIFT;
	for (i = 0; i < npgs; i++) {
		map[i] = PG_V | (cdata + i);
	}

	bzero((char *)dc, sizeof(struct de_cdata));
	
	/* Tell the DEUNA about our PCB */
	DE_WCSR(DE_PCSR2, LOWORD(pdc));
	DE_WCSR(DE_PCSR3, HIWORD(pdc));
	DE_WLOW(CMD_GETPCBB);
	dewait("pcbb");

	/* Get our address */
	dc->dc_pcbb.pcbb0 = FC_RDPHYAD;
	DE_WLOW(CMD_GETCMD);
	dewait("read physaddr");
	bcopy((char *)&dc->dc_pcbb.pcbb2, eaddr, 6);

	/* Create and link the descriptors */
	for (i=0; i < NRCV; i++) {
		volatile struct de_ring *rp = &dc->dc_rrent[i];

		rp->r_lenerr = 0;
		rp->r_segbl = LOWORD(&pdc->dc_rbuf[i][0]);
		rp->r_segbh = HIWORD(&pdc->dc_rbuf[i][0]);
		rp->r_flags = RFLG_OWN;
		rp->r_slen = ETHER_MAX_LEN;
	}
	for (i=0; i < NXMT; i++) {
		volatile struct de_ring *rp = &dc->dc_xrent[i];

		rp->r_segbl = LOWORD(&pdc->dc_xbuf[i][0]);
		rp->r_segbh = HIWORD(&pdc->dc_xbuf[i][0]);
		rp->r_tdrerr = 0;
		rp->r_flags = 0;
	}
	crx = ctx = 0;

	/* set the transmit and receive ring header addresses */
	dc->dc_pcbb.pcbb0 = FC_WTRING;
	dc->dc_pcbb.pcbb2 = LOWORD(&pdc->dc_udbbuf);
	dc->dc_pcbb.pcbb4 = HIWORD(&pdc->dc_udbbuf);

	dc->dc_udbbuf.b_tdrbl = LOWORD(&pdc->dc_xrent[0]);
	dc->dc_udbbuf.b_tdrbh = HIWORD(&pdc->dc_xrent[0]);
	dc->dc_udbbuf.b_telen = sizeof (struct de_ring) / sizeof(u_int16_t);
	dc->dc_udbbuf.b_trlen = NXMT;
	dc->dc_udbbuf.b_rdrbl = LOWORD(&pdc->dc_rrent[0]);
	dc->dc_udbbuf.b_rdrbh = HIWORD(&pdc->dc_rrent[0]);
	dc->dc_udbbuf.b_relen = sizeof (struct de_ring) / sizeof(u_int16_t);
	dc->dc_udbbuf.b_rrlen = NRCV;

	DE_WLOW(CMD_GETCMD);
	dewait("wtring");

	dc->dc_pcbb.pcbb0 = FC_WTMODE;
	dc->dc_pcbb.pcbb2 = MOD_DRDC|MOD_TPAD|MOD_HDX;
	DE_WLOW(CMD_GETCMD);
	dewait("wtmode");

	DE_WLOW(CMD_START);
	dewait("start");

	DE_WLOW(CMD_PDMD);
	dewait("initpoll");
	/* Should be running by now */

	net_devinit(f, &de_driver, eaddr);

	return 0;
}

int
de_get(struct iodesc *desc, void *pkt, size_t maxlen, time_t timeout)
{
	volatile int to = 100000 * timeout;
	int len, csr0;

	if ((csr0 = DE_RCSR(DE_PCSR0)) & PCSR0_INTR)
		DE_WHIGH(csr0 >> 8);
retry:
	if (to-- == 0)
		return 0;

	if (dc->dc_rrent[crx].r_flags & RFLG_OWN)
		goto retry;

	if (dc->dc_rrent[crx].r_flags & RFLG_ERRS)
		len = 0;
	else
		len = dc->dc_rrent[crx].r_lenerr & RERR_MLEN;

	if (len > maxlen)
		len = maxlen;
	if (len)
		bcopy((char *)&dc->dc_rbuf[crx][0], pkt, len);

	dc->dc_rrent[crx].r_flags = RFLG_OWN;
	dc->dc_rrent[crx].r_lenerr = 0;
#ifdef DEV_DEBUG
	printf("Got packet: len %d idx %d maxlen %ld\n", len, crx, maxlen);
#endif
	if (++crx == NRCV)
		crx = 0;

	if (len == 0)
		goto retry;
	return len;
}


int
de_put(struct iodesc *desc, void *pkt, size_t len)
{
	volatile int to = 100000;
	int csr0;

	if ((csr0 = DE_RCSR(DE_PCSR0)) & PCSR0_INTR)
		DE_WHIGH(csr0 >> 8);
#ifdef DEV_DEBUG
	printf("de_put: len %ld\n", len);
#endif
retry:
	if (to-- == 0)
		return -1;

	if (dc->dc_xrent[ctx].r_flags & RFLG_OWN)
		goto retry;

	bcopy(pkt, (char *)&dc->dc_xbuf[ctx][0], len);

	dc->dc_xrent[ctx].r_slen = len;
	dc->dc_xrent[ctx].r_tdrerr = 0;
	dc->dc_xrent[ctx].r_flags = XFLG_OWN|XFLG_STP|XFLG_ENP;

	DE_WLOW(CMD_PDMD);
	dewait("start");

	if (++ctx == NXMT)
		ctx = 0;
	return len;
}

int
declose(struct open_file *f)
{
	DE_WCSR(DE_PCSR0, PCSR0_RSET);
	dewait("close");
	return 0;
}

void
dewait(char *fn)
{
	int csr0;

#ifdef DEV_DEBUG
	printf("dewait: %s...", fn);
#endif
	while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
		;
	csr0 = DE_RCSR(DE_PCSR0);
	DE_WHIGH(csr0 >> 8);
#ifdef DEV_DEBUG
	if (csr0 & PCSR0_PCEI)
		printf("failed! CSR0 %x", csr0);
	else
		printf("done");
	printf(", PCSR1 %x\n", DE_RCSR(DE_PCSR1));
#endif
}