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