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