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