Merge git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[linux-2.6] / drivers / char / rio / rioctrl.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          : rioctrl.c
24 **      SID             : 1.3
25 **      Last Modified   : 11/6/98 10:33:42
26 **      Retrieved       : 11/6/98 10:33:49
27 **
28 **  ident @(#)rioctrl.c 1.3
29 **
30 ** -----------------------------------------------------------------------------
31 */
32 #ifdef SCCS_LABELS
33 static char *_rioctrl_c_sccs_ = "@(#)rioctrl.c  1.3";
34 #endif
35
36
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/errno.h>
40 #include <asm/io.h>
41 #include <asm/system.h>
42 #include <asm/string.h>
43 #include <asm/semaphore.h>
44 #include <asm/uaccess.h>
45
46 #include <linux/termios.h>
47 #include <linux/serial.h>
48
49 #include <linux/generic_serial.h>
50
51
52 #include "linux_compat.h"
53 #include "rio_linux.h"
54 #include "pkt.h"
55 #include "daemon.h"
56 #include "rio.h"
57 #include "riospace.h"
58 #include "cmdpkt.h"
59 #include "map.h"
60 #include "rup.h"
61 #include "port.h"
62 #include "riodrvr.h"
63 #include "rioinfo.h"
64 #include "func.h"
65 #include "errors.h"
66 #include "pci.h"
67
68 #include "parmmap.h"
69 #include "unixrup.h"
70 #include "board.h"
71 #include "host.h"
72 #include "phb.h"
73 #include "link.h"
74 #include "cmdblk.h"
75 #include "route.h"
76 #include "cirrus.h"
77 #include "rioioctl.h"
78
79
80 static struct LpbReq LpbReq;
81 static struct RupReq RupReq;
82 static struct PortReq PortReq;
83 static struct HostReq HostReq;  /* oh really?  global?  and no locking? */
84 static struct HostDpRam HostDpRam;
85 static struct DebugCtrl DebugCtrl;
86 static struct Map MapEnt;
87 static struct PortSetup PortSetup;
88 static struct DownLoad DownLoad;
89 static struct SendPack SendPack;
90 /* static struct StreamInfo     StreamInfo; */
91 /* static char modemtable[RIO_PORTS]; */
92 static struct SpecialRupCmd SpecialRupCmd;
93 static struct PortParams PortParams;
94 static struct portStats portStats;
95
96 static struct SubCmdStruct {
97         ushort Host;
98         ushort Rup;
99         ushort Port;
100         ushort Addr;
101 } SubCmd;
102
103 struct PortTty {
104         uint port;
105         struct ttystatics Tty;
106 };
107
108 static struct PortTty PortTty;
109 typedef struct ttystatics TERMIO;
110
111 /*
112 ** This table is used when the config.rio downloads bin code to the
113 ** driver. We index the table using the product code, 0-F, and call
114 ** the function pointed to by the entry, passing the information
115 ** about the boot.
116 ** The RIOBootCodeUNKNOWN entry is there to politely tell the calling
117 ** process to bog off.
118 */
119 static int
120  (*RIOBootTable[MAX_PRODUCT]) (struct rio_info *, struct DownLoad *) = {
121                                         /* 0 */ RIOBootCodeHOST,
122                                         /* Host Card */
123                                         /* 1 */ RIOBootCodeRTA,
124                                         /* RTA */
125 };
126
127 #define drv_makedev(maj, min) ((((uint) maj & 0xff) << 8) | ((uint) min & 0xff))
128
129 static int copy_from_io(void __user *to, void __iomem *from, size_t size)
130 {
131         void *buf = kmalloc(size, GFP_KERNEL);
132         int res = -ENOMEM;
133         if (buf) {
134                 rio_memcpy_fromio(buf, from, size);
135                 res = copy_to_user(to, buf, size);
136                 kfree(buf);
137         }
138         return res;
139 }
140
141 int riocontrol(struct rio_info *p, dev_t dev, int cmd, unsigned long arg, int su)
142 {
143         uint Host;              /* leave me unsigned! */
144         uint port;              /* and me! */
145         struct Host *HostP;
146         ushort loop;
147         int Entry;
148         struct Port *PortP;
149         struct PKT __iomem *PacketP;
150         int retval = 0;
151         unsigned long flags;
152         void __user *argp = (void __user *)arg;
153
154         func_enter();
155
156         /* Confuse the compiler to think that we've initialized these */
157         Host = 0;
158         PortP = NULL;
159
160         rio_dprintk(RIO_DEBUG_CTRL, "control ioctl cmd: 0x%x arg: %p\n", cmd, argp);
161
162         switch (cmd) {
163                 /*
164                  ** RIO_SET_TIMER
165                  **
166                  ** Change the value of the host card interrupt timer.
167                  ** If the host card number is -1 then all host cards are changed
168                  ** otherwise just the specified host card will be changed.
169                  */
170         case RIO_SET_TIMER:
171                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET_TIMER to %ldms\n", arg);
172                 {
173                         int host, value;
174                         host = (arg >> 16) & 0x0000FFFF;
175                         value = arg & 0x0000ffff;
176                         if (host == -1) {
177                                 for (host = 0; host < p->RIONumHosts; host++) {
178                                         if (p->RIOHosts[host].Flags == RC_RUNNING) {
179                                                 writew(value, &p->RIOHosts[host].ParmMapP->timer);
180                                         }
181                                 }
182                         } else if (host >= p->RIONumHosts) {
183                                 return -EINVAL;
184                         } else {
185                                 if (p->RIOHosts[host].Flags == RC_RUNNING) {
186                                         writew(value, &p->RIOHosts[host].ParmMapP->timer);
187                                 }
188                         }
189                 }
190                 return 0;
191
192         case RIO_FOAD_RTA:
193                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_FOAD_RTA\n");
194                 return RIOCommandRta(p, arg, RIOFoadRta);
195
196         case RIO_ZOMBIE_RTA:
197                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_ZOMBIE_RTA\n");
198                 return RIOCommandRta(p, arg, RIOZombieRta);
199
200         case RIO_IDENTIFY_RTA:
201                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_IDENTIFY_RTA\n");
202                 return RIOIdentifyRta(p, argp);
203
204         case RIO_KILL_NEIGHBOUR:
205                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_KILL_NEIGHBOUR\n");
206                 return RIOKillNeighbour(p, argp);
207
208         case SPECIAL_RUP_CMD:
209                 {
210                         struct CmdBlk *CmdBlkP;
211
212                         rio_dprintk(RIO_DEBUG_CTRL, "SPECIAL_RUP_CMD\n");
213                         if (copy_from_user(&SpecialRupCmd, argp, sizeof(SpecialRupCmd))) {
214                                 rio_dprintk(RIO_DEBUG_CTRL, "SPECIAL_RUP_CMD copy failed\n");
215                                 p->RIOError.Error = COPYIN_FAILED;
216                                 return -EFAULT;
217                         }
218                         CmdBlkP = RIOGetCmdBlk();
219                         if (!CmdBlkP) {
220                                 rio_dprintk(RIO_DEBUG_CTRL, "SPECIAL_RUP_CMD GetCmdBlk failed\n");
221                                 return -ENXIO;
222                         }
223                         CmdBlkP->Packet = SpecialRupCmd.Packet;
224                         if (SpecialRupCmd.Host >= p->RIONumHosts)
225                                 SpecialRupCmd.Host = 0;
226                         rio_dprintk(RIO_DEBUG_CTRL, "Queue special rup command for host %d rup %d\n", SpecialRupCmd.Host, SpecialRupCmd.RupNum);
227                         if (RIOQueueCmdBlk(&p->RIOHosts[SpecialRupCmd.Host], SpecialRupCmd.RupNum, CmdBlkP) == RIO_FAIL) {
228                                 printk(KERN_WARNING "rio: FAILED TO QUEUE SPECIAL RUP COMMAND\n");
229                         }
230                         return 0;
231                 }
232
233         case RIO_DEBUG_MEM:
234                 return -EPERM;
235
236         case RIO_ALL_MODEM:
237                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_ALL_MODEM\n");
238                 p->RIOError.Error = IOCTL_COMMAND_UNKNOWN;
239                 return -EINVAL;
240
241         case RIO_GET_TABLE:
242                 /*
243                  ** Read the routing table from the device driver to user space
244                  */
245                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_TABLE\n");
246
247                 if ((retval = RIOApel(p)) != 0)
248                         return retval;
249
250                 if (copy_to_user(argp, p->RIOConnectTable, TOTAL_MAP_ENTRIES * sizeof(struct Map))) {
251                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_TABLE copy failed\n");
252                         p->RIOError.Error = COPYOUT_FAILED;
253                         return -EFAULT;
254                 }
255
256                 {
257                         int entry;
258                         rio_dprintk(RIO_DEBUG_CTRL, "*****\nMAP ENTRIES\n");
259                         for (entry = 0; entry < TOTAL_MAP_ENTRIES; entry++) {
260                                 if ((p->RIOConnectTable[entry].ID == 0) && (p->RIOConnectTable[entry].HostUniqueNum == 0) && (p->RIOConnectTable[entry].RtaUniqueNum == 0))
261                                         continue;
262
263                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.HostUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].HostUniqueNum);
264                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.RtaUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].RtaUniqueNum);
265                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.ID = 0x%x\n", entry, p->RIOConnectTable[entry].ID);
266                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.ID2 = 0x%x\n", entry, p->RIOConnectTable[entry].ID2);
267                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Flags = 0x%x\n", entry, (int) p->RIOConnectTable[entry].Flags);
268                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.SysPort = 0x%x\n", entry, (int) p->RIOConnectTable[entry].SysPort);
269                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[0].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[0].Unit);
270                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[0].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[0].Link);
271                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[1].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[1].Unit);
272                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[1].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[1].Link);
273                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[2].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[2].Unit);
274                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[2].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[2].Link);
275                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[3].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[3].Unit);
276                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[4].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[3].Link);
277                                 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Name = %s\n", entry, p->RIOConnectTable[entry].Name);
278                         }
279                         rio_dprintk(RIO_DEBUG_CTRL, "*****\nEND MAP ENTRIES\n");
280                 }
281                 p->RIOQuickCheck = NOT_CHANGED; /* a table has been gotten */
282                 return 0;
283
284         case RIO_PUT_TABLE:
285                 /*
286                  ** Write the routing table to the device driver from user space
287                  */
288                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_TABLE\n");
289
290                 if (!su) {
291                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_TABLE !Root\n");
292                         p->RIOError.Error = NOT_SUPER_USER;
293                         return -EPERM;
294                 }
295                 if (copy_from_user(&p->RIOConnectTable[0], argp, TOTAL_MAP_ENTRIES * sizeof(struct Map))) {
296                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_TABLE copy failed\n");
297                         p->RIOError.Error = COPYIN_FAILED;
298                         return -EFAULT;
299                 }
300 /*
301 ***********************************
302                                 {
303                                         int entry;
304                                         rio_dprint(RIO_DEBUG_CTRL,  ("*****\nMAP ENTRIES\n") );
305                                         for ( entry=0; entry<TOTAL_MAP_ENTRIES; entry++ )
306                                         {
307                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.HostUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].HostUniqueNum ) );
308                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.RtaUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].RtaUniqueNum ) );
309                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.ID = 0x%x\n", entry, p->RIOConnectTable[entry].ID ) );
310                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.ID2 = 0x%x\n", entry, p->RIOConnectTable[entry].ID2 ) );
311                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Flags = 0x%x\n", entry, p->RIOConnectTable[entry].Flags ) );
312                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.SysPort = 0x%x\n", entry, p->RIOConnectTable[entry].SysPort ) );
313                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[0].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[0].Unit ) );
314                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[0].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[0].Link ) );
315                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[1].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[1].Unit ) );
316                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[1].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[1].Link ) );
317                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[2].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[2].Unit ) );
318                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[2].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[2].Link ) );
319                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[3].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[3].Unit ) );
320                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[4].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[3].Link ) );
321                                                 rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Name = %s\n", entry, p->RIOConnectTable[entry].Name ) );
322                                         }
323                                         rio_dprint(RIO_DEBUG_CTRL,  ("*****\nEND MAP ENTRIES\n") );
324                                 }
325 ***********************************
326 */
327                 return RIONewTable(p);
328
329         case RIO_GET_BINDINGS:
330                 /*
331                  ** Send bindings table, containing unique numbers of RTAs owned
332                  ** by this system to user space
333                  */
334                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_BINDINGS\n");
335
336                 if (!su) {
337                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_BINDINGS !Root\n");
338                         p->RIOError.Error = NOT_SUPER_USER;
339                         return -EPERM;
340                 }
341                 if (copy_to_user(argp, p->RIOBindTab, (sizeof(ulong) * MAX_RTA_BINDINGS))) {
342                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_BINDINGS copy failed\n");
343                         p->RIOError.Error = COPYOUT_FAILED;
344                         return -EFAULT;
345                 }
346                 return 0;
347
348         case RIO_PUT_BINDINGS:
349                 /*
350                  ** Receive a bindings table, containing unique numbers of RTAs owned
351                  ** by this system
352                  */
353                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_BINDINGS\n");
354
355                 if (!su) {
356                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_BINDINGS !Root\n");
357                         p->RIOError.Error = NOT_SUPER_USER;
358                         return -EPERM;
359                 }
360                 if (copy_from_user(&p->RIOBindTab[0], argp, (sizeof(ulong) * MAX_RTA_BINDINGS))) {
361                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_BINDINGS copy failed\n");
362                         p->RIOError.Error = COPYIN_FAILED;
363                         return -EFAULT;
364                 }
365                 return 0;
366
367         case RIO_BIND_RTA:
368                 {
369                         int EmptySlot = -1;
370                         /*
371                          ** Bind this RTA to host, so that it will be booted by
372                          ** host in 'boot owned RTAs' mode.
373                          */
374                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_BIND_RTA\n");
375
376                         if (!su) {
377                                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_BIND_RTA !Root\n");
378                                 p->RIOError.Error = NOT_SUPER_USER;
379                                 return -EPERM;
380                         }
381                         for (Entry = 0; Entry < MAX_RTA_BINDINGS; Entry++) {
382                                 if ((EmptySlot == -1) && (p->RIOBindTab[Entry] == 0L))
383                                         EmptySlot = Entry;
384                                 else if (p->RIOBindTab[Entry] == arg) {
385                                         /*
386                                          ** Already exists - delete
387                                          */
388                                         p->RIOBindTab[Entry] = 0L;
389                                         rio_dprintk(RIO_DEBUG_CTRL, "Removing Rta %ld from p->RIOBindTab\n", arg);
390                                         return 0;
391                                 }
392                         }
393                         /*
394                          ** Dosen't exist - add
395                          */
396                         if (EmptySlot != -1) {
397                                 p->RIOBindTab[EmptySlot] = arg;
398                                 rio_dprintk(RIO_DEBUG_CTRL, "Adding Rta %lx to p->RIOBindTab\n", arg);
399                         } else {
400                                 rio_dprintk(RIO_DEBUG_CTRL, "p->RIOBindTab full! - Rta %lx not added\n", arg);
401                                 return -ENOMEM;
402                         }
403                         return 0;
404                 }
405
406         case RIO_RESUME:
407                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME\n");
408                 port = arg;
409                 if ((port < 0) || (port > 511)) {
410                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME: Bad port number %d\n", port);
411                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
412                         return -EINVAL;
413                 }
414                 PortP = p->RIOPortp[port];
415                 if (!PortP->Mapped) {
416                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME: Port %d not mapped\n", port);
417                         p->RIOError.Error = PORT_NOT_MAPPED_INTO_SYSTEM;
418                         return -EINVAL;
419                 }
420                 if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
421                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME: Port %d not open\n", port);
422                         return -EINVAL;
423                 }
424
425                 rio_spin_lock_irqsave(&PortP->portSem, flags);
426                 if (RIOPreemptiveCmd(p, (p->RIOPortp[port]), RESUME) == RIO_FAIL) {
427                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME failed\n");
428                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
429                         return -EBUSY;
430                 } else {
431                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME: Port %d resumed\n", port);
432                         PortP->State |= RIO_BUSY;
433                 }
434                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
435                 return retval;
436
437         case RIO_ASSIGN_RTA:
438                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_ASSIGN_RTA\n");
439                 if (!su) {
440                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_ASSIGN_RTA !Root\n");
441                         p->RIOError.Error = NOT_SUPER_USER;
442                         return -EPERM;
443                 }
444                 if (copy_from_user(&MapEnt, argp, sizeof(MapEnt))) {
445                         rio_dprintk(RIO_DEBUG_CTRL, "Copy from user space failed\n");
446                         p->RIOError.Error = COPYIN_FAILED;
447                         return -EFAULT;
448                 }
449                 return RIOAssignRta(p, &MapEnt);
450
451         case RIO_CHANGE_NAME:
452                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_CHANGE_NAME\n");
453                 if (!su) {
454                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_CHANGE_NAME !Root\n");
455                         p->RIOError.Error = NOT_SUPER_USER;
456                         return -EPERM;
457                 }
458                 if (copy_from_user(&MapEnt, argp, sizeof(MapEnt))) {
459                         rio_dprintk(RIO_DEBUG_CTRL, "Copy from user space failed\n");
460                         p->RIOError.Error = COPYIN_FAILED;
461                         return -EFAULT;
462                 }
463                 return RIOChangeName(p, &MapEnt);
464
465         case RIO_DELETE_RTA:
466                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_DELETE_RTA\n");
467                 if (!su) {
468                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_DELETE_RTA !Root\n");
469                         p->RIOError.Error = NOT_SUPER_USER;
470                         return -EPERM;
471                 }
472                 if (copy_from_user(&MapEnt, argp, sizeof(MapEnt))) {
473                         rio_dprintk(RIO_DEBUG_CTRL, "Copy from data space failed\n");
474                         p->RIOError.Error = COPYIN_FAILED;
475                         return -EFAULT;
476                 }
477                 return RIODeleteRta(p, &MapEnt);
478
479         case RIO_QUICK_CHECK:
480                 if (copy_to_user(argp, &p->RIORtaDisCons, sizeof(unsigned int))) {
481                         p->RIOError.Error = COPYOUT_FAILED;
482                         return -EFAULT;
483                 }
484                 return 0;
485
486         case RIO_LAST_ERROR:
487                 if (copy_to_user(argp, &p->RIOError, sizeof(struct Error)))
488                         return -EFAULT;
489                 return 0;
490
491         case RIO_GET_LOG:
492                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_LOG\n");
493                 return -EINVAL;
494
495         case RIO_GET_MODTYPE:
496                 if (copy_from_user(&port, argp, sizeof(unsigned int))) {
497                         p->RIOError.Error = COPYIN_FAILED;
498                         return -EFAULT;
499                 }
500                 rio_dprintk(RIO_DEBUG_CTRL, "Get module type for port %d\n", port);
501                 if (port < 0 || port > 511) {
502                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_MODTYPE: Bad port number %d\n", port);
503                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
504                         return -EINVAL;
505                 }
506                 PortP = (p->RIOPortp[port]);
507                 if (!PortP->Mapped) {
508                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_MODTYPE: Port %d not mapped\n", port);
509                         p->RIOError.Error = PORT_NOT_MAPPED_INTO_SYSTEM;
510                         return -EINVAL;
511                 }
512                 /*
513                  ** Return module type of port
514                  */
515                 port = PortP->HostP->UnixRups[PortP->RupNum].ModTypes;
516                 if (copy_to_user(argp, &port, sizeof(unsigned int))) {
517                         p->RIOError.Error = COPYOUT_FAILED;
518                         return -EFAULT;
519                 }
520                 return (0);
521         case RIO_BLOCK_OPENS:
522                 rio_dprintk(RIO_DEBUG_CTRL, "Opens block until booted\n");
523                 for (Entry = 0; Entry < RIO_PORTS; Entry++) {
524                         rio_spin_lock_irqsave(&PortP->portSem, flags);
525                         p->RIOPortp[Entry]->WaitUntilBooted = 1;
526                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
527                 }
528                 return 0;
529
530         case RIO_SETUP_PORTS:
531                 rio_dprintk(RIO_DEBUG_CTRL, "Setup ports\n");
532                 if (copy_from_user(&PortSetup, argp, sizeof(PortSetup))) {
533                         p->RIOError.Error = COPYIN_FAILED;
534                         rio_dprintk(RIO_DEBUG_CTRL, "EFAULT");
535                         return -EFAULT;
536                 }
537                 if (PortSetup.From > PortSetup.To || PortSetup.To >= RIO_PORTS) {
538                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
539                         rio_dprintk(RIO_DEBUG_CTRL, "ENXIO");
540                         return -ENXIO;
541                 }
542                 if (PortSetup.XpCps > p->RIOConf.MaxXpCps || PortSetup.XpCps < p->RIOConf.MinXpCps) {
543                         p->RIOError.Error = XPRINT_CPS_OUT_OF_RANGE;
544                         rio_dprintk(RIO_DEBUG_CTRL, "EINVAL");
545                         return -EINVAL;
546                 }
547                 if (!p->RIOPortp) {
548                         printk(KERN_ERR "rio: No p->RIOPortp array!\n");
549                         rio_dprintk(RIO_DEBUG_CTRL, "No p->RIOPortp array!\n");
550                         return -EIO;
551                 }
552                 rio_dprintk(RIO_DEBUG_CTRL, "entering loop (%d %d)!\n", PortSetup.From, PortSetup.To);
553                 for (loop = PortSetup.From; loop <= PortSetup.To; loop++) {
554                         rio_dprintk(RIO_DEBUG_CTRL, "in loop (%d)!\n", loop);
555                 }
556                 rio_dprintk(RIO_DEBUG_CTRL, "after loop (%d)!\n", loop);
557                 rio_dprintk(RIO_DEBUG_CTRL, "Retval:%x\n", retval);
558                 return retval;
559
560         case RIO_GET_PORT_SETUP:
561                 rio_dprintk(RIO_DEBUG_CTRL, "Get port setup\n");
562                 if (copy_from_user(&PortSetup, argp, sizeof(PortSetup))) {
563                         p->RIOError.Error = COPYIN_FAILED;
564                         return -EFAULT;
565                 }
566                 if (PortSetup.From >= RIO_PORTS) {
567                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
568                         return -ENXIO;
569                 }
570
571                 port = PortSetup.To = PortSetup.From;
572                 PortSetup.IxAny = (p->RIOPortp[port]->Config & RIO_IXANY) ? 1 : 0;
573                 PortSetup.IxOn = (p->RIOPortp[port]->Config & RIO_IXON) ? 1 : 0;
574                 PortSetup.Drain = (p->RIOPortp[port]->Config & RIO_WAITDRAIN) ? 1 : 0;
575                 PortSetup.Store = p->RIOPortp[port]->Store;
576                 PortSetup.Lock = p->RIOPortp[port]->Lock;
577                 PortSetup.XpCps = p->RIOPortp[port]->Xprint.XpCps;
578                 memcpy(PortSetup.XpOn, p->RIOPortp[port]->Xprint.XpOn, MAX_XP_CTRL_LEN);
579                 memcpy(PortSetup.XpOff, p->RIOPortp[port]->Xprint.XpOff, MAX_XP_CTRL_LEN);
580                 PortSetup.XpOn[MAX_XP_CTRL_LEN - 1] = '\0';
581                 PortSetup.XpOff[MAX_XP_CTRL_LEN - 1] = '\0';
582
583                 if (copy_to_user(argp, &PortSetup, sizeof(PortSetup))) {
584                         p->RIOError.Error = COPYOUT_FAILED;
585                         return -EFAULT;
586                 }
587                 return retval;
588
589         case RIO_GET_PORT_PARAMS:
590                 rio_dprintk(RIO_DEBUG_CTRL, "Get port params\n");
591                 if (copy_from_user(&PortParams, argp, sizeof(struct PortParams))) {
592                         p->RIOError.Error = COPYIN_FAILED;
593                         return -EFAULT;
594                 }
595                 if (PortParams.Port >= RIO_PORTS) {
596                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
597                         return -ENXIO;
598                 }
599                 PortP = (p->RIOPortp[PortParams.Port]);
600                 PortParams.Config = PortP->Config;
601                 PortParams.State = PortP->State;
602                 rio_dprintk(RIO_DEBUG_CTRL, "Port %d\n", PortParams.Port);
603
604                 if (copy_to_user(argp, &PortParams, sizeof(struct PortParams))) {
605                         p->RIOError.Error = COPYOUT_FAILED;
606                         return -EFAULT;
607                 }
608                 return retval;
609
610         case RIO_GET_PORT_TTY:
611                 rio_dprintk(RIO_DEBUG_CTRL, "Get port tty\n");
612                 if (copy_from_user(&PortTty, argp, sizeof(struct PortTty))) {
613                         p->RIOError.Error = COPYIN_FAILED;
614                         return -EFAULT;
615                 }
616                 if (PortTty.port >= RIO_PORTS) {
617                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
618                         return -ENXIO;
619                 }
620
621                 rio_dprintk(RIO_DEBUG_CTRL, "Port %d\n", PortTty.port);
622                 PortP = (p->RIOPortp[PortTty.port]);
623                 if (copy_to_user(argp, &PortTty, sizeof(struct PortTty))) {
624                         p->RIOError.Error = COPYOUT_FAILED;
625                         return -EFAULT;
626                 }
627                 return retval;
628
629         case RIO_SET_PORT_TTY:
630                 if (copy_from_user(&PortTty, argp, sizeof(struct PortTty))) {
631                         p->RIOError.Error = COPYIN_FAILED;
632                         return -EFAULT;
633                 }
634                 rio_dprintk(RIO_DEBUG_CTRL, "Set port %d tty\n", PortTty.port);
635                 if (PortTty.port >= (ushort) RIO_PORTS) {
636                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
637                         return -ENXIO;
638                 }
639                 PortP = (p->RIOPortp[PortTty.port]);
640                 RIOParam(PortP, CONFIG, PortP->State & RIO_MODEM, OK_TO_SLEEP);
641                 return retval;
642
643         case RIO_SET_PORT_PARAMS:
644                 rio_dprintk(RIO_DEBUG_CTRL, "Set port params\n");
645                 if (copy_from_user(&PortParams, argp, sizeof(PortParams))) {
646                         p->RIOError.Error = COPYIN_FAILED;
647                         return -EFAULT;
648                 }
649                 if (PortParams.Port >= (ushort) RIO_PORTS) {
650                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
651                         return -ENXIO;
652                 }
653                 PortP = (p->RIOPortp[PortParams.Port]);
654                 rio_spin_lock_irqsave(&PortP->portSem, flags);
655                 PortP->Config = PortParams.Config;
656                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
657                 return retval;
658
659         case RIO_GET_PORT_STATS:
660                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_PORT_STATS\n");
661                 if (copy_from_user(&portStats, argp, sizeof(struct portStats))) {
662                         p->RIOError.Error = COPYIN_FAILED;
663                         return -EFAULT;
664                 }
665                 if (portStats.port >= RIO_PORTS) {
666                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
667                         return -ENXIO;
668                 }
669                 PortP = (p->RIOPortp[portStats.port]);
670                 portStats.gather = PortP->statsGather;
671                 portStats.txchars = PortP->txchars;
672                 portStats.rxchars = PortP->rxchars;
673                 portStats.opens = PortP->opens;
674                 portStats.closes = PortP->closes;
675                 portStats.ioctls = PortP->ioctls;
676                 if (copy_to_user(argp, &portStats, sizeof(struct portStats))) {
677                         p->RIOError.Error = COPYOUT_FAILED;
678                         return -EFAULT;
679                 }
680                 return retval;
681
682         case RIO_RESET_PORT_STATS:
683                 port = arg;
684                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESET_PORT_STATS\n");
685                 if (port >= RIO_PORTS) {
686                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
687                         return -ENXIO;
688                 }
689                 PortP = (p->RIOPortp[port]);
690                 rio_spin_lock_irqsave(&PortP->portSem, flags);
691                 PortP->txchars = 0;
692                 PortP->rxchars = 0;
693                 PortP->opens = 0;
694                 PortP->closes = 0;
695                 PortP->ioctls = 0;
696                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
697                 return retval;
698
699         case RIO_GATHER_PORT_STATS:
700                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GATHER_PORT_STATS\n");
701                 if (copy_from_user(&portStats, argp, sizeof(struct portStats))) {
702                         p->RIOError.Error = COPYIN_FAILED;
703                         return -EFAULT;
704                 }
705                 if (portStats.port >= RIO_PORTS) {
706                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
707                         return -ENXIO;
708                 }
709                 PortP = (p->RIOPortp[portStats.port]);
710                 rio_spin_lock_irqsave(&PortP->portSem, flags);
711                 PortP->statsGather = portStats.gather;
712                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
713                 return retval;
714
715         case RIO_READ_CONFIG:
716                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_READ_CONFIG\n");
717                 if (copy_to_user(argp, &p->RIOConf, sizeof(struct Conf))) {
718                         p->RIOError.Error = COPYOUT_FAILED;
719                         return -EFAULT;
720                 }
721                 return retval;
722
723         case RIO_SET_CONFIG:
724                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET_CONFIG\n");
725                 if (!su) {
726                         p->RIOError.Error = NOT_SUPER_USER;
727                         return -EPERM;
728                 }
729                 if (copy_from_user(&p->RIOConf, argp, sizeof(struct Conf))) {
730                         p->RIOError.Error = COPYIN_FAILED;
731                         return -EFAULT;
732                 }
733                 /*
734                  ** move a few value around
735                  */
736                 for (Host = 0; Host < p->RIONumHosts; Host++)
737                         if ((p->RIOHosts[Host].Flags & RUN_STATE) == RC_RUNNING)
738                                 writew(p->RIOConf.Timer, &p->RIOHosts[Host].ParmMapP->timer);
739                 return retval;
740
741         case RIO_START_POLLER:
742                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_START_POLLER\n");
743                 return -EINVAL;
744
745         case RIO_STOP_POLLER:
746                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_STOP_POLLER\n");
747                 if (!su) {
748                         p->RIOError.Error = NOT_SUPER_USER;
749                         return -EPERM;
750                 }
751                 p->RIOPolling = NOT_POLLING;
752                 return retval;
753
754         case RIO_SETDEBUG:
755         case RIO_GETDEBUG:
756                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SETDEBUG/RIO_GETDEBUG\n");
757                 if (copy_from_user(&DebugCtrl, argp, sizeof(DebugCtrl))) {
758                         p->RIOError.Error = COPYIN_FAILED;
759                         return -EFAULT;
760                 }
761                 if (DebugCtrl.SysPort == NO_PORT) {
762                         if (cmd == RIO_SETDEBUG) {
763                                 if (!su) {
764                                         p->RIOError.Error = NOT_SUPER_USER;
765                                         return -EPERM;
766                                 }
767                                 p->rio_debug = DebugCtrl.Debug;
768                                 p->RIODebugWait = DebugCtrl.Wait;
769                                 rio_dprintk(RIO_DEBUG_CTRL, "Set global debug to 0x%x set wait to 0x%x\n", p->rio_debug, p->RIODebugWait);
770                         } else {
771                                 rio_dprintk(RIO_DEBUG_CTRL, "Get global debug 0x%x wait 0x%x\n", p->rio_debug, p->RIODebugWait);
772                                 DebugCtrl.Debug = p->rio_debug;
773                                 DebugCtrl.Wait = p->RIODebugWait;
774                                 if (copy_to_user(argp, &DebugCtrl, sizeof(DebugCtrl))) {
775                                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET/GET DEBUG: bad port number %d\n", DebugCtrl.SysPort);
776                                         p->RIOError.Error = COPYOUT_FAILED;
777                                         return -EFAULT;
778                                 }
779                         }
780                 } else if (DebugCtrl.SysPort >= RIO_PORTS && DebugCtrl.SysPort != NO_PORT) {
781                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET/GET DEBUG: bad port number %d\n", DebugCtrl.SysPort);
782                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
783                         return -ENXIO;
784                 } else if (cmd == RIO_SETDEBUG) {
785                         if (!su) {
786                                 p->RIOError.Error = NOT_SUPER_USER;
787                                 return -EPERM;
788                         }
789                         rio_spin_lock_irqsave(&PortP->portSem, flags);
790                         p->RIOPortp[DebugCtrl.SysPort]->Debug = DebugCtrl.Debug;
791                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
792                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_SETDEBUG 0x%x\n", p->RIOPortp[DebugCtrl.SysPort]->Debug);
793                 } else {
794                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_GETDEBUG 0x%x\n", p->RIOPortp[DebugCtrl.SysPort]->Debug);
795                         DebugCtrl.Debug = p->RIOPortp[DebugCtrl.SysPort]->Debug;
796                         if (copy_to_user(argp, &DebugCtrl, sizeof(DebugCtrl))) {
797                                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GETDEBUG: Bad copy to user space\n");
798                                 p->RIOError.Error = COPYOUT_FAILED;
799                                 return -EFAULT;
800                         }
801                 }
802                 return retval;
803
804         case RIO_VERSID:
805                 /*
806                  ** Enquire about the release and version.
807                  ** We return MAX_VERSION_LEN bytes, being a
808                  ** textual null terminated string.
809                  */
810                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_VERSID\n");
811                 if (copy_to_user(argp, RIOVersid(), sizeof(struct rioVersion))) {
812                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_VERSID: Bad copy to user space (host=%d)\n", Host);
813                         p->RIOError.Error = COPYOUT_FAILED;
814                         return -EFAULT;
815                 }
816                 return retval;
817
818         case RIO_NUM_HOSTS:
819                 /*
820                  ** Enquire as to the number of hosts located
821                  ** at init time.
822                  */
823                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_NUM_HOSTS\n");
824                 if (copy_to_user(argp, &p->RIONumHosts, sizeof(p->RIONumHosts))) {
825                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_NUM_HOSTS: Bad copy to user space\n");
826                         p->RIOError.Error = COPYOUT_FAILED;
827                         return -EFAULT;
828                 }
829                 return retval;
830
831         case RIO_HOST_FOAD:
832                 /*
833                  ** Kill host. This may not be in the final version...
834                  */
835                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_FOAD %ld\n", arg);
836                 if (!su) {
837                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_FOAD: Not super user\n");
838                         p->RIOError.Error = NOT_SUPER_USER;
839                         return -EPERM;
840                 }
841                 p->RIOHalted = 1;
842                 p->RIOSystemUp = 0;
843
844                 for (Host = 0; Host < p->RIONumHosts; Host++) {
845                         (void) RIOBoardTest(p->RIOHosts[Host].PaddrP, p->RIOHosts[Host].Caddr, p->RIOHosts[Host].Type, p->RIOHosts[Host].Slot);
846                         memset(&p->RIOHosts[Host].Flags, 0, ((char *) &p->RIOHosts[Host].____end_marker____) - ((char *) &p->RIOHosts[Host].Flags));
847                         p->RIOHosts[Host].Flags = RC_WAITING;
848                 }
849                 RIOFoadWakeup(p);
850                 p->RIONumBootPkts = 0;
851                 p->RIOBooting = 0;
852                 printk("HEEEEELP!\n");
853
854                 for (loop = 0; loop < RIO_PORTS; loop++) {
855                         spin_lock_init(&p->RIOPortp[loop]->portSem);
856                         p->RIOPortp[loop]->InUse = NOT_INUSE;
857                 }
858
859                 p->RIOSystemUp = 0;
860                 return retval;
861
862         case RIO_DOWNLOAD:
863                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_DOWNLOAD\n");
864                 if (!su) {
865                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_DOWNLOAD: Not super user\n");
866                         p->RIOError.Error = NOT_SUPER_USER;
867                         return -EPERM;
868                 }
869                 if (copy_from_user(&DownLoad, argp, sizeof(DownLoad))) {
870                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_DOWNLOAD: Copy in from user space failed\n");
871                         p->RIOError.Error = COPYIN_FAILED;
872                         return -EFAULT;
873                 }
874                 rio_dprintk(RIO_DEBUG_CTRL, "Copied in download code for product code 0x%x\n", DownLoad.ProductCode);
875
876                 /*
877                  ** It is important that the product code is an unsigned object!
878                  */
879                 if (DownLoad.ProductCode > MAX_PRODUCT) {
880                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_DOWNLOAD: Bad product code %d passed\n", DownLoad.ProductCode);
881                         p->RIOError.Error = NO_SUCH_PRODUCT;
882                         return -ENXIO;
883                 }
884                 /*
885                  ** do something!
886                  */
887                 retval = (*(RIOBootTable[DownLoad.ProductCode])) (p, &DownLoad);
888                 /* <-- Panic */
889                 p->RIOHalted = 0;
890                 /*
891                  ** and go back, content with a job well completed.
892                  */
893                 return retval;
894
895         case RIO_PARMS:
896                 {
897                         unsigned int host;
898
899                         if (copy_from_user(&host, argp, sizeof(host))) {
900                                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_REQ: Copy in from user space failed\n");
901                                 p->RIOError.Error = COPYIN_FAILED;
902                                 return -EFAULT;
903                         }
904                         /*
905                          ** Fetch the parmmap
906                          */
907                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_PARMS\n");
908                         if (copy_from_io(argp, p->RIOHosts[host].ParmMapP, sizeof(PARM_MAP))) {
909                                 p->RIOError.Error = COPYOUT_FAILED;
910                                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PARMS: Copy out to user space failed\n");
911                                 return -EFAULT;
912                         }
913                 }
914                 return retval;
915
916         case RIO_HOST_REQ:
917                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_REQ\n");
918                 if (copy_from_user(&HostReq, argp, sizeof(HostReq))) {
919                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_REQ: Copy in from user space failed\n");
920                         p->RIOError.Error = COPYIN_FAILED;
921                         return -EFAULT;
922                 }
923                 if (HostReq.HostNum >= p->RIONumHosts) {
924                         p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
925                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_REQ: Illegal host number %d\n", HostReq.HostNum);
926                         return -ENXIO;
927                 }
928                 rio_dprintk(RIO_DEBUG_CTRL, "Request for host %d\n", HostReq.HostNum);
929
930                 if (copy_to_user(HostReq.HostP, &p->RIOHosts[HostReq.HostNum], sizeof(struct Host))) {
931                         p->RIOError.Error = COPYOUT_FAILED;
932                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_REQ: Bad copy to user space\n");
933                         return -EFAULT;
934                 }
935                 return retval;
936
937         case RIO_HOST_DPRAM:
938                 rio_dprintk(RIO_DEBUG_CTRL, "Request for DPRAM\n");
939                 if (copy_from_user(&HostDpRam, argp, sizeof(HostDpRam))) {
940                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Copy in from user space failed\n");
941                         p->RIOError.Error = COPYIN_FAILED;
942                         return -EFAULT;
943                 }
944                 if (HostDpRam.HostNum >= p->RIONumHosts) {
945                         p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
946                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Illegal host number %d\n", HostDpRam.HostNum);
947                         return -ENXIO;
948                 }
949                 rio_dprintk(RIO_DEBUG_CTRL, "Request for host %d\n", HostDpRam.HostNum);
950
951                 if (p->RIOHosts[HostDpRam.HostNum].Type == RIO_PCI) {
952                         int off;
953                         /* It's hardware like this that really gets on my tits. */
954                         static unsigned char copy[sizeof(struct DpRam)];
955                         for (off = 0; off < sizeof(struct DpRam); off++)
956                                 copy[off] = readb(p->RIOHosts[HostDpRam.HostNum].Caddr + off);
957                         if (copy_to_user(HostDpRam.DpRamP, copy, sizeof(struct DpRam))) {
958                                 p->RIOError.Error = COPYOUT_FAILED;
959                                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Bad copy to user space\n");
960                                 return -EFAULT;
961                         }
962                 } else if (copy_from_io(HostDpRam.DpRamP, p->RIOHosts[HostDpRam.HostNum].Caddr, sizeof(struct DpRam))) {
963                         p->RIOError.Error = COPYOUT_FAILED;
964                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Bad copy to user space\n");
965                         return -EFAULT;
966                 }
967                 return retval;
968
969         case RIO_SET_BUSY:
970                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET_BUSY\n");
971                 if (arg > 511) {
972                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET_BUSY: Bad port number %ld\n", arg);
973                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
974                         return -EINVAL;
975                 }
976                 rio_spin_lock_irqsave(&PortP->portSem, flags);
977                 p->RIOPortp[arg]->State |= RIO_BUSY;
978                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
979                 return retval;
980
981         case RIO_HOST_PORT:
982                 /*
983                  ** The daemon want port information
984                  ** (probably for debug reasons)
985                  */
986                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_PORT\n");
987                 if (copy_from_user(&PortReq, argp, sizeof(PortReq))) {
988                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_PORT: Copy in from user space failed\n");
989                         p->RIOError.Error = COPYIN_FAILED;
990                         return -EFAULT;
991                 }
992
993                 if (PortReq.SysPort >= RIO_PORTS) {     /* SysPort is unsigned */
994                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_PORT: Illegal port number %d\n", PortReq.SysPort);
995                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
996                         return -ENXIO;
997                 }
998                 rio_dprintk(RIO_DEBUG_CTRL, "Request for port %d\n", PortReq.SysPort);
999                 if (copy_to_user(PortReq.PortP, p->RIOPortp[PortReq.SysPort], sizeof(struct Port))) {
1000                         p->RIOError.Error = COPYOUT_FAILED;
1001                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_PORT: Bad copy to user space\n");
1002                         return -EFAULT;
1003                 }
1004                 return retval;
1005
1006         case RIO_HOST_RUP:
1007                 /*
1008                  ** The daemon want rup information
1009                  ** (probably for debug reasons)
1010                  */
1011                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP\n");
1012                 if (copy_from_user(&RupReq, argp, sizeof(RupReq))) {
1013                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP: Copy in from user space failed\n");
1014                         p->RIOError.Error = COPYIN_FAILED;
1015                         return -EFAULT;
1016                 }
1017                 if (RupReq.HostNum >= p->RIONumHosts) { /* host is unsigned */
1018                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP: Illegal host number %d\n", RupReq.HostNum);
1019                         p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1020                         return -ENXIO;
1021                 }
1022                 if (RupReq.RupNum >= MAX_RUP + LINKS_PER_UNIT) {        /* eek! */
1023                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP: Illegal rup number %d\n", RupReq.RupNum);
1024                         p->RIOError.Error = RUP_NUMBER_OUT_OF_RANGE;
1025                         return -EINVAL;
1026                 }
1027                 HostP = &p->RIOHosts[RupReq.HostNum];
1028
1029                 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
1030                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP: Host %d not running\n", RupReq.HostNum);
1031                         p->RIOError.Error = HOST_NOT_RUNNING;
1032                         return -EIO;
1033                 }
1034                 rio_dprintk(RIO_DEBUG_CTRL, "Request for rup %d from host %d\n", RupReq.RupNum, RupReq.HostNum);
1035
1036                 if (copy_from_io(RupReq.RupP, HostP->UnixRups[RupReq.RupNum].RupP, sizeof(struct RUP))) {
1037                         p->RIOError.Error = COPYOUT_FAILED;
1038                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP: Bad copy to user space\n");
1039                         return -EFAULT;
1040                 }
1041                 return retval;
1042
1043         case RIO_HOST_LPB:
1044                 /*
1045                  ** The daemon want lpb information
1046                  ** (probably for debug reasons)
1047                  */
1048                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB\n");
1049                 if (copy_from_user(&LpbReq, argp, sizeof(LpbReq))) {
1050                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB: Bad copy from user space\n");
1051                         p->RIOError.Error = COPYIN_FAILED;
1052                         return -EFAULT;
1053                 }
1054                 if (LpbReq.Host >= p->RIONumHosts) {    /* host is unsigned */
1055                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB: Illegal host number %d\n", LpbReq.Host);
1056                         p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1057                         return -ENXIO;
1058                 }
1059                 if (LpbReq.Link >= LINKS_PER_UNIT) {    /* eek! */
1060                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB: Illegal link number %d\n", LpbReq.Link);
1061                         p->RIOError.Error = LINK_NUMBER_OUT_OF_RANGE;
1062                         return -EINVAL;
1063                 }
1064                 HostP = &p->RIOHosts[LpbReq.Host];
1065
1066                 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
1067                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB: Host %d not running\n", LpbReq.Host);
1068                         p->RIOError.Error = HOST_NOT_RUNNING;
1069                         return -EIO;
1070                 }
1071                 rio_dprintk(RIO_DEBUG_CTRL, "Request for lpb %d from host %d\n", LpbReq.Link, LpbReq.Host);
1072
1073                 if (copy_from_io(LpbReq.LpbP, &HostP->LinkStrP[LpbReq.Link], sizeof(struct LPB))) {
1074                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB: Bad copy to user space\n");
1075                         p->RIOError.Error = COPYOUT_FAILED;
1076                         return -EFAULT;
1077                 }
1078                 return retval;
1079
1080                 /*
1081                  ** Here 3 IOCTL's that allow us to change the way in which
1082                  ** rio logs errors. send them just to syslog or send them
1083                  ** to both syslog and console or send them to just the console.
1084                  **
1085                  ** See RioStrBuf() in util.c for the other half.
1086                  */
1087         case RIO_SYSLOG_ONLY:
1088                 p->RIOPrintLogState = PRINT_TO_LOG;     /* Just syslog */
1089                 return 0;
1090
1091         case RIO_SYSLOG_CONS:
1092                 p->RIOPrintLogState = PRINT_TO_LOG_CONS;        /* syslog and console */
1093                 return 0;
1094
1095         case RIO_CONS_ONLY:
1096                 p->RIOPrintLogState = PRINT_TO_CONS;    /* Just console */
1097                 return 0;
1098
1099         case RIO_SIGNALS_ON:
1100                 if (p->RIOSignalProcess) {
1101                         p->RIOError.Error = SIGNALS_ALREADY_SET;
1102                         return -EBUSY;
1103                 }
1104                 /* FIXME: PID tracking */
1105                 p->RIOSignalProcess = current->pid;
1106                 p->RIOPrintDisabled = DONT_PRINT;
1107                 return retval;
1108
1109         case RIO_SIGNALS_OFF:
1110                 if (p->RIOSignalProcess != current->pid) {
1111                         p->RIOError.Error = NOT_RECEIVING_PROCESS;
1112                         return -EPERM;
1113                 }
1114                 rio_dprintk(RIO_DEBUG_CTRL, "Clear signal process to zero\n");
1115                 p->RIOSignalProcess = 0;
1116                 return retval;
1117
1118         case RIO_SET_BYTE_MODE:
1119                 for (Host = 0; Host < p->RIONumHosts; Host++)
1120                         if (p->RIOHosts[Host].Type == RIO_AT)
1121                                 p->RIOHosts[Host].Mode &= ~WORD_OPERATION;
1122                 return retval;
1123
1124         case RIO_SET_WORD_MODE:
1125                 for (Host = 0; Host < p->RIONumHosts; Host++)
1126                         if (p->RIOHosts[Host].Type == RIO_AT)
1127                                 p->RIOHosts[Host].Mode |= WORD_OPERATION;
1128                 return retval;
1129
1130         case RIO_SET_FAST_BUS:
1131                 for (Host = 0; Host < p->RIONumHosts; Host++)
1132                         if (p->RIOHosts[Host].Type == RIO_AT)
1133                                 p->RIOHosts[Host].Mode |= FAST_AT_BUS;
1134                 return retval;
1135
1136         case RIO_SET_SLOW_BUS:
1137                 for (Host = 0; Host < p->RIONumHosts; Host++)
1138                         if (p->RIOHosts[Host].Type == RIO_AT)
1139                                 p->RIOHosts[Host].Mode &= ~FAST_AT_BUS;
1140                 return retval;
1141
1142         case RIO_MAP_B50_TO_50:
1143         case RIO_MAP_B50_TO_57600:
1144         case RIO_MAP_B110_TO_110:
1145         case RIO_MAP_B110_TO_115200:
1146                 rio_dprintk(RIO_DEBUG_CTRL, "Baud rate mapping\n");
1147                 port = arg;
1148                 if (port < 0 || port > 511) {
1149                         rio_dprintk(RIO_DEBUG_CTRL, "Baud rate mapping: Bad port number %d\n", port);
1150                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1151                         return -EINVAL;
1152                 }
1153                 rio_spin_lock_irqsave(&PortP->portSem, flags);
1154                 switch (cmd) {
1155                 case RIO_MAP_B50_TO_50:
1156                         p->RIOPortp[port]->Config |= RIO_MAP_50_TO_50;
1157                         break;
1158                 case RIO_MAP_B50_TO_57600:
1159                         p->RIOPortp[port]->Config &= ~RIO_MAP_50_TO_50;
1160                         break;
1161                 case RIO_MAP_B110_TO_110:
1162                         p->RIOPortp[port]->Config |= RIO_MAP_110_TO_110;
1163                         break;
1164                 case RIO_MAP_B110_TO_115200:
1165                         p->RIOPortp[port]->Config &= ~RIO_MAP_110_TO_110;
1166                         break;
1167                 }
1168                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1169                 return retval;
1170
1171         case RIO_STREAM_INFO:
1172                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_STREAM_INFO\n");
1173                 return -EINVAL;
1174
1175         case RIO_SEND_PACKET:
1176                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SEND_PACKET\n");
1177                 if (copy_from_user(&SendPack, argp, sizeof(SendPack))) {
1178                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_SEND_PACKET: Bad copy from user space\n");
1179                         p->RIOError.Error = COPYIN_FAILED;
1180                         return -EFAULT;
1181                 }
1182                 if (SendPack.PortNum >= 128) {
1183                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1184                         return -ENXIO;
1185                 }
1186
1187                 PortP = p->RIOPortp[SendPack.PortNum];
1188                 rio_spin_lock_irqsave(&PortP->portSem, flags);
1189
1190                 if (!can_add_transmit(&PacketP, PortP)) {
1191                         p->RIOError.Error = UNIT_IS_IN_USE;
1192                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1193                         return -ENOSPC;
1194                 }
1195
1196                 for (loop = 0; loop < (ushort) (SendPack.Len & 127); loop++)
1197                         writeb(SendPack.Data[loop], &PacketP->data[loop]);
1198
1199                 writeb(SendPack.Len, &PacketP->len);
1200
1201                 add_transmit(PortP);
1202                 /*
1203                  ** Count characters transmitted for port statistics reporting
1204                  */
1205                 if (PortP->statsGather)
1206                         PortP->txchars += (SendPack.Len & 127);
1207                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1208                 return retval;
1209
1210         case RIO_NO_MESG:
1211                 if (su)
1212                         p->RIONoMessage = 1;
1213                 return su ? 0 : -EPERM;
1214
1215         case RIO_MESG:
1216                 if (su)
1217                         p->RIONoMessage = 0;
1218                 return su ? 0 : -EPERM;
1219
1220         case RIO_WHAT_MESG:
1221                 if (copy_to_user(argp, &p->RIONoMessage, sizeof(p->RIONoMessage))) {
1222                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_WHAT_MESG: Bad copy to user space\n");
1223                         p->RIOError.Error = COPYOUT_FAILED;
1224                         return -EFAULT;
1225                 }
1226                 return 0;
1227
1228         case RIO_MEM_DUMP:
1229                 if (copy_from_user(&SubCmd, argp, sizeof(struct SubCmdStruct))) {
1230                         p->RIOError.Error = COPYIN_FAILED;
1231                         return -EFAULT;
1232                 }
1233                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_MEM_DUMP host %d rup %d addr %x\n", SubCmd.Host, SubCmd.Rup, SubCmd.Addr);
1234
1235                 if (SubCmd.Rup >= MAX_RUP + LINKS_PER_UNIT) {
1236                         p->RIOError.Error = RUP_NUMBER_OUT_OF_RANGE;
1237                         return -EINVAL;
1238                 }
1239
1240                 if (SubCmd.Host >= p->RIONumHosts) {
1241                         p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1242                         return -EINVAL;
1243                 }
1244
1245                 port = p->RIOHosts[SubCmd.Host].UnixRups[SubCmd.Rup].BaseSysPort;
1246
1247                 PortP = p->RIOPortp[port];
1248
1249                 rio_spin_lock_irqsave(&PortP->portSem, flags);
1250
1251                 if (RIOPreemptiveCmd(p, PortP, MEMDUMP) == RIO_FAIL) {
1252                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_MEM_DUMP failed\n");
1253                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1254                         return -EBUSY;
1255                 } else
1256                         PortP->State |= RIO_BUSY;
1257
1258                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1259                 if (copy_to_user(argp, p->RIOMemDump, MEMDUMP_SIZE)) {
1260                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_MEM_DUMP copy failed\n");
1261                         p->RIOError.Error = COPYOUT_FAILED;
1262                         return -EFAULT;
1263                 }
1264                 return 0;
1265
1266         case RIO_TICK:
1267                 if (arg >= p->RIONumHosts)
1268                         return -EINVAL;
1269                 rio_dprintk(RIO_DEBUG_CTRL, "Set interrupt for host %ld\n", arg);
1270                 writeb(0xFF, &p->RIOHosts[arg].SetInt);
1271                 return 0;
1272
1273         case RIO_TOCK:
1274                 if (arg >= p->RIONumHosts)
1275                         return -EINVAL;
1276                 rio_dprintk(RIO_DEBUG_CTRL, "Clear interrupt for host %ld\n", arg);
1277                 writeb(0xFF, &p->RIOHosts[arg].ResetInt);
1278                 return 0;
1279
1280         case RIO_READ_CHECK:
1281                 /* Check reads for pkts with data[0] the same */
1282                 p->RIOReadCheck = !p->RIOReadCheck;
1283                 if (copy_to_user(argp, &p->RIOReadCheck, sizeof(unsigned int))) {
1284                         p->RIOError.Error = COPYOUT_FAILED;
1285                         return -EFAULT;
1286                 }
1287                 return 0;
1288
1289         case RIO_READ_REGISTER:
1290                 if (copy_from_user(&SubCmd, argp, sizeof(struct SubCmdStruct))) {
1291                         p->RIOError.Error = COPYIN_FAILED;
1292                         return -EFAULT;
1293                 }
1294                 rio_dprintk(RIO_DEBUG_CTRL, "RIO_READ_REGISTER host %d rup %d port %d reg %x\n", SubCmd.Host, SubCmd.Rup, SubCmd.Port, SubCmd.Addr);
1295
1296                 if (SubCmd.Port > 511) {
1297                         rio_dprintk(RIO_DEBUG_CTRL, "Baud rate mapping: Bad port number %d\n", SubCmd.Port);
1298                         p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1299                         return -EINVAL;
1300                 }
1301
1302                 if (SubCmd.Rup >= MAX_RUP + LINKS_PER_UNIT) {
1303                         p->RIOError.Error = RUP_NUMBER_OUT_OF_RANGE;
1304                         return -EINVAL;
1305                 }
1306
1307                 if (SubCmd.Host >= p->RIONumHosts) {
1308                         p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1309                         return -EINVAL;
1310                 }
1311
1312                 port = p->RIOHosts[SubCmd.Host].UnixRups[SubCmd.Rup].BaseSysPort + SubCmd.Port;
1313                 PortP = p->RIOPortp[port];
1314
1315                 rio_spin_lock_irqsave(&PortP->portSem, flags);
1316
1317                 if (RIOPreemptiveCmd(p, PortP, READ_REGISTER) == RIO_FAIL) {
1318                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_READ_REGISTER failed\n");
1319                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1320                         return -EBUSY;
1321                 } else
1322                         PortP->State |= RIO_BUSY;
1323
1324                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1325                 if (copy_to_user(argp, &p->CdRegister, sizeof(unsigned int))) {
1326                         rio_dprintk(RIO_DEBUG_CTRL, "RIO_READ_REGISTER copy failed\n");
1327                         p->RIOError.Error = COPYOUT_FAILED;
1328                         return -EFAULT;
1329                 }
1330                 return 0;
1331                 /*
1332                  ** rio_make_dev: given port number (0-511) ORed with port type
1333                  ** (RIO_DEV_DIRECT, RIO_DEV_MODEM, RIO_DEV_XPRINT) return dev_t
1334                  ** value to pass to mknod to create the correct device node.
1335                  */
1336         case RIO_MAKE_DEV:
1337                 {
1338                         unsigned int port = arg & RIO_MODEM_MASK;
1339                         unsigned int ret;
1340
1341                         switch (arg & RIO_DEV_MASK) {
1342                         case RIO_DEV_DIRECT:
1343                                 ret = drv_makedev(MAJOR(dev), port);
1344                                 rio_dprintk(RIO_DEBUG_CTRL, "Makedev direct 0x%x is 0x%x\n", port, ret);
1345                                 return ret;
1346                         case RIO_DEV_MODEM:
1347                                 ret = drv_makedev(MAJOR(dev), (port | RIO_MODEM_BIT));
1348                                 rio_dprintk(RIO_DEBUG_CTRL, "Makedev modem 0x%x is 0x%x\n", port, ret);
1349                                 return ret;
1350                         case RIO_DEV_XPRINT:
1351                                 ret = drv_makedev(MAJOR(dev), port);
1352                                 rio_dprintk(RIO_DEBUG_CTRL, "Makedev printer 0x%x is 0x%x\n", port, ret);
1353                                 return ret;
1354                         }
1355                         rio_dprintk(RIO_DEBUG_CTRL, "MAKE Device is called\n");
1356                         return -EINVAL;
1357                 }
1358                 /*
1359                  ** rio_minor: given a dev_t from a stat() call, return
1360                  ** the port number (0-511) ORed with the port type
1361                  ** ( RIO_DEV_DIRECT, RIO_DEV_MODEM, RIO_DEV_XPRINT )
1362                  */
1363         case RIO_MINOR:
1364                 {
1365                         dev_t dv;
1366                         int mino;
1367                         unsigned long ret;
1368
1369                         dv = (dev_t) (arg);
1370                         mino = RIO_UNMODEM(dv);
1371
1372                         if (RIO_ISMODEM(dv)) {
1373                                 rio_dprintk(RIO_DEBUG_CTRL, "Minor for device 0x%x: modem %d\n", dv, mino);
1374                                 ret = mino | RIO_DEV_MODEM;
1375                         } else {
1376                                 rio_dprintk(RIO_DEBUG_CTRL, "Minor for device 0x%x: direct %d\n", dv, mino);
1377                                 ret = mino | RIO_DEV_DIRECT;
1378                         }
1379                         return ret;
1380                 }
1381         }
1382         rio_dprintk(RIO_DEBUG_CTRL, "INVALID DAEMON IOCTL 0x%x\n", cmd);
1383         p->RIOError.Error = IOCTL_COMMAND_UNKNOWN;
1384
1385         func_exit();
1386         return -EINVAL;
1387 }
1388
1389 /*
1390 ** Pre-emptive commands go on RUPs and are only one byte long.
1391 */
1392 int RIOPreemptiveCmd(struct rio_info *p, struct Port *PortP, u8 Cmd)
1393 {
1394         struct CmdBlk *CmdBlkP;
1395         struct PktCmd_M *PktCmdP;
1396         int Ret;
1397         ushort rup;
1398         int port;
1399
1400         if (PortP->State & RIO_DELETED) {
1401                 rio_dprintk(RIO_DEBUG_CTRL, "Preemptive command to deleted RTA ignored\n");
1402                 return RIO_FAIL;
1403         }
1404
1405         if ((PortP->InUse == (typeof(PortP->InUse))-1) ||
1406                         !(CmdBlkP = RIOGetCmdBlk())) {
1407                 rio_dprintk(RIO_DEBUG_CTRL, "Cannot allocate command block "
1408                         "for command %d on port %d\n", Cmd, PortP->PortNum);
1409                 return RIO_FAIL;
1410         }
1411
1412         rio_dprintk(RIO_DEBUG_CTRL, "Command blk %p - InUse now %d\n",
1413                         CmdBlkP, PortP->InUse);
1414
1415         PktCmdP = (struct PktCmd_M *)&CmdBlkP->Packet.data[0];
1416
1417         CmdBlkP->Packet.src_unit = 0;
1418         if (PortP->SecondBlock)
1419                 rup = PortP->ID2;
1420         else
1421                 rup = PortP->RupNum;
1422         CmdBlkP->Packet.dest_unit = rup;
1423         CmdBlkP->Packet.src_port = COMMAND_RUP;
1424         CmdBlkP->Packet.dest_port = COMMAND_RUP;
1425         CmdBlkP->Packet.len = PKT_CMD_BIT | 2;
1426         CmdBlkP->PostFuncP = RIOUnUse;
1427         CmdBlkP->PostArg = (unsigned long) PortP;
1428         PktCmdP->Command = Cmd;
1429         port = PortP->HostPort % (ushort) PORTS_PER_RTA;
1430         /*
1431          ** Index ports 8-15 for 2nd block of 16 port RTA.
1432          */
1433         if (PortP->SecondBlock)
1434                 port += (ushort) PORTS_PER_RTA;
1435         PktCmdP->PhbNum = port;
1436
1437         switch (Cmd) {
1438         case MEMDUMP:
1439                 rio_dprintk(RIO_DEBUG_CTRL, "Queue MEMDUMP command blk %p "
1440                                 "(addr 0x%x)\n", CmdBlkP, (int) SubCmd.Addr);
1441                 PktCmdP->SubCommand = MEMDUMP;
1442                 PktCmdP->SubAddr = SubCmd.Addr;
1443                 break;
1444         case FCLOSE:
1445                 rio_dprintk(RIO_DEBUG_CTRL, "Queue FCLOSE command blk %p\n",
1446                                 CmdBlkP);
1447                 break;
1448         case READ_REGISTER:
1449                 rio_dprintk(RIO_DEBUG_CTRL, "Queue READ_REGISTER (0x%x) "
1450                                 "command blk %p\n", (int) SubCmd.Addr, CmdBlkP);
1451                 PktCmdP->SubCommand = READ_REGISTER;
1452                 PktCmdP->SubAddr = SubCmd.Addr;
1453                 break;
1454         case RESUME:
1455                 rio_dprintk(RIO_DEBUG_CTRL, "Queue RESUME command blk %p\n",
1456                                 CmdBlkP);
1457                 break;
1458         case RFLUSH:
1459                 rio_dprintk(RIO_DEBUG_CTRL, "Queue RFLUSH command blk %p\n",
1460                                 CmdBlkP);
1461                 CmdBlkP->PostFuncP = RIORFlushEnable;
1462                 break;
1463         case SUSPEND:
1464                 rio_dprintk(RIO_DEBUG_CTRL, "Queue SUSPEND command blk %p\n",
1465                                 CmdBlkP);
1466                 break;
1467
1468         case MGET:
1469                 rio_dprintk(RIO_DEBUG_CTRL, "Queue MGET command blk %p\n",
1470                                 CmdBlkP);
1471                 break;
1472
1473         case MSET:
1474         case MBIC:
1475         case MBIS:
1476                 CmdBlkP->Packet.data[4] = (char) PortP->ModemLines;
1477                 rio_dprintk(RIO_DEBUG_CTRL, "Queue MSET/MBIC/MBIS command "
1478                                 "blk %p\n", CmdBlkP);
1479                 break;
1480
1481         case WFLUSH:
1482                 /*
1483                  ** If we have queued up the maximum number of Write flushes
1484                  ** allowed then we should not bother sending any more to the
1485                  ** RTA.
1486                  */
1487                 if (PortP->WflushFlag == (typeof(PortP->WflushFlag))-1) {
1488                         rio_dprintk(RIO_DEBUG_CTRL, "Trashed WFLUSH, "
1489                                         "WflushFlag about to wrap!");
1490                         RIOFreeCmdBlk(CmdBlkP);
1491                         return (RIO_FAIL);
1492                 } else {
1493                         rio_dprintk(RIO_DEBUG_CTRL, "Queue WFLUSH command "
1494                                         "blk %p\n", CmdBlkP);
1495                         CmdBlkP->PostFuncP = RIOWFlushMark;
1496                 }
1497                 break;
1498         }
1499
1500         PortP->InUse++;
1501
1502         Ret = RIOQueueCmdBlk(PortP->HostP, rup, CmdBlkP);
1503
1504         return Ret;
1505 }