3 * 10/100/1000 Base-T Ethernet Driver for the ET1310 and ET131x series MACs
5 * Copyright © 2005 Agere Systems Inc.
9 *------------------------------------------------------------------------------
11 * et131x_netdev.c - Routines and data required by all Linux network devices.
13 *------------------------------------------------------------------------------
17 * This software is provided subject to the following terms and conditions,
18 * which you should read carefully before using the software. Using this
19 * software indicates your acceptance of these terms and conditions. If you do
20 * not agree with these terms and conditions, do not use the software.
22 * Copyright © 2005 Agere Systems Inc.
23 * All rights reserved.
25 * Redistribution and use in source or binary forms, with or without
26 * modifications, are permitted provided that the following conditions are met:
28 * . Redistributions of source code must retain the above copyright notice, this
29 * list of conditions and the following Disclaimer as comments in the code as
30 * well as in the documentation and/or other materials provided with the
33 * . Redistributions in binary form must reproduce the above copyright notice,
34 * this list of conditions and the following Disclaimer in the documentation
35 * and/or other materials provided with the distribution.
37 * . Neither the name of Agere Systems Inc. nor the names of the contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
44 * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
45 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ANY
46 * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
47 * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
48 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51 * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
58 #include "et131x_version.h"
59 #include "et131x_defs.h"
61 #include <linux/init.h>
62 #include <linux/module.h>
63 #include <linux/types.h>
64 #include <linux/kernel.h>
66 #include <linux/sched.h>
67 #include <linux/ptrace.h>
68 #include <linux/ctype.h>
69 #include <linux/string.h>
70 #include <linux/timer.h>
71 #include <linux/interrupt.h>
73 #include <linux/delay.h>
75 #include <linux/bitops.h>
76 #include <linux/pci.h>
77 #include <asm/system.h>
79 #include <linux/mii.h>
80 #include <linux/netdevice.h>
81 #include <linux/etherdevice.h>
82 #include <linux/skbuff.h>
83 #include <linux/if_arp.h>
84 #include <linux/ioport.h>
85 #include <linux/phy.h>
87 #include "et1310_phy.h"
88 #include "et1310_tx.h"
89 #include "et131x_adapter.h"
93 * et131x_stats - Return the current device statistics.
94 * @netdev: device whose stats are being queried
96 * Returns 0 on success, errno on failure (as defined in errno.h)
98 static struct net_device_stats
*et131x_stats(struct net_device
*netdev
)
100 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
101 struct net_device_stats
*stats
= &adapter
->net_stats
;
102 struct ce_stats
*devstat
= &adapter
->stats
;
104 stats
->rx_errors
= devstat
->rx_length_errs
+
105 devstat
->rx_align_errs
+
106 devstat
->rx_crc_errs
+
107 devstat
->rx_code_violations
+
108 devstat
->rx_other_errs
;
109 stats
->tx_errors
= devstat
->tx_max_pkt_errs
;
110 stats
->multicast
= devstat
->multicast_pkts_rcvd
;
111 stats
->collisions
= devstat
->tx_collisions
;
113 stats
->rx_length_errors
= devstat
->rx_length_errs
;
114 stats
->rx_over_errors
= devstat
->rx_overflows
;
115 stats
->rx_crc_errors
= devstat
->rx_crc_errs
;
117 /* NOTE: These stats don't have corresponding values in CE_STATS,
118 * so we're going to have to update these directly from within the
121 /* stats->rx_bytes = 20; devstat->; */
122 /* stats->tx_bytes = 20; devstat->; */
123 /* stats->rx_dropped = devstat->; */
124 /* stats->tx_dropped = devstat->; */
126 /* NOTE: Not used, can't find analogous statistics */
127 /* stats->rx_frame_errors = devstat->; */
128 /* stats->rx_fifo_errors = devstat->; */
129 /* stats->rx_missed_errors = devstat->; */
131 /* stats->tx_aborted_errors = devstat->; */
132 /* stats->tx_carrier_errors = devstat->; */
133 /* stats->tx_fifo_errors = devstat->; */
134 /* stats->tx_heartbeat_errors = devstat->; */
135 /* stats->tx_window_errors = devstat->; */
140 * et131x_open - Open the device for use.
141 * @netdev: device to be opened
143 * Returns 0 on success, errno on failure (as defined in errno.h)
145 int et131x_open(struct net_device
*netdev
)
148 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
150 /* Start the timer to track NIC errors */
151 add_timer(&adapter
->error_timer
);
153 /* Register our IRQ */
154 result
= request_irq(netdev
->irq
, et131x_isr
, IRQF_SHARED
,
155 netdev
->name
, netdev
);
157 dev_err(&adapter
->pdev
->dev
, "could not register IRQ %d\n",
162 /* Enable the Tx and Rx DMA engines (if not already enabled) */
163 et131x_rx_dma_enable(adapter
);
164 et131x_tx_dma_enable(adapter
);
166 /* Enable device interrupts */
167 et131x_enable_interrupts(adapter
);
169 adapter
->flags
|= fMP_ADAPTER_INTERRUPT_IN_USE
;
171 phy_start(adapter
->phydev
);
173 /* We're ready to move some data, so start the queue */
174 netif_start_queue(netdev
);
179 * et131x_close - Close the device
180 * @netdev: device to be closed
182 * Returns 0 on success, errno on failure (as defined in errno.h)
184 int et131x_close(struct net_device
*netdev
)
186 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
188 /* Save the timestamp for the TX watchdog, prevent a timeout */
189 netdev
->trans_start
= jiffies
;
191 /* First thing is to stop the queue */
192 netif_stop_queue(netdev
);
194 /* Stop the Tx and Rx DMA engines */
195 et131x_rx_dma_disable(adapter
);
196 et131x_tx_dma_disable(adapter
);
198 /* Disable device interrupts */
199 et131x_disable_interrupts(adapter
);
201 /* Deregistering ISR */
202 adapter
->flags
&= ~fMP_ADAPTER_INTERRUPT_IN_USE
;
203 free_irq(netdev
->irq
, netdev
);
205 /* Stop the error timer */
206 del_timer_sync(&adapter
->error_timer
);
211 * et131x_ioctl - The I/O Control handler for the driver
212 * @netdev: device on which the control request is being made
213 * @reqbuf: a pointer to the IOCTL request buffer
214 * @cmd: the IOCTL command code
216 * Returns 0 on success, errno on failure (as defined in errno.h)
218 int et131x_ioctl(struct net_device
*netdev
, struct ifreq
*reqbuf
, int cmd
)
220 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
222 if (!adapter
->phydev
)
225 return phy_mii_ioctl(adapter
->phydev
, reqbuf
, cmd
);
229 * et131x_set_packet_filter - Configures the Rx Packet filtering on the device
230 * @adapter: pointer to our private adapter structure
232 * FIXME: lot of dups with MAC code
234 * Returns 0 on success, errno on failure
236 int et131x_set_packet_filter(struct et131x_adapter
*adapter
)
239 uint32_t filter
= adapter
->packet_filter
;
243 ctrl
= readl(&adapter
->regs
->rxmac
.ctrl
);
244 pf_ctrl
= readl(&adapter
->regs
->rxmac
.pf_ctrl
);
246 /* Default to disabled packet filtering. Enable it in the individual
247 * case statements that require the device to filter something
251 /* Set us to be in promiscuous mode so we receive everything, this
252 * is also true when we get a packet filter of 0
254 if ((filter
& ET131X_PACKET_TYPE_PROMISCUOUS
) || filter
== 0)
255 pf_ctrl
&= ~7; /* Clear filter bits */
258 * Set us up with Multicast packet filtering. Three cases are
259 * possible - (1) we have a multi-cast list, (2) we receive ALL
260 * multicast entries or (3) we receive none.
262 if (filter
& ET131X_PACKET_TYPE_ALL_MULTICAST
)
263 pf_ctrl
&= ~2; /* Multicast filter bit */
265 et1310_setup_device_for_multicast(adapter
);
270 /* Set us up with Unicast packet filtering */
271 if (filter
& ET131X_PACKET_TYPE_DIRECTED
) {
272 et1310_setup_device_for_unicast(adapter
);
277 /* Set us up with Broadcast packet filtering */
278 if (filter
& ET131X_PACKET_TYPE_BROADCAST
) {
279 pf_ctrl
|= 1; /* Broadcast filter bit */
284 /* Setup the receive mac configuration registers - Packet
285 * Filter control + the enable / disable for packet filter
286 * in the control reg.
288 writel(pf_ctrl
, &adapter
->regs
->rxmac
.pf_ctrl
);
289 writel(ctrl
, &adapter
->regs
->rxmac
.ctrl
);
295 * et131x_multicast - The handler to configure multicasting on the interface
296 * @netdev: a pointer to a net_device struct representing the device
298 void et131x_multicast(struct net_device
*netdev
)
300 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
301 uint32_t packet_filter
= 0;
303 struct netdev_hw_addr
*ha
;
306 spin_lock_irqsave(&adapter
->lock
, flags
);
308 /* Before we modify the platform-independent filter flags, store them
309 * locally. This allows us to determine if anything's changed and if
310 * we even need to bother the hardware
312 packet_filter
= adapter
->packet_filter
;
314 /* Clear the 'multicast' flag locally; because we only have a single
315 * flag to check multicast, and multiple multicast addresses can be
316 * set, this is the easiest way to determine if more than one
317 * multicast address is being set.
319 packet_filter
&= ~ET131X_PACKET_TYPE_MULTICAST
;
321 /* Check the net_device flags and set the device independent flags
325 if (netdev
->flags
& IFF_PROMISC
)
326 adapter
->packet_filter
|= ET131X_PACKET_TYPE_PROMISCUOUS
;
328 adapter
->packet_filter
&= ~ET131X_PACKET_TYPE_PROMISCUOUS
;
330 if (netdev
->flags
& IFF_ALLMULTI
)
331 adapter
->packet_filter
|= ET131X_PACKET_TYPE_ALL_MULTICAST
;
333 if (netdev_mc_count(netdev
) > NIC_MAX_MCAST_LIST
)
334 adapter
->packet_filter
|= ET131X_PACKET_TYPE_ALL_MULTICAST
;
336 if (netdev_mc_count(netdev
) < 1) {
337 adapter
->packet_filter
&= ~ET131X_PACKET_TYPE_ALL_MULTICAST
;
338 adapter
->packet_filter
&= ~ET131X_PACKET_TYPE_MULTICAST
;
340 adapter
->packet_filter
|= ET131X_PACKET_TYPE_MULTICAST
;
342 /* Set values in the private adapter struct */
344 netdev_for_each_mc_addr(ha
, netdev
) {
345 if (i
== NIC_MAX_MCAST_LIST
)
347 memcpy(adapter
->multicast_list
[i
++], ha
->addr
, ETH_ALEN
);
349 adapter
->multicast_addr_count
= i
;
351 /* Are the new flags different from the previous ones? If not, then no
354 * NOTE - This block will always update the multicast_list with the
355 * hardware, even if the addresses aren't the same.
357 if (packet_filter
!= adapter
->packet_filter
) {
358 /* Call the device's filter function */
359 et131x_set_packet_filter(adapter
);
361 spin_unlock_irqrestore(&adapter
->lock
, flags
);
365 * et131x_tx - The handler to tx a packet on the device
366 * @skb: data to be Tx'd
367 * @netdev: device on which data is to be Tx'd
369 * Returns 0 on success, errno on failure (as defined in errno.h)
371 int et131x_tx(struct sk_buff
*skb
, struct net_device
*netdev
)
375 /* Save the timestamp for the TX timeout watchdog */
376 netdev
->trans_start
= jiffies
;
378 /* Call the device-specific data Tx routine */
379 status
= et131x_send_packets(skb
, netdev
);
381 /* Check status and manage the netif queue if necessary */
383 if (status
== -ENOMEM
) {
384 /* Put the queue to sleep until resources are
387 netif_stop_queue(netdev
);
388 status
= NETDEV_TX_BUSY
;
390 status
= NETDEV_TX_OK
;
397 * et131x_tx_timeout - Timeout handler
398 * @netdev: a pointer to a net_device struct representing the device
400 * The handler called when a Tx request times out. The timeout period is
401 * specified by the 'tx_timeo" element in the net_device structure (see
402 * et131x_alloc_device() to see how this value is set).
404 void et131x_tx_timeout(struct net_device
*netdev
)
406 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
410 /* If the device is closed, ignore the timeout */
411 if (~(adapter
->flags
& fMP_ADAPTER_INTERRUPT_IN_USE
));
414 /* Any nonrecoverable hardware error?
415 * Checks adapter->flags for any failure in phy reading
417 if (adapter
->flags
& fMP_ADAPTER_NON_RECOVER_ERROR
)
420 /* Hardware failure? */
421 if (adapter
->flags
& fMP_ADAPTER_HARDWARE_ERROR
) {
422 dev_err(&adapter
->pdev
->dev
, "hardware error - reset\n");
427 spin_lock_irqsave(&adapter
->tcb_send_qlock
, flags
);
429 tcb
= adapter
->tx_ring
.send_head
;
434 if (tcb
->count
> NIC_SEND_HANG_THRESHOLD
) {
435 spin_unlock_irqrestore(&adapter
->tcb_send_qlock
,
438 dev_warn(&adapter
->pdev
->dev
,
439 "Send stuck - reset. tcb->WrIndex %x, flags 0x%08x\n",
443 adapter
->net_stats
.tx_errors
++;
446 /* First thing is to stop the queue */
447 netif_stop_queue(netdev
);
449 /* Stop the Tx and Rx DMA engines */
450 et131x_rx_dma_disable(adapter
);
451 et131x_tx_dma_disable(adapter
);
453 /* Disable device interrupts */
454 et131x_disable_interrupts(adapter
);
456 /* Enable the Tx and Rx DMA engines (if not already enabled) */
457 et131x_rx_dma_enable(adapter
);
458 et131x_tx_dma_enable(adapter
);
460 /* Enable device interrupts */
461 et131x_enable_interrupts(adapter
);
463 /* We're ready to move some data, so start the queue */
464 netif_start_queue(netdev
);
469 spin_unlock_irqrestore(&adapter
->tcb_send_qlock
, flags
);
473 * et131x_change_mtu - The handler called to change the MTU for the device
474 * @netdev: device whose MTU is to be changed
475 * @new_mtu: the desired MTU
477 * Returns 0 on success, errno on failure (as defined in errno.h)
479 int et131x_change_mtu(struct net_device
*netdev
, int new_mtu
)
482 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
484 /* Make sure the requested MTU is valid */
485 if (new_mtu
< 64 || new_mtu
> 9216)
488 /* Stop the netif queue */
489 netif_stop_queue(netdev
);
491 /* Stop the Tx and Rx DMA engines */
492 et131x_rx_dma_disable(adapter
);
493 et131x_tx_dma_disable(adapter
);
495 /* Disable device interrupts */
496 et131x_disable_interrupts(adapter
);
497 et131x_handle_send_interrupt(adapter
);
498 et131x_handle_recv_interrupt(adapter
);
500 /* Set the new MTU */
501 netdev
->mtu
= new_mtu
;
503 /* Free Rx DMA memory */
504 et131x_adapter_memory_free(adapter
);
506 /* Set the config parameter for Jumbo Packet support */
507 adapter
->registry_jumbo_packet
= new_mtu
+ 14;
508 et131x_soft_reset(adapter
);
510 /* Alloc and init Rx DMA memory */
511 result
= et131x_adapter_memory_alloc(adapter
);
513 dev_warn(&adapter
->pdev
->dev
,
514 "Change MTU failed; couldn't re-alloc DMA memory\n");
518 et131x_init_send(adapter
);
520 et131x_hwaddr_init(adapter
);
521 memcpy(netdev
->dev_addr
, adapter
->addr
, ETH_ALEN
);
523 /* Init the device with the new settings */
524 et131x_adapter_setup(adapter
);
526 /* Enable interrupts */
527 if (adapter
->flags
& fMP_ADAPTER_INTERRUPT_IN_USE
)
528 et131x_enable_interrupts(adapter
);
530 /* Restart the Tx and Rx DMA engines */
531 et131x_rx_dma_enable(adapter
);
532 et131x_tx_dma_enable(adapter
);
534 /* Restart the netif queue */
535 netif_wake_queue(netdev
);
540 * et131x_set_mac_addr - handler to change the MAC address for the device
541 * @netdev: device whose MAC is to be changed
542 * @new_mac: the desired MAC address
544 * Returns 0 on success, errno on failure (as defined in errno.h)
546 * IMPLEMENTED BY : blux http://berndlux.de 22.01.2007 21:14
548 int et131x_set_mac_addr(struct net_device
*netdev
, void *new_mac
)
551 struct et131x_adapter
*adapter
= netdev_priv(netdev
);
552 struct sockaddr
*address
= new_mac
;
559 /* Make sure the requested MAC is valid */
560 if (!is_valid_ether_addr(address
->sa_data
))
563 /* Stop the netif queue */
564 netif_stop_queue(netdev
);
566 /* Stop the Tx and Rx DMA engines */
567 et131x_rx_dma_disable(adapter
);
568 et131x_tx_dma_disable(adapter
);
570 /* Disable device interrupts */
571 et131x_disable_interrupts(adapter
);
572 et131x_handle_send_interrupt(adapter
);
573 et131x_handle_recv_interrupt(adapter
);
575 /* Set the new MAC */
576 /* netdev->set_mac_address = &new_mac; */
578 memcpy(netdev
->dev_addr
, address
->sa_data
, netdev
->addr_len
);
580 printk(KERN_INFO
"%s: Setting MAC address to %pM\n",
581 netdev
->name
, netdev
->dev_addr
);
583 /* Free Rx DMA memory */
584 et131x_adapter_memory_free(adapter
);
586 et131x_soft_reset(adapter
);
588 /* Alloc and init Rx DMA memory */
589 result
= et131x_adapter_memory_alloc(adapter
);
591 dev_err(&adapter
->pdev
->dev
,
592 "Change MAC failed; couldn't re-alloc DMA memory\n");
596 et131x_init_send(adapter
);
598 et131x_hwaddr_init(adapter
);
600 /* Init the device with the new settings */
601 et131x_adapter_setup(adapter
);
603 /* Enable interrupts */
604 if (adapter
->flags
& fMP_ADAPTER_INTERRUPT_IN_USE
)
605 et131x_enable_interrupts(adapter
);
607 /* Restart the Tx and Rx DMA engines */
608 et131x_rx_dma_enable(adapter
);
609 et131x_tx_dma_enable(adapter
);
611 /* Restart the netif queue */
612 netif_wake_queue(netdev
);
616 static const struct net_device_ops et131x_netdev_ops
= {
617 .ndo_open
= et131x_open
,
618 .ndo_stop
= et131x_close
,
619 .ndo_start_xmit
= et131x_tx
,
620 .ndo_set_multicast_list
= et131x_multicast
,
621 .ndo_tx_timeout
= et131x_tx_timeout
,
622 .ndo_change_mtu
= et131x_change_mtu
,
623 .ndo_set_mac_address
= et131x_set_mac_addr
,
624 .ndo_validate_addr
= eth_validate_addr
,
625 .ndo_get_stats
= et131x_stats
,
626 .ndo_do_ioctl
= et131x_ioctl
,
630 * et131x_device_alloc
632 * Returns pointer to the allocated and initialized net_device struct for
635 * Create instances of net_device and wl_private for the new adapter and
636 * register the device's entry points in the net_device structure.
638 struct net_device
*et131x_device_alloc(void)
640 struct net_device
*netdev
;
642 /* Alloc net_device and adapter structs */
643 netdev
= alloc_etherdev(sizeof(struct et131x_adapter
));
646 printk(KERN_ERR
"et131x: Alloc of net_device struct failed\n");
651 * Setup the function registration table (and other data) for a
654 netdev
->watchdog_timeo
= ET131X_TX_TIMEOUT
;
655 netdev
->netdev_ops
= &et131x_netdev_ops
;
658 /* netdev->poll = &et131x_poll; */
659 /* netdev->poll_controller = &et131x_poll_controller; */