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