ieee1394: sbp2: fix sbp2_remove_device for error cases
[usb.git] / drivers / isdn / pcbit / layer2.c
blob5ba2a879df1447e42bcead890600ba9c51ed9800
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/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/workqueue.h>
33 #include <linux/mm.h>
34 #include <linux/skbuff.h>
36 #include <linux/isdnif.h>
38 #include <asm/system.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 printk(KERN_WARNING "pcbit_2_write: kmalloc failed\n");
89 dev_kfree_skb(skb);
90 return -1;
92 frame->msg = msg;
93 frame->refnum = refnum;
94 frame->copied = 0;
95 frame->hdr_len = hdr_len;
97 if (skb)
98 frame->dt_len = skb->len - hdr_len;
99 else
100 frame->dt_len = 0;
102 frame->skb = skb;
104 frame->next = NULL;
106 spin_lock_irqsave(&dev->lock, flags);
108 if (dev->write_queue == NULL) {
109 dev->write_queue = frame;
110 spin_unlock_irqrestore(&dev->lock, flags);
111 pcbit_transmit(dev);
112 } else {
113 for (ptr = dev->write_queue; ptr->next; ptr = ptr->next);
114 ptr->next = frame;
116 spin_unlock_irqrestore(&dev->lock, flags);
118 return 0;
121 static __inline__ void
122 pcbit_tx_update(struct pcbit_dev *dev, ushort len)
124 u_char info;
126 dev->send_seq = (dev->send_seq + 1) % 8;
128 dev->fsize[dev->send_seq] = len;
129 info = 0;
130 info |= dev->rcv_seq << 3;
131 info |= dev->send_seq;
133 writeb(info, dev->sh_mem + BANK4);
138 * called by interrupt service routine or by write_2
141 static void
142 pcbit_transmit(struct pcbit_dev *dev)
144 struct frame_buf *frame = NULL;
145 unsigned char unacked;
146 int flen; /* fragment frame length including all headers */
147 int free;
148 int count,
149 cp_len;
150 unsigned long flags;
151 unsigned short tt;
153 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING)
154 return;
156 unacked = (dev->send_seq + (8 - dev->unack_seq)) & 0x07;
158 spin_lock_irqsave(&dev->lock, flags);
160 if (dev->free > 16 && dev->write_queue && unacked < 7) {
162 if (!dev->w_busy)
163 dev->w_busy = 1;
164 else {
165 spin_unlock_irqrestore(&dev->lock, flags);
166 return;
170 frame = dev->write_queue;
171 free = dev->free;
173 spin_unlock_irqrestore(&dev->lock, flags);
175 if (frame->copied == 0) {
177 /* Type 0 frame */
179 ulong msg;
181 if (frame->skb)
182 flen = FRAME_HDR_LEN + PREHDR_LEN + frame->skb->len;
183 else
184 flen = FRAME_HDR_LEN + PREHDR_LEN;
186 if (flen > free)
187 flen = free;
189 msg = frame->msg;
192 * Board level 2 header
195 pcbit_writew(dev, flen - FRAME_HDR_LEN);
197 pcbit_writeb(dev, GET_MSG_CPU(msg));
199 pcbit_writeb(dev, GET_MSG_PROC(msg));
201 /* TH */
202 pcbit_writew(dev, frame->hdr_len + PREHDR_LEN);
204 /* TD */
205 pcbit_writew(dev, frame->dt_len);
209 * Board level 3 fixed-header
212 /* LEN = TH */
213 pcbit_writew(dev, frame->hdr_len + PREHDR_LEN);
215 /* XX */
216 pcbit_writew(dev, 0);
218 /* C + S */
219 pcbit_writeb(dev, GET_MSG_CMD(msg));
220 pcbit_writeb(dev, GET_MSG_SCMD(msg));
222 /* NUM */
223 pcbit_writew(dev, frame->refnum);
225 count = FRAME_HDR_LEN + PREHDR_LEN;
226 } else {
227 /* Type 1 frame */
229 flen = 2 + (frame->skb->len - frame->copied);
231 if (flen > free)
232 flen = free;
234 /* TT */
235 tt = ((ushort) (flen - 2)) | 0x8000U; /* Type 1 */
236 pcbit_writew(dev, tt);
238 count = 2;
241 if (frame->skb) {
242 cp_len = frame->skb->len - frame->copied;
243 if (cp_len > flen - count)
244 cp_len = flen - count;
246 memcpy_topcbit(dev, frame->skb->data + frame->copied,
247 cp_len);
248 frame->copied += cp_len;
250 /* bookkeeping */
251 dev->free -= flen;
252 pcbit_tx_update(dev, flen);
254 spin_lock_irqsave(&dev->lock, flags);
256 if (frame->skb == NULL || frame->copied == frame->skb->len) {
258 dev->write_queue = frame->next;
260 if (frame->skb != NULL) {
261 /* free frame */
262 dev_kfree_skb(frame->skb);
264 kfree(frame);
266 dev->w_busy = 0;
267 spin_unlock_irqrestore(&dev->lock, flags);
268 } else {
269 spin_unlock_irqrestore(&dev->lock, flags);
270 #ifdef DEBUG
271 printk(KERN_DEBUG "unacked %d free %d write_queue %s\n",
272 unacked, dev->free, dev->write_queue ? "not empty" :
273 "empty");
274 #endif
280 * deliver a queued frame to the upper layer
283 void
284 pcbit_deliver(struct work_struct *work)
286 struct frame_buf *frame;
287 unsigned long flags, msg;
288 struct pcbit_dev *dev =
289 container_of(work, struct pcbit_dev, qdelivery);
291 spin_lock_irqsave(&dev->lock, flags);
293 while ((frame = dev->read_queue)) {
294 dev->read_queue = frame->next;
295 spin_unlock_irqrestore(&dev->lock, flags);
297 msg = 0;
298 SET_MSG_CPU(msg, 0);
299 SET_MSG_PROC(msg, 0);
300 SET_MSG_CMD(msg, frame->skb->data[2]);
301 SET_MSG_SCMD(msg, frame->skb->data[3]);
303 frame->refnum = *((ushort *) frame->skb->data + 4);
304 frame->msg = *((ulong *) & msg);
306 skb_pull(frame->skb, 6);
308 pcbit_l3_receive(dev, frame->msg, frame->skb, frame->hdr_len,
309 frame->refnum);
311 kfree(frame);
313 spin_lock_irqsave(&dev->lock, flags);
316 spin_unlock_irqrestore(&dev->lock, flags);
320 * Reads BANK 2 & Reassembles
323 static void
324 pcbit_receive(struct pcbit_dev *dev)
326 unsigned short tt;
327 u_char cpu,
328 proc;
329 struct frame_buf *frame = NULL;
330 unsigned long flags;
331 u_char type1;
333 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING)
334 return;
336 tt = pcbit_readw(dev);
338 if ((tt & 0x7fffU) > 511) {
339 printk(KERN_INFO "pcbit: invalid frame length -> TT=%04x\n",
340 tt);
341 pcbit_l2_error(dev);
342 return;
344 if (!(tt & 0x8000U)) { /* Type 0 */
345 type1 = 0;
347 if (dev->read_frame) {
348 printk(KERN_DEBUG "pcbit_receive: Type 0 frame and read_frame != NULL\n");
349 /* discard previous queued frame */
350 if (dev->read_frame->skb)
351 kfree_skb(dev->read_frame->skb);
352 kfree(dev->read_frame);
353 dev->read_frame = NULL;
355 frame = kzalloc(sizeof(struct frame_buf), GFP_ATOMIC);
357 if (frame == NULL) {
358 printk(KERN_WARNING "kmalloc failed\n");
359 return;
362 cpu = pcbit_readb(dev);
363 proc = pcbit_readb(dev);
366 if (cpu != 0x06 && cpu != 0x02) {
367 printk(KERN_DEBUG "pcbit: invalid cpu value\n");
368 kfree(frame);
369 pcbit_l2_error(dev);
370 return;
373 * we discard cpu & proc on receiving
374 * but we read it to update the pointer
377 frame->hdr_len = pcbit_readw(dev);
378 frame->dt_len = pcbit_readw(dev);
381 * 0 sized packet
382 * I don't know if they are an error or not...
383 * But they are very frequent
384 * Not documented
387 if (frame->hdr_len == 0) {
388 kfree(frame);
389 #ifdef DEBUG
390 printk(KERN_DEBUG "0 sized frame\n");
391 #endif
392 pcbit_firmware_bug(dev);
393 return;
395 /* sanity check the length values */
396 if (frame->hdr_len > 1024 || frame->dt_len > 2048) {
397 #ifdef DEBUG
398 printk(KERN_DEBUG "length problem: ");
399 printk(KERN_DEBUG "TH=%04x TD=%04x\n",
400 frame->hdr_len,
401 frame->dt_len);
402 #endif
403 pcbit_l2_error(dev);
404 kfree(frame);
405 return;
407 /* minimum frame read */
409 frame->skb = dev_alloc_skb(frame->hdr_len + frame->dt_len +
410 ((frame->hdr_len + 15) & ~15));
412 if (!frame->skb) {
413 printk(KERN_DEBUG "pcbit_receive: out of memory\n");
414 kfree(frame);
415 return;
417 /* 16 byte alignment for IP */
418 if (frame->dt_len)
419 skb_reserve(frame->skb, (frame->hdr_len + 15) & ~15);
421 } else {
422 /* Type 1 */
423 type1 = 1;
424 tt &= 0x7fffU;
426 if (!(frame = dev->read_frame)) {
427 printk("Type 1 frame and no frame queued\n");
428 /* usually after an error: toss frame */
429 dev->readptr += tt;
430 if (dev->readptr > dev->sh_mem + BANK2 + BANKLEN)
431 dev->readptr -= BANKLEN;
432 return;
437 memcpy_frompcbit(dev, skb_put(frame->skb, tt), tt);
439 frame->copied += tt;
440 spin_lock_irqsave(&dev->lock, flags);
441 if (frame->copied == frame->hdr_len + frame->dt_len) {
443 if (type1) {
444 dev->read_frame = NULL;
446 if (dev->read_queue) {
447 struct frame_buf *ptr;
448 for (ptr = dev->read_queue; ptr->next; ptr = ptr->next);
449 ptr->next = frame;
450 } else
451 dev->read_queue = frame;
453 } else {
454 dev->read_frame = frame;
456 spin_unlock_irqrestore(&dev->lock, flags);
460 * The board sends 0 sized frames
461 * They are TDATA_CONFs that get messed up somehow
462 * gotta send a fake acknowledgment to the upper layer somehow
465 static __inline__ void
466 pcbit_fake_conf(struct pcbit_dev *dev, struct pcbit_chan *chan)
468 isdn_ctrl ictl;
470 if (chan->queued) {
471 chan->queued--;
473 ictl.driver = dev->id;
474 ictl.command = ISDN_STAT_BSENT;
475 ictl.arg = chan->id;
476 dev->dev_if->statcallb(&ictl);
480 static void
481 pcbit_firmware_bug(struct pcbit_dev *dev)
483 struct pcbit_chan *chan;
485 chan = dev->b1;
487 if (chan->fsm_state == ST_ACTIVE) {
488 pcbit_fake_conf(dev, chan);
490 chan = dev->b2;
492 if (chan->fsm_state == ST_ACTIVE) {
493 pcbit_fake_conf(dev, chan);
497 irqreturn_t
498 pcbit_irq_handler(int interrupt, void *devptr)
500 struct pcbit_dev *dev;
501 u_char info,
502 ack_seq,
503 read_seq;
505 dev = (struct pcbit_dev *) devptr;
507 if (!dev) {
508 printk(KERN_WARNING "pcbit_irq_handler: wrong device\n");
509 return IRQ_NONE;
511 if (dev->interrupt) {
512 printk(KERN_DEBUG "pcbit: reentering interrupt hander\n");
513 return IRQ_HANDLED;
515 dev->interrupt = 1;
517 info = readb(dev->sh_mem + BANK3);
519 if (dev->l2_state == L2_STARTING || dev->l2_state == L2_ERROR) {
520 pcbit_l2_active_conf(dev, info);
521 dev->interrupt = 0;
522 return IRQ_HANDLED;
524 if (info & 0x40U) { /* E bit set */
525 #ifdef DEBUG
526 printk(KERN_DEBUG "pcbit_irq_handler: E bit on\n");
527 #endif
528 pcbit_l2_error(dev);
529 dev->interrupt = 0;
530 return IRQ_HANDLED;
532 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING) {
533 dev->interrupt = 0;
534 return IRQ_HANDLED;
536 ack_seq = (info >> 3) & 0x07U;
537 read_seq = (info & 0x07U);
539 dev->interrupt = 0;
541 if (read_seq != dev->rcv_seq) {
542 while (read_seq != dev->rcv_seq) {
543 pcbit_receive(dev);
544 dev->rcv_seq = (dev->rcv_seq + 1) % 8;
546 pcbit_sched_delivery(dev);
548 if (ack_seq != dev->unack_seq) {
549 pcbit_recv_ack(dev, ack_seq);
551 info = dev->rcv_seq << 3;
552 info |= dev->send_seq;
554 writeb(info, dev->sh_mem + BANK4);
555 return IRQ_HANDLED;
559 static void
560 pcbit_l2_active_conf(struct pcbit_dev *dev, u_char info)
562 u_char state;
564 state = dev->l2_state;
566 #ifdef DEBUG
567 printk(KERN_DEBUG "layer2_active_confirm\n");
568 #endif
571 if (info & 0x80U) {
572 dev->rcv_seq = info & 0x07U;
573 dev->l2_state = L2_RUNNING;
574 } else
575 dev->l2_state = L2_DOWN;
577 if (state == L2_STARTING)
578 wake_up_interruptible(&dev->set_running_wq);
580 if (state == L2_ERROR && dev->l2_state == L2_RUNNING) {
581 pcbit_transmit(dev);
585 static void
586 pcbit_l2_err_recover(unsigned long data)
589 struct pcbit_dev *dev;
590 struct frame_buf *frame;
592 dev = (struct pcbit_dev *) data;
594 del_timer(&dev->error_recover_timer);
595 if (dev->w_busy || dev->r_busy) {
596 init_timer(&dev->error_recover_timer);
597 dev->error_recover_timer.expires = jiffies + ERRTIME;
598 add_timer(&dev->error_recover_timer);
599 return;
601 dev->w_busy = dev->r_busy = 1;
603 if (dev->read_frame) {
604 if (dev->read_frame->skb)
605 kfree_skb(dev->read_frame->skb);
606 kfree(dev->read_frame);
607 dev->read_frame = NULL;
609 if (dev->write_queue) {
610 frame = dev->write_queue;
611 #ifdef FREE_ON_ERROR
612 dev->write_queue = dev->write_queue->next;
614 if (frame->skb) {
615 dev_kfree_skb(frame->skb);
617 kfree(frame);
618 #else
619 frame->copied = 0;
620 #endif
622 dev->rcv_seq = dev->send_seq = dev->unack_seq = 0;
623 dev->free = 511;
624 dev->l2_state = L2_ERROR;
626 /* this is an hack... */
627 pcbit_firmware_bug(dev);
629 dev->writeptr = dev->sh_mem;
630 dev->readptr = dev->sh_mem + BANK2;
632 writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
633 dev->sh_mem + BANK4);
634 dev->w_busy = dev->r_busy = 0;
638 static void
639 pcbit_l2_error(struct pcbit_dev *dev)
641 if (dev->l2_state == L2_RUNNING) {
643 printk(KERN_INFO "pcbit: layer 2 error\n");
645 #ifdef DEBUG
646 log_state(dev);
647 #endif
649 dev->l2_state = L2_DOWN;
651 init_timer(&dev->error_recover_timer);
652 dev->error_recover_timer.function = &pcbit_l2_err_recover;
653 dev->error_recover_timer.data = (ulong) dev;
654 dev->error_recover_timer.expires = jiffies + ERRTIME;
655 add_timer(&dev->error_recover_timer);
660 * Description:
661 * if board acks frames
662 * update dev->free
663 * call pcbit_transmit to write possible queued frames
666 static void
667 pcbit_recv_ack(struct pcbit_dev *dev, unsigned char ack)
669 int i,
670 count;
671 int unacked;
673 unacked = (dev->send_seq + (8 - dev->unack_seq)) & 0x07;
675 /* dev->unack_seq < ack <= dev->send_seq; */
677 if (unacked) {
679 if (dev->send_seq > dev->unack_seq) {
680 if (ack <= dev->unack_seq || ack > dev->send_seq) {
681 printk(KERN_DEBUG
682 "layer 2 ack unacceptable - dev %d",
683 dev->id);
685 pcbit_l2_error(dev);
686 } else if (ack > dev->send_seq && ack <= dev->unack_seq) {
687 printk(KERN_DEBUG
688 "layer 2 ack unacceptable - dev %d",
689 dev->id);
690 pcbit_l2_error(dev);
693 /* ack is acceptable */
696 i = dev->unack_seq;
698 do {
699 dev->unack_seq = i = (i + 1) % 8;
700 dev->free += dev->fsize[i];
701 } while (i != ack);
703 count = 0;
704 while (count < 7 && dev->write_queue) {
705 u8 lsend_seq = dev->send_seq;
707 pcbit_transmit(dev);
709 if (dev->send_seq == lsend_seq)
710 break;
711 count++;
713 } else
714 printk(KERN_DEBUG "recv_ack: unacked = 0\n");