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>
35 #include <linux/skbuff.h>
37 #include <linux/isdnif.h>
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
);
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
,
82 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
) {
86 if ((frame
= kmalloc(sizeof(struct frame_buf
),
87 GFP_ATOMIC
)) == NULL
) {
92 frame
->refnum
= refnum
;
94 frame
->hdr_len
= hdr_len
;
97 frame
->dt_len
= skb
->len
- hdr_len
;
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
);
112 for (ptr
= dev
->write_queue
; ptr
->next
; ptr
= ptr
->next
);
115 spin_unlock_irqrestore(&dev
->lock
, flags
);
120 static __inline__
void
121 pcbit_tx_update(struct pcbit_dev
*dev
, ushort len
)
125 dev
->send_seq
= (dev
->send_seq
+ 1) % 8;
127 dev
->fsize
[dev
->send_seq
] = len
;
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
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 */
152 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
)
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) {
164 spin_unlock_irqrestore(&dev
->lock
, flags
);
169 frame
= dev
->write_queue
;
172 spin_unlock_irqrestore(&dev
->lock
, flags
);
174 if (frame
->copied
== 0) {
181 flen
= FRAME_HDR_LEN
+ PREHDR_LEN
+ frame
->skb
->len
;
183 flen
= FRAME_HDR_LEN
+ PREHDR_LEN
;
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
));
201 pcbit_writew(dev
, frame
->hdr_len
+ PREHDR_LEN
);
204 pcbit_writew(dev
, frame
->dt_len
);
208 * Board level 3 fixed-header
212 pcbit_writew(dev
, frame
->hdr_len
+ PREHDR_LEN
);
215 pcbit_writew(dev
, 0);
218 pcbit_writeb(dev
, GET_MSG_CMD(msg
));
219 pcbit_writeb(dev
, GET_MSG_SCMD(msg
));
222 pcbit_writew(dev
, frame
->refnum
);
224 count
= FRAME_HDR_LEN
+ PREHDR_LEN
;
228 flen
= 2 + (frame
->skb
->len
- frame
->copied
);
234 tt
= ((ushort
) (flen
- 2)) | 0x8000U
; /* Type 1 */
235 pcbit_writew(dev
, tt
);
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
,
247 frame
->copied
+= cp_len
;
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
) {
261 dev_kfree_skb(frame
->skb
);
266 spin_unlock_irqrestore(&dev
->lock
, flags
);
268 spin_unlock_irqrestore(&dev
->lock
, flags
);
270 printk(KERN_DEBUG
"unacked %d free %d write_queue %s\n",
271 unacked
, dev
->free
, dev
->write_queue
? "not empty" :
279 * deliver a queued frame to the upper layer
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
);
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
,
312 spin_lock_irqsave(&dev
->lock
, flags
);
315 spin_unlock_irqrestore(&dev
->lock
, flags
);
319 * Reads BANK 2 & Reassembles
323 pcbit_receive(struct pcbit_dev
*dev
)
328 struct frame_buf
*frame
= NULL
;
332 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
)
335 tt
= pcbit_readw(dev
);
337 if ((tt
& 0x7fffU
) > 511) {
338 printk(KERN_INFO
"pcbit: invalid frame length -> TT=%04x\n",
343 if (!(tt
& 0x8000U
)) { /* Type 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
);
356 printk(KERN_WARNING
"kmalloc failed\n");
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");
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
);
380 * I don't know if they are an error or not...
381 * But they are very frequent
385 if (frame
->hdr_len
== 0) {
388 printk(KERN_DEBUG
"0 sized frame\n");
390 pcbit_firmware_bug(dev
);
393 /* sanity check the length values */
394 if (frame
->hdr_len
> 1024 || frame
->dt_len
> 2048) {
396 printk(KERN_DEBUG
"length problem: ");
397 printk(KERN_DEBUG
"TH=%04x TD=%04x\n",
405 /* minimum frame read */
407 frame
->skb
= dev_alloc_skb(frame
->hdr_len
+ frame
->dt_len
+
408 ((frame
->hdr_len
+ 15) & ~15));
411 printk(KERN_DEBUG
"pcbit_receive: out of memory\n");
415 /* 16 byte alignment for IP */
417 skb_reserve(frame
->skb
, (frame
->hdr_len
+ 15) & ~15);
424 if (!(frame
= dev
->read_frame
)) {
425 printk("Type 1 frame and no frame queued\n");
426 /* usually after an error: toss frame */
428 if (dev
->readptr
> dev
->sh_mem
+ BANK2
+ BANKLEN
)
429 dev
->readptr
-= BANKLEN
;
435 memcpy_frompcbit(dev
, skb_put(frame
->skb
, tt
), tt
);
438 spin_lock_irqsave(&dev
->lock
, flags
);
439 if (frame
->copied
== frame
->hdr_len
+ frame
->dt_len
) {
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
);
449 dev
->read_queue
= frame
;
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
)
471 ictl
.driver
= dev
->id
;
472 ictl
.command
= ISDN_STAT_BSENT
;
474 dev
->dev_if
->statcallb(&ictl
);
479 pcbit_firmware_bug(struct pcbit_dev
*dev
)
481 struct pcbit_chan
*chan
;
485 if (chan
->fsm_state
== ST_ACTIVE
) {
486 pcbit_fake_conf(dev
, chan
);
490 if (chan
->fsm_state
== ST_ACTIVE
) {
491 pcbit_fake_conf(dev
, chan
);
496 pcbit_irq_handler(int interrupt
, void *devptr
)
498 struct pcbit_dev
*dev
;
503 dev
= (struct pcbit_dev
*) devptr
;
506 printk(KERN_WARNING
"pcbit_irq_handler: wrong device\n");
509 if (dev
->interrupt
) {
510 printk(KERN_DEBUG
"pcbit: reentering interrupt handler\n");
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
);
522 if (info
& 0x40U
) { /* E bit set */
524 printk(KERN_DEBUG
"pcbit_irq_handler: E bit on\n");
530 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
) {
534 ack_seq
= (info
>> 3) & 0x07U
;
535 read_seq
= (info
& 0x07U
);
539 if (read_seq
!= dev
->rcv_seq
) {
540 while (read_seq
!= dev
->rcv_seq
) {
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
);
558 pcbit_l2_active_conf(struct pcbit_dev
*dev
, u_char info
)
562 state
= dev
->l2_state
;
565 printk(KERN_DEBUG
"layer2_active_confirm\n");
570 dev
->rcv_seq
= info
& 0x07U
;
571 dev
->l2_state
= L2_RUNNING
;
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
) {
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
);
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
;
609 dev
->write_queue
= dev
->write_queue
->next
;
612 dev_kfree_skb(frame
->skb
);
619 dev
->rcv_seq
= dev
->send_seq
= dev
->unack_seq
= 0;
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;
636 pcbit_l2_error(struct pcbit_dev
*dev
)
638 if (dev
->l2_state
== L2_RUNNING
) {
640 printk(KERN_INFO
"pcbit: layer 2 error\n");
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
);
658 * if board acks frames
660 * call pcbit_transmit to write possible queued frames
664 pcbit_recv_ack(struct pcbit_dev
*dev
, unsigned char ack
)
670 unacked
= (dev
->send_seq
+ (8 - dev
->unack_seq
)) & 0x07;
672 /* dev->unack_seq < ack <= dev->send_seq; */
676 if (dev
->send_seq
> dev
->unack_seq
) {
677 if (ack
<= dev
->unack_seq
|| ack
> dev
->send_seq
) {
679 "layer 2 ack unacceptable - dev %d",
683 } else if (ack
> dev
->send_seq
&& ack
<= dev
->unack_seq
) {
685 "layer 2 ack unacceptable - dev %d",
690 /* ack is acceptable */
696 dev
->unack_seq
= i
= (i
+ 1) % 8;
697 dev
->free
+= dev
->fsize
[i
];
701 while (count
< 7 && dev
->write_queue
) {
702 u8 lsend_seq
= dev
->send_seq
;
706 if (dev
->send_seq
== lsend_seq
)
711 printk(KERN_DEBUG
"recv_ack: unacked = 0\n");