staging: et131x: Remove last of the forward declarations
[pandora-kernel.git] / drivers / staging / et131x / et131x.c
1 /*
2  * Agere Systems Inc.
3  * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
4  *
5  * Copyright © 2005 Agere Systems Inc.
6  * All rights reserved.
7  *   http://www.agere.com
8  *
9  * Copyright (c) 2011 Mark Einon <mark.einon@gmail.com>
10  *
11  *------------------------------------------------------------------------------
12  *
13  * SOFTWARE LICENSE
14  *
15  * This software is provided subject to the following terms and conditions,
16  * which you should read carefully before using the software.  Using this
17  * software indicates your acceptance of these terms and conditions.  If you do
18  * not agree with these terms and conditions, do not use the software.
19  *
20  * Copyright © 2005 Agere Systems Inc.
21  * All rights reserved.
22  *
23  * Redistribution and use in source or binary forms, with or without
24  * modifications, are permitted provided that the following conditions are met:
25  *
26  * . Redistributions of source code must retain the above copyright notice, this
27  *    list of conditions and the following Disclaimer as comments in the code as
28  *    well as in the documentation and/or other materials provided with the
29  *    distribution.
30  *
31  * . Redistributions in binary form must reproduce the above copyright notice,
32  *    this list of conditions and the following Disclaimer in the documentation
33  *    and/or other materials provided with the distribution.
34  *
35  * . Neither the name of Agere Systems Inc. nor the names of the contributors
36  *    may be used to endorse or promote products derived from this software
37  *    without specific prior written permission.
38  *
39  * Disclaimer
40  *
41  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
42  * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
43  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ANY
44  * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
45  * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
46  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
52  * DAMAGE.
53  *
54  */
55
56 #include <linux/pci.h>
57 #include <linux/init.h>
58 #include <linux/module.h>
59 #include <linux/types.h>
60 #include <linux/kernel.h>
61
62 #include <linux/sched.h>
63 #include <linux/ptrace.h>
64 #include <linux/slab.h>
65 #include <linux/ctype.h>
66 #include <linux/string.h>
67 #include <linux/timer.h>
68 #include <linux/interrupt.h>
69 #include <linux/in.h>
70 #include <linux/delay.h>
71 #include <linux/bitops.h>
72 #include <linux/io.h>
73 #include <asm/system.h>
74
75 #include <linux/netdevice.h>
76 #include <linux/etherdevice.h>
77 #include <linux/skbuff.h>
78 #include <linux/if_arp.h>
79 #include <linux/ioport.h>
80 #include <linux/crc32.h>
81 #include <linux/random.h>
82 #include <linux/phy.h>
83
84 #include "et131x.h"
85
86 MODULE_AUTHOR("Victor Soriano <vjsoriano@agere.com>");
87 MODULE_AUTHOR("Mark Einon <mark.einon@gmail.com>");
88 MODULE_LICENSE("Dual BSD/GPL");
89 MODULE_DESCRIPTION("10/100/1000 Base-T Ethernet Driver "
90                    "for the ET1310 by Agere Systems");
91
92 /* EEPROM defines */
93 #define MAX_NUM_REGISTER_POLLS          1000
94 #define MAX_NUM_WRITE_RETRIES           2
95
96 /* MAC defines */
97 #define COUNTER_WRAP_16_BIT 0x10000
98 #define COUNTER_WRAP_12_BIT 0x1000
99
100 /* PCI defines */
101 #define INTERNAL_MEM_SIZE       0x400   /* 1024 of internal memory */
102 #define INTERNAL_MEM_RX_OFFSET  0x1FF   /* 50%   Tx, 50%   Rx */
103
104 /* ISR defines */
105 /*
106  * For interrupts, normal running is:
107  *       rxdma_xfr_done, phy_interrupt, mac_stat_interrupt,
108  *       watchdog_interrupt & txdma_xfer_done
109  *
110  * In both cases, when flow control is enabled for either Tx or bi-direction,
111  * we additional enable rx_fbr0_low and rx_fbr1_low, so we know when the
112  * buffer rings are running low.
113  */
114 #define INT_MASK_DISABLE            0xffffffff
115
116 /* NOTE: Masking out MAC_STAT Interrupt for now...
117  * #define INT_MASK_ENABLE             0xfff6bf17
118  * #define INT_MASK_ENABLE_NO_FLOW     0xfff6bfd7
119  */
120 #define INT_MASK_ENABLE             0xfffebf17
121 #define INT_MASK_ENABLE_NO_FLOW     0xfffebfd7
122
123 /* General defines */
124 /* Packet and header sizes */
125 #define NIC_MIN_PACKET_SIZE     60
126
127 /* Multicast list size */
128 #define NIC_MAX_MCAST_LIST      128
129
130 /* Supported Filters */
131 #define ET131X_PACKET_TYPE_DIRECTED             0x0001
132 #define ET131X_PACKET_TYPE_MULTICAST            0x0002
133 #define ET131X_PACKET_TYPE_BROADCAST            0x0004
134 #define ET131X_PACKET_TYPE_PROMISCUOUS          0x0008
135 #define ET131X_PACKET_TYPE_ALL_MULTICAST        0x0010
136
137 /* Tx Timeout */
138 #define ET131X_TX_TIMEOUT       (1 * HZ)
139 #define NIC_SEND_HANG_THRESHOLD 0
140
141 /* MP_TCB flags */
142 #define fMP_DEST_MULTI                  0x00000001
143 #define fMP_DEST_BROAD                  0x00000002
144
145 /* MP_ADAPTER flags */
146 #define fMP_ADAPTER_RECV_LOOKASIDE      0x00000004
147 #define fMP_ADAPTER_INTERRUPT_IN_USE    0x00000008
148
149 /* MP_SHARED flags */
150 #define fMP_ADAPTER_LOWER_POWER         0x00200000
151
152 #define fMP_ADAPTER_NON_RECOVER_ERROR   0x00800000
153 #define fMP_ADAPTER_HARDWARE_ERROR      0x04000000
154
155 #define fMP_ADAPTER_FAIL_SEND_MASK      0x3ff00000
156
157 /* Some offsets in PCI config space that are actually used. */
158 #define ET1310_PCI_MAX_PYLD             0x4C
159 #define ET1310_PCI_MAC_ADDRESS          0xA4
160 #define ET1310_PCI_EEPROM_STATUS        0xB2
161 #define ET1310_PCI_ACK_NACK             0xC0
162 #define ET1310_PCI_REPLAY               0xC2
163 #define ET1310_PCI_L0L1LATENCY          0xCF
164
165 /* PCI Product IDs */
166 #define ET131X_PCI_DEVICE_ID_GIG        0xED00  /* ET1310 1000 Base-T 8 */
167 #define ET131X_PCI_DEVICE_ID_FAST       0xED01  /* ET1310 100  Base-T */
168
169 /* Define order of magnitude converter */
170 #define NANO_IN_A_MICRO 1000
171
172 #define PARM_RX_NUM_BUFS_DEF    4
173 #define PARM_RX_TIME_INT_DEF    10
174 #define PARM_RX_MEM_END_DEF     0x2bc
175 #define PARM_TX_TIME_INT_DEF    40
176 #define PARM_TX_NUM_BUFS_DEF    4
177 #define PARM_DMA_CACHE_DEF      0
178
179 /* RX defines */
180 #define USE_FBR0 1
181
182 #define FBR_CHUNKS 32
183
184 #define MAX_DESC_PER_RING_RX         1024
185
186 /* number of RFDs - default and min */
187 #ifdef USE_FBR0
188 #define RFD_LOW_WATER_MARK      40
189 #define NIC_DEFAULT_NUM_RFD     1024
190 #define NUM_FBRS                2
191 #else
192 #define RFD_LOW_WATER_MARK      20
193 #define NIC_DEFAULT_NUM_RFD     256
194 #define NUM_FBRS                1
195 #endif
196
197 #define NIC_MIN_NUM_RFD         64
198
199 #define NUM_PACKETS_HANDLED     256
200
201 #define ALCATEL_MULTICAST_PKT   0x01000000
202 #define ALCATEL_BROADCAST_PKT   0x02000000
203
204 /* typedefs for Free Buffer Descriptors */
205 struct fbr_desc {
206         u32 addr_lo;
207         u32 addr_hi;
208         u32 word2;              /* Bits 10-31 reserved, 0-9 descriptor */
209 };
210
211 /* Packet Status Ring Descriptors
212  *
213  * Word 0:
214  *
215  * top 16 bits are from the Alcatel Status Word as enumerated in
216  * PE-MCXMAC Data Sheet IPD DS54 0210-1 (also IPD-DS80 0205-2)
217  *
218  * 0: hp                        hash pass
219  * 1: ipa                       IP checksum assist
220  * 2: ipp                       IP checksum pass
221  * 3: tcpa                      TCP checksum assist
222  * 4: tcpp                      TCP checksum pass
223  * 5: wol                       WOL Event
224  * 6: rxmac_error               RXMAC Error Indicator
225  * 7: drop                      Drop packet
226  * 8: ft                        Frame Truncated
227  * 9: jp                        Jumbo Packet
228  * 10: vp                       VLAN Packet
229  * 11-15: unused
230  * 16: asw_prev_pkt_dropped     e.g. IFG too small on previous
231  * 17: asw_RX_DV_event          short receive event detected
232  * 18: asw_false_carrier_event  bad carrier since last good packet
233  * 19: asw_code_err             one or more nibbles signalled as errors
234  * 20: asw_CRC_err              CRC error
235  * 21: asw_len_chk_err          frame length field incorrect
236  * 22: asw_too_long             frame length > 1518 bytes
237  * 23: asw_OK                   valid CRC + no code error
238  * 24: asw_multicast            has a multicast address
239  * 25: asw_broadcast            has a broadcast address
240  * 26: asw_dribble_nibble       spurious bits after EOP
241  * 27: asw_control_frame        is a control frame
242  * 28: asw_pause_frame          is a pause frame
243  * 29: asw_unsupported_op       unsupported OP code
244  * 30: asw_VLAN_tag             VLAN tag detected
245  * 31: asw_long_evt             Rx long event
246  *
247  * Word 1:
248  * 0-15: length                 length in bytes
249  * 16-25: bi                    Buffer Index
250  * 26-27: ri                    Ring Index
251  * 28-31: reserved
252  */
253
254 struct pkt_stat_desc {
255         u32 word0;
256         u32 word1;
257 };
258
259 /* Typedefs for the RX DMA status word */
260
261 /*
262  * rx status word 0 holds part of the status bits of the Rx DMA engine
263  * that get copied out to memory by the ET-1310.  Word 0 is a 32 bit word
264  * which contains the Free Buffer ring 0 and 1 available offset.
265  *
266  * bit 0-9 FBR1 offset
267  * bit 10 Wrap flag for FBR1
268  * bit 16-25 FBR0 offset
269  * bit 26 Wrap flag for FBR0
270  */
271
272 /*
273  * RXSTAT_WORD1_t structure holds part of the status bits of the Rx DMA engine
274  * that get copied out to memory by the ET-1310.  Word 3 is a 32 bit word
275  * which contains the Packet Status Ring available offset.
276  *
277  * bit 0-15 reserved
278  * bit 16-27 PSRoffset
279  * bit 28 PSRwrap
280  * bit 29-31 unused
281  */
282
283 /*
284  * struct rx_status_block is a structure representing the status of the Rx
285  * DMA engine it sits in free memory, and is pointed to by 0x101c / 0x1020
286  */
287 struct rx_status_block {
288         u32 word0;
289         u32 word1;
290 };
291
292 /*
293  * Structure for look-up table holding free buffer ring pointers, addresses
294  * and state.
295  */
296 struct fbr_lookup {
297         void            *virt[MAX_DESC_PER_RING_RX];
298         void            *buffer1[MAX_DESC_PER_RING_RX];
299         void            *buffer2[MAX_DESC_PER_RING_RX];
300         u32              bus_high[MAX_DESC_PER_RING_RX];
301         u32              bus_low[MAX_DESC_PER_RING_RX];
302         void            *ring_virtaddr;
303         dma_addr_t       ring_physaddr;
304         void            *mem_virtaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
305         dma_addr_t       mem_physaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
306         uint64_t         real_physaddr;
307         uint64_t         offset;
308         u32              local_full;
309         u32              num_entries;
310         u32              buffsize;
311 };
312
313 /*
314  * struct rx_ring is the sructure representing the adaptor's local
315  * reference(s) to the rings
316  *
317  ******************************************************************************
318  * IMPORTANT NOTE :- fbr_lookup *fbr[NUM_FBRS] uses index 0 to refer to FBR1
319  *                      and index 1 to refer to FRB0
320  ******************************************************************************
321  */
322 struct rx_ring {
323         struct fbr_lookup *fbr[NUM_FBRS];
324         void *ps_ring_virtaddr;
325         dma_addr_t ps_ring_physaddr;
326         u32 local_psr_full;
327         u32 psr_num_entries;
328
329         struct rx_status_block *rx_status_block;
330         dma_addr_t rx_status_bus;
331
332         /* RECV */
333         struct list_head recv_list;
334         u32 num_ready_recv;
335
336         u32 num_rfd;
337
338         bool unfinished_receives;
339
340         /* lookaside lists */
341         struct kmem_cache *recv_lookaside;
342 };
343
344 /* TX defines */
345 /*
346  * word 2 of the control bits in the Tx Descriptor ring for the ET-1310
347  *
348  * 0-15: length of packet
349  * 16-27: VLAN tag
350  * 28: VLAN CFI
351  * 29-31: VLAN priority
352  *
353  * word 3 of the control bits in the Tx Descriptor ring for the ET-1310
354  *
355  * 0: last packet in the sequence
356  * 1: first packet in the sequence
357  * 2: interrupt the processor when this pkt sent
358  * 3: Control word - no packet data
359  * 4: Issue half-duplex backpressure : XON/XOFF
360  * 5: send pause frame
361  * 6: Tx frame has error
362  * 7: append CRC
363  * 8: MAC override
364  * 9: pad packet
365  * 10: Packet is a Huge packet
366  * 11: append VLAN tag
367  * 12: IP checksum assist
368  * 13: TCP checksum assist
369  * 14: UDP checksum assist
370  */
371
372 /* struct tx_desc represents each descriptor on the ring */
373 struct tx_desc {
374         u32 addr_hi;
375         u32 addr_lo;
376         u32 len_vlan;   /* control words how to xmit the */
377         u32 flags;      /* data (detailed above) */
378 };
379
380 /*
381  * The status of the Tx DMA engine it sits in free memory, and is pointed to
382  * by 0x101c / 0x1020. This is a DMA10 type
383  */
384
385 /* TCB (Transmit Control Block: Host Side) */
386 struct tcb {
387         struct tcb *next;       /* Next entry in ring */
388         u32 flags;              /* Our flags for the packet */
389         u32 count;              /* Used to spot stuck/lost packets */
390         u32 stale;              /* Used to spot stuck/lost packets */
391         struct sk_buff *skb;    /* Network skb we are tied to */
392         u32 index;              /* Ring indexes */
393         u32 index_start;
394 };
395
396 /* Structure representing our local reference(s) to the ring */
397 struct tx_ring {
398         /* TCB (Transmit Control Block) memory and lists */
399         struct tcb *tcb_ring;
400
401         /* List of TCBs that are ready to be used */
402         struct tcb *tcb_qhead;
403         struct tcb *tcb_qtail;
404
405         /* list of TCBs that are currently being sent.  NOTE that access to all
406          * three of these (including used) are controlled via the
407          * TCBSendQLock.  This lock should be secured prior to incementing /
408          * decrementing used, or any queue manipulation on send_head /
409          * tail
410          */
411         struct tcb *send_head;
412         struct tcb *send_tail;
413         int used;
414
415         /* The actual descriptor ring */
416         struct tx_desc *tx_desc_ring;
417         dma_addr_t tx_desc_ring_pa;
418
419         /* send_idx indicates where we last wrote to in the descriptor ring. */
420         u32 send_idx;
421
422         /* The location of the write-back status block */
423         u32 *tx_status;
424         dma_addr_t tx_status_pa;
425
426         /* Packets since the last IRQ: used for interrupt coalescing */
427         int since_irq;
428 };
429
430 /* ADAPTER defines */
431 /*
432  * Do not change these values: if changed, then change also in respective
433  * TXdma and Rxdma engines
434  */
435 #define NUM_DESC_PER_RING_TX         512    /* TX Do not change these values */
436 #define NUM_TCB                      64
437
438 /*
439  * These values are all superseded by registry entries to facilitate tuning.
440  * Once the desired performance has been achieved, the optimal registry values
441  * should be re-populated to these #defines:
442  */
443 #define TX_ERROR_PERIOD             1000
444
445 #define LO_MARK_PERCENT_FOR_PSR     15
446 #define LO_MARK_PERCENT_FOR_RX      15
447
448 /* RFD (Receive Frame Descriptor) */
449 struct rfd {
450         struct list_head list_node;
451         struct sk_buff *skb;
452         u32 len;        /* total size of receive frame */
453         u16 bufferindex;
454         u8 ringindex;
455 };
456
457 /* Flow Control */
458 #define FLOW_BOTH       0
459 #define FLOW_TXONLY     1
460 #define FLOW_RXONLY     2
461 #define FLOW_NONE       3
462
463 /* Struct to define some device statistics */
464 struct ce_stats {
465         /* MIB II variables
466          *
467          * NOTE: atomic_t types are only guaranteed to store 24-bits; if we
468          * MUST have 32, then we'll need another way to perform atomic
469          * operations
470          */
471         u32             unicast_pkts_rcvd;
472         atomic_t        unicast_pkts_xmtd;
473         u32             multicast_pkts_rcvd;
474         atomic_t        multicast_pkts_xmtd;
475         u32             broadcast_pkts_rcvd;
476         atomic_t        broadcast_pkts_xmtd;
477         u32             rcvd_pkts_dropped;
478
479         /* Tx Statistics. */
480         u32             tx_underflows;
481
482         u32             tx_collisions;
483         u32             tx_excessive_collisions;
484         u32             tx_first_collisions;
485         u32             tx_late_collisions;
486         u32             tx_max_pkt_errs;
487         u32             tx_deferred;
488
489         /* Rx Statistics. */
490         u32             rx_overflows;
491
492         u32             rx_length_errs;
493         u32             rx_align_errs;
494         u32             rx_crc_errs;
495         u32             rx_code_violations;
496         u32             rx_other_errs;
497
498         u32             synchronous_iterations;
499         u32             interrupt_status;
500 };
501
502 /* The private adapter structure */
503 struct et131x_adapter {
504         struct net_device *netdev;
505         struct pci_dev *pdev;
506         struct mii_bus *mii_bus;
507         struct phy_device *phydev;
508         struct work_struct task;
509
510         /* Flags that indicate current state of the adapter */
511         u32 flags;
512
513         /* local link state, to determine if a state change has occurred */
514         int link;
515
516         /* Configuration  */
517         u8 rom_addr[ETH_ALEN];
518         u8 addr[ETH_ALEN];
519         bool has_eeprom;
520         u8 eeprom_data[2];
521
522         /* Spinlocks */
523         spinlock_t lock;
524
525         spinlock_t tcb_send_qlock;
526         spinlock_t tcb_ready_qlock;
527         spinlock_t send_hw_lock;
528
529         spinlock_t rcv_lock;
530         spinlock_t rcv_pend_lock;
531         spinlock_t fbr_lock;
532
533         spinlock_t phy_lock;
534
535         /* Packet Filter and look ahead size */
536         u32 packet_filter;
537
538         /* multicast list */
539         u32 multicast_addr_count;
540         u8 multicast_list[NIC_MAX_MCAST_LIST][ETH_ALEN];
541
542         /* Pointer to the device's PCI register space */
543         struct address_map __iomem *regs;
544
545         /* Registry parameters */
546         u8 wanted_flow;         /* Flow we want for 802.3x flow control */
547         u32 registry_jumbo_packet;      /* Max supported ethernet packet size */
548
549         /* Derived from the registry: */
550         u8 flowcontrol;         /* flow control validated by the far-end */
551
552         /* Minimize init-time */
553         struct timer_list error_timer;
554
555         /* variable putting the phy into coma mode when boot up with no cable
556          * plugged in after 5 seconds
557          */
558         u8 boot_coma;
559
560         /* Next two used to save power information at power down. This
561          * information will be used during power up to set up parts of Power
562          * Management in JAGCore
563          */
564         u16 pdown_speed;
565         u8 pdown_duplex;
566
567         /* Tx Memory Variables */
568         struct tx_ring tx_ring;
569
570         /* Rx Memory Variables */
571         struct rx_ring rx_ring;
572
573         /* Stats */
574         struct ce_stats stats;
575
576         struct net_device_stats net_stats;
577 };
578
579 /* EEPROM functions */
580
581 static int eeprom_wait_ready(struct pci_dev *pdev, u32 *status)
582 {
583         u32 reg;
584         int i;
585
586         /*
587          * 1. Check LBCIF Status Register for bits 6 & 3:2 all equal to 0 and
588          *    bits 7,1:0 both equal to 1, at least once after reset.
589          *    Subsequent operations need only to check that bits 1:0 are equal
590          *    to 1 prior to starting a single byte read/write
591          */
592
593         for (i = 0; i < MAX_NUM_REGISTER_POLLS; i++) {
594                 /* Read registers grouped in DWORD1 */
595                 if (pci_read_config_dword(pdev, LBCIF_DWORD1_GROUP, &reg))
596                         return -EIO;
597
598                 /* I2C idle and Phy Queue Avail both true */
599                 if ((reg & 0x3000) == 0x3000) {
600                         if (status)
601                                 *status = reg;
602                         return reg & 0xFF;
603                 }
604         }
605         return -ETIMEDOUT;
606 }
607
608
609 /**
610  * eeprom_write - Write a byte to the ET1310's EEPROM
611  * @adapter: pointer to our private adapter structure
612  * @addr: the address to write
613  * @data: the value to write
614  *
615  * Returns 1 for a successful write.
616  */
617 static int eeprom_write(struct et131x_adapter *adapter, u32 addr, u8 data)
618 {
619         struct pci_dev *pdev = adapter->pdev;
620         int index = 0;
621         int retries;
622         int err = 0;
623         int i2c_wack = 0;
624         int writeok = 0;
625         u32 status;
626         u32 val = 0;
627
628         /*
629          * For an EEPROM, an I2C single byte write is defined as a START
630          * condition followed by the device address, EEPROM address, one byte
631          * of data and a STOP condition.  The STOP condition will trigger the
632          * EEPROM's internally timed write cycle to the nonvolatile memory.
633          * All inputs are disabled during this write cycle and the EEPROM will
634          * not respond to any access until the internal write is complete.
635          */
636
637         err = eeprom_wait_ready(pdev, NULL);
638         if (err)
639                 return err;
640
641          /*
642          * 2. Write to the LBCIF Control Register:  bit 7=1, bit 6=1, bit 3=0,
643          *    and bits 1:0 both =0.  Bit 5 should be set according to the
644          *    type of EEPROM being accessed (1=two byte addressing, 0=one
645          *    byte addressing).
646          */
647         if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
648                         LBCIF_CONTROL_LBCIF_ENABLE | LBCIF_CONTROL_I2C_WRITE))
649                 return -EIO;
650
651         i2c_wack = 1;
652
653         /* Prepare EEPROM address for Step 3 */
654
655         for (retries = 0; retries < MAX_NUM_WRITE_RETRIES; retries++) {
656                 /* Write the address to the LBCIF Address Register */
657                 if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
658                         break;
659                 /*
660                  * Write the data to the LBCIF Data Register (the I2C write
661                  * will begin).
662                  */
663                 if (pci_write_config_byte(pdev, LBCIF_DATA_REGISTER, data))
664                         break;
665                 /*
666                  * Monitor bit 1:0 of the LBCIF Status Register.  When bits
667                  * 1:0 are both equal to 1, the I2C write has completed and the
668                  * internal write cycle of the EEPROM is about to start.
669                  * (bits 1:0 = 01 is a legal state while waiting from both
670                  * equal to 1, but bits 1:0 = 10 is invalid and implies that
671                  * something is broken).
672                  */
673                 err = eeprom_wait_ready(pdev, &status);
674                 if (err < 0)
675                         return 0;
676
677                 /*
678                  * Check bit 3 of the LBCIF Status Register.  If  equal to 1,
679                  * an error has occurred.Don't break here if we are revision
680                  * 1, this is so we do a blind write for load bug.
681                  */
682                 if ((status & LBCIF_STATUS_GENERAL_ERROR)
683                         && adapter->pdev->revision == 0)
684                         break;
685
686                 /*
687                  * Check bit 2 of the LBCIF Status Register.  If equal to 1 an
688                  * ACK error has occurred on the address phase of the write.
689                  * This could be due to an actual hardware failure or the
690                  * EEPROM may still be in its internal write cycle from a
691                  * previous write. This write operation was ignored and must be
692                   *repeated later.
693                  */
694                 if (status & LBCIF_STATUS_ACK_ERROR) {
695                         /*
696                          * This could be due to an actual hardware failure
697                          * or the EEPROM may still be in its internal write
698                          * cycle from a previous write. This write operation
699                          * was ignored and must be repeated later.
700                          */
701                         udelay(10);
702                         continue;
703                 }
704
705                 writeok = 1;
706                 break;
707         }
708
709         /*
710          * Set bit 6 of the LBCIF Control Register = 0.
711          */
712         udelay(10);
713
714         while (i2c_wack) {
715                 if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
716                         LBCIF_CONTROL_LBCIF_ENABLE))
717                         writeok = 0;
718
719                 /* Do read until internal ACK_ERROR goes away meaning write
720                  * completed
721                  */
722                 do {
723                         pci_write_config_dword(pdev,
724                                                LBCIF_ADDRESS_REGISTER,
725                                                addr);
726                         do {
727                                 pci_read_config_dword(pdev,
728                                         LBCIF_DATA_REGISTER, &val);
729                         } while ((val & 0x00010000) == 0);
730                 } while (val & 0x00040000);
731
732                 if ((val & 0xFF00) != 0xC000 || index == 10000)
733                         break;
734                 index++;
735         }
736         return writeok ? 0 : -EIO;
737 }
738
739 /**
740  * eeprom_read - Read a byte from the ET1310's EEPROM
741  * @adapter: pointer to our private adapter structure
742  * @addr: the address from which to read
743  * @pdata: a pointer to a byte in which to store the value of the read
744  * @eeprom_id: the ID of the EEPROM
745  * @addrmode: how the EEPROM is to be accessed
746  *
747  * Returns 1 for a successful read
748  */
749 static int eeprom_read(struct et131x_adapter *adapter, u32 addr, u8 *pdata)
750 {
751         struct pci_dev *pdev = adapter->pdev;
752         int err;
753         u32 status;
754
755         /*
756          * A single byte read is similar to the single byte write, with the
757          * exception of the data flow:
758          */
759
760         err = eeprom_wait_ready(pdev, NULL);
761         if (err)
762                 return err;
763         /*
764          * Write to the LBCIF Control Register:  bit 7=1, bit 6=0, bit 3=0,
765          * and bits 1:0 both =0.  Bit 5 should be set according to the type
766          * of EEPROM being accessed (1=two byte addressing, 0=one byte
767          * addressing).
768          */
769         if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
770                                   LBCIF_CONTROL_LBCIF_ENABLE))
771                 return -EIO;
772         /*
773          * Write the address to the LBCIF Address Register (I2C read will
774          * begin).
775          */
776         if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
777                 return -EIO;
778         /*
779          * Monitor bit 0 of the LBCIF Status Register.  When = 1, I2C read
780          * is complete. (if bit 1 =1 and bit 0 stays = 0, a hardware failure
781          * has occurred).
782          */
783         err = eeprom_wait_ready(pdev, &status);
784         if (err < 0)
785                 return err;
786         /*
787          * Regardless of error status, read data byte from LBCIF Data
788          * Register.
789          */
790         *pdata = err;
791         /*
792          * Check bit 2 of the LBCIF Status Register.  If = 1,
793          * then an error has occurred.
794          */
795         return (status & LBCIF_STATUS_ACK_ERROR) ? -EIO : 0;
796 }
797
798 int et131x_init_eeprom(struct et131x_adapter *adapter)
799 {
800         struct pci_dev *pdev = adapter->pdev;
801         u8 eestatus;
802
803         /* We first need to check the EEPROM Status code located at offset
804          * 0xB2 of config space
805          */
806         pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS,
807                                       &eestatus);
808
809         /* THIS IS A WORKAROUND:
810          * I need to call this function twice to get my card in a
811          * LG M1 Express Dual running. I tried also a msleep before this
812          * function, because I thougth there could be some time condidions
813          * but it didn't work. Call the whole function twice also work.
814          */
815         if (pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS, &eestatus)) {
816                 dev_err(&pdev->dev,
817                        "Could not read PCI config space for EEPROM Status\n");
818                 return -EIO;
819         }
820
821         /* Determine if the error(s) we care about are present. If they are
822          * present we need to fail.
823          */
824         if (eestatus & 0x4C) {
825                 int write_failed = 0;
826                 if (pdev->revision == 0x01) {
827                         int     i;
828                         static const u8 eedata[4] = { 0xFE, 0x13, 0x10, 0xFF };
829
830                         /* Re-write the first 4 bytes if we have an eeprom
831                          * present and the revision id is 1, this fixes the
832                          * corruption seen with 1310 B Silicon
833                          */
834                         for (i = 0; i < 3; i++)
835                                 if (eeprom_write(adapter, i, eedata[i]) < 0)
836                                         write_failed = 1;
837                 }
838                 if (pdev->revision  != 0x01 || write_failed) {
839                         dev_err(&pdev->dev,
840                             "Fatal EEPROM Status Error - 0x%04x\n", eestatus);
841
842                         /* This error could mean that there was an error
843                          * reading the eeprom or that the eeprom doesn't exist.
844                          * We will treat each case the same and not try to
845                          * gather additional information that normally would
846                          * come from the eeprom, like MAC Address
847                          */
848                         adapter->has_eeprom = 0;
849                         return -EIO;
850                 }
851         }
852         adapter->has_eeprom = 1;
853
854         /* Read the EEPROM for information regarding LED behavior. Refer to
855          * ET1310_phy.c, et131x_xcvr_init(), for its use.
856          */
857         eeprom_read(adapter, 0x70, &adapter->eeprom_data[0]);
858         eeprom_read(adapter, 0x71, &adapter->eeprom_data[1]);
859
860         if (adapter->eeprom_data[0] != 0xcd)
861                 /* Disable all optional features */
862                 adapter->eeprom_data[1] = 0x00;
863
864         return 0;
865 }
866
867 /**
868  * et131x_rx_dma_enable - re-start of Rx_DMA on the ET1310.
869  * @adapter: pointer to our adapter structure
870  */
871 void et131x_rx_dma_enable(struct et131x_adapter *adapter)
872 {
873         /* Setup the receive dma configuration register for normal operation */
874         u32 csr =  0x2000;      /* FBR1 enable */
875
876         if (adapter->rx_ring.fbr[0]->buffsize == 4096)
877                 csr |= 0x0800;
878         else if (adapter->rx_ring.fbr[0]->buffsize == 8192)
879                 csr |= 0x1000;
880         else if (adapter->rx_ring.fbr[0]->buffsize == 16384)
881                 csr |= 0x1800;
882 #ifdef USE_FBR0
883         csr |= 0x0400;          /* FBR0 enable */
884         if (adapter->rx_ring.fbr[1]->buffsize == 256)
885                 csr |= 0x0100;
886         else if (adapter->rx_ring.fbr[1]->buffsize == 512)
887                 csr |= 0x0200;
888         else if (adapter->rx_ring.fbr[1]->buffsize == 1024)
889                 csr |= 0x0300;
890 #endif
891         writel(csr, &adapter->regs->rxdma.csr);
892
893         csr = readl(&adapter->regs->rxdma.csr);
894         if ((csr & 0x00020000) != 0) {
895                 udelay(5);
896                 csr = readl(&adapter->regs->rxdma.csr);
897                 if ((csr & 0x00020000) != 0) {
898                         dev_err(&adapter->pdev->dev,
899                             "RX Dma failed to exit halt state.  CSR 0x%08x\n",
900                                 csr);
901                 }
902         }
903 }
904
905 /**
906  * et131x_rx_dma_disable - Stop of Rx_DMA on the ET1310
907  * @adapter: pointer to our adapter structure
908  */
909 void et131x_rx_dma_disable(struct et131x_adapter *adapter)
910 {
911         u32 csr;
912         /* Setup the receive dma configuration register */
913         writel(0x00002001, &adapter->regs->rxdma.csr);
914         csr = readl(&adapter->regs->rxdma.csr);
915         if ((csr & 0x00020000) == 0) {  /* Check halt status (bit 17) */
916                 udelay(5);
917                 csr = readl(&adapter->regs->rxdma.csr);
918                 if ((csr & 0x00020000) == 0)
919                         dev_err(&adapter->pdev->dev,
920                         "RX Dma failed to enter halt state. CSR 0x%08x\n",
921                                 csr);
922         }
923 }
924
925 /**
926  * et131x_tx_dma_enable - re-start of Tx_DMA on the ET1310.
927  * @adapter: pointer to our adapter structure
928  *
929  * Mainly used after a return to the D0 (full-power) state from a lower state.
930  */
931 void et131x_tx_dma_enable(struct et131x_adapter *adapter)
932 {
933         /* Setup the transmit dma configuration register for normal
934          * operation
935          */
936         writel(ET_TXDMA_SNGL_EPKT|(PARM_DMA_CACHE_DEF << ET_TXDMA_CACHE_SHIFT),
937                                         &adapter->regs->txdma.csr);
938 }
939
940 static inline void add_10bit(u32 *v, int n)
941 {
942         *v = INDEX10(*v + n) | (*v & ET_DMA10_WRAP);
943 }
944
945 static inline void add_12bit(u32 *v, int n)
946 {
947         *v = INDEX12(*v + n) | (*v & ET_DMA12_WRAP);
948 }
949
950 /**
951  * nic_rx_pkts - Checks the hardware for available packets
952  * @adapter: pointer to our adapter
953  *
954  * Returns rfd, a pointer to our MPRFD.
955  *
956  * Checks the hardware for available packets, using completion ring
957  * If packets are available, it gets an RFD from the recv_list, attaches
958  * the packet to it, puts the RFD in the RecvPendList, and also returns
959  * the pointer to the RFD.
960  */
961 /* MAC functions */
962
963 /**
964  * et1310_config_mac_regs1 - Initialize the first part of MAC regs
965  * @adapter: pointer to our adapter structure
966  */
967 void et1310_config_mac_regs1(struct et131x_adapter *adapter)
968 {
969         struct mac_regs __iomem *macregs = &adapter->regs->mac;
970         u32 station1;
971         u32 station2;
972         u32 ipg;
973
974         /* First we need to reset everything.  Write to MAC configuration
975          * register 1 to perform reset.
976          */
977         writel(0xC00F0000, &macregs->cfg1);
978
979         /* Next lets configure the MAC Inter-packet gap register */
980         ipg = 0x38005860;               /* IPG1 0x38 IPG2 0x58 B2B 0x60 */
981         ipg |= 0x50 << 8;               /* ifg enforce 0x50 */
982         writel(ipg, &macregs->ipg);
983
984         /* Next lets configure the MAC Half Duplex register */
985         /* BEB trunc 0xA, Ex Defer, Rexmit 0xF Coll 0x37 */
986         writel(0x00A1F037, &macregs->hfdp);
987
988         /* Next lets configure the MAC Interface Control register */
989         writel(0, &macregs->if_ctrl);
990
991         /* Let's move on to setting up the mii management configuration */
992         writel(0x07, &macregs->mii_mgmt_cfg);   /* Clock reset 0x7 */
993
994         /* Next lets configure the MAC Station Address register.  These
995          * values are read from the EEPROM during initialization and stored
996          * in the adapter structure.  We write what is stored in the adapter
997          * structure to the MAC Station Address registers high and low.  This
998          * station address is used for generating and checking pause control
999          * packets.
1000          */
1001         station2 = (adapter->addr[1] << ET_MAC_STATION_ADDR2_OC2_SHIFT) |
1002                    (adapter->addr[0] << ET_MAC_STATION_ADDR2_OC1_SHIFT);
1003         station1 = (adapter->addr[5] << ET_MAC_STATION_ADDR1_OC6_SHIFT) |
1004                    (adapter->addr[4] << ET_MAC_STATION_ADDR1_OC5_SHIFT) |
1005                    (adapter->addr[3] << ET_MAC_STATION_ADDR1_OC4_SHIFT) |
1006                     adapter->addr[2];
1007         writel(station1, &macregs->station_addr_1);
1008         writel(station2, &macregs->station_addr_2);
1009
1010         /* Max ethernet packet in bytes that will passed by the mac without
1011          * being truncated.  Allow the MAC to pass 4 more than our max packet
1012          * size.  This is 4 for the Ethernet CRC.
1013          *
1014          * Packets larger than (registry_jumbo_packet) that do not contain a
1015          * VLAN ID will be dropped by the Rx function.
1016          */
1017         writel(adapter->registry_jumbo_packet + 4, &macregs->max_fm_len);
1018
1019         /* clear out MAC config reset */
1020         writel(0, &macregs->cfg1);
1021 }
1022
1023 /**
1024  * et1310_config_mac_regs2 - Initialize the second part of MAC regs
1025  * @adapter: pointer to our adapter structure
1026  */
1027 void et1310_config_mac_regs2(struct et131x_adapter *adapter)
1028 {
1029         int32_t delay = 0;
1030         struct mac_regs __iomem *mac = &adapter->regs->mac;
1031         struct phy_device *phydev = adapter->phydev;
1032         u32 cfg1;
1033         u32 cfg2;
1034         u32 ifctrl;
1035         u32 ctl;
1036
1037         ctl = readl(&adapter->regs->txmac.ctl);
1038         cfg1 = readl(&mac->cfg1);
1039         cfg2 = readl(&mac->cfg2);
1040         ifctrl = readl(&mac->if_ctrl);
1041
1042         /* Set up the if mode bits */
1043         cfg2 &= ~0x300;
1044         if (phydev && phydev->speed == SPEED_1000) {
1045                 cfg2 |= 0x200;
1046                 /* Phy mode bit */
1047                 ifctrl &= ~(1 << 24);
1048         } else {
1049                 cfg2 |= 0x100;
1050                 ifctrl |= (1 << 24);
1051         }
1052
1053         /* We need to enable Rx/Tx */
1054         cfg1 |= CFG1_RX_ENABLE | CFG1_TX_ENABLE | CFG1_TX_FLOW;
1055         /* Initialize loop back to off */
1056         cfg1 &= ~(CFG1_LOOPBACK | CFG1_RX_FLOW);
1057         if (adapter->flowcontrol == FLOW_RXONLY ||
1058                                 adapter->flowcontrol == FLOW_BOTH)
1059                 cfg1 |= CFG1_RX_FLOW;
1060         writel(cfg1, &mac->cfg1);
1061
1062         /* Now we need to initialize the MAC Configuration 2 register */
1063         /* preamble 7, check length, huge frame off, pad crc, crc enable
1064            full duplex off */
1065         cfg2 |= 0x7016;
1066         cfg2 &= ~0x0021;
1067
1068         /* Turn on duplex if needed */
1069         if (phydev && phydev->duplex == DUPLEX_FULL)
1070                 cfg2 |= 0x01;
1071
1072         ifctrl &= ~(1 << 26);
1073         if (phydev && phydev->duplex == DUPLEX_HALF)
1074                 ifctrl |= (1<<26);      /* Enable ghd */
1075
1076         writel(ifctrl, &mac->if_ctrl);
1077         writel(cfg2, &mac->cfg2);
1078
1079         do {
1080                 udelay(10);
1081                 delay++;
1082                 cfg1 = readl(&mac->cfg1);
1083         } while ((cfg1 & CFG1_WAIT) != CFG1_WAIT && delay < 100);
1084
1085         if (delay == 100) {
1086                 dev_warn(&adapter->pdev->dev,
1087                     "Syncd bits did not respond correctly cfg1 word 0x%08x\n",
1088                         cfg1);
1089         }
1090
1091         /* Enable txmac */
1092         ctl |= 0x09;    /* TX mac enable, FC disable */
1093         writel(ctl, &adapter->regs->txmac.ctl);
1094
1095         /* Ready to start the RXDMA/TXDMA engine */
1096         if (adapter->flags & fMP_ADAPTER_LOWER_POWER) {
1097                 et131x_rx_dma_enable(adapter);
1098                 et131x_tx_dma_enable(adapter);
1099         }
1100 }
1101
1102 /**
1103  * et1310_in_phy_coma - check if the device is in phy coma
1104  * @adapter: pointer to our adapter structure
1105  *
1106  * Returns 0 if the device is not in phy coma, 1 if it is in phy coma
1107  */
1108 int et1310_in_phy_coma(struct et131x_adapter *adapter)
1109 {
1110         u32 pmcsr;
1111
1112         pmcsr = readl(&adapter->regs->global.pm_csr);
1113
1114         return ET_PM_PHY_SW_COMA & pmcsr ? 1 : 0;
1115 }
1116
1117 void et1310_setup_device_for_multicast(struct et131x_adapter *adapter)
1118 {
1119         struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
1120         uint32_t nIndex;
1121         uint32_t result;
1122         uint32_t hash1 = 0;
1123         uint32_t hash2 = 0;
1124         uint32_t hash3 = 0;
1125         uint32_t hash4 = 0;
1126         u32 pm_csr;
1127
1128         /* If ET131X_PACKET_TYPE_MULTICAST is specified, then we provision
1129          * the multi-cast LIST.  If it is NOT specified, (and "ALL" is not
1130          * specified) then we should pass NO multi-cast addresses to the
1131          * driver.
1132          */
1133         if (adapter->packet_filter & ET131X_PACKET_TYPE_MULTICAST) {
1134                 /* Loop through our multicast array and set up the device */
1135                 for (nIndex = 0; nIndex < adapter->multicast_addr_count;
1136                      nIndex++) {
1137                         result = ether_crc(6, adapter->multicast_list[nIndex]);
1138
1139                         result = (result & 0x3F800000) >> 23;
1140
1141                         if (result < 32) {
1142                                 hash1 |= (1 << result);
1143                         } else if ((31 < result) && (result < 64)) {
1144                                 result -= 32;
1145                                 hash2 |= (1 << result);
1146                         } else if ((63 < result) && (result < 96)) {
1147                                 result -= 64;
1148                                 hash3 |= (1 << result);
1149                         } else {
1150                                 result -= 96;
1151                                 hash4 |= (1 << result);
1152                         }
1153                 }
1154         }
1155
1156         /* Write out the new hash to the device */
1157         pm_csr = readl(&adapter->regs->global.pm_csr);
1158         if (!et1310_in_phy_coma(adapter)) {
1159                 writel(hash1, &rxmac->multi_hash1);
1160                 writel(hash2, &rxmac->multi_hash2);
1161                 writel(hash3, &rxmac->multi_hash3);
1162                 writel(hash4, &rxmac->multi_hash4);
1163         }
1164 }
1165
1166 void et1310_setup_device_for_unicast(struct et131x_adapter *adapter)
1167 {
1168         struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
1169         u32 uni_pf1;
1170         u32 uni_pf2;
1171         u32 uni_pf3;
1172         u32 pm_csr;
1173
1174         /* Set up unicast packet filter reg 3 to be the first two octets of
1175          * the MAC address for both address
1176          *
1177          * Set up unicast packet filter reg 2 to be the octets 2 - 5 of the
1178          * MAC address for second address
1179          *
1180          * Set up unicast packet filter reg 3 to be the octets 2 - 5 of the
1181          * MAC address for first address
1182          */
1183         uni_pf3 = (adapter->addr[0] << ET_UNI_PF_ADDR2_1_SHIFT) |
1184                   (adapter->addr[1] << ET_UNI_PF_ADDR2_2_SHIFT) |
1185                   (adapter->addr[0] << ET_UNI_PF_ADDR1_1_SHIFT) |
1186                    adapter->addr[1];
1187
1188         uni_pf2 = (adapter->addr[2] << ET_UNI_PF_ADDR2_3_SHIFT) |
1189                   (adapter->addr[3] << ET_UNI_PF_ADDR2_4_SHIFT) |
1190                   (adapter->addr[4] << ET_UNI_PF_ADDR2_5_SHIFT) |
1191                    adapter->addr[5];
1192
1193         uni_pf1 = (adapter->addr[2] << ET_UNI_PF_ADDR1_3_SHIFT) |
1194                   (adapter->addr[3] << ET_UNI_PF_ADDR1_4_SHIFT) |
1195                   (adapter->addr[4] << ET_UNI_PF_ADDR1_5_SHIFT) |
1196                    adapter->addr[5];
1197
1198         pm_csr = readl(&adapter->regs->global.pm_csr);
1199         if (!et1310_in_phy_coma(adapter)) {
1200                 writel(uni_pf1, &rxmac->uni_pf_addr1);
1201                 writel(uni_pf2, &rxmac->uni_pf_addr2);
1202                 writel(uni_pf3, &rxmac->uni_pf_addr3);
1203         }
1204 }
1205
1206 void et1310_config_rxmac_regs(struct et131x_adapter *adapter)
1207 {
1208         struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
1209         struct phy_device *phydev = adapter->phydev;
1210         u32 sa_lo;
1211         u32 sa_hi = 0;
1212         u32 pf_ctrl = 0;
1213
1214         /* Disable the MAC while it is being configured (also disable WOL) */
1215         writel(0x8, &rxmac->ctrl);
1216
1217         /* Initialize WOL to disabled. */
1218         writel(0, &rxmac->crc0);
1219         writel(0, &rxmac->crc12);
1220         writel(0, &rxmac->crc34);
1221
1222         /* We need to set the WOL mask0 - mask4 next.  We initialize it to
1223          * its default Values of 0x00000000 because there are not WOL masks
1224          * as of this time.
1225          */
1226         writel(0, &rxmac->mask0_word0);
1227         writel(0, &rxmac->mask0_word1);
1228         writel(0, &rxmac->mask0_word2);
1229         writel(0, &rxmac->mask0_word3);
1230
1231         writel(0, &rxmac->mask1_word0);
1232         writel(0, &rxmac->mask1_word1);
1233         writel(0, &rxmac->mask1_word2);
1234         writel(0, &rxmac->mask1_word3);
1235
1236         writel(0, &rxmac->mask2_word0);
1237         writel(0, &rxmac->mask2_word1);
1238         writel(0, &rxmac->mask2_word2);
1239         writel(0, &rxmac->mask2_word3);
1240
1241         writel(0, &rxmac->mask3_word0);
1242         writel(0, &rxmac->mask3_word1);
1243         writel(0, &rxmac->mask3_word2);
1244         writel(0, &rxmac->mask3_word3);
1245
1246         writel(0, &rxmac->mask4_word0);
1247         writel(0, &rxmac->mask4_word1);
1248         writel(0, &rxmac->mask4_word2);
1249         writel(0, &rxmac->mask4_word3);
1250
1251         /* Lets setup the WOL Source Address */
1252         sa_lo = (adapter->addr[2] << ET_WOL_LO_SA3_SHIFT) |
1253                 (adapter->addr[3] << ET_WOL_LO_SA4_SHIFT) |
1254                 (adapter->addr[4] << ET_WOL_LO_SA5_SHIFT) |
1255                  adapter->addr[5];
1256         writel(sa_lo, &rxmac->sa_lo);
1257
1258         sa_hi = (u32) (adapter->addr[0] << ET_WOL_HI_SA1_SHIFT) |
1259                        adapter->addr[1];
1260         writel(sa_hi, &rxmac->sa_hi);
1261
1262         /* Disable all Packet Filtering */
1263         writel(0, &rxmac->pf_ctrl);
1264
1265         /* Let's initialize the Unicast Packet filtering address */
1266         if (adapter->packet_filter & ET131X_PACKET_TYPE_DIRECTED) {
1267                 et1310_setup_device_for_unicast(adapter);
1268                 pf_ctrl |= 4;   /* Unicast filter */
1269         } else {
1270                 writel(0, &rxmac->uni_pf_addr1);
1271                 writel(0, &rxmac->uni_pf_addr2);
1272                 writel(0, &rxmac->uni_pf_addr3);
1273         }
1274
1275         /* Let's initialize the Multicast hash */
1276         if (!(adapter->packet_filter & ET131X_PACKET_TYPE_ALL_MULTICAST)) {
1277                 pf_ctrl |= 2;   /* Multicast filter */
1278                 et1310_setup_device_for_multicast(adapter);
1279         }
1280
1281         /* Runt packet filtering.  Didn't work in version A silicon. */
1282         pf_ctrl |= (NIC_MIN_PACKET_SIZE + 4) << 16;
1283         pf_ctrl |= 8;   /* Fragment filter */
1284
1285         if (adapter->registry_jumbo_packet > 8192)
1286                 /* In order to transmit jumbo packets greater than 8k, the
1287                  * FIFO between RxMAC and RxDMA needs to be reduced in size
1288                  * to (16k - Jumbo packet size).  In order to implement this,
1289                  * we must use "cut through" mode in the RxMAC, which chops
1290                  * packets down into segments which are (max_size * 16).  In
1291                  * this case we selected 256 bytes, since this is the size of
1292                  * the PCI-Express TLP's that the 1310 uses.
1293                  *
1294                  * seg_en on, fc_en off, size 0x10
1295                  */
1296                 writel(0x41, &rxmac->mcif_ctrl_max_seg);
1297         else
1298                 writel(0, &rxmac->mcif_ctrl_max_seg);
1299
1300         /* Initialize the MCIF water marks */
1301         writel(0, &rxmac->mcif_water_mark);
1302
1303         /*  Initialize the MIF control */
1304         writel(0, &rxmac->mif_ctrl);
1305
1306         /* Initialize the Space Available Register */
1307         writel(0, &rxmac->space_avail);
1308
1309         /* Initialize the the mif_ctrl register
1310          * bit 3:  Receive code error. One or more nibbles were signaled as
1311          *         errors  during the reception of the packet.  Clear this
1312          *         bit in Gigabit, set it in 100Mbit.  This was derived
1313          *         experimentally at UNH.
1314          * bit 4:  Receive CRC error. The packet's CRC did not match the
1315          *         internally generated CRC.
1316          * bit 5:  Receive length check error. Indicates that frame length
1317          *         field value in the packet does not match the actual data
1318          *         byte length and is not a type field.
1319          * bit 16: Receive frame truncated.
1320          * bit 17: Drop packet enable
1321          */
1322         if (phydev && phydev->speed == SPEED_100)
1323                 writel(0x30038, &rxmac->mif_ctrl);
1324         else
1325                 writel(0x30030, &rxmac->mif_ctrl);
1326
1327         /* Finally we initialize RxMac to be enabled & WOL disabled.  Packet
1328          * filter is always enabled since it is where the runt packets are
1329          * supposed to be dropped.  For version A silicon, runt packet
1330          * dropping doesn't work, so it is disabled in the pf_ctrl register,
1331          * but we still leave the packet filter on.
1332          */
1333         writel(pf_ctrl, &rxmac->pf_ctrl);
1334         writel(0x9, &rxmac->ctrl);
1335 }
1336
1337 void et1310_config_txmac_regs(struct et131x_adapter *adapter)
1338 {
1339         struct txmac_regs __iomem *txmac = &adapter->regs->txmac;
1340
1341         /* We need to update the Control Frame Parameters
1342          * cfpt - control frame pause timer set to 64 (0x40)
1343          * cfep - control frame extended pause timer set to 0x0
1344          */
1345         if (adapter->flowcontrol == FLOW_NONE)
1346                 writel(0, &txmac->cf_param);
1347         else
1348                 writel(0x40, &txmac->cf_param);
1349 }
1350
1351 void et1310_config_macstat_regs(struct et131x_adapter *adapter)
1352 {
1353         struct macstat_regs __iomem *macstat =
1354                 &adapter->regs->macstat;
1355
1356         /* Next we need to initialize all the macstat registers to zero on
1357          * the device.
1358          */
1359         writel(0, &macstat->txrx_0_64_byte_frames);
1360         writel(0, &macstat->txrx_65_127_byte_frames);
1361         writel(0, &macstat->txrx_128_255_byte_frames);
1362         writel(0, &macstat->txrx_256_511_byte_frames);
1363         writel(0, &macstat->txrx_512_1023_byte_frames);
1364         writel(0, &macstat->txrx_1024_1518_byte_frames);
1365         writel(0, &macstat->txrx_1519_1522_gvln_frames);
1366
1367         writel(0, &macstat->rx_bytes);
1368         writel(0, &macstat->rx_packets);
1369         writel(0, &macstat->rx_fcs_errs);
1370         writel(0, &macstat->rx_multicast_packets);
1371         writel(0, &macstat->rx_broadcast_packets);
1372         writel(0, &macstat->rx_control_frames);
1373         writel(0, &macstat->rx_pause_frames);
1374         writel(0, &macstat->rx_unknown_opcodes);
1375         writel(0, &macstat->rx_align_errs);
1376         writel(0, &macstat->rx_frame_len_errs);
1377         writel(0, &macstat->rx_code_errs);
1378         writel(0, &macstat->rx_carrier_sense_errs);
1379         writel(0, &macstat->rx_undersize_packets);
1380         writel(0, &macstat->rx_oversize_packets);
1381         writel(0, &macstat->rx_fragment_packets);
1382         writel(0, &macstat->rx_jabbers);
1383         writel(0, &macstat->rx_drops);
1384
1385         writel(0, &macstat->tx_bytes);
1386         writel(0, &macstat->tx_packets);
1387         writel(0, &macstat->tx_multicast_packets);
1388         writel(0, &macstat->tx_broadcast_packets);
1389         writel(0, &macstat->tx_pause_frames);
1390         writel(0, &macstat->tx_deferred);
1391         writel(0, &macstat->tx_excessive_deferred);
1392         writel(0, &macstat->tx_single_collisions);
1393         writel(0, &macstat->tx_multiple_collisions);
1394         writel(0, &macstat->tx_late_collisions);
1395         writel(0, &macstat->tx_excessive_collisions);
1396         writel(0, &macstat->tx_total_collisions);
1397         writel(0, &macstat->tx_pause_honored_frames);
1398         writel(0, &macstat->tx_drops);
1399         writel(0, &macstat->tx_jabbers);
1400         writel(0, &macstat->tx_fcs_errs);
1401         writel(0, &macstat->tx_control_frames);
1402         writel(0, &macstat->tx_oversize_frames);
1403         writel(0, &macstat->tx_undersize_frames);
1404         writel(0, &macstat->tx_fragments);
1405         writel(0, &macstat->carry_reg1);
1406         writel(0, &macstat->carry_reg2);
1407
1408         /* Unmask any counters that we want to track the overflow of.
1409          * Initially this will be all counters.  It may become clear later
1410          * that we do not need to track all counters.
1411          */
1412         writel(0xFFFFBE32, &macstat->carry_reg1_mask);
1413         writel(0xFFFE7E8B, &macstat->carry_reg2_mask);
1414 }
1415
1416 /**
1417  * et131x_phy_mii_read - Read from the PHY through the MII Interface on the MAC
1418  * @adapter: pointer to our private adapter structure
1419  * @addr: the address of the transceiver
1420  * @reg: the register to read
1421  * @value: pointer to a 16-bit value in which the value will be stored
1422  *
1423  * Returns 0 on success, errno on failure (as defined in errno.h)
1424  */
1425 int et131x_phy_mii_read(struct et131x_adapter *adapter, u8 addr,
1426               u8 reg, u16 *value)
1427 {
1428         struct mac_regs __iomem *mac = &adapter->regs->mac;
1429         int status = 0;
1430         u32 delay = 0;
1431         u32 mii_addr;
1432         u32 mii_cmd;
1433         u32 mii_indicator;
1434
1435         /* Save a local copy of the registers we are dealing with so we can
1436          * set them back
1437          */
1438         mii_addr = readl(&mac->mii_mgmt_addr);
1439         mii_cmd = readl(&mac->mii_mgmt_cmd);
1440
1441         /* Stop the current operation */
1442         writel(0, &mac->mii_mgmt_cmd);
1443
1444         /* Set up the register we need to read from on the correct PHY */
1445         writel(MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
1446
1447         writel(0x1, &mac->mii_mgmt_cmd);
1448
1449         do {
1450                 udelay(50);
1451                 delay++;
1452                 mii_indicator = readl(&mac->mii_mgmt_indicator);
1453         } while ((mii_indicator & MGMT_WAIT) && delay < 50);
1454
1455         /* If we hit the max delay, we could not read the register */
1456         if (delay == 50) {
1457                 dev_warn(&adapter->pdev->dev,
1458                             "reg 0x%08x could not be read\n", reg);
1459                 dev_warn(&adapter->pdev->dev, "status is  0x%08x\n",
1460                             mii_indicator);
1461
1462                 status = -EIO;
1463         }
1464
1465         /* If we hit here we were able to read the register and we need to
1466          * return the value to the caller */
1467         *value = readl(&mac->mii_mgmt_stat) & 0xFFFF;
1468
1469         /* Stop the read operation */
1470         writel(0, &mac->mii_mgmt_cmd);
1471
1472         /* set the registers we touched back to the state at which we entered
1473          * this function
1474          */
1475         writel(mii_addr, &mac->mii_mgmt_addr);
1476         writel(mii_cmd, &mac->mii_mgmt_cmd);
1477
1478         return status;
1479 }
1480
1481 int et131x_mii_read(struct et131x_adapter *adapter, u8 reg, u16 *value)
1482 {
1483         struct phy_device *phydev = adapter->phydev;
1484
1485         if (!phydev)
1486                 return -EIO;
1487
1488         return et131x_phy_mii_read(adapter, phydev->addr, reg, value);
1489 }
1490
1491 /**
1492  * et131x_mii_write - Write to a PHY register through the MII interface of the MAC
1493  * @adapter: pointer to our private adapter structure
1494  * @reg: the register to read
1495  * @value: 16-bit value to write
1496  *
1497  * FIXME: one caller in netdev still
1498  *
1499  * Return 0 on success, errno on failure (as defined in errno.h)
1500  */
1501 int et131x_mii_write(struct et131x_adapter *adapter, u8 reg, u16 value)
1502 {
1503         struct mac_regs __iomem *mac = &adapter->regs->mac;
1504         struct phy_device *phydev = adapter->phydev;
1505         int status = 0;
1506         u8 addr;
1507         u32 delay = 0;
1508         u32 mii_addr;
1509         u32 mii_cmd;
1510         u32 mii_indicator;
1511
1512         if (!phydev)
1513                 return -EIO;
1514
1515         addr = phydev->addr;
1516
1517         /* Save a local copy of the registers we are dealing with so we can
1518          * set them back
1519          */
1520         mii_addr = readl(&mac->mii_mgmt_addr);
1521         mii_cmd = readl(&mac->mii_mgmt_cmd);
1522
1523         /* Stop the current operation */
1524         writel(0, &mac->mii_mgmt_cmd);
1525
1526         /* Set up the register we need to write to on the correct PHY */
1527         writel(MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
1528
1529         /* Add the value to write to the registers to the mac */
1530         writel(value, &mac->mii_mgmt_ctrl);
1531
1532         do {
1533                 udelay(50);
1534                 delay++;
1535                 mii_indicator = readl(&mac->mii_mgmt_indicator);
1536         } while ((mii_indicator & MGMT_BUSY) && delay < 100);
1537
1538         /* If we hit the max delay, we could not write the register */
1539         if (delay == 100) {
1540                 u16 tmp;
1541
1542                 dev_warn(&adapter->pdev->dev,
1543                     "reg 0x%08x could not be written", reg);
1544                 dev_warn(&adapter->pdev->dev, "status is  0x%08x\n",
1545                             mii_indicator);
1546                 dev_warn(&adapter->pdev->dev, "command is  0x%08x\n",
1547                             readl(&mac->mii_mgmt_cmd));
1548
1549                 et131x_mii_read(adapter, reg, &tmp);
1550
1551                 status = -EIO;
1552         }
1553         /* Stop the write operation */
1554         writel(0, &mac->mii_mgmt_cmd);
1555
1556         /*
1557          * set the registers we touched back to the state at which we entered
1558          * this function
1559          */
1560         writel(mii_addr, &mac->mii_mgmt_addr);
1561         writel(mii_cmd, &mac->mii_mgmt_cmd);
1562
1563         return status;
1564 }
1565
1566 /* Still used from _mac for BIT_READ */
1567 void et1310_phy_access_mii_bit(struct et131x_adapter *adapter, u16 action,
1568                                u16 regnum, u16 bitnum, u8 *value)
1569 {
1570         u16 reg;
1571         u16 mask = 0x0001 << bitnum;
1572
1573         /* Read the requested register */
1574         et131x_mii_read(adapter, regnum, &reg);
1575
1576         switch (action) {
1577         case TRUEPHY_BIT_READ:
1578                 *value = (reg & mask) >> bitnum;
1579                 break;
1580
1581         case TRUEPHY_BIT_SET:
1582                 et131x_mii_write(adapter, regnum, reg | mask);
1583                 break;
1584
1585         case TRUEPHY_BIT_CLEAR:
1586                 et131x_mii_write(adapter, regnum, reg & ~mask);
1587                 break;
1588
1589         default:
1590                 break;
1591         }
1592 }
1593
1594 void et1310_config_flow_control(struct et131x_adapter *adapter)
1595 {
1596         struct phy_device *phydev = adapter->phydev;
1597
1598         if (phydev->duplex == DUPLEX_HALF) {
1599                 adapter->flowcontrol = FLOW_NONE;
1600         } else {
1601                 char remote_pause, remote_async_pause;
1602
1603                 et1310_phy_access_mii_bit(adapter,
1604                                 TRUEPHY_BIT_READ, 5, 10, &remote_pause);
1605                 et1310_phy_access_mii_bit(adapter,
1606                                 TRUEPHY_BIT_READ, 5, 11,
1607                                 &remote_async_pause);
1608
1609                 if ((remote_pause == TRUEPHY_BIT_SET) &&
1610                     (remote_async_pause == TRUEPHY_BIT_SET)) {
1611                         adapter->flowcontrol = adapter->wanted_flow;
1612                 } else if ((remote_pause == TRUEPHY_BIT_SET) &&
1613                            (remote_async_pause == TRUEPHY_BIT_CLEAR)) {
1614                         if (adapter->wanted_flow == FLOW_BOTH)
1615                                 adapter->flowcontrol = FLOW_BOTH;
1616                         else
1617                                 adapter->flowcontrol = FLOW_NONE;
1618                 } else if ((remote_pause == TRUEPHY_BIT_CLEAR) &&
1619                            (remote_async_pause == TRUEPHY_BIT_CLEAR)) {
1620                         adapter->flowcontrol = FLOW_NONE;
1621                 } else {/* if (remote_pause == TRUEPHY_CLEAR_BIT &&
1622                                remote_async_pause == TRUEPHY_SET_BIT) */
1623                         if (adapter->wanted_flow == FLOW_BOTH)
1624                                 adapter->flowcontrol = FLOW_RXONLY;
1625                         else
1626                                 adapter->flowcontrol = FLOW_NONE;
1627                 }
1628         }
1629 }
1630
1631 /**
1632  * et1310_update_macstat_host_counters - Update the local copy of the statistics
1633  * @adapter: pointer to the adapter structure
1634  */
1635 void et1310_update_macstat_host_counters(struct et131x_adapter *adapter)
1636 {
1637         struct ce_stats *stats = &adapter->stats;
1638         struct macstat_regs __iomem *macstat =
1639                 &adapter->regs->macstat;
1640
1641         stats->tx_collisions           += readl(&macstat->tx_total_collisions);
1642         stats->tx_first_collisions     += readl(&macstat->tx_single_collisions);
1643         stats->tx_deferred             += readl(&macstat->tx_deferred);
1644         stats->tx_excessive_collisions +=
1645                                 readl(&macstat->tx_multiple_collisions);
1646         stats->tx_late_collisions      += readl(&macstat->tx_late_collisions);
1647         stats->tx_underflows           += readl(&macstat->tx_undersize_frames);
1648         stats->tx_max_pkt_errs         += readl(&macstat->tx_oversize_frames);
1649
1650         stats->rx_align_errs        += readl(&macstat->rx_align_errs);
1651         stats->rx_crc_errs          += readl(&macstat->rx_code_errs);
1652         stats->rcvd_pkts_dropped    += readl(&macstat->rx_drops);
1653         stats->rx_overflows         += readl(&macstat->rx_oversize_packets);
1654         stats->rx_code_violations   += readl(&macstat->rx_fcs_errs);
1655         stats->rx_length_errs       += readl(&macstat->rx_frame_len_errs);
1656         stats->rx_other_errs        += readl(&macstat->rx_fragment_packets);
1657 }
1658
1659 /**
1660  * et1310_handle_macstat_interrupt
1661  * @adapter: pointer to the adapter structure
1662  *
1663  * One of the MACSTAT counters has wrapped.  Update the local copy of
1664  * the statistics held in the adapter structure, checking the "wrap"
1665  * bit for each counter.
1666  */
1667 void et1310_handle_macstat_interrupt(struct et131x_adapter *adapter)
1668 {
1669         u32 carry_reg1;
1670         u32 carry_reg2;
1671
1672         /* Read the interrupt bits from the register(s).  These are Clear On
1673          * Write.
1674          */
1675         carry_reg1 = readl(&adapter->regs->macstat.carry_reg1);
1676         carry_reg2 = readl(&adapter->regs->macstat.carry_reg2);
1677
1678         writel(carry_reg1, &adapter->regs->macstat.carry_reg1);
1679         writel(carry_reg2, &adapter->regs->macstat.carry_reg2);
1680
1681         /* We need to do update the host copy of all the MAC_STAT counters.
1682          * For each counter, check it's overflow bit.  If the overflow bit is
1683          * set, then increment the host version of the count by one complete
1684          * revolution of the counter.  This routine is called when the counter
1685          * block indicates that one of the counters has wrapped.
1686          */
1687         if (carry_reg1 & (1 << 14))
1688                 adapter->stats.rx_code_violations       += COUNTER_WRAP_16_BIT;
1689         if (carry_reg1 & (1 << 8))
1690                 adapter->stats.rx_align_errs    += COUNTER_WRAP_12_BIT;
1691         if (carry_reg1 & (1 << 7))
1692                 adapter->stats.rx_length_errs   += COUNTER_WRAP_16_BIT;
1693         if (carry_reg1 & (1 << 2))
1694                 adapter->stats.rx_other_errs    += COUNTER_WRAP_16_BIT;
1695         if (carry_reg1 & (1 << 6))
1696                 adapter->stats.rx_crc_errs      += COUNTER_WRAP_16_BIT;
1697         if (carry_reg1 & (1 << 3))
1698                 adapter->stats.rx_overflows     += COUNTER_WRAP_16_BIT;
1699         if (carry_reg1 & (1 << 0))
1700                 adapter->stats.rcvd_pkts_dropped        += COUNTER_WRAP_16_BIT;
1701         if (carry_reg2 & (1 << 16))
1702                 adapter->stats.tx_max_pkt_errs  += COUNTER_WRAP_12_BIT;
1703         if (carry_reg2 & (1 << 15))
1704                 adapter->stats.tx_underflows    += COUNTER_WRAP_12_BIT;
1705         if (carry_reg2 & (1 << 6))
1706                 adapter->stats.tx_first_collisions += COUNTER_WRAP_12_BIT;
1707         if (carry_reg2 & (1 << 8))
1708                 adapter->stats.tx_deferred      += COUNTER_WRAP_12_BIT;
1709         if (carry_reg2 & (1 << 5))
1710                 adapter->stats.tx_excessive_collisions += COUNTER_WRAP_12_BIT;
1711         if (carry_reg2 & (1 << 4))
1712                 adapter->stats.tx_late_collisions       += COUNTER_WRAP_12_BIT;
1713         if (carry_reg2 & (1 << 2))
1714                 adapter->stats.tx_collisions    += COUNTER_WRAP_12_BIT;
1715 }
1716
1717 /* PHY functions */
1718
1719 int et131x_mdio_read(struct mii_bus *bus, int phy_addr, int reg)
1720 {
1721         struct net_device *netdev = bus->priv;
1722         struct et131x_adapter *adapter = netdev_priv(netdev);
1723         u16 value;
1724         int ret;
1725
1726         ret = et131x_phy_mii_read(adapter, phy_addr, reg, &value);
1727
1728         if (ret < 0)
1729                 return ret;
1730         else
1731                 return value;
1732 }
1733
1734 int et131x_mdio_write(struct mii_bus *bus, int phy_addr, int reg, u16 value)
1735 {
1736         struct net_device *netdev = bus->priv;
1737         struct et131x_adapter *adapter = netdev_priv(netdev);
1738
1739         return et131x_mii_write(adapter, reg, value);
1740 }
1741
1742 int et131x_mdio_reset(struct mii_bus *bus)
1743 {
1744         struct net_device *netdev = bus->priv;
1745         struct et131x_adapter *adapter = netdev_priv(netdev);
1746
1747         et131x_mii_write(adapter, MII_BMCR, BMCR_RESET);
1748
1749         return 0;
1750 }
1751
1752 /**
1753  *      et1310_phy_power_down   -       PHY power control
1754  *      @adapter: device to control
1755  *      @down: true for off/false for back on
1756  *
1757  *      one hundred, ten, one thousand megs
1758  *      How would you like to have your LAN accessed
1759  *      Can't you see that this code processed
1760  *      Phy power, phy power..
1761  */
1762 void et1310_phy_power_down(struct et131x_adapter *adapter, bool down)
1763 {
1764         u16 data;
1765
1766         et131x_mii_read(adapter, MII_BMCR, &data);
1767         data &= ~BMCR_PDOWN;
1768         if (down)
1769                 data |= BMCR_PDOWN;
1770         et131x_mii_write(adapter, MII_BMCR, data);
1771 }
1772
1773 /**
1774  * et131x_xcvr_init - Init the phy if we are setting it into force mode
1775  * @adapter: pointer to our private adapter structure
1776  *
1777  */
1778 void et131x_xcvr_init(struct et131x_adapter *adapter)
1779 {
1780         u16 imr;
1781         u16 isr;
1782         u16 lcr2;
1783
1784         et131x_mii_read(adapter, PHY_INTERRUPT_STATUS, &isr);
1785         et131x_mii_read(adapter, PHY_INTERRUPT_MASK, &imr);
1786
1787         /* Set the link status interrupt only.  Bad behavior when link status
1788          * and auto neg are set, we run into a nested interrupt problem
1789          */
1790         imr |= (ET_PHY_INT_MASK_AUTONEGSTAT &
1791                 ET_PHY_INT_MASK_LINKSTAT &
1792                 ET_PHY_INT_MASK_ENABLE);
1793
1794         et131x_mii_write(adapter, PHY_INTERRUPT_MASK, imr);
1795
1796         /* Set the LED behavior such that LED 1 indicates speed (off =
1797          * 10Mbits, blink = 100Mbits, on = 1000Mbits) and LED 2 indicates
1798          * link and activity (on for link, blink off for activity).
1799          *
1800          * NOTE: Some customizations have been added here for specific
1801          * vendors; The LED behavior is now determined by vendor data in the
1802          * EEPROM. However, the above description is the default.
1803          */
1804         if ((adapter->eeprom_data[1] & 0x4) == 0) {
1805                 et131x_mii_read(adapter, PHY_LED_2, &lcr2);
1806
1807                 lcr2 &= (ET_LED2_LED_100TX & ET_LED2_LED_1000T);
1808                 lcr2 |= (LED_VAL_LINKON_ACTIVE << LED_LINK_SHIFT);
1809
1810                 if ((adapter->eeprom_data[1] & 0x8) == 0)
1811                         lcr2 |= (LED_VAL_1000BT_100BTX << LED_TXRX_SHIFT);
1812                 else
1813                         lcr2 |= (LED_VAL_LINKON << LED_TXRX_SHIFT);
1814
1815                 et131x_mii_write(adapter, PHY_LED_2, lcr2);
1816         }
1817 }
1818
1819 /**
1820  * et131x_configure_global_regs -       configure JAGCore global regs
1821  * @adapter: pointer to our adapter structure
1822  *
1823  * Used to configure the global registers on the JAGCore
1824  */
1825 void et131x_configure_global_regs(struct et131x_adapter *adapter)
1826 {
1827         struct global_regs __iomem *regs = &adapter->regs->global;
1828
1829         writel(0, &regs->rxq_start_addr);
1830         writel(INTERNAL_MEM_SIZE - 1, &regs->txq_end_addr);
1831
1832         if (adapter->registry_jumbo_packet < 2048) {
1833                 /* Tx / RxDMA and Tx/Rx MAC interfaces have a 1k word
1834                  * block of RAM that the driver can split between Tx
1835                  * and Rx as it desires.  Our default is to split it
1836                  * 50/50:
1837                  */
1838                 writel(PARM_RX_MEM_END_DEF, &regs->rxq_end_addr);
1839                 writel(PARM_RX_MEM_END_DEF + 1, &regs->txq_start_addr);
1840         } else if (adapter->registry_jumbo_packet < 8192) {
1841                 /* For jumbo packets > 2k but < 8k, split 50-50. */
1842                 writel(INTERNAL_MEM_RX_OFFSET, &regs->rxq_end_addr);
1843                 writel(INTERNAL_MEM_RX_OFFSET + 1, &regs->txq_start_addr);
1844         } else {
1845                 /* 9216 is the only packet size greater than 8k that
1846                  * is available. The Tx buffer has to be big enough
1847                  * for one whole packet on the Tx side. We'll make
1848                  * the Tx 9408, and give the rest to Rx
1849                  */
1850                 writel(0x01b3, &regs->rxq_end_addr);
1851                 writel(0x01b4, &regs->txq_start_addr);
1852         }
1853
1854         /* Initialize the loopback register. Disable all loopbacks. */
1855         writel(0, &regs->loopback);
1856
1857         /* MSI Register */
1858         writel(0, &regs->msi_config);
1859
1860         /* By default, disable the watchdog timer.  It will be enabled when
1861          * a packet is queued.
1862          */
1863         writel(0, &regs->watchdog_timer);
1864 }
1865
1866 /* PM functions */
1867
1868 /**
1869  * et131x_config_rx_dma_regs - Start of Rx_DMA init sequence
1870  * @adapter: pointer to our adapter structure
1871  */
1872 void et131x_config_rx_dma_regs(struct et131x_adapter *adapter)
1873 {
1874         struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
1875         struct rx_ring *rx_local = &adapter->rx_ring;
1876         struct fbr_desc *fbr_entry;
1877         u32 entry;
1878         u32 psr_num_des;
1879         unsigned long flags;
1880
1881         /* Halt RXDMA to perform the reconfigure.  */
1882         et131x_rx_dma_disable(adapter);
1883
1884         /* Load the completion writeback physical address
1885          *
1886          * NOTE : dma_alloc_coherent(), used above to alloc DMA regions,
1887          * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
1888          * are ever returned, make sure the high part is retrieved here
1889          * before storing the adjusted address.
1890          */
1891         writel((u32) ((u64)rx_local->rx_status_bus >> 32),
1892                &rx_dma->dma_wb_base_hi);
1893         writel((u32) rx_local->rx_status_bus, &rx_dma->dma_wb_base_lo);
1894
1895         memset(rx_local->rx_status_block, 0, sizeof(struct rx_status_block));
1896
1897         /* Set the address and parameters of the packet status ring into the
1898          * 1310's registers
1899          */
1900         writel((u32) ((u64)rx_local->ps_ring_physaddr >> 32),
1901                &rx_dma->psr_base_hi);
1902         writel((u32) rx_local->ps_ring_physaddr, &rx_dma->psr_base_lo);
1903         writel(rx_local->psr_num_entries - 1, &rx_dma->psr_num_des);
1904         writel(0, &rx_dma->psr_full_offset);
1905
1906         psr_num_des = readl(&rx_dma->psr_num_des) & 0xFFF;
1907         writel((psr_num_des * LO_MARK_PERCENT_FOR_PSR) / 100,
1908                &rx_dma->psr_min_des);
1909
1910         spin_lock_irqsave(&adapter->rcv_lock, flags);
1911
1912         /* These local variables track the PSR in the adapter structure */
1913         rx_local->local_psr_full = 0;
1914
1915         /* Now's the best time to initialize FBR1 contents */
1916         fbr_entry = (struct fbr_desc *) rx_local->fbr[0]->ring_virtaddr;
1917         for (entry = 0; entry < rx_local->fbr[0]->num_entries; entry++) {
1918                 fbr_entry->addr_hi = rx_local->fbr[0]->bus_high[entry];
1919                 fbr_entry->addr_lo = rx_local->fbr[0]->bus_low[entry];
1920                 fbr_entry->word2 = entry;
1921                 fbr_entry++;
1922         }
1923
1924         /* Set the address and parameters of Free buffer ring 1 (and 0 if
1925          * required) into the 1310's registers
1926          */
1927         writel((u32) (rx_local->fbr[0]->real_physaddr >> 32),
1928                &rx_dma->fbr1_base_hi);
1929         writel((u32) rx_local->fbr[0]->real_physaddr, &rx_dma->fbr1_base_lo);
1930         writel(rx_local->fbr[0]->num_entries - 1, &rx_dma->fbr1_num_des);
1931         writel(ET_DMA10_WRAP, &rx_dma->fbr1_full_offset);
1932
1933         /* This variable tracks the free buffer ring 1 full position, so it
1934          * has to match the above.
1935          */
1936         rx_local->fbr[0]->local_full = ET_DMA10_WRAP;
1937         writel(
1938             ((rx_local->fbr[0]->num_entries * LO_MARK_PERCENT_FOR_RX) / 100) - 1,
1939             &rx_dma->fbr1_min_des);
1940
1941 #ifdef USE_FBR0
1942         /* Now's the best time to initialize FBR0 contents */
1943         fbr_entry = (struct fbr_desc *) rx_local->fbr[1]->ring_virtaddr;
1944         for (entry = 0; entry < rx_local->fbr[1]->num_entries; entry++) {
1945                 fbr_entry->addr_hi = rx_local->fbr[1]->bus_high[entry];
1946                 fbr_entry->addr_lo = rx_local->fbr[1]->bus_low[entry];
1947                 fbr_entry->word2 = entry;
1948                 fbr_entry++;
1949         }
1950
1951         writel((u32) (rx_local->fbr[1]->real_physaddr >> 32),
1952                &rx_dma->fbr0_base_hi);
1953         writel((u32) rx_local->fbr[1]->real_physaddr, &rx_dma->fbr0_base_lo);
1954         writel(rx_local->fbr[1]->num_entries - 1, &rx_dma->fbr0_num_des);
1955         writel(ET_DMA10_WRAP, &rx_dma->fbr0_full_offset);
1956
1957         /* This variable tracks the free buffer ring 0 full position, so it
1958          * has to match the above.
1959          */
1960         rx_local->fbr[1]->local_full = ET_DMA10_WRAP;
1961         writel(
1962             ((rx_local->fbr[1]->num_entries * LO_MARK_PERCENT_FOR_RX) / 100) - 1,
1963             &rx_dma->fbr0_min_des);
1964 #endif
1965
1966         /* Program the number of packets we will receive before generating an
1967          * interrupt.
1968          * For version B silicon, this value gets updated once autoneg is
1969          *complete.
1970          */
1971         writel(PARM_RX_NUM_BUFS_DEF, &rx_dma->num_pkt_done);
1972
1973         /* The "time_done" is not working correctly to coalesce interrupts
1974          * after a given time period, but rather is giving us an interrupt
1975          * regardless of whether we have received packets.
1976          * This value gets updated once autoneg is complete.
1977          */
1978         writel(PARM_RX_TIME_INT_DEF, &rx_dma->max_pkt_time);
1979
1980         spin_unlock_irqrestore(&adapter->rcv_lock, flags);
1981 }
1982
1983 /**
1984  * et131x_config_tx_dma_regs - Set up the tx dma section of the JAGCore.
1985  * @adapter: pointer to our private adapter structure
1986  *
1987  * Configure the transmit engine with the ring buffers we have created
1988  * and prepare it for use.
1989  */
1990 void et131x_config_tx_dma_regs(struct et131x_adapter *adapter)
1991 {
1992         struct txdma_regs __iomem *txdma = &adapter->regs->txdma;
1993
1994         /* Load the hardware with the start of the transmit descriptor ring. */
1995         writel((u32) ((u64)adapter->tx_ring.tx_desc_ring_pa >> 32),
1996                &txdma->pr_base_hi);
1997         writel((u32) adapter->tx_ring.tx_desc_ring_pa,
1998                &txdma->pr_base_lo);
1999
2000         /* Initialise the transmit DMA engine */
2001         writel(NUM_DESC_PER_RING_TX - 1, &txdma->pr_num_des);
2002
2003         /* Load the completion writeback physical address */
2004         writel((u32)((u64)adapter->tx_ring.tx_status_pa >> 32),
2005                                                 &txdma->dma_wb_base_hi);
2006         writel((u32)adapter->tx_ring.tx_status_pa, &txdma->dma_wb_base_lo);
2007
2008         *adapter->tx_ring.tx_status = 0;
2009
2010         writel(0, &txdma->service_request);
2011         adapter->tx_ring.send_idx = 0;
2012 }
2013
2014 /**
2015  * et131x_adapter_setup - Set the adapter up as per cassini+ documentation
2016  * @adapter: pointer to our private adapter structure
2017  *
2018  * Returns 0 on success, errno on failure (as defined in errno.h)
2019  */
2020 void et131x_adapter_setup(struct et131x_adapter *adapter)
2021 {
2022         /* Configure the JAGCore */
2023         et131x_configure_global_regs(adapter);
2024
2025         et1310_config_mac_regs1(adapter);
2026
2027         /* Configure the MMC registers */
2028         /* All we need to do is initialize the Memory Control Register */
2029         writel(ET_MMC_ENABLE, &adapter->regs->mmc.mmc_ctrl);
2030
2031         et1310_config_rxmac_regs(adapter);
2032         et1310_config_txmac_regs(adapter);
2033
2034         et131x_config_rx_dma_regs(adapter);
2035         et131x_config_tx_dma_regs(adapter);
2036
2037         et1310_config_macstat_regs(adapter);
2038
2039         et1310_phy_power_down(adapter, 0);
2040         et131x_xcvr_init(adapter);
2041 }
2042
2043 /**
2044  * et131x_soft_reset - Issue a soft reset to the hardware, complete for ET1310
2045  * @adapter: pointer to our private adapter structure
2046  */
2047 void et131x_soft_reset(struct et131x_adapter *adapter)
2048 {
2049         /* Disable MAC Core */
2050         writel(0xc00f0000, &adapter->regs->mac.cfg1);
2051
2052         /* Set everything to a reset value */
2053         writel(0x7F, &adapter->regs->global.sw_reset);
2054         writel(0x000f0000, &adapter->regs->mac.cfg1);
2055         writel(0x00000000, &adapter->regs->mac.cfg1);
2056 }
2057
2058 /**
2059  *      et131x_enable_interrupts        -       enable interrupt
2060  *      @adapter: et131x device
2061  *
2062  *      Enable the appropriate interrupts on the ET131x according to our
2063  *      configuration
2064  */
2065 void et131x_enable_interrupts(struct et131x_adapter *adapter)
2066 {
2067         u32 mask;
2068
2069         /* Enable all global interrupts */
2070         if (adapter->flowcontrol == FLOW_TXONLY ||
2071                             adapter->flowcontrol == FLOW_BOTH)
2072                 mask = INT_MASK_ENABLE;
2073         else
2074                 mask = INT_MASK_ENABLE_NO_FLOW;
2075
2076         writel(mask, &adapter->regs->global.int_mask);
2077 }
2078
2079 /**
2080  *      et131x_disable_interrupts       -       interrupt disable
2081  *      @adapter: et131x device
2082  *
2083  *      Block all interrupts from the et131x device at the device itself
2084  */
2085 void et131x_disable_interrupts(struct et131x_adapter *adapter)
2086 {
2087         /* Disable all global interrupts */
2088         writel(INT_MASK_DISABLE, &adapter->regs->global.int_mask);
2089 }
2090
2091 /**
2092  * et131x_tx_dma_disable - Stop of Tx_DMA on the ET1310
2093  * @adapter: pointer to our adapter structure
2094  */
2095 void et131x_tx_dma_disable(struct et131x_adapter *adapter)
2096 {
2097         /* Setup the tramsmit dma configuration register */
2098         writel(ET_TXDMA_CSR_HALT|ET_TXDMA_SNGL_EPKT,
2099                                         &adapter->regs->txdma.csr);
2100 }
2101
2102 /**
2103  * et131x_enable_txrx - Enable tx/rx queues
2104  * @netdev: device to be enabled
2105  */
2106 void et131x_enable_txrx(struct net_device *netdev)
2107 {
2108         struct et131x_adapter *adapter = netdev_priv(netdev);
2109
2110         /* Enable the Tx and Rx DMA engines (if not already enabled) */
2111         et131x_rx_dma_enable(adapter);
2112         et131x_tx_dma_enable(adapter);
2113
2114         /* Enable device interrupts */
2115         if (adapter->flags & fMP_ADAPTER_INTERRUPT_IN_USE)
2116                 et131x_enable_interrupts(adapter);
2117
2118         /* We're ready to move some data, so start the queue */
2119         netif_start_queue(netdev);
2120 }
2121
2122 /**
2123  * et131x_disable_txrx - Disable tx/rx queues
2124  * @netdev: device to be disabled
2125  */
2126 void et131x_disable_txrx(struct net_device *netdev)
2127 {
2128         struct et131x_adapter *adapter = netdev_priv(netdev);
2129
2130         /* First thing is to stop the queue */
2131         netif_stop_queue(netdev);
2132
2133         /* Stop the Tx and Rx DMA engines */
2134         et131x_rx_dma_disable(adapter);
2135         et131x_tx_dma_disable(adapter);
2136
2137         /* Disable device interrupts */
2138         et131x_disable_interrupts(adapter);
2139 }
2140
2141 /**
2142  * et131x_init_send - Initialize send data structures
2143  * @adapter: pointer to our private adapter structure
2144  */
2145 void et131x_init_send(struct et131x_adapter *adapter)
2146 {
2147         struct tcb *tcb;
2148         u32 ct;
2149         struct tx_ring *tx_ring;
2150
2151         /* Setup some convenience pointers */
2152         tx_ring = &adapter->tx_ring;
2153         tcb = adapter->tx_ring.tcb_ring;
2154
2155         tx_ring->tcb_qhead = tcb;
2156
2157         memset(tcb, 0, sizeof(struct tcb) * NUM_TCB);
2158
2159         /* Go through and set up each TCB */
2160         for (ct = 0; ct++ < NUM_TCB; tcb++)
2161                 /* Set the link pointer in HW TCB to the next TCB in the
2162                  * chain
2163                  */
2164                 tcb->next = tcb + 1;
2165
2166         /* Set the  tail pointer */
2167         tcb--;
2168         tx_ring->tcb_qtail = tcb;
2169         tcb->next = NULL;
2170         /* Curr send queue should now be empty */
2171         tx_ring->send_head = NULL;
2172         tx_ring->send_tail = NULL;
2173 }
2174
2175 /**
2176  * et1310_enable_phy_coma - called when network cable is unplugged
2177  * @adapter: pointer to our adapter structure
2178  *
2179  * driver receive an phy status change interrupt while in D0 and check that
2180  * phy_status is down.
2181  *
2182  *          -- gate off JAGCore;
2183  *          -- set gigE PHY in Coma mode
2184  *          -- wake on phy_interrupt; Perform software reset JAGCore,
2185  *             re-initialize jagcore and gigE PHY
2186  *
2187  *      Add D0-ASPM-PhyLinkDown Support:
2188  *          -- while in D0, when there is a phy_interrupt indicating phy link
2189  *             down status, call the MPSetPhyComa routine to enter this active
2190  *             state power saving mode
2191  *          -- while in D0-ASPM-PhyLinkDown mode, when there is a phy_interrupt
2192  *       indicating linkup status, call the MPDisablePhyComa routine to
2193  *             restore JAGCore and gigE PHY
2194  */
2195 void et1310_enable_phy_coma(struct et131x_adapter *adapter)
2196 {
2197         unsigned long flags;
2198         u32 pmcsr;
2199
2200         pmcsr = readl(&adapter->regs->global.pm_csr);
2201
2202         /* Save the GbE PHY speed and duplex modes. Need to restore this
2203          * when cable is plugged back in
2204          */
2205         /*
2206          * TODO - when PM is re-enabled, check if we need to
2207          * perform a similar task as this -
2208          * adapter->pdown_speed = adapter->ai_force_speed;
2209          * adapter->pdown_duplex = adapter->ai_force_duplex;
2210          */
2211
2212         /* Stop sending packets. */
2213         spin_lock_irqsave(&adapter->send_hw_lock, flags);
2214         adapter->flags |= fMP_ADAPTER_LOWER_POWER;
2215         spin_unlock_irqrestore(&adapter->send_hw_lock, flags);
2216
2217         /* Wait for outstanding Receive packets */
2218
2219         et131x_disable_txrx(adapter->netdev);
2220
2221         /* Gate off JAGCore 3 clock domains */
2222         pmcsr &= ~ET_PMCSR_INIT;
2223         writel(pmcsr, &adapter->regs->global.pm_csr);
2224
2225         /* Program gigE PHY in to Coma mode */
2226         pmcsr |= ET_PM_PHY_SW_COMA;
2227         writel(pmcsr, &adapter->regs->global.pm_csr);
2228 }
2229
2230 /**
2231  * et1310_disable_phy_coma - Disable the Phy Coma Mode
2232  * @adapter: pointer to our adapter structure
2233  */
2234 void et1310_disable_phy_coma(struct et131x_adapter *adapter)
2235 {
2236         u32 pmcsr;
2237
2238         pmcsr = readl(&adapter->regs->global.pm_csr);
2239
2240         /* Disable phy_sw_coma register and re-enable JAGCore clocks */
2241         pmcsr |= ET_PMCSR_INIT;
2242         pmcsr &= ~ET_PM_PHY_SW_COMA;
2243         writel(pmcsr, &adapter->regs->global.pm_csr);
2244
2245         /* Restore the GbE PHY speed and duplex modes;
2246          * Reset JAGCore; re-configure and initialize JAGCore and gigE PHY
2247          */
2248         /* TODO - when PM is re-enabled, check if we need to
2249          * perform a similar task as this -
2250          * adapter->ai_force_speed = adapter->pdown_speed;
2251          * adapter->ai_force_duplex = adapter->pdown_duplex;
2252          */
2253
2254         /* Re-initialize the send structures */
2255         et131x_init_send(adapter);
2256
2257         /* Bring the device back to the state it was during init prior to
2258          * autonegotiation being complete.  This way, when we get the auto-neg
2259          * complete interrupt, we can complete init by calling ConfigMacREGS2.
2260          */
2261         et131x_soft_reset(adapter);
2262
2263         /* setup et1310 as per the documentation ?? */
2264         et131x_adapter_setup(adapter);
2265
2266         /* Allow Tx to restart */
2267         adapter->flags &= ~fMP_ADAPTER_LOWER_POWER;
2268
2269         et131x_enable_txrx(adapter->netdev);
2270 }
2271
2272 /* RX functions */
2273
2274 static inline u32 bump_free_buff_ring(u32 *free_buff_ring, u32 limit)
2275 {
2276         u32 tmp_free_buff_ring = *free_buff_ring;
2277         tmp_free_buff_ring++;
2278         /* This works for all cases where limit < 1024. The 1023 case
2279            works because 1023++ is 1024 which means the if condition is not
2280            taken but the carry of the bit into the wrap bit toggles the wrap
2281            value correctly */
2282         if ((tmp_free_buff_ring & ET_DMA10_MASK) > limit) {
2283                 tmp_free_buff_ring &= ~ET_DMA10_MASK;
2284                 tmp_free_buff_ring ^= ET_DMA10_WRAP;
2285         }
2286         /* For the 1023 case */
2287         tmp_free_buff_ring &= (ET_DMA10_MASK|ET_DMA10_WRAP);
2288         *free_buff_ring = tmp_free_buff_ring;
2289         return tmp_free_buff_ring;
2290 }
2291
2292 /**
2293  * et131x_align_allocated_memory - Align allocated memory on a given boundary
2294  * @adapter: pointer to our adapter structure
2295  * @phys_addr: pointer to Physical address
2296  * @offset: pointer to the offset variable
2297  * @mask: correct mask
2298  */
2299 void et131x_align_allocated_memory(struct et131x_adapter *adapter,
2300                                    uint64_t *phys_addr,
2301                                    uint64_t *offset, uint64_t mask)
2302 {
2303         uint64_t new_addr;
2304
2305         *offset = 0;
2306
2307         new_addr = *phys_addr & ~mask;
2308
2309         if (new_addr != *phys_addr) {
2310                 /* Move to next aligned block */
2311                 new_addr += mask + 1;
2312                 /* Return offset for adjusting virt addr */
2313                 *offset = new_addr - *phys_addr;
2314                 /* Return new physical address */
2315                 *phys_addr = new_addr;
2316         }
2317 }
2318
2319 /**
2320  * et131x_rx_dma_memory_alloc
2321  * @adapter: pointer to our private adapter structure
2322  *
2323  * Returns 0 on success and errno on failure (as defined in errno.h)
2324  *
2325  * Allocates Free buffer ring 1 for sure, free buffer ring 0 if required,
2326  * and the Packet Status Ring.
2327  */
2328 int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
2329 {
2330         u32 i, j;
2331         u32 bufsize;
2332         u32 pktstat_ringsize, fbr_chunksize;
2333         struct rx_ring *rx_ring;
2334
2335         /* Setup some convenience pointers */
2336         rx_ring = &adapter->rx_ring;
2337
2338         /* Alloc memory for the lookup table */
2339 #ifdef USE_FBR0
2340         rx_ring->fbr[1] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
2341 #endif
2342         rx_ring->fbr[0] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
2343
2344         /* The first thing we will do is configure the sizes of the buffer
2345          * rings. These will change based on jumbo packet support.  Larger
2346          * jumbo packets increases the size of each entry in FBR0, and the
2347          * number of entries in FBR0, while at the same time decreasing the
2348          * number of entries in FBR1.
2349          *
2350          * FBR1 holds "large" frames, FBR0 holds "small" frames.  If FBR1
2351          * entries are huge in order to accommodate a "jumbo" frame, then it
2352          * will have less entries.  Conversely, FBR1 will now be relied upon
2353          * to carry more "normal" frames, thus it's entry size also increases
2354          * and the number of entries goes up too (since it now carries
2355          * "small" + "regular" packets.
2356          *
2357          * In this scheme, we try to maintain 512 entries between the two
2358          * rings. Also, FBR1 remains a constant size - when it's size doubles
2359          * the number of entries halves.  FBR0 increases in size, however.
2360          */
2361
2362         if (adapter->registry_jumbo_packet < 2048) {
2363 #ifdef USE_FBR0
2364                 rx_ring->fbr[1]->buffsize = 256;
2365                 rx_ring->fbr[1]->num_entries = 512;
2366 #endif
2367                 rx_ring->fbr[0]->buffsize = 2048;
2368                 rx_ring->fbr[0]->num_entries = 512;
2369         } else if (adapter->registry_jumbo_packet < 4096) {
2370 #ifdef USE_FBR0
2371                 rx_ring->fbr[1]->buffsize = 512;
2372                 rx_ring->fbr[1]->num_entries = 1024;
2373 #endif
2374                 rx_ring->fbr[0]->buffsize = 4096;
2375                 rx_ring->fbr[0]->num_entries = 512;
2376         } else {
2377 #ifdef USE_FBR0
2378                 rx_ring->fbr[1]->buffsize = 1024;
2379                 rx_ring->fbr[1]->num_entries = 768;
2380 #endif
2381                 rx_ring->fbr[0]->buffsize = 16384;
2382                 rx_ring->fbr[0]->num_entries = 128;
2383         }
2384
2385 #ifdef USE_FBR0
2386         adapter->rx_ring.psr_num_entries = adapter->rx_ring.fbr[1]->num_entries +
2387             adapter->rx_ring.fbr[0]->num_entries;
2388 #else
2389         adapter->rx_ring.psr_num_entries = adapter->rx_ring.fbr[0]->num_entries;
2390 #endif
2391
2392         /* Allocate an area of memory for Free Buffer Ring 1 */
2393         bufsize = (sizeof(struct fbr_desc) * rx_ring->fbr[0]->num_entries) + 0xfff;
2394         rx_ring->fbr[0]->ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
2395                                         bufsize,
2396                                         &rx_ring->fbr[0]->ring_physaddr,
2397                                         GFP_KERNEL);
2398         if (!rx_ring->fbr[0]->ring_virtaddr) {
2399                 dev_err(&adapter->pdev->dev,
2400                           "Cannot alloc memory for Free Buffer Ring 1\n");
2401                 return -ENOMEM;
2402         }
2403
2404         /* Save physical address
2405          *
2406          * NOTE: dma_alloc_coherent(), used above to alloc DMA regions,
2407          * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
2408          * are ever returned, make sure the high part is retrieved here
2409          * before storing the adjusted address.
2410          */
2411         rx_ring->fbr[0]->real_physaddr = rx_ring->fbr[0]->ring_physaddr;
2412
2413         /* Align Free Buffer Ring 1 on a 4K boundary */
2414         et131x_align_allocated_memory(adapter,
2415                                       &rx_ring->fbr[0]->real_physaddr,
2416                                       &rx_ring->fbr[0]->offset, 0x0FFF);
2417
2418         rx_ring->fbr[0]->ring_virtaddr =
2419                         (void *)((u8 *) rx_ring->fbr[0]->ring_virtaddr +
2420                         rx_ring->fbr[0]->offset);
2421
2422 #ifdef USE_FBR0
2423         /* Allocate an area of memory for Free Buffer Ring 0 */
2424         bufsize = (sizeof(struct fbr_desc) * rx_ring->fbr[1]->num_entries) + 0xfff;
2425         rx_ring->fbr[1]->ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
2426                                                 bufsize,
2427                                                 &rx_ring->fbr[1]->ring_physaddr,
2428                                                 GFP_KERNEL);
2429         if (!rx_ring->fbr[1]->ring_virtaddr) {
2430                 dev_err(&adapter->pdev->dev,
2431                           "Cannot alloc memory for Free Buffer Ring 0\n");
2432                 return -ENOMEM;
2433         }
2434
2435         /* Save physical address
2436          *
2437          * NOTE: dma_alloc_coherent(), used above to alloc DMA regions,
2438          * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
2439          * are ever returned, make sure the high part is retrieved here before
2440          * storing the adjusted address.
2441          */
2442         rx_ring->fbr[1]->real_physaddr = rx_ring->fbr[1]->ring_physaddr;
2443
2444         /* Align Free Buffer Ring 0 on a 4K boundary */
2445         et131x_align_allocated_memory(adapter,
2446                                       &rx_ring->fbr[1]->real_physaddr,
2447                                       &rx_ring->fbr[1]->offset, 0x0FFF);
2448
2449         rx_ring->fbr[1]->ring_virtaddr =
2450                         (void *)((u8 *) rx_ring->fbr[1]->ring_virtaddr +
2451                         rx_ring->fbr[1]->offset);
2452 #endif
2453         for (i = 0; i < (rx_ring->fbr[0]->num_entries / FBR_CHUNKS); i++) {
2454                 u64 fbr1_offset;
2455                 u64 fbr1_tmp_physaddr;
2456                 u32 fbr1_align;
2457
2458                 /* This code allocates an area of memory big enough for N
2459                  * free buffers + (buffer_size - 1) so that the buffers can
2460                  * be aligned on 4k boundaries.  If each buffer were aligned
2461                  * to a buffer_size boundary, the effect would be to double
2462                  * the size of FBR0.  By allocating N buffers at once, we
2463                  * reduce this overhead.
2464                  */
2465                 if (rx_ring->fbr[0]->buffsize > 4096)
2466                         fbr1_align = 4096;
2467                 else
2468                         fbr1_align = rx_ring->fbr[0]->buffsize;
2469
2470                 fbr_chunksize =
2471                     (FBR_CHUNKS * rx_ring->fbr[0]->buffsize) + fbr1_align - 1;
2472                 rx_ring->fbr[0]->mem_virtaddrs[i] =
2473                     dma_alloc_coherent(&adapter->pdev->dev, fbr_chunksize,
2474                                          &rx_ring->fbr[0]->mem_physaddrs[i], GFP_KERNEL);
2475
2476                 if (!rx_ring->fbr[0]->mem_virtaddrs[i]) {
2477                         dev_err(&adapter->pdev->dev,
2478                                 "Could not alloc memory\n");
2479                         return -ENOMEM;
2480                 }
2481
2482                 /* See NOTE in "Save Physical Address" comment above */
2483                 fbr1_tmp_physaddr = rx_ring->fbr[0]->mem_physaddrs[i];
2484
2485                 et131x_align_allocated_memory(adapter,
2486                                               &fbr1_tmp_physaddr,
2487                                               &fbr1_offset, (fbr1_align - 1));
2488
2489                 for (j = 0; j < FBR_CHUNKS; j++) {
2490                         u32 index = (i * FBR_CHUNKS) + j;
2491
2492                         /* Save the Virtual address of this index for quick
2493                          * access later
2494                          */
2495                         rx_ring->fbr[0]->virt[index] =
2496                             (u8 *) rx_ring->fbr[0]->mem_virtaddrs[i] +
2497                             (j * rx_ring->fbr[0]->buffsize) + fbr1_offset;
2498
2499                         /* now store the physical address in the descriptor
2500                          * so the device can access it
2501                          */
2502                         rx_ring->fbr[0]->bus_high[index] =
2503                             (u32) (fbr1_tmp_physaddr >> 32);
2504                         rx_ring->fbr[0]->bus_low[index] =
2505                             (u32) fbr1_tmp_physaddr;
2506
2507                         fbr1_tmp_physaddr += rx_ring->fbr[0]->buffsize;
2508
2509                         rx_ring->fbr[0]->buffer1[index] =
2510                             rx_ring->fbr[0]->virt[index];
2511                         rx_ring->fbr[0]->buffer2[index] =
2512                             rx_ring->fbr[0]->virt[index] - 4;
2513                 }
2514         }
2515
2516 #ifdef USE_FBR0
2517         /* Same for FBR0 (if in use) */
2518         for (i = 0; i < (rx_ring->fbr[1]->num_entries / FBR_CHUNKS); i++) {
2519                 u64 fbr0_offset;
2520                 u64 fbr0_tmp_physaddr;
2521
2522                 fbr_chunksize =
2523                     ((FBR_CHUNKS + 1) * rx_ring->fbr[1]->buffsize) - 1;
2524                 rx_ring->fbr[1]->mem_virtaddrs[i] =
2525                     dma_alloc_coherent(&adapter->pdev->dev, fbr_chunksize,
2526                                          &rx_ring->fbr[1]->mem_physaddrs[i], GFP_KERNEL);
2527
2528                 if (!rx_ring->fbr[1]->mem_virtaddrs[i]) {
2529                         dev_err(&adapter->pdev->dev,
2530                                 "Could not alloc memory\n");
2531                         return -ENOMEM;
2532                 }
2533
2534                 /* See NOTE in "Save Physical Address" comment above */
2535                 fbr0_tmp_physaddr = rx_ring->fbr[1]->mem_physaddrs[i];
2536
2537                 et131x_align_allocated_memory(adapter,
2538                                               &fbr0_tmp_physaddr,
2539                                               &fbr0_offset,
2540                                               rx_ring->fbr[1]->buffsize - 1);
2541
2542                 for (j = 0; j < FBR_CHUNKS; j++) {
2543                         u32 index = (i * FBR_CHUNKS) + j;
2544
2545                         rx_ring->fbr[1]->virt[index] =
2546                             (u8 *) rx_ring->fbr[1]->mem_virtaddrs[i] +
2547                             (j * rx_ring->fbr[1]->buffsize) + fbr0_offset;
2548
2549                         rx_ring->fbr[1]->bus_high[index] =
2550                             (u32) (fbr0_tmp_physaddr >> 32);
2551                         rx_ring->fbr[1]->bus_low[index] =
2552                             (u32) fbr0_tmp_physaddr;
2553
2554                         fbr0_tmp_physaddr += rx_ring->fbr[1]->buffsize;
2555
2556                         rx_ring->fbr[1]->buffer1[index] =
2557                             rx_ring->fbr[1]->virt[index];
2558                         rx_ring->fbr[1]->buffer2[index] =
2559                             rx_ring->fbr[1]->virt[index] - 4;
2560                 }
2561         }
2562 #endif
2563
2564         /* Allocate an area of memory for FIFO of Packet Status ring entries */
2565         pktstat_ringsize =
2566             sizeof(struct pkt_stat_desc) * adapter->rx_ring.psr_num_entries;
2567
2568         rx_ring->ps_ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
2569                                                   pktstat_ringsize,
2570                                                   &rx_ring->ps_ring_physaddr,
2571                                                   GFP_KERNEL);
2572
2573         if (!rx_ring->ps_ring_virtaddr) {
2574                 dev_err(&adapter->pdev->dev,
2575                           "Cannot alloc memory for Packet Status Ring\n");
2576                 return -ENOMEM;
2577         }
2578         printk(KERN_INFO "Packet Status Ring %lx\n",
2579             (unsigned long) rx_ring->ps_ring_physaddr);
2580
2581         /*
2582          * NOTE : dma_alloc_coherent(), used above to alloc DMA regions,
2583          * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
2584          * are ever returned, make sure the high part is retrieved here before
2585          * storing the adjusted address.
2586          */
2587
2588         /* Allocate an area of memory for writeback of status information */
2589         rx_ring->rx_status_block = dma_alloc_coherent(&adapter->pdev->dev,
2590                                             sizeof(struct rx_status_block),
2591                                             &rx_ring->rx_status_bus,
2592                                             GFP_KERNEL);
2593         if (!rx_ring->rx_status_block) {
2594                 dev_err(&adapter->pdev->dev,
2595                           "Cannot alloc memory for Status Block\n");
2596                 return -ENOMEM;
2597         }
2598         rx_ring->num_rfd = NIC_DEFAULT_NUM_RFD;
2599         printk(KERN_INFO "PRS %lx\n", (unsigned long)rx_ring->rx_status_bus);
2600
2601         /* Recv
2602          * kmem_cache_create initializes a lookaside list. After successful
2603          * creation, nonpaged fixed-size blocks can be allocated from and
2604          * freed to the lookaside list.
2605          * RFDs will be allocated from this pool.
2606          */
2607         rx_ring->recv_lookaside = kmem_cache_create(adapter->netdev->name,
2608                                                    sizeof(struct rfd),
2609                                                    0,
2610                                                    SLAB_CACHE_DMA |
2611                                                    SLAB_HWCACHE_ALIGN,
2612                                                    NULL);
2613
2614         adapter->flags |= fMP_ADAPTER_RECV_LOOKASIDE;
2615
2616         /* The RFDs are going to be put on lists later on, so initialize the
2617          * lists now.
2618          */
2619         INIT_LIST_HEAD(&rx_ring->recv_list);
2620         return 0;
2621 }
2622
2623 /**
2624  * et131x_rx_dma_memory_free - Free all memory allocated within this module.
2625  * @adapter: pointer to our private adapter structure
2626  */
2627 void et131x_rx_dma_memory_free(struct et131x_adapter *adapter)
2628 {
2629         u32 index;
2630         u32 bufsize;
2631         u32 pktstat_ringsize;
2632         struct rfd *rfd;
2633         struct rx_ring *rx_ring;
2634
2635         /* Setup some convenience pointers */
2636         rx_ring = &adapter->rx_ring;
2637
2638         /* Free RFDs and associated packet descriptors */
2639         WARN_ON(rx_ring->num_ready_recv != rx_ring->num_rfd);
2640
2641         while (!list_empty(&rx_ring->recv_list)) {
2642                 rfd = (struct rfd *) list_entry(rx_ring->recv_list.next,
2643                                 struct rfd, list_node);
2644
2645                 list_del(&rfd->list_node);
2646                 rfd->skb = NULL;
2647                 kmem_cache_free(adapter->rx_ring.recv_lookaside, rfd);
2648         }
2649
2650         /* Free Free Buffer Ring 1 */
2651         if (rx_ring->fbr[0]->ring_virtaddr) {
2652                 /* First the packet memory */
2653                 for (index = 0; index <
2654                      (rx_ring->fbr[0]->num_entries / FBR_CHUNKS); index++) {
2655                         if (rx_ring->fbr[0]->mem_virtaddrs[index]) {
2656                                 u32 fbr1_align;
2657
2658                                 if (rx_ring->fbr[0]->buffsize > 4096)
2659                                         fbr1_align = 4096;
2660                                 else
2661                                         fbr1_align = rx_ring->fbr[0]->buffsize;
2662
2663                                 bufsize =
2664                                     (rx_ring->fbr[0]->buffsize * FBR_CHUNKS) +
2665                                     fbr1_align - 1;
2666
2667                                 dma_free_coherent(&adapter->pdev->dev,
2668                                         bufsize,
2669                                         rx_ring->fbr[0]->mem_virtaddrs[index],
2670                                         rx_ring->fbr[0]->mem_physaddrs[index]);
2671
2672                                 rx_ring->fbr[0]->mem_virtaddrs[index] = NULL;
2673                         }
2674                 }
2675
2676                 /* Now the FIFO itself */
2677                 rx_ring->fbr[0]->ring_virtaddr = (void *)((u8 *)
2678                         rx_ring->fbr[0]->ring_virtaddr - rx_ring->fbr[0]->offset);
2679
2680                 bufsize = (sizeof(struct fbr_desc) * rx_ring->fbr[0]->num_entries)
2681                                                             + 0xfff;
2682
2683                 dma_free_coherent(&adapter->pdev->dev, bufsize,
2684                                     rx_ring->fbr[0]->ring_virtaddr,
2685                                     rx_ring->fbr[0]->ring_physaddr);
2686
2687                 rx_ring->fbr[0]->ring_virtaddr = NULL;
2688         }
2689
2690 #ifdef USE_FBR0
2691         /* Now the same for Free Buffer Ring 0 */
2692         if (rx_ring->fbr[1]->ring_virtaddr) {
2693                 /* First the packet memory */
2694                 for (index = 0; index <
2695                      (rx_ring->fbr[1]->num_entries / FBR_CHUNKS); index++) {
2696                         if (rx_ring->fbr[1]->mem_virtaddrs[index]) {
2697                                 bufsize =
2698                                     (rx_ring->fbr[1]->buffsize *
2699                                      (FBR_CHUNKS + 1)) - 1;
2700
2701                                 dma_free_coherent(&adapter->pdev->dev,
2702                                         bufsize,
2703                                         rx_ring->fbr[1]->mem_virtaddrs[index],
2704                                         rx_ring->fbr[1]->mem_physaddrs[index]);
2705
2706                                 rx_ring->fbr[1]->mem_virtaddrs[index] = NULL;
2707                         }
2708                 }
2709
2710                 /* Now the FIFO itself */
2711                 rx_ring->fbr[1]->ring_virtaddr = (void *)((u8 *)
2712                         rx_ring->fbr[1]->ring_virtaddr - rx_ring->fbr[1]->offset);
2713
2714                 bufsize = (sizeof(struct fbr_desc) * rx_ring->fbr[1]->num_entries)
2715                                                             + 0xfff;
2716
2717                 dma_free_coherent(&adapter->pdev->dev,
2718                                     bufsize,
2719                                     rx_ring->fbr[1]->ring_virtaddr,
2720                                     rx_ring->fbr[1]->ring_physaddr);
2721
2722                 rx_ring->fbr[1]->ring_virtaddr = NULL;
2723         }
2724 #endif
2725
2726         /* Free Packet Status Ring */
2727         if (rx_ring->ps_ring_virtaddr) {
2728                 pktstat_ringsize =
2729                     sizeof(struct pkt_stat_desc) *
2730                     adapter->rx_ring.psr_num_entries;
2731
2732                 dma_free_coherent(&adapter->pdev->dev, pktstat_ringsize,
2733                                     rx_ring->ps_ring_virtaddr,
2734                                     rx_ring->ps_ring_physaddr);
2735
2736                 rx_ring->ps_ring_virtaddr = NULL;
2737         }
2738
2739         /* Free area of memory for the writeback of status information */
2740         if (rx_ring->rx_status_block) {
2741                 dma_free_coherent(&adapter->pdev->dev,
2742                         sizeof(struct rx_status_block),
2743                         rx_ring->rx_status_block, rx_ring->rx_status_bus);
2744                 rx_ring->rx_status_block = NULL;
2745         }
2746
2747         /* Destroy the lookaside (RFD) pool */
2748         if (adapter->flags & fMP_ADAPTER_RECV_LOOKASIDE) {
2749                 kmem_cache_destroy(rx_ring->recv_lookaside);
2750                 adapter->flags &= ~fMP_ADAPTER_RECV_LOOKASIDE;
2751         }
2752
2753         /* Free the FBR Lookup Table */
2754 #ifdef USE_FBR0
2755         kfree(rx_ring->fbr[1]);
2756 #endif
2757
2758         kfree(rx_ring->fbr[0]);
2759
2760         /* Reset Counters */
2761         rx_ring->num_ready_recv = 0;
2762 }
2763
2764 /**
2765  * et131x_init_recv - Initialize receive data structures.
2766  * @adapter: pointer to our private adapter structure
2767  *
2768  * Returns 0 on success and errno on failure (as defined in errno.h)
2769  */
2770 int et131x_init_recv(struct et131x_adapter *adapter)
2771 {
2772         int status = -ENOMEM;
2773         struct rfd *rfd = NULL;
2774         u32 rfdct;
2775         u32 numrfd = 0;
2776         struct rx_ring *rx_ring;
2777
2778         /* Setup some convenience pointers */
2779         rx_ring = &adapter->rx_ring;
2780
2781         /* Setup each RFD */
2782         for (rfdct = 0; rfdct < rx_ring->num_rfd; rfdct++) {
2783                 rfd = kmem_cache_alloc(rx_ring->recv_lookaside,
2784                                                      GFP_ATOMIC | GFP_DMA);
2785
2786                 if (!rfd) {
2787                         dev_err(&adapter->pdev->dev,
2788                                   "Couldn't alloc RFD out of kmem_cache\n");
2789                         status = -ENOMEM;
2790                         continue;
2791                 }
2792
2793                 rfd->skb = NULL;
2794
2795                 /* Add this RFD to the recv_list */
2796                 list_add_tail(&rfd->list_node, &rx_ring->recv_list);
2797
2798                 /* Increment both the available RFD's, and the total RFD's. */
2799                 rx_ring->num_ready_recv++;
2800                 numrfd++;
2801         }
2802
2803         if (numrfd > NIC_MIN_NUM_RFD)
2804                 status = 0;
2805
2806         rx_ring->num_rfd = numrfd;
2807
2808         if (status != 0) {
2809                 kmem_cache_free(rx_ring->recv_lookaside, rfd);
2810                 dev_err(&adapter->pdev->dev,
2811                           "Allocation problems in et131x_init_recv\n");
2812         }
2813         return status;
2814 }
2815
2816 /**
2817  * et131x_set_rx_dma_timer - Set the heartbeat timer according to line rate.
2818  * @adapter: pointer to our adapter structure
2819  */
2820 void et131x_set_rx_dma_timer(struct et131x_adapter *adapter)
2821 {
2822         struct phy_device *phydev = adapter->phydev;
2823
2824         if (!phydev)
2825                 return;
2826
2827         /* For version B silicon, we do not use the RxDMA timer for 10 and 100
2828          * Mbits/s line rates. We do not enable and RxDMA interrupt coalescing.
2829          */
2830         if ((phydev->speed == SPEED_100) || (phydev->speed == SPEED_10)) {
2831                 writel(0, &adapter->regs->rxdma.max_pkt_time);
2832                 writel(1, &adapter->regs->rxdma.num_pkt_done);
2833         }
2834 }
2835
2836 /**
2837  * NICReturnRFD - Recycle a RFD and put it back onto the receive list
2838  * @adapter: pointer to our adapter
2839  * @rfd: pointer to the RFD
2840  */
2841 static void nic_return_rfd(struct et131x_adapter *adapter, struct rfd *rfd)
2842 {
2843         struct rx_ring *rx_local = &adapter->rx_ring;
2844         struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
2845         u16 buff_index = rfd->bufferindex;
2846         u8 ring_index = rfd->ringindex;
2847         unsigned long flags;
2848
2849         /* We don't use any of the OOB data besides status. Otherwise, we
2850          * need to clean up OOB data
2851          */
2852         if (
2853 #ifdef USE_FBR0
2854             (ring_index == 0 && buff_index < rx_local->fbr[1]->num_entries) ||
2855 #endif
2856             (ring_index == 1 && buff_index < rx_local->fbr[0]->num_entries)) {
2857                 spin_lock_irqsave(&adapter->fbr_lock, flags);
2858
2859                 if (ring_index == 1) {
2860                         struct fbr_desc *next =
2861                             (struct fbr_desc *) (rx_local->fbr[0]->ring_virtaddr) +
2862                                          INDEX10(rx_local->fbr[0]->local_full);
2863
2864                         /* Handle the Free Buffer Ring advancement here. Write
2865                          * the PA / Buffer Index for the returned buffer into
2866                          * the oldest (next to be freed)FBR entry
2867                          */
2868                         next->addr_hi = rx_local->fbr[0]->bus_high[buff_index];
2869                         next->addr_lo = rx_local->fbr[0]->bus_low[buff_index];
2870                         next->word2 = buff_index;
2871
2872                         writel(bump_free_buff_ring(&rx_local->fbr[0]->local_full,
2873                                 rx_local->fbr[0]->num_entries - 1),
2874                                 &rx_dma->fbr1_full_offset);
2875                 }
2876 #ifdef USE_FBR0
2877                 else {
2878                         struct fbr_desc *next = (struct fbr_desc *)
2879                                 rx_local->fbr[1]->ring_virtaddr +
2880                                     INDEX10(rx_local->fbr[1]->local_full);
2881
2882                         /* Handle the Free Buffer Ring advancement here. Write
2883                          * the PA / Buffer Index for the returned buffer into
2884                          * the oldest (next to be freed) FBR entry
2885                          */
2886                         next->addr_hi = rx_local->fbr[1]->bus_high[buff_index];
2887                         next->addr_lo = rx_local->fbr[1]->bus_low[buff_index];
2888                         next->word2 = buff_index;
2889
2890                         writel(bump_free_buff_ring(
2891                                         &rx_local->fbr[1]->local_full,
2892                                         rx_local->fbr[1]->num_entries - 1),
2893                                &rx_dma->fbr0_full_offset);
2894                 }
2895 #endif
2896                 spin_unlock_irqrestore(&adapter->fbr_lock, flags);
2897         } else {
2898                 dev_err(&adapter->pdev->dev,
2899                           "%s illegal Buffer Index returned\n", __func__);
2900         }
2901
2902         /* The processing on this RFD is done, so put it back on the tail of
2903          * our list
2904          */
2905         spin_lock_irqsave(&adapter->rcv_lock, flags);
2906         list_add_tail(&rfd->list_node, &rx_local->recv_list);
2907         rx_local->num_ready_recv++;
2908         spin_unlock_irqrestore(&adapter->rcv_lock, flags);
2909
2910         WARN_ON(rx_local->num_ready_recv > rx_local->num_rfd);
2911 }
2912
2913 static struct rfd *nic_rx_pkts(struct et131x_adapter *adapter)
2914 {
2915         struct rx_ring *rx_local = &adapter->rx_ring;
2916         struct rx_status_block *status;
2917         struct pkt_stat_desc *psr;
2918         struct rfd *rfd;
2919         u32 i;
2920         u8 *buf;
2921         unsigned long flags;
2922         struct list_head *element;
2923         u8 ring_index;
2924         u16 buff_index;
2925         u32 len;
2926         u32 word0;
2927         u32 word1;
2928
2929         /* RX Status block is written by the DMA engine prior to every
2930          * interrupt. It contains the next to be used entry in the Packet
2931          * Status Ring, and also the two Free Buffer rings.
2932          */
2933         status = rx_local->rx_status_block;
2934         word1 = status->word1 >> 16;    /* Get the useful bits */
2935
2936         /* Check the PSR and wrap bits do not match */
2937         if ((word1 & 0x1FFF) == (rx_local->local_psr_full & 0x1FFF))
2938                 /* Looks like this ring is not updated yet */
2939                 return NULL;
2940
2941         /* The packet status ring indicates that data is available. */
2942         psr = (struct pkt_stat_desc *) (rx_local->ps_ring_virtaddr) +
2943                         (rx_local->local_psr_full & 0xFFF);
2944
2945         /* Grab any information that is required once the PSR is
2946          * advanced, since we can no longer rely on the memory being
2947          * accurate
2948          */
2949         len = psr->word1 & 0xFFFF;
2950         ring_index = (psr->word1 >> 26) & 0x03;
2951         buff_index = (psr->word1 >> 16) & 0x3FF;
2952         word0 = psr->word0;
2953
2954         /* Indicate that we have used this PSR entry. */
2955         /* FIXME wrap 12 */
2956         add_12bit(&rx_local->local_psr_full, 1);
2957         if (
2958           (rx_local->local_psr_full & 0xFFF) > rx_local->psr_num_entries - 1) {
2959                 /* Clear psr full and toggle the wrap bit */
2960                 rx_local->local_psr_full &=  ~0xFFF;
2961                 rx_local->local_psr_full ^= 0x1000;
2962         }
2963
2964         writel(rx_local->local_psr_full,
2965                &adapter->regs->rxdma.psr_full_offset);
2966
2967 #ifndef USE_FBR0
2968         if (ring_index != 1)
2969                 return NULL;
2970 #endif
2971
2972 #ifdef USE_FBR0
2973         if (ring_index > 1 ||
2974                 (ring_index == 0 &&
2975                 buff_index > rx_local->fbr[1]->num_entries - 1) ||
2976                 (ring_index == 1 &&
2977                 buff_index > rx_local->fbr[0]->num_entries - 1))
2978 #else
2979         if (ring_index != 1 || buff_index > rx_local->fbr[0]->num_entries - 1)
2980 #endif
2981         {
2982                 /* Illegal buffer or ring index cannot be used by S/W*/
2983                 dev_err(&adapter->pdev->dev,
2984                           "NICRxPkts PSR Entry %d indicates "
2985                           "length of %d and/or bad bi(%d)\n",
2986                           rx_local->local_psr_full & 0xFFF,
2987                           len, buff_index);
2988                 return NULL;
2989         }
2990
2991         /* Get and fill the RFD. */
2992         spin_lock_irqsave(&adapter->rcv_lock, flags);
2993
2994         rfd = NULL;
2995         element = rx_local->recv_list.next;
2996         rfd = (struct rfd *) list_entry(element, struct rfd, list_node);
2997
2998         if (rfd == NULL) {
2999                 spin_unlock_irqrestore(&adapter->rcv_lock, flags);
3000                 return NULL;
3001         }
3002
3003         list_del(&rfd->list_node);
3004         rx_local->num_ready_recv--;
3005
3006         spin_unlock_irqrestore(&adapter->rcv_lock, flags);
3007
3008         rfd->bufferindex = buff_index;
3009         rfd->ringindex = ring_index;
3010
3011         /* In V1 silicon, there is a bug which screws up filtering of
3012          * runt packets.  Therefore runt packet filtering is disabled
3013          * in the MAC and the packets are dropped here.  They are
3014          * also counted here.
3015          */
3016         if (len < (NIC_MIN_PACKET_SIZE + 4)) {
3017                 adapter->stats.rx_other_errs++;
3018                 len = 0;
3019         }
3020
3021         if (len) {
3022                 /* Determine if this is a multicast packet coming in */
3023                 if ((word0 & ALCATEL_MULTICAST_PKT) &&
3024                     !(word0 & ALCATEL_BROADCAST_PKT)) {
3025                         /* Promiscuous mode and Multicast mode are
3026                          * not mutually exclusive as was first
3027                          * thought.  I guess Promiscuous is just
3028                          * considered a super-set of the other
3029                          * filters. Generally filter is 0x2b when in
3030                          * promiscuous mode.
3031                          */
3032                         if ((adapter->packet_filter &
3033                                         ET131X_PACKET_TYPE_MULTICAST)
3034                             && !(adapter->packet_filter &
3035                                         ET131X_PACKET_TYPE_PROMISCUOUS)
3036                             && !(adapter->packet_filter &
3037                                         ET131X_PACKET_TYPE_ALL_MULTICAST)) {
3038                                 /*
3039                                  * Note - ring_index for fbr[] array is reversed
3040                                  * 1 for FBR0 etc
3041                                  */
3042                                 buf = rx_local->fbr[(ring_index == 0 ? 1 : 0)]->
3043                                                 virt[buff_index];
3044
3045                                 /* Loop through our list to see if the
3046                                  * destination address of this packet
3047                                  * matches one in our list.
3048                                  */
3049                                 for (i = 0; i < adapter->multicast_addr_count;
3050                                      i++) {
3051                                         if (buf[0] ==
3052                                                 adapter->multicast_list[i][0]
3053                                             && buf[1] ==
3054                                                 adapter->multicast_list[i][1]
3055                                             && buf[2] ==
3056                                                 adapter->multicast_list[i][2]
3057                                             && buf[3] ==
3058                                                 adapter->multicast_list[i][3]
3059                                             && buf[4] ==
3060                                                 adapter->multicast_list[i][4]
3061                                             && buf[5] ==
3062                                                 adapter->multicast_list[i][5]) {
3063                                                 break;
3064                                         }
3065                                 }
3066
3067                                 /* If our index is equal to the number
3068                                  * of Multicast address we have, then
3069                                  * this means we did not find this
3070                                  * packet's matching address in our
3071                                  * list.  Set the len to zero,
3072                                  * so we free our RFD when we return
3073                                  * from this function.
3074                                  */
3075                                 if (i == adapter->multicast_addr_count)
3076                                         len = 0;
3077                         }
3078
3079                         if (len > 0)
3080                                 adapter->stats.multicast_pkts_rcvd++;
3081                 } else if (word0 & ALCATEL_BROADCAST_PKT)
3082                         adapter->stats.broadcast_pkts_rcvd++;
3083                 else
3084                         /* Not sure what this counter measures in
3085                          * promiscuous mode. Perhaps we should check
3086                          * the MAC address to see if it is directed
3087                          * to us in promiscuous mode.
3088                          */
3089                         adapter->stats.unicast_pkts_rcvd++;
3090         }
3091
3092         if (len > 0) {
3093                 struct sk_buff *skb = NULL;
3094
3095                 /*rfd->len = len - 4; */
3096                 rfd->len = len;
3097
3098                 skb = dev_alloc_skb(rfd->len + 2);
3099                 if (!skb) {
3100                         dev_err(&adapter->pdev->dev,
3101                                   "Couldn't alloc an SKB for Rx\n");
3102                         return NULL;
3103                 }
3104
3105                 adapter->net_stats.rx_bytes += rfd->len;
3106
3107                 /*
3108                  * Note - ring_index for fbr[] array is reversed,
3109                  * 1 for FBR0 etc
3110                  */
3111                 memcpy(skb_put(skb, rfd->len),
3112                        rx_local->fbr[(ring_index == 0 ? 1 : 0)]->virt[buff_index],
3113                        rfd->len);
3114
3115                 skb->dev = adapter->netdev;
3116                 skb->protocol = eth_type_trans(skb, adapter->netdev);
3117                 skb->ip_summed = CHECKSUM_NONE;
3118
3119                 netif_rx(skb);
3120         } else {
3121                 rfd->len = 0;
3122         }
3123
3124         nic_return_rfd(adapter, rfd);
3125         return rfd;
3126 }
3127
3128 /**
3129  * et131x_handle_recv_interrupt - Interrupt handler for receive processing
3130  * @adapter: pointer to our adapter
3131  *
3132  * Assumption, Rcv spinlock has been acquired.
3133  */
3134 void et131x_handle_recv_interrupt(struct et131x_adapter *adapter)
3135 {
3136         struct rfd *rfd = NULL;
3137         u32 count = 0;
3138         bool done = true;
3139
3140         /* Process up to available RFD's */
3141         while (count < NUM_PACKETS_HANDLED) {
3142                 if (list_empty(&adapter->rx_ring.recv_list)) {
3143                         WARN_ON(adapter->rx_ring.num_ready_recv != 0);
3144                         done = false;
3145                         break;
3146                 }
3147
3148                 rfd = nic_rx_pkts(adapter);
3149
3150                 if (rfd == NULL)
3151                         break;
3152
3153                 /* Do not receive any packets until a filter has been set.
3154                  * Do not receive any packets until we have link.
3155                  * If length is zero, return the RFD in order to advance the
3156                  * Free buffer ring.
3157                  */
3158                 if (!adapter->packet_filter ||
3159                     !netif_carrier_ok(adapter->netdev) ||
3160                     rfd->len == 0)
3161                         continue;
3162
3163                 /* Increment the number of packets we received */
3164                 adapter->net_stats.rx_packets++;
3165
3166                 /* Set the status on the packet, either resources or success */
3167                 if (adapter->rx_ring.num_ready_recv < RFD_LOW_WATER_MARK) {
3168                         dev_warn(&adapter->pdev->dev,
3169                                     "RFD's are running out\n");
3170                 }
3171                 count++;
3172         }
3173
3174         if (count == NUM_PACKETS_HANDLED || !done) {
3175                 adapter->rx_ring.unfinished_receives = true;
3176                 writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
3177                        &adapter->regs->global.watchdog_timer);
3178         } else
3179                 /* Watchdog timer will disable itself if appropriate. */
3180                 adapter->rx_ring.unfinished_receives = false;
3181 }
3182
3183 /* TX functions */
3184
3185 /**
3186  * et131x_tx_dma_memory_alloc
3187  * @adapter: pointer to our private adapter structure
3188  *
3189  * Returns 0 on success and errno on failure (as defined in errno.h).
3190  *
3191  * Allocates memory that will be visible both to the device and to the CPU.
3192  * The OS will pass us packets, pointers to which we will insert in the Tx
3193  * Descriptor queue. The device will read this queue to find the packets in
3194  * memory. The device will update the "status" in memory each time it xmits a
3195  * packet.
3196  */
3197 int et131x_tx_dma_memory_alloc(struct et131x_adapter *adapter)
3198 {
3199         int desc_size = 0;
3200         struct tx_ring *tx_ring = &adapter->tx_ring;
3201
3202         /* Allocate memory for the TCB's (Transmit Control Block) */
3203         adapter->tx_ring.tcb_ring =
3204                 kcalloc(NUM_TCB, sizeof(struct tcb), GFP_ATOMIC | GFP_DMA);
3205         if (!adapter->tx_ring.tcb_ring) {
3206                 dev_err(&adapter->pdev->dev, "Cannot alloc memory for TCBs\n");
3207                 return -ENOMEM;
3208         }
3209
3210         /* Allocate enough memory for the Tx descriptor ring, and allocate
3211          * some extra so that the ring can be aligned on a 4k boundary.
3212          */
3213         desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX) + 4096 - 1;
3214         tx_ring->tx_desc_ring =
3215             (struct tx_desc *) dma_alloc_coherent(&adapter->pdev->dev, desc_size,
3216                                                     &tx_ring->tx_desc_ring_pa, GFP_KERNEL);
3217         if (!adapter->tx_ring.tx_desc_ring) {
3218                 dev_err(&adapter->pdev->dev,
3219                                         "Cannot alloc memory for Tx Ring\n");
3220                 return -ENOMEM;
3221         }
3222
3223         /* Save physical address
3224          *
3225          * NOTE: dma_alloc_coherent(), used above to alloc DMA regions,
3226          * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
3227          * are ever returned, make sure the high part is retrieved here before
3228          * storing the adjusted address.
3229          */
3230         /* Allocate memory for the Tx status block */
3231         tx_ring->tx_status = dma_alloc_coherent(&adapter->pdev->dev,
3232                                                     sizeof(u32),
3233                                                     &tx_ring->tx_status_pa,
3234                                                     GFP_KERNEL);
3235         if (!adapter->tx_ring.tx_status_pa) {
3236                 dev_err(&adapter->pdev->dev,
3237                                   "Cannot alloc memory for Tx status block\n");
3238                 return -ENOMEM;
3239         }
3240         return 0;
3241 }
3242
3243 /**
3244  * et131x_tx_dma_memory_free - Free all memory allocated within this module
3245  * @adapter: pointer to our private adapter structure
3246  *
3247  * Returns 0 on success and errno on failure (as defined in errno.h).
3248  */
3249 void et131x_tx_dma_memory_free(struct et131x_adapter *adapter)
3250 {
3251         int desc_size = 0;
3252
3253         if (adapter->tx_ring.tx_desc_ring) {
3254                 /* Free memory relating to Tx rings here */
3255                 desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX)
3256                                                                 + 4096 - 1;
3257                 dma_free_coherent(&adapter->pdev->dev,
3258                                     desc_size,
3259                                     adapter->tx_ring.tx_desc_ring,
3260                                     adapter->tx_ring.tx_desc_ring_pa);
3261                 adapter->tx_ring.tx_desc_ring = NULL;
3262         }
3263
3264         /* Free memory for the Tx status block */
3265         if (adapter->tx_ring.tx_status) {
3266                 dma_free_coherent(&adapter->pdev->dev,
3267                                     sizeof(u32),
3268                                     adapter->tx_ring.tx_status,
3269                                     adapter->tx_ring.tx_status_pa);
3270
3271                 adapter->tx_ring.tx_status = NULL;
3272         }
3273         /* Free the memory for the tcb structures */
3274         kfree(adapter->tx_ring.tcb_ring);
3275 }
3276
3277 /**
3278  * nic_send_packet - NIC specific send handler for version B silicon.
3279  * @adapter: pointer to our adapter
3280  * @tcb: pointer to struct tcb
3281  *
3282  * Returns 0 or errno.
3283  */
3284 static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
3285 {
3286         u32 i;
3287         struct tx_desc desc[24];        /* 24 x 16 byte */
3288         u32 frag = 0;
3289         u32 thiscopy, remainder;
3290         struct sk_buff *skb = tcb->skb;
3291         u32 nr_frags = skb_shinfo(skb)->nr_frags + 1;
3292         struct skb_frag_struct *frags = &skb_shinfo(skb)->frags[0];
3293         unsigned long flags;
3294         struct phy_device *phydev = adapter->phydev;
3295
3296         /* Part of the optimizations of this send routine restrict us to
3297          * sending 24 fragments at a pass.  In practice we should never see
3298          * more than 5 fragments.
3299          *
3300          * NOTE: The older version of this function (below) can handle any
3301          * number of fragments. If needed, we can call this function,
3302          * although it is less efficient.
3303          */
3304         if (nr_frags > 23)
3305                 return -EIO;
3306
3307         memset(desc, 0, sizeof(struct tx_desc) * (nr_frags + 1));
3308
3309         for (i = 0; i < nr_frags; i++) {
3310                 /* If there is something in this element, lets get a
3311                  * descriptor from the ring and get the necessary data
3312                  */
3313                 if (i == 0) {
3314                         /* If the fragments are smaller than a standard MTU,
3315                          * then map them to a single descriptor in the Tx
3316                          * Desc ring. However, if they're larger, as is
3317                          * possible with support for jumbo packets, then
3318                          * split them each across 2 descriptors.
3319                          *
3320                          * This will work until we determine why the hardware
3321                          * doesn't seem to like large fragments.
3322                          */
3323                         if ((skb->len - skb->data_len) <= 1514) {
3324                                 desc[frag].addr_hi = 0;
3325                                 /* Low 16bits are length, high is vlan and
3326                                    unused currently so zero */
3327                                 desc[frag].len_vlan =
3328                                         skb->len - skb->data_len;
3329
3330                                 /* NOTE: Here, the dma_addr_t returned from
3331                                  * dma_map_single() is implicitly cast as a
3332                                  * u32. Although dma_addr_t can be
3333                                  * 64-bit, the address returned by
3334                                  * dma_map_single() is always 32-bit
3335                                  * addressable (as defined by the pci/dma
3336                                  * subsystem)
3337                                  */
3338                                 desc[frag++].addr_lo =
3339                                     dma_map_single(&adapter->pdev->dev,
3340                                                    skb->data,
3341                                                    skb->len -
3342                                                    skb->data_len,
3343                                                    DMA_TO_DEVICE);
3344                         } else {
3345                                 desc[frag].addr_hi = 0;
3346                                 desc[frag].len_vlan =
3347                                     (skb->len - skb->data_len) / 2;
3348
3349                                 /* NOTE: Here, the dma_addr_t returned from
3350                                  * dma_map_single() is implicitly cast as a
3351                                  * u32. Although dma_addr_t can be
3352                                  * 64-bit, the address returned by
3353                                  * dma_map_single() is always 32-bit
3354                                  * addressable (as defined by the pci/dma
3355                                  * subsystem)
3356                                  */
3357                                 desc[frag++].addr_lo =
3358                                     dma_map_single(&adapter->pdev->dev,
3359                                                    skb->data,
3360                                                    ((skb->len -
3361                                                      skb->data_len) / 2),
3362                                                    DMA_TO_DEVICE);
3363                                 desc[frag].addr_hi = 0;
3364
3365                                 desc[frag].len_vlan =
3366                                     (skb->len - skb->data_len) / 2;
3367
3368                                 /* NOTE: Here, the dma_addr_t returned from
3369                                  * dma_map_single() is implicitly cast as a
3370                                  * u32. Although dma_addr_t can be
3371                                  * 64-bit, the address returned by
3372                                  * dma_map_single() is always 32-bit
3373                                  * addressable (as defined by the pci/dma
3374                                  * subsystem)
3375                                  */
3376                                 desc[frag++].addr_lo =
3377                                     dma_map_single(&adapter->pdev->dev,
3378                                                    skb->data +
3379                                                    ((skb->len -
3380                                                      skb->data_len) / 2),
3381                                                    ((skb->len -
3382                                                      skb->data_len) / 2),
3383                                                    DMA_TO_DEVICE);
3384                         }
3385                 } else {
3386                         desc[frag].addr_hi = 0;
3387                         desc[frag].len_vlan =
3388                                         frags[i - 1].size;
3389
3390                         /* NOTE: Here, the dma_addr_t returned from
3391                          * dma_map_page() is implicitly cast as a u32.
3392                          * Although dma_addr_t can be 64-bit, the address
3393                          * returned by dma_map_page() is always 32-bit
3394                          * addressable (as defined by the pci/dma subsystem)
3395                          */
3396                         desc[frag++].addr_lo =
3397                             dma_map_page(&adapter->pdev->dev,
3398                                          frags[i - 1].page,
3399                                          frags[i - 1].page_offset,
3400                                          frags[i - 1].size,
3401                                          DMA_TO_DEVICE);
3402                 }
3403         }
3404
3405         if (frag == 0)
3406                 return -EIO;
3407
3408         if (phydev && phydev->speed == SPEED_1000) {
3409                 if (++adapter->tx_ring.since_irq == PARM_TX_NUM_BUFS_DEF) {
3410                         /* Last element & Interrupt flag */
3411                         desc[frag - 1].flags = 0x5;
3412                         adapter->tx_ring.since_irq = 0;
3413                 } else { /* Last element */
3414                         desc[frag - 1].flags = 0x1;
3415                 }
3416         } else
3417                 desc[frag - 1].flags = 0x5;
3418
3419         desc[0].flags |= 2;     /* First element flag */
3420
3421         tcb->index_start = adapter->tx_ring.send_idx;
3422         tcb->stale = 0;
3423
3424         spin_lock_irqsave(&adapter->send_hw_lock, flags);
3425
3426         thiscopy = NUM_DESC_PER_RING_TX -
3427                                 INDEX10(adapter->tx_ring.send_idx);
3428
3429         if (thiscopy >= frag) {
3430                 remainder = 0;
3431                 thiscopy = frag;
3432         } else {
3433                 remainder = frag - thiscopy;
3434         }
3435
3436         memcpy(adapter->tx_ring.tx_desc_ring +
3437                INDEX10(adapter->tx_ring.send_idx), desc,
3438                sizeof(struct tx_desc) * thiscopy);
3439
3440         add_10bit(&adapter->tx_ring.send_idx, thiscopy);
3441
3442         if (INDEX10(adapter->tx_ring.send_idx) == 0 ||
3443                   INDEX10(adapter->tx_ring.send_idx) == NUM_DESC_PER_RING_TX) {
3444                 adapter->tx_ring.send_idx &= ~ET_DMA10_MASK;
3445                 adapter->tx_ring.send_idx ^= ET_DMA10_WRAP;
3446         }
3447
3448         if (remainder) {
3449                 memcpy(adapter->tx_ring.tx_desc_ring,
3450                        desc + thiscopy,
3451                        sizeof(struct tx_desc) * remainder);
3452
3453                 add_10bit(&adapter->tx_ring.send_idx, remainder);
3454         }
3455
3456         if (INDEX10(adapter->tx_ring.send_idx) == 0) {
3457                 if (adapter->tx_ring.send_idx)
3458                         tcb->index = NUM_DESC_PER_RING_TX - 1;
3459                 else
3460                         tcb->index = ET_DMA10_WRAP|(NUM_DESC_PER_RING_TX - 1);
3461         } else
3462                 tcb->index = adapter->tx_ring.send_idx - 1;
3463
3464         spin_lock(&adapter->tcb_send_qlock);
3465
3466         if (adapter->tx_ring.send_tail)
3467                 adapter->tx_ring.send_tail->next = tcb;
3468         else
3469                 adapter->tx_ring.send_head = tcb;
3470
3471         adapter->tx_ring.send_tail = tcb;
3472
3473         WARN_ON(tcb->next != NULL);
3474
3475         adapter->tx_ring.used++;
3476
3477         spin_unlock(&adapter->tcb_send_qlock);
3478
3479         /* Write the new write pointer back to the device. */
3480         writel(adapter->tx_ring.send_idx,
3481                &adapter->regs->txdma.service_request);
3482
3483         /* For Gig only, we use Tx Interrupt coalescing.  Enable the software
3484          * timer to wake us up if this packet isn't followed by N more.
3485          */
3486         if (phydev && phydev->speed == SPEED_1000) {
3487                 writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
3488                        &adapter->regs->global.watchdog_timer);
3489         }
3490         spin_unlock_irqrestore(&adapter->send_hw_lock, flags);
3491
3492         return 0;
3493 }
3494
3495 /**
3496  * send_packet - Do the work to send a packet
3497  * @skb: the packet(s) to send
3498  * @adapter: a pointer to the device's private adapter structure
3499  *
3500  * Return 0 in almost all cases; non-zero value in extreme hard failure only.
3501  *
3502  * Assumption: Send spinlock has been acquired
3503  */
3504 static int send_packet(struct sk_buff *skb, struct et131x_adapter *adapter)
3505 {
3506         int status;
3507         struct tcb *tcb = NULL;
3508         u16 *shbufva;
3509         unsigned long flags;
3510
3511         /* All packets must have at least a MAC address and a protocol type */
3512         if (skb->len < ETH_HLEN)
3513                 return -EIO;
3514
3515         /* Get a TCB for this packet */
3516         spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
3517
3518         tcb = adapter->tx_ring.tcb_qhead;
3519
3520         if (tcb == NULL) {
3521                 spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
3522                 return -ENOMEM;
3523         }
3524
3525         adapter->tx_ring.tcb_qhead = tcb->next;
3526
3527         if (adapter->tx_ring.tcb_qhead == NULL)
3528                 adapter->tx_ring.tcb_qtail = NULL;
3529
3530         spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
3531
3532         tcb->skb = skb;
3533
3534         if (skb->data != NULL && skb->len - skb->data_len >= 6) {
3535                 shbufva = (u16 *) skb->data;
3536
3537                 if ((shbufva[0] == 0xffff) &&
3538                     (shbufva[1] == 0xffff) && (shbufva[2] == 0xffff)) {
3539                         tcb->flags |= fMP_DEST_BROAD;
3540                 } else if ((shbufva[0] & 0x3) == 0x0001) {
3541                         tcb->flags |=  fMP_DEST_MULTI;
3542                 }
3543         }
3544
3545         tcb->next = NULL;
3546
3547         /* Call the NIC specific send handler. */
3548         status = nic_send_packet(adapter, tcb);
3549
3550         if (status != 0) {
3551                 spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
3552
3553                 if (adapter->tx_ring.tcb_qtail)
3554                         adapter->tx_ring.tcb_qtail->next = tcb;
3555                 else
3556                         /* Apparently ready Q is empty. */
3557                         adapter->tx_ring.tcb_qhead = tcb;
3558
3559                 adapter->tx_ring.tcb_qtail = tcb;
3560                 spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
3561                 return status;
3562         }
3563         WARN_ON(adapter->tx_ring.used > NUM_TCB);
3564         return 0;
3565 }
3566
3567 /**
3568  * et131x_send_packets - This function is called by the OS to send packets
3569  * @skb: the packet(s) to send
3570  * @netdev:device on which to TX the above packet(s)
3571  *
3572  * Return 0 in almost all cases; non-zero value in extreme hard failure only
3573  */
3574 int et131x_send_packets(struct sk_buff *skb, struct net_device *netdev)
3575 {
3576         int status = 0;
3577         struct et131x_adapter *adapter = netdev_priv(netdev);
3578
3579         /* Send these packets
3580          *
3581          * NOTE: The Linux Tx entry point is only given one packet at a time
3582          * to Tx, so the PacketCount and it's array used makes no sense here
3583          */
3584
3585         /* TCB is not available */
3586         if (adapter->tx_ring.used >= NUM_TCB) {
3587                 /* NOTE: If there's an error on send, no need to queue the
3588                  * packet under Linux; if we just send an error up to the
3589                  * netif layer, it will resend the skb to us.
3590                  */
3591                 status = -ENOMEM;
3592         } else {
3593                 /* We need to see if the link is up; if it's not, make the
3594                  * netif layer think we're good and drop the packet
3595                  */
3596                 if ((adapter->flags & fMP_ADAPTER_FAIL_SEND_MASK) ||
3597                                         !netif_carrier_ok(netdev)) {
3598                         dev_kfree_skb_any(skb);
3599                         skb = NULL;
3600
3601                         adapter->net_stats.tx_dropped++;
3602                 } else {
3603                         status = send_packet(skb, adapter);
3604                         if (status != 0 && status != -ENOMEM) {
3605                                 /* On any other error, make netif think we're
3606                                  * OK and drop the packet
3607                                  */
3608                                 dev_kfree_skb_any(skb);
3609                                 skb = NULL;
3610                                 adapter->net_stats.tx_dropped++;
3611                         }
3612                 }
3613         }
3614         return status;
3615 }
3616
3617 /**
3618  * free_send_packet - Recycle a struct tcb
3619  * @adapter: pointer to our adapter
3620  * @tcb: pointer to struct tcb
3621  *
3622  * Complete the packet if necessary
3623  * Assumption - Send spinlock has been acquired
3624  */
3625 static inline void free_send_packet(struct et131x_adapter *adapter,
3626                                                 struct tcb *tcb)
3627 {
3628         unsigned long flags;
3629         struct tx_desc *desc = NULL;
3630         struct net_device_stats *stats = &adapter->net_stats;
3631
3632         if (tcb->flags & fMP_DEST_BROAD)
3633                 atomic_inc(&adapter->stats.broadcast_pkts_xmtd);
3634         else if (tcb->flags & fMP_DEST_MULTI)
3635                 atomic_inc(&adapter->stats.multicast_pkts_xmtd);
3636         else
3637                 atomic_inc(&adapter->stats.unicast_pkts_xmtd);
3638
3639         if (tcb->skb) {
3640                 stats->tx_bytes += tcb->skb->len;
3641
3642                 /* Iterate through the TX descriptors on the ring
3643                  * corresponding to this packet and umap the fragments
3644                  * they point to
3645                  */
3646                 do {
3647                         desc = (struct tx_desc *)
3648                                     (adapter->tx_ring.tx_desc_ring +
3649                                                 INDEX10(tcb->index_start));
3650
3651                         dma_unmap_single(&adapter->pdev->dev,
3652                                          desc->addr_lo,
3653                                          desc->len_vlan, DMA_TO_DEVICE);
3654
3655                         add_10bit(&tcb->index_start, 1);
3656                         if (INDEX10(tcb->index_start) >=
3657                                                         NUM_DESC_PER_RING_TX) {
3658                                 tcb->index_start &= ~ET_DMA10_MASK;
3659                                 tcb->index_start ^= ET_DMA10_WRAP;
3660                         }
3661                 } while (desc != (adapter->tx_ring.tx_desc_ring +
3662                                 INDEX10(tcb->index)));
3663
3664                 dev_kfree_skb_any(tcb->skb);
3665         }
3666
3667         memset(tcb, 0, sizeof(struct tcb));
3668
3669         /* Add the TCB to the Ready Q */
3670         spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
3671
3672         adapter->net_stats.tx_packets++;
3673
3674         if (adapter->tx_ring.tcb_qtail)
3675                 adapter->tx_ring.tcb_qtail->next = tcb;
3676         else
3677                 /* Apparently ready Q is empty. */
3678                 adapter->tx_ring.tcb_qhead = tcb;
3679
3680         adapter->tx_ring.tcb_qtail = tcb;
3681
3682         spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
3683         WARN_ON(adapter->tx_ring.used < 0);
3684 }
3685
3686 /**
3687  * et131x_free_busy_send_packets - Free and complete the stopped active sends
3688  * @adapter: pointer to our adapter
3689  *
3690  * Assumption - Send spinlock has been acquired
3691  */
3692 void et131x_free_busy_send_packets(struct et131x_adapter *adapter)
3693 {
3694         struct tcb *tcb;
3695         unsigned long flags;
3696         u32 freed = 0;
3697
3698         /* Any packets being sent? Check the first TCB on the send list */
3699         spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3700
3701         tcb = adapter->tx_ring.send_head;
3702
3703         while (tcb != NULL && freed < NUM_TCB) {
3704                 struct tcb *next = tcb->next;
3705
3706                 adapter->tx_ring.send_head = next;
3707
3708                 if (next == NULL)
3709                         adapter->tx_ring.send_tail = NULL;
3710
3711                 adapter->tx_ring.used--;
3712
3713                 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3714
3715                 freed++;
3716                 free_send_packet(adapter, tcb);
3717
3718                 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3719
3720                 tcb = adapter->tx_ring.send_head;
3721         }
3722
3723         WARN_ON(freed == NUM_TCB);
3724
3725         spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3726
3727         adapter->tx_ring.used = 0;
3728 }
3729
3730 /**
3731  * et131x_handle_send_interrupt - Interrupt handler for sending processing
3732  * @adapter: pointer to our adapter
3733  *
3734  * Re-claim the send resources, complete sends and get more to send from
3735  * the send wait queue.
3736  *
3737  * Assumption - Send spinlock has been acquired
3738  */
3739 void et131x_handle_send_interrupt(struct et131x_adapter *adapter)
3740 {
3741         unsigned long flags;
3742         u32 serviced;
3743         struct tcb *tcb;
3744         u32 index;
3745
3746         serviced = readl(&adapter->regs->txdma.new_service_complete);
3747         index = INDEX10(serviced);
3748
3749         /* Has the ring wrapped?  Process any descriptors that do not have
3750          * the same "wrap" indicator as the current completion indicator
3751          */
3752         spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3753
3754         tcb = adapter->tx_ring.send_head;
3755
3756         while (tcb &&
3757                ((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
3758                index < INDEX10(tcb->index)) {
3759                 adapter->tx_ring.used--;
3760                 adapter->tx_ring.send_head = tcb->next;
3761                 if (tcb->next == NULL)
3762                         adapter->tx_ring.send_tail = NULL;
3763
3764                 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3765                 free_send_packet(adapter, tcb);
3766                 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3767
3768                 /* Goto the next packet */
3769                 tcb = adapter->tx_ring.send_head;
3770         }
3771         while (tcb &&
3772                !((serviced ^ tcb->index) & ET_DMA10_WRAP)
3773                && index > (tcb->index & ET_DMA10_MASK)) {
3774                 adapter->tx_ring.used--;
3775                 adapter->tx_ring.send_head = tcb->next;
3776                 if (tcb->next == NULL)
3777                         adapter->tx_ring.send_tail = NULL;
3778
3779                 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3780                 free_send_packet(adapter, tcb);
3781                 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3782
3783                 /* Goto the next packet */
3784                 tcb = adapter->tx_ring.send_head;
3785         }
3786
3787         /* Wake up the queue when we hit a low-water mark */
3788         if (adapter->tx_ring.used <= NUM_TCB / 3)
3789                 netif_wake_queue(adapter->netdev);
3790
3791         spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3792 }
3793
3794 /* ETHTOOL functions */
3795
3796 static int et131x_get_settings(struct net_device *netdev,
3797                                struct ethtool_cmd *cmd)
3798 {
3799         struct et131x_adapter *adapter = netdev_priv(netdev);
3800
3801         return phy_ethtool_gset(adapter->phydev, cmd);
3802 }
3803
3804 static int et131x_set_settings(struct net_device *netdev,
3805                                struct ethtool_cmd *cmd)
3806 {
3807         struct et131x_adapter *adapter = netdev_priv(netdev);
3808
3809         return phy_ethtool_sset(adapter->phydev, cmd);
3810 }
3811
3812 static int et131x_get_regs_len(struct net_device *netdev)
3813 {
3814 #define ET131X_REGS_LEN 256
3815         return ET131X_REGS_LEN * sizeof(u32);
3816 }
3817
3818 static void et131x_get_regs(struct net_device *netdev,
3819                             struct ethtool_regs *regs, void *regs_data)
3820 {
3821         struct et131x_adapter *adapter = netdev_priv(netdev);
3822         struct address_map __iomem *aregs = adapter->regs;
3823         u32 *regs_buff = regs_data;
3824         u32 num = 0;
3825
3826         memset(regs_data, 0, et131x_get_regs_len(netdev));
3827
3828         regs->version = (1 << 24) | (adapter->pdev->revision << 16) |
3829                         adapter->pdev->device;
3830
3831         /* PHY regs */
3832         et131x_mii_read(adapter, MII_BMCR, (u16 *)&regs_buff[num++]);
3833         et131x_mii_read(adapter, MII_BMSR, (u16 *)&regs_buff[num++]);
3834         et131x_mii_read(adapter, MII_PHYSID1, (u16 *)&regs_buff[num++]);
3835         et131x_mii_read(adapter, MII_PHYSID2, (u16 *)&regs_buff[num++]);
3836         et131x_mii_read(adapter, MII_ADVERTISE, (u16 *)&regs_buff[num++]);
3837         et131x_mii_read(adapter, MII_LPA, (u16 *)&regs_buff[num++]);
3838         et131x_mii_read(adapter, MII_EXPANSION, (u16 *)&regs_buff[num++]);
3839         /* Autoneg next page transmit reg */
3840         et131x_mii_read(adapter, 0x07, (u16 *)&regs_buff[num++]);
3841         /* Link partner next page reg */
3842         et131x_mii_read(adapter, 0x08, (u16 *)&regs_buff[num++]);
3843         et131x_mii_read(adapter, MII_CTRL1000, (u16 *)&regs_buff[num++]);
3844         et131x_mii_read(adapter, MII_STAT1000, (u16 *)&regs_buff[num++]);
3845         et131x_mii_read(adapter, MII_ESTATUS, (u16 *)&regs_buff[num++]);
3846         et131x_mii_read(adapter, PHY_INDEX_REG, (u16 *)&regs_buff[num++]);
3847         et131x_mii_read(adapter, PHY_DATA_REG, (u16 *)&regs_buff[num++]);
3848         et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
3849                         (u16 *)&regs_buff[num++]);
3850         et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL,
3851                         (u16 *)&regs_buff[num++]);
3852         et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL+1,
3853                         (u16 *)&regs_buff[num++]);
3854         et131x_mii_read(adapter, PHY_REGISTER_MGMT_CONTROL,
3855                         (u16 *)&regs_buff[num++]);
3856         et131x_mii_read(adapter, PHY_CONFIG, (u16 *)&regs_buff[num++]);
3857         et131x_mii_read(adapter, PHY_PHY_CONTROL, (u16 *)&regs_buff[num++]);
3858         et131x_mii_read(adapter, PHY_INTERRUPT_MASK, (u16 *)&regs_buff[num++]);
3859         et131x_mii_read(adapter, PHY_INTERRUPT_STATUS,
3860                         (u16 *)&regs_buff[num++]);
3861         et131x_mii_read(adapter, PHY_PHY_STATUS, (u16 *)&regs_buff[num++]);
3862         et131x_mii_read(adapter, PHY_LED_1, (u16 *)&regs_buff[num++]);
3863         et131x_mii_read(adapter, PHY_LED_2, (u16 *)&regs_buff[num++]);
3864
3865         /* Global regs */
3866         regs_buff[num++] = readl(&aregs->global.txq_start_addr);
3867         regs_buff[num++] = readl(&aregs->global.txq_end_addr);
3868         regs_buff[num++] = readl(&aregs->global.rxq_start_addr);
3869         regs_buff[num++] = readl(&aregs->global.rxq_end_addr);
3870         regs_buff[num++] = readl(&aregs->global.pm_csr);
3871         regs_buff[num++] = adapter->stats.interrupt_status;
3872         regs_buff[num++] = readl(&aregs->global.int_mask);
3873         regs_buff[num++] = readl(&aregs->global.int_alias_clr_en);
3874         regs_buff[num++] = readl(&aregs->global.int_status_alias);
3875         regs_buff[num++] = readl(&aregs->global.sw_reset);
3876         regs_buff[num++] = readl(&aregs->global.slv_timer);
3877         regs_buff[num++] = readl(&aregs->global.msi_config);
3878         regs_buff[num++] = readl(&aregs->global.loopback);
3879         regs_buff[num++] = readl(&aregs->global.watchdog_timer);
3880
3881         /* TXDMA regs */
3882         regs_buff[num++] = readl(&aregs->txdma.csr);
3883         regs_buff[num++] = readl(&aregs->txdma.pr_base_hi);
3884         regs_buff[num++] = readl(&aregs->txdma.pr_base_lo);
3885         regs_buff[num++] = readl(&aregs->txdma.pr_num_des);
3886         regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr);
3887         regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr_ext);
3888         regs_buff[num++] = readl(&aregs->txdma.txq_rd_addr);
3889         regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_hi);
3890         regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_lo);
3891         regs_buff[num++] = readl(&aregs->txdma.service_request);
3892         regs_buff[num++] = readl(&aregs->txdma.service_complete);
3893         regs_buff[num++] = readl(&aregs->txdma.cache_rd_index);
3894         regs_buff[num++] = readl(&aregs->txdma.cache_wr_index);
3895         regs_buff[num++] = readl(&aregs->txdma.tx_dma_error);
3896         regs_buff[num++] = readl(&aregs->txdma.desc_abort_cnt);
3897         regs_buff[num++] = readl(&aregs->txdma.payload_abort_cnt);
3898         regs_buff[num++] = readl(&aregs->txdma.writeback_abort_cnt);
3899         regs_buff[num++] = readl(&aregs->txdma.desc_timeout_cnt);
3900         regs_buff[num++] = readl(&aregs->txdma.payload_timeout_cnt);
3901         regs_buff[num++] = readl(&aregs->txdma.writeback_timeout_cnt);
3902         regs_buff[num++] = readl(&aregs->txdma.desc_error_cnt);
3903         regs_buff[num++] = readl(&aregs->txdma.payload_error_cnt);
3904         regs_buff[num++] = readl(&aregs->txdma.writeback_error_cnt);
3905         regs_buff[num++] = readl(&aregs->txdma.dropped_tlp_cnt);
3906         regs_buff[num++] = readl(&aregs->txdma.new_service_complete);
3907         regs_buff[num++] = readl(&aregs->txdma.ethernet_packet_cnt);
3908
3909         /* RXDMA regs */
3910         regs_buff[num++] = readl(&aregs->rxdma.csr);
3911         regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_hi);
3912         regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_lo);
3913         regs_buff[num++] = readl(&aregs->rxdma.num_pkt_done);
3914         regs_buff[num++] = readl(&aregs->rxdma.max_pkt_time);
3915         regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr);
3916         regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr_ext);
3917         regs_buff[num++] = readl(&aregs->rxdma.rxq_wr_addr);
3918         regs_buff[num++] = readl(&aregs->rxdma.psr_base_hi);
3919         regs_buff[num++] = readl(&aregs->rxdma.psr_base_lo);
3920         regs_buff[num++] = readl(&aregs->rxdma.psr_num_des);
3921         regs_buff[num++] = readl(&aregs->rxdma.psr_avail_offset);
3922         regs_buff[num++] = readl(&aregs->rxdma.psr_full_offset);
3923         regs_buff[num++] = readl(&aregs->rxdma.psr_access_index);
3924         regs_buff[num++] = readl(&aregs->rxdma.psr_min_des);
3925         regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_lo);
3926         regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_hi);
3927         regs_buff[num++] = readl(&aregs->rxdma.fbr0_num_des);
3928         regs_buff[num++] = readl(&aregs->rxdma.fbr0_avail_offset);
3929         regs_buff[num++] = readl(&aregs->rxdma.fbr0_full_offset);
3930         regs_buff[num++] = readl(&aregs->rxdma.fbr0_rd_index);
3931         regs_buff[num++] = readl(&aregs->rxdma.fbr0_min_des);
3932         regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_lo);
3933         regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_hi);
3934         regs_buff[num++] = readl(&aregs->rxdma.fbr1_num_des);
3935         regs_buff[num++] = readl(&aregs->rxdma.fbr1_avail_offset);
3936         regs_buff[num++] = readl(&aregs->rxdma.fbr1_full_offset);
3937         regs_buff[num++] = readl(&aregs->rxdma.fbr1_rd_index);
3938         regs_buff[num++] = readl(&aregs->rxdma.fbr1_min_des);
3939 }
3940
3941 #define ET131X_DRVINFO_LEN 32 /* value from ethtool.h */
3942 static void et131x_get_drvinfo(struct net_device *netdev,
3943                                struct ethtool_drvinfo *info)
3944 {
3945         struct et131x_adapter *adapter = netdev_priv(netdev);
3946
3947         strncpy(info->driver, DRIVER_NAME, ET131X_DRVINFO_LEN);
3948         strncpy(info->version, DRIVER_VERSION, ET131X_DRVINFO_LEN);
3949         strncpy(info->bus_info, pci_name(adapter->pdev), ET131X_DRVINFO_LEN);
3950 }
3951
3952 static struct ethtool_ops et131x_ethtool_ops = {
3953         .get_settings   = et131x_get_settings,
3954         .set_settings   = et131x_set_settings,
3955         .get_drvinfo    = et131x_get_drvinfo,
3956         .get_regs_len   = et131x_get_regs_len,
3957         .get_regs       = et131x_get_regs,
3958         .get_link = ethtool_op_get_link,
3959 };
3960
3961 void et131x_set_ethtool_ops(struct net_device *netdev)
3962 {
3963         SET_ETHTOOL_OPS(netdev, &et131x_ethtool_ops);
3964 }
3965
3966 /* PCI functions */
3967
3968 /**
3969  * et131x_hwaddr_init - set up the MAC Address on the ET1310
3970  * @adapter: pointer to our private adapter structure
3971  */
3972 void et131x_hwaddr_init(struct et131x_adapter *adapter)
3973 {
3974         /* If have our default mac from init and no mac address from
3975          * EEPROM then we need to generate the last octet and set it on the
3976          * device
3977          */
3978         if (adapter->rom_addr[0] == 0x00 &&
3979             adapter->rom_addr[1] == 0x00 &&
3980             adapter->rom_addr[2] == 0x00 &&
3981             adapter->rom_addr[3] == 0x00 &&
3982             adapter->rom_addr[4] == 0x00 &&
3983             adapter->rom_addr[5] == 0x00) {
3984                 /*
3985                  * We need to randomly generate the last octet so we
3986                  * decrease our chances of setting the mac address to
3987                  * same as another one of our cards in the system
3988                  */
3989                 get_random_bytes(&adapter->addr[5], 1);
3990                 /*
3991                  * We have the default value in the register we are
3992                  * working with so we need to copy the current
3993                  * address into the permanent address
3994                  */
3995                 memcpy(adapter->rom_addr,
3996                         adapter->addr, ETH_ALEN);
3997         } else {
3998                 /* We do not have an override address, so set the
3999                  * current address to the permanent address and add
4000                  * it to the device
4001                  */
4002                 memcpy(adapter->addr,
4003                        adapter->rom_addr, ETH_ALEN);
4004         }
4005 }
4006
4007 /**
4008  * et131x_pci_init       - initial PCI setup
4009  * @adapter: pointer to our private adapter structure
4010  * @pdev: our PCI device
4011  *
4012  * Perform the initial setup of PCI registers and if possible initialise
4013  * the MAC address. At this point the I/O registers have yet to be mapped
4014  */
4015 static int et131x_pci_init(struct et131x_adapter *adapter,
4016                                                 struct pci_dev *pdev)
4017 {
4018         int i;
4019         u8 max_payload;
4020         u8 read_size_reg;
4021
4022         if (et131x_init_eeprom(adapter) < 0)
4023                 return -EIO;
4024
4025         /* Let's set up the PORT LOGIC Register.  First we need to know what
4026          * the max_payload_size is
4027          */
4028         if (pci_read_config_byte(pdev, ET1310_PCI_MAX_PYLD, &max_payload)) {
4029                 dev_err(&pdev->dev,
4030                     "Could not read PCI config space for Max Payload Size\n");
4031                 return -EIO;
4032         }
4033
4034         /* Program the Ack/Nak latency and replay timers */
4035         max_payload &= 0x07;    /* Only the lower 3 bits are valid */
4036
4037         if (max_payload < 2) {
4038                 static const u16 acknak[2] = { 0x76, 0xD0 };
4039                 static const u16 replay[2] = { 0x1E0, 0x2ED };
4040
4041                 if (pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
4042                                                acknak[max_payload])) {
4043                         dev_err(&pdev->dev,
4044                           "Could not write PCI config space for ACK/NAK\n");
4045                         return -EIO;
4046                 }
4047                 if (pci_write_config_word(pdev, ET1310_PCI_REPLAY,
4048                                                replay[max_payload])) {
4049                         dev_err(&pdev->dev,
4050                           "Could not write PCI config space for Replay Timer\n");
4051                         return -EIO;
4052                 }
4053         }
4054
4055         /* l0s and l1 latency timers.  We are using default values.
4056          * Representing 001 for L0s and 010 for L1
4057          */
4058         if (pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11)) {
4059                 dev_err(&pdev->dev,
4060                   "Could not write PCI config space for Latency Timers\n");
4061                 return -EIO;
4062         }
4063
4064         /* Change the max read size to 2k */
4065         if (pci_read_config_byte(pdev, 0x51, &read_size_reg)) {
4066                 dev_err(&pdev->dev,
4067                         "Could not read PCI config space for Max read size\n");
4068                 return -EIO;
4069         }
4070
4071         read_size_reg &= 0x8f;
4072         read_size_reg |= 0x40;
4073
4074         if (pci_write_config_byte(pdev, 0x51, read_size_reg)) {
4075                 dev_err(&pdev->dev,
4076                       "Could not write PCI config space for Max read size\n");
4077                 return -EIO;
4078         }
4079
4080         /* Get MAC address from config space if an eeprom exists, otherwise
4081          * the MAC address there will not be valid
4082          */
4083         if (!adapter->has_eeprom) {
4084                 et131x_hwaddr_init(adapter);
4085                 return 0;
4086         }
4087
4088         for (i = 0; i < ETH_ALEN; i++) {
4089                 if (pci_read_config_byte(pdev, ET1310_PCI_MAC_ADDRESS + i,
4090                                         adapter->rom_addr + i)) {
4091                         dev_err(&pdev->dev, "Could not read PCI config space for MAC address\n");
4092                         return -EIO;
4093                 }
4094         }
4095         memcpy(adapter->addr, adapter->rom_addr, ETH_ALEN);
4096         return 0;
4097 }
4098
4099 /**
4100  * et131x_error_timer_handler
4101  * @data: timer-specific variable; here a pointer to our adapter structure
4102  *
4103  * The routine called when the error timer expires, to track the number of
4104  * recurring errors.
4105  */
4106 void et131x_error_timer_handler(unsigned long data)
4107 {
4108         struct et131x_adapter *adapter = (struct et131x_adapter *) data;
4109         struct phy_device *phydev = adapter->phydev;
4110
4111         if (et1310_in_phy_coma(adapter)) {
4112                 /* Bring the device immediately out of coma, to
4113                  * prevent it from sleeping indefinitely, this
4114                  * mechanism could be improved! */
4115                 et1310_disable_phy_coma(adapter);
4116                 adapter->boot_coma = 20;
4117         } else {
4118                 et1310_update_macstat_host_counters(adapter);
4119         }
4120
4121         if (!phydev->link && adapter->boot_coma < 11)
4122                 adapter->boot_coma++;
4123
4124         if (adapter->boot_coma == 10) {
4125                 if (!phydev->link) {
4126                         if (!et1310_in_phy_coma(adapter)) {
4127                                 /* NOTE - This was originally a 'sync with
4128                                  *  interrupt'. How to do that under Linux?
4129                                  */
4130                                 et131x_enable_interrupts(adapter);
4131                                 et1310_enable_phy_coma(adapter);
4132                         }
4133                 }
4134         }
4135
4136         /* This is a periodic timer, so reschedule */
4137         mod_timer(&adapter->error_timer, jiffies +
4138                                           TX_ERROR_PERIOD * HZ / 1000);
4139 }
4140
4141 /**
4142  * et131x_adapter_memory_alloc
4143  * @adapter: pointer to our private adapter structure
4144  *
4145  * Returns 0 on success, errno on failure (as defined in errno.h).
4146  *
4147  * Allocate all the memory blocks for send, receive and others.
4148  */
4149 int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
4150 {
4151         int status;
4152
4153         /* Allocate memory for the Tx Ring */
4154         status = et131x_tx_dma_memory_alloc(adapter);
4155         if (status != 0) {
4156                 dev_err(&adapter->pdev->dev,
4157                           "et131x_tx_dma_memory_alloc FAILED\n");
4158                 return status;
4159         }
4160         /* Receive buffer memory allocation */
4161         status = et131x_rx_dma_memory_alloc(adapter);
4162         if (status != 0) {
4163                 dev_err(&adapter->pdev->dev,
4164                           "et131x_rx_dma_memory_alloc FAILED\n");
4165                 et131x_tx_dma_memory_free(adapter);
4166                 return status;
4167         }
4168
4169         /* Init receive data structures */
4170         status = et131x_init_recv(adapter);
4171         if (status != 0) {
4172                 dev_err(&adapter->pdev->dev,
4173                         "et131x_init_recv FAILED\n");
4174                 et131x_tx_dma_memory_free(adapter);
4175                 et131x_rx_dma_memory_free(adapter);
4176         }
4177         return status;
4178 }
4179
4180 /**
4181  * et131x_adapter_memory_free - Free all memory allocated for use by Tx & Rx
4182  * @adapter: pointer to our private adapter structure
4183  */
4184 void et131x_adapter_memory_free(struct et131x_adapter *adapter)
4185 {
4186         /* Free DMA memory */
4187         et131x_tx_dma_memory_free(adapter);
4188         et131x_rx_dma_memory_free(adapter);
4189 }
4190
4191 static void et131x_adjust_link(struct net_device *netdev)
4192 {
4193         struct et131x_adapter *adapter = netdev_priv(netdev);
4194         struct  phy_device *phydev = adapter->phydev;
4195
4196         if (netif_carrier_ok(netdev)) {
4197                 adapter->boot_coma = 20;
4198
4199                 if (phydev && phydev->speed == SPEED_10) {
4200                         /*
4201                          * NOTE - Is there a way to query this without
4202                          * TruePHY?
4203                          * && TRU_QueryCoreType(adapter->hTruePhy, 0)==
4204                          * EMI_TRUEPHY_A13O) {
4205                          */
4206                         u16 register18;
4207
4208                         et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
4209                                          &register18);
4210                         et131x_mii_write(adapter, PHY_MPHY_CONTROL_REG,
4211                                          register18 | 0x4);
4212                         et131x_mii_write(adapter, PHY_INDEX_REG,
4213                                          register18 | 0x8402);
4214                         et131x_mii_write(adapter, PHY_DATA_REG,
4215                                          register18 | 511);
4216                         et131x_mii_write(adapter, PHY_MPHY_CONTROL_REG,
4217                                          register18);
4218                 }
4219
4220                 et1310_config_flow_control(adapter);
4221
4222                 if (phydev && phydev->speed == SPEED_1000 &&
4223                                 adapter->registry_jumbo_packet > 2048) {
4224                         u16 reg;
4225
4226                         et131x_mii_read(adapter, PHY_CONFIG, &reg);
4227                         reg &= ~ET_PHY_CONFIG_TX_FIFO_DEPTH;
4228                         reg |= ET_PHY_CONFIG_FIFO_DEPTH_32;
4229                         et131x_mii_write(adapter, PHY_CONFIG, reg);
4230                 }
4231
4232                 et131x_set_rx_dma_timer(adapter);
4233                 et1310_config_mac_regs2(adapter);
4234         }
4235
4236         if (phydev && phydev->link != adapter->link) {
4237                 /*
4238                  * Check to see if we are in coma mode and if
4239                  * so, disable it because we will not be able
4240                  * to read PHY values until we are out.
4241                  */
4242                 if (et1310_in_phy_coma(adapter))
4243                         et1310_disable_phy_coma(adapter);
4244
4245                 if (phydev->link) {
4246                         adapter->boot_coma = 20;
4247                 } else {
4248                         dev_warn(&adapter->pdev->dev,
4249                             "Link down - cable problem ?\n");
4250                         adapter->boot_coma = 0;
4251
4252                         if (phydev->speed == SPEED_10) {
4253                                 /* NOTE - Is there a way to query this without
4254                                  * TruePHY?
4255                                  * && TRU_QueryCoreType(adapter->hTruePhy, 0) ==
4256                                  * EMI_TRUEPHY_A13O)
4257                                  */
4258                                 u16 register18;
4259
4260                                 et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
4261                                                  &register18);
4262                                 et131x_mii_write(adapter, PHY_MPHY_CONTROL_REG,
4263                                                  register18 | 0x4);
4264                                 et131x_mii_write(adapter, PHY_INDEX_REG,
4265                                                  register18 | 0x8402);
4266                                 et131x_mii_write(adapter, PHY_DATA_REG,
4267                                                  register18 | 511);
4268                                 et131x_mii_write(adapter, PHY_MPHY_CONTROL_REG,
4269                                                  register18);
4270                         }
4271
4272                         /* Free the packets being actively sent & stopped */
4273                         et131x_free_busy_send_packets(adapter);
4274
4275                         /* Re-initialize the send structures */
4276                         et131x_init_send(adapter);
4277
4278                         /*
4279                          * Bring the device back to the state it was during
4280                          * init prior to autonegotiation being complete. This
4281                          * way, when we get the auto-neg complete interrupt,
4282                          * we can complete init by calling config_mac_regs2.
4283                          */
4284                         et131x_soft_reset(adapter);
4285
4286                         /* Setup ET1310 as per the documentation */
4287                         et131x_adapter_setup(adapter);
4288
4289                         /* perform reset of tx/rx */
4290                         et131x_disable_txrx(netdev);
4291                         et131x_enable_txrx(netdev);
4292                 }
4293
4294                 adapter->link = phydev->link;
4295
4296                 phy_print_status(phydev);
4297         }
4298 }
4299
4300 static int et131x_mii_probe(struct net_device *netdev)
4301 {
4302         struct et131x_adapter *adapter = netdev_priv(netdev);
4303         struct  phy_device *phydev = NULL;
4304
4305         phydev = phy_find_first(adapter->mii_bus);
4306         if (!phydev) {
4307                 dev_err(&adapter->pdev->dev, "no PHY found\n");
4308                 return -ENODEV;
4309         }
4310
4311         phydev = phy_connect(netdev, dev_name(&phydev->dev),
4312                         &et131x_adjust_link, 0, PHY_INTERFACE_MODE_MII);
4313
4314         if (IS_ERR(phydev)) {
4315                 dev_err(&adapter->pdev->dev, "Could not attach to PHY\n");
4316                 return PTR_ERR(phydev);
4317         }
4318
4319         phydev->supported &= (SUPPORTED_10baseT_Half
4320                                 | SUPPORTED_10baseT_Full
4321                                 | SUPPORTED_100baseT_Half
4322                                 | SUPPORTED_100baseT_Full
4323                                 | SUPPORTED_Autoneg
4324                                 | SUPPORTED_MII
4325                                 | SUPPORTED_TP);
4326
4327         if (adapter->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
4328                 phydev->supported |= SUPPORTED_1000baseT_Full;
4329
4330         phydev->advertising = phydev->supported;
4331         adapter->phydev = phydev;
4332
4333         dev_info(&adapter->pdev->dev, "attached PHY driver [%s] "
4334                  "(mii_bus:phy_addr=%s)\n",
4335                  phydev->drv->name, dev_name(&phydev->dev));
4336
4337         return 0;
4338 }
4339
4340 /**
4341  * et131x_adapter_init
4342  * @adapter: pointer to the private adapter struct
4343  * @pdev: pointer to the PCI device
4344  *
4345  * Initialize the data structures for the et131x_adapter object and link
4346  * them together with the platform provided device structures.
4347  */
4348 static struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
4349                 struct pci_dev *pdev)
4350 {
4351         static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
4352
4353         struct et131x_adapter *adapter;
4354
4355         /* Allocate private adapter struct and copy in relevant information */
4356         adapter = netdev_priv(netdev);
4357         adapter->pdev = pci_dev_get(pdev);
4358         adapter->netdev = netdev;
4359
4360         /* Do the same for the netdev struct */
4361         netdev->irq = pdev->irq;
4362         netdev->base_addr = pci_resource_start(pdev, 0);
4363
4364         /* Initialize spinlocks here */
4365         spin_lock_init(&adapter->lock);
4366         spin_lock_init(&adapter->tcb_send_qlock);
4367         spin_lock_init(&adapter->tcb_ready_qlock);
4368         spin_lock_init(&adapter->send_hw_lock);
4369         spin_lock_init(&adapter->rcv_lock);
4370         spin_lock_init(&adapter->rcv_pend_lock);
4371         spin_lock_init(&adapter->fbr_lock);
4372         spin_lock_init(&adapter->phy_lock);
4373
4374         adapter->registry_jumbo_packet = 1514;  /* 1514-9216 */
4375
4376         /* Set the MAC address to a default */
4377         memcpy(adapter->addr, default_mac, ETH_ALEN);
4378
4379         return adapter;
4380 }
4381
4382 /**
4383  * et131x_pci_remove
4384  * @pdev: a pointer to the device's pci_dev structure
4385  *
4386  * Registered in the pci_driver structure, this function is called when the
4387  * PCI subsystem detects that a PCI device which matches the information
4388  * contained in the pci_device_id table has been removed.
4389  */
4390 static void __devexit et131x_pci_remove(struct pci_dev *pdev)
4391 {
4392         struct net_device *netdev = pci_get_drvdata(pdev);
4393         struct et131x_adapter *adapter = netdev_priv(netdev);
4394
4395         unregister_netdev(netdev);
4396         mdiobus_unregister(adapter->mii_bus);
4397         kfree(adapter->mii_bus->irq);
4398         mdiobus_free(adapter->mii_bus);
4399
4400         et131x_adapter_memory_free(adapter);
4401         iounmap(adapter->regs);
4402         pci_dev_put(pdev);
4403
4404         free_netdev(netdev);
4405         pci_release_regions(pdev);
4406         pci_disable_device(pdev);
4407 }
4408
4409 /**
4410  * et131x_up - Bring up a device for use.
4411  * @netdev: device to be opened
4412  */
4413 void et131x_up(struct net_device *netdev)
4414 {
4415         struct et131x_adapter *adapter = netdev_priv(netdev);
4416
4417         et131x_enable_txrx(netdev);
4418         phy_start(adapter->phydev);
4419 }
4420
4421 /**
4422  * et131x_down - Bring down the device
4423  * @netdev: device to be broght down
4424  */
4425 void et131x_down(struct net_device *netdev)
4426 {
4427         struct et131x_adapter *adapter = netdev_priv(netdev);
4428
4429         /* Save the timestamp for the TX watchdog, prevent a timeout */
4430         netdev->trans_start = jiffies;
4431
4432         phy_stop(adapter->phydev);
4433         et131x_disable_txrx(netdev);
4434 }
4435
4436 #ifdef CONFIG_PM_SLEEP
4437 static int et131x_suspend(struct device *dev)
4438 {
4439         struct pci_dev *pdev = to_pci_dev(dev);
4440         struct net_device *netdev = pci_get_drvdata(pdev);
4441
4442         if (netif_running(netdev)) {
4443                 netif_device_detach(netdev);
4444                 et131x_down(netdev);
4445                 pci_save_state(pdev);
4446         }
4447
4448         return 0;
4449 }
4450
4451 static int et131x_resume(struct device *dev)
4452 {
4453         struct pci_dev *pdev = to_pci_dev(dev);
4454         struct net_device *netdev = pci_get_drvdata(pdev);
4455
4456         if (netif_running(netdev)) {
4457                 pci_restore_state(pdev);
4458                 et131x_up(netdev);
4459                 netif_device_attach(netdev);
4460         }
4461
4462         return 0;
4463 }
4464
4465 /* ISR functions */
4466
4467 /**
4468  * et131x_isr - The Interrupt Service Routine for the driver.
4469  * @irq: the IRQ on which the interrupt was received.
4470  * @dev_id: device-specific info (here a pointer to a net_device struct)
4471  *
4472  * Returns a value indicating if the interrupt was handled.
4473  */
4474 irqreturn_t et131x_isr(int irq, void *dev_id)
4475 {
4476         bool handled = true;
4477         struct net_device *netdev = (struct net_device *)dev_id;
4478         struct et131x_adapter *adapter = NULL;
4479         u32 status;
4480
4481         if (!netif_device_present(netdev)) {
4482                 handled = false;
4483                 goto out;
4484         }
4485
4486         adapter = netdev_priv(netdev);
4487
4488         /* If the adapter is in low power state, then it should not
4489          * recognize any interrupt
4490          */
4491
4492         /* Disable Device Interrupts */
4493         et131x_disable_interrupts(adapter);
4494
4495         /* Get a copy of the value in the interrupt status register
4496          * so we can process the interrupting section
4497          */
4498         status = readl(&adapter->regs->global.int_status);
4499
4500         if (adapter->flowcontrol == FLOW_TXONLY ||
4501             adapter->flowcontrol == FLOW_BOTH) {
4502                 status &= ~INT_MASK_ENABLE;
4503         } else {
4504                 status &= ~INT_MASK_ENABLE_NO_FLOW;
4505         }
4506
4507         /* Make sure this is our interrupt */
4508         if (!status) {
4509                 handled = false;
4510                 et131x_enable_interrupts(adapter);
4511                 goto out;
4512         }
4513
4514         /* This is our interrupt, so process accordingly */
4515
4516         if (status & ET_INTR_WATCHDOG) {
4517                 struct tcb *tcb = adapter->tx_ring.send_head;
4518
4519                 if (tcb)
4520                         if (++tcb->stale > 1)
4521                                 status |= ET_INTR_TXDMA_ISR;
4522
4523                 if (adapter->rx_ring.unfinished_receives)
4524                         status |= ET_INTR_RXDMA_XFR_DONE;
4525                 else if (tcb == NULL)
4526                         writel(0, &adapter->regs->global.watchdog_timer);
4527
4528                 status &= ~ET_INTR_WATCHDOG;
4529         }
4530
4531         if (status == 0) {
4532                 /* This interrupt has in some way been "handled" by
4533                  * the ISR. Either it was a spurious Rx interrupt, or
4534                  * it was a Tx interrupt that has been filtered by
4535                  * the ISR.
4536                  */
4537                 et131x_enable_interrupts(adapter);
4538                 goto out;
4539         }
4540
4541         /* We need to save the interrupt status value for use in our
4542          * DPC. We will clear the software copy of that in that
4543          * routine.
4544          */
4545         adapter->stats.interrupt_status = status;
4546
4547         /* Schedule the ISR handler as a bottom-half task in the
4548          * kernel's tq_immediate queue, and mark the queue for
4549          * execution
4550          */
4551         schedule_work(&adapter->task);
4552 out:
4553         return IRQ_RETVAL(handled);
4554 }
4555
4556 /**
4557  * et131x_isr_handler - The ISR handler
4558  * @p_adapter, a pointer to the device's private adapter structure
4559  *
4560  * scheduled to run in a deferred context by the ISR. This is where the ISR's
4561  * work actually gets done.
4562  */
4563 void et131x_isr_handler(struct work_struct *work)
4564 {
4565         struct et131x_adapter *adapter =
4566                 container_of(work, struct et131x_adapter, task);
4567         u32 status = adapter->stats.interrupt_status;
4568         struct address_map __iomem *iomem = adapter->regs;
4569
4570         /*
4571          * These first two are by far the most common.  Once handled, we clear
4572          * their two bits in the status word.  If the word is now zero, we
4573          * exit.
4574          */
4575         /* Handle all the completed Transmit interrupts */
4576         if (status & ET_INTR_TXDMA_ISR)
4577                 et131x_handle_send_interrupt(adapter);
4578
4579         /* Handle all the completed Receives interrupts */
4580         if (status & ET_INTR_RXDMA_XFR_DONE)
4581                 et131x_handle_recv_interrupt(adapter);
4582
4583         status &= 0xffffffd7;
4584
4585         if (status) {
4586                 /* Handle the TXDMA Error interrupt */
4587                 if (status & ET_INTR_TXDMA_ERR) {
4588                         u32 txdma_err;
4589
4590                         /* Following read also clears the register (COR) */
4591                         txdma_err = readl(&iomem->txdma.tx_dma_error);
4592
4593                         dev_warn(&adapter->pdev->dev,
4594                                     "TXDMA_ERR interrupt, error = %d\n",
4595                                     txdma_err);
4596                 }
4597
4598                 /* Handle Free Buffer Ring 0 and 1 Low interrupt */
4599                 if (status &
4600                     (ET_INTR_RXDMA_FB_R0_LOW | ET_INTR_RXDMA_FB_R1_LOW)) {
4601                         /*
4602                          * This indicates the number of unused buffers in
4603                          * RXDMA free buffer ring 0 is <= the limit you
4604                          * programmed. Free buffer resources need to be
4605                          * returned.  Free buffers are consumed as packets
4606                          * are passed from the network to the host. The host
4607                          * becomes aware of the packets from the contents of
4608                          * the packet status ring. This ring is queried when
4609                          * the packet done interrupt occurs. Packets are then
4610                          * passed to the OS. When the OS is done with the
4611                          * packets the resources can be returned to the
4612                          * ET1310 for re-use. This interrupt is one method of
4613                          * returning resources.
4614                          */
4615
4616                         /* If the user has flow control on, then we will
4617                          * send a pause packet, otherwise just exit
4618                          */
4619                         if (adapter->flowcontrol == FLOW_TXONLY ||
4620                             adapter->flowcontrol == FLOW_BOTH) {
4621                                 u32 pm_csr;
4622
4623                                 /* Tell the device to send a pause packet via
4624                                  * the back pressure register (bp req  and
4625                                  * bp xon/xoff)
4626                                  */
4627                                 pm_csr = readl(&iomem->global.pm_csr);
4628                                 if (!et1310_in_phy_coma(adapter))
4629                                         writel(3, &iomem->txmac.bp_ctrl);
4630                         }
4631                 }
4632
4633                 /* Handle Packet Status Ring Low Interrupt */
4634                 if (status & ET_INTR_RXDMA_STAT_LOW) {
4635
4636                         /*
4637                          * Same idea as with the two Free Buffer Rings.
4638                          * Packets going from the network to the host each
4639                          * consume a free buffer resource and a packet status
4640                          * resource.  These resoures are passed to the OS.
4641                          * When the OS is done with the resources, they need
4642                          * to be returned to the ET1310. This is one method
4643                          * of returning the resources.
4644                          */
4645                 }
4646
4647                 /* Handle RXDMA Error Interrupt */
4648                 if (status & ET_INTR_RXDMA_ERR) {
4649                         /*
4650                          * The rxdma_error interrupt is sent when a time-out
4651                          * on a request issued by the JAGCore has occurred or
4652                          * a completion is returned with an un-successful
4653                          * status.  In both cases the request is considered
4654                          * complete. The JAGCore will automatically re-try the
4655                          * request in question. Normally information on events
4656                          * like these are sent to the host using the "Advanced
4657                          * Error Reporting" capability. This interrupt is
4658                          * another way of getting similar information. The
4659                          * only thing required is to clear the interrupt by
4660                          * reading the ISR in the global resources. The
4661                          * JAGCore will do a re-try on the request.  Normally
4662                          * you should never see this interrupt. If you start
4663                          * to see this interrupt occurring frequently then
4664                          * something bad has occurred. A reset might be the
4665                          * thing to do.
4666                          */
4667                         /* TRAP();*/
4668
4669                         dev_warn(&adapter->pdev->dev,
4670                                     "RxDMA_ERR interrupt, error %x\n",
4671                                     readl(&iomem->txmac.tx_test));
4672                 }
4673
4674                 /* Handle the Wake on LAN Event */
4675                 if (status & ET_INTR_WOL) {
4676                         /*
4677                          * This is a secondary interrupt for wake on LAN.
4678                          * The driver should never see this, if it does,
4679                          * something serious is wrong. We will TRAP the
4680                          * message when we are in DBG mode, otherwise we
4681                          * will ignore it.
4682                          */
4683                         dev_err(&adapter->pdev->dev, "WAKE_ON_LAN interrupt\n");
4684                 }
4685
4686                 /* Let's move on to the TxMac */
4687                 if (status & ET_INTR_TXMAC) {
4688                         u32 err = readl(&iomem->txmac.err);
4689
4690                         /*
4691                          * When any of the errors occur and TXMAC generates
4692                          * an interrupt to report these errors, it usually
4693                          * means that TXMAC has detected an error in the data
4694                          * stream retrieved from the on-chip Tx Q. All of
4695                          * these errors are catastrophic and TXMAC won't be
4696                          * able to recover data when these errors occur.  In
4697                          * a nutshell, the whole Tx path will have to be reset
4698                          * and re-configured afterwards.
4699                          */
4700                         dev_warn(&adapter->pdev->dev,
4701                                     "TXMAC interrupt, error 0x%08x\n",
4702                                     err);
4703
4704                         /* If we are debugging, we want to see this error,
4705                          * otherwise we just want the device to be reset and
4706                          * continue
4707                          */
4708                 }
4709
4710                 /* Handle RXMAC Interrupt */
4711                 if (status & ET_INTR_RXMAC) {
4712                         /*
4713                          * These interrupts are catastrophic to the device,
4714                          * what we need to do is disable the interrupts and
4715                          * set the flag to cause us to reset so we can solve
4716                          * this issue.
4717                          */
4718                         /* MP_SET_FLAG( adapter,
4719                                                 fMP_ADAPTER_HARDWARE_ERROR); */
4720
4721                         dev_warn(&adapter->pdev->dev,
4722                           "RXMAC interrupt, error 0x%08x.  Requesting reset\n",
4723                                     readl(&iomem->rxmac.err_reg));
4724
4725                         dev_warn(&adapter->pdev->dev,
4726                                     "Enable 0x%08x, Diag 0x%08x\n",
4727                                     readl(&iomem->rxmac.ctrl),
4728                                     readl(&iomem->rxmac.rxq_diag));
4729
4730                         /*
4731                          * If we are debugging, we want to see this error,
4732                          * otherwise we just want the device to be reset and
4733                          * continue
4734                          */
4735                 }
4736
4737                 /* Handle MAC_STAT Interrupt */
4738                 if (status & ET_INTR_MAC_STAT) {
4739                         /*
4740                          * This means at least one of the un-masked counters
4741                          * in the MAC_STAT block has rolled over.  Use this
4742                          * to maintain the top, software managed bits of the
4743                          * counter(s).
4744                          */
4745                         et1310_handle_macstat_interrupt(adapter);
4746                 }
4747
4748                 /* Handle SLV Timeout Interrupt */
4749                 if (status & ET_INTR_SLV_TIMEOUT) {
4750                         /*
4751                          * This means a timeout has occurred on a read or
4752                          * write request to one of the JAGCore registers. The
4753                          * Global Resources block has terminated the request
4754                          * and on a read request, returned a "fake" value.
4755                          * The most likely reasons are: Bad Address or the
4756                          * addressed module is in a power-down state and
4757                          * can't respond.
4758                          */
4759                 }
4760         }
4761         et131x_enable_interrupts(adapter);
4762 }
4763
4764 /* NETDEV functions */
4765
4766 /**
4767  * et131x_stats - Return the current device statistics.
4768  * @netdev: device whose stats are being queried
4769  *
4770  * Returns 0 on success, errno on failure (as defined in errno.h)
4771  */
4772 static struct net_device_stats *et131x_stats(struct net_device *netdev)
4773 {
4774         struct et131x_adapter *adapter = netdev_priv(netdev);
4775         struct net_device_stats *stats = &adapter->net_stats;
4776         struct ce_stats *devstat = &adapter->stats;
4777
4778         stats->rx_errors = devstat->rx_length_errs +
4779                            devstat->rx_align_errs +
4780                            devstat->rx_crc_errs +
4781                            devstat->rx_code_violations +
4782                            devstat->rx_other_errs;
4783         stats->tx_errors = devstat->tx_max_pkt_errs;
4784         stats->multicast = devstat->multicast_pkts_rcvd;
4785         stats->collisions = devstat->tx_collisions;
4786
4787         stats->rx_length_errors = devstat->rx_length_errs;
4788         stats->rx_over_errors = devstat->rx_overflows;
4789         stats->rx_crc_errors = devstat->rx_crc_errs;
4790
4791         /* NOTE: These stats don't have corresponding values in CE_STATS,
4792          * so we're going to have to update these directly from within the
4793          * TX/RX code
4794          */
4795         /* stats->rx_bytes            = 20; devstat->; */
4796         /* stats->tx_bytes            = 20;  devstat->; */
4797         /* stats->rx_dropped          = devstat->; */
4798         /* stats->tx_dropped          = devstat->; */
4799
4800         /*  NOTE: Not used, can't find analogous statistics */
4801         /* stats->rx_frame_errors     = devstat->; */
4802         /* stats->rx_fifo_errors      = devstat->; */
4803         /* stats->rx_missed_errors    = devstat->; */
4804
4805         /* stats->tx_aborted_errors   = devstat->; */
4806         /* stats->tx_carrier_errors   = devstat->; */
4807         /* stats->tx_fifo_errors      = devstat->; */
4808         /* stats->tx_heartbeat_errors = devstat->; */
4809         /* stats->tx_window_errors    = devstat->; */
4810         return stats;
4811 }
4812
4813 /**
4814  * et131x_open - Open the device for use.
4815  * @netdev: device to be opened
4816  *
4817  * Returns 0 on success, errno on failure (as defined in errno.h)
4818  */
4819 int et131x_open(struct net_device *netdev)
4820 {
4821         int result = 0;
4822         struct et131x_adapter *adapter = netdev_priv(netdev);
4823
4824         /* Start the timer to track NIC errors */
4825         init_timer(&adapter->error_timer);
4826         adapter->error_timer.expires = jiffies + TX_ERROR_PERIOD * HZ / 1000;
4827         adapter->error_timer.function = et131x_error_timer_handler;
4828         adapter->error_timer.data = (unsigned long)adapter;
4829         add_timer(&adapter->error_timer);
4830
4831         /* Register our IRQ */
4832         result = request_irq(netdev->irq, et131x_isr, IRQF_SHARED,
4833                                         netdev->name, netdev);
4834         if (result) {
4835                 dev_err(&adapter->pdev->dev, "could not register IRQ %d\n",
4836                         netdev->irq);
4837                 return result;
4838         }
4839
4840         adapter->flags |= fMP_ADAPTER_INTERRUPT_IN_USE;
4841
4842         et131x_up(netdev);
4843
4844         return result;
4845 }
4846
4847 /**
4848  * et131x_close - Close the device
4849  * @netdev: device to be closed
4850  *
4851  * Returns 0 on success, errno on failure (as defined in errno.h)
4852  */
4853 int et131x_close(struct net_device *netdev)
4854 {
4855         struct et131x_adapter *adapter = netdev_priv(netdev);
4856
4857         et131x_down(netdev);
4858
4859         adapter->flags &= ~fMP_ADAPTER_INTERRUPT_IN_USE;
4860         free_irq(netdev->irq, netdev);
4861
4862         /* Stop the error timer */
4863         return del_timer_sync(&adapter->error_timer);
4864 }
4865
4866 /**
4867  * et131x_ioctl - The I/O Control handler for the driver
4868  * @netdev: device on which the control request is being made
4869  * @reqbuf: a pointer to the IOCTL request buffer
4870  * @cmd: the IOCTL command code
4871  *
4872  * Returns 0 on success, errno on failure (as defined in errno.h)
4873  */
4874 static int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf, int cmd)
4875 {
4876         struct et131x_adapter *adapter = netdev_priv(netdev);
4877
4878         if (!adapter->phydev)
4879                 return -EINVAL;
4880
4881         return phy_mii_ioctl(adapter->phydev, reqbuf, cmd);
4882 }
4883
4884 /**
4885  * et131x_set_packet_filter - Configures the Rx Packet filtering on the device
4886  * @adapter: pointer to our private adapter structure
4887  *
4888  * FIXME: lot of dups with MAC code
4889  *
4890  * Returns 0 on success, errno on failure
4891  */
4892 static int et131x_set_packet_filter(struct et131x_adapter *adapter)
4893 {
4894         int status = 0;
4895         uint32_t filter = adapter->packet_filter;
4896         u32 ctrl;
4897         u32 pf_ctrl;
4898
4899         ctrl = readl(&adapter->regs->rxmac.ctrl);
4900         pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
4901
4902         /* Default to disabled packet filtering.  Enable it in the individual
4903          * case statements that require the device to filter something
4904          */
4905         ctrl |= 0x04;
4906
4907         /* Set us to be in promiscuous mode so we receive everything, this
4908          * is also true when we get a packet filter of 0
4909          */
4910         if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
4911                 pf_ctrl &= ~7;  /* Clear filter bits */
4912         else {
4913                 /*
4914                  * Set us up with Multicast packet filtering.  Three cases are
4915                  * possible - (1) we have a multi-cast list, (2) we receive ALL
4916                  * multicast entries or (3) we receive none.
4917                  */
4918                 if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
4919                         pf_ctrl &= ~2;  /* Multicast filter bit */
4920                 else {
4921                         et1310_setup_device_for_multicast(adapter);
4922                         pf_ctrl |= 2;
4923                         ctrl &= ~0x04;
4924                 }
4925
4926                 /* Set us up with Unicast packet filtering */
4927                 if (filter & ET131X_PACKET_TYPE_DIRECTED) {
4928                         et1310_setup_device_for_unicast(adapter);
4929                         pf_ctrl |= 4;
4930                         ctrl &= ~0x04;
4931                 }
4932
4933                 /* Set us up with Broadcast packet filtering */
4934                 if (filter & ET131X_PACKET_TYPE_BROADCAST) {
4935                         pf_ctrl |= 1;   /* Broadcast filter bit */
4936                         ctrl &= ~0x04;
4937                 } else
4938                         pf_ctrl &= ~1;
4939
4940                 /* Setup the receive mac configuration registers - Packet
4941                  * Filter control + the enable / disable for packet filter
4942                  * in the control reg.
4943                  */
4944                 writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
4945                 writel(ctrl, &adapter->regs->rxmac.ctrl);
4946         }
4947         return status;
4948 }
4949
4950 /**
4951  * et131x_multicast - The handler to configure multicasting on the interface
4952  * @netdev: a pointer to a net_device struct representing the device
4953  */
4954 static void et131x_multicast(struct net_device *netdev)
4955 {
4956         struct et131x_adapter *adapter = netdev_priv(netdev);
4957         uint32_t packet_filter = 0;
4958         unsigned long flags;
4959         struct netdev_hw_addr *ha;
4960         int i;
4961
4962         spin_lock_irqsave(&adapter->lock, flags);
4963
4964         /* Before we modify the platform-independent filter flags, store them
4965          * locally. This allows us to determine if anything's changed and if
4966          * we even need to bother the hardware
4967          */
4968         packet_filter = adapter->packet_filter;
4969
4970         /* Clear the 'multicast' flag locally; because we only have a single
4971          * flag to check multicast, and multiple multicast addresses can be
4972          * set, this is the easiest way to determine if more than one
4973          * multicast address is being set.
4974          */
4975         packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
4976
4977         /* Check the net_device flags and set the device independent flags
4978          * accordingly
4979          */
4980
4981         if (netdev->flags & IFF_PROMISC)
4982                 adapter->packet_filter |= ET131X_PACKET_TYPE_PROMISCUOUS;
4983         else
4984                 adapter->packet_filter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
4985
4986         if (netdev->flags & IFF_ALLMULTI)
4987                 adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
4988
4989         if (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST)
4990                 adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
4991
4992         if (netdev_mc_count(netdev) < 1) {
4993                 adapter->packet_filter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
4994                 adapter->packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
4995         } else
4996                 adapter->packet_filter |= ET131X_PACKET_TYPE_MULTICAST;
4997
4998         /* Set values in the private adapter struct */
4999         i = 0;
5000         netdev_for_each_mc_addr(ha, netdev) {
5001                 if (i == NIC_MAX_MCAST_LIST)
5002                         break;
5003                 memcpy(adapter->multicast_list[i++], ha->addr, ETH_ALEN);
5004         }
5005         adapter->multicast_addr_count = i;
5006
5007         /* Are the new flags different from the previous ones? If not, then no
5008          * action is required
5009          *
5010          * NOTE - This block will always update the multicast_list with the
5011          *        hardware, even if the addresses aren't the same.
5012          */
5013         if (packet_filter != adapter->packet_filter) {
5014                 /* Call the device's filter function */
5015                 et131x_set_packet_filter(adapter);
5016         }
5017         spin_unlock_irqrestore(&adapter->lock, flags);
5018 }
5019
5020 /**
5021  * et131x_tx - The handler to tx a packet on the device
5022  * @skb: data to be Tx'd
5023  * @netdev: device on which data is to be Tx'd
5024  *
5025  * Returns 0 on success, errno on failure (as defined in errno.h)
5026  */
5027 static int et131x_tx(struct sk_buff *skb, struct net_device *netdev)
5028 {
5029         int status = 0;
5030         struct et131x_adapter *adapter = netdev_priv(netdev);
5031
5032         /* stop the queue if it's getting full */
5033         if(adapter->tx_ring.used >= NUM_TCB - 1 && !netif_queue_stopped(netdev))
5034                 netif_stop_queue(netdev);
5035
5036         /* Save the timestamp for the TX timeout watchdog */
5037         netdev->trans_start = jiffies;
5038
5039         /* Call the device-specific data Tx routine */
5040         status = et131x_send_packets(skb, netdev);
5041
5042         /* Check status and manage the netif queue if necessary */
5043         if (status != 0) {
5044                 if (status == -ENOMEM) {
5045                         status = NETDEV_TX_BUSY;
5046                 } else {
5047                         status = NETDEV_TX_OK;
5048                 }
5049         }
5050         return status;
5051 }
5052
5053 /**
5054  * et131x_tx_timeout - Timeout handler
5055  * @netdev: a pointer to a net_device struct representing the device
5056  *
5057  * The handler called when a Tx request times out. The timeout period is
5058  * specified by the 'tx_timeo" element in the net_device structure (see
5059  * et131x_alloc_device() to see how this value is set).
5060  */
5061 static void et131x_tx_timeout(struct net_device *netdev)
5062 {
5063         struct et131x_adapter *adapter = netdev_priv(netdev);
5064         struct tcb *tcb;
5065         unsigned long flags;
5066
5067         /* If the device is closed, ignore the timeout */
5068         if (~(adapter->flags & fMP_ADAPTER_INTERRUPT_IN_USE))
5069                 return;
5070
5071         /* Any nonrecoverable hardware error?
5072          * Checks adapter->flags for any failure in phy reading
5073          */
5074         if (adapter->flags & fMP_ADAPTER_NON_RECOVER_ERROR)
5075                 return;
5076
5077         /* Hardware failure? */
5078         if (adapter->flags & fMP_ADAPTER_HARDWARE_ERROR) {
5079                 dev_err(&adapter->pdev->dev, "hardware error - reset\n");
5080                 return;
5081         }
5082
5083         /* Is send stuck? */
5084         spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
5085
5086         tcb = adapter->tx_ring.send_head;
5087
5088         if (tcb != NULL) {
5089                 tcb->count++;
5090
5091                 if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
5092                         spin_unlock_irqrestore(&adapter->tcb_send_qlock,
5093                                                flags);
5094
5095                         dev_warn(&adapter->pdev->dev,
5096                                 "Send stuck - reset.  tcb->WrIndex %x, flags 0x%08x\n",
5097                                 tcb->index,
5098                                 tcb->flags);
5099
5100                         adapter->net_stats.tx_errors++;
5101
5102                         /* perform reset of tx/rx */
5103                         et131x_disable_txrx(netdev);
5104                         et131x_enable_txrx(netdev);
5105                         return;
5106                 }
5107         }
5108
5109         spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
5110 }
5111
5112 /**
5113  * et131x_change_mtu - The handler called to change the MTU for the device
5114  * @netdev: device whose MTU is to be changed
5115  * @new_mtu: the desired MTU
5116  *
5117  * Returns 0 on success, errno on failure (as defined in errno.h)
5118  */
5119 static int et131x_change_mtu(struct net_device *netdev, int new_mtu)
5120 {
5121         int result = 0;
5122         struct et131x_adapter *adapter = netdev_priv(netdev);
5123
5124         /* Make sure the requested MTU is valid */
5125         if (new_mtu < 64 || new_mtu > 9216)
5126                 return -EINVAL;
5127
5128         et131x_disable_txrx(netdev);
5129         et131x_handle_send_interrupt(adapter);
5130         et131x_handle_recv_interrupt(adapter);
5131
5132         /* Set the new MTU */
5133         netdev->mtu = new_mtu;
5134
5135         /* Free Rx DMA memory */
5136         et131x_adapter_memory_free(adapter);
5137
5138         /* Set the config parameter for Jumbo Packet support */
5139         adapter->registry_jumbo_packet = new_mtu + 14;
5140         et131x_soft_reset(adapter);
5141
5142         /* Alloc and init Rx DMA memory */
5143         result = et131x_adapter_memory_alloc(adapter);
5144         if (result != 0) {
5145                 dev_warn(&adapter->pdev->dev,
5146                         "Change MTU failed; couldn't re-alloc DMA memory\n");
5147                 return result;
5148         }
5149
5150         et131x_init_send(adapter);
5151
5152         et131x_hwaddr_init(adapter);
5153         memcpy(netdev->dev_addr, adapter->addr, ETH_ALEN);
5154
5155         /* Init the device with the new settings */
5156         et131x_adapter_setup(adapter);
5157
5158         et131x_enable_txrx(netdev);
5159
5160         return result;
5161 }
5162
5163 /**
5164  * et131x_set_mac_addr - handler to change the MAC address for the device
5165  * @netdev: device whose MAC is to be changed
5166  * @new_mac: the desired MAC address
5167  *
5168  * Returns 0 on success, errno on failure (as defined in errno.h)
5169  *
5170  * IMPLEMENTED BY : blux http://berndlux.de 22.01.2007 21:14
5171  */
5172 static int et131x_set_mac_addr(struct net_device *netdev, void *new_mac)
5173 {
5174         int result = 0;
5175         struct et131x_adapter *adapter = netdev_priv(netdev);
5176         struct sockaddr *address = new_mac;
5177
5178         /* begin blux */
5179
5180         if (adapter == NULL)
5181                 return -ENODEV;
5182
5183         /* Make sure the requested MAC is valid */
5184         if (!is_valid_ether_addr(address->sa_data))
5185                 return -EINVAL;
5186
5187         et131x_disable_txrx(netdev);
5188         et131x_handle_send_interrupt(adapter);
5189         et131x_handle_recv_interrupt(adapter);
5190
5191         /* Set the new MAC */
5192         /* netdev->set_mac_address  = &new_mac; */
5193
5194         memcpy(netdev->dev_addr, address->sa_data, netdev->addr_len);
5195
5196         printk(KERN_INFO "%s: Setting MAC address to %pM\n",
5197                         netdev->name, netdev->dev_addr);
5198
5199         /* Free Rx DMA memory */
5200         et131x_adapter_memory_free(adapter);
5201
5202         et131x_soft_reset(adapter);
5203
5204         /* Alloc and init Rx DMA memory */
5205         result = et131x_adapter_memory_alloc(adapter);
5206         if (result != 0) {
5207                 dev_err(&adapter->pdev->dev,
5208                         "Change MAC failed; couldn't re-alloc DMA memory\n");
5209                 return result;
5210         }
5211
5212         et131x_init_send(adapter);
5213
5214         et131x_hwaddr_init(adapter);
5215
5216         /* Init the device with the new settings */
5217         et131x_adapter_setup(adapter);
5218
5219         et131x_enable_txrx(netdev);
5220
5221         return result;
5222 }
5223
5224 static const struct net_device_ops et131x_netdev_ops = {
5225         .ndo_open               = et131x_open,
5226         .ndo_stop               = et131x_close,
5227         .ndo_start_xmit         = et131x_tx,
5228         .ndo_set_multicast_list = et131x_multicast,
5229         .ndo_tx_timeout         = et131x_tx_timeout,
5230         .ndo_change_mtu         = et131x_change_mtu,
5231         .ndo_set_mac_address    = et131x_set_mac_addr,
5232         .ndo_validate_addr      = eth_validate_addr,
5233         .ndo_get_stats          = et131x_stats,
5234         .ndo_do_ioctl           = et131x_ioctl,
5235 };
5236
5237 /**
5238  * et131x_device_alloc
5239  *
5240  * Returns pointer to the allocated and initialized net_device struct for
5241  * this device.
5242  *
5243  * Create instances of net_device and wl_private for the new adapter and
5244  * register the device's entry points in the net_device structure.
5245  */
5246 struct net_device *et131x_device_alloc(void)
5247 {
5248         struct net_device *netdev;
5249
5250         /* Alloc net_device and adapter structs */
5251         netdev = alloc_etherdev(sizeof(struct et131x_adapter));
5252
5253         if (!netdev) {
5254                 printk(KERN_ERR "et131x: Alloc of net_device struct failed\n");
5255                 return NULL;
5256         }
5257
5258         /*
5259          * Setup the function registration table (and other data) for a
5260          * net_device
5261          */
5262         netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
5263         netdev->netdev_ops     = &et131x_netdev_ops;
5264
5265         /* Poll? */
5266         /* netdev->poll               = &et131x_poll; */
5267         /* netdev->poll_controller    = &et131x_poll_controller; */
5268         return netdev;
5269 }
5270
5271 /**
5272  * et131x_pci_setup - Perform device initialization
5273  * @pdev: a pointer to the device's pci_dev structure
5274  * @ent: this device's entry in the pci_device_id table
5275  *
5276  * Returns 0 on success, errno on failure (as defined in errno.h)
5277  *
5278  * Registered in the pci_driver structure, this function is called when the
5279  * PCI subsystem finds a new PCI device which matches the information
5280  * contained in the pci_device_id table. This routine is the equivalent to
5281  * a device insertion routine.
5282  */
5283 static int __devinit et131x_pci_setup(struct pci_dev *pdev,
5284                                const struct pci_device_id *ent)
5285 {
5286         int result;
5287         struct net_device *netdev;
5288         struct et131x_adapter *adapter;
5289         int ii;
5290
5291         result = pci_enable_device(pdev);
5292         if (result) {
5293                 dev_err(&pdev->dev, "pci_enable_device() failed\n");
5294                 goto err_out;
5295         }
5296
5297         /* Perform some basic PCI checks */
5298         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
5299                 dev_err(&pdev->dev, "Can't find PCI device's base address\n");
5300                 goto err_disable;
5301         }
5302
5303         if (pci_request_regions(pdev, DRIVER_NAME)) {
5304                 dev_err(&pdev->dev, "Can't get PCI resources\n");
5305                 goto err_disable;
5306         }
5307
5308         pci_set_master(pdev);
5309
5310         /* Check the DMA addressing support of this device */
5311         if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
5312                 result = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
5313                 if (result) {
5314                         dev_err(&pdev->dev,
5315                           "Unable to obtain 64 bit DMA for consistent allocations\n");
5316                         goto err_release_res;
5317                 }
5318         } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
5319                 result = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
5320                 if (result) {
5321                         dev_err(&pdev->dev,
5322                           "Unable to obtain 32 bit DMA for consistent allocations\n");
5323                         goto err_release_res;
5324                 }
5325         } else {
5326                 dev_err(&pdev->dev, "No usable DMA addressing method\n");
5327                 result = -EIO;
5328                 goto err_release_res;
5329         }
5330
5331         /* Allocate netdev and private adapter structs */
5332         netdev = et131x_device_alloc();
5333         if (!netdev) {
5334                 dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
5335                 result = -ENOMEM;
5336                 goto err_release_res;
5337         }
5338
5339         SET_NETDEV_DEV(netdev, &pdev->dev);
5340         et131x_set_ethtool_ops(netdev);
5341
5342         adapter = et131x_adapter_init(netdev, pdev);
5343
5344         /* Initialise the PCI setup for the device */
5345         et131x_pci_init(adapter, pdev);
5346
5347         /* Map the bus-relative registers to system virtual memory */
5348         adapter->regs = pci_ioremap_bar(pdev, 0);
5349         if (!adapter->regs) {
5350                 dev_err(&pdev->dev, "Cannot map device registers\n");
5351                 result = -ENOMEM;
5352                 goto err_free_dev;
5353         }
5354
5355         /* If Phy COMA mode was enabled when we went down, disable it here. */
5356         writel(ET_PMCSR_INIT,  &adapter->regs->global.pm_csr);
5357
5358         /* Issue a global reset to the et1310 */
5359         et131x_soft_reset(adapter);
5360
5361         /* Disable all interrupts (paranoid) */
5362         et131x_disable_interrupts(adapter);
5363
5364         /* Allocate DMA memory */
5365         result = et131x_adapter_memory_alloc(adapter);
5366         if (result) {
5367                 dev_err(&pdev->dev, "Could not alloc adapater memory (DMA)\n");
5368                 goto err_iounmap;
5369         }
5370
5371         /* Init send data structures */
5372         et131x_init_send(adapter);
5373
5374         /* Set up the task structure for the ISR's deferred handler */
5375         INIT_WORK(&adapter->task, et131x_isr_handler);
5376
5377         /* Copy address into the net_device struct */
5378         memcpy(netdev->dev_addr, adapter->addr, ETH_ALEN);
5379
5380         /* Init variable for counting how long we do not have link status */
5381         adapter->boot_coma = 0;
5382         et1310_disable_phy_coma(adapter);
5383
5384         /* Setup the mii_bus struct */
5385         adapter->mii_bus = mdiobus_alloc();
5386         if (!adapter->mii_bus) {
5387                 dev_err(&pdev->dev, "Alloc of mii_bus struct failed\n");
5388                 goto err_mem_free;
5389         }
5390
5391         adapter->mii_bus->name = "et131x_eth_mii";
5392         snprintf(adapter->mii_bus->id, MII_BUS_ID_SIZE, "%x",
5393                 (adapter->pdev->bus->number << 8) | adapter->pdev->devfn);
5394         adapter->mii_bus->priv = netdev;
5395         adapter->mii_bus->read = et131x_mdio_read;
5396         adapter->mii_bus->write = et131x_mdio_write;
5397         adapter->mii_bus->reset = et131x_mdio_reset;
5398         adapter->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
5399         if (!adapter->mii_bus->irq) {
5400                 dev_err(&pdev->dev, "mii_bus irq allocation failed\n");
5401                 goto err_mdio_free;
5402         }
5403
5404         for (ii = 0; ii < PHY_MAX_ADDR; ii++)
5405                 adapter->mii_bus->irq[ii] = PHY_POLL;
5406
5407         if (mdiobus_register(adapter->mii_bus)) {
5408                 dev_err(&pdev->dev, "failed to register MII bus\n");
5409                 mdiobus_free(adapter->mii_bus);
5410                 goto err_mdio_free_irq;
5411         }
5412
5413         if (et131x_mii_probe(netdev)) {
5414                 dev_err(&pdev->dev, "failed to probe MII bus\n");
5415                 goto err_mdio_unregister;
5416         }
5417
5418         /* Setup et1310 as per the documentation */
5419         et131x_adapter_setup(adapter);
5420
5421         /* We can enable interrupts now
5422          *
5423          *  NOTE - Because registration of interrupt handler is done in the
5424          *         device's open(), defer enabling device interrupts to that
5425          *         point
5426          */
5427
5428         /* Register the net_device struct with the Linux network layer */
5429         result = register_netdev(netdev);
5430         if (result != 0) {
5431                 dev_err(&pdev->dev, "register_netdev() failed\n");
5432                 goto err_mdio_unregister;
5433         }
5434
5435         /* Register the net_device struct with the PCI subsystem. Save a copy
5436          * of the PCI config space for this device now that the device has
5437          * been initialized, just in case it needs to be quickly restored.
5438          */
5439         pci_set_drvdata(pdev, netdev);
5440         pci_save_state(adapter->pdev);
5441
5442         return result;
5443
5444 err_mdio_unregister:
5445         mdiobus_unregister(adapter->mii_bus);
5446 err_mdio_free_irq:
5447         kfree(adapter->mii_bus->irq);
5448 err_mdio_free:
5449         mdiobus_free(adapter->mii_bus);
5450 err_mem_free:
5451         et131x_adapter_memory_free(adapter);
5452 err_iounmap:
5453         iounmap(adapter->regs);
5454 err_free_dev:
5455         pci_dev_put(pdev);
5456         free_netdev(netdev);
5457 err_release_res:
5458         pci_release_regions(pdev);
5459 err_disable:
5460         pci_disable_device(pdev);
5461 err_out:
5462         return result;
5463 }
5464
5465 static SIMPLE_DEV_PM_OPS(et131x_pm_ops, et131x_suspend, et131x_resume);
5466 #define ET131X_PM_OPS (&et131x_pm_ops)
5467 #else
5468 #define ET131X_PM_OPS NULL
5469 #endif
5470
5471 static DEFINE_PCI_DEVICE_TABLE(et131x_pci_table) = {
5472         { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_GIG), 0UL},
5473         { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_FAST), 0UL},
5474         {0,}
5475 };
5476 MODULE_DEVICE_TABLE(pci, et131x_pci_table);
5477
5478 static struct pci_driver et131x_driver = {
5479         .name           = DRIVER_NAME,
5480         .id_table       = et131x_pci_table,
5481         .probe          = et131x_pci_setup,
5482         .remove         = __devexit_p(et131x_pci_remove),
5483         .driver.pm      = ET131X_PM_OPS,
5484 };
5485
5486 /**
5487  * et131x_init_module - The "main" entry point called on driver initialization
5488  *
5489  * Returns 0 on success, errno on failure (as defined in errno.h)
5490  */
5491 static int __init et131x_init_module(void)
5492 {
5493         return pci_register_driver(&et131x_driver);
5494 }
5495
5496 /**
5497  * et131x_cleanup_module - The entry point called on driver cleanup
5498  */
5499 static void __exit et131x_cleanup_module(void)
5500 {
5501         pci_unregister_driver(&et131x_driver);
5502 }
5503
5504 module_init(et131x_init_module);
5505 module_exit(et131x_cleanup_module);
5506