if_se.c revision 1.108 1 1.108 riastrad /* $NetBSD: if_se.c,v 1.108 2020/06/29 23:04:57 riastradh Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1997 Ian W. Dall <ian.dall (at) dsto.defence.gov.au>
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Ian W. Dall.
18 1.1 thorpej * 4. The name of the author may not be used to endorse or promote products
19 1.1 thorpej * derived from this software without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 thorpej */
32 1.1 thorpej
33 1.1 thorpej /*
34 1.11 matthias * Driver for Cabletron EA41x scsi ethernet adaptor.
35 1.11 matthias *
36 1.1 thorpej * Written by Ian Dall <ian.dall (at) dsto.defence.gov.au> Feb 3, 1997
37 1.11 matthias *
38 1.11 matthias * Acknowledgement: Thanks are due to Philip L. Budne <budd (at) cs.bu.edu>
39 1.27 soren * who reverse engineered the EA41x. In developing this code,
40 1.11 matthias * Phil's userland daemon "etherd", was refered to extensively in lieu
41 1.11 matthias * of accurate documentation for the device.
42 1.1 thorpej *
43 1.1 thorpej * This is a weird device! It doesn't conform to the scsi spec in much
44 1.19 is * at all. About the only standard command supported is inquiry. Most
45 1.1 thorpej * commands are 6 bytes long, but the recv data is only 1 byte. Data
46 1.1 thorpej * must be received by periodically polling the device with the recv
47 1.1 thorpej * command.
48 1.1 thorpej *
49 1.1 thorpej * This driver is also a bit unusual. It must look like a network
50 1.1 thorpej * interface and it must also appear to be a scsi device to the scsi
51 1.1 thorpej * system. Hence there are cases where there are two entry points. eg
52 1.106 jdc * sedone is to be called from the scsi subsytem and se_ifstart from
53 1.1 thorpej * the network interface subsystem. In addition, to facilitate scsi
54 1.1 thorpej * commands issued by userland programs, there are open, close and
55 1.1 thorpej * ioctl entry points. This allows a user program to, for example,
56 1.1 thorpej * display the ea41x stats and download new code into the adaptor ---
57 1.1 thorpej * functions which can't be performed through the ifconfig interface.
58 1.5 matthias * Normal operation does not require any special userland program.
59 1.1 thorpej */
60 1.38 lukem
61 1.38 lukem #include <sys/cdefs.h>
62 1.108 riastrad __KERNEL_RCSID(0, "$NetBSD: if_se.c,v 1.108 2020/06/29 23:04:57 riastradh Exp $");
63 1.1 thorpej
64 1.88 pooka #ifdef _KERNEL_OPT
65 1.14 jonathan #include "opt_inet.h"
66 1.106 jdc #include "opt_net_mpsafe.h"
67 1.14 jonathan #include "opt_atalk.h"
68 1.88 pooka #endif
69 1.1 thorpej
70 1.1 thorpej #include <sys/param.h>
71 1.1 thorpej #include <sys/systm.h>
72 1.28 thorpej #include <sys/callout.h>
73 1.1 thorpej #include <sys/syslog.h>
74 1.1 thorpej #include <sys/kernel.h>
75 1.1 thorpej #include <sys/file.h>
76 1.1 thorpej #include <sys/stat.h>
77 1.1 thorpej #include <sys/ioctl.h>
78 1.1 thorpej #include <sys/buf.h>
79 1.1 thorpej #include <sys/uio.h>
80 1.1 thorpej #include <sys/malloc.h>
81 1.1 thorpej #include <sys/errno.h>
82 1.1 thorpej #include <sys/device.h>
83 1.1 thorpej #include <sys/disklabel.h>
84 1.1 thorpej #include <sys/disk.h>
85 1.1 thorpej #include <sys/proc.h>
86 1.1 thorpej #include <sys/conf.h>
87 1.105 jdc #include <sys/mutex.h>
88 1.105 jdc #include <sys/pcq.h>
89 1.106 jdc #include <sys/workqueue.h>
90 1.1 thorpej
91 1.9 bouyer #include <dev/scsipi/scsipi_all.h>
92 1.9 bouyer #include <dev/scsipi/scsi_ctron_ether.h>
93 1.9 bouyer #include <dev/scsipi/scsiconf.h>
94 1.1 thorpej
95 1.1 thorpej #include <sys/mbuf.h>
96 1.1 thorpej
97 1.1 thorpej #include <sys/socket.h>
98 1.1 thorpej #include <net/if.h>
99 1.1 thorpej #include <net/if_dl.h>
100 1.1 thorpej #include <net/if_ether.h>
101 1.5 matthias #include <net/if_media.h>
102 1.96 msaitoh #include <net/bpf.h>
103 1.1 thorpej
104 1.1 thorpej #ifdef INET
105 1.1 thorpej #include <netinet/in.h>
106 1.1 thorpej #include <netinet/if_inarp.h>
107 1.2 cgd #endif
108 1.2 cgd
109 1.2 cgd
110 1.5 matthias #ifdef NETATALK
111 1.5 matthias #include <netatalk/at.h>
112 1.5 matthias #endif
113 1.5 matthias
114 1.1 thorpej
115 1.1 thorpej #define SETIMEOUT 1000
116 1.1 thorpej #define SEOUTSTANDING 4
117 1.1 thorpej #define SERETRIES 4
118 1.1 thorpej #define SE_PREFIX 4
119 1.1 thorpej #define ETHER_CRC 4
120 1.1 thorpej #define SEMINSIZE 60
121 1.1 thorpej
122 1.1 thorpej /* Make this big enough for an ETHERMTU packet in promiscuous mode. */
123 1.1 thorpej #define MAX_SNAP (ETHERMTU + sizeof(struct ether_header) + \
124 1.1 thorpej SE_PREFIX + ETHER_CRC)
125 1.1 thorpej
126 1.11 matthias /* 10 full length packets appears to be the max ever returned. 16k is OK */
127 1.11 matthias #define RBUF_LEN (16 * 1024)
128 1.11 matthias
129 1.57 perry /* Tuning parameters:
130 1.11 matthias * The EA41x only returns a maximum of 10 packets (regardless of size).
131 1.11 matthias * We will attempt to adapt to polling fast enough to get RDATA_GOAL packets
132 1.11 matthias * per read
133 1.11 matthias */
134 1.11 matthias #define RDATA_MAX 10
135 1.11 matthias #define RDATA_GOAL 8
136 1.11 matthias
137 1.11 matthias /* se_poll and se_poll0 are the normal polling rate and the minimum
138 1.11 matthias * polling rate respectively. se_poll0 should be chosen so that at
139 1.11 matthias * maximum ethernet speed, we will read nearly RDATA_MAX packets. se_poll
140 1.11 matthias * should be chosen for reasonable maximum latency.
141 1.11 matthias * In practice, if we are being saturated with min length packets, we
142 1.11 matthias * can't poll fast enough. Polling with zero delay actually
143 1.11 matthias * worsens performance. se_poll0 is enforced to be always at least 1
144 1.5 matthias */
145 1.1 thorpej #define SE_POLL 40 /* default in milliseconds */
146 1.11 matthias #define SE_POLL0 10 /* default in milliseconds */
147 1.11 matthias int se_poll = 0; /* Delay in ticks set at attach time */
148 1.1 thorpej int se_poll0 = 0;
149 1.105 jdc #ifdef SE_DEBUG
150 1.11 matthias int se_max_received = 0; /* Instrumentation */
151 1.105 jdc #endif
152 1.1 thorpej
153 1.1 thorpej #define PROTOCMD(p, d) \
154 1.3 thorpej ((d) = (p))
155 1.1 thorpej
156 1.63 cube #define PROTOCMD_DECL(name) \
157 1.63 cube static const struct scsi_ctron_ether_generic name
158 1.1 thorpej
159 1.63 cube #define PROTOCMD_DECL_SPECIAL(name) \
160 1.100 msaitoh static const struct __CONCAT(scsi_, name) name
161 1.3 thorpej
162 1.3 thorpej /* Command initializers for commands using scsi_ctron_ether_generic */
163 1.101 msaitoh PROTOCMD_DECL(ctron_ether_send) = {CTRON_ETHER_SEND, 0, {0,0}, 0};
164 1.63 cube PROTOCMD_DECL(ctron_ether_add_proto) = {CTRON_ETHER_ADD_PROTO, 0, {0,0}, 0};
165 1.63 cube PROTOCMD_DECL(ctron_ether_get_addr) = {CTRON_ETHER_GET_ADDR, 0, {0,0}, 0};
166 1.63 cube PROTOCMD_DECL(ctron_ether_set_media) = {CTRON_ETHER_SET_MEDIA, 0, {0,0}, 0};
167 1.63 cube PROTOCMD_DECL(ctron_ether_set_addr) = {CTRON_ETHER_SET_ADDR, 0, {0,0}, 0};
168 1.63 cube PROTOCMD_DECL(ctron_ether_set_multi) = {CTRON_ETHER_SET_MULTI, 0, {0,0}, 0};
169 1.63 cube PROTOCMD_DECL(ctron_ether_remove_multi) =
170 1.63 cube {CTRON_ETHER_REMOVE_MULTI, 0, {0,0}, 0};
171 1.3 thorpej
172 1.3 thorpej /* Command initializers for commands using their own structures */
173 1.63 cube PROTOCMD_DECL_SPECIAL(ctron_ether_recv) = {CTRON_ETHER_RECV};
174 1.63 cube PROTOCMD_DECL_SPECIAL(ctron_ether_set_mode) =
175 1.63 cube {CTRON_ETHER_SET_MODE, 0, {0,0}, 0};
176 1.1 thorpej
177 1.1 thorpej struct se_softc {
178 1.85 chs device_t sc_dev;
179 1.1 thorpej struct ethercom sc_ethercom; /* Ethernet common part */
180 1.34 bouyer struct scsipi_periph *sc_periph;/* contains our targ, lun, etc. */
181 1.28 thorpej
182 1.28 thorpej struct callout sc_recv_ch;
183 1.105 jdc struct kmutex sc_iflock;
184 1.105 jdc struct if_percpuq *sc_ipq;
185 1.106 jdc struct workqueue *sc_recv_wq, *sc_send_wq;
186 1.106 jdc struct work sc_recv_work, sc_send_work;
187 1.106 jdc int sc_recv_work_pending, sc_send_work_pending;
188 1.28 thorpej
189 1.1 thorpej char *sc_tbuf;
190 1.1 thorpej char *sc_rbuf;
191 1.1 thorpej int protos;
192 1.5 matthias #define PROTO_IP 0x01
193 1.5 matthias #define PROTO_ARP 0x02
194 1.5 matthias #define PROTO_REVARP 0x04
195 1.5 matthias #define PROTO_AT 0x08
196 1.5 matthias #define PROTO_AARP 0x10
197 1.1 thorpej int sc_debug;
198 1.1 thorpej int sc_flags;
199 1.11 matthias int sc_last_timeout;
200 1.21 thorpej int sc_enabled;
201 1.107 jdc int sc_attach_state;
202 1.1 thorpej };
203 1.1 thorpej
204 1.78 cegger static int sematch(device_t, cfdata_t, void *);
205 1.78 cegger static void seattach(device_t, device_t, void *);
206 1.107 jdc static int sedetach(device_t, int);
207 1.1 thorpej
208 1.56 perry static void se_ifstart(struct ifnet *);
209 1.1 thorpej
210 1.56 perry static void sedone(struct scsipi_xfer *, int);
211 1.64 christos static int se_ioctl(struct ifnet *, u_long, void *);
212 1.56 perry static void sewatchdog(struct ifnet *);
213 1.56 perry
214 1.91 jakllsch #if 0
215 1.100 msaitoh static inline uint16_t ether_cmp(void *, void *);
216 1.91 jakllsch #endif
217 1.106 jdc static void se_recv_callout(void *);
218 1.106 jdc static void se_recv_worker(struct work *wk, void *cookie);
219 1.106 jdc static void se_recv(struct se_softc *);
220 1.56 perry static struct mbuf *se_get(struct se_softc *, char *, int);
221 1.56 perry static int se_read(struct se_softc *, char *, int);
222 1.106 jdc static void se_reset(struct se_softc *);
223 1.56 perry static int se_add_proto(struct se_softc *, int);
224 1.100 msaitoh static int se_get_addr(struct se_softc *, uint8_t *);
225 1.56 perry static int se_set_media(struct se_softc *, int);
226 1.56 perry static int se_init(struct se_softc *);
227 1.100 msaitoh static int se_set_multi(struct se_softc *, uint8_t *);
228 1.100 msaitoh static int se_remove_multi(struct se_softc *, uint8_t *);
229 1.1 thorpej #if 0
230 1.56 perry static int sc_set_all_multi(struct se_softc *, int);
231 1.1 thorpej #endif
232 1.56 perry static void se_stop(struct se_softc *);
233 1.60 perry static inline int se_scsipi_cmd(struct scsipi_periph *periph,
234 1.10 enami struct scsipi_generic *scsipi_cmd,
235 1.55 reinoud int cmdlen, u_char *data_addr, int datalen,
236 1.10 enami int retries, int timeout, struct buf *bp,
237 1.56 perry int flags);
238 1.106 jdc static void se_send_worker(struct work *wk, void *cookie);
239 1.5 matthias static int se_set_mode(struct se_softc *, int, int);
240 1.1 thorpej
241 1.56 perry int se_enable(struct se_softc *);
242 1.56 perry void se_disable(struct se_softc *);
243 1.21 thorpej
244 1.85 chs CFATTACH_DECL_NEW(se, sizeof(struct se_softc),
245 1.107 jdc sematch, seattach, sedetach, NULL);
246 1.1 thorpej
247 1.13 thorpej extern struct cfdriver se_cd;
248 1.40 gehenna
249 1.40 gehenna dev_type_open(seopen);
250 1.40 gehenna dev_type_close(seclose);
251 1.40 gehenna dev_type_ioctl(seioctl);
252 1.40 gehenna
253 1.40 gehenna const struct cdevsw se_cdevsw = {
254 1.86 dholland .d_open = seopen,
255 1.86 dholland .d_close = seclose,
256 1.86 dholland .d_read = noread,
257 1.86 dholland .d_write = nowrite,
258 1.86 dholland .d_ioctl = seioctl,
259 1.86 dholland .d_stop = nostop,
260 1.86 dholland .d_tty = notty,
261 1.86 dholland .d_poll = nopoll,
262 1.86 dholland .d_mmap = nommap,
263 1.86 dholland .d_kqfilter = nokqfilter,
264 1.87 dholland .d_discard = nodiscard,
265 1.105 jdc .d_flag = D_OTHER | D_MPSAFE
266 1.40 gehenna };
267 1.1 thorpej
268 1.34 bouyer const struct scsipi_periphsw se_switch = {
269 1.1 thorpej NULL, /* Use default error handler */
270 1.106 jdc NULL, /* have no queue */
271 1.1 thorpej NULL, /* have no async handler */
272 1.107 jdc sedone, /* deal with send/recv completion */
273 1.1 thorpej };
274 1.1 thorpej
275 1.45 mycroft const struct scsipi_inquiry_pattern se_patterns[] = {
276 1.1 thorpej {T_PROCESSOR, T_FIXED,
277 1.101 msaitoh "CABLETRN", "EA412", ""},
278 1.1 thorpej {T_PROCESSOR, T_FIXED,
279 1.101 msaitoh "Cabletrn", "EA412", ""},
280 1.1 thorpej };
281 1.1 thorpej
282 1.91 jakllsch #if 0
283 1.1 thorpej /*
284 1.1 thorpej * Compare two Ether/802 addresses for equality, inlined and
285 1.1 thorpej * unrolled for speed.
286 1.37 thorpej * Note: use this like memcmp()
287 1.1 thorpej */
288 1.100 msaitoh static inline uint16_t
289 1.76 dsl ether_cmp(void *one, void *two)
290 1.1 thorpej {
291 1.100 msaitoh uint16_t *a = (uint16_t *) one;
292 1.100 msaitoh uint16_t *b = (uint16_t *) two;
293 1.100 msaitoh uint16_t diff;
294 1.1 thorpej
295 1.1 thorpej diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
296 1.1 thorpej
297 1.1 thorpej return (diff);
298 1.1 thorpej }
299 1.1 thorpej
300 1.1 thorpej #define ETHER_CMP ether_cmp
301 1.91 jakllsch #endif
302 1.1 thorpej
303 1.5 matthias static int
304 1.78 cegger sematch(device_t parent, cfdata_t match, void *aux)
305 1.1 thorpej {
306 1.9 bouyer struct scsipibus_attach_args *sa = aux;
307 1.1 thorpej int priority;
308 1.1 thorpej
309 1.9 bouyer (void)scsipi_inqmatch(&sa->sa_inqbuf,
310 1.58 jdc se_patterns, sizeof(se_patterns) / sizeof(se_patterns[0]),
311 1.1 thorpej sizeof(se_patterns[0]), &priority);
312 1.1 thorpej return (priority);
313 1.1 thorpej }
314 1.1 thorpej
315 1.1 thorpej /*
316 1.1 thorpej * The routine called by the low level scsi routine when it discovers
317 1.1 thorpej * a device suitable for this driver.
318 1.1 thorpej */
319 1.5 matthias static void
320 1.78 cegger seattach(device_t parent, device_t self, void *aux)
321 1.1 thorpej {
322 1.61 thorpej struct se_softc *sc = device_private(self);
323 1.9 bouyer struct scsipibus_attach_args *sa = aux;
324 1.34 bouyer struct scsipi_periph *periph = sa->sa_periph;
325 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
326 1.100 msaitoh uint8_t myaddr[ETHER_ADDR_LEN];
327 1.106 jdc char wqname[MAXCOMLEN];
328 1.95 msaitoh int rv;
329 1.1 thorpej
330 1.85 chs sc->sc_dev = self;
331 1.85 chs
332 1.5 matthias printf("\n");
333 1.34 bouyer SC_DEBUG(periph, SCSIPI_DB2, ("seattach: "));
334 1.1 thorpej
335 1.107 jdc sc->sc_attach_state = 0;
336 1.105 jdc callout_init(&sc->sc_recv_ch, CALLOUT_MPSAFE);
337 1.105 jdc mutex_init(&sc->sc_iflock, MUTEX_DEFAULT, IPL_SOFTNET);
338 1.28 thorpej
339 1.1 thorpej /*
340 1.1 thorpej * Store information needed to contact our base driver
341 1.1 thorpej */
342 1.34 bouyer sc->sc_periph = periph;
343 1.85 chs periph->periph_dev = sc->sc_dev;
344 1.34 bouyer periph->periph_switch = &se_switch;
345 1.34 bouyer
346 1.1 thorpej se_poll = (SE_POLL * hz) / 1000;
347 1.1 thorpej se_poll = se_poll? se_poll: 1;
348 1.1 thorpej se_poll0 = (SE_POLL0 * hz) / 1000;
349 1.1 thorpej se_poll0 = se_poll0? se_poll0: 1;
350 1.1 thorpej
351 1.1 thorpej /*
352 1.106 jdc * Initialize and attach send and receive buffers
353 1.1 thorpej */
354 1.1 thorpej sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header),
355 1.103 chs M_DEVBUF, M_WAITOK);
356 1.106 jdc sc->sc_rbuf = malloc(RBUF_LEN, M_DEVBUF, M_WAITOK);
357 1.1 thorpej
358 1.1 thorpej /* Initialize ifnet structure. */
359 1.85 chs strlcpy(ifp->if_xname, device_xname(sc->sc_dev), sizeof(ifp->if_xname));
360 1.1 thorpej ifp->if_softc = sc;
361 1.1 thorpej ifp->if_start = se_ifstart;
362 1.1 thorpej ifp->if_ioctl = se_ioctl;
363 1.1 thorpej ifp->if_watchdog = sewatchdog;
364 1.99 msaitoh ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
365 1.106 jdc ifp->if_extflags = IFEF_MPSAFE;
366 1.33 thorpej IFQ_SET_READY(&ifp->if_snd);
367 1.1 thorpej
368 1.106 jdc se_get_addr(sc, myaddr);
369 1.107 jdc sc->sc_attach_state = 1;
370 1.106 jdc
371 1.1 thorpej /* Attach the interface. */
372 1.95 msaitoh rv = if_initialize(ifp);
373 1.95 msaitoh if (rv != 0) {
374 1.107 jdc sedetach(sc->sc_dev, 0);
375 1.95 msaitoh return; /* Error */
376 1.95 msaitoh }
377 1.108 riastrad
378 1.106 jdc snprintf(wqname, sizeof(wqname), "%sRx", device_xname(sc->sc_dev));
379 1.106 jdc rv = workqueue_create(&sc->sc_recv_wq, wqname, se_recv_worker, sc,
380 1.106 jdc PRI_SOFTNET, IPL_NET, WQ_MPSAFE);
381 1.106 jdc if (rv != 0) {
382 1.106 jdc aprint_error_dev(sc->sc_dev,
383 1.107 jdc "unable to create recv Rx workqueue\n");
384 1.107 jdc sedetach(sc->sc_dev, 0);
385 1.106 jdc return; /* Error */
386 1.106 jdc }
387 1.106 jdc sc->sc_recv_work_pending = false;
388 1.107 jdc sc->sc_attach_state = 2;
389 1.107 jdc
390 1.106 jdc snprintf(wqname, sizeof(wqname), "%sTx", device_xname(sc->sc_dev));
391 1.106 jdc rv = workqueue_create(&sc->sc_send_wq, wqname, se_send_worker, ifp,
392 1.106 jdc PRI_SOFTNET, IPL_NET, WQ_MPSAFE);
393 1.106 jdc if (rv != 0) {
394 1.106 jdc aprint_error_dev(sc->sc_dev,
395 1.107 jdc "unable to create send Tx workqueue\n");
396 1.107 jdc sedetach(sc->sc_dev, 0);
397 1.106 jdc return; /* Error */
398 1.106 jdc }
399 1.106 jdc sc->sc_send_work_pending = false;
400 1.107 jdc sc->sc_attach_state = 3;
401 1.107 jdc
402 1.105 jdc sc->sc_ipq = if_percpuq_create(&sc->sc_ethercom.ec_if);
403 1.1 thorpej ether_ifattach(ifp, myaddr);
404 1.89 ozaki if_register(ifp);
405 1.107 jdc sc->sc_attach_state = 4;
406 1.107 jdc }
407 1.107 jdc
408 1.107 jdc static int
409 1.107 jdc sedetach(device_t self, int flags)
410 1.107 jdc {
411 1.107 jdc struct se_softc *sc = device_private(self);
412 1.107 jdc struct ifnet *ifp = &sc->sc_ethercom.ec_if;
413 1.107 jdc
414 1.107 jdc switch(sc->sc_attach_state) {
415 1.107 jdc case 4:
416 1.107 jdc se_stop(sc);
417 1.107 jdc mutex_enter(&sc->sc_iflock);
418 1.107 jdc ifp->if_flags &= ~IFF_RUNNING;
419 1.107 jdc se_disable(sc);
420 1.107 jdc callout_halt(&sc->sc_recv_ch, NULL);
421 1.107 jdc ether_ifdetach(ifp);
422 1.107 jdc if_detach(ifp);
423 1.107 jdc mutex_exit(&sc->sc_iflock);
424 1.107 jdc if_percpuq_destroy(sc->sc_ipq);
425 1.107 jdc /*FALLTHROUGH*/
426 1.107 jdc case 3:
427 1.107 jdc workqueue_destroy(sc->sc_send_wq);
428 1.107 jdc /*FALLTHROUGH*/
429 1.107 jdc case 2:
430 1.107 jdc workqueue_destroy(sc->sc_recv_wq);
431 1.107 jdc /*FALLTHROUGH*/
432 1.107 jdc case 1:
433 1.107 jdc free(sc->sc_rbuf, M_DEVBUF);
434 1.107 jdc free(sc->sc_tbuf, M_DEVBUF);
435 1.107 jdc callout_destroy(&sc->sc_recv_ch);
436 1.107 jdc mutex_destroy(&sc->sc_iflock);
437 1.107 jdc break;
438 1.107 jdc default:
439 1.107 jdc aprint_error_dev(sc->sc_dev, "detach failed (state %d)\n",
440 1.107 jdc sc->sc_attach_state);
441 1.107 jdc return 1;
442 1.107 jdc break;
443 1.107 jdc }
444 1.107 jdc return 0;
445 1.1 thorpej }
446 1.1 thorpej
447 1.105 jdc /*
448 1.105 jdc * Send a command to the device
449 1.105 jdc */
450 1.60 perry static inline int
451 1.84 christos se_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd,
452 1.84 christos int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
453 1.84 christos struct buf *bp, int flags)
454 1.1 thorpej {
455 1.1 thorpej int error;
456 1.10 enami
457 1.50 mycroft error = scsipi_command(periph, cmd, cmdlen, data_addr,
458 1.10 enami datalen, retries, timeout, bp, flags);
459 1.10 enami return (error);
460 1.1 thorpej }
461 1.5 matthias
462 1.105 jdc /*
463 1.106 jdc * Start routine for calling from network sub system
464 1.105 jdc */
465 1.5 matthias static void
466 1.106 jdc se_ifstart(struct ifnet *ifp)
467 1.1 thorpej {
468 1.21 thorpej struct se_softc *sc = ifp->if_softc;
469 1.106 jdc int i = 100;
470 1.10 enami
471 1.106 jdc mutex_enter(&sc->sc_iflock);
472 1.106 jdc while (i && sc->sc_send_work_pending == true) {
473 1.106 jdc i--;
474 1.106 jdc delay(10);
475 1.21 thorpej }
476 1.106 jdc if (i) {
477 1.106 jdc sc->sc_send_work_pending = true;
478 1.106 jdc workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work, NULL);
479 1.106 jdc } else
480 1.106 jdc if_statinc(ifp, if_oerrors);
481 1.106 jdc mutex_exit(&sc->sc_iflock);
482 1.1 thorpej }
483 1.1 thorpej
484 1.1 thorpej /*
485 1.106 jdc * Invoke the transmit workqueue and transmission on the interface.
486 1.1 thorpej */
487 1.5 matthias static void
488 1.106 jdc se_send_worker(struct work *wk, void *cookie)
489 1.1 thorpej {
490 1.106 jdc struct ifnet *ifp = cookie;
491 1.1 thorpej struct se_softc *sc = ifp->if_softc;
492 1.1 thorpej struct scsi_ctron_ether_generic send_cmd;
493 1.1 thorpej struct mbuf *m, *m0;
494 1.1 thorpej int len, error;
495 1.55 reinoud u_char *cp;
496 1.1 thorpej
497 1.106 jdc mutex_enter(&sc->sc_iflock);
498 1.106 jdc sc->sc_send_work_pending = false;
499 1.106 jdc mutex_exit(&sc->sc_iflock);
500 1.106 jdc
501 1.106 jdc KASSERT(if_is_mpsafe(ifp));
502 1.106 jdc
503 1.1 thorpej /* Don't transmit if interface is busy or not running */
504 1.100 msaitoh if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
505 1.1 thorpej return;
506 1.1 thorpej
507 1.106 jdc while (1) {
508 1.106 jdc IFQ_DEQUEUE(&ifp->if_snd, m0);
509 1.106 jdc if (m0 == 0)
510 1.106 jdc break;
511 1.105 jdc
512 1.106 jdc /* If BPF is listening on this interface, let it see the
513 1.106 jdc * packet before we commit it to the wire.
514 1.106 jdc */
515 1.106 jdc bpf_mtap(ifp, m0, BPF_D_OUT);
516 1.106 jdc
517 1.106 jdc /* We need to use m->m_pkthdr.len, so require the header */
518 1.106 jdc if ((m0->m_flags & M_PKTHDR) == 0)
519 1.106 jdc panic("ctscstart: no header mbuf");
520 1.106 jdc len = m0->m_pkthdr.len;
521 1.106 jdc
522 1.106 jdc /* Mark the interface busy. */
523 1.106 jdc ifp->if_flags |= IFF_OACTIVE;
524 1.106 jdc
525 1.106 jdc /* Chain; copy into linear buffer allocated at attach time. */
526 1.106 jdc cp = sc->sc_tbuf;
527 1.106 jdc for (m = m0; m != NULL; ) {
528 1.106 jdc memcpy(cp, mtod(m, u_char *), m->m_len);
529 1.106 jdc cp += m->m_len;
530 1.106 jdc m = m0 = m_free(m);
531 1.106 jdc }
532 1.106 jdc if (len < SEMINSIZE) {
533 1.1 thorpej #ifdef SEDEBUG
534 1.106 jdc if (sc->sc_debug)
535 1.106 jdc printf("se: packet size %d (%zu) < %d\n", len,
536 1.106 jdc cp - (u_char *)sc->sc_tbuf, SEMINSIZE);
537 1.1 thorpej #endif
538 1.106 jdc memset(cp, 0, SEMINSIZE - len);
539 1.106 jdc len = SEMINSIZE;
540 1.106 jdc }
541 1.106 jdc
542 1.106 jdc /* Fill out SCSI command. */
543 1.106 jdc PROTOCMD(ctron_ether_send, send_cmd);
544 1.106 jdc _lto2b(len, send_cmd.length);
545 1.106 jdc
546 1.106 jdc /* Send command to device. */
547 1.106 jdc error = se_scsipi_cmd(sc->sc_periph,
548 1.106 jdc (void *)&send_cmd, sizeof(send_cmd),
549 1.106 jdc sc->sc_tbuf, len, SERETRIES,
550 1.106 jdc SETIMEOUT, NULL, XS_CTL_NOSLEEP | XS_CTL_DATA_OUT);
551 1.106 jdc if (error) {
552 1.106 jdc aprint_error_dev(sc->sc_dev,
553 1.106 jdc "not queued, error %d\n", error);
554 1.106 jdc if_statinc(ifp, if_oerrors);
555 1.106 jdc ifp->if_flags &= ~IFF_OACTIVE;
556 1.106 jdc } else
557 1.106 jdc if_statinc(ifp, if_opackets);
558 1.1 thorpej }
559 1.1 thorpej }
560 1.1 thorpej
561 1.1 thorpej
562 1.1 thorpej /*
563 1.1 thorpej * Called from the scsibus layer via our scsi device switch.
564 1.1 thorpej */
565 1.4 mycroft static void
566 1.75 dsl sedone(struct scsipi_xfer *xs, int error)
567 1.1 thorpej {
568 1.85 chs struct se_softc *sc = device_private(xs->xs_periph->periph_dev);
569 1.9 bouyer struct scsipi_generic *cmd = xs->cmd;
570 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
571 1.4 mycroft
572 1.100 msaitoh if (IS_SEND(cmd)) {
573 1.106 jdc ifp->if_flags &= ~IFF_OACTIVE;
574 1.100 msaitoh } else if (IS_RECV(cmd)) {
575 1.4 mycroft /* RECV complete */
576 1.4 mycroft /* pass data up. reschedule a recv */
577 1.9 bouyer /* scsipi_free_xs will call start. Harmless. */
578 1.4 mycroft if (error) {
579 1.4 mycroft /* Reschedule after a delay */
580 1.28 thorpej callout_reset(&sc->sc_recv_ch, se_poll,
581 1.106 jdc se_recv_callout, (void *)sc);
582 1.4 mycroft } else {
583 1.11 matthias int n, ntimeo;
584 1.4 mycroft n = se_read(sc, xs->data, xs->datalen - xs->resid);
585 1.105 jdc #ifdef SE_DEBUG
586 1.11 matthias if (n > se_max_received)
587 1.11 matthias se_max_received = n;
588 1.105 jdc #endif
589 1.11 matthias if (n == 0)
590 1.11 matthias ntimeo = se_poll;
591 1.11 matthias else if (n >= RDATA_MAX)
592 1.11 matthias ntimeo = se_poll0;
593 1.11 matthias else {
594 1.11 matthias ntimeo = sc->sc_last_timeout;
595 1.11 matthias ntimeo = (ntimeo * RDATA_GOAL)/n;
596 1.11 matthias ntimeo = (ntimeo < se_poll0?
597 1.11 matthias se_poll0: ntimeo);
598 1.11 matthias ntimeo = (ntimeo > se_poll?
599 1.11 matthias se_poll: ntimeo);
600 1.1 thorpej }
601 1.11 matthias sc->sc_last_timeout = ntimeo;
602 1.105 jdc callout_reset(&sc->sc_recv_ch, ntimeo,
603 1.106 jdc se_recv_callout, (void *)sc);
604 1.1 thorpej }
605 1.1 thorpej }
606 1.1 thorpej }
607 1.10 enami
608 1.106 jdc /*
609 1.106 jdc * Setup a receive command by queuing the work.
610 1.106 jdc * Usually called from a callout, but also from se_init().
611 1.106 jdc */
612 1.5 matthias static void
613 1.106 jdc se_recv_callout(void *v)
614 1.1 thorpej {
615 1.1 thorpej /* do a recv command */
616 1.1 thorpej struct se_softc *sc = (struct se_softc *) v;
617 1.1 thorpej
618 1.21 thorpej if (sc->sc_enabled == 0)
619 1.21 thorpej return;
620 1.21 thorpej
621 1.106 jdc mutex_enter(&sc->sc_iflock);
622 1.106 jdc if (sc->sc_recv_work_pending == true) {
623 1.106 jdc callout_reset(&sc->sc_recv_ch, se_poll,
624 1.106 jdc se_recv_callout, (void *)sc);
625 1.106 jdc return;
626 1.106 jdc }
627 1.106 jdc
628 1.106 jdc sc->sc_recv_work_pending = true;
629 1.106 jdc workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work, NULL);
630 1.106 jdc mutex_exit(&sc->sc_iflock);
631 1.106 jdc }
632 1.106 jdc
633 1.106 jdc /*
634 1.106 jdc * Invoke the receive workqueue
635 1.106 jdc */
636 1.106 jdc static void
637 1.106 jdc se_recv_worker(struct work *wk, void *cookie)
638 1.106 jdc {
639 1.106 jdc struct se_softc *sc = (struct se_softc *) cookie;
640 1.106 jdc
641 1.106 jdc mutex_enter(&sc->sc_iflock);
642 1.106 jdc sc->sc_recv_work_pending = false;
643 1.106 jdc mutex_exit(&sc->sc_iflock);
644 1.106 jdc se_recv(sc);
645 1.106 jdc
646 1.106 jdc }
647 1.106 jdc
648 1.106 jdc /*
649 1.106 jdc * Do the actual work of receiving data.
650 1.106 jdc */
651 1.106 jdc static void
652 1.106 jdc se_recv(struct se_softc *sc)
653 1.106 jdc {
654 1.106 jdc struct scsi_ctron_ether_recv recv_cmd;
655 1.106 jdc int error;
656 1.106 jdc
657 1.106 jdc /* do a recv command */
658 1.1 thorpej PROTOCMD(ctron_ether_recv, recv_cmd);
659 1.1 thorpej
660 1.34 bouyer error = se_scsipi_cmd(sc->sc_periph,
661 1.50 mycroft (void *)&recv_cmd, sizeof(recv_cmd),
662 1.10 enami sc->sc_rbuf, RBUF_LEN, SERETRIES, SETIMEOUT, NULL,
663 1.105 jdc XS_CTL_NOSLEEP | XS_CTL_DATA_IN);
664 1.1 thorpej if (error)
665 1.106 jdc callout_reset(&sc->sc_recv_ch, se_poll,
666 1.106 jdc se_recv_callout, (void *)sc);
667 1.1 thorpej }
668 1.1 thorpej
669 1.1 thorpej /*
670 1.1 thorpej * We copy the data into mbufs. When full cluster sized units are present
671 1.1 thorpej * we copy into clusters.
672 1.1 thorpej */
673 1.1 thorpej static struct mbuf *
674 1.75 dsl se_get(struct se_softc *sc, char *data, int totlen)
675 1.1 thorpej {
676 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
677 1.23 mycroft struct mbuf *m, *m0, *newm;
678 1.23 mycroft int len;
679 1.1 thorpej
680 1.23 mycroft MGETHDR(m0, M_DONTWAIT, MT_DATA);
681 1.23 mycroft if (m0 == 0)
682 1.1 thorpej return (0);
683 1.90 ozaki m_set_rcvif(m0, ifp);
684 1.23 mycroft m0->m_pkthdr.len = totlen;
685 1.23 mycroft len = MHLEN;
686 1.23 mycroft m = m0;
687 1.1 thorpej
688 1.1 thorpej while (totlen > 0) {
689 1.6 mycroft if (totlen >= MINCLSIZE) {
690 1.1 thorpej MCLGET(m, M_DONTWAIT);
691 1.23 mycroft if ((m->m_flags & M_EXT) == 0)
692 1.23 mycroft goto bad;
693 1.6 mycroft len = MCLBYTES;
694 1.1 thorpej }
695 1.23 mycroft
696 1.23 mycroft if (m == m0) {
697 1.65 yamt char *newdata = (char *)
698 1.23 mycroft ALIGN(m->m_data + sizeof(struct ether_header)) -
699 1.23 mycroft sizeof(struct ether_header);
700 1.23 mycroft len -= newdata - m->m_data;
701 1.23 mycroft m->m_data = newdata;
702 1.23 mycroft }
703 1.23 mycroft
704 1.98 riastrad m->m_len = len = uimin(totlen, len);
705 1.64 christos memcpy(mtod(m, void *), data, len);
706 1.1 thorpej data += len;
707 1.23 mycroft
708 1.1 thorpej totlen -= len;
709 1.23 mycroft if (totlen > 0) {
710 1.23 mycroft MGET(newm, M_DONTWAIT, MT_DATA);
711 1.23 mycroft if (newm == 0)
712 1.23 mycroft goto bad;
713 1.23 mycroft len = MLEN;
714 1.23 mycroft m = m->m_next = newm;
715 1.23 mycroft }
716 1.1 thorpej }
717 1.1 thorpej
718 1.23 mycroft return (m0);
719 1.23 mycroft
720 1.23 mycroft bad:
721 1.23 mycroft m_freem(m0);
722 1.23 mycroft return (0);
723 1.1 thorpej }
724 1.1 thorpej
725 1.1 thorpej /*
726 1.1 thorpej * Pass packets to higher levels.
727 1.1 thorpej */
728 1.1 thorpej static int
729 1.75 dsl se_read(struct se_softc *sc, char *data, int datalen)
730 1.1 thorpej {
731 1.1 thorpej struct mbuf *m;
732 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
733 1.1 thorpej int n;
734 1.1 thorpej
735 1.1 thorpej n = 0;
736 1.1 thorpej while (datalen >= 2) {
737 1.1 thorpej int len = _2btol(data);
738 1.1 thorpej data += 2;
739 1.1 thorpej datalen -= 2;
740 1.1 thorpej
741 1.1 thorpej if (len == 0)
742 1.1 thorpej break;
743 1.1 thorpej #ifdef SEDEBUG
744 1.1 thorpej if (sc->sc_debug) {
745 1.1 thorpej printf("se_read: datalen = %d, packetlen = %d, proto = 0x%04x\n", datalen, len,
746 1.1 thorpej ntohs(((struct ether_header *)data)->ether_type));
747 1.1 thorpej }
748 1.1 thorpej #endif
749 1.1 thorpej if (len <= sizeof(struct ether_header) ||
750 1.1 thorpej len > MAX_SNAP) {
751 1.1 thorpej #ifdef SEDEBUG
752 1.1 thorpej printf("%s: invalid packet size %d; dropping\n",
753 1.85 chs device_xname(sc->sc_dev), len);
754 1.1 thorpej #endif
755 1.104 thorpej if_statinc(ifp, if_ierrors);
756 1.1 thorpej goto next_packet;
757 1.1 thorpej }
758 1.1 thorpej
759 1.1 thorpej /* Don't need crc. Must keep ether header for BPF */
760 1.1 thorpej m = se_get(sc, data, len - ETHER_CRC);
761 1.1 thorpej if (m == 0) {
762 1.1 thorpej #ifdef SEDEBUG
763 1.10 enami if (sc->sc_debug)
764 1.1 thorpej printf("se_read: se_get returned null\n");
765 1.1 thorpej #endif
766 1.104 thorpej if_statinc(ifp, if_ierrors);
767 1.1 thorpej goto next_packet;
768 1.1 thorpej }
769 1.1 thorpej if ((ifp->if_flags & IFF_PROMISC) != 0) {
770 1.5 matthias m_adj(m, SE_PREFIX);
771 1.1 thorpej }
772 1.1 thorpej
773 1.24 thorpej /* Pass the packet up. */
774 1.105 jdc if_percpuq_enqueue(sc->sc_ipq, m);
775 1.1 thorpej
776 1.1 thorpej next_packet:
777 1.1 thorpej data += len;
778 1.1 thorpej datalen -= len;
779 1.1 thorpej n++;
780 1.1 thorpej }
781 1.10 enami return (n);
782 1.1 thorpej }
783 1.1 thorpej
784 1.1 thorpej
785 1.5 matthias static void
786 1.75 dsl sewatchdog(struct ifnet *ifp)
787 1.1 thorpej {
788 1.1 thorpej struct se_softc *sc = ifp->if_softc;
789 1.1 thorpej
790 1.85 chs log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
791 1.104 thorpej if_statinc(ifp, if_oerrors);
792 1.1 thorpej
793 1.1 thorpej se_reset(sc);
794 1.1 thorpej }
795 1.1 thorpej
796 1.106 jdc static void
797 1.75 dsl se_reset(struct se_softc *sc)
798 1.1 thorpej {
799 1.1 thorpej #if 0
800 1.1 thorpej /* Maybe we don't *really* want to reset the entire bus
801 1.1 thorpej * because the ctron isn't working. We would like to send a
802 1.1 thorpej * "BUS DEVICE RESET" message, but don't think the ctron
803 1.1 thorpej * understands it.
804 1.1 thorpej */
805 1.106 jdc se_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, SERETRIES, 2000, NULL,
806 1.25 thorpej XS_CTL_RESET);
807 1.1 thorpej #endif
808 1.106 jdc se_init(sc);
809 1.1 thorpej }
810 1.1 thorpej
811 1.5 matthias static int
812 1.75 dsl se_add_proto(struct se_softc *sc, int proto)
813 1.1 thorpej {
814 1.10 enami int error;
815 1.1 thorpej struct scsi_ctron_ether_generic add_proto_cmd;
816 1.100 msaitoh uint8_t data[2];
817 1.1 thorpej _lto2b(proto, data);
818 1.1 thorpej #ifdef SEDEBUG
819 1.1 thorpej if (sc->sc_debug)
820 1.1 thorpej printf("se: adding proto 0x%02x%02x\n", data[0], data[1]);
821 1.1 thorpej #endif
822 1.1 thorpej
823 1.1 thorpej PROTOCMD(ctron_ether_add_proto, add_proto_cmd);
824 1.1 thorpej _lto2b(sizeof(data), add_proto_cmd.length);
825 1.34 bouyer error = se_scsipi_cmd(sc->sc_periph,
826 1.50 mycroft (void *)&add_proto_cmd, sizeof(add_proto_cmd),
827 1.30 enami data, sizeof(data), SERETRIES, SETIMEOUT, NULL,
828 1.79 rmind XS_CTL_DATA_OUT);
829 1.10 enami return (error);
830 1.1 thorpej }
831 1.1 thorpej
832 1.5 matthias static int
833 1.100 msaitoh se_get_addr(struct se_softc *sc, uint8_t *myaddr)
834 1.1 thorpej {
835 1.10 enami int error;
836 1.1 thorpej struct scsi_ctron_ether_generic get_addr_cmd;
837 1.1 thorpej
838 1.1 thorpej PROTOCMD(ctron_ether_get_addr, get_addr_cmd);
839 1.1 thorpej _lto2b(ETHER_ADDR_LEN, get_addr_cmd.length);
840 1.34 bouyer error = se_scsipi_cmd(sc->sc_periph,
841 1.50 mycroft (void *)&get_addr_cmd, sizeof(get_addr_cmd),
842 1.30 enami myaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
843 1.79 rmind XS_CTL_DATA_IN);
844 1.85 chs printf("%s: ethernet address %s\n", device_xname(sc->sc_dev),
845 1.1 thorpej ether_sprintf(myaddr));
846 1.10 enami return (error);
847 1.1 thorpej }
848 1.1 thorpej
849 1.1 thorpej
850 1.5 matthias static int
851 1.75 dsl se_set_media(struct se_softc *sc, int type)
852 1.1 thorpej {
853 1.10 enami int error;
854 1.1 thorpej struct scsi_ctron_ether_generic set_media_cmd;
855 1.1 thorpej
856 1.1 thorpej PROTOCMD(ctron_ether_set_media, set_media_cmd);
857 1.1 thorpej set_media_cmd.byte3 = type;
858 1.34 bouyer error = se_scsipi_cmd(sc->sc_periph,
859 1.50 mycroft (void *)&set_media_cmd, sizeof(set_media_cmd),
860 1.10 enami 0, 0, SERETRIES, SETIMEOUT, NULL, 0);
861 1.10 enami return (error);
862 1.1 thorpej }
863 1.1 thorpej
864 1.5 matthias static int
865 1.75 dsl se_set_mode(struct se_softc *sc, int len, int mode)
866 1.1 thorpej {
867 1.10 enami int error;
868 1.1 thorpej struct scsi_ctron_ether_set_mode set_mode_cmd;
869 1.1 thorpej
870 1.1 thorpej PROTOCMD(ctron_ether_set_mode, set_mode_cmd);
871 1.1 thorpej set_mode_cmd.mode = mode;
872 1.1 thorpej _lto2b(len, set_mode_cmd.length);
873 1.34 bouyer error = se_scsipi_cmd(sc->sc_periph,
874 1.50 mycroft (void *)&set_mode_cmd, sizeof(set_mode_cmd),
875 1.10 enami 0, 0, SERETRIES, SETIMEOUT, NULL, 0);
876 1.10 enami return (error);
877 1.1 thorpej }
878 1.1 thorpej
879 1.1 thorpej
880 1.5 matthias static int
881 1.75 dsl se_init(struct se_softc *sc)
882 1.1 thorpej {
883 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
884 1.1 thorpej struct scsi_ctron_ether_generic set_addr_cmd;
885 1.70 dyoung uint8_t enaddr[ETHER_ADDR_LEN];
886 1.1 thorpej int error;
887 1.1 thorpej
888 1.1 thorpej if (ifp->if_flags & IFF_PROMISC) {
889 1.1 thorpej error = se_set_mode(sc, MAX_SNAP, 1);
890 1.1 thorpej }
891 1.1 thorpej else
892 1.1 thorpej error = se_set_mode(sc, ETHERMTU + sizeof(struct ether_header),
893 1.10 enami 0);
894 1.5 matthias if (error != 0)
895 1.10 enami return (error);
896 1.5 matthias
897 1.1 thorpej PROTOCMD(ctron_ether_set_addr, set_addr_cmd);
898 1.1 thorpej _lto2b(ETHER_ADDR_LEN, set_addr_cmd.length);
899 1.70 dyoung memcpy(enaddr, CLLADDR(ifp->if_sadl), sizeof(enaddr));
900 1.34 bouyer error = se_scsipi_cmd(sc->sc_periph,
901 1.50 mycroft (void *)&set_addr_cmd, sizeof(set_addr_cmd),
902 1.70 dyoung enaddr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL,
903 1.25 thorpej XS_CTL_DATA_OUT);
904 1.10 enami if (error != 0)
905 1.10 enami return (error);
906 1.1 thorpej
907 1.1 thorpej if ((sc->protos & PROTO_IP) &&
908 1.1 thorpej (error = se_add_proto(sc, ETHERTYPE_IP)) != 0)
909 1.10 enami return (error);
910 1.1 thorpej if ((sc->protos & PROTO_ARP) &&
911 1.1 thorpej (error = se_add_proto(sc, ETHERTYPE_ARP)) != 0)
912 1.10 enami return (error);
913 1.1 thorpej if ((sc->protos & PROTO_REVARP) &&
914 1.1 thorpej (error = se_add_proto(sc, ETHERTYPE_REVARP)) != 0)
915 1.10 enami return (error);
916 1.5 matthias #ifdef NETATALK
917 1.5 matthias if ((sc->protos & PROTO_AT) &&
918 1.20 kim (error = se_add_proto(sc, ETHERTYPE_ATALK)) != 0)
919 1.10 enami return (error);
920 1.5 matthias if ((sc->protos & PROTO_AARP) &&
921 1.5 matthias (error = se_add_proto(sc, ETHERTYPE_AARP)) != 0)
922 1.10 enami return (error);
923 1.5 matthias #endif
924 1.1 thorpej
925 1.100 msaitoh if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == IFF_UP) {
926 1.1 thorpej ifp->if_flags |= IFF_RUNNING;
927 1.106 jdc mutex_enter(&sc->sc_iflock);
928 1.106 jdc sc->sc_recv_work_pending = true;
929 1.106 jdc workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work, NULL);
930 1.106 jdc mutex_exit(&sc->sc_iflock);
931 1.1 thorpej ifp->if_flags &= ~IFF_OACTIVE;
932 1.106 jdc mutex_enter(&sc->sc_iflock);
933 1.106 jdc workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work, NULL);
934 1.106 jdc mutex_exit(&sc->sc_iflock);
935 1.1 thorpej }
936 1.10 enami return (error);
937 1.1 thorpej }
938 1.1 thorpej
939 1.5 matthias static int
940 1.100 msaitoh se_set_multi(struct se_softc *sc, uint8_t *addr)
941 1.1 thorpej {
942 1.1 thorpej struct scsi_ctron_ether_generic set_multi_cmd;
943 1.1 thorpej int error;
944 1.1 thorpej
945 1.1 thorpej if (sc->sc_debug)
946 1.85 chs printf("%s: set_set_multi: %s\n", device_xname(sc->sc_dev),
947 1.1 thorpej ether_sprintf(addr));
948 1.1 thorpej
949 1.1 thorpej PROTOCMD(ctron_ether_set_multi, set_multi_cmd);
950 1.106 jdc _lto2b(ETHER_ADDR_LEN, set_multi_cmd.length);
951 1.34 bouyer error = se_scsipi_cmd(sc->sc_periph,
952 1.50 mycroft (void *)&set_multi_cmd, sizeof(set_multi_cmd),
953 1.106 jdc addr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
954 1.10 enami return (error);
955 1.1 thorpej }
956 1.1 thorpej
957 1.5 matthias static int
958 1.100 msaitoh se_remove_multi(struct se_softc *sc, uint8_t *addr)
959 1.1 thorpej {
960 1.1 thorpej struct scsi_ctron_ether_generic remove_multi_cmd;
961 1.1 thorpej int error;
962 1.1 thorpej
963 1.1 thorpej if (sc->sc_debug)
964 1.85 chs printf("%s: se_remove_multi: %s\n", device_xname(sc->sc_dev),
965 1.1 thorpej ether_sprintf(addr));
966 1.1 thorpej
967 1.1 thorpej PROTOCMD(ctron_ether_remove_multi, remove_multi_cmd);
968 1.106 jdc _lto2b(ETHER_ADDR_LEN, remove_multi_cmd.length);
969 1.34 bouyer error = se_scsipi_cmd(sc->sc_periph,
970 1.50 mycroft (void *)&remove_multi_cmd, sizeof(remove_multi_cmd),
971 1.106 jdc addr, ETHER_ADDR_LEN, SERETRIES, SETIMEOUT, NULL, XS_CTL_DATA_OUT);
972 1.10 enami return (error);
973 1.1 thorpej }
974 1.1 thorpej
975 1.1 thorpej #if 0 /* not used --thorpej */
976 1.5 matthias static int
977 1.75 dsl sc_set_all_multi(struct se_softc *sc, int set)
978 1.1 thorpej {
979 1.1 thorpej int error = 0;
980 1.100 msaitoh uint8_t *addr;
981 1.100 msaitoh struct ethercom *ec = &sc->sc_ethercom;
982 1.1 thorpej struct ether_multi *enm;
983 1.1 thorpej struct ether_multistep step;
984 1.10 enami
985 1.102 msaitoh ETHER_LOCK(ec);
986 1.100 msaitoh ETHER_FIRST_MULTI(step, ec, enm);
987 1.1 thorpej while (enm != NULL) {
988 1.1 thorpej if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
989 1.1 thorpej /*
990 1.1 thorpej * We must listen to a range of multicast addresses.
991 1.1 thorpej * For now, just accept all multicasts, rather than
992 1.1 thorpej * trying to set only those filter bits needed to match
993 1.1 thorpej * the range. (At this time, the only use of address
994 1.1 thorpej * ranges is for IP multicast routing, for which the
995 1.1 thorpej * range is big enough to require all bits set.)
996 1.1 thorpej */
997 1.1 thorpej /* We have no way of adding a range to this device.
998 1.1 thorpej * stepping through all addresses in the range is
999 1.1 thorpej * typically not possible. The only real alternative
1000 1.1 thorpej * is to go into promicuous mode and filter by hand.
1001 1.1 thorpej */
1002 1.102 msaitoh ETHER_UNLOCK(ec);
1003 1.10 enami return (ENODEV);
1004 1.1 thorpej
1005 1.1 thorpej }
1006 1.1 thorpej
1007 1.1 thorpej addr = enm->enm_addrlo;
1008 1.10 enami if ((error = set ? se_set_multi(sc, addr) :
1009 1.10 enami se_remove_multi(sc, addr)) != 0)
1010 1.10 enami return (error);
1011 1.1 thorpej ETHER_NEXT_MULTI(step, enm);
1012 1.1 thorpej }
1013 1.102 msaitoh ETHER_UNLOCK(ec);
1014 1.102 msaitoh
1015 1.10 enami return (error);
1016 1.1 thorpej }
1017 1.1 thorpej #endif /* not used */
1018 1.1 thorpej
1019 1.5 matthias static void
1020 1.75 dsl se_stop(struct se_softc *sc)
1021 1.1 thorpej {
1022 1.10 enami
1023 1.1 thorpej /* Don't schedule any reads */
1024 1.28 thorpej callout_stop(&sc->sc_recv_ch);
1025 1.5 matthias
1026 1.106 jdc /* Wait for the workqueues to finish */
1027 1.106 jdc mutex_enter(&sc->sc_iflock);
1028 1.106 jdc workqueue_wait(sc->sc_recv_wq, &sc->sc_recv_work);
1029 1.106 jdc workqueue_wait(sc->sc_send_wq, &sc->sc_send_work);
1030 1.106 jdc mutex_exit(&sc->sc_iflock);
1031 1.106 jdc
1032 1.105 jdc /* Abort any scsi cmds in progress */
1033 1.105 jdc mutex_enter(chan_mtx(sc->sc_periph->periph_channel));
1034 1.105 jdc scsipi_kill_pending(sc->sc_periph);
1035 1.105 jdc mutex_exit(chan_mtx(sc->sc_periph->periph_channel));
1036 1.1 thorpej }
1037 1.1 thorpej
1038 1.1 thorpej
1039 1.1 thorpej /*
1040 1.1 thorpej * Process an ioctl request.
1041 1.1 thorpej */
1042 1.5 matthias static int
1043 1.73 dyoung se_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1044 1.1 thorpej {
1045 1.29 augustss struct se_softc *sc = ifp->if_softc;
1046 1.1 thorpej struct ifaddr *ifa = (struct ifaddr *)data;
1047 1.1 thorpej struct ifreq *ifr = (struct ifreq *)data;
1048 1.68 dyoung struct sockaddr *sa;
1049 1.105 jdc int error = 0;
1050 1.1 thorpej
1051 1.1 thorpej
1052 1.1 thorpej switch (cmd) {
1053 1.1 thorpej
1054 1.73 dyoung case SIOCINITIFADDR:
1055 1.105 jdc mutex_enter(&sc->sc_iflock);
1056 1.21 thorpej if ((error = se_enable(sc)) != 0)
1057 1.21 thorpej break;
1058 1.1 thorpej ifp->if_flags |= IFF_UP;
1059 1.105 jdc mutex_exit(&sc->sc_iflock);
1060 1.1 thorpej
1061 1.83 mbalmer if ((error = se_set_media(sc, CMEDIA_AUTOSENSE)) != 0)
1062 1.21 thorpej break;
1063 1.5 matthias
1064 1.1 thorpej switch (ifa->ifa_addr->sa_family) {
1065 1.1 thorpej #ifdef INET
1066 1.1 thorpej case AF_INET:
1067 1.1 thorpej sc->protos |= (PROTO_IP | PROTO_ARP | PROTO_REVARP);
1068 1.1 thorpej if ((error = se_init(sc)) != 0)
1069 1.1 thorpej break;
1070 1.1 thorpej arp_ifinit(ifp, ifa);
1071 1.1 thorpej break;
1072 1.1 thorpej #endif
1073 1.5 matthias #ifdef NETATALK
1074 1.5 matthias case AF_APPLETALK:
1075 1.5 matthias sc->protos |= (PROTO_AT | PROTO_AARP);
1076 1.5 matthias if ((error = se_init(sc)) != 0)
1077 1.5 matthias break;
1078 1.5 matthias break;
1079 1.5 matthias #endif
1080 1.1 thorpej default:
1081 1.1 thorpej error = se_init(sc);
1082 1.1 thorpej break;
1083 1.1 thorpej }
1084 1.1 thorpej break;
1085 1.1 thorpej
1086 1.1 thorpej
1087 1.1 thorpej case SIOCSIFFLAGS:
1088 1.73 dyoung if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1089 1.73 dyoung break;
1090 1.73 dyoung /* XXX re-use ether_ioctl() */
1091 1.100 msaitoh switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1092 1.73 dyoung case IFF_RUNNING:
1093 1.1 thorpej /*
1094 1.1 thorpej * If interface is marked down and it is running, then
1095 1.1 thorpej * stop it.
1096 1.1 thorpej */
1097 1.1 thorpej se_stop(sc);
1098 1.105 jdc mutex_enter(&sc->sc_iflock);
1099 1.1 thorpej ifp->if_flags &= ~IFF_RUNNING;
1100 1.21 thorpej se_disable(sc);
1101 1.105 jdc mutex_exit(&sc->sc_iflock);
1102 1.73 dyoung break;
1103 1.73 dyoung case IFF_UP:
1104 1.1 thorpej /*
1105 1.1 thorpej * If interface is marked up and it is stopped, then
1106 1.1 thorpej * start it.
1107 1.1 thorpej */
1108 1.105 jdc mutex_enter(&sc->sc_iflock);
1109 1.105 jdc error = se_enable(sc);
1110 1.105 jdc mutex_exit(&sc->sc_iflock);
1111 1.105 jdc if (error)
1112 1.21 thorpej break;
1113 1.1 thorpej error = se_init(sc);
1114 1.73 dyoung break;
1115 1.73 dyoung default:
1116 1.1 thorpej /*
1117 1.1 thorpej * Reset the interface to pick up changes in any other
1118 1.1 thorpej * flags that affect hardware registers.
1119 1.1 thorpej */
1120 1.73 dyoung if (sc->sc_enabled)
1121 1.73 dyoung error = se_init(sc);
1122 1.73 dyoung break;
1123 1.1 thorpej }
1124 1.1 thorpej #ifdef SEDEBUG
1125 1.1 thorpej if (ifp->if_flags & IFF_DEBUG)
1126 1.1 thorpej sc->sc_debug = 1;
1127 1.1 thorpej else
1128 1.1 thorpej sc->sc_debug = 0;
1129 1.1 thorpej #endif
1130 1.1 thorpej break;
1131 1.1 thorpej
1132 1.1 thorpej case SIOCADDMULTI:
1133 1.1 thorpej case SIOCDELMULTI:
1134 1.105 jdc mutex_enter(&sc->sc_iflock);
1135 1.103 chs sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_WAITOK);
1136 1.105 jdc mutex_exit(&sc->sc_iflock);
1137 1.67 dyoung if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1138 1.51 thorpej if (ifp->if_flags & IFF_RUNNING) {
1139 1.51 thorpej error = (cmd == SIOCADDMULTI) ?
1140 1.68 dyoung se_set_multi(sc, sa->sa_data) :
1141 1.68 dyoung se_remove_multi(sc, sa->sa_data);
1142 1.51 thorpej } else
1143 1.51 thorpej error = 0;
1144 1.21 thorpej }
1145 1.105 jdc mutex_enter(&sc->sc_iflock);
1146 1.68 dyoung sockaddr_free(sa);
1147 1.105 jdc mutex_exit(&sc->sc_iflock);
1148 1.1 thorpej break;
1149 1.1 thorpej
1150 1.1 thorpej default:
1151 1.1 thorpej
1152 1.73 dyoung error = ether_ioctl(ifp, cmd, data);
1153 1.1 thorpej break;
1154 1.1 thorpej }
1155 1.1 thorpej
1156 1.1 thorpej return (error);
1157 1.1 thorpej }
1158 1.1 thorpej
1159 1.21 thorpej /*
1160 1.21 thorpej * Enable the network interface.
1161 1.21 thorpej */
1162 1.21 thorpej int
1163 1.75 dsl se_enable(struct se_softc *sc)
1164 1.21 thorpej {
1165 1.34 bouyer struct scsipi_periph *periph = sc->sc_periph;
1166 1.34 bouyer struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1167 1.21 thorpej int error = 0;
1168 1.21 thorpej
1169 1.105 jdc if (sc->sc_enabled == 0) {
1170 1.105 jdc if ((error = scsipi_adapter_addref(adapt)) == 0)
1171 1.105 jdc sc->sc_enabled = 1;
1172 1.105 jdc else
1173 1.105 jdc aprint_error_dev(sc->sc_dev, "device enable failed\n");
1174 1.105 jdc }
1175 1.21 thorpej return (error);
1176 1.21 thorpej }
1177 1.21 thorpej
1178 1.21 thorpej /*
1179 1.21 thorpej * Disable the network interface.
1180 1.21 thorpej */
1181 1.21 thorpej void
1182 1.75 dsl se_disable(struct se_softc *sc)
1183 1.21 thorpej {
1184 1.34 bouyer struct scsipi_periph *periph = sc->sc_periph;
1185 1.34 bouyer struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1186 1.21 thorpej
1187 1.21 thorpej if (sc->sc_enabled != 0) {
1188 1.34 bouyer scsipi_adapter_delref(adapt);
1189 1.21 thorpej sc->sc_enabled = 0;
1190 1.21 thorpej }
1191 1.21 thorpej }
1192 1.21 thorpej
1193 1.1 thorpej #define SEUNIT(z) (minor(z))
1194 1.1 thorpej /*
1195 1.1 thorpej * open the device.
1196 1.1 thorpej */
1197 1.1 thorpej int
1198 1.76 dsl seopen(dev_t dev, int flag, int fmt, struct lwp *l)
1199 1.1 thorpej {
1200 1.21 thorpej int unit, error;
1201 1.1 thorpej struct se_softc *sc;
1202 1.34 bouyer struct scsipi_periph *periph;
1203 1.34 bouyer struct scsipi_adapter *adapt;
1204 1.1 thorpej
1205 1.1 thorpej unit = SEUNIT(dev);
1206 1.72 tsutsui sc = device_lookup_private(&se_cd, unit);
1207 1.10 enami if (sc == NULL)
1208 1.10 enami return (ENXIO);
1209 1.5 matthias
1210 1.34 bouyer periph = sc->sc_periph;
1211 1.34 bouyer adapt = periph->periph_channel->chan_adapter;
1212 1.1 thorpej
1213 1.34 bouyer if ((error = scsipi_adapter_addref(adapt)) != 0)
1214 1.21 thorpej return (error);
1215 1.21 thorpej
1216 1.34 bouyer SC_DEBUG(periph, SCSIPI_DB1,
1217 1.74 cegger ("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
1218 1.10 enami se_cd.cd_ndevs));
1219 1.1 thorpej
1220 1.34 bouyer periph->periph_flags |= PERIPH_OPEN;
1221 1.1 thorpej
1222 1.34 bouyer SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
1223 1.10 enami return (0);
1224 1.1 thorpej }
1225 1.1 thorpej
1226 1.1 thorpej /*
1227 1.1 thorpej * close the device.. only called if we are the LAST
1228 1.1 thorpej * occurence of an open device
1229 1.1 thorpej */
1230 1.1 thorpej int
1231 1.76 dsl seclose(dev_t dev, int flag, int fmt, struct lwp *l)
1232 1.1 thorpej {
1233 1.72 tsutsui struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
1234 1.34 bouyer struct scsipi_periph *periph = sc->sc_periph;
1235 1.34 bouyer struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1236 1.1 thorpej
1237 1.34 bouyer SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
1238 1.22 thorpej
1239 1.34 bouyer scsipi_wait_drain(periph);
1240 1.22 thorpej
1241 1.34 bouyer scsipi_adapter_delref(adapt);
1242 1.34 bouyer periph->periph_flags &= ~PERIPH_OPEN;
1243 1.1 thorpej
1244 1.10 enami return (0);
1245 1.1 thorpej }
1246 1.1 thorpej
1247 1.1 thorpej /*
1248 1.1 thorpej * Perform special action on behalf of the user
1249 1.1 thorpej * Only does generic scsi ioctls.
1250 1.1 thorpej */
1251 1.1 thorpej int
1252 1.75 dsl seioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1253 1.1 thorpej {
1254 1.72 tsutsui struct se_softc *sc = device_lookup_private(&se_cd, SEUNIT(dev));
1255 1.1 thorpej
1256 1.59 christos return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
1257 1.1 thorpej }
1258