gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / isdn / pcbit / layer2.c
blob46e1240ae0741a54008a976602b11a90f00ea685
1 /*
2 * PCBIT-D low-layer interface
4 * Copyright (C) 1996 Universidade de Lisboa
6 * Written by Pedro Roque Marques (roque@di.fc.ul.pt)
8 * This software may be used and distributed according to the terms of
9 * the GNU General Public License, incorporated herein by reference.
13 * 19991203 - Fernando Carvalho - takion@superbofh.org
14 * Hacked to compile with egcs and run with current version of isdn modules
18 * Based on documentation provided by Inesc:
19 * - "Interface com bus do PC para o PCBIT e PCBIT-D", Inesc, Jan 93
23 * TODO: better handling of errors
24 * re-write/remove debug printks
27 #include <linux/string.h>
28 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <linux/sched.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>
37 #include <linux/isdnif.h>
39 #include <asm/io.h>
42 #include "pcbit.h"
43 #include "layer2.h"
44 #include "edss1.h"
46 #undef DEBUG_FRAG
50 * Prototypes
53 static void pcbit_transmit(struct pcbit_dev *dev);
55 static void pcbit_recv_ack(struct pcbit_dev *dev, unsigned char ack);
57 static void pcbit_l2_error(struct pcbit_dev *dev);
58 static void pcbit_l2_active_conf(struct pcbit_dev *dev, u_char info);
59 static void pcbit_l2_err_recover(unsigned long data);
61 static void pcbit_firmware_bug(struct pcbit_dev *dev);
63 static __inline__ void
64 pcbit_sched_delivery(struct pcbit_dev *dev)
66 schedule_work(&dev->qdelivery);
71 * Called from layer3
74 int
75 pcbit_l2_write(struct pcbit_dev *dev, ulong msg, ushort refnum,
76 struct sk_buff *skb, unsigned short hdr_len)
78 struct frame_buf *frame,
79 *ptr;
80 unsigned long flags;
82 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING) {
83 dev_kfree_skb(skb);
84 return -1;
86 if ((frame = kmalloc(sizeof(struct frame_buf),
87 GFP_ATOMIC)) == NULL) {
88 dev_kfree_skb(skb);
89 return -1;
91 frame->msg = msg;
92 frame->refnum = refnum;
93 frame->copied = 0;
94 frame->hdr_len = hdr_len;
96 if (skb)
97 frame->dt_len = skb->len - hdr_len;
98 else
99 frame->dt_len = 0;
101 frame->skb = skb;
103 frame->next = NULL;
105 spin_lock_irqsave(&dev->lock, flags);
107 if (dev->write_queue == NULL) {
108 dev->write_queue = frame;
109 spin_unlock_irqrestore(&dev->lock, flags);
110 pcbit_transmit(dev);
111 } else {
112 for (ptr = dev->write_queue; ptr->next; ptr = ptr->next);
113 ptr->next = frame;
115 spin_unlock_irqrestore(&dev->lock, flags);
117 return 0;
120 static __inline__ void
121 pcbit_tx_update(struct pcbit_dev *dev, ushort len)
123 u_char info;
125 dev->send_seq = (dev->send_seq + 1) % 8;
127 dev->fsize[dev->send_seq] = len;
128 info = 0;
129 info |= dev->rcv_seq << 3;
130 info |= dev->send_seq;
132 writeb(info, dev->sh_mem + BANK4);
137 * called by interrupt service routine or by write_2
140 static void
141 pcbit_transmit(struct pcbit_dev *dev)
143 struct frame_buf *frame = NULL;
144 unsigned char unacked;
145 int flen; /* fragment frame length including all headers */
146 int free;
147 int count,
148 cp_len;
149 unsigned long flags;
150 unsigned short tt;
152 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING)
153 return;
155 unacked = (dev->send_seq + (8 - dev->unack_seq)) & 0x07;
157 spin_lock_irqsave(&dev->lock, flags);
159 if (dev->free > 16 && dev->write_queue && unacked < 7) {
161 if (!dev->w_busy)
162 dev->w_busy = 1;
163 else {
164 spin_unlock_irqrestore(&dev->lock, flags);
165 return;
169 frame = dev->write_queue;
170 free = dev->free;
172 spin_unlock_irqrestore(&dev->lock, flags);
174 if (frame->copied == 0) {
176 /* Type 0 frame */
178 ulong msg;
180 if (frame->skb)
181 flen = FRAME_HDR_LEN + PREHDR_LEN + frame->skb->len;
182 else
183 flen = FRAME_HDR_LEN + PREHDR_LEN;
185 if (flen > free)
186 flen = free;
188 msg = frame->msg;
191 * Board level 2 header
194 pcbit_writew(dev, flen - FRAME_HDR_LEN);
196 pcbit_writeb(dev, GET_MSG_CPU(msg));
198 pcbit_writeb(dev, GET_MSG_PROC(msg));
200 /* TH */
201 pcbit_writew(dev, frame->hdr_len + PREHDR_LEN);
203 /* TD */
204 pcbit_writew(dev, frame->dt_len);
208 * Board level 3 fixed-header
211 /* LEN = TH */
212 pcbit_writew(dev, frame->hdr_len + PREHDR_LEN);
214 /* XX */
215 pcbit_writew(dev, 0);
217 /* C + S */
218 pcbit_writeb(dev, GET_MSG_CMD(msg));
219 pcbit_writeb(dev, GET_MSG_SCMD(msg));
221 /* NUM */
222 pcbit_writew(dev, frame->refnum);
224 count = FRAME_HDR_LEN + PREHDR_LEN;
225 } else {
226 /* Type 1 frame */
228 flen = 2 + (frame->skb->len - frame->copied);
230 if (flen > free)
231 flen = free;
233 /* TT */
234 tt = ((ushort) (flen - 2)) | 0x8000U; /* Type 1 */
235 pcbit_writew(dev, tt);
237 count = 2;
240 if (frame->skb) {
241 cp_len = frame->skb->len - frame->copied;
242 if (cp_len > flen - count)
243 cp_len = flen - count;
245 memcpy_topcbit(dev, frame->skb->data + frame->copied,
246 cp_len);
247 frame->copied += cp_len;
249 /* bookkeeping */
250 dev->free -= flen;
251 pcbit_tx_update(dev, flen);
253 spin_lock_irqsave(&dev->lock, flags);
255 if (frame->skb == NULL || frame->copied == frame->skb->len) {
257 dev->write_queue = frame->next;
259 if (frame->skb != NULL) {
260 /* free frame */
261 dev_kfree_skb(frame->skb);
263 kfree(frame);
265 dev->w_busy = 0;
266 spin_unlock_irqrestore(&dev->lock, flags);
267 } else {
268 spin_unlock_irqrestore(&dev->lock, flags);
269 #ifdef DEBUG
270 printk(KERN_DEBUG "unacked %d free %d write_queue %s\n",
271 unacked, dev->free, dev->write_queue ? "not empty" :
272 "empty");
273 #endif
279 * deliver a queued frame to the upper layer
282 void
283 pcbit_deliver(struct work_struct *work)
285 struct frame_buf *frame;
286 unsigned long flags, msg;
287 struct pcbit_dev *dev =
288 container_of(work, struct pcbit_dev, qdelivery);
290 spin_lock_irqsave(&dev->lock, flags);
292 while ((frame = dev->read_queue)) {
293 dev->read_queue = frame->next;
294 spin_unlock_irqrestore(&dev->lock, flags);
296 msg = 0;
297 SET_MSG_CPU(msg, 0);
298 SET_MSG_PROC(msg, 0);
299 SET_MSG_CMD(msg, frame->skb->data[2]);
300 SET_MSG_SCMD(msg, frame->skb->data[3]);
302 frame->refnum = *((ushort *)frame->skb->data + 4);
303 frame->msg = *((ulong *)&msg);
305 skb_pull(frame->skb, 6);
307 pcbit_l3_receive(dev, frame->msg, frame->skb, frame->hdr_len,
308 frame->refnum);
310 kfree(frame);
312 spin_lock_irqsave(&dev->lock, flags);
315 spin_unlock_irqrestore(&dev->lock, flags);
319 * Reads BANK 2 & Reassembles
322 static void
323 pcbit_receive(struct pcbit_dev *dev)
325 unsigned short tt;
326 u_char cpu,
327 proc;
328 struct frame_buf *frame = NULL;
329 unsigned long flags;
330 u_char type1;
332 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING)
333 return;
335 tt = pcbit_readw(dev);
337 if ((tt & 0x7fffU) > 511) {
338 printk(KERN_INFO "pcbit: invalid frame length -> TT=%04x\n",
339 tt);
340 pcbit_l2_error(dev);
341 return;
343 if (!(tt & 0x8000U)) { /* Type 0 */
344 type1 = 0;
346 if (dev->read_frame) {
347 printk(KERN_DEBUG "pcbit_receive: Type 0 frame and read_frame != NULL\n");
348 /* discard previous queued frame */
349 kfree_skb(dev->read_frame->skb);
350 kfree(dev->read_frame);
351 dev->read_frame = NULL;
353 frame = kzalloc(sizeof(struct frame_buf), GFP_ATOMIC);
355 if (frame == NULL) {
356 printk(KERN_WARNING "kmalloc failed\n");
357 return;
360 cpu = pcbit_readb(dev);
361 proc = pcbit_readb(dev);
364 if (cpu != 0x06 && cpu != 0x02) {
365 printk(KERN_DEBUG "pcbit: invalid cpu value\n");
366 kfree(frame);
367 pcbit_l2_error(dev);
368 return;
371 * we discard cpu & proc on receiving
372 * but we read it to update the pointer
375 frame->hdr_len = pcbit_readw(dev);
376 frame->dt_len = pcbit_readw(dev);
379 * 0 sized packet
380 * I don't know if they are an error or not...
381 * But they are very frequent
382 * Not documented
385 if (frame->hdr_len == 0) {
386 kfree(frame);
387 #ifdef DEBUG
388 printk(KERN_DEBUG "0 sized frame\n");
389 #endif
390 pcbit_firmware_bug(dev);
391 return;
393 /* sanity check the length values */
394 if (frame->hdr_len > 1024 || frame->dt_len > 2048) {
395 #ifdef DEBUG
396 printk(KERN_DEBUG "length problem: ");
397 printk(KERN_DEBUG "TH=%04x TD=%04x\n",
398 frame->hdr_len,
399 frame->dt_len);
400 #endif
401 pcbit_l2_error(dev);
402 kfree(frame);
403 return;
405 /* minimum frame read */
407 frame->skb = dev_alloc_skb(frame->hdr_len + frame->dt_len +
408 ((frame->hdr_len + 15) & ~15));
410 if (!frame->skb) {
411 printk(KERN_DEBUG "pcbit_receive: out of memory\n");
412 kfree(frame);
413 return;
415 /* 16 byte alignment for IP */
416 if (frame->dt_len)
417 skb_reserve(frame->skb, (frame->hdr_len + 15) & ~15);
419 } else {
420 /* Type 1 */
421 type1 = 1;
422 tt &= 0x7fffU;
424 if (!(frame = dev->read_frame)) {
425 printk("Type 1 frame and no frame queued\n");
426 /* usually after an error: toss frame */
427 dev->readptr += tt;
428 if (dev->readptr > dev->sh_mem + BANK2 + BANKLEN)
429 dev->readptr -= BANKLEN;
430 return;
435 memcpy_frompcbit(dev, skb_put(frame->skb, tt), tt);
437 frame->copied += tt;
438 spin_lock_irqsave(&dev->lock, flags);
439 if (frame->copied == frame->hdr_len + frame->dt_len) {
441 if (type1) {
442 dev->read_frame = NULL;
444 if (dev->read_queue) {
445 struct frame_buf *ptr;
446 for (ptr = dev->read_queue; ptr->next; ptr = ptr->next);
447 ptr->next = frame;
448 } else
449 dev->read_queue = frame;
451 } else {
452 dev->read_frame = frame;
454 spin_unlock_irqrestore(&dev->lock, flags);
458 * The board sends 0 sized frames
459 * They are TDATA_CONFs that get messed up somehow
460 * gotta send a fake acknowledgment to the upper layer somehow
463 static __inline__ void
464 pcbit_fake_conf(struct pcbit_dev *dev, struct pcbit_chan *chan)
466 isdn_ctrl ictl;
468 if (chan->queued) {
469 chan->queued--;
471 ictl.driver = dev->id;
472 ictl.command = ISDN_STAT_BSENT;
473 ictl.arg = chan->id;
474 dev->dev_if->statcallb(&ictl);
478 static void
479 pcbit_firmware_bug(struct pcbit_dev *dev)
481 struct pcbit_chan *chan;
483 chan = dev->b1;
485 if (chan->fsm_state == ST_ACTIVE) {
486 pcbit_fake_conf(dev, chan);
488 chan = dev->b2;
490 if (chan->fsm_state == ST_ACTIVE) {
491 pcbit_fake_conf(dev, chan);
495 irqreturn_t
496 pcbit_irq_handler(int interrupt, void *devptr)
498 struct pcbit_dev *dev;
499 u_char info,
500 ack_seq,
501 read_seq;
503 dev = (struct pcbit_dev *) devptr;
505 if (!dev) {
506 printk(KERN_WARNING "pcbit_irq_handler: wrong device\n");
507 return IRQ_NONE;
509 if (dev->interrupt) {
510 printk(KERN_DEBUG "pcbit: reentering interrupt handler\n");
511 return IRQ_HANDLED;
513 dev->interrupt = 1;
515 info = readb(dev->sh_mem + BANK3);
517 if (dev->l2_state == L2_STARTING || dev->l2_state == L2_ERROR) {
518 pcbit_l2_active_conf(dev, info);
519 dev->interrupt = 0;
520 return IRQ_HANDLED;
522 if (info & 0x40U) { /* E bit set */
523 #ifdef DEBUG
524 printk(KERN_DEBUG "pcbit_irq_handler: E bit on\n");
525 #endif
526 pcbit_l2_error(dev);
527 dev->interrupt = 0;
528 return IRQ_HANDLED;
530 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING) {
531 dev->interrupt = 0;
532 return IRQ_HANDLED;
534 ack_seq = (info >> 3) & 0x07U;
535 read_seq = (info & 0x07U);
537 dev->interrupt = 0;
539 if (read_seq != dev->rcv_seq) {
540 while (read_seq != dev->rcv_seq) {
541 pcbit_receive(dev);
542 dev->rcv_seq = (dev->rcv_seq + 1) % 8;
544 pcbit_sched_delivery(dev);
546 if (ack_seq != dev->unack_seq) {
547 pcbit_recv_ack(dev, ack_seq);
549 info = dev->rcv_seq << 3;
550 info |= dev->send_seq;
552 writeb(info, dev->sh_mem + BANK4);
553 return IRQ_HANDLED;
557 static void
558 pcbit_l2_active_conf(struct pcbit_dev *dev, u_char info)
560 u_char state;
562 state = dev->l2_state;
564 #ifdef DEBUG
565 printk(KERN_DEBUG "layer2_active_confirm\n");
566 #endif
569 if (info & 0x80U) {
570 dev->rcv_seq = info & 0x07U;
571 dev->l2_state = L2_RUNNING;
572 } else
573 dev->l2_state = L2_DOWN;
575 if (state == L2_STARTING)
576 wake_up_interruptible(&dev->set_running_wq);
578 if (state == L2_ERROR && dev->l2_state == L2_RUNNING) {
579 pcbit_transmit(dev);
583 static void
584 pcbit_l2_err_recover(unsigned long data)
587 struct pcbit_dev *dev;
588 struct frame_buf *frame;
590 dev = (struct pcbit_dev *) data;
592 del_timer(&dev->error_recover_timer);
593 if (dev->w_busy || dev->r_busy) {
594 init_timer(&dev->error_recover_timer);
595 dev->error_recover_timer.expires = jiffies + ERRTIME;
596 add_timer(&dev->error_recover_timer);
597 return;
599 dev->w_busy = dev->r_busy = 1;
601 if (dev->read_frame) {
602 kfree_skb(dev->read_frame->skb);
603 kfree(dev->read_frame);
604 dev->read_frame = NULL;
606 if (dev->write_queue) {
607 frame = dev->write_queue;
608 #ifdef FREE_ON_ERROR
609 dev->write_queue = dev->write_queue->next;
611 if (frame->skb) {
612 dev_kfree_skb(frame->skb);
614 kfree(frame);
615 #else
616 frame->copied = 0;
617 #endif
619 dev->rcv_seq = dev->send_seq = dev->unack_seq = 0;
620 dev->free = 511;
621 dev->l2_state = L2_ERROR;
623 /* this is an hack... */
624 pcbit_firmware_bug(dev);
626 dev->writeptr = dev->sh_mem;
627 dev->readptr = dev->sh_mem + BANK2;
629 writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
630 dev->sh_mem + BANK4);
631 dev->w_busy = dev->r_busy = 0;
635 static void
636 pcbit_l2_error(struct pcbit_dev *dev)
638 if (dev->l2_state == L2_RUNNING) {
640 printk(KERN_INFO "pcbit: layer 2 error\n");
642 #ifdef DEBUG
643 log_state(dev);
644 #endif
646 dev->l2_state = L2_DOWN;
648 init_timer(&dev->error_recover_timer);
649 dev->error_recover_timer.function = &pcbit_l2_err_recover;
650 dev->error_recover_timer.data = (ulong) dev;
651 dev->error_recover_timer.expires = jiffies + ERRTIME;
652 add_timer(&dev->error_recover_timer);
657 * Description:
658 * if board acks frames
659 * update dev->free
660 * call pcbit_transmit to write possible queued frames
663 static void
664 pcbit_recv_ack(struct pcbit_dev *dev, unsigned char ack)
666 int i,
667 count;
668 int unacked;
670 unacked = (dev->send_seq + (8 - dev->unack_seq)) & 0x07;
672 /* dev->unack_seq < ack <= dev->send_seq; */
674 if (unacked) {
676 if (dev->send_seq > dev->unack_seq) {
677 if (ack <= dev->unack_seq || ack > dev->send_seq) {
678 printk(KERN_DEBUG
679 "layer 2 ack unacceptable - dev %d",
680 dev->id);
682 pcbit_l2_error(dev);
683 } else if (ack > dev->send_seq && ack <= dev->unack_seq) {
684 printk(KERN_DEBUG
685 "layer 2 ack unacceptable - dev %d",
686 dev->id);
687 pcbit_l2_error(dev);
690 /* ack is acceptable */
693 i = dev->unack_seq;
695 do {
696 dev->unack_seq = i = (i + 1) % 8;
697 dev->free += dev->fsize[i];
698 } while (i != ack);
700 count = 0;
701 while (count < 7 && dev->write_queue) {
702 u8 lsend_seq = dev->send_seq;
704 pcbit_transmit(dev);
706 if (dev->send_seq == lsend_seq)
707 break;
708 count++;
710 } else
711 printk(KERN_DEBUG "recv_ack: unacked = 0\n");