]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/si/si.c
Merge OpenSSL 1.0.1m.
[FreeBSD/FreeBSD.git] / sys / dev / si / si.c
1 /*-
2  * Device driver for Specialix range (SI/XIO) of serial line multiplexors.
3  *
4  * Copyright (C) 1990, 1992, 1998 Specialix International,
5  * Copyright (C) 1993, Andy Rutter <andy@acronym.co.uk>
6  * Copyright (C) 2000, Peter Wemm <peter@netplex.com.au>
7  *
8  * Originally derived from:     SunOS 4.x version
9  * Ported from BSDI version to FreeBSD by Peter Wemm.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notices, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notices, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by Andy Rutter of
22  *      Advanced Methods and Tools Ltd. based on original information
23  *      from Specialix International.
24  * 4. Neither the name of Advanced Methods and Tools, nor Specialix
25  *    International may be used to endorse or promote products derived from
26  *    this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
31  * NO EVENT SHALL THE AUTHORS BE LIABLE.
32  *
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #ifndef lint
39 __IDSTRING(si_copyright1, "@(#) Copyright (C) Specialix International, 1990,1992,1998");
40 __IDSTRING(si_copyright2, "@(#) Copyright (C) Andy Rutter 1993");
41 __IDSTRING(si_copyright3, "@(#) Copyright (C) Peter Wemm 2000");
42 #endif  /* not lint */
43
44 #include "opt_compat.h"
45 #include "opt_debug_si.h"
46 #include "opt_eisa.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/serial.h>
51 #include <sys/tty.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/priv.h>
57 #include <sys/sysctl.h>
58 #include <sys/bus.h>
59 #include <machine/bus.h>
60 #include <sys/rman.h>
61 #include <machine/resource.h>
62
63
64 #include <vm/vm.h>
65 #include <vm/pmap.h>
66
67 #include <machine/stdarg.h>
68
69 #include <dev/si/sireg.h>
70 #include <dev/si/sivar.h>
71 #include <dev/si/si.h>
72
73 /*
74  * This device driver is designed to interface the Specialix International
75  * SI, XIO and SX range of serial multiplexor cards to FreeBSD on an ISA,
76  * EISA or PCI bus machine.
77  *
78  * The controller is interfaced to the host via dual port RAM
79  * and an interrupt.
80  *
81  * The code for the Host 1 (very old ISA cards) has not been tested.
82  */
83
84 #undef  POLL            /* turn on poller to scan for lost interrupts */
85 #if 0
86 #define REALPOLL        /* on each poll, scan for work regardless */
87 #endif
88 #define POLLHZ  (hz/10) /* 10 times per second */
89 #define SI_I_HIGH_WATER (TTYHOG - 2 * SI_BUFFERSIZE)
90 #define INT_COUNT 25000         /* max of 125 ints per second */
91 #define JET_INT_COUNT 100       /* max of 100 ints per second */
92 #define RXINT_COUNT 1   /* one rxint per 10 milliseconds */
93
94 static void si_command(struct si_port *, int, int);
95 static int si_Sioctl(struct cdev *, u_long, caddr_t, int, struct thread *);
96 /* static void si_stop(struct tty *, int); */
97 #if 0
98 static timeout_t si_lstart;
99 #endif
100
101 static tsw_outwakeup_t si_start;
102 static tsw_ioctl_t siioctl;
103 static tsw_close_t siclose;
104 static tsw_modem_t simodem;
105 static tsw_open_t siopen;
106 static tsw_param_t siparam;
107
108 static void     si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip);
109 static char *   si_modulename(int host_type, int uart_type);
110
111 static struct cdevsw si_Scdevsw = {
112         .d_version =    D_VERSION,
113         .d_ioctl =      si_Sioctl,
114         .d_name =       "si",
115         .d_flags =      D_TTY | D_NEEDGIANT,
116 };
117
118 static int si_Nports;
119 static int si_Nmodules;
120 static int si_debug;
121
122 SYSCTL_INT(_machdep, OID_AUTO, si_debug, CTLFLAG_RWTUN, &si_debug, 0, "");
123
124 static int si_numunits;
125
126 devclass_t si_devclass;
127
128 struct si_speedtab {
129         int sp_speed;                   /* Speed. */
130         int sp_code;                    /* Code. */
131 };
132
133 #ifndef B2000   /* not standard, but the hardware knows it. */
134 # define B2000 2000
135 #endif
136 static struct si_speedtab bdrates[] = {
137         { B75,          CLK75, },       /* 0x0 */
138         { B110,         CLK110, },      /* 0x1 */
139         { B150,         CLK150, },      /* 0x3 */
140         { B300,         CLK300, },      /* 0x4 */
141         { B600,         CLK600, },      /* 0x5 */
142         { B1200,        CLK1200, },     /* 0x6 */
143         { B2000,        CLK2000, },     /* 0x7 */
144         { B2400,        CLK2400, },     /* 0x8 */
145         { B4800,        CLK4800, },     /* 0x9 */
146         { B9600,        CLK9600, },     /* 0xb */
147         { B19200,       CLK19200, },    /* 0xc */
148         { B38400,       CLK38400, },    /* 0x2 (out of order!) */
149         { B57600,       CLK57600, },    /* 0xd */
150         { B115200,      CLK110, },      /* 0x1 (dupe!, 110 baud on "si") */
151         { -1,           -1 },
152 };
153
154
155 #ifdef POLL
156 static int si_pollrate;                 /* in addition to irq */
157 static int si_realpoll = 0;             /* poll HW on timer */
158
159 SYSCTL_INT(_machdep, OID_AUTO, si_pollrate, CTLFLAG_RW, &si_pollrate, 0, "");
160 SYSCTL_INT(_machdep, OID_AUTO, si_realpoll, CTLFLAG_RW, &si_realpoll, 0, "");
161
162 static int init_finished = 0;
163 static void si_poll(void *);
164 #endif
165
166 /*
167  * Array of adapter types and the corresponding RAM size. The order of
168  * entries here MUST match the ordinal of the adapter type.
169  */
170 static const char *si_type[] = {
171         "EMPTY",
172         "SIHOST",
173         "SIMCA",                /* FreeBSD does not support Microchannel */
174         "SIHOST2",
175         "SIEISA",
176         "SIPCI",
177         "SXPCI",
178         "SXISA",
179 };
180
181 #ifdef SI_DEBUG
182 static char *
183 si_cmdname(int cmd)
184 {
185         static char buf[32];
186
187         switch (cmd) {
188         case IDLE_OPEN:         return("IDLE_OPEN");
189         case LOPEN:             return("LOPEN");
190         case MOPEN:             return("MOPEN");
191         case MPEND:             return("MPEND");
192         case CONFIG:            return("CONFIG");
193         case CLOSE:             return("CLOSE");
194         case SBREAK:            return("SBREAK");
195         case EBREAK:            return("EBREAK");
196         case IDLE_CLOSE:        return("IDLE_CLOSE");
197         case IDLE_BREAK:        return("IDLE_BREAK");
198         case FCLOSE:            return("FCLOSE");
199         case RESUME:            return("RESUME");
200         case WFLUSH:            return("WFLUSH");
201         case RFLUSH:            return("RFLUSH");
202         default:
203                 sprintf(buf, "?cmd:0x%x?", cmd);
204                 return (buf);
205         }
206 }
207 #endif
208
209 /*
210  * We have to make an 8 bit version of bcopy, since some cards can't
211  * deal with 32 bit I/O
212  */
213 static void __inline
214 si_bcopy(const void *src, void *dst, size_t len)
215 {
216         u_char *d;
217         const u_char *s;
218
219         d = dst;
220         s = src;
221         while (len--)
222                 *d++ = *s++;
223 }
224 static void __inline
225 si_vbcopy(const volatile void *src, void *dst, size_t len)
226 {
227         u_char *d;
228         const volatile u_char *s;
229
230         d = dst;
231         s = src;
232         while (len--)
233                 *d++ = *s++;
234 }
235 static void __inline
236 si_bcopyv(const void *src, volatile void *dst, size_t len)
237 {
238         volatile u_char *d;
239         const u_char *s;
240
241         d = dst;
242         s = src;
243         while (len--)
244                 *d++ = *s++;
245 }
246
247 static int
248 si_speedtab(int speed, struct si_speedtab *table)
249 {
250         for ( ; table->sp_speed != -1; table++)
251                 if (table->sp_speed == speed)
252                         return (table->sp_code);
253         return (-1);
254 }
255
256
257 static struct ttydevsw si_tty_class = {
258         .tsw_flags      = TF_INITLOCK|TF_CALLOUT,
259         .tsw_open       = siopen,
260         .tsw_close      = siclose,
261         .tsw_outwakeup  = si_start,
262         /* .tsw_stop = si_stop */
263         .tsw_ioctl      = siioctl,
264         .tsw_param      = siparam,
265         .tsw_modem      = simodem,
266 };
267
268
269 /*
270  * Attach the device.  Initialize the card.
271  */
272 int
273 siattach(device_t dev)
274 {
275         int unit;
276         struct si_softc *sc;
277         struct si_port *pp;
278         struct tty *tp;
279         volatile struct si_channel *ccbp;
280         volatile struct si_reg *regp;
281         volatile caddr_t maddr;
282         struct si_module *modp;
283         int nmodule, nport, x, y;
284         int uart_type;
285
286         sc = device_get_softc(dev);
287         unit = device_get_unit(dev);
288
289         sc->sc_typename = si_type[sc->sc_type];
290         if (si_numunits < unit + 1)
291                 si_numunits = unit + 1;
292
293         DPRINT((0, DBG_AUTOBOOT, "si%d: siattach\n", unit));
294
295 #ifdef POLL
296         if (si_pollrate == 0) {
297                 si_pollrate = POLLHZ;           /* in addition to irq */
298 #ifdef REALPOLL
299                 si_realpoll = 1;                /* scan always */
300 #endif
301         }
302 #endif
303
304         DPRINT((0, DBG_AUTOBOOT, "si%d: type: %s paddr: %x maddr: %x\n", unit,
305                 sc->sc_typename, sc->sc_paddr, sc->sc_maddr));
306
307         sc->sc_ports = NULL;                    /* mark as uninitialised */
308
309         maddr = sc->sc_maddr;
310
311         /* Stop the CPU first so it won't stomp around while we load */
312
313         switch (sc->sc_type) {
314 #ifdef DEV_EISA
315                 case SIEISA:
316                         outb(sc->sc_iobase + 2, sc->sc_irq << 4);
317 #endif
318                 break;
319                 case SIPCI:
320                         *(maddr+SIPCIRESET) = 0;
321                 break;
322                 case SIJETPCI: /* fall through to JET ISA */
323                 case SIJETISA:
324                         *(maddr+SIJETCONFIG) = 0;
325                 break;
326                 case SIHOST2:
327                         *(maddr+SIPLRESET) = 0;
328                 break;
329                 case SIHOST:
330                         *(maddr+SIRESET) = 0;
331                 break;
332                 default: /* this should never happen */
333                         printf("si%d: unsupported configuration\n", unit);
334                         return EINVAL;
335                 break;
336         }
337
338         /* OK, now lets download the download code */
339
340         if (SI_ISJET(sc->sc_type)) {
341                 DPRINT((0, DBG_DOWNLOAD, "si%d: jet_download: nbytes %d\n",
342                         unit, si3_t225_dsize));
343                 si_bcopy(si3_t225_download, maddr + si3_t225_downloadaddr,
344                         si3_t225_dsize);
345                 DPRINT((0, DBG_DOWNLOAD,
346                         "si%d: jet_bootstrap: nbytes %d -> %x\n",
347                         unit, si3_t225_bsize, si3_t225_bootloadaddr));
348                 si_bcopy(si3_t225_bootstrap, maddr + si3_t225_bootloadaddr,
349                         si3_t225_bsize);
350         } else {
351                 DPRINT((0, DBG_DOWNLOAD, "si%d: si_download: nbytes %d\n",
352                         unit, si2_z280_dsize));
353                 si_bcopy(si2_z280_download, maddr + si2_z280_downloadaddr,
354                         si2_z280_dsize);
355         }
356
357         /* Now start the CPU */
358
359         switch (sc->sc_type) {
360 #ifdef DEV_EISA
361         case SIEISA:
362                 /* modify the download code to tell it that it's on an EISA */
363                 *(maddr + 0x42) = 1;
364                 outb(sc->sc_iobase + 2, (sc->sc_irq << 4) | 4);
365                 (void)inb(sc->sc_iobase + 3); /* reset interrupt */
366                 break;
367 #endif
368         case SIPCI:
369                 /* modify the download code to tell it that it's on a PCI */
370                 *(maddr+0x42) = 1;
371                 *(maddr+SIPCIRESET) = 1;
372                 *(maddr+SIPCIINTCL) = 0;
373                 break;
374         case SIJETPCI:
375                 *(maddr+SIJETRESET) = 0;
376                 *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN;
377                 break;
378         case SIJETISA:
379                 *(maddr+SIJETRESET) = 0;
380                 switch (sc->sc_irq) {
381                 case 9:
382                         *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0x90;
383                         break;
384                 case 10:
385                         *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xa0;
386                         break;
387                 case 11:
388                         *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xb0;
389                         break;
390                 case 12:
391                         *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xc0;
392                         break;
393                 case 15:
394                         *(maddr+SIJETCONFIG) = SIJETBUSEN|SIJETIRQEN|0xf0;
395                         break;
396                 }
397                 break;
398         case SIHOST:
399                 *(maddr+SIRESET_CL) = 0;
400                 *(maddr+SIINTCL_CL) = 0;
401                 break;
402         case SIHOST2:
403                 *(maddr+SIPLRESET) = 0x10;
404                 switch (sc->sc_irq) {
405                 case 11:
406                         *(maddr+SIPLIRQ11) = 0x10;
407                         break;
408                 case 12:
409                         *(maddr+SIPLIRQ12) = 0x10;
410                         break;
411                 case 15:
412                         *(maddr+SIPLIRQ15) = 0x10;
413                         break;
414                 }
415                 *(maddr+SIPLIRQCLR) = 0x10;
416                 break;
417         default: /* this should _REALLY_ never happen */
418                 printf("si%d: Uh, it was supported a second ago...\n", unit);
419                 return EINVAL;
420         }
421
422         DELAY(1000000);                 /* wait around for a second */
423
424         regp = (struct si_reg *)maddr;
425         y = 0;
426                                         /* wait max of 5 sec for init OK */
427         while (regp->initstat == 0 && y++ < 10) {
428                 DELAY(500000);
429         }
430         switch (regp->initstat) {
431         case 0:
432                 printf("si%d: startup timeout - aborting\n", unit);
433                 sc->sc_type = SIEMPTY;
434                 return EINVAL;
435         case 1:
436                 if (SI_ISJET(sc->sc_type)) {
437                         /* set throttle to 100 times per second */
438                         regp->int_count = JET_INT_COUNT;
439                         /* rx_intr_count is a NOP in Jet */
440                 } else {
441                         /* set throttle to 125 times per second */
442                         regp->int_count = INT_COUNT;
443                         /* rx intr max of 25 times per second */
444                         regp->rx_int_count = RXINT_COUNT;
445                 }
446                 regp->int_pending = 0;          /* no intr pending */
447                 regp->int_scounter = 0; /* reset counter */
448                 break;
449         case 0xff:
450                 /*
451                  * No modules found, so give up on this one.
452                  */
453                 printf("si%d: %s - no ports found\n", unit,
454                         si_type[sc->sc_type]);
455                 return 0;
456         default:
457                 printf("si%d: download code version error - initstat %x\n",
458                         unit, regp->initstat);
459                 return EINVAL;
460         }
461
462         /*
463          * First time around the ports just count them in order
464          * to allocate some memory.
465          */
466         nport = 0;
467         modp = (struct si_module *)(maddr + 0x80);
468         for (;;) {
469                 DPRINT((0, DBG_DOWNLOAD, "si%d: ccb addr 0x%x\n", unit, modp));
470                 switch (modp->sm_type) {
471                 case TA4:
472                         DPRINT((0, DBG_DOWNLOAD,
473                                 "si%d: Found old TA4 module, 4 ports\n",
474                                 unit));
475                         x = 4;
476                         break;
477                 case TA8:
478                         DPRINT((0, DBG_DOWNLOAD,
479                                 "si%d: Found old TA8 module, 8 ports\n",
480                                 unit));
481                         x = 8;
482                         break;
483                 case TA4_ASIC:
484                         DPRINT((0, DBG_DOWNLOAD,
485                                 "si%d: Found ASIC TA4 module, 4 ports\n",
486                                 unit));
487                         x = 4;
488                         break;
489                 case TA8_ASIC:
490                         DPRINT((0, DBG_DOWNLOAD,
491                                 "si%d: Found ASIC TA8 module, 8 ports\n",
492                                 unit));
493                         x = 8;
494                         break;
495                 case MTA:
496                         DPRINT((0, DBG_DOWNLOAD,
497                                 "si%d: Found CD1400 module, 8 ports\n",
498                                 unit));
499                         x = 8;
500                         break;
501                 case SXDC:
502                         DPRINT((0, DBG_DOWNLOAD,
503                                 "si%d: Found SXDC module, 8 ports\n",
504                                 unit));
505                         x = 8;
506                         break;
507                 default:
508                         printf("si%d: unknown module type %d\n",
509                                 unit, modp->sm_type);
510                         goto try_next;
511                 }
512
513                 /* this was limited in firmware and is also a driver issue */
514                 if ((nport + x) > SI_MAXPORTPERCARD) {
515                         printf("si%d: extra ports ignored\n", unit);
516                         goto try_next;
517                 }
518
519                 nport += x;
520                 si_Nports += x;
521                 si_Nmodules++;
522
523 try_next:
524                 if (modp->sm_next == 0)
525                         break;
526                 modp = (struct si_module *)
527                         (maddr + (unsigned)(modp->sm_next & 0x7fff));
528         }
529         sc->sc_ports = (struct si_port *)malloc(sizeof(struct si_port) * nport,
530                 M_DEVBUF, M_NOWAIT | M_ZERO);
531         if (sc->sc_ports == 0) {
532                 printf("si%d: fail to malloc memory for port structs\n",
533                         unit);
534                 return EINVAL;
535         }
536         sc->sc_nport = nport;
537
538         /*
539          * Scan round the ports again, this time initialising.
540          */
541         pp = sc->sc_ports;
542         nmodule = 0;
543         modp = (struct si_module *)(maddr + 0x80);
544         uart_type = 1000;       /* arbitary, > uchar_max */
545         for (;;) {
546                 switch (modp->sm_type) {
547                 case TA4:
548                         nport = 4;
549                         break;
550                 case TA8:
551                         nport = 8;
552                         break;
553                 case TA4_ASIC:
554                         nport = 4;
555                         break;
556                 case TA8_ASIC:
557                         nport = 8;
558                         break;
559                 case MTA:
560                         nport = 8;
561                         break;
562                 case SXDC:
563                         nport = 8;
564                         break;
565                 default:
566                         goto try_next2;
567                 }
568                 nmodule++;
569                 ccbp = (struct si_channel *)((char *)modp + 0x100);
570                 if (uart_type == 1000)
571                         uart_type = ccbp->type;
572                 else if (uart_type != ccbp->type)
573                         printf("si%d: Warning: module %d mismatch! (%d%s != %d%s)\n",
574                             unit, nmodule,
575                             ccbp->type, si_modulename(sc->sc_type, ccbp->type),
576                             uart_type, si_modulename(sc->sc_type, uart_type));
577
578                 for (x = 0; x < nport; x++, pp++, ccbp++) {
579                         pp->sp_ccb = ccbp;      /* save the address */
580                         pp->sp_pend = IDLE_CLOSE;
581                         pp->sp_state = 0;       /* internal flag */
582 #ifdef SI_DEBUG
583                         sprintf(pp->sp_name, "si%r%r", unit,
584                             (int)(pp - sc->sc_ports));
585 #endif
586                         tp = pp->sp_tty = tty_alloc_mutex(&si_tty_class, pp, &Giant);
587                         tty_makedev(tp, NULL, "A%r%r", unit, (int)(pp - sc->sc_ports));
588                 }
589 try_next2:
590                 if (modp->sm_next == 0) {
591                         printf("si%d: card: %s, ports: %d, modules: %d, type: %d%s\n",
592                                 unit,
593                                 sc->sc_typename,
594                                 sc->sc_nport,
595                                 nmodule,
596                                 uart_type,
597                                 si_modulename(sc->sc_type, uart_type));
598                         break;
599                 }
600                 modp = (struct si_module *)
601                         (maddr + (unsigned)(modp->sm_next & 0x7fff));
602         }
603
604         if (unit == 0)
605                 make_dev(&si_Scdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
606                     "si_control");
607         return (0);
608 }
609
610 static  int
611 siopen(struct tty *tp)
612 {
613
614         DPRINT((0, DBG_ENTRY|DBG_OPEN, "siopen()\n"));
615         mtx_assert(&Giant, MA_OWNED);
616 #ifdef  POLL
617         /*
618          * We've now got a device, so start the poller.
619          */
620         if (init_finished == 0) {
621                 timeout(si_poll, (caddr_t)0L, si_pollrate);
622                 init_finished = 1;
623         }
624 #endif
625         DPRINT((0, DBG_EXIT|DBG_OPEN, "siopen() finished\n"));
626         return(0);
627 }
628
629 static void
630 siclose(struct tty *tp)
631 {
632         struct si_port *pp;
633
634         DPRINT((0, DBG_ENTRY|DBG_CLOSE, "siclose()\n"));
635         mtx_assert(&Giant, MA_OWNED);
636         pp = tty_softc(tp);
637         (void) si_command(pp, FCLOSE, SI_WAIT);
638         DPRINT((0, DBG_EXIT|DBG_CLOSE, "siclose() finished\n"));
639 }
640
641 static int
642 siioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
643 {
644         struct si_port *pp;
645
646         DPRINT((0, DBG_ENTRY|DBG_IOCTL, "siioctl(0x%lx,0x%x)\n", cmd, data));
647         mtx_assert(&Giant, MA_OWNED);
648         pp = tty_softc(tp);
649         switch (cmd) {
650         case TIOCSBRK:
651                 si_command(pp, SBREAK, SI_WAIT);
652                 return (0);
653         case TIOCCBRK:
654                 si_command(pp, EBREAK, SI_WAIT);
655                 return (0);
656         }
657         return (ENOIOCTL);      /* Let the common tty ioctl handler do it */
658 }
659
660 /*
661  * Handle the Specialix ioctls on the control dev.
662  */
663 static int
664 si_Sioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
665 {
666         struct si_softc *xsc;
667         struct si_port *xpp;
668         volatile struct si_reg *regp;
669         struct si_tcsi *dp;
670         struct si_pstat *sps;
671         int *ip, error = 0;
672         int oldspl;
673         int card, port;
674
675         DPRINT((0, DBG_ENTRY|DBG_IOCTL, "si_Sioctl(%s,0x%lx,0x%x)\n",
676                 devtoname(dev), cmd, data));
677         mtx_assert(&Giant, MA_OWNED);
678
679 #if 1
680         DPRINT((0, DBG_IOCTL, "TCSI_PORT=%x\n", TCSI_PORT));
681         DPRINT((0, DBG_IOCTL, "TCSI_CCB=%x\n", TCSI_CCB));
682 #endif
683
684         oldspl = spltty();      /* better safe than sorry */
685
686         ip = (int *)data;
687
688 #define SUCHECK if ((error = priv_check(td, PRIV_DRIVER))) goto out
689
690         switch (cmd) {
691         case TCSIPORTS:
692                 *ip = si_Nports;
693                 goto out;
694         case TCSIMODULES:
695                 *ip = si_Nmodules;
696                 goto out;
697         case TCSISDBG_ALL:
698                 SUCHECK;
699                 si_debug = *ip;
700                 goto out;
701         case TCSIGDBG_ALL:
702                 *ip = si_debug;
703                 goto out;
704         default:
705                 /*
706                  * Check that a controller for this port exists
707                  */
708
709                 /* may also be a struct si_pstat, a superset of si_tcsi */
710
711                 dp = (struct si_tcsi *)data;
712                 sps = (struct si_pstat *)data;
713                 card = dp->tc_card;
714                 xsc = devclass_get_softc(si_devclass, card);    /* check.. */
715                 if (xsc == NULL || xsc->sc_type == SIEMPTY) {
716                         error = ENOENT;
717                         goto out;
718                 }
719                 /*
720                  * And check that a port exists
721                  */
722                 port = dp->tc_port;
723                 if (port < 0 || port >= xsc->sc_nport) {
724                         error = ENOENT;
725                         goto out;
726                 }
727                 xpp = xsc->sc_ports + port;
728                 regp = (struct si_reg *)xsc->sc_maddr;
729         }
730
731         switch (cmd) {
732         case TCSIDEBUG:
733 #ifdef  SI_DEBUG
734                 SUCHECK;
735                 if (xpp->sp_debug)
736                         xpp->sp_debug = 0;
737                 else {
738                         xpp->sp_debug = DBG_ALL;
739                         DPRINT((xpp, DBG_IOCTL, "debug toggled %s\n",
740                                 (xpp->sp_debug&DBG_ALL)?"ON":"OFF"));
741                 }
742                 break;
743 #else
744                 error = ENODEV;
745                 goto out;
746 #endif
747         case TCSISDBG_LEVEL:
748         case TCSIGDBG_LEVEL:
749 #ifdef  SI_DEBUG
750                 if (cmd == TCSIGDBG_LEVEL) {
751                         dp->tc_dbglvl = xpp->sp_debug;
752                 } else {
753                         SUCHECK;
754                         xpp->sp_debug = dp->tc_dbglvl;
755                 }
756                 break;
757 #else
758                 error = ENODEV;
759                 goto out;
760 #endif
761         case TCSIGRXIT:
762                 dp->tc_int = regp->rx_int_count;
763                 break;
764         case TCSIRXIT:
765                 SUCHECK;
766                 regp->rx_int_count = dp->tc_int;
767                 break;
768         case TCSIGIT:
769                 dp->tc_int = regp->int_count;
770                 break;
771         case TCSIIT:
772                 SUCHECK;
773                 regp->int_count = dp->tc_int;
774                 break;
775         case TCSISTATE:
776                 dp->tc_int = xpp->sp_ccb->hi_ip;
777                 break;
778         /* these next three use a different structure */
779         case TCSI_PORT:
780                 SUCHECK;
781                 si_bcopy(xpp, &sps->tc_siport, sizeof(sps->tc_siport));
782                 break;
783         case TCSI_CCB:
784                 SUCHECK;
785                 si_vbcopy(xpp->sp_ccb, &sps->tc_ccb, sizeof(sps->tc_ccb));
786                 break;
787         default:
788                 error = EINVAL;
789                 goto out;
790         }
791 out:
792         splx(oldspl);
793         return(error);          /* success */
794 }
795
796 /*
797  *      siparam()       : Configure line params
798  *      called at spltty();
799  *      this may sleep, does not flush, nor wait for drain, nor block writes
800  *      caller must arrange this if it's important..
801  */
802 static int
803 siparam(struct tty *tp, struct termios *t)
804 {
805         struct si_port *pp = tty_softc(tp);
806         volatile struct si_channel *ccbp;
807         int oldspl, cflag, iflag, oflag, lflag;
808         int error = 0;          /* shutup gcc */
809         int ispeed = 0;         /* shutup gcc */
810         int ospeed = 0;         /* shutup gcc */
811         BYTE val;
812
813         DPRINT((pp, DBG_ENTRY|DBG_PARAM, "siparam(%x,%x)\n", tp, t));
814         mtx_assert(&Giant, MA_OWNED);
815         cflag = t->c_cflag;
816         iflag = t->c_iflag;
817         oflag = t->c_oflag;
818         lflag = t->c_lflag;
819         DPRINT((pp, DBG_PARAM, "OFLAG 0x%x CFLAG 0x%x IFLAG 0x%x LFLAG 0x%x\n",
820                 oflag, cflag, iflag, lflag));
821
822         /* XXX - if Jet host and SXDC module, use extended baud rates */
823
824         /* if not hung up.. */
825         if (t->c_ospeed != 0) {
826                 /* translate baud rate to firmware values */
827                 ospeed = si_speedtab(t->c_ospeed, bdrates);
828                 ispeed = t->c_ispeed ?
829                          si_speedtab(t->c_ispeed, bdrates) : ospeed;
830                 /* enforce legit baud rate */
831                 if (ospeed < 0 || ispeed < 0)
832                         return (EINVAL);
833         }
834
835         oldspl = spltty();
836
837         ccbp = pp->sp_ccb;
838
839         /* ========== set hi_break ========== */
840         val = 0;
841         if (iflag & IGNBRK)             /* Breaks */
842                 val |= BR_IGN;
843         if (iflag & BRKINT)             /* Interrupt on break? */
844                 val |= BR_INT;
845         if (iflag & PARMRK)             /* Parity mark? */
846                 val |= BR_PARMRK;
847         if (iflag & IGNPAR)             /* Ignore chars with parity errors? */
848                 val |= BR_PARIGN;
849         ccbp->hi_break = val;
850
851         /* ========== set hi_csr ========== */
852         /* if not hung up.. */
853         if (t->c_ospeed != 0) {
854                 /* Set I/O speeds */
855                  val = (ispeed << 4) | ospeed;
856         }
857         ccbp->hi_csr = val;
858
859         /* ========== set hi_mr2 ========== */
860         val = 0;
861         if (cflag & CSTOPB)                             /* Stop bits */ 
862                 val |= MR2_2_STOP;
863         else
864                 val |= MR2_1_STOP;
865
866         /*
867          * Enable H/W RTS/CTS handshaking. The default TA/MTA is
868          * a DCE, hence the reverse sense of RTS and CTS
869          */
870         /* Output Flow - RTS must be raised before data can be sent */
871         if (cflag & CCTS_OFLOW)
872                 val |= MR2_RTSCONT;
873
874         ccbp->hi_mr2 = val;
875
876         /* ========== set hi_mr1 ========== */
877         val = 0;
878         if (!(cflag & PARENB))                          /* Parity */
879                 val |= MR1_NONE;
880         else
881                 val |= MR1_WITH;
882         if (cflag & PARODD)
883                 val |= MR1_ODD;
884
885         if ((cflag & CS8) == CS8)                       /* 8 data bits? */
886                 val |= MR1_8_BITS;
887         else if ((cflag & CS7) == CS7)                  /* 7 data bits? */
888                 val |= MR1_7_BITS;
889         else if ((cflag & CS6) == CS6)                  /* 6 data bits? */
890                 val |= MR1_6_BITS;
891         else                                            /* Must be 5 */
892                 val |= MR1_5_BITS;
893
894         /*
895          * Enable H/W RTS/CTS handshaking. The default TA/MTA is
896          * a DCE, hence the reverse sense of RTS and CTS
897          */
898         /* Input Flow - CTS is raised when port is ready to receive data */
899         if (cflag & CRTS_IFLOW)
900                 val |= MR1_CTSCONT;
901
902         ccbp->hi_mr1 = val;
903
904         /* ========== set hi_mask ========== */
905         val = 0xff;
906         if ((cflag & CS8) == CS8) {                     /* 8 data bits? */
907                 val &= 0xFF;
908         } else if ((cflag & CS7) == CS7) {              /* 7 data bits? */
909                 val &= 0x7F;
910         } else if ((cflag & CS6) == CS6) {              /* 6 data bits? */
911                 val &= 0x3F;
912         } else {                                        /* Must be 5 */
913                 val &= 0x1F;
914         }
915         if (iflag & ISTRIP)
916                 val &= 0x7F;
917
918         ccbp->hi_mask = val;
919
920         /* ========== set hi_prtcl ========== */
921         val = SP_DCEN;          /* Monitor DCD always, or TIOCMGET misses it */
922         if (iflag & IXANY)
923                 val |= SP_TANY;
924         if (iflag & IXON)
925                 val |= SP_TXEN;
926         if (iflag & IXOFF)
927                 val |= SP_RXEN;
928         if (iflag & INPCK)
929                 val |= SP_PAEN;
930
931         ccbp->hi_prtcl = val;
932
933
934         /* ========== set hi_{rx|tx}{on|off} ========== */
935         /* XXX: the card TOTALLY shields us from the flow control... */
936         ccbp->hi_txon = t->c_cc[VSTART];
937         ccbp->hi_txoff = t->c_cc[VSTOP];
938
939         ccbp->hi_rxon = t->c_cc[VSTART];
940         ccbp->hi_rxoff = t->c_cc[VSTOP];
941
942         /* ========== send settings to the card ========== */
943         /* potential sleep here */
944         if (ccbp->hi_stat == IDLE_CLOSE)                /* Not yet open */
945                 si_command(pp, LOPEN, SI_WAIT);         /* open it */
946         else
947                 si_command(pp, CONFIG, SI_WAIT);        /* change params */
948
949         /* ========== set DTR etc ========== */
950         /* Hangup if ospeed == 0 */
951         if (t->c_ospeed == 0) {
952                 (void) simodem(tp, 0, SER_DTR | SER_RTS);
953         } else {
954                 /*
955                  * If the previous speed was 0, may need to re-enable
956                  * the modem signals
957                  */
958                 (void) simodem(tp, SER_DTR | SER_RTS, 0);
959         }
960
961         DPRINT((pp, DBG_PARAM, "siparam, complete: MR1 %x MR2 %x HI_MASK %x PRTCL %x HI_BREAK %x HI_CSR %x\n",
962                 ccbp->hi_mr1, ccbp->hi_mr2, ccbp->hi_mask, ccbp->hi_prtcl, ccbp->hi_break, ccbp->hi_csr));
963
964         splx(oldspl);
965         return(error);
966 }
967
968 /*
969  * Set/Get state of modem control lines.
970  * Due to DCE-like behaviour of the adapter, some signals need translation:
971  *      TIOCM_DTR       DSR
972  *      TIOCM_RTS       CTS
973  */
974 static int
975 simodem(struct tty *tp, int sigon, int sigoff)
976 {
977         struct si_port *pp;
978         volatile struct si_channel *ccbp;
979         int x;
980
981         pp = tty_softc(tp);
982         DPRINT((pp, DBG_ENTRY|DBG_MODEM, "simodem(%x,%x)\n", sigon, sigoff));
983         mtx_assert(&Giant, MA_OWNED);
984         ccbp = pp->sp_ccb;              /* Find channel address */
985         if (sigon == 0 && sigoff == 0) {
986                 x = ccbp->hi_ip;
987                 /*
988                  * XXX: not sure this is correct, should it be CTS&DSR ?
989                  * XXX: or do we (just) miss CTS & DSR ?
990                  */
991                 if (x & IP_DCD)         sigon |= SER_DCD;
992                 if (x & IP_DTR)         sigon |= SER_DTR;
993                 if (x & IP_RTS)         sigon |= SER_RTS;
994                 if (x & IP_RI)          sigon |= SER_RI;
995                 return (sigon);
996         }
997
998         x = ccbp->hi_op;
999         if (sigon & SER_DTR)
1000                 x |= OP_DSR;
1001         if (sigoff & SER_DTR)
1002                 x &= ~OP_DSR;
1003         if (sigon & SER_RTS)
1004                 x |= OP_CTS;
1005         if (sigoff & SER_RTS)
1006                 x &= ~OP_CTS;
1007         ccbp->hi_op = x;
1008         return 0;
1009 }
1010
1011 /*
1012  * Handle change of modem state
1013  */
1014 static void
1015 si_modem_state(struct si_port *pp, struct tty *tp, int hi_ip)
1016 {
1017                                                         /* if a modem dev */
1018         mtx_assert(&Giant, MA_OWNED);
1019         if (hi_ip & IP_DCD) {
1020                 if (!(pp->sp_last_hi_ip & IP_DCD)) {
1021                         DPRINT((pp, DBG_INTR, "modem carr on%d\n"));
1022                         (void)ttydisc_modem(tp, 1);
1023                 }
1024         } else {
1025                 if (pp->sp_last_hi_ip & IP_DCD) {
1026                         DPRINT((pp, DBG_INTR, "modem carr off\n"));
1027 #if 0   /* XXX mpsafetty ttyld_modem used to tell us to shutdown the port or not */
1028                         if (ttydisc_modem(tp, 0))
1029                                 (void) simodem(tp, 0, SER_DTR | SER_RTS);
1030 #else
1031                         ttydisc_modem(tp, 0);
1032 #endif
1033                 }
1034         }
1035         pp->sp_last_hi_ip = hi_ip;
1036
1037 }
1038
1039 /*
1040  * Poller to catch missed interrupts.
1041  *
1042  * Note that the SYSV Specialix drivers poll at 100 times per second to get
1043  * better response.  We could really use a "periodic" version timeout(). :-)
1044  */
1045 #ifdef POLL
1046 static void
1047 si_poll(void *nothing)
1048 {
1049         struct si_softc *sc;
1050         int i;
1051         volatile struct si_reg *regp;
1052         struct si_port *pp;
1053         int lost, oldspl, port;
1054
1055         DPRINT((0, DBG_POLL, "si_poll()\n"));
1056         oldspl = spltty();
1057         mtx_assert(&Giant, MA_OWNED);
1058         lost = 0;
1059         for (i = 0; i < si_numunits; i++) {
1060                 sc = devclass_get_softc(si_devclass, i);
1061                 if (sc == NULL || sc->sc_type == SIEMPTY)
1062                         continue;
1063                 regp = (struct si_reg *)sc->sc_maddr;
1064
1065                 /*
1066                  * See if there has been a pending interrupt for 2 seconds
1067                  * or so. The test (int_scounter >= 200) won't correspond
1068                  * to 2 seconds if int_count gets changed.
1069                  */
1070                 if (regp->int_pending != 0) {
1071                         if (regp->int_scounter >= 200 &&
1072                             regp->initstat == 1) {
1073                                 printf("si%d: lost intr\n", i);
1074                                 lost++;
1075                         }
1076                 } else {
1077                         regp->int_scounter = 0;
1078                 }
1079
1080                 /*
1081                  * gripe about no input flow control..
1082                  */
1083                 pp = sc->sc_ports;
1084                 for (port = 0; port < sc->sc_nport; pp++, port++) {
1085                         if (pp->sp_delta_overflows > 0) {
1086                                 printf("si%d: %d tty level buffer overflows\n",
1087                                         i, pp->sp_delta_overflows);
1088                                 pp->sp_delta_overflows = 0;
1089                         }
1090                 }
1091         }
1092         if (lost || si_realpoll)
1093                 si_intr(NULL);  /* call intr with fake vector */
1094         splx(oldspl);
1095
1096         timeout(si_poll, (caddr_t)0L, si_pollrate);
1097 }
1098 #endif  /* ifdef POLL */
1099
1100 /*
1101  * The interrupt handler polls ALL ports on ALL adapters each time
1102  * it is called.
1103  */
1104
1105 static BYTE si_rxbuf[SI_BUFFERSIZE];    /* input staging area */
1106 static BYTE si_txbuf[SI_BUFFERSIZE];    /* output staging area */
1107
1108 void
1109 si_intr(void *arg)
1110 {
1111         struct si_softc *sc;
1112         struct si_port *pp;
1113         volatile struct si_channel *ccbp;
1114         struct tty *tp;
1115         volatile caddr_t maddr;
1116         BYTE op, ip;
1117         int x, card, port, n, i, isopen;
1118         volatile BYTE *z;
1119         BYTE c;
1120
1121         sc = arg;
1122         mtx_assert(&Giant, MA_OWNED);
1123
1124         DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "si_intr\n"));
1125
1126         /*
1127          * When we get an int we poll all the channels and do ALL pending
1128          * work, not just the first one we find. This allows all cards to
1129          * share the same vector.
1130          *
1131          * XXX - But if we're sharing the vector with something that's NOT
1132          * a SI/XIO/SX card, we may be making more work for ourselves.
1133          */
1134         for (card = 0; card < si_numunits; card++) {
1135                 sc = devclass_get_softc(si_devclass, card);
1136                 if (sc == NULL || sc->sc_type == SIEMPTY)
1137                         continue;
1138
1139                 /*
1140                  * First, clear the interrupt
1141                  */
1142                 switch(sc->sc_type) {
1143                 case SIHOST:
1144                         maddr = sc->sc_maddr;
1145                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1146                                                         /* flag nothing pending */
1147                         *(maddr+SIINTCL) = 0x00;        /* Set IRQ clear */
1148                         *(maddr+SIINTCL_CL) = 0x00;     /* Clear IRQ clear */
1149                         break;
1150                 case SIHOST2:
1151                         maddr = sc->sc_maddr;
1152                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1153                         *(maddr+SIPLIRQCLR) = 0x00;
1154                         *(maddr+SIPLIRQCLR) = 0x10;
1155                         break;
1156                 case SIPCI:
1157                         maddr = sc->sc_maddr;
1158                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1159                         *(maddr+SIPCIINTCL) = 0x0;
1160                         break;
1161                 case SIJETPCI:  /* fall through to JETISA case */
1162                 case SIJETISA:
1163                         maddr = sc->sc_maddr;
1164                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1165                         *(maddr+SIJETINTCL) = 0x0;
1166                         break;
1167 #ifdef DEV_EISA
1168                 case SIEISA:
1169                         maddr = sc->sc_maddr;
1170                         ((volatile struct si_reg *)maddr)->int_pending = 0;
1171                         (void)inb(sc->sc_iobase + 3);
1172                         break;
1173 #endif
1174                 case SIEMPTY:
1175                 default:
1176                         continue;
1177                 }
1178                 ((volatile struct si_reg *)maddr)->int_scounter = 0;
1179
1180                 /*
1181                  * check each port
1182                  */
1183                 for (pp = sc->sc_ports, port = 0; port < sc->sc_nport;
1184                      pp++, port++) {
1185                         ccbp = pp->sp_ccb;
1186                         tp = pp->sp_tty;
1187                         tty_lock(tp);
1188
1189                         /*
1190                          * See if a command has completed ?
1191                          */
1192                         if (ccbp->hi_stat != pp->sp_pend) {
1193                                 DPRINT((pp, DBG_INTR,
1194                                         "si_intr hi_stat = %s, pend = %s\n",
1195                                         si_cmdname(ccbp->hi_stat),
1196                                         si_cmdname(pp->sp_pend)));
1197                                 switch(pp->sp_pend) {
1198                                 case LOPEN:
1199                                 case MPEND:
1200                                 case MOPEN:
1201                                 case FCLOSE:
1202                                 case CONFIG:
1203                                 case SBREAK:
1204                                 case EBREAK:
1205                                         /* sleeping in si_command */
1206                                         DPRINT((pp, DBG_INTR, "do wakeup\n"));
1207                                         wakeup(&pp->sp_state);
1208                                         break;
1209                                 }
1210                                 pp->sp_pend = ccbp->hi_stat;
1211                         }
1212
1213                         /*
1214                          * Continue on if it's closed
1215                          */
1216                         if (ccbp->hi_stat == IDLE_CLOSE) {
1217                                 tty_unlock(tp);
1218                                 continue;
1219                         }
1220
1221                         /*
1222                          * Do modem state change if not a local device
1223                          */
1224                         si_modem_state(pp, tp, ccbp->hi_ip);
1225
1226                         /*
1227                          * Check to see if we should 'receive' characters.
1228                          */
1229                         isopen = tty_opened(tp);
1230
1231                         /*
1232                          * Do input break processing
1233                          */
1234                         if (ccbp->hi_state & ST_BREAK) {
1235                                 if (isopen)
1236                                         ttydisc_rint(tp, 0, TRE_BREAK);
1237                                 ccbp->hi_state &= ~ST_BREAK;   /* A Bit iffy this */
1238                                 DPRINT((pp, DBG_INTR, "si_intr break\n"));
1239                         }
1240
1241                         /*
1242                          * Do RX stuff - if not open then dump any characters.
1243                          * XXX: This is VERY messy and needs to be cleaned up.
1244                          *
1245                          * XXX: can we leave data in the host adapter buffer
1246                          * when the clists are full?  That may be dangerous
1247                          * if the user cannot get an interrupt signal through.
1248                          */
1249
1250         more_rx:
1251
1252                         if (!isopen) {
1253                                 DPRINT((pp, DBG_INTR, "intr1: not open\n"));
1254                                 ccbp->hi_rxopos = ccbp->hi_rxipos;
1255                                 goto end_rx;
1256                         }
1257
1258 #if 0 /* XXXMPSAFETTY */
1259                         /*
1260                          * If the tty input buffers are blocked, stop emptying
1261                          * the incoming buffers and let the auto flow control
1262                          * assert..
1263                          */
1264                         if (tp->t_state & TS_TBLOCK)
1265                                 goto end_rx;
1266 #endif
1267
1268                         /*
1269                          * Process read characters if not skipped above
1270                          */
1271                         op = ccbp->hi_rxopos;
1272                         ip = ccbp->hi_rxipos;
1273                         c = ip - op;
1274                         if (c == 0)
1275                                 goto end_rx;
1276
1277                         n = c & 0xff;
1278                         if (n > 250)
1279                                 n = 250;
1280
1281                         DPRINT((pp, DBG_INTR, "n = %d, op = %d, ip = %d\n",
1282                                                 n, op, ip));
1283
1284                         /*
1285                          * Suck characters out of host card buffer into the
1286                          * "input staging buffer" - so that we dont leave the
1287                          * host card in limbo while we're possibly echoing
1288                          * characters and possibly flushing input inside the
1289                          * ldisc l_rint() routine.
1290                          */
1291                         if (n <= SI_BUFFERSIZE - op) {
1292
1293                                 z = ccbp->hi_rxbuf + op;
1294                                 si_vbcopy(z, si_rxbuf, n);
1295
1296                                 op += n;
1297                         } else {
1298                                 x = SI_BUFFERSIZE - op;
1299
1300                                 z = ccbp->hi_rxbuf + op;
1301                                 si_vbcopy(z, si_rxbuf, x);
1302
1303                                 z = ccbp->hi_rxbuf;
1304                                 si_vbcopy(z, si_rxbuf + x, n - x);
1305
1306                                 op += n;
1307                         }
1308
1309                         /* clear collected characters from buffer */
1310                         ccbp->hi_rxopos = op;
1311
1312                         /*
1313                          * at this point...
1314                          * n = number of chars placed in si_rxbuf
1315                          */
1316
1317                         if (0 && ttydisc_can_bypass(tp)) {
1318
1319                                 i =  ttydisc_rint_bypass(tp, (char *)si_rxbuf, n);
1320                                 if (i < n)
1321                                         pp->sp_delta_overflows += (n - i);
1322
1323                         } else {
1324                                 /*
1325                                  * It'd be nice to not have to go through the
1326                                  * function call overhead for each char here.
1327                                  * It'd be nice to block input it, saving a
1328                                  * loop here and the call/return overhead.
1329                                  */
1330                                 for(x = 0; x < n; x++) {
1331                                         i = si_rxbuf[x];
1332                                         if (ttydisc_rint(tp, i, 0) == -1)
1333                                                 pp->sp_delta_overflows++;
1334                                 }
1335                         }
1336                         goto more_rx;   /* try for more until RXbuf is empty */
1337
1338         end_rx:
1339
1340                         ttydisc_rint_done(tp);
1341
1342                         /*
1343                          * Do TX stuff
1344                          */
1345                         si_start(tp);
1346                         tty_unlock(tp);
1347
1348                 } /* end of for (all ports on this controller) */
1349         } /* end of for (all controllers) */
1350
1351         DPRINT((0, arg == NULL ? DBG_POLL:DBG_INTR, "end si_intr\n"));
1352 }
1353
1354 /*
1355  * Nudge the transmitter...
1356  *
1357  * XXX: I inherited some funny code here.  It implies the host card only
1358  * interrupts when the transmit buffer reaches the low-water-mark, and does
1359  * not interrupt when it's actually hits empty.  In some cases, we have
1360  * processes waiting for complete drain, and we need to simulate an interrupt
1361  * about when we think the buffer is going to be empty (and retry if not).
1362  * I really am not certain about this...  I *need* the hardware manuals.
1363  */
1364 static void
1365 si_start(struct tty *tp)
1366 {
1367         struct si_port *pp;
1368         volatile struct si_channel *ccbp;
1369         BYTE ipos, count;
1370 #if 0
1371         int nchar;
1372 #endif
1373         int oldspl, n, amount;
1374
1375         oldspl = spltty();
1376         mtx_assert(&Giant, MA_OWNED);
1377
1378         pp = tty_softc(tp);
1379
1380         DPRINT((pp, DBG_ENTRY|DBG_START,
1381                 "si_start(%x) sp_state %x\n",
1382                 tp, pp->sp_state));
1383
1384         ccbp = pp->sp_ccb;
1385
1386         while ((count = (int)ccbp->hi_txipos - (int)ccbp->hi_txopos) < 255) {
1387                 DPRINT((pp, DBG_START, "txbuf pend count %d\n", (BYTE)count));
1388                 ipos = (unsigned int)ccbp->hi_txipos;
1389                 if ((int)ccbp->hi_txopos <= ipos)
1390                         amount = SI_BUFFERSIZE - ipos;
1391                 else
1392                         amount = 255 - count;
1393                 DPRINT((pp, DBG_START, "spaceleft amount %d\n", amount));
1394                 if (amount == 0)
1395                         break;
1396                 n = ttydisc_getc(tp, si_txbuf, amount);
1397                 DPRINT((pp, DBG_START, "getc n=%d\n", n));
1398                 if (n == 0)
1399                         break;
1400                 si_bcopyv(si_txbuf, &ccbp->hi_txbuf[ipos], n);
1401                 ccbp->hi_txipos += n;
1402         }
1403
1404 #if 0
1405         /*
1406          * See if there are any characters still to come.  If so, we can
1407          * depend on si_start being called again.
1408          *
1409          * XXX the manual is vague on this.  It implies we get an interrupt
1410          * when the transmit queue reaches the 25% low water mark, but NOT
1411          * when it hits empty.
1412          */
1413         nchar = ttyoutq_getsize(&tp->t_outq) - ttyoutq_bytesleft(&tp->t_outq);
1414         DPRINT((pp, DBG_START, "count %d, nchar %d\n",
1415                 (BYTE)count, nchar));
1416
1417         if (count != 0 && nchar == 0) {
1418                 int time;
1419
1420                 /* XXX lame. Ticks per character. used to be a table. */
1421                 time = (tp->t_termios.c_ospeed + 9) / 10;
1422
1423                 if (time > 0) {
1424                         if (time < nchar)
1425                                 time = nchar / time;
1426                         else
1427                                 time = 2;
1428                 } else {
1429                         DPRINT((pp, DBG_START,
1430                                 "bad char time value! %d\n", time));
1431                         time = hz/10;
1432                 }
1433
1434                 if ((pp->sp_state & SS_LSTART) != 0)
1435                         untimeout(si_lstart, (caddr_t)pp, pp->lstart_ch);
1436                 DPRINT((pp, DBG_START, "arming lstart, time=%d\n", time));
1437                 pp->sp_state |= SS_LSTART;
1438                 pp->lstart_ch = timeout(si_lstart, (caddr_t)pp, time);
1439         }
1440 #endif
1441
1442         splx(oldspl);
1443         DPRINT((pp, DBG_EXIT|DBG_START, "leave si_start()\n"));
1444 }
1445
1446 #if 0
1447 /*
1448  * This has to deal with two things...  cause wakeups while waiting for
1449  * tty drains on last process exit, and call l_start at about the right
1450  * time for protocols like ppp.
1451  */
1452 static void
1453 si_lstart(void *arg)
1454 {
1455         struct si_port *pp = arg;
1456         struct tty *tp;
1457         int oldspl;
1458
1459         DPRINT((pp, DBG_ENTRY|DBG_LSTART, "si_lstart(%x) sp_state %x\n",
1460                 pp, pp->sp_state));
1461
1462         oldspl = spltty();
1463         mtx_assert(&Giant, MA_OWNED);
1464         pp->sp_state &= ~SS_LSTART;
1465         tp = pp->sp_tty;
1466
1467         si_start(tp);
1468
1469         splx(oldspl);
1470 }
1471 #endif
1472
1473 #if 0 /* XXX mpsafetty */
1474 /*
1475  * Stop output on a line. called at spltty();
1476  */
1477 static void
1478 si_stop(struct tty *tp, int rw)
1479 {
1480         volatile struct si_channel *ccbp;
1481         struct si_port *pp;
1482
1483         mtx_assert(&Giant, MA_OWNED);
1484         pp = tty_softc(tp);
1485         ccbp = pp->sp_ccb;
1486
1487         DPRINT((pp, DBG_ENTRY|DBG_STOP, "si_stop(%x,%x)\n", tp, rw));
1488
1489         /* XXX: must check (rw & FWRITE | FREAD) etc flushing... */
1490         if (rw & FWRITE) {
1491                 /* what level are we meant to be flushing anyway? */
1492                 if (tp->t_state & TS_BUSY) {
1493                         si_command(pp, WFLUSH, SI_NOWAIT);
1494                         tp->t_state &= ~TS_BUSY;
1495                         ttwwakeup(tp);  /* Bruce???? */
1496                 }
1497         }
1498 #if 1   /* XXX: this doesn't work right yet.. */
1499         /* XXX: this may have been failing because we used to call l_rint()
1500          * while we were looping based on these two counters. Now, we collect
1501          * the data and then loop stuffing it into l_rint(), making this
1502          * useless.  Should we cause this to blow away the staging buffer?
1503          */
1504         if (rw & FREAD) {
1505                 ccbp->hi_rxopos = ccbp->hi_rxipos;
1506         }
1507 #endif
1508 }
1509 #endif
1510
1511 /*
1512  * Issue a command to the host card CPU.
1513  *
1514  * XXX This is all just so WRONG!.  Ed says we're not supposed to sleep
1515  * here anyway.  We sort of get away with it for now by using Giant.
1516  * Something better will have to be done.
1517  * Linux does a busy spin here waiting for the 8-bit cpu to notice the
1518  * posted command and respond to it.  I'm not sure I like that either.
1519  */
1520 static void
1521 si_command(struct si_port *pp, int cmd, int waitflag)
1522 {
1523         int oldspl;
1524         volatile struct si_channel *ccbp = pp->sp_ccb;
1525         int x;
1526         int err;
1527
1528         DPRINT((pp, DBG_ENTRY|DBG_PARAM, "si_command(%x,%s,%d): hi_stat %s, sp_pend: %s\n",
1529                 pp, si_cmdname(cmd), waitflag, si_cmdname(ccbp->hi_stat),
1530                 si_cmdname(pp->sp_pend)));
1531
1532         oldspl = spltty();              /* Keep others out */
1533         mtx_assert(&Giant, MA_OWNED);
1534
1535         /* wait until it's finished what it was doing.. */
1536         /* XXX: sits in IDLE_BREAK until something disturbs it or break
1537          * is turned off. */
1538         while((x = ccbp->hi_stat) != IDLE_OPEN &&
1539                         x != IDLE_CLOSE &&
1540                         x != IDLE_BREAK &&
1541                         x != cmd) {
1542                 DPRINT((pp, DBG_PARAM, "sicmd1 old cmd pending (going to tsleep): hi_stat (%s)\n", si_cmdname(ccbp->hi_stat)));
1543                 err = tsleep(&pp->sp_state, (PSOCK+1)|PCATCH, "sicmd1", hz/4);
1544                 if (err) {
1545                         DPRINT((pp, DBG_PARAM, "sicmd1 timeout: hi_stat (%s)\n",
1546                                 si_cmdname(ccbp->hi_stat)));
1547                         /* This is very very bad.  The card has crashed. */
1548                         /* XXX the driver breaks at this point */
1549                         if (err == ETIMEDOUT)
1550                                 DPRINT(("%s: tsleep1 timeout. hi_stat %s, sp_pend %s\n", pp->sp_name, si_cmdname(ccbp->hi_stat), si_cmdname(pp->sp_pend)));
1551                         splx(oldspl);
1552                         return;
1553                 }
1554         }
1555         /* it should now be in IDLE_{OPEN|CLOSE|BREAK}, or "cmd" */
1556         DPRINT((pp, DBG_PARAM, "sicmd1 now in: hi_stat (%s) sp_pend (%s)\n", si_cmdname(ccbp->hi_stat), si_cmdname(pp->sp_pend)));
1557
1558         /* if there was a pending command, cause a state-change wakeup */
1559         switch(pp->sp_pend) {
1560         case LOPEN:
1561         case MPEND:
1562         case MOPEN:
1563         case FCLOSE:
1564         case CONFIG:
1565         case SBREAK:
1566         case EBREAK:
1567                 DPRINT((pp, DBG_PARAM, "si_command: sp_pend %s, doing wakeup\n", si_cmdname(pp->sp_pend)));
1568                 wakeup(&pp->sp_state);
1569                 break;
1570         default:
1571                 break;
1572         }
1573
1574         pp->sp_pend = cmd;              /* New command pending */
1575         ccbp->hi_stat = cmd;            /* Post it */
1576         DPRINT((pp, DBG_PARAM, "sicmd now posted: hi_stat (%s) sp_pend (%s)\n", si_cmdname(ccbp->hi_stat), si_cmdname(pp->sp_pend)));
1577
1578         if (waitflag) {
1579                 while((x = ccbp->hi_stat) != IDLE_OPEN &&
1580                              x != IDLE_CLOSE &&
1581                              x != IDLE_BREAK) {
1582                         DPRINT((pp, DBG_PARAM, "sicmd2 now waiting: hi_stat (%s) sp_pend (%s) (going to tsleep)\n", si_cmdname(ccbp->hi_stat), si_cmdname(pp->sp_pend)));
1583                         err = tsleep(&pp->sp_state, (PSOCK+1)|PCATCH, "sicmd2", hz);
1584                         if (err) {
1585                                 DPRINT((pp, DBG_PARAM, "sicmd2 tsleep error: hi_stat (%s) sp_pend (%s)\n", si_cmdname(ccbp->hi_stat), si_cmdname(pp->sp_pend)));
1586                                 if (err == ETIMEDOUT) {
1587                                         DPRINT(("%s: tsleep2 timeout. hi_stat %s, sp_pend %s\n", pp->sp_name, si_cmdname(ccbp->hi_stat), si_cmdname(pp->sp_pend)));
1588                                 }
1589                                 break;
1590                         }
1591                 }
1592         }
1593         DPRINT((pp, DBG_PARAM, "sicmd2 finished: hi_stat (%s) sp_pend (%s)\n", si_cmdname(ccbp->hi_stat), si_cmdname(pp->sp_pend)));
1594         splx(oldspl);
1595 }
1596
1597
1598 #ifdef  SI_DEBUG
1599
1600 void
1601 si_dprintf(struct si_port *pp, int flags, const char *fmt, ...)
1602 {
1603         va_list ap;
1604
1605         if ((pp == NULL && (si_debug&flags)) ||
1606             (pp != NULL && ((pp->sp_debug&flags) || (si_debug&flags)))) {
1607                 if (pp != NULL)
1608                         printf("%s: ", pp->sp_name);
1609                 va_start(ap, fmt);
1610                 vprintf(fmt, ap);
1611                 va_end(ap);
1612         }
1613 }
1614
1615 #endif  /* DEBUG */
1616
1617 static char *
1618 si_modulename(int host_type, int uart_type)
1619 {
1620         switch (host_type) {
1621         /* Z280 based cards */
1622 #ifdef DEV_EISA
1623         case SIEISA:
1624 #endif
1625         case SIHOST2:
1626         case SIHOST:
1627         case SIPCI:
1628                 switch (uart_type) {
1629                 case 0:
1630                         return(" (XIO)");
1631                 case 1:
1632                         return(" (SI)");
1633                 }
1634                 break;
1635         /* T225 based hosts */
1636         case SIJETPCI:
1637         case SIJETISA:
1638                 switch (uart_type) {
1639                 case 0:
1640                         return(" (SI)");
1641                 case 40:
1642                         return(" (XIO)");
1643                 case 72:
1644                         return(" (SXDC)");
1645                 }
1646                 break;
1647         }
1648         return("");
1649 }