staging:iio:dac:ad5791: Allow asymmetrical reference voltages
[zen-stable.git] / drivers / staging / et131x / et131x_netdev.c
blob62070cff76f1762583d26b2b5019294368c58f41
1 /*
2 * Agere Systems Inc.
3 * 10/100/1000 Base-T Ethernet Driver for the ET1310 and ET131x series MACs
5 * Copyright © 2005 Agere Systems Inc.
6 * All rights reserved.
7 * http://www.agere.com
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 *------------------------------------------------------------------------------
17 * SOFTWARE LICENSE
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
33 * distribution.
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.
43 * Disclaimer
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
56 * DAMAGE.
60 #include "et131x_defs.h"
62 #include <linux/init.h>
63 #include <linux/module.h>
64 #include <linux/types.h>
65 #include <linux/kernel.h>
67 #include <linux/sched.h>
68 #include <linux/ptrace.h>
69 #include <linux/ctype.h>
70 #include <linux/string.h>
71 #include <linux/timer.h>
72 #include <linux/interrupt.h>
73 #include <linux/in.h>
74 #include <linux/delay.h>
75 #include <linux/io.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>
86 #include <linux/phy.h>
88 #include "et1310_phy.h"
89 #include "et1310_tx.h"
90 #include "et131x_adapter.h"
91 #include "et131x.h"
93 /**
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 static 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_errors = devstat->rx_length_errs +
106 devstat->rx_align_errs +
107 devstat->rx_crc_errs +
108 devstat->rx_code_violations +
109 devstat->rx_other_errs;
110 stats->tx_errors = devstat->tx_max_pkt_errs;
111 stats->multicast = devstat->multicast_pkts_rcvd;
112 stats->collisions = devstat->tx_collisions;
114 stats->rx_length_errors = devstat->rx_length_errs;
115 stats->rx_over_errors = devstat->rx_overflows;
116 stats->rx_crc_errors = devstat->rx_crc_errs;
118 /* NOTE: These stats don't have corresponding values in CE_STATS,
119 * so we're going to have to update these directly from within the
120 * TX/RX code
122 /* stats->rx_bytes = 20; devstat->; */
123 /* stats->tx_bytes = 20; devstat->; */
124 /* stats->rx_dropped = devstat->; */
125 /* stats->tx_dropped = devstat->; */
127 /* NOTE: Not used, can't find analogous statistics */
128 /* stats->rx_frame_errors = devstat->; */
129 /* stats->rx_fifo_errors = devstat->; */
130 /* stats->rx_missed_errors = devstat->; */
132 /* stats->tx_aborted_errors = devstat->; */
133 /* stats->tx_carrier_errors = devstat->; */
134 /* stats->tx_fifo_errors = devstat->; */
135 /* stats->tx_heartbeat_errors = devstat->; */
136 /* stats->tx_window_errors = devstat->; */
137 return stats;
141 * et131x_enable_txrx - Enable tx/rx queues
142 * @netdev: device to be enabled
144 void et131x_enable_txrx(struct net_device *netdev)
146 struct et131x_adapter *adapter = netdev_priv(netdev);
148 /* Enable the Tx and Rx DMA engines (if not already enabled) */
149 et131x_rx_dma_enable(adapter);
150 et131x_tx_dma_enable(adapter);
152 /* Enable device interrupts */
153 if (adapter->flags & fMP_ADAPTER_INTERRUPT_IN_USE)
154 et131x_enable_interrupts(adapter);
156 /* We're ready to move some data, so start the queue */
157 netif_start_queue(netdev);
161 * et131x_disable_txrx - Disable tx/rx queues
162 * @netdev: device to be disabled
164 void et131x_disable_txrx(struct net_device *netdev)
166 struct et131x_adapter *adapter = netdev_priv(netdev);
168 /* First thing is to stop the queue */
169 netif_stop_queue(netdev);
171 /* Stop the Tx and Rx DMA engines */
172 et131x_rx_dma_disable(adapter);
173 et131x_tx_dma_disable(adapter);
175 /* Disable device interrupts */
176 et131x_disable_interrupts(adapter);
180 * et131x_up - Bring up a device for use.
181 * @netdev: device to be opened
183 void et131x_up(struct net_device *netdev)
185 struct et131x_adapter *adapter = netdev_priv(netdev);
187 et131x_enable_txrx(netdev);
188 phy_start(adapter->phydev);
192 * et131x_open - Open the device for use.
193 * @netdev: device to be opened
195 * Returns 0 on success, errno on failure (as defined in errno.h)
197 int et131x_open(struct net_device *netdev)
199 int result = 0;
200 struct et131x_adapter *adapter = netdev_priv(netdev);
202 /* Start the timer to track NIC errors */
203 init_timer(&adapter->error_timer);
204 adapter->error_timer.expires = jiffies + TX_ERROR_PERIOD * HZ / 1000;
205 adapter->error_timer.function = et131x_error_timer_handler;
206 adapter->error_timer.data = (unsigned long)adapter;
207 add_timer(&adapter->error_timer);
209 /* Register our IRQ */
210 result = request_irq(netdev->irq, et131x_isr, IRQF_SHARED,
211 netdev->name, netdev);
212 if (result) {
213 dev_err(&adapter->pdev->dev, "could not register IRQ %d\n",
214 netdev->irq);
215 return result;
218 adapter->flags |= fMP_ADAPTER_INTERRUPT_IN_USE;
220 et131x_up(netdev);
222 return result;
226 * et131x_down - Bring down the device
227 * @netdev: device to be broght down
229 void et131x_down(struct net_device *netdev)
231 struct et131x_adapter *adapter = netdev_priv(netdev);
233 /* Save the timestamp for the TX watchdog, prevent a timeout */
234 netdev->trans_start = jiffies;
236 phy_stop(adapter->phydev);
237 et131x_disable_txrx(netdev);
241 * et131x_close - Close the device
242 * @netdev: device to be closed
244 * Returns 0 on success, errno on failure (as defined in errno.h)
246 int et131x_close(struct net_device *netdev)
248 struct et131x_adapter *adapter = netdev_priv(netdev);
250 et131x_down(netdev);
252 adapter->flags &= ~fMP_ADAPTER_INTERRUPT_IN_USE;
253 free_irq(netdev->irq, netdev);
255 /* Stop the error timer */
256 return del_timer_sync(&adapter->error_timer);
260 * et131x_ioctl - The I/O Control handler for the driver
261 * @netdev: device on which the control request is being made
262 * @reqbuf: a pointer to the IOCTL request buffer
263 * @cmd: the IOCTL command code
265 * Returns 0 on success, errno on failure (as defined in errno.h)
267 static int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf, int cmd)
269 struct et131x_adapter *adapter = netdev_priv(netdev);
271 if (!adapter->phydev)
272 return -EINVAL;
274 return phy_mii_ioctl(adapter->phydev, reqbuf, cmd);
278 * et131x_set_packet_filter - Configures the Rx Packet filtering on the device
279 * @adapter: pointer to our private adapter structure
281 * FIXME: lot of dups with MAC code
283 * Returns 0 on success, errno on failure
285 static int et131x_set_packet_filter(struct et131x_adapter *adapter)
287 int status = 0;
288 uint32_t filter = adapter->packet_filter;
289 u32 ctrl;
290 u32 pf_ctrl;
292 ctrl = readl(&adapter->regs->rxmac.ctrl);
293 pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
295 /* Default to disabled packet filtering. Enable it in the individual
296 * case statements that require the device to filter something
298 ctrl |= 0x04;
300 /* Set us to be in promiscuous mode so we receive everything, this
301 * is also true when we get a packet filter of 0
303 if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
304 pf_ctrl &= ~7; /* Clear filter bits */
305 else {
307 * Set us up with Multicast packet filtering. Three cases are
308 * possible - (1) we have a multi-cast list, (2) we receive ALL
309 * multicast entries or (3) we receive none.
311 if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
312 pf_ctrl &= ~2; /* Multicast filter bit */
313 else {
314 et1310_setup_device_for_multicast(adapter);
315 pf_ctrl |= 2;
316 ctrl &= ~0x04;
319 /* Set us up with Unicast packet filtering */
320 if (filter & ET131X_PACKET_TYPE_DIRECTED) {
321 et1310_setup_device_for_unicast(adapter);
322 pf_ctrl |= 4;
323 ctrl &= ~0x04;
326 /* Set us up with Broadcast packet filtering */
327 if (filter & ET131X_PACKET_TYPE_BROADCAST) {
328 pf_ctrl |= 1; /* Broadcast filter bit */
329 ctrl &= ~0x04;
330 } else
331 pf_ctrl &= ~1;
333 /* Setup the receive mac configuration registers - Packet
334 * Filter control + the enable / disable for packet filter
335 * in the control reg.
337 writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
338 writel(ctrl, &adapter->regs->rxmac.ctrl);
340 return status;
344 * et131x_multicast - The handler to configure multicasting on the interface
345 * @netdev: a pointer to a net_device struct representing the device
347 static void et131x_multicast(struct net_device *netdev)
349 struct et131x_adapter *adapter = netdev_priv(netdev);
350 uint32_t packet_filter = 0;
351 unsigned long flags;
352 struct netdev_hw_addr *ha;
353 int i;
355 spin_lock_irqsave(&adapter->lock, flags);
357 /* Before we modify the platform-independent filter flags, store them
358 * locally. This allows us to determine if anything's changed and if
359 * we even need to bother the hardware
361 packet_filter = adapter->packet_filter;
363 /* Clear the 'multicast' flag locally; because we only have a single
364 * flag to check multicast, and multiple multicast addresses can be
365 * set, this is the easiest way to determine if more than one
366 * multicast address is being set.
368 packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
370 /* Check the net_device flags and set the device independent flags
371 * accordingly
374 if (netdev->flags & IFF_PROMISC)
375 adapter->packet_filter |= ET131X_PACKET_TYPE_PROMISCUOUS;
376 else
377 adapter->packet_filter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
379 if (netdev->flags & IFF_ALLMULTI)
380 adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
382 if (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST)
383 adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
385 if (netdev_mc_count(netdev) < 1) {
386 adapter->packet_filter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
387 adapter->packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
388 } else
389 adapter->packet_filter |= ET131X_PACKET_TYPE_MULTICAST;
391 /* Set values in the private adapter struct */
392 i = 0;
393 netdev_for_each_mc_addr(ha, netdev) {
394 if (i == NIC_MAX_MCAST_LIST)
395 break;
396 memcpy(adapter->multicast_list[i++], ha->addr, ETH_ALEN);
398 adapter->multicast_addr_count = i;
400 /* Are the new flags different from the previous ones? If not, then no
401 * action is required
403 * NOTE - This block will always update the multicast_list with the
404 * hardware, even if the addresses aren't the same.
406 if (packet_filter != adapter->packet_filter) {
407 /* Call the device's filter function */
408 et131x_set_packet_filter(adapter);
410 spin_unlock_irqrestore(&adapter->lock, flags);
414 * et131x_tx - The handler to tx a packet on the device
415 * @skb: data to be Tx'd
416 * @netdev: device on which data is to be Tx'd
418 * Returns 0 on success, errno on failure (as defined in errno.h)
420 static int et131x_tx(struct sk_buff *skb, struct net_device *netdev)
422 int status = 0;
424 /* Save the timestamp for the TX timeout watchdog */
425 netdev->trans_start = jiffies;
427 /* Call the device-specific data Tx routine */
428 status = et131x_send_packets(skb, netdev);
430 /* Check status and manage the netif queue if necessary */
431 if (status != 0) {
432 if (status == -ENOMEM) {
433 /* Put the queue to sleep until resources are
434 * available
436 netif_stop_queue(netdev);
437 status = NETDEV_TX_BUSY;
438 } else {
439 status = NETDEV_TX_OK;
442 return status;
446 * et131x_tx_timeout - Timeout handler
447 * @netdev: a pointer to a net_device struct representing the device
449 * The handler called when a Tx request times out. The timeout period is
450 * specified by the 'tx_timeo" element in the net_device structure (see
451 * et131x_alloc_device() to see how this value is set).
453 static void et131x_tx_timeout(struct net_device *netdev)
455 struct et131x_adapter *adapter = netdev_priv(netdev);
456 struct tcb *tcb;
457 unsigned long flags;
459 /* If the device is closed, ignore the timeout */
460 if (~(adapter->flags & fMP_ADAPTER_INTERRUPT_IN_USE))
461 return;
463 /* Any nonrecoverable hardware error?
464 * Checks adapter->flags for any failure in phy reading
466 if (adapter->flags & fMP_ADAPTER_NON_RECOVER_ERROR)
467 return;
469 /* Hardware failure? */
470 if (adapter->flags & fMP_ADAPTER_HARDWARE_ERROR) {
471 dev_err(&adapter->pdev->dev, "hardware error - reset\n");
472 return;
475 /* Is send stuck? */
476 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
478 tcb = adapter->tx_ring.send_head;
480 if (tcb != NULL) {
481 tcb->count++;
483 if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
484 spin_unlock_irqrestore(&adapter->tcb_send_qlock,
485 flags);
487 dev_warn(&adapter->pdev->dev,
488 "Send stuck - reset. tcb->WrIndex %x, flags 0x%08x\n",
489 tcb->index,
490 tcb->flags);
492 adapter->net_stats.tx_errors++;
494 /* perform reset of tx/rx */
495 et131x_disable_txrx(netdev);
496 et131x_enable_txrx(netdev);
497 return;
501 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
505 * et131x_change_mtu - The handler called to change the MTU for the device
506 * @netdev: device whose MTU is to be changed
507 * @new_mtu: the desired MTU
509 * Returns 0 on success, errno on failure (as defined in errno.h)
511 static int et131x_change_mtu(struct net_device *netdev, int new_mtu)
513 int result = 0;
514 struct et131x_adapter *adapter = netdev_priv(netdev);
516 /* Make sure the requested MTU is valid */
517 if (new_mtu < 64 || new_mtu > 9216)
518 return -EINVAL;
520 et131x_disable_txrx(netdev);
521 et131x_handle_send_interrupt(adapter);
522 et131x_handle_recv_interrupt(adapter);
524 /* Set the new MTU */
525 netdev->mtu = new_mtu;
527 /* Free Rx DMA memory */
528 et131x_adapter_memory_free(adapter);
530 /* Set the config parameter for Jumbo Packet support */
531 adapter->registry_jumbo_packet = new_mtu + 14;
532 et131x_soft_reset(adapter);
534 /* Alloc and init Rx DMA memory */
535 result = et131x_adapter_memory_alloc(adapter);
536 if (result != 0) {
537 dev_warn(&adapter->pdev->dev,
538 "Change MTU failed; couldn't re-alloc DMA memory\n");
539 return result;
542 et131x_init_send(adapter);
544 et131x_hwaddr_init(adapter);
545 memcpy(netdev->dev_addr, adapter->addr, ETH_ALEN);
547 /* Init the device with the new settings */
548 et131x_adapter_setup(adapter);
550 et131x_enable_txrx(netdev);
552 return result;
556 * et131x_set_mac_addr - handler to change the MAC address for the device
557 * @netdev: device whose MAC is to be changed
558 * @new_mac: the desired MAC address
560 * Returns 0 on success, errno on failure (as defined in errno.h)
562 * IMPLEMENTED BY : blux http://berndlux.de 22.01.2007 21:14
564 static int et131x_set_mac_addr(struct net_device *netdev, void *new_mac)
566 int result = 0;
567 struct et131x_adapter *adapter = netdev_priv(netdev);
568 struct sockaddr *address = new_mac;
570 /* begin blux */
572 if (adapter == NULL)
573 return -ENODEV;
575 /* Make sure the requested MAC is valid */
576 if (!is_valid_ether_addr(address->sa_data))
577 return -EINVAL;
579 et131x_disable_txrx(netdev);
580 et131x_handle_send_interrupt(adapter);
581 et131x_handle_recv_interrupt(adapter);
583 /* Set the new MAC */
584 /* netdev->set_mac_address = &new_mac; */
586 memcpy(netdev->dev_addr, address->sa_data, netdev->addr_len);
588 printk(KERN_INFO "%s: Setting MAC address to %pM\n",
589 netdev->name, netdev->dev_addr);
591 /* Free Rx DMA memory */
592 et131x_adapter_memory_free(adapter);
594 et131x_soft_reset(adapter);
596 /* Alloc and init Rx DMA memory */
597 result = et131x_adapter_memory_alloc(adapter);
598 if (result != 0) {
599 dev_err(&adapter->pdev->dev,
600 "Change MAC failed; couldn't re-alloc DMA memory\n");
601 return result;
604 et131x_init_send(adapter);
606 et131x_hwaddr_init(adapter);
608 /* Init the device with the new settings */
609 et131x_adapter_setup(adapter);
611 et131x_enable_txrx(netdev);
613 return result;
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
633 * this device.
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));
645 if (!netdev) {
646 printk(KERN_ERR "et131x: Alloc of net_device struct failed\n");
647 return NULL;
651 * Setup the function registration table (and other data) for a
652 * net_device
654 netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
655 netdev->netdev_ops = &et131x_netdev_ops;
657 /* Poll? */
658 /* netdev->poll = &et131x_poll; */
659 /* netdev->poll_controller = &et131x_poll_controller; */
660 return netdev;