2 ** -----------------------------------------------------------------------------
 
   4 **  Perle Specialix driver for Linux
 
   5 **  Ported from existing RIO Driver for SCO sources.
 
   7  *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
 
   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.
 
  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.
 
  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.
 
  23 **      Module          : rioroute.c
 
  25 **      Last Modified   : 11/6/98 10:33:46
 
  26 **      Retrieved       : 11/6/98 10:33:50
 
  28 **  ident @(#)rioroute.c        1.3
 
  30 ** -----------------------------------------------------------------------------
 
  33 static char *_rioroute_c_sccs_ = "@(#)rioroute.c        1.3";
 
  36 #include <linux/module.h>
 
  37 #include <linux/slab.h>
 
  38 #include <linux/errno.h>
 
  40 #include <asm/system.h>
 
  41 #include <asm/string.h>
 
  42 #include <asm/semaphore.h>
 
  43 #include <asm/uaccess.h>
 
  45 #include <linux/termios.h>
 
  46 #include <linux/serial.h>
 
  48 #include <linux/generic_serial.h>
 
  51 #include "linux_compat.h"
 
  52 #include "rio_linux.h"
 
  86 static int RIOCheckIsolated(struct rio_info *, struct Host *, uint);
 
  87 static int RIOIsolate(struct rio_info *, struct Host *, uint);
 
  88 static int RIOCheck(struct Host *, uint);
 
  89 static void RIOConCon(struct rio_info *, struct Host *, uint, uint, uint, uint, int);
 
  93 ** Incoming on the ROUTE_RUP
 
  94 ** I wrote this while I was tired. Forgive me.
 
  96 int RIORouteRup(struct rio_info *p, uint Rup, struct Host *HostP, PKT * PacketP)
 
  98         struct PktCmd *PktCmdP = (struct PktCmd *) PacketP->data;
 
  99         struct PktCmd_M *PktReplyP;
 
 100         struct CmdBlk *CmdBlkP;
 
 104         int ThisLink, ThisLinkMin, ThisLinkMax;
 
 109         uint ThisUnit, ThisUnit2;       /* 2 ids to accommodate 16 port RTA */
 
 110         uint OldUnit, NewUnit, OldLink, NewLink;
 
 111         char *MyType, *MyName;
 
 116          ** Is this unit telling us it's current link topology?
 
 118         if (RBYTE(PktCmdP->Command) == ROUTE_TOPOLOGY) {
 
 119                 MapP = HostP->Mapping;
 
 122                  ** The packet can be sent either by the host or by an RTA.
 
 123                  ** If it comes from the host, then we need to fill in the
 
 124                  ** Topology array in the host structure. If it came in
 
 125                  ** from an RTA then we need to fill in the Mapping structure's
 
 126                  ** Topology array for the unit.
 
 128                 if (Rup >= (ushort) MAX_RUP) {
 
 130                         TopP = HostP->Topology;
 
 132                         MyName = HostP->Name;
 
 133                         ThisLinkMin = ThisLinkMax = Rup - MAX_RUP;
 
 136                         TopP = HostP->Mapping[Rup].Topology;
 
 138                         MyName = HostP->Mapping[Rup].Name;
 
 140                         ThisLinkMax = LINKS_PER_UNIT - 1;
 
 144                  ** Lies will not be tolerated.
 
 145                  ** If any pair of links claim to be connected to the same
 
 146                  ** place, then ignore this packet completely.
 
 149                 for (ThisLink = ThisLinkMin + 1; ThisLink <= ThisLinkMax; ThisLink++) {
 
 151                          ** it won't lie about network interconnect, total disconnects
 
 152                          ** and no-IDs. (or at least, it doesn't *matter* if it does)
 
 154                         if (RBYTE(PktCmdP->RouteTopology[ThisLink].Unit) > (ushort) MAX_RUP)
 
 157                         for (NewLink = ThisLinkMin; NewLink < ThisLink; NewLink++) {
 
 158                                 if ((RBYTE(PktCmdP->RouteTopology[ThisLink].Unit) == RBYTE(PktCmdP->RouteTopology[NewLink].Unit)) && (RBYTE(PktCmdP->RouteTopology[ThisLink].Link) == RBYTE(PktCmdP->RouteTopology[NewLink].Link))) {
 
 165                         rio_dprintk(RIO_DEBUG_ROUTE, "LIES! DAMN LIES! %d LIES!\n", Lies);
 
 166                         rio_dprintk(RIO_DEBUG_ROUTE, "%d:%c %d:%c %d:%c %d:%c\n",
 
 167                                     RBYTE(PktCmdP->RouteTopology[0].Unit),
 
 168                                     'A' + RBYTE(PktCmdP->RouteTopology[0].Link),
 
 169                                     RBYTE(PktCmdP->RouteTopology[1].Unit),
 
 170                                     'A' + RBYTE(PktCmdP->RouteTopology[1].Link), RBYTE(PktCmdP->RouteTopology[2].Unit), 'A' + RBYTE(PktCmdP->RouteTopology[2].Link), RBYTE(PktCmdP->RouteTopology[3].Unit), 'A' + RBYTE(PktCmdP->RouteTopology[3].Link));
 
 175                  ** now, process each link.
 
 177                 for (ThisLink = ThisLinkMin; ThisLink <= ThisLinkMax; ThisLink++) {
 
 179                          ** this is what it was connected to
 
 181                         OldUnit = TopP[ThisLink].Unit;
 
 182                         OldLink = TopP[ThisLink].Link;
 
 185                          ** this is what it is now connected to
 
 187                         NewUnit = RBYTE(PktCmdP->RouteTopology[ThisLink].Unit);
 
 188                         NewLink = RBYTE(PktCmdP->RouteTopology[ThisLink].Link);
 
 190                         if (OldUnit != NewUnit || OldLink != NewLink) {
 
 192                                  ** something has changed!
 
 195                                 if (NewUnit > MAX_RUP && NewUnit != ROUTE_DISCONNECT && NewUnit != ROUTE_NO_ID && NewUnit != ROUTE_INTERCONNECT) {
 
 196                                         rio_dprintk(RIO_DEBUG_ROUTE, "I have a link from %s %s to unit %d:%d - I don't like it.\n", MyType, MyName, NewUnit, NewLink);
 
 199                                          ** put the new values in
 
 201                                         TopP[ThisLink].Unit = NewUnit;
 
 202                                         TopP[ThisLink].Link = NewLink;
 
 206                                         if (OldUnit <= MAX_RUP) {
 
 208                                                  ** If something has become bust, then re-enable them messages
 
 210                                                 if (!p->RIONoMessage)
 
 211                                                         RIOConCon(p, HostP, ThisUnit, ThisLink, OldUnit, OldLink, DISCONNECT);
 
 214                                         if ((NewUnit <= MAX_RUP) && !p->RIONoMessage)
 
 215                                                 RIOConCon(p, HostP, ThisUnit, ThisLink, NewUnit, NewLink, CONNECT);
 
 217                                         if (NewUnit == ROUTE_NO_ID)
 
 218                                                 rio_dprintk(RIO_DEBUG_ROUTE, "%s %s (%c) is connected to an unconfigured unit.\n", MyType, MyName, 'A' + ThisLink);
 
 220                                         if (NewUnit == ROUTE_INTERCONNECT) {
 
 221                                                 if (!p->RIONoMessage)
 
 222                                                         cprintf("%s '%s' (%c) is connected to another network.\n", MyType, MyName, 'A' + ThisLink);
 
 226                                          ** perform an update for 'the other end', so that these messages
 
 227                                          ** only appears once. Only disconnect the other end if it is pointing
 
 230                                         if (OldUnit == HOST_ID) {
 
 231                                                 if (HostP->Topology[OldLink].Unit == ThisUnit && HostP->Topology[OldLink].Link == ThisLink) {
 
 232                                                         rio_dprintk(RIO_DEBUG_ROUTE, "SETTING HOST (%c) TO DISCONNECTED!\n", OldLink + 'A');
 
 233                                                         HostP->Topology[OldLink].Unit = ROUTE_DISCONNECT;
 
 234                                                         HostP->Topology[OldLink].Link = NO_LINK;
 
 236                                                         rio_dprintk(RIO_DEBUG_ROUTE, "HOST(%c) WAS NOT CONNECTED TO %s (%c)!\n", OldLink + 'A', HostP->Mapping[ThisUnit - 1].Name, ThisLink + 'A');
 
 238                                         } else if (OldUnit <= MAX_RUP) {
 
 239                                                 if (HostP->Mapping[OldUnit - 1].Topology[OldLink].Unit == ThisUnit && HostP->Mapping[OldUnit - 1].Topology[OldLink].Link == ThisLink) {
 
 240                                                         rio_dprintk(RIO_DEBUG_ROUTE, "SETTING RTA %s (%c) TO DISCONNECTED!\n", HostP->Mapping[OldUnit - 1].Name, OldLink + 'A');
 
 241                                                         HostP->Mapping[OldUnit - 1].Topology[OldLink].Unit = ROUTE_DISCONNECT;
 
 242                                                         HostP->Mapping[OldUnit - 1].Topology[OldLink].Link = NO_LINK;
 
 244                                                         rio_dprintk(RIO_DEBUG_ROUTE, "RTA %s (%c) WAS NOT CONNECTED TO %s (%c)\n", HostP->Mapping[OldUnit - 1].Name, OldLink + 'A', HostP->Mapping[ThisUnit - 1].Name, ThisLink + 'A');
 
 247                                         if (NewUnit == HOST_ID) {
 
 248                                                 rio_dprintk(RIO_DEBUG_ROUTE, "MARKING HOST (%c) CONNECTED TO %s (%c)\n", NewLink + 'A', MyName, ThisLink + 'A');
 
 249                                                 HostP->Topology[NewLink].Unit = ThisUnit;
 
 250                                                 HostP->Topology[NewLink].Link = ThisLink;
 
 251                                         } else if (NewUnit <= MAX_RUP) {
 
 252                                                 rio_dprintk(RIO_DEBUG_ROUTE, "MARKING RTA %s (%c) CONNECTED TO %s (%c)\n", HostP->Mapping[NewUnit - 1].Name, NewLink + 'A', MyName, ThisLink + 'A');
 
 253                                                 HostP->Mapping[NewUnit - 1].Topology[NewLink].Unit = ThisUnit;
 
 254                                                 HostP->Mapping[NewUnit - 1].Topology[NewLink].Link = ThisLink;
 
 258                                 RIOCheckIsolated(p, HostP, OldUnit);
 
 265          ** The only other command we recognise is a route_request command
 
 267         if (RBYTE(PktCmdP->Command) != ROUTE_REQUEST) {
 
 268                 rio_dprintk(RIO_DEBUG_ROUTE, "Unknown command %d received on rup %d host %d ROUTE_RUP\n", RBYTE(PktCmdP->Command), Rup, (int) HostP);
 
 272         RtaUniq = (RBYTE(PktCmdP->UniqNum[0])) + (RBYTE(PktCmdP->UniqNum[1]) << 8) + (RBYTE(PktCmdP->UniqNum[2]) << 16) + (RBYTE(PktCmdP->UniqNum[3]) << 24);
 
 275          ** Determine if 8 or 16 port RTA
 
 277         RtaType = GetUnitType(RtaUniq);
 
 279         rio_dprintk(RIO_DEBUG_ROUTE, "Received a request for an ID for serial number %x\n", RtaUniq);
 
 281         Mod = RBYTE(PktCmdP->ModuleTypes);
 
 283         if (RtaType == TYPE_RTA16) {
 
 285                  ** Only one ident is set for a 16 port RTA. To make compatible
 
 286                  ** with 8 port, set 2nd ident in Mod2 to the same as Mod1.
 
 289                 rio_dprintk(RIO_DEBUG_ROUTE, "Backplane type is %s (all ports)\n", p->RIOModuleTypes[Mod1].Name);
 
 292                 rio_dprintk(RIO_DEBUG_ROUTE, "Module types are %s (ports 0-3) and %s (ports 4-7)\n", p->RIOModuleTypes[Mod1].Name, p->RIOModuleTypes[Mod2].Name);
 
 295         if (RtaUniq == 0xffffffff) {
 
 296                 ShowPacket(DBG_SPECIAL, PacketP);
 
 300          ** try to unhook a command block from the command free list.
 
 302         if (!(CmdBlkP = RIOGetCmdBlk())) {
 
 303                 rio_dprintk(RIO_DEBUG_ROUTE, "No command blocks to route RTA! come back later.\n");
 
 308          ** Fill in the default info on the command block
 
 310         CmdBlkP->Packet.dest_unit = Rup;
 
 311         CmdBlkP->Packet.dest_port = ROUTE_RUP;
 
 312         CmdBlkP->Packet.src_unit = HOST_ID;
 
 313         CmdBlkP->Packet.src_port = ROUTE_RUP;
 
 314         CmdBlkP->Packet.len = PKT_CMD_BIT | 1;
 
 315         CmdBlkP->PreFuncP = CmdBlkP->PostFuncP = NULL;
 
 316         PktReplyP = (struct PktCmd_M *) CmdBlkP->Packet.data;
 
 318         if (!RIOBootOk(p, HostP, RtaUniq)) {
 
 319                 rio_dprintk(RIO_DEBUG_ROUTE, "RTA %x tried to get an ID, but does not belong - FOAD it!\n", RtaUniq);
 
 320                 PktReplyP->Command = ROUTE_FOAD;
 
 321                 HostP->Copy("RT_FOAD", PktReplyP->CommandText, 7);
 
 322                 RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
 
 327          ** Check to see if the RTA is configured for this host
 
 329         for (ThisUnit = 0; ThisUnit < MAX_RUP; ThisUnit++) {
 
 330                 rio_dprintk(RIO_DEBUG_ROUTE, "Entry %d Flags=%s %s UniqueNum=0x%x\n",
 
 331                             ThisUnit, HostP->Mapping[ThisUnit].Flags & SLOT_IN_USE ? "Slot-In-Use" : "Not In Use", HostP->Mapping[ThisUnit].Flags & SLOT_TENTATIVE ? "Slot-Tentative" : "Not Tentative", HostP->Mapping[ThisUnit].RtaUniqueNum);
 
 334                  ** We have an entry for it.
 
 336                 if ((HostP->Mapping[ThisUnit].Flags & (SLOT_IN_USE | SLOT_TENTATIVE)) && (HostP->Mapping[ThisUnit].RtaUniqueNum == RtaUniq)) {
 
 337                         if (RtaType == TYPE_RTA16) {
 
 338                                 ThisUnit2 = HostP->Mapping[ThisUnit].ID2 - 1;
 
 339                                 rio_dprintk(RIO_DEBUG_ROUTE, "Found unit 0x%x at slots %d+%d\n", RtaUniq, ThisUnit, ThisUnit2);
 
 341                                 rio_dprintk(RIO_DEBUG_ROUTE, "Found unit 0x%x at slot %d\n", RtaUniq, ThisUnit);
 
 343                          ** If we have no knowledge of booting it, then the host has
 
 344                          ** been re-booted, and so we must kill the RTA, so that it
 
 345                          ** will be booted again (potentially with new bins)
 
 346                          ** and it will then re-ask for an ID, which we will service.
 
 348                         if ((HostP->Mapping[ThisUnit].Flags & SLOT_IN_USE) && !(HostP->Mapping[ThisUnit].Flags & RTA_BOOTED)) {
 
 349                                 if (!(HostP->Mapping[ThisUnit].Flags & MSG_DONE)) {
 
 350                                         if (!p->RIONoMessage)
 
 351                                                 cprintf("RTA '%s' is being updated.\n", HostP->Mapping[ThisUnit].Name);
 
 352                                         HostP->Mapping[ThisUnit].Flags |= MSG_DONE;
 
 354                                 PktReplyP->Command = ROUTE_FOAD;
 
 355                                 HostP->Copy("RT_FOAD", PktReplyP->CommandText, 7);
 
 356                                 RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
 
 361                          ** Send the ID (entry) to this RTA. The ID number is implicit as
 
 362                          ** the offset into the table. It is worth noting at this stage
 
 363                          ** that offset zero in the table contains the entries for the
 
 366                         PktReplyP->Command = ROUTE_ALLOCATE;
 
 367                         PktReplyP->IDNum = ThisUnit + 1;
 
 368                         if (RtaType == TYPE_RTA16) {
 
 369                                 if (HostP->Mapping[ThisUnit].Flags & SLOT_IN_USE)
 
 371                                          ** Adjust the phb and tx pkt dest_units for 2nd block of 8
 
 372                                          ** only if the RTA has ports associated (SLOT_IN_USE)
 
 374                                         RIOFixPhbs(p, HostP, ThisUnit2);
 
 375                                 PktReplyP->IDNum2 = ThisUnit2 + 1;
 
 376                                 rio_dprintk(RIO_DEBUG_ROUTE, "RTA '%s' has been allocated IDs %d+%d\n", HostP->Mapping[ThisUnit].Name, PktReplyP->IDNum, PktReplyP->IDNum2);
 
 378                                 PktReplyP->IDNum2 = ROUTE_NO_ID;
 
 379                                 rio_dprintk(RIO_DEBUG_ROUTE, "RTA '%s' has been allocated ID %d\n", HostP->Mapping[ThisUnit].Name, PktReplyP->IDNum);
 
 381                         HostP->Copy("RT_ALLOCAT", PktReplyP->CommandText, 10);
 
 383                         RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
 
 386                          ** If this is a freshly booted RTA, then we need to re-open
 
 387                          ** the ports, if any where open, so that data may once more
 
 388                          ** flow around the system!
 
 390                         if ((HostP->Mapping[ThisUnit].Flags & RTA_NEWBOOT) && (HostP->Mapping[ThisUnit].SysPort != NO_PORT)) {
 
 392                                  ** look at the ports associated with this beast and
 
 393                                  ** see if any where open. If they was, then re-open
 
 394                                  ** them, using the info from the tty flags.
 
 396                                 for (port = 0; port < PORTS_PER_RTA; port++) {
 
 397                                         PortP = p->RIOPortp[port + HostP->Mapping[ThisUnit].SysPort];
 
 398                                         if (PortP->State & (RIO_MOPEN | RIO_LOPEN)) {
 
 399                                                 rio_dprintk(RIO_DEBUG_ROUTE, "Re-opened this port\n");
 
 400                                                 rio_spin_lock_irqsave(&PortP->portSem, flags);
 
 401                                                 PortP->MagicFlags |= MAGIC_REBOOT;
 
 402                                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
 
 405                                 if (RtaType == TYPE_RTA16) {
 
 406                                         for (port = 0; port < PORTS_PER_RTA; port++) {
 
 407                                                 PortP = p->RIOPortp[port + HostP->Mapping[ThisUnit2].SysPort];
 
 408                                                 if (PortP->State & (RIO_MOPEN | RIO_LOPEN)) {
 
 409                                                         rio_dprintk(RIO_DEBUG_ROUTE, "Re-opened this port\n");
 
 410                                                         rio_spin_lock_irqsave(&PortP->portSem, flags);
 
 411                                                         PortP->MagicFlags |= MAGIC_REBOOT;
 
 412                                                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
 
 419                          ** keep a copy of the module types!
 
 421                         HostP->UnixRups[ThisUnit].ModTypes = Mod;
 
 422                         if (RtaType == TYPE_RTA16)
 
 423                                 HostP->UnixRups[ThisUnit2].ModTypes = Mod;
 
 426                          ** If either of the modules on this unit is read-only or write-only
 
 427                          ** or none-xprint, then we need to transfer that info over to the
 
 430                         if (HostP->Mapping[ThisUnit].SysPort != NO_PORT) {
 
 431                                 for (port = 0; port < PORTS_PER_MODULE; port++) {
 
 432                                         p->RIOPortp[port + HostP->Mapping[ThisUnit].SysPort]->Config &= ~RIO_NOMASK;
 
 433                                         p->RIOPortp[port + HostP->Mapping[ThisUnit].SysPort]->Config |= p->RIOModuleTypes[Mod1].Flags[port];
 
 434                                         p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit].SysPort]->Config &= ~RIO_NOMASK;
 
 435                                         p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit].SysPort]->Config |= p->RIOModuleTypes[Mod2].Flags[port];
 
 437                                 if (RtaType == TYPE_RTA16) {
 
 438                                         for (port = 0; port < PORTS_PER_MODULE; port++) {
 
 439                                                 p->RIOPortp[port + HostP->Mapping[ThisUnit2].SysPort]->Config &= ~RIO_NOMASK;
 
 440                                                 p->RIOPortp[port + HostP->Mapping[ThisUnit2].SysPort]->Config |= p->RIOModuleTypes[Mod1].Flags[port];
 
 441                                                 p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit2].SysPort]->Config &= ~RIO_NOMASK;
 
 442                                                 p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit2].SysPort]->Config |= p->RIOModuleTypes[Mod2].Flags[port];
 
 448                          ** Job done, get on with the interrupts!
 
 454          ** There is no table entry for this RTA at all.
 
 456          ** Lets check to see if we actually booted this unit - if not,
 
 457          ** then we reset it and it will go round the loop of being booted
 
 458          ** we can then worry about trying to fit it into the table.
 
 460         for (ThisUnit = 0; ThisUnit < HostP->NumExtraBooted; ThisUnit++)
 
 461                 if (HostP->ExtraUnits[ThisUnit] == RtaUniq)
 
 463         if (ThisUnit == HostP->NumExtraBooted && ThisUnit != MAX_EXTRA_UNITS) {
 
 465                  ** if the unit wasn't in the table, and the table wasn't full, then
 
 466                  ** we reset the unit, because we didn't boot it.
 
 467                  ** However, if the table is full, it could be that we did boot
 
 468                  ** this unit, and so we won't reboot it, because it isn't really
 
 469                  ** all that disasterous to keep the old bins in most cases. This
 
 470                  ** is a rather tacky feature, but we are on the edge of reallity
 
 471                  ** here, because the implication is that someone has connected
 
 472                  ** 16+MAX_EXTRA_UNITS onto one host.
 
 474                 static int UnknownMesgDone = 0;
 
 476                 if (!UnknownMesgDone) {
 
 477                         if (!p->RIONoMessage)
 
 478                                 cprintf("One or more unknown RTAs are being updated.\n");
 
 482                 PktReplyP->Command = ROUTE_FOAD;
 
 483                 HostP->Copy("RT_FOAD", PktReplyP->CommandText, 7);
 
 486                  ** we did boot it (as an extra), and there may now be a table
 
 487                  ** slot free (because of a delete), so we will try to make
 
 488                  ** a tentative entry for it, so that the configurator can see it
 
 489                  ** and fill in the details for us.
 
 491                 if (RtaType == TYPE_RTA16) {
 
 492                         if (RIOFindFreeID(p, HostP, &ThisUnit, &ThisUnit2) == 0) {
 
 493                                 RIODefaultName(p, HostP, ThisUnit);
 
 494                                 FillSlot(ThisUnit, ThisUnit2, RtaUniq, HostP);
 
 497                         if (RIOFindFreeID(p, HostP, &ThisUnit, NULL) == 0) {
 
 498                                 RIODefaultName(p, HostP, ThisUnit);
 
 499                                 FillSlot(ThisUnit, 0, RtaUniq, HostP);
 
 502                 PktReplyP->Command = ROUTE_USED;
 
 503                 HostP->Copy("RT_USED", PktReplyP->CommandText, 7);
 
 505         RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
 
 510 void RIOFixPhbs(p, HostP, unit)
 
 518         int PortN = HostP->Mapping[unit].SysPort;
 
 520         rio_dprintk(RIO_DEBUG_ROUTE, "RIOFixPhbs unit %d sysport %d\n", unit, PortN);
 
 523                 ushort dest_unit = HostP->Mapping[unit].ID2;
 
 526                  ** Get the link number used for the 1st 8 phbs on this unit.
 
 528                 PortP = p->RIOPortp[HostP->Mapping[dest_unit - 1].SysPort];
 
 530                 link = RWORD(PortP->PhbP->link);
 
 532                 for (port = 0; port < PORTS_PER_RTA; port++, PortN++) {
 
 533                         ushort dest_port = port + 8;
 
 537                         PortP = p->RIOPortp[PortN];
 
 539                         rio_spin_lock_irqsave(&PortP->portSem, flags);
 
 541                          ** If RTA is not powered on, the tx packets will be
 
 542                          ** unset, so go no further.
 
 544                         if (PortP->TxStart == 0) {
 
 545                                 rio_dprintk(RIO_DEBUG_ROUTE, "Tx pkts not set up yet\n");
 
 546                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
 
 551                          ** For the second slot of a 16 port RTA, the driver needs to
 
 552                          ** sort out the phb to port mappings. The dest_unit for this
 
 553                          ** group of 8 phbs is set to the dest_unit of the accompanying
 
 554                          ** 8 port block. The dest_port of the second unit is set to
 
 555                          ** be in the range 8-15 (i.e. 8 is added). Thus, for a 16 port
 
 556                          ** RTA with IDs 5 and 6, traffic bound for port 6 of unit 6
 
 557                          ** (being the second map ID) will be sent to dest_unit 5, port
 
 558                          ** 14. When this RTA is deleted, dest_unit for ID 6 will be
 
 559                          ** restored, and the dest_port will be reduced by 8.
 
 560                          ** Transmit packets also have a destination field which needs
 
 561                          ** adjusting in the same manner.
 
 562                          ** Note that the unit/port bytes in 'dest' are swapped.
 
 563                          ** We also need to adjust the phb and rup link numbers for the
 
 564                          ** second block of 8 ttys.
 
 566                         for (TxPktP = PortP->TxStart; TxPktP <= PortP->TxEnd; TxPktP++) {
 
 568                                  ** *TxPktP is the pointer to the transmit packet on the host
 
 569                                  ** card. This needs to be translated into a 32 bit pointer
 
 570                                  ** so it can be accessed from the driver.
 
 572                                 Pkt = (PKT *) RIO_PTR(HostP->Caddr, RINDW(TxPktP));
 
 575                                  ** If the packet is used, reset it.
 
 577                                 Pkt = (PKT *) ((uint) Pkt & ~PKT_IN_USE);
 
 578                                 WBYTE(Pkt->dest_unit, dest_unit);
 
 579                                 WBYTE(Pkt->dest_port, dest_port);
 
 581                         rio_dprintk(RIO_DEBUG_ROUTE, "phb dest: Old %x:%x New %x:%x\n", RWORD(PortP->PhbP->destination) & 0xff, (RWORD(PortP->PhbP->destination) >> 8) & 0xff, dest_unit, dest_port);
 
 582                         WWORD(PortP->PhbP->destination, dest_unit + (dest_port << 8));
 
 583                         WWORD(PortP->PhbP->link, link);
 
 585                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
 
 588                  ** Now make sure the range of ports to be serviced includes
 
 589                  ** the 2nd 8 on this 16 port RTA.
 
 593                 if (((unit * 8) + 7) > RWORD(HostP->LinkStrP[link].last_port)) {
 
 594                         rio_dprintk(RIO_DEBUG_ROUTE, "last port on host link %d: %d\n", link, (unit * 8) + 7);
 
 595                         WWORD(HostP->LinkStrP[link].last_port, (unit * 8) + 7);
 
 601 ** Check to see if the new disconnection has isolated this unit.
 
 602 ** If it has, then invalidate all its link information, and tell
 
 603 ** the world about it. This is done to ensure that the configurator
 
 604 ** only gets up-to-date information about what is going on.
 
 606 static int RIOCheckIsolated(p, HostP, UnitId)
 
 612         rio_spin_lock_irqsave(&HostP->HostLock, flags);
 
 614         if (RIOCheck(HostP, UnitId)) {
 
 615                 rio_dprintk(RIO_DEBUG_ROUTE, "Unit %d is NOT isolated\n", UnitId);
 
 616                 rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
 
 620         RIOIsolate(p, HostP, UnitId);
 
 622         rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
 
 627 ** Invalidate all the link interconnectivity of this unit, and of
 
 628 ** all the units attached to it. This will mean that the entire
 
 629 ** subnet will re-introduce itself.
 
 631 static int RIOIsolate(p, HostP, UnitId)
 
 638         UnitId--;               /* this trick relies on the Unit Id being UNSIGNED! */
 
 640         if (UnitId >= MAX_RUP)  /* dontcha just lurv unsigned maths! */
 
 643         if (HostP->Mapping[UnitId].Flags & BEEN_HERE)
 
 646         HostP->Mapping[UnitId].Flags |= BEEN_HERE;
 
 648         if (p->RIOPrintDisabled == DO_PRINT)
 
 649                 rio_dprintk(RIO_DEBUG_ROUTE, "RIOMesgIsolated %s", HostP->Mapping[UnitId].Name);
 
 651         for (link = 0; link < LINKS_PER_UNIT; link++) {
 
 652                 unit = HostP->Mapping[UnitId].Topology[link].Unit;
 
 653                 HostP->Mapping[UnitId].Topology[link].Unit = ROUTE_DISCONNECT;
 
 654                 HostP->Mapping[UnitId].Topology[link].Link = NO_LINK;
 
 655                 RIOIsolate(p, HostP, unit);
 
 657         HostP->Mapping[UnitId].Flags &= ~BEEN_HERE;
 
 661 static int RIOCheck(HostP, UnitId)
 
 667 /*      rio_dprint(RIO_DEBUG_ROUTE, ("Check to see if unit %d has a route to the host\n",UnitId)); */
 
 668         rio_dprintk(RIO_DEBUG_ROUTE, "RIOCheck : UnitID = %d\n", UnitId);
 
 670         if (UnitId == HOST_ID) {
 
 671                 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d is NOT isolated - it IS the host!\n", UnitId)); */
 
 677         if (UnitId >= MAX_RUP) {
 
 678                 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d - ignored.\n", UnitId)); */
 
 682         for (link = 0; link < LINKS_PER_UNIT; link++) {
 
 683                 if (HostP->Mapping[UnitId].Topology[link].Unit == HOST_ID) {
 
 684                         /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d is connected directly to host via link (%c).\n", 
 
 685                            UnitId, 'A'+link)); */
 
 690         if (HostP->Mapping[UnitId].Flags & BEEN_HERE) {
 
 691                 /* rio_dprint(RIO_DEBUG_ROUTE, ("Been to Unit %d before - ignoring\n", UnitId)); */
 
 695         HostP->Mapping[UnitId].Flags |= BEEN_HERE;
 
 697         for (link = 0; link < LINKS_PER_UNIT; link++) {
 
 698                 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d check link (%c)\n", UnitId,'A'+link)); */
 
 699                 if (RIOCheck(HostP, HostP->Mapping[UnitId].Topology[link].Unit)) {
 
 700                         /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d is connected to something that knows the host via link (%c)\n", UnitId,link+'A')); */
 
 701                         HostP->Mapping[UnitId].Flags &= ~BEEN_HERE;
 
 706         HostP->Mapping[UnitId].Flags &= ~BEEN_HERE;
 
 708         /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d DOESNT KNOW THE HOST!\n", UnitId)); */
 
 714 ** Returns the type of unit (host, 16/8 port RTA)
 
 717 uint GetUnitType(Uniq)
 
 720         switch ((Uniq >> 28) & 0xf) {
 
 725                 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: Host\n");
 
 728                 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: 16 port RTA\n");
 
 731                 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: 8 port RTA\n");
 
 734                 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: Unrecognised\n");
 
 742         if (p->RIOQuickCheck != NOT_CHANGED)
 
 744         p->RIOQuickCheck = CHANGED;
 
 745         if (p->RIOSignalProcess) {
 
 746                 rio_dprintk(RIO_DEBUG_ROUTE, "Send SIG-HUP");
 
 748                    psignal( RIOSignalProcess, SIGHUP );
 
 754 static void RIOConCon(p, HostP, FromId, FromLink, ToId, ToLink, Change)
 
 770 ** 15.10.1998 ARG - ESIL 0759
 
 771 ** (Part) fix for port being trashed when opened whilst RTA "disconnected"
 
 773 ** What's this doing in here anyway ?
 
 774 ** It was causing the port to be 'unmapped' if opened whilst RTA "disconnected"
 
 776 ** 09.12.1998 ARG - ESIL 0776 - part fix
 
 777 ** Okay, We've found out what this was all about now !
 
 778 ** Someone had botched this to use RIOHalted to indicated the number of RTAs
 
 779 ** 'disconnected'. The value in RIOHalted was then being used in the
 
 780 ** 'RIO_QUICK_CHECK' ioctl. A none zero value indicating that a least one RTA
 
 781 ** is 'disconnected'. The change was put in to satisfy a customer's needs.
 
 782 ** Having taken this bit of code out 'RIO_QUICK_CHECK' now no longer works for
 
 785     if (Change == CONNECT) {
 
 786                 if (p->RIOHalted) p->RIOHalted --;
 
 792 ** So - we need to implement it slightly differently - a new member of the
 
 793 ** rio_info struct - RIORtaDisCons (RIO RTA connections) keeps track of RTA
 
 794 ** connections and disconnections. 
 
 796         if (Change == CONNECT) {
 
 797                 if (p->RIORtaDisCons)
 
 803         if (p->RIOPrintDisabled == DONT_PRINT)
 
 815         FromName = FromId ? HostP->Mapping[FromId - 1].Name : HostP->Name;
 
 816         FromType = FromId ? "RTA" : "HOST";
 
 817         ToName = ToId ? HostP->Mapping[ToId - 1].Name : HostP->Name;
 
 818         ToType = ToId ? "RTA" : "HOST";
 
 820         rio_dprintk(RIO_DEBUG_ROUTE, "Link between %s '%s' (%c) and %s '%s' (%c) %s.\n", FromType, FromName, 'A' + FromLink, ToType, ToName, 'A' + ToLink, (Change == CONNECT) ? "established" : "disconnected");
 
 821         cprintf("Link between %s '%s' (%c) and %s '%s' (%c) %s.\n", FromType, FromName, 'A' + FromLink, ToType, ToName, 'A' + ToLink, (Change == CONNECT) ? "established" : "disconnected");
 
 825 ** RIORemoveFromSavedTable :
 
 827 ** Delete and RTA entry from the saved table given to us
 
 828 ** by the configuration program.
 
 830 static int RIORemoveFromSavedTable(struct rio_info *p, struct Map *pMap)
 
 835          ** We loop for all entries even after finding an entry and
 
 836          ** zeroing it because we may have two entries to delete if
 
 837          ** it's a 16 port RTA.
 
 839         for (entry = 0; entry < TOTAL_MAP_ENTRIES; entry++) {
 
 840                 if (p->RIOSavedTable[entry].RtaUniqueNum == pMap->RtaUniqueNum) {
 
 841                         bzero((caddr_t) & p->RIOSavedTable[entry], sizeof(struct Map));
 
 849 ** RIOCheckDisconnected :
 
 851 ** Scan the unit links to and return zero if the unit is completely
 
 854 static int RIOFreeDisconnected(struct rio_info *p, struct Host *HostP, int unit)
 
 859         rio_dprintk(RIO_DEBUG_ROUTE, "RIOFreeDisconnect unit %d\n", unit);
 
 861          ** If the slot is tentative and does not belong to the
 
 862          ** second half of a 16 port RTA then scan to see if
 
 865         for (link = 0; link < LINKS_PER_UNIT; link++) {
 
 866                 if (HostP->Mapping[unit].Topology[link].Unit != ROUTE_DISCONNECT)
 
 871          ** If not all links are disconnected then we can forget about it.
 
 873         if (link < LINKS_PER_UNIT)
 
 876 #ifdef NEED_TO_FIX_THIS
 
 877         /* Ok so all the links are disconnected. But we may have only just
 
 878          ** made this slot tentative and not yet received a topology update.
 
 879          ** Lets check how long ago we made it tentative.
 
 881         rio_dprintk(RIO_DEBUG_ROUTE, "Just about to check LBOLT on entry %d\n", unit);
 
 882         if (drv_getparm(LBOLT, (ulong_t *) & current_time))
 
 883                 rio_dprintk(RIO_DEBUG_ROUTE, "drv_getparm(LBOLT,....) Failed.\n");
 
 885         elapse_time = current_time - TentTime[unit];
 
 886         rio_dprintk(RIO_DEBUG_ROUTE, "elapse %d = current %d - tent %d (%d usec)\n", elapse_time, current_time, TentTime[unit], drv_hztousec(elapse_time));
 
 887         if (drv_hztousec(elapse_time) < WAIT_TO_FINISH) {
 
 888                 rio_dprintk(RIO_DEBUG_ROUTE, "Skipping slot %d, not timed out yet %d\n", unit, drv_hztousec(elapse_time));
 
 894          ** We have found an usable slot.
 
 895          ** If it is half of a 16 port RTA then delete the other half.
 
 897         if (HostP->Mapping[unit].ID2 != 0) {
 
 898                 int nOther = (HostP->Mapping[unit].ID2) - 1;
 
 900                 rio_dprintk(RIO_DEBUG_ROUTE, "RioFreedis second slot %d.\n", nOther);
 
 901                 bzero((caddr_t) & HostP->Mapping[nOther], sizeof(struct Map));
 
 903         RIORemoveFromSavedTable(p, &HostP->Mapping[unit]);
 
 912 ** This function scans the given host table for either one
 
 913 ** or two free unit ID's.
 
 915 int RIOFindFreeID(struct rio_info *p, struct Host *HostP, uint * pID1, uint * pID2)
 
 920          ** Initialise the ID's to MAX_RUP.
 
 921          ** We do this to make the loop for setting the ID's as simple as
 
 929          ** Scan all entries of the host mapping table for free slots.
 
 930          ** We scan for free slots first and then if that is not successful
 
 931          ** we start all over again looking for tentative slots we can re-use.
 
 933         for (unit = 0; unit < MAX_RUP; unit++) {
 
 934                 rio_dprintk(RIO_DEBUG_ROUTE, "Scanning unit %d\n", unit);
 
 936                  ** If the flags are zero then the slot is empty.
 
 938                 if (HostP->Mapping[unit].Flags == 0) {
 
 939                         rio_dprintk(RIO_DEBUG_ROUTE, "      This slot is empty.\n");
 
 941                          ** If we haven't allocated the first ID then do it now.
 
 943                         if (*pID1 == MAX_RUP) {
 
 944                                 rio_dprintk(RIO_DEBUG_ROUTE, "Make tentative entry for first unit %d\n", unit);
 
 948                                  ** If the second ID is not needed then we can return
 
 955                                  ** Allocate the second slot and return.
 
 957                                 rio_dprintk(RIO_DEBUG_ROUTE, "Make tentative entry for second unit %d\n", unit);
 
 965          ** If we manage to come out of the free slot loop then we
 
 966          ** need to start all over again looking for tentative slots
 
 967          ** that we can re-use.
 
 969         rio_dprintk(RIO_DEBUG_ROUTE, "Starting to scan for tentative slots\n");
 
 970         for (unit = 0; unit < MAX_RUP; unit++) {
 
 971                 if (((HostP->Mapping[unit].Flags & SLOT_TENTATIVE) || (HostP->Mapping[unit].Flags == 0)) && !(HostP->Mapping[unit].Flags & RTA16_SECOND_SLOT)) {
 
 972                         rio_dprintk(RIO_DEBUG_ROUTE, "    Slot %d looks promising.\n", unit);
 
 975                                 rio_dprintk(RIO_DEBUG_ROUTE, "    No it isn't, its the 1st half\n");
 
 980                          ** Slot is Tentative or Empty, but not a tentative second
 
 981                          ** slot of a 16 porter.
 
 982                          ** Attempt to free up this slot (and its parnter if
 
 983                          ** it is a 16 port slot. The second slot will become
 
 984                          ** empty after a call to RIOFreeDisconnected so thats why
 
 985                          ** we look for empty slots above  as well).
 
 987                         if (HostP->Mapping[unit].Flags != 0)
 
 988                                 if (RIOFreeDisconnected(p, HostP, unit) != 0)
 
 991                          ** If we haven't allocated the first ID then do it now.
 
 993                         if (*pID1 == MAX_RUP) {
 
 994                                 rio_dprintk(RIO_DEBUG_ROUTE, "Grab tentative entry for first unit %d\n", unit);
 
 998                                  ** Clear out this slot now that we intend to use it.
 
1000                                 bzero(&HostP->Mapping[unit], sizeof(struct Map));
 
1003                                  ** If the second ID is not needed then we can return
 
1010                                  ** Allocate the second slot and return.
 
1012                                 rio_dprintk(RIO_DEBUG_ROUTE, "Grab tentative/empty  entry for second unit %d\n", unit);
 
1016                                  ** Clear out this slot now that we intend to use it.
 
1018                                 bzero(&HostP->Mapping[unit], sizeof(struct Map));
 
1020                                 /* At this point under the right(wrong?) conditions
 
1021                                  ** we may have a first unit ID being higher than the
 
1022                                  ** second unit ID. This is a bad idea if we are about
 
1023                                  ** to fill the slots with a 16 port RTA.
 
1024                                  ** Better check and swap them over.
 
1027                                 if (*pID1 > *pID2) {
 
1028                                         rio_dprintk(RIO_DEBUG_ROUTE, "Swapping IDS %d %d\n", *pID1, *pID2);
 
1039          ** If we manage to get to the end of the second loop then we
 
1040          ** can give up and return a failure.
 
1047 ** The link switch scenario.
 
1049 ** Rta Wun (A) is connected to Tuw (A).
 
1050 ** The tables are all up to date, and the system is OK.
 
1052 ** If Wun (A) is now moved to Wun (B) before Wun (A) can
 
1053 ** become disconnected, then the follow happens:
 
1055 ** Tuw (A) spots the change of unit:link at the other end
 
1056 ** of its link and Tuw sends a topology packet reflecting
 
1057 ** the change: Tuw (A) now disconnected from Wun (A), and
 
1058 ** this is closely followed by a packet indicating that 
 
1059 ** Tuw (A) is now connected to Wun (B).
 
1061 ** Wun (B) will spot that it has now become connected, and
 
1062 ** Wun will send a topology packet, which indicates that
 
1063 ** both Wun (A) and Wun (B) is connected to Tuw (A).
 
1065 ** Eventually Wun (A) realises that it is now disconnected
 
1066 ** and Wun will send out a topology packet indicating that
 
1067 ** Wun (A) is now disconnected.