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