if_fwe.c revision 110269
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 110269 2003-02-03 07:33:31Z 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	struct fw_eui64 *eui;
150
151	fwe = ((struct fwe_softc *)device_get_softc(dev));
152	unit = device_get_unit(dev);
153
154	bzero(fwe, sizeof(struct fwe_softc));
155	/* XXX */
156	fwe->stream_ch = stream_ch;
157	fwe->dma_ch = -1;
158
159	fwe->fd.fc = device_get_ivars(dev);
160	fwe->fd.dev = dev;
161	fwe->fd.post_explore = NULL;
162	fwe->eth_softc.fwe = fwe;
163
164	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
165	fwe->pkt_hdr.mode.stream.sy = 0;
166	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
167
168	/* generate fake MAC address: first and last 3bytes from eui64 */
169#define LOCAL (0x02)
170#define GROUP (0x01)
171	eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0];
172
173	eui = &fwe->fd.fc->eui;
174	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
175	eaddr[1] = FW_EUI64_BYTE(eui, 1);
176	eaddr[2] = FW_EUI64_BYTE(eui, 2);
177	eaddr[3] = FW_EUI64_BYTE(eui, 5);
178	eaddr[4] = FW_EUI64_BYTE(eui, 6);
179	eaddr[5] = FW_EUI64_BYTE(eui, 7);
180	printf("if_fwe%d: Fake Ethernet address: "
181		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
182		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
183
184	/* fill the rest and attach interface */
185	ifp = &fwe->fwe_if;
186	ifp->if_softc = &fwe->eth_softc;
187
188	ifp->if_unit = unit;
189	ifp->if_name = "fwe";
190	ifp->if_init = fwe_init;
191	ifp->if_output = ether_output;
192	ifp->if_start = fwe_start;
193	ifp->if_ioctl = fwe_ioctl;
194	ifp->if_mtu = ETHERMTU;
195	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
196	ifp->if_snd.ifq_maxlen = FWMAXQUEUE - 1;
197
198	s = splimp();
199#if __FreeBSD_version >= 500000
200	ether_ifattach(ifp, eaddr);
201#else
202	ether_ifattach(ifp, 1);
203#endif
204	splx(s);
205
206        /* Tell the upper layer(s) we support long frames. */
207	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
208#if __FreeBSD_version >= 500000
209	ifp->if_capabilities |= IFCAP_VLAN_MTU;
210#endif
211
212	ifp->if_snd.ifq_maxlen = MAX_QUEUED - 1;
213
214	FWEDEBUG("interface %s%d created.\n", ifp->if_name, ifp->if_unit);
215	return 0;
216}
217
218static void
219fwe_stop(struct fwe_softc *fwe)
220{
221	struct firewire_comm *fc;
222	struct fw_xferq *xferq;
223	struct ifnet *ifp = &fwe->fwe_if;
224
225	fc = fwe->fd.fc;
226
227	FWE_POLL_DEREGISTER(fwe, ifp);
228
229	if (fwe->dma_ch >= 0) {
230		xferq = fc->ir[fwe->dma_ch];
231
232		if (xferq->flag & FWXFERQ_RUNNING)
233			fc->irx_disable(fc, fwe->dma_ch);
234		xferq->flag &=
235			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_HANDLER);
236		/* XXX dequeue xferq->q */
237		fwe->dma_ch = -1;
238	}
239
240	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
241}
242
243static int
244fwe_detach(device_t dev)
245{
246	struct fwe_softc *fwe;
247	int s;
248
249	fwe = (struct fwe_softc *)device_get_softc(dev);
250	s = splimp();
251
252	fwe_stop(fwe);
253#if __FreeBSD_version >= 500000
254	ether_ifdetach(&fwe->fwe_if);
255#else
256	ether_ifdetach(&fwe->fwe_if, 1);
257#endif
258
259	splx(s);
260	return 0;
261}
262
263
264static void
265fwe_init(void *arg)
266{
267	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
268	struct firewire_comm *fc;
269	struct ifnet *ifp = &fwe->fwe_if;
270	struct fw_xferq *xferq;
271	int i;
272
273	FWEDEBUG("initializing %s%d\n", ifp->if_name, ifp->if_unit);
274
275	/* XXX keep promiscoud mode */
276	ifp->if_flags |= IFF_PROMISC;
277
278	fc = fwe->fd.fc;
279#define START 0
280	if (fwe->dma_ch < 0) {
281		xferq = NULL;
282		for (i = START; i < fc->nisodma; i ++) {
283			xferq = fc->ir[i];
284			if ((xferq->flag & FWXFERQ_OPEN) == 0)
285				break;
286		}
287
288		if (xferq == NULL) {
289			printf("no free dma channel\n");
290			return;
291		}
292		fwe->dma_ch = i;
293		fwe->stream_ch = stream_ch;
294		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
295		/* allocate DMA channel and init packet mode */
296		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_PACKET;
297		xferq->flag |= fwe->stream_ch & 0xff;
298		/* register fwe_input handler */
299		xferq->sc = (caddr_t) fwe;
300		xferq->hand = fwe_as_input;
301		xferq->flag |= FWXFERQ_HANDLER;
302	} else
303		xferq = fc->ir[fwe->dma_ch];
304
305
306	/* start dma */
307	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
308		fc->irx_enable(fc, fwe->dma_ch);
309
310	ifp->if_flags |= IFF_RUNNING;
311	ifp->if_flags &= ~IFF_OACTIVE;
312
313	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
314#if 0
315	/* attempt to start output */
316	fwe_start(ifp);
317#endif
318}
319
320
321static int
322fwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
323{
324	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
325	struct ifstat *ifs = NULL;
326	int s, error, len;
327
328	switch (cmd) {
329		case SIOCSIFFLAGS:
330			s = splimp();
331			if (ifp->if_flags & IFF_UP) {
332				if (!(ifp->if_flags & IFF_RUNNING))
333					fwe_init(&fwe->eth_softc);
334			} else {
335				if (ifp->if_flags & IFF_RUNNING)
336					fwe_stop(fwe);
337			}
338			/* XXX keep promiscoud mode */
339			ifp->if_flags |= IFF_PROMISC;
340			splx(s);
341			break;
342		case SIOCADDMULTI:
343		case SIOCDELMULTI:
344			break;
345
346		case SIOCGIFSTATUS:
347			s = splimp();
348			ifs = (struct ifstat *)data;
349			len = strlen(ifs->ascii);
350			if (len < sizeof(ifs->ascii))
351				snprintf(ifs->ascii + len,
352					sizeof(ifs->ascii) - len,
353					"\tch %d dma %d\n",
354						fwe->stream_ch, fwe->dma_ch);
355			splx(s);
356			break;
357#if __FreeBSD_version >= 500000
358		default:
359#else
360		case SIOCSIFADDR:
361		case SIOCGIFADDR:
362		case SIOCSIFMTU:
363#endif
364			s = splimp();
365			error = ether_ioctl(ifp, cmd, data);
366			splx(s);
367			return (error);
368#if __FreeBSD_version < 500000
369		default:
370			return (EINVAL);
371#endif
372	}
373
374	return (0);
375}
376
377static void
378fwe_start(struct ifnet *ifp)
379{
380	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
381	int s;
382
383#if 1
384	FWEDEBUG("%s%d starting\n", ifp->if_name, ifp->if_unit);
385
386	if (fwe->dma_ch < 0) {
387		struct mbuf	*m = NULL;
388
389		FWEDEBUG("%s%d not ready.\n", ifp->if_name, ifp->if_unit);
390
391		s = splimp();
392		do {
393			IF_DEQUEUE(&ifp->if_snd, m);
394			if (m != NULL)
395				m_freem(m);
396			ifp->if_oerrors ++;
397		} while (m != NULL);
398		splx(s);
399
400		return;
401	}
402
403#endif
404	s = splimp();
405	ifp->if_flags |= IFF_OACTIVE;
406
407	if (ifp->if_snd.ifq_len != 0)
408		fwe_as_output(fwe, ifp);
409
410	ifp->if_flags &= ~IFF_OACTIVE;
411	splx(s);
412}
413
414
415static void
416fwe_output_callback(struct fw_xfer *xfer)
417{
418	struct fwe_softc *fwe;
419	struct ifnet *ifp;
420
421	fwe = (struct fwe_softc *)xfer->sc;
422	/* XXX error check */
423	FWEDEBUG("resp = %d\n", xfer->resp);
424	m_freem(xfer->mbuf);
425	xfer->send.buf = NULL;
426	fw_xfer_free(xfer);
427#if 1
428	/* XXX for queue full */
429	ifp = &fwe->fwe_if;
430	if (ifp->if_snd.ifq_head != NULL)
431		fwe_start(ifp);
432#endif
433}
434
435#define HDR_LEN 4
436#define ALIGN_PAD 2
437/* Async. stream output */
438static void
439fwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
440{
441	struct mbuf *m;
442	struct fw_xfer *xfer;
443	struct fw_xferq *xferq;
444	struct fw_pkt *fp;
445	int i = 0;
446
447	xfer = NULL;
448	xferq = fwe->fd.fc->atq;
449	while (xferq->queued < xferq->maxq) {
450		IF_DEQUEUE(&ifp->if_snd, m);
451		if (m == NULL)
452			break;
453		xfer = fw_xfer_alloc(M_FWXFER);
454		if (xfer == NULL) {
455			return;
456		}
457#if __FreeBSD_version >= 500000
458		BPF_MTAP(ifp, m);
459#else
460		if (ifp->if_bpf != NULL)
461			bpf_mtap(ifp, m);
462#endif
463
464		xfer->send.off = 0;
465		xfer->spd = 2;
466		xfer->fc = fwe->fd.fc;
467		xfer->retry_req = fw_asybusy;
468		xfer->sc = (caddr_t)fwe;
469		xfer->act.hand = fwe_output_callback;
470
471		/* keep ip packet alignment for alpha */
472		M_PREPEND(m, ALIGN_PAD, M_NOWAIT);
473		fp = (struct fw_pkt *)&xfer->dst; /* XXX */
474		xfer->dst = *((int32_t *)&fwe->pkt_hdr);
475		fp->mode.stream.len = htons(m->m_pkthdr.len);
476		xfer->send.buf = (caddr_t) fp;
477		xfer->mbuf = m;
478		xfer->send.len = m->m_pkthdr.len + HDR_LEN;
479
480		i++;
481		if (fw_asyreq(xfer->fc, -1, xfer) != 0) {
482			/* error */
483			ifp->if_oerrors ++;
484			/* XXX set error code */
485			fwe_output_callback(xfer);
486		} else {
487			ifp->if_opackets ++;
488		}
489	}
490#if 0
491	if (i > 1)
492		printf("%d queued\n", i);
493#endif
494	if (xfer != NULL)
495		xferq->start(xfer->fc);
496}
497
498#if __FreeBSD_version >= 500000
499static void
500fwe_free(void *buf, void *args)
501{
502	FWEDEBUG("fwe_free:\n");
503	free(buf, M_FW);
504}
505
506#else
507static void
508fwe_free(caddr_t buf, u_int size)
509{
510	int *p;
511	FWEDEBUG("fwe_free:\n");
512	p = (int *)buf;
513	(*p) --;
514	if (*p < 1)
515		free(buf, M_FW);
516}
517
518static void
519fwe_ref(caddr_t buf, u_int size)
520{
521	int *p;
522
523	FWEDEBUG("fwe_ref: called\n");
524	p = (int *)buf;
525	(*p) ++;
526}
527#endif
528
529/* Async. stream output */
530static void
531fwe_as_input(struct fw_xferq *xferq)
532{
533	struct mbuf *m;
534	struct ether_header *eh;
535	struct ifnet *ifp;
536	struct fw_xfer *xfer;
537	struct fwe_softc *fwe;
538	u_char *c;
539	int len;
540	caddr_t p;
541
542	fwe = (struct fwe_softc *)xferq->sc;
543	ifp = &fwe->fwe_if;
544#if 0
545	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
546#endif
547	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
548		STAILQ_REMOVE_HEAD(&xferq->q, link);
549		xferq->queued --;
550		MGETHDR(m, M_NOWAIT, MT_DATA);
551		if (m == NULL) {
552			printf("MGETHDR failed\n");
553			fw_xfer_free(xfer);
554			return;
555		}
556		len = xfer->recv.off + xfer->recv.len;
557		FWEDEBUG("fwe_as_input len=%d\n", len);
558#if __FreeBSD_version >= 500000
559		MEXTADD(m, xfer->recv.buf, len, fwe_free, NULL, 0, EXT_NET_DRV);
560#else
561		m->m_flags |= M_EXT;
562		m->m_ext.ext_buf = xfer->recv.buf;
563		m->m_ext.ext_size = len;
564		m->m_ext.ext_free = fwe_free;
565		m->m_ext.ext_ref = fwe_ref;
566		*((int *)m->m_ext.ext_buf) = 1;  /* XXX refcount */
567#endif
568		p = xfer->recv.buf + xfer->recv.off + HDR_LEN + ALIGN_PAD;
569		eh = (struct ether_header *)p;
570#if __FreeBSD_version >= 500000
571		len -= xfer->recv.off + HDR_LEN + ALIGN_PAD;
572#else
573		p += sizeof(struct ether_header);
574		len -= xfer->recv.off + HDR_LEN + ALIGN_PAD
575						+ sizeof(struct ether_header);
576#endif
577		m->m_data = p;
578		m->m_len = m->m_pkthdr.len = len;
579		m->m_pkthdr.rcvif = ifp;
580		c = (char *)eh;
581#if 0
582		FWEDEBUG("%02x %02x %02x %02x %02x %02x\n"
583			 "%02x %02x %02x %02x %02x %02x\n"
584			 "%02x %02x %02x %02x\n"
585			 "%02x %02x %02x %02x\n"
586			 "%02x %02x %02x %02x\n"
587			 "%02x %02x %02x %02x\n",
588			 c[0], c[1], c[2], c[3], c[4], c[5],
589			 c[6], c[7], c[8], c[9], c[10], c[11],
590			 c[12], c[13], c[14], c[15],
591			 c[16], c[17], c[18], c[19],
592			 c[20], c[21], c[22], c[23],
593			 c[20], c[21], c[22], c[23]
594		 );
595#endif
596#if __FreeBSD_version >= 500000
597		(*ifp->if_input)(ifp, m);
598#else
599		ether_input(ifp, eh, m);
600#endif
601		ifp->if_ipackets ++;
602
603		xfer->recv.buf = NULL;
604		fw_xfer_free(xfer);
605	}
606}
607
608
609static devclass_t fwe_devclass;
610
611static device_method_t fwe_methods[] = {
612	/* device interface */
613	DEVMETHOD(device_identify,	fwe_identify),
614	DEVMETHOD(device_probe,		fwe_probe),
615	DEVMETHOD(device_attach,	fwe_attach),
616	DEVMETHOD(device_detach,	fwe_detach),
617	{ 0, 0 }
618};
619
620static driver_t fwe_driver = {
621        "if_fwe",
622	fwe_methods,
623	sizeof(struct fwe_softc),
624};
625
626
627DRIVER_MODULE(if_fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
628MODULE_VERSION(if_fwe, 1);
629MODULE_DEPEND(if_fwe, firewire, 1, 1, 1);
630