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