18c2ecf20Sopenharmony_ci/* Agere Systems Inc.
28c2ecf20Sopenharmony_ci * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright © 2005 Agere Systems Inc.
58c2ecf20Sopenharmony_ci * All rights reserved.
68c2ecf20Sopenharmony_ci *   http://www.agere.com
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (c) 2011 Mark Einon <mark.einon@gmail.com>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *------------------------------------------------------------------------------
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * SOFTWARE LICENSE
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * This software is provided subject to the following terms and conditions,
158c2ecf20Sopenharmony_ci * which you should read carefully before using the software.  Using this
168c2ecf20Sopenharmony_ci * software indicates your acceptance of these terms and conditions.  If you do
178c2ecf20Sopenharmony_ci * not agree with these terms and conditions, do not use the software.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Copyright © 2005 Agere Systems Inc.
208c2ecf20Sopenharmony_ci * All rights reserved.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Redistribution and use in source or binary forms, with or without
238c2ecf20Sopenharmony_ci * modifications, are permitted provided that the following conditions are met:
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * . Redistributions of source code must retain the above copyright notice, this
268c2ecf20Sopenharmony_ci *    list of conditions and the following Disclaimer as comments in the code as
278c2ecf20Sopenharmony_ci *    well as in the documentation and/or other materials provided with the
288c2ecf20Sopenharmony_ci *    distribution.
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * . Redistributions in binary form must reproduce the above copyright notice,
318c2ecf20Sopenharmony_ci *    this list of conditions and the following Disclaimer in the documentation
328c2ecf20Sopenharmony_ci *    and/or other materials provided with the distribution.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * . Neither the name of Agere Systems Inc. nor the names of the contributors
358c2ecf20Sopenharmony_ci *    may be used to endorse or promote products derived from this software
368c2ecf20Sopenharmony_ci *    without specific prior written permission.
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * Disclaimer
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
418c2ecf20Sopenharmony_ci * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
428c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ANY
438c2ecf20Sopenharmony_ci * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
448c2ecf20Sopenharmony_ci * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
458c2ecf20Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
468c2ecf20Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
478c2ecf20Sopenharmony_ci * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
488c2ecf20Sopenharmony_ci * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
498c2ecf20Sopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
508c2ecf20Sopenharmony_ci * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
518c2ecf20Sopenharmony_ci * DAMAGE.
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#include <linux/pci.h>
578c2ecf20Sopenharmony_ci#include <linux/module.h>
588c2ecf20Sopenharmony_ci#include <linux/types.h>
598c2ecf20Sopenharmony_ci#include <linux/kernel.h>
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#include <linux/sched.h>
628c2ecf20Sopenharmony_ci#include <linux/ptrace.h>
638c2ecf20Sopenharmony_ci#include <linux/slab.h>
648c2ecf20Sopenharmony_ci#include <linux/ctype.h>
658c2ecf20Sopenharmony_ci#include <linux/string.h>
668c2ecf20Sopenharmony_ci#include <linux/timer.h>
678c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
688c2ecf20Sopenharmony_ci#include <linux/in.h>
698c2ecf20Sopenharmony_ci#include <linux/delay.h>
708c2ecf20Sopenharmony_ci#include <linux/bitops.h>
718c2ecf20Sopenharmony_ci#include <linux/io.h>
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
748c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
758c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
768c2ecf20Sopenharmony_ci#include <linux/if_arp.h>
778c2ecf20Sopenharmony_ci#include <linux/ioport.h>
788c2ecf20Sopenharmony_ci#include <linux/crc32.h>
798c2ecf20Sopenharmony_ci#include <linux/random.h>
808c2ecf20Sopenharmony_ci#include <linux/phy.h>
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#include "et131x.h"
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciMODULE_AUTHOR("Victor Soriano <vjsoriano@agere.com>");
858c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mark Einon <mark.einon@gmail.com>");
868c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
878c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("10/100/1000 Base-T Ethernet Driver for the ET1310 by Agere Systems");
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* EEPROM defines */
908c2ecf20Sopenharmony_ci#define MAX_NUM_REGISTER_POLLS          1000
918c2ecf20Sopenharmony_ci#define MAX_NUM_WRITE_RETRIES           2
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* MAC defines */
948c2ecf20Sopenharmony_ci#define COUNTER_WRAP_16_BIT 0x10000
958c2ecf20Sopenharmony_ci#define COUNTER_WRAP_12_BIT 0x1000
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/* PCI defines */
988c2ecf20Sopenharmony_ci#define INTERNAL_MEM_SIZE       0x400	/* 1024 of internal memory */
998c2ecf20Sopenharmony_ci#define INTERNAL_MEM_RX_OFFSET  0x1FF	/* 50%   Tx, 50%   Rx */
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* ISR defines */
1028c2ecf20Sopenharmony_ci/* For interrupts, normal running is:
1038c2ecf20Sopenharmony_ci *       rxdma_xfr_done, phy_interrupt, mac_stat_interrupt,
1048c2ecf20Sopenharmony_ci *       watchdog_interrupt & txdma_xfer_done
1058c2ecf20Sopenharmony_ci *
1068c2ecf20Sopenharmony_ci * In both cases, when flow control is enabled for either Tx or bi-direction,
1078c2ecf20Sopenharmony_ci * we additional enable rx_fbr0_low and rx_fbr1_low, so we know when the
1088c2ecf20Sopenharmony_ci * buffer rings are running low.
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_ci#define INT_MASK_DISABLE            0xffffffff
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/* NOTE: Masking out MAC_STAT Interrupt for now...
1138c2ecf20Sopenharmony_ci * #define INT_MASK_ENABLE             0xfff6bf17
1148c2ecf20Sopenharmony_ci * #define INT_MASK_ENABLE_NO_FLOW     0xfff6bfd7
1158c2ecf20Sopenharmony_ci */
1168c2ecf20Sopenharmony_ci#define INT_MASK_ENABLE             0xfffebf17
1178c2ecf20Sopenharmony_ci#define INT_MASK_ENABLE_NO_FLOW     0xfffebfd7
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/* General defines */
1208c2ecf20Sopenharmony_ci/* Packet and header sizes */
1218c2ecf20Sopenharmony_ci#define NIC_MIN_PACKET_SIZE	60
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* Multicast list size */
1248c2ecf20Sopenharmony_ci#define NIC_MAX_MCAST_LIST	128
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci/* Supported Filters */
1278c2ecf20Sopenharmony_ci#define ET131X_PACKET_TYPE_DIRECTED		0x0001
1288c2ecf20Sopenharmony_ci#define ET131X_PACKET_TYPE_MULTICAST		0x0002
1298c2ecf20Sopenharmony_ci#define ET131X_PACKET_TYPE_BROADCAST		0x0004
1308c2ecf20Sopenharmony_ci#define ET131X_PACKET_TYPE_PROMISCUOUS		0x0008
1318c2ecf20Sopenharmony_ci#define ET131X_PACKET_TYPE_ALL_MULTICAST	0x0010
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/* Tx Timeout */
1348c2ecf20Sopenharmony_ci#define ET131X_TX_TIMEOUT	(1 * HZ)
1358c2ecf20Sopenharmony_ci#define NIC_SEND_HANG_THRESHOLD	0
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci/* MP_ADAPTER flags */
1388c2ecf20Sopenharmony_ci#define FMP_ADAPTER_INTERRUPT_IN_USE	0x00000008
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/* MP_SHARED flags */
1418c2ecf20Sopenharmony_ci#define FMP_ADAPTER_LOWER_POWER		0x00200000
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#define FMP_ADAPTER_NON_RECOVER_ERROR	0x00800000
1448c2ecf20Sopenharmony_ci#define FMP_ADAPTER_HARDWARE_ERROR	0x04000000
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci#define FMP_ADAPTER_FAIL_SEND_MASK	0x3ff00000
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/* Some offsets in PCI config space that are actually used. */
1498c2ecf20Sopenharmony_ci#define ET1310_PCI_MAC_ADDRESS		0xA4
1508c2ecf20Sopenharmony_ci#define ET1310_PCI_EEPROM_STATUS	0xB2
1518c2ecf20Sopenharmony_ci#define ET1310_PCI_ACK_NACK		0xC0
1528c2ecf20Sopenharmony_ci#define ET1310_PCI_REPLAY		0xC2
1538c2ecf20Sopenharmony_ci#define ET1310_PCI_L0L1LATENCY		0xCF
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/* PCI Product IDs */
1568c2ecf20Sopenharmony_ci#define ET131X_PCI_DEVICE_ID_GIG	0xED00	/* ET1310 1000 Base-T 8 */
1578c2ecf20Sopenharmony_ci#define ET131X_PCI_DEVICE_ID_FAST	0xED01	/* ET1310 100  Base-T */
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/* Define order of magnitude converter */
1608c2ecf20Sopenharmony_ci#define NANO_IN_A_MICRO	1000
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci#define PARM_RX_NUM_BUFS_DEF    4
1638c2ecf20Sopenharmony_ci#define PARM_RX_TIME_INT_DEF    10
1648c2ecf20Sopenharmony_ci#define PARM_RX_MEM_END_DEF     0x2bc
1658c2ecf20Sopenharmony_ci#define PARM_TX_TIME_INT_DEF    40
1668c2ecf20Sopenharmony_ci#define PARM_TX_NUM_BUFS_DEF    4
1678c2ecf20Sopenharmony_ci#define PARM_DMA_CACHE_DEF      0
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/* RX defines */
1708c2ecf20Sopenharmony_ci#define FBR_CHUNKS		32
1718c2ecf20Sopenharmony_ci#define MAX_DESC_PER_RING_RX	1024
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/* number of RFDs - default and min */
1748c2ecf20Sopenharmony_ci#define RFD_LOW_WATER_MARK	40
1758c2ecf20Sopenharmony_ci#define NIC_DEFAULT_NUM_RFD	1024
1768c2ecf20Sopenharmony_ci#define NUM_FBRS		2
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci#define MAX_PACKETS_HANDLED	256
1798c2ecf20Sopenharmony_ci#define ET131X_MIN_MTU		64
1808c2ecf20Sopenharmony_ci#define ET131X_MAX_MTU		9216
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci#define ALCATEL_MULTICAST_PKT	0x01000000
1838c2ecf20Sopenharmony_ci#define ALCATEL_BROADCAST_PKT	0x02000000
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/* typedefs for Free Buffer Descriptors */
1868c2ecf20Sopenharmony_cistruct fbr_desc {
1878c2ecf20Sopenharmony_ci	u32 addr_lo;
1888c2ecf20Sopenharmony_ci	u32 addr_hi;
1898c2ecf20Sopenharmony_ci	u32 word2;		/* Bits 10-31 reserved, 0-9 descriptor */
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/* Packet Status Ring Descriptors
1938c2ecf20Sopenharmony_ci *
1948c2ecf20Sopenharmony_ci * Word 0:
1958c2ecf20Sopenharmony_ci *
1968c2ecf20Sopenharmony_ci * top 16 bits are from the Alcatel Status Word as enumerated in
1978c2ecf20Sopenharmony_ci * PE-MCXMAC Data Sheet IPD DS54 0210-1 (also IPD-DS80 0205-2)
1988c2ecf20Sopenharmony_ci *
1998c2ecf20Sopenharmony_ci * 0: hp			hash pass
2008c2ecf20Sopenharmony_ci * 1: ipa			IP checksum assist
2018c2ecf20Sopenharmony_ci * 2: ipp			IP checksum pass
2028c2ecf20Sopenharmony_ci * 3: tcpa			TCP checksum assist
2038c2ecf20Sopenharmony_ci * 4: tcpp			TCP checksum pass
2048c2ecf20Sopenharmony_ci * 5: wol			WOL Event
2058c2ecf20Sopenharmony_ci * 6: rxmac_error		RXMAC Error Indicator
2068c2ecf20Sopenharmony_ci * 7: drop			Drop packet
2078c2ecf20Sopenharmony_ci * 8: ft			Frame Truncated
2088c2ecf20Sopenharmony_ci * 9: jp			Jumbo Packet
2098c2ecf20Sopenharmony_ci * 10: vp			VLAN Packet
2108c2ecf20Sopenharmony_ci * 11-15: unused
2118c2ecf20Sopenharmony_ci * 16: asw_prev_pkt_dropped	e.g. IFG too small on previous
2128c2ecf20Sopenharmony_ci * 17: asw_RX_DV_event		short receive event detected
2138c2ecf20Sopenharmony_ci * 18: asw_false_carrier_event	bad carrier since last good packet
2148c2ecf20Sopenharmony_ci * 19: asw_code_err		one or more nibbles signalled as errors
2158c2ecf20Sopenharmony_ci * 20: asw_CRC_err		CRC error
2168c2ecf20Sopenharmony_ci * 21: asw_len_chk_err		frame length field incorrect
2178c2ecf20Sopenharmony_ci * 22: asw_too_long		frame length > 1518 bytes
2188c2ecf20Sopenharmony_ci * 23: asw_OK			valid CRC + no code error
2198c2ecf20Sopenharmony_ci * 24: asw_multicast		has a multicast address
2208c2ecf20Sopenharmony_ci * 25: asw_broadcast		has a broadcast address
2218c2ecf20Sopenharmony_ci * 26: asw_dribble_nibble	spurious bits after EOP
2228c2ecf20Sopenharmony_ci * 27: asw_control_frame	is a control frame
2238c2ecf20Sopenharmony_ci * 28: asw_pause_frame		is a pause frame
2248c2ecf20Sopenharmony_ci * 29: asw_unsupported_op	unsupported OP code
2258c2ecf20Sopenharmony_ci * 30: asw_VLAN_tag		VLAN tag detected
2268c2ecf20Sopenharmony_ci * 31: asw_long_evt		Rx long event
2278c2ecf20Sopenharmony_ci *
2288c2ecf20Sopenharmony_ci * Word 1:
2298c2ecf20Sopenharmony_ci * 0-15: length			length in bytes
2308c2ecf20Sopenharmony_ci * 16-25: bi			Buffer Index
2318c2ecf20Sopenharmony_ci * 26-27: ri			Ring Index
2328c2ecf20Sopenharmony_ci * 28-31: reserved
2338c2ecf20Sopenharmony_ci */
2348c2ecf20Sopenharmony_cistruct pkt_stat_desc {
2358c2ecf20Sopenharmony_ci	u32 word0;
2368c2ecf20Sopenharmony_ci	u32 word1;
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/* Typedefs for the RX DMA status word */
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci/* rx status word 0 holds part of the status bits of the Rx DMA engine
2428c2ecf20Sopenharmony_ci * that get copied out to memory by the ET-1310.  Word 0 is a 32 bit word
2438c2ecf20Sopenharmony_ci * which contains the Free Buffer ring 0 and 1 available offset.
2448c2ecf20Sopenharmony_ci *
2458c2ecf20Sopenharmony_ci * bit 0-9 FBR1 offset
2468c2ecf20Sopenharmony_ci * bit 10 Wrap flag for FBR1
2478c2ecf20Sopenharmony_ci * bit 16-25 FBR0 offset
2488c2ecf20Sopenharmony_ci * bit 26 Wrap flag for FBR0
2498c2ecf20Sopenharmony_ci */
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci/* RXSTAT_WORD1_t structure holds part of the status bits of the Rx DMA engine
2528c2ecf20Sopenharmony_ci * that get copied out to memory by the ET-1310.  Word 3 is a 32 bit word
2538c2ecf20Sopenharmony_ci * which contains the Packet Status Ring available offset.
2548c2ecf20Sopenharmony_ci *
2558c2ecf20Sopenharmony_ci * bit 0-15 reserved
2568c2ecf20Sopenharmony_ci * bit 16-27 PSRoffset
2578c2ecf20Sopenharmony_ci * bit 28 PSRwrap
2588c2ecf20Sopenharmony_ci * bit 29-31 unused
2598c2ecf20Sopenharmony_ci */
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci/* struct rx_status_block is a structure representing the status of the Rx
2628c2ecf20Sopenharmony_ci * DMA engine it sits in free memory, and is pointed to by 0x101c / 0x1020
2638c2ecf20Sopenharmony_ci */
2648c2ecf20Sopenharmony_cistruct rx_status_block {
2658c2ecf20Sopenharmony_ci	u32 word0;
2668c2ecf20Sopenharmony_ci	u32 word1;
2678c2ecf20Sopenharmony_ci};
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci/* Structure for look-up table holding free buffer ring pointers, addresses
2708c2ecf20Sopenharmony_ci * and state.
2718c2ecf20Sopenharmony_ci */
2728c2ecf20Sopenharmony_cistruct fbr_lookup {
2738c2ecf20Sopenharmony_ci	void		*virt[MAX_DESC_PER_RING_RX];
2748c2ecf20Sopenharmony_ci	u32		 bus_high[MAX_DESC_PER_RING_RX];
2758c2ecf20Sopenharmony_ci	u32		 bus_low[MAX_DESC_PER_RING_RX];
2768c2ecf20Sopenharmony_ci	void		*ring_virtaddr;
2778c2ecf20Sopenharmony_ci	dma_addr_t	 ring_physaddr;
2788c2ecf20Sopenharmony_ci	void		*mem_virtaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
2798c2ecf20Sopenharmony_ci	dma_addr_t	 mem_physaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
2808c2ecf20Sopenharmony_ci	u32		 local_full;
2818c2ecf20Sopenharmony_ci	u32		 num_entries;
2828c2ecf20Sopenharmony_ci	dma_addr_t	 buffsize;
2838c2ecf20Sopenharmony_ci};
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci/* struct rx_ring is the structure representing the adaptor's local
2868c2ecf20Sopenharmony_ci * reference(s) to the rings
2878c2ecf20Sopenharmony_ci */
2888c2ecf20Sopenharmony_cistruct rx_ring {
2898c2ecf20Sopenharmony_ci	struct fbr_lookup *fbr[NUM_FBRS];
2908c2ecf20Sopenharmony_ci	void *ps_ring_virtaddr;
2918c2ecf20Sopenharmony_ci	dma_addr_t ps_ring_physaddr;
2928c2ecf20Sopenharmony_ci	u32 local_psr_full;
2938c2ecf20Sopenharmony_ci	u32 psr_entries;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	struct rx_status_block *rx_status_block;
2968c2ecf20Sopenharmony_ci	dma_addr_t rx_status_bus;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	struct list_head recv_list;
2998c2ecf20Sopenharmony_ci	u32 num_ready_recv;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	u32 num_rfd;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	bool unfinished_receives;
3048c2ecf20Sopenharmony_ci};
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci/* TX defines */
3078c2ecf20Sopenharmony_ci/* word 2 of the control bits in the Tx Descriptor ring for the ET-1310
3088c2ecf20Sopenharmony_ci *
3098c2ecf20Sopenharmony_ci * 0-15: length of packet
3108c2ecf20Sopenharmony_ci * 16-27: VLAN tag
3118c2ecf20Sopenharmony_ci * 28: VLAN CFI
3128c2ecf20Sopenharmony_ci * 29-31: VLAN priority
3138c2ecf20Sopenharmony_ci *
3148c2ecf20Sopenharmony_ci * word 3 of the control bits in the Tx Descriptor ring for the ET-1310
3158c2ecf20Sopenharmony_ci *
3168c2ecf20Sopenharmony_ci * 0: last packet in the sequence
3178c2ecf20Sopenharmony_ci * 1: first packet in the sequence
3188c2ecf20Sopenharmony_ci * 2: interrupt the processor when this pkt sent
3198c2ecf20Sopenharmony_ci * 3: Control word - no packet data
3208c2ecf20Sopenharmony_ci * 4: Issue half-duplex backpressure : XON/XOFF
3218c2ecf20Sopenharmony_ci * 5: send pause frame
3228c2ecf20Sopenharmony_ci * 6: Tx frame has error
3238c2ecf20Sopenharmony_ci * 7: append CRC
3248c2ecf20Sopenharmony_ci * 8: MAC override
3258c2ecf20Sopenharmony_ci * 9: pad packet
3268c2ecf20Sopenharmony_ci * 10: Packet is a Huge packet
3278c2ecf20Sopenharmony_ci * 11: append VLAN tag
3288c2ecf20Sopenharmony_ci * 12: IP checksum assist
3298c2ecf20Sopenharmony_ci * 13: TCP checksum assist
3308c2ecf20Sopenharmony_ci * 14: UDP checksum assist
3318c2ecf20Sopenharmony_ci */
3328c2ecf20Sopenharmony_ci#define TXDESC_FLAG_LASTPKT		0x0001
3338c2ecf20Sopenharmony_ci#define TXDESC_FLAG_FIRSTPKT		0x0002
3348c2ecf20Sopenharmony_ci#define TXDESC_FLAG_INTPROC		0x0004
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci/* struct tx_desc represents each descriptor on the ring */
3378c2ecf20Sopenharmony_cistruct tx_desc {
3388c2ecf20Sopenharmony_ci	u32 addr_hi;
3398c2ecf20Sopenharmony_ci	u32 addr_lo;
3408c2ecf20Sopenharmony_ci	u32 len_vlan;	/* control words how to xmit the */
3418c2ecf20Sopenharmony_ci	u32 flags;	/* data (detailed above) */
3428c2ecf20Sopenharmony_ci};
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci/* The status of the Tx DMA engine it sits in free memory, and is pointed to
3458c2ecf20Sopenharmony_ci * by 0x101c / 0x1020. This is a DMA10 type
3468c2ecf20Sopenharmony_ci */
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci/* TCB (Transmit Control Block: Host Side) */
3498c2ecf20Sopenharmony_cistruct tcb {
3508c2ecf20Sopenharmony_ci	struct tcb *next;	/* Next entry in ring */
3518c2ecf20Sopenharmony_ci	u32 count;		/* Used to spot stuck/lost packets */
3528c2ecf20Sopenharmony_ci	u32 stale;		/* Used to spot stuck/lost packets */
3538c2ecf20Sopenharmony_ci	struct sk_buff *skb;	/* Network skb we are tied to */
3548c2ecf20Sopenharmony_ci	u32 index;		/* Ring indexes */
3558c2ecf20Sopenharmony_ci	u32 index_start;
3568c2ecf20Sopenharmony_ci};
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci/* Structure representing our local reference(s) to the ring */
3598c2ecf20Sopenharmony_cistruct tx_ring {
3608c2ecf20Sopenharmony_ci	/* TCB (Transmit Control Block) memory and lists */
3618c2ecf20Sopenharmony_ci	struct tcb *tcb_ring;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	/* List of TCBs that are ready to be used */
3648c2ecf20Sopenharmony_ci	struct tcb *tcb_qhead;
3658c2ecf20Sopenharmony_ci	struct tcb *tcb_qtail;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	/* list of TCBs that are currently being sent. */
3688c2ecf20Sopenharmony_ci	struct tcb *send_head;
3698c2ecf20Sopenharmony_ci	struct tcb *send_tail;
3708c2ecf20Sopenharmony_ci	int used;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/* The actual descriptor ring */
3738c2ecf20Sopenharmony_ci	struct tx_desc *tx_desc_ring;
3748c2ecf20Sopenharmony_ci	dma_addr_t tx_desc_ring_pa;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	/* send_idx indicates where we last wrote to in the descriptor ring. */
3778c2ecf20Sopenharmony_ci	u32 send_idx;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/* The location of the write-back status block */
3808c2ecf20Sopenharmony_ci	u32 *tx_status;
3818c2ecf20Sopenharmony_ci	dma_addr_t tx_status_pa;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	/* Packets since the last IRQ: used for interrupt coalescing */
3848c2ecf20Sopenharmony_ci	int since_irq;
3858c2ecf20Sopenharmony_ci};
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci/* Do not change these values: if changed, then change also in respective
3888c2ecf20Sopenharmony_ci * TXdma and Rxdma engines
3898c2ecf20Sopenharmony_ci */
3908c2ecf20Sopenharmony_ci#define NUM_DESC_PER_RING_TX         512    /* TX Do not change these values */
3918c2ecf20Sopenharmony_ci#define NUM_TCB                      64
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci/* These values are all superseded by registry entries to facilitate tuning.
3948c2ecf20Sopenharmony_ci * Once the desired performance has been achieved, the optimal registry values
3958c2ecf20Sopenharmony_ci * should be re-populated to these #defines:
3968c2ecf20Sopenharmony_ci */
3978c2ecf20Sopenharmony_ci#define TX_ERROR_PERIOD             1000
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci#define LO_MARK_PERCENT_FOR_PSR     15
4008c2ecf20Sopenharmony_ci#define LO_MARK_PERCENT_FOR_RX      15
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci/* RFD (Receive Frame Descriptor) */
4038c2ecf20Sopenharmony_cistruct rfd {
4048c2ecf20Sopenharmony_ci	struct list_head list_node;
4058c2ecf20Sopenharmony_ci	struct sk_buff *skb;
4068c2ecf20Sopenharmony_ci	u32 len;	/* total size of receive frame */
4078c2ecf20Sopenharmony_ci	u16 bufferindex;
4088c2ecf20Sopenharmony_ci	u8 ringindex;
4098c2ecf20Sopenharmony_ci};
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci/* Flow Control */
4128c2ecf20Sopenharmony_ci#define FLOW_BOTH	0
4138c2ecf20Sopenharmony_ci#define FLOW_TXONLY	1
4148c2ecf20Sopenharmony_ci#define FLOW_RXONLY	2
4158c2ecf20Sopenharmony_ci#define FLOW_NONE	3
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci/* Struct to define some device statistics */
4188c2ecf20Sopenharmony_cistruct ce_stats {
4198c2ecf20Sopenharmony_ci	u32		multicast_pkts_rcvd;
4208c2ecf20Sopenharmony_ci	u32		rcvd_pkts_dropped;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	u32		tx_underflows;
4238c2ecf20Sopenharmony_ci	u32		tx_collisions;
4248c2ecf20Sopenharmony_ci	u32		tx_excessive_collisions;
4258c2ecf20Sopenharmony_ci	u32		tx_first_collisions;
4268c2ecf20Sopenharmony_ci	u32		tx_late_collisions;
4278c2ecf20Sopenharmony_ci	u32		tx_max_pkt_errs;
4288c2ecf20Sopenharmony_ci	u32		tx_deferred;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	u32		rx_overflows;
4318c2ecf20Sopenharmony_ci	u32		rx_length_errs;
4328c2ecf20Sopenharmony_ci	u32		rx_align_errs;
4338c2ecf20Sopenharmony_ci	u32		rx_crc_errs;
4348c2ecf20Sopenharmony_ci	u32		rx_code_violations;
4358c2ecf20Sopenharmony_ci	u32		rx_other_errs;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	u32		interrupt_status;
4388c2ecf20Sopenharmony_ci};
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/* The private adapter structure */
4418c2ecf20Sopenharmony_cistruct et131x_adapter {
4428c2ecf20Sopenharmony_ci	struct net_device *netdev;
4438c2ecf20Sopenharmony_ci	struct pci_dev *pdev;
4448c2ecf20Sopenharmony_ci	struct mii_bus *mii_bus;
4458c2ecf20Sopenharmony_ci	struct napi_struct napi;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	/* Flags that indicate current state of the adapter */
4488c2ecf20Sopenharmony_ci	u32 flags;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	/* local link state, to determine if a state change has occurred */
4518c2ecf20Sopenharmony_ci	int link;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	/* Configuration  */
4548c2ecf20Sopenharmony_ci	u8 rom_addr[ETH_ALEN];
4558c2ecf20Sopenharmony_ci	u8 addr[ETH_ALEN];
4568c2ecf20Sopenharmony_ci	bool has_eeprom;
4578c2ecf20Sopenharmony_ci	u8 eeprom_data[2];
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	spinlock_t tcb_send_qlock; /* protects the tx_ring send tcb list */
4608c2ecf20Sopenharmony_ci	spinlock_t tcb_ready_qlock; /* protects the tx_ring ready tcb list */
4618c2ecf20Sopenharmony_ci	spinlock_t rcv_lock; /* protects the rx_ring receive list */
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	/* Packet Filter and look ahead size */
4648c2ecf20Sopenharmony_ci	u32 packet_filter;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	/* multicast list */
4678c2ecf20Sopenharmony_ci	u32 multicast_addr_count;
4688c2ecf20Sopenharmony_ci	u8 multicast_list[NIC_MAX_MCAST_LIST][ETH_ALEN];
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	/* Pointer to the device's PCI register space */
4718c2ecf20Sopenharmony_ci	struct address_map __iomem *regs;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	/* Registry parameters */
4748c2ecf20Sopenharmony_ci	u8 wanted_flow;		/* Flow we want for 802.3x flow control */
4758c2ecf20Sopenharmony_ci	u32 registry_jumbo_packet;	/* Max supported ethernet packet size */
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	/* Derived from the registry: */
4788c2ecf20Sopenharmony_ci	u8 flow;		/* flow control validated by the far-end */
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	/* Minimize init-time */
4818c2ecf20Sopenharmony_ci	struct timer_list error_timer;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	/* variable putting the phy into coma mode when boot up with no cable
4848c2ecf20Sopenharmony_ci	 * plugged in after 5 seconds
4858c2ecf20Sopenharmony_ci	 */
4868c2ecf20Sopenharmony_ci	u8 boot_coma;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	/* Tx Memory Variables */
4898c2ecf20Sopenharmony_ci	struct tx_ring tx_ring;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	/* Rx Memory Variables */
4928c2ecf20Sopenharmony_ci	struct rx_ring rx_ring;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	struct ce_stats stats;
4958c2ecf20Sopenharmony_ci};
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic int eeprom_wait_ready(struct pci_dev *pdev, u32 *status)
4988c2ecf20Sopenharmony_ci{
4998c2ecf20Sopenharmony_ci	u32 reg;
5008c2ecf20Sopenharmony_ci	int i;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	/* 1. Check LBCIF Status Register for bits 6 & 3:2 all equal to 0 and
5038c2ecf20Sopenharmony_ci	 *    bits 7,1:0 both equal to 1, at least once after reset.
5048c2ecf20Sopenharmony_ci	 *    Subsequent operations need only to check that bits 1:0 are equal
5058c2ecf20Sopenharmony_ci	 *    to 1 prior to starting a single byte read/write
5068c2ecf20Sopenharmony_ci	 */
5078c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_NUM_REGISTER_POLLS; i++) {
5088c2ecf20Sopenharmony_ci		if (pci_read_config_dword(pdev, LBCIF_DWORD1_GROUP, &reg))
5098c2ecf20Sopenharmony_ci			return -EIO;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci		/* I2C idle and Phy Queue Avail both true */
5128c2ecf20Sopenharmony_ci		if ((reg & 0x3000) == 0x3000) {
5138c2ecf20Sopenharmony_ci			if (status)
5148c2ecf20Sopenharmony_ci				*status = reg;
5158c2ecf20Sopenharmony_ci			return reg & 0xFF;
5168c2ecf20Sopenharmony_ci		}
5178c2ecf20Sopenharmony_ci	}
5188c2ecf20Sopenharmony_ci	return -ETIMEDOUT;
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_cistatic int eeprom_write(struct et131x_adapter *adapter, u32 addr, u8 data)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	struct pci_dev *pdev = adapter->pdev;
5248c2ecf20Sopenharmony_ci	int index = 0;
5258c2ecf20Sopenharmony_ci	int retries;
5268c2ecf20Sopenharmony_ci	int err = 0;
5278c2ecf20Sopenharmony_ci	int writeok = 0;
5288c2ecf20Sopenharmony_ci	u32 status;
5298c2ecf20Sopenharmony_ci	u32 val = 0;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	/* For an EEPROM, an I2C single byte write is defined as a START
5328c2ecf20Sopenharmony_ci	 * condition followed by the device address, EEPROM address, one byte
5338c2ecf20Sopenharmony_ci	 * of data and a STOP condition.  The STOP condition will trigger the
5348c2ecf20Sopenharmony_ci	 * EEPROM's internally timed write cycle to the nonvolatile memory.
5358c2ecf20Sopenharmony_ci	 * All inputs are disabled during this write cycle and the EEPROM will
5368c2ecf20Sopenharmony_ci	 * not respond to any access until the internal write is complete.
5378c2ecf20Sopenharmony_ci	 */
5388c2ecf20Sopenharmony_ci	err = eeprom_wait_ready(pdev, NULL);
5398c2ecf20Sopenharmony_ci	if (err < 0)
5408c2ecf20Sopenharmony_ci		return err;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	 /* 2. Write to the LBCIF Control Register:  bit 7=1, bit 6=1, bit 3=0,
5438c2ecf20Sopenharmony_ci	  *    and bits 1:0 both =0.  Bit 5 should be set according to the
5448c2ecf20Sopenharmony_ci	  *    type of EEPROM being accessed (1=two byte addressing, 0=one
5458c2ecf20Sopenharmony_ci	  *    byte addressing).
5468c2ecf20Sopenharmony_ci	  */
5478c2ecf20Sopenharmony_ci	if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
5488c2ecf20Sopenharmony_ci				  LBCIF_CONTROL_LBCIF_ENABLE |
5498c2ecf20Sopenharmony_ci					LBCIF_CONTROL_I2C_WRITE))
5508c2ecf20Sopenharmony_ci		return -EIO;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	/* Prepare EEPROM address for Step 3 */
5538c2ecf20Sopenharmony_ci	for (retries = 0; retries < MAX_NUM_WRITE_RETRIES; retries++) {
5548c2ecf20Sopenharmony_ci		if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
5558c2ecf20Sopenharmony_ci			break;
5568c2ecf20Sopenharmony_ci		/* Write the data to the LBCIF Data Register (the I2C write
5578c2ecf20Sopenharmony_ci		 * will begin).
5588c2ecf20Sopenharmony_ci		 */
5598c2ecf20Sopenharmony_ci		if (pci_write_config_byte(pdev, LBCIF_DATA_REGISTER, data))
5608c2ecf20Sopenharmony_ci			break;
5618c2ecf20Sopenharmony_ci		/* Monitor bit 1:0 of the LBCIF Status Register.  When bits
5628c2ecf20Sopenharmony_ci		 * 1:0 are both equal to 1, the I2C write has completed and the
5638c2ecf20Sopenharmony_ci		 * internal write cycle of the EEPROM is about to start.
5648c2ecf20Sopenharmony_ci		 * (bits 1:0 = 01 is a legal state while waiting from both
5658c2ecf20Sopenharmony_ci		 * equal to 1, but bits 1:0 = 10 is invalid and implies that
5668c2ecf20Sopenharmony_ci		 * something is broken).
5678c2ecf20Sopenharmony_ci		 */
5688c2ecf20Sopenharmony_ci		err = eeprom_wait_ready(pdev, &status);
5698c2ecf20Sopenharmony_ci		if (err < 0)
5708c2ecf20Sopenharmony_ci			return 0;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci		/* Check bit 3 of the LBCIF Status Register.  If  equal to 1,
5738c2ecf20Sopenharmony_ci		 * an error has occurred.Don't break here if we are revision
5748c2ecf20Sopenharmony_ci		 * 1, this is so we do a blind write for load bug.
5758c2ecf20Sopenharmony_ci		 */
5768c2ecf20Sopenharmony_ci		if ((status & LBCIF_STATUS_GENERAL_ERROR) &&
5778c2ecf20Sopenharmony_ci		    adapter->pdev->revision == 0)
5788c2ecf20Sopenharmony_ci			break;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci		/* Check bit 2 of the LBCIF Status Register.  If equal to 1 an
5818c2ecf20Sopenharmony_ci		 * ACK error has occurred on the address phase of the write.
5828c2ecf20Sopenharmony_ci		 * This could be due to an actual hardware failure or the
5838c2ecf20Sopenharmony_ci		 * EEPROM may still be in its internal write cycle from a
5848c2ecf20Sopenharmony_ci		 * previous write. This write operation was ignored and must be
5858c2ecf20Sopenharmony_ci		  *repeated later.
5868c2ecf20Sopenharmony_ci		 */
5878c2ecf20Sopenharmony_ci		if (status & LBCIF_STATUS_ACK_ERROR) {
5888c2ecf20Sopenharmony_ci			/* This could be due to an actual hardware failure
5898c2ecf20Sopenharmony_ci			 * or the EEPROM may still be in its internal write
5908c2ecf20Sopenharmony_ci			 * cycle from a previous write. This write operation
5918c2ecf20Sopenharmony_ci			 * was ignored and must be repeated later.
5928c2ecf20Sopenharmony_ci			 */
5938c2ecf20Sopenharmony_ci			udelay(10);
5948c2ecf20Sopenharmony_ci			continue;
5958c2ecf20Sopenharmony_ci		}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci		writeok = 1;
5988c2ecf20Sopenharmony_ci		break;
5998c2ecf20Sopenharmony_ci	}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	udelay(10);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	while (1) {
6048c2ecf20Sopenharmony_ci		if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
6058c2ecf20Sopenharmony_ci					  LBCIF_CONTROL_LBCIF_ENABLE))
6068c2ecf20Sopenharmony_ci			writeok = 0;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci		/* Do read until internal ACK_ERROR goes away meaning write
6098c2ecf20Sopenharmony_ci		 * completed
6108c2ecf20Sopenharmony_ci		 */
6118c2ecf20Sopenharmony_ci		do {
6128c2ecf20Sopenharmony_ci			pci_write_config_dword(pdev,
6138c2ecf20Sopenharmony_ci					       LBCIF_ADDRESS_REGISTER,
6148c2ecf20Sopenharmony_ci					       addr);
6158c2ecf20Sopenharmony_ci			do {
6168c2ecf20Sopenharmony_ci				pci_read_config_dword(pdev,
6178c2ecf20Sopenharmony_ci						      LBCIF_DATA_REGISTER,
6188c2ecf20Sopenharmony_ci						      &val);
6198c2ecf20Sopenharmony_ci			} while ((val & 0x00010000) == 0);
6208c2ecf20Sopenharmony_ci		} while (val & 0x00040000);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci		if ((val & 0xFF00) != 0xC000 || index == 10000)
6238c2ecf20Sopenharmony_ci			break;
6248c2ecf20Sopenharmony_ci		index++;
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci	return writeok ? 0 : -EIO;
6278c2ecf20Sopenharmony_ci}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_cistatic int eeprom_read(struct et131x_adapter *adapter, u32 addr, u8 *pdata)
6308c2ecf20Sopenharmony_ci{
6318c2ecf20Sopenharmony_ci	struct pci_dev *pdev = adapter->pdev;
6328c2ecf20Sopenharmony_ci	int err;
6338c2ecf20Sopenharmony_ci	u32 status;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/* A single byte read is similar to the single byte write, with the
6368c2ecf20Sopenharmony_ci	 * exception of the data flow:
6378c2ecf20Sopenharmony_ci	 */
6388c2ecf20Sopenharmony_ci	err = eeprom_wait_ready(pdev, NULL);
6398c2ecf20Sopenharmony_ci	if (err < 0)
6408c2ecf20Sopenharmony_ci		return err;
6418c2ecf20Sopenharmony_ci	/* Write to the LBCIF Control Register:  bit 7=1, bit 6=0, bit 3=0,
6428c2ecf20Sopenharmony_ci	 * and bits 1:0 both =0.  Bit 5 should be set according to the type
6438c2ecf20Sopenharmony_ci	 * of EEPROM being accessed (1=two byte addressing, 0=one byte
6448c2ecf20Sopenharmony_ci	 * addressing).
6458c2ecf20Sopenharmony_ci	 */
6468c2ecf20Sopenharmony_ci	if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
6478c2ecf20Sopenharmony_ci				  LBCIF_CONTROL_LBCIF_ENABLE))
6488c2ecf20Sopenharmony_ci		return -EIO;
6498c2ecf20Sopenharmony_ci	/* Write the address to the LBCIF Address Register (I2C read will
6508c2ecf20Sopenharmony_ci	 * begin).
6518c2ecf20Sopenharmony_ci	 */
6528c2ecf20Sopenharmony_ci	if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
6538c2ecf20Sopenharmony_ci		return -EIO;
6548c2ecf20Sopenharmony_ci	/* Monitor bit 0 of the LBCIF Status Register.  When = 1, I2C read
6558c2ecf20Sopenharmony_ci	 * is complete. (if bit 1 =1 and bit 0 stays = 0, a hardware failure
6568c2ecf20Sopenharmony_ci	 * has occurred).
6578c2ecf20Sopenharmony_ci	 */
6588c2ecf20Sopenharmony_ci	err = eeprom_wait_ready(pdev, &status);
6598c2ecf20Sopenharmony_ci	if (err < 0)
6608c2ecf20Sopenharmony_ci		return err;
6618c2ecf20Sopenharmony_ci	/* Regardless of error status, read data byte from LBCIF Data
6628c2ecf20Sopenharmony_ci	 * Register.
6638c2ecf20Sopenharmony_ci	 */
6648c2ecf20Sopenharmony_ci	*pdata = err;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	return (status & LBCIF_STATUS_ACK_ERROR) ? -EIO : 0;
6678c2ecf20Sopenharmony_ci}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_cistatic int et131x_init_eeprom(struct et131x_adapter *adapter)
6708c2ecf20Sopenharmony_ci{
6718c2ecf20Sopenharmony_ci	struct pci_dev *pdev = adapter->pdev;
6728c2ecf20Sopenharmony_ci	u8 eestatus;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS, &eestatus);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	/* THIS IS A WORKAROUND:
6778c2ecf20Sopenharmony_ci	 * I need to call this function twice to get my card in a
6788c2ecf20Sopenharmony_ci	 * LG M1 Express Dual running. I tried also a msleep before this
6798c2ecf20Sopenharmony_ci	 * function, because I thought there could be some time conditions
6808c2ecf20Sopenharmony_ci	 * but it didn't work. Call the whole function twice also work.
6818c2ecf20Sopenharmony_ci	 */
6828c2ecf20Sopenharmony_ci	if (pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS, &eestatus)) {
6838c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
6848c2ecf20Sopenharmony_ci			"Could not read PCI config space for EEPROM Status\n");
6858c2ecf20Sopenharmony_ci		return -EIO;
6868c2ecf20Sopenharmony_ci	}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	/* Determine if the error(s) we care about are present. If they are
6898c2ecf20Sopenharmony_ci	 * present we need to fail.
6908c2ecf20Sopenharmony_ci	 */
6918c2ecf20Sopenharmony_ci	if (eestatus & 0x4C) {
6928c2ecf20Sopenharmony_ci		int write_failed = 0;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci		if (pdev->revision == 0x01) {
6958c2ecf20Sopenharmony_ci			int	i;
6968c2ecf20Sopenharmony_ci			static const u8 eedata[4] = { 0xFE, 0x13, 0x10, 0xFF };
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci			/* Re-write the first 4 bytes if we have an eeprom
6998c2ecf20Sopenharmony_ci			 * present and the revision id is 1, this fixes the
7008c2ecf20Sopenharmony_ci			 * corruption seen with 1310 B Silicon
7018c2ecf20Sopenharmony_ci			 */
7028c2ecf20Sopenharmony_ci			for (i = 0; i < 3; i++)
7038c2ecf20Sopenharmony_ci				if (eeprom_write(adapter, i, eedata[i]) < 0)
7048c2ecf20Sopenharmony_ci					write_failed = 1;
7058c2ecf20Sopenharmony_ci		}
7068c2ecf20Sopenharmony_ci		if (pdev->revision  != 0x01 || write_failed) {
7078c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
7088c2ecf20Sopenharmony_ci				"Fatal EEPROM Status Error - 0x%04x\n",
7098c2ecf20Sopenharmony_ci				eestatus);
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci			/* This error could mean that there was an error
7128c2ecf20Sopenharmony_ci			 * reading the eeprom or that the eeprom doesn't exist.
7138c2ecf20Sopenharmony_ci			 * We will treat each case the same and not try to
7148c2ecf20Sopenharmony_ci			 * gather additional information that normally would
7158c2ecf20Sopenharmony_ci			 * come from the eeprom, like MAC Address
7168c2ecf20Sopenharmony_ci			 */
7178c2ecf20Sopenharmony_ci			adapter->has_eeprom = false;
7188c2ecf20Sopenharmony_ci			return -EIO;
7198c2ecf20Sopenharmony_ci		}
7208c2ecf20Sopenharmony_ci	}
7218c2ecf20Sopenharmony_ci	adapter->has_eeprom = true;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	/* Read the EEPROM for information regarding LED behavior. Refer to
7248c2ecf20Sopenharmony_ci	 * et131x_xcvr_init() for its use.
7258c2ecf20Sopenharmony_ci	 */
7268c2ecf20Sopenharmony_ci	eeprom_read(adapter, 0x70, &adapter->eeprom_data[0]);
7278c2ecf20Sopenharmony_ci	eeprom_read(adapter, 0x71, &adapter->eeprom_data[1]);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	if (adapter->eeprom_data[0] != 0xcd)
7308c2ecf20Sopenharmony_ci		/* Disable all optional features */
7318c2ecf20Sopenharmony_ci		adapter->eeprom_data[1] = 0x00;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	return 0;
7348c2ecf20Sopenharmony_ci}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_cistatic void et131x_rx_dma_enable(struct et131x_adapter *adapter)
7378c2ecf20Sopenharmony_ci{
7388c2ecf20Sopenharmony_ci	/* Setup the receive dma configuration register for normal operation */
7398c2ecf20Sopenharmony_ci	u32 csr =  ET_RXDMA_CSR_FBR1_ENABLE;
7408c2ecf20Sopenharmony_ci	struct rx_ring *rx_ring = &adapter->rx_ring;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	if (rx_ring->fbr[1]->buffsize == 4096)
7438c2ecf20Sopenharmony_ci		csr |= ET_RXDMA_CSR_FBR1_SIZE_LO;
7448c2ecf20Sopenharmony_ci	else if (rx_ring->fbr[1]->buffsize == 8192)
7458c2ecf20Sopenharmony_ci		csr |= ET_RXDMA_CSR_FBR1_SIZE_HI;
7468c2ecf20Sopenharmony_ci	else if (rx_ring->fbr[1]->buffsize == 16384)
7478c2ecf20Sopenharmony_ci		csr |= ET_RXDMA_CSR_FBR1_SIZE_LO | ET_RXDMA_CSR_FBR1_SIZE_HI;
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	csr |= ET_RXDMA_CSR_FBR0_ENABLE;
7508c2ecf20Sopenharmony_ci	if (rx_ring->fbr[0]->buffsize == 256)
7518c2ecf20Sopenharmony_ci		csr |= ET_RXDMA_CSR_FBR0_SIZE_LO;
7528c2ecf20Sopenharmony_ci	else if (rx_ring->fbr[0]->buffsize == 512)
7538c2ecf20Sopenharmony_ci		csr |= ET_RXDMA_CSR_FBR0_SIZE_HI;
7548c2ecf20Sopenharmony_ci	else if (rx_ring->fbr[0]->buffsize == 1024)
7558c2ecf20Sopenharmony_ci		csr |= ET_RXDMA_CSR_FBR0_SIZE_LO | ET_RXDMA_CSR_FBR0_SIZE_HI;
7568c2ecf20Sopenharmony_ci	writel(csr, &adapter->regs->rxdma.csr);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	csr = readl(&adapter->regs->rxdma.csr);
7598c2ecf20Sopenharmony_ci	if (csr & ET_RXDMA_CSR_HALT_STATUS) {
7608c2ecf20Sopenharmony_ci		udelay(5);
7618c2ecf20Sopenharmony_ci		csr = readl(&adapter->regs->rxdma.csr);
7628c2ecf20Sopenharmony_ci		if (csr & ET_RXDMA_CSR_HALT_STATUS) {
7638c2ecf20Sopenharmony_ci			dev_err(&adapter->pdev->dev,
7648c2ecf20Sopenharmony_ci				"RX Dma failed to exit halt state. CSR 0x%08x\n",
7658c2ecf20Sopenharmony_ci				csr);
7668c2ecf20Sopenharmony_ci		}
7678c2ecf20Sopenharmony_ci	}
7688c2ecf20Sopenharmony_ci}
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_cistatic void et131x_rx_dma_disable(struct et131x_adapter *adapter)
7718c2ecf20Sopenharmony_ci{
7728c2ecf20Sopenharmony_ci	u32 csr;
7738c2ecf20Sopenharmony_ci	/* Setup the receive dma configuration register */
7748c2ecf20Sopenharmony_ci	writel(ET_RXDMA_CSR_HALT | ET_RXDMA_CSR_FBR1_ENABLE,
7758c2ecf20Sopenharmony_ci	       &adapter->regs->rxdma.csr);
7768c2ecf20Sopenharmony_ci	csr = readl(&adapter->regs->rxdma.csr);
7778c2ecf20Sopenharmony_ci	if (!(csr & ET_RXDMA_CSR_HALT_STATUS)) {
7788c2ecf20Sopenharmony_ci		udelay(5);
7798c2ecf20Sopenharmony_ci		csr = readl(&adapter->regs->rxdma.csr);
7808c2ecf20Sopenharmony_ci		if (!(csr & ET_RXDMA_CSR_HALT_STATUS))
7818c2ecf20Sopenharmony_ci			dev_err(&adapter->pdev->dev,
7828c2ecf20Sopenharmony_ci				"RX Dma failed to enter halt state. CSR 0x%08x\n",
7838c2ecf20Sopenharmony_ci				csr);
7848c2ecf20Sopenharmony_ci	}
7858c2ecf20Sopenharmony_ci}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_cistatic void et131x_tx_dma_enable(struct et131x_adapter *adapter)
7888c2ecf20Sopenharmony_ci{
7898c2ecf20Sopenharmony_ci	/* Setup the transmit dma configuration register for normal
7908c2ecf20Sopenharmony_ci	 * operation
7918c2ecf20Sopenharmony_ci	 */
7928c2ecf20Sopenharmony_ci	writel(ET_TXDMA_SNGL_EPKT | (PARM_DMA_CACHE_DEF << ET_TXDMA_CACHE_SHIFT),
7938c2ecf20Sopenharmony_ci	       &adapter->regs->txdma.csr);
7948c2ecf20Sopenharmony_ci}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_cistatic inline void add_10bit(u32 *v, int n)
7978c2ecf20Sopenharmony_ci{
7988c2ecf20Sopenharmony_ci	*v = INDEX10(*v + n) | (*v & ET_DMA10_WRAP);
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_cistatic inline void add_12bit(u32 *v, int n)
8028c2ecf20Sopenharmony_ci{
8038c2ecf20Sopenharmony_ci	*v = INDEX12(*v + n) | (*v & ET_DMA12_WRAP);
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_cistatic void et1310_config_mac_regs1(struct et131x_adapter *adapter)
8078c2ecf20Sopenharmony_ci{
8088c2ecf20Sopenharmony_ci	struct mac_regs __iomem *macregs = &adapter->regs->mac;
8098c2ecf20Sopenharmony_ci	u32 station1;
8108c2ecf20Sopenharmony_ci	u32 station2;
8118c2ecf20Sopenharmony_ci	u32 ipg;
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	/* First we need to reset everything.  Write to MAC configuration
8148c2ecf20Sopenharmony_ci	 * register 1 to perform reset.
8158c2ecf20Sopenharmony_ci	 */
8168c2ecf20Sopenharmony_ci	writel(ET_MAC_CFG1_SOFT_RESET | ET_MAC_CFG1_SIM_RESET  |
8178c2ecf20Sopenharmony_ci	       ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
8188c2ecf20Sopenharmony_ci	       ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC,
8198c2ecf20Sopenharmony_ci	       &macregs->cfg1);
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	/* Next lets configure the MAC Inter-packet gap register */
8228c2ecf20Sopenharmony_ci	ipg = 0x38005860;		/* IPG1 0x38 IPG2 0x58 B2B 0x60 */
8238c2ecf20Sopenharmony_ci	ipg |= 0x50 << 8;		/* ifg enforce 0x50 */
8248c2ecf20Sopenharmony_ci	writel(ipg, &macregs->ipg);
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	/* Next lets configure the MAC Half Duplex register */
8278c2ecf20Sopenharmony_ci	/* BEB trunc 0xA, Ex Defer, Rexmit 0xF Coll 0x37 */
8288c2ecf20Sopenharmony_ci	writel(0x00A1F037, &macregs->hfdp);
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	/* Next lets configure the MAC Interface Control register */
8318c2ecf20Sopenharmony_ci	writel(0, &macregs->if_ctrl);
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	writel(ET_MAC_MIIMGMT_CLK_RST, &macregs->mii_mgmt_cfg);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	/* Next lets configure the MAC Station Address register.  These
8368c2ecf20Sopenharmony_ci	 * values are read from the EEPROM during initialization and stored
8378c2ecf20Sopenharmony_ci	 * in the adapter structure.  We write what is stored in the adapter
8388c2ecf20Sopenharmony_ci	 * structure to the MAC Station Address registers high and low.  This
8398c2ecf20Sopenharmony_ci	 * station address is used for generating and checking pause control
8408c2ecf20Sopenharmony_ci	 * packets.
8418c2ecf20Sopenharmony_ci	 */
8428c2ecf20Sopenharmony_ci	station2 = (adapter->addr[1] << ET_MAC_STATION_ADDR2_OC2_SHIFT) |
8438c2ecf20Sopenharmony_ci		   (adapter->addr[0] << ET_MAC_STATION_ADDR2_OC1_SHIFT);
8448c2ecf20Sopenharmony_ci	station1 = (adapter->addr[5] << ET_MAC_STATION_ADDR1_OC6_SHIFT) |
8458c2ecf20Sopenharmony_ci		   (adapter->addr[4] << ET_MAC_STATION_ADDR1_OC5_SHIFT) |
8468c2ecf20Sopenharmony_ci		   (adapter->addr[3] << ET_MAC_STATION_ADDR1_OC4_SHIFT) |
8478c2ecf20Sopenharmony_ci		    adapter->addr[2];
8488c2ecf20Sopenharmony_ci	writel(station1, &macregs->station_addr_1);
8498c2ecf20Sopenharmony_ci	writel(station2, &macregs->station_addr_2);
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	/* Max ethernet packet in bytes that will be passed by the mac without
8528c2ecf20Sopenharmony_ci	 * being truncated.  Allow the MAC to pass 4 more than our max packet
8538c2ecf20Sopenharmony_ci	 * size.  This is 4 for the Ethernet CRC.
8548c2ecf20Sopenharmony_ci	 *
8558c2ecf20Sopenharmony_ci	 * Packets larger than (registry_jumbo_packet) that do not contain a
8568c2ecf20Sopenharmony_ci	 * VLAN ID will be dropped by the Rx function.
8578c2ecf20Sopenharmony_ci	 */
8588c2ecf20Sopenharmony_ci	writel(adapter->registry_jumbo_packet + 4, &macregs->max_fm_len);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	/* clear out MAC config reset */
8618c2ecf20Sopenharmony_ci	writel(0, &macregs->cfg1);
8628c2ecf20Sopenharmony_ci}
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_cistatic void et1310_config_mac_regs2(struct et131x_adapter *adapter)
8658c2ecf20Sopenharmony_ci{
8668c2ecf20Sopenharmony_ci	int32_t delay = 0;
8678c2ecf20Sopenharmony_ci	struct mac_regs __iomem *mac = &adapter->regs->mac;
8688c2ecf20Sopenharmony_ci	struct phy_device *phydev = adapter->netdev->phydev;
8698c2ecf20Sopenharmony_ci	u32 cfg1;
8708c2ecf20Sopenharmony_ci	u32 cfg2;
8718c2ecf20Sopenharmony_ci	u32 ifctrl;
8728c2ecf20Sopenharmony_ci	u32 ctl;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	ctl = readl(&adapter->regs->txmac.ctl);
8758c2ecf20Sopenharmony_ci	cfg1 = readl(&mac->cfg1);
8768c2ecf20Sopenharmony_ci	cfg2 = readl(&mac->cfg2);
8778c2ecf20Sopenharmony_ci	ifctrl = readl(&mac->if_ctrl);
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	/* Set up the if mode bits */
8808c2ecf20Sopenharmony_ci	cfg2 &= ~ET_MAC_CFG2_IFMODE_MASK;
8818c2ecf20Sopenharmony_ci	if (phydev->speed == SPEED_1000) {
8828c2ecf20Sopenharmony_ci		cfg2 |= ET_MAC_CFG2_IFMODE_1000;
8838c2ecf20Sopenharmony_ci		ifctrl &= ~ET_MAC_IFCTRL_PHYMODE;
8848c2ecf20Sopenharmony_ci	} else {
8858c2ecf20Sopenharmony_ci		cfg2 |= ET_MAC_CFG2_IFMODE_100;
8868c2ecf20Sopenharmony_ci		ifctrl |= ET_MAC_IFCTRL_PHYMODE;
8878c2ecf20Sopenharmony_ci	}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	cfg1 |= ET_MAC_CFG1_RX_ENABLE | ET_MAC_CFG1_TX_ENABLE |
8908c2ecf20Sopenharmony_ci							ET_MAC_CFG1_TX_FLOW;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	cfg1 &= ~(ET_MAC_CFG1_LOOPBACK | ET_MAC_CFG1_RX_FLOW);
8938c2ecf20Sopenharmony_ci	if (adapter->flow == FLOW_RXONLY || adapter->flow == FLOW_BOTH)
8948c2ecf20Sopenharmony_ci		cfg1 |= ET_MAC_CFG1_RX_FLOW;
8958c2ecf20Sopenharmony_ci	writel(cfg1, &mac->cfg1);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	/* Now we need to initialize the MAC Configuration 2 register */
8988c2ecf20Sopenharmony_ci	/* preamble 7, check length, huge frame off, pad crc, crc enable
8998c2ecf20Sopenharmony_ci	 * full duplex off
9008c2ecf20Sopenharmony_ci	 */
9018c2ecf20Sopenharmony_ci	cfg2 |= 0x7 << ET_MAC_CFG2_PREAMBLE_SHIFT;
9028c2ecf20Sopenharmony_ci	cfg2 |= ET_MAC_CFG2_IFMODE_LEN_CHECK;
9038c2ecf20Sopenharmony_ci	cfg2 |= ET_MAC_CFG2_IFMODE_PAD_CRC;
9048c2ecf20Sopenharmony_ci	cfg2 |=	ET_MAC_CFG2_IFMODE_CRC_ENABLE;
9058c2ecf20Sopenharmony_ci	cfg2 &= ~ET_MAC_CFG2_IFMODE_HUGE_FRAME;
9068c2ecf20Sopenharmony_ci	cfg2 &= ~ET_MAC_CFG2_IFMODE_FULL_DPLX;
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	if (phydev->duplex == DUPLEX_FULL)
9098c2ecf20Sopenharmony_ci		cfg2 |= ET_MAC_CFG2_IFMODE_FULL_DPLX;
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	ifctrl &= ~ET_MAC_IFCTRL_GHDMODE;
9128c2ecf20Sopenharmony_ci	if (phydev->duplex == DUPLEX_HALF)
9138c2ecf20Sopenharmony_ci		ifctrl |= ET_MAC_IFCTRL_GHDMODE;
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	writel(ifctrl, &mac->if_ctrl);
9168c2ecf20Sopenharmony_ci	writel(cfg2, &mac->cfg2);
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	do {
9198c2ecf20Sopenharmony_ci		udelay(10);
9208c2ecf20Sopenharmony_ci		delay++;
9218c2ecf20Sopenharmony_ci		cfg1 = readl(&mac->cfg1);
9228c2ecf20Sopenharmony_ci	} while ((cfg1 & ET_MAC_CFG1_WAIT) != ET_MAC_CFG1_WAIT && delay < 100);
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci	if (delay == 100) {
9258c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev,
9268c2ecf20Sopenharmony_ci			 "Syncd bits did not respond correctly cfg1 word 0x%08x\n",
9278c2ecf20Sopenharmony_ci			 cfg1);
9288c2ecf20Sopenharmony_ci	}
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	ctl |= ET_TX_CTRL_TXMAC_ENABLE | ET_TX_CTRL_FC_DISABLE;
9318c2ecf20Sopenharmony_ci	writel(ctl, &adapter->regs->txmac.ctl);
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	if (adapter->flags & FMP_ADAPTER_LOWER_POWER) {
9348c2ecf20Sopenharmony_ci		et131x_rx_dma_enable(adapter);
9358c2ecf20Sopenharmony_ci		et131x_tx_dma_enable(adapter);
9368c2ecf20Sopenharmony_ci	}
9378c2ecf20Sopenharmony_ci}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_cistatic int et1310_in_phy_coma(struct et131x_adapter *adapter)
9408c2ecf20Sopenharmony_ci{
9418c2ecf20Sopenharmony_ci	u32 pmcsr = readl(&adapter->regs->global.pm_csr);
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	return ET_PM_PHY_SW_COMA & pmcsr ? 1 : 0;
9448c2ecf20Sopenharmony_ci}
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_cistatic void et1310_setup_device_for_multicast(struct et131x_adapter *adapter)
9478c2ecf20Sopenharmony_ci{
9488c2ecf20Sopenharmony_ci	struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
9498c2ecf20Sopenharmony_ci	u32 hash1 = 0;
9508c2ecf20Sopenharmony_ci	u32 hash2 = 0;
9518c2ecf20Sopenharmony_ci	u32 hash3 = 0;
9528c2ecf20Sopenharmony_ci	u32 hash4 = 0;
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	/* If ET131X_PACKET_TYPE_MULTICAST is specified, then we provision
9558c2ecf20Sopenharmony_ci	 * the multi-cast LIST.  If it is NOT specified, (and "ALL" is not
9568c2ecf20Sopenharmony_ci	 * specified) then we should pass NO multi-cast addresses to the
9578c2ecf20Sopenharmony_ci	 * driver.
9588c2ecf20Sopenharmony_ci	 */
9598c2ecf20Sopenharmony_ci	if (adapter->packet_filter & ET131X_PACKET_TYPE_MULTICAST) {
9608c2ecf20Sopenharmony_ci		int i;
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci		/* Loop through our multicast array and set up the device */
9638c2ecf20Sopenharmony_ci		for (i = 0; i < adapter->multicast_addr_count; i++) {
9648c2ecf20Sopenharmony_ci			u32 result;
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci			result = ether_crc(6, adapter->multicast_list[i]);
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci			result = (result & 0x3F800000) >> 23;
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci			if (result < 32) {
9718c2ecf20Sopenharmony_ci				hash1 |= (1 << result);
9728c2ecf20Sopenharmony_ci			} else if ((31 < result) && (result < 64)) {
9738c2ecf20Sopenharmony_ci				result -= 32;
9748c2ecf20Sopenharmony_ci				hash2 |= (1 << result);
9758c2ecf20Sopenharmony_ci			} else if ((63 < result) && (result < 96)) {
9768c2ecf20Sopenharmony_ci				result -= 64;
9778c2ecf20Sopenharmony_ci				hash3 |= (1 << result);
9788c2ecf20Sopenharmony_ci			} else {
9798c2ecf20Sopenharmony_ci				result -= 96;
9808c2ecf20Sopenharmony_ci				hash4 |= (1 << result);
9818c2ecf20Sopenharmony_ci			}
9828c2ecf20Sopenharmony_ci		}
9838c2ecf20Sopenharmony_ci	}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	/* Write out the new hash to the device */
9868c2ecf20Sopenharmony_ci	if (!et1310_in_phy_coma(adapter)) {
9878c2ecf20Sopenharmony_ci		writel(hash1, &rxmac->multi_hash1);
9888c2ecf20Sopenharmony_ci		writel(hash2, &rxmac->multi_hash2);
9898c2ecf20Sopenharmony_ci		writel(hash3, &rxmac->multi_hash3);
9908c2ecf20Sopenharmony_ci		writel(hash4, &rxmac->multi_hash4);
9918c2ecf20Sopenharmony_ci	}
9928c2ecf20Sopenharmony_ci}
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_cistatic void et1310_setup_device_for_unicast(struct et131x_adapter *adapter)
9958c2ecf20Sopenharmony_ci{
9968c2ecf20Sopenharmony_ci	struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
9978c2ecf20Sopenharmony_ci	u32 uni_pf1;
9988c2ecf20Sopenharmony_ci	u32 uni_pf2;
9998c2ecf20Sopenharmony_ci	u32 uni_pf3;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	/* Set up unicast packet filter reg 3 to be the first two octets of
10028c2ecf20Sopenharmony_ci	 * the MAC address for both address
10038c2ecf20Sopenharmony_ci	 *
10048c2ecf20Sopenharmony_ci	 * Set up unicast packet filter reg 2 to be the octets 2 - 5 of the
10058c2ecf20Sopenharmony_ci	 * MAC address for second address
10068c2ecf20Sopenharmony_ci	 *
10078c2ecf20Sopenharmony_ci	 * Set up unicast packet filter reg 3 to be the octets 2 - 5 of the
10088c2ecf20Sopenharmony_ci	 * MAC address for first address
10098c2ecf20Sopenharmony_ci	 */
10108c2ecf20Sopenharmony_ci	uni_pf3 = (adapter->addr[0] << ET_RX_UNI_PF_ADDR2_1_SHIFT) |
10118c2ecf20Sopenharmony_ci		  (adapter->addr[1] << ET_RX_UNI_PF_ADDR2_2_SHIFT) |
10128c2ecf20Sopenharmony_ci		  (adapter->addr[0] << ET_RX_UNI_PF_ADDR1_1_SHIFT) |
10138c2ecf20Sopenharmony_ci		   adapter->addr[1];
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	uni_pf2 = (adapter->addr[2] << ET_RX_UNI_PF_ADDR2_3_SHIFT) |
10168c2ecf20Sopenharmony_ci		  (adapter->addr[3] << ET_RX_UNI_PF_ADDR2_4_SHIFT) |
10178c2ecf20Sopenharmony_ci		  (adapter->addr[4] << ET_RX_UNI_PF_ADDR2_5_SHIFT) |
10188c2ecf20Sopenharmony_ci		   adapter->addr[5];
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	uni_pf1 = (adapter->addr[2] << ET_RX_UNI_PF_ADDR1_3_SHIFT) |
10218c2ecf20Sopenharmony_ci		  (adapter->addr[3] << ET_RX_UNI_PF_ADDR1_4_SHIFT) |
10228c2ecf20Sopenharmony_ci		  (adapter->addr[4] << ET_RX_UNI_PF_ADDR1_5_SHIFT) |
10238c2ecf20Sopenharmony_ci		   adapter->addr[5];
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	if (!et1310_in_phy_coma(adapter)) {
10268c2ecf20Sopenharmony_ci		writel(uni_pf1, &rxmac->uni_pf_addr1);
10278c2ecf20Sopenharmony_ci		writel(uni_pf2, &rxmac->uni_pf_addr2);
10288c2ecf20Sopenharmony_ci		writel(uni_pf3, &rxmac->uni_pf_addr3);
10298c2ecf20Sopenharmony_ci	}
10308c2ecf20Sopenharmony_ci}
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_cistatic void et1310_config_rxmac_regs(struct et131x_adapter *adapter)
10338c2ecf20Sopenharmony_ci{
10348c2ecf20Sopenharmony_ci	struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
10358c2ecf20Sopenharmony_ci	struct phy_device *phydev = adapter->netdev->phydev;
10368c2ecf20Sopenharmony_ci	u32 sa_lo;
10378c2ecf20Sopenharmony_ci	u32 sa_hi = 0;
10388c2ecf20Sopenharmony_ci	u32 pf_ctrl = 0;
10398c2ecf20Sopenharmony_ci	u32 __iomem *wolw;
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	/* Disable the MAC while it is being configured (also disable WOL) */
10428c2ecf20Sopenharmony_ci	writel(0x8, &rxmac->ctrl);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci	/* Initialize WOL to disabled. */
10458c2ecf20Sopenharmony_ci	writel(0, &rxmac->crc0);
10468c2ecf20Sopenharmony_ci	writel(0, &rxmac->crc12);
10478c2ecf20Sopenharmony_ci	writel(0, &rxmac->crc34);
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci	/* We need to set the WOL mask0 - mask4 next.  We initialize it to
10508c2ecf20Sopenharmony_ci	 * its default Values of 0x00000000 because there are not WOL masks
10518c2ecf20Sopenharmony_ci	 * as of this time.
10528c2ecf20Sopenharmony_ci	 */
10538c2ecf20Sopenharmony_ci	for (wolw = &rxmac->mask0_word0; wolw <= &rxmac->mask4_word3; wolw++)
10548c2ecf20Sopenharmony_ci		writel(0, wolw);
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	/* Lets setup the WOL Source Address */
10578c2ecf20Sopenharmony_ci	sa_lo = (adapter->addr[2] << ET_RX_WOL_LO_SA3_SHIFT) |
10588c2ecf20Sopenharmony_ci		(adapter->addr[3] << ET_RX_WOL_LO_SA4_SHIFT) |
10598c2ecf20Sopenharmony_ci		(adapter->addr[4] << ET_RX_WOL_LO_SA5_SHIFT) |
10608c2ecf20Sopenharmony_ci		 adapter->addr[5];
10618c2ecf20Sopenharmony_ci	writel(sa_lo, &rxmac->sa_lo);
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	sa_hi = (u32)(adapter->addr[0] << ET_RX_WOL_HI_SA1_SHIFT) |
10648c2ecf20Sopenharmony_ci		       adapter->addr[1];
10658c2ecf20Sopenharmony_ci	writel(sa_hi, &rxmac->sa_hi);
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	/* Disable all Packet Filtering */
10688c2ecf20Sopenharmony_ci	writel(0, &rxmac->pf_ctrl);
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	/* Let's initialize the Unicast Packet filtering address */
10718c2ecf20Sopenharmony_ci	if (adapter->packet_filter & ET131X_PACKET_TYPE_DIRECTED) {
10728c2ecf20Sopenharmony_ci		et1310_setup_device_for_unicast(adapter);
10738c2ecf20Sopenharmony_ci		pf_ctrl |= ET_RX_PFCTRL_UNICST_FILTER_ENABLE;
10748c2ecf20Sopenharmony_ci	} else {
10758c2ecf20Sopenharmony_ci		writel(0, &rxmac->uni_pf_addr1);
10768c2ecf20Sopenharmony_ci		writel(0, &rxmac->uni_pf_addr2);
10778c2ecf20Sopenharmony_ci		writel(0, &rxmac->uni_pf_addr3);
10788c2ecf20Sopenharmony_ci	}
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	/* Let's initialize the Multicast hash */
10818c2ecf20Sopenharmony_ci	if (!(adapter->packet_filter & ET131X_PACKET_TYPE_ALL_MULTICAST)) {
10828c2ecf20Sopenharmony_ci		pf_ctrl |= ET_RX_PFCTRL_MLTCST_FILTER_ENABLE;
10838c2ecf20Sopenharmony_ci		et1310_setup_device_for_multicast(adapter);
10848c2ecf20Sopenharmony_ci	}
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	/* Runt packet filtering.  Didn't work in version A silicon. */
10878c2ecf20Sopenharmony_ci	pf_ctrl |= (NIC_MIN_PACKET_SIZE + 4) << ET_RX_PFCTRL_MIN_PKT_SZ_SHIFT;
10888c2ecf20Sopenharmony_ci	pf_ctrl |= ET_RX_PFCTRL_FRAG_FILTER_ENABLE;
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	if (adapter->registry_jumbo_packet > 8192)
10918c2ecf20Sopenharmony_ci		/* In order to transmit jumbo packets greater than 8k, the
10928c2ecf20Sopenharmony_ci		 * FIFO between RxMAC and RxDMA needs to be reduced in size
10938c2ecf20Sopenharmony_ci		 * to (16k - Jumbo packet size).  In order to implement this,
10948c2ecf20Sopenharmony_ci		 * we must use "cut through" mode in the RxMAC, which chops
10958c2ecf20Sopenharmony_ci		 * packets down into segments which are (max_size * 16).  In
10968c2ecf20Sopenharmony_ci		 * this case we selected 256 bytes, since this is the size of
10978c2ecf20Sopenharmony_ci		 * the PCI-Express TLP's that the 1310 uses.
10988c2ecf20Sopenharmony_ci		 *
10998c2ecf20Sopenharmony_ci		 * seg_en on, fc_en off, size 0x10
11008c2ecf20Sopenharmony_ci		 */
11018c2ecf20Sopenharmony_ci		writel(0x41, &rxmac->mcif_ctrl_max_seg);
11028c2ecf20Sopenharmony_ci	else
11038c2ecf20Sopenharmony_ci		writel(0, &rxmac->mcif_ctrl_max_seg);
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	writel(0, &rxmac->mcif_water_mark);
11068c2ecf20Sopenharmony_ci	writel(0, &rxmac->mif_ctrl);
11078c2ecf20Sopenharmony_ci	writel(0, &rxmac->space_avail);
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	/* Initialize the the mif_ctrl register
11108c2ecf20Sopenharmony_ci	 * bit 3:  Receive code error. One or more nibbles were signaled as
11118c2ecf20Sopenharmony_ci	 *	   errors  during the reception of the packet.  Clear this
11128c2ecf20Sopenharmony_ci	 *	   bit in Gigabit, set it in 100Mbit.  This was derived
11138c2ecf20Sopenharmony_ci	 *	   experimentally at UNH.
11148c2ecf20Sopenharmony_ci	 * bit 4:  Receive CRC error. The packet's CRC did not match the
11158c2ecf20Sopenharmony_ci	 *	   internally generated CRC.
11168c2ecf20Sopenharmony_ci	 * bit 5:  Receive length check error. Indicates that frame length
11178c2ecf20Sopenharmony_ci	 *	   field value in the packet does not match the actual data
11188c2ecf20Sopenharmony_ci	 *	   byte length and is not a type field.
11198c2ecf20Sopenharmony_ci	 * bit 16: Receive frame truncated.
11208c2ecf20Sopenharmony_ci	 * bit 17: Drop packet enable
11218c2ecf20Sopenharmony_ci	 */
11228c2ecf20Sopenharmony_ci	if (phydev && phydev->speed == SPEED_100)
11238c2ecf20Sopenharmony_ci		writel(0x30038, &rxmac->mif_ctrl);
11248c2ecf20Sopenharmony_ci	else
11258c2ecf20Sopenharmony_ci		writel(0x30030, &rxmac->mif_ctrl);
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ci	/* Finally we initialize RxMac to be enabled & WOL disabled.  Packet
11288c2ecf20Sopenharmony_ci	 * filter is always enabled since it is where the runt packets are
11298c2ecf20Sopenharmony_ci	 * supposed to be dropped.  For version A silicon, runt packet
11308c2ecf20Sopenharmony_ci	 * dropping doesn't work, so it is disabled in the pf_ctrl register,
11318c2ecf20Sopenharmony_ci	 * but we still leave the packet filter on.
11328c2ecf20Sopenharmony_ci	 */
11338c2ecf20Sopenharmony_ci	writel(pf_ctrl, &rxmac->pf_ctrl);
11348c2ecf20Sopenharmony_ci	writel(ET_RX_CTRL_RXMAC_ENABLE | ET_RX_CTRL_WOL_DISABLE, &rxmac->ctrl);
11358c2ecf20Sopenharmony_ci}
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_cistatic void et1310_config_txmac_regs(struct et131x_adapter *adapter)
11388c2ecf20Sopenharmony_ci{
11398c2ecf20Sopenharmony_ci	struct txmac_regs __iomem *txmac = &adapter->regs->txmac;
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci	/* We need to update the Control Frame Parameters
11428c2ecf20Sopenharmony_ci	 * cfpt - control frame pause timer set to 64 (0x40)
11438c2ecf20Sopenharmony_ci	 * cfep - control frame extended pause timer set to 0x0
11448c2ecf20Sopenharmony_ci	 */
11458c2ecf20Sopenharmony_ci	if (adapter->flow == FLOW_NONE)
11468c2ecf20Sopenharmony_ci		writel(0, &txmac->cf_param);
11478c2ecf20Sopenharmony_ci	else
11488c2ecf20Sopenharmony_ci		writel(0x40, &txmac->cf_param);
11498c2ecf20Sopenharmony_ci}
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_cistatic void et1310_config_macstat_regs(struct et131x_adapter *adapter)
11528c2ecf20Sopenharmony_ci{
11538c2ecf20Sopenharmony_ci	struct macstat_regs __iomem *macstat = &adapter->regs->macstat;
11548c2ecf20Sopenharmony_ci	u32 __iomem *reg;
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci	/* initialize all the macstat registers to zero on the device  */
11578c2ecf20Sopenharmony_ci	for (reg = &macstat->txrx_0_64_byte_frames;
11588c2ecf20Sopenharmony_ci	     reg <= &macstat->carry_reg2; reg++)
11598c2ecf20Sopenharmony_ci		writel(0, reg);
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	/* Unmask any counters that we want to track the overflow of.
11628c2ecf20Sopenharmony_ci	 * Initially this will be all counters.  It may become clear later
11638c2ecf20Sopenharmony_ci	 * that we do not need to track all counters.
11648c2ecf20Sopenharmony_ci	 */
11658c2ecf20Sopenharmony_ci	writel(0xFFFFBE32, &macstat->carry_reg1_mask);
11668c2ecf20Sopenharmony_ci	writel(0xFFFE7E8B, &macstat->carry_reg2_mask);
11678c2ecf20Sopenharmony_ci}
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_cistatic int et131x_phy_mii_read(struct et131x_adapter *adapter, u8 addr,
11708c2ecf20Sopenharmony_ci			       u8 reg, u16 *value)
11718c2ecf20Sopenharmony_ci{
11728c2ecf20Sopenharmony_ci	struct mac_regs __iomem *mac = &adapter->regs->mac;
11738c2ecf20Sopenharmony_ci	int status = 0;
11748c2ecf20Sopenharmony_ci	u32 delay = 0;
11758c2ecf20Sopenharmony_ci	u32 mii_addr;
11768c2ecf20Sopenharmony_ci	u32 mii_cmd;
11778c2ecf20Sopenharmony_ci	u32 mii_indicator;
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	/* Save a local copy of the registers we are dealing with so we can
11808c2ecf20Sopenharmony_ci	 * set them back
11818c2ecf20Sopenharmony_ci	 */
11828c2ecf20Sopenharmony_ci	mii_addr = readl(&mac->mii_mgmt_addr);
11838c2ecf20Sopenharmony_ci	mii_cmd = readl(&mac->mii_mgmt_cmd);
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	/* Stop the current operation */
11868c2ecf20Sopenharmony_ci	writel(0, &mac->mii_mgmt_cmd);
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	/* Set up the register we need to read from on the correct PHY */
11898c2ecf20Sopenharmony_ci	writel(ET_MAC_MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	writel(0x1, &mac->mii_mgmt_cmd);
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	do {
11948c2ecf20Sopenharmony_ci		udelay(50);
11958c2ecf20Sopenharmony_ci		delay++;
11968c2ecf20Sopenharmony_ci		mii_indicator = readl(&mac->mii_mgmt_indicator);
11978c2ecf20Sopenharmony_ci	} while ((mii_indicator & ET_MAC_MGMT_WAIT) && delay < 50);
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci	/* If we hit the max delay, we could not read the register */
12008c2ecf20Sopenharmony_ci	if (delay == 50) {
12018c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev,
12028c2ecf20Sopenharmony_ci			 "reg 0x%08x could not be read\n", reg);
12038c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev, "status is  0x%08x\n",
12048c2ecf20Sopenharmony_ci			 mii_indicator);
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci		status = -EIO;
12078c2ecf20Sopenharmony_ci		goto out;
12088c2ecf20Sopenharmony_ci	}
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	/* If we hit here we were able to read the register and we need to
12118c2ecf20Sopenharmony_ci	 * return the value to the caller
12128c2ecf20Sopenharmony_ci	 */
12138c2ecf20Sopenharmony_ci	*value = readl(&mac->mii_mgmt_stat) & ET_MAC_MIIMGMT_STAT_PHYCRTL_MASK;
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ciout:
12168c2ecf20Sopenharmony_ci	/* Stop the read operation */
12178c2ecf20Sopenharmony_ci	writel(0, &mac->mii_mgmt_cmd);
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	/* set the registers we touched back to the state at which we entered
12208c2ecf20Sopenharmony_ci	 * this function
12218c2ecf20Sopenharmony_ci	 */
12228c2ecf20Sopenharmony_ci	writel(mii_addr, &mac->mii_mgmt_addr);
12238c2ecf20Sopenharmony_ci	writel(mii_cmd, &mac->mii_mgmt_cmd);
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	return status;
12268c2ecf20Sopenharmony_ci}
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_cistatic int et131x_mii_read(struct et131x_adapter *adapter, u8 reg, u16 *value)
12298c2ecf20Sopenharmony_ci{
12308c2ecf20Sopenharmony_ci	struct phy_device *phydev = adapter->netdev->phydev;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	if (!phydev)
12338c2ecf20Sopenharmony_ci		return -EIO;
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci	return et131x_phy_mii_read(adapter, phydev->mdio.addr, reg, value);
12368c2ecf20Sopenharmony_ci}
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_cistatic int et131x_mii_write(struct et131x_adapter *adapter, u8 addr, u8 reg,
12398c2ecf20Sopenharmony_ci			    u16 value)
12408c2ecf20Sopenharmony_ci{
12418c2ecf20Sopenharmony_ci	struct mac_regs __iomem *mac = &adapter->regs->mac;
12428c2ecf20Sopenharmony_ci	int status = 0;
12438c2ecf20Sopenharmony_ci	u32 delay = 0;
12448c2ecf20Sopenharmony_ci	u32 mii_addr;
12458c2ecf20Sopenharmony_ci	u32 mii_cmd;
12468c2ecf20Sopenharmony_ci	u32 mii_indicator;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	/* Save a local copy of the registers we are dealing with so we can
12498c2ecf20Sopenharmony_ci	 * set them back
12508c2ecf20Sopenharmony_ci	 */
12518c2ecf20Sopenharmony_ci	mii_addr = readl(&mac->mii_mgmt_addr);
12528c2ecf20Sopenharmony_ci	mii_cmd = readl(&mac->mii_mgmt_cmd);
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	/* Stop the current operation */
12558c2ecf20Sopenharmony_ci	writel(0, &mac->mii_mgmt_cmd);
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	/* Set up the register we need to write to on the correct PHY */
12588c2ecf20Sopenharmony_ci	writel(ET_MAC_MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci	/* Add the value to write to the registers to the mac */
12618c2ecf20Sopenharmony_ci	writel(value, &mac->mii_mgmt_ctrl);
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	do {
12648c2ecf20Sopenharmony_ci		udelay(50);
12658c2ecf20Sopenharmony_ci		delay++;
12668c2ecf20Sopenharmony_ci		mii_indicator = readl(&mac->mii_mgmt_indicator);
12678c2ecf20Sopenharmony_ci	} while ((mii_indicator & ET_MAC_MGMT_BUSY) && delay < 100);
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	/* If we hit the max delay, we could not write the register */
12708c2ecf20Sopenharmony_ci	if (delay == 100) {
12718c2ecf20Sopenharmony_ci		u16 tmp;
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev,
12748c2ecf20Sopenharmony_ci			 "reg 0x%08x could not be written", reg);
12758c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev, "status is  0x%08x\n",
12768c2ecf20Sopenharmony_ci			 mii_indicator);
12778c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev, "command is  0x%08x\n",
12788c2ecf20Sopenharmony_ci			 readl(&mac->mii_mgmt_cmd));
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci		et131x_mii_read(adapter, reg, &tmp);
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci		status = -EIO;
12838c2ecf20Sopenharmony_ci	}
12848c2ecf20Sopenharmony_ci	/* Stop the write operation */
12858c2ecf20Sopenharmony_ci	writel(0, &mac->mii_mgmt_cmd);
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	/* set the registers we touched back to the state at which we entered
12888c2ecf20Sopenharmony_ci	 * this function
12898c2ecf20Sopenharmony_ci	 */
12908c2ecf20Sopenharmony_ci	writel(mii_addr, &mac->mii_mgmt_addr);
12918c2ecf20Sopenharmony_ci	writel(mii_cmd, &mac->mii_mgmt_cmd);
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci	return status;
12948c2ecf20Sopenharmony_ci}
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_cistatic void et1310_phy_read_mii_bit(struct et131x_adapter *adapter,
12978c2ecf20Sopenharmony_ci				    u16 regnum,
12988c2ecf20Sopenharmony_ci				    u16 bitnum,
12998c2ecf20Sopenharmony_ci				    u8 *value)
13008c2ecf20Sopenharmony_ci{
13018c2ecf20Sopenharmony_ci	u16 reg;
13028c2ecf20Sopenharmony_ci	u16 mask = 1 << bitnum;
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, regnum, &reg);
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci	*value = (reg & mask) >> bitnum;
13078c2ecf20Sopenharmony_ci}
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_cistatic void et1310_config_flow_control(struct et131x_adapter *adapter)
13108c2ecf20Sopenharmony_ci{
13118c2ecf20Sopenharmony_ci	struct phy_device *phydev = adapter->netdev->phydev;
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	if (phydev->duplex == DUPLEX_HALF) {
13148c2ecf20Sopenharmony_ci		adapter->flow = FLOW_NONE;
13158c2ecf20Sopenharmony_ci	} else {
13168c2ecf20Sopenharmony_ci		char remote_pause, remote_async_pause;
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci		et1310_phy_read_mii_bit(adapter, 5, 10, &remote_pause);
13198c2ecf20Sopenharmony_ci		et1310_phy_read_mii_bit(adapter, 5, 11, &remote_async_pause);
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci		if (remote_pause && remote_async_pause) {
13228c2ecf20Sopenharmony_ci			adapter->flow = adapter->wanted_flow;
13238c2ecf20Sopenharmony_ci		} else if (remote_pause && !remote_async_pause) {
13248c2ecf20Sopenharmony_ci			if (adapter->wanted_flow == FLOW_BOTH)
13258c2ecf20Sopenharmony_ci				adapter->flow = FLOW_BOTH;
13268c2ecf20Sopenharmony_ci			else
13278c2ecf20Sopenharmony_ci				adapter->flow = FLOW_NONE;
13288c2ecf20Sopenharmony_ci		} else if (!remote_pause && !remote_async_pause) {
13298c2ecf20Sopenharmony_ci			adapter->flow = FLOW_NONE;
13308c2ecf20Sopenharmony_ci		} else {
13318c2ecf20Sopenharmony_ci			if (adapter->wanted_flow == FLOW_BOTH)
13328c2ecf20Sopenharmony_ci				adapter->flow = FLOW_RXONLY;
13338c2ecf20Sopenharmony_ci			else
13348c2ecf20Sopenharmony_ci				adapter->flow = FLOW_NONE;
13358c2ecf20Sopenharmony_ci		}
13368c2ecf20Sopenharmony_ci	}
13378c2ecf20Sopenharmony_ci}
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci/* et1310_update_macstat_host_counters - Update local copy of the statistics */
13408c2ecf20Sopenharmony_cistatic void et1310_update_macstat_host_counters(struct et131x_adapter *adapter)
13418c2ecf20Sopenharmony_ci{
13428c2ecf20Sopenharmony_ci	struct ce_stats *stats = &adapter->stats;
13438c2ecf20Sopenharmony_ci	struct macstat_regs __iomem *macstat =
13448c2ecf20Sopenharmony_ci		&adapter->regs->macstat;
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci	stats->tx_collisions	       += readl(&macstat->tx_total_collisions);
13478c2ecf20Sopenharmony_ci	stats->tx_first_collisions     += readl(&macstat->tx_single_collisions);
13488c2ecf20Sopenharmony_ci	stats->tx_deferred	       += readl(&macstat->tx_deferred);
13498c2ecf20Sopenharmony_ci	stats->tx_excessive_collisions +=
13508c2ecf20Sopenharmony_ci				readl(&macstat->tx_multiple_collisions);
13518c2ecf20Sopenharmony_ci	stats->tx_late_collisions      += readl(&macstat->tx_late_collisions);
13528c2ecf20Sopenharmony_ci	stats->tx_underflows	       += readl(&macstat->tx_undersize_frames);
13538c2ecf20Sopenharmony_ci	stats->tx_max_pkt_errs	       += readl(&macstat->tx_oversize_frames);
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	stats->rx_align_errs        += readl(&macstat->rx_align_errs);
13568c2ecf20Sopenharmony_ci	stats->rx_crc_errs          += readl(&macstat->rx_code_errs);
13578c2ecf20Sopenharmony_ci	stats->rcvd_pkts_dropped    += readl(&macstat->rx_drops);
13588c2ecf20Sopenharmony_ci	stats->rx_overflows         += readl(&macstat->rx_oversize_packets);
13598c2ecf20Sopenharmony_ci	stats->rx_code_violations   += readl(&macstat->rx_fcs_errs);
13608c2ecf20Sopenharmony_ci	stats->rx_length_errs       += readl(&macstat->rx_frame_len_errs);
13618c2ecf20Sopenharmony_ci	stats->rx_other_errs        += readl(&macstat->rx_fragment_packets);
13628c2ecf20Sopenharmony_ci}
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci/* et1310_handle_macstat_interrupt
13658c2ecf20Sopenharmony_ci *
13668c2ecf20Sopenharmony_ci * One of the MACSTAT counters has wrapped.  Update the local copy of
13678c2ecf20Sopenharmony_ci * the statistics held in the adapter structure, checking the "wrap"
13688c2ecf20Sopenharmony_ci * bit for each counter.
13698c2ecf20Sopenharmony_ci */
13708c2ecf20Sopenharmony_cistatic void et1310_handle_macstat_interrupt(struct et131x_adapter *adapter)
13718c2ecf20Sopenharmony_ci{
13728c2ecf20Sopenharmony_ci	u32 carry_reg1;
13738c2ecf20Sopenharmony_ci	u32 carry_reg2;
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	/* Read the interrupt bits from the register(s).  These are Clear On
13768c2ecf20Sopenharmony_ci	 * Write.
13778c2ecf20Sopenharmony_ci	 */
13788c2ecf20Sopenharmony_ci	carry_reg1 = readl(&adapter->regs->macstat.carry_reg1);
13798c2ecf20Sopenharmony_ci	carry_reg2 = readl(&adapter->regs->macstat.carry_reg2);
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	writel(carry_reg1, &adapter->regs->macstat.carry_reg1);
13828c2ecf20Sopenharmony_ci	writel(carry_reg2, &adapter->regs->macstat.carry_reg2);
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	/* We need to do update the host copy of all the MAC_STAT counters.
13858c2ecf20Sopenharmony_ci	 * For each counter, check it's overflow bit.  If the overflow bit is
13868c2ecf20Sopenharmony_ci	 * set, then increment the host version of the count by one complete
13878c2ecf20Sopenharmony_ci	 * revolution of the counter.  This routine is called when the counter
13888c2ecf20Sopenharmony_ci	 * block indicates that one of the counters has wrapped.
13898c2ecf20Sopenharmony_ci	 */
13908c2ecf20Sopenharmony_ci	if (carry_reg1 & (1 << 14))
13918c2ecf20Sopenharmony_ci		adapter->stats.rx_code_violations	+= COUNTER_WRAP_16_BIT;
13928c2ecf20Sopenharmony_ci	if (carry_reg1 & (1 << 8))
13938c2ecf20Sopenharmony_ci		adapter->stats.rx_align_errs	+= COUNTER_WRAP_12_BIT;
13948c2ecf20Sopenharmony_ci	if (carry_reg1 & (1 << 7))
13958c2ecf20Sopenharmony_ci		adapter->stats.rx_length_errs	+= COUNTER_WRAP_16_BIT;
13968c2ecf20Sopenharmony_ci	if (carry_reg1 & (1 << 2))
13978c2ecf20Sopenharmony_ci		adapter->stats.rx_other_errs	+= COUNTER_WRAP_16_BIT;
13988c2ecf20Sopenharmony_ci	if (carry_reg1 & (1 << 6))
13998c2ecf20Sopenharmony_ci		adapter->stats.rx_crc_errs	+= COUNTER_WRAP_16_BIT;
14008c2ecf20Sopenharmony_ci	if (carry_reg1 & (1 << 3))
14018c2ecf20Sopenharmony_ci		adapter->stats.rx_overflows	+= COUNTER_WRAP_16_BIT;
14028c2ecf20Sopenharmony_ci	if (carry_reg1 & (1 << 0))
14038c2ecf20Sopenharmony_ci		adapter->stats.rcvd_pkts_dropped	+= COUNTER_WRAP_16_BIT;
14048c2ecf20Sopenharmony_ci	if (carry_reg2 & (1 << 16))
14058c2ecf20Sopenharmony_ci		adapter->stats.tx_max_pkt_errs	+= COUNTER_WRAP_12_BIT;
14068c2ecf20Sopenharmony_ci	if (carry_reg2 & (1 << 15))
14078c2ecf20Sopenharmony_ci		adapter->stats.tx_underflows	+= COUNTER_WRAP_12_BIT;
14088c2ecf20Sopenharmony_ci	if (carry_reg2 & (1 << 6))
14098c2ecf20Sopenharmony_ci		adapter->stats.tx_first_collisions += COUNTER_WRAP_12_BIT;
14108c2ecf20Sopenharmony_ci	if (carry_reg2 & (1 << 8))
14118c2ecf20Sopenharmony_ci		adapter->stats.tx_deferred	+= COUNTER_WRAP_12_BIT;
14128c2ecf20Sopenharmony_ci	if (carry_reg2 & (1 << 5))
14138c2ecf20Sopenharmony_ci		adapter->stats.tx_excessive_collisions += COUNTER_WRAP_12_BIT;
14148c2ecf20Sopenharmony_ci	if (carry_reg2 & (1 << 4))
14158c2ecf20Sopenharmony_ci		adapter->stats.tx_late_collisions	+= COUNTER_WRAP_12_BIT;
14168c2ecf20Sopenharmony_ci	if (carry_reg2 & (1 << 2))
14178c2ecf20Sopenharmony_ci		adapter->stats.tx_collisions	+= COUNTER_WRAP_12_BIT;
14188c2ecf20Sopenharmony_ci}
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_cistatic int et131x_mdio_read(struct mii_bus *bus, int phy_addr, int reg)
14218c2ecf20Sopenharmony_ci{
14228c2ecf20Sopenharmony_ci	struct net_device *netdev = bus->priv;
14238c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
14248c2ecf20Sopenharmony_ci	u16 value;
14258c2ecf20Sopenharmony_ci	int ret;
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	ret = et131x_phy_mii_read(adapter, phy_addr, reg, &value);
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci	if (ret < 0)
14308c2ecf20Sopenharmony_ci		return ret;
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	return value;
14338c2ecf20Sopenharmony_ci}
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_cistatic int et131x_mdio_write(struct mii_bus *bus, int phy_addr,
14368c2ecf20Sopenharmony_ci			     int reg, u16 value)
14378c2ecf20Sopenharmony_ci{
14388c2ecf20Sopenharmony_ci	struct net_device *netdev = bus->priv;
14398c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	return et131x_mii_write(adapter, phy_addr, reg, value);
14428c2ecf20Sopenharmony_ci}
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci/*	et1310_phy_power_switch	-	PHY power control
14458c2ecf20Sopenharmony_ci *	@adapter: device to control
14468c2ecf20Sopenharmony_ci *	@down: true for off/false for back on
14478c2ecf20Sopenharmony_ci *
14488c2ecf20Sopenharmony_ci *	one hundred, ten, one thousand megs
14498c2ecf20Sopenharmony_ci *	How would you like to have your LAN accessed
14508c2ecf20Sopenharmony_ci *	Can't you see that this code processed
14518c2ecf20Sopenharmony_ci *	Phy power, phy power..
14528c2ecf20Sopenharmony_ci */
14538c2ecf20Sopenharmony_cistatic void et1310_phy_power_switch(struct et131x_adapter *adapter, bool down)
14548c2ecf20Sopenharmony_ci{
14558c2ecf20Sopenharmony_ci	u16 data;
14568c2ecf20Sopenharmony_ci	struct  phy_device *phydev = adapter->netdev->phydev;
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_BMCR, &data);
14598c2ecf20Sopenharmony_ci	data &= ~BMCR_PDOWN;
14608c2ecf20Sopenharmony_ci	if (down)
14618c2ecf20Sopenharmony_ci		data |= BMCR_PDOWN;
14628c2ecf20Sopenharmony_ci	et131x_mii_write(adapter, phydev->mdio.addr, MII_BMCR, data);
14638c2ecf20Sopenharmony_ci}
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci/* et131x_xcvr_init - Init the phy if we are setting it into force mode */
14668c2ecf20Sopenharmony_cistatic void et131x_xcvr_init(struct et131x_adapter *adapter)
14678c2ecf20Sopenharmony_ci{
14688c2ecf20Sopenharmony_ci	u16 lcr2;
14698c2ecf20Sopenharmony_ci	struct  phy_device *phydev = adapter->netdev->phydev;
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci	/* Set the LED behavior such that LED 1 indicates speed (off =
14728c2ecf20Sopenharmony_ci	 * 10Mbits, blink = 100Mbits, on = 1000Mbits) and LED 2 indicates
14738c2ecf20Sopenharmony_ci	 * link and activity (on for link, blink off for activity).
14748c2ecf20Sopenharmony_ci	 *
14758c2ecf20Sopenharmony_ci	 * NOTE: Some customizations have been added here for specific
14768c2ecf20Sopenharmony_ci	 * vendors; The LED behavior is now determined by vendor data in the
14778c2ecf20Sopenharmony_ci	 * EEPROM. However, the above description is the default.
14788c2ecf20Sopenharmony_ci	 */
14798c2ecf20Sopenharmony_ci	if ((adapter->eeprom_data[1] & 0x4) == 0) {
14808c2ecf20Sopenharmony_ci		et131x_mii_read(adapter, PHY_LED_2, &lcr2);
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci		lcr2 &= (ET_LED2_LED_100TX | ET_LED2_LED_1000T);
14838c2ecf20Sopenharmony_ci		lcr2 |= (LED_VAL_LINKON_ACTIVE << LED_LINK_SHIFT);
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci		if ((adapter->eeprom_data[1] & 0x8) == 0)
14868c2ecf20Sopenharmony_ci			lcr2 |= (LED_VAL_1000BT_100BTX << LED_TXRX_SHIFT);
14878c2ecf20Sopenharmony_ci		else
14888c2ecf20Sopenharmony_ci			lcr2 |= (LED_VAL_LINKON << LED_TXRX_SHIFT);
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_ci		et131x_mii_write(adapter, phydev->mdio.addr, PHY_LED_2, lcr2);
14918c2ecf20Sopenharmony_ci	}
14928c2ecf20Sopenharmony_ci}
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci/* et131x_configure_global_regs	- configure JAGCore global regs */
14958c2ecf20Sopenharmony_cistatic void et131x_configure_global_regs(struct et131x_adapter *adapter)
14968c2ecf20Sopenharmony_ci{
14978c2ecf20Sopenharmony_ci	struct global_regs __iomem *regs = &adapter->regs->global;
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_ci	writel(0, &regs->rxq_start_addr);
15008c2ecf20Sopenharmony_ci	writel(INTERNAL_MEM_SIZE - 1, &regs->txq_end_addr);
15018c2ecf20Sopenharmony_ci
15028c2ecf20Sopenharmony_ci	if (adapter->registry_jumbo_packet < 2048) {
15038c2ecf20Sopenharmony_ci		/* Tx / RxDMA and Tx/Rx MAC interfaces have a 1k word
15048c2ecf20Sopenharmony_ci		 * block of RAM that the driver can split between Tx
15058c2ecf20Sopenharmony_ci		 * and Rx as it desires.  Our default is to split it
15068c2ecf20Sopenharmony_ci		 * 50/50:
15078c2ecf20Sopenharmony_ci		 */
15088c2ecf20Sopenharmony_ci		writel(PARM_RX_MEM_END_DEF, &regs->rxq_end_addr);
15098c2ecf20Sopenharmony_ci		writel(PARM_RX_MEM_END_DEF + 1, &regs->txq_start_addr);
15108c2ecf20Sopenharmony_ci	} else if (adapter->registry_jumbo_packet < 8192) {
15118c2ecf20Sopenharmony_ci		/* For jumbo packets > 2k but < 8k, split 50-50. */
15128c2ecf20Sopenharmony_ci		writel(INTERNAL_MEM_RX_OFFSET, &regs->rxq_end_addr);
15138c2ecf20Sopenharmony_ci		writel(INTERNAL_MEM_RX_OFFSET + 1, &regs->txq_start_addr);
15148c2ecf20Sopenharmony_ci	} else {
15158c2ecf20Sopenharmony_ci		/* 9216 is the only packet size greater than 8k that
15168c2ecf20Sopenharmony_ci		 * is available. The Tx buffer has to be big enough
15178c2ecf20Sopenharmony_ci		 * for one whole packet on the Tx side. We'll make
15188c2ecf20Sopenharmony_ci		 * the Tx 9408, and give the rest to Rx
15198c2ecf20Sopenharmony_ci		 */
15208c2ecf20Sopenharmony_ci		writel(0x01b3, &regs->rxq_end_addr);
15218c2ecf20Sopenharmony_ci		writel(0x01b4, &regs->txq_start_addr);
15228c2ecf20Sopenharmony_ci	}
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci	/* Initialize the loopback register. Disable all loopbacks. */
15258c2ecf20Sopenharmony_ci	writel(0, &regs->loopback);
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_ci	writel(0, &regs->msi_config);
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_ci	/* By default, disable the watchdog timer.  It will be enabled when
15308c2ecf20Sopenharmony_ci	 * a packet is queued.
15318c2ecf20Sopenharmony_ci	 */
15328c2ecf20Sopenharmony_ci	writel(0, &regs->watchdog_timer);
15338c2ecf20Sopenharmony_ci}
15348c2ecf20Sopenharmony_ci
15358c2ecf20Sopenharmony_ci/* et131x_config_rx_dma_regs - Start of Rx_DMA init sequence */
15368c2ecf20Sopenharmony_cistatic void et131x_config_rx_dma_regs(struct et131x_adapter *adapter)
15378c2ecf20Sopenharmony_ci{
15388c2ecf20Sopenharmony_ci	struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
15398c2ecf20Sopenharmony_ci	struct rx_ring *rx_local = &adapter->rx_ring;
15408c2ecf20Sopenharmony_ci	struct fbr_desc *fbr_entry;
15418c2ecf20Sopenharmony_ci	u32 entry;
15428c2ecf20Sopenharmony_ci	u32 psr_num_des;
15438c2ecf20Sopenharmony_ci	unsigned long flags;
15448c2ecf20Sopenharmony_ci	u8 id;
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	et131x_rx_dma_disable(adapter);
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	/* Load the completion writeback physical address */
15498c2ecf20Sopenharmony_ci	writel(upper_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_hi);
15508c2ecf20Sopenharmony_ci	writel(lower_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_lo);
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	memset(rx_local->rx_status_block, 0, sizeof(struct rx_status_block));
15538c2ecf20Sopenharmony_ci
15548c2ecf20Sopenharmony_ci	/* Set the address and parameters of the packet status ring */
15558c2ecf20Sopenharmony_ci	writel(upper_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_hi);
15568c2ecf20Sopenharmony_ci	writel(lower_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_lo);
15578c2ecf20Sopenharmony_ci	writel(rx_local->psr_entries - 1, &rx_dma->psr_num_des);
15588c2ecf20Sopenharmony_ci	writel(0, &rx_dma->psr_full_offset);
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_ci	psr_num_des = readl(&rx_dma->psr_num_des) & ET_RXDMA_PSR_NUM_DES_MASK;
15618c2ecf20Sopenharmony_ci	writel((psr_num_des * LO_MARK_PERCENT_FOR_PSR) / 100,
15628c2ecf20Sopenharmony_ci	       &rx_dma->psr_min_des);
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_ci	spin_lock_irqsave(&adapter->rcv_lock, flags);
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci	/* These local variables track the PSR in the adapter structure */
15678c2ecf20Sopenharmony_ci	rx_local->local_psr_full = 0;
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci	for (id = 0; id < NUM_FBRS; id++) {
15708c2ecf20Sopenharmony_ci		u32 __iomem *num_des;
15718c2ecf20Sopenharmony_ci		u32 __iomem *full_offset;
15728c2ecf20Sopenharmony_ci		u32 __iomem *min_des;
15738c2ecf20Sopenharmony_ci		u32 __iomem *base_hi;
15748c2ecf20Sopenharmony_ci		u32 __iomem *base_lo;
15758c2ecf20Sopenharmony_ci		struct fbr_lookup *fbr = rx_local->fbr[id];
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci		if (id == 0) {
15788c2ecf20Sopenharmony_ci			num_des = &rx_dma->fbr0_num_des;
15798c2ecf20Sopenharmony_ci			full_offset = &rx_dma->fbr0_full_offset;
15808c2ecf20Sopenharmony_ci			min_des = &rx_dma->fbr0_min_des;
15818c2ecf20Sopenharmony_ci			base_hi = &rx_dma->fbr0_base_hi;
15828c2ecf20Sopenharmony_ci			base_lo = &rx_dma->fbr0_base_lo;
15838c2ecf20Sopenharmony_ci		} else {
15848c2ecf20Sopenharmony_ci			num_des = &rx_dma->fbr1_num_des;
15858c2ecf20Sopenharmony_ci			full_offset = &rx_dma->fbr1_full_offset;
15868c2ecf20Sopenharmony_ci			min_des = &rx_dma->fbr1_min_des;
15878c2ecf20Sopenharmony_ci			base_hi = &rx_dma->fbr1_base_hi;
15888c2ecf20Sopenharmony_ci			base_lo = &rx_dma->fbr1_base_lo;
15898c2ecf20Sopenharmony_ci		}
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci		/* Now's the best time to initialize FBR contents */
15928c2ecf20Sopenharmony_ci		fbr_entry = fbr->ring_virtaddr;
15938c2ecf20Sopenharmony_ci		for (entry = 0; entry < fbr->num_entries; entry++) {
15948c2ecf20Sopenharmony_ci			fbr_entry->addr_hi = fbr->bus_high[entry];
15958c2ecf20Sopenharmony_ci			fbr_entry->addr_lo = fbr->bus_low[entry];
15968c2ecf20Sopenharmony_ci			fbr_entry->word2 = entry;
15978c2ecf20Sopenharmony_ci			fbr_entry++;
15988c2ecf20Sopenharmony_ci		}
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci		/* Set the address and parameters of Free buffer ring 1 and 0 */
16018c2ecf20Sopenharmony_ci		writel(upper_32_bits(fbr->ring_physaddr), base_hi);
16028c2ecf20Sopenharmony_ci		writel(lower_32_bits(fbr->ring_physaddr), base_lo);
16038c2ecf20Sopenharmony_ci		writel(fbr->num_entries - 1, num_des);
16048c2ecf20Sopenharmony_ci		writel(ET_DMA10_WRAP, full_offset);
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci		/* This variable tracks the free buffer ring 1 full position,
16078c2ecf20Sopenharmony_ci		 * so it has to match the above.
16088c2ecf20Sopenharmony_ci		 */
16098c2ecf20Sopenharmony_ci		fbr->local_full = ET_DMA10_WRAP;
16108c2ecf20Sopenharmony_ci		writel(((fbr->num_entries * LO_MARK_PERCENT_FOR_RX) / 100) - 1,
16118c2ecf20Sopenharmony_ci		       min_des);
16128c2ecf20Sopenharmony_ci	}
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_ci	/* Program the number of packets we will receive before generating an
16158c2ecf20Sopenharmony_ci	 * interrupt.
16168c2ecf20Sopenharmony_ci	 * For version B silicon, this value gets updated once autoneg is
16178c2ecf20Sopenharmony_ci	 *complete.
16188c2ecf20Sopenharmony_ci	 */
16198c2ecf20Sopenharmony_ci	writel(PARM_RX_NUM_BUFS_DEF, &rx_dma->num_pkt_done);
16208c2ecf20Sopenharmony_ci
16218c2ecf20Sopenharmony_ci	/* The "time_done" is not working correctly to coalesce interrupts
16228c2ecf20Sopenharmony_ci	 * after a given time period, but rather is giving us an interrupt
16238c2ecf20Sopenharmony_ci	 * regardless of whether we have received packets.
16248c2ecf20Sopenharmony_ci	 * This value gets updated once autoneg is complete.
16258c2ecf20Sopenharmony_ci	 */
16268c2ecf20Sopenharmony_ci	writel(PARM_RX_TIME_INT_DEF, &rx_dma->max_pkt_time);
16278c2ecf20Sopenharmony_ci
16288c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&adapter->rcv_lock, flags);
16298c2ecf20Sopenharmony_ci}
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci/* et131x_config_tx_dma_regs - Set up the tx dma section of the JAGCore.
16328c2ecf20Sopenharmony_ci *
16338c2ecf20Sopenharmony_ci * Configure the transmit engine with the ring buffers we have created
16348c2ecf20Sopenharmony_ci * and prepare it for use.
16358c2ecf20Sopenharmony_ci */
16368c2ecf20Sopenharmony_cistatic void et131x_config_tx_dma_regs(struct et131x_adapter *adapter)
16378c2ecf20Sopenharmony_ci{
16388c2ecf20Sopenharmony_ci	struct txdma_regs __iomem *txdma = &adapter->regs->txdma;
16398c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci	/* Load the hardware with the start of the transmit descriptor ring. */
16428c2ecf20Sopenharmony_ci	writel(upper_32_bits(tx_ring->tx_desc_ring_pa), &txdma->pr_base_hi);
16438c2ecf20Sopenharmony_ci	writel(lower_32_bits(tx_ring->tx_desc_ring_pa), &txdma->pr_base_lo);
16448c2ecf20Sopenharmony_ci
16458c2ecf20Sopenharmony_ci	/* Initialise the transmit DMA engine */
16468c2ecf20Sopenharmony_ci	writel(NUM_DESC_PER_RING_TX - 1, &txdma->pr_num_des);
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_ci	/* Load the completion writeback physical address */
16498c2ecf20Sopenharmony_ci	writel(upper_32_bits(tx_ring->tx_status_pa), &txdma->dma_wb_base_hi);
16508c2ecf20Sopenharmony_ci	writel(lower_32_bits(tx_ring->tx_status_pa), &txdma->dma_wb_base_lo);
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci	*tx_ring->tx_status = 0;
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ci	writel(0, &txdma->service_request);
16558c2ecf20Sopenharmony_ci	tx_ring->send_idx = 0;
16568c2ecf20Sopenharmony_ci}
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_ci/* et131x_adapter_setup - Set the adapter up as per cassini+ documentation */
16598c2ecf20Sopenharmony_cistatic void et131x_adapter_setup(struct et131x_adapter *adapter)
16608c2ecf20Sopenharmony_ci{
16618c2ecf20Sopenharmony_ci	et131x_configure_global_regs(adapter);
16628c2ecf20Sopenharmony_ci	et1310_config_mac_regs1(adapter);
16638c2ecf20Sopenharmony_ci
16648c2ecf20Sopenharmony_ci	/* Configure the MMC registers */
16658c2ecf20Sopenharmony_ci	/* All we need to do is initialize the Memory Control Register */
16668c2ecf20Sopenharmony_ci	writel(ET_MMC_ENABLE, &adapter->regs->mmc.mmc_ctrl);
16678c2ecf20Sopenharmony_ci
16688c2ecf20Sopenharmony_ci	et1310_config_rxmac_regs(adapter);
16698c2ecf20Sopenharmony_ci	et1310_config_txmac_regs(adapter);
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci	et131x_config_rx_dma_regs(adapter);
16728c2ecf20Sopenharmony_ci	et131x_config_tx_dma_regs(adapter);
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	et1310_config_macstat_regs(adapter);
16758c2ecf20Sopenharmony_ci
16768c2ecf20Sopenharmony_ci	et1310_phy_power_switch(adapter, 0);
16778c2ecf20Sopenharmony_ci	et131x_xcvr_init(adapter);
16788c2ecf20Sopenharmony_ci}
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci/* et131x_soft_reset - Issue soft reset to the hardware, complete for ET1310 */
16818c2ecf20Sopenharmony_cistatic void et131x_soft_reset(struct et131x_adapter *adapter)
16828c2ecf20Sopenharmony_ci{
16838c2ecf20Sopenharmony_ci	u32 reg;
16848c2ecf20Sopenharmony_ci
16858c2ecf20Sopenharmony_ci	/* Disable MAC Core */
16868c2ecf20Sopenharmony_ci	reg = ET_MAC_CFG1_SOFT_RESET | ET_MAC_CFG1_SIM_RESET |
16878c2ecf20Sopenharmony_ci	      ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
16888c2ecf20Sopenharmony_ci	      ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC;
16898c2ecf20Sopenharmony_ci	writel(reg, &adapter->regs->mac.cfg1);
16908c2ecf20Sopenharmony_ci
16918c2ecf20Sopenharmony_ci	reg = ET_RESET_ALL;
16928c2ecf20Sopenharmony_ci	writel(reg, &adapter->regs->global.sw_reset);
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	reg = ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
16958c2ecf20Sopenharmony_ci	      ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC;
16968c2ecf20Sopenharmony_ci	writel(reg, &adapter->regs->mac.cfg1);
16978c2ecf20Sopenharmony_ci	writel(0, &adapter->regs->mac.cfg1);
16988c2ecf20Sopenharmony_ci}
16998c2ecf20Sopenharmony_ci
17008c2ecf20Sopenharmony_cistatic void et131x_enable_interrupts(struct et131x_adapter *adapter)
17018c2ecf20Sopenharmony_ci{
17028c2ecf20Sopenharmony_ci	u32 mask;
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_ci	if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
17058c2ecf20Sopenharmony_ci		mask = INT_MASK_ENABLE;
17068c2ecf20Sopenharmony_ci	else
17078c2ecf20Sopenharmony_ci		mask = INT_MASK_ENABLE_NO_FLOW;
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	writel(mask, &adapter->regs->global.int_mask);
17108c2ecf20Sopenharmony_ci}
17118c2ecf20Sopenharmony_ci
17128c2ecf20Sopenharmony_cistatic void et131x_disable_interrupts(struct et131x_adapter *adapter)
17138c2ecf20Sopenharmony_ci{
17148c2ecf20Sopenharmony_ci	writel(INT_MASK_DISABLE, &adapter->regs->global.int_mask);
17158c2ecf20Sopenharmony_ci}
17168c2ecf20Sopenharmony_ci
17178c2ecf20Sopenharmony_cistatic void et131x_tx_dma_disable(struct et131x_adapter *adapter)
17188c2ecf20Sopenharmony_ci{
17198c2ecf20Sopenharmony_ci	/* Setup the transmit dma configuration register */
17208c2ecf20Sopenharmony_ci	writel(ET_TXDMA_CSR_HALT | ET_TXDMA_SNGL_EPKT,
17218c2ecf20Sopenharmony_ci	       &adapter->regs->txdma.csr);
17228c2ecf20Sopenharmony_ci}
17238c2ecf20Sopenharmony_ci
17248c2ecf20Sopenharmony_cistatic void et131x_enable_txrx(struct net_device *netdev)
17258c2ecf20Sopenharmony_ci{
17268c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
17278c2ecf20Sopenharmony_ci
17288c2ecf20Sopenharmony_ci	et131x_rx_dma_enable(adapter);
17298c2ecf20Sopenharmony_ci	et131x_tx_dma_enable(adapter);
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_ci	if (adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE)
17328c2ecf20Sopenharmony_ci		et131x_enable_interrupts(adapter);
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_ci	netif_start_queue(netdev);
17358c2ecf20Sopenharmony_ci}
17368c2ecf20Sopenharmony_ci
17378c2ecf20Sopenharmony_cistatic void et131x_disable_txrx(struct net_device *netdev)
17388c2ecf20Sopenharmony_ci{
17398c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci	netif_stop_queue(netdev);
17428c2ecf20Sopenharmony_ci
17438c2ecf20Sopenharmony_ci	et131x_rx_dma_disable(adapter);
17448c2ecf20Sopenharmony_ci	et131x_tx_dma_disable(adapter);
17458c2ecf20Sopenharmony_ci
17468c2ecf20Sopenharmony_ci	et131x_disable_interrupts(adapter);
17478c2ecf20Sopenharmony_ci}
17488c2ecf20Sopenharmony_ci
17498c2ecf20Sopenharmony_cistatic void et131x_init_send(struct et131x_adapter *adapter)
17508c2ecf20Sopenharmony_ci{
17518c2ecf20Sopenharmony_ci	int i;
17528c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
17538c2ecf20Sopenharmony_ci	struct tcb *tcb = tx_ring->tcb_ring;
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	tx_ring->tcb_qhead = tcb;
17568c2ecf20Sopenharmony_ci
17578c2ecf20Sopenharmony_ci	memset(tcb, 0, sizeof(struct tcb) * NUM_TCB);
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_TCB; i++) {
17608c2ecf20Sopenharmony_ci		tcb->next = tcb + 1;
17618c2ecf20Sopenharmony_ci		tcb++;
17628c2ecf20Sopenharmony_ci	}
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci	tcb--;
17658c2ecf20Sopenharmony_ci	tx_ring->tcb_qtail = tcb;
17668c2ecf20Sopenharmony_ci	tcb->next = NULL;
17678c2ecf20Sopenharmony_ci	/* Curr send queue should now be empty */
17688c2ecf20Sopenharmony_ci	tx_ring->send_head = NULL;
17698c2ecf20Sopenharmony_ci	tx_ring->send_tail = NULL;
17708c2ecf20Sopenharmony_ci}
17718c2ecf20Sopenharmony_ci
17728c2ecf20Sopenharmony_ci/* et1310_enable_phy_coma
17738c2ecf20Sopenharmony_ci *
17748c2ecf20Sopenharmony_ci * driver receive an phy status change interrupt while in D0 and check that
17758c2ecf20Sopenharmony_ci * phy_status is down.
17768c2ecf20Sopenharmony_ci *
17778c2ecf20Sopenharmony_ci *          -- gate off JAGCore;
17788c2ecf20Sopenharmony_ci *          -- set gigE PHY in Coma mode
17798c2ecf20Sopenharmony_ci *          -- wake on phy_interrupt; Perform software reset JAGCore,
17808c2ecf20Sopenharmony_ci *             re-initialize jagcore and gigE PHY
17818c2ecf20Sopenharmony_ci */
17828c2ecf20Sopenharmony_cistatic void et1310_enable_phy_coma(struct et131x_adapter *adapter)
17838c2ecf20Sopenharmony_ci{
17848c2ecf20Sopenharmony_ci	u32 pmcsr = readl(&adapter->regs->global.pm_csr);
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_ci	/* Stop sending packets. */
17878c2ecf20Sopenharmony_ci	adapter->flags |= FMP_ADAPTER_LOWER_POWER;
17888c2ecf20Sopenharmony_ci
17898c2ecf20Sopenharmony_ci	/* Wait for outstanding Receive packets */
17908c2ecf20Sopenharmony_ci	et131x_disable_txrx(adapter->netdev);
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_ci	/* Gate off JAGCore 3 clock domains */
17938c2ecf20Sopenharmony_ci	pmcsr &= ~ET_PMCSR_INIT;
17948c2ecf20Sopenharmony_ci	writel(pmcsr, &adapter->regs->global.pm_csr);
17958c2ecf20Sopenharmony_ci
17968c2ecf20Sopenharmony_ci	/* Program gigE PHY in to Coma mode */
17978c2ecf20Sopenharmony_ci	pmcsr |= ET_PM_PHY_SW_COMA;
17988c2ecf20Sopenharmony_ci	writel(pmcsr, &adapter->regs->global.pm_csr);
17998c2ecf20Sopenharmony_ci}
18008c2ecf20Sopenharmony_ci
18018c2ecf20Sopenharmony_cistatic void et1310_disable_phy_coma(struct et131x_adapter *adapter)
18028c2ecf20Sopenharmony_ci{
18038c2ecf20Sopenharmony_ci	u32 pmcsr;
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_ci	pmcsr = readl(&adapter->regs->global.pm_csr);
18068c2ecf20Sopenharmony_ci
18078c2ecf20Sopenharmony_ci	/* Disable phy_sw_coma register and re-enable JAGCore clocks */
18088c2ecf20Sopenharmony_ci	pmcsr |= ET_PMCSR_INIT;
18098c2ecf20Sopenharmony_ci	pmcsr &= ~ET_PM_PHY_SW_COMA;
18108c2ecf20Sopenharmony_ci	writel(pmcsr, &adapter->regs->global.pm_csr);
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_ci	/* Restore the GbE PHY speed and duplex modes;
18138c2ecf20Sopenharmony_ci	 * Reset JAGCore; re-configure and initialize JAGCore and gigE PHY
18148c2ecf20Sopenharmony_ci	 */
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	/* Re-initialize the send structures */
18178c2ecf20Sopenharmony_ci	et131x_init_send(adapter);
18188c2ecf20Sopenharmony_ci
18198c2ecf20Sopenharmony_ci	/* Bring the device back to the state it was during init prior to
18208c2ecf20Sopenharmony_ci	 * autonegotiation being complete.  This way, when we get the auto-neg
18218c2ecf20Sopenharmony_ci	 * complete interrupt, we can complete init by calling ConfigMacREGS2.
18228c2ecf20Sopenharmony_ci	 */
18238c2ecf20Sopenharmony_ci	et131x_soft_reset(adapter);
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_ci	et131x_adapter_setup(adapter);
18268c2ecf20Sopenharmony_ci
18278c2ecf20Sopenharmony_ci	/* Allow Tx to restart */
18288c2ecf20Sopenharmony_ci	adapter->flags &= ~FMP_ADAPTER_LOWER_POWER;
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	et131x_enable_txrx(adapter->netdev);
18318c2ecf20Sopenharmony_ci}
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_cistatic inline u32 bump_free_buff_ring(u32 *free_buff_ring, u32 limit)
18348c2ecf20Sopenharmony_ci{
18358c2ecf20Sopenharmony_ci	u32 tmp_free_buff_ring = *free_buff_ring;
18368c2ecf20Sopenharmony_ci
18378c2ecf20Sopenharmony_ci	tmp_free_buff_ring++;
18388c2ecf20Sopenharmony_ci	/* This works for all cases where limit < 1024. The 1023 case
18398c2ecf20Sopenharmony_ci	 * works because 1023++ is 1024 which means the if condition is not
18408c2ecf20Sopenharmony_ci	 * taken but the carry of the bit into the wrap bit toggles the wrap
18418c2ecf20Sopenharmony_ci	 * value correctly
18428c2ecf20Sopenharmony_ci	 */
18438c2ecf20Sopenharmony_ci	if ((tmp_free_buff_ring & ET_DMA10_MASK) > limit) {
18448c2ecf20Sopenharmony_ci		tmp_free_buff_ring &= ~ET_DMA10_MASK;
18458c2ecf20Sopenharmony_ci		tmp_free_buff_ring ^= ET_DMA10_WRAP;
18468c2ecf20Sopenharmony_ci	}
18478c2ecf20Sopenharmony_ci	/* For the 1023 case */
18488c2ecf20Sopenharmony_ci	tmp_free_buff_ring &= (ET_DMA10_MASK | ET_DMA10_WRAP);
18498c2ecf20Sopenharmony_ci	*free_buff_ring = tmp_free_buff_ring;
18508c2ecf20Sopenharmony_ci	return tmp_free_buff_ring;
18518c2ecf20Sopenharmony_ci}
18528c2ecf20Sopenharmony_ci
18538c2ecf20Sopenharmony_ci/* et131x_rx_dma_memory_alloc
18548c2ecf20Sopenharmony_ci *
18558c2ecf20Sopenharmony_ci * Allocates Free buffer ring 1 for sure, free buffer ring 0 if required,
18568c2ecf20Sopenharmony_ci * and the Packet Status Ring.
18578c2ecf20Sopenharmony_ci */
18588c2ecf20Sopenharmony_cistatic int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
18598c2ecf20Sopenharmony_ci{
18608c2ecf20Sopenharmony_ci	u8 id;
18618c2ecf20Sopenharmony_ci	u32 i, j;
18628c2ecf20Sopenharmony_ci	u32 bufsize;
18638c2ecf20Sopenharmony_ci	u32 psr_size;
18648c2ecf20Sopenharmony_ci	u32 fbr_chunksize;
18658c2ecf20Sopenharmony_ci	struct rx_ring *rx_ring = &adapter->rx_ring;
18668c2ecf20Sopenharmony_ci	struct fbr_lookup *fbr;
18678c2ecf20Sopenharmony_ci
18688c2ecf20Sopenharmony_ci	/* Alloc memory for the lookup table */
18698c2ecf20Sopenharmony_ci	rx_ring->fbr[0] = kzalloc(sizeof(*fbr), GFP_KERNEL);
18708c2ecf20Sopenharmony_ci	if (rx_ring->fbr[0] == NULL)
18718c2ecf20Sopenharmony_ci		return -ENOMEM;
18728c2ecf20Sopenharmony_ci	rx_ring->fbr[1] = kzalloc(sizeof(*fbr), GFP_KERNEL);
18738c2ecf20Sopenharmony_ci	if (rx_ring->fbr[1] == NULL)
18748c2ecf20Sopenharmony_ci		return -ENOMEM;
18758c2ecf20Sopenharmony_ci
18768c2ecf20Sopenharmony_ci	/* The first thing we will do is configure the sizes of the buffer
18778c2ecf20Sopenharmony_ci	 * rings. These will change based on jumbo packet support.  Larger
18788c2ecf20Sopenharmony_ci	 * jumbo packets increases the size of each entry in FBR0, and the
18798c2ecf20Sopenharmony_ci	 * number of entries in FBR0, while at the same time decreasing the
18808c2ecf20Sopenharmony_ci	 * number of entries in FBR1.
18818c2ecf20Sopenharmony_ci	 *
18828c2ecf20Sopenharmony_ci	 * FBR1 holds "large" frames, FBR0 holds "small" frames.  If FBR1
18838c2ecf20Sopenharmony_ci	 * entries are huge in order to accommodate a "jumbo" frame, then it
18848c2ecf20Sopenharmony_ci	 * will have less entries.  Conversely, FBR1 will now be relied upon
18858c2ecf20Sopenharmony_ci	 * to carry more "normal" frames, thus it's entry size also increases
18868c2ecf20Sopenharmony_ci	 * and the number of entries goes up too (since it now carries
18878c2ecf20Sopenharmony_ci	 * "small" + "regular" packets.
18888c2ecf20Sopenharmony_ci	 *
18898c2ecf20Sopenharmony_ci	 * In this scheme, we try to maintain 512 entries between the two
18908c2ecf20Sopenharmony_ci	 * rings. Also, FBR1 remains a constant size - when it's size doubles
18918c2ecf20Sopenharmony_ci	 * the number of entries halves.  FBR0 increases in size, however.
18928c2ecf20Sopenharmony_ci	 */
18938c2ecf20Sopenharmony_ci	if (adapter->registry_jumbo_packet < 2048) {
18948c2ecf20Sopenharmony_ci		rx_ring->fbr[0]->buffsize = 256;
18958c2ecf20Sopenharmony_ci		rx_ring->fbr[0]->num_entries = 512;
18968c2ecf20Sopenharmony_ci		rx_ring->fbr[1]->buffsize = 2048;
18978c2ecf20Sopenharmony_ci		rx_ring->fbr[1]->num_entries = 512;
18988c2ecf20Sopenharmony_ci	} else if (adapter->registry_jumbo_packet < 4096) {
18998c2ecf20Sopenharmony_ci		rx_ring->fbr[0]->buffsize = 512;
19008c2ecf20Sopenharmony_ci		rx_ring->fbr[0]->num_entries = 1024;
19018c2ecf20Sopenharmony_ci		rx_ring->fbr[1]->buffsize = 4096;
19028c2ecf20Sopenharmony_ci		rx_ring->fbr[1]->num_entries = 512;
19038c2ecf20Sopenharmony_ci	} else {
19048c2ecf20Sopenharmony_ci		rx_ring->fbr[0]->buffsize = 1024;
19058c2ecf20Sopenharmony_ci		rx_ring->fbr[0]->num_entries = 768;
19068c2ecf20Sopenharmony_ci		rx_ring->fbr[1]->buffsize = 16384;
19078c2ecf20Sopenharmony_ci		rx_ring->fbr[1]->num_entries = 128;
19088c2ecf20Sopenharmony_ci	}
19098c2ecf20Sopenharmony_ci
19108c2ecf20Sopenharmony_ci	rx_ring->psr_entries = rx_ring->fbr[0]->num_entries +
19118c2ecf20Sopenharmony_ci			       rx_ring->fbr[1]->num_entries;
19128c2ecf20Sopenharmony_ci
19138c2ecf20Sopenharmony_ci	for (id = 0; id < NUM_FBRS; id++) {
19148c2ecf20Sopenharmony_ci		fbr = rx_ring->fbr[id];
19158c2ecf20Sopenharmony_ci		/* Allocate an area of memory for Free Buffer Ring */
19168c2ecf20Sopenharmony_ci		bufsize = sizeof(struct fbr_desc) * fbr->num_entries;
19178c2ecf20Sopenharmony_ci		fbr->ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
19188c2ecf20Sopenharmony_ci							bufsize,
19198c2ecf20Sopenharmony_ci							&fbr->ring_physaddr,
19208c2ecf20Sopenharmony_ci							GFP_KERNEL);
19218c2ecf20Sopenharmony_ci		if (!fbr->ring_virtaddr) {
19228c2ecf20Sopenharmony_ci			dev_err(&adapter->pdev->dev,
19238c2ecf20Sopenharmony_ci				"Cannot alloc memory for Free Buffer Ring %d\n",
19248c2ecf20Sopenharmony_ci				id);
19258c2ecf20Sopenharmony_ci			return -ENOMEM;
19268c2ecf20Sopenharmony_ci		}
19278c2ecf20Sopenharmony_ci	}
19288c2ecf20Sopenharmony_ci
19298c2ecf20Sopenharmony_ci	for (id = 0; id < NUM_FBRS; id++) {
19308c2ecf20Sopenharmony_ci		fbr = rx_ring->fbr[id];
19318c2ecf20Sopenharmony_ci		fbr_chunksize = (FBR_CHUNKS * fbr->buffsize);
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_ci		for (i = 0; i < fbr->num_entries / FBR_CHUNKS; i++) {
19348c2ecf20Sopenharmony_ci			dma_addr_t fbr_physaddr;
19358c2ecf20Sopenharmony_ci
19368c2ecf20Sopenharmony_ci			fbr->mem_virtaddrs[i] = dma_alloc_coherent(
19378c2ecf20Sopenharmony_ci					&adapter->pdev->dev, fbr_chunksize,
19388c2ecf20Sopenharmony_ci					&fbr->mem_physaddrs[i],
19398c2ecf20Sopenharmony_ci					GFP_KERNEL);
19408c2ecf20Sopenharmony_ci
19418c2ecf20Sopenharmony_ci			if (!fbr->mem_virtaddrs[i]) {
19428c2ecf20Sopenharmony_ci				dev_err(&adapter->pdev->dev,
19438c2ecf20Sopenharmony_ci					"Could not alloc memory\n");
19448c2ecf20Sopenharmony_ci				return -ENOMEM;
19458c2ecf20Sopenharmony_ci			}
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_ci			/* See NOTE in "Save Physical Address" comment above */
19488c2ecf20Sopenharmony_ci			fbr_physaddr = fbr->mem_physaddrs[i];
19498c2ecf20Sopenharmony_ci
19508c2ecf20Sopenharmony_ci			for (j = 0; j < FBR_CHUNKS; j++) {
19518c2ecf20Sopenharmony_ci				u32 k = (i * FBR_CHUNKS) + j;
19528c2ecf20Sopenharmony_ci
19538c2ecf20Sopenharmony_ci				/* Save the Virtual address of this index for
19548c2ecf20Sopenharmony_ci				 * quick access later
19558c2ecf20Sopenharmony_ci				 */
19568c2ecf20Sopenharmony_ci				fbr->virt[k] = (u8 *)fbr->mem_virtaddrs[i] +
19578c2ecf20Sopenharmony_ci						   (j * fbr->buffsize);
19588c2ecf20Sopenharmony_ci
19598c2ecf20Sopenharmony_ci				/* now store the physical address in the
19608c2ecf20Sopenharmony_ci				 * descriptor so the device can access it
19618c2ecf20Sopenharmony_ci				 */
19628c2ecf20Sopenharmony_ci				fbr->bus_high[k] = upper_32_bits(fbr_physaddr);
19638c2ecf20Sopenharmony_ci				fbr->bus_low[k] = lower_32_bits(fbr_physaddr);
19648c2ecf20Sopenharmony_ci				fbr_physaddr += fbr->buffsize;
19658c2ecf20Sopenharmony_ci			}
19668c2ecf20Sopenharmony_ci		}
19678c2ecf20Sopenharmony_ci	}
19688c2ecf20Sopenharmony_ci
19698c2ecf20Sopenharmony_ci	/* Allocate an area of memory for FIFO of Packet Status ring entries */
19708c2ecf20Sopenharmony_ci	psr_size = sizeof(struct pkt_stat_desc) * rx_ring->psr_entries;
19718c2ecf20Sopenharmony_ci
19728c2ecf20Sopenharmony_ci	rx_ring->ps_ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
19738c2ecf20Sopenharmony_ci						  psr_size,
19748c2ecf20Sopenharmony_ci						  &rx_ring->ps_ring_physaddr,
19758c2ecf20Sopenharmony_ci						  GFP_KERNEL);
19768c2ecf20Sopenharmony_ci
19778c2ecf20Sopenharmony_ci	if (!rx_ring->ps_ring_virtaddr) {
19788c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev,
19798c2ecf20Sopenharmony_ci			"Cannot alloc memory for Packet Status Ring\n");
19808c2ecf20Sopenharmony_ci		return -ENOMEM;
19818c2ecf20Sopenharmony_ci	}
19828c2ecf20Sopenharmony_ci
19838c2ecf20Sopenharmony_ci	/* Allocate an area of memory for writeback of status information */
19848c2ecf20Sopenharmony_ci	rx_ring->rx_status_block = dma_alloc_coherent(&adapter->pdev->dev,
19858c2ecf20Sopenharmony_ci					    sizeof(struct rx_status_block),
19868c2ecf20Sopenharmony_ci					    &rx_ring->rx_status_bus,
19878c2ecf20Sopenharmony_ci					    GFP_KERNEL);
19888c2ecf20Sopenharmony_ci	if (!rx_ring->rx_status_block) {
19898c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev,
19908c2ecf20Sopenharmony_ci			"Cannot alloc memory for Status Block\n");
19918c2ecf20Sopenharmony_ci		return -ENOMEM;
19928c2ecf20Sopenharmony_ci	}
19938c2ecf20Sopenharmony_ci	rx_ring->num_rfd = NIC_DEFAULT_NUM_RFD;
19948c2ecf20Sopenharmony_ci
19958c2ecf20Sopenharmony_ci	/* The RFDs are going to be put on lists later on, so initialize the
19968c2ecf20Sopenharmony_ci	 * lists now.
19978c2ecf20Sopenharmony_ci	 */
19988c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&rx_ring->recv_list);
19998c2ecf20Sopenharmony_ci	return 0;
20008c2ecf20Sopenharmony_ci}
20018c2ecf20Sopenharmony_ci
20028c2ecf20Sopenharmony_cistatic void et131x_rx_dma_memory_free(struct et131x_adapter *adapter)
20038c2ecf20Sopenharmony_ci{
20048c2ecf20Sopenharmony_ci	u8 id;
20058c2ecf20Sopenharmony_ci	u32 ii;
20068c2ecf20Sopenharmony_ci	u32 bufsize;
20078c2ecf20Sopenharmony_ci	u32 psr_size;
20088c2ecf20Sopenharmony_ci	struct rfd *rfd;
20098c2ecf20Sopenharmony_ci	struct rx_ring *rx_ring = &adapter->rx_ring;
20108c2ecf20Sopenharmony_ci	struct fbr_lookup *fbr;
20118c2ecf20Sopenharmony_ci
20128c2ecf20Sopenharmony_ci	/* Free RFDs and associated packet descriptors */
20138c2ecf20Sopenharmony_ci	WARN_ON(rx_ring->num_ready_recv != rx_ring->num_rfd);
20148c2ecf20Sopenharmony_ci
20158c2ecf20Sopenharmony_ci	while (!list_empty(&rx_ring->recv_list)) {
20168c2ecf20Sopenharmony_ci		rfd = list_entry(rx_ring->recv_list.next,
20178c2ecf20Sopenharmony_ci				 struct rfd, list_node);
20188c2ecf20Sopenharmony_ci
20198c2ecf20Sopenharmony_ci		list_del(&rfd->list_node);
20208c2ecf20Sopenharmony_ci		rfd->skb = NULL;
20218c2ecf20Sopenharmony_ci		kfree(rfd);
20228c2ecf20Sopenharmony_ci	}
20238c2ecf20Sopenharmony_ci
20248c2ecf20Sopenharmony_ci	/* Free Free Buffer Rings */
20258c2ecf20Sopenharmony_ci	for (id = 0; id < NUM_FBRS; id++) {
20268c2ecf20Sopenharmony_ci		fbr = rx_ring->fbr[id];
20278c2ecf20Sopenharmony_ci
20288c2ecf20Sopenharmony_ci		if (!fbr || !fbr->ring_virtaddr)
20298c2ecf20Sopenharmony_ci			continue;
20308c2ecf20Sopenharmony_ci
20318c2ecf20Sopenharmony_ci		/* First the packet memory */
20328c2ecf20Sopenharmony_ci		for (ii = 0; ii < fbr->num_entries / FBR_CHUNKS; ii++) {
20338c2ecf20Sopenharmony_ci			if (fbr->mem_virtaddrs[ii]) {
20348c2ecf20Sopenharmony_ci				bufsize = fbr->buffsize * FBR_CHUNKS;
20358c2ecf20Sopenharmony_ci
20368c2ecf20Sopenharmony_ci				dma_free_coherent(&adapter->pdev->dev,
20378c2ecf20Sopenharmony_ci						  bufsize,
20388c2ecf20Sopenharmony_ci						  fbr->mem_virtaddrs[ii],
20398c2ecf20Sopenharmony_ci						  fbr->mem_physaddrs[ii]);
20408c2ecf20Sopenharmony_ci
20418c2ecf20Sopenharmony_ci				fbr->mem_virtaddrs[ii] = NULL;
20428c2ecf20Sopenharmony_ci			}
20438c2ecf20Sopenharmony_ci		}
20448c2ecf20Sopenharmony_ci
20458c2ecf20Sopenharmony_ci		bufsize = sizeof(struct fbr_desc) * fbr->num_entries;
20468c2ecf20Sopenharmony_ci
20478c2ecf20Sopenharmony_ci		dma_free_coherent(&adapter->pdev->dev,
20488c2ecf20Sopenharmony_ci				  bufsize,
20498c2ecf20Sopenharmony_ci				  fbr->ring_virtaddr,
20508c2ecf20Sopenharmony_ci				  fbr->ring_physaddr);
20518c2ecf20Sopenharmony_ci
20528c2ecf20Sopenharmony_ci		fbr->ring_virtaddr = NULL;
20538c2ecf20Sopenharmony_ci	}
20548c2ecf20Sopenharmony_ci
20558c2ecf20Sopenharmony_ci	/* Free Packet Status Ring */
20568c2ecf20Sopenharmony_ci	if (rx_ring->ps_ring_virtaddr) {
20578c2ecf20Sopenharmony_ci		psr_size = sizeof(struct pkt_stat_desc) * rx_ring->psr_entries;
20588c2ecf20Sopenharmony_ci
20598c2ecf20Sopenharmony_ci		dma_free_coherent(&adapter->pdev->dev, psr_size,
20608c2ecf20Sopenharmony_ci				  rx_ring->ps_ring_virtaddr,
20618c2ecf20Sopenharmony_ci				  rx_ring->ps_ring_physaddr);
20628c2ecf20Sopenharmony_ci
20638c2ecf20Sopenharmony_ci		rx_ring->ps_ring_virtaddr = NULL;
20648c2ecf20Sopenharmony_ci	}
20658c2ecf20Sopenharmony_ci
20668c2ecf20Sopenharmony_ci	/* Free area of memory for the writeback of status information */
20678c2ecf20Sopenharmony_ci	if (rx_ring->rx_status_block) {
20688c2ecf20Sopenharmony_ci		dma_free_coherent(&adapter->pdev->dev,
20698c2ecf20Sopenharmony_ci				  sizeof(struct rx_status_block),
20708c2ecf20Sopenharmony_ci				  rx_ring->rx_status_block,
20718c2ecf20Sopenharmony_ci				  rx_ring->rx_status_bus);
20728c2ecf20Sopenharmony_ci		rx_ring->rx_status_block = NULL;
20738c2ecf20Sopenharmony_ci	}
20748c2ecf20Sopenharmony_ci
20758c2ecf20Sopenharmony_ci	/* Free the FBR Lookup Table */
20768c2ecf20Sopenharmony_ci	kfree(rx_ring->fbr[0]);
20778c2ecf20Sopenharmony_ci	kfree(rx_ring->fbr[1]);
20788c2ecf20Sopenharmony_ci
20798c2ecf20Sopenharmony_ci	/* Reset Counters */
20808c2ecf20Sopenharmony_ci	rx_ring->num_ready_recv = 0;
20818c2ecf20Sopenharmony_ci}
20828c2ecf20Sopenharmony_ci
20838c2ecf20Sopenharmony_ci/* et131x_init_recv - Initialize receive data structures */
20848c2ecf20Sopenharmony_cistatic int et131x_init_recv(struct et131x_adapter *adapter)
20858c2ecf20Sopenharmony_ci{
20868c2ecf20Sopenharmony_ci	struct rfd *rfd;
20878c2ecf20Sopenharmony_ci	u32 rfdct;
20888c2ecf20Sopenharmony_ci	struct rx_ring *rx_ring = &adapter->rx_ring;
20898c2ecf20Sopenharmony_ci
20908c2ecf20Sopenharmony_ci	/* Setup each RFD */
20918c2ecf20Sopenharmony_ci	for (rfdct = 0; rfdct < rx_ring->num_rfd; rfdct++) {
20928c2ecf20Sopenharmony_ci		rfd = kzalloc(sizeof(*rfd), GFP_ATOMIC | GFP_DMA);
20938c2ecf20Sopenharmony_ci		if (!rfd)
20948c2ecf20Sopenharmony_ci			return -ENOMEM;
20958c2ecf20Sopenharmony_ci
20968c2ecf20Sopenharmony_ci		rfd->skb = NULL;
20978c2ecf20Sopenharmony_ci
20988c2ecf20Sopenharmony_ci		/* Add this RFD to the recv_list */
20998c2ecf20Sopenharmony_ci		list_add_tail(&rfd->list_node, &rx_ring->recv_list);
21008c2ecf20Sopenharmony_ci
21018c2ecf20Sopenharmony_ci		/* Increment the available RFD's */
21028c2ecf20Sopenharmony_ci		rx_ring->num_ready_recv++;
21038c2ecf20Sopenharmony_ci	}
21048c2ecf20Sopenharmony_ci
21058c2ecf20Sopenharmony_ci	return 0;
21068c2ecf20Sopenharmony_ci}
21078c2ecf20Sopenharmony_ci
21088c2ecf20Sopenharmony_ci/* et131x_set_rx_dma_timer - Set the heartbeat timer according to line rate */
21098c2ecf20Sopenharmony_cistatic void et131x_set_rx_dma_timer(struct et131x_adapter *adapter)
21108c2ecf20Sopenharmony_ci{
21118c2ecf20Sopenharmony_ci	struct phy_device *phydev = adapter->netdev->phydev;
21128c2ecf20Sopenharmony_ci
21138c2ecf20Sopenharmony_ci	/* For version B silicon, we do not use the RxDMA timer for 10 and 100
21148c2ecf20Sopenharmony_ci	 * Mbits/s line rates. We do not enable and RxDMA interrupt coalescing.
21158c2ecf20Sopenharmony_ci	 */
21168c2ecf20Sopenharmony_ci	if ((phydev->speed == SPEED_100) || (phydev->speed == SPEED_10)) {
21178c2ecf20Sopenharmony_ci		writel(0, &adapter->regs->rxdma.max_pkt_time);
21188c2ecf20Sopenharmony_ci		writel(1, &adapter->regs->rxdma.num_pkt_done);
21198c2ecf20Sopenharmony_ci	}
21208c2ecf20Sopenharmony_ci}
21218c2ecf20Sopenharmony_ci
21228c2ecf20Sopenharmony_ci/* nic_return_rfd - Recycle a RFD and put it back onto the receive list */
21238c2ecf20Sopenharmony_cistatic void nic_return_rfd(struct et131x_adapter *adapter, struct rfd *rfd)
21248c2ecf20Sopenharmony_ci{
21258c2ecf20Sopenharmony_ci	struct rx_ring *rx_local = &adapter->rx_ring;
21268c2ecf20Sopenharmony_ci	struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
21278c2ecf20Sopenharmony_ci	u16 buff_index = rfd->bufferindex;
21288c2ecf20Sopenharmony_ci	u8 ring_index = rfd->ringindex;
21298c2ecf20Sopenharmony_ci	unsigned long flags;
21308c2ecf20Sopenharmony_ci	struct fbr_lookup *fbr = rx_local->fbr[ring_index];
21318c2ecf20Sopenharmony_ci
21328c2ecf20Sopenharmony_ci	/* We don't use any of the OOB data besides status. Otherwise, we
21338c2ecf20Sopenharmony_ci	 * need to clean up OOB data
21348c2ecf20Sopenharmony_ci	 */
21358c2ecf20Sopenharmony_ci	if (buff_index < fbr->num_entries) {
21368c2ecf20Sopenharmony_ci		u32 free_buff_ring;
21378c2ecf20Sopenharmony_ci		u32 __iomem *offset;
21388c2ecf20Sopenharmony_ci		struct fbr_desc *next;
21398c2ecf20Sopenharmony_ci
21408c2ecf20Sopenharmony_ci		if (ring_index == 0)
21418c2ecf20Sopenharmony_ci			offset = &rx_dma->fbr0_full_offset;
21428c2ecf20Sopenharmony_ci		else
21438c2ecf20Sopenharmony_ci			offset = &rx_dma->fbr1_full_offset;
21448c2ecf20Sopenharmony_ci
21458c2ecf20Sopenharmony_ci		next = (struct fbr_desc *)(fbr->ring_virtaddr) +
21468c2ecf20Sopenharmony_ci		       INDEX10(fbr->local_full);
21478c2ecf20Sopenharmony_ci
21488c2ecf20Sopenharmony_ci		/* Handle the Free Buffer Ring advancement here. Write
21498c2ecf20Sopenharmony_ci		 * the PA / Buffer Index for the returned buffer into
21508c2ecf20Sopenharmony_ci		 * the oldest (next to be freed)FBR entry
21518c2ecf20Sopenharmony_ci		 */
21528c2ecf20Sopenharmony_ci		next->addr_hi = fbr->bus_high[buff_index];
21538c2ecf20Sopenharmony_ci		next->addr_lo = fbr->bus_low[buff_index];
21548c2ecf20Sopenharmony_ci		next->word2 = buff_index;
21558c2ecf20Sopenharmony_ci
21568c2ecf20Sopenharmony_ci		free_buff_ring = bump_free_buff_ring(&fbr->local_full,
21578c2ecf20Sopenharmony_ci						     fbr->num_entries - 1);
21588c2ecf20Sopenharmony_ci		writel(free_buff_ring, offset);
21598c2ecf20Sopenharmony_ci	} else {
21608c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev,
21618c2ecf20Sopenharmony_ci			"%s illegal Buffer Index returned\n", __func__);
21628c2ecf20Sopenharmony_ci	}
21638c2ecf20Sopenharmony_ci
21648c2ecf20Sopenharmony_ci	/* The processing on this RFD is done, so put it back on the tail of
21658c2ecf20Sopenharmony_ci	 * our list
21668c2ecf20Sopenharmony_ci	 */
21678c2ecf20Sopenharmony_ci	spin_lock_irqsave(&adapter->rcv_lock, flags);
21688c2ecf20Sopenharmony_ci	list_add_tail(&rfd->list_node, &rx_local->recv_list);
21698c2ecf20Sopenharmony_ci	rx_local->num_ready_recv++;
21708c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&adapter->rcv_lock, flags);
21718c2ecf20Sopenharmony_ci
21728c2ecf20Sopenharmony_ci	WARN_ON(rx_local->num_ready_recv > rx_local->num_rfd);
21738c2ecf20Sopenharmony_ci}
21748c2ecf20Sopenharmony_ci
21758c2ecf20Sopenharmony_ci/* nic_rx_pkts - Checks the hardware for available packets
21768c2ecf20Sopenharmony_ci *
21778c2ecf20Sopenharmony_ci * Checks the hardware for available packets, using completion ring
21788c2ecf20Sopenharmony_ci * If packets are available, it gets an RFD from the recv_list, attaches
21798c2ecf20Sopenharmony_ci * the packet to it, puts the RFD in the RecvPendList, and also returns
21808c2ecf20Sopenharmony_ci * the pointer to the RFD.
21818c2ecf20Sopenharmony_ci */
21828c2ecf20Sopenharmony_cistatic struct rfd *nic_rx_pkts(struct et131x_adapter *adapter)
21838c2ecf20Sopenharmony_ci{
21848c2ecf20Sopenharmony_ci	struct rx_ring *rx_local = &adapter->rx_ring;
21858c2ecf20Sopenharmony_ci	struct rx_status_block *status;
21868c2ecf20Sopenharmony_ci	struct pkt_stat_desc *psr;
21878c2ecf20Sopenharmony_ci	struct rfd *rfd;
21888c2ecf20Sopenharmony_ci	unsigned long flags;
21898c2ecf20Sopenharmony_ci	struct list_head *element;
21908c2ecf20Sopenharmony_ci	u8 ring_index;
21918c2ecf20Sopenharmony_ci	u16 buff_index;
21928c2ecf20Sopenharmony_ci	u32 len;
21938c2ecf20Sopenharmony_ci	u32 word0;
21948c2ecf20Sopenharmony_ci	u32 word1;
21958c2ecf20Sopenharmony_ci	struct sk_buff *skb;
21968c2ecf20Sopenharmony_ci	struct fbr_lookup *fbr;
21978c2ecf20Sopenharmony_ci
21988c2ecf20Sopenharmony_ci	/* RX Status block is written by the DMA engine prior to every
21998c2ecf20Sopenharmony_ci	 * interrupt. It contains the next to be used entry in the Packet
22008c2ecf20Sopenharmony_ci	 * Status Ring, and also the two Free Buffer rings.
22018c2ecf20Sopenharmony_ci	 */
22028c2ecf20Sopenharmony_ci	status = rx_local->rx_status_block;
22038c2ecf20Sopenharmony_ci	word1 = status->word1 >> 16;
22048c2ecf20Sopenharmony_ci
22058c2ecf20Sopenharmony_ci	/* Check the PSR and wrap bits do not match */
22068c2ecf20Sopenharmony_ci	if ((word1 & 0x1FFF) == (rx_local->local_psr_full & 0x1FFF))
22078c2ecf20Sopenharmony_ci		return NULL; /* Looks like this ring is not updated yet */
22088c2ecf20Sopenharmony_ci
22098c2ecf20Sopenharmony_ci	/* The packet status ring indicates that data is available. */
22108c2ecf20Sopenharmony_ci	psr = (struct pkt_stat_desc *)(rx_local->ps_ring_virtaddr) +
22118c2ecf20Sopenharmony_ci			(rx_local->local_psr_full & 0xFFF);
22128c2ecf20Sopenharmony_ci
22138c2ecf20Sopenharmony_ci	/* Grab any information that is required once the PSR is advanced,
22148c2ecf20Sopenharmony_ci	 * since we can no longer rely on the memory being accurate
22158c2ecf20Sopenharmony_ci	 */
22168c2ecf20Sopenharmony_ci	len = psr->word1 & 0xFFFF;
22178c2ecf20Sopenharmony_ci	ring_index = (psr->word1 >> 26) & 0x03;
22188c2ecf20Sopenharmony_ci	fbr = rx_local->fbr[ring_index];
22198c2ecf20Sopenharmony_ci	buff_index = (psr->word1 >> 16) & 0x3FF;
22208c2ecf20Sopenharmony_ci	word0 = psr->word0;
22218c2ecf20Sopenharmony_ci
22228c2ecf20Sopenharmony_ci	/* Indicate that we have used this PSR entry. */
22238c2ecf20Sopenharmony_ci	/* FIXME wrap 12 */
22248c2ecf20Sopenharmony_ci	add_12bit(&rx_local->local_psr_full, 1);
22258c2ecf20Sopenharmony_ci	if ((rx_local->local_psr_full & 0xFFF) > rx_local->psr_entries - 1) {
22268c2ecf20Sopenharmony_ci		/* Clear psr full and toggle the wrap bit */
22278c2ecf20Sopenharmony_ci		rx_local->local_psr_full &=  ~0xFFF;
22288c2ecf20Sopenharmony_ci		rx_local->local_psr_full ^= 0x1000;
22298c2ecf20Sopenharmony_ci	}
22308c2ecf20Sopenharmony_ci
22318c2ecf20Sopenharmony_ci	writel(rx_local->local_psr_full, &adapter->regs->rxdma.psr_full_offset);
22328c2ecf20Sopenharmony_ci
22338c2ecf20Sopenharmony_ci	if (ring_index > 1 || buff_index > fbr->num_entries - 1) {
22348c2ecf20Sopenharmony_ci		/* Illegal buffer or ring index cannot be used by S/W*/
22358c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev,
22368c2ecf20Sopenharmony_ci			"NICRxPkts PSR Entry %d indicates length of %d and/or bad bi(%d)\n",
22378c2ecf20Sopenharmony_ci			rx_local->local_psr_full & 0xFFF, len, buff_index);
22388c2ecf20Sopenharmony_ci		return NULL;
22398c2ecf20Sopenharmony_ci	}
22408c2ecf20Sopenharmony_ci
22418c2ecf20Sopenharmony_ci	/* Get and fill the RFD. */
22428c2ecf20Sopenharmony_ci	spin_lock_irqsave(&adapter->rcv_lock, flags);
22438c2ecf20Sopenharmony_ci
22448c2ecf20Sopenharmony_ci	element = rx_local->recv_list.next;
22458c2ecf20Sopenharmony_ci	rfd = list_entry(element, struct rfd, list_node);
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_ci	if (!rfd) {
22488c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&adapter->rcv_lock, flags);
22498c2ecf20Sopenharmony_ci		return NULL;
22508c2ecf20Sopenharmony_ci	}
22518c2ecf20Sopenharmony_ci
22528c2ecf20Sopenharmony_ci	list_del(&rfd->list_node);
22538c2ecf20Sopenharmony_ci	rx_local->num_ready_recv--;
22548c2ecf20Sopenharmony_ci
22558c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&adapter->rcv_lock, flags);
22568c2ecf20Sopenharmony_ci
22578c2ecf20Sopenharmony_ci	rfd->bufferindex = buff_index;
22588c2ecf20Sopenharmony_ci	rfd->ringindex = ring_index;
22598c2ecf20Sopenharmony_ci
22608c2ecf20Sopenharmony_ci	/* In V1 silicon, there is a bug which screws up filtering of runt
22618c2ecf20Sopenharmony_ci	 * packets. Therefore runt packet filtering is disabled in the MAC and
22628c2ecf20Sopenharmony_ci	 * the packets are dropped here. They are also counted here.
22638c2ecf20Sopenharmony_ci	 */
22648c2ecf20Sopenharmony_ci	if (len < (NIC_MIN_PACKET_SIZE + 4)) {
22658c2ecf20Sopenharmony_ci		adapter->stats.rx_other_errs++;
22668c2ecf20Sopenharmony_ci		rfd->len = 0;
22678c2ecf20Sopenharmony_ci		goto out;
22688c2ecf20Sopenharmony_ci	}
22698c2ecf20Sopenharmony_ci
22708c2ecf20Sopenharmony_ci	if ((word0 & ALCATEL_MULTICAST_PKT) && !(word0 & ALCATEL_BROADCAST_PKT))
22718c2ecf20Sopenharmony_ci		adapter->stats.multicast_pkts_rcvd++;
22728c2ecf20Sopenharmony_ci
22738c2ecf20Sopenharmony_ci	rfd->len = len;
22748c2ecf20Sopenharmony_ci
22758c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(rfd->len + 2);
22768c2ecf20Sopenharmony_ci	if (!skb)
22778c2ecf20Sopenharmony_ci		return NULL;
22788c2ecf20Sopenharmony_ci
22798c2ecf20Sopenharmony_ci	adapter->netdev->stats.rx_bytes += rfd->len;
22808c2ecf20Sopenharmony_ci
22818c2ecf20Sopenharmony_ci	skb_put_data(skb, fbr->virt[buff_index], rfd->len);
22828c2ecf20Sopenharmony_ci
22838c2ecf20Sopenharmony_ci	skb->protocol = eth_type_trans(skb, adapter->netdev);
22848c2ecf20Sopenharmony_ci	skb->ip_summed = CHECKSUM_NONE;
22858c2ecf20Sopenharmony_ci	netif_receive_skb(skb);
22868c2ecf20Sopenharmony_ci
22878c2ecf20Sopenharmony_ciout:
22888c2ecf20Sopenharmony_ci	nic_return_rfd(adapter, rfd);
22898c2ecf20Sopenharmony_ci	return rfd;
22908c2ecf20Sopenharmony_ci}
22918c2ecf20Sopenharmony_ci
22928c2ecf20Sopenharmony_cistatic int et131x_handle_recv_pkts(struct et131x_adapter *adapter, int budget)
22938c2ecf20Sopenharmony_ci{
22948c2ecf20Sopenharmony_ci	struct rfd *rfd = NULL;
22958c2ecf20Sopenharmony_ci	int count = 0;
22968c2ecf20Sopenharmony_ci	int limit = budget;
22978c2ecf20Sopenharmony_ci	bool done = true;
22988c2ecf20Sopenharmony_ci	struct rx_ring *rx_ring = &adapter->rx_ring;
22998c2ecf20Sopenharmony_ci
23008c2ecf20Sopenharmony_ci	if (budget > MAX_PACKETS_HANDLED)
23018c2ecf20Sopenharmony_ci		limit = MAX_PACKETS_HANDLED;
23028c2ecf20Sopenharmony_ci
23038c2ecf20Sopenharmony_ci	/* Process up to available RFD's */
23048c2ecf20Sopenharmony_ci	while (count < limit) {
23058c2ecf20Sopenharmony_ci		if (list_empty(&rx_ring->recv_list)) {
23068c2ecf20Sopenharmony_ci			WARN_ON(rx_ring->num_ready_recv != 0);
23078c2ecf20Sopenharmony_ci			done = false;
23088c2ecf20Sopenharmony_ci			break;
23098c2ecf20Sopenharmony_ci		}
23108c2ecf20Sopenharmony_ci
23118c2ecf20Sopenharmony_ci		rfd = nic_rx_pkts(adapter);
23128c2ecf20Sopenharmony_ci
23138c2ecf20Sopenharmony_ci		if (rfd == NULL)
23148c2ecf20Sopenharmony_ci			break;
23158c2ecf20Sopenharmony_ci
23168c2ecf20Sopenharmony_ci		/* Do not receive any packets until a filter has been set.
23178c2ecf20Sopenharmony_ci		 * Do not receive any packets until we have link.
23188c2ecf20Sopenharmony_ci		 * If length is zero, return the RFD in order to advance the
23198c2ecf20Sopenharmony_ci		 * Free buffer ring.
23208c2ecf20Sopenharmony_ci		 */
23218c2ecf20Sopenharmony_ci		if (!adapter->packet_filter ||
23228c2ecf20Sopenharmony_ci		    !netif_carrier_ok(adapter->netdev) ||
23238c2ecf20Sopenharmony_ci		    rfd->len == 0)
23248c2ecf20Sopenharmony_ci			continue;
23258c2ecf20Sopenharmony_ci
23268c2ecf20Sopenharmony_ci		adapter->netdev->stats.rx_packets++;
23278c2ecf20Sopenharmony_ci
23288c2ecf20Sopenharmony_ci		if (rx_ring->num_ready_recv < RFD_LOW_WATER_MARK)
23298c2ecf20Sopenharmony_ci			dev_warn(&adapter->pdev->dev, "RFD's are running out\n");
23308c2ecf20Sopenharmony_ci
23318c2ecf20Sopenharmony_ci		count++;
23328c2ecf20Sopenharmony_ci	}
23338c2ecf20Sopenharmony_ci
23348c2ecf20Sopenharmony_ci	if (count == limit || !done) {
23358c2ecf20Sopenharmony_ci		rx_ring->unfinished_receives = true;
23368c2ecf20Sopenharmony_ci		writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
23378c2ecf20Sopenharmony_ci		       &adapter->regs->global.watchdog_timer);
23388c2ecf20Sopenharmony_ci	} else {
23398c2ecf20Sopenharmony_ci		/* Watchdog timer will disable itself if appropriate. */
23408c2ecf20Sopenharmony_ci		rx_ring->unfinished_receives = false;
23418c2ecf20Sopenharmony_ci	}
23428c2ecf20Sopenharmony_ci
23438c2ecf20Sopenharmony_ci	return count;
23448c2ecf20Sopenharmony_ci}
23458c2ecf20Sopenharmony_ci
23468c2ecf20Sopenharmony_ci/* et131x_tx_dma_memory_alloc
23478c2ecf20Sopenharmony_ci *
23488c2ecf20Sopenharmony_ci * Allocates memory that will be visible both to the device and to the CPU.
23498c2ecf20Sopenharmony_ci * The OS will pass us packets, pointers to which we will insert in the Tx
23508c2ecf20Sopenharmony_ci * Descriptor queue. The device will read this queue to find the packets in
23518c2ecf20Sopenharmony_ci * memory. The device will update the "status" in memory each time it xmits a
23528c2ecf20Sopenharmony_ci * packet.
23538c2ecf20Sopenharmony_ci */
23548c2ecf20Sopenharmony_cistatic int et131x_tx_dma_memory_alloc(struct et131x_adapter *adapter)
23558c2ecf20Sopenharmony_ci{
23568c2ecf20Sopenharmony_ci	int desc_size = 0;
23578c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
23588c2ecf20Sopenharmony_ci
23598c2ecf20Sopenharmony_ci	/* Allocate memory for the TCB's (Transmit Control Block) */
23608c2ecf20Sopenharmony_ci	tx_ring->tcb_ring = kcalloc(NUM_TCB, sizeof(struct tcb),
23618c2ecf20Sopenharmony_ci				    GFP_KERNEL | GFP_DMA);
23628c2ecf20Sopenharmony_ci	if (!tx_ring->tcb_ring)
23638c2ecf20Sopenharmony_ci		return -ENOMEM;
23648c2ecf20Sopenharmony_ci
23658c2ecf20Sopenharmony_ci	desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
23668c2ecf20Sopenharmony_ci	tx_ring->tx_desc_ring = dma_alloc_coherent(&adapter->pdev->dev,
23678c2ecf20Sopenharmony_ci						   desc_size,
23688c2ecf20Sopenharmony_ci						   &tx_ring->tx_desc_ring_pa,
23698c2ecf20Sopenharmony_ci						   GFP_KERNEL);
23708c2ecf20Sopenharmony_ci	if (!tx_ring->tx_desc_ring) {
23718c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev,
23728c2ecf20Sopenharmony_ci			"Cannot alloc memory for Tx Ring\n");
23738c2ecf20Sopenharmony_ci		return -ENOMEM;
23748c2ecf20Sopenharmony_ci	}
23758c2ecf20Sopenharmony_ci
23768c2ecf20Sopenharmony_ci	tx_ring->tx_status = dma_alloc_coherent(&adapter->pdev->dev,
23778c2ecf20Sopenharmony_ci						    sizeof(u32),
23788c2ecf20Sopenharmony_ci						    &tx_ring->tx_status_pa,
23798c2ecf20Sopenharmony_ci						    GFP_KERNEL);
23808c2ecf20Sopenharmony_ci	if (!tx_ring->tx_status) {
23818c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev,
23828c2ecf20Sopenharmony_ci			"Cannot alloc memory for Tx status block\n");
23838c2ecf20Sopenharmony_ci		return -ENOMEM;
23848c2ecf20Sopenharmony_ci	}
23858c2ecf20Sopenharmony_ci	return 0;
23868c2ecf20Sopenharmony_ci}
23878c2ecf20Sopenharmony_ci
23888c2ecf20Sopenharmony_cistatic void et131x_tx_dma_memory_free(struct et131x_adapter *adapter)
23898c2ecf20Sopenharmony_ci{
23908c2ecf20Sopenharmony_ci	int desc_size = 0;
23918c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
23928c2ecf20Sopenharmony_ci
23938c2ecf20Sopenharmony_ci	if (tx_ring->tx_desc_ring) {
23948c2ecf20Sopenharmony_ci		/* Free memory relating to Tx rings here */
23958c2ecf20Sopenharmony_ci		desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
23968c2ecf20Sopenharmony_ci		dma_free_coherent(&adapter->pdev->dev,
23978c2ecf20Sopenharmony_ci				  desc_size,
23988c2ecf20Sopenharmony_ci				  tx_ring->tx_desc_ring,
23998c2ecf20Sopenharmony_ci				  tx_ring->tx_desc_ring_pa);
24008c2ecf20Sopenharmony_ci		tx_ring->tx_desc_ring = NULL;
24018c2ecf20Sopenharmony_ci	}
24028c2ecf20Sopenharmony_ci
24038c2ecf20Sopenharmony_ci	/* Free memory for the Tx status block */
24048c2ecf20Sopenharmony_ci	if (tx_ring->tx_status) {
24058c2ecf20Sopenharmony_ci		dma_free_coherent(&adapter->pdev->dev,
24068c2ecf20Sopenharmony_ci				  sizeof(u32),
24078c2ecf20Sopenharmony_ci				  tx_ring->tx_status,
24088c2ecf20Sopenharmony_ci				  tx_ring->tx_status_pa);
24098c2ecf20Sopenharmony_ci
24108c2ecf20Sopenharmony_ci		tx_ring->tx_status = NULL;
24118c2ecf20Sopenharmony_ci	}
24128c2ecf20Sopenharmony_ci	/* Free the memory for the tcb structures */
24138c2ecf20Sopenharmony_ci	kfree(tx_ring->tcb_ring);
24148c2ecf20Sopenharmony_ci}
24158c2ecf20Sopenharmony_ci
24168c2ecf20Sopenharmony_ci/* nic_send_packet - NIC specific send handler for version B silicon. */
24178c2ecf20Sopenharmony_cistatic int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
24188c2ecf20Sopenharmony_ci{
24198c2ecf20Sopenharmony_ci	u32 i;
24208c2ecf20Sopenharmony_ci	struct tx_desc desc[24];
24218c2ecf20Sopenharmony_ci	u32 frag = 0;
24228c2ecf20Sopenharmony_ci	u32 thiscopy, remainder;
24238c2ecf20Sopenharmony_ci	struct sk_buff *skb = tcb->skb;
24248c2ecf20Sopenharmony_ci	u32 nr_frags = skb_shinfo(skb)->nr_frags + 1;
24258c2ecf20Sopenharmony_ci	skb_frag_t *frags = &skb_shinfo(skb)->frags[0];
24268c2ecf20Sopenharmony_ci	struct phy_device *phydev = adapter->netdev->phydev;
24278c2ecf20Sopenharmony_ci	dma_addr_t dma_addr;
24288c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
24298c2ecf20Sopenharmony_ci
24308c2ecf20Sopenharmony_ci	/* Part of the optimizations of this send routine restrict us to
24318c2ecf20Sopenharmony_ci	 * sending 24 fragments at a pass.  In practice we should never see
24328c2ecf20Sopenharmony_ci	 * more than 5 fragments.
24338c2ecf20Sopenharmony_ci	 */
24348c2ecf20Sopenharmony_ci
24358c2ecf20Sopenharmony_ci	/* nr_frags should be no more than 18. */
24368c2ecf20Sopenharmony_ci	BUILD_BUG_ON(MAX_SKB_FRAGS + 1 > 23);
24378c2ecf20Sopenharmony_ci
24388c2ecf20Sopenharmony_ci	memset(desc, 0, sizeof(struct tx_desc) * (nr_frags + 1));
24398c2ecf20Sopenharmony_ci
24408c2ecf20Sopenharmony_ci	for (i = 0; i < nr_frags; i++) {
24418c2ecf20Sopenharmony_ci		/* If there is something in this element, lets get a
24428c2ecf20Sopenharmony_ci		 * descriptor from the ring and get the necessary data
24438c2ecf20Sopenharmony_ci		 */
24448c2ecf20Sopenharmony_ci		if (i == 0) {
24458c2ecf20Sopenharmony_ci			/* If the fragments are smaller than a standard MTU,
24468c2ecf20Sopenharmony_ci			 * then map them to a single descriptor in the Tx
24478c2ecf20Sopenharmony_ci			 * Desc ring. However, if they're larger, as is
24488c2ecf20Sopenharmony_ci			 * possible with support for jumbo packets, then
24498c2ecf20Sopenharmony_ci			 * split them each across 2 descriptors.
24508c2ecf20Sopenharmony_ci			 *
24518c2ecf20Sopenharmony_ci			 * This will work until we determine why the hardware
24528c2ecf20Sopenharmony_ci			 * doesn't seem to like large fragments.
24538c2ecf20Sopenharmony_ci			 */
24548c2ecf20Sopenharmony_ci			if (skb_headlen(skb) <= 1514) {
24558c2ecf20Sopenharmony_ci				/* Low 16bits are length, high is vlan and
24568c2ecf20Sopenharmony_ci				 * unused currently so zero
24578c2ecf20Sopenharmony_ci				 */
24588c2ecf20Sopenharmony_ci				desc[frag].len_vlan = skb_headlen(skb);
24598c2ecf20Sopenharmony_ci				dma_addr = dma_map_single(&adapter->pdev->dev,
24608c2ecf20Sopenharmony_ci							  skb->data,
24618c2ecf20Sopenharmony_ci							  skb_headlen(skb),
24628c2ecf20Sopenharmony_ci							  DMA_TO_DEVICE);
24638c2ecf20Sopenharmony_ci				desc[frag].addr_lo = lower_32_bits(dma_addr);
24648c2ecf20Sopenharmony_ci				desc[frag].addr_hi = upper_32_bits(dma_addr);
24658c2ecf20Sopenharmony_ci				frag++;
24668c2ecf20Sopenharmony_ci			} else {
24678c2ecf20Sopenharmony_ci				desc[frag].len_vlan = skb_headlen(skb) / 2;
24688c2ecf20Sopenharmony_ci				dma_addr = dma_map_single(&adapter->pdev->dev,
24698c2ecf20Sopenharmony_ci							  skb->data,
24708c2ecf20Sopenharmony_ci							  skb_headlen(skb) / 2,
24718c2ecf20Sopenharmony_ci							  DMA_TO_DEVICE);
24728c2ecf20Sopenharmony_ci				desc[frag].addr_lo = lower_32_bits(dma_addr);
24738c2ecf20Sopenharmony_ci				desc[frag].addr_hi = upper_32_bits(dma_addr);
24748c2ecf20Sopenharmony_ci				frag++;
24758c2ecf20Sopenharmony_ci
24768c2ecf20Sopenharmony_ci				desc[frag].len_vlan = skb_headlen(skb) / 2;
24778c2ecf20Sopenharmony_ci				dma_addr = dma_map_single(&adapter->pdev->dev,
24788c2ecf20Sopenharmony_ci							  skb->data +
24798c2ecf20Sopenharmony_ci							  skb_headlen(skb) / 2,
24808c2ecf20Sopenharmony_ci							  skb_headlen(skb) / 2,
24818c2ecf20Sopenharmony_ci							  DMA_TO_DEVICE);
24828c2ecf20Sopenharmony_ci				desc[frag].addr_lo = lower_32_bits(dma_addr);
24838c2ecf20Sopenharmony_ci				desc[frag].addr_hi = upper_32_bits(dma_addr);
24848c2ecf20Sopenharmony_ci				frag++;
24858c2ecf20Sopenharmony_ci			}
24868c2ecf20Sopenharmony_ci		} else {
24878c2ecf20Sopenharmony_ci			desc[frag].len_vlan = skb_frag_size(&frags[i - 1]);
24888c2ecf20Sopenharmony_ci			dma_addr = skb_frag_dma_map(&adapter->pdev->dev,
24898c2ecf20Sopenharmony_ci						    &frags[i - 1],
24908c2ecf20Sopenharmony_ci						    0,
24918c2ecf20Sopenharmony_ci						    desc[frag].len_vlan,
24928c2ecf20Sopenharmony_ci						    DMA_TO_DEVICE);
24938c2ecf20Sopenharmony_ci			desc[frag].addr_lo = lower_32_bits(dma_addr);
24948c2ecf20Sopenharmony_ci			desc[frag].addr_hi = upper_32_bits(dma_addr);
24958c2ecf20Sopenharmony_ci			frag++;
24968c2ecf20Sopenharmony_ci		}
24978c2ecf20Sopenharmony_ci	}
24988c2ecf20Sopenharmony_ci
24998c2ecf20Sopenharmony_ci	if (phydev && phydev->speed == SPEED_1000) {
25008c2ecf20Sopenharmony_ci		if (++tx_ring->since_irq == PARM_TX_NUM_BUFS_DEF) {
25018c2ecf20Sopenharmony_ci			/* Last element & Interrupt flag */
25028c2ecf20Sopenharmony_ci			desc[frag - 1].flags =
25038c2ecf20Sopenharmony_ci				    TXDESC_FLAG_INTPROC | TXDESC_FLAG_LASTPKT;
25048c2ecf20Sopenharmony_ci			tx_ring->since_irq = 0;
25058c2ecf20Sopenharmony_ci		} else { /* Last element */
25068c2ecf20Sopenharmony_ci			desc[frag - 1].flags = TXDESC_FLAG_LASTPKT;
25078c2ecf20Sopenharmony_ci		}
25088c2ecf20Sopenharmony_ci	} else {
25098c2ecf20Sopenharmony_ci		desc[frag - 1].flags =
25108c2ecf20Sopenharmony_ci				    TXDESC_FLAG_INTPROC | TXDESC_FLAG_LASTPKT;
25118c2ecf20Sopenharmony_ci	}
25128c2ecf20Sopenharmony_ci
25138c2ecf20Sopenharmony_ci	desc[0].flags |= TXDESC_FLAG_FIRSTPKT;
25148c2ecf20Sopenharmony_ci
25158c2ecf20Sopenharmony_ci	tcb->index_start = tx_ring->send_idx;
25168c2ecf20Sopenharmony_ci	tcb->stale = 0;
25178c2ecf20Sopenharmony_ci
25188c2ecf20Sopenharmony_ci	thiscopy = NUM_DESC_PER_RING_TX - INDEX10(tx_ring->send_idx);
25198c2ecf20Sopenharmony_ci
25208c2ecf20Sopenharmony_ci	if (thiscopy >= frag) {
25218c2ecf20Sopenharmony_ci		remainder = 0;
25228c2ecf20Sopenharmony_ci		thiscopy = frag;
25238c2ecf20Sopenharmony_ci	} else {
25248c2ecf20Sopenharmony_ci		remainder = frag - thiscopy;
25258c2ecf20Sopenharmony_ci	}
25268c2ecf20Sopenharmony_ci
25278c2ecf20Sopenharmony_ci	memcpy(tx_ring->tx_desc_ring + INDEX10(tx_ring->send_idx),
25288c2ecf20Sopenharmony_ci	       desc,
25298c2ecf20Sopenharmony_ci	       sizeof(struct tx_desc) * thiscopy);
25308c2ecf20Sopenharmony_ci
25318c2ecf20Sopenharmony_ci	add_10bit(&tx_ring->send_idx, thiscopy);
25328c2ecf20Sopenharmony_ci
25338c2ecf20Sopenharmony_ci	if (INDEX10(tx_ring->send_idx) == 0 ||
25348c2ecf20Sopenharmony_ci	    INDEX10(tx_ring->send_idx) == NUM_DESC_PER_RING_TX) {
25358c2ecf20Sopenharmony_ci		tx_ring->send_idx &= ~ET_DMA10_MASK;
25368c2ecf20Sopenharmony_ci		tx_ring->send_idx ^= ET_DMA10_WRAP;
25378c2ecf20Sopenharmony_ci	}
25388c2ecf20Sopenharmony_ci
25398c2ecf20Sopenharmony_ci	if (remainder) {
25408c2ecf20Sopenharmony_ci		memcpy(tx_ring->tx_desc_ring,
25418c2ecf20Sopenharmony_ci		       desc + thiscopy,
25428c2ecf20Sopenharmony_ci		       sizeof(struct tx_desc) * remainder);
25438c2ecf20Sopenharmony_ci
25448c2ecf20Sopenharmony_ci		add_10bit(&tx_ring->send_idx, remainder);
25458c2ecf20Sopenharmony_ci	}
25468c2ecf20Sopenharmony_ci
25478c2ecf20Sopenharmony_ci	if (INDEX10(tx_ring->send_idx) == 0) {
25488c2ecf20Sopenharmony_ci		if (tx_ring->send_idx)
25498c2ecf20Sopenharmony_ci			tcb->index = NUM_DESC_PER_RING_TX - 1;
25508c2ecf20Sopenharmony_ci		else
25518c2ecf20Sopenharmony_ci			tcb->index = ET_DMA10_WRAP|(NUM_DESC_PER_RING_TX - 1);
25528c2ecf20Sopenharmony_ci	} else {
25538c2ecf20Sopenharmony_ci		tcb->index = tx_ring->send_idx - 1;
25548c2ecf20Sopenharmony_ci	}
25558c2ecf20Sopenharmony_ci
25568c2ecf20Sopenharmony_ci	spin_lock(&adapter->tcb_send_qlock);
25578c2ecf20Sopenharmony_ci
25588c2ecf20Sopenharmony_ci	if (tx_ring->send_tail)
25598c2ecf20Sopenharmony_ci		tx_ring->send_tail->next = tcb;
25608c2ecf20Sopenharmony_ci	else
25618c2ecf20Sopenharmony_ci		tx_ring->send_head = tcb;
25628c2ecf20Sopenharmony_ci
25638c2ecf20Sopenharmony_ci	tx_ring->send_tail = tcb;
25648c2ecf20Sopenharmony_ci
25658c2ecf20Sopenharmony_ci	WARN_ON(tcb->next != NULL);
25668c2ecf20Sopenharmony_ci
25678c2ecf20Sopenharmony_ci	tx_ring->used++;
25688c2ecf20Sopenharmony_ci
25698c2ecf20Sopenharmony_ci	spin_unlock(&adapter->tcb_send_qlock);
25708c2ecf20Sopenharmony_ci
25718c2ecf20Sopenharmony_ci	/* Write the new write pointer back to the device. */
25728c2ecf20Sopenharmony_ci	writel(tx_ring->send_idx, &adapter->regs->txdma.service_request);
25738c2ecf20Sopenharmony_ci
25748c2ecf20Sopenharmony_ci	/* For Gig only, we use Tx Interrupt coalescing.  Enable the software
25758c2ecf20Sopenharmony_ci	 * timer to wake us up if this packet isn't followed by N more.
25768c2ecf20Sopenharmony_ci	 */
25778c2ecf20Sopenharmony_ci	if (phydev && phydev->speed == SPEED_1000) {
25788c2ecf20Sopenharmony_ci		writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
25798c2ecf20Sopenharmony_ci		       &adapter->regs->global.watchdog_timer);
25808c2ecf20Sopenharmony_ci	}
25818c2ecf20Sopenharmony_ci	return 0;
25828c2ecf20Sopenharmony_ci}
25838c2ecf20Sopenharmony_ci
25848c2ecf20Sopenharmony_cistatic int send_packet(struct sk_buff *skb, struct et131x_adapter *adapter)
25858c2ecf20Sopenharmony_ci{
25868c2ecf20Sopenharmony_ci	int status;
25878c2ecf20Sopenharmony_ci	struct tcb *tcb;
25888c2ecf20Sopenharmony_ci	unsigned long flags;
25898c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
25908c2ecf20Sopenharmony_ci
25918c2ecf20Sopenharmony_ci	/* All packets must have at least a MAC address and a protocol type */
25928c2ecf20Sopenharmony_ci	if (skb->len < ETH_HLEN)
25938c2ecf20Sopenharmony_ci		return -EIO;
25948c2ecf20Sopenharmony_ci
25958c2ecf20Sopenharmony_ci	spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
25968c2ecf20Sopenharmony_ci
25978c2ecf20Sopenharmony_ci	tcb = tx_ring->tcb_qhead;
25988c2ecf20Sopenharmony_ci
25998c2ecf20Sopenharmony_ci	if (tcb == NULL) {
26008c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
26018c2ecf20Sopenharmony_ci		return -ENOMEM;
26028c2ecf20Sopenharmony_ci	}
26038c2ecf20Sopenharmony_ci
26048c2ecf20Sopenharmony_ci	tx_ring->tcb_qhead = tcb->next;
26058c2ecf20Sopenharmony_ci
26068c2ecf20Sopenharmony_ci	if (tx_ring->tcb_qhead == NULL)
26078c2ecf20Sopenharmony_ci		tx_ring->tcb_qtail = NULL;
26088c2ecf20Sopenharmony_ci
26098c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
26108c2ecf20Sopenharmony_ci
26118c2ecf20Sopenharmony_ci	tcb->skb = skb;
26128c2ecf20Sopenharmony_ci	tcb->next = NULL;
26138c2ecf20Sopenharmony_ci
26148c2ecf20Sopenharmony_ci	status = nic_send_packet(adapter, tcb);
26158c2ecf20Sopenharmony_ci
26168c2ecf20Sopenharmony_ci	if (status != 0) {
26178c2ecf20Sopenharmony_ci		spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
26188c2ecf20Sopenharmony_ci
26198c2ecf20Sopenharmony_ci		if (tx_ring->tcb_qtail)
26208c2ecf20Sopenharmony_ci			tx_ring->tcb_qtail->next = tcb;
26218c2ecf20Sopenharmony_ci		else
26228c2ecf20Sopenharmony_ci			/* Apparently ready Q is empty. */
26238c2ecf20Sopenharmony_ci			tx_ring->tcb_qhead = tcb;
26248c2ecf20Sopenharmony_ci
26258c2ecf20Sopenharmony_ci		tx_ring->tcb_qtail = tcb;
26268c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
26278c2ecf20Sopenharmony_ci		return status;
26288c2ecf20Sopenharmony_ci	}
26298c2ecf20Sopenharmony_ci	WARN_ON(tx_ring->used > NUM_TCB);
26308c2ecf20Sopenharmony_ci	return 0;
26318c2ecf20Sopenharmony_ci}
26328c2ecf20Sopenharmony_ci
26338c2ecf20Sopenharmony_ci/* free_send_packet - Recycle a struct tcb */
26348c2ecf20Sopenharmony_cistatic inline void free_send_packet(struct et131x_adapter *adapter,
26358c2ecf20Sopenharmony_ci				    struct tcb *tcb)
26368c2ecf20Sopenharmony_ci{
26378c2ecf20Sopenharmony_ci	unsigned long flags;
26388c2ecf20Sopenharmony_ci	struct tx_desc *desc = NULL;
26398c2ecf20Sopenharmony_ci	struct net_device_stats *stats = &adapter->netdev->stats;
26408c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
26418c2ecf20Sopenharmony_ci	u64  dma_addr;
26428c2ecf20Sopenharmony_ci
26438c2ecf20Sopenharmony_ci	if (tcb->skb) {
26448c2ecf20Sopenharmony_ci		stats->tx_bytes += tcb->skb->len;
26458c2ecf20Sopenharmony_ci
26468c2ecf20Sopenharmony_ci		/* Iterate through the TX descriptors on the ring
26478c2ecf20Sopenharmony_ci		 * corresponding to this packet and umap the fragments
26488c2ecf20Sopenharmony_ci		 * they point to
26498c2ecf20Sopenharmony_ci		 */
26508c2ecf20Sopenharmony_ci		do {
26518c2ecf20Sopenharmony_ci			desc = tx_ring->tx_desc_ring +
26528c2ecf20Sopenharmony_ci			       INDEX10(tcb->index_start);
26538c2ecf20Sopenharmony_ci
26548c2ecf20Sopenharmony_ci			dma_addr = desc->addr_lo;
26558c2ecf20Sopenharmony_ci			dma_addr |= (u64)desc->addr_hi << 32;
26568c2ecf20Sopenharmony_ci
26578c2ecf20Sopenharmony_ci			dma_unmap_single(&adapter->pdev->dev,
26588c2ecf20Sopenharmony_ci					 dma_addr,
26598c2ecf20Sopenharmony_ci					 desc->len_vlan, DMA_TO_DEVICE);
26608c2ecf20Sopenharmony_ci
26618c2ecf20Sopenharmony_ci			add_10bit(&tcb->index_start, 1);
26628c2ecf20Sopenharmony_ci			if (INDEX10(tcb->index_start) >=
26638c2ecf20Sopenharmony_ci							NUM_DESC_PER_RING_TX) {
26648c2ecf20Sopenharmony_ci				tcb->index_start &= ~ET_DMA10_MASK;
26658c2ecf20Sopenharmony_ci				tcb->index_start ^= ET_DMA10_WRAP;
26668c2ecf20Sopenharmony_ci			}
26678c2ecf20Sopenharmony_ci		} while (desc != tx_ring->tx_desc_ring + INDEX10(tcb->index));
26688c2ecf20Sopenharmony_ci
26698c2ecf20Sopenharmony_ci		dev_kfree_skb_any(tcb->skb);
26708c2ecf20Sopenharmony_ci	}
26718c2ecf20Sopenharmony_ci
26728c2ecf20Sopenharmony_ci	memset(tcb, 0, sizeof(struct tcb));
26738c2ecf20Sopenharmony_ci
26748c2ecf20Sopenharmony_ci	/* Add the TCB to the Ready Q */
26758c2ecf20Sopenharmony_ci	spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
26768c2ecf20Sopenharmony_ci
26778c2ecf20Sopenharmony_ci	stats->tx_packets++;
26788c2ecf20Sopenharmony_ci
26798c2ecf20Sopenharmony_ci	if (tx_ring->tcb_qtail)
26808c2ecf20Sopenharmony_ci		tx_ring->tcb_qtail->next = tcb;
26818c2ecf20Sopenharmony_ci	else /* Apparently ready Q is empty. */
26828c2ecf20Sopenharmony_ci		tx_ring->tcb_qhead = tcb;
26838c2ecf20Sopenharmony_ci
26848c2ecf20Sopenharmony_ci	tx_ring->tcb_qtail = tcb;
26858c2ecf20Sopenharmony_ci
26868c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
26878c2ecf20Sopenharmony_ci	WARN_ON(tx_ring->used < 0);
26888c2ecf20Sopenharmony_ci}
26898c2ecf20Sopenharmony_ci
26908c2ecf20Sopenharmony_ci/* et131x_free_busy_send_packets - Free and complete the stopped active sends */
26918c2ecf20Sopenharmony_cistatic void et131x_free_busy_send_packets(struct et131x_adapter *adapter)
26928c2ecf20Sopenharmony_ci{
26938c2ecf20Sopenharmony_ci	struct tcb *tcb;
26948c2ecf20Sopenharmony_ci	unsigned long flags;
26958c2ecf20Sopenharmony_ci	u32 freed = 0;
26968c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
26978c2ecf20Sopenharmony_ci
26988c2ecf20Sopenharmony_ci	/* Any packets being sent? Check the first TCB on the send list */
26998c2ecf20Sopenharmony_ci	spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
27008c2ecf20Sopenharmony_ci
27018c2ecf20Sopenharmony_ci	tcb = tx_ring->send_head;
27028c2ecf20Sopenharmony_ci
27038c2ecf20Sopenharmony_ci	while (tcb != NULL && freed < NUM_TCB) {
27048c2ecf20Sopenharmony_ci		struct tcb *next = tcb->next;
27058c2ecf20Sopenharmony_ci
27068c2ecf20Sopenharmony_ci		tx_ring->send_head = next;
27078c2ecf20Sopenharmony_ci
27088c2ecf20Sopenharmony_ci		if (next == NULL)
27098c2ecf20Sopenharmony_ci			tx_ring->send_tail = NULL;
27108c2ecf20Sopenharmony_ci
27118c2ecf20Sopenharmony_ci		tx_ring->used--;
27128c2ecf20Sopenharmony_ci
27138c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
27148c2ecf20Sopenharmony_ci
27158c2ecf20Sopenharmony_ci		freed++;
27168c2ecf20Sopenharmony_ci		free_send_packet(adapter, tcb);
27178c2ecf20Sopenharmony_ci
27188c2ecf20Sopenharmony_ci		spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
27198c2ecf20Sopenharmony_ci
27208c2ecf20Sopenharmony_ci		tcb = tx_ring->send_head;
27218c2ecf20Sopenharmony_ci	}
27228c2ecf20Sopenharmony_ci
27238c2ecf20Sopenharmony_ci	WARN_ON(freed == NUM_TCB);
27248c2ecf20Sopenharmony_ci
27258c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
27268c2ecf20Sopenharmony_ci
27278c2ecf20Sopenharmony_ci	tx_ring->used = 0;
27288c2ecf20Sopenharmony_ci}
27298c2ecf20Sopenharmony_ci
27308c2ecf20Sopenharmony_ci/* et131x_handle_send_pkts
27318c2ecf20Sopenharmony_ci *
27328c2ecf20Sopenharmony_ci * Re-claim the send resources, complete sends and get more to send from
27338c2ecf20Sopenharmony_ci * the send wait queue.
27348c2ecf20Sopenharmony_ci */
27358c2ecf20Sopenharmony_cistatic void et131x_handle_send_pkts(struct et131x_adapter *adapter)
27368c2ecf20Sopenharmony_ci{
27378c2ecf20Sopenharmony_ci	unsigned long flags;
27388c2ecf20Sopenharmony_ci	u32 serviced;
27398c2ecf20Sopenharmony_ci	struct tcb *tcb;
27408c2ecf20Sopenharmony_ci	u32 index;
27418c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
27428c2ecf20Sopenharmony_ci
27438c2ecf20Sopenharmony_ci	serviced = readl(&adapter->regs->txdma.new_service_complete);
27448c2ecf20Sopenharmony_ci	index = INDEX10(serviced);
27458c2ecf20Sopenharmony_ci
27468c2ecf20Sopenharmony_ci	/* Has the ring wrapped?  Process any descriptors that do not have
27478c2ecf20Sopenharmony_ci	 * the same "wrap" indicator as the current completion indicator
27488c2ecf20Sopenharmony_ci	 */
27498c2ecf20Sopenharmony_ci	spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
27508c2ecf20Sopenharmony_ci
27518c2ecf20Sopenharmony_ci	tcb = tx_ring->send_head;
27528c2ecf20Sopenharmony_ci
27538c2ecf20Sopenharmony_ci	while (tcb &&
27548c2ecf20Sopenharmony_ci	       ((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
27558c2ecf20Sopenharmony_ci	       index < INDEX10(tcb->index)) {
27568c2ecf20Sopenharmony_ci		tx_ring->used--;
27578c2ecf20Sopenharmony_ci		tx_ring->send_head = tcb->next;
27588c2ecf20Sopenharmony_ci		if (tcb->next == NULL)
27598c2ecf20Sopenharmony_ci			tx_ring->send_tail = NULL;
27608c2ecf20Sopenharmony_ci
27618c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
27628c2ecf20Sopenharmony_ci		free_send_packet(adapter, tcb);
27638c2ecf20Sopenharmony_ci		spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
27648c2ecf20Sopenharmony_ci
27658c2ecf20Sopenharmony_ci		/* Goto the next packet */
27668c2ecf20Sopenharmony_ci		tcb = tx_ring->send_head;
27678c2ecf20Sopenharmony_ci	}
27688c2ecf20Sopenharmony_ci	while (tcb &&
27698c2ecf20Sopenharmony_ci	       !((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
27708c2ecf20Sopenharmony_ci	       index > (tcb->index & ET_DMA10_MASK)) {
27718c2ecf20Sopenharmony_ci		tx_ring->used--;
27728c2ecf20Sopenharmony_ci		tx_ring->send_head = tcb->next;
27738c2ecf20Sopenharmony_ci		if (tcb->next == NULL)
27748c2ecf20Sopenharmony_ci			tx_ring->send_tail = NULL;
27758c2ecf20Sopenharmony_ci
27768c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
27778c2ecf20Sopenharmony_ci		free_send_packet(adapter, tcb);
27788c2ecf20Sopenharmony_ci		spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
27798c2ecf20Sopenharmony_ci
27808c2ecf20Sopenharmony_ci		/* Goto the next packet */
27818c2ecf20Sopenharmony_ci		tcb = tx_ring->send_head;
27828c2ecf20Sopenharmony_ci	}
27838c2ecf20Sopenharmony_ci
27848c2ecf20Sopenharmony_ci	/* Wake up the queue when we hit a low-water mark */
27858c2ecf20Sopenharmony_ci	if (tx_ring->used <= NUM_TCB / 3)
27868c2ecf20Sopenharmony_ci		netif_wake_queue(adapter->netdev);
27878c2ecf20Sopenharmony_ci
27888c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
27898c2ecf20Sopenharmony_ci}
27908c2ecf20Sopenharmony_ci
27918c2ecf20Sopenharmony_cistatic int et131x_get_regs_len(struct net_device *netdev)
27928c2ecf20Sopenharmony_ci{
27938c2ecf20Sopenharmony_ci#define ET131X_REGS_LEN 256
27948c2ecf20Sopenharmony_ci	return ET131X_REGS_LEN * sizeof(u32);
27958c2ecf20Sopenharmony_ci}
27968c2ecf20Sopenharmony_ci
27978c2ecf20Sopenharmony_cistatic void et131x_get_regs(struct net_device *netdev,
27988c2ecf20Sopenharmony_ci			    struct ethtool_regs *regs, void *regs_data)
27998c2ecf20Sopenharmony_ci{
28008c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
28018c2ecf20Sopenharmony_ci	struct address_map __iomem *aregs = adapter->regs;
28028c2ecf20Sopenharmony_ci	u32 *regs_buff = regs_data;
28038c2ecf20Sopenharmony_ci	u32 num = 0;
28048c2ecf20Sopenharmony_ci	u16 tmp;
28058c2ecf20Sopenharmony_ci
28068c2ecf20Sopenharmony_ci	memset(regs_data, 0, et131x_get_regs_len(netdev));
28078c2ecf20Sopenharmony_ci
28088c2ecf20Sopenharmony_ci	regs->version = (1 << 24) | (adapter->pdev->revision << 16) |
28098c2ecf20Sopenharmony_ci			adapter->pdev->device;
28108c2ecf20Sopenharmony_ci
28118c2ecf20Sopenharmony_ci	/* PHY regs */
28128c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_BMCR, &tmp);
28138c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28148c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_BMSR, &tmp);
28158c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28168c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_PHYSID1, &tmp);
28178c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28188c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_PHYSID2, &tmp);
28198c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28208c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_ADVERTISE, &tmp);
28218c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28228c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_LPA, &tmp);
28238c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28248c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_EXPANSION, &tmp);
28258c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28268c2ecf20Sopenharmony_ci	/* Autoneg next page transmit reg */
28278c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, 0x07, &tmp);
28288c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28298c2ecf20Sopenharmony_ci	/* Link partner next page reg */
28308c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, 0x08, &tmp);
28318c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28328c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_CTRL1000, &tmp);
28338c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28348c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_STAT1000, &tmp);
28358c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28368c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, 0x0b, &tmp);
28378c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28388c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, 0x0c, &tmp);
28398c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28408c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_MMD_CTRL, &tmp);
28418c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28428c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_MMD_DATA, &tmp);
28438c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28448c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, MII_ESTATUS, &tmp);
28458c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28468c2ecf20Sopenharmony_ci
28478c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_INDEX_REG, &tmp);
28488c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28498c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_DATA_REG, &tmp);
28508c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28518c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG, &tmp);
28528c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28538c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL, &tmp);
28548c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28558c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL + 1, &tmp);
28568c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28578c2ecf20Sopenharmony_ci
28588c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_REGISTER_MGMT_CONTROL, &tmp);
28598c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28608c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_CONFIG, &tmp);
28618c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28628c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_PHY_CONTROL, &tmp);
28638c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28648c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_INTERRUPT_MASK, &tmp);
28658c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28668c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_INTERRUPT_STATUS, &tmp);
28678c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28688c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_PHY_STATUS, &tmp);
28698c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28708c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_LED_1, &tmp);
28718c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28728c2ecf20Sopenharmony_ci	et131x_mii_read(adapter, PHY_LED_2, &tmp);
28738c2ecf20Sopenharmony_ci	regs_buff[num++] = tmp;
28748c2ecf20Sopenharmony_ci
28758c2ecf20Sopenharmony_ci	/* Global regs */
28768c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.txq_start_addr);
28778c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.txq_end_addr);
28788c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.rxq_start_addr);
28798c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.rxq_end_addr);
28808c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.pm_csr);
28818c2ecf20Sopenharmony_ci	regs_buff[num++] = adapter->stats.interrupt_status;
28828c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.int_mask);
28838c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.int_alias_clr_en);
28848c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.int_status_alias);
28858c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.sw_reset);
28868c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.slv_timer);
28878c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.msi_config);
28888c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.loopback);
28898c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->global.watchdog_timer);
28908c2ecf20Sopenharmony_ci
28918c2ecf20Sopenharmony_ci	/* TXDMA regs */
28928c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.csr);
28938c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.pr_base_hi);
28948c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.pr_base_lo);
28958c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.pr_num_des);
28968c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr);
28978c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr_ext);
28988c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.txq_rd_addr);
28998c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_hi);
29008c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_lo);
29018c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.service_request);
29028c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.service_complete);
29038c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.cache_rd_index);
29048c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.cache_wr_index);
29058c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.tx_dma_error);
29068c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.desc_abort_cnt);
29078c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.payload_abort_cnt);
29088c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.writeback_abort_cnt);
29098c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.desc_timeout_cnt);
29108c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.payload_timeout_cnt);
29118c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.writeback_timeout_cnt);
29128c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.desc_error_cnt);
29138c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.payload_error_cnt);
29148c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.writeback_error_cnt);
29158c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.dropped_tlp_cnt);
29168c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.new_service_complete);
29178c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->txdma.ethernet_packet_cnt);
29188c2ecf20Sopenharmony_ci
29198c2ecf20Sopenharmony_ci	/* RXDMA regs */
29208c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.csr);
29218c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_hi);
29228c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_lo);
29238c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.num_pkt_done);
29248c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.max_pkt_time);
29258c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr);
29268c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr_ext);
29278c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.rxq_wr_addr);
29288c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.psr_base_hi);
29298c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.psr_base_lo);
29308c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.psr_num_des);
29318c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.psr_avail_offset);
29328c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.psr_full_offset);
29338c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.psr_access_index);
29348c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.psr_min_des);
29358c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_lo);
29368c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_hi);
29378c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr0_num_des);
29388c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr0_avail_offset);
29398c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr0_full_offset);
29408c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr0_rd_index);
29418c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr0_min_des);
29428c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_lo);
29438c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_hi);
29448c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr1_num_des);
29458c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr1_avail_offset);
29468c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr1_full_offset);
29478c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr1_rd_index);
29488c2ecf20Sopenharmony_ci	regs_buff[num++] = readl(&aregs->rxdma.fbr1_min_des);
29498c2ecf20Sopenharmony_ci}
29508c2ecf20Sopenharmony_ci
29518c2ecf20Sopenharmony_cistatic void et131x_get_drvinfo(struct net_device *netdev,
29528c2ecf20Sopenharmony_ci			       struct ethtool_drvinfo *info)
29538c2ecf20Sopenharmony_ci{
29548c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
29558c2ecf20Sopenharmony_ci
29568c2ecf20Sopenharmony_ci	strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
29578c2ecf20Sopenharmony_ci	strlcpy(info->bus_info, pci_name(adapter->pdev),
29588c2ecf20Sopenharmony_ci		sizeof(info->bus_info));
29598c2ecf20Sopenharmony_ci}
29608c2ecf20Sopenharmony_ci
29618c2ecf20Sopenharmony_cistatic const struct ethtool_ops et131x_ethtool_ops = {
29628c2ecf20Sopenharmony_ci	.get_drvinfo	= et131x_get_drvinfo,
29638c2ecf20Sopenharmony_ci	.get_regs_len	= et131x_get_regs_len,
29648c2ecf20Sopenharmony_ci	.get_regs	= et131x_get_regs,
29658c2ecf20Sopenharmony_ci	.get_link	= ethtool_op_get_link,
29668c2ecf20Sopenharmony_ci	.get_link_ksettings = phy_ethtool_get_link_ksettings,
29678c2ecf20Sopenharmony_ci	.set_link_ksettings = phy_ethtool_set_link_ksettings,
29688c2ecf20Sopenharmony_ci};
29698c2ecf20Sopenharmony_ci
29708c2ecf20Sopenharmony_ci/* et131x_hwaddr_init - set up the MAC Address */
29718c2ecf20Sopenharmony_cistatic void et131x_hwaddr_init(struct et131x_adapter *adapter)
29728c2ecf20Sopenharmony_ci{
29738c2ecf20Sopenharmony_ci	/* If have our default mac from init and no mac address from
29748c2ecf20Sopenharmony_ci	 * EEPROM then we need to generate the last octet and set it on the
29758c2ecf20Sopenharmony_ci	 * device
29768c2ecf20Sopenharmony_ci	 */
29778c2ecf20Sopenharmony_ci	if (is_zero_ether_addr(adapter->rom_addr)) {
29788c2ecf20Sopenharmony_ci		/* We need to randomly generate the last octet so we
29798c2ecf20Sopenharmony_ci		 * decrease our chances of setting the mac address to
29808c2ecf20Sopenharmony_ci		 * same as another one of our cards in the system
29818c2ecf20Sopenharmony_ci		 */
29828c2ecf20Sopenharmony_ci		get_random_bytes(&adapter->addr[5], 1);
29838c2ecf20Sopenharmony_ci		/* We have the default value in the register we are
29848c2ecf20Sopenharmony_ci		 * working with so we need to copy the current
29858c2ecf20Sopenharmony_ci		 * address into the permanent address
29868c2ecf20Sopenharmony_ci		 */
29878c2ecf20Sopenharmony_ci		ether_addr_copy(adapter->rom_addr, adapter->addr);
29888c2ecf20Sopenharmony_ci	} else {
29898c2ecf20Sopenharmony_ci		/* We do not have an override address, so set the
29908c2ecf20Sopenharmony_ci		 * current address to the permanent address and add
29918c2ecf20Sopenharmony_ci		 * it to the device
29928c2ecf20Sopenharmony_ci		 */
29938c2ecf20Sopenharmony_ci		ether_addr_copy(adapter->addr, adapter->rom_addr);
29948c2ecf20Sopenharmony_ci	}
29958c2ecf20Sopenharmony_ci}
29968c2ecf20Sopenharmony_ci
29978c2ecf20Sopenharmony_cistatic int et131x_pci_init(struct et131x_adapter *adapter,
29988c2ecf20Sopenharmony_ci			   struct pci_dev *pdev)
29998c2ecf20Sopenharmony_ci{
30008c2ecf20Sopenharmony_ci	u16 max_payload;
30018c2ecf20Sopenharmony_ci	int i, rc;
30028c2ecf20Sopenharmony_ci
30038c2ecf20Sopenharmony_ci	rc = et131x_init_eeprom(adapter);
30048c2ecf20Sopenharmony_ci	if (rc < 0)
30058c2ecf20Sopenharmony_ci		goto out;
30068c2ecf20Sopenharmony_ci
30078c2ecf20Sopenharmony_ci	if (!pci_is_pcie(pdev)) {
30088c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Missing PCIe capabilities\n");
30098c2ecf20Sopenharmony_ci		goto err_out;
30108c2ecf20Sopenharmony_ci	}
30118c2ecf20Sopenharmony_ci
30128c2ecf20Sopenharmony_ci	/* Program the Ack/Nak latency and replay timers */
30138c2ecf20Sopenharmony_ci	max_payload = pdev->pcie_mpss;
30148c2ecf20Sopenharmony_ci
30158c2ecf20Sopenharmony_ci	if (max_payload < 2) {
30168c2ecf20Sopenharmony_ci		static const u16 acknak[2] = { 0x76, 0xD0 };
30178c2ecf20Sopenharmony_ci		static const u16 replay[2] = { 0x1E0, 0x2ED };
30188c2ecf20Sopenharmony_ci
30198c2ecf20Sopenharmony_ci		if (pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
30208c2ecf20Sopenharmony_ci					  acknak[max_payload])) {
30218c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
30228c2ecf20Sopenharmony_ci				"Could not write PCI config space for ACK/NAK\n");
30238c2ecf20Sopenharmony_ci			goto err_out;
30248c2ecf20Sopenharmony_ci		}
30258c2ecf20Sopenharmony_ci		if (pci_write_config_word(pdev, ET1310_PCI_REPLAY,
30268c2ecf20Sopenharmony_ci					  replay[max_payload])) {
30278c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
30288c2ecf20Sopenharmony_ci				"Could not write PCI config space for Replay Timer\n");
30298c2ecf20Sopenharmony_ci			goto err_out;
30308c2ecf20Sopenharmony_ci		}
30318c2ecf20Sopenharmony_ci	}
30328c2ecf20Sopenharmony_ci
30338c2ecf20Sopenharmony_ci	/* l0s and l1 latency timers.  We are using default values.
30348c2ecf20Sopenharmony_ci	 * Representing 001 for L0s and 010 for L1
30358c2ecf20Sopenharmony_ci	 */
30368c2ecf20Sopenharmony_ci	if (pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11)) {
30378c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
30388c2ecf20Sopenharmony_ci			"Could not write PCI config space for Latency Timers\n");
30398c2ecf20Sopenharmony_ci		goto err_out;
30408c2ecf20Sopenharmony_ci	}
30418c2ecf20Sopenharmony_ci
30428c2ecf20Sopenharmony_ci	/* Change the max read size to 2k */
30438c2ecf20Sopenharmony_ci	if (pcie_set_readrq(pdev, 2048)) {
30448c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
30458c2ecf20Sopenharmony_ci			"Couldn't change PCI config space for Max read size\n");
30468c2ecf20Sopenharmony_ci		goto err_out;
30478c2ecf20Sopenharmony_ci	}
30488c2ecf20Sopenharmony_ci
30498c2ecf20Sopenharmony_ci	/* Get MAC address from config space if an eeprom exists, otherwise
30508c2ecf20Sopenharmony_ci	 * the MAC address there will not be valid
30518c2ecf20Sopenharmony_ci	 */
30528c2ecf20Sopenharmony_ci	if (!adapter->has_eeprom) {
30538c2ecf20Sopenharmony_ci		et131x_hwaddr_init(adapter);
30548c2ecf20Sopenharmony_ci		return 0;
30558c2ecf20Sopenharmony_ci	}
30568c2ecf20Sopenharmony_ci
30578c2ecf20Sopenharmony_ci	for (i = 0; i < ETH_ALEN; i++) {
30588c2ecf20Sopenharmony_ci		if (pci_read_config_byte(pdev, ET1310_PCI_MAC_ADDRESS + i,
30598c2ecf20Sopenharmony_ci					 adapter->rom_addr + i)) {
30608c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "Could not read PCI config space for MAC address\n");
30618c2ecf20Sopenharmony_ci			goto err_out;
30628c2ecf20Sopenharmony_ci		}
30638c2ecf20Sopenharmony_ci	}
30648c2ecf20Sopenharmony_ci	ether_addr_copy(adapter->addr, adapter->rom_addr);
30658c2ecf20Sopenharmony_ciout:
30668c2ecf20Sopenharmony_ci	return rc;
30678c2ecf20Sopenharmony_cierr_out:
30688c2ecf20Sopenharmony_ci	rc = -EIO;
30698c2ecf20Sopenharmony_ci	goto out;
30708c2ecf20Sopenharmony_ci}
30718c2ecf20Sopenharmony_ci
30728c2ecf20Sopenharmony_ci/* et131x_error_timer_handler
30738c2ecf20Sopenharmony_ci * @data: timer-specific variable; here a pointer to our adapter structure
30748c2ecf20Sopenharmony_ci *
30758c2ecf20Sopenharmony_ci * The routine called when the error timer expires, to track the number of
30768c2ecf20Sopenharmony_ci * recurring errors.
30778c2ecf20Sopenharmony_ci */
30788c2ecf20Sopenharmony_cistatic void et131x_error_timer_handler(struct timer_list *t)
30798c2ecf20Sopenharmony_ci{
30808c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = from_timer(adapter, t, error_timer);
30818c2ecf20Sopenharmony_ci	struct phy_device *phydev = adapter->netdev->phydev;
30828c2ecf20Sopenharmony_ci
30838c2ecf20Sopenharmony_ci	if (et1310_in_phy_coma(adapter)) {
30848c2ecf20Sopenharmony_ci		/* Bring the device immediately out of coma, to
30858c2ecf20Sopenharmony_ci		 * prevent it from sleeping indefinitely, this
30868c2ecf20Sopenharmony_ci		 * mechanism could be improved!
30878c2ecf20Sopenharmony_ci		 */
30888c2ecf20Sopenharmony_ci		et1310_disable_phy_coma(adapter);
30898c2ecf20Sopenharmony_ci		adapter->boot_coma = 20;
30908c2ecf20Sopenharmony_ci	} else {
30918c2ecf20Sopenharmony_ci		et1310_update_macstat_host_counters(adapter);
30928c2ecf20Sopenharmony_ci	}
30938c2ecf20Sopenharmony_ci
30948c2ecf20Sopenharmony_ci	if (!phydev->link && adapter->boot_coma < 11)
30958c2ecf20Sopenharmony_ci		adapter->boot_coma++;
30968c2ecf20Sopenharmony_ci
30978c2ecf20Sopenharmony_ci	if (adapter->boot_coma == 10) {
30988c2ecf20Sopenharmony_ci		if (!phydev->link) {
30998c2ecf20Sopenharmony_ci			if (!et1310_in_phy_coma(adapter)) {
31008c2ecf20Sopenharmony_ci				/* NOTE - This was originally a 'sync with
31018c2ecf20Sopenharmony_ci				 *  interrupt'. How to do that under Linux?
31028c2ecf20Sopenharmony_ci				 */
31038c2ecf20Sopenharmony_ci				et131x_enable_interrupts(adapter);
31048c2ecf20Sopenharmony_ci				et1310_enable_phy_coma(adapter);
31058c2ecf20Sopenharmony_ci			}
31068c2ecf20Sopenharmony_ci		}
31078c2ecf20Sopenharmony_ci	}
31088c2ecf20Sopenharmony_ci
31098c2ecf20Sopenharmony_ci	/* This is a periodic timer, so reschedule */
31108c2ecf20Sopenharmony_ci	mod_timer(&adapter->error_timer, jiffies +
31118c2ecf20Sopenharmony_ci		  msecs_to_jiffies(TX_ERROR_PERIOD));
31128c2ecf20Sopenharmony_ci}
31138c2ecf20Sopenharmony_ci
31148c2ecf20Sopenharmony_cistatic void et131x_adapter_memory_free(struct et131x_adapter *adapter)
31158c2ecf20Sopenharmony_ci{
31168c2ecf20Sopenharmony_ci	et131x_tx_dma_memory_free(adapter);
31178c2ecf20Sopenharmony_ci	et131x_rx_dma_memory_free(adapter);
31188c2ecf20Sopenharmony_ci}
31198c2ecf20Sopenharmony_ci
31208c2ecf20Sopenharmony_cistatic int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
31218c2ecf20Sopenharmony_ci{
31228c2ecf20Sopenharmony_ci	int status;
31238c2ecf20Sopenharmony_ci
31248c2ecf20Sopenharmony_ci	status = et131x_tx_dma_memory_alloc(adapter);
31258c2ecf20Sopenharmony_ci	if (status) {
31268c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev,
31278c2ecf20Sopenharmony_ci			"et131x_tx_dma_memory_alloc FAILED\n");
31288c2ecf20Sopenharmony_ci		et131x_tx_dma_memory_free(adapter);
31298c2ecf20Sopenharmony_ci		return status;
31308c2ecf20Sopenharmony_ci	}
31318c2ecf20Sopenharmony_ci
31328c2ecf20Sopenharmony_ci	status = et131x_rx_dma_memory_alloc(adapter);
31338c2ecf20Sopenharmony_ci	if (status) {
31348c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev,
31358c2ecf20Sopenharmony_ci			"et131x_rx_dma_memory_alloc FAILED\n");
31368c2ecf20Sopenharmony_ci		et131x_adapter_memory_free(adapter);
31378c2ecf20Sopenharmony_ci		return status;
31388c2ecf20Sopenharmony_ci	}
31398c2ecf20Sopenharmony_ci
31408c2ecf20Sopenharmony_ci	status = et131x_init_recv(adapter);
31418c2ecf20Sopenharmony_ci	if (status) {
31428c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev, "et131x_init_recv FAILED\n");
31438c2ecf20Sopenharmony_ci		et131x_adapter_memory_free(adapter);
31448c2ecf20Sopenharmony_ci	}
31458c2ecf20Sopenharmony_ci	return status;
31468c2ecf20Sopenharmony_ci}
31478c2ecf20Sopenharmony_ci
31488c2ecf20Sopenharmony_cistatic void et131x_adjust_link(struct net_device *netdev)
31498c2ecf20Sopenharmony_ci{
31508c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
31518c2ecf20Sopenharmony_ci	struct  phy_device *phydev = netdev->phydev;
31528c2ecf20Sopenharmony_ci
31538c2ecf20Sopenharmony_ci	if (!phydev)
31548c2ecf20Sopenharmony_ci		return;
31558c2ecf20Sopenharmony_ci	if (phydev->link == adapter->link)
31568c2ecf20Sopenharmony_ci		return;
31578c2ecf20Sopenharmony_ci
31588c2ecf20Sopenharmony_ci	/* Check to see if we are in coma mode and if
31598c2ecf20Sopenharmony_ci	 * so, disable it because we will not be able
31608c2ecf20Sopenharmony_ci	 * to read PHY values until we are out.
31618c2ecf20Sopenharmony_ci	 */
31628c2ecf20Sopenharmony_ci	if (et1310_in_phy_coma(adapter))
31638c2ecf20Sopenharmony_ci		et1310_disable_phy_coma(adapter);
31648c2ecf20Sopenharmony_ci
31658c2ecf20Sopenharmony_ci	adapter->link = phydev->link;
31668c2ecf20Sopenharmony_ci	phy_print_status(phydev);
31678c2ecf20Sopenharmony_ci
31688c2ecf20Sopenharmony_ci	if (phydev->link) {
31698c2ecf20Sopenharmony_ci		adapter->boot_coma = 20;
31708c2ecf20Sopenharmony_ci		if (phydev->speed == SPEED_10) {
31718c2ecf20Sopenharmony_ci			u16 register18;
31728c2ecf20Sopenharmony_ci
31738c2ecf20Sopenharmony_ci			et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
31748c2ecf20Sopenharmony_ci					&register18);
31758c2ecf20Sopenharmony_ci			et131x_mii_write(adapter, phydev->mdio.addr,
31768c2ecf20Sopenharmony_ci					 PHY_MPHY_CONTROL_REG,
31778c2ecf20Sopenharmony_ci					 register18 | 0x4);
31788c2ecf20Sopenharmony_ci			et131x_mii_write(adapter, phydev->mdio.addr,
31798c2ecf20Sopenharmony_ci					 PHY_INDEX_REG, register18 | 0x8402);
31808c2ecf20Sopenharmony_ci			et131x_mii_write(adapter, phydev->mdio.addr,
31818c2ecf20Sopenharmony_ci					 PHY_DATA_REG, register18 | 511);
31828c2ecf20Sopenharmony_ci			et131x_mii_write(adapter, phydev->mdio.addr,
31838c2ecf20Sopenharmony_ci					 PHY_MPHY_CONTROL_REG, register18);
31848c2ecf20Sopenharmony_ci		}
31858c2ecf20Sopenharmony_ci
31868c2ecf20Sopenharmony_ci		et1310_config_flow_control(adapter);
31878c2ecf20Sopenharmony_ci
31888c2ecf20Sopenharmony_ci		if (phydev->speed == SPEED_1000 &&
31898c2ecf20Sopenharmony_ci		    adapter->registry_jumbo_packet > 2048) {
31908c2ecf20Sopenharmony_ci			u16 reg;
31918c2ecf20Sopenharmony_ci
31928c2ecf20Sopenharmony_ci			et131x_mii_read(adapter, PHY_CONFIG, &reg);
31938c2ecf20Sopenharmony_ci			reg &= ~ET_PHY_CONFIG_TX_FIFO_DEPTH;
31948c2ecf20Sopenharmony_ci			reg |= ET_PHY_CONFIG_FIFO_DEPTH_32;
31958c2ecf20Sopenharmony_ci			et131x_mii_write(adapter, phydev->mdio.addr,
31968c2ecf20Sopenharmony_ci					 PHY_CONFIG, reg);
31978c2ecf20Sopenharmony_ci		}
31988c2ecf20Sopenharmony_ci
31998c2ecf20Sopenharmony_ci		et131x_set_rx_dma_timer(adapter);
32008c2ecf20Sopenharmony_ci		et1310_config_mac_regs2(adapter);
32018c2ecf20Sopenharmony_ci	} else {
32028c2ecf20Sopenharmony_ci		adapter->boot_coma = 0;
32038c2ecf20Sopenharmony_ci
32048c2ecf20Sopenharmony_ci		if (phydev->speed == SPEED_10) {
32058c2ecf20Sopenharmony_ci			u16 register18;
32068c2ecf20Sopenharmony_ci
32078c2ecf20Sopenharmony_ci			et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
32088c2ecf20Sopenharmony_ci					&register18);
32098c2ecf20Sopenharmony_ci			et131x_mii_write(adapter, phydev->mdio.addr,
32108c2ecf20Sopenharmony_ci					 PHY_MPHY_CONTROL_REG,
32118c2ecf20Sopenharmony_ci					 register18 | 0x4);
32128c2ecf20Sopenharmony_ci			et131x_mii_write(adapter, phydev->mdio.addr,
32138c2ecf20Sopenharmony_ci					 PHY_INDEX_REG, register18 | 0x8402);
32148c2ecf20Sopenharmony_ci			et131x_mii_write(adapter, phydev->mdio.addr,
32158c2ecf20Sopenharmony_ci					 PHY_DATA_REG, register18 | 511);
32168c2ecf20Sopenharmony_ci			et131x_mii_write(adapter, phydev->mdio.addr,
32178c2ecf20Sopenharmony_ci					 PHY_MPHY_CONTROL_REG, register18);
32188c2ecf20Sopenharmony_ci		}
32198c2ecf20Sopenharmony_ci
32208c2ecf20Sopenharmony_ci		et131x_free_busy_send_packets(adapter);
32218c2ecf20Sopenharmony_ci		et131x_init_send(adapter);
32228c2ecf20Sopenharmony_ci
32238c2ecf20Sopenharmony_ci		/* Bring the device back to the state it was during
32248c2ecf20Sopenharmony_ci		 * init prior to autonegotiation being complete. This
32258c2ecf20Sopenharmony_ci		 * way, when we get the auto-neg complete interrupt,
32268c2ecf20Sopenharmony_ci		 * we can complete init by calling config_mac_regs2.
32278c2ecf20Sopenharmony_ci		 */
32288c2ecf20Sopenharmony_ci		et131x_soft_reset(adapter);
32298c2ecf20Sopenharmony_ci
32308c2ecf20Sopenharmony_ci		et131x_adapter_setup(adapter);
32318c2ecf20Sopenharmony_ci
32328c2ecf20Sopenharmony_ci		et131x_disable_txrx(netdev);
32338c2ecf20Sopenharmony_ci		et131x_enable_txrx(netdev);
32348c2ecf20Sopenharmony_ci	}
32358c2ecf20Sopenharmony_ci}
32368c2ecf20Sopenharmony_ci
32378c2ecf20Sopenharmony_cistatic int et131x_mii_probe(struct net_device *netdev)
32388c2ecf20Sopenharmony_ci{
32398c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
32408c2ecf20Sopenharmony_ci	struct  phy_device *phydev = NULL;
32418c2ecf20Sopenharmony_ci
32428c2ecf20Sopenharmony_ci	phydev = phy_find_first(adapter->mii_bus);
32438c2ecf20Sopenharmony_ci	if (!phydev) {
32448c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev, "no PHY found\n");
32458c2ecf20Sopenharmony_ci		return -ENODEV;
32468c2ecf20Sopenharmony_ci	}
32478c2ecf20Sopenharmony_ci
32488c2ecf20Sopenharmony_ci	phydev = phy_connect(netdev, phydev_name(phydev),
32498c2ecf20Sopenharmony_ci			     &et131x_adjust_link, PHY_INTERFACE_MODE_MII);
32508c2ecf20Sopenharmony_ci
32518c2ecf20Sopenharmony_ci	if (IS_ERR(phydev)) {
32528c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev, "Could not attach to PHY\n");
32538c2ecf20Sopenharmony_ci		return PTR_ERR(phydev);
32548c2ecf20Sopenharmony_ci	}
32558c2ecf20Sopenharmony_ci
32568c2ecf20Sopenharmony_ci	phy_set_max_speed(phydev, SPEED_100);
32578c2ecf20Sopenharmony_ci
32588c2ecf20Sopenharmony_ci	if (adapter->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
32598c2ecf20Sopenharmony_ci		phy_set_max_speed(phydev, SPEED_1000);
32608c2ecf20Sopenharmony_ci
32618c2ecf20Sopenharmony_ci	phydev->autoneg = AUTONEG_ENABLE;
32628c2ecf20Sopenharmony_ci
32638c2ecf20Sopenharmony_ci	phy_attached_info(phydev);
32648c2ecf20Sopenharmony_ci
32658c2ecf20Sopenharmony_ci	return 0;
32668c2ecf20Sopenharmony_ci}
32678c2ecf20Sopenharmony_ci
32688c2ecf20Sopenharmony_cistatic struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
32698c2ecf20Sopenharmony_ci						  struct pci_dev *pdev)
32708c2ecf20Sopenharmony_ci{
32718c2ecf20Sopenharmony_ci	static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
32728c2ecf20Sopenharmony_ci
32738c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter;
32748c2ecf20Sopenharmony_ci
32758c2ecf20Sopenharmony_ci	adapter = netdev_priv(netdev);
32768c2ecf20Sopenharmony_ci	adapter->pdev = pci_dev_get(pdev);
32778c2ecf20Sopenharmony_ci	adapter->netdev = netdev;
32788c2ecf20Sopenharmony_ci
32798c2ecf20Sopenharmony_ci	spin_lock_init(&adapter->tcb_send_qlock);
32808c2ecf20Sopenharmony_ci	spin_lock_init(&adapter->tcb_ready_qlock);
32818c2ecf20Sopenharmony_ci	spin_lock_init(&adapter->rcv_lock);
32828c2ecf20Sopenharmony_ci
32838c2ecf20Sopenharmony_ci	adapter->registry_jumbo_packet = 1514;	/* 1514-9216 */
32848c2ecf20Sopenharmony_ci
32858c2ecf20Sopenharmony_ci	ether_addr_copy(adapter->addr, default_mac);
32868c2ecf20Sopenharmony_ci
32878c2ecf20Sopenharmony_ci	return adapter;
32888c2ecf20Sopenharmony_ci}
32898c2ecf20Sopenharmony_ci
32908c2ecf20Sopenharmony_cistatic void et131x_pci_remove(struct pci_dev *pdev)
32918c2ecf20Sopenharmony_ci{
32928c2ecf20Sopenharmony_ci	struct net_device *netdev = pci_get_drvdata(pdev);
32938c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
32948c2ecf20Sopenharmony_ci
32958c2ecf20Sopenharmony_ci	unregister_netdev(netdev);
32968c2ecf20Sopenharmony_ci	netif_napi_del(&adapter->napi);
32978c2ecf20Sopenharmony_ci	phy_disconnect(netdev->phydev);
32988c2ecf20Sopenharmony_ci	mdiobus_unregister(adapter->mii_bus);
32998c2ecf20Sopenharmony_ci	mdiobus_free(adapter->mii_bus);
33008c2ecf20Sopenharmony_ci
33018c2ecf20Sopenharmony_ci	et131x_adapter_memory_free(adapter);
33028c2ecf20Sopenharmony_ci	iounmap(adapter->regs);
33038c2ecf20Sopenharmony_ci	pci_dev_put(pdev);
33048c2ecf20Sopenharmony_ci
33058c2ecf20Sopenharmony_ci	free_netdev(netdev);
33068c2ecf20Sopenharmony_ci	pci_release_regions(pdev);
33078c2ecf20Sopenharmony_ci	pci_disable_device(pdev);
33088c2ecf20Sopenharmony_ci}
33098c2ecf20Sopenharmony_ci
33108c2ecf20Sopenharmony_cistatic void et131x_up(struct net_device *netdev)
33118c2ecf20Sopenharmony_ci{
33128c2ecf20Sopenharmony_ci	et131x_enable_txrx(netdev);
33138c2ecf20Sopenharmony_ci	phy_start(netdev->phydev);
33148c2ecf20Sopenharmony_ci}
33158c2ecf20Sopenharmony_ci
33168c2ecf20Sopenharmony_cistatic void et131x_down(struct net_device *netdev)
33178c2ecf20Sopenharmony_ci{
33188c2ecf20Sopenharmony_ci	/* Save the timestamp for the TX watchdog, prevent a timeout */
33198c2ecf20Sopenharmony_ci	netif_trans_update(netdev);
33208c2ecf20Sopenharmony_ci
33218c2ecf20Sopenharmony_ci	phy_stop(netdev->phydev);
33228c2ecf20Sopenharmony_ci	et131x_disable_txrx(netdev);
33238c2ecf20Sopenharmony_ci}
33248c2ecf20Sopenharmony_ci
33258c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
33268c2ecf20Sopenharmony_cistatic int et131x_suspend(struct device *dev)
33278c2ecf20Sopenharmony_ci{
33288c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev);
33298c2ecf20Sopenharmony_ci	struct net_device *netdev = pci_get_drvdata(pdev);
33308c2ecf20Sopenharmony_ci
33318c2ecf20Sopenharmony_ci	if (netif_running(netdev)) {
33328c2ecf20Sopenharmony_ci		netif_device_detach(netdev);
33338c2ecf20Sopenharmony_ci		et131x_down(netdev);
33348c2ecf20Sopenharmony_ci		pci_save_state(pdev);
33358c2ecf20Sopenharmony_ci	}
33368c2ecf20Sopenharmony_ci
33378c2ecf20Sopenharmony_ci	return 0;
33388c2ecf20Sopenharmony_ci}
33398c2ecf20Sopenharmony_ci
33408c2ecf20Sopenharmony_cistatic int et131x_resume(struct device *dev)
33418c2ecf20Sopenharmony_ci{
33428c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev);
33438c2ecf20Sopenharmony_ci	struct net_device *netdev = pci_get_drvdata(pdev);
33448c2ecf20Sopenharmony_ci
33458c2ecf20Sopenharmony_ci	if (netif_running(netdev)) {
33468c2ecf20Sopenharmony_ci		pci_restore_state(pdev);
33478c2ecf20Sopenharmony_ci		et131x_up(netdev);
33488c2ecf20Sopenharmony_ci		netif_device_attach(netdev);
33498c2ecf20Sopenharmony_ci	}
33508c2ecf20Sopenharmony_ci
33518c2ecf20Sopenharmony_ci	return 0;
33528c2ecf20Sopenharmony_ci}
33538c2ecf20Sopenharmony_ci#endif
33548c2ecf20Sopenharmony_ci
33558c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(et131x_pm_ops, et131x_suspend, et131x_resume);
33568c2ecf20Sopenharmony_ci
33578c2ecf20Sopenharmony_cistatic irqreturn_t et131x_isr(int irq, void *dev_id)
33588c2ecf20Sopenharmony_ci{
33598c2ecf20Sopenharmony_ci	bool handled = true;
33608c2ecf20Sopenharmony_ci	bool enable_interrupts = true;
33618c2ecf20Sopenharmony_ci	struct net_device *netdev = dev_id;
33628c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
33638c2ecf20Sopenharmony_ci	struct address_map __iomem *iomem = adapter->regs;
33648c2ecf20Sopenharmony_ci	struct rx_ring *rx_ring = &adapter->rx_ring;
33658c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
33668c2ecf20Sopenharmony_ci	u32 status;
33678c2ecf20Sopenharmony_ci
33688c2ecf20Sopenharmony_ci	if (!netif_device_present(netdev)) {
33698c2ecf20Sopenharmony_ci		handled = false;
33708c2ecf20Sopenharmony_ci		enable_interrupts = false;
33718c2ecf20Sopenharmony_ci		goto out;
33728c2ecf20Sopenharmony_ci	}
33738c2ecf20Sopenharmony_ci
33748c2ecf20Sopenharmony_ci	et131x_disable_interrupts(adapter);
33758c2ecf20Sopenharmony_ci
33768c2ecf20Sopenharmony_ci	status = readl(&adapter->regs->global.int_status);
33778c2ecf20Sopenharmony_ci
33788c2ecf20Sopenharmony_ci	if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
33798c2ecf20Sopenharmony_ci		status &= ~INT_MASK_ENABLE;
33808c2ecf20Sopenharmony_ci	else
33818c2ecf20Sopenharmony_ci		status &= ~INT_MASK_ENABLE_NO_FLOW;
33828c2ecf20Sopenharmony_ci
33838c2ecf20Sopenharmony_ci	/* Make sure this is our interrupt */
33848c2ecf20Sopenharmony_ci	if (!status) {
33858c2ecf20Sopenharmony_ci		handled = false;
33868c2ecf20Sopenharmony_ci		et131x_enable_interrupts(adapter);
33878c2ecf20Sopenharmony_ci		goto out;
33888c2ecf20Sopenharmony_ci	}
33898c2ecf20Sopenharmony_ci
33908c2ecf20Sopenharmony_ci	/* This is our interrupt, so process accordingly */
33918c2ecf20Sopenharmony_ci	if (status & ET_INTR_WATCHDOG) {
33928c2ecf20Sopenharmony_ci		struct tcb *tcb = tx_ring->send_head;
33938c2ecf20Sopenharmony_ci
33948c2ecf20Sopenharmony_ci		if (tcb)
33958c2ecf20Sopenharmony_ci			if (++tcb->stale > 1)
33968c2ecf20Sopenharmony_ci				status |= ET_INTR_TXDMA_ISR;
33978c2ecf20Sopenharmony_ci
33988c2ecf20Sopenharmony_ci		if (rx_ring->unfinished_receives)
33998c2ecf20Sopenharmony_ci			status |= ET_INTR_RXDMA_XFR_DONE;
34008c2ecf20Sopenharmony_ci		else if (tcb == NULL)
34018c2ecf20Sopenharmony_ci			writel(0, &adapter->regs->global.watchdog_timer);
34028c2ecf20Sopenharmony_ci
34038c2ecf20Sopenharmony_ci		status &= ~ET_INTR_WATCHDOG;
34048c2ecf20Sopenharmony_ci	}
34058c2ecf20Sopenharmony_ci
34068c2ecf20Sopenharmony_ci	if (status & (ET_INTR_RXDMA_XFR_DONE | ET_INTR_TXDMA_ISR)) {
34078c2ecf20Sopenharmony_ci		enable_interrupts = false;
34088c2ecf20Sopenharmony_ci		napi_schedule(&adapter->napi);
34098c2ecf20Sopenharmony_ci	}
34108c2ecf20Sopenharmony_ci
34118c2ecf20Sopenharmony_ci	status &= ~(ET_INTR_TXDMA_ISR | ET_INTR_RXDMA_XFR_DONE);
34128c2ecf20Sopenharmony_ci
34138c2ecf20Sopenharmony_ci	if (!status)
34148c2ecf20Sopenharmony_ci		goto out;
34158c2ecf20Sopenharmony_ci
34168c2ecf20Sopenharmony_ci	if (status & ET_INTR_TXDMA_ERR) {
34178c2ecf20Sopenharmony_ci		/* Following read also clears the register (COR) */
34188c2ecf20Sopenharmony_ci		u32 txdma_err = readl(&iomem->txdma.tx_dma_error);
34198c2ecf20Sopenharmony_ci
34208c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev,
34218c2ecf20Sopenharmony_ci			 "TXDMA_ERR interrupt, error = %d\n",
34228c2ecf20Sopenharmony_ci			 txdma_err);
34238c2ecf20Sopenharmony_ci	}
34248c2ecf20Sopenharmony_ci
34258c2ecf20Sopenharmony_ci	if (status & (ET_INTR_RXDMA_FB_R0_LOW | ET_INTR_RXDMA_FB_R1_LOW)) {
34268c2ecf20Sopenharmony_ci		/* This indicates the number of unused buffers in RXDMA free
34278c2ecf20Sopenharmony_ci		 * buffer ring 0 is <= the limit you programmed. Free buffer
34288c2ecf20Sopenharmony_ci		 * resources need to be returned.  Free buffers are consumed as
34298c2ecf20Sopenharmony_ci		 * packets are passed from the network to the host. The host
34308c2ecf20Sopenharmony_ci		 * becomes aware of the packets from the contents of the packet
34318c2ecf20Sopenharmony_ci		 * status ring. This ring is queried when the packet done
34328c2ecf20Sopenharmony_ci		 * interrupt occurs. Packets are then passed to the OS. When
34338c2ecf20Sopenharmony_ci		 * the OS is done with the packets the resources can be
34348c2ecf20Sopenharmony_ci		 * returned to the ET1310 for re-use. This interrupt is one
34358c2ecf20Sopenharmony_ci		 * method of returning resources.
34368c2ecf20Sopenharmony_ci		 */
34378c2ecf20Sopenharmony_ci
34388c2ecf20Sopenharmony_ci		/*  If the user has flow control on, then we will
34398c2ecf20Sopenharmony_ci		 * send a pause packet, otherwise just exit
34408c2ecf20Sopenharmony_ci		 */
34418c2ecf20Sopenharmony_ci		if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH) {
34428c2ecf20Sopenharmony_ci			/* Tell the device to send a pause packet via the back
34438c2ecf20Sopenharmony_ci			 * pressure register (bp req and bp xon/xoff)
34448c2ecf20Sopenharmony_ci			 */
34458c2ecf20Sopenharmony_ci			if (!et1310_in_phy_coma(adapter))
34468c2ecf20Sopenharmony_ci				writel(3, &iomem->txmac.bp_ctrl);
34478c2ecf20Sopenharmony_ci		}
34488c2ecf20Sopenharmony_ci	}
34498c2ecf20Sopenharmony_ci
34508c2ecf20Sopenharmony_ci	/* Handle Packet Status Ring Low Interrupt */
34518c2ecf20Sopenharmony_ci	if (status & ET_INTR_RXDMA_STAT_LOW) {
34528c2ecf20Sopenharmony_ci		/* Same idea as with the two Free Buffer Rings. Packets going
34538c2ecf20Sopenharmony_ci		 * from the network to the host each consume a free buffer
34548c2ecf20Sopenharmony_ci		 * resource and a packet status resource. These resources are
34558c2ecf20Sopenharmony_ci		 * passed to the OS. When the OS is done with the resources,
34568c2ecf20Sopenharmony_ci		 * they need to be returned to the ET1310. This is one method
34578c2ecf20Sopenharmony_ci		 * of returning the resources.
34588c2ecf20Sopenharmony_ci		 */
34598c2ecf20Sopenharmony_ci	}
34608c2ecf20Sopenharmony_ci
34618c2ecf20Sopenharmony_ci	if (status & ET_INTR_RXDMA_ERR) {
34628c2ecf20Sopenharmony_ci		/* The rxdma_error interrupt is sent when a time-out on a
34638c2ecf20Sopenharmony_ci		 * request issued by the JAGCore has occurred or a completion is
34648c2ecf20Sopenharmony_ci		 * returned with an un-successful status. In both cases the
34658c2ecf20Sopenharmony_ci		 * request is considered complete. The JAGCore will
34668c2ecf20Sopenharmony_ci		 * automatically re-try the request in question. Normally
34678c2ecf20Sopenharmony_ci		 * information on events like these are sent to the host using
34688c2ecf20Sopenharmony_ci		 * the "Advanced Error Reporting" capability. This interrupt is
34698c2ecf20Sopenharmony_ci		 * another way of getting similar information. The only thing
34708c2ecf20Sopenharmony_ci		 * required is to clear the interrupt by reading the ISR in the
34718c2ecf20Sopenharmony_ci		 * global resources. The JAGCore will do a re-try on the
34728c2ecf20Sopenharmony_ci		 * request. Normally you should never see this interrupt. If
34738c2ecf20Sopenharmony_ci		 * you start to see this interrupt occurring frequently then
34748c2ecf20Sopenharmony_ci		 * something bad has occurred. A reset might be the thing to do.
34758c2ecf20Sopenharmony_ci		 */
34768c2ecf20Sopenharmony_ci		/* TRAP();*/
34778c2ecf20Sopenharmony_ci
34788c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev, "RxDMA_ERR interrupt, error %x\n",
34798c2ecf20Sopenharmony_ci			 readl(&iomem->txmac.tx_test));
34808c2ecf20Sopenharmony_ci	}
34818c2ecf20Sopenharmony_ci
34828c2ecf20Sopenharmony_ci	/* Handle the Wake on LAN Event */
34838c2ecf20Sopenharmony_ci	if (status & ET_INTR_WOL) {
34848c2ecf20Sopenharmony_ci		/* This is a secondary interrupt for wake on LAN. The driver
34858c2ecf20Sopenharmony_ci		 * should never see this, if it does, something serious is
34868c2ecf20Sopenharmony_ci		 * wrong.
34878c2ecf20Sopenharmony_ci		 */
34888c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev, "WAKE_ON_LAN interrupt\n");
34898c2ecf20Sopenharmony_ci	}
34908c2ecf20Sopenharmony_ci
34918c2ecf20Sopenharmony_ci	if (status & ET_INTR_TXMAC) {
34928c2ecf20Sopenharmony_ci		u32 err = readl(&iomem->txmac.err);
34938c2ecf20Sopenharmony_ci
34948c2ecf20Sopenharmony_ci		/* When any of the errors occur and TXMAC generates an
34958c2ecf20Sopenharmony_ci		 * interrupt to report these errors, it usually means that
34968c2ecf20Sopenharmony_ci		 * TXMAC has detected an error in the data stream retrieved
34978c2ecf20Sopenharmony_ci		 * from the on-chip Tx Q. All of these errors are catastrophic
34988c2ecf20Sopenharmony_ci		 * and TXMAC won't be able to recover data when these errors
34998c2ecf20Sopenharmony_ci		 * occur. In a nutshell, the whole Tx path will have to be reset
35008c2ecf20Sopenharmony_ci		 * and re-configured afterwards.
35018c2ecf20Sopenharmony_ci		 */
35028c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev, "TXMAC interrupt, error 0x%08x\n",
35038c2ecf20Sopenharmony_ci			 err);
35048c2ecf20Sopenharmony_ci
35058c2ecf20Sopenharmony_ci		/* If we are debugging, we want to see this error, otherwise we
35068c2ecf20Sopenharmony_ci		 * just want the device to be reset and continue
35078c2ecf20Sopenharmony_ci		 */
35088c2ecf20Sopenharmony_ci	}
35098c2ecf20Sopenharmony_ci
35108c2ecf20Sopenharmony_ci	if (status & ET_INTR_RXMAC) {
35118c2ecf20Sopenharmony_ci		/* These interrupts are catastrophic to the device, what we need
35128c2ecf20Sopenharmony_ci		 * to do is disable the interrupts and set the flag to cause us
35138c2ecf20Sopenharmony_ci		 * to reset so we can solve this issue.
35148c2ecf20Sopenharmony_ci		 */
35158c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev,
35168c2ecf20Sopenharmony_ci			 "RXMAC interrupt, error 0x%08x.  Requesting reset\n",
35178c2ecf20Sopenharmony_ci			 readl(&iomem->rxmac.err_reg));
35188c2ecf20Sopenharmony_ci
35198c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev,
35208c2ecf20Sopenharmony_ci			 "Enable 0x%08x, Diag 0x%08x\n",
35218c2ecf20Sopenharmony_ci			 readl(&iomem->rxmac.ctrl),
35228c2ecf20Sopenharmony_ci			 readl(&iomem->rxmac.rxq_diag));
35238c2ecf20Sopenharmony_ci
35248c2ecf20Sopenharmony_ci		/* If we are debugging, we want to see this error, otherwise we
35258c2ecf20Sopenharmony_ci		 * just want the device to be reset and continue
35268c2ecf20Sopenharmony_ci		 */
35278c2ecf20Sopenharmony_ci	}
35288c2ecf20Sopenharmony_ci
35298c2ecf20Sopenharmony_ci	if (status & ET_INTR_MAC_STAT) {
35308c2ecf20Sopenharmony_ci		/* This means at least one of the un-masked counters in the
35318c2ecf20Sopenharmony_ci		 * MAC_STAT block has rolled over. Use this to maintain the top,
35328c2ecf20Sopenharmony_ci		 * software managed bits of the counter(s).
35338c2ecf20Sopenharmony_ci		 */
35348c2ecf20Sopenharmony_ci		et1310_handle_macstat_interrupt(adapter);
35358c2ecf20Sopenharmony_ci	}
35368c2ecf20Sopenharmony_ci
35378c2ecf20Sopenharmony_ci	if (status & ET_INTR_SLV_TIMEOUT) {
35388c2ecf20Sopenharmony_ci		/* This means a timeout has occurred on a read or write request
35398c2ecf20Sopenharmony_ci		 * to one of the JAGCore registers. The Global Resources block
35408c2ecf20Sopenharmony_ci		 * has terminated the request and on a read request, returned a
35418c2ecf20Sopenharmony_ci		 * "fake" value. The most likely reasons are: Bad Address or the
35428c2ecf20Sopenharmony_ci		 * addressed module is in a power-down state and can't respond.
35438c2ecf20Sopenharmony_ci		 */
35448c2ecf20Sopenharmony_ci	}
35458c2ecf20Sopenharmony_ci
35468c2ecf20Sopenharmony_ciout:
35478c2ecf20Sopenharmony_ci	if (enable_interrupts)
35488c2ecf20Sopenharmony_ci		et131x_enable_interrupts(adapter);
35498c2ecf20Sopenharmony_ci
35508c2ecf20Sopenharmony_ci	return IRQ_RETVAL(handled);
35518c2ecf20Sopenharmony_ci}
35528c2ecf20Sopenharmony_ci
35538c2ecf20Sopenharmony_cistatic int et131x_poll(struct napi_struct *napi, int budget)
35548c2ecf20Sopenharmony_ci{
35558c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter =
35568c2ecf20Sopenharmony_ci		container_of(napi, struct et131x_adapter, napi);
35578c2ecf20Sopenharmony_ci	int work_done = et131x_handle_recv_pkts(adapter, budget);
35588c2ecf20Sopenharmony_ci
35598c2ecf20Sopenharmony_ci	et131x_handle_send_pkts(adapter);
35608c2ecf20Sopenharmony_ci
35618c2ecf20Sopenharmony_ci	if (work_done < budget) {
35628c2ecf20Sopenharmony_ci		napi_complete_done(&adapter->napi, work_done);
35638c2ecf20Sopenharmony_ci		et131x_enable_interrupts(adapter);
35648c2ecf20Sopenharmony_ci	}
35658c2ecf20Sopenharmony_ci
35668c2ecf20Sopenharmony_ci	return work_done;
35678c2ecf20Sopenharmony_ci}
35688c2ecf20Sopenharmony_ci
35698c2ecf20Sopenharmony_ci/* et131x_stats - Return the current device statistics  */
35708c2ecf20Sopenharmony_cistatic struct net_device_stats *et131x_stats(struct net_device *netdev)
35718c2ecf20Sopenharmony_ci{
35728c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
35738c2ecf20Sopenharmony_ci	struct net_device_stats *stats = &adapter->netdev->stats;
35748c2ecf20Sopenharmony_ci	struct ce_stats *devstat = &adapter->stats;
35758c2ecf20Sopenharmony_ci
35768c2ecf20Sopenharmony_ci	stats->rx_errors = devstat->rx_length_errs +
35778c2ecf20Sopenharmony_ci			   devstat->rx_align_errs +
35788c2ecf20Sopenharmony_ci			   devstat->rx_crc_errs +
35798c2ecf20Sopenharmony_ci			   devstat->rx_code_violations +
35808c2ecf20Sopenharmony_ci			   devstat->rx_other_errs;
35818c2ecf20Sopenharmony_ci	stats->tx_errors = devstat->tx_max_pkt_errs;
35828c2ecf20Sopenharmony_ci	stats->multicast = devstat->multicast_pkts_rcvd;
35838c2ecf20Sopenharmony_ci	stats->collisions = devstat->tx_collisions;
35848c2ecf20Sopenharmony_ci
35858c2ecf20Sopenharmony_ci	stats->rx_length_errors = devstat->rx_length_errs;
35868c2ecf20Sopenharmony_ci	stats->rx_over_errors = devstat->rx_overflows;
35878c2ecf20Sopenharmony_ci	stats->rx_crc_errors = devstat->rx_crc_errs;
35888c2ecf20Sopenharmony_ci	stats->rx_dropped = devstat->rcvd_pkts_dropped;
35898c2ecf20Sopenharmony_ci
35908c2ecf20Sopenharmony_ci	/* NOTE: Not used, can't find analogous statistics */
35918c2ecf20Sopenharmony_ci	/* stats->rx_frame_errors     = devstat->; */
35928c2ecf20Sopenharmony_ci	/* stats->rx_fifo_errors      = devstat->; */
35938c2ecf20Sopenharmony_ci	/* stats->rx_missed_errors    = devstat->; */
35948c2ecf20Sopenharmony_ci
35958c2ecf20Sopenharmony_ci	/* stats->tx_aborted_errors   = devstat->; */
35968c2ecf20Sopenharmony_ci	/* stats->tx_carrier_errors   = devstat->; */
35978c2ecf20Sopenharmony_ci	/* stats->tx_fifo_errors      = devstat->; */
35988c2ecf20Sopenharmony_ci	/* stats->tx_heartbeat_errors = devstat->; */
35998c2ecf20Sopenharmony_ci	/* stats->tx_window_errors    = devstat->; */
36008c2ecf20Sopenharmony_ci	return stats;
36018c2ecf20Sopenharmony_ci}
36028c2ecf20Sopenharmony_ci
36038c2ecf20Sopenharmony_cistatic int et131x_open(struct net_device *netdev)
36048c2ecf20Sopenharmony_ci{
36058c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
36068c2ecf20Sopenharmony_ci	struct pci_dev *pdev = adapter->pdev;
36078c2ecf20Sopenharmony_ci	unsigned int irq = pdev->irq;
36088c2ecf20Sopenharmony_ci	int result;
36098c2ecf20Sopenharmony_ci
36108c2ecf20Sopenharmony_ci	/* Start the timer to track NIC errors */
36118c2ecf20Sopenharmony_ci	timer_setup(&adapter->error_timer, et131x_error_timer_handler, 0);
36128c2ecf20Sopenharmony_ci	adapter->error_timer.expires = jiffies +
36138c2ecf20Sopenharmony_ci		msecs_to_jiffies(TX_ERROR_PERIOD);
36148c2ecf20Sopenharmony_ci	add_timer(&adapter->error_timer);
36158c2ecf20Sopenharmony_ci
36168c2ecf20Sopenharmony_ci	result = request_irq(irq, et131x_isr,
36178c2ecf20Sopenharmony_ci			     IRQF_SHARED, netdev->name, netdev);
36188c2ecf20Sopenharmony_ci	if (result) {
36198c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "could not register IRQ %d\n", irq);
36208c2ecf20Sopenharmony_ci		return result;
36218c2ecf20Sopenharmony_ci	}
36228c2ecf20Sopenharmony_ci
36238c2ecf20Sopenharmony_ci	adapter->flags |= FMP_ADAPTER_INTERRUPT_IN_USE;
36248c2ecf20Sopenharmony_ci
36258c2ecf20Sopenharmony_ci	napi_enable(&adapter->napi);
36268c2ecf20Sopenharmony_ci
36278c2ecf20Sopenharmony_ci	et131x_up(netdev);
36288c2ecf20Sopenharmony_ci
36298c2ecf20Sopenharmony_ci	return result;
36308c2ecf20Sopenharmony_ci}
36318c2ecf20Sopenharmony_ci
36328c2ecf20Sopenharmony_cistatic int et131x_close(struct net_device *netdev)
36338c2ecf20Sopenharmony_ci{
36348c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
36358c2ecf20Sopenharmony_ci
36368c2ecf20Sopenharmony_ci	et131x_down(netdev);
36378c2ecf20Sopenharmony_ci	napi_disable(&adapter->napi);
36388c2ecf20Sopenharmony_ci
36398c2ecf20Sopenharmony_ci	adapter->flags &= ~FMP_ADAPTER_INTERRUPT_IN_USE;
36408c2ecf20Sopenharmony_ci	free_irq(adapter->pdev->irq, netdev);
36418c2ecf20Sopenharmony_ci
36428c2ecf20Sopenharmony_ci	/* Stop the error timer */
36438c2ecf20Sopenharmony_ci	return del_timer_sync(&adapter->error_timer);
36448c2ecf20Sopenharmony_ci}
36458c2ecf20Sopenharmony_ci
36468c2ecf20Sopenharmony_ci/* et131x_set_packet_filter - Configures the Rx Packet filtering */
36478c2ecf20Sopenharmony_cistatic int et131x_set_packet_filter(struct et131x_adapter *adapter)
36488c2ecf20Sopenharmony_ci{
36498c2ecf20Sopenharmony_ci	int filter = adapter->packet_filter;
36508c2ecf20Sopenharmony_ci	u32 ctrl;
36518c2ecf20Sopenharmony_ci	u32 pf_ctrl;
36528c2ecf20Sopenharmony_ci
36538c2ecf20Sopenharmony_ci	ctrl = readl(&adapter->regs->rxmac.ctrl);
36548c2ecf20Sopenharmony_ci	pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
36558c2ecf20Sopenharmony_ci
36568c2ecf20Sopenharmony_ci	/* Default to disabled packet filtering */
36578c2ecf20Sopenharmony_ci	ctrl |= 0x04;
36588c2ecf20Sopenharmony_ci
36598c2ecf20Sopenharmony_ci	/* Set us to be in promiscuous mode so we receive everything, this
36608c2ecf20Sopenharmony_ci	 * is also true when we get a packet filter of 0
36618c2ecf20Sopenharmony_ci	 */
36628c2ecf20Sopenharmony_ci	if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
36638c2ecf20Sopenharmony_ci		pf_ctrl &= ~7;	/* Clear filter bits */
36648c2ecf20Sopenharmony_ci	else {
36658c2ecf20Sopenharmony_ci		/* Set us up with Multicast packet filtering.  Three cases are
36668c2ecf20Sopenharmony_ci		 * possible - (1) we have a multi-cast list, (2) we receive ALL
36678c2ecf20Sopenharmony_ci		 * multicast entries or (3) we receive none.
36688c2ecf20Sopenharmony_ci		 */
36698c2ecf20Sopenharmony_ci		if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
36708c2ecf20Sopenharmony_ci			pf_ctrl &= ~2;	/* Multicast filter bit */
36718c2ecf20Sopenharmony_ci		else {
36728c2ecf20Sopenharmony_ci			et1310_setup_device_for_multicast(adapter);
36738c2ecf20Sopenharmony_ci			pf_ctrl |= 2;
36748c2ecf20Sopenharmony_ci			ctrl &= ~0x04;
36758c2ecf20Sopenharmony_ci		}
36768c2ecf20Sopenharmony_ci
36778c2ecf20Sopenharmony_ci		/* Set us up with Unicast packet filtering */
36788c2ecf20Sopenharmony_ci		if (filter & ET131X_PACKET_TYPE_DIRECTED) {
36798c2ecf20Sopenharmony_ci			et1310_setup_device_for_unicast(adapter);
36808c2ecf20Sopenharmony_ci			pf_ctrl |= 4;
36818c2ecf20Sopenharmony_ci			ctrl &= ~0x04;
36828c2ecf20Sopenharmony_ci		}
36838c2ecf20Sopenharmony_ci
36848c2ecf20Sopenharmony_ci		/* Set us up with Broadcast packet filtering */
36858c2ecf20Sopenharmony_ci		if (filter & ET131X_PACKET_TYPE_BROADCAST) {
36868c2ecf20Sopenharmony_ci			pf_ctrl |= 1;	/* Broadcast filter bit */
36878c2ecf20Sopenharmony_ci			ctrl &= ~0x04;
36888c2ecf20Sopenharmony_ci		} else {
36898c2ecf20Sopenharmony_ci			pf_ctrl &= ~1;
36908c2ecf20Sopenharmony_ci		}
36918c2ecf20Sopenharmony_ci
36928c2ecf20Sopenharmony_ci		/* Setup the receive mac configuration registers - Packet
36938c2ecf20Sopenharmony_ci		 * Filter control + the enable / disable for packet filter
36948c2ecf20Sopenharmony_ci		 * in the control reg.
36958c2ecf20Sopenharmony_ci		 */
36968c2ecf20Sopenharmony_ci		writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
36978c2ecf20Sopenharmony_ci		writel(ctrl, &adapter->regs->rxmac.ctrl);
36988c2ecf20Sopenharmony_ci	}
36998c2ecf20Sopenharmony_ci	return 0;
37008c2ecf20Sopenharmony_ci}
37018c2ecf20Sopenharmony_ci
37028c2ecf20Sopenharmony_cistatic void et131x_multicast(struct net_device *netdev)
37038c2ecf20Sopenharmony_ci{
37048c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
37058c2ecf20Sopenharmony_ci	int packet_filter;
37068c2ecf20Sopenharmony_ci	struct netdev_hw_addr *ha;
37078c2ecf20Sopenharmony_ci	int i;
37088c2ecf20Sopenharmony_ci
37098c2ecf20Sopenharmony_ci	/* Before we modify the platform-independent filter flags, store them
37108c2ecf20Sopenharmony_ci	 * locally. This allows us to determine if anything's changed and if
37118c2ecf20Sopenharmony_ci	 * we even need to bother the hardware
37128c2ecf20Sopenharmony_ci	 */
37138c2ecf20Sopenharmony_ci	packet_filter = adapter->packet_filter;
37148c2ecf20Sopenharmony_ci
37158c2ecf20Sopenharmony_ci	/* Clear the 'multicast' flag locally; because we only have a single
37168c2ecf20Sopenharmony_ci	 * flag to check multicast, and multiple multicast addresses can be
37178c2ecf20Sopenharmony_ci	 * set, this is the easiest way to determine if more than one
37188c2ecf20Sopenharmony_ci	 * multicast address is being set.
37198c2ecf20Sopenharmony_ci	 */
37208c2ecf20Sopenharmony_ci	packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
37218c2ecf20Sopenharmony_ci
37228c2ecf20Sopenharmony_ci	/* Check the net_device flags and set the device independent flags
37238c2ecf20Sopenharmony_ci	 * accordingly
37248c2ecf20Sopenharmony_ci	 */
37258c2ecf20Sopenharmony_ci	if (netdev->flags & IFF_PROMISC)
37268c2ecf20Sopenharmony_ci		adapter->packet_filter |= ET131X_PACKET_TYPE_PROMISCUOUS;
37278c2ecf20Sopenharmony_ci	else
37288c2ecf20Sopenharmony_ci		adapter->packet_filter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
37298c2ecf20Sopenharmony_ci
37308c2ecf20Sopenharmony_ci	if ((netdev->flags & IFF_ALLMULTI) ||
37318c2ecf20Sopenharmony_ci	    (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST))
37328c2ecf20Sopenharmony_ci		adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
37338c2ecf20Sopenharmony_ci
37348c2ecf20Sopenharmony_ci	if (netdev_mc_count(netdev) < 1) {
37358c2ecf20Sopenharmony_ci		adapter->packet_filter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
37368c2ecf20Sopenharmony_ci		adapter->packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
37378c2ecf20Sopenharmony_ci	} else {
37388c2ecf20Sopenharmony_ci		adapter->packet_filter |= ET131X_PACKET_TYPE_MULTICAST;
37398c2ecf20Sopenharmony_ci	}
37408c2ecf20Sopenharmony_ci
37418c2ecf20Sopenharmony_ci	/* Set values in the private adapter struct */
37428c2ecf20Sopenharmony_ci	i = 0;
37438c2ecf20Sopenharmony_ci	netdev_for_each_mc_addr(ha, netdev) {
37448c2ecf20Sopenharmony_ci		if (i == NIC_MAX_MCAST_LIST)
37458c2ecf20Sopenharmony_ci			break;
37468c2ecf20Sopenharmony_ci		ether_addr_copy(adapter->multicast_list[i++], ha->addr);
37478c2ecf20Sopenharmony_ci	}
37488c2ecf20Sopenharmony_ci	adapter->multicast_addr_count = i;
37498c2ecf20Sopenharmony_ci
37508c2ecf20Sopenharmony_ci	/* Are the new flags different from the previous ones? If not, then no
37518c2ecf20Sopenharmony_ci	 * action is required
37528c2ecf20Sopenharmony_ci	 *
37538c2ecf20Sopenharmony_ci	 * NOTE - This block will always update the multicast_list with the
37548c2ecf20Sopenharmony_ci	 *        hardware, even if the addresses aren't the same.
37558c2ecf20Sopenharmony_ci	 */
37568c2ecf20Sopenharmony_ci	if (packet_filter != adapter->packet_filter)
37578c2ecf20Sopenharmony_ci		et131x_set_packet_filter(adapter);
37588c2ecf20Sopenharmony_ci}
37598c2ecf20Sopenharmony_ci
37608c2ecf20Sopenharmony_cistatic netdev_tx_t et131x_tx(struct sk_buff *skb, struct net_device *netdev)
37618c2ecf20Sopenharmony_ci{
37628c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
37638c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
37648c2ecf20Sopenharmony_ci
37658c2ecf20Sopenharmony_ci	/* stop the queue if it's getting full */
37668c2ecf20Sopenharmony_ci	if (tx_ring->used >= NUM_TCB - 1 && !netif_queue_stopped(netdev))
37678c2ecf20Sopenharmony_ci		netif_stop_queue(netdev);
37688c2ecf20Sopenharmony_ci
37698c2ecf20Sopenharmony_ci	/* Save the timestamp for the TX timeout watchdog */
37708c2ecf20Sopenharmony_ci	netif_trans_update(netdev);
37718c2ecf20Sopenharmony_ci
37728c2ecf20Sopenharmony_ci	/* TCB is not available */
37738c2ecf20Sopenharmony_ci	if (tx_ring->used >= NUM_TCB)
37748c2ecf20Sopenharmony_ci		goto drop_err;
37758c2ecf20Sopenharmony_ci
37768c2ecf20Sopenharmony_ci	if ((adapter->flags & FMP_ADAPTER_FAIL_SEND_MASK) ||
37778c2ecf20Sopenharmony_ci	    !netif_carrier_ok(netdev))
37788c2ecf20Sopenharmony_ci		goto drop_err;
37798c2ecf20Sopenharmony_ci
37808c2ecf20Sopenharmony_ci	if (send_packet(skb, adapter))
37818c2ecf20Sopenharmony_ci		goto drop_err;
37828c2ecf20Sopenharmony_ci
37838c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
37848c2ecf20Sopenharmony_ci
37858c2ecf20Sopenharmony_cidrop_err:
37868c2ecf20Sopenharmony_ci	dev_kfree_skb_any(skb);
37878c2ecf20Sopenharmony_ci	adapter->netdev->stats.tx_dropped++;
37888c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
37898c2ecf20Sopenharmony_ci}
37908c2ecf20Sopenharmony_ci
37918c2ecf20Sopenharmony_ci/* et131x_tx_timeout - Timeout handler
37928c2ecf20Sopenharmony_ci *
37938c2ecf20Sopenharmony_ci * The handler called when a Tx request times out. The timeout period is
37948c2ecf20Sopenharmony_ci * specified by the 'tx_timeo" element in the net_device structure (see
37958c2ecf20Sopenharmony_ci * et131x_alloc_device() to see how this value is set).
37968c2ecf20Sopenharmony_ci */
37978c2ecf20Sopenharmony_cistatic void et131x_tx_timeout(struct net_device *netdev, unsigned int txqueue)
37988c2ecf20Sopenharmony_ci{
37998c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
38008c2ecf20Sopenharmony_ci	struct tx_ring *tx_ring = &adapter->tx_ring;
38018c2ecf20Sopenharmony_ci	struct tcb *tcb;
38028c2ecf20Sopenharmony_ci	unsigned long flags;
38038c2ecf20Sopenharmony_ci
38048c2ecf20Sopenharmony_ci	/* If the device is closed, ignore the timeout */
38058c2ecf20Sopenharmony_ci	if (!(adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE))
38068c2ecf20Sopenharmony_ci		return;
38078c2ecf20Sopenharmony_ci
38088c2ecf20Sopenharmony_ci	/* Any nonrecoverable hardware error?
38098c2ecf20Sopenharmony_ci	 * Checks adapter->flags for any failure in phy reading
38108c2ecf20Sopenharmony_ci	 */
38118c2ecf20Sopenharmony_ci	if (adapter->flags & FMP_ADAPTER_NON_RECOVER_ERROR)
38128c2ecf20Sopenharmony_ci		return;
38138c2ecf20Sopenharmony_ci
38148c2ecf20Sopenharmony_ci	/* Hardware failure? */
38158c2ecf20Sopenharmony_ci	if (adapter->flags & FMP_ADAPTER_HARDWARE_ERROR) {
38168c2ecf20Sopenharmony_ci		dev_err(&adapter->pdev->dev, "hardware error - reset\n");
38178c2ecf20Sopenharmony_ci		return;
38188c2ecf20Sopenharmony_ci	}
38198c2ecf20Sopenharmony_ci
38208c2ecf20Sopenharmony_ci	/* Is send stuck? */
38218c2ecf20Sopenharmony_ci	spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
38228c2ecf20Sopenharmony_ci	tcb = tx_ring->send_head;
38238c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
38248c2ecf20Sopenharmony_ci
38258c2ecf20Sopenharmony_ci	if (tcb) {
38268c2ecf20Sopenharmony_ci		tcb->count++;
38278c2ecf20Sopenharmony_ci
38288c2ecf20Sopenharmony_ci		if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
38298c2ecf20Sopenharmony_ci			dev_warn(&adapter->pdev->dev,
38308c2ecf20Sopenharmony_ci				 "Send stuck - reset. tcb->WrIndex %x\n",
38318c2ecf20Sopenharmony_ci				 tcb->index);
38328c2ecf20Sopenharmony_ci
38338c2ecf20Sopenharmony_ci			adapter->netdev->stats.tx_errors++;
38348c2ecf20Sopenharmony_ci
38358c2ecf20Sopenharmony_ci			/* perform reset of tx/rx */
38368c2ecf20Sopenharmony_ci			et131x_disable_txrx(netdev);
38378c2ecf20Sopenharmony_ci			et131x_enable_txrx(netdev);
38388c2ecf20Sopenharmony_ci		}
38398c2ecf20Sopenharmony_ci	}
38408c2ecf20Sopenharmony_ci}
38418c2ecf20Sopenharmony_ci
38428c2ecf20Sopenharmony_cistatic int et131x_change_mtu(struct net_device *netdev, int new_mtu)
38438c2ecf20Sopenharmony_ci{
38448c2ecf20Sopenharmony_ci	int result = 0;
38458c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter = netdev_priv(netdev);
38468c2ecf20Sopenharmony_ci
38478c2ecf20Sopenharmony_ci	et131x_disable_txrx(netdev);
38488c2ecf20Sopenharmony_ci
38498c2ecf20Sopenharmony_ci	netdev->mtu = new_mtu;
38508c2ecf20Sopenharmony_ci
38518c2ecf20Sopenharmony_ci	et131x_adapter_memory_free(adapter);
38528c2ecf20Sopenharmony_ci
38538c2ecf20Sopenharmony_ci	/* Set the config parameter for Jumbo Packet support */
38548c2ecf20Sopenharmony_ci	adapter->registry_jumbo_packet = new_mtu + 14;
38558c2ecf20Sopenharmony_ci	et131x_soft_reset(adapter);
38568c2ecf20Sopenharmony_ci
38578c2ecf20Sopenharmony_ci	result = et131x_adapter_memory_alloc(adapter);
38588c2ecf20Sopenharmony_ci	if (result != 0) {
38598c2ecf20Sopenharmony_ci		dev_warn(&adapter->pdev->dev,
38608c2ecf20Sopenharmony_ci			 "Change MTU failed; couldn't re-alloc DMA memory\n");
38618c2ecf20Sopenharmony_ci		return result;
38628c2ecf20Sopenharmony_ci	}
38638c2ecf20Sopenharmony_ci
38648c2ecf20Sopenharmony_ci	et131x_init_send(adapter);
38658c2ecf20Sopenharmony_ci	et131x_hwaddr_init(adapter);
38668c2ecf20Sopenharmony_ci	ether_addr_copy(netdev->dev_addr, adapter->addr);
38678c2ecf20Sopenharmony_ci
38688c2ecf20Sopenharmony_ci	/* Init the device with the new settings */
38698c2ecf20Sopenharmony_ci	et131x_adapter_setup(adapter);
38708c2ecf20Sopenharmony_ci	et131x_enable_txrx(netdev);
38718c2ecf20Sopenharmony_ci
38728c2ecf20Sopenharmony_ci	return result;
38738c2ecf20Sopenharmony_ci}
38748c2ecf20Sopenharmony_ci
38758c2ecf20Sopenharmony_cistatic const struct net_device_ops et131x_netdev_ops = {
38768c2ecf20Sopenharmony_ci	.ndo_open		= et131x_open,
38778c2ecf20Sopenharmony_ci	.ndo_stop		= et131x_close,
38788c2ecf20Sopenharmony_ci	.ndo_start_xmit		= et131x_tx,
38798c2ecf20Sopenharmony_ci	.ndo_set_rx_mode	= et131x_multicast,
38808c2ecf20Sopenharmony_ci	.ndo_tx_timeout		= et131x_tx_timeout,
38818c2ecf20Sopenharmony_ci	.ndo_change_mtu		= et131x_change_mtu,
38828c2ecf20Sopenharmony_ci	.ndo_set_mac_address	= eth_mac_addr,
38838c2ecf20Sopenharmony_ci	.ndo_validate_addr	= eth_validate_addr,
38848c2ecf20Sopenharmony_ci	.ndo_get_stats		= et131x_stats,
38858c2ecf20Sopenharmony_ci	.ndo_do_ioctl		= phy_do_ioctl,
38868c2ecf20Sopenharmony_ci};
38878c2ecf20Sopenharmony_ci
38888c2ecf20Sopenharmony_cistatic int et131x_pci_setup(struct pci_dev *pdev,
38898c2ecf20Sopenharmony_ci			    const struct pci_device_id *ent)
38908c2ecf20Sopenharmony_ci{
38918c2ecf20Sopenharmony_ci	struct net_device *netdev;
38928c2ecf20Sopenharmony_ci	struct et131x_adapter *adapter;
38938c2ecf20Sopenharmony_ci	int rc;
38948c2ecf20Sopenharmony_ci
38958c2ecf20Sopenharmony_ci	rc = pci_enable_device(pdev);
38968c2ecf20Sopenharmony_ci	if (rc < 0) {
38978c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "pci_enable_device() failed\n");
38988c2ecf20Sopenharmony_ci		goto out;
38998c2ecf20Sopenharmony_ci	}
39008c2ecf20Sopenharmony_ci
39018c2ecf20Sopenharmony_ci	/* Perform some basic PCI checks */
39028c2ecf20Sopenharmony_ci	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
39038c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Can't find PCI device's base address\n");
39048c2ecf20Sopenharmony_ci		rc = -ENODEV;
39058c2ecf20Sopenharmony_ci		goto err_disable;
39068c2ecf20Sopenharmony_ci	}
39078c2ecf20Sopenharmony_ci
39088c2ecf20Sopenharmony_ci	rc = pci_request_regions(pdev, DRIVER_NAME);
39098c2ecf20Sopenharmony_ci	if (rc < 0) {
39108c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Can't get PCI resources\n");
39118c2ecf20Sopenharmony_ci		goto err_disable;
39128c2ecf20Sopenharmony_ci	}
39138c2ecf20Sopenharmony_ci
39148c2ecf20Sopenharmony_ci	pci_set_master(pdev);
39158c2ecf20Sopenharmony_ci
39168c2ecf20Sopenharmony_ci	/* Check the DMA addressing support of this device */
39178c2ecf20Sopenharmony_ci	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
39188c2ecf20Sopenharmony_ci	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
39198c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "No usable DMA addressing method\n");
39208c2ecf20Sopenharmony_ci		rc = -EIO;
39218c2ecf20Sopenharmony_ci		goto err_release_res;
39228c2ecf20Sopenharmony_ci	}
39238c2ecf20Sopenharmony_ci
39248c2ecf20Sopenharmony_ci	netdev = alloc_etherdev(sizeof(struct et131x_adapter));
39258c2ecf20Sopenharmony_ci	if (!netdev) {
39268c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
39278c2ecf20Sopenharmony_ci		rc = -ENOMEM;
39288c2ecf20Sopenharmony_ci		goto err_release_res;
39298c2ecf20Sopenharmony_ci	}
39308c2ecf20Sopenharmony_ci
39318c2ecf20Sopenharmony_ci	netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
39328c2ecf20Sopenharmony_ci	netdev->netdev_ops     = &et131x_netdev_ops;
39338c2ecf20Sopenharmony_ci	netdev->min_mtu        = ET131X_MIN_MTU;
39348c2ecf20Sopenharmony_ci	netdev->max_mtu        = ET131X_MAX_MTU;
39358c2ecf20Sopenharmony_ci
39368c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(netdev, &pdev->dev);
39378c2ecf20Sopenharmony_ci	netdev->ethtool_ops = &et131x_ethtool_ops;
39388c2ecf20Sopenharmony_ci
39398c2ecf20Sopenharmony_ci	adapter = et131x_adapter_init(netdev, pdev);
39408c2ecf20Sopenharmony_ci
39418c2ecf20Sopenharmony_ci	rc = et131x_pci_init(adapter, pdev);
39428c2ecf20Sopenharmony_ci	if (rc < 0)
39438c2ecf20Sopenharmony_ci		goto err_free_dev;
39448c2ecf20Sopenharmony_ci
39458c2ecf20Sopenharmony_ci	/* Map the bus-relative registers to system virtual memory */
39468c2ecf20Sopenharmony_ci	adapter->regs = pci_ioremap_bar(pdev, 0);
39478c2ecf20Sopenharmony_ci	if (!adapter->regs) {
39488c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Cannot map device registers\n");
39498c2ecf20Sopenharmony_ci		rc = -ENOMEM;
39508c2ecf20Sopenharmony_ci		goto err_free_dev;
39518c2ecf20Sopenharmony_ci	}
39528c2ecf20Sopenharmony_ci
39538c2ecf20Sopenharmony_ci	/* If Phy COMA mode was enabled when we went down, disable it here. */
39548c2ecf20Sopenharmony_ci	writel(ET_PMCSR_INIT,  &adapter->regs->global.pm_csr);
39558c2ecf20Sopenharmony_ci
39568c2ecf20Sopenharmony_ci	et131x_soft_reset(adapter);
39578c2ecf20Sopenharmony_ci	et131x_disable_interrupts(adapter);
39588c2ecf20Sopenharmony_ci
39598c2ecf20Sopenharmony_ci	rc = et131x_adapter_memory_alloc(adapter);
39608c2ecf20Sopenharmony_ci	if (rc < 0) {
39618c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Could not alloc adapter memory (DMA)\n");
39628c2ecf20Sopenharmony_ci		goto err_iounmap;
39638c2ecf20Sopenharmony_ci	}
39648c2ecf20Sopenharmony_ci
39658c2ecf20Sopenharmony_ci	et131x_init_send(adapter);
39668c2ecf20Sopenharmony_ci
39678c2ecf20Sopenharmony_ci	netif_napi_add(netdev, &adapter->napi, et131x_poll, 64);
39688c2ecf20Sopenharmony_ci
39698c2ecf20Sopenharmony_ci	ether_addr_copy(netdev->dev_addr, adapter->addr);
39708c2ecf20Sopenharmony_ci
39718c2ecf20Sopenharmony_ci	rc = -ENOMEM;
39728c2ecf20Sopenharmony_ci
39738c2ecf20Sopenharmony_ci	adapter->mii_bus = mdiobus_alloc();
39748c2ecf20Sopenharmony_ci	if (!adapter->mii_bus) {
39758c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Alloc of mii_bus struct failed\n");
39768c2ecf20Sopenharmony_ci		goto err_mem_free;
39778c2ecf20Sopenharmony_ci	}
39788c2ecf20Sopenharmony_ci
39798c2ecf20Sopenharmony_ci	adapter->mii_bus->name = "et131x_eth_mii";
39808c2ecf20Sopenharmony_ci	snprintf(adapter->mii_bus->id, MII_BUS_ID_SIZE, "%x",
39818c2ecf20Sopenharmony_ci		 (adapter->pdev->bus->number << 8) | adapter->pdev->devfn);
39828c2ecf20Sopenharmony_ci	adapter->mii_bus->priv = netdev;
39838c2ecf20Sopenharmony_ci	adapter->mii_bus->read = et131x_mdio_read;
39848c2ecf20Sopenharmony_ci	adapter->mii_bus->write = et131x_mdio_write;
39858c2ecf20Sopenharmony_ci
39868c2ecf20Sopenharmony_ci	rc = mdiobus_register(adapter->mii_bus);
39878c2ecf20Sopenharmony_ci	if (rc < 0) {
39888c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to register MII bus\n");
39898c2ecf20Sopenharmony_ci		goto err_mdio_free;
39908c2ecf20Sopenharmony_ci	}
39918c2ecf20Sopenharmony_ci
39928c2ecf20Sopenharmony_ci	rc = et131x_mii_probe(netdev);
39938c2ecf20Sopenharmony_ci	if (rc < 0) {
39948c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to probe MII bus\n");
39958c2ecf20Sopenharmony_ci		goto err_mdio_unregister;
39968c2ecf20Sopenharmony_ci	}
39978c2ecf20Sopenharmony_ci
39988c2ecf20Sopenharmony_ci	et131x_adapter_setup(adapter);
39998c2ecf20Sopenharmony_ci
40008c2ecf20Sopenharmony_ci	/* Init variable for counting how long we do not have link status */
40018c2ecf20Sopenharmony_ci	adapter->boot_coma = 0;
40028c2ecf20Sopenharmony_ci	et1310_disable_phy_coma(adapter);
40038c2ecf20Sopenharmony_ci
40048c2ecf20Sopenharmony_ci	/* We can enable interrupts now
40058c2ecf20Sopenharmony_ci	 *
40068c2ecf20Sopenharmony_ci	 *  NOTE - Because registration of interrupt handler is done in the
40078c2ecf20Sopenharmony_ci	 *         device's open(), defer enabling device interrupts to that
40088c2ecf20Sopenharmony_ci	 *         point
40098c2ecf20Sopenharmony_ci	 */
40108c2ecf20Sopenharmony_ci
40118c2ecf20Sopenharmony_ci	rc = register_netdev(netdev);
40128c2ecf20Sopenharmony_ci	if (rc < 0) {
40138c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "register_netdev() failed\n");
40148c2ecf20Sopenharmony_ci		goto err_phy_disconnect;
40158c2ecf20Sopenharmony_ci	}
40168c2ecf20Sopenharmony_ci
40178c2ecf20Sopenharmony_ci	/* Register the net_device struct with the PCI subsystem. Save a copy
40188c2ecf20Sopenharmony_ci	 * of the PCI config space for this device now that the device has
40198c2ecf20Sopenharmony_ci	 * been initialized, just in case it needs to be quickly restored.
40208c2ecf20Sopenharmony_ci	 */
40218c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, netdev);
40228c2ecf20Sopenharmony_ciout:
40238c2ecf20Sopenharmony_ci	return rc;
40248c2ecf20Sopenharmony_ci
40258c2ecf20Sopenharmony_cierr_phy_disconnect:
40268c2ecf20Sopenharmony_ci	phy_disconnect(netdev->phydev);
40278c2ecf20Sopenharmony_cierr_mdio_unregister:
40288c2ecf20Sopenharmony_ci	mdiobus_unregister(adapter->mii_bus);
40298c2ecf20Sopenharmony_cierr_mdio_free:
40308c2ecf20Sopenharmony_ci	mdiobus_free(adapter->mii_bus);
40318c2ecf20Sopenharmony_cierr_mem_free:
40328c2ecf20Sopenharmony_ci	et131x_adapter_memory_free(adapter);
40338c2ecf20Sopenharmony_cierr_iounmap:
40348c2ecf20Sopenharmony_ci	iounmap(adapter->regs);
40358c2ecf20Sopenharmony_cierr_free_dev:
40368c2ecf20Sopenharmony_ci	pci_dev_put(pdev);
40378c2ecf20Sopenharmony_ci	free_netdev(netdev);
40388c2ecf20Sopenharmony_cierr_release_res:
40398c2ecf20Sopenharmony_ci	pci_release_regions(pdev);
40408c2ecf20Sopenharmony_cierr_disable:
40418c2ecf20Sopenharmony_ci	pci_disable_device(pdev);
40428c2ecf20Sopenharmony_ci	goto out;
40438c2ecf20Sopenharmony_ci}
40448c2ecf20Sopenharmony_ci
40458c2ecf20Sopenharmony_cistatic const struct pci_device_id et131x_pci_table[] = {
40468c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_GIG), 0UL},
40478c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_FAST), 0UL},
40488c2ecf20Sopenharmony_ci	{ 0,}
40498c2ecf20Sopenharmony_ci};
40508c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, et131x_pci_table);
40518c2ecf20Sopenharmony_ci
40528c2ecf20Sopenharmony_cistatic struct pci_driver et131x_driver = {
40538c2ecf20Sopenharmony_ci	.name		= DRIVER_NAME,
40548c2ecf20Sopenharmony_ci	.id_table	= et131x_pci_table,
40558c2ecf20Sopenharmony_ci	.probe		= et131x_pci_setup,
40568c2ecf20Sopenharmony_ci	.remove		= et131x_pci_remove,
40578c2ecf20Sopenharmony_ci	.driver.pm	= &et131x_pm_ops,
40588c2ecf20Sopenharmony_ci};
40598c2ecf20Sopenharmony_ci
40608c2ecf20Sopenharmony_cimodule_pci_driver(et131x_driver);
4061