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/slab.h>
69 #include <linux/ctype.h>
70 #include <linux/string.h>
71 #include <linux/timer.h>
72 #include <linux/interrupt.h>
74 #include <linux/delay.h>
76 #include <linux/bitops.h>
77 #include <linux/pci.h>
78 #include <asm/system.h>
80 #include <linux/mii.h>
81 #include <linux/netdevice.h>
82 #include <linux/etherdevice.h>
83 #include <linux/skbuff.h>
84 #include <linux/if_arp.h>
85 #include <linux/ioport.h>
87 #include "et1310_phy.h"
88 #include "et1310_pm.h"
89 #include "et1310_jagcore.h"
90 #include "et1310_mac.h"
91 #include "et1310_tx.h"
93 #include "et131x_adapter.h"
94 #include "et131x_isr.h"
95 #include "et131x_initpci.h"
97 struct net_device_stats *et131x_stats(struct net_device *netdev);
98 int et131x_open(struct net_device *netdev);
99 int et131x_close(struct net_device *netdev);
100 int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf, int cmd);
101 void et131x_multicast(struct net_device *netdev);
102 int et131x_tx(struct sk_buff *skb, struct net_device *netdev);
103 void et131x_tx_timeout(struct net_device *netdev);
104 int et131x_change_mtu(struct net_device *netdev, int new_mtu);
105 int et131x_set_mac_addr(struct net_device *netdev, void *new_mac);
106 void et131x_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
107 void et131x_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);
108 void et131x_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
110 static const struct net_device_ops et131x_netdev_ops = {
111 .ndo_open = et131x_open,
112 .ndo_stop = et131x_close,
113 .ndo_start_xmit = et131x_tx,
114 .ndo_set_multicast_list = et131x_multicast,
115 .ndo_tx_timeout = et131x_tx_timeout,
116 .ndo_change_mtu = et131x_change_mtu,
117 .ndo_set_mac_address = et131x_set_mac_addr,
118 .ndo_validate_addr = eth_validate_addr,
119 .ndo_get_stats = et131x_stats,
120 .ndo_do_ioctl = et131x_ioctl,
124 * et131x_device_alloc
126 * Returns pointer to the allocated and initialized net_device struct for
129 * Create instances of net_device and wl_private for the new adapter and
130 * register the device's entry points in the net_device structure.
132 struct net_device *et131x_device_alloc(void)
134 struct net_device *netdev;
136 /* Alloc net_device and adapter structs */
137 netdev = alloc_etherdev(sizeof(struct et131x_adapter));
139 if (netdev == NULL) {
140 printk(KERN_ERR "et131x: Alloc of net_device struct failed\n");
144 /* Setup the function registration table (and other data) for a
147 /* netdev->init = &et131x_init; */
148 /* netdev->set_config = &et131x_config; */
149 netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
150 netdev->netdev_ops = &et131x_netdev_ops;
152 /* netdev->ethtool_ops = &et131x_ethtool_ops; */
155 /* netdev->poll = &et131x_poll; */
156 /* netdev->poll_controller = &et131x_poll_controller; */
161 * et131x_stats - Return the current device statistics.
162 * @netdev: device whose stats are being queried
164 * Returns 0 on success, errno on failure (as defined in errno.h)
166 struct net_device_stats *et131x_stats(struct net_device *netdev)
168 struct et131x_adapter *adapter = netdev_priv(netdev);
169 struct net_device_stats *stats = &adapter->net_stats;
170 CE_STATS_t *devstat = &adapter->Stats;
172 stats->rx_packets = devstat->ipackets;
173 stats->tx_packets = devstat->opackets;
174 stats->rx_errors = devstat->length_err + devstat->alignment_err +
175 devstat->crc_err + devstat->code_violations + devstat->other_errors;
176 stats->tx_errors = devstat->max_pkt_error;
177 stats->multicast = devstat->multircv;
178 stats->collisions = devstat->collisions;
180 stats->rx_length_errors = devstat->length_err;
181 stats->rx_over_errors = devstat->rx_ov_flow;
182 stats->rx_crc_errors = devstat->crc_err;
184 /* NOTE: These stats don't have corresponding values in CE_STATS,
185 * so we're going to have to update these directly from within the
188 /* stats->rx_bytes = 20; devstat->; */
189 /* stats->tx_bytes = 20; devstat->; */
190 /* stats->rx_dropped = devstat->; */
191 /* stats->tx_dropped = devstat->; */
193 /* NOTE: Not used, can't find analogous statistics */
194 /* stats->rx_frame_errors = devstat->; */
195 /* stats->rx_fifo_errors = devstat->; */
196 /* stats->rx_missed_errors = devstat->; */
198 /* stats->tx_aborted_errors = devstat->; */
199 /* stats->tx_carrier_errors = devstat->; */
200 /* stats->tx_fifo_errors = devstat->; */
201 /* stats->tx_heartbeat_errors = devstat->; */
202 /* stats->tx_window_errors = devstat->; */
207 * et131x_open - Open the device for use.
208 * @netdev: device to be opened
210 * Returns 0 on success, errno on failure (as defined in errno.h)
212 int et131x_open(struct net_device *netdev)
215 struct et131x_adapter *adapter = netdev_priv(netdev);
217 /* Start the timer to track NIC errors */
218 add_timer(&adapter->ErrorTimer);
220 /* Register our IRQ */
221 result = request_irq(netdev->irq, et131x_isr, IRQF_SHARED,
222 netdev->name, netdev);
224 dev_err(&adapter->pdev->dev, "c ould not register IRQ %d\n",
229 /* Enable the Tx and Rx DMA engines (if not already enabled) */
230 et131x_rx_dma_enable(adapter);
231 et131x_tx_dma_enable(adapter);
233 /* Enable device interrupts */
234 et131x_enable_interrupts(adapter);
236 adapter->Flags |= fMP_ADAPTER_INTERRUPT_IN_USE;
238 /* We're ready to move some data, so start the queue */
239 netif_start_queue(netdev);
244 * et131x_close - Close the device
245 * @netdev: device to be closed
247 * Returns 0 on success, errno on failure (as defined in errno.h)
249 int et131x_close(struct net_device *netdev)
251 struct et131x_adapter *adapter = netdev_priv(netdev);
253 /* First thing is to stop the queue */
254 netif_stop_queue(netdev);
256 /* Stop the Tx and Rx DMA engines */
257 et131x_rx_dma_disable(adapter);
258 et131x_tx_dma_disable(adapter);
260 /* Disable device interrupts */
261 et131x_disable_interrupts(adapter);
263 /* Deregistering ISR */
264 adapter->Flags &= ~fMP_ADAPTER_INTERRUPT_IN_USE;
265 free_irq(netdev->irq, netdev);
267 /* Stop the error timer */
268 del_timer_sync(&adapter->ErrorTimer);
273 * et131x_ioctl_mii - The function which handles MII IOCTLs
274 * @netdev: device on which the query is being made
275 * @reqbuf: the request-specific data buffer
276 * @cmd: the command request code
278 * Returns 0 on success, errno on failure (as defined in errno.h)
280 int et131x_ioctl_mii(struct net_device *netdev, struct ifreq *reqbuf, int cmd)
283 struct et131x_adapter *etdev = netdev_priv(netdev);
284 struct mii_ioctl_data *data = if_mii(reqbuf);
288 data->phy_id = etdev->Stats.xcvr_addr;
292 if (!capable(CAP_NET_ADMIN))
295 status = MiRead(etdev,
296 data->reg_num, &data->val_out);
300 if (!capable(CAP_NET_ADMIN))
303 status = MiWrite(etdev, data->reg_num,
308 status = -EOPNOTSUPP;
314 * et131x_ioctl - The I/O Control handler for the driver
315 * @netdev: device on which the control request is being made
316 * @reqbuf: a pointer to the IOCTL request buffer
317 * @cmd: the IOCTL command code
319 * Returns 0 on success, errno on failure (as defined in errno.h)
321 int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf, int cmd)
329 status = et131x_ioctl_mii(netdev, reqbuf, cmd);
333 status = -EOPNOTSUPP;
339 * et131x_set_packet_filter - Configures the Rx Packet filtering on the device
340 * @adapter: pointer to our private adapter structure
342 * Returns 0 on success, errno on failure
344 int et131x_set_packet_filter(struct et131x_adapter *adapter)
347 uint32_t filter = adapter->PacketFilter;
349 RXMAC_PF_CTRL_t pf_ctrl;
351 ctrl.value = readl(&adapter->regs->rxmac.ctrl.value);
352 pf_ctrl.value = readl(&adapter->regs->rxmac.pf_ctrl.value);
354 /* Default to disabled packet filtering. Enable it in the individual
355 * case statements that require the device to filter something
357 ctrl.bits.pkt_filter_disable = 1;
359 /* Set us to be in promiscuous mode so we receive everything, this
360 * is also true when we get a packet filter of 0
362 if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0) {
363 pf_ctrl.bits.filter_broad_en = 0;
364 pf_ctrl.bits.filter_multi_en = 0;
365 pf_ctrl.bits.filter_uni_en = 0;
368 * Set us up with Multicast packet filtering. Three cases are
369 * possible - (1) we have a multi-cast list, (2) we receive ALL
370 * multicast entries or (3) we receive none.
372 if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST) {
373 pf_ctrl.bits.filter_multi_en = 0;
375 SetupDeviceForMulticast(adapter);
376 pf_ctrl.bits.filter_multi_en = 1;
377 ctrl.bits.pkt_filter_disable = 0;
380 /* Set us up with Unicast packet filtering */
381 if (filter & ET131X_PACKET_TYPE_DIRECTED) {
382 SetupDeviceForUnicast(adapter);
383 pf_ctrl.bits.filter_uni_en = 1;
384 ctrl.bits.pkt_filter_disable = 0;
387 /* Set us up with Broadcast packet filtering */
388 if (filter & ET131X_PACKET_TYPE_BROADCAST) {
389 pf_ctrl.bits.filter_broad_en = 1;
390 ctrl.bits.pkt_filter_disable = 0;
392 pf_ctrl.bits.filter_broad_en = 0;
395 /* Setup the receive mac configuration registers - Packet
396 * Filter control + the enable / disable for packet filter
397 * in the control reg.
399 writel(pf_ctrl.value,
400 &adapter->regs->rxmac.pf_ctrl.value);
401 writel(ctrl.value, &adapter->regs->rxmac.ctrl.value);
407 * et131x_multicast - The handler to configure multicasting on the interface
408 * @netdev: a pointer to a net_device struct representing the device
410 void et131x_multicast(struct net_device *netdev)
412 struct et131x_adapter *adapter = netdev_priv(netdev);
413 uint32_t PacketFilter = 0;
415 struct dev_mc_list *mclist;
418 spin_lock_irqsave(&adapter->Lock, flags);
420 /* Before we modify the platform-independent filter flags, store them
421 * locally. This allows us to determine if anything's changed and if
422 * we even need to bother the hardware
424 PacketFilter = adapter->PacketFilter;
426 /* Clear the 'multicast' flag locally; becuase we only have a single
427 * flag to check multicast, and multiple multicast addresses can be
428 * set, this is the easiest way to determine if more than one
429 * multicast address is being set.
431 PacketFilter &= ~ET131X_PACKET_TYPE_MULTICAST;
433 /* Check the net_device flags and set the device independent flags
437 if (netdev->flags & IFF_PROMISC) {
438 adapter->PacketFilter |= ET131X_PACKET_TYPE_PROMISCUOUS;
440 adapter->PacketFilter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
443 if (netdev->flags & IFF_ALLMULTI) {
444 adapter->PacketFilter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
447 if (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST) {
448 adapter->PacketFilter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
451 if (netdev_mc_count(netdev) < 1) {
452 adapter->PacketFilter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
453 adapter->PacketFilter &= ~ET131X_PACKET_TYPE_MULTICAST;
455 adapter->PacketFilter |= ET131X_PACKET_TYPE_MULTICAST;
458 /* Set values in the private adapter struct */
460 netdev_for_each_mc_addr(mclist, netdev) {
461 if (i == NIC_MAX_MCAST_LIST)
463 memcpy(adapter->MCList[i++], mclist->dmi_addr, ETH_ALEN);
465 adapter->MCAddressCount = i;
467 /* Are the new flags different from the previous ones? If not, then no
470 * NOTE - This block will always update the MCList with the hardware,
471 * even if the addresses aren't the same.
473 if (PacketFilter != adapter->PacketFilter) {
474 /* Call the device's filter function */
475 et131x_set_packet_filter(adapter);
477 spin_unlock_irqrestore(&adapter->Lock, flags);
481 * et131x_tx - The handler to tx a packet on the device
482 * @skb: data to be Tx'd
483 * @netdev: device on which data is to be Tx'd
485 * Returns 0 on success, errno on failure (as defined in errno.h)
487 int et131x_tx(struct sk_buff *skb, struct net_device *netdev)
491 /* Save the timestamp for the TX timeout watchdog */
492 netdev->trans_start = jiffies;
494 /* Call the device-specific data Tx routine */
495 status = et131x_send_packets(skb, netdev);
497 /* Check status and manage the netif queue if necessary */
499 if (status == -ENOMEM) {
500 /* Put the queue to sleep until resources are
503 netif_stop_queue(netdev);
504 status = NETDEV_TX_BUSY;
506 status = NETDEV_TX_OK;
513 * et131x_tx_timeout - Timeout handler
514 * @netdev: a pointer to a net_device struct representing the device
516 * The handler called when a Tx request times out. The timeout period is
517 * specified by the 'tx_timeo" element in the net_device structure (see
518 * et131x_alloc_device() to see how this value is set).
520 void et131x_tx_timeout(struct net_device *netdev)
522 struct et131x_adapter *etdev = netdev_priv(netdev);
526 /* Just skip this part if the adapter is doing link detection */
527 if (etdev->Flags & fMP_ADAPTER_LINK_DETECTION)
530 /* Any nonrecoverable hardware error?
531 * Checks adapter->flags for any failure in phy reading
533 if (etdev->Flags & fMP_ADAPTER_NON_RECOVER_ERROR)
536 /* Hardware failure? */
537 if (etdev->Flags & fMP_ADAPTER_HARDWARE_ERROR) {
538 dev_err(&etdev->pdev->dev, "hardware error - reset\n");
543 spin_lock_irqsave(&etdev->TCBSendQLock, flags);
545 tcb = etdev->tx_ring.send_head;
550 if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
551 spin_unlock_irqrestore(&etdev->TCBSendQLock,
554 dev_warn(&etdev->pdev->dev,
555 "Send stuck - reset. tcb->WrIndex %x, Flags 0x%08x\n",
559 et131x_close(netdev);
566 spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
570 * et131x_change_mtu - The handler called to change the MTU for the device
571 * @netdev: device whose MTU is to be changed
572 * @new_mtu: the desired MTU
574 * Returns 0 on success, errno on failure (as defined in errno.h)
576 int et131x_change_mtu(struct net_device *netdev, int new_mtu)
579 struct et131x_adapter *adapter = netdev_priv(netdev);
581 /* Make sure the requested MTU is valid */
582 if (new_mtu < 64 || new_mtu > 9216)
585 /* Stop the netif queue */
586 netif_stop_queue(netdev);
588 /* Stop the Tx and Rx DMA engines */
589 et131x_rx_dma_disable(adapter);
590 et131x_tx_dma_disable(adapter);
592 /* Disable device interrupts */
593 et131x_disable_interrupts(adapter);
594 et131x_handle_send_interrupt(adapter);
595 et131x_handle_recv_interrupt(adapter);
597 /* Set the new MTU */
598 netdev->mtu = new_mtu;
600 /* Free Rx DMA memory */
601 et131x_adapter_memory_free(adapter);
603 /* Set the config parameter for Jumbo Packet support */
604 adapter->RegistryJumboPacket = new_mtu + 14;
605 et131x_soft_reset(adapter);
607 /* Alloc and init Rx DMA memory */
608 result = et131x_adapter_memory_alloc(adapter);
610 dev_warn(&adapter->pdev->dev,
611 "Change MTU failed; couldn't re-alloc DMA memory\n");
615 et131x_init_send(adapter);
617 et131x_hwaddr_init(adapter);
618 memcpy(netdev->dev_addr, adapter->CurrentAddress, ETH_ALEN);
620 /* Init the device with the new settings */
621 et131x_adapter_setup(adapter);
623 /* Enable interrupts */
624 if (adapter->Flags & fMP_ADAPTER_INTERRUPT_IN_USE)
625 et131x_enable_interrupts(adapter);
627 /* Restart the Tx and Rx DMA engines */
628 et131x_rx_dma_enable(adapter);
629 et131x_tx_dma_enable(adapter);
631 /* Restart the netif queue */
632 netif_wake_queue(netdev);
637 * et131x_set_mac_addr - handler to change the MAC address for the device
638 * @netdev: device whose MAC is to be changed
639 * @new_mac: the desired MAC address
641 * Returns 0 on success, errno on failure (as defined in errno.h)
643 * IMPLEMENTED BY : blux http://berndlux.de 22.01.2007 21:14
645 int et131x_set_mac_addr(struct net_device *netdev, void *new_mac)
648 struct et131x_adapter *adapter = netdev_priv(netdev);
649 struct sockaddr *address = new_mac;
656 /* Make sure the requested MAC is valid */
657 if (!is_valid_ether_addr(address->sa_data))
660 /* Stop the netif queue */
661 netif_stop_queue(netdev);
663 /* Stop the Tx and Rx DMA engines */
664 et131x_rx_dma_disable(adapter);
665 et131x_tx_dma_disable(adapter);
667 /* Disable device interrupts */
668 et131x_disable_interrupts(adapter);
669 et131x_handle_send_interrupt(adapter);
670 et131x_handle_recv_interrupt(adapter);
672 /* Set the new MAC */
673 /* netdev->set_mac_address = &new_mac; */
674 /* netdev->mtu = new_mtu; */
676 memcpy(netdev->dev_addr, address->sa_data, netdev->addr_len);
679 "%s: Setting MAC address to %02x:%02x:%02x:%02x:%02x:%02x\n",
681 netdev->dev_addr[0], netdev->dev_addr[1],
682 netdev->dev_addr[2], netdev->dev_addr[3],
683 netdev->dev_addr[4], netdev->dev_addr[5]);
685 /* Free Rx DMA memory */
686 et131x_adapter_memory_free(adapter);
688 /* Set the config parameter for Jumbo Packet support */
689 /* adapter->RegistryJumboPacket = new_mtu + 14; */
690 /* blux: not needet here, we'll change the MAC */
692 et131x_soft_reset(adapter);
694 /* Alloc and init Rx DMA memory */
695 result = et131x_adapter_memory_alloc(adapter);
697 dev_err(&adapter->pdev->dev,
698 "Change MAC failed; couldn't re-alloc DMA memory\n");
702 et131x_init_send(adapter);
704 et131x_hwaddr_init(adapter);
706 /* Init the device with the new settings */
707 et131x_adapter_setup(adapter);
709 /* Enable interrupts */
710 if (adapter->Flags & fMP_ADAPTER_INTERRUPT_IN_USE)
711 et131x_enable_interrupts(adapter);
713 /* Restart the Tx and Rx DMA engines */
714 et131x_rx_dma_enable(adapter);
715 et131x_tx_dma_enable(adapter);
717 /* Restart the netif queue */
718 netif_wake_queue(netdev);