xref: /titanic_52/usr/src/uts/common/io/nge/nge.h (revision 98157a7002f4f2cf7978f3084ca5577f0a1d72b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_NGE_H
28 #define	_SYS_NGE_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 
37 #include <sys/types.h>
38 #include <sys/stream.h>
39 #include <sys/strsun.h>
40 #include <sys/strsubr.h>
41 #include <sys/stat.h>
42 #include <sys/pci.h>
43 #include <sys/note.h>
44 #include <sys/modctl.h>
45 #include <sys/kstat.h>
46 #include <sys/ethernet.h>
47 #include <sys/pattr.h>
48 #include <sys/errno.h>
49 #include <sys/dlpi.h>
50 #include <sys/devops.h>
51 #include <sys/debug.h>
52 #include <sys/conf.h>
53 #include <sys/callb.h>
54 
55 #include <netinet/ip6.h>
56 
57 #include <inet/common.h>
58 #include <inet/ip.h>
59 #include <netinet/udp.h>
60 #include <inet/mi.h>
61 #include <inet/nd.h>
62 
63 #include <sys/ddi.h>
64 #include <sys/sunddi.h>
65 
66 #include <sys/mac.h>
67 #include <sys/mac_ether.h>
68 
69 /*
70  * Reconfiguring the network devices requires the net_config privilege
71  * in Solaris 10+.
72  */
73 extern int secpolicy_net_config(const cred_t *, boolean_t);
74 
75 #include <sys/netlb.h>
76 #include <sys/miiregs.h>
77 
78 #include "nge_chip.h"
79 
80 #define	PIO_ADDR(ngep, offset)	((void *)((caddr_t)(ngep)->io_regs+(offset)))
81 /*
82  * Copy an ethernet address
83  */
84 #define	ethaddr_copy(src, dst)	bcopy((src), (dst), ETHERADDRL)
85 #define	ether_eq(a, b) (bcmp((caddr_t)(a), (caddr_t)(b), (ETHERADDRL)) == 0)
86 
87 #define	BIS(w, b)	(((w) & (b)) ? B_TRUE : B_FALSE)
88 #define	BIC(w, b)	(((w) & (b)) ? B_FALSE : B_TRUE)
89 #define	UPORDOWN(x)	((x) ? "up" : "down")
90 
91 #define	NGE_DRIVER_NAME		"nge"
92 
93 /*
94  * 'Progress' bit flags ...
95  */
96 #define	PROGRESS_CFG		0x0001	/* config space mapped		*/
97 #define	PROGRESS_REGS		0x0002	/* registers mapped		*/
98 #define	PROGRESS_BUFS		0x0004	/* registers mapped		*/
99 #define	PROGRESS_RESCHED	0x0008	/* resched softint registered	*/
100 #define	PROGRESS_FACTOTUM	0x0010	/* factotum softint registered	*/
101 #define	PROGRESS_SWINT		0x0020	/* s/w interrupt registered	*/
102 #define	PROGRESS_INTR		0x0040	/* h/w interrupt registered	*/
103 					/* and mutexen initialised	*/
104 #define	PROGRESS_HWINT		0x0080
105 #define	PROGRESS_PHY		0x0100	/* PHY initialised		*/
106 #define	PROGRESS_NDD		0x0200	/* NDD parameters set up	*/
107 #define	PROGRESS_KSTATS		0x0400	/* kstats created		*/
108 #define	PROGRESS_READY		0x0800	/* ready for work		*/
109 
110 #define	NGE_HW_ERR		0x00
111 #define	NGE_HW_LINK		0x01
112 #define	NGE_HW_BM		0x02
113 #define	NGE_HW_RCHAN		0x03
114 #define	NGE_HW_TCHAN		0x04
115 #define	NGE_HW_ROM		0x05
116 #define	NGE_SW_PROBLEM_ID	0x06
117 
118 
119 /*
120  * NOTES:
121  *
122  * #defines:
123  *
124  *	NGE_PCI_CONFIG_RNUMBER and NGE_PCI_OPREGS_RNUMBER are the
125  *	register-set numbers to use for the config space registers
126  *	and the operating registers respectively.  On an OBP-based
127  *	machine, regset 0 refers to CONFIG space, and regset 1 will
128  *	be the operating registers in MEMORY space.  If an expansion
129  *	ROM is fitted, it may appear as a further register set.
130  *
131  *	NGE_DMA_MODE defines the mode (STREAMING/CONSISTENT) used
132  *	for the data buffers.  The descriptors are always set up
133  *	in CONSISTENT mode.
134  *
135  *	NGE_HEADROOM defines how much space we'll leave in allocated
136  *	mblks before the first valid data byte.  This should be chosen
137  *	to be 2 modulo 4, so that once the ethernet header (14 bytes)
138  *	has been stripped off, the packet data will be 4-byte aligned.
139  *	The remaining space can be used by upstream modules to prepend
140  *	any headers required.
141  */
142 
143 
144 #define	NGE_PCI_OPREGS_RNUMBER	1
145 #define	NGE_DMA_MODE		DDI_DMA_STREAMING
146 #define	NGE_HEADROOM		6
147 #define	ETHER_HEAD_LEN		14
148 #ifndef	VTAG_SIZE
149 #define	VTAG_SIZE		4
150 #endif
151 
152 #define	NGE_HALFTICK		268435456LL		/* 2**28 ns!	*/
153 #define	NGE_CYCLIC_PERIOD	(4*NGE_HALFTICK)	/*    ~0.5s	*/
154 
155 #define	NGE_DEFAULT_MTU		1500
156 #define	NGE_DEFAULT_SDU		1518
157 #define	NGE_MTU_2500		2500
158 #define	NGE_MTU_4500		4500
159 #define	NGE_MAX_MTU		9000
160 #define	NGE_MAX_SDU		9018
161 
162 #define	NGE_DESC_MIN		0x200
163 
164 #define	NGE_STD_BUFSZ		1792
165 #define	NGE_JB2500_BUFSZ	(3*1024)
166 #define	NGE_JB4500_BUFSZ	(5*1024)
167 #define	NGE_JB9000_BUFSZ	(9*1024)
168 
169 #define	NGE_SEND_SLOTS_DESC_1024	1024
170 #define	NGE_SEND_SLOTS_DESC_3072	3072
171 #define	NGE_SEND_JB2500_SLOTS_DESC	3072
172 #define	NGE_SEND_JB4500_SLOTS_DESC	2048
173 #define	NGE_SEND_JB9000_SLOTS_DESC	1024
174 #define	NGE_SEND_LOWMEM_SLOTS_DESC	1024
175 #define	NGE_SEND_SLOTS_BUF		3072
176 
177 #define	NGE_RECV_SLOTS_DESC_1024	1024
178 #define	NGE_RECV_SLOTS_DESC_3072	3072
179 #define	NGE_RECV_JB2500_SLOTS_DESC	3072
180 #define	NGE_RECV_JB4500_SLOTS_DESC	2048
181 #define	NGE_RECV_JB9000_SLOTS_DESC	1024
182 #define	NGE_RECV_LOWMEM_SLOTS_DESC	1024
183 #define	NGE_RECV_SLOTS_BUF		6144
184 
185 #define	NGE_SPLIT_32		32
186 #define	NGE_SPLIT_96		96
187 #define	NGE_SPLIT_256		256
188 
189 #define	NGE_RX_COPY_SIZE	512
190 #define	NGE_TX_COPY_SIZE	512
191 #define	NGE_MAP_FRAGS		3
192 #define	NGE_MAX_COOKIES		3
193 #define	NGE_MAX_DMA_HDR		(4*1024)
194 
195 /* Used by interrupt moderation */
196 #define	NGE_POLL_QUIET_TIME	100
197 #define	NGE_POLL_BUSY_TIME	2
198 #define	NGE_TX_N_INTR		128
199 
200 /*
201  * NGE-specific ioctls ...
202  */
203 #define	NGE_IOC			((((('N' << 8) + 'G') << 8) + 'E') << 8)
204 
205 /*
206  * PHY register read/write ioctls, used by cable test software
207  */
208 #define	NGE_MII_READ		(NGE_IOC|1)
209 #define	NGE_MII_WRITE		(NGE_IOC|2)
210 
211 /*
212  * SEEPROM read/write ioctls, for use by SEEPROM upgrade utility
213  *
214  * Note: SEEPROMs can only be accessed as 32-bit words, so <see_addr>
215  * must be a multiple of 4.  Not all systems have a SEEPROM fitted!
216  */
217 #define	NGE_SEE_READ		(NGE_IOC|3)
218 #define	NGE_SEE_WRITE		(NGE_IOC|4)
219 
220 
221 /*
222  * These diagnostic IOCTLS are enabled only in DEBUG drivers
223  */
224 #define	NGE_DIAG		(NGE_IOC|5)	/* currently a no-op	*/
225 #define	NGE_PEEK		(NGE_IOC|6)
226 #define	NGE_POKE		(NGE_IOC|7)
227 #define	NGE_PHY_RESET		(NGE_IOC|8)
228 #define	NGE_SOFT_RESET		(NGE_IOC|9)
229 #define	NGE_HARD_RESET		(NGE_IOC|10)
230 
231 
232 enum NGE_HW_OP {
233 	NGE_CLEAR = 0,
234 	NGE_SET
235 };
236 
237 /*
238  * Required state according to GLD
239  */
240 enum nge_mac_state {
241 	NGE_MAC_UNKNOWN,
242 	NGE_MAC_RESET,
243 	NGE_MAC_STOPPED,
244 	NGE_MAC_STARTED,
245 	NGE_MAC_UNATTACH
246 };
247 enum loop_type {
248 	NGE_LOOP_NONE = 0,
249 	NGE_LOOP_EXTERNAL_100,
250 	NGE_LOOP_EXTERNAL_10,
251 	NGE_LOOP_INTERNAL_PHY,
252 };
253 
254 /*
255  * (Internal) return values from send_msg subroutines
256  */
257 enum send_status {
258 	SEND_COPY_FAIL = -1,		/* => GLD_NORESOURCES	*/
259 	SEND_MAP_FAIL,			/* => GLD_NORESOURCES	*/
260 	SEND_COPY_SUCESS,		/* OK, msg queued	*/
261 	SEND_MAP_SUCCESS		/* OK, free msg		*/
262 };
263 
264 
265 /*
266  * NDD parameter indexes, divided into:
267  *
268  *	read-only parameters describing the hardware's capabilities
269  *	read-write parameters controlling the advertised capabilities
270  *	read-only parameters describing the partner's capabilities
271  *	read-only parameters describing the link state
272  */
273 enum {
274 	PARAM_AUTONEG_CAP,
275 	PARAM_PAUSE_CAP,
276 	PARAM_ASYM_PAUSE_CAP,
277 	PARAM_1000FDX_CAP,
278 	PARAM_1000HDX_CAP,
279 	PARAM_100T4_CAP,
280 	PARAM_100FDX_CAP,
281 	PARAM_100HDX_CAP,
282 	PARAM_10FDX_CAP,
283 	PARAM_10HDX_CAP,
284 
285 	PARAM_ADV_AUTONEG_CAP,
286 	PARAM_ADV_PAUSE_CAP,
287 	PARAM_ADV_ASYM_PAUSE_CAP,
288 	PARAM_ADV_1000FDX_CAP,
289 	PARAM_ADV_1000HDX_CAP,
290 	PARAM_ADV_100T4_CAP,
291 	PARAM_ADV_100FDX_CAP,
292 	PARAM_ADV_100HDX_CAP,
293 	PARAM_ADV_10FDX_CAP,
294 	PARAM_ADV_10HDX_CAP,
295 
296 	PARAM_LP_AUTONEG_CAP,
297 	PARAM_LP_PAUSE_CAP,
298 	PARAM_LP_ASYM_PAUSE_CAP,
299 	PARAM_LP_1000FDX_CAP,
300 	PARAM_LP_1000HDX_CAP,
301 	PARAM_LP_100T4_CAP,
302 	PARAM_LP_100FDX_CAP,
303 	PARAM_LP_100HDX_CAP,
304 	PARAM_LP_10FDX_CAP,
305 	PARAM_LP_10HDX_CAP,
306 
307 	PARAM_LINK_STATUS,
308 	PARAM_LINK_SPEED,
309 	PARAM_LINK_DUPLEX,
310 
311 	PARAM_LINK_AUTONEG,
312 	PARAM_LINK_RX_PAUSE,
313 	PARAM_LINK_TX_PAUSE,
314 
315 	PARAM_LOOP_MODE,
316 	PARAM_TXBCOPY_THRESHOLD,
317 	PARAM_RXBCOPY_THRESHOLD,
318 	PARAM_RECV_MAX_PACKET,
319 	PARAM_POLL_QUIET_TIME,
320 	PARAM_POLL_BUSY_TIME,
321 	PARAM_RX_INTR_HWATER,
322 	PARAM_RX_INTR_LWATER,
323 	PARAM_TX_N_INTR,
324 
325 	PARAM_COUNT
326 };
327 
328 
329 /*
330  * (Internal) return values from ioctl subroutines
331  */
332 enum ioc_reply {
333 	IOC_INVAL = -1,			/* bad, NAK with EINVAL	*/
334 	IOC_DONE,			/* OK, reply sent	*/
335 	IOC_ACK,			/* OK, just send ACK	*/
336 	IOC_REPLY,			/* OK, just send reply	*/
337 	IOC_RESTART_ACK,		/* OK, restart & ACK	*/
338 	IOC_RESTART_REPLY		/* OK, restart & reply	*/
339 };
340 
341 enum nge_pp_type {
342 	NGE_PP_SPACE_CFG = 0,
343 	NGE_PP_SPACE_REG,
344 	NGE_PP_SPACE_NIC,
345 	NGE_PP_SPACE_MII,
346 	NGE_PP_SPACE_NGE,
347 	NGE_PP_SPACE_TXDESC,
348 	NGE_PP_SPACE_TXBUFF,
349 	NGE_PP_SPACE_RXDESC,
350 	NGE_PP_SPACE_RXBUFF,
351 	NGE_PP_SPACE_STATISTICS,
352 	NGE_PP_SPACE_SEEPROM,
353 	NGE_PP_SPACE_FLASH
354 };
355 
356 /*
357  * Flag to kstat type
358  */
359 enum nge_kstat_type {
360 	NGE_KSTAT_RAW = 0,
361 	NGE_KSTAT_STATS,
362 	NGE_KSTAT_CHIPID,
363 	NGE_KSTAT_DEBUG,
364 	NGE_KSTAT_COUNT
365 };
366 
367 
368 /*
369  * Actual state of the nvidia's chip
370  */
371 enum nge_chip_state {
372 	NGE_CHIP_FAULT = -2,		/* fault, need reset	*/
373 	NGE_CHIP_ERROR,			/* error, want reset	*/
374 	NGE_CHIP_INITIAL,		/* Initial state only	*/
375 	NGE_CHIP_RESET,			/* reset, need init	*/
376 	NGE_CHIP_STOPPED,		/* Tx/Rx stopped	*/
377 	NGE_CHIP_RUNNING		/* with interrupts	*/
378 };
379 
380 enum nge_eeprom_size {
381 	EEPROM_1K = 0,
382 	EEPROM_2K,
383 	EEPROM_4K,
384 	EEPROM_8K,
385 	EEPROM_16K,
386 	EEPROM_32K,
387 	EEPROM_64K
388 };
389 
390 enum nge_eeprom_access_wid {
391 	ACCESS_8BIT = 0,
392 	ACCESS_16BIT
393 };
394 
395 /*
396  * MDIO operation
397  */
398 enum nge_mdio_operation {
399 	NGE_MDIO_READ = 0,
400 	NGE_MDIO_WRITE
401 };
402 
403 /*
404  * Speed selection
405  */
406 enum nge_speed {
407 	UNKOWN_SPEED = 0,
408 	NGE_10M,
409 	NGE_100M,
410 	NGE_1000M
411 };
412 
413 /*
414  * Duplex selection
415  */
416 enum nge_duplex {
417 	UNKOWN_DUPLEX = 0,
418 	NGE_HD,
419 	NGE_FD
420 };
421 
422 typedef struct {
423 	ether_addr_t		addr;		/* in canonical form	*/
424 	uint8_t			spare;
425 	uint8_t			set;		/* nonzero => valid	*/
426 } nge_mac_addr_t;
427 
428 struct nge;
429 
430 /*
431  * Named Data (ND) Parameter Management Structure
432  */
433 typedef struct {
434 	int			ndp_info;
435 	int			ndp_min;
436 	int			ndp_max;
437 	int			ndp_val;
438 	char			*ndp_name;
439 } nd_param_t;
440 
441 
442 #define	CHIP_FLAG_COPPER	0x40
443 
444 /*
445  * Collection of physical-layer functions to:
446  *	(re)initialise the physical layer
447  *	update it to match software settings
448  *	check for link status change
449  */
450 typedef struct {
451 	void		(*phys_restart)(struct nge *);
452 	void		(*phys_update)(struct nge *);
453 	boolean_t	(*phys_check)(struct nge *);
454 } phys_ops_t;
455 
456 struct nge_see_rw {
457 	uint32_t	see_addr;	/* Byte offset within SEEPROM	*/
458 	uint32_t	see_data;	/* Data read/data to write	*/
459 };
460 
461 typedef struct {
462 	uint64_t	pp_acc_size;	/* in bytes: 1,2,4,8	*/
463 	uint64_t	pp_acc_space;	/* See #defines below	*/
464 	uint64_t	pp_acc_offset;
465 	uint64_t	pp_acc_data;	/* output for peek	*/
466 					/* input for poke	*/
467 } nge_peekpoke_t;
468 
469 typedef uintptr_t 	nge_regno_t;	/* register # (offset)	*/
470 
471 typedef struct _mul_list {
472 	struct _mul_list *next;
473 	uint32_t ref_cnt;
474 	ether_addr_t mul_addr;
475 }mul_item, *pmul_item;
476 
477 /*
478  * Describes one chunk of allocated DMA-able memory
479  *
480  * In some cases, this is a single chunk as allocated from the system;
481  * but we also use this structure to represent slices carved off such
482  * a chunk.  Even when we don't really need all the information, we
483  * use this structure as a convenient way of correlating the various
484  * ways of looking at a piece of memory (kernel VA, IO space DVMA,
485  * handle+offset, etc).
486  */
487 typedef struct dma_area
488 {
489 
490 	caddr_t			private;	/* pointer to nge */
491 	frtn_t			rx_recycle;	/* recycle function */
492 	mblk_t			*mp;
493 	ddi_acc_handle_t	acc_hdl;	/* handle for memory	*/
494 	void			*mem_va;	/* CPU VA of memory	*/
495 	uint32_t		nslots;		/* number of slots	*/
496 	uint32_t		size;		/* size per slot	*/
497 	size_t			alength;	/* allocated size	*/
498 						/* >= product of above	*/
499 	ddi_dma_handle_t	dma_hdl;	/* DMA handle		*/
500 	offset_t		offset;		/* relative to handle	*/
501 	ddi_dma_cookie_t	cookie;		/* associated cookie	*/
502 	uint32_t		ncookies;
503 	uint32_t		signature;	/* buffer signature	*/
504 						/* for deciding to free */
505 						/* or to reuse buffers	*/
506 	boolean_t		rx_delivered;	/* hold by upper layer	*/
507 	struct dma_area		*next;
508 } dma_area_t;
509 
510 #define	HOST_OWN	0x00000000
511 #define	CONTROLER_OWN	0x00000001
512 #define	NGE_END_PACKET	0x00000002
513 
514 
515 typedef struct nge_dmah_node
516 {
517 	struct nge_dmah_node	*next;
518 	ddi_dma_handle_t	hndl;
519 } nge_dmah_node_t;
520 
521 typedef struct nge_dmah_list
522 {
523 	nge_dmah_node_t	*head;
524 	nge_dmah_node_t	*tail;
525 } nge_dmah_list_t;
526 
527 /*
528  * Software version of the Recv Descriptor
529  * There's one of these for each recv buffer (up to 512 per ring)
530  */
531 typedef struct sw_rx_sbd {
532 
533 	dma_area_t		desc;		/* (const) related h/w	*/
534 						/* descriptor area	*/
535 	dma_area_t		*bufp;		/* (const) related	*/
536 						/* buffer area		*/
537 	uint8_t			flags;
538 } sw_rx_sbd_t;
539 
540 /*
541  * Software version of the send Buffer Descriptor
542  * There's one of these for each send buffer (up to 512 per ring)
543  */
544 typedef struct sw_tx_sbd {
545 
546 	dma_area_t		desc;		/* (const) related h/w	*/
547 						/* descriptor area	*/
548 	dma_area_t		pbuf;		/* (const) related	*/
549 						/* buffer area		*/
550 	void			(*tx_recycle)(struct sw_tx_sbd *);
551 	uint32_t		flags;
552 	mblk_t			*mp;		/* related mblk, if any	*/
553 	nge_dmah_list_t		mp_hndl;
554 	uint32_t		frags;
555 	uint32_t		ncookies;	/* dma cookie number */
556 
557 } sw_tx_sbd_t;
558 
559 /*
560  * Software Receive Buffer (Producer) Ring Control Block
561  * There's one of these for each receiver producer ring (up to 3),
562  * but each holds buffers of a different size.
563  */
564 typedef struct buff_ring {
565 
566 	uint64_t		nslots;		/* descriptor area	*/
567 	struct nge		*ngep;		/* (const) containing	*/
568 						/* driver soft state	*/
569 						/* initialise same	*/
570 	uint64_t		rx_hold;
571 	sw_rx_sbd_t		*sw_rbds; 	/* software descriptors	*/
572 	sw_rx_sbd_t		*free_rbds;	/* free ring */
573 	dma_area_t		*free_list;	/* available buffer queue */
574 	dma_area_t		*recycle_list;	/* recycling buffer queue */
575 	kmutex_t		recycle_lock[1];
576 	uint32_t		buf_sign;	/* buffer ring signature */
577 						/* for deciding to free  */
578 						/* or to reuse buffers   */
579 	boolean_t		rx_bcopy;
580 } buff_ring_t;
581 
582 /*
583  * Software Receive (Return) Ring Control Block
584  * There's one of these for each receiver return ring (up to 16).
585  */
586 typedef struct recv_ring {
587 	/*
588 	 * The elements flagged (const) in the comments below are
589 	 * set up once during initialiation and thereafter unchanged.
590 	 */
591 	dma_area_t		desc;		/* (const) related h/w	*/
592 						/* descriptor area	*/
593 	struct nge		*ngep;		/* (const) containing	*/
594 						/* driver soft state	*/
595 	uint16_t		prod_index;	/* (const) ptr to h/w	*/
596 						/* "producer index"	*/
597 	mac_resource_handle_t	handle;
598 } recv_ring_t;
599 
600 
601 
602 /*
603  * Software Send Ring Control Block
604  * There's one of these for each of (up to) 1 send rings
605  */
606 typedef struct send_ring {
607 	/*
608 	 * The elements flagged (const) in the comments below are
609 	 * set up once during initialiation and thereafter unchanged.
610 	 */
611 	dma_area_t		desc;		/* (const) related h/w	*/
612 						/* descriptor area	*/
613 	dma_area_t		buf[NGE_SEND_SLOTS_BUF];
614 						/* buffer area(s)	*/
615 	struct nge		*ngep;		/* (const) containing	*/
616 						/* driver soft state	*/
617 
618 	uint64_t		tx_hwmark;
619 	uint64_t		tx_lwmark;
620 
621 	/*
622 	 * The tx_lock must be held when updating
623 	 * the s/w producer index
624 	 * (tx_next)
625 	 */
626 	kmutex_t		tx_lock[1];	/* serialize h/w update	*/
627 	uint64_t		tx_next;	/* next slot to use	*/
628 	uint64_t		tx_flow;
629 
630 	/*
631 	 * These counters/indexes are manipulated in the transmit
632 	 * path using atomics rather than mutexes for speed
633 	 */
634 	uint64_t		tx_free;	/* # of slots available	*/
635 
636 	/*
637 	 * index (tc_next).
638 	 */
639 	kmutex_t		tc_lock[1];
640 	uint64_t		tc_next;	/* next slot to recycle	*/
641 						/* ("consumer index")	*/
642 
643 	sw_tx_sbd_t		*sw_sbds; 	/* software descriptors	*/
644 
645 	kmutex_t		dmah_lock;
646 	nge_dmah_list_t		dmah_free;
647 	nge_dmah_node_t		dmahndl[NGE_MAX_DMA_HDR];
648 
649 } send_ring_t;
650 
651 
652 typedef struct {
653 	uint32_t		businfo;	/* from private reg	*/
654 	uint16_t		command;	/* saved during attach	*/
655 
656 	uint16_t		vendor;		/* vendor-id		*/
657 	uint16_t		device;		/* device-id		*/
658 	uint16_t		subven;		/* subsystem-vendor-id	*/
659 	uint16_t		subdev;		/* subsystem-id		*/
660 	uint8_t			class_code;
661 	uint8_t			revision;	/* revision-id		*/
662 	uint8_t			clsize;		/* cache-line-size	*/
663 	uint8_t			latency;	/* latency-timer	*/
664 	uint8_t			flags;
665 
666 	uint16_t		phy_type;	/* Fiber module type 	*/
667 	uint64_t		hw_mac_addr;	/* from chip register	*/
668 	nge_mac_addr_t		vendor_addr;	/* transform of same	*/
669 } chip_info_t;
670 
671 
672 typedef struct {
673 	offset_t	index;
674 	char		*name;
675 } nge_ksindex_t;
676 
677 typedef struct {
678 	uint64_t tso_err_mss;
679 	uint64_t tso_dis;
680 	uint64_t tso_err_nosum;
681 	uint64_t tso_err_hov;
682 	uint64_t tso_err_huf;
683 	uint64_t tso_err_l2;
684 	uint64_t tso_err_ip;
685 	uint64_t tso_err_l4;
686 	uint64_t tso_err_tcp;
687 	uint64_t hsum_err_ip;
688 	uint64_t hsum_err_l4;
689 }fe_statistics_t;
690 
691 /*
692  * statistics parameters to tune the driver
693  */
694 typedef struct {
695 	uint64_t		intr_count;
696 	uint64_t		intr_lval;
697 	uint64_t		recv_realloc;
698 	uint64_t		poll_time;
699 	uint64_t		recy_free;
700 	uint64_t		recv_count;
701 	uint64_t		xmit_count;
702 	uint64_t		obytes;
703 	uint64_t		rbytes;
704 	uint64_t		mp_alloc_err;
705 	uint64_t		dma_alloc_err;
706 	uint64_t		kmem_alloc_err;
707 	uint64_t		load_context;
708 	uint64_t		ip_hwsum_err;
709 	uint64_t		tcp_hwsum_err;
710 	uint64_t		rx_nobuffer;
711 	uint64_t		rx_err;
712 	uint64_t		tx_stop_err;
713 	uint64_t		tx_stall;
714 	uint64_t		tx_rsrv_fail;
715 	uint64_t		tx_resched;
716 	fe_statistics_t	fe_err;
717 }nge_sw_statistics_t;
718 
719 typedef struct {
720 	nge_hw_statistics_t	hw_statistics;
721 	nge_sw_statistics_t	sw_statistics;
722 }nge_statistics_t;
723 
724 struct nge_desc_attr	{
725 
726 	size_t	rxd_size;
727 	size_t	txd_size;
728 
729 	ddi_dma_attr_t	*dma_attr;
730 	ddi_dma_attr_t	*tx_dma_attr;
731 
732 	void (*rxd_fill)(void *, const ddi_dma_cookie_t *, size_t);
733 	uint32_t (*rxd_check)(const void *, size_t *);
734 
735 	void (*txd_fill)(void *, const ddi_dma_cookie_t *, size_t,
736 			uint32_t, boolean_t);
737 
738 	uint32_t (*txd_check)(const void *, size_t *);
739 };
740 
741 typedef struct nge_desc_attr nge_desc_attr_t;
742 
743 /*
744  * Structure used to hold the device-specific config parameters.
745  * The setting of such parameters may not consistent with the
746  * hardware feature of the device. It's used for software purpose.
747  */
748 typedef struct nge_dev_spec_param {
749 	boolean_t	msi;		/* specifies msi support */
750 	boolean_t	msi_x;		/* specifies msi_x support */
751 	boolean_t	vlan;		/* specifies vlan support */
752 	boolean_t	advanced_pm;	/* advanced power management support */
753 	boolean_t	tx_pause_frame;	/* specifies tx pause frame support */
754 	boolean_t	rx_pause_frame;	/* specifies rx pause frame support */
755 	boolean_t	jumbo;		/* jumbo frame support */
756 	boolean_t	tx_rx_64byte;	/* set the max tx/rx prd fetch size */
757 	boolean_t	rx_hw_checksum;	/* specifies tx hw checksum feature */
758 	uint32_t	tx_hw_checksum;	/* specifies rx hw checksum feature */
759 	uint32_t	desc_type;	/* specifies descriptor type */
760 	uint32_t	rx_desc_num;	/* specifies rx descriptor number */
761 	uint32_t	tx_desc_num;	/* specifies tx descriptor number */
762 	uint32_t	nge_split;	/* specifies the split number */
763 } nge_dev_spec_param_t;
764 
765 typedef struct nge {
766 	/*
767 	 * These fields are set by attach() and unchanged thereafter ...
768 	 */
769 	dev_info_t		*devinfo;	/* device instance	*/
770 	mac_handle_t		mh;		/* mac module handle    */
771 	chip_info_t		chipinfo;
772 	ddi_acc_handle_t	cfg_handle;	/* DDI I/O handle	*/
773 	ddi_acc_handle_t	io_handle;	/* DDI I/O handle	*/
774 	void			*io_regs;	/* mapped registers	*/
775 
776 	ddi_periodic_t		periodic_id;	/* periodical callback	*/
777 	uint32_t		factotum_flag;
778 	ddi_softint_handle_t	factotum_hdl;	/* factotum callback	*/
779 	ddi_softint_handle_t	resched_hdl;	/* reschedule callback	*/
780 	uint_t			soft_pri;
781 
782 	ddi_intr_handle_t 	*htable;	/* for array of interrupts */
783 	int			intr_type;	/* type of interrupt */
784 	int			intr_actual_cnt; /* alloc intrs count */
785 	int			intr_req_cnt;	/* request intrs count */
786 	uint_t			intr_pri;	/* interrupt priority	*/
787 	int			intr_cap;	/* interrupt capabilities */
788 
789 	uint32_t		progress;	/* attach tracking	*/
790 	uint32_t		debug;		/* flag to debug function */
791 
792 	char			ifname[8];	/* "nge0" ... "nge999" */
793 
794 
795 	enum nge_mac_state	nge_mac_state;	/* definitions above	*/
796 	enum nge_chip_state	nge_chip_state; /* definitions above	*/
797 	boolean_t		promisc;
798 	boolean_t		suspended;
799 
800 	int			resched_needed;
801 	uint32_t		default_mtu;
802 	uint32_t		max_sdu;
803 	uint32_t		buf_size;
804 	uint32_t		rx_desc;
805 	uint32_t		tx_desc;
806 	uint32_t		rx_buf;
807 	uint32_t		nge_split;
808 	uint32_t		watchdog;
809 	uint32_t		lowmem_mode;
810 
811 
812 	/*
813 	 * Runtime read-write data starts here ...
814 	 * 1 Receive Rings
815 	 * 1 Send Rings
816 	 *
817 	 * Note: they're not necessarily all used.
818 	 */
819 	struct buff_ring	buff[1];
820 	struct recv_ring	recv[1];
821 	struct send_ring	send[1];
822 
823 
824 	kmutex_t		genlock[1];
825 	krwlock_t		rwlock[1];
826 	kmutex_t		softlock[1];
827 	uint32_t		intr_masks;
828 	boolean_t		poll;
829 	boolean_t		ch_intr_mode;
830 	boolean_t		intr_moderation;
831 	uint32_t		recv_count;
832 	uint32_t		quiet_time;
833 	uint32_t		busy_time;
834 	uint32_t		stint_count;
835 	uint32_t		sw_intr_intv;
836 	nge_mac_addr_t		cur_uni_addr;
837 	uint32_t		rx_datahwm;
838 	uint32_t		rx_prdlwm;
839 	uint32_t		rx_prdhwm;
840 	uint32_t		rx_def;
841 	uint32_t		desc_mode;
842 
843 	mul_item		*pcur_mulist;
844 	nge_mac_addr_t		cur_mul_addr;
845 	nge_mac_addr_t		cur_mul_mask;
846 
847 	nge_desc_attr_t		desc_attr;
848 
849 	/*
850 	 * Link state data (protected by genlock)
851 	 */
852 	int32_t			link_state;	/* See GLD #defines	*/
853 	uint32_t		stall_cknum;	/* Stall check number */
854 
855 	uint32_t		phy_xmii_addr;
856 	uint32_t		phy_id;
857 	uint32_t		phy_mode;
858 	const phys_ops_t	*physops;
859 	uint16_t		phy_gen_status;
860 
861 	uint32_t		param_loop_mode;
862 
863 	/*
864 	 * NDD parameters (protected by genlock)
865 	 */
866 	caddr_t			nd_data_p;
867 	nd_param_t		nd_params[PARAM_COUNT];
868 
869 	kstat_t			*nge_kstats[NGE_KSTAT_COUNT];
870 	nge_statistics_t	statistics;
871 
872 	nge_dev_spec_param_t	dev_spec_param;
873 
874 } nge_t;
875 
876 extern const nge_ksindex_t nge_statistics[];
877 
878 /*
879  * Shorthand for the NDD parameters
880  */
881 #define	param_adv_autoneg	nd_params[PARAM_ADV_AUTONEG_CAP].ndp_val
882 #define	param_adv_pause		nd_params[PARAM_ADV_PAUSE_CAP].ndp_val
883 #define	param_adv_asym_pause	nd_params[PARAM_ADV_ASYM_PAUSE_CAP].ndp_val
884 #define	param_adv_1000fdx	nd_params[PARAM_ADV_1000FDX_CAP].ndp_val
885 #define	param_adv_1000hdx	nd_params[PARAM_ADV_1000HDX_CAP].ndp_val
886 #define	param_adv_100fdx	nd_params[PARAM_ADV_100FDX_CAP].ndp_val
887 #define	param_adv_100hdx	nd_params[PARAM_ADV_100HDX_CAP].ndp_val
888 #define	param_adv_10fdx		nd_params[PARAM_ADV_10FDX_CAP].ndp_val
889 #define	param_adv_10hdx		nd_params[PARAM_ADV_10HDX_CAP].ndp_val
890 
891 #define	param_lp_autoneg	nd_params[PARAM_LP_AUTONEG_CAP].ndp_val
892 #define	param_lp_pause		nd_params[PARAM_LP_PAUSE_CAP].ndp_val
893 #define	param_lp_asym_pause	nd_params[PARAM_LP_ASYM_PAUSE_CAP].ndp_val
894 #define	param_lp_1000fdx	nd_params[PARAM_LP_1000FDX_CAP].ndp_val
895 #define	param_lp_1000hdx	nd_params[PARAM_LP_1000HDX_CAP].ndp_val
896 #define	param_lp_100fdx		nd_params[PARAM_LP_100FDX_CAP].ndp_val
897 #define	param_lp_100hdx		nd_params[PARAM_LP_100HDX_CAP].ndp_val
898 #define	param_lp_10fdx		nd_params[PARAM_LP_10FDX_CAP].ndp_val
899 #define	param_lp_10hdx		nd_params[PARAM_LP_10HDX_CAP].ndp_val
900 
901 #define	param_link_up		nd_params[PARAM_LINK_STATUS].ndp_val
902 #define	param_link_speed	nd_params[PARAM_LINK_SPEED].ndp_val
903 #define	param_link_duplex	nd_params[PARAM_LINK_DUPLEX].ndp_val
904 
905 #define	param_link_autoneg	nd_params[PARAM_LINK_AUTONEG].ndp_val
906 #define	param_link_rx_pause	nd_params[PARAM_LINK_RX_PAUSE].ndp_val
907 #define	param_link_tx_pause	nd_params[PARAM_LINK_TX_PAUSE].ndp_val
908 
909 #define	param_loop_mode		nd_params[PARAM_LOOP_MODE].ndp_val
910 
911 #define	param_txbcopy_threshold	nd_params[PARAM_TXBCOPY_THRESHOLD].ndp_val
912 #define	param_rxbcopy_threshold	nd_params[PARAM_RXBCOPY_THRESHOLD].ndp_val
913 #define	param_recv_max_packet	nd_params[PARAM_RECV_MAX_PACKET].ndp_val
914 #define	param_poll_quiet_time	nd_params[PARAM_POLL_QUIET_TIME].ndp_val
915 #define	param_poll_busy_time	nd_params[PARAM_POLL_BUSY_TIME].ndp_val
916 #define	param_rx_intr_hwater	nd_params[PARAM_RX_INTR_HWATER].ndp_val
917 #define	param_rx_intr_lwater	nd_params[PARAM_RX_INTR_LWATER].ndp_val
918 #define	param_tx_n_intr		nd_params[PARAM_TX_N_INTR].ndp_val
919 
920 /*
921  * Sync a DMA area described by a dma_area_t
922  */
923 #define	DMA_SYNC(area, flag)	((void) ddi_dma_sync((area).dma_hdl,	\
924 				    (area).offset, (area).alength, (flag)))
925 
926 /*
927  * Find the (kernel virtual) address of block of memory
928  * described by a dma_area_t
929  */
930 #define	DMA_VPTR(area)		((area).mem_va)
931 
932 /*
933  * Zero a block of memory described by a dma_area_t
934  */
935 #define	DMA_ZERO(area)		bzero(DMA_VPTR(area), (area).alength)
936 
937 /*
938  * Next/Prev value of a cyclic index
939  */
940 #define	NEXT(index, limit)	((index) + 1 < (limit) ? (index) + 1 : 0)
941 #define	PREV(index, limit)	(0 == (index) ? (limit - 1) : (index) - 1)
942 
943 #define	NEXT_INDEX(ndx, num, lim)\
944 	(((ndx) + (num) < (lim)) ? ((ndx) + (num)) : ((ndx) + (num) - (lim)))
945 
946 
947 /*
948  * Property lookups
949  */
950 #define	NGE_PROP_EXISTS(d, n)	ddi_prop_exists(DDI_DEV_T_ANY, (d),	\
951 					DDI_PROP_DONTPASS, (n))
952 #define	NGE_PROP_GET_INT(d, n)	ddi_prop_get_int(DDI_DEV_T_ANY, (d),	\
953 					DDI_PROP_DONTPASS, (n), -1)
954 
955 
956 /*
957  * Debugging ...
958  */
959 #ifdef	DEBUG
960 #define	NGE_DEBUGGING		1
961 #else
962 #define	NGE_DEBUGGING		0
963 #endif	/* DEBUG */
964 
965 /*
966  * Bit flags in the 'debug' word ...
967  */
968 #define	NGE_DBG_STOP		0x00000001	/* early debug_enter()	*/
969 #define	NGE_DBG_TRACE		0x00000002	/* general flow tracing	*/
970 
971 #define	NGE_DBG_MII		0x00000010	/* low-level MII access	*/
972 #define	NGE_DBG_CHIP		0x00000020	/* low(ish)-level code	*/
973 
974 #define	NGE_DBG_RECV		0x00000100	/* receive-side code	*/
975 #define	NGE_DBG_SEND		0x00000200	/* packet-send code	*/
976 
977 #define	NGE_DBG_INIT		0x00100000	/* initialisation	*/
978 #define	NGE_DBG_NEMO		0x00200000	/* MAC layer entry points */
979 #define	NGE_DBG_STATS		0x00400000	/* statistics		*/
980 
981 #define	NGE_DBG_BADIOC		0x01000000	/* unknown ioctls	*/
982 
983 #define	NGE_DBG_NDD		0x10000000	/* NDD operations	*/
984 
985 
986 
987 /*
988  * 'Do-if-debugging' macro.  The parameter <command> should be one or more
989  * C statements (but without the *final* semicolon), which will either be
990  * compiled inline or completely ignored, depending on the NGE_DEBUGGING
991  * compile-time flag.
992  *
993  * You should get a compile-time error (at least on a DEBUG build) if
994  * your statement isn't actually a statement, rather than unexpected
995  * run-time behaviour caused by unintended matching of if-then-elses etc.
996  *
997  * Note that the NGE_DDB() macro itself can only be used as a statement,
998  * not an expression, and should always be followed by a semicolon.
999  */
1000 #if NGE_DEBUGGING
1001 #define	NGE_DDB(command)	do {					\
1002 					{ command; }			\
1003 					_NOTE(CONSTANTCONDITION)	\
1004 				} while (0)
1005 #else 	/* NGE_DEBUGGING */
1006 #define	NGE_DDB(command)
1007 /*
1008  * Old way of debugging.  This is a poor way, as it leeaves empty
1009  * statements that cause lint to croak.
1010  * #define	NGE_DDB(command)	do {				\
1011  * 					{ _NOTE(EMPTY); }		\
1012  * 					_NOTE(CONSTANTCONDITION)	\
1013  * 				} while (0)
1014  */
1015 #endif	/* NGE_DEBUGGING */
1016 
1017 /*
1018  * 'Internal' macros used to construct the TRACE/DEBUG macros below.
1019  * These provide the primitive conditional-call capability required.
1020  * Note: the parameter <args> is a parenthesised list of the actual
1021  * printf-style arguments to be passed to the debug function ...
1022  */
1023 #define	NGE_XDB(b, w, f, args)	NGE_DDB(if ((b) & (w)) f args)
1024 #define	NGE_GDB(b, args)	NGE_XDB(b, nge_debug, (*nge_gdb()), args)
1025 #define	NGE_LDB(b, args)	NGE_XDB(b, ngep->debug, \
1026 				    (*nge_db(ngep)), args)
1027 #define	NGE_CDB(f, args)	NGE_XDB(NGE_DBG, ngep->debug, f, args)
1028 
1029 /*
1030  * Conditional-print macros.
1031  *
1032  * Define NGE_DBG to be the relevant member of the set of NGE_DBG_* values
1033  * above before using the NGE_GDEBUG() or NGE_DEBUG() macros.  The 'G'
1034  * versions look at the Global debug flag word (nge_debug); the non-G
1035  * versions look in the per-instance data (ngep->debug) and so require a
1036  * variable called 'ngep' to be in scope (and initialised!) before use.
1037  *
1038  * You could redefine NGE_TRC too if you really need two different
1039  * flavours of debugging output in the same area of code, but I don't
1040  * really recommend it.
1041  *
1042  * Note: the parameter <args> is a parenthesised list of the actual
1043  * arguments to be passed to the debug function, usually a printf-style
1044  * format string and corresponding values to be formatted.
1045  */
1046 
1047 #define	NGE_TRC	NGE_DBG_TRACE
1048 
1049 #define	NGE_GTRACE(args)	NGE_GDB(NGE_TRC, args)
1050 #define	NGE_GDEBUG(args)	NGE_GDB(NGE_DBG, args)
1051 #define	NGE_TRACE(args)		NGE_LDB(NGE_TRC, args)
1052 #define	NGE_DEBUG(args)		NGE_LDB(NGE_DBG, args)
1053 
1054 /*
1055  * Debug-only action macros
1056  */
1057 
1058 
1059 #define	NGE_REPORT(args)	NGE_DDB(nge_log args)
1060 
1061 boolean_t nge_atomic_decrease(uint64_t *count_p, uint64_t n);
1062 void nge_atomic_increase(uint64_t *count_p, uint64_t n);
1063 
1064 int nge_alloc_dma_mem(nge_t *ngep, size_t memsize,
1065     ddi_device_acc_attr_t *attr_p, uint_t dma_flags, dma_area_t *dma_p);
1066 void nge_free_dma_mem(dma_area_t *dma_p);
1067 int nge_restart(nge_t *ngep);
1068 void nge_wake_factotum(nge_t *ngep);
1069 
1070 uint8_t nge_reg_get8(nge_t *ngep, nge_regno_t regno);
1071 void nge_reg_put8(nge_t *ngep, nge_regno_t regno, uint8_t data);
1072 uint16_t nge_reg_get16(nge_t *ngep, nge_regno_t regno);
1073 void nge_reg_put16(nge_t *ngep, nge_regno_t regno, uint16_t data);
1074 uint32_t nge_reg_get32(nge_t *ngep, nge_regno_t regno);
1075 void nge_reg_put32(nge_t *ngep, nge_regno_t regno, uint32_t data);
1076 uint_t nge_chip_factotum(caddr_t args1, caddr_t args2);
1077 void nge_chip_cfg_init(nge_t *ngep, chip_info_t *infop, boolean_t reset);
1078 void nge_init_dev_spec_param(nge_t *ngep);
1079 int nge_chip_stop(nge_t *ngep, boolean_t fault);
1080 void nge_restore_mac_addr(nge_t *ngep);
1081 int nge_chip_reset(nge_t *ngep);
1082 int nge_chip_start(nge_t *ngep);
1083 void nge_chip_sync(nge_t *ngep);
1084 
1085 uint_t nge_chip_intr(caddr_t arg1, caddr_t arg2);
1086 enum ioc_reply nge_chip_ioctl(nge_t *ngep, mblk_t *mp, struct iocblk *iocp);
1087 
1088 void nge_phys_init(nge_t *ngep);
1089 boolean_t nge_phy_reset(nge_t *ngep);
1090 uint16_t nge_mii_get16(nge_t *ngep, nge_regno_t regno);
1091 void nge_mii_put16(nge_t *ngep, nge_regno_t regno, uint16_t data);
1092 
1093 void nge_recv_recycle(caddr_t arg);
1094 void nge_receive(nge_t *ngep);
1095 
1096 uint_t nge_reschedule(caddr_t args1, caddr_t args2);
1097 mblk_t *nge_m_tx(void *arg, mblk_t *mp);
1098 
1099 void nge_tx_recycle(nge_t *ngep, boolean_t is_intr);
1100 void nge_tx_recycle_all(nge_t *ngep);
1101 
1102 enum ioc_reply nge_nd_ioctl(nge_t *ngep, queue_t *wq,
1103     mblk_t *mp, struct iocblk *iocp);
1104 int nge_nd_init(nge_t *ngep);
1105 void nge_nd_cleanup(nge_t *ngep);
1106 
1107 
1108 void nge_init_kstats(nge_t *ngep, int instance);
1109 void nge_fini_kstats(nge_t *ngep);
1110 int nge_m_stat(void *arg, uint_t stat, uint64_t *val);
1111 
1112 uint32_t nge_atomic_shl32(uint32_t *sp, uint_t count);
1113 
1114 void nge_log(nge_t *ngep, const char *fmt, ...);
1115 void nge_problem(nge_t *ngep, const char *fmt, ...);
1116 void nge_error(nge_t *ngep, const char *fmt, ...);
1117 void
1118 nge_report(nge_t *ngep, uint8_t error_id);
1119 
1120 void (*nge_db(nge_t *ngep))(const char *fmt, ...);
1121 void (*nge_gdb(void))(const char *fmt, ...);
1122 extern	uint32_t nge_debug;
1123 
1124 /*
1125  * DESC MODE 2
1126  */
1127 
1128 extern void nge_sum_rxd_fill(void *, const ddi_dma_cookie_t *, size_t);
1129 extern uint32_t nge_sum_rxd_check(const void *, size_t *);
1130 
1131 extern void nge_sum_txd_fill(void *, const ddi_dma_cookie_t *,
1132 				size_t, uint32_t, boolean_t);
1133 extern uint32_t nge_sum_txd_check(const void *, size_t *);
1134 
1135 /*
1136  * DESC MODE 3
1137  */
1138 
1139 extern void nge_hot_rxd_fill(void *, const ddi_dma_cookie_t *, size_t);
1140 extern uint32_t nge_hot_rxd_check(const void *, size_t *);
1141 
1142 extern void nge_hot_txd_fill(void *, const ddi_dma_cookie_t *,
1143 				size_t, uint32_t, boolean_t);
1144 extern uint32_t nge_hot_txd_check(const void *, size_t *);
1145 
1146 #ifdef __cplusplus
1147 }
1148 #endif
1149 
1150 #endif	/* _SYS_NGE_H */
1151