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