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