if_fwip.c revision 1.4.18.2 1 1.4.18.2 yamt /* $NetBSD: if_fwip.c,v 1.4.18.2 2006/06/21 15:04:08 yamt Exp $ */
2 1.4.18.2 yamt /*-
3 1.4.18.2 yamt * Copyright (c) 2004
4 1.4.18.2 yamt * Doug Rabson
5 1.4.18.2 yamt * Copyright (c) 2002-2003
6 1.4.18.2 yamt * Hidetoshi Shimokawa. All rights reserved.
7 1.4.18.2 yamt *
8 1.4.18.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.4.18.2 yamt * modification, are permitted provided that the following conditions
10 1.4.18.2 yamt * are met:
11 1.4.18.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.4.18.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.4.18.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.4.18.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.4.18.2 yamt * documentation and/or other materials provided with the distribution.
16 1.4.18.2 yamt * 3. All advertising materials mentioning features or use of this software
17 1.4.18.2 yamt * must display the following acknowledgement:
18 1.4.18.2 yamt *
19 1.4.18.2 yamt * This product includes software developed by Hidetoshi Shimokawa.
20 1.4.18.2 yamt *
21 1.4.18.2 yamt * 4. Neither the name of the author nor the names of its contributors
22 1.4.18.2 yamt * may be used to endorse or promote products derived from this software
23 1.4.18.2 yamt * without specific prior written permission.
24 1.4.18.2 yamt *
25 1.4.18.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.4.18.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.4.18.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.4.18.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.4.18.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.4.18.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.4.18.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.4.18.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.4.18.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.4.18.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.4.18.2 yamt * SUCH DAMAGE.
36 1.4.18.2 yamt *
37 1.4.18.2 yamt * $FreeBSD: /repoman/r/ncvs/src/sys/dev/firewire/if_fwip.c,v 1.6 2005/01/06 01:42:41 imp Exp $
38 1.4.18.2 yamt */
39 1.4.18.2 yamt
40 1.4.18.2 yamt #include "opt_inet.h"
41 1.4.18.2 yamt
42 1.4.18.2 yamt #if defined(__FreeBSD__)
43 1.4.18.2 yamt #include <sys/param.h>
44 1.4.18.2 yamt #include <sys/kernel.h>
45 1.4.18.2 yamt #include <sys/malloc.h>
46 1.4.18.2 yamt #include <sys/mbuf.h>
47 1.4.18.2 yamt #include <sys/socket.h>
48 1.4.18.2 yamt #include <sys/sockio.h>
49 1.4.18.2 yamt #include <sys/sysctl.h>
50 1.4.18.2 yamt #include <sys/systm.h>
51 1.4.18.2 yamt #include <sys/taskqueue.h>
52 1.4.18.2 yamt #include <sys/module.h>
53 1.4.18.2 yamt #include <sys/bus.h>
54 1.4.18.2 yamt #include <machine/bus.h>
55 1.4.18.2 yamt
56 1.4.18.2 yamt #include <net/bpf.h>
57 1.4.18.2 yamt #include <net/if.h>
58 1.4.18.2 yamt #include <net/firewire.h>
59 1.4.18.2 yamt #include <net/if_arp.h>
60 1.4.18.2 yamt #ifdef __DragonFly__
61 1.4.18.2 yamt #include <bus/firewire/fw_port.h>
62 1.4.18.2 yamt #include <bus/firewire/firewire.h>
63 1.4.18.2 yamt #include <bus/firewire/firewirereg.h>
64 1.4.18.2 yamt #include "if_fwipvar.h"
65 1.4.18.2 yamt #else
66 1.4.18.2 yamt #include <dev/firewire/fw_port.h>
67 1.4.18.2 yamt #include <dev/firewire/firewire.h>
68 1.4.18.2 yamt #include <dev/firewire/firewirereg.h>
69 1.4.18.2 yamt #include <dev/firewire/iec13213.h>
70 1.4.18.2 yamt #include <dev/firewire/if_fwipvar.h>
71 1.4.18.2 yamt #endif
72 1.4.18.2 yamt #elif defined(__NetBSD__)
73 1.4.18.2 yamt #include <sys/param.h>
74 1.4.18.2 yamt #include <sys/device.h>
75 1.4.18.2 yamt #include <sys/errno.h>
76 1.4.18.2 yamt #include <sys/malloc.h>
77 1.4.18.2 yamt #include <sys/mbuf.h>
78 1.4.18.2 yamt #include <sys/sysctl.h>
79 1.4.18.2 yamt
80 1.4.18.2 yamt #include <machine/bus.h>
81 1.4.18.2 yamt
82 1.4.18.2 yamt #include <net/if.h>
83 1.4.18.2 yamt #include <net/if_ieee1394.h>
84 1.4.18.2 yamt
85 1.4.18.2 yamt #include <dev/ieee1394/fw_port.h>
86 1.4.18.2 yamt #include <dev/ieee1394/firewire.h>
87 1.4.18.2 yamt #include <dev/ieee1394/firewirereg.h>
88 1.4.18.2 yamt #include <dev/ieee1394/iec13213.h>
89 1.4.18.2 yamt #include <dev/ieee1394/if_fwipvar.h>
90 1.4.18.2 yamt #endif
91 1.4.18.2 yamt
92 1.4.18.2 yamt /*
93 1.4.18.2 yamt * We really need a mechanism for allocating regions in the FIFO
94 1.4.18.2 yamt * address space. We pick a address in the OHCI controller's 'middle'
95 1.4.18.2 yamt * address space. This means that the controller will automatically
96 1.4.18.2 yamt * send responses for us, which is fine since we don't have any
97 1.4.18.2 yamt * important information to put in the response anyway.
98 1.4.18.2 yamt */
99 1.4.18.2 yamt #define INET_FIFO 0xfffe00000000LL
100 1.4.18.2 yamt
101 1.4.18.2 yamt #if defined(__FreeBSD__)
102 1.4.18.2 yamt #define FWIPDEBUG if (fwipdebug) if_printf
103 1.4.18.2 yamt #elif defined(__NetBSD__)
104 1.4.18.2 yamt #define FWIPDEBUG(ifp, fmt, ...) \
105 1.4.18.2 yamt if (fwipdebug) {\
106 1.4.18.2 yamt aprint_normal("%s: ", (ifp)->if_xname); \
107 1.4.18.2 yamt aprint_normal((fmt) ,##__VA_ARGS__); \
108 1.4.18.2 yamt }
109 1.4.18.2 yamt #endif
110 1.4.18.2 yamt #define TX_MAX_QUEUE (FWMAXQUEUE - 1)
111 1.4.18.2 yamt
112 1.4.18.2 yamt #if defined(__NetBSD__)
113 1.4.18.2 yamt int fwipmatch (struct device *, struct cfdata *, void *);
114 1.4.18.2 yamt void fwipattach (struct device *, struct device *, void *);
115 1.4.18.2 yamt int fwipdetach (struct device *, int);
116 1.4.18.2 yamt int fwipactivate (struct device *, enum devact);
117 1.4.18.2 yamt
118 1.4.18.2 yamt #endif
119 1.4.18.2 yamt /* network interface */
120 1.4.18.2 yamt static void fwip_start (struct ifnet *);
121 1.4.18.2 yamt static int fwip_ioctl (struct ifnet *, u_long, caddr_t);
122 1.4.18.2 yamt IF_INIT(fwip);
123 1.4.18.2 yamt IF_STOP(fwip);
124 1.4.18.2 yamt
125 1.4.18.2 yamt static void fwip_post_busreset (void *);
126 1.4.18.2 yamt static void fwip_output_callback (struct fw_xfer *);
127 1.4.18.2 yamt static void fwip_async_output (struct fwip_softc *, struct ifnet *);
128 1.4.18.2 yamt #if defined(__FreeBSD__)
129 1.4.18.2 yamt static void fwip_start_send (void *, int);
130 1.4.18.2 yamt #endif
131 1.4.18.2 yamt static void fwip_stream_input (struct fw_xferq *);
132 1.4.18.2 yamt static void fwip_unicast_input(struct fw_xfer *);
133 1.4.18.2 yamt
134 1.4.18.2 yamt static int fwipdebug = 0;
135 1.4.18.2 yamt static int broadcast_channel = 0xc0 | 0x1f; /* tag | channel(XXX) */
136 1.4.18.2 yamt static int tx_speed = 2;
137 1.4.18.2 yamt static int rx_queue_len = FWMAXQUEUE;
138 1.4.18.2 yamt
139 1.4.18.2 yamt #if defined(__FreeBSD__)
140 1.4.18.2 yamt MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over FireWire interface");
141 1.4.18.2 yamt SYSCTL_INT(_debug, OID_AUTO, if_fwip_debug, CTLFLAG_RW, &fwipdebug, 0, "");
142 1.4.18.2 yamt SYSCTL_DECL(_hw_firewire);
143 1.4.18.2 yamt SYSCTL_NODE(_hw_firewire, OID_AUTO, fwip, CTLFLAG_RD, 0,
144 1.4.18.2 yamt "Firewire ip subsystem");
145 1.4.18.2 yamt SYSCTL_INT(_hw_firewire_fwip, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
146 1.4.18.2 yamt 0, "Length of the receive queue");
147 1.4.18.2 yamt
148 1.4.18.2 yamt TUNABLE_INT("hw.firewire.fwip.rx_queue_len", &rx_queue_len);
149 1.4.18.2 yamt #elif defined(__NetBSD__)
150 1.4.18.2 yamt MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over IEEE1394 interface");
151 1.4.18.2 yamt /*
152 1.4.18.2 yamt * Setup sysctl(3) MIB, hw.fwip.*
153 1.4.18.2 yamt *
154 1.4.18.2 yamt * TBD condition CTLFLAG_PERMANENT on being an LKM or not
155 1.4.18.2 yamt */
156 1.4.18.2 yamt SYSCTL_SETUP(sysctl_fwip, "sysctl fwip(4) subtree setup")
157 1.4.18.2 yamt {
158 1.4.18.2 yamt int rc, fwip_node_num;
159 1.4.18.2 yamt const struct sysctlnode *node;
160 1.4.18.2 yamt
161 1.4.18.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, NULL,
162 1.4.18.2 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
163 1.4.18.2 yamt NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
164 1.4.18.2 yamt goto err;
165 1.4.18.2 yamt }
166 1.4.18.2 yamt
167 1.4.18.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, &node,
168 1.4.18.2 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "fwip",
169 1.4.18.2 yamt SYSCTL_DESCR("fwip controls"),
170 1.4.18.2 yamt NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
171 1.4.18.2 yamt goto err;
172 1.4.18.2 yamt }
173 1.4.18.2 yamt fwip_node_num = node->sysctl_num;
174 1.4.18.2 yamt
175 1.4.18.2 yamt /* fwip RX queue length */
176 1.4.18.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, &node,
177 1.4.18.2 yamt CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
178 1.4.18.2 yamt "rx_queue_len", SYSCTL_DESCR("Length of the receive queue"),
179 1.4.18.2 yamt NULL, 0, &rx_queue_len,
180 1.4.18.2 yamt 0, CTL_HW, fwip_node_num, CTL_CREATE, CTL_EOL)) != 0) {
181 1.4.18.2 yamt goto err;
182 1.4.18.2 yamt }
183 1.4.18.2 yamt
184 1.4.18.2 yamt /* fwip RX queue length */
185 1.4.18.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, &node,
186 1.4.18.2 yamt CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
187 1.4.18.2 yamt "if_fwip_debug", SYSCTL_DESCR("fwip driver debug flag"),
188 1.4.18.2 yamt NULL, 0, &fwipdebug,
189 1.4.18.2 yamt 0, CTL_HW, fwip_node_num, CTL_CREATE, CTL_EOL)) != 0) {
190 1.4.18.2 yamt goto err;
191 1.4.18.2 yamt }
192 1.4.18.2 yamt
193 1.4.18.2 yamt return;
194 1.4.18.2 yamt
195 1.4.18.2 yamt err:
196 1.4.18.2 yamt printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
197 1.4.18.2 yamt }
198 1.4.18.2 yamt #endif
199 1.4.18.2 yamt
200 1.4.18.2 yamt #ifdef DEVICE_POLLING
201 1.4.18.2 yamt #define FWIP_POLL_REGISTER(func, fwip, ifp) \
202 1.4.18.2 yamt if (ether_poll_register(func, ifp)) { \
203 1.4.18.2 yamt struct firewire_comm *fc = (fwip)->fd.fc; \
204 1.4.18.2 yamt fc->set_intr(fc, 0); \
205 1.4.18.2 yamt }
206 1.4.18.2 yamt
207 1.4.18.2 yamt #define FWIP_POLL_DEREGISTER(fwip, ifp) \
208 1.4.18.2 yamt do { \
209 1.4.18.2 yamt struct firewire_comm *fc = (fwip)->fd.fc; \
210 1.4.18.2 yamt ether_poll_deregister(ifp); \
211 1.4.18.2 yamt fc->set_intr(fc, 1); \
212 1.4.18.2 yamt } while(0) \
213 1.4.18.2 yamt
214 1.4.18.2 yamt static poll_handler_t fwip_poll;
215 1.4.18.2 yamt
216 1.4.18.2 yamt static void
217 1.4.18.2 yamt fwip_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
218 1.4.18.2 yamt {
219 1.4.18.2 yamt struct fwip_softc *fwip;
220 1.4.18.2 yamt struct firewire_comm *fc;
221 1.4.18.2 yamt
222 1.4.18.2 yamt fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
223 1.4.18.2 yamt fc = fwip->fd.fc;
224 1.4.18.2 yamt if (cmd == POLL_DEREGISTER) {
225 1.4.18.2 yamt /* enable interrupts */
226 1.4.18.2 yamt fc->set_intr(fc, 1);
227 1.4.18.2 yamt return;
228 1.4.18.2 yamt }
229 1.4.18.2 yamt fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
230 1.4.18.2 yamt }
231 1.4.18.2 yamt #else
232 1.4.18.2 yamt #define FWIP_POLL_REGISTER(func, fwip, ifp)
233 1.4.18.2 yamt #define FWIP_POLL_DEREGISTER(fwip, ifp)
234 1.4.18.2 yamt #endif
235 1.4.18.2 yamt #if defined(__FreeBSD__)
236 1.4.18.2 yamt static void
237 1.4.18.2 yamt fwip_identify(driver_t *driver, device_t parent)
238 1.4.18.2 yamt {
239 1.4.18.2 yamt BUS_ADD_CHILD(parent, 0, "fwip", device_get_unit(parent));
240 1.4.18.2 yamt }
241 1.4.18.2 yamt
242 1.4.18.2 yamt static int
243 1.4.18.2 yamt fwip_probe(device_t dev)
244 1.4.18.2 yamt {
245 1.4.18.2 yamt device_t pa;
246 1.4.18.2 yamt
247 1.4.18.2 yamt pa = device_get_parent(dev);
248 1.4.18.2 yamt if(device_get_unit(dev) != device_get_unit(pa)){
249 1.4.18.2 yamt return(ENXIO);
250 1.4.18.2 yamt }
251 1.4.18.2 yamt
252 1.4.18.2 yamt device_set_desc(dev, "IP over FireWire");
253 1.4.18.2 yamt return (0);
254 1.4.18.2 yamt }
255 1.4.18.2 yamt #elif defined(__NetBSD__)
256 1.4.18.2 yamt int
257 1.4.18.2 yamt fwipmatch(struct device *parent, struct cfdata *cf, void *aux)
258 1.4.18.2 yamt {
259 1.4.18.2 yamt struct fw_attach_args *fwa = aux;
260 1.4.18.2 yamt
261 1.4.18.2 yamt if (strcmp(fwa->name, "fwip") == 0)
262 1.4.18.2 yamt return (1);
263 1.4.18.2 yamt return (0);
264 1.4.18.2 yamt }
265 1.4.18.2 yamt #endif
266 1.4.18.2 yamt
267 1.4.18.2 yamt FW_ATTACH(fwip)
268 1.4.18.2 yamt {
269 1.4.18.2 yamt FW_ATTACH_START(fwip, fwip, fwa);
270 1.4.18.2 yamt FWIP_ATTACH_START;
271 1.4.18.2 yamt struct ifnet *ifp;
272 1.4.18.2 yamt int s;
273 1.4.18.2 yamt
274 1.4.18.2 yamt FWIP_ATTACH_SETUP;
275 1.4.18.2 yamt
276 1.4.18.2 yamt /* XXX */
277 1.4.18.2 yamt fwip->dma_ch = -1;
278 1.4.18.2 yamt
279 1.4.18.2 yamt fwip->fd.fc = fwa->fc;
280 1.4.18.2 yamt if (tx_speed < 0)
281 1.4.18.2 yamt tx_speed = fwip->fd.fc->speed;
282 1.4.18.2 yamt
283 1.4.18.2 yamt fwip->fd.post_explore = NULL;
284 1.4.18.2 yamt fwip->fd.post_busreset = fwip_post_busreset;
285 1.4.18.2 yamt fwip->fw_softc.fwip = fwip;
286 1.4.18.2 yamt TASK_INIT(&fwip->start_send, 0, fwip_start_send, fwip);
287 1.4.18.2 yamt
288 1.4.18.2 yamt /*
289 1.4.18.2 yamt * Encode our hardware the way that arp likes it.
290 1.4.18.2 yamt */
291 1.4.18.2 yamt hwaddr->sender_unique_ID_hi = htonl(fwip->fd.fc->eui.hi);
292 1.4.18.2 yamt hwaddr->sender_unique_ID_lo = htonl(fwip->fd.fc->eui.lo);
293 1.4.18.2 yamt hwaddr->sender_max_rec = fwip->fd.fc->maxrec;
294 1.4.18.2 yamt hwaddr->sspd = fwip->fd.fc->speed;
295 1.4.18.2 yamt hwaddr->sender_unicast_FIFO_hi = htons((uint16_t)(INET_FIFO >> 32));
296 1.4.18.2 yamt hwaddr->sender_unicast_FIFO_lo = htonl((uint32_t)INET_FIFO);
297 1.4.18.2 yamt
298 1.4.18.2 yamt /* fill the rest and attach interface */
299 1.4.18.2 yamt ifp = &fwip->fwip_if;
300 1.4.18.2 yamt ifp->if_softc = &fwip->fw_softc;
301 1.4.18.2 yamt
302 1.4.18.2 yamt #if __FreeBSD_version >= 501113 || defined(__DragonFly__) || defined(__NetBSD__)
303 1.4.18.2 yamt IF_INITNAME(ifp, dev, unit);
304 1.4.18.2 yamt #else
305 1.4.18.2 yamt ifp->if_unit = unit;
306 1.4.18.2 yamt ifp->if_name = "fwip";
307 1.4.18.2 yamt #endif
308 1.4.18.2 yamt #if defined(__NetBSD__)
309 1.4.18.2 yamt IFQ_SET_READY(&ifp->if_snd);
310 1.4.18.2 yamt #endif
311 1.4.18.2 yamt SET_IFFUNC(ifp, fwip_start, fwip_ioctl, fwip_init, fwip_stop);
312 1.4.18.2 yamt ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|
313 1.4.18.2 yamt IFF_NEEDSGIANT);
314 1.4.18.2 yamt ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
315 1.4.18.2 yamt
316 1.4.18.2 yamt s = splfwnet();
317 1.4.18.2 yamt FIREWIRE_IFATTACH(ifp, hwaddr);
318 1.4.18.2 yamt splx(s);
319 1.4.18.2 yamt
320 1.4.18.2 yamt FWIPDEBUG(ifp, "interface created\n");
321 1.4.18.2 yamt FW_ATTACH_RETURN(0);
322 1.4.18.2 yamt }
323 1.4.18.2 yamt
324 1.4.18.2 yamt IF_STOP(fwip)
325 1.4.18.2 yamt {
326 1.4.18.2 yamt IF_STOP_START(fwip, ifp, fwip);
327 1.4.18.2 yamt struct firewire_comm *fc;
328 1.4.18.2 yamt struct fw_xferq *xferq;
329 1.4.18.2 yamt struct fw_xfer *xfer, *next;
330 1.4.18.2 yamt int i;
331 1.4.18.2 yamt
332 1.4.18.2 yamt fc = fwip->fd.fc;
333 1.4.18.2 yamt
334 1.4.18.2 yamt FWIP_POLL_DEREGISTER(fwip, ifp);
335 1.4.18.2 yamt
336 1.4.18.2 yamt if (fwip->dma_ch >= 0) {
337 1.4.18.2 yamt xferq = fc->ir[fwip->dma_ch];
338 1.4.18.2 yamt
339 1.4.18.2 yamt if (xferq->flag & FWXFERQ_RUNNING)
340 1.4.18.2 yamt fc->irx_disable(fc, fwip->dma_ch);
341 1.4.18.2 yamt xferq->flag &=
342 1.4.18.2 yamt ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
343 1.4.18.2 yamt FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
344 1.4.18.2 yamt xferq->hand = NULL;
345 1.4.18.2 yamt
346 1.4.18.2 yamt for (i = 0; i < xferq->bnchunk; i ++)
347 1.4.18.2 yamt m_freem(xferq->bulkxfer[i].mbuf);
348 1.4.18.2 yamt free(xferq->bulkxfer, M_FWIP);
349 1.4.18.2 yamt
350 1.4.18.2 yamt fw_bindremove(fc, &fwip->fwb);
351 1.4.18.2 yamt for (xfer = STAILQ_FIRST(&fwip->fwb.xferlist); xfer != NULL;
352 1.4.18.2 yamt xfer = next) {
353 1.4.18.2 yamt next = STAILQ_NEXT(xfer, link);
354 1.4.18.2 yamt fw_xfer_free(xfer);
355 1.4.18.2 yamt }
356 1.4.18.2 yamt
357 1.4.18.2 yamt for (xfer = STAILQ_FIRST(&fwip->xferlist); xfer != NULL;
358 1.4.18.2 yamt xfer = next) {
359 1.4.18.2 yamt next = STAILQ_NEXT(xfer, link);
360 1.4.18.2 yamt fw_xfer_free(xfer);
361 1.4.18.2 yamt }
362 1.4.18.2 yamt STAILQ_INIT(&fwip->xferlist);
363 1.4.18.2 yamt
364 1.4.18.2 yamt xferq->bulkxfer = NULL;
365 1.4.18.2 yamt fwip->dma_ch = -1;
366 1.4.18.2 yamt }
367 1.4.18.2 yamt
368 1.4.18.2 yamt ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
369 1.4.18.2 yamt }
370 1.4.18.2 yamt
371 1.4.18.2 yamt FW_DETACH(fwip)
372 1.4.18.2 yamt {
373 1.4.18.2 yamt IF_DETACH_START(fwip, fwip);
374 1.4.18.2 yamt int s;
375 1.4.18.2 yamt
376 1.4.18.2 yamt s = splfwnet();
377 1.4.18.2 yamt
378 1.4.18.2 yamt FWIP_STOP(fwip);
379 1.4.18.2 yamt FIREWIRE_IFDETACH(&fwip->fwip_if);
380 1.4.18.2 yamt
381 1.4.18.2 yamt splx(s);
382 1.4.18.2 yamt return 0;
383 1.4.18.2 yamt }
384 1.4.18.2 yamt
385 1.4.18.2 yamt #if defined(__NetBSD__)
386 1.4.18.2 yamt int
387 1.4.18.2 yamt fwipactivate(struct device *self, enum devact act)
388 1.4.18.2 yamt {
389 1.4.18.2 yamt struct fwip_softc *fwip = (struct fwip_softc *)self;
390 1.4.18.2 yamt int s, error = 0;
391 1.4.18.2 yamt
392 1.4.18.2 yamt s = splfwnet();
393 1.4.18.2 yamt switch (act) {
394 1.4.18.2 yamt case DVACT_ACTIVATE:
395 1.4.18.2 yamt error = EOPNOTSUPP;
396 1.4.18.2 yamt break;
397 1.4.18.2 yamt
398 1.4.18.2 yamt case DVACT_DEACTIVATE:
399 1.4.18.2 yamt if_deactivate(&fwip->fwip_if);
400 1.4.18.2 yamt break;
401 1.4.18.2 yamt }
402 1.4.18.2 yamt splx(s);
403 1.4.18.2 yamt
404 1.4.18.2 yamt return (error);
405 1.4.18.2 yamt }
406 1.4.18.2 yamt
407 1.4.18.2 yamt #endif
408 1.4.18.2 yamt IF_INIT(fwip)
409 1.4.18.2 yamt {
410 1.4.18.2 yamt IF_INIT_START(fwip, fwip, ifp);
411 1.4.18.2 yamt struct firewire_comm *fc;
412 1.4.18.2 yamt struct fw_xferq *xferq;
413 1.4.18.2 yamt struct fw_xfer *xfer;
414 1.4.18.2 yamt struct mbuf *m;
415 1.4.18.2 yamt int i;
416 1.4.18.2 yamt
417 1.4.18.2 yamt FWIPDEBUG(ifp, "initializing\n");
418 1.4.18.2 yamt
419 1.4.18.2 yamt fc = fwip->fd.fc;
420 1.4.18.2 yamt #define START 0
421 1.4.18.2 yamt if (fwip->dma_ch < 0) {
422 1.4.18.2 yamt for (i = START; i < fc->nisodma; i ++) {
423 1.4.18.2 yamt xferq = fc->ir[i];
424 1.4.18.2 yamt if ((xferq->flag & FWXFERQ_OPEN) == 0)
425 1.4.18.2 yamt goto found;
426 1.4.18.2 yamt }
427 1.4.18.2 yamt printf("no free dma channel\n");
428 1.4.18.2 yamt IF_INIT_RETURN(ENXIO);
429 1.4.18.2 yamt found:
430 1.4.18.2 yamt fwip->dma_ch = i;
431 1.4.18.2 yamt /* allocate DMA channel and init packet mode */
432 1.4.18.2 yamt xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
433 1.4.18.2 yamt FWXFERQ_HANDLER | FWXFERQ_STREAM;
434 1.4.18.2 yamt xferq->flag &= ~0xff;
435 1.4.18.2 yamt xferq->flag |= broadcast_channel & 0xff;
436 1.4.18.2 yamt /* register fwip_input handler */
437 1.4.18.2 yamt xferq->sc = (caddr_t) fwip;
438 1.4.18.2 yamt xferq->hand = fwip_stream_input;
439 1.4.18.2 yamt xferq->bnchunk = rx_queue_len;
440 1.4.18.2 yamt xferq->bnpacket = 1;
441 1.4.18.2 yamt xferq->psize = MCLBYTES;
442 1.4.18.2 yamt xferq->queued = 0;
443 1.4.18.2 yamt xferq->buf = NULL;
444 1.4.18.2 yamt xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
445 1.4.18.2 yamt sizeof(struct fw_bulkxfer) * xferq->bnchunk,
446 1.4.18.2 yamt M_FWIP, M_WAITOK);
447 1.4.18.2 yamt if (xferq->bulkxfer == NULL) {
448 1.4.18.2 yamt printf("if_fwip: malloc failed\n");
449 1.4.18.2 yamt IF_INIT_RETURN(ENOMEM);
450 1.4.18.2 yamt }
451 1.4.18.2 yamt STAILQ_INIT(&xferq->stvalid);
452 1.4.18.2 yamt STAILQ_INIT(&xferq->stfree);
453 1.4.18.2 yamt STAILQ_INIT(&xferq->stdma);
454 1.4.18.2 yamt xferq->stproc = NULL;
455 1.4.18.2 yamt for (i = 0; i < xferq->bnchunk; i ++) {
456 1.4.18.2 yamt m =
457 1.4.18.2 yamt #if defined(__DragonFly__) || __FreeBSD_version < 500000
458 1.4.18.2 yamt m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
459 1.4.18.2 yamt #else
460 1.4.18.2 yamt m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
461 1.4.18.2 yamt #endif
462 1.4.18.2 yamt xferq->bulkxfer[i].mbuf = m;
463 1.4.18.2 yamt if (m != NULL) {
464 1.4.18.2 yamt m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
465 1.4.18.2 yamt STAILQ_INSERT_TAIL(&xferq->stfree,
466 1.4.18.2 yamt &xferq->bulkxfer[i], link);
467 1.4.18.2 yamt } else
468 1.4.18.2 yamt printf("fwip_as_input: m_getcl failed\n");
469 1.4.18.2 yamt }
470 1.4.18.2 yamt
471 1.4.18.2 yamt fwip->fwb.start = INET_FIFO;
472 1.4.18.2 yamt fwip->fwb.end = INET_FIFO + 16384; /* S3200 packet size */
473 1.4.18.2 yamt
474 1.4.18.2 yamt /* pre-allocate xfer */
475 1.4.18.2 yamt STAILQ_INIT(&fwip->fwb.xferlist);
476 1.4.18.2 yamt for (i = 0; i < rx_queue_len; i ++) {
477 1.4.18.2 yamt xfer = fw_xfer_alloc(M_FWIP);
478 1.4.18.2 yamt if (xfer == NULL)
479 1.4.18.2 yamt break;
480 1.4.18.2 yamt m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
481 1.4.18.2 yamt xfer->recv.payload = mtod(m, uint32_t *);
482 1.4.18.2 yamt xfer->recv.pay_len = MCLBYTES;
483 1.4.18.2 yamt xfer->hand = fwip_unicast_input;
484 1.4.18.2 yamt xfer->fc = fc;
485 1.4.18.2 yamt xfer->sc = (caddr_t)fwip;
486 1.4.18.2 yamt xfer->mbuf = m;
487 1.4.18.2 yamt STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
488 1.4.18.2 yamt }
489 1.4.18.2 yamt fw_bindadd(fc, &fwip->fwb);
490 1.4.18.2 yamt
491 1.4.18.2 yamt STAILQ_INIT(&fwip->xferlist);
492 1.4.18.2 yamt for (i = 0; i < TX_MAX_QUEUE; i++) {
493 1.4.18.2 yamt xfer = fw_xfer_alloc(M_FWIP);
494 1.4.18.2 yamt if (xfer == NULL)
495 1.4.18.2 yamt break;
496 1.4.18.2 yamt xfer->send.spd = tx_speed;
497 1.4.18.2 yamt xfer->fc = fwip->fd.fc;
498 1.4.18.2 yamt xfer->sc = (caddr_t)fwip;
499 1.4.18.2 yamt xfer->hand = fwip_output_callback;
500 1.4.18.2 yamt STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
501 1.4.18.2 yamt }
502 1.4.18.2 yamt } else
503 1.4.18.2 yamt xferq = fc->ir[fwip->dma_ch];
504 1.4.18.2 yamt
505 1.4.18.2 yamt fwip->last_dest.hi = 0;
506 1.4.18.2 yamt fwip->last_dest.lo = 0;
507 1.4.18.2 yamt
508 1.4.18.2 yamt /* start dma */
509 1.4.18.2 yamt if ((xferq->flag & FWXFERQ_RUNNING) == 0)
510 1.4.18.2 yamt fc->irx_enable(fc, fwip->dma_ch);
511 1.4.18.2 yamt
512 1.4.18.2 yamt ifp->if_flags |= IFF_RUNNING;
513 1.4.18.2 yamt ifp->if_flags &= ~IFF_OACTIVE;
514 1.4.18.2 yamt
515 1.4.18.2 yamt FWIP_POLL_REGISTER(fwip_poll, fwip, ifp);
516 1.4.18.2 yamt #if 0
517 1.4.18.2 yamt /* attempt to start output */
518 1.4.18.2 yamt fwip_start(ifp);
519 1.4.18.2 yamt #endif
520 1.4.18.2 yamt IF_INIT_RETURN(0);
521 1.4.18.2 yamt }
522 1.4.18.2 yamt
523 1.4.18.2 yamt static int
524 1.4.18.2 yamt fwip_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
525 1.4.18.2 yamt {
526 1.4.18.2 yamt IF_IOCTL_START(fwip, fwip);
527 1.4.18.2 yamt int s, error;
528 1.4.18.2 yamt
529 1.4.18.2 yamt switch (cmd) {
530 1.4.18.2 yamt case SIOCSIFFLAGS:
531 1.4.18.2 yamt s = splfwnet();
532 1.4.18.2 yamt if (ifp->if_flags & IFF_UP) {
533 1.4.18.2 yamt if (!(ifp->if_flags & IFF_RUNNING))
534 1.4.18.2 yamt FWIP_INIT(fwip);
535 1.4.18.2 yamt } else {
536 1.4.18.2 yamt if (ifp->if_flags & IFF_RUNNING)
537 1.4.18.2 yamt FWIP_STOP(fwip);
538 1.4.18.2 yamt }
539 1.4.18.2 yamt splx(s);
540 1.4.18.2 yamt break;
541 1.4.18.2 yamt case SIOCADDMULTI:
542 1.4.18.2 yamt case SIOCDELMULTI:
543 1.4.18.2 yamt break;
544 1.4.18.2 yamt
545 1.4.18.2 yamt #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__NetBSD__)
546 1.4.18.2 yamt default:
547 1.4.18.2 yamt #else
548 1.4.18.2 yamt case SIOCSIFADDR:
549 1.4.18.2 yamt case SIOCGIFADDR:
550 1.4.18.2 yamt case SIOCSIFMTU:
551 1.4.18.2 yamt #endif
552 1.4.18.2 yamt s = splfwnet();
553 1.4.18.2 yamt error = FIREWIRE_IOCTL(ifp, cmd, data);
554 1.4.18.2 yamt splx(s);
555 1.4.18.2 yamt return (error);
556 1.4.18.2 yamt #if defined(__DragonFly__) || \
557 1.4.18.2 yamt (defined(__FreeBSD__) && __FreeBSD_version < 500000)
558 1.4.18.2 yamt default:
559 1.4.18.2 yamt return (EINVAL);
560 1.4.18.2 yamt #endif
561 1.4.18.2 yamt }
562 1.4.18.2 yamt
563 1.4.18.2 yamt return (0);
564 1.4.18.2 yamt }
565 1.4.18.2 yamt
566 1.4.18.2 yamt static void
567 1.4.18.2 yamt fwip_post_busreset(void *arg)
568 1.4.18.2 yamt {
569 1.4.18.2 yamt struct fwip_softc *fwip = arg;
570 1.4.18.2 yamt struct crom_src *src;
571 1.4.18.2 yamt struct crom_chunk *root;
572 1.4.18.2 yamt
573 1.4.18.2 yamt src = fwip->fd.fc->crom_src;
574 1.4.18.2 yamt root = fwip->fd.fc->crom_root;
575 1.4.18.2 yamt
576 1.4.18.2 yamt /* RFC2734 IPv4 over IEEE1394 */
577 1.4.18.2 yamt bzero(&fwip->unit4, sizeof(struct crom_chunk));
578 1.4.18.2 yamt crom_add_chunk(src, root, &fwip->unit4, CROM_UDIR);
579 1.4.18.2 yamt crom_add_entry(&fwip->unit4, CSRKEY_SPEC, CSRVAL_IETF);
580 1.4.18.2 yamt crom_add_simple_text(src, &fwip->unit4, &fwip->spec4, "IANA");
581 1.4.18.2 yamt crom_add_entry(&fwip->unit4, CSRKEY_VER, 1);
582 1.4.18.2 yamt crom_add_simple_text(src, &fwip->unit4, &fwip->ver4, "IPv4");
583 1.4.18.2 yamt
584 1.4.18.2 yamt /* RFC3146 IPv6 over IEEE1394 */
585 1.4.18.2 yamt bzero(&fwip->unit6, sizeof(struct crom_chunk));
586 1.4.18.2 yamt crom_add_chunk(src, root, &fwip->unit6, CROM_UDIR);
587 1.4.18.2 yamt crom_add_entry(&fwip->unit6, CSRKEY_SPEC, CSRVAL_IETF);
588 1.4.18.2 yamt crom_add_simple_text(src, &fwip->unit6, &fwip->spec6, "IANA");
589 1.4.18.2 yamt crom_add_entry(&fwip->unit6, CSRKEY_VER, 2);
590 1.4.18.2 yamt crom_add_simple_text(src, &fwip->unit6, &fwip->ver6, "IPv6");
591 1.4.18.2 yamt
592 1.4.18.2 yamt fwip->last_dest.hi = 0;
593 1.4.18.2 yamt fwip->last_dest.lo = 0;
594 1.4.18.2 yamt FIREWIRE_BUSRESET(&fwip->fwip_if);
595 1.4.18.2 yamt }
596 1.4.18.2 yamt
597 1.4.18.2 yamt static void
598 1.4.18.2 yamt fwip_output_callback(struct fw_xfer *xfer)
599 1.4.18.2 yamt {
600 1.4.18.2 yamt struct fwip_softc *fwip;
601 1.4.18.2 yamt struct ifnet *ifp;
602 1.4.18.2 yamt int s;
603 1.4.18.2 yamt
604 1.4.18.2 yamt GIANT_REQUIRED;
605 1.4.18.2 yamt
606 1.4.18.2 yamt fwip = (struct fwip_softc *)xfer->sc;
607 1.4.18.2 yamt ifp = &fwip->fwip_if;
608 1.4.18.2 yamt /* XXX error check */
609 1.4.18.2 yamt FWIPDEBUG(ifp, "resp = %d\n", xfer->resp);
610 1.4.18.2 yamt if (xfer->resp != 0)
611 1.4.18.2 yamt ifp->if_oerrors ++;
612 1.4.18.2 yamt
613 1.4.18.2 yamt m_freem(xfer->mbuf);
614 1.4.18.2 yamt fw_xfer_unload(xfer);
615 1.4.18.2 yamt
616 1.4.18.2 yamt s = splfwnet();
617 1.4.18.2 yamt STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
618 1.4.18.2 yamt splx(s);
619 1.4.18.2 yamt
620 1.4.18.2 yamt /* for queue full */
621 1.4.18.2 yamt if (ifp->if_snd.ifq_head != NULL)
622 1.4.18.2 yamt fwip_start(ifp);
623 1.4.18.2 yamt }
624 1.4.18.2 yamt
625 1.4.18.2 yamt static void
626 1.4.18.2 yamt fwip_start(struct ifnet *ifp)
627 1.4.18.2 yamt {
628 1.4.18.2 yamt struct fwip_softc *fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
629 1.4.18.2 yamt int s;
630 1.4.18.2 yamt
631 1.4.18.2 yamt GIANT_REQUIRED;
632 1.4.18.2 yamt
633 1.4.18.2 yamt FWIPDEBUG(ifp, "starting\n");
634 1.4.18.2 yamt
635 1.4.18.2 yamt if (fwip->dma_ch < 0) {
636 1.4.18.2 yamt struct mbuf *m = NULL;
637 1.4.18.2 yamt
638 1.4.18.2 yamt FWIPDEBUG(ifp, "not ready\n");
639 1.4.18.2 yamt
640 1.4.18.2 yamt s = splfwnet();
641 1.4.18.2 yamt do {
642 1.4.18.2 yamt IF_DEQUEUE(&ifp->if_snd, m);
643 1.4.18.2 yamt if (m != NULL)
644 1.4.18.2 yamt m_freem(m);
645 1.4.18.2 yamt ifp->if_oerrors ++;
646 1.4.18.2 yamt } while (m != NULL);
647 1.4.18.2 yamt splx(s);
648 1.4.18.2 yamt
649 1.4.18.2 yamt return;
650 1.4.18.2 yamt }
651 1.4.18.2 yamt
652 1.4.18.2 yamt s = splfwnet();
653 1.4.18.2 yamt ifp->if_flags |= IFF_OACTIVE;
654 1.4.18.2 yamt
655 1.4.18.2 yamt if (ifp->if_snd.ifq_len != 0)
656 1.4.18.2 yamt fwip_async_output(fwip, ifp);
657 1.4.18.2 yamt
658 1.4.18.2 yamt ifp->if_flags &= ~IFF_OACTIVE;
659 1.4.18.2 yamt splx(s);
660 1.4.18.2 yamt }
661 1.4.18.2 yamt
662 1.4.18.2 yamt /* Async. stream output */
663 1.4.18.2 yamt static void
664 1.4.18.2 yamt fwip_async_output(struct fwip_softc *fwip, struct ifnet *ifp)
665 1.4.18.2 yamt {
666 1.4.18.2 yamt struct firewire_comm *fc = fwip->fd.fc;
667 1.4.18.2 yamt struct mbuf *m;
668 1.4.18.2 yamt struct m_tag *mtag;
669 1.4.18.2 yamt struct fw_hwaddr *destfw;
670 1.4.18.2 yamt struct fw_xfer *xfer;
671 1.4.18.2 yamt struct fw_xferq *xferq;
672 1.4.18.2 yamt struct fw_pkt *fp;
673 1.4.18.2 yamt uint16_t nodeid;
674 1.4.18.2 yamt int error;
675 1.4.18.2 yamt int i = 0;
676 1.4.18.2 yamt
677 1.4.18.2 yamt GIANT_REQUIRED;
678 1.4.18.2 yamt
679 1.4.18.2 yamt xfer = NULL;
680 1.4.18.2 yamt xferq = fwip->fd.fc->atq;
681 1.4.18.2 yamt while (xferq->queued < xferq->maxq - 1) {
682 1.4.18.2 yamt xfer = STAILQ_FIRST(&fwip->xferlist);
683 1.4.18.2 yamt if (xfer == NULL) {
684 1.4.18.2 yamt printf("if_fwip: lack of xfer\n");
685 1.4.18.2 yamt return;
686 1.4.18.2 yamt }
687 1.4.18.2 yamt IF_DEQUEUE(&ifp->if_snd, m);
688 1.4.18.2 yamt if (m == NULL)
689 1.4.18.2 yamt break;
690 1.4.18.2 yamt
691 1.4.18.2 yamt /*
692 1.4.18.2 yamt * Dig out the link-level address which
693 1.4.18.2 yamt * firewire_output got via arp or neighbour
694 1.4.18.2 yamt * discovery. If we don't have a link-level address,
695 1.4.18.2 yamt * just stick the thing on the broadcast channel.
696 1.4.18.2 yamt */
697 1.4.18.2 yamt mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, 0);
698 1.4.18.2 yamt if (mtag == NULL)
699 1.4.18.2 yamt destfw = 0;
700 1.4.18.2 yamt else
701 1.4.18.2 yamt destfw = (struct fw_hwaddr *) (mtag + 1);
702 1.4.18.2 yamt
703 1.4.18.2 yamt STAILQ_REMOVE_HEAD(&fwip->xferlist, link);
704 1.4.18.2 yamt
705 1.4.18.2 yamt /*
706 1.4.18.2 yamt * We don't do any bpf stuff here - the generic code
707 1.4.18.2 yamt * in firewire_output gives the packet to bpf before
708 1.4.18.2 yamt * it adds the link-level encapsulation.
709 1.4.18.2 yamt */
710 1.4.18.2 yamt
711 1.4.18.2 yamt /*
712 1.4.18.2 yamt * Put the mbuf in the xfer early in case we hit an
713 1.4.18.2 yamt * error case below - fwip_output_callback will free
714 1.4.18.2 yamt * the mbuf.
715 1.4.18.2 yamt */
716 1.4.18.2 yamt xfer->mbuf = m;
717 1.4.18.2 yamt
718 1.4.18.2 yamt /*
719 1.4.18.2 yamt * We use the arp result (if any) to add a suitable firewire
720 1.4.18.2 yamt * packet header before handing off to the bus.
721 1.4.18.2 yamt */
722 1.4.18.2 yamt fp = &xfer->send.hdr;
723 1.4.18.2 yamt nodeid = FWLOCALBUS | fc->nodeid;
724 1.4.18.2 yamt if ((m->m_flags & M_BCAST) || !destfw) {
725 1.4.18.2 yamt /*
726 1.4.18.2 yamt * Broadcast packets are sent as GASP packets with
727 1.4.18.2 yamt * specifier ID 0x00005e, version 1 on the broadcast
728 1.4.18.2 yamt * channel. To be conservative, we send at the
729 1.4.18.2 yamt * slowest possible speed.
730 1.4.18.2 yamt */
731 1.4.18.2 yamt uint32_t *p;
732 1.4.18.2 yamt
733 1.4.18.2 yamt M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
734 1.4.18.2 yamt p = mtod(m, uint32_t *);
735 1.4.18.2 yamt fp->mode.stream.len = m->m_pkthdr.len;
736 1.4.18.2 yamt fp->mode.stream.chtag = broadcast_channel;
737 1.4.18.2 yamt fp->mode.stream.tcode = FWTCODE_STREAM;
738 1.4.18.2 yamt fp->mode.stream.sy = 0;
739 1.4.18.2 yamt xfer->send.spd = 0;
740 1.4.18.2 yamt p[0] = htonl(nodeid << 16);
741 1.4.18.2 yamt p[1] = htonl((0x5e << 24) | 1);
742 1.4.18.2 yamt } else {
743 1.4.18.2 yamt /*
744 1.4.18.2 yamt * Unicast packets are sent as block writes to the
745 1.4.18.2 yamt * target's unicast fifo address. If we can't
746 1.4.18.2 yamt * find the node address, we just give up. We
747 1.4.18.2 yamt * could broadcast it but that might overflow
748 1.4.18.2 yamt * the packet size limitations due to the
749 1.4.18.2 yamt * extra GASP header. Note: the hardware
750 1.4.18.2 yamt * address is stored in network byte order to
751 1.4.18.2 yamt * make life easier for ARP.
752 1.4.18.2 yamt */
753 1.4.18.2 yamt struct fw_device *fd;
754 1.4.18.2 yamt struct fw_eui64 eui;
755 1.4.18.2 yamt
756 1.4.18.2 yamt eui.hi = ntohl(destfw->sender_unique_ID_hi);
757 1.4.18.2 yamt eui.lo = ntohl(destfw->sender_unique_ID_lo);
758 1.4.18.2 yamt if (fwip->last_dest.hi != eui.hi ||
759 1.4.18.2 yamt fwip->last_dest.lo != eui.lo) {
760 1.4.18.2 yamt fd = fw_noderesolve_eui64(fc, &eui);
761 1.4.18.2 yamt if (!fd) {
762 1.4.18.2 yamt /* error */
763 1.4.18.2 yamt ifp->if_oerrors ++;
764 1.4.18.2 yamt /* XXX set error code */
765 1.4.18.2 yamt fwip_output_callback(xfer);
766 1.4.18.2 yamt continue;
767 1.4.18.2 yamt
768 1.4.18.2 yamt }
769 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.dst = FWLOCALBUS | fd->dst;
770 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.tlrt = 0;
771 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.tcode = FWTCODE_WREQB;
772 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.pri = 0;
773 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.src = nodeid;
774 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.dest_hi =
775 1.4.18.2 yamt ntohs(destfw->sender_unicast_FIFO_hi);
776 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.dest_lo =
777 1.4.18.2 yamt ntohl(destfw->sender_unicast_FIFO_lo);
778 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.extcode = 0;
779 1.4.18.2 yamt fwip->last_dest = eui;
780 1.4.18.2 yamt }
781 1.4.18.2 yamt
782 1.4.18.2 yamt fp->mode.wreqb = fwip->last_hdr.mode.wreqb;
783 1.4.18.2 yamt fp->mode.wreqb.len = m->m_pkthdr.len;
784 1.4.18.2 yamt xfer->send.spd = min(destfw->sspd, fc->speed);
785 1.4.18.2 yamt }
786 1.4.18.2 yamt
787 1.4.18.2 yamt xfer->send.pay_len = m->m_pkthdr.len;
788 1.4.18.2 yamt
789 1.4.18.2 yamt error = fw_asyreq(fc, -1, xfer);
790 1.4.18.2 yamt if (error == EAGAIN) {
791 1.4.18.2 yamt /*
792 1.4.18.2 yamt * We ran out of tlabels - requeue the packet
793 1.4.18.2 yamt * for later transmission.
794 1.4.18.2 yamt */
795 1.4.18.2 yamt xfer->mbuf = 0;
796 1.4.18.2 yamt STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
797 1.4.18.2 yamt IF_PREPEND(&ifp->if_snd, m);
798 1.4.18.2 yamt break;
799 1.4.18.2 yamt }
800 1.4.18.2 yamt if (error) {
801 1.4.18.2 yamt /* error */
802 1.4.18.2 yamt ifp->if_oerrors ++;
803 1.4.18.2 yamt /* XXX set error code */
804 1.4.18.2 yamt fwip_output_callback(xfer);
805 1.4.18.2 yamt continue;
806 1.4.18.2 yamt } else {
807 1.4.18.2 yamt ifp->if_opackets ++;
808 1.4.18.2 yamt i++;
809 1.4.18.2 yamt }
810 1.4.18.2 yamt }
811 1.4.18.2 yamt #if 0
812 1.4.18.2 yamt if (i > 1)
813 1.4.18.2 yamt printf("%d queued\n", i);
814 1.4.18.2 yamt #endif
815 1.4.18.2 yamt if (i > 0) {
816 1.4.18.2 yamt #if 1
817 1.4.18.2 yamt xferq->start(fc);
818 1.4.18.2 yamt #else
819 1.4.18.2 yamt taskqueue_enqueue(taskqueue_swi_giant, &fwip->start_send);
820 1.4.18.2 yamt #endif
821 1.4.18.2 yamt }
822 1.4.18.2 yamt }
823 1.4.18.2 yamt
824 1.4.18.2 yamt #if defined(__FreeBSD__)
825 1.4.18.2 yamt static void
826 1.4.18.2 yamt fwip_start_send (void *arg, int count)
827 1.4.18.2 yamt {
828 1.4.18.2 yamt struct fwip_softc *fwip = arg;
829 1.4.18.2 yamt
830 1.4.18.2 yamt GIANT_REQUIRED;
831 1.4.18.2 yamt fwip->fd.fc->atq->start(fwip->fd.fc);
832 1.4.18.2 yamt }
833 1.4.18.2 yamt #endif
834 1.4.18.2 yamt
835 1.4.18.2 yamt /* Async. stream output */
836 1.4.18.2 yamt static void
837 1.4.18.2 yamt fwip_stream_input(struct fw_xferq *xferq)
838 1.4.18.2 yamt {
839 1.4.18.2 yamt struct mbuf *m, *m0;
840 1.4.18.2 yamt struct m_tag *mtag;
841 1.4.18.2 yamt struct ifnet *ifp;
842 1.4.18.2 yamt struct fwip_softc *fwip;
843 1.4.18.2 yamt struct fw_bulkxfer *sxfer;
844 1.4.18.2 yamt struct fw_pkt *fp;
845 1.4.18.2 yamt uint16_t src;
846 1.4.18.2 yamt uint32_t *p;
847 1.4.18.2 yamt
848 1.4.18.2 yamt GIANT_REQUIRED;
849 1.4.18.2 yamt
850 1.4.18.2 yamt fwip = (struct fwip_softc *)xferq->sc;
851 1.4.18.2 yamt ifp = &fwip->fwip_if;
852 1.4.18.2 yamt #if 0
853 1.4.18.2 yamt FWIP_POLL_REGISTER(fwip_poll, fwip, ifp);
854 1.4.18.2 yamt #endif
855 1.4.18.2 yamt while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
856 1.4.18.2 yamt STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
857 1.4.18.2 yamt fp = mtod(sxfer->mbuf, struct fw_pkt *);
858 1.4.18.2 yamt if (fwip->fd.fc->irx_post != NULL)
859 1.4.18.2 yamt fwip->fd.fc->irx_post(fwip->fd.fc, fp->mode.ld);
860 1.4.18.2 yamt m = sxfer->mbuf;
861 1.4.18.2 yamt
862 1.4.18.2 yamt /* insert new rbuf */
863 1.4.18.2 yamt sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
864 1.4.18.2 yamt if (m0 != NULL) {
865 1.4.18.2 yamt m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
866 1.4.18.2 yamt STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
867 1.4.18.2 yamt } else
868 1.4.18.2 yamt printf("fwip_as_input: m_getcl failed\n");
869 1.4.18.2 yamt
870 1.4.18.2 yamt /*
871 1.4.18.2 yamt * We must have a GASP header - leave the
872 1.4.18.2 yamt * encapsulation sanity checks to the generic
873 1.4.18.2 yamt * code. Remeber that we also have the firewire async
874 1.4.18.2 yamt * stream header even though that isn't accounted for
875 1.4.18.2 yamt * in mode.stream.len.
876 1.4.18.2 yamt */
877 1.4.18.2 yamt if (sxfer->resp != 0 || fp->mode.stream.len <
878 1.4.18.2 yamt 2*sizeof(uint32_t)) {
879 1.4.18.2 yamt m_freem(m);
880 1.4.18.2 yamt ifp->if_ierrors ++;
881 1.4.18.2 yamt continue;
882 1.4.18.2 yamt }
883 1.4.18.2 yamt m->m_len = m->m_pkthdr.len = fp->mode.stream.len
884 1.4.18.2 yamt + sizeof(fp->mode.stream);
885 1.4.18.2 yamt
886 1.4.18.2 yamt /*
887 1.4.18.2 yamt * If we received the packet on the broadcast channel,
888 1.4.18.2 yamt * mark it as broadcast, otherwise we assume it must
889 1.4.18.2 yamt * be multicast.
890 1.4.18.2 yamt */
891 1.4.18.2 yamt if (fp->mode.stream.chtag == broadcast_channel)
892 1.4.18.2 yamt m->m_flags |= M_BCAST;
893 1.4.18.2 yamt else
894 1.4.18.2 yamt m->m_flags |= M_MCAST;
895 1.4.18.2 yamt
896 1.4.18.2 yamt /*
897 1.4.18.2 yamt * Make sure we recognise the GASP specifier and
898 1.4.18.2 yamt * version.
899 1.4.18.2 yamt */
900 1.4.18.2 yamt p = mtod(m, uint32_t *);
901 1.4.18.2 yamt if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) != 0x00005e
902 1.4.18.2 yamt || (ntohl(p[2]) & 0xffffff) != 1) {
903 1.4.18.2 yamt FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n",
904 1.4.18.2 yamt ntohl(p[1]), ntohl(p[2]));
905 1.4.18.2 yamt m_freem(m);
906 1.4.18.2 yamt ifp->if_ierrors ++;
907 1.4.18.2 yamt continue;
908 1.4.18.2 yamt }
909 1.4.18.2 yamt
910 1.4.18.2 yamt /*
911 1.4.18.2 yamt * Record the sender ID for possible BPF usage.
912 1.4.18.2 yamt */
913 1.4.18.2 yamt src = ntohl(p[1]) >> 16;
914 1.4.18.2 yamt if (ifp->if_bpf) {
915 1.4.18.2 yamt mtag = m_tag_alloc(MTAG_FIREWIRE,
916 1.4.18.2 yamt MTAG_FIREWIRE_SENDER_EUID,
917 1.4.18.2 yamt 2*sizeof(uint32_t), M_NOWAIT);
918 1.4.18.2 yamt if (mtag) {
919 1.4.18.2 yamt /* bpf wants it in network byte order */
920 1.4.18.2 yamt struct fw_device *fd;
921 1.4.18.2 yamt uint32_t *p2 = (uint32_t *) (mtag + 1);
922 1.4.18.2 yamt fd = fw_noderesolve_nodeid(fwip->fd.fc,
923 1.4.18.2 yamt src & 0x3f);
924 1.4.18.2 yamt if (fd) {
925 1.4.18.2 yamt p2[0] = htonl(fd->eui.hi);
926 1.4.18.2 yamt p2[1] = htonl(fd->eui.lo);
927 1.4.18.2 yamt } else {
928 1.4.18.2 yamt p2[0] = 0;
929 1.4.18.2 yamt p2[1] = 0;
930 1.4.18.2 yamt }
931 1.4.18.2 yamt m_tag_prepend(m, mtag);
932 1.4.18.2 yamt }
933 1.4.18.2 yamt }
934 1.4.18.2 yamt
935 1.4.18.2 yamt /*
936 1.4.18.2 yamt * Trim off the GASP header
937 1.4.18.2 yamt */
938 1.4.18.2 yamt m_adj(m, 3*sizeof(uint32_t));
939 1.4.18.2 yamt m->m_pkthdr.rcvif = ifp;
940 1.4.18.2 yamt FIREWIRE_INPUT(ifp, m, src);
941 1.4.18.2 yamt ifp->if_ipackets ++;
942 1.4.18.2 yamt }
943 1.4.18.2 yamt if (STAILQ_FIRST(&xferq->stfree) != NULL)
944 1.4.18.2 yamt fwip->fd.fc->irx_enable(fwip->fd.fc, fwip->dma_ch);
945 1.4.18.2 yamt }
946 1.4.18.2 yamt
947 1.4.18.2 yamt static inline void
948 1.4.18.2 yamt fwip_unicast_input_recycle(struct fwip_softc *fwip, struct fw_xfer *xfer)
949 1.4.18.2 yamt {
950 1.4.18.2 yamt struct mbuf *m;
951 1.4.18.2 yamt
952 1.4.18.2 yamt GIANT_REQUIRED;
953 1.4.18.2 yamt
954 1.4.18.2 yamt /*
955 1.4.18.2 yamt * We have finished with a unicast xfer. Allocate a new
956 1.4.18.2 yamt * cluster and stick it on the back of the input queue.
957 1.4.18.2 yamt */
958 1.4.18.2 yamt m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
959 1.4.18.2 yamt if (m == NULL)
960 1.4.18.2 yamt printf("fwip_unicast_input_recycle: m_getcl failed\n");
961 1.4.18.2 yamt xfer->mbuf = m;
962 1.4.18.2 yamt xfer->recv.payload = mtod(m, uint32_t *);
963 1.4.18.2 yamt xfer->recv.pay_len = MCLBYTES;
964 1.4.18.2 yamt xfer->mbuf = m;
965 1.4.18.2 yamt STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
966 1.4.18.2 yamt }
967 1.4.18.2 yamt
968 1.4.18.2 yamt static void
969 1.4.18.2 yamt fwip_unicast_input(struct fw_xfer *xfer)
970 1.4.18.2 yamt {
971 1.4.18.2 yamt uint64_t address;
972 1.4.18.2 yamt struct mbuf *m;
973 1.4.18.2 yamt struct m_tag *mtag;
974 1.4.18.2 yamt struct ifnet *ifp;
975 1.4.18.2 yamt struct fwip_softc *fwip;
976 1.4.18.2 yamt struct fw_pkt *fp;
977 1.4.18.2 yamt //struct fw_pkt *sfp;
978 1.4.18.2 yamt int rtcode;
979 1.4.18.2 yamt
980 1.4.18.2 yamt GIANT_REQUIRED;
981 1.4.18.2 yamt
982 1.4.18.2 yamt fwip = (struct fwip_softc *)xfer->sc;
983 1.4.18.2 yamt ifp = &fwip->fwip_if;
984 1.4.18.2 yamt m = xfer->mbuf;
985 1.4.18.2 yamt xfer->mbuf = 0;
986 1.4.18.2 yamt fp = &xfer->recv.hdr;
987 1.4.18.2 yamt
988 1.4.18.2 yamt /*
989 1.4.18.2 yamt * Check the fifo address - we only accept addresses of
990 1.4.18.2 yamt * exactly INET_FIFO.
991 1.4.18.2 yamt */
992 1.4.18.2 yamt address = ((uint64_t)fp->mode.wreqb.dest_hi << 32)
993 1.4.18.2 yamt | fp->mode.wreqb.dest_lo;
994 1.4.18.2 yamt if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
995 1.4.18.2 yamt rtcode = FWRCODE_ER_TYPE;
996 1.4.18.2 yamt } else if (address != INET_FIFO) {
997 1.4.18.2 yamt rtcode = FWRCODE_ER_ADDR;
998 1.4.18.2 yamt } else {
999 1.4.18.2 yamt rtcode = FWRCODE_COMPLETE;
1000 1.4.18.2 yamt }
1001 1.4.18.2 yamt
1002 1.4.18.2 yamt /*
1003 1.4.18.2 yamt * Pick up a new mbuf and stick it on the back of the receive
1004 1.4.18.2 yamt * queue.
1005 1.4.18.2 yamt */
1006 1.4.18.2 yamt fwip_unicast_input_recycle(fwip, xfer);
1007 1.4.18.2 yamt
1008 1.4.18.2 yamt /*
1009 1.4.18.2 yamt * If we've already rejected the packet, give up now.
1010 1.4.18.2 yamt */
1011 1.4.18.2 yamt if (rtcode != FWRCODE_COMPLETE) {
1012 1.4.18.2 yamt m_freem(m);
1013 1.4.18.2 yamt ifp->if_ierrors ++;
1014 1.4.18.2 yamt return;
1015 1.4.18.2 yamt }
1016 1.4.18.2 yamt
1017 1.4.18.2 yamt if (ifp->if_bpf) {
1018 1.4.18.2 yamt /*
1019 1.4.18.2 yamt * Record the sender ID for possible BPF usage.
1020 1.4.18.2 yamt */
1021 1.4.18.2 yamt mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID,
1022 1.4.18.2 yamt 2*sizeof(uint32_t), M_NOWAIT);
1023 1.4.18.2 yamt if (mtag) {
1024 1.4.18.2 yamt /* bpf wants it in network byte order */
1025 1.4.18.2 yamt struct fw_device *fd;
1026 1.4.18.2 yamt uint32_t *p = (uint32_t *) (mtag + 1);
1027 1.4.18.2 yamt fd = fw_noderesolve_nodeid(fwip->fd.fc,
1028 1.4.18.2 yamt fp->mode.wreqb.src & 0x3f);
1029 1.4.18.2 yamt if (fd) {
1030 1.4.18.2 yamt p[0] = htonl(fd->eui.hi);
1031 1.4.18.2 yamt p[1] = htonl(fd->eui.lo);
1032 1.4.18.2 yamt } else {
1033 1.4.18.2 yamt p[0] = 0;
1034 1.4.18.2 yamt p[1] = 0;
1035 1.4.18.2 yamt }
1036 1.4.18.2 yamt m_tag_prepend(m, mtag);
1037 1.4.18.2 yamt }
1038 1.4.18.2 yamt }
1039 1.4.18.2 yamt
1040 1.4.18.2 yamt /*
1041 1.4.18.2 yamt * Hand off to the generic encapsulation code. We don't use
1042 1.4.18.2 yamt * ifp->if_input so that we can pass the source nodeid as an
1043 1.4.18.2 yamt * argument to facilitate link-level fragment reassembly.
1044 1.4.18.2 yamt */
1045 1.4.18.2 yamt m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len;
1046 1.4.18.2 yamt m->m_pkthdr.rcvif = ifp;
1047 1.4.18.2 yamt FIREWIRE_INPUT(ifp, m, fp->mode.wreqb.src);
1048 1.4.18.2 yamt ifp->if_ipackets ++;
1049 1.4.18.2 yamt }
1050 1.4.18.2 yamt
1051 1.4.18.2 yamt #if defined(__FreeBSD__)
1052 1.4.18.2 yamt static devclass_t fwip_devclass;
1053 1.4.18.2 yamt
1054 1.4.18.2 yamt static device_method_t fwip_methods[] = {
1055 1.4.18.2 yamt /* device interface */
1056 1.4.18.2 yamt DEVMETHOD(device_identify, fwip_identify),
1057 1.4.18.2 yamt DEVMETHOD(device_probe, fwip_probe),
1058 1.4.18.2 yamt DEVMETHOD(device_attach, fwip_attach),
1059 1.4.18.2 yamt DEVMETHOD(device_detach, fwip_detach),
1060 1.4.18.2 yamt { 0, 0 }
1061 1.4.18.2 yamt };
1062 1.4.18.2 yamt
1063 1.4.18.2 yamt static driver_t fwip_driver = {
1064 1.4.18.2 yamt "fwip",
1065 1.4.18.2 yamt fwip_methods,
1066 1.4.18.2 yamt sizeof(struct fwip_softc),
1067 1.4.18.2 yamt };
1068 1.4.18.2 yamt
1069 1.4.18.2 yamt
1070 1.4.18.2 yamt #ifdef __DragonFly__
1071 1.4.18.2 yamt DECLARE_DUMMY_MODULE(fwip);
1072 1.4.18.2 yamt #endif
1073 1.4.18.2 yamt DRIVER_MODULE(fwip, firewire, fwip_driver, fwip_devclass, 0, 0);
1074 1.4.18.2 yamt MODULE_VERSION(fwip, 1);
1075 1.4.18.2 yamt MODULE_DEPEND(fwip, firewire, 1, 1, 1);
1076 1.4.18.2 yamt #elif defined(__NetBSD__)
1077 1.4.18.2 yamt CFATTACH_DECL(fwip, sizeof (struct fwip_softc),
1078 1.4.18.2 yamt fwipmatch, fwipattach, fwipdetach, NULL);
1079 1.4.18.2 yamt #endif
1080