Thu Dec 5 05:28:09 2019 UTC ()
 Remove SIOC[SIFMEDIA because ifmedia_ioctl() does the same thing.


(msaitoh)
diff -r1.20 -r1.21 src/sys/arch/emips/ebus/if_le_ebus.c

cvs diff -r1.20 -r1.21 src/sys/arch/emips/ebus/if_le_ebus.c (switch to unified diff)

--- src/sys/arch/emips/ebus/if_le_ebus.c 2019/05/29 10:07:28 1.20
+++ src/sys/arch/emips/ebus/if_le_ebus.c 2019/12/05 05:28:09 1.21
@@ -1,995 +1,964 @@ @@ -1,995 +1,964 @@
1/* $NetBSD: if_le_ebus.c,v 1.20 2019/05/29 10:07:28 msaitoh Exp $ */ 1/* $NetBSD: if_le_ebus.c,v 1.21 2019/12/05 05:28:09 msaitoh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc. 4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code was written by Alessandro Forin and Neil Pittman 7 * This code was written by Alessandro Forin and Neil Pittman
8 * at Microsoft Research and contributed to The NetBSD Foundation 8 * at Microsoft Research and contributed to The NetBSD Foundation
9 * by Microsoft Corporation. 9 * by Microsoft Corporation.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright 16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the 17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution. 18 * documentation and/or other materials provided with the distribution.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: if_le_ebus.c,v 1.20 2019/05/29 10:07:28 msaitoh Exp $"); 34__KERNEL_RCSID(0, "$NetBSD: if_le_ebus.c,v 1.21 2019/12/05 05:28:09 msaitoh Exp $");
35 35
36#include "opt_inet.h" 36#include "opt_inet.h"
37 37
38 38
39 39
40#include <sys/param.h> 40#include <sys/param.h>
41#include <sys/systm.h> 41#include <sys/systm.h>
42#include <sys/mbuf.h> 42#include <sys/mbuf.h>
43#include <sys/syslog.h> 43#include <sys/syslog.h>
44#include <sys/socket.h> 44#include <sys/socket.h>
45#include <sys/device.h> 45#include <sys/device.h>
46#include <sys/malloc.h> 46#include <sys/malloc.h>
47#include <sys/ioctl.h> 47#include <sys/ioctl.h>
48#include <sys/errno.h> 48#include <sys/errno.h>
49 49
50#include <net/if.h> 50#include <net/if.h>
51#include <net/if_dl.h> 51#include <net/if_dl.h>
52#include <net/if_ether.h> 52#include <net/if_ether.h>
53#include <net/if_media.h> 53#include <net/if_media.h>
54#include <net/bpf.h> 54#include <net/bpf.h>
55 55
56#ifdef INET 56#ifdef INET
57#include <netinet/in.h> 57#include <netinet/in.h>
58#include <netinet/if_inarp.h> 58#include <netinet/if_inarp.h>
59#endif 59#endif
60 60
61#include <sys/rndsource.h> 61#include <sys/rndsource.h>
62 62
63#include <emips/ebus/ebusvar.h> 63#include <emips/ebus/ebusvar.h>
64#include <emips/emips/machdep.h> 64#include <emips/emips/machdep.h>
65#include <machine/emipsreg.h> 65#include <machine/emipsreg.h>
66 66
67extern paddr_t kvtophys(vaddr_t); 67extern paddr_t kvtophys(vaddr_t);
68 68
69struct bufmap { 69struct bufmap {
70 struct mbuf *mbuf; 70 struct mbuf *mbuf;
71 paddr_t phys; 71 paddr_t phys;
72}; 72};
73 73
74struct enic_softc { 74struct enic_softc {
75 device_t sc_dev; /* base device glue */ 75 device_t sc_dev; /* base device glue */
76 struct ethercom sc_ethercom; /* Ethernet common part */ 76 struct ethercom sc_ethercom; /* Ethernet common part */
77 struct ifmedia sc_media; /* our supported media */ 77 struct ifmedia sc_media; /* our supported media */
78 78
79 struct _Enic *sc_regs; /* hw registers */ 79 struct _Enic *sc_regs; /* hw registers */
80 80
81 int sc_havecarrier; /* carrier status */ 81 int sc_havecarrier; /* carrier status */
82 void *sc_sh; /* shutdownhook cookie */ 82 void *sc_sh; /* shutdownhook cookie */
83 int inited; 83 int inited;
84 84
85 int sc_no_rd; 85 int sc_no_rd;
86 int sc_n_recv; 86 int sc_n_recv;
87 int sc_recv_h; 87 int sc_recv_h;
88 /* BUGBUG really should be malloc-ed */ 88 /* BUGBUG really should be malloc-ed */
89#define SC_MAX_N_RECV 64 89#define SC_MAX_N_RECV 64
90 struct bufmap sc_recv[SC_MAX_N_RECV]; 90 struct bufmap sc_recv[SC_MAX_N_RECV];
91 91
92 int sc_no_td; 92 int sc_no_td;
93 int sc_n_xmit; 93 int sc_n_xmit;
94 int sc_xmit_h; 94 int sc_xmit_h;
95 /* BUGBUG really should be malloc-ed */ 95 /* BUGBUG really should be malloc-ed */
96#define SC_MAX_N_XMIT 16 96#define SC_MAX_N_XMIT 16
97 struct bufmap sc_xmit[SC_MAX_N_XMIT]; 97 struct bufmap sc_xmit[SC_MAX_N_XMIT];
98 98
99#if DEBUG 99#if DEBUG
100 int xhit; 100 int xhit;
101 int xmiss; 101 int xmiss;
102 int tfull; 102 int tfull;
103 int tfull2; 103 int tfull2;
104 int brh; 104 int brh;
105 int rf; 105 int rf;
106 int bxh; 106 int bxh;
107 107
108 int it; 108 int it;
109#endif 109#endif
110 110
111 uint8_t sc_enaddr[ETHER_ADDR_LEN]; 111 uint8_t sc_enaddr[ETHER_ADDR_LEN];
112 uint8_t sc_pad[2]; 112 uint8_t sc_pad[2];
113 krndsource_t rnd_source; 113 krndsource_t rnd_source;
114}; 114};
115 115
116void enic_reset(struct ifnet *); 116void enic_reset(struct ifnet *);
117int enic_init(struct ifnet *); 117int enic_init(struct ifnet *);
118void enic_stop(struct ifnet *, int); 118void enic_stop(struct ifnet *, int);
119void enic_start(struct ifnet *); 119void enic_start(struct ifnet *);
120void enic_shutdown(void *); 120void enic_shutdown(void *);
121void enic_watchdog(struct ifnet *); 121void enic_watchdog(struct ifnet *);
122int enic_mediachange(struct ifnet *); 122int enic_mediachange(struct ifnet *);
123void enic_mediastatus(struct ifnet *, struct ifmediareq *); 123void enic_mediastatus(struct ifnet *, struct ifmediareq *);
124int enic_ioctl(struct ifnet *, u_long, void *); 124int enic_ioctl(struct ifnet *, u_long, void *);
125int enic_intr(void *, void *); 125int enic_intr(void *, void *);
126void enic_rint(struct enic_softc *, uint32_t, paddr_t); 126void enic_rint(struct enic_softc *, uint32_t, paddr_t);
127void enic_tint(struct enic_softc *, uint32_t, paddr_t); 127void enic_tint(struct enic_softc *, uint32_t, paddr_t);
128void enic_kill_xmit(struct enic_softc *); 128void enic_kill_xmit(struct enic_softc *);
129void enic_post_recv(struct enic_softc *, struct mbuf *); 129void enic_post_recv(struct enic_softc *, struct mbuf *);
130void enic_refill(struct enic_softc *); 130void enic_refill(struct enic_softc *);
131static int enic_gethwinfo(struct enic_softc *); 131static int enic_gethwinfo(struct enic_softc *);
132int enic_put(struct enic_softc *, struct mbuf **); 132int enic_put(struct enic_softc *, struct mbuf **);
133 133
134static int enic_match(device_t, cfdata_t, void *); 134static int enic_match(device_t, cfdata_t, void *);
135static void enic_attach(device_t, device_t, void *); 135static void enic_attach(device_t, device_t, void *);
136 136
137CFATTACH_DECL_NEW(enic_emips, sizeof(struct enic_softc), 137CFATTACH_DECL_NEW(enic_emips, sizeof(struct enic_softc),
138 enic_match, enic_attach, NULL, NULL); 138 enic_match, enic_attach, NULL, NULL);
139 139
140int 140int
141enic_match(device_t parent, cfdata_t cf, void *aux) 141enic_match(device_t parent, cfdata_t cf, void *aux)
142{ 142{
143 struct ebus_attach_args *d = aux; 143 struct ebus_attach_args *d = aux;
144 /* donno yet */ 144 /* donno yet */
145 struct _Enic *et = (struct _Enic *)d->ia_vaddr; 145 struct _Enic *et = (struct _Enic *)d->ia_vaddr;
146 146
147 if (strcmp("enic", d->ia_name) != 0) 147 if (strcmp("enic", d->ia_name) != 0)
148 return 0; 148 return 0;
149 if ((et == NULL) || (et->Tag != PMTTAG_ETHERNET)) 149 if ((et == NULL) || (et->Tag != PMTTAG_ETHERNET))
150 return 0; 150 return 0;
151 return 1; 151 return 1;
152} 152}
153 153
154void 154void
155enic_attach(device_t parent, device_t self, void *aux) 155enic_attach(device_t parent, device_t self, void *aux)
156{ 156{
157 struct enic_softc *sc = device_private(self); 157 struct enic_softc *sc = device_private(self);
158 struct ebus_attach_args *ia = aux; 158 struct ebus_attach_args *ia = aux;
159 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 159 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
160 160
161 sc->sc_regs = (struct _Enic *)(ia->ia_vaddr); 161 sc->sc_regs = (struct _Enic *)(ia->ia_vaddr);
162#if DEBUG 162#if DEBUG
163 printf(" virt=%p ", (void *)sc->sc_regs); 163 printf(" virt=%p ", (void *)sc->sc_regs);
164#endif 164#endif
165 165
166 /* Get the MAC and the depth of the FIFOs */ 166 /* Get the MAC and the depth of the FIFOs */
167 if (!enic_gethwinfo(sc)) { 167 if (!enic_gethwinfo(sc)) {
168 printf(" <cannot get hw info> DISABLED.\n"); 168 printf(" <cannot get hw info> DISABLED.\n");
169 /* 169 /*
170 * NB: caveat maintainer: make sure what we 170 * NB: caveat maintainer: make sure what we
171 * did NOT do below does not hurt the system 171 * did NOT do below does not hurt the system
172 */ 172 */
173 return; 173 return;
174 } 174 }
175 175
176 sc->sc_dev = self; 176 sc->sc_dev = self;
177 sc->sc_no_td = 0; 177 sc->sc_no_td = 0;
178 sc->sc_havecarrier = 1; /* BUGBUG */ 178 sc->sc_havecarrier = 1; /* BUGBUG */
179 sc->sc_recv_h = 0; 179 sc->sc_recv_h = 0;
180 sc->sc_xmit_h = 0; 180 sc->sc_xmit_h = 0;
181 /* uhmm do I need to do this? */ 181 /* uhmm do I need to do this? */
182 memset(sc->sc_recv, 0, sizeof sc->sc_recv); 182 memset(sc->sc_recv, 0, sizeof sc->sc_recv);
183 memset(sc->sc_xmit, 0, sizeof sc->sc_xmit); 183 memset(sc->sc_xmit, 0, sizeof sc->sc_xmit);
184 184
185 /* Initialize ifnet structure. */ 185 /* Initialize ifnet structure. */
186 strcpy(ifp->if_xname, device_xname(sc->sc_dev)); 186 strcpy(ifp->if_xname, device_xname(sc->sc_dev));
187 ifp->if_softc = sc; 187 ifp->if_softc = sc;
188 ifp->if_start = enic_start; 188 ifp->if_start = enic_start;
189 ifp->if_ioctl = enic_ioctl; 189 ifp->if_ioctl = enic_ioctl;
190 ifp->if_watchdog = enic_watchdog; 190 ifp->if_watchdog = enic_watchdog;
191 ifp->if_init = enic_init; 191 ifp->if_init = enic_init;
192 ifp->if_stop = enic_stop; 192 ifp->if_stop = enic_stop;
193 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 193 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
194 IFQ_SET_READY(&ifp->if_snd); 194 IFQ_SET_READY(&ifp->if_snd);
195 195
196 /* Initialize ifmedia structures. */ 196 /* Initialize ifmedia structures. */
197 sc->sc_ethercom.ec_ifmedia = &sc->sc_media; 197 sc->sc_ethercom.ec_ifmedia = &sc->sc_media;
198 ifmedia_init(&sc->sc_media, 0, enic_mediachange, enic_mediastatus); 198 ifmedia_init(&sc->sc_media, 0, enic_mediachange, enic_mediastatus);
199 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL); 199 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
200 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL); 200 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);
201 201
202 /* Make sure the chip is stopped. */ 202 /* Make sure the chip is stopped. */
203 enic_stop(ifp, 0); 203 enic_stop(ifp, 0);
204 sc->inited = 0; 204 sc->inited = 0;
205 205
206 /* Get the mac address and print it */ 206 /* Get the mac address and print it */
207 printf(": eNIC [%d %d], address %s\n", 207 printf(": eNIC [%d %d], address %s\n",
208 sc->sc_n_recv, sc->sc_n_xmit, ether_sprintf(sc->sc_enaddr)); 208 sc->sc_n_recv, sc->sc_n_xmit, ether_sprintf(sc->sc_enaddr));
209 209
210 /* claim 802.1q capability */ 210 /* claim 802.1q capability */
211#if 0 211#if 0
212 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU; 212 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
213#endif 213#endif
214 214
215 /* Attach the interface. */ 215 /* Attach the interface. */
216 if_attach(ifp); 216 if_attach(ifp);
217 if_deferred_start_init(ifp, NULL); 217 if_deferred_start_init(ifp, NULL);
218 ether_ifattach(ifp, sc->sc_enaddr); 218 ether_ifattach(ifp, sc->sc_enaddr);
219 219
220 sc->sc_sh = shutdownhook_establish(enic_shutdown, ifp); 220 sc->sc_sh = shutdownhook_establish(enic_shutdown, ifp);
221 if (sc->sc_sh == NULL) 221 if (sc->sc_sh == NULL)
222 panic("enic_attach: cannot establish shutdown hook"); 222 panic("enic_attach: cannot establish shutdown hook");
223 223
224 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev), 224 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
225 RND_TYPE_NET, RND_FLAG_DEFAULT); 225 RND_TYPE_NET, RND_FLAG_DEFAULT);
226 226
227 ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_NET, 227 ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_NET,
228 enic_intr, sc); 228 enic_intr, sc);
229} 229}
230 230
231/* 231/*
232 * Beware: does not work while the nic is running 232 * Beware: does not work while the nic is running
233 */ 233 */
234static int enic_gethwinfo(struct enic_softc *sc) 234static int enic_gethwinfo(struct enic_softc *sc)
235{ 235{
236 uint8_t buffer[8]; /* 64bits max */ 236 uint8_t buffer[8]; /* 64bits max */
237 PENIC_INFO hw = (PENIC_INFO)buffer; 237 PENIC_INFO hw = (PENIC_INFO)buffer;
238 paddr_t phys = kvtophys((vaddr_t)&buffer[0]), phys2; 238 paddr_t phys = kvtophys((vaddr_t)&buffer[0]), phys2;
239 int i; 239 int i;
240 240
241 /* 241 /*
242 * First thing first, get the MAC address 242 * First thing first, get the MAC address
243 */ 243 */
244 memset(buffer, 0, sizeof buffer); 244 memset(buffer, 0, sizeof buffer);
245 buffer[0] = ENIC_CMD_GET_ADDRESS; 245 buffer[0] = ENIC_CMD_GET_ADDRESS;
246 buffer[3] = ENIC_CMD_GET_ADDRESS; /* bswap bug */ 246 buffer[3] = ENIC_CMD_GET_ADDRESS; /* bswap bug */
247 sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD; 247 sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
248 sc->sc_regs->BufferAddressHi32 = 0; 248 sc->sc_regs->BufferAddressHi32 = 0;
249 sc->sc_regs->BufferAddressLo32 = phys; /* go! */ 249 sc->sc_regs->BufferAddressLo32 = phys; /* go! */
250 250
251 for (i = 0; i < 100; i++) { 251 for (i = 0; i < 100; i++) {
252 DELAY(100); 252 DELAY(100);
253 if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0) 253 if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
254 break; 254 break;
255 } 255 }
256 if (i == 100) 256 if (i == 100)
257 return 0; 257 return 0;
258 258
259 phys2 = sc->sc_regs->BufferAddressLo32; 259 phys2 = sc->sc_regs->BufferAddressLo32;
260 if (phys2 != phys) { 260 if (phys2 != phys) {
261 printf("enic uhu? %llx != %llx?\n", 261 printf("enic uhu? %llx != %llx?\n",
262 (long long)phys, (long long)phys2); 262 (long long)phys, (long long)phys2);
263 return 0; 263 return 0;
264 } 264 }
265 memcpy(sc->sc_enaddr, buffer, ETHER_ADDR_LEN); 265 memcpy(sc->sc_enaddr, buffer, ETHER_ADDR_LEN);
266 266
267 /* 267 /*
268 * Next get the HW parameters 268 * Next get the HW parameters
269 */ 269 */
270 memset(buffer, 0, sizeof buffer); 270 memset(buffer, 0, sizeof buffer);
271 buffer[0] = ENIC_CMD_GET_INFO; 271 buffer[0] = ENIC_CMD_GET_INFO;
272 buffer[3] = ENIC_CMD_GET_INFO; /* bswap bug */ 272 buffer[3] = ENIC_CMD_GET_INFO; /* bswap bug */
273 sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD; 273 sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
274 sc->sc_regs->BufferAddressHi32 = 0; 274 sc->sc_regs->BufferAddressHi32 = 0;
275 sc->sc_regs->BufferAddressLo32 = phys; /* go! */ 275 sc->sc_regs->BufferAddressLo32 = phys; /* go! */
276 276
277 for (i = 0; i < 100; i++) { 277 for (i = 0; i < 100; i++) {
278 DELAY(100); 278 DELAY(100);
279 if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0) 279 if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
280 break; 280 break;
281 } 281 }
282 if (i == 100) 282 if (i == 100)
283 return 0; 283 return 0;
284 284
285 phys2 = sc->sc_regs->BufferAddressLo32; 285 phys2 = sc->sc_regs->BufferAddressLo32;
286 if (phys2 != phys) { 286 if (phys2 != phys) {
287 printf("enic uhu2? %llx != %llx?\n", 287 printf("enic uhu2? %llx != %llx?\n",
288 (long long)phys, (long long)phys2); 288 (long long)phys, (long long)phys2);
289 return 0; 289 return 0;
290 } 290 }
291#if 0 291#if 0
292 printf("enic: hwinfo: %x %x %x %x %x %x \n", 292 printf("enic: hwinfo: %x %x %x %x %x %x \n",
293 hw->InputFifoSize, hw->OutputFifoSize, hw->CompletionFifoSize, 293 hw->InputFifoSize, hw->OutputFifoSize, hw->CompletionFifoSize,
294 hw->ErrorCount, hw->FramesDropped, hw->Reserved); 294 hw->ErrorCount, hw->FramesDropped, hw->Reserved);
295#endif 295#endif
296 296
297 /* 297 /*
298 * Get FIFO depths and cap them 298 * Get FIFO depths and cap them
299 */ 299 */
300 sc->sc_n_recv = hw->InputFifoSize; 300 sc->sc_n_recv = hw->InputFifoSize;
301 if (sc->sc_n_recv > SC_MAX_N_RECV) 301 if (sc->sc_n_recv > SC_MAX_N_RECV)
302 sc->sc_n_recv = SC_MAX_N_RECV; 302 sc->sc_n_recv = SC_MAX_N_RECV;
303 if (sc->sc_n_recv == 0) { /* sanity and compat with old hw/simulator */ 303 if (sc->sc_n_recv == 0) { /* sanity and compat with old hw/simulator */
304 sc->sc_n_recv = 8; 304 sc->sc_n_recv = 8;
305 sc->sc_n_xmit = 4; 305 sc->sc_n_xmit = 4;
306 } else { 306 } else {
307 sc->sc_n_xmit = hw->OutputFifoSize; 307 sc->sc_n_xmit = hw->OutputFifoSize;
308 if (sc->sc_n_xmit > SC_MAX_N_XMIT) 308 if (sc->sc_n_xmit > SC_MAX_N_XMIT)
309 sc->sc_n_xmit = SC_MAX_N_XMIT; 309 sc->sc_n_xmit = SC_MAX_N_XMIT;
310 } 310 }
311 311
312 return 1; 312 return 1;
313} 313}
314 314
315void 315void
316enic_reset(struct ifnet *ifp) 316enic_reset(struct ifnet *ifp)
317{ 317{
318 int s; 318 int s;
319 319
320 s = splnet(); 320 s = splnet();
321 enic_stop(ifp, 0); 321 enic_stop(ifp, 0);
322 enic_init(ifp); 322 enic_init(ifp);
323 splx(s); 323 splx(s);
324} 324}
325 325
326void 326void
327enic_stop(struct ifnet *ifp, int suspend) 327enic_stop(struct ifnet *ifp, int suspend)
328{ 328{
329 struct enic_softc *sc = ifp->if_softc; 329 struct enic_softc *sc = ifp->if_softc;
330 330
331#if 0 331#if 0
332 printf("enic_stop %x\n", sc->sc_regs->Control); 332 printf("enic_stop %x\n", sc->sc_regs->Control);
333#endif 333#endif
334 334
335 /* 335 /*
336 * NB: only "ifconfig down" says suspend=1 (then "up" calls init) 336 * NB: only "ifconfig down" says suspend=1 (then "up" calls init)
337 * Could simply set RXDIS in this case 337 * Could simply set RXDIS in this case
338 */ 338 */
339 sc->inited = 2; 339 sc->inited = 2;
340 sc->sc_regs->Control = EC_RESET; 340 sc->sc_regs->Control = EC_RESET;
341 sc->sc_no_rd = 0; /* they are gone */ 341 sc->sc_no_rd = 0; /* they are gone */
342 sc->sc_no_td = 0; /* they are gone */ 342 sc->sc_no_td = 0; /* they are gone */
343} 343}
344 344
345void 345void
346enic_shutdown(void *arg) 346enic_shutdown(void *arg)
347{ 347{
348 struct ifnet *ifp = arg; 348 struct ifnet *ifp = arg;
349 349
350 enic_stop(ifp, 0); 350 enic_stop(ifp, 0);
351} 351}
352 352
353void 353void
354enic_kill_xmit(struct enic_softc *sc) 354enic_kill_xmit(struct enic_softc *sc)
355{ 355{
356 int i; 356 int i;
357 struct mbuf *m; 357 struct mbuf *m;
358 358
359 for (i = 0; i < sc->sc_n_xmit; i++) { 359 for (i = 0; i < sc->sc_n_xmit; i++) {
360 if ((m = sc->sc_xmit[i].mbuf) != NULL) { 360 if ((m = sc->sc_xmit[i].mbuf) != NULL) {
361 sc->sc_xmit[i].mbuf = NULL; 361 sc->sc_xmit[i].mbuf = NULL;
362 sc->sc_xmit[i].phys = ~0; 362 sc->sc_xmit[i].phys = ~0;
363 m_freem(m); 363 m_freem(m);
364 } 364 }
365 } 365 }
366 sc->sc_no_td = 0; 366 sc->sc_no_td = 0;
367 sc->sc_xmit_h = 0; 367 sc->sc_xmit_h = 0;
368} 368}
369 369
370void 370void
371enic_post_recv(struct enic_softc *sc, struct mbuf *m) 371enic_post_recv(struct enic_softc *sc, struct mbuf *m)
372{ 372{
373 int i, waitmode = M_DONTWAIT; 373 int i, waitmode = M_DONTWAIT;
374 paddr_t phys; 374 paddr_t phys;
375 375
376#define rpostone(_p_) \ 376#define rpostone(_p_) \
377 sc->sc_regs->SizeAndFlags = ES_F_RECV | MCLBYTES; \ 377 sc->sc_regs->SizeAndFlags = ES_F_RECV | MCLBYTES; \
378 sc->sc_regs->BufferAddressHi32 = 0; \ 378 sc->sc_regs->BufferAddressHi32 = 0; \
379 sc->sc_regs->BufferAddressLo32 = _p_; 379 sc->sc_regs->BufferAddressLo32 = _p_;
380#define tpostone(_p_,_s_) \ 380#define tpostone(_p_,_s_) \
381 sc->sc_regs->SizeAndFlags = ES_F_XMIT | (_s_); \ 381 sc->sc_regs->SizeAndFlags = ES_F_XMIT | (_s_); \
382 sc->sc_regs->BufferAddressHi32 = 0; \ 382 sc->sc_regs->BufferAddressHi32 = 0; \
383 sc->sc_regs->BufferAddressLo32 = _p_; 383 sc->sc_regs->BufferAddressLo32 = _p_;
384 384
385 /* Operational reload? */ 385 /* Operational reload? */
386 if (m != NULL) { 386 if (m != NULL) {
387 /* But is the hw ready for it */ 387 /* But is the hw ready for it */
388 if (sc->sc_regs->Control & EC_IF_FULL) 388 if (sc->sc_regs->Control & EC_IF_FULL)
389 goto no_room; 389 goto no_room;
390 /* Yes, find a spot. Include empty q case. */ 390 /* Yes, find a spot. Include empty q case. */
391 for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++) 391 for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
392 if (sc->sc_recv[i].mbuf == NULL) 392 if (sc->sc_recv[i].mbuf == NULL)
393 goto found; 393 goto found;
394 for (i = 0; i < sc->sc_recv_h; i++) 394 for (i = 0; i < sc->sc_recv_h; i++)
395 if (sc->sc_recv[i].mbuf == NULL) 395 if (sc->sc_recv[i].mbuf == NULL)
396 goto found; 396 goto found;
397 /* no spot, drop it (sigh) */ 397 /* no spot, drop it (sigh) */
398 no_room: 398 no_room:
399#if DEBUG 399#if DEBUG
400 sc->rf++; 400 sc->rf++;
401#endif 401#endif
402 m_freem(m); 402 m_freem(m);
403 return; 403 return;
404 found: 404 found:
405 phys = kvtophys((vaddr_t)m->m_data); 405 phys = kvtophys((vaddr_t)m->m_data);
406 sc->sc_recv[i].mbuf = m; 406 sc->sc_recv[i].mbuf = m;
407 sc->sc_recv[i].phys = phys; 407 sc->sc_recv[i].phys = phys;
408 rpostone(phys); 408 rpostone(phys);
409 sc->sc_no_rd++; 409 sc->sc_no_rd++;
410 return; 410 return;
411 } 411 }
412 412
413 /* Repost after reset? */ 413 /* Repost after reset? */
414 if (sc->inited) { 414 if (sc->inited) {
415 /* order doesnt matter, might as well keep it clean */ 415 /* order doesnt matter, might as well keep it clean */
416 int j = 0; 416 int j = 0;
417 sc->sc_recv_h = 0; 417 sc->sc_recv_h = 0;
418 for (i = 0; i < sc->sc_n_recv; i++) 418 for (i = 0; i < sc->sc_n_recv; i++)
419 if ((m = sc->sc_recv[i].mbuf) != NULL) { 419 if ((m = sc->sc_recv[i].mbuf) != NULL) {
420 phys = sc->sc_recv[i].phys; 420 phys = sc->sc_recv[i].phys;
421 sc->sc_recv[i].mbuf = NULL; 421 sc->sc_recv[i].mbuf = NULL;
422 sc->sc_recv[i].phys = ~0; 422 sc->sc_recv[i].phys = ~0;
423 sc->sc_recv[j].mbuf = m; 423 sc->sc_recv[j].mbuf = m;
424 sc->sc_recv[j].phys = phys; 424 sc->sc_recv[j].phys = phys;
425#if DEBUG 425#if DEBUG
426 if (sc->sc_regs->Control & EC_IF_FULL) 426 if (sc->sc_regs->Control & EC_IF_FULL)
427 printf("?uhu? postrecv full? %d\n", 427 printf("?uhu? postrecv full? %d\n",
428 sc->sc_no_rd); 428 sc->sc_no_rd);
429#endif 429#endif
430 sc->sc_no_rd++; 430 sc->sc_no_rd++;
431 rpostone(phys); 431 rpostone(phys);
432 j++; 432 j++;
433 } 433 }
434 /* Any holes left? */ 434 /* Any holes left? */
435 sc->inited = 1; 435 sc->inited = 1;
436 if (j >= sc->sc_n_recv) 436 if (j >= sc->sc_n_recv)
437 return; /* no, we are done */ 437 return; /* no, we are done */
438 /* continue on with the loop below */ 438 /* continue on with the loop below */
439 i = j; m = NULL; 439 i = j; m = NULL;
440 goto fillem; 440 goto fillem;
441 } 441 }
442 442
443 /* Initial refill, we can wait */ 443 /* Initial refill, we can wait */
444 waitmode = M_WAIT; 444 waitmode = M_WAIT;
445 sc->sc_recv_h = 0; 445 sc->sc_recv_h = 0;
446 memset(sc->sc_recv, 0, sizeof(sc->sc_recv[0]) * sc->sc_n_recv); 446 memset(sc->sc_recv, 0, sizeof(sc->sc_recv[0]) * sc->sc_n_recv);
447 i = 0; 447 i = 0;
448 fillem: 448 fillem:
449 for (; i < sc->sc_n_recv; i++) { 449 for (; i < sc->sc_n_recv; i++) {
450 MGETHDR(m, waitmode, MT_DATA); 450 MGETHDR(m, waitmode, MT_DATA);
451 if (m == 0) 451 if (m == 0)
452 break; 452 break;
453 m_set_rcvif(m, &sc->sc_ethercom.ec_if); 453 m_set_rcvif(m, &sc->sc_ethercom.ec_if);
454 m->m_pkthdr.len = 0; 454 m->m_pkthdr.len = 0;
455 455
456 MCLGET(m, waitmode); 456 MCLGET(m, waitmode);
457 if ((m->m_flags & M_EXT) == 0) 457 if ((m->m_flags & M_EXT) == 0)
458 break; 458 break;
459 459
460 /* 460 /*
461 * This offset aligns IP/TCP headers and helps performance 461 * This offset aligns IP/TCP headers and helps performance
462 */ 462 */
463#if 1 463#if 1
464#define ADJUST_MBUF_OFFSET(_m_) { \ 464#define ADJUST_MBUF_OFFSET(_m_) { \
465 (_m_)->m_data += 2; \ 465 (_m_)->m_data += 2; \
466 (_m_)->m_len -= 2; \ 466 (_m_)->m_len -= 2; \
467} 467}
468#else 468#else
469#define ADJUST_MBUF_OFFSET(_m_) 469#define ADJUST_MBUF_OFFSET(_m_)
470#endif 470#endif
471 471
472 m->m_len = MCLBYTES; 472 m->m_len = MCLBYTES;
473 473
474 ADJUST_MBUF_OFFSET(m); 474 ADJUST_MBUF_OFFSET(m);
475 phys = kvtophys((vaddr_t)m->m_data); 475 phys = kvtophys((vaddr_t)m->m_data);
476 sc->sc_recv[i].mbuf = m; 476 sc->sc_recv[i].mbuf = m;
477 sc->sc_recv[i].phys = phys; 477 sc->sc_recv[i].phys = phys;
478#if DEBUG 478#if DEBUG
479 if (sc->sc_regs->Control & EC_IF_FULL) 479 if (sc->sc_regs->Control & EC_IF_FULL)
480 printf("?uhu? postrecv2 full? %d\n", sc->sc_no_rd); 480 printf("?uhu? postrecv2 full? %d\n", sc->sc_no_rd);
481#endif 481#endif
482 sc->sc_no_rd++; 482 sc->sc_no_rd++;
483 rpostone(phys); 483 rpostone(phys);
484 m = NULL; 484 m = NULL;
485 } 485 }
486 486
487 if (m) 487 if (m)
488 m_freem(m); 488 m_freem(m);
489 sc->inited = 1; 489 sc->inited = 1;
490} 490}
491 491
492void enic_refill(struct enic_softc *sc) 492void enic_refill(struct enic_softc *sc)
493{ 493{
494 struct mbuf *m; 494 struct mbuf *m;
495 int waitmode = M_DONTWAIT; 495 int waitmode = M_DONTWAIT;
496 496
497 MGETHDR(m, waitmode, MT_DATA); 497 MGETHDR(m, waitmode, MT_DATA);
498 if (m == NULL) 498 if (m == NULL)
499 return; 499 return;
500 m_set_rcvif(m, &sc->sc_ethercom.ec_if); 500 m_set_rcvif(m, &sc->sc_ethercom.ec_if);
501 m->m_pkthdr.len = 0; 501 m->m_pkthdr.len = 0;
502 502
503 MCLGET(m, waitmode); 503 MCLGET(m, waitmode);
504 if ((m->m_flags & M_EXT) == 0) { 504 if ((m->m_flags & M_EXT) == 0) {
505 m_freem(m); 505 m_freem(m);
506 return; 506 return;
507 } 507 }
508 508
509 m->m_len = MCLBYTES; 509 m->m_len = MCLBYTES;
510 ADJUST_MBUF_OFFSET(m); 510 ADJUST_MBUF_OFFSET(m);
511 511
512 enic_post_recv(sc, m); 512 enic_post_recv(sc, m);
513} 513}
514 514
515int 515int
516enic_init(struct ifnet *ifp) 516enic_init(struct ifnet *ifp)
517{ 517{
518 struct enic_softc *sc = ifp->if_softc; 518 struct enic_softc *sc = ifp->if_softc;
519 uint32_t ctl; 519 uint32_t ctl;
520 520
521 /* no need to init many times unless we are in reset */ 521 /* no need to init many times unless we are in reset */
522 if (sc->inited != 1) { 522 if (sc->inited != 1) {
523 523
524 /* Cancel all xmit buffers */ 524 /* Cancel all xmit buffers */
525 enic_kill_xmit(sc); 525 enic_kill_xmit(sc);
526 526
527 /* Re-post all recv buffers */ 527 /* Re-post all recv buffers */
528 enic_post_recv(sc, NULL); 528 enic_post_recv(sc, NULL);
529 } 529 }
530 530
531 /* Start the eNIC */ 531 /* Start the eNIC */
532 ifp->if_flags |= IFF_RUNNING; 532 ifp->if_flags |= IFF_RUNNING;
533 ifp->if_flags &= ~IFF_OACTIVE; 533 ifp->if_flags &= ~IFF_OACTIVE;
534 ifp->if_timer = 0; 534 ifp->if_timer = 0;
535 ctl = sc->sc_regs->Control | EC_INTEN; 535 ctl = sc->sc_regs->Control | EC_INTEN;
536 ctl &= ~EC_RXDIS; 536 ctl &= ~EC_RXDIS;
537 sc->sc_regs->Control = ctl; 537 sc->sc_regs->Control = ctl;
538#if 0 538#if 0
539 printf("enic_init <- %x\n", ctl); 539 printf("enic_init <- %x\n", ctl);
540#endif 540#endif
541 541
542 if_schedule_deferred_start(ifp); 542 if_schedule_deferred_start(ifp);
543 543
544 return 0; 544 return 0;
545} 545}
546 546
547 547
548void 548void
549enic_watchdog(struct ifnet *ifp) 549enic_watchdog(struct ifnet *ifp)
550{ 550{
551 struct enic_softc *sc = ifp->if_softc; 551 struct enic_softc *sc = ifp->if_softc;
552 552
553#if 0 553#if 0
554 printf("enic_watch ctl=%x\n", sc->sc_regs->Control); 554 printf("enic_watch ctl=%x\n", sc->sc_regs->Control);
555#endif 555#endif
556 log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev)); 556 log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
557 ++ifp->if_oerrors; 557 ++ifp->if_oerrors;
558 558
559 enic_reset(ifp); 559 enic_reset(ifp);
560} 560}
561 561
562int 562int
563enic_mediachange(struct ifnet *ifp) 563enic_mediachange(struct ifnet *ifp)
564{ 564{
565#if 0 565#if 0
566 struct enic_softc *sc = ifp->if_softc; 566 struct enic_softc *sc = ifp->if_softc;
567#endif 567#endif
568 /* more code here.. */ 568 /* more code here.. */
569 569
570 return 0; 570 return 0;
571} 571}
572 572
573void 573void
574enic_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 574enic_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
575{ 575{
576 struct enic_softc *sc = ifp->if_softc; 576 struct enic_softc *sc = ifp->if_softc;
577 577
578 if ((ifp->if_flags & IFF_UP) == 0) 578 if ((ifp->if_flags & IFF_UP) == 0)
579 return; 579 return;
580 580
581 ifmr->ifm_status = IFM_AVALID; 581 ifmr->ifm_status = IFM_AVALID;
582 if (sc->sc_havecarrier) 582 if (sc->sc_havecarrier)
583 ifmr->ifm_status |= IFM_ACTIVE; 583 ifmr->ifm_status |= IFM_ACTIVE;
584 584
585 /* more code here someday.. */ 585 /* more code here someday.. */
586} 586}
587 587
588/* 588/*
589 * Process an ioctl request. 589 * Process an ioctl request.
590 */ 590 */
591int 591int
592enic_ioctl(struct ifnet *ifp, u_long cmd, void *data) 592enic_ioctl(struct ifnet *ifp, u_long cmd, void *data)
593{ 593{
594 struct enic_softc *sc = ifp->if_softc; 
595 struct ifreq *ifr = (struct ifreq *)data; 
596 int s, error = 0; 594 int s, error = 0;
597 595
598 s = splnet(); 596 s = splnet();
599 597
600 switch (cmd) { 598 switch (cmd) {
601 case SIOCSIFMEDIA: 
602#if 0 /*DEBUG*/ 
603 { 
604 extern int ei_drops[]; 
605 static int flip = 0; 
606 if (flip++ == 2) { 
607 int i; 
608 flip = 0; 
609 printf("enic_ioctl(%x) %qd/%qd %qd/%qd %d/%d %d:%d " 
610 "%d+%d %d/%d/%d\n", ifp->if_flags, 
611 ifp->if_ierrors, ifp->if_oerrors, 
612 ifp->if_ipackets, ifp->if_opackets, 
613 sc->sc_no_rd, sc->sc_no_td, 
614 sc->xhit, sc->xmiss, 
615 sc->tfull, sc->tfull2, 
616 sc->brh, sc->rf, sc->bxh); 
617 printf(" Ctl %x lt %x tim %d\n", 
618 sc->sc_regs->Control, sc->it, ifp->if_timer); 
619 
620 for (i = 0; i < 64; i++) 
621 if (ei_drops[i]) 
622 printf(" %d.%d", i, ei_drops[i]); 
623 printf("\n"); 
624 } 
625 } 
626#endif 
627 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 
628 break; 
629 
630 default: 599 default:
631 error = ether_ioctl(ifp, cmd, data); 600 error = ether_ioctl(ifp, cmd, data);
632 if (cmd == SIOCSIFADDR) { 601 if (cmd == SIOCSIFADDR) {
633 /* 602 /*
634 * hackattack: NFS does not turn us back 603 * hackattack: NFS does not turn us back
635 * on after a stop. So. 604 * on after a stop. So.
636 */ 605 */
637#if 0 606#if 0
638 printf("enic_ioctl(%lx)\n", cmd); 607 printf("enic_ioctl(%lx)\n", cmd);
639#endif 608#endif
640 enic_init(ifp); 609 enic_init(ifp);
641 } 610 }
642 if (error != ENETRESET) 611 if (error != ENETRESET)
643 break; 612 break;
644 error = 0; 613 error = 0;
645 if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI) 614 if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
646 break; 615 break;
647 if (ifp->if_flags & IFF_RUNNING) { 616 if (ifp->if_flags & IFF_RUNNING) {
648 enic_reset(ifp); 617 enic_reset(ifp);
649 } 618 }
650 break; 619 break;
651 } 620 }
652 splx(s); 621 splx(s);
653 622
654 return error; 623 return error;
655} 624}
656 625
657int 626int
658enic_intr(void *cookie, void *f) 627enic_intr(void *cookie, void *f)
659{ 628{
660 struct enic_softc *sc = cookie; 629 struct enic_softc *sc = cookie;
661 uint32_t isr, saf, hi, lo, fl; 630 uint32_t isr, saf, hi, lo, fl;
662 631
663 isr = sc->sc_regs->Control; 632 isr = sc->sc_regs->Control;
664 633
665 /* Make sure there is one and that we should take it */ 634 /* Make sure there is one and that we should take it */
666 if ((isr & (EC_INTEN | EC_DONE)) != (EC_INTEN | EC_DONE)) 635 if ((isr & (EC_INTEN | EC_DONE)) != (EC_INTEN | EC_DONE))
667 return 0; 636 return 0;
668 637
669 if (isr & EC_ERROR) { 638 if (isr & EC_ERROR) {
670 printf("%s: internal error\n", device_xname(sc->sc_dev)); 639 printf("%s: internal error\n", device_xname(sc->sc_dev));
671 enic_reset(&sc->sc_ethercom.ec_if); 640 enic_reset(&sc->sc_ethercom.ec_if);
672 return 1; 641 return 1;
673 } 642 }
674 643
675 /* 644 /*
676 * pull out all completed buffers 645 * pull out all completed buffers
677 */ 646 */
678 while ((isr & EC_OF_EMPTY) == 0) { 647 while ((isr & EC_OF_EMPTY) == 0) {
679 648
680 /* beware, order matters */ 649 /* beware, order matters */
681 saf = sc->sc_regs->SizeAndFlags; 650 saf = sc->sc_regs->SizeAndFlags;
682 hi = sc->sc_regs->BufferAddressHi32; /* BUGBUG 64bit */ 651 hi = sc->sc_regs->BufferAddressHi32; /* BUGBUG 64bit */
683 lo = sc->sc_regs->BufferAddressLo32; /* this pops the fifo */ 652 lo = sc->sc_regs->BufferAddressLo32; /* this pops the fifo */
684 __USE(hi); 653 __USE(hi);
685 654
686 fl = saf & (ES_F_MASK &~ ES_F_DONE); 655 fl = saf & (ES_F_MASK &~ ES_F_DONE);
687 if (fl == ES_F_RECV) 656 if (fl == ES_F_RECV)
688 enic_rint(sc, saf, lo); 657 enic_rint(sc, saf, lo);
689 else if (fl == ES_F_XMIT) 658 else if (fl == ES_F_XMIT)
690 enic_tint(sc, saf, lo); 659 enic_tint(sc, saf, lo);
691 else { 660 else {
692 /* 661 /*
693 * we do not currently expect or care for 662 * we do not currently expect or care for
694 * command completions? 663 * command completions?
695 */ 664 */
696 if (fl != ES_F_CMD) 665 if (fl != ES_F_CMD)
697 printf("%s: invalid saf=x%x (lo=%x)\n", 666 printf("%s: invalid saf=x%x (lo=%x)\n",
698 device_xname(sc->sc_dev), saf, lo); 667 device_xname(sc->sc_dev), saf, lo);
699 } 668 }
700 669
701 isr = sc->sc_regs->Control; 670 isr = sc->sc_regs->Control;
702 } 671 }
703 672
704 rnd_add_uint32(&sc->rnd_source, isr); 673 rnd_add_uint32(&sc->rnd_source, isr);
705 674
706 return 1; 675 return 1;
707} 676}
708 677
709void 678void
710enic_rint(struct enic_softc *sc, uint32_t saf, paddr_t phys) 679enic_rint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
711{ 680{
712 struct mbuf *m; 681 struct mbuf *m;
713 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 682 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
714 int len = saf & ES_S_MASK, i; 683 int len = saf & ES_S_MASK, i;
715 684
716 /* Find what buffer it is. Should be the first. */ 685 /* Find what buffer it is. Should be the first. */
717 for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++) 686 for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
718 if (sc->sc_recv[i].phys == phys) 687 if (sc->sc_recv[i].phys == phys)
719 goto found; 688 goto found;
720 for (i = 0; i < sc->sc_recv_h; i++) 689 for (i = 0; i < sc->sc_recv_h; i++)
721 if (sc->sc_recv[i].phys == phys) 690 if (sc->sc_recv[i].phys == phys)
722 goto found; 691 goto found;
723 692
724 /* uhu?? */ 693 /* uhu?? */
725 printf("%s: bad recv phys %llx\n", device_xname(sc->sc_dev), 694 printf("%s: bad recv phys %llx\n", device_xname(sc->sc_dev),
726 (long long)phys); 695 (long long)phys);
727 ifp->if_ierrors++; 696 ifp->if_ierrors++;
728 return; 697 return;
729 698
730 /* got it, pop it */ 699 /* got it, pop it */
731 found: 700 found:
732 sc->sc_no_rd--; 701 sc->sc_no_rd--;
733 m = sc->sc_recv[i].mbuf; 702 m = sc->sc_recv[i].mbuf;
734 sc->sc_recv[i].mbuf = NULL; 703 sc->sc_recv[i].mbuf = NULL;
735 sc->sc_recv[i].phys = ~0; 704 sc->sc_recv[i].phys = ~0;
736 if (i == sc->sc_recv_h) { /* should be */ 705 if (i == sc->sc_recv_h) { /* should be */
737 sc->sc_recv_h = (++i == sc->sc_n_recv) ? 0 : i; 706 sc->sc_recv_h = (++i == sc->sc_n_recv) ? 0 : i;
738 } 707 }
739#if DEBUG 708#if DEBUG
740 else 709 else
741 sc->brh++; 710 sc->brh++;
742#endif 711#endif
743 712
744 if (len <= sizeof(struct ether_header) || 713 if (len <= sizeof(struct ether_header) ||
745 len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ? 714 len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
746 ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) : 715 ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
747 ETHERMTU + sizeof(struct ether_header))) { 716 ETHERMTU + sizeof(struct ether_header))) {
748 ifp->if_ierrors++; 717 ifp->if_ierrors++;
749 718
750 /* reuse it */ 719 /* reuse it */
751 enic_post_recv(sc, m); 720 enic_post_recv(sc, m);
752 return; 721 return;
753 } 722 }
754 723
755 /* Adjust size */ 724 /* Adjust size */
756 m->m_pkthdr.len = len; 725 m->m_pkthdr.len = len;
757 m->m_len = len; /* recheck */ 726 m->m_len = len; /* recheck */
758 727
759 /* Pass the packet up. */ 728 /* Pass the packet up. */
760 if_percpuq_enqueue(ifp->if_percpuq, m); 729 if_percpuq_enqueue(ifp->if_percpuq, m);
761 730
762 /* Need to refill now */ 731 /* Need to refill now */
763 enic_refill(sc); 732 enic_refill(sc);
764} 733}
765 734
766void enic_tint(struct enic_softc *sc, uint32_t saf, paddr_t phys) 735void enic_tint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
767{ 736{
768 struct mbuf *m; 737 struct mbuf *m;
769 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 738 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
770 int i; 739 int i;
771 740
772#if DEBUG 741#if DEBUG
773 sc->it = 1; 742 sc->it = 1;
774#endif 743#endif
775 744
776 /* BUGBUG should there be a per-buffer error bit in SAF? */ 745 /* BUGBUG should there be a per-buffer error bit in SAF? */
777 746
778 /* Find what buffer it is. Should be the first. */ 747 /* Find what buffer it is. Should be the first. */
779 for (i = sc->sc_xmit_h; i < sc->sc_n_xmit; i++) 748 for (i = sc->sc_xmit_h; i < sc->sc_n_xmit; i++)
780 if (sc->sc_xmit[i].phys == phys) 749 if (sc->sc_xmit[i].phys == phys)
781 goto found; 750 goto found;
782 for (i = 0; i < sc->sc_xmit_h; i++) 751 for (i = 0; i < sc->sc_xmit_h; i++)
783 if (sc->sc_xmit[i].phys == phys) 752 if (sc->sc_xmit[i].phys == phys)
784 goto found; 753 goto found;
785 754
786 /* uhu?? */ 755 /* uhu?? */
787 printf("%s: bad xmit phys %llx\n", device_xname(sc->sc_dev), 756 printf("%s: bad xmit phys %llx\n", device_xname(sc->sc_dev),
788 (long long)phys); 757 (long long)phys);
789 ifp->if_oerrors++; 758 ifp->if_oerrors++;
790 return; 759 return;
791 760
792 /* got it, pop it */ 761 /* got it, pop it */
793 found: 762 found:
794 m = sc->sc_xmit[i].mbuf; 763 m = sc->sc_xmit[i].mbuf;
795 sc->sc_xmit[i].mbuf = NULL; 764 sc->sc_xmit[i].mbuf = NULL;
796 sc->sc_xmit[i].phys = ~0; 765 sc->sc_xmit[i].phys = ~0;
797 if (i == sc->sc_xmit_h) { /* should be */ 766 if (i == sc->sc_xmit_h) { /* should be */
798 sc->sc_xmit_h = (++i == sc->sc_n_xmit) ? 0 : i; 767 sc->sc_xmit_h = (++i == sc->sc_n_xmit) ? 0 : i;
799 } 768 }
800#if DEBUG 769#if DEBUG
801 else 770 else
802 sc->bxh++; 771 sc->bxh++;
803#endif 772#endif
804 m_freem(m); 773 m_freem(m);
805 ifp->if_opackets++; 774 ifp->if_opackets++;
806 775
807 if (--sc->sc_no_td == 0) 776 if (--sc->sc_no_td == 0)
808 ifp->if_timer = 0; 777 ifp->if_timer = 0;
809 778
810 ifp->if_flags &= ~IFF_OACTIVE; 779 ifp->if_flags &= ~IFF_OACTIVE;
811 if_schedule_deferred_start(ifp); 780 if_schedule_deferred_start(ifp);
812#if DEBUG 781#if DEBUG
813 sc->it = 1; 782 sc->it = 1;
814#endif 783#endif
815} 784}
816 785
817/* 786/*
818 * Setup output on interface. 787 * Setup output on interface.
819 * Get another datagram to send off of the interface queue, and map it to the 788 * Get another datagram to send off of the interface queue, and map it to the
820 * interface before starting the output. 789 * interface before starting the output.
821 * Called only at splnet or interrupt level. 790 * Called only at splnet or interrupt level.
822 */ 791 */
823void 792void
824enic_start(struct ifnet *ifp) 793enic_start(struct ifnet *ifp)
825{ 794{
826 struct enic_softc *sc = ifp->if_softc; 795 struct enic_softc *sc = ifp->if_softc;
827 struct mbuf *m; 796 struct mbuf *m;
828 int len, ix, s; 797 int len, ix, s;
829 paddr_t phys; 798 paddr_t phys;
830 799
831#if DEBUG 800#if DEBUG
832 sc->it = 0; 801 sc->it = 0;
833#endif 802#endif
834 803
835#if 0 804#if 0
836 printf("enic_start(%x)\n", ifp->if_flags); 805 printf("enic_start(%x)\n", ifp->if_flags);
837#endif 806#endif
838 807
839 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 808 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
840 return; 809 return;
841 810
842 s = splnet(); /* I know, I dont trust people.. */ 811 s = splnet(); /* I know, I dont trust people.. */
843 812
844 ix = sc->sc_xmit_h; 813 ix = sc->sc_xmit_h;
845 for (;;) { 814 for (;;) {
846 815
847 /* Anything to do? */ 816 /* Anything to do? */
848 IFQ_POLL(&ifp->if_snd, m); 817 IFQ_POLL(&ifp->if_snd, m);
849 if (m == NULL) 818 if (m == NULL)
850 break; 819 break;
851 820
852 /* find a spot, if any */ 821 /* find a spot, if any */
853 for (; ix < sc->sc_n_xmit; ix++) 822 for (; ix < sc->sc_n_xmit; ix++)
854 if (sc->sc_xmit[ix].mbuf == NULL) 823 if (sc->sc_xmit[ix].mbuf == NULL)
855 goto found; 824 goto found;
856 for (ix = 0; ix < sc->sc_xmit_h; ix++) 825 for (ix = 0; ix < sc->sc_xmit_h; ix++)
857 if (sc->sc_xmit[ix].mbuf == NULL) 826 if (sc->sc_xmit[ix].mbuf == NULL)
858 goto found; 827 goto found;
859 /* oh well */ 828 /* oh well */
860 ifp->if_flags |= IFF_OACTIVE; 829 ifp->if_flags |= IFF_OACTIVE;
861#if DEBUG 830#if DEBUG
862 sc->tfull++; 831 sc->tfull++;
863#endif 832#endif
864 break; 833 break;
865 834
866 found: 835 found:
867 IFQ_DEQUEUE(&ifp->if_snd, m); 836 IFQ_DEQUEUE(&ifp->if_snd, m);
868 if (m == NULL) 837 if (m == NULL)
869 break; 838 break;
870 839
871 /* 840 /*
872 * If BPF is listening on this interface, let it see the packet 841 * If BPF is listening on this interface, let it see the packet
873 * before we commit it to the wire. 842 * before we commit it to the wire.
874 */ 843 */
875 bpf_mtap(ifp, m, BPF_D_OUT); 844 bpf_mtap(ifp, m, BPF_D_OUT);
876 845
877 /* 846 /*
878 * Copy the mbuf chain into a contiguous transmit buffer. 847 * Copy the mbuf chain into a contiguous transmit buffer.
879 */ 848 */
880 len = enic_put(sc, &m); 849 len = enic_put(sc, &m);
881 if (len == 0) 850 if (len == 0)
882 break; /* sanity */ 851 break; /* sanity */
883 if (len > (ETHERMTU + sizeof(struct ether_header))) { 852 if (len > (ETHERMTU + sizeof(struct ether_header))) {
884 printf("enic? tlen %d > %d\n", 853 printf("enic? tlen %d > %d\n",
885 len, ETHERMTU + sizeof(struct ether_header)); 854 len, ETHERMTU + sizeof(struct ether_header));
886 len = ETHERMTU + sizeof(struct ether_header); 855 len = ETHERMTU + sizeof(struct ether_header);
887 } 856 }
888 857
889 ifp->if_timer = 5; 858 ifp->if_timer = 5;
890 859
891 /* 860 /*
892 * Remember and post the buffer 861 * Remember and post the buffer
893 */ 862 */
894 phys = kvtophys((vaddr_t)m->m_data); 863 phys = kvtophys((vaddr_t)m->m_data);
895 sc->sc_xmit[ix].mbuf = m; 864 sc->sc_xmit[ix].mbuf = m;
896 sc->sc_xmit[ix].phys = phys; 865 sc->sc_xmit[ix].phys = phys;
897 866
898 sc->sc_no_td++; 867 sc->sc_no_td++;
899 868
900 tpostone(phys, len); 869 tpostone(phys, len);
901 870
902 if (sc->sc_regs->Control & EC_IF_FULL) { 871 if (sc->sc_regs->Control & EC_IF_FULL) {
903 ifp->if_flags |= IFF_OACTIVE; 872 ifp->if_flags |= IFF_OACTIVE;
904#if DEBUG 873#if DEBUG
905 sc->tfull2++; 874 sc->tfull2++;
906#endif 875#endif
907 break; 876 break;
908 } 877 }
909 878
910 ix++; 879 ix++;
911 } 880 }
912 881
913 splx(s); 882 splx(s);
914} 883}
915 884
916int enic_put(struct enic_softc *sc, struct mbuf **pm) 885int enic_put(struct enic_softc *sc, struct mbuf **pm)
917{ 886{
918 struct mbuf *n, *m = *pm, *mm; 887 struct mbuf *n, *m = *pm, *mm;
919 int len, tlen = 0, xlen = m->m_pkthdr.len; 888 int len, tlen = 0, xlen = m->m_pkthdr.len;
920 uint8_t *cp; 889 uint8_t *cp;
921 890
922#if 0 891#if 0
923 /* drop garbage */ 892 /* drop garbage */
924 tlen = xlen; 893 tlen = xlen;
925 for (; m; m = n) { 894 for (; m; m = n) {
926 len = m->m_len; 895 len = m->m_len;
927 if (len == 0) { 896 if (len == 0) {
928 n = m_free(m); 897 n = m_free(m);
929 if (m == *pm) 898 if (m == *pm)
930 *pm = n; 899 *pm = n;
931 continue; 900 continue;
932 } 901 }
933 tlen -= len; 902 tlen -= len;
934 KASSERT(m != m->m_next); 903 KASSERT(m != m->m_next);
935 n = m->m_next; 904 n = m->m_next;
936 if (tlen <= 0) 905 if (tlen <= 0)
937 break; 906 break;
938 } 907 }
939 908
940 /* 909 /*
941 * We might be done: 910 * We might be done:
942 * (a) empty chain (b) only one segment (c) bad chain 911 * (a) empty chain (b) only one segment (c) bad chain
943 */ 912 */
944 if (((m = *pm) == NULL) || (tlen > 0)) 913 if (((m = *pm) == NULL) || (tlen > 0))
945 xlen = 0; 914 xlen = 0;
946#endif 915#endif
947 916
948 if ((xlen == 0) || (xlen <= m->m_len)) { 917 if ((xlen == 0) || (xlen <= m->m_len)) {
949#if DEBUG 918#if DEBUG
950 sc->xhit++; 919 sc->xhit++;
951#endif 920#endif
952 return xlen; 921 return xlen;
953 } 922 }
954 923
955 /* Nope, true chain. Copy to contig :-(( */ 924 /* Nope, true chain. Copy to contig :-(( */
956 tlen = xlen; 925 tlen = xlen;
957 MGETHDR(n, M_NOWAIT, MT_DATA); 926 MGETHDR(n, M_NOWAIT, MT_DATA);
958 if (n == NULL) 927 if (n == NULL)
959 goto Bad; 928 goto Bad;
960 m_set_rcvif(n, &sc->sc_ethercom.ec_if); 929 m_set_rcvif(n, &sc->sc_ethercom.ec_if);
961 n->m_pkthdr.len = tlen; 930 n->m_pkthdr.len = tlen;
962 931
963 MCLGET(n, M_NOWAIT); 932 MCLGET(n, M_NOWAIT);
964 if ((n->m_flags & M_EXT) == 0) { 933 if ((n->m_flags & M_EXT) == 0) {
965 m_freem(n); 934 m_freem(n);
966 goto Bad; 935 goto Bad;
967 } 936 }
968 937
969 n->m_len = tlen; 938 n->m_len = tlen;
970 cp = mtod(n, uint8_t *); 939 cp = mtod(n, uint8_t *);
971 for (; m && tlen; m = mm) { 940 for (; m && tlen; m = mm) {
972 941
973 len = m->m_len; 942 len = m->m_len;
974 if (len > tlen) 943 if (len > tlen)
975 len = tlen; 944 len = tlen;
976 if (len) 945 if (len)
977 memcpy(cp, mtod(m, void *), len); 946 memcpy(cp, mtod(m, void *), len);
978 947
979 cp += len; 948 cp += len;
980 tlen -= len; 949 tlen -= len;
981 mm = m_free(m); 950 mm = m_free(m);
982 951
983 } 952 }
984 953
985 *pm = n; 954 *pm = n;
986#if DEBUG 955#if DEBUG
987 sc->xmiss++; 956 sc->xmiss++;
988#endif 957#endif
989 return (xlen); 958 return (xlen);
990 959
991 Bad: 960 Bad:
992 printf("enic_put: no mem?\n"); 961 printf("enic_put: no mem?\n");
993 m_freem(m); 962 m_freem(m);
994 return 0; 963 return 0;
995} 964}