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