ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / rio / riointr.c
1 /*
2 ** -----------------------------------------------------------------------------
3 **
4 **  Perle Specialix driver for Linux
5 **  Ported from existing RIO Driver for SCO sources.
6  *
7  *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License, or
12  *      (at your option) any later version.
13  *
14  *      This program is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *      GNU General Public License for more details.
18  *
19  *      You should have received a copy of the GNU General Public License
20  *      along with this program; if not, write to the Free Software
21  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 **
23 **      Module          : riointr.c
24 **      SID             : 1.2
25 **      Last Modified   : 11/6/98 10:33:44
26 **      Retrieved       : 11/6/98 10:33:49
27 **
28 **  ident @(#)riointr.c 1.2
29 **
30 ** -----------------------------------------------------------------------------
31 */
32 #ifdef SCCS_LABELS
33 static char *_riointr_c_sccs_ = "@(#)riointr.c  1.2";
34 #endif
35
36
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/errno.h>
40 #include <linux/tty.h>
41 #include <asm/io.h>
42 #include <asm/system.h>
43 #include <asm/string.h>
44 #include <asm/semaphore.h>
45 #include <asm/uaccess.h>
46
47 #include <linux/termios.h>
48 #include <linux/serial.h>
49
50 #include <linux/generic_serial.h>
51
52 #include <linux/delay.h>
53
54 #include "linux_compat.h"
55 #include "rio_linux.h"
56 #include "typdef.h"
57 #include "pkt.h"
58 #include "daemon.h"
59 #include "rio.h"
60 #include "riospace.h"
61 #include "top.h"
62 #include "cmdpkt.h"
63 #include "map.h"
64 #include "riotypes.h"
65 #include "rup.h"
66 #include "port.h"
67 #include "riodrvr.h"
68 #include "rioinfo.h"
69 #include "func.h"
70 #include "errors.h"
71 #include "pci.h"
72
73 #include "parmmap.h"
74 #include "unixrup.h"
75 #include "board.h"
76 #include "host.h"
77 #include "error.h"
78 #include "phb.h"
79 #include "link.h"
80 #include "cmdblk.h"
81 #include "route.h"
82 #include "control.h"
83 #include "cirrus.h"
84 #include "rioioctl.h"
85
86
87
88
89 /*
90 ** riopoll is called every clock tick. Once the /dev/rio device has been
91 ** opened, and polldistributed( ) has been called, this routine is called
92 ** every clock tick *by every cpu*. The 'interesting' piece of code that
93 ** manipulates 'RIONumCpus' and 'RIOCpuCountdown' is used to fair-share
94 ** the work between the CPUs. If there are 'N' cpus, then each poll time
95 ** we increment a counter, modulo 'N-1'. When this counter is 0, we call
96 ** the interrupt handler. This has the effect that polls are serviced
97 ** by processor 'N', 'N-1', 'N-2', ... '0', round and round. Neat.
98 */
99 void
100 riopoll(p)
101 struct rio_info *       p;
102 {
103         int   host;
104
105         /*
106         ** Here's the deal. We try to fair share as much as possible amongst
107         ** all the processors that are available. Since each processor 
108         ** should generate HZ ticks per second and since we only need HZ ticks
109         ** in total for proper operation we simply attempt to cycle round each
110         ** processor in turn, using RIOCpuCountdown to decide whether to call
111         ** the interrupt routine. ( In fact the count zeroes when it reaches
112         ** one less than the total number of processors - so e.g. on a two
113         ** processor system RIOService will get called 2*HZ times per second. )
114         ** this_cpu (cur_cpu()) tells us the number of the current processor
115         ** as follows:
116         **
117         **              0 - default CPU
118         **              1 - first extra CPU
119         **              2 - second extra CPU
120         **              etc.
121         */
122
123         /*
124         ** okay, we've got a cpu that hasn't had a go recently 
125         ** - lets check to see what needs doing.
126         */
127         for ( host=0; host<p->RIONumHosts; host++ ) {
128                 struct Host *HostP = &p->RIOHosts[host];
129
130                 rio_spin_lock( &HostP->HostLock );
131
132                 if ( ( (HostP->Flags & RUN_STATE) != RC_RUNNING ) ||
133                      HostP->InIntr ) {
134                         rio_spin_unlock (&HostP->HostLock); 
135                         continue;
136                 }
137
138                 if ( RWORD( HostP->ParmMapP->rup_intr ) ||
139                          RWORD( HostP->ParmMapP->rx_intr  ) ||
140                          RWORD( HostP->ParmMapP->tx_intr  ) ) {
141                         HostP->InIntr = 1;
142
143 #ifdef FUTURE_RELEASE
144                         if( HostP->Type == RIO_EISA )
145                                 INBZ( HostP->Slot, EISA_INTERRUPT_RESET );
146                         else
147 #endif
148                                 WBYTE( HostP->ResetInt , 0xff );
149
150                         rio_spin_lock(&HostP->HostLock); 
151
152                         p->_RIO_Polled++;
153                         RIOServiceHost(p, HostP, 'p' );
154                         rio_spin_lock( &HostP->HostLock); 
155                         HostP->InIntr = 0;
156                         rio_spin_unlock (&HostP->HostLock);
157                 }
158         }
159         rio_spin_unlock (&p->RIOIntrSem); 
160 }
161
162
163 char *firstchars (char *p, int nch)
164 {
165   static char buf[2][128];
166   static int t=0;
167   t = ! t;
168   memcpy (buf[t], p, nch);
169   buf[t][nch] = 0;
170   return buf[t];
171 }
172
173
174 #define INCR( P, I )    ((P) = (((P)+(I)) & p->RIOBufferMask))
175 /* Enable and start the transmission of packets */
176 void
177 RIOTxEnable(en)
178 char *          en;
179 {
180   struct Port * PortP;
181   struct rio_info *p;
182   struct tty_struct* tty;
183   int c;
184   struct PKT *  PacketP;
185   unsigned long flags;
186
187   PortP = (struct Port *)en; 
188   p = (struct rio_info *)PortP->p;
189   tty = PortP->gs.tty;
190
191
192   rio_dprintk (RIO_DEBUG_INTR, "tx port %d: %d chars queued.\n", 
193               PortP->PortNum, PortP->gs.xmit_cnt);
194
195   if (!PortP->gs.xmit_cnt) return;
196   
197
198   /* This routine is an order of magnitude simpler than the specialix
199      version. One of the disadvantages is that this version will send
200      an incomplete packet (usually 64 bytes instead of 72) once for
201      every 4k worth of data. Let's just say that this won't influence
202      performance significantly..... */
203
204   rio_spin_lock_irqsave(&PortP->portSem, flags);
205
206   while (can_add_transmit( &PacketP, PortP )) {
207     c = PortP->gs.xmit_cnt;
208     if (c > PKT_MAX_DATA_LEN) c = PKT_MAX_DATA_LEN;
209
210     /* Don't copy past the end of the source buffer */
211     if (c > SERIAL_XMIT_SIZE - PortP->gs.xmit_tail) 
212       c = SERIAL_XMIT_SIZE - PortP->gs.xmit_tail;
213
214     { int t;
215     t = (c > 10)?10:c;
216     
217     rio_dprintk (RIO_DEBUG_INTR, "rio: tx port %d: copying %d chars: %s - %s\n", 
218                  PortP->PortNum, c, 
219                  firstchars (PortP->gs.xmit_buf + PortP->gs.xmit_tail      , t),
220                  firstchars (PortP->gs.xmit_buf + PortP->gs.xmit_tail + c-t, t));
221     }
222     /* If for one reason or another, we can't copy more data, 
223        we're done! */
224     if (c == 0) break;
225
226     rio_memcpy_toio (PortP->HostP->Caddr, (caddr_t)PacketP->data, 
227                  PortP->gs.xmit_buf + PortP->gs.xmit_tail, c);
228     /*    udelay (1); */
229
230     writeb (c, &(PacketP->len));
231     if (!( PortP->State & RIO_DELETED ) ) {
232       add_transmit ( PortP );
233       /*
234       ** Count chars tx'd for port statistics reporting
235       */
236       if ( PortP->statsGather )
237         PortP->txchars += c;
238     }
239     PortP->gs.xmit_tail = (PortP->gs.xmit_tail + c) & (SERIAL_XMIT_SIZE-1);
240     PortP->gs.xmit_cnt -= c;
241   }
242
243   rio_spin_unlock_irqrestore(&PortP->portSem, flags);
244
245   if (PortP->gs.xmit_cnt <= (PortP->gs.wakeup_chars + 2*PKT_MAX_DATA_LEN)) {
246     rio_dprintk (RIO_DEBUG_INTR, "Waking up.... ldisc:%d (%d/%d)....",
247                  (int)(PortP->gs.tty->flags & (1 << TTY_DO_WRITE_WAKEUP)),
248                  PortP->gs.wakeup_chars, PortP->gs.xmit_cnt); 
249     if ((PortP->gs.tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
250         PortP->gs.tty->ldisc.write_wakeup)
251       (PortP->gs.tty->ldisc.write_wakeup)(PortP->gs.tty);
252     rio_dprintk (RIO_DEBUG_INTR, "(%d/%d)\n",
253                 PortP->gs.wakeup_chars, PortP->gs.xmit_cnt); 
254     wake_up_interruptible(&PortP->gs.tty->write_wait);
255   }
256
257 }
258
259
260 /*
261 ** When a real-life interrupt comes in here, we try to find out
262 ** which host card it belongs to, and then service only that host
263 ** Notice the cunning way that, once we've found a candidate, we
264 ** continue just in case we are sharing interrupts.
265 */
266 void
267 riointr(p)
268 struct rio_info *       p;
269 {
270         int host;
271
272         for ( host=0; host<p->RIONumHosts; host++ ) {
273                 struct Host *HostP = &p->RIOHosts[host];
274
275                 rio_dprintk (RIO_DEBUG_INTR,  "riointr() doing host %d type %d\n", host, HostP->Type);
276
277                 switch( HostP->Type ) {
278                         case RIO_AT:
279                         case RIO_MCA:
280                         case RIO_PCI:
281                                 rio_spin_lock(&HostP->HostLock);
282                                 WBYTE(HostP->ResetInt , 0xff);
283                                 if ( !HostP->InIntr ) {
284                                         HostP->InIntr = 1;
285                                         rio_spin_unlock (&HostP->HostLock);
286                                         p->_RIO_Interrupted++;
287                                         RIOServiceHost(p, HostP, 'i');
288                                         rio_spin_lock(&HostP->HostLock);
289                                         HostP->InIntr = 0;
290                                 }
291                                 rio_spin_unlock(&HostP->HostLock); 
292                                 break;
293 #ifdef FUTURE_RELEASE
294                 case RIO_EISA:
295                         if ( ivec == HostP->Ivec )
296                         {
297                                 OldSpl = LOCKB( &HostP->HostLock );
298                                 INBZ( HostP->Slot, EISA_INTERRUPT_RESET );
299                                 if ( !HostP->InIntr )
300                                 {
301                                         HostP->InIntr = 1;
302                                         UNLOCKB( &HostP->HostLock, OldSpl );
303                                         if ( this_cpu < RIO_CPU_LIMIT )
304                                         {
305                                                 int intrSpl = LOCKB( &RIOIntrLock );
306                                                 UNLOCKB( &RIOIntrLock, intrSpl );
307                                         }
308                                                 p->_RIO_Interrupted++;
309                                         RIOServiceHost( HostP, 'i' );
310                                         OldSpl = LOCKB( &HostP->HostLock );
311                                         HostP->InIntr = 0;
312                                 }
313                                 UNLOCKB( &HostP->HostLock, OldSpl );
314                                 done++;
315                         }
316                         break;
317 #endif
318                 }
319
320                 HostP->IntSrvDone++;
321         }
322
323 #ifdef FUTURE_RELEASE
324         if ( !done )
325         {
326                 cmn_err( CE_WARN, "RIO: Interrupt received with vector 0x%x\n", ivec );
327                 cmn_err( CE_CONT, "      Valid vectors are:\n");
328                 for ( host=0; host<RIONumHosts; host++ )
329                 {
330                         switch( RIOHosts[host].Type )
331                         {
332                                 case RIO_AT:
333                                 case RIO_MCA:
334                                 case RIO_EISA:
335                                                 cmn_err( CE_CONT, "0x%x ", RIOHosts[host].Ivec );
336                                                 break;
337                                 case RIO_PCI:
338                                                 cmn_err( CE_CONT, "0x%x ", get_intr_arg( RIOHosts[host].PciDevInfo.busnum, IDIST_PCI_IRQ( RIOHosts[host].PciDevInfo.slotnum, RIOHosts[host].PciDevInfo.funcnum ) ));
339                                                 break;
340                         }
341                 }
342                 cmn_err( CE_CONT, "\n" );
343         }
344 #endif
345 }
346
347 /*
348 ** RIO Host Service routine. Does all the work traditionally associated with an
349 ** interrupt.
350 */
351 static int      RupIntr;
352 static int      RxIntr;
353 static int      TxIntr;
354 void
355 RIOServiceHost(p, HostP, From)
356 struct rio_info *       p;
357 struct Host *HostP;
358 int From; 
359 {
360   rio_spin_lock (&HostP->HostLock);
361   if ( (HostP->Flags & RUN_STATE) != RC_RUNNING ) { 
362     static int t =0;
363     rio_spin_unlock (&HostP->HostLock); 
364     if ((t++ % 200) == 0)
365       rio_dprintk (RIO_DEBUG_INTR, "Interrupt but host not running. flags=%x.\n", (int)HostP->Flags);
366     return;
367   }
368   rio_spin_unlock (&HostP->HostLock); 
369
370   if ( RWORD( HostP->ParmMapP->rup_intr ) ) {
371     WWORD( HostP->ParmMapP->rup_intr , 0 );
372     p->RIORupCount++;
373     RupIntr++;
374     rio_dprintk (RIO_DEBUG_INTR, "rio: RUP interrupt on host %d\n", HostP-p->RIOHosts);
375     RIOPollHostCommands(p, HostP );
376   }
377
378   if ( RWORD( HostP->ParmMapP->rx_intr ) ) {
379     int port;
380
381     WWORD( HostP->ParmMapP->rx_intr , 0 );
382     p->RIORxCount++;
383     RxIntr++;
384
385     rio_dprintk (RIO_DEBUG_INTR, "rio: RX interrupt on host %d\n", HostP-p->RIOHosts);
386     /*
387     ** Loop through every port. If the port is mapped into
388     ** the system ( i.e. has /dev/ttyXXXX associated ) then it is
389     ** worth checking. If the port isn't open, grab any packets
390     ** hanging on its receive queue and stuff them on the free
391     ** list; check for commands on the way.
392     */
393     for ( port=p->RIOFirstPortsBooted; 
394           port<p->RIOLastPortsBooted+PORTS_PER_RTA; port++ ) {
395       struct Port *PortP = p->RIOPortp[port];
396       struct tty_struct *ttyP;
397       struct PKT *PacketP;
398                 
399       /*
400       ** not mapped in - most of the RIOPortp[] information
401       ** has not been set up!
402       ** Optimise: ports come in bundles of eight.
403       */
404       if ( !PortP->Mapped ) {
405         port += 7;
406         continue; /* with the next port */
407       }
408
409       /*
410       ** If the host board isn't THIS host board, check the next one.
411       ** optimise: ports come in bundles of eight.
412       */
413       if ( PortP->HostP != HostP ) {
414         port += 7;
415         continue;
416       }
417
418       /*
419       ** Let us see - is the port open? If not, then don't service it.
420       */
421       if ( !( PortP->PortState & PORT_ISOPEN ) ) {
422         continue;
423       }
424
425       /*
426       ** find corresponding tty structure. The process of mapping
427       ** the ports puts these here.
428       */
429       ttyP = PortP->gs.tty;
430
431       /*
432       ** Lock the port before we begin working on it.
433       */
434       rio_spin_lock(&PortP->portSem);
435
436       /*
437       ** Process received data if there is any.
438       */
439       if ( can_remove_receive( &PacketP, PortP ) )
440         RIOReceive(p, PortP);
441
442       /*
443       ** If there is no data left to be read from the port, and
444       ** it's handshake bit is set, then we must clear the handshake,
445       ** so that that downstream RTA is re-enabled.
446       */
447       if ( !can_remove_receive( &PacketP, PortP ) && 
448            ( RWORD( PortP->PhbP->handshake )==PHB_HANDSHAKE_SET ) ) {
449                                 /*
450                                 ** MAGIC! ( Basically, handshake the RX buffer, so that
451                                 ** the RTAs upstream can be re-enabled. )
452                                 */
453         rio_dprintk (RIO_DEBUG_INTR, "Set RX handshake bit\n");
454         WWORD( PortP->PhbP->handshake, 
455                PHB_HANDSHAKE_SET|PHB_HANDSHAKE_RESET );
456       }
457       rio_spin_unlock(&PortP->portSem);
458     }
459   }
460
461   if ( RWORD( HostP->ParmMapP->tx_intr ) ) {
462     int port;
463
464     WWORD( HostP->ParmMapP->tx_intr , 0);
465
466     p->RIOTxCount++;
467     TxIntr++;
468     rio_dprintk (RIO_DEBUG_INTR, "rio: TX interrupt on host %d\n", HostP-p->RIOHosts);
469
470     /*
471     ** Loop through every port.
472     ** If the port is mapped into the system ( i.e. has /dev/ttyXXXX
473     ** associated ) then it is worth checking.
474     */
475     for ( port=p->RIOFirstPortsBooted; 
476           port<p->RIOLastPortsBooted+PORTS_PER_RTA; port++ ) {
477       struct Port *PortP = p->RIOPortp[port];
478       struct tty_struct *ttyP;
479       struct PKT *PacketP;
480
481       /*
482       ** not mapped in - most of the RIOPortp[] information
483       ** has not been set up!
484       */
485       if ( !PortP->Mapped ) {
486         port += 7;
487         continue; /* with the next port */
488       }
489
490       /*
491       ** If the host board isn't running, then its data structures
492       ** are no use to us - continue quietly.
493       */
494       if ( PortP->HostP != HostP ) {
495         port += 7;
496         continue; /* with the next port */
497       }
498
499       /*
500       ** Let us see - is the port open? If not, then don't service it.
501       */
502       if ( !( PortP->PortState & PORT_ISOPEN ) ) {
503         continue;
504       }
505
506       rio_dprintk (RIO_DEBUG_INTR, "rio: Looking into port %d.\n", port);
507       /*
508       ** Lock the port before we begin working on it.
509       */
510       rio_spin_lock(&PortP->portSem);
511
512       /*
513       ** If we can't add anything to the transmit queue, then
514       ** we need do none of this processing.
515       */
516       if ( !can_add_transmit( &PacketP, PortP ) ) {
517         rio_dprintk (RIO_DEBUG_INTR, "Can't add to port, so skipping.\n");
518         rio_spin_unlock(&PortP->portSem);
519         continue;
520       }
521
522       /*
523       ** find corresponding tty structure. The process of mapping
524       ** the ports puts these here.
525       */
526       ttyP = PortP->gs.tty;
527       /* If ttyP is NULL, the port is getting closed. Forget about it. */
528       if (!ttyP) {
529         rio_dprintk (RIO_DEBUG_INTR, "no tty, so skipping.\n");
530         rio_spin_unlock(&PortP->portSem);
531         continue;
532       }
533       /*
534       ** If there is more room available we start up the transmit
535       ** data process again. This can be direct I/O, if the cookmode
536       ** is set to COOK_RAW or COOK_MEDIUM, or will be a call to the
537       ** riotproc( T_OUTPUT ) if we are in COOK_WELL mode, to fetch
538       ** characters via the line discipline. We must always call
539       ** the line discipline,
540       ** so that user input characters can be echoed correctly.
541       **
542       ** ++++ Update +++++
543       ** With the advent of double buffering, we now see if
544       ** TxBufferOut-In is non-zero. If so, then we copy a packet
545       ** to the output place, and set it going. If this empties
546       ** the buffer, then we must issue a wakeup( ) on OUT.
547       ** If it frees space in the buffer then we must issue
548       ** a wakeup( ) on IN.
549       **
550       ** ++++ Extra! Extra! If PortP->WflushFlag is set, then we
551       ** have to send a WFLUSH command down the PHB, to mark the
552       ** end point of a WFLUSH. We also need to clear out any
553       ** data from the double buffer! ( note that WflushFlag is a
554       ** *count* of the number of WFLUSH commands outstanding! )
555       **
556       ** ++++ And there's more!
557       ** If an RTA is powered off, then on again, and rebooted,
558       ** whilst it has ports open, then we need to re-open the ports.
559       ** ( reasonable enough ). We can't do this when we spot the
560       ** re-boot, in interrupt time, because the queue is probably
561       ** full. So, when we come in here, we need to test if any
562       ** ports are in this condition, and re-open the port before
563       ** we try to send any more data to it. Now, the re-booted
564       ** RTA will be discarding packets from the PHB until it
565       ** receives this open packet, but don't worry tooo much
566       ** about that. The one thing that is interesting is the
567       ** combination of this effect and the WFLUSH effect!
568       */
569       /* For now don't handle RTA reboots. -- REW. 
570          Reenabled. Otherwise RTA reboots didn't work. Duh. -- REW */
571       if ( PortP->MagicFlags ) {
572 #if 1
573         if ( PortP->MagicFlags & MAGIC_REBOOT ) {
574           /*
575           ** well, the RTA has been rebooted, and there is room
576           ** on its queue to add the open packet that is required.
577           **
578           ** The messy part of this line is trying to decide if
579           ** we need to call the Param function as a tty or as
580           ** a modem.
581           ** DONT USE CLOCAL AS A TEST FOR THIS!
582           **
583           ** If we can't param the port, then move on to the
584           ** next port.
585           */
586           PortP->InUse = NOT_INUSE;
587
588           rio_spin_unlock(&PortP->portSem);
589           if ( RIOParam(PortP, OPEN, ((PortP->Cor2Copy & 
590                                        (COR2_RTSFLOW|COR2_CTSFLOW ) )== 
591                                       (COR2_RTSFLOW|COR2_CTSFLOW ) ) ? 
592                         TRUE : FALSE, DONT_SLEEP ) == RIO_FAIL ) {
593             continue; /* with next port */
594           }
595           rio_spin_lock(&PortP->portSem);
596           PortP->MagicFlags &= ~MAGIC_REBOOT;
597         }
598 #endif
599
600         /*
601         ** As mentioned above, this is a tacky hack to cope
602         ** with WFLUSH
603         */
604         if ( PortP->WflushFlag ) {
605           rio_dprintk (RIO_DEBUG_INTR, "Want to WFLUSH mark this port\n");
606
607           if ( PortP->InUse )
608             rio_dprintk (RIO_DEBUG_INTR, "FAILS - PORT IS IN USE\n");
609         }
610                                 
611         while ( PortP->WflushFlag &&
612                 can_add_transmit( &PacketP, PortP ) && 
613                 ( PortP->InUse == NOT_INUSE ) ) {
614           int p;
615           struct PktCmd *PktCmdP;
616
617           rio_dprintk (RIO_DEBUG_INTR, "Add WFLUSH marker to data queue\n");
618           /*
619           ** make it look just like a WFLUSH command
620           */
621           PktCmdP = ( struct PktCmd * )&PacketP->data[0];
622
623           WBYTE( PktCmdP->Command , WFLUSH );
624
625           p =  PortP->HostPort % ( ushort )PORTS_PER_RTA;
626
627           /*
628           ** If second block of ports for 16 port RTA, add 8
629           ** to index 8-15.
630           */
631           if ( PortP->SecondBlock )
632             p += PORTS_PER_RTA;
633
634           WBYTE( PktCmdP->PhbNum, p );
635
636           /*
637           ** to make debuggery easier
638           */
639           WBYTE( PacketP->data[ 2], 'W'  );
640           WBYTE( PacketP->data[ 3], 'F'  );
641           WBYTE( PacketP->data[ 4], 'L'  );
642           WBYTE( PacketP->data[ 5], 'U'  );
643           WBYTE( PacketP->data[ 6], 'S'  );
644           WBYTE( PacketP->data[ 7], 'H'  );
645           WBYTE( PacketP->data[ 8], ' '  );
646           WBYTE( PacketP->data[ 9], '0'+PortP->WflushFlag );
647           WBYTE( PacketP->data[10], ' '  );
648           WBYTE( PacketP->data[11], ' '  );
649           WBYTE( PacketP->data[12], '\0' );
650
651           /*
652           ** its two bytes long!
653           */
654           WBYTE( PacketP->len , PKT_CMD_BIT | 2 );
655
656           /*
657           ** queue it!
658           */
659           if ( !( PortP->State & RIO_DELETED ) ) {
660             add_transmit( PortP );
661             /*
662             ** Count chars tx'd for port statistics reporting
663             */
664             if ( PortP->statsGather )
665               PortP->txchars += 2;
666           }
667
668           if ( --( PortP->WflushFlag ) == 0 ) {
669             PortP->MagicFlags &= ~MAGIC_FLUSH;
670           }
671
672           rio_dprintk (RIO_DEBUG_INTR, "Wflush count now stands at %d\n", 
673                  PortP->WflushFlag);
674         }
675         if ( PortP->MagicFlags & MORE_OUTPUT_EYGOR ) {
676           if ( PortP->MagicFlags & MAGIC_FLUSH ) {
677             PortP->MagicFlags |= MORE_OUTPUT_EYGOR;
678           }
679           else {
680             if ( !can_add_transmit( &PacketP, PortP ) ) {
681               rio_spin_unlock(&PortP->portSem);
682               continue;
683             }
684             rio_spin_unlock(&PortP->portSem);
685             RIOTxEnable((char *)PortP);
686             rio_spin_lock(&PortP->portSem);
687             PortP->MagicFlags &= ~MORE_OUTPUT_EYGOR;
688           }
689         }
690       }
691
692
693       /*
694       ** If we can't add anything to the transmit queue, then
695       ** we need do none of the remaining processing.
696       */
697       if (!can_add_transmit( &PacketP, PortP ) ) {
698         rio_spin_unlock(&PortP->portSem);
699         continue;
700       }
701
702       rio_spin_unlock(&PortP->portSem);
703       RIOTxEnable((char *)PortP);
704     }
705   }
706 }
707
708 /*
709 ** Routine for handling received data for clist drivers.
710 ** NB: Called with the tty locked. The spl from the lockb( ) is passed.
711 ** we return the ttySpl level that we re-locked at.
712 */
713 void
714 RIOReceive(p, PortP)
715 struct rio_info *       p;
716 struct Port *           PortP;
717 {
718   struct tty_struct *TtyP;
719   register ushort transCount;
720   struct PKT *PacketP;
721   register uint DataCnt;
722   uchar *       ptr;
723   int copied =0;
724
725   static int intCount, RxIntCnt;
726
727   /*
728   ** The receive data process is to remove packets from the
729   ** PHB until there aren't any more or the current cblock
730   ** is full. When this occurs, there will be some left over
731   ** data in the packet, that we must do something with.
732   ** As we haven't unhooked the packet from the read list
733   ** yet, we can just leave the packet there, having first
734   ** made a note of how far we got. This means that we need
735   ** a pointer per port saying where we start taking the
736   ** data from - this will normally be zero, but when we
737   ** run out of space it will be set to the offset of the
738   ** next byte to copy from the packet data area. The packet
739   ** length field is decremented by the number of bytes that
740   ** we succesfully removed from the packet. When this reaches
741   ** zero, we reset the offset pointer to be zero, and free
742   ** the packet from the front of the queue.
743   */
744
745   intCount++;
746
747   TtyP = PortP->gs.tty;
748   if (!TtyP) {
749     rio_dprintk (RIO_DEBUG_INTR, "RIOReceive: tty is null. \n");
750     return;
751   }
752
753   if (PortP->State & RIO_THROTTLE_RX) {
754     rio_dprintk (RIO_DEBUG_INTR, "RIOReceive: Throttled. Can't handle more input.\n");
755     return;
756   }
757
758   if ( PortP->State & RIO_DELETED )
759     {
760       while ( can_remove_receive( &PacketP, PortP ) )
761         {
762           remove_receive( PortP );
763           put_free_end( PortP->HostP, PacketP );
764         }
765     }
766   else
767     {
768       /*
769       ** loop, just so long as:
770       **   i ) there's some data ( i.e. can_remove_receive )
771       **  ii ) we haven't been blocked
772       ** iii ) there's somewhere to put the data
773       **  iv ) we haven't outstayed our welcome
774       */
775       transCount = 1;
776       while ( can_remove_receive(&PacketP, PortP)
777               && transCount)
778         {
779 #ifdef STATS
780           PortP->Stat.RxIntCnt++;
781 #endif /* STATS */
782           RxIntCnt++;
783
784           /*
785           ** check that it is not a command!
786           */
787           if ( PacketP->len & PKT_CMD_BIT ) {
788             rio_dprintk (RIO_DEBUG_INTR, "RIO: unexpected command packet received on PHB\n");
789             /*      rio_dprint(RIO_DEBUG_INTR, (" sysport   = %d\n", p->RIOPortp->PortNum)); */
790             rio_dprintk (RIO_DEBUG_INTR, " dest_unit = %d\n", PacketP->dest_unit);
791             rio_dprintk (RIO_DEBUG_INTR, " dest_port = %d\n", PacketP->dest_port);
792             rio_dprintk (RIO_DEBUG_INTR, " src_unit  = %d\n", PacketP->src_unit);
793             rio_dprintk (RIO_DEBUG_INTR, " src_port  = %d\n", PacketP->src_port);
794             rio_dprintk (RIO_DEBUG_INTR, " len     = %d\n", PacketP->len);
795             rio_dprintk (RIO_DEBUG_INTR, " control   = %d\n", PacketP->control);
796             rio_dprintk (RIO_DEBUG_INTR, " csum    = %d\n", PacketP->csum);
797             rio_dprintk (RIO_DEBUG_INTR, "       data bytes: ");
798             for ( DataCnt=0; DataCnt<PKT_MAX_DATA_LEN; DataCnt++ )
799               rio_dprintk (RIO_DEBUG_INTR, "%d\n", PacketP->data[DataCnt]);
800             remove_receive( PortP );
801             put_free_end( PortP->HostP, PacketP );
802             continue; /* with next packet */
803           }
804
805           /*
806           ** How many characters can we move 'upstream' ?
807           **
808           ** Determine the minimum of the amount of data
809           ** available and the amount of space in which to
810           ** put it.
811           **
812           ** 1. Get the packet length by masking 'len'
813           **    for only the length bits.
814           ** 2. Available space is [buffer size] - [space used]
815           **
816           ** Transfer count is the minimum of packet length
817           ** and available space.
818           */
819                         
820           transCount = min_t(unsigned int, PacketP->len & PKT_LEN_MASK,
821                            TTY_FLIPBUF_SIZE - TtyP->flip.count);
822           rio_dprintk (RIO_DEBUG_REC,  "port %d: Copy %d bytes\n", 
823                                       PortP->PortNum, transCount);
824           /*
825           ** To use the following 'kkprintfs' for debugging - change the '#undef'
826           ** to '#define', (this is the only place ___DEBUG_IT___ occurs in the
827           ** driver).
828           */
829 #undef ___DEBUG_IT___
830 #ifdef ___DEBUG_IT___
831           kkprintf("I:%d R:%d P:%d Q:%d C:%d F:%x ",
832                    intCount,
833                    RxIntCnt,
834                    PortP->PortNum,
835                    TtyP->rxqueue.count,
836                    transCount,
837                    TtyP->flags );
838 #endif
839           ptr = (uchar *) PacketP->data + PortP->RxDataStart;
840
841           rio_memcpy_fromio (TtyP->flip.char_buf_ptr, ptr, transCount);
842           memset(TtyP->flip.flag_buf_ptr, TTY_NORMAL, transCount);
843
844 #ifdef STATS
845           /*
846           ** keep a count for statistical purposes
847           */
848           PortP->Stat.RxCharCnt += transCount;
849 #endif
850           PortP->RxDataStart    += transCount;
851           PacketP->len          -= transCount;
852           copied += transCount;
853           TtyP->flip.count += transCount;
854           TtyP->flip.char_buf_ptr += transCount;
855           TtyP->flip.flag_buf_ptr += transCount;
856
857
858 #ifdef ___DEBUG_IT___
859           kkprintf("T:%d L:%d\n", DataCnt, PacketP->len );
860 #endif
861
862           if ( PacketP->len == 0 )
863             {
864                                 /*
865                                 ** If we have emptied the packet, then we can
866                                 ** free it, and reset the start pointer for
867                                 ** the next packet.
868                                 */
869               remove_receive( PortP );
870               put_free_end( PortP->HostP, PacketP );
871               PortP->RxDataStart = 0;
872 #ifdef STATS
873                                 /*
874                                 ** more lies ( oops, I mean statistics )
875                                 */
876               PortP->Stat.RxPktCnt++;
877 #endif /* STATS */
878             }
879         }
880     }
881   if (copied) {
882     rio_dprintk (RIO_DEBUG_REC, "port %d: pushing tty flip buffer: %d total bytes copied.\n", PortP->PortNum, copied);
883     tty_flip_buffer_push (TtyP);
884   }
885
886   return;
887 }
888
889 #ifdef FUTURE_RELEASE
890 /*
891 ** The proc routine called by the line discipline to do the work for it.
892 ** The proc routine works hand in hand with the interrupt routine.
893 */
894 int
895 riotproc(p, tp, cmd, port)
896 struct rio_info *       p;
897 register struct ttystatics *tp;
898 int cmd;
899 int     port;
900 {
901         register struct Port *PortP;
902         int SysPort;
903         struct PKT *PacketP;
904
905         SysPort = port; /* Believe me, it works. */
906
907         if ( SysPort < 0 || SysPort >= RIO_PORTS ) {
908                 rio_dprintk (RIO_DEBUG_INTR, "Illegal port %d derived from TTY in riotproc()\n",SysPort);
909                 return 0;
910         }
911         PortP = p->RIOPortp[SysPort];
912
913         if ((uint)PortP->PhbP < (uint)PortP->Caddr || 
914                         (uint)PortP->PhbP >= (uint)PortP->Caddr+SIXTY_FOUR_K ) {
915                 rio_dprintk (RIO_DEBUG_INTR, "RIO: NULL or BAD PhbP on sys port %d in proc routine\n",
916                                                         SysPort);
917                 rio_dprintk (RIO_DEBUG_INTR, "   PortP = 0x%x\n",PortP);
918                 rio_dprintk (RIO_DEBUG_INTR, "   PortP->PhbP = 0x%x\n",PortP->PhbP);
919                 rio_dprintk (RIO_DEBUG_INTR, "   PortP->Caddr = 0x%x\n",PortP->PhbP);
920                 rio_dprintk (RIO_DEBUG_INTR, "   PortP->HostPort = 0x%x\n",PortP->HostPort);
921                 return 0;
922         }
923
924         switch(cmd) {
925                 case T_WFLUSH:
926                         rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH\n");
927                         /*
928                         ** Because of the spooky way the RIO works, we don't need
929                         ** to issue a flush command on any of the SET*F commands,
930                         ** as that causes trouble with getty and login, which issue
931                         ** these commands to incur a READ flush, and rely on the fact
932                         ** that the line discipline does a wait for drain for them.
933                         ** As the rio doesn't wait for drain, the write flush would
934                         ** destroy the Password: prompt. This isn't very friendly, so
935                         ** here we only issue a WFLUSH command if we are in the interrupt
936                         ** routine, or we aren't executing a SET*F command.
937                         */
938                         if ( PortP->HostP->InIntr || !PortP->FlushCmdBodge ) {
939                                 /*
940                                 ** form a wflush packet - 1 byte long, no data
941                                 */
942                                 if ( PortP->State & RIO_DELETED ) {
943                                         rio_dprintk (RIO_DEBUG_INTR, "WFLUSH on deleted RTA\n");
944                                 }
945                                 else {
946                                         if ( RIOPreemptiveCmd(p, PortP, WFLUSH ) == RIO_FAIL ) {
947                                                 rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH Command failed\n");
948                                         }
949                                         else
950                                                 rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH Command\n");
951                                 }
952                                 /*
953                                 ** WFLUSH operation - flush the data!
954                                 */
955                                 PortP->TxBufferIn = PortP->TxBufferOut = 0;
956                         }
957                         else {
958                                 rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH Command ignored\n");
959                         }
960                         /*
961                         ** sort out the line discipline
962                         */
963                         if (PortP->CookMode == COOK_WELL)
964                                 goto start;
965                         break;
966         
967                 case T_RESUME:
968                         rio_dprintk (RIO_DEBUG_INTR, "T_RESUME\n");
969                         /*
970                         ** send pre-emptive resume packet
971                         */
972                         if ( PortP->State & RIO_DELETED ) {
973                                 rio_dprintk (RIO_DEBUG_INTR, "RESUME on deleted RTA\n");
974                         }
975                         else {
976                                 if ( RIOPreemptiveCmd(p, PortP, RESUME ) == RIO_FAIL ) {
977                                         rio_dprintk (RIO_DEBUG_INTR, "T_RESUME Command failed\n");
978                                 }
979                         }
980                         /*
981                         ** and re-start the sender software!
982                         */
983                         if (PortP->CookMode == COOK_WELL)
984                                 goto start;
985                         break;
986         
987                 case T_TIME:
988                         rio_dprintk (RIO_DEBUG_INTR, "T_TIME\n");
989                         /*
990                         ** T_TIME is called when xDLY is set in oflags and
991                         ** the line discipline timeout has expired. It's
992                         ** function in life is to clear the TIMEOUT flag
993                         ** and to re-start output to the port.
994                         */
995                         /*
996                         ** Fall through and re-start output
997                         */
998                 case T_OUTPUT:
999 start:
1000                         if ( PortP->MagicFlags & MAGIC_FLUSH ) {
1001                                 PortP->MagicFlags |= MORE_OUTPUT_EYGOR;
1002                                 return 0;
1003                         }
1004                         RIOTxEnable((char *)PortP);
1005                         PortP->MagicFlags &= ~MORE_OUTPUT_EYGOR;
1006                         /*rio_dprint(RIO_DEBUG_INTR, PortP,DBG_PROC,"T_OUTPUT finished\n");*/
1007                         break;
1008         
1009                 case T_SUSPEND:
1010                         rio_dprintk (RIO_DEBUG_INTR, "T_SUSPEND\n");
1011                         /*
1012                         ** send a suspend pre-emptive packet.
1013                         */
1014                         if ( PortP->State & RIO_DELETED ) {
1015                                 rio_dprintk (RIO_DEBUG_INTR, "SUSPEND deleted RTA\n");
1016                         }
1017                         else {
1018                                 if ( RIOPreemptiveCmd(p, PortP, SUSPEND ) == RIO_FAIL ) {
1019                                         rio_dprintk (RIO_DEBUG_INTR, "T_SUSPEND Command failed\n");
1020                                 }
1021                         }
1022                         /*
1023                         ** done!
1024                         */
1025                         break;
1026         
1027                 case T_BLOCK:
1028                         rio_dprintk (RIO_DEBUG_INTR, "T_BLOCK\n");
1029                         break;
1030         
1031                 case T_RFLUSH:
1032                         rio_dprintk (RIO_DEBUG_INTR, "T_RFLUSH\n");
1033                         if ( PortP->State & RIO_DELETED ) {
1034                                 rio_dprintk (RIO_DEBUG_INTR, "RFLUSH on deleted RTA\n");
1035                                 PortP->RxDataStart = 0;
1036                         }
1037                         else {
1038                                 if ( RIOPreemptiveCmd( p, PortP, RFLUSH ) == RIO_FAIL ) {
1039                                         rio_dprintk (RIO_DEBUG_INTR, "T_RFLUSH Command failed\n");
1040                                         return 0;
1041                                 }
1042                                 PortP->RxDataStart = 0;
1043                                 while ( can_remove_receive(&PacketP, PortP) ) {
1044                                         remove_receive(PortP);
1045                                         ShowPacket(DBG_PROC, PacketP );
1046                                         put_free_end(PortP->HostP, PacketP );
1047                                 }
1048                                 if ( PortP->PhbP->handshake == PHB_HANDSHAKE_SET ) {
1049                                         /*
1050                                         ** MAGIC!
1051                                         */
1052                                         rio_dprintk (RIO_DEBUG_INTR, "Set receive handshake bit\n");
1053                                         PortP->PhbP->handshake |= PHB_HANDSHAKE_RESET;
1054                                 }
1055                         }
1056                         break;
1057                         /* FALLTHROUGH */
1058                 case T_UNBLOCK:
1059                         rio_dprintk (RIO_DEBUG_INTR, "T_UNBLOCK\n");
1060                         /*
1061                         ** If there is any data to receive set a timeout to service it.
1062                         */
1063                         RIOReceive(p, PortP);
1064                         break;
1065         
1066                 case T_BREAK:
1067                         rio_dprintk (RIO_DEBUG_INTR, "T_BREAK\n");
1068                         /*
1069                         ** Send a break command. For Sys V
1070                         ** this is a timed break, so we
1071                         ** send a SBREAK[time] packet
1072                         */
1073                         /*
1074                         ** Build a BREAK command
1075                         */
1076                         if ( PortP->State & RIO_DELETED ) {
1077                                 rio_dprintk (RIO_DEBUG_INTR, "BREAK on deleted RTA\n");
1078                         }
1079                         else {
1080                                 if (RIOShortCommand(PortP,SBREAK,2,
1081                                                                 p->RIOConf.BreakInterval)==RIO_FAIL) {
1082                                         rio_dprintk (RIO_DEBUG_INTR, "SBREAK RIOShortCommand failed\n");
1083                                 }
1084                         }
1085         
1086                         /*
1087                         ** done!
1088                         */
1089                         break;
1090         
1091                 case T_INPUT:
1092                         rio_dprintk (RIO_DEBUG_INTR, "Proc T_INPUT called - I don't know what to do!\n");
1093                         break;
1094                 case T_PARM:
1095                         rio_dprintk (RIO_DEBUG_INTR, "Proc T_PARM called - I don't know what to do!\n");
1096                         break;
1097         
1098                 case T_SWTCH:
1099                         rio_dprintk (RIO_DEBUG_INTR, "Proc T_SWTCH called - I don't know what to do!\n");
1100                         break;
1101         
1102                 default:
1103                         rio_dprintk (RIO_DEBUG_INTR, "Proc UNKNOWN command %d\n",cmd);
1104         }
1105         /*
1106         ** T_OUTPUT returns without passing through this point!
1107         */
1108         /*rio_dprint(RIO_DEBUG_INTR, PortP,DBG_PROC,"riotproc done\n");*/
1109         return(0);
1110 }
1111 #endif