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 RUN_REALTIME 0
86 #define DRIVER_VERSION "2.0.6"
88 char atl1_driver_name[] = "atl1";
89 static const char atl1_driver_string[] = "Attansic L1 Ethernet Network Driver";
90 static const char atl1_copyright[] = "Copyright(c) 2005-2006 Attansic Corporation.";
91 char atl1_driver_version[] = DRIVER_VERSION;
94 ("Attansic Corporation <xiong_huang@attansic.com>, Chris Snook <csnook@redhat.com>, Jay Cliburn <jcliburn@gmail.com>");
95 MODULE_DESCRIPTION("Attansic 1000M Ethernet Network Driver");
96 MODULE_LICENSE("GPL");
97 MODULE_VERSION(DRIVER_VERSION);
100 * atl1_pci_tbl - PCI Device ID Table
102 static const struct pci_device_id atl1_pci_tbl[] = {
103 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, 0x1048)},
104 /* required last entry */
108 MODULE_DEVICE_TABLE(pci, atl1_pci_tbl);
111 * atl1_sw_init - Initialize general software structures (struct atl1_adapter)
112 * @adapter: board private structure to initialize
114 * atl1_sw_init initializes the Adapter private data structure.
115 * Fields are initialized based on PCI device information and
116 * OS network device settings (MTU size).
118 static int __devinit atl1_sw_init(struct atl1_adapter *adapter)
120 struct atl1_hw *hw = &adapter->hw;
121 struct net_device *netdev = adapter->netdev;
122 struct pci_dev *pdev = adapter->pdev;
124 /* PCI config space info */
125 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
127 hw->max_frame_size = netdev->mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
128 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
131 adapter->rx_buffer_len = (hw->max_frame_size + 7) & ~7;
132 adapter->ict = 50000; /* 100ms */
133 adapter->link_speed = SPEED_0; /* hardware init */
134 adapter->link_duplex = FULL_DUPLEX;
136 hw->phy_configured = false;
137 hw->preamble_len = 7;
147 hw->rfd_fetch_gap = 1;
148 hw->rx_jumbo_th = adapter->rx_buffer_len / 8;
149 hw->rx_jumbo_lkah = 1;
150 hw->rrd_ret_timer = 16;
152 hw->tpd_fetch_th = 16;
153 hw->txf_burst = 0x100;
154 hw->tx_jumbo_task_th = (hw->max_frame_size + 7) >> 3;
155 hw->tpd_fetch_gap = 1;
156 hw->rcb_value = atl1_rcb_64;
157 hw->dma_ord = atl1_dma_ord_enh;
158 hw->dmar_block = atl1_dma_req_256;
159 hw->dmaw_block = atl1_dma_req_256;
162 hw->cmb_rx_timer = 1; /* about 2us */
163 hw->cmb_tx_timer = 1; /* about 2us */
164 hw->smb_timer = 100000; /* about 200ms */
166 atomic_set(&adapter->irq_sem, 0);
167 spin_lock_init(&adapter->lock);
168 spin_lock_init(&adapter->mb_lock);
174 * atl1_setup_mem_resources - allocate Tx / RX descriptor resources
175 * @adapter: board private structure
177 * Return 0 on success, negative on failure
179 s32 atl1_setup_ring_resources(struct atl1_adapter *adapter)
181 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
182 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
183 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
184 struct atl1_ring_header *ring_header = &adapter->ring_header;
185 struct pci_dev *pdev = adapter->pdev;
189 size = sizeof(struct atl1_buffer) * (tpd_ring->count + rfd_ring->count);
190 tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
191 if (unlikely(!tpd_ring->buffer_info)) {
192 printk(KERN_WARNING "%s: kzalloc failed , size = D%d\n",
193 atl1_driver_name, size);
196 rfd_ring->buffer_info =
197 (struct atl1_buffer *)(tpd_ring->buffer_info + tpd_ring->count);
199 /* real ring DMA buffer */
200 ring_header->size = size = sizeof(struct tx_packet_desc) *
202 + sizeof(struct rx_free_desc) * rfd_ring->count
203 + sizeof(struct rx_return_desc) * rrd_ring->count
204 + sizeof(struct coals_msg_block)
205 + sizeof(struct stats_msg_block)
206 + 40; /* "40: for 8 bytes align" huh? -- CHS */
208 ring_header->desc = pci_alloc_consistent(pdev, ring_header->size,
210 if (unlikely(!ring_header->desc)) {
212 "%s: pci_alloc_consistent failed, size = D%d\n",
213 atl1_driver_name, size);
217 memset(ring_header->desc, 0, ring_header->size);
220 tpd_ring->dma = ring_header->dma;
221 offset = (tpd_ring->dma & 0x7) ? (8 - (ring_header->dma & 0x7)) : 0;
222 tpd_ring->dma += offset;
223 tpd_ring->desc = (u8 *) ring_header->desc + offset;
224 tpd_ring->size = sizeof(struct tx_packet_desc) * tpd_ring->count;
225 atomic_set(&tpd_ring->next_to_use, 0);
226 atomic_set(&tpd_ring->next_to_clean, 0);
229 rfd_ring->dma = tpd_ring->dma + tpd_ring->size;
230 offset = (rfd_ring->dma & 0x7) ? (8 - (rfd_ring->dma & 0x7)) : 0;
231 rfd_ring->dma += offset;
232 rfd_ring->desc = (u8 *) tpd_ring->desc + (tpd_ring->size + offset);
233 rfd_ring->size = sizeof(struct rx_free_desc) * rfd_ring->count;
234 rfd_ring->next_to_clean = 0;
235 /* rfd_ring->next_to_use = rfd_ring->count - 1; */
236 atomic_set(&rfd_ring->next_to_use, 0);
239 rrd_ring->dma = rfd_ring->dma + rfd_ring->size;
240 offset = (rrd_ring->dma & 0x7) ? (8 - (rrd_ring->dma & 0x7)) : 0;
241 rrd_ring->dma += offset;
242 rrd_ring->desc = (u8 *) rfd_ring->desc + (rfd_ring->size + offset);
243 rrd_ring->size = sizeof(struct rx_return_desc) * rrd_ring->count;
244 rrd_ring->next_to_use = 0;
245 atomic_set(&rrd_ring->next_to_clean, 0);
248 adapter->cmb.dma = rrd_ring->dma + rrd_ring->size;
249 offset = (adapter->cmb.dma & 0x7) ? (8 - (adapter->cmb.dma & 0x7)) : 0;
250 adapter->cmb.dma += offset;
252 (struct coals_msg_block *) ((u8 *) rrd_ring->desc +
253 (rrd_ring->size + offset));
256 adapter->smb.dma = adapter->cmb.dma + sizeof(struct coals_msg_block);
257 offset = (adapter->smb.dma & 0x7) ? (8 - (adapter->smb.dma & 0x7)) : 0;
258 adapter->smb.dma += offset;
259 adapter->smb.smb = (struct stats_msg_block *)
260 ((u8 *) adapter->cmb.cmb + (sizeof(struct coals_msg_block) + offset));
265 kfree(tpd_ring->buffer_info);
270 * atl1_irq_enable - Enable default interrupt generation settings
271 * @adapter: board private structure
273 static void atl1_irq_enable(struct atl1_adapter *adapter)
275 if (likely(!atomic_dec_and_test(&adapter->irq_sem)))
276 iowrite32(IMR_NORMAL_MASK, adapter->hw.hw_addr + REG_IMR);
279 static void atl1_clear_phy_int(struct atl1_adapter *adapter)
284 spin_lock_irqsave(&adapter->lock, flags);
285 atl1_read_phy_reg(&adapter->hw, 19, &phy_data);
286 spin_unlock_irqrestore(&adapter->lock, flags);
289 static void atl1_inc_smb(struct atl1_adapter *adapter)
291 struct stats_msg_block *smb = adapter->smb.smb;
293 /* Fill out the OS statistics structure */
294 adapter->soft_stats.rx_packets += smb->rx_ok;
295 adapter->soft_stats.tx_packets += smb->tx_ok;
296 adapter->soft_stats.rx_bytes += smb->rx_byte_cnt;
297 adapter->soft_stats.tx_bytes += smb->tx_byte_cnt;
298 adapter->soft_stats.multicast += smb->rx_mcast;
299 adapter->soft_stats.collisions += (smb->tx_1_col +
303 adapter->hw.max_retry);
306 adapter->soft_stats.rx_errors += (smb->rx_frag +
311 smb->rx_rrd_ov + smb->rx_align_err);
312 adapter->soft_stats.rx_fifo_errors += smb->rx_rxf_ov;
313 adapter->soft_stats.rx_length_errors += smb->rx_len_err;
314 adapter->soft_stats.rx_crc_errors += smb->rx_fcs_err;
315 adapter->soft_stats.rx_frame_errors += smb->rx_align_err;
316 adapter->soft_stats.rx_missed_errors += (smb->rx_rrd_ov +
319 adapter->soft_stats.rx_pause += smb->rx_pause;
320 adapter->soft_stats.rx_rrd_ov += smb->rx_rrd_ov;
321 adapter->soft_stats.rx_trunc += smb->rx_sz_ov;
324 adapter->soft_stats.tx_errors += (smb->tx_late_col +
326 smb->tx_underrun + smb->tx_trunc);
327 adapter->soft_stats.tx_fifo_errors += smb->tx_underrun;
328 adapter->soft_stats.tx_aborted_errors += smb->tx_abort_col;
329 adapter->soft_stats.tx_window_errors += smb->tx_late_col;
331 adapter->soft_stats.excecol += smb->tx_abort_col;
332 adapter->soft_stats.deffer += smb->tx_defer;
333 adapter->soft_stats.scc += smb->tx_1_col;
334 adapter->soft_stats.mcc += smb->tx_2_col;
335 adapter->soft_stats.latecol += smb->tx_late_col;
336 adapter->soft_stats.tx_underun += smb->tx_underrun;
337 adapter->soft_stats.tx_trunc += smb->tx_trunc;
338 adapter->soft_stats.tx_pause += smb->tx_pause;
340 adapter->net_stats.rx_packets = adapter->soft_stats.rx_packets;
341 adapter->net_stats.tx_packets = adapter->soft_stats.tx_packets;
342 adapter->net_stats.rx_bytes = adapter->soft_stats.rx_bytes;
343 adapter->net_stats.tx_bytes = adapter->soft_stats.tx_bytes;
344 adapter->net_stats.multicast = adapter->soft_stats.multicast;
345 adapter->net_stats.collisions = adapter->soft_stats.collisions;
346 adapter->net_stats.rx_errors = adapter->soft_stats.rx_errors;
347 adapter->net_stats.rx_over_errors =
348 adapter->soft_stats.rx_missed_errors;
349 adapter->net_stats.rx_length_errors =
350 adapter->soft_stats.rx_length_errors;
351 adapter->net_stats.rx_crc_errors = adapter->soft_stats.rx_crc_errors;
352 adapter->net_stats.rx_frame_errors =
353 adapter->soft_stats.rx_frame_errors;
354 adapter->net_stats.rx_fifo_errors = adapter->soft_stats.rx_fifo_errors;
355 adapter->net_stats.rx_missed_errors =
356 adapter->soft_stats.rx_missed_errors;
357 adapter->net_stats.tx_errors = adapter->soft_stats.tx_errors;
358 adapter->net_stats.tx_fifo_errors = adapter->soft_stats.tx_fifo_errors;
359 adapter->net_stats.tx_aborted_errors =
360 adapter->soft_stats.tx_aborted_errors;
361 adapter->net_stats.tx_window_errors =
362 adapter->soft_stats.tx_window_errors;
363 adapter->net_stats.tx_carrier_errors =
364 adapter->soft_stats.tx_carrier_errors;
367 static void atl1_rx_checksum(struct atl1_adapter *adapter,
368 struct rx_return_desc *rrd,
371 skb->ip_summed = CHECKSUM_NONE;
373 if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
374 if (rrd->err_flg & (ERR_FLAG_CRC | ERR_FLAG_TRUNC |
375 ERR_FLAG_CODE | ERR_FLAG_OV)) {
376 adapter->hw_csum_err++;
377 printk(KERN_DEBUG "%s: rx checksum error\n",
384 if (!(rrd->pkt_flg & PACKET_FLAG_IPV4))
385 /* checksum is invalid, but it's not an IPv4 pkt, so ok */
389 if (likely(!(rrd->err_flg &
390 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM)))) {
391 skb->ip_summed = CHECKSUM_UNNECESSARY;
392 adapter->hw_csum_good++;
396 /* IPv4, but hardware thinks its checksum is wrong */
397 printk(KERN_DEBUG "%s: hw csum wrong pkt_flag:%x, err_flag:%x\n",
398 atl1_driver_name, rrd->pkt_flg, rrd->err_flg);
399 skb->ip_summed = CHECKSUM_COMPLETE;
400 skb->csum = htons(rrd->xsz.xsum_sz.rx_chksum);
401 adapter->hw_csum_err++;
406 * atl1_alloc_rx_buffers - Replace used receive buffers
407 * @adapter: address of board private structure
409 static u16 atl1_alloc_rx_buffers(struct atl1_adapter *adapter)
411 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
412 struct net_device *netdev = adapter->netdev;
413 struct pci_dev *pdev = adapter->pdev;
415 unsigned long offset;
416 struct atl1_buffer *buffer_info, *next_info;
419 u16 rfd_next_to_use, next_next;
420 struct rx_free_desc *rfd_desc;
422 next_next = rfd_next_to_use = atomic_read(&rfd_ring->next_to_use);
423 if (++next_next == rfd_ring->count)
425 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
426 next_info = &rfd_ring->buffer_info[next_next];
428 while (!buffer_info->alloced && !next_info->alloced) {
429 if (buffer_info->skb) {
430 buffer_info->alloced = 1;
434 rfd_desc = ATL1_RFD_DESC(rfd_ring, rfd_next_to_use);
436 skb = dev_alloc_skb(adapter->rx_buffer_len + NET_IP_ALIGN);
437 if (unlikely(!skb)) { /* Better luck next round */
438 adapter->net_stats.rx_dropped++;
443 * Make buffer alignment 2 beyond a 16 byte boundary
444 * this will result in a 16 byte aligned IP header after
445 * the 14 byte MAC header is removed
447 skb_reserve(skb, NET_IP_ALIGN);
450 buffer_info->alloced = 1;
451 buffer_info->skb = skb;
452 buffer_info->length = (u16) adapter->rx_buffer_len;
453 page = virt_to_page(skb->data);
454 offset = (unsigned long)skb->data & ~PAGE_MASK;
455 buffer_info->dma = pci_map_page(pdev, page, offset,
456 adapter->rx_buffer_len,
458 rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
459 rfd_desc->buf_len = cpu_to_le16(adapter->rx_buffer_len);
460 rfd_desc->coalese = 0;
463 rfd_next_to_use = next_next;
464 if (unlikely(++next_next == rfd_ring->count))
467 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
468 next_info = &rfd_ring->buffer_info[next_next];
474 * Force memory writes to complete before letting h/w
475 * know there are new descriptors to fetch. (Only
476 * applicable for weak-ordered memory model archs,
480 atomic_set(&rfd_ring->next_to_use, (int)rfd_next_to_use);
485 static void atl1_intr_rx(struct atl1_adapter *adapter)
489 u16 rrd_next_to_clean;
491 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
492 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
493 struct atl1_buffer *buffer_info;
494 struct rx_return_desc *rrd;
499 rrd_next_to_clean = atomic_read(&rrd_ring->next_to_clean);
502 rrd = ATL1_RRD_DESC(rrd_ring, rrd_next_to_clean);
504 if (likely(rrd->xsz.valid)) { /* packet valid */
506 /* check rrd status */
507 if (likely(rrd->num_buf == 1))
510 /* rrd seems to be bad */
511 if (unlikely(i-- > 0)) {
512 /* rrd may not be DMAed completely */
514 "%s: RRD may not be DMAed completely\n",
520 printk(KERN_DEBUG "%s: bad RRD\n", atl1_driver_name);
521 /* see if update RFD index */
522 if (rrd->num_buf > 1) {
525 (rrd->xsz.xsum_sz.pkt_size +
526 adapter->rx_buffer_len -
527 1) / adapter->rx_buffer_len;
528 if (rrd->num_buf == num_buf) {
529 /* clean alloc flag for bad rrd */
530 while (rfd_ring->next_to_clean !=
531 (rrd->buf_indx + num_buf)) {
532 rfd_ring->buffer_info[rfd_ring->
533 next_to_clean].alloced = 0;
534 if (++rfd_ring->next_to_clean ==
545 if (++rrd_next_to_clean == rrd_ring->count)
546 rrd_next_to_clean = 0;
549 } else { /* current rrd still not be updated */
554 /* clean alloc flag for bad rrd */
555 while (rfd_ring->next_to_clean != rrd->buf_indx) {
556 rfd_ring->buffer_info[rfd_ring->next_to_clean].alloced =
558 if (++rfd_ring->next_to_clean == rfd_ring->count)
559 rfd_ring->next_to_clean = 0;
562 buffer_info = &rfd_ring->buffer_info[rrd->buf_indx];
563 if (++rfd_ring->next_to_clean == rfd_ring->count)
564 rfd_ring->next_to_clean = 0;
566 /* update rrd next to clean */
567 if (++rrd_next_to_clean == rrd_ring->count)
568 rrd_next_to_clean = 0;
571 if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
573 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM
575 /* packet error, don't need upstream */
576 buffer_info->alloced = 0;
583 pci_unmap_page(adapter->pdev, buffer_info->dma,
584 buffer_info->length, PCI_DMA_FROMDEVICE);
585 skb = buffer_info->skb;
586 length = le16_to_cpu(rrd->xsz.xsum_sz.pkt_size);
588 skb_put(skb, length - ETHERNET_FCS_SIZE);
590 /* Receive Checksum Offload */
591 atl1_rx_checksum(adapter, rrd, skb);
592 skb->protocol = eth_type_trans(skb, adapter->netdev);
594 if (adapter->vlgrp && (rrd->pkt_flg & PACKET_FLAG_VLAN_INS)) {
595 u16 vlan_tag = (rrd->vlan_tag >> 4) |
596 ((rrd->vlan_tag & 7) << 13) |
597 ((rrd->vlan_tag & 8) << 9);
598 vlan_hwaccel_rx(skb, adapter->vlgrp, vlan_tag);
602 /* let protocol layer free skb */
603 buffer_info->skb = NULL;
604 buffer_info->alloced = 0;
607 adapter->netdev->last_rx = jiffies;
610 atomic_set(&rrd_ring->next_to_clean, rrd_next_to_clean);
612 atl1_alloc_rx_buffers(adapter);
614 /* update mailbox ? */
618 u32 rrd_next_to_clean;
620 spin_lock(&adapter->mb_lock);
622 tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
624 atomic_read(&adapter->rfd_ring.next_to_use);
626 atomic_read(&adapter->rrd_ring.next_to_clean);
627 value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
628 MB_RFD_PROD_INDX_SHIFT) |
629 ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
630 MB_RRD_CONS_INDX_SHIFT) |
631 ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
632 MB_TPD_PROD_INDX_SHIFT);
633 iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
634 spin_unlock(&adapter->mb_lock);
638 static void atl1_intr_tx(struct atl1_adapter *adapter)
640 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
641 struct atl1_buffer *buffer_info;
642 u16 sw_tpd_next_to_clean;
643 u16 cmb_tpd_next_to_clean;
646 sw_tpd_next_to_clean = atomic_read(&tpd_ring->next_to_clean);
647 cmb_tpd_next_to_clean = le16_to_cpu(adapter->cmb.cmb->tpd_cons_idx);
649 while (cmb_tpd_next_to_clean != sw_tpd_next_to_clean) {
650 struct tx_packet_desc *tpd;
652 tpd = ATL1_TPD_DESC(tpd_ring, sw_tpd_next_to_clean);
653 buffer_info = &tpd_ring->buffer_info[sw_tpd_next_to_clean];
654 if (buffer_info->dma) {
655 pci_unmap_page(adapter->pdev, buffer_info->dma,
656 buffer_info->length, PCI_DMA_TODEVICE);
657 buffer_info->dma = 0;
660 if (buffer_info->skb) {
661 dev_kfree_skb_irq(buffer_info->skb);
662 buffer_info->skb = NULL;
664 tpd->buffer_addr = 0;
667 if (++sw_tpd_next_to_clean == tpd_ring->count)
668 sw_tpd_next_to_clean = 0;
670 atomic_set(&tpd_ring->next_to_clean, sw_tpd_next_to_clean);
672 if (netif_queue_stopped(adapter->netdev)
673 && netif_carrier_ok(adapter->netdev))
674 netif_wake_queue(adapter->netdev);
677 static void atl1_check_for_link(struct atl1_adapter *adapter)
679 struct net_device *netdev = adapter->netdev;
682 spin_lock(&adapter->lock);
683 adapter->phy_timer_pending = false;
684 atl1_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
685 atl1_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
686 spin_unlock(&adapter->lock);
688 /* notify upper layer link down ASAP */
689 if (!(phy_data & BMSR_LSTATUS)) { /* Link Down */
690 if (netif_carrier_ok(netdev)) { /* old link state: Up */
691 printk(KERN_INFO "%s: %s link is down\n",
692 atl1_driver_name, netdev->name);
693 adapter->link_speed = SPEED_0;
694 netif_carrier_off(netdev);
695 netif_stop_queue(netdev);
698 schedule_work(&adapter->link_chg_task);
702 * atl1_intr - Interrupt Handler
703 * @irq: interrupt number
704 * @data: pointer to a network interface device structure
705 * @pt_regs: CPU registers structure
707 static irqreturn_t atl1_intr(int irq, void *data)
709 /*struct atl1_adapter *adapter = ((struct net_device *)data)->priv;*/
710 struct atl1_adapter *adapter = netdev_priv(data);
715 status = adapter->cmb.cmb->int_stats;
722 /* clear CMB interrupt status at once */
723 adapter->cmb.cmb->int_stats = 0;
725 if (status & ISR_GPHY) /* clear phy status */
726 atl1_clear_phy_int(adapter);
728 /* clear ISR status, and Enable CMB DMA/Disable Interrupt */
729 iowrite32(status | ISR_DIS_INT, adapter->hw.hw_addr + REG_ISR);
731 /* check if SMB intr */
732 if (status & ISR_SMB)
733 atl1_inc_smb(adapter);
735 /* check if PCIE PHY Link down */
736 if (status & ISR_PHY_LINKDOWN) {
737 printk(KERN_DEBUG "%s: pcie phy link down %x\n",
738 atl1_driver_name, status);
739 if (netif_running(adapter->netdev)) { /* reset MAC */
740 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
741 schedule_work(&adapter->pcie_dma_to_rst_task);
746 /* check if DMA read/write error ? */
747 if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
749 "%s: pcie DMA r/w error (status = 0x%x)\n",
750 atl1_driver_name, status);
751 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
752 schedule_work(&adapter->pcie_dma_to_rst_task);
757 if (status & ISR_GPHY) {
758 adapter->soft_stats.tx_carrier_errors++;
759 atl1_check_for_link(adapter);
763 if (status & ISR_CMB_TX)
764 atl1_intr_tx(adapter);
767 if (unlikely(status & (ISR_RXF_OV | ISR_RFD_UNRUN |
768 ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
769 ISR_HOST_RRD_OV | ISR_CMB_RX))) {
771 (ISR_RXF_OV | ISR_RFD_UNRUN | ISR_RRD_OV |
772 ISR_HOST_RFD_UNRUN | ISR_HOST_RRD_OV))
774 "%s: rx exception: status = 0x%x\n",
775 atl1_driver_name, status);
776 atl1_intr_rx(adapter);
782 } while ((status = adapter->cmb.cmb->int_stats));
784 /* re-enable Interrupt */
785 iowrite32(ISR_DIS_SMB | ISR_DIS_DMA, adapter->hw.hw_addr + REG_ISR);
790 * atl1_set_multi - Multicast and Promiscuous mode set
791 * @netdev: network interface device structure
793 * The set_multi entry point is called whenever the multicast address
794 * list or the network interface flags are updated. This routine is
795 * responsible for configuring the hardware for proper multicast,
796 * promiscuous mode, and all-multi behavior.
798 static void atl1_set_multi(struct net_device *netdev)
800 struct atl1_adapter *adapter = netdev_priv(netdev);
801 struct atl1_hw *hw = &adapter->hw;
802 struct dev_mc_list *mc_ptr;
806 /* Check for Promiscuous and All Multicast modes */
807 rctl = ioread32(hw->hw_addr + REG_MAC_CTRL);
808 if (netdev->flags & IFF_PROMISC)
809 rctl |= MAC_CTRL_PROMIS_EN;
810 else if (netdev->flags & IFF_ALLMULTI) {
811 rctl |= MAC_CTRL_MC_ALL_EN;
812 rctl &= ~MAC_CTRL_PROMIS_EN;
814 rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
816 iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL);
818 /* clear the old settings from the multicast hash table */
819 iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
820 iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
822 /* compute mc addresses' hash value ,and put it into hash table */
823 for (mc_ptr = netdev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
824 hash_value = atl1_hash_mc_addr(hw, mc_ptr->dmi_addr);
825 atl1_hash_set(hw, hash_value);
829 static void atl1_setup_mac_ctrl(struct atl1_adapter *adapter)
832 struct atl1_hw *hw = &adapter->hw;
833 struct net_device *netdev = adapter->netdev;
834 /* Config MAC CTRL Register */
835 value = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN;
837 if (FULL_DUPLEX == adapter->link_duplex)
838 value |= MAC_CTRL_DUPLX;
840 value |= ((u32) ((SPEED_1000 == adapter->link_speed) ?
841 MAC_CTRL_SPEED_1000 : MAC_CTRL_SPEED_10_100) <<
842 MAC_CTRL_SPEED_SHIFT);
844 value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
846 value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
847 /* preamble length */
848 value |= (((u32) adapter->hw.preamble_len
849 & MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
852 value |= MAC_CTRL_RMV_VLAN;
854 if (adapter->rx_csum)
855 value |= MAC_CTRL_RX_CHKSUM_EN;
858 value |= MAC_CTRL_BC_EN;
859 if (netdev->flags & IFF_PROMISC)
860 value |= MAC_CTRL_PROMIS_EN;
861 else if (netdev->flags & IFF_ALLMULTI)
862 value |= MAC_CTRL_MC_ALL_EN;
863 /* value |= MAC_CTRL_LOOPBACK; */
864 iowrite32(value, hw->hw_addr + REG_MAC_CTRL);
867 static u32 atl1_check_link(struct atl1_adapter *adapter)
869 struct atl1_hw *hw = &adapter->hw;
870 struct net_device *netdev = adapter->netdev;
872 u16 speed, duplex, phy_data;
875 /* MII_BMSR must read twice */
876 atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
877 atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
878 if (!(phy_data & BMSR_LSTATUS)) { /* link down */
879 if (netif_carrier_ok(netdev)) { /* old link state: Up */
880 printk(KERN_INFO "%s: link is down\n",
882 adapter->link_speed = SPEED_0;
883 netif_carrier_off(netdev);
884 netif_stop_queue(netdev);
890 ret_val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
894 switch (hw->media_type) {
895 case MEDIA_TYPE_1000M_FULL:
896 if (speed != SPEED_1000 || duplex != FULL_DUPLEX)
899 case MEDIA_TYPE_100M_FULL:
900 if (speed != SPEED_100 || duplex != FULL_DUPLEX)
903 case MEDIA_TYPE_100M_HALF:
904 if (speed != SPEED_100 || duplex != HALF_DUPLEX)
907 case MEDIA_TYPE_10M_FULL:
908 if (speed != SPEED_10 || duplex != FULL_DUPLEX)
911 case MEDIA_TYPE_10M_HALF:
912 if (speed != SPEED_10 || duplex != HALF_DUPLEX)
917 /* link result is our setting */
919 if (adapter->link_speed != speed
920 || adapter->link_duplex != duplex) {
921 adapter->link_speed = speed;
922 adapter->link_duplex = duplex;
923 atl1_setup_mac_ctrl(adapter);
924 printk(KERN_INFO "%s: %s link is up %d Mbps %s\n",
925 atl1_driver_name, netdev->name,
927 adapter->link_duplex ==
928 FULL_DUPLEX ? "full duplex" : "half duplex");
930 if (!netif_carrier_ok(netdev)) { /* Link down -> Up */
931 netif_carrier_on(netdev);
932 netif_wake_queue(netdev);
937 /* change orignal link status */
938 if (netif_carrier_ok(netdev)) {
939 adapter->link_speed = SPEED_0;
940 netif_carrier_off(netdev);
941 netif_stop_queue(netdev);
944 if (hw->media_type != MEDIA_TYPE_AUTO_SENSOR &&
945 hw->media_type != MEDIA_TYPE_1000M_FULL) {
946 switch (hw->media_type) {
947 case MEDIA_TYPE_100M_FULL:
948 phy_data = MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
951 case MEDIA_TYPE_100M_HALF:
952 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
954 case MEDIA_TYPE_10M_FULL:
956 MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
958 default: /* MEDIA_TYPE_10M_HALF: */
959 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
962 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
966 /* auto-neg, insert timer to re-config phy */
967 if (!adapter->phy_timer_pending) {
968 adapter->phy_timer_pending = true;
969 mod_timer(&adapter->phy_config_timer, jiffies + 3 * HZ);
975 static void set_flow_ctrl_old(struct atl1_adapter *adapter)
979 /* RFD Flow Control */
980 value = adapter->rfd_ring.count;
986 value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
987 ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
988 iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
990 /* RRD Flow Control */
991 value = adapter->rrd_ring.count;
996 value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
997 ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
998 iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1001 static void set_flow_ctrl_new(struct atl1_hw *hw)
1005 /* RXF Flow Control */
1006 value = ioread32(hw->hw_addr + REG_SRAM_RXF_LEN);
1013 value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
1014 ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
1015 iowrite32(value, hw->hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
1017 /* RRD Flow Control */
1018 value = ioread32(hw->hw_addr + REG_SRAM_RRD_LEN);
1025 value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
1026 ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
1027 iowrite32(value, hw->hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1031 * atl1_configure - Configure Transmit&Receive Unit after Reset
1032 * @adapter: board private structure
1034 * Configure the Tx /Rx unit of the MAC after a reset.
1036 static u32 atl1_configure(struct atl1_adapter *adapter)
1038 struct atl1_hw *hw = &adapter->hw;
1041 /* clear interrupt status */
1042 iowrite32(0xffffffff, adapter->hw.hw_addr + REG_ISR);
1044 /* set MAC Address */
1045 value = (((u32) hw->mac_addr[2]) << 24) |
1046 (((u32) hw->mac_addr[3]) << 16) |
1047 (((u32) hw->mac_addr[4]) << 8) |
1048 (((u32) hw->mac_addr[5]));
1049 iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
1050 value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
1051 iowrite32(value, hw->hw_addr + (REG_MAC_STA_ADDR + 4));
1055 /* HI base address */
1056 iowrite32((u32) ((adapter->tpd_ring.dma & 0xffffffff00000000ULL) >> 32),
1057 hw->hw_addr + REG_DESC_BASE_ADDR_HI);
1058 /* LO base address */
1059 iowrite32((u32) (adapter->rfd_ring.dma & 0x00000000ffffffffULL),
1060 hw->hw_addr + REG_DESC_RFD_ADDR_LO);
1061 iowrite32((u32) (adapter->rrd_ring.dma & 0x00000000ffffffffULL),
1062 hw->hw_addr + REG_DESC_RRD_ADDR_LO);
1063 iowrite32((u32) (adapter->tpd_ring.dma & 0x00000000ffffffffULL),
1064 hw->hw_addr + REG_DESC_TPD_ADDR_LO);
1065 iowrite32((u32) (adapter->cmb.dma & 0x00000000ffffffffULL),
1066 hw->hw_addr + REG_DESC_CMB_ADDR_LO);
1067 iowrite32((u32) (adapter->smb.dma & 0x00000000ffffffffULL),
1068 hw->hw_addr + REG_DESC_SMB_ADDR_LO);
1071 value = adapter->rrd_ring.count;
1073 value += adapter->rfd_ring.count;
1074 iowrite32(value, hw->hw_addr + REG_DESC_RFD_RRD_RING_SIZE);
1075 iowrite32(adapter->tpd_ring.count, hw->hw_addr + REG_DESC_TPD_RING_SIZE);
1078 iowrite32(1, hw->hw_addr + REG_LOAD_PTR);
1080 /* config Mailbox */
1081 value = ((atomic_read(&adapter->tpd_ring.next_to_use)
1082 & MB_TPD_PROD_INDX_MASK) << MB_TPD_PROD_INDX_SHIFT) |
1083 ((atomic_read(&adapter->rrd_ring.next_to_clean)
1084 & MB_RRD_CONS_INDX_MASK) << MB_RRD_CONS_INDX_SHIFT) |
1085 ((atomic_read(&adapter->rfd_ring.next_to_use)
1086 & MB_RFD_PROD_INDX_MASK) << MB_RFD_PROD_INDX_SHIFT);
1087 iowrite32(value, hw->hw_addr + REG_MAILBOX);
1089 /* config IPG/IFG */
1090 value = (((u32) hw->ipgt & MAC_IPG_IFG_IPGT_MASK)
1091 << MAC_IPG_IFG_IPGT_SHIFT) |
1092 (((u32) hw->min_ifg & MAC_IPG_IFG_MIFG_MASK)
1093 << MAC_IPG_IFG_MIFG_SHIFT) |
1094 (((u32) hw->ipgr1 & MAC_IPG_IFG_IPGR1_MASK)
1095 << MAC_IPG_IFG_IPGR1_SHIFT) |
1096 (((u32) hw->ipgr2 & MAC_IPG_IFG_IPGR2_MASK)
1097 << MAC_IPG_IFG_IPGR2_SHIFT);
1098 iowrite32(value, hw->hw_addr + REG_MAC_IPG_IFG);
1100 /* config Half-Duplex Control */
1101 value = ((u32) hw->lcol & MAC_HALF_DUPLX_CTRL_LCOL_MASK) |
1102 (((u32) hw->max_retry & MAC_HALF_DUPLX_CTRL_RETRY_MASK)
1103 << MAC_HALF_DUPLX_CTRL_RETRY_SHIFT) |
1104 MAC_HALF_DUPLX_CTRL_EXC_DEF_EN |
1105 (0xa << MAC_HALF_DUPLX_CTRL_ABEBT_SHIFT) |
1106 (((u32) hw->jam_ipg & MAC_HALF_DUPLX_CTRL_JAMIPG_MASK)
1107 << MAC_HALF_DUPLX_CTRL_JAMIPG_SHIFT);
1108 iowrite32(value, hw->hw_addr + REG_MAC_HALF_DUPLX_CTRL);
1110 /* set Interrupt Moderator Timer */
1111 iowrite16(adapter->imt, hw->hw_addr + REG_IRQ_MODU_TIMER_INIT);
1112 iowrite32(MASTER_CTRL_ITIMER_EN, hw->hw_addr + REG_MASTER_CTRL);
1114 /* set Interrupt Clear Timer */
1115 iowrite16(adapter->ict, hw->hw_addr + REG_CMBDISDMA_TIMER);
1117 /* set MTU, 4 : VLAN */
1118 iowrite32(hw->max_frame_size + 4, hw->hw_addr + REG_MTU);
1120 /* jumbo size & rrd retirement timer */
1121 value = (((u32) hw->rx_jumbo_th & RXQ_JMBOSZ_TH_MASK)
1122 << RXQ_JMBOSZ_TH_SHIFT) |
1123 (((u32) hw->rx_jumbo_lkah & RXQ_JMBO_LKAH_MASK)
1124 << RXQ_JMBO_LKAH_SHIFT) |
1125 (((u32) hw->rrd_ret_timer & RXQ_RRD_TIMER_MASK)
1126 << RXQ_RRD_TIMER_SHIFT);
1127 iowrite32(value, hw->hw_addr + REG_RXQ_JMBOSZ_RRDTIM);
1130 switch (hw->dev_rev) {
1135 set_flow_ctrl_old(adapter);
1138 set_flow_ctrl_new(hw);
1143 value = (((u32) hw->tpd_burst & TXQ_CTRL_TPD_BURST_NUM_MASK)
1144 << TXQ_CTRL_TPD_BURST_NUM_SHIFT) |
1145 (((u32) hw->txf_burst & TXQ_CTRL_TXF_BURST_NUM_MASK)
1146 << TXQ_CTRL_TXF_BURST_NUM_SHIFT) |
1147 (((u32) hw->tpd_fetch_th & TXQ_CTRL_TPD_FETCH_TH_MASK)
1148 << TXQ_CTRL_TPD_FETCH_TH_SHIFT) | TXQ_CTRL_ENH_MODE | TXQ_CTRL_EN;
1149 iowrite32(value, hw->hw_addr + REG_TXQ_CTRL);
1151 /* min tpd fetch gap & tx jumbo packet size threshold for taskoffload */
1152 value = (((u32) hw->tx_jumbo_task_th & TX_JUMBO_TASK_TH_MASK)
1153 << TX_JUMBO_TASK_TH_SHIFT) |
1154 (((u32) hw->tpd_fetch_gap & TX_TPD_MIN_IPG_MASK)
1155 << TX_TPD_MIN_IPG_SHIFT);
1156 iowrite32(value, hw->hw_addr + REG_TX_JUMBO_TASK_TH_TPD_IPG);
1159 value = (((u32) hw->rfd_burst & RXQ_CTRL_RFD_BURST_NUM_MASK)
1160 << RXQ_CTRL_RFD_BURST_NUM_SHIFT) |
1161 (((u32) hw->rrd_burst & RXQ_CTRL_RRD_BURST_THRESH_MASK)
1162 << RXQ_CTRL_RRD_BURST_THRESH_SHIFT) |
1163 (((u32) hw->rfd_fetch_gap & RXQ_CTRL_RFD_PREF_MIN_IPG_MASK)
1164 << RXQ_CTRL_RFD_PREF_MIN_IPG_SHIFT) |
1165 RXQ_CTRL_CUT_THRU_EN | RXQ_CTRL_EN;
1166 iowrite32(value, hw->hw_addr + REG_RXQ_CTRL);
1168 /* config DMA Engine */
1169 value = ((((u32) hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
1170 << DMA_CTRL_DMAR_BURST_LEN_SHIFT) |
1171 ((((u32) hw->dmaw_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
1172 << DMA_CTRL_DMAR_BURST_LEN_SHIFT) |
1173 DMA_CTRL_DMAR_EN | DMA_CTRL_DMAW_EN;
1174 value |= (u32) hw->dma_ord;
1175 if (atl1_rcb_128 == hw->rcb_value)
1176 value |= DMA_CTRL_RCB_VALUE;
1177 iowrite32(value, hw->hw_addr + REG_DMA_CTRL);
1179 /* config CMB / SMB */
1180 value = hw->cmb_rrd | ((u32) hw->cmb_tpd << 16);
1181 iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TH);
1182 value = hw->cmb_rx_timer | ((u32) hw->cmb_tx_timer << 16);
1183 iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TIMER);
1184 iowrite32(hw->smb_timer, hw->hw_addr + REG_SMB_TIMER);
1186 /* --- enable CMB / SMB */
1187 value = CSMB_CTRL_CMB_EN | CSMB_CTRL_SMB_EN;
1188 iowrite32(value, hw->hw_addr + REG_CSMB_CTRL);
1190 value = ioread32(adapter->hw.hw_addr + REG_ISR);
1191 if (unlikely((value & ISR_PHY_LINKDOWN) != 0))
1192 value = 1; /* config failed */
1196 /* clear all interrupt status */
1197 iowrite32(0x3fffffff, adapter->hw.hw_addr + REG_ISR);
1198 iowrite32(0, adapter->hw.hw_addr + REG_ISR);
1203 * atl1_irq_disable - Mask off interrupt generation on the NIC
1204 * @adapter: board private structure
1206 static void atl1_irq_disable(struct atl1_adapter *adapter)
1208 atomic_inc(&adapter->irq_sem);
1209 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
1210 ioread32(adapter->hw.hw_addr + REG_IMR);
1211 synchronize_irq(adapter->pdev->irq);
1214 static void atl1_vlan_rx_register(struct net_device *netdev,
1215 struct vlan_group *grp)
1217 struct atl1_adapter *adapter = netdev_priv(netdev);
1218 unsigned long flags;
1221 spin_lock_irqsave(&adapter->lock, flags);
1222 /* atl1_irq_disable(adapter); */
1223 adapter->vlgrp = grp;
1226 /* enable VLAN tag insert/strip */
1227 ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
1228 ctrl |= MAC_CTRL_RMV_VLAN;
1229 iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
1231 /* disable VLAN tag insert/strip */
1232 ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
1233 ctrl &= ~MAC_CTRL_RMV_VLAN;
1234 iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
1237 /* atl1_irq_enable(adapter); */
1238 spin_unlock_irqrestore(&adapter->lock, flags);
1241 /* FIXME: justify or remove -- CHS */
1242 static void atl1_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1244 /* We don't do Vlan filtering */
1248 /* FIXME: this looks wrong too -- CHS */
1249 static void atl1_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1251 struct atl1_adapter *adapter = netdev_priv(netdev);
1252 unsigned long flags;
1254 spin_lock_irqsave(&adapter->lock, flags);
1255 /* atl1_irq_disable(adapter); */
1257 adapter->vlgrp->vlan_devices[vid] = NULL;
1258 /* atl1_irq_enable(adapter); */
1259 spin_unlock_irqrestore(&adapter->lock, flags);
1260 /* We don't do Vlan filtering */
1264 static void atl1_restore_vlan(struct atl1_adapter *adapter)
1266 atl1_vlan_rx_register(adapter->netdev, adapter->vlgrp);
1267 if (adapter->vlgrp) {
1269 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1270 if (!adapter->vlgrp->vlan_devices[vid])
1272 atl1_vlan_rx_add_vid(adapter->netdev, vid);
1277 static u16 tpd_avail(struct atl1_tpd_ring *tpd_ring)
1279 u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1280 u16 next_to_use = atomic_read(&tpd_ring->next_to_use);
1281 return ((next_to_clean >
1282 next_to_use) ? next_to_clean - next_to_use -
1283 1 : tpd_ring->count + next_to_clean - next_to_use - 1);
1286 static int atl1_tso(struct atl1_adapter *adapter, struct sk_buff *skb,
1287 struct tso_param *tso)
1289 /* We enter this function holding a spinlock. */
1293 if (skb_shinfo(skb)->gso_size) {
1294 if (skb_header_cloned(skb)) {
1295 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1300 if (skb->protocol == ntohs(ETH_P_IP)) {
1301 skb->nh.iph->tot_len = 0;
1302 skb->nh.iph->check = 0;
1304 ~csum_tcpudp_magic(skb->nh.iph->saddr,
1305 skb->nh.iph->daddr, 0,
1307 ipofst = skb->nh.raw - skb->data;
1308 if (ipofst != ENET_HEADER_SIZE) /* 802.3 frame */
1309 tso->tsopl |= 1 << TSO_PARAM_ETHTYPE_SHIFT;
1311 tso->tsopl |= (skb->nh.iph->ihl &
1312 CSUM_PARAM_IPHL_MASK) << CSUM_PARAM_IPHL_SHIFT;
1313 tso->tsopl |= ((skb->h.th->doff << 2) &
1314 TSO_PARAM_TCPHDRLEN_MASK) << TSO_PARAM_TCPHDRLEN_SHIFT;
1315 tso->tsopl |= (skb_shinfo(skb)->gso_size &
1316 TSO_PARAM_MSS_MASK) << TSO_PARAM_MSS_SHIFT;
1317 tso->tsopl |= 1 << TSO_PARAM_IPCKSUM_SHIFT;
1318 tso->tsopl |= 1 << TSO_PARAM_TCPCKSUM_SHIFT;
1319 tso->tsopl |= 1 << TSO_PARAM_SEGMENT_SHIFT;
1326 static int atl1_tx_csum(struct atl1_adapter *adapter, struct sk_buff *skb,
1327 struct csum_param *csum)
1331 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1332 cso = skb->h.raw - skb->data;
1333 css = (skb->h.raw + skb->csum) - skb->data;
1334 if (unlikely(cso & 0x1)) {
1335 printk(KERN_DEBUG "%s: payload offset != even number\n",
1339 csum->csumpl |= (cso & CSUM_PARAM_PLOADOFFSET_MASK) <<
1340 CSUM_PARAM_PLOADOFFSET_SHIFT;
1341 csum->csumpl |= (css & CSUM_PARAM_XSUMOFFSET_MASK) <<
1342 CSUM_PARAM_XSUMOFFSET_SHIFT;
1343 csum->csumpl |= 1 << CSUM_PARAM_CUSTOMCKSUM_SHIFT;
1350 static void atl1_tx_map(struct atl1_adapter *adapter,
1351 struct sk_buff *skb, bool tcp_seg)
1353 /* We enter this function holding a spinlock. */
1354 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1355 struct atl1_buffer *buffer_info;
1357 int first_buf_len = skb->len;
1358 unsigned long offset;
1359 unsigned int nr_frags;
1361 u16 tpd_next_to_use;
1365 first_buf_len -= skb->data_len;
1366 nr_frags = skb_shinfo(skb)->nr_frags;
1367 tpd_next_to_use = atomic_read(&tpd_ring->next_to_use);
1368 buffer_info = &tpd_ring->buffer_info[tpd_next_to_use];
1369 if (unlikely(buffer_info->skb))
1371 buffer_info->skb = NULL; /* put skb in last TPD */
1376 ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
1377 buffer_info->length = proto_hdr_len;
1378 page = virt_to_page(skb->data);
1379 offset = (unsigned long)skb->data & ~PAGE_MASK;
1380 buffer_info->dma = pci_map_page(adapter->pdev, page,
1381 offset, proto_hdr_len,
1384 if (++tpd_next_to_use == tpd_ring->count)
1385 tpd_next_to_use = 0;
1387 if (first_buf_len > proto_hdr_len) {
1388 len12 = first_buf_len - proto_hdr_len;
1389 m = (len12 + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1390 for (i = 0; i < m; i++) {
1392 &tpd_ring->buffer_info[tpd_next_to_use];
1393 buffer_info->skb = NULL;
1394 buffer_info->length =
1396 len12) ? MAX_TX_BUF_LEN : len12;
1397 len12 -= buffer_info->length;
1398 page = virt_to_page(skb->data +
1400 i * MAX_TX_BUF_LEN));
1401 offset = (unsigned long)(skb->data +
1403 i * MAX_TX_BUF_LEN)) &
1406 pci_map_page(adapter->pdev, page, offset,
1407 buffer_info->length,
1409 if (++tpd_next_to_use == tpd_ring->count)
1410 tpd_next_to_use = 0;
1415 buffer_info->length = first_buf_len;
1416 page = virt_to_page(skb->data);
1417 offset = (unsigned long)skb->data & ~PAGE_MASK;
1418 buffer_info->dma = pci_map_page(adapter->pdev, page,
1419 offset, first_buf_len,
1421 if (++tpd_next_to_use == tpd_ring->count)
1422 tpd_next_to_use = 0;
1425 for (f = 0; f < nr_frags; f++) {
1426 struct skb_frag_struct *frag;
1429 frag = &skb_shinfo(skb)->frags[f];
1432 m = (lenf + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1433 for (i = 0; i < m; i++) {
1434 buffer_info = &tpd_ring->buffer_info[tpd_next_to_use];
1435 if (unlikely(buffer_info->skb))
1437 buffer_info->skb = NULL;
1438 buffer_info->length =
1439 (lenf > MAX_TX_BUF_LEN) ? MAX_TX_BUF_LEN : lenf;
1440 lenf -= buffer_info->length;
1442 pci_map_page(adapter->pdev, frag->page,
1443 frag->page_offset + i * MAX_TX_BUF_LEN,
1444 buffer_info->length, PCI_DMA_TODEVICE);
1446 if (++tpd_next_to_use == tpd_ring->count)
1447 tpd_next_to_use = 0;
1451 /* last tpd's buffer-info */
1452 buffer_info->skb = skb;
1455 static void atl1_tx_queue(struct atl1_adapter *adapter, int count,
1456 union tpd_descr *descr)
1458 /* We enter this function holding a spinlock. */
1459 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1462 struct atl1_buffer *buffer_info;
1463 struct tx_packet_desc *tpd;
1464 u16 tpd_next_to_use = atomic_read(&tpd_ring->next_to_use);
1466 for (j = 0; j < count; j++) {
1467 buffer_info = &tpd_ring->buffer_info[tpd_next_to_use];
1468 tpd = ATL1_TPD_DESC(&adapter->tpd_ring, tpd_next_to_use);
1469 tpd->desc.csum.csumpu = descr->csum.csumpu;
1470 tpd->desc.csum.csumpl = descr->csum.csumpl;
1471 tpd->desc.tso.tsopu = descr->tso.tsopu;
1472 tpd->desc.tso.tsopl = descr->tso.tsopl;
1473 tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
1474 tpd->desc.data = descr->data;
1475 tpd->desc.csum.csumpu |= (cpu_to_le16(buffer_info->length) &
1476 CSUM_PARAM_BUFLEN_MASK) << CSUM_PARAM_BUFLEN_SHIFT;
1478 val = (descr->tso.tsopl >> TSO_PARAM_SEGMENT_SHIFT) &
1479 TSO_PARAM_SEGMENT_MASK;
1481 tpd->desc.tso.tsopl |= 1 << TSO_PARAM_HDRFLAG_SHIFT;
1483 if (j == (count - 1))
1484 tpd->desc.csum.csumpl |= 1 << CSUM_PARAM_EOP_SHIFT;
1486 if (++tpd_next_to_use == tpd_ring->count)
1487 tpd_next_to_use = 0;
1490 * Force memory writes to complete before letting h/w
1491 * know there are new descriptors to fetch. (Only
1492 * applicable for weak-ordered memory model archs,
1497 atomic_set(&tpd_ring->next_to_use, (int)tpd_next_to_use);
1500 static void atl1_update_mailbox(struct atl1_adapter *adapter)
1502 unsigned long flags;
1503 u32 tpd_next_to_use;
1504 u32 rfd_next_to_use;
1505 u32 rrd_next_to_clean;
1508 spin_lock_irqsave(&adapter->mb_lock, flags);
1510 tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
1511 rfd_next_to_use = atomic_read(&adapter->rfd_ring.next_to_use);
1512 rrd_next_to_clean = atomic_read(&adapter->rrd_ring.next_to_clean);
1514 value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
1515 MB_RFD_PROD_INDX_SHIFT) |
1516 ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
1517 MB_RRD_CONS_INDX_SHIFT) |
1518 ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
1519 MB_TPD_PROD_INDX_SHIFT);
1520 iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
1522 spin_unlock_irqrestore(&adapter->mb_lock, flags);
1525 static int atl1_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1527 struct atl1_adapter *adapter = netdev_priv(netdev);
1533 union tpd_descr param;
1536 unsigned long flags;
1537 unsigned int nr_frags = 0;
1538 unsigned int mss = 0;
1540 unsigned int proto_hdr_len;
1542 len -= skb->data_len;
1544 if (unlikely(skb->len == 0)) {
1545 dev_kfree_skb_any(skb);
1546 return NETDEV_TX_OK;
1550 param.tso.tsopu = 0;
1551 param.tso.tsopl = 0;
1552 param.csum.csumpu = 0;
1553 param.csum.csumpl = 0;
1555 /* nr_frags will be nonzero if we're doing scatter/gather (SG) */
1556 nr_frags = skb_shinfo(skb)->nr_frags;
1557 for (f = 0; f < nr_frags; f++) {
1558 frag_size = skb_shinfo(skb)->frags[f].size;
1561 (frag_size + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1564 /* mss will be nonzero if we're doing segment offload (TSO/GSO) */
1565 mss = skb_shinfo(skb)->gso_size;
1567 if (skb->protocol == ntohs(ETH_P_IP)) {
1568 proto_hdr_len = ((skb->h.raw - skb->data) +
1569 (skb->h.th->doff << 2));
1570 if (unlikely(proto_hdr_len > len)) {
1571 dev_kfree_skb_any(skb);
1572 return NETDEV_TX_OK;
1574 /* need additional TPD ? */
1575 if (proto_hdr_len != len)
1576 count += (len - proto_hdr_len +
1577 MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
1581 local_irq_save(flags);
1582 if (!spin_trylock(&adapter->lock)) {
1583 /* Can't get lock - tell upper layer to requeue */
1584 local_irq_restore(flags);
1585 printk(KERN_DEBUG "%s: TX locked\n", atl1_driver_name);
1586 return NETDEV_TX_LOCKED;
1589 if (tpd_avail(&adapter->tpd_ring) < count) {
1590 /* not enough descriptors */
1591 netif_stop_queue(netdev);
1592 spin_unlock_irqrestore(&adapter->lock, flags);
1593 printk(KERN_DEBUG "%s: TX busy\n", atl1_driver_name);
1594 return NETDEV_TX_BUSY;
1599 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
1600 vlan_tag = vlan_tx_tag_get(skb);
1601 vlan_tag = (vlan_tag << 4) | (vlan_tag >> 13) |
1602 ((vlan_tag >> 9) & 0x8);
1603 param.csum.csumpl |= 1 << CSUM_PARAM_INSVLAG_SHIFT;
1604 param.csum.csumpu |= (vlan_tag & CSUM_PARAM_VALANTAG_MASK) <<
1605 CSUM_PARAM_VALAN_SHIFT;
1608 tso = atl1_tso(adapter, skb, ¶m.tso);
1610 spin_unlock_irqrestore(&adapter->lock, flags);
1611 dev_kfree_skb_any(skb);
1612 return NETDEV_TX_OK;
1616 ret_val = atl1_tx_csum(adapter, skb, ¶m.csum);
1618 spin_unlock_irqrestore(&adapter->lock, flags);
1619 dev_kfree_skb_any(skb);
1620 return NETDEV_TX_OK;
1624 val = (param.csum.csumpl >> CSUM_PARAM_SEGMENT_SHIFT) &
1625 CSUM_PARAM_SEGMENT_MASK;
1626 atl1_tx_map(adapter, skb, 1 == val);
1627 atl1_tx_queue(adapter, count, ¶m);
1628 netdev->trans_start = jiffies;
1629 spin_unlock_irqrestore(&adapter->lock, flags);
1630 atl1_update_mailbox(adapter);
1631 return NETDEV_TX_OK;
1635 * atl1_get_stats - Get System Network Statistics
1636 * @netdev: network interface device structure
1638 * Returns the address of the device statistics structure.
1639 * The statistics are actually updated from the timer callback.
1641 static struct net_device_stats *atl1_get_stats(struct net_device *netdev)
1643 struct atl1_adapter *adapter = netdev_priv(netdev);
1644 return &adapter->net_stats;
1648 * atl1_clean_rx_ring - Free RFD Buffers
1649 * @adapter: board private structure
1651 static void atl1_clean_rx_ring(struct atl1_adapter *adapter)
1653 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1654 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1655 struct atl1_buffer *buffer_info;
1656 struct pci_dev *pdev = adapter->pdev;
1660 /* Free all the Rx ring sk_buffs */
1661 for (i = 0; i < rfd_ring->count; i++) {
1662 buffer_info = &rfd_ring->buffer_info[i];
1663 if (buffer_info->dma) {
1664 pci_unmap_page(pdev,
1666 buffer_info->length,
1667 PCI_DMA_FROMDEVICE);
1668 buffer_info->dma = 0;
1670 if (buffer_info->skb) {
1671 dev_kfree_skb(buffer_info->skb);
1672 buffer_info->skb = NULL;
1676 size = sizeof(struct atl1_buffer) * rfd_ring->count;
1677 memset(rfd_ring->buffer_info, 0, size);
1679 /* Zero out the descriptor ring */
1680 memset(rfd_ring->desc, 0, rfd_ring->size);
1682 rfd_ring->next_to_clean = 0;
1683 atomic_set(&rfd_ring->next_to_use, 0);
1685 rrd_ring->next_to_use = 0;
1686 atomic_set(&rrd_ring->next_to_clean, 0);
1690 * atl1_clean_tx_ring - Free Tx Buffers
1691 * @adapter: board private structure
1693 static void atl1_clean_tx_ring(struct atl1_adapter *adapter)
1695 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1696 struct atl1_buffer *buffer_info;
1697 struct pci_dev *pdev = adapter->pdev;
1701 /* Free all the Tx ring sk_buffs */
1702 for (i = 0; i < tpd_ring->count; i++) {
1703 buffer_info = &tpd_ring->buffer_info[i];
1704 if (buffer_info->dma) {
1705 pci_unmap_page(pdev, buffer_info->dma,
1706 buffer_info->length, PCI_DMA_TODEVICE);
1707 buffer_info->dma = 0;
1711 for (i = 0; i < tpd_ring->count; i++) {
1712 buffer_info = &tpd_ring->buffer_info[i];
1713 if (buffer_info->skb) {
1714 dev_kfree_skb_any(buffer_info->skb);
1715 buffer_info->skb = NULL;
1719 size = sizeof(struct atl1_buffer) * tpd_ring->count;
1720 memset(tpd_ring->buffer_info, 0, size);
1722 /* Zero out the descriptor ring */
1723 memset(tpd_ring->desc, 0, tpd_ring->size);
1725 atomic_set(&tpd_ring->next_to_use, 0);
1726 atomic_set(&tpd_ring->next_to_clean, 0);
1730 * atl1_free_ring_resources - Free Tx / RX descriptor Resources
1731 * @adapter: board private structure
1733 * Free all transmit software resources
1735 void atl1_free_ring_resources(struct atl1_adapter *adapter)
1737 struct pci_dev *pdev = adapter->pdev;
1738 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1739 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1740 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1741 struct atl1_ring_header *ring_header = &adapter->ring_header;
1743 atl1_clean_tx_ring(adapter);
1744 atl1_clean_rx_ring(adapter);
1746 kfree(tpd_ring->buffer_info);
1747 pci_free_consistent(pdev, ring_header->size, ring_header->desc,
1750 tpd_ring->buffer_info = NULL;
1751 tpd_ring->desc = NULL;
1754 rfd_ring->buffer_info = NULL;
1755 rfd_ring->desc = NULL;
1758 rrd_ring->desc = NULL;
1762 s32 atl1_up(struct atl1_adapter *adapter)
1764 struct net_device *netdev = adapter->netdev;
1766 int irq_flags = IRQF_SAMPLE_RANDOM;
1768 /* hardware has been reset, we need to reload some things */
1769 atl1_set_multi(netdev);
1770 atl1_restore_vlan(adapter);
1771 err = atl1_alloc_rx_buffers(adapter);
1772 if (unlikely(!err)) /* no RX BUFFER allocated */
1775 if (unlikely(atl1_configure(adapter))) {
1780 err = pci_enable_msi(adapter->pdev);
1782 dev_info(&adapter->pdev->dev,
1783 "Unable to enable MSI: %d\n", err);
1784 irq_flags |= IRQF_SHARED;
1787 err = request_irq(adapter->pdev->irq, &atl1_intr, irq_flags,
1788 netdev->name, netdev);
1792 mod_timer(&adapter->watchdog_timer, jiffies);
1793 atl1_irq_enable(adapter);
1794 atl1_check_link(adapter);
1797 /* FIXME: unreachable code! -- CHS */
1798 /* free irq disable any interrupt */
1799 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
1800 free_irq(adapter->pdev->irq, netdev);
1803 pci_disable_msi(adapter->pdev);
1804 /* free rx_buffers */
1805 atl1_clean_rx_ring(adapter);
1809 void atl1_down(struct atl1_adapter *adapter)
1811 struct net_device *netdev = adapter->netdev;
1813 del_timer_sync(&adapter->watchdog_timer);
1814 del_timer_sync(&adapter->phy_config_timer);
1815 adapter->phy_timer_pending = false;
1817 atl1_irq_disable(adapter);
1818 free_irq(adapter->pdev->irq, netdev);
1819 pci_disable_msi(adapter->pdev);
1820 atl1_reset_hw(&adapter->hw);
1821 adapter->cmb.cmb->int_stats = 0;
1823 adapter->link_speed = SPEED_0;
1824 adapter->link_duplex = -1;
1825 netif_carrier_off(netdev);
1826 netif_stop_queue(netdev);
1828 atl1_clean_tx_ring(adapter);
1829 atl1_clean_rx_ring(adapter);
1833 * atl1_change_mtu - Change the Maximum Transfer Unit
1834 * @netdev: network interface device structure
1835 * @new_mtu: new value for maximum frame size
1837 * Returns 0 on success, negative on failure
1839 static int atl1_change_mtu(struct net_device *netdev, int new_mtu)
1841 struct atl1_adapter *adapter = netdev_priv(netdev);
1842 int old_mtu = netdev->mtu;
1843 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
1845 if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
1846 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
1847 printk(KERN_WARNING "%s: invalid MTU setting\n",
1852 adapter->hw.max_frame_size = max_frame;
1853 adapter->hw.tx_jumbo_task_th = (max_frame + 7) >> 3;
1854 adapter->rx_buffer_len = (max_frame + 7) & ~7;
1855 adapter->hw.rx_jumbo_th = adapter->rx_buffer_len / 8;
1857 netdev->mtu = new_mtu;
1858 if ((old_mtu != new_mtu) && netif_running(netdev)) {
1867 * atl1_set_mac - Change the Ethernet Address of the NIC
1868 * @netdev: network interface device structure
1869 * @p: pointer to an address structure
1871 * Returns 0 on success, negative on failure
1873 static int atl1_set_mac(struct net_device *netdev, void *p)
1875 struct atl1_adapter *adapter = netdev_priv(netdev);
1876 struct sockaddr *addr = p;
1878 if (netif_running(netdev))
1881 if (!is_valid_ether_addr(addr->sa_data))
1882 return -EADDRNOTAVAIL;
1884 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1885 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
1887 atl1_set_mac_addr(&adapter->hw);
1892 * atl1_watchdog - Timer Call-back
1893 * @data: pointer to netdev cast into an unsigned long
1895 static void atl1_watchdog(unsigned long data)
1897 struct atl1_adapter *adapter = (struct atl1_adapter *)data;
1899 /* Reset the timer */
1900 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1903 static int mdio_read(struct net_device *netdev, int phy_id, int reg_num)
1905 struct atl1_adapter *adapter = netdev_priv(netdev);
1908 atl1_read_phy_reg(&adapter->hw, reg_num & 0x1f, &result);
1913 static void mdio_write(struct net_device *netdev, int phy_id, int reg_num, int val)
1915 struct atl1_adapter *adapter = netdev_priv(netdev);
1917 atl1_write_phy_reg(&adapter->hw, reg_num, val);
1926 static int atl1_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1928 struct atl1_adapter *adapter = netdev_priv(netdev);
1929 unsigned long flags;
1932 if (!netif_running(netdev))
1935 spin_lock_irqsave(&adapter->lock, flags);
1936 retval = generic_mii_ioctl(&adapter->mii, if_mii(ifr), cmd, NULL);
1937 spin_unlock_irqrestore(&adapter->lock, flags);
1948 static int atl1_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1954 return atl1_mii_ioctl(netdev, ifr, cmd);
1961 * atl1_tx_timeout - Respond to a Tx Hang
1962 * @netdev: network interface device structure
1964 static void atl1_tx_timeout(struct net_device *netdev)
1966 struct atl1_adapter *adapter = netdev_priv(netdev);
1967 /* Do the reset outside of interrupt context */
1968 schedule_work(&adapter->tx_timeout_task);
1972 * atl1_phy_config - Timer Call-back
1973 * @data: pointer to netdev cast into an unsigned long
1975 static void atl1_phy_config(unsigned long data)
1977 struct atl1_adapter *adapter = (struct atl1_adapter *)data;
1978 struct atl1_hw *hw = &adapter->hw;
1979 unsigned long flags;
1981 spin_lock_irqsave(&adapter->lock, flags);
1982 adapter->phy_timer_pending = false;
1983 atl1_write_phy_reg(hw, MII_ADVERTISE, hw->mii_autoneg_adv_reg);
1984 atl1_write_phy_reg(hw, MII_AT001_CR, hw->mii_1000t_ctrl_reg);
1985 atl1_write_phy_reg(hw, MII_BMCR, MII_CR_RESET | MII_CR_AUTO_NEG_EN);
1986 spin_unlock_irqrestore(&adapter->lock, flags);
1989 int atl1_reset(struct atl1_adapter *adapter)
1993 ret = atl1_reset_hw(&adapter->hw);
1994 if (ret != ATL1_SUCCESS)
1996 return atl1_init_hw(&adapter->hw);
2000 * atl1_open - Called when a network interface is made active
2001 * @netdev: network interface device structure
2003 * Returns 0 on success, negative value on failure
2005 * The open entry point is called when a network interface is made
2006 * active by the system (IFF_UP). At this point all resources needed
2007 * for transmit and receive operations are allocated, the interrupt
2008 * handler is registered with the OS, the watchdog timer is started,
2009 * and the stack is notified that the interface is ready.
2011 static int atl1_open(struct net_device *netdev)
2013 struct atl1_adapter *adapter = netdev_priv(netdev);
2016 /* allocate transmit descriptors */
2017 err = atl1_setup_ring_resources(adapter);
2021 err = atl1_up(adapter);
2028 atl1_reset(adapter);
2033 * atl1_close - Disables a network interface
2034 * @netdev: network interface device structure
2036 * Returns 0, this is not allowed to fail
2038 * The close entry point is called when an interface is de-activated
2039 * by the OS. The hardware is still under the drivers control, but
2040 * needs to be disabled. A global MAC reset is issued to stop the
2041 * hardware, and all transmit and receive resources are freed.
2043 static int atl1_close(struct net_device *netdev)
2045 struct atl1_adapter *adapter = netdev_priv(netdev);
2047 atl1_free_ring_resources(adapter);
2052 * If TPD Buffer size equal to 0, PCIE DMAR_TO_INT
2053 * will assert. We do soft reset <0x1400=1> according
2054 * with the SPEC. BUT, it seemes that PCIE or DMA
2055 * state-machine will not be reset. DMAR_TO_INT will
2056 * assert again and again.
2058 static void atl1_tx_timeout_task(struct work_struct *work)
2060 struct atl1_adapter *adapter =
2061 container_of(work, struct atl1_adapter, tx_timeout_task);
2062 struct net_device *netdev = adapter->netdev;
2064 netif_device_detach(netdev);
2067 netif_device_attach(netdev);
2071 * atl1_link_chg_task - deal with link change event Out of interrupt context
2073 static void atl1_link_chg_task(struct work_struct *work)
2075 struct atl1_adapter *adapter =
2076 container_of(work, struct atl1_adapter, link_chg_task);
2077 unsigned long flags;
2079 spin_lock_irqsave(&adapter->lock, flags);
2080 atl1_check_link(adapter);
2081 spin_unlock_irqrestore(&adapter->lock, flags);
2085 * atl1_pcie_patch - Patch for PCIE module
2087 static void atl1_pcie_patch(struct atl1_adapter *adapter)
2091 iowrite32(value, adapter->hw.hw_addr + 0x12FC);
2092 /* pcie flow control mode change */
2093 value = ioread32(adapter->hw.hw_addr + 0x1008);
2095 iowrite32(value, adapter->hw.hw_addr + 0x1008);
2099 * When ACPI resume on some VIA MotherBoard, the Interrupt Disable bit/0x400
2100 * on PCI Command register is disable.
2101 * The function enable this bit.
2102 * Brackett, 2006/03/15
2104 static void atl1_via_workaround(struct atl1_adapter *adapter)
2106 unsigned long value;
2108 value = ioread16(adapter->hw.hw_addr + PCI_COMMAND);
2109 if (value & PCI_COMMAND_INTX_DISABLE)
2110 value &= ~PCI_COMMAND_INTX_DISABLE;
2111 iowrite32(value, adapter->hw.hw_addr + PCI_COMMAND);
2115 * atl1_probe - Device Initialization Routine
2116 * @pdev: PCI device information struct
2117 * @ent: entry in atl1_pci_tbl
2119 * Returns 0 on success, negative on failure
2121 * atl1_probe initializes an adapter identified by a pci_dev structure.
2122 * The OS initialization, configuring of the adapter private structure,
2123 * and a hardware reset occur.
2125 static int __devinit atl1_probe(struct pci_dev *pdev,
2126 const struct pci_device_id *ent)
2128 struct net_device *netdev;
2129 struct atl1_adapter *adapter;
2130 static int cards_found = 0;
2131 bool pci_using_64 = true;
2134 err = pci_enable_device(pdev);
2138 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
2140 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2143 "%s: no usable DMA configuration, aborting\n",
2147 pci_using_64 = false;
2149 /* Mark all PCI regions associated with PCI device
2150 * pdev as being reserved by owner atl1_driver_name
2152 err = pci_request_regions(pdev, atl1_driver_name);
2154 goto err_request_regions;
2156 /* Enables bus-mastering on the device and calls
2157 * pcibios_set_master to do the needed arch specific settings
2159 pci_set_master(pdev);
2161 netdev = alloc_etherdev(sizeof(struct atl1_adapter));
2164 goto err_alloc_etherdev;
2166 SET_MODULE_OWNER(netdev);
2167 SET_NETDEV_DEV(netdev, &pdev->dev);
2169 pci_set_drvdata(pdev, netdev);
2170 adapter = netdev_priv(netdev);
2171 adapter->netdev = netdev;
2172 adapter->pdev = pdev;
2173 adapter->hw.back = adapter;
2175 adapter->hw.hw_addr = pci_iomap(pdev, 0, 0);
2176 if (!adapter->hw.hw_addr) {
2180 /* get device revision number */
2181 adapter->hw.dev_rev = ioread16(adapter->hw.hw_addr + (REG_MASTER_CTRL + 2));
2183 /* set default ring resource counts */
2184 adapter->rfd_ring.count = adapter->rrd_ring.count = ATL1_DEFAULT_RFD;
2185 adapter->tpd_ring.count = ATL1_DEFAULT_TPD;
2187 adapter->mii.dev = netdev;
2188 adapter->mii.mdio_read = mdio_read;
2189 adapter->mii.mdio_write = mdio_write;
2190 adapter->mii.phy_id_mask = 0x1f;
2191 adapter->mii.reg_num_mask = 0x1f;
2193 netdev->open = &atl1_open;
2194 netdev->stop = &atl1_close;
2195 netdev->hard_start_xmit = &atl1_xmit_frame;
2196 netdev->get_stats = &atl1_get_stats;
2197 netdev->set_multicast_list = &atl1_set_multi;
2198 netdev->set_mac_address = &atl1_set_mac;
2199 netdev->change_mtu = &atl1_change_mtu;
2200 netdev->do_ioctl = &atl1_ioctl;
2201 netdev->tx_timeout = &atl1_tx_timeout;
2202 netdev->watchdog_timeo = 5 * HZ;
2203 netdev->vlan_rx_register = atl1_vlan_rx_register;
2204 netdev->vlan_rx_add_vid = atl1_vlan_rx_add_vid;
2205 netdev->vlan_rx_kill_vid = atl1_vlan_rx_kill_vid;
2206 netdev->ethtool_ops = &atl1_ethtool_ops;
2207 adapter->bd_number = cards_found;
2208 adapter->pci_using_64 = pci_using_64;
2210 /* setup the private structure */
2211 err = atl1_sw_init(adapter);
2215 netdev->features = NETIF_F_HW_CSUM;
2216 netdev->features |= NETIF_F_SG;
2217 netdev->features |= (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
2220 * FIXME - Until tso performance gets fixed, disable the feature.
2221 * Enable it with ethtool -K if desired.
2223 /* netdev->features |= NETIF_F_TSO; */
2226 netdev->features |= NETIF_F_HIGHDMA;
2228 netdev->features |= NETIF_F_LLTX;
2231 * patch for some L1 of old version,
2232 * the final version of L1 may not need these
2235 /* atl1_pcie_patch(adapter); */
2237 /* really reset GPHY core */
2238 iowrite16(0, adapter->hw.hw_addr + REG_GPHY_ENABLE);
2241 * reset the controller to
2242 * put the device in a known good starting state
2244 if (atl1_reset_hw(&adapter->hw)) {
2249 /* copy the MAC address out of the EEPROM */
2250 atl1_read_mac_addr(&adapter->hw);
2251 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
2253 if (!is_valid_ether_addr(netdev->dev_addr)) {
2258 atl1_check_options(adapter);
2260 /* pre-init the MAC, and setup link */
2261 err = atl1_init_hw(&adapter->hw);
2267 atl1_pcie_patch(adapter);
2268 /* assume we have no link for now */
2269 netif_carrier_off(netdev);
2270 netif_stop_queue(netdev);
2272 init_timer(&adapter->watchdog_timer);
2273 adapter->watchdog_timer.function = &atl1_watchdog;
2274 adapter->watchdog_timer.data = (unsigned long)adapter;
2276 init_timer(&adapter->phy_config_timer);
2277 adapter->phy_config_timer.function = &atl1_phy_config;
2278 adapter->phy_config_timer.data = (unsigned long)adapter;
2279 adapter->phy_timer_pending = false;
2281 INIT_WORK(&adapter->tx_timeout_task, atl1_tx_timeout_task);
2283 INIT_WORK(&adapter->link_chg_task, atl1_link_chg_task);
2285 INIT_WORK(&adapter->pcie_dma_to_rst_task, atl1_tx_timeout_task);
2287 err = register_netdev(netdev);
2292 atl1_via_workaround(adapter);
2296 pci_iounmap(pdev, adapter->hw.hw_addr);
2298 free_netdev(netdev);
2300 pci_release_regions(pdev);
2302 err_request_regions:
2303 pci_disable_device(pdev);
2308 * atl1_remove - Device Removal Routine
2309 * @pdev: PCI device information struct
2311 * atl1_remove is called by the PCI subsystem to alert the driver
2312 * that it should release a PCI device. The could be caused by a
2313 * Hot-Plug event, or because the driver is going to be removed from
2316 static void __devexit atl1_remove(struct pci_dev *pdev)
2318 struct net_device *netdev = pci_get_drvdata(pdev);
2319 struct atl1_adapter *adapter;
2320 /* Device not available. Return. */
2324 adapter = netdev_priv(netdev);
2325 iowrite16(0, adapter->hw.hw_addr + REG_GPHY_ENABLE);
2326 unregister_netdev(netdev);
2327 pci_iounmap(pdev, adapter->hw.hw_addr);
2328 pci_release_regions(pdev);
2329 free_netdev(netdev);
2330 pci_disable_device(pdev);
2334 static int atl1_suspend(struct pci_dev *pdev, pm_message_t state)
2336 struct net_device *netdev = pci_get_drvdata(pdev);
2337 struct atl1_adapter *adapter = netdev_priv(netdev);
2338 struct atl1_hw *hw = &adapter->hw;
2340 u32 wufc = adapter->wol;
2342 netif_device_detach(netdev);
2343 if (netif_running(netdev))
2346 atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2347 atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2348 if (ctrl & BMSR_LSTATUS)
2349 wufc &= ~ATL1_WUFC_LNKC;
2351 /* reduce speed to 10/100M */
2353 atl1_phy_enter_power_saving(hw);
2354 /* if resume, let driver to re- setup link */
2355 hw->phy_configured = false;
2356 atl1_set_mac_addr(hw);
2357 atl1_set_multi(netdev);
2360 /* turn on magic packet wol */
2361 if (wufc & ATL1_WUFC_MAG)
2362 ctrl = WOL_MAGIC_EN | WOL_MAGIC_PME_EN;
2364 /* turn on Link change WOL */
2365 if (wufc & ATL1_WUFC_LNKC)
2366 ctrl |= (WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN);
2367 iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
2369 /* turn on all-multi mode if wake on multicast is enabled */
2370 ctrl = ioread32(hw->hw_addr + REG_MAC_CTRL);
2371 ctrl &= ~MAC_CTRL_DBG;
2372 ctrl &= ~MAC_CTRL_PROMIS_EN;
2373 if (wufc & ATL1_WUFC_MC)
2374 ctrl |= MAC_CTRL_MC_ALL_EN;
2376 ctrl &= ~MAC_CTRL_MC_ALL_EN;
2378 /* turn on broadcast mode if wake on-BC is enabled */
2379 if (wufc & ATL1_WUFC_BC)
2380 ctrl |= MAC_CTRL_BC_EN;
2382 ctrl &= ~MAC_CTRL_BC_EN;
2385 ctrl |= MAC_CTRL_RX_EN;
2386 iowrite32(ctrl, hw->hw_addr + REG_MAC_CTRL);
2387 pci_enable_wake(pdev, PCI_D3hot, 1);
2388 pci_enable_wake(pdev, PCI_D3cold, 1); /* 4 == D3 cold */
2390 iowrite32(0, hw->hw_addr + REG_WOL_CTRL);
2391 pci_enable_wake(pdev, PCI_D3hot, 0);
2392 pci_enable_wake(pdev, PCI_D3cold, 0); /* 4 == D3 cold */
2395 pci_save_state(pdev);
2396 pci_disable_device(pdev);
2398 pci_set_power_state(pdev, PCI_D3hot);
2403 static int atl1_resume(struct pci_dev *pdev)
2405 struct net_device *netdev = pci_get_drvdata(pdev);
2406 struct atl1_adapter *adapter = netdev_priv(netdev);
2409 pci_set_power_state(pdev, 0);
2410 pci_restore_state(pdev);
2412 ret_val = pci_enable_device(pdev);
2413 pci_enable_wake(pdev, PCI_D3hot, 0);
2414 pci_enable_wake(pdev, PCI_D3cold, 0);
2416 iowrite32(0, adapter->hw.hw_addr + REG_WOL_CTRL);
2417 atl1_reset(adapter);
2419 if (netif_running(netdev))
2421 netif_device_attach(netdev);
2423 atl1_via_workaround(adapter);
2428 #define atl1_suspend NULL
2429 #define atl1_resume NULL
2432 static struct pci_driver atl1_driver = {
2433 .name = atl1_driver_name,
2434 .id_table = atl1_pci_tbl,
2435 .probe = atl1_probe,
2436 .remove = __devexit_p(atl1_remove),
2437 /* Power Managment Hooks */
2438 /* probably broken right now -- CHS */
2439 .suspend = atl1_suspend,
2440 .resume = atl1_resume
2444 * atl1_exit_module - Driver Exit Cleanup Routine
2446 * atl1_exit_module is called just before the driver is removed
2449 static void __exit atl1_exit_module(void)
2451 pci_unregister_driver(&atl1_driver);
2455 * atl1_init_module - Driver Registration Routine
2457 * atl1_init_module is the first routine called when the driver is
2458 * loaded. All it does is register with the PCI subsystem.
2460 static int __init atl1_init_module(void)
2462 printk(KERN_INFO "%s - version %s\n", atl1_driver_string, DRIVER_VERSION);
2463 printk(KERN_INFO "%s\n", atl1_copyright);
2464 return pci_register_driver(&atl1_driver);
2467 module_init(atl1_init_module);
2468 module_exit(atl1_exit_module);