xref: /freebsd/sys/dev/rge/if_rgevar.h (revision 4bf8ce037dc8fa699be87350bb6467f1b74cb96d)
1*4bf8ce03SAdrian Chadd /*-
2*4bf8ce03SAdrian Chadd  * SPDX-License-Identifier: BSD-2-Clause
3*4bf8ce03SAdrian Chadd  *
4*4bf8ce03SAdrian Chadd  * Copyright (c) 2019, 2020, 2025 Kevin Lo <kevlo@openbsd.org>
5*4bf8ce03SAdrian Chadd  *
6*4bf8ce03SAdrian Chadd  * Permission to use, copy, modify, and distribute this software for any
7*4bf8ce03SAdrian Chadd  * purpose with or without fee is hereby granted, provided that the above
8*4bf8ce03SAdrian Chadd  * copyright notice and this permission notice appear in all copies.
9*4bf8ce03SAdrian Chadd  *
10*4bf8ce03SAdrian Chadd  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*4bf8ce03SAdrian Chadd  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*4bf8ce03SAdrian Chadd  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*4bf8ce03SAdrian Chadd  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*4bf8ce03SAdrian Chadd  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*4bf8ce03SAdrian Chadd  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*4bf8ce03SAdrian Chadd  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*4bf8ce03SAdrian Chadd  */
18*4bf8ce03SAdrian Chadd 
19*4bf8ce03SAdrian Chadd /*	$OpenBSD: if_rgereg.h,v 1.15 2025/09/19 00:41:14 kevlo Exp $	*/
20*4bf8ce03SAdrian Chadd 
21*4bf8ce03SAdrian Chadd #ifndef	__IF_RGEVAR_H__
22*4bf8ce03SAdrian Chadd #define	__IF_RGEVAR_H__
23*4bf8ce03SAdrian Chadd 
24*4bf8ce03SAdrian Chadd #define	RGE_LOCK(sc)		(mtx_lock(&sc->sc_mtx))
25*4bf8ce03SAdrian Chadd #define	RGE_UNLOCK(sc)		(mtx_unlock(&sc->sc_mtx))
26*4bf8ce03SAdrian Chadd #define	RGE_ASSERT_LOCKED(sc)	(mtx_assert(&sc->sc_mtx, MA_OWNED))
27*4bf8ce03SAdrian Chadd #define	RGE_ASSERT_UNLOCKED(sc)	(mtx_assert(&sc->sc_mtx, MA_NOTOWNED))
28*4bf8ce03SAdrian Chadd 
29*4bf8ce03SAdrian Chadd enum rge_mac_type {
30*4bf8ce03SAdrian Chadd 	MAC_UNKNOWN = 1,
31*4bf8ce03SAdrian Chadd 	MAC_R25,
32*4bf8ce03SAdrian Chadd 	MAC_R25B,
33*4bf8ce03SAdrian Chadd 	MAC_R25D,
34*4bf8ce03SAdrian Chadd 	MAC_R26,
35*4bf8ce03SAdrian Chadd 	MAC_R27
36*4bf8ce03SAdrian Chadd };
37*4bf8ce03SAdrian Chadd 
38*4bf8ce03SAdrian Chadd struct rge_drv_stats {
39*4bf8ce03SAdrian Chadd 	/* How many times if_transmit() was called */
40*4bf8ce03SAdrian Chadd 	uint64_t		transmit_call_cnt;
41*4bf8ce03SAdrian Chadd 	/* Transmitted frame failed because the interface was stopped */
42*4bf8ce03SAdrian Chadd 	uint64_t		transmit_stopped_cnt;
43*4bf8ce03SAdrian Chadd 	/* Transmitted frame failed because the TX queue is full */
44*4bf8ce03SAdrian Chadd 	uint64_t		transmit_full_cnt;
45*4bf8ce03SAdrian Chadd 	/* How many transmit frames were queued for transmit */
46*4bf8ce03SAdrian Chadd 	uint64_t		transmit_queued_cnt;
47*4bf8ce03SAdrian Chadd 
48*4bf8ce03SAdrian Chadd 	/* How many times the interrupt routine was called */
49*4bf8ce03SAdrian Chadd 	uint64_t		intr_cnt;
50*4bf8ce03SAdrian Chadd 	/* How many times SYSTEM_ERR was set, requiring a hardware reset */
51*4bf8ce03SAdrian Chadd 	uint64_t		intr_system_err_cnt;
52*4bf8ce03SAdrian Chadd 	/* How many times rge_rxeof was called */
53*4bf8ce03SAdrian Chadd 	uint64_t		rxeof_cnt;
54*4bf8ce03SAdrian Chadd 	/* How many times rge_txeof was called */
55*4bf8ce03SAdrian Chadd 	uint64_t		txeof_cnt;
56*4bf8ce03SAdrian Chadd 
57*4bf8ce03SAdrian Chadd 	/* How many times the link state changed */
58*4bf8ce03SAdrian Chadd 	uint64_t		link_state_change_cnt;
59*4bf8ce03SAdrian Chadd 
60*4bf8ce03SAdrian Chadd 	/* How many times tx_task was run */
61*4bf8ce03SAdrian Chadd 	uint64_t		tx_task_cnt;
62*4bf8ce03SAdrian Chadd 
63*4bf8ce03SAdrian Chadd 	/* Count of frames passed up into if_input() */
64*4bf8ce03SAdrian Chadd 	uint64_t		recv_input_cnt;
65*4bf8ce03SAdrian Chadd 
66*4bf8ce03SAdrian Chadd 	/*
67*4bf8ce03SAdrian Chadd 	 * For now - driver doesn't support multi descriptor
68*4bf8ce03SAdrian Chadd 	 * RX frames; so count if it happens so it'll be noticed.
69*4bf8ce03SAdrian Chadd 	 */
70*4bf8ce03SAdrian Chadd 	uint64_t		rx_desc_err_multidesc;
71*4bf8ce03SAdrian Chadd 
72*4bf8ce03SAdrian Chadd 	/*
73*4bf8ce03SAdrian Chadd 	 * Number of TX watchdog timeouts.
74*4bf8ce03SAdrian Chadd 	 */
75*4bf8ce03SAdrian Chadd 	uint64_t		tx_watchdog_timeout_cnt;
76*4bf8ce03SAdrian Chadd 
77*4bf8ce03SAdrian Chadd 	uint64_t		tx_encap_cnt;
78*4bf8ce03SAdrian Chadd 	uint64_t		tx_encap_refrag_cnt;
79*4bf8ce03SAdrian Chadd 	uint64_t		tx_encap_err_toofrag;
80*4bf8ce03SAdrian Chadd 	uint64_t		tx_offload_ip_csum_set;
81*4bf8ce03SAdrian Chadd 	uint64_t		tx_offload_tcp_csum_set;
82*4bf8ce03SAdrian Chadd 	uint64_t		tx_offload_udp_csum_set;
83*4bf8ce03SAdrian Chadd 	uint64_t		tx_offload_vlan_tag_set;
84*4bf8ce03SAdrian Chadd 
85*4bf8ce03SAdrian Chadd 	uint64_t		rx_ether_csum_err;
86*4bf8ce03SAdrian Chadd 	uint64_t		rx_desc_jumbo_frag;
87*4bf8ce03SAdrian Chadd 	uint64_t		rx_offload_vlan_tag;
88*4bf8ce03SAdrian Chadd 	uint64_t		rx_offload_csum_ipv4_exists;
89*4bf8ce03SAdrian Chadd 	uint64_t		rx_offload_csum_ipv4_valid;
90*4bf8ce03SAdrian Chadd 
91*4bf8ce03SAdrian Chadd 	uint64_t		rx_offload_csum_tcp_exists;
92*4bf8ce03SAdrian Chadd 	uint64_t		rx_offload_csum_tcp_valid;
93*4bf8ce03SAdrian Chadd 
94*4bf8ce03SAdrian Chadd 	uint64_t		rx_offload_csum_udp_exists;
95*4bf8ce03SAdrian Chadd 	uint64_t		rx_offload_csum_udp_valid;
96*4bf8ce03SAdrian Chadd };
97*4bf8ce03SAdrian Chadd 
98*4bf8ce03SAdrian Chadd struct rge_txq {
99*4bf8ce03SAdrian Chadd 	struct mbuf		*txq_mbuf;
100*4bf8ce03SAdrian Chadd 	bus_dmamap_t		txq_dmamap;
101*4bf8ce03SAdrian Chadd 	int			txq_descidx;
102*4bf8ce03SAdrian Chadd };
103*4bf8ce03SAdrian Chadd 
104*4bf8ce03SAdrian Chadd struct rge_rxq {
105*4bf8ce03SAdrian Chadd 	struct mbuf		*rxq_mbuf;
106*4bf8ce03SAdrian Chadd 	bus_dmamap_t		rxq_dmamap;
107*4bf8ce03SAdrian Chadd };
108*4bf8ce03SAdrian Chadd 
109*4bf8ce03SAdrian Chadd struct rge_tx {
110*4bf8ce03SAdrian Chadd 	struct rge_txq		rge_txq[RGE_TX_LIST_CNT];
111*4bf8ce03SAdrian Chadd 	int			rge_txq_prodidx;
112*4bf8ce03SAdrian Chadd 	int			rge_txq_considx;
113*4bf8ce03SAdrian Chadd 
114*4bf8ce03SAdrian Chadd 	bus_addr_t		rge_tx_list_paddr;
115*4bf8ce03SAdrian Chadd 	bus_dmamap_t		rge_tx_list_map;
116*4bf8ce03SAdrian Chadd 	struct rge_tx_desc	*rge_tx_list;
117*4bf8ce03SAdrian Chadd };
118*4bf8ce03SAdrian Chadd 
119*4bf8ce03SAdrian Chadd struct rge_rx {
120*4bf8ce03SAdrian Chadd 	struct rge_rxq		rge_rxq[RGE_RX_LIST_CNT];
121*4bf8ce03SAdrian Chadd 	int			rge_rxq_prodidx;
122*4bf8ce03SAdrian Chadd 	int			rge_rxq_considx;
123*4bf8ce03SAdrian Chadd 
124*4bf8ce03SAdrian Chadd //	struct if_rxring	rge_rx_ring;
125*4bf8ce03SAdrian Chadd 	bus_addr_t		rge_rx_list_paddr;
126*4bf8ce03SAdrian Chadd 	bus_dmamap_t		rge_rx_list_map;
127*4bf8ce03SAdrian Chadd 	struct rge_rx_desc	*rge_rx_list;
128*4bf8ce03SAdrian Chadd 
129*4bf8ce03SAdrian Chadd 	struct mbuf		*rge_head;
130*4bf8ce03SAdrian Chadd 	struct mbuf		**rge_tail;
131*4bf8ce03SAdrian Chadd };
132*4bf8ce03SAdrian Chadd 
133*4bf8ce03SAdrian Chadd struct rge_queues {
134*4bf8ce03SAdrian Chadd 	struct rge_softc	*q_sc;
135*4bf8ce03SAdrian Chadd 	void			*q_ihc;
136*4bf8ce03SAdrian Chadd 	int			q_index;
137*4bf8ce03SAdrian Chadd 	char			q_name[16];
138*4bf8ce03SAdrian Chadd //	pci_intr_handle_t	q_ih;
139*4bf8ce03SAdrian Chadd 	struct rge_tx		q_tx;
140*4bf8ce03SAdrian Chadd 	struct rge_rx		q_rx;
141*4bf8ce03SAdrian Chadd };
142*4bf8ce03SAdrian Chadd 
143*4bf8ce03SAdrian Chadd struct rge_mac_stats {
144*4bf8ce03SAdrian Chadd 	bus_addr_t		paddr;
145*4bf8ce03SAdrian Chadd 	bus_dmamap_t		map;
146*4bf8ce03SAdrian Chadd 	/* NIC dma buffer, NIC order */
147*4bf8ce03SAdrian Chadd 	struct rge_hw_mac_stats	*stats;
148*4bf8ce03SAdrian Chadd 
149*4bf8ce03SAdrian Chadd 	/* Local copy for retrieval, host order */
150*4bf8ce03SAdrian Chadd 	struct rge_hw_mac_stats	lcl_stats;
151*4bf8ce03SAdrian Chadd };
152*4bf8ce03SAdrian Chadd 
153*4bf8ce03SAdrian Chadd struct rge_softc {
154*4bf8ce03SAdrian Chadd 	device_t		sc_dev;
155*4bf8ce03SAdrian Chadd 	if_t			sc_ifp;		/* Ethernet common data */
156*4bf8ce03SAdrian Chadd 	bool			sc_ether_attached;
157*4bf8ce03SAdrian Chadd 	struct mtx		sc_mtx;
158*4bf8ce03SAdrian Chadd 	struct resource		*sc_irq[RGE_MSI_MESSAGES];
159*4bf8ce03SAdrian Chadd 	void			*sc_ih[RGE_MSI_MESSAGES];
160*4bf8ce03SAdrian Chadd 	uint32_t		sc_expcap;	/* PCe exp cap */
161*4bf8ce03SAdrian Chadd 	struct resource		*sc_bres;	/* bus space MMIO/IOPORT resource */
162*4bf8ce03SAdrian Chadd 	bus_space_handle_t	rge_bhandle;	/* bus space handle */
163*4bf8ce03SAdrian Chadd 	bus_space_tag_t		rge_btag;	/* bus space tag */
164*4bf8ce03SAdrian Chadd 	bus_size_t		rge_bsize;
165*4bf8ce03SAdrian Chadd 	bus_dma_tag_t		sc_dmat;
166*4bf8ce03SAdrian Chadd 	bus_dma_tag_t		sc_dmat_tx_desc;
167*4bf8ce03SAdrian Chadd 	bus_dma_tag_t		sc_dmat_tx_buf;
168*4bf8ce03SAdrian Chadd 	bus_dma_tag_t		sc_dmat_rx_desc;
169*4bf8ce03SAdrian Chadd 	bus_dma_tag_t		sc_dmat_rx_buf;
170*4bf8ce03SAdrian Chadd 	bus_dma_tag_t		sc_dmat_stats_buf;
171*4bf8ce03SAdrian Chadd 
172*4bf8ce03SAdrian Chadd //	pci_chipset_tag_t	sc_pc;
173*4bf8ce03SAdrian Chadd //	pcitag_t		sc_tag;
174*4bf8ce03SAdrian Chadd 	struct ifmedia		sc_media;	/* media info */
175*4bf8ce03SAdrian Chadd 	enum rge_mac_type	rge_type;
176*4bf8ce03SAdrian Chadd 
177*4bf8ce03SAdrian Chadd 	struct rge_queues	*sc_queues;
178*4bf8ce03SAdrian Chadd 	unsigned int		sc_nqueues;
179*4bf8ce03SAdrian Chadd 
180*4bf8ce03SAdrian Chadd 	bool			sc_detaching;
181*4bf8ce03SAdrian Chadd 	bool			sc_stopped;
182*4bf8ce03SAdrian Chadd 	bool			sc_suspended;
183*4bf8ce03SAdrian Chadd 
184*4bf8ce03SAdrian Chadd 	/* Note: these likely should be per-TXQ */
185*4bf8ce03SAdrian Chadd 	struct mbufq		sc_txq;
186*4bf8ce03SAdrian Chadd 	struct taskqueue *	sc_tq;
187*4bf8ce03SAdrian Chadd 	char			sc_tq_name[32];
188*4bf8ce03SAdrian Chadd 	char			sc_tq_thr_name[32];
189*4bf8ce03SAdrian Chadd 	struct task		sc_tx_task;
190*4bf8ce03SAdrian Chadd 
191*4bf8ce03SAdrian Chadd 	struct callout		sc_timeout;	/* 1 second tick */
192*4bf8ce03SAdrian Chadd 
193*4bf8ce03SAdrian Chadd 	uint64_t		rge_mcodever;
194*4bf8ce03SAdrian Chadd 	uint16_t		rge_rcodever;
195*4bf8ce03SAdrian Chadd 	uint32_t		rge_flags;
196*4bf8ce03SAdrian Chadd #define RGE_FLAG_MSI		0x00000001
197*4bf8ce03SAdrian Chadd #define RGE_FLAG_PCIE		0x00000002
198*4bf8ce03SAdrian Chadd 
199*4bf8ce03SAdrian Chadd 	uint32_t		rge_intrs;
200*4bf8ce03SAdrian Chadd 	int			rge_timerintr;
201*4bf8ce03SAdrian Chadd #define RGE_IMTYPE_NONE		0
202*4bf8ce03SAdrian Chadd #define RGE_IMTYPE_SIM		1
203*4bf8ce03SAdrian Chadd 	int			sc_watchdog;
204*4bf8ce03SAdrian Chadd 
205*4bf8ce03SAdrian Chadd 	uint32_t		sc_debug;
206*4bf8ce03SAdrian Chadd 
207*4bf8ce03SAdrian Chadd 	struct rge_drv_stats	sc_drv_stats;
208*4bf8ce03SAdrian Chadd 
209*4bf8ce03SAdrian Chadd 	struct rge_mac_stats	sc_mac_stats;
210*4bf8ce03SAdrian Chadd };
211*4bf8ce03SAdrian Chadd 
212*4bf8ce03SAdrian Chadd /*
213*4bf8ce03SAdrian Chadd  * Register space access macros.
214*4bf8ce03SAdrian Chadd  */
215*4bf8ce03SAdrian Chadd #define RGE_WRITE_4(sc, reg, val)	\
216*4bf8ce03SAdrian Chadd 	bus_space_write_4(sc->rge_btag, sc->rge_bhandle, reg, val)
217*4bf8ce03SAdrian Chadd #define RGE_WRITE_2(sc, reg, val)	\
218*4bf8ce03SAdrian Chadd 	bus_space_write_2(sc->rge_btag, sc->rge_bhandle, reg, val)
219*4bf8ce03SAdrian Chadd #define RGE_WRITE_1(sc, reg, val)	\
220*4bf8ce03SAdrian Chadd 	bus_space_write_1(sc->rge_btag, sc->rge_bhandle, reg, val)
221*4bf8ce03SAdrian Chadd 
222*4bf8ce03SAdrian Chadd #define	RGE_WRITE_BARRIER_4(sc, reg)					\
223*4bf8ce03SAdrian Chadd 	bus_space_barrier(sc->rge_btag, sc->rge_bhandle, reg, 4,	\
224*4bf8ce03SAdrian Chadd 	    BUS_SPACE_BARRIER_WRITE)
225*4bf8ce03SAdrian Chadd #define	RGE_READ_BARRIER_4(sc, reg)					\
226*4bf8ce03SAdrian Chadd 	bus_space_barrier(sc->rge_btag, sc->rge_bhandle, reg, 4,	\
227*4bf8ce03SAdrian Chadd 	    BUS_SPACE_BARRIER_READ)
228*4bf8ce03SAdrian Chadd 
229*4bf8ce03SAdrian Chadd 
230*4bf8ce03SAdrian Chadd #define RGE_READ_4(sc, reg)		\
231*4bf8ce03SAdrian Chadd 	bus_space_read_4(sc->rge_btag, sc->rge_bhandle, reg)
232*4bf8ce03SAdrian Chadd #define RGE_READ_2(sc, reg)		\
233*4bf8ce03SAdrian Chadd 	bus_space_read_2(sc->rge_btag, sc->rge_bhandle, reg)
234*4bf8ce03SAdrian Chadd #define RGE_READ_1(sc, reg)		\
235*4bf8ce03SAdrian Chadd 	bus_space_read_1(sc->rge_btag, sc->rge_bhandle, reg)
236*4bf8ce03SAdrian Chadd 
237*4bf8ce03SAdrian Chadd #define RGE_SETBIT_4(sc, reg, val)	\
238*4bf8ce03SAdrian Chadd 	RGE_WRITE_4(sc, reg, RGE_READ_4(sc, reg) | (val))
239*4bf8ce03SAdrian Chadd #define RGE_SETBIT_2(sc, reg, val)	\
240*4bf8ce03SAdrian Chadd 	RGE_WRITE_2(sc, reg, RGE_READ_2(sc, reg) | (val))
241*4bf8ce03SAdrian Chadd #define RGE_SETBIT_1(sc, reg, val)	\
242*4bf8ce03SAdrian Chadd 	RGE_WRITE_1(sc, reg, RGE_READ_1(sc, reg) | (val))
243*4bf8ce03SAdrian Chadd 
244*4bf8ce03SAdrian Chadd #define RGE_CLRBIT_4(sc, reg, val)	\
245*4bf8ce03SAdrian Chadd 	RGE_WRITE_4(sc, reg, RGE_READ_4(sc, reg) & ~(val))
246*4bf8ce03SAdrian Chadd #define RGE_CLRBIT_2(sc, reg, val)	\
247*4bf8ce03SAdrian Chadd 	RGE_WRITE_2(sc, reg, RGE_READ_2(sc, reg) & ~(val))
248*4bf8ce03SAdrian Chadd #define RGE_CLRBIT_1(sc, reg, val)	\
249*4bf8ce03SAdrian Chadd 	RGE_WRITE_1(sc, reg, RGE_READ_1(sc, reg) & ~(val))
250*4bf8ce03SAdrian Chadd 
251*4bf8ce03SAdrian Chadd #define RGE_EPHY_SETBIT(sc, reg, val)	\
252*4bf8ce03SAdrian Chadd 	rge_write_ephy(sc, reg, rge_read_ephy(sc, reg) | (val))
253*4bf8ce03SAdrian Chadd 
254*4bf8ce03SAdrian Chadd #define RGE_EPHY_CLRBIT(sc, reg, val)	\
255*4bf8ce03SAdrian Chadd 	rge_write_ephy(sc, reg, rge_read_ephy(sc, reg) & ~(val))
256*4bf8ce03SAdrian Chadd 
257*4bf8ce03SAdrian Chadd #define RGE_PHY_SETBIT(sc, reg, val)	\
258*4bf8ce03SAdrian Chadd 	rge_write_phy_ocp(sc, reg, rge_read_phy_ocp(sc, reg) | (val))
259*4bf8ce03SAdrian Chadd 
260*4bf8ce03SAdrian Chadd #define RGE_PHY_CLRBIT(sc, reg, val)	\
261*4bf8ce03SAdrian Chadd 	rge_write_phy_ocp(sc, reg, rge_read_phy_ocp(sc, reg) & ~(val))
262*4bf8ce03SAdrian Chadd 
263*4bf8ce03SAdrian Chadd #define RGE_MAC_SETBIT(sc, reg, val)	\
264*4bf8ce03SAdrian Chadd 	rge_write_mac_ocp(sc, reg, rge_read_mac_ocp(sc, reg) | (val))
265*4bf8ce03SAdrian Chadd 
266*4bf8ce03SAdrian Chadd #define RGE_MAC_CLRBIT(sc, reg, val)	\
267*4bf8ce03SAdrian Chadd 	rge_write_mac_ocp(sc, reg, rge_read_mac_ocp(sc, reg) & ~(val))
268*4bf8ce03SAdrian Chadd 
269*4bf8ce03SAdrian Chadd #endif	/* __IF_RGEVAR_H__ */
270