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 6*34866Sbostic * provided that the above copyright notice and this paragraph are 7*34866Sbostic * duplicated in all such forms and that any documentation, 8*34866Sbostic * advertising materials, and other materials related to such 9*34866Sbostic * distribution and use acknowledge that the software was developed 10*34866Sbostic * by the University of California, Berkeley. The name of the 11*34866Sbostic * University may not be used to endorse or promote products derived 12*34866Sbostic * from this software without specific prior written permission. 13*34866Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34866Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34866Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1634506Skarels * 17*34866Sbostic * @(#)mp.c 7.3 (Berkeley) 06/29/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 } 20632633Ssam mpcmd(ev, EVCMD_OPEN, 0, ms->ms_mb, port); 20732633Ssam while ((tp->t_state & TS_CARR_ON) == 0) 20832633Ssam sleep((caddr_t)&tp->t_rawq, TTIPRI); 20932633Ssam error = mpmodem(unit, MMOD_ON); 21032633Ssam if (error) 21132633Ssam goto bad; 21232633Ssam while ((tp->t_state & TS_CARR_ON) == 0) 21332633Ssam sleep((caddr_t)&tp->t_rawq, TTIPRI); 21432633Ssam error = (*linesw[tp->t_line].l_open)(dev,tp); 21532633Ssam done: 21632633Ssam splx(s); 21732633Ssam /* wakeup anyone waiting for open to complete */ 21832633Ssam wakeup((caddr_t)&tp->t_canq); 21932633Ssam 22032633Ssam return (error); 22132633Ssam bad: 22232633Ssam tp->t_state &= ~TS_WOPEN; 22332633Ssam goto done; 22432633Ssam } 22532633Ssam 22632633Ssam /* 22732633Ssam * Close an mpcc port. 22832633Ssam */ 22934506Skarels /* ARGSUSED */ 23034506Skarels mpclose(dev, flag) 23132633Ssam dev_t dev; 23232633Ssam { 23332633Ssam register struct tty *tp; 23432633Ssam register struct mpport *mp; 23532633Ssam register struct mpevent *ev; 23632633Ssam int s, port, unit, error; 23732633Ssam struct mblok *mb; 23832633Ssam 23932633Ssam unit = minor(dev); 24032633Ssam tp = &mp_tty[unit]; 24132633Ssam port = MPPORT(unit); 24232633Ssam mb = mp_softc[MPUNIT(unit)].ms_mb; 24332633Ssam mp = &mb->mb_port[port]; 24432633Ssam s = spl8(); 24532633Ssam if (mp->mp_flags & MP_PROGRESS) { /* close in progress */ 24632633Ssam if (mp->mp_flags & MP_REMBSY) { 24732633Ssam mp->mp_flags &= ~MP_REMBSY; 24832633Ssam splx(s); 24932633Ssam return (0); 25032633Ssam } 25132633Ssam while (mp->mp_flags & MP_PROGRESS) 25232633Ssam sleep((caddr_t)&tp->t_canq,TTIPRI); 25332633Ssam } 25432633Ssam error = 0; 25532633Ssam mp->mp_flags |= MP_PROGRESS; 25632633Ssam (*linesw[tp->t_line].l_close)(tp); 25732633Ssam if (tp->t_state & TS_HUPCLS || (tp->t_state & TS_ISOPEN) == 0) 25832633Ssam if (error = mpmodem(unit, MMOD_OFF)) { 25932633Ssam mp->mp_flags &= ~MP_PROGRESS; 26032633Ssam goto out; 26132633Ssam } 26232633Ssam while (tp->t_state & TS_FLUSH) /* ??? */ 26332633Ssam sleep((caddr_t)&tp->t_state, TTOPRI); /* ??? */ 26432633Ssam ttyclose(tp); 26532633Ssam ev = mp_getevent(mp, unit); 26632633Ssam if (ev == 0) { 26732633Ssam error = ENOBUFS; 26832633Ssam goto out; 26932633Ssam } 27032633Ssam mpcmd(ev, EVCMD_CLOSE, 0, mb, port); 27132633Ssam out: 27232633Ssam if (mp->mp_flags & MP_REMBSY) 27332633Ssam mpclean(mb, port); 27432633Ssam splx(s); 27532633Ssam return (error); 27632633Ssam } 27732633Ssam 27832633Ssam /* 27932633Ssam * Read from an mpcc port. 28032633Ssam */ 28132633Ssam mpread(dev, uio) 28232633Ssam dev_t dev; 28332633Ssam struct uio *uio; 28432633Ssam { 28532633Ssam struct tty *tp; 28632633Ssam 28732633Ssam tp = &mp_tty[minor(dev)]; 28832633Ssam return ((*linesw[tp->t_line].l_read)(tp, uio)); 28932633Ssam } 29032633Ssam 29132633Ssam /* 29232633Ssam * Write to an mpcc port. 29332633Ssam */ 29432633Ssam mpwrite(dev, uio) 29532633Ssam dev_t dev; 29632633Ssam struct uio *uio; 29732633Ssam { 29832633Ssam struct tty *tp; 29932633Ssam 30032633Ssam tp = &mp_tty[minor(dev)]; 30132633Ssam return ((*linesw[tp->t_line].l_write)(tp, uio)); 30232633Ssam } 30332633Ssam 30432633Ssam /* 30532633Ssam * Ioctl for a mpcc port 30632633Ssam */ 30732633Ssam mpioctl(dev, cmd, data, flag) 30832633Ssam dev_t dev; 30932633Ssam caddr_t data; 31032633Ssam { 31132633Ssam register struct tty *tp; 31232633Ssam register struct mpsoftc *ms; 31332633Ssam register struct mpevent *ev; 31432633Ssam register struct mpport *mp; 31532633Ssam int s, port, error, unit; 31632633Ssam struct mblok *mb; 31732633Ssam 31832633Ssam unit = minor(dev); 31932633Ssam tp = &mp_tty[unit]; 32032633Ssam ms = &mp_softc[MPUNIT(unit)]; 32132633Ssam mb = ms->ms_mb; 32232633Ssam error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag); 32332633Ssam if (error >= 0) 32432633Ssam return (error); 32532633Ssam error = ttioctl(tp, cmd, data, flag); 32632633Ssam if (error >= 0) { 32732633Ssam if (cmd == TIOCSETP || cmd == TIOCSETN || cmd == TIOCLBIS || 32834796Sbostic cmd == TIOCLBIC || cmd == TIOCLSET || cmd == TIOCSETC) { 32932633Ssam ev = mpparam(unit); 33032633Ssam if (ev == 0) 33132633Ssam error = ENOBUFS; 33232633Ssam else 33332633Ssam mpcmd(ev, EVCMD_IOCTL, A_CHGALL, mb, 33432633Ssam MPPORT(unit)); 33532633Ssam } 33632633Ssam return (error); 33732633Ssam } 33832633Ssam switch (cmd) { 33932633Ssam case TIOCSBRK: /* send break */ 34032633Ssam case TIOCCBRK: /* clear break */ 34132633Ssam port = MPPORT(unit); 34232633Ssam mp = &mb->mb_port[port]; 34332633Ssam s = spl8(); 34432633Ssam ev = mp_getevent(mp, unit); 34532633Ssam if (ev) 34632633Ssam mpcmd(ev, EVCMD_IOCTL, 34732633Ssam (cmd == TIOCSBRK ? A_BRKON : A_BRKOFF), 34832633Ssam mb, port); 34932633Ssam else 35032633Ssam error = ENOBUFS; 35132633Ssam splx(s); 35232633Ssam break; 35332633Ssam case TIOCSDTR: /* set dtr control line */ 35432633Ssam break; 35532633Ssam case TIOCCDTR: /* clear dtr control line */ 35632633Ssam break; 35732633Ssam default: 35832633Ssam error = ENOTTY; 35932633Ssam break; 36032633Ssam } 36132633Ssam return (error); 36232633Ssam } 36332633Ssam 36432633Ssam struct mpevent * 36532633Ssam mpparam(unit) 36632633Ssam int unit; 36732633Ssam { 36832633Ssam register struct mpevent *ev; 36932633Ssam register struct mpport *mp; 37032633Ssam register struct tty *tp; 37132633Ssam struct mblok *mb; 37232633Ssam struct mpsoftc *ms; 37332633Ssam register struct asyncparam *asp; 37432633Ssam int port; 37532633Ssam 37632633Ssam ms = &mp_softc[MPUNIT(unit)]; 37732633Ssam mb = ms->ms_mb; 37832633Ssam port = MPPORT(unit); 37932633Ssam mp = &mb->mb_port[port]; 38032633Ssam ev = mp_getevent(mp, unit); /* XXX */ 38132633Ssam if (ev == 0) 38232633Ssam return (ev); 38332633Ssam tp = &mp_tty[unit]; 38432633Ssam /* YUCK */ 38532633Ssam asp = &ms->ms_async[port][mp->mp_on?mp->mp_on-1:MPINSET-1]; 38634796Sbostic asp->ap_xon = (u_char)tp->t_startc; 38734796Sbostic asp->ap_xoff = (u_char)tp->t_stopc; 38834796Sbostic if ((tp->t_flags & RAW) || (tp->t_stopc == -1) || (tp->t_startc == -1)) 38934796Sbostic asp->ap_xena = MPA_DIS; 39034796Sbostic else 39134796Sbostic asp->ap_xena = MPA_ENA; 39233995Sbostic asp->ap_xany = ((tp->t_flags & DECCTQ) ? MPA_DIS : MPA_ENA); 39332633Ssam #ifdef notnow 39432633Ssam if (tp->t_flags & (RAW|LITOUT|PASS8)) { 39532633Ssam #endif 39632633Ssam asp->ap_data = MPCHAR_8; 39732633Ssam asp->ap_parity = MPPAR_NONE; 39832633Ssam #ifdef notnow 39932633Ssam } else { 40032633Ssam asp->ap_data = MPCHAR_7; 40132633Ssam if ((tp->t_flags & (EVENP|ODDP)) == ODDP) 40232633Ssam asp->ap_parity = MPPAR_ODD; 40332633Ssam else 40432633Ssam asp->ap_parity = MPPAR_EVEN; 40532633Ssam } 40632633Ssam #endif 40732633Ssam if (tp->t_ospeed == B110) 40832633Ssam asp->ap_stop = MPSTOP_2; 40932633Ssam else 41032633Ssam asp->ap_stop = MPSTOP_1; 41132633Ssam if (tp->t_ospeed == EXTA || tp->t_ospeed == EXTB) 41232633Ssam asp->ap_baud = M19200; 41332633Ssam else 41432633Ssam asp->ap_baud = tp->t_ospeed; 41532633Ssam asp->ap_loop = MPA_DIS; /* disable loopback */ 41632633Ssam asp->ap_rtimer = A_RCVTIM; /* default receive timer */ 41732633Ssam if (ms->ms_softCAR & (1<<port)) 41832633Ssam setm(&asp->ap_modem, A_DTR, ASSERT); 41932633Ssam else 42032633Ssam setm(&asp->ap_modem, A_DTR, AUTO); 42132633Ssam seti(&asp->ap_intena, A_DCD); 42232633Ssam return (ev); 42332633Ssam } 42432633Ssam 42532633Ssam mpstart(tp) 42632633Ssam register struct tty *tp; 42732633Ssam { 42832633Ssam register struct mpevent *ev; 42932633Ssam register struct mpport *mp; 43032633Ssam struct mblok *mb; 43132633Ssam struct mpsoftc *ms; 43232633Ssam int port, unit, xcnt, n, s, i; 43332633Ssam struct hxmtl *hxp; 43432633Ssam struct clist outq; 43532633Ssam 43632633Ssam s = spl8(); 43732633Ssam unit = minor(tp->t_dev); 43832633Ssam ms = &mp_softc[MPUNIT(unit)]; 43932633Ssam mb = ms->ms_mb; 44032633Ssam port = MPPORT(unit); 44132633Ssam mp = &mb->mb_port[port]; 44232633Ssam hxp = &ms->ms_hxl[port]; 44332633Ssam xcnt = 0; 44432633Ssam outq = tp->t_outq; 44532633Ssam for (i = 0; i < MPXMIT; i++) { 44632633Ssam if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) 44732633Ssam break; 44832633Ssam if (outq.c_cc <= TTLOWAT(tp)) { 44932633Ssam if (tp->t_state & TS_ASLEEP) { 45032633Ssam tp->t_state &= ~TS_ASLEEP; 45132633Ssam wakeup((caddr_t)&tp->t_outq); 45232633Ssam } 45332633Ssam if (tp->t_wsel) { 45432633Ssam selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL); 45532633Ssam tp->t_wsel = 0; 45632633Ssam tp->t_state &= ~TS_WCOLL; 45732633Ssam } 45832633Ssam } 45932633Ssam if (outq.c_cc == 0) 46032633Ssam break; 46132633Ssam /* 46232633Ssam * If we're not currently busy outputting, 46332633Ssam * and there is data to be output, set up 46432633Ssam * port transmit structure to send to mpcc. 46532633Ssam */ 46632633Ssam if (tp->t_flags & (RAW|LITOUT)) 46732633Ssam n = ndqb(&outq, 0); 46832633Ssam else { 46932633Ssam n = ndqb(&outq, 0200); 47032633Ssam if (n == 0) { 47132633Ssam n = getc(&outq); 47232633Ssam timeout(ttrstrt, (caddr_t)tp, (n&0177)+6); 47332633Ssam tp->t_state |= TS_TIMEOUT; 47432633Ssam break; 47532633Ssam } 47632633Ssam } 47734506Skarels hxp->dblock[i] = (caddr_t)kvtophys(outq.c_cf); 47832633Ssam hxp->size[i] = n; 47932633Ssam xcnt++; /* count of xmts to send */ 48032633Ssam ndadvance(&outq, n); 48132633Ssam } 48232633Ssam /* 48332633Ssam * If data to send, poke mpcc. 48432633Ssam */ 48532633Ssam if (xcnt) { 48632633Ssam ev = mp_getevent(mp, unit); 48732633Ssam if (ev == 0) { 48832633Ssam tp->t_state &= ~(TS_BUSY|TS_TIMEOUT); 48932633Ssam } else { 49032633Ssam tp->t_state |= TS_BUSY; 49132633Ssam ev->ev_count = xcnt; 49232633Ssam mpcmd(ev, EVCMD_WRITE, 0, mb, MPPORT(unit)); 49332633Ssam } 49432633Ssam } 49532633Ssam splx(s); 49632633Ssam } 49732633Ssam 49832633Ssam /* 49932633Ssam * Advance cc bytes from q but don't free memory. 50032633Ssam */ 50132633Ssam ndadvance(q, cc) 50232633Ssam register struct clist *q; 50332633Ssam register cc; 50432633Ssam { 50532633Ssam register struct cblock *bp; 50632633Ssam char *end; 50732633Ssam int rem, s; 50832633Ssam 50932633Ssam s = spltty(); 51032633Ssam if (q->c_cc <= 0) 51132633Ssam goto out; 51232633Ssam while (cc>0 && q->c_cc) { 51332633Ssam bp = (struct cblock *)((int)q->c_cf & ~CROUND); 51432633Ssam if ((int)bp == (((int)q->c_cl-1) & ~CROUND)) { 51532633Ssam end = q->c_cl; 51632633Ssam } else { 51732633Ssam end = (char *)((int)bp + sizeof (struct cblock)); 51832633Ssam } 51932633Ssam rem = end - q->c_cf; 52032633Ssam if (cc >= rem) { 52132633Ssam cc -= rem; 52232633Ssam q->c_cc -= rem; 52332633Ssam q->c_cf = bp->c_next->c_info; 52432633Ssam } else { 52532633Ssam q->c_cc -= cc; 52632633Ssam q->c_cf += cc; 52732633Ssam break; 52832633Ssam } 52932633Ssam } 53032633Ssam if (q->c_cc <= 0) { 53132633Ssam q->c_cf = q->c_cl = NULL; 53232633Ssam q->c_cc = 0; 53332633Ssam } 53432633Ssam out: 53532633Ssam splx(s); 53632633Ssam } 53732633Ssam 53832633Ssam /* 53932633Ssam * Stop output on a line, e.g. for ^S/^Q or output flush. 54032633Ssam */ 54134506Skarels /* ARGSUSED */ 54232633Ssam mpstop(tp, rw) 54332633Ssam register struct tty *tp; 54432633Ssam int rw; 54532633Ssam { 54634506Skarels int s; 54732633Ssam 54832633Ssam s = spl8(); 54932633Ssam /* XXX: DISABLE TRANSMITTER */ 55032633Ssam if (tp->t_state & TS_BUSY) { 55132633Ssam if ((tp->t_state & TS_TTSTOP) == 0) 55232633Ssam tp->t_state |= TS_FLUSH; 55332633Ssam } 55432633Ssam splx(s); 55532633Ssam } 55632633Ssam 55732633Ssam /* 55832633Ssam * Initialize an async port's MPCC state. 55932633Ssam */ 56032633Ssam mpportinit(ms, mp, port) 56132633Ssam register struct mpsoftc *ms; 56232633Ssam register struct mpport *mp; 56332633Ssam int port; 56432633Ssam { 56532633Ssam register struct mpevent *ev; 56632633Ssam register int i; 56732633Ssam caddr_t ptr; 56832633Ssam 56932633Ssam mp->mp_on = mp->mp_off = 0; 57032633Ssam mp->mp_nextrcv = 0; 57132633Ssam mp->mp_flags = 0; 57232633Ssam ev = &mp->mp_recvq[0]; 57332633Ssam for (i = 0; ev < &mp->mp_recvq[MPINSET]; ev++, i++) { 57432633Ssam ev->ev_status = EVSTATUS_FREE; 57532633Ssam ev->ev_cmd = 0; 57632633Ssam ev->ev_opts = 0; 57732633Ssam ev->ev_error = 0; 57832633Ssam ev->ev_flags = 0; 57932633Ssam ev->ev_count = 0; 58034506Skarels ev->ev_un.hxl = (struct hxmtl *) kvtophys(&ms->ms_hxl[port]); 58134506Skarels ev->ev_params = (caddr_t) kvtophys(&ms->ms_async[port][i]); 58232633Ssam } 58332633Ssam ev = &mp->mp_sendq[0]; 58432633Ssam for (i = 0; ev < &mp->mp_sendq[MPOUTSET]; ev++, i++) { 58532633Ssam /* init so that L2 can't send any events */ 58632633Ssam /* to host until open has completed */ 58732633Ssam ev->ev_status = EVSTATUS_FREE; 58832633Ssam ev->ev_cmd = 0; 58932633Ssam ev->ev_error = 0; 59032633Ssam ev->ev_flags = 0; 59132633Ssam ev->ev_count = 0; 59232633Ssam ptr = (caddr_t) &ms->ms_cbuf[port][i][0]; 59334506Skarels ev->ev_un.rcvblk = (u_char *)kvtophys(ptr); 59434506Skarels ev->ev_params = (caddr_t) kvtophys(ptr); 59532633Ssam } 59632633Ssam return (0); 59732633Ssam } 59832633Ssam 59932633Ssam /* 60032633Ssam * Send an event to an mpcc. 60132633Ssam */ 60232633Ssam mpcmd(ev, cmd, flags, mb, port) 60332633Ssam register struct mpevent *ev; 60432633Ssam struct mblok *mb; 60532633Ssam { 60632633Ssam int s; 60732633Ssam 60832633Ssam s = spl8(); 60932633Ssam /* move host values to inbound entry */ 61032633Ssam ev->ev_cmd = cmd; 61132633Ssam ev->ev_opts = flags; 61232633Ssam /* show event ready for mpcc */ 61332633Ssam ev->ev_status = EVSTATUS_GO; 61432633Ssam mpintmpcc(mb, port); 61532633Ssam splx(s); 61632633Ssam } 61732633Ssam 61832633Ssam /* 61932633Ssam * Return the next available event entry for the indicated port. 62032633Ssam */ 62132633Ssam struct mpevent * 62232633Ssam mp_getevent(mp, unit) 62332633Ssam register struct mpport *mp; 62432633Ssam int unit; 62532633Ssam { 62632633Ssam register struct mpevent *ev; 62732633Ssam int i, s; 62832633Ssam 62932633Ssam s = spl8(); 63032633Ssam ev = &mp->mp_recvq[mp->mp_on]; 63132633Ssam if (ev->ev_status != EVSTATUS_FREE) 63232633Ssam goto bad; 63332633Ssam /* 63432633Ssam * If not a close request, verify one extra 63532633Ssam * event is available for closing the port. 63632633Ssam */ 63734506Skarels if ((mp->mp_flags & MP_PROGRESS) == 0) { 63832633Ssam if ((i = mp->mp_on + 1) >= MPINSET) 63932633Ssam i = 0; 64032633Ssam if (mp->mp_recvq[i].ev_status != EVSTATUS_FREE) 64132633Ssam goto bad; 64232633Ssam } 64332633Ssam /* init inbound fields marking this entry as busy */ 64432633Ssam ev->ev_error = 0; 64532633Ssam ev->ev_flags = 0; 64632633Ssam ev->ev_count = 0; 64732633Ssam ev->ev_status = EVSTATUS_BUSY; 64832633Ssam /* adjust pointer to next available inbound entry */ 64932633Ssam adjptr(mp->mp_on, MPINSET); 65032633Ssam splx(s); 65132633Ssam return (ev); 65232633Ssam bad: 65332633Ssam splx(s); 65432633Ssam log(LOG_ERR, "mp%d: port%d, out of events", MPUNIT(unit), MPPORT(unit)); 65532633Ssam return ((struct mpevent *)0); 65632633Ssam } 65732633Ssam 65832633Ssam mpmodem(unit, flag) 65932633Ssam int unit, flag; 66032633Ssam { 66132633Ssam struct mpsoftc *ms = &mp_softc[MPUNIT(unit)]; 66232633Ssam int port = MPPORT(unit); 66332633Ssam register struct mpport *mp; 66432633Ssam register struct mpevent *ev; 66532633Ssam register struct asyncparam *asp; 66632633Ssam 66732633Ssam mp = &ms->ms_mb->mb_port[port]; 66832633Ssam ev = mp_getevent(mp, unit); 66932633Ssam if (ev == 0) 67032633Ssam return (ENOBUFS); 67132633Ssam /* YUCK */ 67232633Ssam asp = &ms->ms_async[port][mp->mp_on?mp->mp_on-1:MPINSET-1]; 67332633Ssam if (flag == MMOD_ON) { 67432633Ssam if (ms->ms_softCAR & (1 << port)) 67532633Ssam setm(&asp->ap_modem, A_DTR, ASSERT); 67632633Ssam else 67732633Ssam setm(&asp->ap_modem, A_DTR, AUTO); 67832633Ssam seti(&asp->ap_intena, A_DCD); 67932633Ssam } else { 68032633Ssam setm(&asp->ap_modem, 0, DROP); 68132633Ssam seti(&asp->ap_intena, 0); 68232633Ssam } 68332633Ssam mpcmd(ev, EVCMD_IOCTL, A_MDMCHG, ms->ms_mb, port); 68432633Ssam return (0); 68532633Ssam } 68632633Ssam 68732633Ssam /* 68832633Ssam * Set up the modem control structure according to mask. 68932633Ssam * Each set bit in the mask means assert the corresponding 69032633Ssam * modem control line, otherwise, it will be dropped. 69132633Ssam * RTS is special since it can either be asserted, dropped 69232633Ssam * or put in auto mode for auto modem control. 69332633Ssam */ 69432633Ssam static 69532633Ssam setm(mc, mask, rts) 69632633Ssam register struct mdmctl *mc; 69732633Ssam register int mask; 69832633Ssam { 69932633Ssam 70032633Ssam mc->mc_rngdsr = (mask & A_RNGDSR) ? ASSERT : DROP; 70132633Ssam mc->mc_rate = (mask & A_RATE) ? ASSERT : DROP; 70232633Ssam mc->mc_dcd = (mask & A_DCD) ? ASSERT : DROP; 70332633Ssam mc->mc_sectx = (mask & A_SECTX) ? ASSERT : DROP; 70432633Ssam mc->mc_cts = (mask & A_CTS) ? ASSERT : DROP; 70532633Ssam mc->mc_secrx = (mask & A_SECRX) ? ASSERT : DROP; 70632633Ssam mc->mc_dtr = (mask & A_DTR) ? ASSERT : DROP; 70732633Ssam mc->mc_rts = rts; 70832633Ssam } 70932633Ssam 71032633Ssam /* 71132633Ssam * Set up the status change enable field from mask. 71232633Ssam * When a signal is enabled in this structure and 71332633Ssam * and a change in state on a corresponding modem 71432633Ssam * control line occurs, a status change event will 71532633Ssam * be delivered to the host. 71632633Ssam */ 71732633Ssam static 71832633Ssam seti(mc, mask) 71932633Ssam register struct mdmctl *mc; 72032633Ssam register int mask; 72132633Ssam { 72232633Ssam 72332633Ssam mc->mc_rngdsr = (mask & A_RNGDSR) ? MDM_ON : MDM_OFF; 72432633Ssam mc->mc_rate = (mask & A_RATE) ? MDM_ON : MDM_OFF; 72532633Ssam mc->mc_dcd = (mask & A_DCD) ? MDM_ON : MDM_OFF; 72632633Ssam mc->mc_sectx = (mask & A_SECTX) ? MDM_ON : MDM_OFF; 72732633Ssam mc->mc_cts = (mask & A_CTS) ? MDM_ON : MDM_OFF; 72832633Ssam mc->mc_secrx = (mask & A_SECRX) ? MDM_ON : MDM_OFF; 72932633Ssam mc->mc_dtr = (mask & A_DTR) ? MDM_ON : MDM_OFF; 73032633Ssam mc->mc_rts = (mask & A_RTS) ? MDM_ON : MDM_OFF; 73132633Ssam } 73232633Ssam 73332633Ssam mpcleanport(mb, port) 73432633Ssam struct mblok *mb; 73532633Ssam int port; 73632633Ssam { 73732633Ssam register struct mpport *mp; 73832633Ssam register struct tty *tp; 73932633Ssam 74032633Ssam mp = &mb->mb_port[port]; 74132633Ssam if (mp->mp_proto == MPPROTO_ASYNC) { 74232633Ssam mp->mp_flags = MP_REMBSY; 74334506Skarels /* signal loss of carrier and close */ 74432633Ssam tp = &mp_tty[mb->mb_unit*MPCHUNK+port]; 74532633Ssam ttyflush(tp, FREAD|FWRITE); 74634506Skarels (void) (*linesw[tp->t_line].l_modem)(tp, 0); 74734506Skarels (void) mpclose(tp->t_dev, 0); 74832633Ssam } 74932633Ssam } 75032633Ssam 75132633Ssam mpclean(mb, port) 75232633Ssam register struct mblok *mb; 75332633Ssam int port; 75432633Ssam { 75532633Ssam register struct mpport *mp; 75632633Ssam register struct mpevent *ev; 75732633Ssam register int i; 75834506Skarels u_char list[2]; 75932633Ssam int unit; 76032633Ssam 76132633Ssam mp = &mb->mb_port[port]; 76232633Ssam unit = mb->mb_unit; 76332633Ssam for (i = mp->mp_off; i != mp->mp_on; i = (i+1 % MPINSET)) { 76432633Ssam ev = &mp->mp_recvq[i]; 76532633Ssam ev->ev_error = ENXIO; 76632633Ssam ev->ev_status = EVSTATUS_DONE; 76732633Ssam } 76832633Ssam list[0] = port, list[1] = MPPORT_EOL; 76932633Ssam mpxintr(unit, list); 77032633Ssam mprintr(unit, list); 77132633Ssam /* Clear async for port */ 77232633Ssam mp->mp_proto = MPPROTO_UNUSED; 77332633Ssam mp->mp_flags = 0; 77432633Ssam mp->mp_on = 0; 77532633Ssam mp->mp_off = 0; 77632633Ssam mp->mp_nextrcv = 0; 77732633Ssam 77832633Ssam mp_tty[unit*MPCHUNK + port].t_state = 0; 77932633Ssam for (ev = &mp->mp_sendq[0]; ev < &mp->mp_sendq[MPOUTSET]; ev++) { 78032633Ssam ev->ev_status = EVSTATUS_FREE; 78132633Ssam ev->ev_cmd = 0; 78232633Ssam ev->ev_error = 0; 78332633Ssam ev->ev_un.rcvblk = 0; 78432633Ssam ev->ev_params = 0; 78532633Ssam } 78632633Ssam for (ev = &mp->mp_recvq[0]; ev < &mp->mp_recvq[MPINSET]; ev++) { 78732633Ssam ev->ev_status = EVSTATUS_FREE; 78832633Ssam ev->ev_cmd = 0; 78932633Ssam ev->ev_error = 0; 79032633Ssam ev->ev_params = 0; 79132633Ssam } 79232633Ssam } 79332633Ssam 79432633Ssam /* 79532633Ssam * MPCC interrupt handler. 79632633Ssam */ 79732633Ssam mpintr(mpcc) 79832633Ssam int mpcc; 79932633Ssam { 80032633Ssam register struct mblok *mb; 80132633Ssam register struct his *his; 80232633Ssam 80332633Ssam mb = mp_softc[mpcc].ms_mb; 80432633Ssam if (mb == 0) { 80532633Ssam printf("mp%d: stray interrupt\n", mpcc); 80632633Ssam return; 80732633Ssam } 80832633Ssam his = &mb->mb_hostint; 80932633Ssam his->semaphore &= ~MPSEMA_AVAILABLE; 81032633Ssam /* 81132633Ssam * Check for events to be processed. 81232633Ssam */ 81332633Ssam if (his->proto[MPPROTO_ASYNC].outbdone[0] != MPPORT_EOL) 81432633Ssam mprintr(mpcc, his->proto[MPPROTO_ASYNC].outbdone); 81532633Ssam if (his->proto[MPPROTO_ASYNC].inbdone[0] != MPPORT_EOL) 81632633Ssam mpxintr(mpcc, his->proto[MPPROTO_ASYNC].inbdone); 81732633Ssam if (mb->mb_harderr || mb->mb_softerr) 81832633Ssam mperror(mb, mpcc); 81932633Ssam his->semaphore |= MPSEMA_AVAILABLE; 82032633Ssam } 82132633Ssam 82232633Ssam /* 82332633Ssam * Handler for processing completion of transmitted events. 82432633Ssam */ 82532633Ssam mpxintr(unit, list) 82634506Skarels register u_char *list; 82732633Ssam { 82832633Ssam register struct mpport *mp; 82932633Ssam register struct mpevent *ev; 83032633Ssam register struct mblok *mb; 83132633Ssam register struct tty *tp; 83232633Ssam register struct asyncparam *ap; 83332633Ssam struct mpsoftc *ms; 83432633Ssam int port, i, j; 83532633Ssam 83632633Ssam ms = &mp_softc[unit]; 83732633Ssam mb = mp_softc[unit].ms_mb; 83832633Ssam for (j = 0; j < MPMAXPORT && ((port = *list++) != MPPORT_EOL); j++) { 83932633Ssam /* 84032633Ssam * Process each completed entry in the inbound queue. 84132633Ssam */ 84232633Ssam mp = &mb->mb_port[port]; 84332633Ssam tp = &mp_tty[unit*MPCHUNK + port]; 84432633Ssam #define nextevent(mp) &mp->mp_recvq[mp->mp_off] 84532633Ssam ev = nextevent(mp); 84632633Ssam for(; ev->ev_status & EVSTATUS_DONE; ev = nextevent(mp)) { 84732633Ssam /* YUCK */ 84832633Ssam ap = &ms->ms_async[port][mp->mp_off]; 84934506Skarels mppurge((caddr_t)ap, (int)sizeof (*ap)); 85032633Ssam switch (ev->ev_cmd) { 85132633Ssam case EVCMD_OPEN: 85232633Ssam /* 85332633Ssam * Open completion, start all reads and 85432633Ssam * assert modem status information. 85532633Ssam */ 85632633Ssam for (i = 0; i < MPOUTSET; i++) 85732633Ssam mp->mp_sendq[i].ev_status = EVSTATUS_GO; 85832633Ssam (*linesw[tp->t_line].l_modem) 85932633Ssam (tp, ap->ap_modem.mc_dcd == ASSERT); 86032633Ssam break; 86132633Ssam case EVCMD_CLOSE: 86232633Ssam /* 86332633Ssam * Close completion, flush all pending 86432633Ssam * transmissions, free resources, and 86532633Ssam * cleanup mpcc port state. 86632633Ssam */ 86732633Ssam for (i = 0; i < MPOUTSET; i++) { 86832633Ssam mp->mp_sendq[i].ev_status = 86932633Ssam EVSTATUS_FREE; 87032633Ssam mp->mp_sendq[i].ev_un.rcvblk = 0; 87132633Ssam mp->mp_sendq[i].ev_params = 0; 87232633Ssam } 87332633Ssam tp->t_state &= ~TS_CARR_ON; 87432633Ssam mp->mp_on = mp->mp_off = mp->mp_nextrcv = 0; 87532633Ssam mp->mp_flags &= ~MP_PROGRESS; 87632633Ssam mp->mp_proto = MPPROTO_UNUSED; 87732633Ssam wakeup((caddr_t)&tp->t_canq); /* ??? */ 87832633Ssam goto done; 87932633Ssam case EVCMD_IOCTL: 88032633Ssam /* 88132633Ssam * Nothing to do, just pitch. 88232633Ssam */ 88332633Ssam break; 88432633Ssam case EVCMD_WRITE: 88532633Ssam /* 88632633Ssam * Transmission completed, update tty 88732633Ssam * state and restart output. 88832633Ssam */ 88932633Ssam tp->t_state &= ~TS_BUSY; 89032633Ssam if (tp->t_state & TS_FLUSH) { 89132633Ssam tp->t_state &= ~TS_FLUSH; 89232633Ssam wakeup((caddr_t)&tp->t_state); 89332633Ssam } else { 89434506Skarels register int cc = 0, n; 89532633Ssam struct hxmtl *hxp; 89632633Ssam 89732633Ssam hxp = &ms->ms_hxl[port]; 89834506Skarels for(n = 0; n < ev->ev_count; n++) 89934506Skarels cc += hxp->size[n]; 90032633Ssam ndflush(&tp->t_outq, cc); 90132633Ssam } 90232633Ssam switch (ev->ev_error) { 90332633Ssam case A_SIZERR: /*# error in xmt data size */ 90432633Ssam mplog(unit, port, A_XSIZE, 0); 90532633Ssam break; 90632633Ssam case A_NXBERR: /*# no more xmt evt buffers */ 90732633Ssam mplog(unit, port, A_NOXBUF, 0); 90832633Ssam break; 90932633Ssam } 91032633Ssam mpstart(tp); 91132633Ssam break; 91232633Ssam default: 91334506Skarels mplog(unit, port, A_INVCMD, (int)ev->ev_cmd); 91432633Ssam break; 91532633Ssam } 91632633Ssam /* re-init all values in this entry */ 91732633Ssam ev->ev_cmd = 0; 91832633Ssam ev->ev_opts = 0; 91932633Ssam ev->ev_error = 0; 92032633Ssam ev->ev_flags = 0; 92132633Ssam ev->ev_count = 0; 92232633Ssam /* show this entry is available for use */ 92332633Ssam ev->ev_status = EVSTATUS_FREE; 92432633Ssam adjptr(mp->mp_off, MPINSET); 92532633Ssam #undef nextevent 92632633Ssam } 92732633Ssam done: 92832633Ssam ; 92932633Ssam } 93032633Ssam } 93132633Ssam 93232633Ssam /* 93332633Ssam * Handler for processing received events. 93432633Ssam */ 93532633Ssam mprintr(unit, list) 93634506Skarels u_char *list; 93732633Ssam { 93832633Ssam register struct tty *tp; 93932633Ssam register struct mpport *mp; 94032633Ssam register struct mpevent *ev; 94132633Ssam struct mblok *mb; 94232633Ssam register int cc; 94332633Ssam register char *cp; 94432633Ssam struct mpsoftc *ms; 94532633Ssam caddr_t ptr; 94632633Ssam char *rcverr; 94732633Ssam int port, i; 94832633Ssam 94932633Ssam ms = &mp_softc[unit]; 95032633Ssam mb = mp_softc[unit].ms_mb; 95132633Ssam for (i = 0; i < MPMAXPORT && (port = *list++) != MPPORT_EOL; i++) { 95232633Ssam tp = &mp_tty[unit*MPCHUNK + port]; 95332633Ssam mp = &mb->mb_port[port]; 95432633Ssam ev = &mp->mp_sendq[mp->mp_nextrcv]; 95532633Ssam while (ev->ev_status & EVSTATUS_DONE) { 95632633Ssam if (ev->ev_cmd != EVCMD_READ && 95732633Ssam ev->ev_cmd != EVCMD_STATUS) { 95832633Ssam mplog(unit, port, "unexpected command", 95934506Skarels (int)ev->ev_cmd); 96032633Ssam goto next; 96132633Ssam } 96232633Ssam if (ev->ev_cmd == EVCMD_STATUS) { 96332633Ssam /* 96432633Ssam * Status change, look for carrier changes. 96532633Ssam */ 96632633Ssam if (ev->ev_opts == DCDASRT || 96732633Ssam ev->ev_opts == DCDDROP) 96832633Ssam (*linesw[tp->t_line].l_modem) 96932633Ssam (tp, ev->ev_opts == DCDASRT); 97032633Ssam else 97132633Ssam mplog(unit, port, 97232633Ssam "unexpect status command", 97334506Skarels (int)ev->ev_opts); 97432633Ssam goto next; 97532633Ssam } 97632633Ssam /* 97732633Ssam * Process received data. 97832633Ssam */ 97932633Ssam if ((tp->t_state & (TS_ISOPEN|TS_WOPEN)) == 0) 98032633Ssam goto next; 98132633Ssam cc = ev->ev_count; 98232633Ssam if (cc == 0) 98332633Ssam goto next; 98432633Ssam /* YUCK */ 98532633Ssam cp = ms->ms_cbuf[port][mp->mp_nextrcv]; 98632633Ssam mppurge(cp, CBSIZE); 98732633Ssam while (cc-- > 0) { 98832633Ssam /* 98932633Ssam * A null character is inserted, potentially 99032633Ssam * when a break or framing error occurs. If 99132633Ssam * we're not in raw mode, substitute the 99232633Ssam * interrupt character. 99332633Ssam */ 99432633Ssam if (*cp == 0 && 99532633Ssam (ev->ev_error == BRKASRT || 99632633Ssam ev->ev_error == FRAMERR)) 99732633Ssam if ((tp->t_flags&RAW) == 0) 99832633Ssam *cp = tp->t_intrc; 99932633Ssam (*linesw[tp->t_line].l_rint)(*cp++, tp); 100032633Ssam } 100132633Ssam /* setup for next read */ 100232633Ssam ptr = (caddr_t)&mp_softc[unit].ms_cbuf[port][mp->mp_nextrcv][0]; 100334506Skarels ev->ev_un.rcvblk = (u_char *)kvtophys(ptr); 100434506Skarels ev->ev_params = (caddr_t) kvtophys(ptr); 100533673Sbostic switch(ev->ev_error) { 100633673Sbostic case RCVDTA: /* Normal (good) rcv data */ 100733673Sbostic /* do not report the following */ 100833673Sbostic /* they are "normal" errors */ 100933673Sbostic case FRAMERR: /* frame error */ 101033673Sbostic case BRKASRT: /* Break condition */ 101132633Ssam case PARERR: /* parity error */ 101233673Sbostic rcverr = (char *)0; 101332633Ssam break; 101432633Ssam case OVRNERR: /* Overrun error */ 101532633Ssam rcverr = "overrun error"; 101632633Ssam break; 101732633Ssam case OVFERR: /* Overflow error */ 101832633Ssam rcverr = "overflow error"; 101932633Ssam break; 102032633Ssam default: 102132633Ssam rcverr = "undefined rcv error"; 102232633Ssam } 102332633Ssam if (rcverr != (char *)0) 102434506Skarels mplog(unit, port, rcverr, (int)ev->ev_error); 102532633Ssam next: 102632633Ssam ev->ev_cmd = 0; 102732633Ssam ev->ev_opts = 0; 102832633Ssam ev->ev_error = 0; 102932633Ssam ev->ev_flags = 0; 103032633Ssam ev->ev_status = EVSTATUS_GO; /* start next read */ 103132633Ssam adjptr(mp->mp_nextrcv, MPOUTSET); 103232633Ssam ev = &mp->mp_sendq[mp->mp_nextrcv]; 103332633Ssam } 103432633Ssam } 103532633Ssam } 103632633Ssam 103732633Ssam /* 103832633Ssam * Log an mpcc diagnostic. 103932633Ssam */ 104032633Ssam mplog(unit, port, cp, flags) 104132633Ssam char *cp; 104232633Ssam { 104332633Ssam 104432633Ssam if (flags) 104532633Ssam log(LOG_ERR, "mp%d: port%d, %s (%d)\n", 104632633Ssam unit, port, cp, flags); 104732633Ssam else 104832633Ssam log(LOG_ERR, "mp%d: port%d, %s\n", unit, port, cp); 104932633Ssam } 105032633Ssam 105132633Ssam int MPHOSTINT = 1; 105232633Ssam 105332633Ssam mptimeint(mb) 105432633Ssam register struct mblok *mb; 105532633Ssam { 105632633Ssam 105732633Ssam mb->mb_mpintcnt = 0; 105832633Ssam mb->mb_mpintclk = (caddr_t)0; 105932633Ssam *(u_short *)mpinfo[mb->mb_unit]->ui_addr = 2; 106032633Ssam } 106132633Ssam 106232633Ssam /* 106332633Ssam * Interupt mpcc 106432633Ssam */ 106532633Ssam mpintmpcc(mb, port) 106632633Ssam register struct mblok *mb; 106732633Ssam { 106832633Ssam 106932633Ssam mb->mb_intr[port] |= MPSEMA_WORK; 107032633Ssam if (++mb->mb_mpintcnt == MPHOSTINT) { 107132633Ssam mb->mb_mpintcnt = 0; 107232633Ssam *(u_short *)mpinfo[mb->mb_unit]->ui_addr = 2; 107332633Ssam if (mb->mb_mpintclk) { 107434506Skarels untimeout(mptimeint, (caddr_t)mb); 107532633Ssam mb->mb_mpintclk = 0; 107632633Ssam } 107732633Ssam } else { 107832633Ssam if (mb->mb_mpintclk == 0) { 107934506Skarels timeout(mptimeint, (caddr_t)mb, 4); 108032633Ssam mb->mb_mpintclk = (caddr_t)1; 108132633Ssam } 108232633Ssam } 108332633Ssam } 108432633Ssam 108532633Ssam static char *mpherrmsg[] = { 108632633Ssam "", 108732633Ssam "Bus error", /* MPBUSERR */ 108832633Ssam "Address error", /* ADDRERR */ 108932633Ssam "Undefined ecc interrupt", /* UNDECC */ 109032633Ssam "Undefined interrupt", /* UNDINT */ 109132633Ssam "Power failure occurred", /* PWRFL */ 109232633Ssam "Stray transmit done interrupt", /* NOXENTRY */ 109332633Ssam "Two fast timers on one port", /* TWOFTMRS */ 109432633Ssam "Interrupt queue full", /* INTQFULL */ 109532633Ssam "Interrupt queue ack error", /* INTQERR */ 109632633Ssam "Uncorrectable dma parity error", /* CBPERR */ 109732633Ssam "32 port ACAP failed power up", /* ACPDEAD */ 109832633Ssam }; 109932633Ssam #define NHERRS (sizeof (mpherrmsg) / sizeof (mpherrmsg[0])) 110032633Ssam 110132633Ssam mperror(mb, unit) 110232633Ssam register struct mblok *mb; 110332633Ssam int unit; 110432633Ssam { 110532633Ssam register char *cp; 110632633Ssam register int i; 110732633Ssam 110832633Ssam if (mb->mb_softerr) { 110932633Ssam switch (mb->mb_softerr) { 111032633Ssam case DMAPERR: /* dma parity error */ 111132633Ssam cp = "dma parity error"; 111232633Ssam break; 111332633Ssam case ECCERR: 111432633Ssam cp = "local memory ecc error"; 111532633Ssam break; 111632633Ssam default: 111732633Ssam cp = "unknown error"; 111832633Ssam break; 111932633Ssam } 112032633Ssam log(LOG_ERR, "mp%d: soft error, %s", unit, cp); 112132633Ssam mb->mb_softerr = 0; 112232633Ssam } 112332633Ssam if (mb->mb_harderr) { 112432633Ssam if (mb->mb_harderr < NHERRS) 112532633Ssam cp = mpherrmsg[mb->mb_harderr]; 112632633Ssam else 112732633Ssam cp = "unknown error"; 112832633Ssam log(LOG_ERR, "mp%d: hard error, %s", unit, cp); 112932633Ssam if (mb->mb_status == MP_OPOPEN) { 113032633Ssam for (i = 0; i < MPMAXPORT; i++) { 113132633Ssam mpcleanport(mb, i); 113232633Ssam mb->mb_proto[i] = MPPROTO_UNUSED; 113332633Ssam } 113432633Ssam } 113532633Ssam mb->mb_harderr = 0; 113632633Ssam mb->mb_status = 0; 113732633Ssam } 113832633Ssam } 113932633Ssam 114032633Ssam mppurge(addr, cc) 114132633Ssam register caddr_t addr; 114232633Ssam register int cc; 114332633Ssam { 114432633Ssam 114532633Ssam for (; cc >= 0; addr += NBPG, cc -= NBPG) 114632633Ssam mtpr(P1DC, addr); 114732633Ssam } 114832633Ssam 114932633Ssam /* 115032633Ssam * MPCC Download Pseudo-device. 115132633Ssam */ 115232633Ssam char mpdlbuf[MPDLBUFSIZE]; 115332633Ssam int mpdlbusy; /* interlock on download buffer */ 115432633Ssam int mpdlerr; 115532633Ssam 115632633Ssam mpdlopen(dev) 115732633Ssam dev_t dev; 115832633Ssam { 115932633Ssam int unit, mpu; 116032633Ssam struct vba_device *vi; 116132633Ssam 116232633Ssam unit = minor(dev); 116332633Ssam mpu = MPUNIT(unit); 116432633Ssam if (mpu >= NMP || (vi = mpinfo[mpu]) == 0 || vi->ui_alive == 0) 116532633Ssam return (ENODEV); 116632633Ssam return (0); 116732633Ssam } 116832633Ssam 116932633Ssam mpdlwrite(dev, uio) 117032633Ssam dev_t dev; 117132633Ssam struct uio *uio; 117232633Ssam { 117332633Ssam register struct mpsoftc *ms = &mp_softc[MPUNIT(minor(dev))]; 117432633Ssam register struct mpdl *dl; 117532633Ssam int error; 117632633Ssam 117732633Ssam if (ms->ms_mb == 0 || ms->ms_mb->mb_status != MP_DLOPEN) 117832633Ssam return (EFAULT); 117932633Ssam dl = &ms->ms_mb->mb_dl; 118032633Ssam dl->mpdl_count = uio->uio_iov->iov_len; 118134506Skarels dl->mpdl_data = (caddr_t) kvtophys(mpdlbuf); 118234506Skarels if (error = uiomove(mpdlbuf, (int)dl->mpdl_count, UIO_WRITE, uio)) 118332633Ssam return (error); 118432633Ssam uio->uio_resid -= dl->mpdl_count; /* set up return from write */ 118532633Ssam dl->mpdl_cmd = MPDLCMD_NORMAL; 118632633Ssam error = mpdlwait(dl); 118732633Ssam return (error); 118832633Ssam } 118932633Ssam 119032633Ssam mpdlclose(dev) 119132633Ssam dev_t dev; 119232633Ssam { 119332633Ssam register struct mblok *mb = mp_softc[MPUNIT(minor(dev))].ms_mb; 119432633Ssam 119532633Ssam if (mb == 0 || mb->mb_status != MP_DLDONE) { 119632633Ssam mpbogus.status = 0; 119732633Ssam if (mpbogus.mb == mpbogus.mbloks[MPUNIT(minor(dev))]) 119832633Ssam mpdlbusy--; 119932633Ssam return (EEXIST); 120032633Ssam } 120132633Ssam mb->mb_status = MP_OPOPEN; 120232633Ssam mpbogus.status = 0; 120332633Ssam /* set to dead, for board handshake */ 120432633Ssam mb->mb_hostint.imok = MPIMOK_DEAD; 120532633Ssam return (0); 120632633Ssam } 120732633Ssam 120832633Ssam int mpdltimeout(); 120932633Ssam 121034506Skarels /* ARGSUSED */ 121132633Ssam mpdlioctl(dev, cmd, data, flag) 121232633Ssam dev_t dev; 121332633Ssam caddr_t data; 121432633Ssam { 121532633Ssam register struct mblok *mb; 121632633Ssam register struct mpdl *dl; 121734506Skarels int unit, error, s, i; 121832633Ssam 121932633Ssam mb = mp_softc[unit=MPUNIT(minor(dev))].ms_mb; 122032633Ssam if (mb == 0) 122132633Ssam return (EEXIST); 122232633Ssam dl = &mb->mb_dl; 122332633Ssam error = 0; 122432633Ssam switch (cmd) { 122532633Ssam case MPIOPORTMAP: 122632633Ssam bcopy(data, (caddr_t)mb->mb_proto, sizeof (mb->mb_proto)); 122732633Ssam break; 122832633Ssam case MPIOHILO: 122932633Ssam bcopy(data, (caddr_t)&mb->mb_hiport, 2*(sizeof(mb->mb_hiport))); 123032633Ssam break; 123132633Ssam case MPIOENDDL: 123232633Ssam dl->mpdl_count = 0; 123332633Ssam dl->mpdl_data = 0; 123432633Ssam dl->mpdl_cmd = MPIOENDDL&IOCPARM_MASK; 123532633Ssam error = mpdlwait(dl); 123632633Ssam mpccinit(unit); 123732633Ssam mb->mb_status = MP_DLDONE; 123832633Ssam mpdlbusy--; 123932633Ssam break; 124032633Ssam case MPIOENDCODE: 124132633Ssam dl->mpdl_count = 0; 124232633Ssam dl->mpdl_data = 0; 124332633Ssam dl->mpdl_cmd = MPIOENDCODE&IOCPARM_MASK; 124432633Ssam error = mpdlwait(dl); 124532633Ssam break; 124632633Ssam case MPIOASYNCNF: 124732633Ssam bcopy(data, mpdlbuf, sizeof (struct abdcf)); 124834506Skarels dl->mpdl_data = (caddr_t) kvtophys(mpdlbuf); 124932633Ssam dl->mpdl_count = sizeof (struct abdcf); 125032633Ssam dl->mpdl_cmd = MPIOASYNCNF&IOCPARM_MASK; 125132633Ssam error = mpdlwait(dl); 125232633Ssam break; 125332633Ssam case MPIOSTARTDL: 125432633Ssam while (mpdlbusy) 125532633Ssam sleep((caddr_t)&mpdlbusy, PZERO+1); 125632633Ssam mpdlbusy++; 125732633Ssam /* initialize the downloading interface */ 125832633Ssam mpbogus.magic = MPMAGIC; 125932633Ssam mpbogus.mb = mpbogus.mbloks[unit]; 126032633Ssam mpbogus.status = 1; 126132633Ssam dl->mpdl_status = EVSTATUS_FREE; 126232633Ssam dl->mpdl_count = 0; 126332633Ssam dl->mpdl_cmd = 0; 126432633Ssam dl->mpdl_data = (char *) 0; 126532633Ssam mpdlerr = 0; 126632633Ssam mb->mb_magic = MPMAGIC; 126732633Ssam mb->mb_ivec = mp_softc[unit].ms_ivec+1; /* download vector */ 126832633Ssam mb->mb_status = MP_DLPEND; 126932633Ssam mb->mb_diagswitch[0] = 'A'; 127032633Ssam mb->mb_diagswitch[1] = 'P'; 127132633Ssam s = spl8(); 127232633Ssam *(u_short *)mpinfo[unit]->ui_addr = 2; 127334506Skarels timeout(mpdltimeout, (caddr_t)mb, 30*hz); 127432633Ssam sleep((caddr_t)&mb->mb_status, PZERO+1); 127532633Ssam splx(s); 127632633Ssam if (mb->mb_status == MP_DLOPEN) { 127734506Skarels untimeout(mpdltimeout, (caddr_t)mb); 127832633Ssam } else if (mb->mb_status == MP_DLTIME) { 127932633Ssam mpbogus.status = 0; 128032633Ssam error = ETIMEDOUT; 128132633Ssam } else { 128232633Ssam mpbogus.status = 0; 128332633Ssam error = ENXIO; 128432633Ssam log(LOG_ERR, "mp%d: start download: unknown status %x", 128532633Ssam unit, mb->mb_status); 128632633Ssam } 128734506Skarels bzero((caddr_t)mb->mb_port, sizeof (mb->mb_port)); 128832633Ssam break; 128932633Ssam case MPIORESETBOARD: 129032633Ssam s = spl8(); 129132633Ssam if (mb->mb_imokclk) 129232633Ssam mb->mb_imokclk = 0; 129332633Ssam *(u_short *)mpinfo[unit]->ui_addr = 0x100; 129432633Ssam if (mb->mb_status == MP_DLOPEN || mb->mb_status == MP_DLDONE) { 129532633Ssam mpdlerr = MP_DLERROR; 129632633Ssam dl->mpdl_status = EVSTATUS_FREE; 129732633Ssam wakeup((caddr_t)&dl->mpdl_status); 129832633Ssam mpbogus.status = 0; 129932633Ssam } 130032633Ssam for (i = 0; i < MPMAXPORT; i++) { 130132633Ssam if (mb->mb_harderr || mb->mb_softerr) 130232633Ssam mperror(mb, i); 130332633Ssam mpcleanport(mb, i); 130432633Ssam mb->mb_proto[i] = MPPROTO_UNUSED; 130532633Ssam } 130632633Ssam mb->mb_status = 0; 130732633Ssam splx(s); 130832633Ssam break; 130932633Ssam default: 131032633Ssam error = EINVAL; 131132633Ssam break; 131232633Ssam } 131332633Ssam return (error); 131432633Ssam } 131532633Ssam 131632633Ssam mpccinit(unit) 131732633Ssam int unit; 131832633Ssam { 131932633Ssam register struct mblok *mb = mp_softc[unit].ms_mb; 132032633Ssam register struct his *his; 132132633Ssam register int i, j; 132232633Ssam 132332633Ssam mb->mb_status = MP_DLDONE; 132432633Ssam mb->mb_ivec = mp_softc[unit].ms_ivec; 132532633Ssam mb->mb_magic = MPMAGIC; 132632633Ssam /* Init host interface structure */ 132732633Ssam his = &mb->mb_hostint; 132832633Ssam his->semaphore = MPSEMA_AVAILABLE; 132932633Ssam for (i = 0; i < NMPPROTO; i++) 133032633Ssam for (j = 0; j < MPMAXPORT; j++) { 133132633Ssam his->proto[i].inbdone[j] = MPPORT_EOL; 133232633Ssam his->proto[i].outbdone[j] = MPPORT_EOL; 133332633Ssam } 133432633Ssam mb->mb_unit = unit; 133532633Ssam } 133632633Ssam 133732633Ssam mpdlintr(mpcc) 133832633Ssam int mpcc; 133932633Ssam { 134032633Ssam register struct mblok *mb; 134132633Ssam register struct mpdl *dl; 134232633Ssam 134332633Ssam mb = mp_softc[mpcc].ms_mb; 134432633Ssam if (mb == 0) { 134532633Ssam printf("mp%d: stray download interrupt\n", mpcc); 134632633Ssam return; 134732633Ssam } 134832633Ssam dl = &mb->mb_dl; 134932633Ssam switch (mb->mb_status) { 135032633Ssam case MP_DLOPEN: 135132633Ssam if (dl->mpdl_status != EVSTATUS_DONE) 135232633Ssam mpdlerr = MP_DLERROR; 135332633Ssam dl->mpdl_status = EVSTATUS_FREE; 135432633Ssam wakeup((caddr_t)&dl->mpdl_status); 135532633Ssam return; 135632633Ssam case MP_DLPEND: 135732633Ssam mb->mb_status = MP_DLOPEN; 135834506Skarels wakeup((caddr_t)&mb->mb_status); 135932633Ssam /* fall thru... */ 136032633Ssam case MP_DLTIME: 136132633Ssam return; 136232633Ssam case MP_OPOPEN: 136332633Ssam if (mb->mb_imokclk) 136432633Ssam mb->mb_imokclk = 0; 136532633Ssam mb->mb_nointcnt = 0; /* reset no interrupt count */ 136632633Ssam mb->mb_hostint.imok = MPIMOK_DEAD; 136732633Ssam mb->mb_imokclk = (caddr_t)1; 136832633Ssam break; 136932633Ssam default: 137032633Ssam log(LOG_ERR, "mp%d: mpdlintr, status %x\n", 137132633Ssam mpcc, mb->mb_status); 137232633Ssam break; 137332633Ssam } 137432633Ssam } 137532633Ssam 137632633Ssam mpdltimeout(mp) 137732633Ssam struct mblok *mp; 137832633Ssam { 137932633Ssam 138032633Ssam mp->mb_status = MP_DLTIME; 138132633Ssam wakeup((caddr_t)&mp->mb_status); 138232633Ssam } 138332633Ssam 138432633Ssam /* 138532633Ssam * Wait for a transfer to complete or a timeout to occur. 138632633Ssam */ 138732633Ssam mpdlwait(dl) 138832633Ssam register struct mpdl *dl; 138932633Ssam { 139032633Ssam int s, error = 0; 139132633Ssam 139232633Ssam s = spl8(); 139332633Ssam dl->mpdl_status = EVSTATUS_GO; 139432633Ssam while (dl->mpdl_status != EVSTATUS_FREE) { 139532633Ssam sleep((caddr_t)&dl->mpdl_status, PZERO+1); 139632633Ssam if (mpdlerr == MP_DLERROR) 139732633Ssam error = EIO; 139832633Ssam } 139932633Ssam splx(s); 140032633Ssam return (error); 140132633Ssam } 140232633Ssam #endif 1403