xref: /illumos-gate/usr/src/uts/common/io/usbgem/usbgem.h (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
1 /*
2  * usbgem.h: General USB to Ethernet MAC driver framework
3  * @(#)usbgem.h	1.4 12/02/09
4  * (C) Copyright 2003-2009 Masayuki Murayama KHF04453@nifty.ne.jp
5  */
6 
7 #ifndef __USBGEM_H__
8 #define	__USBGEM_H__
9 
10 #include <sys/mac.h>
11 #ifndef MAC_VERSION
12 #include <sys/mac_provider.h>
13 #endif
14 #include <sys/mac_ether.h>
15 
16 /*
17  * Useful macros and typedefs
18  */
19 #define	USBGEM_NAME_LEN	32
20 
21 #define	USBGEM_TX_TIMEOUT		(drv_usectohz(3*1000000))
22 #define	USBGEM_TX_TIMEOUT_INTERVAL	(drv_usectohz(1*1000000))
23 #define	USBGEM_LINK_WATCH_INTERVAL	(drv_usectohz(1*1000000))
24 
25 /* general return code */
26 #define	USBGEM_SUCCESS	0
27 #define	USBGEM_FAILURE	1
28 
29 /* return code of usbgem_tx_done */
30 #define	INTR_RESTART_TX	0x80000000U
31 
32 struct usbgem_stats {
33 	uint32_t	intr;
34 
35 	uint32_t	crc;
36 	uint32_t	errrcv;
37 	uint32_t	overflow;
38 	uint32_t	frame;
39 	uint32_t	missed;
40 	uint32_t	runt;
41 	uint32_t	frame_too_long;
42 	uint32_t	norcvbuf;
43 	uint32_t	sqe;
44 
45 	uint32_t	collisions;
46 	uint32_t	first_coll;
47 	uint32_t	multi_coll;
48 	uint32_t	excoll;
49 	uint32_t	xmit_internal_err;
50 	uint32_t	nocarrier;
51 	uint32_t	defer;
52 	uint32_t	errxmt;
53 	uint32_t	underflow;
54 	uint32_t	xmtlatecoll;
55 	uint32_t	noxmtbuf;
56 	uint32_t	jabber;
57 
58 
59 	uint64_t	rbytes;
60 	uint64_t	obytes;
61 	uint64_t	rpackets;
62 	uint64_t	opackets;
63 	uint32_t	rbcast;
64 	uint32_t	obcast;
65 	uint32_t	rmcast;
66 	uint32_t	omcast;
67 	uint32_t	rcv_internal_err;
68 };
69 
70 struct mcast_addr {
71 	struct ether_addr	addr;
72 	uint32_t		hash;
73 };
74 
75 #define	USBGEM_MAXMC	64
76 #define	USBGEM_MCALLOC	(sizeof (struct mcast_addr) * USBGEM_MAXMC)
77 
78 #define	SLOT(dp, n)	((n) % (dp)->ugc.usbgc_tx_list_max)
79 
80 /*
81  * mac soft state
82  */
83 struct usbgem_dev {
84 	dev_info_t	*dip;
85 	mac_handle_t	mh;
86 	char		name[USBGEM_NAME_LEN];
87 
88 	/* pointer to usb private data */
89 	usb_client_dev_data_t	*reg_data;
90 
91 	/* usb handles */
92 	usb_pipe_handle_t	default_pipe;
93 	usb_pipe_handle_t	bulkin_pipe;
94 	usb_pipe_handle_t	bulkout_pipe;
95 	usb_pipe_handle_t	intr_pipe;
96 
97 	/* usb endpoints */
98 	usb_ep_descr_t		*ep_default;
99 	usb_ep_descr_t		*ep_bulkin;
100 	usb_ep_descr_t		*ep_bulkout;
101 	usb_ep_descr_t		*ep_intr;
102 
103 	/* usb policies */
104 	usb_pipe_policy_t	policy_default;
105 	usb_pipe_policy_t	policy_bulkin;
106 	usb_pipe_policy_t	policy_bulkout;
107 	usb_pipe_policy_t	policy_interrupt;
108 
109 	/* MAC address information */
110 	struct ether_addr	cur_addr;
111 	struct ether_addr	dev_addr;
112 
113 	/* RX state and resource management */
114 	kmutex_t		rxlock;
115 	int			rx_busy_cnt;
116 	boolean_t		rx_active;
117 	kcondvar_t		rx_drain_cv;
118 
119 	/* RX buffer management */
120 	int			rx_buf_len;
121 
122 	/* TX state and resource management */
123 	kmutex_t		txlock;
124 	int			tx_busy_cnt;
125 	usb_bulk_req_t		*tx_free_list;
126 	kcondvar_t		tx_drain_cv;
127 	clock_t			tx_start_time;
128 	int			bulkout_timeout;	/* in second */
129 	int			tx_max_packets;
130 	int			tx_seq_num;
131 	int			tx_intr_pended;
132 
133 	/* NIC state from OS view */
134 	int			nic_state;
135 #define	NIC_STATE_UNKNOWN	0
136 #define	NIC_STATE_STOPPED	1
137 #define	NIC_STATE_INITIALIZED	2
138 #define	NIC_STATE_ONLINE	3
139 
140 	/* MAC state from hardware view */
141 	int			mac_state;
142 #define	MAC_STATE_DISCONNECTED	0	/* it includes suspended state too */
143 #define	MAC_STATE_STOPPED	1	/* powered up / buf not initialized */
144 #define	MAC_STATE_INITIALIZED	2	/* initialized */
145 #define	MAC_STATE_ONLINE	3	/* working correctly  */
146 #define	MAC_STATE_ERROR		4	/* need to restart nic */
147 
148 	clock_t			fatal_error;
149 
150 	/* robustness: timer and watchdog */
151 	uint_t			tx_watcher_stop;
152 	kt_did_t		tx_watcher_did;
153 	kcondvar_t		tx_watcher_cv;
154 	kmutex_t		tx_watcher_lock;
155 	clock_t			tx_watcher_timeout;
156 	clock_t			tx_watcher_interval;
157 
158 	/* MII mamagement */
159 	boolean_t		anadv_autoneg:1;
160 	boolean_t		anadv_1000fdx:1;
161 	boolean_t		anadv_1000hdx:1;
162 	boolean_t		anadv_100t4:1;
163 	boolean_t		anadv_100fdx:1;
164 	boolean_t		anadv_100hdx:1;
165 	boolean_t		anadv_10fdx:1;
166 	boolean_t		anadv_10hdx:1;
167 	boolean_t		anadv_1000t_ms:2;
168 	boolean_t		anadv_pause:1;
169 	boolean_t		anadv_asmpause:1;
170 	boolean_t		mii_advert_ro:1;
171 
172 	boolean_t		full_duplex:1;
173 	int			speed:3;
174 #define		USBGEM_SPD_10	0
175 #define		USBGEM_SPD_100	1
176 #define		USBGEM_SPD_1000	2
177 #define		USBGEM_SPD_NUM	3
178 	unsigned int		flow_control:2;
179 #define		FLOW_CONTROL_NONE	0
180 #define		FLOW_CONTROL_SYMMETRIC	1
181 #define		FLOW_CONTROL_TX_PAUSE	2
182 #define		FLOW_CONTROL_RX_PAUSE	3
183 
184 	boolean_t		mii_supress_msg:1;
185 
186 	uint32_t		mii_phy_id;
187 	uint16_t		mii_status;
188 	uint16_t		mii_advert;
189 	uint16_t		mii_lpable;
190 	uint16_t		mii_exp;
191 	uint16_t		mii_ctl1000;
192 	uint16_t		mii_stat1000;
193 	uint16_t		mii_xstatus;
194 	int8_t			mii_phy_addr;	/* must be signed */
195 
196 	uint16_t		mii_status_ro;
197 	uint16_t		mii_xstatus_ro;
198 
199 	int			mii_state;
200 #define		MII_STATE_UNKNOWN		0
201 #define		MII_STATE_RESETTING		1
202 #define		MII_STATE_AUTONEGOTIATING	2
203 #define		MII_STATE_AN_DONE		3
204 #define		MII_STATE_MEDIA_SETUP		4
205 #define		MII_STATE_LINKUP		5
206 #define		MII_STATE_LINKDOWN		6
207 
208 	clock_t			mii_last_check;	/* in tick */
209 	clock_t			mii_timer;	/* in tick */
210 #define		MII_RESET_TIMEOUT	drv_usectohz(1000*1000)
211 #define		MII_AN_TIMEOUT		drv_usectohz(5000*1000)
212 #define		MII_LINKDOWN_TIMEOUT	drv_usectohz(10000*1000)
213 
214 	clock_t			mii_interval;	/* in tick */
215 	clock_t			linkup_delay;	/* in tick */
216 
217 	uint_t			link_watcher_stop;
218 	kt_did_t		link_watcher_did;
219 	kcondvar_t		link_watcher_wait_cv;
220 	kmutex_t		link_watcher_lock;
221 
222 	krwlock_t		dev_state_lock;	/* mac_state and nic_state */
223 	ksema_t			hal_op_lock;	/* serialize hw operations */
224 	ksema_t			drv_op_lock;	/* hotplug op lock */
225 
226 	/* multcast list */
227 	ksema_t			rxfilter_lock;
228 	int			mc_count;
229 	int			mc_count_req;
230 	struct mcast_addr	*mc_list;
231 	int			rxmode;
232 #define		RXMODE_PROMISC		0x01
233 #define		RXMODE_ALLMULTI_REQ	0x02
234 #define		RXMODE_MULTI_OVF	0x04
235 #define		RXMODE_ENABLE		0x08
236 #define		RXMODE_ALLMULTI		(RXMODE_ALLMULTI_REQ | RXMODE_MULTI_OVF)
237 #define		RXMODE_BITS	\
238 			"\020"	\
239 			"\004ENABLE"	\
240 			"\003MULTI_OVF"	\
241 			"\002ALLMULTI_REQ"	\
242 			"\001PROMISC"
243 
244 	/* statistcs */
245 	struct usbgem_stats		stats;
246 
247 	/* pointer to local structure */
248 	void			*private;
249 	int			priv_size;
250 
251 	/* configuration */
252 	struct usbgem_conf {
253 		/* name */
254 		char		usbgc_name[USBGEM_NAME_LEN];
255 		int		usbgc_ppa;
256 
257 		/* specification on usb */
258 		int	usbgc_ifnum;	/* interface number */
259 		int	usbgc_alt;	/* alternate */
260 
261 		/* specification on tx engine */
262 		int		usbgc_tx_list_max;
263 
264 		/* specification on rx engine */
265 		int		usbgc_rx_header_len;
266 		int		usbgc_rx_list_max;
267 
268 		/* time out parameters */
269 		clock_t		usbgc_tx_timeout;
270 		clock_t		usbgc_tx_timeout_interval;
271 
272 		/* flow control */
273 		int		usbgc_flow_control;
274 
275 		/* MII timeout parameters */
276 		clock_t	usbgc_mii_linkdown_timeout;
277 		clock_t	usbgc_mii_link_watch_interval;
278 		clock_t	usbgc_mii_reset_timeout;
279 
280 		clock_t	usbgc_mii_an_watch_interval;
281 		clock_t	usbgc_mii_an_timeout;
282 		clock_t	usbgc_mii_an_wait;
283 		clock_t	usbgc_mii_an_delay;
284 
285 		/* MII configuration */
286 		int	usbgc_mii_addr_min;
287 		int	usbgc_mii_linkdown_action;
288 		int	usbgc_mii_linkdown_timeout_action;
289 #define		MII_ACTION_NONE		0
290 #define		MII_ACTION_RESET	1
291 #define		MII_ACTION_RSA		2
292 		boolean_t	usbgc_mii_dont_reset:1;
293 		boolean_t	usbgc_mii_an_oneshot:1;
294 		boolean_t	usbgc_mii_hw_link_detection:1;
295 		boolean_t	usbgc_mii_stop_mac_on_linkdown:1;
296 		uint16_t	usbgc_mii_an_cmd;
297 
298 		/* I/O methods */
299 
300 		/* mac operation */
301 		int	(*usbgc_attach_chip)(struct usbgem_dev *dp);
302 		int	(*usbgc_reset_chip)(struct usbgem_dev *dp);
303 		int	(*usbgc_init_chip)(struct usbgem_dev *dp);
304 		int	(*usbgc_start_chip)(struct usbgem_dev *dp);
305 		int	(*usbgc_stop_chip)(struct usbgem_dev *dp);
306 		uint32_t (*usbgc_multicast_hash)(struct usbgem_dev *dp,
307 		    const uint8_t *);
308 		int	(*usbgc_set_rx_filter)(struct usbgem_dev *dp);
309 		int	(*usbgc_set_media)(struct usbgem_dev *dp);
310 		int	(*usbgc_get_stats)(struct usbgem_dev *dp);
311 		void	(*usbgc_interrupt)(struct usbgem_dev *dp, mblk_t *mp);
312 
313 		/* packet manipulation */
314 		mblk_t	*(*usbgc_tx_make_packet)(struct usbgem_dev *dp,
315 		    mblk_t *mp);
316 		mblk_t	*(*usbgc_rx_make_packet)(struct usbgem_dev *dp,
317 		    mblk_t *mp);
318 		/* mii operations */
319 		int	(*usbgc_mii_probe)(struct usbgem_dev *dp);
320 		int	(*usbgc_mii_init)(struct usbgem_dev *dp);
321 		int	(*usbgc_mii_config)(struct usbgem_dev *dp, int *errp);
322 		uint16_t (*usbgc_mii_read)(struct usbgem_dev *dp, uint_t reg,
323 		    int *errp);
324 		void	(*usbgc_mii_write)(struct usbgem_dev *dp, uint_t reg,
325 		    uint16_t val, int *errp);
326 
327 		/* jumbo frame */
328 		int	usbgc_max_mtu;
329 		int	usbgc_default_mtu;
330 		int	usbgc_min_mtu;
331 	} ugc;
332 
333 	int	misc_flag;
334 #define	USBGEM_VLAN	0x0001
335 	timeout_id_t	intr_watcher_id;
336 
337 	/* buffer size */
338 	uint_t	mtu;
339 
340 	/* performance tuning parameters */
341 	uint_t	txthr;		/* tx fifo threshoold */
342 	uint_t	txmaxdma;	/* tx max dma burst size */
343 	uint_t	rxthr;		/* rx fifo threshoold */
344 	uint_t	rxmaxdma;	/* tx max dma burst size */
345 
346 	/* kstat stuff */
347 	kstat_t	*ksp;
348 
349 	/* ndd stuff */
350 	caddr_t	nd_data_p;
351 	caddr_t	nd_arg_p;
352 
353 #ifdef USBGEM_DEBUG_LEVEL
354 	int	tx_cnt;
355 #endif
356 };
357 
358 /*
359  * Exported functions
360  */
361 int usbgem_ctrl_out(struct usbgem_dev *dp,
362     uint8_t reqt, uint8_t req, uint16_t val, uint16_t ix, uint16_t len,
363     void *bp, int size);
364 
365 int usbgem_ctrl_in(struct usbgem_dev *dp,
366     uint8_t reqt, uint8_t req, uint16_t val, uint16_t ix, uint16_t len,
367     void *bp, int size);
368 
369 int usbgem_ctrl_out_val(struct usbgem_dev *dp,
370     uint8_t reqt, uint8_t req, uint16_t val, uint16_t ix, uint16_t len,
371     uint32_t v);
372 
373 int usbgem_ctrl_in_val(struct usbgem_dev *dp,
374     uint8_t reqt, uint8_t req, uint16_t val, uint16_t ix, uint16_t len,
375     void *valp);
376 
377 void usbgem_generate_macaddr(struct usbgem_dev *, uint8_t *);
378 boolean_t usbgem_get_mac_addr_conf(struct usbgem_dev *);
379 int usbgem_mii_probe_default(struct usbgem_dev *);
380 int usbgem_mii_init_default(struct usbgem_dev *);
381 int usbgem_mii_config_default(struct usbgem_dev *, int *errp);
382 void usbgem_mii_update_link(struct usbgem_dev *);
383 void usbgem_restart_tx(struct usbgem_dev *);
384 boolean_t usbgem_tx_done(struct usbgem_dev *, int);
385 void usbgem_receive(struct usbgem_dev *);
386 struct usbgem_dev *usbgem_do_attach(dev_info_t *,
387     struct usbgem_conf *, void *, int);
388 int usbgem_do_detach(dev_info_t *);
389 
390 uint32_t usbgem_ether_crc_le(const uint8_t *addr);
391 uint32_t usbgem_ether_crc_be(const uint8_t *addr);
392 
393 int usbgem_resume(dev_info_t *);
394 int usbgem_suspend(dev_info_t *);
395 int usbgem_quiesce(dev_info_t *);
396 
397 #define	USBGEM_STREAM_OPS(dev_ops, attach, detach) \
398     DDI_DEFINE_STREAM_OPS(dev_ops, nulldev, nulldev, attach, detach, \
399     nodev, NULL, D_MP, NULL, usbgem_quiesce)
400 
401 int usbgem_mod_init(struct dev_ops *, char *);
402 void usbgem_mod_fini(struct dev_ops *);
403 
404 #define	USBGEM_GET_DEV(dip) \
405 	((struct usbgem_dev *)(ddi_get_driver_private(dip)))
406 
407 #endif /* __USBGEM_H__ */
408