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