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