1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#define __EXPLICIT_DEF_H__
34
35#include <linux/module.h>
36#include <linux/sched.h>
37#include <linux/slab.h>
38#include <linux/errno.h>
39#include <linux/tty.h>
40#include <linux/string.h>
41#include <asm/io.h>
42#include <asm/system.h>
43#include <asm/string.h>
44#include <asm/uaccess.h>
45
46#include <linux/termios.h>
47
48#include <linux/serial.h>
49
50#include <linux/generic_serial.h>
51
52
53#include "linux_compat.h"
54#include "rio_linux.h"
55#include "pkt.h"
56#include "daemon.h"
57#include "rio.h"
58#include "riospace.h"
59#include "cmdpkt.h"
60#include "map.h"
61#include "rup.h"
62#include "port.h"
63#include "riodrvr.h"
64#include "rioinfo.h"
65#include "func.h"
66#include "errors.h"
67#include "pci.h"
68
69#include "parmmap.h"
70#include "unixrup.h"
71#include "board.h"
72#include "host.h"
73#include "phb.h"
74#include "link.h"
75#include "cmdblk.h"
76#include "route.h"
77#include "cirrus.h"
78#include "rioioctl.h"
79#include "param.h"
80
81static void RIOClearUp(struct Port *PortP);
82
83
84int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
85
86
87extern struct rio_info *p;
88
89
90int riotopen(struct tty_struct *tty, struct file *filp)
91{
92 unsigned int SysPort;
93 int repeat_this = 250;
94 struct Port *PortP;
95 unsigned long flags;
96 int retval = 0;
97
98 func_enter();
99
100
101
102
103 tty->driver_data = NULL;
104
105 SysPort = rio_minor(tty);
106
107 if (p->RIOFailed) {
108 rio_dprintk(RIO_DEBUG_TTY, "System initialisation failed\n");
109 func_exit();
110 return -ENXIO;
111 }
112
113 rio_dprintk(RIO_DEBUG_TTY, "port open SysPort %d (mapped:%d)\n", SysPort, p->RIOPortp[SysPort]->Mapped);
114
115
116
117
118
119
120
121 if (SysPort >= RIO_PORTS) {
122 rio_dprintk(RIO_DEBUG_TTY, "Illegal port number %d\n", SysPort);
123 func_exit();
124 return -ENXIO;
125 }
126
127
128
129
130 PortP = p->RIOPortp[SysPort];
131 rio_dprintk(RIO_DEBUG_TTY, "PortP: %p\n", PortP);
132 if (!PortP->Mapped) {
133
134
135
136
137 rio_dprintk(RIO_DEBUG_TTY, "port not mapped into system\n");
138 func_exit();
139 return -ENXIO;
140 }
141
142 tty->driver_data = PortP;
143
144 PortP->gs.port.tty = tty;
145 PortP->gs.port.count++;
146
147 rio_dprintk(RIO_DEBUG_TTY, "%d bytes in tx buffer\n", PortP->gs.xmit_cnt);
148
149 retval = gs_init_port(&PortP->gs);
150 if (retval) {
151 PortP->gs.port.count--;
152 return -ENXIO;
153 }
154
155
156
157
158 if ((PortP->HostP->Flags & RUN_STATE) != RC_RUNNING) {
159 rio_dprintk(RIO_DEBUG_TTY, "Host not running\n");
160 func_exit();
161 return -ENXIO;
162 }
163
164
165
166
167
168
169
170
171
172
173 rio_dprintk(RIO_DEBUG_TTY, "Checking if RTA has booted... \n");
174 while (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
175 if (!PortP->WaitUntilBooted) {
176 rio_dprintk(RIO_DEBUG_TTY, "RTA never booted\n");
177 func_exit();
178 return -ENXIO;
179 }
180
181
182
183
184
185 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
186 rio_dprintk(RIO_DEBUG_TTY, "RTA_wait_for_boot: EINTR in delay \n");
187 func_exit();
188 return -EINTR;
189 }
190 if (repeat_this-- <= 0) {
191 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
192 func_exit();
193 return -EIO;
194 }
195 }
196 rio_dprintk(RIO_DEBUG_TTY, "RTA has been booted\n");
197 rio_spin_lock_irqsave(&PortP->portSem, flags);
198 if (p->RIOHalted) {
199 goto bombout;
200 }
201
202
203
204
205
206
207 while ((PortP->State & RIO_CLOSING) && !p->RIOHalted) {
208 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RIO_CLOSING to go away\n");
209 if (repeat_this-- <= 0) {
210 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
211 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
212 retval = -EINTR;
213 goto bombout;
214 }
215 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
216 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
217 rio_spin_lock_irqsave(&PortP->portSem, flags);
218 retval = -EINTR;
219 goto bombout;
220 }
221 rio_spin_lock_irqsave(&PortP->portSem, flags);
222 }
223
224 if (!PortP->Mapped) {
225 rio_dprintk(RIO_DEBUG_TTY, "Port unmapped while closing!\n");
226 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
227 retval = -ENXIO;
228 func_exit();
229 return retval;
230 }
231
232 if (p->RIOHalted) {
233 goto bombout;
234 }
235
236
237
238
239
240
241
242
243 if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
244 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
245 }
246
247 if (!(PortP->firstOpen)) {
248 rio_dprintk(RIO_DEBUG_TTY, "First open for this port\n");
249
250
251 PortP->firstOpen++;
252 PortP->CookMode = 0;
253 PortP->InUse = NOT_INUSE;
254
255
256
257
258 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
259
260
261
262
263
264 RIOParam(PortP, RIOC_OPEN, 1, OK_TO_SLEEP);
265 rio_spin_lock_irqsave(&PortP->portSem, flags);
266
267
268
269
270 while (!(PortP->PortState & PORT_ISOPEN) && !p->RIOHalted) {
271 rio_dprintk(RIO_DEBUG_TTY, "Waiting for PORT_ISOPEN-currently %x\n", PortP->PortState);
272 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
273 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
274 rio_dprintk(RIO_DEBUG_TTY, "Waiting for open to finish broken by signal\n");
275 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
276 func_exit();
277 return -EINTR;
278 }
279 rio_spin_lock_irqsave(&PortP->portSem, flags);
280 }
281
282 if (p->RIOHalted) {
283 retval = -EIO;
284 bombout:
285
286 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
287 return retval;
288 }
289 rio_dprintk(RIO_DEBUG_TTY, "PORT_ISOPEN found\n");
290 }
291 rio_dprintk(RIO_DEBUG_TTY, "Modem - test for carrier\n");
292
293
294
295
296
297 if ((PortP->gs.port.tty->termios->c_cflag & CLOCAL) ||
298 (PortP->ModemState & RIOC_MSVR1_CD)) {
299 rio_dprintk(RIO_DEBUG_TTY, "open(%d) Modem carr on\n", SysPort);
300
301
302
303
304 PortP->State |= RIO_CARR_ON;
305 wake_up_interruptible(&PortP->gs.port.open_wait);
306 } else {
307
308
309
310
311 while (!(PortP->State & RIO_CARR_ON) && !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted) {
312 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr on\n", SysPort);
313
314
315
316 PortP->State |= RIO_WOPEN;
317 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
318 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
319 rio_spin_lock_irqsave(&PortP->portSem, flags);
320
321
322
323
324
325 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr broken by signal\n", SysPort);
326 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
327
328
329
330 PortP->State &= ~RIO_WOPEN;
331 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
332 func_exit();
333 return -EINTR;
334 }
335 rio_spin_lock_irqsave(&PortP->portSem, flags);
336 }
337 PortP->State &= ~RIO_WOPEN;
338 }
339 if (p->RIOHalted)
340 goto bombout;
341 rio_dprintk(RIO_DEBUG_TTY, "Setting RIO_MOPEN\n");
342 PortP->State |= RIO_MOPEN;
343
344 if (p->RIOHalted)
345 goto bombout;
346
347 rio_dprintk(RIO_DEBUG_TTY, "high level open done\n");
348
349
350
351
352 if (PortP->statsGather)
353 PortP->opens++;
354
355 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
356 rio_dprintk(RIO_DEBUG_TTY, "Returning from open\n");
357 func_exit();
358 return 0;
359}
360
361
362
363
364
365
366
367int riotclose(void *ptr)
368{
369 struct Port *PortP = ptr;
370 int deleted = 0;
371 int try = -1;
372 int repeat_this = -1;
373
374 unsigned long end_time;
375 struct tty_struct *tty;
376 unsigned long flags;
377 int rv = 0;
378
379 rio_dprintk(RIO_DEBUG_TTY, "port close SysPort %d\n", PortP->PortNum);
380
381
382 rio_dprintk(RIO_DEBUG_TTY, "Port is at address %p\n", PortP);
383
384 tty = PortP->gs.port.tty;
385 rio_dprintk(RIO_DEBUG_TTY, "TTY is at address %p\n", tty);
386
387 if (PortP->gs.closing_wait)
388 end_time = jiffies + PortP->gs.closing_wait;
389 else
390 end_time = jiffies + MAX_SCHEDULE_TIMEOUT;
391
392 rio_spin_lock_irqsave(&PortP->portSem, flags);
393
394
395
396
397
398 PortP->State |= RIO_CLOSING;
399
400 if ((PortP->State & RIO_DELETED)) {
401 rio_dprintk(RIO_DEBUG_TTY, "Close on deleted RTA\n");
402 deleted = 1;
403 }
404
405 if (p->RIOHalted) {
406 RIOClearUp(PortP);
407 rv = -EIO;
408 goto close_end;
409 }
410
411 rio_dprintk(RIO_DEBUG_TTY, "Clear bits\n");
412
413
414
415 PortP->State &= ~RIO_MOPEN;
416 PortP->State &= ~RIO_CARR_ON;
417 PortP->ModemState &= ~RIOC_MSVR1_CD;
418
419
420
421
422
423
424
425 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
426
427
428
429
430 rio_dprintk(RIO_DEBUG_TTY, "Channel %d still open !\n", PortP->PortNum);
431 PortP->State &= ~RIO_CLOSING;
432 if (PortP->firstOpen)
433 PortP->firstOpen--;
434 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
435 return -EIO;
436 }
437
438 rio_dprintk(RIO_DEBUG_TTY, "Closing down - everything must go!\n");
439
440 PortP->State &= ~RIO_DYNOROD;
441
442
443
444
445
446
447 rio_dprintk(RIO_DEBUG_TTY, "Timeout 1 starts\n");
448
449 if (!deleted)
450 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted && (PortP->TxBufferIn != PortP->TxBufferOut)) {
451 if (repeat_this-- <= 0) {
452 rv = -EINTR;
453 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
454 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
455 goto close_end;
456 }
457 rio_dprintk(RIO_DEBUG_TTY, "Calling timeout to flush in closing\n");
458 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
459 if (RIODelay_ni(PortP, HUNDRED_MS * 10) == RIO_FAIL) {
460 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
461 rv = -EINTR;
462 rio_spin_lock_irqsave(&PortP->portSem, flags);
463 goto close_end;
464 }
465 rio_spin_lock_irqsave(&PortP->portSem, flags);
466 }
467
468 PortP->TxBufferIn = PortP->TxBufferOut = 0;
469 repeat_this = 0xff;
470
471 PortP->InUse = 0;
472 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
473
474
475
476
477 rio_dprintk(RIO_DEBUG_TTY, "Channel %d re-open!\n", PortP->PortNum);
478 PortP->State &= ~RIO_CLOSING;
479 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
480 if (PortP->firstOpen)
481 PortP->firstOpen--;
482 return -EIO;
483 }
484
485 if (p->RIOHalted) {
486 RIOClearUp(PortP);
487 goto close_end;
488 }
489
490
491 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
492
493 if (RIOShortCommand(p, PortP, RIOC_CLOSE, 1, 0) == RIO_FAIL) {
494 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
495 rio_spin_lock_irqsave(&PortP->portSem, flags);
496 goto close_end;
497 }
498
499 if (!deleted)
500 while (try && (PortP->PortState & PORT_ISOPEN)) {
501 try--;
502 if (time_after(jiffies, end_time)) {
503 rio_dprintk(RIO_DEBUG_TTY, "Run out of tries - force the bugger shut!\n");
504 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
505 break;
506 }
507 rio_dprintk(RIO_DEBUG_TTY, "Close: PortState:ISOPEN is %d\n", PortP->PortState & PORT_ISOPEN);
508
509 if (p->RIOHalted) {
510 RIOClearUp(PortP);
511 rio_spin_lock_irqsave(&PortP->portSem, flags);
512 goto close_end;
513 }
514 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
515 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
516 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
517 break;
518 }
519 }
520 rio_spin_lock_irqsave(&PortP->portSem, flags);
521 rio_dprintk(RIO_DEBUG_TTY, "Close: try was %d on completion\n", try);
522
523
524
525
526
527
528
529 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
530
531
532
533
534 if (PortP->statsGather)
535 PortP->closes++;
536
537close_end:
538
539
540
541
542
543 PortP->State &= ~(RIO_CLOSING | RIO_DELETED);
544 if (PortP->firstOpen)
545 PortP->firstOpen--;
546 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
547 rio_dprintk(RIO_DEBUG_TTY, "Return from close\n");
548 return rv;
549}
550
551
552
553static void RIOClearUp(struct Port *PortP)
554{
555 rio_dprintk(RIO_DEBUG_TTY, "RIOHalted set\n");
556 PortP->Config = 0;
557 PortP->PortState = 0;
558 PortP->firstOpen = 0;
559 PortP->FlushCmdBodge = 0;
560 PortP->ModemState = PortP->CookMode = 0;
561 PortP->Mapped = 0;
562 PortP->WflushFlag = 0;
563 PortP->MagicFlags = 0;
564 PortP->RxDataStart = 0;
565 PortP->TxBufferIn = 0;
566 PortP->TxBufferOut = 0;
567}
568
569
570
571
572
573
574
575
576
577
578int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg)
579{
580 struct PKT __iomem *PacketP;
581 int retries = 20;
582 unsigned long flags;
583
584 rio_dprintk(RIO_DEBUG_TTY, "entering shortcommand.\n");
585
586 if (PortP->State & RIO_DELETED) {
587 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
588 return RIO_FAIL;
589 }
590 rio_spin_lock_irqsave(&PortP->portSem, flags);
591
592
593
594
595
596 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted) {
597 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not in use (%d)\n", retries);
598 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
599 if (retries-- <= 0) {
600 return RIO_FAIL;
601 }
602 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
603 return RIO_FAIL;
604 }
605 rio_spin_lock_irqsave(&PortP->portSem, flags);
606 }
607 if (PortP->State & RIO_DELETED) {
608 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
609 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
610 return RIO_FAIL;
611 }
612
613 while (!can_add_transmit(&PacketP, PortP) && !p->RIOHalted) {
614 rio_dprintk(RIO_DEBUG_TTY, "Waiting to add short command to queue (%d)\n", retries);
615 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
616 if (retries-- <= 0) {
617 rio_dprintk(RIO_DEBUG_TTY, "out of tries. Failing\n");
618 return RIO_FAIL;
619 }
620 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
621 return RIO_FAIL;
622 }
623 rio_spin_lock_irqsave(&PortP->portSem, flags);
624 }
625
626 if (p->RIOHalted) {
627 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
628 return RIO_FAIL;
629 }
630
631
632
633
634 writeb(command, &PacketP->data[0]);
635
636 if (len == 2)
637 writeb(arg, &PacketP->data[1]);
638
639
640
641
642 writeb(PKT_CMD_BIT | len, &PacketP->len);
643
644 add_transmit(PortP);
645
646
647
648 if (PortP->statsGather)
649 PortP->txchars += len;
650
651 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
652 return p->RIOHalted ? RIO_FAIL : ~RIO_FAIL;
653}
654
655
656