134506Skarels /* 234506Skarels * Copyright (c) 1988 Regents of the University of California. 334506Skarels * All rights reserved. 434506Skarels * 534506Skarels * Redistribution and use in source and binary forms are permitted 634866Sbostic * provided that the above copyright notice and this paragraph are 734866Sbostic * duplicated in all such forms and that any documentation, 834866Sbostic * advertising materials, and other materials related to such 934866Sbostic * distribution and use acknowledge that the software was developed 1034866Sbostic * by the University of California, Berkeley. The name of the 1134866Sbostic * University may not be used to endorse or promote products derived 1234866Sbostic * from this software without specific prior written permission. 1334866Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434866Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534866Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1634506Skarels * 17*34977Sbostic * @(#)mp.c 7.4 (Berkeley) 07/08/88 1834506Skarels */ 1932633Ssam 2032633Ssam #include "mp.h" 2132633Ssam #if NMP > 0 2232633Ssam /* 2332633Ssam * Multi Protocol Communications Controller (MPCC). 2432633Ssam * Asynchronous Terminal Protocol Support. 2532633Ssam */ 2632633Ssam #include "param.h" 2732633Ssam #include "ioctl.h" 2832633Ssam #include "tty.h" 2932633Ssam #include "dir.h" 3032633Ssam #include "user.h" 3132633Ssam #include "map.h" 3232633Ssam #include "buf.h" 3332633Ssam #include "conf.h" 3432633Ssam #include "file.h" 3532633Ssam #include "uio.h" 3632633Ssam #include "errno.h" 3732633Ssam #include "syslog.h" 3832633Ssam #include "vmmac.h" 3932633Ssam #include "kernel.h" 4032633Ssam #include "clist.h" 4132633Ssam 4234506Skarels #include "../machine/pte.h" 4334506Skarels #include "../machine/mtpr.h" 4434506Skarels 4532633Ssam #include "../tahoevba/vbavar.h" 4632633Ssam #include "../tahoevba/mpreg.h" 4732633Ssam 4832633Ssam #define MPCHUNK 16 4932633Ssam #define MPPORT(n) ((n) & 0xf) 5032633Ssam #define MPUNIT(n) ((n) >> 4) 5132633Ssam 5232633Ssam /* 5332633Ssam * Driver information for auto-configuration stuff. 5432633Ssam */ 5532633Ssam int mpprobe(), mpattach(), mpintr(); 5632633Ssam struct vba_device *mpinfo[NMP]; 5732633Ssam long mpstd[] = { 0 }; 5832633Ssam struct vba_driver mpdriver = 5932633Ssam { mpprobe, 0, mpattach, 0, mpstd, "mp", mpinfo }; 6032633Ssam 6132633Ssam int mpstart(); 6232633Ssam struct mpevent *mpparam(); 6332633Ssam struct mpevent *mp_getevent(); 6432633Ssam 6532633Ssam /* 6632633Ssam * The following structure is needed to deal with mpcc's convoluted 6732633Ssam * method for locating it's mblok structures (hold your stomach). 6832633Ssam * When an mpcc is reset at boot time it searches host memory 6932633Ssam * looking for a string that says ``ThIs Is MpCc''. The mpcc 7032633Ssam * then reads the structure to locate the pointer to it's mblok 7132633Ssam * structure (you can wretch now). 7232633Ssam */ 7332633Ssam struct mpbogus { 7432633Ssam char s[12]; /* `ThIs Is MpCc'' */ 7532633Ssam u_char status; 7632633Ssam u_char unused; 7732633Ssam u_short magic; 7832633Ssam struct mblok *mb; 7932633Ssam struct mblok *mbloks[NMP]; /* can support at most 16 mpcc's */ 8032633Ssam } mpbogus = { 'T','h','I','s',' ','I','s',' ','M','p','C','c' }; 8132633Ssam 8232633Ssam /* 8332633Ssam * Software state per unit. 8432633Ssam */ 8532633Ssam struct mpsoftc { 8632633Ssam u_int ms_ivec; /* interrupt vector */ 8732633Ssam u_int ms_softCAR; /* software carrier for async */ 8832633Ssam struct mblok *ms_mb; /* mpcc status area */ 8932633Ssam struct vb_buf ms_buf; /* vba resources for ms_mb */ 9032633Ssam struct hxmtl ms_hxl[MPMAXPORT];/* host transmit list */ 9132633Ssam struct asyncparam ms_async[MPMAXPORT][MPINSET];/* async structs */ 9232633Ssam char ms_cbuf[MPMAXPORT][MPOUTSET][CBSIZE];/* input character buffers */ 9332633Ssam } mp_softc[NMP]; 9432633Ssam 9532633Ssam struct tty mp_tty[NMP*MPCHUNK]; 9632633Ssam #ifndef lint 9732633Ssam int nmp = NMP*MPCHUNK; 9832633Ssam #endif 9932633Ssam 10032633Ssam int ttrstrt(); 10132633Ssam 10232633Ssam mpprobe(reg, vi) 10332633Ssam caddr_t reg; 10432633Ssam struct vba_device *vi; 10532633Ssam { 10632633Ssam register int br, cvec; 10732633Ssam register struct mpsoftc *ms; 10832633Ssam 10932633Ssam #ifdef lint 11032633Ssam br = 0; cvec = br; br = cvec; 11132633Ssam mpintr(0); 11234506Skarels mpdlintr(0); 11332633Ssam #endif 11432633Ssam if (badaddr(reg, 2)) 11532633Ssam return (0); 11632633Ssam ms = &mp_softc[vi->ui_unit]; 11732633Ssam /* 11832633Ssam * Allocate page tables and mblok 11932633Ssam * structure (mblok in non-cached memory). 12032633Ssam */ 12132633Ssam if (vbainit(&ms->ms_buf, sizeof (struct mblok), VB_32BIT) == 0) { 12232633Ssam printf("mp%d: vbainit failed\n", vi->ui_unit); 12332633Ssam return (0); 12432633Ssam } 12532633Ssam ms->ms_mb = (struct mblok *)ms->ms_buf.vb_rawbuf; 12632633Ssam ms->ms_ivec = MPINTRBASE + 2*vi->ui_unit; /* XXX */ 12732633Ssam br = 0x14, cvec = ms->ms_ivec; /* XXX */ 12834287Skarels return (sizeof (*reg)); 12932633Ssam } 13032633Ssam 13132633Ssam mpattach(vi) 13232633Ssam register struct vba_device *vi; 13332633Ssam { 13432633Ssam register struct mpsoftc *ms = &mp_softc[vi->ui_unit]; 13532633Ssam 13632633Ssam ms->ms_softCAR = vi->ui_flags; 13732633Ssam /* 13832633Ssam * Setup pointer to mblok, initialize bogus 13932633Ssam * status block used by mpcc to locate the pointer 14032633Ssam * and then poke the mpcc to get it to search host 14132633Ssam * memory to find mblok pointer. 14232633Ssam */ 14332633Ssam mpbogus.mbloks[vi->ui_unit] = (struct mblok *)ms->ms_buf.vb_physbuf; 14432633Ssam *(short *)vi->ui_addr = 0x100; /* magic */ 14532633Ssam } 14632633Ssam 14732633Ssam /* 14832633Ssam * Open an mpcc port. 14932633Ssam */ 15034506Skarels /* ARGSUSED */ 15132633Ssam mpopen(dev, mode) 15232633Ssam dev_t dev; 15332633Ssam { 15432633Ssam register struct tty *tp; 15532633Ssam register struct mpsoftc *ms; 15632633Ssam int error, s, port, unit, mpu; 15732633Ssam struct vba_device *vi; 15832633Ssam struct mpport *mp; 15932633Ssam struct mpevent *ev; 16032633Ssam 16132633Ssam unit = minor(dev); 16232633Ssam mpu = MPUNIT(unit); 16332633Ssam if (mpu >= NMP || (vi = mpinfo[mpu]) == 0 || vi->ui_alive == 0) 16432633Ssam return (ENXIO); 16532633Ssam tp = &mp_tty[unit]; 16632633Ssam if (tp->t_state & TS_XCLUDE && u.u_uid != 0) 16732633Ssam return (EBUSY); 16832633Ssam ms = &mp_softc[mpu]; 16932633Ssam port = MPPORT(unit); 17032633Ssam if (ms->ms_mb->mb_proto[port] != MPPROTO_ASYNC || 17132633Ssam ms->ms_mb->mb_status != MP_OPOPEN) 17232633Ssam return (ENXIO); 17332633Ssam mp = &ms->ms_mb->mb_port[port]; /* host mpcc struct */ 17432633Ssam s = spl8(); 17532633Ssam while (mp->mp_flags & MP_PROGRESS) 17632633Ssam sleep((caddr_t)&tp->t_canq, TTIPRI); 17732633Ssam while (tp->t_state & TS_WOPEN) 17832633Ssam sleep((caddr_t)&tp->t_canq, TTIPRI); 17932633Ssam if (tp->t_state & TS_ISOPEN) { 18032633Ssam splx(s); 18132633Ssam return (0); 18232633Ssam } 18332633Ssam tp->t_state |= TS_WOPEN; 18432633Ssam tp->t_addr = (caddr_t)ms; 18532633Ssam tp->t_oproc = mpstart; 18632633Ssam tp->t_dev = dev; 18732633Ssam ttychars(tp); 18832633Ssam if (tp->t_ispeed == 0) { 18932633Ssam tp->t_ispeed = B9600; 19032633Ssam tp->t_ospeed = B9600; 19132633Ssam tp->t_flags |= ODDP|EVENP|ECHO; 19232633Ssam } 19332633Ssam /* 19432633Ssam * Initialize port state: init MPCC interface 19532633Ssam * structures for port and setup modem control. 19632633Ssam */ 19732633Ssam mp->mp_proto = MPPROTO_ASYNC; /* XXX */ 19832633Ssam error = mpportinit(ms, mp, port); 19932633Ssam if (error) 20032633Ssam goto bad; 20132633Ssam ev = mpparam(unit); 20232633Ssam if (ev == 0) { 20332633Ssam error = ENOBUFS; 20432633Ssam goto bad; 20532633Ssam } 206*34977Sbostic mpmodem(unit, MMOD_ON); 20732633Ssam mpcmd(ev, EVCMD_OPEN, 0, ms->ms_mb, port); 20832633Ssam while ((tp->t_state & TS_CARR_ON) == 0) 20932633Ssam sleep((caddr_t)&tp->t_rawq, TTIPRI); 21032633Ssam error = (*linesw[tp->t_line].l_open)(dev,tp); 21132633Ssam done: 21232633Ssam splx(s); 21332633Ssam /* wakeup anyone waiting for open to complete */ 21432633Ssam wakeup((caddr_t)&tp->t_canq); 21532633Ssam 21632633Ssam return (error); 21732633Ssam bad: 21832633Ssam tp->t_state &= ~TS_WOPEN; 21932633Ssam goto done; 22032633Ssam } 22132633Ssam 22232633Ssam /* 22332633Ssam * Close an mpcc port. 22432633Ssam */ 22534506Skarels /* ARGSUSED */ 22634506Skarels mpclose(dev, flag) 22732633Ssam dev_t dev; 22832633Ssam { 22932633Ssam register struct tty *tp; 23032633Ssam register struct mpport *mp; 23132633Ssam register struct mpevent *ev; 23232633Ssam int s, port, unit, error; 23332633Ssam struct mblok *mb; 23432633Ssam 23532633Ssam unit = minor(dev); 23632633Ssam tp = &mp_tty[unit]; 23732633Ssam port = MPPORT(unit); 23832633Ssam mb = mp_softc[MPUNIT(unit)].ms_mb; 23932633Ssam mp = &mb->mb_port[port]; 24032633Ssam s = spl8(); 24132633Ssam if (mp->mp_flags & MP_PROGRESS) { /* close in progress */ 24232633Ssam if (mp->mp_flags & MP_REMBSY) { 24332633Ssam mp->mp_flags &= ~MP_REMBSY; 24432633Ssam splx(s); 24532633Ssam return (0); 24632633Ssam } 24732633Ssam while (mp->mp_flags & MP_PROGRESS) 24832633Ssam sleep((caddr_t)&tp->t_canq,TTIPRI); 24932633Ssam } 25032633Ssam error = 0; 25132633Ssam mp->mp_flags |= MP_PROGRESS; 25232633Ssam (*linesw[tp->t_line].l_close)(tp); 25332633Ssam ev = mp_getevent(mp, unit); 25432633Ssam if (ev == 0) { 255*34977Sbostic error = ENOBUFS; 256*34977Sbostic mp->mp_flags &= ~MP_PROGRESS; 257*34977Sbostic goto out; 25832633Ssam } 259*34977Sbostic if (tp->t_state & TS_HUPCLS || (tp->t_state & TS_ISOPEN) == 0) 260*34977Sbostic mpmodem(unit, MMOD_OFF); 261*34977Sbostic else 262*34977Sbostic mpmodem(unit, MMOD_ON); 26332633Ssam mpcmd(ev, EVCMD_CLOSE, 0, mb, port); 264*34977Sbostic ttyclose(tp); 26532633Ssam out: 26632633Ssam if (mp->mp_flags & MP_REMBSY) 26732633Ssam mpclean(mb, port); 26832633Ssam splx(s); 26932633Ssam return (error); 27032633Ssam } 27132633Ssam 27232633Ssam /* 27332633Ssam * Read from an mpcc port. 27432633Ssam */ 27532633Ssam mpread(dev, uio) 27632633Ssam dev_t dev; 27732633Ssam struct uio *uio; 27832633Ssam { 27932633Ssam struct tty *tp; 28032633Ssam 28132633Ssam tp = &mp_tty[minor(dev)]; 28232633Ssam return ((*linesw[tp->t_line].l_read)(tp, uio)); 28332633Ssam } 28432633Ssam 28532633Ssam /* 28632633Ssam * Write to an mpcc port. 28732633Ssam */ 28832633Ssam mpwrite(dev, uio) 28932633Ssam dev_t dev; 29032633Ssam struct uio *uio; 29132633Ssam { 29232633Ssam struct tty *tp; 29332633Ssam 29432633Ssam tp = &mp_tty[minor(dev)]; 29532633Ssam return ((*linesw[tp->t_line].l_write)(tp, uio)); 29632633Ssam } 29732633Ssam 29832633Ssam /* 29932633Ssam * Ioctl for a mpcc port 30032633Ssam */ 30132633Ssam mpioctl(dev, cmd, data, flag) 30232633Ssam dev_t dev; 30332633Ssam caddr_t data; 30432633Ssam { 30532633Ssam register struct tty *tp; 30632633Ssam register struct mpsoftc *ms; 30732633Ssam register struct mpevent *ev; 30832633Ssam register struct mpport *mp; 30932633Ssam int s, port, error, unit; 31032633Ssam struct mblok *mb; 31132633Ssam 31232633Ssam unit = minor(dev); 31332633Ssam tp = &mp_tty[unit]; 31432633Ssam ms = &mp_softc[MPUNIT(unit)]; 31532633Ssam mb = ms->ms_mb; 31632633Ssam error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag); 31732633Ssam if (error >= 0) 31832633Ssam return (error); 31932633Ssam error = ttioctl(tp, cmd, data, flag); 32032633Ssam if (error >= 0) { 32132633Ssam if (cmd == TIOCSETP || cmd == TIOCSETN || cmd == TIOCLBIS || 32234796Sbostic cmd == TIOCLBIC || cmd == TIOCLSET || cmd == TIOCSETC) { 32332633Ssam ev = mpparam(unit); 32432633Ssam if (ev == 0) 32532633Ssam error = ENOBUFS; 32632633Ssam else 32732633Ssam mpcmd(ev, EVCMD_IOCTL, A_CHGALL, mb, 32832633Ssam MPPORT(unit)); 32932633Ssam } 33032633Ssam return (error); 33132633Ssam } 33232633Ssam switch (cmd) { 33332633Ssam case TIOCSBRK: /* send break */ 33432633Ssam case TIOCCBRK: /* clear break */ 33532633Ssam port = MPPORT(unit); 33632633Ssam mp = &mb->mb_port[port]; 33732633Ssam s = spl8(); 33832633Ssam ev = mp_getevent(mp, unit); 33932633Ssam if (ev) 34032633Ssam mpcmd(ev, EVCMD_IOCTL, 34132633Ssam (cmd == TIOCSBRK ? A_BRKON : A_BRKOFF), 34232633Ssam mb, port); 34332633Ssam else 34432633Ssam error = ENOBUFS; 34532633Ssam splx(s); 34632633Ssam break; 34732633Ssam case TIOCSDTR: /* set dtr control line */ 34832633Ssam break; 34932633Ssam case TIOCCDTR: /* clear dtr control line */ 35032633Ssam break; 35132633Ssam default: 35232633Ssam error = ENOTTY; 35332633Ssam break; 35432633Ssam } 35532633Ssam return (error); 35632633Ssam } 35732633Ssam 35832633Ssam struct mpevent * 35932633Ssam mpparam(unit) 36032633Ssam int unit; 36132633Ssam { 36232633Ssam register struct mpevent *ev; 36332633Ssam register struct mpport *mp; 36432633Ssam register struct tty *tp; 36532633Ssam struct mblok *mb; 36632633Ssam struct mpsoftc *ms; 36732633Ssam register struct asyncparam *asp; 36832633Ssam int port; 36932633Ssam 37032633Ssam ms = &mp_softc[MPUNIT(unit)]; 37132633Ssam mb = ms->ms_mb; 37232633Ssam port = MPPORT(unit); 37332633Ssam mp = &mb->mb_port[port]; 37432633Ssam ev = mp_getevent(mp, unit); /* XXX */ 37532633Ssam if (ev == 0) 37632633Ssam return (ev); 37732633Ssam tp = &mp_tty[unit]; 37832633Ssam /* YUCK */ 37932633Ssam asp = &ms->ms_async[port][mp->mp_on?mp->mp_on-1:MPINSET-1]; 38034796Sbostic asp->ap_xon = (u_char)tp->t_startc; 38134796Sbostic asp->ap_xoff = (u_char)tp->t_stopc; 38234796Sbostic if ((tp->t_flags & RAW) || (tp->t_stopc == -1) || (tp->t_startc == -1)) 38334796Sbostic asp->ap_xena = MPA_DIS; 38434796Sbostic else 38534796Sbostic asp->ap_xena = MPA_ENA; 38633995Sbostic asp->ap_xany = ((tp->t_flags & DECCTQ) ? MPA_DIS : MPA_ENA); 38732633Ssam #ifdef notnow 38832633Ssam if (tp->t_flags & (RAW|LITOUT|PASS8)) { 38932633Ssam #endif 39032633Ssam asp->ap_data = MPCHAR_8; 39132633Ssam asp->ap_parity = MPPAR_NONE; 39232633Ssam #ifdef notnow 39332633Ssam } else { 39432633Ssam asp->ap_data = MPCHAR_7; 39532633Ssam if ((tp->t_flags & (EVENP|ODDP)) == ODDP) 39632633Ssam asp->ap_parity = MPPAR_ODD; 39732633Ssam else 39832633Ssam asp->ap_parity = MPPAR_EVEN; 39932633Ssam } 40032633Ssam #endif 40132633Ssam if (tp->t_ospeed == B110) 40232633Ssam asp->ap_stop = MPSTOP_2; 40332633Ssam else 40432633Ssam asp->ap_stop = MPSTOP_1; 40532633Ssam if (tp->t_ospeed == EXTA || tp->t_ospeed == EXTB) 40632633Ssam asp->ap_baud = M19200; 40732633Ssam else 40832633Ssam asp->ap_baud = tp->t_ospeed; 40932633Ssam asp->ap_loop = MPA_DIS; /* disable loopback */ 41032633Ssam asp->ap_rtimer = A_RCVTIM; /* default receive timer */ 41132633Ssam if (ms->ms_softCAR & (1<<port)) 41232633Ssam setm(&asp->ap_modem, A_DTR, ASSERT); 41332633Ssam else 41432633Ssam setm(&asp->ap_modem, A_DTR, AUTO); 41532633Ssam seti(&asp->ap_intena, A_DCD); 41632633Ssam return (ev); 41732633Ssam } 41832633Ssam 41932633Ssam mpstart(tp) 42032633Ssam register struct tty *tp; 42132633Ssam { 42232633Ssam register struct mpevent *ev; 42332633Ssam register struct mpport *mp; 42432633Ssam struct mblok *mb; 42532633Ssam struct mpsoftc *ms; 42632633Ssam int port, unit, xcnt, n, s, i; 42732633Ssam struct hxmtl *hxp; 42832633Ssam struct clist outq; 42932633Ssam 43032633Ssam s = spl8(); 43132633Ssam unit = minor(tp->t_dev); 43232633Ssam ms = &mp_softc[MPUNIT(unit)]; 43332633Ssam mb = ms->ms_mb; 43432633Ssam port = MPPORT(unit); 43532633Ssam mp = &mb->mb_port[port]; 43632633Ssam hxp = &ms->ms_hxl[port]; 43732633Ssam xcnt = 0; 43832633Ssam outq = tp->t_outq; 43932633Ssam for (i = 0; i < MPXMIT; i++) { 44032633Ssam if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) 44132633Ssam break; 44232633Ssam if (outq.c_cc <= TTLOWAT(tp)) { 44332633Ssam if (tp->t_state & TS_ASLEEP) { 44432633Ssam tp->t_state &= ~TS_ASLEEP; 44532633Ssam wakeup((caddr_t)&tp->t_outq); 44632633Ssam } 44732633Ssam if (tp->t_wsel) { 44832633Ssam selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL); 44932633Ssam tp->t_wsel = 0; 45032633Ssam tp->t_state &= ~TS_WCOLL; 45132633Ssam } 45232633Ssam } 45332633Ssam if (outq.c_cc == 0) 45432633Ssam break; 45532633Ssam /* 45632633Ssam * If we're not currently busy outputting, 45732633Ssam * and there is data to be output, set up 45832633Ssam * port transmit structure to send to mpcc. 45932633Ssam */ 46032633Ssam if (tp->t_flags & (RAW|LITOUT)) 46132633Ssam n = ndqb(&outq, 0); 46232633Ssam else { 46332633Ssam n = ndqb(&outq, 0200); 46432633Ssam if (n == 0) { 46532633Ssam n = getc(&outq); 46632633Ssam timeout(ttrstrt, (caddr_t)tp, (n&0177)+6); 46732633Ssam tp->t_state |= TS_TIMEOUT; 46832633Ssam break; 46932633Ssam } 47032633Ssam } 47134506Skarels hxp->dblock[i] = (caddr_t)kvtophys(outq.c_cf); 47232633Ssam hxp->size[i] = n; 47332633Ssam xcnt++; /* count of xmts to send */ 47432633Ssam ndadvance(&outq, n); 47532633Ssam } 47632633Ssam /* 47732633Ssam * If data to send, poke mpcc. 47832633Ssam */ 47932633Ssam if (xcnt) { 48032633Ssam ev = mp_getevent(mp, unit); 48132633Ssam if (ev == 0) { 48232633Ssam tp->t_state &= ~(TS_BUSY|TS_TIMEOUT); 48332633Ssam } else { 48432633Ssam tp->t_state |= TS_BUSY; 48532633Ssam ev->ev_count = xcnt; 48632633Ssam mpcmd(ev, EVCMD_WRITE, 0, mb, MPPORT(unit)); 48732633Ssam } 48832633Ssam } 48932633Ssam splx(s); 49032633Ssam } 49132633Ssam 49232633Ssam /* 49332633Ssam * Advance cc bytes from q but don't free memory. 49432633Ssam */ 49532633Ssam ndadvance(q, cc) 49632633Ssam register struct clist *q; 49732633Ssam register cc; 49832633Ssam { 49932633Ssam register struct cblock *bp; 50032633Ssam char *end; 50132633Ssam int rem, s; 50232633Ssam 50332633Ssam s = spltty(); 50432633Ssam if (q->c_cc <= 0) 50532633Ssam goto out; 50632633Ssam while (cc>0 && q->c_cc) { 50732633Ssam bp = (struct cblock *)((int)q->c_cf & ~CROUND); 50832633Ssam if ((int)bp == (((int)q->c_cl-1) & ~CROUND)) { 50932633Ssam end = q->c_cl; 51032633Ssam } else { 51132633Ssam end = (char *)((int)bp + sizeof (struct cblock)); 51232633Ssam } 51332633Ssam rem = end - q->c_cf; 51432633Ssam if (cc >= rem) { 51532633Ssam cc -= rem; 51632633Ssam q->c_cc -= rem; 51732633Ssam q->c_cf = bp->c_next->c_info; 51832633Ssam } else { 51932633Ssam q->c_cc -= cc; 52032633Ssam q->c_cf += cc; 52132633Ssam break; 52232633Ssam } 52332633Ssam } 52432633Ssam if (q->c_cc <= 0) { 52532633Ssam q->c_cf = q->c_cl = NULL; 52632633Ssam q->c_cc = 0; 52732633Ssam } 52832633Ssam out: 52932633Ssam splx(s); 53032633Ssam } 53132633Ssam 53232633Ssam /* 53332633Ssam * Stop output on a line, e.g. for ^S/^Q or output flush. 53432633Ssam */ 53534506Skarels /* ARGSUSED */ 53632633Ssam mpstop(tp, rw) 53732633Ssam register struct tty *tp; 53832633Ssam int rw; 53932633Ssam { 54034506Skarels int s; 54132633Ssam 54232633Ssam s = spl8(); 54332633Ssam /* XXX: DISABLE TRANSMITTER */ 54432633Ssam if (tp->t_state & TS_BUSY) { 54532633Ssam if ((tp->t_state & TS_TTSTOP) == 0) 54632633Ssam tp->t_state |= TS_FLUSH; 54732633Ssam } 54832633Ssam splx(s); 54932633Ssam } 55032633Ssam 55132633Ssam /* 55232633Ssam * Initialize an async port's MPCC state. 55332633Ssam */ 55432633Ssam mpportinit(ms, mp, port) 55532633Ssam register struct mpsoftc *ms; 55632633Ssam register struct mpport *mp; 55732633Ssam int port; 55832633Ssam { 55932633Ssam register struct mpevent *ev; 56032633Ssam register int i; 56132633Ssam caddr_t ptr; 56232633Ssam 56332633Ssam mp->mp_on = mp->mp_off = 0; 56432633Ssam mp->mp_nextrcv = 0; 56532633Ssam mp->mp_flags = 0; 56632633Ssam ev = &mp->mp_recvq[0]; 56732633Ssam for (i = 0; ev < &mp->mp_recvq[MPINSET]; ev++, i++) { 56832633Ssam ev->ev_status = EVSTATUS_FREE; 56932633Ssam ev->ev_cmd = 0; 57032633Ssam ev->ev_opts = 0; 57132633Ssam ev->ev_error = 0; 57232633Ssam ev->ev_flags = 0; 57332633Ssam ev->ev_count = 0; 57434506Skarels ev->ev_un.hxl = (struct hxmtl *) kvtophys(&ms->ms_hxl[port]); 57534506Skarels ev->ev_params = (caddr_t) kvtophys(&ms->ms_async[port][i]); 57632633Ssam } 57732633Ssam ev = &mp->mp_sendq[0]; 57832633Ssam for (i = 0; ev < &mp->mp_sendq[MPOUTSET]; ev++, i++) { 57932633Ssam /* init so that L2 can't send any events */ 58032633Ssam /* to host until open has completed */ 58132633Ssam ev->ev_status = EVSTATUS_FREE; 58232633Ssam ev->ev_cmd = 0; 58332633Ssam ev->ev_error = 0; 58432633Ssam ev->ev_flags = 0; 58532633Ssam ev->ev_count = 0; 58632633Ssam ptr = (caddr_t) &ms->ms_cbuf[port][i][0]; 58734506Skarels ev->ev_un.rcvblk = (u_char *)kvtophys(ptr); 58834506Skarels ev->ev_params = (caddr_t) kvtophys(ptr); 58932633Ssam } 59032633Ssam return (0); 59132633Ssam } 59232633Ssam 59332633Ssam /* 59432633Ssam * Send an event to an mpcc. 59532633Ssam */ 59632633Ssam mpcmd(ev, cmd, flags, mb, port) 59732633Ssam register struct mpevent *ev; 59832633Ssam struct mblok *mb; 59932633Ssam { 60032633Ssam int s; 60132633Ssam 60232633Ssam s = spl8(); 60332633Ssam /* move host values to inbound entry */ 60432633Ssam ev->ev_cmd = cmd; 60532633Ssam ev->ev_opts = flags; 60632633Ssam /* show event ready for mpcc */ 60732633Ssam ev->ev_status = EVSTATUS_GO; 60832633Ssam mpintmpcc(mb, port); 60932633Ssam splx(s); 61032633Ssam } 61132633Ssam 61232633Ssam /* 61332633Ssam * Return the next available event entry for the indicated port. 61432633Ssam */ 61532633Ssam struct mpevent * 61632633Ssam mp_getevent(mp, unit) 61732633Ssam register struct mpport *mp; 61832633Ssam int unit; 61932633Ssam { 62032633Ssam register struct mpevent *ev; 62132633Ssam int i, s; 62232633Ssam 62332633Ssam s = spl8(); 62432633Ssam ev = &mp->mp_recvq[mp->mp_on]; 62532633Ssam if (ev->ev_status != EVSTATUS_FREE) 62632633Ssam goto bad; 62732633Ssam /* 62832633Ssam * If not a close request, verify one extra 62932633Ssam * event is available for closing the port. 63032633Ssam */ 63134506Skarels if ((mp->mp_flags & MP_PROGRESS) == 0) { 63232633Ssam if ((i = mp->mp_on + 1) >= MPINSET) 63332633Ssam i = 0; 63432633Ssam if (mp->mp_recvq[i].ev_status != EVSTATUS_FREE) 63532633Ssam goto bad; 63632633Ssam } 63732633Ssam /* init inbound fields marking this entry as busy */ 63832633Ssam ev->ev_error = 0; 63932633Ssam ev->ev_flags = 0; 64032633Ssam ev->ev_count = 0; 64132633Ssam ev->ev_status = EVSTATUS_BUSY; 64232633Ssam /* adjust pointer to next available inbound entry */ 64332633Ssam adjptr(mp->mp_on, MPINSET); 64432633Ssam splx(s); 64532633Ssam return (ev); 64632633Ssam bad: 64732633Ssam splx(s); 64832633Ssam log(LOG_ERR, "mp%d: port%d, out of events", MPUNIT(unit), MPPORT(unit)); 64932633Ssam return ((struct mpevent *)0); 65032633Ssam } 65132633Ssam 65232633Ssam mpmodem(unit, flag) 65332633Ssam int unit, flag; 65432633Ssam { 65532633Ssam struct mpsoftc *ms = &mp_softc[MPUNIT(unit)]; 65632633Ssam int port = MPPORT(unit); 65732633Ssam register struct mpport *mp; 65832633Ssam register struct asyncparam *asp; 65932633Ssam 66032633Ssam mp = &ms->ms_mb->mb_port[port]; 66132633Ssam asp = &ms->ms_async[port][mp->mp_on?mp->mp_on-1:MPINSET-1]; 66232633Ssam if (flag == MMOD_ON) { 66332633Ssam if (ms->ms_softCAR & (1 << port)) 66432633Ssam setm(&asp->ap_modem, A_DTR, ASSERT); 66532633Ssam else 66632633Ssam setm(&asp->ap_modem, A_DTR, AUTO); 66732633Ssam seti(&asp->ap_intena, A_DCD); 66832633Ssam } else { 66932633Ssam setm(&asp->ap_modem, 0, DROP); 67032633Ssam seti(&asp->ap_intena, 0); 67132633Ssam } 67232633Ssam } 67332633Ssam 67432633Ssam /* 67532633Ssam * Set up the modem control structure according to mask. 67632633Ssam * Each set bit in the mask means assert the corresponding 67732633Ssam * modem control line, otherwise, it will be dropped. 67832633Ssam * RTS is special since it can either be asserted, dropped 67932633Ssam * or put in auto mode for auto modem control. 68032633Ssam */ 68132633Ssam static 68232633Ssam setm(mc, mask, rts) 68332633Ssam register struct mdmctl *mc; 68432633Ssam register int mask; 68532633Ssam { 68632633Ssam 68732633Ssam mc->mc_rngdsr = (mask & A_RNGDSR) ? ASSERT : DROP; 68832633Ssam mc->mc_rate = (mask & A_RATE) ? ASSERT : DROP; 68932633Ssam mc->mc_dcd = (mask & A_DCD) ? ASSERT : DROP; 69032633Ssam mc->mc_sectx = (mask & A_SECTX) ? ASSERT : DROP; 69132633Ssam mc->mc_cts = (mask & A_CTS) ? ASSERT : DROP; 69232633Ssam mc->mc_secrx = (mask & A_SECRX) ? ASSERT : DROP; 69332633Ssam mc->mc_dtr = (mask & A_DTR) ? ASSERT : DROP; 69432633Ssam mc->mc_rts = rts; 69532633Ssam } 69632633Ssam 69732633Ssam /* 69832633Ssam * Set up the status change enable field from mask. 69932633Ssam * When a signal is enabled in this structure and 70032633Ssam * and a change in state on a corresponding modem 70132633Ssam * control line occurs, a status change event will 70232633Ssam * be delivered to the host. 70332633Ssam */ 70432633Ssam static 70532633Ssam seti(mc, mask) 70632633Ssam register struct mdmctl *mc; 70732633Ssam register int mask; 70832633Ssam { 70932633Ssam 71032633Ssam mc->mc_rngdsr = (mask & A_RNGDSR) ? MDM_ON : MDM_OFF; 71132633Ssam mc->mc_rate = (mask & A_RATE) ? MDM_ON : MDM_OFF; 71232633Ssam mc->mc_dcd = (mask & A_DCD) ? MDM_ON : MDM_OFF; 71332633Ssam mc->mc_sectx = (mask & A_SECTX) ? MDM_ON : MDM_OFF; 71432633Ssam mc->mc_cts = (mask & A_CTS) ? MDM_ON : MDM_OFF; 71532633Ssam mc->mc_secrx = (mask & A_SECRX) ? MDM_ON : MDM_OFF; 71632633Ssam mc->mc_dtr = (mask & A_DTR) ? MDM_ON : MDM_OFF; 71732633Ssam mc->mc_rts = (mask & A_RTS) ? MDM_ON : MDM_OFF; 71832633Ssam } 71932633Ssam 72032633Ssam mpcleanport(mb, port) 72132633Ssam struct mblok *mb; 72232633Ssam int port; 72332633Ssam { 72432633Ssam register struct mpport *mp; 72532633Ssam register struct tty *tp; 72632633Ssam 72732633Ssam mp = &mb->mb_port[port]; 72832633Ssam if (mp->mp_proto == MPPROTO_ASYNC) { 72932633Ssam mp->mp_flags = MP_REMBSY; 73034506Skarels /* signal loss of carrier and close */ 73132633Ssam tp = &mp_tty[mb->mb_unit*MPCHUNK+port]; 73232633Ssam ttyflush(tp, FREAD|FWRITE); 73334506Skarels (void) (*linesw[tp->t_line].l_modem)(tp, 0); 73434506Skarels (void) mpclose(tp->t_dev, 0); 73532633Ssam } 73632633Ssam } 73732633Ssam 73832633Ssam mpclean(mb, port) 73932633Ssam register struct mblok *mb; 74032633Ssam int port; 74132633Ssam { 74232633Ssam register struct mpport *mp; 74332633Ssam register struct mpevent *ev; 74432633Ssam register int i; 74534506Skarels u_char list[2]; 74632633Ssam int unit; 74732633Ssam 74832633Ssam mp = &mb->mb_port[port]; 74932633Ssam unit = mb->mb_unit; 75032633Ssam for (i = mp->mp_off; i != mp->mp_on; i = (i+1 % MPINSET)) { 75132633Ssam ev = &mp->mp_recvq[i]; 75232633Ssam ev->ev_error = ENXIO; 75332633Ssam ev->ev_status = EVSTATUS_DONE; 75432633Ssam } 75532633Ssam list[0] = port, list[1] = MPPORT_EOL; 75632633Ssam mpxintr(unit, list); 75732633Ssam mprintr(unit, list); 75832633Ssam /* Clear async for port */ 75932633Ssam mp->mp_proto = MPPROTO_UNUSED; 76032633Ssam mp->mp_flags = 0; 76132633Ssam mp->mp_on = 0; 76232633Ssam mp->mp_off = 0; 76332633Ssam mp->mp_nextrcv = 0; 76432633Ssam 76532633Ssam mp_tty[unit*MPCHUNK + port].t_state = 0; 76632633Ssam for (ev = &mp->mp_sendq[0]; ev < &mp->mp_sendq[MPOUTSET]; ev++) { 76732633Ssam ev->ev_status = EVSTATUS_FREE; 76832633Ssam ev->ev_cmd = 0; 76932633Ssam ev->ev_error = 0; 77032633Ssam ev->ev_un.rcvblk = 0; 77132633Ssam ev->ev_params = 0; 77232633Ssam } 77332633Ssam for (ev = &mp->mp_recvq[0]; ev < &mp->mp_recvq[MPINSET]; ev++) { 77432633Ssam ev->ev_status = EVSTATUS_FREE; 77532633Ssam ev->ev_cmd = 0; 77632633Ssam ev->ev_error = 0; 77732633Ssam ev->ev_params = 0; 77832633Ssam } 77932633Ssam } 78032633Ssam 78132633Ssam /* 78232633Ssam * MPCC interrupt handler. 78332633Ssam */ 78432633Ssam mpintr(mpcc) 78532633Ssam int mpcc; 78632633Ssam { 78732633Ssam register struct mblok *mb; 78832633Ssam register struct his *his; 78932633Ssam 79032633Ssam mb = mp_softc[mpcc].ms_mb; 79132633Ssam if (mb == 0) { 79232633Ssam printf("mp%d: stray interrupt\n", mpcc); 79332633Ssam return; 79432633Ssam } 79532633Ssam his = &mb->mb_hostint; 79632633Ssam his->semaphore &= ~MPSEMA_AVAILABLE; 79732633Ssam /* 79832633Ssam * Check for events to be processed. 79932633Ssam */ 80032633Ssam if (his->proto[MPPROTO_ASYNC].outbdone[0] != MPPORT_EOL) 80132633Ssam mprintr(mpcc, his->proto[MPPROTO_ASYNC].outbdone); 80232633Ssam if (his->proto[MPPROTO_ASYNC].inbdone[0] != MPPORT_EOL) 80332633Ssam mpxintr(mpcc, his->proto[MPPROTO_ASYNC].inbdone); 80432633Ssam if (mb->mb_harderr || mb->mb_softerr) 80532633Ssam mperror(mb, mpcc); 80632633Ssam his->semaphore |= MPSEMA_AVAILABLE; 80732633Ssam } 80832633Ssam 80932633Ssam /* 81032633Ssam * Handler for processing completion of transmitted events. 81132633Ssam */ 81232633Ssam mpxintr(unit, list) 81334506Skarels register u_char *list; 81432633Ssam { 81532633Ssam register struct mpport *mp; 81632633Ssam register struct mpevent *ev; 81732633Ssam register struct mblok *mb; 81832633Ssam register struct tty *tp; 81932633Ssam register struct asyncparam *ap; 82032633Ssam struct mpsoftc *ms; 82132633Ssam int port, i, j; 82232633Ssam 82332633Ssam ms = &mp_softc[unit]; 82432633Ssam mb = mp_softc[unit].ms_mb; 82532633Ssam for (j = 0; j < MPMAXPORT && ((port = *list++) != MPPORT_EOL); j++) { 82632633Ssam /* 82732633Ssam * Process each completed entry in the inbound queue. 82832633Ssam */ 82932633Ssam mp = &mb->mb_port[port]; 83032633Ssam tp = &mp_tty[unit*MPCHUNK + port]; 83132633Ssam #define nextevent(mp) &mp->mp_recvq[mp->mp_off] 83232633Ssam ev = nextevent(mp); 83332633Ssam for(; ev->ev_status & EVSTATUS_DONE; ev = nextevent(mp)) { 83432633Ssam /* YUCK */ 83532633Ssam ap = &ms->ms_async[port][mp->mp_off]; 83634506Skarels mppurge((caddr_t)ap, (int)sizeof (*ap)); 83732633Ssam switch (ev->ev_cmd) { 83832633Ssam case EVCMD_OPEN: 83932633Ssam /* 84032633Ssam * Open completion, start all reads and 84132633Ssam * assert modem status information. 84232633Ssam */ 84332633Ssam for (i = 0; i < MPOUTSET; i++) 84432633Ssam mp->mp_sendq[i].ev_status = EVSTATUS_GO; 84532633Ssam (*linesw[tp->t_line].l_modem) 84632633Ssam (tp, ap->ap_modem.mc_dcd == ASSERT); 84732633Ssam break; 84832633Ssam case EVCMD_CLOSE: 84932633Ssam /* 85032633Ssam * Close completion, flush all pending 85132633Ssam * transmissions, free resources, and 85232633Ssam * cleanup mpcc port state. 85332633Ssam */ 85432633Ssam for (i = 0; i < MPOUTSET; i++) { 85532633Ssam mp->mp_sendq[i].ev_status = 85632633Ssam EVSTATUS_FREE; 85732633Ssam mp->mp_sendq[i].ev_un.rcvblk = 0; 85832633Ssam mp->mp_sendq[i].ev_params = 0; 85932633Ssam } 86032633Ssam tp->t_state &= ~TS_CARR_ON; 86132633Ssam mp->mp_on = mp->mp_off = mp->mp_nextrcv = 0; 86232633Ssam mp->mp_flags &= ~MP_PROGRESS; 86332633Ssam mp->mp_proto = MPPROTO_UNUSED; 86432633Ssam wakeup((caddr_t)&tp->t_canq); /* ??? */ 86532633Ssam goto done; 86632633Ssam case EVCMD_IOCTL: 86732633Ssam /* 86832633Ssam * Nothing to do, just pitch. 86932633Ssam */ 87032633Ssam break; 87132633Ssam case EVCMD_WRITE: 87232633Ssam /* 87332633Ssam * Transmission completed, update tty 87432633Ssam * state and restart output. 87532633Ssam */ 87632633Ssam tp->t_state &= ~TS_BUSY; 87732633Ssam if (tp->t_state & TS_FLUSH) { 87832633Ssam tp->t_state &= ~TS_FLUSH; 87932633Ssam wakeup((caddr_t)&tp->t_state); 88032633Ssam } else { 88134506Skarels register int cc = 0, n; 88232633Ssam struct hxmtl *hxp; 88332633Ssam 88432633Ssam hxp = &ms->ms_hxl[port]; 88534506Skarels for(n = 0; n < ev->ev_count; n++) 88634506Skarels cc += hxp->size[n]; 88732633Ssam ndflush(&tp->t_outq, cc); 88832633Ssam } 88932633Ssam switch (ev->ev_error) { 89032633Ssam case A_SIZERR: /*# error in xmt data size */ 89132633Ssam mplog(unit, port, A_XSIZE, 0); 89232633Ssam break; 89332633Ssam case A_NXBERR: /*# no more xmt evt buffers */ 89432633Ssam mplog(unit, port, A_NOXBUF, 0); 89532633Ssam break; 89632633Ssam } 89732633Ssam mpstart(tp); 89832633Ssam break; 89932633Ssam default: 90034506Skarels mplog(unit, port, A_INVCMD, (int)ev->ev_cmd); 90132633Ssam break; 90232633Ssam } 90332633Ssam /* re-init all values in this entry */ 90432633Ssam ev->ev_cmd = 0; 90532633Ssam ev->ev_opts = 0; 90632633Ssam ev->ev_error = 0; 90732633Ssam ev->ev_flags = 0; 90832633Ssam ev->ev_count = 0; 90932633Ssam /* show this entry is available for use */ 91032633Ssam ev->ev_status = EVSTATUS_FREE; 91132633Ssam adjptr(mp->mp_off, MPINSET); 91232633Ssam #undef nextevent 91332633Ssam } 91432633Ssam done: 91532633Ssam ; 91632633Ssam } 91732633Ssam } 91832633Ssam 91932633Ssam /* 92032633Ssam * Handler for processing received events. 92132633Ssam */ 92232633Ssam mprintr(unit, list) 92334506Skarels u_char *list; 92432633Ssam { 92532633Ssam register struct tty *tp; 92632633Ssam register struct mpport *mp; 92732633Ssam register struct mpevent *ev; 92832633Ssam struct mblok *mb; 92932633Ssam register int cc; 93032633Ssam register char *cp; 93132633Ssam struct mpsoftc *ms; 93232633Ssam caddr_t ptr; 93332633Ssam char *rcverr; 93432633Ssam int port, i; 93532633Ssam 93632633Ssam ms = &mp_softc[unit]; 93732633Ssam mb = mp_softc[unit].ms_mb; 93832633Ssam for (i = 0; i < MPMAXPORT && (port = *list++) != MPPORT_EOL; i++) { 93932633Ssam tp = &mp_tty[unit*MPCHUNK + port]; 94032633Ssam mp = &mb->mb_port[port]; 94132633Ssam ev = &mp->mp_sendq[mp->mp_nextrcv]; 94232633Ssam while (ev->ev_status & EVSTATUS_DONE) { 94332633Ssam if (ev->ev_cmd != EVCMD_READ && 94432633Ssam ev->ev_cmd != EVCMD_STATUS) { 94532633Ssam mplog(unit, port, "unexpected command", 94634506Skarels (int)ev->ev_cmd); 94732633Ssam goto next; 94832633Ssam } 94932633Ssam if (ev->ev_cmd == EVCMD_STATUS) { 95032633Ssam /* 95132633Ssam * Status change, look for carrier changes. 95232633Ssam */ 95332633Ssam if (ev->ev_opts == DCDASRT || 95432633Ssam ev->ev_opts == DCDDROP) 95532633Ssam (*linesw[tp->t_line].l_modem) 95632633Ssam (tp, ev->ev_opts == DCDASRT); 95732633Ssam else 95832633Ssam mplog(unit, port, 95932633Ssam "unexpect status command", 96034506Skarels (int)ev->ev_opts); 96132633Ssam goto next; 96232633Ssam } 96332633Ssam /* 96432633Ssam * Process received data. 96532633Ssam */ 96632633Ssam if ((tp->t_state & (TS_ISOPEN|TS_WOPEN)) == 0) 96732633Ssam goto next; 96832633Ssam cc = ev->ev_count; 96932633Ssam if (cc == 0) 97032633Ssam goto next; 97132633Ssam /* YUCK */ 97232633Ssam cp = ms->ms_cbuf[port][mp->mp_nextrcv]; 97332633Ssam mppurge(cp, CBSIZE); 97432633Ssam while (cc-- > 0) { 97532633Ssam /* 97632633Ssam * A null character is inserted, potentially 97732633Ssam * when a break or framing error occurs. If 97832633Ssam * we're not in raw mode, substitute the 97932633Ssam * interrupt character. 98032633Ssam */ 98132633Ssam if (*cp == 0 && 98232633Ssam (ev->ev_error == BRKASRT || 98332633Ssam ev->ev_error == FRAMERR)) 98432633Ssam if ((tp->t_flags&RAW) == 0) 98532633Ssam *cp = tp->t_intrc; 98632633Ssam (*linesw[tp->t_line].l_rint)(*cp++, tp); 98732633Ssam } 98832633Ssam /* setup for next read */ 98932633Ssam ptr = (caddr_t)&mp_softc[unit].ms_cbuf[port][mp->mp_nextrcv][0]; 99034506Skarels ev->ev_un.rcvblk = (u_char *)kvtophys(ptr); 99134506Skarels ev->ev_params = (caddr_t) kvtophys(ptr); 99233673Sbostic switch(ev->ev_error) { 99333673Sbostic case RCVDTA: /* Normal (good) rcv data */ 99433673Sbostic /* do not report the following */ 99533673Sbostic /* they are "normal" errors */ 99633673Sbostic case FRAMERR: /* frame error */ 99733673Sbostic case BRKASRT: /* Break condition */ 99832633Ssam case PARERR: /* parity error */ 99933673Sbostic rcverr = (char *)0; 100032633Ssam break; 100132633Ssam case OVRNERR: /* Overrun error */ 100232633Ssam rcverr = "overrun error"; 100332633Ssam break; 100432633Ssam case OVFERR: /* Overflow error */ 100532633Ssam rcverr = "overflow error"; 100632633Ssam break; 100732633Ssam default: 100832633Ssam rcverr = "undefined rcv error"; 100932633Ssam } 101032633Ssam if (rcverr != (char *)0) 101134506Skarels mplog(unit, port, rcverr, (int)ev->ev_error); 101232633Ssam next: 101332633Ssam ev->ev_cmd = 0; 101432633Ssam ev->ev_opts = 0; 101532633Ssam ev->ev_error = 0; 101632633Ssam ev->ev_flags = 0; 101732633Ssam ev->ev_status = EVSTATUS_GO; /* start next read */ 101832633Ssam adjptr(mp->mp_nextrcv, MPOUTSET); 101932633Ssam ev = &mp->mp_sendq[mp->mp_nextrcv]; 102032633Ssam } 102132633Ssam } 102232633Ssam } 102332633Ssam 102432633Ssam /* 102532633Ssam * Log an mpcc diagnostic. 102632633Ssam */ 102732633Ssam mplog(unit, port, cp, flags) 102832633Ssam char *cp; 102932633Ssam { 103032633Ssam 103132633Ssam if (flags) 103232633Ssam log(LOG_ERR, "mp%d: port%d, %s (%d)\n", 103332633Ssam unit, port, cp, flags); 103432633Ssam else 103532633Ssam log(LOG_ERR, "mp%d: port%d, %s\n", unit, port, cp); 103632633Ssam } 103732633Ssam 103832633Ssam int MPHOSTINT = 1; 103932633Ssam 104032633Ssam mptimeint(mb) 104132633Ssam register struct mblok *mb; 104232633Ssam { 104332633Ssam 104432633Ssam mb->mb_mpintcnt = 0; 104532633Ssam mb->mb_mpintclk = (caddr_t)0; 104632633Ssam *(u_short *)mpinfo[mb->mb_unit]->ui_addr = 2; 104732633Ssam } 104832633Ssam 104932633Ssam /* 105032633Ssam * Interupt mpcc 105132633Ssam */ 105232633Ssam mpintmpcc(mb, port) 105332633Ssam register struct mblok *mb; 105432633Ssam { 105532633Ssam 105632633Ssam mb->mb_intr[port] |= MPSEMA_WORK; 105732633Ssam if (++mb->mb_mpintcnt == MPHOSTINT) { 105832633Ssam mb->mb_mpintcnt = 0; 105932633Ssam *(u_short *)mpinfo[mb->mb_unit]->ui_addr = 2; 106032633Ssam if (mb->mb_mpintclk) { 106134506Skarels untimeout(mptimeint, (caddr_t)mb); 106232633Ssam mb->mb_mpintclk = 0; 106332633Ssam } 106432633Ssam } else { 106532633Ssam if (mb->mb_mpintclk == 0) { 106634506Skarels timeout(mptimeint, (caddr_t)mb, 4); 106732633Ssam mb->mb_mpintclk = (caddr_t)1; 106832633Ssam } 106932633Ssam } 107032633Ssam } 107132633Ssam 107232633Ssam static char *mpherrmsg[] = { 107332633Ssam "", 107432633Ssam "Bus error", /* MPBUSERR */ 107532633Ssam "Address error", /* ADDRERR */ 107632633Ssam "Undefined ecc interrupt", /* UNDECC */ 107732633Ssam "Undefined interrupt", /* UNDINT */ 107832633Ssam "Power failure occurred", /* PWRFL */ 107932633Ssam "Stray transmit done interrupt", /* NOXENTRY */ 108032633Ssam "Two fast timers on one port", /* TWOFTMRS */ 108132633Ssam "Interrupt queue full", /* INTQFULL */ 108232633Ssam "Interrupt queue ack error", /* INTQERR */ 108332633Ssam "Uncorrectable dma parity error", /* CBPERR */ 108432633Ssam "32 port ACAP failed power up", /* ACPDEAD */ 108532633Ssam }; 108632633Ssam #define NHERRS (sizeof (mpherrmsg) / sizeof (mpherrmsg[0])) 108732633Ssam 108832633Ssam mperror(mb, unit) 108932633Ssam register struct mblok *mb; 109032633Ssam int unit; 109132633Ssam { 109232633Ssam register char *cp; 109332633Ssam register int i; 109432633Ssam 109532633Ssam if (mb->mb_softerr) { 109632633Ssam switch (mb->mb_softerr) { 109732633Ssam case DMAPERR: /* dma parity error */ 109832633Ssam cp = "dma parity error"; 109932633Ssam break; 110032633Ssam case ECCERR: 110132633Ssam cp = "local memory ecc error"; 110232633Ssam break; 110332633Ssam default: 110432633Ssam cp = "unknown error"; 110532633Ssam break; 110632633Ssam } 110732633Ssam log(LOG_ERR, "mp%d: soft error, %s", unit, cp); 110832633Ssam mb->mb_softerr = 0; 110932633Ssam } 111032633Ssam if (mb->mb_harderr) { 111132633Ssam if (mb->mb_harderr < NHERRS) 111232633Ssam cp = mpherrmsg[mb->mb_harderr]; 111332633Ssam else 111432633Ssam cp = "unknown error"; 111532633Ssam log(LOG_ERR, "mp%d: hard error, %s", unit, cp); 111632633Ssam if (mb->mb_status == MP_OPOPEN) { 111732633Ssam for (i = 0; i < MPMAXPORT; i++) { 111832633Ssam mpcleanport(mb, i); 111932633Ssam mb->mb_proto[i] = MPPROTO_UNUSED; 112032633Ssam } 112132633Ssam } 112232633Ssam mb->mb_harderr = 0; 112332633Ssam mb->mb_status = 0; 112432633Ssam } 112532633Ssam } 112632633Ssam 112732633Ssam mppurge(addr, cc) 112832633Ssam register caddr_t addr; 112932633Ssam register int cc; 113032633Ssam { 113132633Ssam 113232633Ssam for (; cc >= 0; addr += NBPG, cc -= NBPG) 113332633Ssam mtpr(P1DC, addr); 113432633Ssam } 113532633Ssam 113632633Ssam /* 113732633Ssam * MPCC Download Pseudo-device. 113832633Ssam */ 113932633Ssam char mpdlbuf[MPDLBUFSIZE]; 114032633Ssam int mpdlbusy; /* interlock on download buffer */ 114132633Ssam int mpdlerr; 114232633Ssam 114332633Ssam mpdlopen(dev) 114432633Ssam dev_t dev; 114532633Ssam { 114632633Ssam int unit, mpu; 114732633Ssam struct vba_device *vi; 114832633Ssam 114932633Ssam unit = minor(dev); 115032633Ssam mpu = MPUNIT(unit); 115132633Ssam if (mpu >= NMP || (vi = mpinfo[mpu]) == 0 || vi->ui_alive == 0) 115232633Ssam return (ENODEV); 115332633Ssam return (0); 115432633Ssam } 115532633Ssam 115632633Ssam mpdlwrite(dev, uio) 115732633Ssam dev_t dev; 115832633Ssam struct uio *uio; 115932633Ssam { 116032633Ssam register struct mpsoftc *ms = &mp_softc[MPUNIT(minor(dev))]; 116132633Ssam register struct mpdl *dl; 116232633Ssam int error; 116332633Ssam 116432633Ssam if (ms->ms_mb == 0 || ms->ms_mb->mb_status != MP_DLOPEN) 116532633Ssam return (EFAULT); 116632633Ssam dl = &ms->ms_mb->mb_dl; 116732633Ssam dl->mpdl_count = uio->uio_iov->iov_len; 116834506Skarels dl->mpdl_data = (caddr_t) kvtophys(mpdlbuf); 116934506Skarels if (error = uiomove(mpdlbuf, (int)dl->mpdl_count, UIO_WRITE, uio)) 117032633Ssam return (error); 117132633Ssam uio->uio_resid -= dl->mpdl_count; /* set up return from write */ 117232633Ssam dl->mpdl_cmd = MPDLCMD_NORMAL; 117332633Ssam error = mpdlwait(dl); 117432633Ssam return (error); 117532633Ssam } 117632633Ssam 117732633Ssam mpdlclose(dev) 117832633Ssam dev_t dev; 117932633Ssam { 118032633Ssam register struct mblok *mb = mp_softc[MPUNIT(minor(dev))].ms_mb; 118132633Ssam 118232633Ssam if (mb == 0 || mb->mb_status != MP_DLDONE) { 118332633Ssam mpbogus.status = 0; 118432633Ssam if (mpbogus.mb == mpbogus.mbloks[MPUNIT(minor(dev))]) 118532633Ssam mpdlbusy--; 118632633Ssam return (EEXIST); 118732633Ssam } 118832633Ssam mb->mb_status = MP_OPOPEN; 118932633Ssam mpbogus.status = 0; 119032633Ssam /* set to dead, for board handshake */ 119132633Ssam mb->mb_hostint.imok = MPIMOK_DEAD; 119232633Ssam return (0); 119332633Ssam } 119432633Ssam 119532633Ssam int mpdltimeout(); 119632633Ssam 119734506Skarels /* ARGSUSED */ 119832633Ssam mpdlioctl(dev, cmd, data, flag) 119932633Ssam dev_t dev; 120032633Ssam caddr_t data; 120132633Ssam { 120232633Ssam register struct mblok *mb; 120332633Ssam register struct mpdl *dl; 120434506Skarels int unit, error, s, i; 120532633Ssam 120632633Ssam mb = mp_softc[unit=MPUNIT(minor(dev))].ms_mb; 120732633Ssam if (mb == 0) 120832633Ssam return (EEXIST); 120932633Ssam dl = &mb->mb_dl; 121032633Ssam error = 0; 121132633Ssam switch (cmd) { 121232633Ssam case MPIOPORTMAP: 121332633Ssam bcopy(data, (caddr_t)mb->mb_proto, sizeof (mb->mb_proto)); 121432633Ssam break; 121532633Ssam case MPIOHILO: 121632633Ssam bcopy(data, (caddr_t)&mb->mb_hiport, 2*(sizeof(mb->mb_hiport))); 121732633Ssam break; 121832633Ssam case MPIOENDDL: 121932633Ssam dl->mpdl_count = 0; 122032633Ssam dl->mpdl_data = 0; 122132633Ssam dl->mpdl_cmd = MPIOENDDL&IOCPARM_MASK; 122232633Ssam error = mpdlwait(dl); 122332633Ssam mpccinit(unit); 122432633Ssam mb->mb_status = MP_DLDONE; 122532633Ssam mpdlbusy--; 122632633Ssam break; 122732633Ssam case MPIOENDCODE: 122832633Ssam dl->mpdl_count = 0; 122932633Ssam dl->mpdl_data = 0; 123032633Ssam dl->mpdl_cmd = MPIOENDCODE&IOCPARM_MASK; 123132633Ssam error = mpdlwait(dl); 123232633Ssam break; 123332633Ssam case MPIOASYNCNF: 123432633Ssam bcopy(data, mpdlbuf, sizeof (struct abdcf)); 123534506Skarels dl->mpdl_data = (caddr_t) kvtophys(mpdlbuf); 123632633Ssam dl->mpdl_count = sizeof (struct abdcf); 123732633Ssam dl->mpdl_cmd = MPIOASYNCNF&IOCPARM_MASK; 123832633Ssam error = mpdlwait(dl); 123932633Ssam break; 124032633Ssam case MPIOSTARTDL: 124132633Ssam while (mpdlbusy) 124232633Ssam sleep((caddr_t)&mpdlbusy, PZERO+1); 124332633Ssam mpdlbusy++; 124432633Ssam /* initialize the downloading interface */ 124532633Ssam mpbogus.magic = MPMAGIC; 124632633Ssam mpbogus.mb = mpbogus.mbloks[unit]; 124732633Ssam mpbogus.status = 1; 124832633Ssam dl->mpdl_status = EVSTATUS_FREE; 124932633Ssam dl->mpdl_count = 0; 125032633Ssam dl->mpdl_cmd = 0; 125132633Ssam dl->mpdl_data = (char *) 0; 125232633Ssam mpdlerr = 0; 125332633Ssam mb->mb_magic = MPMAGIC; 125432633Ssam mb->mb_ivec = mp_softc[unit].ms_ivec+1; /* download vector */ 125532633Ssam mb->mb_status = MP_DLPEND; 125632633Ssam mb->mb_diagswitch[0] = 'A'; 125732633Ssam mb->mb_diagswitch[1] = 'P'; 125832633Ssam s = spl8(); 125932633Ssam *(u_short *)mpinfo[unit]->ui_addr = 2; 126034506Skarels timeout(mpdltimeout, (caddr_t)mb, 30*hz); 126132633Ssam sleep((caddr_t)&mb->mb_status, PZERO+1); 126232633Ssam splx(s); 126332633Ssam if (mb->mb_status == MP_DLOPEN) { 126434506Skarels untimeout(mpdltimeout, (caddr_t)mb); 126532633Ssam } else if (mb->mb_status == MP_DLTIME) { 126632633Ssam mpbogus.status = 0; 126732633Ssam error = ETIMEDOUT; 126832633Ssam } else { 126932633Ssam mpbogus.status = 0; 127032633Ssam error = ENXIO; 127132633Ssam log(LOG_ERR, "mp%d: start download: unknown status %x", 127232633Ssam unit, mb->mb_status); 127332633Ssam } 127434506Skarels bzero((caddr_t)mb->mb_port, sizeof (mb->mb_port)); 127532633Ssam break; 127632633Ssam case MPIORESETBOARD: 127732633Ssam s = spl8(); 127832633Ssam if (mb->mb_imokclk) 127932633Ssam mb->mb_imokclk = 0; 128032633Ssam *(u_short *)mpinfo[unit]->ui_addr = 0x100; 128132633Ssam if (mb->mb_status == MP_DLOPEN || mb->mb_status == MP_DLDONE) { 128232633Ssam mpdlerr = MP_DLERROR; 128332633Ssam dl->mpdl_status = EVSTATUS_FREE; 128432633Ssam wakeup((caddr_t)&dl->mpdl_status); 128532633Ssam mpbogus.status = 0; 128632633Ssam } 128732633Ssam for (i = 0; i < MPMAXPORT; i++) { 128832633Ssam if (mb->mb_harderr || mb->mb_softerr) 128932633Ssam mperror(mb, i); 129032633Ssam mpcleanport(mb, i); 129132633Ssam mb->mb_proto[i] = MPPROTO_UNUSED; 129232633Ssam } 129332633Ssam mb->mb_status = 0; 129432633Ssam splx(s); 129532633Ssam break; 129632633Ssam default: 129732633Ssam error = EINVAL; 129832633Ssam break; 129932633Ssam } 130032633Ssam return (error); 130132633Ssam } 130232633Ssam 130332633Ssam mpccinit(unit) 130432633Ssam int unit; 130532633Ssam { 130632633Ssam register struct mblok *mb = mp_softc[unit].ms_mb; 130732633Ssam register struct his *his; 130832633Ssam register int i, j; 130932633Ssam 131032633Ssam mb->mb_status = MP_DLDONE; 131132633Ssam mb->mb_ivec = mp_softc[unit].ms_ivec; 131232633Ssam mb->mb_magic = MPMAGIC; 131332633Ssam /* Init host interface structure */ 131432633Ssam his = &mb->mb_hostint; 131532633Ssam his->semaphore = MPSEMA_AVAILABLE; 131632633Ssam for (i = 0; i < NMPPROTO; i++) 131732633Ssam for (j = 0; j < MPMAXPORT; j++) { 131832633Ssam his->proto[i].inbdone[j] = MPPORT_EOL; 131932633Ssam his->proto[i].outbdone[j] = MPPORT_EOL; 132032633Ssam } 132132633Ssam mb->mb_unit = unit; 132232633Ssam } 132332633Ssam 132432633Ssam mpdlintr(mpcc) 132532633Ssam int mpcc; 132632633Ssam { 132732633Ssam register struct mblok *mb; 132832633Ssam register struct mpdl *dl; 132932633Ssam 133032633Ssam mb = mp_softc[mpcc].ms_mb; 133132633Ssam if (mb == 0) { 133232633Ssam printf("mp%d: stray download interrupt\n", mpcc); 133332633Ssam return; 133432633Ssam } 133532633Ssam dl = &mb->mb_dl; 133632633Ssam switch (mb->mb_status) { 133732633Ssam case MP_DLOPEN: 133832633Ssam if (dl->mpdl_status != EVSTATUS_DONE) 133932633Ssam mpdlerr = MP_DLERROR; 134032633Ssam dl->mpdl_status = EVSTATUS_FREE; 134132633Ssam wakeup((caddr_t)&dl->mpdl_status); 134232633Ssam return; 134332633Ssam case MP_DLPEND: 134432633Ssam mb->mb_status = MP_DLOPEN; 134534506Skarels wakeup((caddr_t)&mb->mb_status); 134632633Ssam /* fall thru... */ 134732633Ssam case MP_DLTIME: 134832633Ssam return; 134932633Ssam case MP_OPOPEN: 135032633Ssam if (mb->mb_imokclk) 135132633Ssam mb->mb_imokclk = 0; 135232633Ssam mb->mb_nointcnt = 0; /* reset no interrupt count */ 135332633Ssam mb->mb_hostint.imok = MPIMOK_DEAD; 135432633Ssam mb->mb_imokclk = (caddr_t)1; 135532633Ssam break; 135632633Ssam default: 135732633Ssam log(LOG_ERR, "mp%d: mpdlintr, status %x\n", 135832633Ssam mpcc, mb->mb_status); 135932633Ssam break; 136032633Ssam } 136132633Ssam } 136232633Ssam 136332633Ssam mpdltimeout(mp) 136432633Ssam struct mblok *mp; 136532633Ssam { 136632633Ssam 136732633Ssam mp->mb_status = MP_DLTIME; 136832633Ssam wakeup((caddr_t)&mp->mb_status); 136932633Ssam } 137032633Ssam 137132633Ssam /* 137232633Ssam * Wait for a transfer to complete or a timeout to occur. 137332633Ssam */ 137432633Ssam mpdlwait(dl) 137532633Ssam register struct mpdl *dl; 137632633Ssam { 137732633Ssam int s, error = 0; 137832633Ssam 137932633Ssam s = spl8(); 138032633Ssam dl->mpdl_status = EVSTATUS_GO; 138132633Ssam while (dl->mpdl_status != EVSTATUS_FREE) { 138232633Ssam sleep((caddr_t)&dl->mpdl_status, PZERO+1); 138332633Ssam if (mpdlerr == MP_DLERROR) 138432633Ssam error = EIO; 138532633Ssam } 138632633Ssam splx(s); 138732633Ssam return (error); 138832633Ssam } 138932633Ssam #endif 1390