3 * 10/100/1000 Base-T Ethernet Driver for the ET1310 and ET131x series MACs
5 * Copyright © 2005 Agere Systems Inc.
9 * Copyright (c) 2011 Mark Einon <mark.einon@gmail.com>
11 *------------------------------------------------------------------------------
13 * et131x_netdev.c - Routines and data required by all Linux network devices.
15 *------------------------------------------------------------------------------
19 * This software is provided subject to the following terms and conditions,
20 * which you should read carefully before using the software. Using this
21 * software indicates your acceptance of these terms and conditions. If you do
22 * not agree with these terms and conditions, do not use the software.
24 * Copyright © 2005 Agere Systems Inc.
25 * All rights reserved.
27 * Redistribution and use in source or binary forms, with or without
28 * modifications, are permitted provided that the following conditions are met:
30 * . Redistributions of source code must retain the above copyright notice, this
31 * list of conditions and the following Disclaimer as comments in the code as
32 * well as in the documentation and/or other materials provided with the
35 * . Redistributions in binary form must reproduce the above copyright notice,
36 * this list of conditions and the following Disclaimer in the documentation
37 * and/or other materials provided with the distribution.
39 * . Neither the name of Agere Systems Inc. nor the names of the contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
46 * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
47 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ANY
48 * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
49 * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
50 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
60 #include "et131x_version.h"
61 #include "et131x_defs.h"
63 #include <linux/init.h>
64 #include <linux/module.h>
65 #include <linux/types.h>
66 #include <linux/kernel.h>
68 #include <linux/sched.h>
69 #include <linux/ptrace.h>
70 #include <linux/ctype.h>
71 #include <linux/string.h>
72 #include <linux/timer.h>
73 #include <linux/interrupt.h>
75 #include <linux/delay.h>
77 #include <linux/bitops.h>
78 #include <linux/pci.h>
79 #include <asm/system.h>
81 #include <linux/mii.h>
82 #include <linux/netdevice.h>
83 #include <linux/etherdevice.h>
84 #include <linux/skbuff.h>
85 #include <linux/if_arp.h>
86 #include <linux/ioport.h>
87 #include <linux/phy.h>
89 #include "et1310_phy.h"
90 #include "et1310_tx.h"
91 #include "et131x_adapter.h"
95 * et131x_stats - Return the current device statistics.
96 * @netdev: device whose stats are being queried
98 * Returns 0 on success, errno on failure (as defined in errno.h)
100 static struct net_device_stats
*et131x_stats(struct net_device
*netdev
)
102 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
103 struct net_device_stats
*stats
= &adapter
->net_stats
;
104 struct ce_stats
*devstat
= &adapter
->stats
;
106 stats
->rx_errors
= devstat
->rx_length_errs
+
107 devstat
->rx_align_errs
+
108 devstat
->rx_crc_errs
+
109 devstat
->rx_code_violations
+
110 devstat
->rx_other_errs
;
111 stats
->tx_errors
= devstat
->tx_max_pkt_errs
;
112 stats
->multicast
= devstat
->multicast_pkts_rcvd
;
113 stats
->collisions
= devstat
->tx_collisions
;
115 stats
->rx_length_errors
= devstat
->rx_length_errs
;
116 stats
->rx_over_errors
= devstat
->rx_overflows
;
117 stats
->rx_crc_errors
= devstat
->rx_crc_errs
;
119 /* NOTE: These stats don't have corresponding values in CE_STATS,
120 * so we're going to have to update these directly from within the
123 /* stats->rx_bytes = 20; devstat->; */
124 /* stats->tx_bytes = 20; devstat->; */
125 /* stats->rx_dropped = devstat->; */
126 /* stats->tx_dropped = devstat->; */
128 /* NOTE: Not used, can't find analogous statistics */
129 /* stats->rx_frame_errors = devstat->; */
130 /* stats->rx_fifo_errors = devstat->; */
131 /* stats->rx_missed_errors = devstat->; */
133 /* stats->tx_aborted_errors = devstat->; */
134 /* stats->tx_carrier_errors = devstat->; */
135 /* stats->tx_fifo_errors = devstat->; */
136 /* stats->tx_heartbeat_errors = devstat->; */
137 /* stats->tx_window_errors = devstat->; */
142 * et131x_enable_txrx - Enable tx/rx queues
143 * @netdev: device to be enabled
145 void et131x_enable_txrx(struct net_device
*netdev
)
147 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
149 /* Enable the Tx and Rx DMA engines (if not already enabled) */
150 et131x_rx_dma_enable(adapter
);
151 et131x_tx_dma_enable(adapter
);
153 /* Enable device interrupts */
154 if (adapter
->flags
& fMP_ADAPTER_INTERRUPT_IN_USE
)
155 et131x_enable_interrupts(adapter
);
157 /* We're ready to move some data, so start the queue */
158 netif_start_queue(netdev
);
162 * et131x_disable_txrx - Disable tx/rx queues
163 * @netdev: device to be disabled
165 void et131x_disable_txrx(struct net_device
*netdev
)
167 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
169 /* First thing is to stop the queue */
170 netif_stop_queue(netdev
);
172 /* Stop the Tx and Rx DMA engines */
173 et131x_rx_dma_disable(adapter
);
174 et131x_tx_dma_disable(adapter
);
176 /* Disable device interrupts */
177 et131x_disable_interrupts(adapter
);
181 * et131x_up - Bring up a device for use.
182 * @netdev: device to be opened
184 void et131x_up(struct net_device
*netdev
)
186 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
188 et131x_enable_txrx(netdev
);
189 phy_start(adapter
->phydev
);
193 * et131x_open - Open the device for use.
194 * @netdev: device to be opened
196 * Returns 0 on success, errno on failure (as defined in errno.h)
198 int et131x_open(struct net_device
*netdev
)
201 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
203 /* Start the timer to track NIC errors */
204 init_timer(&adapter
->error_timer
);
205 adapter
->error_timer
.expires
= jiffies
+ TX_ERROR_PERIOD
* HZ
/ 1000;
206 adapter
->error_timer
.function
= et131x_error_timer_handler
;
207 adapter
->error_timer
.data
= (unsigned long)adapter
;
208 add_timer(&adapter
->error_timer
);
210 /* Register our IRQ */
211 result
= request_irq(netdev
->irq
, et131x_isr
, IRQF_SHARED
,
212 netdev
->name
, netdev
);
214 dev_err(&adapter
->pdev
->dev
, "could not register IRQ %d\n",
219 adapter
->flags
|= fMP_ADAPTER_INTERRUPT_IN_USE
;
227 * et131x_down - Bring down the device
228 * @netdev: device to be broght down
230 void et131x_down(struct net_device
*netdev
)
232 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
234 /* Save the timestamp for the TX watchdog, prevent a timeout */
235 netdev
->trans_start
= jiffies
;
237 phy_stop(adapter
->phydev
);
238 et131x_disable_txrx(netdev
);
242 * et131x_close - Close the device
243 * @netdev: device to be closed
245 * Returns 0 on success, errno on failure (as defined in errno.h)
247 int et131x_close(struct net_device
*netdev
)
249 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
253 adapter
->flags
&= ~fMP_ADAPTER_INTERRUPT_IN_USE
;
254 free_irq(netdev
->irq
, netdev
);
256 /* Stop the error timer */
257 return del_timer_sync(&adapter
->error_timer
);
261 * et131x_ioctl - The I/O Control handler for the driver
262 * @netdev: device on which the control request is being made
263 * @reqbuf: a pointer to the IOCTL request buffer
264 * @cmd: the IOCTL command code
266 * Returns 0 on success, errno on failure (as defined in errno.h)
268 int et131x_ioctl(struct net_device
*netdev
, struct ifreq
*reqbuf
, int cmd
)
270 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
272 if (!adapter
->phydev
)
275 return phy_mii_ioctl(adapter
->phydev
, reqbuf
, cmd
);
279 * et131x_set_packet_filter - Configures the Rx Packet filtering on the device
280 * @adapter: pointer to our private adapter structure
282 * FIXME: lot of dups with MAC code
284 * Returns 0 on success, errno on failure
286 int et131x_set_packet_filter(struct et131x_adapter
*adapter
)
289 uint32_t filter
= adapter
->packet_filter
;
293 ctrl
= readl(&adapter
->regs
->rxmac
.ctrl
);
294 pf_ctrl
= readl(&adapter
->regs
->rxmac
.pf_ctrl
);
296 /* Default to disabled packet filtering. Enable it in the individual
297 * case statements that require the device to filter something
301 /* Set us to be in promiscuous mode so we receive everything, this
302 * is also true when we get a packet filter of 0
304 if ((filter
& ET131X_PACKET_TYPE_PROMISCUOUS
) || filter
== 0)
305 pf_ctrl
&= ~7; /* Clear filter bits */
308 * Set us up with Multicast packet filtering. Three cases are
309 * possible - (1) we have a multi-cast list, (2) we receive ALL
310 * multicast entries or (3) we receive none.
312 if (filter
& ET131X_PACKET_TYPE_ALL_MULTICAST
)
313 pf_ctrl
&= ~2; /* Multicast filter bit */
315 et1310_setup_device_for_multicast(adapter
);
320 /* Set us up with Unicast packet filtering */
321 if (filter
& ET131X_PACKET_TYPE_DIRECTED
) {
322 et1310_setup_device_for_unicast(adapter
);
327 /* Set us up with Broadcast packet filtering */
328 if (filter
& ET131X_PACKET_TYPE_BROADCAST
) {
329 pf_ctrl
|= 1; /* Broadcast filter bit */
334 /* Setup the receive mac configuration registers - Packet
335 * Filter control + the enable / disable for packet filter
336 * in the control reg.
338 writel(pf_ctrl
, &adapter
->regs
->rxmac
.pf_ctrl
);
339 writel(ctrl
, &adapter
->regs
->rxmac
.ctrl
);
345 * et131x_multicast - The handler to configure multicasting on the interface
346 * @netdev: a pointer to a net_device struct representing the device
348 void et131x_multicast(struct net_device
*netdev
)
350 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
351 uint32_t packet_filter
= 0;
353 struct netdev_hw_addr
*ha
;
356 spin_lock_irqsave(&adapter
->lock
, flags
);
358 /* Before we modify the platform-independent filter flags, store them
359 * locally. This allows us to determine if anything's changed and if
360 * we even need to bother the hardware
362 packet_filter
= adapter
->packet_filter
;
364 /* Clear the 'multicast' flag locally; because we only have a single
365 * flag to check multicast, and multiple multicast addresses can be
366 * set, this is the easiest way to determine if more than one
367 * multicast address is being set.
369 packet_filter
&= ~ET131X_PACKET_TYPE_MULTICAST
;
371 /* Check the net_device flags and set the device independent flags
375 if (netdev
->flags
& IFF_PROMISC
)
376 adapter
->packet_filter
|= ET131X_PACKET_TYPE_PROMISCUOUS
;
378 adapter
->packet_filter
&= ~ET131X_PACKET_TYPE_PROMISCUOUS
;
380 if (netdev
->flags
& IFF_ALLMULTI
)
381 adapter
->packet_filter
|= ET131X_PACKET_TYPE_ALL_MULTICAST
;
383 if (netdev_mc_count(netdev
) > NIC_MAX_MCAST_LIST
)
384 adapter
->packet_filter
|= ET131X_PACKET_TYPE_ALL_MULTICAST
;
386 if (netdev_mc_count(netdev
) < 1) {
387 adapter
->packet_filter
&= ~ET131X_PACKET_TYPE_ALL_MULTICAST
;
388 adapter
->packet_filter
&= ~ET131X_PACKET_TYPE_MULTICAST
;
390 adapter
->packet_filter
|= ET131X_PACKET_TYPE_MULTICAST
;
392 /* Set values in the private adapter struct */
394 netdev_for_each_mc_addr(ha
, netdev
) {
395 if (i
== NIC_MAX_MCAST_LIST
)
397 memcpy(adapter
->multicast_list
[i
++], ha
->addr
, ETH_ALEN
);
399 adapter
->multicast_addr_count
= i
;
401 /* Are the new flags different from the previous ones? If not, then no
404 * NOTE - This block will always update the multicast_list with the
405 * hardware, even if the addresses aren't the same.
407 if (packet_filter
!= adapter
->packet_filter
) {
408 /* Call the device's filter function */
409 et131x_set_packet_filter(adapter
);
411 spin_unlock_irqrestore(&adapter
->lock
, flags
);
415 * et131x_tx - The handler to tx a packet on the device
416 * @skb: data to be Tx'd
417 * @netdev: device on which data is to be Tx'd
419 * Returns 0 on success, errno on failure (as defined in errno.h)
421 int et131x_tx(struct sk_buff
*skb
, struct net_device
*netdev
)
425 /* Save the timestamp for the TX timeout watchdog */
426 netdev
->trans_start
= jiffies
;
428 /* Call the device-specific data Tx routine */
429 status
= et131x_send_packets(skb
, netdev
);
431 /* Check status and manage the netif queue if necessary */
433 if (status
== -ENOMEM
) {
434 /* Put the queue to sleep until resources are
437 netif_stop_queue(netdev
);
438 status
= NETDEV_TX_BUSY
;
440 status
= NETDEV_TX_OK
;
447 * et131x_tx_timeout - Timeout handler
448 * @netdev: a pointer to a net_device struct representing the device
450 * The handler called when a Tx request times out. The timeout period is
451 * specified by the 'tx_timeo" element in the net_device structure (see
452 * et131x_alloc_device() to see how this value is set).
454 void et131x_tx_timeout(struct net_device
*netdev
)
456 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
460 /* If the device is closed, ignore the timeout */
461 if (~(adapter
->flags
& fMP_ADAPTER_INTERRUPT_IN_USE
))
464 /* Any nonrecoverable hardware error?
465 * Checks adapter->flags for any failure in phy reading
467 if (adapter
->flags
& fMP_ADAPTER_NON_RECOVER_ERROR
)
470 /* Hardware failure? */
471 if (adapter
->flags
& fMP_ADAPTER_HARDWARE_ERROR
) {
472 dev_err(&adapter
->pdev
->dev
, "hardware error - reset\n");
477 spin_lock_irqsave(&adapter
->tcb_send_qlock
, flags
);
479 tcb
= adapter
->tx_ring
.send_head
;
484 if (tcb
->count
> NIC_SEND_HANG_THRESHOLD
) {
485 spin_unlock_irqrestore(&adapter
->tcb_send_qlock
,
488 dev_warn(&adapter
->pdev
->dev
,
489 "Send stuck - reset. tcb->WrIndex %x, flags 0x%08x\n",
493 adapter
->net_stats
.tx_errors
++;
495 /* perform reset of tx/rx */
496 et131x_disable_txrx(netdev
);
497 et131x_enable_txrx(netdev
);
502 spin_unlock_irqrestore(&adapter
->tcb_send_qlock
, flags
);
506 * et131x_change_mtu - The handler called to change the MTU for the device
507 * @netdev: device whose MTU is to be changed
508 * @new_mtu: the desired MTU
510 * Returns 0 on success, errno on failure (as defined in errno.h)
512 int et131x_change_mtu(struct net_device
*netdev
, int new_mtu
)
515 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
517 /* Make sure the requested MTU is valid */
518 if (new_mtu
< 64 || new_mtu
> 9216)
521 et131x_disable_txrx(netdev
);
522 et131x_handle_send_interrupt(adapter
);
523 et131x_handle_recv_interrupt(adapter
);
525 /* Set the new MTU */
526 netdev
->mtu
= new_mtu
;
528 /* Free Rx DMA memory */
529 et131x_adapter_memory_free(adapter
);
531 /* Set the config parameter for Jumbo Packet support */
532 adapter
->registry_jumbo_packet
= new_mtu
+ 14;
533 et131x_soft_reset(adapter
);
535 /* Alloc and init Rx DMA memory */
536 result
= et131x_adapter_memory_alloc(adapter
);
538 dev_warn(&adapter
->pdev
->dev
,
539 "Change MTU failed; couldn't re-alloc DMA memory\n");
543 et131x_init_send(adapter
);
545 et131x_hwaddr_init(adapter
);
546 memcpy(netdev
->dev_addr
, adapter
->addr
, ETH_ALEN
);
548 /* Init the device with the new settings */
549 et131x_adapter_setup(adapter
);
551 et131x_enable_txrx(netdev
);
557 * et131x_set_mac_addr - handler to change the MAC address for the device
558 * @netdev: device whose MAC is to be changed
559 * @new_mac: the desired MAC address
561 * Returns 0 on success, errno on failure (as defined in errno.h)
563 * IMPLEMENTED BY : blux http://berndlux.de 22.01.2007 21:14
565 int et131x_set_mac_addr(struct net_device
*netdev
, void *new_mac
)
568 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
569 struct sockaddr
*address
= new_mac
;
576 /* Make sure the requested MAC is valid */
577 if (!is_valid_ether_addr(address
->sa_data
))
580 et131x_disable_txrx(netdev
);
581 et131x_handle_send_interrupt(adapter
);
582 et131x_handle_recv_interrupt(adapter
);
584 /* Set the new MAC */
585 /* netdev->set_mac_address = &new_mac; */
587 memcpy(netdev
->dev_addr
, address
->sa_data
, netdev
->addr_len
);
589 printk(KERN_INFO
"%s: Setting MAC address to %pM\n",
590 netdev
->name
, netdev
->dev_addr
);
592 /* Free Rx DMA memory */
593 et131x_adapter_memory_free(adapter
);
595 et131x_soft_reset(adapter
);
597 /* Alloc and init Rx DMA memory */
598 result
= et131x_adapter_memory_alloc(adapter
);
600 dev_err(&adapter
->pdev
->dev
,
601 "Change MAC failed; couldn't re-alloc DMA memory\n");
605 et131x_init_send(adapter
);
607 et131x_hwaddr_init(adapter
);
609 /* Init the device with the new settings */
610 et131x_adapter_setup(adapter
);
612 et131x_enable_txrx(netdev
);
617 static const struct net_device_ops et131x_netdev_ops
= {
618 .ndo_open
= et131x_open
,
619 .ndo_stop
= et131x_close
,
620 .ndo_start_xmit
= et131x_tx
,
621 .ndo_set_multicast_list
= et131x_multicast
,
622 .ndo_tx_timeout
= et131x_tx_timeout
,
623 .ndo_change_mtu
= et131x_change_mtu
,
624 .ndo_set_mac_address
= et131x_set_mac_addr
,
625 .ndo_validate_addr
= eth_validate_addr
,
626 .ndo_get_stats
= et131x_stats
,
627 .ndo_do_ioctl
= et131x_ioctl
,
631 * et131x_device_alloc
633 * Returns pointer to the allocated and initialized net_device struct for
636 * Create instances of net_device and wl_private for the new adapter and
637 * register the device's entry points in the net_device structure.
639 struct net_device
*et131x_device_alloc(void)
641 struct net_device
*netdev
;
643 /* Alloc net_device and adapter structs */
644 netdev
= alloc_etherdev(sizeof(struct et131x_adapter
));
647 printk(KERN_ERR
"et131x: Alloc of net_device struct failed\n");
652 * Setup the function registration table (and other data) for a
655 netdev
->watchdog_timeo
= ET131X_TX_TIMEOUT
;
656 netdev
->netdev_ops
= &et131x_netdev_ops
;
659 /* netdev->poll = &et131x_poll; */
660 /* netdev->poll_controller = &et131x_poll_controller; */