vserver 1.9.3
[linux-2.6.git] / drivers / net / skfp / skfddi.c
1 /*
2  * File Name:
3  *   skfddi.c
4  *
5  * Copyright Information:
6  *   Copyright SysKonnect 1998,1999.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * The information in this file is provided "AS IS" without warranty.
14  *
15  * Abstract:
16  *   A Linux device driver supporting the SysKonnect FDDI PCI controller
17  *   familie.
18  *
19  * Maintainers:
20  *   CG    Christoph Goos (cgoos@syskonnect.de)
21  *
22  * Contributors:
23  *   DM    David S. Miller
24  *
25  * Address all question to:
26  *   linux@syskonnect.de
27  *
28  * The technical manual for the adapters is available from SysKonnect's
29  * web pages: www.syskonnect.com
30  * Goto "Support" and search Knowledge Base for "manual".
31  *
32  * Driver Architecture:
33  *   The driver architecture is based on the DEC FDDI driver by
34  *   Lawrence V. Stefani and several ethernet drivers.
35  *   I also used an existing Windows NT miniport driver.
36  *   All hardware dependent fuctions are handled by the SysKonnect
37  *   Hardware Module.
38  *   The only headerfiles that are directly related to this source
39  *   are skfddi.c, h/types.h, h/osdef1st.h, h/targetos.h.
40  *   The others belong to the SysKonnect FDDI Hardware Module and
41  *   should better not be changed.
42  *
43  * Modification History:
44  *              Date            Name    Description
45  *              02-Mar-98       CG      Created.
46  *
47  *              10-Mar-99       CG      Support for 2.2.x added.
48  *              25-Mar-99       CG      Corrected IRQ routing for SMP (APIC)
49  *              26-Oct-99       CG      Fixed compilation error on 2.2.13
50  *              12-Nov-99       CG      Source code release
51  *              22-Nov-99       CG      Included in kernel source.
52  *              07-May-00       DM      64 bit fixes, new dma interface
53  *              31-Jul-03       DB      Audit copy_*_user in skfp_ioctl
54  *                                        Daniele Bellucci <bellucda@tiscali.it>
55  *              03-Dec-03       SH      Convert to PCI device model
56  *
57  * Compilation options (-Dxxx):
58  *              DRIVERDEBUG     print lots of messages to log file
59  *              DUMPPACKETS     print received/transmitted packets to logfile
60  * 
61  * Tested cpu architectures:
62  *      - i386
63  *      - sparc64
64  */
65
66 /* Version information string - should be updated prior to */
67 /* each new release!!! */
68 #define VERSION         "2.07"
69
70 static const char *boot_msg = 
71         "SysKonnect FDDI PCI Adapter driver v" VERSION " for\n"
72         "  SK-55xx/SK-58xx adapters (SK-NET FDDI-FP/UP/LP)";
73
74 /* Include files */
75
76 #include <linux/module.h>
77 #include <linux/kernel.h>
78 #include <linux/errno.h>
79 #include <linux/ioport.h>
80 #include <linux/slab.h>
81 #include <linux/interrupt.h>
82 #include <linux/pci.h>
83 #include <linux/netdevice.h>
84 #include <linux/fddidevice.h>
85 #include <linux/skbuff.h>
86
87 #include <asm/byteorder.h>
88 #include <asm/bitops.h>
89 #include <asm/io.h>
90 #include <asm/uaccess.h>
91
92 #include        "h/types.h"
93 #undef ADDR                     // undo Linux definition
94 #include        "h/skfbi.h"
95 #include        "h/fddi.h"
96 #include        "h/smc.h"
97 #include        "h/smtstate.h"
98
99
100 // Define module-wide (static) routines
101 static int skfp_driver_init(struct net_device *dev);
102 static int skfp_open(struct net_device *dev);
103 static int skfp_close(struct net_device *dev);
104 static irqreturn_t skfp_interrupt(int irq, void *dev_id, struct pt_regs *regs);
105 static struct net_device_stats *skfp_ctl_get_stats(struct net_device *dev);
106 static void skfp_ctl_set_multicast_list(struct net_device *dev);
107 static void skfp_ctl_set_multicast_list_wo_lock(struct net_device *dev);
108 static int skfp_ctl_set_mac_address(struct net_device *dev, void *addr);
109 static int skfp_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
110 static int skfp_send_pkt(struct sk_buff *skb, struct net_device *dev);
111 static void send_queued_packets(struct s_smc *smc);
112 static void CheckSourceAddress(unsigned char *frame, unsigned char *hw_addr);
113 static void ResetAdapter(struct s_smc *smc);
114
115
116 // Functions needed by the hardware module
117 void *mac_drv_get_space(struct s_smc *smc, u_int size);
118 void *mac_drv_get_desc_mem(struct s_smc *smc, u_int size);
119 unsigned long mac_drv_virt2phys(struct s_smc *smc, void *virt);
120 unsigned long dma_master(struct s_smc *smc, void *virt, int len, int flag);
121 void dma_complete(struct s_smc *smc, volatile union s_fp_descr *descr,
122                   int flag);
123 void mac_drv_tx_complete(struct s_smc *smc, volatile struct s_smt_fp_txd *txd);
124 void llc_restart_tx(struct s_smc *smc);
125 void mac_drv_rx_complete(struct s_smc *smc, volatile struct s_smt_fp_rxd *rxd,
126                          int frag_count, int len);
127 void mac_drv_requeue_rxd(struct s_smc *smc, volatile struct s_smt_fp_rxd *rxd,
128                          int frag_count);
129 void mac_drv_fill_rxd(struct s_smc *smc);
130 void mac_drv_clear_rxd(struct s_smc *smc, volatile struct s_smt_fp_rxd *rxd,
131                        int frag_count);
132 int mac_drv_rx_init(struct s_smc *smc, int len, int fc, char *look_ahead,
133                     int la_len);
134 void dump_data(unsigned char *Data, int length);
135
136 // External functions from the hardware module
137 extern u_int mac_drv_check_space(void);
138 extern void read_address(struct s_smc *smc, u_char * mac_addr);
139 extern void card_stop(struct s_smc *smc);
140 extern int mac_drv_init(struct s_smc *smc);
141 extern void hwm_tx_frag(struct s_smc *smc, char far * virt, u_long phys,
142                         int len, int frame_status);
143 extern int hwm_tx_init(struct s_smc *smc, u_char fc, int frag_count,
144                        int frame_len, int frame_status);
145 extern int init_smt(struct s_smc *smc, u_char * mac_addr);
146 extern void fddi_isr(struct s_smc *smc);
147 extern void hwm_rx_frag(struct s_smc *smc, char far * virt, u_long phys,
148                         int len, int frame_status);
149 extern void mac_drv_rx_mode(struct s_smc *smc, int mode);
150 extern void mac_drv_clear_rx_queue(struct s_smc *smc);
151 extern void enable_tx_irq(struct s_smc *smc, u_short queue);
152 extern void mac_drv_clear_txd(struct s_smc *smc);
153
154 static struct pci_device_id skfddi_pci_tbl[] = {
155         { PCI_VENDOR_ID_SK, PCI_DEVICE_ID_SK_FP, PCI_ANY_ID, PCI_ANY_ID, },
156         { }                     /* Terminating entry */
157 };
158 MODULE_DEVICE_TABLE(pci, skfddi_pci_tbl);
159 MODULE_LICENSE("GPL");
160 MODULE_AUTHOR("Mirko Lindner <mlindner@syskonnect.de>");
161
162 // Define module-wide (static) variables
163
164 static int num_boards;  /* total number of adapters configured */
165
166 #ifdef DRIVERDEBUG
167 #define PRINTK(s, args...) printk(s, ## args)
168 #else
169 #define PRINTK(s, args...)
170 #endif                          // DRIVERDEBUG
171
172 #define PRIV(dev) (&(((struct s_smc *)dev->priv)->os))
173
174 /*
175  * =================
176  * = skfp_init_one =
177  * =================
178  *   
179  * Overview:
180  *   Probes for supported FDDI PCI controllers
181  *  
182  * Returns:
183  *   Condition code
184  *       
185  * Arguments:
186  *   pdev - pointer to PCI device information
187  *
188  * Functional Description:
189  *   This is now called by PCI driver registration process
190  *   for each board found.
191  *   
192  * Return Codes:
193  *   0           - This device (fddi0, fddi1, etc) configured successfully
194  *   -ENODEV - No devices present, or no SysKonnect FDDI PCI device
195  *                         present for this device name
196  *
197  *
198  * Side Effects:
199  *   Device structures for FDDI adapters (fddi0, fddi1, etc) are
200  *   initialized and the board resources are read and stored in
201  *   the device structure.
202  */
203 static int skfp_init_one(struct pci_dev *pdev,
204                                 const struct pci_device_id *ent)
205 {
206         struct net_device *dev;
207         struct s_smc *smc;      /* board pointer */
208         unsigned long port, len;
209         int err;
210
211         PRINTK(KERN_INFO "entering skfp_init_one\n");
212
213         if (num_boards == 0) 
214                 printk("%s\n", boot_msg);
215
216         err = pci_enable_device(pdev);
217         if (err)
218                 goto err_out1;
219
220
221 #ifdef MEM_MAPPED_IO
222         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
223                 printk(KERN_ERR "skfp: region is not an MMIO resource\n");
224                 err = -EIO;
225                 goto err_out1;
226         }
227         port = pci_resource_start(pdev, 0);
228         len = pci_resource_len(pdev, 0);
229
230         if (len < 0x4000) {
231                 printk(KERN_ERR "skfp: Invalid PCI region size: %lu\n", len);
232                 err = -EIO;
233                 goto err_out1;
234         }
235 #else
236         if (!(pci_resource_flags(pdev, 1) & IO_RESOURCE_IO)) {
237                 printk(KERN_ERR "skfp: region is not PIO resource\n");
238                 err = -EIO;
239                 goto err_out1;
240         }
241
242         port = pci_resource_start(pdev, 1);
243         len = pci_resource_len(pdev, 1);
244         if (len < FP_IO_LEN) {
245                 printk(KERN_ERR "skfp: Invalid PCI region size: %d\n",
246                        io_len);
247                 err = -EIO;
248                 goto err_out1;
249         }
250 #endif
251         err = pci_request_regions(pdev, "skfddi");
252         if (err)
253                 goto err_out1;
254
255         pci_set_master(pdev);
256
257         dev = alloc_fddidev(sizeof(struct s_smc));
258         if (!dev) {
259                 printk(KERN_ERR "skfp: Unable to allocate fddi device, "
260                                 "FDDI adapter will be disabled.\n");
261                 err = -ENOMEM;
262                 goto err_out2;
263         }
264
265 #ifdef MEM_MAPPED_IO
266         dev->base_addr = (unsigned long) ioremap(port, len);
267         if (!dev->base_addr) {
268                 printk(KERN_ERR "skfp:  Unable to map MEMORY register, "
269                                 "FDDI adapter will be disabled.\n");
270                 err = -EIO;
271                 goto err_out3;
272         }
273 #else
274         dev->base_addr = port;
275 #endif
276
277         dev->irq = pdev->irq;
278         dev->get_stats = &skfp_ctl_get_stats;
279         dev->open = &skfp_open;
280         dev->stop = &skfp_close;
281         dev->hard_start_xmit = &skfp_send_pkt;
282         dev->set_multicast_list = &skfp_ctl_set_multicast_list;
283         dev->set_mac_address = &skfp_ctl_set_mac_address;
284         dev->do_ioctl = &skfp_ioctl;
285         dev->header_cache_update = NULL;        /* not supported */
286
287         SET_MODULE_OWNER(dev);
288         SET_NETDEV_DEV(dev, &pdev->dev);
289
290         /* Initialize board structure with bus-specific info */
291         smc = (struct s_smc *) dev->priv;
292         smc->os.dev = dev;
293         smc->os.bus_type = SK_BUS_TYPE_PCI;
294         smc->os.pdev = *pdev;
295         smc->os.QueueSkb = MAX_TX_QUEUE_LEN;
296         smc->os.MaxFrameSize = MAX_FRAME_SIZE;
297         smc->os.dev = dev;
298         smc->hw.slot = -1;
299         smc->os.ResetRequested = FALSE;
300         skb_queue_head_init(&smc->os.SendSkbQueue);
301
302         err = skfp_driver_init(dev);
303         if (err)
304                 goto err_out4;
305
306         err = register_netdev(dev);
307         if (err)
308                 goto err_out5;
309
310         ++num_boards;
311         pci_set_drvdata(pdev, dev);
312
313         if ((pdev->subsystem_device & 0xff00) == 0x5500 ||
314             (pdev->subsystem_device & 0xff00) == 0x5800) 
315                 printk("%s: SysKonnect FDDI PCI adapter"
316                        " found (SK-%04X)\n", dev->name, 
317                        pdev->subsystem_device);
318         else
319                 printk("%s: FDDI PCI adapter found\n", dev->name);
320
321         return 0;
322 err_out5:
323         if (smc->os.SharedMemAddr) 
324                 pci_free_consistent(pdev, smc->os.SharedMemSize,
325                                     smc->os.SharedMemAddr, 
326                                     smc->os.SharedMemDMA);
327         pci_free_consistent(pdev, MAX_FRAME_SIZE,
328                             smc->os.LocalRxBuffer, smc->os.LocalRxBufferDMA);
329 err_out4:
330 #ifdef MEM_MAPPED_IO
331         iounmap((void *) dev->base_addr);
332 #endif
333 err_out3:
334         free_netdev(dev);
335 err_out2:
336         pci_release_regions(pdev);
337 err_out1:
338         return err;
339 }
340
341 /*
342  * Called for each adapter board from pci_unregister_driver
343  */
344 static void __devexit skfp_remove_one(struct pci_dev *pdev)
345 {
346         struct net_device *p = pci_get_drvdata(pdev);
347         struct s_smc *lp = p->priv;
348
349         unregister_netdev(p);
350
351         if (lp->os.SharedMemAddr) {
352                 pci_free_consistent(&lp->os.pdev,
353                                     lp->os.SharedMemSize,
354                                     lp->os.SharedMemAddr,
355                                     lp->os.SharedMemDMA);
356                 lp->os.SharedMemAddr = NULL;
357         }
358         if (lp->os.LocalRxBuffer) {
359                 pci_free_consistent(&lp->os.pdev,
360                                     MAX_FRAME_SIZE,
361                                     lp->os.LocalRxBuffer,
362                                     lp->os.LocalRxBufferDMA);
363                 lp->os.LocalRxBuffer = NULL;
364         }
365 #ifdef MEM_MAPPED_IO
366         iounmap((void *) p->base_addr);
367 #endif
368         pci_release_regions(pdev);
369         free_netdev(p);
370
371         pci_set_drvdata(pdev, NULL);
372 }
373
374 /*
375  * ====================
376  * = skfp_driver_init =
377  * ====================
378  *   
379  * Overview:
380  *   Initializes remaining adapter board structure information
381  *   and makes sure adapter is in a safe state prior to skfp_open().
382  *  
383  * Returns:
384  *   Condition code
385  *       
386  * Arguments:
387  *   dev - pointer to device information
388  *
389  * Functional Description:
390  *   This function allocates additional resources such as the host memory
391  *   blocks needed by the adapter.
392  *   The adapter is also reset. The OS must call skfp_open() to open 
393  *   the adapter and bring it on-line.
394  *
395  * Return Codes:
396  *    0 - initialization succeeded
397  *   -1 - initialization failed
398  */
399 static  int skfp_driver_init(struct net_device *dev)
400 {
401         struct s_smc *smc = (struct s_smc *) dev->priv;
402         skfddi_priv *bp = PRIV(dev);
403         int err = -EIO;
404
405         PRINTK(KERN_INFO "entering skfp_driver_init\n");
406
407         // set the io address in private structures
408         bp->base_addr = dev->base_addr;
409         smc->hw.iop = dev->base_addr;
410
411         // Get the interrupt level from the PCI Configuration Table
412         smc->hw.irq = dev->irq;
413
414         spin_lock_init(&bp->DriverLock);
415         
416         // Allocate invalid frame
417         bp->LocalRxBuffer = pci_alloc_consistent(&bp->pdev, MAX_FRAME_SIZE, &bp->LocalRxBufferDMA);
418         if (!bp->LocalRxBuffer) {
419                 printk("could not allocate mem for ");
420                 printk("LocalRxBuffer: %d byte\n", MAX_FRAME_SIZE);
421                 goto fail;
422         }
423
424         // Determine the required size of the 'shared' memory area.
425         bp->SharedMemSize = mac_drv_check_space();
426         PRINTK(KERN_INFO "Memory for HWM: %ld\n", bp->SharedMemSize);
427         if (bp->SharedMemSize > 0) {
428                 bp->SharedMemSize += 16;        // for descriptor alignment
429
430                 bp->SharedMemAddr = pci_alloc_consistent(&bp->pdev,
431                                                          bp->SharedMemSize,
432                                                          &bp->SharedMemDMA);
433                 if (!bp->SharedMemSize) {
434                         printk("could not allocate mem for ");
435                         printk("hardware module: %ld byte\n",
436                                bp->SharedMemSize);
437                         goto fail;
438                 }
439                 bp->SharedMemHeap = 0;  // Nothing used yet.
440
441         } else {
442                 bp->SharedMemAddr = NULL;
443                 bp->SharedMemHeap = 0;
444         }                       // SharedMemSize > 0
445
446         memset(bp->SharedMemAddr, 0, bp->SharedMemSize);
447
448         card_stop(smc);         // Reset adapter.
449
450         PRINTK(KERN_INFO "mac_drv_init()..\n");
451         if (mac_drv_init(smc) != 0) {
452                 PRINTK(KERN_INFO "mac_drv_init() failed.\n");
453                 goto fail;
454         }
455         read_address(smc, NULL);
456         PRINTK(KERN_INFO "HW-Addr: %02x %02x %02x %02x %02x %02x\n",
457                smc->hw.fddi_canon_addr.a[0],
458                smc->hw.fddi_canon_addr.a[1],
459                smc->hw.fddi_canon_addr.a[2],
460                smc->hw.fddi_canon_addr.a[3],
461                smc->hw.fddi_canon_addr.a[4],
462                smc->hw.fddi_canon_addr.a[5]);
463         memcpy(dev->dev_addr, smc->hw.fddi_canon_addr.a, 6);
464
465         smt_reset_defaults(smc, 0);
466
467         return (0);
468
469 fail:
470         if (bp->SharedMemAddr) {
471                 pci_free_consistent(&bp->pdev,
472                                     bp->SharedMemSize,
473                                     bp->SharedMemAddr,
474                                     bp->SharedMemDMA);
475                 bp->SharedMemAddr = NULL;
476         }
477         if (bp->LocalRxBuffer) {
478                 pci_free_consistent(&bp->pdev, MAX_FRAME_SIZE,
479                                     bp->LocalRxBuffer, bp->LocalRxBufferDMA);
480                 bp->LocalRxBuffer = NULL;
481         }
482         return err;
483 }                               // skfp_driver_init
484
485
486 /*
487  * =============
488  * = skfp_open =
489  * =============
490  *   
491  * Overview:
492  *   Opens the adapter
493  *  
494  * Returns:
495  *   Condition code
496  *       
497  * Arguments:
498  *   dev - pointer to device information
499  *
500  * Functional Description:
501  *   This function brings the adapter to an operational state.
502  *
503  * Return Codes:
504  *   0           - Adapter was successfully opened
505  *   -EAGAIN - Could not register IRQ
506  */
507 static int skfp_open(struct net_device *dev)
508 {
509         struct s_smc *smc = (struct s_smc *) dev->priv;
510         int err;
511
512         PRINTK(KERN_INFO "entering skfp_open\n");
513         /* Register IRQ - support shared interrupts by passing device ptr */
514         err = request_irq(dev->irq, (void *) skfp_interrupt, SA_SHIRQ,
515                           dev->name, dev);
516         if (err)
517                 return err;
518
519         /*
520          * Set current address to factory MAC address
521          *
522          * Note: We've already done this step in skfp_driver_init.
523          *       However, it's possible that a user has set a node
524          *               address override, then closed and reopened the
525          *               adapter.  Unless we reset the device address field
526          *               now, we'll continue to use the existing modified
527          *               address.
528          */
529         read_address(smc, NULL);
530         memcpy(dev->dev_addr, smc->hw.fddi_canon_addr.a, 6);
531
532         init_smt(smc, NULL);
533         smt_online(smc, 1);
534         STI_FBI();
535
536         /* Clear local multicast address tables */
537         mac_clear_multicast(smc);
538
539         /* Disable promiscuous filter settings */
540         mac_drv_rx_mode(smc, RX_DISABLE_PROMISC);
541
542         netif_start_queue(dev);
543         return (0);
544 }                               // skfp_open
545
546
547 /*
548  * ==============
549  * = skfp_close =
550  * ==============
551  *   
552  * Overview:
553  *   Closes the device/module.
554  *  
555  * Returns:
556  *   Condition code
557  *       
558  * Arguments:
559  *   dev - pointer to device information
560  *
561  * Functional Description:
562  *   This routine closes the adapter and brings it to a safe state.
563  *   The interrupt service routine is deregistered with the OS.
564  *   The adapter can be opened again with another call to skfp_open().
565  *
566  * Return Codes:
567  *   Always return 0.
568  *
569  * Assumptions:
570  *   No further requests for this adapter are made after this routine is
571  *   called.  skfp_open() can be called to reset and reinitialize the
572  *   adapter.
573  */
574 static int skfp_close(struct net_device *dev)
575 {
576         struct s_smc *smc = (struct s_smc *) dev->priv;
577         skfddi_priv *bp = PRIV(dev);
578
579         CLI_FBI();
580         smt_reset_defaults(smc, 1);
581         card_stop(smc);
582         mac_drv_clear_tx_queue(smc);
583         mac_drv_clear_rx_queue(smc);
584
585         netif_stop_queue(dev);
586         /* Deregister (free) IRQ */
587         free_irq(dev->irq, dev);
588
589         skb_queue_purge(&bp->SendSkbQueue);
590         bp->QueueSkb = MAX_TX_QUEUE_LEN;
591
592         return (0);
593 }                               // skfp_close
594
595
596 /*
597  * ==================
598  * = skfp_interrupt =
599  * ==================
600  *   
601  * Overview:
602  *   Interrupt processing routine
603  *  
604  * Returns:
605  *   None
606  *       
607  * Arguments:
608  *   irq        - interrupt vector
609  *   dev_id     - pointer to device information
610  *       regs   - pointer to registers structure
611  *
612  * Functional Description:
613  *   This routine calls the interrupt processing routine for this adapter.  It
614  *   disables and reenables adapter interrupts, as appropriate.  We can support
615  *   shared interrupts since the incoming dev_id pointer provides our device
616  *   structure context. All the real work is done in the hardware module.
617  *
618  * Return Codes:
619  *   None
620  *
621  * Assumptions:
622  *   The interrupt acknowledgement at the hardware level (eg. ACKing the PIC
623  *   on Intel-based systems) is done by the operating system outside this
624  *   routine.
625  *
626  *       System interrupts are enabled through this call.
627  *
628  * Side Effects:
629  *   Interrupts are disabled, then reenabled at the adapter.
630  */
631
632 irqreturn_t skfp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
633 {
634         struct net_device *dev = (struct net_device *) dev_id;
635         struct s_smc *smc;      /* private board structure pointer */
636         skfddi_priv *bp = PRIV(dev);
637
638
639         if (dev == NULL) {
640                 printk("%s: irq %d for unknown device\n", dev->name, irq);
641                 return IRQ_NONE;
642         }
643
644         smc = (struct s_smc *) dev->priv;
645
646         // IRQs enabled or disabled ?
647         if (inpd(ADDR(B0_IMSK)) == 0) {
648                 // IRQs are disabled: must be shared interrupt
649                 return IRQ_NONE;
650         }
651         // Note: At this point, IRQs are enabled.
652         if ((inpd(ISR_A) & smc->hw.is_imask) == 0) {    // IRQ?
653                 // Adapter did not issue an IRQ: must be shared interrupt
654                 return IRQ_NONE;
655         }
656         CLI_FBI();              // Disable IRQs from our adapter.
657         spin_lock(&bp->DriverLock);
658
659         // Call interrupt handler in hardware module (HWM).
660         fddi_isr(smc);
661
662         if (smc->os.ResetRequested) {
663                 ResetAdapter(smc);
664                 smc->os.ResetRequested = FALSE;
665         }
666         spin_unlock(&bp->DriverLock);
667         STI_FBI();              // Enable IRQs from our adapter.
668
669         return IRQ_HANDLED;
670 }                               // skfp_interrupt
671
672
673 /*
674  * ======================
675  * = skfp_ctl_get_stats =
676  * ======================
677  *   
678  * Overview:
679  *   Get statistics for FDDI adapter
680  *  
681  * Returns:
682  *   Pointer to FDDI statistics structure
683  *       
684  * Arguments:
685  *   dev - pointer to device information
686  *
687  * Functional Description:
688  *   Gets current MIB objects from adapter, then
689  *   returns FDDI statistics structure as defined
690  *   in if_fddi.h.
691  *
692  *   Note: Since the FDDI statistics structure is
693  *   still new and the device structure doesn't
694  *   have an FDDI-specific get statistics handler,
695  *   we'll return the FDDI statistics structure as
696  *   a pointer to an Ethernet statistics structure.
697  *   That way, at least the first part of the statistics
698  *   structure can be decoded properly.
699  *   We'll have to pay attention to this routine as the
700  *   device structure becomes more mature and LAN media
701  *   independent.
702  *
703  */
704 struct net_device_stats *skfp_ctl_get_stats(struct net_device *dev)
705 {
706         struct s_smc *bp = (struct s_smc *) dev->priv;
707
708         /* Fill the bp->stats structure with driver-maintained counters */
709
710         bp->os.MacStat.port_bs_flag[0] = 0x1234;
711         bp->os.MacStat.port_bs_flag[1] = 0x5678;
712 // goos: need to fill out fddi statistic
713 #if 0
714         /* Get FDDI SMT MIB objects */
715
716 /* Fill the bp->stats structure with the SMT MIB object values */
717
718         memcpy(bp->stats.smt_station_id, &bp->cmd_rsp_virt->smt_mib_get.smt_station_id, sizeof(bp->cmd_rsp_virt->smt_mib_get.smt_station_id));
719         bp->stats.smt_op_version_id = bp->cmd_rsp_virt->smt_mib_get.smt_op_version_id;
720         bp->stats.smt_hi_version_id = bp->cmd_rsp_virt->smt_mib_get.smt_hi_version_id;
721         bp->stats.smt_lo_version_id = bp->cmd_rsp_virt->smt_mib_get.smt_lo_version_id;
722         memcpy(bp->stats.smt_user_data, &bp->cmd_rsp_virt->smt_mib_get.smt_user_data, sizeof(bp->cmd_rsp_virt->smt_mib_get.smt_user_data));
723         bp->stats.smt_mib_version_id = bp->cmd_rsp_virt->smt_mib_get.smt_mib_version_id;
724         bp->stats.smt_mac_cts = bp->cmd_rsp_virt->smt_mib_get.smt_mac_ct;
725         bp->stats.smt_non_master_cts = bp->cmd_rsp_virt->smt_mib_get.smt_non_master_ct;
726         bp->stats.smt_master_cts = bp->cmd_rsp_virt->smt_mib_get.smt_master_ct;
727         bp->stats.smt_available_paths = bp->cmd_rsp_virt->smt_mib_get.smt_available_paths;
728         bp->stats.smt_config_capabilities = bp->cmd_rsp_virt->smt_mib_get.smt_config_capabilities;
729         bp->stats.smt_config_policy = bp->cmd_rsp_virt->smt_mib_get.smt_config_policy;
730         bp->stats.smt_connection_policy = bp->cmd_rsp_virt->smt_mib_get.smt_connection_policy;
731         bp->stats.smt_t_notify = bp->cmd_rsp_virt->smt_mib_get.smt_t_notify;
732         bp->stats.smt_stat_rpt_policy = bp->cmd_rsp_virt->smt_mib_get.smt_stat_rpt_policy;
733         bp->stats.smt_trace_max_expiration = bp->cmd_rsp_virt->smt_mib_get.smt_trace_max_expiration;
734         bp->stats.smt_bypass_present = bp->cmd_rsp_virt->smt_mib_get.smt_bypass_present;
735         bp->stats.smt_ecm_state = bp->cmd_rsp_virt->smt_mib_get.smt_ecm_state;
736         bp->stats.smt_cf_state = bp->cmd_rsp_virt->smt_mib_get.smt_cf_state;
737         bp->stats.smt_remote_disconnect_flag = bp->cmd_rsp_virt->smt_mib_get.smt_remote_disconnect_flag;
738         bp->stats.smt_station_status = bp->cmd_rsp_virt->smt_mib_get.smt_station_status;
739         bp->stats.smt_peer_wrap_flag = bp->cmd_rsp_virt->smt_mib_get.smt_peer_wrap_flag;
740         bp->stats.smt_time_stamp = bp->cmd_rsp_virt->smt_mib_get.smt_msg_time_stamp.ls;
741         bp->stats.smt_transition_time_stamp = bp->cmd_rsp_virt->smt_mib_get.smt_transition_time_stamp.ls;
742         bp->stats.mac_frame_status_functions = bp->cmd_rsp_virt->smt_mib_get.mac_frame_status_functions;
743         bp->stats.mac_t_max_capability = bp->cmd_rsp_virt->smt_mib_get.mac_t_max_capability;
744         bp->stats.mac_tvx_capability = bp->cmd_rsp_virt->smt_mib_get.mac_tvx_capability;
745         bp->stats.mac_available_paths = bp->cmd_rsp_virt->smt_mib_get.mac_available_paths;
746         bp->stats.mac_current_path = bp->cmd_rsp_virt->smt_mib_get.mac_current_path;
747         memcpy(bp->stats.mac_upstream_nbr, &bp->cmd_rsp_virt->smt_mib_get.mac_upstream_nbr, FDDI_K_ALEN);
748         memcpy(bp->stats.mac_downstream_nbr, &bp->cmd_rsp_virt->smt_mib_get.mac_downstream_nbr, FDDI_K_ALEN);
749         memcpy(bp->stats.mac_old_upstream_nbr, &bp->cmd_rsp_virt->smt_mib_get.mac_old_upstream_nbr, FDDI_K_ALEN);
750         memcpy(bp->stats.mac_old_downstream_nbr, &bp->cmd_rsp_virt->smt_mib_get.mac_old_downstream_nbr, FDDI_K_ALEN);
751         bp->stats.mac_dup_address_test = bp->cmd_rsp_virt->smt_mib_get.mac_dup_address_test;
752         bp->stats.mac_requested_paths = bp->cmd_rsp_virt->smt_mib_get.mac_requested_paths;
753         bp->stats.mac_downstream_port_type = bp->cmd_rsp_virt->smt_mib_get.mac_downstream_port_type;
754         memcpy(bp->stats.mac_smt_address, &bp->cmd_rsp_virt->smt_mib_get.mac_smt_address, FDDI_K_ALEN);
755         bp->stats.mac_t_req = bp->cmd_rsp_virt->smt_mib_get.mac_t_req;
756         bp->stats.mac_t_neg = bp->cmd_rsp_virt->smt_mib_get.mac_t_neg;
757         bp->stats.mac_t_max = bp->cmd_rsp_virt->smt_mib_get.mac_t_max;
758         bp->stats.mac_tvx_value = bp->cmd_rsp_virt->smt_mib_get.mac_tvx_value;
759         bp->stats.mac_frame_error_threshold = bp->cmd_rsp_virt->smt_mib_get.mac_frame_error_threshold;
760         bp->stats.mac_frame_error_ratio = bp->cmd_rsp_virt->smt_mib_get.mac_frame_error_ratio;
761         bp->stats.mac_rmt_state = bp->cmd_rsp_virt->smt_mib_get.mac_rmt_state;
762         bp->stats.mac_da_flag = bp->cmd_rsp_virt->smt_mib_get.mac_da_flag;
763         bp->stats.mac_una_da_flag = bp->cmd_rsp_virt->smt_mib_get.mac_unda_flag;
764         bp->stats.mac_frame_error_flag = bp->cmd_rsp_virt->smt_mib_get.mac_frame_error_flag;
765         bp->stats.mac_ma_unitdata_available = bp->cmd_rsp_virt->smt_mib_get.mac_ma_unitdata_available;
766         bp->stats.mac_hardware_present = bp->cmd_rsp_virt->smt_mib_get.mac_hardware_present;
767         bp->stats.mac_ma_unitdata_enable = bp->cmd_rsp_virt->smt_mib_get.mac_ma_unitdata_enable;
768         bp->stats.path_tvx_lower_bound = bp->cmd_rsp_virt->smt_mib_get.path_tvx_lower_bound;
769         bp->stats.path_t_max_lower_bound = bp->cmd_rsp_virt->smt_mib_get.path_t_max_lower_bound;
770         bp->stats.path_max_t_req = bp->cmd_rsp_virt->smt_mib_get.path_max_t_req;
771         memcpy(bp->stats.path_configuration, &bp->cmd_rsp_virt->smt_mib_get.path_configuration, sizeof(bp->cmd_rsp_virt->smt_mib_get.path_configuration));
772         bp->stats.port_my_type[0] = bp->cmd_rsp_virt->smt_mib_get.port_my_type[0];
773         bp->stats.port_my_type[1] = bp->cmd_rsp_virt->smt_mib_get.port_my_type[1];
774         bp->stats.port_neighbor_type[0] = bp->cmd_rsp_virt->smt_mib_get.port_neighbor_type[0];
775         bp->stats.port_neighbor_type[1] = bp->cmd_rsp_virt->smt_mib_get.port_neighbor_type[1];
776         bp->stats.port_connection_policies[0] = bp->cmd_rsp_virt->smt_mib_get.port_connection_policies[0];
777         bp->stats.port_connection_policies[1] = bp->cmd_rsp_virt->smt_mib_get.port_connection_policies[1];
778         bp->stats.port_mac_indicated[0] = bp->cmd_rsp_virt->smt_mib_get.port_mac_indicated[0];
779         bp->stats.port_mac_indicated[1] = bp->cmd_rsp_virt->smt_mib_get.port_mac_indicated[1];
780         bp->stats.port_current_path[0] = bp->cmd_rsp_virt->smt_mib_get.port_current_path[0];
781         bp->stats.port_current_path[1] = bp->cmd_rsp_virt->smt_mib_get.port_current_path[1];
782         memcpy(&bp->stats.port_requested_paths[0 * 3], &bp->cmd_rsp_virt->smt_mib_get.port_requested_paths[0], 3);
783         memcpy(&bp->stats.port_requested_paths[1 * 3], &bp->cmd_rsp_virt->smt_mib_get.port_requested_paths[1], 3);
784         bp->stats.port_mac_placement[0] = bp->cmd_rsp_virt->smt_mib_get.port_mac_placement[0];
785         bp->stats.port_mac_placement[1] = bp->cmd_rsp_virt->smt_mib_get.port_mac_placement[1];
786         bp->stats.port_available_paths[0] = bp->cmd_rsp_virt->smt_mib_get.port_available_paths[0];
787         bp->stats.port_available_paths[1] = bp->cmd_rsp_virt->smt_mib_get.port_available_paths[1];
788         bp->stats.port_pmd_class[0] = bp->cmd_rsp_virt->smt_mib_get.port_pmd_class[0];
789         bp->stats.port_pmd_class[1] = bp->cmd_rsp_virt->smt_mib_get.port_pmd_class[1];
790         bp->stats.port_connection_capabilities[0] = bp->cmd_rsp_virt->smt_mib_get.port_connection_capabilities[0];
791         bp->stats.port_connection_capabilities[1] = bp->cmd_rsp_virt->smt_mib_get.port_connection_capabilities[1];
792         bp->stats.port_bs_flag[0] = bp->cmd_rsp_virt->smt_mib_get.port_bs_flag[0];
793         bp->stats.port_bs_flag[1] = bp->cmd_rsp_virt->smt_mib_get.port_bs_flag[1];
794         bp->stats.port_ler_estimate[0] = bp->cmd_rsp_virt->smt_mib_get.port_ler_estimate[0];
795         bp->stats.port_ler_estimate[1] = bp->cmd_rsp_virt->smt_mib_get.port_ler_estimate[1];
796         bp->stats.port_ler_cutoff[0] = bp->cmd_rsp_virt->smt_mib_get.port_ler_cutoff[0];
797         bp->stats.port_ler_cutoff[1] = bp->cmd_rsp_virt->smt_mib_get.port_ler_cutoff[1];
798         bp->stats.port_ler_alarm[0] = bp->cmd_rsp_virt->smt_mib_get.port_ler_alarm[0];
799         bp->stats.port_ler_alarm[1] = bp->cmd_rsp_virt->smt_mib_get.port_ler_alarm[1];
800         bp->stats.port_connect_state[0] = bp->cmd_rsp_virt->smt_mib_get.port_connect_state[0];
801         bp->stats.port_connect_state[1] = bp->cmd_rsp_virt->smt_mib_get.port_connect_state[1];
802         bp->stats.port_pcm_state[0] = bp->cmd_rsp_virt->smt_mib_get.port_pcm_state[0];
803         bp->stats.port_pcm_state[1] = bp->cmd_rsp_virt->smt_mib_get.port_pcm_state[1];
804         bp->stats.port_pc_withhold[0] = bp->cmd_rsp_virt->smt_mib_get.port_pc_withhold[0];
805         bp->stats.port_pc_withhold[1] = bp->cmd_rsp_virt->smt_mib_get.port_pc_withhold[1];
806         bp->stats.port_ler_flag[0] = bp->cmd_rsp_virt->smt_mib_get.port_ler_flag[0];
807         bp->stats.port_ler_flag[1] = bp->cmd_rsp_virt->smt_mib_get.port_ler_flag[1];
808         bp->stats.port_hardware_present[0] = bp->cmd_rsp_virt->smt_mib_get.port_hardware_present[0];
809         bp->stats.port_hardware_present[1] = bp->cmd_rsp_virt->smt_mib_get.port_hardware_present[1];
810
811
812         /* Fill the bp->stats structure with the FDDI counter values */
813
814         bp->stats.mac_frame_cts = bp->cmd_rsp_virt->cntrs_get.cntrs.frame_cnt.ls;
815         bp->stats.mac_copied_cts = bp->cmd_rsp_virt->cntrs_get.cntrs.copied_cnt.ls;
816         bp->stats.mac_transmit_cts = bp->cmd_rsp_virt->cntrs_get.cntrs.transmit_cnt.ls;
817         bp->stats.mac_error_cts = bp->cmd_rsp_virt->cntrs_get.cntrs.error_cnt.ls;
818         bp->stats.mac_lost_cts = bp->cmd_rsp_virt->cntrs_get.cntrs.lost_cnt.ls;
819         bp->stats.port_lct_fail_cts[0] = bp->cmd_rsp_virt->cntrs_get.cntrs.lct_rejects[0].ls;
820         bp->stats.port_lct_fail_cts[1] = bp->cmd_rsp_virt->cntrs_get.cntrs.lct_rejects[1].ls;
821         bp->stats.port_lem_reject_cts[0] = bp->cmd_rsp_virt->cntrs_get.cntrs.lem_rejects[0].ls;
822         bp->stats.port_lem_reject_cts[1] = bp->cmd_rsp_virt->cntrs_get.cntrs.lem_rejects[1].ls;
823         bp->stats.port_lem_cts[0] = bp->cmd_rsp_virt->cntrs_get.cntrs.link_errors[0].ls;
824         bp->stats.port_lem_cts[1] = bp->cmd_rsp_virt->cntrs_get.cntrs.link_errors[1].ls;
825
826 #endif
827         return ((struct net_device_stats *) &bp->os.MacStat);
828 }                               // ctl_get_stat
829
830
831 /*
832  * ==============================
833  * = skfp_ctl_set_multicast_list =
834  * ==============================
835  *   
836  * Overview:
837  *   Enable/Disable LLC frame promiscuous mode reception
838  *   on the adapter and/or update multicast address table.
839  *  
840  * Returns:
841  *   None
842  *       
843  * Arguments:
844  *   dev - pointer to device information
845  *
846  * Functional Description:
847  *   This function acquires the driver lock and only calls
848  *   skfp_ctl_set_multicast_list_wo_lock then.
849  *   This routine follows a fairly simple algorithm for setting the
850  *   adapter filters and CAM:
851  *
852  *      if IFF_PROMISC flag is set
853  *              enable promiscuous mode
854  *      else
855  *              disable promiscuous mode
856  *              if number of multicast addresses <= max. multicast number
857  *                      add mc addresses to adapter table
858  *              else
859  *                      enable promiscuous mode
860  *              update adapter filters
861  *
862  * Assumptions:
863  *   Multicast addresses are presented in canonical (LSB) format.
864  *
865  * Side Effects:
866  *   On-board adapter filters are updated.
867  */
868 static void skfp_ctl_set_multicast_list(struct net_device *dev)
869 {
870         skfddi_priv *bp = PRIV(dev);
871         unsigned long Flags;
872
873         spin_lock_irqsave(&bp->DriverLock, Flags);
874         skfp_ctl_set_multicast_list_wo_lock(dev);
875         spin_unlock_irqrestore(&bp->DriverLock, Flags);
876         return;
877 }                               // skfp_ctl_set_multicast_list
878
879
880
881 static void skfp_ctl_set_multicast_list_wo_lock(struct net_device *dev)
882 {
883         struct s_smc *smc = (struct s_smc *) dev->priv;
884         struct dev_mc_list *dmi;        /* ptr to multicast addr entry */
885         int i;
886
887         /* Enable promiscuous mode, if necessary */
888         if (dev->flags & IFF_PROMISC) {
889                 mac_drv_rx_mode(smc, RX_ENABLE_PROMISC);
890                 PRINTK(KERN_INFO "PROMISCUOUS MODE ENABLED\n");
891         }
892         /* Else, update multicast address table */
893         else {
894                 mac_drv_rx_mode(smc, RX_DISABLE_PROMISC);
895                 PRINTK(KERN_INFO "PROMISCUOUS MODE DISABLED\n");
896
897                 // Reset all MC addresses
898                 mac_clear_multicast(smc);
899                 mac_drv_rx_mode(smc, RX_DISABLE_ALLMULTI);
900
901                 if (dev->flags & IFF_ALLMULTI) {
902                         mac_drv_rx_mode(smc, RX_ENABLE_ALLMULTI);
903                         PRINTK(KERN_INFO "ENABLE ALL MC ADDRESSES\n");
904                 } else if (dev->mc_count > 0) {
905                         if (dev->mc_count <= FPMAX_MULTICAST) {
906                                 /* use exact filtering */
907
908                                 // point to first multicast addr
909                                 dmi = dev->mc_list;
910
911                                 for (i = 0; i < dev->mc_count; i++) {
912                                         mac_add_multicast(smc, 
913                                                           (struct fddi_addr *)dmi->dmi_addr, 
914                                                           1);
915
916                                         PRINTK(KERN_INFO "ENABLE MC ADDRESS:");
917                                         PRINTK(" %02x %02x %02x ",
918                                                dmi->dmi_addr[0],
919                                                dmi->dmi_addr[1],
920                                                dmi->dmi_addr[2]);
921                                         PRINTK("%02x %02x %02x\n",
922                                                dmi->dmi_addr[3],
923                                                dmi->dmi_addr[4],
924                                                dmi->dmi_addr[5]);
925                                         dmi = dmi->next;
926                                 }       // for
927
928                         } else {        // more MC addresses than HW supports
929
930                                 mac_drv_rx_mode(smc, RX_ENABLE_ALLMULTI);
931                                 PRINTK(KERN_INFO "ENABLE ALL MC ADDRESSES\n");
932                         }
933                 } else {        // no MC addresses
934
935                         PRINTK(KERN_INFO "DISABLE ALL MC ADDRESSES\n");
936                 }
937
938                 /* Update adapter filters */
939                 mac_update_multicast(smc);
940         }
941         return;
942 }                               // skfp_ctl_set_multicast_list_wo_lock
943
944
945 /*
946  * ===========================
947  * = skfp_ctl_set_mac_address =
948  * ===========================
949  *   
950  * Overview:
951  *   set new mac address on adapter and update dev_addr field in device table.
952  *  
953  * Returns:
954  *   None
955  *       
956  * Arguments:
957  *   dev  - pointer to device information
958  *   addr - pointer to sockaddr structure containing unicast address to set
959  *
960  * Assumptions:
961  *   The address pointed to by addr->sa_data is a valid unicast
962  *   address and is presented in canonical (LSB) format.
963  */
964 static int skfp_ctl_set_mac_address(struct net_device *dev, void *addr)
965 {
966         struct s_smc *smc = (struct s_smc *) dev->priv;
967         struct sockaddr *p_sockaddr = (struct sockaddr *) addr;
968         skfddi_priv *bp = (skfddi_priv *) & smc->os;
969         unsigned long Flags;
970
971
972         memcpy(dev->dev_addr, p_sockaddr->sa_data, FDDI_K_ALEN);
973         spin_lock_irqsave(&bp->DriverLock, Flags);
974         ResetAdapter(smc);
975         spin_unlock_irqrestore(&bp->DriverLock, Flags);
976
977         return (0);             /* always return zero */
978 }                               // skfp_ctl_set_mac_address
979
980
981 /*
982  * ==============
983  * = skfp_ioctl =
984  * ==============
985  *   
986  * Overview:
987  *
988  * Perform IOCTL call functions here. Some are privileged operations and the
989  * effective uid is checked in those cases.
990  *  
991  * Returns:
992  *   status value
993  *   0 - success
994  *   other - failure
995  *       
996  * Arguments:
997  *   dev  - pointer to device information
998  *   rq - pointer to ioctl request structure
999  *   cmd - ?
1000  *
1001  */
1002
1003
1004 static int skfp_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1005 {
1006         skfddi_priv *lp = PRIV(dev);
1007         struct s_skfp_ioctl ioc;
1008         int status = 0;
1009
1010         if (copy_from_user(&ioc, rq->ifr_data, sizeof(struct s_skfp_ioctl)))
1011                 return -EFAULT;
1012         switch (ioc.cmd) {
1013         case SKFP_GET_STATS:    /* Get the driver statistics */
1014                 ioc.len = sizeof(lp->MacStat);
1015                 status = copy_to_user(ioc.data, skfp_ctl_get_stats(dev), ioc.len)
1016                                 ? -EFAULT : 0;
1017                 break;
1018         case SKFP_CLR_STATS:    /* Zero out the driver statistics */
1019                 if (!capable(CAP_NET_ADMIN)) {
1020                         memset(&lp->MacStat, 0, sizeof(lp->MacStat));
1021                 } else {
1022                         status = -EPERM;
1023                 }
1024                 break;
1025         default:
1026                 printk("ioctl for %s: unknow cmd: %04x\n", dev->name, ioc.cmd);
1027                 status = -EOPNOTSUPP;
1028
1029         }                       // switch
1030
1031         return status;
1032 }                               // skfp_ioctl
1033
1034
1035 /*
1036  * =====================
1037  * = skfp_send_pkt     =
1038  * =====================
1039  *   
1040  * Overview:
1041  *   Queues a packet for transmission and try to transmit it.
1042  *  
1043  * Returns:
1044  *   Condition code
1045  *       
1046  * Arguments:
1047  *   skb - pointer to sk_buff to queue for transmission
1048  *   dev - pointer to device information
1049  *
1050  * Functional Description:
1051  *   Here we assume that an incoming skb transmit request
1052  *   is contained in a single physically contiguous buffer
1053  *   in which the virtual address of the start of packet
1054  *   (skb->data) can be converted to a physical address
1055  *   by using pci_map_single().
1056  *
1057  *   We have an internal queue for packets we can not send 
1058  *   immediately. Packets in this queue can be given to the 
1059  *   adapter if transmit buffers are freed.
1060  *
1061  *   We can't free the skb until after it's been DMA'd
1062  *   out by the adapter, so we'll keep it in the driver and
1063  *   return it in mac_drv_tx_complete.
1064  *
1065  * Return Codes:
1066  *   0 - driver has queued and/or sent packet
1067  *       1 - caller should requeue the sk_buff for later transmission
1068  *
1069  * Assumptions:
1070  *   The entire packet is stored in one physically
1071  *   contiguous buffer which is not cached and whose
1072  *   32-bit physical address can be determined.
1073  *
1074  *   It's vital that this routine is NOT reentered for the
1075  *   same board and that the OS is not in another section of
1076  *   code (eg. skfp_interrupt) for the same board on a
1077  *   different thread.
1078  *
1079  * Side Effects:
1080  *   None
1081  */
1082 static int skfp_send_pkt(struct sk_buff *skb, struct net_device *dev)
1083 {
1084         skfddi_priv *bp = PRIV(dev);
1085
1086         PRINTK(KERN_INFO "skfp_send_pkt\n");
1087
1088         /*
1089          * Verify that incoming transmit request is OK
1090          *
1091          * Note: The packet size check is consistent with other
1092          *               Linux device drivers, although the correct packet
1093          *               size should be verified before calling the
1094          *               transmit routine.
1095          */
1096
1097         if (!(skb->len >= FDDI_K_LLC_ZLEN && skb->len <= FDDI_K_LLC_LEN)) {
1098                 bp->MacStat.gen.tx_errors++;    /* bump error counter */
1099                 // dequeue packets from xmt queue and send them
1100                 netif_start_queue(dev);
1101                 dev_kfree_skb(skb);
1102                 return (0);     /* return "success" */
1103         }
1104         if (bp->QueueSkb == 0) {        // return with tbusy set: queue full
1105
1106                 netif_stop_queue(dev);
1107                 return 1;
1108         }
1109         bp->QueueSkb--;
1110         skb_queue_tail(&bp->SendSkbQueue, skb);
1111         send_queued_packets((struct s_smc *) dev->priv);
1112         if (bp->QueueSkb == 0) {
1113                 netif_stop_queue(dev);
1114         }
1115         dev->trans_start = jiffies;
1116         return 0;
1117
1118 }                               // skfp_send_pkt
1119
1120
1121 /*
1122  * =======================
1123  * = send_queued_packets =
1124  * =======================
1125  *   
1126  * Overview:
1127  *   Send packets from the driver queue as long as there are some and
1128  *   transmit resources are available.
1129  *  
1130  * Returns:
1131  *   None
1132  *       
1133  * Arguments:
1134  *   smc - pointer to smc (adapter) structure
1135  *
1136  * Functional Description:
1137  *   Take a packet from queue if there is any. If not, then we are done.
1138  *   Check if there are resources to send the packet. If not, requeue it
1139  *   and exit. 
1140  *   Set packet descriptor flags and give packet to adapter.
1141  *   Check if any send resources can be freed (we do not use the
1142  *   transmit complete interrupt).
1143  */
1144 static void send_queued_packets(struct s_smc *smc)
1145 {
1146         skfddi_priv *bp = (skfddi_priv *) & smc->os;
1147         struct sk_buff *skb;
1148         unsigned char fc;
1149         int queue;
1150         struct s_smt_fp_txd *txd;       // Current TxD.
1151         dma_addr_t dma_address;
1152         unsigned long Flags;
1153
1154         int frame_status;       // HWM tx frame status.
1155
1156         PRINTK(KERN_INFO "send queued packets\n");
1157         for (;;) {
1158                 // send first buffer from queue
1159                 skb = skb_dequeue(&bp->SendSkbQueue);
1160
1161                 if (!skb) {
1162                         PRINTK(KERN_INFO "queue empty\n");
1163                         return;
1164                 }               // queue empty !
1165
1166                 spin_lock_irqsave(&bp->DriverLock, Flags);
1167                 fc = skb->data[0];
1168                 queue = (fc & FC_SYNC_BIT) ? QUEUE_S : QUEUE_A0;
1169 #ifdef ESS
1170                 // Check if the frame may/must be sent as a synchronous frame.
1171
1172                 if ((fc & ~(FC_SYNC_BIT | FC_LLC_PRIOR)) == FC_ASYNC_LLC) {
1173                         // It's an LLC frame.
1174                         if (!smc->ess.sync_bw_available)
1175                                 fc &= ~FC_SYNC_BIT; // No bandwidth available.
1176
1177                         else {  // Bandwidth is available.
1178
1179                                 if (smc->mib.fddiESSSynchTxMode) {
1180                                         // Send as sync. frame.
1181                                         fc |= FC_SYNC_BIT;
1182                                 }
1183                         }
1184                 }
1185 #endif                          // ESS
1186                 frame_status = hwm_tx_init(smc, fc, 1, skb->len, queue);
1187
1188                 if ((frame_status & (LOC_TX | LAN_TX)) == 0) {
1189                         // Unable to send the frame.
1190
1191                         if ((frame_status & RING_DOWN) != 0) {
1192                                 // Ring is down.
1193                                 PRINTK("Tx attempt while ring down.\n");
1194                         } else if ((frame_status & OUT_OF_TXD) != 0) {
1195                                 PRINTK("%s: out of TXDs.\n", bp->dev->name);
1196                         } else {
1197                                 PRINTK("%s: out of transmit resources",
1198                                         bp->dev->name);
1199                         }
1200
1201                         // Note: We will retry the operation as soon as
1202                         // transmit resources become available.
1203                         skb_queue_head(&bp->SendSkbQueue, skb);
1204                         spin_unlock_irqrestore(&bp->DriverLock, Flags);
1205                         return; // Packet has been queued.
1206
1207                 }               // if (unable to send frame)
1208
1209                 bp->QueueSkb++; // one packet less in local queue
1210
1211                 // source address in packet ?
1212                 CheckSourceAddress(skb->data, smc->hw.fddi_canon_addr.a);
1213
1214                 txd = (struct s_smt_fp_txd *) HWM_GET_CURR_TXD(smc, queue);
1215
1216                 dma_address = pci_map_single(&bp->pdev, skb->data,
1217                                              skb->len, PCI_DMA_TODEVICE);
1218                 if (frame_status & LAN_TX) {
1219                         txd->txd_os.skb = skb;                  // save skb
1220                         txd->txd_os.dma_addr = dma_address;     // save dma mapping
1221                 }
1222                 hwm_tx_frag(smc, skb->data, dma_address, skb->len,
1223                       frame_status | FIRST_FRAG | LAST_FRAG | EN_IRQ_EOF);
1224
1225                 if (!(frame_status & LAN_TX)) {         // local only frame
1226                         pci_unmap_single(&bp->pdev, dma_address,
1227                                          skb->len, PCI_DMA_TODEVICE);
1228                         dev_kfree_skb_irq(skb);
1229                 }
1230                 spin_unlock_irqrestore(&bp->DriverLock, Flags);
1231         }                       // for
1232
1233         return;                 // never reached
1234
1235 }                               // send_queued_packets
1236
1237
1238 /************************
1239  * 
1240  * CheckSourceAddress
1241  *
1242  * Verify if the source address is set. Insert it if necessary.
1243  *
1244  ************************/
1245 void CheckSourceAddress(unsigned char *frame, unsigned char *hw_addr)
1246 {
1247         unsigned char SRBit;
1248
1249         if ((((unsigned long) frame[1 + 6]) & ~0x01) != 0) // source routing bit
1250
1251                 return;
1252         if ((unsigned short) frame[1 + 10] != 0)
1253                 return;
1254         SRBit = frame[1 + 6] & 0x01;
1255         memcpy(&frame[1 + 6], hw_addr, 6);
1256         frame[8] |= SRBit;
1257 }                               // CheckSourceAddress
1258
1259
1260 /************************
1261  *
1262  *      ResetAdapter
1263  *
1264  *      Reset the adapter and bring it back to operational mode.
1265  * Args
1266  *      smc - A pointer to the SMT context struct.
1267  * Out
1268  *      Nothing.
1269  *
1270  ************************/
1271 static void ResetAdapter(struct s_smc *smc)
1272 {
1273
1274         PRINTK(KERN_INFO "[fddi: ResetAdapter]\n");
1275
1276         // Stop the adapter.
1277
1278         card_stop(smc);         // Stop all activity.
1279
1280         // Clear the transmit and receive descriptor queues.
1281         mac_drv_clear_tx_queue(smc);
1282         mac_drv_clear_rx_queue(smc);
1283
1284         // Restart the adapter.
1285
1286         smt_reset_defaults(smc, 1);     // Initialize the SMT module.
1287
1288         init_smt(smc, (smc->os.dev)->dev_addr); // Initialize the hardware.
1289
1290         smt_online(smc, 1);     // Insert into the ring again.
1291         STI_FBI();
1292
1293         // Restore original receive mode (multicasts, promiscuous, etc.).
1294         skfp_ctl_set_multicast_list_wo_lock(smc->os.dev);
1295 }                               // ResetAdapter
1296
1297
1298 //--------------- functions called by hardware module ----------------
1299
1300 /************************
1301  *
1302  *      llc_restart_tx
1303  *
1304  *      The hardware driver calls this routine when the transmit complete
1305  *      interrupt bits (end of frame) for the synchronous or asynchronous
1306  *      queue is set.
1307  *
1308  * NOTE The hardware driver calls this function also if no packets are queued.
1309  *      The routine must be able to handle this case.
1310  * Args
1311  *      smc - A pointer to the SMT context struct.
1312  * Out
1313  *      Nothing.
1314  *
1315  ************************/
1316 void llc_restart_tx(struct s_smc *smc)
1317 {
1318         skfddi_priv *bp = (skfddi_priv *) & smc->os;
1319
1320         PRINTK(KERN_INFO "[llc_restart_tx]\n");
1321
1322         // Try to send queued packets
1323         spin_unlock(&bp->DriverLock);
1324         send_queued_packets(smc);
1325         spin_lock(&bp->DriverLock);
1326         netif_start_queue(bp->dev);// system may send again if it was blocked
1327
1328 }                               // llc_restart_tx
1329
1330
1331 /************************
1332  *
1333  *      mac_drv_get_space
1334  *
1335  *      The hardware module calls this function to allocate the memory
1336  *      for the SMT MBufs if the define MB_OUTSIDE_SMC is specified.
1337  * Args
1338  *      smc - A pointer to the SMT context struct.
1339  *
1340  *      size - Size of memory in bytes to allocate.
1341  * Out
1342  *      != 0    A pointer to the virtual address of the allocated memory.
1343  *      == 0    Allocation error.
1344  *
1345  ************************/
1346 void *mac_drv_get_space(struct s_smc *smc, unsigned int size)
1347 {
1348         void *virt;
1349
1350         PRINTK(KERN_INFO "mac_drv_get_space (%d bytes), ", size);
1351         virt = (void *) (smc->os.SharedMemAddr + smc->os.SharedMemHeap);
1352
1353         if ((smc->os.SharedMemHeap + size) > smc->os.SharedMemSize) {
1354                 printk("Unexpected SMT memory size requested: %d\n", size);
1355                 return (NULL);
1356         }
1357         smc->os.SharedMemHeap += size;  // Move heap pointer.
1358
1359         PRINTK(KERN_INFO "mac_drv_get_space end\n");
1360         PRINTK(KERN_INFO "virt addr: %lx\n", (ulong) virt);
1361         PRINTK(KERN_INFO "bus  addr: %lx\n", (ulong)
1362                (smc->os.SharedMemDMA +
1363                 ((char *) virt - (char *)smc->os.SharedMemAddr)));
1364         return (virt);
1365 }                               // mac_drv_get_space
1366
1367
1368 /************************
1369  *
1370  *      mac_drv_get_desc_mem
1371  *
1372  *      This function is called by the hardware dependent module.
1373  *      It allocates the memory for the RxD and TxD descriptors.
1374  *
1375  *      This memory must be non-cached, non-movable and non-swappable.
1376  *      This memory should start at a physical page boundary.
1377  * Args
1378  *      smc - A pointer to the SMT context struct.
1379  *
1380  *      size - Size of memory in bytes to allocate.
1381  * Out
1382  *      != 0    A pointer to the virtual address of the allocated memory.
1383  *      == 0    Allocation error.
1384  *
1385  ************************/
1386 void *mac_drv_get_desc_mem(struct s_smc *smc, unsigned int size)
1387 {
1388
1389         char *virt;
1390
1391         PRINTK(KERN_INFO "mac_drv_get_desc_mem\n");
1392
1393         // Descriptor memory must be aligned on 16-byte boundary.
1394
1395         virt = mac_drv_get_space(smc, size);
1396
1397         size = (u_int) (16 - (((unsigned long) virt) & 15UL));
1398         size = size % 16;
1399
1400         PRINTK("Allocate %u bytes alignment gap ", size);
1401         PRINTK("for descriptor memory.\n");
1402
1403         if (!mac_drv_get_space(smc, size)) {
1404                 printk("fddi: Unable to align descriptor memory.\n");
1405                 return (NULL);
1406         }
1407         return (virt + size);
1408 }                               // mac_drv_get_desc_mem
1409
1410
1411 /************************
1412  *
1413  *      mac_drv_virt2phys
1414  *
1415  *      Get the physical address of a given virtual address.
1416  * Args
1417  *      smc - A pointer to the SMT context struct.
1418  *
1419  *      virt - A (virtual) pointer into our 'shared' memory area.
1420  * Out
1421  *      Physical address of the given virtual address.
1422  *
1423  ************************/
1424 unsigned long mac_drv_virt2phys(struct s_smc *smc, void *virt)
1425 {
1426         return (smc->os.SharedMemDMA +
1427                 ((char *) virt - (char *)smc->os.SharedMemAddr));
1428 }                               // mac_drv_virt2phys
1429
1430
1431 /************************
1432  *
1433  *      dma_master
1434  *
1435  *      The HWM calls this function, when the driver leads through a DMA
1436  *      transfer. If the OS-specific module must prepare the system hardware
1437  *      for the DMA transfer, it should do it in this function.
1438  *
1439  *      The hardware module calls this dma_master if it wants to send an SMT
1440  *      frame.  This means that the virt address passed in here is part of
1441  *      the 'shared' memory area.
1442  * Args
1443  *      smc - A pointer to the SMT context struct.
1444  *
1445  *      virt - The virtual address of the data.
1446  *
1447  *      len - The length in bytes of the data.
1448  *
1449  *      flag - Indicates the transmit direction and the buffer type:
1450  *              DMA_RD  (0x01)  system RAM ==> adapter buffer memory
1451  *              DMA_WR  (0x02)  adapter buffer memory ==> system RAM
1452  *              SMT_BUF (0x80)  SMT buffer
1453  *
1454  *      >> NOTE: SMT_BUF and DMA_RD are always set for PCI. <<
1455  * Out
1456  *      Returns the pyhsical address for the DMA transfer.
1457  *
1458  ************************/
1459 u_long dma_master(struct s_smc * smc, void *virt, int len, int flag)
1460 {
1461         return (smc->os.SharedMemDMA +
1462                 ((char *) virt - (char *)smc->os.SharedMemAddr));
1463 }                               // dma_master
1464
1465
1466 /************************
1467  *
1468  *      dma_complete
1469  *
1470  *      The hardware module calls this routine when it has completed a DMA
1471  *      transfer. If the operating system dependent module has set up the DMA
1472  *      channel via dma_master() (e.g. Windows NT or AIX) it should clean up
1473  *      the DMA channel.
1474  * Args
1475  *      smc - A pointer to the SMT context struct.
1476  *
1477  *      descr - A pointer to a TxD or RxD, respectively.
1478  *
1479  *      flag - Indicates the DMA transfer direction / SMT buffer:
1480  *              DMA_RD  (0x01)  system RAM ==> adapter buffer memory
1481  *              DMA_WR  (0x02)  adapter buffer memory ==> system RAM
1482  *              SMT_BUF (0x80)  SMT buffer (managed by HWM)
1483  * Out
1484  *      Nothing.
1485  *
1486  ************************/
1487 void dma_complete(struct s_smc *smc, volatile union s_fp_descr *descr, int flag)
1488 {
1489         /* For TX buffers, there are two cases.  If it is an SMT transmit
1490          * buffer, there is nothing to do since we use consistent memory
1491          * for the 'shared' memory area.  The other case is for normal
1492          * transmit packets given to us by the networking stack, and in
1493          * that case we cleanup the PCI DMA mapping in mac_drv_tx_complete
1494          * below.
1495          *
1496          * For RX buffers, we have to unmap dynamic PCI DMA mappings here
1497          * because the hardware module is about to potentially look at
1498          * the contents of the buffer.  If we did not call the PCI DMA
1499          * unmap first, the hardware module could read inconsistent data.
1500          */
1501         if (flag & DMA_WR) {
1502                 skfddi_priv *bp = (skfddi_priv *) & smc->os;
1503                 volatile struct s_smt_fp_rxd *r = &descr->r;
1504
1505                 /* If SKB is NULL, we used the local buffer. */
1506                 if (r->rxd_os.skb && r->rxd_os.dma_addr) {
1507                         int MaxFrameSize = bp->MaxFrameSize;
1508
1509                         pci_unmap_single(&bp->pdev, r->rxd_os.dma_addr,
1510                                          MaxFrameSize, PCI_DMA_FROMDEVICE);
1511                         r->rxd_os.dma_addr = 0;
1512                 }
1513         }
1514 }                               // dma_complete
1515
1516
1517 /************************
1518  *
1519  *      mac_drv_tx_complete
1520  *
1521  *      Transmit of a packet is complete. Release the tx staging buffer.
1522  *
1523  * Args
1524  *      smc - A pointer to the SMT context struct.
1525  *
1526  *      txd - A pointer to the last TxD which is used by the frame.
1527  * Out
1528  *      Returns nothing.
1529  *
1530  ************************/
1531 void mac_drv_tx_complete(struct s_smc *smc, volatile struct s_smt_fp_txd *txd)
1532 {
1533         struct sk_buff *skb;
1534
1535         PRINTK(KERN_INFO "entering mac_drv_tx_complete\n");
1536         // Check if this TxD points to a skb
1537
1538         if (!(skb = txd->txd_os.skb)) {
1539                 PRINTK("TXD with no skb assigned.\n");
1540                 return;
1541         }
1542         txd->txd_os.skb = NULL;
1543
1544         // release the DMA mapping
1545         pci_unmap_single(&smc->os.pdev, txd->txd_os.dma_addr,
1546                          skb->len, PCI_DMA_TODEVICE);
1547         txd->txd_os.dma_addr = 0;
1548
1549         smc->os.MacStat.gen.tx_packets++;       // Count transmitted packets.
1550         smc->os.MacStat.gen.tx_bytes+=skb->len; // Count bytes
1551
1552         // free the skb
1553         dev_kfree_skb_irq(skb);
1554
1555         PRINTK(KERN_INFO "leaving mac_drv_tx_complete\n");
1556 }                               // mac_drv_tx_complete
1557
1558
1559 /************************
1560  *
1561  * dump packets to logfile
1562  *
1563  ************************/
1564 #ifdef DUMPPACKETS
1565 void dump_data(unsigned char *Data, int length)
1566 {
1567         int i, j;
1568         unsigned char s[255], sh[10];
1569         if (length > 64) {
1570                 length = 64;
1571         }
1572         printk(KERN_INFO "---Packet start---\n");
1573         for (i = 0, j = 0; i < length / 8; i++, j += 8)
1574                 printk(KERN_INFO "%02x %02x %02x %02x %02x %02x %02x %02x\n",
1575                        Data[j + 0], Data[j + 1], Data[j + 2], Data[j + 3],
1576                        Data[j + 4], Data[j + 5], Data[j + 6], Data[j + 7]);
1577         strcpy(s, "");
1578         for (i = 0; i < length % 8; i++) {
1579                 sprintf(sh, "%02x ", Data[j + i]);
1580                 strcat(s, sh);
1581         }
1582         printk(KERN_INFO "%s\n", s);
1583         printk(KERN_INFO "------------------\n");
1584 }                               // dump_data
1585 #else
1586 #define dump_data(data,len)
1587 #endif                          // DUMPPACKETS
1588
1589 /************************
1590  *
1591  *      mac_drv_rx_complete
1592  *
1593  *      The hardware module calls this function if an LLC frame is received
1594  *      in a receive buffer. Also the SMT, NSA, and directed beacon frames
1595  *      from the network will be passed to the LLC layer by this function
1596  *      if passing is enabled.
1597  *
1598  *      mac_drv_rx_complete forwards the frame to the LLC layer if it should
1599  *      be received. It also fills the RxD ring with new receive buffers if
1600  *      some can be queued.
1601  * Args
1602  *      smc - A pointer to the SMT context struct.
1603  *
1604  *      rxd - A pointer to the first RxD which is used by the receive frame.
1605  *
1606  *      frag_count - Count of RxDs used by the received frame.
1607  *
1608  *      len - Frame length.
1609  * Out
1610  *      Nothing.
1611  *
1612  ************************/
1613 void mac_drv_rx_complete(struct s_smc *smc, volatile struct s_smt_fp_rxd *rxd,
1614                          int frag_count, int len)
1615 {
1616         skfddi_priv *bp = (skfddi_priv *) & smc->os;
1617         struct sk_buff *skb;
1618         unsigned char *virt, *cp;
1619         unsigned short ri;
1620         u_int RifLength;
1621
1622         PRINTK(KERN_INFO "entering mac_drv_rx_complete (len=%d)\n", len);
1623         if (frag_count != 1) {  // This is not allowed to happen.
1624
1625                 printk("fddi: Multi-fragment receive!\n");
1626                 goto RequeueRxd;        // Re-use the given RXD(s).
1627
1628         }
1629         skb = rxd->rxd_os.skb;
1630         if (!skb) {
1631                 PRINTK(KERN_INFO "No skb in rxd\n");
1632                 smc->os.MacStat.gen.rx_errors++;
1633                 goto RequeueRxd;
1634         }
1635         virt = skb->data;
1636
1637         // The DMA mapping was released in dma_complete above.
1638
1639         dump_data(skb->data, len);
1640
1641         /*
1642          * FDDI Frame format:
1643          * +-------+-------+-------+------------+--------+------------+
1644          * | FC[1] | DA[6] | SA[6] | RIF[0..18] | LLC[3] | Data[0..n] |
1645          * +-------+-------+-------+------------+--------+------------+
1646          *
1647          * FC = Frame Control
1648          * DA = Destination Address
1649          * SA = Source Address
1650          * RIF = Routing Information Field
1651          * LLC = Logical Link Control
1652          */
1653
1654         // Remove Routing Information Field (RIF), if present.
1655
1656         if ((virt[1 + 6] & FDDI_RII) == 0)
1657                 RifLength = 0;
1658         else {
1659                 int n;
1660 // goos: RIF removal has still to be tested
1661                 PRINTK(KERN_INFO "RIF found\n");
1662                 // Get RIF length from Routing Control (RC) field.
1663                 cp = virt + FDDI_MAC_HDR_LEN;   // Point behind MAC header.
1664
1665                 ri = ntohs(*((unsigned short *) cp));
1666                 RifLength = ri & FDDI_RCF_LEN_MASK;
1667                 if (len < (int) (FDDI_MAC_HDR_LEN + RifLength)) {
1668                         printk("fddi: Invalid RIF.\n");
1669                         goto RequeueRxd;        // Discard the frame.
1670
1671                 }
1672                 virt[1 + 6] &= ~FDDI_RII;       // Clear RII bit.
1673                 // regions overlap
1674
1675                 virt = cp + RifLength;
1676                 for (n = FDDI_MAC_HDR_LEN; n; n--)
1677                         *--virt = *--cp;
1678                 // adjust sbd->data pointer
1679                 skb_pull(skb, RifLength);
1680                 len -= RifLength;
1681                 RifLength = 0;
1682         }
1683
1684         // Count statistics.
1685         smc->os.MacStat.gen.rx_packets++;       // Count indicated receive
1686                                                 // packets.
1687         smc->os.MacStat.gen.rx_bytes+=len;      // Count bytes.
1688
1689         // virt points to header again
1690         if (virt[1] & 0x01) {   // Check group (multicast) bit.
1691
1692                 smc->os.MacStat.gen.multicast++;
1693         }
1694
1695         // deliver frame to system
1696         rxd->rxd_os.skb = NULL;
1697         skb_trim(skb, len);
1698         skb->protocol = fddi_type_trans(skb, bp->dev);
1699         skb->dev = bp->dev;     /* pass up device pointer */
1700
1701         netif_rx(skb);
1702         bp->dev->last_rx = jiffies;
1703
1704         HWM_RX_CHECK(smc, RX_LOW_WATERMARK);
1705         return;
1706
1707       RequeueRxd:
1708         PRINTK(KERN_INFO "Rx: re-queue RXD.\n");
1709         mac_drv_requeue_rxd(smc, rxd, frag_count);
1710         smc->os.MacStat.gen.rx_errors++;        // Count receive packets
1711                                                 // not indicated.
1712
1713 }                               // mac_drv_rx_complete
1714
1715
1716 /************************
1717  *
1718  *      mac_drv_requeue_rxd
1719  *
1720  *      The hardware module calls this function to request the OS-specific
1721  *      module to queue the receive buffer(s) represented by the pointer
1722  *      to the RxD and the frag_count into the receive queue again. This
1723  *      buffer was filled with an invalid frame or an SMT frame.
1724  * Args
1725  *      smc - A pointer to the SMT context struct.
1726  *
1727  *      rxd - A pointer to the first RxD which is used by the receive frame.
1728  *
1729  *      frag_count - Count of RxDs used by the received frame.
1730  * Out
1731  *      Nothing.
1732  *
1733  ************************/
1734 void mac_drv_requeue_rxd(struct s_smc *smc, volatile struct s_smt_fp_rxd *rxd,
1735                          int frag_count)
1736 {
1737         volatile struct s_smt_fp_rxd *next_rxd;
1738         volatile struct s_smt_fp_rxd *src_rxd;
1739         struct sk_buff *skb;
1740         int MaxFrameSize;
1741         unsigned char *v_addr;
1742         dma_addr_t b_addr;
1743
1744         if (frag_count != 1)    // This is not allowed to happen.
1745
1746                 printk("fddi: Multi-fragment requeue!\n");
1747
1748         MaxFrameSize = ((skfddi_priv *) & smc->os)->MaxFrameSize;
1749         src_rxd = rxd;
1750         for (; frag_count > 0; frag_count--) {
1751                 next_rxd = src_rxd->rxd_next;
1752                 rxd = HWM_GET_CURR_RXD(smc);
1753
1754                 skb = src_rxd->rxd_os.skb;
1755                 if (skb == NULL) {      // this should not happen
1756
1757                         PRINTK("Requeue with no skb in rxd!\n");
1758                         skb = alloc_skb(MaxFrameSize + 3, GFP_ATOMIC);
1759                         if (skb) {
1760                                 // we got a skb
1761                                 rxd->rxd_os.skb = skb;
1762                                 skb_reserve(skb, 3);
1763                                 skb_put(skb, MaxFrameSize);
1764                                 v_addr = skb->data;
1765                                 b_addr = pci_map_single(&smc->os.pdev,
1766                                                         v_addr,
1767                                                         MaxFrameSize,
1768                                                         PCI_DMA_FROMDEVICE);
1769                                 rxd->rxd_os.dma_addr = b_addr;
1770                         } else {
1771                                 // no skb available, use local buffer
1772                                 PRINTK("Queueing invalid buffer!\n");
1773                                 rxd->rxd_os.skb = NULL;
1774                                 v_addr = smc->os.LocalRxBuffer;
1775                                 b_addr = smc->os.LocalRxBufferDMA;
1776                         }
1777                 } else {
1778                         // we use skb from old rxd
1779                         rxd->rxd_os.skb = skb;
1780                         v_addr = skb->data;
1781                         b_addr = pci_map_single(&smc->os.pdev,
1782                                                 v_addr,
1783                                                 MaxFrameSize,
1784                                                 PCI_DMA_FROMDEVICE);
1785                         rxd->rxd_os.dma_addr = b_addr;
1786                 }
1787                 hwm_rx_frag(smc, v_addr, b_addr, MaxFrameSize,
1788                             FIRST_FRAG | LAST_FRAG);
1789
1790                 src_rxd = next_rxd;
1791         }
1792 }                               // mac_drv_requeue_rxd
1793
1794
1795 /************************
1796  *
1797  *      mac_drv_fill_rxd
1798  *
1799  *      The hardware module calls this function at initialization time
1800  *      to fill the RxD ring with receive buffers. It is also called by
1801  *      mac_drv_rx_complete if rx_free is large enough to queue some new
1802  *      receive buffers into the RxD ring. mac_drv_fill_rxd queues new
1803  *      receive buffers as long as enough RxDs and receive buffers are
1804  *      available.
1805  * Args
1806  *      smc - A pointer to the SMT context struct.
1807  * Out
1808  *      Nothing.
1809  *
1810  ************************/
1811 void mac_drv_fill_rxd(struct s_smc *smc)
1812 {
1813         int MaxFrameSize;
1814         unsigned char *v_addr;
1815         unsigned long b_addr;
1816         struct sk_buff *skb;
1817         volatile struct s_smt_fp_rxd *rxd;
1818
1819         PRINTK(KERN_INFO "entering mac_drv_fill_rxd\n");
1820
1821         // Walk through the list of free receive buffers, passing receive
1822         // buffers to the HWM as long as RXDs are available.
1823
1824         MaxFrameSize = ((skfddi_priv *) & smc->os)->MaxFrameSize;
1825         // Check if there is any RXD left.
1826         while (HWM_GET_RX_FREE(smc) > 0) {
1827                 PRINTK(KERN_INFO ".\n");
1828
1829                 rxd = HWM_GET_CURR_RXD(smc);
1830                 skb = alloc_skb(MaxFrameSize + 3, GFP_ATOMIC);
1831                 if (skb) {
1832                         // we got a skb
1833                         skb_reserve(skb, 3);
1834                         skb_put(skb, MaxFrameSize);
1835                         v_addr = skb->data;
1836                         b_addr = pci_map_single(&smc->os.pdev,
1837                                                 v_addr,
1838                                                 MaxFrameSize,
1839                                                 PCI_DMA_FROMDEVICE);
1840                         rxd->rxd_os.dma_addr = b_addr;
1841                 } else {
1842                         // no skb available, use local buffer
1843                         // System has run out of buffer memory, but we want to
1844                         // keep the receiver running in hope of better times.
1845                         // Multiple descriptors may point to this local buffer,
1846                         // so data in it must be considered invalid.
1847                         PRINTK("Queueing invalid buffer!\n");
1848                         v_addr = smc->os.LocalRxBuffer;
1849                         b_addr = smc->os.LocalRxBufferDMA;
1850                 }
1851
1852                 rxd->rxd_os.skb = skb;
1853
1854                 // Pass receive buffer to HWM.
1855                 hwm_rx_frag(smc, v_addr, b_addr, MaxFrameSize,
1856                             FIRST_FRAG | LAST_FRAG);
1857         }
1858         PRINTK(KERN_INFO "leaving mac_drv_fill_rxd\n");
1859 }                               // mac_drv_fill_rxd
1860
1861
1862 /************************
1863  *
1864  *      mac_drv_clear_rxd
1865  *
1866  *      The hardware module calls this function to release unused
1867  *      receive buffers.
1868  * Args
1869  *      smc - A pointer to the SMT context struct.
1870  *
1871  *      rxd - A pointer to the first RxD which is used by the receive buffer.
1872  *
1873  *      frag_count - Count of RxDs used by the receive buffer.
1874  * Out
1875  *      Nothing.
1876  *
1877  ************************/
1878 void mac_drv_clear_rxd(struct s_smc *smc, volatile struct s_smt_fp_rxd *rxd,
1879                        int frag_count)
1880 {
1881
1882         struct sk_buff *skb;
1883
1884         PRINTK("entering mac_drv_clear_rxd\n");
1885
1886         if (frag_count != 1)    // This is not allowed to happen.
1887
1888                 printk("fddi: Multi-fragment clear!\n");
1889
1890         for (; frag_count > 0; frag_count--) {
1891                 skb = rxd->rxd_os.skb;
1892                 if (skb != NULL) {
1893                         skfddi_priv *bp = (skfddi_priv *) & smc->os;
1894                         int MaxFrameSize = bp->MaxFrameSize;
1895
1896                         pci_unmap_single(&bp->pdev, rxd->rxd_os.dma_addr,
1897                                          MaxFrameSize, PCI_DMA_FROMDEVICE);
1898
1899                         dev_kfree_skb(skb);
1900                         rxd->rxd_os.skb = NULL;
1901                 }
1902                 rxd = rxd->rxd_next;    // Next RXD.
1903
1904         }
1905 }                               // mac_drv_clear_rxd
1906
1907
1908 /************************
1909  *
1910  *      mac_drv_rx_init
1911  *
1912  *      The hardware module calls this routine when an SMT or NSA frame of the
1913  *      local SMT should be delivered to the LLC layer.
1914  *
1915  *      It is necessary to have this function, because there is no other way to
1916  *      copy the contents of SMT MBufs into receive buffers.
1917  *
1918  *      mac_drv_rx_init allocates the required target memory for this frame,
1919  *      and receives the frame fragment by fragment by calling mac_drv_rx_frag.
1920  * Args
1921  *      smc - A pointer to the SMT context struct.
1922  *
1923  *      len - The length (in bytes) of the received frame (FC, DA, SA, Data).
1924  *
1925  *      fc - The Frame Control field of the received frame.
1926  *
1927  *      look_ahead - A pointer to the lookahead data buffer (may be NULL).
1928  *
1929  *      la_len - The length of the lookahead data stored in the lookahead
1930  *      buffer (may be zero).
1931  * Out
1932  *      Always returns zero (0).
1933  *
1934  ************************/
1935 int mac_drv_rx_init(struct s_smc *smc, int len, int fc,
1936                     char *look_ahead, int la_len)
1937 {
1938         struct sk_buff *skb;
1939
1940         PRINTK("entering mac_drv_rx_init(len=%d)\n", len);
1941
1942         // "Received" a SMT or NSA frame of the local SMT.
1943
1944         if (len != la_len || len < FDDI_MAC_HDR_LEN || !look_ahead) {
1945                 PRINTK("fddi: Discard invalid local SMT frame\n");
1946                 PRINTK("  len=%d, la_len=%d, (ULONG) look_ahead=%08lXh.\n",
1947                        len, la_len, (unsigned long) look_ahead);
1948                 return (0);
1949         }
1950         skb = alloc_skb(len + 3, GFP_ATOMIC);
1951         if (!skb) {
1952                 PRINTK("fddi: Local SMT: skb memory exhausted.\n");
1953                 return (0);
1954         }
1955         skb_reserve(skb, 3);
1956         skb_put(skb, len);
1957         memcpy(skb->data, look_ahead, len);
1958
1959         // deliver frame to system
1960         skb->protocol = fddi_type_trans(skb, ((skfddi_priv *) & smc->os)->dev);
1961         skb->dev->last_rx = jiffies;
1962         netif_rx(skb);
1963
1964         return (0);
1965 }                               // mac_drv_rx_init
1966
1967
1968 /************************
1969  *
1970  *      smt_timer_poll
1971  *
1972  *      This routine is called periodically by the SMT module to clean up the
1973  *      driver.
1974  *
1975  *      Return any queued frames back to the upper protocol layers if the ring
1976  *      is down.
1977  * Args
1978  *      smc - A pointer to the SMT context struct.
1979  * Out
1980  *      Nothing.
1981  *
1982  ************************/
1983 void smt_timer_poll(struct s_smc *smc)
1984 {
1985 }                               // smt_timer_poll
1986
1987
1988 /************************
1989  *
1990  *      ring_status_indication
1991  *
1992  *      This function indicates a change of the ring state.
1993  * Args
1994  *      smc - A pointer to the SMT context struct.
1995  *
1996  *      status - The current ring status.
1997  * Out
1998  *      Nothing.
1999  *
2000  ************************/
2001 void ring_status_indication(struct s_smc *smc, u_long status)
2002 {
2003         PRINTK("ring_status_indication( ");
2004         if (status & RS_RES15)
2005                 PRINTK("RS_RES15 ");
2006         if (status & RS_HARDERROR)
2007                 PRINTK("RS_HARDERROR ");
2008         if (status & RS_SOFTERROR)
2009                 PRINTK("RS_SOFTERROR ");
2010         if (status & RS_BEACON)
2011                 PRINTK("RS_BEACON ");
2012         if (status & RS_PATHTEST)
2013                 PRINTK("RS_PATHTEST ");
2014         if (status & RS_SELFTEST)
2015                 PRINTK("RS_SELFTEST ");
2016         if (status & RS_RES9)
2017                 PRINTK("RS_RES9 ");
2018         if (status & RS_DISCONNECT)
2019                 PRINTK("RS_DISCONNECT ");
2020         if (status & RS_RES7)
2021                 PRINTK("RS_RES7 ");
2022         if (status & RS_DUPADDR)
2023                 PRINTK("RS_DUPADDR ");
2024         if (status & RS_NORINGOP)
2025                 PRINTK("RS_NORINGOP ");
2026         if (status & RS_VERSION)
2027                 PRINTK("RS_VERSION ");
2028         if (status & RS_STUCKBYPASSS)
2029                 PRINTK("RS_STUCKBYPASSS ");
2030         if (status & RS_EVENT)
2031                 PRINTK("RS_EVENT ");
2032         if (status & RS_RINGOPCHANGE)
2033                 PRINTK("RS_RINGOPCHANGE ");
2034         if (status & RS_RES0)
2035                 PRINTK("RS_RES0 ");
2036         PRINTK("]\n");
2037 }                               // ring_status_indication
2038
2039
2040 /************************
2041  *
2042  *      smt_get_time
2043  *
2044  *      Gets the current time from the system.
2045  * Args
2046  *      None.
2047  * Out
2048  *      The current time in TICKS_PER_SECOND.
2049  *
2050  *      TICKS_PER_SECOND has the unit 'count of timer ticks per second'. It is
2051  *      defined in "targetos.h". The definition of TICKS_PER_SECOND must comply
2052  *      to the time returned by smt_get_time().
2053  *
2054  ************************/
2055 unsigned long smt_get_time(void)
2056 {
2057         return jiffies;
2058 }                               // smt_get_time
2059
2060
2061 /************************
2062  *
2063  *      smt_stat_counter
2064  *
2065  *      Status counter update (ring_op, fifo full).
2066  * Args
2067  *      smc - A pointer to the SMT context struct.
2068  *
2069  *      stat -  = 0: A ring operational change occurred.
2070  *              = 1: The FORMAC FIFO buffer is full / FIFO overflow.
2071  * Out
2072  *      Nothing.
2073  *
2074  ************************/
2075 void smt_stat_counter(struct s_smc *smc, int stat)
2076 {
2077 //      BOOLEAN RingIsUp ;
2078
2079         PRINTK(KERN_INFO "smt_stat_counter\n");
2080         switch (stat) {
2081         case 0:
2082                 PRINTK(KERN_INFO "Ring operational change.\n");
2083                 break;
2084         case 1:
2085                 PRINTK(KERN_INFO "Receive fifo overflow.\n");
2086                 smc->os.MacStat.gen.rx_errors++;
2087                 break;
2088         default:
2089                 PRINTK(KERN_INFO "Unknown status (%d).\n", stat);
2090                 break;
2091         }
2092 }                               // smt_stat_counter
2093
2094
2095 /************************
2096  *
2097  *      cfm_state_change
2098  *
2099  *      Sets CFM state in custom statistics.
2100  * Args
2101  *      smc - A pointer to the SMT context struct.
2102  *
2103  *      c_state - Possible values are:
2104  *
2105  *              EC0_OUT, EC1_IN, EC2_TRACE, EC3_LEAVE, EC4_PATH_TEST,
2106  *              EC5_INSERT, EC6_CHECK, EC7_DEINSERT
2107  * Out
2108  *      Nothing.
2109  *
2110  ************************/
2111 void cfm_state_change(struct s_smc *smc, int c_state)
2112 {
2113 #ifdef DRIVERDEBUG
2114         char *s;
2115
2116         switch (c_state) {
2117         case SC0_ISOLATED:
2118                 s = "SC0_ISOLATED";
2119                 break;
2120         case SC1_WRAP_A:
2121                 s = "SC1_WRAP_A";
2122                 break;
2123         case SC2_WRAP_B:
2124                 s = "SC2_WRAP_B";
2125                 break;
2126         case SC4_THRU_A:
2127                 s = "SC4_THRU_A";
2128                 break;
2129         case SC5_THRU_B:
2130                 s = "SC5_THRU_B";
2131                 break;
2132         case SC7_WRAP_S:
2133                 s = "SC7_WRAP_S";
2134                 break;
2135         case SC9_C_WRAP_A:
2136                 s = "SC9_C_WRAP_A";
2137                 break;
2138         case SC10_C_WRAP_B:
2139                 s = "SC10_C_WRAP_B";
2140                 break;
2141         case SC11_C_WRAP_S:
2142                 s = "SC11_C_WRAP_S";
2143                 break;
2144         default:
2145                 PRINTK(KERN_INFO "cfm_state_change: unknown %d\n", c_state);
2146                 return;
2147         }
2148         PRINTK(KERN_INFO "cfm_state_change: %s\n", s);
2149 #endif                          // DRIVERDEBUG
2150 }                               // cfm_state_change
2151
2152
2153 /************************
2154  *
2155  *      ecm_state_change
2156  *
2157  *      Sets ECM state in custom statistics.
2158  * Args
2159  *      smc - A pointer to the SMT context struct.
2160  *
2161  *      e_state - Possible values are:
2162  *
2163  *              SC0_ISOLATED, SC1_WRAP_A (5), SC2_WRAP_B (6), SC4_THRU_A (12),
2164  *              SC5_THRU_B (7), SC7_WRAP_S (8)
2165  * Out
2166  *      Nothing.
2167  *
2168  ************************/
2169 void ecm_state_change(struct s_smc *smc, int e_state)
2170 {
2171 #ifdef DRIVERDEBUG
2172         char *s;
2173
2174         switch (e_state) {
2175         case EC0_OUT:
2176                 s = "EC0_OUT";
2177                 break;
2178         case EC1_IN:
2179                 s = "EC1_IN";
2180                 break;
2181         case EC2_TRACE:
2182                 s = "EC2_TRACE";
2183                 break;
2184         case EC3_LEAVE:
2185                 s = "EC3_LEAVE";
2186                 break;
2187         case EC4_PATH_TEST:
2188                 s = "EC4_PATH_TEST";
2189                 break;
2190         case EC5_INSERT:
2191                 s = "EC5_INSERT";
2192                 break;
2193         case EC6_CHECK:
2194                 s = "EC6_CHECK";
2195                 break;
2196         case EC7_DEINSERT:
2197                 s = "EC7_DEINSERT";
2198                 break;
2199         default:
2200                 s = "unknown";
2201                 break;
2202         }
2203         PRINTK(KERN_INFO "ecm_state_change: %s\n", s);
2204 #endif                          //DRIVERDEBUG
2205 }                               // ecm_state_change
2206
2207
2208 /************************
2209  *
2210  *      rmt_state_change
2211  *
2212  *      Sets RMT state in custom statistics.
2213  * Args
2214  *      smc - A pointer to the SMT context struct.
2215  *
2216  *      r_state - Possible values are:
2217  *
2218  *              RM0_ISOLATED, RM1_NON_OP, RM2_RING_OP, RM3_DETECT,
2219  *              RM4_NON_OP_DUP, RM5_RING_OP_DUP, RM6_DIRECTED, RM7_TRACE
2220  * Out
2221  *      Nothing.
2222  *
2223  ************************/
2224 void rmt_state_change(struct s_smc *smc, int r_state)
2225 {
2226 #ifdef DRIVERDEBUG
2227         char *s;
2228
2229         switch (r_state) {
2230         case RM0_ISOLATED:
2231                 s = "RM0_ISOLATED";
2232                 break;
2233         case RM1_NON_OP:
2234                 s = "RM1_NON_OP - not operational";
2235                 break;
2236         case RM2_RING_OP:
2237                 s = "RM2_RING_OP - ring operational";
2238                 break;
2239         case RM3_DETECT:
2240                 s = "RM3_DETECT - detect dupl addresses";
2241                 break;
2242         case RM4_NON_OP_DUP:
2243                 s = "RM4_NON_OP_DUP - dupl. addr detected";
2244                 break;
2245         case RM5_RING_OP_DUP:
2246                 s = "RM5_RING_OP_DUP - ring oper. with dupl. addr";
2247                 break;
2248         case RM6_DIRECTED:
2249                 s = "RM6_DIRECTED - sending directed beacons";
2250                 break;
2251         case RM7_TRACE:
2252                 s = "RM7_TRACE - trace initiated";
2253                 break;
2254         default:
2255                 s = "unknown";
2256                 break;
2257         }
2258         PRINTK(KERN_INFO "[rmt_state_change: %s]\n", s);
2259 #endif                          // DRIVERDEBUG
2260 }                               // rmt_state_change
2261
2262
2263 /************************
2264  *
2265  *      drv_reset_indication
2266  *
2267  *      This function is called by the SMT when it has detected a severe
2268  *      hardware problem. The driver should perform a reset on the adapter
2269  *      as soon as possible, but not from within this function.
2270  * Args
2271  *      smc - A pointer to the SMT context struct.
2272  * Out
2273  *      Nothing.
2274  *
2275  ************************/
2276 void drv_reset_indication(struct s_smc *smc)
2277 {
2278         PRINTK(KERN_INFO "entering drv_reset_indication\n");
2279
2280         smc->os.ResetRequested = TRUE;  // Set flag.
2281
2282 }                               // drv_reset_indication
2283
2284 static struct pci_driver skfddi_pci_driver = {
2285         .name           = "skfddi",
2286         .id_table       = skfddi_pci_tbl,
2287         .probe          = skfp_init_one,
2288         .remove         = __devexit_p(skfp_remove_one),
2289 };
2290
2291 static int __init skfd_init(void)
2292 {
2293         return pci_module_init(&skfddi_pci_driver);
2294 }
2295
2296 static void __exit skfd_exit(void)
2297 {
2298         pci_unregister_driver(&skfddi_pci_driver);
2299 }
2300
2301 module_init(skfd_init);
2302 module_exit(skfd_exit);