1 /* $Id: b1dma.c,v 1.1.2.3 2004/02/10 01:07:12 keil Exp $
2  *
3  * Common module for AVM B1 cards that support dma with AMCC
4  *
5  * Copyright 2000 by Carsten Paeth <calle@calle.de>
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11 
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
16 #include <linux/skbuff.h>
17 #include <linux/delay.h>
18 #include <linux/mm.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
21 #include <linux/capi.h>
22 #include <linux/kernelcapi.h>
23 #include <linux/gfp.h>
24 #include <asm/io.h>
25 #include <linux/init.h>
26 #include <asm/uaccess.h>
27 #include <linux/netdevice.h>
28 #include <linux/isdn/capilli.h>
29 #include "avmcard.h"
30 #include <linux/isdn/capicmd.h>
31 #include <linux/isdn/capiutil.h>
32 
33 static char *revision = "$Revision: 1.1.2.3 $";
34 
35 #undef AVM_B1DMA_DEBUG
36 
37 /* ------------------------------------------------------------- */
38 
39 MODULE_DESCRIPTION("CAPI4Linux: DMA support for active AVM cards");
40 MODULE_AUTHOR("Carsten Paeth");
41 MODULE_LICENSE("GPL");
42 
43 static int suppress_pollack = 0;
44 module_param(suppress_pollack, bool, 0);
45 
46 /* ------------------------------------------------------------- */
47 
48 static void b1dma_dispatch_tx(avmcard *card);
49 
50 /* ------------------------------------------------------------- */
51 
52 /* S5933 */
53 
54 #define	AMCC_RXPTR	0x24
55 #define	AMCC_RXLEN	0x28
56 #define	AMCC_TXPTR	0x2c
57 #define	AMCC_TXLEN	0x30
58 
59 #define	AMCC_INTCSR	0x38
60 #	define EN_READ_TC_INT		0x00008000L
61 #	define EN_WRITE_TC_INT		0x00004000L
62 #	define EN_TX_TC_INT		EN_READ_TC_INT
63 #	define EN_RX_TC_INT		EN_WRITE_TC_INT
64 #	define AVM_FLAG			0x30000000L
65 
66 #	define ANY_S5933_INT		0x00800000L
67 #	define READ_TC_INT		0x00080000L
68 #	define WRITE_TC_INT		0x00040000L
69 #	define	TX_TC_INT		READ_TC_INT
70 #	define	RX_TC_INT		WRITE_TC_INT
71 #	define MASTER_ABORT_INT		0x00100000L
72 #	define TARGET_ABORT_INT		0x00200000L
73 #	define BUS_MASTER_INT		0x00200000L
74 #	define ALL_INT			0x000C0000L
75 
76 #define	AMCC_MCSR	0x3c
77 #	define A2P_HI_PRIORITY		0x00000100L
78 #	define EN_A2P_TRANSFERS		0x00000400L
79 #	define P2A_HI_PRIORITY		0x00001000L
80 #	define EN_P2A_TRANSFERS		0x00004000L
81 #	define RESET_A2P_FLAGS		0x04000000L
82 #	define RESET_P2A_FLAGS		0x02000000L
83 
84 /* ------------------------------------------------------------- */
85 
b1dma_writel(avmcard * card,u32 value,int off)86 static inline void b1dma_writel(avmcard *card, u32 value, int off)
87 {
88 	writel(value, card->mbase + off);
89 }
90 
b1dma_readl(avmcard * card,int off)91 static inline u32 b1dma_readl(avmcard *card, int off)
92 {
93 	return readl(card->mbase + off);
94 }
95 
96 /* ------------------------------------------------------------- */
97 
b1dma_tx_empty(unsigned int port)98 static inline int b1dma_tx_empty(unsigned int port)
99 {
100 	return inb(port + 0x03) & 0x1;
101 }
102 
b1dma_rx_full(unsigned int port)103 static inline int b1dma_rx_full(unsigned int port)
104 {
105 	return inb(port + 0x02) & 0x1;
106 }
107 
b1dma_tolink(avmcard * card,void * buf,unsigned int len)108 static int b1dma_tolink(avmcard *card, void *buf, unsigned int len)
109 {
110 	unsigned long stop = jiffies + 1 * HZ;	/* maximum wait time 1 sec */
111 	unsigned char *s = (unsigned char *)buf;
112 	while (len--) {
113 		while (   !b1dma_tx_empty(card->port)
114 		       && time_before(jiffies, stop));
115 		if (!b1dma_tx_empty(card->port))
116 			return -1;
117 	        t1outp(card->port, 0x01, *s++);
118 	}
119 	return 0;
120 }
121 
b1dma_fromlink(avmcard * card,void * buf,unsigned int len)122 static int b1dma_fromlink(avmcard *card, void *buf, unsigned int len)
123 {
124 	unsigned long stop = jiffies + 1 * HZ;	/* maximum wait time 1 sec */
125 	unsigned char *s = (unsigned char *)buf;
126 	while (len--) {
127 		while (   !b1dma_rx_full(card->port)
128 		       && time_before(jiffies, stop));
129 		if (!b1dma_rx_full(card->port))
130 			return -1;
131 	        *s++ = t1inp(card->port, 0x00);
132 	}
133 	return 0;
134 }
135 
WriteReg(avmcard * card,u32 reg,u8 val)136 static int WriteReg(avmcard *card, u32 reg, u8 val)
137 {
138 	u8 cmd = 0x00;
139 	if (   b1dma_tolink(card, &cmd, 1) == 0
140 	    && b1dma_tolink(card, &reg, 4) == 0) {
141 		u32 tmp = val;
142 		return b1dma_tolink(card, &tmp, 4);
143 	}
144 	return -1;
145 }
146 
ReadReg(avmcard * card,u32 reg)147 static u8 ReadReg(avmcard *card, u32 reg)
148 {
149 	u8 cmd = 0x01;
150 	if (   b1dma_tolink(card, &cmd, 1) == 0
151 	    && b1dma_tolink(card, &reg, 4) == 0) {
152 		u32 tmp;
153 		if (b1dma_fromlink(card, &tmp, 4) == 0)
154 			return (u8)tmp;
155 	}
156 	return 0xff;
157 }
158 
159 /* ------------------------------------------------------------- */
160 
_put_byte(void ** pp,u8 val)161 static inline void _put_byte(void **pp, u8 val)
162 {
163 	u8 *s = *pp;
164 	*s++ = val;
165 	*pp = s;
166 }
167 
_put_word(void ** pp,u32 val)168 static inline void _put_word(void **pp, u32 val)
169 {
170 	u8 *s = *pp;
171 	*s++ = val & 0xff;
172 	*s++ = (val >> 8) & 0xff;
173 	*s++ = (val >> 16) & 0xff;
174 	*s++ = (val >> 24) & 0xff;
175 	*pp = s;
176 }
177 
_put_slice(void ** pp,unsigned char * dp,unsigned int len)178 static inline void _put_slice(void **pp, unsigned char *dp, unsigned int len)
179 {
180 	unsigned i = len;
181 	_put_word(pp, i);
182 	while (i-- > 0)
183 		_put_byte(pp, *dp++);
184 }
185 
_get_byte(void ** pp)186 static inline u8 _get_byte(void **pp)
187 {
188 	u8 *s = *pp;
189 	u8 val;
190 	val = *s++;
191 	*pp = s;
192 	return val;
193 }
194 
_get_word(void ** pp)195 static inline u32 _get_word(void **pp)
196 {
197 	u8 *s = *pp;
198 	u32 val;
199 	val = *s++;
200 	val |= (*s++ << 8);
201 	val |= (*s++ << 16);
202 	val |= (*s++ << 24);
203 	*pp = s;
204 	return val;
205 }
206 
_get_slice(void ** pp,unsigned char * dp)207 static inline u32 _get_slice(void **pp, unsigned char *dp)
208 {
209 	unsigned int len, i;
210 
211 	len = i = _get_word(pp);
212 	while (i-- > 0) *dp++ = _get_byte(pp);
213 	return len;
214 }
215 
216 /* ------------------------------------------------------------- */
217 
b1dma_reset(avmcard * card)218 void b1dma_reset(avmcard *card)
219 {
220 	card->csr = 0x0;
221 	b1dma_writel(card, card->csr, AMCC_INTCSR);
222 	b1dma_writel(card, 0, AMCC_MCSR);
223 	b1dma_writel(card, 0, AMCC_RXLEN);
224 	b1dma_writel(card, 0, AMCC_TXLEN);
225 
226 	t1outp(card->port, 0x10, 0x00);
227 	t1outp(card->port, 0x07, 0x00);
228 
229 	b1dma_writel(card, 0, AMCC_MCSR);
230 	mdelay(10);
231 	b1dma_writel(card, 0x0f000000, AMCC_MCSR); /* reset all */
232 	mdelay(10);
233 	b1dma_writel(card, 0, AMCC_MCSR);
234 	if (card->cardtype == avm_t1pci)
235 		mdelay(42);
236 	else
237 		mdelay(10);
238 }
239 
240 /* ------------------------------------------------------------- */
241 
b1dma_detect(avmcard * card)242 static int b1dma_detect(avmcard *card)
243 {
244 	b1dma_writel(card, 0, AMCC_MCSR);
245 	mdelay(10);
246 	b1dma_writel(card, 0x0f000000, AMCC_MCSR); /* reset all */
247 	mdelay(10);
248 	b1dma_writel(card, 0, AMCC_MCSR);
249 	mdelay(42);
250 
251 	b1dma_writel(card, 0, AMCC_RXLEN);
252 	b1dma_writel(card, 0, AMCC_TXLEN);
253 	card->csr = 0x0;
254 	b1dma_writel(card, card->csr, AMCC_INTCSR);
255 
256 	if (b1dma_readl(card, AMCC_MCSR) != 0x000000E6)
257 		return 1;
258 
259 	b1dma_writel(card, 0xffffffff, AMCC_RXPTR);
260 	b1dma_writel(card, 0xffffffff, AMCC_TXPTR);
261 	if (   b1dma_readl(card, AMCC_RXPTR) != 0xfffffffc
262 	    || b1dma_readl(card, AMCC_TXPTR) != 0xfffffffc)
263 		return 2;
264 
265 	b1dma_writel(card, 0x0, AMCC_RXPTR);
266 	b1dma_writel(card, 0x0, AMCC_TXPTR);
267 	if (   b1dma_readl(card, AMCC_RXPTR) != 0x0
268 	    || b1dma_readl(card, AMCC_TXPTR) != 0x0)
269 		return 3;
270 
271 	t1outp(card->port, 0x10, 0x00);
272 	t1outp(card->port, 0x07, 0x00);
273 
274 	t1outp(card->port, 0x02, 0x02);
275 	t1outp(card->port, 0x03, 0x02);
276 
277 	if (   (t1inp(card->port, 0x02) & 0xFE) != 0x02
278 	    || t1inp(card->port, 0x3) != 0x03)
279 		return 4;
280 
281 	t1outp(card->port, 0x02, 0x00);
282 	t1outp(card->port, 0x03, 0x00);
283 
284 	if (   (t1inp(card->port, 0x02) & 0xFE) != 0x00
285 	    || t1inp(card->port, 0x3) != 0x01)
286 		return 5;
287 
288 	return 0;
289 }
290 
t1pci_detect(avmcard * card)291 int t1pci_detect(avmcard *card)
292 {
293 	int ret;
294 
295 	if ((ret = b1dma_detect(card)) != 0)
296 		return ret;
297 
298 	/* Transputer test */
299 
300 	if (   WriteReg(card, 0x80001000, 0x11) != 0
301 	    || WriteReg(card, 0x80101000, 0x22) != 0
302 	    || WriteReg(card, 0x80201000, 0x33) != 0
303 	    || WriteReg(card, 0x80301000, 0x44) != 0)
304 		return 6;
305 
306 	if (   ReadReg(card, 0x80001000) != 0x11
307 	    || ReadReg(card, 0x80101000) != 0x22
308 	    || ReadReg(card, 0x80201000) != 0x33
309 	    || ReadReg(card, 0x80301000) != 0x44)
310 		return 7;
311 
312 	if (   WriteReg(card, 0x80001000, 0x55) != 0
313 	    || WriteReg(card, 0x80101000, 0x66) != 0
314 	    || WriteReg(card, 0x80201000, 0x77) != 0
315 	    || WriteReg(card, 0x80301000, 0x88) != 0)
316 		return 8;
317 
318 	if (   ReadReg(card, 0x80001000) != 0x55
319 	    || ReadReg(card, 0x80101000) != 0x66
320 	    || ReadReg(card, 0x80201000) != 0x77
321 	    || ReadReg(card, 0x80301000) != 0x88)
322 		return 9;
323 
324 	return 0;
325 }
326 
b1pciv4_detect(avmcard * card)327 int b1pciv4_detect(avmcard *card)
328 {
329 	int ret, i;
330 
331 	if ((ret = b1dma_detect(card)) != 0)
332 		return ret;
333 
334 	for (i=0; i < 5 ; i++) {
335 		if (WriteReg(card, 0x80A00000, 0x21) != 0)
336 			return 6;
337 		if ((ReadReg(card, 0x80A00000) & 0x01) != 0x01)
338 			return 7;
339 	}
340 	for (i=0; i < 5 ; i++) {
341 		if (WriteReg(card, 0x80A00000, 0x20) != 0)
342 			return 8;
343 		if ((ReadReg(card, 0x80A00000) & 0x01) != 0x00)
344 			return 9;
345 	}
346 
347 	return 0;
348 }
349 
b1dma_queue_tx(avmcard * card,struct sk_buff * skb)350 static void b1dma_queue_tx(avmcard *card, struct sk_buff *skb)
351 {
352 	unsigned long flags;
353 
354 	spin_lock_irqsave(&card->lock, flags);
355 
356 	skb_queue_tail(&card->dma->send_queue, skb);
357 
358 	if (!(card->csr & EN_TX_TC_INT)) {
359 		b1dma_dispatch_tx(card);
360 		b1dma_writel(card, card->csr, AMCC_INTCSR);
361 	}
362 
363 	spin_unlock_irqrestore(&card->lock, flags);
364 }
365 
366 /* ------------------------------------------------------------- */
367 
b1dma_dispatch_tx(avmcard * card)368 static void b1dma_dispatch_tx(avmcard *card)
369 {
370 	avmcard_dmainfo *dma = card->dma;
371 	struct sk_buff *skb;
372 	u8 cmd, subcmd;
373 	u16 len;
374 	u32 txlen;
375 	void *p;
376 
377 	skb = skb_dequeue(&dma->send_queue);
378 
379 	len = CAPIMSG_LEN(skb->data);
380 
381 	if (len) {
382 		cmd = CAPIMSG_COMMAND(skb->data);
383 		subcmd = CAPIMSG_SUBCOMMAND(skb->data);
384 
385 		p = dma->sendbuf.dmabuf;
386 
387 		if (CAPICMD(cmd, subcmd) == CAPI_DATA_B3_REQ) {
388 			u16 dlen = CAPIMSG_DATALEN(skb->data);
389 			_put_byte(&p, SEND_DATA_B3_REQ);
390 			_put_slice(&p, skb->data, len);
391 			_put_slice(&p, skb->data + len, dlen);
392 		} else {
393 			_put_byte(&p, SEND_MESSAGE);
394 			_put_slice(&p, skb->data, len);
395 		}
396 		txlen = (u8 *)p - (u8 *)dma->sendbuf.dmabuf;
397 #ifdef AVM_B1DMA_DEBUG
398 		printk(KERN_DEBUG "tx: put msg len=%d\n", txlen);
399 #endif
400 	} else {
401 		txlen = skb->len-2;
402 #ifdef AVM_B1DMA_POLLDEBUG
403 		if (skb->data[2] == SEND_POLLACK)
404 			printk(KERN_INFO "%s: send ack\n", card->name);
405 #endif
406 #ifdef AVM_B1DMA_DEBUG
407 		printk(KERN_DEBUG "tx: put 0x%x len=%d\n",
408 		       skb->data[2], txlen);
409 #endif
410 		skb_copy_from_linear_data_offset(skb, 2, dma->sendbuf.dmabuf,
411 						 skb->len - 2);
412 	}
413 	txlen = (txlen + 3) & ~3;
414 
415 	b1dma_writel(card, dma->sendbuf.dmaaddr, AMCC_TXPTR);
416 	b1dma_writel(card, txlen, AMCC_TXLEN);
417 
418 	card->csr |= EN_TX_TC_INT;
419 
420 	dev_kfree_skb_any(skb);
421 }
422 
423 /* ------------------------------------------------------------- */
424 
queue_pollack(avmcard * card)425 static void queue_pollack(avmcard *card)
426 {
427 	struct sk_buff *skb;
428 	void *p;
429 
430 	skb = alloc_skb(3, GFP_ATOMIC);
431 	if (!skb) {
432 		printk(KERN_CRIT "%s: no memory, lost poll ack\n",
433 					card->name);
434 		return;
435 	}
436 	p = skb->data;
437 	_put_byte(&p, 0);
438 	_put_byte(&p, 0);
439 	_put_byte(&p, SEND_POLLACK);
440 	skb_put(skb, (u8 *)p - (u8 *)skb->data);
441 
442 	b1dma_queue_tx(card, skb);
443 }
444 
445 /* ------------------------------------------------------------- */
446 
b1dma_handle_rx(avmcard * card)447 static void b1dma_handle_rx(avmcard *card)
448 {
449 	avmctrl_info *cinfo = &card->ctrlinfo[0];
450 	avmcard_dmainfo *dma = card->dma;
451 	struct capi_ctr *ctrl = &cinfo->capi_ctrl;
452 	struct sk_buff *skb;
453 	void *p = dma->recvbuf.dmabuf+4;
454 	u32 ApplId, MsgLen, DataB3Len, NCCI, WindowSize;
455 	u8 b1cmd =  _get_byte(&p);
456 
457 #ifdef AVM_B1DMA_DEBUG
458 	printk(KERN_DEBUG "rx: 0x%x %lu\n", b1cmd, (unsigned long)dma->recvlen);
459 #endif
460 
461 	switch (b1cmd) {
462 	case RECEIVE_DATA_B3_IND:
463 
464 		ApplId = (unsigned) _get_word(&p);
465 		MsgLen = _get_slice(&p, card->msgbuf);
466 		DataB3Len = _get_slice(&p, card->databuf);
467 
468 		if (MsgLen < 30) { /* not CAPI 64Bit */
469 			memset(card->msgbuf+MsgLen, 0, 30-MsgLen);
470 			MsgLen = 30;
471 			CAPIMSG_SETLEN(card->msgbuf, 30);
472 		}
473 		if (!(skb = alloc_skb(DataB3Len+MsgLen, GFP_ATOMIC))) {
474 			printk(KERN_ERR "%s: incoming packet dropped\n",
475 					card->name);
476 		} else {
477 			memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
478 			memcpy(skb_put(skb, DataB3Len), card->databuf, DataB3Len);
479 			capi_ctr_handle_message(ctrl, ApplId, skb);
480 		}
481 		break;
482 
483 	case RECEIVE_MESSAGE:
484 
485 		ApplId = (unsigned) _get_word(&p);
486 		MsgLen = _get_slice(&p, card->msgbuf);
487 		if (!(skb = alloc_skb(MsgLen, GFP_ATOMIC))) {
488 			printk(KERN_ERR "%s: incoming packet dropped\n",
489 					card->name);
490 		} else {
491 			memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
492 			if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_CONF) {
493 				spin_lock(&card->lock);
494 				capilib_data_b3_conf(&cinfo->ncci_head, ApplId,
495 					CAPIMSG_NCCI(skb->data),
496 					CAPIMSG_MSGID(skb->data));
497 				spin_unlock(&card->lock);
498 			}
499 			capi_ctr_handle_message(ctrl, ApplId, skb);
500 		}
501 		break;
502 
503 	case RECEIVE_NEW_NCCI:
504 
505 		ApplId = _get_word(&p);
506 		NCCI = _get_word(&p);
507 		WindowSize = _get_word(&p);
508 		spin_lock(&card->lock);
509 		capilib_new_ncci(&cinfo->ncci_head, ApplId, NCCI, WindowSize);
510 		spin_unlock(&card->lock);
511 		break;
512 
513 	case RECEIVE_FREE_NCCI:
514 
515 		ApplId = _get_word(&p);
516 		NCCI = _get_word(&p);
517 
518 		if (NCCI != 0xffffffff) {
519 			spin_lock(&card->lock);
520 			capilib_free_ncci(&cinfo->ncci_head, ApplId, NCCI);
521 			spin_unlock(&card->lock);
522 		}
523 		break;
524 
525 	case RECEIVE_START:
526 #ifdef AVM_B1DMA_POLLDEBUG
527 		printk(KERN_INFO "%s: receive poll\n", card->name);
528 #endif
529 		if (!suppress_pollack)
530 			queue_pollack(card);
531 		capi_ctr_resume_output(ctrl);
532 		break;
533 
534 	case RECEIVE_STOP:
535 		capi_ctr_suspend_output(ctrl);
536 		break;
537 
538 	case RECEIVE_INIT:
539 
540 		cinfo->versionlen = _get_slice(&p, cinfo->versionbuf);
541 		b1_parse_version(cinfo);
542 		printk(KERN_INFO "%s: %s-card (%s) now active\n",
543 		       card->name,
544 		       cinfo->version[VER_CARDTYPE],
545 		       cinfo->version[VER_DRIVER]);
546 		capi_ctr_ready(ctrl);
547 		break;
548 
549 	case RECEIVE_TASK_READY:
550 		ApplId = (unsigned) _get_word(&p);
551 		MsgLen = _get_slice(&p, card->msgbuf);
552 		card->msgbuf[MsgLen] = 0;
553 		while (    MsgLen > 0
554 		       && (   card->msgbuf[MsgLen-1] == '\n'
555 			   || card->msgbuf[MsgLen-1] == '\r')) {
556 			card->msgbuf[MsgLen-1] = 0;
557 			MsgLen--;
558 		}
559 		printk(KERN_INFO "%s: task %d \"%s\" ready.\n",
560 				card->name, ApplId, card->msgbuf);
561 		break;
562 
563 	case RECEIVE_DEBUGMSG:
564 		MsgLen = _get_slice(&p, card->msgbuf);
565 		card->msgbuf[MsgLen] = 0;
566 		while (    MsgLen > 0
567 		       && (   card->msgbuf[MsgLen-1] == '\n'
568 			   || card->msgbuf[MsgLen-1] == '\r')) {
569 			card->msgbuf[MsgLen-1] = 0;
570 			MsgLen--;
571 		}
572 		printk(KERN_INFO "%s: DEBUG: %s\n", card->name, card->msgbuf);
573 		break;
574 
575 	default:
576 		printk(KERN_ERR "%s: b1dma_interrupt: 0x%x ???\n",
577 				card->name, b1cmd);
578 		return;
579 	}
580 }
581 
582 /* ------------------------------------------------------------- */
583 
b1dma_handle_interrupt(avmcard * card)584 static void b1dma_handle_interrupt(avmcard *card)
585 {
586 	u32 status;
587 	u32 newcsr;
588 
589 	spin_lock(&card->lock);
590 
591 	status = b1dma_readl(card, AMCC_INTCSR);
592 	if ((status & ANY_S5933_INT) == 0) {
593 		spin_unlock(&card->lock);
594 		return;
595 	}
596 
597         newcsr = card->csr | (status & ALL_INT);
598 	if (status & TX_TC_INT) newcsr &= ~EN_TX_TC_INT;
599 	if (status & RX_TC_INT) newcsr &= ~EN_RX_TC_INT;
600 	b1dma_writel(card, newcsr, AMCC_INTCSR);
601 
602 	if ((status & RX_TC_INT) != 0) {
603 		struct avmcard_dmainfo *dma = card->dma;
604 		u32 rxlen;
605 	   	if (card->dma->recvlen == 0) {
606 	        	rxlen = b1dma_readl(card, AMCC_RXLEN);
607 			if (rxlen == 0) {
608 				dma->recvlen = *((u32 *)dma->recvbuf.dmabuf);
609 				rxlen = (dma->recvlen + 3) & ~3;
610 				b1dma_writel(card, dma->recvbuf.dmaaddr+4, AMCC_RXPTR);
611 				b1dma_writel(card, rxlen, AMCC_RXLEN);
612 #ifdef AVM_B1DMA_DEBUG
613 			} else {
614 				printk(KERN_ERR "%s: rx not complete (%d).\n",
615 					card->name, rxlen);
616 #endif
617 			}
618 		} else {
619 			spin_unlock(&card->lock);
620 			b1dma_handle_rx(card);
621 	   		dma->recvlen = 0;
622 			spin_lock(&card->lock);
623 			b1dma_writel(card, dma->recvbuf.dmaaddr, AMCC_RXPTR);
624 			b1dma_writel(card, 4, AMCC_RXLEN);
625 		}
626 	}
627 
628 	if ((status & TX_TC_INT) != 0) {
629 		if (skb_queue_empty(&card->dma->send_queue))
630 			card->csr &= ~EN_TX_TC_INT;
631 		else
632 			b1dma_dispatch_tx(card);
633 	}
634 	b1dma_writel(card, card->csr, AMCC_INTCSR);
635 
636 	spin_unlock(&card->lock);
637 }
638 
b1dma_interrupt(int interrupt,void * devptr)639 irqreturn_t b1dma_interrupt(int interrupt, void *devptr)
640 {
641 	avmcard *card = devptr;
642 
643 	b1dma_handle_interrupt(card);
644 	return IRQ_HANDLED;
645 }
646 
647 /* ------------------------------------------------------------- */
648 
b1dma_loaded(avmcard * card)649 static int b1dma_loaded(avmcard *card)
650 {
651 	unsigned long stop;
652 	unsigned char ans;
653 	unsigned long tout = 2;
654 	unsigned int base = card->port;
655 
656 	for (stop = jiffies + tout * HZ; time_before(jiffies, stop);) {
657 		if (b1_tx_empty(base))
658 			break;
659 	}
660 	if (!b1_tx_empty(base)) {
661 		printk(KERN_ERR "%s: b1dma_loaded: tx err, corrupted t4 file ?\n",
662 				card->name);
663 		return 0;
664 	}
665 	b1_put_byte(base, SEND_POLLACK);
666 	for (stop = jiffies + tout * HZ; time_before(jiffies, stop);) {
667 		if (b1_rx_full(base)) {
668 			if ((ans = b1_get_byte(base)) == RECEIVE_POLLDWORD) {
669 				return 1;
670 			}
671 			printk(KERN_ERR "%s: b1dma_loaded: got 0x%x, firmware not running in dword mode\n", card->name, ans);
672 			return 0;
673 		}
674 	}
675 	printk(KERN_ERR "%s: b1dma_loaded: firmware not running\n", card->name);
676 	return 0;
677 }
678 
679 /* ------------------------------------------------------------- */
680 
b1dma_send_init(avmcard * card)681 static void b1dma_send_init(avmcard *card)
682 {
683 	struct sk_buff *skb;
684 	void *p;
685 
686 	skb = alloc_skb(15, GFP_ATOMIC);
687 	if (!skb) {
688 		printk(KERN_CRIT "%s: no memory, lost register appl.\n",
689 					card->name);
690 		return;
691 	}
692 	p = skb->data;
693 	_put_byte(&p, 0);
694 	_put_byte(&p, 0);
695 	_put_byte(&p, SEND_INIT);
696 	_put_word(&p, CAPI_MAXAPPL);
697 	_put_word(&p, AVM_NCCI_PER_CHANNEL*30);
698 	_put_word(&p, card->cardnr - 1);
699 	skb_put(skb, (u8 *)p - (u8 *)skb->data);
700 
701 	b1dma_queue_tx(card, skb);
702 }
703 
b1dma_load_firmware(struct capi_ctr * ctrl,capiloaddata * data)704 int b1dma_load_firmware(struct capi_ctr *ctrl, capiloaddata *data)
705 {
706 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
707 	avmcard *card = cinfo->card;
708 	int retval;
709 
710 	b1dma_reset(card);
711 
712 	if ((retval = b1_load_t4file(card, &data->firmware))) {
713 		b1dma_reset(card);
714 		printk(KERN_ERR "%s: failed to load t4file!!\n",
715 					card->name);
716 		return retval;
717 	}
718 
719 	if (data->configuration.len > 0 && data->configuration.data) {
720 		if ((retval = b1_load_config(card, &data->configuration))) {
721 			b1dma_reset(card);
722 			printk(KERN_ERR "%s: failed to load config!!\n",
723 					card->name);
724 			return retval;
725 		}
726 	}
727 
728 	if (!b1dma_loaded(card)) {
729 		b1dma_reset(card);
730 		printk(KERN_ERR "%s: failed to load t4file.\n", card->name);
731 		return -EIO;
732 	}
733 
734 	card->csr = AVM_FLAG;
735 	b1dma_writel(card, card->csr, AMCC_INTCSR);
736 	b1dma_writel(card, EN_A2P_TRANSFERS|EN_P2A_TRANSFERS|A2P_HI_PRIORITY|
737 		     P2A_HI_PRIORITY|RESET_A2P_FLAGS|RESET_P2A_FLAGS,
738 		     AMCC_MCSR);
739 	t1outp(card->port, 0x07, 0x30);
740 	t1outp(card->port, 0x10, 0xF0);
741 
742 	card->dma->recvlen = 0;
743 	b1dma_writel(card, card->dma->recvbuf.dmaaddr, AMCC_RXPTR);
744 	b1dma_writel(card, 4, AMCC_RXLEN);
745 	card->csr |= EN_RX_TC_INT;
746 	b1dma_writel(card, card->csr, AMCC_INTCSR);
747 
748         b1dma_send_init(card);
749 
750 	return 0;
751 }
752 
b1dma_reset_ctr(struct capi_ctr * ctrl)753 void b1dma_reset_ctr(struct capi_ctr *ctrl)
754 {
755 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
756 	avmcard *card = cinfo->card;
757 	unsigned long flags;
758 
759 	spin_lock_irqsave(&card->lock, flags);
760  	b1dma_reset(card);
761 
762 	memset(cinfo->version, 0, sizeof(cinfo->version));
763 	capilib_release(&cinfo->ncci_head);
764 	spin_unlock_irqrestore(&card->lock, flags);
765 	capi_ctr_down(ctrl);
766 }
767 
768 /* ------------------------------------------------------------- */
769 
b1dma_register_appl(struct capi_ctr * ctrl,u16 appl,capi_register_params * rp)770 void b1dma_register_appl(struct capi_ctr *ctrl,
771 				u16 appl,
772 				capi_register_params *rp)
773 {
774 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
775 	avmcard *card = cinfo->card;
776 	struct sk_buff *skb;
777 	int want = rp->level3cnt;
778 	int nconn;
779 	void *p;
780 
781 	if (want > 0) nconn = want;
782 	else nconn = ctrl->profile.nbchannel * -want;
783 	if (nconn == 0) nconn = ctrl->profile.nbchannel;
784 
785 	skb = alloc_skb(23, GFP_ATOMIC);
786 	if (!skb) {
787 		printk(KERN_CRIT "%s: no memory, lost register appl.\n",
788 					card->name);
789 		return;
790 	}
791 	p = skb->data;
792 	_put_byte(&p, 0);
793 	_put_byte(&p, 0);
794 	_put_byte(&p, SEND_REGISTER);
795 	_put_word(&p, appl);
796 	_put_word(&p, 1024 * (nconn+1));
797 	_put_word(&p, nconn);
798 	_put_word(&p, rp->datablkcnt);
799 	_put_word(&p, rp->datablklen);
800 	skb_put(skb, (u8 *)p - (u8 *)skb->data);
801 
802 	b1dma_queue_tx(card, skb);
803 }
804 
805 /* ------------------------------------------------------------- */
806 
b1dma_release_appl(struct capi_ctr * ctrl,u16 appl)807 void b1dma_release_appl(struct capi_ctr *ctrl, u16 appl)
808 {
809 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
810 	avmcard *card = cinfo->card;
811 	struct sk_buff *skb;
812 	void *p;
813 	unsigned long flags;
814 
815 	spin_lock_irqsave(&card->lock, flags);
816 	capilib_release_appl(&cinfo->ncci_head, appl);
817 	spin_unlock_irqrestore(&card->lock, flags);
818 
819 	skb = alloc_skb(7, GFP_ATOMIC);
820 	if (!skb) {
821 		printk(KERN_CRIT "%s: no memory, lost release appl.\n",
822 					card->name);
823 		return;
824 	}
825 	p = skb->data;
826 	_put_byte(&p, 0);
827 	_put_byte(&p, 0);
828 	_put_byte(&p, SEND_RELEASE);
829 	_put_word(&p, appl);
830 
831 	skb_put(skb, (u8 *)p - (u8 *)skb->data);
832 
833 	b1dma_queue_tx(card, skb);
834 }
835 
836 /* ------------------------------------------------------------- */
837 
b1dma_send_message(struct capi_ctr * ctrl,struct sk_buff * skb)838 u16 b1dma_send_message(struct capi_ctr *ctrl, struct sk_buff *skb)
839 {
840 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
841 	avmcard *card = cinfo->card;
842 	u16 retval = CAPI_NOERROR;
843 
844  	if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
845 		unsigned long flags;
846 		spin_lock_irqsave(&card->lock, flags);
847 		retval = capilib_data_b3_req(&cinfo->ncci_head,
848 					     CAPIMSG_APPID(skb->data),
849 					     CAPIMSG_NCCI(skb->data),
850 					     CAPIMSG_MSGID(skb->data));
851 		spin_unlock_irqrestore(&card->lock, flags);
852 	}
853 	if (retval == CAPI_NOERROR)
854 		b1dma_queue_tx(card, skb);
855 
856 	return retval;
857 }
858 
859 /* ------------------------------------------------------------- */
860 
b1dmactl_proc_show(struct seq_file * m,void * v)861 static int b1dmactl_proc_show(struct seq_file *m, void *v)
862 {
863 	struct capi_ctr *ctrl = m->private;
864 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
865 	avmcard *card = cinfo->card;
866 	u8 flag;
867 	char *s;
868 	u32 txoff, txlen, rxoff, rxlen, csr;
869 	unsigned long flags;
870 
871 	seq_printf(m, "%-16s %s\n", "name", card->name);
872 	seq_printf(m, "%-16s 0x%x\n", "io", card->port);
873 	seq_printf(m, "%-16s %d\n", "irq", card->irq);
874 	seq_printf(m, "%-16s 0x%lx\n", "membase", card->membase);
875 	switch (card->cardtype) {
876 	case avm_b1isa: s = "B1 ISA"; break;
877 	case avm_b1pci: s = "B1 PCI"; break;
878 	case avm_b1pcmcia: s = "B1 PCMCIA"; break;
879 	case avm_m1: s = "M1"; break;
880 	case avm_m2: s = "M2"; break;
881 	case avm_t1isa: s = "T1 ISA (HEMA)"; break;
882 	case avm_t1pci: s = "T1 PCI"; break;
883 	case avm_c4: s = "C4"; break;
884 	case avm_c2: s = "C2"; break;
885 	default: s = "???"; break;
886 	}
887 	seq_printf(m, "%-16s %s\n", "type", s);
888 	if ((s = cinfo->version[VER_DRIVER]) != NULL)
889 		seq_printf(m, "%-16s %s\n", "ver_driver", s);
890 	if ((s = cinfo->version[VER_CARDTYPE]) != NULL)
891 		seq_printf(m, "%-16s %s\n", "ver_cardtype", s);
892 	if ((s = cinfo->version[VER_SERIAL]) != NULL)
893 		seq_printf(m, "%-16s %s\n", "ver_serial", s);
894 
895 	if (card->cardtype != avm_m1) {
896         	flag = ((u8 *)(ctrl->profile.manu))[3];
897         	if (flag)
898 			seq_printf(m, "%-16s%s%s%s%s%s%s%s\n",
899 			"protocol",
900 			(flag & 0x01) ? " DSS1" : "",
901 			(flag & 0x02) ? " CT1" : "",
902 			(flag & 0x04) ? " VN3" : "",
903 			(flag & 0x08) ? " NI1" : "",
904 			(flag & 0x10) ? " AUSTEL" : "",
905 			(flag & 0x20) ? " ESS" : "",
906 			(flag & 0x40) ? " 1TR6" : ""
907 			);
908 	}
909 	if (card->cardtype != avm_m1) {
910         	flag = ((u8 *)(ctrl->profile.manu))[5];
911 		if (flag)
912 			seq_printf(m, "%-16s%s%s%s%s\n",
913 			"linetype",
914 			(flag & 0x01) ? " point to point" : "",
915 			(flag & 0x02) ? " point to multipoint" : "",
916 			(flag & 0x08) ? " leased line without D-channel" : "",
917 			(flag & 0x04) ? " leased line with D-channel" : ""
918 			);
919 	}
920 	seq_printf(m, "%-16s %s\n", "cardname", cinfo->cardname);
921 
922 
923 	spin_lock_irqsave(&card->lock, flags);
924 
925 	txoff = (dma_addr_t)b1dma_readl(card, AMCC_TXPTR)-card->dma->sendbuf.dmaaddr;
926 	txlen = b1dma_readl(card, AMCC_TXLEN);
927 
928 	rxoff = (dma_addr_t)b1dma_readl(card, AMCC_RXPTR)-card->dma->recvbuf.dmaaddr;
929 	rxlen = b1dma_readl(card, AMCC_RXLEN);
930 
931 	csr  = b1dma_readl(card, AMCC_INTCSR);
932 
933 	spin_unlock_irqrestore(&card->lock, flags);
934 
935 	seq_printf(m, "%-16s 0x%lx\n", "csr (cached)", (unsigned long)card->csr);
936 	seq_printf(m, "%-16s 0x%lx\n", "csr", (unsigned long)csr);
937 	seq_printf(m, "%-16s %lu\n", "txoff", (unsigned long)txoff);
938 	seq_printf(m, "%-16s %lu\n", "txlen", (unsigned long)txlen);
939 	seq_printf(m, "%-16s %lu\n", "rxoff", (unsigned long)rxoff);
940 	seq_printf(m, "%-16s %lu\n", "rxlen", (unsigned long)rxlen);
941 
942 	return 0;
943 }
944 
b1dmactl_proc_open(struct inode * inode,struct file * file)945 static int b1dmactl_proc_open(struct inode *inode, struct file *file)
946 {
947 	return single_open(file, b1dmactl_proc_show, PDE(inode)->data);
948 }
949 
950 const struct file_operations b1dmactl_proc_fops = {
951 	.owner		= THIS_MODULE,
952 	.open		= b1dmactl_proc_open,
953 	.read		= seq_read,
954 	.llseek		= seq_lseek,
955 	.release	= single_release,
956 };
957 EXPORT_SYMBOL(b1dmactl_proc_fops);
958 
959 /* ------------------------------------------------------------- */
960 
961 EXPORT_SYMBOL(b1dma_reset);
962 EXPORT_SYMBOL(t1pci_detect);
963 EXPORT_SYMBOL(b1pciv4_detect);
964 EXPORT_SYMBOL(b1dma_interrupt);
965 
966 EXPORT_SYMBOL(b1dma_load_firmware);
967 EXPORT_SYMBOL(b1dma_reset_ctr);
968 EXPORT_SYMBOL(b1dma_register_appl);
969 EXPORT_SYMBOL(b1dma_release_appl);
970 EXPORT_SYMBOL(b1dma_send_message);
971 
b1dma_init(void)972 static int __init b1dma_init(void)
973 {
974 	char *p;
975 	char rev[32];
976 
977 	if ((p = strchr(revision, ':')) != NULL && p[1]) {
978 		strlcpy(rev, p + 2, sizeof(rev));
979 		if ((p = strchr(rev, '$')) != NULL && p > rev)
980 		   *(p-1) = 0;
981 	} else
982 		strcpy(rev, "1.0");
983 
984 	printk(KERN_INFO "b1dma: revision %s\n", rev);
985 
986 	return 0;
987 }
988 
b1dma_exit(void)989 static void __exit b1dma_exit(void)
990 {
991 }
992 
993 module_init(b1dma_init);
994 module_exit(b1dma_exit);
995