[ACPI] merge 3549 4320 4485 4588 4980 5483 5651 acpica asus fops pnpacpi branches...
[linux-2.6] / drivers / char / rio / riotable.c
1 /*
2 ** -----------------------------------------------------------------------------
3 **
4 **  Perle Specialix driver for Linux
5 **  Ported from existing RIO Driver for SCO sources.
6  *
7  *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License, or
12  *      (at your option) any later version.
13  *
14  *      This program is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *      GNU General Public License for more details.
18  *
19  *      You should have received a copy of the GNU General Public License
20  *      along with this program; if not, write to the Free Software
21  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 **
23 **      Module          : riotable.c
24 **      SID             : 1.2
25 **      Last Modified   : 11/6/98 10:33:47
26 **      Retrieved       : 11/6/98 10:33:50
27 **
28 **  ident @(#)riotable.c        1.2
29 **
30 ** -----------------------------------------------------------------------------
31 */
32 #ifdef SCCS_LABELS
33 static char *_riotable_c_sccs_ = "@(#)riotable.c        1.2";
34 #endif
35
36 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/errno.h>
39 #include <linux/interrupt.h>
40 #include <linux/string.h>
41
42 #include <asm/io.h>
43 #include <asm/system.h>
44 #include <asm/string.h>
45 #include <asm/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(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", MapP->Name, MapP->HostUniqueNum);
191                         MapP->HostUniqueNum = 0;
192                         /* MapP->RtaUniqueNum   = 0; */
193                         /* MapP->ID                     = 0; */
194                         /* MapP->Flags           = 0; */
195                         /* MapP->SysPort                 = 0; */
196                         /* MapP->Name[0]                 = 0; */
197                         continue;
198                 }
199
200                 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(5)\n");
201                 if (MapP->RtaUniqueNum) {       /* (5) */
202                         if (!MapP->ID) {
203                                 rio_dprintk(RIO_DEBUG_TABLE, "RIO: RTA %s has been allocated an ID of zero!\n", MapP->Name);
204                                 p->RIOError.Error = ZERO_RTA_ID;
205                                 p->RIOError.Entry = Entry;
206                                 return -ENXIO;
207                         }
208                         if (MapP->ID > MAX_RUP) {
209                                 rio_dprintk(RIO_DEBUG_TABLE, "RIO: RTA %s has been allocated an invalid ID %d\n", MapP->Name, MapP->ID);
210                                 p->RIOError.Error = ID_NUMBER_OUT_OF_RANGE;
211                                 p->RIOError.Entry = Entry;
212                                 return -ENXIO;
213                         }
214                         for (SubEnt = 0; SubEnt < Entry; SubEnt++) {
215                                 if (MapP->HostUniqueNum == p->RIOConnectTable[SubEnt].HostUniqueNum && MapP->ID == p->RIOConnectTable[SubEnt].ID) {
216                                         rio_dprintk(RIO_DEBUG_TABLE, "Dupl. ID number allocated to RTA %s and RTA %s\n", MapP->Name, p->RIOConnectTable[SubEnt].Name);
217                                         p->RIOError.Error = DUPLICATED_RTA_ID;
218                                         p->RIOError.Entry = Entry;
219                                         p->RIOError.Other = SubEnt;
220                                         return -ENXIO;
221                                 }
222                                 /*
223                                  ** If the RtaUniqueNum is the same, it may be looking at both
224                                  ** entries for a 16 port RTA, so check the ids
225                                  */
226                                 if ((MapP->RtaUniqueNum == p->RIOConnectTable[SubEnt].RtaUniqueNum)
227                                     && (MapP->ID2 != p->RIOConnectTable[SubEnt].ID)) {
228                                         rio_dprintk(RIO_DEBUG_TABLE, "RTA %s has duplicate unique number\n", MapP->Name);
229                                         rio_dprintk(RIO_DEBUG_TABLE, "RTA %s has duplicate unique number\n", p->RIOConnectTable[SubEnt].Name);
230                                         p->RIOError.Error = DUPLICATE_UNIQUE_NUMBER;
231                                         p->RIOError.Entry = Entry;
232                                         p->RIOError.Other = SubEnt;
233                                         return -ENXIO;
234                                 }
235                         }
236                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(7a)\n");
237                         /* (7a) */
238                         if ((MapP->SysPort != NO_PORT) && (MapP->SysPort % PORTS_PER_RTA)) {
239                                 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);
240                                 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
241                                 p->RIOError.Entry = Entry;
242                                 return -ENXIO;
243                         }
244                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(7b)\n");
245                         /* (7b) */
246                         if ((MapP->SysPort != NO_PORT) && (MapP->SysPort >= RIO_PORTS)) {
247                                 rio_dprintk(RIO_DEBUG_TABLE, "TTY Port number %d for RTA %s is too big\n", (int) MapP->SysPort, MapP->Name);
248                                 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
249                                 p->RIOError.Entry = Entry;
250                                 return -ENXIO;
251                         }
252                         for (SubEnt = 0; SubEnt < Entry; SubEnt++) {
253                                 if (p->RIOConnectTable[SubEnt].Flags & RTA16_SECOND_SLOT)
254                                         continue;
255                                 if (p->RIOConnectTable[SubEnt].RtaUniqueNum) {
256                                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(8)\n");
257                                         /* (8) */
258                                         if ((MapP->SysPort != NO_PORT) && (MapP->SysPort == p->RIOConnectTable[SubEnt].SysPort)) {
259                                                 rio_dprintk(RIO_DEBUG_TABLE, "RTA %s:same TTY port # as RTA %s (%d)\n", MapP->Name, p->RIOConnectTable[SubEnt].Name, (int) MapP->SysPort);
260                                                 p->RIOError.Error = TTY_NUMBER_IN_USE;
261                                                 p->RIOError.Entry = Entry;
262                                                 p->RIOError.Other = SubEnt;
263                                                 return -ENXIO;
264                                         }
265                                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(9)\n");
266                                         if (strcmp(MapP->Name, p->RIOConnectTable[SubEnt].Name) == 0 && !(MapP->Flags & RTA16_SECOND_SLOT)) {   /* (9) */
267                                                 rio_dprintk(RIO_DEBUG_TABLE, "RTA name %s used twice\n", MapP->Name);
268                                                 p->RIOError.Error = NAME_USED_TWICE;
269                                                 p->RIOError.Entry = Entry;
270                                                 p->RIOError.Other = SubEnt;
271                                                 return -ENXIO;
272                                         }
273                                 }
274                         }
275                 } else {        /* (6) */
276                         rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(6)\n");
277                         if (MapP->ID) {
278                                 rio_dprintk(RIO_DEBUG_TABLE, "RIO:HOST %s has been allocated ID that isn't zero!\n", MapP->Name);
279                                 p->RIOError.Error = HOST_ID_NOT_ZERO;
280                                 p->RIOError.Entry = Entry;
281                                 return -ENXIO;
282                         }
283                         if (MapP->SysPort != NO_PORT) {
284                                 rio_dprintk(RIO_DEBUG_TABLE, "RIO: HOST %s has been allocated port numbers!\n", MapP->Name);
285                                 p->RIOError.Error = HOST_SYSPORT_BAD;
286                                 p->RIOError.Entry = Entry;
287                                 return -ENXIO;
288                         }
289                 }
290         }
291
292         /*
293          ** wow! if we get here then it's a goody!
294          */
295
296         /*
297          ** Zero the (old) entries for each host...
298          */
299         for (Host = 0; Host < RIO_HOSTS; Host++) {
300                 for (Entry = 0; Entry < MAX_RUP; Entry++) {
301                         bzero((caddr_t) & p->RIOHosts[Host].Mapping[Entry], sizeof(struct Map));
302                 }
303                 bzero((caddr_t) & p->RIOHosts[Host].Name[0], sizeof(p->RIOHosts[Host].Name));
304         }
305
306         /*
307          ** Copy in the new table entries
308          */
309         for (Entry = 0; Entry < TOTAL_MAP_ENTRIES; Entry++) {
310                 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: Copy table for Host entry %d\n", Entry);
311                 MapP = &p->RIOConnectTable[Entry];
312
313                 /*
314                  ** Now, if it is an empty slot ignore it!
315                  */
316                 if (MapP->HostUniqueNum == 0)
317                         continue;
318
319                 /*
320                  ** we saved the host number earlier, so grab it back
321                  */
322                 HostP = &p->RIOHosts[MapP->Topology[0].Unit];
323
324                 /*
325                  ** If it is a host, then we only need to fill in the name field.
326                  */
327                 if (MapP->ID == 0) {
328                         rio_dprintk(RIO_DEBUG_TABLE, "Host entry found. Name %s\n", MapP->Name);
329                         bcopy(MapP->Name, HostP->Name, MAX_NAME_LEN);
330                         continue;
331                 }
332
333                 /*
334                  ** Its an RTA entry, so fill in the host mapping entries for it
335                  ** and the port mapping entries. Notice that entry zero is for
336                  ** ID one.
337                  */
338                 HostMapP = &HostP->Mapping[MapP->ID - 1];
339
340                 if (MapP->Flags & SLOT_IN_USE) {
341                         rio_dprintk(RIO_DEBUG_TABLE, "Rta entry found. Name %s\n", MapP->Name);
342                         /*
343                          ** structure assign, then sort out the bits we shouldn't have done
344                          */
345                         *HostMapP = *MapP;
346
347                         HostMapP->Flags = SLOT_IN_USE;
348                         if (MapP->Flags & RTA16_SECOND_SLOT)
349                                 HostMapP->Flags |= RTA16_SECOND_SLOT;
350
351                         RIOReMapPorts(p, HostP, HostMapP);
352                 } else {
353                         rio_dprintk(RIO_DEBUG_TABLE, "TENTATIVE Rta entry found. Name %s\n", MapP->Name);
354                 }
355         }
356
357         for (Entry = 0; Entry < TOTAL_MAP_ENTRIES; Entry++) {
358                 p->RIOSavedTable[Entry] = p->RIOConnectTable[Entry];
359         }
360
361         for (Host = 0; Host < p->RIONumHosts; Host++) {
362                 for (SubEnt = 0; SubEnt < LINKS_PER_UNIT; SubEnt++) {
363                         p->RIOHosts[Host].Topology[SubEnt].Unit = ROUTE_DISCONNECT;
364                         p->RIOHosts[Host].Topology[SubEnt].Link = NO_LINK;
365                 }
366                 for (Entry = 0; Entry < MAX_RUP; Entry++) {
367                         for (SubEnt = 0; SubEnt < LINKS_PER_UNIT; SubEnt++) {
368                                 p->RIOHosts[Host].Mapping[Entry].Topology[SubEnt].Unit = ROUTE_DISCONNECT;
369                                 p->RIOHosts[Host].Mapping[Entry].Topology[SubEnt].Link = NO_LINK;
370                         }
371                 }
372                 if (!p->RIOHosts[Host].Name[0]) {
373                         bcopy("HOST 1", p->RIOHosts[Host].Name, 7);
374                         p->RIOHosts[Host].Name[5] += Host;
375                 }
376                 /*
377                  ** Check that default name assigned is unique.
378                  */
379                 Host1 = Host;
380                 NameIsUnique = 0;
381                 while (!NameIsUnique) {
382                         NameIsUnique = 1;
383                         for (Host2 = 0; Host2 < p->RIONumHosts; Host2++) {
384                                 if (Host2 == Host)
385                                         continue;
386                                 if (strcmp(p->RIOHosts[Host].Name, p->RIOHosts[Host2].Name)
387                                     == 0) {
388                                         NameIsUnique = 0;
389                                         Host1++;
390                                         if (Host1 >= p->RIONumHosts)
391                                                 Host1 = 0;
392                                         p->RIOHosts[Host].Name[5] = '1' + Host1;
393                                 }
394                         }
395                 }
396                 /*
397                  ** Rename host if name already used.
398                  */
399                 if (Host1 != Host) {
400                         rio_dprintk(RIO_DEBUG_TABLE, "Default name %s already used\n", p->RIOHosts[Host].Name);
401                         bcopy("HOST 1", p->RIOHosts[Host].Name, 7);
402                         p->RIOHosts[Host].Name[5] += Host1;
403                 }
404                 rio_dprintk(RIO_DEBUG_TABLE, "Assigning default name %s\n", p->RIOHosts[Host].Name);
405         }
406         return 0;
407 }
408
409 /*
410 ** User process needs the config table - build it from first
411 ** principles.
412 */
413 int RIOApel(p)
414 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         long oldspl;
423
424         disable(oldspl);        /* strange but true! */
425
426         rio_dprintk(RIO_DEBUG_TABLE, "Generating a table to return to config.rio\n");
427
428         bzero((caddr_t) & p->RIOConnectTable[0], sizeof(struct Map) * TOTAL_MAP_ENTRIES);
429
430         for (Host = 0; Host < RIO_HOSTS; Host++) {
431                 rio_dprintk(RIO_DEBUG_TABLE, "Processing host %d\n", Host);
432                 HostP = &p->RIOHosts[Host];
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         }
457         restore(oldspl);
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(p, MapP)
467 struct rio_info *p;
468 struct Map *MapP;
469 {
470         int host, entry, port, link;
471         int SysPort;
472         struct Host *HostP;
473         struct Map *HostMapP;
474         struct Port *PortP;
475         int work_done = 0;
476         unsigned long lock_flags, sem_flags;
477
478         rio_dprintk(RIO_DEBUG_TABLE, "Delete entry on host %x, rta %x\n", MapP->HostUniqueNum, MapP->RtaUniqueNum);
479
480         for (host = 0; host < p->RIONumHosts; host++) {
481                 HostP = &p->RIOHosts[host];
482
483                 rio_spin_lock_irqsave(&HostP->HostLock, lock_flags);
484
485                 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
486                         rio_spin_unlock_irqrestore(&HostP->HostLock, lock_flags);
487                         continue;
488                 }
489
490                 for (entry = 0; entry < MAX_RUP; entry++) {
491                         if (MapP->RtaUniqueNum == HostP->Mapping[entry].RtaUniqueNum) {
492                                 HostMapP = &HostP->Mapping[entry];
493                                 rio_dprintk(RIO_DEBUG_TABLE, "Found entry offset %d on host %s\n", entry, HostP->Name);
494
495                                 /*
496                                  ** Check all four links of the unit are disconnected
497                                  */
498                                 for (link = 0; link < LINKS_PER_UNIT; link++) {
499                                         if (HostMapP->Topology[link].Unit != ROUTE_DISCONNECT) {
500                                                 rio_dprintk(RIO_DEBUG_TABLE, "Entry is in use and cannot be deleted!\n");
501                                                 p->RIOError.Error = UNIT_IS_IN_USE;
502                                                 rio_spin_unlock_irqrestore(&HostP->HostLock, lock_flags);
503                                                 return -EBUSY;
504                                         }
505                                 }
506                                 /*
507                                  ** Slot has been allocated, BUT not booted/routed/
508                                  ** connected/selected or anything else-ed
509                                  */
510                                 SysPort = HostMapP->SysPort;
511
512                                 if (SysPort != NO_PORT) {
513                                         for (port = SysPort; port < SysPort + PORTS_PER_RTA; port++) {
514                                                 PortP = p->RIOPortp[port];
515                                                 rio_dprintk(RIO_DEBUG_TABLE, "Unmap port\n");
516
517                                                 rio_spin_lock_irqsave(&PortP->portSem, sem_flags);
518
519                                                 PortP->Mapped = 0;
520
521                                                 if (PortP->State & (RIO_MOPEN | RIO_LOPEN)) {
522
523                                                         rio_dprintk(RIO_DEBUG_TABLE, "Gob on port\n");
524                                                         PortP->TxBufferIn = PortP->TxBufferOut = 0;
525                                                         /* What should I do 
526                                                            wakeup( &PortP->TxBufferIn );
527                                                            wakeup( &PortP->TxBufferOut);
528                                                          */
529                                                         PortP->InUse = NOT_INUSE;
530                                                         /* What should I do 
531                                                            wakeup( &PortP->InUse );
532                                                            signal(PortP->TtyP->t_pgrp,SIGKILL);
533                                                            ttyflush(PortP->TtyP,(FREAD|FWRITE));
534                                                          */
535                                                         PortP->State |= RIO_CLOSING | RIO_DELETED;
536                                                 }
537
538                                                 /*
539                                                  ** For the second slot of a 16 port RTA, the
540                                                  ** driver needs to reset the changes made to
541                                                  ** the phb to port mappings in RIORouteRup.
542                                                  */
543                                                 if (PortP->SecondBlock) {
544                                                         ushort dest_unit = HostMapP->ID;
545                                                         ushort dest_port = port - SysPort;
546                                                         WORD *TxPktP;
547                                                         PKT *Pkt;
548
549                                                         for (TxPktP = PortP->TxStart; TxPktP <= PortP->TxEnd; TxPktP++) {
550                                                                 /*
551                                                                  ** *TxPktP is the pointer to the
552                                                                  ** transmit packet on the host card.
553                                                                  ** This needs to be translated into
554                                                                  ** a 32 bit pointer so it can be
555                                                                  ** accessed from the driver.
556                                                                  */
557                                                                 Pkt = (PKT *) RIO_PTR(HostP->Caddr, RWORD(*TxPktP));
558                                                                 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);
559                                                                 WWORD(Pkt->dest_unit, dest_unit);
560                                                                 WWORD(Pkt->dest_port, dest_port);
561                                                         }
562                                                         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);
563                                                         WWORD(PortP->PhbP->destination, dest_unit + (dest_port << 8));
564                                                 }
565                                                 rio_spin_unlock_irqrestore(&PortP->portSem, sem_flags);
566                                         }
567                                 }
568                                 rio_dprintk(RIO_DEBUG_TABLE, "Entry nulled.\n");
569                                 bzero((char *) HostMapP, sizeof(struct Map));
570                                 work_done++;
571                         }
572                 }
573                 rio_spin_unlock_irqrestore(&HostP->HostLock, lock_flags);
574         }
575
576         /* XXXXX lock me up */
577         for (entry = 0; entry < TOTAL_MAP_ENTRIES; entry++) {
578                 if (p->RIOSavedTable[entry].RtaUniqueNum == MapP->RtaUniqueNum) {
579                         bzero((char *) &p->RIOSavedTable[entry], sizeof(struct Map));
580                         work_done++;
581                 }
582                 if (p->RIOConnectTable[entry].RtaUniqueNum == MapP->RtaUniqueNum) {
583                         bzero((char *) &p->RIOConnectTable[entry], sizeof(struct Map));
584                         work_done++;
585                 }
586         }
587         if (work_done)
588                 return 0;
589
590         rio_dprintk(RIO_DEBUG_TABLE, "Couldn't find entry to be deleted\n");
591         p->RIOError.Error = COULDNT_FIND_ENTRY;
592         return -ENXIO;
593 }
594
595 int RIOAssignRta(struct rio_info *p, struct Map *MapP)
596 {
597         int host;
598         struct Map *HostMapP;
599         char *sptr;
600         int link;
601
602
603         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);
604
605         if ((MapP->ID != (ushort) - 1) && ((int) MapP->ID < (int) 1 || (int) MapP->ID > MAX_RUP)) {
606                 rio_dprintk(RIO_DEBUG_TABLE, "Bad ID in map entry!\n");
607                 p->RIOError.Error = ID_NUMBER_OUT_OF_RANGE;
608                 return -EINVAL;
609         }
610         if (MapP->RtaUniqueNum == 0) {
611                 rio_dprintk(RIO_DEBUG_TABLE, "Rta Unique number zero!\n");
612                 p->RIOError.Error = RTA_UNIQUE_NUMBER_ZERO;
613                 return -EINVAL;
614         }
615         if ((MapP->SysPort != NO_PORT) && (MapP->SysPort % PORTS_PER_RTA)) {
616                 rio_dprintk(RIO_DEBUG_TABLE, "Port %d not multiple of %d!\n", (int) MapP->SysPort, PORTS_PER_RTA);
617                 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
618                 return -EINVAL;
619         }
620         if ((MapP->SysPort != NO_PORT) && (MapP->SysPort >= RIO_PORTS)) {
621                 rio_dprintk(RIO_DEBUG_TABLE, "Port %d not valid!\n", (int) MapP->SysPort);
622                 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
623                 return -EINVAL;
624         }
625
626         /*
627          ** Copy the name across to the map entry.
628          */
629         MapP->Name[MAX_NAME_LEN - 1] = '\0';
630         sptr = MapP->Name;
631         while (*sptr) {
632                 if (*sptr < ' ' || *sptr > '~') {
633                         rio_dprintk(RIO_DEBUG_TABLE, "Name entry contains non-printing characters!\n");
634                         p->RIOError.Error = BAD_CHARACTER_IN_NAME;
635                         return -EINVAL;
636                 }
637                 sptr++;
638         }
639
640         for (host = 0; host < p->RIONumHosts; host++) {
641                 if (MapP->HostUniqueNum == p->RIOHosts[host].UniqueNum) {
642                         if ((p->RIOHosts[host].Flags & RUN_STATE) != RC_RUNNING) {
643                                 p->RIOError.Error = HOST_NOT_RUNNING;
644                                 return -ENXIO;
645                         }
646
647                         /*
648                          ** Now we have a host we need to allocate an ID
649                          ** if the entry does not already have one.
650                          */
651                         if (MapP->ID == (ushort) - 1) {
652                                 int nNewID;
653
654                                 rio_dprintk(RIO_DEBUG_TABLE, "Attempting to get a new ID for rta \"%s\"\n", MapP->Name);
655                                 /*
656                                  ** The idea here is to allow RTA's to be assigned
657                                  ** before they actually appear on the network.
658                                  ** This allows the addition of RTA's without having
659                                  ** to plug them in.
660                                  ** What we do is:
661                                  **  - Find a free ID and allocate it to the RTA.
662                                  **  - If this map entry is the second half of a
663                                  **    16 port entry then find the other half and
664                                  **    make sure the 2 cross reference each other.
665                                  */
666                                 if (RIOFindFreeID(p, &p->RIOHosts[host], &nNewID, NULL) != 0) {
667                                         p->RIOError.Error = COULDNT_FIND_ENTRY;
668                                         return -EBUSY;
669                                 }
670                                 MapP->ID = (ushort) nNewID + 1;
671                                 rio_dprintk(RIO_DEBUG_TABLE, "Allocated ID %d for this new RTA.\n", MapP->ID);
672                                 HostMapP = &p->RIOHosts[host].Mapping[nNewID];
673                                 HostMapP->RtaUniqueNum = MapP->RtaUniqueNum;
674                                 HostMapP->HostUniqueNum = MapP->HostUniqueNum;
675                                 HostMapP->ID = MapP->ID;
676                                 for (link = 0; link < LINKS_PER_UNIT; link++) {
677                                         HostMapP->Topology[link].Unit = ROUTE_DISCONNECT;
678                                         HostMapP->Topology[link].Link = NO_LINK;
679                                 }
680                                 if (MapP->Flags & RTA16_SECOND_SLOT) {
681                                         int unit;
682
683                                         for (unit = 0; unit < MAX_RUP; unit++)
684                                                 if (p->RIOHosts[host].Mapping[unit].RtaUniqueNum == MapP->RtaUniqueNum)
685                                                         break;
686                                         if (unit == MAX_RUP) {
687                                                 p->RIOError.Error = COULDNT_FIND_ENTRY;
688                                                 return -EBUSY;
689                                         }
690                                         HostMapP->Flags |= RTA16_SECOND_SLOT;
691                                         HostMapP->ID2 = MapP->ID2 = p->RIOHosts[host].Mapping[unit].ID;
692                                         p->RIOHosts[host].Mapping[unit].ID2 = MapP->ID;
693                                         rio_dprintk(RIO_DEBUG_TABLE, "Cross referenced id %d to ID %d.\n", MapP->ID, p->RIOHosts[host].Mapping[unit].ID);
694                                 }
695                         }
696
697                         HostMapP = &p->RIOHosts[host].Mapping[MapP->ID - 1];
698
699                         if (HostMapP->Flags & SLOT_IN_USE) {
700                                 rio_dprintk(RIO_DEBUG_TABLE, "Map table slot for ID %d is already in use.\n", MapP->ID);
701                                 p->RIOError.Error = ID_ALREADY_IN_USE;
702                                 return -EBUSY;
703                         }
704
705                         /*
706                          ** Assign the sys ports and the name, and mark the slot as
707                          ** being in use.
708                          */
709                         HostMapP->SysPort = MapP->SysPort;
710                         if ((MapP->Flags & RTA16_SECOND_SLOT) == 0)
711                                 CCOPY(MapP->Name, HostMapP->Name, MAX_NAME_LEN);
712                         HostMapP->Flags = SLOT_IN_USE | RTA_BOOTED;
713 #ifdef NEED_TO_FIX
714                         RIO_SV_BROADCAST(p->RIOHosts[host].svFlags[MapP->ID - 1]);
715 #endif
716                         if (MapP->Flags & RTA16_SECOND_SLOT)
717                                 HostMapP->Flags |= RTA16_SECOND_SLOT;
718
719                         RIOReMapPorts(p, &p->RIOHosts[host], HostMapP);
720                         /*
721                          ** Adjust 2nd block of 8 phbs
722                          */
723                         if (MapP->Flags & RTA16_SECOND_SLOT)
724                                 RIOFixPhbs(p, &p->RIOHosts[host], HostMapP->ID - 1);
725
726                         if (HostMapP->SysPort != NO_PORT) {
727                                 if (HostMapP->SysPort < p->RIOFirstPortsBooted)
728                                         p->RIOFirstPortsBooted = HostMapP->SysPort;
729                                 if (HostMapP->SysPort > p->RIOLastPortsBooted)
730                                         p->RIOLastPortsBooted = HostMapP->SysPort;
731                         }
732                         if (MapP->Flags & RTA16_SECOND_SLOT)
733                                 rio_dprintk(RIO_DEBUG_TABLE, "Second map of RTA %s added to configuration\n", p->RIOHosts[host].Mapping[MapP->ID2 - 1].Name);
734                         else
735                                 rio_dprintk(RIO_DEBUG_TABLE, "RTA %s added to configuration\n", MapP->Name);
736                         return 0;
737                 }
738         }
739         p->RIOError.Error = UNKNOWN_HOST_NUMBER;
740         rio_dprintk(RIO_DEBUG_TABLE, "Unknown host %x\n", MapP->HostUniqueNum);
741         return -ENXIO;
742 }
743
744
745 int RIOReMapPorts(p, HostP, HostMapP)
746 struct rio_info *p;
747 struct Host *HostP;
748 struct Map *HostMapP;
749 {
750         register struct Port *PortP;
751         uint SubEnt;
752         uint HostPort;
753         uint SysPort;
754         ushort RtaType;
755         unsigned long flags;
756
757         rio_dprintk(RIO_DEBUG_TABLE, "Mapping sysport %d to id %d\n", (int) HostMapP->SysPort, HostMapP->ID);
758
759         /*
760          ** We need to tell the UnixRups which sysport the rup corresponds to
761          */
762         HostP->UnixRups[HostMapP->ID - 1].BaseSysPort = HostMapP->SysPort;
763
764         if (HostMapP->SysPort == NO_PORT)
765                 return (0);
766
767         RtaType = GetUnitType(HostMapP->RtaUniqueNum);
768         rio_dprintk(RIO_DEBUG_TABLE, "Mapping sysport %d-%d\n", (int) HostMapP->SysPort, (int) HostMapP->SysPort + PORTS_PER_RTA - 1);
769
770         /*
771          ** now map each of its eight ports
772          */
773         for (SubEnt = 0; SubEnt < PORTS_PER_RTA; SubEnt++) {
774                 rio_dprintk(RIO_DEBUG_TABLE, "subent = %d, HostMapP->SysPort = %d\n", SubEnt, (int) HostMapP->SysPort);
775                 SysPort = HostMapP->SysPort + SubEnt;   /* portnumber within system */
776                 /* portnumber on host */
777
778                 HostPort = (HostMapP->ID - 1) * PORTS_PER_RTA + SubEnt;
779
780                 rio_dprintk(RIO_DEBUG_TABLE, "c1 p = %p, p->rioPortp = %p\n", p, p->RIOPortp);
781                 PortP = p->RIOPortp[SysPort];
782                 rio_dprintk(RIO_DEBUG_TABLE, "Map port\n");
783
784                 /*
785                  ** Point at all the real neat data structures
786                  */
787                 rio_spin_lock_irqsave(&PortP->portSem, flags);
788                 PortP->HostP = HostP;
789                 PortP->Caddr = HostP->Caddr;
790
791                 /*
792                  ** The PhbP cannot be filled in yet
793                  ** unless the host has been booted
794                  */
795                 if ((HostP->Flags & RUN_STATE) == RC_RUNNING) {
796                         struct PHB *PhbP = PortP->PhbP = &HostP->PhbP[HostPort];
797                         PortP->TxAdd = (WORD *) RIO_PTR(HostP->Caddr, RWORD(PhbP->tx_add));
798                         PortP->TxStart = (WORD *) RIO_PTR(HostP->Caddr, RWORD(PhbP->tx_start));
799                         PortP->TxEnd = (WORD *) RIO_PTR(HostP->Caddr, RWORD(PhbP->tx_end));
800                         PortP->RxRemove = (WORD *) RIO_PTR(HostP->Caddr, RWORD(PhbP->rx_remove));
801                         PortP->RxStart = (WORD *) RIO_PTR(HostP->Caddr, RWORD(PhbP->rx_start));
802                         PortP->RxEnd = (WORD *) RIO_PTR(HostP->Caddr, RWORD(PhbP->rx_end));
803                 } else
804                         PortP->PhbP = NULL;
805
806                 /*
807                  ** port related flags
808                  */
809                 PortP->HostPort = HostPort;
810                 /*
811                  ** For each part of a 16 port RTA, RupNum is ID - 1.
812                  */
813                 PortP->RupNum = HostMapP->ID - 1;
814                 if (HostMapP->Flags & RTA16_SECOND_SLOT) {
815                         PortP->ID2 = HostMapP->ID2 - 1;
816                         PortP->SecondBlock = TRUE;
817                 } else {
818                         PortP->ID2 = 0;
819                         PortP->SecondBlock = FALSE;
820                 }
821                 PortP->RtaUniqueNum = HostMapP->RtaUniqueNum;
822
823                 /*
824                  ** If the port was already mapped then thats all we need to do.
825                  */
826                 if (PortP->Mapped) {
827                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
828                         continue;
829                 } else
830                         HostMapP->Flags &= ~RTA_NEWBOOT;
831
832                 PortP->State = 0;
833                 PortP->Config = 0;
834                 /*
835                  ** Check out the module type - if it is special (read only etc.)
836                  ** then we need to set flags in the PortP->Config.
837                  ** Note: For 16 port RTA, all ports are of the same type.
838                  */
839                 if (RtaType == TYPE_RTA16) {
840                         PortP->Config |= p->RIOModuleTypes[HostP->UnixRups[HostMapP->ID - 1].ModTypes].Flags[SubEnt % PORTS_PER_MODULE];
841                 } else {
842                         if (SubEnt < PORTS_PER_MODULE)
843                                 PortP->Config |= p->RIOModuleTypes[LONYBLE(HostP->UnixRups[HostMapP->ID - 1].ModTypes)].Flags[SubEnt % PORTS_PER_MODULE];
844                         else
845                                 PortP->Config |= p->RIOModuleTypes[HINYBLE(HostP->UnixRups[HostMapP->ID - 1].ModTypes)].Flags[SubEnt % PORTS_PER_MODULE];
846                 }
847
848                 /*
849                  ** more port related flags
850                  */
851                 PortP->PortState = 0;
852                 PortP->ModemLines = 0;
853                 PortP->ModemState = 0;
854                 PortP->CookMode = COOK_WELL;
855                 PortP->ParamSem = 0;
856                 PortP->FlushCmdBodge = 0;
857                 PortP->WflushFlag = 0;
858                 PortP->MagicFlags = 0;
859                 PortP->Lock = 0;
860                 PortP->Store = 0;
861                 PortP->FirstOpen = 1;
862
863                 /*
864                  ** Buffers 'n things
865                  */
866                 PortP->RxDataStart = 0;
867                 PortP->Cor2Copy = 0;
868                 PortP->Name = &HostMapP->Name[0];
869 #ifdef STATS
870                 bzero((caddr_t) & PortP->Stat, sizeof(struct RIOStats));
871 #endif
872                 PortP->statsGather = 0;
873                 PortP->txchars = 0;
874                 PortP->rxchars = 0;
875                 PortP->opens = 0;
876                 PortP->closes = 0;
877                 PortP->ioctls = 0;
878                 if (PortP->TxRingBuffer)
879                         bzero(PortP->TxRingBuffer, p->RIOBufferSize);
880                 else if (p->RIOBufferSize) {
881                         PortP->TxRingBuffer = sysbrk(p->RIOBufferSize);
882                         bzero(PortP->TxRingBuffer, p->RIOBufferSize);
883                 }
884                 PortP->TxBufferOut = 0;
885                 PortP->TxBufferIn = 0;
886                 PortP->Debug = 0;
887                 /*
888                  ** LastRxTgl stores the state of the rx toggle bit for this
889                  ** port, to be compared with the state of the next pkt received.
890                  ** If the same, we have received the same rx pkt from the RTA
891                  ** twice. Initialise to a value not equal to PHB_RX_TGL or 0.
892                  */
893                 PortP->LastRxTgl = ~(uchar) PHB_RX_TGL;
894
895                 /*
896                  ** and mark the port as usable
897                  */
898                 PortP->Mapped = 1;
899                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
900         }
901         if (HostMapP->SysPort < p->RIOFirstPortsMapped)
902                 p->RIOFirstPortsMapped = HostMapP->SysPort;
903         if (HostMapP->SysPort > p->RIOLastPortsMapped)
904                 p->RIOLastPortsMapped = HostMapP->SysPort;
905
906         return 0;
907 }
908
909 int RIOChangeName(p, MapP)
910 struct rio_info *p;
911 struct Map *MapP;
912 {
913         int host;
914         struct Map *HostMapP;
915         char *sptr;
916
917         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);
918
919         if (MapP->ID > MAX_RUP) {
920                 rio_dprintk(RIO_DEBUG_TABLE, "Bad ID in map entry!\n");
921                 p->RIOError.Error = ID_NUMBER_OUT_OF_RANGE;
922                 return -EINVAL;
923         }
924
925         MapP->Name[MAX_NAME_LEN - 1] = '\0';
926         sptr = MapP->Name;
927
928         while (*sptr) {
929                 if (*sptr < ' ' || *sptr > '~') {
930                         rio_dprintk(RIO_DEBUG_TABLE, "Name entry contains non-printing characters!\n");
931                         p->RIOError.Error = BAD_CHARACTER_IN_NAME;
932                         return -EINVAL;
933                 }
934                 sptr++;
935         }
936
937         for (host = 0; host < p->RIONumHosts; host++) {
938                 if (MapP->HostUniqueNum == p->RIOHosts[host].UniqueNum) {
939                         if ((p->RIOHosts[host].Flags & RUN_STATE) != RC_RUNNING) {
940                                 p->RIOError.Error = HOST_NOT_RUNNING;
941                                 return -ENXIO;
942                         }
943                         if (MapP->ID == 0) {
944                                 CCOPY(MapP->Name, p->RIOHosts[host].Name, MAX_NAME_LEN);
945                                 return 0;
946                         }
947
948                         HostMapP = &p->RIOHosts[host].Mapping[MapP->ID - 1];
949
950                         if (HostMapP->RtaUniqueNum != MapP->RtaUniqueNum) {
951                                 p->RIOError.Error = RTA_NUMBER_WRONG;
952                                 return -ENXIO;
953                         }
954                         CCOPY(MapP->Name, HostMapP->Name, MAX_NAME_LEN);
955                         return 0;
956                 }
957         }
958         p->RIOError.Error = UNKNOWN_HOST_NUMBER;
959         rio_dprintk(RIO_DEBUG_TABLE, "Unknown host %x\n", MapP->HostUniqueNum);
960         return -ENXIO;
961 }