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