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