This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.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 **  Francois Romieu, Apr 2003: Converted to pci DMA mapping API.
33 **
34 **  Pete Popov, Oct 2001: Fixed a few bugs to make the driver functional
35 **  again. Note that this card is not supported or manufactured by 
36 **  RedCreek anymore.
37 **   
38 **  Rasmus Andersen, December 2000: Converted to new PCI API and general
39 **  cleanup.
40 **
41 **  Pete Popov, January 11,99: Fixed a couple of 2.1.x problems 
42 **  (virt_to_bus() not called), tested it under 2.2pre5 (as a module), and 
43 **  added a #define(s) to enable the use of the same file for both, the 2.0.x 
44 **  kernels as well as the 2.1.x.
45 **
46 **  Ported to 2.1.x by Alan Cox 1998/12/9. 
47 **
48 **  Sometime in mid 1998, written by Pete Popov and Brian Moyle.
49 **
50 ***************************************************************************/
51
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/string.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
64 #include <asm/irq.h>            /* For NR_IRQS only. */
65 #include <asm/bitops.h>
66 #include <asm/uaccess.h>
67
68 static char version[] __initdata =
69     "RedCreek Communications PCI linux driver version 2.21\n";
70
71 #define RC_LINUX_MODULE
72 #include "rclanmtl.h"
73 #include "rcif.h"
74
75 #define RUN_AT(x) (jiffies + (x))
76
77 #define NEW_MULTICAST
78
79 #define MAX_ETHER_SIZE        1520
80 #define MAX_NMBR_RCV_BUFFERS    96
81 #define RC_POSTED_BUFFERS_LOW_MARK MAX_NMBR_RCV_BUFFERS-16
82 #define BD_SIZE 3               /* Bucket Descriptor size */
83 #define BD_LEN_OFFSET 2         /* Bucket Descriptor offset to length field */
84
85 /* RedCreek LAN device Target ID */
86 #define RC_LAN_TARGET_ID  0x10
87 /* RedCreek's OSM default LAN receive Initiator */
88 #define DEFAULT_RECV_INIT_CONTEXT  0xA17
89
90 /* minimum msg buffer size needed by the card 
91  * Note that the size of this buffer is hard code in the
92  * ipsec card's firmware. Thus, the size MUST be a minimum
93  * of 16K. Otherwise the card will end up using memory
94  * that does not belong to it.
95  */
96 #define MSG_BUF_SIZE  16384
97
98 /* 2003/04/20: I don't know about the hardware ability but the driver won't
99  * play safe with 64 bit addressing and DAC without NETIF_F_HIGHDMA doesn't
100  * really make sense anyway. Let's play safe - romieu.
101  */
102 #define RCPCI45_DMA_MASK        ((u64) 0xffffffff)
103
104 static U32 DriverControlWord;
105
106 static void rc_timer (unsigned long);
107
108 static int RCopen (struct net_device *);
109 static int RC_xmit_packet (struct sk_buff *, struct net_device *);
110 static irqreturn_t RCinterrupt (int, void *, struct pt_regs *);
111 static int RCclose (struct net_device *dev);
112 static struct net_device_stats *RCget_stats (struct net_device *);
113 static int RCioctl (struct net_device *, struct ifreq *, int);
114 static int RCconfig (struct net_device *, struct ifmap *);
115 static void RCxmit_callback (U32, U16, PU32, struct net_device *);
116 static void RCrecv_callback (U32, U8, U32, PU32, struct net_device *);
117 static void RCreset_callback (U32, U32, U32, struct net_device *);
118 static void RCreboot_callback (U32, U32, U32, struct net_device *);
119 static int RC_allocate_and_post_buffers (struct net_device *, int);
120
121 static struct pci_device_id rcpci45_pci_table[] = {
122         { PCI_VENDOR_ID_REDCREEK, PCI_DEVICE_ID_RC45, PCI_ANY_ID, PCI_ANY_ID,},
123         {}
124 };
125 MODULE_DEVICE_TABLE (pci, rcpci45_pci_table);
126 MODULE_LICENSE("GPL");
127
128 static void __devexit
129 rcpci45_remove_one (struct pci_dev *pdev)
130 {
131         struct net_device *dev = pci_get_drvdata (pdev);
132         PDPA pDpa = dev->priv;
133
134         RCResetIOP (dev);
135         unregister_netdev (dev);
136         free_irq (dev->irq, dev);
137         iounmap ((void *) dev->base_addr);
138         pci_release_regions (pdev);
139         pci_free_consistent (pdev, MSG_BUF_SIZE, pDpa->msgbuf,
140                              pDpa->msgbuf_dma);
141         if (pDpa->pPab)
142                 kfree (pDpa->pPab);
143         free_netdev (dev);
144         pci_set_drvdata (pdev, NULL);
145 }
146
147 static int
148 rcpci45_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
149 {
150         unsigned long *vaddr;
151         PDPA pDpa;
152         int error;
153         static int card_idx = -1;
154         struct net_device *dev;
155         unsigned long pci_start, pci_len;
156
157         card_idx++;
158
159         /* 
160          * Allocate and fill new device structure. 
161          * We need enough for struct net_device plus DPA plus the LAN 
162          * API private area, which requires a minimum of 16KB.  The top 
163          * of the allocated area will be assigned to struct net_device; 
164          * the next chunk will be assigned to DPA; and finally, the rest 
165          * will be assigned to the LAN API layer.
166          */
167
168         dev = alloc_etherdev(sizeof(*pDpa));
169         if (!dev) {
170                 printk (KERN_ERR
171                         "(rcpci45 driver:) alloc_etherdev alloc failed\n");
172                 error = -ENOMEM;
173                 goto err_out;
174         }
175
176         SET_MODULE_OWNER(dev);
177         SET_NETDEV_DEV(dev, &pdev->dev);
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         pci_start = pci_resource_start (pdev, 0);
187         pci_len = pci_resource_len (pdev, 0);
188         printk("pci_start %lx pci_len %lx\n", pci_start, pci_len);
189
190         pci_set_drvdata (pdev, dev);
191
192         pDpa = dev->priv;
193         pDpa->id = card_idx;
194         pDpa->pci_dev = pdev;
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 memory.
207          * 2003/04/20:  pci_alloc_consistent() provides well over the needed
208          * alignment on a 256 bytes boundary for the LAN API private area.
209          * Thus it isn't needed anymore to align it by hand.
210          */
211         pDpa->msgbuf = pci_alloc_consistent (pdev, MSG_BUF_SIZE,
212                                              &pDpa->msgbuf_dma);
213         if (!pDpa->msgbuf) {
214                 printk (KERN_ERR "(rcpci45 driver:) \
215                         Could not allocate %d byte memory for the \
216                                 private msgbuf!\n", MSG_BUF_SIZE);
217                 error = -ENOMEM;
218                 goto err_out_free_dev;
219         }
220
221         /* The adapter is accessible through memory-access read/write, not
222          * I/O read/write.  Thus, we need to map it to some virtual address
223          * area in order to access the registers as normal memory.
224          */
225         error = pci_request_regions (pdev, dev->name);
226         if (error)
227                 goto err_out_free_msgbuf;
228
229         error = pci_set_dma_mask (pdev, RCPCI45_DMA_MASK);
230         if (error) {
231                 printk (KERN_ERR
232                         "(rcpci45 driver:) pci_set_dma_mask failed!\n");
233                 goto err_out_free_region;
234         }
235
236         vaddr = (ulong *) ioremap (pci_start, pci_len);
237         if (!vaddr) {
238                 printk (KERN_ERR
239                         "(rcpci45 driver:) \
240                         Unable to remap address range from %lu to %lu\n",
241                         pci_start, pci_start + pci_len);
242                 error = -EIO;
243                 goto err_out_free_region;
244         }
245
246         dev->base_addr = (unsigned long) vaddr;
247         dev->irq = pdev->irq;
248         dev->open = &RCopen;
249         dev->hard_start_xmit = &RC_xmit_packet;
250         dev->stop = &RCclose;
251         dev->get_stats = &RCget_stats;
252         dev->do_ioctl = &RCioctl;
253         dev->set_config = &RCconfig;
254
255         if ((error = register_netdev(dev)))
256                 goto err_out_iounmap;
257
258         return 0;               /* success */
259
260 err_out_iounmap:
261         iounmap((void *) dev->base_addr);
262 err_out_free_region:
263         pci_release_regions (pdev);
264 err_out_free_msgbuf:
265         pci_free_consistent (pdev, MSG_BUF_SIZE, pDpa->msgbuf,
266                              pDpa->msgbuf_dma);
267 err_out_free_dev:
268         free_netdev (dev);
269 err_out:
270         card_idx--;
271         return error;
272 }
273
274 static struct pci_driver rcpci45_driver = {
275         .name           = "rcpci45",
276         .id_table       = rcpci45_pci_table,
277         .probe          = rcpci45_init_one,
278         .remove         = __devexit_p(rcpci45_remove_one),
279 };
280
281 static int __init
282 rcpci_init_module (void)
283 {
284         int rc = pci_module_init (&rcpci45_driver);
285         if (!rc)
286                 printk (KERN_ERR "%s", version);
287         return rc;
288 }
289
290 static int
291 RCopen (struct net_device *dev)
292 {
293         int post_buffers = MAX_NMBR_RCV_BUFFERS;
294         PDPA pDpa = dev->priv;
295         int count = 0;
296         int requested = 0;
297         int error;
298
299         if (pDpa->nexus) {
300                 /* This is not the first time RCopen is called.  Thus,
301                  * the interface was previously opened and later closed
302                  * by RCclose().  RCclose() does a Shutdown; to wake up
303                  * the adapter, a reset is mandatory before we can post
304                  * receive buffers.  However, if the adapter initiated 
305                  * a reboot while the interface was closed -- and interrupts
306                  * were turned off -- we need will need to reinitialize
307                  * the adapter, rather than simply waking it up.  
308                  */
309                 printk (KERN_INFO "Waking up adapter...\n");
310                 RCResetLANCard (dev, 0, 0, 0);
311         } else {
312                 pDpa->nexus = 1;
313                 /* 
314                  * RCInitI2OMsgLayer is done only once, unless the
315                  * adapter was sent a warm reboot
316                  */
317                 error = RCInitI2OMsgLayer (dev, (PFNTXCALLBACK) RCxmit_callback,
318                                            (PFNRXCALLBACK) RCrecv_callback,
319                                            (PFNCALLBACK) RCreboot_callback);
320                 if (error) {
321                         printk (KERN_ERR "%s: Unable to init msg layer (%x)\n",
322                                         dev->name, error);
323                         goto err_out;
324                 }
325                 if ((error = RCGetMAC (dev, NULL))) {
326                         printk (KERN_ERR "%s: Unable to get adapter MAC\n",
327                                         dev->name);
328                         goto err_out;
329                 }
330         }
331
332         /* Request a shared interrupt line. */
333         error = request_irq (dev->irq, RCinterrupt, SA_SHIRQ, dev->name, dev);
334         if (error) {
335                 printk (KERN_ERR "%s: unable to get IRQ %d\n", 
336                                 dev->name, dev->irq);
337                 goto err_out;
338         }
339
340         DriverControlWord |= WARM_REBOOT_CAPABLE;
341         RCReportDriverCapability (dev, DriverControlWord);
342
343         printk (KERN_INFO "%s: RedCreek Communications IPSEC VPN adapter\n",
344                 dev->name);
345
346         RCEnableI2OInterrupts (dev);
347
348         while (post_buffers) {
349                 if (post_buffers > MAX_NMBR_POST_BUFFERS_PER_MSG)
350                         requested = MAX_NMBR_POST_BUFFERS_PER_MSG;
351                 else
352                         requested = post_buffers;
353                 count = RC_allocate_and_post_buffers (dev, requested);
354
355                 if (count < requested) {
356                         /*
357                          * Check to see if we were able to post 
358                          * any buffers at all.
359                          */
360                         if (post_buffers == MAX_NMBR_RCV_BUFFERS) {
361                                 printk (KERN_ERR "%s: \
362                                         unable to allocate any buffers\n", 
363                                                 dev->name);
364                                 goto err_out_free_irq;
365                         }
366                         printk (KERN_WARNING "%s: \
367                         unable to allocate all requested buffers\n", dev->name);
368                         break;  /* we'll try to post more buffers later */
369                 } else
370                         post_buffers -= count;
371         }
372         pDpa->numOutRcvBuffers = MAX_NMBR_RCV_BUFFERS - post_buffers;
373         pDpa->shutdown = 0;     /* just in case */
374         netif_start_queue (dev);
375         return 0;
376
377 err_out_free_irq:
378         free_irq (dev->irq, dev);
379 err_out:
380         return error;
381 }
382
383 static int
384 RC_xmit_packet (struct sk_buff *skb, struct net_device *dev)
385 {
386
387         PDPA pDpa = dev->priv;
388         singleTCB tcb;
389         psingleTCB ptcb = &tcb;
390         RC_RETURN status = 0;
391
392         netif_stop_queue (dev);
393
394         if (pDpa->shutdown || pDpa->reboot) {
395                 printk ("RC_xmit_packet: tbusy!\n");
396                 return 1;
397         }
398
399         /*
400          * The user is free to reuse the TCB after RCI2OSendPacket() 
401          * returns, since the function copies the necessary info into its 
402          * own private space.  Thus, our TCB can be a local structure.  
403          * The skb, on the other hand, will be freed up in our interrupt 
404          * handler.
405          */
406
407         ptcb->bcount = 1;
408
409         /* 
410          * we'll get the context when the adapter interrupts us to tell us that
411          * the transmission is done. At that time, we can free skb.
412          */
413         ptcb->b.context = (U32) skb;
414         ptcb->b.scount = 1;
415         ptcb->b.size = skb->len;
416         ptcb->b.addr = pci_map_single(pDpa->pci_dev, skb->data, skb->len,
417                                       PCI_DMA_TODEVICE);
418
419         if ((status = RCI2OSendPacket (dev, (U32) NULL, (PRCTCB) ptcb))
420             != RC_RTN_NO_ERROR) {
421                 printk ("%s: send error 0x%x\n", dev->name, (uint) status);
422                 return 1;
423         } else {
424                 dev->trans_start = jiffies;
425                 netif_wake_queue (dev);
426         }
427         /*
428          * That's it!
429          */
430         return 0;
431 }
432
433 /*
434  * RCxmit_callback()
435  *
436  * The transmit callback routine. It's called by RCProcI2OMsgQ()
437  * because the adapter is done with one or more transmit buffers and
438  * it's returning them to us, or we asked the adapter to return the
439  * outstanding transmit buffers by calling RCResetLANCard() with 
440  * RC_RESOURCE_RETURN_PEND_TX_BUFFERS flag. 
441  * All we need to do is free the buffers.
442  */
443 static void
444 RCxmit_callback (U32 Status,
445                  U16 PcktCount, PU32 BufferContext, struct net_device *dev)
446 {
447         struct sk_buff *skb;
448         PDPA pDpa = dev->priv;
449
450         if (!pDpa) {
451                 printk (KERN_ERR "%s: Fatal Error in xmit callback, !pDpa\n",
452                                 dev->name);
453                 return;
454         }
455
456         if (Status != I2O_REPLY_STATUS_SUCCESS)
457                 printk (KERN_INFO "%s: xmit_callback: Status = 0x%x\n", 
458                                 dev->name, (uint) Status);
459         if (pDpa->shutdown || pDpa->reboot)
460                 printk (KERN_INFO "%s: xmit callback: shutdown||reboot\n",
461                                 dev->name);
462
463         while (PcktCount--) {
464                 skb = (struct sk_buff *) (BufferContext[0]);
465                 BufferContext++;
466                 pci_unmap_single(pDpa->pci_dev, BufferContext[1], skb->len,
467                                  PCI_DMA_TODEVICE);
468                 dev_kfree_skb_irq (skb);
469         }
470         netif_wake_queue (dev);
471 }
472
473 static void
474 RCreset_callback (U32 Status, U32 p1, U32 p2, struct net_device *dev)
475 {
476         PDPA pDpa = dev->priv;
477
478         printk ("RCreset_callback Status 0x%x\n", (uint) Status);
479         /*
480          * Check to see why we were called.
481          */
482         if (pDpa->shutdown) {
483                 printk (KERN_INFO "%s: shutting down interface\n",
484                                 dev->name);
485                 pDpa->shutdown = 0;
486                 pDpa->reboot = 0;
487         } else if (pDpa->reboot) {
488                 printk (KERN_INFO "%s: reboot, shutdown adapter\n",
489                                 dev->name);
490                 /*
491                  * We don't set any of the flags in RCShutdownLANCard()
492                  * and we don't pass a callback routine to it.
493                  * The adapter will have already initiated the reboot by
494                  * the time the function returns.
495                  */
496                 RCDisableI2OInterrupts (dev);
497                 RCShutdownLANCard (dev, 0, 0, 0);
498                 printk (KERN_INFO "%s: scheduling timer...\n", dev->name);
499                 init_timer (&pDpa->timer);
500                 pDpa->timer.expires = RUN_AT ((40 * HZ) / 10);  /* 4 sec. */
501                 pDpa->timer.data = (unsigned long) dev;
502                 pDpa->timer.function = &rc_timer;       /* timer handler */
503                 add_timer (&pDpa->timer);
504         }
505 }
506
507 static void
508 RCreboot_callback (U32 Status, U32 p1, U32 p2, struct net_device *dev)
509 {
510         PDPA pDpa = dev->priv;
511
512         printk (KERN_INFO "%s: reboot: rcv buffers outstanding = %d\n",
513                  dev->name, (uint) pDpa->numOutRcvBuffers);
514
515         if (pDpa->shutdown) {
516                 printk (KERN_INFO "%s: skip reboot, shutdown initiated\n",
517                                 dev->name);
518                 return;
519         }
520         pDpa->reboot = 1;
521         /*
522          * OK, we reset the adapter and ask it to return all
523          * outstanding transmit buffers as well as the posted
524          * receive buffers.  When the adapter is done returning
525          * those buffers, it will call our RCreset_callback() 
526          * routine.  In that routine, we'll call RCShutdownLANCard()
527          * to tell the adapter that it's OK to start the reboot and
528          * schedule a timer callback routine to execute 3 seconds 
529          * later; this routine will reinitialize the adapter at that time.
530          */
531         RCResetLANCard (dev, RC_RESOURCE_RETURN_POSTED_RX_BUCKETS |
532                         RC_RESOURCE_RETURN_PEND_TX_BUFFERS, 0,
533                         (PFNCALLBACK) RCreset_callback);
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 irqreturn_t
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         return 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 shutting down interface\n", dev->name);
709                         RCDisableI2OInterrupts (dev);
710                         dev->flags &= ~IFF_UP;
711                 } else {
712                         printk (KERN_INFO "%s: rescheduling timer...\n",
713                                         dev->name);
714                         init_timer (&pDpa->timer);
715                         pDpa->timer.expires = RUN_AT ((40 * HZ) / 10);
716                         pDpa->timer.data = (unsigned long) dev;
717                         pDpa->timer.function = &rc_timer;
718                         add_timer (&pDpa->timer);
719                 }
720         } else
721                 printk (KERN_WARNING "%s: unexpected timer irq\n", dev->name);
722 }
723
724 static int
725 RCclose (struct net_device *dev)
726 {
727         PDPA pDpa = dev->priv;
728
729         printk("RCclose\n");
730         netif_stop_queue (dev);
731
732         if (pDpa->reboot) {
733                 printk (KERN_INFO "%s skipping reset -- adapter already in reboot mode\n", dev->name);
734                 dev->flags &= ~IFF_UP;
735                 pDpa->shutdown = 1;
736                 return 0;
737         }
738
739         pDpa->shutdown = 1;
740
741         /*
742          * We can't allow the driver to be unloaded until the adapter returns
743          * all posted receive buffers.  It doesn't hurt to tell the adapter
744          * to return all posted receive buffers and outstanding xmit buffers,
745          * even if there are none.
746          */
747
748         RCShutdownLANCard (dev, RC_RESOURCE_RETURN_POSTED_RX_BUCKETS |
749                            RC_RESOURCE_RETURN_PEND_TX_BUFFERS, 0,
750                            (PFNCALLBACK) RCreset_callback);
751
752         dev->flags &= ~IFF_UP;
753         return 0;
754 }
755
756 static struct net_device_stats *
757 RCget_stats (struct net_device *dev)
758 {
759         RCLINKSTATS RCstats;
760
761         PDPA pDpa = dev->priv;
762
763         if (!pDpa) {
764                 return 0;
765         } else if (!(dev->flags & IFF_UP)) {
766                 return 0;
767         }
768
769         memset (&RCstats, 0, sizeof (RCLINKSTATS));
770         if ((RCGetLinkStatistics (dev, &RCstats, (void *) 0)) ==
771             RC_RTN_NO_ERROR) {
772
773                 /* total packets received    */
774                 pDpa->stats.rx_packets = RCstats.Rcv_good
775                 /* total packets transmitted    */;
776                 pDpa->stats.tx_packets = RCstats.TX_good;
777
778                 pDpa->stats.rx_errors = RCstats.Rcv_CRCerr + 
779                         RCstats.Rcv_alignerr + RCstats.Rcv_reserr + 
780                         RCstats.Rcv_orun + RCstats.Rcv_cdt + RCstats.Rcv_runt;
781
782                 pDpa->stats.tx_errors = RCstats.TX_urun + RCstats.TX_crs + 
783                         RCstats.TX_def + RCstats.TX_totcol;
784
785                 /*
786                  * This needs improvement.
787                  */
788                 pDpa->stats.rx_dropped = 0; /* no space in linux buffers   */
789                 pDpa->stats.tx_dropped = 0; /* no space available in linux */
790                 pDpa->stats.multicast = 0;  /* multicast packets received  */
791                 pDpa->stats.collisions = RCstats.TX_totcol;
792
793                 /* detailed rx_errors: */
794                 pDpa->stats.rx_length_errors = 0;
795                 pDpa->stats.rx_over_errors = RCstats.Rcv_orun;
796                 pDpa->stats.rx_crc_errors = RCstats.Rcv_CRCerr;
797                 pDpa->stats.rx_frame_errors = 0;
798                 pDpa->stats.rx_fifo_errors = 0; 
799                 pDpa->stats.rx_missed_errors = 0;
800
801                 /* detailed tx_errors */
802                 pDpa->stats.tx_aborted_errors = 0;
803                 pDpa->stats.tx_carrier_errors = 0;
804                 pDpa->stats.tx_fifo_errors = 0;
805                 pDpa->stats.tx_heartbeat_errors = 0;
806                 pDpa->stats.tx_window_errors = 0;
807
808                 return ((struct net_device_stats *) &(pDpa->stats));
809         }
810         return 0;
811 }
812
813 static int
814 RCioctl (struct net_device *dev, struct ifreq *rq, int cmd)
815 {
816         RCuser_struct RCuser;
817         PDPA pDpa = dev->priv;
818
819         if (!capable (CAP_NET_ADMIN))
820                 return -EPERM;
821
822         switch (cmd) {
823
824         case RCU_PROTOCOL_REV:
825                 /*
826                  * Assign user protocol revision, to tell user-level
827                  * controller program whether or not it's in sync.
828                  */
829                 rq->ifr_ifru.ifru_data = (caddr_t) USER_PROTOCOL_REV;
830                 break;
831
832         case RCU_COMMAND:
833                 {
834                         if (copy_from_user
835                             (&RCuser, rq->ifr_data, sizeof (RCuser)))
836                                 return -EFAULT;
837
838                         dprintk ("RCioctl: RCuser_cmd = 0x%x\n", RCuser.cmd);
839
840                         switch (RCuser.cmd) {
841                         case RCUC_GETFWVER:
842                                 RCUD_GETFWVER = &RCuser.RCUS_GETFWVER;
843                                 RCGetFirmwareVer (dev,
844                                                   (PU8) & RCUD_GETFWVER->
845                                                   FirmString, NULL);
846                                 break;
847                         case RCUC_GETINFO:
848                                 RCUD_GETINFO = &RCuser.RCUS_GETINFO;
849                                 RCUD_GETINFO->mem_start = dev->base_addr;
850                                 RCUD_GETINFO->mem_end =
851                                     dev->base_addr + pDpa->pci_addr_len;
852                                 RCUD_GETINFO->base_addr = pDpa->pci_addr;
853                                 RCUD_GETINFO->irq = dev->irq;
854                                 break;
855                         case RCUC_GETIPANDMASK:
856                                 RCUD_GETIPANDMASK = &RCuser.RCUS_GETIPANDMASK;
857                                 RCGetRavlinIPandMask (dev,
858                                                       (PU32) &
859                                                       RCUD_GETIPANDMASK->IpAddr,
860                                                       (PU32) &
861                                                       RCUD_GETIPANDMASK->
862                                                       NetMask, NULL);
863                                 break;
864                         case RCUC_GETLINKSTATISTICS:
865                                 RCUD_GETLINKSTATISTICS =
866                                     &RCuser.RCUS_GETLINKSTATISTICS;
867                                 RCGetLinkStatistics (dev,
868                                                      (P_RCLINKSTATS) &
869                                                      RCUD_GETLINKSTATISTICS->
870                                                      StatsReturn, NULL);
871                                 break;
872                         case RCUC_GETLINKSTATUS:
873                                 RCUD_GETLINKSTATUS = &RCuser.RCUS_GETLINKSTATUS;
874                                 RCGetLinkStatus (dev,
875                                                  (PU32) & RCUD_GETLINKSTATUS->
876                                                  ReturnStatus, NULL);
877                                 break;
878                         case RCUC_GETMAC:
879                                 RCUD_GETMAC = &RCuser.RCUS_GETMAC;
880                                 RCGetMAC (dev, NULL);
881                                 memcpy(RCUD_GETMAC, dev->dev_addr, 8);
882                                 break;
883                         case RCUC_GETPROM:
884                                 RCUD_GETPROM = &RCuser.RCUS_GETPROM;
885                                 RCGetPromiscuousMode (dev,
886                                                       (PU32) & RCUD_GETPROM->
887                                                       PromMode, NULL);
888                                 break;
889                         case RCUC_GETBROADCAST:
890                                 RCUD_GETBROADCAST = &RCuser.RCUS_GETBROADCAST;
891                                 RCGetBroadcastMode (dev,
892                                                     (PU32) & RCUD_GETBROADCAST->
893                                                     BroadcastMode, NULL);
894                                 break;
895                         case RCUC_GETSPEED:
896                                 if (!(dev->flags & IFF_UP)) {
897                                         return -ENODATA;
898                                 }
899                                 RCUD_GETSPEED = &RCuser.RCUS_GETSPEED;
900                                 RCGetLinkSpeed (dev,
901                                                 (PU32) & RCUD_GETSPEED->
902                                                 LinkSpeedCode, NULL);
903                                 break;
904                         case RCUC_SETIPANDMASK:
905                                 RCUD_SETIPANDMASK = &RCuser.RCUS_SETIPANDMASK;
906                                 RCSetRavlinIPandMask (dev,
907                                                       (U32) RCUD_SETIPANDMASK->
908                                                       IpAddr,
909                                                       (U32) RCUD_SETIPANDMASK->
910                                                       NetMask);
911                                 break;
912                         case RCUC_SETMAC:
913                                 RCSetMAC (dev, (PU8) & RCUD_SETMAC->mac);
914                                 break;
915                         case RCUC_SETSPEED:
916                                 RCUD_SETSPEED = &RCuser.RCUS_SETSPEED;
917                                 RCSetLinkSpeed (dev,
918                                                 (U16) RCUD_SETSPEED->
919                                                 LinkSpeedCode);
920                                 break;
921                         case RCUC_SETPROM:
922                                 RCUD_SETPROM = &RCuser.RCUS_SETPROM;
923                                 RCSetPromiscuousMode (dev,
924                                                       (U16) RCUD_SETPROM->
925                                                       PromMode);
926                                 break;
927                         case RCUC_SETBROADCAST:
928                                 RCUD_SETBROADCAST = &RCuser.RCUS_SETBROADCAST;
929                                 RCSetBroadcastMode (dev,
930                                                     (U16) RCUD_SETBROADCAST->
931                                                     BroadcastMode);
932                                 break;
933                         default:
934                                 RCUD_DEFAULT = &RCuser.RCUS_DEFAULT;
935                                 RCUD_DEFAULT->rc = 0x11223344;
936                                 break;
937                         }
938                         if (copy_to_user (rq->ifr_data, &RCuser, 
939                                                 sizeof (RCuser)))
940                                 return -EFAULT;
941                         break;
942                 }               /* RCU_COMMAND */
943
944         default:
945                 rq->ifr_ifru.ifru_data = (caddr_t) 0x12345678;
946                 return -EINVAL;
947         }
948         return 0;
949 }
950
951 static int
952 RCconfig (struct net_device *dev, struct ifmap *map)
953 {
954         /*
955          * To be completed ...
956          */
957         return 0;
958         if (dev->flags & IFF_UP)        /* can't act on a running interface */
959                 return -EBUSY;
960
961         /* Don't allow changing the I/O address */
962         if (map->base_addr != dev->base_addr) {
963                 printk (KERN_WARNING "%s Change I/O address not implemented\n",
964                                 dev->name);
965                 return -EOPNOTSUPP;
966         }
967         return 0;
968 }
969
970 static void __exit
971 rcpci_cleanup_module (void)
972 {
973         pci_unregister_driver (&rcpci45_driver);
974 }
975
976 module_init (rcpci_init_module);
977 module_exit (rcpci_cleanup_module);
978
979 static int
980 RC_allocate_and_post_buffers (struct net_device *dev, int numBuffers)
981 {
982
983         int i;
984         PU32 p;
985         psingleB pB;
986         struct sk_buff *skb;
987         PDPA pDpa = dev->priv;
988         RC_RETURN status;
989         U32 res = 0;
990
991         if (!numBuffers)
992                 return 0;
993         else if (numBuffers > MAX_NMBR_POST_BUFFERS_PER_MSG) {
994                 printk (KERN_ERR "%s: Too many buffers requested!\n",
995                                 dev->name);
996                 numBuffers = 32;
997         }
998
999         p = (PU32) kmalloc (sizeof (U32) + numBuffers * sizeof (singleB),
1000                             GFP_DMA | GFP_ATOMIC);
1001
1002         if (!p) {
1003                 printk (KERN_WARNING "%s unable to allocate TCB\n",
1004                                 dev->name);
1005                 goto out;
1006         }
1007
1008         p[0] = 0;               /* Buffer Count */
1009         pB = (psingleB) ((U32) p + sizeof (U32));/* point to the first buffer */
1010
1011         for (i = 0; i < numBuffers; i++) {
1012                 skb = dev_alloc_skb (MAX_ETHER_SIZE + 2);
1013                 if (!skb) {
1014                         printk (KERN_WARNING 
1015                                         "%s: unable to allocate enough skbs!\n",
1016                                         dev->name);
1017                         goto err_out_unmap;
1018                 }
1019                 skb_reserve (skb, 2);   /* Align IP on 16 byte boundaries */
1020                 pB->context = (U32) skb;
1021                 pB->scount = 1; /* segment count */
1022                 pB->size = MAX_ETHER_SIZE;
1023                 pB->addr = pci_map_single(pDpa->pci_dev, skb->data, 
1024                                           MAX_ETHER_SIZE, PCI_DMA_FROMDEVICE);
1025                 p[0]++;
1026                 pB++;
1027         }
1028
1029         if ((status = RCPostRecvBuffers (dev, (PRCTCB) p)) != RC_RTN_NO_ERROR) {
1030                 printk (KERN_WARNING "%s: Post buffer failed, error 0x%x\n",
1031                                 dev->name, status);
1032                 goto err_out_unmap;
1033         }
1034 out_free:
1035         res = p[0];
1036         kfree (p);
1037 out:
1038         return (res);           /* return the number of posted buffers */
1039
1040 err_out_unmap:
1041         for (; p[0] > 0; p[0]--) {
1042                 --pB;
1043                 skb = (struct sk_buff *) pB->context;
1044                 pci_unmap_single(pDpa->pci_dev, pB->addr, MAX_ETHER_SIZE,
1045                                  PCI_DMA_FROMDEVICE);
1046                 dev_kfree_skb (skb);
1047         }
1048         goto out_free;
1049 }