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/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>
35 #include <linux/skbuff.h>
37 #include <linux/isdnif.h>
39 #include <asm/system.h>
58 * Layer 3 packet demultiplexer
62 extern void pcbit_l3_receive(struct pcbit_dev
*dev
, ulong msg
,
64 ushort hdr_len
, ushort refnum
);
70 static void pcbit_transmit(struct pcbit_dev
*dev
);
72 static void pcbit_recv_ack(struct pcbit_dev
*dev
, unsigned char ack
);
74 static void pcbit_l2_error(struct pcbit_dev
*dev
);
75 static void pcbit_l2_active_conf(struct pcbit_dev
*dev
, u_char info
);
76 static void pcbit_l2_err_recover(unsigned long data
);
78 static void pcbit_firmware_bug(struct pcbit_dev
*dev
);
80 static __inline__
void
81 pcbit_sched_delivery(struct pcbit_dev
*dev
)
83 schedule_work(&dev
->qdelivery
);
92 pcbit_l2_write(struct pcbit_dev
*dev
, ulong msg
, ushort refnum
,
93 struct sk_buff
*skb
, unsigned short hdr_len
)
95 struct frame_buf
*frame
,
99 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
) {
103 if ((frame
= (struct frame_buf
*) kmalloc(sizeof(struct frame_buf
),
104 GFP_ATOMIC
)) == NULL
) {
105 printk(KERN_WARNING
"pcbit_2_write: kmalloc failed\n");
110 frame
->refnum
= refnum
;
112 frame
->hdr_len
= hdr_len
;
115 frame
->dt_len
= skb
->len
- hdr_len
;
123 spin_lock_irqsave(&dev
->lock
, flags
);
125 if (dev
->write_queue
== NULL
) {
126 dev
->write_queue
= frame
;
127 spin_unlock_irqrestore(&dev
->lock
, flags
);
130 for (ptr
= dev
->write_queue
; ptr
->next
; ptr
= ptr
->next
);
133 spin_unlock_irqrestore(&dev
->lock
, flags
);
138 static __inline__
void
139 pcbit_tx_update(struct pcbit_dev
*dev
, ushort len
)
143 dev
->send_seq
= (dev
->send_seq
+ 1) % 8;
145 dev
->fsize
[dev
->send_seq
] = len
;
147 info
|= dev
->rcv_seq
<< 3;
148 info
|= dev
->send_seq
;
150 writeb(info
, dev
->sh_mem
+ BANK4
);
155 * called by interrupt service routine or by write_2
159 pcbit_transmit(struct pcbit_dev
*dev
)
161 struct frame_buf
*frame
= NULL
;
162 unsigned char unacked
;
163 int flen
; /* fragment frame length including all headers */
170 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
)
173 unacked
= (dev
->send_seq
+ (8 - dev
->unack_seq
)) & 0x07;
175 spin_lock_irqsave(&dev
->lock
, flags
);
177 if (dev
->free
> 16 && dev
->write_queue
&& unacked
< 7) {
182 spin_unlock_irqrestore(&dev
->lock
, flags
);
187 frame
= dev
->write_queue
;
190 spin_unlock_irqrestore(&dev
->lock
, flags
);
192 if (frame
->copied
== 0) {
199 flen
= FRAME_HDR_LEN
+ PREHDR_LEN
+ frame
->skb
->len
;
201 flen
= FRAME_HDR_LEN
+ PREHDR_LEN
;
209 * Board level 2 header
212 pcbit_writew(dev
, flen
- FRAME_HDR_LEN
);
214 pcbit_writeb(dev
, GET_MSG_CPU(msg
));
216 pcbit_writeb(dev
, GET_MSG_PROC(msg
));
219 pcbit_writew(dev
, frame
->hdr_len
+ PREHDR_LEN
);
222 pcbit_writew(dev
, frame
->dt_len
);
226 * Board level 3 fixed-header
230 pcbit_writew(dev
, frame
->hdr_len
+ PREHDR_LEN
);
233 pcbit_writew(dev
, 0);
236 pcbit_writeb(dev
, GET_MSG_CMD(msg
));
237 pcbit_writeb(dev
, GET_MSG_SCMD(msg
));
240 pcbit_writew(dev
, frame
->refnum
);
242 count
= FRAME_HDR_LEN
+ PREHDR_LEN
;
246 flen
= 2 + (frame
->skb
->len
- frame
->copied
);
252 tt
= ((ushort
) (flen
- 2)) | 0x8000U
; /* Type 1 */
253 pcbit_writew(dev
, tt
);
259 cp_len
= frame
->skb
->len
- frame
->copied
;
260 if (cp_len
> flen
- count
)
261 cp_len
= flen
- count
;
263 memcpy_topcbit(dev
, frame
->skb
->data
+ frame
->copied
,
265 frame
->copied
+= cp_len
;
269 pcbit_tx_update(dev
, flen
);
271 spin_lock_irqsave(&dev
->lock
, flags
);
273 if (frame
->skb
== NULL
|| frame
->copied
== frame
->skb
->len
) {
275 dev
->write_queue
= frame
->next
;
277 if (frame
->skb
!= NULL
) {
279 dev_kfree_skb(frame
->skb
);
284 spin_unlock_irqrestore(&dev
->lock
, flags
);
286 spin_unlock_irqrestore(&dev
->lock
, flags
);
288 printk(KERN_DEBUG
"unacked %d free %d write_queue %s\n",
289 unacked
, dev
->free
, dev
->write_queue
? "not empty" :
297 * deliver a queued frame to the upper layer
301 pcbit_deliver(struct work_struct
*work
)
303 struct frame_buf
*frame
;
304 unsigned long flags
, msg
;
305 struct pcbit_dev
*dev
=
306 container_of(work
, struct pcbit_dev
, qdelivery
);
308 spin_lock_irqsave(&dev
->lock
, flags
);
310 while ((frame
= dev
->read_queue
)) {
311 dev
->read_queue
= frame
->next
;
312 spin_unlock_irqrestore(&dev
->lock
, flags
);
316 SET_MSG_PROC(msg
, 0);
317 SET_MSG_CMD(msg
, frame
->skb
->data
[2]);
318 SET_MSG_SCMD(msg
, frame
->skb
->data
[3]);
320 frame
->refnum
= *((ushort
*) frame
->skb
->data
+ 4);
321 frame
->msg
= *((ulong
*) & msg
);
323 skb_pull(frame
->skb
, 6);
325 pcbit_l3_receive(dev
, frame
->msg
, frame
->skb
, frame
->hdr_len
,
330 spin_lock_irqsave(&dev
->lock
, flags
);
333 spin_unlock_irqrestore(&dev
->lock
, flags
);
337 * Reads BANK 2 & Reassembles
341 pcbit_receive(struct pcbit_dev
*dev
)
346 struct frame_buf
*frame
= NULL
;
350 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
)
353 tt
= pcbit_readw(dev
);
355 if ((tt
& 0x7fffU
) > 511) {
356 printk(KERN_INFO
"pcbit: invalid frame length -> TT=%04x\n",
361 if (!(tt
& 0x8000U
)) { /* Type 0 */
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
;
372 frame
= kmalloc(sizeof(struct frame_buf
), GFP_ATOMIC
);
375 printk(KERN_WARNING
"kmalloc failed\n");
378 memset(frame
, 0, sizeof(struct frame_buf
));
380 cpu
= pcbit_readb(dev
);
381 proc
= pcbit_readb(dev
);
384 if (cpu
!= 0x06 && cpu
!= 0x02) {
385 printk(KERN_DEBUG
"pcbit: invalid cpu value\n");
391 * we discard cpu & proc on receiving
392 * but we read it to update the pointer
395 frame
->hdr_len
= pcbit_readw(dev
);
396 frame
->dt_len
= pcbit_readw(dev
);
400 * I don't know if they are an error or not...
401 * But they are very frequent
405 if (frame
->hdr_len
== 0) {
408 printk(KERN_DEBUG
"0 sized frame\n");
410 pcbit_firmware_bug(dev
);
413 /* sanity check the length values */
414 if (frame
->hdr_len
> 1024 || frame
->dt_len
> 2048) {
416 printk(KERN_DEBUG
"length problem: ");
417 printk(KERN_DEBUG
"TH=%04x TD=%04x\n",
425 /* minimum frame read */
427 frame
->skb
= dev_alloc_skb(frame
->hdr_len
+ frame
->dt_len
+
428 ((frame
->hdr_len
+ 15) & ~15));
431 printk(KERN_DEBUG
"pcbit_receive: out of memory\n");
435 /* 16 byte alignment for IP */
437 skb_reserve(frame
->skb
, (frame
->hdr_len
+ 15) & ~15);
444 if (!(frame
= dev
->read_frame
)) {
445 printk("Type 1 frame and no frame queued\n");
446 /* usually after an error: toss frame */
448 if (dev
->readptr
> dev
->sh_mem
+ BANK2
+ BANKLEN
)
449 dev
->readptr
-= BANKLEN
;
455 memcpy_frompcbit(dev
, skb_put(frame
->skb
, tt
), tt
);
458 spin_lock_irqsave(&dev
->lock
, flags
);
459 if (frame
->copied
== frame
->hdr_len
+ frame
->dt_len
) {
462 dev
->read_frame
= NULL
;
464 if (dev
->read_queue
) {
465 struct frame_buf
*ptr
;
466 for (ptr
= dev
->read_queue
; ptr
->next
; ptr
= ptr
->next
);
469 dev
->read_queue
= frame
;
472 dev
->read_frame
= frame
;
474 spin_unlock_irqrestore(&dev
->lock
, flags
);
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
483 static __inline__
void
484 pcbit_fake_conf(struct pcbit_dev
*dev
, struct pcbit_chan
*chan
)
491 ictl
.driver
= dev
->id
;
492 ictl
.command
= ISDN_STAT_BSENT
;
494 dev
->dev_if
->statcallb(&ictl
);
499 pcbit_firmware_bug(struct pcbit_dev
*dev
)
501 struct pcbit_chan
*chan
;
505 if (chan
->fsm_state
== ST_ACTIVE
) {
506 pcbit_fake_conf(dev
, chan
);
510 if (chan
->fsm_state
== ST_ACTIVE
) {
511 pcbit_fake_conf(dev
, chan
);
516 pcbit_irq_handler(int interrupt
, void *devptr
)
518 struct pcbit_dev
*dev
;
523 dev
= (struct pcbit_dev
*) devptr
;
526 printk(KERN_WARNING
"pcbit_irq_handler: wrong device\n");
529 if (dev
->interrupt
) {
530 printk(KERN_DEBUG
"pcbit: reentering interrupt hander\n");
535 info
= readb(dev
->sh_mem
+ BANK3
);
537 if (dev
->l2_state
== L2_STARTING
|| dev
->l2_state
== L2_ERROR
) {
538 pcbit_l2_active_conf(dev
, info
);
542 if (info
& 0x40U
) { /* E bit set */
544 printk(KERN_DEBUG
"pcbit_irq_handler: E bit on\n");
550 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
) {
554 ack_seq
= (info
>> 3) & 0x07U
;
555 read_seq
= (info
& 0x07U
);
559 if (read_seq
!= dev
->rcv_seq
) {
560 while (read_seq
!= dev
->rcv_seq
) {
562 dev
->rcv_seq
= (dev
->rcv_seq
+ 1) % 8;
564 pcbit_sched_delivery(dev
);
566 if (ack_seq
!= dev
->unack_seq
) {
567 pcbit_recv_ack(dev
, ack_seq
);
569 info
= dev
->rcv_seq
<< 3;
570 info
|= dev
->send_seq
;
572 writeb(info
, dev
->sh_mem
+ BANK4
);
578 pcbit_l2_active_conf(struct pcbit_dev
*dev
, u_char info
)
582 state
= dev
->l2_state
;
585 printk(KERN_DEBUG
"layer2_active_confirm\n");
590 dev
->rcv_seq
= info
& 0x07U
;
591 dev
->l2_state
= L2_RUNNING
;
593 dev
->l2_state
= L2_DOWN
;
595 if (state
== L2_STARTING
)
596 wake_up_interruptible(&dev
->set_running_wq
);
598 if (state
== L2_ERROR
&& dev
->l2_state
== L2_RUNNING
) {
604 pcbit_l2_err_recover(unsigned long data
)
607 struct pcbit_dev
*dev
;
608 struct frame_buf
*frame
;
610 dev
= (struct pcbit_dev
*) data
;
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
);
619 dev
->w_busy
= dev
->r_busy
= 1;
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
;
627 if (dev
->write_queue
) {
628 frame
= dev
->write_queue
;
630 dev
->write_queue
= dev
->write_queue
->next
;
633 dev_kfree_skb(frame
->skb
);
640 dev
->rcv_seq
= dev
->send_seq
= dev
->unack_seq
= 0;
642 dev
->l2_state
= L2_ERROR
;
644 /* this is an hack... */
645 pcbit_firmware_bug(dev
);
647 dev
->writeptr
= dev
->sh_mem
;
648 dev
->readptr
= dev
->sh_mem
+ BANK2
;
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;
657 pcbit_l2_error(struct pcbit_dev
*dev
)
659 if (dev
->l2_state
== L2_RUNNING
) {
661 printk(KERN_INFO
"pcbit: layer 2 error\n");
667 dev
->l2_state
= L2_DOWN
;
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
);
679 * if board acks frames
681 * call pcbit_transmit to write possible queued frames
685 pcbit_recv_ack(struct pcbit_dev
*dev
, unsigned char ack
)
691 unacked
= (dev
->send_seq
+ (8 - dev
->unack_seq
)) & 0x07;
693 /* dev->unack_seq < ack <= dev->send_seq; */
697 if (dev
->send_seq
> dev
->unack_seq
) {
698 if (ack
<= dev
->unack_seq
|| ack
> dev
->send_seq
) {
700 "layer 2 ack unacceptable - dev %d",
704 } else if (ack
> dev
->send_seq
&& ack
<= dev
->unack_seq
) {
706 "layer 2 ack unacceptable - dev %d",
711 /* ack is acceptable */
717 dev
->unack_seq
= i
= (i
+ 1) % 8;
718 dev
->free
+= dev
->fsize
[i
];
722 while (count
< 7 && dev
->write_queue
) {
723 u8 lsend_seq
= dev
->send_seq
;
727 if (dev
->send_seq
== lsend_seq
)
732 printk(KERN_DEBUG
"recv_ack: unacked = 0\n");