diff --git a/libexec/telnetd/telnetd.c b/libexec/telnetd/telnetd.c index 5695f51ca73a..bcb5babc0db3 100644 --- a/libexec/telnetd/telnetd.c +++ b/libexec/telnetd/telnetd.c @@ -1,1585 +1,1585 @@ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1989, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)telnetd.c 8.2 (Berkeley) 12/15/93"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include "telnetd.h" #include "pathnames.h" #if defined(_SC_CRAY_SECURE_SYS) && !defined(SCM_SECURITY) /* * UNICOS 6.0/6.1 do not have SCM_SECURITY defined, so we can * use it to tell us to turn off all the socket security code, * since that is only used in UNICOS 7.0 and later. */ # undef _SC_CRAY_SECURE_SYS #endif #include #include #include #include #include #include #if defined(_SC_CRAY_SECURE_SYS) #include #include # ifdef SO_SEC_MULTI /* 8.0 code */ #include #include # endif /* SO_SEC_MULTI */ /* wrapper for KAME-special getnameinfo() */ #ifndef NI_WITHSCOPEID #define NI_WITHSCOPEID 0 #endif int secflag; char tty_dev[16]; struct secdev dv; struct sysv sysv; # ifdef SO_SEC_MULTI /* 8.0 code */ struct socksec ss; # else /* SO_SEC_MULTI */ /* 7.0 code */ struct socket_security ss; # endif /* SO_SEC_MULTI */ #endif /* _SC_CRAY_SECURE_SYS */ #if defined(AUTHENTICATION) #include int auth_level = 0; #endif #if defined(SecurID) int require_SecurID = 0; #endif char remote_hostname[MAXHOSTNAMELEN]; int utmp_len = sizeof(remote_hostname) - 1; int registerd_host_only = 0; #ifdef STREAMSPTY # include # include /* make sure we don't get the bsd version */ # include "/usr/include/sys/tty.h" # include /* * Because of the way ptyibuf is used with streams messages, we need - * ptyibuf+1 to be on a full-word boundary. The following wierdness + * ptyibuf+1 to be on a full-word boundary. The following weirdness * is simply to make that happen. */ long ptyibufbuf[BUFSIZ/sizeof(long)+1]; char *ptyibuf = ((char *)&ptyibufbuf[1])-1; char *ptyip = ((char *)&ptyibufbuf[1])-1; char ptyibuf2[BUFSIZ]; unsigned char ctlbuf[BUFSIZ]; struct strbuf strbufc, strbufd; int readstream(); #else /* ! STREAMPTY */ /* * I/O data buffers, * pointers, and counters. */ char ptyibuf[BUFSIZ], *ptyip = ptyibuf; char ptyibuf2[BUFSIZ]; #endif /* ! STREAMPTY */ int hostinfo = 1; /* do we print login banner? */ #ifdef CRAY extern int newmap; /* nonzero if \n maps to ^M^J */ int lowpty = 0, highpty; /* low, high pty numbers */ #endif /* CRAY */ int debug = 0; int keepalive = 1; char *altlogin; void doit __P((struct sockaddr *)); int terminaltypeok __P((char *)); void startslave __P((char *, int, char *)); extern void usage P((void)); /* * The string to pass to getopt(). We do it this way so * that only the actual options that we support will be * passed off to getopt(). */ char valid_opts[] = { 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U', '4', '6', #ifdef AUTHENTICATION 'a', ':', 'X', ':', #endif #ifdef BFTPDAEMON 'B', #endif #ifdef DIAGNOSTICS 'D', ':', #endif #if defined(CRAY) && defined(NEWINIT) 'I', ':', #endif #ifdef LINEMODE 'l', #endif #ifdef CRAY 'r', ':', #endif #ifdef SecurID 's', #endif '\0' }; int family = AF_INET; int main(argc, argv) char *argv[]; { struct sockaddr_storage from; int on = 1, fromlen; register int ch; #if defined(IPPROTO_IP) && defined(IP_TOS) int tos = -1; #endif pfrontp = pbackp = ptyobuf; netip = netibuf; nfrontp = nbackp = netobuf; /* * This initialization causes linemode to default to a configuration * that works on all telnet clients, including the FreeBSD client. * This is not quite the same as the telnet client issuing a "mode * character" command, but has most of the same benefits, and is * preferable since some clients (like usofts) don't have the * mode character command anyway and linemode breaks things. * The most notable symptom of fix is that csh "set filec" operations * like (filename completion) and ^D (choices) keys now work * in telnet sessions and can be used more than once on the same line. * CR/LF handling is also corrected in some termio modes. This * change resolves problem reports bin/771 and bin/1037. */ linemode=1; /*Default to mode that works on bulk of clients*/ #ifdef CRAY /* * Get number of pty's before trying to process options, * which may include changing pty range. */ highpty = getnpty(); #endif /* CRAY */ while ((ch = getopt(argc, argv, valid_opts)) != -1) { switch(ch) { #ifdef AUTHENTICATION case 'a': /* * Check for required authentication level */ if (strcmp(optarg, "debug") == 0) { extern int auth_debug_mode; auth_debug_mode = 1; } else if (strcasecmp(optarg, "none") == 0) { auth_level = 0; } else if (strcasecmp(optarg, "other") == 0) { auth_level = AUTH_OTHER; } else if (strcasecmp(optarg, "user") == 0) { auth_level = AUTH_USER; } else if (strcasecmp(optarg, "valid") == 0) { auth_level = AUTH_VALID; } else if (strcasecmp(optarg, "off") == 0) { /* * This hack turns off authentication */ auth_level = -1; } else { warnx("unknown authorization level for -a"); } break; #endif /* AUTHENTICATION */ #ifdef BFTPDAEMON case 'B': bftpd++; break; #endif /* BFTPDAEMON */ case 'd': if (strcmp(optarg, "ebug") == 0) { debug++; break; } usage(); /* NOTREACHED */ break; #ifdef DIAGNOSTICS case 'D': /* * Check for desired diagnostics capabilities. */ if (!strcmp(optarg, "report")) { diagnostic |= TD_REPORT|TD_OPTIONS; } else if (!strcmp(optarg, "exercise")) { diagnostic |= TD_EXERCISE; } else if (!strcmp(optarg, "netdata")) { diagnostic |= TD_NETDATA; } else if (!strcmp(optarg, "ptydata")) { diagnostic |= TD_PTYDATA; } else if (!strcmp(optarg, "options")) { diagnostic |= TD_OPTIONS; } else { usage(); /* NOT REACHED */ } break; #endif /* DIAGNOSTICS */ case 'h': hostinfo = 0; break; #if defined(CRAY) && defined(NEWINIT) case 'I': { extern char *gen_id; gen_id = optarg; break; } #endif /* defined(CRAY) && defined(NEWINIT) */ #ifdef LINEMODE case 'l': alwayslinemode = 1; break; #endif /* LINEMODE */ case 'k': #if defined(LINEMODE) && defined(KLUDGELINEMODE) lmodetype = NO_AUTOKLUDGE; #else /* ignore -k option if built without kludge linemode */ #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */ break; case 'n': keepalive = 0; break; case 'p': altlogin = optarg; break; #ifdef CRAY case 'r': { char *strchr(); char *c; /* * Allow the specification of alterations * to the pty search range. It is legal to * specify only one, and not change the * other from its default. */ c = strchr(optarg, '-'); if (c) { *c++ = '\0'; highpty = atoi(c); } if (*optarg != '\0') lowpty = atoi(optarg); if ((lowpty > highpty) || (lowpty < 0) || (highpty > 32767)) { usage(); /* NOT REACHED */ } break; } #endif /* CRAY */ #ifdef SecurID case 's': /* SecurID required */ require_SecurID = 1; break; #endif /* SecurID */ case 'S': #ifdef HAS_GETTOS if ((tos = parsetos(optarg, "tcp")) < 0) warnx("%s%s%s", "bad TOS argument '", optarg, "'; will try to use default TOS"); #else warnx("TOS option unavailable; -S flag not supported"); #endif break; case 'u': utmp_len = atoi(optarg); if (utmp_len < 0) utmp_len = -utmp_len; if (utmp_len >= sizeof(remote_hostname)) utmp_len = sizeof(remote_hostname) - 1; break; case 'U': registerd_host_only = 1; break; #ifdef AUTHENTICATION case 'X': /* * Check for invalid authentication types */ auth_disable_name(optarg); break; #endif /* AUTHENTICATION */ case '4': family = AF_INET; break; #ifdef INET6 case '6': family = AF_INET6; break; #endif default: warnx("%c: unknown option", ch); /* FALLTHROUGH */ case '?': usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; if (debug) { int s, ns, foo, error; char *service = "telnet"; struct addrinfo hints, *res; if (argc > 1) { usage(); /* NOT REACHED */ } else if (argc == 1) service = *argv; memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_PASSIVE; hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = 0; error = getaddrinfo(NULL, service, &hints, &res); if (error) { errx(1, "tcp/%s: %s\n", service, gai_strerror(error)); if (error == EAI_SYSTEM) errx(1, "tcp/%s: %s\n", service, strerror(errno)); usage(); } s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (s < 0) err(1, "socket"); (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); if (bind(s, res->ai_addr, res->ai_addrlen) < 0) err(1, "bind"); if (listen(s, 1) < 0) err(1, "listen"); foo = res->ai_addrlen; ns = accept(s, res->ai_addr, &foo); if (ns < 0) err(1, "accept"); (void) dup2(ns, 0); (void) close(ns); (void) close(s); #ifdef convex } else if (argc == 1) { ; /* VOID*/ /* Just ignore the host/port name */ #endif } else if (argc > 0) { usage(); /* NOT REACHED */ } #if defined(_SC_CRAY_SECURE_SYS) secflag = sysconf(_SC_CRAY_SECURE_SYS); /* * Get socket's security label */ if (secflag) { int szss = sizeof(ss); #ifdef SO_SEC_MULTI /* 8.0 code */ int sock_multi; int szi = sizeof(int); #endif /* SO_SEC_MULTI */ bzero((char *)&dv, sizeof(dv)); if (getsysv(&sysv, sizeof(struct sysv)) != 0) err(1, "getsysv"); /* * Get socket security label and set device values * {security label to be set on ttyp device} */ #ifdef SO_SEC_MULTI /* 8.0 code */ if ((getsockopt(0, SOL_SOCKET, SO_SECURITY, (char *)&ss, &szss) < 0) || (getsockopt(0, SOL_SOCKET, SO_SEC_MULTI, (char *)&sock_multi, &szi) < 0)) { err(1, "getsockopt"); } else { dv.dv_actlvl = ss.ss_actlabel.lt_level; dv.dv_actcmp = ss.ss_actlabel.lt_compart; if (!sock_multi) { dv.dv_minlvl = dv.dv_maxlvl = dv.dv_actlvl; dv.dv_valcmp = dv.dv_actcmp; } else { dv.dv_minlvl = ss.ss_minlabel.lt_level; dv.dv_maxlvl = ss.ss_maxlabel.lt_level; dv.dv_valcmp = ss.ss_maxlabel.lt_compart; } dv.dv_devflg = 0; } #else /* SO_SEC_MULTI */ /* 7.0 code */ if (getsockopt(0, SOL_SOCKET, SO_SECURITY, (char *)&ss, &szss) >= 0) { dv.dv_actlvl = ss.ss_slevel; dv.dv_actcmp = ss.ss_compart; dv.dv_minlvl = ss.ss_minlvl; dv.dv_maxlvl = ss.ss_maxlvl; dv.dv_valcmp = ss.ss_maxcmp; } #endif /* SO_SEC_MULTI */ } #endif /* _SC_CRAY_SECURE_SYS */ openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON); fromlen = sizeof (from); if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { warn("getpeername"); _exit(1); } if (keepalive && setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof (on)) < 0) { syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); } #if defined(IPPROTO_IP) && defined(IP_TOS) if (from.ss_family == AF_INET) { # if defined(HAS_GETTOS) struct tosent *tp; if (tos < 0 && (tp = gettosbyname("telnet", "tcp"))) tos = tp->t_tos; # endif if (tos < 0) tos = 020; /* Low Delay bit */ if (tos && (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(tos)) < 0) && (errno != ENOPROTOOPT) ) syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); } #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */ net = 0; doit((struct sockaddr *)&from); /* NOTREACHED */ return(0); } /* end of main */ void usage() { fprintf(stderr, "usage: telnetd"); #ifdef AUTHENTICATION fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t"); #endif #ifdef BFTPDAEMON fprintf(stderr, " [-B]"); #endif fprintf(stderr, " [-debug]"); #ifdef DIAGNOSTICS fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t"); #endif #ifdef AUTHENTICATION fprintf(stderr, " [-edebug]"); #endif fprintf(stderr, " [-h]"); #if defined(CRAY) && defined(NEWINIT) fprintf(stderr, " [-Iinitid]"); #endif #if defined(LINEMODE) && defined(KLUDGELINEMODE) fprintf(stderr, " [-k]"); #endif #ifdef LINEMODE fprintf(stderr, " [-l]"); #endif fprintf(stderr, " [-n]"); #ifdef CRAY fprintf(stderr, " [-r[lowpty]-[highpty]]"); #endif fprintf(stderr, "\n\t"); #ifdef SecurID fprintf(stderr, " [-s]"); #endif #ifdef HAS_GETTOS fprintf(stderr, " [-S tos]"); #endif #ifdef AUTHENTICATION fprintf(stderr, " [-X auth-type]"); #endif fprintf(stderr, " [-u utmp_hostname_length] [-U]"); fprintf(stderr, " [port]\n"); exit(1); } /* * getterminaltype * * Ask the other end to send along its terminal type and speed. * Output is the variable terminaltype filled in. */ static unsigned char ttytype_sbbuf[] = { IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE }; int getterminaltype(name) char *name; { int retval = -1; void _gettermname(); settimer(baseline); #if defined(AUTHENTICATION) /* * Handle the Authentication option before we do anything else. */ send_do(TELOPT_AUTHENTICATION, 1); while (his_will_wont_is_changing(TELOPT_AUTHENTICATION)) ttloop(); if (his_state_is_will(TELOPT_AUTHENTICATION)) { retval = auth_wait(name); } #endif send_do(TELOPT_TTYPE, 1); send_do(TELOPT_TSPEED, 1); send_do(TELOPT_XDISPLOC, 1); send_do(TELOPT_NEW_ENVIRON, 1); send_do(TELOPT_OLD_ENVIRON, 1); while ( his_will_wont_is_changing(TELOPT_TTYPE) || his_will_wont_is_changing(TELOPT_TSPEED) || his_will_wont_is_changing(TELOPT_XDISPLOC) || his_will_wont_is_changing(TELOPT_NEW_ENVIRON) || his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) { ttloop(); } if (his_state_is_will(TELOPT_TSPEED)) { static unsigned char sb[] = { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } if (his_state_is_will(TELOPT_XDISPLOC)) { static unsigned char sb[] = { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } if (his_state_is_will(TELOPT_NEW_ENVIRON)) { static unsigned char sb[] = { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } else if (his_state_is_will(TELOPT_OLD_ENVIRON)) { static unsigned char sb[] = { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } if (his_state_is_will(TELOPT_TTYPE)) { bcopy(ttytype_sbbuf, nfrontp, sizeof ttytype_sbbuf); nfrontp += sizeof ttytype_sbbuf; } if (his_state_is_will(TELOPT_TSPEED)) { while (sequenceIs(tspeedsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_XDISPLOC)) { while (sequenceIs(xdisplocsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_NEW_ENVIRON)) { while (sequenceIs(environsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_OLD_ENVIRON)) { while (sequenceIs(oenvironsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_TTYPE)) { char first[256], last[256]; while (sequenceIs(ttypesubopt, baseline)) ttloop(); /* * If the other side has already disabled the option, then * we have to just go with what we (might) have already gotten. */ if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) { (void) strncpy(first, terminaltype, sizeof(first)-1); first[sizeof(first)-1] = '\0'; for(;;) { /* * Save the unknown name, and request the next name. */ (void) strncpy(last, terminaltype, sizeof(last)-1); last[sizeof(last)-1] = '\0'; _gettermname(); if (terminaltypeok(terminaltype)) break; if ((strncmp(last, terminaltype, sizeof(last)) == 0) || his_state_is_wont(TELOPT_TTYPE)) { /* * We've hit the end. If this is the same as * the first name, just go with it. */ if (strncmp(first, terminaltype, sizeof(first)) == 0) break; /* * Get the terminal name one more time, so that * RFC1091 compliant telnets will cycle back to * the start of the list. */ _gettermname(); if (strncmp(first, terminaltype, sizeof(first)) != 0) { (void) strncpy(terminaltype, first, sizeof(terminaltype)-1); terminaltype[sizeof(terminaltype)-1] = '\0'; } break; } } } } return(retval); } /* end of getterminaltype */ void _gettermname() { /* * If the client turned off the option, * we can't send another request, so we * just return. */ if (his_state_is_wont(TELOPT_TTYPE)) return; settimer(baseline); bcopy(ttytype_sbbuf, nfrontp, sizeof ttytype_sbbuf); nfrontp += sizeof ttytype_sbbuf; while (sequenceIs(ttypesubopt, baseline)) ttloop(); } int terminaltypeok(s) char *s; { char buf[1024]; if (terminaltype == NULL) return(1); /* * tgetent() will return 1 if the type is known, and * 0 if it is not known. If it returns -1, it couldn't * open the database. But if we can't open the database, * it won't help to say we failed, because we won't be * able to verify anything else. So, we treat -1 like 1. */ if (tgetent(buf, s) == 0) return(0); return(1); } #ifndef MAXHOSTNAMELEN #define MAXHOSTNAMELEN 256 #endif /* MAXHOSTNAMELEN */ char *hostname; char host_name[MAXHOSTNAMELEN]; extern void telnet P((int, int, char *)); int level; char user_name[256]; /* * Get a pty, scan input lines. */ void doit(who) struct sockaddr *who; { int err; int ptynum; /* * Find an available pty to use. */ #ifndef convex pty = getpty(&ptynum); if (pty < 0) fatal(net, "All network ports in use"); #else for (;;) { char *lp; if ((lp = getpty()) == NULL) fatal(net, "Out of ptys"); if ((pty = open(lp, 2)) >= 0) { strlcpy(line,lp,sizeof(line)); line[5] = 't'; break; } } #endif #if defined(_SC_CRAY_SECURE_SYS) /* * set ttyp line security label */ if (secflag) { char slave_dev[16]; sprintf(tty_dev, "%spty/%03d", _PATH_DEV, ptynum); if (setdevs(tty_dev, &dv) < 0) fatal(net, "cannot set pty security"); sprintf(slave_dev, "%sp%03d", _PATH_TTY, ptynum); if (setdevs(slave_dev, &dv) < 0) fatal(net, "cannot set tty security"); } #endif /* _SC_CRAY_SECURE_SYS */ /* get name of connected client */ if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1, who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only) fatal(net, "Couldn't resolve your address into a host name.\r\n\ Please contact your net administrator"); remote_hostname[sizeof(remote_hostname) - 1] = '\0'; trimdomain(remote_hostname, UT_HOSTSIZE); if (!isdigit(remote_hostname[0]) && strlen(remote_hostname) > utmp_len) err = getnameinfo(who, who->sa_len, remote_hostname, sizeof(remote_hostname), NULL, 0, NI_NUMERICHOST|NI_WITHSCOPEID); /* XXX: do 'err' check */ (void) gethostname(host_name, sizeof(host_name) - 1); host_name[sizeof(host_name) - 1] = '\0'; hostname = host_name; #if defined(AUTHENTICATION) auth_encrypt_init(hostname, remote_hostname, "TELNETD", 1); #endif init_env(); /* * get terminal type. */ *user_name = 0; level = getterminaltype(user_name); setenv("TERM", terminaltype ? terminaltype : "network", 1); #if defined(_SC_CRAY_SECURE_SYS) if (secflag) { if (setulvl(dv.dv_actlvl) < 0) fatal(net,"cannot setulvl()"); if (setucmp(dv.dv_actcmp) < 0) fatal(net, "cannot setucmp()"); } #endif /* _SC_CRAY_SECURE_SYS */ telnet(net, pty, remote_hostname); /* begin server process */ /*NOTREACHED*/ } /* end of doit */ #if defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) int Xterm_output(ibufp, obuf, icountp, ocount) char **ibufp, *obuf; int *icountp, ocount; { int ret; ret = term_output(*ibufp, obuf, *icountp, ocount); *ibufp += *icountp; *icountp = 0; return(ret); } #define term_output Xterm_output #endif /* defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) */ /* * Main loop. Select from pty and network, and * hand data to telnet receiver finite state machine. */ void telnet(f, p, host) int f, p; char *host; { int on = 1; #define TABBUFSIZ 512 char defent[TABBUFSIZ]; char defstrs[TABBUFSIZ]; #undef TABBUFSIZ char *HE; char *HN; char *IM; char *IF; char *if_buf; int if_fd; struct stat statbuf; void netflush(); /* * Initialize the slc mapping table. */ get_slc_defaults(); /* * Do some tests where it is desireable to wait for a response. * Rather than doing them slowly, one at a time, do them all * at once. */ if (my_state_is_wont(TELOPT_SGA)) send_will(TELOPT_SGA, 1); /* * Is the client side a 4.2 (NOT 4.3) system? We need to know this * because 4.2 clients are unable to deal with TCP urgent data. * * To find out, we send out a "DO ECHO". If the remote system * answers "WILL ECHO" it is probably a 4.2 client, and we note * that fact ("WILL ECHO" ==> that the client will echo what * WE, the server, sends it; it does NOT mean that the client will * echo the terminal input). */ send_do(TELOPT_ECHO, 1); #ifdef LINEMODE if (his_state_is_wont(TELOPT_LINEMODE)) { /* Query the peer for linemode support by trying to negotiate * the linemode option. */ linemode = 0; editmode = 0; send_do(TELOPT_LINEMODE, 1); /* send do linemode */ } #endif /* LINEMODE */ /* * Send along a couple of other options that we wish to negotiate. */ send_do(TELOPT_NAWS, 1); send_will(TELOPT_STATUS, 1); flowmode = 1; /* default flow control state */ restartany = -1; /* uninitialized... */ send_do(TELOPT_LFLOW, 1); /* * Spin, waiting for a response from the DO ECHO. However, * some REALLY DUMB telnets out there might not respond * to the DO ECHO. So, we spin looking for NAWS, (most dumb * telnets so far seem to respond with WONT for a DO that * they don't understand...) because by the time we get the * response, it will already have processed the DO ECHO. * Kludge upon kludge. */ while (his_will_wont_is_changing(TELOPT_NAWS)) ttloop(); /* * But... * The client might have sent a WILL NAWS as part of its * startup code; if so, we'll be here before we get the * response to the DO ECHO. We'll make the assumption * that any implementation that understands about NAWS * is a modern enough implementation that it will respond * to our DO ECHO request; hence we'll do another spin * waiting for the ECHO option to settle down, which is * what we wanted to do in the first place... */ if (his_want_state_is_will(TELOPT_ECHO) && his_state_is_will(TELOPT_NAWS)) { while (his_will_wont_is_changing(TELOPT_ECHO)) ttloop(); } /* * On the off chance that the telnet client is broken and does not * respond to the DO ECHO we sent, (after all, we did send the * DO NAWS negotiation after the DO ECHO, and we won't get here * until a response to the DO NAWS comes back) simulate the * receipt of a will echo. This will also send a WONT ECHO * to the client, since we assume that the client failed to * respond because it believes that it is already in DO ECHO * mode, which we do not want. */ if (his_want_state_is_will(TELOPT_ECHO)) { DIAG(TD_OPTIONS, {sprintf(nfrontp, "td: simulating recv\r\n"); nfrontp += strlen(nfrontp);}); willoption(TELOPT_ECHO); } /* * Finally, to clean things up, we turn on our echo. This * will break stupid 4.2 telnets out of local terminal echo. */ if (my_state_is_wont(TELOPT_ECHO)) send_will(TELOPT_ECHO, 1); #ifndef STREAMSPTY /* * Turn on packet mode */ (void) ioctl(p, TIOCPKT, (char *)&on); #endif #if defined(LINEMODE) && defined(KLUDGELINEMODE) /* * Continuing line mode support. If client does not support * real linemode, attempt to negotiate kludge linemode by sending * the do timing mark sequence. */ if (lmodetype < REAL_LINEMODE) send_do(TELOPT_TM, 1); #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */ /* * Call telrcv() once to pick up anything received during * terminal type negotiation, 4.2/4.3 determination, and * linemode negotiation. */ telrcv(); (void) ioctl(f, FIONBIO, (char *)&on); (void) ioctl(p, FIONBIO, (char *)&on); #if defined(CRAY2) && defined(UNICOS5) init_termdriver(f, p, interrupt, sendbrk); #endif #if defined(SO_OOBINLINE) (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof on); #endif /* defined(SO_OOBINLINE) */ #ifdef SIGTSTP (void) signal(SIGTSTP, SIG_IGN); #endif #ifdef SIGTTOU /* * Ignoring SIGTTOU keeps the kernel from blocking us * in ttioct() in /sys/tty.c. */ (void) signal(SIGTTOU, SIG_IGN); #endif (void) signal(SIGCHLD, cleanup); #if defined(CRAY2) && defined(UNICOS5) /* * Cray-2 will send a signal when pty modes are changed by slave * side. Set up signal handler now. */ if ((int)signal(SIGUSR1, termstat) < 0) warn("signal"); else if (ioctl(p, TCSIGME, (char *)SIGUSR1) < 0) warn("ioctl:TCSIGME"); /* * Make processing loop check terminal characteristics early on. */ termstat(); #endif #ifdef TIOCNOTTY { register int t; t = open(_PATH_TTY, O_RDWR); if (t >= 0) { (void) ioctl(t, TIOCNOTTY, (char *)0); (void) close(t); } } #endif #if defined(CRAY) && defined(NEWINIT) && defined(TIOCSCTTY) (void) setsid(); ioctl(p, TIOCSCTTY, 0); #endif /* * Show banner that getty never gave. * * We put the banner in the pty input buffer. This way, it * gets carriage return null processing, etc., just like all * other pty --> client data. */ #if !defined(CRAY) || !defined(NEWINIT) if (getenv("USER")) hostinfo = 0; #endif if (getent(defent, "default") == 1) { char *Getstr(); char *cp=defstrs; HE = Getstr("he", &cp); HN = Getstr("hn", &cp); IM = Getstr("im", &cp); IF = Getstr("if", &cp); if (HN && *HN) (void) strlcpy(host_name, HN, sizeof(host_name)); if (IF && (if_fd = open(IF, O_RDONLY, 000)) != -1) IM = 0; if (IM == 0) IM = ""; } else { IM = DEFAULT_IM; HE = 0; } edithost(HE, host_name); if (hostinfo && *IM) putf(IM, ptyibuf2); else if (IF && if_fd != -1) { fstat (if_fd, &statbuf); if_buf = (char *) mmap (0, statbuf.st_size, PROT_READ, 0, if_fd, 0); putf(if_buf, ptyibuf2); munmap (if_buf, statbuf.st_size); close (if_fd); } if (pcc) (void) strncat(ptyibuf2, ptyip, pcc+1); ptyip = ptyibuf2; pcc = strlen(ptyip); #ifdef LINEMODE /* * Last check to make sure all our states are correct. */ init_termbuf(); localstat(); #endif /* LINEMODE */ DIAG(TD_REPORT, {sprintf(nfrontp, "td: Entering processing loop\r\n"); nfrontp += strlen(nfrontp);}); /* * Startup the login process on the slave side of the terminal * now. We delay this until here to insure option negotiation * is complete. */ startslave(host, level, user_name); for (;;) { fd_set ibits, obits, xbits; register int c; if (ncc < 0 && pcc < 0) break; #if defined(CRAY2) && defined(UNICOS5) if (needtermstat) _termstat(); #endif /* defined(CRAY2) && defined(UNICOS5) */ FD_ZERO(&ibits); FD_ZERO(&obits); FD_ZERO(&xbits); /* * Never look for input if there's still * stuff in the corresponding output buffer */ if (nfrontp - nbackp || pcc > 0) { FD_SET(f, &obits); } else { FD_SET(p, &ibits); } if (pfrontp - pbackp || ncc > 0) { FD_SET(p, &obits); } else { FD_SET(f, &ibits); } if (!SYNCHing) { FD_SET(f, &xbits); } if ((c = select(16, &ibits, &obits, &xbits, (struct timeval *)0)) < 1) { if (c == -1) { if (errno == EINTR) { continue; } } sleep(5); continue; } /* * Any urgent data? */ if (FD_ISSET(net, &xbits)) { SYNCHing = 1; } /* * Something to read from the network... */ if (FD_ISSET(net, &ibits)) { #if !defined(SO_OOBINLINE) /* * In 4.2 (and 4.3 beta) systems, the * OOB indication and data handling in the kernel * is such that if two separate TCP Urgent requests * come in, one byte of TCP data will be overlaid. * This is fatal for Telnet, but we try to live * with it. * * In addition, in 4.2 (and...), a special protocol * is needed to pick up the TCP Urgent data in * the correct sequence. * * What we do is: if we think we are in urgent * mode, we look to see if we are "at the mark". * If we are, we do an OOB receive. If we run * this twice, we will do the OOB receive twice, * but the second will fail, since the second * time we were "at the mark", but there wasn't * any data there (the kernel doesn't reset * "at the mark" until we do a normal read). * Once we've read the OOB data, we go ahead * and do normal reads. * * There is also another problem, which is that * since the OOB byte we read doesn't put us * out of OOB state, and since that byte is most * likely the TELNET DM (data mark), we would * stay in the TELNET SYNCH (SYNCHing) state. * So, clocks to the rescue. If we've "just" * received a DM, then we test for the * presence of OOB data when the receive OOB * fails (and AFTER we did the normal mode read * to clear "at the mark"). */ if (SYNCHing) { int atmark; (void) ioctl(net, SIOCATMARK, (char *)&atmark); if (atmark) { ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB); if ((ncc == -1) && (errno == EINVAL)) { ncc = read(net, netibuf, sizeof (netibuf)); if (sequenceIs(didnetreceive, gotDM)) { SYNCHing = stilloob(net); } } } else { ncc = read(net, netibuf, sizeof (netibuf)); } } else { ncc = read(net, netibuf, sizeof (netibuf)); } settimer(didnetreceive); #else /* !defined(SO_OOBINLINE)) */ ncc = read(net, netibuf, sizeof (netibuf)); #endif /* !defined(SO_OOBINLINE)) */ if (ncc < 0 && errno == EWOULDBLOCK) ncc = 0; else { if (ncc <= 0) { break; } netip = netibuf; } DIAG((TD_REPORT | TD_NETDATA), {sprintf(nfrontp, "td: netread %d chars\r\n", ncc); nfrontp += strlen(nfrontp);}); DIAG(TD_NETDATA, printdata("nd", netip, ncc)); } /* * Something to read from the pty... */ if (FD_ISSET(p, &ibits)) { #ifndef STREAMSPTY pcc = read(p, ptyibuf, BUFSIZ); #else pcc = readstream(p, ptyibuf, BUFSIZ); #endif /* * On some systems, if we try to read something * off the master side before the slave side is * opened, we get EIO. */ if (pcc < 0 && (errno == EWOULDBLOCK || #ifdef EAGAIN errno == EAGAIN || #endif errno == EIO)) { pcc = 0; } else { if (pcc <= 0) break; #if !defined(CRAY2) || !defined(UNICOS5) #ifdef LINEMODE /* * If ioctl from pty, pass it through net */ if (ptyibuf[0] & TIOCPKT_IOCTL) { copy_termbuf(ptyibuf+1, pcc-1); localstat(); pcc = 1; } #endif /* LINEMODE */ if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) { netclear(); /* clear buffer back */ #ifndef NO_URGENT /* * There are client telnets on some * operating systems get screwed up * royally if we send them urgent * mode data. */ *nfrontp++ = IAC; *nfrontp++ = DM; neturg = nfrontp-1; /* off by one XXX */ #endif } if (his_state_is_will(TELOPT_LFLOW) && (ptyibuf[0] & (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) { int newflow = ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0; if (newflow != flowmode) { flowmode = newflow; (void) sprintf(nfrontp, "%c%c%c%c%c%c", IAC, SB, TELOPT_LFLOW, flowmode ? LFLOW_ON : LFLOW_OFF, IAC, SE); nfrontp += 6; } } pcc--; ptyip = ptyibuf+1; #else /* defined(CRAY2) && defined(UNICOS5) */ if (!uselinemode) { unpcc = pcc; unptyip = ptyibuf; pcc = term_output(&unptyip, ptyibuf2, &unpcc, BUFSIZ); ptyip = ptyibuf2; } else ptyip = ptyibuf; #endif /* defined(CRAY2) && defined(UNICOS5) */ } } while (pcc > 0) { if ((&netobuf[BUFSIZ] - nfrontp) < 2) break; c = *ptyip++ & 0377, pcc--; if (c == IAC) *nfrontp++ = c; #if defined(CRAY2) && defined(UNICOS5) else if (c == '\n' && my_state_is_wont(TELOPT_BINARY) && newmap) *nfrontp++ = '\r'; #endif /* defined(CRAY2) && defined(UNICOS5) */ *nfrontp++ = c; if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) { if (pcc > 0 && ((*ptyip & 0377) == '\n')) { *nfrontp++ = *ptyip++ & 0377; pcc--; } else *nfrontp++ = '\0'; } } #if defined(CRAY2) && defined(UNICOS5) /* * If chars were left over from the terminal driver, * note their existence. */ if (!uselinemode && unpcc) { pcc = unpcc; unpcc = 0; ptyip = unptyip; } #endif /* defined(CRAY2) && defined(UNICOS5) */ if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0) netflush(); if (ncc > 0) telrcv(); if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0) ptyflush(); } cleanup(0); } /* end of telnet */ #ifndef TCSIG # ifdef TIOCSIG # define TCSIG TIOCSIG # endif #endif #ifdef STREAMSPTY int flowison = -1; /* current state of flow: -1 is unknown */ int readstream(p, ibuf, bufsize) int p; char *ibuf; int bufsize; { int flags = 0; int ret = 0; struct termios *tsp; struct termio *tp; struct iocblk *ip; char vstop, vstart; int ixon; int newflow; strbufc.maxlen = BUFSIZ; strbufc.buf = (char *)ctlbuf; strbufd.maxlen = bufsize-1; strbufd.len = 0; strbufd.buf = ibuf+1; ibuf[0] = 0; ret = getmsg(p, &strbufc, &strbufd, &flags); if (ret < 0) /* error of some sort -- probably EAGAIN */ return(-1); if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) { /* data message */ if (strbufd.len > 0) { /* real data */ return(strbufd.len + 1); /* count header char */ } else { /* nothing there */ errno = EAGAIN; return(-1); } } /* * It's a control message. Return 1, to look at the flag we set */ switch (ctlbuf[0]) { case M_FLUSH: if (ibuf[1] & FLUSHW) ibuf[0] = TIOCPKT_FLUSHWRITE; return(1); case M_IOCTL: ip = (struct iocblk *) (ibuf+1); switch (ip->ioc_cmd) { case TCSETS: case TCSETSW: case TCSETSF: tsp = (struct termios *) (ibuf+1 + sizeof(struct iocblk)); vstop = tsp->c_cc[VSTOP]; vstart = tsp->c_cc[VSTART]; ixon = tsp->c_iflag & IXON; break; case TCSETA: case TCSETAW: case TCSETAF: tp = (struct termio *) (ibuf+1 + sizeof(struct iocblk)); vstop = tp->c_cc[VSTOP]; vstart = tp->c_cc[VSTART]; ixon = tp->c_iflag & IXON; break; default: errno = EAGAIN; return(-1); } newflow = (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0; if (newflow != flowison) { /* it's a change */ flowison = newflow; ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP; return(1); } } /* nothing worth doing anything about */ errno = EAGAIN; return(-1); } #endif /* STREAMSPTY */ /* * Send interrupt to process on other side of pty. * If it is in raw mode, just write NULL; * otherwise, write intr char. */ void interrupt() { ptyflush(); /* half-hearted */ #ifdef TCSIG (void) ioctl(pty, TCSIG, (char *)SIGINT); #else /* TCSIG */ init_termbuf(); *pfrontp++ = slctab[SLC_IP].sptr ? (unsigned char)*slctab[SLC_IP].sptr : '\177'; #endif /* TCSIG */ } /* * Send quit to process on other side of pty. * If it is in raw mode, just write NULL; * otherwise, write quit char. */ void sendbrk() { ptyflush(); /* half-hearted */ #ifdef TCSIG (void) ioctl(pty, TCSIG, (char *)SIGQUIT); #else /* TCSIG */ init_termbuf(); *pfrontp++ = slctab[SLC_ABORT].sptr ? (unsigned char)*slctab[SLC_ABORT].sptr : '\034'; #endif /* TCSIG */ } void sendsusp() { #ifdef SIGTSTP ptyflush(); /* half-hearted */ # ifdef TCSIG (void) ioctl(pty, TCSIG, (char *)SIGTSTP); # else /* TCSIG */ *pfrontp++ = slctab[SLC_SUSP].sptr ? (unsigned char)*slctab[SLC_SUSP].sptr : '\032'; # endif /* TCSIG */ #endif /* SIGTSTP */ } /* * When we get an AYT, if ^T is enabled, use that. Otherwise, * just send back "[Yes]". */ void recv_ayt() { #if defined(SIGINFO) && defined(TCSIG) if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) { (void) ioctl(pty, TCSIG, (char *)SIGINFO); return; } #endif (void) strcpy(nfrontp, "\r\n[Yes]\r\n"); nfrontp += 9; } void doeof() { init_termbuf(); #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN) if (!tty_isediting()) { extern char oldeofc; *pfrontp++ = oldeofc; return; } #endif *pfrontp++ = slctab[SLC_EOF].sptr ? (unsigned char)*slctab[SLC_EOF].sptr : '\004'; } diff --git a/sys/compat/svr4/svr4_stream.c b/sys/compat/svr4/svr4_stream.c index 6bf2155e7979..87e8482287aa 100644 --- a/sys/compat/svr4/svr4_stream.c +++ b/sys/compat/svr4/svr4_stream.c @@ -1,2274 +1,2274 @@ /* * Copyright (c) 1998 Mark Newton. All rights reserved. * Copyright (c) 1994, 1996 Christos Zoulas. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Christos Zoulas. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ /* * Pretend that we have streams... * Yes, this is gross. * * ToDo: The state machine for getmsg needs re-thinking */ #define COMPAT_43 1 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* Utils */ static int clean_pipe __P((struct proc *, const char *)); static void getparm __P((struct file *, struct svr4_si_sockparms *)); /* Address Conversions */ static void sockaddr_to_netaddr_in __P((struct svr4_strmcmd *, const struct sockaddr_in *)); static void sockaddr_to_netaddr_un __P((struct svr4_strmcmd *, const struct sockaddr_un *)); static void netaddr_to_sockaddr_in __P((struct sockaddr_in *, const struct svr4_strmcmd *)); static void netaddr_to_sockaddr_un __P((struct sockaddr_un *, const struct svr4_strmcmd *)); /* stream ioctls */ static int i_nread __P((struct file *, struct proc *, register_t *, int, u_long, caddr_t)); static int i_fdinsert __P((struct file *, struct proc *, register_t *, int, u_long, caddr_t)); static int i_str __P((struct file *, struct proc *, register_t *, int, u_long, caddr_t)); static int i_setsig __P((struct file *, struct proc *, register_t *, int, u_long, caddr_t)); static int i_getsig __P((struct file *, struct proc *, register_t *, int, u_long, caddr_t)); static int _i_bind_rsvd __P((struct file *, struct proc *, register_t *, int, u_long, caddr_t)); static int _i_rele_rsvd __P((struct file *, struct proc *, register_t *, int, u_long, caddr_t)); /* i_str sockmod calls */ static int sockmod __P((struct file *, int, struct svr4_strioctl *, struct proc *)); static int si_listen __P((struct file *, int, struct svr4_strioctl *, struct proc *)); static int si_ogetudata __P((struct file *, int, struct svr4_strioctl *, struct proc *)); static int si_sockparams __P((struct file *, int, struct svr4_strioctl *, struct proc *)); static int si_shutdown __P((struct file *, int, struct svr4_strioctl *, struct proc *)); static int si_getudata __P((struct file *, int, struct svr4_strioctl *, struct proc *)); /* i_str timod calls */ static int timod __P((struct file *, int, struct svr4_strioctl *, struct proc *)); static int ti_getinfo __P((struct file *, int, struct svr4_strioctl *, struct proc *)); static int ti_bind __P((struct file *, int, struct svr4_strioctl *, struct proc *)); /* infrastructure */ static int svr4_sendit __P((struct proc *p, int s, struct msghdr *mp, int flags)); static int svr4_recvit __P((struct proc *p, int s, struct msghdr *mp, caddr_t namelenp)); /* Ok, so we shouldn't use sendit() in uipc_syscalls.c because * it isn't part of a "public" interface; We're supposed to use * pru_sosend instead. Same goes for recvit()/pru_soreceive() for * that matter. Solution: Suck sendit()/recvit() into here where we * can do what we like. * * I hate code duplication. * * I will take out all the #ifdef COMPAT_OLDSOCK gumph, though. */ static int svr4_sendit(p, s, mp, flags) register struct proc *p; int s; register struct msghdr *mp; int flags; { struct file *fp; struct uio auio; register struct iovec *iov; register int i; struct mbuf *control; struct sockaddr *to; int len, error; struct socket *so; #ifdef KTRACE struct iovec *ktriov = NULL; struct uio ktruio; #endif error = holdsock(p->p_fd, s, &fp); if (error) return (error); auio.uio_iov = mp->msg_iov; auio.uio_iovcnt = mp->msg_iovlen; auio.uio_segflg = UIO_USERSPACE; auio.uio_rw = UIO_WRITE; auio.uio_procp = p; auio.uio_offset = 0; /* XXX */ auio.uio_resid = 0; iov = mp->msg_iov; for (i = 0; i < mp->msg_iovlen; i++, iov++) { if ((auio.uio_resid += iov->iov_len) < 0) { fdrop(fp, p); return (EINVAL); } } if (mp->msg_name) { error = getsockaddr(&to, mp->msg_name, mp->msg_namelen); if (error) { fdrop(fp, p); return (error); } } else { to = 0; } if (mp->msg_control) { if (mp->msg_controllen < sizeof(struct cmsghdr)) { error = EINVAL; goto bad; } error = sockargs(&control, mp->msg_control, mp->msg_controllen, MT_CONTROL); if (error) goto bad; } else { control = 0; } #ifdef KTRACE if (KTRPOINT(p, KTR_GENIO)) { int iovlen = auio.uio_iovcnt * sizeof (struct iovec); MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); ktruio = auio; } #endif len = auio.uio_resid; so = (struct socket *)fp->f_data; error = so->so_proto->pr_usrreqs->pru_sosend(so, to, &auio, 0, control, flags, p); if (error) { if (auio.uio_resid != len && (error == ERESTART || error == EINTR || error == EWOULDBLOCK)) error = 0; if (error == EPIPE) psignal(p, SIGPIPE); } if (error == 0) p->p_retval[0] = len - auio.uio_resid; #ifdef KTRACE if (ktriov != NULL) { if (error == 0) { ktruio.uio_iov = ktriov; ktruio.uio_resid = p->p_retval[0]; ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error); } FREE(ktriov, M_TEMP); } #endif bad: if (to) FREE(to, M_SONAME); fdrop(fp, p); return (error); } static int svr4_recvit(p, s, mp, namelenp) register struct proc *p; int s; register struct msghdr *mp; caddr_t namelenp; { struct file *fp; struct uio auio; register struct iovec *iov; register int i; int len, error; struct mbuf *m, *control = 0; caddr_t ctlbuf; struct socket *so; struct sockaddr *fromsa = 0; #ifdef KTRACE struct iovec *ktriov = NULL; struct uio ktruio; #endif error = holdsock(p->p_fd, s, &fp); if (error) return (error); auio.uio_iov = mp->msg_iov; auio.uio_iovcnt = mp->msg_iovlen; auio.uio_segflg = UIO_USERSPACE; auio.uio_rw = UIO_READ; auio.uio_procp = p; auio.uio_offset = 0; /* XXX */ auio.uio_resid = 0; iov = mp->msg_iov; for (i = 0; i < mp->msg_iovlen; i++, iov++) { if ((auio.uio_resid += iov->iov_len) < 0) { fdrop(fp, p); return (EINVAL); } } #ifdef KTRACE if (KTRPOINT(p, KTR_GENIO)) { int iovlen = auio.uio_iovcnt * sizeof (struct iovec); MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); ktruio = auio; } #endif len = auio.uio_resid; so = (struct socket *)fp->f_data; error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio, (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0, &mp->msg_flags); if (error) { if (auio.uio_resid != len && (error == ERESTART || error == EINTR || error == EWOULDBLOCK)) error = 0; } #ifdef KTRACE if (ktriov != NULL) { if (error == 0) { ktruio.uio_iov = ktriov; ktruio.uio_resid = len - auio.uio_resid; ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error); } FREE(ktriov, M_TEMP); } #endif if (error) goto out; p->p_retval[0] = len - auio.uio_resid; if (mp->msg_name) { len = mp->msg_namelen; if (len <= 0 || fromsa == 0) len = 0; else { #ifndef MIN #define MIN(a,b) ((a)>(b)?(b):(a)) #endif /* save sa_len before it is destroyed by MSG_COMPAT */ len = MIN(len, fromsa->sa_len); error = copyout(fromsa, (caddr_t)mp->msg_name, (unsigned)len); if (error) goto out; } mp->msg_namelen = len; if (namelenp && (error = copyout((caddr_t)&len, namelenp, sizeof (int)))) { goto out; } } if (mp->msg_control) { len = mp->msg_controllen; m = control; mp->msg_controllen = 0; ctlbuf = (caddr_t) mp->msg_control; while (m && len > 0) { unsigned int tocopy; if (len >= m->m_len) tocopy = m->m_len; else { mp->msg_flags |= MSG_CTRUNC; tocopy = len; } if ((error = copyout((caddr_t)mtod(m, caddr_t), ctlbuf, tocopy)) != 0) goto out; ctlbuf += tocopy; len -= tocopy; m = m->m_next; } mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control; } out: if (fromsa) FREE(fromsa, M_SONAME); if (control) m_freem(control); fdrop(fp, p); return (error); } #ifdef DEBUG_SVR4 static void bufprint __P((u_char *, size_t)); static int show_ioc __P((const char *, struct svr4_strioctl *)); static int show_strbuf __P((struct svr4_strbuf *)); static void show_msg __P((const char *, int, struct svr4_strbuf *, struct svr4_strbuf *, int)); static void bufprint(buf, len) u_char *buf; size_t len; { size_t i; uprintf("\n\t"); for (i = 0; i < len; i++) { uprintf("%x ", buf[i]); if (i && (i % 16) == 0) uprintf("\n\t"); } } static int show_ioc(str, ioc) const char *str; struct svr4_strioctl *ioc; { u_char *ptr = (u_char *) malloc(ioc->len, M_TEMP, M_WAITOK); int error; uprintf("%s cmd = %ld, timeout = %d, len = %d, buf = %p { ", str, ioc->cmd, ioc->timeout, ioc->len, ioc->buf); if ((error = copyin(ioc->buf, ptr, ioc->len)) != 0) { free((char *) ptr, M_TEMP); return error; } bufprint(ptr, ioc->len); uprintf("}\n"); free((char *) ptr, M_TEMP); return 0; } static int show_strbuf(str) struct svr4_strbuf *str; { int error; u_char *ptr = NULL; int maxlen = str->maxlen; int len = str->len; if (maxlen < 0) maxlen = 0; if (len >= maxlen) len = maxlen; if (len > 0) { ptr = (u_char *) malloc(len, M_TEMP, M_WAITOK); if ((error = copyin(str->buf, ptr, len)) != 0) { free((char *) ptr, M_TEMP); return error; } } uprintf(", { %d, %d, %p=[ ", str->maxlen, str->len, str->buf); if (ptr) bufprint(ptr, len); uprintf("]}"); if (ptr) free((char *) ptr, M_TEMP); return 0; } static void show_msg(str, fd, ctl, dat, flags) const char *str; int fd; struct svr4_strbuf *ctl; struct svr4_strbuf *dat; int flags; { struct svr4_strbuf buf; int error; uprintf("%s(%d", str, fd); if (ctl != NULL) { if ((error = copyin(ctl, &buf, sizeof(buf))) != 0) return; show_strbuf(&buf); } else uprintf(", NULL"); if (dat != NULL) { if ((error = copyin(dat, &buf, sizeof(buf))) != 0) return; show_strbuf(&buf); } else uprintf(", NULL"); uprintf(", %x);\n", flags); } #endif /* DEBUG_SVR4 */ /* * We are faced with an interesting situation. On svr4 unix sockets * are really pipes. But we really have sockets, and we might as * well use them. At the point where svr4 calls TI_BIND, it has * already created a named pipe for the socket using mknod(2). * We need to create a socket with the same name when we bind, * so we need to remove the pipe before, otherwise we'll get address * already in use. So we *carefully* remove the pipe, to avoid * using this as a random file removal tool. We use system calls * to avoid code duplication. */ static int clean_pipe(p, path) struct proc *p; const char *path; { struct lstat_args la; struct unlink_args ua; struct stat st; int error; caddr_t sg = stackgap_init(); size_t l = strlen(path) + 1; void *tpath; tpath = stackgap_alloc(&sg, l); SCARG(&la, ub) = stackgap_alloc(&sg, sizeof(struct stat)); if ((error = copyout(path, tpath, l)) != 0) return error; SCARG(&la, path) = tpath; if ((error = lstat(p, &la)) != 0) return 0; if ((error = copyin(SCARG(&la, ub), &st, sizeof(st))) != 0) return 0; /* * Make sure we are dealing with a mode 0 named pipe. */ if ((st.st_mode & S_IFMT) != S_IFIFO) return 0; if ((st.st_mode & ALLPERMS) != 0) return 0; SCARG(&ua, path) = SCARG(&la, path); if ((error = unlink(p, &ua)) != 0) { DPRINTF(("clean_pipe: unlink failed %d\n", error)); return error; } return 0; } static void sockaddr_to_netaddr_in(sc, sain) struct svr4_strmcmd *sc; const struct sockaddr_in *sain; { struct svr4_netaddr_in *na; na = SVR4_ADDROF(sc); na->family = sain->sin_family; na->port = sain->sin_port; na->addr = sain->sin_addr.s_addr; DPRINTF(("sockaddr_in -> netaddr %d %d %lx\n", na->family, na->port, na->addr)); } static void sockaddr_to_netaddr_un(sc, saun) struct svr4_strmcmd *sc; const struct sockaddr_un *saun; { struct svr4_netaddr_un *na; char *dst, *edst = ((char *) sc) + sc->offs + sizeof(na->family) + 1 - sizeof(*sc); const char *src; na = SVR4_ADDROF(sc); na->family = saun->sun_family; for (src = saun->sun_path, dst = na->path; (*dst++ = *src++) != '\0'; ) if (dst == edst) break; DPRINTF(("sockaddr_un -> netaddr %d %s\n", na->family, na->path)); } static void netaddr_to_sockaddr_in(sain, sc) struct sockaddr_in *sain; const struct svr4_strmcmd *sc; { const struct svr4_netaddr_in *na; na = SVR4_C_ADDROF(sc); memset(sain, 0, sizeof(*sain)); sain->sin_len = sizeof(*sain); sain->sin_family = na->family; sain->sin_port = na->port; sain->sin_addr.s_addr = na->addr; DPRINTF(("netaddr -> sockaddr_in %d %d %x\n", sain->sin_family, sain->sin_port, sain->sin_addr.s_addr)); } static void netaddr_to_sockaddr_un(saun, sc) struct sockaddr_un *saun; const struct svr4_strmcmd *sc; { const struct svr4_netaddr_un *na; char *dst, *edst = &saun->sun_path[sizeof(saun->sun_path) - 1]; const char *src; na = SVR4_C_ADDROF(sc); memset(saun, 0, sizeof(*saun)); saun->sun_family = na->family; for (src = na->path, dst = saun->sun_path; (*dst++ = *src++) != '\0'; ) if (dst == edst) break; saun->sun_len = dst - saun->sun_path; DPRINTF(("netaddr -> sockaddr_un %d %s\n", saun->sun_family, saun->sun_path)); } static void getparm(fp, pa) struct file *fp; struct svr4_si_sockparms *pa; { struct svr4_strm *st = svr4_stream_get(fp); struct socket *so = (struct socket *) fp->f_data; if (st == NULL) return; pa->family = st->s_family; switch (so->so_type) { case SOCK_DGRAM: pa->type = SVR4_T_CLTS; pa->protocol = IPPROTO_UDP; DPRINTF(("getparm(dgram)\n")); return; case SOCK_STREAM: pa->type = SVR4_T_COTS; /* What about T_COTS_ORD? XXX */ pa->protocol = IPPROTO_IP; DPRINTF(("getparm(stream)\n")); return; case SOCK_RAW: pa->type = SVR4_T_CLTS; pa->protocol = IPPROTO_RAW; DPRINTF(("getparm(raw)\n")); return; default: pa->type = 0; pa->protocol = 0; DPRINTF(("getparm(type %d?)\n", so->so_type)); return; } } static int si_ogetudata(fp, fd, ioc, p) struct file *fp; int fd; struct svr4_strioctl *ioc; struct proc *p; { int error; struct svr4_si_oudata ud; struct svr4_si_sockparms pa; if (ioc->len != sizeof(ud) && ioc->len != sizeof(ud) - sizeof(int)) { DPRINTF(("SI_OGETUDATA: Wrong size %d != %d\n", sizeof(ud), ioc->len)); return EINVAL; } if ((error = copyin(ioc->buf, &ud, sizeof(ud))) != 0) return error; getparm(fp, &pa); switch (pa.family) { case AF_INET: ud.tidusize = 16384; ud.addrsize = sizeof(struct svr4_sockaddr_in); if (pa.type == SVR4_SOCK_STREAM) ud.etsdusize = 1; else ud.etsdusize = 0; break; case AF_LOCAL: ud.tidusize = 65536; ud.addrsize = 128; ud.etsdusize = 128; break; default: DPRINTF(("SI_OGETUDATA: Unsupported address family %d\n", pa.family)); return ENOSYS; } /* I have no idea what these should be! */ ud.optsize = 128; ud.tsdusize = 128; ud.servtype = pa.type; /* XXX: Fixme */ ud.so_state = 0; ud.so_options = 0; return copyout(&ud, ioc->buf, ioc->len); } static int si_sockparams(fp, fd, ioc, p) struct file *fp; int fd; struct svr4_strioctl *ioc; struct proc *p; { struct svr4_si_sockparms pa; getparm(fp, &pa); return copyout(&pa, ioc->buf, sizeof(pa)); } static int si_listen(fp, fd, ioc, p) struct file *fp; int fd; struct svr4_strioctl *ioc; struct proc *p; { int error; struct svr4_strm *st = svr4_stream_get(fp); struct svr4_strmcmd lst; struct listen_args la; if (st == NULL) return EINVAL; if ((error = copyin(ioc->buf, &lst, ioc->len)) != 0) return error; if (lst.cmd != SVR4_TI_OLD_BIND_REQUEST) { DPRINTF(("si_listen: bad request %ld\n", lst.cmd)); return EINVAL; } /* * We are making assumptions again... */ SCARG(&la, s) = fd; DPRINTF(("SI_LISTEN: fileno %d backlog = %d\n", fd, 5)); SCARG(&la, backlog) = 5; if ((error = listen(p, &la)) != 0) { DPRINTF(("SI_LISTEN: listen failed %d\n", error)); return error; } st->s_cmd = SVR4_TI__ACCEPT_WAIT; lst.cmd = SVR4_TI_BIND_REPLY; switch (st->s_family) { case AF_INET: /* XXX: Fill the length here */ break; case AF_LOCAL: lst.len = 140; lst.pad[28] = 0x00000000; /* magic again */ lst.pad[29] = 0x00000800; /* magic again */ lst.pad[30] = 0x80001400; /* magic again */ break; default: DPRINTF(("SI_LISTEN: Unsupported address family %d\n", st->s_family)); return ENOSYS; } if ((error = copyout(&lst, ioc->buf, ioc->len)) != 0) return error; return 0; } static int si_getudata(fp, fd, ioc, p) struct file *fp; int fd; struct svr4_strioctl *ioc; struct proc *p; { int error; struct svr4_si_udata ud; if (sizeof(ud) != ioc->len) { DPRINTF(("SI_GETUDATA: Wrong size %d != %d\n", sizeof(ud), ioc->len)); return EINVAL; } if ((error = copyin(ioc->buf, &ud, sizeof(ud))) != 0) return error; getparm(fp, &ud.sockparms); switch (ud.sockparms.family) { case AF_INET: DPRINTF(("getudata_inet\n")); ud.tidusize = 16384; ud.tsdusize = 16384; ud.addrsize = sizeof(struct svr4_sockaddr_in); if (ud.sockparms.type == SVR4_SOCK_STREAM) ud.etsdusize = 1; else ud.etsdusize = 0; ud.optsize = 0; break; case AF_LOCAL: DPRINTF(("getudata_local\n")); ud.tidusize = 65536; ud.tsdusize = 128; ud.addrsize = 128; ud.etsdusize = 128; ud.optsize = 128; break; default: DPRINTF(("SI_GETUDATA: Unsupported address family %d\n", ud.sockparms.family)); return ENOSYS; } ud.servtype = ud.sockparms.type; DPRINTF(("ud.servtype = %d\n", ud.servtype)); /* XXX: Fixme */ ud.so_state = 0; ud.so_options = 0; return copyout(&ud, ioc->buf, sizeof(ud)); } static int si_shutdown(fp, fd, ioc, p) struct file *fp; int fd; struct svr4_strioctl *ioc; struct proc *p; { int error; struct shutdown_args ap; if (ioc->len != sizeof(SCARG(&ap, how))) { DPRINTF(("SI_SHUTDOWN: Wrong size %d != %d\n", sizeof(SCARG(&ap, how)), ioc->len)); return EINVAL; } if ((error = copyin(ioc->buf, &SCARG(&ap, how), ioc->len)) != 0) return error; SCARG(&ap, s) = fd; return shutdown(p, &ap); } static int sockmod(fp, fd, ioc, p) struct file *fp; int fd; struct svr4_strioctl *ioc; struct proc *p; { switch (ioc->cmd) { case SVR4_SI_OGETUDATA: DPRINTF(("SI_OGETUDATA\n")); return si_ogetudata(fp, fd, ioc, p); case SVR4_SI_SHUTDOWN: DPRINTF(("SI_SHUTDOWN\n")); return si_shutdown(fp, fd, ioc, p); case SVR4_SI_LISTEN: DPRINTF(("SI_LISTEN\n")); return si_listen(fp, fd, ioc, p); case SVR4_SI_SETMYNAME: DPRINTF(("SI_SETMYNAME\n")); return 0; case SVR4_SI_SETPEERNAME: DPRINTF(("SI_SETPEERNAME\n")); return 0; case SVR4_SI_GETINTRANSIT: DPRINTF(("SI_GETINTRANSIT\n")); return 0; case SVR4_SI_TCL_LINK: DPRINTF(("SI_TCL_LINK\n")); return 0; case SVR4_SI_TCL_UNLINK: DPRINTF(("SI_TCL_UNLINK\n")); return 0; case SVR4_SI_SOCKPARAMS: DPRINTF(("SI_SOCKPARAMS\n")); return si_sockparams(fp, fd, ioc, p); case SVR4_SI_GETUDATA: DPRINTF(("SI_GETUDATA\n")); return si_getudata(fp, fd, ioc, p); default: DPRINTF(("Unknown sockmod ioctl %lx\n", ioc->cmd)); return 0; } } static int ti_getinfo(fp, fd, ioc, p) struct file *fp; int fd; struct svr4_strioctl *ioc; struct proc *p; { int error; struct svr4_infocmd info; memset(&info, 0, sizeof(info)); if ((error = copyin(ioc->buf, &info, ioc->len)) != 0) return error; if (info.cmd != SVR4_TI_INFO_REQUEST) return EINVAL; info.cmd = SVR4_TI_INFO_REPLY; info.tsdu = 0; info.etsdu = 1; info.cdata = -2; info.ddata = -2; info.addr = 16; info.opt = -1; info.tidu = 16384; info.serv = 2; info.current = 0; info.provider = 2; ioc->len = sizeof(info); if ((error = copyout(&info, ioc->buf, ioc->len)) != 0) return error; return 0; } static int ti_bind(fp, fd, ioc, p) struct file *fp; int fd; struct svr4_strioctl *ioc; struct proc *p; { int error; struct svr4_strm *st = svr4_stream_get(fp); struct sockaddr_in sain; struct sockaddr_un saun; caddr_t sg; void *skp, *sup = NULL; int sasize; struct svr4_strmcmd bnd; struct bind_args ba; if (st == NULL) { DPRINTF(("ti_bind: bad file descriptor\n")); return EINVAL; } if ((error = copyin(ioc->buf, &bnd, ioc->len)) != 0) return error; if (bnd.cmd != SVR4_TI_OLD_BIND_REQUEST) { DPRINTF(("ti_bind: bad request %ld\n", bnd.cmd)); return EINVAL; } switch (st->s_family) { case AF_INET: skp = &sain; sasize = sizeof(sain); if (bnd.offs == 0) goto reply; netaddr_to_sockaddr_in(&sain, &bnd); DPRINTF(("TI_BIND: fam %d, port %d, addr %x\n", sain.sin_family, sain.sin_port, sain.sin_addr.s_addr)); break; case AF_LOCAL: skp = &saun; sasize = sizeof(saun); if (bnd.offs == 0) goto reply; netaddr_to_sockaddr_un(&saun, &bnd); if (saun.sun_path[0] == '\0') goto reply; DPRINTF(("TI_BIND: fam %d, path %s\n", saun.sun_family, saun.sun_path)); if ((error = clean_pipe(p, saun.sun_path)) != 0) return error; bnd.pad[28] = 0x00001000; /* magic again */ break; default: DPRINTF(("TI_BIND: Unsupported address family %d\n", st->s_family)); return ENOSYS; } sg = stackgap_init(); sup = stackgap_alloc(&sg, sasize); if ((error = copyout(skp, sup, sasize)) != 0) return error; SCARG(&ba, s) = fd; DPRINTF(("TI_BIND: fileno %d\n", fd)); SCARG(&ba, name) = (void *) sup; SCARG(&ba, namelen) = sasize; if ((error = bind(p, &ba)) != 0) { DPRINTF(("TI_BIND: bind failed %d\n", error)); return error; } reply: if (sup == NULL) { memset(&bnd, 0, sizeof(bnd)); bnd.len = sasize + 4; bnd.offs = 0x10; /* XXX */ } bnd.cmd = SVR4_TI_BIND_REPLY; if ((error = copyout(&bnd, ioc->buf, ioc->len)) != 0) return error; return 0; } static int timod(fp, fd, ioc, p) struct file *fp; int fd; struct svr4_strioctl *ioc; struct proc *p; { switch (ioc->cmd) { case SVR4_TI_GETINFO: DPRINTF(("TI_GETINFO\n")); return ti_getinfo(fp, fd, ioc, p); case SVR4_TI_OPTMGMT: DPRINTF(("TI_OPTMGMT\n")); return 0; case SVR4_TI_BIND: DPRINTF(("TI_BIND\n")); return ti_bind(fp, fd, ioc, p); case SVR4_TI_UNBIND: DPRINTF(("TI_UNBIND\n")); return 0; default: DPRINTF(("Unknown timod ioctl %lx\n", ioc->cmd)); return 0; } } int svr4_stream_ti_ioctl(fp, p, retval, fd, cmd, dat) struct file *fp; struct proc *p; register_t *retval; int fd; u_long cmd; caddr_t dat; { struct svr4_strbuf skb, *sub = (struct svr4_strbuf *) dat; struct svr4_strm *st = svr4_stream_get(fp); int error; void *skp, *sup; struct sockaddr_in sain; struct sockaddr_un saun; struct svr4_strmcmd sc; int sasize; caddr_t sg; int *lenp; DPRINTF(("svr4_stream_ti_ioctl\n")); if (st == NULL) return EINVAL; sc.offs = 0x10; if ((error = copyin(sub, &skb, sizeof(skb))) != 0) { DPRINTF(("ti_ioctl: error copying in strbuf\n")); return error; } switch (st->s_family) { case AF_INET: skp = &sain; sasize = sizeof(sain); break; case AF_LOCAL: skp = &saun; sasize = sizeof(saun); break; default: DPRINTF(("ti_ioctl: Unsupported address family %d\n", st->s_family)); return ENOSYS; } sg = stackgap_init(); sup = stackgap_alloc(&sg, sasize); lenp = stackgap_alloc(&sg, sizeof(*lenp)); if ((error = copyout(&sasize, lenp, sizeof(*lenp))) != 0) { DPRINTF(("ti_ioctl: error copying out lenp\n")); return error; } switch (cmd) { case SVR4_TI_GETMYNAME: DPRINTF(("TI_GETMYNAME\n")); { struct getsockname_args ap; SCARG(&ap, fdes) = fd; SCARG(&ap, asa) = sup; SCARG(&ap, alen) = lenp; if ((error = getsockname(p, &ap)) != 0) { DPRINTF(("ti_ioctl: getsockname error\n")); return error; } } break; case SVR4_TI_GETPEERNAME: DPRINTF(("TI_GETPEERNAME\n")); { struct getpeername_args ap; SCARG(&ap, fdes) = fd; SCARG(&ap, asa) = sup; SCARG(&ap, alen) = lenp; if ((error = getpeername(p, &ap)) != 0) { DPRINTF(("ti_ioctl: getpeername error\n")); return error; } } break; case SVR4_TI_SETMYNAME: DPRINTF(("TI_SETMYNAME\n")); return 0; case SVR4_TI_SETPEERNAME: DPRINTF(("TI_SETPEERNAME\n")); return 0; default: DPRINTF(("ti_ioctl: Unknown ioctl %lx\n", cmd)); return ENOSYS; } if ((error = copyin(sup, skp, sasize)) != 0) { DPRINTF(("ti_ioctl: error copying in socket data\n")); return error; } if ((error = copyin(lenp, &sasize, sizeof(*lenp))) != 0) { DPRINTF(("ti_ioctl: error copying in socket size\n")); return error; } switch (st->s_family) { case AF_INET: sockaddr_to_netaddr_in(&sc, &sain); skb.len = sasize; break; case AF_LOCAL: sockaddr_to_netaddr_un(&sc, &saun); skb.len = sasize + 4; break; default: return ENOSYS; } if ((error = copyout(SVR4_ADDROF(&sc), skb.buf, sasize)) != 0) { DPRINTF(("ti_ioctl: error copying out socket data\n")); return error; } if ((error = copyout(&skb, sub, sizeof(skb))) != 0) { DPRINTF(("ti_ioctl: error copying out strbuf\n")); return error; } return error; } static int i_nread(fp, p, retval, fd, cmd, dat) struct file *fp; struct proc *p; register_t *retval; int fd; u_long cmd; caddr_t dat; { int error; int nread = 0; /* * We are supposed to return the message length in nread, and the * number of messages in retval. We don't have the notion of number * of stream messages, so we just find out if we have any bytes waiting * for us, and if we do, then we assume that we have at least one * message waiting for us. */ if ((error = fo_ioctl(fp, FIONREAD, (caddr_t) &nread, p)) != 0) return error; if (nread != 0) *retval = 1; else *retval = 0; return copyout(&nread, dat, sizeof(nread)); } static int i_fdinsert(fp, p, retval, fd, cmd, dat) struct file *fp; struct proc *p; register_t *retval; int fd; u_long cmd; caddr_t dat; { /* * Major hack again here. We assume that we are using this to * implement accept(2). If that is the case, we have already * called accept, and we have stored the file descriptor in * afd. We find the file descriptor that the code wants to use * in fd insert, and then we dup2() our accepted file descriptor * to it. */ int error; struct svr4_strm *st = svr4_stream_get(fp); struct svr4_strfdinsert fdi; struct dup2_args d2p; struct close_args clp; if (st == NULL) { DPRINTF(("fdinsert: bad file type\n")); return EINVAL; } if (st->s_afd == -1) { DPRINTF(("fdinsert: accept fd not found\n")); return ENOENT; } if ((error = copyin(dat, &fdi, sizeof(fdi))) != 0) { DPRINTF(("fdinsert: copyin failed %d\n", error)); return error; } SCARG(&d2p, from) = st->s_afd; SCARG(&d2p, to) = fdi.fd; if ((error = dup2(p, &d2p)) != 0) { DPRINTF(("fdinsert: dup2(%d, %d) failed %d\n", st->s_afd, fdi.fd, error)); return error; } SCARG(&clp, fd) = st->s_afd; if ((error = close(p, &clp)) != 0) { DPRINTF(("fdinsert: close(%d) failed %d\n", st->s_afd, error)); return error; } st->s_afd = -1; *retval = 0; return 0; } static int _i_bind_rsvd(fp, p, retval, fd, cmd, dat) struct file *fp; struct proc *p; register_t *retval; int fd; u_long cmd; caddr_t dat; { struct mkfifo_args ap; /* * This is a supposed to be a kernel and library only ioctl. * It gets called before ti_bind, when we have a unix * socket, to physically create the socket transport and * ``reserve'' it. I don't know how this get reserved inside * the kernel, but we are going to create it nevertheless. */ SCARG(&ap, path) = dat; SCARG(&ap, mode) = S_IFIFO; return mkfifo(p, &ap); } static int _i_rele_rsvd(fp, p, retval, fd, cmd, dat) struct file *fp; struct proc *p; register_t *retval; int fd; u_long cmd; caddr_t dat; { struct unlink_args ap; /* * This is a supposed to be a kernel and library only ioctl. * I guess it is supposed to release the socket. */ SCARG(&ap, path) = dat; return unlink(p, &ap); } static int i_str(fp, p, retval, fd, cmd, dat) struct file *fp; struct proc *p; register_t *retval; int fd; u_long cmd; caddr_t dat; { int error; struct svr4_strioctl ioc; if ((error = copyin(dat, &ioc, sizeof(ioc))) != 0) return error; #ifdef DEBUG_SVR4 if ((error = show_ioc(">", &ioc)) != 0) return error; #endif /* DEBUG_SVR4 */ switch (ioc.cmd & 0xff00) { case SVR4_SIMOD: if ((error = sockmod(fp, fd, &ioc, p)) != 0) return error; break; case SVR4_TIMOD: if ((error = timod(fp, fd, &ioc, p)) != 0) return error; break; default: DPRINTF(("Unimplemented module %c %ld\n", (char) (cmd >> 8), cmd & 0xff)); return 0; } #ifdef DEBUG_SVR4 if ((error = show_ioc("<", &ioc)) != 0) return error; #endif /* DEBUG_SVR4 */ return copyout(&ioc, dat, sizeof(ioc)); } static int i_setsig(fp, p, retval, fd, cmd, dat) struct file *fp; struct proc *p; register_t *retval; int fd; u_long cmd; caddr_t dat; { /* * This is the best we can do for now; we cannot generate * signals only for specific events so the signal mask gets * ignored; we save it just to pass it to a possible I_GETSIG... * * We alse have to fix the O_ASYNC fcntl bit, so the * process will get SIGPOLLs. */ struct fcntl_args fa; int error; register_t oflags, flags; struct svr4_strm *st = svr4_stream_get(fp); if (st == NULL) { DPRINTF(("i_setsig: bad file descriptor\n")); return EINVAL; } /* get old status flags */ SCARG(&fa, fd) = fd; SCARG(&fa, cmd) = F_GETFL; if ((error = fcntl(p, &fa)) != 0) return error; oflags = p->p_retval[0]; /* update the flags */ if (dat != NULL) { int mask; flags = oflags | O_ASYNC; if ((error = copyin(dat, &mask, sizeof(mask))) != 0) { DPRINTF(("i_setsig: bad eventmask pointer\n")); return error; } if (mask & SVR4_S_ALLMASK) { DPRINTF(("i_setsig: bad eventmask data %x\n", mask)); return EINVAL; } st->s_eventmask = mask; } else { flags = oflags & ~O_ASYNC; st->s_eventmask = 0; } /* set the new flags, if changed */ if (flags != oflags) { SCARG(&fa, cmd) = F_SETFL; SCARG(&fa, arg) = (long) flags; if ((error = fcntl(p, &fa)) != 0) return error; flags = p->p_retval[0]; } /* set up SIGIO receiver if needed */ if (dat != NULL) { SCARG(&fa, cmd) = F_SETOWN; SCARG(&fa, arg) = (long) p->p_pid; return fcntl(p, &fa); } return 0; } static int i_getsig(fp, p, retval, fd, cmd, dat) struct file *fp; struct proc *p; register_t *retval; int fd; u_long cmd; caddr_t dat; { int error; if (dat != NULL) { struct svr4_strm *st = svr4_stream_get(fp); if (st == NULL) { DPRINTF(("i_getsig: bad file descriptor\n")); return EINVAL; } if ((error = copyout(&st->s_eventmask, dat, sizeof(st->s_eventmask))) != 0) { DPRINTF(("i_getsig: bad eventmask pointer\n")); return error; } } return 0; } int svr4_stream_ioctl(fp, p, retval, fd, cmd, dat) struct file *fp; struct proc *p; register_t *retval; int fd; u_long cmd; caddr_t dat; { *retval = 0; /* * All the following stuff assumes "sockmod" is pushed... */ switch (cmd) { case SVR4_I_NREAD: DPRINTF(("I_NREAD\n")); return i_nread(fp, p, retval, fd, cmd, dat); case SVR4_I_PUSH: DPRINTF(("I_PUSH %x\n", dat)); #if defined(DEBUG_SVR4) show_strbuf(dat); #endif return 0; case SVR4_I_POP: DPRINTF(("I_POP\n")); return 0; case SVR4_I_LOOK: DPRINTF(("I_LOOK\n")); return 0; case SVR4_I_FLUSH: DPRINTF(("I_FLUSH\n")); return 0; case SVR4_I_SRDOPT: DPRINTF(("I_SRDOPT\n")); return 0; case SVR4_I_GRDOPT: DPRINTF(("I_GRDOPT\n")); return 0; case SVR4_I_STR: DPRINTF(("I_STR\n")); return i_str(fp, p, retval, fd, cmd, dat); case SVR4_I_SETSIG: DPRINTF(("I_SETSIG\n")); return i_setsig(fp, p, retval, fd, cmd, dat); case SVR4_I_GETSIG: DPRINTF(("I_GETSIG\n")); return i_getsig(fp, p, retval, fd, cmd, dat); case SVR4_I_FIND: DPRINTF(("I_FIND\n")); /* * Here we are not pushing modules really, we just * pretend all are present */ *retval = 0; return 0; case SVR4_I_LINK: DPRINTF(("I_LINK\n")); return 0; case SVR4_I_UNLINK: DPRINTF(("I_UNLINK\n")); return 0; case SVR4_I_ERECVFD: DPRINTF(("I_ERECVFD\n")); return 0; case SVR4_I_PEEK: DPRINTF(("I_PEEK\n")); return 0; case SVR4_I_FDINSERT: DPRINTF(("I_FDINSERT\n")); return i_fdinsert(fp, p, retval, fd, cmd, dat); case SVR4_I_SENDFD: DPRINTF(("I_SENDFD\n")); return 0; case SVR4_I_RECVFD: DPRINTF(("I_RECVFD\n")); return 0; case SVR4_I_SWROPT: DPRINTF(("I_SWROPT\n")); return 0; case SVR4_I_GWROPT: DPRINTF(("I_GWROPT\n")); return 0; case SVR4_I_LIST: DPRINTF(("I_LIST\n")); return 0; case SVR4_I_PLINK: DPRINTF(("I_PLINK\n")); return 0; case SVR4_I_PUNLINK: DPRINTF(("I_PUNLINK\n")); return 0; case SVR4_I_SETEV: DPRINTF(("I_SETEV\n")); return 0; case SVR4_I_GETEV: DPRINTF(("I_GETEV\n")); return 0; case SVR4_I_STREV: DPRINTF(("I_STREV\n")); return 0; case SVR4_I_UNSTREV: DPRINTF(("I_UNSTREV\n")); return 0; case SVR4_I_FLUSHBAND: DPRINTF(("I_FLUSHBAND\n")); return 0; case SVR4_I_CKBAND: DPRINTF(("I_CKBAND\n")); return 0; case SVR4_I_GETBAND: DPRINTF(("I_GETBANK\n")); return 0; case SVR4_I_ATMARK: DPRINTF(("I_ATMARK\n")); return 0; case SVR4_I_SETCLTIME: DPRINTF(("I_SETCLTIME\n")); return 0; case SVR4_I_GETCLTIME: DPRINTF(("I_GETCLTIME\n")); return 0; case SVR4_I_CANPUT: DPRINTF(("I_CANPUT\n")); return 0; case SVR4__I_BIND_RSVD: DPRINTF(("_I_BIND_RSVD\n")); return _i_bind_rsvd(fp, p, retval, fd, cmd, dat); case SVR4__I_RELE_RSVD: DPRINTF(("_I_RELE_RSVD\n")); return _i_rele_rsvd(fp, p, retval, fd, cmd, dat); default: DPRINTF(("unimpl cmd = %lx\n", cmd)); break; } return 0; } int svr4_sys_putmsg(p, uap) register struct proc *p; struct svr4_sys_putmsg_args *uap; { struct filedesc *fdp = p->p_fd; struct file *fp; struct svr4_strbuf dat, ctl; struct svr4_strmcmd sc; struct sockaddr_in sain; struct sockaddr_un saun; void *skp, *sup; int sasize, *retval; struct svr4_strm *st; int error; caddr_t sg; retval = p->p_retval; fp = fdp->fd_ofiles[SCARG(uap, fd)]; if (((u_int)SCARG(uap, fd) >= fdp->fd_nfiles) || (fp == NULL)) { #ifdef DEBUG_SVR4 uprintf("putmsg: bad fp\n"); #endif return EBADF; } #ifdef DEBUG_SVR4 show_msg(">putmsg", SCARG(uap, fd), SCARG(uap, ctl), SCARG(uap, dat), SCARG(uap, flags)); #endif /* DEBUG_SVR4 */ if (((u_int)SCARG(uap, fd) >= fdp->fd_nfiles) || (fp == NULL)) { #ifdef DEBUG_SVR4 uprintf("putmsg: bad fp(2)\n"); #endif return EBADF; } if (SCARG(uap, ctl) != NULL) { if ((error = copyin(SCARG(uap, ctl), &ctl, sizeof(ctl))) != 0) { #ifdef DEBUG_SVR4 uprintf("putmsg: copyin(): %d\n", error); #endif return error; } } else ctl.len = -1; if (SCARG(uap, dat) != NULL) { if ((error = copyin(SCARG(uap, dat), &dat, sizeof(dat))) != 0) { #ifdef DEBUG_SVR4 uprintf("putmsg: copyin(): %d (2)\n", error); #endif return error; } } else dat.len = -1; /* * Only for sockets for now. */ if ((st = svr4_stream_get(fp)) == NULL) { DPRINTF(("putmsg: bad file type\n")); return EINVAL; } if (ctl.len > sizeof(sc)) { DPRINTF(("putmsg: Bad control size %d != %d\n", ctl.len, sizeof(struct svr4_strmcmd))); return EINVAL; } if ((error = copyin(ctl.buf, &sc, ctl.len)) != 0) return error; switch (st->s_family) { case AF_INET: if (sc.len != sizeof(sain)) { if (sc.cmd == SVR4_TI_DATA_REQUEST) { struct write_args wa; /* Solaris seems to use sc.cmd = 3 to * send "expedited" data. telnet uses * this for options processing, sending EOF, * etc. I'm sure other things use it too. * I don't have any documentation * on it, so I'm making a guess that this * is how it works. newton@atdot.dotat.org XXX */ DPRINTF(("sending expedited data ??\n")); SCARG(&wa, fd) = SCARG(uap, fd); SCARG(&wa, buf) = dat.buf; SCARG(&wa, nbyte) = dat.len; return write(p, &wa); } DPRINTF(("putmsg: Invalid inet length %ld\n", sc.len)); return EINVAL; } netaddr_to_sockaddr_in(&sain, &sc); skp = &sain; sasize = sizeof(sain); error = sain.sin_family != st->s_family; break; case AF_LOCAL: if (ctl.len == 8) { /* We are doing an accept; succeed */ DPRINTF(("putmsg: Do nothing\n")); *retval = 0; return 0; } else { /* Maybe we've been given a device/inode pair */ udev_t *dev = SVR4_ADDROF(&sc); ino_t *ino = (ino_t *) &dev[1]; skp = svr4_find_socket(p, fp, *dev, *ino); if (skp == NULL) { skp = &saun; /* I guess we have it by name */ netaddr_to_sockaddr_un(skp, &sc); } sasize = sizeof(saun); } break; default: DPRINTF(("putmsg: Unsupported address family %d\n", st->s_family)); return ENOSYS; } sg = stackgap_init(); sup = stackgap_alloc(&sg, sasize); if ((error = copyout(skp, sup, sasize)) != 0) return error; switch (st->s_cmd = sc.cmd) { case SVR4_TI_CONNECT_REQUEST: /* connect */ { struct connect_args co; SCARG(&co, s) = SCARG(uap, fd); SCARG(&co, name) = (void *) sup; SCARG(&co, namelen) = (int) sasize; return connect(p, &co); } case SVR4_TI_SENDTO_REQUEST: /* sendto */ { struct msghdr msg; struct iovec aiov; msg.msg_name = (caddr_t) sup; msg.msg_namelen = sasize; msg.msg_iov = &aiov; msg.msg_iovlen = 1; msg.msg_control = 0; msg.msg_flags = 0; aiov.iov_base = dat.buf; aiov.iov_len = dat.len; #if 0 error = so->so_proto->pr_usrreqs->pru_sosend(so, 0, uio, 0, 0, 0, uio->uio_procp); #endif error = svr4_sendit(p, SCARG(uap, fd), &msg, SCARG(uap, flags)); DPRINTF(("sendto_request error: %d\n", error)); *retval = 0; return error; } default: DPRINTF(("putmsg: Unimplemented command %lx\n", sc.cmd)); return ENOSYS; } } int svr4_sys_getmsg(p, uap) register struct proc *p; struct svr4_sys_getmsg_args *uap; { struct filedesc *fdp = p->p_fd; struct file *fp; struct getpeername_args ga; struct accept_args aa; struct svr4_strbuf dat, ctl; struct svr4_strmcmd sc; int error, *retval; struct msghdr msg; struct iovec aiov; struct sockaddr_in sain; struct sockaddr_un saun; void *skp, *sup; int sasize; struct svr4_strm *st; int *flen; int fl; caddr_t sg; retval = p->p_retval; fp = fdp->fd_ofiles[SCARG(uap, fd)]; if (((u_int)SCARG(uap, fd) >= fdp->fd_nfiles) || (fp == NULL)) return EBADF; memset(&sc, 0, sizeof(sc)); #ifdef DEBUG_SVR4 show_msg(">getmsg", SCARG(uap, fd), SCARG(uap, ctl), SCARG(uap, dat), 0); #endif /* DEBUG_SVR4 */ if (((u_int)SCARG(uap, fd) >= fdp->fd_nfiles) || (fp == NULL)) return EBADF; if (SCARG(uap, ctl) != NULL) { if ((error = copyin(SCARG(uap, ctl), &ctl, sizeof(ctl))) != 0) return error; } else { ctl.len = -1; ctl.maxlen = 0; } if (SCARG(uap, dat) != NULL) { if ((error = copyin(SCARG(uap, dat), &dat, sizeof(dat))) != 0) return error; } else { dat.len = -1; dat.maxlen = 0; } /* * Only for sockets for now. */ if ((st = svr4_stream_get(fp)) == NULL) { DPRINTF(("getmsg: bad file type\n")); return EINVAL; } if (ctl.maxlen == -1 || dat.maxlen == -1) { DPRINTF(("getmsg: Cannot handle -1 maxlen (yet)\n")); return ENOSYS; } switch (st->s_family) { case AF_INET: skp = &sain; sasize = sizeof(sain); break; case AF_LOCAL: skp = &saun; sasize = sizeof(saun); break; default: DPRINTF(("getmsg: Unsupported address family %d\n", st->s_family)); return ENOSYS; } sg = stackgap_init(); sup = stackgap_alloc(&sg, sasize); flen = (int *) stackgap_alloc(&sg, sizeof(*flen)); fl = sasize; if ((error = copyout(&fl, flen, sizeof(fl))) != 0) return error; switch (st->s_cmd) { case SVR4_TI_CONNECT_REQUEST: DPRINTF(("getmsg: TI_CONNECT_REQUEST\n")); /* * We do the connect in one step, so the putmsg should * have gotten the error. */ sc.cmd = SVR4_TI_OK_REPLY; sc.len = 0; ctl.len = 8; dat.len = -1; fl = 1; st->s_cmd = sc.cmd; break; case SVR4_TI_OK_REPLY: DPRINTF(("getmsg: TI_OK_REPLY\n")); /* * We are immediately after a connect reply, so we send * a connect verification. */ SCARG(&ga, fdes) = SCARG(uap, fd); SCARG(&ga, asa) = (void *) sup; SCARG(&ga, alen) = flen; if ((error = getpeername(p, &ga)) != 0) { DPRINTF(("getmsg: getpeername failed %d\n", error)); return error; } if ((error = copyin(sup, skp, sasize)) != 0) return error; sc.cmd = SVR4_TI_CONNECT_REPLY; sc.pad[0] = 0x4; sc.offs = 0x18; sc.pad[1] = 0x14; sc.pad[2] = 0x04000402; switch (st->s_family) { case AF_INET: sc.len = sasize; sockaddr_to_netaddr_in(&sc, &sain); break; case AF_LOCAL: sc.len = sasize + 4; sockaddr_to_netaddr_un(&sc, &saun); break; default: return ENOSYS; } ctl.len = 40; dat.len = -1; fl = 0; st->s_cmd = sc.cmd; break; case SVR4_TI__ACCEPT_OK: DPRINTF(("getmsg: TI__ACCEPT_OK\n")); /* * We do the connect in one step, so the putmsg should * have gotten the error. */ sc.cmd = SVR4_TI_OK_REPLY; sc.len = 1; ctl.len = 8; dat.len = -1; fl = 1; st->s_cmd = SVR4_TI__ACCEPT_WAIT; break; case SVR4_TI__ACCEPT_WAIT: DPRINTF(("getmsg: TI__ACCEPT_WAIT\n")); /* * We are after a listen, so we try to accept... */ SCARG(&aa, s) = SCARG(uap, fd); SCARG(&aa, name) = (void *) sup; SCARG(&aa, anamelen) = flen; if ((error = accept(p, &aa)) != 0) { DPRINTF(("getmsg: accept failed %d\n", error)); return error; } st->s_afd = *retval; DPRINTF(("getmsg: Accept fd = %d\n", st->s_afd)); if ((error = copyin(sup, skp, sasize)) != 0) return error; sc.cmd = SVR4_TI_ACCEPT_REPLY; sc.offs = 0x18; sc.pad[0] = 0x0; switch (st->s_family) { case AF_INET: sc.pad[1] = 0x28; sockaddr_to_netaddr_in(&sc, &sain); ctl.len = 40; sc.len = sasize; break; case AF_LOCAL: sc.pad[1] = 0x00010000; sc.pad[2] = 0xf6bcdaa0; /* I don't know what that is */ sc.pad[3] = 0x00010000; ctl.len = 134; sc.len = sasize + 4; break; default: return ENOSYS; } dat.len = -1; fl = 0; st->s_cmd = SVR4_TI__ACCEPT_OK; break; case SVR4_TI_SENDTO_REQUEST: DPRINTF(("getmsg: TI_SENDTO_REQUEST\n")); if (ctl.maxlen > 36 && ctl.len < 36) ctl.len = 36; if ((error = copyin(ctl.buf, &sc, ctl.len)) != 0) return error; switch (st->s_family) { case AF_INET: sockaddr_to_netaddr_in(&sc, &sain); break; case AF_LOCAL: sockaddr_to_netaddr_un(&sc, &saun); break; default: return ENOSYS; } msg.msg_name = (caddr_t) sup; msg.msg_namelen = sasize; msg.msg_iov = &aiov; msg.msg_iovlen = 1; msg.msg_control = 0; aiov.iov_base = dat.buf; aiov.iov_len = dat.maxlen; msg.msg_flags = 0; error = svr4_recvit(p, SCARG(uap, fd), &msg, (caddr_t) flen); if (error) { DPRINTF(("getmsg: recvit failed %d\n", error)); return error; } if ((error = copyin(msg.msg_name, skp, sasize)) != 0) return error; sc.cmd = SVR4_TI_RECVFROM_IND; switch (st->s_family) { case AF_INET: sc.len = sasize; sockaddr_to_netaddr_in(&sc, &sain); break; case AF_LOCAL: sc.len = sasize + 4; sockaddr_to_netaddr_un(&sc, &saun); break; default: return ENOSYS; } dat.len = *retval; fl = 0; st->s_cmd = sc.cmd; break; default: st->s_cmd = sc.cmd; if (st->s_cmd == SVR4_TI_CONNECT_REQUEST) { struct read_args ra; - /* More wierdness: Again, I can't find documentation + /* More weirdness: Again, I can't find documentation * to back this up, but when a process does a generic * "getmsg()" call it seems that the command field is * zero and the length of the data area is zero. I * think processes expect getmsg() to fill in dat.len * after reading at most dat.maxlen octets from the * stream. Since we're using sockets I can let * read() look after it and frob return values * appropriately (or inappropriately :-) * -- newton@atdot.dotat.org XXX */ SCARG(&ra, fd) = SCARG(uap, fd); SCARG(&ra, buf) = dat.buf; SCARG(&ra, nbyte) = dat.maxlen; if ((error = read(p, &ra)) != 0) { return error; } dat.len = *retval; *retval = 0; st->s_cmd = SVR4_TI_SENDTO_REQUEST; break; } DPRINTF(("getmsg: Unknown state %x\n", st->s_cmd)); return EINVAL; } if (SCARG(uap, ctl)) { if (ctl.len != -1) if ((error = copyout(&sc, ctl.buf, ctl.len)) != 0) return error; if ((error = copyout(&ctl, SCARG(uap, ctl), sizeof(ctl))) != 0) return error; } if (SCARG(uap, dat)) { if ((error = copyout(&dat, SCARG(uap, dat), sizeof(dat))) != 0) return error; } if (SCARG(uap, flags)) { /* XXX: Need translation */ if ((error = copyout(&fl, SCARG(uap, flags), sizeof(fl))) != 0) return error; } *retval = 0; #ifdef DEBUG_SVR4 show_msg(") * * Written by Jonathan Chen */ #define CARDBUS_DEBUG #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "power_if.h" #include "card_if.h" #include "pcib_if.h" #if defined CARDBUS_DEBUG #define STATIC #define DPRINTF(a) printf a #define DEVPRINTF(x) device_printf x #else #define STATIC static #define DPRINTF(a) #define DEVPRINTF(x) #endif #if !defined(lint) static const char rcsid[] = "$FreeBSD$"; #endif struct cardbus_quirk { u_int32_t devid; /* Vendor/device of the card */ int type; -#define CARDBUS_QUIRK_MAP_REG 1 /* PCI map register in wierd place */ +#define CARDBUS_QUIRK_MAP_REG 1 /* PCI map register in weird place */ int arg1; int arg2; }; struct cardbus_quirk cardbus_quirks[] = { { 0 } }; static int cardbus_probe(device_t dev); static int cardbus_attach(device_t dev); static void device_setup_regs(device_t cbdev, int b, int s, int f, pcicfgregs *cfg); static int cardbus_attach_card(device_t dev); static int cardbus_detach_card(device_t dev, int flags); static struct cardbus_devinfo *cardbus_read_device(device_t pcib, int b, int s, int f); static void cardbus_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg); static int cardbus_freecfg(struct cardbus_devinfo *dinfo); static void cardbus_print_verbose(struct cardbus_devinfo *dinfo); static int cardbus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count); static int cardbus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp); static void cardbus_delete_resource(device_t dev, device_t child, int type, int rid); static int cardbus_set_resource_method(device_t dev, device_t child, int type, int rid, u_long start, u_long count); static int cardbus_get_resource_method(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp); static int cardbus_add_map(device_t bdev, device_t dev, pcicfgregs *cfg, int reg); static void cardbus_add_resources(device_t dev, pcicfgregs* cfg); static void cardbus_release_all_resources(device_t dev, struct resource_list *rl); static struct resource* cardbus_alloc_resource(device_t self, device_t child, int type, int* rid,u_long start, u_long end, u_long count, u_int flags); static int cardbus_release_resource(device_t dev, device_t child, int type, int rid, struct resource *r); static int cardbus_print_resources(struct resource_list *rl, const char *name, int type, const char *format); static int cardbus_print_child(device_t dev, device_t child); static void cardbus_probe_nomatch(device_t dev, device_t child); static int cardbus_read_ivar(device_t dev, device_t child, int which, u_long *result); static int cardbus_write_ivar(device_t dev, device_t child, int which, uintptr_t value); static u_int32_t cardbus_read_config_method(device_t dev, device_t child, int reg, int width); static void cardbus_write_config_method(device_t dev, device_t child, int reg, u_int32_t val, int width); /************************************************************************/ /* Probe/Attach */ /************************************************************************/ static int cardbus_probe(device_t dev) { device_set_desc(dev, "Cardbus bus (newcard)"); return 0; } static int cardbus_attach(device_t dev) { return 0; } static int cardbus_detach(device_t dev) { cardbus_detach_card(dev, DETACH_FORCE); return 0; } /************************************************************************/ /* Attach/Detach card */ /************************************************************************/ static void device_setup_regs(device_t bdev, int b, int s, int f, pcicfgregs *cfg) { PCIB_WRITE_CONFIG(bdev, b, s, f, PCIR_INTLINE, pci_get_irq(device_get_parent(bdev)), 1); cfg->intline = PCIB_READ_CONFIG(bdev, b, s, f, PCIR_INTLINE, 1); PCIB_WRITE_CONFIG(bdev, b, s, f, PCIR_CACHELNSZ, 0x08, 1); cfg->cachelnsz = PCIB_READ_CONFIG(bdev, b, s, f, PCIR_CACHELNSZ, 1); PCIB_WRITE_CONFIG(bdev, b, s, f, PCIR_LATTIMER, 0xa8, 1); cfg->lattimer = PCIB_READ_CONFIG(bdev, b, s, f, PCIR_LATTIMER, 1); PCIB_WRITE_CONFIG(bdev, b, s, f, PCIR_MINGNT, 0x14, 1); cfg->mingnt = PCIB_READ_CONFIG(bdev, b, s, f, PCIR_MINGNT, 1); PCIB_WRITE_CONFIG(bdev, b, s, f, PCIR_MAXLAT, 0x14, 1); cfg->maxlat = PCIB_READ_CONFIG(bdev, b, s, f, PCIR_MAXLAT, 1); } static int cardbus_attach_card(device_t dev) { device_t bdev = device_get_parent(dev); int cardattached = 0; static int curr_bus_number = 2; /* XXX EVILE BAD (see below) */ int bus, slot, func; cardbus_detach_card(dev, DETACH_NOWARN); /* detach existing cards */ POWER_ENABLE_SOCKET(bdev, dev); bus = pcib_get_bus(dev); if (bus == 0) { /* * XXX EVILE BAD XXX * Not all BIOSes initialize the secondary bus number properly, * so if the default is bad, we just put one in and hope it * works. */ bus = curr_bus_number; pci_write_config (bdev, PCIR_SECBUS_2, curr_bus_number, 1); pci_write_config (bdev, PCIR_SUBBUS_2, curr_bus_number+2, 1); curr_bus_number += 3; } for (slot = 0; slot <= CARDBUS_SLOTMAX; slot++) { int cardbusfunchigh = 0; for (func = 0; func <= cardbusfunchigh; func++) { struct cardbus_devinfo *dinfo = cardbus_read_device(bdev, bus, slot, func); if (dinfo == NULL) continue; if (dinfo->cfg.mfdev) cardbusfunchigh = CARDBUS_FUNCMAX; device_setup_regs(bdev, bus, slot, func, &dinfo->cfg); cardbus_print_verbose(dinfo); dinfo->cfg.dev = device_add_child(dev, NULL, -1); if (!dinfo->cfg.dev) { DEVPRINTF((dev, "Cannot add child!\n")); cardbus_freecfg(dinfo); continue; } resource_list_init(&dinfo->resources); device_set_ivars(dinfo->cfg.dev, dinfo); cardbus_add_resources(dinfo->cfg.dev, &dinfo->cfg); cardbus_do_cis(dev, dinfo->cfg.dev); if (device_probe_and_attach(dinfo->cfg.dev) != 0) { cardbus_release_all_resources(dinfo->cfg.dev, &dinfo->resources); } else cardattached++; } } if (cardattached > 0) return 0; POWER_DISABLE_SOCKET(bdev, dev); return ENOENT; } static int cardbus_detach_card(device_t dev, int flags) { int numdevs; device_t *devlist; int tmp; int err=0; device_get_children(dev, &devlist, &numdevs); if (numdevs == 0) { if (!(flags & DETACH_NOWARN)) { DEVPRINTF((dev, "Detaching card: no cards to detach!\n")); POWER_DISABLE_SOCKET(device_get_parent(dev), dev); } return ENOENT; } for (tmp = 0; tmp < numdevs; tmp++) { struct cardbus_devinfo *dinfo = device_get_ivars(devlist[tmp]); int status = device_get_state(devlist[tmp]); if (status == DS_ATTACHED || status == DS_BUSY) { if (device_detach(dinfo->cfg.dev) == 0 || flags & DETACH_FORCE){ cardbus_release_all_resources(dinfo->cfg.dev, &dinfo->resources); device_delete_child(dev, devlist[tmp]); } else { err++; } cardbus_freecfg(dinfo); } else { device_delete_child(dev, devlist[tmp]); } } if (err == 0) POWER_DISABLE_SOCKET(device_get_parent(dev), dev); return err; } static void cardbus_driver_added(device_t dev, driver_t *driver) { int numdevs; device_t *devlist; device_t bdev = device_get_parent(dev); int tmp, cardattached; device_get_children(dev, &devlist, &numdevs); cardattached = 0; for (tmp = 0; tmp < numdevs; tmp++) { if (device_get_state(devlist[tmp]) != DS_NOTPRESENT) cardattached++; } if (cardattached == 0) POWER_ENABLE_SOCKET(bdev, dev); DEVICE_IDENTIFY(driver, dev); for (tmp = 0; tmp < numdevs; tmp++) { if (device_get_state(devlist[tmp]) == DS_NOTPRESENT){ struct cardbus_devinfo *dinfo; dinfo = device_get_ivars(devlist[tmp]); resource_list_init(&dinfo->resources); cardbus_add_resources(dinfo->cfg.dev, &dinfo->cfg); cardbus_do_cis(dev, dinfo->cfg.dev); if (device_probe_and_attach(dinfo->cfg.dev) != 0) { cardbus_release_all_resources(dinfo->cfg.dev, &dinfo->resources); } else cardattached++; } } if (cardattached == 0) POWER_DISABLE_SOCKET(bdev, dev); } /************************************************************************/ /* PCI-Like config reading (copied from pci.c */ /************************************************************************/ /* read configuration header into pcicfgrect structure */ static struct cardbus_devinfo * cardbus_read_device(device_t pcib, int b, int s, int f) { #define REG(n, w) PCIB_READ_CONFIG(pcib, b, s, f, n, w) pcicfgregs *cfg = NULL; struct cardbus_devinfo *devlist_entry = NULL; if (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_DEVVENDOR, 4) != -1) { devlist_entry = malloc(sizeof(struct cardbus_devinfo), M_DEVBUF, M_WAITOK | M_ZERO); if (devlist_entry == NULL) return (NULL); cfg = &devlist_entry->cfg; cfg->bus = b; cfg->slot = s; cfg->func = f; cfg->vendor = REG(PCIR_VENDOR, 2); cfg->device = REG(PCIR_DEVICE, 2); cfg->cmdreg = REG(PCIR_COMMAND, 2); cfg->statreg = REG(PCIR_STATUS, 2); cfg->baseclass = REG(PCIR_CLASS, 1); cfg->subclass = REG(PCIR_SUBCLASS, 1); cfg->progif = REG(PCIR_PROGIF, 1); cfg->revid = REG(PCIR_REVID, 1); cfg->hdrtype = REG(PCIR_HEADERTYPE, 1); cfg->cachelnsz = REG(PCIR_CACHELNSZ, 1); cfg->lattimer = REG(PCIR_LATTIMER, 1); cfg->intpin = REG(PCIR_INTPIN, 1); cfg->intline = REG(PCIR_INTLINE, 1); #ifdef __alpha__ alpha_platform_assign_pciintr(cfg); #endif #ifdef APIC_IO if (cfg->intpin != 0) { int airq; airq = pci_apic_irq(cfg->bus, cfg->slot, cfg->intpin); if (airq >= 0) { /* PCI specific entry found in MP table */ if (airq != cfg->intline) { undirect_pci_irq(cfg->intline); cfg->intline = airq; } } else { /* * PCI interrupts might be redirected to the * ISA bus according to some MP tables. Use the * same methods as used by the ISA devices * devices to find the proper IOAPIC int pin. */ airq = isa_apic_irq(cfg->intline); if ((airq >= 0) && (airq != cfg->intline)) { /* XXX: undirect_pci_irq() ? */ undirect_isa_irq(cfg->intline); cfg->intline = airq; } } } #endif /* APIC_IO */ cfg->mingnt = REG(PCIR_MINGNT, 1); cfg->maxlat = REG(PCIR_MAXLAT, 1); cfg->mfdev = (cfg->hdrtype & PCIM_MFDEV) != 0; cfg->hdrtype &= ~PCIM_MFDEV; cardbus_hdrtypedata(pcib, b, s, f, cfg); devlist_entry->conf.pc_sel.pc_bus = cfg->bus; devlist_entry->conf.pc_sel.pc_dev = cfg->slot; devlist_entry->conf.pc_sel.pc_func = cfg->func; devlist_entry->conf.pc_hdr = cfg->hdrtype; devlist_entry->conf.pc_subvendor = cfg->subvendor; devlist_entry->conf.pc_subdevice = cfg->subdevice; devlist_entry->conf.pc_vendor = cfg->vendor; devlist_entry->conf.pc_device = cfg->device; devlist_entry->conf.pc_class = cfg->baseclass; devlist_entry->conf.pc_subclass = cfg->subclass; devlist_entry->conf.pc_progif = cfg->progif; devlist_entry->conf.pc_revid = cfg->revid; } return (devlist_entry); #undef REG } /* extract header type specific config data */ static void cardbus_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg) { #define REG(n, w) PCIB_READ_CONFIG(pcib, b, s, f, n, w) switch (cfg->hdrtype) { case 0: cfg->subvendor = REG(PCIR_SUBVEND_0, 2); cfg->subdevice = REG(PCIR_SUBDEV_0, 2); cfg->nummaps = PCI_MAXMAPS_0; break; case 1: cfg->subvendor = REG(PCIR_SUBVEND_1, 2); cfg->subdevice = REG(PCIR_SUBDEV_1, 2); cfg->nummaps = PCI_MAXMAPS_1; break; case 2: cfg->subvendor = REG(PCIR_SUBVEND_2, 2); cfg->subdevice = REG(PCIR_SUBDEV_2, 2); cfg->nummaps = PCI_MAXMAPS_2; break; } #undef REG } /* free pcicfgregs structure and all depending data structures */ static int cardbus_freecfg(struct cardbus_devinfo *dinfo) { free(dinfo, M_DEVBUF); return (0); } static void cardbus_print_verbose(struct cardbus_devinfo *dinfo) { if (bootverbose) { pcicfgregs *cfg = &dinfo->cfg; printf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n", cfg->vendor, cfg->device, cfg->revid); printf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n", cfg->baseclass, cfg->subclass, cfg->progif, cfg->hdrtype, cfg->mfdev); #ifdef CARDBUS_DEBUG printf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n", cfg->cmdreg, cfg->statreg, cfg->cachelnsz); printf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n", cfg->lattimer, cfg->lattimer * 30, cfg->mingnt, cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250); #endif /* CARDBUS_DEBUG */ if (cfg->intpin > 0) printf("\tintpin=%c, irq=%d\n", cfg->intpin +'a' -1, cfg->intline); } } /************************************************************************/ /* Resources */ /************************************************************************/ static int cardbus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count) { struct cardbus_devinfo *dinfo = device_get_ivars(child); struct resource_list *rl = &dinfo->resources; resource_list_add(rl, type, rid, start, start + count - 1, count); if (device_get_parent(child) == dev) pci_write_config(child, rid, start, 4); return 0; } static int cardbus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) { struct cardbus_devinfo *dinfo = device_get_ivars(child); struct resource_list *rl = &dinfo->resources; struct resource_list_entry *rle; rle = resource_list_find(rl, type, rid); if (!rle) return ENOENT; if (startp) *startp = rle->start; if (countp) *countp = rle->count; return 0; } static void cardbus_delete_resource(device_t dev, device_t child, int type, int rid) { struct cardbus_devinfo *dinfo = device_get_ivars(child); struct resource_list *rl = &dinfo->resources; struct resource_list_entry *rle; rle = resource_list_find(rl, type, rid); if (rle) { if (rle->res) bus_generic_release_resource(dev, child, type, rid, rle->res); resource_list_delete(rl, type, rid); } if (device_get_parent(child) == dev) pci_write_config(child, rid, 0, 4); } static int cardbus_set_resource_method(device_t dev, device_t child, int type, int rid, u_long start, u_long count) { int ret; ret = cardbus_set_resource(dev, child, type, rid, start, count); if (ret != 0) return ret; return BUS_SET_RESOURCE(device_get_parent(dev), child, type, rid, start, count); } static int cardbus_get_resource_method(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) { int ret; ret = cardbus_get_resource(dev, child, type, rid, startp, countp); if (ret != 0) return ret; return BUS_GET_RESOURCE(device_get_parent(dev), child, type, rid, startp, countp); } static void cardbus_delete_resource_method(device_t dev, device_t child, int type, int rid) { cardbus_delete_resource(dev, child, type, rid); BUS_DELETE_RESOURCE(device_get_parent(dev), child, type, rid); } static int cardbus_add_map(device_t cbdev, device_t dev, pcicfgregs *cfg, int reg) { struct cardbus_devinfo *dinfo = device_get_ivars(dev); struct resource_list *rl = &dinfo->resources; struct resource_list_entry *rle; struct resource *res; device_t bdev = device_get_parent(cbdev); u_int32_t size; u_int32_t testval; int type; if (reg == CARDBUS_ROM_REG) testval = CARDBUS_ROM_ADDRMASK; else testval = ~0; PCIB_WRITE_CONFIG(bdev, cfg->bus, cfg->slot, cfg->func, reg, testval, 4); testval = PCIB_READ_CONFIG(bdev, cfg->bus, cfg->slot, cfg->func, reg, 4); if (testval == ~0 || testval == 0) return 0; if ((testval&1) == 0) type = SYS_RES_MEMORY; else type = SYS_RES_IOPORT; size = CARDBUS_MAPREG_MEM_SIZE(testval); res = bus_generic_alloc_resource(cbdev, dev, type, ®, 0, ~0, size, rman_make_alignment_flags(size)); if (res) { u_int32_t start = rman_get_start(res); u_int32_t end = rman_get_end(res); cardbus_set_resource(cbdev, dev, type, reg, start,end-start+1); rle = resource_list_find(rl, type, reg); rle->res = res; } else { device_printf(dev, "Unable to add map %02x\n", reg); type = 0; } return type; } static void cardbus_add_resources(device_t dev, pcicfgregs* cfg) { device_t cbdev = device_get_parent(dev); device_t bdev = device_get_parent(cbdev); struct cardbus_devinfo *dinfo = device_get_ivars(dev); struct resource_list *rl = &dinfo->resources; struct cardbus_quirk *q; struct resource_list_entry *rle; struct resource *res; int rid; u_int command; int type; int types; int i; types = 0; for (i = 0; i < cfg->nummaps; i++) { type = cardbus_add_map(cbdev, dev, cfg, PCIR_MAPS + i*4); types |= 0x1 << type; } type = cardbus_add_map(cbdev, dev, cfg, CARDBUS_ROM_REG); types |= 0x1 << type; for (q = &cardbus_quirks[0]; q->devid; q++) { if (q->devid == ((cfg->device << 16) | cfg->vendor) && q->type == CARDBUS_QUIRK_MAP_REG) { type = cardbus_add_map(cbdev, dev, cfg, q->arg1); types |= 0x1 << type; } } command = PCIB_READ_CONFIG(bdev, cfg->bus, cfg->slot, cfg->func, PCIR_COMMAND, 2); if ((types & (0x1 << SYS_RES_MEMORY)) != 0) command |= PCIM_CMD_MEMEN; if ((types & (0x1 << SYS_RES_IOPORT)) != 0) command |= PCIM_CMD_PORTEN; command |= PCIM_CMD_BUSMASTEREN; PCIB_WRITE_CONFIG(bdev, cfg->bus, cfg->slot, cfg->func, PCIR_COMMAND, command, 2); rid = 0; res = bus_generic_alloc_resource(cbdev, dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE); if (res == NULL) panic("Cannot allocate IRQ for card\n"); resource_list_add(rl, SYS_RES_IRQ, rid, rman_get_start(res), rman_get_start(res), 1); rle = resource_list_find(rl, SYS_RES_IRQ, rid); rle->res = res; } static void cardbus_release_all_resources(device_t dev, struct resource_list *rl) { struct resource_list_entry *rle; SLIST_FOREACH(rle, rl, link) { if (rle->res) { bus_generic_release_resource(device_get_parent(dev), dev, rle->type, rle->rid, rle->res); } } } static struct resource* cardbus_alloc_resource(device_t self, device_t child, int type, int* rid, u_long start, u_long end, u_long count, u_int flags) { struct cardbus_devinfo *dinfo = device_get_ivars(child); struct resource_list *rl = &dinfo->resources; struct resource_list_entry *rle = NULL; struct resource *res; if (device_get_parent(child) == self || child == self) rle = resource_list_find(rl, type, *rid); if (rle) { if (flags & RF_ACTIVE) { if (bus_activate_resource(child, rle->type, *rid, rle->res)) { return NULL; } if (*rid == CARDBUS_ROM_REG) { uint32_t rom_reg; rom_reg = pci_read_config(child, *rid, 4); rom_reg |= CARDBUS_ROM_ENABLE; pci_write_config(child, *rid, rom_reg, 4); } } return rle->res; /* XXX: check if range within start/end */ } else { res = bus_generic_alloc_resource(self, child, type, rid, start, end, count, flags); if (res) { start = rman_get_start(res); end = rman_get_end(res); cardbus_set_resource(self, child, type, *rid, start, end-start+1); rle = resource_list_find(rl, type, *rid); rle->res = res; return res; } else { device_printf(self, "Resource Allocation Failed!\n"); return NULL; } } } static int cardbus_release_resource(device_t dev, device_t child, int type, int rid, struct resource *r) { /* * According to the PCI 2.2 spec, devices may share an address * decoder between memory mapped ROM access and memory * mapped register access. To be safe, disable ROM access * whenever it is released. */ if (rid == CARDBUS_ROM_REG) { uint32_t rom_reg; rom_reg = pci_read_config(child, rid, 4); rom_reg &= ~CARDBUS_ROM_ENABLE; pci_write_config(child, rid, rom_reg, 4); } return bus_deactivate_resource(child, type, rid, r); } /************************************************************************/ /* Other Bus Methods */ /************************************************************************/ static int cardbus_print_resources(struct resource_list *rl, const char *name, int type, const char *format) { struct resource_list_entry *rle; int printed, retval; printed = 0; retval = 0; /* Yes, this is kinda cheating */ SLIST_FOREACH(rle, rl, link) { if (rle->type == type) { if (printed == 0) retval += printf(" %s ", name); else if (printed > 0) retval += printf(","); printed++; retval += printf(format, rle->start); if (rle->count > 1) { retval += printf("-"); retval += printf(format, rle->start + rle->count - 1); } } } return retval; } static int cardbus_print_child(device_t dev, device_t child) { struct cardbus_devinfo *dinfo; struct resource_list *rl; pcicfgregs *cfg; int retval = 0; dinfo = device_get_ivars(child); cfg = &dinfo->cfg; rl = &dinfo->resources; retval += bus_print_child_header(dev, child); retval += cardbus_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx"); retval += cardbus_print_resources(rl, "mem", SYS_RES_MEMORY, "%#lx"); retval += cardbus_print_resources(rl, "irq", SYS_RES_IRQ, "%ld"); if (device_get_flags(dev)) retval += printf(" flags %#x", device_get_flags(dev)); retval += printf(" at device %d.%d", pci_get_slot(child), pci_get_function(child)); retval += bus_print_child_footer(dev, child); return (retval); } static void cardbus_probe_nomatch(device_t dev, device_t child) { struct cardbus_devinfo *dinfo; pcicfgregs *cfg; dinfo = device_get_ivars(child); cfg = &dinfo->cfg; device_printf(dev, ""); printf(" (vendor=0x%04x, dev=0x%04x)", cfg->vendor, cfg->device); printf(" at %d.%d", pci_get_slot(child), pci_get_function(child)); if (cfg->intpin > 0 && cfg->intline != 255) { printf(" irq %d", cfg->intline); } printf("\n"); return; } static int cardbus_read_ivar(device_t dev, device_t child, int which, u_long *result) { struct cardbus_devinfo *dinfo; pcicfgregs *cfg; dinfo = device_get_ivars(child); cfg = &dinfo->cfg; switch (which) { case PCI_IVAR_SUBVENDOR: *result = cfg->subvendor; break; case PCI_IVAR_SUBDEVICE: *result = cfg->subdevice; break; case PCI_IVAR_VENDOR: *result = cfg->vendor; break; case PCI_IVAR_DEVICE: *result = cfg->device; break; case PCI_IVAR_DEVID: *result = (cfg->device << 16) | cfg->vendor; break; case PCI_IVAR_CLASS: *result = cfg->baseclass; break; case PCI_IVAR_SUBCLASS: *result = cfg->subclass; break; case PCI_IVAR_PROGIF: *result = cfg->progif; break; case PCI_IVAR_REVID: *result = cfg->revid; break; case PCI_IVAR_INTPIN: *result = cfg->intpin; break; case PCI_IVAR_IRQ: *result = cfg->intline; break; case PCI_IVAR_BUS: *result = cfg->bus; break; case PCI_IVAR_SLOT: *result = cfg->slot; break; case PCI_IVAR_FUNCTION: *result = cfg->func; break; default: return ENOENT; } return 0; } static int cardbus_write_ivar(device_t dev, device_t child, int which, uintptr_t value) { struct cardbus_devinfo *dinfo; pcicfgregs *cfg; dinfo = device_get_ivars(child); cfg = &dinfo->cfg; switch (which) { case PCI_IVAR_SUBVENDOR: case PCI_IVAR_SUBDEVICE: case PCI_IVAR_VENDOR: case PCI_IVAR_DEVICE: case PCI_IVAR_DEVID: case PCI_IVAR_CLASS: case PCI_IVAR_SUBCLASS: case PCI_IVAR_PROGIF: case PCI_IVAR_REVID: case PCI_IVAR_INTPIN: case PCI_IVAR_IRQ: case PCI_IVAR_BUS: case PCI_IVAR_SLOT: case PCI_IVAR_FUNCTION: return EINVAL; /* disallow for now */ default: return ENOENT; } return 0; } /************************************************************************/ /* Compatibility with PCI bus (XXX: Do we need this?) */ /************************************************************************/ static u_int32_t cardbus_read_config_method(device_t dev, device_t child, int reg, int width) { struct cardbus_devinfo *dinfo = device_get_ivars(child); pcicfgregs *cfg = &dinfo->cfg; return PCIB_READ_CONFIG(device_get_parent(dev), cfg->bus, cfg->slot, cfg->func, reg, width); } static void cardbus_write_config_method(device_t dev, device_t child, int reg, u_int32_t val, int width) { struct cardbus_devinfo *dinfo = device_get_ivars(child); pcicfgregs *cfg = &dinfo->cfg; PCIB_WRITE_CONFIG(device_get_parent(dev), cfg->bus, cfg->slot, cfg->func, reg, val, width); } static device_method_t cardbus_methods[] = { /* Device interface */ DEVMETHOD(device_probe, cardbus_probe), DEVMETHOD(device_attach, cardbus_attach), DEVMETHOD(device_detach, cardbus_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), DEVMETHOD(device_suspend, bus_generic_suspend), DEVMETHOD(device_resume, bus_generic_resume), /* Bus interface */ DEVMETHOD(bus_print_child, cardbus_print_child), DEVMETHOD(bus_probe_nomatch, cardbus_probe_nomatch), DEVMETHOD(bus_read_ivar, cardbus_read_ivar), DEVMETHOD(bus_write_ivar, cardbus_write_ivar), DEVMETHOD(bus_driver_added, cardbus_driver_added), DEVMETHOD(bus_alloc_resource, cardbus_alloc_resource), DEVMETHOD(bus_release_resource, cardbus_release_resource), DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), DEVMETHOD(bus_driver_added, bus_generic_driver_added), DEVMETHOD(bus_set_resource, cardbus_set_resource_method), DEVMETHOD(bus_get_resource, cardbus_get_resource_method), DEVMETHOD(bus_delete_resource, cardbus_delete_resource_method), /* Card Interface */ DEVMETHOD(card_attach_card, cardbus_attach_card), DEVMETHOD(card_detach_card, cardbus_detach_card), DEVMETHOD(card_cis_read, cardbus_cis_read), DEVMETHOD(card_cis_free, cardbus_cis_free), /* Cardbus/PCI interface */ DEVMETHOD(pci_read_config, cardbus_read_config_method), DEVMETHOD(pci_write_config, cardbus_write_config_method), {0,0} }; static driver_t cardbus_driver = { "cardbus", cardbus_methods, 0 /* no softc */ }; static devclass_t cardbus_devclass; DRIVER_MODULE(cardbus, pccbb, cardbus_driver, cardbus_devclass, 0, 0); /* MODULE_DEPEND(cardbus, pccbb, 1, 1, 1); */ diff --git a/sys/dev/ed/if_ed.c b/sys/dev/ed/if_ed.c index afa9cd4d020f..a8979aaf0c80 100644 --- a/sys/dev/ed/if_ed.c +++ b/sys/dev/ed/if_ed.c @@ -1,3296 +1,3296 @@ /* * Copyright (c) 1995, David Greenman * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ /* * Device driver for National Semiconductor DS8390/WD83C690 based ethernet * adapters. By David Greenman, 29-April-1993 * * Currently supports the Western Digital/SMC 8003 and 8013 series, * the SMC Elite Ultra (8216), the 3Com 3c503, the NE1000 and NE2000, * and a variety of similar clones. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "opt_bdg.h" #ifdef BRIDGE #include #endif #include #include #include devclass_t ed_devclass; static void ed_init __P((void *)); static int ed_ioctl __P((struct ifnet *, u_long, caddr_t)); static void ed_start __P((struct ifnet *)); static void ed_reset __P((struct ifnet *)); static void ed_watchdog __P((struct ifnet *)); static void ds_getmcaf __P((struct ed_softc *, u_int32_t *)); static void ed_get_packet __P((struct ed_softc *, char *, /* u_short */ int)); static __inline void ed_rint __P((struct ed_softc *)); static __inline void ed_xmit __P((struct ed_softc *)); static __inline char * ed_ring_copy __P((struct ed_softc *, char *, char *, /* u_short */ int)); static void ed_hpp_set_physical_link __P((struct ed_softc *)); static void ed_hpp_readmem __P((struct ed_softc *, int, unsigned char *, /* u_short */ int)); static void ed_hpp_writemem __P((struct ed_softc *, unsigned char *, /* u_short */ int, /* u_short */ int)); static u_short ed_hpp_write_mbufs __P((struct ed_softc *, struct mbuf *, int)); static u_short ed_pio_write_mbufs __P((struct ed_softc *, struct mbuf *, int)); static void ed_setrcr __P((struct ed_softc *)); static u_int32_t ds_crc __P((u_char *ep)); /* * Interrupt conversion table for WD/SMC ASIC/83C584 */ static unsigned short ed_intr_val[] = { 9, 3, 5, 7, 10, 11, 15, 4 }; /* * Interrupt conversion table for 83C790 */ static unsigned short ed_790_intr_val[] = { 0, 9, 3, 5, 7, 10, 11, 15 }; /* * Interrupt conversion table for the HP PC LAN+ */ static unsigned short ed_hpp_intr_val[] = { 0, /* 0 */ 0, /* 1 */ 0, /* 2 */ 3, /* 3 */ 4, /* 4 */ 5, /* 5 */ 6, /* 6 */ 7, /* 7 */ 0, /* 8 */ 9, /* 9 */ 10, /* 10 */ 11, /* 11 */ 12, /* 12 */ 0, /* 13 */ 0, /* 14 */ 15 /* 15 */ }; /* * Generic probe routine for testing for the existance of a DS8390. * Must be called after the NIC has just been reset. This routine * works by looking at certain register values that are guaranteed * to be initialized a certain way after power-up or reset. Seems * not to currently work on the 83C690. * * Specifically: * * Register reset bits set bits * Command Register (CR) TXP, STA RD2, STP * Interrupt Status (ISR) RST * Interrupt Mask (IMR) All bits * Data Control (DCR) LAS * Transmit Config. (TCR) LB1, LB0 * * We only look at the CR and ISR registers, however, because looking at * the others would require changing register pages (which would be * intrusive if this isn't an 8390). * * Return 1 if 8390 was found, 0 if not. */ int ed_probe_generic8390(sc) struct ed_softc *sc; { if ((ed_nic_inb(sc, ED_P0_CR) & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) != (ED_CR_RD2 | ED_CR_STP)) return (0); if ((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST) != ED_ISR_RST) return (0); return (1); } /* * Probe and vendor-specific initialization routine for SMC/WD80x3 boards */ int ed_probe_WD80x3_generic(dev, flags, intr_vals) device_t dev; int flags; unsigned short *intr_vals[]; { struct ed_softc *sc = device_get_softc(dev); int error; int i; u_int memsize, maddr; u_char iptr, isa16bit, sum, totalsum; u_long conf_maddr, conf_msize, irq, junk; sc->chip_type = ED_CHIP_TYPE_DP8390; if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_TOSH_ETHER) { totalsum = ED_WD_ROM_CHECKSUM_TOTAL_TOSH_ETHER; ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_POW); DELAY(10000); } else totalsum = ED_WD_ROM_CHECKSUM_TOTAL; /* * Attempt to do a checksum over the station address PROM. If it * fails, it's probably not a SMC/WD board. There is a problem with * this, though: some clone WD boards don't pass the checksum test. * Danpex boards for one. */ for (sum = 0, i = 0; i < 8; ++i) sum += ed_asic_inb(sc, ED_WD_PROM + i); if (sum != totalsum) { /* * Checksum is invalid. This often happens with cheap WD8003E * clones. In this case, the checksum byte (the eighth byte) * seems to always be zero. */ if (ed_asic_inb(sc, ED_WD_CARD_ID) != ED_TYPE_WD8003E || ed_asic_inb(sc, ED_WD_PROM + 7) != 0) return (ENXIO); } /* reset card to force it into a known state. */ if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_TOSH_ETHER) ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_RST | ED_WD_MSR_POW); else ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_RST); DELAY(100); ed_asic_outb(sc, ED_WD_MSR, ed_asic_inb(sc, ED_WD_MSR) & ~ED_WD_MSR_RST); /* wait in the case this card is reading its EEROM */ DELAY(5000); sc->vendor = ED_VENDOR_WD_SMC; sc->type = ed_asic_inb(sc, ED_WD_CARD_ID); /* * Set initial values for width/size. */ memsize = 8192; isa16bit = 0; switch (sc->type) { case ED_TYPE_WD8003S: sc->type_str = "WD8003S"; break; case ED_TYPE_WD8003E: sc->type_str = "WD8003E"; break; case ED_TYPE_WD8003EB: sc->type_str = "WD8003EB"; break; case ED_TYPE_WD8003W: sc->type_str = "WD8003W"; break; case ED_TYPE_WD8013EBT: sc->type_str = "WD8013EBT"; memsize = 16384; isa16bit = 1; break; case ED_TYPE_WD8013W: sc->type_str = "WD8013W"; memsize = 16384; isa16bit = 1; break; case ED_TYPE_WD8013EP: /* also WD8003EP */ if (ed_asic_inb(sc, ED_WD_ICR) & ED_WD_ICR_16BIT) { isa16bit = 1; memsize = 16384; sc->type_str = "WD8013EP"; } else { sc->type_str = "WD8003EP"; } break; case ED_TYPE_WD8013WC: sc->type_str = "WD8013WC"; memsize = 16384; isa16bit = 1; break; case ED_TYPE_WD8013EBP: sc->type_str = "WD8013EBP"; memsize = 16384; isa16bit = 1; break; case ED_TYPE_WD8013EPC: sc->type_str = "WD8013EPC"; memsize = 16384; isa16bit = 1; break; case ED_TYPE_SMC8216C: /* 8216 has 16K shared mem -- 8416 has 8K */ case ED_TYPE_SMC8216T: if (sc->type == ED_TYPE_SMC8216C) { sc->type_str = "SMC8216/SMC8216C"; } else { sc->type_str = "SMC8216T"; } ed_asic_outb(sc, ED_WD790_HWR, ed_asic_inb(sc, ED_WD790_HWR) | ED_WD790_HWR_SWH); switch (ed_asic_inb(sc, ED_WD790_RAR) & ED_WD790_RAR_SZ64) { case ED_WD790_RAR_SZ64: memsize = 65536; break; case ED_WD790_RAR_SZ32: memsize = 32768; break; case ED_WD790_RAR_SZ16: memsize = 16384; break; case ED_WD790_RAR_SZ8: /* 8216 has 16K shared mem -- 8416 has 8K */ if (sc->type == ED_TYPE_SMC8216C) { sc->type_str = "SMC8416C/SMC8416BT"; } else { sc->type_str = "SMC8416T"; } memsize = 8192; break; } ed_asic_outb(sc, ED_WD790_HWR, ed_asic_inb(sc, ED_WD790_HWR) & ~ED_WD790_HWR_SWH); isa16bit = 1; sc->chip_type = ED_CHIP_TYPE_WD790; break; case ED_TYPE_TOSHIBA1: sc->type_str = "Toshiba1"; memsize = 32768; isa16bit = 1; break; case ED_TYPE_TOSHIBA4: sc->type_str = "Toshiba4"; memsize = 32768; isa16bit = 1; break; default: sc->type_str = ""; break; } /* * Make some adjustments to initial values depending on what is found * in the ICR. */ if (isa16bit && (sc->type != ED_TYPE_WD8013EBT) && (sc->type != ED_TYPE_TOSHIBA1) && (sc->type != ED_TYPE_TOSHIBA4) && ((ed_asic_inb(sc, ED_WD_ICR) & ED_WD_ICR_16BIT) == 0)) { isa16bit = 0; memsize = 8192; } error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &conf_maddr, &conf_msize); if (error) return (error); #if ED_DEBUG printf("type = %x type_str=%s isa16bit=%d memsize=%d id_msize=%d\n", sc->type, sc->type_str, isa16bit, memsize, conf_msize); for (i = 0; i < 8; i++) printf("%x -> %x\n", i, ed_asic_inb(sc, i)); #endif /* * Allow the user to override the autoconfiguration */ if (conf_msize > 1) memsize = conf_msize; maddr = conf_maddr; if (maddr < 0xa0000 || maddr + memsize > 0x1000000) { device_printf(dev, "Invalid ISA memory address range configured: 0x%x - 0x%x\n", maddr, maddr + memsize); return (ENXIO); } /* * (note that if the user specifies both of the following flags that * '8bit' mode intentionally has precedence) */ if (flags & ED_FLAGS_FORCE_16BIT_MODE) isa16bit = 1; if (flags & ED_FLAGS_FORCE_8BIT_MODE) isa16bit = 0; /* * If possible, get the assigned interrupt number from the card and * use it. */ if ((sc->type & ED_WD_SOFTCONFIG) && (sc->chip_type != ED_CHIP_TYPE_WD790)) { /* * Assemble together the encoded interrupt number. */ iptr = (ed_asic_inb(sc, ED_WD_ICR) & ED_WD_ICR_IR2) | ((ed_asic_inb(sc, ED_WD_IRR) & (ED_WD_IRR_IR0 | ED_WD_IRR_IR1)) >> 5); /* * If no interrupt specified (or "?"), use what the board tells us. */ error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk); if (error && intr_vals[0] != NULL) { error = bus_set_resource(dev, SYS_RES_IRQ, 0, intr_vals[0][iptr], 1); } if (error) return (error); /* * Enable the interrupt. */ ed_asic_outb(sc, ED_WD_IRR, ed_asic_inb(sc, ED_WD_IRR) | ED_WD_IRR_IEN); } if (sc->chip_type == ED_CHIP_TYPE_WD790) { ed_asic_outb(sc, ED_WD790_HWR, ed_asic_inb(sc, ED_WD790_HWR) | ED_WD790_HWR_SWH); iptr = (((ed_asic_inb(sc, ED_WD790_GCR) & ED_WD790_GCR_IR2) >> 4) | (ed_asic_inb(sc, ED_WD790_GCR) & (ED_WD790_GCR_IR1 | ED_WD790_GCR_IR0)) >> 2); ed_asic_outb(sc, ED_WD790_HWR, ed_asic_inb(sc, ED_WD790_HWR) & ~ED_WD790_HWR_SWH); /* * If no interrupt specified (or "?"), use what the board tells us. */ error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk); if (error && intr_vals[1] != NULL) { error = bus_set_resource(dev, SYS_RES_IRQ, 0, intr_vals[1][iptr], 1); } if (error) return (error); /* * Enable interrupts. */ ed_asic_outb(sc, ED_WD790_ICR, ed_asic_inb(sc, ED_WD790_ICR) | ED_WD790_ICR_EIL); } error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk); if (error) { device_printf(dev, "%s cards don't support auto-detected/assigned interrupts.\n", sc->type_str); return (ENXIO); } sc->isa16bit = isa16bit; sc->mem_shared = 1; error = ed_alloc_memory(dev, 0, memsize); if (error) { printf("*** ed_alloc_memory() failed! (%d)\n", error); return (error); } sc->mem_start = (caddr_t) rman_get_virtual(sc->mem_res); /* * allocate one xmit buffer if < 16k, two buffers otherwise */ if ((memsize < 16384) || (flags & ED_FLAGS_NO_MULTI_BUFFERING)) { sc->txb_cnt = 1; } else { sc->txb_cnt = 2; } sc->tx_page_start = ED_WD_PAGE_OFFSET; sc->rec_page_start = ED_WD_PAGE_OFFSET + ED_TXBUF_SIZE * sc->txb_cnt; sc->rec_page_stop = ED_WD_PAGE_OFFSET + memsize / ED_PAGE_SIZE; sc->mem_ring = sc->mem_start + (ED_PAGE_SIZE * sc->rec_page_start); sc->mem_size = memsize; sc->mem_end = sc->mem_start + memsize; /* * Get station address from on-board ROM */ for (i = 0; i < ETHER_ADDR_LEN; ++i) sc->arpcom.ac_enaddr[i] = ed_asic_inb(sc, ED_WD_PROM + i); /* * Set upper address bits and 8/16 bit access to shared memory. */ if (isa16bit) { if (sc->chip_type == ED_CHIP_TYPE_WD790) { sc->wd_laar_proto = ed_asic_inb(sc, ED_WD_LAAR); } else { sc->wd_laar_proto = ED_WD_LAAR_L16EN | ((kvtop(sc->mem_start) >> 19) & ED_WD_LAAR_ADDRHI); } /* * Enable 16bit access */ ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto | ED_WD_LAAR_M16EN); } else { if (((sc->type & ED_WD_SOFTCONFIG) || (sc->type == ED_TYPE_TOSHIBA1) || (sc->type == ED_TYPE_TOSHIBA4) || (sc->type == ED_TYPE_WD8013EBT)) && (sc->chip_type != ED_CHIP_TYPE_WD790)) { sc->wd_laar_proto = (kvtop(sc->mem_start) >> 19) & ED_WD_LAAR_ADDRHI; ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto); } } /* * Set address and enable interface shared memory. */ if (sc->chip_type != ED_CHIP_TYPE_WD790) { if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_TOSH_ETHER) { ed_asic_outb(sc, ED_WD_MSR + 1, ((kvtop(sc->mem_start) >> 8) & 0xe0) | 4); ed_asic_outb(sc, ED_WD_MSR + 2, ((kvtop(sc->mem_start) >> 16) & 0x0f)); ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_MENB | ED_WD_MSR_POW); } else { ed_asic_outb(sc, ED_WD_MSR, ((kvtop(sc->mem_start) >> 13) & ED_WD_MSR_ADDR) | ED_WD_MSR_MENB); } sc->cr_proto = ED_CR_RD2; } else { ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_MENB); ed_asic_outb(sc, ED_WD790_HWR, (ed_asic_inb(sc, ED_WD790_HWR) | ED_WD790_HWR_SWH)); ed_asic_outb(sc, ED_WD790_RAR, ((kvtop(sc->mem_start) >> 13) & 0x0f) | ((kvtop(sc->mem_start) >> 11) & 0x40) | (ed_asic_inb(sc, ED_WD790_RAR) & 0xb0)); ed_asic_outb(sc, ED_WD790_HWR, (ed_asic_inb(sc, ED_WD790_HWR) & ~ED_WD790_HWR_SWH)); sc->cr_proto = 0; } #if 0 printf("starting memory performance test at 0x%x, size %d...\n", sc->mem_start, memsize*16384); for (i = 0; i < 16384; i++) bzero(sc->mem_start, memsize); printf("***DONE***\n"); #endif /* * Now zero memory and verify that it is clear */ bzero(sc->mem_start, memsize); for (i = 0; i < memsize; ++i) { if (sc->mem_start[i]) { device_printf(dev, "failed to clear shared memory at %lx - check configuration\n", kvtop(sc->mem_start + i)); /* * Disable 16 bit access to shared memory */ if (isa16bit) { if (sc->chip_type == ED_CHIP_TYPE_WD790) { ed_asic_outb(sc, ED_WD_MSR, 0x00); } ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto & ~ED_WD_LAAR_M16EN); } return (ENXIO); } } /* * Disable 16bit access to shared memory - we leave it * disabled so that 1) machines reboot properly when the board * is set 16 bit mode and there are conflicting 8bit * devices/ROMS in the same 128k address space as this boards * shared memory. and 2) so that other 8 bit devices with * shared memory can be used in this 128k region, too. */ if (isa16bit) { if (sc->chip_type == ED_CHIP_TYPE_WD790) { ed_asic_outb(sc, ED_WD_MSR, 0x00); } ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto & ~ED_WD_LAAR_M16EN); } return (0); } int ed_probe_WD80x3(dev, port_rid, flags) device_t dev; int port_rid; int flags; { struct ed_softc *sc = device_get_softc(dev); int error; static unsigned short *intr_vals[] = {ed_intr_val, ed_790_intr_val}; error = ed_alloc_port(dev, port_rid, ED_WD_IO_PORTS); if (error) return (error); sc->asic_offset = ED_WD_ASIC_OFFSET; sc->nic_offset = ED_WD_NIC_OFFSET; return ed_probe_WD80x3_generic(dev, flags, intr_vals); } /* * Probe and vendor-specific initialization routine for 3Com 3c503 boards */ int ed_probe_3Com(dev, port_rid, flags) device_t dev; int port_rid; int flags; { struct ed_softc *sc = device_get_softc(dev); int error; int i; u_int memsize; u_char isa16bit; u_long conf_maddr, conf_msize, irq, junk; error = ed_alloc_port(dev, 0, ED_3COM_IO_PORTS); if (error) return (error); sc->asic_offset = ED_3COM_ASIC_OFFSET; sc->nic_offset = ED_3COM_NIC_OFFSET; /* * Verify that the kernel configured I/O address matches the board * configured address */ switch (ed_asic_inb(sc, ED_3COM_BCFR)) { case ED_3COM_BCFR_300: if (rman_get_start(sc->port_res) != 0x300) return (ENXIO); break; case ED_3COM_BCFR_310: if (rman_get_start(sc->port_res) != 0x310) return (ENXIO); break; case ED_3COM_BCFR_330: if (rman_get_start(sc->port_res) != 0x330) return (ENXIO); break; case ED_3COM_BCFR_350: if (rman_get_start(sc->port_res) != 0x350) return (ENXIO); break; case ED_3COM_BCFR_250: if (rman_get_start(sc->port_res) != 0x250) return (ENXIO); break; case ED_3COM_BCFR_280: if (rman_get_start(sc->port_res) != 0x280) return (ENXIO); break; case ED_3COM_BCFR_2A0: if (rman_get_start(sc->port_res) != 0x2a0) return (ENXIO); break; case ED_3COM_BCFR_2E0: if (rman_get_start(sc->port_res) != 0x2e0) return (ENXIO); break; default: return (ENXIO); } error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &conf_maddr, &conf_msize); if (error) return (error); /* * Verify that the kernel shared memory address matches the board * configured address. */ switch (ed_asic_inb(sc, ED_3COM_PCFR)) { case ED_3COM_PCFR_DC000: if (conf_maddr != 0xdc000) return (ENXIO); break; case ED_3COM_PCFR_D8000: if (conf_maddr != 0xd8000) return (ENXIO); break; case ED_3COM_PCFR_CC000: if (conf_maddr != 0xcc000) return (ENXIO); break; case ED_3COM_PCFR_C8000: if (conf_maddr != 0xc8000) return (ENXIO); break; default: return (ENXIO); } /* * Reset NIC and ASIC. Enable on-board transceiver throughout reset * sequence because it'll lock up if the cable isn't connected if we * don't. */ ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_RST | ED_3COM_CR_XSEL); /* * Wait for a while, then un-reset it */ DELAY(50); /* * The 3Com ASIC defaults to rather strange settings for the CR after * a reset - it's important to set it again after the following outb * (this is done when we map the PROM below). */ ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_XSEL); /* * Wait a bit for the NIC to recover from the reset */ DELAY(5000); sc->vendor = ED_VENDOR_3COM; sc->type_str = "3c503"; sc->mem_shared = 1; sc->cr_proto = ED_CR_RD2; /* * Hmmm...a 16bit 3Com board has 16k of memory, but only an 8k window * to it. */ memsize = 8192; /* * Get station address from on-board ROM */ /* * First, map ethernet address PROM over the top of where the NIC * registers normally appear. */ ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_EALO | ED_3COM_CR_XSEL); for (i = 0; i < ETHER_ADDR_LEN; ++i) sc->arpcom.ac_enaddr[i] = ed_nic_inb(sc, i); /* * Unmap PROM - select NIC registers. The proper setting of the * tranceiver is set in ed_init so that the attach code is given a * chance to set the default based on a compile-time config option */ ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_XSEL); /* * Determine if this is an 8bit or 16bit board */ /* * select page 0 registers */ ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP); /* * Attempt to clear WTS bit. If it doesn't clear, then this is a 16bit * board. */ ed_nic_outb(sc, ED_P0_DCR, 0); /* * select page 2 registers */ ed_nic_outb(sc, ED_P0_CR, ED_CR_PAGE_2 | ED_CR_RD2 | ED_CR_STP); /* * The 3c503 forces the WTS bit to a one if this is a 16bit board */ if (ed_nic_inb(sc, ED_P2_DCR) & ED_DCR_WTS) isa16bit = 1; else isa16bit = 0; /* * select page 0 registers */ ed_nic_outb(sc, ED_P2_CR, ED_CR_RD2 | ED_CR_STP); error = ed_alloc_memory(dev, 0, memsize); if (error) return (error); sc->mem_start = (caddr_t) rman_get_virtual(sc->mem_res); sc->mem_size = memsize; sc->mem_end = sc->mem_start + memsize; /* * We have an entire 8k window to put the transmit buffers on the * 16bit boards. But since the 16bit 3c503's shared memory is only * fast enough to overlap the loading of one full-size packet, trying * to load more than 2 buffers can actually leave the transmitter idle * during the load. So 2 seems the best value. (Although a mix of * variable-sized packets might change this assumption. Nonetheless, * we optimize for linear transfers of same-size packets.) */ if (isa16bit) { if (flags & ED_FLAGS_NO_MULTI_BUFFERING) sc->txb_cnt = 1; else sc->txb_cnt = 2; sc->tx_page_start = ED_3COM_TX_PAGE_OFFSET_16BIT; sc->rec_page_start = ED_3COM_RX_PAGE_OFFSET_16BIT; sc->rec_page_stop = memsize / ED_PAGE_SIZE + ED_3COM_RX_PAGE_OFFSET_16BIT; sc->mem_ring = sc->mem_start; } else { sc->txb_cnt = 1; sc->tx_page_start = ED_3COM_TX_PAGE_OFFSET_8BIT; sc->rec_page_start = ED_TXBUF_SIZE + ED_3COM_TX_PAGE_OFFSET_8BIT; sc->rec_page_stop = memsize / ED_PAGE_SIZE + ED_3COM_TX_PAGE_OFFSET_8BIT; sc->mem_ring = sc->mem_start + (ED_PAGE_SIZE * ED_TXBUF_SIZE); } sc->isa16bit = isa16bit; /* * Initialize GA page start/stop registers. Probably only needed if * doing DMA, but what the hell. */ ed_asic_outb(sc, ED_3COM_PSTR, sc->rec_page_start); ed_asic_outb(sc, ED_3COM_PSPR, sc->rec_page_stop); /* * Set IRQ. 3c503 only allows a choice of irq 2-5. */ error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk); if (error) return (error); switch (irq) { case 2: case 9: ed_asic_outb(sc, ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ2); break; case 3: ed_asic_outb(sc, ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ3); break; case 4: ed_asic_outb(sc, ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ4); break; case 5: ed_asic_outb(sc, ED_3COM_IDCFR, ED_3COM_IDCFR_IRQ5); break; default: device_printf(dev, "Invalid irq configuration (%ld) must be 3-5,9 for 3c503\n", irq); return (ENXIO); } /* * Initialize GA configuration register. Set bank and enable shared * mem. */ ed_asic_outb(sc, ED_3COM_GACFR, ED_3COM_GACFR_RSEL | ED_3COM_GACFR_MBS0); /* * Initialize "Vector Pointer" registers. These gawd-awful things are * compared to 20 bits of the address on ISA, and if they match, the * shared memory is disabled. We set them to 0xffff0...allegedly the * reset vector. */ ed_asic_outb(sc, ED_3COM_VPTR2, 0xff); ed_asic_outb(sc, ED_3COM_VPTR1, 0xff); ed_asic_outb(sc, ED_3COM_VPTR0, 0x00); /* * Zero memory and verify that it is clear */ bzero(sc->mem_start, memsize); for (i = 0; i < memsize; ++i) if (sc->mem_start[i]) { device_printf(dev, "failed to clear shared memory at %lx - check configuration\n", kvtop(sc->mem_start + i)); return (ENXIO); } return (0); } /* * Probe and vendor-specific initialization routine for NE1000/2000 boards */ int ed_probe_Novell_generic(dev, flags) device_t dev; int flags; { struct ed_softc *sc = device_get_softc(dev); u_int memsize, n; u_char romdata[16], tmp; static char test_pattern[32] = "THIS is A memory TEST pattern"; char test_buffer[32]; /* XXX - do Novell-specific probe here */ /* Reset the board */ if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_GWETHER) { ed_asic_outb(sc, ED_NOVELL_RESET, 0); DELAY(200); } tmp = ed_asic_inb(sc, ED_NOVELL_RESET); /* * I don't know if this is necessary; probably cruft leftover from * Clarkson packet driver code. Doesn't do a thing on the boards I've * tested. -DG [note that a outb(0x84, 0) seems to work here, and is * non-invasive...but some boards don't seem to reset and I don't have * complete documentation on what the 'right' thing to do is...so we * do the invasive thing for now. Yuck.] */ ed_asic_outb(sc, ED_NOVELL_RESET, tmp); DELAY(5000); /* * This is needed because some NE clones apparently don't reset the * NIC properly (or the NIC chip doesn't reset fully on power-up) XXX * - this makes the probe invasive! ...Done against my better * judgement. -DLG */ ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP); DELAY(5000); /* Make sure that we really have an 8390 based board */ if (!ed_probe_generic8390(sc)) return (ENXIO); sc->vendor = ED_VENDOR_NOVELL; sc->mem_shared = 0; sc->cr_proto = ED_CR_RD2; /* * Test the ability to read and write to the NIC memory. This has the * side affect of determining if this is an NE1000 or an NE2000. */ /* * This prevents packets from being stored in the NIC memory when the * readmem routine turns on the start bit in the CR. */ ed_nic_outb(sc, ED_P0_RCR, ED_RCR_MON); /* Temporarily initialize DCR for byte operations */ ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS); ed_nic_outb(sc, ED_P0_PSTART, 8192 / ED_PAGE_SIZE); ed_nic_outb(sc, ED_P0_PSTOP, 16384 / ED_PAGE_SIZE); sc->isa16bit = 0; /* * Write a test pattern in byte mode. If this fails, then there * probably isn't any memory at 8k - which likely means that the board * is an NE2000. */ ed_pio_writemem(sc, test_pattern, 8192, sizeof(test_pattern)); ed_pio_readmem(sc, 8192, test_buffer, sizeof(test_pattern)); if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)) == 0) { sc->type = ED_TYPE_NE1000; sc->type_str = "NE1000"; } else { /* neither an NE1000 nor a Linksys - try NE2000 */ ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS); ed_nic_outb(sc, ED_P0_PSTART, 16384 / ED_PAGE_SIZE); ed_nic_outb(sc, ED_P0_PSTOP, 32768 / ED_PAGE_SIZE); sc->isa16bit = 1; /* * Write a test pattern in word mode. If this also fails, then * we don't know what this board is. */ ed_pio_writemem(sc, test_pattern, 16384, sizeof(test_pattern)); ed_pio_readmem(sc, 16384, test_buffer, sizeof(test_pattern)); if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)) == 0) { sc->type = ED_TYPE_NE2000; sc->type_str = "NE2000"; } else { return (ENXIO); } } /* 8k of memory plus an additional 8k if 16bit */ memsize = 8192 + sc->isa16bit * 8192; #if 0 /* probably not useful - NE boards only come two ways */ /* allow kernel config file overrides */ if (isa_dev->id_msize) memsize = isa_dev->id_msize; #endif sc->mem_size = memsize; /* NIC memory doesn't start at zero on an NE board */ /* The start address is tied to the bus width */ sc->mem_start = (char *) 8192 + sc->isa16bit * 8192; sc->mem_end = sc->mem_start + memsize; sc->tx_page_start = memsize / ED_PAGE_SIZE; if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_GWETHER) { int x, i, mstart = 0, msize = 0; char pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE], tbuf[ED_PAGE_SIZE]; for (i = 0; i < ED_PAGE_SIZE; i++) pbuf0[i] = 0; /* Clear all the memory. */ for (x = 1; x < 256; x++) ed_pio_writemem(sc, pbuf0, x * 256, ED_PAGE_SIZE); /* Search for the start of RAM. */ for (x = 1; x < 256; x++) { ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE); if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) { for (i = 0; i < ED_PAGE_SIZE; i++) pbuf[i] = 255 - x; ed_pio_writemem(sc, pbuf, x * 256, ED_PAGE_SIZE); ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE); if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) { mstart = x * ED_PAGE_SIZE; msize = ED_PAGE_SIZE; break; } } } if (mstart == 0) { device_printf(dev, "Cannot find start of RAM.\n"); return (ENXIO); } /* Search for the start of RAM. */ for (x = (mstart / ED_PAGE_SIZE) + 1; x < 256; x++) { ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE); if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) { for (i = 0; i < ED_PAGE_SIZE; i++) pbuf[i] = 255 - x; ed_pio_writemem(sc, pbuf, x * 256, ED_PAGE_SIZE); ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE); if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) msize += ED_PAGE_SIZE; else { break; } } else { break; } } if (msize == 0) { device_printf(dev, "Cannot find any RAM, start : %d, x = %d.\n", mstart, x); return (ENXIO); } device_printf(dev, "RAM start at %d, size : %d.\n", mstart, msize); sc->mem_size = msize; sc->mem_start = (caddr_t) mstart; sc->mem_end = (caddr_t) (msize + mstart); sc->tx_page_start = mstart / ED_PAGE_SIZE; } /* * Use one xmit buffer if < 16k, two buffers otherwise (if not told * otherwise). */ if ((memsize < 16384) || (flags & ED_FLAGS_NO_MULTI_BUFFERING)) sc->txb_cnt = 1; else sc->txb_cnt = 2; sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE; sc->rec_page_stop = sc->tx_page_start + memsize / ED_PAGE_SIZE; sc->mem_ring = sc->mem_start + sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE; ed_pio_readmem(sc, 0, romdata, 16); for (n = 0; n < ETHER_ADDR_LEN; n++) sc->arpcom.ac_enaddr[n] = romdata[n * (sc->isa16bit + 1)]; if ((ED_FLAGS_GETTYPE(flags) == ED_FLAGS_GWETHER) && (sc->arpcom.ac_enaddr[2] == 0x86)) { sc->type_str = "Gateway AT"; } /* clear any pending interrupts that might have occurred above */ ed_nic_outb(sc, ED_P0_ISR, 0xff); return (0); } int ed_probe_Novell(dev, port_rid, flags) device_t dev; int port_rid; int flags; { struct ed_softc *sc = device_get_softc(dev); int error; error = ed_alloc_port(dev, port_rid, ED_NOVELL_IO_PORTS); if (error) return (error); sc->asic_offset = ED_NOVELL_ASIC_OFFSET; sc->nic_offset = ED_NOVELL_NIC_OFFSET; return ed_probe_Novell_generic(dev, flags); } #define ED_HPP_TEST_SIZE 16 /* * Probe and vendor specific initialization for the HP PC Lan+ Cards. * (HP Part nos: 27247B and 27252A). * * The card has an asic wrapper around a DS8390 core. The asic handles * host accesses and offers both standard register IO and memory mapped * IO. Memory mapped I/O allows better performance at the expense of greater * chance of an incompatibility with existing ISA cards. * * The card has a few caveats: it isn't tolerant of byte wide accesses, only * short (16 bit) or word (32 bit) accesses are allowed. Some card revisions * don't allow 32 bit accesses; these are indicated by a bit in the software * ID register (see if_edreg.h). * * Other caveats are: we should read the MAC address only when the card * is inactive. * * For more information; please consult the CRYNWR packet driver. * * The AUI port is turned on using the "link2" option on the ifconfig * command line. */ int ed_probe_HP_pclanp(dev, port_rid, flags) device_t dev; int port_rid; int flags; { struct ed_softc *sc = device_get_softc(dev); int error; int n; /* temp var */ int memsize; /* mem on board */ u_char checksum; /* checksum of board address */ u_char irq; /* board configured IRQ */ char test_pattern[ED_HPP_TEST_SIZE]; /* read/write areas for */ char test_buffer[ED_HPP_TEST_SIZE]; /* probing card */ u_long conf_maddr, conf_msize, conf_irq, junk; error = ed_alloc_port(dev, 0, ED_HPP_IO_PORTS); if (error) return (error); /* Fill in basic information */ sc->asic_offset = ED_HPP_ASIC_OFFSET; sc->nic_offset = ED_HPP_NIC_OFFSET; sc->chip_type = ED_CHIP_TYPE_DP8390; sc->isa16bit = 0; /* the 8390 core needs to be in byte mode */ /* * Look for the HP PCLAN+ signature: "0x50,0x48,0x00,0x53" */ if ((ed_asic_inb(sc, ED_HPP_ID) != 0x50) || (ed_asic_inb(sc, ED_HPP_ID + 1) != 0x48) || ((ed_asic_inb(sc, ED_HPP_ID + 2) & 0xF0) != 0) || (ed_asic_inb(sc, ED_HPP_ID + 3) != 0x53)) return ENXIO; /* * Read the MAC address and verify checksum on the address. */ ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_MAC); for (n = 0, checksum = 0; n < ETHER_ADDR_LEN; n++) checksum += (sc->arpcom.ac_enaddr[n] = ed_asic_inb(sc, ED_HPP_MAC_ADDR + n)); checksum += ed_asic_inb(sc, ED_HPP_MAC_ADDR + ETHER_ADDR_LEN); if (checksum != 0xFF) return ENXIO; /* * Verify that the software model number is 0. */ ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_ID); if (((sc->hpp_id = ed_asic_inw(sc, ED_HPP_PAGE_4)) & ED_HPP_ID_SOFT_MODEL_MASK) != 0x0000) return ENXIO; /* * Read in and save the current options configured on card. */ sc->hpp_options = ed_asic_inw(sc, ED_HPP_OPTION); sc->hpp_options |= (ED_HPP_OPTION_NIC_RESET | ED_HPP_OPTION_CHIP_RESET | ED_HPP_OPTION_ENABLE_IRQ); /* * Reset the chip. This requires writing to the option register * so take care to preserve the other bits. */ ed_asic_outw(sc, ED_HPP_OPTION, (sc->hpp_options & ~(ED_HPP_OPTION_NIC_RESET | ED_HPP_OPTION_CHIP_RESET))); DELAY(5000); /* wait for chip reset to complete */ ed_asic_outw(sc, ED_HPP_OPTION, (sc->hpp_options | (ED_HPP_OPTION_NIC_RESET | ED_HPP_OPTION_CHIP_RESET | ED_HPP_OPTION_ENABLE_IRQ))); DELAY(5000); if (!(ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST)) return ENXIO; /* reset did not complete */ /* * Read out configuration information. */ ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_HW); irq = ed_asic_inb(sc, ED_HPP_HW_IRQ); /* * Check for impossible IRQ. */ if (irq >= (sizeof(ed_hpp_intr_val) / sizeof(ed_hpp_intr_val[0]))) return ENXIO; /* * If the kernel IRQ was specified with a '?' use the cards idea * of the IRQ. If the kernel IRQ was explicitly specified, it * should match that of the hardware. */ error = bus_get_resource(dev, SYS_RES_IRQ, 0, &conf_irq, &junk); if (error) { bus_set_resource(dev, SYS_RES_IRQ, 0, ed_hpp_intr_val[irq], 1); } else { if (conf_irq != ed_hpp_intr_val[irq]) return (ENXIO); } /* * Fill in softconfig info. */ sc->vendor = ED_VENDOR_HP; sc->type = ED_TYPE_HP_PCLANPLUS; sc->type_str = "HP-PCLAN+"; sc->mem_shared = 0; /* we DON'T have dual ported RAM */ sc->mem_start = 0; /* we use offsets inside the card RAM */ sc->hpp_mem_start = NULL;/* no memory mapped I/O by default */ /* * The board has 32KB of memory. Is there a way to determine * this programmatically? */ memsize = 32768; /* * Check if memory mapping of the I/O registers possible. */ if (sc->hpp_options & ED_HPP_OPTION_MEM_ENABLE) { u_long mem_addr; /* * determine the memory address from the board. */ ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_HW); mem_addr = (ed_asic_inw(sc, ED_HPP_HW_MEM_MAP) << 8); /* * Check that the kernel specified start of memory and * hardware's idea of it match. */ error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &conf_maddr, &conf_msize); if (error) return (error); if (mem_addr != conf_maddr) return ENXIO; error = ed_alloc_memory(dev, 0, memsize); if (error) return (error); sc->hpp_mem_start = rman_get_virtual(sc->mem_res); } /* * Fill in the rest of the soft config structure. */ /* * The transmit page index. */ sc->tx_page_start = ED_HPP_TX_PAGE_OFFSET; if (device_get_flags(dev) & ED_FLAGS_NO_MULTI_BUFFERING) sc->txb_cnt = 1; else sc->txb_cnt = 2; /* * Memory description */ sc->mem_size = memsize; sc->mem_ring = sc->mem_start + (sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE); sc->mem_end = sc->mem_start + sc->mem_size; /* * Receive area starts after the transmit area and * continues till the end of memory. */ sc->rec_page_start = sc->tx_page_start + (sc->txb_cnt * ED_TXBUF_SIZE); sc->rec_page_stop = (sc->mem_size / ED_PAGE_SIZE); sc->cr_proto = 0; /* value works */ /* * Set the wrap registers for string I/O reads. */ ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_HW); ed_asic_outw(sc, ED_HPP_HW_WRAP, ((sc->rec_page_start / ED_PAGE_SIZE) | (((sc->rec_page_stop / ED_PAGE_SIZE) - 1) << 8))); /* * Reset the register page to normal operation. */ ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_PERF); /* * Verify that we can read/write from adapter memory. * Create test pattern. */ for (n = 0; n < ED_HPP_TEST_SIZE; n++) { test_pattern[n] = (n*n) ^ ~n; } #undef ED_HPP_TEST_SIZE /* * Check that the memory is accessible thru the I/O ports. * Write out the contents of "test_pattern", read back * into "test_buffer" and compare the two for any * mismatch. */ for (n = 0; n < (32768 / ED_PAGE_SIZE); n ++) { ed_hpp_writemem(sc, test_pattern, (n * ED_PAGE_SIZE), sizeof(test_pattern)); ed_hpp_readmem(sc, (n * ED_PAGE_SIZE), test_buffer, sizeof(test_pattern)); if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) return ENXIO; } return (ED_HPP_IO_PORTS); } /* * HP PC Lan+ : Set the physical link to use AUI or TP/TL. */ void ed_hpp_set_physical_link(struct ed_softc *sc) { struct ifnet *ifp = &sc->arpcom.ac_if; int lan_page; ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_LAN); lan_page = ed_asic_inw(sc, ED_HPP_PAGE_0); if (ifp->if_flags & IFF_ALTPHYS) { /* * Use the AUI port. */ lan_page |= ED_HPP_LAN_AUI; ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_LAN); ed_asic_outw(sc, ED_HPP_PAGE_0, lan_page); } else { /* * Use the ThinLan interface */ lan_page &= ~ED_HPP_LAN_AUI; ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_LAN); ed_asic_outw(sc, ED_HPP_PAGE_0, lan_page); } /* * Wait for the lan card to re-initialize itself */ DELAY(150000); /* wait 150 ms */ /* * Restore normal pages. */ ed_asic_outw(sc, ED_HPP_PAGING, ED_HPP_PAGE_PERF); } /* * Allocate a port resource with the given resource id. */ int ed_alloc_port(dev, rid, size) device_t dev; int rid; int size; { struct ed_softc *sc = device_get_softc(dev); struct resource *res; res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0ul, ~0ul, size, RF_ACTIVE); if (res) { sc->port_rid = rid; sc->port_res = res; sc->port_used = size; return (0); } else { return (ENOENT); } } /* * Allocate a memory resource with the given resource id. */ int ed_alloc_memory(dev, rid, size) device_t dev; int rid; int size; { struct ed_softc *sc = device_get_softc(dev); struct resource *res; res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0ul, ~0ul, size, RF_ACTIVE); if (res) { sc->mem_rid = rid; sc->mem_res = res; sc->mem_used = size; return (0); } else { return (ENOENT); } } /* * Allocate an irq resource with the given resource id. */ int ed_alloc_irq(dev, rid, flags) device_t dev; int rid; int flags; { struct ed_softc *sc = device_get_softc(dev); struct resource *res; res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1, (RF_ACTIVE | flags)); if (res) { sc->irq_rid = rid; sc->irq_res = res; return (0); } else { return (ENOENT); } } /* * Release all resources */ void ed_release_resources(dev) device_t dev; { struct ed_softc *sc = device_get_softc(dev); if (sc->port_res) { bus_deactivate_resource(dev, SYS_RES_IOPORT, sc->port_rid, sc->port_res); bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid, sc->port_res); sc->port_res = 0; } if (sc->mem_res) { bus_deactivate_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem_res); bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem_res); sc->mem_res = 0; } if (sc->irq_res) { bus_deactivate_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq_res); bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq_res); sc->irq_res = 0; } } /* * Install interface into kernel networking data structures */ int ed_attach(sc, unit, flags) struct ed_softc *sc; int unit; int flags; { struct ifnet *ifp = &sc->arpcom.ac_if; /* * Set interface to stopped condition (reset) */ ed_stop(sc); if (!ifp->if_name) { /* * Initialize ifnet structure */ ifp->if_softc = sc; ifp->if_unit = unit; ifp->if_name = "ed"; ifp->if_output = ether_output; ifp->if_start = ed_start; ifp->if_ioctl = ed_ioctl; ifp->if_watchdog = ed_watchdog; ifp->if_init = ed_init; ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; ifp->if_linkmib = &sc->mibdata; ifp->if_linkmiblen = sizeof sc->mibdata; /* * XXX - should do a better job. */ if (sc->chip_type == ED_CHIP_TYPE_WD790) sc->mibdata.dot3StatsEtherChipSet = DOT3CHIPSET(dot3VendorWesternDigital, dot3ChipSetWesternDigital83C790); else sc->mibdata.dot3StatsEtherChipSet = DOT3CHIPSET(dot3VendorNational, dot3ChipSetNational8390); sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS; /* * Set default state for ALTPHYS flag (used to disable the * tranceiver for AUI operation), based on compile-time * config option. */ if (flags & ED_FLAGS_DISABLE_TRANCEIVER) ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | IFF_ALTPHYS); else ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); /* * Attach the interface */ ether_ifattach(ifp, ETHER_BPF_SUPPORTED); } /* device attach does transition from UNCONFIGURED to IDLE state */ /* * Print additional info when attached */ printf("%s%d: address %6D, ", ifp->if_name, ifp->if_unit, sc->arpcom.ac_enaddr, ":"); if (sc->type_str && (*sc->type_str != 0)) printf("type %s ", sc->type_str); else printf("type unknown (0x%x) ", sc->type); if (sc->vendor == ED_VENDOR_HP) printf("(%s %s IO)", (sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS) ? "16-bit" : "32-bit", sc->hpp_mem_start ? "memory mapped" : "regular"); else printf("%s ", sc->isa16bit ? "(16 bit)" : "(8 bit)"); printf("%s\n", (((sc->vendor == ED_VENDOR_3COM) || (sc->vendor == ED_VENDOR_HP)) && (ifp->if_flags & IFF_ALTPHYS)) ? " tranceiver disabled" : ""); return (0); } /* * Reset interface. */ static void ed_reset(ifp) struct ifnet *ifp; { struct ed_softc *sc = ifp->if_softc; int s; if (sc->gone) return; s = splimp(); /* * Stop interface and re-initialize. */ ed_stop(sc); ed_init(sc); (void) splx(s); } /* * Take interface offline. */ void ed_stop(sc) struct ed_softc *sc; { int n = 5000; if (sc->gone) return; /* * Stop everything on the interface, and select page 0 registers. */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP); /* * Wait for interface to enter stopped state, but limit # of checks to * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but * just in case it's an old one. */ if (sc->chip_type != ED_CHIP_TYPE_AX88190) while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n); } /* * Device timeout/watchdog routine. Entered if the device neglects to * generate an interrupt after a transmit has been started on it. */ static void ed_watchdog(ifp) struct ifnet *ifp; { struct ed_softc *sc = ifp->if_softc; if (sc->gone) return; log(LOG_ERR, "ed%d: device timeout\n", ifp->if_unit); ifp->if_oerrors++; ed_reset(ifp); } /* * Initialize device. */ static void ed_init(xsc) void *xsc; { struct ed_softc *sc = xsc; struct ifnet *ifp = &sc->arpcom.ac_if; int i, s; if (sc->gone) return; /* address not known */ if (TAILQ_EMPTY(&ifp->if_addrhead)) /* unlikely? XXX */ return; /* * Initialize the NIC in the exact order outlined in the NS manual. * This init procedure is "mandatory"...don't change what or when * things happen. */ s = splimp(); /* reset transmitter flags */ sc->xmit_busy = 0; ifp->if_timer = 0; sc->txb_inuse = 0; sc->txb_new = 0; sc->txb_next_tx = 0; /* This variable is used below - don't move this assignment */ sc->next_packet = sc->rec_page_start + 1; /* * Set interface for page 0, Remote DMA complete, Stopped */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP); if (sc->isa16bit) { /* * Set FIFO threshold to 8, No auto-init Remote DMA, byte * order=80x86, word-wide DMA xfers, */ ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS); } else { /* * Same as above, but byte-wide DMA xfers */ ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS); } /* * Clear Remote Byte Count Registers */ ed_nic_outb(sc, ED_P0_RBCR0, 0); ed_nic_outb(sc, ED_P0_RBCR1, 0); /* * For the moment, don't store incoming packets in memory. */ ed_nic_outb(sc, ED_P0_RCR, ED_RCR_MON); /* * Place NIC in internal loopback mode */ ed_nic_outb(sc, ED_P0_TCR, ED_TCR_LB0); /* * Initialize transmit/receive (ring-buffer) Page Start */ ed_nic_outb(sc, ED_P0_TPSR, sc->tx_page_start); ed_nic_outb(sc, ED_P0_PSTART, sc->rec_page_start); /* Set lower bits of byte addressable framing to 0 */ if (sc->chip_type == ED_CHIP_TYPE_WD790) ed_nic_outb(sc, 0x09, 0); /* * Initialize Receiver (ring-buffer) Page Stop and Boundry */ ed_nic_outb(sc, ED_P0_PSTOP, sc->rec_page_stop); ed_nic_outb(sc, ED_P0_BNRY, sc->rec_page_start); /* * Clear all interrupts. A '1' in each bit position clears the * corresponding flag. */ ed_nic_outb(sc, ED_P0_ISR, 0xff); /* * Enable the following interrupts: receive/transmit complete, * receive/transmit error, and Receiver OverWrite. * * Counter overflow and Remote DMA complete are *not* enabled. */ ed_nic_outb(sc, ED_P0_IMR, ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE | ED_IMR_OVWE); /* * Program Command Register for page 1 */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP); /* * Copy out our station address */ for (i = 0; i < ETHER_ADDR_LEN; ++i) ed_nic_outb(sc, ED_P1_PAR(i), sc->arpcom.ac_enaddr[i]); /* * Set Current Page pointer to next_packet (initialized above) */ ed_nic_outb(sc, ED_P1_CURR, sc->next_packet); /* * Program Receiver Configuration Register and multicast filter. CR is * set to page 0 on return. */ ed_setrcr(sc); /* * Take interface out of loopback */ ed_nic_outb(sc, ED_P0_TCR, 0); /* * If this is a 3Com board, the tranceiver must be software enabled * (there is no settable hardware default). */ if (sc->vendor == ED_VENDOR_3COM) { if (ifp->if_flags & IFF_ALTPHYS) { ed_asic_outb(sc, ED_3COM_CR, 0); } else { ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_XSEL); } } /* * Set 'running' flag, and clear output active flag. */ ifp->if_flags |= IFF_RUNNING; ifp->if_flags &= ~IFF_OACTIVE; /* * ...and attempt to start output */ ed_start(ifp); (void) splx(s); } /* * This routine actually starts the transmission on the interface */ static __inline void ed_xmit(sc) struct ed_softc *sc; { struct ifnet *ifp = (struct ifnet *)sc; unsigned short len; if (sc->gone) return; len = sc->txb_len[sc->txb_next_tx]; /* * Set NIC for page 0 register access */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA); /* * Set TX buffer start page */ ed_nic_outb(sc, ED_P0_TPSR, sc->tx_page_start + sc->txb_next_tx * ED_TXBUF_SIZE); /* * Set TX length */ ed_nic_outb(sc, ED_P0_TBCR0, len); ed_nic_outb(sc, ED_P0_TBCR1, len >> 8); /* * Set page 0, Remote DMA complete, Transmit Packet, and *Start* */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_TXP | ED_CR_STA); sc->xmit_busy = 1; /* * Point to next transmit buffer slot and wrap if necessary. */ sc->txb_next_tx++; if (sc->txb_next_tx == sc->txb_cnt) sc->txb_next_tx = 0; /* * Set a timer just in case we never hear from the board again */ ifp->if_timer = 2; } /* * Start output on interface. * We make two assumptions here: * 1) that the current priority is set to splimp _before_ this code * is called *and* is returned to the appropriate priority after * return * 2) that the IFF_OACTIVE flag is checked before this code is called * (i.e. that the output part of the interface is idle) */ static void ed_start(ifp) struct ifnet *ifp; { struct ed_softc *sc = ifp->if_softc; struct mbuf *m0, *m; caddr_t buffer; int len; if (sc->gone) { printf("ed_start(%p) GONE\n",ifp); return; } outloop: /* * First, see if there are buffered packets and an idle transmitter - * should never happen at this point. */ if (sc->txb_inuse && (sc->xmit_busy == 0)) { printf("ed: packets buffered, but transmitter idle\n"); ed_xmit(sc); } /* * See if there is room to put another packet in the buffer. */ if (sc->txb_inuse == sc->txb_cnt) { /* * No room. Indicate this to the outside world and exit. */ ifp->if_flags |= IFF_OACTIVE; return; } IF_DEQUEUE(&ifp->if_snd, m); if (m == 0) { /* * We are using the !OACTIVE flag to indicate to the outside * world that we can accept an additional packet rather than * that the transmitter is _actually_ active. Indeed, the * transmitter may be active, but if we haven't filled all the * buffers with data then we still want to accept more. */ ifp->if_flags &= ~IFF_OACTIVE; return; } /* * Copy the mbuf chain into the transmit buffer */ m0 = m; /* txb_new points to next open buffer slot */ buffer = sc->mem_start + (sc->txb_new * ED_TXBUF_SIZE * ED_PAGE_SIZE); if (sc->mem_shared) { /* * Special case setup for 16 bit boards... */ if (sc->isa16bit) { switch (sc->vendor) { /* * For 16bit 3Com boards (which have 16k of * memory), we have the xmit buffers in a * different page of memory ('page 0') - so * change pages. */ case ED_VENDOR_3COM: ed_asic_outb(sc, ED_3COM_GACFR, ED_3COM_GACFR_RSEL); break; /* * Enable 16bit access to shared memory on * WD/SMC boards. */ case ED_VENDOR_WD_SMC: ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto | ED_WD_LAAR_M16EN); if (sc->chip_type == ED_CHIP_TYPE_WD790) { ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_MENB); } break; } } for (len = 0; m != 0; m = m->m_next) { bcopy(mtod(m, caddr_t), buffer, m->m_len); buffer += m->m_len; len += m->m_len; } /* * Restore previous shared memory access */ if (sc->isa16bit) { switch (sc->vendor) { case ED_VENDOR_3COM: ed_asic_outb(sc, ED_3COM_GACFR, ED_3COM_GACFR_RSEL | ED_3COM_GACFR_MBS0); break; case ED_VENDOR_WD_SMC: if (sc->chip_type == ED_CHIP_TYPE_WD790) { ed_asic_outb(sc, ED_WD_MSR, 0x00); } ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto & ~ED_WD_LAAR_M16EN); break; } } } else { len = ed_pio_write_mbufs(sc, m, (int)buffer); if (len == 0) goto outloop; } sc->txb_len[sc->txb_new] = max(len, (ETHER_MIN_LEN-ETHER_CRC_LEN)); sc->txb_inuse++; /* * Point to next buffer slot and wrap if necessary. */ sc->txb_new++; if (sc->txb_new == sc->txb_cnt) sc->txb_new = 0; if (sc->xmit_busy == 0) ed_xmit(sc); /* * Tap off here if there is a bpf listener. */ if (ifp->if_bpf) { bpf_mtap(ifp, m0); } m_freem(m0); /* * Loop back to the top to possibly buffer more packets */ goto outloop; } /* * Ethernet interface receiver interrupt. */ static __inline void ed_rint(sc) struct ed_softc *sc; { struct ifnet *ifp = &sc->arpcom.ac_if; u_char boundry; u_short len; struct ed_ring packet_hdr; char *packet_ptr; if (sc->gone) return; /* * Set NIC to page 1 registers to get 'current' pointer */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA); /* * 'sc->next_packet' is the logical beginning of the ring-buffer - * i.e. it points to where new data has been buffered. The 'CURR' * (current) register points to the logical end of the ring-buffer - * i.e. it points to where additional new data will be added. We loop * here until the logical beginning equals the logical end (or in * other words, until the ring-buffer is empty). */ while (sc->next_packet != ed_nic_inb(sc, ED_P1_CURR)) { /* get pointer to this buffer's header structure */ packet_ptr = sc->mem_ring + (sc->next_packet - sc->rec_page_start) * ED_PAGE_SIZE; /* * The byte count includes a 4 byte header that was added by * the NIC. */ if (sc->mem_shared) packet_hdr = *(struct ed_ring *) packet_ptr; else ed_pio_readmem(sc, (int)packet_ptr, (char *) &packet_hdr, sizeof(packet_hdr)); len = packet_hdr.count; if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring)) || len < (ETHER_MIN_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring))) { /* * Length is a wild value. There's a good chance that * this was caused by the NIC being old and buggy. * The bug is that the length low byte is duplicated in * the high byte. Try to recalculate the length based on * the pointer to the next packet. */ /* * NOTE: sc->next_packet is pointing at the current packet. */ len &= ED_PAGE_SIZE - 1; /* preserve offset into page */ if (packet_hdr.next_packet >= sc->next_packet) { len += (packet_hdr.next_packet - sc->next_packet) * ED_PAGE_SIZE; } else { len += ((packet_hdr.next_packet - sc->rec_page_start) + (sc->rec_page_stop - sc->next_packet)) * ED_PAGE_SIZE; } /* * because buffers are aligned on 256-byte boundary, * the length computed above is off by 256 in almost * all cases. Fix it... */ if (len & 0xff) len -= 256 ; if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN + sizeof(struct ed_ring))) sc->mibdata.dot3StatsFrameTooLongs++; } /* * Be fairly liberal about what we allow as a "reasonable" length * so that a [crufty] packet will make it to BPF (and can thus * be analyzed). Note that all that is really important is that * we have a length that will fit into one mbuf cluster or less; * the upper layer protocols can then figure out the length from * their own length field(s). */ if ((len > sizeof(struct ed_ring)) && (len <= MCLBYTES) && (packet_hdr.next_packet >= sc->rec_page_start) && (packet_hdr.next_packet < sc->rec_page_stop)) { /* * Go get packet. */ ed_get_packet(sc, packet_ptr + sizeof(struct ed_ring), len - sizeof(struct ed_ring)); ifp->if_ipackets++; } else { /* * Really BAD. The ring pointers are corrupted. */ log(LOG_ERR, "ed%d: NIC memory corrupt - invalid packet length %d\n", ifp->if_unit, len); ifp->if_ierrors++; ed_reset(ifp); return; } /* * Update next packet pointer */ sc->next_packet = packet_hdr.next_packet; /* * Update NIC boundry pointer - being careful to keep it one * buffer behind. (as recommended by NS databook) */ boundry = sc->next_packet - 1; if (boundry < sc->rec_page_start) boundry = sc->rec_page_stop - 1; /* * Set NIC to page 0 registers to update boundry register */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA); ed_nic_outb(sc, ED_P0_BNRY, boundry); /* * Set NIC to page 1 registers before looping to top (prepare * to get 'CURR' current pointer) */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA); } } /* * Ethernet interface interrupt processor */ void edintr(arg) void *arg; { struct ed_softc *sc = (struct ed_softc*) arg; struct ifnet *ifp = (struct ifnet *)sc; u_char isr; if (sc->gone) return; /* * Set NIC to page 0 registers */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA); /* * loop until there are no more new interrupts */ while ((isr = ed_nic_inb(sc, ED_P0_ISR)) != 0) { /* * reset all the bits that we are 'acknowledging' by writing a * '1' to each bit position that was set (writing a '1' * *clears* the bit) */ ed_nic_outb(sc, ED_P0_ISR, isr); /* XXX workaround for AX88190 */ if (sc->chip_type == ED_CHIP_TYPE_AX88190) { while (ed_nic_inb(sc, ED_P0_ISR) & isr) { ed_nic_outb(sc, ED_P0_ISR,0); ed_nic_outb(sc, ED_P0_ISR,isr); } } /* * Handle transmitter interrupts. Handle these first because * the receiver will reset the board under some conditions. */ if (isr & (ED_ISR_PTX | ED_ISR_TXE)) { u_char collisions = ed_nic_inb(sc, ED_P0_NCR) & 0x0f; /* * Check for transmit error. If a TX completed with an * error, we end up throwing the packet away. Really * the only error that is possible is excessive * collisions, and in this case it is best to allow * the automatic mechanisms of TCP to backoff the * flow. Of course, with UDP we're screwed, but this * is expected when a network is heavily loaded. */ (void) ed_nic_inb(sc, ED_P0_TSR); if (isr & ED_ISR_TXE) { u_char tsr; /* * Excessive collisions (16) */ tsr = ed_nic_inb(sc, ED_P0_TSR); if ((tsr & ED_TSR_ABT) && (collisions == 0)) { /* * When collisions total 16, the * P0_NCR will indicate 0, and the * TSR_ABT is set. */ collisions = 16; sc->mibdata.dot3StatsExcessiveCollisions++; sc->mibdata.dot3StatsCollFrequencies[15]++; } if (tsr & ED_TSR_OWC) sc->mibdata.dot3StatsLateCollisions++; if (tsr & ED_TSR_CDH) sc->mibdata.dot3StatsSQETestErrors++; if (tsr & ED_TSR_CRS) sc->mibdata.dot3StatsCarrierSenseErrors++; if (tsr & ED_TSR_FU) sc->mibdata.dot3StatsInternalMacTransmitErrors++; /* * update output errors counter */ ifp->if_oerrors++; } else { /* * Update total number of successfully * transmitted packets. */ ifp->if_opackets++; } /* * reset tx busy and output active flags */ sc->xmit_busy = 0; ifp->if_flags &= ~IFF_OACTIVE; /* * clear watchdog timer */ ifp->if_timer = 0; /* * Add in total number of collisions on last * transmission. */ ifp->if_collisions += collisions; switch(collisions) { case 0: case 16: break; case 1: sc->mibdata.dot3StatsSingleCollisionFrames++; sc->mibdata.dot3StatsCollFrequencies[0]++; break; default: sc->mibdata.dot3StatsMultipleCollisionFrames++; sc->mibdata. dot3StatsCollFrequencies[collisions-1] ++; break; } /* * Decrement buffer in-use count if not zero (can only * be zero if a transmitter interrupt occured while * not actually transmitting). If data is ready to * transmit, start it transmitting, otherwise defer * until after handling receiver */ if (sc->txb_inuse && --sc->txb_inuse) ed_xmit(sc); } /* * Handle receiver interrupts */ if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) { /* * Overwrite warning. In order to make sure that a * lockup of the local DMA hasn't occurred, we reset * and re-init the NIC. The NSC manual suggests only a * partial reset/re-init is necessary - but some chips * seem to want more. The DMA lockup has been seen * only with early rev chips - Methinks this bug was * fixed in later revs. -DG */ if (isr & ED_ISR_OVW) { ifp->if_ierrors++; #ifdef DIAGNOSTIC log(LOG_WARNING, "ed%d: warning - receiver ring buffer overrun\n", ifp->if_unit); #endif /* * Stop/reset/re-init NIC */ ed_reset(ifp); } else { /* * Receiver Error. One or more of: CRC error, * frame alignment error FIFO overrun, or * missed packet. */ if (isr & ED_ISR_RXE) { u_char rsr; rsr = ed_nic_inb(sc, ED_P0_RSR); if (rsr & ED_RSR_CRC) sc->mibdata.dot3StatsFCSErrors++; if (rsr & ED_RSR_FAE) sc->mibdata.dot3StatsAlignmentErrors++; if (rsr & ED_RSR_FO) sc->mibdata.dot3StatsInternalMacReceiveErrors++; ifp->if_ierrors++; #ifdef ED_DEBUG printf("ed%d: receive error %x\n", ifp->if_unit, ed_nic_inb(sc, ED_P0_RSR)); #endif } /* * Go get the packet(s) XXX - Doing this on an * error is dubious because there shouldn't be * any data to get (we've configured the * interface to not accept packets with * errors). */ /* * Enable 16bit access to shared memory first * on WD/SMC boards. */ if (sc->isa16bit && (sc->vendor == ED_VENDOR_WD_SMC)) { ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto | ED_WD_LAAR_M16EN); if (sc->chip_type == ED_CHIP_TYPE_WD790) { ed_asic_outb(sc, ED_WD_MSR, ED_WD_MSR_MENB); } } ed_rint(sc); /* disable 16bit access */ if (sc->isa16bit && (sc->vendor == ED_VENDOR_WD_SMC)) { if (sc->chip_type == ED_CHIP_TYPE_WD790) { ed_asic_outb(sc, ED_WD_MSR, 0x00); } ed_asic_outb(sc, ED_WD_LAAR, sc->wd_laar_proto & ~ED_WD_LAAR_M16EN); } } } /* * If it looks like the transmitter can take more data, * attempt to start output on the interface. This is done * after handling the receiver to give the receiver priority. */ if ((ifp->if_flags & IFF_OACTIVE) == 0) ed_start(ifp); /* * return NIC CR to standard state: page 0, remote DMA * complete, start (toggling the TXP bit off, even if was just * set in the transmit routine, is *okay* - it is 'edge' * triggered from low to high) */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA); /* * If the Network Talley Counters overflow, read them to reset * them. It appears that old 8390's won't clear the ISR flag * otherwise - resulting in an infinite loop. */ if (isr & ED_ISR_CNT) { (void) ed_nic_inb(sc, ED_P0_CNTR0); (void) ed_nic_inb(sc, ED_P0_CNTR1); (void) ed_nic_inb(sc, ED_P0_CNTR2); } } } /* * Process an ioctl request. This code needs some work - it looks * pretty ugly. */ static int ed_ioctl(ifp, command, data) register struct ifnet *ifp; u_long command; caddr_t data; { struct ed_softc *sc = ifp->if_softc; int s, error = 0; if (sc == NULL || sc->gone) { ifp->if_flags &= ~IFF_RUNNING; return ENXIO; } s = splimp(); switch (command) { case SIOCSIFADDR: case SIOCGIFADDR: case SIOCSIFMTU: error = ether_ioctl(ifp, command, data); break; case SIOCSIFFLAGS: /* * If the interface is marked up and stopped, then start it. * If it is marked down and running, then stop it. */ if (ifp->if_flags & IFF_UP) { if ((ifp->if_flags & IFF_RUNNING) == 0) ed_init(sc); } else { if (ifp->if_flags & IFF_RUNNING) { ed_stop(sc); ifp->if_flags &= ~IFF_RUNNING; } } /* * Promiscuous flag may have changed, so reprogram the RCR. */ ed_setrcr(sc); /* * An unfortunate hack to provide the (required) software * control of the tranceiver for 3Com boards. The ALTPHYS flag * disables the tranceiver if set. */ if (sc->vendor == ED_VENDOR_3COM) { if (ifp->if_flags & IFF_ALTPHYS) { ed_asic_outb(sc, ED_3COM_CR, 0); } else { ed_asic_outb(sc, ED_3COM_CR, ED_3COM_CR_XSEL); } } else if (sc->vendor == ED_VENDOR_HP) ed_hpp_set_physical_link(sc); break; case SIOCADDMULTI: case SIOCDELMULTI: /* * Multicast list has changed; set the hardware filter * accordingly. */ ed_setrcr(sc); error = 0; break; default: error = EINVAL; } (void) splx(s); return (error); } /* * Given a source and destination address, copy 'amount' of a packet from * the ring buffer into a linear destination buffer. Takes into account * ring-wrap. */ static __inline char * ed_ring_copy(sc, src, dst, amount) struct ed_softc *sc; char *src; char *dst; u_short amount; { u_short tmp_amount; /* does copy wrap to lower addr in ring buffer? */ if (src + amount > sc->mem_end) { tmp_amount = sc->mem_end - src; /* copy amount up to end of NIC memory */ if (sc->mem_shared) bcopy(src, dst, tmp_amount); else ed_pio_readmem(sc, (int)src, dst, tmp_amount); amount -= tmp_amount; src = sc->mem_ring; dst += tmp_amount; } if (sc->mem_shared) bcopy(src, dst, amount); else ed_pio_readmem(sc, (int)src, dst, amount); return (src + amount); } /* * Retreive packet from shared memory and send to the next level up via * ether_input(). */ static void ed_get_packet(sc, buf, len) struct ed_softc *sc; char *buf; u_short len; { struct ether_header *eh; struct mbuf *m; /* Allocate a header mbuf */ MGETHDR(m, M_DONTWAIT, MT_DATA); if (m == NULL) return; m->m_pkthdr.rcvif = &sc->arpcom.ac_if; m->m_pkthdr.len = m->m_len = len; /* * We always put the received packet in a single buffer - * either with just an mbuf header or in a cluster attached * to the header. The +2 is to compensate for the alignment * fixup below. */ if ((len + 2) > MHLEN) { /* Attach an mbuf cluster */ MCLGET(m, M_DONTWAIT); /* Insist on getting a cluster */ if ((m->m_flags & M_EXT) == 0) { m_freem(m); return; } } /* * The +2 is to longword align the start of the real packet. * This is important for NFS. */ m->m_data += 2; eh = mtod(m, struct ether_header *); #ifdef BRIDGE /* * Don't read in the entire packet if we know we're going to drop it */ if (do_bridge) { struct ifnet *bif; ed_ring_copy(sc, buf, (char *)eh, ETHER_HDR_LEN); if ((bif = bridge_in(&sc->arpcom.ac_if, eh)) == BDG_DROP) { m_freem(m); return; } ed_ring_copy(sc, buf + ETHER_HDR_LEN, (char *)eh + ETHER_HDR_LEN, len - ETHER_HDR_LEN); } else #endif /* * Get packet, including link layer address, from interface. */ ed_ring_copy(sc, buf, (char *)eh, len); /* * Remove link layer address. */ m->m_pkthdr.len = m->m_len = len - sizeof(struct ether_header); m->m_data += sizeof(struct ether_header); ether_input(&sc->arpcom.ac_if, eh, m); } /* * Supporting routines */ /* * Given a NIC memory source address and a host memory destination * address, copy 'amount' from NIC to host using Programmed I/O. * The 'amount' is rounded up to a word - okay as long as mbufs * are word sized. * This routine is currently Novell-specific. */ void ed_pio_readmem(sc, src, dst, amount) struct ed_softc *sc; int src; unsigned char *dst; unsigned short amount; { /* HP PC Lan+ cards need special handling */ if (sc->vendor == ED_VENDOR_HP && sc->type == ED_TYPE_HP_PCLANPLUS) { ed_hpp_readmem(sc, src, dst, amount); return; } /* Regular Novell cards */ /* select page 0 registers */ ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA); /* round up to a word */ if (amount & 1) ++amount; /* set up DMA byte count */ ed_nic_outb(sc, ED_P0_RBCR0, amount); ed_nic_outb(sc, ED_P0_RBCR1, amount >> 8); /* set up source address in NIC mem */ ed_nic_outb(sc, ED_P0_RSAR0, src); ed_nic_outb(sc, ED_P0_RSAR1, src >> 8); ed_nic_outb(sc, ED_P0_CR, ED_CR_RD0 | ED_CR_STA); if (sc->isa16bit) { ed_asic_insw(sc, ED_NOVELL_DATA, dst, amount / 2); } else { ed_asic_insb(sc, ED_NOVELL_DATA, dst, amount); } } /* * Stripped down routine for writing a linear buffer to NIC memory. * Only used in the probe routine to test the memory. 'len' must * be even. */ void ed_pio_writemem(sc, src, dst, len) struct ed_softc *sc; char *src; unsigned short dst; unsigned short len; { int maxwait = 200; /* about 240us */ /* select page 0 registers */ ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA); /* reset remote DMA complete flag */ ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC); /* set up DMA byte count */ ed_nic_outb(sc, ED_P0_RBCR0, len); ed_nic_outb(sc, ED_P0_RBCR1, len >> 8); /* set up destination address in NIC mem */ ed_nic_outb(sc, ED_P0_RSAR0, dst); ed_nic_outb(sc, ED_P0_RSAR1, dst >> 8); /* set remote DMA write */ ed_nic_outb(sc, ED_P0_CR, ED_CR_RD1 | ED_CR_STA); if (sc->isa16bit) { ed_asic_outsw(sc, ED_NOVELL_DATA, src, len / 2); } else { ed_asic_outsb(sc, ED_NOVELL_DATA, src, len); } /* * Wait for remote DMA complete. This is necessary because on the * transmit side, data is handled internally by the NIC in bursts and * we can't start another remote DMA until this one completes. Not * waiting causes really bad things to happen - like the NIC * irrecoverably jamming the ISA bus. */ while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) && --maxwait); } /* * Write an mbuf chain to the destination NIC memory address using * programmed I/O. */ static u_short ed_pio_write_mbufs(sc, m, dst) struct ed_softc *sc; struct mbuf *m; int dst; { struct ifnet *ifp = (struct ifnet *)sc; unsigned short total_len, dma_len; struct mbuf *mp; int maxwait = 200; /* about 240us */ /* HP PC Lan+ cards need special handling */ if (sc->vendor == ED_VENDOR_HP && sc->type == ED_TYPE_HP_PCLANPLUS) { return ed_hpp_write_mbufs(sc, m, dst); } /* Regular Novell cards */ /* First, count up the total number of bytes to copy */ for (total_len = 0, mp = m; mp; mp = mp->m_next) total_len += mp->m_len; dma_len = total_len; if (sc->isa16bit && (dma_len & 1)) dma_len++; /* select page 0 registers */ ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STA); /* reset remote DMA complete flag */ ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC); /* set up DMA byte count */ ed_nic_outb(sc, ED_P0_RBCR0, dma_len); ed_nic_outb(sc, ED_P0_RBCR1, dma_len >> 8); /* set up destination address in NIC mem */ ed_nic_outb(sc, ED_P0_RSAR0, dst); ed_nic_outb(sc, ED_P0_RSAR1, dst >> 8); /* set remote DMA write */ ed_nic_outb(sc, ED_P0_CR, ED_CR_RD1 | ED_CR_STA); /* * Transfer the mbuf chain to the NIC memory. * 16-bit cards require that data be transferred as words, and only words. * So that case requires some extra code to patch over odd-length mbufs. */ if (!sc->isa16bit) { /* NE1000s are easy */ while (m) { if (m->m_len) { ed_asic_outsb(sc, ED_NOVELL_DATA, m->m_data, m->m_len); } m = m->m_next; } } else { /* NE2000s are a pain */ unsigned char *data; int len, wantbyte; unsigned char savebyte[2]; wantbyte = 0; while (m) { len = m->m_len; if (len) { data = mtod(m, caddr_t); /* finish the last word */ if (wantbyte) { savebyte[1] = *data; ed_asic_outw(sc, ED_NOVELL_DATA, *(u_short *)savebyte); data++; len--; wantbyte = 0; } /* output contiguous words */ if (len > 1) { ed_asic_outsw(sc, ED_NOVELL_DATA, data, len >> 1); data += len & ~1; len &= 1; } /* save last byte, if necessary */ if (len == 1) { savebyte[0] = *data; wantbyte = 1; } } m = m->m_next; } /* spit last byte */ if (wantbyte) { ed_asic_outw(sc, ED_NOVELL_DATA, *(u_short *)savebyte); } } /* * Wait for remote DMA complete. This is necessary because on the * transmit side, data is handled internally by the NIC in bursts and * we can't start another remote DMA until this one completes. Not * waiting causes really bad things to happen - like the NIC * irrecoverably jamming the ISA bus. */ while (((ed_nic_inb(sc, ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) && --maxwait); if (!maxwait) { log(LOG_WARNING, "ed%d: remote transmit DMA failed to complete\n", ifp->if_unit); ed_reset(ifp); return(0); } return (total_len); } /* * Support routines to handle the HP PC Lan+ card. */ /* * HP PC Lan+: Read from NIC memory, using either PIO or memory mapped * IO. */ static void ed_hpp_readmem(sc, src, dst, amount) struct ed_softc *sc; unsigned short src; unsigned char *dst; unsigned short amount; { int use_32bit_access = !(sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS); /* Program the source address in RAM */ ed_asic_outw(sc, ED_HPP_PAGE_2, src); /* * The HP PC Lan+ card supports word reads as well as * a memory mapped i/o port that is aliased to every * even address on the board. */ if (sc->hpp_mem_start) { /* Enable memory mapped access. */ ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options & ~(ED_HPP_OPTION_MEM_DISABLE | ED_HPP_OPTION_BOOT_ROM_ENB)); if (use_32bit_access && (amount > 3)) { u_int32_t *dl = (u_int32_t *) dst; volatile u_int32_t *const sl = (u_int32_t *) sc->hpp_mem_start; u_int32_t *const fence = dl + (amount >> 2); /* Copy out NIC data. We could probably write this as a `movsl'. The currently generated code is lousy. */ while (dl < fence) *dl++ = *sl; dst += (amount & ~3); amount &= 3; } /* Finish off any words left, as a series of short reads */ if (amount > 1) { u_short *d = (u_short *) dst; volatile u_short *const s = (u_short *) sc->hpp_mem_start; u_short *const fence = d + (amount >> 1); /* Copy out NIC data. */ while (d < fence) *d++ = *s; dst += (amount & ~1); amount &= 1; } /* * read in a byte; however we need to always read 16 bits * at a time or the hardware gets into a funny state */ if (amount == 1) { /* need to read in a short and copy LSB */ volatile u_short *const s = (volatile u_short *) sc->hpp_mem_start; *dst = (*s) & 0xFF; } /* Restore Boot ROM access. */ ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options); } else { /* Read in data using the I/O port */ if (use_32bit_access && (amount > 3)) { ed_asic_insl(sc, ED_HPP_PAGE_4, dst, amount >> 2); dst += (amount & ~3); amount &= 3; } if (amount > 1) { ed_asic_insw(sc, ED_HPP_PAGE_4, dst, amount >> 1); dst += (amount & ~1); amount &= 1; } if (amount == 1) { /* read in a short and keep the LSB */ *dst = ed_asic_inw(sc, ED_HPP_PAGE_4) & 0xFF; } } } /* * HP PC Lan+: Write to NIC memory, using either PIO or memory mapped * IO. * Only used in the probe routine to test the memory. 'len' must * be even. */ void ed_hpp_writemem(sc, src, dst, len) struct ed_softc *sc; unsigned char *src; unsigned short dst; unsigned short len; { /* reset remote DMA complete flag */ ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC); /* program the write address in RAM */ ed_asic_outw(sc, ED_HPP_PAGE_0, dst); if (sc->hpp_mem_start) { u_short *s = (u_short *) src; volatile u_short *d = (u_short *) sc->hpp_mem_start; u_short *const fence = s + (len >> 1); /* * Enable memory mapped access. */ ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options & ~(ED_HPP_OPTION_MEM_DISABLE | ED_HPP_OPTION_BOOT_ROM_ENB)); /* * Copy to NIC memory. */ while (s < fence) *d = *s++; /* * Restore Boot ROM access. */ ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options); } else { /* write data using I/O writes */ ed_asic_outsw(sc, ED_HPP_PAGE_4, src, len / 2); } } /* * Write to HP PC Lan+ NIC memory. Access to the NIC can be by using * outsw() or via the memory mapped interface to the same register. * Writes have to be in word units; byte accesses won't work and may cause - * the NIC to behave wierdly. Long word accesses are permitted if the ASIC + * the NIC to behave weirdly. Long word accesses are permitted if the ASIC * allows it. */ static u_short ed_hpp_write_mbufs(struct ed_softc *sc, struct mbuf *m, int dst) { int len, wantbyte; unsigned short total_len; unsigned char savebyte[2]; volatile u_short * const d = (volatile u_short *) sc->hpp_mem_start; int use_32bit_accesses = !(sc->hpp_id & ED_HPP_ID_16_BIT_ACCESS); /* select page 0 registers */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA); /* reset remote DMA complete flag */ ed_nic_outb(sc, ED_P0_ISR, ED_ISR_RDC); /* program the write address in RAM */ ed_asic_outw(sc, ED_HPP_PAGE_0, dst); if (sc->hpp_mem_start) /* enable memory mapped I/O */ ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options & ~(ED_HPP_OPTION_MEM_DISABLE | ED_HPP_OPTION_BOOT_ROM_ENB)); wantbyte = 0; total_len = 0; if (sc->hpp_mem_start) { /* Memory mapped I/O port */ while (m) { total_len += (len = m->m_len); if (len) { caddr_t data = mtod(m, caddr_t); /* finish the last word of the previous mbuf */ if (wantbyte) { savebyte[1] = *data; *d = *((ushort *) savebyte); data++; len--; wantbyte = 0; } /* output contiguous words */ if ((len > 3) && (use_32bit_accesses)) { volatile u_int32_t *const dl = (volatile u_int32_t *) d; u_int32_t *sl = (u_int32_t *) data; u_int32_t *fence = sl + (len >> 2); while (sl < fence) *dl = *sl++; data += (len & ~3); len &= 3; } /* finish off remain 16 bit writes */ if (len > 1) { u_short *s = (u_short *) data; u_short *fence = s + (len >> 1); while (s < fence) *d = *s++; data += (len & ~1); len &= 1; } /* save last byte if needed */ if ((wantbyte = (len == 1)) != 0) savebyte[0] = *data; } m = m->m_next; /* to next mbuf */ } if (wantbyte) /* write last byte */ *d = *((u_short *) savebyte); } else { /* use programmed I/O */ while (m) { total_len += (len = m->m_len); if (len) { caddr_t data = mtod(m, caddr_t); /* finish the last word of the previous mbuf */ if (wantbyte) { savebyte[1] = *data; ed_asic_outw(sc, ED_HPP_PAGE_4, *((u_short *)savebyte)); data++; len--; wantbyte = 0; } /* output contiguous words */ if ((len > 3) && use_32bit_accesses) { ed_asic_outsl(sc, ED_HPP_PAGE_4, data, len >> 2); data += (len & ~3); len &= 3; } /* finish off remaining 16 bit accesses */ if (len > 1) { ed_asic_outsw(sc, ED_HPP_PAGE_4, data, len >> 1); data += (len & ~1); len &= 1; } if ((wantbyte = (len == 1)) != 0) savebyte[0] = *data; } /* if len != 0 */ m = m->m_next; } if (wantbyte) /* spit last byte */ ed_asic_outw(sc, ED_HPP_PAGE_4, *(u_short *)savebyte); } if (sc->hpp_mem_start) /* turn off memory mapped i/o */ ed_asic_outw(sc, ED_HPP_OPTION, sc->hpp_options); return (total_len); } static void ed_setrcr(sc) struct ed_softc *sc; { struct ifnet *ifp = (struct ifnet *)sc; int i; u_char reg1; /* Bit 6 in AX88190 RCR register must be set. */ if (sc->chip_type == ED_CHIP_TYPE_AX88190) reg1 = ED_RCR_INTT; else reg1 = 0x00; /* set page 1 registers */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP); if (ifp->if_flags & IFF_PROMISC) { /* * Reconfigure the multicast filter. */ for (i = 0; i < 8; i++) ed_nic_outb(sc, ED_P1_MAR(i), 0xff); /* * And turn on promiscuous mode. Also enable reception of * runts and packets with CRC & alignment errors. */ /* Set page 0 registers */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP); ed_nic_outb(sc, ED_P0_RCR, ED_RCR_PRO | ED_RCR_AM | ED_RCR_AB | ED_RCR_AR | ED_RCR_SEP | reg1); } else { /* set up multicast addresses and filter modes */ if (ifp->if_flags & IFF_MULTICAST) { u_int32_t mcaf[2]; if (ifp->if_flags & IFF_ALLMULTI) { mcaf[0] = 0xffffffff; mcaf[1] = 0xffffffff; } else ds_getmcaf(sc, mcaf); /* * Set multicast filter on chip. */ for (i = 0; i < 8; i++) ed_nic_outb(sc, ED_P1_MAR(i), ((u_char *) mcaf)[i]); /* Set page 0 registers */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP); ed_nic_outb(sc, ED_P0_RCR, ED_RCR_AM | ED_RCR_AB | reg1); } else { /* * Initialize multicast address hashing registers to * not accept multicasts. */ for (i = 0; i < 8; ++i) ed_nic_outb(sc, ED_P1_MAR(i), 0x00); /* Set page 0 registers */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STP); ed_nic_outb(sc, ED_P0_RCR, ED_RCR_AB | reg1); } } /* * Start interface. */ ed_nic_outb(sc, ED_P0_CR, sc->cr_proto | ED_CR_STA); } /* * Compute crc for ethernet address */ static u_int32_t ds_crc(ep) u_char *ep; { #define POLYNOMIAL 0x04c11db6 register u_int32_t crc = 0xffffffff; register int carry, i, j; register u_char b; for (i = 6; --i >= 0;) { b = *ep++; for (j = 8; --j >= 0;) { carry = ((crc & 0x80000000) ? 1 : 0) ^ (b & 0x01); crc <<= 1; b >>= 1; if (carry) crc = (crc ^ POLYNOMIAL) | carry; } } return crc; #undef POLYNOMIAL } /* * Compute the multicast address filter from the * list of multicast addresses we need to listen to. */ static void ds_getmcaf(sc, mcaf) struct ed_softc *sc; u_int32_t *mcaf; { register u_int32_t index; register u_char *af = (u_char *) mcaf; struct ifmultiaddr *ifma; mcaf[0] = 0; mcaf[1] = 0; LIST_FOREACH(ifma, &sc->arpcom.ac_if.if_multiaddrs, ifma_link) { if (ifma->ifma_addr->sa_family != AF_LINK) continue; index = ds_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)) >> 26; af[index >> 3] |= 1 << (index & 7); } } diff --git a/sys/dev/isp/isp_target.c b/sys/dev/isp/isp_target.c index 8d7a9d55ffe8..b0f7245db01f 100644 --- a/sys/dev/isp/isp_target.c +++ b/sys/dev/isp/isp_target.c @@ -1,1225 +1,1225 @@ /* $FreeBSD$ */ /* * Machine and OS Independent Target Mode Code for the Qlogic SCSI/FC adapters. * * Copyright (c) 1999, 2000 by Matthew Jacob * All rights reserved. * mjacob@feral.com * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice immediately at the beginning of the file, without modification, * this list of conditions, and the following disclaimer. * 2. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Include header file appropriate for platform we're building on. */ #ifdef __NetBSD__ #include #endif #ifdef __FreeBSD__ #include #endif #ifdef __OpenBSD__ #include #endif #ifdef __linux__ #include "isp_linux.h" #endif #ifdef ISP_TARGET_MODE static char *atiocope = "ATIO returned for lun %d because it was in the middle of Bus Device Reset"; static char *atior = "ATIO returned for lun %d from initiator %d because a Bus Reset occurred"; static void isp_got_msg __P((struct ispsoftc *, int, in_entry_t *)); static void isp_got_msg_fc __P((struct ispsoftc *, int, in_fcentry_t *)); static void isp_notify_ack __P((struct ispsoftc *, void *)); static void isp_handle_atio(struct ispsoftc *, at_entry_t *); static void isp_handle_atio2(struct ispsoftc *, at2_entry_t *); static void isp_handle_ctio(struct ispsoftc *, ct_entry_t *); static void isp_handle_ctio2(struct ispsoftc *, ct2_entry_t *); /* * The Qlogic driver gets an interrupt to look at response queue entries. * Some of these are status completions for initiatior mode commands, but * if target mode is enabled, we get a whole wad of response queue entries * to be handled here. * * Basically the split into 3 main groups: Lun Enable/Modification responses, * SCSI Command processing, and Immediate Notification events. * * You start by writing a request queue entry to enable target mode (and * establish some resource limitations which you can modify later). * The f/w responds with a LUN ENABLE or LUN MODIFY response with * the status of this action. If the enable was successful, you can expect... * * Response queue entries with SCSI commands encapsulate show up in an ATIO * (Accept Target IO) type- sometimes with enough info to stop the command at * this level. Ultimately the driver has to feed back to the f/w's request * queue a sequence of CTIOs (continue target I/O) that describe data to * be moved and/or status to be sent) and finally finishing with sending * to the f/w's response queue an ATIO which then completes the handshake * with the f/w for that command. There's a lot of variations on this theme, * including flags you can set in the CTIO for the Qlogic 2X00 fibre channel * cards that 'auto-replenish' the f/w's ATIO count, but this is the basic * gist of it. * * The third group that can show up in the response queue are Immediate * Notification events. These include things like notifications of SCSI bus * resets, or Bus Device Reset messages or other messages received. This - * a classic oddbins area. It can get a little wierd because you then turn + * a classic oddbins area. It can get a little weird because you then turn * around and acknowledge the Immediate Notify by writing an entry onto the * request queue and then the f/w turns around and gives you an acknowledgement * to *your* acknowledgement on the response queue (the idea being to let * the f/w tell you when the event is *really* over I guess). * */ /* * A new response queue entry has arrived. The interrupt service code * has already swizzled it into the platform dependent from canonical form. * * Because of the way this driver is designed, unfortunately most of the * actual synchronization work has to be done in the platform specific * code- we have no synchroniation primitives in the common code. */ int isp_target_notify(isp, vptr, optrp) struct ispsoftc *isp; void *vptr; u_int16_t *optrp; { u_int16_t status, seqid; union { at_entry_t *atiop; at2_entry_t *at2iop; ct_entry_t *ctiop; ct2_entry_t *ct2iop; lun_entry_t *lunenp; in_entry_t *inotp; in_fcentry_t *inot_fcp; na_entry_t *nackp; na_fcentry_t *nack_fcp; isphdr_t *hp; void * *vp; #define atiop unp.atiop #define at2iop unp.at2iop #define ctiop unp.ctiop #define ct2iop unp.ct2iop #define lunenp unp.lunenp #define inotp unp.inotp #define inot_fcp unp.inot_fcp #define nackp unp.nackp #define nack_fcp unp.nack_fcp #define hdrp unp.hp } unp; int bus, rval = 0; unp.vp = vptr; ISP_TDQE(isp, "isp_target_notify", (int) *optrp, vptr); switch(hdrp->rqs_entry_type) { case RQSTYPE_ATIO: isp_handle_atio(isp, atiop); break; case RQSTYPE_CTIO: isp_handle_ctio(isp, ctiop); break; case RQSTYPE_ATIO2: isp_handle_atio2(isp, at2iop); break; case RQSTYPE_CTIO2: isp_handle_ctio2(isp, ct2iop); break; case RQSTYPE_ENABLE_LUN: case RQSTYPE_MODIFY_LUN: (void) isp_async(isp, ISPASYNC_TARGET_ACTION, vptr); break; case RQSTYPE_NOTIFY: /* * Either the ISP received a SCSI message it can't * handle, or it's returning an Immed. Notify entry * we sent. We can send Immed. Notify entries to * increment the firmware's resource count for them * (we set this initially in the Enable Lun entry). */ bus = 0; if (IS_FC(isp)) { status = inot_fcp->in_status; seqid = inot_fcp->in_seqid; } else { status = inotp->in_status & 0xff; seqid = inotp->in_seqid; if (IS_DUALBUS(isp)) { bus = (inotp->in_iid & 0x80) >> 7; inotp->in_iid &= ~0x80; } } isp_prt(isp, ISP_LOGTDEBUG1, "Immediate Notify, status=0x%x seqid=0x%x", status, seqid); switch (status) { case IN_RESET: (void) isp_async(isp, ISPASYNC_BUS_RESET, &bus); break; case IN_MSG_RECEIVED: case IN_IDE_RECEIVED: if (IS_FC(isp)) { isp_got_msg_fc(isp, bus, vptr); } else { isp_got_msg(isp, bus, vptr); } break; case IN_RSRC_UNAVAIL: isp_prt(isp, ISP_LOGWARN, "Firmware out of ATIOs"); break; case IN_ABORT_TASK: isp_prt(isp, ISP_LOGWARN, "Abort Task for Initiator %d RX_ID 0x%x", inot_fcp->in_iid, seqid); break; case IN_PORT_LOGOUT: isp_prt(isp, ISP_LOGWARN, "Port Logout for Initiator %d RX_ID 0x%x", inot_fcp->in_iid, seqid); break; case IN_PORT_CHANGED: isp_prt(isp, ISP_LOGWARN, "Port Changed for Initiator %d RX_ID 0x%x", inot_fcp->in_iid, seqid); break; case IN_GLOBAL_LOGO: isp_prt(isp, ISP_LOGWARN, "All ports logged out"); break; default: isp_prt(isp, ISP_LOGERR, "bad status (0x%x) in isp_target_notify", status); break; } isp_notify_ack(isp, vptr); break; case RQSTYPE_NOTIFY_ACK: /* * The ISP is acknowledging our acknowledgement of an * Immediate Notify entry for some asynchronous event. */ if (IS_FC(isp)) { isp_prt(isp, ISP_LOGTDEBUG1, "Notify Ack status=0x%x seqid 0x%x", nack_fcp->na_status, nack_fcp->na_seqid); } else { isp_prt(isp, ISP_LOGTDEBUG1, "Notify Ack event 0x%x status=0x%x seqid 0x%x", nackp->na_event, nackp->na_status, nackp->na_seqid); } break; default: isp_prt(isp, ISP_LOGERR, "Unknown entry type 0x%x in isp_target_notify", hdrp->rqs_entry_type); rval = -1; break; } #undef atiop #undef at2iop #undef ctiop #undef ct2iop #undef lunenp #undef inotp #undef inot_fcp #undef nackp #undef nack_fcp #undef hdrp return (rval); } /* * Toggle (on/off) target mode for bus/target/lun * * The caller has checked for overlap and legality. * * Note that not all of bus, target or lun can be paid attention to. * Note also that this action will not be complete until the f/w writes * response entry. The caller is responsible for synchronizing this. */ int isp_lun_cmd(isp, cmd, bus, tgt, lun, opaque) struct ispsoftc *isp; int cmd; int bus; int tgt; int lun; u_int32_t opaque; { lun_entry_t el; u_int16_t iptr, optr; void *outp; MEMZERO(&el, sizeof (el)); if (IS_DUALBUS(isp)) { el.le_rsvd = (bus & 0x1) << 7; } el.le_cmd_count = DFLT_CMD_CNT; el.le_in_count = DFLT_INOTIFY; if (cmd == RQSTYPE_ENABLE_LUN) { if (IS_SCSI(isp)) { el.le_flags = LUN_TQAE|LUN_DISAD; el.le_cdb6len = 12; el.le_cdb7len = 12; } } else if (cmd == -RQSTYPE_ENABLE_LUN) { cmd = RQSTYPE_ENABLE_LUN; el.le_cmd_count = 0; el.le_in_count = 0; } else if (cmd == -RQSTYPE_MODIFY_LUN) { cmd = RQSTYPE_MODIFY_LUN; el.le_ops = LUN_CCDECR | LUN_INDECR; } else { el.le_ops = LUN_CCINCR | LUN_ININCR; } el.le_header.rqs_entry_type = cmd; el.le_header.rqs_entry_count = 1; el.le_reserved = opaque; if (IS_SCSI(isp)) { el.le_tgt = tgt; el.le_lun = lun; } else if (isp->isp_maxluns <= 16) { el.le_lun = lun; } if (isp_getrqentry(isp, &iptr, &optr, &outp)) { isp_prt(isp, ISP_LOGWARN, "Request Queue Overflow in isp_lun_cmd"); return (-1); } ISP_SWIZ_ENABLE_LUN(isp, outp, &el); ISP_TDQE(isp, "isp_lun_cmd", (int) optr, &el); ISP_ADD_REQUEST(isp, iptr); return (0); } int isp_target_put_entry(isp, ap) struct ispsoftc *isp; void *ap; { void *outp; u_int16_t iptr, optr; u_int8_t etype = ((isphdr_t *) ap)->rqs_entry_type; if (isp_getrqentry(isp, &iptr, &optr, &outp)) { isp_prt(isp, ISP_LOGWARN, "Request Queue Overflow in isp_target_put_entry"); return (-1); } switch (etype) { case RQSTYPE_ATIO: ISP_SWIZ_ATIO(isp, outp, ap); break; case RQSTYPE_ATIO2: ISP_SWIZ_ATIO2(isp, outp, ap); break; case RQSTYPE_CTIO: ISP_SWIZ_CTIO(isp, outp, ap); break; case RQSTYPE_CTIO2: ISP_SWIZ_CTIO2(isp, outp, ap); break; default: isp_prt(isp, ISP_LOGERR, "Unknown type 0x%x in isp_put_entry", etype); return (-1); } ISP_TDQE(isp, "isp_target_put_entry", (int) optr, ap);; ISP_ADD_REQUEST(isp, iptr); return (0); } int isp_target_put_atio(isp, iid, tgt, lun, ttype, tval) struct ispsoftc *isp; int iid; int tgt; int lun; int ttype; int tval; { union { at_entry_t _atio; at2_entry_t _atio2; } atun; MEMZERO(&atun, sizeof atun); if (IS_FC(isp)) { atun._atio2.at_header.rqs_entry_type = RQSTYPE_ATIO2; atun._atio2.at_header.rqs_entry_count = 1; if (isp->isp_maxluns > 16) { atun._atio2.at_scclun = (u_int16_t) lun; } else { atun._atio2.at_lun = (u_int8_t) lun; } atun._atio2.at_status = CT_OK; } else { atun._atio.at_header.rqs_entry_type = RQSTYPE_ATIO; atun._atio.at_header.rqs_entry_count = 1; atun._atio.at_iid = iid; atun._atio.at_tgt = tgt; atun._atio.at_lun = lun; atun._atio.at_tag_type = ttype; atun._atio.at_tag_val = tval; atun._atio.at_status = CT_OK; } return (isp_target_put_entry(isp, &atun)); } /* * Command completion- both for handling cases of no resources or * no blackhole driver, or other cases where we have to, inline, * finish the command sanely, or for normal command completion. * * The 'completion' code value has the scsi status byte in the low 8 bits. * If status is a CHECK CONDITION and bit 8 is nonzero, then bits 12..15 have * the sense key and bits 16..23 have the ASCQ and bits 24..31 have the ASC * values. * * NB: the key, asc, ascq, cannot be used for parallel SCSI as it doesn't * NB: inline SCSI sense reporting. * * For both parallel && fibre channel, we use the feature that does * an automatic resource autoreplenish so we don't have then later do * put of an atio to replenish the f/w's resource count. */ int isp_endcmd(struct ispsoftc *isp, void *arg, u_int32_t code, u_int32_t hdl) { int sts; union { ct_entry_t _ctio; ct2_entry_t _ctio2; } un; MEMZERO(&un, sizeof un); sts = code & 0xff; if (IS_FC(isp)) { at2_entry_t *aep = arg; ct2_entry_t *cto = &un._ctio2; cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2; cto->ct_header.rqs_entry_count = 1; cto->ct_iid = aep->at_iid; if (isp->isp_maxluns <= 16) { cto->ct_lun = aep->at_lun; } cto->ct_rxid = aep->at_rxid; cto->rsp.m1.ct_scsi_status = sts & 0xff; cto->ct_flags = CT2_SENDSTATUS | CT2_NO_DATA | CT2_FLAG_MODE1; if (hdl == 0) { cto->ct_flags |= CT2_CCINCR; } if (aep->at_datalen) { cto->ct_resid = aep->at_datalen; cto->ct_flags |= CT2_DATA_UNDER; } if ((sts & 0xff) == SCSI_CHECK && (sts & ECMD_SVALID)) { cto->rsp.m1.ct_resp[0] = 0xf0; cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf; cto->rsp.m1.ct_resp[7] = 8; cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff; cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff; cto->rsp.m1.ct_senselen = 16; cto->ct_flags |= CT2_SNSLEN_VALID; } cto->ct_reserved = hdl; } else { at_entry_t *aep = arg; ct_entry_t *cto = &un._ctio; cto->ct_header.rqs_entry_type = RQSTYPE_CTIO; cto->ct_header.rqs_entry_count = 1; cto->ct_iid = aep->at_iid; cto->ct_tgt = aep->at_tgt; cto->ct_lun = aep->at_lun; cto->ct_tag_type = aep->at_tag_type; cto->ct_tag_val = aep->at_tag_val; cto->ct_flags = CT_SENDSTATUS | CT_NO_DATA; if (hdl == 0) { cto->ct_flags |= CT_CCINCR; } cto->ct_scsi_status = sts; cto->ct_reserved = hdl; } return (isp_target_put_entry(isp, &un)); } void isp_target_async(isp, bus, event) struct ispsoftc *isp; int bus; int event; { tmd_event_t evt; tmd_msg_t msg; switch (event) { /* * These three we handle here to propagate an effective bus reset * upstream, but these do not require any immediate notify actions * so we return when done. */ case ASYNC_LIP_OCCURRED: case ASYNC_LOOP_UP: case ASYNC_LOOP_DOWN: evt.ev_bus = bus; evt.ev_event = event; (void) isp_async(isp, ISPASYNC_TARGET_EVENT, &evt); return; case ASYNC_LOOP_RESET: case ASYNC_BUS_RESET: case ASYNC_TIMEOUT_RESET: if (IS_FC(isp)) { return; /* we'll be getting an inotify instead */ } evt.ev_bus = bus; evt.ev_event = event; (void) isp_async(isp, ISPASYNC_TARGET_EVENT, &evt); break; case ASYNC_DEVICE_RESET: /* * Bus Device Reset resets a specific target, so * we pass this as a synthesized message. */ MEMZERO(&msg, sizeof msg); if (IS_FC(isp)) { msg.nt_iid = FCPARAM(isp)->isp_loopid; } else { msg.nt_iid = SDPARAM(isp)->isp_initiator_id; } msg.nt_bus = bus; msg.nt_msg[0] = MSG_BUS_DEV_RESET; (void) isp_async(isp, ISPASYNC_TARGET_MESSAGE, &msg); break; default: isp_prt(isp, ISP_LOGERR, "isp_target_async: unknown event 0x%x", event); break; } if (isp->isp_state == ISP_RUNSTATE) isp_notify_ack(isp, NULL); } /* * Process a received message. * The ISP firmware can handle most messages, there are only * a few that we need to deal with: * - abort: clean up the current command * - abort tag and clear queue */ static void isp_got_msg(isp, bus, inp) struct ispsoftc *isp; int bus; in_entry_t *inp; { u_int8_t status = inp->in_status & ~QLTM_SVALID; if (status == IN_IDE_RECEIVED || status == IN_MSG_RECEIVED) { tmd_msg_t msg; MEMZERO(&msg, sizeof (msg)); msg.nt_bus = bus; msg.nt_iid = inp->in_iid; msg.nt_tgt = inp->in_tgt; msg.nt_lun = inp->in_lun; msg.nt_tagtype = inp->in_tag_type; msg.nt_tagval = inp->in_tag_val; MEMCPY(msg.nt_msg, inp->in_msg, IN_MSGLEN); (void) isp_async(isp, ISPASYNC_TARGET_MESSAGE, &msg); } else { isp_prt(isp, ISP_LOGERR, "unknown immediate notify status 0x%x", inp->in_status); } } /* * Synthesize a message from the task management flags in a FCP_CMND_IU. */ static void isp_got_msg_fc(isp, bus, inp) struct ispsoftc *isp; int bus; in_fcentry_t *inp; { static char *f1 = "%s from iid %d lun %d seq 0x%x"; static char *f2 = "unknown %s 0x%x lun %d iid %d task flags 0x%x seq 0x%x\n"; if (inp->in_status != IN_MSG_RECEIVED) { isp_prt(isp, ISP_LOGINFO, f2, "immediate notify status", inp->in_status, inp->in_lun, inp->in_iid, inp->in_task_flags, inp->in_seqid); } else { tmd_msg_t msg; MEMZERO(&msg, sizeof (msg)); msg.nt_bus = bus; msg.nt_iid = inp->in_iid; if (isp->isp_maxluns > 16) { msg.nt_lun = inp->in_scclun; } else { msg.nt_lun = inp->in_lun; } msg.nt_tagval = inp->in_seqid; if (inp->in_task_flags & TASK_FLAGS_ABORT_TASK) { isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK", inp->in_iid, inp->in_lun, inp->in_seqid); msg.nt_msg[0] = MSG_ABORT_TAG; } else if (inp->in_task_flags & TASK_FLAGS_CLEAR_TASK_SET) { isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", inp->in_iid, inp->in_lun, inp->in_seqid); msg.nt_msg[0] = MSG_CLEAR_QUEUE; } else if (inp->in_task_flags & TASK_FLAGS_TARGET_RESET) { isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", inp->in_iid, inp->in_lun, inp->in_seqid); msg.nt_msg[0] = MSG_BUS_DEV_RESET; } else if (inp->in_task_flags & TASK_FLAGS_CLEAR_ACA) { isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", inp->in_iid, inp->in_lun, inp->in_seqid); /* ???? */ msg.nt_msg[0] = MSG_REL_RECOVERY; } else if (inp->in_task_flags & TASK_FLAGS_TERMINATE_TASK) { isp_prt(isp, ISP_LOGINFO, f1, "TERMINATE TASK", inp->in_iid, inp->in_lun, inp->in_seqid); msg.nt_msg[0] = MSG_TERM_IO_PROC; } else { isp_prt(isp, ISP_LOGWARN, f2, "task flag", inp->in_status, inp->in_lun, inp->in_iid, inp->in_task_flags, inp->in_seqid); } if (msg.nt_msg[0]) { (void) isp_async(isp, ISPASYNC_TARGET_MESSAGE, &msg); } } } static void isp_notify_ack(isp, arg) struct ispsoftc *isp; void *arg; { char storage[QENTRY_LEN]; u_int16_t iptr, optr; void *outp; if (isp_getrqentry(isp, &iptr, &optr, &outp)) { isp_prt(isp, ISP_LOGWARN, "Request Queue Overflow For isp_notify_ack"); return; } MEMZERO(storage, QENTRY_LEN); if (IS_FC(isp)) { na_fcentry_t *na = (na_fcentry_t *) storage; if (arg) { in_fcentry_t *inp = arg; MEMCPY(storage, arg, sizeof (isphdr_t)); na->na_iid = inp->in_iid; if (isp->isp_maxluns > 16) { na->na_lun = inp->in_scclun; } else { na->na_lun = inp->in_lun; } na->na_task_flags = inp->in_task_flags; na->na_seqid = inp->in_seqid; na->na_flags = NAFC_RCOUNT; if (inp->in_status == IN_RESET) { na->na_flags |= NAFC_RST_CLRD; } } else { na->na_flags = NAFC_RST_CLRD; } na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK; na->na_header.rqs_entry_count = 1; ISP_SWIZ_NOT_ACK_FC(isp, outp, na); } else { na_entry_t *na = (na_entry_t *) storage; if (arg) { in_entry_t *inp = arg; MEMCPY(storage, arg, sizeof (isphdr_t)); na->na_iid = inp->in_iid; na->na_lun = inp->in_lun; na->na_tgt = inp->in_tgt; na->na_seqid = inp->in_seqid; if (inp->in_status == IN_RESET) { na->na_event = NA_RST_CLRD; } } else { na->na_event = NA_RST_CLRD; } na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK; na->na_header.rqs_entry_count = 1; ISP_SWIZ_NOT_ACK(isp, outp, na); } ISP_TDQE(isp, "isp_notify_ack", (int) optr, storage); ISP_ADD_REQUEST(isp, iptr); } static void isp_handle_atio(isp, aep) struct ispsoftc *isp; at_entry_t *aep; { int lun; lun = aep->at_lun; /* * The firmware status (except for the QLTM_SVALID bit) indicates * why this ATIO was sent to us. * * If QLTM_SVALID is set, the firware has recommended Sense Data. * * If the DISCONNECTS DISABLED bit is set in the flags field, * we're still connected on the SCSI bus - i.e. the initiator * did not set DiscPriv in the identify message. We don't care * about this so it's ignored. */ switch(aep->at_status & ~QLTM_SVALID) { case AT_PATH_INVALID: /* * ATIO rejected by the firmware due to disabled lun. */ isp_prt(isp, ISP_LOGERR, "rejected ATIO for disabled lun %d", lun); break; case AT_NOCAP: /* * Requested Capability not available * We sent an ATIO that overflowed the firmware's * command resource count. */ isp_prt(isp, ISP_LOGERR, "rejected ATIO for lun %d because of command count" " overflow", lun); break; case AT_BDR_MSG: /* * If we send an ATIO to the firmware to increment * its command resource count, and the firmware is * recovering from a Bus Device Reset, it returns * the ATIO with this status. We set the command * resource count in the Enable Lun entry and no * not increment it. Therefore we should never get * this status here. */ isp_prt(isp, ISP_LOGERR, atiocope, lun); break; case AT_CDB: /* Got a CDB */ case AT_PHASE_ERROR: /* Bus Phase Sequence Error */ /* * Punt to platform specific layer. */ (void) isp_async(isp, ISPASYNC_TARGET_ACTION, aep); break; case AT_RESET: /* * A bus reset came along an blew away this command. Why * they do this in addition the async event code stuff, * I dunno. * * Ignore it because the async event will clear things * up for us. */ isp_prt(isp, ISP_LOGWARN, atior, lun, aep->at_iid); break; default: isp_prt(isp, ISP_LOGERR, "Unknown ATIO status 0x%x from initiator %d for lun %d", aep->at_status, aep->at_iid, lun); (void) isp_target_put_atio(isp, aep->at_iid, aep->at_tgt, lun, aep->at_tag_type, aep->at_tag_val); break; } } static void isp_handle_atio2(isp, aep) struct ispsoftc *isp; at2_entry_t *aep; { int lun; if (isp->isp_maxluns > 16) { lun = aep->at_scclun; } else { lun = aep->at_lun; } /* * The firmware status (except for the QLTM_SVALID bit) indicates * why this ATIO was sent to us. * * If QLTM_SVALID is set, the firware has recommended Sense Data. * * If the DISCONNECTS DISABLED bit is set in the flags field, * we're still connected on the SCSI bus - i.e. the initiator * did not set DiscPriv in the identify message. We don't care * about this so it's ignored. */ switch(aep->at_status & ~QLTM_SVALID) { case AT_PATH_INVALID: /* * ATIO rejected by the firmware due to disabled lun. */ isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for disabled lun %d", lun); break; case AT_NOCAP: /* * Requested Capability not available * We sent an ATIO that overflowed the firmware's * command resource count. */ isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for lun %d- command count overflow", lun); break; case AT_BDR_MSG: /* * If we send an ATIO to the firmware to increment * its command resource count, and the firmware is * recovering from a Bus Device Reset, it returns * the ATIO with this status. We set the command * resource count in the Enable Lun entry and no * not increment it. Therefore we should never get * this status here. */ isp_prt(isp, ISP_LOGERR, atiocope, lun); break; case AT_CDB: /* Got a CDB */ /* * Punt to platform specific layer. */ (void) isp_async(isp, ISPASYNC_TARGET_ACTION, aep); break; case AT_RESET: /* * A bus reset came along an blew away this command. Why * they do this in addition the async event code stuff, * I dunno. * * Ignore it because the async event will clear things * up for us. */ isp_prt(isp, ISP_LOGERR, atior, lun, aep->at_iid); break; default: isp_prt(isp, ISP_LOGERR, "Unknown ATIO2 status 0x%x from initiator %d for lun %d", aep->at_status, aep->at_iid, lun); (void) isp_target_put_atio(isp, aep->at_iid, 0, lun, 0, 0); break; } } static void isp_handle_ctio(isp, ct) struct ispsoftc *isp; ct_entry_t *ct; { XS_T *xs; int pl = ISP_LOGTDEBUG2; char *fmsg = NULL; if (ct->ct_reserved) { xs = isp_find_xs(isp, ct->ct_reserved); if (xs == NULL) pl = ISP_LOGALL; } else { pl = ISP_LOGTDEBUG1; xs = NULL; } switch(ct->ct_status & ~QLTM_SVALID) { case CT_OK: /* * There are generally 3 possibilities as to why we'd get * this condition: * We disconnected after receiving a CDB. * We sent or received data. * We sent status & command complete. */ if (ct->ct_flags & CT_SENDSTATUS) { break; } else if ((ct->ct_flags & CT_DATAMASK) == CT_NO_DATA) { /* * Nothing to do in this case. */ isp_prt(isp, pl, "CTIO- iid %d disconnected OK", ct->ct_iid); return; } break; case CT_BDR_MSG: /* * Bus Device Reset message received or the SCSI Bus has * been Reset; the firmware has gone to Bus Free. * * The firmware generates an async mailbox interupt to * notify us of this and returns outstanding CTIOs with this * status. These CTIOs are handled in that same way as * CT_ABORTED ones, so just fall through here. */ fmsg = "Bus Device Reset"; /*FALLTHROUGH*/ case CT_RESET: if (fmsg == NULL) fmsg = "Bus Reset"; /*FALLTHROUGH*/ case CT_ABORTED: /* * When an Abort message is received the firmware goes to * Bus Free and returns all outstanding CTIOs with the status * set, then sends us an Immediate Notify entry. */ if (fmsg == NULL) fmsg = "ABORT TASK sent by Initiator"; isp_prt(isp, ISP_LOGWARN, "CTIO destroyed by %s", fmsg); break; case CT_INVAL: /* * CTIO rejected by the firmware due to disabled lun. * "Cannot Happen". */ isp_prt(isp, ISP_LOGERR, "Firmware rejected CTIO for disabled lun %d", ct->ct_lun); break; case CT_NOPATH: /* * CTIO rejected by the firmware due "no path for the * nondisconnecting nexus specified". This means that * we tried to access the bus while a non-disconnecting * command is in process. */ isp_prt(isp, ISP_LOGERR, "Firmware rejected CTIO for bad nexus %d/%d/%d", ct->ct_iid, ct->ct_tgt, ct->ct_lun); break; case CT_RSELTMO: fmsg = "Reselection"; /*FALLTHROUGH*/ case CT_TIMEOUT: if (fmsg == NULL) fmsg = "Command"; isp_prt(isp, ISP_LOGERR, "Firmware timed out on %s", fmsg); break; case CT_ERR: fmsg = "Completed with Error"; /*FALLTHROUGH*/ case CT_PHASE_ERROR: if (fmsg == NULL) fmsg = "Phase Sequence Error"; /*FALLTHROUGH*/ case CT_TERMINATED: if (fmsg == NULL) fmsg = "terminated by TERMINATE TRANSFER"; /*FALLTHROUGH*/ case CT_NOACK: if (fmsg == NULL) fmsg = "unacknowledged Immediate Notify pending"; isp_prt(isp, ISP_LOGERR, "CTIO returned by f/w- %s", fmsg); #if 0 if (status & SENSEVALID) { bcopy((caddr_t) (cep + CTIO_SENSE_OFFSET), (caddr_t) &cdp->cd_sensedata, sizeof(scsi_sense_t)); cdp->cd_flags |= CDF_SENSEVALID; } #endif break; default: isp_prt(isp, ISP_LOGERR, "Unknown CTIO status 0x%x", ct->ct_status & ~QLTM_SVALID); break; } if (xs == NULL) { /* * There may be more than one CTIO for a data transfer, * or this may be a status CTIO we're not monitoring. * * The assumption is that they'll all be returned in the * order we got them. */ if (ct->ct_reserved == 0) { if ((ct->ct_flags & CT_SENDSTATUS) == 0) { isp_prt(isp, pl, "intermediate CTIO completed ok"); } else { isp_prt(isp, pl, "unmonitored CTIO completed ok"); } } else { isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_reserved, ct->ct_status & ~QLTM_SVALID); } } else { if (ct->ct_flags & CT_SENDSTATUS) { /* * Sent status and command complete. * * We're now really done with this command, so we * punt to the platform dependent layers because * only there can we do the appropriate command * complete thread synchronization. */ isp_prt(isp, pl, "status CTIO complete"); } else { /* * Final CTIO completed. Release DMA resources and * notify platform dependent layers. */ isp_prt(isp, pl, "data CTIO complete"); ISP_DMAFREE(isp, xs, ct->ct_reserved); } (void) isp_async(isp, ISPASYNC_TARGET_ACTION, ct); /* * The platform layer will destroy the handle if appropriate. */ } } static void isp_handle_ctio2(isp, ct) struct ispsoftc *isp; ct2_entry_t *ct; { XS_T *xs; int pl = ISP_LOGTDEBUG2; char *fmsg = NULL; if (ct->ct_reserved) { xs = isp_find_xs(isp, ct->ct_reserved); if (xs == NULL) pl = ISP_LOGALL; } else { pl = ISP_LOGTDEBUG1; xs = NULL; } switch(ct->ct_status & ~QLTM_SVALID) { case CT_OK: /* * There are generally 2 possibilities as to why we'd get * this condition: * We sent or received data. * We sent status & command complete. */ break; case CT_BDR_MSG: /* * Bus Device Reset message received or the SCSI Bus has * been Reset; the firmware has gone to Bus Free. * * The firmware generates an async mailbox interupt to * notify us of this and returns outstanding CTIOs with this * status. These CTIOs are handled in that same way as * CT_ABORTED ones, so just fall through here. */ fmsg = "Bus Device Reset"; /*FALLTHROUGH*/ case CT_RESET: if (fmsg == NULL) fmsg = "Bus Reset"; /*FALLTHROUGH*/ case CT_ABORTED: /* * When an Abort message is received the firmware goes to * Bus Free and returns all outstanding CTIOs with the status * set, then sends us an Immediate Notify entry. */ if (fmsg == NULL) fmsg = "ABORT TASK sent by Initiator"; isp_prt(isp, ISP_LOGERR, "CTIO2 destroyed by %s", fmsg); break; case CT_INVAL: /* * CTIO rejected by the firmware - invalid data direction. */ isp_prt(isp, ISP_LOGERR, "CTIO2 had wrong data directiond"); break; case CT_NOPATH: /* * CTIO rejected by the firmware due "no path for the * nondisconnecting nexus specified". This means that * we tried to access the bus while a non-disconnecting * command is in process. */ isp_prt(isp, ISP_LOGERR, "Firmware rejected CTIO2 for bad nexus %d->%d", ct->ct_iid, ct->ct_lun); break; case CT_RSELTMO: fmsg = "Reselection"; /*FALLTHROUGH*/ case CT_TIMEOUT: if (fmsg == NULL) fmsg = "Command"; isp_prt(isp, ISP_LOGERR, "Firmware timed out on %s", fmsg); break; case CT_ERR: fmsg = "Completed with Error"; /*FALLTHROUGH*/ case CT_PHASE_ERROR: /* Bus phase sequence error */ if (fmsg == NULL) fmsg = "Phase Sequence Error"; /*FALLTHROUGH*/ case CT_TERMINATED: if (fmsg == NULL) fmsg = "terminated by TERMINATE TRANSFER"; /*FALLTHROUGH*/ case CT_LOGOUT: if (fmsg == NULL) fmsg = "Port Logout"; /*FALLTHROUGH*/ case CT_PORTNOTAVAIL: if (fmsg == NULL) fmsg = "Port not available"; case CT_NOACK: if (fmsg == NULL) fmsg = "unacknowledged Immediate Notify pending"; isp_prt(isp, ISP_LOGERR, "CTIO returned by f/w- %s", fmsg); #if 0 if (status & SENSEVALID) { bcopy((caddr_t) (cep + CTIO_SENSE_OFFSET), (caddr_t) &cdp->cd_sensedata, sizeof(scsi_sense_t)); cdp->cd_flags |= CDF_SENSEVALID; } #endif break; case CT_INVRXID: /* * CTIO rejected by the firmware because an invalid RX_ID. * Just print a message. */ isp_prt(isp, ISP_LOGERR, "CTIO2 completed with Invalid RX_ID 0x%x", ct->ct_rxid); break; default: isp_prt(isp, ISP_LOGERR, "Unknown CTIO status 0x%x", ct->ct_status & ~QLTM_SVALID); break; } if (xs == NULL) { /* * There may be more than one CTIO for a data transfer, * or this may be a status CTIO we're not monitoring. * * The assumption is that they'll all be returned in the * order we got them. */ if (ct->ct_reserved == 0) { if ((ct->ct_flags & CT_SENDSTATUS) == 0) { isp_prt(isp, pl, "intermediate CTIO completed ok"); } else { isp_prt(isp, pl, "unmonitored CTIO completed ok"); } } else { isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_reserved, ct->ct_status & ~QLTM_SVALID); } } else { if (ct->ct_flags & CT_SENDSTATUS) { /* * Sent status and command complete. * * We're now really done with this command, so we * punt to the platform dependent layers because * only there can we do the appropriate command * complete thread synchronization. */ isp_prt(isp, pl, "status CTIO complete"); } else { /* * Final CTIO completed. Release DMA resources and * notify platform dependent layers. */ isp_prt(isp, pl, "data CTIO complete"); ISP_DMAFREE(isp, xs, ct->ct_reserved); } (void) isp_async(isp, ISPASYNC_TARGET_ACTION, ct); /* * The platform layer will destroy the handle if appropriate. */ } } #endif diff --git a/sys/dev/pci/pci.c b/sys/dev/pci/pci.c index 523c21d78b23..3681765716ed 100644 --- a/sys/dev/pci/pci.c +++ b/sys/dev/pci/pci.c @@ -1,1263 +1,1263 @@ /* * Copyright (c) 1997, Stefan Esser * Copyright (c) 2000, Michael Smith * Copyright (c) 2000, BSDi * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ * */ #include "opt_bus.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "pcib_if.h" #include "pci_if.h" static u_int32_t pci_mapbase(unsigned mapreg); static int pci_maptype(unsigned mapreg); static int pci_mapsize(unsigned testval); static int pci_maprange(unsigned mapreg); static void pci_fixancient(pcicfgregs *cfg); static void pci_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg); static struct pci_devinfo *pci_read_device(device_t pcib, int b, int s, int f); static void pci_read_extcap(device_t pcib, pcicfgregs *cfg); static void pci_print_verbose(struct pci_devinfo *dinfo); static int pci_porten(device_t pcib, int b, int s, int f); static int pci_memen(device_t pcib, int b, int s, int f); static int pci_add_map(device_t pcib, int b, int s, int f, int reg, struct resource_list *rl); static void pci_add_resources(device_t pcib, int b, int s, int f, device_t dev); static void pci_add_children(device_t dev, int busno); static int pci_probe(device_t dev); static int pci_print_resources(struct resource_list *rl, const char *name, int type, const char *format); static int pci_print_child(device_t dev, device_t child); static void pci_probe_nomatch(device_t dev, device_t child); static int pci_describe_parse_line(char **ptr, int *vendor, int *device, char **desc); static char *pci_describe_device(device_t dev); static int pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result); static int pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value); static struct resource *pci_alloc_resource(device_t dev, device_t child, int type, int *rid, u_long start, u_long end, u_long count, u_int flags); static void pci_delete_resource(device_t dev, device_t child, int type, int rid); static struct resource_list *pci_get_resource_list (device_t dev, device_t child); static u_int32_t pci_read_config_method(device_t dev, device_t child, int reg, int width); static void pci_write_config_method(device_t dev, device_t child, int reg, u_int32_t val, int width); static int pci_modevent(module_t mod, int what, void *arg); static device_method_t pci_methods[] = { /* Device interface */ DEVMETHOD(device_probe, pci_probe), DEVMETHOD(device_attach, bus_generic_attach), DEVMETHOD(device_shutdown, bus_generic_shutdown), DEVMETHOD(device_suspend, bus_generic_suspend), DEVMETHOD(device_resume, bus_generic_resume), /* Bus interface */ DEVMETHOD(bus_print_child, pci_print_child), DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch), DEVMETHOD(bus_read_ivar, pci_read_ivar), DEVMETHOD(bus_write_ivar, pci_write_ivar), DEVMETHOD(bus_driver_added, bus_generic_driver_added), DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), DEVMETHOD(bus_get_resource_list,pci_get_resource_list), DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), DEVMETHOD(bus_delete_resource, pci_delete_resource), DEVMETHOD(bus_alloc_resource, pci_alloc_resource), DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), /* PCI interface */ DEVMETHOD(pci_read_config, pci_read_config_method), DEVMETHOD(pci_write_config, pci_write_config_method), { 0, 0 } }; static driver_t pci_driver = { "pci", pci_methods, 0, /* no softc */ }; static devclass_t pci_devclass; DRIVER_MODULE(pci, pcib, pci_driver, pci_devclass, pci_modevent, 0); DRIVER_MODULE(pci, acpi_pcib, pci_driver, pci_devclass, pci_modevent, 0); static char *pci_vendordata; static size_t pci_vendordata_size; struct pci_quirk { u_int32_t devid; /* Vendor/device of the card */ int type; -#define PCI_QUIRK_MAP_REG 1 /* PCI map register in wierd place */ +#define PCI_QUIRK_MAP_REG 1 /* PCI map register in weird place */ int arg1; int arg2; }; struct pci_quirk pci_quirks[] = { /* * The Intel 82371AB has a map register at offset 0x90. */ { 0x71138086, PCI_QUIRK_MAP_REG, 0x90, 0 }, { 0 } }; /* map register information */ #define PCI_MAPMEM 0x01 /* memory map */ #define PCI_MAPMEMP 0x02 /* prefetchable memory map */ #define PCI_MAPPORT 0x04 /* port map */ u_int32_t pci_numdevs = 0; /* return base address of memory or port map */ static u_int32_t pci_mapbase(unsigned mapreg) { int mask = 0x03; if ((mapreg & 0x01) == 0) mask = 0x0f; return (mapreg & ~mask); } /* return map type of memory or port map */ static int pci_maptype(unsigned mapreg) { static u_int8_t maptype[0x10] = { PCI_MAPMEM, PCI_MAPPORT, PCI_MAPMEM, 0, PCI_MAPMEM, PCI_MAPPORT, 0, 0, PCI_MAPMEM|PCI_MAPMEMP, PCI_MAPPORT, PCI_MAPMEM|PCI_MAPMEMP, 0, PCI_MAPMEM|PCI_MAPMEMP, PCI_MAPPORT, 0, 0, }; return maptype[mapreg & 0x0f]; } /* return log2 of map size decoded for memory or port map */ static int pci_mapsize(unsigned testval) { int ln2size; testval = pci_mapbase(testval); ln2size = 0; if (testval != 0) { while ((testval & 1) == 0) { ln2size++; testval >>= 1; } } return (ln2size); } /* return log2 of address range supported by map register */ static int pci_maprange(unsigned mapreg) { int ln2range = 0; switch (mapreg & 0x07) { case 0x00: case 0x01: case 0x05: ln2range = 32; break; case 0x02: ln2range = 20; break; case 0x04: ln2range = 64; break; } return (ln2range); } /* adjust some values from PCI 1.0 devices to match 2.0 standards ... */ static void pci_fixancient(pcicfgregs *cfg) { if (cfg->hdrtype != 0) return; /* PCI to PCI bridges use header type 1 */ if (cfg->baseclass == PCIC_BRIDGE && cfg->subclass == PCIS_BRIDGE_PCI) cfg->hdrtype = 1; } /* extract header type specific config data */ static void pci_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg) { #define REG(n, w) PCIB_READ_CONFIG(pcib, b, s, f, n, w) switch (cfg->hdrtype) { case 0: cfg->subvendor = REG(PCIR_SUBVEND_0, 2); cfg->subdevice = REG(PCIR_SUBDEV_0, 2); cfg->nummaps = PCI_MAXMAPS_0; break; case 1: cfg->subvendor = REG(PCIR_SUBVEND_1, 2); cfg->subdevice = REG(PCIR_SUBDEV_1, 2); cfg->nummaps = PCI_MAXMAPS_1; break; case 2: cfg->subvendor = REG(PCIR_SUBVEND_2, 2); cfg->subdevice = REG(PCIR_SUBDEV_2, 2); cfg->nummaps = PCI_MAXMAPS_2; break; } #undef REG } /* read configuration header into pcicfgregs structure */ static struct pci_devinfo * pci_read_device(device_t pcib, int b, int s, int f) { #define REG(n, w) PCIB_READ_CONFIG(pcib, b, s, f, n, w) pcicfgregs *cfg = NULL; struct pci_devinfo *devlist_entry; struct devlist *devlist_head; devlist_head = &pci_devq; devlist_entry = NULL; if (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_DEVVENDOR, 4) != -1) { devlist_entry = malloc(sizeof(struct pci_devinfo), M_DEVBUF, M_WAITOK | M_ZERO); if (devlist_entry == NULL) return (NULL); cfg = &devlist_entry->cfg; cfg->bus = b; cfg->slot = s; cfg->func = f; cfg->vendor = REG(PCIR_VENDOR, 2); cfg->device = REG(PCIR_DEVICE, 2); cfg->cmdreg = REG(PCIR_COMMAND, 2); cfg->statreg = REG(PCIR_STATUS, 2); cfg->baseclass = REG(PCIR_CLASS, 1); cfg->subclass = REG(PCIR_SUBCLASS, 1); cfg->progif = REG(PCIR_PROGIF, 1); cfg->revid = REG(PCIR_REVID, 1); cfg->hdrtype = REG(PCIR_HEADERTYPE, 1); cfg->cachelnsz = REG(PCIR_CACHELNSZ, 1); cfg->lattimer = REG(PCIR_LATTIMER, 1); cfg->intpin = REG(PCIR_INTPIN, 1); cfg->intline = REG(PCIR_INTLINE, 1); cfg->mingnt = REG(PCIR_MINGNT, 1); cfg->maxlat = REG(PCIR_MAXLAT, 1); cfg->mfdev = (cfg->hdrtype & PCIM_MFDEV) != 0; cfg->hdrtype &= ~PCIM_MFDEV; pci_fixancient(cfg); pci_hdrtypedata(pcib, b, s, f, cfg); if (REG(PCIR_STATUS, 2) & PCIM_STATUS_CAPPRESENT) pci_read_extcap(pcib, cfg); STAILQ_INSERT_TAIL(devlist_head, devlist_entry, pci_links); devlist_entry->conf.pc_sel.pc_bus = cfg->bus; devlist_entry->conf.pc_sel.pc_dev = cfg->slot; devlist_entry->conf.pc_sel.pc_func = cfg->func; devlist_entry->conf.pc_hdr = cfg->hdrtype; devlist_entry->conf.pc_subvendor = cfg->subvendor; devlist_entry->conf.pc_subdevice = cfg->subdevice; devlist_entry->conf.pc_vendor = cfg->vendor; devlist_entry->conf.pc_device = cfg->device; devlist_entry->conf.pc_class = cfg->baseclass; devlist_entry->conf.pc_subclass = cfg->subclass; devlist_entry->conf.pc_progif = cfg->progif; devlist_entry->conf.pc_revid = cfg->revid; pci_numdevs++; pci_generation++; } return (devlist_entry); #undef REG } static void pci_read_extcap(device_t pcib, pcicfgregs *cfg) { #define REG(n, w) PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w) int ptr, nextptr, ptrptr; switch (cfg->hdrtype) { case 0: ptrptr = 0x34; break; case 2: ptrptr = 0x14; break; default: return; /* no extended capabilities support */ } nextptr = REG(ptrptr, 1); /* sanity check? */ /* * Read capability entries. */ while (nextptr != 0) { /* Sanity check */ if (nextptr > 255) { printf("illegal PCI extended capability offset %d\n", nextptr); return; } /* Find the next entry */ ptr = nextptr; nextptr = REG(ptr + 1, 1); /* Process this entry */ switch (REG(ptr, 1)) { case 0x01: /* PCI power management */ if (cfg->pp_cap == 0) { cfg->pp_cap = REG(ptr + PCIR_POWER_CAP, 2); cfg->pp_status = ptr + PCIR_POWER_STATUS; cfg->pp_pmcsr = ptr + PCIR_POWER_PMCSR; if ((nextptr - ptr) > PCIR_POWER_DATA) cfg->pp_data = ptr + PCIR_POWER_DATA; } break; default: break; } } #undef REG } #if 0 /* free pcicfgregs structure and all depending data structures */ static int pci_freecfg(struct pci_devinfo *dinfo) { struct devlist *devlist_head; devlist_head = &pci_devq; if (dinfo->cfg.map != NULL) free(dinfo->cfg.map, M_DEVBUF); /* XXX this hasn't been tested */ STAILQ_REMOVE(devlist_head, dinfo, pci_devinfo, pci_links); free(dinfo, M_DEVBUF); /* increment the generation count */ pci_generation++; /* we're losing one device */ pci_numdevs--; return (0); } #endif /* * PCI power manangement */ int pci_set_powerstate(device_t dev, int state) { struct pci_devinfo *dinfo = device_get_ivars(dev); pcicfgregs *cfg = &dinfo->cfg; u_int16_t status; int result; if (cfg->pp_cap != 0) { status = pci_read_config(dev, cfg->pp_status, 2) & ~PCIM_PSTAT_DMASK; result = 0; switch (state) { case PCI_POWERSTATE_D0: status |= PCIM_PSTAT_D0; break; case PCI_POWERSTATE_D1: if (cfg->pp_cap & PCIM_PCAP_D1SUPP) { status |= PCIM_PSTAT_D1; } else { result = EOPNOTSUPP; } break; case PCI_POWERSTATE_D2: if (cfg->pp_cap & PCIM_PCAP_D2SUPP) { status |= PCIM_PSTAT_D2; } else { result = EOPNOTSUPP; } break; case PCI_POWERSTATE_D3: status |= PCIM_PSTAT_D3; break; default: result = EINVAL; } if (result == 0) pci_write_config(dev, cfg->pp_status, status, 2); } else { result = ENXIO; } return(result); } int pci_get_powerstate(device_t dev) { struct pci_devinfo *dinfo = device_get_ivars(dev); pcicfgregs *cfg = &dinfo->cfg; u_int16_t status; int result; if (cfg->pp_cap != 0) { status = pci_read_config(dev, cfg->pp_status, 2); switch (status & PCIM_PSTAT_DMASK) { case PCIM_PSTAT_D0: result = PCI_POWERSTATE_D0; break; case PCIM_PSTAT_D1: result = PCI_POWERSTATE_D1; break; case PCIM_PSTAT_D2: result = PCI_POWERSTATE_D2; break; case PCIM_PSTAT_D3: result = PCI_POWERSTATE_D3; break; default: result = PCI_POWERSTATE_UNKNOWN; break; } } else { /* No support, device is always at D0 */ result = PCI_POWERSTATE_D0; } return(result); } /* * Some convenience functions for PCI device drivers. */ static __inline void pci_set_command_bit(device_t dev, u_int16_t bit) { u_int16_t command; command = pci_read_config(dev, PCIR_COMMAND, 2); command |= bit; pci_write_config(dev, PCIR_COMMAND, command, 2); } static __inline void pci_clear_command_bit(device_t dev, u_int16_t bit) { u_int16_t command; command = pci_read_config(dev, PCIR_COMMAND, 2); command &= ~bit; pci_write_config(dev, PCIR_COMMAND, command, 2); } void pci_enable_busmaster(device_t dev) { pci_set_command_bit(dev, PCIM_CMD_BUSMASTEREN); } void pci_disable_busmaster(device_t dev) { pci_clear_command_bit(dev, PCIM_CMD_BUSMASTEREN); } void pci_enable_io(device_t dev, int space) { switch(space) { case SYS_RES_IOPORT: pci_set_command_bit(dev, PCIM_CMD_PORTEN); break; case SYS_RES_MEMORY: pci_set_command_bit(dev, PCIM_CMD_MEMEN); break; } } void pci_disable_io(device_t dev, int space) { switch(space) { case SYS_RES_IOPORT: pci_clear_command_bit(dev, PCIM_CMD_PORTEN); break; case SYS_RES_MEMORY: pci_clear_command_bit(dev, PCIM_CMD_MEMEN); break; } } /* * New style pci driver. Parent device is either a pci-host-bridge or a * pci-pci-bridge. Both kinds are represented by instances of pcib. */ static void pci_print_verbose(struct pci_devinfo *dinfo) { if (bootverbose) { pcicfgregs *cfg = &dinfo->cfg; printf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n", cfg->vendor, cfg->device, cfg->revid); printf("\tbus=%d, slot=%d, func=%d\n", cfg->bus, cfg->slot, cfg->func); printf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n", cfg->baseclass, cfg->subclass, cfg->progif, cfg->hdrtype, cfg->mfdev); #ifdef PCI_DEBUG printf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n", cfg->cmdreg, cfg->statreg, cfg->cachelnsz); printf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n", cfg->lattimer, cfg->lattimer * 30, cfg->mingnt, cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250); #endif /* PCI_DEBUG */ if (cfg->intpin > 0) printf("\tintpin=%c, irq=%d\n", cfg->intpin +'a' -1, cfg->intline); if (cfg->pp_cap) { u_int16_t status; status = pci_read_config(cfg->dev, cfg->pp_status, 2); printf("\tpowerspec %d supports D0%s%s D3 current D%d\n", cfg->pp_cap & PCIM_PCAP_SPEC, cfg->pp_cap & PCIM_PCAP_D1SUPP ? " D1" : "", cfg->pp_cap & PCIM_PCAP_D2SUPP ? " D2" : "", status & PCIM_PSTAT_DMASK); } } } static int pci_porten(device_t pcib, int b, int s, int f) { return (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2) & PCIM_CMD_PORTEN) != 0; } static int pci_memen(device_t pcib, int b, int s, int f) { return (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2) & PCIM_CMD_MEMEN) != 0; } /* * Add a resource based on a pci map register. Return 1 if the map * register is a 32bit map register or 2 if it is a 64bit register. */ static int pci_add_map(device_t pcib, int b, int s, int f, int reg, struct resource_list *rl) { u_int32_t map; u_int64_t base; u_int8_t ln2size; u_int8_t ln2range; u_int32_t testval; #ifdef PCI_ENABLE_IO_MODES u_int16_t cmd; #endif int type; map = PCIB_READ_CONFIG(pcib, b, s, f, reg, 4); if (map == 0 || map == 0xffffffff) return 1; /* skip invalid entry */ PCIB_WRITE_CONFIG(pcib, b, s, f, reg, 0xffffffff, 4); testval = PCIB_READ_CONFIG(pcib, b, s, f, reg, 4); PCIB_WRITE_CONFIG(pcib, b, s, f, reg, map, 4); base = pci_mapbase(map); if (pci_maptype(map) & PCI_MAPMEM) type = SYS_RES_MEMORY; else type = SYS_RES_IOPORT; ln2size = pci_mapsize(testval); ln2range = pci_maprange(testval); if (ln2range == 64) { /* Read the other half of a 64bit map register */ base |= (u_int64_t) PCIB_READ_CONFIG(pcib, b, s, f, reg + 4, 4) << 32; } if (bootverbose) { printf("\tmap[%02x]: type %x, range %2d, base %08x, size %2d", reg, pci_maptype(map), ln2range, (unsigned int) base, ln2size); if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f)) printf(", port disabled\n"); else if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f)) printf(", memory disabled\n"); else printf(", enabled\n"); } /* * This code theoretically does the right thing, but has * undesirable side effects in some cases where * peripherals respond oddly to having these bits * enabled. Leave them alone by default. */ #ifdef PCI_ENABLE_IO_MODES /* Turn on resources that have been left off by a lazy BIOS */ if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f)) { cmd = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2); cmd |= PCIM_CMD_PORTEN; PCIB_WRITE_CONFIG(pcib, b, s, f, PCIR_COMMAND, cmd, 2); } if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f)) { cmd = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2); cmd |= PCIM_CMD_MEMEN; PCIB_WRITE_CONFIG(pcib, b, s, f, PCIR_COMMAND, cmd, 2); } #else if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f)) return 1; if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f)) return 1; #endif resource_list_add(rl, type, reg, base, base + (1 << ln2size) - 1, (1 << ln2size)); return (ln2range == 64) ? 2 : 1; } static void pci_add_resources(device_t pcib, int b, int s, int f, device_t dev) { struct pci_devinfo *dinfo = device_get_ivars(dev); pcicfgregs *cfg = &dinfo->cfg; struct resource_list *rl = &dinfo->resources; struct pci_quirk *q; int i; for (i = 0; i < cfg->nummaps;) { i += pci_add_map(pcib, b, s, f, PCIR_MAPS + i*4, rl); } for (q = &pci_quirks[0]; q->devid; q++) { if (q->devid == ((cfg->device << 16) | cfg->vendor) && q->type == PCI_QUIRK_MAP_REG) pci_add_map(pcib, b, s, f, q->arg1, rl); } if (cfg->intpin > 0 && cfg->intline != 255) resource_list_add(rl, SYS_RES_IRQ, 0, cfg->intline, cfg->intline, 1); } static void pci_add_children(device_t dev, int busno) { device_t pcib = device_get_parent(dev); int maxslots; int s, f; maxslots = PCIB_MAXSLOTS(pcib); for (s = 0; s <= maxslots; s++) { int pcifunchigh = 0; for (f = 0; f <= pcifunchigh; f++) { struct pci_devinfo *dinfo = pci_read_device(pcib, busno, s, f); if (dinfo != NULL) { if (dinfo->cfg.mfdev) pcifunchigh = PCI_FUNCMAX; dinfo->cfg.dev = device_add_child(dev, NULL, -1); device_set_ivars(dinfo->cfg.dev, dinfo); pci_add_resources(pcib, busno, s, f, dinfo->cfg.dev); pci_print_verbose(dinfo); } } } } static int pci_probe(device_t dev) { static int once, busno; caddr_t vendordata, info; device_set_desc(dev, "PCI bus"); if (bootverbose) device_printf(dev, "physical bus=%d\n", pcib_get_bus(dev)); /* * Since there can be multiple independantly numbered PCI * busses on some large alpha systems, we can't use the unit * number to decide what bus we are probing. We ask the parent * pcib what our bus number is. */ busno = pcib_get_bus(dev); if (busno < 0) return ENXIO; pci_add_children(dev, busno); if (!once) { make_dev(&pcicdev, 0, UID_ROOT, GID_WHEEL, 0644, "pci"); if ((vendordata = preload_search_by_type("pci_vendor_data")) != NULL) { info = preload_search_info(vendordata, MODINFO_ADDR); pci_vendordata = *(char **)info; info = preload_search_info(vendordata, MODINFO_SIZE); pci_vendordata_size = *(size_t *)info; /* terminate the database */ pci_vendordata[pci_vendordata_size] = '\n'; } once++; } return 0; } static int pci_print_resources(struct resource_list *rl, const char *name, int type, const char *format) { struct resource_list_entry *rle; int printed, retval; printed = 0; retval = 0; /* Yes, this is kinda cheating */ SLIST_FOREACH(rle, rl, link) { if (rle->type == type) { if (printed == 0) retval += printf(" %s ", name); else if (printed > 0) retval += printf(","); printed++; retval += printf(format, rle->start); if (rle->count > 1) { retval += printf("-"); retval += printf(format, rle->start + rle->count - 1); } } } return retval; } static int pci_print_child(device_t dev, device_t child) { struct pci_devinfo *dinfo; struct resource_list *rl; pcicfgregs *cfg; int retval = 0; dinfo = device_get_ivars(child); cfg = &dinfo->cfg; rl = &dinfo->resources; retval += bus_print_child_header(dev, child); retval += pci_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx"); retval += pci_print_resources(rl, "mem", SYS_RES_MEMORY, "%#lx"); retval += pci_print_resources(rl, "irq", SYS_RES_IRQ, "%ld"); if (device_get_flags(dev)) retval += printf(" flags %#x", device_get_flags(dev)); retval += printf(" at device %d.%d", pci_get_slot(child), pci_get_function(child)); retval += bus_print_child_footer(dev, child); return (retval); } static struct { int class; int subclass; char *desc; } pci_nomatch_tab[] = { {PCIC_OLD, -1, "old"}, {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"}, {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"}, {PCIC_STORAGE, -1, "mass storage"}, {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"}, {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"}, {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"}, {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"}, {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"}, {PCIC_NETWORK, -1, "network"}, {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"}, {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"}, {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"}, {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"}, {PCIC_DISPLAY, -1, "display"}, {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"}, {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"}, {PCIC_MULTIMEDIA, -1, "multimedia"}, {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"}, {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"}, {PCIC_MEMORY, -1, "memory"}, {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"}, {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"}, {PCIC_BRIDGE, -1, "bridge"}, {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"}, {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"}, {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"}, {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"}, {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"}, {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"}, {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"}, {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"}, {PCIC_BRIDGE, PCIS_BRIDGE_OTHER, "PCI-unknown"}, {PCIC_SIMPLECOMM, -1, "simple comms"}, {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */ {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"}, {PCIC_BASEPERIPH, -1, "base peripheral"}, {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"}, {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"}, {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"}, {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"}, {PCIC_INPUTDEV, -1, "input device"}, {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"}, {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"}, {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"}, {PCIC_DOCKING, -1, "docking station"}, {PCIC_PROCESSOR, -1, "processor"}, {PCIC_SERIALBUS, -1, "serial bus"}, {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"}, {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"}, {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"}, {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"}, {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"}, {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"}, {0, 0, NULL} }; static void pci_probe_nomatch(device_t dev, device_t child) { int i; char *cp, *scp, *device; /* * Look for a listing for this device in a loaded device database. */ if ((device = pci_describe_device(child)) != NULL) { device_printf(dev, "<%s>", device); free(device, M_DEVBUF); } else { /* * Scan the class/subclass descriptions for a general description. */ cp = "unknown"; scp = NULL; for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) { if (pci_nomatch_tab[i].class == pci_get_class(child)) { if (pci_nomatch_tab[i].subclass == -1) { cp = pci_nomatch_tab[i].desc; } else if (pci_nomatch_tab[i].subclass == pci_get_subclass(child)) { scp = pci_nomatch_tab[i].desc; } } } device_printf(dev, "<%s%s%s>", cp ? : "", ((cp != NULL) && (scp != NULL)) ? ", " : "", scp ? : ""); } printf(" at %d.%d (no driver attached)\n", pci_get_slot(child), pci_get_function(child)); return; } /* * Parse the PCI device database, if loaded, and return a pointer to a * description of the device. * * The database is flat text formatted as follows: * * Any line not in a valid format is ignored. * Lines are terminated with newline '\n' characters. * * A VENDOR line consists of the 4 digit (hex) vendor code, a TAB, then * the vendor name. * * A DEVICE line is entered immediately below the corresponding VENDOR ID. * - devices cannot be listed without a corresponding VENDOR line. * A DEVICE line consists of a TAB, the 4 digit (hex) device code, * another TAB, then the device name. */ /* * Assuming (ptr) points to the beginning of a line in the database, * return the vendor or device and description of the next entry. * The value of (vendor) or (device) inappropriate for the entry type * is set to -1. Returns nonzero at the end of the database. * * Note that this is slightly unrobust in the face of corrupt data; * we attempt to safeguard against this by spamming the end of the * database with a newline when we initialise. */ static int pci_describe_parse_line(char **ptr, int *vendor, int *device, char **desc) { char *cp = *ptr; int left; *device = -1; *vendor = -1; **desc = '\0'; for (;;) { left = pci_vendordata_size - (cp - pci_vendordata); if (left <= 0) { *ptr = cp; return(1); } /* vendor entry? */ if (*cp != '\t' && sscanf(cp, "%x\t%80[^\n]", vendor, *desc) == 2) break; /* device entry? */ if (*cp == '\t' && sscanf(cp, "%x\t%80[^\n]", device, *desc) == 2) break; /* skip to next line */ while (*cp != '\n' && left > 0) { cp++; left--; } if (*cp == '\n') { cp++; left--; } } /* skip to next line */ while (*cp != '\n' && left > 0) { cp++; left--; } if (*cp == '\n' && left > 0) cp++; *ptr = cp; return(0); } static char * pci_describe_device(device_t dev) { int vendor, device; char *desc, *vp, *dp, *line; desc = vp = dp = NULL; /* * If we have no vendor data, we can't do anything. */ if (pci_vendordata == NULL) goto out; /* * Scan the vendor data looking for this device */ line = pci_vendordata; if ((vp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL) goto out; for (;;) { if (pci_describe_parse_line(&line, &vendor, &device, &vp)) goto out; if (vendor == pci_get_vendor(dev)) break; } if ((dp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL) goto out; for (;;) { if (pci_describe_parse_line(&line, &vendor, &device, &dp)) { *dp = 0; break; } if (vendor != -1) { *dp = 0; break; } if (device == pci_get_device(dev)) break; } if (dp[0] == '\0') snprintf(dp, 80, "0x%x", pci_get_device(dev)); if ((desc = malloc(strlen(vp) + strlen(dp) + 3, M_DEVBUF, M_NOWAIT)) != NULL) sprintf(desc, "%s, %s", vp, dp); out: if (vp != NULL) free(vp, M_DEVBUF); if (dp != NULL) free(dp, M_DEVBUF); return(desc); } static int pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) { struct pci_devinfo *dinfo; pcicfgregs *cfg; dinfo = device_get_ivars(child); cfg = &dinfo->cfg; switch (which) { case PCI_IVAR_SUBVENDOR: *result = cfg->subvendor; break; case PCI_IVAR_SUBDEVICE: *result = cfg->subdevice; break; case PCI_IVAR_VENDOR: *result = cfg->vendor; break; case PCI_IVAR_DEVICE: *result = cfg->device; break; case PCI_IVAR_DEVID: *result = (cfg->device << 16) | cfg->vendor; break; case PCI_IVAR_CLASS: *result = cfg->baseclass; break; case PCI_IVAR_SUBCLASS: *result = cfg->subclass; break; case PCI_IVAR_PROGIF: *result = cfg->progif; break; case PCI_IVAR_REVID: *result = cfg->revid; break; case PCI_IVAR_INTPIN: *result = cfg->intpin; break; case PCI_IVAR_IRQ: *result = cfg->intline; break; case PCI_IVAR_BUS: *result = cfg->bus; break; case PCI_IVAR_SLOT: *result = cfg->slot; break; case PCI_IVAR_FUNCTION: *result = cfg->func; break; default: return ENOENT; } return 0; } static int pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) { struct pci_devinfo *dinfo; pcicfgregs *cfg; dinfo = device_get_ivars(child); cfg = &dinfo->cfg; switch (which) { case PCI_IVAR_SUBVENDOR: case PCI_IVAR_SUBDEVICE: case PCI_IVAR_VENDOR: case PCI_IVAR_DEVICE: case PCI_IVAR_DEVID: case PCI_IVAR_CLASS: case PCI_IVAR_SUBCLASS: case PCI_IVAR_PROGIF: case PCI_IVAR_REVID: case PCI_IVAR_INTPIN: case PCI_IVAR_IRQ: case PCI_IVAR_BUS: case PCI_IVAR_SLOT: case PCI_IVAR_FUNCTION: return EINVAL; /* disallow for now */ default: return ENOENT; } return 0; } static struct resource * pci_alloc_resource(device_t dev, device_t child, int type, int *rid, u_long start, u_long end, u_long count, u_int flags) { struct pci_devinfo *dinfo = device_get_ivars(child); struct resource_list *rl = &dinfo->resources; pcicfgregs *cfg = &dinfo->cfg; /* * Perform lazy resource allocation * * XXX add support here for SYS_RES_IOPORT and SYS_RES_MEMORY */ if (device_get_parent(child) == dev) { /* * If device doesn't have an interrupt routed, and is deserving of * an interrupt, try to assign it one. */ if ((type == SYS_RES_IRQ) && (cfg->intline == 255) && (cfg->intpin != 0)) { cfg->intline = PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child, cfg->intpin); if (cfg->intline != 255) { pci_write_config(child, PCIR_INTLINE, cfg->intline, 1); resource_list_add(rl, SYS_RES_IRQ, 0, cfg->intline, cfg->intline, 1); } } } return resource_list_alloc(rl, dev, child, type, rid, start, end, count, flags); } static void pci_delete_resource(device_t dev, device_t child, int type, int rid) { printf("pci_delete_resource: PCI resources can not be deleted\n"); } static struct resource_list * pci_get_resource_list (device_t dev, device_t child) { struct pci_devinfo * dinfo = device_get_ivars(child); struct resource_list * rl = &dinfo->resources; if (!rl) return (NULL); return (rl); } static u_int32_t pci_read_config_method(device_t dev, device_t child, int reg, int width) { struct pci_devinfo *dinfo = device_get_ivars(child); pcicfgregs *cfg = &dinfo->cfg; return PCIB_READ_CONFIG(device_get_parent(dev), cfg->bus, cfg->slot, cfg->func, reg, width); } static void pci_write_config_method(device_t dev, device_t child, int reg, u_int32_t val, int width) { struct pci_devinfo *dinfo = device_get_ivars(child); pcicfgregs *cfg = &dinfo->cfg; PCIB_WRITE_CONFIG(device_get_parent(dev), cfg->bus, cfg->slot, cfg->func, reg, val, width); } static int pci_modevent(module_t mod, int what, void *arg) { switch (what) { case MOD_LOAD: STAILQ_INIT(&pci_devq); pci_generation = 0; break; case MOD_UNLOAD: break; } return 0; } diff --git a/sys/kern/vfs_aio.c b/sys/kern/vfs_aio.c index c16cc8c9dba4..8c4175a2af84 100644 --- a/sys/kern/vfs_aio.c +++ b/sys/kern/vfs_aio.c @@ -1,2418 +1,2418 @@ /* * Copyright (c) 1997 John S. Dyson. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. John S. Dyson's name may not be used to endorse or promote products * derived from this software without specific prior written permission. * * DISCLAIMER: This code isn't warranted to do anything useful. Anything * bad that happens because of using this software isn't the responsibility * of the author. This software is distributed AS-IS. * * $FreeBSD$ */ /* * This file contains support for the POSIX 1003.1B AIO/LIO facility. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "opt_vfs_aio.h" #ifdef VFS_AIO static long jobrefid; #define JOBST_NULL 0x0 #define JOBST_JOBQPROC 0x1 #define JOBST_JOBQGLOBAL 0x2 #define JOBST_JOBRUNNING 0x3 #define JOBST_JOBFINISHED 0x4 #define JOBST_JOBQBUF 0x5 #define JOBST_JOBBFINISHED 0x6 #ifndef MAX_AIO_PER_PROC #define MAX_AIO_PER_PROC 32 #endif #ifndef MAX_AIO_QUEUE_PER_PROC #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */ #endif #ifndef MAX_AIO_PROCS #define MAX_AIO_PROCS 32 #endif #ifndef MAX_AIO_QUEUE #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */ #endif #ifndef TARGET_AIO_PROCS #define TARGET_AIO_PROCS 4 #endif #ifndef MAX_BUF_AIO #define MAX_BUF_AIO 16 #endif #ifndef AIOD_TIMEOUT_DEFAULT #define AIOD_TIMEOUT_DEFAULT (10 * hz) #endif #ifndef AIOD_LIFETIME_DEFAULT #define AIOD_LIFETIME_DEFAULT (30 * hz) #endif static int max_aio_procs = MAX_AIO_PROCS; static int num_aio_procs = 0; static int target_aio_procs = TARGET_AIO_PROCS; static int max_queue_count = MAX_AIO_QUEUE; static int num_queue_count = 0; static int num_buf_aio = 0; static int num_aio_resv_start = 0; static int aiod_timeout; static int aiod_lifetime; static int max_aio_per_proc = MAX_AIO_PER_PROC; static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC; static int max_buf_aio = MAX_BUF_AIO; SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "AIO mgmt"); SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc, 0, ""); SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW, &max_aio_queue_per_proc, 0, ""); SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0, ""); SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0, ""); SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0, ""); SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0, ""); SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs, 0, ""); SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0, ""); SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0, ""); SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0, ""); SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0, ""); /* * AIO process info */ #define AIOP_FREE 0x1 /* proc on free queue */ #define AIOP_SCHED 0x2 /* proc explicitly scheduled */ struct aioproclist { int aioprocflags; /* AIO proc flags */ TAILQ_ENTRY(aioproclist) list; /* List of processes */ struct proc *aioproc; /* The AIO thread */ TAILQ_HEAD (,aiocblist) jobtorun; /* suggested job to run */ }; /* * data-structure for lio signal management */ struct aio_liojob { int lioj_flags; int lioj_buffer_count; int lioj_buffer_finished_count; int lioj_queue_count; int lioj_queue_finished_count; struct sigevent lioj_signal; /* signal on all I/O done */ TAILQ_ENTRY (aio_liojob) lioj_list; struct kaioinfo *lioj_ki; }; #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */ #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */ /* * per process aio data structure */ struct kaioinfo { int kaio_flags; /* per process kaio flags */ int kaio_maxactive_count; /* maximum number of AIOs */ int kaio_active_count; /* number of currently used AIOs */ int kaio_qallowed_count; /* maxiumu size of AIO queue */ int kaio_queue_count; /* size of AIO queue */ int kaio_ballowed_count; /* maximum number of buffers */ int kaio_queue_finished_count; /* number of daemon jobs finished */ int kaio_buffer_count; /* number of physio buffers */ int kaio_buffer_finished_count; /* count of I/O done */ struct proc *kaio_p; /* process that uses this kaio block */ TAILQ_HEAD (,aio_liojob) kaio_liojoblist; /* list of lio jobs */ TAILQ_HEAD (,aiocblist) kaio_jobqueue; /* job queue for process */ TAILQ_HEAD (,aiocblist) kaio_jobdone; /* done queue for process */ TAILQ_HEAD (,aiocblist) kaio_bufqueue; /* buffer job queue for process */ TAILQ_HEAD (,aiocblist) kaio_bufdone; /* buffer done queue for process */ TAILQ_HEAD (,aiocblist) kaio_sockqueue; /* queue for aios waiting on sockets */ }; #define KAIO_RUNDOWN 0x1 /* process is being run down */ #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */ static TAILQ_HEAD(,aioproclist) aio_freeproc, aio_activeproc; static TAILQ_HEAD(,aiocblist) aio_jobs; /* Async job list */ static TAILQ_HEAD(,aiocblist) aio_bufjobs; /* Phys I/O job list */ static TAILQ_HEAD(,aiocblist) aio_freejobs; /* Pool of free jobs */ static void aio_init_aioinfo(struct proc *p); static void aio_onceonly(void *); static int aio_free_entry(struct aiocblist *aiocbe); static void aio_process(struct aiocblist *aiocbe); static int aio_newproc(void); static int aio_aqueue(struct proc *p, struct aiocb *job, int type); static void aio_physwakeup(struct buf *bp); static int aio_fphysio(struct proc *p, struct aiocblist *aiocbe, int type); static int aio_qphysio(struct proc *p, struct aiocblist *iocb); static void aio_daemon(void *uproc); SYSINIT(aio, SI_SUB_VFS, SI_ORDER_ANY, aio_onceonly, NULL); static vm_zone_t kaio_zone = 0, aiop_zone = 0, aiocb_zone = 0, aiol_zone = 0; static vm_zone_t aiolio_zone = 0; /* * Startup initialization */ void aio_onceonly(void *na) { TAILQ_INIT(&aio_freeproc); TAILQ_INIT(&aio_activeproc); TAILQ_INIT(&aio_jobs); TAILQ_INIT(&aio_bufjobs); TAILQ_INIT(&aio_freejobs); kaio_zone = zinit("AIO", sizeof (struct kaioinfo), 0, 0, 1); aiop_zone = zinit("AIOP", sizeof (struct aioproclist), 0, 0, 1); aiocb_zone = zinit("AIOCB", sizeof (struct aiocblist), 0, 0, 1); aiol_zone = zinit("AIOL", AIO_LISTIO_MAX * sizeof (int), 0, 0, 1); aiolio_zone = zinit("AIOLIO", AIO_LISTIO_MAX * sizeof (struct aio_liojob), 0, 0, 1); aiod_timeout = AIOD_TIMEOUT_DEFAULT; aiod_lifetime = AIOD_LIFETIME_DEFAULT; jobrefid = 1; } /* * Init the per-process aioinfo structure. The aioinfo limits are set * per-process for user limit (resource) management. */ void aio_init_aioinfo(struct proc *p) { struct kaioinfo *ki; if (p->p_aioinfo == NULL) { ki = zalloc(kaio_zone); p->p_aioinfo = ki; ki->kaio_flags = 0; ki->kaio_maxactive_count = max_aio_per_proc; ki->kaio_active_count = 0; ki->kaio_qallowed_count = max_aio_queue_per_proc; ki->kaio_queue_count = 0; ki->kaio_ballowed_count = max_buf_aio; ki->kaio_buffer_count = 0; ki->kaio_buffer_finished_count = 0; ki->kaio_p = p; TAILQ_INIT(&ki->kaio_jobdone); TAILQ_INIT(&ki->kaio_jobqueue); TAILQ_INIT(&ki->kaio_bufdone); TAILQ_INIT(&ki->kaio_bufqueue); TAILQ_INIT(&ki->kaio_liojoblist); TAILQ_INIT(&ki->kaio_sockqueue); } while (num_aio_procs < target_aio_procs) aio_newproc(); } /* * Free a job entry. Wait for completion if it is currently active, but don't * delay forever. If we delay, we return a flag that says that we have to * restart the queue scan. */ int aio_free_entry(struct aiocblist *aiocbe) { struct kaioinfo *ki; struct aioproclist *aiop; struct aio_liojob *lj; struct proc *p; int error; int s; if (aiocbe->jobstate == JOBST_NULL) panic("aio_free_entry: freeing already free job"); p = aiocbe->userproc; ki = p->p_aioinfo; lj = aiocbe->lio; if (ki == NULL) panic("aio_free_entry: missing p->p_aioinfo"); if (aiocbe->jobstate == JOBST_JOBRUNNING) { if (aiocbe->jobflags & AIOCBLIST_ASYNCFREE) return 0; aiocbe->jobflags |= AIOCBLIST_RUNDOWN; tsleep(aiocbe, PRIBIO|PCATCH, "jobwai", 0); } aiocbe->jobflags &= ~AIOCBLIST_ASYNCFREE; if (aiocbe->bp == NULL) { if (ki->kaio_queue_count <= 0) panic("aio_free_entry: process queue size <= 0"); if (num_queue_count <= 0) panic("aio_free_entry: system wide queue size <= 0"); if (lj) { lj->lioj_queue_count--; if (aiocbe->jobflags & AIOCBLIST_DONE) lj->lioj_queue_finished_count--; } ki->kaio_queue_count--; if (aiocbe->jobflags & AIOCBLIST_DONE) ki->kaio_queue_finished_count--; num_queue_count--; } else { if (lj) { lj->lioj_buffer_count--; if (aiocbe->jobflags & AIOCBLIST_DONE) lj->lioj_buffer_finished_count--; } if (aiocbe->jobflags & AIOCBLIST_DONE) ki->kaio_buffer_finished_count--; ki->kaio_buffer_count--; num_buf_aio--; } /* aiocbe is going away, we need to destroy any knotes */ knote_remove(p, &aiocbe->klist); if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags & KAIO_RUNDOWN) && ((ki->kaio_buffer_count == 0) && (ki->kaio_queue_count == 0)))) { ki->kaio_flags &= ~KAIO_WAKEUP; wakeup(p); } if (aiocbe->jobstate == JOBST_JOBQBUF) { if ((error = aio_fphysio(p, aiocbe, 1)) != 0) return error; if (aiocbe->jobstate != JOBST_JOBBFINISHED) panic("aio_free_entry: invalid physio finish-up state"); s = splbio(); TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist); splx(s); } else if (aiocbe->jobstate == JOBST_JOBQPROC) { aiop = aiocbe->jobaioproc; TAILQ_REMOVE(&aiop->jobtorun, aiocbe, list); } else if (aiocbe->jobstate == JOBST_JOBQGLOBAL) TAILQ_REMOVE(&aio_jobs, aiocbe, list); else if (aiocbe->jobstate == JOBST_JOBFINISHED) TAILQ_REMOVE(&ki->kaio_jobdone, aiocbe, plist); else if (aiocbe->jobstate == JOBST_JOBBFINISHED) { s = splbio(); TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist); splx(s); if (aiocbe->bp) { vunmapbuf(aiocbe->bp); relpbuf(aiocbe->bp, NULL); aiocbe->bp = NULL; } } if (lj && (lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 0)) { TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); zfree(aiolio_zone, lj); } TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list); aiocbe->jobstate = JOBST_NULL; return 0; } #endif /* VFS_AIO */ /* * Rundown the jobs for a given process. */ void aio_proc_rundown(struct proc *p) { #ifndef VFS_AIO return; #else int s; struct kaioinfo *ki; struct aio_liojob *lj, *ljn; struct aiocblist *aiocbe, *aiocbn; struct file *fp; struct filedesc *fdp; struct socket *so; ki = p->p_aioinfo; if (ki == NULL) return; ki->kaio_flags |= LIOJ_SIGNAL_POSTED; while ((ki->kaio_active_count > 0) || (ki->kaio_buffer_count > ki->kaio_buffer_finished_count)) { ki->kaio_flags |= KAIO_RUNDOWN; if (tsleep(p, PRIBIO, "kaiowt", aiod_timeout)) break; } /* * Move any aio ops that are waiting on socket I/O to the normal job * queues so they are cleaned up with any others. */ fdp = p->p_fd; s = splnet(); for (aiocbe = TAILQ_FIRST(&ki->kaio_sockqueue); aiocbe; aiocbe = aiocbn) { aiocbn = TAILQ_NEXT(aiocbe, plist); fp = fdp->fd_ofiles[aiocbe->uaiocb.aio_fildes]; /* * Under some circumstances, the aio_fildes and the file * structure don't match. This would leave aiocbe's in the * TAILQ associated with the socket and cause a panic later. * * Detect and fix. */ if ((fp == NULL) || (fp != aiocbe->fd_file)) fp = aiocbe->fd_file; if (fp) { so = (struct socket *)fp->f_data; TAILQ_REMOVE(&so->so_aiojobq, aiocbe, list); if (TAILQ_EMPTY(&so->so_aiojobq)) { so->so_snd.sb_flags &= ~SB_AIO; so->so_rcv.sb_flags &= ~SB_AIO; } } TAILQ_REMOVE(&ki->kaio_sockqueue, aiocbe, plist); TAILQ_INSERT_HEAD(&aio_jobs, aiocbe, list); TAILQ_INSERT_HEAD(&ki->kaio_jobqueue, aiocbe, plist); } splx(s); restart1: for (aiocbe = TAILQ_FIRST(&ki->kaio_jobdone); aiocbe; aiocbe = aiocbn) { aiocbn = TAILQ_NEXT(aiocbe, plist); if (aio_free_entry(aiocbe)) goto restart1; } restart2: for (aiocbe = TAILQ_FIRST(&ki->kaio_jobqueue); aiocbe; aiocbe = aiocbn) { aiocbn = TAILQ_NEXT(aiocbe, plist); if (aio_free_entry(aiocbe)) goto restart2; } /* * Note the use of lots of splbio here, trying to avoid splbio for long chains * of I/O. Probably unnecessary. */ restart3: s = splbio(); while (TAILQ_FIRST(&ki->kaio_bufqueue)) { ki->kaio_flags |= KAIO_WAKEUP; tsleep(p, PRIBIO, "aioprn", 0); splx(s); goto restart3; } splx(s); restart4: s = splbio(); for (aiocbe = TAILQ_FIRST(&ki->kaio_bufdone); aiocbe; aiocbe = aiocbn) { aiocbn = TAILQ_NEXT(aiocbe, plist); if (aio_free_entry(aiocbe)) { splx(s); goto restart4; } } splx(s); for (lj = TAILQ_FIRST(&ki->kaio_liojoblist); lj; lj = ljn) { ljn = TAILQ_NEXT(lj, lioj_list); if ((lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 0)) { TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); zfree(aiolio_zone, lj); } else { #ifdef DIAGNOSTIC printf("LIO job not cleaned up: B:%d, BF:%d, Q:%d, " "QF:%d\n", lj->lioj_buffer_count, lj->lioj_buffer_finished_count, lj->lioj_queue_count, lj->lioj_queue_finished_count); #endif } } zfree(kaio_zone, ki); p->p_aioinfo = NULL; #endif /* VFS_AIO */ } #ifdef VFS_AIO /* * Select a job to run (called by an AIO daemon). */ static struct aiocblist * aio_selectjob(struct aioproclist *aiop) { int s; struct aiocblist *aiocbe; struct kaioinfo *ki; struct proc *userp; aiocbe = TAILQ_FIRST(&aiop->jobtorun); if (aiocbe) { TAILQ_REMOVE(&aiop->jobtorun, aiocbe, list); return aiocbe; } s = splnet(); for (aiocbe = TAILQ_FIRST(&aio_jobs); aiocbe; aiocbe = TAILQ_NEXT(aiocbe, list)) { userp = aiocbe->userproc; ki = userp->p_aioinfo; if (ki->kaio_active_count < ki->kaio_maxactive_count) { TAILQ_REMOVE(&aio_jobs, aiocbe, list); splx(s); return aiocbe; } } splx(s); return NULL; } /* * The AIO processing activity. This is the code that does the I/O request for * the non-physio version of the operations. The normal vn operations are used, * and this code should work in all instances for every type of file, including * pipes, sockets, fifos, and regular files. */ void aio_process(struct aiocblist *aiocbe) { struct filedesc *fdp; struct proc *userp, *mycp; struct aiocb *cb; struct file *fp; struct uio auio; struct iovec aiov; unsigned int fd; int cnt; int error; off_t offset; int oublock_st, oublock_end; int inblock_st, inblock_end; userp = aiocbe->userproc; cb = &aiocbe->uaiocb; mycp = curproc; fdp = mycp->p_fd; fd = cb->aio_fildes; fp = fdp->fd_ofiles[fd]; if ((fp == NULL) || (fp != aiocbe->fd_file)) { cb->_aiocb_private.error = EBADF; cb->_aiocb_private.status = -1; return; } aiov.iov_base = (void *)cb->aio_buf; aiov.iov_len = cb->aio_nbytes; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = offset = cb->aio_offset; auio.uio_resid = cb->aio_nbytes; cnt = cb->aio_nbytes; auio.uio_segflg = UIO_USERSPACE; auio.uio_procp = mycp; inblock_st = mycp->p_stats->p_ru.ru_inblock; oublock_st = mycp->p_stats->p_ru.ru_oublock; /* * Temporarily bump the ref count while reading to avoid the * descriptor being ripped out from under us. */ fhold(fp); if (cb->aio_lio_opcode == LIO_READ) { auio.uio_rw = UIO_READ; error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, mycp); } else { auio.uio_rw = UIO_WRITE; error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, mycp); } fdrop(fp, mycp); inblock_end = mycp->p_stats->p_ru.ru_inblock; oublock_end = mycp->p_stats->p_ru.ru_oublock; aiocbe->inputcharge = inblock_end - inblock_st; aiocbe->outputcharge = oublock_end - oublock_st; if ((error) && (auio.uio_resid != cnt)) { if (error == ERESTART || error == EINTR || error == EWOULDBLOCK) error = 0; if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) psignal(userp, SIGPIPE); } cnt -= auio.uio_resid; cb->_aiocb_private.error = error; cb->_aiocb_private.status = cnt; return; } /* * The AIO daemon, most of the actual work is done in aio_process, * but the setup (and address space mgmt) is done in this routine. */ static void aio_daemon(void *uproc) { int s; struct aio_liojob *lj; struct aiocb *cb; struct aiocblist *aiocbe; struct aioproclist *aiop; struct kaioinfo *ki; struct proc *curcp, *mycp, *userp; struct vmspace *myvm, *tmpvm; mtx_enter(&Giant, MTX_DEF); /* * Local copies of curproc (cp) and vmspace (myvm) */ mycp = curproc; myvm = mycp->p_vmspace; if (mycp->p_textvp) { vrele(mycp->p_textvp); mycp->p_textvp = NULL; } /* * Allocate and ready the aio control info. There is one aiop structure * per daemon. */ aiop = zalloc(aiop_zone); aiop->aioproc = mycp; aiop->aioprocflags |= AIOP_FREE; TAILQ_INIT(&aiop->jobtorun); s = splnet(); /* * Place thread (lightweight process) onto the AIO free thread list. */ if (TAILQ_EMPTY(&aio_freeproc)) wakeup(&aio_freeproc); TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list); splx(s); /* Make up a name for the daemon. */ strcpy(mycp->p_comm, "aiod"); /* * Get rid of our current filedescriptors. AIOD's don't need any * filedescriptors, except as temporarily inherited from the client. * Credentials are also cloned, and made equivalent to "root". */ fdfree(mycp); mycp->p_fd = NULL; mycp->p_ucred = crcopy(mycp->p_ucred); mycp->p_ucred->cr_uid = 0; uifree(mycp->p_ucred->cr_uidinfo); mycp->p_ucred->cr_uidinfo = uifind(0); mycp->p_ucred->cr_ngroups = 1; mycp->p_ucred->cr_groups[0] = 1; /* The daemon resides in its own pgrp. */ enterpgrp(mycp, mycp->p_pid, 1); /* Mark special process type. */ mycp->p_flag |= P_SYSTEM; /* * Wakeup parent process. (Parent sleeps to keep from blasting away * creating to many daemons.) */ wakeup(mycp); for (;;) { /* * curcp is the current daemon process context. * userp is the current user process context. */ curcp = mycp; /* * Take daemon off of free queue */ if (aiop->aioprocflags & AIOP_FREE) { s = splnet(); TAILQ_REMOVE(&aio_freeproc, aiop, list); TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list); aiop->aioprocflags &= ~AIOP_FREE; splx(s); } aiop->aioprocflags &= ~AIOP_SCHED; /* * Check for jobs. */ while ((aiocbe = aio_selectjob(aiop)) != NULL) { cb = &aiocbe->uaiocb; userp = aiocbe->userproc; aiocbe->jobstate = JOBST_JOBRUNNING; /* * Connect to process address space for user program. */ if (userp != curcp) { /* * Save the current address space that we are * connected to. */ tmpvm = mycp->p_vmspace; /* * Point to the new user address space, and * refer to it. */ mycp->p_vmspace = userp->p_vmspace; mycp->p_vmspace->vm_refcnt++; /* Activate the new mapping. */ pmap_activate(mycp); /* * If the old address space wasn't the daemons * own address space, then we need to remove the * daemon's reference from the other process * that it was acting on behalf of. */ if (tmpvm != myvm) { vmspace_free(tmpvm); } /* * Disassociate from previous clients file * descriptors, and associate to the new clients * descriptors. Note that the daemon doesn't * need to worry about its orginal descriptors, * because they were originally freed. */ if (mycp->p_fd) fdfree(mycp); mycp->p_fd = fdshare(userp); curcp = userp; } ki = userp->p_aioinfo; lj = aiocbe->lio; /* Account for currently active jobs. */ ki->kaio_active_count++; /* Do the I/O function. */ aiocbe->jobaioproc = aiop; aio_process(aiocbe); /* Decrement the active job count. */ ki->kaio_active_count--; /* * Increment the completion count for wakeup/signal * comparisons. */ aiocbe->jobflags |= AIOCBLIST_DONE; ki->kaio_queue_finished_count++; if (lj) lj->lioj_queue_finished_count++; if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags & KAIO_RUNDOWN) && (ki->kaio_active_count == 0))) { ki->kaio_flags &= ~KAIO_WAKEUP; wakeup(userp); } s = splbio(); if (lj && (lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) { if ((lj->lioj_queue_finished_count == lj->lioj_queue_count) && (lj->lioj_buffer_finished_count == lj->lioj_buffer_count)) { psignal(userp, lj->lioj_signal.sigev_signo); lj->lioj_flags |= LIOJ_SIGNAL_POSTED; } } splx(s); aiocbe->jobstate = JOBST_JOBFINISHED; /* * If the I/O request should be automatically rundown, * do the needed cleanup. Otherwise, place the queue * entry for the just finished I/O request into the done * queue for the associated client. */ s = splnet(); if (aiocbe->jobflags & AIOCBLIST_ASYNCFREE) { aiocbe->jobflags &= ~AIOCBLIST_ASYNCFREE; TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list); } else { TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist); TAILQ_INSERT_TAIL(&ki->kaio_jobdone, aiocbe, plist); } splx(s); KNOTE(&aiocbe->klist, 0); if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) { wakeup(aiocbe); aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN; } if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) { psignal(userp, cb->aio_sigevent.sigev_signo); } } /* * Disconnect from user address space. */ if (curcp != mycp) { /* Get the user address space to disconnect from. */ tmpvm = mycp->p_vmspace; /* Get original address space for daemon. */ mycp->p_vmspace = myvm; /* Activate the daemon's address space. */ pmap_activate(mycp); #ifdef DIAGNOSTIC if (tmpvm == myvm) { printf("AIOD: vmspace problem -- %d\n", mycp->p_pid); } #endif /* Remove our vmspace reference. */ vmspace_free(tmpvm); /* * Disassociate from the user process's file * descriptors. */ if (mycp->p_fd) fdfree(mycp); mycp->p_fd = NULL; curcp = mycp; } /* * If we are the first to be put onto the free queue, wakeup * anyone waiting for a daemon. */ s = splnet(); TAILQ_REMOVE(&aio_activeproc, aiop, list); if (TAILQ_EMPTY(&aio_freeproc)) wakeup(&aio_freeproc); TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list); aiop->aioprocflags |= AIOP_FREE; splx(s); /* * If daemon is inactive for a long time, allow it to exit, * thereby freeing resources. */ if (((aiop->aioprocflags & AIOP_SCHED) == 0) && tsleep(mycp, PRIBIO, "aiordy", aiod_lifetime)) { s = splnet(); if ((TAILQ_FIRST(&aio_jobs) == NULL) && (TAILQ_FIRST(&aiop->jobtorun) == NULL)) { if ((aiop->aioprocflags & AIOP_FREE) && (num_aio_procs > target_aio_procs)) { TAILQ_REMOVE(&aio_freeproc, aiop, list); splx(s); zfree(aiop_zone, aiop); num_aio_procs--; #ifdef DIAGNOSTIC if (mycp->p_vmspace->vm_refcnt <= 1) { printf("AIOD: bad vm refcnt for" " exiting daemon: %d\n", mycp->p_vmspace->vm_refcnt); } #endif exit1(mycp, 0); } } splx(s); } } } /* * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The * AIO daemon modifies its environment itself. */ static int aio_newproc() { int error; struct proc *p, *np; p = &proc0; error = fork1(p, RFPROC|RFMEM|RFNOWAIT, &np); if (error) return error; cpu_set_fork_handler(np, aio_daemon, curproc); /* * Wait until daemon is started, but continue on just in case to * handle error conditions. */ error = tsleep(np, PZERO, "aiosta", aiod_timeout); num_aio_procs++; return error; } /* * Try the high-performance physio method for eligible VCHR devices. This * routine doesn't require the use of any additional threads, and have overhead. */ int aio_qphysio(struct proc *p, struct aiocblist *aiocbe) { int error; struct aiocb *cb; struct file *fp; struct buf *bp; struct vnode *vp; struct kaioinfo *ki; struct filedesc *fdp; struct aio_liojob *lj; int fd; int s; int notify; cb = &aiocbe->uaiocb; fdp = p->p_fd; fd = cb->aio_fildes; fp = fdp->fd_ofiles[fd]; if (fp->f_type != DTYPE_VNODE) return (-1); vp = (struct vnode *)fp->f_data; /* * If its not a disk, we don't want to return a positive error. * It causes the aio code to not fall through to try the thread * way when you're talking to a regular file. */ if (!vn_isdisk(vp, &error)) { if (error == ENOTBLK) return (-1); else return (error); } if (cb->aio_nbytes % vp->v_rdev->si_bsize_phys) return (-1); if (cb->aio_nbytes > MAXPHYS) return (-1); ki = p->p_aioinfo; if (ki->kaio_buffer_count >= ki->kaio_ballowed_count) return (-1); fhold(fp); ki->kaio_buffer_count++; lj = aiocbe->lio; if (lj) lj->lioj_buffer_count++; /* Create and build a buffer header for a transfer. */ bp = (struct buf *)getpbuf(NULL); /* * Get a copy of the kva from the physical buffer. */ bp->b_caller1 = p; bp->b_dev = vp->v_rdev; error = bp->b_error = 0; bp->b_bcount = cb->aio_nbytes; bp->b_bufsize = cb->aio_nbytes; bp->b_flags = B_PHYS; bp->b_iodone = aio_physwakeup; bp->b_saveaddr = bp->b_data; bp->b_data = (void *)cb->aio_buf; bp->b_blkno = btodb(cb->aio_offset); if (cb->aio_lio_opcode == LIO_WRITE) { bp->b_iocmd = BIO_WRITE; if (!useracc(bp->b_data, bp->b_bufsize, VM_PROT_READ)) { error = EFAULT; goto doerror; } } else { bp->b_iocmd = BIO_READ; if (!useracc(bp->b_data, bp->b_bufsize, VM_PROT_WRITE)) { error = EFAULT; goto doerror; } } /* Bring buffer into kernel space. */ vmapbuf(bp); s = splbio(); aiocbe->bp = bp; bp->b_spc = (void *)aiocbe; TAILQ_INSERT_TAIL(&aio_bufjobs, aiocbe, list); TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist); aiocbe->jobstate = JOBST_JOBQBUF; cb->_aiocb_private.status = cb->aio_nbytes; num_buf_aio++; bp->b_error = 0; splx(s); /* Perform transfer. */ DEV_STRATEGY(bp, 0); notify = 0; s = splbio(); /* * If we had an error invoking the request, or an error in processing * the request before we have returned, we process it as an error in * transfer. Note that such an I/O error is not indicated immediately, * but is returned using the aio_error mechanism. In this case, * aio_suspend will return immediately. */ if (bp->b_error || (bp->b_ioflags & BIO_ERROR)) { struct aiocb *job = aiocbe->uuaiocb; aiocbe->uaiocb._aiocb_private.status = 0; suword(&job->_aiocb_private.status, 0); aiocbe->uaiocb._aiocb_private.error = bp->b_error; suword(&job->_aiocb_private.error, bp->b_error); ki->kaio_buffer_finished_count++; if (aiocbe->jobstate != JOBST_JOBBFINISHED) { aiocbe->jobstate = JOBST_JOBBFINISHED; aiocbe->jobflags |= AIOCBLIST_DONE; TAILQ_REMOVE(&aio_bufjobs, aiocbe, list); TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist); TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist); notify = 1; } } splx(s); if (notify) KNOTE(&aiocbe->klist, 0); fdrop(fp, p); return 0; doerror: ki->kaio_buffer_count--; if (lj) lj->lioj_buffer_count--; aiocbe->bp = NULL; relpbuf(bp, NULL); fdrop(fp, p); return error; } /* * This waits/tests physio completion. */ int aio_fphysio(struct proc *p, struct aiocblist *iocb, int flgwait) { int s; struct buf *bp; int error; bp = iocb->bp; s = splbio(); if (flgwait == 0) { if ((bp->b_flags & B_DONE) == 0) { splx(s); return EINPROGRESS; } } while ((bp->b_flags & B_DONE) == 0) { if (tsleep((caddr_t)bp, PRIBIO, "physstr", aiod_timeout)) { if ((bp->b_flags & B_DONE) == 0) { splx(s); return EINPROGRESS; } else break; } } /* Release mapping into kernel space. */ vunmapbuf(bp); iocb->bp = 0; error = 0; /* Check for an error. */ if (bp->b_ioflags & BIO_ERROR) error = bp->b_error; relpbuf(bp, NULL); return (error); } #endif /* VFS_AIO */ /* * Wake up aio requests that may be serviceable now. */ void aio_swake(struct socket *so, struct sockbuf *sb) { #ifndef VFS_AIO return; #else struct aiocblist *cb,*cbn; struct proc *p; struct kaioinfo *ki = NULL; int opcode, wakecount = 0; struct aioproclist *aiop; if (sb == &so->so_snd) { opcode = LIO_WRITE; so->so_snd.sb_flags &= ~SB_AIO; } else { opcode = LIO_READ; so->so_rcv.sb_flags &= ~SB_AIO; } for (cb = TAILQ_FIRST(&so->so_aiojobq); cb; cb = cbn) { cbn = TAILQ_NEXT(cb, list); if (opcode == cb->uaiocb.aio_lio_opcode) { p = cb->userproc; ki = p->p_aioinfo; TAILQ_REMOVE(&so->so_aiojobq, cb, list); TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist); TAILQ_INSERT_TAIL(&aio_jobs, cb, list); TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist); wakecount++; if (cb->jobstate != JOBST_JOBQGLOBAL) panic("invalid queue value"); } } while (wakecount--) { if ((aiop = TAILQ_FIRST(&aio_freeproc)) != 0) { TAILQ_REMOVE(&aio_freeproc, aiop, list); TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list); aiop->aioprocflags &= ~AIOP_FREE; wakeup(aiop->aioproc); } } #endif /* VFS_AIO */ } #ifdef VFS_AIO /* * Queue a new AIO request. Choosing either the threaded or direct physio VCHR * technique is done in this code. */ static int _aio_aqueue(struct proc *p, struct aiocb *job, struct aio_liojob *lj, int type) { struct filedesc *fdp; struct file *fp; unsigned int fd; struct socket *so; int s; int error; int opcode; struct aiocblist *aiocbe; struct aioproclist *aiop; struct kaioinfo *ki; struct kevent kev; struct kqueue *kq; struct file *kq_fp; if ((aiocbe = TAILQ_FIRST(&aio_freejobs)) != NULL) TAILQ_REMOVE(&aio_freejobs, aiocbe, list); else aiocbe = zalloc (aiocb_zone); aiocbe->inputcharge = 0; aiocbe->outputcharge = 0; SLIST_INIT(&aiocbe->klist); suword(&job->_aiocb_private.status, -1); suword(&job->_aiocb_private.error, 0); suword(&job->_aiocb_private.kernelinfo, -1); error = copyin((caddr_t)job, (caddr_t) &aiocbe->uaiocb, sizeof aiocbe->uaiocb); if (error) { suword(&job->_aiocb_private.error, error); TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list); return error; } /* Save userspace address of the job info. */ aiocbe->uuaiocb = job; /* Get the opcode. */ if (type != LIO_NOP) aiocbe->uaiocb.aio_lio_opcode = type; opcode = aiocbe->uaiocb.aio_lio_opcode; /* Get the fd info for process. */ fdp = p->p_fd; /* * Range check file descriptor. */ fd = aiocbe->uaiocb.aio_fildes; if (fd >= fdp->fd_nfiles) { TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list); if (type == 0) suword(&job->_aiocb_private.error, EBADF); return EBADF; } fp = aiocbe->fd_file = fdp->fd_ofiles[fd]; if ((fp == NULL) || ((opcode == LIO_WRITE) && ((fp->f_flag & FWRITE) == 0))) { TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list); if (type == 0) suword(&job->_aiocb_private.error, EBADF); return EBADF; } if (aiocbe->uaiocb.aio_offset == -1LL) { TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list); if (type == 0) suword(&job->_aiocb_private.error, EINVAL); return EINVAL; } error = suword(&job->_aiocb_private.kernelinfo, jobrefid); if (error) { TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list); if (type == 0) suword(&job->_aiocb_private.error, EINVAL); return error; } aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jobrefid; if (jobrefid == LONG_MAX) jobrefid = 1; else jobrefid++; if (opcode == LIO_NOP) { TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list); if (type == 0) { suword(&job->_aiocb_private.error, 0); suword(&job->_aiocb_private.status, 0); suword(&job->_aiocb_private.kernelinfo, 0); } return 0; } if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) { TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list); if (type == 0) { suword(&job->_aiocb_private.status, 0); suword(&job->_aiocb_private.error, EINVAL); } return EINVAL; } fhold(fp); if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) { kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue; kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sigval_ptr; } else { /* * This method for requesting kevent-based notification won't * work on the alpha, since we're passing in a pointer * via aio_lio_opcode, which is an int. Use the SIGEV_KEVENT- * based method instead. */ struct kevent *kevp; kevp = (struct kevent *)job->aio_lio_opcode; if (kevp == NULL) goto no_kqueue; error = copyin((caddr_t)kevp, (caddr_t)&kev, sizeof(kev)); if (error) goto aqueue_fail; } if ((u_int)kev.ident >= fdp->fd_nfiles || (kq_fp = fdp->fd_ofiles[kev.ident]) == NULL || (kq_fp->f_type != DTYPE_KQUEUE)) { error = EBADF; goto aqueue_fail; } kq = (struct kqueue *)kq_fp->f_data; kev.ident = (uintptr_t)aiocbe; kev.filter = EVFILT_AIO; kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1; error = kqueue_register(kq, &kev, p); aqueue_fail: if (error) { TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list); if (type == 0) suword(&job->_aiocb_private.error, error); goto done; } no_kqueue: suword(&job->_aiocb_private.error, EINPROGRESS); aiocbe->uaiocb._aiocb_private.error = EINPROGRESS; aiocbe->userproc = p; aiocbe->jobflags = 0; aiocbe->lio = lj; ki = p->p_aioinfo; if (fp->f_type == DTYPE_SOCKET) { /* * Alternate queueing for socket ops: Reach down into the * descriptor to get the socket data. Then check to see if the * socket is ready to be read or written (based on the requested * operation). * * If it is not ready for io, then queue the aiocbe on the * socket, and set the flags so we get a call when sbnotify() * happens. */ so = (struct socket *)fp->f_data; s = splnet(); if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode == LIO_WRITE) && (!sowriteable(so)))) { TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list); TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist); if (opcode == LIO_READ) so->so_rcv.sb_flags |= SB_AIO; else so->so_snd.sb_flags |= SB_AIO; aiocbe->jobstate = JOBST_JOBQGLOBAL; /* XXX */ ki->kaio_queue_count++; num_queue_count++; splx(s); error = 0; goto done; } splx(s); } if ((error = aio_qphysio(p, aiocbe)) == 0) goto done; if (error > 0) { suword(&job->_aiocb_private.status, 0); aiocbe->uaiocb._aiocb_private.error = error; suword(&job->_aiocb_private.error, error); goto done; } /* No buffer for daemon I/O. */ aiocbe->bp = NULL; ki->kaio_queue_count++; if (lj) lj->lioj_queue_count++; s = splnet(); TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist); TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list); splx(s); aiocbe->jobstate = JOBST_JOBQGLOBAL; num_queue_count++; error = 0; /* * If we don't have a free AIO process, and we are below our quota, then * start one. Otherwise, depend on the subsequent I/O completions to * pick-up this job. If we don't sucessfully create the new process * (thread) due to resource issues, we return an error for now (EAGAIN), * which is likely not the correct thing to do. */ retryproc: s = splnet(); if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { TAILQ_REMOVE(&aio_freeproc, aiop, list); TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list); aiop->aioprocflags &= ~AIOP_FREE; wakeup(aiop->aioproc); } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) && ((ki->kaio_active_count + num_aio_resv_start) < ki->kaio_maxactive_count)) { num_aio_resv_start++; if ((error = aio_newproc()) == 0) { num_aio_resv_start--; p->p_retval[0] = 0; goto retryproc; } num_aio_resv_start--; } splx(s); done: fdrop(fp, p); return error; } /* * This routine queues an AIO request, checking for quotas. */ static int aio_aqueue(struct proc *p, struct aiocb *job, int type) { struct kaioinfo *ki; if (p->p_aioinfo == NULL) aio_init_aioinfo(p); if (num_queue_count >= max_queue_count) return EAGAIN; ki = p->p_aioinfo; if (ki->kaio_queue_count >= ki->kaio_qallowed_count) return EAGAIN; return _aio_aqueue(p, job, NULL, type); } #endif /* VFS_AIO */ /* * Support the aio_return system call, as a side-effect, kernel resources are * released. */ int aio_return(struct proc *p, struct aio_return_args *uap) { #ifndef VFS_AIO return ENOSYS; #else int s; int jobref; struct aiocblist *cb, *ncb; struct aiocb *ujob; struct kaioinfo *ki; ki = p->p_aioinfo; if (ki == NULL) return EINVAL; ujob = uap->aiocbp; jobref = fuword(&ujob->_aiocb_private.kernelinfo); if (jobref == -1 || jobref == 0) return EINVAL; s = splnet(); for (cb = TAILQ_FIRST(&ki->kaio_jobdone); cb; cb = TAILQ_NEXT(cb, plist)) { if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) == jobref) { splx(s); if (ujob == cb->uuaiocb) { p->p_retval[0] = cb->uaiocb._aiocb_private.status; } else p->p_retval[0] = EFAULT; if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { curproc->p_stats->p_ru.ru_oublock += cb->outputcharge; cb->outputcharge = 0; } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { curproc->p_stats->p_ru.ru_inblock += cb->inputcharge; cb->inputcharge = 0; } aio_free_entry(cb); return 0; } } splx(s); s = splbio(); for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = ncb) { ncb = TAILQ_NEXT(cb, plist); if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) == jobref) { splx(s); if (ujob == cb->uuaiocb) { p->p_retval[0] = cb->uaiocb._aiocb_private.status; } else p->p_retval[0] = EFAULT; aio_free_entry(cb); return 0; } } splx(s); return (EINVAL); #endif /* VFS_AIO */ } /* * Allow a process to wakeup when any of the I/O requests are completed. */ int aio_suspend(struct proc *p, struct aio_suspend_args *uap) { #ifndef VFS_AIO return ENOSYS; #else struct timeval atv; struct timespec ts; struct aiocb *const *cbptr, *cbp; struct kaioinfo *ki; struct aiocblist *cb; int i; int njoblist; int error, s, timo; int *ijoblist; struct aiocb **ujoblist; if (uap->nent >= AIO_LISTIO_MAX) return EINVAL; timo = 0; if (uap->timeout) { /* Get timespec struct. */ if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0) return error; if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) return (EINVAL); TIMESPEC_TO_TIMEVAL(&atv, &ts); if (itimerfix(&atv)) return (EINVAL); timo = tvtohz(&atv); } ki = p->p_aioinfo; if (ki == NULL) return EAGAIN; njoblist = 0; ijoblist = zalloc(aiol_zone); ujoblist = zalloc(aiol_zone); cbptr = uap->aiocbp; for (i = 0; i < uap->nent; i++) { cbp = (struct aiocb *)(intptr_t)fuword((caddr_t)&cbptr[i]); if (cbp == 0) continue; ujoblist[njoblist] = cbp; ijoblist[njoblist] = fuword(&cbp->_aiocb_private.kernelinfo); njoblist++; } if (njoblist == 0) { zfree(aiol_zone, ijoblist); zfree(aiol_zone, ujoblist); return 0; } error = 0; for (;;) { for (cb = TAILQ_FIRST(&ki->kaio_jobdone); cb; cb = TAILQ_NEXT(cb, plist)) { for (i = 0; i < njoblist; i++) { if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) == ijoblist[i]) { if (ujoblist[i] != cb->uuaiocb) error = EINVAL; zfree(aiol_zone, ijoblist); zfree(aiol_zone, ujoblist); return error; } } } s = splbio(); for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb, plist)) { for (i = 0; i < njoblist; i++) { if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) == ijoblist[i]) { splx(s); if (ujoblist[i] != cb->uuaiocb) error = EINVAL; zfree(aiol_zone, ijoblist); zfree(aiol_zone, ujoblist); return error; } } } ki->kaio_flags |= KAIO_WAKEUP; error = tsleep(p, PRIBIO | PCATCH, "aiospn", timo); splx(s); if (error == ERESTART || error == EINTR) { zfree(aiol_zone, ijoblist); zfree(aiol_zone, ujoblist); return EINTR; } else if (error == EWOULDBLOCK) { zfree(aiol_zone, ijoblist); zfree(aiol_zone, ujoblist); return EAGAIN; } } /* NOTREACHED */ return EINVAL; #endif /* VFS_AIO */ } /* * aio_cancel cancels any non-physio aio operations not currently in * progress. */ int aio_cancel(struct proc *p, struct aio_cancel_args *uap) { #ifndef VFS_AIO return ENOSYS; #else struct kaioinfo *ki; struct aiocblist *cbe, *cbn; struct file *fp; struct filedesc *fdp; struct socket *so; struct proc *po; int s,error; int cancelled=0; int notcancelled=0; struct vnode *vp; fdp = p->p_fd; fp = fdp->fd_ofiles[uap->fd]; if (fp == NULL) { return EBADF; } if (fp->f_type == DTYPE_VNODE) { vp = (struct vnode *)fp->f_data; if (vn_isdisk(vp,&error)) { p->p_retval[0] = AIO_NOTCANCELED; return 0; } } else if (fp->f_type == DTYPE_SOCKET) { so = (struct socket *)fp->f_data; s = splnet(); for (cbe = TAILQ_FIRST(&so->so_aiojobq); cbe; cbe = cbn) { cbn = TAILQ_NEXT(cbe, list); if ((uap->aiocbp == NULL) || (uap->aiocbp == cbe->uuaiocb) ) { po = cbe->userproc; ki = po->p_aioinfo; TAILQ_REMOVE(&so->so_aiojobq, cbe, list); TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist); TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist); if (ki->kaio_flags & KAIO_WAKEUP) { wakeup(po); } cbe->jobstate = JOBST_JOBFINISHED; cbe->uaiocb._aiocb_private.status=-1; cbe->uaiocb._aiocb_private.error=ECANCELED; cancelled++; /* XXX cancelled, knote? */ if (cbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL) psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo); if (uap->aiocbp) break; } } splx(s); if ((cancelled) && (uap->aiocbp)) { p->p_retval[0] = AIO_CANCELED; return 0; } } ki=p->p_aioinfo; s = splnet(); for (cbe = TAILQ_FIRST(&ki->kaio_jobqueue); cbe; cbe = cbn) { cbn = TAILQ_NEXT(cbe, plist); if ((uap->fd == cbe->uaiocb.aio_fildes) && ((uap->aiocbp == NULL ) || (uap->aiocbp == cbe->uuaiocb))) { if (cbe->jobstate == JOBST_JOBQGLOBAL) { TAILQ_REMOVE(&aio_jobs, cbe, list); TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist); TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist); cancelled++; ki->kaio_queue_finished_count++; cbe->jobstate = JOBST_JOBFINISHED; cbe->uaiocb._aiocb_private.status = -1; cbe->uaiocb._aiocb_private.error = ECANCELED; /* XXX cancelled, knote? */ if (cbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL) psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo); } else { notcancelled++; } } } splx(s); if (notcancelled) { p->p_retval[0] = AIO_NOTCANCELED; return 0; } if (cancelled) { p->p_retval[0] = AIO_CANCELED; return 0; } p->p_retval[0] = AIO_ALLDONE; return 0; #endif /* VFS_AIO */ } /* * aio_error is implemented in the kernel level for compatibility purposes only. * For a user mode async implementation, it would be best to do it in a userland * subroutine. */ int aio_error(struct proc *p, struct aio_error_args *uap) { #ifndef VFS_AIO return ENOSYS; #else int s; struct aiocblist *cb; struct kaioinfo *ki; int jobref; ki = p->p_aioinfo; if (ki == NULL) return EINVAL; jobref = fuword(&uap->aiocbp->_aiocb_private.kernelinfo); if ((jobref == -1) || (jobref == 0)) return EINVAL; for (cb = TAILQ_FIRST(&ki->kaio_jobdone); cb; cb = TAILQ_NEXT(cb, plist)) { if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == jobref) { p->p_retval[0] = cb->uaiocb._aiocb_private.error; return 0; } } s = splnet(); for (cb = TAILQ_FIRST(&ki->kaio_jobqueue); cb; cb = TAILQ_NEXT(cb, plist)) { if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == jobref) { p->p_retval[0] = EINPROGRESS; splx(s); return 0; } } for (cb = TAILQ_FIRST(&ki->kaio_sockqueue); cb; cb = TAILQ_NEXT(cb, plist)) { if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == jobref) { p->p_retval[0] = EINPROGRESS; splx(s); return 0; } } splx(s); s = splbio(); for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb, plist)) { if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == jobref) { p->p_retval[0] = cb->uaiocb._aiocb_private.error; splx(s); return 0; } } for (cb = TAILQ_FIRST(&ki->kaio_bufqueue); cb; cb = TAILQ_NEXT(cb, plist)) { if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == jobref) { p->p_retval[0] = EINPROGRESS; splx(s); return 0; } } splx(s); #if (0) /* * Hack for lio. */ status = fuword(&uap->aiocbp->_aiocb_private.status); if (status == -1) return fuword(&uap->aiocbp->_aiocb_private.error); #endif return EINVAL; #endif /* VFS_AIO */ } int aio_read(struct proc *p, struct aio_read_args *uap) { #ifndef VFS_AIO return ENOSYS; #else struct filedesc *fdp; struct file *fp; struct uio auio; struct iovec aiov; unsigned int fd; int cnt; struct aiocb iocb; int error, pmodes; pmodes = fuword(&uap->aiocbp->_aiocb_private.privatemodes); if ((pmodes & AIO_PMODE_SYNC) == 0) return aio_aqueue(p, (struct aiocb *)uap->aiocbp, LIO_READ); /* Get control block. */ if ((error = copyin((caddr_t)uap->aiocbp, (caddr_t)&iocb, sizeof iocb)) != 0) return error; /* Get the fd info for process. */ fdp = p->p_fd; /* * Range check file descriptor. */ fd = iocb.aio_fildes; if (fd >= fdp->fd_nfiles) return EBADF; fp = fdp->fd_ofiles[fd]; if ((fp == NULL) || ((fp->f_flag & FREAD) == 0)) return EBADF; if (iocb.aio_offset == -1LL) return EINVAL; auio.uio_resid = iocb.aio_nbytes; if (auio.uio_resid < 0) return (EINVAL); /* * Process sync simply -- queue async request. */ if ((iocb._aiocb_private.privatemodes & AIO_PMODE_SYNC) == 0) return aio_aqueue(p, (struct aiocb *)uap->aiocbp, LIO_READ); aiov.iov_base = (void *)iocb.aio_buf; aiov.iov_len = iocb.aio_nbytes; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = iocb.aio_offset; auio.uio_rw = UIO_READ; auio.uio_segflg = UIO_USERSPACE; auio.uio_procp = p; cnt = iocb.aio_nbytes; /* * Temporarily bump the ref count while reading to avoid the * descriptor being ripped out from under us. */ fhold(fp); error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, p); fdrop(fp, p); if (error && (auio.uio_resid != cnt) && (error == ERESTART || error == EINTR || error == EWOULDBLOCK)) error = 0; cnt -= auio.uio_resid; p->p_retval[0] = cnt; return error; #endif /* VFS_AIO */ } int aio_write(struct proc *p, struct aio_write_args *uap) { #ifndef VFS_AIO return ENOSYS; #else struct filedesc *fdp; struct file *fp; struct uio auio; struct iovec aiov; unsigned int fd; int cnt; struct aiocb iocb; int error; int pmodes; /* * Process sync simply -- queue async request. */ pmodes = fuword(&uap->aiocbp->_aiocb_private.privatemodes); if ((pmodes & AIO_PMODE_SYNC) == 0) return aio_aqueue(p, (struct aiocb *)uap->aiocbp, LIO_WRITE); if ((error = copyin((caddr_t)uap->aiocbp, (caddr_t)&iocb, sizeof iocb)) != 0) return error; /* Get the fd info for process. */ fdp = p->p_fd; /* * Range check file descriptor. */ fd = iocb.aio_fildes; if (fd >= fdp->fd_nfiles) return EBADF; fp = fdp->fd_ofiles[fd]; if ((fp == NULL) || ((fp->f_flag & FWRITE) == 0)) return EBADF; if (iocb.aio_offset == -1LL) return EINVAL; aiov.iov_base = (void *)iocb.aio_buf; aiov.iov_len = iocb.aio_nbytes; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = iocb.aio_offset; auio.uio_resid = iocb.aio_nbytes; if (auio.uio_resid < 0) return (EINVAL); auio.uio_rw = UIO_WRITE; auio.uio_segflg = UIO_USERSPACE; auio.uio_procp = p; cnt = iocb.aio_nbytes; /* * Temporarily bump the ref count while writing to avoid the * descriptor being ripped out from under us. */ fhold(fp); error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, p); fdrop(fp, p); if (error) { if (auio.uio_resid != cnt) { if (error == ERESTART || error == EINTR || error == EWOULDBLOCK) error = 0; if (error == EPIPE) psignal(p, SIGPIPE); } } cnt -= auio.uio_resid; p->p_retval[0] = cnt; return error; #endif /* VFS_AIO */ } int lio_listio(struct proc *p, struct lio_listio_args *uap) { #ifndef VFS_AIO return ENOSYS; #else int nent, nentqueued; struct aiocb *iocb, * const *cbptr; struct aiocblist *cb; struct kaioinfo *ki; struct aio_liojob *lj; int error, runningcode; int nerror; int i; int s; if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) return EINVAL; nent = uap->nent; if (nent > AIO_LISTIO_MAX) return EINVAL; if (p->p_aioinfo == NULL) aio_init_aioinfo(p); if ((nent + num_queue_count) > max_queue_count) return EAGAIN; ki = p->p_aioinfo; if ((nent + ki->kaio_queue_count) > ki->kaio_qallowed_count) return EAGAIN; lj = zalloc(aiolio_zone); if (!lj) return EAGAIN; lj->lioj_flags = 0; lj->lioj_buffer_count = 0; lj->lioj_buffer_finished_count = 0; lj->lioj_queue_count = 0; lj->lioj_queue_finished_count = 0; lj->lioj_ki = ki; TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list); /* * Setup signal. */ if (uap->sig && (uap->mode == LIO_NOWAIT)) { error = copyin(uap->sig, &lj->lioj_signal, sizeof(lj->lioj_signal)); if (error) return error; lj->lioj_flags |= LIOJ_SIGNAL; lj->lioj_flags &= ~LIOJ_SIGNAL_POSTED; } else lj->lioj_flags &= ~LIOJ_SIGNAL; /* * Get pointers to the list of I/O requests. */ nerror = 0; nentqueued = 0; cbptr = uap->acb_list; for (i = 0; i < uap->nent; i++) { iocb = (struct aiocb *)(intptr_t)fuword((caddr_t)&cbptr[i]); if (((intptr_t)iocb != -1) && ((intptr_t)iocb != NULL)) { error = _aio_aqueue(p, iocb, lj, 0); if (error == 0) nentqueued++; else nerror++; } } /* * If we haven't queued any, then just return error. */ if (nentqueued == 0) return 0; /* * Calculate the appropriate error return. */ runningcode = 0; if (nerror) runningcode = EIO; if (uap->mode == LIO_WAIT) { int command, found, jobref; for (;;) { found = 0; for (i = 0; i < uap->nent; i++) { /* * Fetch address of the control buf pointer in * user space. */ iocb = (struct aiocb *)(intptr_t)fuword((caddr_t)&cbptr[i]); if (((intptr_t)iocb == -1) || ((intptr_t)iocb == 0)) continue; /* * Fetch the associated command from user space. */ command = fuword(&iocb->aio_lio_opcode); if (command == LIO_NOP) { found++; continue; } jobref = fuword(&iocb->_aiocb_private.kernelinfo); TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) { if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == jobref) { if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { curproc->p_stats->p_ru.ru_oublock += cb->outputcharge; cb->outputcharge = 0; } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { curproc->p_stats->p_ru.ru_inblock += cb->inputcharge; cb->inputcharge = 0; } found++; break; } } s = splbio(); TAILQ_FOREACH(cb, &ki->kaio_bufdone, plist) { if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == jobref) { found++; break; } } splx(s); } /* * If all I/Os have been disposed of, then we can * return. */ if (found == nentqueued) return runningcode; ki->kaio_flags |= KAIO_WAKEUP; error = tsleep(p, PRIBIO | PCATCH, "aiospn", 0); if (error == EINTR) return EINTR; else if (error == EWOULDBLOCK) return EAGAIN; } } return runningcode; #endif /* VFS_AIO */ } #ifdef VFS_AIO /* - * This is a wierd hack so that we can post a signal. It is safe to do so from + * This is a weird hack so that we can post a signal. It is safe to do so from * a timeout routine, but *not* from an interrupt routine. */ static void process_signal(void *aioj) { struct aiocblist *aiocbe = aioj; struct aio_liojob *lj = aiocbe->lio; struct aiocb *cb = &aiocbe->uaiocb; if ((lj) && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL) && (lj->lioj_queue_count == lj->lioj_queue_finished_count)) { psignal(lj->lioj_ki->kaio_p, lj->lioj_signal.sigev_signo); lj->lioj_flags |= LIOJ_SIGNAL_POSTED; } if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) psignal(aiocbe->userproc, cb->aio_sigevent.sigev_signo); } /* * Interrupt handler for physio, performs the necessary process wakeups, and * signals. */ static void aio_physwakeup(struct buf *bp) { struct aiocblist *aiocbe; struct proc *p; struct kaioinfo *ki; struct aio_liojob *lj; wakeup((caddr_t)bp); aiocbe = (struct aiocblist *)bp->b_spc; if (aiocbe) { p = bp->b_caller1; aiocbe->jobstate = JOBST_JOBBFINISHED; aiocbe->uaiocb._aiocb_private.status -= bp->b_resid; aiocbe->uaiocb._aiocb_private.error = 0; aiocbe->jobflags |= AIOCBLIST_DONE; if (bp->b_ioflags & BIO_ERROR) aiocbe->uaiocb._aiocb_private.error = bp->b_error; lj = aiocbe->lio; if (lj) { lj->lioj_buffer_finished_count++; /* * wakeup/signal if all of the interrupt jobs are done. */ if (lj->lioj_buffer_finished_count == lj->lioj_buffer_count) { /* * Post a signal if it is called for. */ if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) { lj->lioj_flags |= LIOJ_SIGNAL_POSTED; timeout(process_signal, aiocbe, 0); } } } ki = p->p_aioinfo; if (ki) { ki->kaio_buffer_finished_count++; TAILQ_REMOVE(&aio_bufjobs, aiocbe, list); TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist); TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist); KNOTE(&aiocbe->klist, 0); /* Do the wakeup. */ if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) { ki->kaio_flags &= ~KAIO_WAKEUP; wakeup(p); } } if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL) timeout(process_signal, aiocbe, 0); } } #endif /* VFS_AIO */ int aio_waitcomplete(struct proc *p, struct aio_waitcomplete_args *uap) { #ifndef VFS_AIO return ENOSYS; #else struct timeval atv; struct timespec ts; struct aiocb **cbptr; struct kaioinfo *ki; struct aiocblist *cb = NULL; int error, s, timo; suword(uap->aiocbp, (int)NULL); timo = 0; if (uap->timeout) { /* Get timespec struct. */ error = copyin((caddr_t)uap->timeout, (caddr_t)&ts, sizeof(ts)); if (error) return error; if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000)) return (EINVAL); TIMESPEC_TO_TIMEVAL(&atv, &ts); if (itimerfix(&atv)) return (EINVAL); timo = tvtohz(&atv); } ki = p->p_aioinfo; if (ki == NULL) return EAGAIN; cbptr = uap->aiocbp; for (;;) { if ((cb = TAILQ_FIRST(&ki->kaio_jobdone)) != 0) { suword(uap->aiocbp, (int)cb->uuaiocb); p->p_retval[0] = cb->uaiocb._aiocb_private.status; if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { curproc->p_stats->p_ru.ru_oublock += cb->outputcharge; cb->outputcharge = 0; } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { curproc->p_stats->p_ru.ru_inblock += cb->inputcharge; cb->inputcharge = 0; } aio_free_entry(cb); return cb->uaiocb._aiocb_private.error; } s = splbio(); if ((cb = TAILQ_FIRST(&ki->kaio_bufdone)) != 0 ) { splx(s); suword(uap->aiocbp, (int)cb->uuaiocb); p->p_retval[0] = cb->uaiocb._aiocb_private.status; aio_free_entry(cb); return cb->uaiocb._aiocb_private.error; } ki->kaio_flags |= KAIO_WAKEUP; error = tsleep(p, PRIBIO | PCATCH, "aiowc", timo); splx(s); if (error == ERESTART) return EINTR; else if (error < 0) return error; else if (error == EINTR) return EINTR; else if (error == EWOULDBLOCK) return EAGAIN; } #endif /* VFS_AIO */ } #ifndef VFS_AIO static int filt_aioattach(struct knote *kn) { return (ENXIO); } struct filterops aio_filtops = { 0, filt_aioattach, NULL, NULL }; #else static int filt_aioattach(struct knote *kn) { struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_id; /* * The aiocbe pointer must be validated before using it, so * registration is restricted to the kernel; the user cannot * set EV_FLAG1. */ if ((kn->kn_flags & EV_FLAG1) == 0) return (EPERM); kn->kn_flags &= ~EV_FLAG1; SLIST_INSERT_HEAD(&aiocbe->klist, kn, kn_selnext); return (0); } static void filt_aiodetach(struct knote *kn) { struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_id; int s = splhigh(); /* XXX no clue, so overkill */ SLIST_REMOVE(&aiocbe->klist, kn, knote, kn_selnext); splx(s); } /*ARGSUSED*/ static int filt_aio(struct knote *kn, long hint) { struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_id; kn->kn_data = 0; /* XXX data returned? */ if (aiocbe->jobstate != JOBST_JOBFINISHED && aiocbe->jobstate != JOBST_JOBBFINISHED) return (0); kn->kn_flags |= EV_EOF; return (1); } struct filterops aio_filtops = { 0, filt_aioattach, filt_aiodetach, filt_aio }; #endif /* VFS_AIO */ diff --git a/usr.bin/telnet/externs.h b/usr.bin/telnet/externs.h index 10e89acdee36..0ec33132ddec 100644 --- a/usr.bin/telnet/externs.h +++ b/usr.bin/telnet/externs.h @@ -1,489 +1,489 @@ /* * Copyright (c) 1988, 1990, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)externs.h 8.2 (Berkeley) 12/15/93 * $FreeBSD$ */ #ifndef BSD # define BSD 43 #endif /* - * ucb stdio.h defines BSD as something wierd + * ucb stdio.h defines BSD as something weird */ #if defined(sun) && defined(__svr4__) #define BSD 43 #endif #ifndef USE_TERMIO # if BSD > 43 || defined(SYSV_TERMIO) # define USE_TERMIO # endif #endif #include #include #if defined(CRAY) && !defined(NO_BSD_SETJMP) #include #endif #ifndef FILIO_H #include #else #include #endif #ifdef CRAY # include #endif /* CRAY */ #ifdef USE_TERMIO # ifndef VINTR # ifdef SYSV_TERMIO # include # else # include # define termio termios # endif # endif #endif #if defined(NO_CC_T) || !defined(USE_TERMIO) # if !defined(USE_TERMIO) typedef char cc_t; # else typedef unsigned char cc_t; # endif #endif #ifndef NO_STRING_H #include #endif #include #if defined(IPSEC) #include #if defined(IPSEC_POLICY_IPSEC) extern char *ipsec_policy_in; extern char *ipsec_policy_out; #endif #endif #ifndef _POSIX_VDISABLE # ifdef sun # include /* pick up VDISABLE definition, mayby */ # endif # ifdef VDISABLE # define _POSIX_VDISABLE VDISABLE # else # define _POSIX_VDISABLE ((cc_t)'\377') # endif #endif #define SUBBUFSIZE 256 #ifndef CRAY extern int errno; /* outside this world */ #endif /* !CRAY */ #if !defined(P) # ifdef __STDC__ # define P(x) x # else # define P(x) () # endif #endif extern int autologin, /* Autologin enabled */ skiprc, /* Don't process the ~/.telnetrc file */ eight, /* use eight bit mode (binary in and/or out */ family, /* address family of peer */ flushout, /* flush output */ connected, /* Are we connected to the other side? */ globalmode, /* Mode tty should be in */ In3270, /* Are we in 3270 mode? */ telnetport, /* Are we connected to the telnet port? */ localflow, /* Flow control handled locally */ restartany, /* If flow control, restart output on any character */ localchars, /* we recognize interrupt/quit */ donelclchars, /* the user has set "localchars" */ showoptions, net, /* Network file descriptor */ tin, /* Terminal input file descriptor */ tout, /* Terminal output file descriptor */ crlf, /* Should '\r' be mapped to (or )? */ autoflush, /* flush output when interrupting? */ autosynch, /* send interrupt characters with SYNCH? */ SYNCHing, /* Is the stream in telnet SYNCH mode? */ donebinarytoggle, /* the user has put us in binary */ dontlecho, /* do we suppress local echoing right now? */ crmod, netdata, /* Print out network data flow */ prettydump, /* Print "netdata" output in user readable format */ #if defined(unix) #if defined(TN3270) cursesdata, /* Print out curses data flow */ apitrace, /* Trace API transactions */ #endif /* defined(TN3270) */ termdata, /* Print out terminal data flow */ #endif /* defined(unix) */ debug, /* Debug level */ doaddrlookup, /* do a reverse lookup? */ clienteof; /* Client received EOF */ extern cc_t escape; /* Escape to command mode */ extern cc_t rlogin; /* Rlogin mode escape character */ #ifdef KLUDGELINEMODE extern cc_t echoc; /* Toggle local echoing */ #endif extern char *prompt; /* Prompt for command. */ extern char doopt[], dont[], will[], wont[], options[], /* All the little options */ *hostname; /* Who are we connected to? */ /* * We keep track of each side of the option negotiation. */ #define MY_STATE_WILL 0x01 #define MY_WANT_STATE_WILL 0x02 #define MY_STATE_DO 0x04 #define MY_WANT_STATE_DO 0x08 /* * Macros to check the current state of things */ #define my_state_is_do(opt) (options[opt]&MY_STATE_DO) #define my_state_is_will(opt) (options[opt]&MY_STATE_WILL) #define my_want_state_is_do(opt) (options[opt]&MY_WANT_STATE_DO) #define my_want_state_is_will(opt) (options[opt]&MY_WANT_STATE_WILL) #define my_state_is_dont(opt) (!my_state_is_do(opt)) #define my_state_is_wont(opt) (!my_state_is_will(opt)) #define my_want_state_is_dont(opt) (!my_want_state_is_do(opt)) #define my_want_state_is_wont(opt) (!my_want_state_is_will(opt)) #define set_my_state_do(opt) {options[opt] |= MY_STATE_DO;} #define set_my_state_will(opt) {options[opt] |= MY_STATE_WILL;} #define set_my_want_state_do(opt) {options[opt] |= MY_WANT_STATE_DO;} #define set_my_want_state_will(opt) {options[opt] |= MY_WANT_STATE_WILL;} #define set_my_state_dont(opt) {options[opt] &= ~MY_STATE_DO;} #define set_my_state_wont(opt) {options[opt] &= ~MY_STATE_WILL;} #define set_my_want_state_dont(opt) {options[opt] &= ~MY_WANT_STATE_DO;} #define set_my_want_state_wont(opt) {options[opt] &= ~MY_WANT_STATE_WILL;} /* * Make everything symetrical */ #define HIS_STATE_WILL MY_STATE_DO #define HIS_WANT_STATE_WILL MY_WANT_STATE_DO #define HIS_STATE_DO MY_STATE_WILL #define HIS_WANT_STATE_DO MY_WANT_STATE_WILL #define his_state_is_do my_state_is_will #define his_state_is_will my_state_is_do #define his_want_state_is_do my_want_state_is_will #define his_want_state_is_will my_want_state_is_do #define his_state_is_dont my_state_is_wont #define his_state_is_wont my_state_is_dont #define his_want_state_is_dont my_want_state_is_wont #define his_want_state_is_wont my_want_state_is_dont #define set_his_state_do set_my_state_will #define set_his_state_will set_my_state_do #define set_his_want_state_do set_my_want_state_will #define set_his_want_state_will set_my_want_state_do #define set_his_state_dont set_my_state_wont #define set_his_state_wont set_my_state_dont #define set_his_want_state_dont set_my_want_state_wont #define set_his_want_state_wont set_my_want_state_dont extern FILE *NetTrace; /* Where debugging output goes */ extern unsigned char NetTraceFile[]; /* Name of file where debugging output goes */ extern void SetNetTrace P((char *)); /* Function to change where debugging goes */ extern jmp_buf peerdied, toplevel; /* For error conditions. */ extern void command P((int, char *, int)), Dump P((int, unsigned char *, int)), init_3270 P((void)), printoption P((char *, int, int)), printsub P((int, unsigned char *, int)), sendnaws P((void)), setconnmode P((int)), setcommandmode P((void)), setneturg P((void)), sys_telnet_init P((void)), telnet P((char *)), tel_enter_binary P((int)), TerminalFlushOutput P((void)), TerminalNewMode P((int)), TerminalRestoreState P((void)), TerminalSaveState P((void)), tninit P((void)), upcase P((char *)), willoption P((int)), wontoption P((int)); extern void send_do P((int, int)), send_dont P((int, int)), send_will P((int, int)), send_wont P((int, int)); extern void lm_will P((unsigned char *, int)), lm_wont P((unsigned char *, int)), lm_do P((unsigned char *, int)), lm_dont P((unsigned char *, int)), lm_mode P((unsigned char *, int, int)); extern void slc_init P((void)), slcstate P((void)), slc_mode_export P((void)), slc_mode_import P((int)), slc_import P((int)), slc_export P((void)), slc P((unsigned char *, int)), slc_check P((void)), slc_start_reply P((void)), slc_add_reply P((int, int, int)), slc_end_reply P((void)); extern int slc_update P((void)); extern void env_opt P((unsigned char *, int)), env_opt_start P((void)), env_opt_start_info P((void)), env_opt_add P((unsigned char *)), env_opt_end P((int)); extern unsigned char *env_default P((int, int)), *env_getvalue P((unsigned char *)); extern int get_status P((void)), dosynch P((void)); extern cc_t *tcval P((int)); #ifndef USE_TERMIO extern struct tchars ntc; extern struct ltchars nltc; extern struct sgttyb nttyb; # define termEofChar ntc.t_eofc # define termEraseChar nttyb.sg_erase # define termFlushChar nltc.t_flushc # define termIntChar ntc.t_intrc # define termKillChar nttyb.sg_kill # define termLiteralNextChar nltc.t_lnextc # define termQuitChar ntc.t_quitc # define termSuspChar nltc.t_suspc # define termRprntChar nltc.t_rprntc # define termWerasChar nltc.t_werasc # define termStartChar ntc.t_startc # define termStopChar ntc.t_stopc # define termForw1Char ntc.t_brkc extern cc_t termForw2Char; extern cc_t termAytChar; # define termEofCharp (cc_t *)&ntc.t_eofc # define termEraseCharp (cc_t *)&nttyb.sg_erase # define termFlushCharp (cc_t *)&nltc.t_flushc # define termIntCharp (cc_t *)&ntc.t_intrc # define termKillCharp (cc_t *)&nttyb.sg_kill # define termLiteralNextCharp (cc_t *)&nltc.t_lnextc # define termQuitCharp (cc_t *)&ntc.t_quitc # define termSuspCharp (cc_t *)&nltc.t_suspc # define termRprntCharp (cc_t *)&nltc.t_rprntc # define termWerasCharp (cc_t *)&nltc.t_werasc # define termStartCharp (cc_t *)&ntc.t_startc # define termStopCharp (cc_t *)&ntc.t_stopc # define termForw1Charp (cc_t *)&ntc.t_brkc # define termForw2Charp (cc_t *)&termForw2Char # define termAytCharp (cc_t *)&termAytChar # else extern struct termio new_tc; # define termEofChar new_tc.c_cc[VEOF] # define termEraseChar new_tc.c_cc[VERASE] # define termIntChar new_tc.c_cc[VINTR] # define termKillChar new_tc.c_cc[VKILL] # define termQuitChar new_tc.c_cc[VQUIT] # ifndef VSUSP extern cc_t termSuspChar; # else # define termSuspChar new_tc.c_cc[VSUSP] # endif # if defined(VFLUSHO) && !defined(VDISCARD) # define VDISCARD VFLUSHO # endif # ifndef VDISCARD extern cc_t termFlushChar; # else # define termFlushChar new_tc.c_cc[VDISCARD] # endif # ifndef VWERASE extern cc_t termWerasChar; # else # define termWerasChar new_tc.c_cc[VWERASE] # endif # ifndef VREPRINT extern cc_t termRprntChar; # else # define termRprntChar new_tc.c_cc[VREPRINT] # endif # ifndef VLNEXT extern cc_t termLiteralNextChar; # else # define termLiteralNextChar new_tc.c_cc[VLNEXT] # endif # ifndef VSTART extern cc_t termStartChar; # else # define termStartChar new_tc.c_cc[VSTART] # endif # ifndef VSTOP extern cc_t termStopChar; # else # define termStopChar new_tc.c_cc[VSTOP] # endif # ifndef VEOL extern cc_t termForw1Char; # else # define termForw1Char new_tc.c_cc[VEOL] # endif # ifndef VEOL2 extern cc_t termForw2Char; # else # define termForw2Char new_tc.c_cc[VEOL] # endif # ifndef VSTATUS extern cc_t termAytChar; #else # define termAytChar new_tc.c_cc[VSTATUS] #endif # if !defined(CRAY) || defined(__STDC__) # define termEofCharp &termEofChar # define termEraseCharp &termEraseChar # define termIntCharp &termIntChar # define termKillCharp &termKillChar # define termQuitCharp &termQuitChar # define termSuspCharp &termSuspChar # define termFlushCharp &termFlushChar # define termWerasCharp &termWerasChar # define termRprntCharp &termRprntChar # define termLiteralNextCharp &termLiteralNextChar # define termStartCharp &termStartChar # define termStopCharp &termStopChar # define termForw1Charp &termForw1Char # define termForw2Charp &termForw2Char # define termAytCharp &termAytChar # else /* Work around a compiler bug */ # define termEofCharp 0 # define termEraseCharp 0 # define termIntCharp 0 # define termKillCharp 0 # define termQuitCharp 0 # define termSuspCharp 0 # define termFlushCharp 0 # define termWerasCharp 0 # define termRprntCharp 0 # define termLiteralNextCharp 0 # define termStartCharp 0 # define termStopCharp 0 # define termForw1Charp 0 # define termForw2Charp 0 # define termAytCharp 0 # endif #endif /* Ring buffer structures which are shared */ extern Ring netoring, netiring, ttyoring, ttyiring; /* Tn3270 section */ #if defined(TN3270) extern int HaveInput, /* Whether an asynchronous I/O indication came in */ noasynchtty, /* Don't do signals on I/O (SIGURG, SIGIO) */ noasynchnet, /* Don't do signals on I/O (SIGURG, SIGIO) */ sigiocount, /* Count of SIGIO receptions */ shell_active; /* Subshell is active */ extern char *Ibackp, /* Oldest byte of 3270 data */ Ibuf[], /* 3270 buffer */ *Ifrontp, /* Where next 3270 byte goes */ tline[], *transcom; /* Transparent command */ extern int settranscom P((int, char**)); extern void inputAvailable P((int)); #endif /* defined(TN3270) */