Pull vector-domain into release branch
[linux-2.6] / drivers / char / rio / riocmd.c
1 /*
2 ** -----------------------------------------------------------------------------
3 **
4 **  Perle Specialix driver for Linux
5 **  ported from the existing SCO driver source
6 **
7  *
8  *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
9  *
10  *      This program is free software; you can redistribute it and/or modify
11  *      it under the terms of the GNU General Public License as published by
12  *      the Free Software Foundation; either version 2 of the License, or
13  *      (at your option) any later version.
14  *
15  *      This program is distributed in the hope that it will be useful,
16  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *      GNU General Public License for more details.
19  *
20  *      You should have received a copy of the GNU General Public License
21  *      along with this program; if not, write to the Free Software
22  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 **
24 **      Module          : riocmd.c
25 **      SID             : 1.2
26 **      Last Modified   : 11/6/98 10:33:41
27 **      Retrieved       : 11/6/98 10:33:49
28 **
29 **  ident @(#)riocmd.c  1.2
30 **
31 ** -----------------------------------------------------------------------------
32 */
33 #ifdef SCCS_LABELS
34 static char *_riocmd_c_sccs_ = "@(#)riocmd.c    1.2";
35 #endif
36
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/errno.h>
40 #include <linux/tty.h>
41 #include <asm/io.h>
42 #include <asm/system.h>
43 #include <asm/string.h>
44 #include <asm/semaphore.h>
45 #include <asm/uaccess.h>
46
47 #include <linux/termios.h>
48 #include <linux/serial.h>
49
50 #include <linux/generic_serial.h>
51
52 #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
78
79 static struct IdentifyRta IdRta;
80 static struct KillNeighbour KillUnit;
81
82 int RIOFoadRta(struct Host *HostP, struct Map *MapP)
83 {
84         struct CmdBlk *CmdBlkP;
85
86         rio_dprintk(RIO_DEBUG_CMD, "FOAD RTA\n");
87
88         CmdBlkP = RIOGetCmdBlk();
89
90         if (!CmdBlkP) {
91                 rio_dprintk(RIO_DEBUG_CMD, "FOAD RTA: GetCmdBlk failed\n");
92                 return -ENXIO;
93         }
94
95         CmdBlkP->Packet.dest_unit = MapP->ID;
96         CmdBlkP->Packet.dest_port = BOOT_RUP;
97         CmdBlkP->Packet.src_unit = 0;
98         CmdBlkP->Packet.src_port = BOOT_RUP;
99         CmdBlkP->Packet.len = 0x84;
100         CmdBlkP->Packet.data[0] = IFOAD;
101         CmdBlkP->Packet.data[1] = 0;
102         CmdBlkP->Packet.data[2] = IFOAD_MAGIC & 0xFF;
103         CmdBlkP->Packet.data[3] = (IFOAD_MAGIC >> 8) & 0xFF;
104
105         if (RIOQueueCmdBlk(HostP, MapP->ID - 1, CmdBlkP) == RIO_FAIL) {
106                 rio_dprintk(RIO_DEBUG_CMD, "FOAD RTA: Failed to queue foad command\n");
107                 return -EIO;
108         }
109         return 0;
110 }
111
112 int RIOZombieRta(struct Host *HostP, struct Map *MapP)
113 {
114         struct CmdBlk *CmdBlkP;
115
116         rio_dprintk(RIO_DEBUG_CMD, "ZOMBIE RTA\n");
117
118         CmdBlkP = RIOGetCmdBlk();
119
120         if (!CmdBlkP) {
121                 rio_dprintk(RIO_DEBUG_CMD, "ZOMBIE RTA: GetCmdBlk failed\n");
122                 return -ENXIO;
123         }
124
125         CmdBlkP->Packet.dest_unit = MapP->ID;
126         CmdBlkP->Packet.dest_port = BOOT_RUP;
127         CmdBlkP->Packet.src_unit = 0;
128         CmdBlkP->Packet.src_port = BOOT_RUP;
129         CmdBlkP->Packet.len = 0x84;
130         CmdBlkP->Packet.data[0] = ZOMBIE;
131         CmdBlkP->Packet.data[1] = 0;
132         CmdBlkP->Packet.data[2] = ZOMBIE_MAGIC & 0xFF;
133         CmdBlkP->Packet.data[3] = (ZOMBIE_MAGIC >> 8) & 0xFF;
134
135         if (RIOQueueCmdBlk(HostP, MapP->ID - 1, CmdBlkP) == RIO_FAIL) {
136                 rio_dprintk(RIO_DEBUG_CMD, "ZOMBIE RTA: Failed to queue zombie command\n");
137                 return -EIO;
138         }
139         return 0;
140 }
141
142 int RIOCommandRta(struct rio_info *p, unsigned long RtaUnique, int (*func) (struct Host * HostP, struct Map * MapP))
143 {
144         unsigned int Host;
145
146         rio_dprintk(RIO_DEBUG_CMD, "Command RTA 0x%lx func %p\n", RtaUnique, func);
147
148         if (!RtaUnique)
149                 return (0);
150
151         for (Host = 0; Host < p->RIONumHosts; Host++) {
152                 unsigned int Rta;
153                 struct Host *HostP = &p->RIOHosts[Host];
154
155                 for (Rta = 0; Rta < RTAS_PER_HOST; Rta++) {
156                         struct Map *MapP = &HostP->Mapping[Rta];
157
158                         if (MapP->RtaUniqueNum == RtaUnique) {
159                                 uint Link;
160
161                                 /*
162                                  ** now, lets just check we have a route to it...
163                                  ** IF the routing stuff is working, then one of the
164                                  ** topology entries for this unit will have a legit
165                                  ** route *somewhere*. We care not where - if its got
166                                  ** any connections, we can get to it.
167                                  */
168                                 for (Link = 0; Link < LINKS_PER_UNIT; Link++) {
169                                         if (MapP->Topology[Link].Unit <= (u8) MAX_RUP) {
170                                                 /*
171                                                  ** Its worth trying the operation...
172                                                  */
173                                                 return (*func) (HostP, MapP);
174                                         }
175                                 }
176                         }
177                 }
178         }
179         return -ENXIO;
180 }
181
182
183 int RIOIdentifyRta(struct rio_info *p, void __user * arg)
184 {
185         unsigned int Host;
186
187         if (copy_from_user(&IdRta, arg, sizeof(IdRta))) {
188                 rio_dprintk(RIO_DEBUG_CMD, "RIO_IDENTIFY_RTA copy failed\n");
189                 p->RIOError.Error = COPYIN_FAILED;
190                 return -EFAULT;
191         }
192
193         for (Host = 0; Host < p->RIONumHosts; Host++) {
194                 unsigned int Rta;
195                 struct Host *HostP = &p->RIOHosts[Host];
196
197                 for (Rta = 0; Rta < RTAS_PER_HOST; Rta++) {
198                         struct Map *MapP = &HostP->Mapping[Rta];
199
200                         if (MapP->RtaUniqueNum == IdRta.RtaUnique) {
201                                 uint Link;
202                                 /*
203                                  ** now, lets just check we have a route to it...
204                                  ** IF the routing stuff is working, then one of the
205                                  ** topology entries for this unit will have a legit
206                                  ** route *somewhere*. We care not where - if its got
207                                  ** any connections, we can get to it.
208                                  */
209                                 for (Link = 0; Link < LINKS_PER_UNIT; Link++) {
210                                         if (MapP->Topology[Link].Unit <= (u8) MAX_RUP) {
211                                                 /*
212                                                  ** Its worth trying the operation...
213                                                  */
214                                                 struct CmdBlk *CmdBlkP;
215
216                                                 rio_dprintk(RIO_DEBUG_CMD, "IDENTIFY RTA\n");
217
218                                                 CmdBlkP = RIOGetCmdBlk();
219
220                                                 if (!CmdBlkP) {
221                                                         rio_dprintk(RIO_DEBUG_CMD, "IDENTIFY RTA: GetCmdBlk failed\n");
222                                                         return -ENXIO;
223                                                 }
224
225                                                 CmdBlkP->Packet.dest_unit = MapP->ID;
226                                                 CmdBlkP->Packet.dest_port = BOOT_RUP;
227                                                 CmdBlkP->Packet.src_unit = 0;
228                                                 CmdBlkP->Packet.src_port = BOOT_RUP;
229                                                 CmdBlkP->Packet.len = 0x84;
230                                                 CmdBlkP->Packet.data[0] = IDENTIFY;
231                                                 CmdBlkP->Packet.data[1] = 0;
232                                                 CmdBlkP->Packet.data[2] = IdRta.ID;
233
234                                                 if (RIOQueueCmdBlk(HostP, MapP->ID - 1, CmdBlkP) == RIO_FAIL) {
235                                                         rio_dprintk(RIO_DEBUG_CMD, "IDENTIFY RTA: Failed to queue command\n");
236                                                         return -EIO;
237                                                 }
238                                                 return 0;
239                                         }
240                                 }
241                         }
242                 }
243         }
244         return -ENOENT;
245 }
246
247
248 int RIOKillNeighbour(struct rio_info *p, void __user * arg)
249 {
250         uint Host;
251         uint ID;
252         struct Host *HostP;
253         struct CmdBlk *CmdBlkP;
254
255         rio_dprintk(RIO_DEBUG_CMD, "KILL HOST NEIGHBOUR\n");
256
257         if (copy_from_user(&KillUnit, arg, sizeof(KillUnit))) {
258                 rio_dprintk(RIO_DEBUG_CMD, "RIO_KILL_NEIGHBOUR copy failed\n");
259                 p->RIOError.Error = COPYIN_FAILED;
260                 return -EFAULT;
261         }
262
263         if (KillUnit.Link > 3)
264                 return -ENXIO;
265
266         CmdBlkP = RIOGetCmdBlk();
267
268         if (!CmdBlkP) {
269                 rio_dprintk(RIO_DEBUG_CMD, "UFOAD: GetCmdBlk failed\n");
270                 return -ENXIO;
271         }
272
273         CmdBlkP->Packet.dest_unit = 0;
274         CmdBlkP->Packet.src_unit = 0;
275         CmdBlkP->Packet.dest_port = BOOT_RUP;
276         CmdBlkP->Packet.src_port = BOOT_RUP;
277         CmdBlkP->Packet.len = 0x84;
278         CmdBlkP->Packet.data[0] = UFOAD;
279         CmdBlkP->Packet.data[1] = KillUnit.Link;
280         CmdBlkP->Packet.data[2] = UFOAD_MAGIC & 0xFF;
281         CmdBlkP->Packet.data[3] = (UFOAD_MAGIC >> 8) & 0xFF;
282
283         for (Host = 0; Host < p->RIONumHosts; Host++) {
284                 ID = 0;
285                 HostP = &p->RIOHosts[Host];
286
287                 if (HostP->UniqueNum == KillUnit.UniqueNum) {
288                         if (RIOQueueCmdBlk(HostP, RTAS_PER_HOST + KillUnit.Link, CmdBlkP) == RIO_FAIL) {
289                                 rio_dprintk(RIO_DEBUG_CMD, "UFOAD: Failed queue command\n");
290                                 return -EIO;
291                         }
292                         return 0;
293                 }
294
295                 for (ID = 0; ID < RTAS_PER_HOST; ID++) {
296                         if (HostP->Mapping[ID].RtaUniqueNum == KillUnit.UniqueNum) {
297                                 CmdBlkP->Packet.dest_unit = ID + 1;
298                                 if (RIOQueueCmdBlk(HostP, ID, CmdBlkP) == RIO_FAIL) {
299                                         rio_dprintk(RIO_DEBUG_CMD, "UFOAD: Failed queue command\n");
300                                         return -EIO;
301                                 }
302                                 return 0;
303                         }
304                 }
305         }
306         RIOFreeCmdBlk(CmdBlkP);
307         return -ENXIO;
308 }
309
310 int RIOSuspendBootRta(struct Host *HostP, int ID, int Link)
311 {
312         struct CmdBlk *CmdBlkP;
313
314         rio_dprintk(RIO_DEBUG_CMD, "SUSPEND BOOT ON RTA ID %d, link %c\n", ID, 'A' + Link);
315
316         CmdBlkP = RIOGetCmdBlk();
317
318         if (!CmdBlkP) {
319                 rio_dprintk(RIO_DEBUG_CMD, "SUSPEND BOOT ON RTA: GetCmdBlk failed\n");
320                 return -ENXIO;
321         }
322
323         CmdBlkP->Packet.dest_unit = ID;
324         CmdBlkP->Packet.dest_port = BOOT_RUP;
325         CmdBlkP->Packet.src_unit = 0;
326         CmdBlkP->Packet.src_port = BOOT_RUP;
327         CmdBlkP->Packet.len = 0x84;
328         CmdBlkP->Packet.data[0] = IWAIT;
329         CmdBlkP->Packet.data[1] = Link;
330         CmdBlkP->Packet.data[2] = IWAIT_MAGIC & 0xFF;
331         CmdBlkP->Packet.data[3] = (IWAIT_MAGIC >> 8) & 0xFF;
332
333         if (RIOQueueCmdBlk(HostP, ID - 1, CmdBlkP) == RIO_FAIL) {
334                 rio_dprintk(RIO_DEBUG_CMD, "SUSPEND BOOT ON RTA: Failed to queue iwait command\n");
335                 return -EIO;
336         }
337         return 0;
338 }
339
340 int RIOFoadWakeup(struct rio_info *p)
341 {
342         int port;
343         struct Port *PortP;
344         unsigned long flags;
345
346         for (port = 0; port < RIO_PORTS; port++) {
347                 PortP = p->RIOPortp[port];
348
349                 rio_spin_lock_irqsave(&PortP->portSem, flags);
350                 PortP->Config = 0;
351                 PortP->State = 0;
352                 PortP->InUse = NOT_INUSE;
353                 PortP->PortState = 0;
354                 PortP->FlushCmdBodge = 0;
355                 PortP->ModemLines = 0;
356                 PortP->ModemState = 0;
357                 PortP->CookMode = 0;
358                 PortP->ParamSem = 0;
359                 PortP->Mapped = 0;
360                 PortP->WflushFlag = 0;
361                 PortP->MagicFlags = 0;
362                 PortP->RxDataStart = 0;
363                 PortP->TxBufferIn = 0;
364                 PortP->TxBufferOut = 0;
365                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
366         }
367         return (0);
368 }
369
370 /*
371 ** Incoming command on the COMMAND_RUP to be processed.
372 */
373 static int RIOCommandRup(struct rio_info *p, uint Rup, struct Host *HostP, struct PKT __iomem *PacketP)
374 {
375         struct PktCmd __iomem *PktCmdP = (struct PktCmd __iomem *)PacketP->data;
376         struct Port *PortP;
377         struct UnixRup *UnixRupP;
378         unsigned short SysPort;
379         unsigned short ReportedModemStatus;
380         unsigned short rup;
381         unsigned short subCommand;
382         unsigned long flags;
383
384         func_enter();
385
386         /*
387          ** 16 port RTA note:
388          ** Command rup packets coming from the RTA will have pkt->data[1] (which
389          ** translates to PktCmdP->PhbNum) set to the host port number for the
390          ** particular unit. To access the correct BaseSysPort for a 16 port RTA,
391          ** we can use PhbNum to get the rup number for the appropriate 8 port
392          ** block (for the first block, this should be equal to 'Rup').
393          */
394         rup = readb(&PktCmdP->PhbNum) / (unsigned short) PORTS_PER_RTA;
395         UnixRupP = &HostP->UnixRups[rup];
396         SysPort = UnixRupP->BaseSysPort + (readb(&PktCmdP->PhbNum) % (unsigned short) PORTS_PER_RTA);
397         rio_dprintk(RIO_DEBUG_CMD, "Command on rup %d, port %d\n", rup, SysPort);
398
399         if (UnixRupP->BaseSysPort == NO_PORT) {
400                 rio_dprintk(RIO_DEBUG_CMD, "OBSCURE ERROR!\n");
401                 rio_dprintk(RIO_DEBUG_CMD, "Diagnostics follow. Please WRITE THESE DOWN and report them to Specialix Technical Support\n");
402                 rio_dprintk(RIO_DEBUG_CMD, "CONTROL information: Host number %Zd, name ``%s''\n", HostP - p->RIOHosts, HostP->Name);
403                 rio_dprintk(RIO_DEBUG_CMD, "CONTROL information: Rup number  0x%x\n", rup);
404
405                 if (Rup < (unsigned short) MAX_RUP) {
406                         rio_dprintk(RIO_DEBUG_CMD, "CONTROL information: This is the RUP for RTA ``%s''\n", HostP->Mapping[Rup].Name);
407                 } else
408                         rio_dprintk(RIO_DEBUG_CMD, "CONTROL information: This is the RUP for link ``%c'' of host ``%s''\n", ('A' + Rup - MAX_RUP), HostP->Name);
409
410                 rio_dprintk(RIO_DEBUG_CMD, "PACKET information: Destination 0x%x:0x%x\n", readb(&PacketP->dest_unit), readb(&PacketP->dest_port));
411                 rio_dprintk(RIO_DEBUG_CMD, "PACKET information: Source    0x%x:0x%x\n", readb(&PacketP->src_unit), readb(&PacketP->src_port));
412                 rio_dprintk(RIO_DEBUG_CMD, "PACKET information: Length    0x%x (%d)\n", readb(&PacketP->len), readb(&PacketP->len));
413                 rio_dprintk(RIO_DEBUG_CMD, "PACKET information: Control  0x%x (%d)\n", readb(&PacketP->control), readb(&PacketP->control));
414                 rio_dprintk(RIO_DEBUG_CMD, "PACKET information: Check      0x%x (%d)\n", readw(&PacketP->csum), readw(&PacketP->csum));
415                 rio_dprintk(RIO_DEBUG_CMD, "COMMAND information: Host Port Number 0x%x, " "Command Code 0x%x\n", readb(&PktCmdP->PhbNum), readb(&PktCmdP->Command));
416                 return 1;
417         }
418         PortP = p->RIOPortp[SysPort];
419         rio_spin_lock_irqsave(&PortP->portSem, flags);
420         switch (readb(&PktCmdP->Command)) {
421         case BREAK_RECEIVED:
422                 rio_dprintk(RIO_DEBUG_CMD, "Received a break!\n");
423                 /* If the current line disc. is not multi-threading and
424                    the current processor is not the default, reset rup_intr
425                    and return 0 to ensure that the command packet is
426                    not freed. */
427                 /* Call tmgr HANGUP HERE */
428                 /* Fix this later when every thing works !!!! RAMRAJ */
429                 gs_got_break(&PortP->gs);
430                 break;
431
432         case COMPLETE:
433                 rio_dprintk(RIO_DEBUG_CMD, "Command complete on phb %d host %Zd\n", readb(&PktCmdP->PhbNum), HostP - p->RIOHosts);
434                 subCommand = 1;
435                 switch (readb(&PktCmdP->SubCommand)) {
436                 case MEMDUMP:
437                         rio_dprintk(RIO_DEBUG_CMD, "Memory dump cmd (0x%x) from addr 0x%x\n", readb(&PktCmdP->SubCommand), readw(&PktCmdP->SubAddr));
438                         break;
439                 case READ_REGISTER:
440                         rio_dprintk(RIO_DEBUG_CMD, "Read register (0x%x)\n", readw(&PktCmdP->SubAddr));
441                         p->CdRegister = (readb(&PktCmdP->ModemStatus) & MSVR1_HOST);
442                         break;
443                 default:
444                         subCommand = 0;
445                         break;
446                 }
447                 if (subCommand)
448                         break;
449                 rio_dprintk(RIO_DEBUG_CMD, "New status is 0x%x was 0x%x\n", readb(&PktCmdP->PortStatus), PortP->PortState);
450                 if (PortP->PortState != readb(&PktCmdP->PortStatus)) {
451                         rio_dprintk(RIO_DEBUG_CMD, "Mark status & wakeup\n");
452                         PortP->PortState = readb(&PktCmdP->PortStatus);
453                         /* What should we do here ...
454                            wakeup( &PortP->PortState );
455                          */
456                 } else
457                         rio_dprintk(RIO_DEBUG_CMD, "No change\n");
458
459                 /* FALLTHROUGH */
460         case MODEM_STATUS:
461                 /*
462                  ** Knock out the tbusy and tstop bits, as these are not relevant
463                  ** to the check for modem status change (they're just there because
464                  ** it's a convenient place to put them!).
465                  */
466                 ReportedModemStatus = readb(&PktCmdP->ModemStatus);
467                 if ((PortP->ModemState & MSVR1_HOST) == (ReportedModemStatus & MSVR1_HOST)) {
468                         rio_dprintk(RIO_DEBUG_CMD, "Modem status unchanged 0x%x\n", PortP->ModemState);
469                         /*
470                          ** Update ModemState just in case tbusy or tstop states have
471                          ** changed.
472                          */
473                         PortP->ModemState = ReportedModemStatus;
474                 } else {
475                         rio_dprintk(RIO_DEBUG_CMD, "Modem status change from 0x%x to 0x%x\n", PortP->ModemState, ReportedModemStatus);
476                         PortP->ModemState = ReportedModemStatus;
477 #ifdef MODEM_SUPPORT
478                         if (PortP->Mapped) {
479                                 /***********************************************************\
480                                 *************************************************************
481                                 ***                                                                                                        ***
482                                 ***               M O D E M   S T A T E   C H A N G E             ***
483                                 ***                                                                                                        ***
484                                 *************************************************************
485                                 \***********************************************************/
486                                 /*
487                                  ** If the device is a modem, then check the modem
488                                  ** carrier.
489                                  */
490                                 if (PortP->gs.tty == NULL)
491                                         break;
492                                 if (PortP->gs.tty->termios == NULL)
493                                         break;
494
495                                 if (!(PortP->gs.tty->termios->c_cflag & CLOCAL) && ((PortP->State & (RIO_MOPEN | RIO_WOPEN)))) {
496
497                                         rio_dprintk(RIO_DEBUG_CMD, "Is there a Carrier?\n");
498                                         /*
499                                          ** Is there a carrier?
500                                          */
501                                         if (PortP->ModemState & MSVR1_CD) {
502                                                 /*
503                                                  ** Has carrier just appeared?
504                                                  */
505                                                 if (!(PortP->State & RIO_CARR_ON)) {
506                                                         rio_dprintk(RIO_DEBUG_CMD, "Carrier just came up.\n");
507                                                         PortP->State |= RIO_CARR_ON;
508                                                         /*
509                                                          ** wakeup anyone in WOPEN
510                                                          */
511                                                         if (PortP->State & (PORT_ISOPEN | RIO_WOPEN))
512                                                                 wake_up_interruptible(&PortP->gs.open_wait);
513                                                 }
514                                         } else {
515                                                 /*
516                                                  ** Has carrier just dropped?
517                                                  */
518                                                 if (PortP->State & RIO_CARR_ON) {
519                                                         if (PortP->State & (PORT_ISOPEN | RIO_WOPEN | RIO_MOPEN))
520                                                                 tty_hangup(PortP->gs.tty);
521                                                         PortP->State &= ~RIO_CARR_ON;
522                                                         rio_dprintk(RIO_DEBUG_CMD, "Carrirer just went down\n");
523                                                 }
524                                         }
525                                 }
526                         }
527 #endif
528                 }
529                 break;
530
531         default:
532                 rio_dprintk(RIO_DEBUG_CMD, "Unknown command %d on CMD_RUP of host %Zd\n", readb(&PktCmdP->Command), HostP - p->RIOHosts);
533                 break;
534         }
535         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
536
537         func_exit();
538
539         return 1;
540 }
541
542 /*
543 ** The command mechanism:
544 **      Each rup has a chain of commands associated with it.
545 **      This chain is maintained by routines in this file.
546 **      Periodically we are called and we run a quick check of all the
547 **      active chains to determine if there is a command to be executed,
548 **      and if the rup is ready to accept it.
549 **
550 */
551
552 /*
553 ** Allocate an empty command block.
554 */
555 struct CmdBlk *RIOGetCmdBlk(void)
556 {
557         struct CmdBlk *CmdBlkP;
558
559         CmdBlkP = kzalloc(sizeof(struct CmdBlk), GFP_ATOMIC);
560         return CmdBlkP;
561 }
562
563 /*
564 ** Return a block to the head of the free list.
565 */
566 void RIOFreeCmdBlk(struct CmdBlk *CmdBlkP)
567 {
568         kfree(CmdBlkP);
569 }
570
571 /*
572 ** attach a command block to the list of commands to be performed for
573 ** a given rup.
574 */
575 int RIOQueueCmdBlk(struct Host *HostP, uint Rup, struct CmdBlk *CmdBlkP)
576 {
577         struct CmdBlk **Base;
578         struct UnixRup *UnixRupP;
579         unsigned long flags;
580
581         if (Rup >= (unsigned short) (MAX_RUP + LINKS_PER_UNIT)) {
582                 rio_dprintk(RIO_DEBUG_CMD, "Illegal rup number %d in RIOQueueCmdBlk\n", Rup);
583                 RIOFreeCmdBlk(CmdBlkP);
584                 return RIO_FAIL;
585         }
586
587         UnixRupP = &HostP->UnixRups[Rup];
588
589         rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
590
591         /*
592          ** If the RUP is currently inactive, then put the request
593          ** straight on the RUP....
594          */
595         if ((UnixRupP->CmdsWaitingP == NULL) && (UnixRupP->CmdPendingP == NULL) && (readw(&UnixRupP->RupP->txcontrol) == TX_RUP_INACTIVE) && (CmdBlkP->PreFuncP ? (*CmdBlkP->PreFuncP) (CmdBlkP->PreArg, CmdBlkP)
596                                                                                                                                              : 1)) {
597                 rio_dprintk(RIO_DEBUG_CMD, "RUP inactive-placing command straight on. Cmd byte is 0x%x\n", CmdBlkP->Packet.data[0]);
598
599                 /*
600                  ** Whammy! blat that pack!
601                  */
602                 HostP->Copy(&CmdBlkP->Packet, RIO_PTR(HostP->Caddr, readw(&UnixRupP->RupP->txpkt)), sizeof(struct PKT));
603
604                 /*
605                  ** place command packet on the pending position.
606                  */
607                 UnixRupP->CmdPendingP = CmdBlkP;
608
609                 /*
610                  ** set the command register
611                  */
612                 writew(TX_PACKET_READY, &UnixRupP->RupP->txcontrol);
613
614                 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
615
616                 return 0;
617         }
618         rio_dprintk(RIO_DEBUG_CMD, "RUP active - en-queing\n");
619
620         if (UnixRupP->CmdsWaitingP != NULL)
621                 rio_dprintk(RIO_DEBUG_CMD, "Rup active - command waiting\n");
622         if (UnixRupP->CmdPendingP != NULL)
623                 rio_dprintk(RIO_DEBUG_CMD, "Rup active - command pending\n");
624         if (readw(&UnixRupP->RupP->txcontrol) != TX_RUP_INACTIVE)
625                 rio_dprintk(RIO_DEBUG_CMD, "Rup active - command rup not ready\n");
626
627         Base = &UnixRupP->CmdsWaitingP;
628
629         rio_dprintk(RIO_DEBUG_CMD, "First try to queue cmdblk %p at %p\n", CmdBlkP, Base);
630
631         while (*Base) {
632                 rio_dprintk(RIO_DEBUG_CMD, "Command cmdblk %p here\n", *Base);
633                 Base = &((*Base)->NextP);
634                 rio_dprintk(RIO_DEBUG_CMD, "Now try to queue cmd cmdblk %p at %p\n", CmdBlkP, Base);
635         }
636
637         rio_dprintk(RIO_DEBUG_CMD, "Will queue cmdblk %p at %p\n", CmdBlkP, Base);
638
639         *Base = CmdBlkP;
640
641         CmdBlkP->NextP = NULL;
642
643         rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
644
645         return 0;
646 }
647
648 /*
649 ** Here we go - if there is an empty rup, fill it!
650 ** must be called at splrio() or higher.
651 */
652 void RIOPollHostCommands(struct rio_info *p, struct Host *HostP)
653 {
654         struct CmdBlk *CmdBlkP;
655         struct UnixRup *UnixRupP;
656         struct PKT __iomem *PacketP;
657         unsigned short Rup;
658         unsigned long flags;
659
660
661         Rup = MAX_RUP + LINKS_PER_UNIT;
662
663         do {                    /* do this loop for each RUP */
664                 /*
665                  ** locate the rup we are processing & lock it
666                  */
667                 UnixRupP = &HostP->UnixRups[--Rup];
668
669                 spin_lock_irqsave(&UnixRupP->RupLock, flags);
670
671                 /*
672                  ** First check for incoming commands:
673                  */
674                 if (readw(&UnixRupP->RupP->rxcontrol) != RX_RUP_INACTIVE) {
675                         int FreeMe;
676
677                         PacketP = (struct PKT __iomem *) RIO_PTR(HostP->Caddr, readw(&UnixRupP->RupP->rxpkt));
678
679                         switch (readb(&PacketP->dest_port)) {
680                         case BOOT_RUP:
681                                 rio_dprintk(RIO_DEBUG_CMD, "Incoming Boot %s packet '%x'\n", readb(&PacketP->len) & 0x80 ? "Command" : "Data", readb(&PacketP->data[0]));
682                                 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
683                                 FreeMe = RIOBootRup(p, Rup, HostP, PacketP);
684                                 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
685                                 break;
686
687                         case COMMAND_RUP:
688                                 /*
689                                  ** Free the RUP lock as loss of carrier causes a
690                                  ** ttyflush which will (eventually) call another
691                                  ** routine that uses the RUP lock.
692                                  */
693                                 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
694                                 FreeMe = RIOCommandRup(p, Rup, HostP, PacketP);
695                                 if (readb(&PacketP->data[5]) == MEMDUMP) {
696                                         rio_dprintk(RIO_DEBUG_CMD, "Memdump from 0x%x complete\n", readw(&(PacketP->data[6])));
697                                         rio_memcpy_fromio(p->RIOMemDump, &(PacketP->data[8]), 32);
698                                 }
699                                 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
700                                 break;
701
702                         case ROUTE_RUP:
703                                 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
704                                 FreeMe = RIORouteRup(p, Rup, HostP, PacketP);
705                                 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
706                                 break;
707
708                         default:
709                                 rio_dprintk(RIO_DEBUG_CMD, "Unknown RUP %d\n", readb(&PacketP->dest_port));
710                                 FreeMe = 1;
711                                 break;
712                         }
713
714                         if (FreeMe) {
715                                 rio_dprintk(RIO_DEBUG_CMD, "Free processed incoming command packet\n");
716                                 put_free_end(HostP, PacketP);
717
718                                 writew(RX_RUP_INACTIVE, &UnixRupP->RupP->rxcontrol);
719
720                                 if (readw(&UnixRupP->RupP->handshake) == PHB_HANDSHAKE_SET) {
721                                         rio_dprintk(RIO_DEBUG_CMD, "Handshake rup %d\n", Rup);
722                                         writew(PHB_HANDSHAKE_SET | PHB_HANDSHAKE_RESET, &UnixRupP->RupP->handshake);
723                                 }
724                         }
725                 }
726
727                 /*
728                  ** IF a command was running on the port,
729                  ** and it has completed, then tidy it up.
730                  */
731                 if ((CmdBlkP = UnixRupP->CmdPendingP) &&        /* ASSIGN! */
732                     (readw(&UnixRupP->RupP->txcontrol) == TX_RUP_INACTIVE)) {
733                         /*
734                          ** we are idle.
735                          ** there is a command in pending.
736                          ** Therefore, this command has finished.
737                          ** So, wakeup whoever is waiting for it (and tell them
738                          ** what happened).
739                          */
740                         if (CmdBlkP->Packet.dest_port == BOOT_RUP)
741                                 rio_dprintk(RIO_DEBUG_CMD, "Free Boot %s Command Block '%x'\n", CmdBlkP->Packet.len & 0x80 ? "Command" : "Data", CmdBlkP->Packet.data[0]);
742
743                         rio_dprintk(RIO_DEBUG_CMD, "Command %p completed\n", CmdBlkP);
744
745                         /*
746                          ** Clear the Rup lock to prevent mutual exclusion.
747                          */
748                         if (CmdBlkP->PostFuncP) {
749                                 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
750                                 (*CmdBlkP->PostFuncP) (CmdBlkP->PostArg, CmdBlkP);
751                                 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
752                         }
753
754                         /*
755                          ** ....clear the pending flag....
756                          */
757                         UnixRupP->CmdPendingP = NULL;
758
759                         /*
760                          ** ....and return the command block to the freelist.
761                          */
762                         RIOFreeCmdBlk(CmdBlkP);
763                 }
764
765                 /*
766                  ** If there is a command for this rup, and the rup
767                  ** is idle, then process the command
768                  */
769                 if ((CmdBlkP = UnixRupP->CmdsWaitingP) &&       /* ASSIGN! */
770                     (UnixRupP->CmdPendingP == NULL) && (readw(&UnixRupP->RupP->txcontrol) == TX_RUP_INACTIVE)) {
771                         /*
772                          ** if the pre-function is non-zero, call it.
773                          ** If it returns RIO_FAIL then don't
774                          ** send this command yet!
775                          */
776                         if (!(CmdBlkP->PreFuncP ? (*CmdBlkP->PreFuncP) (CmdBlkP->PreArg, CmdBlkP) : 1)) {
777                                 rio_dprintk(RIO_DEBUG_CMD, "Not ready to start command %p\n", CmdBlkP);
778                         } else {
779                                 rio_dprintk(RIO_DEBUG_CMD, "Start new command %p Cmd byte is 0x%x\n", CmdBlkP, CmdBlkP->Packet.data[0]);
780                                 /*
781                                  ** Whammy! blat that pack!
782                                  */
783                                 HostP->Copy(&CmdBlkP->Packet, RIO_PTR(HostP->Caddr, readw(&UnixRupP->RupP->txpkt)), sizeof(struct PKT));
784
785                                 /*
786                                  ** remove the command from the rup command queue...
787                                  */
788                                 UnixRupP->CmdsWaitingP = CmdBlkP->NextP;
789
790                                 /*
791                                  ** ...and place it on the pending position.
792                                  */
793                                 UnixRupP->CmdPendingP = CmdBlkP;
794
795                                 /*
796                                  ** set the command register
797                                  */
798                                 writew(TX_PACKET_READY, &UnixRupP->RupP->txcontrol);
799
800                                 /*
801                                  ** the command block will be freed
802                                  ** when the command has been processed.
803                                  */
804                         }
805                 }
806                 spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
807         } while (Rup);
808 }
809
810 int RIOWFlushMark(unsigned long iPortP, struct CmdBlk *CmdBlkP)
811 {
812         struct Port *PortP = (struct Port *) iPortP;
813         unsigned long flags;
814
815         rio_spin_lock_irqsave(&PortP->portSem, flags);
816         PortP->WflushFlag++;
817         PortP->MagicFlags |= MAGIC_FLUSH;
818         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
819         return RIOUnUse(iPortP, CmdBlkP);
820 }
821
822 int RIORFlushEnable(unsigned long iPortP, struct CmdBlk *CmdBlkP)
823 {
824         struct Port *PortP = (struct Port *) iPortP;
825         struct PKT __iomem *PacketP;
826         unsigned long flags;
827
828         rio_spin_lock_irqsave(&PortP->portSem, flags);
829
830         while (can_remove_receive(&PacketP, PortP)) {
831                 remove_receive(PortP);
832                 put_free_end(PortP->HostP, PacketP);
833         }
834
835         if (readw(&PortP->PhbP->handshake) == PHB_HANDSHAKE_SET) {
836                 /*
837                  ** MAGIC! (Basically, handshake the RX buffer, so that
838                  ** the RTAs upstream can be re-enabled.)
839                  */
840                 rio_dprintk(RIO_DEBUG_CMD, "Util: Set RX handshake bit\n");
841                 writew(PHB_HANDSHAKE_SET | PHB_HANDSHAKE_RESET, &PortP->PhbP->handshake);
842         }
843         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
844         return RIOUnUse(iPortP, CmdBlkP);
845 }
846
847 int RIOUnUse(unsigned long iPortP, struct CmdBlk *CmdBlkP)
848 {
849         struct Port *PortP = (struct Port *) iPortP;
850         unsigned long flags;
851
852         rio_spin_lock_irqsave(&PortP->portSem, flags);
853
854         rio_dprintk(RIO_DEBUG_CMD, "Decrement in use count for port\n");
855
856         if (PortP->InUse) {
857                 if (--PortP->InUse != NOT_INUSE) {
858                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
859                         return 0;
860                 }
861         }
862         /*
863          ** While PortP->InUse is set (i.e. a preemptive command has been sent to
864          ** the RTA and is awaiting completion), any transmit data is prevented from
865          ** being transferred from the write queue into the transmit packets
866          ** (add_transmit) and no furthur transmit interrupt will be sent for that
867          ** data. The next interrupt will occur up to 500ms later (RIOIntr is called
868          ** twice a second as a saftey measure). This was the case when kermit was
869          ** used to send data into a RIO port. After each packet was sent, TCFLSH
870          ** was called to flush the read queue preemptively. PortP->InUse was
871          ** incremented, thereby blocking the 6 byte acknowledgement packet
872          ** transmitted back. This acknowledgment hung around for 500ms before
873          ** being sent, thus reducing input performance substantially!.
874          ** When PortP->InUse becomes NOT_INUSE, we must ensure that any data
875          ** hanging around in the transmit buffer is sent immediately.
876          */
877         writew(1, &PortP->HostP->ParmMapP->tx_intr);
878         /* What to do here ..
879            wakeup( (caddr_t)&(PortP->InUse) );
880          */
881         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
882         return 0;
883 }
884
885 /*
886 ** 
887 ** How to use this file:
888 ** 
889 ** To send a command down a rup, you need to allocate a command block, fill
890 ** in the packet information, fill in the command number, fill in the pre-
891 ** and post- functions and arguments, and then add the command block to the
892 ** queue of command blocks for the port in question. When the port is idle,
893 ** then the pre-function will be called. If this returns RIO_FAIL then the
894 ** command will be re-queued and tried again at a later date (probably in one
895 ** clock tick). If the pre-function returns NOT RIO_FAIL, then the command
896 ** packet will be queued on the RUP, and the txcontrol field set to the
897 ** command number. When the txcontrol field has changed from being the
898 ** command number, then the post-function will be called, with the argument
899 ** specified earlier, a pointer to the command block, and the value of
900 ** txcontrol.
901 ** 
902 ** To allocate a command block, call RIOGetCmdBlk(). This returns a pointer
903 ** to the command block structure allocated, or NULL if there aren't any.
904 ** The block will have been zeroed for you.
905 ** 
906 ** The structure has the following fields:
907 ** 
908 ** struct CmdBlk
909 ** {
910 **       struct CmdBlk *NextP;            ** Pointer to next command block   **
911 **       struct PKT      Packet;                ** A packet, to copy to the rup **
912 **                      int      (*PreFuncP)();  ** The func to call to check if OK **
913 **                      int      PreArg;                ** The arg for the func                 **
914 **                      int      (*PostFuncP)(); ** The func to call when completed **
915 **                      int      PostArg;          ** The arg for the func                      **
916 ** };
917 ** 
918 ** You need to fill in ALL fields EXCEPT NextP, which is used to link the
919 ** blocks together either on the free list or on the Rup list.
920 ** 
921 ** Packet is an actual packet structure to be filled in with the packet
922 ** information associated with the command. You need to fill in everything,
923 ** as the command processor doesn't process the command packet in any way.
924 ** 
925 ** The PreFuncP is called before the packet is enqueued on the host rup.
926 ** PreFuncP is called as (*PreFuncP)(PreArg, CmdBlkP);. PreFuncP must
927 ** return !RIO_FAIL to have the packet queued on the rup, and RIO_FAIL
928 ** if the packet is NOT to be queued.
929 ** 
930 ** The PostFuncP is called when the command has completed. It is called
931 ** as (*PostFuncP)(PostArg, CmdBlkP, txcontrol);. PostFuncP is not expected
932 ** to return a value. PostFuncP does NOT need to free the command block,
933 ** as this happens automatically after PostFuncP returns.
934 ** 
935 ** Once the command block has been filled in, it is attached to the correct
936 ** queue by calling RIOQueueCmdBlk( HostP, Rup, CmdBlkP ) where HostP is
937 ** a pointer to the struct Host, Rup is the NUMBER of the rup (NOT a pointer
938 ** to it!), and CmdBlkP is the pointer to the command block allocated using
939 ** RIOGetCmdBlk().
940 ** 
941 */