Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
[sfrench/cifs-2.6.git] / drivers / char / rio / rioroute.c
1 /*
2 ** -----------------------------------------------------------------------------
3 **
4 **  Perle Specialix driver for Linux
5 **  Ported from existing RIO Driver for SCO sources.
6  *
7  *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License, or
12  *      (at your option) any later version.
13  *
14  *      This program is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *      GNU General Public License for more details.
18  *
19  *      You should have received a copy of the GNU General Public License
20  *      along with this program; if not, write to the Free Software
21  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 **
23 **      Module          : rioroute.c
24 **      SID             : 1.3
25 **      Last Modified   : 11/6/98 10:33:46
26 **      Retrieved       : 11/6/98 10:33:50
27 **
28 **  ident @(#)rioroute.c        1.3
29 **
30 ** -----------------------------------------------------------------------------
31 */
32 #ifdef SCCS_LABELS
33 static char *_rioroute_c_sccs_ = "@(#)rioroute.c        1.3";
34 #endif
35
36 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/errno.h>
39 #include <asm/io.h>
40 #include <asm/system.h>
41 #include <asm/string.h>
42 #include <asm/uaccess.h>
43
44 #include <linux/termios.h>
45 #include <linux/serial.h>
46
47 #include <linux/generic_serial.h>
48
49
50 #include "linux_compat.h"
51 #include "rio_linux.h"
52 #include "pkt.h"
53 #include "daemon.h"
54 #include "rio.h"
55 #include "riospace.h"
56 #include "cmdpkt.h"
57 #include "map.h"
58 #include "rup.h"
59 #include "port.h"
60 #include "riodrvr.h"
61 #include "rioinfo.h"
62 #include "func.h"
63 #include "errors.h"
64 #include "pci.h"
65
66 #include "parmmap.h"
67 #include "unixrup.h"
68 #include "board.h"
69 #include "host.h"
70 #include "phb.h"
71 #include "link.h"
72 #include "cmdblk.h"
73 #include "route.h"
74 #include "cirrus.h"
75 #include "rioioctl.h"
76 #include "param.h"
77
78 static int RIOCheckIsolated(struct rio_info *, struct Host *, unsigned int);
79 static int RIOIsolate(struct rio_info *, struct Host *, unsigned int);
80 static int RIOCheck(struct Host *, unsigned int);
81 static void RIOConCon(struct rio_info *, struct Host *, unsigned int, unsigned int, unsigned int, unsigned int, int);
82
83
84 /*
85 ** Incoming on the ROUTE_RUP
86 ** I wrote this while I was tired. Forgive me.
87 */
88 int RIORouteRup(struct rio_info *p, unsigned int Rup, struct Host *HostP, struct PKT __iomem * PacketP)
89 {
90         struct PktCmd __iomem *PktCmdP = (struct PktCmd __iomem *) PacketP->data;
91         struct PktCmd_M *PktReplyP;
92         struct CmdBlk *CmdBlkP;
93         struct Port *PortP;
94         struct Map *MapP;
95         struct Top *TopP;
96         int ThisLink, ThisLinkMin, ThisLinkMax;
97         int port;
98         int Mod, Mod1, Mod2;
99         unsigned short RtaType;
100         unsigned int RtaUniq;
101         unsigned int ThisUnit, ThisUnit2;       /* 2 ids to accommodate 16 port RTA */
102         unsigned int OldUnit, NewUnit, OldLink, NewLink;
103         char *MyType, *MyName;
104         int Lies;
105         unsigned long flags;
106
107         /*
108          ** Is this unit telling us it's current link topology?
109          */
110         if (readb(&PktCmdP->Command) == ROUTE_TOPOLOGY) {
111                 MapP = HostP->Mapping;
112
113                 /*
114                  ** The packet can be sent either by the host or by an RTA.
115                  ** If it comes from the host, then we need to fill in the
116                  ** Topology array in the host structure. If it came in
117                  ** from an RTA then we need to fill in the Mapping structure's
118                  ** Topology array for the unit.
119                  */
120                 if (Rup >= (unsigned short) MAX_RUP) {
121                         ThisUnit = HOST_ID;
122                         TopP = HostP->Topology;
123                         MyType = "Host";
124                         MyName = HostP->Name;
125                         ThisLinkMin = ThisLinkMax = Rup - MAX_RUP;
126                 } else {
127                         ThisUnit = Rup + 1;
128                         TopP = HostP->Mapping[Rup].Topology;
129                         MyType = "RTA";
130                         MyName = HostP->Mapping[Rup].Name;
131                         ThisLinkMin = 0;
132                         ThisLinkMax = LINKS_PER_UNIT - 1;
133                 }
134
135                 /*
136                  ** Lies will not be tolerated.
137                  ** If any pair of links claim to be connected to the same
138                  ** place, then ignore this packet completely.
139                  */
140                 Lies = 0;
141                 for (ThisLink = ThisLinkMin + 1; ThisLink <= ThisLinkMax; ThisLink++) {
142                         /*
143                          ** it won't lie about network interconnect, total disconnects
144                          ** and no-IDs. (or at least, it doesn't *matter* if it does)
145                          */
146                         if (readb(&PktCmdP->RouteTopology[ThisLink].Unit) > (unsigned short) MAX_RUP)
147                                 continue;
148
149                         for (NewLink = ThisLinkMin; NewLink < ThisLink; NewLink++) {
150                                 if ((readb(&PktCmdP->RouteTopology[ThisLink].Unit) == readb(&PktCmdP->RouteTopology[NewLink].Unit)) && (readb(&PktCmdP->RouteTopology[ThisLink].Link) == readb(&PktCmdP->RouteTopology[NewLink].Link))) {
151                                         Lies++;
152                                 }
153                         }
154                 }
155
156                 if (Lies) {
157                         rio_dprintk(RIO_DEBUG_ROUTE, "LIES! DAMN LIES! %d LIES!\n", Lies);
158                         rio_dprintk(RIO_DEBUG_ROUTE, "%d:%c %d:%c %d:%c %d:%c\n",
159                                     readb(&PktCmdP->RouteTopology[0].Unit),
160                                     'A' + readb(&PktCmdP->RouteTopology[0].Link),
161                                     readb(&PktCmdP->RouteTopology[1].Unit),
162                                     'A' + readb(&PktCmdP->RouteTopology[1].Link), readb(&PktCmdP->RouteTopology[2].Unit), 'A' + readb(&PktCmdP->RouteTopology[2].Link), readb(&PktCmdP->RouteTopology[3].Unit), 'A' + readb(&PktCmdP->RouteTopology[3].Link));
163                         return 1;
164                 }
165
166                 /*
167                  ** now, process each link.
168                  */
169                 for (ThisLink = ThisLinkMin; ThisLink <= ThisLinkMax; ThisLink++) {
170                         /*
171                          ** this is what it was connected to
172                          */
173                         OldUnit = TopP[ThisLink].Unit;
174                         OldLink = TopP[ThisLink].Link;
175
176                         /*
177                          ** this is what it is now connected to
178                          */
179                         NewUnit = readb(&PktCmdP->RouteTopology[ThisLink].Unit);
180                         NewLink = readb(&PktCmdP->RouteTopology[ThisLink].Link);
181
182                         if (OldUnit != NewUnit || OldLink != NewLink) {
183                                 /*
184                                  ** something has changed!
185                                  */
186
187                                 if (NewUnit > MAX_RUP && NewUnit != ROUTE_DISCONNECT && NewUnit != ROUTE_NO_ID && NewUnit != ROUTE_INTERCONNECT) {
188                                         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);
189                                 } else {
190                                         /*
191                                          ** put the new values in
192                                          */
193                                         TopP[ThisLink].Unit = NewUnit;
194                                         TopP[ThisLink].Link = NewLink;
195
196                                         RIOSetChange(p);
197
198                                         if (OldUnit <= MAX_RUP) {
199                                                 /*
200                                                  ** If something has become bust, then re-enable them messages
201                                                  */
202                                                 if (!p->RIONoMessage)
203                                                         RIOConCon(p, HostP, ThisUnit, ThisLink, OldUnit, OldLink, DISCONNECT);
204                                         }
205
206                                         if ((NewUnit <= MAX_RUP) && !p->RIONoMessage)
207                                                 RIOConCon(p, HostP, ThisUnit, ThisLink, NewUnit, NewLink, CONNECT);
208
209                                         if (NewUnit == ROUTE_NO_ID)
210                                                 rio_dprintk(RIO_DEBUG_ROUTE, "%s %s (%c) is connected to an unconfigured unit.\n", MyType, MyName, 'A' + ThisLink);
211
212                                         if (NewUnit == ROUTE_INTERCONNECT) {
213                                                 if (!p->RIONoMessage)
214                                                         printk(KERN_DEBUG "rio: %s '%s' (%c) is connected to another network.\n", MyType, MyName, 'A' + ThisLink);
215                                         }
216
217                                         /*
218                                          ** perform an update for 'the other end', so that these messages
219                                          ** only appears once. Only disconnect the other end if it is pointing
220                                          ** at us!
221                                          */
222                                         if (OldUnit == HOST_ID) {
223                                                 if (HostP->Topology[OldLink].Unit == ThisUnit && HostP->Topology[OldLink].Link == ThisLink) {
224                                                         rio_dprintk(RIO_DEBUG_ROUTE, "SETTING HOST (%c) TO DISCONNECTED!\n", OldLink + 'A');
225                                                         HostP->Topology[OldLink].Unit = ROUTE_DISCONNECT;
226                                                         HostP->Topology[OldLink].Link = NO_LINK;
227                                                 } else {
228                                                         rio_dprintk(RIO_DEBUG_ROUTE, "HOST(%c) WAS NOT CONNECTED TO %s (%c)!\n", OldLink + 'A', HostP->Mapping[ThisUnit - 1].Name, ThisLink + 'A');
229                                                 }
230                                         } else if (OldUnit <= MAX_RUP) {
231                                                 if (HostP->Mapping[OldUnit - 1].Topology[OldLink].Unit == ThisUnit && HostP->Mapping[OldUnit - 1].Topology[OldLink].Link == ThisLink) {
232                                                         rio_dprintk(RIO_DEBUG_ROUTE, "SETTING RTA %s (%c) TO DISCONNECTED!\n", HostP->Mapping[OldUnit - 1].Name, OldLink + 'A');
233                                                         HostP->Mapping[OldUnit - 1].Topology[OldLink].Unit = ROUTE_DISCONNECT;
234                                                         HostP->Mapping[OldUnit - 1].Topology[OldLink].Link = NO_LINK;
235                                                 } else {
236                                                         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');
237                                                 }
238                                         }
239                                         if (NewUnit == HOST_ID) {
240                                                 rio_dprintk(RIO_DEBUG_ROUTE, "MARKING HOST (%c) CONNECTED TO %s (%c)\n", NewLink + 'A', MyName, ThisLink + 'A');
241                                                 HostP->Topology[NewLink].Unit = ThisUnit;
242                                                 HostP->Topology[NewLink].Link = ThisLink;
243                                         } else if (NewUnit <= MAX_RUP) {
244                                                 rio_dprintk(RIO_DEBUG_ROUTE, "MARKING RTA %s (%c) CONNECTED TO %s (%c)\n", HostP->Mapping[NewUnit - 1].Name, NewLink + 'A', MyName, ThisLink + 'A');
245                                                 HostP->Mapping[NewUnit - 1].Topology[NewLink].Unit = ThisUnit;
246                                                 HostP->Mapping[NewUnit - 1].Topology[NewLink].Link = ThisLink;
247                                         }
248                                 }
249                                 RIOSetChange(p);
250                                 RIOCheckIsolated(p, HostP, OldUnit);
251                         }
252                 }
253                 return 1;
254         }
255
256         /*
257          ** The only other command we recognise is a route_request command
258          */
259         if (readb(&PktCmdP->Command) != ROUTE_REQUEST) {
260                 rio_dprintk(RIO_DEBUG_ROUTE, "Unknown command %d received on rup %d host %p ROUTE_RUP\n", readb(&PktCmdP->Command), Rup, HostP);
261                 return 1;
262         }
263
264         RtaUniq = (readb(&PktCmdP->UniqNum[0])) + (readb(&PktCmdP->UniqNum[1]) << 8) + (readb(&PktCmdP->UniqNum[2]) << 16) + (readb(&PktCmdP->UniqNum[3]) << 24);
265
266         /*
267          ** Determine if 8 or 16 port RTA
268          */
269         RtaType = GetUnitType(RtaUniq);
270
271         rio_dprintk(RIO_DEBUG_ROUTE, "Received a request for an ID for serial number %x\n", RtaUniq);
272
273         Mod = readb(&PktCmdP->ModuleTypes);
274         Mod1 = LONYBLE(Mod);
275         if (RtaType == TYPE_RTA16) {
276                 /*
277                  ** Only one ident is set for a 16 port RTA. To make compatible
278                  ** with 8 port, set 2nd ident in Mod2 to the same as Mod1.
279                  */
280                 Mod2 = Mod1;
281                 rio_dprintk(RIO_DEBUG_ROUTE, "Backplane type is %s (all ports)\n", p->RIOModuleTypes[Mod1].Name);
282         } else {
283                 Mod2 = HINYBLE(Mod);
284                 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);
285         }
286
287         /*
288          ** try to unhook a command block from the command free list.
289          */
290         if (!(CmdBlkP = RIOGetCmdBlk())) {
291                 rio_dprintk(RIO_DEBUG_ROUTE, "No command blocks to route RTA! come back later.\n");
292                 return 0;
293         }
294
295         /*
296          ** Fill in the default info on the command block
297          */
298         CmdBlkP->Packet.dest_unit = Rup;
299         CmdBlkP->Packet.dest_port = ROUTE_RUP;
300         CmdBlkP->Packet.src_unit = HOST_ID;
301         CmdBlkP->Packet.src_port = ROUTE_RUP;
302         CmdBlkP->Packet.len = PKT_CMD_BIT | 1;
303         CmdBlkP->PreFuncP = CmdBlkP->PostFuncP = NULL;
304         PktReplyP = (struct PktCmd_M *) CmdBlkP->Packet.data;
305
306         if (!RIOBootOk(p, HostP, RtaUniq)) {
307                 rio_dprintk(RIO_DEBUG_ROUTE, "RTA %x tried to get an ID, but does not belong - FOAD it!\n", RtaUniq);
308                 PktReplyP->Command = ROUTE_FOAD;
309                 memcpy(PktReplyP->CommandText, "RT_FOAD", 7);
310                 RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
311                 return 1;
312         }
313
314         /*
315          ** Check to see if the RTA is configured for this host
316          */
317         for (ThisUnit = 0; ThisUnit < MAX_RUP; ThisUnit++) {
318                 rio_dprintk(RIO_DEBUG_ROUTE, "Entry %d Flags=%s %s UniqueNum=0x%x\n",
319                             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);
320
321                 /*
322                  ** We have an entry for it.
323                  */
324                 if ((HostP->Mapping[ThisUnit].Flags & (SLOT_IN_USE | SLOT_TENTATIVE)) && (HostP->Mapping[ThisUnit].RtaUniqueNum == RtaUniq)) {
325                         if (RtaType == TYPE_RTA16) {
326                                 ThisUnit2 = HostP->Mapping[ThisUnit].ID2 - 1;
327                                 rio_dprintk(RIO_DEBUG_ROUTE, "Found unit 0x%x at slots %d+%d\n", RtaUniq, ThisUnit, ThisUnit2);
328                         } else
329                                 rio_dprintk(RIO_DEBUG_ROUTE, "Found unit 0x%x at slot %d\n", RtaUniq, ThisUnit);
330                         /*
331                          ** If we have no knowledge of booting it, then the host has
332                          ** been re-booted, and so we must kill the RTA, so that it
333                          ** will be booted again (potentially with new bins)
334                          ** and it will then re-ask for an ID, which we will service.
335                          */
336                         if ((HostP->Mapping[ThisUnit].Flags & SLOT_IN_USE) && !(HostP->Mapping[ThisUnit].Flags & RTA_BOOTED)) {
337                                 if (!(HostP->Mapping[ThisUnit].Flags & MSG_DONE)) {
338                                         if (!p->RIONoMessage)
339                                                 printk(KERN_DEBUG "rio: RTA '%s' is being updated.\n", HostP->Mapping[ThisUnit].Name);
340                                         HostP->Mapping[ThisUnit].Flags |= MSG_DONE;
341                                 }
342                                 PktReplyP->Command = ROUTE_FOAD;
343                                 memcpy(PktReplyP->CommandText, "RT_FOAD", 7);
344                                 RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
345                                 return 1;
346                         }
347
348                         /*
349                          ** Send the ID (entry) to this RTA. The ID number is implicit as
350                          ** the offset into the table. It is worth noting at this stage
351                          ** that offset zero in the table contains the entries for the
352                          ** RTA with ID 1!!!!
353                          */
354                         PktReplyP->Command = ROUTE_ALLOCATE;
355                         PktReplyP->IDNum = ThisUnit + 1;
356                         if (RtaType == TYPE_RTA16) {
357                                 if (HostP->Mapping[ThisUnit].Flags & SLOT_IN_USE)
358                                         /*
359                                          ** Adjust the phb and tx pkt dest_units for 2nd block of 8
360                                          ** only if the RTA has ports associated (SLOT_IN_USE)
361                                          */
362                                         RIOFixPhbs(p, HostP, ThisUnit2);
363                                 PktReplyP->IDNum2 = ThisUnit2 + 1;
364                                 rio_dprintk(RIO_DEBUG_ROUTE, "RTA '%s' has been allocated IDs %d+%d\n", HostP->Mapping[ThisUnit].Name, PktReplyP->IDNum, PktReplyP->IDNum2);
365                         } else {
366                                 PktReplyP->IDNum2 = ROUTE_NO_ID;
367                                 rio_dprintk(RIO_DEBUG_ROUTE, "RTA '%s' has been allocated ID %d\n", HostP->Mapping[ThisUnit].Name, PktReplyP->IDNum);
368                         }
369                         memcpy(PktReplyP->CommandText, "RT_ALLOCAT", 10);
370
371                         RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
372
373                         /*
374                          ** If this is a freshly booted RTA, then we need to re-open
375                          ** the ports, if any where open, so that data may once more
376                          ** flow around the system!
377                          */
378                         if ((HostP->Mapping[ThisUnit].Flags & RTA_NEWBOOT) && (HostP->Mapping[ThisUnit].SysPort != NO_PORT)) {
379                                 /*
380                                  ** look at the ports associated with this beast and
381                                  ** see if any where open. If they was, then re-open
382                                  ** them, using the info from the tty flags.
383                                  */
384                                 for (port = 0; port < PORTS_PER_RTA; port++) {
385                                         PortP = p->RIOPortp[port + HostP->Mapping[ThisUnit].SysPort];
386                                         if (PortP->State & (RIO_MOPEN | RIO_LOPEN)) {
387                                                 rio_dprintk(RIO_DEBUG_ROUTE, "Re-opened this port\n");
388                                                 rio_spin_lock_irqsave(&PortP->portSem, flags);
389                                                 PortP->MagicFlags |= MAGIC_REBOOT;
390                                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
391                                         }
392                                 }
393                                 if (RtaType == TYPE_RTA16) {
394                                         for (port = 0; port < PORTS_PER_RTA; port++) {
395                                                 PortP = p->RIOPortp[port + HostP->Mapping[ThisUnit2].SysPort];
396                                                 if (PortP->State & (RIO_MOPEN | RIO_LOPEN)) {
397                                                         rio_dprintk(RIO_DEBUG_ROUTE, "Re-opened this port\n");
398                                                         rio_spin_lock_irqsave(&PortP->portSem, flags);
399                                                         PortP->MagicFlags |= MAGIC_REBOOT;
400                                                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
401                                                 }
402                                         }
403                                 }
404                         }
405
406                         /*
407                          ** keep a copy of the module types!
408                          */
409                         HostP->UnixRups[ThisUnit].ModTypes = Mod;
410                         if (RtaType == TYPE_RTA16)
411                                 HostP->UnixRups[ThisUnit2].ModTypes = Mod;
412
413                         /*
414                          ** If either of the modules on this unit is read-only or write-only
415                          ** or none-xprint, then we need to transfer that info over to the
416                          ** relevant ports.
417                          */
418                         if (HostP->Mapping[ThisUnit].SysPort != NO_PORT) {
419                                 for (port = 0; port < PORTS_PER_MODULE; port++) {
420                                         p->RIOPortp[port + HostP->Mapping[ThisUnit].SysPort]->Config &= ~RIO_NOMASK;
421                                         p->RIOPortp[port + HostP->Mapping[ThisUnit].SysPort]->Config |= p->RIOModuleTypes[Mod1].Flags[port];
422                                         p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit].SysPort]->Config &= ~RIO_NOMASK;
423                                         p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit].SysPort]->Config |= p->RIOModuleTypes[Mod2].Flags[port];
424                                 }
425                                 if (RtaType == TYPE_RTA16) {
426                                         for (port = 0; port < PORTS_PER_MODULE; port++) {
427                                                 p->RIOPortp[port + HostP->Mapping[ThisUnit2].SysPort]->Config &= ~RIO_NOMASK;
428                                                 p->RIOPortp[port + HostP->Mapping[ThisUnit2].SysPort]->Config |= p->RIOModuleTypes[Mod1].Flags[port];
429                                                 p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit2].SysPort]->Config &= ~RIO_NOMASK;
430                                                 p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit2].SysPort]->Config |= p->RIOModuleTypes[Mod2].Flags[port];
431                                         }
432                                 }
433                         }
434
435                         /*
436                          ** Job done, get on with the interrupts!
437                          */
438                         return 1;
439                 }
440         }
441         /*
442          ** There is no table entry for this RTA at all.
443          **
444          ** Lets check to see if we actually booted this unit - if not,
445          ** then we reset it and it will go round the loop of being booted
446          ** we can then worry about trying to fit it into the table.
447          */
448         for (ThisUnit = 0; ThisUnit < HostP->NumExtraBooted; ThisUnit++)
449                 if (HostP->ExtraUnits[ThisUnit] == RtaUniq)
450                         break;
451         if (ThisUnit == HostP->NumExtraBooted && ThisUnit != MAX_EXTRA_UNITS) {
452                 /*
453                  ** if the unit wasn't in the table, and the table wasn't full, then
454                  ** we reset the unit, because we didn't boot it.
455                  ** However, if the table is full, it could be that we did boot
456                  ** this unit, and so we won't reboot it, because it isn't really
457                  ** all that disasterous to keep the old bins in most cases. This
458                  ** is a rather tacky feature, but we are on the edge of reallity
459                  ** here, because the implication is that someone has connected
460                  ** 16+MAX_EXTRA_UNITS onto one host.
461                  */
462                 static int UnknownMesgDone = 0;
463
464                 if (!UnknownMesgDone) {
465                         if (!p->RIONoMessage)
466                                 printk(KERN_DEBUG "rio: One or more unknown RTAs are being updated.\n");
467                         UnknownMesgDone = 1;
468                 }
469
470                 PktReplyP->Command = ROUTE_FOAD;
471                 memcpy(PktReplyP->CommandText, "RT_FOAD", 7);
472         } else {
473                 /*
474                  ** we did boot it (as an extra), and there may now be a table
475                  ** slot free (because of a delete), so we will try to make
476                  ** a tentative entry for it, so that the configurator can see it
477                  ** and fill in the details for us.
478                  */
479                 if (RtaType == TYPE_RTA16) {
480                         if (RIOFindFreeID(p, HostP, &ThisUnit, &ThisUnit2) == 0) {
481                                 RIODefaultName(p, HostP, ThisUnit);
482                                 rio_fill_host_slot(ThisUnit, ThisUnit2, RtaUniq, HostP);
483                         }
484                 } else {
485                         if (RIOFindFreeID(p, HostP, &ThisUnit, NULL) == 0) {
486                                 RIODefaultName(p, HostP, ThisUnit);
487                                 rio_fill_host_slot(ThisUnit, 0, RtaUniq, HostP);
488                         }
489                 }
490                 PktReplyP->Command = ROUTE_USED;
491                 memcpy(PktReplyP->CommandText, "RT_USED", 7);
492         }
493         RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
494         return 1;
495 }
496
497
498 void RIOFixPhbs(struct rio_info *p, struct Host *HostP, unsigned int unit)
499 {
500         unsigned short link, port;
501         struct Port *PortP;
502         unsigned long flags;
503         int PortN = HostP->Mapping[unit].SysPort;
504
505         rio_dprintk(RIO_DEBUG_ROUTE, "RIOFixPhbs unit %d sysport %d\n", unit, PortN);
506
507         if (PortN != -1) {
508                 unsigned short dest_unit = HostP->Mapping[unit].ID2;
509
510                 /*
511                  ** Get the link number used for the 1st 8 phbs on this unit.
512                  */
513                 PortP = p->RIOPortp[HostP->Mapping[dest_unit - 1].SysPort];
514
515                 link = readw(&PortP->PhbP->link);
516
517                 for (port = 0; port < PORTS_PER_RTA; port++, PortN++) {
518                         unsigned short dest_port = port + 8;
519                         u16 __iomem *TxPktP;
520                         struct PKT __iomem *Pkt;
521
522                         PortP = p->RIOPortp[PortN];
523
524                         rio_spin_lock_irqsave(&PortP->portSem, flags);
525                         /*
526                          ** If RTA is not powered on, the tx packets will be
527                          ** unset, so go no further.
528                          */
529                         if (PortP->TxStart == 0) {
530                                 rio_dprintk(RIO_DEBUG_ROUTE, "Tx pkts not set up yet\n");
531                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
532                                 break;
533                         }
534
535                         /*
536                          ** For the second slot of a 16 port RTA, the driver needs to
537                          ** sort out the phb to port mappings. The dest_unit for this
538                          ** group of 8 phbs is set to the dest_unit of the accompanying
539                          ** 8 port block. The dest_port of the second unit is set to
540                          ** be in the range 8-15 (i.e. 8 is added). Thus, for a 16 port
541                          ** RTA with IDs 5 and 6, traffic bound for port 6 of unit 6
542                          ** (being the second map ID) will be sent to dest_unit 5, port
543                          ** 14. When this RTA is deleted, dest_unit for ID 6 will be
544                          ** restored, and the dest_port will be reduced by 8.
545                          ** Transmit packets also have a destination field which needs
546                          ** adjusting in the same manner.
547                          ** Note that the unit/port bytes in 'dest' are swapped.
548                          ** We also need to adjust the phb and rup link numbers for the
549                          ** second block of 8 ttys.
550                          */
551                         for (TxPktP = PortP->TxStart; TxPktP <= PortP->TxEnd; TxPktP++) {
552                                 /*
553                                  ** *TxPktP is the pointer to the transmit packet on the host
554                                  ** card. This needs to be translated into a 32 bit pointer
555                                  ** so it can be accessed from the driver.
556                                  */
557                                 Pkt = (struct PKT __iomem *) RIO_PTR(HostP->Caddr, readw(TxPktP));
558
559                                 /*
560                                  ** If the packet is used, reset it.
561                                  */
562                                 Pkt = (struct PKT __iomem *) ((unsigned long) Pkt & ~PKT_IN_USE);
563                                 writeb(dest_unit, &Pkt->dest_unit);
564                                 writeb(dest_port, &Pkt->dest_port);
565                         }
566                         rio_dprintk(RIO_DEBUG_ROUTE, "phb dest: Old %x:%x New %x:%x\n", readw(&PortP->PhbP->destination) & 0xff, (readw(&PortP->PhbP->destination) >> 8) & 0xff, dest_unit, dest_port);
567                         writew(dest_unit + (dest_port << 8), &PortP->PhbP->destination);
568                         writew(link, &PortP->PhbP->link);
569
570                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
571                 }
572                 /*
573                  ** Now make sure the range of ports to be serviced includes
574                  ** the 2nd 8 on this 16 port RTA.
575                  */
576                 if (link > 3)
577                         return;
578                 if (((unit * 8) + 7) > readw(&HostP->LinkStrP[link].last_port)) {
579                         rio_dprintk(RIO_DEBUG_ROUTE, "last port on host link %d: %d\n", link, (unit * 8) + 7);
580                         writew((unit * 8) + 7, &HostP->LinkStrP[link].last_port);
581                 }
582         }
583 }
584
585 /*
586 ** Check to see if the new disconnection has isolated this unit.
587 ** If it has, then invalidate all its link information, and tell
588 ** the world about it. This is done to ensure that the configurator
589 ** only gets up-to-date information about what is going on.
590 */
591 static int RIOCheckIsolated(struct rio_info *p, struct Host *HostP, unsigned int UnitId)
592 {
593         unsigned long flags;
594         rio_spin_lock_irqsave(&HostP->HostLock, flags);
595
596         if (RIOCheck(HostP, UnitId)) {
597                 rio_dprintk(RIO_DEBUG_ROUTE, "Unit %d is NOT isolated\n", UnitId);
598                 rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
599                 return (0);
600         }
601
602         RIOIsolate(p, HostP, UnitId);
603         RIOSetChange(p);
604         rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
605         return 1;
606 }
607
608 /*
609 ** Invalidate all the link interconnectivity of this unit, and of
610 ** all the units attached to it. This will mean that the entire
611 ** subnet will re-introduce itself.
612 */
613 static int RIOIsolate(struct rio_info *p, struct Host *HostP, unsigned int UnitId)
614 {
615         unsigned int link, unit;
616
617         UnitId--;               /* this trick relies on the Unit Id being UNSIGNED! */
618
619         if (UnitId >= MAX_RUP)  /* dontcha just lurv unsigned maths! */
620                 return (0);
621
622         if (HostP->Mapping[UnitId].Flags & BEEN_HERE)
623                 return (0);
624
625         HostP->Mapping[UnitId].Flags |= BEEN_HERE;
626
627         if (p->RIOPrintDisabled == DO_PRINT)
628                 rio_dprintk(RIO_DEBUG_ROUTE, "RIOMesgIsolated %s", HostP->Mapping[UnitId].Name);
629
630         for (link = 0; link < LINKS_PER_UNIT; link++) {
631                 unit = HostP->Mapping[UnitId].Topology[link].Unit;
632                 HostP->Mapping[UnitId].Topology[link].Unit = ROUTE_DISCONNECT;
633                 HostP->Mapping[UnitId].Topology[link].Link = NO_LINK;
634                 RIOIsolate(p, HostP, unit);
635         }
636         HostP->Mapping[UnitId].Flags &= ~BEEN_HERE;
637         return 1;
638 }
639
640 static int RIOCheck(struct Host *HostP, unsigned int UnitId)
641 {
642         unsigned char link;
643
644 /*      rio_dprint(RIO_DEBUG_ROUTE, ("Check to see if unit %d has a route to the host\n",UnitId)); */
645         rio_dprintk(RIO_DEBUG_ROUTE, "RIOCheck : UnitID = %d\n", UnitId);
646
647         if (UnitId == HOST_ID) {
648                 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d is NOT isolated - it IS the host!\n", UnitId)); */
649                 return 1;
650         }
651
652         UnitId--;
653
654         if (UnitId >= MAX_RUP) {
655                 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d - ignored.\n", UnitId)); */
656                 return 0;
657         }
658
659         for (link = 0; link < LINKS_PER_UNIT; link++) {
660                 if (HostP->Mapping[UnitId].Topology[link].Unit == HOST_ID) {
661                         /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d is connected directly to host via link (%c).\n", 
662                            UnitId, 'A'+link)); */
663                         return 1;
664                 }
665         }
666
667         if (HostP->Mapping[UnitId].Flags & BEEN_HERE) {
668                 /* rio_dprint(RIO_DEBUG_ROUTE, ("Been to Unit %d before - ignoring\n", UnitId)); */
669                 return 0;
670         }
671
672         HostP->Mapping[UnitId].Flags |= BEEN_HERE;
673
674         for (link = 0; link < LINKS_PER_UNIT; link++) {
675                 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d check link (%c)\n", UnitId,'A'+link)); */
676                 if (RIOCheck(HostP, HostP->Mapping[UnitId].Topology[link].Unit)) {
677                         /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d is connected to something that knows the host via link (%c)\n", UnitId,link+'A')); */
678                         HostP->Mapping[UnitId].Flags &= ~BEEN_HERE;
679                         return 1;
680                 }
681         }
682
683         HostP->Mapping[UnitId].Flags &= ~BEEN_HERE;
684
685         /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d DOESNT KNOW THE HOST!\n", UnitId)); */
686
687         return 0;
688 }
689
690 /*
691 ** Returns the type of unit (host, 16/8 port RTA)
692 */
693
694 unsigned int GetUnitType(unsigned int Uniq)
695 {
696         switch ((Uniq >> 28) & 0xf) {
697         case RIO_AT:
698         case RIO_MCA:
699         case RIO_EISA:
700         case RIO_PCI:
701                 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: Host\n");
702                 return (TYPE_HOST);
703         case RIO_RTA_16:
704                 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: 16 port RTA\n");
705                 return (TYPE_RTA16);
706         case RIO_RTA:
707                 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: 8 port RTA\n");
708                 return (TYPE_RTA8);
709         default:
710                 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: Unrecognised\n");
711                 return (99);
712         }
713 }
714
715 int RIOSetChange(struct rio_info *p)
716 {
717         if (p->RIOQuickCheck != NOT_CHANGED)
718                 return (0);
719         p->RIOQuickCheck = CHANGED;
720         if (p->RIOSignalProcess) {
721                 rio_dprintk(RIO_DEBUG_ROUTE, "Send SIG-HUP");
722                 /*
723                    psignal( RIOSignalProcess, SIGHUP );
724                  */
725         }
726         return (0);
727 }
728
729 static void RIOConCon(struct rio_info *p,
730                       struct Host *HostP,
731                       unsigned int FromId,
732                       unsigned int FromLink,
733                       unsigned int ToId,
734                       unsigned int ToLink,
735                       int Change)
736 {
737         char *FromName;
738         char *FromType;
739         char *ToName;
740         char *ToType;
741         unsigned int tp;
742
743 /*
744 ** 15.10.1998 ARG - ESIL 0759
745 ** (Part) fix for port being trashed when opened whilst RTA "disconnected"
746 **
747 ** What's this doing in here anyway ?
748 ** It was causing the port to be 'unmapped' if opened whilst RTA "disconnected"
749 **
750 ** 09.12.1998 ARG - ESIL 0776 - part fix
751 ** Okay, We've found out what this was all about now !
752 ** Someone had botched this to use RIOHalted to indicated the number of RTAs
753 ** 'disconnected'. The value in RIOHalted was then being used in the
754 ** 'RIO_QUICK_CHECK' ioctl. A none zero value indicating that a least one RTA
755 ** is 'disconnected'. The change was put in to satisfy a customer's needs.
756 ** Having taken this bit of code out 'RIO_QUICK_CHECK' now no longer works for
757 ** the customer.
758 **
759     if (Change == CONNECT) {
760                 if (p->RIOHalted) p->RIOHalted --;
761          }
762          else {
763                 p->RIOHalted ++;
764          }
765 **
766 ** So - we need to implement it slightly differently - a new member of the
767 ** rio_info struct - RIORtaDisCons (RIO RTA connections) keeps track of RTA
768 ** connections and disconnections. 
769 */
770         if (Change == CONNECT) {
771                 if (p->RIORtaDisCons)
772                         p->RIORtaDisCons--;
773         } else {
774                 p->RIORtaDisCons++;
775         }
776
777         if (p->RIOPrintDisabled == DONT_PRINT)
778                 return;
779
780         if (FromId > ToId) {
781                 tp = FromId;
782                 FromId = ToId;
783                 ToId = tp;
784                 tp = FromLink;
785                 FromLink = ToLink;
786                 ToLink = tp;
787         }
788
789         FromName = FromId ? HostP->Mapping[FromId - 1].Name : HostP->Name;
790         FromType = FromId ? "RTA" : "HOST";
791         ToName = ToId ? HostP->Mapping[ToId - 1].Name : HostP->Name;
792         ToType = ToId ? "RTA" : "HOST";
793
794         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");
795         printk(KERN_DEBUG "rio: Link between %s '%s' (%c) and %s '%s' (%c) %s.\n", FromType, FromName, 'A' + FromLink, ToType, ToName, 'A' + ToLink, (Change == CONNECT) ? "established" : "disconnected");
796 }
797
798 /*
799 ** RIORemoveFromSavedTable :
800 **
801 ** Delete and RTA entry from the saved table given to us
802 ** by the configuration program.
803 */
804 static int RIORemoveFromSavedTable(struct rio_info *p, struct Map *pMap)
805 {
806         int entry;
807
808         /*
809          ** We loop for all entries even after finding an entry and
810          ** zeroing it because we may have two entries to delete if
811          ** it's a 16 port RTA.
812          */
813         for (entry = 0; entry < TOTAL_MAP_ENTRIES; entry++) {
814                 if (p->RIOSavedTable[entry].RtaUniqueNum == pMap->RtaUniqueNum) {
815                         memset(&p->RIOSavedTable[entry], 0, sizeof(struct Map));
816                 }
817         }
818         return 0;
819 }
820
821
822 /*
823 ** RIOCheckDisconnected :
824 **
825 ** Scan the unit links to and return zero if the unit is completely
826 ** disconnected.
827 */
828 static int RIOFreeDisconnected(struct rio_info *p, struct Host *HostP, int unit)
829 {
830         int link;
831
832
833         rio_dprintk(RIO_DEBUG_ROUTE, "RIOFreeDisconnect unit %d\n", unit);
834         /*
835          ** If the slot is tentative and does not belong to the
836          ** second half of a 16 port RTA then scan to see if
837          ** is disconnected.
838          */
839         for (link = 0; link < LINKS_PER_UNIT; link++) {
840                 if (HostP->Mapping[unit].Topology[link].Unit != ROUTE_DISCONNECT)
841                         break;
842         }
843
844         /*
845          ** If not all links are disconnected then we can forget about it.
846          */
847         if (link < LINKS_PER_UNIT)
848                 return 1;
849
850 #ifdef NEED_TO_FIX_THIS
851         /* Ok so all the links are disconnected. But we may have only just
852          ** made this slot tentative and not yet received a topology update.
853          ** Lets check how long ago we made it tentative.
854          */
855         rio_dprintk(RIO_DEBUG_ROUTE, "Just about to check LBOLT on entry %d\n", unit);
856         if (drv_getparm(LBOLT, (ulong_t *) & current_time))
857                 rio_dprintk(RIO_DEBUG_ROUTE, "drv_getparm(LBOLT,....) Failed.\n");
858
859         elapse_time = current_time - TentTime[unit];
860         rio_dprintk(RIO_DEBUG_ROUTE, "elapse %d = current %d - tent %d (%d usec)\n", elapse_time, current_time, TentTime[unit], drv_hztousec(elapse_time));
861         if (drv_hztousec(elapse_time) < WAIT_TO_FINISH) {
862                 rio_dprintk(RIO_DEBUG_ROUTE, "Skipping slot %d, not timed out yet %d\n", unit, drv_hztousec(elapse_time));
863                 return 1;
864         }
865 #endif
866
867         /*
868          ** We have found an usable slot.
869          ** If it is half of a 16 port RTA then delete the other half.
870          */
871         if (HostP->Mapping[unit].ID2 != 0) {
872                 int nOther = (HostP->Mapping[unit].ID2) - 1;
873
874                 rio_dprintk(RIO_DEBUG_ROUTE, "RioFreedis second slot %d.\n", nOther);
875                 memset(&HostP->Mapping[nOther], 0, sizeof(struct Map));
876         }
877         RIORemoveFromSavedTable(p, &HostP->Mapping[unit]);
878
879         return 0;
880 }
881
882
883 /*
884 ** RIOFindFreeID :
885 **
886 ** This function scans the given host table for either one
887 ** or two free unit ID's.
888 */
889
890 int RIOFindFreeID(struct rio_info *p, struct Host *HostP, unsigned int * pID1, unsigned int * pID2)
891 {
892         int unit, tempID;
893
894         /*
895          ** Initialise the ID's to MAX_RUP.
896          ** We do this to make the loop for setting the ID's as simple as
897          ** possible.
898          */
899         *pID1 = MAX_RUP;
900         if (pID2 != NULL)
901                 *pID2 = MAX_RUP;
902
903         /*
904          ** Scan all entries of the host mapping table for free slots.
905          ** We scan for free slots first and then if that is not successful
906          ** we start all over again looking for tentative slots we can re-use.
907          */
908         for (unit = 0; unit < MAX_RUP; unit++) {
909                 rio_dprintk(RIO_DEBUG_ROUTE, "Scanning unit %d\n", unit);
910                 /*
911                  ** If the flags are zero then the slot is empty.
912                  */
913                 if (HostP->Mapping[unit].Flags == 0) {
914                         rio_dprintk(RIO_DEBUG_ROUTE, "      This slot is empty.\n");
915                         /*
916                          ** If we haven't allocated the first ID then do it now.
917                          */
918                         if (*pID1 == MAX_RUP) {
919                                 rio_dprintk(RIO_DEBUG_ROUTE, "Make tentative entry for first unit %d\n", unit);
920                                 *pID1 = unit;
921
922                                 /*
923                                  ** If the second ID is not needed then we can return
924                                  ** now.
925                                  */
926                                 if (pID2 == NULL)
927                                         return 0;
928                         } else {
929                                 /*
930                                  ** Allocate the second slot and return.
931                                  */
932                                 rio_dprintk(RIO_DEBUG_ROUTE, "Make tentative entry for second unit %d\n", unit);
933                                 *pID2 = unit;
934                                 return 0;
935                         }
936                 }
937         }
938
939         /*
940          ** If we manage to come out of the free slot loop then we
941          ** need to start all over again looking for tentative slots
942          ** that we can re-use.
943          */
944         rio_dprintk(RIO_DEBUG_ROUTE, "Starting to scan for tentative slots\n");
945         for (unit = 0; unit < MAX_RUP; unit++) {
946                 if (((HostP->Mapping[unit].Flags & SLOT_TENTATIVE) || (HostP->Mapping[unit].Flags == 0)) && !(HostP->Mapping[unit].Flags & RTA16_SECOND_SLOT)) {
947                         rio_dprintk(RIO_DEBUG_ROUTE, "    Slot %d looks promising.\n", unit);
948
949                         if (unit == *pID1) {
950                                 rio_dprintk(RIO_DEBUG_ROUTE, "    No it isn't, its the 1st half\n");
951                                 continue;
952                         }
953
954                         /*
955                          ** Slot is Tentative or Empty, but not a tentative second
956                          ** slot of a 16 porter.
957                          ** Attempt to free up this slot (and its parnter if
958                          ** it is a 16 port slot. The second slot will become
959                          ** empty after a call to RIOFreeDisconnected so thats why
960                          ** we look for empty slots above  as well).
961                          */
962                         if (HostP->Mapping[unit].Flags != 0)
963                                 if (RIOFreeDisconnected(p, HostP, unit) != 0)
964                                         continue;
965                         /*
966                          ** If we haven't allocated the first ID then do it now.
967                          */
968                         if (*pID1 == MAX_RUP) {
969                                 rio_dprintk(RIO_DEBUG_ROUTE, "Grab tentative entry for first unit %d\n", unit);
970                                 *pID1 = unit;
971
972                                 /*
973                                  ** Clear out this slot now that we intend to use it.
974                                  */
975                                 memset(&HostP->Mapping[unit], 0, sizeof(struct Map));
976
977                                 /*
978                                  ** If the second ID is not needed then we can return
979                                  ** now.
980                                  */
981                                 if (pID2 == NULL)
982                                         return 0;
983                         } else {
984                                 /*
985                                  ** Allocate the second slot and return.
986                                  */
987                                 rio_dprintk(RIO_DEBUG_ROUTE, "Grab tentative/empty  entry for second unit %d\n", unit);
988                                 *pID2 = unit;
989
990                                 /*
991                                  ** Clear out this slot now that we intend to use it.
992                                  */
993                                 memset(&HostP->Mapping[unit], 0, sizeof(struct Map));
994
995                                 /* At this point under the right(wrong?) conditions
996                                  ** we may have a first unit ID being higher than the
997                                  ** second unit ID. This is a bad idea if we are about
998                                  ** to fill the slots with a 16 port RTA.
999                                  ** Better check and swap them over.
1000                                  */
1001
1002                                 if (*pID1 > *pID2) {
1003                                         rio_dprintk(RIO_DEBUG_ROUTE, "Swapping IDS %d %d\n", *pID1, *pID2);
1004                                         tempID = *pID1;
1005                                         *pID1 = *pID2;
1006                                         *pID2 = tempID;
1007                                 }
1008                                 return 0;
1009                         }
1010                 }
1011         }
1012
1013         /*
1014          ** If we manage to get to the end of the second loop then we
1015          ** can give up and return a failure.
1016          */
1017         return 1;
1018 }
1019
1020
1021 /*
1022 ** The link switch scenario.
1023 **
1024 ** Rta Wun (A) is connected to Tuw (A).
1025 ** The tables are all up to date, and the system is OK.
1026 **
1027 ** If Wun (A) is now moved to Wun (B) before Wun (A) can
1028 ** become disconnected, then the follow happens:
1029 **
1030 ** Tuw (A) spots the change of unit:link at the other end
1031 ** of its link and Tuw sends a topology packet reflecting
1032 ** the change: Tuw (A) now disconnected from Wun (A), and
1033 ** this is closely followed by a packet indicating that 
1034 ** Tuw (A) is now connected to Wun (B).
1035 **
1036 ** Wun (B) will spot that it has now become connected, and
1037 ** Wun will send a topology packet, which indicates that
1038 ** both Wun (A) and Wun (B) is connected to Tuw (A).
1039 **
1040 ** Eventually Wun (A) realises that it is now disconnected
1041 ** and Wun will send out a topology packet indicating that
1042 ** Wun (A) is now disconnected.
1043 */