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