[NET]: Make NAPI polling independent of struct net_device objects.
[linux-2.6.git] / drivers / net / fs_enet / fs_enet-main.c
1 /*
2  * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
3  *
4  * Copyright (c) 2003 Intracom S.A. 
5  *  by Pantelis Antoniou <panto@intracom.gr>
6  * 
7  * 2005 (c) MontaVista Software, Inc. 
8  * Vitaly Bordug <vbordug@ru.mvista.com>
9  *
10  * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
11  * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
12  *
13  * This file is licensed under the terms of the GNU General Public License 
14  * version 2. This program is licensed "as is" without any warranty of any 
15  * kind, whether express or implied.
16  */
17
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/ptrace.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/spinlock.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/bitops.h>
36 #include <linux/fs.h>
37 #include <linux/platform_device.h>
38 #include <linux/phy.h>
39
40 #include <linux/vmalloc.h>
41 #include <asm/pgtable.h>
42
43 #include <asm/pgtable.h>
44 #include <asm/irq.h>
45 #include <asm/uaccess.h>
46
47 #include "fs_enet.h"
48
49 /*************************************************/
50
51 static char version[] __devinitdata =
52     DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
53
54 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
55 MODULE_DESCRIPTION("Freescale Ethernet Driver");
56 MODULE_LICENSE("GPL");
57 MODULE_VERSION(DRV_MODULE_VERSION);
58
59 int fs_enet_debug = -1;         /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
60 module_param(fs_enet_debug, int, 0);
61 MODULE_PARM_DESC(fs_enet_debug,
62                  "Freescale bitmapped debugging message enable value");
63
64
65 static void fs_set_multicast_list(struct net_device *dev)
66 {
67         struct fs_enet_private *fep = netdev_priv(dev);
68
69         (*fep->ops->set_multicast_list)(dev);
70 }
71
72 /* NAPI receive function */
73 static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
74 {
75         struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
76         struct net_device *dev = to_net_dev(fep->dev);
77         const struct fs_platform_info *fpi = fep->fpi;
78         cbd_t *bdp;
79         struct sk_buff *skb, *skbn, *skbt;
80         int received = 0;
81         u16 pkt_len, sc;
82         int curidx;
83
84         if (!netif_running(dev))
85                 return 0;
86
87         /*
88          * First, grab all of the stats for the incoming packet.
89          * These get messed up if we get called due to a busy condition.
90          */
91         bdp = fep->cur_rx;
92
93         /* clear RX status bits for napi*/
94         (*fep->ops->napi_clear_rx_event)(dev);
95
96         while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
97                 curidx = bdp - fep->rx_bd_base;
98
99                 /*
100                  * Since we have allocated space to hold a complete frame,
101                  * the last indicator should be set.
102                  */
103                 if ((sc & BD_ENET_RX_LAST) == 0)
104                         printk(KERN_WARNING DRV_MODULE_NAME
105                                ": %s rcv is not +last\n",
106                                dev->name);
107
108                 /*
109                  * Check for errors. 
110                  */
111                 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
112                           BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
113                         fep->stats.rx_errors++;
114                         /* Frame too long or too short. */
115                         if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
116                                 fep->stats.rx_length_errors++;
117                         /* Frame alignment */
118                         if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
119                                 fep->stats.rx_frame_errors++;
120                         /* CRC Error */
121                         if (sc & BD_ENET_RX_CR)
122                                 fep->stats.rx_crc_errors++;
123                         /* FIFO overrun */
124                         if (sc & BD_ENET_RX_OV)
125                                 fep->stats.rx_crc_errors++;
126
127                         skb = fep->rx_skbuff[curidx];
128
129                         dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
130                                 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
131                                 DMA_FROM_DEVICE);
132
133                         skbn = skb;
134
135                 } else {
136                         skb = fep->rx_skbuff[curidx];
137
138                         dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
139                                 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
140                                 DMA_FROM_DEVICE);
141
142                         /*
143                          * Process the incoming frame.
144                          */
145                         fep->stats.rx_packets++;
146                         pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
147                         fep->stats.rx_bytes += pkt_len + 4;
148
149                         if (pkt_len <= fpi->rx_copybreak) {
150                                 /* +2 to make IP header L1 cache aligned */
151                                 skbn = dev_alloc_skb(pkt_len + 2);
152                                 if (skbn != NULL) {
153                                         skb_reserve(skbn, 2);   /* align IP header */
154                                         skb_copy_from_linear_data(skb,
155                                                       skbn->data, pkt_len);
156                                         /* swap */
157                                         skbt = skb;
158                                         skb = skbn;
159                                         skbn = skbt;
160                                 }
161                         } else
162                                 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
163
164                         if (skbn != NULL) {
165                                 skb_put(skb, pkt_len);  /* Make room */
166                                 skb->protocol = eth_type_trans(skb, dev);
167                                 received++;
168                                 netif_receive_skb(skb);
169                         } else {
170                                 printk(KERN_WARNING DRV_MODULE_NAME
171                                        ": %s Memory squeeze, dropping packet.\n",
172                                        dev->name);
173                                 fep->stats.rx_dropped++;
174                                 skbn = skb;
175                         }
176                 }
177
178                 fep->rx_skbuff[curidx] = skbn;
179                 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
180                              L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
181                              DMA_FROM_DEVICE));
182                 CBDW_DATLEN(bdp, 0);
183                 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
184
185                 /*
186                  * Update BD pointer to next entry. 
187                  */
188                 if ((sc & BD_ENET_RX_WRAP) == 0)
189                         bdp++;
190                 else
191                         bdp = fep->rx_bd_base;
192
193                 (*fep->ops->rx_bd_done)(dev);
194
195                 if (received >= budget)
196                         break;
197         }
198
199         fep->cur_rx = bdp;
200
201         if (received >= budget) {
202                 /* done */
203                 netif_rx_complete(dev, napi);
204                 (*fep->ops->napi_enable_rx)(dev);
205         }
206         return received;
207 }
208
209 /* non NAPI receive function */
210 static int fs_enet_rx_non_napi(struct net_device *dev)
211 {
212         struct fs_enet_private *fep = netdev_priv(dev);
213         const struct fs_platform_info *fpi = fep->fpi;
214         cbd_t *bdp;
215         struct sk_buff *skb, *skbn, *skbt;
216         int received = 0;
217         u16 pkt_len, sc;
218         int curidx;
219         /*
220          * First, grab all of the stats for the incoming packet.
221          * These get messed up if we get called due to a busy condition.
222          */
223         bdp = fep->cur_rx;
224
225         while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
226
227                 curidx = bdp - fep->rx_bd_base;
228
229                 /*
230                  * Since we have allocated space to hold a complete frame,
231                  * the last indicator should be set.
232                  */
233                 if ((sc & BD_ENET_RX_LAST) == 0)
234                         printk(KERN_WARNING DRV_MODULE_NAME
235                                ": %s rcv is not +last\n",
236                                dev->name);
237
238                 /*
239                  * Check for errors. 
240                  */
241                 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
242                           BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
243                         fep->stats.rx_errors++;
244                         /* Frame too long or too short. */
245                         if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
246                                 fep->stats.rx_length_errors++;
247                         /* Frame alignment */
248                         if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
249                                 fep->stats.rx_frame_errors++;
250                         /* CRC Error */
251                         if (sc & BD_ENET_RX_CR)
252                                 fep->stats.rx_crc_errors++;
253                         /* FIFO overrun */
254                         if (sc & BD_ENET_RX_OV)
255                                 fep->stats.rx_crc_errors++;
256
257                         skb = fep->rx_skbuff[curidx];
258
259                         dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
260                                 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
261                                 DMA_FROM_DEVICE);
262
263                         skbn = skb;
264
265                 } else {
266
267                         skb = fep->rx_skbuff[curidx];
268
269                         dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
270                                 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
271                                 DMA_FROM_DEVICE);
272
273                         /*
274                          * Process the incoming frame.
275                          */
276                         fep->stats.rx_packets++;
277                         pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
278                         fep->stats.rx_bytes += pkt_len + 4;
279
280                         if (pkt_len <= fpi->rx_copybreak) {
281                                 /* +2 to make IP header L1 cache aligned */
282                                 skbn = dev_alloc_skb(pkt_len + 2);
283                                 if (skbn != NULL) {
284                                         skb_reserve(skbn, 2);   /* align IP header */
285                                         skb_copy_from_linear_data(skb,
286                                                       skbn->data, pkt_len);
287                                         /* swap */
288                                         skbt = skb;
289                                         skb = skbn;
290                                         skbn = skbt;
291                                 }
292                         } else
293                                 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
294
295                         if (skbn != NULL) {
296                                 skb_put(skb, pkt_len);  /* Make room */
297                                 skb->protocol = eth_type_trans(skb, dev);
298                                 received++;
299                                 netif_rx(skb);
300                         } else {
301                                 printk(KERN_WARNING DRV_MODULE_NAME
302                                        ": %s Memory squeeze, dropping packet.\n",
303                                        dev->name);
304                                 fep->stats.rx_dropped++;
305                                 skbn = skb;
306                         }
307                 }
308
309                 fep->rx_skbuff[curidx] = skbn;
310                 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
311                              L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
312                              DMA_FROM_DEVICE));
313                 CBDW_DATLEN(bdp, 0);
314                 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
315
316                 /*
317                  * Update BD pointer to next entry. 
318                  */
319                 if ((sc & BD_ENET_RX_WRAP) == 0)
320                         bdp++;
321                 else
322                         bdp = fep->rx_bd_base;
323
324                 (*fep->ops->rx_bd_done)(dev);
325         }
326
327         fep->cur_rx = bdp;
328
329         return 0;
330 }
331
332 static void fs_enet_tx(struct net_device *dev)
333 {
334         struct fs_enet_private *fep = netdev_priv(dev);
335         cbd_t *bdp;
336         struct sk_buff *skb;
337         int dirtyidx, do_wake, do_restart;
338         u16 sc;
339
340         spin_lock(&fep->lock);
341         bdp = fep->dirty_tx;
342
343         do_wake = do_restart = 0;
344         while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
345
346                 dirtyidx = bdp - fep->tx_bd_base;
347
348                 if (fep->tx_free == fep->tx_ring)
349                         break;
350
351                 skb = fep->tx_skbuff[dirtyidx];
352
353                 /*
354                  * Check for errors. 
355                  */
356                 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
357                           BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
358
359                         if (sc & BD_ENET_TX_HB) /* No heartbeat */
360                                 fep->stats.tx_heartbeat_errors++;
361                         if (sc & BD_ENET_TX_LC) /* Late collision */
362                                 fep->stats.tx_window_errors++;
363                         if (sc & BD_ENET_TX_RL) /* Retrans limit */
364                                 fep->stats.tx_aborted_errors++;
365                         if (sc & BD_ENET_TX_UN) /* Underrun */
366                                 fep->stats.tx_fifo_errors++;
367                         if (sc & BD_ENET_TX_CSL)        /* Carrier lost */
368                                 fep->stats.tx_carrier_errors++;
369
370                         if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
371                                 fep->stats.tx_errors++;
372                                 do_restart = 1;
373                         }
374                 } else
375                         fep->stats.tx_packets++;
376
377                 if (sc & BD_ENET_TX_READY)
378                         printk(KERN_WARNING DRV_MODULE_NAME
379                                ": %s HEY! Enet xmit interrupt and TX_READY.\n",
380                                dev->name);
381
382                 /*
383                  * Deferred means some collisions occurred during transmit,
384                  * but we eventually sent the packet OK.
385                  */
386                 if (sc & BD_ENET_TX_DEF)
387                         fep->stats.collisions++;
388
389                 /* unmap */
390                 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
391                                 skb->len, DMA_TO_DEVICE);
392
393                 /*
394                  * Free the sk buffer associated with this last transmit. 
395                  */
396                 dev_kfree_skb_irq(skb);
397                 fep->tx_skbuff[dirtyidx] = NULL;
398
399                 /*
400                  * Update pointer to next buffer descriptor to be transmitted. 
401                  */
402                 if ((sc & BD_ENET_TX_WRAP) == 0)
403                         bdp++;
404                 else
405                         bdp = fep->tx_bd_base;
406
407                 /*
408                  * Since we have freed up a buffer, the ring is no longer
409                  * full.
410                  */
411                 if (!fep->tx_free++)
412                         do_wake = 1;
413         }
414
415         fep->dirty_tx = bdp;
416
417         if (do_restart)
418                 (*fep->ops->tx_restart)(dev);
419
420         spin_unlock(&fep->lock);
421
422         if (do_wake)
423                 netif_wake_queue(dev);
424 }
425
426 /*
427  * The interrupt handler.
428  * This is called from the MPC core interrupt.
429  */
430 static irqreturn_t
431 fs_enet_interrupt(int irq, void *dev_id)
432 {
433         struct net_device *dev = dev_id;
434         struct fs_enet_private *fep;
435         const struct fs_platform_info *fpi;
436         u32 int_events;
437         u32 int_clr_events;
438         int nr, napi_ok;
439         int handled;
440
441         fep = netdev_priv(dev);
442         fpi = fep->fpi;
443
444         nr = 0;
445         while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
446
447                 nr++;
448
449                 int_clr_events = int_events;
450                 if (fpi->use_napi)
451                         int_clr_events &= ~fep->ev_napi_rx;
452
453                 (*fep->ops->clear_int_events)(dev, int_clr_events);
454
455                 if (int_events & fep->ev_err)
456                         (*fep->ops->ev_error)(dev, int_events);
457
458                 if (int_events & fep->ev_rx) {
459                         if (!fpi->use_napi)
460                                 fs_enet_rx_non_napi(dev);
461                         else {
462                                 napi_ok = napi_schedule_prep(&fep->napi);
463
464                                 (*fep->ops->napi_disable_rx)(dev);
465                                 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
466
467                                 /* NOTE: it is possible for FCCs in NAPI mode    */
468                                 /* to submit a spurious interrupt while in poll  */
469                                 if (napi_ok)
470                                         __netif_rx_schedule(dev, &fep->napi);
471                         }
472                 }
473
474                 if (int_events & fep->ev_tx)
475                         fs_enet_tx(dev);
476         }
477
478         handled = nr > 0;
479         return IRQ_RETVAL(handled);
480 }
481
482 void fs_init_bds(struct net_device *dev)
483 {
484         struct fs_enet_private *fep = netdev_priv(dev);
485         cbd_t *bdp;
486         struct sk_buff *skb;
487         int i;
488
489         fs_cleanup_bds(dev);
490
491         fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
492         fep->tx_free = fep->tx_ring;
493         fep->cur_rx = fep->rx_bd_base;
494
495         /*
496          * Initialize the receive buffer descriptors. 
497          */
498         for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
499                 skb = dev_alloc_skb(ENET_RX_FRSIZE);
500                 if (skb == NULL) {
501                         printk(KERN_WARNING DRV_MODULE_NAME
502                                ": %s Memory squeeze, unable to allocate skb\n",
503                                dev->name);
504                         break;
505                 }
506                 fep->rx_skbuff[i] = skb;
507                 CBDW_BUFADDR(bdp,
508                         dma_map_single(fep->dev, skb->data,
509                                 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
510                                 DMA_FROM_DEVICE));
511                 CBDW_DATLEN(bdp, 0);    /* zero */
512                 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
513                         ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
514         }
515         /*
516          * if we failed, fillup remainder 
517          */
518         for (; i < fep->rx_ring; i++, bdp++) {
519                 fep->rx_skbuff[i] = NULL;
520                 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
521         }
522
523         /*
524          * ...and the same for transmit.  
525          */
526         for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
527                 fep->tx_skbuff[i] = NULL;
528                 CBDW_BUFADDR(bdp, 0);
529                 CBDW_DATLEN(bdp, 0);
530                 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
531         }
532 }
533
534 void fs_cleanup_bds(struct net_device *dev)
535 {
536         struct fs_enet_private *fep = netdev_priv(dev);
537         struct sk_buff *skb;
538         cbd_t *bdp;
539         int i;
540
541         /*
542          * Reset SKB transmit buffers.  
543          */
544         for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
545                 if ((skb = fep->tx_skbuff[i]) == NULL)
546                         continue;
547
548                 /* unmap */
549                 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
550                                 skb->len, DMA_TO_DEVICE);
551
552                 fep->tx_skbuff[i] = NULL;
553                 dev_kfree_skb(skb);
554         }
555
556         /*
557          * Reset SKB receive buffers 
558          */
559         for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
560                 if ((skb = fep->rx_skbuff[i]) == NULL)
561                         continue;
562
563                 /* unmap */
564                 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
565                         L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
566                         DMA_FROM_DEVICE);
567
568                 fep->rx_skbuff[i] = NULL;
569
570                 dev_kfree_skb(skb);
571         }
572 }
573
574 /**********************************************************************************/
575
576 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
577 {
578         struct fs_enet_private *fep = netdev_priv(dev);
579         cbd_t *bdp;
580         int curidx;
581         u16 sc;
582         unsigned long flags;
583
584         spin_lock_irqsave(&fep->tx_lock, flags);
585
586         /*
587          * Fill in a Tx ring entry 
588          */
589         bdp = fep->cur_tx;
590
591         if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
592                 netif_stop_queue(dev);
593                 spin_unlock_irqrestore(&fep->tx_lock, flags);
594
595                 /*
596                  * Ooops.  All transmit buffers are full.  Bail out.
597                  * This should not happen, since the tx queue should be stopped.
598                  */
599                 printk(KERN_WARNING DRV_MODULE_NAME
600                        ": %s tx queue full!.\n", dev->name);
601                 return NETDEV_TX_BUSY;
602         }
603
604         curidx = bdp - fep->tx_bd_base;
605         /*
606          * Clear all of the status flags. 
607          */
608         CBDC_SC(bdp, BD_ENET_TX_STATS);
609
610         /*
611          * Save skb pointer. 
612          */
613         fep->tx_skbuff[curidx] = skb;
614
615         fep->stats.tx_bytes += skb->len;
616
617         /*
618          * Push the data cache so the CPM does not get stale memory data. 
619          */
620         CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
621                                 skb->data, skb->len, DMA_TO_DEVICE));
622         CBDW_DATLEN(bdp, skb->len);
623
624         dev->trans_start = jiffies;
625
626         /*
627          * If this was the last BD in the ring, start at the beginning again. 
628          */
629         if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
630                 fep->cur_tx++;
631         else
632                 fep->cur_tx = fep->tx_bd_base;
633
634         if (!--fep->tx_free)
635                 netif_stop_queue(dev);
636
637         /* Trigger transmission start */
638         sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
639              BD_ENET_TX_LAST | BD_ENET_TX_TC;
640
641         /* note that while FEC does not have this bit
642          * it marks it as available for software use
643          * yay for hw reuse :) */
644         if (skb->len <= 60)
645                 sc |= BD_ENET_TX_PAD;
646         CBDS_SC(bdp, sc);
647
648         (*fep->ops->tx_kickstart)(dev);
649
650         spin_unlock_irqrestore(&fep->tx_lock, flags);
651
652         return NETDEV_TX_OK;
653 }
654
655 static int fs_request_irq(struct net_device *dev, int irq, const char *name,
656                 irq_handler_t irqf)
657 {
658         struct fs_enet_private *fep = netdev_priv(dev);
659
660         (*fep->ops->pre_request_irq)(dev, irq);
661         return request_irq(irq, irqf, IRQF_SHARED, name, dev);
662 }
663
664 static void fs_free_irq(struct net_device *dev, int irq)
665 {
666         struct fs_enet_private *fep = netdev_priv(dev);
667
668         free_irq(irq, dev);
669         (*fep->ops->post_free_irq)(dev, irq);
670 }
671
672 static void fs_timeout(struct net_device *dev)
673 {
674         struct fs_enet_private *fep = netdev_priv(dev);
675         unsigned long flags;
676         int wake = 0;
677
678         fep->stats.tx_errors++;
679
680         spin_lock_irqsave(&fep->lock, flags);
681
682         if (dev->flags & IFF_UP) {
683                 phy_stop(fep->phydev);
684                 (*fep->ops->stop)(dev);
685                 (*fep->ops->restart)(dev);
686                 phy_start(fep->phydev);
687         }
688
689         phy_start(fep->phydev);
690         wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
691         spin_unlock_irqrestore(&fep->lock, flags);
692
693         if (wake)
694                 netif_wake_queue(dev);
695 }
696
697 /*-----------------------------------------------------------------------------
698  *  generic link-change handler - should be sufficient for most cases
699  *-----------------------------------------------------------------------------*/
700 static void generic_adjust_link(struct  net_device *dev)
701 {
702        struct fs_enet_private *fep = netdev_priv(dev);
703        struct phy_device *phydev = fep->phydev;
704        int new_state = 0;
705
706        if (phydev->link) {
707
708                /* adjust to duplex mode */
709                if (phydev->duplex != fep->oldduplex){
710                        new_state = 1;
711                        fep->oldduplex = phydev->duplex;
712                }
713
714                if (phydev->speed != fep->oldspeed) {
715                        new_state = 1;
716                        fep->oldspeed = phydev->speed;
717                }
718
719                if (!fep->oldlink) {
720                        new_state = 1;
721                        fep->oldlink = 1;
722                        netif_schedule(dev);
723                        netif_carrier_on(dev);
724                        netif_start_queue(dev);
725                }
726
727                if (new_state)
728                        fep->ops->restart(dev);
729
730        } else if (fep->oldlink) {
731                new_state = 1;
732                fep->oldlink = 0;
733                fep->oldspeed = 0;
734                fep->oldduplex = -1;
735                netif_carrier_off(dev);
736                netif_stop_queue(dev);
737        }
738
739        if (new_state && netif_msg_link(fep))
740                phy_print_status(phydev);
741 }
742
743
744 static void fs_adjust_link(struct net_device *dev)
745 {
746         struct fs_enet_private *fep = netdev_priv(dev);
747         unsigned long flags;
748
749         spin_lock_irqsave(&fep->lock, flags);
750
751         if(fep->ops->adjust_link)
752                 fep->ops->adjust_link(dev);
753         else
754                 generic_adjust_link(dev);
755
756         spin_unlock_irqrestore(&fep->lock, flags);
757 }
758
759 static int fs_init_phy(struct net_device *dev)
760 {
761         struct fs_enet_private *fep = netdev_priv(dev);
762         struct phy_device *phydev;
763
764         fep->oldlink = 0;
765         fep->oldspeed = 0;
766         fep->oldduplex = -1;
767         if(fep->fpi->bus_id)
768                 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
769                                 PHY_INTERFACE_MODE_MII);
770         else {
771                 printk("No phy bus ID specified in BSP code\n");
772                 return -EINVAL;
773         }
774         if (IS_ERR(phydev)) {
775                 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
776                 return PTR_ERR(phydev);
777         }
778
779         fep->phydev = phydev;
780
781         return 0;
782 }
783
784
785 static int fs_enet_open(struct net_device *dev)
786 {
787         struct fs_enet_private *fep = netdev_priv(dev);
788         int r;
789         int err;
790
791         napi_enable(&fep->napi);
792
793         /* Install our interrupt handler. */
794         r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
795         if (r != 0) {
796                 printk(KERN_ERR DRV_MODULE_NAME
797                        ": %s Could not allocate FS_ENET IRQ!", dev->name);
798                 napi_disable(&fep->napi);
799                 return -EINVAL;
800         }
801
802         err = fs_init_phy(dev);
803         if(err) {
804                 napi_disable(&fep->napi);
805                 return err;
806         }
807         phy_start(fep->phydev);
808
809         return 0;
810 }
811
812 static int fs_enet_close(struct net_device *dev)
813 {
814         struct fs_enet_private *fep = netdev_priv(dev);
815         unsigned long flags;
816
817         netif_stop_queue(dev);
818         netif_carrier_off(dev);
819         napi_disable(&fep->napi);
820         phy_stop(fep->phydev);
821
822         spin_lock_irqsave(&fep->lock, flags);
823         (*fep->ops->stop)(dev);
824         spin_unlock_irqrestore(&fep->lock, flags);
825
826         /* release any irqs */
827         phy_disconnect(fep->phydev);
828         fep->phydev = NULL;
829         fs_free_irq(dev, fep->interrupt);
830
831         return 0;
832 }
833
834 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
835 {
836         struct fs_enet_private *fep = netdev_priv(dev);
837         return &fep->stats;
838 }
839
840 /*************************************************************************/
841
842 static void fs_get_drvinfo(struct net_device *dev,
843                             struct ethtool_drvinfo *info)
844 {
845         strcpy(info->driver, DRV_MODULE_NAME);
846         strcpy(info->version, DRV_MODULE_VERSION);
847 }
848
849 static int fs_get_regs_len(struct net_device *dev)
850 {
851         struct fs_enet_private *fep = netdev_priv(dev);
852
853         return (*fep->ops->get_regs_len)(dev);
854 }
855
856 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
857                          void *p)
858 {
859         struct fs_enet_private *fep = netdev_priv(dev);
860         unsigned long flags;
861         int r, len;
862
863         len = regs->len;
864
865         spin_lock_irqsave(&fep->lock, flags);
866         r = (*fep->ops->get_regs)(dev, p, &len);
867         spin_unlock_irqrestore(&fep->lock, flags);
868
869         if (r == 0)
870                 regs->version = 0;
871 }
872
873 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
874 {
875         struct fs_enet_private *fep = netdev_priv(dev);
876         return phy_ethtool_gset(fep->phydev, cmd);
877 }
878
879 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
880 {
881         struct fs_enet_private *fep = netdev_priv(dev);
882         phy_ethtool_sset(fep->phydev, cmd);
883         return 0;
884 }
885
886 static int fs_nway_reset(struct net_device *dev)
887 {
888         return 0;
889 }
890
891 static u32 fs_get_msglevel(struct net_device *dev)
892 {
893         struct fs_enet_private *fep = netdev_priv(dev);
894         return fep->msg_enable;
895 }
896
897 static void fs_set_msglevel(struct net_device *dev, u32 value)
898 {
899         struct fs_enet_private *fep = netdev_priv(dev);
900         fep->msg_enable = value;
901 }
902
903 static const struct ethtool_ops fs_ethtool_ops = {
904         .get_drvinfo = fs_get_drvinfo,
905         .get_regs_len = fs_get_regs_len,
906         .get_settings = fs_get_settings,
907         .set_settings = fs_set_settings,
908         .nway_reset = fs_nway_reset,
909         .get_link = ethtool_op_get_link,
910         .get_msglevel = fs_get_msglevel,
911         .set_msglevel = fs_set_msglevel,
912         .get_tx_csum = ethtool_op_get_tx_csum,
913         .set_tx_csum = ethtool_op_set_tx_csum,  /* local! */
914         .get_sg = ethtool_op_get_sg,
915         .set_sg = ethtool_op_set_sg,
916         .get_regs = fs_get_regs,
917 };
918
919 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
920 {
921         struct fs_enet_private *fep = netdev_priv(dev);
922         struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
923         unsigned long flags;
924         int rc;
925
926         if (!netif_running(dev))
927                 return -EINVAL;
928
929         spin_lock_irqsave(&fep->lock, flags);
930         rc = phy_mii_ioctl(fep->phydev, mii, cmd);
931         spin_unlock_irqrestore(&fep->lock, flags);
932         return rc;
933 }
934
935 extern int fs_mii_connect(struct net_device *dev);
936 extern void fs_mii_disconnect(struct net_device *dev);
937
938 static struct net_device *fs_init_instance(struct device *dev,
939                 struct fs_platform_info *fpi)
940 {
941         struct net_device *ndev = NULL;
942         struct fs_enet_private *fep = NULL;
943         int privsize, i, r, err = 0, registered = 0;
944
945         fpi->fs_no = fs_get_id(fpi);
946         /* guard */
947         if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
948                 return ERR_PTR(-EINVAL);
949
950         privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
951                             (fpi->rx_ring + fpi->tx_ring));
952
953         ndev = alloc_etherdev(privsize);
954         if (!ndev) {
955                 err = -ENOMEM;
956                 goto err;
957         }
958         SET_MODULE_OWNER(ndev);
959
960         fep = netdev_priv(ndev);
961         memset(fep, 0, privsize);       /* clear everything */
962
963         fep->dev = dev;
964         dev_set_drvdata(dev, ndev);
965         fep->fpi = fpi;
966         if (fpi->init_ioports)
967                 fpi->init_ioports((struct fs_platform_info *)fpi);
968
969 #ifdef CONFIG_FS_ENET_HAS_FEC
970         if (fs_get_fec_index(fpi->fs_no) >= 0)
971                 fep->ops = &fs_fec_ops;
972 #endif
973
974 #ifdef CONFIG_FS_ENET_HAS_SCC
975         if (fs_get_scc_index(fpi->fs_no) >=0 )
976                 fep->ops = &fs_scc_ops;
977 #endif
978
979 #ifdef CONFIG_FS_ENET_HAS_FCC
980         if (fs_get_fcc_index(fpi->fs_no) >= 0)
981                 fep->ops = &fs_fcc_ops;
982 #endif
983
984         if (fep->ops == NULL) {
985                 printk(KERN_ERR DRV_MODULE_NAME
986                        ": %s No matching ops found (%d).\n",
987                        ndev->name, fpi->fs_no);
988                 err = -EINVAL;
989                 goto err;
990         }
991
992         r = (*fep->ops->setup_data)(ndev);
993         if (r != 0) {
994                 printk(KERN_ERR DRV_MODULE_NAME
995                        ": %s setup_data failed\n",
996                         ndev->name);
997                 err = r;
998                 goto err;
999         }
1000
1001         /* point rx_skbuff, tx_skbuff */
1002         fep->rx_skbuff = (struct sk_buff **)&fep[1];
1003         fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1004
1005         /* init locks */
1006         spin_lock_init(&fep->lock);
1007         spin_lock_init(&fep->tx_lock);
1008
1009         /*
1010          * Set the Ethernet address. 
1011          */
1012         for (i = 0; i < 6; i++)
1013                 ndev->dev_addr[i] = fpi->macaddr[i];
1014         
1015         r = (*fep->ops->allocate_bd)(ndev);
1016         
1017         if (fep->ring_base == NULL) {
1018                 printk(KERN_ERR DRV_MODULE_NAME
1019                        ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
1020                 err = r;
1021                 goto err;
1022         }
1023
1024         /*
1025          * Set receive and transmit descriptor base.
1026          */
1027         fep->rx_bd_base = fep->ring_base;
1028         fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1029
1030         /* initialize ring size variables */
1031         fep->tx_ring = fpi->tx_ring;
1032         fep->rx_ring = fpi->rx_ring;
1033
1034         /*
1035          * The FEC Ethernet specific entries in the device structure. 
1036          */
1037         ndev->open = fs_enet_open;
1038         ndev->hard_start_xmit = fs_enet_start_xmit;
1039         ndev->tx_timeout = fs_timeout;
1040         ndev->watchdog_timeo = 2 * HZ;
1041         ndev->stop = fs_enet_close;
1042         ndev->get_stats = fs_enet_get_stats;
1043         ndev->set_multicast_list = fs_set_multicast_list;
1044         netif_napi_add(ndev, &fep->napi,
1045                        fs_enet_rx_napi, fpi->napi_weight);
1046
1047         ndev->ethtool_ops = &fs_ethtool_ops;
1048         ndev->do_ioctl = fs_ioctl;
1049
1050         init_timer(&fep->phy_timer_list);
1051
1052         netif_carrier_off(ndev);
1053
1054         err = register_netdev(ndev);
1055         if (err != 0) {
1056                 printk(KERN_ERR DRV_MODULE_NAME
1057                        ": %s register_netdev failed.\n", ndev->name);
1058                 goto err;
1059         }
1060         registered = 1;
1061
1062
1063         return ndev;
1064
1065       err:
1066         if (ndev != NULL) {
1067
1068                 if (registered)
1069                         unregister_netdev(ndev);
1070
1071                 if (fep != NULL) {
1072                         (*fep->ops->free_bd)(ndev);
1073                         (*fep->ops->cleanup_data)(ndev);
1074                 }
1075
1076                 free_netdev(ndev);
1077         }
1078
1079         dev_set_drvdata(dev, NULL);
1080
1081         return ERR_PTR(err);
1082 }
1083
1084 static int fs_cleanup_instance(struct net_device *ndev)
1085 {
1086         struct fs_enet_private *fep;
1087         const struct fs_platform_info *fpi;
1088         struct device *dev;
1089
1090         if (ndev == NULL)
1091                 return -EINVAL;
1092
1093         fep = netdev_priv(ndev);
1094         if (fep == NULL)
1095                 return -EINVAL;
1096
1097         fpi = fep->fpi;
1098
1099         unregister_netdev(ndev);
1100
1101         dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1102                           fep->ring_base, fep->ring_mem_addr);
1103
1104         /* reset it */
1105         (*fep->ops->cleanup_data)(ndev);
1106
1107         dev = fep->dev;
1108         if (dev != NULL) {
1109                 dev_set_drvdata(dev, NULL);
1110                 fep->dev = NULL;
1111         }
1112
1113         free_netdev(ndev);
1114
1115         return 0;
1116 }
1117
1118 /**************************************************************************************/
1119
1120 /* handy pointer to the immap */
1121 void *fs_enet_immap = NULL;
1122
1123 static int setup_immap(void)
1124 {
1125         phys_addr_t paddr = 0;
1126         unsigned long size = 0;
1127
1128 #ifdef CONFIG_CPM1
1129         paddr = IMAP_ADDR;
1130         size = 0x10000; /* map 64K */
1131 #endif
1132
1133 #ifdef CONFIG_CPM2
1134         paddr = CPM_MAP_ADDR;
1135         size = 0x40000; /* map 256 K */
1136 #endif
1137         fs_enet_immap = ioremap(paddr, size);
1138         if (fs_enet_immap == NULL)
1139                 return -EBADF;  /* XXX ahem; maybe just BUG_ON? */
1140
1141         return 0;
1142 }
1143
1144 static void cleanup_immap(void)
1145 {
1146         if (fs_enet_immap != NULL) {
1147                 iounmap(fs_enet_immap);
1148                 fs_enet_immap = NULL;
1149         }
1150 }
1151
1152 /**************************************************************************************/
1153
1154 static int __devinit fs_enet_probe(struct device *dev)
1155 {
1156         struct net_device *ndev;
1157
1158         /* no fixup - no device */
1159         if (dev->platform_data == NULL) {
1160                 printk(KERN_INFO "fs_enet: "
1161                                 "probe called with no platform data; "
1162                                 "remove unused devices\n");
1163                 return -ENODEV;
1164         }
1165
1166         ndev = fs_init_instance(dev, dev->platform_data);
1167         if (IS_ERR(ndev))
1168                 return PTR_ERR(ndev);
1169         return 0;
1170 }
1171
1172 static int fs_enet_remove(struct device *dev)
1173 {
1174         return fs_cleanup_instance(dev_get_drvdata(dev));
1175 }
1176
1177 static struct device_driver fs_enet_fec_driver = {
1178         .name           = "fsl-cpm-fec",
1179         .bus            = &platform_bus_type,
1180         .probe          = fs_enet_probe,
1181         .remove         = fs_enet_remove,
1182 #ifdef CONFIG_PM
1183 /*      .suspend        = fs_enet_suspend,      TODO */
1184 /*      .resume         = fs_enet_resume,       TODO */
1185 #endif
1186 };
1187
1188 static struct device_driver fs_enet_scc_driver = {
1189         .name           = "fsl-cpm-scc",
1190         .bus            = &platform_bus_type,
1191         .probe          = fs_enet_probe,
1192         .remove         = fs_enet_remove,
1193 #ifdef CONFIG_PM
1194 /*      .suspend        = fs_enet_suspend,      TODO */
1195 /*      .resume         = fs_enet_resume,       TODO */
1196 #endif
1197 };
1198
1199 static struct device_driver fs_enet_fcc_driver = {
1200         .name           = "fsl-cpm-fcc",
1201         .bus            = &platform_bus_type,
1202         .probe          = fs_enet_probe,
1203         .remove         = fs_enet_remove,
1204 #ifdef CONFIG_PM
1205 /*      .suspend        = fs_enet_suspend,      TODO */
1206 /*      .resume         = fs_enet_resume,       TODO */
1207 #endif
1208 };
1209
1210 static int __init fs_init(void)
1211 {
1212         int r;
1213
1214         printk(KERN_INFO
1215                         "%s", version);
1216
1217         r = setup_immap();
1218         if (r != 0)
1219                 return r;
1220
1221 #ifdef CONFIG_FS_ENET_HAS_FCC
1222         /* let's insert mii stuff */
1223         r = fs_enet_mdio_bb_init();
1224
1225         if (r != 0) {
1226                 printk(KERN_ERR DRV_MODULE_NAME
1227                         "BB PHY init failed.\n");
1228                 return r;
1229         }
1230         r = driver_register(&fs_enet_fcc_driver);
1231         if (r != 0)
1232                 goto err;
1233 #endif
1234
1235 #ifdef CONFIG_FS_ENET_HAS_FEC
1236         r =  fs_enet_mdio_fec_init();
1237         if (r != 0) {
1238                 printk(KERN_ERR DRV_MODULE_NAME
1239                         "FEC PHY init failed.\n");
1240                 return r;
1241         }
1242
1243         r = driver_register(&fs_enet_fec_driver);
1244         if (r != 0)
1245                 goto err;
1246 #endif
1247
1248 #ifdef CONFIG_FS_ENET_HAS_SCC
1249         r = driver_register(&fs_enet_scc_driver);
1250         if (r != 0)
1251                 goto err;
1252 #endif
1253
1254         return 0;
1255 err:
1256         cleanup_immap();
1257         return r;
1258         
1259 }
1260
1261 static void __exit fs_cleanup(void)
1262 {
1263         driver_unregister(&fs_enet_fec_driver);
1264         driver_unregister(&fs_enet_fcc_driver);
1265         driver_unregister(&fs_enet_scc_driver);
1266         cleanup_immap();
1267 }
1268
1269 /**************************************************************************************/
1270
1271 module_init(fs_init);
1272 module_exit(fs_cleanup);