6bd817bf3e35631d799c7d8fdf6257f2e281449e
[dragonfly.git] / sys / dev / netif / pdq_layer / pdq_ifsubr.c
1 /*-
2  * Copyright (c) 1995, 1996 Matt Thomas <matt@3am-software.com>
3  * 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. The name of the author may not be used to endorse or promote products
11  *    derived from this software withough specific prior written permission
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * $FreeBSD: src/sys/dev/pdq/pdq_ifsubr.c,v 1.11.2.1 2000/08/02 22:39:30 peter Exp $
25  * $DragonFly: src/sys/dev/netif/pdq_layer/Attic/pdq_ifsubr.c,v 1.13 2005/06/20 15:10:41 joerg Exp $
26  *
27  */
28
29 /*
30  * DEC PDQ FDDI Controller; code for BSD derived operating systems
31  *
32  *      This module provide bus independent BSD specific O/S functions.
33  *      (ie. it provides an ifnet interface to the rest of the system)
34  */
35
36
37 #include "opt_inet.h"
38
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/thread2.h>
43 #if defined(__bsdi__) || defined(__NetBSD__)
44 #include <sys/device.h>
45 #endif
46
47 #include <net/if.h>
48 #include <net/ifq_var.h>
49 #include <net/if_dl.h>
50
51 #include <net/bpf.h>
52
53 #if defined(__DragonFly__) || defined(__FreeBSD__)
54 #ifdef INET
55 #include <netinet/in.h>
56 #include <netinet/if_ether.h>
57 #endif
58 #include <netinet/if_fddi.h>
59 #else
60 #include <net/if_fddi.h>
61 #endif
62
63 #if defined(__bsdi__)
64 #include <i386/isa/isavar.h>
65 #endif
66
67 #ifdef NS
68 #include <netns/ns.h>
69 #include <netns/ns_if.h>
70 #endif
71
72 #if defined(__DragonFly__) || defined(__FreeBSD__)
73 #include "pdqvar.h"
74 #include "pdqreg.h"
75 #else
76 #include "pdqvar.h"
77 #include "pdqreg.h"
78 #endif
79
80 #if defined(__bsdi__) && _BSDI_VERSION < 199506 /* XXX */
81 static void
82 arp_ifinit(
83     struct arpcom *ac,
84     struct ifaddr *ifa)
85 {
86     sc->sc_ac.ac_ipaddr = IA_SIN(ifa)->sin_addr;
87     arpwhohas(&sc->sc_ac, &IA_SIN(ifa)->sin_addr);
88 #if _BSDI_VERSION >= 199401
89     ifa->ifa_rtrequest = arp_rtrequest;
90     ifa->ifa_flags |= RTF_CLONING;
91 #endif
92 }
93 #endif
94
95
96 void
97 pdq_ifinit(
98     pdq_softc_t *sc)
99 {
100     if (sc->sc_if.if_flags & IFF_UP) {
101         sc->sc_if.if_flags |= IFF_RUNNING;
102         if (sc->sc_if.if_flags & IFF_PROMISC) {
103             sc->sc_pdq->pdq_flags |= PDQ_PROMISC;
104         } else {
105             sc->sc_pdq->pdq_flags &= ~PDQ_PROMISC;
106         }
107         if (sc->sc_if.if_flags & IFF_ALLMULTI) {
108             sc->sc_pdq->pdq_flags |= PDQ_ALLMULTI;
109         } else {
110             sc->sc_pdq->pdq_flags &= ~PDQ_ALLMULTI;
111         }
112         if (sc->sc_if.if_flags & IFF_LINK1) {
113             sc->sc_pdq->pdq_flags |= PDQ_PASS_SMT;
114         } else {
115             sc->sc_pdq->pdq_flags &= ~PDQ_PASS_SMT;
116         }
117         sc->sc_pdq->pdq_flags |= PDQ_RUNNING;
118         pdq_run(sc->sc_pdq);
119     } else {
120         sc->sc_if.if_flags &= ~IFF_RUNNING;
121         sc->sc_pdq->pdq_flags &= ~PDQ_RUNNING;
122         pdq_stop(sc->sc_pdq);
123     }
124 }
125 \f
126 void
127 pdq_ifwatchdog(
128     struct ifnet *ifp)
129 {
130     /*
131      * No progress was made on the transmit queue for PDQ_OS_TX_TRANSMIT
132      * seconds.  Remove all queued packets.
133      */
134
135     ifp->if_flags &= ~IFF_OACTIVE;
136     ifp->if_timer = 0;
137     ifq_purge(&ifp->if_snd);
138 }
139
140 ifnet_ret_t
141 pdq_ifstart(
142     struct ifnet *ifp)
143 {
144     pdq_softc_t *sc = (pdq_softc_t *) ((caddr_t) ifp - offsetof(pdq_softc_t, sc_ac.ac_if));
145     struct mbuf *m;
146     int tx = 0;
147
148     if ((ifp->if_flags & IFF_RUNNING) == 0)
149         return;
150
151     if (sc->sc_if.if_timer == 0)
152         sc->sc_if.if_timer = PDQ_OS_TX_TIMEOUT;
153
154     if ((sc->sc_pdq->pdq_flags & PDQ_TXOK) == 0) {
155         sc->sc_if.if_flags |= IFF_OACTIVE;
156         return;
157     }
158     for (;; tx = 1) {
159         m = ifq_poll(&ifp->if_snd);
160         if (m == NULL)
161             break;
162
163         if (pdq_queue_transmit_data(ifp, sc->sc_pdq, m) == PDQ_FALSE) {
164             break;
165         }
166     }
167     if (tx)
168         PDQ_DO_TYPE2_PRODUCER(sc->sc_pdq);
169 }
170 \f
171 void
172 pdq_os_receive_pdu(
173     pdq_t *pdq,
174     struct mbuf *m,
175     size_t pktlen)
176 {
177     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
178     struct fddi_header *fh = mtod(m, struct fddi_header *);
179
180     sc->sc_if.if_ipackets++;
181     BPF_MTAP(&sc->sc_if, m);
182     if ((fh->fddi_fc & (FDDIFC_L|FDDIFC_F)) != FDDIFC_LLC_ASYNC) {
183         m_freem(m);
184         return;
185     }
186
187     m->m_pkthdr.len = pktlen;
188     (*sc->sc_if.if_input)(&sc->sc_if, m);
189 }
190
191 void
192 pdq_os_restart_transmitter(
193     pdq_t *pdq)
194 {
195     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
196     sc->sc_if.if_flags &= ~IFF_OACTIVE;
197     if (ifq_is_empty(&sc->sc_if.if_snd)) {
198         pdq_ifstart(&sc->sc_if);
199         sc->sc_if.if_timer = PDQ_OS_TX_TIMEOUT;
200     } else {
201         sc->sc_if.if_timer = 0;
202     }
203 }
204
205 void
206 pdq_os_transmit_done(
207     pdq_t *pdq,
208     struct mbuf *m)
209 {
210     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
211     BPF_MTAP(&sc->sc_if, m);
212     m_freem(m);
213     sc->sc_if.if_opackets++;
214 }
215 \f
216 void
217 pdq_os_addr_fill(
218     pdq_t *pdq,
219     pdq_lanaddr_t *addr,
220     size_t num_addrs)
221 {
222     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
223     struct ifmultiaddr *ifma;
224
225     LIST_FOREACH(ifma, &sc->sc_if.if_multiaddrs, ifma_link) {
226             char *mcaddr;
227             if (ifma->ifma_addr->sa_family != AF_LINK)
228                     continue;
229             mcaddr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
230             ((u_short *) addr->lanaddr_bytes)[0] = ((u_short *) mcaddr)[0];
231             ((u_short *) addr->lanaddr_bytes)[1] = ((u_short *) mcaddr)[1];
232             ((u_short *) addr->lanaddr_bytes)[2] = ((u_short *) mcaddr)[2];
233             addr++;
234             num_addrs--;
235     }
236 }
237 \f
238 int
239 pdq_ifioctl(struct ifnet *ifp, ioctl_cmd_t cmd, caddr_t data, struct ucred *cr)
240 {
241     pdq_softc_t *sc = (pdq_softc_t *) ((caddr_t) ifp - offsetof(pdq_softc_t, sc_ac.ac_if));
242     int error = 0;
243
244     crit_enter();
245
246     switch (cmd) {
247         case SIOCSIFADDR: {
248             struct ifaddr *ifa = (struct ifaddr *)data;
249
250             ifp->if_flags |= IFF_UP;
251             switch(ifa->ifa_addr->sa_family) {
252 #if defined(INET)
253                 case AF_INET: {
254                     pdq_ifinit(sc);
255                     arp_ifinit(&sc->sc_ac.ac_if, ifa);
256                     break;
257                 }
258 #endif /* INET */
259
260 #if defined(NS)
261                 /* This magic copied from if_is.c; I don't use XNS,
262                  * so I have no way of telling if this actually
263                  * works or not.
264                  */
265                 case AF_NS: {
266                     struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
267                     if (ns_nullhost(*ina)) {
268                         ina->x_host = *(union ns_host *)(sc->sc_ac.ac_enaddr);
269                     } else {
270                         ifp->if_flags &= ~IFF_RUNNING;
271                         bcopy((caddr_t)ina->x_host.c_host,
272                               (caddr_t)sc->sc_ac.ac_enaddr,
273                               sizeof sc->sc_ac.ac_enaddr);
274                     }
275
276                     pdq_ifinit(sc);
277                     break;
278                 }
279 #endif /* NS */
280
281                 default: {
282                     pdq_ifinit(sc);
283                     break;
284                 }
285             }
286             break;
287         }
288         case SIOCGIFADDR: {
289             struct ifreq *ifr = (struct ifreq *)data;
290             bcopy((caddr_t) sc->sc_ac.ac_enaddr,
291                   (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
292                   6);
293             break;
294         }
295
296         case SIOCSIFFLAGS: {
297             pdq_ifinit(sc);
298             break;
299         }
300
301         case SIOCADDMULTI:
302         case SIOCDELMULTI:
303                 /*
304                  * Update multicast listeners
305                  */
306                 if (sc->sc_if.if_flags & IFF_RUNNING)
307                         pdq_run(sc->sc_pdq);
308                 error = 0;
309                 break;
310
311 #if defined(SIOCSIFMTU)
312 #if !defined(ifr_mtu)
313 #define ifr_mtu ifr_metric
314 #endif
315         case SIOCSIFMTU: {
316             struct ifreq *ifr = (struct ifreq *)data;
317             /*
318              * Set the interface MTU.
319              */
320             if (ifr->ifr_mtu > FDDIMTU) {
321                 error = EINVAL;
322                 break;
323             }
324             ifp->if_mtu = ifr->ifr_mtu;
325             break;
326         }
327 #endif /* SIOCSIFMTU */
328
329         default: {
330             error = EINVAL;
331             break;
332         }
333     }
334
335     crit_exit();
336
337     return error;
338 }
339 \f
340 #ifndef IFF_NOTRAILERS
341 #define IFF_NOTRAILERS  0
342 #endif
343
344 void
345 pdq_ifattach(
346     pdq_softc_t *sc,
347     ifnet_ret_t (*ifwatchdog)(int unit))
348 {
349     struct ifnet *ifp = &sc->sc_if;
350
351     ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
352
353 #if defined(__DragonFly__) || (defined(__FreeBSD__) && BSD >= 199506) || defined(__NetBSD__)
354     ifp->if_watchdog = pdq_ifwatchdog;
355 #else
356     ifp->if_watchdog = ifwatchdog;
357 #endif
358
359     ifp->if_ioctl = pdq_ifioctl;
360     ifp->if_start = pdq_ifstart;
361     ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
362     ifq_set_ready(&ifp->if_snd);
363   
364     fddi_ifattach(ifp);
365 }