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"
91 struct net_device_stats *et131x_stats(struct net_device *netdev);
94 * et131x_stats - Return the current device statistics.
95 * @netdev: device whose stats are being queried
97 * Returns 0 on success, errno on failure (as defined in errno.h)
99 struct net_device_stats *et131x_stats(struct net_device *netdev)
101 struct et131x_adapter *adapter = netdev_priv(netdev);
102 struct net_device_stats *stats = &adapter->net_stats;
103 struct ce_stats *devstat = &adapter->stats;
105 stats->rx_packets = devstat->ipackets;
106 stats->tx_packets = devstat->opackets;
107 stats->rx_errors = devstat->length_err + devstat->alignment_err +
108 devstat->crc_err + devstat->code_violations + devstat->other_errors;
109 stats->tx_errors = devstat->max_pkt_error;
110 stats->multicast = devstat->multircv;
111 stats->collisions = devstat->collisions;
113 stats->rx_length_errors = devstat->length_err;
114 stats->rx_over_errors = devstat->rx_ov_flow;
115 stats->rx_crc_errors = devstat->crc_err;
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->ErrorTimer);
153 /* Register our IRQ */
154 result = request_irq(netdev->irq, et131x_isr, IRQF_SHARED,
155 netdev->name, netdev);
157 dev_err(&adapter->pdev->dev, "c ould 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 /* We're ready to move some data, so start the queue */
172 netif_start_queue(netdev);
177 * et131x_close - Close the device
178 * @netdev: device to be closed
180 * Returns 0 on success, errno on failure (as defined in errno.h)
182 int et131x_close(struct net_device *netdev)
184 struct et131x_adapter *adapter = netdev_priv(netdev);
186 /* First thing is to stop the queue */
187 netif_stop_queue(netdev);
189 /* Stop the Tx and Rx DMA engines */
190 et131x_rx_dma_disable(adapter);
191 et131x_tx_dma_disable(adapter);
193 /* Disable device interrupts */
194 et131x_disable_interrupts(adapter);
196 /* Deregistering ISR */
197 adapter->Flags &= ~fMP_ADAPTER_INTERRUPT_IN_USE;
198 free_irq(netdev->irq, netdev);
200 /* Stop the error timer */
201 del_timer_sync(&adapter->ErrorTimer);
206 * et131x_ioctl_mii - The function which handles MII IOCTLs
207 * @netdev: device on which the query is being made
208 * @reqbuf: the request-specific data buffer
209 * @cmd: the command request code
211 * Returns 0 on success, errno on failure (as defined in errno.h)
213 int et131x_ioctl_mii(struct net_device *netdev, struct ifreq *reqbuf, int cmd)
216 struct et131x_adapter *etdev = netdev_priv(netdev);
217 struct mii_ioctl_data *data = if_mii(reqbuf);
221 data->phy_id = etdev->stats.xcvr_addr;
225 if (!capable(CAP_NET_ADMIN))
228 status = MiRead(etdev,
229 data->reg_num, &data->val_out);
233 if (!capable(CAP_NET_ADMIN))
236 status = MiWrite(etdev, data->reg_num,
241 status = -EOPNOTSUPP;
247 * et131x_ioctl - The I/O Control handler for the driver
248 * @netdev: device on which the control request is being made
249 * @reqbuf: a pointer to the IOCTL request buffer
250 * @cmd: the IOCTL command code
252 * Returns 0 on success, errno on failure (as defined in errno.h)
254 int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf, int cmd)
262 status = et131x_ioctl_mii(netdev, reqbuf, cmd);
266 status = -EOPNOTSUPP;
272 * et131x_set_packet_filter - Configures the Rx Packet filtering on the device
273 * @adapter: pointer to our private adapter structure
275 * FIXME: lot of dups with MAC code
277 * Returns 0 on success, errno on failure
279 int et131x_set_packet_filter(struct et131x_adapter *adapter)
282 uint32_t filter = adapter->PacketFilter;
286 ctrl = readl(&adapter->regs->rxmac.ctrl);
287 pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
289 /* Default to disabled packet filtering. Enable it in the individual
290 * case statements that require the device to filter something
294 /* Set us to be in promiscuous mode so we receive everything, this
295 * is also true when we get a packet filter of 0
297 if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
298 pf_ctrl &= ~7; /* Clear filter bits */
301 * Set us up with Multicast packet filtering. Three cases are
302 * possible - (1) we have a multi-cast list, (2) we receive ALL
303 * multicast entries or (3) we receive none.
305 if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
306 pf_ctrl &= ~2; /* Multicast filter bit */
308 SetupDeviceForMulticast(adapter);
313 /* Set us up with Unicast packet filtering */
314 if (filter & ET131X_PACKET_TYPE_DIRECTED) {
315 SetupDeviceForUnicast(adapter);
320 /* Set us up with Broadcast packet filtering */
321 if (filter & ET131X_PACKET_TYPE_BROADCAST) {
322 pf_ctrl |= 1; /* Broadcast filter bit */
327 /* Setup the receive mac configuration registers - Packet
328 * Filter control + the enable / disable for packet filter
329 * in the control reg.
331 writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
332 writel(ctrl, &adapter->regs->rxmac.ctrl);
338 * et131x_multicast - The handler to configure multicasting on the interface
339 * @netdev: a pointer to a net_device struct representing the device
341 void et131x_multicast(struct net_device *netdev)
343 struct et131x_adapter *adapter = netdev_priv(netdev);
344 uint32_t PacketFilter = 0;
346 struct netdev_hw_addr *ha;
349 spin_lock_irqsave(&adapter->Lock, flags);
351 /* Before we modify the platform-independent filter flags, store them
352 * locally. This allows us to determine if anything's changed and if
353 * we even need to bother the hardware
355 PacketFilter = adapter->PacketFilter;
357 /* Clear the 'multicast' flag locally; because we only have a single
358 * flag to check multicast, and multiple multicast addresses can be
359 * set, this is the easiest way to determine if more than one
360 * multicast address is being set.
362 PacketFilter &= ~ET131X_PACKET_TYPE_MULTICAST;
364 /* Check the net_device flags and set the device independent flags
368 if (netdev->flags & IFF_PROMISC)
369 adapter->PacketFilter |= ET131X_PACKET_TYPE_PROMISCUOUS;
371 adapter->PacketFilter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
373 if (netdev->flags & IFF_ALLMULTI)
374 adapter->PacketFilter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
376 if (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST)
377 adapter->PacketFilter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
379 if (netdev_mc_count(netdev) < 1) {
380 adapter->PacketFilter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
381 adapter->PacketFilter &= ~ET131X_PACKET_TYPE_MULTICAST;
383 adapter->PacketFilter |= ET131X_PACKET_TYPE_MULTICAST;
385 /* Set values in the private adapter struct */
387 netdev_for_each_mc_addr(ha, netdev) {
388 if (i == NIC_MAX_MCAST_LIST)
390 memcpy(adapter->MCList[i++], ha->addr, ETH_ALEN);
392 adapter->MCAddressCount = i;
394 /* Are the new flags different from the previous ones? If not, then no
397 * NOTE - This block will always update the MCList with the hardware,
398 * even if the addresses aren't the same.
400 if (PacketFilter != adapter->PacketFilter) {
401 /* Call the device's filter function */
402 et131x_set_packet_filter(adapter);
404 spin_unlock_irqrestore(&adapter->Lock, flags);
408 * et131x_tx - The handler to tx a packet on the device
409 * @skb: data to be Tx'd
410 * @netdev: device on which data is to be Tx'd
412 * Returns 0 on success, errno on failure (as defined in errno.h)
414 int et131x_tx(struct sk_buff *skb, struct net_device *netdev)
418 /* Save the timestamp for the TX timeout watchdog */
419 netdev->trans_start = jiffies;
421 /* Call the device-specific data Tx routine */
422 status = et131x_send_packets(skb, netdev);
424 /* Check status and manage the netif queue if necessary */
426 if (status == -ENOMEM) {
427 /* Put the queue to sleep until resources are
430 netif_stop_queue(netdev);
431 status = NETDEV_TX_BUSY;
433 status = NETDEV_TX_OK;
440 * et131x_tx_timeout - Timeout handler
441 * @netdev: a pointer to a net_device struct representing the device
443 * The handler called when a Tx request times out. The timeout period is
444 * specified by the 'tx_timeo" element in the net_device structure (see
445 * et131x_alloc_device() to see how this value is set).
447 void et131x_tx_timeout(struct net_device *netdev)
449 struct et131x_adapter *etdev = netdev_priv(netdev);
453 /* Any nonrecoverable hardware error?
454 * Checks adapter->flags for any failure in phy reading
456 if (etdev->Flags & fMP_ADAPTER_NON_RECOVER_ERROR)
459 /* Hardware failure? */
460 if (etdev->Flags & fMP_ADAPTER_HARDWARE_ERROR) {
461 dev_err(&etdev->pdev->dev, "hardware error - reset\n");
466 spin_lock_irqsave(&etdev->TCBSendQLock, flags);
468 tcb = etdev->tx_ring.send_head;
473 if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
474 spin_unlock_irqrestore(&etdev->TCBSendQLock,
477 dev_warn(&etdev->pdev->dev,
478 "Send stuck - reset. tcb->WrIndex %x, Flags 0x%08x\n",
482 et131x_close(netdev);
489 spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
493 * et131x_change_mtu - The handler called to change the MTU for the device
494 * @netdev: device whose MTU is to be changed
495 * @new_mtu: the desired MTU
497 * Returns 0 on success, errno on failure (as defined in errno.h)
499 int et131x_change_mtu(struct net_device *netdev, int new_mtu)
502 struct et131x_adapter *adapter = netdev_priv(netdev);
504 /* Make sure the requested MTU is valid */
505 if (new_mtu < 64 || new_mtu > 9216)
508 /* Stop the netif queue */
509 netif_stop_queue(netdev);
511 /* Stop the Tx and Rx DMA engines */
512 et131x_rx_dma_disable(adapter);
513 et131x_tx_dma_disable(adapter);
515 /* Disable device interrupts */
516 et131x_disable_interrupts(adapter);
517 et131x_handle_send_interrupt(adapter);
518 et131x_handle_recv_interrupt(adapter);
520 /* Set the new MTU */
521 netdev->mtu = new_mtu;
523 /* Free Rx DMA memory */
524 et131x_adapter_memory_free(adapter);
526 /* Set the config parameter for Jumbo Packet support */
527 adapter->RegistryJumboPacket = new_mtu + 14;
528 et131x_soft_reset(adapter);
530 /* Alloc and init Rx DMA memory */
531 result = et131x_adapter_memory_alloc(adapter);
533 dev_warn(&adapter->pdev->dev,
534 "Change MTU failed; couldn't re-alloc DMA memory\n");
538 et131x_init_send(adapter);
540 et131x_hwaddr_init(adapter);
541 memcpy(netdev->dev_addr, adapter->addr, ETH_ALEN);
543 /* Init the device with the new settings */
544 et131x_adapter_setup(adapter);
546 /* Enable interrupts */
547 if (adapter->Flags & fMP_ADAPTER_INTERRUPT_IN_USE)
548 et131x_enable_interrupts(adapter);
550 /* Restart the Tx and Rx DMA engines */
551 et131x_rx_dma_enable(adapter);
552 et131x_tx_dma_enable(adapter);
554 /* Restart the netif queue */
555 netif_wake_queue(netdev);
560 * et131x_set_mac_addr - handler to change the MAC address for the device
561 * @netdev: device whose MAC is to be changed
562 * @new_mac: the desired MAC address
564 * Returns 0 on success, errno on failure (as defined in errno.h)
566 * IMPLEMENTED BY : blux http://berndlux.de 22.01.2007 21:14
568 int et131x_set_mac_addr(struct net_device *netdev, void *new_mac)
571 struct et131x_adapter *adapter = netdev_priv(netdev);
572 struct sockaddr *address = new_mac;
579 /* Make sure the requested MAC is valid */
580 if (!is_valid_ether_addr(address->sa_data))
583 /* Stop the netif queue */
584 netif_stop_queue(netdev);
586 /* Stop the Tx and Rx DMA engines */
587 et131x_rx_dma_disable(adapter);
588 et131x_tx_dma_disable(adapter);
590 /* Disable device interrupts */
591 et131x_disable_interrupts(adapter);
592 et131x_handle_send_interrupt(adapter);
593 et131x_handle_recv_interrupt(adapter);
595 /* Set the new MAC */
596 /* netdev->set_mac_address = &new_mac; */
597 /* netdev->mtu = new_mtu; */
599 memcpy(netdev->dev_addr, address->sa_data, netdev->addr_len);
601 printk(KERN_INFO "%s: Setting MAC address to %pM\n",
602 netdev->name, netdev->dev_addr);
604 /* Free Rx DMA memory */
605 et131x_adapter_memory_free(adapter);
607 /* Set the config parameter for Jumbo Packet support */
608 /* adapter->RegistryJumboPacket = new_mtu + 14; */
609 /* blux: not needet here, we'll change the MAC */
611 et131x_soft_reset(adapter);
613 /* Alloc and init Rx DMA memory */
614 result = et131x_adapter_memory_alloc(adapter);
616 dev_err(&adapter->pdev->dev,
617 "Change MAC failed; couldn't re-alloc DMA memory\n");
621 et131x_init_send(adapter);
623 et131x_hwaddr_init(adapter);
625 /* Init the device with the new settings */
626 et131x_adapter_setup(adapter);
628 /* Enable interrupts */
629 if (adapter->Flags & fMP_ADAPTER_INTERRUPT_IN_USE)
630 et131x_enable_interrupts(adapter);
632 /* Restart the Tx and Rx DMA engines */
633 et131x_rx_dma_enable(adapter);
634 et131x_tx_dma_enable(adapter);
636 /* Restart the netif queue */
637 netif_wake_queue(netdev);
641 static const struct net_device_ops et131x_netdev_ops = {
642 .ndo_open = et131x_open,
643 .ndo_stop = et131x_close,
644 .ndo_start_xmit = et131x_tx,
645 .ndo_set_multicast_list = et131x_multicast,
646 .ndo_tx_timeout = et131x_tx_timeout,
647 .ndo_change_mtu = et131x_change_mtu,
648 .ndo_set_mac_address = et131x_set_mac_addr,
649 .ndo_validate_addr = eth_validate_addr,
650 .ndo_get_stats = et131x_stats,
651 .ndo_do_ioctl = et131x_ioctl,
655 * et131x_device_alloc
657 * Returns pointer to the allocated and initialized net_device struct for
660 * Create instances of net_device and wl_private for the new adapter and
661 * register the device's entry points in the net_device structure.
663 struct net_device *et131x_device_alloc(void)
665 struct net_device *netdev;
667 /* Alloc net_device and adapter structs */
668 netdev = alloc_etherdev(sizeof(struct et131x_adapter));
670 if (netdev == NULL) {
671 printk(KERN_ERR "et131x: Alloc of net_device struct failed\n");
675 /* Setup the function registration table (and other data) for a
678 /* netdev->init = &et131x_init; */
679 /* netdev->set_config = &et131x_config; */
680 netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
681 netdev->netdev_ops = &et131x_netdev_ops;
683 /* netdev->ethtool_ops = &et131x_ethtool_ops; */
686 /* netdev->poll = &et131x_poll; */
687 /* netdev->poll_controller = &et131x_poll_controller; */