if_dse.c revision 1.3 1 1.3 nat /* $NetBSD: if_dse.c,v 1.3 2022/12/22 23:06:11 nat Exp $ */
2 1.1 nat
3 1.1 nat /*
4 1.1 nat * Driver for DaynaPORT SCSI/Link SCSI-Ethernet
5 1.1 nat *
6 1.1 nat * Written by Hiroshi Noguchi <ngc (at) ff.iij4u.or.jp>
7 1.1 nat *
8 1.1 nat * Modified by Matt Sandstrom <mattias (at) beauty.se> for NetBSD 1.5.3
9 1.1 nat *
10 1.1 nat * This driver is written based on "if_se.c".
11 1.1 nat */
12 1.1 nat
13 1.1 nat /*
14 1.1 nat * Copyright (c) 1997 Ian W. Dall <ian.dall (at) dsto.defence.gov.au>
15 1.1 nat * All rights reserved.
16 1.1 nat *
17 1.1 nat * Redistribution and use in source and binary forms, with or without
18 1.1 nat * modification, are permitted provided that the following conditions
19 1.1 nat * are met:
20 1.1 nat * 1. Redistributions of source code must retain the above copyright
21 1.1 nat * notice, this list of conditions and the following disclaimer.
22 1.1 nat * 2. Redistributions in binary form must reproduce the above copyright
23 1.1 nat * notice, this list of conditions and the following disclaimer in the
24 1.1 nat * documentation and/or other materials provided with the distribution.
25 1.1 nat * 3. All advertising materials mentioning features or use of this software
26 1.1 nat * must display the following acknowledgement:
27 1.1 nat * This product includes software developed by Ian W. Dall.
28 1.1 nat * 4. The name of the author may not be used to endorse or promote products
29 1.1 nat * derived from this software without specific prior written permission.
30 1.1 nat *
31 1.1 nat * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 1.1 nat * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 1.1 nat * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 1.1 nat * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 1.1 nat * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 1.1 nat * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 1.1 nat * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 1.1 nat * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 1.1 nat * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 1.1 nat * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 1.1 nat */
42 1.1 nat
43 1.1 nat
44 1.1 nat
45 1.1 nat #include "opt_inet.h"
46 1.1 nat #include "opt_net_mpsafe.h"
47 1.1 nat #include "opt_atalk.h"
48 1.1 nat
49 1.1 nat #include <sys/types.h>
50 1.1 nat #include <sys/param.h>
51 1.1 nat #include <sys/systm.h>
52 1.1 nat #include <sys/callout.h>
53 1.1 nat #include <sys/syslog.h>
54 1.1 nat #include <sys/kernel.h>
55 1.1 nat #include <sys/file.h>
56 1.1 nat #include <sys/stat.h>
57 1.1 nat #include <sys/ioctl.h>
58 1.1 nat #include <sys/buf.h>
59 1.1 nat #include <sys/uio.h>
60 1.1 nat #include <sys/malloc.h>
61 1.1 nat #include <sys/errno.h>
62 1.1 nat #include <sys/device.h>
63 1.1 nat #include <sys/disklabel.h>
64 1.1 nat #include <sys/disk.h>
65 1.1 nat #include <sys/proc.h>
66 1.1 nat #include <sys/conf.h>
67 1.1 nat
68 1.1 nat #include <sys/workqueue.h>
69 1.1 nat
70 1.1 nat #include <dev/scsipi/scsipi_all.h>
71 1.1 nat #include <dev/scsipi/scsiconf.h>
72 1.1 nat
73 1.1 nat #include <sys/mbuf.h>
74 1.1 nat
75 1.1 nat #include <sys/socket.h>
76 1.1 nat #include <net/if.h>
77 1.1 nat #include <net/if_dl.h>
78 1.1 nat #include <net/if_ether.h>
79 1.1 nat #include <net/if_media.h>
80 1.1 nat
81 1.1 nat #ifdef INET
82 1.1 nat #include <netinet/in.h>
83 1.1 nat #include <netinet/if_inarp.h>
84 1.1 nat #endif
85 1.1 nat
86 1.1 nat #ifdef NETATALK
87 1.1 nat #include <netatalk/at.h>
88 1.1 nat #endif
89 1.1 nat
90 1.1 nat #include <net/bpf.h>
91 1.1 nat
92 1.1 nat
93 1.1 nat /*
94 1.1 nat * debug flag
95 1.1 nat */
96 1.1 nat #if 0
97 1.1 nat #define DSE_DEBUG
98 1.1 nat #endif
99 1.1 nat
100 1.1 nat
101 1.1 nat #define DSE_TIMEOUT 100000
102 1.1 nat #define DSE_OUTSTANDING 4
103 1.1 nat #define DSE_RETRIES 4
104 1.1 nat #define DSE_MINSIZE 60
105 1.1 nat
106 1.1 nat #define DSE_HEADER_TX 4
107 1.1 nat #define DSE_TAIL_TX 4
108 1.1 nat #define DSE_EXTRAS_TX (DSE_HEADER_TX + DSE_TAIL_TX)
109 1.1 nat
110 1.1 nat #define DSE_HEADER_RX 6
111 1.1 nat #define DSE_TAIL_RX 0
112 1.1 nat #define DSE_EXTRAS_RX (DSE_HEADER_RX + DSE_TAIL_RX)
113 1.1 nat
114 1.1 nat #define MAX_BYTES_RX (ETHERMTU + sizeof(struct ether_header) + ETHER_CRC_LEN)
115 1.1 nat
116 1.1 nat /* 10 full length packets appears to be the max ever returned. 16k is OK */
117 1.1 nat #define RBUF_LEN (16 * 1024)
118 1.1 nat
119 1.1 nat /*
120 1.1 nat * Tuning parameters:
121 1.1 nat * We will attempt to adapt to polling fast enough to get RDATA_GOAL packets
122 1.1 nat * per read
123 1.1 nat */
124 1.1 nat #define RDATA_MAX 10 /* maximum of returned packets (guessed) */
125 1.1 nat #define RDATA_GOAL 8
126 1.1 nat
127 1.1 nat /*
128 1.1 nat * maximum of available multicast address entries (guessed)
129 1.1 nat */
130 1.1 nat #define DSE_MCAST_MAX 10
131 1.1 nat
132 1.1 nat
133 1.1 nat /* dse_poll and dse_poll0 are the normal polling rate and the minimum
134 1.1 nat * polling rate respectively. dse_poll0 should be chosen so that at
135 1.1 nat * maximum ethernet speed, we will read nearly RDATA_MAX packets. dse_poll
136 1.1 nat * should be chosen for reasonable maximum latency.
137 1.1 nat * In practice, if we are being saturated with min length packets, we
138 1.1 nat * can't poll fast enough. Polling with zero delay actually
139 1.1 nat * worsens performance. dse_poll0 is enforced to be always at least 1
140 1.1 nat */
141 1.1 nat #if MAC68K_DEBUG
142 1.1 nat #define DSE_POLL 50 /* default in milliseconds */
143 1.1 nat #define DSE_POLL0 30 /* default in milliseconds */
144 1.1 nat #else
145 1.1 nat #define DSE_POLL 80 /* default in milliseconds */
146 1.1 nat #define DSE_POLL0 40 /* default in milliseconds */
147 1.1 nat #endif
148 1.1 nat int dse_poll = 0; /* Delay in ticks set at attach time */
149 1.1 nat int dse_poll0 = 0;
150 1.1 nat int dse_max_received = 0; /* Instrumentation */
151 1.1 nat
152 1.1 nat
153 1.1 nat
154 1.1 nat
155 1.1 nat /*==========================================
156 1.1 nat data type defs
157 1.1 nat ==========================================*/
158 1.1 nat typedef struct scsipi_inquiry_data dayna_ether_inquiry_data;
159 1.1 nat
160 1.1 nat typedef struct {
161 1.1 nat uint8_t opcode[2];
162 1.1 nat uint8_t byte3;
163 1.1 nat uint8_t length[2];
164 1.1 nat uint8_t byte6;
165 1.1 nat } scsi_dayna_ether_generic;
166 1.1 nat
167 1.1 nat #define DAYNA_CMD_SEND 0x0A /* same as generic "Write" */
168 1.1 nat #define DAYNA_CMD_RECV 0x08 /* same as generic "Read" */
169 1.1 nat
170 1.1 nat #define DAYNA_CMD_GET_ADDR 0x09 /* ???: read MAC address ? */
171 1.1 nat #define REQ_LEN_GET_ADDR 0x12
172 1.1 nat
173 1.1 nat #define DAYNA_CMD_SET_MULTI 0x0D /* set multicast address */
174 1.1 nat
175 1.1 nat #define DAYNA_CMD_VENDOR1 0x0E /* ???: initialize signal ? */
176 1.1 nat
177 1.1 nat #define IS_SEND(generic) ((generic)->opcode == DAYNA_CMD_SEND)
178 1.1 nat #define IS_RECV(generic) ((generic)->opcode == DAYNA_CMD_RECV)
179 1.1 nat
180 1.1 nat struct dse_softc {
181 1.1 nat device_t sc_dev;
182 1.1 nat struct ethercom sc_ethercom; /* Ethernet common part */
183 1.1 nat struct scsipi_periph *sc_periph;/* contains our targ, lun, etc. */
184 1.1 nat
185 1.1 nat struct callout sc_recv_ch;
186 1.1 nat struct kmutex sc_iflock;
187 1.1 nat struct if_percpuq *sc_ipq;
188 1.1 nat struct workqueue *sc_recv_wq, *sc_send_wq;
189 1.1 nat struct work sc_recv_work, sc_send_work;
190 1.1 nat int sc_recv_work_pending, sc_send_work_pending;
191 1.1 nat
192 1.1 nat char *sc_tbuf;
193 1.1 nat char *sc_rbuf;
194 1.1 nat int sc_debug;
195 1.1 nat int sc_flags;
196 1.1 nat int sc_last_timeout;
197 1.1 nat int sc_enabled;
198 1.1 nat int sc_attach_state;
199 1.1 nat };
200 1.1 nat
201 1.1 nat /* bit defs of "sc_flags" */
202 1.1 nat #define DSE_NEED_RECV 0x1
203 1.1 nat
204 1.1 nat static int dsematch(device_t, cfdata_t, void *);
205 1.1 nat static void dseattach(device_t, device_t, void *);
206 1.1 nat static int dsedetach(device_t, int);
207 1.1 nat
208 1.1 nat static void dse_ifstart(struct ifnet *);
209 1.1 nat static void dse_send_worker(struct work *wk, void *cookie);
210 1.1 nat
211 1.1 nat static void dsedone(struct scsipi_xfer *, int);
212 1.1 nat static int dse_ioctl(struct ifnet *, u_long, void *);
213 1.1 nat static void dsewatchdog(struct ifnet *);
214 1.1 nat
215 1.1 nat static void dse_recv_callout(void *);
216 1.1 nat static void dse_recv_worker(struct work *wk, void *cookie);
217 1.1 nat static void dse_recv(struct dse_softc *);
218 1.1 nat static struct mbuf* dse_get(struct dse_softc *, uint8_t *, int);
219 1.1 nat static int dse_read(struct dse_softc *, uint8_t *, int);
220 1.1 nat
221 1.1 nat static int dse_init_adaptor(struct dse_softc *);
222 1.1 nat static int dse_get_addr(struct dse_softc *, uint8_t *);
223 1.1 nat static int dse_set_multi(struct dse_softc *);
224 1.1 nat
225 1.1 nat static int dse_reset(struct dse_softc *);
226 1.1 nat
227 1.1 nat #if 0 /* 07/16/2000 comment-out */
228 1.1 nat static int dse_set_mode(struct dse_softc *, int, int);
229 1.1 nat #endif
230 1.1 nat static int dse_init(struct dse_softc *);
231 1.1 nat static void dse_stop(struct dse_softc *);
232 1.1 nat
233 1.1 nat #if 0
234 1.1 nat static __inline uint16_t ether_cmp(void *, void *);
235 1.1 nat #endif
236 1.1 nat
237 1.1 nat static inline int dse_scsipi_cmd(struct scsipi_periph *periph,
238 1.1 nat struct scsipi_generic *scsipi_cmd,
239 1.1 nat int cmdlen, u_char *data_addr, int datalen,
240 1.1 nat int retries, int timeout, struct buf *bp,
241 1.1 nat int flags);
242 1.1 nat
243 1.1 nat int dse_enable(struct dse_softc *);
244 1.1 nat void dse_disable(struct dse_softc *);
245 1.1 nat
246 1.1 nat
247 1.1 nat CFATTACH_DECL_NEW(dse, sizeof(struct dse_softc),
248 1.1 nat dsematch, dseattach, dsedetach, NULL);
249 1.1 nat
250 1.1 nat extern struct cfdriver dse_cd;
251 1.1 nat
252 1.1 nat dev_type_open(dseopen);
253 1.1 nat dev_type_close(dseclose);
254 1.1 nat dev_type_ioctl(dseioctl);
255 1.1 nat
256 1.1 nat const struct cdevsw dse_cdevsw = {
257 1.1 nat .d_open = dseopen,
258 1.1 nat .d_close = dseclose,
259 1.1 nat .d_read = noread,
260 1.1 nat .d_write = nowrite,
261 1.1 nat .d_ioctl = dseioctl,
262 1.1 nat .d_stop = nostop,
263 1.1 nat .d_tty = notty,
264 1.1 nat .d_poll = nopoll,
265 1.1 nat .d_mmap = nommap,
266 1.1 nat .d_kqfilter = nokqfilter,
267 1.1 nat .d_discard = nodiscard,
268 1.1 nat .d_flag = D_OTHER | D_MPSAFE
269 1.1 nat };
270 1.1 nat
271 1.1 nat const struct scsipi_periphsw dse_switch = {
272 1.1 nat
273 1.1 nat NULL, /* Use default error handler */
274 1.1 nat NULL, /* have no queue */
275 1.1 nat NULL, /* have no async handler */
276 1.1 nat dsedone, /* deal with stats at interrupt time */
277 1.1 nat };
278 1.1 nat
279 1.1 nat struct scsipi_inquiry_pattern dse_patterns[] = {
280 1.1 nat { T_PROCESSOR, T_FIXED,
281 1.1 nat "Dayna", "SCSI/Link", "" },
282 1.1 nat };
283 1.1 nat
284 1.1 nat
285 1.1 nat
286 1.1 nat /*====================================================
287 1.1 nat definitions for SCSI commands
288 1.1 nat ====================================================*/
289 1.1 nat
290 1.1 nat /*
291 1.1 nat * command templates
292 1.1 nat */
293 1.1 nat /* unknown commands */
294 1.1 nat /* Vendor #1 */
295 1.1 nat static const scsi_dayna_ether_generic sonic_ether_vendor1 = {
296 1.1 nat { DAYNA_CMD_VENDOR1, 0x00 },
297 1.1 nat 0x00,
298 1.1 nat { 0x00, 0x00 },
299 1.1 nat 0x80
300 1.1 nat };
301 1.1 nat
302 1.1 nat
303 1.1 nat
304 1.1 nat #if 0
305 1.1 nat /*
306 1.1 nat * Compare two Ether/802 addredses for equality, inlined and
307 1.1 nat * unrolled for speed.
308 1.1 nat * Note: use this like memcmp()
309 1.1 nat */
310 1.1 nat static __inline uint16_t
311 1.1 nat ether_cmp(void *one, void *two)
312 1.1 nat {
313 1.1 nat uint16_t* a;
314 1.1 nat uint16_t* b;
315 1.1 nat uint16_t diff;
316 1.1 nat
317 1.1 nat a = (uint16_t *) one;
318 1.1 nat b = (uint16_t *) two;
319 1.1 nat
320 1.1 nat diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
321 1.1 nat
322 1.1 nat return (diff);
323 1.1 nat }
324 1.1 nat
325 1.1 nat #define ETHER_CMP ether_cmp
326 1.1 nat #endif
327 1.1 nat
328 1.1 nat /*
329 1.1 nat * check to match with SCSI inquiry information
330 1.1 nat */
331 1.1 nat static int
332 1.1 nat dsematch(device_t parent, cfdata_t match, void *aux)
333 1.1 nat {
334 1.1 nat struct scsipibus_attach_args *sa = aux;
335 1.1 nat int priority;
336 1.1 nat
337 1.1 nat (void)scsipi_inqmatch(&sa->sa_inqbuf,
338 1.1 nat dse_patterns, sizeof(dse_patterns) / sizeof(dse_patterns[0]),
339 1.1 nat sizeof(dse_patterns[0]), &priority);
340 1.1 nat return priority;
341 1.1 nat }
342 1.1 nat
343 1.1 nat
344 1.1 nat /*
345 1.1 nat * The routine called by the low level scsi routine when it discovers
346 1.1 nat * a device suitable for this driver.
347 1.1 nat */
348 1.1 nat static void
349 1.1 nat dseattach(device_t parent, device_t self, void *aux)
350 1.1 nat {
351 1.1 nat struct dse_softc *sc = device_private(self);
352 1.1 nat struct scsipibus_attach_args *sa = aux;
353 1.1 nat struct scsipi_periph *periph = sa->sa_periph;
354 1.1 nat struct ifnet *ifp = &sc->sc_ethercom.ec_if;
355 1.1 nat uint8_t myaddr[ETHER_ADDR_LEN];
356 1.1 nat char wqname[MAXCOMLEN];
357 1.1 nat int rv;
358 1.1 nat
359 1.1 nat sc->sc_dev = self;
360 1.1 nat
361 1.1 nat aprint_normal("\n");
362 1.1 nat SC_DEBUG(periph, SCSIPI_DB2, ("dseattach: "));
363 1.1 nat
364 1.1 nat sc->sc_attach_state = 0;
365 1.1 nat callout_init(&sc->sc_recv_ch, CALLOUT_MPSAFE);
366 1.1 nat callout_setfunc(&sc->sc_recv_ch, dse_recv_callout, (void *)sc);
367 1.1 nat mutex_init(&sc->sc_iflock, MUTEX_DEFAULT, IPL_SOFTNET);
368 1.1 nat
369 1.1 nat /*
370 1.1 nat * Store information needed to contact our base driver
371 1.1 nat */
372 1.1 nat sc->sc_periph = periph;
373 1.1 nat periph->periph_dev = sc->sc_dev;
374 1.1 nat periph->periph_switch = &dse_switch;
375 1.1 nat #if 0
376 1.1 nat sc_periph->sc_link_dbflags = SCSIPI_DB1;
377 1.1 nat #endif
378 1.1 nat
379 1.1 nat dse_poll = mstohz(DSE_POLL);
380 1.1 nat dse_poll = dse_poll? dse_poll: 1;
381 1.1 nat dse_poll0 = mstohz(DSE_POLL0);
382 1.1 nat dse_poll0 = dse_poll0? dse_poll0: 1;
383 1.1 nat
384 1.1 nat /*
385 1.1 nat * Initialize and attach send and receive buffers
386 1.1 nat */
387 1.1 nat sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header) +
388 1.1 nat DSE_EXTRAS_TX + 16, M_DEVBUF, M_WAITOK);
389 1.1 nat
390 1.1 nat sc->sc_rbuf = malloc(RBUF_LEN + 16, M_DEVBUF, M_WAITOK);
391 1.1 nat
392 1.1 nat /* initialize adaptor and obtain MAC address */
393 1.1 nat dse_init_adaptor(sc);
394 1.1 nat sc->sc_attach_state = 1;
395 1.1 nat
396 1.1 nat /* Initialize ifnet structure. */
397 1.1 nat strcpy(ifp->if_xname, device_xname(sc->sc_dev));
398 1.1 nat ifp->if_softc = sc;
399 1.1 nat ifp->if_start = dse_ifstart;
400 1.1 nat ifp->if_ioctl = dse_ioctl;
401 1.1 nat ifp->if_watchdog = dsewatchdog;
402 1.1 nat ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
403 1.1 nat ifp->if_extflags = IFEF_MPSAFE;
404 1.1 nat
405 1.1 nat dse_get_addr(sc, myaddr);
406 1.1 nat
407 1.1 nat /* Attach the interface. */
408 1.1 nat if_initialize(ifp);
409 1.1 nat
410 1.1 nat snprintf(wqname, sizeof(wqname), "%sRx", device_xname(sc->sc_dev));
411 1.1 nat rv = workqueue_create(&sc->sc_recv_wq, wqname, dse_recv_worker, sc,
412 1.1 nat PRI_SOFTNET, IPL_NET, WQ_MPSAFE);
413 1.1 nat if (rv != 0) {
414 1.1 nat aprint_error_dev(sc->sc_dev,
415 1.1 nat "unable to create recv Rx workqueue\n");
416 1.1 nat dsedetach(sc->sc_dev, 0);
417 1.1 nat return; /* Error */
418 1.1 nat }
419 1.1 nat sc->sc_recv_work_pending = false;
420 1.1 nat sc->sc_attach_state = 2;
421 1.1 nat
422 1.1 nat snprintf(wqname, sizeof(wqname), "%sTx", device_xname(sc->sc_dev));
423 1.1 nat rv = workqueue_create(&sc->sc_send_wq, wqname, dse_send_worker, ifp,
424 1.1 nat PRI_SOFTNET, IPL_NET, WQ_MPSAFE);
425 1.1 nat if (rv != 0) {
426 1.1 nat aprint_error_dev(sc->sc_dev,
427 1.1 nat "unable to create send Tx workqueue\n");
428 1.1 nat dsedetach(sc->sc_dev, 0);
429 1.1 nat return; /* Error */
430 1.1 nat }
431 1.1 nat sc->sc_send_work_pending = false;
432 1.1 nat sc->sc_ipq = if_percpuq_create(&sc->sc_ethercom.ec_if);
433 1.1 nat ether_ifattach(ifp, myaddr);
434 1.1 nat if_register(ifp);
435 1.1 nat sc->sc_attach_state = 4;
436 1.1 nat
437 1.1 nat bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
438 1.1 nat }
439 1.1 nat
440 1.1 nat static int
441 1.1 nat dsedetach(device_t self, int flags)
442 1.1 nat {
443 1.1 nat struct dse_softc *sc = device_private(self);
444 1.1 nat struct ifnet *ifp = &sc->sc_ethercom.ec_if;
445 1.1 nat
446 1.1 nat switch(sc->sc_attach_state) {
447 1.1 nat case 4:
448 1.1 nat dse_stop(sc);
449 1.1 nat mutex_enter(&sc->sc_iflock);
450 1.1 nat ifp->if_flags &= ~IFF_RUNNING;
451 1.1 nat dse_disable(sc);
452 1.1 nat ether_ifdetach(ifp);
453 1.1 nat if_detach(ifp);
454 1.1 nat mutex_exit(&sc->sc_iflock);
455 1.1 nat if_percpuq_destroy(sc->sc_ipq);
456 1.1 nat /*FALLTHROUGH*/
457 1.1 nat case 3:
458 1.1 nat workqueue_destroy(sc->sc_send_wq);
459 1.1 nat /*FALLTHROUGH*/
460 1.1 nat case 2:
461 1.1 nat workqueue_destroy(sc->sc_recv_wq);
462 1.1 nat /*FALLTHROUGH*/
463 1.1 nat case 1:
464 1.1 nat free(sc->sc_rbuf, M_DEVBUF);
465 1.1 nat free(sc->sc_tbuf, M_DEVBUF);
466 1.1 nat callout_destroy(&sc->sc_recv_ch);
467 1.1 nat mutex_destroy(&sc->sc_iflock);
468 1.1 nat break;
469 1.1 nat default:
470 1.1 nat aprint_error_dev(sc->sc_dev, "detach failed (state %d)\n",
471 1.1 nat sc->sc_attach_state);
472 1.1 nat return 1;
473 1.1 nat break;
474 1.1 nat }
475 1.1 nat
476 1.1 nat return 0;
477 1.1 nat }
478 1.1 nat
479 1.1 nat
480 1.1 nat /*
481 1.1 nat * submit SCSI command
482 1.1 nat */
483 1.1 nat static __inline int
484 1.1 nat dse_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd,
485 1.1 nat int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
486 1.1 nat struct buf *bp, int flags)
487 1.1 nat {
488 1.1 nat int error = 0;
489 1.1 nat
490 1.1 nat error = scsipi_command(periph, cmd, cmdlen, data_addr,
491 1.1 nat datalen, retries, timeout, bp, flags);
492 1.1 nat
493 1.1 nat return error;
494 1.1 nat }
495 1.1 nat
496 1.1 nat
497 1.1 nat /*
498 1.1 nat * Start routine for calling from network sub system
499 1.1 nat */
500 1.1 nat static void
501 1.1 nat dse_ifstart(struct ifnet *ifp)
502 1.1 nat {
503 1.1 nat struct dse_softc *sc = ifp->if_softc;
504 1.1 nat
505 1.1 nat mutex_enter(&sc->sc_iflock);
506 1.1 nat if (!sc->sc_send_work_pending) {
507 1.1 nat sc->sc_send_work_pending = true;
508 1.1 nat workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work, NULL);
509 1.1 nat }
510 1.1 nat mutex_exit(&sc->sc_iflock);
511 1.1 nat if (sc->sc_flags & DSE_NEED_RECV) {
512 1.1 nat sc->sc_flags &= ~DSE_NEED_RECV;
513 1.1 nat }
514 1.1 nat }
515 1.1 nat
516 1.1 nat /*
517 1.1 nat * Invoke the transmit workqueue and transmission on the interface.
518 1.1 nat */
519 1.1 nat static void
520 1.1 nat dse_send_worker(struct work *wk, void *cookie)
521 1.1 nat {
522 1.1 nat struct ifnet *ifp = cookie;
523 1.1 nat struct dse_softc *sc = ifp->if_softc;
524 1.1 nat scsi_dayna_ether_generic cmd_send;
525 1.1 nat struct mbuf *m, *m0;
526 1.1 nat int len, error;
527 1.1 nat u_char *cp;
528 1.1 nat
529 1.1 nat mutex_enter(&sc->sc_iflock);
530 1.1 nat sc->sc_send_work_pending = false;
531 1.1 nat mutex_exit(&sc->sc_iflock);
532 1.1 nat
533 1.1 nat KASSERT(if_is_mpsafe(ifp));
534 1.1 nat
535 1.1 nat /* Don't transmit if interface is busy or not running */
536 1.1 nat if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
537 1.1 nat return;
538 1.1 nat
539 1.1 nat while (1) {
540 1.1 nat IFQ_DEQUEUE(&ifp->if_snd, m0);
541 1.1 nat if (m0 == NULL)
542 1.1 nat break;
543 1.1 nat /* If BPF is listening on this interface, let it see the
544 1.1 nat * packet before we commit it to the wire.
545 1.1 nat */
546 1.1 nat bpf_mtap(ifp, m0, BPF_D_OUT);
547 1.1 nat
548 1.1 nat /* We need to use m->m_pkthdr.len, so require the header */
549 1.1 nat if ((m0->m_flags & M_PKTHDR) == 0)
550 1.1 nat panic("ctscstart: no header mbuf");
551 1.1 nat len = m0->m_pkthdr.len;
552 1.1 nat
553 1.1 nat /* Mark the interface busy. */
554 1.1 nat ifp->if_flags |= IFF_OACTIVE;
555 1.1 nat
556 1.1 nat /* Chain; copy into linear buffer allocated at attach time. */
557 1.1 nat cp = sc->sc_tbuf;
558 1.1 nat for (m = m0; m != NULL; ) {
559 1.1 nat memcpy(cp, mtod(m, u_char *), m->m_len);
560 1.1 nat cp += m->m_len;
561 1.1 nat m = m0 = m_free(m);
562 1.1 nat }
563 1.1 nat if (len < DSE_MINSIZE) {
564 1.2 nat #ifdef DSE_DEBUG
565 1.1 nat if (sc->sc_debug)
566 1.2 nat aprint_error_dev(sc->sc_dev,
567 1.1 nat "packet size %d (%zu) < %d\n", len,
568 1.2 nat cp - (u_char *)sc->sc_tbuf, DSE_MINSIZE);
569 1.1 nat #endif
570 1.1 nat memset(cp, 0, DSE_MINSIZE - len);
571 1.1 nat len = DSE_MINSIZE;
572 1.1 nat }
573 1.1 nat
574 1.1 nat /* Fill out SCSI command. */
575 1.1 nat memset(&cmd_send, 0, sizeof(cmd_send));
576 1.1 nat cmd_send.opcode[0] = DAYNA_CMD_SEND;
577 1.1 nat _lto2b(len, &(cmd_send.length[0]));
578 1.1 nat cmd_send.byte6 = 0x00;
579 1.1 nat
580 1.1 nat /* Send command to device. */
581 1.1 nat error = dse_scsipi_cmd(sc->sc_periph,
582 1.1 nat (void *)&cmd_send, sizeof(cmd_send),
583 1.1 nat sc->sc_tbuf, len, DSE_RETRIES,
584 1.1 nat DSE_TIMEOUT, NULL, XS_CTL_NOSLEEP | XS_CTL_POLL |
585 1.1 nat XS_CTL_DATA_OUT);
586 1.1 nat if (error) {
587 1.1 nat aprint_error_dev(sc->sc_dev,
588 1.1 nat "not queued, error %d\n", error);
589 1.1 nat if_statinc(ifp, if_oerrors);
590 1.1 nat ifp->if_flags &= ~IFF_OACTIVE;
591 1.1 nat } else
592 1.1 nat if_statinc(ifp, if_opackets);
593 1.1 nat }
594 1.1 nat }
595 1.1 nat
596 1.1 nat
597 1.1 nat /*
598 1.1 nat * Called from the scsibus layer via our scsi device switch.
599 1.1 nat */
600 1.1 nat static void
601 1.1 nat dsedone(struct scsipi_xfer *xs, int error)
602 1.1 nat {
603 1.1 nat struct dse_softc *sc = device_private(xs->xs_periph->periph_dev);
604 1.1 nat struct scsipi_generic *cmd = xs->cmd;
605 1.1 nat struct ifnet *ifp = &sc->sc_ethercom.ec_if;
606 1.1 nat
607 1.1 nat if (IS_SEND(cmd)) {
608 1.1 nat ifp->if_flags &= ~IFF_OACTIVE;
609 1.1 nat } else if (IS_RECV(cmd)) {
610 1.1 nat /* RECV complete */
611 1.1 nat /* pass data up. reschedule a recv */
612 1.1 nat /* scsipi_free_xs will call start. Harmless. */
613 1.1 nat
614 1.1 nat if (error) {
615 1.1 nat /* Reschedule after a delay */
616 1.1 nat callout_schedule(&sc->sc_recv_ch, dse_poll);
617 1.1 nat } else {
618 1.1 nat int n, ntimeo;
619 1.1 nat n = dse_read(sc, xs->data, xs->datalen - xs->resid);
620 1.1 nat if (n > dse_max_received)
621 1.1 nat dse_max_received = n;
622 1.1 nat if (n == 0)
623 1.1 nat ntimeo = dse_poll;
624 1.1 nat else if (n >= RDATA_MAX)
625 1.1 nat ntimeo = dse_poll0;
626 1.1 nat else {
627 1.1 nat ntimeo = sc->sc_last_timeout;
628 1.1 nat ntimeo = (ntimeo * RDATA_GOAL)/n;
629 1.1 nat ntimeo = (ntimeo < dse_poll0?
630 1.1 nat dse_poll0: ntimeo);
631 1.1 nat ntimeo = (ntimeo > dse_poll?
632 1.1 nat dse_poll: ntimeo);
633 1.1 nat }
634 1.1 nat sc->sc_last_timeout = ntimeo;
635 1.1 nat callout_schedule(&sc->sc_recv_ch, ntimeo);
636 1.1 nat }
637 1.1 nat }
638 1.1 nat }
639 1.1 nat
640 1.1 nat
641 1.1 nat /*
642 1.1 nat * Setup a receive command by queuing the work.
643 1.1 nat * Usually called from a callout, but also from se_init().
644 1.1 nat */
645 1.1 nat static void
646 1.1 nat dse_recv_callout(void *v)
647 1.1 nat {
648 1.1 nat /* do a recv command */
649 1.1 nat struct dse_softc *sc = (struct dse_softc *) v;
650 1.1 nat
651 1.1 nat if (sc->sc_enabled == 0)
652 1.1 nat return;
653 1.1 nat
654 1.1 nat mutex_enter(&sc->sc_iflock);
655 1.1 nat if (sc->sc_recv_work_pending == true) {
656 1.1 nat callout_schedule(&sc->sc_recv_ch, dse_poll);
657 1.1 nat mutex_exit(&sc->sc_iflock);
658 1.1 nat return;
659 1.1 nat }
660 1.1 nat
661 1.1 nat sc->sc_recv_work_pending = true;
662 1.1 nat workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work, NULL);
663 1.1 nat mutex_exit(&sc->sc_iflock);
664 1.1 nat }
665 1.1 nat
666 1.1 nat /*
667 1.1 nat * Invoke the receive workqueue
668 1.1 nat */
669 1.1 nat static void
670 1.1 nat dse_recv_worker(struct work *wk, void *cookie)
671 1.1 nat {
672 1.1 nat struct dse_softc *sc = (struct dse_softc *) cookie;
673 1.1 nat
674 1.1 nat dse_recv(sc);
675 1.1 nat mutex_enter(&sc->sc_iflock);
676 1.1 nat sc->sc_recv_work_pending = false;
677 1.1 nat mutex_exit(&sc->sc_iflock);
678 1.1 nat
679 1.1 nat }
680 1.1 nat
681 1.1 nat /*
682 1.1 nat * Do the actual work of receiving data.
683 1.1 nat */
684 1.1 nat static void
685 1.1 nat dse_recv(struct dse_softc *sc)
686 1.1 nat {
687 1.1 nat scsi_dayna_ether_generic cmd_recv;
688 1.1 nat int error, len;
689 1.1 nat
690 1.1 nat /* do a recv command */
691 1.1 nat /* fill out command buffer */
692 1.1 nat memset(&cmd_recv, 0, sizeof(cmd_recv));
693 1.1 nat cmd_recv.opcode[0] = DAYNA_CMD_RECV;
694 1.1 nat len = MAX_BYTES_RX + DSE_EXTRAS_RX;
695 1.1 nat _lto2b(len, &(cmd_recv.length[0]));
696 1.1 nat cmd_recv.byte6 = 0xC0;
697 1.1 nat
698 1.1 nat error = dse_scsipi_cmd(sc->sc_periph,
699 1.1 nat (void *)&cmd_recv, sizeof(cmd_recv),
700 1.1 nat sc->sc_rbuf, RBUF_LEN, DSE_RETRIES, DSE_TIMEOUT, NULL,
701 1.1 nat XS_CTL_NOSLEEP | XS_CTL_POLL | XS_CTL_DATA_IN);
702 1.1 nat if (error)
703 1.1 nat callout_schedule(&sc->sc_recv_ch, dse_poll);
704 1.1 nat }
705 1.1 nat
706 1.1 nat
707 1.1 nat /*
708 1.1 nat * We copy the data into mbufs. When full cluster sized units are present
709 1.1 nat * we copy into clusters.
710 1.1 nat */
711 1.1 nat static struct mbuf *
712 1.1 nat dse_get(struct dse_softc *sc, uint8_t *data, int totlen)
713 1.1 nat {
714 1.1 nat struct ifnet *ifp = &sc->sc_ethercom.ec_if;
715 1.1 nat struct mbuf *m, *m0, *newm;
716 1.1 nat int len;
717 1.1 nat
718 1.1 nat MGETHDR(m0, M_DONTWAIT, MT_DATA);
719 1.1 nat if (m0 == NULL)
720 1.1 nat return NULL;
721 1.1 nat
722 1.1 nat m_set_rcvif(m0, ifp);
723 1.1 nat m0->m_pkthdr.len = totlen;
724 1.1 nat len = MHLEN;
725 1.1 nat m = m0;
726 1.1 nat
727 1.1 nat while (totlen > 0) {
728 1.1 nat if (totlen >= MINCLSIZE) {
729 1.1 nat MCLGET(m, M_DONTWAIT);
730 1.1 nat if((m->m_flags & M_EXT) == 0)
731 1.1 nat goto bad;
732 1.1 nat
733 1.1 nat len = MCLBYTES;
734 1.1 nat }
735 1.1 nat
736 1.1 nat if (m == m0) {
737 1.1 nat char *newdata = (char *)
738 1.1 nat ALIGN(m->m_data + sizeof(struct ether_header)) -
739 1.1 nat sizeof(struct ether_header);
740 1.1 nat len -= newdata - m->m_data;
741 1.1 nat m->m_data = newdata;
742 1.1 nat }
743 1.1 nat
744 1.1 nat m->m_len = len = uimin(totlen, len);
745 1.1 nat memcpy(mtod(m, void *), data, len);
746 1.1 nat data += len;
747 1.1 nat
748 1.1 nat totlen -= len;
749 1.1 nat if (totlen > 0) {
750 1.1 nat MGET(newm, M_DONTWAIT, MT_DATA);
751 1.1 nat if (newm == NULL)
752 1.1 nat goto bad;
753 1.1 nat
754 1.1 nat len = MLEN;
755 1.1 nat m = m->m_next = newm;
756 1.1 nat }
757 1.1 nat }
758 1.1 nat
759 1.1 nat return m0;
760 1.1 nat
761 1.1 nat bad:
762 1.1 nat m_freem(m0);
763 1.1 nat return NULL ;
764 1.1 nat }
765 1.1 nat
766 1.1 nat
767 1.1 nat #ifdef MAC68K_DEBUG
768 1.1 nat static int
769 1.1 nat peek_packet(uint8_t* buf)
770 1.1 nat {
771 1.1 nat struct ether_header *eh;
772 1.1 nat uint16_t type;
773 1.1 nat int len;
774 1.1 nat
775 1.1 nat eh = (struct ether_header*)buf;
776 1.1 nat type = _2btol((uint8_t*)&(eh->ether_type));
777 1.1 nat
778 1.1 nat len = sizeof(struct ether_header);
779 1.1 nat
780 1.1 nat if (type <= ETHERMTU) {
781 1.1 nat /* for 802.3 */
782 1.1 nat len += type;
783 1.1 nat } else{
784 1.1 nat /* for Ethernet II (DIX) */
785 1.1 nat switch (type) {
786 1.1 nat case ETHERTYPE_ARP:
787 1.1 nat len += 28;
788 1.1 nat break;
789 1.1 nat case ETHERTYPE_IP:
790 1.1 nat len += _2btol(buf + sizeof(struct ether_header) + 2);
791 1.1 nat break;
792 1.1 nat default:
793 1.1 nat len = 0;
794 1.1 nat goto l_end;
795 1.1 nat break;
796 1.1 nat }
797 1.1 nat }
798 1.1 nat if (len < DSE_MINSIZE) {
799 1.1 nat len = DSE_MINSIZE;
800 1.1 nat }
801 1.1 nat len += ETHER_CRC_LEN;
802 1.1 nat
803 1.1 nat l_end:;
804 1.1 nat return len;
805 1.1 nat }
806 1.1 nat #endif
807 1.1 nat
808 1.1 nat
809 1.1 nat /*
810 1.1 nat * Pass packets to higher levels.
811 1.1 nat */
812 1.1 nat static int
813 1.1 nat dse_read(struct dse_softc *sc, uint8_t *data, int datalen)
814 1.1 nat {
815 1.1 nat struct mbuf *m;
816 1.1 nat struct ifnet *ifp = &sc->sc_ethercom.ec_if;
817 1.1 nat int len;
818 1.1 nat int n;
819 1.1 nat #ifdef MAC68K_DEBUG
820 1.1 nat int peek_flag = 1;
821 1.1 nat #endif
822 1.1 nat
823 1.1 nat mutex_enter(&sc->sc_iflock);
824 1.1 nat n = 0;
825 1.1 nat while (datalen >= DSE_HEADER_RX) {
826 1.1 nat /*
827 1.1 nat * fetch bytes of stream.
828 1.1 nat * here length = (ether frame length) + (FCS's 4 bytes)
829 1.1 nat */
830 1.1 nat /* fetch frame length */
831 1.1 nat len = _2btol(data);
832 1.1 nat
833 1.1 nat /* skip header part */
834 1.1 nat data += DSE_HEADER_RX;
835 1.1 nat datalen -= DSE_HEADER_RX;
836 1.1 nat
837 1.1 nat #if 0 /* 03/10/2001 only for debug */
838 1.1 nat {
839 1.1 nat printf("DATALEN %d len %d\n", datalen, len);
840 1.1 nat int j;
841 1.1 nat printf("\ndump[%d]: ",n);
842 1.1 nat for ( j = 0 ; j < datalen ; j++ ) {
843 1.1 nat printf("%02X ",data[j-DSE_HEADER_RX]);
844 1.1 nat }
845 1.1 nat }
846 1.1 nat #endif
847 1.1 nat #ifdef MAC68K_DEBUG
848 1.1 nat if (peek_flag) {
849 1.1 nat peek_flag = 0;
850 1.1 nat len = peek_packet(data);
851 1.1 nat }
852 1.1 nat #endif
853 1.3 nat if (len == 0)
854 1.1 nat break;
855 1.1 nat
856 1.1 nat #ifdef DSE_DEBUG
857 1.2 nat aprint_error_dev(sc->sc_dev, "dse_read: datalen = %d, packetlen"
858 1.2 nat " = %d, proto = 0x%04x\n", datalen, len,
859 1.1 nat ntohs(((struct ether_header *)data)->ether_type));
860 1.1 nat #endif
861 1.1 nat if ((len < (DSE_MINSIZE + ETHER_CRC_LEN)) ||
862 1.1 nat (MAX_BYTES_RX < len)) {
863 1.1 nat #ifdef DSE_DEBUG
864 1.1 nat aprint_error_dev(sc->sc_dev, "invalid packet size "
865 1.1 nat "%d; dropping\n", len);
866 1.1 nat #endif
867 1.1 nat if_statinc(ifp, if_ierrors);
868 1.1 nat break;
869 1.1 nat }
870 1.1 nat
871 1.1 nat /* Don't need crc. Must keep ether header for BPF */
872 1.1 nat m = dse_get(sc, data, len - ETHER_CRC_LEN);
873 1.1 nat if (m == NULL) {
874 1.1 nat #ifdef DSE_DEBUG
875 1.1 nat if (sc->sc_debug)
876 1.2 nat aprint_error_dev(sc->sc_dev, "dse_read: "
877 1.2 nat "dse_get returned null\n");
878 1.1 nat #endif
879 1.1 nat if_statinc(ifp, if_ierrors);
880 1.1 nat goto next_packet;
881 1.1 nat }
882 1.1 nat if_statinc(ifp, if_ipackets);
883 1.1 nat
884 1.1 nat /*
885 1.1 nat * Check if there's a BPF listener on this interface.
886 1.1 nat * If so, hand off the raw packet to BPF.
887 1.1 nat */
888 1.1 nat if (ifp->if_bpf)
889 1.1 nat bpf_mtap(ifp, m, BPF_D_OUT);
890 1.1 nat
891 1.1 nat /* Pass the packet up. */
892 1.1 nat if_percpuq_enqueue(sc->sc_ipq, m);
893 1.1 nat
894 1.1 nat next_packet:
895 1.1 nat data += len;
896 1.1 nat datalen -= len;
897 1.1 nat n++;
898 1.1 nat }
899 1.1 nat mutex_exit(&sc->sc_iflock);
900 1.1 nat
901 1.1 nat return n;
902 1.1 nat }
903 1.1 nat
904 1.1 nat
905 1.1 nat static void
906 1.1 nat dsewatchdog(struct ifnet *ifp)
907 1.1 nat {
908 1.1 nat struct dse_softc *sc = ifp->if_softc;
909 1.1 nat
910 1.1 nat log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
911 1.1 nat if_statinc(ifp, if_oerrors);
912 1.1 nat
913 1.1 nat dse_reset(sc);
914 1.1 nat }
915 1.1 nat
916 1.1 nat
917 1.1 nat static int
918 1.1 nat dse_reset(struct dse_softc *sc)
919 1.1 nat {
920 1.1 nat int error;
921 1.1 nat #if 0
922 1.1 nat /* Maybe we don't *really* want to reset the entire bus
923 1.1 nat * because the ctron isn't working. We would like to send a
924 1.1 nat * "BUS DEVICE RESET" message, but don't think the ctron
925 1.1 nat * understands it.
926 1.1 nat */
927 1.1 nat error = dse_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, DSE_RETRIES, 2000,
928 1.1 nat NULL, XS_CTL_RESET);
929 1.1 nat #endif
930 1.1 nat error = dse_init(sc);
931 1.1 nat return error;
932 1.1 nat }
933 1.1 nat
934 1.1 nat
935 1.1 nat static int
936 1.1 nat dse_init_adaptor(struct dse_softc *sc)
937 1.1 nat {
938 1.1 nat scsi_dayna_ether_generic cmd_vend1;
939 1.1 nat u_char tmpbuf[sizeof(cmd_vend1)];
940 1.1 nat int error;
941 1.1 nat
942 1.1 nat #if 0 /* 07/21/2001 for test */
943 1.1 nat /* Maybe we don't *really* want to reset the entire bus
944 1.1 nat * because the ctron isn't working. We would like to send a
945 1.1 nat * "BUS DEVICE RESET" message, but don't think the ctron
946 1.1 nat * understands it.
947 1.1 nat */
948 1.1 nat error = dse_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, DSE_RETRIES,
949 1.1 nat 2000, NULL, XS_CTL_RESET);
950 1.1 nat #endif
951 1.1 nat
952 1.1 nat cmd_vend1 = sonic_ether_vendor1;
953 1.1 nat
954 1.1 nat error = dse_scsipi_cmd(sc->sc_periph,
955 1.1 nat (struct scsipi_generic *)&cmd_vend1, sizeof(cmd_vend1),
956 1.1 nat &(tmpbuf[0]), sizeof(tmpbuf),
957 1.1 nat DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_IN);
958 1.1 nat
959 1.1 nat if (error)
960 1.1 nat goto l_end;
961 1.1 nat
962 1.1 nat /* wait 500 msec */
963 1.1 nat kpause("dsesleep", false, hz / 2, NULL);
964 1.1 nat
965 1.1 nat l_end:
966 1.1 nat return error;
967 1.1 nat }
968 1.1 nat
969 1.1 nat
970 1.1 nat static int
971 1.1 nat dse_get_addr(struct dse_softc *sc, uint8_t *myaddr)
972 1.1 nat {
973 1.1 nat scsi_dayna_ether_generic cmd_get_addr;
974 1.1 nat u_char tmpbuf[REQ_LEN_GET_ADDR];
975 1.1 nat int error;
976 1.1 nat
977 1.1 nat memset(&cmd_get_addr, 0, sizeof(cmd_get_addr));
978 1.1 nat cmd_get_addr.opcode[0] = DAYNA_CMD_GET_ADDR;
979 1.1 nat _lto2b(REQ_LEN_GET_ADDR, cmd_get_addr.length);
980 1.1 nat
981 1.1 nat error = dse_scsipi_cmd(sc->sc_periph,
982 1.1 nat (struct scsipi_generic *)&cmd_get_addr, sizeof(cmd_get_addr),
983 1.1 nat tmpbuf, sizeof(tmpbuf),
984 1.1 nat DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_IN);
985 1.1 nat
986 1.1 nat if (error == 0) {
987 1.1 nat memcpy(myaddr, &(tmpbuf[0]), ETHER_ADDR_LEN);
988 1.1 nat
989 1.1 nat aprint_error_dev(sc->sc_dev, "ethernet address %s\n",
990 1.1 nat ether_sprintf(myaddr));
991 1.1 nat }
992 1.1 nat
993 1.1 nat return error;
994 1.1 nat }
995 1.1 nat
996 1.1 nat
997 1.1 nat #if 0 /* 07/16/2000 comment-out */
998 1.1 nat static int
999 1.1 nat dse_set_mode(struct dse_softc *sc, int len, int mode)
1000 1.1 nat
1001 1.1 nat return 0;
1002 1.1 nat }
1003 1.1 nat #endif
1004 1.1 nat
1005 1.1 nat
1006 1.1 nat static int
1007 1.1 nat dse_init(struct dse_softc *sc)
1008 1.1 nat {
1009 1.1 nat struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1010 1.1 nat int error = 0;
1011 1.1 nat
1012 1.1 nat if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == IFF_UP) {
1013 1.1 nat ifp->if_flags |= IFF_RUNNING;
1014 1.1 nat mutex_enter(&sc->sc_iflock);
1015 1.1 nat if (!sc->sc_recv_work_pending) {
1016 1.1 nat sc->sc_recv_work_pending = true;
1017 1.1 nat workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work,
1018 1.1 nat NULL);
1019 1.1 nat }
1020 1.1 nat mutex_exit(&sc->sc_iflock);
1021 1.1 nat ifp->if_flags &= ~IFF_OACTIVE;
1022 1.1 nat mutex_enter(&sc->sc_iflock);
1023 1.1 nat if (!sc->sc_send_work_pending) {
1024 1.1 nat sc->sc_send_work_pending = true;
1025 1.1 nat workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work,
1026 1.1 nat NULL);
1027 1.1 nat }
1028 1.1 nat mutex_exit(&sc->sc_iflock);
1029 1.1 nat }
1030 1.1 nat return error;
1031 1.1 nat }
1032 1.1 nat
1033 1.1 nat
1034 1.1 nat static uint8_t BROADCAST_ADDR[ETHER_ADDR_LEN] =
1035 1.1 nat { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
1036 1.1 nat
1037 1.1 nat
1038 1.1 nat static int
1039 1.1 nat dse_set_multi(struct dse_softc *sc)
1040 1.1 nat {
1041 1.1 nat scsi_dayna_ether_generic cmd_set_multi;
1042 1.1 nat struct ether_multistep step;
1043 1.1 nat struct ether_multi *enm;
1044 1.1 nat u_char *cp, *mybuf;
1045 1.1 nat int error, len;
1046 1.1 nat
1047 1.1 nat error = 0;
1048 1.1 nat
1049 1.1 nat #ifdef DSE_DEBUG
1050 1.1 nat aprint_error_dev(sc->sc_dev, "dse_set_multi\n");
1051 1.1 nat #endif
1052 1.1 nat
1053 1.1 nat mybuf = malloc(ETHER_ADDR_LEN * DSE_MCAST_MAX, M_DEVBUF, M_NOWAIT);
1054 1.1 nat if (mybuf == NULL) {
1055 1.1 nat error = EIO;
1056 1.1 nat goto l_end;
1057 1.1 nat }
1058 1.1 nat
1059 1.1 nat /*
1060 1.1 nat * copy all entries to transfer buffer
1061 1.1 nat */
1062 1.1 nat cp = mybuf;
1063 1.1 nat len = 0;
1064 1.1 nat ETHER_FIRST_MULTI(step, &(sc->sc_ethercom), enm);
1065 1.1 nat while ((len < (DSE_MCAST_MAX - 1)) && (enm != NULL)) {
1066 1.1 nat /* ### refer low side entry */
1067 1.1 nat memcpy(cp, enm->enm_addrlo, ETHER_ADDR_LEN);
1068 1.1 nat
1069 1.1 nat cp += ETHER_ADDR_LEN;
1070 1.1 nat len++;
1071 1.1 nat ETHER_NEXT_MULTI(step, enm);
1072 1.1 nat }
1073 1.1 nat
1074 1.1 nat /* add broadcast address as default */
1075 1.1 nat memcpy(cp, BROADCAST_ADDR, ETHER_ADDR_LEN);
1076 1.1 nat len++;
1077 1.1 nat
1078 1.1 nat len *= ETHER_ADDR_LEN;
1079 1.1 nat
1080 1.1 nat memset(&cmd_set_multi, 0, sizeof(cmd_set_multi));
1081 1.1 nat cmd_set_multi.opcode[0] = DAYNA_CMD_SET_MULTI;
1082 1.1 nat _lto2b(len, cmd_set_multi.length);
1083 1.1 nat
1084 1.1 nat error = dse_scsipi_cmd(sc->sc_periph,
1085 1.1 nat (struct scsipi_generic*)&cmd_set_multi, sizeof(cmd_set_multi),
1086 1.1 nat mybuf, len, DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_OUT);
1087 1.1 nat
1088 1.1 nat free(mybuf, M_DEVBUF);
1089 1.1 nat
1090 1.1 nat l_end:
1091 1.1 nat return error;
1092 1.1 nat }
1093 1.1 nat
1094 1.1 nat
1095 1.1 nat static void
1096 1.1 nat dse_stop(struct dse_softc *sc)
1097 1.1 nat {
1098 1.1 nat /* Don't schedule any reads */
1099 1.1 nat callout_stop(&sc->sc_recv_ch);
1100 1.1 nat
1101 1.1 nat /* Wait for the workqueues to finish */
1102 1.1 nat mutex_enter(&sc->sc_iflock);
1103 1.1 nat workqueue_wait(sc->sc_recv_wq, &sc->sc_recv_work);
1104 1.1 nat workqueue_wait(sc->sc_send_wq, &sc->sc_send_work);
1105 1.1 nat mutex_exit(&sc->sc_iflock);
1106 1.1 nat
1107 1.1 nat /* Abort any scsi cmds in progress */
1108 1.1 nat mutex_enter(chan_mtx(sc->sc_periph->periph_channel));
1109 1.1 nat scsipi_kill_pending(sc->sc_periph);
1110 1.1 nat mutex_exit(chan_mtx(sc->sc_periph->periph_channel));
1111 1.1 nat }
1112 1.1 nat
1113 1.1 nat
1114 1.1 nat /*
1115 1.1 nat * Process an ioctl request.
1116 1.1 nat */
1117 1.1 nat static int
1118 1.1 nat dse_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1119 1.1 nat {
1120 1.1 nat struct dse_softc *sc;
1121 1.1 nat struct ifaddr *ifa;
1122 1.1 nat struct ifreq *ifr;
1123 1.1 nat struct sockaddr *sa;
1124 1.1 nat int error;
1125 1.1 nat
1126 1.1 nat error = 0;
1127 1.1 nat sc = ifp->if_softc;
1128 1.1 nat ifa = (struct ifaddr *)data;
1129 1.1 nat ifr = (struct ifreq *)data;
1130 1.1 nat
1131 1.1 nat switch (cmd) {
1132 1.1 nat case SIOCINITIFADDR:
1133 1.1 nat mutex_enter(&sc->sc_iflock);
1134 1.1 nat if ((error = dse_enable(sc)) != 0)
1135 1.1 nat break;
1136 1.1 nat ifp->if_flags |= IFF_UP;
1137 1.1 nat mutex_exit(&sc->sc_iflock);
1138 1.1 nat
1139 1.1 nat #if 0
1140 1.1 nat if ((error = dse_set_media(sc, CMEDIA_AUTOSENSE)) != 0)
1141 1.1 nat break;
1142 1.1 nat #endif
1143 1.1 nat
1144 1.1 nat switch (ifa->ifa_addr->sa_family) {
1145 1.1 nat #ifdef INET
1146 1.1 nat case AF_INET:
1147 1.1 nat if ((error = dse_init(sc)) != 0)
1148 1.1 nat break;
1149 1.1 nat arp_ifinit(ifp, ifa);
1150 1.1 nat break;
1151 1.1 nat #endif
1152 1.1 nat #ifdef NETATALK
1153 1.1 nat case AF_APPLETALK:
1154 1.1 nat if ((error = dse_init(sc)) != 0)
1155 1.1 nat break;
1156 1.1 nat break;
1157 1.1 nat #endif
1158 1.1 nat default:
1159 1.1 nat error = dse_init(sc);
1160 1.1 nat break;
1161 1.1 nat }
1162 1.1 nat break;
1163 1.1 nat
1164 1.1 nat
1165 1.1 nat case SIOCSIFADDR:
1166 1.1 nat mutex_enter(&sc->sc_iflock);
1167 1.1 nat error = dse_enable(sc);
1168 1.1 nat mutex_exit(&sc->sc_iflock);
1169 1.1 nat if (error != 0)
1170 1.1 nat break;
1171 1.1 nat ifp->if_flags |= IFF_UP;
1172 1.1 nat
1173 1.1 nat switch (ifa->ifa_addr->sa_family) {
1174 1.1 nat #ifdef INET
1175 1.1 nat case AF_INET:
1176 1.1 nat if ((error = dse_init(sc)) != 0)
1177 1.1 nat break;
1178 1.1 nat arp_ifinit(ifp, ifa);
1179 1.1 nat break;
1180 1.1 nat #endif
1181 1.1 nat #ifdef NETATALK
1182 1.1 nat case AF_APPLETALK:
1183 1.1 nat if ((error = dse_init(sc)) != 0)
1184 1.1 nat break;
1185 1.1 nat break;
1186 1.1 nat #endif
1187 1.1 nat default:
1188 1.1 nat error = dse_init(sc);
1189 1.1 nat break;
1190 1.1 nat }
1191 1.1 nat break;
1192 1.1 nat
1193 1.1 nat case SIOCSIFFLAGS:
1194 1.1 nat if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1195 1.1 nat break;
1196 1.1 nat /* XXX re-use ether_ioctl() */
1197 1.1 nat switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1198 1.1 nat case IFF_RUNNING:
1199 1.1 nat /*
1200 1.1 nat * If interface is marked down and it is running, then
1201 1.1 nat * stop it.
1202 1.1 nat */
1203 1.1 nat dse_stop(sc);
1204 1.1 nat mutex_enter(&sc->sc_iflock);
1205 1.1 nat ifp->if_flags &= ~IFF_RUNNING;
1206 1.1 nat dse_disable(sc);
1207 1.1 nat mutex_exit(&sc->sc_iflock);
1208 1.1 nat break;
1209 1.1 nat case IFF_UP:
1210 1.1 nat /*
1211 1.1 nat * If interface is marked up and it is stopped, then
1212 1.1 nat * start it.
1213 1.1 nat */
1214 1.1 nat mutex_enter(&sc->sc_iflock);
1215 1.1 nat error = dse_enable(sc);
1216 1.1 nat mutex_exit(&sc->sc_iflock);
1217 1.1 nat if (error)
1218 1.1 nat break;
1219 1.1 nat error = dse_init(sc);
1220 1.1 nat break;
1221 1.1 nat default:
1222 1.1 nat /*
1223 1.1 nat * Reset the interface to pick up changes in any other
1224 1.1 nat * flags that affect hardware registers.
1225 1.1 nat */
1226 1.1 nat mutex_enter(&sc->sc_iflock);
1227 1.1 nat if (sc->sc_enabled)
1228 1.1 nat error = dse_init(sc);
1229 1.1 nat mutex_exit(&sc->sc_iflock);
1230 1.1 nat break;
1231 1.1 nat }
1232 1.2 nat #ifdef DSE_DEBUG
1233 1.1 nat if (ifp->if_flags & IFF_DEBUG)
1234 1.1 nat sc->sc_debug = 1;
1235 1.1 nat else
1236 1.1 nat sc->sc_debug = 0;
1237 1.1 nat #endif
1238 1.1 nat break;
1239 1.1 nat
1240 1.1 nat case SIOCADDMULTI:
1241 1.1 nat if (sc->sc_enabled == 0) {
1242 1.1 nat error = EIO;
1243 1.1 nat break;
1244 1.1 nat }
1245 1.1 nat mutex_enter(&sc->sc_iflock);
1246 1.1 nat sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_WAITOK);
1247 1.1 nat mutex_exit(&sc->sc_iflock);
1248 1.1 nat if (ether_addmulti(sa, &sc->sc_ethercom) == ENETRESET) {
1249 1.1 nat error = dse_set_multi(sc);
1250 1.1 nat #ifdef DSE_DEBUG
1251 1.1 nat aprint_error_dev(sc->sc_dev, "add multi: %s\n",
1252 1.1 nat ether_sprintf(ifr->ifr_addr.sa_data));
1253 1.1 nat #endif
1254 1.1 nat } else
1255 1.1 nat error = 0;
1256 1.1 nat
1257 1.1 nat mutex_enter(&sc->sc_iflock);
1258 1.1 nat sockaddr_free(sa);
1259 1.1 nat mutex_exit(&sc->sc_iflock);
1260 1.1 nat
1261 1.1 nat break;
1262 1.1 nat
1263 1.1 nat case SIOCDELMULTI:
1264 1.1 nat if (sc->sc_enabled == 0) {
1265 1.1 nat error = EIO;
1266 1.1 nat break;
1267 1.1 nat }
1268 1.1 nat mutex_enter(&sc->sc_iflock);
1269 1.1 nat sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_WAITOK);
1270 1.1 nat mutex_exit(&sc->sc_iflock);
1271 1.1 nat if (ether_delmulti(sa, &sc->sc_ethercom) == ENETRESET) {
1272 1.1 nat error = dse_set_multi(sc);
1273 1.1 nat #ifdef DSE_DEBUG
1274 1.1 nat aprint_error_dev(sc->sc_dev, "delete multi: %s\n",
1275 1.1 nat ether_sprintf(ifr->ifr_addr.sa_data));
1276 1.1 nat #endif
1277 1.1 nat } else
1278 1.1 nat error = 0;
1279 1.1 nat
1280 1.1 nat mutex_enter(&sc->sc_iflock);
1281 1.1 nat sockaddr_free(sa);
1282 1.1 nat mutex_exit(&sc->sc_iflock);
1283 1.1 nat
1284 1.1 nat break;
1285 1.1 nat
1286 1.1 nat default:
1287 1.1 nat error = ether_ioctl(ifp, cmd, data);
1288 1.1 nat break;
1289 1.1 nat }
1290 1.1 nat
1291 1.1 nat
1292 1.1 nat return error;
1293 1.1 nat }
1294 1.1 nat
1295 1.1 nat
1296 1.1 nat /*
1297 1.1 nat * Enable the network interface.
1298 1.1 nat */
1299 1.1 nat int
1300 1.1 nat dse_enable(struct dse_softc *sc)
1301 1.1 nat {
1302 1.1 nat struct scsipi_periph *periph = sc->sc_periph;
1303 1.1 nat struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1304 1.1 nat int error = 0;
1305 1.1 nat
1306 1.1 nat if (sc->sc_enabled == 0) {
1307 1.1 nat if ((error = scsipi_adapter_addref(adapt)) == 0)
1308 1.1 nat sc->sc_enabled = 1;
1309 1.1 nat else
1310 1.1 nat aprint_error_dev(sc->sc_dev, "device enable failed\n");
1311 1.1 nat }
1312 1.1 nat
1313 1.1 nat return error;
1314 1.1 nat }
1315 1.1 nat
1316 1.1 nat
1317 1.1 nat /*
1318 1.1 nat * Disable the network interface.
1319 1.1 nat */
1320 1.1 nat void
1321 1.1 nat dse_disable(struct dse_softc *sc)
1322 1.1 nat {
1323 1.1 nat struct scsipi_periph *periph = sc->sc_periph;
1324 1.1 nat struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1325 1.1 nat if (sc->sc_enabled != 0) {
1326 1.1 nat scsipi_adapter_delref(adapt);
1327 1.1 nat sc->sc_enabled = 0;
1328 1.1 nat }
1329 1.1 nat }
1330 1.1 nat
1331 1.1 nat
1332 1.1 nat #define DSEUNIT(z) (minor(z))
1333 1.1 nat
1334 1.1 nat /*
1335 1.1 nat * open the device.
1336 1.1 nat */
1337 1.1 nat int
1338 1.1 nat dseopen(dev_t dev, int flag, int fmt, struct lwp *l)
1339 1.1 nat {
1340 1.1 nat int unit, error;
1341 1.1 nat struct dse_softc *sc;
1342 1.1 nat struct scsipi_periph *periph;
1343 1.1 nat struct scsipi_adapter *adapt;
1344 1.1 nat
1345 1.1 nat unit = DSEUNIT(dev);
1346 1.1 nat sc = device_lookup_private(&dse_cd, unit);
1347 1.1 nat if (sc == NULL)
1348 1.1 nat return ENXIO;
1349 1.1 nat
1350 1.1 nat periph = sc->sc_periph;
1351 1.1 nat adapt = periph->periph_channel->chan_adapter;
1352 1.1 nat
1353 1.1 nat if ((error = scsipi_adapter_addref(adapt)) != 0)
1354 1.1 nat return error;
1355 1.1 nat
1356 1.1 nat SC_DEBUG(periph, SCSIPI_DB1,
1357 1.1 nat ("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
1358 1.1 nat dse_cd.cd_ndevs));
1359 1.1 nat
1360 1.1 nat periph->periph_flags |= PERIPH_OPEN;
1361 1.1 nat
1362 1.1 nat SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
1363 1.1 nat
1364 1.1 nat return 0;
1365 1.1 nat }
1366 1.1 nat
1367 1.1 nat
1368 1.1 nat /*
1369 1.1 nat * close the device.. only called if we are the LAST
1370 1.1 nat * occurence of an open device
1371 1.1 nat */
1372 1.1 nat int
1373 1.1 nat dseclose(dev_t dev, int flag, int fmt, struct lwp *l)
1374 1.1 nat {
1375 1.1 nat struct dse_softc *sc = device_lookup_private(&dse_cd, DSEUNIT(dev));
1376 1.1 nat struct scsipi_periph *periph = sc->sc_periph;
1377 1.1 nat struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
1378 1.1 nat
1379 1.1 nat SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
1380 1.1 nat
1381 1.1 nat scsipi_wait_drain(periph);
1382 1.1 nat
1383 1.1 nat scsipi_adapter_delref(adapt);
1384 1.1 nat periph->periph_flags &= ~PERIPH_OPEN;
1385 1.1 nat
1386 1.1 nat return 0;
1387 1.1 nat }
1388 1.1 nat
1389 1.1 nat
1390 1.1 nat /*
1391 1.1 nat * Perform special action on behalf of the user
1392 1.1 nat * Only does generic scsi ioctls.
1393 1.1 nat */
1394 1.1 nat int
1395 1.1 nat dseioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1396 1.1 nat {
1397 1.1 nat struct dse_softc *sc = device_lookup_private(&dse_cd, DSEUNIT(dev));
1398 1.1 nat
1399 1.1 nat return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
1400 1.1 nat }
1401 1.1 nat
1402