1*5256Slh155975 /*
2*5256Slh155975  * CDDL HEADER START
3*5256Slh155975  *
4*5256Slh155975  * The contents of this file are subject to the terms of the
5*5256Slh155975  * Common Development and Distribution License (the "License").
6*5256Slh155975  * You may not use this file except in compliance with the License.
7*5256Slh155975  *
8*5256Slh155975  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5256Slh155975  * or http://www.opensolaris.org/os/licensing.
10*5256Slh155975  * See the License for the specific language governing permissions
11*5256Slh155975  * and limitations under the License.
12*5256Slh155975  *
13*5256Slh155975  * When distributing Covered Code, include this CDDL HEADER in each
14*5256Slh155975  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5256Slh155975  * If applicable, add the following below this CDDL HEADER, with the
16*5256Slh155975  * fields enclosed by brackets "[]" replaced with your own identifying
17*5256Slh155975  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5256Slh155975  *
19*5256Slh155975  * CDDL HEADER END
20*5256Slh155975  */
21*5256Slh155975 
22*5256Slh155975 /*
23*5256Slh155975  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*5256Slh155975  * Use is subject to license terms.
25*5256Slh155975  */
26*5256Slh155975 
27*5256Slh155975 #ifndef AMD8111S_MAIN_H
28*5256Slh155975 #define	AMD8111S_MAIN_H
29*5256Slh155975 
30*5256Slh155975 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*5256Slh155975 
32*5256Slh155975 /*
33*5256Slh155975  * Copyright (c) 2001-2006 Advanced Micro Devices, Inc.  All rights reserved.
34*5256Slh155975  *
35*5256Slh155975  * Redistribution and use in source and binary forms, with or without
36*5256Slh155975  * modification, are permitted provided that the following conditions are met:
37*5256Slh155975  *
38*5256Slh155975  * + Redistributions of source code must retain the above copyright notice,
39*5256Slh155975  * + this list of conditions and the following disclaimer.
40*5256Slh155975  *
41*5256Slh155975  * + Redistributions in binary form must reproduce the above copyright
42*5256Slh155975  * + notice, this list of conditions and the following disclaimer in the
43*5256Slh155975  * + documentation and/or other materials provided with the distribution.
44*5256Slh155975  *
45*5256Slh155975  * + Neither the name of Advanced Micro Devices, Inc. nor the names of its
46*5256Slh155975  * + contributors may be used to endorse or promote products derived from
47*5256Slh155975  * + this software without specific prior written permission.
48*5256Slh155975  *
49*5256Slh155975  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
50*5256Slh155975  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
51*5256Slh155975  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
52*5256Slh155975  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53*5256Slh155975  * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. OR
54*5256Slh155975  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55*5256Slh155975  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56*5256Slh155975  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57*5256Slh155975  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*5256Slh155975  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59*5256Slh155975  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60*5256Slh155975  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
61*5256Slh155975  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62*5256Slh155975  *
63*5256Slh155975  * Import/Export/Re-Export/Use/Release/Transfer Restrictions and
64*5256Slh155975  * Compliance with Applicable Laws.  Notice is hereby given that
65*5256Slh155975  * the software may be subject to restrictions on use, release,
66*5256Slh155975  * transfer, importation, exportation and/or re-exportation under
67*5256Slh155975  * the laws and regulations of the United States or other
68*5256Slh155975  * countries ("Applicable Laws"), which include but are not
69*5256Slh155975  * limited to U.S. export control laws such as the Export
70*5256Slh155975  * Administration Regulations and national security controls as
71*5256Slh155975  * defined thereunder, as well as State Department controls under
72*5256Slh155975  * the U.S. Munitions List.  Permission to use and/or
73*5256Slh155975  * redistribute the software is conditioned upon compliance with
74*5256Slh155975  * all Applicable Laws, including U.S. export control laws
75*5256Slh155975  * regarding specifically designated persons, countries and
76*5256Slh155975  * nationals of countries subject to national security controls.
77*5256Slh155975  */
78*5256Slh155975 
79*5256Slh155975 
80*5256Slh155975 #pragma ident "@(#)$RCSfile: odl.h,v $ $Revision: 1.1 $  " \
81*5256Slh155975 "$Date: 2004/04/22 15:22:52 $ AMD"
82*5256Slh155975 
83*5256Slh155975 #include <sys/types.h>
84*5256Slh155975 #include <sys/errno.h>
85*5256Slh155975 #include <sys/kmem.h>
86*5256Slh155975 #include <sys/conf.h>
87*5256Slh155975 #include <sys/stat.h>
88*5256Slh155975 #include <sys/note.h>
89*5256Slh155975 #include <sys/modctl.h>
90*5256Slh155975 
91*5256Slh155975 #include <sys/stream.h>
92*5256Slh155975 #include <sys/strsubr.h>
93*5256Slh155975 #include <sys/strsun.h>
94*5256Slh155975 
95*5256Slh155975 #include <sys/dditypes.h>
96*5256Slh155975 #include <sys/ddi.h>
97*5256Slh155975 #include <sys/sunddi.h>
98*5256Slh155975 
99*5256Slh155975 #include <sys/pci.h>
100*5256Slh155975 
101*5256Slh155975 #include <sys/ethernet.h>
102*5256Slh155975 #include <sys/dlpi.h>
103*5256Slh155975 #include <sys/mac.h>
104*5256Slh155975 #include <sys/mac_ether.h>
105*5256Slh155975 #include <sys/netlb.h>
106*5256Slh155975 #include "amd8111s_hw.h"
107*5256Slh155975 
108*5256Slh155975 #define	MEM_REQ_MAX		100
109*5256Slh155975 #define	MEMSET			4
110*5256Slh155975 
111*5256Slh155975 #define	IOC_LINESIZE	40
112*5256Slh155975 
113*5256Slh155975 /*
114*5256Slh155975  * Loopback definitions
115*5256Slh155975  */
116*5256Slh155975 #define	AMD8111S_LB_NONE			0
117*5256Slh155975 #define	AMD8111S_LB_EXTERNAL_1000		1
118*5256Slh155975 #define	AMD8111S_LB_EXTERNAL_100		2
119*5256Slh155975 #define	AMD8111S_LB_EXTERNAL_10			3
120*5256Slh155975 #define	AMD8111S_LB_INTERNAL_PHY		4
121*5256Slh155975 #define	AMD8111S_LB_INTERNAL_MAC		5
122*5256Slh155975 
123*5256Slh155975 /* ((2 ^ (32 - 1)) * 8) / (10 ^ 8) >= 100 */
124*5256Slh155975 #define	AMD8111S_DUMP_MIB_SECONDS_THRESHOLD	100
125*5256Slh155975 #define	AMD8111S_DUMP_MIB_BYTES_THRESHOLD	0x80000000
126*5256Slh155975 
127*5256Slh155975 /* Bit flags for 'attach_progress' */
128*5256Slh155975 #define	AMD8111S_ATTACH_PCI		0x0001	/* pci_config_setup() */
129*5256Slh155975 #define	AMD8111S_ATTACH_RESOURCE	0x0002	/* odlInit() */
130*5256Slh155975 #define	AMD8111S_ATTACH_REGS		0x0004	/* ddi_regs_map_setup() */
131*5256Slh155975 #define	AMD8111S_ATTACH_INTRADDED	0x0010	/* intr_add() */
132*5256Slh155975 #define	AMD8111S_ATTACH_MACREGED	0x0020	/* mac_register() */
133*5256Slh155975 #define	AMD8111S_ATTACH_RESCHED		0x0040	/* soft_intr() */
134*5256Slh155975 
135*5256Slh155975 #define	AMD8111S_TRY_SEND		0x0001
136*5256Slh155975 #define	AMD8111S_SEND_READY		0x0002
137*5256Slh155975 
138*5256Slh155975 #define	NEXT(buf, ptr) \
139*5256Slh155975 	(buf.ptr + 1 >= buf.msg_buf + \
140*5256Slh155975 	buf.ring_size ? \
141*5256Slh155975 	buf.msg_buf : \
142*5256Slh155975 	buf.ptr + 1)
143*5256Slh155975 /*
144*5256Slh155975  * (Internal) return values from ioctl subroutines
145*5256Slh155975  */
146*5256Slh155975 enum ioc_reply {
147*5256Slh155975 	IOC_INVAL = -1,				/* bad, NAK with EINVAL	*/
148*5256Slh155975 	IOC_DONE,				/* OK, reply sent	*/
149*5256Slh155975 	IOC_ACK,				/* OK, just send ACK	*/
150*5256Slh155975 	IOC_REPLY,				/* OK, just send reply	*/
151*5256Slh155975 	IOC_RESTART_ACK,			/* OK, restart & ACK	*/
152*5256Slh155975 	IOC_RESTART_REPLY			/* OK, restart & reply	*/
153*5256Slh155975 };
154*5256Slh155975 
155*5256Slh155975 typedef int (*TIMERfUNC) (struct LayerPointers *);
156*5256Slh155975 
157*5256Slh155975 struct TimerStructure {
158*5256Slh155975 	int Type;
159*5256Slh155975 	int Period;	/* in milliseconds */
160*5256Slh155975 	timeout_id_t TimerHandle;
161*5256Slh155975 	int (*TimerFunptr)(struct LayerPointers *);
162*5256Slh155975 	struct LayerPointers *pLayerPointers;
163*5256Slh155975 };
164*5256Slh155975 
165*5256Slh155975 struct amd8111s_statistics
166*5256Slh155975 {
167*5256Slh155975 	uint64_t intr_TINT0;	/* # of TINT0 (Tx interrupts) */
168*5256Slh155975 	uint64_t intr_RINT0;	/* # of RINT0 (Rx interrupts) */
169*5256Slh155975 	uint64_t intr_STINT;	/* # of STINT (Software Timer Intr) */
170*5256Slh155975 	uint64_t intr_OTHER;	/* Intr caused by other device */
171*5256Slh155975 
172*5256Slh155975 	uint64_t tx_ok_packets;
173*5256Slh155975 	uint64_t tx_no_descriptor;
174*5256Slh155975 	uint64_t tx_no_buffer;
175*5256Slh155975 	uint64_t tx_rescheduled;
176*5256Slh155975 	uint64_t tx_unrescheduled;
177*5256Slh155975 
178*5256Slh155975 	/* # of call amd8111s_dump_mib function */
179*5256Slh155975 	uint64_t mib_dump_counter;
180*5256Slh155975 
181*5256Slh155975 	/*
182*5256Slh155975 	 * From MIB registers (TX)
183*5256Slh155975 	 */
184*5256Slh155975 	uint64_t tx_mib_packets;		/* # of packets */
185*5256Slh155975 	uint64_t tx_mib_multicst_packets;	/* # of multicast packets */
186*5256Slh155975 	uint64_t tx_mib_broadcst_packets;	/* # of broadcast packets */
187*5256Slh155975 	uint64_t tx_mib_flowctrl_packets;	/* # of flow ctrl packets */
188*5256Slh155975 
189*5256Slh155975 	uint64_t tx_mib_bytes;			/* # of all Tx bytes */
190*5256Slh155975 
191*5256Slh155975 	/* Packet drop due to Tx FIFO underrun */
192*5256Slh155975 	uint64_t tx_mib_underrun_packets;
193*5256Slh155975 	uint64_t tx_mib_collision_packets;
194*5256Slh155975 	/* Packets successfully transmitted after experiencing one collision */
195*5256Slh155975 	uint64_t tx_mib_one_coll_packets;
196*5256Slh155975 	uint64_t tx_mib_multi_coll_packets;
197*5256Slh155975 	/* # of late collisions that occur */
198*5256Slh155975 	uint64_t tx_mib_late_coll_packets;
199*5256Slh155975 	uint64_t tx_mib_ex_coll_packets;	/* excessive collision */
200*5256Slh155975 	uint64_t tx_mib_oversize_packets;
201*5256Slh155975 	uint64_t tx_mib_defer_trans_packets;	/* defer transmit */
202*5256Slh155975 
203*5256Slh155975 
204*5256Slh155975 	/*
205*5256Slh155975 	 * Some error counter after "ifconfig amd8111sX unplumb"
206*5256Slh155975 	 */
207*5256Slh155975 	/*
208*5256Slh155975 	 * Count Tx mp number from GLD even after NIC has been unplumbed.
209*5256Slh155975 	 * This value should always be 0.
210*5256Slh155975 	 */
211*5256Slh155975 	uint64_t tx_afterunplumb;
212*5256Slh155975 	/*
213*5256Slh155975 	 * We drain all pending tx packets during unplumb operation. This
214*5256Slh155975 	 * variable is to count the drain time.
215*5256Slh155975 	 *	30 means success; =30 means fail
216*5256Slh155975 	 */
217*5256Slh155975 	uint64_t tx_draintime;
218*5256Slh155975 
219*5256Slh155975 	uint64_t rx_ok_packets;		/* # of all good packets */
220*5256Slh155975 	uint64_t rx_allocfail;		/* alloc memory fail during Rx */
221*5256Slh155975 	uint64_t rx_error_zerosize;
222*5256Slh155975 
223*5256Slh155975 	uint64_t rx_0_packets;
224*5256Slh155975 	uint64_t rx_1_15_packets;
225*5256Slh155975 	uint64_t rx_16_31_packets;
226*5256Slh155975 	uint64_t rx_32_47_packets;
227*5256Slh155975 	uint64_t rx_48_63_packets;
228*5256Slh155975 	uint64_t rx_double_overflow;
229*5256Slh155975 
230*5256Slh155975 	uint64_t rx_desc_err;
231*5256Slh155975 	uint64_t rx_desc_err_FRAM;	/* Framing error */
232*5256Slh155975 	uint64_t rx_desc_err_OFLO;	/* Overflow error */
233*5256Slh155975 	uint64_t rx_desc_err_CRC;	/* CRC error */
234*5256Slh155975 	uint64_t rx_desc_err_BUFF;	/* BCRC error */
235*5256Slh155975 
236*5256Slh155975 	/*
237*5256Slh155975 	 * From MIB registers (RX)
238*5256Slh155975 	 */
239*5256Slh155975 	uint64_t rx_mib_unicst_packets;		/* # of unicast packets */
240*5256Slh155975 	uint64_t rx_mib_multicst_packets;	/* # of multicast packets */
241*5256Slh155975 	uint64_t rx_mib_broadcst_packets;	/* # of broadcast packets */
242*5256Slh155975 	uint64_t rx_mib_macctrl_packets;	/* # of mac ctrl packets */
243*5256Slh155975 	uint64_t rx_mib_flowctrl_packets;	/* # of flow ctrl packets */
244*5256Slh155975 
245*5256Slh155975 	uint64_t rx_mib_bytes;			/* # of all Rx bytes */
246*5256Slh155975 	uint64_t rx_mib_good_bytes;			/* # of all Rx bytes */
247*5256Slh155975 	/*
248*5256Slh155975 	 * The total number of valid frames received that are less than 64
249*5256Slh155975 	 * bytes long (include the FCS).
250*5256Slh155975 	 */
251*5256Slh155975 	uint64_t rx_mib_undersize_packets;
252*5256Slh155975 	/*
253*5256Slh155975 	 * The total number of valid frames received that are greater than the
254*5256Slh155975 	 * maximum valid frame size (include the FCS).
255*5256Slh155975 	 */
256*5256Slh155975 	uint64_t rx_mib_oversize_packets;
257*5256Slh155975 
258*5256Slh155975 	uint64_t rx_mib_align_err_packets;
259*5256Slh155975 	uint64_t rx_mib_fcs_err_packets;	/* has a bad FCS */
260*5256Slh155975 	/* Invalid data symbol (RX_ER) */
261*5256Slh155975 	uint64_t rx_mib_symbol_err_packets;
262*5256Slh155975 	/* Packets that were dropped because no descriptor was available */
263*5256Slh155975 	uint64_t rx_mib_drop_packets;
264*5256Slh155975 	/*
265*5256Slh155975 	 * Packets that were dropped due to lack of resources. This includes
266*5256Slh155975 	 * the number of times a packet was dropped due to receive FIFO
267*5256Slh155975 	 * overflow and lack of receive descriptor.
268*5256Slh155975 	 */
269*5256Slh155975 	uint64_t rx_mib_miss_packets;
270*5256Slh155975 };
271*5256Slh155975 
272*5256Slh155975 struct amd8111s_msgbuf {
273*5256Slh155975 	uint64_t phy_addr;
274*5256Slh155975 	caddr_t vir_addr;
275*5256Slh155975 	uint32_t msg_size;
276*5256Slh155975 	ddi_dma_handle_t p_hdl;
277*5256Slh155975 	uint32_t offset;
278*5256Slh155975 };
279*5256Slh155975 
280*5256Slh155975 struct amd8111s_dma_ringbuf {
281*5256Slh155975 	ddi_dma_handle_t *dma_hdl;
282*5256Slh155975 	ddi_acc_handle_t *acc_hdl;
283*5256Slh155975 	ddi_dma_cookie_t *dma_cookie;
284*5256Slh155975 	caddr_t		 *trunk_addr;
285*5256Slh155975 	uint32_t buf_sz;
286*5256Slh155975 	uint32_t trunk_sz;
287*5256Slh155975 	uint32_t trunk_num;
288*5256Slh155975 	struct amd8111s_msgbuf	*msg_buf;
289*5256Slh155975 	uint32_t ring_size;
290*5256Slh155975 	uint32_t dma_buf_sz;
291*5256Slh155975 	struct amd8111s_msgbuf *free;
292*5256Slh155975 	struct amd8111s_msgbuf *next;
293*5256Slh155975 	struct amd8111s_msgbuf *curr;
294*5256Slh155975 
295*5256Slh155975 	kmutex_t ring_lock;
296*5256Slh155975 };
297*5256Slh155975 
298*5256Slh155975 struct odl {
299*5256Slh155975 	dev_info_t *devinfo;
300*5256Slh155975 
301*5256Slh155975 	mac_handle_t mh;		/* mac module handle */
302*5256Slh155975 	mac_resource_handle_t mrh;
303*5256Slh155975 
304*5256Slh155975 	struct amd8111s_statistics statistics;
305*5256Slh155975 
306*5256Slh155975 	/* Locks */
307*5256Slh155975 	kmutex_t mdlSendLock;
308*5256Slh155975 	kmutex_t mdlRcvLock;
309*5256Slh155975 	kmutex_t timer_lock;
310*5256Slh155975 	kmutex_t send_cv_lock;
311*5256Slh155975 	kcondvar_t send_cv;
312*5256Slh155975 
313*5256Slh155975 	ddi_softintr_t drain_id;
314*5256Slh155975 	/*
315*5256Slh155975 	 * The chip_lock assures that the Rx/Tx process must be stopped while
316*5256Slh155975 	 * other functions change the hardware configuration, such as attach()
317*5256Slh155975 	 * detach() etc are executed.
318*5256Slh155975 	 */
319*5256Slh155975 	krwlock_t chip_lock;
320*5256Slh155975 
321*5256Slh155975 	/*
322*5256Slh155975 	 * HW operators and parameters on attach period
323*5256Slh155975 	 */
324*5256Slh155975 	ddi_iblock_cookie_t iblock;	/* HW: interrupt block cookie */
325*5256Slh155975 	ddi_acc_handle_t MemBasehandle;
326*5256Slh155975 
327*5256Slh155975 	/* For pci configuration */
328*5256Slh155975 	ddi_acc_handle_t pci_handle;	/* HW: access handle of PCI space */
329*5256Slh155975 	uint16_t vendor_id;
330*5256Slh155975 	uint16_t device_id;
331*5256Slh155975 
332*5256Slh155975 	/*
333*5256Slh155975 	 * FreeQ: Transfer Rx Buffer parameters from top layer to low layers.
334*5256Slh155975 	 * Format of parameter:
335*5256Slh155975 	 *	(struct RxBufInfo *, physical address)
336*5256Slh155975 	 */
337*5256Slh155975 	unsigned long FreeQ[2 * RX_RING_SIZE];
338*5256Slh155975 	unsigned long *FreeQStart;
339*5256Slh155975 	unsigned long *FreeQEnd;
340*5256Slh155975 	long *FreeQWrite;
341*5256Slh155975 	long *FreeQRead;
342*5256Slh155975 
343*5256Slh155975 	/* For Rx descriptors */
344*5256Slh155975 	ddi_dma_handle_t rx_desc_dma_handle;
345*5256Slh155975 	ddi_acc_handle_t rx_desc_acc_handle;
346*5256Slh155975 	ddi_dma_cookie_t rx_desc_dma_cookie;
347*5256Slh155975 
348*5256Slh155975 	/* For Tx descriptors */
349*5256Slh155975 	ddi_dma_handle_t tx_desc_dma_handle;
350*5256Slh155975 	ddi_acc_handle_t tx_desc_acc_handle;
351*5256Slh155975 	ddi_dma_cookie_t tx_desc_dma_cookie;
352*5256Slh155975 
353*5256Slh155975 	/* For Tx buffers */
354*5256Slh155975 	struct amd8111s_dma_ringbuf tx_buf;
355*5256Slh155975 
356*5256Slh155975 	/* For Rx buffers */
357*5256Slh155975 	struct amd8111s_dma_ringbuf rx_buf;
358*5256Slh155975 
359*5256Slh155975 	ether_addr_t MacAddress;	/* Mac address */
360*5256Slh155975 
361*5256Slh155975 	/* Multicast addresses table */
362*5256Slh155975 	UCHAR MulticastAddresses
363*5256Slh155975 	    [MAX_MULTICAST_ADDRESSES][ETH_LENGTH_OF_ADDRESS];
364*5256Slh155975 
365*5256Slh155975 	link_state_t LinkStatus;
366*5256Slh155975 
367*5256Slh155975 	/* Timer */
368*5256Slh155975 	timeout_id_t Timer_id;
369*5256Slh155975 	int (*TimerFunc)(struct LayerPointers *);
370*5256Slh155975 	int timer_run;
371*5256Slh155975 	int timer_linkdown;
372*5256Slh155975 
373*5256Slh155975 	unsigned int dump_mib_seconds;
374*5256Slh155975 
375*5256Slh155975 	uint32_t loopback_mode;
376*5256Slh155975 	unsigned int rx_fcs_stripped;
377*5256Slh155975 
378*5256Slh155975 	unsigned int rx_overflow_counter;
379*5256Slh155975 	unsigned int pause_interval;
380*5256Slh155975 
381*5256Slh155975 };
382*5256Slh155975 
383*5256Slh155975 #endif	/* AMD8111S_MAIN_H */
384