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