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