xref: /netbsd-src/sys/dev/ofw/ofnet.c (revision 72f872d2b2a4bb046ed63d418bbcd0538033d5a6)
1*72f872d2Sthorpej /*	$NetBSD: ofnet.c,v 1.63 2020/01/29 06:18:17 thorpej Exp $	*/
25804d3f6Sws 
35804d3f6Sws /*
45804d3f6Sws  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
55804d3f6Sws  * Copyright (C) 1995, 1996 TooLs GmbH.
65804d3f6Sws  * All rights reserved.
75804d3f6Sws  *
85804d3f6Sws  * Redistribution and use in source and binary forms, with or without
95804d3f6Sws  * modification, are permitted provided that the following conditions
105804d3f6Sws  * are met:
115804d3f6Sws  * 1. Redistributions of source code must retain the above copyright
125804d3f6Sws  *    notice, this list of conditions and the following disclaimer.
135804d3f6Sws  * 2. Redistributions in binary form must reproduce the above copyright
145804d3f6Sws  *    notice, this list of conditions and the following disclaimer in the
155804d3f6Sws  *    documentation and/or other materials provided with the distribution.
165804d3f6Sws  * 3. All advertising materials mentioning features or use of this software
175804d3f6Sws  *    must display the following acknowledgement:
185804d3f6Sws  *	This product includes software developed by TooLs GmbH.
195804d3f6Sws  * 4. The name of TooLs GmbH may not be used to endorse or promote products
205804d3f6Sws  *    derived from this software without specific prior written permission.
215804d3f6Sws  *
225804d3f6Sws  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
235804d3f6Sws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
245804d3f6Sws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
255804d3f6Sws  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265804d3f6Sws  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
275804d3f6Sws  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
285804d3f6Sws  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
295804d3f6Sws  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
305804d3f6Sws  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
315804d3f6Sws  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325804d3f6Sws  */
33ab5d9d2bSlukem 
34ab5d9d2bSlukem #include <sys/cdefs.h>
35*72f872d2Sthorpej __KERNEL_RCSID(0, "$NetBSD: ofnet.c,v 1.63 2020/01/29 06:18:17 thorpej Exp $");
36ab5d9d2bSlukem 
375804d3f6Sws #include "ofnet.h"
383751946bSjonathan #include "opt_inet.h"
395804d3f6Sws 
405804d3f6Sws #include <sys/param.h>
41fe227898Stv #include <sys/systm.h>
42fc96443dSthorpej #include <sys/callout.h>
435804d3f6Sws #include <sys/device.h>
445911b9a6Smatt #include <sys/disk.h>
455804d3f6Sws #include <sys/ioctl.h>
465804d3f6Sws #include <sys/mbuf.h>
475804d3f6Sws #include <sys/socket.h>
485804d3f6Sws #include <sys/syslog.h>
495804d3f6Sws 
505804d3f6Sws #include <net/if.h>
5107b064e0Sis #include <net/if_ether.h>
524b508fb1Smsaitoh #include <net/bpf.h>
535804d3f6Sws 
545804d3f6Sws #ifdef INET
555804d3f6Sws #include <netinet/in.h>
5607b064e0Sis #include <netinet/if_inarp.h>
575804d3f6Sws #endif
585804d3f6Sws 
595804d3f6Sws #include <dev/ofw/openfirm.h>
605804d3f6Sws 
61310f6fefSmycroft struct ofnet_softc {
629af887f0Smrg 	device_t sc_dev;
635804d3f6Sws 	int sc_phandle;
645804d3f6Sws 	int sc_ihandle;
6507b064e0Sis 	struct ethercom sc_ethercom;
66fc96443dSthorpej 	struct callout sc_callout;
675804d3f6Sws };
685804d3f6Sws 
697cf29912Scegger static int ofnet_match (device_t, cfdata_t, void *);
707cf29912Scegger static void ofnet_attach (device_t, device_t, void *);
715804d3f6Sws 
729af887f0Smrg CFATTACH_DECL_NEW(ofnet, sizeof(struct ofnet_softc),
73674c37a0Sthorpej     ofnet_match, ofnet_attach, NULL, NULL);
745804d3f6Sws 
755911b9a6Smatt static void ofnet_read (struct ofnet_softc *);
765911b9a6Smatt static void ofnet_timer (void *);
775911b9a6Smatt static void ofnet_init (struct ofnet_softc *);
785911b9a6Smatt static void ofnet_stop (struct ofnet_softc *);
795804d3f6Sws 
805911b9a6Smatt static void ofnet_start (struct ifnet *);
8153524e44Schristos static int ofnet_ioctl (struct ifnet *, u_long, void *);
825911b9a6Smatt static void ofnet_watchdog (struct ifnet *);
835804d3f6Sws 
845804d3f6Sws static int
ofnet_match(device_t parent,cfdata_t match,void * aux)857cf29912Scegger ofnet_match(device_t parent, cfdata_t match, void *aux)
865804d3f6Sws {
87310f6fefSmycroft 	struct ofbus_attach_args *oba = aux;
885804d3f6Sws 	char type[32];
895804d3f6Sws 	int l;
905804d3f6Sws 
91310f6fefSmycroft 	if (strcmp(oba->oba_busname, "ofw"))
92310f6fefSmycroft 		return (0);
93310f6fefSmycroft 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
94310f6fefSmycroft 	    sizeof type - 1)) < 0)
955804d3f6Sws 		return 0;
965804d3f6Sws 	if (l >= sizeof type)
975804d3f6Sws 		return 0;
985804d3f6Sws 	type[l] = 0;
995804d3f6Sws 	if (strcmp(type, "network"))
1005804d3f6Sws 		return 0;
1015804d3f6Sws 	return 1;
1025804d3f6Sws }
1035804d3f6Sws 
1045804d3f6Sws static void
ofnet_attach(device_t parent,device_t self,void * aux)1057cf29912Scegger ofnet_attach(device_t parent, device_t self, void *aux)
1065804d3f6Sws {
107838ee1e0Sthorpej 	struct ofnet_softc *of = device_private(self);
10807b064e0Sis 	struct ifnet *ifp = &of->sc_ethercom.ec_if;
109310f6fefSmycroft 	struct ofbus_attach_args *oba = aux;
1105804d3f6Sws 	char path[256];
1115804d3f6Sws 	int l;
11207b064e0Sis 	u_int8_t myaddr[ETHER_ADDR_LEN];
1135804d3f6Sws 
1149af887f0Smrg 	of->sc_dev = self;
1159af887f0Smrg 
116310f6fefSmycroft 	of->sc_phandle = oba->oba_phandle;
11794924a74Smaxv 
118310f6fefSmycroft 	if ((l = OF_package_to_path(oba->oba_phandle, path,
119310f6fefSmycroft 	    sizeof path - 1)) < 0 ||
120310f6fefSmycroft 	    l >= sizeof path ||
121310f6fefSmycroft 	    (path[l] = 0, !(of->sc_ihandle = OF_open(path))))
122310f6fefSmycroft 		panic("ofnet_attach: unable to open");
123310f6fefSmycroft 	if (OF_getprop(oba->oba_phandle, "mac-address", myaddr,
124310f6fefSmycroft 	    sizeof myaddr) < 0)
125310f6fefSmycroft 		panic("ofnet_attach: no mac-address");
12607b064e0Sis 	printf(": address %s\n", ether_sprintf(myaddr));
1275804d3f6Sws 
12888ab7da9Sad 	callout_init(&of->sc_callout, 0);
129fc96443dSthorpej 
1309af887f0Smrg 	strlcpy(ifp->if_xname, device_xname(of->sc_dev), IFNAMSIZ);
1315804d3f6Sws 	ifp->if_softc = of;
132310f6fefSmycroft 	ifp->if_start = ofnet_start;
133310f6fefSmycroft 	ifp->if_ioctl = ofnet_ioctl;
134310f6fefSmycroft 	ifp->if_watchdog = ofnet_watchdog;
135091e1526Smsaitoh 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
136ac36f7cbSitojun 	IFQ_SET_READY(&ifp->if_snd);
1375804d3f6Sws 
1385804d3f6Sws 	if_attach(ifp);
13907b064e0Sis 	ether_ifattach(ifp, myaddr);
1405804d3f6Sws }
1415804d3f6Sws 
14222044e51Sthorpej static char buf[ETHER_MAX_LEN];
1435804d3f6Sws 
1445804d3f6Sws static void
ofnet_read(struct ofnet_softc * of)1455911b9a6Smatt ofnet_read(struct ofnet_softc *of)
1465804d3f6Sws {
14707b064e0Sis 	struct ifnet *ifp = &of->sc_ethercom.ec_if;
1485804d3f6Sws 	struct mbuf *m, **mp, *head;
149f5191984Schs 	int s, l, len;
1505804d3f6Sws 	char *bufp;
1515804d3f6Sws 
152f5191984Schs 	s = splnet();
15394924a74Smaxv 
154f5191984Schs 	for (;;) {
155cb1254e9Schs 		len = OF_read(of->sc_ihandle, buf, sizeof buf);
156d8615e4fSmycroft 		if (len == -2 || len == 0)
157f5191984Schs 			break;
1585804d3f6Sws 		if (len < sizeof(struct ether_header)) {
159*72f872d2Sthorpej 			if_statinc(ifp, if_ierrors);
1605804d3f6Sws 			continue;
1615804d3f6Sws 		}
1625804d3f6Sws 		bufp = buf;
1635804d3f6Sws 
1640b357f29Sbillc 		/*
1650b357f29Sbillc 		 * We don't know if the interface included the FCS
1660b357f29Sbillc 		 * or not.  For now, assume that it did if we got
1670b357f29Sbillc 		 * a packet length that looks like it could include
1680b357f29Sbillc 		 * the FCS.
1690b357f29Sbillc 		 *
1700b357f29Sbillc 		 * XXX Yuck.
1710b357f29Sbillc 		 */
17222044e51Sthorpej 		if (len > ETHER_MAX_LEN - ETHER_CRC_LEN)
17322044e51Sthorpej 			len = ETHER_MAX_LEN - ETHER_CRC_LEN;
1740b357f29Sbillc 
17522044e51Sthorpej 		/* Allocate a header mbuf */
17622044e51Sthorpej 		MGETHDR(m, M_DONTWAIT, MT_DATA);
17722044e51Sthorpej 		if (m == 0) {
178*72f872d2Sthorpej 			if_statinc(ifp, if_ierrors);
17922044e51Sthorpej 			continue;
18022044e51Sthorpej 		}
181d938d837Sozaki-r 		m_set_rcvif(m, ifp);
18222044e51Sthorpej 		m->m_pkthdr.len = len;
1830b357f29Sbillc 
1845804d3f6Sws 		l = MHLEN;
1855804d3f6Sws 		head = 0;
1865804d3f6Sws 		mp = &head;
1875804d3f6Sws 
1885804d3f6Sws 		while (len > 0) {
1895804d3f6Sws 			if (head) {
1905804d3f6Sws 				MGET(m, M_DONTWAIT, MT_DATA);
1915804d3f6Sws 				if (m == 0) {
192*72f872d2Sthorpej 					if_statinc(ifp, if_ierrors);
1935804d3f6Sws 					m_freem(head);
1945804d3f6Sws 					head = 0;
1955804d3f6Sws 					break;
1965804d3f6Sws 				}
1975804d3f6Sws 				l = MLEN;
1985804d3f6Sws 			}
1995804d3f6Sws 			if (len >= MINCLSIZE) {
2005804d3f6Sws 				MCLGET(m, M_DONTWAIT);
20191a80852Smycroft 				if ((m->m_flags & M_EXT) == 0) {
202*72f872d2Sthorpej 					if_statinc(ifp, if_ierrors);
203d8615e4fSmycroft 					m_free(m);
204030c7010Smycroft 					m_freem(head);
205030c7010Smycroft 					head = 0;
206030c7010Smycroft 					break;
207030c7010Smycroft 				}
2085804d3f6Sws 				l = MCLBYTES;
2095804d3f6Sws 			}
2109e47d445Scgd 
2119e47d445Scgd 			/*
2129e47d445Scgd 			 * Make sure the data after the Ethernet header
2139e47d445Scgd 			 * is aligned.
2149e47d445Scgd 			 *
2159e47d445Scgd 			 * XXX Assumes the device is an ethernet, but
2169e47d445Scgd 			 * XXX then so does other code in this driver.
2179e47d445Scgd 			 */
2189e47d445Scgd 			if (head == NULL) {
2192a18c897Smatt 				char *newdata = (char *)ALIGN(m->m_data +
2209e47d445Scgd 				      sizeof(struct ether_header)) -
2219e47d445Scgd 				    sizeof(struct ether_header);
2229e47d445Scgd 				l -= newdata - m->m_data;
2239e47d445Scgd 				m->m_data = newdata;
2249e47d445Scgd 			}
2259e47d445Scgd 
226d1579b2dSriastradh 			m->m_len = l = uimin(len, l);
2274f9cf8aaScegger 			memcpy(mtod(m, char *), bufp, l);
2285804d3f6Sws 			bufp += l;
2295804d3f6Sws 			len -= l;
2305804d3f6Sws 			*mp = m;
2315804d3f6Sws 			mp = &m->m_next;
2325804d3f6Sws 		}
2335804d3f6Sws 		if (head == 0)
2345804d3f6Sws 			continue;
2355804d3f6Sws 
2369c4cd063Sozaki-r 		if_percpuq_enqueue(ifp->if_percpuq, head);
2375804d3f6Sws 	}
238f5191984Schs 	splx(s);
2395804d3f6Sws }
2405804d3f6Sws 
2415804d3f6Sws static void
ofnet_timer(void * arg)242454af1c0Sdsl ofnet_timer(void *arg)
2435804d3f6Sws {
244dbe4c0c0Sthorpej 	struct ofnet_softc *of = arg;
245dbe4c0c0Sthorpej 
246310f6fefSmycroft 	ofnet_read(of);
247fc96443dSthorpej 	callout_reset(&of->sc_callout, 1, ofnet_timer, of);
2485804d3f6Sws }
2495804d3f6Sws 
2505804d3f6Sws static void
ofnet_init(struct ofnet_softc * of)2515911b9a6Smatt ofnet_init(struct ofnet_softc *of)
2525804d3f6Sws {
25307b064e0Sis 	struct ifnet *ifp = &of->sc_ethercom.ec_if;
2545804d3f6Sws 
2555804d3f6Sws 	if (ifp->if_flags & IFF_RUNNING)
2565804d3f6Sws 		return;
2575804d3f6Sws 
2585804d3f6Sws 	ifp->if_flags |= IFF_RUNNING;
2595804d3f6Sws 	/* Start reading from interface */
260310f6fefSmycroft 	ofnet_timer(of);
2615804d3f6Sws 	/* Attempt to start output */
262310f6fefSmycroft 	ofnet_start(ifp);
2635804d3f6Sws }
2645804d3f6Sws 
2655804d3f6Sws static void
ofnet_stop(struct ofnet_softc * of)2665911b9a6Smatt ofnet_stop(struct ofnet_softc *of)
2675804d3f6Sws {
268fc96443dSthorpej 	callout_stop(&of->sc_callout);
26907b064e0Sis 	of->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
2705804d3f6Sws }
2715804d3f6Sws 
2725804d3f6Sws static void
ofnet_start(struct ifnet * ifp)2735911b9a6Smatt ofnet_start(struct ifnet *ifp)
2745804d3f6Sws {
275310f6fefSmycroft 	struct ofnet_softc *of = ifp->if_softc;
2765804d3f6Sws 	struct mbuf *m, *m0;
2775804d3f6Sws 	char *bufp;
2785804d3f6Sws 	int len;
2795804d3f6Sws 
2805804d3f6Sws 	if (!(ifp->if_flags & IFF_RUNNING))
2815804d3f6Sws 		return;
2825804d3f6Sws 
2835804d3f6Sws 	for (;;) {
2845804d3f6Sws 		/* First try reading any packets */
285310f6fefSmycroft 		ofnet_read(of);
2865804d3f6Sws 
2875804d3f6Sws 		/* Now get the first packet on the queue */
288ac36f7cbSitojun 		IFQ_DEQUEUE(&ifp->if_snd, m0);
2895804d3f6Sws 		if (!m0)
2905804d3f6Sws 			return;
2915804d3f6Sws 
2925804d3f6Sws 		if (!(m0->m_flags & M_PKTHDR))
293310f6fefSmycroft 			panic("ofnet_start: no header mbuf");
2945804d3f6Sws 		len = m0->m_pkthdr.len;
2955804d3f6Sws 
2963cd62456Smsaitoh 		bpf_mtap(ifp, m0, BPF_D_OUT);
2978338396bSthorpej 
2985804d3f6Sws 		if (len > ETHERMTU + sizeof(struct ether_header)) {
2995804d3f6Sws 			/* packet too large, toss it */
300*72f872d2Sthorpej 			if_statinc(ifp, if_oerrors);
3015804d3f6Sws 			m_freem(m0);
3025804d3f6Sws 			continue;
3035804d3f6Sws 		}
3045804d3f6Sws 
3055911b9a6Smatt 		for (bufp = buf; (m = m0) != NULL;) {
306e2cb8590Scegger 			memcpy(bufp, mtod(m, char *), m->m_len);
3075804d3f6Sws 			bufp += m->m_len;
3089c7db92fSchristos 			m0 = m_free(m);
3095804d3f6Sws 		}
3100b357f29Sbillc 
3110b357f29Sbillc 		/*
3120b357f29Sbillc 		 * We don't know if the interface will auto-pad for
3130b357f29Sbillc 		 * us, so make sure it's at least as large as a
3140b357f29Sbillc 		 * minimum size Ethernet packet.
3150b357f29Sbillc 		 */
3160b357f29Sbillc 
317847cb1faSbouyer 		if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN)) {
318847cb1faSbouyer 			memset(bufp, 0, ETHER_MIN_LEN - ETHER_CRC_LEN - len);
319847cb1faSbouyer 			bufp += ETHER_MIN_LEN - ETHER_CRC_LEN - len;
320847cb1faSbouyer 		} else
3210b357f29Sbillc 			len = bufp - buf;
3220b357f29Sbillc 
3230b357f29Sbillc 		if (OF_write(of->sc_ihandle, buf, len) != len)
324*72f872d2Sthorpej 			if_statinc(ifp, if_oerrors);
3255804d3f6Sws 		else
326*72f872d2Sthorpej 			if_statinc(ifp, if_opackets);
3275804d3f6Sws 	}
3285804d3f6Sws }
3295804d3f6Sws 
3305804d3f6Sws static int
ofnet_ioctl(struct ifnet * ifp,u_long cmd,void * data)33153524e44Schristos ofnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
3325804d3f6Sws {
333310f6fefSmycroft 	struct ofnet_softc *of = ifp->if_softc;
3345804d3f6Sws 	struct ifaddr *ifa = (struct ifaddr *)data;
3355911b9a6Smatt 	/* struct ifreq *ifr = (struct ifreq *)data; */
3365804d3f6Sws 	int error = 0;
3375804d3f6Sws 
3385804d3f6Sws 	switch (cmd) {
339de87fe67Sdyoung 	case SIOCINITIFADDR:
3405804d3f6Sws 		ifp->if_flags |= IFF_UP;
3415804d3f6Sws 
3425804d3f6Sws 		switch (ifa->ifa_addr->sa_family) {
3435804d3f6Sws #ifdef	INET
3445804d3f6Sws 		case AF_INET:
34507b064e0Sis 			arp_ifinit(ifp, ifa);
3465804d3f6Sws 			break;
3475804d3f6Sws #endif
3485804d3f6Sws 		default:
3495804d3f6Sws 			break;
3505804d3f6Sws 		}
351310f6fefSmycroft 		ofnet_init(of);
3525804d3f6Sws 		break;
3535804d3f6Sws 	case SIOCSIFFLAGS:
354de87fe67Sdyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
355de87fe67Sdyoung 			break;
356de87fe67Sdyoung 		/* XXX re-use ether_ioctl() */
357de87fe67Sdyoung 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
358de87fe67Sdyoung 		case IFF_RUNNING:
3595804d3f6Sws 			/* If interface is down, but running, stop it. */
360310f6fefSmycroft 			ofnet_stop(of);
361de87fe67Sdyoung 			break;
362de87fe67Sdyoung 		case IFF_UP:
3635804d3f6Sws 			/* If interface is up, but not running, start it. */
364310f6fefSmycroft 			ofnet_init(of);
365de87fe67Sdyoung 			break;
366de87fe67Sdyoung 		default:
3675804d3f6Sws 			/* Other flags are ignored. */
368de87fe67Sdyoung 			break;
3695804d3f6Sws 		}
3705804d3f6Sws 		break;
3715804d3f6Sws 	default:
372de87fe67Sdyoung 		error = ether_ioctl(ifp, cmd, data);
3735804d3f6Sws 		break;
3745804d3f6Sws 	}
3755804d3f6Sws 	return error;
3765804d3f6Sws }
3775804d3f6Sws 
3785804d3f6Sws static void
ofnet_watchdog(struct ifnet * ifp)3795911b9a6Smatt ofnet_watchdog(struct ifnet *ifp)
3805804d3f6Sws {
381310f6fefSmycroft 	struct ofnet_softc *of = ifp->if_softc;
3825804d3f6Sws 
3839af887f0Smrg 	log(LOG_ERR, "%s: device timeout\n", device_xname(of->sc_dev));
384*72f872d2Sthorpej 	if_statinc(ifp, if_oerrors);
385310f6fefSmycroft 	ofnet_stop(of);
386310f6fefSmycroft 	ofnet_init(of);
3875804d3f6Sws }
388