if_fwe.c revision 107653
1/*
2 * Copyright (C) 2002
3 * 	Hidetoshi Shimokawa. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *
16 *	This product includes software developed by Hidetoshi Shimokawa.
17 *
18 * 4. Neither the name of the author nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/dev/firewire/if_fwe.c 107653 2002-12-06 02:17:30Z simokawa $
35 */
36
37#include "opt_inet.h"
38
39#include <sys/param.h>
40#include <sys/conf.h>
41#include <sys/kernel.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
45#include <sys/sockio.h>
46#include <sys/sysctl.h>
47#include <sys/systm.h>
48#include <sys/module.h>
49#include <sys/bus.h>
50
51#include <net/bpf.h>
52#include <net/ethernet.h>
53#include <net/if.h>
54#include <net/if_arp.h>
55#include <net/if_vlan_var.h>
56#include <net/route.h>
57
58#include <netinet/in.h>
59
60#include <dev/firewire/firewire.h>
61#include <dev/firewire/firewirereg.h>
62#include <dev/firewire/if_fwevar.h>
63
64#define FWEDEBUG	if (fwedebug) printf
65#define MAX_QUEUED	IFQ_MAXLEN /* 50 */
66
67/* network interface */
68static void fwe_start __P((struct ifnet *));
69static int fwe_ioctl __P((struct ifnet *, u_long, caddr_t));
70static void fwe_init __P((void *));
71
72static void fwe_as_output __P((struct fwe_softc *, struct ifnet *));
73static void fwe_as_input __P((struct fw_xferq *));
74
75static int fwedebug = 0;
76static int stream_ch = 1;
77
78MALLOC_DECLARE(M_FWE);
79MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over Firewire interface");
80SYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
81SYSCTL_DECL(_hw_firewire);
82SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
83	"Ethernet Emulation Subsystem");
84SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
85	"Stream channel to use");
86
87#ifdef DEVICE_POLLING
88#define FWE_POLL_REGISTER(func, fwe, ifp)			\
89	if (ether_poll_register(func, ifp)) {			\
90		struct firewire_comm *fc = (fwe)->fd.fc;	\
91		fc->set_intr(fc, 0);				\
92	}
93
94#define FWE_POLL_DEREGISTER(fwe, ifp)				\
95	do {							\
96		struct firewire_comm *fc = (fwe)->fd.fc;	\
97		ether_poll_deregister(ifp);			\
98		fc->set_intr(fc, 1);				\
99	} while(0)						\
100
101static poll_handler_t fwe_poll;
102
103static void
104fwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
105{
106	struct fwe_softc *fwe;
107	struct firewire_comm *fc;
108
109	fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
110	fc = fwe->fd.fc;
111	if (cmd == POLL_DEREGISTER) {
112		/* enable interrupts */
113		fc->set_intr(fc, 1);
114		return;
115	}
116	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
117}
118#else
119#define FWE_POLL_REGISTER(func, fwe, ifp)
120#define FWE_POLL_DEREGISTER(fwe, ifp)
121#endif
122static void
123fwe_identify(driver_t *driver, device_t parent)
124{
125	BUS_ADD_CHILD(parent, 0, "if_fwe", device_get_unit(parent));
126}
127
128static int
129fwe_probe(device_t dev)
130{
131	device_t pa;
132
133	pa = device_get_parent(dev);
134	if(device_get_unit(dev) != device_get_unit(pa)){
135		return(ENXIO);
136	}
137
138	device_set_desc(dev, "Ethernet over Firewire");
139	return (0);
140}
141
142static int
143fwe_attach(device_t dev)
144{
145	struct fwe_softc *fwe;
146	struct ifnet *ifp;
147	int unit, s;
148	u_char *eaddr;
149
150	fwe = ((struct fwe_softc *)device_get_softc(dev));
151	unit = device_get_unit(dev);
152
153	bzero(fwe, sizeof(struct fwe_softc));
154	/* XXX */
155	fwe->stream_ch = stream_ch;
156	fwe->dma_ch = -1;
157
158	fwe->fd.fc = device_get_ivars(dev);
159	fwe->fd.dev = dev;
160	fwe->fd.post_explore = NULL;
161	fwe->eth_softc.fwe = fwe;
162
163	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
164	fwe->pkt_hdr.mode.stream.sy = 0;
165	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
166
167	/* generate fake MAC address: first and last 3bytes from eui64 */
168#define LOCAL (0x02)
169#define GROUP (0x01)
170	eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0];
171	eaddr[0] = (fwe->fd.fc->eui[0] | LOCAL) & ~GROUP;
172	eaddr[1] = fwe->fd.fc->eui[1];
173	eaddr[2] = fwe->fd.fc->eui[2];
174	eaddr[3] = fwe->fd.fc->eui[5];
175	eaddr[4] = fwe->fd.fc->eui[6];
176	eaddr[5] = fwe->fd.fc->eui[7];
177	printf("if_fwe%d: Fake Ethernet address: "
178		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
179		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
180
181	/* fill the rest and attach interface */
182	ifp = &fwe->fwe_if;
183	ifp->if_softc = &fwe->eth_softc;
184
185	ifp->if_unit = unit;
186	ifp->if_name = "fwe";
187	ifp->if_init = fwe_init;
188	ifp->if_output = ether_output;
189	ifp->if_start = fwe_start;
190	ifp->if_ioctl = fwe_ioctl;
191	ifp->if_mtu = ETHERMTU;
192	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
193	ifp->if_snd.ifq_maxlen = FWMAXQUEUE - 1;
194
195	s = splimp();
196	ether_ifattach(ifp, eaddr);
197	splx(s);
198
199        /* Tell the upper layer(s) we support long frames. */
200	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
201	ifp->if_capabilities |= IFCAP_VLAN_MTU;
202
203	ifp->if_snd.ifq_maxlen = MAX_QUEUED - 1;
204
205	FWEDEBUG("interface %s%d created.\n", ifp->if_name, ifp->if_unit);
206	return 0;
207}
208
209static void
210fwe_stop(struct fwe_softc *fwe)
211{
212	struct firewire_comm *fc;
213	struct fw_xferq *xferq;
214	struct ifnet *ifp = &fwe->fwe_if;
215
216	fc = fwe->fd.fc;
217
218	FWE_POLL_DEREGISTER(fwe, ifp);
219
220	if (fwe->dma_ch >= 0) {
221		xferq = fc->ir[fwe->dma_ch];
222
223		if (xferq->flag & FWXFERQ_RUNNING)
224			fc->irx_disable(fc, fwe->dma_ch);
225		xferq->flag &=
226			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_HANDLER);
227		/* XXX dequeue xferq->q */
228		fwe->dma_ch = -1;
229	}
230
231	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
232}
233
234static int
235fwe_detach(device_t dev)
236{
237	struct fwe_softc *fwe;
238	int s;
239
240	fwe = (struct fwe_softc *)device_get_softc(dev);
241	s = splimp();
242
243	fwe_stop(fwe);
244	ether_ifdetach(&fwe->fwe_if);
245
246	splx(s);
247	return 0;
248}
249
250
251static void
252fwe_init(void *arg)
253{
254	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
255	struct firewire_comm *fc;
256	struct ifnet *ifp = &fwe->fwe_if;
257	struct fw_xferq *xferq;
258	int i;
259
260	FWEDEBUG("initializing %s%d\n", ifp->if_name, ifp->if_unit);
261
262	/* XXX keep promiscoud mode */
263	ifp->if_flags |= IFF_PROMISC;
264
265	fc = fwe->fd.fc;
266#define START 0
267	if (fwe->dma_ch < 0) {
268		xferq = NULL;
269		for (i = START; i < fc->nisodma; i ++) {
270			xferq = fc->ir[i];
271			if ((xferq->flag & FWXFERQ_OPEN) == 0)
272				break;
273		}
274
275		if (xferq == NULL) {
276			printf("no free dma channel\n");
277			return;
278		}
279		fwe->dma_ch = i;
280		fwe->stream_ch = stream_ch;
281		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
282		/* allocate DMA channel and init packet mode */
283		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_PACKET;
284		xferq->flag |= fwe->stream_ch & 0xff;
285		/* register fwe_input handler */
286		xferq->sc = (caddr_t) fwe;
287		xferq->hand = fwe_as_input;
288		xferq->flag |= FWXFERQ_HANDLER;
289	} else
290		xferq = fc->ir[fwe->dma_ch];
291
292
293	/* start dma */
294	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
295		fc->irx_enable(fc, fwe->dma_ch);
296
297	ifp->if_flags |= IFF_RUNNING;
298	ifp->if_flags &= ~IFF_OACTIVE;
299
300	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
301#if 0
302	/* attempt to start output */
303	fwe_start(ifp);
304#endif
305}
306
307
308static int
309fwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
310{
311	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
312	struct ifstat *ifs = NULL;
313	int s, error, len;
314
315	switch (cmd) {
316		case SIOCSIFFLAGS:
317			s = splimp();
318			if (ifp->if_flags & IFF_UP) {
319				if (!(ifp->if_flags & IFF_RUNNING))
320					fwe_init(&fwe->eth_softc);
321			} else {
322				if (ifp->if_flags & IFF_RUNNING)
323					fwe_stop(fwe);
324			}
325			/* XXX keep promiscoud mode */
326			ifp->if_flags |= IFF_PROMISC;
327			splx(s);
328			break;
329		case SIOCADDMULTI:
330		case SIOCDELMULTI:
331		break;
332
333		case SIOCGIFSTATUS:
334			s = splimp();
335			ifs = (struct ifstat *)data;
336			len = strlen(ifs->ascii);
337			if (len < sizeof(ifs->ascii))
338				snprintf(ifs->ascii + len,
339					sizeof(ifs->ascii) - len,
340					"\tch %d dma %d\n",
341						fwe->stream_ch, fwe->dma_ch);
342			splx(s);
343		break;
344
345		default:
346			s = splimp();
347			error = ether_ioctl(ifp, cmd, data);
348			splx(s);
349			return (error);
350	}
351
352	return (0);
353}
354
355static void
356fwe_start(struct ifnet *ifp)
357{
358	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
359	int s;
360
361#if 1
362	FWEDEBUG("%s%d starting\n", ifp->if_name, ifp->if_unit);
363
364	if (fwe->dma_ch < 0) {
365		struct mbuf	*m = NULL;
366
367		FWEDEBUG("%s%d not ready.\n", ifp->if_name, ifp->if_unit);
368
369		s = splimp();
370		do {
371			IF_DEQUEUE(&ifp->if_snd, m);
372			if (m != NULL)
373				m_freem(m);
374			ifp->if_oerrors ++;
375		} while (m != NULL);
376		splx(s);
377
378		return;
379	}
380
381#endif
382	s = splimp();
383	ifp->if_flags |= IFF_OACTIVE;
384
385	if (ifp->if_snd.ifq_len != 0)
386		fwe_as_output(fwe, ifp);
387
388	ifp->if_flags &= ~IFF_OACTIVE;
389	splx(s);
390}
391
392
393static void
394fwe_output_callback(struct fw_xfer *xfer)
395{
396	struct fwe_softc *fwe;
397	struct ifnet *ifp;
398
399	fwe = (struct fwe_softc *)xfer->sc;
400	/* XXX error check */
401	FWEDEBUG("resp = %d\n", xfer->resp);
402	m_freem(xfer->mbuf);
403	xfer->send.buf = NULL;
404	fw_xfer_free(xfer);
405#if 1
406	/* XXX for queue full */
407	ifp = &fwe->fwe_if;
408	if (ifp->if_snd.ifq_head != NULL)
409		fwe_start(ifp);
410#endif
411}
412
413#define HDR_LEN 4
414#define ALIGN_PAD 2
415/* Async. stream output */
416static void
417fwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
418{
419	struct mbuf *m;
420	struct fw_xfer *xfer;
421	struct fw_xferq *xferq;
422	struct fw_pkt *fp;
423	int i = 0;
424
425	xfer = NULL;
426	xferq = fwe->fd.fc->atq;
427	while (xferq->queued < xferq->maxq) {
428		IF_DEQUEUE(&ifp->if_snd, m);
429		if (m == NULL)
430			break;
431		xfer = fw_xfer_alloc();
432		if (xfer == NULL) {
433			return;
434		}
435		BPF_MTAP(ifp, m);
436
437		xfer->send.off = 0;
438		xfer->spd = 2;
439		xfer->fc = fwe->fd.fc;
440		xfer->retry_req = fw_asybusy;
441		xfer->sc = (caddr_t)fwe;
442		xfer->act.hand = fwe_output_callback;
443
444		/* keep ip packet alignment for alpha */
445		M_PREPEND(m, ALIGN_PAD, M_DONTWAIT);
446		fp = (struct fw_pkt *)&xfer->dst; /* XXX */
447		xfer->dst = *((int32_t *)&fwe->pkt_hdr);
448		fp->mode.stream.len = htons(m->m_pkthdr.len);
449		xfer->send.buf = (caddr_t) fp;
450		xfer->mbuf = m;
451		xfer->send.len = m->m_pkthdr.len + HDR_LEN;
452
453		i++;
454		if (fw_asyreq(xfer->fc, -1, xfer) != 0) {
455			/* error */
456			ifp->if_oerrors ++;
457			/* XXX set error code */
458			fwe_output_callback(xfer);
459		} else {
460			ifp->if_opackets ++;
461		}
462	}
463#if 0
464	if (i > 1)
465		printf("%d queued\n", i);
466#endif
467	if (xfer != NULL)
468		xferq->start(xfer->fc);
469}
470
471#if __FreeBSD_version >= 500000
472static void
473fwe_free(void *buf, void *args)
474{
475	FWEDEBUG("fwe_free:\n");
476	free(buf, M_DEVBUF);
477}
478
479#else
480static void
481fwe_free(caddr_t buf, u_int size)
482{
483	int *p;
484	FWEDEBUG("fwe_free:\n");
485	p = (int *)buf;
486	(*p) --;
487	if (*p < 1)
488		free(buf, M_DEVBUF);
489}
490
491static void
492fwe_ref(caddr_t buf, u_int size)
493{
494	int *p;
495
496	FWEDEBUG("fwe_ref: called\n");
497	p = (int *)buf;
498	(*p) ++;
499}
500#endif
501
502/* Async. stream output */
503static void
504fwe_as_input(struct fw_xferq *xferq)
505{
506	struct mbuf *m;
507	struct ether_header *eh;
508	struct ifnet *ifp;
509	struct fw_xfer *xfer;
510	struct fwe_softc *fwe;
511	u_char *c;
512	int len;
513	caddr_t p;
514
515	fwe = (struct fwe_softc *)xferq->sc;
516	ifp = &fwe->fwe_if;
517#if 0
518	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
519#endif
520	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
521		STAILQ_REMOVE_HEAD(&xferq->q, link);
522		xferq->queued --;
523		MGETHDR(m, M_DONTWAIT, MT_DATA);
524		if (m == NULL) {
525			printf("MGETHDR failed\n");
526			fw_xfer_free(xfer);
527			return;
528		}
529		len = xfer->recv.off + xfer->recv.len;
530		FWEDEBUG("fwe_as_input len=%d\n", len);
531#if __FreeBSD_version >= 500000
532		MEXTADD(m, xfer->recv.buf, len, fwe_free, NULL, 0, EXT_NET_DRV);
533#else
534		m->m_flags |= M_EXT;
535		m->m_ext.ext_buf = xfer->recv.buf;
536		m->m_ext.ext_size = len;
537		m->m_ext.ext_free = fwe_free;
538		m->m_ext.ext_ref = fwe_ref;
539		*((int *)m->m_ext.ext_buf) = 1;  /* XXX refcount */
540#endif
541		p = xfer->recv.buf + xfer->recv.off + HDR_LEN + ALIGN_PAD;
542		eh = (struct ether_header *)p;
543		len -= xfer->recv.off + HDR_LEN + ALIGN_PAD;
544		m->m_data = p;
545		m->m_len = m->m_pkthdr.len = len;
546		m->m_pkthdr.rcvif = ifp;
547		c = (char *)eh;
548#if 0
549		FWEDEBUG("%02x %02x %02x %02x %02x %02x\n"
550			 "%02x %02x %02x %02x %02x %02x\n"
551			 "%02x %02x %02x %02x\n"
552			 "%02x %02x %02x %02x\n"
553			 "%02x %02x %02x %02x\n"
554			 "%02x %02x %02x %02x\n",
555			 c[0], c[1], c[2], c[3], c[4], c[5],
556			 c[6], c[7], c[8], c[9], c[10], c[11],
557			 c[12], c[13], c[14], c[15],
558			 c[16], c[17], c[18], c[19],
559			 c[20], c[21], c[22], c[23],
560			 c[20], c[21], c[22], c[23]
561		 );
562#endif
563		(*ifp->if_input)(ifp, m);
564		ifp->if_ipackets ++;
565
566		xfer->recv.buf = NULL;
567		fw_xfer_free(xfer);
568	}
569}
570
571
572static devclass_t fwe_devclass;
573
574static device_method_t fwe_methods[] = {
575	/* device interface */
576	DEVMETHOD(device_identify,	fwe_identify),
577	DEVMETHOD(device_probe,		fwe_probe),
578	DEVMETHOD(device_attach,	fwe_attach),
579	DEVMETHOD(device_detach,	fwe_detach),
580	{ 0, 0 }
581};
582
583static driver_t fwe_driver = {
584        "if_fwe",
585	fwe_methods,
586	sizeof(struct fwe_softc),
587};
588
589
590DRIVER_MODULE(if_fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
591MODULE_VERSION(if_fwe, 1);
592MODULE_DEPEND(if_fwe, firewire, 1, 1, 1);
593