if_qn.c revision 1.1 1 1.1 chopps /* $NetBSD: if_qn.c,v 1.1 1995/10/07 18:04:27 chopps Exp $ */
2 1.1 chopps
3 1.1 chopps /*
4 1.1 chopps * Copyright (c) 1995 Mika Kortelainen
5 1.1 chopps * All rights reserved.
6 1.1 chopps *
7 1.1 chopps * Redistribution and use in source and binary forms, with or without
8 1.1 chopps * modification, are permitted provided that the following conditions
9 1.1 chopps * are met:
10 1.1 chopps * 1. Redistributions of source code must retain the above copyright
11 1.1 chopps * notice, this list of conditions and the following disclaimer.
12 1.1 chopps * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 chopps * notice, this list of conditions and the following disclaimer in the
14 1.1 chopps * documentation and/or other materials provided with the distribution.
15 1.1 chopps * 3. All advertising materials mentioning features or use of this software
16 1.1 chopps * must display the following acknowledgement:
17 1.1 chopps * This product includes software developed by Mika Kortelainen
18 1.1 chopps * 4. The name of the author may not be used to endorse or promote products
19 1.1 chopps * derived from this software without specific prior written permission
20 1.1 chopps *
21 1.1 chopps * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 chopps * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 chopps * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 chopps * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 chopps * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 chopps * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 chopps * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 chopps * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 chopps * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 chopps * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 chopps *
32 1.1 chopps * Thanks for Aspecs Oy (Finland) for the data book for the NIC used
33 1.1 chopps * in this card and also many thanks for the Resource Management Force
34 1.1 chopps * (QuickNet card manufacturer) and especially Daniel Koch for providing
35 1.1 chopps * me with the necessary 'inside' information to write the driver.
36 1.1 chopps *
37 1.1 chopps * This is partly based on other code:
38 1.1 chopps * - if_ed.c: basic function structure for Ethernet driver
39 1.1 chopps *
40 1.1 chopps * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
41 1.1 chopps * adapters.
42 1.1 chopps *
43 1.1 chopps * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
44 1.1 chopps *
45 1.1 chopps * Copyright (C) 1993, David Greenman. This software may be used,
46 1.1 chopps * modified, copied, distributed, and sold, in both source and binary
47 1.1 chopps * form provided that the above copyright and these terms are retained.
48 1.1 chopps * Under no circumstances is the author responsible for the proper
49 1.1 chopps * functioning of this software, nor does the author assume any
50 1.1 chopps * responsibility for damages incurred with its use.
51 1.1 chopps *
52 1.1 chopps * - if_es.c: used as an example of -current driver
53 1.1 chopps *
54 1.1 chopps * Copyright (c) 1995 Michael L. Hitch
55 1.1 chopps * All rights reserved.
56 1.1 chopps *
57 1.1 chopps *
58 1.1 chopps * TODO:
59 1.1 chopps * - add multicast support
60 1.1 chopps * - try to find out what is the reason for random lock-ups happening
61 1.1 chopps * when (or after) getting data
62 1.1 chopps */
63 1.1 chopps
64 1.1 chopps #include "qn.h"
65 1.1 chopps #if NQN > 0
66 1.1 chopps
67 1.1 chopps #define QN_DEBUG
68 1.1 chopps
69 1.1 chopps #include "bpfilter.h"
70 1.1 chopps
71 1.1 chopps /*
72 1.1 chopps * Fujitsu MB86950 Ethernet Controller (as used in QuickNet QN2000 Ethernet card)
73 1.1 chopps */
74 1.1 chopps
75 1.1 chopps #include <sys/param.h>
76 1.1 chopps #include <sys/systm.h>
77 1.1 chopps #include <sys/mbuf.h>
78 1.1 chopps #include <sys/buf.h>
79 1.1 chopps #include <sys/device.h>
80 1.1 chopps #include <sys/protosw.h>
81 1.1 chopps #include <sys/socket.h>
82 1.1 chopps #include <sys/syslog.h>
83 1.1 chopps #include <sys/ioctl.h>
84 1.1 chopps #include <sys/errno.h>
85 1.1 chopps
86 1.1 chopps #include <net/if.h>
87 1.1 chopps #include <net/netisr.h>
88 1.1 chopps #include <net/route.h>
89 1.1 chopps
90 1.1 chopps #ifdef INET
91 1.1 chopps #include <netinet/in.h>
92 1.1 chopps #include <netinet/in_systm.h>
93 1.1 chopps #include <netinet/in_var.h>
94 1.1 chopps #include <netinet/ip.h>
95 1.1 chopps #include <netinet/if_ether.h>
96 1.1 chopps #endif
97 1.1 chopps
98 1.1 chopps #ifdef NS
99 1.1 chopps #include <netns/ns.h>
100 1.1 chopps #include <netns/ns_if.h>
101 1.1 chopps #endif
102 1.1 chopps
103 1.1 chopps #include <machine/cpu.h>
104 1.1 chopps #include <machine/mtpr.h>
105 1.1 chopps #include <amiga/amiga/device.h>
106 1.1 chopps #include <amiga/amiga/isr.h>
107 1.1 chopps #include <amiga/dev/zbusvar.h>
108 1.1 chopps #include <amiga/dev/if_qnreg.h>
109 1.1 chopps
110 1.1 chopps
111 1.1 chopps #define ETHER_MIN_LEN 64
112 1.1 chopps #define ETHER_MAX_LEN 1518
113 1.1 chopps #define ETHER_ADDR_LEN 6
114 1.1 chopps
115 1.1 chopps
116 1.1 chopps /*
117 1.1 chopps * Ethernet software status per interface
118 1.1 chopps *
119 1.1 chopps * Each interface is referenced by a network interface structure,
120 1.1 chopps * qn_if, which the routing code uses to locate the interface.
121 1.1 chopps * This structure contains the output queue for the interface, its address, ...
122 1.1 chopps */
123 1.1 chopps struct qn_softc {
124 1.1 chopps struct device sc_dev;
125 1.1 chopps struct isr sc_isr;
126 1.1 chopps struct arpcom sc_arpcom; /* Common ethernet structures */
127 1.1 chopps u_char volatile *sc_base;
128 1.1 chopps u_char volatile *sc_nic_base;
129 1.1 chopps u_short volatile *nic_fifo;
130 1.1 chopps u_short volatile *nic_r_status;
131 1.1 chopps u_short volatile *nic_t_status;
132 1.1 chopps u_short volatile *nic_r_mask;
133 1.1 chopps u_short volatile *nic_t_mask;
134 1.1 chopps u_short volatile *nic_r_mode;
135 1.1 chopps u_short volatile *nic_t_mode;
136 1.1 chopps u_short volatile *nic_reset;
137 1.1 chopps u_short volatile *nic_len;
138 1.1 chopps u_char transmit_pending;
139 1.1 chopps #if NBPFILTER > 0
140 1.1 chopps caddr_t sc_bpf;
141 1.1 chopps #endif
142 1.1 chopps } qn_softc[NQN];
143 1.1 chopps
144 1.1 chopps #if NBPFILTER > 0
145 1.1 chopps #include <net/bpf.h>
146 1.1 chopps #include <net/bpfdesc.h>
147 1.1 chopps #endif
148 1.1 chopps
149 1.1 chopps
150 1.1 chopps int qnmatch __P((struct device *, void *, void *));
151 1.1 chopps void qnattach __P((struct device *, struct device *, void *));
152 1.1 chopps int qnintr __P((struct qn_softc *sc));
153 1.1 chopps int qnioctl __P((struct ifnet *, u_long, caddr_t));
154 1.1 chopps void qnstart __P((struct ifnet *));
155 1.1 chopps void qnwatchdog __P((int));
156 1.1 chopps void qnreset __P((struct qn_softc *));
157 1.1 chopps void qninit __P((struct qn_softc *));
158 1.1 chopps void qnstop __P((struct qn_softc *));
159 1.1 chopps static u_short qn_put __P((u_short volatile *, struct mbuf *));
160 1.1 chopps static void qn_flush __P((struct qn_softc *));
161 1.1 chopps
162 1.1 chopps struct cfdriver qncd = {
163 1.1 chopps NULL, "qn", qnmatch, qnattach, DV_IFNET, sizeof(struct qn_softc)
164 1.1 chopps };
165 1.1 chopps
166 1.1 chopps int
167 1.1 chopps qnmatch(parent, match, aux)
168 1.1 chopps struct device *parent;
169 1.1 chopps void *match, *aux;
170 1.1 chopps {
171 1.1 chopps struct zbus_args *zap;
172 1.1 chopps
173 1.1 chopps zap = (struct zbus_args *)aux;
174 1.1 chopps
175 1.1 chopps /* RMF QuickNet QN2000 EtherNet card */
176 1.1 chopps if (zap->manid == 2011 && zap->prodid == 2)
177 1.1 chopps return (1);
178 1.1 chopps
179 1.1 chopps return (0);
180 1.1 chopps }
181 1.1 chopps
182 1.1 chopps /*
183 1.1 chopps * Interface exists: make available by filling in network interface
184 1.1 chopps * record. System will initialize the interface when it is ready
185 1.1 chopps * to accept packets.
186 1.1 chopps */
187 1.1 chopps void
188 1.1 chopps qnattach(parent, self, aux)
189 1.1 chopps struct device *parent, *self;
190 1.1 chopps void *aux;
191 1.1 chopps {
192 1.1 chopps struct zbus_args *zap;
193 1.1 chopps struct qn_softc *sc = (struct qn_softc *)self;
194 1.1 chopps struct ifnet *ifp = &sc->sc_arpcom.ac_if;
195 1.1 chopps int i;
196 1.1 chopps
197 1.1 chopps zap = (struct zbus_args *)aux;
198 1.1 chopps
199 1.1 chopps sc->sc_base = zap->va;
200 1.1 chopps sc->sc_nic_base = sc->sc_base + QUICKNET_NIC_BASE;
201 1.1 chopps sc->nic_fifo = (u_short volatile *)(sc->sc_nic_base + NIC_BMPR0);
202 1.1 chopps sc->nic_len = (u_short volatile *)(sc->sc_nic_base + NIC_BMPR2);
203 1.1 chopps sc->nic_t_status = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR0);
204 1.1 chopps sc->nic_r_status = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR2);
205 1.1 chopps sc->nic_t_mask = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR1);
206 1.1 chopps sc->nic_r_mask = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR3);
207 1.1 chopps sc->nic_t_mode = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR4);
208 1.1 chopps sc->nic_r_mode = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR5);
209 1.1 chopps sc->nic_reset = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR6);
210 1.1 chopps sc->transmit_pending = 0;
211 1.1 chopps
212 1.1 chopps /*
213 1.1 chopps * The ethernet address of the board (1st three bytes are the vendor
214 1.1 chopps * address, the rest is the serial number of the board).
215 1.1 chopps */
216 1.1 chopps sc->sc_arpcom.ac_enaddr[0] = 0x5c;
217 1.1 chopps sc->sc_arpcom.ac_enaddr[1] = 0x5c;
218 1.1 chopps sc->sc_arpcom.ac_enaddr[2] = 0x00;
219 1.1 chopps sc->sc_arpcom.ac_enaddr[3] = (zap->serno >> 16) & 0xff;
220 1.1 chopps sc->sc_arpcom.ac_enaddr[4] = (zap->serno >> 8) & 0xff;
221 1.1 chopps sc->sc_arpcom.ac_enaddr[5] = zap->serno & 0xff;
222 1.1 chopps
223 1.1 chopps /* set interface to stopped condition (reset) */
224 1.1 chopps qnstop(sc);
225 1.1 chopps
226 1.1 chopps ifp->if_unit = sc->sc_dev.dv_unit;
227 1.1 chopps ifp->if_name = qncd.cd_name;
228 1.1 chopps ifp->if_ioctl = qnioctl;
229 1.1 chopps ifp->if_watchdog = qnwatchdog;
230 1.1 chopps ifp->if_output = ether_output;
231 1.1 chopps ifp->if_start = qnstart;
232 1.1 chopps /* XXX IFF_MULTICAST */
233 1.1 chopps ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
234 1.1 chopps ifp->if_mtu = ETHERMTU;
235 1.1 chopps
236 1.1 chopps /* Attach the interface. */
237 1.1 chopps if_attach(ifp);
238 1.1 chopps ether_ifattach(ifp);
239 1.1 chopps
240 1.1 chopps #ifdef QN_DEBUG
241 1.1 chopps printf(": hardware address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
242 1.1 chopps #endif
243 1.1 chopps
244 1.1 chopps #if NBPFILTER > 0
245 1.1 chopps bpfattach(&sc->sc_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
246 1.1 chopps #endif
247 1.1 chopps
248 1.1 chopps sc->sc_isr.isr_intr = qnintr;
249 1.1 chopps sc->sc_isr.isr_arg = sc;
250 1.1 chopps sc->sc_isr.isr_ipl = 2;
251 1.1 chopps add_isr(&sc->sc_isr);
252 1.1 chopps }
253 1.1 chopps
254 1.1 chopps /*
255 1.1 chopps * Initialize device
256 1.1 chopps *
257 1.1 chopps */
258 1.1 chopps void
259 1.1 chopps qninit(sc)
260 1.1 chopps struct qn_softc *sc;
261 1.1 chopps {
262 1.1 chopps struct ifnet *ifp = &sc->sc_arpcom.ac_if;
263 1.1 chopps u_short i;
264 1.1 chopps int s;
265 1.1 chopps
266 1.1 chopps s = splimp();
267 1.1 chopps
268 1.1 chopps /* Initialize NIC */
269 1.1 chopps *sc->nic_reset = DISABLE_DLC;
270 1.1 chopps *sc->nic_t_status = CLEAR_T_ERR;
271 1.1 chopps *sc->nic_t_mask = CLEAR_T_MASK;
272 1.1 chopps *sc->nic_r_status = CLEAR_R_ERR;
273 1.1 chopps *sc->nic_r_mask = R_INT_PKT_RDY;
274 1.1 chopps *sc->nic_t_mode = NO_LOOPBACK;
275 1.1 chopps
276 1.1 chopps /* Turn DMA off */
277 1.1 chopps *((u_short volatile *)(sc->sc_nic_base + NIC_BMPR4)) = (u_short)0x0000;
278 1.1 chopps
279 1.1 chopps if (sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) {
280 1.1 chopps *sc->nic_r_mode = PROMISCUOUS_MODE;
281 1.1 chopps log(LOG_INFO, "qn: Promiscuous mode (not tested)\n");
282 1.1 chopps } else
283 1.1 chopps *sc->nic_r_mode = NORMAL_MODE;
284 1.1 chopps
285 1.1 chopps /* Set physical ethernet address. */
286 1.1 chopps for (i = 0; i < ETHER_ADDR_LEN; i++)
287 1.1 chopps *((u_short volatile *)(sc->sc_nic_base+QNET_HARDWARE_ADDRESS+2*i)) =
288 1.1 chopps ((((u_short)sc->sc_arpcom.ac_enaddr[i]) << 8) |
289 1.1 chopps sc->sc_arpcom.ac_enaddr[i]);
290 1.1 chopps
291 1.1 chopps sc->transmit_pending = 0;
292 1.1 chopps ifp->if_flags |= IFF_RUNNING;
293 1.1 chopps ifp->if_flags &= ~IFF_OACTIVE;
294 1.1 chopps
295 1.1 chopps qn_flush(sc);
296 1.1 chopps
297 1.1 chopps /* Enable data link controller. */
298 1.1 chopps *((u_short volatile *)(sc->sc_nic_base + QNET_MAGIC)) = (u_short)0x0000;
299 1.1 chopps *sc->nic_reset = ENABLE_DLC;
300 1.1 chopps
301 1.1 chopps /* Attempt to start output, if any. */
302 1.1 chopps qnstart(ifp);
303 1.1 chopps
304 1.1 chopps splx(s);
305 1.1 chopps }
306 1.1 chopps
307 1.1 chopps
308 1.1 chopps /*
309 1.1 chopps * Device timeout/watchdog routine. Entered if the device neglects to
310 1.1 chopps * generate an interrupt after a transmit has been started on it.
311 1.1 chopps */
312 1.1 chopps void
313 1.1 chopps qnwatchdog(unit)
314 1.1 chopps int unit;
315 1.1 chopps {
316 1.1 chopps struct qn_softc *sc = qncd.cd_devs[unit];
317 1.1 chopps
318 1.1 chopps log(LOG_ERR, "%s: device timeout (watchdog)\n", sc->sc_dev.dv_xname);
319 1.1 chopps ++sc->sc_arpcom.ac_if.if_oerrors;
320 1.1 chopps
321 1.1 chopps qnreset(sc);
322 1.1 chopps }
323 1.1 chopps
324 1.1 chopps
325 1.1 chopps /*
326 1.1 chopps * Flush card's buffer RAM
327 1.1 chopps */
328 1.1 chopps static void
329 1.1 chopps qn_flush(sc)
330 1.1 chopps struct qn_softc *sc;
331 1.1 chopps {
332 1.1 chopps #ifdef QN_DEBUG1
333 1.1 chopps int cnt = 0;
334 1.1 chopps #endif
335 1.1 chopps /* Read data until bus read error (i.e. buffer empty). */
336 1.1 chopps while (!(*sc->nic_r_status & R_BUS_RD_ERR)) {
337 1.1 chopps (void)(*sc->nic_fifo);
338 1.1 chopps #ifdef QN_DEBUG1
339 1.1 chopps cnt++;
340 1.1 chopps #endif
341 1.1 chopps }
342 1.1 chopps #ifdef QN_DEBUG1
343 1.1 chopps log(LOG_INFO, "Flushed %d words\n", cnt);
344 1.1 chopps #endif
345 1.1 chopps
346 1.1 chopps /* Clear bus read error. */
347 1.1 chopps *sc->nic_r_status = R_BUS_RD_ERR;
348 1.1 chopps }
349 1.1 chopps
350 1.1 chopps
351 1.1 chopps /*
352 1.1 chopps * Reset the interface...
353 1.1 chopps *
354 1.1 chopps */
355 1.1 chopps void
356 1.1 chopps qnreset(sc)
357 1.1 chopps struct qn_softc *sc;
358 1.1 chopps {
359 1.1 chopps int s;
360 1.1 chopps
361 1.1 chopps s = splimp();
362 1.1 chopps qnstop(sc);
363 1.1 chopps qninit(sc);
364 1.1 chopps splx(s);
365 1.1 chopps }
366 1.1 chopps
367 1.1 chopps
368 1.1 chopps /*
369 1.1 chopps * Take interface offline
370 1.1 chopps */
371 1.1 chopps void
372 1.1 chopps qnstop(sc)
373 1.1 chopps struct qn_softc *sc;
374 1.1 chopps {
375 1.1 chopps
376 1.1 chopps /* Stop the interface. */
377 1.1 chopps *sc->nic_reset = DISABLE_DLC;
378 1.1 chopps
379 1.1 chopps *sc->nic_t_mask = CLEAR_T_MASK;
380 1.1 chopps *sc->nic_r_mask = CLEAR_R_MASK;
381 1.1 chopps
382 1.1 chopps qn_flush(sc);
383 1.1 chopps }
384 1.1 chopps
385 1.1 chopps
386 1.1 chopps /*
387 1.1 chopps * Start output on interface. Get another datagram to send
388 1.1 chopps * off the interface queue, and copy it to the
389 1.1 chopps * interface before starting the output.
390 1.1 chopps *
391 1.1 chopps * This assumes that it is called inside a critical section...
392 1.1 chopps *
393 1.1 chopps */
394 1.1 chopps void
395 1.1 chopps qnstart(ifp)
396 1.1 chopps struct ifnet *ifp;
397 1.1 chopps {
398 1.1 chopps struct qn_softc *sc = qncd.cd_devs[ifp->if_unit];
399 1.1 chopps struct mbuf *m;
400 1.1 chopps u_short len;
401 1.1 chopps int timout = 50000;
402 1.1 chopps
403 1.1 chopps if ((sc->sc_arpcom.ac_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
404 1.1 chopps IFF_RUNNING)
405 1.1 chopps return;
406 1.1 chopps
407 1.1 chopps IF_DEQUEUE(&sc->sc_arpcom.ac_if.if_snd, m);
408 1.1 chopps if (m == 0)
409 1.1 chopps return;
410 1.1 chopps
411 1.1 chopps #if NBPFILTER > 0
412 1.1 chopps /*
413 1.1 chopps * If bpf is listening on this interface, let it
414 1.1 chopps * see the packet before we commit it to the wire
415 1.1 chopps *
416 1.1 chopps * (can't give the copy in QuickNet card RAM to bpf, because
417 1.1 chopps * that RAM is not visible to the host but is read from FIFO)
418 1.1 chopps *
419 1.1 chopps */
420 1.1 chopps log(LOG_INFO, "NBPFILTER... no-one has tested this with qn.\n");
421 1.1 chopps if (sc->sc_bpf)
422 1.1 chopps bpf_mtap(sc->sc_bpf, m);
423 1.1 chopps #endif
424 1.1 chopps len = qn_put(sc->nic_fifo, m);
425 1.1 chopps
426 1.1 chopps /*
427 1.1 chopps * Really transmit the packet
428 1.1 chopps */
429 1.1 chopps
430 1.1 chopps /* set packet length (byte-swapped) */
431 1.1 chopps len = ((len >> 8) & 0x0007) | TRANSMIT_START | ((len & 0x00ff) << 8);
432 1.1 chopps *sc->nic_len = len;
433 1.1 chopps
434 1.1 chopps /* Wait for the packet to really leave */
435 1.1 chopps while (!(*sc->nic_t_status & T_TMT_OK) && --timout)
436 1.1 chopps ;
437 1.1 chopps if (timout == 0)
438 1.1 chopps /* Maybe we should try to recover from this one? */
439 1.1 chopps /* But for now on, let's just fall thru and hope the best... */
440 1.1 chopps log(LOG_INFO, "qn:timout\n");
441 1.1 chopps
442 1.1 chopps sc->transmit_pending = 1;
443 1.1 chopps *sc->nic_t_mask = INT_TMT_OK | INT_SIXTEEN_COL;
444 1.1 chopps
445 1.1 chopps sc->sc_arpcom.ac_if.if_flags |= IFF_OACTIVE;
446 1.1 chopps ifp->if_timer = 2;
447 1.1 chopps }
448 1.1 chopps
449 1.1 chopps
450 1.1 chopps /*
451 1.1 chopps * Memory copy, copies word at a time
452 1.1 chopps */
453 1.1 chopps static void inline
454 1.1 chopps word_copy_from_card(card, b, len)
455 1.1 chopps u_short volatile *card;
456 1.1 chopps u_short *b, len;
457 1.1 chopps {
458 1.1 chopps register u_short l = len/2;
459 1.1 chopps
460 1.1 chopps while (l--)
461 1.1 chopps *b++ = *card;
462 1.1 chopps }
463 1.1 chopps
464 1.1 chopps static void inline
465 1.1 chopps word_copy_to_card(a, card, len)
466 1.1 chopps u_short *a, len;
467 1.1 chopps u_short volatile *card;
468 1.1 chopps {
469 1.1 chopps register u_short l = len/2;
470 1.1 chopps
471 1.1 chopps while (l--)
472 1.1 chopps *card = *a++;
473 1.1 chopps }
474 1.1 chopps
475 1.1 chopps
476 1.1 chopps /*
477 1.1 chopps * Copy packet from mbuf to the board memory
478 1.1 chopps *
479 1.1 chopps * Uses an extra buffer/extra memory copy,
480 1.1 chopps * unless the whole packet fits in one mbuf.
481 1.1 chopps *
482 1.1 chopps */
483 1.1 chopps static u_short
484 1.1 chopps qn_put(addr, m)
485 1.1 chopps u_short volatile *addr;
486 1.1 chopps struct mbuf *m;
487 1.1 chopps {
488 1.1 chopps struct mbuf *mp;
489 1.1 chopps register u_short len, tlen;
490 1.1 chopps static u_short packet_buf[1536/2];
491 1.1 chopps register u_char *p;
492 1.1 chopps
493 1.1 chopps /*
494 1.1 chopps * If buffer is bzeroed, we might copy max(60, tlen) bytes and get
495 1.1 chopps * rid of extra zero fill-ins.
496 1.1 chopps */
497 1.1 chopps bzero(packet_buf, 1536);
498 1.1 chopps
499 1.1 chopps /*
500 1.1 chopps * The whole packet in one mbuf?
501 1.1 chopps */
502 1.1 chopps if (m->m_next == NULL) {
503 1.1 chopps tlen = m->m_len;
504 1.1 chopps bcopy(mtod(m, u_char *), (u_char *)packet_buf, tlen);
505 1.1 chopps /*word_copy_to_card(mtod(m, u_short *), addr, tlen + 1);*/
506 1.1 chopps word_copy_to_card(packet_buf,
507 1.1 chopps addr,
508 1.1 chopps max(tlen + 1, ETHER_MIN_LEN-4));
509 1.1 chopps } else {
510 1.1 chopps /* No it wasn't, let's start copying */
511 1.1 chopps for (p = (u_char *)packet_buf, tlen = 0, mp = m;
512 1.1 chopps mp;
513 1.1 chopps mp = mp->m_next) {
514 1.1 chopps if ((len = mp->m_len) == 0)
515 1.1 chopps continue;
516 1.1 chopps tlen += len;
517 1.1 chopps bcopy(mtod(mp, u_char *), p, len);
518 1.1 chopps p += len;
519 1.1 chopps }
520 1.1 chopps /* word_copy_to_card(packet_buf, addr, tlen + 1); */
521 1.1 chopps word_copy_to_card(packet_buf,
522 1.1 chopps addr,
523 1.1 chopps max(tlen + 1, ETHER_MIN_LEN-4));
524 1.1 chopps }
525 1.1 chopps m_freem(m);
526 1.1 chopps
527 1.1 chopps if (tlen < ETHER_MIN_LEN - 4)
528 1.1 chopps /* We have copied ETHER_MIN_LEN-4 bytes. */
529 1.1 chopps tlen = ETHER_MIN_LEN - 4;
530 1.1 chopps return (tlen);
531 1.1 chopps }
532 1.1 chopps
533 1.1 chopps /*
534 1.1 chopps * Copy packet from board RAM
535 1.1 chopps *
536 1.1 chopps * Trailers not supported.
537 1.1 chopps *
538 1.1 chopps */
539 1.1 chopps static void
540 1.1 chopps qn_get_packet(sc, len)
541 1.1 chopps struct qn_softc *sc;
542 1.1 chopps u_short len;
543 1.1 chopps {
544 1.1 chopps register u_short volatile *nic_fifo_ptr = sc->nic_fifo;
545 1.1 chopps struct ether_header *eh;
546 1.1 chopps struct mbuf *m, *dst, *head = 0;
547 1.1 chopps register u_short len1;
548 1.1 chopps u_short amount;
549 1.1 chopps
550 1.1 chopps /* Allocate header mbuf. */
551 1.1 chopps MGETHDR(m, M_DONTWAIT, MT_DATA);
552 1.1 chopps if (m == 0)
553 1.1 chopps goto bad;
554 1.1 chopps
555 1.1 chopps m->m_pkthdr.rcvif = &sc->sc_arpcom.ac_if;
556 1.1 chopps m->m_pkthdr.len = len;
557 1.1 chopps m->m_len = 0;
558 1.1 chopps head = m;
559 1.1 chopps
560 1.1 chopps eh = mtod(head, struct ether_header *);
561 1.1 chopps
562 1.1 chopps word_copy_from_card(nic_fifo_ptr,
563 1.1 chopps mtod(head, u_short *),
564 1.1 chopps sizeof(struct ether_header));
565 1.1 chopps
566 1.1 chopps head->m_len += sizeof(struct ether_header);
567 1.1 chopps len -= sizeof(struct ether_header);
568 1.1 chopps
569 1.1 chopps while (len > 0) {
570 1.1 chopps len1 = len;
571 1.1 chopps
572 1.1 chopps amount = M_TRAILINGSPACE(m);
573 1.1 chopps if (amount == 0) {
574 1.1 chopps /* allocate another mbuf */
575 1.1 chopps dst = m;
576 1.1 chopps MGET(m, M_DONTWAIT, MT_DATA);
577 1.1 chopps if (m == 0)
578 1.1 chopps goto bad;
579 1.1 chopps
580 1.1 chopps if (len1 >= MINCLSIZE)
581 1.1 chopps MCLGET(m, M_DONTWAIT);
582 1.1 chopps
583 1.1 chopps m->m_len = 0;
584 1.1 chopps dst->m_next = m;
585 1.1 chopps
586 1.1 chopps amount = M_TRAILINGSPACE(m);
587 1.1 chopps }
588 1.1 chopps
589 1.1 chopps if (amount < len1) {
590 1.1 chopps if (amount & 1)
591 1.1 chopps log(LOG_INFO, "Room for odd-length packet\n");
592 1.1 chopps len1 = amount;
593 1.1 chopps }
594 1.1 chopps word_copy_from_card(nic_fifo_ptr,
595 1.1 chopps (u_short *)(mtod(m, caddr_t) + m->m_len),
596 1.1 chopps len1 + 1);
597 1.1 chopps m->m_len += len1;
598 1.1 chopps len -= len1;
599 1.1 chopps }
600 1.1 chopps
601 1.1 chopps #if NBPFILTER > 0
602 1.1 chopps log(LOG_INFO, "qn: Beware, an untested code section\n");
603 1.1 chopps if (sc->sc_bpf) {
604 1.1 chopps bpf_mtap(sc->sc_bpf, head);
605 1.1 chopps
606 1.1 chopps /*
607 1.1 chopps * The interface cannot be in promiscuous mode if there are
608 1.1 chopps * no BPF listeners. And in prom. mode we have to check
609 1.1 chopps * if the packet is really ours...
610 1.1 chopps */
611 1.1 chopps if ((sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) &&
612 1.1 chopps (eh->ether_dhost[0] & 1) == 0 && /* not bcast or mcast */
613 1.1 chopps bcmp(eh->ether_dhost,
614 1.1 chopps sc->sc_arpcom.ac_enaddr,
615 1.1 chopps ETHER_ADDR_LEN) != 0) {
616 1.1 chopps m_freem(head);
617 1.1 chopps return;
618 1.1 chopps }
619 1.1 chopps }
620 1.1 chopps #endif
621 1.1 chopps
622 1.1 chopps m_adj(head, sizeof(struct ether_header));
623 1.1 chopps ether_input(&sc->sc_arpcom.ac_if, eh, head);
624 1.1 chopps return;
625 1.1 chopps
626 1.1 chopps bad:
627 1.1 chopps if (head)
628 1.1 chopps m_freem(head);
629 1.1 chopps }
630 1.1 chopps
631 1.1 chopps
632 1.1 chopps /*
633 1.1 chopps * Ethernet interface receiver interrupt.
634 1.1 chopps */
635 1.1 chopps static void
636 1.1 chopps qn_rint(sc)
637 1.1 chopps struct qn_softc *sc;
638 1.1 chopps {
639 1.1 chopps int i;
640 1.1 chopps u_short len, status;
641 1.1 chopps
642 1.1 chopps /*
643 1.1 chopps * Read at most 25 packets per interrupt
644 1.1 chopps */
645 1.1 chopps for (i = 0; i < 25; i++) {
646 1.1 chopps if (*sc->nic_r_mode & RM_BUF_EMP) {
647 1.1 chopps /*
648 1.1 chopps * Buffer empty
649 1.1 chopps */
650 1.1 chopps if ((status = *sc->nic_r_status)) {
651 1.1 chopps /* It was some error: let's first clear
652 1.1 chopps * the register.
653 1.1 chopps */
654 1.1 chopps *sc->nic_r_status = CLEAR_R_ERR;
655 1.1 chopps if (status & 0x0101) {
656 1.1 chopps #ifdef QN_DEBUG
657 1.1 chopps log(LOG_INFO, "Overflow\n");
658 1.1 chopps #endif
659 1.1 chopps ++sc->sc_arpcom.ac_if.if_ierrors;
660 1.1 chopps } else if (status & 0x0202) {
661 1.1 chopps #ifdef QN_DEBUG
662 1.1 chopps log(LOG_INFO, "CRC Error\n");
663 1.1 chopps #endif
664 1.1 chopps ++sc->sc_arpcom.ac_if.if_ierrors;
665 1.1 chopps } else if (status & 0x0404) {
666 1.1 chopps #ifdef QN_DEBUG
667 1.1 chopps log(LOG_INFO, "Alignment error\n");
668 1.1 chopps #endif
669 1.1 chopps ++sc->sc_arpcom.ac_if.if_ierrors;
670 1.1 chopps } else if (status & 0x0808) {
671 1.1 chopps /* Short packet (these may occur and are
672 1.1 chopps * no reason to worry about - or maybe
673 1.1 chopps * they are?).
674 1.1 chopps */
675 1.1 chopps #ifdef QN_DEBUG
676 1.1 chopps log(LOG_INFO, "Short packet\n");
677 1.1 chopps #endif
678 1.1 chopps ++sc->sc_arpcom.ac_if.if_ierrors;
679 1.1 chopps /* qnreset(sc); */
680 1.1 chopps } else if (status & 0x4040) {
681 1.1 chopps #ifdef QN_DEBUG
682 1.1 chopps log(LOG_INFO, "Bus read error\n");
683 1.1 chopps #endif
684 1.1 chopps ++sc->sc_arpcom.ac_if.if_ierrors;
685 1.1 chopps } else {
686 1.1 chopps /* There are some registers which are
687 1.1 chopps * not meaningful when read, so don't
688 1.1 chopps * care...
689 1.1 chopps */
690 1.1 chopps }
691 1.1 chopps }
692 1.1 chopps return;
693 1.1 chopps } else {
694 1.1 chopps /* At least one valid packet. Clear the whole register. */
695 1.1 chopps *sc->nic_r_status = CLEAR_R_ERR;
696 1.1 chopps
697 1.1 chopps /* Read one word of garbage. */
698 1.1 chopps (void)(*sc->nic_fifo);
699 1.1 chopps
700 1.1 chopps /* Read packet length. */
701 1.1 chopps len = *sc->nic_fifo;
702 1.1 chopps len = ((len << 8) & 0xff00) | ((len >> 8) & 0x00ff);
703 1.1 chopps
704 1.1 chopps /* Read the packet. */
705 1.1 chopps qn_get_packet(sc, len);
706 1.1 chopps ++sc->sc_arpcom.ac_if.if_ipackets;
707 1.1 chopps }
708 1.1 chopps }
709 1.1 chopps
710 1.1 chopps if (i == 25)
711 1.1 chopps log(LOG_INFO, "used all the 25 loops\n");
712 1.1 chopps }
713 1.1 chopps
714 1.1 chopps
715 1.1 chopps /*
716 1.1 chopps * Our interrupt routine
717 1.1 chopps */
718 1.1 chopps int
719 1.1 chopps qnintr(sc)
720 1.1 chopps struct qn_softc *sc;
721 1.1 chopps {
722 1.1 chopps u_short tint, rint, tintmask, rintmask;
723 1.1 chopps
724 1.1 chopps /*
725 1.1 chopps * If the driver has not been initialized, just return immediately.
726 1.1 chopps * This also happens if there is no QuickNet board present.
727 1.1 chopps */
728 1.1 chopps
729 1.1 chopps if (sc->sc_base == NULL)
730 1.1 chopps return (0);
731 1.1 chopps
732 1.1 chopps /* Get interrupt statuses and masks. */
733 1.1 chopps rint = *sc->nic_r_status;
734 1.1 chopps tint = *sc->nic_t_status;
735 1.1 chopps rintmask = *sc->nic_r_mask /* 0x8f8f */;
736 1.1 chopps tintmask = *sc->nic_t_mask;
737 1.1 chopps if (!(tint&tintmask) && !(rint&rintmask))
738 1.1 chopps return (0);
739 1.1 chopps
740 1.1 chopps /* Disable receive interrupts so that we won't miss anything. */
741 1.1 chopps *sc->nic_r_mask = CLEAR_R_MASK;
742 1.1 chopps
743 1.1 chopps /*
744 1.1 chopps * Handle transmitter interrupts. Some of them are not asked for
745 1.1 chopps * but do happen, anyway.
746 1.1 chopps */
747 1.1 chopps if (tint) {
748 1.1 chopps *sc->nic_t_mask = CLEAR_T_MASK;
749 1.1 chopps *sc->nic_t_status = CLEAR_T_ERR;
750 1.1 chopps if (sc->transmit_pending && (tint & T_TMT_OK)) {
751 1.1 chopps sc->transmit_pending = 0;
752 1.1 chopps /*
753 1.1 chopps * Update total number of successfully
754 1.1 chopps * transmitted packets.
755 1.1 chopps */
756 1.1 chopps sc->sc_arpcom.ac_if.if_opackets++;
757 1.1 chopps }
758 1.1 chopps
759 1.1 chopps if (tint & T_SIXTEEN_COL) {
760 1.1 chopps /*
761 1.1 chopps * 16 collision (i.e., packet lost).
762 1.1 chopps */
763 1.1 chopps log(LOG_INFO, "qn: 16 collision - packet lost\n");
764 1.1 chopps sc->sc_arpcom.ac_if.if_oerrors++;
765 1.1 chopps sc->sc_arpcom.ac_if.if_collisions += 16;
766 1.1 chopps sc->transmit_pending = 0;
767 1.1 chopps }
768 1.1 chopps
769 1.1 chopps if (tint & T_COL && !(tint & T_TMT_OK)) {
770 1.1 chopps /*
771 1.1 chopps * Normal collision
772 1.1 chopps */
773 1.1 chopps log(LOG_INFO, "qn:collision (shouldn't hurt)\n");
774 1.1 chopps sc->transmit_pending = 1;
775 1.1 chopps sc->sc_arpcom.ac_if.if_oerrors++;
776 1.1 chopps sc->sc_arpcom.ac_if.if_collisions++;
777 1.1 chopps }
778 1.1 chopps
779 1.1 chopps if (tint & BUS_WRITE_ERROR) {
780 1.1 chopps /* One bus write error occurs at start up, at least on my
781 1.1 chopps * card. So I don't care about this one.
782 1.1 chopps */
783 1.1 chopps sc->transmit_pending = 0;
784 1.1 chopps }
785 1.1 chopps
786 1.1 chopps if (tint & T_UNDERFLOW) {
787 1.1 chopps log(LOG_INFO, "qn:underflow\n");
788 1.1 chopps }
789 1.1 chopps
790 1.1 chopps if (sc->transmit_pending) {
791 1.1 chopps log(LOG_INFO, "qn:still pending...\n");
792 1.1 chopps /* Return transmission interrupt mask. */
793 1.1 chopps *sc->nic_t_mask = tintmask;
794 1.1 chopps } else {
795 1.1 chopps sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
796 1.1 chopps
797 1.1 chopps /* Clear watchdog timer. */
798 1.1 chopps sc->sc_arpcom.ac_if.if_timer = 0;
799 1.1 chopps }
800 1.1 chopps }
801 1.1 chopps
802 1.1 chopps /*
803 1.1 chopps * Handle receiver interrupts.
804 1.1 chopps */
805 1.1 chopps if (rint & rintmask)
806 1.1 chopps qn_rint(sc);
807 1.1 chopps
808 1.1 chopps /* Set receive interrupt mask back. */
809 1.1 chopps *sc->nic_r_mask = rintmask;
810 1.1 chopps
811 1.1 chopps if ((sc->sc_arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
812 1.1 chopps qnstart(&sc->sc_arpcom.ac_if);
813 1.1 chopps
814 1.1 chopps return (1);
815 1.1 chopps }
816 1.1 chopps
817 1.1 chopps /*
818 1.1 chopps * Process an ioctl request. This code needs some work - it looks pretty ugly.
819 1.1 chopps * I somehow think that this is quite a common excuse... ;-)
820 1.1 chopps */
821 1.1 chopps int
822 1.1 chopps qnioctl(ifp, command, data)
823 1.1 chopps register struct ifnet *ifp;
824 1.1 chopps u_long command;
825 1.1 chopps caddr_t data;
826 1.1 chopps {
827 1.1 chopps struct qn_softc *sc = qncd.cd_devs[ifp->if_unit];
828 1.1 chopps register struct ifaddr *ifa = (struct ifaddr *)data;
829 1.1 chopps #if 0
830 1.1 chopps struct ifreg *ifr = (struct ifreg *)data;
831 1.1 chopps #endif
832 1.1 chopps int s, error = 0;
833 1.1 chopps
834 1.1 chopps s = splimp();
835 1.1 chopps
836 1.1 chopps switch (command) {
837 1.1 chopps
838 1.1 chopps case SIOCSIFADDR:
839 1.1 chopps ifp->if_flags |= IFF_UP;
840 1.1 chopps
841 1.1 chopps switch (ifa->ifa_addr->sa_family) {
842 1.1 chopps #ifdef INET
843 1.1 chopps case AF_INET:
844 1.1 chopps qninit(sc);
845 1.1 chopps arp_ifinit(&sc->sc_arpcom, ifa);
846 1.1 chopps break;
847 1.1 chopps #endif
848 1.1 chopps #ifdef NS
849 1.1 chopps case AF_NS:
850 1.1 chopps {
851 1.1 chopps register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
852 1.1 chopps
853 1.1 chopps if (ns_nullhost(*ina))
854 1.1 chopps ina->x_host =
855 1.1 chopps *(union ns_host *)(sc->sc_arpcom.sc_enaddr);
856 1.1 chopps else
857 1.1 chopps bcopy(ina->x_host.c_host,
858 1.1 chopps sc->sc_arpcom.ac_enaddr,
859 1.1 chopps sizeof(sc->sc_arpcom.ac_enaddr));
860 1.1 chopps qninit(sc);
861 1.1 chopps break;
862 1.1 chopps }
863 1.1 chopps #endif
864 1.1 chopps default:
865 1.1 chopps log(LOG_INFO, "qn:sa_family:default (not tested)\n");
866 1.1 chopps qninit(sc);
867 1.1 chopps break;
868 1.1 chopps }
869 1.1 chopps break;
870 1.1 chopps
871 1.1 chopps case SIOCSIFFLAGS:
872 1.1 chopps /*
873 1.1 chopps * If interface is marked down and it is running, then stop it.
874 1.1 chopps */
875 1.1 chopps if ((ifp->if_flags & IFF_UP) == 0 &&
876 1.1 chopps (ifp->if_flags & IFF_RUNNING) != 0) {
877 1.1 chopps /*
878 1.1 chopps * If interface is marked down and it is running, then
879 1.1 chopps * stop it.
880 1.1 chopps */
881 1.1 chopps qnstop(sc);
882 1.1 chopps ifp->if_flags &= ~IFF_RUNNING;
883 1.1 chopps } else if ((ifp->if_flags & IFF_UP) != 0 &&
884 1.1 chopps (ifp->if_flags & IFF_RUNNING) == 0)
885 1.1 chopps /*
886 1.1 chopps * If interface is marked up and it is stopped, then
887 1.1 chopps * start it.
888 1.1 chopps */
889 1.1 chopps qninit(sc);
890 1.1 chopps else {
891 1.1 chopps /*
892 1.1 chopps * Something else... we won't do anything so we won't
893 1.1 chopps * break anything (hope so).
894 1.1 chopps */
895 1.1 chopps }
896 1.1 chopps break;
897 1.1 chopps
898 1.1 chopps case SIOCADDMULTI:
899 1.1 chopps case SIOCDELMULTI:
900 1.1 chopps log(LOG_INFO, "qnioctl: multicast not done yet\n");
901 1.1 chopps #if 0
902 1.1 chopps error = (command == SIOCADDMULTI) ?
903 1.1 chopps ether_addmulti(ifr, &sc->sc_arpcom) :
904 1.1 chopps ether_delmulti(ifr, &sc->sc_arpcom);
905 1.1 chopps
906 1.1 chopps if (error == ENETRESET) {
907 1.1 chopps /*
908 1.1 chopps * Multicast list has changed; set the hardware filter
909 1.1 chopps * accordingly.
910 1.1 chopps */
911 1.1 chopps log(LOG_INFO, "qnioctl: multicast not done yet\n");
912 1.1 chopps error = 0;
913 1.1 chopps }
914 1.1 chopps #else
915 1.1 chopps error = EINVAL;
916 1.1 chopps #endif
917 1.1 chopps break;
918 1.1 chopps
919 1.1 chopps default:
920 1.1 chopps log(LOG_INFO, "qnioctl: default\n");
921 1.1 chopps error = EINVAL;
922 1.1 chopps }
923 1.1 chopps
924 1.1 chopps (void)splx(s);
925 1.1 chopps return (error);
926 1.1 chopps }
927 1.1 chopps
928 1.1 chopps #endif /* NQN > 0 */
929