[PATCH] freepgt: remove MM_VM_SIZE(mm)
[linux-2.6/kvm.git] / drivers / char / rio / riocmd.c
blob533085ec6f1bb3fb05aff76f35cb39d74b8d5140
1 /*
2 ** -----------------------------------------------------------------------------
3 **
4 ** Perle Specialix driver for Linux
5 ** ported from the existing SCO driver source
6 **
8 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 ** Module : riocmd.c
25 ** SID : 1.2
26 ** Last Modified : 11/6/98 10:33:41
27 ** Retrieved : 11/6/98 10:33:49
29 ** ident @(#)riocmd.c 1.2
31 ** -----------------------------------------------------------------------------
33 #ifdef SCCS_LABELS
34 static char *_riocmd_c_sccs_ = "@(#)riocmd.c 1.2";
35 #endif
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>
46 #include <linux/termios.h>
47 #include <linux/serial.h>
49 #include <linux/generic_serial.h>
51 #include "linux_compat.h"
52 #include "rio_linux.h"
53 #include "typdef.h"
54 #include "pkt.h"
55 #include "daemon.h"
56 #include "rio.h"
57 #include "riospace.h"
58 #include "top.h"
59 #include "cmdpkt.h"
60 #include "map.h"
61 #include "riotypes.h"
62 #include "rup.h"
63 #include "port.h"
64 #include "riodrvr.h"
65 #include "rioinfo.h"
66 #include "func.h"
67 #include "errors.h"
68 #include "pci.h"
70 #include "parmmap.h"
71 #include "unixrup.h"
72 #include "board.h"
73 #include "host.h"
74 #include "error.h"
75 #include "phb.h"
76 #include "link.h"
77 #include "cmdblk.h"
78 #include "route.h"
79 #include "control.h"
80 #include "cirrus.h"
83 static struct IdentifyRta IdRta;
84 static struct KillNeighbour KillUnit;
86 int
87 RIOFoadRta(struct Host *HostP, struct Map *MapP)
89 struct CmdBlk *CmdBlkP;
91 rio_dprintk (RIO_DEBUG_CMD, "FOAD RTA\n");
93 CmdBlkP = RIOGetCmdBlk();
95 if ( !CmdBlkP ) {
96 rio_dprintk (RIO_DEBUG_CMD, "FOAD RTA: GetCmdBlk failed\n");
97 return -ENXIO;
100 CmdBlkP->Packet.dest_unit = MapP->ID;
101 CmdBlkP->Packet.dest_port = BOOT_RUP;
102 CmdBlkP->Packet.src_unit = 0;
103 CmdBlkP->Packet.src_port = BOOT_RUP;
104 CmdBlkP->Packet.len = 0x84;
105 CmdBlkP->Packet.data[0] = IFOAD;
106 CmdBlkP->Packet.data[1] = 0;
107 CmdBlkP->Packet.data[2] = IFOAD_MAGIC & 0xFF;
108 CmdBlkP->Packet.data[3] = (IFOAD_MAGIC >> 8) & 0xFF;
110 if ( RIOQueueCmdBlk( HostP, MapP->ID-1, CmdBlkP) == RIO_FAIL ) {
111 rio_dprintk (RIO_DEBUG_CMD, "FOAD RTA: Failed to queue foad command\n");
112 return -EIO;
114 return 0;
118 RIOZombieRta(struct Host *HostP, struct Map *MapP)
120 struct CmdBlk *CmdBlkP;
122 rio_dprintk (RIO_DEBUG_CMD, "ZOMBIE RTA\n");
124 CmdBlkP = RIOGetCmdBlk();
126 if ( !CmdBlkP ) {
127 rio_dprintk (RIO_DEBUG_CMD, "ZOMBIE RTA: GetCmdBlk failed\n");
128 return -ENXIO;
131 CmdBlkP->Packet.dest_unit = MapP->ID;
132 CmdBlkP->Packet.dest_port = BOOT_RUP;
133 CmdBlkP->Packet.src_unit = 0;
134 CmdBlkP->Packet.src_port = BOOT_RUP;
135 CmdBlkP->Packet.len = 0x84;
136 CmdBlkP->Packet.data[0] = ZOMBIE;
137 CmdBlkP->Packet.data[1] = 0;
138 CmdBlkP->Packet.data[2] = ZOMBIE_MAGIC & 0xFF;
139 CmdBlkP->Packet.data[3] = (ZOMBIE_MAGIC >> 8) & 0xFF;
141 if ( RIOQueueCmdBlk( HostP, MapP->ID-1, CmdBlkP) == RIO_FAIL ) {
142 rio_dprintk (RIO_DEBUG_CMD, "ZOMBIE RTA: Failed to queue zombie command\n");
143 return -EIO;
145 return 0;
149 RIOCommandRta(struct rio_info *p, uint RtaUnique,
150 int (* func)(struct Host *HostP, struct Map *MapP))
152 uint Host;
154 rio_dprintk (RIO_DEBUG_CMD, "Command RTA 0x%x func 0x%x\n", RtaUnique, (int)func);
156 if ( !RtaUnique )
157 return(0);
159 for ( Host = 0; Host < p->RIONumHosts; Host++ ) {
160 uint Rta;
161 struct Host *HostP = &p->RIOHosts[Host];
163 for ( Rta = 0; Rta < RTAS_PER_HOST; Rta++ ) {
164 struct Map *MapP = &HostP->Mapping[Rta];
166 if ( MapP->RtaUniqueNum == RtaUnique ) {
167 uint Link;
170 ** now, lets just check we have a route to it...
171 ** IF the routing stuff is working, then one of the
172 ** topology entries for this unit will have a legit
173 ** route *somewhere*. We care not where - if its got
174 ** any connections, we can get to it.
176 for ( Link = 0; Link < LINKS_PER_UNIT; Link++ ) {
177 if ( MapP->Topology[Link].Unit <= (uchar)MAX_RUP ) {
179 ** Its worth trying the operation...
181 return (*func)( HostP, MapP );
187 return -ENXIO;
192 RIOIdentifyRta(struct rio_info *p, caddr_t arg)
194 uint Host;
196 if ( copyin( (int)arg, (caddr_t)&IdRta, sizeof(IdRta) ) == COPYFAIL ) {
197 rio_dprintk (RIO_DEBUG_CMD, "RIO_IDENTIFY_RTA copy failed\n");
198 p->RIOError.Error = COPYIN_FAILED;
199 return -EFAULT;
202 for ( Host = 0 ; Host < p->RIONumHosts; Host++ ) {
203 uint Rta;
204 struct Host *HostP = &p->RIOHosts[Host];
206 for ( Rta = 0; Rta < RTAS_PER_HOST; Rta++ ) {
207 struct Map *MapP = &HostP->Mapping[Rta];
209 if ( MapP->RtaUniqueNum == IdRta.RtaUnique ) {
210 uint Link;
212 ** now, lets just check we have a route to it...
213 ** IF the routing stuff is working, then one of the
214 ** topology entries for this unit will have a legit
215 ** route *somewhere*. We care not where - if its got
216 ** any connections, we can get to it.
218 for ( Link = 0; Link < LINKS_PER_UNIT; Link++ ) {
219 if ( MapP->Topology[Link].Unit <= (uchar)MAX_RUP ) {
221 ** Its worth trying the operation...
223 struct CmdBlk *CmdBlkP;
225 rio_dprintk (RIO_DEBUG_CMD, "IDENTIFY RTA\n");
227 CmdBlkP = RIOGetCmdBlk();
229 if ( !CmdBlkP ) {
230 rio_dprintk (RIO_DEBUG_CMD, "IDENTIFY RTA: GetCmdBlk failed\n");
231 return -ENXIO;
234 CmdBlkP->Packet.dest_unit = MapP->ID;
235 CmdBlkP->Packet.dest_port = BOOT_RUP;
236 CmdBlkP->Packet.src_unit = 0;
237 CmdBlkP->Packet.src_port = BOOT_RUP;
238 CmdBlkP->Packet.len = 0x84;
239 CmdBlkP->Packet.data[0] = IDENTIFY;
240 CmdBlkP->Packet.data[1] = 0;
241 CmdBlkP->Packet.data[2] = IdRta.ID;
243 if ( RIOQueueCmdBlk( HostP, MapP->ID-1, CmdBlkP) == RIO_FAIL ) {
244 rio_dprintk (RIO_DEBUG_CMD, "IDENTIFY RTA: Failed to queue command\n");
245 return -EIO;
247 return 0;
253 return -ENOENT;
258 RIOKillNeighbour(struct rio_info *p, caddr_t arg)
260 uint Host;
261 uint ID;
262 struct Host *HostP;
263 struct CmdBlk *CmdBlkP;
265 rio_dprintk (RIO_DEBUG_CMD, "KILL HOST NEIGHBOUR\n");
267 if ( copyin( (int)arg, (caddr_t)&KillUnit, sizeof(KillUnit) ) == COPYFAIL ) {
268 rio_dprintk (RIO_DEBUG_CMD, "RIO_KILL_NEIGHBOUR copy failed\n");
269 p->RIOError.Error = COPYIN_FAILED;
270 return -EFAULT;
273 if ( KillUnit.Link > 3 )
274 return -ENXIO;
276 CmdBlkP = RIOGetCmdBlk();
278 if ( !CmdBlkP ) {
279 rio_dprintk (RIO_DEBUG_CMD, "UFOAD: GetCmdBlk failed\n");
280 return -ENXIO;
283 CmdBlkP->Packet.dest_unit = 0;
284 CmdBlkP->Packet.src_unit = 0;
285 CmdBlkP->Packet.dest_port = BOOT_RUP;
286 CmdBlkP->Packet.src_port = BOOT_RUP;
287 CmdBlkP->Packet.len = 0x84;
288 CmdBlkP->Packet.data[0] = UFOAD;
289 CmdBlkP->Packet.data[1] = KillUnit.Link;
290 CmdBlkP->Packet.data[2] = UFOAD_MAGIC & 0xFF;
291 CmdBlkP->Packet.data[3] = (UFOAD_MAGIC >> 8) & 0xFF;
293 for ( Host = 0; Host < p->RIONumHosts; Host++ ) {
294 ID = 0;
295 HostP = &p->RIOHosts[Host];
297 if ( HostP->UniqueNum == KillUnit.UniqueNum ) {
298 if ( RIOQueueCmdBlk( HostP, RTAS_PER_HOST+KillUnit.Link,
299 CmdBlkP) == RIO_FAIL ) {
300 rio_dprintk (RIO_DEBUG_CMD, "UFOAD: Failed queue command\n");
301 return -EIO;
303 return 0;
306 for ( ID=0; ID < RTAS_PER_HOST; ID++ ) {
307 if ( HostP->Mapping[ID].RtaUniqueNum == KillUnit.UniqueNum ) {
308 CmdBlkP->Packet.dest_unit = ID+1;
309 if ( RIOQueueCmdBlk( HostP, ID, CmdBlkP) == RIO_FAIL ) {
310 rio_dprintk (RIO_DEBUG_CMD, "UFOAD: Failed queue command\n");
311 return -EIO;
313 return 0;
317 RIOFreeCmdBlk( CmdBlkP );
318 return -ENXIO;
322 RIOSuspendBootRta(struct Host *HostP, int ID, int Link)
324 struct CmdBlk *CmdBlkP;
326 rio_dprintk (RIO_DEBUG_CMD, "SUSPEND BOOT ON RTA ID %d, link %c\n", ID, 'A' + Link);
328 CmdBlkP = RIOGetCmdBlk();
330 if ( !CmdBlkP ) {
331 rio_dprintk (RIO_DEBUG_CMD, "SUSPEND BOOT ON RTA: GetCmdBlk failed\n");
332 return -ENXIO;
335 CmdBlkP->Packet.dest_unit = ID;
336 CmdBlkP->Packet.dest_port = BOOT_RUP;
337 CmdBlkP->Packet.src_unit = 0;
338 CmdBlkP->Packet.src_port = BOOT_RUP;
339 CmdBlkP->Packet.len = 0x84;
340 CmdBlkP->Packet.data[0] = IWAIT;
341 CmdBlkP->Packet.data[1] = Link;
342 CmdBlkP->Packet.data[2] = IWAIT_MAGIC & 0xFF;
343 CmdBlkP->Packet.data[3] = (IWAIT_MAGIC >> 8) & 0xFF;
345 if ( RIOQueueCmdBlk( HostP, ID - 1, CmdBlkP) == RIO_FAIL ) {
346 rio_dprintk (RIO_DEBUG_CMD, "SUSPEND BOOT ON RTA: Failed to queue iwait command\n");
347 return -EIO;
349 return 0;
353 RIOFoadWakeup(struct rio_info *p)
355 int port;
356 register struct Port *PortP;
357 unsigned long flags;
359 for ( port=0; port<RIO_PORTS; port++) {
360 PortP = p->RIOPortp[port];
362 rio_spin_lock_irqsave(&PortP->portSem, flags);
363 PortP->Config = 0;
364 PortP->State = 0;
365 PortP->InUse = NOT_INUSE;
366 PortP->PortState = 0;
367 PortP->FlushCmdBodge = 0;
368 PortP->ModemLines = 0;
369 PortP->ModemState = 0;
370 PortP->CookMode = 0;
371 PortP->ParamSem = 0;
372 PortP->Mapped = 0;
373 PortP->WflushFlag = 0;
374 PortP->MagicFlags = 0;
375 PortP->RxDataStart = 0;
376 PortP->TxBufferIn = 0;
377 PortP->TxBufferOut = 0;
378 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
380 return(0);
384 ** Incoming command on the COMMAND_RUP to be processed.
386 static int
387 RIOCommandRup(struct rio_info *p, uint Rup, struct Host *HostP, PKT *PacketP)
389 struct PktCmd *PktCmdP = (struct PktCmd *)PacketP->data;
390 struct Port *PortP;
391 struct UnixRup *UnixRupP;
392 ushort SysPort;
393 ushort ReportedModemStatus;
394 ushort rup;
395 ushort subCommand;
396 unsigned long flags;
398 func_enter ();
400 #ifdef CHECK
401 CheckHost( Host );
402 CheckHostP( HostP );
403 CheckPacketP( PacketP );
404 #endif
407 ** 16 port RTA note:
408 ** Command rup packets coming from the RTA will have pkt->data[1] (which
409 ** translates to PktCmdP->PhbNum) set to the host port number for the
410 ** particular unit. To access the correct BaseSysPort for a 16 port RTA,
411 ** we can use PhbNum to get the rup number for the appropriate 8 port
412 ** block (for the first block, this should be equal to 'Rup').
414 rup = RBYTE(PktCmdP->PhbNum) / (ushort)PORTS_PER_RTA;
415 UnixRupP = &HostP->UnixRups[rup];
416 SysPort = UnixRupP->BaseSysPort +
417 (RBYTE(PktCmdP->PhbNum) % (ushort)PORTS_PER_RTA);
418 rio_dprintk (RIO_DEBUG_CMD, "Command on rup %d, port %d\n", rup, SysPort);
420 #ifdef CHECK
421 CheckRup( rup );
422 CheckUnixRupP( UnixRupP );
423 #endif
424 if ( UnixRupP->BaseSysPort == NO_PORT ) {
425 rio_dprintk (RIO_DEBUG_CMD, "OBSCURE ERROR!\n");
426 rio_dprintk (RIO_DEBUG_CMD, "Diagnostics follow. Please WRITE THESE DOWN and report them to Specialix Technical Support\n");
427 rio_dprintk (RIO_DEBUG_CMD, "CONTROL information: Host number %d, name ``%s''\n",
428 HostP-p->RIOHosts, HostP->Name );
429 rio_dprintk (RIO_DEBUG_CMD, "CONTROL information: Rup number 0x%x\n", rup);
431 if ( Rup >= (ushort)MAX_RUP ) {
432 rio_dprintk (RIO_DEBUG_CMD, "CONTROL information: This is the RUP for RTA ``%s''\n",
433 HostP->Mapping[Rup].Name);
434 } else
435 rio_dprintk (RIO_DEBUG_CMD, "CONTROL information: This is the RUP for link ``%c'' of host ``%s''\n",
436 ('A' + Rup - MAX_RUP), HostP->Name);
438 rio_dprintk (RIO_DEBUG_CMD, "PACKET information: Destination 0x%x:0x%x\n",
439 PacketP->dest_unit, PacketP->dest_port );
440 rio_dprintk (RIO_DEBUG_CMD, "PACKET information: Source 0x%x:0x%x\n",
441 PacketP->src_unit, PacketP->src_port );
442 rio_dprintk (RIO_DEBUG_CMD, "PACKET information: Length 0x%x (%d)\n", PacketP->len,PacketP->len );
443 rio_dprintk (RIO_DEBUG_CMD, "PACKET information: Control 0x%x (%d)\n", PacketP->control, PacketP->control);
444 rio_dprintk (RIO_DEBUG_CMD, "PACKET information: Check 0x%x (%d)\n", PacketP->csum, PacketP->csum );
445 rio_dprintk (RIO_DEBUG_CMD, "COMMAND information: Host Port Number 0x%x, "
446 "Command Code 0x%x\n", PktCmdP->PhbNum, PktCmdP->Command );
447 return TRUE;
450 #ifdef CHECK
451 CheckSysPort( SysPort );
452 #endif
453 PortP = p->RIOPortp[ SysPort ];
454 rio_spin_lock_irqsave(&PortP->portSem, flags);
455 switch( RBYTE(PktCmdP->Command) ) {
456 case BREAK_RECEIVED:
457 rio_dprintk (RIO_DEBUG_CMD, "Received a break!\n");
458 /* If the current line disc. is not multi-threading and
459 the current processor is not the default, reset rup_intr
460 and return FALSE to ensure that the command packet is
461 not freed. */
462 /* Call tmgr HANGUP HERE */
463 /* Fix this later when every thing works !!!! RAMRAJ */
464 gs_got_break (&PortP->gs);
465 break;
467 case COMPLETE:
468 rio_dprintk (RIO_DEBUG_CMD, "Command complete on phb %d host %d\n",
469 RBYTE(PktCmdP->PhbNum), HostP-p->RIOHosts);
470 subCommand = 1;
471 switch (RBYTE(PktCmdP->SubCommand)) {
472 case MEMDUMP :
473 rio_dprintk (RIO_DEBUG_CMD, "Memory dump cmd (0x%x) from addr 0x%x\n",
474 RBYTE(PktCmdP->SubCommand), RWORD(PktCmdP->SubAddr));
475 break;
476 case READ_REGISTER :
477 rio_dprintk (RIO_DEBUG_CMD, "Read register (0x%x)\n", RWORD(PktCmdP->SubAddr));
478 p->CdRegister = (RBYTE(PktCmdP->ModemStatus) & MSVR1_HOST);
479 break;
480 default :
481 subCommand = 0;
482 break;
484 if (subCommand)
485 break;
486 rio_dprintk (RIO_DEBUG_CMD, "New status is 0x%x was 0x%x\n",
487 RBYTE(PktCmdP->PortStatus),PortP->PortState);
488 if (PortP->PortState != RBYTE(PktCmdP->PortStatus)) {
489 rio_dprintk (RIO_DEBUG_CMD, "Mark status & wakeup\n");
490 PortP->PortState = RBYTE(PktCmdP->PortStatus);
491 /* What should we do here ...
492 wakeup( &PortP->PortState );
494 } else
495 rio_dprintk (RIO_DEBUG_CMD, "No change\n");
497 /* FALLTHROUGH */
498 case MODEM_STATUS:
500 ** Knock out the tbusy and tstop bits, as these are not relevant
501 ** to the check for modem status change (they're just there because
502 ** it's a convenient place to put them!).
504 ReportedModemStatus = RBYTE(PktCmdP->ModemStatus);
505 if ((PortP->ModemState & MSVR1_HOST) ==
506 (ReportedModemStatus & MSVR1_HOST)) {
507 rio_dprintk (RIO_DEBUG_CMD, "Modem status unchanged 0x%x\n", PortP->ModemState);
509 ** Update ModemState just in case tbusy or tstop states have
510 ** changed.
512 PortP->ModemState = ReportedModemStatus;
514 else {
515 rio_dprintk (RIO_DEBUG_CMD, "Modem status change from 0x%x to 0x%x\n",
516 PortP->ModemState, ReportedModemStatus);
517 PortP->ModemState = ReportedModemStatus;
518 #ifdef MODEM_SUPPORT
519 if ( PortP->Mapped ) {
520 /***********************************************************\
521 *************************************************************
522 *** ***
523 *** M O D E M S T A T E C H A N G E ***
524 *** ***
525 *************************************************************
526 \***********************************************************/
528 ** If the device is a modem, then check the modem
529 ** carrier.
531 if (PortP->gs.tty == NULL)
532 break;
533 if (PortP->gs.tty->termios == NULL)
534 break;
536 if (!(PortP->gs.tty->termios->c_cflag & CLOCAL) &&
537 ((PortP->State & (RIO_MOPEN|RIO_WOPEN)))) {
539 rio_dprintk (RIO_DEBUG_CMD, "Is there a Carrier?\n");
541 ** Is there a carrier?
543 if ( PortP->ModemState & MSVR1_CD ) {
545 ** Has carrier just appeared?
547 if (!(PortP->State & RIO_CARR_ON)) {
548 rio_dprintk (RIO_DEBUG_CMD, "Carrier just came up.\n");
549 PortP->State |= RIO_CARR_ON;
551 ** wakeup anyone in WOPEN
553 if (PortP->State & (PORT_ISOPEN | RIO_WOPEN) )
554 wake_up_interruptible (&PortP->gs.open_wait);
555 #ifdef STATS
556 PortP->Stat.ModemOnCnt++;
557 #endif
559 } else {
561 ** Has carrier just dropped?
563 if (PortP->State & RIO_CARR_ON) {
564 if (PortP->State & (PORT_ISOPEN|RIO_WOPEN|RIO_MOPEN))
565 tty_hangup (PortP->gs.tty);
566 PortP->State &= ~RIO_CARR_ON;
567 rio_dprintk (RIO_DEBUG_CMD, "Carrirer just went down\n");
568 #ifdef STATS
569 PortP->Stat.ModemOffCnt++;
570 #endif
575 #endif
577 break;
579 default:
580 rio_dprintk (RIO_DEBUG_CMD, "Unknown command %d on CMD_RUP of host %d\n",
581 RBYTE(PktCmdP->Command),HostP-p->RIOHosts);
582 break;
584 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
586 func_exit ();
588 return TRUE;
591 ** The command mechanism:
592 ** Each rup has a chain of commands associated with it.
593 ** This chain is maintained by routines in this file.
594 ** Periodically we are called and we run a quick check of all the
595 ** active chains to determine if there is a command to be executed,
596 ** and if the rup is ready to accept it.
601 ** Allocate an empty command block.
603 struct CmdBlk *
604 RIOGetCmdBlk(void)
606 struct CmdBlk *CmdBlkP;
608 CmdBlkP = (struct CmdBlk *)sysbrk(sizeof(struct CmdBlk));
609 if (CmdBlkP)
610 bzero(CmdBlkP, sizeof(struct CmdBlk));
612 return CmdBlkP;
616 ** Return a block to the head of the free list.
618 void
619 RIOFreeCmdBlk(struct CmdBlk *CmdBlkP)
621 sysfree((void *)CmdBlkP, sizeof(struct CmdBlk));
625 ** attach a command block to the list of commands to be performed for
626 ** a given rup.
629 RIOQueueCmdBlk(struct Host *HostP, uint Rup, struct CmdBlk *CmdBlkP)
631 struct CmdBlk **Base;
632 struct UnixRup *UnixRupP;
633 unsigned long flags;
635 #ifdef CHECK
636 CheckHostP( HostP );
637 CheckRup( Rup );
638 CheckCmdBlkP( CmdBlkP );
639 #endif
640 if ( Rup >= (ushort)(MAX_RUP+LINKS_PER_UNIT) ) {
641 rio_dprintk (RIO_DEBUG_CMD, "Illegal rup number %d in RIOQueueCmdBlk\n",Rup);
642 RIOFreeCmdBlk( CmdBlkP );
643 return RIO_FAIL;
646 UnixRupP = &HostP->UnixRups[Rup];
648 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
651 ** If the RUP is currently inactive, then put the request
652 ** straight on the RUP....
654 if ( (UnixRupP->CmdsWaitingP == NULL) && (UnixRupP->CmdPendingP == NULL) &&
655 (RWORD(UnixRupP->RupP->txcontrol) == TX_RUP_INACTIVE ) &&
656 (CmdBlkP->PreFuncP ? (*CmdBlkP->PreFuncP)(CmdBlkP->PreArg,CmdBlkP)
657 :TRUE)) {
658 rio_dprintk (RIO_DEBUG_CMD, "RUP inactive-placing command straight on. Cmd byte is 0x%x\n",
659 CmdBlkP->Packet.data[0]);
662 ** Whammy! blat that pack!
664 HostP->Copy( (caddr_t)&CmdBlkP->Packet,
665 RIO_PTR(HostP->Caddr, UnixRupP->RupP->txpkt ), sizeof(PKT) );
668 ** place command packet on the pending position.
670 UnixRupP->CmdPendingP = CmdBlkP;
673 ** set the command register
675 WWORD(UnixRupP->RupP->txcontrol , TX_PACKET_READY);
677 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
679 return RIO_SUCCESS;
681 rio_dprintk (RIO_DEBUG_CMD, "RUP active - en-queing\n");
683 if ( UnixRupP->CmdsWaitingP != NULL)
684 rio_dprintk (RIO_DEBUG_CMD, "Rup active - command waiting\n");
685 if ( UnixRupP->CmdPendingP != NULL )
686 rio_dprintk (RIO_DEBUG_CMD, "Rup active - command pending\n");
687 if ( RWORD(UnixRupP->RupP->txcontrol) != TX_RUP_INACTIVE )
688 rio_dprintk (RIO_DEBUG_CMD, "Rup active - command rup not ready\n");
690 Base = &UnixRupP->CmdsWaitingP;
692 rio_dprintk (RIO_DEBUG_CMD, "First try to queue cmdblk 0x%x at 0x%x\n", (int)CmdBlkP,(int)Base);
694 while ( *Base ) {
695 rio_dprintk (RIO_DEBUG_CMD, "Command cmdblk 0x%x here\n", (int)(*Base));
696 Base = &((*Base)->NextP);
697 rio_dprintk (RIO_DEBUG_CMD, "Now try to queue cmd cmdblk 0x%x at 0x%x\n",
698 (int)CmdBlkP,(int)Base);
701 rio_dprintk (RIO_DEBUG_CMD, "Will queue cmdblk 0x%x at 0x%x\n",(int)CmdBlkP,(int)Base);
703 *Base = CmdBlkP;
705 CmdBlkP->NextP = NULL;
707 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
709 return RIO_SUCCESS;
713 ** Here we go - if there is an empty rup, fill it!
714 ** must be called at splrio() or higher.
716 void
717 RIOPollHostCommands(struct rio_info *p, struct Host *HostP)
719 register struct CmdBlk *CmdBlkP;
720 register struct UnixRup *UnixRupP;
721 struct PKT *PacketP;
722 ushort Rup;
723 unsigned long flags;
726 Rup = MAX_RUP+LINKS_PER_UNIT;
728 do { /* do this loop for each RUP */
730 ** locate the rup we are processing & lock it
732 UnixRupP = &HostP->UnixRups[--Rup];
734 spin_lock_irqsave(&UnixRupP->RupLock, flags);
737 ** First check for incoming commands:
739 if ( RWORD(UnixRupP->RupP->rxcontrol) != RX_RUP_INACTIVE ) {
740 int FreeMe;
742 PacketP =(PKT *)RIO_PTR(HostP->Caddr,RWORD(UnixRupP->RupP->rxpkt));
744 ShowPacket( DBG_CMD, PacketP );
746 switch ( RBYTE(PacketP->dest_port) ) {
747 case BOOT_RUP:
748 rio_dprintk (RIO_DEBUG_CMD, "Incoming Boot %s packet '%x'\n",
749 RBYTE(PacketP->len) & 0x80 ? "Command":"Data",
750 RBYTE(PacketP->data[0]));
751 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
752 FreeMe= RIOBootRup(p, Rup,HostP,PacketP);
753 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
754 break;
756 case COMMAND_RUP:
758 ** Free the RUP lock as loss of carrier causes a
759 ** ttyflush which will (eventually) call another
760 ** routine that uses the RUP lock.
762 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
763 FreeMe= RIOCommandRup(p, Rup,HostP,PacketP);
764 if (PacketP->data[5] == MEMDUMP) {
765 rio_dprintk (RIO_DEBUG_CMD, "Memdump from 0x%x complete\n",
766 *(ushort *) &(PacketP->data[6]));
767 HostP->Copy( (caddr_t)&(PacketP->data[8]),
768 (caddr_t)p->RIOMemDump, 32 );
770 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
771 break;
773 case ROUTE_RUP:
774 rio_spin_unlock_irqrestore( &UnixRupP->RupLock, flags);
775 FreeMe = RIORouteRup(p, Rup, HostP, PacketP );
776 rio_spin_lock_irqsave( &UnixRupP->RupLock, flags );
777 break;
779 default:
780 rio_dprintk (RIO_DEBUG_CMD, "Unknown RUP %d\n", RBYTE(PacketP->dest_port));
781 FreeMe = 1;
782 break;
785 if ( FreeMe ) {
786 rio_dprintk (RIO_DEBUG_CMD, "Free processed incoming command packet\n");
787 put_free_end(HostP,PacketP);
789 WWORD(UnixRupP->RupP->rxcontrol , RX_RUP_INACTIVE);
791 if ( RWORD(UnixRupP->RupP->handshake)==PHB_HANDSHAKE_SET ) {
792 rio_dprintk (RIO_DEBUG_CMD, "Handshake rup %d\n",Rup);
793 WWORD(UnixRupP->RupP->handshake,
794 PHB_HANDSHAKE_SET|PHB_HANDSHAKE_RESET);
800 ** IF a command was running on the port,
801 ** and it has completed, then tidy it up.
803 if ( (CmdBlkP = UnixRupP->CmdPendingP) && /* ASSIGN! */
804 (RWORD(UnixRupP->RupP->txcontrol) == TX_RUP_INACTIVE)) {
806 ** we are idle.
807 ** there is a command in pending.
808 ** Therefore, this command has finished.
809 ** So, wakeup whoever is waiting for it (and tell them
810 ** what happened).
812 if ( CmdBlkP->Packet.dest_port == BOOT_RUP )
813 rio_dprintk (RIO_DEBUG_CMD, "Free Boot %s Command Block '%x'\n",
814 CmdBlkP->Packet.len & 0x80 ? "Command":"Data",
815 CmdBlkP->Packet.data[0]);
817 rio_dprintk (RIO_DEBUG_CMD, "Command 0x%x completed\n",(int)CmdBlkP);
820 ** Clear the Rup lock to prevent mutual exclusion.
822 if ( CmdBlkP->PostFuncP ) {
823 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
824 (*CmdBlkP->PostFuncP) (CmdBlkP->PostArg,CmdBlkP);
825 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
829 ** ....clear the pending flag....
831 UnixRupP->CmdPendingP = NULL;
834 ** ....and return the command block to the freelist.
836 RIOFreeCmdBlk( CmdBlkP );
840 ** If there is a command for this rup, and the rup
841 ** is idle, then process the command
843 if ( (CmdBlkP = UnixRupP->CmdsWaitingP) && /* ASSIGN! */
844 (UnixRupP->CmdPendingP == NULL) &&
845 (RWORD(UnixRupP->RupP->txcontrol) == TX_RUP_INACTIVE)) {
847 ** if the pre-function is non-zero, call it.
848 ** If it returns RIO_FAIL then don't
849 ** send this command yet!
851 #ifdef CHECK
852 CheckCmdBlkP (CmdBlkP);
853 #endif
854 if ( !(CmdBlkP->PreFuncP ?
855 (*CmdBlkP->PreFuncP)(CmdBlkP->PreArg, CmdBlkP) : TRUE)) {
856 rio_dprintk (RIO_DEBUG_CMD, "Not ready to start command 0x%x\n",(int)CmdBlkP);
858 else {
859 rio_dprintk (RIO_DEBUG_CMD, "Start new command 0x%x Cmd byte is 0x%x\n",
860 (int)CmdBlkP, CmdBlkP->Packet.data[0]);
862 ** Whammy! blat that pack!
864 #ifdef CHECK
865 CheckPacketP ((PKT *)RIO_PTR(HostP->Caddr, UnixRupP->RupP->txpkt));
866 #endif
867 HostP->Copy( (caddr_t)&CmdBlkP->Packet,
868 RIO_PTR(HostP->Caddr, UnixRupP->RupP->txpkt), sizeof(PKT));
871 ** remove the command from the rup command queue...
873 UnixRupP->CmdsWaitingP = CmdBlkP->NextP;
876 ** ...and place it on the pending position.
878 UnixRupP->CmdPendingP = CmdBlkP;
881 ** set the command register
883 WWORD(UnixRupP->RupP->txcontrol,TX_PACKET_READY);
886 ** the command block will be freed
887 ** when the command has been processed.
891 spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
892 } while ( Rup );
896 RIOWFlushMark(int iPortP, struct CmdBlk *CmdBlkP)
898 struct Port * PortP = (struct Port *)iPortP;
899 unsigned long flags;
901 rio_spin_lock_irqsave(&PortP->portSem, flags);
902 #ifdef CHECK
903 CheckPortP( PortP );
904 #endif
905 PortP->WflushFlag++;
906 PortP->MagicFlags |= MAGIC_FLUSH;
907 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
908 return RIOUnUse( iPortP, CmdBlkP );
912 RIORFlushEnable(int iPortP, struct CmdBlk *CmdBlkP)
914 struct Port * PortP = (struct Port *)iPortP;
915 PKT *PacketP;
916 unsigned long flags;
918 rio_spin_lock_irqsave(&PortP->portSem, flags);
920 while ( can_remove_receive(&PacketP, PortP) ) {
921 remove_receive(PortP);
922 ShowPacket(DBG_PROC, PacketP );
923 put_free_end( PortP->HostP, PacketP );
926 if ( RWORD(PortP->PhbP->handshake)==PHB_HANDSHAKE_SET ) {
928 ** MAGIC! (Basically, handshake the RX buffer, so that
929 ** the RTAs upstream can be re-enabled.)
931 rio_dprintk (RIO_DEBUG_CMD, "Util: Set RX handshake bit\n");
932 WWORD(PortP->PhbP->handshake, PHB_HANDSHAKE_SET|PHB_HANDSHAKE_RESET);
934 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
935 return RIOUnUse( iPortP, CmdBlkP );
939 RIOUnUse(int iPortP, struct CmdBlk *CmdBlkP)
941 struct Port * PortP = (struct Port *)iPortP;
942 unsigned long flags;
944 rio_spin_lock_irqsave(&PortP->portSem, flags);
946 #ifdef CHECK
947 CheckPortP( PortP );
948 #endif
949 rio_dprintk (RIO_DEBUG_CMD, "Decrement in use count for port\n");
951 if (PortP->InUse) {
952 if ( --PortP->InUse != NOT_INUSE ) {
953 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
954 return 0;
958 ** While PortP->InUse is set (i.e. a preemptive command has been sent to
959 ** the RTA and is awaiting completion), any transmit data is prevented from
960 ** being transferred from the write queue into the transmit packets
961 ** (add_transmit) and no furthur transmit interrupt will be sent for that
962 ** data. The next interrupt will occur up to 500ms later (RIOIntr is called
963 ** twice a second as a saftey measure). This was the case when kermit was
964 ** used to send data into a RIO port. After each packet was sent, TCFLSH
965 ** was called to flush the read queue preemptively. PortP->InUse was
966 ** incremented, thereby blocking the 6 byte acknowledgement packet
967 ** transmitted back. This acknowledgment hung around for 500ms before
968 ** being sent, thus reducing input performance substantially!.
969 ** When PortP->InUse becomes NOT_INUSE, we must ensure that any data
970 ** hanging around in the transmit buffer is sent immediately.
972 WWORD(PortP->HostP->ParmMapP->tx_intr, 1);
973 /* What to do here ..
974 wakeup( (caddr_t)&(PortP->InUse) );
976 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
977 return 0;
980 void
981 ShowPacket(uint Flags, struct PKT *PacketP)
987 ** How to use this file:
989 ** To send a command down a rup, you need to allocate a command block, fill
990 ** in the packet information, fill in the command number, fill in the pre-
991 ** and post- functions and arguments, and then add the command block to the
992 ** queue of command blocks for the port in question. When the port is idle,
993 ** then the pre-function will be called. If this returns RIO_FAIL then the
994 ** command will be re-queued and tried again at a later date (probably in one
995 ** clock tick). If the pre-function returns NOT RIO_FAIL, then the command
996 ** packet will be queued on the RUP, and the txcontrol field set to the
997 ** command number. When the txcontrol field has changed from being the
998 ** command number, then the post-function will be called, with the argument
999 ** specified earlier, a pointer to the command block, and the value of
1000 ** txcontrol.
1002 ** To allocate a command block, call RIOGetCmdBlk(). This returns a pointer
1003 ** to the command block structure allocated, or NULL if there aren't any.
1004 ** The block will have been zeroed for you.
1006 ** The structure has the following fields:
1008 ** struct CmdBlk
1009 ** {
1010 ** struct CmdBlk *NextP; ** Pointer to next command block **
1011 ** struct PKT Packet; ** A packet, to copy to the rup **
1012 ** int (*PreFuncP)(); ** The func to call to check if OK **
1013 ** int PreArg; ** The arg for the func **
1014 ** int (*PostFuncP)(); ** The func to call when completed **
1015 ** int PostArg; ** The arg for the func **
1016 ** };
1018 ** You need to fill in ALL fields EXCEPT NextP, which is used to link the
1019 ** blocks together either on the free list or on the Rup list.
1021 ** Packet is an actual packet structure to be filled in with the packet
1022 ** information associated with the command. You need to fill in everything,
1023 ** as the command processore doesn't process the command packet in any way.
1025 ** The PreFuncP is called before the packet is enqueued on the host rup.
1026 ** PreFuncP is called as (*PreFuncP)(PreArg, CmdBlkP);. PreFuncP must
1027 ** return !RIO_FAIL to have the packet queued on the rup, and RIO_FAIL
1028 ** if the packet is NOT to be queued.
1030 ** The PostFuncP is called when the command has completed. It is called
1031 ** as (*PostFuncP)(PostArg, CmdBlkP, txcontrol);. PostFuncP is not expected
1032 ** to return a value. PostFuncP does NOT need to free the command block,
1033 ** as this happens automatically after PostFuncP returns.
1035 ** Once the command block has been filled in, it is attached to the correct
1036 ** queue by calling RIOQueueCmdBlk( HostP, Rup, CmdBlkP ) where HostP is
1037 ** a pointer to the struct Host, Rup is the NUMBER of the rup (NOT a pointer
1038 ** to it!), and CmdBlkP is the pointer to the command block allocated using
1039 ** RIOGetCmdBlk().