cs89x0.c revision 1.2.2.5 1 1.2.2.5 thorpej /* $NetBSD: cs89x0.c,v 1.2.2.5 2003/01/07 21:34:15 thorpej Exp $ */
2 1.2.2.2 nathanw
3 1.2.2.2 nathanw /*
4 1.2.2.2 nathanw * Copyright 1997
5 1.2.2.2 nathanw * Digital Equipment Corporation. All rights reserved.
6 1.2.2.2 nathanw *
7 1.2.2.2 nathanw * This software is furnished under license and may be used and
8 1.2.2.2 nathanw * copied only in accordance with the following terms and conditions.
9 1.2.2.2 nathanw * Subject to these conditions, you may download, copy, install,
10 1.2.2.2 nathanw * use, modify and distribute this software in source and/or binary
11 1.2.2.2 nathanw * form. No title or ownership is transferred hereby.
12 1.2.2.2 nathanw *
13 1.2.2.2 nathanw * 1) Any source code used, modified or distributed must reproduce
14 1.2.2.2 nathanw * and retain this copyright notice and list of conditions as
15 1.2.2.2 nathanw * they appear in the source file.
16 1.2.2.2 nathanw *
17 1.2.2.2 nathanw * 2) No right is granted to use any trade name, trademark, or logo of
18 1.2.2.2 nathanw * Digital Equipment Corporation. Neither the "Digital Equipment
19 1.2.2.2 nathanw * Corporation" name nor any trademark or logo of Digital Equipment
20 1.2.2.2 nathanw * Corporation may be used to endorse or promote products derived
21 1.2.2.2 nathanw * from this software without the prior written permission of
22 1.2.2.2 nathanw * Digital Equipment Corporation.
23 1.2.2.2 nathanw *
24 1.2.2.2 nathanw * 3) This software is provided "AS-IS" and any express or implied
25 1.2.2.2 nathanw * warranties, including but not limited to, any implied warranties
26 1.2.2.2 nathanw * of merchantability, fitness for a particular purpose, or
27 1.2.2.2 nathanw * non-infringement are disclaimed. In no event shall DIGITAL be
28 1.2.2.2 nathanw * liable for any damages whatsoever, and in particular, DIGITAL
29 1.2.2.2 nathanw * shall not be liable for special, indirect, consequential, or
30 1.2.2.2 nathanw * incidental damages or damages for lost profits, loss of
31 1.2.2.2 nathanw * revenue or loss of use, whether such damages arise in contract,
32 1.2.2.2 nathanw * negligence, tort, under statute, in equity, at law or otherwise,
33 1.2.2.2 nathanw * even if advised of the possibility of such damage.
34 1.2.2.2 nathanw */
35 1.2.2.2 nathanw
36 1.2.2.2 nathanw /*
37 1.2.2.2 nathanw **++
38 1.2.2.2 nathanw ** FACILITY
39 1.2.2.2 nathanw **
40 1.2.2.2 nathanw ** Device Driver for the Crystal CS8900 ISA Ethernet Controller.
41 1.2.2.2 nathanw **
42 1.2.2.2 nathanw ** ABSTRACT
43 1.2.2.2 nathanw **
44 1.2.2.2 nathanw ** This module provides standard ethernet access for INET protocols
45 1.2.2.2 nathanw ** only.
46 1.2.2.2 nathanw **
47 1.2.2.2 nathanw ** AUTHORS
48 1.2.2.2 nathanw **
49 1.2.2.2 nathanw ** Peter Dettori SEA - Software Engineering.
50 1.2.2.2 nathanw **
51 1.2.2.2 nathanw ** CREATION DATE:
52 1.2.2.2 nathanw **
53 1.2.2.2 nathanw ** 13-Feb-1997.
54 1.2.2.2 nathanw **
55 1.2.2.2 nathanw ** MODIFICATION HISTORY (Digital):
56 1.2.2.2 nathanw **
57 1.2.2.2 nathanw ** Revision 1.27 1998/01/20 17:59:40 cgd
58 1.2.2.2 nathanw ** update for moved headers
59 1.2.2.2 nathanw **
60 1.2.2.2 nathanw ** Revision 1.26 1998/01/12 19:29:36 cgd
61 1.2.2.2 nathanw ** use arm32/isa versions of isadma code.
62 1.2.2.2 nathanw **
63 1.2.2.2 nathanw ** Revision 1.25 1997/12/12 01:35:27 cgd
64 1.2.2.2 nathanw ** convert to use new arp code (from Brini)
65 1.2.2.2 nathanw **
66 1.2.2.2 nathanw ** Revision 1.24 1997/12/10 22:31:56 cgd
67 1.2.2.2 nathanw ** trim some fat (get rid of ability to explicitly supply enet addr, since
68 1.2.2.2 nathanw ** it was never used and added a bunch of code which really doesn't belong in
69 1.2.2.2 nathanw ** an enet driver), and clean up slightly.
70 1.2.2.2 nathanw **
71 1.2.2.2 nathanw ** Revision 1.23 1997/10/06 16:42:12 cgd
72 1.2.2.2 nathanw ** copyright notices
73 1.2.2.2 nathanw **
74 1.2.2.2 nathanw ** Revision 1.22 1997/06/20 19:38:01 chaiken
75 1.2.2.2 nathanw ** fixes some smartcard problems
76 1.2.2.2 nathanw **
77 1.2.2.2 nathanw ** Revision 1.21 1997/06/10 02:56:20 grohn
78 1.2.2.2 nathanw ** Added call to ledNetActive
79 1.2.2.2 nathanw **
80 1.2.2.2 nathanw ** Revision 1.20 1997/06/05 00:47:06 dettori
81 1.2.2.2 nathanw ** Changed cs_process_rx_dma to reset and re-initialise the
82 1.2.2.2 nathanw ** ethernet chip when DMA gets out of sync, or mbufs
83 1.2.2.2 nathanw ** can't be allocated.
84 1.2.2.2 nathanw **
85 1.2.2.2 nathanw ** Revision 1.19 1997/06/03 03:09:58 dettori
86 1.2.2.2 nathanw ** Turn off sc_txbusy flag when a transmit underrun
87 1.2.2.2 nathanw ** occurs.
88 1.2.2.2 nathanw **
89 1.2.2.2 nathanw ** Revision 1.18 1997/06/02 00:04:35 dettori
90 1.2.2.2 nathanw ** redefined the transmit table to get around the nfs_timer bug while we are
91 1.2.2.2 nathanw ** looking into it further.
92 1.2.2.2 nathanw **
93 1.2.2.2 nathanw ** Also changed interrupts from EDGE to LEVEL.
94 1.2.2.2 nathanw **
95 1.2.2.2 nathanw ** Revision 1.17 1997/05/27 23:31:01 dettori
96 1.2.2.2 nathanw ** Pulled out changes to DMAMODE defines.
97 1.2.2.2 nathanw **
98 1.2.2.2 nathanw ** Revision 1.16 1997/05/23 04:25:16 cgd
99 1.2.2.2 nathanw ** reformat log so it fits in 80cols
100 1.2.2.2 nathanw **
101 1.2.2.2 nathanw ** Revision 1.15 1997/05/23 04:22:18 cgd
102 1.2.2.2 nathanw ** remove the existing copyright notice (which Peter Dettori indicated
103 1.2.2.2 nathanw ** was incorrect, copied from an existing NetBSD file only so that the
104 1.2.2.2 nathanw ** file would have a copyright notice on it, and which he'd intended to
105 1.2.2.2 nathanw ** replace). Replace it with a Digital copyright notice, cloned from
106 1.2.2.2 nathanw ** ess.c. It's not really correct either (it indicates that the source
107 1.2.2.2 nathanw ** is Digital confidential!), but is better than nothing and more
108 1.2.2.2 nathanw ** correct than what was there before.
109 1.2.2.2 nathanw **
110 1.2.2.2 nathanw ** Revision 1.14 1997/05/23 04:12:50 cgd
111 1.2.2.2 nathanw ** use an adaptive transmit start algorithm: start by telling the chip
112 1.2.2.2 nathanw ** to start transmitting after 381 bytes have been fed to it. if that
113 1.2.2.2 nathanw ** gets transmit underruns, ramp down to 1021 bytes then "whole
114 1.2.2.2 nathanw ** packet." If successful at a given level for a while, try the next
115 1.2.2.2 nathanw ** more agressive level. This code doesn't ever try to start
116 1.2.2.2 nathanw ** transmitting after 5 bytes have been sent to the NIC, because
117 1.2.2.2 nathanw ** that underruns rather regularly. The back-off and ramp-up mechanism
118 1.2.2.2 nathanw ** could probably be tuned a little bit, but this works well enough to
119 1.2.2.2 nathanw ** support > 1MB/s transmit rates on a clear ethernet (which is about
120 1.2.2.2 nathanw ** 20-25% better than the driver had previously been getting).
121 1.2.2.2 nathanw **
122 1.2.2.2 nathanw ** Revision 1.13 1997/05/22 21:06:54 cgd
123 1.2.2.2 nathanw ** redo cs_copy_tx_frame() from scratch. It had a fatal flaw: it was blindly
124 1.2.2.2 nathanw ** casting from u_int8_t * to u_int16_t * without worrying about alignment
125 1.2.2.2 nathanw ** issues. This would cause bogus data to be spit out for mbufs with
126 1.2.2.2 nathanw ** misaligned data. For instance, it caused the following bits to appear
127 1.2.2.2 nathanw ** on the wire:
128 1.2.2.2 nathanw ** ... etBND 1S2C .SHA(K) R ...
129 1.2.2.2 nathanw ** 11112222333344445555
130 1.2.2.2 nathanw ** which should have appeared as:
131 1.2.2.2 nathanw ** ... NetBSD 1.2C (SHARK) ...
132 1.2.2.2 nathanw ** 11112222333344445555
133 1.2.2.2 nathanw ** Note the apparent 'rotate' of the bytes in the word, which was due to
134 1.2.2.2 nathanw ** incorrect unaligned accesses. This data corruption was the cause of
135 1.2.2.2 nathanw ** incoming telnet/rlogin hangs.
136 1.2.2.2 nathanw **
137 1.2.2.2 nathanw ** Revision 1.12 1997/05/22 01:55:32 cgd
138 1.2.2.2 nathanw ** reformat log so it fits in 80cols
139 1.2.2.2 nathanw **
140 1.2.2.2 nathanw ** Revision 1.11 1997/05/22 01:50:27 cgd
141 1.2.2.2 nathanw ** * enable input packet address checking in the BPF+IFF_PROMISCUOUS case,
142 1.2.2.2 nathanw ** so packets aimed at other hosts don't get sent to ether_input().
143 1.2.2.2 nathanw ** * Add a static const char *rcsid initialized with an RCS Id tag, so that
144 1.2.2.2 nathanw ** you can easily tell (`strings`) what version of the driver is in your
145 1.2.2.2 nathanw ** kernel binary.
146 1.2.2.2 nathanw ** * get rid of ether_cmp(). It was inconsistently used, not necessarily
147 1.2.2.2 nathanw ** safe, and not really a performance win anyway. (It was only used when
148 1.2.2.2 nathanw ** setting up the multicast logical address filter, which is an
149 1.2.2.2 nathanw ** infrequent event. It could have been used in the IFF_PROMISCUOUS
150 1.2.2.2 nathanw ** address check above, but the benefit of it vs. memcmp would be
151 1.2.2.2 nathanw ** inconsequential, there.) Use memcmp() instead.
152 1.2.2.2 nathanw ** * restructure csStartOuput to avoid the following bugs in the case where
153 1.2.2.2 nathanw ** txWait was being set:
154 1.2.2.2 nathanw ** * it would accidentally drop the outgoing packet if told to wait
155 1.2.2.2 nathanw ** but the outgoing packet queue was empty.
156 1.2.2.2 nathanw ** * it would bpf_mtap() the outgoing packet multiple times (once for
157 1.2.2.2 nathanw ** each time it was told to wait), and would also recalculate
158 1.2.2.2 nathanw ** the length of the outgoing packet each time it was told to
159 1.2.2.2 nathanw ** wait.
160 1.2.2.2 nathanw ** While there, rename txWait to txLoop, since with the new structure of
161 1.2.2.2 nathanw ** the code, the latter name makes more sense.
162 1.2.2.2 nathanw **
163 1.2.2.2 nathanw ** Revision 1.10 1997/05/19 02:03:20 cgd
164 1.2.2.2 nathanw ** Set RX_CTL in cs_set_ladr_filt(), rather than cs_initChip(). cs_initChip()
165 1.2.2.2 nathanw ** is the only caller of cs_set_ladr_filt(), and always calls it, so this
166 1.2.2.2 nathanw ** ends up being logically the same. In cs_set_ladr_filt(), if IFF_PROMISC
167 1.2.2.2 nathanw ** is set, enable promiscuous mode (and set IFF_ALLMULTI), otherwise behave
168 1.2.2.2 nathanw ** as before.
169 1.2.2.2 nathanw **
170 1.2.2.2 nathanw ** Revision 1.9 1997/05/19 01:45:37 cgd
171 1.2.2.2 nathanw ** create a new function, cs_ether_input(), which does received-packet
172 1.2.2.2 nathanw ** BPF and ether_input processing. This code used to be in three places,
173 1.2.2.2 nathanw ** and centralizing it will make adding IFF_PROMISC support much easier.
174 1.2.2.2 nathanw ** Also, in cs_copy_tx_frame(), put it some (currently disabled) code to
175 1.2.2.2 nathanw ** do copies with bus_space_write_region_2(). It's more correct, and
176 1.2.2.2 nathanw ** potentially more efficient. That function needs to be gutted (to
177 1.2.2.2 nathanw ** deal properly with alignment issues, which it currently does wrong),
178 1.2.2.2 nathanw ** however, and the change doesn't gain much, so there's no point in
179 1.2.2.2 nathanw ** enabling it now.
180 1.2.2.2 nathanw **
181 1.2.2.2 nathanw ** Revision 1.8 1997/05/19 01:17:10 cgd
182 1.2.2.2 nathanw ** fix a comment re: the setting of the TxConfig register. Clean up
183 1.2.2.2 nathanw ** interface counter maintenance (make it use standard idiom).
184 1.2.2.2 nathanw **
185 1.2.2.2 nathanw **--
186 1.2.2.2 nathanw */
187 1.2.2.2 nathanw
188 1.2.2.2 nathanw #include <sys/cdefs.h>
189 1.2.2.5 thorpej __KERNEL_RCSID(0, "$NetBSD: cs89x0.c,v 1.2.2.5 2003/01/07 21:34:15 thorpej Exp $");
190 1.2.2.2 nathanw
191 1.2.2.2 nathanw #include "opt_inet.h"
192 1.2.2.2 nathanw
193 1.2.2.2 nathanw #include <sys/param.h>
194 1.2.2.2 nathanw #include <sys/systm.h>
195 1.2.2.2 nathanw #include <sys/mbuf.h>
196 1.2.2.2 nathanw #include <sys/syslog.h>
197 1.2.2.2 nathanw #include <sys/socket.h>
198 1.2.2.2 nathanw #include <sys/device.h>
199 1.2.2.2 nathanw #include <sys/malloc.h>
200 1.2.2.2 nathanw #include <sys/ioctl.h>
201 1.2.2.2 nathanw #include <sys/errno.h>
202 1.2.2.2 nathanw
203 1.2.2.2 nathanw #include "rnd.h"
204 1.2.2.2 nathanw #if NRND > 0
205 1.2.2.2 nathanw #include <sys/rnd.h>
206 1.2.2.2 nathanw #endif
207 1.2.2.2 nathanw
208 1.2.2.2 nathanw #include <net/if.h>
209 1.2.2.2 nathanw #include <net/if_ether.h>
210 1.2.2.2 nathanw #include <net/if_media.h>
211 1.2.2.2 nathanw #ifdef INET
212 1.2.2.2 nathanw #include <netinet/in.h>
213 1.2.2.2 nathanw #include <netinet/if_inarp.h>
214 1.2.2.2 nathanw #endif
215 1.2.2.2 nathanw
216 1.2.2.2 nathanw #include "bpfilter.h"
217 1.2.2.2 nathanw #if NBPFILTER > 0
218 1.2.2.2 nathanw #include <net/bpf.h>
219 1.2.2.2 nathanw #include <net/bpfdesc.h>
220 1.2.2.2 nathanw #endif
221 1.2.2.2 nathanw
222 1.2.2.2 nathanw #include <uvm/uvm_extern.h>
223 1.2.2.2 nathanw
224 1.2.2.2 nathanw #include <machine/bus.h>
225 1.2.2.2 nathanw #include <machine/intr.h>
226 1.2.2.2 nathanw
227 1.2.2.2 nathanw #include <dev/ic/cs89x0reg.h>
228 1.2.2.2 nathanw #include <dev/ic/cs89x0var.h>
229 1.2.2.2 nathanw
230 1.2.2.2 nathanw #ifdef SHARK
231 1.2.2.3 nathanw #include <shark/shark/sequoia.h>
232 1.2.2.2 nathanw #endif
233 1.2.2.2 nathanw
234 1.2.2.2 nathanw /*
235 1.2.2.2 nathanw * MACRO DEFINITIONS
236 1.2.2.2 nathanw */
237 1.2.2.2 nathanw #define CS_OUTPUT_LOOP_MAX 100 /* max times round notorious tx loop */
238 1.2.2.2 nathanw
239 1.2.2.2 nathanw /*
240 1.2.2.2 nathanw * FUNCTION PROTOTYPES
241 1.2.2.2 nathanw */
242 1.2.2.4 nathanw void cs_get_default_media(struct cs_softc *);
243 1.2.2.4 nathanw int cs_get_params(struct cs_softc *);
244 1.2.2.4 nathanw int cs_get_enaddr(struct cs_softc *);
245 1.2.2.4 nathanw int cs_reset_chip(struct cs_softc *);
246 1.2.2.4 nathanw void cs_reset(void *);
247 1.2.2.4 nathanw int cs_ioctl(struct ifnet *, u_long, caddr_t);
248 1.2.2.4 nathanw void cs_initChip(struct cs_softc *);
249 1.2.2.4 nathanw void cs_buffer_event(struct cs_softc *, u_int16_t);
250 1.2.2.4 nathanw void cs_transmit_event(struct cs_softc *, u_int16_t);
251 1.2.2.4 nathanw void cs_receive_event(struct cs_softc *, u_int16_t);
252 1.2.2.4 nathanw void cs_process_receive(struct cs_softc *);
253 1.2.2.4 nathanw void cs_process_rx_early(struct cs_softc *);
254 1.2.2.4 nathanw void cs_start_output(struct ifnet *);
255 1.2.2.4 nathanw void cs_copy_tx_frame(struct cs_softc *, struct mbuf *);
256 1.2.2.4 nathanw void cs_set_ladr_filt(struct cs_softc *, struct ethercom *);
257 1.2.2.4 nathanw u_int16_t cs_hash_index(char *);
258 1.2.2.4 nathanw void cs_counter_event(struct cs_softc *, u_int16_t);
259 1.2.2.4 nathanw
260 1.2.2.4 nathanw int cs_mediachange(struct ifnet *);
261 1.2.2.4 nathanw void cs_mediastatus(struct ifnet *, struct ifmediareq *);
262 1.2.2.4 nathanw
263 1.2.2.4 nathanw static int cs_enable(struct cs_softc *);
264 1.2.2.4 nathanw static void cs_disable(struct cs_softc *);
265 1.2.2.4 nathanw static void cs_stop(struct ifnet *, int);
266 1.2.2.4 nathanw static void cs_power(int, void *);
267 1.2.2.2 nathanw
268 1.2.2.2 nathanw /*
269 1.2.2.2 nathanw * GLOBAL DECLARATIONS
270 1.2.2.2 nathanw */
271 1.2.2.2 nathanw
272 1.2.2.2 nathanw /*
273 1.2.2.2 nathanw * Xmit-early table.
274 1.2.2.2 nathanw *
275 1.2.2.2 nathanw * To get better performance, we tell the chip to start packet
276 1.2.2.2 nathanw * transmission before the whole packet is copied to the chip.
277 1.2.2.2 nathanw * However, this can fail under load. When it fails, we back off
278 1.2.2.2 nathanw * to a safer setting for a little while.
279 1.2.2.2 nathanw *
280 1.2.2.2 nathanw * txcmd is the value of txcmd used to indicate when to start transmission.
281 1.2.2.2 nathanw * better is the next 'better' state in the table.
282 1.2.2.2 nathanw * better_count is the number of output packets before transition to the
283 1.2.2.2 nathanw * better state.
284 1.2.2.2 nathanw * worse is the next 'worse' state in the table.
285 1.2.2.2 nathanw *
286 1.2.2.2 nathanw * Transition to the next worse state happens automatically when a
287 1.2.2.2 nathanw * transmittion underrun occurs.
288 1.2.2.2 nathanw */
289 1.2.2.2 nathanw struct cs_xmit_early {
290 1.2.2.2 nathanw u_int16_t txcmd;
291 1.2.2.2 nathanw int better;
292 1.2.2.2 nathanw int better_count;
293 1.2.2.2 nathanw int worse;
294 1.2.2.2 nathanw } cs_xmit_early_table[3] = {
295 1.2.2.2 nathanw { TX_CMD_START_381, 0, INT_MAX, 1, },
296 1.2.2.2 nathanw { TX_CMD_START_1021, 0, 50000, 2, },
297 1.2.2.2 nathanw { TX_CMD_START_ALL, 1, 5000, 2, },
298 1.2.2.2 nathanw };
299 1.2.2.2 nathanw
300 1.2.2.2 nathanw int cs_default_media[] = {
301 1.2.2.2 nathanw IFM_ETHER|IFM_10_2,
302 1.2.2.2 nathanw IFM_ETHER|IFM_10_5,
303 1.2.2.2 nathanw IFM_ETHER|IFM_10_T,
304 1.2.2.2 nathanw IFM_ETHER|IFM_10_T|IFM_FDX,
305 1.2.2.2 nathanw };
306 1.2.2.2 nathanw int cs_default_nmedia = sizeof(cs_default_media) / sizeof(cs_default_media[0]);
307 1.2.2.2 nathanw
308 1.2.2.2 nathanw int
309 1.2.2.4 nathanw cs_attach(struct cs_softc *sc, u_int8_t *enaddr, int *media,
310 1.2.2.4 nathanw int nmedia, int defmedia)
311 1.2.2.2 nathanw {
312 1.2.2.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
313 1.2.2.2 nathanw const char *chipname, *medname;
314 1.2.2.2 nathanw u_int16_t reg;
315 1.2.2.2 nathanw int i;
316 1.2.2.2 nathanw
317 1.2.2.2 nathanw /* Start out in IO mode */
318 1.2.2.2 nathanw sc->sc_memorymode = FALSE;
319 1.2.2.2 nathanw
320 1.2.2.2 nathanw /* make sure we're right */
321 1.2.2.2 nathanw for (i = 0; i < 10000; i++) {
322 1.2.2.2 nathanw reg = CS_READ_PACKET_PAGE(sc, PKTPG_EISA_NUM);
323 1.2.2.2 nathanw if (reg == EISA_NUM_CRYSTAL) {
324 1.2.2.2 nathanw break;
325 1.2.2.2 nathanw }
326 1.2.2.2 nathanw }
327 1.2.2.2 nathanw if (i == 10000) {
328 1.2.2.2 nathanw printf("%s: wrong id(0x%x)\n", sc->sc_dev.dv_xname, reg);
329 1.2.2.2 nathanw return 1; /* XXX should panic? */
330 1.2.2.2 nathanw }
331 1.2.2.2 nathanw
332 1.2.2.2 nathanw reg = CS_READ_PACKET_PAGE(sc, PKTPG_PRODUCT_ID);
333 1.2.2.2 nathanw sc->sc_prodid = reg & PROD_ID_MASK;
334 1.2.2.2 nathanw sc->sc_prodrev = (reg & PROD_REV_MASK) >> 8;
335 1.2.2.2 nathanw
336 1.2.2.2 nathanw switch (sc->sc_prodid) {
337 1.2.2.2 nathanw case PROD_ID_CS8900:
338 1.2.2.2 nathanw chipname = "CS8900";
339 1.2.2.2 nathanw break;
340 1.2.2.2 nathanw case PROD_ID_CS8920:
341 1.2.2.2 nathanw chipname = "CS8920";
342 1.2.2.2 nathanw break;
343 1.2.2.2 nathanw case PROD_ID_CS8920M:
344 1.2.2.2 nathanw chipname = "CS8920M";
345 1.2.2.2 nathanw break;
346 1.2.2.2 nathanw default:
347 1.2.2.2 nathanw panic("cs_attach: impossible");
348 1.2.2.2 nathanw }
349 1.2.2.2 nathanw
350 1.2.2.2 nathanw /*
351 1.2.2.2 nathanw * the first thing to do is check that the mbuf cluster size is
352 1.2.2.2 nathanw * greater than the MTU for an ethernet frame. The code depends on
353 1.2.2.2 nathanw * this and to port this to a OS where this was not the case would
354 1.2.2.2 nathanw * not be straightforward.
355 1.2.2.2 nathanw *
356 1.2.2.2 nathanw * we need 1 byte spare because our
357 1.2.2.2 nathanw * packet read loop can overrun.
358 1.2.2.2 nathanw * and we may need pad bytes to align ip header.
359 1.2.2.2 nathanw */
360 1.2.2.2 nathanw if (MCLBYTES < ETHER_MAX_LEN + 1 +
361 1.2.2.2 nathanw ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header)) {
362 1.2.2.2 nathanw printf("%s: MCLBYTES too small for Ethernet frame\n",
363 1.2.2.2 nathanw sc->sc_dev.dv_xname);
364 1.2.2.2 nathanw return 1;
365 1.2.2.2 nathanw }
366 1.2.2.2 nathanw
367 1.2.2.2 nathanw /* Start out not transmitting */
368 1.2.2.2 nathanw sc->sc_txbusy = FALSE;
369 1.2.2.2 nathanw
370 1.2.2.2 nathanw /* Set up early transmit threshhold */
371 1.2.2.2 nathanw sc->sc_xe_ent = 0;
372 1.2.2.2 nathanw sc->sc_xe_togo = cs_xmit_early_table[sc->sc_xe_ent].better_count;
373 1.2.2.2 nathanw
374 1.2.2.2 nathanw /* Initialize ifnet structure. */
375 1.2.2.2 nathanw strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
376 1.2.2.2 nathanw ifp->if_softc = sc;
377 1.2.2.2 nathanw ifp->if_start = cs_start_output;
378 1.2.2.2 nathanw ifp->if_init = cs_init;
379 1.2.2.2 nathanw ifp->if_ioctl = cs_ioctl;
380 1.2.2.2 nathanw ifp->if_stop = cs_stop;
381 1.2.2.2 nathanw ifp->if_watchdog = NULL; /* no watchdog at this stage */
382 1.2.2.2 nathanw ifp->if_flags = IFF_SIMPLEX | IFF_NOTRAILERS |
383 1.2.2.2 nathanw IFF_BROADCAST | IFF_MULTICAST;
384 1.2.2.2 nathanw IFQ_SET_READY(&ifp->if_snd);
385 1.2.2.2 nathanw
386 1.2.2.2 nathanw /* Initialize ifmedia structures. */
387 1.2.2.2 nathanw ifmedia_init(&sc->sc_media, 0, cs_mediachange, cs_mediastatus);
388 1.2.2.2 nathanw
389 1.2.2.2 nathanw if (media != NULL) {
390 1.2.2.2 nathanw for (i = 0; i < nmedia; i++)
391 1.2.2.2 nathanw ifmedia_add(&sc->sc_media, media[i], 0, NULL);
392 1.2.2.2 nathanw ifmedia_set(&sc->sc_media, defmedia);
393 1.2.2.2 nathanw } else {
394 1.2.2.2 nathanw for (i = 0; i < cs_default_nmedia; i++)
395 1.2.2.2 nathanw ifmedia_add(&sc->sc_media, cs_default_media[i],
396 1.2.2.2 nathanw 0, NULL);
397 1.2.2.2 nathanw cs_get_default_media(sc);
398 1.2.2.2 nathanw }
399 1.2.2.2 nathanw
400 1.2.2.2 nathanw if ((sc->sc_cfgflags & CFGFLG_NOT_EEPROM) == 0) {
401 1.2.2.2 nathanw /* Get parameters from the EEPROM */
402 1.2.2.2 nathanw if (cs_get_params(sc) == CS_ERROR) {
403 1.2.2.2 nathanw printf("%s: unable to get settings from EEPROM\n",
404 1.2.2.2 nathanw sc->sc_dev.dv_xname);
405 1.2.2.2 nathanw return 1;
406 1.2.2.2 nathanw }
407 1.2.2.2 nathanw }
408 1.2.2.2 nathanw
409 1.2.2.2 nathanw if (enaddr != NULL)
410 1.2.2.2 nathanw memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr));
411 1.2.2.2 nathanw else if ((sc->sc_cfgflags & CFGFLG_NOT_EEPROM) == 0) {
412 1.2.2.2 nathanw /* Get and store the Ethernet address */
413 1.2.2.2 nathanw if (cs_get_enaddr(sc) == CS_ERROR) {
414 1.2.2.2 nathanw printf("%s: unable to read Ethernet address\n",
415 1.2.2.2 nathanw sc->sc_dev.dv_xname);
416 1.2.2.2 nathanw return 1;
417 1.2.2.2 nathanw }
418 1.2.2.2 nathanw } else {
419 1.2.2.4 nathanw #if 1
420 1.2.2.4 nathanw int i;
421 1.2.2.4 nathanw uint v;
422 1.2.2.4 nathanw
423 1.2.2.4 nathanw for (i = 0; i < 6; i += 2) {
424 1.2.2.4 nathanw v = CS_READ_PACKET_PAGE(sc, PKTPG_IND_ADDR + i);
425 1.2.2.4 nathanw sc->sc_enaddr[i + 0] = v;
426 1.2.2.4 nathanw sc->sc_enaddr[i + 1] = v >> 8;
427 1.2.2.4 nathanw }
428 1.2.2.4 nathanw #else
429 1.2.2.2 nathanw printf("%s: no Ethernet address!\n", sc->sc_dev.dv_xname);
430 1.2.2.2 nathanw return 1;
431 1.2.2.4 nathanw #endif
432 1.2.2.2 nathanw }
433 1.2.2.2 nathanw
434 1.2.2.2 nathanw switch (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media)) {
435 1.2.2.2 nathanw case IFM_10_2:
436 1.2.2.2 nathanw medname = "BNC";
437 1.2.2.2 nathanw break;
438 1.2.2.2 nathanw case IFM_10_5:
439 1.2.2.2 nathanw medname = "AUI";
440 1.2.2.2 nathanw break;
441 1.2.2.2 nathanw case IFM_10_T:
442 1.2.2.2 nathanw if (sc->sc_media.ifm_cur->ifm_media & IFM_FDX)
443 1.2.2.2 nathanw medname = "UTP <full-duplex>";
444 1.2.2.2 nathanw else
445 1.2.2.2 nathanw medname = "UTP";
446 1.2.2.2 nathanw break;
447 1.2.2.2 nathanw default:
448 1.2.2.2 nathanw panic("cs_attach: impossible");
449 1.2.2.2 nathanw }
450 1.2.2.2 nathanw printf("%s: %s rev. %c, address %s, media %s\n", sc->sc_dev.dv_xname,
451 1.2.2.2 nathanw chipname, sc->sc_prodrev + 'A', ether_sprintf(sc->sc_enaddr),
452 1.2.2.2 nathanw medname);
453 1.2.2.2 nathanw
454 1.2.2.2 nathanw if (sc->sc_dma_attach)
455 1.2.2.2 nathanw (*sc->sc_dma_attach)(sc);
456 1.2.2.2 nathanw
457 1.2.2.2 nathanw sc->sc_sh = shutdownhook_establish(cs_reset, sc);
458 1.2.2.2 nathanw if (sc->sc_sh == NULL) {
459 1.2.2.2 nathanw printf("%s: unable to establish shutdownhook\n",
460 1.2.2.2 nathanw sc->sc_dev.dv_xname);
461 1.2.2.2 nathanw cs_detach(sc);
462 1.2.2.2 nathanw return 1;
463 1.2.2.2 nathanw }
464 1.2.2.2 nathanw
465 1.2.2.2 nathanw /* Attach the interface. */
466 1.2.2.2 nathanw if_attach(ifp);
467 1.2.2.2 nathanw ether_ifattach(ifp, sc->sc_enaddr);
468 1.2.2.2 nathanw
469 1.2.2.2 nathanw #if NRND > 0
470 1.2.2.2 nathanw rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
471 1.2.2.2 nathanw RND_TYPE_NET, 0);
472 1.2.2.2 nathanw #endif
473 1.2.2.2 nathanw sc->sc_cfgflags |= CFGFLG_ATTACHED;
474 1.2.2.2 nathanw
475 1.2.2.2 nathanw /* Reset the chip */
476 1.2.2.2 nathanw if (cs_reset_chip(sc) == CS_ERROR) {
477 1.2.2.2 nathanw printf("%s: reset failed\n", sc->sc_dev.dv_xname);
478 1.2.2.2 nathanw cs_detach(sc);
479 1.2.2.2 nathanw return 1;
480 1.2.2.2 nathanw }
481 1.2.2.2 nathanw
482 1.2.2.2 nathanw sc->sc_powerhook = powerhook_establish(cs_power, sc);
483 1.2.2.2 nathanw if (sc->sc_powerhook == 0)
484 1.2.2.2 nathanw printf("%s: warning: powerhook_establish failed\n",
485 1.2.2.2 nathanw sc->sc_dev.dv_xname);
486 1.2.2.2 nathanw
487 1.2.2.2 nathanw return 0;
488 1.2.2.2 nathanw }
489 1.2.2.2 nathanw
490 1.2.2.2 nathanw int
491 1.2.2.4 nathanw cs_detach(struct cs_softc *sc)
492 1.2.2.2 nathanw {
493 1.2.2.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
494 1.2.2.2 nathanw
495 1.2.2.2 nathanw if (sc->sc_powerhook) {
496 1.2.2.2 nathanw powerhook_disestablish(sc->sc_powerhook);
497 1.2.2.2 nathanw sc->sc_powerhook = 0;
498 1.2.2.2 nathanw }
499 1.2.2.2 nathanw
500 1.2.2.2 nathanw if (sc->sc_cfgflags & CFGFLG_ATTACHED) {
501 1.2.2.2 nathanw #if NRND > 0
502 1.2.2.2 nathanw rnd_detach_source(&sc->rnd_source);
503 1.2.2.2 nathanw #endif
504 1.2.2.2 nathanw ether_ifdetach(ifp);
505 1.2.2.2 nathanw if_detach(ifp);
506 1.2.2.2 nathanw sc->sc_cfgflags &= ~CFGFLG_ATTACHED;
507 1.2.2.2 nathanw }
508 1.2.2.2 nathanw
509 1.2.2.2 nathanw if (sc->sc_sh != NULL)
510 1.2.2.2 nathanw shutdownhook_disestablish(sc->sc_sh);
511 1.2.2.2 nathanw
512 1.2.2.2 nathanw #if 0
513 1.2.2.2 nathanw /*
514 1.2.2.2 nathanw * XXX not necessary
515 1.2.2.2 nathanw */
516 1.2.2.2 nathanw if (sc->sc_cfgflags & CFGFLG_DMA_MODE) {
517 1.2.2.2 nathanw isa_dmamem_unmap(sc->sc_ic, sc->sc_drq, sc->sc_dmabase, sc->sc_dmasize);
518 1.2.2.2 nathanw isa_dmamem_free(sc->sc_ic, sc->sc_drq, sc->sc_dmaaddr, sc->sc_dmasize);
519 1.2.2.2 nathanw isa_dmamap_destroy(sc->sc_ic, sc->sc_drq);
520 1.2.2.2 nathanw sc->sc_cfgflags &= ~CFGFLG_DMA_MODE;
521 1.2.2.2 nathanw }
522 1.2.2.2 nathanw #endif
523 1.2.2.2 nathanw
524 1.2.2.2 nathanw return 0;
525 1.2.2.2 nathanw }
526 1.2.2.2 nathanw
527 1.2.2.2 nathanw void
528 1.2.2.4 nathanw cs_get_default_media(struct cs_softc *sc)
529 1.2.2.2 nathanw {
530 1.2.2.2 nathanw u_int16_t adp_cfg, xmit_ctl;
531 1.2.2.2 nathanw
532 1.2.2.4 nathanw if (cs_verify_eeprom(sc) == CS_ERROR) {
533 1.2.2.2 nathanw printf("%s: cs_get_default_media: EEPROM missing or bad\n",
534 1.2.2.2 nathanw sc->sc_dev.dv_xname);
535 1.2.2.2 nathanw goto fakeit;
536 1.2.2.2 nathanw }
537 1.2.2.2 nathanw
538 1.2.2.4 nathanw if (cs_read_eeprom(sc, EEPROM_ADPTR_CFG, &adp_cfg) == CS_ERROR) {
539 1.2.2.2 nathanw printf("%s: unable to read adapter config from EEPROM\n",
540 1.2.2.2 nathanw sc->sc_dev.dv_xname);
541 1.2.2.2 nathanw goto fakeit;
542 1.2.2.2 nathanw }
543 1.2.2.2 nathanw
544 1.2.2.4 nathanw if (cs_read_eeprom(sc, EEPROM_XMIT_CTL, &xmit_ctl) == CS_ERROR) {
545 1.2.2.2 nathanw printf("%s: unable to read transmit control from EEPROM\n",
546 1.2.2.2 nathanw sc->sc_dev.dv_xname);
547 1.2.2.2 nathanw goto fakeit;
548 1.2.2.2 nathanw }
549 1.2.2.2 nathanw
550 1.2.2.2 nathanw switch (adp_cfg & ADPTR_CFG_MEDIA) {
551 1.2.2.2 nathanw case ADPTR_CFG_AUI:
552 1.2.2.2 nathanw ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_5);
553 1.2.2.2 nathanw break;
554 1.2.2.2 nathanw case ADPTR_CFG_10BASE2:
555 1.2.2.2 nathanw ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_2);
556 1.2.2.2 nathanw break;
557 1.2.2.2 nathanw case ADPTR_CFG_10BASET:
558 1.2.2.2 nathanw default:
559 1.2.2.2 nathanw if (xmit_ctl & XMIT_CTL_FDX)
560 1.2.2.2 nathanw ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_T|IFM_FDX);
561 1.2.2.2 nathanw else
562 1.2.2.2 nathanw ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_T);
563 1.2.2.2 nathanw break;
564 1.2.2.2 nathanw }
565 1.2.2.2 nathanw return;
566 1.2.2.2 nathanw
567 1.2.2.2 nathanw fakeit:
568 1.2.2.2 nathanw printf("%s: WARNING: default media setting may be inaccurate\n",
569 1.2.2.2 nathanw sc->sc_dev.dv_xname);
570 1.2.2.2 nathanw /* XXX Arbitrary... */
571 1.2.2.2 nathanw ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_T);
572 1.2.2.2 nathanw }
573 1.2.2.2 nathanw
574 1.2.2.2 nathanw int
575 1.2.2.4 nathanw cs_get_params(struct cs_softc *sc)
576 1.2.2.2 nathanw {
577 1.2.2.2 nathanw u_int16_t isaConfig;
578 1.2.2.2 nathanw u_int16_t adapterConfig;
579 1.2.2.2 nathanw
580 1.2.2.4 nathanw if (cs_verify_eeprom(sc) == CS_ERROR) {
581 1.2.2.2 nathanw printf("%s: cs_get_params: EEPROM missing or bad\n",
582 1.2.2.2 nathanw sc->sc_dev.dv_xname);
583 1.2.2.2 nathanw return (CS_ERROR);
584 1.2.2.2 nathanw }
585 1.2.2.2 nathanw
586 1.2.2.2 nathanw /* Get ISA configuration from the EEPROM */
587 1.2.2.4 nathanw if (cs_read_eeprom(sc, EEPROM_ISA_CFG, &isaConfig) == CS_ERROR)
588 1.2.2.2 nathanw goto eeprom_bad;
589 1.2.2.2 nathanw
590 1.2.2.2 nathanw /* Get adapter configuration from the EEPROM */
591 1.2.2.4 nathanw if (cs_read_eeprom(sc, EEPROM_ADPTR_CFG, &adapterConfig) == CS_ERROR)
592 1.2.2.2 nathanw goto eeprom_bad;
593 1.2.2.2 nathanw
594 1.2.2.2 nathanw /* Copy the USE_SA flag */
595 1.2.2.2 nathanw if (isaConfig & ISA_CFG_USE_SA)
596 1.2.2.2 nathanw sc->sc_cfgflags |= CFGFLG_USE_SA;
597 1.2.2.2 nathanw
598 1.2.2.2 nathanw /* Copy the IO Channel Ready flag */
599 1.2.2.2 nathanw if (isaConfig & ISA_CFG_IOCHRDY)
600 1.2.2.2 nathanw sc->sc_cfgflags |= CFGFLG_IOCHRDY;
601 1.2.2.2 nathanw
602 1.2.2.2 nathanw /* Copy the DC/DC Polarity flag */
603 1.2.2.2 nathanw if (adapterConfig & ADPTR_CFG_DCDC_POL)
604 1.2.2.2 nathanw sc->sc_cfgflags |= CFGFLG_DCDC_POL;
605 1.2.2.2 nathanw
606 1.2.2.2 nathanw return (CS_OK);
607 1.2.2.2 nathanw
608 1.2.2.2 nathanw eeprom_bad:
609 1.2.2.2 nathanw printf("%s: cs_get_params: unable to read from EEPROM\n",
610 1.2.2.2 nathanw sc->sc_dev.dv_xname);
611 1.2.2.2 nathanw return (CS_ERROR);
612 1.2.2.2 nathanw }
613 1.2.2.2 nathanw
614 1.2.2.2 nathanw int
615 1.2.2.4 nathanw cs_get_enaddr(struct cs_softc *sc)
616 1.2.2.2 nathanw {
617 1.2.2.2 nathanw u_int16_t *myea;
618 1.2.2.2 nathanw
619 1.2.2.4 nathanw if (cs_verify_eeprom(sc) == CS_ERROR) {
620 1.2.2.2 nathanw printf("%s: cs_get_enaddr: EEPROM missing or bad\n",
621 1.2.2.2 nathanw sc->sc_dev.dv_xname);
622 1.2.2.2 nathanw return (CS_ERROR);
623 1.2.2.2 nathanw }
624 1.2.2.2 nathanw
625 1.2.2.2 nathanw myea = (u_int16_t *)sc->sc_enaddr;
626 1.2.2.2 nathanw
627 1.2.2.2 nathanw /* Get Ethernet address from the EEPROM */
628 1.2.2.2 nathanw /* XXX this will likely lose on a big-endian machine. -- cgd */
629 1.2.2.4 nathanw if (cs_read_eeprom(sc, EEPROM_IND_ADDR_H, &myea[0]) == CS_ERROR)
630 1.2.2.2 nathanw goto eeprom_bad;
631 1.2.2.4 nathanw if (cs_read_eeprom(sc, EEPROM_IND_ADDR_M, &myea[1]) == CS_ERROR)
632 1.2.2.2 nathanw goto eeprom_bad;
633 1.2.2.4 nathanw if (cs_read_eeprom(sc, EEPROM_IND_ADDR_L, &myea[2]) == CS_ERROR)
634 1.2.2.2 nathanw goto eeprom_bad;
635 1.2.2.2 nathanw
636 1.2.2.2 nathanw return (CS_OK);
637 1.2.2.2 nathanw
638 1.2.2.2 nathanw eeprom_bad:
639 1.2.2.2 nathanw printf("%s: cs_get_enaddr: unable to read from EEPROM\n",
640 1.2.2.2 nathanw sc->sc_dev.dv_xname);
641 1.2.2.2 nathanw return (CS_ERROR);
642 1.2.2.2 nathanw }
643 1.2.2.2 nathanw
644 1.2.2.2 nathanw int
645 1.2.2.4 nathanw cs_reset_chip(struct cs_softc *sc)
646 1.2.2.2 nathanw {
647 1.2.2.2 nathanw int intState;
648 1.2.2.2 nathanw int x;
649 1.2.2.2 nathanw
650 1.2.2.2 nathanw /* Disable interrupts at the CPU so reset command is atomic */
651 1.2.2.2 nathanw intState = splnet();
652 1.2.2.2 nathanw
653 1.2.2.2 nathanw /*
654 1.2.2.2 nathanw * We are now resetting the chip
655 1.2.2.2 nathanw *
656 1.2.2.2 nathanw * A spurious interrupt is generated by the chip when it is reset. This
657 1.2.2.2 nathanw * variable informs the interrupt handler to ignore this interrupt.
658 1.2.2.2 nathanw */
659 1.2.2.2 nathanw sc->sc_resetting = TRUE;
660 1.2.2.2 nathanw
661 1.2.2.2 nathanw /* Issue a reset command to the chip */
662 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_SELF_CTL, SELF_CTL_RESET);
663 1.2.2.2 nathanw
664 1.2.2.2 nathanw /* Re-enable interrupts at the CPU */
665 1.2.2.2 nathanw splx(intState);
666 1.2.2.2 nathanw
667 1.2.2.2 nathanw /* The chip is always in IO mode after a reset */
668 1.2.2.2 nathanw sc->sc_memorymode = FALSE;
669 1.2.2.2 nathanw
670 1.2.2.2 nathanw /* If transmission was in progress, it is not now */
671 1.2.2.2 nathanw sc->sc_txbusy = FALSE;
672 1.2.2.2 nathanw
673 1.2.2.2 nathanw /*
674 1.2.2.2 nathanw * there was a delay(125); here, but it seems uneccesary 125 usec is
675 1.2.2.2 nathanw * 1/8000 of a second, not 1/8 of a second. the data sheet advises
676 1.2.2.2 nathanw * 1/10 of a second here, but the SI_BUSY and INIT_DONE loops below
677 1.2.2.2 nathanw * should be sufficient.
678 1.2.2.2 nathanw */
679 1.2.2.2 nathanw
680 1.2.2.2 nathanw /* Transition SBHE to switch chip from 8-bit to 16-bit */
681 1.2.2.4 nathanw IO_READ_1(sc, PORT_PKTPG_PTR + 0);
682 1.2.2.4 nathanw IO_READ_1(sc, PORT_PKTPG_PTR + 1);
683 1.2.2.4 nathanw IO_READ_1(sc, PORT_PKTPG_PTR + 0);
684 1.2.2.4 nathanw IO_READ_1(sc, PORT_PKTPG_PTR + 1);
685 1.2.2.2 nathanw
686 1.2.2.2 nathanw /* Wait until the EEPROM is not busy */
687 1.2.2.2 nathanw for (x = 0; x < MAXLOOP; x++) {
688 1.2.2.2 nathanw if (!(CS_READ_PACKET_PAGE(sc, PKTPG_SELF_ST) & SELF_ST_SI_BUSY))
689 1.2.2.2 nathanw break;
690 1.2.2.2 nathanw }
691 1.2.2.2 nathanw
692 1.2.2.2 nathanw if (x == MAXLOOP)
693 1.2.2.2 nathanw return CS_ERROR;
694 1.2.2.2 nathanw
695 1.2.2.2 nathanw /* Wait until initialization is done */
696 1.2.2.2 nathanw for (x = 0; x < MAXLOOP; x++) {
697 1.2.2.2 nathanw if (CS_READ_PACKET_PAGE(sc, PKTPG_SELF_ST) & SELF_ST_INIT_DONE)
698 1.2.2.2 nathanw break;
699 1.2.2.2 nathanw }
700 1.2.2.2 nathanw
701 1.2.2.2 nathanw if (x == MAXLOOP)
702 1.2.2.2 nathanw return CS_ERROR;
703 1.2.2.2 nathanw
704 1.2.2.2 nathanw /* Reset is no longer in progress */
705 1.2.2.2 nathanw sc->sc_resetting = FALSE;
706 1.2.2.2 nathanw
707 1.2.2.2 nathanw return CS_OK;
708 1.2.2.2 nathanw }
709 1.2.2.2 nathanw
710 1.2.2.2 nathanw int
711 1.2.2.4 nathanw cs_verify_eeprom(struct cs_softc *sc)
712 1.2.2.2 nathanw {
713 1.2.2.2 nathanw u_int16_t self_status;
714 1.2.2.2 nathanw
715 1.2.2.2 nathanw /* Verify that the EEPROM is present and OK */
716 1.2.2.4 nathanw self_status = CS_READ_PACKET_PAGE_IO(sc, PKTPG_SELF_ST);
717 1.2.2.2 nathanw if (((self_status & SELF_ST_EEP_PRES) &&
718 1.2.2.2 nathanw (self_status & SELF_ST_EEP_OK)) == 0)
719 1.2.2.2 nathanw return (CS_ERROR);
720 1.2.2.2 nathanw
721 1.2.2.2 nathanw return (CS_OK);
722 1.2.2.2 nathanw }
723 1.2.2.2 nathanw
724 1.2.2.2 nathanw int
725 1.2.2.4 nathanw cs_read_eeprom(struct cs_softc *sc, int offset, u_int16_t *pValue)
726 1.2.2.2 nathanw {
727 1.2.2.2 nathanw int x;
728 1.2.2.2 nathanw
729 1.2.2.2 nathanw /* Ensure that the EEPROM is not busy */
730 1.2.2.2 nathanw for (x = 0; x < MAXLOOP; x++) {
731 1.2.2.4 nathanw if (!(CS_READ_PACKET_PAGE_IO(sc, PKTPG_SELF_ST) &
732 1.2.2.2 nathanw SELF_ST_SI_BUSY))
733 1.2.2.2 nathanw break;
734 1.2.2.2 nathanw }
735 1.2.2.2 nathanw
736 1.2.2.2 nathanw if (x == MAXLOOP)
737 1.2.2.2 nathanw return (CS_ERROR);
738 1.2.2.2 nathanw
739 1.2.2.2 nathanw /* Issue the command to read the offset within the EEPROM */
740 1.2.2.4 nathanw CS_WRITE_PACKET_PAGE_IO(sc, PKTPG_EEPROM_CMD,
741 1.2.2.2 nathanw offset | EEPROM_CMD_READ);
742 1.2.2.2 nathanw
743 1.2.2.2 nathanw /* Wait until the command is completed */
744 1.2.2.2 nathanw for (x = 0; x < MAXLOOP; x++) {
745 1.2.2.4 nathanw if (!(CS_READ_PACKET_PAGE_IO(sc, PKTPG_SELF_ST) &
746 1.2.2.2 nathanw SELF_ST_SI_BUSY))
747 1.2.2.2 nathanw break;
748 1.2.2.2 nathanw }
749 1.2.2.2 nathanw
750 1.2.2.2 nathanw if (x == MAXLOOP)
751 1.2.2.2 nathanw return (CS_ERROR);
752 1.2.2.2 nathanw
753 1.2.2.2 nathanw /* Get the EEPROM data from the EEPROM Data register */
754 1.2.2.4 nathanw *pValue = CS_READ_PACKET_PAGE_IO(sc, PKTPG_EEPROM_DATA);
755 1.2.2.2 nathanw
756 1.2.2.2 nathanw return (CS_OK);
757 1.2.2.2 nathanw }
758 1.2.2.2 nathanw
759 1.2.2.2 nathanw void
760 1.2.2.4 nathanw cs_initChip(struct cs_softc *sc)
761 1.2.2.2 nathanw {
762 1.2.2.2 nathanw u_int16_t busCtl;
763 1.2.2.2 nathanw u_int16_t selfCtl;
764 1.2.2.4 nathanw u_int16_t v;
765 1.2.2.2 nathanw u_int16_t isaId;
766 1.2.2.4 nathanw int i;
767 1.2.2.2 nathanw int media = IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media);
768 1.2.2.2 nathanw
769 1.2.2.2 nathanw /* Disable reception and transmission of frames */
770 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL,
771 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_LINE_CTL) &
772 1.2.2.2 nathanw ~LINE_CTL_RX_ON & ~LINE_CTL_TX_ON);
773 1.2.2.2 nathanw
774 1.2.2.2 nathanw /* Disable interrupt at the chip */
775 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
776 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) & ~BUS_CTL_INT_ENBL);
777 1.2.2.2 nathanw
778 1.2.2.2 nathanw /* If IOCHRDY is enabled then clear the bit in the busCtl register */
779 1.2.2.2 nathanw busCtl = CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL);
780 1.2.2.2 nathanw if (sc->sc_cfgflags & CFGFLG_IOCHRDY) {
781 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
782 1.2.2.2 nathanw busCtl & ~BUS_CTL_IOCHRDY);
783 1.2.2.2 nathanw } else {
784 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
785 1.2.2.2 nathanw busCtl | BUS_CTL_IOCHRDY);
786 1.2.2.2 nathanw }
787 1.2.2.2 nathanw
788 1.2.2.2 nathanw /* Set the Line Control register to match the media type */
789 1.2.2.2 nathanw if (media == IFM_10_T)
790 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL, LINE_CTL_10BASET);
791 1.2.2.2 nathanw else
792 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL, LINE_CTL_AUI_ONLY);
793 1.2.2.2 nathanw
794 1.2.2.2 nathanw /*
795 1.2.2.2 nathanw * Set the BSTATUS/HC1 pin to be used as HC1. HC1 is used to
796 1.2.2.2 nathanw * enable the DC/DC converter
797 1.2.2.2 nathanw */
798 1.2.2.2 nathanw selfCtl = SELF_CTL_HC1E;
799 1.2.2.2 nathanw
800 1.2.2.2 nathanw /* If the media type is 10Base2 */
801 1.2.2.2 nathanw if (media == IFM_10_2) {
802 1.2.2.2 nathanw /*
803 1.2.2.2 nathanw * Enable the DC/DC converter if it has a low enable.
804 1.2.2.2 nathanw */
805 1.2.2.2 nathanw if ((sc->sc_cfgflags & CFGFLG_DCDC_POL) == 0)
806 1.2.2.2 nathanw /*
807 1.2.2.2 nathanw * Set the HCB1 bit, which causes the HC1 pin to go
808 1.2.2.2 nathanw * low.
809 1.2.2.2 nathanw */
810 1.2.2.2 nathanw selfCtl |= SELF_CTL_HCB1;
811 1.2.2.2 nathanw } else { /* Media type is 10BaseT or AUI */
812 1.2.2.2 nathanw /*
813 1.2.2.2 nathanw * Disable the DC/DC converter if it has a high enable.
814 1.2.2.2 nathanw */
815 1.2.2.2 nathanw if ((sc->sc_cfgflags & CFGFLG_DCDC_POL) != 0) {
816 1.2.2.2 nathanw /*
817 1.2.2.2 nathanw * Set the HCB1 bit, which causes the HC1 pin to go
818 1.2.2.2 nathanw * low.
819 1.2.2.2 nathanw */
820 1.2.2.2 nathanw selfCtl |= SELF_CTL_HCB1;
821 1.2.2.2 nathanw }
822 1.2.2.2 nathanw }
823 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_SELF_CTL, selfCtl);
824 1.2.2.2 nathanw
825 1.2.2.2 nathanw /* enable normal link pulse */
826 1.2.2.2 nathanw if (sc->sc_prodid == PROD_ID_CS8920 || sc->sc_prodid == PROD_ID_CS8920M)
827 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_AUTONEG_CTL, AUTOCTL_NLP_ENABLE);
828 1.2.2.2 nathanw
829 1.2.2.2 nathanw /* Enable full-duplex, if appropriate */
830 1.2.2.2 nathanw if (sc->sc_media.ifm_cur->ifm_media & IFM_FDX)
831 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_TEST_CTL, TEST_CTL_FDX);
832 1.2.2.2 nathanw
833 1.2.2.2 nathanw /* RX_CTL set in cs_set_ladr_filt(), below */
834 1.2.2.2 nathanw
835 1.2.2.2 nathanw /* enable all transmission interrupts */
836 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_CFG, TX_CFG_ALL_IE);
837 1.2.2.2 nathanw
838 1.2.2.2 nathanw /* Accept all receive interrupts */
839 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG, RX_CFG_ALL_IE);
840 1.2.2.2 nathanw
841 1.2.2.2 nathanw /*
842 1.2.2.2 nathanw * Configure Operational Modes
843 1.2.2.2 nathanw *
844 1.2.2.2 nathanw * I have turned off the BUF_CFG_RX_MISS_IE, to speed things up, this is
845 1.2.2.2 nathanw * a better way to do it because the card has a counter which can be
846 1.2.2.5 thorpej * read to update the RX_MISS counter. This saves many interrupts.
847 1.2.2.2 nathanw *
848 1.2.2.5 thorpej * I have turned on the tx and rx overflow interrupts to counter using
849 1.2.2.2 nathanw * the receive miss interrupt. This is a better estimate of errors
850 1.2.2.2 nathanw * and requires lower system overhead.
851 1.2.2.2 nathanw */
852 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_BUF_CFG, BUF_CFG_TX_UNDR_IE |
853 1.2.2.2 nathanw BUF_CFG_RX_DMA_IE);
854 1.2.2.2 nathanw
855 1.2.2.2 nathanw if (sc->sc_dma_chipinit)
856 1.2.2.2 nathanw (*sc->sc_dma_chipinit)(sc);
857 1.2.2.2 nathanw
858 1.2.2.2 nathanw /* If memory mode is enabled */
859 1.2.2.2 nathanw if (sc->sc_cfgflags & CFGFLG_MEM_MODE) {
860 1.2.2.2 nathanw /* If external logic is present for address decoding */
861 1.2.2.2 nathanw if (CS_READ_PACKET_PAGE(sc, PKTPG_SELF_ST) & SELF_ST_EL_PRES) {
862 1.2.2.2 nathanw /*
863 1.2.2.2 nathanw * Program the external logic to decode address bits
864 1.2.2.2 nathanw * SA20-SA23
865 1.2.2.2 nathanw */
866 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_EEPROM_CMD,
867 1.2.2.2 nathanw ((sc->sc_pktpgaddr & 0xffffff) >> 20) |
868 1.2.2.2 nathanw EEPROM_CMD_ELSEL);
869 1.2.2.2 nathanw }
870 1.2.2.2 nathanw
871 1.2.2.2 nathanw /*
872 1.2.2.2 nathanw * Write the packet page base physical address to the memory
873 1.2.2.2 nathanw * base register.
874 1.2.2.2 nathanw */
875 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_MEM_BASE + 0,
876 1.2.2.2 nathanw sc->sc_pktpgaddr & 0xFFFF);
877 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_MEM_BASE + 2,
878 1.2.2.2 nathanw sc->sc_pktpgaddr >> 16);
879 1.2.2.2 nathanw busCtl = BUS_CTL_MEM_MODE;
880 1.2.2.2 nathanw
881 1.2.2.2 nathanw /* tell the chip to read the addresses off the SA pins */
882 1.2.2.2 nathanw if (sc->sc_cfgflags & CFGFLG_USE_SA) {
883 1.2.2.2 nathanw busCtl |= BUS_CTL_USE_SA;
884 1.2.2.2 nathanw }
885 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
886 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) | busCtl);
887 1.2.2.2 nathanw
888 1.2.2.2 nathanw /* We are in memory mode now! */
889 1.2.2.2 nathanw sc->sc_memorymode = TRUE;
890 1.2.2.2 nathanw
891 1.2.2.2 nathanw /*
892 1.2.2.2 nathanw * wait here (10ms) for the chip to swap over. this is the
893 1.2.2.2 nathanw * maximum time that this could take.
894 1.2.2.2 nathanw */
895 1.2.2.2 nathanw delay(10000);
896 1.2.2.2 nathanw
897 1.2.2.2 nathanw /* Verify that we can read from the chip */
898 1.2.2.2 nathanw isaId = CS_READ_PACKET_PAGE(sc, PKTPG_EISA_NUM);
899 1.2.2.2 nathanw
900 1.2.2.2 nathanw /*
901 1.2.2.2 nathanw * As a last minute sanity check before actually using mapped
902 1.2.2.2 nathanw * memory we verify that we can read the isa number from the
903 1.2.2.2 nathanw * chip in memory mode.
904 1.2.2.2 nathanw */
905 1.2.2.2 nathanw if (isaId != EISA_NUM_CRYSTAL) {
906 1.2.2.2 nathanw printf("%s: failed to enable memory mode\n",
907 1.2.2.2 nathanw sc->sc_dev.dv_xname);
908 1.2.2.2 nathanw sc->sc_memorymode = FALSE;
909 1.2.2.2 nathanw } else {
910 1.2.2.2 nathanw /*
911 1.2.2.2 nathanw * we are in memory mode so if we aren't using DMA,
912 1.2.2.2 nathanw * then program the chip to interrupt early.
913 1.2.2.2 nathanw */
914 1.2.2.2 nathanw if ((sc->sc_cfgflags & CFGFLG_DMA_MODE) == 0) {
915 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_BUF_CFG,
916 1.2.2.2 nathanw BUF_CFG_RX_DEST_IE |
917 1.2.2.2 nathanw BUF_CFG_RX_MISS_OVER_IE |
918 1.2.2.2 nathanw BUF_CFG_TX_COL_OVER_IE);
919 1.2.2.2 nathanw }
920 1.2.2.2 nathanw }
921 1.2.2.2 nathanw
922 1.2.2.2 nathanw }
923 1.2.2.2 nathanw
924 1.2.2.2 nathanw /* Put Ethernet address into the Individual Address register */
925 1.2.2.4 nathanw for (i = 0; i < 6; i += 2) {
926 1.2.2.4 nathanw v = sc->sc_enaddr[i + 0] | (sc->sc_enaddr[i + 1]) << 8;
927 1.2.2.4 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_IND_ADDR + i, v);
928 1.2.2.4 nathanw }
929 1.2.2.2 nathanw
930 1.2.2.2 nathanw if (sc->sc_irq != -1) {
931 1.2.2.2 nathanw /* Set the interrupt level in the chip */
932 1.2.2.2 nathanw if (sc->sc_prodid == PROD_ID_CS8900) {
933 1.2.2.2 nathanw if (sc->sc_irq == 5) {
934 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_INT_NUM, 3);
935 1.2.2.2 nathanw } else {
936 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_INT_NUM, (sc->sc_irq) - 10);
937 1.2.2.2 nathanw }
938 1.2.2.2 nathanw }
939 1.2.2.2 nathanw else { /* CS8920 */
940 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_8920_INT_NUM, sc->sc_irq);
941 1.2.2.2 nathanw }
942 1.2.2.2 nathanw }
943 1.2.2.2 nathanw
944 1.2.2.2 nathanw /* write the multicast mask to the address filter register */
945 1.2.2.2 nathanw cs_set_ladr_filt(sc, &sc->sc_ethercom);
946 1.2.2.2 nathanw
947 1.2.2.2 nathanw /* Enable reception and transmission of frames */
948 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL,
949 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_LINE_CTL) |
950 1.2.2.2 nathanw LINE_CTL_RX_ON | LINE_CTL_TX_ON);
951 1.2.2.2 nathanw
952 1.2.2.2 nathanw /* Enable interrupt at the chip */
953 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
954 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) | BUS_CTL_INT_ENBL);
955 1.2.2.2 nathanw }
956 1.2.2.2 nathanw
957 1.2.2.2 nathanw int
958 1.2.2.4 nathanw cs_init(struct ifnet *ifp)
959 1.2.2.2 nathanw {
960 1.2.2.2 nathanw int intState;
961 1.2.2.2 nathanw int error = CS_OK;
962 1.2.2.2 nathanw struct cs_softc *sc = ifp->if_softc;
963 1.2.2.2 nathanw
964 1.2.2.2 nathanw if (cs_enable(sc))
965 1.2.2.2 nathanw goto out;
966 1.2.2.2 nathanw
967 1.2.2.2 nathanw cs_stop(ifp, 0);
968 1.2.2.2 nathanw
969 1.2.2.2 nathanw intState = splnet();
970 1.2.2.2 nathanw
971 1.2.2.2 nathanw #if 0
972 1.2.2.2 nathanw /* Mark the interface as down */
973 1.2.2.2 nathanw sc->sc_ethercom.ec_if.if_flags &= ~(IFF_UP | IFF_RUNNING);
974 1.2.2.2 nathanw #endif
975 1.2.2.2 nathanw
976 1.2.2.2 nathanw #ifdef CS_DEBUG
977 1.2.2.2 nathanw /* Enable debugging */
978 1.2.2.2 nathanw sc->sc_ethercom.ec_if.if_flags |= IFF_DEBUG;
979 1.2.2.2 nathanw #endif
980 1.2.2.2 nathanw
981 1.2.2.2 nathanw /* Reset the chip */
982 1.2.2.2 nathanw if ((error = cs_reset_chip(sc)) == CS_OK) {
983 1.2.2.2 nathanw /* Initialize the chip */
984 1.2.2.2 nathanw cs_initChip(sc);
985 1.2.2.2 nathanw
986 1.2.2.2 nathanw /* Mark the interface as running */
987 1.2.2.2 nathanw sc->sc_ethercom.ec_if.if_flags |= IFF_RUNNING;
988 1.2.2.2 nathanw sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
989 1.2.2.2 nathanw sc->sc_ethercom.ec_if.if_timer = 0;
990 1.2.2.2 nathanw
991 1.2.2.2 nathanw /* Assume we have carrier until we are told otherwise. */
992 1.2.2.2 nathanw sc->sc_carrier = 1;
993 1.2.2.2 nathanw } else {
994 1.2.2.2 nathanw printf("%s: unable to reset chip\n", sc->sc_dev.dv_xname);
995 1.2.2.2 nathanw }
996 1.2.2.2 nathanw
997 1.2.2.2 nathanw splx(intState);
998 1.2.2.2 nathanw out:
999 1.2.2.2 nathanw if (error == CS_OK)
1000 1.2.2.2 nathanw return 0;
1001 1.2.2.2 nathanw return EIO;
1002 1.2.2.2 nathanw }
1003 1.2.2.2 nathanw
1004 1.2.2.2 nathanw void
1005 1.2.2.4 nathanw cs_set_ladr_filt(struct cs_softc *sc, struct ethercom *ec)
1006 1.2.2.2 nathanw {
1007 1.2.2.2 nathanw struct ifnet *ifp = &ec->ec_if;
1008 1.2.2.2 nathanw struct ether_multi *enm;
1009 1.2.2.2 nathanw struct ether_multistep step;
1010 1.2.2.2 nathanw u_int16_t af[4];
1011 1.2.2.2 nathanw u_int16_t port, mask, index;
1012 1.2.2.2 nathanw
1013 1.2.2.2 nathanw /*
1014 1.2.2.2 nathanw * Set up multicast address filter by passing all multicast addresses
1015 1.2.2.2 nathanw * through a crc generator, and then using the high order 6 bits as an
1016 1.2.2.2 nathanw * index into the 64 bit logical address filter. The high order bit
1017 1.2.2.2 nathanw * selects the word, while the rest of the bits select the bit within
1018 1.2.2.2 nathanw * the word.
1019 1.2.2.2 nathanw */
1020 1.2.2.2 nathanw if (ifp->if_flags & IFF_PROMISC) {
1021 1.2.2.2 nathanw /* accept all valid frames. */
1022 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CTL,
1023 1.2.2.2 nathanw RX_CTL_PROMISC_A | RX_CTL_RX_OK_A |
1024 1.2.2.2 nathanw RX_CTL_IND_A | RX_CTL_BCAST_A | RX_CTL_MCAST_A);
1025 1.2.2.2 nathanw ifp->if_flags |= IFF_ALLMULTI;
1026 1.2.2.2 nathanw return;
1027 1.2.2.2 nathanw }
1028 1.2.2.2 nathanw
1029 1.2.2.2 nathanw /*
1030 1.2.2.2 nathanw * accept frames if a. crc valid, b. individual address match c.
1031 1.2.2.2 nathanw * broadcast address,and d. multicast addresses matched in the hash
1032 1.2.2.2 nathanw * filter
1033 1.2.2.2 nathanw */
1034 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CTL,
1035 1.2.2.2 nathanw RX_CTL_RX_OK_A | RX_CTL_IND_A | RX_CTL_BCAST_A | RX_CTL_MCAST_A);
1036 1.2.2.2 nathanw
1037 1.2.2.2 nathanw
1038 1.2.2.2 nathanw /*
1039 1.2.2.2 nathanw * start off with all multicast flag clear, set it if we need to
1040 1.2.2.2 nathanw * later, otherwise we will leave it.
1041 1.2.2.2 nathanw */
1042 1.2.2.2 nathanw ifp->if_flags &= ~IFF_ALLMULTI;
1043 1.2.2.2 nathanw af[0] = af[1] = af[2] = af[3] = 0x0000;
1044 1.2.2.2 nathanw
1045 1.2.2.2 nathanw /*
1046 1.2.2.2 nathanw * Loop through all the multicast addresses unless we get a range of
1047 1.2.2.2 nathanw * addresses, in which case we will just accept all packets.
1048 1.2.2.2 nathanw * Justification for this is given in the next comment.
1049 1.2.2.2 nathanw */
1050 1.2.2.2 nathanw ETHER_FIRST_MULTI(step, ec, enm);
1051 1.2.2.2 nathanw while (enm != NULL) {
1052 1.2.2.2 nathanw if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1053 1.2.2.2 nathanw sizeof enm->enm_addrlo)) {
1054 1.2.2.2 nathanw /*
1055 1.2.2.2 nathanw * We must listen to a range of multicast addresses.
1056 1.2.2.2 nathanw * For now, just accept all multicasts, rather than
1057 1.2.2.2 nathanw * trying to set only those filter bits needed to match
1058 1.2.2.2 nathanw * the range. (At this time, the only use of address
1059 1.2.2.2 nathanw * ranges is for IP multicast routing, for which the
1060 1.2.2.2 nathanw * range is big enough to require all bits set.)
1061 1.2.2.2 nathanw */
1062 1.2.2.2 nathanw ifp->if_flags |= IFF_ALLMULTI;
1063 1.2.2.2 nathanw af[0] = af[1] = af[2] = af[3] = 0xffff;
1064 1.2.2.2 nathanw break;
1065 1.2.2.2 nathanw } else {
1066 1.2.2.2 nathanw /*
1067 1.2.2.2 nathanw * we have got an individual address so just set that
1068 1.2.2.2 nathanw * bit.
1069 1.2.2.2 nathanw */
1070 1.2.2.2 nathanw index = cs_hash_index(enm->enm_addrlo);
1071 1.2.2.2 nathanw
1072 1.2.2.2 nathanw /* Set the bit the Logical address filter. */
1073 1.2.2.2 nathanw port = (u_int16_t) (index >> 4);
1074 1.2.2.2 nathanw mask = (u_int16_t) (1 << (index & 0xf));
1075 1.2.2.2 nathanw af[port] |= mask;
1076 1.2.2.2 nathanw
1077 1.2.2.2 nathanw ETHER_NEXT_MULTI(step, enm);
1078 1.2.2.2 nathanw }
1079 1.2.2.2 nathanw }
1080 1.2.2.2 nathanw
1081 1.2.2.2 nathanw /* now program the chip with the addresses */
1082 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 0, af[0]);
1083 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 2, af[1]);
1084 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 4, af[2]);
1085 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 6, af[3]);
1086 1.2.2.2 nathanw return;
1087 1.2.2.2 nathanw }
1088 1.2.2.2 nathanw
1089 1.2.2.2 nathanw u_int16_t
1090 1.2.2.4 nathanw cs_hash_index(char *addr)
1091 1.2.2.2 nathanw {
1092 1.2.2.4 nathanw uint32_t crc;
1093 1.2.2.4 nathanw uint16_t hash_code;
1094 1.2.2.2 nathanw
1095 1.2.2.4 nathanw crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
1096 1.2.2.2 nathanw
1097 1.2.2.4 nathanw hash_code = crc >> 26;
1098 1.2.2.4 nathanw return (hash_code);
1099 1.2.2.2 nathanw }
1100 1.2.2.2 nathanw
1101 1.2.2.2 nathanw void
1102 1.2.2.4 nathanw cs_reset(void *arg)
1103 1.2.2.2 nathanw {
1104 1.2.2.2 nathanw struct cs_softc *sc = arg;
1105 1.2.2.2 nathanw
1106 1.2.2.2 nathanw /* Mark the interface as down */
1107 1.2.2.2 nathanw sc->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
1108 1.2.2.2 nathanw
1109 1.2.2.2 nathanw /* Reset the chip */
1110 1.2.2.2 nathanw cs_reset_chip(sc);
1111 1.2.2.2 nathanw }
1112 1.2.2.2 nathanw
1113 1.2.2.2 nathanw int
1114 1.2.2.4 nathanw cs_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1115 1.2.2.2 nathanw {
1116 1.2.2.2 nathanw struct cs_softc *sc = ifp->if_softc;
1117 1.2.2.2 nathanw struct ifreq *ifr = (struct ifreq *) data;
1118 1.2.2.2 nathanw int state;
1119 1.2.2.2 nathanw int result;
1120 1.2.2.2 nathanw
1121 1.2.2.2 nathanw state = splnet();
1122 1.2.2.2 nathanw
1123 1.2.2.2 nathanw result = 0; /* only set if something goes wrong */
1124 1.2.2.2 nathanw
1125 1.2.2.2 nathanw switch (cmd) {
1126 1.2.2.2 nathanw case SIOCGIFMEDIA:
1127 1.2.2.2 nathanw case SIOCSIFMEDIA:
1128 1.2.2.2 nathanw result = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1129 1.2.2.2 nathanw break;
1130 1.2.2.2 nathanw
1131 1.2.2.2 nathanw default:
1132 1.2.2.2 nathanw result = ether_ioctl(ifp, cmd, data);
1133 1.2.2.2 nathanw if (result == ENETRESET) {
1134 1.2.2.2 nathanw if (CS_IS_ENABLED(sc)) {
1135 1.2.2.2 nathanw /*
1136 1.2.2.2 nathanw * Multicast list has changed. Set the
1137 1.2.2.2 nathanw * hardware filter accordingly.
1138 1.2.2.2 nathanw */
1139 1.2.2.2 nathanw cs_set_ladr_filt(sc, &sc->sc_ethercom);
1140 1.2.2.2 nathanw }
1141 1.2.2.2 nathanw result = 0;
1142 1.2.2.2 nathanw }
1143 1.2.2.2 nathanw break;
1144 1.2.2.2 nathanw }
1145 1.2.2.2 nathanw
1146 1.2.2.2 nathanw splx(state);
1147 1.2.2.2 nathanw
1148 1.2.2.2 nathanw return result;
1149 1.2.2.2 nathanw }
1150 1.2.2.2 nathanw
1151 1.2.2.2 nathanw int
1152 1.2.2.4 nathanw cs_mediachange(struct ifnet *ifp)
1153 1.2.2.2 nathanw {
1154 1.2.2.2 nathanw
1155 1.2.2.2 nathanw /*
1156 1.2.2.2 nathanw * Current media is already set up. Just reset the interface
1157 1.2.2.2 nathanw * to let the new value take hold.
1158 1.2.2.2 nathanw */
1159 1.2.2.2 nathanw cs_init(ifp);
1160 1.2.2.2 nathanw return (0);
1161 1.2.2.2 nathanw }
1162 1.2.2.2 nathanw
1163 1.2.2.2 nathanw void
1164 1.2.2.4 nathanw cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1165 1.2.2.2 nathanw {
1166 1.2.2.2 nathanw struct cs_softc *sc = ifp->if_softc;
1167 1.2.2.2 nathanw
1168 1.2.2.2 nathanw /*
1169 1.2.2.2 nathanw * The currently selected media is always the active media.
1170 1.2.2.2 nathanw */
1171 1.2.2.2 nathanw ifmr->ifm_active = sc->sc_media.ifm_cur->ifm_media;
1172 1.2.2.2 nathanw
1173 1.2.2.2 nathanw if (ifp->if_flags & IFF_UP) {
1174 1.2.2.2 nathanw /* Interface up, status is valid. */
1175 1.2.2.2 nathanw ifmr->ifm_status = IFM_AVALID |
1176 1.2.2.2 nathanw (sc->sc_carrier ? IFM_ACTIVE : 0);
1177 1.2.2.2 nathanw }
1178 1.2.2.2 nathanw else ifmr->ifm_status = 0;
1179 1.2.2.2 nathanw }
1180 1.2.2.2 nathanw
1181 1.2.2.2 nathanw int
1182 1.2.2.4 nathanw cs_intr(void *arg)
1183 1.2.2.2 nathanw {
1184 1.2.2.2 nathanw struct cs_softc *sc = arg;
1185 1.2.2.2 nathanw u_int16_t Event;
1186 1.2.2.2 nathanw #if NRND > 0
1187 1.2.2.2 nathanw u_int16_t rndEvent;
1188 1.2.2.2 nathanw #endif
1189 1.2.2.2 nathanw
1190 1.2.2.4 nathanw /*printf("cs_intr %p\n", sc);*/
1191 1.2.2.2 nathanw /* Ignore any interrupts that happen while the chip is being reset */
1192 1.2.2.2 nathanw if (sc->sc_resetting) {
1193 1.2.2.2 nathanw printf("%s: cs_intr: reset in progress\n",
1194 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1195 1.2.2.2 nathanw return 1;
1196 1.2.2.2 nathanw }
1197 1.2.2.2 nathanw
1198 1.2.2.2 nathanw /* Read an event from the Interrupt Status Queue */
1199 1.2.2.2 nathanw if (sc->sc_memorymode)
1200 1.2.2.2 nathanw Event = CS_READ_PACKET_PAGE(sc, PKTPG_ISQ);
1201 1.2.2.2 nathanw else
1202 1.2.2.2 nathanw Event = CS_READ_PORT(sc, PORT_ISQ);
1203 1.2.2.2 nathanw
1204 1.2.2.2 nathanw if ((Event & REG_NUM_MASK) == 0 || Event == 0xffff)
1205 1.2.2.2 nathanw return 0; /* not ours */
1206 1.2.2.2 nathanw
1207 1.2.2.2 nathanw #if NRND > 0
1208 1.2.2.2 nathanw rndEvent = Event;
1209 1.2.2.2 nathanw #endif
1210 1.2.2.2 nathanw
1211 1.2.2.2 nathanw /* Process all the events in the Interrupt Status Queue */
1212 1.2.2.2 nathanw while ((Event & REG_NUM_MASK) != 0 && Event != 0xffff) {
1213 1.2.2.2 nathanw /* Dispatch to an event handler based on the register number */
1214 1.2.2.2 nathanw switch (Event & REG_NUM_MASK) {
1215 1.2.2.2 nathanw case REG_NUM_RX_EVENT:
1216 1.2.2.2 nathanw cs_receive_event(sc, Event);
1217 1.2.2.2 nathanw break;
1218 1.2.2.2 nathanw case REG_NUM_TX_EVENT:
1219 1.2.2.2 nathanw cs_transmit_event(sc, Event);
1220 1.2.2.2 nathanw break;
1221 1.2.2.2 nathanw case REG_NUM_BUF_EVENT:
1222 1.2.2.2 nathanw cs_buffer_event(sc, Event);
1223 1.2.2.2 nathanw break;
1224 1.2.2.2 nathanw case REG_NUM_TX_COL:
1225 1.2.2.2 nathanw case REG_NUM_RX_MISS:
1226 1.2.2.2 nathanw cs_counter_event(sc, Event);
1227 1.2.2.2 nathanw break;
1228 1.2.2.2 nathanw default:
1229 1.2.2.2 nathanw printf("%s: unknown interrupt event 0x%x\n",
1230 1.2.2.2 nathanw sc->sc_dev.dv_xname, Event);
1231 1.2.2.2 nathanw break;
1232 1.2.2.2 nathanw }
1233 1.2.2.2 nathanw
1234 1.2.2.2 nathanw /* Read another event from the Interrupt Status Queue */
1235 1.2.2.2 nathanw if (sc->sc_memorymode)
1236 1.2.2.2 nathanw Event = CS_READ_PACKET_PAGE(sc, PKTPG_ISQ);
1237 1.2.2.2 nathanw else
1238 1.2.2.2 nathanw Event = CS_READ_PORT(sc, PORT_ISQ);
1239 1.2.2.2 nathanw }
1240 1.2.2.2 nathanw
1241 1.2.2.5 thorpej /* have handled the interrupt */
1242 1.2.2.2 nathanw #if NRND > 0
1243 1.2.2.2 nathanw rnd_add_uint32(&sc->rnd_source, rndEvent);
1244 1.2.2.2 nathanw #endif
1245 1.2.2.2 nathanw return 1;
1246 1.2.2.2 nathanw }
1247 1.2.2.2 nathanw
1248 1.2.2.2 nathanw void
1249 1.2.2.4 nathanw cs_counter_event(struct cs_softc *sc, u_int16_t cntEvent)
1250 1.2.2.2 nathanw {
1251 1.2.2.2 nathanw struct ifnet *ifp;
1252 1.2.2.2 nathanw u_int16_t errorCount;
1253 1.2.2.2 nathanw
1254 1.2.2.2 nathanw ifp = &sc->sc_ethercom.ec_if;
1255 1.2.2.2 nathanw
1256 1.2.2.2 nathanw switch (cntEvent & REG_NUM_MASK) {
1257 1.2.2.2 nathanw case REG_NUM_TX_COL:
1258 1.2.2.2 nathanw /*
1259 1.2.2.2 nathanw * the count should be read before an overflow occurs.
1260 1.2.2.2 nathanw */
1261 1.2.2.2 nathanw errorCount = CS_READ_PACKET_PAGE(sc, PKTPG_TX_COL);
1262 1.2.2.2 nathanw /*
1263 1.2.2.2 nathanw * the tramsit event routine always checks the number of
1264 1.2.2.2 nathanw * collisions for any packet so we don't increment any
1265 1.2.2.2 nathanw * counters here, as they should already have been
1266 1.2.2.2 nathanw * considered.
1267 1.2.2.2 nathanw */
1268 1.2.2.2 nathanw break;
1269 1.2.2.2 nathanw case REG_NUM_RX_MISS:
1270 1.2.2.2 nathanw /*
1271 1.2.2.2 nathanw * the count should be read before an overflow occurs.
1272 1.2.2.2 nathanw */
1273 1.2.2.2 nathanw errorCount = CS_READ_PACKET_PAGE(sc, PKTPG_RX_MISS);
1274 1.2.2.2 nathanw /*
1275 1.2.2.2 nathanw * Increment the input error count, the first 6bits are the
1276 1.2.2.2 nathanw * register id.
1277 1.2.2.2 nathanw */
1278 1.2.2.2 nathanw ifp->if_ierrors += ((errorCount & 0xffC0) >> 6);
1279 1.2.2.2 nathanw break;
1280 1.2.2.2 nathanw default:
1281 1.2.2.2 nathanw /* do nothing */
1282 1.2.2.2 nathanw break;
1283 1.2.2.2 nathanw }
1284 1.2.2.2 nathanw }
1285 1.2.2.2 nathanw
1286 1.2.2.2 nathanw void
1287 1.2.2.4 nathanw cs_buffer_event(struct cs_softc *sc, u_int16_t bufEvent)
1288 1.2.2.2 nathanw {
1289 1.2.2.2 nathanw struct ifnet *ifp;
1290 1.2.2.2 nathanw
1291 1.2.2.2 nathanw ifp = &sc->sc_ethercom.ec_if;
1292 1.2.2.2 nathanw
1293 1.2.2.2 nathanw /*
1294 1.2.2.2 nathanw * multiple events can be in the buffer event register at one time so
1295 1.2.2.2 nathanw * a standard switch statement will not suffice, here every event
1296 1.2.2.2 nathanw * must be checked.
1297 1.2.2.2 nathanw */
1298 1.2.2.2 nathanw
1299 1.2.2.2 nathanw /*
1300 1.2.2.2 nathanw * if 128 bits have been rxed by the time we get here, the dest event
1301 1.2.2.2 nathanw * will be cleared and 128 event will be set.
1302 1.2.2.2 nathanw */
1303 1.2.2.2 nathanw if ((bufEvent & (BUF_EVENT_RX_DEST | BUF_EVENT_RX_128)) != 0) {
1304 1.2.2.2 nathanw cs_process_rx_early(sc);
1305 1.2.2.2 nathanw }
1306 1.2.2.2 nathanw
1307 1.2.2.2 nathanw if (bufEvent & BUF_EVENT_RX_DMA) {
1308 1.2.2.2 nathanw /* process the receive data */
1309 1.2.2.2 nathanw if (sc->sc_dma_process_rx)
1310 1.2.2.2 nathanw (*sc->sc_dma_process_rx)(sc);
1311 1.2.2.2 nathanw else
1312 1.2.2.2 nathanw /* should panic? */
1313 1.2.2.2 nathanw printf("%s: unexpected dma event\n", sc->sc_dev.dv_xname);
1314 1.2.2.2 nathanw }
1315 1.2.2.2 nathanw
1316 1.2.2.2 nathanw if (bufEvent & BUF_EVENT_TX_UNDR) {
1317 1.2.2.2 nathanw #if 0
1318 1.2.2.2 nathanw /*
1319 1.2.2.2 nathanw * This can happen occasionally, and it's not worth worrying
1320 1.2.2.2 nathanw * about.
1321 1.2.2.2 nathanw */
1322 1.2.2.2 nathanw printf("%s: transmit underrun (%d -> %d)\n",
1323 1.2.2.2 nathanw sc->sc_dev.dv_xname, sc->sc_xe_ent,
1324 1.2.2.2 nathanw cs_xmit_early_table[sc->sc_xe_ent].worse);
1325 1.2.2.2 nathanw #endif
1326 1.2.2.2 nathanw sc->sc_xe_ent = cs_xmit_early_table[sc->sc_xe_ent].worse;
1327 1.2.2.2 nathanw sc->sc_xe_togo =
1328 1.2.2.2 nathanw cs_xmit_early_table[sc->sc_xe_ent].better_count;
1329 1.2.2.2 nathanw
1330 1.2.2.2 nathanw /* had an underrun, transmit is finished */
1331 1.2.2.2 nathanw sc->sc_txbusy = FALSE;
1332 1.2.2.2 nathanw }
1333 1.2.2.2 nathanw
1334 1.2.2.2 nathanw if (bufEvent & BUF_EVENT_SW_INT) {
1335 1.2.2.2 nathanw printf("%s: software initiated interrupt\n",
1336 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1337 1.2.2.2 nathanw }
1338 1.2.2.2 nathanw }
1339 1.2.2.2 nathanw
1340 1.2.2.2 nathanw void
1341 1.2.2.4 nathanw cs_transmit_event(struct cs_softc *sc, u_int16_t txEvent)
1342 1.2.2.2 nathanw {
1343 1.2.2.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1344 1.2.2.2 nathanw
1345 1.2.2.2 nathanw /* If there were any errors transmitting this frame */
1346 1.2.2.2 nathanw if (txEvent & (TX_EVENT_LOSS_CRS | TX_EVENT_SQE_ERR | TX_EVENT_OUT_WIN |
1347 1.2.2.2 nathanw TX_EVENT_JABBER | TX_EVENT_16_COLL)) {
1348 1.2.2.2 nathanw /* Increment the output error count */
1349 1.2.2.2 nathanw ifp->if_oerrors++;
1350 1.2.2.2 nathanw
1351 1.2.2.2 nathanw /* Note carrier loss. */
1352 1.2.2.2 nathanw if (txEvent & TX_EVENT_LOSS_CRS)
1353 1.2.2.2 nathanw sc->sc_carrier = 0;
1354 1.2.2.2 nathanw
1355 1.2.2.2 nathanw /* If debugging is enabled then log error messages */
1356 1.2.2.2 nathanw if (ifp->if_flags & IFF_DEBUG) {
1357 1.2.2.2 nathanw if (txEvent & TX_EVENT_LOSS_CRS) {
1358 1.2.2.2 nathanw printf("%s: lost carrier\n",
1359 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1360 1.2.2.2 nathanw }
1361 1.2.2.2 nathanw if (txEvent & TX_EVENT_SQE_ERR) {
1362 1.2.2.2 nathanw printf("%s: SQE error\n",
1363 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1364 1.2.2.2 nathanw }
1365 1.2.2.2 nathanw if (txEvent & TX_EVENT_OUT_WIN) {
1366 1.2.2.2 nathanw printf("%s: out-of-window collision\n",
1367 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1368 1.2.2.2 nathanw }
1369 1.2.2.2 nathanw if (txEvent & TX_EVENT_JABBER) {
1370 1.2.2.2 nathanw printf("%s: jabber\n", sc->sc_dev.dv_xname);
1371 1.2.2.2 nathanw }
1372 1.2.2.2 nathanw if (txEvent & TX_EVENT_16_COLL) {
1373 1.2.2.2 nathanw printf("%s: 16 collisions\n",
1374 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1375 1.2.2.2 nathanw }
1376 1.2.2.2 nathanw }
1377 1.2.2.2 nathanw }
1378 1.2.2.2 nathanw else {
1379 1.2.2.2 nathanw /* Transmission successful, carrier is up. */
1380 1.2.2.2 nathanw sc->sc_carrier = 1;
1381 1.2.2.2 nathanw #ifdef SHARK
1382 1.2.2.2 nathanw ledNetActive();
1383 1.2.2.2 nathanw #endif
1384 1.2.2.2 nathanw }
1385 1.2.2.2 nathanw
1386 1.2.2.2 nathanw /* Add the number of collisions for this frame */
1387 1.2.2.2 nathanw if (txEvent & TX_EVENT_16_COLL) {
1388 1.2.2.2 nathanw ifp->if_collisions += 16;
1389 1.2.2.2 nathanw } else {
1390 1.2.2.2 nathanw ifp->if_collisions += ((txEvent & TX_EVENT_COLL_MASK) >> 11);
1391 1.2.2.2 nathanw }
1392 1.2.2.2 nathanw
1393 1.2.2.2 nathanw ifp->if_opackets++;
1394 1.2.2.2 nathanw
1395 1.2.2.2 nathanw /* Transmission is no longer in progress */
1396 1.2.2.2 nathanw sc->sc_txbusy = FALSE;
1397 1.2.2.2 nathanw
1398 1.2.2.2 nathanw /* If there is more to transmit */
1399 1.2.2.2 nathanw if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
1400 1.2.2.2 nathanw /* Start the next transmission */
1401 1.2.2.2 nathanw cs_start_output(ifp);
1402 1.2.2.2 nathanw }
1403 1.2.2.2 nathanw }
1404 1.2.2.2 nathanw
1405 1.2.2.2 nathanw void
1406 1.2.2.4 nathanw cs_print_rx_errors(struct cs_softc *sc, u_int16_t rxEvent)
1407 1.2.2.2 nathanw {
1408 1.2.2.2 nathanw
1409 1.2.2.2 nathanw if (rxEvent & RX_EVENT_RUNT)
1410 1.2.2.2 nathanw printf("%s: runt\n", sc->sc_dev.dv_xname);
1411 1.2.2.2 nathanw
1412 1.2.2.2 nathanw if (rxEvent & RX_EVENT_X_DATA)
1413 1.2.2.2 nathanw printf("%s: extra data\n", sc->sc_dev.dv_xname);
1414 1.2.2.2 nathanw
1415 1.2.2.2 nathanw if (rxEvent & RX_EVENT_CRC_ERR) {
1416 1.2.2.2 nathanw if (rxEvent & RX_EVENT_DRIBBLE)
1417 1.2.2.2 nathanw printf("%s: alignment error\n", sc->sc_dev.dv_xname);
1418 1.2.2.2 nathanw else
1419 1.2.2.2 nathanw printf("%s: CRC error\n", sc->sc_dev.dv_xname);
1420 1.2.2.2 nathanw } else {
1421 1.2.2.2 nathanw if (rxEvent & RX_EVENT_DRIBBLE)
1422 1.2.2.2 nathanw printf("%s: dribble bits\n", sc->sc_dev.dv_xname);
1423 1.2.2.2 nathanw }
1424 1.2.2.2 nathanw }
1425 1.2.2.2 nathanw
1426 1.2.2.2 nathanw void
1427 1.2.2.4 nathanw cs_receive_event(struct cs_softc *sc, u_int16_t rxEvent)
1428 1.2.2.2 nathanw {
1429 1.2.2.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1430 1.2.2.2 nathanw
1431 1.2.2.2 nathanw /* If the frame was not received OK */
1432 1.2.2.2 nathanw if (!(rxEvent & RX_EVENT_RX_OK)) {
1433 1.2.2.2 nathanw /* Increment the input error count */
1434 1.2.2.2 nathanw ifp->if_ierrors++;
1435 1.2.2.2 nathanw
1436 1.2.2.2 nathanw /*
1437 1.2.2.2 nathanw * If debugging is enabled then log error messages.
1438 1.2.2.2 nathanw */
1439 1.2.2.2 nathanw if (ifp->if_flags & IFF_DEBUG) {
1440 1.2.2.2 nathanw if (rxEvent != REG_NUM_RX_EVENT) {
1441 1.2.2.2 nathanw cs_print_rx_errors(sc, rxEvent);
1442 1.2.2.2 nathanw
1443 1.2.2.2 nathanw /*
1444 1.2.2.2 nathanw * Must read the length of all received
1445 1.2.2.2 nathanw * frames
1446 1.2.2.2 nathanw */
1447 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_RX_LENGTH);
1448 1.2.2.2 nathanw
1449 1.2.2.2 nathanw /* Skip the received frame */
1450 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
1451 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) |
1452 1.2.2.2 nathanw RX_CFG_SKIP);
1453 1.2.2.2 nathanw } else {
1454 1.2.2.2 nathanw printf("%s: implied skip\n",
1455 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1456 1.2.2.2 nathanw }
1457 1.2.2.2 nathanw }
1458 1.2.2.2 nathanw } else {
1459 1.2.2.2 nathanw /*
1460 1.2.2.2 nathanw * process the received frame and pass it up to the upper
1461 1.2.2.2 nathanw * layers.
1462 1.2.2.2 nathanw */
1463 1.2.2.2 nathanw cs_process_receive(sc);
1464 1.2.2.2 nathanw }
1465 1.2.2.2 nathanw }
1466 1.2.2.2 nathanw
1467 1.2.2.2 nathanw void
1468 1.2.2.4 nathanw cs_ether_input(struct cs_softc *sc, struct mbuf *m)
1469 1.2.2.2 nathanw {
1470 1.2.2.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1471 1.2.2.2 nathanw
1472 1.2.2.2 nathanw ifp->if_ipackets++;
1473 1.2.2.2 nathanw
1474 1.2.2.2 nathanw #if NBPFILTER > 0
1475 1.2.2.2 nathanw /*
1476 1.2.2.2 nathanw * Check if there's a BPF listener on this interface.
1477 1.2.2.2 nathanw * If so, hand off the raw packet to BPF.
1478 1.2.2.2 nathanw */
1479 1.2.2.2 nathanw if (ifp->if_bpf)
1480 1.2.2.2 nathanw bpf_mtap(ifp->if_bpf, m);
1481 1.2.2.2 nathanw #endif
1482 1.2.2.2 nathanw
1483 1.2.2.2 nathanw /* Pass the packet up. */
1484 1.2.2.2 nathanw (*ifp->if_input)(ifp, m);
1485 1.2.2.2 nathanw }
1486 1.2.2.2 nathanw
1487 1.2.2.2 nathanw void
1488 1.2.2.4 nathanw cs_process_receive(struct cs_softc *sc)
1489 1.2.2.2 nathanw {
1490 1.2.2.2 nathanw struct ifnet *ifp;
1491 1.2.2.2 nathanw struct mbuf *m;
1492 1.2.2.2 nathanw int totlen;
1493 1.2.2.2 nathanw u_int16_t *pBuff, *pBuffLimit;
1494 1.2.2.2 nathanw int pad;
1495 1.2.2.2 nathanw unsigned int frameOffset;
1496 1.2.2.2 nathanw
1497 1.2.2.2 nathanw #ifdef SHARK
1498 1.2.2.2 nathanw ledNetActive();
1499 1.2.2.2 nathanw #endif
1500 1.2.2.2 nathanw
1501 1.2.2.2 nathanw ifp = &sc->sc_ethercom.ec_if;
1502 1.2.2.2 nathanw
1503 1.2.2.2 nathanw /* Received a packet; carrier is up. */
1504 1.2.2.2 nathanw sc->sc_carrier = 1;
1505 1.2.2.2 nathanw
1506 1.2.2.2 nathanw if (sc->sc_memorymode) {
1507 1.2.2.2 nathanw /* Initialize the frame offset */
1508 1.2.2.2 nathanw frameOffset = PKTPG_RX_LENGTH;
1509 1.2.2.2 nathanw
1510 1.2.2.2 nathanw /* Get the length of the received frame */
1511 1.2.2.2 nathanw totlen = CS_READ_PACKET_PAGE(sc, frameOffset);
1512 1.2.2.2 nathanw frameOffset += 2;
1513 1.2.2.2 nathanw }
1514 1.2.2.2 nathanw else {
1515 1.2.2.2 nathanw /* drop status */
1516 1.2.2.2 nathanw CS_READ_PORT(sc, PORT_RXTX_DATA);
1517 1.2.2.2 nathanw
1518 1.2.2.2 nathanw /* Get the length of the received frame */
1519 1.2.2.2 nathanw totlen = CS_READ_PORT(sc, PORT_RXTX_DATA);
1520 1.2.2.2 nathanw }
1521 1.2.2.2 nathanw
1522 1.2.2.2 nathanw if (totlen > ETHER_MAX_LEN) {
1523 1.2.2.2 nathanw printf("%s: invalid packet length\n", sc->sc_dev.dv_xname);
1524 1.2.2.2 nathanw
1525 1.2.2.2 nathanw /* skip the received frame */
1526 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
1527 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
1528 1.2.2.2 nathanw return;
1529 1.2.2.2 nathanw }
1530 1.2.2.2 nathanw
1531 1.2.2.2 nathanw MGETHDR(m, M_DONTWAIT, MT_DATA);
1532 1.2.2.2 nathanw if (m == 0) {
1533 1.2.2.2 nathanw printf("%s: cs_process_receive: unable to allocate mbuf\n",
1534 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1535 1.2.2.2 nathanw ifp->if_ierrors++;
1536 1.2.2.2 nathanw /*
1537 1.2.2.2 nathanw * couldn't allocate an mbuf so things are not good, may as
1538 1.2.2.2 nathanw * well drop the packet I think.
1539 1.2.2.2 nathanw *
1540 1.2.2.2 nathanw * have already read the length so we should be right to skip
1541 1.2.2.2 nathanw * the packet.
1542 1.2.2.2 nathanw */
1543 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
1544 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
1545 1.2.2.2 nathanw return;
1546 1.2.2.2 nathanw }
1547 1.2.2.2 nathanw m->m_pkthdr.rcvif = ifp;
1548 1.2.2.2 nathanw m->m_pkthdr.len = totlen;
1549 1.2.2.2 nathanw
1550 1.2.2.2 nathanw /* number of bytes to align ip header on word boundary for ipintr */
1551 1.2.2.2 nathanw pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
1552 1.2.2.2 nathanw
1553 1.2.2.2 nathanw /*
1554 1.2.2.2 nathanw * alloc mbuf cluster if we need.
1555 1.2.2.2 nathanw * we need 1 byte spare because following
1556 1.2.2.2 nathanw * packet read loop can overrun.
1557 1.2.2.2 nathanw */
1558 1.2.2.2 nathanw if (totlen + pad + 1 > MHLEN) {
1559 1.2.2.2 nathanw MCLGET(m, M_DONTWAIT);
1560 1.2.2.2 nathanw if ((m->m_flags & M_EXT) == 0) {
1561 1.2.2.2 nathanw /* couldn't allocate an mbuf cluster */
1562 1.2.2.2 nathanw printf("%s: cs_process_receive: unable to allocate a cluster\n",
1563 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1564 1.2.2.2 nathanw m_freem(m);
1565 1.2.2.2 nathanw
1566 1.2.2.2 nathanw /* skip the received frame */
1567 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
1568 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
1569 1.2.2.2 nathanw return;
1570 1.2.2.2 nathanw }
1571 1.2.2.2 nathanw }
1572 1.2.2.2 nathanw
1573 1.2.2.2 nathanw /* align ip header on word boundary for ipintr */
1574 1.2.2.2 nathanw m->m_data += pad;
1575 1.2.2.2 nathanw
1576 1.2.2.2 nathanw m->m_len = totlen;
1577 1.2.2.2 nathanw pBuff = mtod(m, u_int16_t *);
1578 1.2.2.2 nathanw
1579 1.2.2.2 nathanw /* now read the data from the chip */
1580 1.2.2.2 nathanw if (sc->sc_memorymode) {
1581 1.2.2.2 nathanw pBuffLimit = pBuff + (totlen + 1) / 2; /* don't want to go over */
1582 1.2.2.2 nathanw while (pBuff < pBuffLimit) {
1583 1.2.2.2 nathanw *pBuff++ = CS_READ_PACKET_PAGE(sc, frameOffset);
1584 1.2.2.2 nathanw frameOffset += 2;
1585 1.2.2.2 nathanw }
1586 1.2.2.2 nathanw }
1587 1.2.2.2 nathanw else {
1588 1.2.2.4 nathanw IO_READ_MULTI_2(sc, PORT_RXTX_DATA, pBuff, (totlen + 1)>>1);
1589 1.2.2.2 nathanw }
1590 1.2.2.2 nathanw
1591 1.2.2.2 nathanw cs_ether_input(sc, m);
1592 1.2.2.2 nathanw }
1593 1.2.2.2 nathanw
1594 1.2.2.2 nathanw void
1595 1.2.2.4 nathanw cs_process_rx_early(struct cs_softc *sc)
1596 1.2.2.2 nathanw {
1597 1.2.2.2 nathanw struct ifnet *ifp;
1598 1.2.2.2 nathanw struct mbuf *m;
1599 1.2.2.2 nathanw u_int16_t frameCount, oldFrameCount;
1600 1.2.2.2 nathanw u_int16_t rxEvent;
1601 1.2.2.2 nathanw u_int16_t *pBuff;
1602 1.2.2.2 nathanw int pad;
1603 1.2.2.2 nathanw unsigned int frameOffset;
1604 1.2.2.2 nathanw
1605 1.2.2.2 nathanw
1606 1.2.2.2 nathanw ifp = &sc->sc_ethercom.ec_if;
1607 1.2.2.2 nathanw
1608 1.2.2.2 nathanw /* Initialize the frame offset */
1609 1.2.2.2 nathanw frameOffset = PKTPG_RX_FRAME;
1610 1.2.2.2 nathanw frameCount = 0;
1611 1.2.2.2 nathanw
1612 1.2.2.2 nathanw MGETHDR(m, M_DONTWAIT, MT_DATA);
1613 1.2.2.2 nathanw if (m == 0) {
1614 1.2.2.2 nathanw printf("%s: cs_process_rx_early: unable to allocate mbuf\n",
1615 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1616 1.2.2.2 nathanw ifp->if_ierrors++;
1617 1.2.2.2 nathanw /*
1618 1.2.2.2 nathanw * couldn't allocate an mbuf so things are not good, may as
1619 1.2.2.2 nathanw * well drop the packet I think.
1620 1.2.2.2 nathanw *
1621 1.2.2.2 nathanw * have already read the length so we should be right to skip
1622 1.2.2.2 nathanw * the packet.
1623 1.2.2.2 nathanw */
1624 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
1625 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
1626 1.2.2.2 nathanw return;
1627 1.2.2.2 nathanw }
1628 1.2.2.2 nathanw m->m_pkthdr.rcvif = ifp;
1629 1.2.2.2 nathanw /*
1630 1.2.2.2 nathanw * save processing by always using a mbuf cluster, guarenteed to fit
1631 1.2.2.2 nathanw * packet
1632 1.2.2.2 nathanw */
1633 1.2.2.2 nathanw MCLGET(m, M_DONTWAIT);
1634 1.2.2.2 nathanw if ((m->m_flags & M_EXT) == 0) {
1635 1.2.2.2 nathanw /* couldn't allocate an mbuf cluster */
1636 1.2.2.2 nathanw printf("%s: cs_process_rx_early: unable to allocate a cluster\n",
1637 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1638 1.2.2.2 nathanw m_freem(m);
1639 1.2.2.2 nathanw /* skip the frame */
1640 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
1641 1.2.2.2 nathanw CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
1642 1.2.2.2 nathanw return;
1643 1.2.2.2 nathanw }
1644 1.2.2.2 nathanw
1645 1.2.2.2 nathanw /* align ip header on word boundary for ipintr */
1646 1.2.2.2 nathanw pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
1647 1.2.2.2 nathanw m->m_data += pad;
1648 1.2.2.2 nathanw
1649 1.2.2.2 nathanw /* set up the buffer pointer to point to the data area */
1650 1.2.2.2 nathanw pBuff = mtod(m, u_int16_t *);
1651 1.2.2.2 nathanw
1652 1.2.2.2 nathanw /*
1653 1.2.2.2 nathanw * now read the frame byte counter until we have finished reading the
1654 1.2.2.2 nathanw * frame
1655 1.2.2.2 nathanw */
1656 1.2.2.2 nathanw oldFrameCount = 0;
1657 1.2.2.2 nathanw frameCount = CS_READ_PACKET_PAGE(sc, PKTPG_FRAME_BYTE_COUNT);
1658 1.2.2.2 nathanw while ((frameCount != 0) && (frameCount < MCLBYTES)) {
1659 1.2.2.2 nathanw for (; oldFrameCount < frameCount; oldFrameCount += 2) {
1660 1.2.2.2 nathanw *pBuff++ = CS_READ_PACKET_PAGE(sc, frameOffset);
1661 1.2.2.2 nathanw frameOffset += 2;
1662 1.2.2.2 nathanw }
1663 1.2.2.2 nathanw
1664 1.2.2.2 nathanw /* read the new count from the chip */
1665 1.2.2.2 nathanw frameCount = CS_READ_PACKET_PAGE(sc, PKTPG_FRAME_BYTE_COUNT);
1666 1.2.2.2 nathanw }
1667 1.2.2.2 nathanw
1668 1.2.2.2 nathanw /* update the mbuf counts */
1669 1.2.2.2 nathanw m->m_len = oldFrameCount;
1670 1.2.2.2 nathanw m->m_pkthdr.len = oldFrameCount;
1671 1.2.2.2 nathanw
1672 1.2.2.2 nathanw /* now check the Rx Event register */
1673 1.2.2.2 nathanw rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_EVENT);
1674 1.2.2.2 nathanw
1675 1.2.2.2 nathanw if ((rxEvent & RX_EVENT_RX_OK) != 0) {
1676 1.2.2.2 nathanw /*
1677 1.2.2.2 nathanw * do an implied skip, it seems to be more reliable than a
1678 1.2.2.2 nathanw * forced skip.
1679 1.2.2.2 nathanw */
1680 1.2.2.2 nathanw rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_STATUS);
1681 1.2.2.2 nathanw rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_LENGTH);
1682 1.2.2.2 nathanw
1683 1.2.2.2 nathanw /*
1684 1.2.2.2 nathanw * now read the RX_EVENT register to perform an implied skip.
1685 1.2.2.2 nathanw */
1686 1.2.2.2 nathanw rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_EVENT);
1687 1.2.2.2 nathanw
1688 1.2.2.2 nathanw cs_ether_input(sc, m);
1689 1.2.2.2 nathanw } else {
1690 1.2.2.2 nathanw m_freem(m);
1691 1.2.2.2 nathanw ifp->if_ierrors++;
1692 1.2.2.2 nathanw }
1693 1.2.2.2 nathanw }
1694 1.2.2.2 nathanw
1695 1.2.2.2 nathanw void
1696 1.2.2.4 nathanw cs_start_output(struct ifnet *ifp)
1697 1.2.2.2 nathanw {
1698 1.2.2.2 nathanw struct cs_softc *sc;
1699 1.2.2.2 nathanw struct mbuf *pMbuf;
1700 1.2.2.2 nathanw struct mbuf *pMbufChain;
1701 1.2.2.2 nathanw u_int16_t BusStatus;
1702 1.2.2.2 nathanw u_int16_t Length;
1703 1.2.2.2 nathanw int txLoop = 0;
1704 1.2.2.2 nathanw int dropout = 0;
1705 1.2.2.2 nathanw
1706 1.2.2.2 nathanw sc = ifp->if_softc;
1707 1.2.2.2 nathanw
1708 1.2.2.2 nathanw /* check that the interface is up and running */
1709 1.2.2.2 nathanw if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
1710 1.2.2.2 nathanw return;
1711 1.2.2.2 nathanw }
1712 1.2.2.2 nathanw
1713 1.2.2.2 nathanw /* Don't interrupt a transmission in progress */
1714 1.2.2.2 nathanw if (sc->sc_txbusy) {
1715 1.2.2.2 nathanw return;
1716 1.2.2.2 nathanw }
1717 1.2.2.2 nathanw
1718 1.2.2.2 nathanw /* this loop will only run through once if transmission is successful */
1719 1.2.2.2 nathanw /*
1720 1.2.2.2 nathanw * While there are packets to transmit and a transmit is not in
1721 1.2.2.2 nathanw * progress
1722 1.2.2.2 nathanw */
1723 1.2.2.2 nathanw while (sc->sc_txbusy == 0 && dropout == 0) {
1724 1.2.2.2 nathanw IFQ_DEQUEUE(&ifp->if_snd, pMbufChain);
1725 1.2.2.2 nathanw if (pMbufChain == NULL)
1726 1.2.2.2 nathanw break;
1727 1.2.2.2 nathanw
1728 1.2.2.2 nathanw #if NBPFILTER > 0
1729 1.2.2.2 nathanw /*
1730 1.2.2.2 nathanw * If BPF is listening on this interface, let it see the packet
1731 1.2.2.2 nathanw * before we commit it to the wire.
1732 1.2.2.2 nathanw */
1733 1.2.2.2 nathanw if (ifp->if_bpf)
1734 1.2.2.2 nathanw bpf_mtap(ifp->if_bpf, pMbufChain);
1735 1.2.2.2 nathanw #endif
1736 1.2.2.2 nathanw
1737 1.2.2.2 nathanw /* Find the total length of the data to transmit */
1738 1.2.2.2 nathanw Length = 0;
1739 1.2.2.2 nathanw for (pMbuf = pMbufChain; pMbuf != NULL; pMbuf = pMbuf->m_next)
1740 1.2.2.2 nathanw Length += pMbuf->m_len;
1741 1.2.2.2 nathanw
1742 1.2.2.2 nathanw do {
1743 1.2.2.2 nathanw /*
1744 1.2.2.2 nathanw * Request that the transmit be started after all
1745 1.2.2.2 nathanw * data has been copied
1746 1.2.2.2 nathanw *
1747 1.2.2.2 nathanw * In IO mode must write to the IO port not the packet
1748 1.2.2.2 nathanw * page address
1749 1.2.2.2 nathanw *
1750 1.2.2.2 nathanw * If this is changed to start transmission after a
1751 1.2.2.2 nathanw * small amount of data has been copied you tend to
1752 1.2.2.2 nathanw * get packet missed errors i think because the ISA
1753 1.2.2.2 nathanw * bus is too slow. Or possibly the copy routine is
1754 1.2.2.2 nathanw * not streamlined enough.
1755 1.2.2.2 nathanw */
1756 1.2.2.2 nathanw if (sc->sc_memorymode) {
1757 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_CMD,
1758 1.2.2.2 nathanw cs_xmit_early_table[sc->sc_xe_ent].txcmd);
1759 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_LENGTH, Length);
1760 1.2.2.2 nathanw }
1761 1.2.2.2 nathanw else {
1762 1.2.2.2 nathanw CS_WRITE_PORT(sc, PORT_TX_CMD,
1763 1.2.2.2 nathanw cs_xmit_early_table[sc->sc_xe_ent].txcmd);
1764 1.2.2.2 nathanw CS_WRITE_PORT(sc, PORT_TX_LENGTH, Length);
1765 1.2.2.2 nathanw }
1766 1.2.2.2 nathanw
1767 1.2.2.2 nathanw /*
1768 1.2.2.2 nathanw * Adjust early-transmit machinery.
1769 1.2.2.2 nathanw */
1770 1.2.2.2 nathanw if (--sc->sc_xe_togo == 0) {
1771 1.2.2.2 nathanw sc->sc_xe_ent =
1772 1.2.2.2 nathanw cs_xmit_early_table[sc->sc_xe_ent].better;
1773 1.2.2.2 nathanw sc->sc_xe_togo =
1774 1.2.2.2 nathanw cs_xmit_early_table[sc->sc_xe_ent].better_count;
1775 1.2.2.2 nathanw }
1776 1.2.2.2 nathanw /*
1777 1.2.2.2 nathanw * Read the BusStatus register which indicates
1778 1.2.2.2 nathanw * success of the request
1779 1.2.2.2 nathanw */
1780 1.2.2.2 nathanw BusStatus = CS_READ_PACKET_PAGE(sc, PKTPG_BUS_ST);
1781 1.2.2.2 nathanw
1782 1.2.2.2 nathanw /*
1783 1.2.2.2 nathanw * If there was an error in the transmit bid free the
1784 1.2.2.2 nathanw * mbuf and go on. This is presuming that mbuf is
1785 1.2.2.2 nathanw * corrupt.
1786 1.2.2.2 nathanw */
1787 1.2.2.2 nathanw if (BusStatus & BUS_ST_TX_BID_ERR) {
1788 1.2.2.2 nathanw printf("%s: transmit bid error (too big)",
1789 1.2.2.2 nathanw sc->sc_dev.dv_xname);
1790 1.2.2.2 nathanw
1791 1.2.2.2 nathanw /* Discard the bad mbuf chain */
1792 1.2.2.2 nathanw m_freem(pMbufChain);
1793 1.2.2.2 nathanw sc->sc_ethercom.ec_if.if_oerrors++;
1794 1.2.2.2 nathanw
1795 1.2.2.2 nathanw /* Loop up to transmit the next chain */
1796 1.2.2.2 nathanw txLoop = 0;
1797 1.2.2.2 nathanw } else {
1798 1.2.2.2 nathanw if (BusStatus & BUS_ST_RDY4TXNOW) {
1799 1.2.2.2 nathanw /*
1800 1.2.2.2 nathanw * The chip is ready for transmission
1801 1.2.2.2 nathanw * now
1802 1.2.2.2 nathanw */
1803 1.2.2.2 nathanw /*
1804 1.2.2.2 nathanw * Copy the frame to the chip to
1805 1.2.2.2 nathanw * start transmission
1806 1.2.2.2 nathanw */
1807 1.2.2.2 nathanw cs_copy_tx_frame(sc, pMbufChain);
1808 1.2.2.2 nathanw
1809 1.2.2.2 nathanw /* Free the mbuf chain */
1810 1.2.2.2 nathanw m_freem(pMbufChain);
1811 1.2.2.2 nathanw
1812 1.2.2.2 nathanw /* Transmission is now in progress */
1813 1.2.2.2 nathanw sc->sc_txbusy = TRUE;
1814 1.2.2.2 nathanw txLoop = 0;
1815 1.2.2.2 nathanw } else {
1816 1.2.2.2 nathanw /*
1817 1.2.2.2 nathanw * if we get here we want to try
1818 1.2.2.2 nathanw * again with the same mbuf, until
1819 1.2.2.2 nathanw * the chip lets us transmit.
1820 1.2.2.2 nathanw */
1821 1.2.2.2 nathanw txLoop++;
1822 1.2.2.2 nathanw if (txLoop > CS_OUTPUT_LOOP_MAX) {
1823 1.2.2.2 nathanw /* Free the mbuf chain */
1824 1.2.2.2 nathanw m_freem(pMbufChain);
1825 1.2.2.2 nathanw /*
1826 1.2.2.2 nathanw * Transmission is not in
1827 1.2.2.2 nathanw * progress
1828 1.2.2.2 nathanw */
1829 1.2.2.2 nathanw sc->sc_txbusy = FALSE;
1830 1.2.2.2 nathanw /*
1831 1.2.2.2 nathanw * Increment the output error
1832 1.2.2.2 nathanw * count
1833 1.2.2.2 nathanw */
1834 1.2.2.2 nathanw ifp->if_oerrors++;
1835 1.2.2.2 nathanw /*
1836 1.2.2.2 nathanw * exit the routine and drop
1837 1.2.2.2 nathanw * the packet.
1838 1.2.2.2 nathanw */
1839 1.2.2.2 nathanw txLoop = 0;
1840 1.2.2.2 nathanw dropout = 1;
1841 1.2.2.2 nathanw }
1842 1.2.2.2 nathanw }
1843 1.2.2.2 nathanw }
1844 1.2.2.2 nathanw } while (txLoop);
1845 1.2.2.2 nathanw }
1846 1.2.2.2 nathanw }
1847 1.2.2.2 nathanw
1848 1.2.2.2 nathanw void
1849 1.2.2.4 nathanw cs_copy_tx_frame(struct cs_softc *sc, struct mbuf *m0)
1850 1.2.2.2 nathanw {
1851 1.2.2.2 nathanw struct mbuf *m;
1852 1.2.2.2 nathanw int len, leftover, frameoff;
1853 1.2.2.2 nathanw u_int16_t dbuf;
1854 1.2.2.2 nathanw u_int8_t *p;
1855 1.2.2.2 nathanw #ifdef DIAGNOSTIC
1856 1.2.2.2 nathanw u_int8_t *lim;
1857 1.2.2.2 nathanw #endif
1858 1.2.2.2 nathanw
1859 1.2.2.2 nathanw /* Initialize frame pointer and data port address */
1860 1.2.2.2 nathanw frameoff = PKTPG_TX_FRAME;
1861 1.2.2.2 nathanw
1862 1.2.2.2 nathanw /* start out with no leftover data */
1863 1.2.2.2 nathanw leftover = 0;
1864 1.2.2.2 nathanw dbuf = 0;
1865 1.2.2.2 nathanw
1866 1.2.2.2 nathanw /* Process the chain of mbufs */
1867 1.2.2.2 nathanw for (m = m0; m != NULL; m = m->m_next) {
1868 1.2.2.2 nathanw /*
1869 1.2.2.2 nathanw * Process all of the data in a single mbuf.
1870 1.2.2.2 nathanw */
1871 1.2.2.2 nathanw p = mtod(m, u_int8_t *);
1872 1.2.2.2 nathanw len = m->m_len;
1873 1.2.2.2 nathanw #ifdef DIAGNOSTIC
1874 1.2.2.2 nathanw lim = p + len;
1875 1.2.2.2 nathanw #endif
1876 1.2.2.2 nathanw
1877 1.2.2.2 nathanw while (len > 0) {
1878 1.2.2.2 nathanw if (leftover) {
1879 1.2.2.2 nathanw /*
1880 1.2.2.2 nathanw * Data left over (from mbuf or realignment).
1881 1.2.2.2 nathanw * Buffer the next byte, and write it and
1882 1.2.2.2 nathanw * the leftover data out.
1883 1.2.2.2 nathanw */
1884 1.2.2.2 nathanw dbuf |= *p++ << 8;
1885 1.2.2.2 nathanw len--;
1886 1.2.2.2 nathanw if (sc->sc_memorymode) {
1887 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, frameoff, dbuf);
1888 1.2.2.2 nathanw frameoff += 2;
1889 1.2.2.2 nathanw }
1890 1.2.2.2 nathanw else {
1891 1.2.2.2 nathanw CS_WRITE_PORT(sc, PORT_RXTX_DATA, dbuf);
1892 1.2.2.2 nathanw }
1893 1.2.2.2 nathanw leftover = 0;
1894 1.2.2.2 nathanw } else if ((long) p & 1) {
1895 1.2.2.2 nathanw /*
1896 1.2.2.2 nathanw * Misaligned data. Buffer the next byte.
1897 1.2.2.2 nathanw */
1898 1.2.2.2 nathanw dbuf = *p++;
1899 1.2.2.2 nathanw len--;
1900 1.2.2.2 nathanw leftover = 1;
1901 1.2.2.2 nathanw } else {
1902 1.2.2.2 nathanw /*
1903 1.2.2.2 nathanw * Aligned data. This is the case we like.
1904 1.2.2.2 nathanw *
1905 1.2.2.2 nathanw * Write-region out as much as we can, then
1906 1.2.2.2 nathanw * buffer the remaining byte (if any).
1907 1.2.2.2 nathanw */
1908 1.2.2.2 nathanw leftover = len & 1;
1909 1.2.2.2 nathanw len &= ~1;
1910 1.2.2.2 nathanw if (sc->sc_memorymode) {
1911 1.2.2.4 nathanw MEM_WRITE_REGION_2(sc, frameoff,
1912 1.2.2.2 nathanw (u_int16_t *) p, len >> 1);
1913 1.2.2.2 nathanw frameoff += len;
1914 1.2.2.2 nathanw }
1915 1.2.2.2 nathanw else {
1916 1.2.2.4 nathanw IO_WRITE_MULTI_2(sc,
1917 1.2.2.2 nathanw PORT_RXTX_DATA, (u_int16_t *)p, len >> 1);
1918 1.2.2.2 nathanw }
1919 1.2.2.2 nathanw p += len;
1920 1.2.2.2 nathanw
1921 1.2.2.2 nathanw if (leftover)
1922 1.2.2.2 nathanw dbuf = *p++;
1923 1.2.2.2 nathanw len = 0;
1924 1.2.2.2 nathanw }
1925 1.2.2.2 nathanw }
1926 1.2.2.2 nathanw if (len < 0)
1927 1.2.2.2 nathanw panic("cs_copy_tx_frame: negative len");
1928 1.2.2.2 nathanw #ifdef DIAGNOSTIC
1929 1.2.2.2 nathanw if (p != lim)
1930 1.2.2.2 nathanw panic("cs_copy_tx_frame: p != lim");
1931 1.2.2.2 nathanw #endif
1932 1.2.2.2 nathanw }
1933 1.2.2.2 nathanw if (leftover) {
1934 1.2.2.2 nathanw if (sc->sc_memorymode) {
1935 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, frameoff, dbuf);
1936 1.2.2.2 nathanw }
1937 1.2.2.2 nathanw else {
1938 1.2.2.2 nathanw CS_WRITE_PORT(sc, PORT_RXTX_DATA, dbuf);
1939 1.2.2.2 nathanw }
1940 1.2.2.2 nathanw }
1941 1.2.2.2 nathanw }
1942 1.2.2.2 nathanw
1943 1.2.2.2 nathanw static int
1944 1.2.2.4 nathanw cs_enable(struct cs_softc *sc)
1945 1.2.2.2 nathanw {
1946 1.2.2.2 nathanw
1947 1.2.2.4 nathanw if (CS_IS_ENABLED(sc) == 0) {
1948 1.2.2.4 nathanw if (sc->sc_enable != NULL) {
1949 1.2.2.4 nathanw int error;
1950 1.2.2.2 nathanw
1951 1.2.2.4 nathanw error = (*sc->sc_enable)(sc);
1952 1.2.2.4 nathanw if (error)
1953 1.2.2.4 nathanw return (error);
1954 1.2.2.4 nathanw }
1955 1.2.2.2 nathanw sc->sc_cfgflags |= CFGFLG_ENABLED;
1956 1.2.2.2 nathanw }
1957 1.2.2.2 nathanw
1958 1.2.2.4 nathanw return (0);
1959 1.2.2.2 nathanw }
1960 1.2.2.2 nathanw
1961 1.2.2.2 nathanw static void
1962 1.2.2.4 nathanw cs_disable(struct cs_softc *sc)
1963 1.2.2.2 nathanw {
1964 1.2.2.4 nathanw
1965 1.2.2.4 nathanw if (CS_IS_ENABLED(sc)) {
1966 1.2.2.4 nathanw if (sc->sc_disable != NULL)
1967 1.2.2.4 nathanw (*sc->sc_disable)(sc);
1968 1.2.2.2 nathanw
1969 1.2.2.2 nathanw sc->sc_cfgflags &= ~CFGFLG_ENABLED;
1970 1.2.2.2 nathanw }
1971 1.2.2.2 nathanw }
1972 1.2.2.2 nathanw
1973 1.2.2.2 nathanw static void
1974 1.2.2.4 nathanw cs_stop(struct ifnet *ifp, int disable)
1975 1.2.2.2 nathanw {
1976 1.2.2.2 nathanw struct cs_softc *sc = ifp->if_softc;
1977 1.2.2.2 nathanw
1978 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG, 0);
1979 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_CFG, 0);
1980 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_BUF_CFG, 0);
1981 1.2.2.2 nathanw CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL, 0);
1982 1.2.2.2 nathanw
1983 1.2.2.2 nathanw if (disable) {
1984 1.2.2.2 nathanw cs_disable(sc);
1985 1.2.2.2 nathanw }
1986 1.2.2.2 nathanw
1987 1.2.2.2 nathanw ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1988 1.2.2.2 nathanw }
1989 1.2.2.2 nathanw
1990 1.2.2.2 nathanw int
1991 1.2.2.4 nathanw cs_activate(struct device *self, enum devact act)
1992 1.2.2.2 nathanw {
1993 1.2.2.2 nathanw struct cs_softc *sc = (void *)self;
1994 1.2.2.2 nathanw int s, error = 0;
1995 1.2.2.2 nathanw
1996 1.2.2.2 nathanw s = splnet();
1997 1.2.2.2 nathanw switch (act) {
1998 1.2.2.2 nathanw case DVACT_ACTIVATE:
1999 1.2.2.2 nathanw error = EOPNOTSUPP;
2000 1.2.2.2 nathanw break;
2001 1.2.2.2 nathanw
2002 1.2.2.2 nathanw case DVACT_DEACTIVATE:
2003 1.2.2.2 nathanw if_deactivate(&sc->sc_ethercom.ec_if);
2004 1.2.2.2 nathanw break;
2005 1.2.2.2 nathanw }
2006 1.2.2.2 nathanw splx(s);
2007 1.2.2.2 nathanw
2008 1.2.2.2 nathanw return error;
2009 1.2.2.2 nathanw }
2010 1.2.2.2 nathanw
2011 1.2.2.2 nathanw static void
2012 1.2.2.4 nathanw cs_power(int why, void *arg)
2013 1.2.2.2 nathanw {
2014 1.2.2.2 nathanw struct cs_softc *sc = arg;
2015 1.2.2.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2016 1.2.2.2 nathanw int s;
2017 1.2.2.2 nathanw
2018 1.2.2.2 nathanw s = splnet();
2019 1.2.2.2 nathanw switch (why) {
2020 1.2.2.2 nathanw case PWR_STANDBY:
2021 1.2.2.2 nathanw case PWR_SUSPEND:
2022 1.2.2.2 nathanw cs_stop(ifp, 0);
2023 1.2.2.2 nathanw break;
2024 1.2.2.2 nathanw case PWR_RESUME:
2025 1.2.2.2 nathanw if (ifp->if_flags & IFF_UP) {
2026 1.2.2.2 nathanw cs_init(ifp);
2027 1.2.2.2 nathanw }
2028 1.2.2.2 nathanw break;
2029 1.2.2.2 nathanw case PWR_SOFTSUSPEND:
2030 1.2.2.2 nathanw case PWR_SOFTSTANDBY:
2031 1.2.2.2 nathanw case PWR_SOFTRESUME:
2032 1.2.2.2 nathanw break;
2033 1.2.2.2 nathanw }
2034 1.2.2.2 nathanw splx(s);
2035 1.2.2.2 nathanw }
2036