Merge branch 'powerpc-next' of master.kernel.org:/pub/scm/linux/kernel/git/galak...
[linux-2.6] / drivers / char / rio / riotable.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          : riotable.c
24 **      SID             : 1.2
25 **      Last Modified   : 11/6/98 10:33:47
26 **      Retrieved       : 11/6/98 10:33:50
27 **
28 **  ident @(#)riotable.c        1.2
29 **
30 ** -----------------------------------------------------------------------------
31 */
32 #ifdef SCCS_LABELS
33 static char *_riotable_c_sccs_ = "@(#)riotable.c        1.2";
34 #endif
35
36 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/errno.h>
39 #include <linux/interrupt.h>
40 #include <linux/string.h>
41
42 #include <asm/io.h>
43 #include <asm/system.h>
44 #include <asm/string.h>
45 #include <asm/uaccess.h>
46
47 #include <linux/termios.h>
48 #include <linux/serial.h>
49
50 #include <linux/generic_serial.h>
51
52
53 #include "linux_compat.h"
54 #include "rio_linux.h"
55 #include "pkt.h"
56 #include "daemon.h"
57 #include "rio.h"
58 #include "riospace.h"
59 #include "cmdpkt.h"
60 #include "map.h"
61 #include "rup.h"
62 #include "port.h"
63 #include "riodrvr.h"
64 #include "rioinfo.h"
65 #include "func.h"
66 #include "errors.h"
67 #include "pci.h"
68
69 #include "parmmap.h"
70 #include "unixrup.h"
71 #include "board.h"
72 #include "host.h"
73 #include "phb.h"
74 #include "link.h"
75 #include "cmdblk.h"
76 #include "route.h"
77 #include "cirrus.h"
78 #include "rioioctl.h"
79 #include "param.h"
80 #include "protsts.h"
81
82 /*
83 ** A configuration table has been loaded. It is now up to us
84 ** to sort it out and use the information contained therein.
85 */
86 int RIONewTable(struct rio_info *p)
87 {
88         int Host, Host1, Host2, NameIsUnique, Entry, SubEnt;
89         struct Map *MapP;
90         struct Map *HostMapP;
91         struct Host *HostP;
92
93         char *cptr;
94
95         /*
96          ** We have been sent a new table to install. We need to break
97          ** it down into little bits and spread it around a bit to see
98          ** what we have got.
99          */
100         /*
101          ** Things to check:
102          ** (things marked 'xx' aren't checked any more!)
103          ** (1) That there are no booted Hosts/RTAs out there.
104          ** (2) That the names are properly formed
105          ** (3) That blank entries really are.
106          ** xx (4)      That hosts mentioned in the table actually exist. xx
107          ** (5) That the IDs are unique (per host).
108          ** (6) That host IDs are zero
109          ** (7) That port numbers are valid
110          ** (8) That port numbers aren't duplicated
111          ** (9) That names aren't duplicated
112          ** xx (10) That hosts that actually exist are mentioned in the table. xx
113          */
114         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(1)\n");
115         if (p->RIOSystemUp) {   /* (1) */
116                 p->RIOError.Error = HOST_HAS_ALREADY_BEEN_BOOTED;
117                 return -EBUSY;
118         }
119
120         p->RIOError.Error = NOTHING_WRONG_AT_ALL;
121         p->RIOError.Entry = -1;
122         p->RIOError.Other = -1;
123
124         for (Entry = 0; Entry < TOTAL_MAP_ENTRIES; Entry++) {
125                 MapP = &p->RIOConnectTable[Entry];
126                 if ((MapP->Flags & RTA16_SECOND_SLOT) == 0) {
127                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(2)\n");
128                         cptr = MapP->Name;      /* (2) */
129                         cptr[MAX_NAME_LEN - 1] = '\0';
130                         if (cptr[0] == '\0') {
131                                 memcpy(MapP->Name, MapP->RtaUniqueNum ? "RTA    NN" : "HOST NN", 8);
132                                 MapP->Name[5] = '0' + Entry / 10;
133                                 MapP->Name[6] = '0' + Entry % 10;
134                         }
135                         while (*cptr) {
136                                 if (*cptr < ' ' || *cptr > '~') {
137                                         p->RIOError.Error = BAD_CHARACTER_IN_NAME;
138                                         p->RIOError.Entry = Entry;
139                                         return -ENXIO;
140                                 }
141                                 cptr++;
142                         }
143                 }
144
145                 /*
146                  ** If the entry saved was a tentative entry then just forget
147                  ** about it.
148                  */
149                 if (MapP->Flags & SLOT_TENTATIVE) {
150                         MapP->HostUniqueNum = 0;
151                         MapP->RtaUniqueNum = 0;
152                         continue;
153                 }
154
155                 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(3)\n");
156                 if (!MapP->RtaUniqueNum && !MapP->HostUniqueNum) {      /* (3) */
157                         if (MapP->ID || MapP->SysPort || MapP->Flags) {
158                                 rio_dprintk(RIO_DEBUG_TABLE, "%s pretending to be empty but isn't\n", MapP->Name);
159                                 p->RIOError.Error = TABLE_ENTRY_ISNT_PROPERLY_NULL;
160                                 p->RIOError.Entry = Entry;
161                                 return -ENXIO;
162                         }
163                         rio_dprintk(RIO_DEBUG_TABLE, "!RIO: Daemon: test (3) passes\n");
164                         continue;
165                 }
166
167                 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(4)\n");
168                 for (Host = 0; Host < p->RIONumHosts; Host++) { /* (4) */
169                         if (p->RIOHosts[Host].UniqueNum == MapP->HostUniqueNum) {
170                                 HostP = &p->RIOHosts[Host];
171                                 /*
172                                  ** having done the lookup, we don't really want to do
173                                  ** it again, so hang the host number in a safe place
174                                  */
175                                 MapP->Topology[0].Unit = Host;
176                                 break;
177                         }
178                 }
179
180                 if (Host >= p->RIONumHosts) {
181                         rio_dprintk(RIO_DEBUG_TABLE, "RTA %s has unknown host unique number 0x%x\n", MapP->Name, MapP->HostUniqueNum);
182                         MapP->HostUniqueNum = 0;
183                         /* MapP->RtaUniqueNum   = 0; */
184                         /* MapP->ID                     = 0; */
185                         /* MapP->Flags           = 0; */
186                         /* MapP->SysPort                 = 0; */
187                         /* MapP->Name[0]                 = 0; */
188                         continue;
189                 }
190
191                 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(5)\n");
192                 if (MapP->RtaUniqueNum) {       /* (5) */
193                         if (!MapP->ID) {
194                                 rio_dprintk(RIO_DEBUG_TABLE, "RIO: RTA %s has been allocated an ID of zero!\n", MapP->Name);
195                                 p->RIOError.Error = ZERO_RTA_ID;
196                                 p->RIOError.Entry = Entry;
197                                 return -ENXIO;
198                         }
199                         if (MapP->ID > MAX_RUP) {
200                                 rio_dprintk(RIO_DEBUG_TABLE, "RIO: RTA %s has been allocated an invalid ID %d\n", MapP->Name, MapP->ID);
201                                 p->RIOError.Error = ID_NUMBER_OUT_OF_RANGE;
202                                 p->RIOError.Entry = Entry;
203                                 return -ENXIO;
204                         }
205                         for (SubEnt = 0; SubEnt < Entry; SubEnt++) {
206                                 if (MapP->HostUniqueNum == p->RIOConnectTable[SubEnt].HostUniqueNum && MapP->ID == p->RIOConnectTable[SubEnt].ID) {
207                                         rio_dprintk(RIO_DEBUG_TABLE, "Dupl. ID number allocated to RTA %s and RTA %s\n", MapP->Name, p->RIOConnectTable[SubEnt].Name);
208                                         p->RIOError.Error = DUPLICATED_RTA_ID;
209                                         p->RIOError.Entry = Entry;
210                                         p->RIOError.Other = SubEnt;
211                                         return -ENXIO;
212                                 }
213                                 /*
214                                  ** If the RtaUniqueNum is the same, it may be looking at both
215                                  ** entries for a 16 port RTA, so check the ids
216                                  */
217                                 if ((MapP->RtaUniqueNum == p->RIOConnectTable[SubEnt].RtaUniqueNum)
218                                     && (MapP->ID2 != p->RIOConnectTable[SubEnt].ID)) {
219                                         rio_dprintk(RIO_DEBUG_TABLE, "RTA %s has duplicate unique number\n", MapP->Name);
220                                         rio_dprintk(RIO_DEBUG_TABLE, "RTA %s has duplicate unique number\n", p->RIOConnectTable[SubEnt].Name);
221                                         p->RIOError.Error = DUPLICATE_UNIQUE_NUMBER;
222                                         p->RIOError.Entry = Entry;
223                                         p->RIOError.Other = SubEnt;
224                                         return -ENXIO;
225                                 }
226                         }
227                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(7a)\n");
228                         /* (7a) */
229                         if ((MapP->SysPort != NO_PORT) && (MapP->SysPort % PORTS_PER_RTA)) {
230                                 rio_dprintk(RIO_DEBUG_TABLE, "TTY Port number %d-RTA %s is not a multiple of %d!\n", (int) MapP->SysPort, MapP->Name, PORTS_PER_RTA);
231                                 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
232                                 p->RIOError.Entry = Entry;
233                                 return -ENXIO;
234                         }
235                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(7b)\n");
236                         /* (7b) */
237                         if ((MapP->SysPort != NO_PORT) && (MapP->SysPort >= RIO_PORTS)) {
238                                 rio_dprintk(RIO_DEBUG_TABLE, "TTY Port number %d for RTA %s is too big\n", (int) MapP->SysPort, MapP->Name);
239                                 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
240                                 p->RIOError.Entry = Entry;
241                                 return -ENXIO;
242                         }
243                         for (SubEnt = 0; SubEnt < Entry; SubEnt++) {
244                                 if (p->RIOConnectTable[SubEnt].Flags & RTA16_SECOND_SLOT)
245                                         continue;
246                                 if (p->RIOConnectTable[SubEnt].RtaUniqueNum) {
247                                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(8)\n");
248                                         /* (8) */
249                                         if ((MapP->SysPort != NO_PORT) && (MapP->SysPort == p->RIOConnectTable[SubEnt].SysPort)) {
250                                                 rio_dprintk(RIO_DEBUG_TABLE, "RTA %s:same TTY port # as RTA %s (%d)\n", MapP->Name, p->RIOConnectTable[SubEnt].Name, (int) MapP->SysPort);
251                                                 p->RIOError.Error = TTY_NUMBER_IN_USE;
252                                                 p->RIOError.Entry = Entry;
253                                                 p->RIOError.Other = SubEnt;
254                                                 return -ENXIO;
255                                         }
256                                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(9)\n");
257                                         if (strcmp(MapP->Name, p->RIOConnectTable[SubEnt].Name) == 0 && !(MapP->Flags & RTA16_SECOND_SLOT)) {   /* (9) */
258                                                 rio_dprintk(RIO_DEBUG_TABLE, "RTA name %s used twice\n", MapP->Name);
259                                                 p->RIOError.Error = NAME_USED_TWICE;
260                                                 p->RIOError.Entry = Entry;
261                                                 p->RIOError.Other = SubEnt;
262                                                 return -ENXIO;
263                                         }
264                                 }
265                         }
266                 } else {        /* (6) */
267                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(6)\n");
268                         if (MapP->ID) {
269                                 rio_dprintk(RIO_DEBUG_TABLE, "RIO:HOST %s has been allocated ID that isn't zero!\n", MapP->Name);
270                                 p->RIOError.Error = HOST_ID_NOT_ZERO;
271                                 p->RIOError.Entry = Entry;
272                                 return -ENXIO;
273                         }
274                         if (MapP->SysPort != NO_PORT) {
275                                 rio_dprintk(RIO_DEBUG_TABLE, "RIO: HOST %s has been allocated port numbers!\n", MapP->Name);
276                                 p->RIOError.Error = HOST_SYSPORT_BAD;
277                                 p->RIOError.Entry = Entry;
278                                 return -ENXIO;
279                         }
280                 }
281         }
282
283         /*
284          ** wow! if we get here then it's a goody!
285          */
286
287         /*
288          ** Zero the (old) entries for each host...
289          */
290         for (Host = 0; Host < RIO_HOSTS; Host++) {
291                 for (Entry = 0; Entry < MAX_RUP; Entry++) {
292                         memset(&p->RIOHosts[Host].Mapping[Entry], 0, sizeof(struct Map));
293                 }
294                 memset(&p->RIOHosts[Host].Name[0], 0, sizeof(p->RIOHosts[Host].Name));
295         }
296
297         /*
298          ** Copy in the new table entries
299          */
300         for (Entry = 0; Entry < TOTAL_MAP_ENTRIES; Entry++) {
301                 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: Copy table for Host entry %d\n", Entry);
302                 MapP = &p->RIOConnectTable[Entry];
303
304                 /*
305                  ** Now, if it is an empty slot ignore it!
306                  */
307                 if (MapP->HostUniqueNum == 0)
308                         continue;
309
310                 /*
311                  ** we saved the host number earlier, so grab it back
312                  */
313                 HostP = &p->RIOHosts[MapP->Topology[0].Unit];
314
315                 /*
316                  ** If it is a host, then we only need to fill in the name field.
317                  */
318                 if (MapP->ID == 0) {
319                         rio_dprintk(RIO_DEBUG_TABLE, "Host entry found. Name %s\n", MapP->Name);
320                         memcpy(HostP->Name, MapP->Name, MAX_NAME_LEN);
321                         continue;
322                 }
323
324                 /*
325                  ** Its an RTA entry, so fill in the host mapping entries for it
326                  ** and the port mapping entries. Notice that entry zero is for
327                  ** ID one.
328                  */
329                 HostMapP = &HostP->Mapping[MapP->ID - 1];
330
331                 if (MapP->Flags & SLOT_IN_USE) {
332                         rio_dprintk(RIO_DEBUG_TABLE, "Rta entry found. Name %s\n", MapP->Name);
333                         /*
334                          ** structure assign, then sort out the bits we shouldn't have done
335                          */
336                         *HostMapP = *MapP;
337
338                         HostMapP->Flags = SLOT_IN_USE;
339                         if (MapP->Flags & RTA16_SECOND_SLOT)
340                                 HostMapP->Flags |= RTA16_SECOND_SLOT;
341
342                         RIOReMapPorts(p, HostP, HostMapP);
343                 } else {
344                         rio_dprintk(RIO_DEBUG_TABLE, "TENTATIVE Rta entry found. Name %s\n", MapP->Name);
345                 }
346         }
347
348         for (Entry = 0; Entry < TOTAL_MAP_ENTRIES; Entry++) {
349                 p->RIOSavedTable[Entry] = p->RIOConnectTable[Entry];
350         }
351
352         for (Host = 0; Host < p->RIONumHosts; Host++) {
353                 for (SubEnt = 0; SubEnt < LINKS_PER_UNIT; SubEnt++) {
354                         p->RIOHosts[Host].Topology[SubEnt].Unit = ROUTE_DISCONNECT;
355                         p->RIOHosts[Host].Topology[SubEnt].Link = NO_LINK;
356                 }
357                 for (Entry = 0; Entry < MAX_RUP; Entry++) {
358                         for (SubEnt = 0; SubEnt < LINKS_PER_UNIT; SubEnt++) {
359                                 p->RIOHosts[Host].Mapping[Entry].Topology[SubEnt].Unit = ROUTE_DISCONNECT;
360                                 p->RIOHosts[Host].Mapping[Entry].Topology[SubEnt].Link = NO_LINK;
361                         }
362                 }
363                 if (!p->RIOHosts[Host].Name[0]) {
364                         memcpy(p->RIOHosts[Host].Name, "HOST 1", 7);
365                         p->RIOHosts[Host].Name[5] += Host;
366                 }
367                 /*
368                  ** Check that default name assigned is unique.
369                  */
370                 Host1 = Host;
371                 NameIsUnique = 0;
372                 while (!NameIsUnique) {
373                         NameIsUnique = 1;
374                         for (Host2 = 0; Host2 < p->RIONumHosts; Host2++) {
375                                 if (Host2 == Host)
376                                         continue;
377                                 if (strcmp(p->RIOHosts[Host].Name, p->RIOHosts[Host2].Name)
378                                     == 0) {
379                                         NameIsUnique = 0;
380                                         Host1++;
381                                         if (Host1 >= p->RIONumHosts)
382                                                 Host1 = 0;
383                                         p->RIOHosts[Host].Name[5] = '1' + Host1;
384                                 }
385                         }
386                 }
387                 /*
388                  ** Rename host if name already used.
389                  */
390                 if (Host1 != Host) {
391                         rio_dprintk(RIO_DEBUG_TABLE, "Default name %s already used\n", p->RIOHosts[Host].Name);
392                         memcpy(p->RIOHosts[Host].Name, "HOST 1", 7);
393                         p->RIOHosts[Host].Name[5] += Host1;
394                 }
395                 rio_dprintk(RIO_DEBUG_TABLE, "Assigning default name %s\n", p->RIOHosts[Host].Name);
396         }
397         return 0;
398 }
399
400 /*
401 ** User process needs the config table - build it from first
402 ** principles.
403 **
404 *       FIXME: SMP locking
405 */
406 int RIOApel(struct rio_info *p)
407 {
408         int Host;
409         int link;
410         int Rup;
411         int Next = 0;
412         struct Map *MapP;
413         struct Host *HostP;
414         unsigned long flags;
415
416         rio_dprintk(RIO_DEBUG_TABLE, "Generating a table to return to config.rio\n");
417
418         memset(&p->RIOConnectTable[0], 0, sizeof(struct Map) * TOTAL_MAP_ENTRIES);
419
420         for (Host = 0; Host < RIO_HOSTS; Host++) {
421                 rio_dprintk(RIO_DEBUG_TABLE, "Processing host %d\n", Host);
422                 HostP = &p->RIOHosts[Host];
423                 rio_spin_lock_irqsave(&HostP->HostLock, flags);
424
425                 MapP = &p->RIOConnectTable[Next++];
426                 MapP->HostUniqueNum = HostP->UniqueNum;
427                 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
428                         rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
429                         continue;
430                 }
431                 MapP->RtaUniqueNum = 0;
432                 MapP->ID = 0;
433                 MapP->Flags = SLOT_IN_USE;
434                 MapP->SysPort = NO_PORT;
435                 for (link = 0; link < LINKS_PER_UNIT; link++)
436                         MapP->Topology[link] = HostP->Topology[link];
437                 memcpy(MapP->Name, HostP->Name, MAX_NAME_LEN);
438                 for (Rup = 0; Rup < MAX_RUP; Rup++) {
439                         if (HostP->Mapping[Rup].Flags & (SLOT_IN_USE | SLOT_TENTATIVE)) {
440                                 p->RIOConnectTable[Next] = HostP->Mapping[Rup];
441                                 if (HostP->Mapping[Rup].Flags & SLOT_IN_USE)
442                                         p->RIOConnectTable[Next].Flags |= SLOT_IN_USE;
443                                 if (HostP->Mapping[Rup].Flags & SLOT_TENTATIVE)
444                                         p->RIOConnectTable[Next].Flags |= SLOT_TENTATIVE;
445                                 if (HostP->Mapping[Rup].Flags & RTA16_SECOND_SLOT)
446                                         p->RIOConnectTable[Next].Flags |= RTA16_SECOND_SLOT;
447                                 Next++;
448                         }
449                 }
450                 rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
451         }
452         return 0;
453 }
454
455 /*
456 ** config.rio has taken a dislike to one of the gross maps entries.
457 ** if the entry is suitably inactive, then we can gob on it and remove
458 ** it from the table.
459 */
460 int RIODeleteRta(struct rio_info *p, struct Map *MapP)
461 {
462         int host, entry, port, link;
463         int SysPort;
464         struct Host *HostP;
465         struct Map *HostMapP;
466         struct Port *PortP;
467         int work_done = 0;
468         unsigned long lock_flags, sem_flags;
469
470         rio_dprintk(RIO_DEBUG_TABLE, "Delete entry on host %x, rta %x\n", MapP->HostUniqueNum, MapP->RtaUniqueNum);
471
472         for (host = 0; host < p->RIONumHosts; host++) {
473                 HostP = &p->RIOHosts[host];
474
475                 rio_spin_lock_irqsave(&HostP->HostLock, lock_flags);
476
477                 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
478                         rio_spin_unlock_irqrestore(&HostP->HostLock, lock_flags);
479                         continue;
480                 }
481
482                 for (entry = 0; entry < MAX_RUP; entry++) {
483                         if (MapP->RtaUniqueNum == HostP->Mapping[entry].RtaUniqueNum) {
484                                 HostMapP = &HostP->Mapping[entry];
485                                 rio_dprintk(RIO_DEBUG_TABLE, "Found entry offset %d on host %s\n", entry, HostP->Name);
486
487                                 /*
488                                  ** Check all four links of the unit are disconnected
489                                  */
490                                 for (link = 0; link < LINKS_PER_UNIT; link++) {
491                                         if (HostMapP->Topology[link].Unit != ROUTE_DISCONNECT) {
492                                                 rio_dprintk(RIO_DEBUG_TABLE, "Entry is in use and cannot be deleted!\n");
493                                                 p->RIOError.Error = UNIT_IS_IN_USE;
494                                                 rio_spin_unlock_irqrestore(&HostP->HostLock, lock_flags);
495                                                 return -EBUSY;
496                                         }
497                                 }
498                                 /*
499                                  ** Slot has been allocated, BUT not booted/routed/
500                                  ** connected/selected or anything else-ed
501                                  */
502                                 SysPort = HostMapP->SysPort;
503
504                                 if (SysPort != NO_PORT) {
505                                         for (port = SysPort; port < SysPort + PORTS_PER_RTA; port++) {
506                                                 PortP = p->RIOPortp[port];
507                                                 rio_dprintk(RIO_DEBUG_TABLE, "Unmap port\n");
508
509                                                 rio_spin_lock_irqsave(&PortP->portSem, sem_flags);
510
511                                                 PortP->Mapped = 0;
512
513                                                 if (PortP->State & (RIO_MOPEN | RIO_LOPEN)) {
514
515                                                         rio_dprintk(RIO_DEBUG_TABLE, "Gob on port\n");
516                                                         PortP->TxBufferIn = PortP->TxBufferOut = 0;
517                                                         /* What should I do 
518                                                            wakeup( &PortP->TxBufferIn );
519                                                            wakeup( &PortP->TxBufferOut);
520                                                          */
521                                                         PortP->InUse = NOT_INUSE;
522                                                         /* What should I do 
523                                                            wakeup( &PortP->InUse );
524                                                            signal(PortP->TtyP->t_pgrp,SIGKILL);
525                                                            ttyflush(PortP->TtyP,(FREAD|FWRITE));
526                                                          */
527                                                         PortP->State |= RIO_CLOSING | RIO_DELETED;
528                                                 }
529
530                                                 /*
531                                                  ** For the second slot of a 16 port RTA, the
532                                                  ** driver needs to reset the changes made to
533                                                  ** the phb to port mappings in RIORouteRup.
534                                                  */
535                                                 if (PortP->SecondBlock) {
536                                                         u16 dest_unit = HostMapP->ID;
537                                                         u16 dest_port = port - SysPort;
538                                                         u16 __iomem *TxPktP;
539                                                         struct PKT __iomem *Pkt;
540
541                                                         for (TxPktP = PortP->TxStart; TxPktP <= PortP->TxEnd; TxPktP++) {
542                                                                 /*
543                                                                  ** *TxPktP is the pointer to the
544                                                                  ** transmit packet on the host card.
545                                                                  ** This needs to be translated into
546                                                                  ** a 32 bit pointer so it can be
547                                                                  ** accessed from the driver.
548                                                                  */
549                                                                 Pkt = (struct PKT __iomem *) RIO_PTR(HostP->Caddr, readw(&*TxPktP));
550                                                                 rio_dprintk(RIO_DEBUG_TABLE, "Tx packet (%x) destination: Old %x:%x New %x:%x\n", readw(TxPktP), readb(&Pkt->dest_unit), readb(&Pkt->dest_port), dest_unit, dest_port);
551                                                                 writew(dest_unit, &Pkt->dest_unit);
552                                                                 writew(dest_port, &Pkt->dest_port);
553                                                         }
554                                                         rio_dprintk(RIO_DEBUG_TABLE, "Port %d phb destination: Old %x:%x New %x:%x\n", port, readb(&PortP->PhbP->destination) & 0xff, (readb(&PortP->PhbP->destination) >> 8) & 0xff, dest_unit, dest_port);
555                                                         writew(dest_unit + (dest_port << 8), &PortP->PhbP->destination);
556                                                 }
557                                                 rio_spin_unlock_irqrestore(&PortP->portSem, sem_flags);
558                                         }
559                                 }
560                                 rio_dprintk(RIO_DEBUG_TABLE, "Entry nulled.\n");
561                                 memset(HostMapP, 0, sizeof(struct Map));
562                                 work_done++;
563                         }
564                 }
565                 rio_spin_unlock_irqrestore(&HostP->HostLock, lock_flags);
566         }
567
568         /* XXXXX lock me up */
569         for (entry = 0; entry < TOTAL_MAP_ENTRIES; entry++) {
570                 if (p->RIOSavedTable[entry].RtaUniqueNum == MapP->RtaUniqueNum) {
571                         memset(&p->RIOSavedTable[entry], 0, sizeof(struct Map));
572                         work_done++;
573                 }
574                 if (p->RIOConnectTable[entry].RtaUniqueNum == MapP->RtaUniqueNum) {
575                         memset(&p->RIOConnectTable[entry], 0, sizeof(struct Map));
576                         work_done++;
577                 }
578         }
579         if (work_done)
580                 return 0;
581
582         rio_dprintk(RIO_DEBUG_TABLE, "Couldn't find entry to be deleted\n");
583         p->RIOError.Error = COULDNT_FIND_ENTRY;
584         return -ENXIO;
585 }
586
587 int RIOAssignRta(struct rio_info *p, struct Map *MapP)
588 {
589         int host;
590         struct Map *HostMapP;
591         char *sptr;
592         int link;
593
594
595         rio_dprintk(RIO_DEBUG_TABLE, "Assign entry on host %x, rta %x, ID %d, Sysport %d\n", MapP->HostUniqueNum, MapP->RtaUniqueNum, MapP->ID, (int) MapP->SysPort);
596
597         if ((MapP->ID != (u16) - 1) && ((int) MapP->ID < (int) 1 || (int) MapP->ID > MAX_RUP)) {
598                 rio_dprintk(RIO_DEBUG_TABLE, "Bad ID in map entry!\n");
599                 p->RIOError.Error = ID_NUMBER_OUT_OF_RANGE;
600                 return -EINVAL;
601         }
602         if (MapP->RtaUniqueNum == 0) {
603                 rio_dprintk(RIO_DEBUG_TABLE, "Rta Unique number zero!\n");
604                 p->RIOError.Error = RTA_UNIQUE_NUMBER_ZERO;
605                 return -EINVAL;
606         }
607         if ((MapP->SysPort != NO_PORT) && (MapP->SysPort % PORTS_PER_RTA)) {
608                 rio_dprintk(RIO_DEBUG_TABLE, "Port %d not multiple of %d!\n", (int) MapP->SysPort, PORTS_PER_RTA);
609                 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
610                 return -EINVAL;
611         }
612         if ((MapP->SysPort != NO_PORT) && (MapP->SysPort >= RIO_PORTS)) {
613                 rio_dprintk(RIO_DEBUG_TABLE, "Port %d not valid!\n", (int) MapP->SysPort);
614                 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
615                 return -EINVAL;
616         }
617
618         /*
619          ** Copy the name across to the map entry.
620          */
621         MapP->Name[MAX_NAME_LEN - 1] = '\0';
622         sptr = MapP->Name;
623         while (*sptr) {
624                 if (*sptr < ' ' || *sptr > '~') {
625                         rio_dprintk(RIO_DEBUG_TABLE, "Name entry contains non-printing characters!\n");
626                         p->RIOError.Error = BAD_CHARACTER_IN_NAME;
627                         return -EINVAL;
628                 }
629                 sptr++;
630         }
631
632         for (host = 0; host < p->RIONumHosts; host++) {
633                 if (MapP->HostUniqueNum == p->RIOHosts[host].UniqueNum) {
634                         if ((p->RIOHosts[host].Flags & RUN_STATE) != RC_RUNNING) {
635                                 p->RIOError.Error = HOST_NOT_RUNNING;
636                                 return -ENXIO;
637                         }
638
639                         /*
640                          ** Now we have a host we need to allocate an ID
641                          ** if the entry does not already have one.
642                          */
643                         if (MapP->ID == (u16) - 1) {
644                                 int nNewID;
645
646                                 rio_dprintk(RIO_DEBUG_TABLE, "Attempting to get a new ID for rta \"%s\"\n", MapP->Name);
647                                 /*
648                                  ** The idea here is to allow RTA's to be assigned
649                                  ** before they actually appear on the network.
650                                  ** This allows the addition of RTA's without having
651                                  ** to plug them in.
652                                  ** What we do is:
653                                  **  - Find a free ID and allocate it to the RTA.
654                                  **  - If this map entry is the second half of a
655                                  **    16 port entry then find the other half and
656                                  **    make sure the 2 cross reference each other.
657                                  */
658                                 if (RIOFindFreeID(p, &p->RIOHosts[host], &nNewID, NULL) != 0) {
659                                         p->RIOError.Error = COULDNT_FIND_ENTRY;
660                                         return -EBUSY;
661                                 }
662                                 MapP->ID = (u16) nNewID + 1;
663                                 rio_dprintk(RIO_DEBUG_TABLE, "Allocated ID %d for this new RTA.\n", MapP->ID);
664                                 HostMapP = &p->RIOHosts[host].Mapping[nNewID];
665                                 HostMapP->RtaUniqueNum = MapP->RtaUniqueNum;
666                                 HostMapP->HostUniqueNum = MapP->HostUniqueNum;
667                                 HostMapP->ID = MapP->ID;
668                                 for (link = 0; link < LINKS_PER_UNIT; link++) {
669                                         HostMapP->Topology[link].Unit = ROUTE_DISCONNECT;
670                                         HostMapP->Topology[link].Link = NO_LINK;
671                                 }
672                                 if (MapP->Flags & RTA16_SECOND_SLOT) {
673                                         int unit;
674
675                                         for (unit = 0; unit < MAX_RUP; unit++)
676                                                 if (p->RIOHosts[host].Mapping[unit].RtaUniqueNum == MapP->RtaUniqueNum)
677                                                         break;
678                                         if (unit == MAX_RUP) {
679                                                 p->RIOError.Error = COULDNT_FIND_ENTRY;
680                                                 return -EBUSY;
681                                         }
682                                         HostMapP->Flags |= RTA16_SECOND_SLOT;
683                                         HostMapP->ID2 = MapP->ID2 = p->RIOHosts[host].Mapping[unit].ID;
684                                         p->RIOHosts[host].Mapping[unit].ID2 = MapP->ID;
685                                         rio_dprintk(RIO_DEBUG_TABLE, "Cross referenced id %d to ID %d.\n", MapP->ID, p->RIOHosts[host].Mapping[unit].ID);
686                                 }
687                         }
688
689                         HostMapP = &p->RIOHosts[host].Mapping[MapP->ID - 1];
690
691                         if (HostMapP->Flags & SLOT_IN_USE) {
692                                 rio_dprintk(RIO_DEBUG_TABLE, "Map table slot for ID %d is already in use.\n", MapP->ID);
693                                 p->RIOError.Error = ID_ALREADY_IN_USE;
694                                 return -EBUSY;
695                         }
696
697                         /*
698                          ** Assign the sys ports and the name, and mark the slot as
699                          ** being in use.
700                          */
701                         HostMapP->SysPort = MapP->SysPort;
702                         if ((MapP->Flags & RTA16_SECOND_SLOT) == 0)
703                                 memcpy(HostMapP->Name, MapP->Name, MAX_NAME_LEN);
704                         HostMapP->Flags = SLOT_IN_USE | RTA_BOOTED;
705 #ifdef NEED_TO_FIX
706                         RIO_SV_BROADCAST(p->RIOHosts[host].svFlags[MapP->ID - 1]);
707 #endif
708                         if (MapP->Flags & RTA16_SECOND_SLOT)
709                                 HostMapP->Flags |= RTA16_SECOND_SLOT;
710
711                         RIOReMapPorts(p, &p->RIOHosts[host], HostMapP);
712                         /*
713                          ** Adjust 2nd block of 8 phbs
714                          */
715                         if (MapP->Flags & RTA16_SECOND_SLOT)
716                                 RIOFixPhbs(p, &p->RIOHosts[host], HostMapP->ID - 1);
717
718                         if (HostMapP->SysPort != NO_PORT) {
719                                 if (HostMapP->SysPort < p->RIOFirstPortsBooted)
720                                         p->RIOFirstPortsBooted = HostMapP->SysPort;
721                                 if (HostMapP->SysPort > p->RIOLastPortsBooted)
722                                         p->RIOLastPortsBooted = HostMapP->SysPort;
723                         }
724                         if (MapP->Flags & RTA16_SECOND_SLOT)
725                                 rio_dprintk(RIO_DEBUG_TABLE, "Second map of RTA %s added to configuration\n", p->RIOHosts[host].Mapping[MapP->ID2 - 1].Name);
726                         else
727                                 rio_dprintk(RIO_DEBUG_TABLE, "RTA %s added to configuration\n", MapP->Name);
728                         return 0;
729                 }
730         }
731         p->RIOError.Error = UNKNOWN_HOST_NUMBER;
732         rio_dprintk(RIO_DEBUG_TABLE, "Unknown host %x\n", MapP->HostUniqueNum);
733         return -ENXIO;
734 }
735
736
737 int RIOReMapPorts(struct rio_info *p, struct Host *HostP, struct Map *HostMapP)
738 {
739         struct Port *PortP;
740         unsigned int SubEnt;
741         unsigned int HostPort;
742         unsigned int SysPort;
743         u16 RtaType;
744         unsigned long flags;
745
746         rio_dprintk(RIO_DEBUG_TABLE, "Mapping sysport %d to id %d\n", (int) HostMapP->SysPort, HostMapP->ID);
747
748         /*
749          ** We need to tell the UnixRups which sysport the rup corresponds to
750          */
751         HostP->UnixRups[HostMapP->ID - 1].BaseSysPort = HostMapP->SysPort;
752
753         if (HostMapP->SysPort == NO_PORT)
754                 return (0);
755
756         RtaType = GetUnitType(HostMapP->RtaUniqueNum);
757         rio_dprintk(RIO_DEBUG_TABLE, "Mapping sysport %d-%d\n", (int) HostMapP->SysPort, (int) HostMapP->SysPort + PORTS_PER_RTA - 1);
758
759         /*
760          ** now map each of its eight ports
761          */
762         for (SubEnt = 0; SubEnt < PORTS_PER_RTA; SubEnt++) {
763                 rio_dprintk(RIO_DEBUG_TABLE, "subent = %d, HostMapP->SysPort = %d\n", SubEnt, (int) HostMapP->SysPort);
764                 SysPort = HostMapP->SysPort + SubEnt;   /* portnumber within system */
765                 /* portnumber on host */
766
767                 HostPort = (HostMapP->ID - 1) * PORTS_PER_RTA + SubEnt;
768
769                 rio_dprintk(RIO_DEBUG_TABLE, "c1 p = %p, p->rioPortp = %p\n", p, p->RIOPortp);
770                 PortP = p->RIOPortp[SysPort];
771                 rio_dprintk(RIO_DEBUG_TABLE, "Map port\n");
772
773                 /*
774                  ** Point at all the real neat data structures
775                  */
776                 rio_spin_lock_irqsave(&PortP->portSem, flags);
777                 PortP->HostP = HostP;
778                 PortP->Caddr = HostP->Caddr;
779
780                 /*
781                  ** The PhbP cannot be filled in yet
782                  ** unless the host has been booted
783                  */
784                 if ((HostP->Flags & RUN_STATE) == RC_RUNNING) {
785                         struct PHB __iomem *PhbP = PortP->PhbP = &HostP->PhbP[HostPort];
786                         PortP->TxAdd = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->tx_add));
787                         PortP->TxStart = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->tx_start));
788                         PortP->TxEnd = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->tx_end));
789                         PortP->RxRemove = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->rx_remove));
790                         PortP->RxStart = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->rx_start));
791                         PortP->RxEnd = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->rx_end));
792                 } else
793                         PortP->PhbP = NULL;
794
795                 /*
796                  ** port related flags
797                  */
798                 PortP->HostPort = HostPort;
799                 /*
800                  ** For each part of a 16 port RTA, RupNum is ID - 1.
801                  */
802                 PortP->RupNum = HostMapP->ID - 1;
803                 if (HostMapP->Flags & RTA16_SECOND_SLOT) {
804                         PortP->ID2 = HostMapP->ID2 - 1;
805                         PortP->SecondBlock = 1;
806                 } else {
807                         PortP->ID2 = 0;
808                         PortP->SecondBlock = 0;
809                 }
810                 PortP->RtaUniqueNum = HostMapP->RtaUniqueNum;
811
812                 /*
813                  ** If the port was already mapped then thats all we need to do.
814                  */
815                 if (PortP->Mapped) {
816                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
817                         continue;
818                 } else
819                         HostMapP->Flags &= ~RTA_NEWBOOT;
820
821                 PortP->State = 0;
822                 PortP->Config = 0;
823                 /*
824                  ** Check out the module type - if it is special (read only etc.)
825                  ** then we need to set flags in the PortP->Config.
826                  ** Note: For 16 port RTA, all ports are of the same type.
827                  */
828                 if (RtaType == TYPE_RTA16) {
829                         PortP->Config |= p->RIOModuleTypes[HostP->UnixRups[HostMapP->ID - 1].ModTypes].Flags[SubEnt % PORTS_PER_MODULE];
830                 } else {
831                         if (SubEnt < PORTS_PER_MODULE)
832                                 PortP->Config |= p->RIOModuleTypes[LONYBLE(HostP->UnixRups[HostMapP->ID - 1].ModTypes)].Flags[SubEnt % PORTS_PER_MODULE];
833                         else
834                                 PortP->Config |= p->RIOModuleTypes[HINYBLE(HostP->UnixRups[HostMapP->ID - 1].ModTypes)].Flags[SubEnt % PORTS_PER_MODULE];
835                 }
836
837                 /*
838                  ** more port related flags
839                  */
840                 PortP->PortState = 0;
841                 PortP->ModemLines = 0;
842                 PortP->ModemState = 0;
843                 PortP->CookMode = COOK_WELL;
844                 PortP->ParamSem = 0;
845                 PortP->FlushCmdBodge = 0;
846                 PortP->WflushFlag = 0;
847                 PortP->MagicFlags = 0;
848                 PortP->Lock = 0;
849                 PortP->Store = 0;
850                 PortP->FirstOpen = 1;
851
852                 /*
853                  ** Buffers 'n things
854                  */
855                 PortP->RxDataStart = 0;
856                 PortP->Cor2Copy = 0;
857                 PortP->Name = &HostMapP->Name[0];
858                 PortP->statsGather = 0;
859                 PortP->txchars = 0;
860                 PortP->rxchars = 0;
861                 PortP->opens = 0;
862                 PortP->closes = 0;
863                 PortP->ioctls = 0;
864                 if (PortP->TxRingBuffer)
865                         memset(PortP->TxRingBuffer, 0, p->RIOBufferSize);
866                 else if (p->RIOBufferSize) {
867                         PortP->TxRingBuffer = kzalloc(p->RIOBufferSize, GFP_KERNEL);
868                 }
869                 PortP->TxBufferOut = 0;
870                 PortP->TxBufferIn = 0;
871                 PortP->Debug = 0;
872                 /*
873                  ** LastRxTgl stores the state of the rx toggle bit for this
874                  ** port, to be compared with the state of the next pkt received.
875                  ** If the same, we have received the same rx pkt from the RTA
876                  ** twice. Initialise to a value not equal to PHB_RX_TGL or 0.
877                  */
878                 PortP->LastRxTgl = ~(u8) PHB_RX_TGL;
879
880                 /*
881                  ** and mark the port as usable
882                  */
883                 PortP->Mapped = 1;
884                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
885         }
886         if (HostMapP->SysPort < p->RIOFirstPortsMapped)
887                 p->RIOFirstPortsMapped = HostMapP->SysPort;
888         if (HostMapP->SysPort > p->RIOLastPortsMapped)
889                 p->RIOLastPortsMapped = HostMapP->SysPort;
890
891         return 0;
892 }
893
894 int RIOChangeName(struct rio_info *p, struct Map *MapP)
895 {
896         int host;
897         struct Map *HostMapP;
898         char *sptr;
899
900         rio_dprintk(RIO_DEBUG_TABLE, "Change name entry on host %x, rta %x, ID %d, Sysport %d\n", MapP->HostUniqueNum, MapP->RtaUniqueNum, MapP->ID, (int) MapP->SysPort);
901
902         if (MapP->ID > MAX_RUP) {
903                 rio_dprintk(RIO_DEBUG_TABLE, "Bad ID in map entry!\n");
904                 p->RIOError.Error = ID_NUMBER_OUT_OF_RANGE;
905                 return -EINVAL;
906         }
907
908         MapP->Name[MAX_NAME_LEN - 1] = '\0';
909         sptr = MapP->Name;
910
911         while (*sptr) {
912                 if (*sptr < ' ' || *sptr > '~') {
913                         rio_dprintk(RIO_DEBUG_TABLE, "Name entry contains non-printing characters!\n");
914                         p->RIOError.Error = BAD_CHARACTER_IN_NAME;
915                         return -EINVAL;
916                 }
917                 sptr++;
918         }
919
920         for (host = 0; host < p->RIONumHosts; host++) {
921                 if (MapP->HostUniqueNum == p->RIOHosts[host].UniqueNum) {
922                         if ((p->RIOHosts[host].Flags & RUN_STATE) != RC_RUNNING) {
923                                 p->RIOError.Error = HOST_NOT_RUNNING;
924                                 return -ENXIO;
925                         }
926                         if (MapP->ID == 0) {
927                                 memcpy(p->RIOHosts[host].Name, MapP->Name, MAX_NAME_LEN);
928                                 return 0;
929                         }
930
931                         HostMapP = &p->RIOHosts[host].Mapping[MapP->ID - 1];
932
933                         if (HostMapP->RtaUniqueNum != MapP->RtaUniqueNum) {
934                                 p->RIOError.Error = RTA_NUMBER_WRONG;
935                                 return -ENXIO;
936                         }
937                         memcpy(HostMapP->Name, MapP->Name, MAX_NAME_LEN);
938                         return 0;
939                 }
940         }
941         p->RIOError.Error = UNKNOWN_HOST_NUMBER;
942         rio_dprintk(RIO_DEBUG_TABLE, "Unknown host %x\n", MapP->HostUniqueNum);
943         return -ENXIO;
944 }