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