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
) {
88 printk(KERN_WARNING
"pcbit_2_write: kmalloc failed\n");
93 frame
->refnum
= refnum
;
95 frame
->hdr_len
= hdr_len
;
98 frame
->dt_len
= skb
->len
- hdr_len
;
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
);
113 for (ptr
= dev
->write_queue
; ptr
->next
; ptr
= ptr
->next
);
116 spin_unlock_irqrestore(&dev
->lock
, flags
);
121 static __inline__
void
122 pcbit_tx_update(struct pcbit_dev
*dev
, ushort len
)
126 dev
->send_seq
= (dev
->send_seq
+ 1) % 8;
128 dev
->fsize
[dev
->send_seq
] = len
;
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
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 */
153 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
)
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) {
165 spin_unlock_irqrestore(&dev
->lock
, flags
);
170 frame
= dev
->write_queue
;
173 spin_unlock_irqrestore(&dev
->lock
, flags
);
175 if (frame
->copied
== 0) {
182 flen
= FRAME_HDR_LEN
+ PREHDR_LEN
+ frame
->skb
->len
;
184 flen
= FRAME_HDR_LEN
+ PREHDR_LEN
;
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
));
202 pcbit_writew(dev
, frame
->hdr_len
+ PREHDR_LEN
);
205 pcbit_writew(dev
, frame
->dt_len
);
209 * Board level 3 fixed-header
213 pcbit_writew(dev
, frame
->hdr_len
+ PREHDR_LEN
);
216 pcbit_writew(dev
, 0);
219 pcbit_writeb(dev
, GET_MSG_CMD(msg
));
220 pcbit_writeb(dev
, GET_MSG_SCMD(msg
));
223 pcbit_writew(dev
, frame
->refnum
);
225 count
= FRAME_HDR_LEN
+ PREHDR_LEN
;
229 flen
= 2 + (frame
->skb
->len
- frame
->copied
);
235 tt
= ((ushort
) (flen
- 2)) | 0x8000U
; /* Type 1 */
236 pcbit_writew(dev
, tt
);
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
,
248 frame
->copied
+= cp_len
;
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
) {
262 dev_kfree_skb(frame
->skb
);
267 spin_unlock_irqrestore(&dev
->lock
, flags
);
269 spin_unlock_irqrestore(&dev
->lock
, flags
);
271 printk(KERN_DEBUG
"unacked %d free %d write_queue %s\n",
272 unacked
, dev
->free
, dev
->write_queue
? "not empty" :
280 * deliver a queued frame to the upper layer
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
);
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
,
313 spin_lock_irqsave(&dev
->lock
, flags
);
316 spin_unlock_irqrestore(&dev
->lock
, flags
);
320 * Reads BANK 2 & Reassembles
324 pcbit_receive(struct pcbit_dev
*dev
)
329 struct frame_buf
*frame
= NULL
;
333 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
)
336 tt
= pcbit_readw(dev
);
338 if ((tt
& 0x7fffU
) > 511) {
339 printk(KERN_INFO
"pcbit: invalid frame length -> TT=%04x\n",
344 if (!(tt
& 0x8000U
)) { /* Type 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 kfree_skb(dev
->read_frame
->skb
);
351 kfree(dev
->read_frame
);
352 dev
->read_frame
= NULL
;
354 frame
= kzalloc(sizeof(struct frame_buf
), GFP_ATOMIC
);
357 printk(KERN_WARNING
"kmalloc failed\n");
361 cpu
= pcbit_readb(dev
);
362 proc
= pcbit_readb(dev
);
365 if (cpu
!= 0x06 && cpu
!= 0x02) {
366 printk(KERN_DEBUG
"pcbit: invalid cpu value\n");
372 * we discard cpu & proc on receiving
373 * but we read it to update the pointer
376 frame
->hdr_len
= pcbit_readw(dev
);
377 frame
->dt_len
= pcbit_readw(dev
);
381 * I don't know if they are an error or not...
382 * But they are very frequent
386 if (frame
->hdr_len
== 0) {
389 printk(KERN_DEBUG
"0 sized frame\n");
391 pcbit_firmware_bug(dev
);
394 /* sanity check the length values */
395 if (frame
->hdr_len
> 1024 || frame
->dt_len
> 2048) {
397 printk(KERN_DEBUG
"length problem: ");
398 printk(KERN_DEBUG
"TH=%04x TD=%04x\n",
406 /* minimum frame read */
408 frame
->skb
= dev_alloc_skb(frame
->hdr_len
+ frame
->dt_len
+
409 ((frame
->hdr_len
+ 15) & ~15));
412 printk(KERN_DEBUG
"pcbit_receive: out of memory\n");
416 /* 16 byte alignment for IP */
418 skb_reserve(frame
->skb
, (frame
->hdr_len
+ 15) & ~15);
425 if (!(frame
= dev
->read_frame
)) {
426 printk("Type 1 frame and no frame queued\n");
427 /* usually after an error: toss frame */
429 if (dev
->readptr
> dev
->sh_mem
+ BANK2
+ BANKLEN
)
430 dev
->readptr
-= BANKLEN
;
436 memcpy_frompcbit(dev
, skb_put(frame
->skb
, tt
), tt
);
439 spin_lock_irqsave(&dev
->lock
, flags
);
440 if (frame
->copied
== frame
->hdr_len
+ frame
->dt_len
) {
443 dev
->read_frame
= NULL
;
445 if (dev
->read_queue
) {
446 struct frame_buf
*ptr
;
447 for (ptr
= dev
->read_queue
; ptr
->next
; ptr
= ptr
->next
);
450 dev
->read_queue
= frame
;
453 dev
->read_frame
= frame
;
455 spin_unlock_irqrestore(&dev
->lock
, flags
);
459 * The board sends 0 sized frames
460 * They are TDATA_CONFs that get messed up somehow
461 * gotta send a fake acknowledgment to the upper layer somehow
464 static __inline__
void
465 pcbit_fake_conf(struct pcbit_dev
*dev
, struct pcbit_chan
*chan
)
472 ictl
.driver
= dev
->id
;
473 ictl
.command
= ISDN_STAT_BSENT
;
475 dev
->dev_if
->statcallb(&ictl
);
480 pcbit_firmware_bug(struct pcbit_dev
*dev
)
482 struct pcbit_chan
*chan
;
486 if (chan
->fsm_state
== ST_ACTIVE
) {
487 pcbit_fake_conf(dev
, chan
);
491 if (chan
->fsm_state
== ST_ACTIVE
) {
492 pcbit_fake_conf(dev
, chan
);
497 pcbit_irq_handler(int interrupt
, void *devptr
)
499 struct pcbit_dev
*dev
;
504 dev
= (struct pcbit_dev
*) devptr
;
507 printk(KERN_WARNING
"pcbit_irq_handler: wrong device\n");
510 if (dev
->interrupt
) {
511 printk(KERN_DEBUG
"pcbit: reentering interrupt handler\n");
516 info
= readb(dev
->sh_mem
+ BANK3
);
518 if (dev
->l2_state
== L2_STARTING
|| dev
->l2_state
== L2_ERROR
) {
519 pcbit_l2_active_conf(dev
, info
);
523 if (info
& 0x40U
) { /* E bit set */
525 printk(KERN_DEBUG
"pcbit_irq_handler: E bit on\n");
531 if (dev
->l2_state
!= L2_RUNNING
&& dev
->l2_state
!= L2_LOADING
) {
535 ack_seq
= (info
>> 3) & 0x07U
;
536 read_seq
= (info
& 0x07U
);
540 if (read_seq
!= dev
->rcv_seq
) {
541 while (read_seq
!= dev
->rcv_seq
) {
543 dev
->rcv_seq
= (dev
->rcv_seq
+ 1) % 8;
545 pcbit_sched_delivery(dev
);
547 if (ack_seq
!= dev
->unack_seq
) {
548 pcbit_recv_ack(dev
, ack_seq
);
550 info
= dev
->rcv_seq
<< 3;
551 info
|= dev
->send_seq
;
553 writeb(info
, dev
->sh_mem
+ BANK4
);
559 pcbit_l2_active_conf(struct pcbit_dev
*dev
, u_char info
)
563 state
= dev
->l2_state
;
566 printk(KERN_DEBUG
"layer2_active_confirm\n");
571 dev
->rcv_seq
= info
& 0x07U
;
572 dev
->l2_state
= L2_RUNNING
;
574 dev
->l2_state
= L2_DOWN
;
576 if (state
== L2_STARTING
)
577 wake_up_interruptible(&dev
->set_running_wq
);
579 if (state
== L2_ERROR
&& dev
->l2_state
== L2_RUNNING
) {
585 pcbit_l2_err_recover(unsigned long data
)
588 struct pcbit_dev
*dev
;
589 struct frame_buf
*frame
;
591 dev
= (struct pcbit_dev
*) data
;
593 del_timer(&dev
->error_recover_timer
);
594 if (dev
->w_busy
|| dev
->r_busy
) {
595 init_timer(&dev
->error_recover_timer
);
596 dev
->error_recover_timer
.expires
= jiffies
+ ERRTIME
;
597 add_timer(&dev
->error_recover_timer
);
600 dev
->w_busy
= dev
->r_busy
= 1;
602 if (dev
->read_frame
) {
603 kfree_skb(dev
->read_frame
->skb
);
604 kfree(dev
->read_frame
);
605 dev
->read_frame
= NULL
;
607 if (dev
->write_queue
) {
608 frame
= dev
->write_queue
;
610 dev
->write_queue
= dev
->write_queue
->next
;
613 dev_kfree_skb(frame
->skb
);
620 dev
->rcv_seq
= dev
->send_seq
= dev
->unack_seq
= 0;
622 dev
->l2_state
= L2_ERROR
;
624 /* this is an hack... */
625 pcbit_firmware_bug(dev
);
627 dev
->writeptr
= dev
->sh_mem
;
628 dev
->readptr
= dev
->sh_mem
+ BANK2
;
630 writeb((0x80U
| ((dev
->rcv_seq
& 0x07) << 3) | (dev
->send_seq
& 0x07)),
631 dev
->sh_mem
+ BANK4
);
632 dev
->w_busy
= dev
->r_busy
= 0;
637 pcbit_l2_error(struct pcbit_dev
*dev
)
639 if (dev
->l2_state
== L2_RUNNING
) {
641 printk(KERN_INFO
"pcbit: layer 2 error\n");
647 dev
->l2_state
= L2_DOWN
;
649 init_timer(&dev
->error_recover_timer
);
650 dev
->error_recover_timer
.function
= &pcbit_l2_err_recover
;
651 dev
->error_recover_timer
.data
= (ulong
) dev
;
652 dev
->error_recover_timer
.expires
= jiffies
+ ERRTIME
;
653 add_timer(&dev
->error_recover_timer
);
659 * if board acks frames
661 * call pcbit_transmit to write possible queued frames
665 pcbit_recv_ack(struct pcbit_dev
*dev
, unsigned char ack
)
671 unacked
= (dev
->send_seq
+ (8 - dev
->unack_seq
)) & 0x07;
673 /* dev->unack_seq < ack <= dev->send_seq; */
677 if (dev
->send_seq
> dev
->unack_seq
) {
678 if (ack
<= dev
->unack_seq
|| ack
> dev
->send_seq
) {
680 "layer 2 ack unacceptable - dev %d",
684 } else if (ack
> dev
->send_seq
&& ack
<= dev
->unack_seq
) {
686 "layer 2 ack unacceptable - dev %d",
691 /* ack is acceptable */
697 dev
->unack_seq
= i
= (i
+ 1) % 8;
698 dev
->free
+= dev
->fsize
[i
];
702 while (count
< 7 && dev
->write_queue
) {
703 u8 lsend_seq
= dev
->send_seq
;
707 if (dev
->send_seq
== lsend_seq
)
712 printk(KERN_DEBUG
"recv_ack: unacked = 0\n");