1 /*
2 * PCBIT-D low-layer interface
3 *
4 * Copyright (C) 1996 Universidade de Lisboa
5 *
6 * Written by Pedro Roque Marques (pedro_m@yahoo.com)
7 *
8 * This software may be used and distributed according to the terms of
9 * the GNU General Public License, incorporated herein by reference.
10 */
11
12 /*
13 * 19991203 - Fernando Carvalho - takion@superbofh.org
14 * Hacked to compile with egcs and run with current version of isdn modules
15 */
16
17 /*
18 * Based on documentation provided by Inesc:
19 * - "Interface com bus do PC para o PCBIT e PCBIT-D", Inesc, Jan 93
20 */
21
22 /*
23 * TODO: better handling of errors
24 * re-write/remove debug printks
25 */
26
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/tqueue.h>
34 #include <linux/mm.h>
35 #include <linux/skbuff.h>
36
37 #include <linux/isdnif.h>
38
39 #include <asm/system.h>
40 #include <asm/io.h>
41
42
43 #include "pcbit.h"
44 #include "layer2.h"
45 #include "edss1.h"
46
47 #undef DEBUG_FRAG
48
49
50
51 /*
52 * task queue struct
53 */
54
55
56
57 /*
58 * Layer 3 packet demultiplexer
59 * drv.c
60 */
61
62 extern void pcbit_l3_receive(struct pcbit_dev *dev, ulong msg,
63 struct sk_buff *skb,
64 ushort hdr_len, ushort refnum);
65
66 /*
67 * Prototypes
68 */
69
70 void pcbit_deliver(void *data);
71 static void pcbit_transmit(struct pcbit_dev *dev);
72
73 static void pcbit_recv_ack(struct pcbit_dev *dev, unsigned char ack);
74
75 static void pcbit_l2_error(struct pcbit_dev *dev);
76 static void pcbit_l2_active_conf(struct pcbit_dev *dev, u_char info);
77 static void pcbit_l2_err_recover(unsigned long data);
78
79 static void pcbit_firmware_bug(struct pcbit_dev *dev);
80
81 static __inline__ void
pcbit_sched_delivery(struct pcbit_dev * dev)82 pcbit_sched_delivery(struct pcbit_dev *dev)
83 {
84 queue_task(&dev->qdelivery, &tq_immediate);
85 mark_bh(IMMEDIATE_BH);
86 }
87
88
89 /*
90 * Called from layer3
91 */
92
93 int
pcbit_l2_write(struct pcbit_dev * dev,ulong msg,ushort refnum,struct sk_buff * skb,unsigned short hdr_len)94 pcbit_l2_write(struct pcbit_dev *dev, ulong msg, ushort refnum,
95 struct sk_buff *skb, unsigned short hdr_len)
96 {
97 struct frame_buf *frame,
98 *ptr;
99 unsigned long flags;
100
101 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING) {
102 dev_kfree_skb(skb);
103 return -1;
104 }
105 if ((frame = (struct frame_buf *) kmalloc(sizeof(struct frame_buf),
106 GFP_ATOMIC)) == NULL) {
107 printk(KERN_WARNING "pcbit_2_write: kmalloc failed\n");
108 dev_kfree_skb(skb);
109 return -1;
110 }
111 frame->msg = msg;
112 frame->refnum = refnum;
113 frame->copied = 0;
114 frame->hdr_len = hdr_len;
115
116 if (skb)
117 frame->dt_len = skb->len - hdr_len;
118 else
119 frame->dt_len = 0;
120
121 frame->skb = skb;
122
123 frame->next = NULL;
124
125 save_flags(flags);
126 cli();
127
128 if (dev->write_queue == NULL) {
129 dev->write_queue = frame;
130 restore_flags(flags);
131 pcbit_transmit(dev);
132 } else {
133 for (ptr = dev->write_queue; ptr->next; ptr = ptr->next);
134 ptr->next = frame;
135
136 restore_flags(flags);
137 }
138 return 0;
139 }
140
141 static __inline__ void
pcbit_tx_update(struct pcbit_dev * dev,ushort len)142 pcbit_tx_update(struct pcbit_dev *dev, ushort len)
143 {
144 u_char info;
145
146 dev->send_seq = (dev->send_seq + 1) % 8;
147
148 dev->fsize[dev->send_seq] = len;
149 info = 0;
150 info |= dev->rcv_seq << 3;
151 info |= dev->send_seq;
152
153 writeb(info, dev->sh_mem + BANK4);
154
155 }
156
157 /*
158 * called by interrupt service routine or by write_2
159 */
160
161 static void
pcbit_transmit(struct pcbit_dev * dev)162 pcbit_transmit(struct pcbit_dev *dev)
163 {
164 struct frame_buf *frame = NULL;
165 unsigned char unacked;
166 int flen; /* fragment frame length including all headers */
167 int free;
168 int count,
169 cp_len;
170 unsigned long flags;
171 unsigned short tt;
172
173 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING)
174 return;
175
176 unacked = (dev->send_seq + (8 - dev->unack_seq)) & 0x07;
177
178 save_flags(flags);
179 cli();
180
181 if (dev->free > 16 && dev->write_queue && unacked < 7) {
182
183 if (!dev->w_busy)
184 dev->w_busy = 1;
185 else {
186 restore_flags(flags);
187 return;
188 }
189
190
191 frame = dev->write_queue;
192 free = dev->free;
193
194 restore_flags(flags);
195
196 if (frame->copied == 0) {
197
198 /* Type 0 frame */
199
200 ulong msg;
201
202 if (frame->skb)
203 flen = FRAME_HDR_LEN + PREHDR_LEN + frame->skb->len;
204 else
205 flen = FRAME_HDR_LEN + PREHDR_LEN;
206
207 if (flen > free)
208 flen = free;
209
210 msg = frame->msg;
211
212 /*
213 * Board level 2 header
214 */
215
216 pcbit_writew(dev, flen - FRAME_HDR_LEN);
217
218 pcbit_writeb(dev, GET_MSG_CPU(msg));
219
220 pcbit_writeb(dev, GET_MSG_PROC(msg));
221
222 /* TH */
223 pcbit_writew(dev, frame->hdr_len + PREHDR_LEN);
224
225 /* TD */
226 pcbit_writew(dev, frame->dt_len);
227
228
229 /*
230 * Board level 3 fixed-header
231 */
232
233 /* LEN = TH */
234 pcbit_writew(dev, frame->hdr_len + PREHDR_LEN);
235
236 /* XX */
237 pcbit_writew(dev, 0);
238
239 /* C + S */
240 pcbit_writeb(dev, GET_MSG_CMD(msg));
241 pcbit_writeb(dev, GET_MSG_SCMD(msg));
242
243 /* NUM */
244 pcbit_writew(dev, frame->refnum);
245
246 count = FRAME_HDR_LEN + PREHDR_LEN;
247 } else {
248 /* Type 1 frame */
249
250 flen = 2 + (frame->skb->len - frame->copied);
251
252 if (flen > free)
253 flen = free;
254
255 /* TT */
256 tt = ((ushort) (flen - 2)) | 0x8000U; /* Type 1 */
257 pcbit_writew(dev, tt);
258
259 count = 2;
260 }
261
262 if (frame->skb) {
263 cp_len = frame->skb->len - frame->copied;
264 if (cp_len > flen - count)
265 cp_len = flen - count;
266
267 memcpy_topcbit(dev, frame->skb->data + frame->copied,
268 cp_len);
269 frame->copied += cp_len;
270 }
271 /* bookkeeping */
272 dev->free -= flen;
273 pcbit_tx_update(dev, flen);
274
275 save_flags(flags);
276 cli();
277
278
279 if (frame->skb == NULL || frame->copied == frame->skb->len) {
280
281 dev->write_queue = frame->next;
282
283 if (frame->skb != NULL) {
284 /* free frame */
285 dev_kfree_skb(frame->skb);
286 }
287 kfree(frame);
288 }
289 dev->w_busy = 0;
290 restore_flags(flags);
291 } else {
292 restore_flags(flags);
293 #ifdef DEBUG
294 printk(KERN_DEBUG "unacked %d free %d write_queue %s\n",
295 unacked, dev->free, dev->write_queue ? "not empty" :
296 "empty");
297 #endif
298 }
299 }
300
301
302 /*
303 * deliver a queued frame to the upper layer
304 */
305
306 void
pcbit_deliver(void * data)307 pcbit_deliver(void *data)
308 {
309 struct frame_buf *frame;
310 unsigned long flags, msg;
311 struct pcbit_dev *dev = (struct pcbit_dev *) data;
312
313 save_flags(flags);
314 cli();
315
316 while ((frame = dev->read_queue)) {
317 dev->read_queue = frame->next;
318 restore_flags(flags);
319
320 SET_MSG_CPU(msg, 0);
321 SET_MSG_PROC(msg, 0);
322 SET_MSG_CMD(msg, frame->skb->data[2]);
323 SET_MSG_SCMD(msg, frame->skb->data[3]);
324
325 frame->refnum = *((ushort *) frame->skb->data + 4);
326 frame->msg = *((ulong *) & msg);
327
328 skb_pull(frame->skb, 6);
329
330 pcbit_l3_receive(dev, frame->msg, frame->skb, frame->hdr_len,
331 frame->refnum);
332
333 kfree(frame);
334
335 save_flags(flags);
336 cli();
337 }
338
339 restore_flags(flags);
340 }
341
342 /*
343 * Reads BANK 2 & Reassembles
344 */
345
346 static void
pcbit_receive(struct pcbit_dev * dev)347 pcbit_receive(struct pcbit_dev *dev)
348 {
349 unsigned short tt;
350 u_char cpu,
351 proc;
352 struct frame_buf *frame = NULL;
353 unsigned long flags;
354 u_char type1;
355
356 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING)
357 return;
358
359 tt = pcbit_readw(dev);
360
361 if ((tt & 0x7fffU) > 511) {
362 printk(KERN_INFO "pcbit: invalid frame length -> TT=%04x\n",
363 tt);
364 pcbit_l2_error(dev);
365 return;
366 }
367 if (!(tt & 0x8000U)) { /* Type 0 */
368 type1 = 0;
369
370 if (dev->read_frame) {
371 printk(KERN_DEBUG "pcbit_receive: Type 0 frame and read_frame != NULL\n");
372 /* discard previous queued frame */
373 if (dev->read_frame->skb)
374 kfree_skb(dev->read_frame->skb);
375 kfree(dev->read_frame);
376 dev->read_frame = NULL;
377 }
378 frame = kmalloc(sizeof(struct frame_buf), GFP_ATOMIC);
379
380 if (frame == NULL) {
381 printk(KERN_WARNING "kmalloc failed\n");
382 return;
383 }
384 memset(frame, 0, sizeof(struct frame_buf));
385
386 cpu = pcbit_readb(dev);
387 proc = pcbit_readb(dev);
388
389
390 if (cpu != 0x06 && cpu != 0x02) {
391 printk(KERN_DEBUG "pcbit: invalid cpu value\n");
392 kfree(frame);
393 pcbit_l2_error(dev);
394 return;
395 }
396 /*
397 * we discard cpu & proc on receiving
398 * but we read it to update the pointer
399 */
400
401 frame->hdr_len = pcbit_readw(dev);
402 frame->dt_len = pcbit_readw(dev);
403
404 /*
405 * 0 sized packet
406 * I don't know if they are an error or not...
407 * But they are very frequent
408 * Not documented
409 */
410
411 if (frame->hdr_len == 0) {
412 kfree(frame);
413 #ifdef DEBUG
414 printk(KERN_DEBUG "0 sized frame\n");
415 #endif
416 pcbit_firmware_bug(dev);
417 return;
418 }
419 /* sanity check the length values */
420 if (frame->hdr_len > 1024 || frame->dt_len > 2048) {
421 #ifdef DEBUG
422 printk(KERN_DEBUG "length problem: ");
423 printk(KERN_DEBUG "TH=%04x TD=%04x\n",
424 frame->hdr_len,
425 frame->dt_len);
426 #endif
427 pcbit_l2_error(dev);
428 kfree(frame);
429 return;
430 }
431 /* minimum frame read */
432
433 frame->skb = dev_alloc_skb(frame->hdr_len + frame->dt_len +
434 ((frame->hdr_len + 15) & ~15));
435
436 if (!frame->skb) {
437 printk(KERN_DEBUG "pcbit_receive: out of memory\n");
438 kfree(frame);
439 return;
440 }
441 /* 16 byte alignment for IP */
442 if (frame->dt_len)
443 skb_reserve(frame->skb, (frame->hdr_len + 15) & ~15);
444
445 } else {
446 /* Type 1 */
447 type1 = 1;
448 tt &= 0x7fffU;
449
450 if (!(frame = dev->read_frame)) {
451 printk("Type 1 frame and no frame queued\n");
452 /* usually after an error: toss frame */
453 dev->readptr += tt;
454 if (dev->readptr > dev->sh_mem + BANK2 + BANKLEN)
455 dev->readptr -= BANKLEN;
456 return;
457
458 }
459 }
460
461 memcpy_frompcbit(dev, skb_put(frame->skb, tt), tt);
462
463 frame->copied += tt;
464
465 if (frame->copied == frame->hdr_len + frame->dt_len) {
466
467 save_flags(flags);
468 cli();
469
470 if (type1) {
471 dev->read_frame = NULL;
472 }
473 if (dev->read_queue) {
474 struct frame_buf *ptr;
475 for (ptr = dev->read_queue; ptr->next; ptr = ptr->next);
476 ptr->next = frame;
477 } else
478 dev->read_queue = frame;
479
480 restore_flags(flags);
481
482 } else {
483 save_flags(flags);
484 cli();
485 dev->read_frame = frame;
486 restore_flags(flags);
487 }
488 }
489
490 /*
491 * The board sends 0 sized frames
492 * They are TDATA_CONFs that get messed up somehow
493 * gotta send a fake acknowledgment to the upper layer somehow
494 */
495
496 static __inline__ void
pcbit_fake_conf(struct pcbit_dev * dev,struct pcbit_chan * chan)497 pcbit_fake_conf(struct pcbit_dev *dev, struct pcbit_chan *chan)
498 {
499 isdn_ctrl ictl;
500
501 if (chan->queued) {
502 chan->queued--;
503
504 ictl.driver = dev->id;
505 ictl.command = ISDN_STAT_BSENT;
506 ictl.arg = chan->id;
507 dev->dev_if->statcallb(&ictl);
508 }
509 }
510
511 static void
pcbit_firmware_bug(struct pcbit_dev * dev)512 pcbit_firmware_bug(struct pcbit_dev *dev)
513 {
514 struct pcbit_chan *chan;
515
516 chan = dev->b1;
517
518 if (chan->fsm_state == ST_ACTIVE) {
519 pcbit_fake_conf(dev, chan);
520 }
521 chan = dev->b2;
522
523 if (chan->fsm_state == ST_ACTIVE) {
524 pcbit_fake_conf(dev, chan);
525 }
526 }
527
528 void
pcbit_irq_handler(int interrupt,void * devptr,struct pt_regs * regs)529 pcbit_irq_handler(int interrupt, void *devptr, struct pt_regs *regs)
530 {
531 struct pcbit_dev *dev;
532 u_char info,
533 ack_seq,
534 read_seq;
535
536 dev = (struct pcbit_dev *) devptr;
537
538 if (!dev) {
539 printk(KERN_WARNING "pcbit_irq_handler: wrong device\n");
540 return;
541 }
542 if (dev->interrupt) {
543 printk(KERN_DEBUG "pcbit: reentering interrupt hander\n");
544 return;
545 }
546 dev->interrupt = 1;
547
548 info = readb(dev->sh_mem + BANK3);
549
550 if (dev->l2_state == L2_STARTING || dev->l2_state == L2_ERROR) {
551 pcbit_l2_active_conf(dev, info);
552 dev->interrupt = 0;
553 return;
554 }
555 if (info & 0x40U) { /* E bit set */
556 #ifdef DEBUG
557 printk(KERN_DEBUG "pcbit_irq_handler: E bit on\n");
558 #endif
559 pcbit_l2_error(dev);
560 dev->interrupt = 0;
561 return;
562 }
563 if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING) {
564 dev->interrupt = 0;
565 return;
566 }
567 ack_seq = (info >> 3) & 0x07U;
568 read_seq = (info & 0x07U);
569
570 dev->interrupt = 0;
571
572 if (read_seq != dev->rcv_seq) {
573 while (read_seq != dev->rcv_seq) {
574 pcbit_receive(dev);
575 dev->rcv_seq = (dev->rcv_seq + 1) % 8;
576 }
577 pcbit_sched_delivery(dev);
578 }
579 if (ack_seq != dev->unack_seq) {
580 pcbit_recv_ack(dev, ack_seq);
581 }
582 info = dev->rcv_seq << 3;
583 info |= dev->send_seq;
584
585 writeb(info, dev->sh_mem + BANK4);
586 }
587
588
589 static void
pcbit_l2_active_conf(struct pcbit_dev * dev,u_char info)590 pcbit_l2_active_conf(struct pcbit_dev *dev, u_char info)
591 {
592 u_char state;
593
594 state = dev->l2_state;
595
596 #ifdef DEBUG
597 printk(KERN_DEBUG "layer2_active_confirm\n");
598 #endif
599
600
601 if (info & 0x80U) {
602 dev->rcv_seq = info & 0x07U;
603 dev->l2_state = L2_RUNNING;
604 } else
605 dev->l2_state = L2_DOWN;
606
607 if (state == L2_STARTING)
608 wake_up_interruptible(&dev->set_running_wq);
609
610 if (state == L2_ERROR && dev->l2_state == L2_RUNNING) {
611 pcbit_transmit(dev);
612 }
613 }
614
615 static void
pcbit_l2_err_recover(unsigned long data)616 pcbit_l2_err_recover(unsigned long data)
617 {
618
619 struct pcbit_dev *dev;
620 struct frame_buf *frame;
621
622 dev = (struct pcbit_dev *) data;
623
624 del_timer(&dev->error_recover_timer);
625 if (dev->w_busy || dev->r_busy) {
626 init_timer(&dev->error_recover_timer);
627 dev->error_recover_timer.expires = jiffies + ERRTIME;
628 add_timer(&dev->error_recover_timer);
629 return;
630 }
631 dev->w_busy = dev->r_busy = 1;
632
633 if (dev->read_frame) {
634 if (dev->read_frame->skb)
635 kfree_skb(dev->read_frame->skb);
636 kfree(dev->read_frame);
637 dev->read_frame = NULL;
638 }
639 if (dev->write_queue) {
640 frame = dev->write_queue;
641 #ifdef FREE_ON_ERROR
642 dev->write_queue = dev->write_queue->next;
643
644 if (frame->skb) {
645 dev_kfree_skb(frame->skb);
646 }
647 kfree(frame);
648 #else
649 frame->copied = 0;
650 #endif
651 }
652 dev->rcv_seq = dev->send_seq = dev->unack_seq = 0;
653 dev->free = 511;
654 dev->l2_state = L2_ERROR;
655
656 /* this is an hack... */
657 pcbit_firmware_bug(dev);
658
659 dev->writeptr = dev->sh_mem;
660 dev->readptr = dev->sh_mem + BANK2;
661
662 writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
663 dev->sh_mem + BANK4);
664 dev->w_busy = dev->r_busy = 0;
665
666 }
667
668 static void
pcbit_l2_error(struct pcbit_dev * dev)669 pcbit_l2_error(struct pcbit_dev *dev)
670 {
671 if (dev->l2_state == L2_RUNNING) {
672
673 printk(KERN_INFO "pcbit: layer 2 error\n");
674
675 #ifdef DEBUG
676 log_state(dev);
677 #endif
678
679 dev->l2_state = L2_DOWN;
680
681 init_timer(&dev->error_recover_timer);
682 dev->error_recover_timer.function = &pcbit_l2_err_recover;
683 dev->error_recover_timer.data = (ulong) dev;
684 dev->error_recover_timer.expires = jiffies + ERRTIME;
685 add_timer(&dev->error_recover_timer);
686 }
687 }
688
689 /*
690 * Description:
691 * if board acks frames
692 * update dev->free
693 * call pcbit_transmit to write possible queued frames
694 */
695
696 static void
pcbit_recv_ack(struct pcbit_dev * dev,unsigned char ack)697 pcbit_recv_ack(struct pcbit_dev *dev, unsigned char ack)
698 {
699 int i,
700 count;
701 int unacked;
702
703 unacked = (dev->send_seq + (8 - dev->unack_seq)) & 0x07;
704
705 /* dev->unack_seq < ack <= dev->send_seq; */
706
707 if (unacked) {
708
709 if (dev->send_seq > dev->unack_seq) {
710 if (ack <= dev->unack_seq || ack > dev->send_seq) {
711 printk(KERN_DEBUG
712 "layer 2 ack unacceptable - dev %d",
713 dev->id);
714
715 pcbit_l2_error(dev);
716 } else if (ack > dev->send_seq && ack <= dev->unack_seq) {
717 printk(KERN_DEBUG
718 "layer 2 ack unacceptable - dev %d",
719 dev->id);
720 pcbit_l2_error(dev);
721 }
722 }
723 /* ack is acceptable */
724
725
726 i = dev->unack_seq;
727
728 do {
729 dev->unack_seq = i = (i + 1) % 8;
730 dev->free += dev->fsize[i];
731 } while (i != ack);
732
733 count = 0;
734 while (count < 7 && dev->write_queue) {
735 u8 lsend_seq = dev->send_seq;
736
737 pcbit_transmit(dev);
738
739 if (dev->send_seq == lsend_seq)
740 break;
741 count++;
742 }
743 } else
744 printk(KERN_DEBUG "recv_ack: unacked = 0\n");
745 }
746