1 /*
2 **
3 **  RCpci45.c
4 **
5 **
6 **
7 **  ---------------------------------------------------------------------
8 **  ---     Copyright (c) 1998, 1999, RedCreek Communications Inc.    ---
9 **  ---                   All rights reserved.                        ---
10 **  ---------------------------------------------------------------------
11 **
12 ** Written by Pete Popov and Brian Moyle.
13 **
14 ** Known Problems
15 **
16 ** None known at this time.
17 **
18 **  This program is free software; you can redistribute it and/or modify
19 **  it under the terms of the GNU General Public License as published by
20 **  the Free Software Foundation; either version 2 of the License, or
21 **  (at your option) any later version.
22 
23 **  This program is distributed in the hope that it will be useful,
24 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
25 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 **  GNU General Public License for more details.
27 
28 **  You should have received a copy of the GNU General Public License
29 **  along with this program; if not, write to the Free Software
30 **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 **
32 **  Pete Popov, Oct 2001: Fixed a few bugs to make the driver functional
33 **  again. Note that this card is not supported or manufactured by
34 **  RedCreek anymore.
35 **
36 **  Rasmus Andersen, December 2000: Converted to new PCI API and general
37 **  cleanup.
38 **
39 **  Pete Popov, January 11,99: Fixed a couple of 2.1.x problems
40 **  (virt_to_bus() not called), tested it under 2.2pre5 (as a module), and
41 **  added a #define(s) to enable the use of the same file for both, the 2.0.x
42 **  kernels as well as the 2.1.x.
43 **
44 **  Ported to 2.1.x by Alan Cox 1998/12/9.
45 **
46 **  Sometime in mid 1998, written by Pete Popov and Brian Moyle.
47 **
48 ***************************************************************************/
49 
50 #include <linux/module.h>
51 #include <linux/kernel.h>
52 #include <linux/sched.h>
53 #include <linux/string.h>
54 #include <linux/ptrace.h>
55 #include <linux/errno.h>
56 #include <linux/in.h>
57 #include <linux/init.h>
58 #include <linux/ioport.h>
59 #include <linux/slab.h>
60 #include <linux/interrupt.h>
61 #include <linux/pci.h>
62 #include <linux/timer.h>
63 #include <asm/irq.h>		/* For NR_IRQS only. */
64 #include <asm/bitops.h>
65 #include <asm/uaccess.h>
66 
67 static char version[] __initdata =
68     "RedCreek Communications PCI linux driver version 2.20\n";
69 
70 #define RC_LINUX_MODULE
71 #include "rclanmtl.h"
72 #include "rcif.h"
73 
74 #define RUN_AT(x) (jiffies + (x))
75 
76 #define NEW_MULTICAST
77 
78 #define MAX_ETHER_SIZE        1520
79 #define MAX_NMBR_RCV_BUFFERS    96
80 #define RC_POSTED_BUFFERS_LOW_MARK MAX_NMBR_RCV_BUFFERS-16
81 #define BD_SIZE 3		/* Bucket Descriptor size */
82 #define BD_LEN_OFFSET 2		/* Bucket Descriptor offset to length field */
83 
84 /* RedCreek LAN device Target ID */
85 #define RC_LAN_TARGET_ID  0x10
86 /* RedCreek's OSM default LAN receive Initiator */
87 #define DEFAULT_RECV_INIT_CONTEXT  0xA17
88 
89 /* minimum msg buffer size needed by the card
90  * Note that the size of this buffer is hard code in the
91  * ipsec card's firmware. Thus, the size MUST be a minimum
92  * of 16K. Otherwise the card will end up using memory
93  * that does not belong to it.
94  */
95 #define MSG_BUF_SIZE  16384
96 
97 static U32 DriverControlWord;
98 
99 static void rc_timer (unsigned long);
100 
101 static int RCopen (struct net_device *);
102 static int RC_xmit_packet (struct sk_buff *, struct net_device *);
103 static void RCinterrupt (int, void *, struct pt_regs *);
104 static int RCclose (struct net_device *dev);
105 static struct net_device_stats *RCget_stats (struct net_device *);
106 static int RCioctl (struct net_device *, struct ifreq *, int);
107 static int RCconfig (struct net_device *, struct ifmap *);
108 static void RCxmit_callback (U32, U16, PU32, struct net_device *);
109 static void RCrecv_callback (U32, U8, U32, PU32, struct net_device *);
110 static void RCreset_callback (U32, U32, U32, struct net_device *);
111 static void RCreboot_callback (U32, U32, U32, struct net_device *);
112 static int RC_allocate_and_post_buffers (struct net_device *, int);
113 
114 static struct pci_device_id rcpci45_pci_table[] __devinitdata = {
115 	{ PCI_VENDOR_ID_REDCREEK, PCI_DEVICE_ID_RC45, PCI_ANY_ID, PCI_ANY_ID,},
116 	{}
117 };
118 MODULE_DEVICE_TABLE (pci, rcpci45_pci_table);
119 MODULE_LICENSE("GPL");
120 
121 static void __devexit
rcpci45_remove_one(struct pci_dev * pdev)122 rcpci45_remove_one (struct pci_dev *pdev)
123 {
124 	struct net_device *dev = pci_get_drvdata (pdev);
125 	PDPA pDpa = dev->priv;
126 
127 	if (!dev) {
128 		printk (KERN_ERR "%s: remove non-existent device\n",
129 				dev->name);
130 		return;
131 	}
132 
133 	RCResetIOP (dev);
134 	unregister_netdev (dev);
135 	free_irq (dev->irq, dev);
136 	iounmap ((void *) dev->base_addr);
137 	pci_release_regions (pdev);
138 	if (pDpa->msgbuf)
139 		kfree (pDpa->msgbuf);
140 	if (pDpa->pPab)
141 		kfree (pDpa->pPab);
142 	kfree (dev);
143 	pci_set_drvdata (pdev, NULL);
144 }
145 
146 static int
rcpci45_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)147 rcpci45_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
148 {
149 	unsigned long *vaddr;
150 	PDPA pDpa;
151 	int error;
152 	static int card_idx = -1;
153 	struct net_device *dev;
154 	unsigned long pci_start, pci_len;
155 
156 	card_idx++;
157 
158 	/*
159 	 * Allocate and fill new device structure.
160 	 * We need enough for struct net_device plus DPA plus the LAN
161 	 * API private area, which requires a minimum of 16KB.  The top
162 	 * of the allocated area will be assigned to struct net_device;
163 	 * the next chunk will be assigned to DPA; and finally, the rest
164 	 * will be assigned to the LAN API layer.
165 	 */
166 
167 	dev = init_etherdev (NULL, sizeof (*pDpa));
168 	if (!dev) {
169 		printk (KERN_ERR
170 			"(rcpci45 driver:) init_etherdev alloc failed\n");
171 		error = -ENOMEM;
172 		goto err_out;
173 	}
174 
175 	error = pci_enable_device (pdev);
176 	if (error) {
177 		printk (KERN_ERR
178 			"(rcpci45 driver:) %d: pci enable device error\n",
179 			card_idx);
180 		goto err_out;
181 	}
182 	error = -ENOMEM;
183 	pci_start = pci_resource_start (pdev, 0);
184 	pci_len = pci_resource_len (pdev, 0);
185 	printk("pci_start %lx pci_len %lx\n", pci_start, pci_len);
186 
187 	pci_set_drvdata (pdev, dev);
188 
189 	pDpa = dev->priv;
190 	pDpa->id = card_idx;
191 	pDpa->pci_addr = pci_start;
192 
193 	if (!pci_start || !(pci_resource_flags (pdev, 0) & IORESOURCE_MEM)) {
194 		printk (KERN_ERR
195 			"(rcpci45 driver:) No PCI mem resources! Aborting\n");
196 		error = -EBUSY;
197 		goto err_out_free_dev;
198 	}
199 
200 	/*
201 	 * pDpa->msgbuf is where the card will dma the I2O
202 	 * messages. Thus, we need contiguous physical pages of
203 	 * memory.
204 	 */
205 	pDpa->msgbuf = kmalloc (MSG_BUF_SIZE, GFP_DMA | GFP_KERNEL);
206 	if (!pDpa->msgbuf) {
207 		printk (KERN_ERR "(rcpci45 driver:) \
208 			Could not allocate %d byte memory for the \
209 				private msgbuf!\n", MSG_BUF_SIZE);
210 		goto err_out_free_dev;
211 	}
212 
213 	/*
214 	 * Save the starting address of the LAN API private area.  We'll
215 	 * pass that to RCInitI2OMsgLayer().
216 	 *
217 	 */
218 	pDpa->PLanApiPA = (void *) (((long) pDpa->msgbuf + 0xff) & ~0xff);
219 
220 	/* The adapter is accessible through memory-access read/write, not
221 	 * I/O read/write.  Thus, we need to map it to some virtual address
222 	 * area in order to access the registers as normal memory.
223 	 */
224 	error = pci_request_regions (pdev, dev->name);
225 	if (error)
226 		goto err_out_free_msgbuf;
227 
228 	vaddr = (ulong *) ioremap (pci_start, pci_len);
229 	if (!vaddr) {
230 		printk (KERN_ERR
231 			"(rcpci45 driver:) \
232 			Unable to remap address range from %lu to %lu\n",
233 			pci_start, pci_start + pci_len);
234 		goto err_out_free_region;
235 	}
236 
237 	dev->base_addr = (unsigned long) vaddr;
238 	dev->irq = pdev->irq;
239 	dev->open = &RCopen;
240 	dev->hard_start_xmit = &RC_xmit_packet;
241 	dev->stop = &RCclose;
242 	dev->get_stats = &RCget_stats;
243 	dev->do_ioctl = &RCioctl;
244 	dev->set_config = &RCconfig;
245 
246 	return 0;		/* success */
247 
248 err_out_free_region:
249 	pci_release_regions (pdev);
250 err_out_free_msgbuf:
251 	kfree (pDpa->msgbuf);
252 err_out_free_dev:
253 	unregister_netdev (dev);
254 	kfree (dev);
255 err_out:
256 	card_idx--;
257 	return -ENODEV;
258 }
259 
260 static struct pci_driver rcpci45_driver = {
261 	name:		"rcpci45",
262 	id_table:	rcpci45_pci_table,
263 	probe:		rcpci45_init_one,
264 	remove:		__devexit_p(rcpci45_remove_one),
265 };
266 
267 static int __init
rcpci_init_module(void)268 rcpci_init_module (void)
269 {
270 	int rc = pci_module_init (&rcpci45_driver);
271 	if (!rc)
272 		printk (KERN_ERR "%s", version);
273 	return rc;
274 }
275 
276 static int
RCopen(struct net_device * dev)277 RCopen (struct net_device *dev)
278 {
279 	int post_buffers = MAX_NMBR_RCV_BUFFERS;
280 	PDPA pDpa = dev->priv;
281 	int count = 0;
282 	int requested = 0;
283 	int error;
284 
285 	MOD_INC_USE_COUNT;
286 	if (pDpa->nexus) {
287 		/* This is not the first time RCopen is called.  Thus,
288 		 * the interface was previously opened and later closed
289 		 * by RCclose().  RCclose() does a Shutdown; to wake up
290 		 * the adapter, a reset is mandatory before we can post
291 		 * receive buffers.  However, if the adapter initiated
292 		 * a reboot while the interface was closed -- and interrupts
293 		 * were turned off -- we need will need to reinitialize
294 		 * the adapter, rather than simply waking it up.
295 		 */
296 		printk (KERN_INFO "Waking up adapter...\n");
297 		RCResetLANCard (dev, 0, 0, 0);
298 	} else {
299 		pDpa->nexus = 1;
300 		/*
301 		 * RCInitI2OMsgLayer is done only once, unless the
302 		 * adapter was sent a warm reboot
303 		 */
304 		error = RCInitI2OMsgLayer (dev, (PFNTXCALLBACK) RCxmit_callback,
305 					   (PFNRXCALLBACK) RCrecv_callback,
306 					   (PFNCALLBACK) RCreboot_callback);
307 		if (error) {
308 			printk (KERN_ERR "%s: Unable to init msg layer (%x)\n",
309 					dev->name, error);
310 			goto err_out;
311 		}
312 		if ((error = RCGetMAC (dev, NULL))) {
313 			printk (KERN_ERR "%s: Unable to get adapter MAC\n",
314 					dev->name);
315 			goto err_out;
316 		}
317 	}
318 
319 	/* Request a shared interrupt line. */
320 	error = request_irq (dev->irq, RCinterrupt, SA_SHIRQ, dev->name, dev);
321 	if (error) {
322 		printk (KERN_ERR "%s: unable to get IRQ %d\n",
323 				dev->name, dev->irq);
324 		goto err_out;
325 	}
326 
327 	DriverControlWord |= WARM_REBOOT_CAPABLE;
328 	RCReportDriverCapability (dev, DriverControlWord);
329 
330 	printk (KERN_INFO "%s: RedCreek Communications IPSEC VPN adapter\n",
331 		dev->name);
332 
333 	RCEnableI2OInterrupts (dev);
334 
335 	while (post_buffers) {
336 		if (post_buffers > MAX_NMBR_POST_BUFFERS_PER_MSG)
337 			requested = MAX_NMBR_POST_BUFFERS_PER_MSG;
338 		else
339 			requested = post_buffers;
340 		count = RC_allocate_and_post_buffers (dev, requested);
341 
342 		if (count < requested) {
343 			/*
344 			 * Check to see if we were able to post
345 			 * any buffers at all.
346 			 */
347 			if (post_buffers == MAX_NMBR_RCV_BUFFERS) {
348 				printk (KERN_ERR "%s: \
349 					unable to allocate any buffers\n",
350 						dev->name);
351 				goto err_out_free_irq;
352 			}
353 			printk (KERN_WARNING "%s: \
354 			unable to allocate all requested buffers\n", dev->name);
355 			break;	/* we'll try to post more buffers later */
356 		} else
357 			post_buffers -= count;
358 	}
359 	pDpa->numOutRcvBuffers = MAX_NMBR_RCV_BUFFERS - post_buffers;
360 	pDpa->shutdown = 0;	/* just in case */
361 	netif_start_queue (dev);
362 	return 0;
363 
364 err_out_free_irq:
365 	free_irq (dev->irq, dev);
366 err_out:
367 	MOD_DEC_USE_COUNT;
368 	return error;
369 }
370 
371 static int
RC_xmit_packet(struct sk_buff * skb,struct net_device * dev)372 RC_xmit_packet (struct sk_buff *skb, struct net_device *dev)
373 {
374 
375 	PDPA pDpa = dev->priv;
376 	singleTCB tcb;
377 	psingleTCB ptcb = &tcb;
378 	RC_RETURN status = 0;
379 
380 	netif_stop_queue (dev);
381 
382 	if (pDpa->shutdown || pDpa->reboot) {
383 		printk ("RC_xmit_packet: tbusy!\n");
384 		return 1;
385 	}
386 
387 	/*
388 	 * The user is free to reuse the TCB after RCI2OSendPacket()
389 	 * returns, since the function copies the necessary info into its
390 	 * own private space.  Thus, our TCB can be a local structure.
391 	 * The skb, on the other hand, will be freed up in our interrupt
392 	 * handler.
393 	 */
394 
395 	ptcb->bcount = 1;
396 
397 	/*
398 	 * we'll get the context when the adapter interrupts us to tell us that
399 	 * the transmission is done. At that time, we can free skb.
400 	 */
401 	ptcb->b.context = (U32) skb;
402 	ptcb->b.scount = 1;
403 	ptcb->b.size = skb->len;
404 	ptcb->b.addr = virt_to_bus ((void *) skb->data);
405 
406 	if ((status = RCI2OSendPacket (dev, (U32) NULL, (PRCTCB) ptcb))
407 	    != RC_RTN_NO_ERROR) {
408 		printk ("%s: send error 0x%x\n", dev->name, (uint) status);
409 		return 1;
410 	} else {
411 		dev->trans_start = jiffies;
412 		netif_wake_queue (dev);
413 	}
414 	/*
415 	 * That's it!
416 	 */
417 	return 0;
418 }
419 
420 /*
421  * RCxmit_callback()
422  *
423  * The transmit callback routine. It's called by RCProcI2OMsgQ()
424  * because the adapter is done with one or more transmit buffers and
425  * it's returning them to us, or we asked the adapter to return the
426  * outstanding transmit buffers by calling RCResetLANCard() with
427  * RC_RESOURCE_RETURN_PEND_TX_BUFFERS flag.
428  * All we need to do is free the buffers.
429  */
430 static void
RCxmit_callback(U32 Status,U16 PcktCount,PU32 BufferContext,struct net_device * dev)431 RCxmit_callback (U32 Status,
432 		 U16 PcktCount, PU32 BufferContext, struct net_device *dev)
433 {
434 	struct sk_buff *skb;
435 	PDPA pDpa = dev->priv;
436 
437 	if (!pDpa) {
438 		printk (KERN_ERR "%s: Fatal Error in xmit callback, !pDpa\n",
439 				dev->name);
440 		return;
441 	}
442 
443 	if (Status != I2O_REPLY_STATUS_SUCCESS)
444 		printk (KERN_INFO "%s: xmit_callback: Status = 0x%x\n",
445 				dev->name, (uint) Status);
446 	if (pDpa->shutdown || pDpa->reboot)
447 		printk (KERN_INFO "%s: xmit callback: shutdown||reboot\n",
448 				dev->name);
449 
450 	while (PcktCount--) {
451 		skb = (struct sk_buff *) (BufferContext[0]);
452 		BufferContext++;
453 		dev_kfree_skb_irq (skb);
454 	}
455 	netif_wake_queue (dev);
456 }
457 
458 static void
RCreset_callback(U32 Status,U32 p1,U32 p2,struct net_device * dev)459 RCreset_callback (U32 Status, U32 p1, U32 p2, struct net_device *dev)
460 {
461 	PDPA pDpa = dev->priv;
462 
463 	printk ("RCreset_callback Status 0x%x\n", (uint) Status);
464 	/*
465 	 * Check to see why we were called.
466 	 */
467 	if (pDpa->shutdown) {
468 		printk (KERN_INFO "%s: shutting down interface\n",
469 				dev->name);
470 		pDpa->shutdown = 0;
471 		pDpa->reboot = 0;
472 	} else if (pDpa->reboot) {
473 		printk (KERN_INFO "%s: reboot, shutdown adapter\n",
474 				dev->name);
475 		/*
476 		 * We don't set any of the flags in RCShutdownLANCard()
477 		 * and we don't pass a callback routine to it.
478 		 * The adapter will have already initiated the reboot by
479 		 * the time the function returns.
480 		 */
481 		RCDisableI2OInterrupts (dev);
482 		RCShutdownLANCard (dev, 0, 0, 0);
483 		printk (KERN_INFO "%s: scheduling timer...\n", dev->name);
484 		init_timer (&pDpa->timer);
485 		pDpa->timer.expires = RUN_AT ((40 * HZ) / 10);	/* 4 sec. */
486 		pDpa->timer.data = (unsigned long) dev;
487 		pDpa->timer.function = &rc_timer;	/* timer handler */
488 		add_timer (&pDpa->timer);
489 	}
490 }
491 
492 static void
RCreboot_callback(U32 Status,U32 p1,U32 p2,struct net_device * dev)493 RCreboot_callback (U32 Status, U32 p1, U32 p2, struct net_device *dev)
494 {
495 	PDPA pDpa = dev->priv;
496 
497 	printk (KERN_INFO "%s: reboot: rcv buffers outstanding = %d\n",
498 		 dev->name, (uint) pDpa->numOutRcvBuffers);
499 
500 	if (pDpa->shutdown) {
501 		printk (KERN_INFO "%s: skip reboot, shutdown initiated\n",
502 				dev->name);
503 		return;
504 	}
505 	pDpa->reboot = 1;
506 	/*
507 	 * OK, we reset the adapter and ask it to return all
508 	 * outstanding transmit buffers as well as the posted
509 	 * receive buffers.  When the adapter is done returning
510 	 * those buffers, it will call our RCreset_callback()
511 	 * routine.  In that routine, we'll call RCShutdownLANCard()
512 	 * to tell the adapter that it's OK to start the reboot and
513 	 * schedule a timer callback routine to execute 3 seconds
514 	 * later; this routine will reinitialize the adapter at that time.
515 	 */
516 	RCResetLANCard (dev, RC_RESOURCE_RETURN_POSTED_RX_BUCKETS |
517 			RC_RESOURCE_RETURN_PEND_TX_BUFFERS, 0,
518 			(PFNCALLBACK) RCreset_callback);
519 }
520 
521 int
broadcast_packet(unsigned char * address)522 broadcast_packet (unsigned char *address)
523 {
524 	int i;
525 	for (i = 0; i < 6; i++)
526 		if (address[i] != 0xff)
527 			return 0;
528 
529 	return 1;
530 }
531 
532 /*
533  * RCrecv_callback()
534  *
535  * The receive packet callback routine.  This is called by
536  * RCProcI2OMsgQ() after the adapter posts buffers which have been
537  * filled (one ethernet packet per buffer).
538  */
539 static void
RCrecv_callback(U32 Status,U8 PktCount,U32 BucketsRemain,PU32 PacketDescBlock,struct net_device * dev)540 RCrecv_callback (U32 Status,
541 		 U8 PktCount,
542 		 U32 BucketsRemain,
543 		 PU32 PacketDescBlock, struct net_device *dev)
544 {
545 
546 	U32 len, count;
547 	PDPA pDpa = dev->priv;
548 	struct sk_buff *skb;
549 	singleTCB tcb;
550 	psingleTCB ptcb = &tcb;
551 
552 	ptcb->bcount = 1;
553 
554 	if ((pDpa->shutdown || pDpa->reboot) && !Status)
555 		printk (KERN_INFO "%s: shutdown||reboot && !Status (%d)\n",
556 				dev->name, PktCount);
557 
558 	if ((Status != I2O_REPLY_STATUS_SUCCESS) || pDpa->shutdown) {
559 		/*
560 		 * Free whatever buffers the adapter returned, but don't
561 		 * pass them to the kernel.
562 		 */
563 
564 		if (!pDpa->shutdown && !pDpa->reboot)
565 			printk (KERN_INFO "%s: recv error status = 0x%x\n",
566 					dev->name, (uint) Status);
567 		else
568 			printk (KERN_DEBUG "%s: Returning %d buffs stat 0x%x\n",
569 					dev->name, PktCount, (uint) Status);
570 		/*
571 		 * TO DO: check the nature of the failure and put the
572 		 * adapter in failed mode if it's a hard failure.
573 		 * Send a reset to the adapter and free all outstanding memory.
574 		 */
575 		if (PacketDescBlock) {
576 			while (PktCount--) {
577 				skb = (struct sk_buff *) PacketDescBlock[0];
578 				dev_kfree_skb (skb);
579 				pDpa->numOutRcvBuffers--;
580 				/* point to next context field */
581 				PacketDescBlock += BD_SIZE;
582 			}
583 		}
584 		return;
585 	} else {
586 		while (PktCount--) {
587 			skb = (struct sk_buff *) PacketDescBlock[0];
588 			len = PacketDescBlock[2];
589 			skb->dev = dev;
590 			skb_put (skb, len);	/* adjust length and tail */
591 			skb->protocol = eth_type_trans (skb, dev);
592 			netif_rx (skb);	/* send the packet to the kernel */
593 			dev->last_rx = jiffies;
594 			pDpa->numOutRcvBuffers--;
595 			/* point to next context field */
596 			PacketDescBlock += BD_SIZE;
597 		}
598 	}
599 
600 	/*
601 	 * Replenish the posted receive buffers.
602 	 * DO NOT replenish buffers if the driver has already
603 	 * initiated a reboot or shutdown!
604 	 */
605 
606 	if (!pDpa->shutdown && !pDpa->reboot) {
607 		count = RC_allocate_and_post_buffers (dev,
608 						      MAX_NMBR_RCV_BUFFERS -
609 						      pDpa->numOutRcvBuffers);
610 		pDpa->numOutRcvBuffers += count;
611 	}
612 
613 }
614 
615 /*
616  * RCinterrupt()
617  *
618  * Interrupt handler.
619  * This routine sets up a couple of pointers and calls
620  * RCProcI2OMsgQ(), which in turn process the message and
621  * calls one of our callback functions.
622  */
623 static void
RCinterrupt(int irq,void * dev_id,struct pt_regs * regs)624 RCinterrupt (int irq, void *dev_id, struct pt_regs *regs)
625 {
626 
627 	PDPA pDpa;
628 	struct net_device *dev = dev_id;
629 
630 	pDpa = dev->priv;
631 
632 	if (pDpa->shutdown)
633 		printk (KERN_DEBUG "%s: shutdown, service irq\n",
634 				dev->name);
635 
636 	RCProcI2OMsgQ (dev);
637 }
638 
639 #define REBOOT_REINIT_RETRY_LIMIT 4
640 static void
rc_timer(unsigned long data)641 rc_timer (unsigned long data)
642 {
643 	struct net_device *dev = (struct net_device *) data;
644 	PDPA pDpa = dev->priv;
645 	int init_status;
646 	static int retry;
647 	int post_buffers = MAX_NMBR_RCV_BUFFERS;
648 	int count = 0;
649 	int requested = 0;
650 
651 	if (pDpa->reboot) {
652 		init_status =
653 		    RCInitI2OMsgLayer (dev, (PFNTXCALLBACK) RCxmit_callback,
654 				       (PFNRXCALLBACK) RCrecv_callback,
655 				       (PFNCALLBACK) RCreboot_callback);
656 
657 		switch (init_status) {
658 		case RC_RTN_NO_ERROR:
659 
660 			pDpa->reboot = 0;
661 			pDpa->shutdown = 0;	/* just in case */
662 			RCReportDriverCapability (dev, DriverControlWord);
663 			RCEnableI2OInterrupts (dev);
664 
665 
666 			if (!(dev->flags & IFF_UP)) {
667 				retry = 0;
668 				return;
669 			}
670 			while (post_buffers) {
671 				if (post_buffers >
672 						MAX_NMBR_POST_BUFFERS_PER_MSG)
673 					requested =
674 						MAX_NMBR_POST_BUFFERS_PER_MSG;
675 				else
676 					requested = post_buffers;
677 				count =
678 				    RC_allocate_and_post_buffers (dev,
679 								  requested);
680 				post_buffers -= count;
681 				if (count < requested)
682 					break;
683 			}
684 			pDpa->numOutRcvBuffers =
685 			    MAX_NMBR_RCV_BUFFERS - post_buffers;
686 			printk ("Initialization done.\n");
687 			netif_wake_queue (dev);
688 			retry = 0;
689 			return;
690 		case RC_RTN_FREE_Q_EMPTY:
691 			retry++;
692 			printk (KERN_WARNING "%s inbound free q empty\n",
693 					dev->name);
694 			break;
695 		default:
696 			retry++;
697 			printk (KERN_WARNING "%s bad stat after reboot: %d\n",
698 					dev->name, init_status);
699 			break;
700 		}
701 
702 		if (retry > REBOOT_REINIT_RETRY_LIMIT) {
703 			printk (KERN_WARNING "%s unable to reinitialize adapter after reboot\n", dev->name);
704 			printk (KERN_WARNING "%s decrementing driver and closing interface\n", dev->name);
705 			RCDisableI2OInterrupts (dev);
706 			dev->flags &= ~IFF_UP;
707 			MOD_DEC_USE_COUNT;
708 		} else {
709 			printk (KERN_INFO "%s: rescheduling timer...\n",
710 					dev->name);
711 			init_timer (&pDpa->timer);
712 			pDpa->timer.expires = RUN_AT ((40 * HZ) / 10);
713 			pDpa->timer.data = (unsigned long) dev;
714 			pDpa->timer.function = &rc_timer;
715 			add_timer (&pDpa->timer);
716 		}
717 	} else
718 		printk (KERN_WARNING "%s: unexpected timer irq\n", dev->name);
719 }
720 
721 static int
RCclose(struct net_device * dev)722 RCclose (struct net_device *dev)
723 {
724 	PDPA pDpa = dev->priv;
725 
726 	printk("RCclose\n");
727 	netif_stop_queue (dev);
728 
729 	if (pDpa->reboot) {
730 		printk (KERN_INFO "%s skipping reset -- adapter already in reboot mode\n", dev->name);
731 		dev->flags &= ~IFF_UP;
732 		pDpa->shutdown = 1;
733 		MOD_DEC_USE_COUNT;
734 		return 0;
735 	}
736 
737 	pDpa->shutdown = 1;
738 
739 	/*
740 	 * We can't allow the driver to be unloaded until the adapter returns
741 	 * all posted receive buffers.  It doesn't hurt to tell the adapter
742 	 * to return all posted receive buffers and outstanding xmit buffers,
743 	 * even if there are none.
744 	 */
745 
746 	RCShutdownLANCard (dev, RC_RESOURCE_RETURN_POSTED_RX_BUCKETS |
747 			   RC_RESOURCE_RETURN_PEND_TX_BUFFERS, 0,
748 			   (PFNCALLBACK) RCreset_callback);
749 
750 	dev->flags &= ~IFF_UP;
751 	MOD_DEC_USE_COUNT;
752 	return 0;
753 }
754 
755 static struct net_device_stats *
RCget_stats(struct net_device * dev)756 RCget_stats (struct net_device *dev)
757 {
758 	RCLINKSTATS RCstats;
759 
760 	PDPA pDpa = dev->priv;
761 
762 	if (!pDpa) {
763 		return 0;
764 	} else if (!(dev->flags & IFF_UP)) {
765 		return 0;
766 	}
767 
768 	memset (&RCstats, 0, sizeof (RCLINKSTATS));
769 	if ((RCGetLinkStatistics (dev, &RCstats, (void *) 0)) ==
770 	    RC_RTN_NO_ERROR) {
771 
772 		/* total packets received    */
773 		pDpa->stats.rx_packets = RCstats.Rcv_good
774 		/* total packets transmitted    */;
775 		pDpa->stats.tx_packets = RCstats.TX_good;
776 
777 		pDpa->stats.rx_errors = RCstats.Rcv_CRCerr +
778 			RCstats.Rcv_alignerr + RCstats.Rcv_reserr +
779 			RCstats.Rcv_orun + RCstats.Rcv_cdt + RCstats.Rcv_runt;
780 
781 		pDpa->stats.tx_errors = RCstats.TX_urun + RCstats.TX_crs +
782 			RCstats.TX_def + RCstats.TX_totcol;
783 
784 		/*
785 		 * This needs improvement.
786 		 */
787 		pDpa->stats.rx_dropped = 0; /* no space in linux buffers   */
788 		pDpa->stats.tx_dropped = 0; /* no space available in linux */
789 		pDpa->stats.multicast = 0;  /* multicast packets received  */
790 		pDpa->stats.collisions = RCstats.TX_totcol;
791 
792 		/* detailed rx_errors: */
793 		pDpa->stats.rx_length_errors = 0;
794 		pDpa->stats.rx_over_errors = RCstats.Rcv_orun;
795 		pDpa->stats.rx_crc_errors = RCstats.Rcv_CRCerr;
796 		pDpa->stats.rx_frame_errors = 0;
797 		pDpa->stats.rx_fifo_errors = 0;
798 		pDpa->stats.rx_missed_errors = 0;
799 
800 		/* detailed tx_errors */
801 		pDpa->stats.tx_aborted_errors = 0;
802 		pDpa->stats.tx_carrier_errors = 0;
803 		pDpa->stats.tx_fifo_errors = 0;
804 		pDpa->stats.tx_heartbeat_errors = 0;
805 		pDpa->stats.tx_window_errors = 0;
806 
807 		return ((struct net_device_stats *) &(pDpa->stats));
808 	}
809 	return 0;
810 }
811 
812 static int
RCioctl(struct net_device * dev,struct ifreq * rq,int cmd)813 RCioctl (struct net_device *dev, struct ifreq *rq, int cmd)
814 {
815 	RCuser_struct RCuser;
816 	PDPA pDpa = dev->priv;
817 
818 	if (!capable (CAP_NET_ADMIN))
819 		return -EPERM;
820 
821 	switch (cmd) {
822 
823 	case RCU_PROTOCOL_REV:
824 		/*
825 		 * Assign user protocol revision, to tell user-level
826 		 * controller program whether or not it's in sync.
827 		 */
828 		rq->ifr_ifru.ifru_data = (caddr_t) USER_PROTOCOL_REV;
829 		break;
830 
831 	case RCU_COMMAND:
832 		{
833 			if (copy_from_user
834 			    (&RCuser, rq->ifr_data, sizeof (RCuser)))
835 				return -EFAULT;
836 
837 			dprintk ("RCioctl: RCuser_cmd = 0x%x\n", RCuser.cmd);
838 
839 			switch (RCuser.cmd) {
840 			case RCUC_GETFWVER:
841 				RCUD_GETFWVER = &RCuser.RCUS_GETFWVER;
842 				RCGetFirmwareVer (dev,
843 						  (PU8) & RCUD_GETFWVER->
844 						  FirmString, NULL);
845 				break;
846 			case RCUC_GETINFO:
847 				RCUD_GETINFO = &RCuser.RCUS_GETINFO;
848 				RCUD_GETINFO->mem_start = dev->base_addr;
849 				RCUD_GETINFO->mem_end =
850 				    dev->base_addr + pDpa->pci_addr_len;
851 				RCUD_GETINFO->base_addr = pDpa->pci_addr;
852 				RCUD_GETINFO->irq = dev->irq;
853 				break;
854 			case RCUC_GETIPANDMASK:
855 				RCUD_GETIPANDMASK = &RCuser.RCUS_GETIPANDMASK;
856 				RCGetRavlinIPandMask (dev,
857 						      (PU32) &
858 						      RCUD_GETIPANDMASK->IpAddr,
859 						      (PU32) &
860 						      RCUD_GETIPANDMASK->
861 						      NetMask, NULL);
862 				break;
863 			case RCUC_GETLINKSTATISTICS:
864 				RCUD_GETLINKSTATISTICS =
865 				    &RCuser.RCUS_GETLINKSTATISTICS;
866 				RCGetLinkStatistics (dev,
867 						     (P_RCLINKSTATS) &
868 						     RCUD_GETLINKSTATISTICS->
869 						     StatsReturn, NULL);
870 				break;
871 			case RCUC_GETLINKSTATUS:
872 				RCUD_GETLINKSTATUS = &RCuser.RCUS_GETLINKSTATUS;
873 				RCGetLinkStatus (dev,
874 						 (PU32) & RCUD_GETLINKSTATUS->
875 						 ReturnStatus, NULL);
876 				break;
877 			case RCUC_GETMAC:
878 				RCUD_GETMAC = &RCuser.RCUS_GETMAC;
879 				RCGetMAC (dev, NULL);
880 				memcpy(RCUD_GETMAC, dev->dev_addr, 8);
881 				break;
882 			case RCUC_GETPROM:
883 				RCUD_GETPROM = &RCuser.RCUS_GETPROM;
884 				RCGetPromiscuousMode (dev,
885 						      (PU32) & RCUD_GETPROM->
886 						      PromMode, NULL);
887 				break;
888 			case RCUC_GETBROADCAST:
889 				RCUD_GETBROADCAST = &RCuser.RCUS_GETBROADCAST;
890 				RCGetBroadcastMode (dev,
891 						    (PU32) & RCUD_GETBROADCAST->
892 						    BroadcastMode, NULL);
893 				break;
894 			case RCUC_GETSPEED:
895 				if (!(dev->flags & IFF_UP)) {
896 					return -ENODATA;
897 				}
898 				RCUD_GETSPEED = &RCuser.RCUS_GETSPEED;
899 				RCGetLinkSpeed (dev,
900 						(PU32) & RCUD_GETSPEED->
901 						LinkSpeedCode, NULL);
902 				break;
903 			case RCUC_SETIPANDMASK:
904 				RCUD_SETIPANDMASK = &RCuser.RCUS_SETIPANDMASK;
905 				RCSetRavlinIPandMask (dev,
906 						      (U32) RCUD_SETIPANDMASK->
907 						      IpAddr,
908 						      (U32) RCUD_SETIPANDMASK->
909 						      NetMask);
910 				break;
911 			case RCUC_SETMAC:
912 				RCSetMAC (dev, (PU8) & RCUD_SETMAC->mac);
913 				break;
914 			case RCUC_SETSPEED:
915 				RCUD_SETSPEED = &RCuser.RCUS_SETSPEED;
916 				RCSetLinkSpeed (dev,
917 						(U16) RCUD_SETSPEED->
918 						LinkSpeedCode);
919 				break;
920 			case RCUC_SETPROM:
921 				RCUD_SETPROM = &RCuser.RCUS_SETPROM;
922 				RCSetPromiscuousMode (dev,
923 						      (U16) RCUD_SETPROM->
924 						      PromMode);
925 				break;
926 			case RCUC_SETBROADCAST:
927 				RCUD_SETBROADCAST = &RCuser.RCUS_SETBROADCAST;
928 				RCSetBroadcastMode (dev,
929 						    (U16) RCUD_SETBROADCAST->
930 						    BroadcastMode);
931 				break;
932 			default:
933 				RCUD_DEFAULT = &RCuser.RCUS_DEFAULT;
934 				RCUD_DEFAULT->rc = 0x11223344;
935 				break;
936 			}
937 			if (copy_to_user (rq->ifr_data, &RCuser,
938 						sizeof (RCuser)))
939 				return -EFAULT;
940 			break;
941 		}		/* RCU_COMMAND */
942 
943 	default:
944 		rq->ifr_ifru.ifru_data = (caddr_t) 0x12345678;
945 		return -EINVAL;
946 	}
947 	return 0;
948 }
949 
950 static int
RCconfig(struct net_device * dev,struct ifmap * map)951 RCconfig (struct net_device *dev, struct ifmap *map)
952 {
953 	/*
954 	 * To be completed ...
955 	 */
956 	return 0;
957 	if (dev->flags & IFF_UP)	/* can't act on a running interface */
958 		return -EBUSY;
959 
960 	/* Don't allow changing the I/O address */
961 	if (map->base_addr != dev->base_addr) {
962 		printk (KERN_WARNING "%s Change I/O address not implemented\n",
963 				dev->name);
964 		return -EOPNOTSUPP;
965 	}
966 	return 0;
967 }
968 
969 static void __exit
rcpci_cleanup_module(void)970 rcpci_cleanup_module (void)
971 {
972 	pci_unregister_driver (&rcpci45_driver);
973 }
974 
975 module_init (rcpci_init_module);
976 module_exit (rcpci_cleanup_module);
977 
978 static int
RC_allocate_and_post_buffers(struct net_device * dev,int numBuffers)979 RC_allocate_and_post_buffers (struct net_device *dev, int numBuffers)
980 {
981 
982 	int i;
983 	PU32 p;
984 	psingleB pB;
985 	struct sk_buff *skb;
986 	RC_RETURN status;
987 	U32 res;
988 
989 	if (!numBuffers)
990 		return 0;
991 	else if (numBuffers > MAX_NMBR_POST_BUFFERS_PER_MSG) {
992 		printk (KERN_ERR "%s: Too many buffers requested!\n",
993 				dev->name);
994 		numBuffers = 32;
995 	}
996 
997 	p = (PU32) kmalloc (sizeof (U32) + numBuffers * sizeof (singleB),
998 			    GFP_DMA | GFP_ATOMIC);
999 
1000 	if (!p) {
1001 		printk (KERN_WARNING "%s unable to allocate TCB\n",
1002 				dev->name);
1003 		return 0;
1004 	}
1005 
1006 	p[0] = 0;		/* Buffer Count */
1007 	pB = (psingleB) ((U32) p + sizeof (U32));/* point to the first buffer */
1008 
1009 	for (i = 0; i < numBuffers; i++) {
1010 		skb = dev_alloc_skb (MAX_ETHER_SIZE + 2);
1011 		if (!skb) {
1012 			printk (KERN_WARNING
1013 					"%s: unable to allocate enough skbs!\n",
1014 					dev->name);
1015 			if (*p != 0) {	/* did we allocate any buffers */
1016 				break;
1017 			} else {
1018 				kfree (p);	/* Free the TCB */
1019 				return 0;
1020 			}
1021 		}
1022 		skb_reserve (skb, 2);	/* Align IP on 16 byte boundaries */
1023 		pB->context = (U32) skb;
1024 		pB->scount = 1;	/* segment count */
1025 		pB->size = MAX_ETHER_SIZE;
1026 		pB->addr = virt_to_bus ((void *) skb->data);
1027 		p[0]++;
1028 		pB++;
1029 	}
1030 
1031 	if ((status = RCPostRecvBuffers (dev, (PRCTCB) p)) != RC_RTN_NO_ERROR) {
1032 		printk (KERN_WARNING "%s: Post buffer failed, error 0x%x\n",
1033 				dev->name, status);
1034 		/* point to the first buffer */
1035 		pB = (psingleB) ((U32) p + sizeof (U32));
1036 		while (p[0]) {
1037 			skb = (struct sk_buff *) pB->context;
1038 			dev_kfree_skb (skb);
1039 			p[0]--;
1040 			pB++;
1041 		}
1042 	}
1043 	res = p[0];
1044 	kfree (p);
1045 	return (res);		/* return the number of posted buffers */
1046 }
1047