xref: /openbmc/linux/drivers/net/ethernet/agere/et131x.c (revision 38df6492eb511d2a6823303cb1a194c4fe423154)
1*38df6492SMark Einon /* Agere Systems Inc.
2*38df6492SMark Einon  * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
3*38df6492SMark Einon  *
4*38df6492SMark Einon  * Copyright © 2005 Agere Systems Inc.
5*38df6492SMark Einon  * All rights reserved.
6*38df6492SMark Einon  *   http://www.agere.com
7*38df6492SMark Einon  *
8*38df6492SMark Einon  * Copyright (c) 2011 Mark Einon <mark.einon@gmail.com>
9*38df6492SMark Einon  *
10*38df6492SMark Einon  *------------------------------------------------------------------------------
11*38df6492SMark Einon  *
12*38df6492SMark Einon  * SOFTWARE LICENSE
13*38df6492SMark Einon  *
14*38df6492SMark Einon  * This software is provided subject to the following terms and conditions,
15*38df6492SMark Einon  * which you should read carefully before using the software.  Using this
16*38df6492SMark Einon  * software indicates your acceptance of these terms and conditions.  If you do
17*38df6492SMark Einon  * not agree with these terms and conditions, do not use the software.
18*38df6492SMark Einon  *
19*38df6492SMark Einon  * Copyright © 2005 Agere Systems Inc.
20*38df6492SMark Einon  * All rights reserved.
21*38df6492SMark Einon  *
22*38df6492SMark Einon  * Redistribution and use in source or binary forms, with or without
23*38df6492SMark Einon  * modifications, are permitted provided that the following conditions are met:
24*38df6492SMark Einon  *
25*38df6492SMark Einon  * . Redistributions of source code must retain the above copyright notice, this
26*38df6492SMark Einon  *    list of conditions and the following Disclaimer as comments in the code as
27*38df6492SMark Einon  *    well as in the documentation and/or other materials provided with the
28*38df6492SMark Einon  *    distribution.
29*38df6492SMark Einon  *
30*38df6492SMark Einon  * . Redistributions in binary form must reproduce the above copyright notice,
31*38df6492SMark Einon  *    this list of conditions and the following Disclaimer in the documentation
32*38df6492SMark Einon  *    and/or other materials provided with the distribution.
33*38df6492SMark Einon  *
34*38df6492SMark Einon  * . Neither the name of Agere Systems Inc. nor the names of the contributors
35*38df6492SMark Einon  *    may be used to endorse or promote products derived from this software
36*38df6492SMark Einon  *    without specific prior written permission.
37*38df6492SMark Einon  *
38*38df6492SMark Einon  * Disclaimer
39*38df6492SMark Einon  *
40*38df6492SMark Einon  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
41*38df6492SMark Einon  * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
42*38df6492SMark Einon  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ANY
43*38df6492SMark Einon  * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
44*38df6492SMark Einon  * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
45*38df6492SMark Einon  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46*38df6492SMark Einon  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47*38df6492SMark Einon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48*38df6492SMark Einon  * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
49*38df6492SMark Einon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50*38df6492SMark Einon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
51*38df6492SMark Einon  * DAMAGE.
52*38df6492SMark Einon  */
53*38df6492SMark Einon 
54*38df6492SMark Einon #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
55*38df6492SMark Einon 
56*38df6492SMark Einon #include <linux/pci.h>
57*38df6492SMark Einon #include <linux/module.h>
58*38df6492SMark Einon #include <linux/types.h>
59*38df6492SMark Einon #include <linux/kernel.h>
60*38df6492SMark Einon 
61*38df6492SMark Einon #include <linux/sched.h>
62*38df6492SMark Einon #include <linux/ptrace.h>
63*38df6492SMark Einon #include <linux/slab.h>
64*38df6492SMark Einon #include <linux/ctype.h>
65*38df6492SMark Einon #include <linux/string.h>
66*38df6492SMark Einon #include <linux/timer.h>
67*38df6492SMark Einon #include <linux/interrupt.h>
68*38df6492SMark Einon #include <linux/in.h>
69*38df6492SMark Einon #include <linux/delay.h>
70*38df6492SMark Einon #include <linux/bitops.h>
71*38df6492SMark Einon #include <linux/io.h>
72*38df6492SMark Einon 
73*38df6492SMark Einon #include <linux/netdevice.h>
74*38df6492SMark Einon #include <linux/etherdevice.h>
75*38df6492SMark Einon #include <linux/skbuff.h>
76*38df6492SMark Einon #include <linux/if_arp.h>
77*38df6492SMark Einon #include <linux/ioport.h>
78*38df6492SMark Einon #include <linux/crc32.h>
79*38df6492SMark Einon #include <linux/random.h>
80*38df6492SMark Einon #include <linux/phy.h>
81*38df6492SMark Einon 
82*38df6492SMark Einon #include "et131x.h"
83*38df6492SMark Einon 
84*38df6492SMark Einon MODULE_AUTHOR("Victor Soriano <vjsoriano@agere.com>");
85*38df6492SMark Einon MODULE_AUTHOR("Mark Einon <mark.einon@gmail.com>");
86*38df6492SMark Einon MODULE_LICENSE("Dual BSD/GPL");
87*38df6492SMark Einon MODULE_DESCRIPTION("10/100/1000 Base-T Ethernet Driver for the ET1310 by Agere Systems");
88*38df6492SMark Einon 
89*38df6492SMark Einon /* EEPROM defines */
90*38df6492SMark Einon #define MAX_NUM_REGISTER_POLLS          1000
91*38df6492SMark Einon #define MAX_NUM_WRITE_RETRIES           2
92*38df6492SMark Einon 
93*38df6492SMark Einon /* MAC defines */
94*38df6492SMark Einon #define COUNTER_WRAP_16_BIT 0x10000
95*38df6492SMark Einon #define COUNTER_WRAP_12_BIT 0x1000
96*38df6492SMark Einon 
97*38df6492SMark Einon /* PCI defines */
98*38df6492SMark Einon #define INTERNAL_MEM_SIZE       0x400	/* 1024 of internal memory */
99*38df6492SMark Einon #define INTERNAL_MEM_RX_OFFSET  0x1FF	/* 50%   Tx, 50%   Rx */
100*38df6492SMark Einon 
101*38df6492SMark Einon /* ISR defines */
102*38df6492SMark Einon /* For interrupts, normal running is:
103*38df6492SMark Einon  *       rxdma_xfr_done, phy_interrupt, mac_stat_interrupt,
104*38df6492SMark Einon  *       watchdog_interrupt & txdma_xfer_done
105*38df6492SMark Einon  *
106*38df6492SMark Einon  * In both cases, when flow control is enabled for either Tx or bi-direction,
107*38df6492SMark Einon  * we additional enable rx_fbr0_low and rx_fbr1_low, so we know when the
108*38df6492SMark Einon  * buffer rings are running low.
109*38df6492SMark Einon  */
110*38df6492SMark Einon #define INT_MASK_DISABLE            0xffffffff
111*38df6492SMark Einon 
112*38df6492SMark Einon /* NOTE: Masking out MAC_STAT Interrupt for now...
113*38df6492SMark Einon  * #define INT_MASK_ENABLE             0xfff6bf17
114*38df6492SMark Einon  * #define INT_MASK_ENABLE_NO_FLOW     0xfff6bfd7
115*38df6492SMark Einon  */
116*38df6492SMark Einon #define INT_MASK_ENABLE             0xfffebf17
117*38df6492SMark Einon #define INT_MASK_ENABLE_NO_FLOW     0xfffebfd7
118*38df6492SMark Einon 
119*38df6492SMark Einon /* General defines */
120*38df6492SMark Einon /* Packet and header sizes */
121*38df6492SMark Einon #define NIC_MIN_PACKET_SIZE	60
122*38df6492SMark Einon 
123*38df6492SMark Einon /* Multicast list size */
124*38df6492SMark Einon #define NIC_MAX_MCAST_LIST	128
125*38df6492SMark Einon 
126*38df6492SMark Einon /* Supported Filters */
127*38df6492SMark Einon #define ET131X_PACKET_TYPE_DIRECTED		0x0001
128*38df6492SMark Einon #define ET131X_PACKET_TYPE_MULTICAST		0x0002
129*38df6492SMark Einon #define ET131X_PACKET_TYPE_BROADCAST		0x0004
130*38df6492SMark Einon #define ET131X_PACKET_TYPE_PROMISCUOUS		0x0008
131*38df6492SMark Einon #define ET131X_PACKET_TYPE_ALL_MULTICAST	0x0010
132*38df6492SMark Einon 
133*38df6492SMark Einon /* Tx Timeout */
134*38df6492SMark Einon #define ET131X_TX_TIMEOUT	(1 * HZ)
135*38df6492SMark Einon #define NIC_SEND_HANG_THRESHOLD	0
136*38df6492SMark Einon 
137*38df6492SMark Einon /* MP_ADAPTER flags */
138*38df6492SMark Einon #define FMP_ADAPTER_INTERRUPT_IN_USE	0x00000008
139*38df6492SMark Einon 
140*38df6492SMark Einon /* MP_SHARED flags */
141*38df6492SMark Einon #define FMP_ADAPTER_LOWER_POWER		0x00200000
142*38df6492SMark Einon 
143*38df6492SMark Einon #define FMP_ADAPTER_NON_RECOVER_ERROR	0x00800000
144*38df6492SMark Einon #define FMP_ADAPTER_HARDWARE_ERROR	0x04000000
145*38df6492SMark Einon 
146*38df6492SMark Einon #define FMP_ADAPTER_FAIL_SEND_MASK	0x3ff00000
147*38df6492SMark Einon 
148*38df6492SMark Einon /* Some offsets in PCI config space that are actually used. */
149*38df6492SMark Einon #define ET1310_PCI_MAC_ADDRESS		0xA4
150*38df6492SMark Einon #define ET1310_PCI_EEPROM_STATUS	0xB2
151*38df6492SMark Einon #define ET1310_PCI_ACK_NACK		0xC0
152*38df6492SMark Einon #define ET1310_PCI_REPLAY		0xC2
153*38df6492SMark Einon #define ET1310_PCI_L0L1LATENCY		0xCF
154*38df6492SMark Einon 
155*38df6492SMark Einon /* PCI Product IDs */
156*38df6492SMark Einon #define ET131X_PCI_DEVICE_ID_GIG	0xED00	/* ET1310 1000 Base-T 8 */
157*38df6492SMark Einon #define ET131X_PCI_DEVICE_ID_FAST	0xED01	/* ET1310 100  Base-T */
158*38df6492SMark Einon 
159*38df6492SMark Einon /* Define order of magnitude converter */
160*38df6492SMark Einon #define NANO_IN_A_MICRO	1000
161*38df6492SMark Einon 
162*38df6492SMark Einon #define PARM_RX_NUM_BUFS_DEF    4
163*38df6492SMark Einon #define PARM_RX_TIME_INT_DEF    10
164*38df6492SMark Einon #define PARM_RX_MEM_END_DEF     0x2bc
165*38df6492SMark Einon #define PARM_TX_TIME_INT_DEF    40
166*38df6492SMark Einon #define PARM_TX_NUM_BUFS_DEF    4
167*38df6492SMark Einon #define PARM_DMA_CACHE_DEF      0
168*38df6492SMark Einon 
169*38df6492SMark Einon /* RX defines */
170*38df6492SMark Einon #define FBR_CHUNKS		32
171*38df6492SMark Einon #define MAX_DESC_PER_RING_RX	1024
172*38df6492SMark Einon 
173*38df6492SMark Einon /* number of RFDs - default and min */
174*38df6492SMark Einon #define RFD_LOW_WATER_MARK	40
175*38df6492SMark Einon #define NIC_DEFAULT_NUM_RFD	1024
176*38df6492SMark Einon #define NUM_FBRS		2
177*38df6492SMark Einon 
178*38df6492SMark Einon #define MAX_PACKETS_HANDLED	256
179*38df6492SMark Einon 
180*38df6492SMark Einon #define ALCATEL_MULTICAST_PKT	0x01000000
181*38df6492SMark Einon #define ALCATEL_BROADCAST_PKT	0x02000000
182*38df6492SMark Einon 
183*38df6492SMark Einon /* typedefs for Free Buffer Descriptors */
184*38df6492SMark Einon struct fbr_desc {
185*38df6492SMark Einon 	u32 addr_lo;
186*38df6492SMark Einon 	u32 addr_hi;
187*38df6492SMark Einon 	u32 word2;		/* Bits 10-31 reserved, 0-9 descriptor */
188*38df6492SMark Einon };
189*38df6492SMark Einon 
190*38df6492SMark Einon /* Packet Status Ring Descriptors
191*38df6492SMark Einon  *
192*38df6492SMark Einon  * Word 0:
193*38df6492SMark Einon  *
194*38df6492SMark Einon  * top 16 bits are from the Alcatel Status Word as enumerated in
195*38df6492SMark Einon  * PE-MCXMAC Data Sheet IPD DS54 0210-1 (also IPD-DS80 0205-2)
196*38df6492SMark Einon  *
197*38df6492SMark Einon  * 0: hp			hash pass
198*38df6492SMark Einon  * 1: ipa			IP checksum assist
199*38df6492SMark Einon  * 2: ipp			IP checksum pass
200*38df6492SMark Einon  * 3: tcpa			TCP checksum assist
201*38df6492SMark Einon  * 4: tcpp			TCP checksum pass
202*38df6492SMark Einon  * 5: wol			WOL Event
203*38df6492SMark Einon  * 6: rxmac_error		RXMAC Error Indicator
204*38df6492SMark Einon  * 7: drop			Drop packet
205*38df6492SMark Einon  * 8: ft			Frame Truncated
206*38df6492SMark Einon  * 9: jp			Jumbo Packet
207*38df6492SMark Einon  * 10: vp			VLAN Packet
208*38df6492SMark Einon  * 11-15: unused
209*38df6492SMark Einon  * 16: asw_prev_pkt_dropped	e.g. IFG too small on previous
210*38df6492SMark Einon  * 17: asw_RX_DV_event		short receive event detected
211*38df6492SMark Einon  * 18: asw_false_carrier_event	bad carrier since last good packet
212*38df6492SMark Einon  * 19: asw_code_err		one or more nibbles signalled as errors
213*38df6492SMark Einon  * 20: asw_CRC_err		CRC error
214*38df6492SMark Einon  * 21: asw_len_chk_err		frame length field incorrect
215*38df6492SMark Einon  * 22: asw_too_long		frame length > 1518 bytes
216*38df6492SMark Einon  * 23: asw_OK			valid CRC + no code error
217*38df6492SMark Einon  * 24: asw_multicast		has a multicast address
218*38df6492SMark Einon  * 25: asw_broadcast		has a broadcast address
219*38df6492SMark Einon  * 26: asw_dribble_nibble	spurious bits after EOP
220*38df6492SMark Einon  * 27: asw_control_frame	is a control frame
221*38df6492SMark Einon  * 28: asw_pause_frame		is a pause frame
222*38df6492SMark Einon  * 29: asw_unsupported_op	unsupported OP code
223*38df6492SMark Einon  * 30: asw_VLAN_tag		VLAN tag detected
224*38df6492SMark Einon  * 31: asw_long_evt		Rx long event
225*38df6492SMark Einon  *
226*38df6492SMark Einon  * Word 1:
227*38df6492SMark Einon  * 0-15: length			length in bytes
228*38df6492SMark Einon  * 16-25: bi			Buffer Index
229*38df6492SMark Einon  * 26-27: ri			Ring Index
230*38df6492SMark Einon  * 28-31: reserved
231*38df6492SMark Einon  */
232*38df6492SMark Einon struct pkt_stat_desc {
233*38df6492SMark Einon 	u32 word0;
234*38df6492SMark Einon 	u32 word1;
235*38df6492SMark Einon };
236*38df6492SMark Einon 
237*38df6492SMark Einon /* Typedefs for the RX DMA status word */
238*38df6492SMark Einon 
239*38df6492SMark Einon /* rx status word 0 holds part of the status bits of the Rx DMA engine
240*38df6492SMark Einon  * that get copied out to memory by the ET-1310.  Word 0 is a 32 bit word
241*38df6492SMark Einon  * which contains the Free Buffer ring 0 and 1 available offset.
242*38df6492SMark Einon  *
243*38df6492SMark Einon  * bit 0-9 FBR1 offset
244*38df6492SMark Einon  * bit 10 Wrap flag for FBR1
245*38df6492SMark Einon  * bit 16-25 FBR0 offset
246*38df6492SMark Einon  * bit 26 Wrap flag for FBR0
247*38df6492SMark Einon  */
248*38df6492SMark Einon 
249*38df6492SMark Einon /* RXSTAT_WORD1_t structure holds part of the status bits of the Rx DMA engine
250*38df6492SMark Einon  * that get copied out to memory by the ET-1310.  Word 3 is a 32 bit word
251*38df6492SMark Einon  * which contains the Packet Status Ring available offset.
252*38df6492SMark Einon  *
253*38df6492SMark Einon  * bit 0-15 reserved
254*38df6492SMark Einon  * bit 16-27 PSRoffset
255*38df6492SMark Einon  * bit 28 PSRwrap
256*38df6492SMark Einon  * bit 29-31 unused
257*38df6492SMark Einon  */
258*38df6492SMark Einon 
259*38df6492SMark Einon /* struct rx_status_block is a structure representing the status of the Rx
260*38df6492SMark Einon  * DMA engine it sits in free memory, and is pointed to by 0x101c / 0x1020
261*38df6492SMark Einon  */
262*38df6492SMark Einon struct rx_status_block {
263*38df6492SMark Einon 	u32 word0;
264*38df6492SMark Einon 	u32 word1;
265*38df6492SMark Einon };
266*38df6492SMark Einon 
267*38df6492SMark Einon /* Structure for look-up table holding free buffer ring pointers, addresses
268*38df6492SMark Einon  * and state.
269*38df6492SMark Einon  */
270*38df6492SMark Einon struct fbr_lookup {
271*38df6492SMark Einon 	void		*virt[MAX_DESC_PER_RING_RX];
272*38df6492SMark Einon 	u32		 bus_high[MAX_DESC_PER_RING_RX];
273*38df6492SMark Einon 	u32		 bus_low[MAX_DESC_PER_RING_RX];
274*38df6492SMark Einon 	void		*ring_virtaddr;
275*38df6492SMark Einon 	dma_addr_t	 ring_physaddr;
276*38df6492SMark Einon 	void		*mem_virtaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
277*38df6492SMark Einon 	dma_addr_t	 mem_physaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
278*38df6492SMark Einon 	u32		 local_full;
279*38df6492SMark Einon 	u32		 num_entries;
280*38df6492SMark Einon 	dma_addr_t	 buffsize;
281*38df6492SMark Einon };
282*38df6492SMark Einon 
283*38df6492SMark Einon /* struct rx_ring is the structure representing the adaptor's local
284*38df6492SMark Einon  * reference(s) to the rings
285*38df6492SMark Einon  */
286*38df6492SMark Einon struct rx_ring {
287*38df6492SMark Einon 	struct fbr_lookup *fbr[NUM_FBRS];
288*38df6492SMark Einon 	void *ps_ring_virtaddr;
289*38df6492SMark Einon 	dma_addr_t ps_ring_physaddr;
290*38df6492SMark Einon 	u32 local_psr_full;
291*38df6492SMark Einon 	u32 psr_entries;
292*38df6492SMark Einon 
293*38df6492SMark Einon 	struct rx_status_block *rx_status_block;
294*38df6492SMark Einon 	dma_addr_t rx_status_bus;
295*38df6492SMark Einon 
296*38df6492SMark Einon 	struct list_head recv_list;
297*38df6492SMark Einon 	u32 num_ready_recv;
298*38df6492SMark Einon 
299*38df6492SMark Einon 	u32 num_rfd;
300*38df6492SMark Einon 
301*38df6492SMark Einon 	bool unfinished_receives;
302*38df6492SMark Einon };
303*38df6492SMark Einon 
304*38df6492SMark Einon /* TX defines */
305*38df6492SMark Einon /* word 2 of the control bits in the Tx Descriptor ring for the ET-1310
306*38df6492SMark Einon  *
307*38df6492SMark Einon  * 0-15: length of packet
308*38df6492SMark Einon  * 16-27: VLAN tag
309*38df6492SMark Einon  * 28: VLAN CFI
310*38df6492SMark Einon  * 29-31: VLAN priority
311*38df6492SMark Einon  *
312*38df6492SMark Einon  * word 3 of the control bits in the Tx Descriptor ring for the ET-1310
313*38df6492SMark Einon  *
314*38df6492SMark Einon  * 0: last packet in the sequence
315*38df6492SMark Einon  * 1: first packet in the sequence
316*38df6492SMark Einon  * 2: interrupt the processor when this pkt sent
317*38df6492SMark Einon  * 3: Control word - no packet data
318*38df6492SMark Einon  * 4: Issue half-duplex backpressure : XON/XOFF
319*38df6492SMark Einon  * 5: send pause frame
320*38df6492SMark Einon  * 6: Tx frame has error
321*38df6492SMark Einon  * 7: append CRC
322*38df6492SMark Einon  * 8: MAC override
323*38df6492SMark Einon  * 9: pad packet
324*38df6492SMark Einon  * 10: Packet is a Huge packet
325*38df6492SMark Einon  * 11: append VLAN tag
326*38df6492SMark Einon  * 12: IP checksum assist
327*38df6492SMark Einon  * 13: TCP checksum assist
328*38df6492SMark Einon  * 14: UDP checksum assist
329*38df6492SMark Einon  */
330*38df6492SMark Einon #define TXDESC_FLAG_LASTPKT		0x0001
331*38df6492SMark Einon #define TXDESC_FLAG_FIRSTPKT		0x0002
332*38df6492SMark Einon #define TXDESC_FLAG_INTPROC		0x0004
333*38df6492SMark Einon 
334*38df6492SMark Einon /* struct tx_desc represents each descriptor on the ring */
335*38df6492SMark Einon struct tx_desc {
336*38df6492SMark Einon 	u32 addr_hi;
337*38df6492SMark Einon 	u32 addr_lo;
338*38df6492SMark Einon 	u32 len_vlan;	/* control words how to xmit the */
339*38df6492SMark Einon 	u32 flags;	/* data (detailed above) */
340*38df6492SMark Einon };
341*38df6492SMark Einon 
342*38df6492SMark Einon /* The status of the Tx DMA engine it sits in free memory, and is pointed to
343*38df6492SMark Einon  * by 0x101c / 0x1020. This is a DMA10 type
344*38df6492SMark Einon  */
345*38df6492SMark Einon 
346*38df6492SMark Einon /* TCB (Transmit Control Block: Host Side) */
347*38df6492SMark Einon struct tcb {
348*38df6492SMark Einon 	struct tcb *next;	/* Next entry in ring */
349*38df6492SMark Einon 	u32 count;		/* Used to spot stuck/lost packets */
350*38df6492SMark Einon 	u32 stale;		/* Used to spot stuck/lost packets */
351*38df6492SMark Einon 	struct sk_buff *skb;	/* Network skb we are tied to */
352*38df6492SMark Einon 	u32 index;		/* Ring indexes */
353*38df6492SMark Einon 	u32 index_start;
354*38df6492SMark Einon };
355*38df6492SMark Einon 
356*38df6492SMark Einon /* Structure representing our local reference(s) to the ring */
357*38df6492SMark Einon struct tx_ring {
358*38df6492SMark Einon 	/* TCB (Transmit Control Block) memory and lists */
359*38df6492SMark Einon 	struct tcb *tcb_ring;
360*38df6492SMark Einon 
361*38df6492SMark Einon 	/* List of TCBs that are ready to be used */
362*38df6492SMark Einon 	struct tcb *tcb_qhead;
363*38df6492SMark Einon 	struct tcb *tcb_qtail;
364*38df6492SMark Einon 
365*38df6492SMark Einon 	/* list of TCBs that are currently being sent. */
366*38df6492SMark Einon 	struct tcb *send_head;
367*38df6492SMark Einon 	struct tcb *send_tail;
368*38df6492SMark Einon 	int used;
369*38df6492SMark Einon 
370*38df6492SMark Einon 	/* The actual descriptor ring */
371*38df6492SMark Einon 	struct tx_desc *tx_desc_ring;
372*38df6492SMark Einon 	dma_addr_t tx_desc_ring_pa;
373*38df6492SMark Einon 
374*38df6492SMark Einon 	/* send_idx indicates where we last wrote to in the descriptor ring. */
375*38df6492SMark Einon 	u32 send_idx;
376*38df6492SMark Einon 
377*38df6492SMark Einon 	/* The location of the write-back status block */
378*38df6492SMark Einon 	u32 *tx_status;
379*38df6492SMark Einon 	dma_addr_t tx_status_pa;
380*38df6492SMark Einon 
381*38df6492SMark Einon 	/* Packets since the last IRQ: used for interrupt coalescing */
382*38df6492SMark Einon 	int since_irq;
383*38df6492SMark Einon };
384*38df6492SMark Einon 
385*38df6492SMark Einon /* Do not change these values: if changed, then change also in respective
386*38df6492SMark Einon  * TXdma and Rxdma engines
387*38df6492SMark Einon  */
388*38df6492SMark Einon #define NUM_DESC_PER_RING_TX         512    /* TX Do not change these values */
389*38df6492SMark Einon #define NUM_TCB                      64
390*38df6492SMark Einon 
391*38df6492SMark Einon /* These values are all superseded by registry entries to facilitate tuning.
392*38df6492SMark Einon  * Once the desired performance has been achieved, the optimal registry values
393*38df6492SMark Einon  * should be re-populated to these #defines:
394*38df6492SMark Einon  */
395*38df6492SMark Einon #define TX_ERROR_PERIOD             1000
396*38df6492SMark Einon 
397*38df6492SMark Einon #define LO_MARK_PERCENT_FOR_PSR     15
398*38df6492SMark Einon #define LO_MARK_PERCENT_FOR_RX      15
399*38df6492SMark Einon 
400*38df6492SMark Einon /* RFD (Receive Frame Descriptor) */
401*38df6492SMark Einon struct rfd {
402*38df6492SMark Einon 	struct list_head list_node;
403*38df6492SMark Einon 	struct sk_buff *skb;
404*38df6492SMark Einon 	u32 len;	/* total size of receive frame */
405*38df6492SMark Einon 	u16 bufferindex;
406*38df6492SMark Einon 	u8 ringindex;
407*38df6492SMark Einon };
408*38df6492SMark Einon 
409*38df6492SMark Einon /* Flow Control */
410*38df6492SMark Einon #define FLOW_BOTH	0
411*38df6492SMark Einon #define FLOW_TXONLY	1
412*38df6492SMark Einon #define FLOW_RXONLY	2
413*38df6492SMark Einon #define FLOW_NONE	3
414*38df6492SMark Einon 
415*38df6492SMark Einon /* Struct to define some device statistics */
416*38df6492SMark Einon struct ce_stats {
417*38df6492SMark Einon 	u32		multicast_pkts_rcvd;
418*38df6492SMark Einon 	u32		rcvd_pkts_dropped;
419*38df6492SMark Einon 
420*38df6492SMark Einon 	u32		tx_underflows;
421*38df6492SMark Einon 	u32		tx_collisions;
422*38df6492SMark Einon 	u32		tx_excessive_collisions;
423*38df6492SMark Einon 	u32		tx_first_collisions;
424*38df6492SMark Einon 	u32		tx_late_collisions;
425*38df6492SMark Einon 	u32		tx_max_pkt_errs;
426*38df6492SMark Einon 	u32		tx_deferred;
427*38df6492SMark Einon 
428*38df6492SMark Einon 	u32		rx_overflows;
429*38df6492SMark Einon 	u32		rx_length_errs;
430*38df6492SMark Einon 	u32		rx_align_errs;
431*38df6492SMark Einon 	u32		rx_crc_errs;
432*38df6492SMark Einon 	u32		rx_code_violations;
433*38df6492SMark Einon 	u32		rx_other_errs;
434*38df6492SMark Einon 
435*38df6492SMark Einon 	u32		interrupt_status;
436*38df6492SMark Einon };
437*38df6492SMark Einon 
438*38df6492SMark Einon /* The private adapter structure */
439*38df6492SMark Einon struct et131x_adapter {
440*38df6492SMark Einon 	struct net_device *netdev;
441*38df6492SMark Einon 	struct pci_dev *pdev;
442*38df6492SMark Einon 	struct mii_bus *mii_bus;
443*38df6492SMark Einon 	struct phy_device *phydev;
444*38df6492SMark Einon 	struct napi_struct napi;
445*38df6492SMark Einon 
446*38df6492SMark Einon 	/* Flags that indicate current state of the adapter */
447*38df6492SMark Einon 	u32 flags;
448*38df6492SMark Einon 
449*38df6492SMark Einon 	/* local link state, to determine if a state change has occurred */
450*38df6492SMark Einon 	int link;
451*38df6492SMark Einon 
452*38df6492SMark Einon 	/* Configuration  */
453*38df6492SMark Einon 	u8 rom_addr[ETH_ALEN];
454*38df6492SMark Einon 	u8 addr[ETH_ALEN];
455*38df6492SMark Einon 	bool has_eeprom;
456*38df6492SMark Einon 	u8 eeprom_data[2];
457*38df6492SMark Einon 
458*38df6492SMark Einon 	spinlock_t tcb_send_qlock; /* protects the tx_ring send tcb list */
459*38df6492SMark Einon 	spinlock_t tcb_ready_qlock; /* protects the tx_ring ready tcb list */
460*38df6492SMark Einon 	spinlock_t rcv_lock; /* protects the rx_ring receive list */
461*38df6492SMark Einon 
462*38df6492SMark Einon 	/* Packet Filter and look ahead size */
463*38df6492SMark Einon 	u32 packet_filter;
464*38df6492SMark Einon 
465*38df6492SMark Einon 	/* multicast list */
466*38df6492SMark Einon 	u32 multicast_addr_count;
467*38df6492SMark Einon 	u8 multicast_list[NIC_MAX_MCAST_LIST][ETH_ALEN];
468*38df6492SMark Einon 
469*38df6492SMark Einon 	/* Pointer to the device's PCI register space */
470*38df6492SMark Einon 	struct address_map __iomem *regs;
471*38df6492SMark Einon 
472*38df6492SMark Einon 	/* Registry parameters */
473*38df6492SMark Einon 	u8 wanted_flow;		/* Flow we want for 802.3x flow control */
474*38df6492SMark Einon 	u32 registry_jumbo_packet;	/* Max supported ethernet packet size */
475*38df6492SMark Einon 
476*38df6492SMark Einon 	/* Derived from the registry: */
477*38df6492SMark Einon 	u8 flow;		/* flow control validated by the far-end */
478*38df6492SMark Einon 
479*38df6492SMark Einon 	/* Minimize init-time */
480*38df6492SMark Einon 	struct timer_list error_timer;
481*38df6492SMark Einon 
482*38df6492SMark Einon 	/* variable putting the phy into coma mode when boot up with no cable
483*38df6492SMark Einon 	 * plugged in after 5 seconds
484*38df6492SMark Einon 	 */
485*38df6492SMark Einon 	u8 boot_coma;
486*38df6492SMark Einon 
487*38df6492SMark Einon 	/* Tx Memory Variables */
488*38df6492SMark Einon 	struct tx_ring tx_ring;
489*38df6492SMark Einon 
490*38df6492SMark Einon 	/* Rx Memory Variables */
491*38df6492SMark Einon 	struct rx_ring rx_ring;
492*38df6492SMark Einon 
493*38df6492SMark Einon 	struct ce_stats stats;
494*38df6492SMark Einon };
495*38df6492SMark Einon 
496*38df6492SMark Einon static int eeprom_wait_ready(struct pci_dev *pdev, u32 *status)
497*38df6492SMark Einon {
498*38df6492SMark Einon 	u32 reg;
499*38df6492SMark Einon 	int i;
500*38df6492SMark Einon 
501*38df6492SMark Einon 	/* 1. Check LBCIF Status Register for bits 6 & 3:2 all equal to 0 and
502*38df6492SMark Einon 	 *    bits 7,1:0 both equal to 1, at least once after reset.
503*38df6492SMark Einon 	 *    Subsequent operations need only to check that bits 1:0 are equal
504*38df6492SMark Einon 	 *    to 1 prior to starting a single byte read/write
505*38df6492SMark Einon 	 */
506*38df6492SMark Einon 	for (i = 0; i < MAX_NUM_REGISTER_POLLS; i++) {
507*38df6492SMark Einon 		if (pci_read_config_dword(pdev, LBCIF_DWORD1_GROUP, &reg))
508*38df6492SMark Einon 			return -EIO;
509*38df6492SMark Einon 
510*38df6492SMark Einon 		/* I2C idle and Phy Queue Avail both true */
511*38df6492SMark Einon 		if ((reg & 0x3000) == 0x3000) {
512*38df6492SMark Einon 			if (status)
513*38df6492SMark Einon 				*status = reg;
514*38df6492SMark Einon 			return reg & 0xFF;
515*38df6492SMark Einon 		}
516*38df6492SMark Einon 	}
517*38df6492SMark Einon 	return -ETIMEDOUT;
518*38df6492SMark Einon }
519*38df6492SMark Einon 
520*38df6492SMark Einon static int eeprom_write(struct et131x_adapter *adapter, u32 addr, u8 data)
521*38df6492SMark Einon {
522*38df6492SMark Einon 	struct pci_dev *pdev = adapter->pdev;
523*38df6492SMark Einon 	int index = 0;
524*38df6492SMark Einon 	int retries;
525*38df6492SMark Einon 	int err = 0;
526*38df6492SMark Einon 	int writeok = 0;
527*38df6492SMark Einon 	u32 status;
528*38df6492SMark Einon 	u32 val = 0;
529*38df6492SMark Einon 
530*38df6492SMark Einon 	/* For an EEPROM, an I2C single byte write is defined as a START
531*38df6492SMark Einon 	 * condition followed by the device address, EEPROM address, one byte
532*38df6492SMark Einon 	 * of data and a STOP condition.  The STOP condition will trigger the
533*38df6492SMark Einon 	 * EEPROM's internally timed write cycle to the nonvolatile memory.
534*38df6492SMark Einon 	 * All inputs are disabled during this write cycle and the EEPROM will
535*38df6492SMark Einon 	 * not respond to any access until the internal write is complete.
536*38df6492SMark Einon 	 */
537*38df6492SMark Einon 	err = eeprom_wait_ready(pdev, NULL);
538*38df6492SMark Einon 	if (err < 0)
539*38df6492SMark Einon 		return err;
540*38df6492SMark Einon 
541*38df6492SMark Einon 	 /* 2. Write to the LBCIF Control Register:  bit 7=1, bit 6=1, bit 3=0,
542*38df6492SMark Einon 	  *    and bits 1:0 both =0.  Bit 5 should be set according to the
543*38df6492SMark Einon 	  *    type of EEPROM being accessed (1=two byte addressing, 0=one
544*38df6492SMark Einon 	  *    byte addressing).
545*38df6492SMark Einon 	  */
546*38df6492SMark Einon 	if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
547*38df6492SMark Einon 				  LBCIF_CONTROL_LBCIF_ENABLE |
548*38df6492SMark Einon 					LBCIF_CONTROL_I2C_WRITE))
549*38df6492SMark Einon 		return -EIO;
550*38df6492SMark Einon 
551*38df6492SMark Einon 	/* Prepare EEPROM address for Step 3 */
552*38df6492SMark Einon 	for (retries = 0; retries < MAX_NUM_WRITE_RETRIES; retries++) {
553*38df6492SMark Einon 		if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
554*38df6492SMark Einon 			break;
555*38df6492SMark Einon 		/* Write the data to the LBCIF Data Register (the I2C write
556*38df6492SMark Einon 		 * will begin).
557*38df6492SMark Einon 		 */
558*38df6492SMark Einon 		if (pci_write_config_byte(pdev, LBCIF_DATA_REGISTER, data))
559*38df6492SMark Einon 			break;
560*38df6492SMark Einon 		/* Monitor bit 1:0 of the LBCIF Status Register.  When bits
561*38df6492SMark Einon 		 * 1:0 are both equal to 1, the I2C write has completed and the
562*38df6492SMark Einon 		 * internal write cycle of the EEPROM is about to start.
563*38df6492SMark Einon 		 * (bits 1:0 = 01 is a legal state while waiting from both
564*38df6492SMark Einon 		 * equal to 1, but bits 1:0 = 10 is invalid and implies that
565*38df6492SMark Einon 		 * something is broken).
566*38df6492SMark Einon 		 */
567*38df6492SMark Einon 		err = eeprom_wait_ready(pdev, &status);
568*38df6492SMark Einon 		if (err < 0)
569*38df6492SMark Einon 			return 0;
570*38df6492SMark Einon 
571*38df6492SMark Einon 		/* Check bit 3 of the LBCIF Status Register.  If  equal to 1,
572*38df6492SMark Einon 		 * an error has occurred.Don't break here if we are revision
573*38df6492SMark Einon 		 * 1, this is so we do a blind write for load bug.
574*38df6492SMark Einon 		 */
575*38df6492SMark Einon 		if ((status & LBCIF_STATUS_GENERAL_ERROR) &&
576*38df6492SMark Einon 		    adapter->pdev->revision == 0)
577*38df6492SMark Einon 			break;
578*38df6492SMark Einon 
579*38df6492SMark Einon 		/* Check bit 2 of the LBCIF Status Register.  If equal to 1 an
580*38df6492SMark Einon 		 * ACK error has occurred on the address phase of the write.
581*38df6492SMark Einon 		 * This could be due to an actual hardware failure or the
582*38df6492SMark Einon 		 * EEPROM may still be in its internal write cycle from a
583*38df6492SMark Einon 		 * previous write. This write operation was ignored and must be
584*38df6492SMark Einon 		  *repeated later.
585*38df6492SMark Einon 		 */
586*38df6492SMark Einon 		if (status & LBCIF_STATUS_ACK_ERROR) {
587*38df6492SMark Einon 			/* This could be due to an actual hardware failure
588*38df6492SMark Einon 			 * or the EEPROM may still be in its internal write
589*38df6492SMark Einon 			 * cycle from a previous write. This write operation
590*38df6492SMark Einon 			 * was ignored and must be repeated later.
591*38df6492SMark Einon 			 */
592*38df6492SMark Einon 			udelay(10);
593*38df6492SMark Einon 			continue;
594*38df6492SMark Einon 		}
595*38df6492SMark Einon 
596*38df6492SMark Einon 		writeok = 1;
597*38df6492SMark Einon 		break;
598*38df6492SMark Einon 	}
599*38df6492SMark Einon 
600*38df6492SMark Einon 	udelay(10);
601*38df6492SMark Einon 
602*38df6492SMark Einon 	while (1) {
603*38df6492SMark Einon 		if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
604*38df6492SMark Einon 					  LBCIF_CONTROL_LBCIF_ENABLE))
605*38df6492SMark Einon 			writeok = 0;
606*38df6492SMark Einon 
607*38df6492SMark Einon 		/* Do read until internal ACK_ERROR goes away meaning write
608*38df6492SMark Einon 		 * completed
609*38df6492SMark Einon 		 */
610*38df6492SMark Einon 		do {
611*38df6492SMark Einon 			pci_write_config_dword(pdev,
612*38df6492SMark Einon 					       LBCIF_ADDRESS_REGISTER,
613*38df6492SMark Einon 					       addr);
614*38df6492SMark Einon 			do {
615*38df6492SMark Einon 				pci_read_config_dword(pdev,
616*38df6492SMark Einon 						      LBCIF_DATA_REGISTER,
617*38df6492SMark Einon 						      &val);
618*38df6492SMark Einon 			} while ((val & 0x00010000) == 0);
619*38df6492SMark Einon 		} while (val & 0x00040000);
620*38df6492SMark Einon 
621*38df6492SMark Einon 		if ((val & 0xFF00) != 0xC000 || index == 10000)
622*38df6492SMark Einon 			break;
623*38df6492SMark Einon 		index++;
624*38df6492SMark Einon 	}
625*38df6492SMark Einon 	return writeok ? 0 : -EIO;
626*38df6492SMark Einon }
627*38df6492SMark Einon 
628*38df6492SMark Einon static int eeprom_read(struct et131x_adapter *adapter, u32 addr, u8 *pdata)
629*38df6492SMark Einon {
630*38df6492SMark Einon 	struct pci_dev *pdev = adapter->pdev;
631*38df6492SMark Einon 	int err;
632*38df6492SMark Einon 	u32 status;
633*38df6492SMark Einon 
634*38df6492SMark Einon 	/* A single byte read is similar to the single byte write, with the
635*38df6492SMark Einon 	 * exception of the data flow:
636*38df6492SMark Einon 	 */
637*38df6492SMark Einon 	err = eeprom_wait_ready(pdev, NULL);
638*38df6492SMark Einon 	if (err < 0)
639*38df6492SMark Einon 		return err;
640*38df6492SMark Einon 	/* Write to the LBCIF Control Register:  bit 7=1, bit 6=0, bit 3=0,
641*38df6492SMark Einon 	 * and bits 1:0 both =0.  Bit 5 should be set according to the type
642*38df6492SMark Einon 	 * of EEPROM being accessed (1=two byte addressing, 0=one byte
643*38df6492SMark Einon 	 * addressing).
644*38df6492SMark Einon 	 */
645*38df6492SMark Einon 	if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
646*38df6492SMark Einon 				  LBCIF_CONTROL_LBCIF_ENABLE))
647*38df6492SMark Einon 		return -EIO;
648*38df6492SMark Einon 	/* Write the address to the LBCIF Address Register (I2C read will
649*38df6492SMark Einon 	 * begin).
650*38df6492SMark Einon 	 */
651*38df6492SMark Einon 	if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
652*38df6492SMark Einon 		return -EIO;
653*38df6492SMark Einon 	/* Monitor bit 0 of the LBCIF Status Register.  When = 1, I2C read
654*38df6492SMark Einon 	 * is complete. (if bit 1 =1 and bit 0 stays = 0, a hardware failure
655*38df6492SMark Einon 	 * has occurred).
656*38df6492SMark Einon 	 */
657*38df6492SMark Einon 	err = eeprom_wait_ready(pdev, &status);
658*38df6492SMark Einon 	if (err < 0)
659*38df6492SMark Einon 		return err;
660*38df6492SMark Einon 	/* Regardless of error status, read data byte from LBCIF Data
661*38df6492SMark Einon 	 * Register.
662*38df6492SMark Einon 	 */
663*38df6492SMark Einon 	*pdata = err;
664*38df6492SMark Einon 
665*38df6492SMark Einon 	return (status & LBCIF_STATUS_ACK_ERROR) ? -EIO : 0;
666*38df6492SMark Einon }
667*38df6492SMark Einon 
668*38df6492SMark Einon static int et131x_init_eeprom(struct et131x_adapter *adapter)
669*38df6492SMark Einon {
670*38df6492SMark Einon 	struct pci_dev *pdev = adapter->pdev;
671*38df6492SMark Einon 	u8 eestatus;
672*38df6492SMark Einon 
673*38df6492SMark Einon 	pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS, &eestatus);
674*38df6492SMark Einon 
675*38df6492SMark Einon 	/* THIS IS A WORKAROUND:
676*38df6492SMark Einon 	 * I need to call this function twice to get my card in a
677*38df6492SMark Einon 	 * LG M1 Express Dual running. I tried also a msleep before this
678*38df6492SMark Einon 	 * function, because I thought there could be some time conditions
679*38df6492SMark Einon 	 * but it didn't work. Call the whole function twice also work.
680*38df6492SMark Einon 	 */
681*38df6492SMark Einon 	if (pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS, &eestatus)) {
682*38df6492SMark Einon 		dev_err(&pdev->dev,
683*38df6492SMark Einon 			"Could not read PCI config space for EEPROM Status\n");
684*38df6492SMark Einon 		return -EIO;
685*38df6492SMark Einon 	}
686*38df6492SMark Einon 
687*38df6492SMark Einon 	/* Determine if the error(s) we care about are present. If they are
688*38df6492SMark Einon 	 * present we need to fail.
689*38df6492SMark Einon 	 */
690*38df6492SMark Einon 	if (eestatus & 0x4C) {
691*38df6492SMark Einon 		int write_failed = 0;
692*38df6492SMark Einon 
693*38df6492SMark Einon 		if (pdev->revision == 0x01) {
694*38df6492SMark Einon 			int	i;
695*38df6492SMark Einon 			static const u8 eedata[4] = { 0xFE, 0x13, 0x10, 0xFF };
696*38df6492SMark Einon 
697*38df6492SMark Einon 			/* Re-write the first 4 bytes if we have an eeprom
698*38df6492SMark Einon 			 * present and the revision id is 1, this fixes the
699*38df6492SMark Einon 			 * corruption seen with 1310 B Silicon
700*38df6492SMark Einon 			 */
701*38df6492SMark Einon 			for (i = 0; i < 3; i++)
702*38df6492SMark Einon 				if (eeprom_write(adapter, i, eedata[i]) < 0)
703*38df6492SMark Einon 					write_failed = 1;
704*38df6492SMark Einon 		}
705*38df6492SMark Einon 		if (pdev->revision  != 0x01 || write_failed) {
706*38df6492SMark Einon 			dev_err(&pdev->dev,
707*38df6492SMark Einon 				"Fatal EEPROM Status Error - 0x%04x\n",
708*38df6492SMark Einon 				eestatus);
709*38df6492SMark Einon 
710*38df6492SMark Einon 			/* This error could mean that there was an error
711*38df6492SMark Einon 			 * reading the eeprom or that the eeprom doesn't exist.
712*38df6492SMark Einon 			 * We will treat each case the same and not try to
713*38df6492SMark Einon 			 * gather additional information that normally would
714*38df6492SMark Einon 			 * come from the eeprom, like MAC Address
715*38df6492SMark Einon 			 */
716*38df6492SMark Einon 			adapter->has_eeprom = 0;
717*38df6492SMark Einon 			return -EIO;
718*38df6492SMark Einon 		}
719*38df6492SMark Einon 	}
720*38df6492SMark Einon 	adapter->has_eeprom = 1;
721*38df6492SMark Einon 
722*38df6492SMark Einon 	/* Read the EEPROM for information regarding LED behavior. Refer to
723*38df6492SMark Einon 	 * et131x_xcvr_init() for its use.
724*38df6492SMark Einon 	 */
725*38df6492SMark Einon 	eeprom_read(adapter, 0x70, &adapter->eeprom_data[0]);
726*38df6492SMark Einon 	eeprom_read(adapter, 0x71, &adapter->eeprom_data[1]);
727*38df6492SMark Einon 
728*38df6492SMark Einon 	if (adapter->eeprom_data[0] != 0xcd)
729*38df6492SMark Einon 		/* Disable all optional features */
730*38df6492SMark Einon 		adapter->eeprom_data[1] = 0x00;
731*38df6492SMark Einon 
732*38df6492SMark Einon 	return 0;
733*38df6492SMark Einon }
734*38df6492SMark Einon 
735*38df6492SMark Einon static void et131x_rx_dma_enable(struct et131x_adapter *adapter)
736*38df6492SMark Einon {
737*38df6492SMark Einon 	/* Setup the receive dma configuration register for normal operation */
738*38df6492SMark Einon 	u32 csr =  ET_RXDMA_CSR_FBR1_ENABLE;
739*38df6492SMark Einon 	struct rx_ring *rx_ring = &adapter->rx_ring;
740*38df6492SMark Einon 
741*38df6492SMark Einon 	if (rx_ring->fbr[1]->buffsize == 4096)
742*38df6492SMark Einon 		csr |= ET_RXDMA_CSR_FBR1_SIZE_LO;
743*38df6492SMark Einon 	else if (rx_ring->fbr[1]->buffsize == 8192)
744*38df6492SMark Einon 		csr |= ET_RXDMA_CSR_FBR1_SIZE_HI;
745*38df6492SMark Einon 	else if (rx_ring->fbr[1]->buffsize == 16384)
746*38df6492SMark Einon 		csr |= ET_RXDMA_CSR_FBR1_SIZE_LO | ET_RXDMA_CSR_FBR1_SIZE_HI;
747*38df6492SMark Einon 
748*38df6492SMark Einon 	csr |= ET_RXDMA_CSR_FBR0_ENABLE;
749*38df6492SMark Einon 	if (rx_ring->fbr[0]->buffsize == 256)
750*38df6492SMark Einon 		csr |= ET_RXDMA_CSR_FBR0_SIZE_LO;
751*38df6492SMark Einon 	else if (rx_ring->fbr[0]->buffsize == 512)
752*38df6492SMark Einon 		csr |= ET_RXDMA_CSR_FBR0_SIZE_HI;
753*38df6492SMark Einon 	else if (rx_ring->fbr[0]->buffsize == 1024)
754*38df6492SMark Einon 		csr |= ET_RXDMA_CSR_FBR0_SIZE_LO | ET_RXDMA_CSR_FBR0_SIZE_HI;
755*38df6492SMark Einon 	writel(csr, &adapter->regs->rxdma.csr);
756*38df6492SMark Einon 
757*38df6492SMark Einon 	csr = readl(&adapter->regs->rxdma.csr);
758*38df6492SMark Einon 	if (csr & ET_RXDMA_CSR_HALT_STATUS) {
759*38df6492SMark Einon 		udelay(5);
760*38df6492SMark Einon 		csr = readl(&adapter->regs->rxdma.csr);
761*38df6492SMark Einon 		if (csr & ET_RXDMA_CSR_HALT_STATUS) {
762*38df6492SMark Einon 			dev_err(&adapter->pdev->dev,
763*38df6492SMark Einon 				"RX Dma failed to exit halt state. CSR 0x%08x\n",
764*38df6492SMark Einon 				csr);
765*38df6492SMark Einon 		}
766*38df6492SMark Einon 	}
767*38df6492SMark Einon }
768*38df6492SMark Einon 
769*38df6492SMark Einon static void et131x_rx_dma_disable(struct et131x_adapter *adapter)
770*38df6492SMark Einon {
771*38df6492SMark Einon 	u32 csr;
772*38df6492SMark Einon 	/* Setup the receive dma configuration register */
773*38df6492SMark Einon 	writel(ET_RXDMA_CSR_HALT | ET_RXDMA_CSR_FBR1_ENABLE,
774*38df6492SMark Einon 	       &adapter->regs->rxdma.csr);
775*38df6492SMark Einon 	csr = readl(&adapter->regs->rxdma.csr);
776*38df6492SMark Einon 	if (!(csr & ET_RXDMA_CSR_HALT_STATUS)) {
777*38df6492SMark Einon 		udelay(5);
778*38df6492SMark Einon 		csr = readl(&adapter->regs->rxdma.csr);
779*38df6492SMark Einon 		if (!(csr & ET_RXDMA_CSR_HALT_STATUS))
780*38df6492SMark Einon 			dev_err(&adapter->pdev->dev,
781*38df6492SMark Einon 				"RX Dma failed to enter halt state. CSR 0x%08x\n",
782*38df6492SMark Einon 				csr);
783*38df6492SMark Einon 	}
784*38df6492SMark Einon }
785*38df6492SMark Einon 
786*38df6492SMark Einon static void et131x_tx_dma_enable(struct et131x_adapter *adapter)
787*38df6492SMark Einon {
788*38df6492SMark Einon 	/* Setup the transmit dma configuration register for normal
789*38df6492SMark Einon 	 * operation
790*38df6492SMark Einon 	 */
791*38df6492SMark Einon 	writel(ET_TXDMA_SNGL_EPKT | (PARM_DMA_CACHE_DEF << ET_TXDMA_CACHE_SHIFT),
792*38df6492SMark Einon 	       &adapter->regs->txdma.csr);
793*38df6492SMark Einon }
794*38df6492SMark Einon 
795*38df6492SMark Einon static inline void add_10bit(u32 *v, int n)
796*38df6492SMark Einon {
797*38df6492SMark Einon 	*v = INDEX10(*v + n) | (*v & ET_DMA10_WRAP);
798*38df6492SMark Einon }
799*38df6492SMark Einon 
800*38df6492SMark Einon static inline void add_12bit(u32 *v, int n)
801*38df6492SMark Einon {
802*38df6492SMark Einon 	*v = INDEX12(*v + n) | (*v & ET_DMA12_WRAP);
803*38df6492SMark Einon }
804*38df6492SMark Einon 
805*38df6492SMark Einon static void et1310_config_mac_regs1(struct et131x_adapter *adapter)
806*38df6492SMark Einon {
807*38df6492SMark Einon 	struct mac_regs __iomem *macregs = &adapter->regs->mac;
808*38df6492SMark Einon 	u32 station1;
809*38df6492SMark Einon 	u32 station2;
810*38df6492SMark Einon 	u32 ipg;
811*38df6492SMark Einon 
812*38df6492SMark Einon 	/* First we need to reset everything.  Write to MAC configuration
813*38df6492SMark Einon 	 * register 1 to perform reset.
814*38df6492SMark Einon 	 */
815*38df6492SMark Einon 	writel(ET_MAC_CFG1_SOFT_RESET | ET_MAC_CFG1_SIM_RESET  |
816*38df6492SMark Einon 	       ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
817*38df6492SMark Einon 	       ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC,
818*38df6492SMark Einon 	       &macregs->cfg1);
819*38df6492SMark Einon 
820*38df6492SMark Einon 	/* Next lets configure the MAC Inter-packet gap register */
821*38df6492SMark Einon 	ipg = 0x38005860;		/* IPG1 0x38 IPG2 0x58 B2B 0x60 */
822*38df6492SMark Einon 	ipg |= 0x50 << 8;		/* ifg enforce 0x50 */
823*38df6492SMark Einon 	writel(ipg, &macregs->ipg);
824*38df6492SMark Einon 
825*38df6492SMark Einon 	/* Next lets configure the MAC Half Duplex register */
826*38df6492SMark Einon 	/* BEB trunc 0xA, Ex Defer, Rexmit 0xF Coll 0x37 */
827*38df6492SMark Einon 	writel(0x00A1F037, &macregs->hfdp);
828*38df6492SMark Einon 
829*38df6492SMark Einon 	/* Next lets configure the MAC Interface Control register */
830*38df6492SMark Einon 	writel(0, &macregs->if_ctrl);
831*38df6492SMark Einon 
832*38df6492SMark Einon 	writel(ET_MAC_MIIMGMT_CLK_RST, &macregs->mii_mgmt_cfg);
833*38df6492SMark Einon 
834*38df6492SMark Einon 	/* Next lets configure the MAC Station Address register.  These
835*38df6492SMark Einon 	 * values are read from the EEPROM during initialization and stored
836*38df6492SMark Einon 	 * in the adapter structure.  We write what is stored in the adapter
837*38df6492SMark Einon 	 * structure to the MAC Station Address registers high and low.  This
838*38df6492SMark Einon 	 * station address is used for generating and checking pause control
839*38df6492SMark Einon 	 * packets.
840*38df6492SMark Einon 	 */
841*38df6492SMark Einon 	station2 = (adapter->addr[1] << ET_MAC_STATION_ADDR2_OC2_SHIFT) |
842*38df6492SMark Einon 		   (adapter->addr[0] << ET_MAC_STATION_ADDR2_OC1_SHIFT);
843*38df6492SMark Einon 	station1 = (adapter->addr[5] << ET_MAC_STATION_ADDR1_OC6_SHIFT) |
844*38df6492SMark Einon 		   (adapter->addr[4] << ET_MAC_STATION_ADDR1_OC5_SHIFT) |
845*38df6492SMark Einon 		   (adapter->addr[3] << ET_MAC_STATION_ADDR1_OC4_SHIFT) |
846*38df6492SMark Einon 		    adapter->addr[2];
847*38df6492SMark Einon 	writel(station1, &macregs->station_addr_1);
848*38df6492SMark Einon 	writel(station2, &macregs->station_addr_2);
849*38df6492SMark Einon 
850*38df6492SMark Einon 	/* Max ethernet packet in bytes that will be passed by the mac without
851*38df6492SMark Einon 	 * being truncated.  Allow the MAC to pass 4 more than our max packet
852*38df6492SMark Einon 	 * size.  This is 4 for the Ethernet CRC.
853*38df6492SMark Einon 	 *
854*38df6492SMark Einon 	 * Packets larger than (registry_jumbo_packet) that do not contain a
855*38df6492SMark Einon 	 * VLAN ID will be dropped by the Rx function.
856*38df6492SMark Einon 	 */
857*38df6492SMark Einon 	writel(adapter->registry_jumbo_packet + 4, &macregs->max_fm_len);
858*38df6492SMark Einon 
859*38df6492SMark Einon 	/* clear out MAC config reset */
860*38df6492SMark Einon 	writel(0, &macregs->cfg1);
861*38df6492SMark Einon }
862*38df6492SMark Einon 
863*38df6492SMark Einon static void et1310_config_mac_regs2(struct et131x_adapter *adapter)
864*38df6492SMark Einon {
865*38df6492SMark Einon 	int32_t delay = 0;
866*38df6492SMark Einon 	struct mac_regs __iomem *mac = &adapter->regs->mac;
867*38df6492SMark Einon 	struct phy_device *phydev = adapter->phydev;
868*38df6492SMark Einon 	u32 cfg1;
869*38df6492SMark Einon 	u32 cfg2;
870*38df6492SMark Einon 	u32 ifctrl;
871*38df6492SMark Einon 	u32 ctl;
872*38df6492SMark Einon 
873*38df6492SMark Einon 	ctl = readl(&adapter->regs->txmac.ctl);
874*38df6492SMark Einon 	cfg1 = readl(&mac->cfg1);
875*38df6492SMark Einon 	cfg2 = readl(&mac->cfg2);
876*38df6492SMark Einon 	ifctrl = readl(&mac->if_ctrl);
877*38df6492SMark Einon 
878*38df6492SMark Einon 	/* Set up the if mode bits */
879*38df6492SMark Einon 	cfg2 &= ~ET_MAC_CFG2_IFMODE_MASK;
880*38df6492SMark Einon 	if (phydev->speed == SPEED_1000) {
881*38df6492SMark Einon 		cfg2 |= ET_MAC_CFG2_IFMODE_1000;
882*38df6492SMark Einon 		ifctrl &= ~ET_MAC_IFCTRL_PHYMODE;
883*38df6492SMark Einon 	} else {
884*38df6492SMark Einon 		cfg2 |= ET_MAC_CFG2_IFMODE_100;
885*38df6492SMark Einon 		ifctrl |= ET_MAC_IFCTRL_PHYMODE;
886*38df6492SMark Einon 	}
887*38df6492SMark Einon 
888*38df6492SMark Einon 	cfg1 |= ET_MAC_CFG1_RX_ENABLE | ET_MAC_CFG1_TX_ENABLE |
889*38df6492SMark Einon 							ET_MAC_CFG1_TX_FLOW;
890*38df6492SMark Einon 
891*38df6492SMark Einon 	cfg1 &= ~(ET_MAC_CFG1_LOOPBACK | ET_MAC_CFG1_RX_FLOW);
892*38df6492SMark Einon 	if (adapter->flow == FLOW_RXONLY || adapter->flow == FLOW_BOTH)
893*38df6492SMark Einon 		cfg1 |= ET_MAC_CFG1_RX_FLOW;
894*38df6492SMark Einon 	writel(cfg1, &mac->cfg1);
895*38df6492SMark Einon 
896*38df6492SMark Einon 	/* Now we need to initialize the MAC Configuration 2 register */
897*38df6492SMark Einon 	/* preamble 7, check length, huge frame off, pad crc, crc enable
898*38df6492SMark Einon 	 * full duplex off
899*38df6492SMark Einon 	 */
900*38df6492SMark Einon 	cfg2 |= 0x7 << ET_MAC_CFG2_PREAMBLE_SHIFT;
901*38df6492SMark Einon 	cfg2 |= ET_MAC_CFG2_IFMODE_LEN_CHECK;
902*38df6492SMark Einon 	cfg2 |= ET_MAC_CFG2_IFMODE_PAD_CRC;
903*38df6492SMark Einon 	cfg2 |=	ET_MAC_CFG2_IFMODE_CRC_ENABLE;
904*38df6492SMark Einon 	cfg2 &= ~ET_MAC_CFG2_IFMODE_HUGE_FRAME;
905*38df6492SMark Einon 	cfg2 &= ~ET_MAC_CFG2_IFMODE_FULL_DPLX;
906*38df6492SMark Einon 
907*38df6492SMark Einon 	if (phydev->duplex == DUPLEX_FULL)
908*38df6492SMark Einon 		cfg2 |= ET_MAC_CFG2_IFMODE_FULL_DPLX;
909*38df6492SMark Einon 
910*38df6492SMark Einon 	ifctrl &= ~ET_MAC_IFCTRL_GHDMODE;
911*38df6492SMark Einon 	if (phydev->duplex == DUPLEX_HALF)
912*38df6492SMark Einon 		ifctrl |= ET_MAC_IFCTRL_GHDMODE;
913*38df6492SMark Einon 
914*38df6492SMark Einon 	writel(ifctrl, &mac->if_ctrl);
915*38df6492SMark Einon 	writel(cfg2, &mac->cfg2);
916*38df6492SMark Einon 
917*38df6492SMark Einon 	do {
918*38df6492SMark Einon 		udelay(10);
919*38df6492SMark Einon 		delay++;
920*38df6492SMark Einon 		cfg1 = readl(&mac->cfg1);
921*38df6492SMark Einon 	} while ((cfg1 & ET_MAC_CFG1_WAIT) != ET_MAC_CFG1_WAIT && delay < 100);
922*38df6492SMark Einon 
923*38df6492SMark Einon 	if (delay == 100) {
924*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev,
925*38df6492SMark Einon 			 "Syncd bits did not respond correctly cfg1 word 0x%08x\n",
926*38df6492SMark Einon 			 cfg1);
927*38df6492SMark Einon 	}
928*38df6492SMark Einon 
929*38df6492SMark Einon 	ctl |= ET_TX_CTRL_TXMAC_ENABLE | ET_TX_CTRL_FC_DISABLE;
930*38df6492SMark Einon 	writel(ctl, &adapter->regs->txmac.ctl);
931*38df6492SMark Einon 
932*38df6492SMark Einon 	if (adapter->flags & FMP_ADAPTER_LOWER_POWER) {
933*38df6492SMark Einon 		et131x_rx_dma_enable(adapter);
934*38df6492SMark Einon 		et131x_tx_dma_enable(adapter);
935*38df6492SMark Einon 	}
936*38df6492SMark Einon }
937*38df6492SMark Einon 
938*38df6492SMark Einon static int et1310_in_phy_coma(struct et131x_adapter *adapter)
939*38df6492SMark Einon {
940*38df6492SMark Einon 	u32 pmcsr = readl(&adapter->regs->global.pm_csr);
941*38df6492SMark Einon 
942*38df6492SMark Einon 	return ET_PM_PHY_SW_COMA & pmcsr ? 1 : 0;
943*38df6492SMark Einon }
944*38df6492SMark Einon 
945*38df6492SMark Einon static void et1310_setup_device_for_multicast(struct et131x_adapter *adapter)
946*38df6492SMark Einon {
947*38df6492SMark Einon 	struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
948*38df6492SMark Einon 	u32 hash1 = 0;
949*38df6492SMark Einon 	u32 hash2 = 0;
950*38df6492SMark Einon 	u32 hash3 = 0;
951*38df6492SMark Einon 	u32 hash4 = 0;
952*38df6492SMark Einon 	u32 pm_csr;
953*38df6492SMark Einon 
954*38df6492SMark Einon 	/* If ET131X_PACKET_TYPE_MULTICAST is specified, then we provision
955*38df6492SMark Einon 	 * the multi-cast LIST.  If it is NOT specified, (and "ALL" is not
956*38df6492SMark Einon 	 * specified) then we should pass NO multi-cast addresses to the
957*38df6492SMark Einon 	 * driver.
958*38df6492SMark Einon 	 */
959*38df6492SMark Einon 	if (adapter->packet_filter & ET131X_PACKET_TYPE_MULTICAST) {
960*38df6492SMark Einon 		int i;
961*38df6492SMark Einon 
962*38df6492SMark Einon 		/* Loop through our multicast array and set up the device */
963*38df6492SMark Einon 		for (i = 0; i < adapter->multicast_addr_count; i++) {
964*38df6492SMark Einon 			u32 result;
965*38df6492SMark Einon 
966*38df6492SMark Einon 			result = ether_crc(6, adapter->multicast_list[i]);
967*38df6492SMark Einon 
968*38df6492SMark Einon 			result = (result & 0x3F800000) >> 23;
969*38df6492SMark Einon 
970*38df6492SMark Einon 			if (result < 32) {
971*38df6492SMark Einon 				hash1 |= (1 << result);
972*38df6492SMark Einon 			} else if ((31 < result) && (result < 64)) {
973*38df6492SMark Einon 				result -= 32;
974*38df6492SMark Einon 				hash2 |= (1 << result);
975*38df6492SMark Einon 			} else if ((63 < result) && (result < 96)) {
976*38df6492SMark Einon 				result -= 64;
977*38df6492SMark Einon 				hash3 |= (1 << result);
978*38df6492SMark Einon 			} else {
979*38df6492SMark Einon 				result -= 96;
980*38df6492SMark Einon 				hash4 |= (1 << result);
981*38df6492SMark Einon 			}
982*38df6492SMark Einon 		}
983*38df6492SMark Einon 	}
984*38df6492SMark Einon 
985*38df6492SMark Einon 	/* Write out the new hash to the device */
986*38df6492SMark Einon 	pm_csr = readl(&adapter->regs->global.pm_csr);
987*38df6492SMark Einon 	if (!et1310_in_phy_coma(adapter)) {
988*38df6492SMark Einon 		writel(hash1, &rxmac->multi_hash1);
989*38df6492SMark Einon 		writel(hash2, &rxmac->multi_hash2);
990*38df6492SMark Einon 		writel(hash3, &rxmac->multi_hash3);
991*38df6492SMark Einon 		writel(hash4, &rxmac->multi_hash4);
992*38df6492SMark Einon 	}
993*38df6492SMark Einon }
994*38df6492SMark Einon 
995*38df6492SMark Einon static void et1310_setup_device_for_unicast(struct et131x_adapter *adapter)
996*38df6492SMark Einon {
997*38df6492SMark Einon 	struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
998*38df6492SMark Einon 	u32 uni_pf1;
999*38df6492SMark Einon 	u32 uni_pf2;
1000*38df6492SMark Einon 	u32 uni_pf3;
1001*38df6492SMark Einon 	u32 pm_csr;
1002*38df6492SMark Einon 
1003*38df6492SMark Einon 	/* Set up unicast packet filter reg 3 to be the first two octets of
1004*38df6492SMark Einon 	 * the MAC address for both address
1005*38df6492SMark Einon 	 *
1006*38df6492SMark Einon 	 * Set up unicast packet filter reg 2 to be the octets 2 - 5 of the
1007*38df6492SMark Einon 	 * MAC address for second address
1008*38df6492SMark Einon 	 *
1009*38df6492SMark Einon 	 * Set up unicast packet filter reg 3 to be the octets 2 - 5 of the
1010*38df6492SMark Einon 	 * MAC address for first address
1011*38df6492SMark Einon 	 */
1012*38df6492SMark Einon 	uni_pf3 = (adapter->addr[0] << ET_RX_UNI_PF_ADDR2_1_SHIFT) |
1013*38df6492SMark Einon 		  (adapter->addr[1] << ET_RX_UNI_PF_ADDR2_2_SHIFT) |
1014*38df6492SMark Einon 		  (adapter->addr[0] << ET_RX_UNI_PF_ADDR1_1_SHIFT) |
1015*38df6492SMark Einon 		   adapter->addr[1];
1016*38df6492SMark Einon 
1017*38df6492SMark Einon 	uni_pf2 = (adapter->addr[2] << ET_RX_UNI_PF_ADDR2_3_SHIFT) |
1018*38df6492SMark Einon 		  (adapter->addr[3] << ET_RX_UNI_PF_ADDR2_4_SHIFT) |
1019*38df6492SMark Einon 		  (adapter->addr[4] << ET_RX_UNI_PF_ADDR2_5_SHIFT) |
1020*38df6492SMark Einon 		   adapter->addr[5];
1021*38df6492SMark Einon 
1022*38df6492SMark Einon 	uni_pf1 = (adapter->addr[2] << ET_RX_UNI_PF_ADDR1_3_SHIFT) |
1023*38df6492SMark Einon 		  (adapter->addr[3] << ET_RX_UNI_PF_ADDR1_4_SHIFT) |
1024*38df6492SMark Einon 		  (adapter->addr[4] << ET_RX_UNI_PF_ADDR1_5_SHIFT) |
1025*38df6492SMark Einon 		   adapter->addr[5];
1026*38df6492SMark Einon 
1027*38df6492SMark Einon 	pm_csr = readl(&adapter->regs->global.pm_csr);
1028*38df6492SMark Einon 	if (!et1310_in_phy_coma(adapter)) {
1029*38df6492SMark Einon 		writel(uni_pf1, &rxmac->uni_pf_addr1);
1030*38df6492SMark Einon 		writel(uni_pf2, &rxmac->uni_pf_addr2);
1031*38df6492SMark Einon 		writel(uni_pf3, &rxmac->uni_pf_addr3);
1032*38df6492SMark Einon 	}
1033*38df6492SMark Einon }
1034*38df6492SMark Einon 
1035*38df6492SMark Einon static void et1310_config_rxmac_regs(struct et131x_adapter *adapter)
1036*38df6492SMark Einon {
1037*38df6492SMark Einon 	struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
1038*38df6492SMark Einon 	struct phy_device *phydev = adapter->phydev;
1039*38df6492SMark Einon 	u32 sa_lo;
1040*38df6492SMark Einon 	u32 sa_hi = 0;
1041*38df6492SMark Einon 	u32 pf_ctrl = 0;
1042*38df6492SMark Einon 	u32 __iomem *wolw;
1043*38df6492SMark Einon 
1044*38df6492SMark Einon 	/* Disable the MAC while it is being configured (also disable WOL) */
1045*38df6492SMark Einon 	writel(0x8, &rxmac->ctrl);
1046*38df6492SMark Einon 
1047*38df6492SMark Einon 	/* Initialize WOL to disabled. */
1048*38df6492SMark Einon 	writel(0, &rxmac->crc0);
1049*38df6492SMark Einon 	writel(0, &rxmac->crc12);
1050*38df6492SMark Einon 	writel(0, &rxmac->crc34);
1051*38df6492SMark Einon 
1052*38df6492SMark Einon 	/* We need to set the WOL mask0 - mask4 next.  We initialize it to
1053*38df6492SMark Einon 	 * its default Values of 0x00000000 because there are not WOL masks
1054*38df6492SMark Einon 	 * as of this time.
1055*38df6492SMark Einon 	 */
1056*38df6492SMark Einon 	for (wolw = &rxmac->mask0_word0; wolw <= &rxmac->mask4_word3; wolw++)
1057*38df6492SMark Einon 		writel(0, wolw);
1058*38df6492SMark Einon 
1059*38df6492SMark Einon 	/* Lets setup the WOL Source Address */
1060*38df6492SMark Einon 	sa_lo = (adapter->addr[2] << ET_RX_WOL_LO_SA3_SHIFT) |
1061*38df6492SMark Einon 		(adapter->addr[3] << ET_RX_WOL_LO_SA4_SHIFT) |
1062*38df6492SMark Einon 		(adapter->addr[4] << ET_RX_WOL_LO_SA5_SHIFT) |
1063*38df6492SMark Einon 		 adapter->addr[5];
1064*38df6492SMark Einon 	writel(sa_lo, &rxmac->sa_lo);
1065*38df6492SMark Einon 
1066*38df6492SMark Einon 	sa_hi = (u32)(adapter->addr[0] << ET_RX_WOL_HI_SA1_SHIFT) |
1067*38df6492SMark Einon 		       adapter->addr[1];
1068*38df6492SMark Einon 	writel(sa_hi, &rxmac->sa_hi);
1069*38df6492SMark Einon 
1070*38df6492SMark Einon 	/* Disable all Packet Filtering */
1071*38df6492SMark Einon 	writel(0, &rxmac->pf_ctrl);
1072*38df6492SMark Einon 
1073*38df6492SMark Einon 	/* Let's initialize the Unicast Packet filtering address */
1074*38df6492SMark Einon 	if (adapter->packet_filter & ET131X_PACKET_TYPE_DIRECTED) {
1075*38df6492SMark Einon 		et1310_setup_device_for_unicast(adapter);
1076*38df6492SMark Einon 		pf_ctrl |= ET_RX_PFCTRL_UNICST_FILTER_ENABLE;
1077*38df6492SMark Einon 	} else {
1078*38df6492SMark Einon 		writel(0, &rxmac->uni_pf_addr1);
1079*38df6492SMark Einon 		writel(0, &rxmac->uni_pf_addr2);
1080*38df6492SMark Einon 		writel(0, &rxmac->uni_pf_addr3);
1081*38df6492SMark Einon 	}
1082*38df6492SMark Einon 
1083*38df6492SMark Einon 	/* Let's initialize the Multicast hash */
1084*38df6492SMark Einon 	if (!(adapter->packet_filter & ET131X_PACKET_TYPE_ALL_MULTICAST)) {
1085*38df6492SMark Einon 		pf_ctrl |= ET_RX_PFCTRL_MLTCST_FILTER_ENABLE;
1086*38df6492SMark Einon 		et1310_setup_device_for_multicast(adapter);
1087*38df6492SMark Einon 	}
1088*38df6492SMark Einon 
1089*38df6492SMark Einon 	/* Runt packet filtering.  Didn't work in version A silicon. */
1090*38df6492SMark Einon 	pf_ctrl |= (NIC_MIN_PACKET_SIZE + 4) << ET_RX_PFCTRL_MIN_PKT_SZ_SHIFT;
1091*38df6492SMark Einon 	pf_ctrl |= ET_RX_PFCTRL_FRAG_FILTER_ENABLE;
1092*38df6492SMark Einon 
1093*38df6492SMark Einon 	if (adapter->registry_jumbo_packet > 8192)
1094*38df6492SMark Einon 		/* In order to transmit jumbo packets greater than 8k, the
1095*38df6492SMark Einon 		 * FIFO between RxMAC and RxDMA needs to be reduced in size
1096*38df6492SMark Einon 		 * to (16k - Jumbo packet size).  In order to implement this,
1097*38df6492SMark Einon 		 * we must use "cut through" mode in the RxMAC, which chops
1098*38df6492SMark Einon 		 * packets down into segments which are (max_size * 16).  In
1099*38df6492SMark Einon 		 * this case we selected 256 bytes, since this is the size of
1100*38df6492SMark Einon 		 * the PCI-Express TLP's that the 1310 uses.
1101*38df6492SMark Einon 		 *
1102*38df6492SMark Einon 		 * seg_en on, fc_en off, size 0x10
1103*38df6492SMark Einon 		 */
1104*38df6492SMark Einon 		writel(0x41, &rxmac->mcif_ctrl_max_seg);
1105*38df6492SMark Einon 	else
1106*38df6492SMark Einon 		writel(0, &rxmac->mcif_ctrl_max_seg);
1107*38df6492SMark Einon 
1108*38df6492SMark Einon 	writel(0, &rxmac->mcif_water_mark);
1109*38df6492SMark Einon 	writel(0, &rxmac->mif_ctrl);
1110*38df6492SMark Einon 	writel(0, &rxmac->space_avail);
1111*38df6492SMark Einon 
1112*38df6492SMark Einon 	/* Initialize the the mif_ctrl register
1113*38df6492SMark Einon 	 * bit 3:  Receive code error. One or more nibbles were signaled as
1114*38df6492SMark Einon 	 *	   errors  during the reception of the packet.  Clear this
1115*38df6492SMark Einon 	 *	   bit in Gigabit, set it in 100Mbit.  This was derived
1116*38df6492SMark Einon 	 *	   experimentally at UNH.
1117*38df6492SMark Einon 	 * bit 4:  Receive CRC error. The packet's CRC did not match the
1118*38df6492SMark Einon 	 *	   internally generated CRC.
1119*38df6492SMark Einon 	 * bit 5:  Receive length check error. Indicates that frame length
1120*38df6492SMark Einon 	 *	   field value in the packet does not match the actual data
1121*38df6492SMark Einon 	 *	   byte length and is not a type field.
1122*38df6492SMark Einon 	 * bit 16: Receive frame truncated.
1123*38df6492SMark Einon 	 * bit 17: Drop packet enable
1124*38df6492SMark Einon 	 */
1125*38df6492SMark Einon 	if (phydev && phydev->speed == SPEED_100)
1126*38df6492SMark Einon 		writel(0x30038, &rxmac->mif_ctrl);
1127*38df6492SMark Einon 	else
1128*38df6492SMark Einon 		writel(0x30030, &rxmac->mif_ctrl);
1129*38df6492SMark Einon 
1130*38df6492SMark Einon 	/* Finally we initialize RxMac to be enabled & WOL disabled.  Packet
1131*38df6492SMark Einon 	 * filter is always enabled since it is where the runt packets are
1132*38df6492SMark Einon 	 * supposed to be dropped.  For version A silicon, runt packet
1133*38df6492SMark Einon 	 * dropping doesn't work, so it is disabled in the pf_ctrl register,
1134*38df6492SMark Einon 	 * but we still leave the packet filter on.
1135*38df6492SMark Einon 	 */
1136*38df6492SMark Einon 	writel(pf_ctrl, &rxmac->pf_ctrl);
1137*38df6492SMark Einon 	writel(ET_RX_CTRL_RXMAC_ENABLE | ET_RX_CTRL_WOL_DISABLE, &rxmac->ctrl);
1138*38df6492SMark Einon }
1139*38df6492SMark Einon 
1140*38df6492SMark Einon static void et1310_config_txmac_regs(struct et131x_adapter *adapter)
1141*38df6492SMark Einon {
1142*38df6492SMark Einon 	struct txmac_regs __iomem *txmac = &adapter->regs->txmac;
1143*38df6492SMark Einon 
1144*38df6492SMark Einon 	/* We need to update the Control Frame Parameters
1145*38df6492SMark Einon 	 * cfpt - control frame pause timer set to 64 (0x40)
1146*38df6492SMark Einon 	 * cfep - control frame extended pause timer set to 0x0
1147*38df6492SMark Einon 	 */
1148*38df6492SMark Einon 	if (adapter->flow == FLOW_NONE)
1149*38df6492SMark Einon 		writel(0, &txmac->cf_param);
1150*38df6492SMark Einon 	else
1151*38df6492SMark Einon 		writel(0x40, &txmac->cf_param);
1152*38df6492SMark Einon }
1153*38df6492SMark Einon 
1154*38df6492SMark Einon static void et1310_config_macstat_regs(struct et131x_adapter *adapter)
1155*38df6492SMark Einon {
1156*38df6492SMark Einon 	struct macstat_regs __iomem *macstat = &adapter->regs->macstat;
1157*38df6492SMark Einon 	u32 __iomem *reg;
1158*38df6492SMark Einon 
1159*38df6492SMark Einon 	/* initialize all the macstat registers to zero on the device  */
1160*38df6492SMark Einon 	for (reg = &macstat->txrx_0_64_byte_frames;
1161*38df6492SMark Einon 	     reg <= &macstat->carry_reg2; reg++)
1162*38df6492SMark Einon 		writel(0, reg);
1163*38df6492SMark Einon 
1164*38df6492SMark Einon 	/* Unmask any counters that we want to track the overflow of.
1165*38df6492SMark Einon 	 * Initially this will be all counters.  It may become clear later
1166*38df6492SMark Einon 	 * that we do not need to track all counters.
1167*38df6492SMark Einon 	 */
1168*38df6492SMark Einon 	writel(0xFFFFBE32, &macstat->carry_reg1_mask);
1169*38df6492SMark Einon 	writel(0xFFFE7E8B, &macstat->carry_reg2_mask);
1170*38df6492SMark Einon }
1171*38df6492SMark Einon 
1172*38df6492SMark Einon static int et131x_phy_mii_read(struct et131x_adapter *adapter, u8 addr,
1173*38df6492SMark Einon 			       u8 reg, u16 *value)
1174*38df6492SMark Einon {
1175*38df6492SMark Einon 	struct mac_regs __iomem *mac = &adapter->regs->mac;
1176*38df6492SMark Einon 	int status = 0;
1177*38df6492SMark Einon 	u32 delay = 0;
1178*38df6492SMark Einon 	u32 mii_addr;
1179*38df6492SMark Einon 	u32 mii_cmd;
1180*38df6492SMark Einon 	u32 mii_indicator;
1181*38df6492SMark Einon 
1182*38df6492SMark Einon 	/* Save a local copy of the registers we are dealing with so we can
1183*38df6492SMark Einon 	 * set them back
1184*38df6492SMark Einon 	 */
1185*38df6492SMark Einon 	mii_addr = readl(&mac->mii_mgmt_addr);
1186*38df6492SMark Einon 	mii_cmd = readl(&mac->mii_mgmt_cmd);
1187*38df6492SMark Einon 
1188*38df6492SMark Einon 	/* Stop the current operation */
1189*38df6492SMark Einon 	writel(0, &mac->mii_mgmt_cmd);
1190*38df6492SMark Einon 
1191*38df6492SMark Einon 	/* Set up the register we need to read from on the correct PHY */
1192*38df6492SMark Einon 	writel(ET_MAC_MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
1193*38df6492SMark Einon 
1194*38df6492SMark Einon 	writel(0x1, &mac->mii_mgmt_cmd);
1195*38df6492SMark Einon 
1196*38df6492SMark Einon 	do {
1197*38df6492SMark Einon 		udelay(50);
1198*38df6492SMark Einon 		delay++;
1199*38df6492SMark Einon 		mii_indicator = readl(&mac->mii_mgmt_indicator);
1200*38df6492SMark Einon 	} while ((mii_indicator & ET_MAC_MGMT_WAIT) && delay < 50);
1201*38df6492SMark Einon 
1202*38df6492SMark Einon 	/* If we hit the max delay, we could not read the register */
1203*38df6492SMark Einon 	if (delay == 50) {
1204*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev,
1205*38df6492SMark Einon 			 "reg 0x%08x could not be read\n", reg);
1206*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev, "status is  0x%08x\n",
1207*38df6492SMark Einon 			 mii_indicator);
1208*38df6492SMark Einon 
1209*38df6492SMark Einon 		status = -EIO;
1210*38df6492SMark Einon 		goto out;
1211*38df6492SMark Einon 	}
1212*38df6492SMark Einon 
1213*38df6492SMark Einon 	/* If we hit here we were able to read the register and we need to
1214*38df6492SMark Einon 	 * return the value to the caller
1215*38df6492SMark Einon 	 */
1216*38df6492SMark Einon 	*value = readl(&mac->mii_mgmt_stat) & ET_MAC_MIIMGMT_STAT_PHYCRTL_MASK;
1217*38df6492SMark Einon 
1218*38df6492SMark Einon out:
1219*38df6492SMark Einon 	/* Stop the read operation */
1220*38df6492SMark Einon 	writel(0, &mac->mii_mgmt_cmd);
1221*38df6492SMark Einon 
1222*38df6492SMark Einon 	/* set the registers we touched back to the state at which we entered
1223*38df6492SMark Einon 	 * this function
1224*38df6492SMark Einon 	 */
1225*38df6492SMark Einon 	writel(mii_addr, &mac->mii_mgmt_addr);
1226*38df6492SMark Einon 	writel(mii_cmd, &mac->mii_mgmt_cmd);
1227*38df6492SMark Einon 
1228*38df6492SMark Einon 	return status;
1229*38df6492SMark Einon }
1230*38df6492SMark Einon 
1231*38df6492SMark Einon static int et131x_mii_read(struct et131x_adapter *adapter, u8 reg, u16 *value)
1232*38df6492SMark Einon {
1233*38df6492SMark Einon 	struct phy_device *phydev = adapter->phydev;
1234*38df6492SMark Einon 
1235*38df6492SMark Einon 	if (!phydev)
1236*38df6492SMark Einon 		return -EIO;
1237*38df6492SMark Einon 
1238*38df6492SMark Einon 	return et131x_phy_mii_read(adapter, phydev->addr, reg, value);
1239*38df6492SMark Einon }
1240*38df6492SMark Einon 
1241*38df6492SMark Einon static int et131x_mii_write(struct et131x_adapter *adapter, u8 addr, u8 reg,
1242*38df6492SMark Einon 			    u16 value)
1243*38df6492SMark Einon {
1244*38df6492SMark Einon 	struct mac_regs __iomem *mac = &adapter->regs->mac;
1245*38df6492SMark Einon 	int status = 0;
1246*38df6492SMark Einon 	u32 delay = 0;
1247*38df6492SMark Einon 	u32 mii_addr;
1248*38df6492SMark Einon 	u32 mii_cmd;
1249*38df6492SMark Einon 	u32 mii_indicator;
1250*38df6492SMark Einon 
1251*38df6492SMark Einon 	/* Save a local copy of the registers we are dealing with so we can
1252*38df6492SMark Einon 	 * set them back
1253*38df6492SMark Einon 	 */
1254*38df6492SMark Einon 	mii_addr = readl(&mac->mii_mgmt_addr);
1255*38df6492SMark Einon 	mii_cmd = readl(&mac->mii_mgmt_cmd);
1256*38df6492SMark Einon 
1257*38df6492SMark Einon 	/* Stop the current operation */
1258*38df6492SMark Einon 	writel(0, &mac->mii_mgmt_cmd);
1259*38df6492SMark Einon 
1260*38df6492SMark Einon 	/* Set up the register we need to write to on the correct PHY */
1261*38df6492SMark Einon 	writel(ET_MAC_MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
1262*38df6492SMark Einon 
1263*38df6492SMark Einon 	/* Add the value to write to the registers to the mac */
1264*38df6492SMark Einon 	writel(value, &mac->mii_mgmt_ctrl);
1265*38df6492SMark Einon 
1266*38df6492SMark Einon 	do {
1267*38df6492SMark Einon 		udelay(50);
1268*38df6492SMark Einon 		delay++;
1269*38df6492SMark Einon 		mii_indicator = readl(&mac->mii_mgmt_indicator);
1270*38df6492SMark Einon 	} while ((mii_indicator & ET_MAC_MGMT_BUSY) && delay < 100);
1271*38df6492SMark Einon 
1272*38df6492SMark Einon 	/* If we hit the max delay, we could not write the register */
1273*38df6492SMark Einon 	if (delay == 100) {
1274*38df6492SMark Einon 		u16 tmp;
1275*38df6492SMark Einon 
1276*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev,
1277*38df6492SMark Einon 			 "reg 0x%08x could not be written", reg);
1278*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev, "status is  0x%08x\n",
1279*38df6492SMark Einon 			 mii_indicator);
1280*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev, "command is  0x%08x\n",
1281*38df6492SMark Einon 			 readl(&mac->mii_mgmt_cmd));
1282*38df6492SMark Einon 
1283*38df6492SMark Einon 		et131x_mii_read(adapter, reg, &tmp);
1284*38df6492SMark Einon 
1285*38df6492SMark Einon 		status = -EIO;
1286*38df6492SMark Einon 	}
1287*38df6492SMark Einon 	/* Stop the write operation */
1288*38df6492SMark Einon 	writel(0, &mac->mii_mgmt_cmd);
1289*38df6492SMark Einon 
1290*38df6492SMark Einon 	/* set the registers we touched back to the state at which we entered
1291*38df6492SMark Einon 	 * this function
1292*38df6492SMark Einon 	 */
1293*38df6492SMark Einon 	writel(mii_addr, &mac->mii_mgmt_addr);
1294*38df6492SMark Einon 	writel(mii_cmd, &mac->mii_mgmt_cmd);
1295*38df6492SMark Einon 
1296*38df6492SMark Einon 	return status;
1297*38df6492SMark Einon }
1298*38df6492SMark Einon 
1299*38df6492SMark Einon static void et1310_phy_read_mii_bit(struct et131x_adapter *adapter,
1300*38df6492SMark Einon 				    u16 regnum,
1301*38df6492SMark Einon 				    u16 bitnum,
1302*38df6492SMark Einon 				    u8 *value)
1303*38df6492SMark Einon {
1304*38df6492SMark Einon 	u16 reg;
1305*38df6492SMark Einon 	u16 mask = 1 << bitnum;
1306*38df6492SMark Einon 
1307*38df6492SMark Einon 	et131x_mii_read(adapter, regnum, &reg);
1308*38df6492SMark Einon 
1309*38df6492SMark Einon 	*value = (reg & mask) >> bitnum;
1310*38df6492SMark Einon }
1311*38df6492SMark Einon 
1312*38df6492SMark Einon static void et1310_config_flow_control(struct et131x_adapter *adapter)
1313*38df6492SMark Einon {
1314*38df6492SMark Einon 	struct phy_device *phydev = adapter->phydev;
1315*38df6492SMark Einon 
1316*38df6492SMark Einon 	if (phydev->duplex == DUPLEX_HALF) {
1317*38df6492SMark Einon 		adapter->flow = FLOW_NONE;
1318*38df6492SMark Einon 	} else {
1319*38df6492SMark Einon 		char remote_pause, remote_async_pause;
1320*38df6492SMark Einon 
1321*38df6492SMark Einon 		et1310_phy_read_mii_bit(adapter, 5, 10, &remote_pause);
1322*38df6492SMark Einon 		et1310_phy_read_mii_bit(adapter, 5, 11, &remote_async_pause);
1323*38df6492SMark Einon 
1324*38df6492SMark Einon 		if (remote_pause && remote_async_pause) {
1325*38df6492SMark Einon 			adapter->flow = adapter->wanted_flow;
1326*38df6492SMark Einon 		} else if (remote_pause && !remote_async_pause) {
1327*38df6492SMark Einon 			if (adapter->wanted_flow == FLOW_BOTH)
1328*38df6492SMark Einon 				adapter->flow = FLOW_BOTH;
1329*38df6492SMark Einon 			else
1330*38df6492SMark Einon 				adapter->flow = FLOW_NONE;
1331*38df6492SMark Einon 		} else if (!remote_pause && !remote_async_pause) {
1332*38df6492SMark Einon 			adapter->flow = FLOW_NONE;
1333*38df6492SMark Einon 		} else {
1334*38df6492SMark Einon 			if (adapter->wanted_flow == FLOW_BOTH)
1335*38df6492SMark Einon 				adapter->flow = FLOW_RXONLY;
1336*38df6492SMark Einon 			else
1337*38df6492SMark Einon 				adapter->flow = FLOW_NONE;
1338*38df6492SMark Einon 		}
1339*38df6492SMark Einon 	}
1340*38df6492SMark Einon }
1341*38df6492SMark Einon 
1342*38df6492SMark Einon /* et1310_update_macstat_host_counters - Update local copy of the statistics */
1343*38df6492SMark Einon static void et1310_update_macstat_host_counters(struct et131x_adapter *adapter)
1344*38df6492SMark Einon {
1345*38df6492SMark Einon 	struct ce_stats *stats = &adapter->stats;
1346*38df6492SMark Einon 	struct macstat_regs __iomem *macstat =
1347*38df6492SMark Einon 		&adapter->regs->macstat;
1348*38df6492SMark Einon 
1349*38df6492SMark Einon 	stats->tx_collisions	       += readl(&macstat->tx_total_collisions);
1350*38df6492SMark Einon 	stats->tx_first_collisions     += readl(&macstat->tx_single_collisions);
1351*38df6492SMark Einon 	stats->tx_deferred	       += readl(&macstat->tx_deferred);
1352*38df6492SMark Einon 	stats->tx_excessive_collisions +=
1353*38df6492SMark Einon 				readl(&macstat->tx_multiple_collisions);
1354*38df6492SMark Einon 	stats->tx_late_collisions      += readl(&macstat->tx_late_collisions);
1355*38df6492SMark Einon 	stats->tx_underflows	       += readl(&macstat->tx_undersize_frames);
1356*38df6492SMark Einon 	stats->tx_max_pkt_errs	       += readl(&macstat->tx_oversize_frames);
1357*38df6492SMark Einon 
1358*38df6492SMark Einon 	stats->rx_align_errs        += readl(&macstat->rx_align_errs);
1359*38df6492SMark Einon 	stats->rx_crc_errs          += readl(&macstat->rx_code_errs);
1360*38df6492SMark Einon 	stats->rcvd_pkts_dropped    += readl(&macstat->rx_drops);
1361*38df6492SMark Einon 	stats->rx_overflows         += readl(&macstat->rx_oversize_packets);
1362*38df6492SMark Einon 	stats->rx_code_violations   += readl(&macstat->rx_fcs_errs);
1363*38df6492SMark Einon 	stats->rx_length_errs       += readl(&macstat->rx_frame_len_errs);
1364*38df6492SMark Einon 	stats->rx_other_errs        += readl(&macstat->rx_fragment_packets);
1365*38df6492SMark Einon }
1366*38df6492SMark Einon 
1367*38df6492SMark Einon /* et1310_handle_macstat_interrupt
1368*38df6492SMark Einon  *
1369*38df6492SMark Einon  * One of the MACSTAT counters has wrapped.  Update the local copy of
1370*38df6492SMark Einon  * the statistics held in the adapter structure, checking the "wrap"
1371*38df6492SMark Einon  * bit for each counter.
1372*38df6492SMark Einon  */
1373*38df6492SMark Einon static void et1310_handle_macstat_interrupt(struct et131x_adapter *adapter)
1374*38df6492SMark Einon {
1375*38df6492SMark Einon 	u32 carry_reg1;
1376*38df6492SMark Einon 	u32 carry_reg2;
1377*38df6492SMark Einon 
1378*38df6492SMark Einon 	/* Read the interrupt bits from the register(s).  These are Clear On
1379*38df6492SMark Einon 	 * Write.
1380*38df6492SMark Einon 	 */
1381*38df6492SMark Einon 	carry_reg1 = readl(&adapter->regs->macstat.carry_reg1);
1382*38df6492SMark Einon 	carry_reg2 = readl(&adapter->regs->macstat.carry_reg2);
1383*38df6492SMark Einon 
1384*38df6492SMark Einon 	writel(carry_reg1, &adapter->regs->macstat.carry_reg1);
1385*38df6492SMark Einon 	writel(carry_reg2, &adapter->regs->macstat.carry_reg2);
1386*38df6492SMark Einon 
1387*38df6492SMark Einon 	/* We need to do update the host copy of all the MAC_STAT counters.
1388*38df6492SMark Einon 	 * For each counter, check it's overflow bit.  If the overflow bit is
1389*38df6492SMark Einon 	 * set, then increment the host version of the count by one complete
1390*38df6492SMark Einon 	 * revolution of the counter.  This routine is called when the counter
1391*38df6492SMark Einon 	 * block indicates that one of the counters has wrapped.
1392*38df6492SMark Einon 	 */
1393*38df6492SMark Einon 	if (carry_reg1 & (1 << 14))
1394*38df6492SMark Einon 		adapter->stats.rx_code_violations	+= COUNTER_WRAP_16_BIT;
1395*38df6492SMark Einon 	if (carry_reg1 & (1 << 8))
1396*38df6492SMark Einon 		adapter->stats.rx_align_errs	+= COUNTER_WRAP_12_BIT;
1397*38df6492SMark Einon 	if (carry_reg1 & (1 << 7))
1398*38df6492SMark Einon 		adapter->stats.rx_length_errs	+= COUNTER_WRAP_16_BIT;
1399*38df6492SMark Einon 	if (carry_reg1 & (1 << 2))
1400*38df6492SMark Einon 		adapter->stats.rx_other_errs	+= COUNTER_WRAP_16_BIT;
1401*38df6492SMark Einon 	if (carry_reg1 & (1 << 6))
1402*38df6492SMark Einon 		adapter->stats.rx_crc_errs	+= COUNTER_WRAP_16_BIT;
1403*38df6492SMark Einon 	if (carry_reg1 & (1 << 3))
1404*38df6492SMark Einon 		adapter->stats.rx_overflows	+= COUNTER_WRAP_16_BIT;
1405*38df6492SMark Einon 	if (carry_reg1 & (1 << 0))
1406*38df6492SMark Einon 		adapter->stats.rcvd_pkts_dropped	+= COUNTER_WRAP_16_BIT;
1407*38df6492SMark Einon 	if (carry_reg2 & (1 << 16))
1408*38df6492SMark Einon 		adapter->stats.tx_max_pkt_errs	+= COUNTER_WRAP_12_BIT;
1409*38df6492SMark Einon 	if (carry_reg2 & (1 << 15))
1410*38df6492SMark Einon 		adapter->stats.tx_underflows	+= COUNTER_WRAP_12_BIT;
1411*38df6492SMark Einon 	if (carry_reg2 & (1 << 6))
1412*38df6492SMark Einon 		adapter->stats.tx_first_collisions += COUNTER_WRAP_12_BIT;
1413*38df6492SMark Einon 	if (carry_reg2 & (1 << 8))
1414*38df6492SMark Einon 		adapter->stats.tx_deferred	+= COUNTER_WRAP_12_BIT;
1415*38df6492SMark Einon 	if (carry_reg2 & (1 << 5))
1416*38df6492SMark Einon 		adapter->stats.tx_excessive_collisions += COUNTER_WRAP_12_BIT;
1417*38df6492SMark Einon 	if (carry_reg2 & (1 << 4))
1418*38df6492SMark Einon 		adapter->stats.tx_late_collisions	+= COUNTER_WRAP_12_BIT;
1419*38df6492SMark Einon 	if (carry_reg2 & (1 << 2))
1420*38df6492SMark Einon 		adapter->stats.tx_collisions	+= COUNTER_WRAP_12_BIT;
1421*38df6492SMark Einon }
1422*38df6492SMark Einon 
1423*38df6492SMark Einon static int et131x_mdio_read(struct mii_bus *bus, int phy_addr, int reg)
1424*38df6492SMark Einon {
1425*38df6492SMark Einon 	struct net_device *netdev = bus->priv;
1426*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
1427*38df6492SMark Einon 	u16 value;
1428*38df6492SMark Einon 	int ret;
1429*38df6492SMark Einon 
1430*38df6492SMark Einon 	ret = et131x_phy_mii_read(adapter, phy_addr, reg, &value);
1431*38df6492SMark Einon 
1432*38df6492SMark Einon 	if (ret < 0)
1433*38df6492SMark Einon 		return ret;
1434*38df6492SMark Einon 
1435*38df6492SMark Einon 	return value;
1436*38df6492SMark Einon }
1437*38df6492SMark Einon 
1438*38df6492SMark Einon static int et131x_mdio_write(struct mii_bus *bus, int phy_addr,
1439*38df6492SMark Einon 			     int reg, u16 value)
1440*38df6492SMark Einon {
1441*38df6492SMark Einon 	struct net_device *netdev = bus->priv;
1442*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
1443*38df6492SMark Einon 
1444*38df6492SMark Einon 	return et131x_mii_write(adapter, phy_addr, reg, value);
1445*38df6492SMark Einon }
1446*38df6492SMark Einon 
1447*38df6492SMark Einon /*	et1310_phy_power_switch	-	PHY power control
1448*38df6492SMark Einon  *	@adapter: device to control
1449*38df6492SMark Einon  *	@down: true for off/false for back on
1450*38df6492SMark Einon  *
1451*38df6492SMark Einon  *	one hundred, ten, one thousand megs
1452*38df6492SMark Einon  *	How would you like to have your LAN accessed
1453*38df6492SMark Einon  *	Can't you see that this code processed
1454*38df6492SMark Einon  *	Phy power, phy power..
1455*38df6492SMark Einon  */
1456*38df6492SMark Einon static void et1310_phy_power_switch(struct et131x_adapter *adapter, bool down)
1457*38df6492SMark Einon {
1458*38df6492SMark Einon 	u16 data;
1459*38df6492SMark Einon 	struct  phy_device *phydev = adapter->phydev;
1460*38df6492SMark Einon 
1461*38df6492SMark Einon 	et131x_mii_read(adapter, MII_BMCR, &data);
1462*38df6492SMark Einon 	data &= ~BMCR_PDOWN;
1463*38df6492SMark Einon 	if (down)
1464*38df6492SMark Einon 		data |= BMCR_PDOWN;
1465*38df6492SMark Einon 	et131x_mii_write(adapter, phydev->addr, MII_BMCR, data);
1466*38df6492SMark Einon }
1467*38df6492SMark Einon 
1468*38df6492SMark Einon /* et131x_xcvr_init - Init the phy if we are setting it into force mode */
1469*38df6492SMark Einon static void et131x_xcvr_init(struct et131x_adapter *adapter)
1470*38df6492SMark Einon {
1471*38df6492SMark Einon 	u16 lcr2;
1472*38df6492SMark Einon 	struct  phy_device *phydev = adapter->phydev;
1473*38df6492SMark Einon 
1474*38df6492SMark Einon 	/* Set the LED behavior such that LED 1 indicates speed (off =
1475*38df6492SMark Einon 	 * 10Mbits, blink = 100Mbits, on = 1000Mbits) and LED 2 indicates
1476*38df6492SMark Einon 	 * link and activity (on for link, blink off for activity).
1477*38df6492SMark Einon 	 *
1478*38df6492SMark Einon 	 * NOTE: Some customizations have been added here for specific
1479*38df6492SMark Einon 	 * vendors; The LED behavior is now determined by vendor data in the
1480*38df6492SMark Einon 	 * EEPROM. However, the above description is the default.
1481*38df6492SMark Einon 	 */
1482*38df6492SMark Einon 	if ((adapter->eeprom_data[1] & 0x4) == 0) {
1483*38df6492SMark Einon 		et131x_mii_read(adapter, PHY_LED_2, &lcr2);
1484*38df6492SMark Einon 
1485*38df6492SMark Einon 		lcr2 &= (ET_LED2_LED_100TX | ET_LED2_LED_1000T);
1486*38df6492SMark Einon 		lcr2 |= (LED_VAL_LINKON_ACTIVE << LED_LINK_SHIFT);
1487*38df6492SMark Einon 
1488*38df6492SMark Einon 		if ((adapter->eeprom_data[1] & 0x8) == 0)
1489*38df6492SMark Einon 			lcr2 |= (LED_VAL_1000BT_100BTX << LED_TXRX_SHIFT);
1490*38df6492SMark Einon 		else
1491*38df6492SMark Einon 			lcr2 |= (LED_VAL_LINKON << LED_TXRX_SHIFT);
1492*38df6492SMark Einon 
1493*38df6492SMark Einon 		et131x_mii_write(adapter, phydev->addr, PHY_LED_2, lcr2);
1494*38df6492SMark Einon 	}
1495*38df6492SMark Einon }
1496*38df6492SMark Einon 
1497*38df6492SMark Einon /* et131x_configure_global_regs	- configure JAGCore global regs */
1498*38df6492SMark Einon static void et131x_configure_global_regs(struct et131x_adapter *adapter)
1499*38df6492SMark Einon {
1500*38df6492SMark Einon 	struct global_regs __iomem *regs = &adapter->regs->global;
1501*38df6492SMark Einon 
1502*38df6492SMark Einon 	writel(0, &regs->rxq_start_addr);
1503*38df6492SMark Einon 	writel(INTERNAL_MEM_SIZE - 1, &regs->txq_end_addr);
1504*38df6492SMark Einon 
1505*38df6492SMark Einon 	if (adapter->registry_jumbo_packet < 2048) {
1506*38df6492SMark Einon 		/* Tx / RxDMA and Tx/Rx MAC interfaces have a 1k word
1507*38df6492SMark Einon 		 * block of RAM that the driver can split between Tx
1508*38df6492SMark Einon 		 * and Rx as it desires.  Our default is to split it
1509*38df6492SMark Einon 		 * 50/50:
1510*38df6492SMark Einon 		 */
1511*38df6492SMark Einon 		writel(PARM_RX_MEM_END_DEF, &regs->rxq_end_addr);
1512*38df6492SMark Einon 		writel(PARM_RX_MEM_END_DEF + 1, &regs->txq_start_addr);
1513*38df6492SMark Einon 	} else if (adapter->registry_jumbo_packet < 8192) {
1514*38df6492SMark Einon 		/* For jumbo packets > 2k but < 8k, split 50-50. */
1515*38df6492SMark Einon 		writel(INTERNAL_MEM_RX_OFFSET, &regs->rxq_end_addr);
1516*38df6492SMark Einon 		writel(INTERNAL_MEM_RX_OFFSET + 1, &regs->txq_start_addr);
1517*38df6492SMark Einon 	} else {
1518*38df6492SMark Einon 		/* 9216 is the only packet size greater than 8k that
1519*38df6492SMark Einon 		 * is available. The Tx buffer has to be big enough
1520*38df6492SMark Einon 		 * for one whole packet on the Tx side. We'll make
1521*38df6492SMark Einon 		 * the Tx 9408, and give the rest to Rx
1522*38df6492SMark Einon 		 */
1523*38df6492SMark Einon 		writel(0x01b3, &regs->rxq_end_addr);
1524*38df6492SMark Einon 		writel(0x01b4, &regs->txq_start_addr);
1525*38df6492SMark Einon 	}
1526*38df6492SMark Einon 
1527*38df6492SMark Einon 	/* Initialize the loopback register. Disable all loopbacks. */
1528*38df6492SMark Einon 	writel(0, &regs->loopback);
1529*38df6492SMark Einon 
1530*38df6492SMark Einon 	writel(0, &regs->msi_config);
1531*38df6492SMark Einon 
1532*38df6492SMark Einon 	/* By default, disable the watchdog timer.  It will be enabled when
1533*38df6492SMark Einon 	 * a packet is queued.
1534*38df6492SMark Einon 	 */
1535*38df6492SMark Einon 	writel(0, &regs->watchdog_timer);
1536*38df6492SMark Einon }
1537*38df6492SMark Einon 
1538*38df6492SMark Einon /* et131x_config_rx_dma_regs - Start of Rx_DMA init sequence */
1539*38df6492SMark Einon static void et131x_config_rx_dma_regs(struct et131x_adapter *adapter)
1540*38df6492SMark Einon {
1541*38df6492SMark Einon 	struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
1542*38df6492SMark Einon 	struct rx_ring *rx_local = &adapter->rx_ring;
1543*38df6492SMark Einon 	struct fbr_desc *fbr_entry;
1544*38df6492SMark Einon 	u32 entry;
1545*38df6492SMark Einon 	u32 psr_num_des;
1546*38df6492SMark Einon 	unsigned long flags;
1547*38df6492SMark Einon 	u8 id;
1548*38df6492SMark Einon 
1549*38df6492SMark Einon 	et131x_rx_dma_disable(adapter);
1550*38df6492SMark Einon 
1551*38df6492SMark Einon 	/* Load the completion writeback physical address */
1552*38df6492SMark Einon 	writel(upper_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_hi);
1553*38df6492SMark Einon 	writel(lower_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_lo);
1554*38df6492SMark Einon 
1555*38df6492SMark Einon 	memset(rx_local->rx_status_block, 0, sizeof(struct rx_status_block));
1556*38df6492SMark Einon 
1557*38df6492SMark Einon 	/* Set the address and parameters of the packet status ring */
1558*38df6492SMark Einon 	writel(upper_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_hi);
1559*38df6492SMark Einon 	writel(lower_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_lo);
1560*38df6492SMark Einon 	writel(rx_local->psr_entries - 1, &rx_dma->psr_num_des);
1561*38df6492SMark Einon 	writel(0, &rx_dma->psr_full_offset);
1562*38df6492SMark Einon 
1563*38df6492SMark Einon 	psr_num_des = readl(&rx_dma->psr_num_des) & ET_RXDMA_PSR_NUM_DES_MASK;
1564*38df6492SMark Einon 	writel((psr_num_des * LO_MARK_PERCENT_FOR_PSR) / 100,
1565*38df6492SMark Einon 	       &rx_dma->psr_min_des);
1566*38df6492SMark Einon 
1567*38df6492SMark Einon 	spin_lock_irqsave(&adapter->rcv_lock, flags);
1568*38df6492SMark Einon 
1569*38df6492SMark Einon 	/* These local variables track the PSR in the adapter structure */
1570*38df6492SMark Einon 	rx_local->local_psr_full = 0;
1571*38df6492SMark Einon 
1572*38df6492SMark Einon 	for (id = 0; id < NUM_FBRS; id++) {
1573*38df6492SMark Einon 		u32 __iomem *num_des;
1574*38df6492SMark Einon 		u32 __iomem *full_offset;
1575*38df6492SMark Einon 		u32 __iomem *min_des;
1576*38df6492SMark Einon 		u32 __iomem *base_hi;
1577*38df6492SMark Einon 		u32 __iomem *base_lo;
1578*38df6492SMark Einon 		struct fbr_lookup *fbr = rx_local->fbr[id];
1579*38df6492SMark Einon 
1580*38df6492SMark Einon 		if (id == 0) {
1581*38df6492SMark Einon 			num_des = &rx_dma->fbr0_num_des;
1582*38df6492SMark Einon 			full_offset = &rx_dma->fbr0_full_offset;
1583*38df6492SMark Einon 			min_des = &rx_dma->fbr0_min_des;
1584*38df6492SMark Einon 			base_hi = &rx_dma->fbr0_base_hi;
1585*38df6492SMark Einon 			base_lo = &rx_dma->fbr0_base_lo;
1586*38df6492SMark Einon 		} else {
1587*38df6492SMark Einon 			num_des = &rx_dma->fbr1_num_des;
1588*38df6492SMark Einon 			full_offset = &rx_dma->fbr1_full_offset;
1589*38df6492SMark Einon 			min_des = &rx_dma->fbr1_min_des;
1590*38df6492SMark Einon 			base_hi = &rx_dma->fbr1_base_hi;
1591*38df6492SMark Einon 			base_lo = &rx_dma->fbr1_base_lo;
1592*38df6492SMark Einon 		}
1593*38df6492SMark Einon 
1594*38df6492SMark Einon 		/* Now's the best time to initialize FBR contents */
1595*38df6492SMark Einon 		fbr_entry = fbr->ring_virtaddr;
1596*38df6492SMark Einon 		for (entry = 0; entry < fbr->num_entries; entry++) {
1597*38df6492SMark Einon 			fbr_entry->addr_hi = fbr->bus_high[entry];
1598*38df6492SMark Einon 			fbr_entry->addr_lo = fbr->bus_low[entry];
1599*38df6492SMark Einon 			fbr_entry->word2 = entry;
1600*38df6492SMark Einon 			fbr_entry++;
1601*38df6492SMark Einon 		}
1602*38df6492SMark Einon 
1603*38df6492SMark Einon 		/* Set the address and parameters of Free buffer ring 1 and 0 */
1604*38df6492SMark Einon 		writel(upper_32_bits(fbr->ring_physaddr), base_hi);
1605*38df6492SMark Einon 		writel(lower_32_bits(fbr->ring_physaddr), base_lo);
1606*38df6492SMark Einon 		writel(fbr->num_entries - 1, num_des);
1607*38df6492SMark Einon 		writel(ET_DMA10_WRAP, full_offset);
1608*38df6492SMark Einon 
1609*38df6492SMark Einon 		/* This variable tracks the free buffer ring 1 full position,
1610*38df6492SMark Einon 		 * so it has to match the above.
1611*38df6492SMark Einon 		 */
1612*38df6492SMark Einon 		fbr->local_full = ET_DMA10_WRAP;
1613*38df6492SMark Einon 		writel(((fbr->num_entries * LO_MARK_PERCENT_FOR_RX) / 100) - 1,
1614*38df6492SMark Einon 		       min_des);
1615*38df6492SMark Einon 	}
1616*38df6492SMark Einon 
1617*38df6492SMark Einon 	/* Program the number of packets we will receive before generating an
1618*38df6492SMark Einon 	 * interrupt.
1619*38df6492SMark Einon 	 * For version B silicon, this value gets updated once autoneg is
1620*38df6492SMark Einon 	 *complete.
1621*38df6492SMark Einon 	 */
1622*38df6492SMark Einon 	writel(PARM_RX_NUM_BUFS_DEF, &rx_dma->num_pkt_done);
1623*38df6492SMark Einon 
1624*38df6492SMark Einon 	/* The "time_done" is not working correctly to coalesce interrupts
1625*38df6492SMark Einon 	 * after a given time period, but rather is giving us an interrupt
1626*38df6492SMark Einon 	 * regardless of whether we have received packets.
1627*38df6492SMark Einon 	 * This value gets updated once autoneg is complete.
1628*38df6492SMark Einon 	 */
1629*38df6492SMark Einon 	writel(PARM_RX_TIME_INT_DEF, &rx_dma->max_pkt_time);
1630*38df6492SMark Einon 
1631*38df6492SMark Einon 	spin_unlock_irqrestore(&adapter->rcv_lock, flags);
1632*38df6492SMark Einon }
1633*38df6492SMark Einon 
1634*38df6492SMark Einon /* et131x_config_tx_dma_regs - Set up the tx dma section of the JAGCore.
1635*38df6492SMark Einon  *
1636*38df6492SMark Einon  * Configure the transmit engine with the ring buffers we have created
1637*38df6492SMark Einon  * and prepare it for use.
1638*38df6492SMark Einon  */
1639*38df6492SMark Einon static void et131x_config_tx_dma_regs(struct et131x_adapter *adapter)
1640*38df6492SMark Einon {
1641*38df6492SMark Einon 	struct txdma_regs __iomem *txdma = &adapter->regs->txdma;
1642*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
1643*38df6492SMark Einon 
1644*38df6492SMark Einon 	/* Load the hardware with the start of the transmit descriptor ring. */
1645*38df6492SMark Einon 	writel(upper_32_bits(tx_ring->tx_desc_ring_pa), &txdma->pr_base_hi);
1646*38df6492SMark Einon 	writel(lower_32_bits(tx_ring->tx_desc_ring_pa), &txdma->pr_base_lo);
1647*38df6492SMark Einon 
1648*38df6492SMark Einon 	/* Initialise the transmit DMA engine */
1649*38df6492SMark Einon 	writel(NUM_DESC_PER_RING_TX - 1, &txdma->pr_num_des);
1650*38df6492SMark Einon 
1651*38df6492SMark Einon 	/* Load the completion writeback physical address */
1652*38df6492SMark Einon 	writel(upper_32_bits(tx_ring->tx_status_pa), &txdma->dma_wb_base_hi);
1653*38df6492SMark Einon 	writel(lower_32_bits(tx_ring->tx_status_pa), &txdma->dma_wb_base_lo);
1654*38df6492SMark Einon 
1655*38df6492SMark Einon 	*tx_ring->tx_status = 0;
1656*38df6492SMark Einon 
1657*38df6492SMark Einon 	writel(0, &txdma->service_request);
1658*38df6492SMark Einon 	tx_ring->send_idx = 0;
1659*38df6492SMark Einon }
1660*38df6492SMark Einon 
1661*38df6492SMark Einon /* et131x_adapter_setup - Set the adapter up as per cassini+ documentation */
1662*38df6492SMark Einon static void et131x_adapter_setup(struct et131x_adapter *adapter)
1663*38df6492SMark Einon {
1664*38df6492SMark Einon 	et131x_configure_global_regs(adapter);
1665*38df6492SMark Einon 	et1310_config_mac_regs1(adapter);
1666*38df6492SMark Einon 
1667*38df6492SMark Einon 	/* Configure the MMC registers */
1668*38df6492SMark Einon 	/* All we need to do is initialize the Memory Control Register */
1669*38df6492SMark Einon 	writel(ET_MMC_ENABLE, &adapter->regs->mmc.mmc_ctrl);
1670*38df6492SMark Einon 
1671*38df6492SMark Einon 	et1310_config_rxmac_regs(adapter);
1672*38df6492SMark Einon 	et1310_config_txmac_regs(adapter);
1673*38df6492SMark Einon 
1674*38df6492SMark Einon 	et131x_config_rx_dma_regs(adapter);
1675*38df6492SMark Einon 	et131x_config_tx_dma_regs(adapter);
1676*38df6492SMark Einon 
1677*38df6492SMark Einon 	et1310_config_macstat_regs(adapter);
1678*38df6492SMark Einon 
1679*38df6492SMark Einon 	et1310_phy_power_switch(adapter, 0);
1680*38df6492SMark Einon 	et131x_xcvr_init(adapter);
1681*38df6492SMark Einon }
1682*38df6492SMark Einon 
1683*38df6492SMark Einon /* et131x_soft_reset - Issue soft reset to the hardware, complete for ET1310 */
1684*38df6492SMark Einon static void et131x_soft_reset(struct et131x_adapter *adapter)
1685*38df6492SMark Einon {
1686*38df6492SMark Einon 	u32 reg;
1687*38df6492SMark Einon 
1688*38df6492SMark Einon 	/* Disable MAC Core */
1689*38df6492SMark Einon 	reg = ET_MAC_CFG1_SOFT_RESET | ET_MAC_CFG1_SIM_RESET |
1690*38df6492SMark Einon 	      ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
1691*38df6492SMark Einon 	      ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC;
1692*38df6492SMark Einon 	writel(reg, &adapter->regs->mac.cfg1);
1693*38df6492SMark Einon 
1694*38df6492SMark Einon 	reg = ET_RESET_ALL;
1695*38df6492SMark Einon 	writel(reg, &adapter->regs->global.sw_reset);
1696*38df6492SMark Einon 
1697*38df6492SMark Einon 	reg = ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
1698*38df6492SMark Einon 	      ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC;
1699*38df6492SMark Einon 	writel(reg, &adapter->regs->mac.cfg1);
1700*38df6492SMark Einon 	writel(0, &adapter->regs->mac.cfg1);
1701*38df6492SMark Einon }
1702*38df6492SMark Einon 
1703*38df6492SMark Einon static void et131x_enable_interrupts(struct et131x_adapter *adapter)
1704*38df6492SMark Einon {
1705*38df6492SMark Einon 	u32 mask;
1706*38df6492SMark Einon 
1707*38df6492SMark Einon 	if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
1708*38df6492SMark Einon 		mask = INT_MASK_ENABLE;
1709*38df6492SMark Einon 	else
1710*38df6492SMark Einon 		mask = INT_MASK_ENABLE_NO_FLOW;
1711*38df6492SMark Einon 
1712*38df6492SMark Einon 	writel(mask, &adapter->regs->global.int_mask);
1713*38df6492SMark Einon }
1714*38df6492SMark Einon 
1715*38df6492SMark Einon static void et131x_disable_interrupts(struct et131x_adapter *adapter)
1716*38df6492SMark Einon {
1717*38df6492SMark Einon 	writel(INT_MASK_DISABLE, &adapter->regs->global.int_mask);
1718*38df6492SMark Einon }
1719*38df6492SMark Einon 
1720*38df6492SMark Einon static void et131x_tx_dma_disable(struct et131x_adapter *adapter)
1721*38df6492SMark Einon {
1722*38df6492SMark Einon 	/* Setup the transmit dma configuration register */
1723*38df6492SMark Einon 	writel(ET_TXDMA_CSR_HALT | ET_TXDMA_SNGL_EPKT,
1724*38df6492SMark Einon 	       &adapter->regs->txdma.csr);
1725*38df6492SMark Einon }
1726*38df6492SMark Einon 
1727*38df6492SMark Einon static void et131x_enable_txrx(struct net_device *netdev)
1728*38df6492SMark Einon {
1729*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
1730*38df6492SMark Einon 
1731*38df6492SMark Einon 	et131x_rx_dma_enable(adapter);
1732*38df6492SMark Einon 	et131x_tx_dma_enable(adapter);
1733*38df6492SMark Einon 
1734*38df6492SMark Einon 	if (adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE)
1735*38df6492SMark Einon 		et131x_enable_interrupts(adapter);
1736*38df6492SMark Einon 
1737*38df6492SMark Einon 	netif_start_queue(netdev);
1738*38df6492SMark Einon }
1739*38df6492SMark Einon 
1740*38df6492SMark Einon static void et131x_disable_txrx(struct net_device *netdev)
1741*38df6492SMark Einon {
1742*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
1743*38df6492SMark Einon 
1744*38df6492SMark Einon 	netif_stop_queue(netdev);
1745*38df6492SMark Einon 
1746*38df6492SMark Einon 	et131x_rx_dma_disable(adapter);
1747*38df6492SMark Einon 	et131x_tx_dma_disable(adapter);
1748*38df6492SMark Einon 
1749*38df6492SMark Einon 	et131x_disable_interrupts(adapter);
1750*38df6492SMark Einon }
1751*38df6492SMark Einon 
1752*38df6492SMark Einon static void et131x_init_send(struct et131x_adapter *adapter)
1753*38df6492SMark Einon {
1754*38df6492SMark Einon 	int i;
1755*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
1756*38df6492SMark Einon 	struct tcb *tcb = tx_ring->tcb_ring;
1757*38df6492SMark Einon 
1758*38df6492SMark Einon 	tx_ring->tcb_qhead = tcb;
1759*38df6492SMark Einon 
1760*38df6492SMark Einon 	memset(tcb, 0, sizeof(struct tcb) * NUM_TCB);
1761*38df6492SMark Einon 
1762*38df6492SMark Einon 	for (i = 0; i < NUM_TCB; i++) {
1763*38df6492SMark Einon 		tcb->next = tcb + 1;
1764*38df6492SMark Einon 		tcb++;
1765*38df6492SMark Einon 	}
1766*38df6492SMark Einon 
1767*38df6492SMark Einon 	tcb--;
1768*38df6492SMark Einon 	tx_ring->tcb_qtail = tcb;
1769*38df6492SMark Einon 	tcb->next = NULL;
1770*38df6492SMark Einon 	/* Curr send queue should now be empty */
1771*38df6492SMark Einon 	tx_ring->send_head = NULL;
1772*38df6492SMark Einon 	tx_ring->send_tail = NULL;
1773*38df6492SMark Einon }
1774*38df6492SMark Einon 
1775*38df6492SMark Einon /* et1310_enable_phy_coma
1776*38df6492SMark Einon  *
1777*38df6492SMark Einon  * driver receive an phy status change interrupt while in D0 and check that
1778*38df6492SMark Einon  * phy_status is down.
1779*38df6492SMark Einon  *
1780*38df6492SMark Einon  *          -- gate off JAGCore;
1781*38df6492SMark Einon  *          -- set gigE PHY in Coma mode
1782*38df6492SMark Einon  *          -- wake on phy_interrupt; Perform software reset JAGCore,
1783*38df6492SMark Einon  *             re-initialize jagcore and gigE PHY
1784*38df6492SMark Einon  */
1785*38df6492SMark Einon static void et1310_enable_phy_coma(struct et131x_adapter *adapter)
1786*38df6492SMark Einon {
1787*38df6492SMark Einon 	u32 pmcsr = readl(&adapter->regs->global.pm_csr);
1788*38df6492SMark Einon 
1789*38df6492SMark Einon 	/* Stop sending packets. */
1790*38df6492SMark Einon 	adapter->flags |= FMP_ADAPTER_LOWER_POWER;
1791*38df6492SMark Einon 
1792*38df6492SMark Einon 	/* Wait for outstanding Receive packets */
1793*38df6492SMark Einon 	et131x_disable_txrx(adapter->netdev);
1794*38df6492SMark Einon 
1795*38df6492SMark Einon 	/* Gate off JAGCore 3 clock domains */
1796*38df6492SMark Einon 	pmcsr &= ~ET_PMCSR_INIT;
1797*38df6492SMark Einon 	writel(pmcsr, &adapter->regs->global.pm_csr);
1798*38df6492SMark Einon 
1799*38df6492SMark Einon 	/* Program gigE PHY in to Coma mode */
1800*38df6492SMark Einon 	pmcsr |= ET_PM_PHY_SW_COMA;
1801*38df6492SMark Einon 	writel(pmcsr, &adapter->regs->global.pm_csr);
1802*38df6492SMark Einon }
1803*38df6492SMark Einon 
1804*38df6492SMark Einon static void et1310_disable_phy_coma(struct et131x_adapter *adapter)
1805*38df6492SMark Einon {
1806*38df6492SMark Einon 	u32 pmcsr;
1807*38df6492SMark Einon 
1808*38df6492SMark Einon 	pmcsr = readl(&adapter->regs->global.pm_csr);
1809*38df6492SMark Einon 
1810*38df6492SMark Einon 	/* Disable phy_sw_coma register and re-enable JAGCore clocks */
1811*38df6492SMark Einon 	pmcsr |= ET_PMCSR_INIT;
1812*38df6492SMark Einon 	pmcsr &= ~ET_PM_PHY_SW_COMA;
1813*38df6492SMark Einon 	writel(pmcsr, &adapter->regs->global.pm_csr);
1814*38df6492SMark Einon 
1815*38df6492SMark Einon 	/* Restore the GbE PHY speed and duplex modes;
1816*38df6492SMark Einon 	 * Reset JAGCore; re-configure and initialize JAGCore and gigE PHY
1817*38df6492SMark Einon 	 */
1818*38df6492SMark Einon 
1819*38df6492SMark Einon 	/* Re-initialize the send structures */
1820*38df6492SMark Einon 	et131x_init_send(adapter);
1821*38df6492SMark Einon 
1822*38df6492SMark Einon 	/* Bring the device back to the state it was during init prior to
1823*38df6492SMark Einon 	 * autonegotiation being complete.  This way, when we get the auto-neg
1824*38df6492SMark Einon 	 * complete interrupt, we can complete init by calling ConfigMacREGS2.
1825*38df6492SMark Einon 	 */
1826*38df6492SMark Einon 	et131x_soft_reset(adapter);
1827*38df6492SMark Einon 
1828*38df6492SMark Einon 	et131x_adapter_setup(adapter);
1829*38df6492SMark Einon 
1830*38df6492SMark Einon 	/* Allow Tx to restart */
1831*38df6492SMark Einon 	adapter->flags &= ~FMP_ADAPTER_LOWER_POWER;
1832*38df6492SMark Einon 
1833*38df6492SMark Einon 	et131x_enable_txrx(adapter->netdev);
1834*38df6492SMark Einon }
1835*38df6492SMark Einon 
1836*38df6492SMark Einon static inline u32 bump_free_buff_ring(u32 *free_buff_ring, u32 limit)
1837*38df6492SMark Einon {
1838*38df6492SMark Einon 	u32 tmp_free_buff_ring = *free_buff_ring;
1839*38df6492SMark Einon 
1840*38df6492SMark Einon 	tmp_free_buff_ring++;
1841*38df6492SMark Einon 	/* This works for all cases where limit < 1024. The 1023 case
1842*38df6492SMark Einon 	 * works because 1023++ is 1024 which means the if condition is not
1843*38df6492SMark Einon 	 * taken but the carry of the bit into the wrap bit toggles the wrap
1844*38df6492SMark Einon 	 * value correctly
1845*38df6492SMark Einon 	 */
1846*38df6492SMark Einon 	if ((tmp_free_buff_ring & ET_DMA10_MASK) > limit) {
1847*38df6492SMark Einon 		tmp_free_buff_ring &= ~ET_DMA10_MASK;
1848*38df6492SMark Einon 		tmp_free_buff_ring ^= ET_DMA10_WRAP;
1849*38df6492SMark Einon 	}
1850*38df6492SMark Einon 	/* For the 1023 case */
1851*38df6492SMark Einon 	tmp_free_buff_ring &= (ET_DMA10_MASK | ET_DMA10_WRAP);
1852*38df6492SMark Einon 	*free_buff_ring = tmp_free_buff_ring;
1853*38df6492SMark Einon 	return tmp_free_buff_ring;
1854*38df6492SMark Einon }
1855*38df6492SMark Einon 
1856*38df6492SMark Einon /* et131x_rx_dma_memory_alloc
1857*38df6492SMark Einon  *
1858*38df6492SMark Einon  * Allocates Free buffer ring 1 for sure, free buffer ring 0 if required,
1859*38df6492SMark Einon  * and the Packet Status Ring.
1860*38df6492SMark Einon  */
1861*38df6492SMark Einon static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
1862*38df6492SMark Einon {
1863*38df6492SMark Einon 	u8 id;
1864*38df6492SMark Einon 	u32 i, j;
1865*38df6492SMark Einon 	u32 bufsize;
1866*38df6492SMark Einon 	u32 psr_size;
1867*38df6492SMark Einon 	u32 fbr_chunksize;
1868*38df6492SMark Einon 	struct rx_ring *rx_ring = &adapter->rx_ring;
1869*38df6492SMark Einon 	struct fbr_lookup *fbr;
1870*38df6492SMark Einon 
1871*38df6492SMark Einon 	/* Alloc memory for the lookup table */
1872*38df6492SMark Einon 	rx_ring->fbr[0] = kzalloc(sizeof(*fbr), GFP_KERNEL);
1873*38df6492SMark Einon 	if (rx_ring->fbr[0] == NULL)
1874*38df6492SMark Einon 		return -ENOMEM;
1875*38df6492SMark Einon 	rx_ring->fbr[1] = kzalloc(sizeof(*fbr), GFP_KERNEL);
1876*38df6492SMark Einon 	if (rx_ring->fbr[1] == NULL)
1877*38df6492SMark Einon 		return -ENOMEM;
1878*38df6492SMark Einon 
1879*38df6492SMark Einon 	/* The first thing we will do is configure the sizes of the buffer
1880*38df6492SMark Einon 	 * rings. These will change based on jumbo packet support.  Larger
1881*38df6492SMark Einon 	 * jumbo packets increases the size of each entry in FBR0, and the
1882*38df6492SMark Einon 	 * number of entries in FBR0, while at the same time decreasing the
1883*38df6492SMark Einon 	 * number of entries in FBR1.
1884*38df6492SMark Einon 	 *
1885*38df6492SMark Einon 	 * FBR1 holds "large" frames, FBR0 holds "small" frames.  If FBR1
1886*38df6492SMark Einon 	 * entries are huge in order to accommodate a "jumbo" frame, then it
1887*38df6492SMark Einon 	 * will have less entries.  Conversely, FBR1 will now be relied upon
1888*38df6492SMark Einon 	 * to carry more "normal" frames, thus it's entry size also increases
1889*38df6492SMark Einon 	 * and the number of entries goes up too (since it now carries
1890*38df6492SMark Einon 	 * "small" + "regular" packets.
1891*38df6492SMark Einon 	 *
1892*38df6492SMark Einon 	 * In this scheme, we try to maintain 512 entries between the two
1893*38df6492SMark Einon 	 * rings. Also, FBR1 remains a constant size - when it's size doubles
1894*38df6492SMark Einon 	 * the number of entries halves.  FBR0 increases in size, however.
1895*38df6492SMark Einon 	 */
1896*38df6492SMark Einon 	if (adapter->registry_jumbo_packet < 2048) {
1897*38df6492SMark Einon 		rx_ring->fbr[0]->buffsize = 256;
1898*38df6492SMark Einon 		rx_ring->fbr[0]->num_entries = 512;
1899*38df6492SMark Einon 		rx_ring->fbr[1]->buffsize = 2048;
1900*38df6492SMark Einon 		rx_ring->fbr[1]->num_entries = 512;
1901*38df6492SMark Einon 	} else if (adapter->registry_jumbo_packet < 4096) {
1902*38df6492SMark Einon 		rx_ring->fbr[0]->buffsize = 512;
1903*38df6492SMark Einon 		rx_ring->fbr[0]->num_entries = 1024;
1904*38df6492SMark Einon 		rx_ring->fbr[1]->buffsize = 4096;
1905*38df6492SMark Einon 		rx_ring->fbr[1]->num_entries = 512;
1906*38df6492SMark Einon 	} else {
1907*38df6492SMark Einon 		rx_ring->fbr[0]->buffsize = 1024;
1908*38df6492SMark Einon 		rx_ring->fbr[0]->num_entries = 768;
1909*38df6492SMark Einon 		rx_ring->fbr[1]->buffsize = 16384;
1910*38df6492SMark Einon 		rx_ring->fbr[1]->num_entries = 128;
1911*38df6492SMark Einon 	}
1912*38df6492SMark Einon 
1913*38df6492SMark Einon 	rx_ring->psr_entries = rx_ring->fbr[0]->num_entries +
1914*38df6492SMark Einon 			       rx_ring->fbr[1]->num_entries;
1915*38df6492SMark Einon 
1916*38df6492SMark Einon 	for (id = 0; id < NUM_FBRS; id++) {
1917*38df6492SMark Einon 		fbr = rx_ring->fbr[id];
1918*38df6492SMark Einon 		/* Allocate an area of memory for Free Buffer Ring */
1919*38df6492SMark Einon 		bufsize = sizeof(struct fbr_desc) * fbr->num_entries;
1920*38df6492SMark Einon 		fbr->ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
1921*38df6492SMark Einon 							bufsize,
1922*38df6492SMark Einon 							&fbr->ring_physaddr,
1923*38df6492SMark Einon 							GFP_KERNEL);
1924*38df6492SMark Einon 		if (!fbr->ring_virtaddr) {
1925*38df6492SMark Einon 			dev_err(&adapter->pdev->dev,
1926*38df6492SMark Einon 				"Cannot alloc memory for Free Buffer Ring %d\n",
1927*38df6492SMark Einon 				id);
1928*38df6492SMark Einon 			return -ENOMEM;
1929*38df6492SMark Einon 		}
1930*38df6492SMark Einon 	}
1931*38df6492SMark Einon 
1932*38df6492SMark Einon 	for (id = 0; id < NUM_FBRS; id++) {
1933*38df6492SMark Einon 		fbr = rx_ring->fbr[id];
1934*38df6492SMark Einon 		fbr_chunksize = (FBR_CHUNKS * fbr->buffsize);
1935*38df6492SMark Einon 
1936*38df6492SMark Einon 		for (i = 0; i < fbr->num_entries / FBR_CHUNKS; i++) {
1937*38df6492SMark Einon 			dma_addr_t fbr_physaddr;
1938*38df6492SMark Einon 
1939*38df6492SMark Einon 			fbr->mem_virtaddrs[i] = dma_alloc_coherent(
1940*38df6492SMark Einon 					&adapter->pdev->dev, fbr_chunksize,
1941*38df6492SMark Einon 					&fbr->mem_physaddrs[i],
1942*38df6492SMark Einon 					GFP_KERNEL);
1943*38df6492SMark Einon 
1944*38df6492SMark Einon 			if (!fbr->mem_virtaddrs[i]) {
1945*38df6492SMark Einon 				dev_err(&adapter->pdev->dev,
1946*38df6492SMark Einon 					"Could not alloc memory\n");
1947*38df6492SMark Einon 				return -ENOMEM;
1948*38df6492SMark Einon 			}
1949*38df6492SMark Einon 
1950*38df6492SMark Einon 			/* See NOTE in "Save Physical Address" comment above */
1951*38df6492SMark Einon 			fbr_physaddr = fbr->mem_physaddrs[i];
1952*38df6492SMark Einon 
1953*38df6492SMark Einon 			for (j = 0; j < FBR_CHUNKS; j++) {
1954*38df6492SMark Einon 				u32 k = (i * FBR_CHUNKS) + j;
1955*38df6492SMark Einon 
1956*38df6492SMark Einon 				/* Save the Virtual address of this index for
1957*38df6492SMark Einon 				 * quick access later
1958*38df6492SMark Einon 				 */
1959*38df6492SMark Einon 				fbr->virt[k] = (u8 *)fbr->mem_virtaddrs[i] +
1960*38df6492SMark Einon 						   (j * fbr->buffsize);
1961*38df6492SMark Einon 
1962*38df6492SMark Einon 				/* now store the physical address in the
1963*38df6492SMark Einon 				 * descriptor so the device can access it
1964*38df6492SMark Einon 				 */
1965*38df6492SMark Einon 				fbr->bus_high[k] = upper_32_bits(fbr_physaddr);
1966*38df6492SMark Einon 				fbr->bus_low[k] = lower_32_bits(fbr_physaddr);
1967*38df6492SMark Einon 				fbr_physaddr += fbr->buffsize;
1968*38df6492SMark Einon 			}
1969*38df6492SMark Einon 		}
1970*38df6492SMark Einon 	}
1971*38df6492SMark Einon 
1972*38df6492SMark Einon 	/* Allocate an area of memory for FIFO of Packet Status ring entries */
1973*38df6492SMark Einon 	psr_size = sizeof(struct pkt_stat_desc) * rx_ring->psr_entries;
1974*38df6492SMark Einon 
1975*38df6492SMark Einon 	rx_ring->ps_ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
1976*38df6492SMark Einon 						  psr_size,
1977*38df6492SMark Einon 						  &rx_ring->ps_ring_physaddr,
1978*38df6492SMark Einon 						  GFP_KERNEL);
1979*38df6492SMark Einon 
1980*38df6492SMark Einon 	if (!rx_ring->ps_ring_virtaddr) {
1981*38df6492SMark Einon 		dev_err(&adapter->pdev->dev,
1982*38df6492SMark Einon 			"Cannot alloc memory for Packet Status Ring\n");
1983*38df6492SMark Einon 		return -ENOMEM;
1984*38df6492SMark Einon 	}
1985*38df6492SMark Einon 
1986*38df6492SMark Einon 	/* Allocate an area of memory for writeback of status information */
1987*38df6492SMark Einon 	rx_ring->rx_status_block = dma_alloc_coherent(&adapter->pdev->dev,
1988*38df6492SMark Einon 					    sizeof(struct rx_status_block),
1989*38df6492SMark Einon 					    &rx_ring->rx_status_bus,
1990*38df6492SMark Einon 					    GFP_KERNEL);
1991*38df6492SMark Einon 	if (!rx_ring->rx_status_block) {
1992*38df6492SMark Einon 		dev_err(&adapter->pdev->dev,
1993*38df6492SMark Einon 			"Cannot alloc memory for Status Block\n");
1994*38df6492SMark Einon 		return -ENOMEM;
1995*38df6492SMark Einon 	}
1996*38df6492SMark Einon 	rx_ring->num_rfd = NIC_DEFAULT_NUM_RFD;
1997*38df6492SMark Einon 
1998*38df6492SMark Einon 	/* The RFDs are going to be put on lists later on, so initialize the
1999*38df6492SMark Einon 	 * lists now.
2000*38df6492SMark Einon 	 */
2001*38df6492SMark Einon 	INIT_LIST_HEAD(&rx_ring->recv_list);
2002*38df6492SMark Einon 	return 0;
2003*38df6492SMark Einon }
2004*38df6492SMark Einon 
2005*38df6492SMark Einon static void et131x_rx_dma_memory_free(struct et131x_adapter *adapter)
2006*38df6492SMark Einon {
2007*38df6492SMark Einon 	u8 id;
2008*38df6492SMark Einon 	u32 ii;
2009*38df6492SMark Einon 	u32 bufsize;
2010*38df6492SMark Einon 	u32 psr_size;
2011*38df6492SMark Einon 	struct rfd *rfd;
2012*38df6492SMark Einon 	struct rx_ring *rx_ring = &adapter->rx_ring;
2013*38df6492SMark Einon 	struct fbr_lookup *fbr;
2014*38df6492SMark Einon 
2015*38df6492SMark Einon 	/* Free RFDs and associated packet descriptors */
2016*38df6492SMark Einon 	WARN_ON(rx_ring->num_ready_recv != rx_ring->num_rfd);
2017*38df6492SMark Einon 
2018*38df6492SMark Einon 	while (!list_empty(&rx_ring->recv_list)) {
2019*38df6492SMark Einon 		rfd = list_entry(rx_ring->recv_list.next,
2020*38df6492SMark Einon 				 struct rfd, list_node);
2021*38df6492SMark Einon 
2022*38df6492SMark Einon 		list_del(&rfd->list_node);
2023*38df6492SMark Einon 		rfd->skb = NULL;
2024*38df6492SMark Einon 		kfree(rfd);
2025*38df6492SMark Einon 	}
2026*38df6492SMark Einon 
2027*38df6492SMark Einon 	/* Free Free Buffer Rings */
2028*38df6492SMark Einon 	for (id = 0; id < NUM_FBRS; id++) {
2029*38df6492SMark Einon 		fbr = rx_ring->fbr[id];
2030*38df6492SMark Einon 
2031*38df6492SMark Einon 		if (!fbr || !fbr->ring_virtaddr)
2032*38df6492SMark Einon 			continue;
2033*38df6492SMark Einon 
2034*38df6492SMark Einon 		/* First the packet memory */
2035*38df6492SMark Einon 		for (ii = 0; ii < fbr->num_entries / FBR_CHUNKS; ii++) {
2036*38df6492SMark Einon 			if (fbr->mem_virtaddrs[ii]) {
2037*38df6492SMark Einon 				bufsize = fbr->buffsize * FBR_CHUNKS;
2038*38df6492SMark Einon 
2039*38df6492SMark Einon 				dma_free_coherent(&adapter->pdev->dev,
2040*38df6492SMark Einon 						  bufsize,
2041*38df6492SMark Einon 						  fbr->mem_virtaddrs[ii],
2042*38df6492SMark Einon 						  fbr->mem_physaddrs[ii]);
2043*38df6492SMark Einon 
2044*38df6492SMark Einon 				fbr->mem_virtaddrs[ii] = NULL;
2045*38df6492SMark Einon 			}
2046*38df6492SMark Einon 		}
2047*38df6492SMark Einon 
2048*38df6492SMark Einon 		bufsize = sizeof(struct fbr_desc) * fbr->num_entries;
2049*38df6492SMark Einon 
2050*38df6492SMark Einon 		dma_free_coherent(&adapter->pdev->dev,
2051*38df6492SMark Einon 				  bufsize,
2052*38df6492SMark Einon 				  fbr->ring_virtaddr,
2053*38df6492SMark Einon 				  fbr->ring_physaddr);
2054*38df6492SMark Einon 
2055*38df6492SMark Einon 		fbr->ring_virtaddr = NULL;
2056*38df6492SMark Einon 	}
2057*38df6492SMark Einon 
2058*38df6492SMark Einon 	/* Free Packet Status Ring */
2059*38df6492SMark Einon 	if (rx_ring->ps_ring_virtaddr) {
2060*38df6492SMark Einon 		psr_size = sizeof(struct pkt_stat_desc) * rx_ring->psr_entries;
2061*38df6492SMark Einon 
2062*38df6492SMark Einon 		dma_free_coherent(&adapter->pdev->dev, psr_size,
2063*38df6492SMark Einon 				  rx_ring->ps_ring_virtaddr,
2064*38df6492SMark Einon 				  rx_ring->ps_ring_physaddr);
2065*38df6492SMark Einon 
2066*38df6492SMark Einon 		rx_ring->ps_ring_virtaddr = NULL;
2067*38df6492SMark Einon 	}
2068*38df6492SMark Einon 
2069*38df6492SMark Einon 	/* Free area of memory for the writeback of status information */
2070*38df6492SMark Einon 	if (rx_ring->rx_status_block) {
2071*38df6492SMark Einon 		dma_free_coherent(&adapter->pdev->dev,
2072*38df6492SMark Einon 				  sizeof(struct rx_status_block),
2073*38df6492SMark Einon 				  rx_ring->rx_status_block,
2074*38df6492SMark Einon 				  rx_ring->rx_status_bus);
2075*38df6492SMark Einon 		rx_ring->rx_status_block = NULL;
2076*38df6492SMark Einon 	}
2077*38df6492SMark Einon 
2078*38df6492SMark Einon 	/* Free the FBR Lookup Table */
2079*38df6492SMark Einon 	kfree(rx_ring->fbr[0]);
2080*38df6492SMark Einon 	kfree(rx_ring->fbr[1]);
2081*38df6492SMark Einon 
2082*38df6492SMark Einon 	/* Reset Counters */
2083*38df6492SMark Einon 	rx_ring->num_ready_recv = 0;
2084*38df6492SMark Einon }
2085*38df6492SMark Einon 
2086*38df6492SMark Einon /* et131x_init_recv - Initialize receive data structures */
2087*38df6492SMark Einon static int et131x_init_recv(struct et131x_adapter *adapter)
2088*38df6492SMark Einon {
2089*38df6492SMark Einon 	struct rfd *rfd;
2090*38df6492SMark Einon 	u32 rfdct;
2091*38df6492SMark Einon 	struct rx_ring *rx_ring = &adapter->rx_ring;
2092*38df6492SMark Einon 
2093*38df6492SMark Einon 	/* Setup each RFD */
2094*38df6492SMark Einon 	for (rfdct = 0; rfdct < rx_ring->num_rfd; rfdct++) {
2095*38df6492SMark Einon 		rfd = kzalloc(sizeof(*rfd), GFP_ATOMIC | GFP_DMA);
2096*38df6492SMark Einon 		if (!rfd)
2097*38df6492SMark Einon 			return -ENOMEM;
2098*38df6492SMark Einon 
2099*38df6492SMark Einon 		rfd->skb = NULL;
2100*38df6492SMark Einon 
2101*38df6492SMark Einon 		/* Add this RFD to the recv_list */
2102*38df6492SMark Einon 		list_add_tail(&rfd->list_node, &rx_ring->recv_list);
2103*38df6492SMark Einon 
2104*38df6492SMark Einon 		/* Increment the available RFD's */
2105*38df6492SMark Einon 		rx_ring->num_ready_recv++;
2106*38df6492SMark Einon 	}
2107*38df6492SMark Einon 
2108*38df6492SMark Einon 	return 0;
2109*38df6492SMark Einon }
2110*38df6492SMark Einon 
2111*38df6492SMark Einon /* et131x_set_rx_dma_timer - Set the heartbeat timer according to line rate */
2112*38df6492SMark Einon static void et131x_set_rx_dma_timer(struct et131x_adapter *adapter)
2113*38df6492SMark Einon {
2114*38df6492SMark Einon 	struct phy_device *phydev = adapter->phydev;
2115*38df6492SMark Einon 
2116*38df6492SMark Einon 	/* For version B silicon, we do not use the RxDMA timer for 10 and 100
2117*38df6492SMark Einon 	 * Mbits/s line rates. We do not enable and RxDMA interrupt coalescing.
2118*38df6492SMark Einon 	 */
2119*38df6492SMark Einon 	if ((phydev->speed == SPEED_100) || (phydev->speed == SPEED_10)) {
2120*38df6492SMark Einon 		writel(0, &adapter->regs->rxdma.max_pkt_time);
2121*38df6492SMark Einon 		writel(1, &adapter->regs->rxdma.num_pkt_done);
2122*38df6492SMark Einon 	}
2123*38df6492SMark Einon }
2124*38df6492SMark Einon 
2125*38df6492SMark Einon /* nic_return_rfd - Recycle a RFD and put it back onto the receive list */
2126*38df6492SMark Einon static void nic_return_rfd(struct et131x_adapter *adapter, struct rfd *rfd)
2127*38df6492SMark Einon {
2128*38df6492SMark Einon 	struct rx_ring *rx_local = &adapter->rx_ring;
2129*38df6492SMark Einon 	struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
2130*38df6492SMark Einon 	u16 buff_index = rfd->bufferindex;
2131*38df6492SMark Einon 	u8 ring_index = rfd->ringindex;
2132*38df6492SMark Einon 	unsigned long flags;
2133*38df6492SMark Einon 	struct fbr_lookup *fbr = rx_local->fbr[ring_index];
2134*38df6492SMark Einon 
2135*38df6492SMark Einon 	/* We don't use any of the OOB data besides status. Otherwise, we
2136*38df6492SMark Einon 	 * need to clean up OOB data
2137*38df6492SMark Einon 	 */
2138*38df6492SMark Einon 	if (buff_index < fbr->num_entries) {
2139*38df6492SMark Einon 		u32 free_buff_ring;
2140*38df6492SMark Einon 		u32 __iomem *offset;
2141*38df6492SMark Einon 		struct fbr_desc *next;
2142*38df6492SMark Einon 
2143*38df6492SMark Einon 		if (ring_index == 0)
2144*38df6492SMark Einon 			offset = &rx_dma->fbr0_full_offset;
2145*38df6492SMark Einon 		else
2146*38df6492SMark Einon 			offset = &rx_dma->fbr1_full_offset;
2147*38df6492SMark Einon 
2148*38df6492SMark Einon 		next = (struct fbr_desc *)(fbr->ring_virtaddr) +
2149*38df6492SMark Einon 		       INDEX10(fbr->local_full);
2150*38df6492SMark Einon 
2151*38df6492SMark Einon 		/* Handle the Free Buffer Ring advancement here. Write
2152*38df6492SMark Einon 		 * the PA / Buffer Index for the returned buffer into
2153*38df6492SMark Einon 		 * the oldest (next to be freed)FBR entry
2154*38df6492SMark Einon 		 */
2155*38df6492SMark Einon 		next->addr_hi = fbr->bus_high[buff_index];
2156*38df6492SMark Einon 		next->addr_lo = fbr->bus_low[buff_index];
2157*38df6492SMark Einon 		next->word2 = buff_index;
2158*38df6492SMark Einon 
2159*38df6492SMark Einon 		free_buff_ring = bump_free_buff_ring(&fbr->local_full,
2160*38df6492SMark Einon 						     fbr->num_entries - 1);
2161*38df6492SMark Einon 		writel(free_buff_ring, offset);
2162*38df6492SMark Einon 	} else {
2163*38df6492SMark Einon 		dev_err(&adapter->pdev->dev,
2164*38df6492SMark Einon 			"%s illegal Buffer Index returned\n", __func__);
2165*38df6492SMark Einon 	}
2166*38df6492SMark Einon 
2167*38df6492SMark Einon 	/* The processing on this RFD is done, so put it back on the tail of
2168*38df6492SMark Einon 	 * our list
2169*38df6492SMark Einon 	 */
2170*38df6492SMark Einon 	spin_lock_irqsave(&adapter->rcv_lock, flags);
2171*38df6492SMark Einon 	list_add_tail(&rfd->list_node, &rx_local->recv_list);
2172*38df6492SMark Einon 	rx_local->num_ready_recv++;
2173*38df6492SMark Einon 	spin_unlock_irqrestore(&adapter->rcv_lock, flags);
2174*38df6492SMark Einon 
2175*38df6492SMark Einon 	WARN_ON(rx_local->num_ready_recv > rx_local->num_rfd);
2176*38df6492SMark Einon }
2177*38df6492SMark Einon 
2178*38df6492SMark Einon /* nic_rx_pkts - Checks the hardware for available packets
2179*38df6492SMark Einon  *
2180*38df6492SMark Einon  * Checks the hardware for available packets, using completion ring
2181*38df6492SMark Einon  * If packets are available, it gets an RFD from the recv_list, attaches
2182*38df6492SMark Einon  * the packet to it, puts the RFD in the RecvPendList, and also returns
2183*38df6492SMark Einon  * the pointer to the RFD.
2184*38df6492SMark Einon  */
2185*38df6492SMark Einon static struct rfd *nic_rx_pkts(struct et131x_adapter *adapter)
2186*38df6492SMark Einon {
2187*38df6492SMark Einon 	struct rx_ring *rx_local = &adapter->rx_ring;
2188*38df6492SMark Einon 	struct rx_status_block *status;
2189*38df6492SMark Einon 	struct pkt_stat_desc *psr;
2190*38df6492SMark Einon 	struct rfd *rfd;
2191*38df6492SMark Einon 	unsigned long flags;
2192*38df6492SMark Einon 	struct list_head *element;
2193*38df6492SMark Einon 	u8 ring_index;
2194*38df6492SMark Einon 	u16 buff_index;
2195*38df6492SMark Einon 	u32 len;
2196*38df6492SMark Einon 	u32 word0;
2197*38df6492SMark Einon 	u32 word1;
2198*38df6492SMark Einon 	struct sk_buff *skb;
2199*38df6492SMark Einon 	struct fbr_lookup *fbr;
2200*38df6492SMark Einon 
2201*38df6492SMark Einon 	/* RX Status block is written by the DMA engine prior to every
2202*38df6492SMark Einon 	 * interrupt. It contains the next to be used entry in the Packet
2203*38df6492SMark Einon 	 * Status Ring, and also the two Free Buffer rings.
2204*38df6492SMark Einon 	 */
2205*38df6492SMark Einon 	status = rx_local->rx_status_block;
2206*38df6492SMark Einon 	word1 = status->word1 >> 16;
2207*38df6492SMark Einon 
2208*38df6492SMark Einon 	/* Check the PSR and wrap bits do not match */
2209*38df6492SMark Einon 	if ((word1 & 0x1FFF) == (rx_local->local_psr_full & 0x1FFF))
2210*38df6492SMark Einon 		return NULL; /* Looks like this ring is not updated yet */
2211*38df6492SMark Einon 
2212*38df6492SMark Einon 	/* The packet status ring indicates that data is available. */
2213*38df6492SMark Einon 	psr = (struct pkt_stat_desc *)(rx_local->ps_ring_virtaddr) +
2214*38df6492SMark Einon 			(rx_local->local_psr_full & 0xFFF);
2215*38df6492SMark Einon 
2216*38df6492SMark Einon 	/* Grab any information that is required once the PSR is advanced,
2217*38df6492SMark Einon 	 * since we can no longer rely on the memory being accurate
2218*38df6492SMark Einon 	 */
2219*38df6492SMark Einon 	len = psr->word1 & 0xFFFF;
2220*38df6492SMark Einon 	ring_index = (psr->word1 >> 26) & 0x03;
2221*38df6492SMark Einon 	fbr = rx_local->fbr[ring_index];
2222*38df6492SMark Einon 	buff_index = (psr->word1 >> 16) & 0x3FF;
2223*38df6492SMark Einon 	word0 = psr->word0;
2224*38df6492SMark Einon 
2225*38df6492SMark Einon 	/* Indicate that we have used this PSR entry. */
2226*38df6492SMark Einon 	/* FIXME wrap 12 */
2227*38df6492SMark Einon 	add_12bit(&rx_local->local_psr_full, 1);
2228*38df6492SMark Einon 	if ((rx_local->local_psr_full & 0xFFF) > rx_local->psr_entries - 1) {
2229*38df6492SMark Einon 		/* Clear psr full and toggle the wrap bit */
2230*38df6492SMark Einon 		rx_local->local_psr_full &=  ~0xFFF;
2231*38df6492SMark Einon 		rx_local->local_psr_full ^= 0x1000;
2232*38df6492SMark Einon 	}
2233*38df6492SMark Einon 
2234*38df6492SMark Einon 	writel(rx_local->local_psr_full, &adapter->regs->rxdma.psr_full_offset);
2235*38df6492SMark Einon 
2236*38df6492SMark Einon 	if (ring_index > 1 || buff_index > fbr->num_entries - 1) {
2237*38df6492SMark Einon 		/* Illegal buffer or ring index cannot be used by S/W*/
2238*38df6492SMark Einon 		dev_err(&adapter->pdev->dev,
2239*38df6492SMark Einon 			"NICRxPkts PSR Entry %d indicates length of %d and/or bad bi(%d)\n",
2240*38df6492SMark Einon 			rx_local->local_psr_full & 0xFFF, len, buff_index);
2241*38df6492SMark Einon 		return NULL;
2242*38df6492SMark Einon 	}
2243*38df6492SMark Einon 
2244*38df6492SMark Einon 	/* Get and fill the RFD. */
2245*38df6492SMark Einon 	spin_lock_irqsave(&adapter->rcv_lock, flags);
2246*38df6492SMark Einon 
2247*38df6492SMark Einon 	element = rx_local->recv_list.next;
2248*38df6492SMark Einon 	rfd = list_entry(element, struct rfd, list_node);
2249*38df6492SMark Einon 
2250*38df6492SMark Einon 	if (!rfd) {
2251*38df6492SMark Einon 		spin_unlock_irqrestore(&adapter->rcv_lock, flags);
2252*38df6492SMark Einon 		return NULL;
2253*38df6492SMark Einon 	}
2254*38df6492SMark Einon 
2255*38df6492SMark Einon 	list_del(&rfd->list_node);
2256*38df6492SMark Einon 	rx_local->num_ready_recv--;
2257*38df6492SMark Einon 
2258*38df6492SMark Einon 	spin_unlock_irqrestore(&adapter->rcv_lock, flags);
2259*38df6492SMark Einon 
2260*38df6492SMark Einon 	rfd->bufferindex = buff_index;
2261*38df6492SMark Einon 	rfd->ringindex = ring_index;
2262*38df6492SMark Einon 
2263*38df6492SMark Einon 	/* In V1 silicon, there is a bug which screws up filtering of runt
2264*38df6492SMark Einon 	 * packets. Therefore runt packet filtering is disabled in the MAC and
2265*38df6492SMark Einon 	 * the packets are dropped here. They are also counted here.
2266*38df6492SMark Einon 	 */
2267*38df6492SMark Einon 	if (len < (NIC_MIN_PACKET_SIZE + 4)) {
2268*38df6492SMark Einon 		adapter->stats.rx_other_errs++;
2269*38df6492SMark Einon 		rfd->len = 0;
2270*38df6492SMark Einon 		goto out;
2271*38df6492SMark Einon 	}
2272*38df6492SMark Einon 
2273*38df6492SMark Einon 	if ((word0 & ALCATEL_MULTICAST_PKT) && !(word0 & ALCATEL_BROADCAST_PKT))
2274*38df6492SMark Einon 		adapter->stats.multicast_pkts_rcvd++;
2275*38df6492SMark Einon 
2276*38df6492SMark Einon 	rfd->len = len;
2277*38df6492SMark Einon 
2278*38df6492SMark Einon 	skb = dev_alloc_skb(rfd->len + 2);
2279*38df6492SMark Einon 	if (!skb)
2280*38df6492SMark Einon 		return NULL;
2281*38df6492SMark Einon 
2282*38df6492SMark Einon 	adapter->netdev->stats.rx_bytes += rfd->len;
2283*38df6492SMark Einon 
2284*38df6492SMark Einon 	memcpy(skb_put(skb, rfd->len), fbr->virt[buff_index], rfd->len);
2285*38df6492SMark Einon 
2286*38df6492SMark Einon 	skb->protocol = eth_type_trans(skb, adapter->netdev);
2287*38df6492SMark Einon 	skb->ip_summed = CHECKSUM_NONE;
2288*38df6492SMark Einon 	netif_receive_skb(skb);
2289*38df6492SMark Einon 
2290*38df6492SMark Einon out:
2291*38df6492SMark Einon 	nic_return_rfd(adapter, rfd);
2292*38df6492SMark Einon 	return rfd;
2293*38df6492SMark Einon }
2294*38df6492SMark Einon 
2295*38df6492SMark Einon static int et131x_handle_recv_pkts(struct et131x_adapter *adapter, int budget)
2296*38df6492SMark Einon {
2297*38df6492SMark Einon 	struct rfd *rfd = NULL;
2298*38df6492SMark Einon 	int count = 0;
2299*38df6492SMark Einon 	int limit = budget;
2300*38df6492SMark Einon 	bool done = true;
2301*38df6492SMark Einon 	struct rx_ring *rx_ring = &adapter->rx_ring;
2302*38df6492SMark Einon 
2303*38df6492SMark Einon 	if (budget > MAX_PACKETS_HANDLED)
2304*38df6492SMark Einon 		limit = MAX_PACKETS_HANDLED;
2305*38df6492SMark Einon 
2306*38df6492SMark Einon 	/* Process up to available RFD's */
2307*38df6492SMark Einon 	while (count < limit) {
2308*38df6492SMark Einon 		if (list_empty(&rx_ring->recv_list)) {
2309*38df6492SMark Einon 			WARN_ON(rx_ring->num_ready_recv != 0);
2310*38df6492SMark Einon 			done = false;
2311*38df6492SMark Einon 			break;
2312*38df6492SMark Einon 		}
2313*38df6492SMark Einon 
2314*38df6492SMark Einon 		rfd = nic_rx_pkts(adapter);
2315*38df6492SMark Einon 
2316*38df6492SMark Einon 		if (rfd == NULL)
2317*38df6492SMark Einon 			break;
2318*38df6492SMark Einon 
2319*38df6492SMark Einon 		/* Do not receive any packets until a filter has been set.
2320*38df6492SMark Einon 		 * Do not receive any packets until we have link.
2321*38df6492SMark Einon 		 * If length is zero, return the RFD in order to advance the
2322*38df6492SMark Einon 		 * Free buffer ring.
2323*38df6492SMark Einon 		 */
2324*38df6492SMark Einon 		if (!adapter->packet_filter ||
2325*38df6492SMark Einon 		    !netif_carrier_ok(adapter->netdev) ||
2326*38df6492SMark Einon 		    rfd->len == 0)
2327*38df6492SMark Einon 			continue;
2328*38df6492SMark Einon 
2329*38df6492SMark Einon 		adapter->netdev->stats.rx_packets++;
2330*38df6492SMark Einon 
2331*38df6492SMark Einon 		if (rx_ring->num_ready_recv < RFD_LOW_WATER_MARK)
2332*38df6492SMark Einon 			dev_warn(&adapter->pdev->dev, "RFD's are running out\n");
2333*38df6492SMark Einon 
2334*38df6492SMark Einon 		count++;
2335*38df6492SMark Einon 	}
2336*38df6492SMark Einon 
2337*38df6492SMark Einon 	if (count == limit || !done) {
2338*38df6492SMark Einon 		rx_ring->unfinished_receives = true;
2339*38df6492SMark Einon 		writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
2340*38df6492SMark Einon 		       &adapter->regs->global.watchdog_timer);
2341*38df6492SMark Einon 	} else {
2342*38df6492SMark Einon 		/* Watchdog timer will disable itself if appropriate. */
2343*38df6492SMark Einon 		rx_ring->unfinished_receives = false;
2344*38df6492SMark Einon 	}
2345*38df6492SMark Einon 
2346*38df6492SMark Einon 	return count;
2347*38df6492SMark Einon }
2348*38df6492SMark Einon 
2349*38df6492SMark Einon /* et131x_tx_dma_memory_alloc
2350*38df6492SMark Einon  *
2351*38df6492SMark Einon  * Allocates memory that will be visible both to the device and to the CPU.
2352*38df6492SMark Einon  * The OS will pass us packets, pointers to which we will insert in the Tx
2353*38df6492SMark Einon  * Descriptor queue. The device will read this queue to find the packets in
2354*38df6492SMark Einon  * memory. The device will update the "status" in memory each time it xmits a
2355*38df6492SMark Einon  * packet.
2356*38df6492SMark Einon  */
2357*38df6492SMark Einon static int et131x_tx_dma_memory_alloc(struct et131x_adapter *adapter)
2358*38df6492SMark Einon {
2359*38df6492SMark Einon 	int desc_size = 0;
2360*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
2361*38df6492SMark Einon 
2362*38df6492SMark Einon 	/* Allocate memory for the TCB's (Transmit Control Block) */
2363*38df6492SMark Einon 	tx_ring->tcb_ring = kcalloc(NUM_TCB, sizeof(struct tcb),
2364*38df6492SMark Einon 				    GFP_ATOMIC | GFP_DMA);
2365*38df6492SMark Einon 	if (!tx_ring->tcb_ring)
2366*38df6492SMark Einon 		return -ENOMEM;
2367*38df6492SMark Einon 
2368*38df6492SMark Einon 	desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
2369*38df6492SMark Einon 	tx_ring->tx_desc_ring = dma_alloc_coherent(&adapter->pdev->dev,
2370*38df6492SMark Einon 						   desc_size,
2371*38df6492SMark Einon 						   &tx_ring->tx_desc_ring_pa,
2372*38df6492SMark Einon 						   GFP_KERNEL);
2373*38df6492SMark Einon 	if (!tx_ring->tx_desc_ring) {
2374*38df6492SMark Einon 		dev_err(&adapter->pdev->dev,
2375*38df6492SMark Einon 			"Cannot alloc memory for Tx Ring\n");
2376*38df6492SMark Einon 		return -ENOMEM;
2377*38df6492SMark Einon 	}
2378*38df6492SMark Einon 
2379*38df6492SMark Einon 	tx_ring->tx_status = dma_alloc_coherent(&adapter->pdev->dev,
2380*38df6492SMark Einon 						    sizeof(u32),
2381*38df6492SMark Einon 						    &tx_ring->tx_status_pa,
2382*38df6492SMark Einon 						    GFP_KERNEL);
2383*38df6492SMark Einon 	if (!tx_ring->tx_status_pa) {
2384*38df6492SMark Einon 		dev_err(&adapter->pdev->dev,
2385*38df6492SMark Einon 			"Cannot alloc memory for Tx status block\n");
2386*38df6492SMark Einon 		return -ENOMEM;
2387*38df6492SMark Einon 	}
2388*38df6492SMark Einon 	return 0;
2389*38df6492SMark Einon }
2390*38df6492SMark Einon 
2391*38df6492SMark Einon static void et131x_tx_dma_memory_free(struct et131x_adapter *adapter)
2392*38df6492SMark Einon {
2393*38df6492SMark Einon 	int desc_size = 0;
2394*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
2395*38df6492SMark Einon 
2396*38df6492SMark Einon 	if (tx_ring->tx_desc_ring) {
2397*38df6492SMark Einon 		/* Free memory relating to Tx rings here */
2398*38df6492SMark Einon 		desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
2399*38df6492SMark Einon 		dma_free_coherent(&adapter->pdev->dev,
2400*38df6492SMark Einon 				  desc_size,
2401*38df6492SMark Einon 				  tx_ring->tx_desc_ring,
2402*38df6492SMark Einon 				  tx_ring->tx_desc_ring_pa);
2403*38df6492SMark Einon 		tx_ring->tx_desc_ring = NULL;
2404*38df6492SMark Einon 	}
2405*38df6492SMark Einon 
2406*38df6492SMark Einon 	/* Free memory for the Tx status block */
2407*38df6492SMark Einon 	if (tx_ring->tx_status) {
2408*38df6492SMark Einon 		dma_free_coherent(&adapter->pdev->dev,
2409*38df6492SMark Einon 				  sizeof(u32),
2410*38df6492SMark Einon 				  tx_ring->tx_status,
2411*38df6492SMark Einon 				  tx_ring->tx_status_pa);
2412*38df6492SMark Einon 
2413*38df6492SMark Einon 		tx_ring->tx_status = NULL;
2414*38df6492SMark Einon 	}
2415*38df6492SMark Einon 	/* Free the memory for the tcb structures */
2416*38df6492SMark Einon 	kfree(tx_ring->tcb_ring);
2417*38df6492SMark Einon }
2418*38df6492SMark Einon 
2419*38df6492SMark Einon /* nic_send_packet - NIC specific send handler for version B silicon. */
2420*38df6492SMark Einon static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
2421*38df6492SMark Einon {
2422*38df6492SMark Einon 	u32 i;
2423*38df6492SMark Einon 	struct tx_desc desc[24];
2424*38df6492SMark Einon 	u32 frag = 0;
2425*38df6492SMark Einon 	u32 thiscopy, remainder;
2426*38df6492SMark Einon 	struct sk_buff *skb = tcb->skb;
2427*38df6492SMark Einon 	u32 nr_frags = skb_shinfo(skb)->nr_frags + 1;
2428*38df6492SMark Einon 	struct skb_frag_struct *frags = &skb_shinfo(skb)->frags[0];
2429*38df6492SMark Einon 	struct phy_device *phydev = adapter->phydev;
2430*38df6492SMark Einon 	dma_addr_t dma_addr;
2431*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
2432*38df6492SMark Einon 
2433*38df6492SMark Einon 	/* Part of the optimizations of this send routine restrict us to
2434*38df6492SMark Einon 	 * sending 24 fragments at a pass.  In practice we should never see
2435*38df6492SMark Einon 	 * more than 5 fragments.
2436*38df6492SMark Einon 	 */
2437*38df6492SMark Einon 
2438*38df6492SMark Einon 	/* nr_frags should be no more than 18. */
2439*38df6492SMark Einon 	BUILD_BUG_ON(MAX_SKB_FRAGS + 1 > 23);
2440*38df6492SMark Einon 
2441*38df6492SMark Einon 	memset(desc, 0, sizeof(struct tx_desc) * (nr_frags + 1));
2442*38df6492SMark Einon 
2443*38df6492SMark Einon 	for (i = 0; i < nr_frags; i++) {
2444*38df6492SMark Einon 		/* If there is something in this element, lets get a
2445*38df6492SMark Einon 		 * descriptor from the ring and get the necessary data
2446*38df6492SMark Einon 		 */
2447*38df6492SMark Einon 		if (i == 0) {
2448*38df6492SMark Einon 			/* If the fragments are smaller than a standard MTU,
2449*38df6492SMark Einon 			 * then map them to a single descriptor in the Tx
2450*38df6492SMark Einon 			 * Desc ring. However, if they're larger, as is
2451*38df6492SMark Einon 			 * possible with support for jumbo packets, then
2452*38df6492SMark Einon 			 * split them each across 2 descriptors.
2453*38df6492SMark Einon 			 *
2454*38df6492SMark Einon 			 * This will work until we determine why the hardware
2455*38df6492SMark Einon 			 * doesn't seem to like large fragments.
2456*38df6492SMark Einon 			 */
2457*38df6492SMark Einon 			if (skb_headlen(skb) <= 1514) {
2458*38df6492SMark Einon 				/* Low 16bits are length, high is vlan and
2459*38df6492SMark Einon 				 * unused currently so zero
2460*38df6492SMark Einon 				 */
2461*38df6492SMark Einon 				desc[frag].len_vlan = skb_headlen(skb);
2462*38df6492SMark Einon 				dma_addr = dma_map_single(&adapter->pdev->dev,
2463*38df6492SMark Einon 							  skb->data,
2464*38df6492SMark Einon 							  skb_headlen(skb),
2465*38df6492SMark Einon 							  DMA_TO_DEVICE);
2466*38df6492SMark Einon 				desc[frag].addr_lo = lower_32_bits(dma_addr);
2467*38df6492SMark Einon 				desc[frag].addr_hi = upper_32_bits(dma_addr);
2468*38df6492SMark Einon 				frag++;
2469*38df6492SMark Einon 			} else {
2470*38df6492SMark Einon 				desc[frag].len_vlan = skb_headlen(skb) / 2;
2471*38df6492SMark Einon 				dma_addr = dma_map_single(&adapter->pdev->dev,
2472*38df6492SMark Einon 							  skb->data,
2473*38df6492SMark Einon 							  skb_headlen(skb) / 2,
2474*38df6492SMark Einon 							  DMA_TO_DEVICE);
2475*38df6492SMark Einon 				desc[frag].addr_lo = lower_32_bits(dma_addr);
2476*38df6492SMark Einon 				desc[frag].addr_hi = upper_32_bits(dma_addr);
2477*38df6492SMark Einon 				frag++;
2478*38df6492SMark Einon 
2479*38df6492SMark Einon 				desc[frag].len_vlan = skb_headlen(skb) / 2;
2480*38df6492SMark Einon 				dma_addr = dma_map_single(&adapter->pdev->dev,
2481*38df6492SMark Einon 							  skb->data +
2482*38df6492SMark Einon 							  skb_headlen(skb) / 2,
2483*38df6492SMark Einon 							  skb_headlen(skb) / 2,
2484*38df6492SMark Einon 							  DMA_TO_DEVICE);
2485*38df6492SMark Einon 				desc[frag].addr_lo = lower_32_bits(dma_addr);
2486*38df6492SMark Einon 				desc[frag].addr_hi = upper_32_bits(dma_addr);
2487*38df6492SMark Einon 				frag++;
2488*38df6492SMark Einon 			}
2489*38df6492SMark Einon 		} else {
2490*38df6492SMark Einon 			desc[frag].len_vlan = frags[i - 1].size;
2491*38df6492SMark Einon 			dma_addr = skb_frag_dma_map(&adapter->pdev->dev,
2492*38df6492SMark Einon 						    &frags[i - 1],
2493*38df6492SMark Einon 						    0,
2494*38df6492SMark Einon 						    frags[i - 1].size,
2495*38df6492SMark Einon 						    DMA_TO_DEVICE);
2496*38df6492SMark Einon 			desc[frag].addr_lo = lower_32_bits(dma_addr);
2497*38df6492SMark Einon 			desc[frag].addr_hi = upper_32_bits(dma_addr);
2498*38df6492SMark Einon 			frag++;
2499*38df6492SMark Einon 		}
2500*38df6492SMark Einon 	}
2501*38df6492SMark Einon 
2502*38df6492SMark Einon 	if (phydev && phydev->speed == SPEED_1000) {
2503*38df6492SMark Einon 		if (++tx_ring->since_irq == PARM_TX_NUM_BUFS_DEF) {
2504*38df6492SMark Einon 			/* Last element & Interrupt flag */
2505*38df6492SMark Einon 			desc[frag - 1].flags =
2506*38df6492SMark Einon 				    TXDESC_FLAG_INTPROC | TXDESC_FLAG_LASTPKT;
2507*38df6492SMark Einon 			tx_ring->since_irq = 0;
2508*38df6492SMark Einon 		} else { /* Last element */
2509*38df6492SMark Einon 			desc[frag - 1].flags = TXDESC_FLAG_LASTPKT;
2510*38df6492SMark Einon 		}
2511*38df6492SMark Einon 	} else {
2512*38df6492SMark Einon 		desc[frag - 1].flags =
2513*38df6492SMark Einon 				    TXDESC_FLAG_INTPROC | TXDESC_FLAG_LASTPKT;
2514*38df6492SMark Einon 	}
2515*38df6492SMark Einon 
2516*38df6492SMark Einon 	desc[0].flags |= TXDESC_FLAG_FIRSTPKT;
2517*38df6492SMark Einon 
2518*38df6492SMark Einon 	tcb->index_start = tx_ring->send_idx;
2519*38df6492SMark Einon 	tcb->stale = 0;
2520*38df6492SMark Einon 
2521*38df6492SMark Einon 	thiscopy = NUM_DESC_PER_RING_TX - INDEX10(tx_ring->send_idx);
2522*38df6492SMark Einon 
2523*38df6492SMark Einon 	if (thiscopy >= frag) {
2524*38df6492SMark Einon 		remainder = 0;
2525*38df6492SMark Einon 		thiscopy = frag;
2526*38df6492SMark Einon 	} else {
2527*38df6492SMark Einon 		remainder = frag - thiscopy;
2528*38df6492SMark Einon 	}
2529*38df6492SMark Einon 
2530*38df6492SMark Einon 	memcpy(tx_ring->tx_desc_ring + INDEX10(tx_ring->send_idx),
2531*38df6492SMark Einon 	       desc,
2532*38df6492SMark Einon 	       sizeof(struct tx_desc) * thiscopy);
2533*38df6492SMark Einon 
2534*38df6492SMark Einon 	add_10bit(&tx_ring->send_idx, thiscopy);
2535*38df6492SMark Einon 
2536*38df6492SMark Einon 	if (INDEX10(tx_ring->send_idx) == 0 ||
2537*38df6492SMark Einon 	    INDEX10(tx_ring->send_idx) == NUM_DESC_PER_RING_TX) {
2538*38df6492SMark Einon 		tx_ring->send_idx &= ~ET_DMA10_MASK;
2539*38df6492SMark Einon 		tx_ring->send_idx ^= ET_DMA10_WRAP;
2540*38df6492SMark Einon 	}
2541*38df6492SMark Einon 
2542*38df6492SMark Einon 	if (remainder) {
2543*38df6492SMark Einon 		memcpy(tx_ring->tx_desc_ring,
2544*38df6492SMark Einon 		       desc + thiscopy,
2545*38df6492SMark Einon 		       sizeof(struct tx_desc) * remainder);
2546*38df6492SMark Einon 
2547*38df6492SMark Einon 		add_10bit(&tx_ring->send_idx, remainder);
2548*38df6492SMark Einon 	}
2549*38df6492SMark Einon 
2550*38df6492SMark Einon 	if (INDEX10(tx_ring->send_idx) == 0) {
2551*38df6492SMark Einon 		if (tx_ring->send_idx)
2552*38df6492SMark Einon 			tcb->index = NUM_DESC_PER_RING_TX - 1;
2553*38df6492SMark Einon 		else
2554*38df6492SMark Einon 			tcb->index = ET_DMA10_WRAP|(NUM_DESC_PER_RING_TX - 1);
2555*38df6492SMark Einon 	} else {
2556*38df6492SMark Einon 		tcb->index = tx_ring->send_idx - 1;
2557*38df6492SMark Einon 	}
2558*38df6492SMark Einon 
2559*38df6492SMark Einon 	spin_lock(&adapter->tcb_send_qlock);
2560*38df6492SMark Einon 
2561*38df6492SMark Einon 	if (tx_ring->send_tail)
2562*38df6492SMark Einon 		tx_ring->send_tail->next = tcb;
2563*38df6492SMark Einon 	else
2564*38df6492SMark Einon 		tx_ring->send_head = tcb;
2565*38df6492SMark Einon 
2566*38df6492SMark Einon 	tx_ring->send_tail = tcb;
2567*38df6492SMark Einon 
2568*38df6492SMark Einon 	WARN_ON(tcb->next != NULL);
2569*38df6492SMark Einon 
2570*38df6492SMark Einon 	tx_ring->used++;
2571*38df6492SMark Einon 
2572*38df6492SMark Einon 	spin_unlock(&adapter->tcb_send_qlock);
2573*38df6492SMark Einon 
2574*38df6492SMark Einon 	/* Write the new write pointer back to the device. */
2575*38df6492SMark Einon 	writel(tx_ring->send_idx, &adapter->regs->txdma.service_request);
2576*38df6492SMark Einon 
2577*38df6492SMark Einon 	/* For Gig only, we use Tx Interrupt coalescing.  Enable the software
2578*38df6492SMark Einon 	 * timer to wake us up if this packet isn't followed by N more.
2579*38df6492SMark Einon 	 */
2580*38df6492SMark Einon 	if (phydev && phydev->speed == SPEED_1000) {
2581*38df6492SMark Einon 		writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
2582*38df6492SMark Einon 		       &adapter->regs->global.watchdog_timer);
2583*38df6492SMark Einon 	}
2584*38df6492SMark Einon 	return 0;
2585*38df6492SMark Einon }
2586*38df6492SMark Einon 
2587*38df6492SMark Einon static int send_packet(struct sk_buff *skb, struct et131x_adapter *adapter)
2588*38df6492SMark Einon {
2589*38df6492SMark Einon 	int status;
2590*38df6492SMark Einon 	struct tcb *tcb;
2591*38df6492SMark Einon 	unsigned long flags;
2592*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
2593*38df6492SMark Einon 
2594*38df6492SMark Einon 	/* All packets must have at least a MAC address and a protocol type */
2595*38df6492SMark Einon 	if (skb->len < ETH_HLEN)
2596*38df6492SMark Einon 		return -EIO;
2597*38df6492SMark Einon 
2598*38df6492SMark Einon 	spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
2599*38df6492SMark Einon 
2600*38df6492SMark Einon 	tcb = tx_ring->tcb_qhead;
2601*38df6492SMark Einon 
2602*38df6492SMark Einon 	if (tcb == NULL) {
2603*38df6492SMark Einon 		spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
2604*38df6492SMark Einon 		return -ENOMEM;
2605*38df6492SMark Einon 	}
2606*38df6492SMark Einon 
2607*38df6492SMark Einon 	tx_ring->tcb_qhead = tcb->next;
2608*38df6492SMark Einon 
2609*38df6492SMark Einon 	if (tx_ring->tcb_qhead == NULL)
2610*38df6492SMark Einon 		tx_ring->tcb_qtail = NULL;
2611*38df6492SMark Einon 
2612*38df6492SMark Einon 	spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
2613*38df6492SMark Einon 
2614*38df6492SMark Einon 	tcb->skb = skb;
2615*38df6492SMark Einon 	tcb->next = NULL;
2616*38df6492SMark Einon 
2617*38df6492SMark Einon 	status = nic_send_packet(adapter, tcb);
2618*38df6492SMark Einon 
2619*38df6492SMark Einon 	if (status != 0) {
2620*38df6492SMark Einon 		spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
2621*38df6492SMark Einon 
2622*38df6492SMark Einon 		if (tx_ring->tcb_qtail)
2623*38df6492SMark Einon 			tx_ring->tcb_qtail->next = tcb;
2624*38df6492SMark Einon 		else
2625*38df6492SMark Einon 			/* Apparently ready Q is empty. */
2626*38df6492SMark Einon 			tx_ring->tcb_qhead = tcb;
2627*38df6492SMark Einon 
2628*38df6492SMark Einon 		tx_ring->tcb_qtail = tcb;
2629*38df6492SMark Einon 		spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
2630*38df6492SMark Einon 		return status;
2631*38df6492SMark Einon 	}
2632*38df6492SMark Einon 	WARN_ON(tx_ring->used > NUM_TCB);
2633*38df6492SMark Einon 	return 0;
2634*38df6492SMark Einon }
2635*38df6492SMark Einon 
2636*38df6492SMark Einon /* free_send_packet - Recycle a struct tcb */
2637*38df6492SMark Einon static inline void free_send_packet(struct et131x_adapter *adapter,
2638*38df6492SMark Einon 				    struct tcb *tcb)
2639*38df6492SMark Einon {
2640*38df6492SMark Einon 	unsigned long flags;
2641*38df6492SMark Einon 	struct tx_desc *desc = NULL;
2642*38df6492SMark Einon 	struct net_device_stats *stats = &adapter->netdev->stats;
2643*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
2644*38df6492SMark Einon 	u64  dma_addr;
2645*38df6492SMark Einon 
2646*38df6492SMark Einon 	if (tcb->skb) {
2647*38df6492SMark Einon 		stats->tx_bytes += tcb->skb->len;
2648*38df6492SMark Einon 
2649*38df6492SMark Einon 		/* Iterate through the TX descriptors on the ring
2650*38df6492SMark Einon 		 * corresponding to this packet and umap the fragments
2651*38df6492SMark Einon 		 * they point to
2652*38df6492SMark Einon 		 */
2653*38df6492SMark Einon 		do {
2654*38df6492SMark Einon 			desc = tx_ring->tx_desc_ring +
2655*38df6492SMark Einon 			       INDEX10(tcb->index_start);
2656*38df6492SMark Einon 
2657*38df6492SMark Einon 			dma_addr = desc->addr_lo;
2658*38df6492SMark Einon 			dma_addr |= (u64)desc->addr_hi << 32;
2659*38df6492SMark Einon 
2660*38df6492SMark Einon 			dma_unmap_single(&adapter->pdev->dev,
2661*38df6492SMark Einon 					 dma_addr,
2662*38df6492SMark Einon 					 desc->len_vlan, DMA_TO_DEVICE);
2663*38df6492SMark Einon 
2664*38df6492SMark Einon 			add_10bit(&tcb->index_start, 1);
2665*38df6492SMark Einon 			if (INDEX10(tcb->index_start) >=
2666*38df6492SMark Einon 							NUM_DESC_PER_RING_TX) {
2667*38df6492SMark Einon 				tcb->index_start &= ~ET_DMA10_MASK;
2668*38df6492SMark Einon 				tcb->index_start ^= ET_DMA10_WRAP;
2669*38df6492SMark Einon 			}
2670*38df6492SMark Einon 		} while (desc != tx_ring->tx_desc_ring + INDEX10(tcb->index));
2671*38df6492SMark Einon 
2672*38df6492SMark Einon 		dev_kfree_skb_any(tcb->skb);
2673*38df6492SMark Einon 	}
2674*38df6492SMark Einon 
2675*38df6492SMark Einon 	memset(tcb, 0, sizeof(struct tcb));
2676*38df6492SMark Einon 
2677*38df6492SMark Einon 	/* Add the TCB to the Ready Q */
2678*38df6492SMark Einon 	spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
2679*38df6492SMark Einon 
2680*38df6492SMark Einon 	stats->tx_packets++;
2681*38df6492SMark Einon 
2682*38df6492SMark Einon 	if (tx_ring->tcb_qtail)
2683*38df6492SMark Einon 		tx_ring->tcb_qtail->next = tcb;
2684*38df6492SMark Einon 	else /* Apparently ready Q is empty. */
2685*38df6492SMark Einon 		tx_ring->tcb_qhead = tcb;
2686*38df6492SMark Einon 
2687*38df6492SMark Einon 	tx_ring->tcb_qtail = tcb;
2688*38df6492SMark Einon 
2689*38df6492SMark Einon 	spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
2690*38df6492SMark Einon 	WARN_ON(tx_ring->used < 0);
2691*38df6492SMark Einon }
2692*38df6492SMark Einon 
2693*38df6492SMark Einon /* et131x_free_busy_send_packets - Free and complete the stopped active sends */
2694*38df6492SMark Einon static void et131x_free_busy_send_packets(struct et131x_adapter *adapter)
2695*38df6492SMark Einon {
2696*38df6492SMark Einon 	struct tcb *tcb;
2697*38df6492SMark Einon 	unsigned long flags;
2698*38df6492SMark Einon 	u32 freed = 0;
2699*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
2700*38df6492SMark Einon 
2701*38df6492SMark Einon 	/* Any packets being sent? Check the first TCB on the send list */
2702*38df6492SMark Einon 	spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
2703*38df6492SMark Einon 
2704*38df6492SMark Einon 	tcb = tx_ring->send_head;
2705*38df6492SMark Einon 
2706*38df6492SMark Einon 	while (tcb != NULL && freed < NUM_TCB) {
2707*38df6492SMark Einon 		struct tcb *next = tcb->next;
2708*38df6492SMark Einon 
2709*38df6492SMark Einon 		tx_ring->send_head = next;
2710*38df6492SMark Einon 
2711*38df6492SMark Einon 		if (next == NULL)
2712*38df6492SMark Einon 			tx_ring->send_tail = NULL;
2713*38df6492SMark Einon 
2714*38df6492SMark Einon 		tx_ring->used--;
2715*38df6492SMark Einon 
2716*38df6492SMark Einon 		spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
2717*38df6492SMark Einon 
2718*38df6492SMark Einon 		freed++;
2719*38df6492SMark Einon 		free_send_packet(adapter, tcb);
2720*38df6492SMark Einon 
2721*38df6492SMark Einon 		spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
2722*38df6492SMark Einon 
2723*38df6492SMark Einon 		tcb = tx_ring->send_head;
2724*38df6492SMark Einon 	}
2725*38df6492SMark Einon 
2726*38df6492SMark Einon 	WARN_ON(freed == NUM_TCB);
2727*38df6492SMark Einon 
2728*38df6492SMark Einon 	spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
2729*38df6492SMark Einon 
2730*38df6492SMark Einon 	tx_ring->used = 0;
2731*38df6492SMark Einon }
2732*38df6492SMark Einon 
2733*38df6492SMark Einon /* et131x_handle_send_pkts
2734*38df6492SMark Einon  *
2735*38df6492SMark Einon  * Re-claim the send resources, complete sends and get more to send from
2736*38df6492SMark Einon  * the send wait queue.
2737*38df6492SMark Einon  */
2738*38df6492SMark Einon static void et131x_handle_send_pkts(struct et131x_adapter *adapter)
2739*38df6492SMark Einon {
2740*38df6492SMark Einon 	unsigned long flags;
2741*38df6492SMark Einon 	u32 serviced;
2742*38df6492SMark Einon 	struct tcb *tcb;
2743*38df6492SMark Einon 	u32 index;
2744*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
2745*38df6492SMark Einon 
2746*38df6492SMark Einon 	serviced = readl(&adapter->regs->txdma.new_service_complete);
2747*38df6492SMark Einon 	index = INDEX10(serviced);
2748*38df6492SMark Einon 
2749*38df6492SMark Einon 	/* Has the ring wrapped?  Process any descriptors that do not have
2750*38df6492SMark Einon 	 * the same "wrap" indicator as the current completion indicator
2751*38df6492SMark Einon 	 */
2752*38df6492SMark Einon 	spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
2753*38df6492SMark Einon 
2754*38df6492SMark Einon 	tcb = tx_ring->send_head;
2755*38df6492SMark Einon 
2756*38df6492SMark Einon 	while (tcb &&
2757*38df6492SMark Einon 	       ((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
2758*38df6492SMark Einon 	       index < INDEX10(tcb->index)) {
2759*38df6492SMark Einon 		tx_ring->used--;
2760*38df6492SMark Einon 		tx_ring->send_head = tcb->next;
2761*38df6492SMark Einon 		if (tcb->next == NULL)
2762*38df6492SMark Einon 			tx_ring->send_tail = NULL;
2763*38df6492SMark Einon 
2764*38df6492SMark Einon 		spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
2765*38df6492SMark Einon 		free_send_packet(adapter, tcb);
2766*38df6492SMark Einon 		spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
2767*38df6492SMark Einon 
2768*38df6492SMark Einon 		/* Goto the next packet */
2769*38df6492SMark Einon 		tcb = tx_ring->send_head;
2770*38df6492SMark Einon 	}
2771*38df6492SMark Einon 	while (tcb &&
2772*38df6492SMark Einon 	       !((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
2773*38df6492SMark Einon 	       index > (tcb->index & ET_DMA10_MASK)) {
2774*38df6492SMark Einon 		tx_ring->used--;
2775*38df6492SMark Einon 		tx_ring->send_head = tcb->next;
2776*38df6492SMark Einon 		if (tcb->next == NULL)
2777*38df6492SMark Einon 			tx_ring->send_tail = NULL;
2778*38df6492SMark Einon 
2779*38df6492SMark Einon 		spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
2780*38df6492SMark Einon 		free_send_packet(adapter, tcb);
2781*38df6492SMark Einon 		spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
2782*38df6492SMark Einon 
2783*38df6492SMark Einon 		/* Goto the next packet */
2784*38df6492SMark Einon 		tcb = tx_ring->send_head;
2785*38df6492SMark Einon 	}
2786*38df6492SMark Einon 
2787*38df6492SMark Einon 	/* Wake up the queue when we hit a low-water mark */
2788*38df6492SMark Einon 	if (tx_ring->used <= NUM_TCB / 3)
2789*38df6492SMark Einon 		netif_wake_queue(adapter->netdev);
2790*38df6492SMark Einon 
2791*38df6492SMark Einon 	spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
2792*38df6492SMark Einon }
2793*38df6492SMark Einon 
2794*38df6492SMark Einon static int et131x_get_settings(struct net_device *netdev,
2795*38df6492SMark Einon 			       struct ethtool_cmd *cmd)
2796*38df6492SMark Einon {
2797*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
2798*38df6492SMark Einon 
2799*38df6492SMark Einon 	return phy_ethtool_gset(adapter->phydev, cmd);
2800*38df6492SMark Einon }
2801*38df6492SMark Einon 
2802*38df6492SMark Einon static int et131x_set_settings(struct net_device *netdev,
2803*38df6492SMark Einon 			       struct ethtool_cmd *cmd)
2804*38df6492SMark Einon {
2805*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
2806*38df6492SMark Einon 
2807*38df6492SMark Einon 	return phy_ethtool_sset(adapter->phydev, cmd);
2808*38df6492SMark Einon }
2809*38df6492SMark Einon 
2810*38df6492SMark Einon static int et131x_get_regs_len(struct net_device *netdev)
2811*38df6492SMark Einon {
2812*38df6492SMark Einon #define ET131X_REGS_LEN 256
2813*38df6492SMark Einon 	return ET131X_REGS_LEN * sizeof(u32);
2814*38df6492SMark Einon }
2815*38df6492SMark Einon 
2816*38df6492SMark Einon static void et131x_get_regs(struct net_device *netdev,
2817*38df6492SMark Einon 			    struct ethtool_regs *regs, void *regs_data)
2818*38df6492SMark Einon {
2819*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
2820*38df6492SMark Einon 	struct address_map __iomem *aregs = adapter->regs;
2821*38df6492SMark Einon 	u32 *regs_buff = regs_data;
2822*38df6492SMark Einon 	u32 num = 0;
2823*38df6492SMark Einon 	u16 tmp;
2824*38df6492SMark Einon 
2825*38df6492SMark Einon 	memset(regs_data, 0, et131x_get_regs_len(netdev));
2826*38df6492SMark Einon 
2827*38df6492SMark Einon 	regs->version = (1 << 24) | (adapter->pdev->revision << 16) |
2828*38df6492SMark Einon 			adapter->pdev->device;
2829*38df6492SMark Einon 
2830*38df6492SMark Einon 	/* PHY regs */
2831*38df6492SMark Einon 	et131x_mii_read(adapter, MII_BMCR, &tmp);
2832*38df6492SMark Einon 	regs_buff[num++] = tmp;
2833*38df6492SMark Einon 	et131x_mii_read(adapter, MII_BMSR, &tmp);
2834*38df6492SMark Einon 	regs_buff[num++] = tmp;
2835*38df6492SMark Einon 	et131x_mii_read(adapter, MII_PHYSID1, &tmp);
2836*38df6492SMark Einon 	regs_buff[num++] = tmp;
2837*38df6492SMark Einon 	et131x_mii_read(adapter, MII_PHYSID2, &tmp);
2838*38df6492SMark Einon 	regs_buff[num++] = tmp;
2839*38df6492SMark Einon 	et131x_mii_read(adapter, MII_ADVERTISE, &tmp);
2840*38df6492SMark Einon 	regs_buff[num++] = tmp;
2841*38df6492SMark Einon 	et131x_mii_read(adapter, MII_LPA, &tmp);
2842*38df6492SMark Einon 	regs_buff[num++] = tmp;
2843*38df6492SMark Einon 	et131x_mii_read(adapter, MII_EXPANSION, &tmp);
2844*38df6492SMark Einon 	regs_buff[num++] = tmp;
2845*38df6492SMark Einon 	/* Autoneg next page transmit reg */
2846*38df6492SMark Einon 	et131x_mii_read(adapter, 0x07, &tmp);
2847*38df6492SMark Einon 	regs_buff[num++] = tmp;
2848*38df6492SMark Einon 	/* Link partner next page reg */
2849*38df6492SMark Einon 	et131x_mii_read(adapter, 0x08, &tmp);
2850*38df6492SMark Einon 	regs_buff[num++] = tmp;
2851*38df6492SMark Einon 	et131x_mii_read(adapter, MII_CTRL1000, &tmp);
2852*38df6492SMark Einon 	regs_buff[num++] = tmp;
2853*38df6492SMark Einon 	et131x_mii_read(adapter, MII_STAT1000, &tmp);
2854*38df6492SMark Einon 	regs_buff[num++] = tmp;
2855*38df6492SMark Einon 	et131x_mii_read(adapter, 0x0b, &tmp);
2856*38df6492SMark Einon 	regs_buff[num++] = tmp;
2857*38df6492SMark Einon 	et131x_mii_read(adapter, 0x0c, &tmp);
2858*38df6492SMark Einon 	regs_buff[num++] = tmp;
2859*38df6492SMark Einon 	et131x_mii_read(adapter, MII_MMD_CTRL, &tmp);
2860*38df6492SMark Einon 	regs_buff[num++] = tmp;
2861*38df6492SMark Einon 	et131x_mii_read(adapter, MII_MMD_DATA, &tmp);
2862*38df6492SMark Einon 	regs_buff[num++] = tmp;
2863*38df6492SMark Einon 	et131x_mii_read(adapter, MII_ESTATUS, &tmp);
2864*38df6492SMark Einon 	regs_buff[num++] = tmp;
2865*38df6492SMark Einon 
2866*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_INDEX_REG, &tmp);
2867*38df6492SMark Einon 	regs_buff[num++] = tmp;
2868*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_DATA_REG, &tmp);
2869*38df6492SMark Einon 	regs_buff[num++] = tmp;
2870*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG, &tmp);
2871*38df6492SMark Einon 	regs_buff[num++] = tmp;
2872*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL, &tmp);
2873*38df6492SMark Einon 	regs_buff[num++] = tmp;
2874*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL + 1, &tmp);
2875*38df6492SMark Einon 	regs_buff[num++] = tmp;
2876*38df6492SMark Einon 
2877*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_REGISTER_MGMT_CONTROL, &tmp);
2878*38df6492SMark Einon 	regs_buff[num++] = tmp;
2879*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_CONFIG, &tmp);
2880*38df6492SMark Einon 	regs_buff[num++] = tmp;
2881*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_PHY_CONTROL, &tmp);
2882*38df6492SMark Einon 	regs_buff[num++] = tmp;
2883*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_INTERRUPT_MASK, &tmp);
2884*38df6492SMark Einon 	regs_buff[num++] = tmp;
2885*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_INTERRUPT_STATUS, &tmp);
2886*38df6492SMark Einon 	regs_buff[num++] = tmp;
2887*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_PHY_STATUS, &tmp);
2888*38df6492SMark Einon 	regs_buff[num++] = tmp;
2889*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_LED_1, &tmp);
2890*38df6492SMark Einon 	regs_buff[num++] = tmp;
2891*38df6492SMark Einon 	et131x_mii_read(adapter, PHY_LED_2, &tmp);
2892*38df6492SMark Einon 	regs_buff[num++] = tmp;
2893*38df6492SMark Einon 
2894*38df6492SMark Einon 	/* Global regs */
2895*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.txq_start_addr);
2896*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.txq_end_addr);
2897*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.rxq_start_addr);
2898*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.rxq_end_addr);
2899*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.pm_csr);
2900*38df6492SMark Einon 	regs_buff[num++] = adapter->stats.interrupt_status;
2901*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.int_mask);
2902*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.int_alias_clr_en);
2903*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.int_status_alias);
2904*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.sw_reset);
2905*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.slv_timer);
2906*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.msi_config);
2907*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.loopback);
2908*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->global.watchdog_timer);
2909*38df6492SMark Einon 
2910*38df6492SMark Einon 	/* TXDMA regs */
2911*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.csr);
2912*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.pr_base_hi);
2913*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.pr_base_lo);
2914*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.pr_num_des);
2915*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr);
2916*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr_ext);
2917*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.txq_rd_addr);
2918*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_hi);
2919*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_lo);
2920*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.service_request);
2921*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.service_complete);
2922*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.cache_rd_index);
2923*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.cache_wr_index);
2924*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.tx_dma_error);
2925*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.desc_abort_cnt);
2926*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.payload_abort_cnt);
2927*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.writeback_abort_cnt);
2928*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.desc_timeout_cnt);
2929*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.payload_timeout_cnt);
2930*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.writeback_timeout_cnt);
2931*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.desc_error_cnt);
2932*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.payload_error_cnt);
2933*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.writeback_error_cnt);
2934*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.dropped_tlp_cnt);
2935*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.new_service_complete);
2936*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->txdma.ethernet_packet_cnt);
2937*38df6492SMark Einon 
2938*38df6492SMark Einon 	/* RXDMA regs */
2939*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.csr);
2940*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_hi);
2941*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_lo);
2942*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.num_pkt_done);
2943*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.max_pkt_time);
2944*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr);
2945*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr_ext);
2946*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.rxq_wr_addr);
2947*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.psr_base_hi);
2948*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.psr_base_lo);
2949*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.psr_num_des);
2950*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.psr_avail_offset);
2951*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.psr_full_offset);
2952*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.psr_access_index);
2953*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.psr_min_des);
2954*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_lo);
2955*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_hi);
2956*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr0_num_des);
2957*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr0_avail_offset);
2958*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr0_full_offset);
2959*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr0_rd_index);
2960*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr0_min_des);
2961*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_lo);
2962*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_hi);
2963*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr1_num_des);
2964*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr1_avail_offset);
2965*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr1_full_offset);
2966*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr1_rd_index);
2967*38df6492SMark Einon 	regs_buff[num++] = readl(&aregs->rxdma.fbr1_min_des);
2968*38df6492SMark Einon }
2969*38df6492SMark Einon 
2970*38df6492SMark Einon static void et131x_get_drvinfo(struct net_device *netdev,
2971*38df6492SMark Einon 			       struct ethtool_drvinfo *info)
2972*38df6492SMark Einon {
2973*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
2974*38df6492SMark Einon 
2975*38df6492SMark Einon 	strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
2976*38df6492SMark Einon 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
2977*38df6492SMark Einon 	strlcpy(info->bus_info, pci_name(adapter->pdev),
2978*38df6492SMark Einon 		sizeof(info->bus_info));
2979*38df6492SMark Einon }
2980*38df6492SMark Einon 
2981*38df6492SMark Einon static struct ethtool_ops et131x_ethtool_ops = {
2982*38df6492SMark Einon 	.get_settings	= et131x_get_settings,
2983*38df6492SMark Einon 	.set_settings	= et131x_set_settings,
2984*38df6492SMark Einon 	.get_drvinfo	= et131x_get_drvinfo,
2985*38df6492SMark Einon 	.get_regs_len	= et131x_get_regs_len,
2986*38df6492SMark Einon 	.get_regs	= et131x_get_regs,
2987*38df6492SMark Einon 	.get_link	= ethtool_op_get_link,
2988*38df6492SMark Einon };
2989*38df6492SMark Einon 
2990*38df6492SMark Einon /* et131x_hwaddr_init - set up the MAC Address */
2991*38df6492SMark Einon static void et131x_hwaddr_init(struct et131x_adapter *adapter)
2992*38df6492SMark Einon {
2993*38df6492SMark Einon 	/* If have our default mac from init and no mac address from
2994*38df6492SMark Einon 	 * EEPROM then we need to generate the last octet and set it on the
2995*38df6492SMark Einon 	 * device
2996*38df6492SMark Einon 	 */
2997*38df6492SMark Einon 	if (is_zero_ether_addr(adapter->rom_addr)) {
2998*38df6492SMark Einon 		/* We need to randomly generate the last octet so we
2999*38df6492SMark Einon 		 * decrease our chances of setting the mac address to
3000*38df6492SMark Einon 		 * same as another one of our cards in the system
3001*38df6492SMark Einon 		 */
3002*38df6492SMark Einon 		get_random_bytes(&adapter->addr[5], 1);
3003*38df6492SMark Einon 		/* We have the default value in the register we are
3004*38df6492SMark Einon 		 * working with so we need to copy the current
3005*38df6492SMark Einon 		 * address into the permanent address
3006*38df6492SMark Einon 		 */
3007*38df6492SMark Einon 		ether_addr_copy(adapter->rom_addr, adapter->addr);
3008*38df6492SMark Einon 	} else {
3009*38df6492SMark Einon 		/* We do not have an override address, so set the
3010*38df6492SMark Einon 		 * current address to the permanent address and add
3011*38df6492SMark Einon 		 * it to the device
3012*38df6492SMark Einon 		 */
3013*38df6492SMark Einon 		ether_addr_copy(adapter->addr, adapter->rom_addr);
3014*38df6492SMark Einon 	}
3015*38df6492SMark Einon }
3016*38df6492SMark Einon 
3017*38df6492SMark Einon static int et131x_pci_init(struct et131x_adapter *adapter,
3018*38df6492SMark Einon 			   struct pci_dev *pdev)
3019*38df6492SMark Einon {
3020*38df6492SMark Einon 	u16 max_payload;
3021*38df6492SMark Einon 	int i, rc;
3022*38df6492SMark Einon 
3023*38df6492SMark Einon 	rc = et131x_init_eeprom(adapter);
3024*38df6492SMark Einon 	if (rc < 0)
3025*38df6492SMark Einon 		goto out;
3026*38df6492SMark Einon 
3027*38df6492SMark Einon 	if (!pci_is_pcie(pdev)) {
3028*38df6492SMark Einon 		dev_err(&pdev->dev, "Missing PCIe capabilities\n");
3029*38df6492SMark Einon 		goto err_out;
3030*38df6492SMark Einon 	}
3031*38df6492SMark Einon 
3032*38df6492SMark Einon 	/* Program the Ack/Nak latency and replay timers */
3033*38df6492SMark Einon 	max_payload = pdev->pcie_mpss;
3034*38df6492SMark Einon 
3035*38df6492SMark Einon 	if (max_payload < 2) {
3036*38df6492SMark Einon 		static const u16 acknak[2] = { 0x76, 0xD0 };
3037*38df6492SMark Einon 		static const u16 replay[2] = { 0x1E0, 0x2ED };
3038*38df6492SMark Einon 
3039*38df6492SMark Einon 		if (pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
3040*38df6492SMark Einon 					  acknak[max_payload])) {
3041*38df6492SMark Einon 			dev_err(&pdev->dev,
3042*38df6492SMark Einon 				"Could not write PCI config space for ACK/NAK\n");
3043*38df6492SMark Einon 			goto err_out;
3044*38df6492SMark Einon 		}
3045*38df6492SMark Einon 		if (pci_write_config_word(pdev, ET1310_PCI_REPLAY,
3046*38df6492SMark Einon 					  replay[max_payload])) {
3047*38df6492SMark Einon 			dev_err(&pdev->dev,
3048*38df6492SMark Einon 				"Could not write PCI config space for Replay Timer\n");
3049*38df6492SMark Einon 			goto err_out;
3050*38df6492SMark Einon 		}
3051*38df6492SMark Einon 	}
3052*38df6492SMark Einon 
3053*38df6492SMark Einon 	/* l0s and l1 latency timers.  We are using default values.
3054*38df6492SMark Einon 	 * Representing 001 for L0s and 010 for L1
3055*38df6492SMark Einon 	 */
3056*38df6492SMark Einon 	if (pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11)) {
3057*38df6492SMark Einon 		dev_err(&pdev->dev,
3058*38df6492SMark Einon 			"Could not write PCI config space for Latency Timers\n");
3059*38df6492SMark Einon 		goto err_out;
3060*38df6492SMark Einon 	}
3061*38df6492SMark Einon 
3062*38df6492SMark Einon 	/* Change the max read size to 2k */
3063*38df6492SMark Einon 	if (pcie_set_readrq(pdev, 2048)) {
3064*38df6492SMark Einon 		dev_err(&pdev->dev,
3065*38df6492SMark Einon 			"Couldn't change PCI config space for Max read size\n");
3066*38df6492SMark Einon 		goto err_out;
3067*38df6492SMark Einon 	}
3068*38df6492SMark Einon 
3069*38df6492SMark Einon 	/* Get MAC address from config space if an eeprom exists, otherwise
3070*38df6492SMark Einon 	 * the MAC address there will not be valid
3071*38df6492SMark Einon 	 */
3072*38df6492SMark Einon 	if (!adapter->has_eeprom) {
3073*38df6492SMark Einon 		et131x_hwaddr_init(adapter);
3074*38df6492SMark Einon 		return 0;
3075*38df6492SMark Einon 	}
3076*38df6492SMark Einon 
3077*38df6492SMark Einon 	for (i = 0; i < ETH_ALEN; i++) {
3078*38df6492SMark Einon 		if (pci_read_config_byte(pdev, ET1310_PCI_MAC_ADDRESS + i,
3079*38df6492SMark Einon 					 adapter->rom_addr + i)) {
3080*38df6492SMark Einon 			dev_err(&pdev->dev, "Could not read PCI config space for MAC address\n");
3081*38df6492SMark Einon 			goto err_out;
3082*38df6492SMark Einon 		}
3083*38df6492SMark Einon 	}
3084*38df6492SMark Einon 	ether_addr_copy(adapter->addr, adapter->rom_addr);
3085*38df6492SMark Einon out:
3086*38df6492SMark Einon 	return rc;
3087*38df6492SMark Einon err_out:
3088*38df6492SMark Einon 	rc = -EIO;
3089*38df6492SMark Einon 	goto out;
3090*38df6492SMark Einon }
3091*38df6492SMark Einon 
3092*38df6492SMark Einon /* et131x_error_timer_handler
3093*38df6492SMark Einon  * @data: timer-specific variable; here a pointer to our adapter structure
3094*38df6492SMark Einon  *
3095*38df6492SMark Einon  * The routine called when the error timer expires, to track the number of
3096*38df6492SMark Einon  * recurring errors.
3097*38df6492SMark Einon  */
3098*38df6492SMark Einon static void et131x_error_timer_handler(unsigned long data)
3099*38df6492SMark Einon {
3100*38df6492SMark Einon 	struct et131x_adapter *adapter = (struct et131x_adapter *)data;
3101*38df6492SMark Einon 	struct phy_device *phydev = adapter->phydev;
3102*38df6492SMark Einon 
3103*38df6492SMark Einon 	if (et1310_in_phy_coma(adapter)) {
3104*38df6492SMark Einon 		/* Bring the device immediately out of coma, to
3105*38df6492SMark Einon 		 * prevent it from sleeping indefinitely, this
3106*38df6492SMark Einon 		 * mechanism could be improved!
3107*38df6492SMark Einon 		 */
3108*38df6492SMark Einon 		et1310_disable_phy_coma(adapter);
3109*38df6492SMark Einon 		adapter->boot_coma = 20;
3110*38df6492SMark Einon 	} else {
3111*38df6492SMark Einon 		et1310_update_macstat_host_counters(adapter);
3112*38df6492SMark Einon 	}
3113*38df6492SMark Einon 
3114*38df6492SMark Einon 	if (!phydev->link && adapter->boot_coma < 11)
3115*38df6492SMark Einon 		adapter->boot_coma++;
3116*38df6492SMark Einon 
3117*38df6492SMark Einon 	if (adapter->boot_coma == 10) {
3118*38df6492SMark Einon 		if (!phydev->link) {
3119*38df6492SMark Einon 			if (!et1310_in_phy_coma(adapter)) {
3120*38df6492SMark Einon 				/* NOTE - This was originally a 'sync with
3121*38df6492SMark Einon 				 *  interrupt'. How to do that under Linux?
3122*38df6492SMark Einon 				 */
3123*38df6492SMark Einon 				et131x_enable_interrupts(adapter);
3124*38df6492SMark Einon 				et1310_enable_phy_coma(adapter);
3125*38df6492SMark Einon 			}
3126*38df6492SMark Einon 		}
3127*38df6492SMark Einon 	}
3128*38df6492SMark Einon 
3129*38df6492SMark Einon 	/* This is a periodic timer, so reschedule */
3130*38df6492SMark Einon 	mod_timer(&adapter->error_timer, jiffies + TX_ERROR_PERIOD * HZ / 1000);
3131*38df6492SMark Einon }
3132*38df6492SMark Einon 
3133*38df6492SMark Einon static void et131x_adapter_memory_free(struct et131x_adapter *adapter)
3134*38df6492SMark Einon {
3135*38df6492SMark Einon 	et131x_tx_dma_memory_free(adapter);
3136*38df6492SMark Einon 	et131x_rx_dma_memory_free(adapter);
3137*38df6492SMark Einon }
3138*38df6492SMark Einon 
3139*38df6492SMark Einon static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
3140*38df6492SMark Einon {
3141*38df6492SMark Einon 	int status;
3142*38df6492SMark Einon 
3143*38df6492SMark Einon 	status = et131x_tx_dma_memory_alloc(adapter);
3144*38df6492SMark Einon 	if (status) {
3145*38df6492SMark Einon 		dev_err(&adapter->pdev->dev,
3146*38df6492SMark Einon 			"et131x_tx_dma_memory_alloc FAILED\n");
3147*38df6492SMark Einon 		et131x_tx_dma_memory_free(adapter);
3148*38df6492SMark Einon 		return status;
3149*38df6492SMark Einon 	}
3150*38df6492SMark Einon 
3151*38df6492SMark Einon 	status = et131x_rx_dma_memory_alloc(adapter);
3152*38df6492SMark Einon 	if (status) {
3153*38df6492SMark Einon 		dev_err(&adapter->pdev->dev,
3154*38df6492SMark Einon 			"et131x_rx_dma_memory_alloc FAILED\n");
3155*38df6492SMark Einon 		et131x_adapter_memory_free(adapter);
3156*38df6492SMark Einon 		return status;
3157*38df6492SMark Einon 	}
3158*38df6492SMark Einon 
3159*38df6492SMark Einon 	status = et131x_init_recv(adapter);
3160*38df6492SMark Einon 	if (status) {
3161*38df6492SMark Einon 		dev_err(&adapter->pdev->dev, "et131x_init_recv FAILED\n");
3162*38df6492SMark Einon 		et131x_adapter_memory_free(adapter);
3163*38df6492SMark Einon 	}
3164*38df6492SMark Einon 	return status;
3165*38df6492SMark Einon }
3166*38df6492SMark Einon 
3167*38df6492SMark Einon static void et131x_adjust_link(struct net_device *netdev)
3168*38df6492SMark Einon {
3169*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3170*38df6492SMark Einon 	struct  phy_device *phydev = adapter->phydev;
3171*38df6492SMark Einon 
3172*38df6492SMark Einon 	if (!phydev)
3173*38df6492SMark Einon 		return;
3174*38df6492SMark Einon 	if (phydev->link == adapter->link)
3175*38df6492SMark Einon 		return;
3176*38df6492SMark Einon 
3177*38df6492SMark Einon 	/* Check to see if we are in coma mode and if
3178*38df6492SMark Einon 	 * so, disable it because we will not be able
3179*38df6492SMark Einon 	 * to read PHY values until we are out.
3180*38df6492SMark Einon 	 */
3181*38df6492SMark Einon 	if (et1310_in_phy_coma(adapter))
3182*38df6492SMark Einon 		et1310_disable_phy_coma(adapter);
3183*38df6492SMark Einon 
3184*38df6492SMark Einon 	adapter->link = phydev->link;
3185*38df6492SMark Einon 	phy_print_status(phydev);
3186*38df6492SMark Einon 
3187*38df6492SMark Einon 	if (phydev->link) {
3188*38df6492SMark Einon 		adapter->boot_coma = 20;
3189*38df6492SMark Einon 		if (phydev->speed == SPEED_10) {
3190*38df6492SMark Einon 			u16 register18;
3191*38df6492SMark Einon 
3192*38df6492SMark Einon 			et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
3193*38df6492SMark Einon 					&register18);
3194*38df6492SMark Einon 			et131x_mii_write(adapter, phydev->addr,
3195*38df6492SMark Einon 					 PHY_MPHY_CONTROL_REG,
3196*38df6492SMark Einon 					 register18 | 0x4);
3197*38df6492SMark Einon 			et131x_mii_write(adapter, phydev->addr, PHY_INDEX_REG,
3198*38df6492SMark Einon 					 register18 | 0x8402);
3199*38df6492SMark Einon 			et131x_mii_write(adapter, phydev->addr, PHY_DATA_REG,
3200*38df6492SMark Einon 					 register18 | 511);
3201*38df6492SMark Einon 			et131x_mii_write(adapter, phydev->addr,
3202*38df6492SMark Einon 					 PHY_MPHY_CONTROL_REG, register18);
3203*38df6492SMark Einon 		}
3204*38df6492SMark Einon 
3205*38df6492SMark Einon 		et1310_config_flow_control(adapter);
3206*38df6492SMark Einon 
3207*38df6492SMark Einon 		if (phydev->speed == SPEED_1000 &&
3208*38df6492SMark Einon 		    adapter->registry_jumbo_packet > 2048) {
3209*38df6492SMark Einon 			u16 reg;
3210*38df6492SMark Einon 
3211*38df6492SMark Einon 			et131x_mii_read(adapter, PHY_CONFIG, &reg);
3212*38df6492SMark Einon 			reg &= ~ET_PHY_CONFIG_TX_FIFO_DEPTH;
3213*38df6492SMark Einon 			reg |= ET_PHY_CONFIG_FIFO_DEPTH_32;
3214*38df6492SMark Einon 			et131x_mii_write(adapter, phydev->addr, PHY_CONFIG,
3215*38df6492SMark Einon 					 reg);
3216*38df6492SMark Einon 		}
3217*38df6492SMark Einon 
3218*38df6492SMark Einon 		et131x_set_rx_dma_timer(adapter);
3219*38df6492SMark Einon 		et1310_config_mac_regs2(adapter);
3220*38df6492SMark Einon 	} else {
3221*38df6492SMark Einon 		adapter->boot_coma = 0;
3222*38df6492SMark Einon 
3223*38df6492SMark Einon 		if (phydev->speed == SPEED_10) {
3224*38df6492SMark Einon 			u16 register18;
3225*38df6492SMark Einon 
3226*38df6492SMark Einon 			et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
3227*38df6492SMark Einon 					&register18);
3228*38df6492SMark Einon 			et131x_mii_write(adapter, phydev->addr,
3229*38df6492SMark Einon 					 PHY_MPHY_CONTROL_REG,
3230*38df6492SMark Einon 					 register18 | 0x4);
3231*38df6492SMark Einon 			et131x_mii_write(adapter, phydev->addr,
3232*38df6492SMark Einon 					 PHY_INDEX_REG, register18 | 0x8402);
3233*38df6492SMark Einon 			et131x_mii_write(adapter, phydev->addr,
3234*38df6492SMark Einon 					 PHY_DATA_REG, register18 | 511);
3235*38df6492SMark Einon 			et131x_mii_write(adapter, phydev->addr,
3236*38df6492SMark Einon 					 PHY_MPHY_CONTROL_REG, register18);
3237*38df6492SMark Einon 		}
3238*38df6492SMark Einon 
3239*38df6492SMark Einon 		et131x_free_busy_send_packets(adapter);
3240*38df6492SMark Einon 		et131x_init_send(adapter);
3241*38df6492SMark Einon 
3242*38df6492SMark Einon 		/* Bring the device back to the state it was during
3243*38df6492SMark Einon 		 * init prior to autonegotiation being complete. This
3244*38df6492SMark Einon 		 * way, when we get the auto-neg complete interrupt,
3245*38df6492SMark Einon 		 * we can complete init by calling config_mac_regs2.
3246*38df6492SMark Einon 		 */
3247*38df6492SMark Einon 		et131x_soft_reset(adapter);
3248*38df6492SMark Einon 
3249*38df6492SMark Einon 		et131x_adapter_setup(adapter);
3250*38df6492SMark Einon 
3251*38df6492SMark Einon 		et131x_disable_txrx(netdev);
3252*38df6492SMark Einon 		et131x_enable_txrx(netdev);
3253*38df6492SMark Einon 	}
3254*38df6492SMark Einon }
3255*38df6492SMark Einon 
3256*38df6492SMark Einon static int et131x_mii_probe(struct net_device *netdev)
3257*38df6492SMark Einon {
3258*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3259*38df6492SMark Einon 	struct  phy_device *phydev = NULL;
3260*38df6492SMark Einon 
3261*38df6492SMark Einon 	phydev = phy_find_first(adapter->mii_bus);
3262*38df6492SMark Einon 	if (!phydev) {
3263*38df6492SMark Einon 		dev_err(&adapter->pdev->dev, "no PHY found\n");
3264*38df6492SMark Einon 		return -ENODEV;
3265*38df6492SMark Einon 	}
3266*38df6492SMark Einon 
3267*38df6492SMark Einon 	phydev = phy_connect(netdev, dev_name(&phydev->dev),
3268*38df6492SMark Einon 			     &et131x_adjust_link, PHY_INTERFACE_MODE_MII);
3269*38df6492SMark Einon 
3270*38df6492SMark Einon 	if (IS_ERR(phydev)) {
3271*38df6492SMark Einon 		dev_err(&adapter->pdev->dev, "Could not attach to PHY\n");
3272*38df6492SMark Einon 		return PTR_ERR(phydev);
3273*38df6492SMark Einon 	}
3274*38df6492SMark Einon 
3275*38df6492SMark Einon 	phydev->supported &= (SUPPORTED_10baseT_Half |
3276*38df6492SMark Einon 			      SUPPORTED_10baseT_Full |
3277*38df6492SMark Einon 			      SUPPORTED_100baseT_Half |
3278*38df6492SMark Einon 			      SUPPORTED_100baseT_Full |
3279*38df6492SMark Einon 			      SUPPORTED_Autoneg |
3280*38df6492SMark Einon 			      SUPPORTED_MII |
3281*38df6492SMark Einon 			      SUPPORTED_TP);
3282*38df6492SMark Einon 
3283*38df6492SMark Einon 	if (adapter->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
3284*38df6492SMark Einon 		phydev->supported |= SUPPORTED_1000baseT_Half |
3285*38df6492SMark Einon 				     SUPPORTED_1000baseT_Full;
3286*38df6492SMark Einon 
3287*38df6492SMark Einon 	phydev->advertising = phydev->supported;
3288*38df6492SMark Einon 	phydev->autoneg = AUTONEG_ENABLE;
3289*38df6492SMark Einon 	adapter->phydev = phydev;
3290*38df6492SMark Einon 
3291*38df6492SMark Einon 	dev_info(&adapter->pdev->dev,
3292*38df6492SMark Einon 		 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
3293*38df6492SMark Einon 		 phydev->drv->name, dev_name(&phydev->dev));
3294*38df6492SMark Einon 
3295*38df6492SMark Einon 	return 0;
3296*38df6492SMark Einon }
3297*38df6492SMark Einon 
3298*38df6492SMark Einon static struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
3299*38df6492SMark Einon 						  struct pci_dev *pdev)
3300*38df6492SMark Einon {
3301*38df6492SMark Einon 	static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
3302*38df6492SMark Einon 
3303*38df6492SMark Einon 	struct et131x_adapter *adapter;
3304*38df6492SMark Einon 
3305*38df6492SMark Einon 	adapter = netdev_priv(netdev);
3306*38df6492SMark Einon 	adapter->pdev = pci_dev_get(pdev);
3307*38df6492SMark Einon 	adapter->netdev = netdev;
3308*38df6492SMark Einon 
3309*38df6492SMark Einon 	spin_lock_init(&adapter->tcb_send_qlock);
3310*38df6492SMark Einon 	spin_lock_init(&adapter->tcb_ready_qlock);
3311*38df6492SMark Einon 	spin_lock_init(&adapter->rcv_lock);
3312*38df6492SMark Einon 
3313*38df6492SMark Einon 	adapter->registry_jumbo_packet = 1514;	/* 1514-9216 */
3314*38df6492SMark Einon 
3315*38df6492SMark Einon 	ether_addr_copy(adapter->addr, default_mac);
3316*38df6492SMark Einon 
3317*38df6492SMark Einon 	return adapter;
3318*38df6492SMark Einon }
3319*38df6492SMark Einon 
3320*38df6492SMark Einon static void et131x_pci_remove(struct pci_dev *pdev)
3321*38df6492SMark Einon {
3322*38df6492SMark Einon 	struct net_device *netdev = pci_get_drvdata(pdev);
3323*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3324*38df6492SMark Einon 
3325*38df6492SMark Einon 	unregister_netdev(netdev);
3326*38df6492SMark Einon 	netif_napi_del(&adapter->napi);
3327*38df6492SMark Einon 	phy_disconnect(adapter->phydev);
3328*38df6492SMark Einon 	mdiobus_unregister(adapter->mii_bus);
3329*38df6492SMark Einon 	kfree(adapter->mii_bus->irq);
3330*38df6492SMark Einon 	mdiobus_free(adapter->mii_bus);
3331*38df6492SMark Einon 
3332*38df6492SMark Einon 	et131x_adapter_memory_free(adapter);
3333*38df6492SMark Einon 	iounmap(adapter->regs);
3334*38df6492SMark Einon 	pci_dev_put(pdev);
3335*38df6492SMark Einon 
3336*38df6492SMark Einon 	free_netdev(netdev);
3337*38df6492SMark Einon 	pci_release_regions(pdev);
3338*38df6492SMark Einon 	pci_disable_device(pdev);
3339*38df6492SMark Einon }
3340*38df6492SMark Einon 
3341*38df6492SMark Einon static void et131x_up(struct net_device *netdev)
3342*38df6492SMark Einon {
3343*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3344*38df6492SMark Einon 
3345*38df6492SMark Einon 	et131x_enable_txrx(netdev);
3346*38df6492SMark Einon 	phy_start(adapter->phydev);
3347*38df6492SMark Einon }
3348*38df6492SMark Einon 
3349*38df6492SMark Einon static void et131x_down(struct net_device *netdev)
3350*38df6492SMark Einon {
3351*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3352*38df6492SMark Einon 
3353*38df6492SMark Einon 	/* Save the timestamp for the TX watchdog, prevent a timeout */
3354*38df6492SMark Einon 	netdev->trans_start = jiffies;
3355*38df6492SMark Einon 
3356*38df6492SMark Einon 	phy_stop(adapter->phydev);
3357*38df6492SMark Einon 	et131x_disable_txrx(netdev);
3358*38df6492SMark Einon }
3359*38df6492SMark Einon 
3360*38df6492SMark Einon #ifdef CONFIG_PM_SLEEP
3361*38df6492SMark Einon static int et131x_suspend(struct device *dev)
3362*38df6492SMark Einon {
3363*38df6492SMark Einon 	struct pci_dev *pdev = to_pci_dev(dev);
3364*38df6492SMark Einon 	struct net_device *netdev = pci_get_drvdata(pdev);
3365*38df6492SMark Einon 
3366*38df6492SMark Einon 	if (netif_running(netdev)) {
3367*38df6492SMark Einon 		netif_device_detach(netdev);
3368*38df6492SMark Einon 		et131x_down(netdev);
3369*38df6492SMark Einon 		pci_save_state(pdev);
3370*38df6492SMark Einon 	}
3371*38df6492SMark Einon 
3372*38df6492SMark Einon 	return 0;
3373*38df6492SMark Einon }
3374*38df6492SMark Einon 
3375*38df6492SMark Einon static int et131x_resume(struct device *dev)
3376*38df6492SMark Einon {
3377*38df6492SMark Einon 	struct pci_dev *pdev = to_pci_dev(dev);
3378*38df6492SMark Einon 	struct net_device *netdev = pci_get_drvdata(pdev);
3379*38df6492SMark Einon 
3380*38df6492SMark Einon 	if (netif_running(netdev)) {
3381*38df6492SMark Einon 		pci_restore_state(pdev);
3382*38df6492SMark Einon 		et131x_up(netdev);
3383*38df6492SMark Einon 		netif_device_attach(netdev);
3384*38df6492SMark Einon 	}
3385*38df6492SMark Einon 
3386*38df6492SMark Einon 	return 0;
3387*38df6492SMark Einon }
3388*38df6492SMark Einon #endif
3389*38df6492SMark Einon 
3390*38df6492SMark Einon static SIMPLE_DEV_PM_OPS(et131x_pm_ops, et131x_suspend, et131x_resume);
3391*38df6492SMark Einon 
3392*38df6492SMark Einon static irqreturn_t et131x_isr(int irq, void *dev_id)
3393*38df6492SMark Einon {
3394*38df6492SMark Einon 	bool handled = true;
3395*38df6492SMark Einon 	bool enable_interrupts = true;
3396*38df6492SMark Einon 	struct net_device *netdev = dev_id;
3397*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3398*38df6492SMark Einon 	struct address_map __iomem *iomem = adapter->regs;
3399*38df6492SMark Einon 	struct rx_ring *rx_ring = &adapter->rx_ring;
3400*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
3401*38df6492SMark Einon 	u32 status;
3402*38df6492SMark Einon 
3403*38df6492SMark Einon 	if (!netif_device_present(netdev)) {
3404*38df6492SMark Einon 		handled = false;
3405*38df6492SMark Einon 		enable_interrupts = false;
3406*38df6492SMark Einon 		goto out;
3407*38df6492SMark Einon 	}
3408*38df6492SMark Einon 
3409*38df6492SMark Einon 	et131x_disable_interrupts(adapter);
3410*38df6492SMark Einon 
3411*38df6492SMark Einon 	status = readl(&adapter->regs->global.int_status);
3412*38df6492SMark Einon 
3413*38df6492SMark Einon 	if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
3414*38df6492SMark Einon 		status &= ~INT_MASK_ENABLE;
3415*38df6492SMark Einon 	else
3416*38df6492SMark Einon 		status &= ~INT_MASK_ENABLE_NO_FLOW;
3417*38df6492SMark Einon 
3418*38df6492SMark Einon 	/* Make sure this is our interrupt */
3419*38df6492SMark Einon 	if (!status) {
3420*38df6492SMark Einon 		handled = false;
3421*38df6492SMark Einon 		et131x_enable_interrupts(adapter);
3422*38df6492SMark Einon 		goto out;
3423*38df6492SMark Einon 	}
3424*38df6492SMark Einon 
3425*38df6492SMark Einon 	/* This is our interrupt, so process accordingly */
3426*38df6492SMark Einon 	if (status & ET_INTR_WATCHDOG) {
3427*38df6492SMark Einon 		struct tcb *tcb = tx_ring->send_head;
3428*38df6492SMark Einon 
3429*38df6492SMark Einon 		if (tcb)
3430*38df6492SMark Einon 			if (++tcb->stale > 1)
3431*38df6492SMark Einon 				status |= ET_INTR_TXDMA_ISR;
3432*38df6492SMark Einon 
3433*38df6492SMark Einon 		if (rx_ring->unfinished_receives)
3434*38df6492SMark Einon 			status |= ET_INTR_RXDMA_XFR_DONE;
3435*38df6492SMark Einon 		else if (tcb == NULL)
3436*38df6492SMark Einon 			writel(0, &adapter->regs->global.watchdog_timer);
3437*38df6492SMark Einon 
3438*38df6492SMark Einon 		status &= ~ET_INTR_WATCHDOG;
3439*38df6492SMark Einon 	}
3440*38df6492SMark Einon 
3441*38df6492SMark Einon 	if (status & (ET_INTR_RXDMA_XFR_DONE | ET_INTR_TXDMA_ISR)) {
3442*38df6492SMark Einon 		enable_interrupts = false;
3443*38df6492SMark Einon 		napi_schedule(&adapter->napi);
3444*38df6492SMark Einon 	}
3445*38df6492SMark Einon 
3446*38df6492SMark Einon 	status &= ~(ET_INTR_TXDMA_ISR | ET_INTR_RXDMA_XFR_DONE);
3447*38df6492SMark Einon 
3448*38df6492SMark Einon 	if (!status)
3449*38df6492SMark Einon 		goto out;
3450*38df6492SMark Einon 
3451*38df6492SMark Einon 	if (status & ET_INTR_TXDMA_ERR) {
3452*38df6492SMark Einon 		/* Following read also clears the register (COR) */
3453*38df6492SMark Einon 		u32 txdma_err = readl(&iomem->txdma.tx_dma_error);
3454*38df6492SMark Einon 
3455*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev,
3456*38df6492SMark Einon 			 "TXDMA_ERR interrupt, error = %d\n",
3457*38df6492SMark Einon 			 txdma_err);
3458*38df6492SMark Einon 	}
3459*38df6492SMark Einon 
3460*38df6492SMark Einon 	if (status & (ET_INTR_RXDMA_FB_R0_LOW | ET_INTR_RXDMA_FB_R1_LOW)) {
3461*38df6492SMark Einon 		/* This indicates the number of unused buffers in RXDMA free
3462*38df6492SMark Einon 		 * buffer ring 0 is <= the limit you programmed. Free buffer
3463*38df6492SMark Einon 		 * resources need to be returned.  Free buffers are consumed as
3464*38df6492SMark Einon 		 * packets are passed from the network to the host. The host
3465*38df6492SMark Einon 		 * becomes aware of the packets from the contents of the packet
3466*38df6492SMark Einon 		 * status ring. This ring is queried when the packet done
3467*38df6492SMark Einon 		 * interrupt occurs. Packets are then passed to the OS. When
3468*38df6492SMark Einon 		 * the OS is done with the packets the resources can be
3469*38df6492SMark Einon 		 * returned to the ET1310 for re-use. This interrupt is one
3470*38df6492SMark Einon 		 * method of returning resources.
3471*38df6492SMark Einon 		 */
3472*38df6492SMark Einon 
3473*38df6492SMark Einon 		/*  If the user has flow control on, then we will
3474*38df6492SMark Einon 		 * send a pause packet, otherwise just exit
3475*38df6492SMark Einon 		 */
3476*38df6492SMark Einon 		if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH) {
3477*38df6492SMark Einon 			u32 pm_csr;
3478*38df6492SMark Einon 
3479*38df6492SMark Einon 			/* Tell the device to send a pause packet via the back
3480*38df6492SMark Einon 			 * pressure register (bp req and bp xon/xoff)
3481*38df6492SMark Einon 			 */
3482*38df6492SMark Einon 			pm_csr = readl(&iomem->global.pm_csr);
3483*38df6492SMark Einon 			if (!et1310_in_phy_coma(adapter))
3484*38df6492SMark Einon 				writel(3, &iomem->txmac.bp_ctrl);
3485*38df6492SMark Einon 		}
3486*38df6492SMark Einon 	}
3487*38df6492SMark Einon 
3488*38df6492SMark Einon 	/* Handle Packet Status Ring Low Interrupt */
3489*38df6492SMark Einon 	if (status & ET_INTR_RXDMA_STAT_LOW) {
3490*38df6492SMark Einon 		/* Same idea as with the two Free Buffer Rings. Packets going
3491*38df6492SMark Einon 		 * from the network to the host each consume a free buffer
3492*38df6492SMark Einon 		 * resource and a packet status resource. These resources are
3493*38df6492SMark Einon 		 * passed to the OS. When the OS is done with the resources,
3494*38df6492SMark Einon 		 * they need to be returned to the ET1310. This is one method
3495*38df6492SMark Einon 		 * of returning the resources.
3496*38df6492SMark Einon 		 */
3497*38df6492SMark Einon 	}
3498*38df6492SMark Einon 
3499*38df6492SMark Einon 	if (status & ET_INTR_RXDMA_ERR) {
3500*38df6492SMark Einon 		/* The rxdma_error interrupt is sent when a time-out on a
3501*38df6492SMark Einon 		 * request issued by the JAGCore has occurred or a completion is
3502*38df6492SMark Einon 		 * returned with an un-successful status. In both cases the
3503*38df6492SMark Einon 		 * request is considered complete. The JAGCore will
3504*38df6492SMark Einon 		 * automatically re-try the request in question. Normally
3505*38df6492SMark Einon 		 * information on events like these are sent to the host using
3506*38df6492SMark Einon 		 * the "Advanced Error Reporting" capability. This interrupt is
3507*38df6492SMark Einon 		 * another way of getting similar information. The only thing
3508*38df6492SMark Einon 		 * required is to clear the interrupt by reading the ISR in the
3509*38df6492SMark Einon 		 * global resources. The JAGCore will do a re-try on the
3510*38df6492SMark Einon 		 * request. Normally you should never see this interrupt. If
3511*38df6492SMark Einon 		 * you start to see this interrupt occurring frequently then
3512*38df6492SMark Einon 		 * something bad has occurred. A reset might be the thing to do.
3513*38df6492SMark Einon 		 */
3514*38df6492SMark Einon 		/* TRAP();*/
3515*38df6492SMark Einon 
3516*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev, "RxDMA_ERR interrupt, error %x\n",
3517*38df6492SMark Einon 			 readl(&iomem->txmac.tx_test));
3518*38df6492SMark Einon 	}
3519*38df6492SMark Einon 
3520*38df6492SMark Einon 	/* Handle the Wake on LAN Event */
3521*38df6492SMark Einon 	if (status & ET_INTR_WOL) {
3522*38df6492SMark Einon 		/* This is a secondary interrupt for wake on LAN. The driver
3523*38df6492SMark Einon 		 * should never see this, if it does, something serious is
3524*38df6492SMark Einon 		 * wrong.
3525*38df6492SMark Einon 		 */
3526*38df6492SMark Einon 		dev_err(&adapter->pdev->dev, "WAKE_ON_LAN interrupt\n");
3527*38df6492SMark Einon 	}
3528*38df6492SMark Einon 
3529*38df6492SMark Einon 	if (status & ET_INTR_TXMAC) {
3530*38df6492SMark Einon 		u32 err = readl(&iomem->txmac.err);
3531*38df6492SMark Einon 
3532*38df6492SMark Einon 		/* When any of the errors occur and TXMAC generates an
3533*38df6492SMark Einon 		 * interrupt to report these errors, it usually means that
3534*38df6492SMark Einon 		 * TXMAC has detected an error in the data stream retrieved
3535*38df6492SMark Einon 		 * from the on-chip Tx Q. All of these errors are catastrophic
3536*38df6492SMark Einon 		 * and TXMAC won't be able to recover data when these errors
3537*38df6492SMark Einon 		 * occur. In a nutshell, the whole Tx path will have to be reset
3538*38df6492SMark Einon 		 * and re-configured afterwards.
3539*38df6492SMark Einon 		 */
3540*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev, "TXMAC interrupt, error 0x%08x\n",
3541*38df6492SMark Einon 			 err);
3542*38df6492SMark Einon 
3543*38df6492SMark Einon 		/* If we are debugging, we want to see this error, otherwise we
3544*38df6492SMark Einon 		 * just want the device to be reset and continue
3545*38df6492SMark Einon 		 */
3546*38df6492SMark Einon 	}
3547*38df6492SMark Einon 
3548*38df6492SMark Einon 	if (status & ET_INTR_RXMAC) {
3549*38df6492SMark Einon 		/* These interrupts are catastrophic to the device, what we need
3550*38df6492SMark Einon 		 * to do is disable the interrupts and set the flag to cause us
3551*38df6492SMark Einon 		 * to reset so we can solve this issue.
3552*38df6492SMark Einon 		 */
3553*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev,
3554*38df6492SMark Einon 			 "RXMAC interrupt, error 0x%08x.  Requesting reset\n",
3555*38df6492SMark Einon 			 readl(&iomem->rxmac.err_reg));
3556*38df6492SMark Einon 
3557*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev,
3558*38df6492SMark Einon 			 "Enable 0x%08x, Diag 0x%08x\n",
3559*38df6492SMark Einon 			 readl(&iomem->rxmac.ctrl),
3560*38df6492SMark Einon 			 readl(&iomem->rxmac.rxq_diag));
3561*38df6492SMark Einon 
3562*38df6492SMark Einon 		/* If we are debugging, we want to see this error, otherwise we
3563*38df6492SMark Einon 		 * just want the device to be reset and continue
3564*38df6492SMark Einon 		 */
3565*38df6492SMark Einon 	}
3566*38df6492SMark Einon 
3567*38df6492SMark Einon 	if (status & ET_INTR_MAC_STAT) {
3568*38df6492SMark Einon 		/* This means at least one of the un-masked counters in the
3569*38df6492SMark Einon 		 * MAC_STAT block has rolled over. Use this to maintain the top,
3570*38df6492SMark Einon 		 * software managed bits of the counter(s).
3571*38df6492SMark Einon 		 */
3572*38df6492SMark Einon 		et1310_handle_macstat_interrupt(adapter);
3573*38df6492SMark Einon 	}
3574*38df6492SMark Einon 
3575*38df6492SMark Einon 	if (status & ET_INTR_SLV_TIMEOUT) {
3576*38df6492SMark Einon 		/* This means a timeout has occurred on a read or write request
3577*38df6492SMark Einon 		 * to one of the JAGCore registers. The Global Resources block
3578*38df6492SMark Einon 		 * has terminated the request and on a read request, returned a
3579*38df6492SMark Einon 		 * "fake" value. The most likely reasons are: Bad Address or the
3580*38df6492SMark Einon 		 * addressed module is in a power-down state and can't respond.
3581*38df6492SMark Einon 		 */
3582*38df6492SMark Einon 	}
3583*38df6492SMark Einon 
3584*38df6492SMark Einon out:
3585*38df6492SMark Einon 	if (enable_interrupts)
3586*38df6492SMark Einon 		et131x_enable_interrupts(adapter);
3587*38df6492SMark Einon 
3588*38df6492SMark Einon 	return IRQ_RETVAL(handled);
3589*38df6492SMark Einon }
3590*38df6492SMark Einon 
3591*38df6492SMark Einon static int et131x_poll(struct napi_struct *napi, int budget)
3592*38df6492SMark Einon {
3593*38df6492SMark Einon 	struct et131x_adapter *adapter =
3594*38df6492SMark Einon 		container_of(napi, struct et131x_adapter, napi);
3595*38df6492SMark Einon 	int work_done = et131x_handle_recv_pkts(adapter, budget);
3596*38df6492SMark Einon 
3597*38df6492SMark Einon 	et131x_handle_send_pkts(adapter);
3598*38df6492SMark Einon 
3599*38df6492SMark Einon 	if (work_done < budget) {
3600*38df6492SMark Einon 		napi_complete(&adapter->napi);
3601*38df6492SMark Einon 		et131x_enable_interrupts(adapter);
3602*38df6492SMark Einon 	}
3603*38df6492SMark Einon 
3604*38df6492SMark Einon 	return work_done;
3605*38df6492SMark Einon }
3606*38df6492SMark Einon 
3607*38df6492SMark Einon /* et131x_stats - Return the current device statistics  */
3608*38df6492SMark Einon static struct net_device_stats *et131x_stats(struct net_device *netdev)
3609*38df6492SMark Einon {
3610*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3611*38df6492SMark Einon 	struct net_device_stats *stats = &adapter->netdev->stats;
3612*38df6492SMark Einon 	struct ce_stats *devstat = &adapter->stats;
3613*38df6492SMark Einon 
3614*38df6492SMark Einon 	stats->rx_errors = devstat->rx_length_errs +
3615*38df6492SMark Einon 			   devstat->rx_align_errs +
3616*38df6492SMark Einon 			   devstat->rx_crc_errs +
3617*38df6492SMark Einon 			   devstat->rx_code_violations +
3618*38df6492SMark Einon 			   devstat->rx_other_errs;
3619*38df6492SMark Einon 	stats->tx_errors = devstat->tx_max_pkt_errs;
3620*38df6492SMark Einon 	stats->multicast = devstat->multicast_pkts_rcvd;
3621*38df6492SMark Einon 	stats->collisions = devstat->tx_collisions;
3622*38df6492SMark Einon 
3623*38df6492SMark Einon 	stats->rx_length_errors = devstat->rx_length_errs;
3624*38df6492SMark Einon 	stats->rx_over_errors = devstat->rx_overflows;
3625*38df6492SMark Einon 	stats->rx_crc_errors = devstat->rx_crc_errs;
3626*38df6492SMark Einon 	stats->rx_dropped = devstat->rcvd_pkts_dropped;
3627*38df6492SMark Einon 
3628*38df6492SMark Einon 	/* NOTE: Not used, can't find analogous statistics */
3629*38df6492SMark Einon 	/* stats->rx_frame_errors     = devstat->; */
3630*38df6492SMark Einon 	/* stats->rx_fifo_errors      = devstat->; */
3631*38df6492SMark Einon 	/* stats->rx_missed_errors    = devstat->; */
3632*38df6492SMark Einon 
3633*38df6492SMark Einon 	/* stats->tx_aborted_errors   = devstat->; */
3634*38df6492SMark Einon 	/* stats->tx_carrier_errors   = devstat->; */
3635*38df6492SMark Einon 	/* stats->tx_fifo_errors      = devstat->; */
3636*38df6492SMark Einon 	/* stats->tx_heartbeat_errors = devstat->; */
3637*38df6492SMark Einon 	/* stats->tx_window_errors    = devstat->; */
3638*38df6492SMark Einon 	return stats;
3639*38df6492SMark Einon }
3640*38df6492SMark Einon 
3641*38df6492SMark Einon static int et131x_open(struct net_device *netdev)
3642*38df6492SMark Einon {
3643*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3644*38df6492SMark Einon 	struct pci_dev *pdev = adapter->pdev;
3645*38df6492SMark Einon 	unsigned int irq = pdev->irq;
3646*38df6492SMark Einon 	int result;
3647*38df6492SMark Einon 
3648*38df6492SMark Einon 	/* Start the timer to track NIC errors */
3649*38df6492SMark Einon 	init_timer(&adapter->error_timer);
3650*38df6492SMark Einon 	adapter->error_timer.expires = jiffies + TX_ERROR_PERIOD * HZ / 1000;
3651*38df6492SMark Einon 	adapter->error_timer.function = et131x_error_timer_handler;
3652*38df6492SMark Einon 	adapter->error_timer.data = (unsigned long)adapter;
3653*38df6492SMark Einon 	add_timer(&adapter->error_timer);
3654*38df6492SMark Einon 
3655*38df6492SMark Einon 	result = request_irq(irq, et131x_isr,
3656*38df6492SMark Einon 			     IRQF_SHARED, netdev->name, netdev);
3657*38df6492SMark Einon 	if (result) {
3658*38df6492SMark Einon 		dev_err(&pdev->dev, "could not register IRQ %d\n", irq);
3659*38df6492SMark Einon 		return result;
3660*38df6492SMark Einon 	}
3661*38df6492SMark Einon 
3662*38df6492SMark Einon 	adapter->flags |= FMP_ADAPTER_INTERRUPT_IN_USE;
3663*38df6492SMark Einon 
3664*38df6492SMark Einon 	napi_enable(&adapter->napi);
3665*38df6492SMark Einon 
3666*38df6492SMark Einon 	et131x_up(netdev);
3667*38df6492SMark Einon 
3668*38df6492SMark Einon 	return result;
3669*38df6492SMark Einon }
3670*38df6492SMark Einon 
3671*38df6492SMark Einon static int et131x_close(struct net_device *netdev)
3672*38df6492SMark Einon {
3673*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3674*38df6492SMark Einon 
3675*38df6492SMark Einon 	et131x_down(netdev);
3676*38df6492SMark Einon 	napi_disable(&adapter->napi);
3677*38df6492SMark Einon 
3678*38df6492SMark Einon 	adapter->flags &= ~FMP_ADAPTER_INTERRUPT_IN_USE;
3679*38df6492SMark Einon 	free_irq(adapter->pdev->irq, netdev);
3680*38df6492SMark Einon 
3681*38df6492SMark Einon 	/* Stop the error timer */
3682*38df6492SMark Einon 	return del_timer_sync(&adapter->error_timer);
3683*38df6492SMark Einon }
3684*38df6492SMark Einon 
3685*38df6492SMark Einon static int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf,
3686*38df6492SMark Einon 			int cmd)
3687*38df6492SMark Einon {
3688*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3689*38df6492SMark Einon 
3690*38df6492SMark Einon 	if (!adapter->phydev)
3691*38df6492SMark Einon 		return -EINVAL;
3692*38df6492SMark Einon 
3693*38df6492SMark Einon 	return phy_mii_ioctl(adapter->phydev, reqbuf, cmd);
3694*38df6492SMark Einon }
3695*38df6492SMark Einon 
3696*38df6492SMark Einon /* et131x_set_packet_filter - Configures the Rx Packet filtering */
3697*38df6492SMark Einon static int et131x_set_packet_filter(struct et131x_adapter *adapter)
3698*38df6492SMark Einon {
3699*38df6492SMark Einon 	int filter = adapter->packet_filter;
3700*38df6492SMark Einon 	u32 ctrl;
3701*38df6492SMark Einon 	u32 pf_ctrl;
3702*38df6492SMark Einon 
3703*38df6492SMark Einon 	ctrl = readl(&adapter->regs->rxmac.ctrl);
3704*38df6492SMark Einon 	pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
3705*38df6492SMark Einon 
3706*38df6492SMark Einon 	/* Default to disabled packet filtering */
3707*38df6492SMark Einon 	ctrl |= 0x04;
3708*38df6492SMark Einon 
3709*38df6492SMark Einon 	/* Set us to be in promiscuous mode so we receive everything, this
3710*38df6492SMark Einon 	 * is also true when we get a packet filter of 0
3711*38df6492SMark Einon 	 */
3712*38df6492SMark Einon 	if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
3713*38df6492SMark Einon 		pf_ctrl &= ~7;	/* Clear filter bits */
3714*38df6492SMark Einon 	else {
3715*38df6492SMark Einon 		/* Set us up with Multicast packet filtering.  Three cases are
3716*38df6492SMark Einon 		 * possible - (1) we have a multi-cast list, (2) we receive ALL
3717*38df6492SMark Einon 		 * multicast entries or (3) we receive none.
3718*38df6492SMark Einon 		 */
3719*38df6492SMark Einon 		if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
3720*38df6492SMark Einon 			pf_ctrl &= ~2;	/* Multicast filter bit */
3721*38df6492SMark Einon 		else {
3722*38df6492SMark Einon 			et1310_setup_device_for_multicast(adapter);
3723*38df6492SMark Einon 			pf_ctrl |= 2;
3724*38df6492SMark Einon 			ctrl &= ~0x04;
3725*38df6492SMark Einon 		}
3726*38df6492SMark Einon 
3727*38df6492SMark Einon 		/* Set us up with Unicast packet filtering */
3728*38df6492SMark Einon 		if (filter & ET131X_PACKET_TYPE_DIRECTED) {
3729*38df6492SMark Einon 			et1310_setup_device_for_unicast(adapter);
3730*38df6492SMark Einon 			pf_ctrl |= 4;
3731*38df6492SMark Einon 			ctrl &= ~0x04;
3732*38df6492SMark Einon 		}
3733*38df6492SMark Einon 
3734*38df6492SMark Einon 		/* Set us up with Broadcast packet filtering */
3735*38df6492SMark Einon 		if (filter & ET131X_PACKET_TYPE_BROADCAST) {
3736*38df6492SMark Einon 			pf_ctrl |= 1;	/* Broadcast filter bit */
3737*38df6492SMark Einon 			ctrl &= ~0x04;
3738*38df6492SMark Einon 		} else {
3739*38df6492SMark Einon 			pf_ctrl &= ~1;
3740*38df6492SMark Einon 		}
3741*38df6492SMark Einon 
3742*38df6492SMark Einon 		/* Setup the receive mac configuration registers - Packet
3743*38df6492SMark Einon 		 * Filter control + the enable / disable for packet filter
3744*38df6492SMark Einon 		 * in the control reg.
3745*38df6492SMark Einon 		 */
3746*38df6492SMark Einon 		writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
3747*38df6492SMark Einon 		writel(ctrl, &adapter->regs->rxmac.ctrl);
3748*38df6492SMark Einon 	}
3749*38df6492SMark Einon 	return 0;
3750*38df6492SMark Einon }
3751*38df6492SMark Einon 
3752*38df6492SMark Einon static void et131x_multicast(struct net_device *netdev)
3753*38df6492SMark Einon {
3754*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3755*38df6492SMark Einon 	int packet_filter;
3756*38df6492SMark Einon 	struct netdev_hw_addr *ha;
3757*38df6492SMark Einon 	int i;
3758*38df6492SMark Einon 
3759*38df6492SMark Einon 	/* Before we modify the platform-independent filter flags, store them
3760*38df6492SMark Einon 	 * locally. This allows us to determine if anything's changed and if
3761*38df6492SMark Einon 	 * we even need to bother the hardware
3762*38df6492SMark Einon 	 */
3763*38df6492SMark Einon 	packet_filter = adapter->packet_filter;
3764*38df6492SMark Einon 
3765*38df6492SMark Einon 	/* Clear the 'multicast' flag locally; because we only have a single
3766*38df6492SMark Einon 	 * flag to check multicast, and multiple multicast addresses can be
3767*38df6492SMark Einon 	 * set, this is the easiest way to determine if more than one
3768*38df6492SMark Einon 	 * multicast address is being set.
3769*38df6492SMark Einon 	 */
3770*38df6492SMark Einon 	packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
3771*38df6492SMark Einon 
3772*38df6492SMark Einon 	/* Check the net_device flags and set the device independent flags
3773*38df6492SMark Einon 	 * accordingly
3774*38df6492SMark Einon 	 */
3775*38df6492SMark Einon 	if (netdev->flags & IFF_PROMISC)
3776*38df6492SMark Einon 		adapter->packet_filter |= ET131X_PACKET_TYPE_PROMISCUOUS;
3777*38df6492SMark Einon 	else
3778*38df6492SMark Einon 		adapter->packet_filter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
3779*38df6492SMark Einon 
3780*38df6492SMark Einon 	if ((netdev->flags & IFF_ALLMULTI) ||
3781*38df6492SMark Einon 	    (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST))
3782*38df6492SMark Einon 		adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
3783*38df6492SMark Einon 
3784*38df6492SMark Einon 	if (netdev_mc_count(netdev) < 1) {
3785*38df6492SMark Einon 		adapter->packet_filter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
3786*38df6492SMark Einon 		adapter->packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
3787*38df6492SMark Einon 	} else {
3788*38df6492SMark Einon 		adapter->packet_filter |= ET131X_PACKET_TYPE_MULTICAST;
3789*38df6492SMark Einon 	}
3790*38df6492SMark Einon 
3791*38df6492SMark Einon 	/* Set values in the private adapter struct */
3792*38df6492SMark Einon 	i = 0;
3793*38df6492SMark Einon 	netdev_for_each_mc_addr(ha, netdev) {
3794*38df6492SMark Einon 		if (i == NIC_MAX_MCAST_LIST)
3795*38df6492SMark Einon 			break;
3796*38df6492SMark Einon 		ether_addr_copy(adapter->multicast_list[i++], ha->addr);
3797*38df6492SMark Einon 	}
3798*38df6492SMark Einon 	adapter->multicast_addr_count = i;
3799*38df6492SMark Einon 
3800*38df6492SMark Einon 	/* Are the new flags different from the previous ones? If not, then no
3801*38df6492SMark Einon 	 * action is required
3802*38df6492SMark Einon 	 *
3803*38df6492SMark Einon 	 * NOTE - This block will always update the multicast_list with the
3804*38df6492SMark Einon 	 *        hardware, even if the addresses aren't the same.
3805*38df6492SMark Einon 	 */
3806*38df6492SMark Einon 	if (packet_filter != adapter->packet_filter)
3807*38df6492SMark Einon 		et131x_set_packet_filter(adapter);
3808*38df6492SMark Einon }
3809*38df6492SMark Einon 
3810*38df6492SMark Einon static netdev_tx_t et131x_tx(struct sk_buff *skb, struct net_device *netdev)
3811*38df6492SMark Einon {
3812*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3813*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
3814*38df6492SMark Einon 
3815*38df6492SMark Einon 	/* stop the queue if it's getting full */
3816*38df6492SMark Einon 	if (tx_ring->used >= NUM_TCB - 1 && !netif_queue_stopped(netdev))
3817*38df6492SMark Einon 		netif_stop_queue(netdev);
3818*38df6492SMark Einon 
3819*38df6492SMark Einon 	/* Save the timestamp for the TX timeout watchdog */
3820*38df6492SMark Einon 	netdev->trans_start = jiffies;
3821*38df6492SMark Einon 
3822*38df6492SMark Einon 	/* TCB is not available */
3823*38df6492SMark Einon 	if (tx_ring->used >= NUM_TCB)
3824*38df6492SMark Einon 		goto drop_err;
3825*38df6492SMark Einon 
3826*38df6492SMark Einon 	if ((adapter->flags & FMP_ADAPTER_FAIL_SEND_MASK) ||
3827*38df6492SMark Einon 	    !netif_carrier_ok(netdev))
3828*38df6492SMark Einon 		goto drop_err;
3829*38df6492SMark Einon 
3830*38df6492SMark Einon 	if (send_packet(skb, adapter))
3831*38df6492SMark Einon 		goto drop_err;
3832*38df6492SMark Einon 
3833*38df6492SMark Einon 	return NETDEV_TX_OK;
3834*38df6492SMark Einon 
3835*38df6492SMark Einon drop_err:
3836*38df6492SMark Einon 	dev_kfree_skb_any(skb);
3837*38df6492SMark Einon 	adapter->netdev->stats.tx_dropped++;
3838*38df6492SMark Einon 	return NETDEV_TX_OK;
3839*38df6492SMark Einon }
3840*38df6492SMark Einon 
3841*38df6492SMark Einon /* et131x_tx_timeout - Timeout handler
3842*38df6492SMark Einon  *
3843*38df6492SMark Einon  * The handler called when a Tx request times out. The timeout period is
3844*38df6492SMark Einon  * specified by the 'tx_timeo" element in the net_device structure (see
3845*38df6492SMark Einon  * et131x_alloc_device() to see how this value is set).
3846*38df6492SMark Einon  */
3847*38df6492SMark Einon static void et131x_tx_timeout(struct net_device *netdev)
3848*38df6492SMark Einon {
3849*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3850*38df6492SMark Einon 	struct tx_ring *tx_ring = &adapter->tx_ring;
3851*38df6492SMark Einon 	struct tcb *tcb;
3852*38df6492SMark Einon 	unsigned long flags;
3853*38df6492SMark Einon 
3854*38df6492SMark Einon 	/* If the device is closed, ignore the timeout */
3855*38df6492SMark Einon 	if (~(adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE))
3856*38df6492SMark Einon 		return;
3857*38df6492SMark Einon 
3858*38df6492SMark Einon 	/* Any nonrecoverable hardware error?
3859*38df6492SMark Einon 	 * Checks adapter->flags for any failure in phy reading
3860*38df6492SMark Einon 	 */
3861*38df6492SMark Einon 	if (adapter->flags & FMP_ADAPTER_NON_RECOVER_ERROR)
3862*38df6492SMark Einon 		return;
3863*38df6492SMark Einon 
3864*38df6492SMark Einon 	/* Hardware failure? */
3865*38df6492SMark Einon 	if (adapter->flags & FMP_ADAPTER_HARDWARE_ERROR) {
3866*38df6492SMark Einon 		dev_err(&adapter->pdev->dev, "hardware error - reset\n");
3867*38df6492SMark Einon 		return;
3868*38df6492SMark Einon 	}
3869*38df6492SMark Einon 
3870*38df6492SMark Einon 	/* Is send stuck? */
3871*38df6492SMark Einon 	spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3872*38df6492SMark Einon 	tcb = tx_ring->send_head;
3873*38df6492SMark Einon 	spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3874*38df6492SMark Einon 
3875*38df6492SMark Einon 	if (tcb) {
3876*38df6492SMark Einon 		tcb->count++;
3877*38df6492SMark Einon 
3878*38df6492SMark Einon 		if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
3879*38df6492SMark Einon 			dev_warn(&adapter->pdev->dev,
3880*38df6492SMark Einon 				 "Send stuck - reset. tcb->WrIndex %x\n",
3881*38df6492SMark Einon 				 tcb->index);
3882*38df6492SMark Einon 
3883*38df6492SMark Einon 			adapter->netdev->stats.tx_errors++;
3884*38df6492SMark Einon 
3885*38df6492SMark Einon 			/* perform reset of tx/rx */
3886*38df6492SMark Einon 			et131x_disable_txrx(netdev);
3887*38df6492SMark Einon 			et131x_enable_txrx(netdev);
3888*38df6492SMark Einon 		}
3889*38df6492SMark Einon 	}
3890*38df6492SMark Einon }
3891*38df6492SMark Einon 
3892*38df6492SMark Einon static int et131x_change_mtu(struct net_device *netdev, int new_mtu)
3893*38df6492SMark Einon {
3894*38df6492SMark Einon 	int result = 0;
3895*38df6492SMark Einon 	struct et131x_adapter *adapter = netdev_priv(netdev);
3896*38df6492SMark Einon 
3897*38df6492SMark Einon 	if (new_mtu < 64 || new_mtu > 9216)
3898*38df6492SMark Einon 		return -EINVAL;
3899*38df6492SMark Einon 
3900*38df6492SMark Einon 	et131x_disable_txrx(netdev);
3901*38df6492SMark Einon 
3902*38df6492SMark Einon 	netdev->mtu = new_mtu;
3903*38df6492SMark Einon 
3904*38df6492SMark Einon 	et131x_adapter_memory_free(adapter);
3905*38df6492SMark Einon 
3906*38df6492SMark Einon 	/* Set the config parameter for Jumbo Packet support */
3907*38df6492SMark Einon 	adapter->registry_jumbo_packet = new_mtu + 14;
3908*38df6492SMark Einon 	et131x_soft_reset(adapter);
3909*38df6492SMark Einon 
3910*38df6492SMark Einon 	result = et131x_adapter_memory_alloc(adapter);
3911*38df6492SMark Einon 	if (result != 0) {
3912*38df6492SMark Einon 		dev_warn(&adapter->pdev->dev,
3913*38df6492SMark Einon 			 "Change MTU failed; couldn't re-alloc DMA memory\n");
3914*38df6492SMark Einon 		return result;
3915*38df6492SMark Einon 	}
3916*38df6492SMark Einon 
3917*38df6492SMark Einon 	et131x_init_send(adapter);
3918*38df6492SMark Einon 	et131x_hwaddr_init(adapter);
3919*38df6492SMark Einon 	ether_addr_copy(netdev->dev_addr, adapter->addr);
3920*38df6492SMark Einon 
3921*38df6492SMark Einon 	/* Init the device with the new settings */
3922*38df6492SMark Einon 	et131x_adapter_setup(adapter);
3923*38df6492SMark Einon 	et131x_enable_txrx(netdev);
3924*38df6492SMark Einon 
3925*38df6492SMark Einon 	return result;
3926*38df6492SMark Einon }
3927*38df6492SMark Einon 
3928*38df6492SMark Einon static const struct net_device_ops et131x_netdev_ops = {
3929*38df6492SMark Einon 	.ndo_open		= et131x_open,
3930*38df6492SMark Einon 	.ndo_stop		= et131x_close,
3931*38df6492SMark Einon 	.ndo_start_xmit		= et131x_tx,
3932*38df6492SMark Einon 	.ndo_set_rx_mode	= et131x_multicast,
3933*38df6492SMark Einon 	.ndo_tx_timeout		= et131x_tx_timeout,
3934*38df6492SMark Einon 	.ndo_change_mtu		= et131x_change_mtu,
3935*38df6492SMark Einon 	.ndo_set_mac_address	= eth_mac_addr,
3936*38df6492SMark Einon 	.ndo_validate_addr	= eth_validate_addr,
3937*38df6492SMark Einon 	.ndo_get_stats		= et131x_stats,
3938*38df6492SMark Einon 	.ndo_do_ioctl		= et131x_ioctl,
3939*38df6492SMark Einon };
3940*38df6492SMark Einon 
3941*38df6492SMark Einon static int et131x_pci_setup(struct pci_dev *pdev,
3942*38df6492SMark Einon 			    const struct pci_device_id *ent)
3943*38df6492SMark Einon {
3944*38df6492SMark Einon 	struct net_device *netdev;
3945*38df6492SMark Einon 	struct et131x_adapter *adapter;
3946*38df6492SMark Einon 	int rc;
3947*38df6492SMark Einon 	int ii;
3948*38df6492SMark Einon 
3949*38df6492SMark Einon 	rc = pci_enable_device(pdev);
3950*38df6492SMark Einon 	if (rc < 0) {
3951*38df6492SMark Einon 		dev_err(&pdev->dev, "pci_enable_device() failed\n");
3952*38df6492SMark Einon 		goto out;
3953*38df6492SMark Einon 	}
3954*38df6492SMark Einon 
3955*38df6492SMark Einon 	/* Perform some basic PCI checks */
3956*38df6492SMark Einon 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
3957*38df6492SMark Einon 		dev_err(&pdev->dev, "Can't find PCI device's base address\n");
3958*38df6492SMark Einon 		rc = -ENODEV;
3959*38df6492SMark Einon 		goto err_disable;
3960*38df6492SMark Einon 	}
3961*38df6492SMark Einon 
3962*38df6492SMark Einon 	rc = pci_request_regions(pdev, DRIVER_NAME);
3963*38df6492SMark Einon 	if (rc < 0) {
3964*38df6492SMark Einon 		dev_err(&pdev->dev, "Can't get PCI resources\n");
3965*38df6492SMark Einon 		goto err_disable;
3966*38df6492SMark Einon 	}
3967*38df6492SMark Einon 
3968*38df6492SMark Einon 	pci_set_master(pdev);
3969*38df6492SMark Einon 
3970*38df6492SMark Einon 	/* Check the DMA addressing support of this device */
3971*38df6492SMark Einon 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
3972*38df6492SMark Einon 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
3973*38df6492SMark Einon 		dev_err(&pdev->dev, "No usable DMA addressing method\n");
3974*38df6492SMark Einon 		rc = -EIO;
3975*38df6492SMark Einon 		goto err_release_res;
3976*38df6492SMark Einon 	}
3977*38df6492SMark Einon 
3978*38df6492SMark Einon 	netdev = alloc_etherdev(sizeof(struct et131x_adapter));
3979*38df6492SMark Einon 	if (!netdev) {
3980*38df6492SMark Einon 		dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
3981*38df6492SMark Einon 		rc = -ENOMEM;
3982*38df6492SMark Einon 		goto err_release_res;
3983*38df6492SMark Einon 	}
3984*38df6492SMark Einon 
3985*38df6492SMark Einon 	netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
3986*38df6492SMark Einon 	netdev->netdev_ops     = &et131x_netdev_ops;
3987*38df6492SMark Einon 
3988*38df6492SMark Einon 	SET_NETDEV_DEV(netdev, &pdev->dev);
3989*38df6492SMark Einon 	netdev->ethtool_ops = &et131x_ethtool_ops;
3990*38df6492SMark Einon 
3991*38df6492SMark Einon 	adapter = et131x_adapter_init(netdev, pdev);
3992*38df6492SMark Einon 
3993*38df6492SMark Einon 	rc = et131x_pci_init(adapter, pdev);
3994*38df6492SMark Einon 	if (rc < 0)
3995*38df6492SMark Einon 		goto err_free_dev;
3996*38df6492SMark Einon 
3997*38df6492SMark Einon 	/* Map the bus-relative registers to system virtual memory */
3998*38df6492SMark Einon 	adapter->regs = pci_ioremap_bar(pdev, 0);
3999*38df6492SMark Einon 	if (!adapter->regs) {
4000*38df6492SMark Einon 		dev_err(&pdev->dev, "Cannot map device registers\n");
4001*38df6492SMark Einon 		rc = -ENOMEM;
4002*38df6492SMark Einon 		goto err_free_dev;
4003*38df6492SMark Einon 	}
4004*38df6492SMark Einon 
4005*38df6492SMark Einon 	/* If Phy COMA mode was enabled when we went down, disable it here. */
4006*38df6492SMark Einon 	writel(ET_PMCSR_INIT,  &adapter->regs->global.pm_csr);
4007*38df6492SMark Einon 
4008*38df6492SMark Einon 	et131x_soft_reset(adapter);
4009*38df6492SMark Einon 	et131x_disable_interrupts(adapter);
4010*38df6492SMark Einon 
4011*38df6492SMark Einon 	rc = et131x_adapter_memory_alloc(adapter);
4012*38df6492SMark Einon 	if (rc < 0) {
4013*38df6492SMark Einon 		dev_err(&pdev->dev, "Could not alloc adapter memory (DMA)\n");
4014*38df6492SMark Einon 		goto err_iounmap;
4015*38df6492SMark Einon 	}
4016*38df6492SMark Einon 
4017*38df6492SMark Einon 	et131x_init_send(adapter);
4018*38df6492SMark Einon 
4019*38df6492SMark Einon 	netif_napi_add(netdev, &adapter->napi, et131x_poll, 64);
4020*38df6492SMark Einon 
4021*38df6492SMark Einon 	ether_addr_copy(netdev->dev_addr, adapter->addr);
4022*38df6492SMark Einon 
4023*38df6492SMark Einon 	rc = -ENOMEM;
4024*38df6492SMark Einon 
4025*38df6492SMark Einon 	adapter->mii_bus = mdiobus_alloc();
4026*38df6492SMark Einon 	if (!adapter->mii_bus) {
4027*38df6492SMark Einon 		dev_err(&pdev->dev, "Alloc of mii_bus struct failed\n");
4028*38df6492SMark Einon 		goto err_mem_free;
4029*38df6492SMark Einon 	}
4030*38df6492SMark Einon 
4031*38df6492SMark Einon 	adapter->mii_bus->name = "et131x_eth_mii";
4032*38df6492SMark Einon 	snprintf(adapter->mii_bus->id, MII_BUS_ID_SIZE, "%x",
4033*38df6492SMark Einon 		 (adapter->pdev->bus->number << 8) | adapter->pdev->devfn);
4034*38df6492SMark Einon 	adapter->mii_bus->priv = netdev;
4035*38df6492SMark Einon 	adapter->mii_bus->read = et131x_mdio_read;
4036*38df6492SMark Einon 	adapter->mii_bus->write = et131x_mdio_write;
4037*38df6492SMark Einon 	adapter->mii_bus->irq = kmalloc_array(PHY_MAX_ADDR, sizeof(int),
4038*38df6492SMark Einon 					      GFP_KERNEL);
4039*38df6492SMark Einon 	if (!adapter->mii_bus->irq)
4040*38df6492SMark Einon 		goto err_mdio_free;
4041*38df6492SMark Einon 
4042*38df6492SMark Einon 	for (ii = 0; ii < PHY_MAX_ADDR; ii++)
4043*38df6492SMark Einon 		adapter->mii_bus->irq[ii] = PHY_POLL;
4044*38df6492SMark Einon 
4045*38df6492SMark Einon 	rc = mdiobus_register(adapter->mii_bus);
4046*38df6492SMark Einon 	if (rc < 0) {
4047*38df6492SMark Einon 		dev_err(&pdev->dev, "failed to register MII bus\n");
4048*38df6492SMark Einon 		goto err_mdio_free_irq;
4049*38df6492SMark Einon 	}
4050*38df6492SMark Einon 
4051*38df6492SMark Einon 	rc = et131x_mii_probe(netdev);
4052*38df6492SMark Einon 	if (rc < 0) {
4053*38df6492SMark Einon 		dev_err(&pdev->dev, "failed to probe MII bus\n");
4054*38df6492SMark Einon 		goto err_mdio_unregister;
4055*38df6492SMark Einon 	}
4056*38df6492SMark Einon 
4057*38df6492SMark Einon 	et131x_adapter_setup(adapter);
4058*38df6492SMark Einon 
4059*38df6492SMark Einon 	/* Init variable for counting how long we do not have link status */
4060*38df6492SMark Einon 	adapter->boot_coma = 0;
4061*38df6492SMark Einon 	et1310_disable_phy_coma(adapter);
4062*38df6492SMark Einon 
4063*38df6492SMark Einon 	/* We can enable interrupts now
4064*38df6492SMark Einon 	 *
4065*38df6492SMark Einon 	 *  NOTE - Because registration of interrupt handler is done in the
4066*38df6492SMark Einon 	 *         device's open(), defer enabling device interrupts to that
4067*38df6492SMark Einon 	 *         point
4068*38df6492SMark Einon 	 */
4069*38df6492SMark Einon 
4070*38df6492SMark Einon 	rc = register_netdev(netdev);
4071*38df6492SMark Einon 	if (rc < 0) {
4072*38df6492SMark Einon 		dev_err(&pdev->dev, "register_netdev() failed\n");
4073*38df6492SMark Einon 		goto err_phy_disconnect;
4074*38df6492SMark Einon 	}
4075*38df6492SMark Einon 
4076*38df6492SMark Einon 	/* Register the net_device struct with the PCI subsystem. Save a copy
4077*38df6492SMark Einon 	 * of the PCI config space for this device now that the device has
4078*38df6492SMark Einon 	 * been initialized, just in case it needs to be quickly restored.
4079*38df6492SMark Einon 	 */
4080*38df6492SMark Einon 	pci_set_drvdata(pdev, netdev);
4081*38df6492SMark Einon out:
4082*38df6492SMark Einon 	return rc;
4083*38df6492SMark Einon 
4084*38df6492SMark Einon err_phy_disconnect:
4085*38df6492SMark Einon 	phy_disconnect(adapter->phydev);
4086*38df6492SMark Einon err_mdio_unregister:
4087*38df6492SMark Einon 	mdiobus_unregister(adapter->mii_bus);
4088*38df6492SMark Einon err_mdio_free_irq:
4089*38df6492SMark Einon 	kfree(adapter->mii_bus->irq);
4090*38df6492SMark Einon err_mdio_free:
4091*38df6492SMark Einon 	mdiobus_free(adapter->mii_bus);
4092*38df6492SMark Einon err_mem_free:
4093*38df6492SMark Einon 	et131x_adapter_memory_free(adapter);
4094*38df6492SMark Einon err_iounmap:
4095*38df6492SMark Einon 	iounmap(adapter->regs);
4096*38df6492SMark Einon err_free_dev:
4097*38df6492SMark Einon 	pci_dev_put(pdev);
4098*38df6492SMark Einon 	free_netdev(netdev);
4099*38df6492SMark Einon err_release_res:
4100*38df6492SMark Einon 	pci_release_regions(pdev);
4101*38df6492SMark Einon err_disable:
4102*38df6492SMark Einon 	pci_disable_device(pdev);
4103*38df6492SMark Einon 	goto out;
4104*38df6492SMark Einon }
4105*38df6492SMark Einon 
4106*38df6492SMark Einon static const struct pci_device_id et131x_pci_table[] = {
4107*38df6492SMark Einon 	{ PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_GIG), 0UL},
4108*38df6492SMark Einon 	{ PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_FAST), 0UL},
4109*38df6492SMark Einon 	{ 0,}
4110*38df6492SMark Einon };
4111*38df6492SMark Einon MODULE_DEVICE_TABLE(pci, et131x_pci_table);
4112*38df6492SMark Einon 
4113*38df6492SMark Einon static struct pci_driver et131x_driver = {
4114*38df6492SMark Einon 	.name		= DRIVER_NAME,
4115*38df6492SMark Einon 	.id_table	= et131x_pci_table,
4116*38df6492SMark Einon 	.probe		= et131x_pci_setup,
4117*38df6492SMark Einon 	.remove		= et131x_pci_remove,
4118*38df6492SMark Einon 	.driver.pm	= &et131x_pm_ops,
4119*38df6492SMark Einon };
4120*38df6492SMark Einon 
4121*38df6492SMark Einon module_pci_driver(et131x_driver);
4122