blob: 937fd21203816b7521aa92b660672b424e8f6dc3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCBIT-D low-layer interface
3 *
4 * Copyright (C) 1996 Universidade de Lisboa
5 *
6 * Written by Pedro Roque Marques (roque@di.fc.ul.pt)
7 *
8 * This software may be used and distributed according to the terms of
9 * the GNU General Public License, incorporated herein by reference.
10 */
11
12/*
13 * 19991203 - Fernando Carvalho - takion@superbofh.org
14 * Hacked to compile with egcs and run with current version of isdn modules
15*/
16
17/*
18 * Based on documentation provided by Inesc:
19 * - "Interface com bus do PC para o PCBIT e PCBIT-D", Inesc, Jan 93
20 */
21
22/*
23 * TODO: better handling of errors
24 * re-write/remove debug printks
25 */
26
27#include <linux/sched.h>
28#include <linux/string.h>
29#include <linux/kernel.h>
30#include <linux/types.h>
31#include <linux/slab.h>
32#include <linux/interrupt.h>
33#include <linux/workqueue.h>
34#include <linux/mm.h>
35#include <linux/skbuff.h>
36
37#include <linux/isdnif.h>
38
39#include <asm/system.h>
40#include <asm/io.h>
41
42
43#include "pcbit.h"
44#include "layer2.h"
45#include "edss1.h"
46
47#undef DEBUG_FRAG
48
49
50
51/*
52 * task queue struct
53 */
54
55
56
57/*
58 * Layer 3 packet demultiplexer
59 * drv.c
60 */
61
62extern void pcbit_l3_receive(struct pcbit_dev *dev, ulong msg,
63 struct sk_buff *skb,
64 ushort hdr_len, ushort refnum);
65
66/*
67 * Prototypes
68 */
69
70void pcbit_deliver(void *data);
71static void pcbit_transmit(struct pcbit_dev *dev);
72
73static void pcbit_recv_ack(struct pcbit_dev *dev, unsigned char ack);
74
75static void pcbit_l2_error(struct pcbit_dev *dev);
76static void pcbit_l2_active_conf(struct pcbit_dev *dev, u_char info);
77static void pcbit_l2_err_recover(unsigned long data);
78
79static void pcbit_firmware_bug(struct pcbit_dev *dev);
80
81static __inline__ void
82pcbit_sched_delivery(struct pcbit_dev *dev)
83{
84 schedule_work(&dev->qdelivery);
85}
86
87
88/*
89 * Called from layer3
90 */
91
92int
93pcbit_l2_write(struct pcbit_dev *dev, ulong msg, ushort refnum,
94 struct sk_buff *skb, unsigned short hdr_len)
95{
96 struct frame_buf *frame,
97 *ptr;
98 unsigned long flags;
99
100 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING) {
101 dev_kfree_skb(skb);
102 return -1;
103 }
104 if ((frame = (struct frame_buf *) kmalloc(sizeof(struct frame_buf),
105 GFP_ATOMIC)) == NULL) {
106 printk(KERN_WARNING "pcbit_2_write: kmalloc failed\n");
107 dev_kfree_skb(skb);
108 return -1;
109 }
110 frame->msg = msg;
111 frame->refnum = refnum;
112 frame->copied = 0;
113 frame->hdr_len = hdr_len;
114
115 if (skb)
116 frame->dt_len = skb->len - hdr_len;
117 else
118 frame->dt_len = 0;
119
120 frame->skb = skb;
121
122 frame->next = NULL;
123
124 spin_lock_irqsave(&dev->lock, flags);
125
126 if (dev->write_queue == NULL) {
127 dev->write_queue = frame;
128 spin_unlock_irqrestore(&dev->lock, flags);
129 pcbit_transmit(dev);
130 } else {
131 for (ptr = dev->write_queue; ptr->next; ptr = ptr->next);
132 ptr->next = frame;
133
134 spin_unlock_irqrestore(&dev->lock, flags);
135 }
136 return 0;
137}
138
139static __inline__ void
140pcbit_tx_update(struct pcbit_dev *dev, ushort len)
141{
142 u_char info;
143
144 dev->send_seq = (dev->send_seq + 1) % 8;
145
146 dev->fsize[dev->send_seq] = len;
147 info = 0;
148 info |= dev->rcv_seq << 3;
149 info |= dev->send_seq;
150
151 writeb(info, dev->sh_mem + BANK4);
152
153}
154
155/*
156 * called by interrupt service routine or by write_2
157 */
158
159static void
160pcbit_transmit(struct pcbit_dev *dev)
161{
162 struct frame_buf *frame = NULL;
163 unsigned char unacked;
164 int flen; /* fragment frame length including all headers */
165 int free;
166 int count,
167 cp_len;
168 unsigned long flags;
169 unsigned short tt;
170
171 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING)
172 return;
173
174 unacked = (dev->send_seq + (8 - dev->unack_seq)) & 0x07;
175
176 spin_lock_irqsave(&dev->lock, flags);
177
178 if (dev->free > 16 && dev->write_queue && unacked < 7) {
179
180 if (!dev->w_busy)
181 dev->w_busy = 1;
182 else {
183 spin_unlock_irqrestore(&dev->lock, flags);
184 return;
185 }
186
187
188 frame = dev->write_queue;
189 free = dev->free;
190
191 spin_unlock_irqrestore(&dev->lock, flags);
192
193 if (frame->copied == 0) {
194
195 /* Type 0 frame */
196
197 ulong msg;
198
199 if (frame->skb)
200 flen = FRAME_HDR_LEN + PREHDR_LEN + frame->skb->len;
201 else
202 flen = FRAME_HDR_LEN + PREHDR_LEN;
203
204 if (flen > free)
205 flen = free;
206
207 msg = frame->msg;
208
209 /*
210 * Board level 2 header
211 */
212
213 pcbit_writew(dev, flen - FRAME_HDR_LEN);
214
215 pcbit_writeb(dev, GET_MSG_CPU(msg));
216
217 pcbit_writeb(dev, GET_MSG_PROC(msg));
218
219 /* TH */
220 pcbit_writew(dev, frame->hdr_len + PREHDR_LEN);
221
222 /* TD */
223 pcbit_writew(dev, frame->dt_len);
224
225
226 /*
227 * Board level 3 fixed-header
228 */
229
230 /* LEN = TH */
231 pcbit_writew(dev, frame->hdr_len + PREHDR_LEN);
232
233 /* XX */
234 pcbit_writew(dev, 0);
235
236 /* C + S */
237 pcbit_writeb(dev, GET_MSG_CMD(msg));
238 pcbit_writeb(dev, GET_MSG_SCMD(msg));
239
240 /* NUM */
241 pcbit_writew(dev, frame->refnum);
242
243 count = FRAME_HDR_LEN + PREHDR_LEN;
244 } else {
245 /* Type 1 frame */
246
247 flen = 2 + (frame->skb->len - frame->copied);
248
249 if (flen > free)
250 flen = free;
251
252 /* TT */
253 tt = ((ushort) (flen - 2)) | 0x8000U; /* Type 1 */
254 pcbit_writew(dev, tt);
255
256 count = 2;
257 }
258
259 if (frame->skb) {
260 cp_len = frame->skb->len - frame->copied;
261 if (cp_len > flen - count)
262 cp_len = flen - count;
263
264 memcpy_topcbit(dev, frame->skb->data + frame->copied,
265 cp_len);
266 frame->copied += cp_len;
267 }
268 /* bookkeeping */
269 dev->free -= flen;
270 pcbit_tx_update(dev, flen);
271
272 spin_lock_irqsave(&dev->lock, flags);
273
274 if (frame->skb == NULL || frame->copied == frame->skb->len) {
275
276 dev->write_queue = frame->next;
277
278 if (frame->skb != NULL) {
279 /* free frame */
280 dev_kfree_skb(frame->skb);
281 }
282 kfree(frame);
283 }
284 dev->w_busy = 0;
285 spin_unlock_irqrestore(&dev->lock, flags);
286 } else {
287 spin_unlock_irqrestore(&dev->lock, flags);
288#ifdef DEBUG
289 printk(KERN_DEBUG "unacked %d free %d write_queue %s\n",
290 unacked, dev->free, dev->write_queue ? "not empty" :
291 "empty");
292#endif
293 }
294}
295
296
297/*
298 * deliver a queued frame to the upper layer
299 */
300
301void
302pcbit_deliver(void *data)
303{
304 struct frame_buf *frame;
305 unsigned long flags, msg;
306 struct pcbit_dev *dev = (struct pcbit_dev *) data;
307
308 spin_lock_irqsave(&dev->lock, flags);
309
310 while ((frame = dev->read_queue)) {
311 dev->read_queue = frame->next;
312 spin_unlock_irqrestore(&dev->lock, flags);
313
Jeff Garzik76fd0202006-10-11 01:22:25 -0700314 msg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 SET_MSG_CPU(msg, 0);
316 SET_MSG_PROC(msg, 0);
317 SET_MSG_CMD(msg, frame->skb->data[2]);
318 SET_MSG_SCMD(msg, frame->skb->data[3]);
319
320 frame->refnum = *((ushort *) frame->skb->data + 4);
321 frame->msg = *((ulong *) & msg);
322
323 skb_pull(frame->skb, 6);
324
325 pcbit_l3_receive(dev, frame->msg, frame->skb, frame->hdr_len,
326 frame->refnum);
327
328 kfree(frame);
329
330 spin_lock_irqsave(&dev->lock, flags);
331 }
332
333 spin_unlock_irqrestore(&dev->lock, flags);
334}
335
336/*
337 * Reads BANK 2 & Reassembles
338 */
339
340static void
341pcbit_receive(struct pcbit_dev *dev)
342{
343 unsigned short tt;
344 u_char cpu,
345 proc;
346 struct frame_buf *frame = NULL;
347 unsigned long flags;
348 u_char type1;
349
350 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING)
351 return;
352
353 tt = pcbit_readw(dev);
354
355 if ((tt & 0x7fffU) > 511) {
356 printk(KERN_INFO "pcbit: invalid frame length -> TT=%04x\n",
357 tt);
358 pcbit_l2_error(dev);
359 return;
360 }
361 if (!(tt & 0x8000U)) { /* Type 0 */
362 type1 = 0;
363
364 if (dev->read_frame) {
365 printk(KERN_DEBUG "pcbit_receive: Type 0 frame and read_frame != NULL\n");
366 /* discard previous queued frame */
367 if (dev->read_frame->skb)
368 kfree_skb(dev->read_frame->skb);
369 kfree(dev->read_frame);
370 dev->read_frame = NULL;
371 }
372 frame = kmalloc(sizeof(struct frame_buf), GFP_ATOMIC);
373
374 if (frame == NULL) {
375 printk(KERN_WARNING "kmalloc failed\n");
376 return;
377 }
378 memset(frame, 0, sizeof(struct frame_buf));
379
380 cpu = pcbit_readb(dev);
381 proc = pcbit_readb(dev);
382
383
384 if (cpu != 0x06 && cpu != 0x02) {
385 printk(KERN_DEBUG "pcbit: invalid cpu value\n");
386 kfree(frame);
387 pcbit_l2_error(dev);
388 return;
389 }
390 /*
391 * we discard cpu & proc on receiving
392 * but we read it to update the pointer
393 */
394
395 frame->hdr_len = pcbit_readw(dev);
396 frame->dt_len = pcbit_readw(dev);
397
398 /*
399 * 0 sized packet
400 * I don't know if they are an error or not...
401 * But they are very frequent
402 * Not documented
403 */
404
405 if (frame->hdr_len == 0) {
406 kfree(frame);
407#ifdef DEBUG
408 printk(KERN_DEBUG "0 sized frame\n");
409#endif
410 pcbit_firmware_bug(dev);
411 return;
412 }
413 /* sanity check the length values */
414 if (frame->hdr_len > 1024 || frame->dt_len > 2048) {
415#ifdef DEBUG
416 printk(KERN_DEBUG "length problem: ");
417 printk(KERN_DEBUG "TH=%04x TD=%04x\n",
418 frame->hdr_len,
419 frame->dt_len);
420#endif
421 pcbit_l2_error(dev);
422 kfree(frame);
423 return;
424 }
425 /* minimum frame read */
426
427 frame->skb = dev_alloc_skb(frame->hdr_len + frame->dt_len +
428 ((frame->hdr_len + 15) & ~15));
429
430 if (!frame->skb) {
431 printk(KERN_DEBUG "pcbit_receive: out of memory\n");
432 kfree(frame);
433 return;
434 }
435 /* 16 byte alignment for IP */
436 if (frame->dt_len)
437 skb_reserve(frame->skb, (frame->hdr_len + 15) & ~15);
438
439 } else {
440 /* Type 1 */
441 type1 = 1;
442 tt &= 0x7fffU;
443
444 if (!(frame = dev->read_frame)) {
445 printk("Type 1 frame and no frame queued\n");
446 /* usually after an error: toss frame */
447 dev->readptr += tt;
448 if (dev->readptr > dev->sh_mem + BANK2 + BANKLEN)
449 dev->readptr -= BANKLEN;
450 return;
451
452 }
453 }
454
455 memcpy_frompcbit(dev, skb_put(frame->skb, tt), tt);
456
457 frame->copied += tt;
458 spin_lock_irqsave(&dev->lock, flags);
459 if (frame->copied == frame->hdr_len + frame->dt_len) {
460
461 if (type1) {
462 dev->read_frame = NULL;
463 }
464 if (dev->read_queue) {
465 struct frame_buf *ptr;
466 for (ptr = dev->read_queue; ptr->next; ptr = ptr->next);
467 ptr->next = frame;
468 } else
469 dev->read_queue = frame;
470
471 } else {
472 dev->read_frame = frame;
473 }
474 spin_unlock_irqrestore(&dev->lock, flags);
475}
476
477/*
478 * The board sends 0 sized frames
479 * They are TDATA_CONFs that get messed up somehow
480 * gotta send a fake acknowledgment to the upper layer somehow
481 */
482
483static __inline__ void
484pcbit_fake_conf(struct pcbit_dev *dev, struct pcbit_chan *chan)
485{
486 isdn_ctrl ictl;
487
488 if (chan->queued) {
489 chan->queued--;
490
491 ictl.driver = dev->id;
492 ictl.command = ISDN_STAT_BSENT;
493 ictl.arg = chan->id;
494 dev->dev_if->statcallb(&ictl);
495 }
496}
497
498static void
499pcbit_firmware_bug(struct pcbit_dev *dev)
500{
501 struct pcbit_chan *chan;
502
503 chan = dev->b1;
504
505 if (chan->fsm_state == ST_ACTIVE) {
506 pcbit_fake_conf(dev, chan);
507 }
508 chan = dev->b2;
509
510 if (chan->fsm_state == ST_ACTIVE) {
511 pcbit_fake_conf(dev, chan);
512 }
513}
514
515irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100516pcbit_irq_handler(int interrupt, void *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct pcbit_dev *dev;
519 u_char info,
520 ack_seq,
521 read_seq;
522
523 dev = (struct pcbit_dev *) devptr;
524
525 if (!dev) {
526 printk(KERN_WARNING "pcbit_irq_handler: wrong device\n");
527 return IRQ_NONE;
528 }
529 if (dev->interrupt) {
530 printk(KERN_DEBUG "pcbit: reentering interrupt hander\n");
531 return IRQ_HANDLED;
532 }
533 dev->interrupt = 1;
534
535 info = readb(dev->sh_mem + BANK3);
536
537 if (dev->l2_state == L2_STARTING || dev->l2_state == L2_ERROR) {
538 pcbit_l2_active_conf(dev, info);
539 dev->interrupt = 0;
540 return IRQ_HANDLED;
541 }
542 if (info & 0x40U) { /* E bit set */
543#ifdef DEBUG
544 printk(KERN_DEBUG "pcbit_irq_handler: E bit on\n");
545#endif
546 pcbit_l2_error(dev);
547 dev->interrupt = 0;
548 return IRQ_HANDLED;
549 }
550 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING) {
551 dev->interrupt = 0;
552 return IRQ_HANDLED;
553 }
554 ack_seq = (info >> 3) & 0x07U;
555 read_seq = (info & 0x07U);
556
557 dev->interrupt = 0;
558
559 if (read_seq != dev->rcv_seq) {
560 while (read_seq != dev->rcv_seq) {
561 pcbit_receive(dev);
562 dev->rcv_seq = (dev->rcv_seq + 1) % 8;
563 }
564 pcbit_sched_delivery(dev);
565 }
566 if (ack_seq != dev->unack_seq) {
567 pcbit_recv_ack(dev, ack_seq);
568 }
569 info = dev->rcv_seq << 3;
570 info |= dev->send_seq;
571
572 writeb(info, dev->sh_mem + BANK4);
573 return IRQ_HANDLED;
574}
575
576
577static void
578pcbit_l2_active_conf(struct pcbit_dev *dev, u_char info)
579{
580 u_char state;
581
582 state = dev->l2_state;
583
584#ifdef DEBUG
585 printk(KERN_DEBUG "layer2_active_confirm\n");
586#endif
587
588
589 if (info & 0x80U) {
590 dev->rcv_seq = info & 0x07U;
591 dev->l2_state = L2_RUNNING;
592 } else
593 dev->l2_state = L2_DOWN;
594
595 if (state == L2_STARTING)
596 wake_up_interruptible(&dev->set_running_wq);
597
598 if (state == L2_ERROR && dev->l2_state == L2_RUNNING) {
599 pcbit_transmit(dev);
600 }
601}
602
603static void
604pcbit_l2_err_recover(unsigned long data)
605{
606
607 struct pcbit_dev *dev;
608 struct frame_buf *frame;
609
610 dev = (struct pcbit_dev *) data;
611
612 del_timer(&dev->error_recover_timer);
613 if (dev->w_busy || dev->r_busy) {
614 init_timer(&dev->error_recover_timer);
615 dev->error_recover_timer.expires = jiffies + ERRTIME;
616 add_timer(&dev->error_recover_timer);
617 return;
618 }
619 dev->w_busy = dev->r_busy = 1;
620
621 if (dev->read_frame) {
622 if (dev->read_frame->skb)
623 kfree_skb(dev->read_frame->skb);
624 kfree(dev->read_frame);
625 dev->read_frame = NULL;
626 }
627 if (dev->write_queue) {
628 frame = dev->write_queue;
629#ifdef FREE_ON_ERROR
630 dev->write_queue = dev->write_queue->next;
631
632 if (frame->skb) {
633 dev_kfree_skb(frame->skb);
634 }
635 kfree(frame);
636#else
637 frame->copied = 0;
638#endif
639 }
640 dev->rcv_seq = dev->send_seq = dev->unack_seq = 0;
641 dev->free = 511;
642 dev->l2_state = L2_ERROR;
643
644 /* this is an hack... */
645 pcbit_firmware_bug(dev);
646
647 dev->writeptr = dev->sh_mem;
648 dev->readptr = dev->sh_mem + BANK2;
649
650 writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
651 dev->sh_mem + BANK4);
652 dev->w_busy = dev->r_busy = 0;
653
654}
655
656static void
657pcbit_l2_error(struct pcbit_dev *dev)
658{
659 if (dev->l2_state == L2_RUNNING) {
660
661 printk(KERN_INFO "pcbit: layer 2 error\n");
662
663#ifdef DEBUG
664 log_state(dev);
665#endif
666
667 dev->l2_state = L2_DOWN;
668
669 init_timer(&dev->error_recover_timer);
670 dev->error_recover_timer.function = &pcbit_l2_err_recover;
671 dev->error_recover_timer.data = (ulong) dev;
672 dev->error_recover_timer.expires = jiffies + ERRTIME;
673 add_timer(&dev->error_recover_timer);
674 }
675}
676
677/*
678 * Description:
679 * if board acks frames
680 * update dev->free
681 * call pcbit_transmit to write possible queued frames
682 */
683
684static void
685pcbit_recv_ack(struct pcbit_dev *dev, unsigned char ack)
686{
687 int i,
688 count;
689 int unacked;
690
691 unacked = (dev->send_seq + (8 - dev->unack_seq)) & 0x07;
692
693 /* dev->unack_seq < ack <= dev->send_seq; */
694
695 if (unacked) {
696
697 if (dev->send_seq > dev->unack_seq) {
698 if (ack <= dev->unack_seq || ack > dev->send_seq) {
699 printk(KERN_DEBUG
700 "layer 2 ack unacceptable - dev %d",
701 dev->id);
702
703 pcbit_l2_error(dev);
704 } else if (ack > dev->send_seq && ack <= dev->unack_seq) {
705 printk(KERN_DEBUG
706 "layer 2 ack unacceptable - dev %d",
707 dev->id);
708 pcbit_l2_error(dev);
709 }
710 }
711 /* ack is acceptable */
712
713
714 i = dev->unack_seq;
715
716 do {
717 dev->unack_seq = i = (i + 1) % 8;
718 dev->free += dev->fsize[i];
719 } while (i != ack);
720
721 count = 0;
722 while (count < 7 && dev->write_queue) {
723 u8 lsend_seq = dev->send_seq;
724
725 pcbit_transmit(dev);
726
727 if (dev->send_seq == lsend_seq)
728 break;
729 count++;
730 }
731 } else
732 printk(KERN_DEBUG "recv_ack: unacked = 0\n");
733}