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 #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 */
101 void
riopoll(p)102 riopoll(p)
103 struct 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 
firstchars(char * p,int nch)165 char *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 */
178 void
RIOTxEnable(en)179 RIOTxEnable(en)
180 char *		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     tty_wakeup(PortP->gs.tty);
252     rio_dprintk (RIO_DEBUG_INTR, "(%d/%d)\n",
253 		PortP->gs.wakeup_chars, PortP->gs.xmit_cnt);
254   }
255 
256 }
257 
258 
259 /*
260 ** When a real-life interrupt comes in here, we try to find out
261 ** which host card it belongs to, and then service only that host
262 ** Notice the cunning way that, once we've found a candidate, we
263 ** continue just in case we are sharing interrupts.
264 */
265 void
riointr(p)266 riointr(p)
267 struct rio_info *	p;
268 {
269 	int host;
270 
271 	for ( host=0; host<p->RIONumHosts; host++ ) {
272 		struct Host *HostP = &p->RIOHosts[host];
273 
274 		rio_dprintk (RIO_DEBUG_INTR,  "riointr() doing host %d type %d\n", host, HostP->Type);
275 
276 		switch( HostP->Type ) {
277 			case RIO_AT:
278 			case RIO_MCA:
279 			case RIO_PCI:
280 			  	rio_spin_lock(&HostP->HostLock);
281 				WBYTE(HostP->ResetInt , 0xff);
282 				if ( !HostP->InIntr ) {
283 					HostP->InIntr = 1;
284 					rio_spin_unlock (&HostP->HostLock);
285 					p->_RIO_Interrupted++;
286 					RIOServiceHost(p, HostP, 'i');
287 					rio_spin_lock(&HostP->HostLock);
288 					HostP->InIntr = 0;
289 				}
290 				rio_spin_unlock(&HostP->HostLock);
291 				break;
292 #ifdef FUTURE_RELEASE
293 		case RIO_EISA:
294 			if ( ivec == HostP->Ivec )
295 			{
296 				OldSpl = LOCKB( &HostP->HostLock );
297 				INBZ( HostP->Slot, EISA_INTERRUPT_RESET );
298 				if ( !HostP->InIntr )
299 				{
300 					HostP->InIntr = 1;
301 					UNLOCKB( &HostP->HostLock, OldSpl );
302 					if ( this_cpu < RIO_CPU_LIMIT )
303 					{
304 						int intrSpl = LOCKB( &RIOIntrLock );
305 						UNLOCKB( &RIOIntrLock, intrSpl );
306 					}
307 						p->_RIO_Interrupted++;
308 					RIOServiceHost( HostP, 'i' );
309 					OldSpl = LOCKB( &HostP->HostLock );
310 					HostP->InIntr = 0;
311 				}
312 				UNLOCKB( &HostP->HostLock, OldSpl );
313 				done++;
314 			}
315 			break;
316 #endif
317 		}
318 
319 		HostP->IntSrvDone++;
320 	}
321 
322 #ifdef FUTURE_RELEASE
323 	if ( !done )
324 	{
325 		cmn_err( CE_WARN, "RIO: Interrupt received with vector 0x%x\n", ivec );
326 		cmn_err( CE_CONT, "	 Valid vectors are:\n");
327 		for ( host=0; host<RIONumHosts; host++ )
328 		{
329 			switch( RIOHosts[host].Type )
330 			{
331 				case RIO_AT:
332 				case RIO_MCA:
333 				case RIO_EISA:
334 						cmn_err( CE_CONT, "0x%x ", RIOHosts[host].Ivec );
335 						break;
336 				case RIO_PCI:
337 						cmn_err( CE_CONT, "0x%x ", get_intr_arg( RIOHosts[host].PciDevInfo.busnum, IDIST_PCI_IRQ( RIOHosts[host].PciDevInfo.slotnum, RIOHosts[host].PciDevInfo.funcnum ) ));
338 						break;
339 			}
340 		}
341 		cmn_err( CE_CONT, "\n" );
342 	}
343 #endif
344 }
345 
346 /*
347 ** RIO Host Service routine. Does all the work traditionally associated with an
348 ** interrupt.
349 */
350 static int	RupIntr;
351 static int	RxIntr;
352 static int	TxIntr;
353 void
RIOServiceHost(p,HostP,From)354 RIOServiceHost(p, HostP, From)
355 struct rio_info *	p;
356 struct Host *HostP;
357 int From;
358 {
359   rio_spin_lock (&HostP->HostLock);
360   if ( (HostP->Flags & RUN_STATE) != RC_RUNNING ) {
361     static int t =0;
362     rio_spin_unlock (&HostP->HostLock);
363     if ((t++ % 200) == 0)
364       rio_dprintk (RIO_DEBUG_INTR, "Interrupt but host not running. flags=%x.\n", (int)HostP->Flags);
365     return;
366   }
367   rio_spin_unlock (&HostP->HostLock);
368 
369   if ( RWORD( HostP->ParmMapP->rup_intr ) ) {
370     WWORD( HostP->ParmMapP->rup_intr , 0 );
371     p->RIORupCount++;
372     RupIntr++;
373     rio_dprintk (RIO_DEBUG_INTR, "rio: RUP interrupt on host %d\n", HostP-p->RIOHosts);
374     RIOPollHostCommands(p, HostP );
375   }
376 
377   if ( RWORD( HostP->ParmMapP->rx_intr ) ) {
378     int port;
379 
380     WWORD( HostP->ParmMapP->rx_intr , 0 );
381     p->RIORxCount++;
382     RxIntr++;
383 
384     rio_dprintk (RIO_DEBUG_INTR, "rio: RX interrupt on host %d\n", HostP-p->RIOHosts);
385     /*
386     ** Loop through every port. If the port is mapped into
387     ** the system ( i.e. has /dev/ttyXXXX associated ) then it is
388     ** worth checking. If the port isn't open, grab any packets
389     ** hanging on its receive queue and stuff them on the free
390     ** list; check for commands on the way.
391     */
392     for ( port=p->RIOFirstPortsBooted;
393 	  port<p->RIOLastPortsBooted+PORTS_PER_RTA; port++ ) {
394       struct Port *PortP = p->RIOPortp[port];
395       struct tty_struct *ttyP;
396       struct PKT *PacketP;
397 
398       /*
399       ** not mapped in - most of the RIOPortp[] information
400       ** has not been set up!
401       ** Optimise: ports come in bundles of eight.
402       */
403       if ( !PortP->Mapped ) {
404 	port += 7;
405 	continue; /* with the next port */
406       }
407 
408       /*
409       ** If the host board isn't THIS host board, check the next one.
410       ** optimise: ports come in bundles of eight.
411       */
412       if ( PortP->HostP != HostP ) {
413 	port += 7;
414 	continue;
415       }
416 
417       /*
418       ** Let us see - is the port open? If not, then don't service it.
419       */
420       if ( !( PortP->PortState & PORT_ISOPEN ) ) {
421 	continue;
422       }
423 
424       /*
425       ** find corresponding tty structure. The process of mapping
426       ** the ports puts these here.
427       */
428       ttyP = PortP->gs.tty;
429 
430       /*
431       ** Lock the port before we begin working on it.
432       */
433       rio_spin_lock(&PortP->portSem);
434 
435       /*
436       ** Process received data if there is any.
437       */
438       if ( can_remove_receive( &PacketP, PortP ) )
439 	RIOReceive(p, PortP);
440 
441       /*
442       ** If there is no data left to be read from the port, and
443       ** it's handshake bit is set, then we must clear the handshake,
444       ** so that that downstream RTA is re-enabled.
445       */
446       if ( !can_remove_receive( &PacketP, PortP ) &&
447 	   ( RWORD( PortP->PhbP->handshake )==PHB_HANDSHAKE_SET ) ) {
448 				/*
449 				** MAGIC! ( Basically, handshake the RX buffer, so that
450 				** the RTAs upstream can be re-enabled. )
451 				*/
452 	rio_dprintk (RIO_DEBUG_INTR, "Set RX handshake bit\n");
453 	WWORD( PortP->PhbP->handshake,
454 	       PHB_HANDSHAKE_SET|PHB_HANDSHAKE_RESET );
455       }
456       rio_spin_unlock(&PortP->portSem);
457     }
458   }
459 
460   if ( RWORD( HostP->ParmMapP->tx_intr ) ) {
461     int port;
462 
463     WWORD( HostP->ParmMapP->tx_intr , 0);
464 
465     p->RIOTxCount++;
466     TxIntr++;
467     rio_dprintk (RIO_DEBUG_INTR, "rio: TX interrupt on host %d\n", HostP-p->RIOHosts);
468 
469     /*
470     ** Loop through every port.
471     ** If the port is mapped into the system ( i.e. has /dev/ttyXXXX
472     ** associated ) then it is worth checking.
473     */
474     for ( port=p->RIOFirstPortsBooted;
475 	  port<p->RIOLastPortsBooted+PORTS_PER_RTA; port++ ) {
476       struct Port *PortP = p->RIOPortp[port];
477       struct tty_struct *ttyP;
478       struct PKT *PacketP;
479 
480       /*
481       ** not mapped in - most of the RIOPortp[] information
482       ** has not been set up!
483       */
484       if ( !PortP->Mapped ) {
485 	port += 7;
486 	continue; /* with the next port */
487       }
488 
489       /*
490       ** If the host board isn't running, then its data structures
491       ** are no use to us - continue quietly.
492       */
493       if ( PortP->HostP != HostP ) {
494 	port += 7;
495 	continue; /* with the next port */
496       }
497 
498       /*
499       ** Let us see - is the port open? If not, then don't service it.
500       */
501       if ( !( PortP->PortState & PORT_ISOPEN ) ) {
502 	continue;
503       }
504 
505       rio_dprintk (RIO_DEBUG_INTR, "rio: Looking into port %d.\n", port);
506       /*
507       ** Lock the port before we begin working on it.
508       */
509       rio_spin_lock(&PortP->portSem);
510 
511       /*
512       ** If we can't add anything to the transmit queue, then
513       ** we need do none of this processing.
514       */
515       if ( !can_add_transmit( &PacketP, PortP ) ) {
516 	rio_dprintk (RIO_DEBUG_INTR, "Can't add to port, so skipping.\n");
517 	rio_spin_unlock(&PortP->portSem);
518 	continue;
519       }
520 
521       /*
522       ** find corresponding tty structure. The process of mapping
523       ** the ports puts these here.
524       */
525       ttyP = PortP->gs.tty;
526       /* If ttyP is NULL, the port is getting closed. Forget about it. */
527       if (!ttyP) {
528 	rio_dprintk (RIO_DEBUG_INTR, "no tty, so skipping.\n");
529 	rio_spin_unlock(&PortP->portSem);
530 	continue;
531       }
532       /*
533       ** If there is more room available we start up the transmit
534       ** data process again. This can be direct I/O, if the cookmode
535       ** is set to COOK_RAW or COOK_MEDIUM, or will be a call to the
536       ** riotproc( T_OUTPUT ) if we are in COOK_WELL mode, to fetch
537       ** characters via the line discipline. We must always call
538       ** the line discipline,
539       ** so that user input characters can be echoed correctly.
540       **
541       ** ++++ Update +++++
542       ** With the advent of double buffering, we now see if
543       ** TxBufferOut-In is non-zero. If so, then we copy a packet
544       ** to the output place, and set it going. If this empties
545       ** the buffer, then we must issue a wakeup( ) on OUT.
546       ** If it frees space in the buffer then we must issue
547       ** a wakeup( ) on IN.
548       **
549       ** ++++ Extra! Extra! If PortP->WflushFlag is set, then we
550       ** have to send a WFLUSH command down the PHB, to mark the
551       ** end point of a WFLUSH. We also need to clear out any
552       ** data from the double buffer! ( note that WflushFlag is a
553       ** *count* of the number of WFLUSH commands outstanding! )
554       **
555       ** ++++ And there's more!
556       ** If an RTA is powered off, then on again, and rebooted,
557       ** whilst it has ports open, then we need to re-open the ports.
558       ** ( reasonable enough ). We can't do this when we spot the
559       ** re-boot, in interrupt time, because the queue is probably
560       ** full. So, when we come in here, we need to test if any
561       ** ports are in this condition, and re-open the port before
562       ** we try to send any more data to it. Now, the re-booted
563       ** RTA will be discarding packets from the PHB until it
564       ** receives this open packet, but don't worry tooo much
565       ** about that. The one thing that is interesting is the
566       ** combination of this effect and the WFLUSH effect!
567       */
568       /* For now don't handle RTA reboots. -- REW.
569 	 Reenabled. Otherwise RTA reboots didn't work. Duh. -- REW */
570       if ( PortP->MagicFlags ) {
571 #if 1
572 	if ( PortP->MagicFlags & MAGIC_REBOOT ) {
573 	  /*
574 	  ** well, the RTA has been rebooted, and there is room
575 	  ** on its queue to add the open packet that is required.
576 	  **
577 	  ** The messy part of this line is trying to decide if
578 	  ** we need to call the Param function as a tty or as
579 	  ** a modem.
580 	  ** DONT USE CLOCAL AS A TEST FOR THIS!
581 	  **
582 	  ** If we can't param the port, then move on to the
583 	  ** next port.
584 	  */
585 	  PortP->InUse = NOT_INUSE;
586 
587 	  rio_spin_unlock(&PortP->portSem);
588 	  if ( RIOParam(PortP, OPEN, ((PortP->Cor2Copy &
589 				       (COR2_RTSFLOW|COR2_CTSFLOW ) )==
590 				      (COR2_RTSFLOW|COR2_CTSFLOW ) ) ?
591 			TRUE : FALSE, DONT_SLEEP ) == RIO_FAIL ) {
592 	    continue; /* with next port */
593 	  }
594 	  rio_spin_lock(&PortP->portSem);
595 	  PortP->MagicFlags &= ~MAGIC_REBOOT;
596 	}
597 #endif
598 
599 	/*
600 	** As mentioned above, this is a tacky hack to cope
601 	** with WFLUSH
602 	*/
603 	if ( PortP->WflushFlag ) {
604 	  rio_dprintk (RIO_DEBUG_INTR, "Want to WFLUSH mark this port\n");
605 
606 	  if ( PortP->InUse )
607 	    rio_dprintk (RIO_DEBUG_INTR, "FAILS - PORT IS IN USE\n");
608 	}
609 
610 	while ( PortP->WflushFlag &&
611 		can_add_transmit( &PacketP, PortP ) &&
612 		( PortP->InUse == NOT_INUSE ) ) {
613 	  int p;
614 	  struct PktCmd *PktCmdP;
615 
616 	  rio_dprintk (RIO_DEBUG_INTR, "Add WFLUSH marker to data queue\n");
617 	  /*
618 	  ** make it look just like a WFLUSH command
619 	  */
620 	  PktCmdP = ( struct PktCmd * )&PacketP->data[0];
621 
622 	  WBYTE( PktCmdP->Command , WFLUSH );
623 
624 	  p =  PortP->HostPort % ( ushort )PORTS_PER_RTA;
625 
626 	  /*
627 	  ** If second block of ports for 16 port RTA, add 8
628 	  ** to index 8-15.
629 	  */
630 	  if ( PortP->SecondBlock )
631 	    p += PORTS_PER_RTA;
632 
633 	  WBYTE( PktCmdP->PhbNum, p );
634 
635 	  /*
636 	  ** to make debuggery easier
637 	  */
638 	  WBYTE( PacketP->data[ 2], 'W'  );
639 	  WBYTE( PacketP->data[ 3], 'F'  );
640 	  WBYTE( PacketP->data[ 4], 'L'  );
641 	  WBYTE( PacketP->data[ 5], 'U'  );
642 	  WBYTE( PacketP->data[ 6], 'S'  );
643 	  WBYTE( PacketP->data[ 7], 'H'  );
644 	  WBYTE( PacketP->data[ 8], ' '  );
645 	  WBYTE( PacketP->data[ 9], '0'+PortP->WflushFlag );
646 	  WBYTE( PacketP->data[10], ' '  );
647 	  WBYTE( PacketP->data[11], ' '  );
648 	  WBYTE( PacketP->data[12], '\0' );
649 
650 	  /*
651 	  ** its two bytes long!
652 	  */
653 	  WBYTE( PacketP->len , PKT_CMD_BIT | 2 );
654 
655 	  /*
656 	  ** queue it!
657 	  */
658 	  if ( !( PortP->State & RIO_DELETED ) ) {
659 	    add_transmit( PortP );
660 	    /*
661 	    ** Count chars tx'd for port statistics reporting
662 	    */
663 	    if ( PortP->statsGather )
664 	      PortP->txchars += 2;
665 	  }
666 
667 	  if ( --( PortP->WflushFlag ) == 0 ) {
668 	    PortP->MagicFlags &= ~MAGIC_FLUSH;
669 	  }
670 
671 	  rio_dprintk (RIO_DEBUG_INTR, "Wflush count now stands at %d\n",
672 		 PortP->WflushFlag);
673 	}
674 	if ( PortP->MagicFlags & MORE_OUTPUT_EYGOR ) {
675 	  if ( PortP->MagicFlags & MAGIC_FLUSH ) {
676 	    PortP->MagicFlags |= MORE_OUTPUT_EYGOR;
677 	  }
678 	  else {
679 	    if ( !can_add_transmit( &PacketP, PortP ) ) {
680 	      rio_spin_unlock(&PortP->portSem);
681 	      continue;
682 	    }
683 	    rio_spin_unlock(&PortP->portSem);
684 	    RIOTxEnable((char *)PortP);
685 	    rio_spin_lock(&PortP->portSem);
686 	    PortP->MagicFlags &= ~MORE_OUTPUT_EYGOR;
687 	  }
688 	}
689       }
690 
691 
692       /*
693       ** If we can't add anything to the transmit queue, then
694       ** we need do none of the remaining processing.
695       */
696       if (!can_add_transmit( &PacketP, PortP ) ) {
697 	rio_spin_unlock(&PortP->portSem);
698 	continue;
699       }
700 
701       rio_spin_unlock(&PortP->portSem);
702       RIOTxEnable((char *)PortP);
703     }
704   }
705 }
706 
707 /*
708 ** Routine for handling received data for clist drivers.
709 ** NB: Called with the tty locked. The spl from the lockb( ) is passed.
710 ** we return the ttySpl level that we re-locked at.
711 */
712 void
RIOReceive(p,PortP)713 RIOReceive(p, PortP)
714 struct rio_info *	p;
715 struct Port *		PortP;
716 {
717   struct tty_struct *TtyP;
718   register ushort transCount;
719   struct PKT *PacketP;
720   register uint	DataCnt;
721   uchar *	ptr;
722   int copied =0;
723 
724   static int intCount, RxIntCnt;
725 
726   /*
727   ** The receive data process is to remove packets from the
728   ** PHB until there aren't any more or the current cblock
729   ** is full. When this occurs, there will be some left over
730   ** data in the packet, that we must do something with.
731   ** As we haven't unhooked the packet from the read list
732   ** yet, we can just leave the packet there, having first
733   ** made a note of how far we got. This means that we need
734   ** a pointer per port saying where we start taking the
735   ** data from - this will normally be zero, but when we
736   ** run out of space it will be set to the offset of the
737   ** next byte to copy from the packet data area. The packet
738   ** length field is decremented by the number of bytes that
739   ** we succesfully removed from the packet. When this reaches
740   ** zero, we reset the offset pointer to be zero, and free
741   ** the packet from the front of the queue.
742   */
743 
744   intCount++;
745 
746   TtyP = PortP->gs.tty;
747   if (!TtyP) {
748     rio_dprintk (RIO_DEBUG_INTR, "RIOReceive: tty is null. \n");
749     return;
750   }
751 
752   if (PortP->State & RIO_THROTTLE_RX) {
753     rio_dprintk (RIO_DEBUG_INTR, "RIOReceive: Throttled. Can't handle more input.\n");
754     return;
755   }
756 
757   if ( PortP->State & RIO_DELETED )
758     {
759       while ( can_remove_receive( &PacketP, PortP ) )
760 	{
761 	  remove_receive( PortP );
762 	  put_free_end( PortP->HostP, PacketP );
763 	}
764     }
765   else
766     {
767       /*
768       ** loop, just so long as:
769       **   i ) there's some data ( i.e. can_remove_receive )
770       **  ii ) we haven't been blocked
771       ** iii ) there's somewhere to put the data
772       **  iv ) we haven't outstayed our welcome
773       */
774       transCount = 1;
775       while ( can_remove_receive(&PacketP, PortP)
776 	      && transCount)
777 	{
778 #ifdef STATS
779 	  PortP->Stat.RxIntCnt++;
780 #endif /* STATS */
781 	  RxIntCnt++;
782 
783 	  /*
784 	  ** check that it is not a command!
785 	  */
786 	  if ( PacketP->len & PKT_CMD_BIT ) {
787 	    rio_dprintk (RIO_DEBUG_INTR, "RIO: unexpected command packet received on PHB\n");
788 	    /*	    rio_dprint(RIO_DEBUG_INTR, (" sysport   = %d\n", p->RIOPortp->PortNum)); */
789 	    rio_dprintk (RIO_DEBUG_INTR, " dest_unit = %d\n", PacketP->dest_unit);
790 	    rio_dprintk (RIO_DEBUG_INTR, " dest_port = %d\n", PacketP->dest_port);
791 	    rio_dprintk (RIO_DEBUG_INTR, " src_unit  = %d\n", PacketP->src_unit);
792 	    rio_dprintk (RIO_DEBUG_INTR, " src_port  = %d\n", PacketP->src_port);
793 	    rio_dprintk (RIO_DEBUG_INTR, " len	   = %d\n", PacketP->len);
794 	    rio_dprintk (RIO_DEBUG_INTR, " control   = %d\n", PacketP->control);
795 	    rio_dprintk (RIO_DEBUG_INTR, " csum	   = %d\n", PacketP->csum);
796 	    rio_dprintk (RIO_DEBUG_INTR, "	 data bytes: ");
797 	    for ( DataCnt=0; DataCnt<PKT_MAX_DATA_LEN; DataCnt++ )
798 	      rio_dprintk (RIO_DEBUG_INTR, "%d\n", PacketP->data[DataCnt]);
799 	    remove_receive( PortP );
800 	    put_free_end( PortP->HostP, PacketP );
801 	    continue; /* with next packet */
802 	  }
803 
804 	  /*
805 	  ** How many characters can we move 'upstream' ?
806 	  **
807 	  ** Determine the minimum of the amount of data
808 	  ** available and the amount of space in which to
809 	  ** put it.
810 	  **
811 	  ** 1.	Get the packet length by masking 'len'
812 	  **	for only the length bits.
813 	  ** 2.	Available space is [buffer size] - [space used]
814 	  **
815 	  ** Transfer count is the minimum of packet length
816 	  ** and available space.
817 	  */
818 
819 	  transCount = min_t(unsigned int, PacketP->len & PKT_LEN_MASK,
820 			   TTY_FLIPBUF_SIZE - TtyP->flip.count);
821 	  rio_dprintk (RIO_DEBUG_REC,  "port %d: Copy %d bytes\n",
822 				      PortP->PortNum, transCount);
823 	  /*
824 	  ** To use the following 'kkprintfs' for debugging - change the '#undef'
825 	  ** to '#define', (this is the only place ___DEBUG_IT___ occurs in the
826 	  ** driver).
827 	  */
828 #undef ___DEBUG_IT___
829 #ifdef ___DEBUG_IT___
830 	  kkprintf("I:%d R:%d P:%d Q:%d C:%d F:%x ",
831 		   intCount,
832 		   RxIntCnt,
833 		   PortP->PortNum,
834 		   TtyP->rxqueue.count,
835 		   transCount,
836 		   TtyP->flags );
837 #endif
838 	  ptr = (uchar *) PacketP->data + PortP->RxDataStart;
839 
840 	  rio_memcpy_fromio (TtyP->flip.char_buf_ptr, ptr, transCount);
841 	  memset(TtyP->flip.flag_buf_ptr, TTY_NORMAL, transCount);
842 
843 #ifdef STATS
844 	  /*
845 	  ** keep a count for statistical purposes
846 	  */
847 	  PortP->Stat.RxCharCnt	+= transCount;
848 #endif
849 	  PortP->RxDataStart	+= transCount;
850 	  PacketP->len		-= transCount;
851 	  copied += transCount;
852 	  TtyP->flip.count += transCount;
853 	  TtyP->flip.char_buf_ptr += transCount;
854 	  TtyP->flip.flag_buf_ptr += transCount;
855 
856 
857 #ifdef ___DEBUG_IT___
858 	  kkprintf("T:%d L:%d\n", DataCnt, PacketP->len );
859 #endif
860 
861 	  if ( PacketP->len == 0 )
862 	    {
863 				/*
864 				** If we have emptied the packet, then we can
865 				** free it, and reset the start pointer for
866 				** the next packet.
867 				*/
868 	      remove_receive( PortP );
869 	      put_free_end( PortP->HostP, PacketP );
870 	      PortP->RxDataStart = 0;
871 #ifdef STATS
872 				/*
873 				** more lies ( oops, I mean statistics )
874 				*/
875 	      PortP->Stat.RxPktCnt++;
876 #endif /* STATS */
877 	    }
878 	}
879     }
880   if (copied) {
881     rio_dprintk (RIO_DEBUG_REC, "port %d: pushing tty flip buffer: %d total bytes copied.\n", PortP->PortNum, copied);
882     tty_flip_buffer_push (TtyP);
883   }
884 
885   return;
886 }
887 
888 #ifdef FUTURE_RELEASE
889 /*
890 ** The proc routine called by the line discipline to do the work for it.
891 ** The proc routine works hand in hand with the interrupt routine.
892 */
893 int
riotproc(p,tp,cmd,port)894 riotproc(p, tp, cmd, port)
895 struct rio_info *	p;
896 register struct ttystatics *tp;
897 int cmd;
898 int	port;
899 {
900 	register struct Port *PortP;
901 	int SysPort;
902 	struct PKT *PacketP;
903 
904 	SysPort = port;	/* Believe me, it works. */
905 
906 	if ( SysPort < 0 || SysPort >= RIO_PORTS ) {
907 		rio_dprintk (RIO_DEBUG_INTR, "Illegal port %d derived from TTY in riotproc()\n",SysPort);
908 		return 0;
909 	}
910 	PortP = p->RIOPortp[SysPort];
911 
912 	if ((uint)PortP->PhbP < (uint)PortP->Caddr ||
913 			(uint)PortP->PhbP >= (uint)PortP->Caddr+SIXTY_FOUR_K ) {
914 		rio_dprintk (RIO_DEBUG_INTR, "RIO: NULL or BAD PhbP on sys port %d in proc routine\n",
915 							SysPort);
916 		rio_dprintk (RIO_DEBUG_INTR, "	 PortP = 0x%x\n",PortP);
917 		rio_dprintk (RIO_DEBUG_INTR, "	 PortP->PhbP = 0x%x\n",PortP->PhbP);
918 		rio_dprintk (RIO_DEBUG_INTR, "	 PortP->Caddr = 0x%x\n",PortP->PhbP);
919 		rio_dprintk (RIO_DEBUG_INTR, "	 PortP->HostPort = 0x%x\n",PortP->HostPort);
920 		return 0;
921 	}
922 
923 	switch(cmd) {
924 		case T_WFLUSH:
925 			rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH\n");
926 			/*
927 			** Because of the spooky way the RIO works, we don't need
928 			** to issue a flush command on any of the SET*F commands,
929 			** as that causes trouble with getty and login, which issue
930 			** these commands to incur a READ flush, and rely on the fact
931 			** that the line discipline does a wait for drain for them.
932 			** As the rio doesn't wait for drain, the write flush would
933 			** destroy the Password: prompt. This isn't very friendly, so
934 			** here we only issue a WFLUSH command if we are in the interrupt
935 			** routine, or we aren't executing a SET*F command.
936 			*/
937 			if ( PortP->HostP->InIntr || !PortP->FlushCmdBodge ) {
938 				/*
939 				** form a wflush packet - 1 byte long, no data
940 				*/
941 				if ( PortP->State & RIO_DELETED ) {
942 					rio_dprintk (RIO_DEBUG_INTR, "WFLUSH on deleted RTA\n");
943 				}
944 				else {
945 					if ( RIOPreemptiveCmd(p, PortP, WFLUSH ) == RIO_FAIL ) {
946 						rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH Command failed\n");
947 					}
948 					else
949 						rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH Command\n");
950 				}
951 				/*
952 				** WFLUSH operation - flush the data!
953 				*/
954 				PortP->TxBufferIn = PortP->TxBufferOut = 0;
955 			}
956 			else {
957 				rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH Command ignored\n");
958 			}
959 			/*
960 			** sort out the line discipline
961 			*/
962 			if (PortP->CookMode == COOK_WELL)
963 				goto start;
964 			break;
965 
966 		case T_RESUME:
967 			rio_dprintk (RIO_DEBUG_INTR, "T_RESUME\n");
968 			/*
969 			** send pre-emptive resume packet
970 			*/
971 			if ( PortP->State & RIO_DELETED ) {
972 				rio_dprintk (RIO_DEBUG_INTR, "RESUME on deleted RTA\n");
973 			}
974 			else {
975 				if ( RIOPreemptiveCmd(p, PortP, RESUME ) == RIO_FAIL ) {
976 					rio_dprintk (RIO_DEBUG_INTR, "T_RESUME Command failed\n");
977 				}
978 			}
979 			/*
980 			** and re-start the sender software!
981 			*/
982 			if (PortP->CookMode == COOK_WELL)
983 				goto start;
984 			break;
985 
986 		case T_TIME:
987 			rio_dprintk (RIO_DEBUG_INTR, "T_TIME\n");
988 			/*
989 			** T_TIME is called when xDLY is set in oflags and
990 			** the line discipline timeout has expired. It's
991 			** function in life is to clear the TIMEOUT flag
992 			** and to re-start output to the port.
993 			*/
994 			/*
995 			** Fall through and re-start output
996 			*/
997 		case T_OUTPUT:
998 start:
999 			if ( PortP->MagicFlags & MAGIC_FLUSH ) {
1000 				PortP->MagicFlags |= MORE_OUTPUT_EYGOR;
1001 				return 0;
1002 			}
1003 			RIOTxEnable((char *)PortP);
1004 			PortP->MagicFlags &= ~MORE_OUTPUT_EYGOR;
1005 			/*rio_dprint(RIO_DEBUG_INTR, PortP,DBG_PROC,"T_OUTPUT finished\n");*/
1006 			break;
1007 
1008 		case T_SUSPEND:
1009 			rio_dprintk (RIO_DEBUG_INTR, "T_SUSPEND\n");
1010 			/*
1011 			** send a suspend pre-emptive packet.
1012 			*/
1013 			if ( PortP->State & RIO_DELETED ) {
1014 				rio_dprintk (RIO_DEBUG_INTR, "SUSPEND deleted RTA\n");
1015 			}
1016 			else {
1017 				if ( RIOPreemptiveCmd(p, PortP, SUSPEND ) == RIO_FAIL ) {
1018 					rio_dprintk (RIO_DEBUG_INTR, "T_SUSPEND Command failed\n");
1019 				}
1020 			}
1021 			/*
1022 			** done!
1023 			*/
1024 			break;
1025 
1026 		case T_BLOCK:
1027 			rio_dprintk (RIO_DEBUG_INTR, "T_BLOCK\n");
1028 			break;
1029 
1030 		case T_RFLUSH:
1031 			rio_dprintk (RIO_DEBUG_INTR, "T_RFLUSH\n");
1032 			if ( PortP->State & RIO_DELETED ) {
1033 				rio_dprintk (RIO_DEBUG_INTR, "RFLUSH on deleted RTA\n");
1034 				PortP->RxDataStart = 0;
1035 			}
1036 			else {
1037 				if ( RIOPreemptiveCmd( p, PortP, RFLUSH ) == RIO_FAIL ) {
1038 					rio_dprintk (RIO_DEBUG_INTR, "T_RFLUSH Command failed\n");
1039 					return 0;
1040 				}
1041 				PortP->RxDataStart = 0;
1042 				while ( can_remove_receive(&PacketP, PortP) ) {
1043 					remove_receive(PortP);
1044 					ShowPacket(DBG_PROC, PacketP );
1045 					put_free_end(PortP->HostP, PacketP );
1046 				}
1047 				if ( PortP->PhbP->handshake == PHB_HANDSHAKE_SET ) {
1048 					/*
1049 					** MAGIC!
1050 					*/
1051 					rio_dprintk (RIO_DEBUG_INTR, "Set receive handshake bit\n");
1052 					PortP->PhbP->handshake |= PHB_HANDSHAKE_RESET;
1053 				}
1054 			}
1055 			break;
1056 			/* FALLTHROUGH */
1057 		case T_UNBLOCK:
1058 			rio_dprintk (RIO_DEBUG_INTR, "T_UNBLOCK\n");
1059 			/*
1060 			** If there is any data to receive set a timeout to service it.
1061 			*/
1062 			RIOReceive(p, PortP);
1063 			break;
1064 
1065 		case T_BREAK:
1066 			rio_dprintk (RIO_DEBUG_INTR, "T_BREAK\n");
1067 			/*
1068 			** Send a break command. For Sys V
1069 			** this is a timed break, so we
1070 			** send a SBREAK[time] packet
1071 			*/
1072 			/*
1073 			** Build a BREAK command
1074 			*/
1075 			if ( PortP->State & RIO_DELETED ) {
1076 				rio_dprintk (RIO_DEBUG_INTR, "BREAK on deleted RTA\n");
1077 			}
1078 			else {
1079 				if (RIOShortCommand(PortP,SBREAK,2,
1080 								p->RIOConf.BreakInterval)==RIO_FAIL) {
1081 			   		rio_dprintk (RIO_DEBUG_INTR, "SBREAK RIOShortCommand failed\n");
1082 				}
1083 			}
1084 
1085 			/*
1086 			** done!
1087 			*/
1088 			break;
1089 
1090 		case T_INPUT:
1091 			rio_dprintk (RIO_DEBUG_INTR, "Proc T_INPUT called - I don't know what to do!\n");
1092 			break;
1093 		case T_PARM:
1094 			rio_dprintk (RIO_DEBUG_INTR, "Proc T_PARM called - I don't know what to do!\n");
1095 			break;
1096 
1097 		case T_SWTCH:
1098 			rio_dprintk (RIO_DEBUG_INTR, "Proc T_SWTCH called - I don't know what to do!\n");
1099 			break;
1100 
1101 		default:
1102 			rio_dprintk (RIO_DEBUG_INTR, "Proc UNKNOWN command %d\n",cmd);
1103 	}
1104 	/*
1105 	** T_OUTPUT returns without passing through this point!
1106 	*/
1107 	/*rio_dprint(RIO_DEBUG_INTR, PortP,DBG_PROC,"riotproc done\n");*/
1108 	return(0);
1109 }
1110 #endif
1111