Initial import from FreeBSD RELENG_4:
[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  *
26  */
27
28 /*
29  * DEC PDQ FDDI Controller; code for BSD derived operating systems
30  *
31  *      This module provide bus independent BSD specific O/S functions.
32  *      (ie. it provides an ifnet interface to the rest of the system)
33  */
34
35
36 #include "opt_inet.h"
37
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 #if defined(__bsdi__) || defined(__NetBSD__)
42 #include <sys/device.h>
43 #endif
44
45 #include <net/if.h>
46 #include <net/if_dl.h>
47
48 #include <net/bpf.h>
49
50 #if defined(__FreeBSD__)
51 #ifdef INET
52 #include <netinet/in.h>
53 #include <netinet/if_ether.h>
54 #endif
55 #include <netinet/if_fddi.h>
56 #else
57 #include <net/if_fddi.h>
58 #endif
59
60 #if defined(__bsdi__)
61 #include <i386/isa/isavar.h>
62 #endif
63
64 #ifdef NS
65 #include <netns/ns.h>
66 #include <netns/ns_if.h>
67 #endif
68
69 #if defined(__FreeBSD__)
70 #include <dev/pdq/pdqvar.h>
71 #include <dev/pdq/pdqreg.h>
72 #else
73 #include "pdqvar.h"
74 #include "pdqreg.h"
75 #endif
76
77 #if defined(__bsdi__) && _BSDI_VERSION < 199506 /* XXX */
78 static void
79 arp_ifinit(
80     struct arpcom *ac,
81     struct ifaddr *ifa)
82 {
83     sc->sc_ac.ac_ipaddr = IA_SIN(ifa)->sin_addr;
84     arpwhohas(&sc->sc_ac, &IA_SIN(ifa)->sin_addr);
85 #if _BSDI_VERSION >= 199401
86     ifa->ifa_rtrequest = arp_rtrequest;
87     ifa->ifa_flags |= RTF_CLONING;
88 #endif
89 }
90 #endif
91
92
93 void
94 pdq_ifinit(
95     pdq_softc_t *sc)
96 {
97     if (sc->sc_if.if_flags & IFF_UP) {
98         sc->sc_if.if_flags |= IFF_RUNNING;
99         if (sc->sc_if.if_flags & IFF_PROMISC) {
100             sc->sc_pdq->pdq_flags |= PDQ_PROMISC;
101         } else {
102             sc->sc_pdq->pdq_flags &= ~PDQ_PROMISC;
103         }
104         if (sc->sc_if.if_flags & IFF_ALLMULTI) {
105             sc->sc_pdq->pdq_flags |= PDQ_ALLMULTI;
106         } else {
107             sc->sc_pdq->pdq_flags &= ~PDQ_ALLMULTI;
108         }
109         if (sc->sc_if.if_flags & IFF_LINK1) {
110             sc->sc_pdq->pdq_flags |= PDQ_PASS_SMT;
111         } else {
112             sc->sc_pdq->pdq_flags &= ~PDQ_PASS_SMT;
113         }
114         sc->sc_pdq->pdq_flags |= PDQ_RUNNING;
115         pdq_run(sc->sc_pdq);
116     } else {
117         sc->sc_if.if_flags &= ~IFF_RUNNING;
118         sc->sc_pdq->pdq_flags &= ~PDQ_RUNNING;
119         pdq_stop(sc->sc_pdq);
120     }
121 }
122 \f
123 void
124 pdq_ifwatchdog(
125     struct ifnet *ifp)
126 {
127     /*
128      * No progress was made on the transmit queue for PDQ_OS_TX_TRANSMIT
129      * seconds.  Remove all queued packets.
130      */
131
132     ifp->if_flags &= ~IFF_OACTIVE;
133     ifp->if_timer = 0;
134     for (;;) {
135         struct mbuf *m;
136         IF_DEQUEUE(&ifp->if_snd, m);
137         if (m == NULL)
138             return;
139         m_freem(m);
140     }
141 }
142
143 ifnet_ret_t
144 pdq_ifstart(
145     struct ifnet *ifp)
146 {
147     pdq_softc_t *sc = (pdq_softc_t *) ((caddr_t) ifp - offsetof(pdq_softc_t, sc_ac.ac_if));
148     struct ifqueue *ifq = &ifp->if_snd;
149     struct mbuf *m;
150     int tx = 0;
151
152     if ((ifp->if_flags & IFF_RUNNING) == 0)
153         return;
154
155     if (sc->sc_if.if_timer == 0)
156         sc->sc_if.if_timer = PDQ_OS_TX_TIMEOUT;
157
158     if ((sc->sc_pdq->pdq_flags & PDQ_TXOK) == 0) {
159         sc->sc_if.if_flags |= IFF_OACTIVE;
160         return;
161     }
162     for (;; tx = 1) {
163         IF_DEQUEUE(ifq, m);
164         if (m == NULL)
165             break;
166
167         if (pdq_queue_transmit_data(sc->sc_pdq, m) == PDQ_FALSE) {
168             ifp->if_flags |= IFF_OACTIVE;
169             IF_PREPEND(ifq, m);
170             break;
171         }
172     }
173     if (tx)
174         PDQ_DO_TYPE2_PRODUCER(sc->sc_pdq);
175 }
176 \f
177 void
178 pdq_os_receive_pdu(
179     pdq_t *pdq,
180     struct mbuf *m,
181     size_t pktlen)
182 {
183     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
184     struct fddi_header *fh = mtod(m, struct fddi_header *);
185
186     sc->sc_if.if_ipackets++;
187     if (sc->sc_bpf != NULL)
188         PDQ_BPF_MTAP(sc, m);
189     if ((fh->fddi_fc & (FDDIFC_L|FDDIFC_F)) != FDDIFC_LLC_ASYNC) {
190         m_freem(m);
191         return;
192     }
193
194     m->m_data += sizeof(struct fddi_header);
195     m->m_len  -= sizeof(struct fddi_header);
196     m->m_pkthdr.len = pktlen - sizeof(struct fddi_header);
197     m->m_pkthdr.rcvif = &sc->sc_if;
198     fddi_input(&sc->sc_if, fh, m);
199 }
200
201 void
202 pdq_os_restart_transmitter(
203     pdq_t *pdq)
204 {
205     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
206     sc->sc_if.if_flags &= ~IFF_OACTIVE;
207     if (sc->sc_if.if_snd.ifq_head != NULL) {
208         sc->sc_if.if_timer = PDQ_OS_TX_TIMEOUT;
209         pdq_ifstart(&sc->sc_if);
210     } else {
211         sc->sc_if.if_timer = 0;
212     }
213 }
214
215 void
216 pdq_os_transmit_done(
217     pdq_t *pdq,
218     struct mbuf *m)
219 {
220     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
221     if (sc->sc_bpf != NULL)
222         PDQ_BPF_MTAP(sc, m);
223     m_freem(m);
224     sc->sc_if.if_opackets++;
225 }
226 \f
227 void
228 pdq_os_addr_fill(
229     pdq_t *pdq,
230     pdq_lanaddr_t *addr,
231     size_t num_addrs)
232 {
233     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
234     struct ifmultiaddr *ifma;
235
236     for (ifma = sc->sc_if.if_multiaddrs.lh_first; ifma && num_addrs > 0;
237          ifma = ifma->ifma_link.le_next) {
238             char *mcaddr;
239             if (ifma->ifma_addr->sa_family != AF_LINK)
240                     continue;
241             mcaddr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
242             ((u_short *) addr->lanaddr_bytes)[0] = ((u_short *) mcaddr)[0];
243             ((u_short *) addr->lanaddr_bytes)[1] = ((u_short *) mcaddr)[1];
244             ((u_short *) addr->lanaddr_bytes)[2] = ((u_short *) mcaddr)[2];
245             addr++;
246             num_addrs--;
247     }
248 }
249 \f
250 int
251 pdq_ifioctl(
252     struct ifnet *ifp,
253     ioctl_cmd_t cmd,
254     caddr_t data)
255 {
256     pdq_softc_t *sc = (pdq_softc_t *) ((caddr_t) ifp - offsetof(pdq_softc_t, sc_ac.ac_if));
257     int s, error = 0;
258
259     s = splimp();
260
261     switch (cmd) {
262         case SIOCSIFADDR: {
263             struct ifaddr *ifa = (struct ifaddr *)data;
264
265             ifp->if_flags |= IFF_UP;
266             switch(ifa->ifa_addr->sa_family) {
267 #if defined(INET)
268                 case AF_INET: {
269                     pdq_ifinit(sc);
270                     arp_ifinit(&sc->sc_ac, ifa);
271                     break;
272                 }
273 #endif /* INET */
274
275 #if defined(NS)
276                 /* This magic copied from if_is.c; I don't use XNS,
277                  * so I have no way of telling if this actually
278                  * works or not.
279                  */
280                 case AF_NS: {
281                     struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
282                     if (ns_nullhost(*ina)) {
283                         ina->x_host = *(union ns_host *)(sc->sc_ac.ac_enaddr);
284                     } else {
285                         ifp->if_flags &= ~IFF_RUNNING;
286                         bcopy((caddr_t)ina->x_host.c_host,
287                               (caddr_t)sc->sc_ac.ac_enaddr,
288                               sizeof sc->sc_ac.ac_enaddr);
289                     }
290
291                     pdq_ifinit(sc);
292                     break;
293                 }
294 #endif /* NS */
295
296                 default: {
297                     pdq_ifinit(sc);
298                     break;
299                 }
300             }
301             break;
302         }
303         case SIOCGIFADDR: {
304             struct ifreq *ifr = (struct ifreq *)data;
305             bcopy((caddr_t) sc->sc_ac.ac_enaddr,
306                   (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
307                   6);
308             break;
309         }
310
311         case SIOCSIFFLAGS: {
312             pdq_ifinit(sc);
313             break;
314         }
315
316         case SIOCADDMULTI:
317         case SIOCDELMULTI:
318                 /*
319                  * Update multicast listeners
320                  */
321                 if (sc->sc_if.if_flags & IFF_RUNNING)
322                         pdq_run(sc->sc_pdq);
323                 error = 0;
324                 break;
325
326 #if defined(SIOCSIFMTU)
327 #if !defined(ifr_mtu)
328 #define ifr_mtu ifr_metric
329 #endif
330         case SIOCSIFMTU: {
331             struct ifreq *ifr = (struct ifreq *)data;
332             /*
333              * Set the interface MTU.
334              */
335             if (ifr->ifr_mtu > FDDIMTU) {
336                 error = EINVAL;
337                 break;
338             }
339             ifp->if_mtu = ifr->ifr_mtu;
340             break;
341         }
342 #endif /* SIOCSIFMTU */
343
344         default: {
345             error = EINVAL;
346             break;
347         }
348     }
349
350     splx(s);
351     return error;
352 }
353 \f
354 #ifndef IFF_NOTRAILERS
355 #define IFF_NOTRAILERS  0
356 #endif
357
358 void
359 pdq_ifattach(
360     pdq_softc_t *sc,
361     ifnet_ret_t (*ifwatchdog)(int unit))
362 {
363     struct ifnet *ifp = &sc->sc_if;
364
365     ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
366
367 #if (defined(__FreeBSD__) && BSD >= 199506) || defined(__NetBSD__)
368     ifp->if_watchdog = pdq_ifwatchdog;
369 #else
370     ifp->if_watchdog = ifwatchdog;
371 #endif
372
373     ifp->if_ioctl = pdq_ifioctl;
374     ifp->if_output = fddi_output;
375     ifp->if_start = pdq_ifstart;
376     ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
377 #warning "Implement fddi_resolvemulti!"
378 /*    ifp->if_resolvemulti = ether_resolvemulti; XXX */
379   
380     if_attach(ifp);
381     fddi_ifattach(ifp);
382     PDQ_BPFATTACH(sc, DLT_FDDI, sizeof(struct fddi_header));
383 }