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
33static char *_riointr_c_sccs_ = "@(#)riointr.c	1.2";
34#endif
35
36
37#define __NO_VERSION__
38#include <linux/module.h>
39#include <linux/slab.h>
40#include <linux/errno.h>
41#include <linux/tty.h>
42#include <asm/io.h>
43#include <asm/system.h>
44#include <asm/string.h>
45#include <asm/semaphore.h>
46#include <asm/uaccess.h>
47
48#include <linux/termios.h>
49#include <linux/serial.h>
50
51#include <linux/compatmac.h>
52#include <linux/generic_serial.h>
53
54#include <linux/delay.h>
55
56#include "linux_compat.h"
57#include "rio_linux.h"
58#include "typdef.h"
59#include "pkt.h"
60#include "daemon.h"
61#include "rio.h"
62#include "riospace.h"
63#include "top.h"
64#include "cmdpkt.h"
65#include "map.h"
66#include "riotypes.h"
67#include "rup.h"
68#include "port.h"
69#include "riodrvr.h"
70#include "rioinfo.h"
71#include "func.h"
72#include "errors.h"
73#include "pci.h"
74
75#include "parmmap.h"
76#include "unixrup.h"
77#include "board.h"
78#include "host.h"
79#include "error.h"
80#include "phb.h"
81#include "link.h"
82#include "cmdblk.h"
83#include "route.h"
84#include "control.h"
85#include "cirrus.h"
86#include "rioioctl.h"
87
88
89
90
91/*
92** riopoll is called every clock tick. Once the /dev/rio device has been
93** opened, and polldistributed( ) has been called, this routine is called
94** every clock tick *by every cpu*. The 'interesting' piece of code that
95** manipulates 'RIONumCpus' and 'RIOCpuCountdown' is used to fair-share
96** the work between the CPUs. If there are 'N' cpus, then each poll time
97** we increment a counter, modulo 'N-1'. When this counter is 0, we call
98** the interrupt handler. This has the effect that polls are serviced
99** by processor 'N', 'N-1', 'N-2', ... '0', round and round. Neat.
100*/
101void
102riopoll(p)
103struct rio_info *	p;
104{
105	int   host;
106
107	/*
108	** Here's the deal. We try to fair share as much as possible amongst
109	** all the processors that are available. Since each processor
110	** should generate HZ ticks per second and since we only need HZ ticks
111	** in total for proper operation we simply attempt to cycle round each
112	** processor in turn, using RIOCpuCountdown to decide whether to call
113	** the interrupt routine. ( In fact the count zeroes when it reaches
114	** one less than the total number of processors - so e.g. on a two
115	** processor system RIOService will get called 2*HZ times per second. )
116	** this_cpu (cur_cpu()) tells us the number of the current processor
117	** as follows:
118	**
119	**		0 - default CPU
120	**		1 - first extra CPU
121	**		2 - second extra CPU
122	**		etc.
123	*/
124
125	/*
126	** okay, we've got a cpu that hasn't had a go recently
127	** - lets check to see what needs doing.
128	*/
129	for ( host=0; host<p->RIONumHosts; host++ ) {
130		struct Host *HostP = &p->RIOHosts[host];
131
132		rio_spin_lock( &HostP->HostLock );
133
134		if ( ( (HostP->Flags & RUN_STATE) != RC_RUNNING ) ||
135		     HostP->InIntr ) {
136			rio_spin_unlock (&HostP->HostLock);
137			continue;
138		}
139
140		if ( RWORD( HostP->ParmMapP->rup_intr ) ||
141			 RWORD( HostP->ParmMapP->rx_intr  ) ||
142			 RWORD( HostP->ParmMapP->tx_intr  ) ) {
143			HostP->InIntr = 1;
144
145#ifdef FUTURE_RELEASE
146			if( HostP->Type == RIO_EISA )
147				INBZ( HostP->Slot, EISA_INTERRUPT_RESET );
148			else
149#endif
150				WBYTE( HostP->ResetInt , 0xff );
151
152			rio_spin_lock(&HostP->HostLock);
153
154			p->_RIO_Polled++;
155			RIOServiceHost(p, HostP, 'p' );
156			rio_spin_lock( &HostP->HostLock);
157			HostP->InIntr = 0;
158			rio_spin_unlock (&HostP->HostLock);
159		}
160	}
161	rio_spin_unlock (&p->RIOIntrSem);
162}
163
164
165char *firstchars (char *p, int nch)
166{
167  static char buf[2][128];
168  static int t=0;
169  t = ! t;
170  memcpy (buf[t], p, nch);
171  buf[t][nch] = 0;
172  return buf[t];
173}
174
175
176#define	INCR( P, I )	((P) = (((P)+(I)) & p->RIOBufferMask))
177/* Enable and start the transmission of packets */
178void
179RIOTxEnable(en)
180char *		en;
181{
182  struct Port *	PortP;
183  struct rio_info *p;
184  struct tty_struct* tty;
185  int c;
186  struct PKT *	PacketP;
187  unsigned long flags;
188
189  PortP = (struct Port *)en;
190  p = (struct rio_info *)PortP->p;
191  tty = PortP->gs.tty;
192
193
194  rio_dprintk (RIO_DEBUG_INTR, "tx port %d: %d chars queued.\n",
195	      PortP->PortNum, PortP->gs.xmit_cnt);
196
197  if (!PortP->gs.xmit_cnt) return;
198
199
200  /* This routine is an order of magnitude simpler than the specialix
201     version. One of the disadvantages is that this version will send
202     an incomplete packet (usually 64 bytes instead of 72) once for
203     every 4k worth of data. Let's just say that this won't influence
204     performance significantly..... */
205
206  rio_spin_lock_irqsave(&PortP->portSem, flags);
207
208  while (can_add_transmit( &PacketP, PortP )) {
209    c = PortP->gs.xmit_cnt;
210    if (c > PKT_MAX_DATA_LEN) c = PKT_MAX_DATA_LEN;
211
212    /* Don't copy past the end of the source buffer */
213    if (c > SERIAL_XMIT_SIZE - PortP->gs.xmit_tail)
214      c = SERIAL_XMIT_SIZE - PortP->gs.xmit_tail;
215
216    { int t;
217    t = (c > 10)?10:c;
218
219    rio_dprintk (RIO_DEBUG_INTR, "rio: tx port %d: copying %d chars: %s - %s\n",
220		 PortP->PortNum, c,
221		 firstchars (PortP->gs.xmit_buf + PortP->gs.xmit_tail      , t),
222		 firstchars (PortP->gs.xmit_buf + PortP->gs.xmit_tail + c-t, t));
223    }
224    /* If for one reason or another, we can't copy more data,
225       we're done! */
226    if (c == 0) break;
227
228    rio_memcpy_toio (PortP->HostP->Caddr, (caddr_t)PacketP->data,
229		 PortP->gs.xmit_buf + PortP->gs.xmit_tail, c);
230    /*    udelay (1); */
231
232    writeb (c, &(PacketP->len));
233    if (!( PortP->State & RIO_DELETED ) ) {
234      add_transmit ( PortP );
235      /*
236      ** Count chars tx'd for port statistics reporting
237      */
238      if ( PortP->statsGather )
239	PortP->txchars += c;
240    }
241    PortP->gs.xmit_tail = (PortP->gs.xmit_tail + c) & (SERIAL_XMIT_SIZE-1);
242    PortP->gs.xmit_cnt -= c;
243  }
244
245  rio_spin_unlock_irqrestore(&PortP->portSem, flags);
246
247  if (PortP->gs.xmit_cnt <= (PortP->gs.wakeup_chars + 2*PKT_MAX_DATA_LEN)) {
248    rio_dprintk (RIO_DEBUG_INTR, "Waking up.... ldisc:%d (%d/%d)....",
249		 (int)(PortP->gs.tty->flags & (1 << TTY_DO_WRITE_WAKEUP)),
250		 PortP->gs.wakeup_chars, PortP->gs.xmit_cnt);
251    if ((PortP->gs.tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
252	PortP->gs.tty->ldisc.write_wakeup)
253      (PortP->gs.tty->ldisc.write_wakeup)(PortP->gs.tty);
254    rio_dprintk (RIO_DEBUG_INTR, "(%d/%d)\n",
255		PortP->gs.wakeup_chars, PortP->gs.xmit_cnt);
256    wake_up_interruptible(&PortP->gs.tty->write_wait);
257  }
258
259}
260
261
262/*
263** When a real-life interrupt comes in here, we try to find out
264** which host card it belongs to, and then service only that host
265** Notice the cunning way that, once we've found a candidate, we
266** continue just in case we are sharing interrupts.
267*/
268void
269riointr(p)
270struct rio_info *	p;
271{
272	int host;
273
274	for ( host=0; host<p->RIONumHosts; host++ ) {
275		struct Host *HostP = &p->RIOHosts[host];
276
277		rio_dprintk (RIO_DEBUG_INTR,  "riointr() doing host %d type %d\n", host, HostP->Type);
278
279		switch( HostP->Type ) {
280			case RIO_AT:
281			case RIO_MCA:
282			case RIO_PCI:
283			  	rio_spin_lock(&HostP->HostLock);
284				WBYTE(HostP->ResetInt , 0xff);
285				if ( !HostP->InIntr ) {
286					HostP->InIntr = 1;
287					rio_spin_unlock (&HostP->HostLock);
288					p->_RIO_Interrupted++;
289					RIOServiceHost(p, HostP, 'i');
290					rio_spin_lock(&HostP->HostLock);
291					HostP->InIntr = 0;
292				}
293				rio_spin_unlock(&HostP->HostLock);
294				break;
295#ifdef FUTURE_RELEASE
296		case RIO_EISA:
297			if ( ivec == HostP->Ivec )
298			{
299				OldSpl = LOCKB( &HostP->HostLock );
300				INBZ( HostP->Slot, EISA_INTERRUPT_RESET );
301				if ( !HostP->InIntr )
302				{
303					HostP->InIntr = 1;
304					UNLOCKB( &HostP->HostLock, OldSpl );
305					if ( this_cpu < RIO_CPU_LIMIT )
306					{
307						int intrSpl = LOCKB( &RIOIntrLock );
308						UNLOCKB( &RIOIntrLock, intrSpl );
309					}
310						p->_RIO_Interrupted++;
311					RIOServiceHost( HostP, 'i' );
312					OldSpl = LOCKB( &HostP->HostLock );
313					HostP->InIntr = 0;
314				}
315				UNLOCKB( &HostP->HostLock, OldSpl );
316				done++;
317			}
318			break;
319#endif
320		}
321
322		HostP->IntSrvDone++;
323	}
324
325#ifdef FUTURE_RELEASE
326	if ( !done )
327	{
328		cmn_err( CE_WARN, "RIO: Interrupt received with vector 0x%x\n", ivec );
329		cmn_err( CE_CONT, "	 Valid vectors are:\n");
330		for ( host=0; host<RIONumHosts; host++ )
331		{
332			switch( RIOHosts[host].Type )
333			{
334				case RIO_AT:
335				case RIO_MCA:
336				case RIO_EISA:
337						cmn_err( CE_CONT, "0x%x ", RIOHosts[host].Ivec );
338						break;
339				case RIO_PCI:
340						cmn_err( CE_CONT, "0x%x ", get_intr_arg( RIOHosts[host].PciDevInfo.busnum, IDIST_PCI_IRQ( RIOHosts[host].PciDevInfo.slotnum, RIOHosts[host].PciDevInfo.funcnum ) ));
341						break;
342			}
343		}
344		cmn_err( CE_CONT, "\n" );
345	}
346#endif
347}
348
349/*
350** RIO Host Service routine. Does all the work traditionally associated with an
351** interrupt.
352*/
353static int	RupIntr;
354static int	RxIntr;
355static int	TxIntr;
356void
357RIOServiceHost(p, HostP, From)
358struct rio_info *	p;
359struct Host *HostP;
360int From;
361{
362  rio_spin_lock (&HostP->HostLock);
363  if ( (HostP->Flags & RUN_STATE) != RC_RUNNING ) {
364    static int t =0;
365    rio_spin_unlock (&HostP->HostLock);
366    if ((t++ % 200) == 0)
367      rio_dprintk (RIO_DEBUG_INTR, "Interrupt but host not running. flags=%x.\n", (int)HostP->Flags);
368    return;
369  }
370  rio_spin_unlock (&HostP->HostLock);
371
372  if ( RWORD( HostP->ParmMapP->rup_intr ) ) {
373    WWORD( HostP->ParmMapP->rup_intr , 0 );
374    p->RIORupCount++;
375    RupIntr++;
376    rio_dprintk (RIO_DEBUG_INTR, "rio: RUP interrupt on host %d\n", HostP-p->RIOHosts);
377    RIOPollHostCommands(p, HostP );
378  }
379
380  if ( RWORD( HostP->ParmMapP->rx_intr ) ) {
381    int port;
382
383    WWORD( HostP->ParmMapP->rx_intr , 0 );
384    p->RIORxCount++;
385    RxIntr++;
386
387    rio_dprintk (RIO_DEBUG_INTR, "rio: RX interrupt on host %d\n", HostP-p->RIOHosts);
388    /*
389    ** Loop through every port. If the port is mapped into
390    ** the system ( i.e. has /dev/ttyXXXX associated ) then it is
391    ** worth checking. If the port isn't open, grab any packets
392    ** hanging on its receive queue and stuff them on the free
393    ** list; check for commands on the way.
394    */
395    for ( port=p->RIOFirstPortsBooted;
396	  port<p->RIOLastPortsBooted+PORTS_PER_RTA; port++ ) {
397      struct Port *PortP = p->RIOPortp[port];
398      struct tty_struct *ttyP;
399      struct PKT *PacketP;
400
401      /*
402      ** not mapped in - most of the RIOPortp[] information
403      ** has not been set up!
404      ** Optimise: ports come in bundles of eight.
405      */
406      if ( !PortP->Mapped ) {
407	port += 7;
408	continue; /* with the next port */
409      }
410
411      /*
412      ** If the host board isn't THIS host board, check the next one.
413      ** optimise: ports come in bundles of eight.
414      */
415      if ( PortP->HostP != HostP ) {
416	port += 7;
417	continue;
418      }
419
420      /*
421      ** Let us see - is the port open? If not, then don't service it.
422      */
423      if ( !( PortP->PortState & PORT_ISOPEN ) ) {
424	continue;
425      }
426
427      /*
428      ** find corresponding tty structure. The process of mapping
429      ** the ports puts these here.
430      */
431      ttyP = PortP->gs.tty;
432
433      /*
434      ** Lock the port before we begin working on it.
435      */
436      rio_spin_lock(&PortP->portSem);
437
438      /*
439      ** Process received data if there is any.
440      */
441      if ( can_remove_receive( &PacketP, PortP ) )
442	RIOReceive(p, PortP);
443
444      /*
445      ** If there is no data left to be read from the port, and
446      ** it's handshake bit is set, then we must clear the handshake,
447      ** so that that downstream RTA is re-enabled.
448      */
449      if ( !can_remove_receive( &PacketP, PortP ) &&
450	   ( RWORD( PortP->PhbP->handshake )==PHB_HANDSHAKE_SET ) ) {
451				/*
452				** MAGIC! ( Basically, handshake the RX buffer, so that
453				** the RTAs upstream can be re-enabled. )
454				*/
455	rio_dprintk (RIO_DEBUG_INTR, "Set RX handshake bit\n");
456	WWORD( PortP->PhbP->handshake,
457	       PHB_HANDSHAKE_SET|PHB_HANDSHAKE_RESET );
458      }
459      rio_spin_unlock(&PortP->portSem);
460    }
461  }
462
463  if ( RWORD( HostP->ParmMapP->tx_intr ) ) {
464    int port;
465
466    WWORD( HostP->ParmMapP->tx_intr , 0);
467
468    p->RIOTxCount++;
469    TxIntr++;
470    rio_dprintk (RIO_DEBUG_INTR, "rio: TX interrupt on host %d\n", HostP-p->RIOHosts);
471
472    /*
473    ** Loop through every port.
474    ** If the port is mapped into the system ( i.e. has /dev/ttyXXXX
475    ** associated ) then it is worth checking.
476    */
477    for ( port=p->RIOFirstPortsBooted;
478	  port<p->RIOLastPortsBooted+PORTS_PER_RTA; port++ ) {
479      struct Port *PortP = p->RIOPortp[port];
480      struct tty_struct *ttyP;
481      struct PKT *PacketP;
482
483      /*
484      ** not mapped in - most of the RIOPortp[] information
485      ** has not been set up!
486      */
487      if ( !PortP->Mapped ) {
488	port += 7;
489	continue; /* with the next port */
490      }
491
492      /*
493      ** If the host board isn't running, then its data structures
494      ** are no use to us - continue quietly.
495      */
496      if ( PortP->HostP != HostP ) {
497	port += 7;
498	continue; /* with the next port */
499      }
500
501      /*
502      ** Let us see - is the port open? If not, then don't service it.
503      */
504      if ( !( PortP->PortState & PORT_ISOPEN ) ) {
505	continue;
506      }
507
508      rio_dprintk (RIO_DEBUG_INTR, "rio: Looking into port %d.\n", port);
509      /*
510      ** Lock the port before we begin working on it.
511      */
512      rio_spin_lock(&PortP->portSem);
513
514      /*
515      ** If we can't add anything to the transmit queue, then
516      ** we need do none of this processing.
517      */
518      if ( !can_add_transmit( &PacketP, PortP ) ) {
519	rio_dprintk (RIO_DEBUG_INTR, "Can't add to port, so skipping.\n");
520	rio_spin_unlock(&PortP->portSem);
521	continue;
522      }
523
524      /*
525      ** find corresponding tty structure. The process of mapping
526      ** the ports puts these here.
527      */
528      ttyP = PortP->gs.tty;
529      /* If ttyP is NULL, the port is getting closed. Forget about it. */
530      if (!ttyP) {
531	rio_dprintk (RIO_DEBUG_INTR, "no tty, so skipping.\n");
532	rio_spin_unlock(&PortP->portSem);
533	continue;
534      }
535      /*
536      ** If there is more room available we start up the transmit
537      ** data process again. This can be direct I/O, if the cookmode
538      ** is set to COOK_RAW or COOK_MEDIUM, or will be a call to the
539      ** riotproc( T_OUTPUT ) if we are in COOK_WELL mode, to fetch
540      ** characters via the line discipline. We must always call
541      ** the line discipline,
542      ** so that user input characters can be echoed correctly.
543      **
544      ** ++++ Update +++++
545      ** With the advent of double buffering, we now see if
546      ** TxBufferOut-In is non-zero. If so, then we copy a packet
547      ** to the output place, and set it going. If this empties
548      ** the buffer, then we must issue a wakeup( ) on OUT.
549      ** If it frees space in the buffer then we must issue
550      ** a wakeup( ) on IN.
551      **
552      ** ++++ Extra! Extra! If PortP->WflushFlag is set, then we
553      ** have to send a WFLUSH command down the PHB, to mark the
554      ** end point of a WFLUSH. We also need to clear out any
555      ** data from the double buffer! ( note that WflushFlag is a
556      ** *count* of the number of WFLUSH commands outstanding! )
557      **
558      ** ++++ And there's more!
559      ** If an RTA is powered off, then on again, and rebooted,
560      ** whilst it has ports open, then we need to re-open the ports.
561      ** ( reasonable enough ). We can't do this when we spot the
562      ** re-boot, in interrupt time, because the queue is probably
563      ** full. So, when we come in here, we need to test if any
564      ** ports are in this condition, and re-open the port before
565      ** we try to send any more data to it. Now, the re-booted
566      ** RTA will be discarding packets from the PHB until it
567      ** receives this open packet, but don't worry tooo much
568      ** about that. The one thing that is interesting is the
569      ** combination of this effect and the WFLUSH effect!
570      */
571      /* For now don't handle RTA reboots. -- REW.
572	 Reenabled. Otherwise RTA reboots didn't work. Duh. -- REW */
573      if ( PortP->MagicFlags ) {
574	if ( PortP->MagicFlags & MAGIC_REBOOT ) {
575	  /*
576	  ** well, the RTA has been rebooted, and there is room
577	  ** on its queue to add the open packet that is required.
578	  **
579	  ** The messy part of this line is trying to decide if
580	  ** we need to call the Param function as a tty or as
581	  ** a modem.
582	  ** DONT USE CLOCAL AS A TEST FOR THIS!
583	  **
584	  ** If we can't param the port, then move on to the
585	  ** next port.
586	  */
587	  PortP->InUse = NOT_INUSE;
588
589	  rio_spin_unlock(&PortP->portSem);
590	  if ( RIOParam(PortP, OPEN, ((PortP->Cor2Copy &
591				       (COR2_RTSFLOW|COR2_CTSFLOW ) )==
592				      (COR2_RTSFLOW|COR2_CTSFLOW ) ) ?
593			TRUE : FALSE, DONT_SLEEP ) == RIO_FAIL ) {
594	    continue; /* with next port */
595	  }
596	  rio_spin_lock(&PortP->portSem);
597	  PortP->MagicFlags &= ~MAGIC_REBOOT;
598	}
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*/
713void
714RIOReceive(p, PortP)
715struct rio_info *	p;
716struct 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*/
894int
895riotproc(p, tp, cmd, port)
896struct rio_info *	p;
897register struct ttystatics *tp;
898int cmd;
899int	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:
999start:
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
1112