3 * 10/100/1000 Base-T Ethernet Driver for the ET1301 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>
86 #include "et1310_phy.h"
87 #include "et1310_tx.h"
88 #include "et131x_adapter.h"
92 * et131x_stats - Return the current device statistics.
93 * @netdev: device whose stats are being queried
95 * Returns 0 on success, errno on failure (as defined in errno.h)
97 static struct net_device_stats *et131x_stats(struct net_device *netdev)
99 struct et131x_adapter *adapter = netdev_priv(netdev);
100 struct net_device_stats *stats = &adapter->net_stats;
101 struct ce_stats *devstat = &adapter->stats;
103 stats->rx_errors = devstat->rx_length_errs +
104 devstat->rx_align_errs +
105 devstat->rx_crc_errs +
106 devstat->rx_code_violations +
107 devstat->rx_other_errs;
108 stats->tx_errors = devstat->tx_max_pkt_errs;
109 stats->multicast = devstat->multicast_pkts_rcvd;
110 stats->collisions = devstat->tx_collisions;
112 stats->rx_length_errors = devstat->rx_length_errs;
113 stats->rx_over_errors = devstat->rx_overflows;
114 stats->rx_crc_errors = devstat->rx_crc_errs;
116 /* NOTE: These stats don't have corresponding values in CE_STATS,
117 * so we're going to have to update these directly from within the
120 /* stats->rx_bytes = 20; devstat->; */
121 /* stats->tx_bytes = 20; devstat->; */
122 /* stats->rx_dropped = devstat->; */
123 /* stats->tx_dropped = devstat->; */
125 /* NOTE: Not used, can't find analogous statistics */
126 /* stats->rx_frame_errors = devstat->; */
127 /* stats->rx_fifo_errors = devstat->; */
128 /* stats->rx_missed_errors = devstat->; */
130 /* stats->tx_aborted_errors = devstat->; */
131 /* stats->tx_carrier_errors = devstat->; */
132 /* stats->tx_fifo_errors = devstat->; */
133 /* stats->tx_heartbeat_errors = devstat->; */
134 /* stats->tx_window_errors = devstat->; */
139 * et131x_open - Open the device for use.
140 * @netdev: device to be opened
142 * Returns 0 on success, errno on failure (as defined in errno.h)
144 int et131x_open(struct net_device *netdev)
147 struct et131x_adapter *adapter = netdev_priv(netdev);
149 /* Start the timer to track NIC errors */
150 add_timer(&adapter->error_timer);
152 /* Register our IRQ */
153 result = request_irq(netdev->irq, et131x_isr, IRQF_SHARED,
154 netdev->name, netdev);
156 dev_err(&adapter->pdev->dev, "c ould not register IRQ %d\n",
161 /* Enable the Tx and Rx DMA engines (if not already enabled) */
162 et131x_rx_dma_enable(adapter);
163 et131x_tx_dma_enable(adapter);
165 /* Enable device interrupts */
166 et131x_enable_interrupts(adapter);
168 adapter->flags |= fMP_ADAPTER_INTERRUPT_IN_USE;
170 /* We're ready to move some data, so start the queue */
171 netif_start_queue(netdev);
176 * et131x_close - Close the device
177 * @netdev: device to be closed
179 * Returns 0 on success, errno on failure (as defined in errno.h)
181 int et131x_close(struct net_device *netdev)
183 struct et131x_adapter *adapter = netdev_priv(netdev);
185 /* First thing is to stop the queue */
186 netif_stop_queue(netdev);
188 /* Stop the Tx and Rx DMA engines */
189 et131x_rx_dma_disable(adapter);
190 et131x_tx_dma_disable(adapter);
192 /* Disable device interrupts */
193 et131x_disable_interrupts(adapter);
195 /* Deregistering ISR */
196 adapter->flags &= ~fMP_ADAPTER_INTERRUPT_IN_USE;
197 free_irq(netdev->irq, netdev);
199 /* Stop the error timer */
200 del_timer_sync(&adapter->error_timer);
205 * et131x_ioctl_mii - The function which handles MII IOCTLs
206 * @netdev: device on which the query is being made
207 * @reqbuf: the request-specific data buffer
208 * @cmd: the command request code
210 * Returns 0 on success, errno on failure (as defined in errno.h)
212 int et131x_ioctl_mii(struct net_device *netdev, struct ifreq *reqbuf, int cmd)
215 struct et131x_adapter *adapter = netdev_priv(netdev);
216 struct mii_ioctl_data *data = if_mii(reqbuf);
220 data->phy_id = adapter->stats.xcvr_addr;
224 if (!capable(CAP_NET_ADMIN))
227 status = et131x_mii_read(adapter,
228 data->reg_num, &data->val_out);
232 if (!capable(CAP_NET_ADMIN))
235 status = et131x_mii_write(adapter, data->reg_num,
240 status = -EOPNOTSUPP;
246 * et131x_ioctl - The I/O Control handler for the driver
247 * @netdev: device on which the control request is being made
248 * @reqbuf: a pointer to the IOCTL request buffer
249 * @cmd: the IOCTL command code
251 * Returns 0 on success, errno on failure (as defined in errno.h)
253 int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf, int cmd)
261 status = et131x_ioctl_mii(netdev, reqbuf, cmd);
265 status = -EOPNOTSUPP;
271 * et131x_set_packet_filter - Configures the Rx Packet filtering on the device
272 * @adapter: pointer to our private adapter structure
274 * FIXME: lot of dups with MAC code
276 * Returns 0 on success, errno on failure
278 int et131x_set_packet_filter(struct et131x_adapter *adapter)
281 uint32_t filter = adapter->packet_filter;
285 ctrl = readl(&adapter->regs->rxmac.ctrl);
286 pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
288 /* Default to disabled packet filtering. Enable it in the individual
289 * case statements that require the device to filter something
293 /* Set us to be in promiscuous mode so we receive everything, this
294 * is also true when we get a packet filter of 0
296 if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
297 pf_ctrl &= ~7; /* Clear filter bits */
300 * Set us up with Multicast packet filtering. Three cases are
301 * possible - (1) we have a multi-cast list, (2) we receive ALL
302 * multicast entries or (3) we receive none.
304 if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
305 pf_ctrl &= ~2; /* Multicast filter bit */
307 et1310_setup_device_for_multicast(adapter);
312 /* Set us up with Unicast packet filtering */
313 if (filter & ET131X_PACKET_TYPE_DIRECTED) {
314 et1310_setup_device_for_unicast(adapter);
319 /* Set us up with Broadcast packet filtering */
320 if (filter & ET131X_PACKET_TYPE_BROADCAST) {
321 pf_ctrl |= 1; /* Broadcast filter bit */
326 /* Setup the receive mac configuration registers - Packet
327 * Filter control + the enable / disable for packet filter
328 * in the control reg.
330 writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
331 writel(ctrl, &adapter->regs->rxmac.ctrl);
337 * et131x_multicast - The handler to configure multicasting on the interface
338 * @netdev: a pointer to a net_device struct representing the device
340 void et131x_multicast(struct net_device *netdev)
342 struct et131x_adapter *adapter = netdev_priv(netdev);
343 uint32_t packet_filter = 0;
345 struct netdev_hw_addr *ha;
348 spin_lock_irqsave(&adapter->lock, flags);
350 /* Before we modify the platform-independent filter flags, store them
351 * locally. This allows us to determine if anything's changed and if
352 * we even need to bother the hardware
354 packet_filter = adapter->packet_filter;
356 /* Clear the 'multicast' flag locally; because we only have a single
357 * flag to check multicast, and multiple multicast addresses can be
358 * set, this is the easiest way to determine if more than one
359 * multicast address is being set.
361 packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
363 /* Check the net_device flags and set the device independent flags
367 if (netdev->flags & IFF_PROMISC)
368 adapter->packet_filter |= ET131X_PACKET_TYPE_PROMISCUOUS;
370 adapter->packet_filter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
372 if (netdev->flags & IFF_ALLMULTI)
373 adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
375 if (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST)
376 adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
378 if (netdev_mc_count(netdev) < 1) {
379 adapter->packet_filter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
380 adapter->packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
382 adapter->packet_filter |= ET131X_PACKET_TYPE_MULTICAST;
384 /* Set values in the private adapter struct */
386 netdev_for_each_mc_addr(ha, netdev) {
387 if (i == NIC_MAX_MCAST_LIST)
389 memcpy(adapter->multicast_list[i++], ha->addr, ETH_ALEN);
391 adapter->multicast_addr_count = i;
393 /* Are the new flags different from the previous ones? If not, then no
396 * NOTE - This block will always update the multicast_list with the
397 * hardware, even if the addresses aren't the same.
399 if (packet_filter != adapter->packet_filter) {
400 /* Call the device's filter function */
401 et131x_set_packet_filter(adapter);
403 spin_unlock_irqrestore(&adapter->lock, flags);
407 * et131x_tx - The handler to tx a packet on the device
408 * @skb: data to be Tx'd
409 * @netdev: device on which data is to be Tx'd
411 * Returns 0 on success, errno on failure (as defined in errno.h)
413 int et131x_tx(struct sk_buff *skb, struct net_device *netdev)
417 /* Save the timestamp for the TX timeout watchdog */
418 netdev->trans_start = jiffies;
420 /* Call the device-specific data Tx routine */
421 status = et131x_send_packets(skb, netdev);
423 /* Check status and manage the netif queue if necessary */
425 if (status == -ENOMEM) {
426 /* Put the queue to sleep until resources are
429 netif_stop_queue(netdev);
430 status = NETDEV_TX_BUSY;
432 status = NETDEV_TX_OK;
439 * et131x_tx_timeout - Timeout handler
440 * @netdev: a pointer to a net_device struct representing the device
442 * The handler called when a Tx request times out. The timeout period is
443 * specified by the 'tx_timeo" element in the net_device structure (see
444 * et131x_alloc_device() to see how this value is set).
446 void et131x_tx_timeout(struct net_device *netdev)
448 struct et131x_adapter *adapter = netdev_priv(netdev);
452 /* Any nonrecoverable hardware error?
453 * Checks adapter->flags for any failure in phy reading
455 if (adapter->flags & fMP_ADAPTER_NON_RECOVER_ERROR)
458 /* Hardware failure? */
459 if (adapter->flags & fMP_ADAPTER_HARDWARE_ERROR) {
460 dev_err(&adapter->pdev->dev, "hardware error - reset\n");
465 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
467 tcb = adapter->tx_ring.send_head;
472 if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
473 spin_unlock_irqrestore(&adapter->tcb_send_qlock,
476 dev_warn(&adapter->pdev->dev,
477 "Send stuck - reset. tcb->WrIndex %x, flags 0x%08x\n",
481 et131x_close(netdev);
488 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
492 * et131x_change_mtu - The handler called to change the MTU for the device
493 * @netdev: device whose MTU is to be changed
494 * @new_mtu: the desired MTU
496 * Returns 0 on success, errno on failure (as defined in errno.h)
498 int et131x_change_mtu(struct net_device *netdev, int new_mtu)
501 struct et131x_adapter *adapter = netdev_priv(netdev);
503 /* Make sure the requested MTU is valid */
504 if (new_mtu < 64 || new_mtu > 9216)
507 /* Stop the netif queue */
508 netif_stop_queue(netdev);
510 /* Stop the Tx and Rx DMA engines */
511 et131x_rx_dma_disable(adapter);
512 et131x_tx_dma_disable(adapter);
514 /* Disable device interrupts */
515 et131x_disable_interrupts(adapter);
516 et131x_handle_send_interrupt(adapter);
517 et131x_handle_recv_interrupt(adapter);
519 /* Set the new MTU */
520 netdev->mtu = new_mtu;
522 /* Free Rx DMA memory */
523 et131x_adapter_memory_free(adapter);
525 /* Set the config parameter for Jumbo Packet support */
526 adapter->registry_jumbo_packet = new_mtu + 14;
527 et131x_soft_reset(adapter);
529 /* Alloc and init Rx DMA memory */
530 result = et131x_adapter_memory_alloc(adapter);
532 dev_warn(&adapter->pdev->dev,
533 "Change MTU failed; couldn't re-alloc DMA memory\n");
537 et131x_init_send(adapter);
539 et131x_hwaddr_init(adapter);
540 memcpy(netdev->dev_addr, adapter->addr, ETH_ALEN);
542 /* Init the device with the new settings */
543 et131x_adapter_setup(adapter);
545 /* Enable interrupts */
546 if (adapter->flags & fMP_ADAPTER_INTERRUPT_IN_USE)
547 et131x_enable_interrupts(adapter);
549 /* Restart the Tx and Rx DMA engines */
550 et131x_rx_dma_enable(adapter);
551 et131x_tx_dma_enable(adapter);
553 /* Restart the netif queue */
554 netif_wake_queue(netdev);
559 * et131x_set_mac_addr - handler to change the MAC address for the device
560 * @netdev: device whose MAC is to be changed
561 * @new_mac: the desired MAC address
563 * Returns 0 on success, errno on failure (as defined in errno.h)
565 * IMPLEMENTED BY : blux http://berndlux.de 22.01.2007 21:14
567 int et131x_set_mac_addr(struct net_device *netdev, void *new_mac)
570 struct et131x_adapter *adapter = netdev_priv(netdev);
571 struct sockaddr *address = new_mac;
578 /* Make sure the requested MAC is valid */
579 if (!is_valid_ether_addr(address->sa_data))
582 /* Stop the netif queue */
583 netif_stop_queue(netdev);
585 /* Stop the Tx and Rx DMA engines */
586 et131x_rx_dma_disable(adapter);
587 et131x_tx_dma_disable(adapter);
589 /* Disable device interrupts */
590 et131x_disable_interrupts(adapter);
591 et131x_handle_send_interrupt(adapter);
592 et131x_handle_recv_interrupt(adapter);
594 /* Set the new MAC */
595 /* netdev->set_mac_address = &new_mac; */
597 memcpy(netdev->dev_addr, address->sa_data, netdev->addr_len);
599 printk(KERN_INFO "%s: Setting MAC address to %pM\n",
600 netdev->name, netdev->dev_addr);
602 /* Free Rx DMA memory */
603 et131x_adapter_memory_free(adapter);
605 et131x_soft_reset(adapter);
607 /* Alloc and init Rx DMA memory */
608 result = et131x_adapter_memory_alloc(adapter);
610 dev_err(&adapter->pdev->dev,
611 "Change MAC failed; couldn't re-alloc DMA memory\n");
615 et131x_init_send(adapter);
617 et131x_hwaddr_init(adapter);
619 /* Init the device with the new settings */
620 et131x_adapter_setup(adapter);
622 /* Enable interrupts */
623 if (adapter->flags & fMP_ADAPTER_INTERRUPT_IN_USE)
624 et131x_enable_interrupts(adapter);
626 /* Restart the Tx and Rx DMA engines */
627 et131x_rx_dma_enable(adapter);
628 et131x_tx_dma_enable(adapter);
630 /* Restart the netif queue */
631 netif_wake_queue(netdev);
635 static const struct net_device_ops et131x_netdev_ops = {
636 .ndo_open = et131x_open,
637 .ndo_stop = et131x_close,
638 .ndo_start_xmit = et131x_tx,
639 .ndo_set_multicast_list = et131x_multicast,
640 .ndo_tx_timeout = et131x_tx_timeout,
641 .ndo_change_mtu = et131x_change_mtu,
642 .ndo_set_mac_address = et131x_set_mac_addr,
643 .ndo_validate_addr = eth_validate_addr,
644 .ndo_get_stats = et131x_stats,
645 .ndo_do_ioctl = et131x_ioctl,
649 * et131x_device_alloc
651 * Returns pointer to the allocated and initialized net_device struct for
654 * Create instances of net_device and wl_private for the new adapter and
655 * register the device's entry points in the net_device structure.
657 struct net_device *et131x_device_alloc(void)
659 struct net_device *netdev;
661 /* Alloc net_device and adapter structs */
662 netdev = alloc_etherdev(sizeof(struct et131x_adapter));
664 if (netdev == NULL) {
665 printk(KERN_ERR "et131x: Alloc of net_device struct failed\n");
669 /* Setup the function registration table (and other data) for a
673 netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
674 netdev->netdev_ops = &et131x_netdev_ops;
676 /* netdev->ethtool_ops = &et131x_ethtool_ops; */
679 /* netdev->poll = &et131x_poll; */
680 /* netdev->poll_controller = &et131x_poll_controller; */