lanstreamer.c 63.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 *   lanstreamer.c -- driver for the IBM Auto LANStreamer PCI Adapter
 *
 *  Written By: Mike Sullivan, IBM Corporation
 *
 *  Copyright (C) 1999 IBM Corporation
 *
 *  Linux driver for IBM PCI tokenring cards based on the LanStreamer MPC
 *  chipset. 
 *
 *  This driver is based on the olympic driver for IBM PCI TokenRing cards (Pit/Pit-Phy/Olympic
 *  chipsets) written  by:
 *      1999 Peter De Schrijver All Rights Reserved
 *	1999 Mike Phillips (phillim@amtrak.com)
 *
 *  Base Driver Skeleton:
 *      Written 1993-94 by Donald Becker.
 *
 *      Copyright 1993 United States Government as represented by the
 *      Director, National Security Agency.
 *
 * This program is free software; you can redistribute it and/or modify      
 * it under the terms of the GNU General Public License as published by      
 * the Free Software Foundation; either version 2 of the License, or         
 * (at your option) any later version.                                       
 *                                                                           
 * This program is distributed in the hope that it will be useful,           
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             
 * GNU General Public License for more details.                              
 *                                                                           
 * NO WARRANTY                                                               
 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR        
 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT      
 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,      
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is    
 * solely responsible for determining the appropriateness of using and       
 * distributing the Program and assumes all risks associated with its        
 * exercise of rights under this Agreement, including but not limited to     
 * the risks and costs of program errors, damage to or loss of data,         
 * programs or equipment, and unavailability or interruption of operations.  
 *                                                                           
 * DISCLAIMER OF LIABILITY                                                   
 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY   
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL        
 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND   
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR     
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    
 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED  
 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES             
 *                                                                           
 * You should have received a copy of the GNU General Public License         
 * along with this program; if not, write to the Free Software               
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 *                                                                           
 * 
 *  12/10/99 - Alpha Release 0.1.0
 *            First release to the public
 *  03/03/00 - Merged to kernel, indented -kr -i8 -bri0, fixed some missing
 *		malloc free checks, reviewed code. <alan@redhat.com>
 *  03/13/00 - Added spinlocks for smp
 *  03/08/01 - Added support for module_init() and module_exit()
 *  08/15/01 - Added ioctl() functionality for debugging, changed netif_*_queue
 *             calls and other incorrectness - Kent Yoder <yoder1@us.ibm.com>
 *  11/05/01 - Restructured the interrupt function, added delays, reduced the 
 *             the number of TX descriptors to 1, which together can prevent 
 *             the card from locking up the box - <yoder1@us.ibm.com>
 *  09/27/02 - New PCI interface + bug fix. - <yoder1@us.ibm.com>
 *  11/13/02 - Removed free_irq calls which could cause a hang, added
 *	       netif_carrier_{on|off} - <yoder1@us.ibm.com>
 *  
 *  To Do:
 *
 *
 *  If Problems do Occur
 *  Most problems can be rectified by either closing and opening the interface
 *  (ifconfig down and up) or rmmod and insmod'ing the driver (a bit difficult
 *  if compiled into the kernel).
 */

/* Change STREAMER_DEBUG to 1 to get verbose, and I mean really verbose, messages */

#define STREAMER_DEBUG 0
#define STREAMER_DEBUG_PACKETS 0

/* Change STREAMER_NETWORK_MONITOR to receive mac frames through the arb channel.
 * Will also create a /proc/net/streamer_tr entry if proc_fs is compiled into the
 * kernel.
 * Intended to be used to create a ring-error reporting network module 
 * i.e. it will give you the source address of beaconers on the ring 
 */

#define STREAMER_NETWORK_MONITOR 0

/* #define CONFIG_PROC_FS */

/*
 *  Allow or disallow ioctl's for debugging
 */

#define STREAMER_IOCTL 0

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/timer.h>
#include <linux/in.h>
#include <linux/ioport.h>
#include <linux/string.h>
#include <linux/proc_fs.h>
#include <linux/ptrace.h>
#include <linux/skbuff.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/trdevice.h>
#include <linux/stddef.h>
#include <linux/init.h>
#include <linux/pci.h>
120
#include <linux/dma-mapping.h>
Linus Torvalds's avatar
Linus Torvalds committed
121
122
#include <linux/spinlock.h>
#include <linux/bitops.h>
123
#include <linux/jiffies.h>
Linus Torvalds's avatar
Linus Torvalds committed
124

125
#include <net/net_namespace.h>
Linus Torvalds's avatar
Linus Torvalds committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <net/checksum.h>

#include <asm/io.h>
#include <asm/system.h>

#include "lanstreamer.h"

#if (BITS_PER_LONG == 64)
#error broken on 64-bit: stores pointer to rx_ring->buffer in 32-bit int
#endif


/* I've got to put some intelligence into the version number so that Peter and I know
 * which version of the code somebody has got. 
 * Version Number = a.b.c.d  where a.b.c is the level of code and d is the latest author.
 * So 0.0.1.pds = Peter, 0.0.1.mlp = Mike
 * 
 * Official releases will only have an a.b.c version number format.
 */

static char version[] = "LanStreamer.c v0.4.0 03/08/01 - Mike Sullivan\n"
                        "              v0.5.3 11/13/02 - Kent Yoder";

static struct pci_device_id streamer_pci_tbl[] = {
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_TR, PCI_ANY_ID, PCI_ANY_ID,},
	{}	/* terminating entry */
};
MODULE_DEVICE_TABLE(pci,streamer_pci_tbl);


static char *open_maj_error[] = {
	"No error", "Lobe Media Test", "Physical Insertion",
	"Address Verification", "Neighbor Notification (Ring Poll)",
	"Request Parameters", "FDX Registration Request",
	"FDX Lobe Media Test", "FDX Duplicate Address Check",
	"Unknown stage"
};

static char *open_min_error[] = {
	"No error", "Function Failure", "Signal Lost", "Wire Fault",
	"Ring Speed Mismatch", "Timeout", "Ring Failure", "Ring Beaconing",
	"Duplicate Node Address", "Request Parameters", "Remove Received",
	"Reserved", "Reserved", "No Monitor Detected for RPL",
	"Monitor Contention failer for RPL", "FDX Protocol Error"
};

/* Module paramters */

/* Ring Speed 0,4,16
 * 0 = Autosense         
 * 4,16 = Selected speed only, no autosense
 * This allows the card to be the first on the ring
 * and become the active monitor.
 *
 * WARNING: Some hubs will allow you to insert
 * at the wrong speed
 */

static int ringspeed[STREAMER_MAX_ADAPTERS] = { 0, };

module_param_array(ringspeed, int, NULL, 0);

/* Packet buffer size */

static int pkt_buf_sz[STREAMER_MAX_ADAPTERS] = { 0, };

module_param_array(pkt_buf_sz, int, NULL, 0);

/* Message Level */

static int message_level[STREAMER_MAX_ADAPTERS] = { 1, };

module_param_array(message_level, int, NULL, 0);

#if STREAMER_IOCTL
static int streamer_ioctl(struct net_device *, struct ifreq *, int);
#endif

static int streamer_reset(struct net_device *dev);
static int streamer_open(struct net_device *dev);
static int streamer_xmit(struct sk_buff *skb, struct net_device *dev);
static int streamer_close(struct net_device *dev);
static void streamer_set_rx_mode(struct net_device *dev);
209
static irqreturn_t streamer_interrupt(int irq, void *dev_id);
Linus Torvalds's avatar
Linus Torvalds committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
static struct net_device_stats *streamer_get_stats(struct net_device *dev);
static int streamer_set_mac_address(struct net_device *dev, void *addr);
static void streamer_arb_cmd(struct net_device *dev);
static int streamer_change_mtu(struct net_device *dev, int mtu);
static void streamer_srb_bh(struct net_device *dev);
static void streamer_asb_bh(struct net_device *dev);
#if STREAMER_NETWORK_MONITOR
#ifdef CONFIG_PROC_FS
static int streamer_proc_info(char *buffer, char **start, off_t offset,
			      int length, int *eof, void *data);
static int sprintf_info(char *buffer, struct net_device *dev);
struct streamer_private *dev_streamer=NULL;
#endif
#endif

static int __devinit streamer_init_one(struct pci_dev *pdev,
				       const struct pci_device_id *ent)
{
	struct net_device *dev;
	struct streamer_private *streamer_priv;
	unsigned long pio_start, pio_end, pio_flags, pio_len;
	unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
	int rc = 0;
	static int card_no=-1;
	u16 pcr;

#if STREAMER_DEBUG
	printk("lanstreamer::streamer_init_one, entry pdev %p\n",pdev);
#endif

	card_no++;
	dev = alloc_trdev(sizeof(*streamer_priv));
	if (dev==NULL) {
		printk(KERN_ERR "lanstreamer: out of memory.\n");
		return -ENOMEM;
	}

247
	streamer_priv = netdev_priv(dev);
Linus Torvalds's avatar
Linus Torvalds committed
248
249
250
251

#if STREAMER_NETWORK_MONITOR
#ifdef CONFIG_PROC_FS
	if (!dev_streamer)
252
		create_proc_read_entry("streamer_tr", 0, init_net.proc_net,
Linus Torvalds's avatar
Linus Torvalds committed
253
254
255
256
257
258
					streamer_proc_info, NULL); 
	streamer_priv->next = dev_streamer;
	dev_streamer = streamer_priv;
#endif
#endif

259
	rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
Linus Torvalds's avatar
Linus Torvalds committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
	if (rc) {
		printk(KERN_ERR "%s: No suitable PCI mapping available.\n",
				dev->name);
		rc = -ENODEV;
		goto err_out;
	}

	rc = pci_enable_device(pdev);
	if (rc) {
		printk(KERN_ERR "lanstreamer: unable to enable pci device\n");
		rc=-EIO;
		goto err_out;
	}

	pci_set_master(pdev);

	rc = pci_set_mwi(pdev);
	if (rc) {
		printk(KERN_ERR "lanstreamer: unable to enable MWI on pci device\n");
		goto err_out_disable;
	}

	pio_start = pci_resource_start(pdev, 0);
	pio_end = pci_resource_end(pdev, 0);
	pio_flags = pci_resource_flags(pdev, 0);
	pio_len = pci_resource_len(pdev, 0);

	mmio_start = pci_resource_start(pdev, 1);
	mmio_end = pci_resource_end(pdev, 1);
	mmio_flags = pci_resource_flags(pdev, 1);
	mmio_len = pci_resource_len(pdev, 1);

#if STREAMER_DEBUG
	printk("lanstreamer: pio_start %x pio_end %x pio_len %x pio_flags %x\n",
		pio_start, pio_end, pio_len, pio_flags);
	printk("lanstreamer: mmio_start %x mmio_end %x mmio_len %x mmio_flags %x\n",
		mmio_start, mmio_end, mmio_flags, mmio_len);
#endif

	if (!request_region(pio_start, pio_len, "lanstreamer")) {
		printk(KERN_ERR "lanstreamer: unable to get pci io addr %lx\n",
			pio_start);
		rc= -EBUSY;
		goto err_out_mwi;
	}

	if (!request_mem_region(mmio_start, mmio_len, "lanstreamer")) {
		printk(KERN_ERR "lanstreamer: unable to get pci mmio addr %lx\n",
			mmio_start);
		rc= -EBUSY;
		goto err_out_free_pio;
	}

	streamer_priv->streamer_mmio=ioremap(mmio_start, mmio_len);
	if (streamer_priv->streamer_mmio == NULL) {
		printk(KERN_ERR "lanstreamer: unable to remap MMIO %lx\n",
			mmio_start);
		rc= -EIO;
		goto err_out_free_mmio;
	}

	init_waitqueue_head(&streamer_priv->srb_wait);
	init_waitqueue_head(&streamer_priv->trb_wait);

	dev->open = &streamer_open;
	dev->hard_start_xmit = &streamer_xmit;
	dev->change_mtu = &streamer_change_mtu;
	dev->stop = &streamer_close;
#if STREAMER_IOCTL
	dev->do_ioctl = &streamer_ioctl;
#else
	dev->do_ioctl = NULL;
#endif
	dev->set_multicast_list = &streamer_set_rx_mode;
	dev->get_stats = &streamer_get_stats;
	dev->set_mac_address = &streamer_set_mac_address;
	dev->irq = pdev->irq;
	dev->base_addr=pio_start;
	SET_NETDEV_DEV(dev, &pdev->dev);

	streamer_priv->streamer_card_name = (char *)pdev->resource[0].name;
	streamer_priv->pci_dev = pdev;

	if ((pkt_buf_sz[card_no] < 100) || (pkt_buf_sz[card_no] > 18000))
		streamer_priv->pkt_buf_sz = PKT_BUF_SZ;
	else
		streamer_priv->pkt_buf_sz = pkt_buf_sz[card_no];

	streamer_priv->streamer_ring_speed = ringspeed[card_no];
	streamer_priv->streamer_message_level = message_level[card_no];

	pci_set_drvdata(pdev, dev);

	spin_lock_init(&streamer_priv->streamer_lock);

	pci_read_config_word (pdev, PCI_COMMAND, &pcr);
	pcr |= PCI_COMMAND_SERR;
	pci_write_config_word (pdev, PCI_COMMAND, pcr);

	printk("%s \n", version);
	printk("%s: %s. I/O at %hx, MMIO at %p, using irq %d\n",dev->name,
		streamer_priv->streamer_card_name,
		(unsigned int) dev->base_addr,
		streamer_priv->streamer_mmio, 
		dev->irq);

	if (streamer_reset(dev))
		goto err_out_unmap;

	rc = register_netdev(dev);
	if (rc)
		goto err_out_unmap;
	return 0;

err_out_unmap:
	iounmap(streamer_priv->streamer_mmio);
err_out_free_mmio:
	release_mem_region(mmio_start, mmio_len);
err_out_free_pio:
	release_region(pio_start, pio_len);
err_out_mwi:
	pci_clear_mwi(pdev);
err_out_disable:
	pci_disable_device(pdev);
err_out:
	free_netdev(dev);
#if STREAMER_DEBUG
	printk("lanstreamer: Exit error %x\n",rc);
#endif
	return rc;
}

static void __devexit streamer_remove_one(struct pci_dev *pdev)
{
	struct net_device *dev=pci_get_drvdata(pdev);
	struct streamer_private *streamer_priv;

#if STREAMER_DEBUG
	printk("lanstreamer::streamer_remove_one entry pdev %p\n",pdev);
#endif

	if (dev == NULL) {
		printk(KERN_ERR "lanstreamer::streamer_remove_one, ERROR dev is NULL\n");
		return;
	}

406
	streamer_priv=netdev_priv(dev);
Linus Torvalds's avatar
Linus Torvalds committed
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	if (streamer_priv == NULL) {
		printk(KERN_ERR "lanstreamer::streamer_remove_one, ERROR dev->priv is NULL\n");
		return;
	}

#if STREAMER_NETWORK_MONITOR
#ifdef CONFIG_PROC_FS
	{
		struct streamer_private **p, **next;

		for (p = &dev_streamer; *p; p = next) {
			next = &(*p)->next;
			if (*p == streamer_priv) {
				*p = *next;
				break;
			}
		}
		if (!dev_streamer)
425
			remove_proc_entry("streamer_tr", init_net.proc_net);
Linus Torvalds's avatar
Linus Torvalds committed
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
	}
#endif
#endif

	unregister_netdev(dev);
	iounmap(streamer_priv->streamer_mmio);
	release_mem_region(pci_resource_start(pdev, 1), pci_resource_len(pdev,1));
	release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev,0));
	pci_clear_mwi(pdev);
	pci_disable_device(pdev);
	free_netdev(dev);
	pci_set_drvdata(pdev, NULL);
}


static int streamer_reset(struct net_device *dev)
{
	struct streamer_private *streamer_priv;
	__u8 __iomem *streamer_mmio;
	unsigned long t;
	unsigned int uaa_addr;
	struct sk_buff *skb = NULL;
	__u16 misr;
449
450
451
#if STREAMER_DEBUG
	DECLARE_MAC_BUF(mac);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
452

453
	streamer_priv = netdev_priv(dev);
Linus Torvalds's avatar
Linus Torvalds committed
454
455
456
457
458
	streamer_mmio = streamer_priv->streamer_mmio;

	writew(readw(streamer_mmio + BCTL) | BCTL_SOFTRESET, streamer_mmio + BCTL);
	t = jiffies;
	/* Hold soft reset bit for a while */
459
	ssleep(1);
Linus Torvalds's avatar
Linus Torvalds committed
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
	
	writew(readw(streamer_mmio + BCTL) & ~BCTL_SOFTRESET,
	       streamer_mmio + BCTL);

#if STREAMER_DEBUG
	printk("BCTL: %x\n", readw(streamer_mmio + BCTL));
	printk("GPR: %x\n", readw(streamer_mmio + GPR));
	printk("SISRMASK: %x\n", readw(streamer_mmio + SISR_MASK));
#endif
	writew(readw(streamer_mmio + BCTL) | (BCTL_RX_FIFO_8 | BCTL_TX_FIFO_8), streamer_mmio + BCTL );

	if (streamer_priv->streamer_ring_speed == 0) {	/* Autosense */
		writew(readw(streamer_mmio + GPR) | GPR_AUTOSENSE,
		       streamer_mmio + GPR);
		if (streamer_priv->streamer_message_level)
			printk(KERN_INFO "%s: Ringspeed autosense mode on\n",
			       dev->name);
	} else if (streamer_priv->streamer_ring_speed == 16) {
		if (streamer_priv->streamer_message_level)
			printk(KERN_INFO "%s: Trying to open at 16 Mbps as requested\n",
			       dev->name);
		writew(GPR_16MBPS, streamer_mmio + GPR);
	} else if (streamer_priv->streamer_ring_speed == 4) {
		if (streamer_priv->streamer_message_level)
			printk(KERN_INFO "%s: Trying to open at 4 Mbps as requested\n",
			       dev->name);
		writew(0, streamer_mmio + GPR);
	}

	skb = dev_alloc_skb(streamer_priv->pkt_buf_sz);
	if (!skb) {
		printk(KERN_INFO "%s: skb allocation for diagnostics failed...proceeding\n",
		       dev->name);
	} else {
	        struct streamer_rx_desc *rx_ring;
                u8 *data;

		rx_ring=(struct streamer_rx_desc *)skb->data;
		data=((u8 *)skb->data)+sizeof(struct streamer_rx_desc);
		rx_ring->forward=0;
		rx_ring->status=0;
		rx_ring->buffer=cpu_to_le32(pci_map_single(streamer_priv->pci_dev, data, 
							512, PCI_DMA_FROMDEVICE));
		rx_ring->framelen_buflen=512; 
		writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev, rx_ring, 512, PCI_DMA_FROMDEVICE)),
			streamer_mmio+RXBDA);
	}

#if STREAMER_DEBUG
	printk("GPR = %x\n", readw(streamer_mmio + GPR));
#endif
	/* start solo init */
	writew(SISR_MI, streamer_mmio + SISR_MASK_SUM);

	while (!((readw(streamer_mmio + SISR)) & SISR_SRB_REPLY)) {
515
		msleep_interruptible(100);
516
		if (time_after(jiffies, t + 40 * HZ)) {
Linus Torvalds's avatar
Linus Torvalds committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
			printk(KERN_ERR
			       "IBM PCI tokenring card not responding\n");
			release_region(dev->base_addr, STREAMER_IO_SPACE);
			if (skb)
				dev_kfree_skb(skb);
			return -1;
		}
	}
	writew(~SISR_SRB_REPLY, streamer_mmio + SISR_RUM);
	misr = readw(streamer_mmio + MISR_RUM);
	writew(~misr, streamer_mmio + MISR_RUM);

	if (skb)
		dev_kfree_skb(skb);	/* release skb used for diagnostics */

#if STREAMER_DEBUG
	printk("LAPWWO: %x, LAPA: %x LAPE:  %x\n",
	       readw(streamer_mmio + LAPWWO), readw(streamer_mmio + LAPA),
	       readw(streamer_mmio + LAPE));
#endif

#if STREAMER_DEBUG
	{
		int i;
		writew(readw(streamer_mmio + LAPWWO),
		       streamer_mmio + LAPA);
		printk("initialization response srb dump: ");
		for (i = 0; i < 10; i++)
			printk("%x:",
			       ntohs(readw(streamer_mmio + LAPDINC)));
		printk("\n");
	}
#endif

	writew(readw(streamer_mmio + LAPWWO) + 6, streamer_mmio + LAPA);
	if (readw(streamer_mmio + LAPD)) {
		printk(KERN_INFO "tokenring card initialization failed. errorcode : %x\n",
		       ntohs(readw(streamer_mmio + LAPD)));
		release_region(dev->base_addr, STREAMER_IO_SPACE);
		return -1;
	}

	writew(readw(streamer_mmio + LAPWWO) + 8, streamer_mmio + LAPA);
	uaa_addr = ntohs(readw(streamer_mmio + LAPDINC));
	readw(streamer_mmio + LAPDINC);	/* skip over Level.Addr field */
	streamer_priv->streamer_addr_table_addr = ntohs(readw(streamer_mmio + LAPDINC));
	streamer_priv->streamer_parms_addr = ntohs(readw(streamer_mmio + LAPDINC));

#if STREAMER_DEBUG
	printk("UAA resides at %x\n", uaa_addr);
#endif

	/* setup uaa area for access with LAPD */
	{
		int i;
		__u16 addr;
		writew(uaa_addr, streamer_mmio + LAPA);
		for (i = 0; i < 6; i += 2) {
		        addr=ntohs(readw(streamer_mmio+LAPDINC));
			dev->dev_addr[i]= (addr >> 8) & 0xff;
			dev->dev_addr[i+1]= addr & 0xff;
		}
#if STREAMER_DEBUG
580
581
		printk("Adapter address: %s\n",
		       print_mac(mac, dev->dev_addr));
Linus Torvalds's avatar
Linus Torvalds committed
582
583
584
585
586
587
588
#endif
	}
	return 0;
}

static int streamer_open(struct net_device *dev)
{
589
	struct streamer_private *streamer_priv = netdev_priv(dev);
Linus Torvalds's avatar
Linus Torvalds committed
590
591
592
593
594
595
596
597
598
599
600
601
	__u8 __iomem *streamer_mmio = streamer_priv->streamer_mmio;
	unsigned long flags;
	char open_error[255];
	int i, open_finished = 1;
	__u16 srb_word;
	__u16 srb_open;
	int rc;

	if (readw(streamer_mmio+BMCTL_SUM) & BMCTL_RX_ENABLED) {
	        rc=streamer_reset(dev);
	}

602
	if (request_irq(dev->irq, &streamer_interrupt, IRQF_SHARED, "lanstreamer", dev)) {
Linus Torvalds's avatar
Linus Torvalds committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
		return -EAGAIN;
	}
#if STREAMER_DEBUG
	printk("BMCTL: %x\n", readw(streamer_mmio + BMCTL_SUM));
	printk("pending ints: %x\n", readw(streamer_mmio + SISR));
#endif

	writew(SISR_MI | SISR_SRB_REPLY, streamer_mmio + SISR_MASK);	/* more ints later, doesn't stop arb cmd interrupt */
	writew(LISR_LIE, streamer_mmio + LISR);	/* more ints later */

	/* adapter is closed, so SRB is pointed to by LAPWWO */
	writew(readw(streamer_mmio + LAPWWO), streamer_mmio + LAPA);

#if STREAMER_DEBUG
	printk("LAPWWO: %x, LAPA: %x\n", readw(streamer_mmio + LAPWWO),
	       readw(streamer_mmio + LAPA));
	printk("LAPE: %x\n", readw(streamer_mmio + LAPE));
	printk("SISR Mask = %04x\n", readw(streamer_mmio + SISR_MASK));
#endif
	do {
		int i;

		for (i = 0; i < SRB_COMMAND_SIZE; i += 2) {
			writew(0, streamer_mmio + LAPDINC);
		}

		writew(readw(streamer_mmio+LAPWWO),streamer_mmio+LAPA);
		writew(htons(SRB_OPEN_ADAPTER<<8),streamer_mmio+LAPDINC) ; 	/* open */
		writew(htons(STREAMER_CLEAR_RET_CODE<<8),streamer_mmio+LAPDINC);
		writew(STREAMER_CLEAR_RET_CODE, streamer_mmio + LAPDINC);

		writew(readw(streamer_mmio + LAPWWO) + 8, streamer_mmio + LAPA);
#if STREAMER_NETWORK_MONITOR
		/* If Network Monitor, instruct card to copy MAC frames through the ARB */
		writew(htons(OPEN_ADAPTER_ENABLE_FDX | OPEN_ADAPTER_PASS_ADC_MAC | OPEN_ADAPTER_PASS_ATT_MAC | OPEN_ADAPTER_PASS_BEACON), streamer_mmio + LAPDINC);	/* offset 8 word contains open options */
#else
		writew(htons(OPEN_ADAPTER_ENABLE_FDX), streamer_mmio + LAPDINC);	/* Offset 8 word contains Open.Options */
#endif

		if (streamer_priv->streamer_laa[0]) {
			writew(readw(streamer_mmio + LAPWWO) + 12, streamer_mmio + LAPA);
			writew(htons((streamer_priv->streamer_laa[0] << 8) | 
				     streamer_priv->streamer_laa[1]),streamer_mmio+LAPDINC);
			writew(htons((streamer_priv->streamer_laa[2] << 8) | 
				     streamer_priv->streamer_laa[3]),streamer_mmio+LAPDINC);
			writew(htons((streamer_priv->streamer_laa[4] << 8) | 
				     streamer_priv->streamer_laa[5]),streamer_mmio+LAPDINC);
			memcpy(dev->dev_addr, streamer_priv->streamer_laa, dev->addr_len);
		}

		/* save off srb open offset */
		srb_open = readw(streamer_mmio + LAPWWO);
#if STREAMER_DEBUG
		writew(readw(streamer_mmio + LAPWWO),
		       streamer_mmio + LAPA);
		printk("srb open request: \n");
		for (i = 0; i < 16; i++) {
			printk("%x:", ntohs(readw(streamer_mmio + LAPDINC)));
		}
		printk("\n");
#endif
		spin_lock_irqsave(&streamer_priv->streamer_lock, flags);
		streamer_priv->srb_queued = 1;

		/* signal solo that SRB command has been issued */
		writew(LISR_SRB_CMD, streamer_mmio + LISR_SUM);
		spin_unlock_irqrestore(&streamer_priv->streamer_lock, flags);

		while (streamer_priv->srb_queued) {
			interruptible_sleep_on_timeout(&streamer_priv->srb_wait, 5 * HZ);
			if (signal_pending(current)) {
				printk(KERN_WARNING "%s: SRB timed out.\n", dev->name);
				printk(KERN_WARNING "SISR=%x MISR=%x, LISR=%x\n",
				       readw(streamer_mmio + SISR),
				       readw(streamer_mmio + MISR_RUM),
				       readw(streamer_mmio + LISR));
				streamer_priv->srb_queued = 0;
				break;
			}
		}

#if STREAMER_DEBUG
		printk("SISR_MASK: %x\n", readw(streamer_mmio + SISR_MASK));
		printk("srb open response:\n");
		writew(srb_open, streamer_mmio + LAPA);
		for (i = 0; i < 10; i++) {
			printk("%x:",
			       ntohs(readw(streamer_mmio + LAPDINC)));
		}
#endif

		/* If we get the same return response as we set, the interrupt wasn't raised and the open
		 * timed out.
		 */
		writew(srb_open + 2, streamer_mmio + LAPA);
		srb_word = ntohs(readw(streamer_mmio + LAPD)) >> 8;
		if (srb_word == STREAMER_CLEAR_RET_CODE) {
			printk(KERN_WARNING "%s: Adapter Open time out or error.\n",
			       dev->name);
			return -EIO;
		}

		if (srb_word != 0) {
			if (srb_word == 0x07) {
				if (!streamer_priv->streamer_ring_speed && open_finished) {	/* Autosense , first time around */
					printk(KERN_WARNING "%s: Retrying at different ring speed \n",
					       dev->name);
					open_finished = 0;
				} else {
					__u16 error_code;

					writew(srb_open + 6, streamer_mmio + LAPA);
					error_code = ntohs(readw(streamer_mmio + LAPD));
					strcpy(open_error, open_maj_error[(error_code & 0xf0) >> 4]);
					strcat(open_error, " - ");
					strcat(open_error, open_min_error[(error_code & 0x0f)]);

					if (!streamer_priv->streamer_ring_speed
					    && ((error_code & 0x0f) == 0x0d)) 
					{
						printk(KERN_WARNING "%s: Tried to autosense ring speed with no monitors present\n", dev->name);
						printk(KERN_WARNING "%s: Please try again with a specified ring speed \n", dev->name);
						free_irq(dev->irq, dev);
						return -EIO;
					}

					printk(KERN_WARNING "%s: %s\n",
					       dev->name, open_error);
					free_irq(dev->irq, dev);
					return -EIO;

				}	/* if autosense && open_finished */
			} else {
				printk(KERN_WARNING "%s: Bad OPEN response: %x\n",
				       dev->name, srb_word);
				free_irq(dev->irq, dev);
				return -EIO;
			}
		} else
			open_finished = 1;
	} while (!(open_finished));	/* Will only loop if ring speed mismatch re-open attempted && autosense is on */

	writew(srb_open + 18, streamer_mmio + LAPA);
	srb_word=ntohs(readw(streamer_mmio+LAPD)) >> 8;
	if (srb_word & (1 << 3))
		if (streamer_priv->streamer_message_level)
			printk(KERN_INFO "%s: Opened in FDX Mode\n", dev->name);

	if (srb_word & 1)
		streamer_priv->streamer_ring_speed = 16;
	else
		streamer_priv->streamer_ring_speed = 4;

	if (streamer_priv->streamer_message_level)
		printk(KERN_INFO "%s: Opened in %d Mbps mode\n", 
			dev->name,
			streamer_priv->streamer_ring_speed);

	writew(srb_open + 8, streamer_mmio + LAPA);
	streamer_priv->asb = ntohs(readw(streamer_mmio + LAPDINC));
	streamer_priv->srb = ntohs(readw(streamer_mmio + LAPDINC));
	streamer_priv->arb = ntohs(readw(streamer_mmio + LAPDINC));
	readw(streamer_mmio + LAPDINC);	/* offset 14 word is rsvd */
	streamer_priv->trb = ntohs(readw(streamer_mmio + LAPDINC));

	streamer_priv->streamer_receive_options = 0x00;
	streamer_priv->streamer_copy_all_options = 0;

	/* setup rx ring */
	/* enable rx channel */
	writew(~BMCTL_RX_DIS, streamer_mmio + BMCTL_RUM);

	/* setup rx descriptors */
	streamer_priv->streamer_rx_ring=
	    kmalloc( sizeof(struct streamer_rx_desc)*
		     STREAMER_RX_RING_SIZE,GFP_KERNEL);
	if (!streamer_priv->streamer_rx_ring) {
	    printk(KERN_WARNING "%s ALLOC of streamer rx ring FAILED!!\n",dev->name);
	    return -EIO;
	}

	for (i = 0; i < STREAMER_RX_RING_SIZE; i++) {
		struct sk_buff *skb;

		skb = dev_alloc_skb(streamer_priv->pkt_buf_sz);
		if (skb == NULL)
			break;

		skb->dev = dev;

		streamer_priv->streamer_rx_ring[i].forward = 
			cpu_to_le32(pci_map_single(streamer_priv->pci_dev, &streamer_priv->streamer_rx_ring[i + 1],
					sizeof(struct streamer_rx_desc), PCI_DMA_FROMDEVICE));
		streamer_priv->streamer_rx_ring[i].status = 0;
		streamer_priv->streamer_rx_ring[i].buffer = 
			cpu_to_le32(pci_map_single(streamer_priv->pci_dev, skb->data,
					      streamer_priv->pkt_buf_sz, PCI_DMA_FROMDEVICE));
		streamer_priv->streamer_rx_ring[i].framelen_buflen = streamer_priv->pkt_buf_sz;
		streamer_priv->rx_ring_skb[i] = skb;
	}
	streamer_priv->streamer_rx_ring[STREAMER_RX_RING_SIZE - 1].forward =
				cpu_to_le32(pci_map_single(streamer_priv->pci_dev, &streamer_priv->streamer_rx_ring[0],
						sizeof(struct streamer_rx_desc), PCI_DMA_FROMDEVICE));

	if (i == 0) {
		printk(KERN_WARNING "%s: Not enough memory to allocate rx buffers. Adapter disabled\n", dev->name);
		free_irq(dev->irq, dev);
		return -EIO;
	}

	streamer_priv->rx_ring_last_received = STREAMER_RX_RING_SIZE - 1;	/* last processed rx status */

	writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev, &streamer_priv->streamer_rx_ring[0],
				sizeof(struct streamer_rx_desc), PCI_DMA_TODEVICE)), 
		streamer_mmio + RXBDA);
	writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev, &streamer_priv->streamer_rx_ring[STREAMER_RX_RING_SIZE - 1],
				sizeof(struct streamer_rx_desc), PCI_DMA_TODEVICE)), 
		streamer_mmio + RXLBDA);

	/* set bus master interrupt event mask */
	writew(MISR_RX_NOBUF | MISR_RX_EOF, streamer_mmio + MISR_MASK);


	/* setup tx ring */
	streamer_priv->streamer_tx_ring=kmalloc(sizeof(struct streamer_tx_desc)*
						STREAMER_TX_RING_SIZE,GFP_KERNEL);
	if (!streamer_priv->streamer_tx_ring) {
	    printk(KERN_WARNING "%s ALLOC of streamer_tx_ring FAILED\n",dev->name);
	    return -EIO;
	}

	writew(~BMCTL_TX2_DIS, streamer_mmio + BMCTL_RUM);	/* Enables TX channel 2 */
	for (i = 0; i < STREAMER_TX_RING_SIZE; i++) {
		streamer_priv->streamer_tx_ring[i].forward = cpu_to_le32(pci_map_single(streamer_priv->pci_dev, 
										&streamer_priv->streamer_tx_ring[i + 1],
										sizeof(struct streamer_tx_desc),
										PCI_DMA_TODEVICE));
		streamer_priv->streamer_tx_ring[i].status = 0;
		streamer_priv->streamer_tx_ring[i].bufcnt_framelen = 0;
		streamer_priv->streamer_tx_ring[i].buffer = 0;
		streamer_priv->streamer_tx_ring[i].buflen = 0;
		streamer_priv->streamer_tx_ring[i].rsvd1 = 0;
		streamer_priv->streamer_tx_ring[i].rsvd2 = 0;
		streamer_priv->streamer_tx_ring[i].rsvd3 = 0;
	}
	streamer_priv->streamer_tx_ring[STREAMER_TX_RING_SIZE - 1].forward =
					cpu_to_le32(pci_map_single(streamer_priv->pci_dev, &streamer_priv->streamer_tx_ring[0],
							sizeof(struct streamer_tx_desc), PCI_DMA_TODEVICE));

	streamer_priv->free_tx_ring_entries = STREAMER_TX_RING_SIZE;
	streamer_priv->tx_ring_free = 0;	/* next entry in tx ring to use */
	streamer_priv->tx_ring_last_status = STREAMER_TX_RING_SIZE - 1;

	/* set Busmaster interrupt event mask (handle receives on interrupt only */
	writew(MISR_TX2_EOF | MISR_RX_NOBUF | MISR_RX_EOF, streamer_mmio + MISR_MASK);
	/* set system event interrupt mask */
	writew(SISR_ADAPTER_CHECK | SISR_ARB_CMD | SISR_TRB_REPLY | SISR_ASB_FREE, streamer_mmio + SISR_MASK_SUM);

#if STREAMER_DEBUG
	printk("BMCTL: %x\n", readw(streamer_mmio + BMCTL_SUM));
	printk("SISR MASK: %x\n", readw(streamer_mmio + SISR_MASK));
#endif

#if STREAMER_NETWORK_MONITOR

	writew(streamer_priv->streamer_addr_table_addr, streamer_mmio + LAPA);
	printk("%s: Node Address: %04x:%04x:%04x\n", dev->name,
		ntohs(readw(streamer_mmio + LAPDINC)),
		ntohs(readw(streamer_mmio + LAPDINC)),
		ntohs(readw(streamer_mmio + LAPDINC)));
	readw(streamer_mmio + LAPDINC);
	readw(streamer_mmio + LAPDINC);
	printk("%s: Functional Address: %04x:%04x\n", dev->name,
		ntohs(readw(streamer_mmio + LAPDINC)),
		ntohs(readw(streamer_mmio + LAPDINC)));

	writew(streamer_priv->streamer_parms_addr + 4,
		streamer_mmio + LAPA);
	printk("%s: NAUN Address: %04x:%04x:%04x\n", dev->name,
		ntohs(readw(streamer_mmio + LAPDINC)),
		ntohs(readw(streamer_mmio + LAPDINC)),
		ntohs(readw(streamer_mmio + LAPDINC)));
#endif

	netif_start_queue(dev);
	netif_carrier_on(dev);
	return 0;
}

/*
 *	When we enter the rx routine we do not know how many frames have been 
 *	queued on the rx channel.  Therefore we start at the next rx status
 *	position and travel around the receive ring until we have completed
 *	all the frames.
 *
 *	This means that we may process the frame before we receive the end
 *	of frame interrupt. This is why we always test the status instead
 *	of blindly processing the next frame.
 *	
 */
static void streamer_rx(struct net_device *dev)
{
	struct streamer_private *streamer_priv =
906
	    netdev_priv(dev);
Linus Torvalds's avatar
Linus Torvalds committed
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
	__u8 __iomem *streamer_mmio = streamer_priv->streamer_mmio;
	struct streamer_rx_desc *rx_desc;
	int rx_ring_last_received, length, frame_length, buffer_cnt = 0;
	struct sk_buff *skb, *skb2;

	/* setup the next rx descriptor to be received */
	rx_desc = &streamer_priv->streamer_rx_ring[(streamer_priv->rx_ring_last_received + 1) & (STREAMER_RX_RING_SIZE - 1)];
	rx_ring_last_received = streamer_priv->rx_ring_last_received;

	while (rx_desc->status & 0x01000000) {	/* While processed descriptors are available */
		if (rx_ring_last_received != streamer_priv->rx_ring_last_received) 
		{
			printk(KERN_WARNING "RX Error 1 rx_ring_last_received not the same %x %x\n",
				rx_ring_last_received, streamer_priv->rx_ring_last_received);
		}
		streamer_priv->rx_ring_last_received = (streamer_priv->rx_ring_last_received + 1) & (STREAMER_RX_RING_SIZE - 1);
		rx_ring_last_received = streamer_priv->rx_ring_last_received;

		length = rx_desc->framelen_buflen & 0xffff;	/* buffer length */
		frame_length = (rx_desc->framelen_buflen >> 16) & 0xffff;

		if (rx_desc->status & 0x7E830000) {	/* errors */
			if (streamer_priv->streamer_message_level) {
				printk(KERN_WARNING "%s: Rx Error %x \n",
				       dev->name, rx_desc->status);
			}
		} else {	/* received without errors */
			if (rx_desc->status & 0x80000000) {	/* frame complete */
				buffer_cnt = 1;
				skb = dev_alloc_skb(streamer_priv->pkt_buf_sz);
			} else {
				skb = dev_alloc_skb(frame_length);
			}

			if (skb == NULL) 
			{
				printk(KERN_WARNING "%s: Not enough memory to copy packet to upper layers. \n",	dev->name);
				streamer_priv->streamer_stats.rx_dropped++;
			} else {	/* we allocated an skb OK */
				if (buffer_cnt == 1) {
					/* release the DMA mapping */
					pci_unmap_single(streamer_priv->pci_dev, 
						le32_to_cpu(streamer_priv->streamer_rx_ring[rx_ring_last_received].buffer),
						streamer_priv->pkt_buf_sz, 
						PCI_DMA_FROMDEVICE);
					skb2 = streamer_priv->rx_ring_skb[rx_ring_last_received];
#if STREAMER_DEBUG_PACKETS
					{
						int i;
						printk("streamer_rx packet print: skb->data2 %p  skb->head %p\n", skb2->data, skb2->head);
						for (i = 0; i < frame_length; i++) 
						{
							printk("%x:", skb2->data[i]);
							if (((i + 1) % 16) == 0)
								printk("\n");
						}
						printk("\n");
					}
#endif
					skb_put(skb2, length);
					skb2->protocol = tr_type_trans(skb2, dev);
					/* recycle this descriptor */
					streamer_priv->streamer_rx_ring[rx_ring_last_received].status = 0;
					streamer_priv->streamer_rx_ring[rx_ring_last_received].framelen_buflen = streamer_priv->pkt_buf_sz;
					streamer_priv->streamer_rx_ring[rx_ring_last_received].buffer = 
						cpu_to_le32(pci_map_single(streamer_priv->pci_dev, skb->data, streamer_priv->pkt_buf_sz,
								PCI_DMA_FROMDEVICE));
					streamer_priv->rx_ring_skb[rx_ring_last_received] = skb;
					/* place recycled descriptor back on the adapter */
					writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev, 
									&streamer_priv->streamer_rx_ring[rx_ring_last_received],
									sizeof(struct streamer_rx_desc), PCI_DMA_FROMDEVICE)),
						streamer_mmio + RXLBDA);
					/* pass the received skb up to the protocol */
					netif_rx(skb2);
				} else {
					do {	/* Walk the buffers */
						pci_unmap_single(streamer_priv->pci_dev, le32_to_cpu(rx_desc->buffer), length, PCI_DMA_FROMDEVICE), 
						memcpy(skb_put(skb, length), (void *)rx_desc->buffer, length);	/* copy this fragment */
						streamer_priv->streamer_rx_ring[rx_ring_last_received].status = 0;
						streamer_priv->streamer_rx_ring[rx_ring_last_received].framelen_buflen = streamer_priv->pkt_buf_sz;
						
						/* give descriptor back to the adapter */
						writel(cpu_to_le32(pci_map_single(streamer_priv->pci_dev, 
									&streamer_priv->streamer_rx_ring[rx_ring_last_received],
									length, PCI_DMA_FROMDEVICE)), 
							streamer_mmio + RXLBDA);

						if (rx_desc->status & 0x80000000)
							break;	/* this descriptor completes the frame */

						/* else get the next pending descriptor */
						if (rx_ring_last_received!= streamer_priv->rx_ring_last_received)
						{