if_qn.c revision 1.35 1 1.35 cegger /* $NetBSD: if_qn.c,v 1.35 2009/10/26 19:16:54 cegger 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.2 jtc * - if_ed.c: basic function structure for Ethernet driver and now also
39 1.2 jtc * qn_put() is done similarly, i.e. no extra packet buffers.
40 1.1 chopps *
41 1.1 chopps * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
42 1.1 chopps * adapters.
43 1.1 chopps *
44 1.1 chopps * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
45 1.1 chopps *
46 1.1 chopps * Copyright (C) 1993, David Greenman. This software may be used,
47 1.1 chopps * modified, copied, distributed, and sold, in both source and binary
48 1.1 chopps * form provided that the above copyright and these terms are retained.
49 1.1 chopps * Under no circumstances is the author responsible for the proper
50 1.1 chopps * functioning of this software, nor does the author assume any
51 1.1 chopps * responsibility for damages incurred with its use.
52 1.1 chopps *
53 1.1 chopps * - if_es.c: used as an example of -current driver
54 1.1 chopps *
55 1.1 chopps * Copyright (c) 1995 Michael L. Hitch
56 1.1 chopps * All rights reserved.
57 1.1 chopps *
58 1.2 jtc * - if_fe.c: some ideas for error handling for qn_rint() which might
59 1.2 jtc * have fixed those random lock ups, too.
60 1.2 jtc *
61 1.2 jtc * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
62 1.2 jtc *
63 1.1 chopps *
64 1.1 chopps * TODO:
65 1.1 chopps * - add multicast support
66 1.1 chopps */
67 1.22 aymeric
68 1.22 aymeric #include <sys/cdefs.h>
69 1.35 cegger __KERNEL_RCSID(0, "$NetBSD: if_qn.c,v 1.35 2009/10/26 19:16:54 cegger Exp $");
70 1.1 chopps
71 1.1 chopps #include "qn.h"
72 1.1 chopps #if NQN > 0
73 1.1 chopps
74 1.1 chopps #define QN_DEBUG
75 1.2 jtc #define QN_DEBUG1_no /* hides some old tests */
76 1.7 is #define QN_CHECKS_no /* adds some checks (not needed in normal situations) */
77 1.1 chopps
78 1.1 chopps #include "bpfilter.h"
79 1.1 chopps
80 1.1 chopps /*
81 1.2 jtc * Fujitsu MB86950 Ethernet Controller (as used in the QuickNet QN2000
82 1.2 jtc * Ethernet card)
83 1.1 chopps */
84 1.15 jonathan
85 1.15 jonathan #include "opt_inet.h"
86 1.16 jonathan #include "opt_ns.h"
87 1.1 chopps
88 1.1 chopps #include <sys/param.h>
89 1.1 chopps #include <sys/systm.h>
90 1.1 chopps #include <sys/mbuf.h>
91 1.1 chopps #include <sys/buf.h>
92 1.1 chopps #include <sys/device.h>
93 1.1 chopps #include <sys/protosw.h>
94 1.1 chopps #include <sys/socket.h>
95 1.1 chopps #include <sys/syslog.h>
96 1.1 chopps #include <sys/ioctl.h>
97 1.1 chopps #include <sys/errno.h>
98 1.1 chopps
99 1.1 chopps #include <net/if.h>
100 1.12 is #include <net/if_dl.h>
101 1.11 is #include <net/if_ether.h>
102 1.1 chopps
103 1.1 chopps #ifdef INET
104 1.1 chopps #include <netinet/in.h>
105 1.1 chopps #include <netinet/in_systm.h>
106 1.1 chopps #include <netinet/in_var.h>
107 1.1 chopps #include <netinet/ip.h>
108 1.11 is #include <netinet/if_inarp.h>
109 1.1 chopps #endif
110 1.1 chopps
111 1.1 chopps #ifdef NS
112 1.1 chopps #include <netns/ns.h>
113 1.1 chopps #include <netns/ns_if.h>
114 1.1 chopps #endif
115 1.1 chopps
116 1.1 chopps #include <machine/cpu.h>
117 1.1 chopps #include <amiga/amiga/device.h>
118 1.1 chopps #include <amiga/amiga/isr.h>
119 1.1 chopps #include <amiga/dev/zbusvar.h>
120 1.1 chopps #include <amiga/dev/if_qnreg.h>
121 1.1 chopps
122 1.1 chopps
123 1.2 jtc #define NIC_R_MASK (R_INT_PKT_RDY | R_INT_ALG_ERR |\
124 1.2 jtc R_INT_CRC_ERR | R_INT_OVR_FLO)
125 1.2 jtc #define MAX_PACKETS 30 /* max number of packets read per interrupt */
126 1.1 chopps
127 1.1 chopps /*
128 1.1 chopps * Ethernet software status per interface
129 1.1 chopps *
130 1.1 chopps * Each interface is referenced by a network interface structure,
131 1.1 chopps * qn_if, which the routing code uses to locate the interface.
132 1.1 chopps * This structure contains the output queue for the interface, its address, ...
133 1.1 chopps */
134 1.1 chopps struct qn_softc {
135 1.1 chopps struct device sc_dev;
136 1.1 chopps struct isr sc_isr;
137 1.11 is struct ethercom sc_ethercom; /* Common ethernet structures */
138 1.1 chopps u_char volatile *sc_base;
139 1.1 chopps u_char volatile *sc_nic_base;
140 1.1 chopps u_short volatile *nic_fifo;
141 1.1 chopps u_short volatile *nic_r_status;
142 1.1 chopps u_short volatile *nic_t_status;
143 1.1 chopps u_short volatile *nic_r_mask;
144 1.1 chopps u_short volatile *nic_t_mask;
145 1.1 chopps u_short volatile *nic_r_mode;
146 1.1 chopps u_short volatile *nic_t_mode;
147 1.1 chopps u_short volatile *nic_reset;
148 1.1 chopps u_short volatile *nic_len;
149 1.1 chopps u_char transmit_pending;
150 1.1 chopps #if NBPFILTER > 0
151 1.26 christos void * sc_bpf;
152 1.1 chopps #endif
153 1.1 chopps } qn_softc[NQN];
154 1.1 chopps
155 1.1 chopps #if NBPFILTER > 0
156 1.1 chopps #include <net/bpf.h>
157 1.1 chopps #include <net/bpfdesc.h>
158 1.1 chopps #endif
159 1.1 chopps
160 1.1 chopps
161 1.21 aymeric int qnmatch(struct device *, struct cfdata *, void *);
162 1.21 aymeric void qnattach(struct device *, struct device *, void *);
163 1.21 aymeric int qnintr(void *);
164 1.26 christos int qnioctl(struct ifnet *, u_long, void *);
165 1.21 aymeric void qnstart(struct ifnet *);
166 1.21 aymeric void qnwatchdog(struct ifnet *);
167 1.21 aymeric void qnreset(struct qn_softc *);
168 1.21 aymeric void qninit(struct qn_softc *);
169 1.21 aymeric void qnstop(struct qn_softc *);
170 1.21 aymeric static u_short qn_put(u_short volatile *, struct mbuf *);
171 1.21 aymeric static void qn_rint(struct qn_softc *, u_short);
172 1.21 aymeric static void qn_flush(struct qn_softc *);
173 1.21 aymeric static void inline word_copy_from_card(u_short volatile *, u_short *, u_short);
174 1.21 aymeric static void inline word_copy_to_card(u_short *, u_short volatile *,
175 1.21 aymeric register u_short);
176 1.21 aymeric static void qn_get_packet(struct qn_softc *, u_short);
177 1.2 jtc #ifdef QN_DEBUG1
178 1.21 aymeric static void qn_dump(struct qn_softc *);
179 1.2 jtc #endif
180 1.1 chopps
181 1.24 thorpej CFATTACH_DECL(qn, sizeof(struct qn_softc),
182 1.24 thorpej qnmatch, qnattach, NULL, NULL);
183 1.2 jtc
184 1.1 chopps int
185 1.21 aymeric qnmatch(struct device *parent, struct cfdata *cfp, void *aux)
186 1.1 chopps {
187 1.1 chopps struct zbus_args *zap;
188 1.1 chopps
189 1.1 chopps zap = (struct zbus_args *)aux;
190 1.1 chopps
191 1.1 chopps /* RMF QuickNet QN2000 EtherNet card */
192 1.1 chopps if (zap->manid == 2011 && zap->prodid == 2)
193 1.1 chopps return (1);
194 1.1 chopps
195 1.1 chopps return (0);
196 1.1 chopps }
197 1.1 chopps
198 1.1 chopps /*
199 1.1 chopps * Interface exists: make available by filling in network interface
200 1.1 chopps * record. System will initialize the interface when it is ready
201 1.1 chopps * to accept packets.
202 1.1 chopps */
203 1.1 chopps void
204 1.21 aymeric qnattach(struct device *parent, struct device *self, void *aux)
205 1.1 chopps {
206 1.1 chopps struct zbus_args *zap;
207 1.1 chopps struct qn_softc *sc = (struct qn_softc *)self;
208 1.11 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
209 1.11 is u_int8_t myaddr[ETHER_ADDR_LEN];
210 1.1 chopps
211 1.1 chopps zap = (struct zbus_args *)aux;
212 1.1 chopps
213 1.1 chopps sc->sc_base = zap->va;
214 1.1 chopps sc->sc_nic_base = sc->sc_base + QUICKNET_NIC_BASE;
215 1.1 chopps sc->nic_fifo = (u_short volatile *)(sc->sc_nic_base + NIC_BMPR0);
216 1.1 chopps sc->nic_len = (u_short volatile *)(sc->sc_nic_base + NIC_BMPR2);
217 1.1 chopps sc->nic_t_status = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR0);
218 1.1 chopps sc->nic_r_status = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR2);
219 1.1 chopps sc->nic_t_mask = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR1);
220 1.1 chopps sc->nic_r_mask = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR3);
221 1.1 chopps sc->nic_t_mode = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR4);
222 1.1 chopps sc->nic_r_mode = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR5);
223 1.1 chopps sc->nic_reset = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR6);
224 1.1 chopps sc->transmit_pending = 0;
225 1.1 chopps
226 1.1 chopps /*
227 1.1 chopps * The ethernet address of the board (1st three bytes are the vendor
228 1.1 chopps * address, the rest is the serial number of the board).
229 1.1 chopps */
230 1.11 is myaddr[0] = 0x5c;
231 1.11 is myaddr[1] = 0x5c;
232 1.11 is myaddr[2] = 0x00;
233 1.11 is myaddr[3] = (zap->serno >> 16) & 0xff;
234 1.11 is myaddr[4] = (zap->serno >> 8) & 0xff;
235 1.11 is myaddr[5] = zap->serno & 0xff;
236 1.1 chopps
237 1.1 chopps /* set interface to stopped condition (reset) */
238 1.1 chopps qnstop(sc);
239 1.1 chopps
240 1.35 cegger memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
241 1.6 thorpej ifp->if_softc = sc;
242 1.1 chopps ifp->if_ioctl = qnioctl;
243 1.1 chopps ifp->if_watchdog = qnwatchdog;
244 1.1 chopps ifp->if_start = qnstart;
245 1.1 chopps /* XXX IFF_MULTICAST */
246 1.1 chopps ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
247 1.1 chopps ifp->if_mtu = ETHERMTU;
248 1.1 chopps
249 1.1 chopps /* Attach the interface. */
250 1.1 chopps if_attach(ifp);
251 1.11 is ether_ifattach(ifp, myaddr);
252 1.1 chopps
253 1.1 chopps #ifdef QN_DEBUG
254 1.11 is printf(": hardware address %s\n", ether_sprintf(myaddr));
255 1.1 chopps #endif
256 1.1 chopps
257 1.1 chopps sc->sc_isr.isr_intr = qnintr;
258 1.1 chopps sc->sc_isr.isr_arg = sc;
259 1.1 chopps sc->sc_isr.isr_ipl = 2;
260 1.1 chopps add_isr(&sc->sc_isr);
261 1.1 chopps }
262 1.1 chopps
263 1.1 chopps /*
264 1.1 chopps * Initialize device
265 1.1 chopps *
266 1.1 chopps */
267 1.1 chopps void
268 1.21 aymeric qninit(struct qn_softc *sc)
269 1.1 chopps {
270 1.11 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
271 1.1 chopps u_short i;
272 1.14 veego static int retry = 0;
273 1.1 chopps
274 1.2 jtc *sc->nic_r_mask = NIC_R_MASK;
275 1.2 jtc *sc->nic_t_mode = NO_LOOPBACK;
276 1.1 chopps
277 1.11 is if (sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC) {
278 1.1 chopps *sc->nic_r_mode = PROMISCUOUS_MODE;
279 1.1 chopps log(LOG_INFO, "qn: Promiscuous mode (not tested)\n");
280 1.1 chopps } else
281 1.1 chopps *sc->nic_r_mode = NORMAL_MODE;
282 1.1 chopps
283 1.1 chopps /* Set physical ethernet address. */
284 1.1 chopps for (i = 0; i < ETHER_ADDR_LEN; i++)
285 1.2 jtc *((u_short volatile *)(sc->sc_nic_base+
286 1.2 jtc QNET_HARDWARE_ADDRESS+2*i)) =
287 1.28 dyoung ((((u_short)CLLADDR(ifp->if_sadl)[i]) << 8) |
288 1.28 dyoung CLLADDR(ifp->if_sadl)[i]);
289 1.1 chopps
290 1.1 chopps ifp->if_flags |= IFF_RUNNING;
291 1.1 chopps ifp->if_flags &= ~IFF_OACTIVE;
292 1.2 jtc sc->transmit_pending = 0;
293 1.1 chopps
294 1.1 chopps qn_flush(sc);
295 1.1 chopps
296 1.2 jtc /* QuickNet magic. Done ONLY once, otherwise a lockup occurs. */
297 1.2 jtc if (retry == 0) {
298 1.2 jtc *((u_short volatile *)(sc->sc_nic_base + QNET_MAGIC)) = 0;
299 1.2 jtc retry = 1;
300 1.2 jtc }
301 1.2 jtc
302 1.1 chopps /* Enable data link controller. */
303 1.1 chopps *sc->nic_reset = ENABLE_DLC;
304 1.1 chopps
305 1.1 chopps /* Attempt to start output, if any. */
306 1.1 chopps qnstart(ifp);
307 1.1 chopps }
308 1.1 chopps
309 1.1 chopps /*
310 1.1 chopps * Device timeout/watchdog routine. Entered if the device neglects to
311 1.1 chopps * generate an interrupt after a transmit has been started on it.
312 1.1 chopps */
313 1.1 chopps void
314 1.21 aymeric qnwatchdog(struct ifnet *ifp)
315 1.1 chopps {
316 1.6 thorpej struct qn_softc *sc = ifp->if_softc;
317 1.1 chopps
318 1.2 jtc log(LOG_INFO, "qn: device timeout (watchdog)\n");
319 1.11 is ++sc->sc_ethercom.ec_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.2 jtc * Flush card's buffer RAM.
326 1.1 chopps */
327 1.1 chopps static void
328 1.21 aymeric qn_flush(struct qn_softc *sc)
329 1.1 chopps {
330 1.2 jtc #if 1
331 1.1 chopps /* Read data until bus read error (i.e. buffer empty). */
332 1.2 jtc while (!(*sc->nic_r_status & R_BUS_RD_ERR))
333 1.1 chopps (void)(*sc->nic_fifo);
334 1.2 jtc #else
335 1.2 jtc /* Read data twice to clear some internal pipelines. */
336 1.2 jtc (void)(*sc->nic_fifo);
337 1.2 jtc (void)(*sc->nic_fifo);
338 1.1 chopps #endif
339 1.1 chopps
340 1.1 chopps /* Clear bus read error. */
341 1.1 chopps *sc->nic_r_status = R_BUS_RD_ERR;
342 1.1 chopps }
343 1.1 chopps
344 1.1 chopps /*
345 1.2 jtc * Reset the interface.
346 1.1 chopps *
347 1.1 chopps */
348 1.1 chopps void
349 1.21 aymeric qnreset(struct qn_softc *sc)
350 1.1 chopps {
351 1.1 chopps int s;
352 1.1 chopps
353 1.3 mycroft s = splnet();
354 1.1 chopps qnstop(sc);
355 1.1 chopps qninit(sc);
356 1.1 chopps splx(s);
357 1.1 chopps }
358 1.1 chopps
359 1.1 chopps /*
360 1.2 jtc * Take interface offline.
361 1.1 chopps */
362 1.1 chopps void
363 1.21 aymeric qnstop(struct qn_softc *sc)
364 1.1 chopps {
365 1.1 chopps
366 1.1 chopps /* Stop the interface. */
367 1.2 jtc *sc->nic_reset = DISABLE_DLC;
368 1.2 jtc delay(200);
369 1.2 jtc *sc->nic_t_status = CLEAR_T_ERR;
370 1.2 jtc *sc->nic_t_mask = CLEAR_T_MASK;
371 1.2 jtc *sc->nic_r_status = CLEAR_R_ERR;
372 1.2 jtc *sc->nic_r_mask = CLEAR_R_MASK;
373 1.2 jtc
374 1.2 jtc /* Turn DMA off */
375 1.2 jtc *((u_short volatile *)(sc->sc_nic_base + NIC_BMPR4)) = 0;
376 1.1 chopps
377 1.2 jtc /* Accept no packets. */
378 1.2 jtc *sc->nic_r_mode = 0;
379 1.2 jtc *sc->nic_t_mode = 0;
380 1.1 chopps
381 1.1 chopps qn_flush(sc);
382 1.1 chopps }
383 1.1 chopps
384 1.1 chopps /*
385 1.1 chopps * Start output on interface. Get another datagram to send
386 1.1 chopps * off the interface queue, and copy it to the
387 1.1 chopps * interface before starting the output.
388 1.1 chopps *
389 1.1 chopps * This assumes that it is called inside a critical section...
390 1.1 chopps *
391 1.1 chopps */
392 1.1 chopps void
393 1.21 aymeric qnstart(struct ifnet *ifp)
394 1.1 chopps {
395 1.6 thorpej struct qn_softc *sc = ifp->if_softc;
396 1.1 chopps struct mbuf *m;
397 1.1 chopps u_short len;
398 1.2 jtc int timout = 60000;
399 1.2 jtc
400 1.1 chopps
401 1.2 jtc if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
402 1.1 chopps return;
403 1.1 chopps
404 1.11 is IF_DEQUEUE(&ifp->if_snd, m);
405 1.1 chopps if (m == 0)
406 1.1 chopps return;
407 1.1 chopps
408 1.1 chopps #if NBPFILTER > 0
409 1.1 chopps /*
410 1.1 chopps * If bpf is listening on this interface, let it
411 1.1 chopps * see the packet before we commit it to the wire
412 1.1 chopps *
413 1.1 chopps * (can't give the copy in QuickNet card RAM to bpf, because
414 1.1 chopps * that RAM is not visible to the host but is read from FIFO)
415 1.1 chopps *
416 1.1 chopps */
417 1.1 chopps if (sc->sc_bpf)
418 1.1 chopps bpf_mtap(sc->sc_bpf, m);
419 1.1 chopps #endif
420 1.1 chopps len = qn_put(sc->nic_fifo, m);
421 1.2 jtc m_freem(m);
422 1.1 chopps
423 1.1 chopps /*
424 1.2 jtc * Really transmit the packet.
425 1.1 chopps */
426 1.1 chopps
427 1.2 jtc /* Set packet length (byte-swapped). */
428 1.1 chopps len = ((len >> 8) & 0x0007) | TRANSMIT_START | ((len & 0x00ff) << 8);
429 1.1 chopps *sc->nic_len = len;
430 1.1 chopps
431 1.2 jtc /* Wait for the packet to really leave. */
432 1.2 jtc while (!(*sc->nic_t_status & T_TMT_OK) && --timout) {
433 1.2 jtc if ((timout % 10000) == 0)
434 1.2 jtc log(LOG_INFO, "qn: timout...\n");
435 1.2 jtc }
436 1.2 jtc
437 1.1 chopps if (timout == 0)
438 1.1 chopps /* Maybe we should try to recover from this one? */
439 1.2 jtc /* But now, let's just fall thru and hope the best... */
440 1.2 jtc log(LOG_INFO, "qn: transmit timout (fatal?)\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.11 is ifp->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 * Memory copy, copies word at a time
451 1.1 chopps */
452 1.1 chopps static void inline
453 1.21 aymeric word_copy_from_card(u_short volatile *card, u_short *b, u_short len)
454 1.1 chopps {
455 1.1 chopps register u_short l = len/2;
456 1.1 chopps
457 1.1 chopps while (l--)
458 1.1 chopps *b++ = *card;
459 1.1 chopps }
460 1.1 chopps
461 1.1 chopps static void inline
462 1.21 aymeric word_copy_to_card(u_short *a, u_short volatile *card, register u_short len)
463 1.1 chopps {
464 1.7 is /*register u_short l = len/2;*/
465 1.1 chopps
466 1.7 is while (len--)
467 1.1 chopps *card = *a++;
468 1.1 chopps }
469 1.1 chopps
470 1.1 chopps /*
471 1.1 chopps * Copy packet from mbuf to the board memory
472 1.1 chopps *
473 1.1 chopps */
474 1.1 chopps static u_short
475 1.21 aymeric qn_put(u_short volatile *addr, struct mbuf *m)
476 1.1 chopps {
477 1.7 is u_short *data;
478 1.7 is u_char savebyte[2];
479 1.7 is int len, len1, wantbyte;
480 1.2 jtc u_short totlen;
481 1.2 jtc
482 1.2 jtc totlen = wantbyte = 0;
483 1.2 jtc
484 1.2 jtc for (; m != NULL; m = m->m_next) {
485 1.5 veego data = mtod(m, u_short *);
486 1.2 jtc len = m->m_len;
487 1.2 jtc if (len > 0) {
488 1.7 is totlen += len;
489 1.7 is
490 1.2 jtc /* Finish the last word. */
491 1.2 jtc if (wantbyte) {
492 1.7 is savebyte[1] = *((u_char *)data);
493 1.2 jtc *addr = *((u_short *)savebyte);
494 1.25 skrll data = (u_short *)((u_char *)data + 1);
495 1.2 jtc len--;
496 1.2 jtc wantbyte = 0;
497 1.2 jtc }
498 1.2 jtc /* Output contiguous words. */
499 1.2 jtc if (len > 1) {
500 1.7 is len1 = len/2;
501 1.7 is word_copy_to_card(data, addr, len1);
502 1.7 is data += len1;
503 1.2 jtc len &= 1;
504 1.2 jtc }
505 1.2 jtc /* Save last byte, if necessary. */
506 1.2 jtc if (len == 1) {
507 1.7 is savebyte[0] = *((u_char *)data);
508 1.2 jtc wantbyte = 1;
509 1.2 jtc }
510 1.2 jtc }
511 1.2 jtc }
512 1.1 chopps
513 1.2 jtc if (wantbyte) {
514 1.2 jtc savebyte[1] = 0;
515 1.2 jtc *addr = *((u_short *)savebyte);
516 1.2 jtc }
517 1.1 chopps
518 1.17 thorpej if(totlen < (ETHER_MIN_LEN - ETHER_CRC_LEN)) {
519 1.2 jtc /*
520 1.2 jtc * Fill the rest of the packet with zeros.
521 1.2 jtc * N.B.: This is required! Otherwise MB86950 fails.
522 1.2 jtc */
523 1.17 thorpej for(len = totlen + 1; len < (ETHER_MIN_LEN - ETHER_CRC_LEN);
524 1.17 thorpej len += 2)
525 1.2 jtc *addr = (u_short)0x0000;
526 1.17 thorpej totlen = (ETHER_MIN_LEN - ETHER_CRC_LEN);
527 1.1 chopps }
528 1.1 chopps
529 1.2 jtc return (totlen);
530 1.1 chopps }
531 1.1 chopps
532 1.1 chopps /*
533 1.2 jtc * Copy packet from board RAM.
534 1.1 chopps *
535 1.1 chopps * Trailers not supported.
536 1.1 chopps *
537 1.1 chopps */
538 1.1 chopps static void
539 1.21 aymeric qn_get_packet(struct qn_softc *sc, u_short len)
540 1.1 chopps {
541 1.1 chopps register u_short volatile *nic_fifo_ptr = sc->nic_fifo;
542 1.12 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
543 1.2 jtc struct mbuf *m, *dst, *head = NULL;
544 1.1 chopps register u_short len1;
545 1.1 chopps u_short amount;
546 1.1 chopps
547 1.1 chopps /* Allocate header mbuf. */
548 1.1 chopps MGETHDR(m, M_DONTWAIT, MT_DATA);
549 1.2 jtc if (m == NULL)
550 1.1 chopps goto bad;
551 1.1 chopps
552 1.2 jtc /*
553 1.2 jtc * Round len to even value.
554 1.2 jtc */
555 1.2 jtc if (len & 1)
556 1.2 jtc len++;
557 1.2 jtc
558 1.11 is m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
559 1.1 chopps m->m_pkthdr.len = len;
560 1.1 chopps m->m_len = 0;
561 1.1 chopps head = m;
562 1.1 chopps
563 1.1 chopps word_copy_from_card(nic_fifo_ptr,
564 1.1 chopps mtod(head, u_short *),
565 1.1 chopps sizeof(struct ether_header));
566 1.1 chopps
567 1.1 chopps head->m_len += sizeof(struct ether_header);
568 1.1 chopps len -= sizeof(struct ether_header);
569 1.1 chopps
570 1.1 chopps while (len > 0) {
571 1.1 chopps len1 = len;
572 1.1 chopps
573 1.1 chopps amount = M_TRAILINGSPACE(m);
574 1.1 chopps if (amount == 0) {
575 1.2 jtc /* Allocate another mbuf. */
576 1.1 chopps dst = m;
577 1.1 chopps MGET(m, M_DONTWAIT, MT_DATA);
578 1.2 jtc if (m == NULL)
579 1.1 chopps goto bad;
580 1.1 chopps
581 1.1 chopps if (len1 >= MINCLSIZE)
582 1.1 chopps MCLGET(m, M_DONTWAIT);
583 1.1 chopps
584 1.1 chopps m->m_len = 0;
585 1.1 chopps dst->m_next = m;
586 1.1 chopps
587 1.1 chopps amount = M_TRAILINGSPACE(m);
588 1.1 chopps }
589 1.1 chopps
590 1.2 jtc if (amount < len1)
591 1.1 chopps len1 = amount;
592 1.2 jtc
593 1.1 chopps word_copy_from_card(nic_fifo_ptr,
594 1.27 he (u_short *)(mtod(m, char *) + m->m_len),
595 1.2 jtc len1);
596 1.1 chopps m->m_len += len1;
597 1.1 chopps len -= len1;
598 1.1 chopps }
599 1.1 chopps
600 1.1 chopps #if NBPFILTER > 0
601 1.19 thorpej if (sc->sc_bpf)
602 1.1 chopps bpf_mtap(sc->sc_bpf, head);
603 1.1 chopps #endif
604 1.1 chopps
605 1.18 thorpej (*ifp->if_input)(ifp, head);
606 1.1 chopps return;
607 1.1 chopps
608 1.1 chopps bad:
609 1.2 jtc if (head) {
610 1.1 chopps m_freem(head);
611 1.2 jtc log(LOG_INFO, "qn_get_packet: mbuf alloc failed\n");
612 1.2 jtc }
613 1.1 chopps }
614 1.1 chopps
615 1.1 chopps /*
616 1.1 chopps * Ethernet interface receiver interrupt.
617 1.1 chopps */
618 1.1 chopps static void
619 1.21 aymeric qn_rint(struct qn_softc *sc, u_short rstat)
620 1.1 chopps {
621 1.1 chopps int i;
622 1.1 chopps u_short len, status;
623 1.1 chopps
624 1.2 jtc /* Clear the status register. */
625 1.2 jtc *sc->nic_r_status = CLEAR_R_ERR;
626 1.2 jtc
627 1.1 chopps /*
628 1.2 jtc * Was there some error?
629 1.2 jtc * Some of them are senseless because they are masked off.
630 1.2 jtc * XXX
631 1.1 chopps */
632 1.7 is if (rstat & R_INT_OVR_FLO) {
633 1.1 chopps #ifdef QN_DEBUG
634 1.2 jtc log(LOG_INFO, "Overflow\n");
635 1.1 chopps #endif
636 1.11 is ++sc->sc_ethercom.ec_if.if_ierrors;
637 1.2 jtc }
638 1.7 is if (rstat & R_INT_CRC_ERR) {
639 1.1 chopps #ifdef QN_DEBUG
640 1.2 jtc log(LOG_INFO, "CRC Error\n");
641 1.1 chopps #endif
642 1.11 is ++sc->sc_ethercom.ec_if.if_ierrors;
643 1.2 jtc }
644 1.7 is if (rstat & R_INT_ALG_ERR) {
645 1.1 chopps #ifdef QN_DEBUG
646 1.2 jtc log(LOG_INFO, "Alignment error\n");
647 1.1 chopps #endif
648 1.11 is ++sc->sc_ethercom.ec_if.if_ierrors;
649 1.2 jtc }
650 1.7 is if (rstat & R_INT_SRT_PKT) {
651 1.2 jtc /* Short packet (these may occur and are
652 1.2 jtc * no reason to worry about - or maybe
653 1.2 jtc * they are?).
654 1.2 jtc */
655 1.1 chopps #ifdef QN_DEBUG
656 1.2 jtc log(LOG_INFO, "Short packet\n");
657 1.1 chopps #endif
658 1.11 is ++sc->sc_ethercom.ec_if.if_ierrors;
659 1.2 jtc }
660 1.2 jtc if (rstat & 0x4040) {
661 1.1 chopps #ifdef QN_DEBUG
662 1.2 jtc log(LOG_INFO, "Bus read error\n");
663 1.1 chopps #endif
664 1.11 is ++sc->sc_ethercom.ec_if.if_ierrors;
665 1.2 jtc qnreset(sc);
666 1.2 jtc }
667 1.1 chopps
668 1.2 jtc /*
669 1.2 jtc * Read at most MAX_PACKETS packets per interrupt
670 1.2 jtc */
671 1.2 jtc for (i = 0; i < MAX_PACKETS; i++) {
672 1.2 jtc if (*sc->nic_r_mode & RM_BUF_EMP)
673 1.2 jtc /* Buffer empty. */
674 1.2 jtc break;
675 1.1 chopps
676 1.2 jtc /*
677 1.2 jtc * Read the first word: upper byte contains useful
678 1.2 jtc * information.
679 1.2 jtc */
680 1.2 jtc status = *sc->nic_fifo;
681 1.2 jtc if ((status & 0x7000) != 0x2000) {
682 1.2 jtc log(LOG_INFO, "qn: ERROR: status=%04x\n", status);
683 1.2 jtc continue;
684 1.2 jtc }
685 1.2 jtc
686 1.2 jtc /*
687 1.2 jtc * Read packet length (byte-swapped).
688 1.2 jtc * CRC is stripped off by the NIC.
689 1.2 jtc */
690 1.2 jtc len = *sc->nic_fifo;
691 1.2 jtc len = ((len << 8) & 0xff00) | ((len >> 8) & 0x00ff);
692 1.1 chopps
693 1.7 is #ifdef QN_CHECKS
694 1.17 thorpej if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN) ||
695 1.17 thorpej len < ETHER_HDR_LEN) {
696 1.2 jtc log(LOG_WARNING,
697 1.2 jtc "%s: received a %s packet? (%u bytes)\n",
698 1.2 jtc sc->sc_dev.dv_xname,
699 1.17 thorpej len < ETHER_HDR_LEN ? "partial" : "big", len);
700 1.11 is ++sc->sc_ethercom.ec_if.if_ierrors;
701 1.2 jtc continue;
702 1.1 chopps }
703 1.2 jtc #endif
704 1.7 is #ifdef QN_CHECKS
705 1.17 thorpej if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
706 1.2 jtc log(LOG_WARNING,
707 1.2 jtc "%s: received a short packet? (%u bytes)\n",
708 1.2 jtc sc->sc_dev.dv_xname, len);
709 1.21 aymeric #endif
710 1.2 jtc
711 1.2 jtc /* Read the packet. */
712 1.2 jtc qn_get_packet(sc, len);
713 1.2 jtc
714 1.11 is ++sc->sc_ethercom.ec_if.if_ipackets;
715 1.1 chopps }
716 1.1 chopps
717 1.2 jtc #ifdef QN_DEBUG
718 1.2 jtc /* This print just to see whether MAX_PACKETS is large enough. */
719 1.2 jtc if (i == MAX_PACKETS)
720 1.2 jtc log(LOG_INFO, "used all the %d loops\n", MAX_PACKETS);
721 1.2 jtc #endif
722 1.1 chopps }
723 1.1 chopps
724 1.1 chopps /*
725 1.1 chopps * Our interrupt routine
726 1.1 chopps */
727 1.1 chopps int
728 1.21 aymeric qnintr(void *arg)
729 1.1 chopps {
730 1.5 veego struct qn_softc *sc = arg;
731 1.2 jtc u_short tint, rint, tintmask;
732 1.2 jtc char return_tintmask = 0;
733 1.1 chopps
734 1.1 chopps /*
735 1.1 chopps * If the driver has not been initialized, just return immediately.
736 1.1 chopps * This also happens if there is no QuickNet board present.
737 1.1 chopps */
738 1.1 chopps if (sc->sc_base == NULL)
739 1.1 chopps return (0);
740 1.1 chopps
741 1.1 chopps /* Get interrupt statuses and masks. */
742 1.2 jtc rint = (*sc->nic_r_status) & NIC_R_MASK;
743 1.1 chopps tintmask = *sc->nic_t_mask;
744 1.2 jtc tint = (*sc->nic_t_status) & tintmask;
745 1.2 jtc if (tint == 0 && rint == 0)
746 1.1 chopps return (0);
747 1.1 chopps
748 1.2 jtc /* Disable interrupts so that we won't miss anything. */
749 1.1 chopps *sc->nic_r_mask = CLEAR_R_MASK;
750 1.2 jtc *sc->nic_t_mask = CLEAR_T_MASK;
751 1.1 chopps
752 1.1 chopps /*
753 1.1 chopps * Handle transmitter interrupts. Some of them are not asked for
754 1.1 chopps * but do happen, anyway.
755 1.1 chopps */
756 1.2 jtc
757 1.2 jtc if (tint != 0) {
758 1.2 jtc /* Clear transmit interrupt status. */
759 1.1 chopps *sc->nic_t_status = CLEAR_T_ERR;
760 1.2 jtc
761 1.1 chopps if (sc->transmit_pending && (tint & T_TMT_OK)) {
762 1.1 chopps sc->transmit_pending = 0;
763 1.1 chopps /*
764 1.1 chopps * Update total number of successfully
765 1.1 chopps * transmitted packets.
766 1.1 chopps */
767 1.11 is sc->sc_ethercom.ec_if.if_opackets++;
768 1.1 chopps }
769 1.1 chopps
770 1.1 chopps if (tint & T_SIXTEEN_COL) {
771 1.1 chopps /*
772 1.1 chopps * 16 collision (i.e., packet lost).
773 1.1 chopps */
774 1.1 chopps log(LOG_INFO, "qn: 16 collision - packet lost\n");
775 1.2 jtc #ifdef QN_DEBUG1
776 1.2 jtc qn_dump(sc);
777 1.2 jtc #endif
778 1.11 is sc->sc_ethercom.ec_if.if_oerrors++;
779 1.11 is sc->sc_ethercom.ec_if.if_collisions += 16;
780 1.1 chopps sc->transmit_pending = 0;
781 1.1 chopps }
782 1.1 chopps
783 1.1 chopps if (sc->transmit_pending) {
784 1.1 chopps log(LOG_INFO, "qn:still pending...\n");
785 1.2 jtc
786 1.2 jtc /* Must return transmission interrupt mask. */
787 1.2 jtc return_tintmask = 1;
788 1.1 chopps } else {
789 1.11 is sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
790 1.1 chopps
791 1.1 chopps /* Clear watchdog timer. */
792 1.11 is sc->sc_ethercom.ec_if.if_timer = 0;
793 1.1 chopps }
794 1.2 jtc } else
795 1.2 jtc return_tintmask = 1;
796 1.1 chopps
797 1.1 chopps /*
798 1.1 chopps * Handle receiver interrupts.
799 1.1 chopps */
800 1.2 jtc if (rint != 0)
801 1.2 jtc qn_rint(sc, rint);
802 1.1 chopps
803 1.11 is if ((sc->sc_ethercom.ec_if.if_flags & IFF_OACTIVE) == 0)
804 1.11 is qnstart(&sc->sc_ethercom.ec_if);
805 1.2 jtc else if (return_tintmask == 1)
806 1.2 jtc *sc->nic_t_mask = tintmask;
807 1.2 jtc
808 1.2 jtc /* Set receive interrupt mask back. */
809 1.2 jtc *sc->nic_r_mask = NIC_R_MASK;
810 1.1 chopps
811 1.1 chopps return (1);
812 1.1 chopps }
813 1.1 chopps
814 1.1 chopps /*
815 1.1 chopps * Process an ioctl request. This code needs some work - it looks pretty ugly.
816 1.1 chopps * I somehow think that this is quite a common excuse... ;-)
817 1.1 chopps */
818 1.1 chopps int
819 1.30 he qnioctl(register struct ifnet *ifp, u_long cmd, void *data)
820 1.1 chopps {
821 1.6 thorpej struct qn_softc *sc = ifp->if_softc;
822 1.1 chopps register struct ifaddr *ifa = (struct ifaddr *)data;
823 1.1 chopps #if 0
824 1.1 chopps struct ifreg *ifr = (struct ifreg *)data;
825 1.1 chopps #endif
826 1.1 chopps int s, error = 0;
827 1.1 chopps
828 1.3 mycroft s = splnet();
829 1.1 chopps
830 1.30 he switch (cmd) {
831 1.1 chopps
832 1.32 dyoung case SIOCINITIFADDR:
833 1.1 chopps ifp->if_flags |= IFF_UP;
834 1.1 chopps
835 1.1 chopps switch (ifa->ifa_addr->sa_family) {
836 1.1 chopps #ifdef INET
837 1.1 chopps case AF_INET:
838 1.2 jtc qnstop(sc);
839 1.1 chopps qninit(sc);
840 1.11 is arp_ifinit(ifp, ifa);
841 1.1 chopps break;
842 1.1 chopps #endif
843 1.1 chopps #ifdef NS
844 1.1 chopps case AF_NS:
845 1.1 chopps {
846 1.1 chopps register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
847 1.1 chopps
848 1.1 chopps if (ns_nullhost(*ina))
849 1.1 chopps ina->x_host =
850 1.11 is *(union ns_host *)LLADDR(ifp->if_sadl);
851 1.1 chopps else
852 1.1 chopps bcopy(ina->x_host.c_host,
853 1.11 is LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
854 1.2 jtc qnstop(sc);
855 1.1 chopps qninit(sc);
856 1.1 chopps break;
857 1.1 chopps }
858 1.1 chopps #endif
859 1.1 chopps default:
860 1.1 chopps log(LOG_INFO, "qn:sa_family:default (not tested)\n");
861 1.2 jtc qnstop(sc);
862 1.1 chopps qninit(sc);
863 1.1 chopps break;
864 1.1 chopps }
865 1.1 chopps break;
866 1.1 chopps
867 1.1 chopps case SIOCSIFFLAGS:
868 1.32 dyoung if ((error = ifioctl_common(ifp, cmd, data)) != 0)
869 1.32 dyoung break;
870 1.32 dyoung /* XXX see the comment in ed_ioctl() about code re-use */
871 1.1 chopps if ((ifp->if_flags & IFF_UP) == 0 &&
872 1.1 chopps (ifp->if_flags & IFF_RUNNING) != 0) {
873 1.1 chopps /*
874 1.1 chopps * If interface is marked down and it is running, then
875 1.1 chopps * stop it.
876 1.1 chopps */
877 1.2 jtc #ifdef QN_DEBUG1
878 1.2 jtc qn_dump(sc);
879 1.2 jtc #endif
880 1.1 chopps qnstop(sc);
881 1.1 chopps ifp->if_flags &= ~IFF_RUNNING;
882 1.1 chopps } else if ((ifp->if_flags & IFF_UP) != 0 &&
883 1.2 jtc (ifp->if_flags & IFF_RUNNING) == 0) {
884 1.1 chopps /*
885 1.1 chopps * If interface is marked up and it is stopped, then
886 1.1 chopps * start it.
887 1.1 chopps */
888 1.1 chopps qninit(sc);
889 1.2 jtc } else {
890 1.1 chopps /*
891 1.1 chopps * Something else... we won't do anything so we won't
892 1.1 chopps * break anything (hope so).
893 1.1 chopps */
894 1.2 jtc #ifdef QN_DEBUG1
895 1.2 jtc log(LOG_INFO, "Else branch...\n");
896 1.2 jtc #endif
897 1.1 chopps }
898 1.1 chopps break;
899 1.1 chopps
900 1.1 chopps case SIOCADDMULTI:
901 1.1 chopps case SIOCDELMULTI:
902 1.1 chopps log(LOG_INFO, "qnioctl: multicast not done yet\n");
903 1.1 chopps #if 0
904 1.29 dyoung if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
905 1.1 chopps /*
906 1.1 chopps * Multicast list has changed; set the hardware filter
907 1.1 chopps * accordingly.
908 1.1 chopps */
909 1.1 chopps log(LOG_INFO, "qnioctl: multicast not done yet\n");
910 1.1 chopps error = 0;
911 1.1 chopps }
912 1.1 chopps #else
913 1.1 chopps error = EINVAL;
914 1.1 chopps #endif
915 1.1 chopps break;
916 1.1 chopps
917 1.1 chopps default:
918 1.32 dyoung error = ether_ioctl(ifp, cmd, data);
919 1.1 chopps }
920 1.1 chopps
921 1.2 jtc splx(s);
922 1.1 chopps return (error);
923 1.1 chopps }
924 1.2 jtc
925 1.2 jtc /*
926 1.2 jtc * Dump some register information.
927 1.2 jtc */
928 1.2 jtc #ifdef QN_DEBUG1
929 1.2 jtc static void
930 1.21 aymeric qn_dump(struct qn_softc *sc)
931 1.2 jtc {
932 1.2 jtc
933 1.2 jtc log(LOG_INFO, "t_status : %04x\n", *sc->nic_t_status);
934 1.2 jtc log(LOG_INFO, "t_mask : %04x\n", *sc->nic_t_mask);
935 1.2 jtc log(LOG_INFO, "t_mode : %04x\n", *sc->nic_t_mode);
936 1.2 jtc log(LOG_INFO, "r_status : %04x\n", *sc->nic_r_status);
937 1.2 jtc log(LOG_INFO, "r_mask : %04x\n", *sc->nic_r_mask);
938 1.2 jtc log(LOG_INFO, "r_mode : %04x\n", *sc->nic_r_mode);
939 1.2 jtc log(LOG_INFO, "pending : %02x\n", sc->transmit_pending);
940 1.11 is log(LOG_INFO, "if_flags : %04x\n", sc->sc_ethercom.ec_if.if_flags);
941 1.2 jtc }
942 1.2 jtc #endif
943 1.1 chopps
944 1.1 chopps #endif /* NQN > 0 */
945