summaryrefslogtreecommitdiff
path: root/sys/arch/amiga/dev/zz9k_if.c
blob: ff4aee82cdf2cf82bbe1cdb688c027dcf637fa72 (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
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
/*	$NetBSD: zz9k_if.c,v 1.1 2023/05/03 13:49:30 phx Exp $ */

/*
 * Copyright (c) 2020 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Alain Runa.
 *
 * 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.
 *
 * 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: zz9k_if.c,v 1.1 2023/05/03 13:49:30 phx Exp $");

/* miscellaneous */
#include <sys/types.h>			/* size_t */
#include <sys/stdint.h>			/* uintXX_t */
#include <sys/stdbool.h>		/* bool */
#include <sys/syslog.h>			/* log(), LOG_ERR */

/* driver(9) */
#include <sys/param.h>			/* NODEV */
#include <sys/device.h>			/* CFATTACH_DECL_NEW(), device_priv() */
#include <sys/errno.h>			/* EINVAL, ENODEV, EPASSTHROUGH */

/* bus_space(9) and zorro bus */
#include <sys/bus.h>			/* bus_space_xxx(), bus_space_xxx_t */
#include <sys/cpu.h>			/* kvtop() */
#include <sys/systm.h>			/* aprint_xxx(), memcpy() */
#include <amiga/dev/zbusvar.h>		/* zbus_args */

/* arp(9) and mbuf(9) */
#include <net/if.h>			/* if_oerrors */
#include <net/if_ether.h>		/* ethercom, ifnet, ether_ifattach() */
#include <net/if_dl.h>			/* satosdl(), sockaddr_dl, dl_addr */
#include <net/bpf.h>			/* bpf_mtap(), BPF_D_OUT */
#include <netinet/if_inarp.h>		/* arp_ifinit() */
#include <sys/mbuf.h>			/* mbuf_xxx */
#include <sys/sockio.h>			/* SIOXXX */

/* Interrupt related */
#include <sys/intr.h>			/* splvm(), splx() */ 
#include <amiga/amiga/isr.h>		/* isr */

/* zz9k related */
#include <amiga/dev/zz9kvar.h>		/* zz9kbus_attach_args */
#include <amiga/dev/zz9kreg.h>		/* ZZ9000 registers */
#include "zz9k_if.h"			/* NZZ9K_IF */


/* The allmighty softc structure */
struct zzif_softc {
	device_t sc_dev;
	struct bus_space_tag sc_bst;
	bus_space_tag_t sc_iot;
	bus_space_handle_t sc_regh;
	bus_space_handle_t sc_rxh;
	bus_space_handle_t sc_txh;
	
	struct ethercom sc_ethercom;
	struct isr sc_isr;
	void* sc_txbuffer;
	void* sc_rxbuffer;
	uint16_t sc_sequence;
};

/* rx buffer contents */
struct zzif_frame {
	uint16_t size;
	uint16_t serial;
	struct ether_header header;
	uint8_t payload[ETHER_MAX_LEN - ETHER_HDR_LEN];
};


/* ifnet related callbacks */
static void zzif_init(struct zzif_softc *sc);
static void zzif_stop(struct zzif_softc *sc);
static void zzif_start(struct ifnet *ifp);
static int zzif_intr(void *arg);
static int zzif_ioctl(struct ifnet *ifp, u_long cmd, void *data);

/* driver(9) essentials */
static int zzif_match(device_t parent, cfdata_t match, void *aux);
static void zzif_attach(device_t parent, device_t self, void *aux);
CFATTACH_DECL_NEW(
    zz9k_if, sizeof(struct zzif_softc), zzif_match, zzif_attach, NULL, NULL);


/* Oh my God, it's full of stars! */

static int
zzif_match(device_t parent, cfdata_t match, void *aux)
{
	struct zz9kbus_attach_args *bap = aux;

	if (strcmp(bap->zzaa_name, "zz9k_if") != 0)
		return 0;

	return 1;
}

static void
zzif_attach(device_t parent, device_t self, void *aux)
{
	struct zz9kbus_attach_args *bap = aux;
	struct zzif_softc *sc = device_private(self);
	struct zz9k_softc *psc = device_private(parent);
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	uint8_t lla[ETHER_ADDR_LEN];

	sc->sc_dev = self;
	sc->sc_bst.base = bap->zzaa_base;
	sc->sc_bst.absm = &amiga_bus_stride_1;
	sc->sc_iot = &sc->sc_bst;
	sc->sc_regh = psc->sc_regh;

	if (bus_space_map(sc->sc_iot, ZZ9K_RX_BASE, ZZ9K_RX_SIZE,
	    BUS_SPACE_MAP_LINEAR, &sc->sc_rxh)) {
		aprint_error(": Failed to map MNT ZZ9000 eth rx buffer.\n");
		return;
	}

	if (bus_space_map(sc->sc_iot, ZZ9K_TX_BASE, ZZ9K_TX_SIZE,
	    BUS_SPACE_MAP_LINEAR, &sc->sc_txh)) {
		aprint_error(": Failed to map MNT ZZ9000 eth tx buffer.\n");
		return;
	}

	/* get MAC from NIC */
	uint16_t macHI = ZZREG_R(ZZ9K_ETH_MAC_HI);
	uint16_t macMD = ZZREG_R(ZZ9K_ETH_MAC_MD);
	uint16_t macLO = ZZREG_R(ZZ9K_ETH_MAC_LO);
	lla[0] = macHI >> 8;
	lla[1] = macHI & 0xff;
	lla[2] = macMD >> 8;
	lla[3] = macMD & 0xff;
	lla[4] = macLO >> 8;
	lla[5] = macLO & 0xff;

#if 0
	aprint_normal(": Ethernet address %s  "
	    "(10BASE-T, 100BASE-TX, AUTO)\n", ether_sprintf(lla));
#endif

	aprint_debug_dev(sc->sc_dev, "[DEBUG] registers at %p/%p (pa/va), "
	    "rx buffer at %p/%p (pa/va), tx buffer at %p/%p (pa/va)\n",
	    (void *)kvtop((void *)sc->sc_regh),
	    bus_space_vaddr(sc->sc_iot, sc->sc_regh),
	    (void *)kvtop((void *)sc->sc_rxh),
	    bus_space_vaddr(sc->sc_iot, sc->sc_rxh),
	    (void *)kvtop((void *)sc->sc_txh),
	    bus_space_vaddr(sc->sc_iot, sc->sc_txh));

	/* set rx/tx buffers */
	sc->sc_txbuffer = bus_space_vaddr(sc->sc_iot, sc->sc_txh);
	sc->sc_rxbuffer = bus_space_vaddr(sc->sc_iot, sc->sc_rxh);

	/* configure the network interface */
	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
	ifp->if_softc = sc;
	ifp->if_ioctl = zzif_ioctl;
	ifp->if_start = zzif_start;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
	ifp->if_mtu   = ETHERMTU;

	/* attach the network interface. */
	if_attach(ifp);
	if_deferred_start_init(ifp, NULL);
	ether_ifattach(ifp, lla);

	aprint_normal(": Ethernet address %s\n", ether_sprintf(lla));

	/* setup the (receiving) interrupt service routine on int6 (default) */
	sc->sc_isr.isr_intr = zzif_intr;
	sc->sc_isr.isr_arg  = sc;
	sc->sc_isr.isr_ipl  = 6;
	add_isr(&sc->sc_isr);
}

static void
zzif_init(struct zzif_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	int s = splvm();

	ZZREG_W(ZZ9K_CONFIG, ZZREG_R(ZZ9K_CONFIG) | ZZ9K_CONFIG_INT_ETH);
	ifp->if_flags |= IFF_RUNNING;
	ifp->if_flags &= ~IFF_OACTIVE;
	if_schedule_deferred_start(ifp);
	splx(s);
}

static void
zzif_stop(struct zzif_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	int s = splvm();

	ZZREG_W(ZZ9K_CONFIG, ZZREG_R(ZZ9K_CONFIG) & ~ZZ9K_CONFIG_INT_ETH);
	ifp->if_flags &= ~IFF_RUNNING;
	splx(s);
}

static void
zzif_start(struct ifnet *ifp)
{
	struct zzif_softc *sc = ifp->if_softc;
	uint8_t *frame= (uint8_t *)sc->sc_txbuffer;
	struct mbuf *m;
	int size;

	if ((ifp->if_flags & IFF_RUNNING) == 0)
		return;

	ifp->if_flags |= IFF_OACTIVE;

	for (;;) {
		IF_DEQUEUE(&ifp->if_snd, m);
		if (m == NULL)
			break;

		/* this should never happen, otherwise there's no help */
		if ((m->m_flags & M_PKTHDR) == 0)
			panic("zzif_start: no packet header mbuf");
		
		size = m->m_pkthdr.len;
		if (size == 0)
			panic("zzif_start: header mbuf with 0 len");

		if ((size < (ETHER_HDR_LEN)) ||
		    (size > (ETHER_MAX_LEN-ETHER_CRC_LEN))) {
			aprint_error_dev(sc->sc_dev,
			    ": abnormal tx frame size of %i bytes\n", size);
			m_freem(m);
			break;
		}

		/* make bpf happy */
		bpf_mtap(ifp, m, BPF_D_OUT);
		
		/* copy dequeued mbuf data to tranmit buffer of the ZZ9000 */
		for (struct mbuf *n = m; n != NULL; n = n->m_next) {
			memcpy(frame, n->m_data, n->m_len);
			frame += n->m_len;
		}

		m_freem(m);	/* we don't need it anymore */

		/* tell ZZ9000 to transmit packet with size and check result */ 
		ZZREG_W(ZZ9K_ETH_TX, size);
		size = ZZREG_R(ZZ9K_ETH_TX);
		if (size == 0) {
			if_statinc(ifp, if_opackets);
		} else {
			if_statinc(ifp, if_oerrors);
		}
	}

	ifp->if_flags &= ~IFF_OACTIVE;
}

static int
zzif_intr(void *arg)
{
	struct zzif_softc *sc = arg;

	uint16_t conf = ZZREG_R(ZZ9K_CONFIG);
	if ((conf & ZZ9K_CONFIG_INT_ETH) == 0)
		return 0;

	ZZREG_W(ZZ9K_CONFIG, conf & ~ZZ9K_CONFIG_INT_ETH);  /* disable INT */
	ZZREG_W(ZZ9K_CONFIG, ZZ9K_CONFIG_INT_ACK | ZZ9K_CONFIG_INT_ACK_ETH);

	struct zzif_frame *frame = (struct zzif_frame *)sc->sc_rxbuffer;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mbuf *m;

	/* check if interrupt was due to new packet arrival */
	if (frame->serial == sc->sc_sequence) {
		ZZREG_W(ZZ9K_ETH_RX, 1);	/* discard packet */
		ZZREG_W(ZZ9K_CONFIG, conf);	/* restore interrupt */
		return 0;	/* nope, something else triggered it */
	}

	sc->sc_sequence = frame->serial;

	if ((frame->size < (ETHER_MIN_LEN-ETHER_CRC_LEN)) ||
	    (frame->size > ETHER_MAX_LEN)) {
		aprint_error_dev(sc->sc_dev,
		    ": abnormal rx frame size of %i bytes\n", frame->size);
		ZZREG_W(ZZ9K_ETH_RX, 1);	/* discard packet */
		ZZREG_W(ZZ9K_CONFIG, conf);	/* restore interrupt */
		return 0;
	}

	int s = splvm();
	m = m_devget((char *)&frame->header, frame->size, 0, ifp);
	if_percpuq_enqueue(ifp->if_percpuq, m);
	splx(s);
	ZZREG_W(ZZ9K_ETH_RX, 1);	/* packet served */
	ZZREG_W(ZZ9K_CONFIG, conf);	/* restore interrupt */

	return 1;
}

static int
zzif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
	struct zzif_softc *sc = ifp->if_softc;
	struct ifaddr *ifa;
	struct if_laddrreq *lar;
	const struct sockaddr_dl *sdl;
	int retval = 0;

	int s = splvm();

	switch (cmd) {
	case SIOCINITIFADDR:
		ifa = (struct ifaddr *)data;
		zzif_stop(sc);
		zzif_init(sc);
		switch (ifa->ifa_addr->sa_family) {
		case AF_INET:
			arp_ifinit(ifp, ifa);
			break;
		default:
			break;
		}
		break;
	case SIOCSIFFLAGS:
		retval = ifioctl_common(ifp, cmd, data);
		if (retval != 0)
			break;

		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
		case IFF_RUNNING: /* and not UP */
			zzif_stop(sc);
			break;
		case IFF_UP: /* and not RUNNING */
			zzif_init(sc);
			break;
		case (IFF_UP | IFF_RUNNING):
			zzif_stop(sc);
			zzif_init(sc);
			break;
		default:
			break;
		}
		break;
	case SIOCADDMULTI:
	case SIOCDELMULTI:
		retval = EINVAL;
		break;
	case SIOCALIFADDR:
		lar = (struct if_laddrreq *)data;
		sdl = satocsdl(&lar->addr);
		uint8_t *mac = (uint8_t *)&sdl->sdl_addr.dl_data;

		if ((lar->flags==IFLR_ACTIVE) && (sdl->sdl_family==AF_LINK)) {
			ZZREG_W(ZZ9K_ETH_MAC_HI, (mac[0] << 8) | mac[1]);
			ZZREG_W(ZZ9K_ETH_MAC_MD, (mac[2] << 8) | mac[3]);
			ZZREG_W(ZZ9K_ETH_MAC_LO, (mac[4] << 8) | mac[5]);
		}
		aprint_normal(": Ethernet address %s", ether_sprintf(mac));
		retval = ether_ioctl(ifp, cmd, data);
		break;
	default:
		retval = ether_ioctl(ifp, cmd, data);
		break;
	}

	splx(s);
	
	return retval;
}