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