2 * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved.
3 * Copyright(c) 2006 Chris Snook <csnook@redhat.com>
4 * Copyright(c) 2006 Jay Cliburn <jcliburn@gmail.com>
6 * Derived from Intel e1000 driver
7 * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The full GNU General Public License is included in this distribution in the
24 * file called COPYING.
26 * Contact Information:
27 * Xiong Huang <xiong_huang@attansic.com>
28 * Attansic Technology Corp. 3F 147, Xianzheng 9th Road, Zhubei,
29 * Xinzhu 302, TAIWAN, REPUBLIC OF CHINA
31 * Chris Snook <csnook@redhat.com>
32 * Jay Cliburn <jcliburn@gmail.com>
34 * This version is adapted from the Attansic reference driver for
35 * inclusion in the Linux kernel. It is currently under heavy development.
36 * A very incomplete list of things that need to be dealt with:
39 * Fix TSO; tx performance is horrible with TSO enabled.
41 * Add more ethtool functions, including set ring parameters.
42 * Fix abstruse irq enable/disable condition described here:
43 * http://marc.theaimsgroup.com/?l=linux-netdev&m=116398508500553&w=2
49 * interrupt coalescing
53 #include <linux/types.h>
54 #include <linux/netdevice.h>
55 #include <linux/pci.h>
56 #include <linux/spinlock.h>
57 #include <linux/slab.h>
58 #include <linux/string.h>
59 #include <linux/skbuff.h>
60 #include <linux/etherdevice.h>
61 #include <linux/if_vlan.h>
62 #include <linux/irqreturn.h>
63 #include <linux/workqueue.h>
64 #include <linux/timer.h>
65 #include <linux/jiffies.h>
66 #include <linux/hardirq.h>
67 #include <linux/interrupt.h>
68 #include <linux/irqflags.h>
69 #include <linux/dma-mapping.h>
70 #include <linux/net.h>
74 #include <linux/tcp.h>
75 #include <linux/compiler.h>
76 #include <linux/delay.h>
77 #include <linux/mii.h>
78 #include <net/checksum.h>
80 #include <asm/atomic.h>
81 #include <asm/byteorder.h>
85 #define DRIVER_VERSION "2.0.7"
87 char atl1_driver_name[] = "atl1";
88 static const char atl1_driver_string[] = "Attansic L1 Ethernet Network Driver";
89 static const char atl1_copyright[] = "Copyright(c) 2005-2006 Attansic Corporation.";
90 char atl1_driver_version[] = DRIVER_VERSION;
93 ("Attansic Corporation <xiong_huang@attansic.com>, Chris Snook <csnook@redhat.com>, Jay Cliburn <jcliburn@gmail.com>");
94 MODULE_DESCRIPTION("Attansic 1000M Ethernet Network Driver");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(DRIVER_VERSION);
99 * atl1_pci_tbl - PCI Device ID Table
101 static const struct pci_device_id atl1_pci_tbl[] = {
102 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1)},
103 /* required last entry */
107 MODULE_DEVICE_TABLE(pci, atl1_pci_tbl);
110 * atl1_sw_init - Initialize general software structures (struct atl1_adapter)
111 * @adapter: board private structure to initialize
113 * atl1_sw_init initializes the Adapter private data structure.
114 * Fields are initialized based on PCI device information and
115 * OS network device settings (MTU size).
117 static int __devinit atl1_sw_init(struct atl1_adapter *adapter)
119 struct atl1_hw *hw = &adapter->hw;
120 struct net_device *netdev = adapter->netdev;
121 struct pci_dev *pdev = adapter->pdev;
123 /* PCI config space info */
124 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
126 hw->max_frame_size = netdev->mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
127 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
130 adapter->rx_buffer_len = (hw->max_frame_size + 7) & ~7;
131 adapter->ict = 50000; /* 100ms */
132 adapter->link_speed = SPEED_0; /* hardware init */
133 adapter->link_duplex = FULL_DUPLEX;
135 hw->phy_configured = false;
136 hw->preamble_len = 7;
146 hw->rfd_fetch_gap = 1;
147 hw->rx_jumbo_th = adapter->rx_buffer_len / 8;
148 hw->rx_jumbo_lkah = 1;
149 hw->rrd_ret_timer = 16;
151 hw->tpd_fetch_th = 16;
152 hw->txf_burst = 0x100;
153 hw->tx_jumbo_task_th = (hw->max_frame_size + 7) >> 3;
154 hw->tpd_fetch_gap = 1;
155 hw->rcb_value = atl1_rcb_64;
156 hw->dma_ord = atl1_dma_ord_enh;
157 hw->dmar_block = atl1_dma_req_256;
158 hw->dmaw_block = atl1_dma_req_256;
161 hw->cmb_rx_timer = 1; /* about 2us */
162 hw->cmb_tx_timer = 1; /* about 2us */
163 hw->smb_timer = 100000; /* about 200ms */
165 atomic_set(&adapter->irq_sem, 0);
166 spin_lock_init(&adapter->lock);
167 spin_lock_init(&adapter->mb_lock);
173 * atl1_setup_mem_resources - allocate Tx / RX descriptor resources
174 * @adapter: board private structure
176 * Return 0 on success, negative on failure
178 s32 atl1_setup_ring_resources(struct atl1_adapter *adapter)
180 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
181 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
182 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
183 struct atl1_ring_header *ring_header = &adapter->ring_header;
184 struct pci_dev *pdev = adapter->pdev;
188 size = sizeof(struct atl1_buffer) * (tpd_ring->count + rfd_ring->count);
189 tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
190 if (unlikely(!tpd_ring->buffer_info)) {
191 printk(KERN_WARNING "%s: kzalloc failed , size = D%d\n",
192 atl1_driver_name, size);
195 rfd_ring->buffer_info =
196 (struct atl1_buffer *)(tpd_ring->buffer_info + tpd_ring->count);
198 /* real ring DMA buffer */
199 ring_header->size = size = sizeof(struct tx_packet_desc) *
201 + sizeof(struct rx_free_desc) * rfd_ring->count
202 + sizeof(struct rx_return_desc) * rrd_ring->count
203 + sizeof(struct coals_msg_block)
204 + sizeof(struct stats_msg_block)
205 + 40; /* "40: for 8 bytes align" huh? -- CHS */
207 ring_header->desc = pci_alloc_consistent(pdev, ring_header->size,
209 if (unlikely(!ring_header->desc)) {
211 "%s: pci_alloc_consistent failed, size = D%d\n",
212 atl1_driver_name, size);
216 memset(ring_header->desc, 0, ring_header->size);
219 tpd_ring->dma = ring_header->dma;
220 offset = (tpd_ring->dma & 0x7) ? (8 - (ring_header->dma & 0x7)) : 0;
221 tpd_ring->dma += offset;
222 tpd_ring->desc = (u8 *) ring_header->desc + offset;
223 tpd_ring->size = sizeof(struct tx_packet_desc) * tpd_ring->count;
224 atomic_set(&tpd_ring->next_to_use, 0);
225 atomic_set(&tpd_ring->next_to_clean, 0);
228 rfd_ring->dma = tpd_ring->dma + tpd_ring->size;
229 offset = (rfd_ring->dma & 0x7) ? (8 - (rfd_ring->dma & 0x7)) : 0;
230 rfd_ring->dma += offset;
231 rfd_ring->desc = (u8 *) tpd_ring->desc + (tpd_ring->size + offset);
232 rfd_ring->size = sizeof(struct rx_free_desc) * rfd_ring->count;
233 rfd_ring->next_to_clean = 0;
234 /* rfd_ring->next_to_use = rfd_ring->count - 1; */
235 atomic_set(&rfd_ring->next_to_use, 0);
238 rrd_ring->dma = rfd_ring->dma + rfd_ring->size;
239 offset = (rrd_ring->dma & 0x7) ? (8 - (rrd_ring->dma & 0x7)) : 0;
240 rrd_ring->dma += offset;
241 rrd_ring->desc = (u8 *) rfd_ring->desc + (rfd_ring->size + offset);
242 rrd_ring->size = sizeof(struct rx_return_desc) * rrd_ring->count;
243 rrd_ring->next_to_use = 0;
244 atomic_set(&rrd_ring->next_to_clean, 0);
247 adapter->cmb.dma = rrd_ring->dma + rrd_ring->size;
248 offset = (adapter->cmb.dma & 0x7) ? (8 - (adapter->cmb.dma & 0x7)) : 0;
249 adapter->cmb.dma += offset;
251 (struct coals_msg_block *) ((u8 *) rrd_ring->desc +
252 (rrd_ring->size + offset));
255 adapter->smb.dma = adapter->cmb.dma + sizeof(struct coals_msg_block);
256 offset = (adapter->smb.dma & 0x7) ? (8 - (adapter->smb.dma & 0x7)) : 0;
257 adapter->smb.dma += offset;
258 adapter->smb.smb = (struct stats_msg_block *)
259 ((u8 *) adapter->cmb.cmb + (sizeof(struct coals_msg_block) + offset));
264 kfree(tpd_ring->buffer_info);
269 * atl1_irq_enable - Enable default interrupt generation settings
270 * @adapter: board private structure
272 static void atl1_irq_enable(struct atl1_adapter *adapter)
274 if (likely(!atomic_dec_and_test(&adapter->irq_sem)))
275 iowrite32(IMR_NORMAL_MASK, adapter->hw.hw_addr + REG_IMR);
278 static void atl1_clear_phy_int(struct atl1_adapter *adapter)
283 spin_lock_irqsave(&adapter->lock, flags);
284 atl1_read_phy_reg(&adapter->hw, 19, &phy_data);
285 spin_unlock_irqrestore(&adapter->lock, flags);
288 static void atl1_inc_smb(struct atl1_adapter *adapter)
290 struct stats_msg_block *smb = adapter->smb.smb;
292 /* Fill out the OS statistics structure */
293 adapter->soft_stats.rx_packets += smb->rx_ok;
294 adapter->soft_stats.tx_packets += smb->tx_ok;
295 adapter->soft_stats.rx_bytes += smb->rx_byte_cnt;
296 adapter->soft_stats.tx_bytes += smb->tx_byte_cnt;
297 adapter->soft_stats.multicast += smb->rx_mcast;
298 adapter->soft_stats.collisions += (smb->tx_1_col +
302 adapter->hw.max_retry);
305 adapter->soft_stats.rx_errors += (smb->rx_frag +
310 smb->rx_rrd_ov + smb->rx_align_err);
311 adapter->soft_stats.rx_fifo_errors += smb->rx_rxf_ov;
312 adapter->soft_stats.rx_length_errors += smb->rx_len_err;
313 adapter->soft_stats.rx_crc_errors += smb->rx_fcs_err;
314 adapter->soft_stats.rx_frame_errors += smb->rx_align_err;
315 adapter->soft_stats.rx_missed_errors += (smb->rx_rrd_ov +
318 adapter->soft_stats.rx_pause += smb->rx_pause;
319 adapter->soft_stats.rx_rrd_ov += smb->rx_rrd_ov;
320 adapter->soft_stats.rx_trunc += smb->rx_sz_ov;
323 adapter->soft_stats.tx_errors += (smb->tx_late_col +
325 smb->tx_underrun + smb->tx_trunc);
326 adapter->soft_stats.tx_fifo_errors += smb->tx_underrun;
327 adapter->soft_stats.tx_aborted_errors += smb->tx_abort_col;
328 adapter->soft_stats.tx_window_errors += smb->tx_late_col;
330 adapter->soft_stats.excecol += smb->tx_abort_col;
331 adapter->soft_stats.deffer += smb->tx_defer;
332 adapter->soft_stats.scc += smb->tx_1_col;
333 adapter->soft_stats.mcc += smb->tx_2_col;
334 adapter->soft_stats.latecol += smb->tx_late_col;
335 adapter->soft_stats.tx_underun += smb->tx_underrun;
336 adapter->soft_stats.tx_trunc += smb->tx_trunc;
337 adapter->soft_stats.tx_pause += smb->tx_pause;
339 adapter->net_stats.rx_packets = adapter->soft_stats.rx_packets;
340 adapter->net_stats.tx_packets = adapter->soft_stats.tx_packets;
341 adapter->net_stats.rx_bytes = adapter->soft_stats.rx_bytes;
342 adapter->net_stats.tx_bytes = adapter->soft_stats.tx_bytes;
343 adapter->net_stats.multicast = adapter->soft_stats.multicast;
344 adapter->net_stats.collisions = adapter->soft_stats.collisions;
345 adapter->net_stats.rx_errors = adapter->soft_stats.rx_errors;
346 adapter->net_stats.rx_over_errors =
347 adapter->soft_stats.rx_missed_errors;
348 adapter->net_stats.rx_length_errors =
349 adapter->soft_stats.rx_length_errors;
350 adapter->net_stats.rx_crc_errors = adapter->soft_stats.rx_crc_errors;
351 adapter->net_stats.rx_frame_errors =
352 adapter->soft_stats.rx_frame_errors;
353 adapter->net_stats.rx_fifo_errors = adapter->soft_stats.rx_fifo_errors;
354 adapter->net_stats.rx_missed_errors =
355 adapter->soft_stats.rx_missed_errors;
356 adapter->net_stats.tx_errors = adapter->soft_stats.tx_errors;
357 adapter->net_stats.tx_fifo_errors = adapter->soft_stats.tx_fifo_errors;
358 adapter->net_stats.tx_aborted_errors =
359 adapter->soft_stats.tx_aborted_errors;
360 adapter->net_stats.tx_window_errors =
361 adapter->soft_stats.tx_window_errors;
362 adapter->net_stats.tx_carrier_errors =
363 adapter->soft_stats.tx_carrier_errors;
366 static void atl1_rx_checksum(struct atl1_adapter *adapter,
367 struct rx_return_desc *rrd,
370 skb->ip_summed = CHECKSUM_NONE;
372 if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
373 if (rrd->err_flg & (ERR_FLAG_CRC | ERR_FLAG_TRUNC |
374 ERR_FLAG_CODE | ERR_FLAG_OV)) {
375 adapter->hw_csum_err++;
376 printk(KERN_DEBUG "%s: rx checksum error\n",
383 if (!(rrd->pkt_flg & PACKET_FLAG_IPV4))
384 /* checksum is invalid, but it's not an IPv4 pkt, so ok */
388 if (likely(!(rrd->err_flg &
389 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM)))) {
390 skb->ip_summed = CHECKSUM_UNNECESSARY;
391 adapter->hw_csum_good++;
395 /* IPv4, but hardware thinks its checksum is wrong */
396 printk(KERN_DEBUG "%s: hw csum wrong pkt_flag:%x, err_flag:%x\n",
397 atl1_driver_name, rrd->pkt_flg, rrd->err_flg);
398 skb->ip_summed = CHECKSUM_COMPLETE;
399 skb->csum = htons(rrd->xsz.xsum_sz.rx_chksum);
400 adapter->hw_csum_err++;
405 * atl1_alloc_rx_buffers - Replace used receive buffers
406 * @adapter: address of board private structure
408 static u16 atl1_alloc_rx_buffers(struct atl1_adapter *adapter)
410 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
411 struct net_device *netdev = adapter->netdev;
412 struct pci_dev *pdev = adapter->pdev;
414 unsigned long offset;
415 struct atl1_buffer *buffer_info, *next_info;
418 u16 rfd_next_to_use, next_next;
419 struct rx_free_desc *rfd_desc;
421 next_next = rfd_next_to_use = atomic_read(&rfd_ring->next_to_use);
422 if (++next_next == rfd_ring->count)
424 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
425 next_info = &rfd_ring->buffer_info[next_next];
427 while (!buffer_info->alloced && !next_info->alloced) {
428 if (buffer_info->skb) {
429 buffer_info->alloced = 1;
433 rfd_desc = ATL1_RFD_DESC(rfd_ring, rfd_next_to_use);
435 skb = dev_alloc_skb(adapter->rx_buffer_len + NET_IP_ALIGN);
436 if (unlikely(!skb)) { /* Better luck next round */
437 adapter->net_stats.rx_dropped++;
442 * Make buffer alignment 2 beyond a 16 byte boundary
443 * this will result in a 16 byte aligned IP header after
444 * the 14 byte MAC header is removed
446 skb_reserve(skb, NET_IP_ALIGN);
449 buffer_info->alloced = 1;
450 buffer_info->skb = skb;
451 buffer_info->length = (u16) adapter->rx_buffer_len;
452 page = virt_to_page(skb->data);
453 offset = (unsigned long)skb->data & ~PAGE_MASK;
454 buffer_info->dma = pci_map_page(pdev, page, offset,
455 adapter->rx_buffer_len,
457 rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
458 rfd_desc->buf_len = cpu_to_le16(adapter->rx_buffer_len);
459 rfd_desc->coalese = 0;
462 rfd_next_to_use = next_next;
463 if (unlikely(++next_next == rfd_ring->count))
466 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
467 next_info = &rfd_ring->buffer_info[next_next];
473 * Force memory writes to complete before letting h/w
474 * know there are new descriptors to fetch. (Only
475 * applicable for weak-ordered memory model archs,
479 atomic_set(&rfd_ring->next_to_use, (int)rfd_next_to_use);
484 static void atl1_intr_rx(struct atl1_adapter *adapter)
488 u16 rrd_next_to_clean;
490 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
491 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
492 struct atl1_buffer *buffer_info;
493 struct rx_return_desc *rrd;
498 rrd_next_to_clean = atomic_read(&rrd_ring->next_to_clean);
501 rrd = ATL1_RRD_DESC(rrd_ring, rrd_next_to_clean);
503 if (likely(rrd->xsz.valid)) { /* packet valid */
505 /* check rrd status */
506 if (likely(rrd->num_buf == 1))
509 /* rrd seems to be bad */
510 if (unlikely(i-- > 0)) {
511 /* rrd may not be DMAed completely */
513 "%s: RRD may not be DMAed completely\n",
519 printk(KERN_DEBUG "%s: bad RRD\n", atl1_driver_name);
520 /* see if update RFD index */
521 if (rrd->num_buf > 1) {
524 (rrd->xsz.xsum_sz.pkt_size +
525 adapter->rx_buffer_len -
526 1) / adapter->rx_buffer_len;
527 if (rrd->num_buf == num_buf) {
528 /* clean alloc flag for bad rrd */
529 while (rfd_ring->next_to_clean !=
530 (rrd->buf_indx + num_buf)) {
531 rfd_ring->buffer_info[rfd_ring->
532 next_to_clean].alloced = 0;
533 if (++rfd_ring->next_to_clean ==
544 if (++rrd_next_to_clean == rrd_ring->count)
545 rrd_next_to_clean = 0;
548 } else { /* current rrd still not be updated */
553 /* clean alloc flag for bad rrd */
554 while (rfd_ring->next_to_clean != rrd->buf_indx) {
555 rfd_ring->buffer_info[rfd_ring->next_to_clean].alloced =
557 if (++rfd_ring->next_to_clean == rfd_ring->count)
558 rfd_ring->next_to_clean = 0;
561 buffer_info = &rfd_ring->buffer_info[rrd->buf_indx];
562 if (++rfd_ring->next_to_clean == rfd_ring->count)
563 rfd_ring->next_to_clean = 0;
565 /* update rrd next to clean */
566 if (++rrd_next_to_clean == rrd_ring->count)
567 rrd_next_to_clean = 0;
570 if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
572 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM
574 /* packet error, don't need upstream */
575 buffer_info->alloced = 0;
582 pci_unmap_page(adapter->pdev, buffer_info->dma,
583 buffer_info->length, PCI_DMA_FROMDEVICE);
584 skb = buffer_info->skb;
585 length = le16_to_cpu(rrd->xsz.xsum_sz.pkt_size);
587 skb_put(skb, length - ETHERNET_FCS_SIZE);
589 /* Receive Checksum Offload */
590 atl1_rx_checksum(adapter, rrd, skb);
591 skb->protocol = eth_type_trans(skb, adapter->netdev);
593 if (adapter->vlgrp && (rrd->pkt_flg & PACKET_FLAG_VLAN_INS)) {
594 u16 vlan_tag = (rrd->vlan_tag >> 4) |
595 ((rrd->vlan_tag & 7) << 13) |
596 ((rrd->vlan_tag & 8) << 9);
597 vlan_hwaccel_rx(skb, adapter->vlgrp, vlan_tag);
601 /* let protocol layer free skb */
602 buffer_info->skb = NULL;
603 buffer_info->alloced = 0;
606 adapter->netdev->last_rx = jiffies;
609 atomic_set(&rrd_ring->next_to_clean, rrd_next_to_clean);
611 atl1_alloc_rx_buffers(adapter);
613 /* update mailbox ? */
617 u32 rrd_next_to_clean;
619 spin_lock(&adapter->mb_lock);
621 tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
623 atomic_read(&adapter->rfd_ring.next_to_use);
625 atomic_read(&adapter->rrd_ring.next_to_clean);
626 value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
627 MB_RFD_PROD_INDX_SHIFT) |
628 ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
629 MB_RRD_CONS_INDX_SHIFT) |
630 ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
631 MB_TPD_PROD_INDX_SHIFT);
632 iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
633 spin_unlock(&adapter->mb_lock);
637 static void atl1_intr_tx(struct atl1_adapter *adapter)
639 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
640 struct atl1_buffer *buffer_info;
641 u16 sw_tpd_next_to_clean;
642 u16 cmb_tpd_next_to_clean;
645 sw_tpd_next_to_clean = atomic_read(&tpd_ring->next_to_clean);
646 cmb_tpd_next_to_clean = le16_to_cpu(adapter->cmb.cmb->tpd_cons_idx);
648 while (cmb_tpd_next_to_clean != sw_tpd_next_to_clean) {
649 struct tx_packet_desc *tpd;
651 tpd = ATL1_TPD_DESC(tpd_ring, sw_tpd_next_to_clean);
652 buffer_info = &tpd_ring->buffer_info[sw_tpd_next_to_clean];
653 if (buffer_info->dma) {
654 pci_unmap_page(adapter->pdev, buffer_info->dma,
655 buffer_info->length, PCI_DMA_TODEVICE);
656 buffer_info->dma = 0;
659 if (buffer_info->skb) {
660 dev_kfree_skb_irq(buffer_info->skb);
661 buffer_info->skb = NULL;
663 tpd->buffer_addr = 0;
666 if (++sw_tpd_next_to_clean == tpd_ring->count)
667 sw_tpd_next_to_clean = 0;
669 atomic_set(&tpd_ring->next_to_clean, sw_tpd_next_to_clean);
671 if (netif_queue_stopped(adapter->netdev)
672 && netif_carrier_ok(adapter->netdev))
673 netif_wake_queue(adapter->netdev);
676 static void atl1_check_for_link(struct atl1_adapter *adapter)
678 struct net_device *netdev = adapter->netdev;
681 spin_lock(&adapter->lock);
682 adapter->phy_timer_pending = false;
683 atl1_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
684 atl1_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
685 spin_unlock(&adapter->lock);
687 /* notify upper layer link down ASAP */
688 if (!(phy_data & BMSR_LSTATUS)) { /* Link Down */
689 if (netif_carrier_ok(netdev)) { /* old link state: Up */
690 printk(KERN_INFO "%s: %s link is down\n",
691 atl1_driver_name, netdev->name);
692 adapter->link_speed = SPEED_0;
693 netif_carrier_off(netdev);
694 netif_stop_queue(netdev);
697 schedule_work(&adapter->link_chg_task);
701 * atl1_intr - Interrupt Handler
702 * @irq: interrupt number
703 * @data: pointer to a network interface device structure
704 * @pt_regs: CPU registers structure
706 static irqreturn_t atl1_intr(int irq, void *data)
708 /*struct atl1_adapter *adapter = ((struct net_device *)data)->priv;*/
709 struct atl1_adapter *adapter = netdev_priv(data);
714 status = adapter->cmb.cmb->int_stats;
721 /* clear CMB interrupt status at once */
722 adapter->cmb.cmb->int_stats = 0;
724 if (status & ISR_GPHY) /* clear phy status */
725 atl1_clear_phy_int(adapter);
727 /* clear ISR status, and Enable CMB DMA/Disable Interrupt */
728 iowrite32(status | ISR_DIS_INT, adapter->hw.hw_addr + REG_ISR);
730 /* check if SMB intr */
731 if (status & ISR_SMB)
732 atl1_inc_smb(adapter);
734 /* check if PCIE PHY Link down */
735 if (status & ISR_PHY_LINKDOWN) {
736 printk(KERN_DEBUG "%s: pcie phy link down %x\n",
737 atl1_driver_name, status);
738 if (netif_running(adapter->netdev)) { /* reset MAC */
739 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
740 schedule_work(&adapter->pcie_dma_to_rst_task);
745 /* check if DMA read/write error ? */
746 if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
748 "%s: pcie DMA r/w error (status = 0x%x)\n",
749 atl1_driver_name, status);
750 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
751 schedule_work(&adapter->pcie_dma_to_rst_task);
756 if (status & ISR_GPHY) {
757 adapter->soft_stats.tx_carrier_errors++;
758 atl1_check_for_link(adapter);
762 if (status & ISR_CMB_TX)
763 atl1_intr_tx(adapter);
766 if (unlikely(status & (ISR_RXF_OV | ISR_RFD_UNRUN |
767 ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
768 ISR_HOST_RRD_OV | ISR_CMB_RX))) {
770 (ISR_RXF_OV | ISR_RFD_UNRUN | ISR_RRD_OV |
771 ISR_HOST_RFD_UNRUN | ISR_HOST_RRD_OV))
773 "%s: rx exception: status = 0x%x\n",
774 atl1_driver_name, status);
775 atl1_intr_rx(adapter);
781 } while ((status = adapter->cmb.cmb->int_stats));
783 /* re-enable Interrupt */
784 iowrite32(ISR_DIS_SMB | ISR_DIS_DMA, adapter->hw.hw_addr + REG_ISR);
789 * atl1_set_multi - Multicast and Promiscuous mode set
790 * @netdev: network interface device structure
792 * The set_multi entry point is called whenever the multicast address
793 * list or the network interface flags are updated. This routine is
794 * responsible for configuring the hardware for proper multicast,
795 * promiscuous mode, and all-multi behavior.
797 static void atl1_set_multi(struct net_device *netdev)
799 struct atl1_adapter *adapter = netdev_priv(netdev);
800 struct atl1_hw *hw = &adapter->hw;
801 struct dev_mc_list *mc_ptr;
805 /* Check for Promiscuous and All Multicast modes */
806 rctl = ioread32(hw->hw_addr + REG_MAC_CTRL);
807 if (netdev->flags & IFF_PROMISC)
808 rctl |= MAC_CTRL_PROMIS_EN;
809 else if (netdev->flags & IFF_ALLMULTI) {
810 rctl |= MAC_CTRL_MC_ALL_EN;
811 rctl &= ~MAC_CTRL_PROMIS_EN;
813 rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
815 iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL);
817 /* clear the old settings from the multicast hash table */
818 iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
819 iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
821 /* compute mc addresses' hash value ,and put it into hash table */
822 for (mc_ptr = netdev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
823 hash_value = atl1_hash_mc_addr(hw, mc_ptr->dmi_addr);
824 atl1_hash_set(hw, hash_value);
828 static void atl1_setup_mac_ctrl(struct atl1_adapter *adapter)
831 struct atl1_hw *hw = &adapter->hw;
832 struct net_device *netdev = adapter->netdev;
833 /* Config MAC CTRL Register */
834 value = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN;
836 if (FULL_DUPLEX == adapter->link_duplex)
837 value |= MAC_CTRL_DUPLX;
839 value |= ((u32) ((SPEED_1000 == adapter->link_speed) ?
840 MAC_CTRL_SPEED_1000 : MAC_CTRL_SPEED_10_100) <<
841 MAC_CTRL_SPEED_SHIFT);
843 value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
845 value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
846 /* preamble length */
847 value |= (((u32) adapter->hw.preamble_len
848 & MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
851 value |= MAC_CTRL_RMV_VLAN;
853 if (adapter->rx_csum)
854 value |= MAC_CTRL_RX_CHKSUM_EN;
857 value |= MAC_CTRL_BC_EN;
858 if (netdev->flags & IFF_PROMISC)
859 value |= MAC_CTRL_PROMIS_EN;
860 else if (netdev->flags & IFF_ALLMULTI)
861 value |= MAC_CTRL_MC_ALL_EN;
862 /* value |= MAC_CTRL_LOOPBACK; */
863 iowrite32(value, hw->hw_addr + REG_MAC_CTRL);
866 static u32 atl1_check_link(struct atl1_adapter *adapter)
868 struct atl1_hw *hw = &adapter->hw;
869 struct net_device *netdev = adapter->netdev;
871 u16 speed, duplex, phy_data;
874 /* MII_BMSR must read twice */
875 atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
876 atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
877 if (!(phy_data & BMSR_LSTATUS)) { /* link down */
878 if (netif_carrier_ok(netdev)) { /* old link state: Up */
879 printk(KERN_INFO "%s: link is down\n",
881 adapter->link_speed = SPEED_0;
882 netif_carrier_off(netdev);
883 netif_stop_queue(netdev);
889 ret_val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
893 switch (hw->media_type) {
894 case MEDIA_TYPE_1000M_FULL:
895 if (speed != SPEED_1000 || duplex != FULL_DUPLEX)
898 case MEDIA_TYPE_100M_FULL:
899 if (speed != SPEED_100 || duplex != FULL_DUPLEX)
902 case MEDIA_TYPE_100M_HALF:
903 if (speed != SPEED_100 || duplex != HALF_DUPLEX)
906 case MEDIA_TYPE_10M_FULL:
907 if (speed != SPEED_10 || duplex != FULL_DUPLEX)
910 case MEDIA_TYPE_10M_HALF:
911 if (speed != SPEED_10 || duplex != HALF_DUPLEX)
916 /* link result is our setting */
918 if (adapter->link_speed != speed
919 || adapter->link_duplex != duplex) {
920 adapter->link_speed = speed;
921 adapter->link_duplex = duplex;
922 atl1_setup_mac_ctrl(adapter);
923 printk(KERN_INFO "%s: %s link is up %d Mbps %s\n",
924 atl1_driver_name, netdev->name,
926 adapter->link_duplex ==
927 FULL_DUPLEX ? "full duplex" : "half duplex");
929 if (!netif_carrier_ok(netdev)) { /* Link down -> Up */
930 netif_carrier_on(netdev);
931 netif_wake_queue(netdev);
936 /* change orignal link status */
937 if (netif_carrier_ok(netdev)) {
938 adapter->link_speed = SPEED_0;
939 netif_carrier_off(netdev);
940 netif_stop_queue(netdev);
943 if (hw->media_type != MEDIA_TYPE_AUTO_SENSOR &&
944 hw->media_type != MEDIA_TYPE_1000M_FULL) {
945 switch (hw->media_type) {
946 case MEDIA_TYPE_100M_FULL:
947 phy_data = MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
950 case MEDIA_TYPE_100M_HALF:
951 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
953 case MEDIA_TYPE_10M_FULL:
955 MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
957 default: /* MEDIA_TYPE_10M_HALF: */
958 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
961 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
965 /* auto-neg, insert timer to re-config phy */
966 if (!adapter->phy_timer_pending) {
967 adapter->phy_timer_pending = true;
968 mod_timer(&adapter->phy_config_timer, jiffies + 3 * HZ);
974 static void set_flow_ctrl_old(struct atl1_adapter *adapter)
978 /* RFD Flow Control */
979 value = adapter->rfd_ring.count;
985 value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
986 ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
987 iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
989 /* RRD Flow Control */
990 value = adapter->rrd_ring.count;
995 value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
996 ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
997 iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1000 static void set_flow_ctrl_new(struct atl1_hw *hw)
1004 /* RXF Flow Control */
1005 value = ioread32(hw->hw_addr + REG_SRAM_RXF_LEN);
1012 value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
1013 ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
1014 iowrite32(value, hw->hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
1016 /* RRD Flow Control */
1017 value = ioread32(hw->hw_addr + REG_SRAM_RRD_LEN);
1024 value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
1025 ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
1026 iowrite32(value, hw->hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1030 * atl1_configure - Configure Transmit&Receive Unit after Reset
1031 * @adapter: board private structure
1033 * Configure the Tx /Rx unit of the MAC after a reset.
1035 static u32 atl1_configure(struct atl1_adapter *adapter)
1037 struct atl1_hw *hw = &adapter->hw;
1040 /* clear interrupt status */
1041 iowrite32(0xffffffff, adapter->hw.hw_addr + REG_ISR);
1043 /* set MAC Address */
1044 value = (((u32) hw->mac_addr[2]) << 24) |
1045 (((u32) hw->mac_addr[3]) << 16) |
1046 (((u32) hw->mac_addr[4]) << 8) |
1047 (((u32) hw->mac_addr[5]));
1048 iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
1049 value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
1050 iowrite32(value, hw->hw_addr + (REG_MAC_STA_ADDR + 4));
1054 /* HI base address */
1055 iowrite32((u32) ((adapter->tpd_ring.dma & 0xffffffff00000000ULL) >> 32),
1056 hw->hw_addr + REG_DESC_BASE_ADDR_HI);
1057 /* LO base address */
1058 iowrite32((u32) (adapter->rfd_ring.dma & 0x00000000ffffffffULL),
1059 hw->hw_addr + REG_DESC_RFD_ADDR_LO);
1060 iowrite32((u32) (adapter->rrd_ring.dma & 0x00000000ffffffffULL),
1061 hw->hw_addr + REG_DESC_RRD_ADDR_LO);
1062 iowrite32((u32) (adapter->tpd_ring.dma & 0x00000000ffffffffULL),
1063 hw->hw_addr + REG_DESC_TPD_ADDR_LO);
1064 iowrite32((u32) (adapter->cmb.dma & 0x00000000ffffffffULL),
1065 hw->hw_addr + REG_DESC_CMB_ADDR_LO);
1066 iowrite32((u32) (adapter->smb.dma & 0x00000000ffffffffULL),
1067 hw->hw_addr + REG_DESC_SMB_ADDR_LO);
1070 value = adapter->rrd_ring.count;
1072 value += adapter->rfd_ring.count;
1073 iowrite32(value, hw->hw_addr + REG_DESC_RFD_RRD_RING_SIZE);
1074 iowrite32(adapter->tpd_ring.count, hw->hw_addr + REG_DESC_TPD_RING_SIZE);
1077 iowrite32(1, hw->hw_addr + REG_LOAD_PTR);
1079 /* config Mailbox */
1080 value = ((atomic_read(&adapter->tpd_ring.next_to_use)
1081 & MB_TPD_PROD_INDX_MASK) << MB_TPD_PROD_INDX_SHIFT) |
1082 ((atomic_read(&adapter->rrd_ring.next_to_clean)
1083 & MB_RRD_CONS_INDX_MASK) << MB_RRD_CONS_INDX_SHIFT) |
1084 ((atomic_read(&adapter->rfd_ring.next_to_use)
1085 & MB_RFD_PROD_INDX_MASK) << MB_RFD_PROD_INDX_SHIFT);
1086 iowrite32(value, hw->hw_addr + REG_MAILBOX);
1088 /* config IPG/IFG */
1089 value = (((u32) hw->ipgt & MAC_IPG_IFG_IPGT_MASK)
1090 << MAC_IPG_IFG_IPGT_SHIFT) |
1091 (((u32) hw->min_ifg & MAC_IPG_IFG_MIFG_MASK)
1092 << MAC_IPG_IFG_MIFG_SHIFT) |
1093 (((u32) hw->ipgr1 & MAC_IPG_IFG_IPGR1_MASK)
1094 << MAC_IPG_IFG_IPGR1_SHIFT) |
1095 (((u32) hw->ipgr2 & MAC_IPG_IFG_IPGR2_MASK)
1096 << MAC_IPG_IFG_IPGR2_SHIFT);
1097 iowrite32(value, hw->hw_addr + REG_MAC_IPG_IFG);
1099 /* config Half-Duplex Control */
1100 value = ((u32) hw->lcol & MAC_HALF_DUPLX_CTRL_LCOL_MASK) |
1101 (((u32) hw->max_retry & MAC_HALF_DUPLX_CTRL_RETRY_MASK)
1102 << MAC_HALF_DUPLX_CTRL_RETRY_SHIFT) |
1103 MAC_HALF_DUPLX_CTRL_EXC_DEF_EN |
1104 (0xa << MAC_HALF_DUPLX_CTRL_ABEBT_SHIFT) |
1105 (((u32) hw->jam_ipg & MAC_HALF_DUPLX_CTRL_JAMIPG_MASK)
1106 << MAC_HALF_DUPLX_CTRL_JAMIPG_SHIFT);
1107 iowrite32(value, hw->hw_addr + REG_MAC_HALF_DUPLX_CTRL);
1109 /* set Interrupt Moderator Timer */
1110 iowrite16(adapter->imt, hw->hw_addr + REG_IRQ_MODU_TIMER_INIT);
1111 iowrite32(MASTER_CTRL_ITIMER_EN, hw->hw_addr + REG_MASTER_CTRL);
1113 /* set Interrupt Clear Timer */
1114 iowrite16(adapter->ict, hw->hw_addr + REG_CMBDISDMA_TIMER);
1116 /* set MTU, 4 : VLAN */
1117 iowrite32(hw->max_frame_size + 4, hw->hw_addr + REG_MTU);
1119 /* jumbo size & rrd retirement timer */
1120 value = (((u32) hw->rx_jumbo_th & RXQ_JMBOSZ_TH_MASK)
1121 << RXQ_JMBOSZ_TH_SHIFT) |
1122 (((u32) hw->rx_jumbo_lkah & RXQ_JMBO_LKAH_MASK)
1123 << RXQ_JMBO_LKAH_SHIFT) |
1124 (((u32) hw->rrd_ret_timer & RXQ_RRD_TIMER_MASK)
1125 << RXQ_RRD_TIMER_SHIFT);
1126 iowrite32(value, hw->hw_addr + REG_RXQ_JMBOSZ_RRDTIM);
1129 switch (hw->dev_rev) {
1134 set_flow_ctrl_old(adapter);
1137 set_flow_ctrl_new(hw);
1142 value = (((u32) hw->tpd_burst & TXQ_CTRL_TPD_BURST_NUM_MASK)
1143 << TXQ_CTRL_TPD_BURST_NUM_SHIFT) |
1144 (((u32) hw->txf_burst & TXQ_CTRL_TXF_BURST_NUM_MASK)
1145 << TXQ_CTRL_TXF_BURST_NUM_SHIFT) |
1146 (((u32) hw->tpd_fetch_th & TXQ_CTRL_TPD_FETCH_TH_MASK)
1147 << TXQ_CTRL_TPD_FETCH_TH_SHIFT) | TXQ_CTRL_ENH_MODE | TXQ_CTRL_EN;
1148 iowrite32(value, hw->hw_addr + REG_TXQ_CTRL);
1150 /* min tpd fetch gap & tx jumbo packet size threshold for taskoffload */
1151 value = (((u32) hw->tx_jumbo_task_th & TX_JUMBO_TASK_TH_MASK)
1152 << TX_JUMBO_TASK_TH_SHIFT) |
1153 (((u32) hw->tpd_fetch_gap & TX_TPD_MIN_IPG_MASK)
1154 << TX_TPD_MIN_IPG_SHIFT);
1155 iowrite32(value, hw->hw_addr + REG_TX_JUMBO_TASK_TH_TPD_IPG);
1158 value = (((u32) hw->rfd_burst & RXQ_CTRL_RFD_BURST_NUM_MASK)
1159 << RXQ_CTRL_RFD_BURST_NUM_SHIFT) |
1160 (((u32) hw->rrd_burst & RXQ_CTRL_RRD_BURST_THRESH_MASK)
1161 << RXQ_CTRL_RRD_BURST_THRESH_SHIFT) |
1162 (((u32) hw->rfd_fetch_gap & RXQ_CTRL_RFD_PREF_MIN_IPG_MASK)
1163 << RXQ_CTRL_RFD_PREF_MIN_IPG_SHIFT) |
1164 RXQ_CTRL_CUT_THRU_EN | RXQ_CTRL_EN;
1165 iowrite32(value, hw->hw_addr + REG_RXQ_CTRL);
1167 /* config DMA Engine */
1168 value = ((((u32) hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
1169 << DMA_CTRL_DMAR_BURST_LEN_SHIFT) |
1170 ((((u32) hw->dmaw_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
1171 << DMA_CTRL_DMAR_BURST_LEN_SHIFT) |
1172 DMA_CTRL_DMAR_EN | DMA_CTRL_DMAW_EN;
1173 value |= (u32) hw->dma_ord;
1174 if (atl1_rcb_128 == hw->rcb_value)
1175 value |= DMA_CTRL_RCB_VALUE;
1176 iowrite32(value, hw->hw_addr + REG_DMA_CTRL);
1178 /* config CMB / SMB */
1179 value = hw->cmb_rrd | ((u32) hw->cmb_tpd << 16);
1180 iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TH);
1181 value = hw->cmb_rx_timer | ((u32) hw->cmb_tx_timer << 16);
1182 iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TIMER);
1183 iowrite32(hw->smb_timer, hw->hw_addr + REG_SMB_TIMER);
1185 /* --- enable CMB / SMB */
1186 value = CSMB_CTRL_CMB_EN | CSMB_CTRL_SMB_EN;
1187 iowrite32(value, hw->hw_addr + REG_CSMB_CTRL);
1189 value = ioread32(adapter->hw.hw_addr + REG_ISR);
1190 if (unlikely((value & ISR_PHY_LINKDOWN) != 0))
1191 value = 1; /* config failed */
1195 /* clear all interrupt status */
1196 iowrite32(0x3fffffff, adapter->hw.hw_addr + REG_ISR);
1197 iowrite32(0, adapter->hw.hw_addr + REG_ISR);
1202 * atl1_irq_disable - Mask off interrupt generation on the NIC
1203 * @adapter: board private structure
1205 static void atl1_irq_disable(struct atl1_adapter *adapter)
1207 atomic_inc(&adapter->irq_sem);
1208 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
1209 ioread32(adapter->hw.hw_addr + REG_IMR);
1210 synchronize_irq(adapter->pdev->irq);
1213 static void atl1_vlan_rx_register(struct net_device *netdev,
1214 struct vlan_group *grp)
1216 struct atl1_adapter *adapter = netdev_priv(netdev);
1217 unsigned long flags;
1220 spin_lock_irqsave(&adapter->lock, flags);
1221 /* atl1_irq_disable(adapter); */
1222 adapter->vlgrp = grp;
1225 /* enable VLAN tag insert/strip */
1226 ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
1227 ctrl |= MAC_CTRL_RMV_VLAN;
1228 iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
1230 /* disable VLAN tag insert/strip */
1231 ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
1232 ctrl &= ~MAC_CTRL_RMV_VLAN;
1233 iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
1236 /* atl1_irq_enable(adapter); */
1237 spin_unlock_irqrestore(&adapter->lock, flags);
1240 /* FIXME: justify or remove -- CHS */
1241 static void atl1_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1243 /* We don't do Vlan filtering */
1247 /* FIXME: this looks wrong too -- CHS */
1248 static void atl1_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1250 struct atl1_adapter *adapter = netdev_priv(netdev);
1251 unsigned long flags;
1253 spin_lock_irqsave(&adapter->lock, flags);
1254 /* atl1_irq_disable(adapter); */
1255 vlan_group_set_device(adapter->vlgrp, vid, NULL);
1256 /* atl1_irq_enable(adapter); */
1257 spin_unlock_irqrestore(&adapter->lock, flags);
1258 /* We don't do Vlan filtering */
1262 static void atl1_restore_vlan(struct atl1_adapter *adapter)
1264 atl1_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1265 if (adapter->vlgrp) {
1267 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1268 if (!vlan_group_get_device(adapter->vlgrp, vid))
1270 atl1_vlan_rx_add_vid(adapter->netdev, vid);
1275 static u16 tpd_avail(struct atl1_tpd_ring *tpd_ring)
1277 u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1278 u16 next_to_use = atomic_read(&tpd_ring->next_to_use);
1279 return ((next_to_clean >
1280 next_to_use) ? next_to_clean - next_to_use -
1281 1 : tpd_ring->count + next_to_clean - next_to_use - 1);
1284 static int atl1_tso(struct atl1_adapter *adapter, struct sk_buff *skb,
1285 struct tso_param *tso)
1287 /* We enter this function holding a spinlock. */
1291 if (skb_shinfo(skb)->gso_size) {
1292 if (skb_header_cloned(skb)) {
1293 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1298 if (skb->protocol == ntohs(ETH_P_IP)) {
1299 skb->nh.iph->tot_len = 0;
1300 skb->nh.iph->check = 0;
1302 ~csum_tcpudp_magic(skb->nh.iph->saddr,
1303 skb->nh.iph->daddr, 0,
1305 ipofst = skb->nh.raw - skb->data;
1306 if (ipofst != ENET_HEADER_SIZE) /* 802.3 frame */
1307 tso->tsopl |= 1 << TSO_PARAM_ETHTYPE_SHIFT;
1309 tso->tsopl |= (skb->nh.iph->ihl &
1310 CSUM_PARAM_IPHL_MASK) << CSUM_PARAM_IPHL_SHIFT;
1311 tso->tsopl |= ((skb->h.th->doff << 2) &
1312 TSO_PARAM_TCPHDRLEN_MASK) << TSO_PARAM_TCPHDRLEN_SHIFT;
1313 tso->tsopl |= (skb_shinfo(skb)->gso_size &
1314 TSO_PARAM_MSS_MASK) << TSO_PARAM_MSS_SHIFT;
1315 tso->tsopl |= 1 << TSO_PARAM_IPCKSUM_SHIFT;
1316 tso->tsopl |= 1 << TSO_PARAM_TCPCKSUM_SHIFT;
1317 tso->tsopl |= 1 << TSO_PARAM_SEGMENT_SHIFT;
1324 static int atl1_tx_csum(struct atl1_adapter *adapter, struct sk_buff *skb,
1325 struct csum_param *csum)
1329 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1330 cso = skb->h.raw - skb->data;
1331 css = (skb->h.raw + skb->csum) - skb->data;
1332 if (unlikely(cso & 0x1)) {
1333 printk(KERN_DEBUG "%s: payload offset != even number\n",
1337 csum->csumpl |= (cso & CSUM_PARAM_PLOADOFFSET_MASK) <<
1338 CSUM_PARAM_PLOADOFFSET_SHIFT;
1339 csum->csumpl |= (css & CSUM_PARAM_XSUMOFFSET_MASK) <<
1340 CSUM_PARAM_XSUMOFFSET_SHIFT;
1341 csum->csumpl |= 1 << CSUM_PARAM_CUSTOMCKSUM_SHIFT;
1348 static void atl1_tx_map(struct atl1_adapter *adapter,
1349 struct sk_buff *skb, bool tcp_seg)
1351 /* We enter this function holding a spinlock. */
1352 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1353 struct atl1_buffer *buffer_info;
1355 int first_buf_len = skb->len;
1356 unsigned long offset;
1357 unsigned int nr_frags;
1359 u16 tpd_next_to_use;
1363 first_buf_len -= skb->data_len;
1364 nr_frags = skb_shinfo(skb)->nr_frags;
1365 tpd_next_to_use = atomic_read(&tpd_ring->next_to_use);
1366 buffer_info = &tpd_ring->buffer_info[tpd_next_to_use];
1367 if (unlikely(buffer_info->skb))
1369 buffer_info->skb = NULL; /* put skb in last TPD */
1374 ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
1375 buffer_info->length = proto_hdr_len;
1376 page = virt_to_page(skb->data);
1377 offset = (unsigned long)skb->data & ~PAGE_MASK;
1378 buffer_info->dma = pci_map_page(adapter->pdev, page,
1379 offset, proto_hdr_len,
1382 if (++tpd_next_to_use == tpd_ring->count)
1383 tpd_next_to_use = 0;
1385 if (first_buf_len > proto_hdr_len) {
1386 len12 = first_buf_len - proto_hdr_len;
1387 m = (len12 + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1388 for (i = 0; i < m; i++) {
1390 &tpd_ring->buffer_info[tpd_next_to_use];
1391 buffer_info->skb = NULL;
1392 buffer_info->length =
1394 len12) ? MAX_TX_BUF_LEN : len12;
1395 len12 -= buffer_info->length;
1396 page = virt_to_page(skb->data +
1398 i * MAX_TX_BUF_LEN));
1399 offset = (unsigned long)(skb->data +
1401 i * MAX_TX_BUF_LEN)) &
1404 pci_map_page(adapter->pdev, page, offset,
1405 buffer_info->length,
1407 if (++tpd_next_to_use == tpd_ring->count)
1408 tpd_next_to_use = 0;
1413 buffer_info->length = first_buf_len;
1414 page = virt_to_page(skb->data);
1415 offset = (unsigned long)skb->data & ~PAGE_MASK;
1416 buffer_info->dma = pci_map_page(adapter->pdev, page,
1417 offset, first_buf_len,
1419 if (++tpd_next_to_use == tpd_ring->count)
1420 tpd_next_to_use = 0;
1423 for (f = 0; f < nr_frags; f++) {
1424 struct skb_frag_struct *frag;
1427 frag = &skb_shinfo(skb)->frags[f];
1430 m = (lenf + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1431 for (i = 0; i < m; i++) {
1432 buffer_info = &tpd_ring->buffer_info[tpd_next_to_use];
1433 if (unlikely(buffer_info->skb))
1435 buffer_info->skb = NULL;
1436 buffer_info->length =
1437 (lenf > MAX_TX_BUF_LEN) ? MAX_TX_BUF_LEN : lenf;
1438 lenf -= buffer_info->length;
1440 pci_map_page(adapter->pdev, frag->page,
1441 frag->page_offset + i * MAX_TX_BUF_LEN,
1442 buffer_info->length, PCI_DMA_TODEVICE);
1444 if (++tpd_next_to_use == tpd_ring->count)
1445 tpd_next_to_use = 0;
1449 /* last tpd's buffer-info */
1450 buffer_info->skb = skb;
1453 static void atl1_tx_queue(struct atl1_adapter *adapter, int count,
1454 union tpd_descr *descr)
1456 /* We enter this function holding a spinlock. */
1457 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1460 struct atl1_buffer *buffer_info;
1461 struct tx_packet_desc *tpd;
1462 u16 tpd_next_to_use = atomic_read(&tpd_ring->next_to_use);
1464 for (j = 0; j < count; j++) {
1465 buffer_info = &tpd_ring->buffer_info[tpd_next_to_use];
1466 tpd = ATL1_TPD_DESC(&adapter->tpd_ring, tpd_next_to_use);
1467 tpd->desc.csum.csumpu = descr->csum.csumpu;
1468 tpd->desc.csum.csumpl = descr->csum.csumpl;
1469 tpd->desc.tso.tsopu = descr->tso.tsopu;
1470 tpd->desc.tso.tsopl = descr->tso.tsopl;
1471 tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
1472 tpd->desc.data = descr->data;
1473 tpd->desc.csum.csumpu |= (cpu_to_le16(buffer_info->length) &
1474 CSUM_PARAM_BUFLEN_MASK) << CSUM_PARAM_BUFLEN_SHIFT;
1476 val = (descr->tso.tsopl >> TSO_PARAM_SEGMENT_SHIFT) &
1477 TSO_PARAM_SEGMENT_MASK;
1479 tpd->desc.tso.tsopl |= 1 << TSO_PARAM_HDRFLAG_SHIFT;
1481 if (j == (count - 1))
1482 tpd->desc.csum.csumpl |= 1 << CSUM_PARAM_EOP_SHIFT;
1484 if (++tpd_next_to_use == tpd_ring->count)
1485 tpd_next_to_use = 0;
1488 * Force memory writes to complete before letting h/w
1489 * know there are new descriptors to fetch. (Only
1490 * applicable for weak-ordered memory model archs,
1495 atomic_set(&tpd_ring->next_to_use, (int)tpd_next_to_use);
1498 static void atl1_update_mailbox(struct atl1_adapter *adapter)
1500 unsigned long flags;
1501 u32 tpd_next_to_use;
1502 u32 rfd_next_to_use;
1503 u32 rrd_next_to_clean;
1506 spin_lock_irqsave(&adapter->mb_lock, flags);
1508 tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
1509 rfd_next_to_use = atomic_read(&adapter->rfd_ring.next_to_use);
1510 rrd_next_to_clean = atomic_read(&adapter->rrd_ring.next_to_clean);
1512 value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
1513 MB_RFD_PROD_INDX_SHIFT) |
1514 ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
1515 MB_RRD_CONS_INDX_SHIFT) |
1516 ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
1517 MB_TPD_PROD_INDX_SHIFT);
1518 iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
1520 spin_unlock_irqrestore(&adapter->mb_lock, flags);
1523 static int atl1_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1525 struct atl1_adapter *adapter = netdev_priv(netdev);
1531 union tpd_descr param;
1534 unsigned long flags;
1535 unsigned int nr_frags = 0;
1536 unsigned int mss = 0;
1538 unsigned int proto_hdr_len;
1540 len -= skb->data_len;
1542 if (unlikely(skb->len == 0)) {
1543 dev_kfree_skb_any(skb);
1544 return NETDEV_TX_OK;
1548 param.tso.tsopu = 0;
1549 param.tso.tsopl = 0;
1550 param.csum.csumpu = 0;
1551 param.csum.csumpl = 0;
1553 /* nr_frags will be nonzero if we're doing scatter/gather (SG) */
1554 nr_frags = skb_shinfo(skb)->nr_frags;
1555 for (f = 0; f < nr_frags; f++) {
1556 frag_size = skb_shinfo(skb)->frags[f].size;
1559 (frag_size + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1562 /* mss will be nonzero if we're doing segment offload (TSO/GSO) */
1563 mss = skb_shinfo(skb)->gso_size;
1565 if (skb->protocol == ntohs(ETH_P_IP)) {
1566 proto_hdr_len = ((skb->h.raw - skb->data) +
1567 (skb->h.th->doff << 2));
1568 if (unlikely(proto_hdr_len > len)) {
1569 dev_kfree_skb_any(skb);
1570 return NETDEV_TX_OK;
1572 /* need additional TPD ? */
1573 if (proto_hdr_len != len)
1574 count += (len - proto_hdr_len +
1575 MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1579 local_irq_save(flags);
1580 if (!spin_trylock(&adapter->lock)) {
1581 /* Can't get lock - tell upper layer to requeue */
1582 local_irq_restore(flags);
1583 printk(KERN_DEBUG "%s: TX locked\n", atl1_driver_name);
1584 return NETDEV_TX_LOCKED;
1587 if (tpd_avail(&adapter->tpd_ring) < count) {
1588 /* not enough descriptors */
1589 netif_stop_queue(netdev);
1590 spin_unlock_irqrestore(&adapter->lock, flags);
1591 printk(KERN_DEBUG "%s: TX busy\n", atl1_driver_name);
1592 return NETDEV_TX_BUSY;
1597 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
1598 vlan_tag = vlan_tx_tag_get(skb);
1599 vlan_tag = (vlan_tag << 4) | (vlan_tag >> 13) |
1600 ((vlan_tag >> 9) & 0x8);
1601 param.csum.csumpl |= 1 << CSUM_PARAM_INSVLAG_SHIFT;
1602 param.csum.csumpu |= (vlan_tag & CSUM_PARAM_VALANTAG_MASK) <<
1603 CSUM_PARAM_VALAN_SHIFT;
1606 tso = atl1_tso(adapter, skb, ¶m.tso);
1608 spin_unlock_irqrestore(&adapter->lock, flags);
1609 dev_kfree_skb_any(skb);
1610 return NETDEV_TX_OK;
1614 ret_val = atl1_tx_csum(adapter, skb, ¶m.csum);
1616 spin_unlock_irqrestore(&adapter->lock, flags);
1617 dev_kfree_skb_any(skb);
1618 return NETDEV_TX_OK;
1622 val = (param.csum.csumpl >> CSUM_PARAM_SEGMENT_SHIFT) &
1623 CSUM_PARAM_SEGMENT_MASK;
1624 atl1_tx_map(adapter, skb, 1 == val);
1625 atl1_tx_queue(adapter, count, ¶m);
1626 netdev->trans_start = jiffies;
1627 spin_unlock_irqrestore(&adapter->lock, flags);
1628 atl1_update_mailbox(adapter);
1629 return NETDEV_TX_OK;
1633 * atl1_get_stats - Get System Network Statistics
1634 * @netdev: network interface device structure
1636 * Returns the address of the device statistics structure.
1637 * The statistics are actually updated from the timer callback.
1639 static struct net_device_stats *atl1_get_stats(struct net_device *netdev)
1641 struct atl1_adapter *adapter = netdev_priv(netdev);
1642 return &adapter->net_stats;
1646 * atl1_clean_rx_ring - Free RFD Buffers
1647 * @adapter: board private structure
1649 static void atl1_clean_rx_ring(struct atl1_adapter *adapter)
1651 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1652 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1653 struct atl1_buffer *buffer_info;
1654 struct pci_dev *pdev = adapter->pdev;
1658 /* Free all the Rx ring sk_buffs */
1659 for (i = 0; i < rfd_ring->count; i++) {
1660 buffer_info = &rfd_ring->buffer_info[i];
1661 if (buffer_info->dma) {
1662 pci_unmap_page(pdev,
1664 buffer_info->length,
1665 PCI_DMA_FROMDEVICE);
1666 buffer_info->dma = 0;
1668 if (buffer_info->skb) {
1669 dev_kfree_skb(buffer_info->skb);
1670 buffer_info->skb = NULL;
1674 size = sizeof(struct atl1_buffer) * rfd_ring->count;
1675 memset(rfd_ring->buffer_info, 0, size);
1677 /* Zero out the descriptor ring */
1678 memset(rfd_ring->desc, 0, rfd_ring->size);
1680 rfd_ring->next_to_clean = 0;
1681 atomic_set(&rfd_ring->next_to_use, 0);
1683 rrd_ring->next_to_use = 0;
1684 atomic_set(&rrd_ring->next_to_clean, 0);
1688 * atl1_clean_tx_ring - Free Tx Buffers
1689 * @adapter: board private structure
1691 static void atl1_clean_tx_ring(struct atl1_adapter *adapter)
1693 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1694 struct atl1_buffer *buffer_info;
1695 struct pci_dev *pdev = adapter->pdev;
1699 /* Free all the Tx ring sk_buffs */
1700 for (i = 0; i < tpd_ring->count; i++) {
1701 buffer_info = &tpd_ring->buffer_info[i];
1702 if (buffer_info->dma) {
1703 pci_unmap_page(pdev, buffer_info->dma,
1704 buffer_info->length, PCI_DMA_TODEVICE);
1705 buffer_info->dma = 0;
1709 for (i = 0; i < tpd_ring->count; i++) {
1710 buffer_info = &tpd_ring->buffer_info[i];
1711 if (buffer_info->skb) {
1712 dev_kfree_skb_any(buffer_info->skb);
1713 buffer_info->skb = NULL;
1717 size = sizeof(struct atl1_buffer) * tpd_ring->count;
1718 memset(tpd_ring->buffer_info, 0, size);
1720 /* Zero out the descriptor ring */
1721 memset(tpd_ring->desc, 0, tpd_ring->size);
1723 atomic_set(&tpd_ring->next_to_use, 0);
1724 atomic_set(&tpd_ring->next_to_clean, 0);
1728 * atl1_free_ring_resources - Free Tx / RX descriptor Resources
1729 * @adapter: board private structure
1731 * Free all transmit software resources
1733 void atl1_free_ring_resources(struct atl1_adapter *adapter)
1735 struct pci_dev *pdev = adapter->pdev;
1736 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1737 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1738 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1739 struct atl1_ring_header *ring_header = &adapter->ring_header;
1741 atl1_clean_tx_ring(adapter);
1742 atl1_clean_rx_ring(adapter);
1744 kfree(tpd_ring->buffer_info);
1745 pci_free_consistent(pdev, ring_header->size, ring_header->desc,
1748 tpd_ring->buffer_info = NULL;
1749 tpd_ring->desc = NULL;
1752 rfd_ring->buffer_info = NULL;
1753 rfd_ring->desc = NULL;
1756 rrd_ring->desc = NULL;
1760 s32 atl1_up(struct atl1_adapter *adapter)
1762 struct net_device *netdev = adapter->netdev;
1764 int irq_flags = IRQF_SAMPLE_RANDOM;
1766 /* hardware has been reset, we need to reload some things */
1767 atl1_set_multi(netdev);
1768 atl1_restore_vlan(adapter);
1769 err = atl1_alloc_rx_buffers(adapter);
1770 if (unlikely(!err)) /* no RX BUFFER allocated */
1773 if (unlikely(atl1_configure(adapter))) {
1778 err = pci_enable_msi(adapter->pdev);
1780 dev_info(&adapter->pdev->dev,
1781 "Unable to enable MSI: %d\n", err);
1782 irq_flags |= IRQF_SHARED;
1785 err = request_irq(adapter->pdev->irq, &atl1_intr, irq_flags,
1786 netdev->name, netdev);
1790 mod_timer(&adapter->watchdog_timer, jiffies);
1791 atl1_irq_enable(adapter);
1792 atl1_check_link(adapter);
1795 /* FIXME: unreachable code! -- CHS */
1796 /* free irq disable any interrupt */
1797 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
1798 free_irq(adapter->pdev->irq, netdev);
1801 pci_disable_msi(adapter->pdev);
1802 /* free rx_buffers */
1803 atl1_clean_rx_ring(adapter);
1807 void atl1_down(struct atl1_adapter *adapter)
1809 struct net_device *netdev = adapter->netdev;
1811 del_timer_sync(&adapter->watchdog_timer);
1812 del_timer_sync(&adapter->phy_config_timer);
1813 adapter->phy_timer_pending = false;
1815 atl1_irq_disable(adapter);
1816 free_irq(adapter->pdev->irq, netdev);
1817 pci_disable_msi(adapter->pdev);
1818 atl1_reset_hw(&adapter->hw);
1819 adapter->cmb.cmb->int_stats = 0;
1821 adapter->link_speed = SPEED_0;
1822 adapter->link_duplex = -1;
1823 netif_carrier_off(netdev);
1824 netif_stop_queue(netdev);
1826 atl1_clean_tx_ring(adapter);
1827 atl1_clean_rx_ring(adapter);
1831 * atl1_change_mtu - Change the Maximum Transfer Unit
1832 * @netdev: network interface device structure
1833 * @new_mtu: new value for maximum frame size
1835 * Returns 0 on success, negative on failure
1837 static int atl1_change_mtu(struct net_device *netdev, int new_mtu)
1839 struct atl1_adapter *adapter = netdev_priv(netdev);
1840 int old_mtu = netdev->mtu;
1841 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
1843 if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
1844 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
1845 printk(KERN_WARNING "%s: invalid MTU setting\n",
1850 adapter->hw.max_frame_size = max_frame;
1851 adapter->hw.tx_jumbo_task_th = (max_frame + 7) >> 3;
1852 adapter->rx_buffer_len = (max_frame + 7) & ~7;
1853 adapter->hw.rx_jumbo_th = adapter->rx_buffer_len / 8;
1855 netdev->mtu = new_mtu;
1856 if ((old_mtu != new_mtu) && netif_running(netdev)) {
1865 * atl1_set_mac - Change the Ethernet Address of the NIC
1866 * @netdev: network interface device structure
1867 * @p: pointer to an address structure
1869 * Returns 0 on success, negative on failure
1871 static int atl1_set_mac(struct net_device *netdev, void *p)
1873 struct atl1_adapter *adapter = netdev_priv(netdev);
1874 struct sockaddr *addr = p;
1876 if (netif_running(netdev))
1879 if (!is_valid_ether_addr(addr->sa_data))
1880 return -EADDRNOTAVAIL;
1882 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1883 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
1885 atl1_set_mac_addr(&adapter->hw);
1890 * atl1_watchdog - Timer Call-back
1891 * @data: pointer to netdev cast into an unsigned long
1893 static void atl1_watchdog(unsigned long data)
1895 struct atl1_adapter *adapter = (struct atl1_adapter *)data;
1897 /* Reset the timer */
1898 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1901 static int mdio_read(struct net_device *netdev, int phy_id, int reg_num)
1903 struct atl1_adapter *adapter = netdev_priv(netdev);
1906 atl1_read_phy_reg(&adapter->hw, reg_num & 0x1f, &result);
1911 static void mdio_write(struct net_device *netdev, int phy_id, int reg_num, int val)
1913 struct atl1_adapter *adapter = netdev_priv(netdev);
1915 atl1_write_phy_reg(&adapter->hw, reg_num, val);
1924 static int atl1_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1926 struct atl1_adapter *adapter = netdev_priv(netdev);
1927 unsigned long flags;
1930 if (!netif_running(netdev))
1933 spin_lock_irqsave(&adapter->lock, flags);
1934 retval = generic_mii_ioctl(&adapter->mii, if_mii(ifr), cmd, NULL);
1935 spin_unlock_irqrestore(&adapter->lock, flags);
1946 static int atl1_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1952 return atl1_mii_ioctl(netdev, ifr, cmd);
1959 * atl1_tx_timeout - Respond to a Tx Hang
1960 * @netdev: network interface device structure
1962 static void atl1_tx_timeout(struct net_device *netdev)
1964 struct atl1_adapter *adapter = netdev_priv(netdev);
1965 /* Do the reset outside of interrupt context */
1966 schedule_work(&adapter->tx_timeout_task);
1970 * atl1_phy_config - Timer Call-back
1971 * @data: pointer to netdev cast into an unsigned long
1973 static void atl1_phy_config(unsigned long data)
1975 struct atl1_adapter *adapter = (struct atl1_adapter *)data;
1976 struct atl1_hw *hw = &adapter->hw;
1977 unsigned long flags;
1979 spin_lock_irqsave(&adapter->lock, flags);
1980 adapter->phy_timer_pending = false;
1981 atl1_write_phy_reg(hw, MII_ADVERTISE, hw->mii_autoneg_adv_reg);
1982 atl1_write_phy_reg(hw, MII_AT001_CR, hw->mii_1000t_ctrl_reg);
1983 atl1_write_phy_reg(hw, MII_BMCR, MII_CR_RESET | MII_CR_AUTO_NEG_EN);
1984 spin_unlock_irqrestore(&adapter->lock, flags);
1987 int atl1_reset(struct atl1_adapter *adapter)
1991 ret = atl1_reset_hw(&adapter->hw);
1992 if (ret != ATL1_SUCCESS)
1994 return atl1_init_hw(&adapter->hw);
1998 * atl1_open - Called when a network interface is made active
1999 * @netdev: network interface device structure
2001 * Returns 0 on success, negative value on failure
2003 * The open entry point is called when a network interface is made
2004 * active by the system (IFF_UP). At this point all resources needed
2005 * for transmit and receive operations are allocated, the interrupt
2006 * handler is registered with the OS, the watchdog timer is started,
2007 * and the stack is notified that the interface is ready.
2009 static int atl1_open(struct net_device *netdev)
2011 struct atl1_adapter *adapter = netdev_priv(netdev);
2014 /* allocate transmit descriptors */
2015 err = atl1_setup_ring_resources(adapter);
2019 err = atl1_up(adapter);
2026 atl1_reset(adapter);
2031 * atl1_close - Disables a network interface
2032 * @netdev: network interface device structure
2034 * Returns 0, this is not allowed to fail
2036 * The close entry point is called when an interface is de-activated
2037 * by the OS. The hardware is still under the drivers control, but
2038 * needs to be disabled. A global MAC reset is issued to stop the
2039 * hardware, and all transmit and receive resources are freed.
2041 static int atl1_close(struct net_device *netdev)
2043 struct atl1_adapter *adapter = netdev_priv(netdev);
2045 atl1_free_ring_resources(adapter);
2050 * If TPD Buffer size equal to 0, PCIE DMAR_TO_INT
2051 * will assert. We do soft reset <0x1400=1> according
2052 * with the SPEC. BUT, it seemes that PCIE or DMA
2053 * state-machine will not be reset. DMAR_TO_INT will
2054 * assert again and again.
2056 static void atl1_tx_timeout_task(struct work_struct *work)
2058 struct atl1_adapter *adapter =
2059 container_of(work, struct atl1_adapter, tx_timeout_task);
2060 struct net_device *netdev = adapter->netdev;
2062 netif_device_detach(netdev);
2065 netif_device_attach(netdev);
2069 * atl1_link_chg_task - deal with link change event Out of interrupt context
2071 static void atl1_link_chg_task(struct work_struct *work)
2073 struct atl1_adapter *adapter =
2074 container_of(work, struct atl1_adapter, link_chg_task);
2075 unsigned long flags;
2077 spin_lock_irqsave(&adapter->lock, flags);
2078 atl1_check_link(adapter);
2079 spin_unlock_irqrestore(&adapter->lock, flags);
2083 * atl1_pcie_patch - Patch for PCIE module
2085 static void atl1_pcie_patch(struct atl1_adapter *adapter)
2089 iowrite32(value, adapter->hw.hw_addr + 0x12FC);
2090 /* pcie flow control mode change */
2091 value = ioread32(adapter->hw.hw_addr + 0x1008);
2093 iowrite32(value, adapter->hw.hw_addr + 0x1008);
2097 * When ACPI resume on some VIA MotherBoard, the Interrupt Disable bit/0x400
2098 * on PCI Command register is disable.
2099 * The function enable this bit.
2100 * Brackett, 2006/03/15
2102 static void atl1_via_workaround(struct atl1_adapter *adapter)
2104 unsigned long value;
2106 value = ioread16(adapter->hw.hw_addr + PCI_COMMAND);
2107 if (value & PCI_COMMAND_INTX_DISABLE)
2108 value &= ~PCI_COMMAND_INTX_DISABLE;
2109 iowrite32(value, adapter->hw.hw_addr + PCI_COMMAND);
2113 * atl1_probe - Device Initialization Routine
2114 * @pdev: PCI device information struct
2115 * @ent: entry in atl1_pci_tbl
2117 * Returns 0 on success, negative on failure
2119 * atl1_probe initializes an adapter identified by a pci_dev structure.
2120 * The OS initialization, configuring of the adapter private structure,
2121 * and a hardware reset occur.
2123 static int __devinit atl1_probe(struct pci_dev *pdev,
2124 const struct pci_device_id *ent)
2126 struct net_device *netdev;
2127 struct atl1_adapter *adapter;
2128 static int cards_found = 0;
2129 bool pci_using_64 = true;
2132 err = pci_enable_device(pdev);
2136 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
2138 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2141 "%s: no usable DMA configuration, aborting\n",
2145 pci_using_64 = false;
2147 /* Mark all PCI regions associated with PCI device
2148 * pdev as being reserved by owner atl1_driver_name
2150 err = pci_request_regions(pdev, atl1_driver_name);
2152 goto err_request_regions;
2154 /* Enables bus-mastering on the device and calls
2155 * pcibios_set_master to do the needed arch specific settings
2157 pci_set_master(pdev);
2159 netdev = alloc_etherdev(sizeof(struct atl1_adapter));
2162 goto err_alloc_etherdev;
2164 SET_MODULE_OWNER(netdev);
2165 SET_NETDEV_DEV(netdev, &pdev->dev);
2167 pci_set_drvdata(pdev, netdev);
2168 adapter = netdev_priv(netdev);
2169 adapter->netdev = netdev;
2170 adapter->pdev = pdev;
2171 adapter->hw.back = adapter;
2173 adapter->hw.hw_addr = pci_iomap(pdev, 0, 0);
2174 if (!adapter->hw.hw_addr) {
2178 /* get device revision number */
2179 adapter->hw.dev_rev = ioread16(adapter->hw.hw_addr + (REG_MASTER_CTRL + 2));
2181 /* set default ring resource counts */
2182 adapter->rfd_ring.count = adapter->rrd_ring.count = ATL1_DEFAULT_RFD;
2183 adapter->tpd_ring.count = ATL1_DEFAULT_TPD;
2185 adapter->mii.dev = netdev;
2186 adapter->mii.mdio_read = mdio_read;
2187 adapter->mii.mdio_write = mdio_write;
2188 adapter->mii.phy_id_mask = 0x1f;
2189 adapter->mii.reg_num_mask = 0x1f;
2191 netdev->open = &atl1_open;
2192 netdev->stop = &atl1_close;
2193 netdev->hard_start_xmit = &atl1_xmit_frame;
2194 netdev->get_stats = &atl1_get_stats;
2195 netdev->set_multicast_list = &atl1_set_multi;
2196 netdev->set_mac_address = &atl1_set_mac;
2197 netdev->change_mtu = &atl1_change_mtu;
2198 netdev->do_ioctl = &atl1_ioctl;
2199 netdev->tx_timeout = &atl1_tx_timeout;
2200 netdev->watchdog_timeo = 5 * HZ;
2201 netdev->vlan_rx_register = atl1_vlan_rx_register;
2202 netdev->vlan_rx_add_vid = atl1_vlan_rx_add_vid;
2203 netdev->vlan_rx_kill_vid = atl1_vlan_rx_kill_vid;
2204 netdev->ethtool_ops = &atl1_ethtool_ops;
2205 adapter->bd_number = cards_found;
2206 adapter->pci_using_64 = pci_using_64;
2208 /* setup the private structure */
2209 err = atl1_sw_init(adapter);
2213 netdev->features = NETIF_F_HW_CSUM;
2214 netdev->features |= NETIF_F_SG;
2215 netdev->features |= (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
2218 * FIXME - Until tso performance gets fixed, disable the feature.
2219 * Enable it with ethtool -K if desired.
2221 /* netdev->features |= NETIF_F_TSO; */
2224 netdev->features |= NETIF_F_HIGHDMA;
2226 netdev->features |= NETIF_F_LLTX;
2229 * patch for some L1 of old version,
2230 * the final version of L1 may not need these
2233 /* atl1_pcie_patch(adapter); */
2235 /* really reset GPHY core */
2236 iowrite16(0, adapter->hw.hw_addr + REG_GPHY_ENABLE);
2239 * reset the controller to
2240 * put the device in a known good starting state
2242 if (atl1_reset_hw(&adapter->hw)) {
2247 /* copy the MAC address out of the EEPROM */
2248 atl1_read_mac_addr(&adapter->hw);
2249 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
2251 if (!is_valid_ether_addr(netdev->dev_addr)) {
2256 atl1_check_options(adapter);
2258 /* pre-init the MAC, and setup link */
2259 err = atl1_init_hw(&adapter->hw);
2265 atl1_pcie_patch(adapter);
2266 /* assume we have no link for now */
2267 netif_carrier_off(netdev);
2268 netif_stop_queue(netdev);
2270 init_timer(&adapter->watchdog_timer);
2271 adapter->watchdog_timer.function = &atl1_watchdog;
2272 adapter->watchdog_timer.data = (unsigned long)adapter;
2274 init_timer(&adapter->phy_config_timer);
2275 adapter->phy_config_timer.function = &atl1_phy_config;
2276 adapter->phy_config_timer.data = (unsigned long)adapter;
2277 adapter->phy_timer_pending = false;
2279 INIT_WORK(&adapter->tx_timeout_task, atl1_tx_timeout_task);
2281 INIT_WORK(&adapter->link_chg_task, atl1_link_chg_task);
2283 INIT_WORK(&adapter->pcie_dma_to_rst_task, atl1_tx_timeout_task);
2285 err = register_netdev(netdev);
2290 atl1_via_workaround(adapter);
2294 pci_iounmap(pdev, adapter->hw.hw_addr);
2296 free_netdev(netdev);
2298 pci_release_regions(pdev);
2300 err_request_regions:
2301 pci_disable_device(pdev);
2306 * atl1_remove - Device Removal Routine
2307 * @pdev: PCI device information struct
2309 * atl1_remove is called by the PCI subsystem to alert the driver
2310 * that it should release a PCI device. The could be caused by a
2311 * Hot-Plug event, or because the driver is going to be removed from
2314 static void __devexit atl1_remove(struct pci_dev *pdev)
2316 struct net_device *netdev = pci_get_drvdata(pdev);
2317 struct atl1_adapter *adapter;
2318 /* Device not available. Return. */
2322 adapter = netdev_priv(netdev);
2323 iowrite16(0, adapter->hw.hw_addr + REG_GPHY_ENABLE);
2324 unregister_netdev(netdev);
2325 pci_iounmap(pdev, adapter->hw.hw_addr);
2326 pci_release_regions(pdev);
2327 free_netdev(netdev);
2328 pci_disable_device(pdev);
2332 static int atl1_suspend(struct pci_dev *pdev, pm_message_t state)
2334 struct net_device *netdev = pci_get_drvdata(pdev);
2335 struct atl1_adapter *adapter = netdev_priv(netdev);
2336 struct atl1_hw *hw = &adapter->hw;
2338 u32 wufc = adapter->wol;
2340 netif_device_detach(netdev);
2341 if (netif_running(netdev))
2344 atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2345 atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2346 if (ctrl & BMSR_LSTATUS)
2347 wufc &= ~ATL1_WUFC_LNKC;
2349 /* reduce speed to 10/100M */
2351 atl1_phy_enter_power_saving(hw);
2352 /* if resume, let driver to re- setup link */
2353 hw->phy_configured = false;
2354 atl1_set_mac_addr(hw);
2355 atl1_set_multi(netdev);
2358 /* turn on magic packet wol */
2359 if (wufc & ATL1_WUFC_MAG)
2360 ctrl = WOL_MAGIC_EN | WOL_MAGIC_PME_EN;
2362 /* turn on Link change WOL */
2363 if (wufc & ATL1_WUFC_LNKC)
2364 ctrl |= (WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN);
2365 iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
2367 /* turn on all-multi mode if wake on multicast is enabled */
2368 ctrl = ioread32(hw->hw_addr + REG_MAC_CTRL);
2369 ctrl &= ~MAC_CTRL_DBG;
2370 ctrl &= ~MAC_CTRL_PROMIS_EN;
2371 if (wufc & ATL1_WUFC_MC)
2372 ctrl |= MAC_CTRL_MC_ALL_EN;
2374 ctrl &= ~MAC_CTRL_MC_ALL_EN;
2376 /* turn on broadcast mode if wake on-BC is enabled */
2377 if (wufc & ATL1_WUFC_BC)
2378 ctrl |= MAC_CTRL_BC_EN;
2380 ctrl &= ~MAC_CTRL_BC_EN;
2383 ctrl |= MAC_CTRL_RX_EN;
2384 iowrite32(ctrl, hw->hw_addr + REG_MAC_CTRL);
2385 pci_enable_wake(pdev, PCI_D3hot, 1);
2386 pci_enable_wake(pdev, PCI_D3cold, 1); /* 4 == D3 cold */
2388 iowrite32(0, hw->hw_addr + REG_WOL_CTRL);
2389 pci_enable_wake(pdev, PCI_D3hot, 0);
2390 pci_enable_wake(pdev, PCI_D3cold, 0); /* 4 == D3 cold */
2393 pci_save_state(pdev);
2394 pci_disable_device(pdev);
2396 pci_set_power_state(pdev, PCI_D3hot);
2401 static int atl1_resume(struct pci_dev *pdev)
2403 struct net_device *netdev = pci_get_drvdata(pdev);
2404 struct atl1_adapter *adapter = netdev_priv(netdev);
2407 pci_set_power_state(pdev, 0);
2408 pci_restore_state(pdev);
2410 ret_val = pci_enable_device(pdev);
2411 pci_enable_wake(pdev, PCI_D3hot, 0);
2412 pci_enable_wake(pdev, PCI_D3cold, 0);
2414 iowrite32(0, adapter->hw.hw_addr + REG_WOL_CTRL);
2415 atl1_reset(adapter);
2417 if (netif_running(netdev))
2419 netif_device_attach(netdev);
2421 atl1_via_workaround(adapter);
2426 #define atl1_suspend NULL
2427 #define atl1_resume NULL
2430 static struct pci_driver atl1_driver = {
2431 .name = atl1_driver_name,
2432 .id_table = atl1_pci_tbl,
2433 .probe = atl1_probe,
2434 .remove = __devexit_p(atl1_remove),
2435 /* Power Managment Hooks */
2436 /* probably broken right now -- CHS */
2437 .suspend = atl1_suspend,
2438 .resume = atl1_resume
2442 * atl1_exit_module - Driver Exit Cleanup Routine
2444 * atl1_exit_module is called just before the driver is removed
2447 static void __exit atl1_exit_module(void)
2449 pci_unregister_driver(&atl1_driver);
2453 * atl1_init_module - Driver Registration Routine
2455 * atl1_init_module is the first routine called when the driver is
2456 * loaded. All it does is register with the PCI subsystem.
2458 static int __init atl1_init_module(void)
2460 printk(KERN_INFO "%s - version %s\n", atl1_driver_string, DRIVER_VERSION);
2461 printk(KERN_INFO "%s\n", atl1_copyright);
2462 return pci_register_driver(&atl1_driver);
2465 module_init(atl1_init_module);
2466 module_exit(atl1_exit_module);