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