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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * EHCI Host Controller Driver (EHCI)
30  *
31  * The EHCI driver is a software driver which interfaces to the Universal
32  * Serial Bus layer (USBA) and the Host Controller (HC). The interface to
33  * the Host Controller is defined by the EHCI Host Controller Interface.
34  *
35  * This module contains the main EHCI driver code which handles all USB
36  * transfers, bandwidth allocations and other general functionalities.
37  */
38 
39 #include <sys/usb/hcd/ehci/ehcid.h>
40 #include <sys/usb/hcd/ehci/ehci_isoch.h>
41 #include <sys/usb/hcd/ehci/ehci_xfer.h>
42 
43 /*
44  * EHCI MSI tunable:
45  *
46  * By default MSI is enabled on all supported platforms except for the
47  * EHCI controller of ULI1575 South bridge.
48  */
49 boolean_t ehci_enable_msi = B_TRUE;
50 
51 /* Pointer to the state structure */
52 extern void *ehci_statep;
53 
54 extern void ehci_handle_endpoint_reclaimation(ehci_state_t *);
55 
56 extern uint_t ehci_vt62x2_workaround;
57 
58 /* Adjustable variables for the size of the pools */
59 int ehci_qh_pool_size = EHCI_QH_POOL_SIZE;
60 int ehci_qtd_pool_size = EHCI_QTD_POOL_SIZE;
61 
62 /*
63  * Initialize the values which the order of 32ms intr qh are executed
64  * by the host controller in the lattice tree.
65  */
66 static uchar_t ehci_index[EHCI_NUM_INTR_QH_LISTS] =
67 	{0x00, 0x10, 0x08, 0x18,
68 	0x04, 0x14, 0x0c, 0x1c,
69 	0x02, 0x12, 0x0a, 0x1a,
70 	0x06, 0x16, 0x0e, 0x1e,
71 	0x01, 0x11, 0x09, 0x19,
72 	0x05, 0x15, 0x0d, 0x1d,
73 	0x03, 0x13, 0x0b, 0x1b,
74 	0x07, 0x17, 0x0f, 0x1f};
75 
76 /*
77  * Initialize the values which are used to calculate start split mask
78  * for the low/full/high speed interrupt and isochronous endpoints.
79  */
80 static uint_t ehci_start_split_mask[15] = {
81 		/*
82 		 * For high/full/low speed usb devices. For high speed
83 		 * device with polling interval greater than or equal
84 		 * to 8us (125us).
85 		 */
86 		0x01,	/* 00000001 */
87 		0x02,	/* 00000010 */
88 		0x04,	/* 00000100 */
89 		0x08,	/* 00001000 */
90 		0x10,	/* 00010000 */
91 		0x20,	/* 00100000 */
92 		0x40,	/* 01000000 */
93 		0x80,	/* 10000000 */
94 
95 		/* Only for high speed devices with polling interval 4us */
96 		0x11,	/* 00010001 */
97 		0x22,	/* 00100010 */
98 		0x44,	/* 01000100 */
99 		0x88,	/* 10001000 */
100 
101 		/* Only for high speed devices with polling interval 2us */
102 		0x55,	/* 01010101 */
103 		0xaa,	/* 10101010 */
104 
105 		/* Only for high speed devices with polling interval 1us */
106 		0xff	/* 11111111 */
107 };
108 
109 /*
110  * Initialize the values which are used to calculate complete split mask
111  * for the low/full speed interrupt and isochronous endpoints.
112  */
113 static uint_t ehci_intr_complete_split_mask[7] = {
114 		/* Only full/low speed devices */
115 		0x1c,	/* 00011100 */
116 		0x38,	/* 00111000 */
117 		0x70,	/* 01110000 */
118 		0xe0,	/* 11100000 */
119 		0x00,	/* Need FSTN feature */
120 		0x00,	/* Need FSTN feature */
121 		0x00	/* Need FSTN feature */
122 };
123 
124 
125 /*
126  * EHCI Internal Function Prototypes
127  */
128 
129 /* Host Controller Driver (HCD) initialization functions */
130 void		ehci_set_dma_attributes(ehci_state_t	*ehcip);
131 int		ehci_allocate_pools(ehci_state_t	*ehcip);
132 void		ehci_decode_ddi_dma_addr_bind_handle_result(
133 				ehci_state_t		*ehcip,
134 				int			result);
135 int		ehci_map_regs(ehci_state_t		*ehcip);
136 int		ehci_register_intrs_and_init_mutex(
137 				ehci_state_t		*ehcip);
138 static int	ehci_add_intrs(ehci_state_t		*ehcip,
139 				int			intr_type);
140 int		ehci_init_ctlr(ehci_state_t		*ehcip,
141 				int			init_type);
142 static int	ehci_take_control(ehci_state_t		*ehcip);
143 static int	ehci_init_periodic_frame_lst_table(
144 				ehci_state_t		*ehcip);
145 static void	ehci_build_interrupt_lattice(
146 				ehci_state_t		*ehcip);
147 usba_hcdi_ops_t *ehci_alloc_hcdi_ops(ehci_state_t	*ehcip);
148 
149 /* Host Controller Driver (HCD) deinitialization functions */
150 int		ehci_cleanup(ehci_state_t		*ehcip);
151 static void	ehci_rem_intrs(ehci_state_t		*ehcip);
152 int		ehci_cpr_suspend(ehci_state_t		*ehcip);
153 int		ehci_cpr_resume(ehci_state_t		*ehcip);
154 
155 /* Bandwidth Allocation functions */
156 int		ehci_allocate_bandwidth(ehci_state_t	*ehcip,
157 				usba_pipe_handle_data_t	*ph,
158 				uint_t			*pnode,
159 				uchar_t			*smask,
160 				uchar_t			*cmask);
161 static int	ehci_allocate_high_speed_bandwidth(
162 				ehci_state_t		*ehcip,
163 				usba_pipe_handle_data_t	*ph,
164 				uint_t			*hnode,
165 				uchar_t			*smask,
166 				uchar_t			*cmask);
167 static int	ehci_allocate_classic_tt_bandwidth(
168 				ehci_state_t		*ehcip,
169 				usba_pipe_handle_data_t	*ph,
170 				uint_t			pnode);
171 void		ehci_deallocate_bandwidth(ehci_state_t	*ehcip,
172 				usba_pipe_handle_data_t	*ph,
173 				uint_t			pnode,
174 				uchar_t			smask,
175 				uchar_t			cmask);
176 static void	ehci_deallocate_high_speed_bandwidth(
177 				ehci_state_t		*ehcip,
178 				usba_pipe_handle_data_t	*ph,
179 				uint_t			hnode,
180 				uchar_t			smask,
181 				uchar_t			cmask);
182 static void	ehci_deallocate_classic_tt_bandwidth(
183 				ehci_state_t		*ehcip,
184 				usba_pipe_handle_data_t	*ph,
185 				uint_t			pnode);
186 static int	ehci_compute_high_speed_bandwidth(
187 				ehci_state_t		*ehcip,
188 				usb_ep_descr_t		*endpoint,
189 				usb_port_status_t	port_status,
190 				uint_t			*sbandwidth,
191 				uint_t			*cbandwidth);
192 static int	ehci_compute_classic_bandwidth(
193 				usb_ep_descr_t		*endpoint,
194 				usb_port_status_t	port_status,
195 				uint_t			*bandwidth);
196 int		ehci_adjust_polling_interval(
197 				ehci_state_t		*ehcip,
198 				usb_ep_descr_t		*endpoint,
199 				usb_port_status_t	port_status);
200 static int	ehci_adjust_high_speed_polling_interval(
201 				ehci_state_t		*ehcip,
202 				usb_ep_descr_t		*endpoint);
203 static uint_t	ehci_lattice_height(uint_t		interval);
204 static uint_t	ehci_lattice_parent(uint_t		node);
205 static uint_t	ehci_find_periodic_node(
206 				uint_t			leaf,
207 				int			interval);
208 static uint_t	ehci_leftmost_leaf(uint_t		node,
209 				uint_t			height);
210 static uint_t	ehci_pow_2(uint_t x);
211 static uint_t	ehci_log_2(uint_t x);
212 static int	ehci_find_bestfit_hs_mask(
213 				ehci_state_t		*ehcip,
214 				uchar_t			*smask,
215 				uint_t			*pnode,
216 				usb_ep_descr_t		*endpoint,
217 				uint_t			bandwidth,
218 				int			interval);
219 static int	ehci_find_bestfit_ls_intr_mask(
220 				ehci_state_t		*ehcip,
221 				uchar_t			*smask,
222 				uchar_t			*cmask,
223 				uint_t			*pnode,
224 				uint_t			sbandwidth,
225 				uint_t			cbandwidth,
226 				int			interval);
227 static int	ehci_find_bestfit_sitd_in_mask(
228 				ehci_state_t		*ehcip,
229 				uchar_t			*smask,
230 				uchar_t			*cmask,
231 				uint_t			*pnode,
232 				uint_t			sbandwidth,
233 				uint_t			cbandwidth,
234 				int			interval);
235 static int	ehci_find_bestfit_sitd_out_mask(
236 				ehci_state_t		*ehcip,
237 				uchar_t			*smask,
238 				uint_t			*pnode,
239 				uint_t			sbandwidth,
240 				int			interval);
241 static uint_t	ehci_calculate_bw_availability_mask(
242 				ehci_state_t		*ehcip,
243 				uint_t			bandwidth,
244 				int			leaf,
245 				int			leaf_count,
246 				uchar_t			*bw_mask);
247 static void	ehci_update_bw_availability(
248 				ehci_state_t		*ehcip,
249 				int			bandwidth,
250 				int			leftmost_leaf,
251 				int			leaf_count,
252 				uchar_t			mask);
253 
254 /* Miscellaneous functions */
255 ehci_state_t	*ehci_obtain_state(
256 				dev_info_t		*dip);
257 int		ehci_state_is_operational(
258 				ehci_state_t		*ehcip);
259 int		ehci_do_soft_reset(
260 				ehci_state_t		*ehcip);
261 usb_req_attrs_t ehci_get_xfer_attrs(ehci_state_t	*ehcip,
262 				ehci_pipe_private_t	*pp,
263 				ehci_trans_wrapper_t	*tw);
264 usb_frame_number_t ehci_get_current_frame_number(
265 				ehci_state_t		*ehcip);
266 static void	ehci_cpr_cleanup(
267 				ehci_state_t		*ehcip);
268 int		ehci_wait_for_sof(
269 				ehci_state_t		*ehcip);
270 void		ehci_toggle_scheduler(
271 				ehci_state_t		*ehcip);
272 void		ehci_print_caps(ehci_state_t		*ehcip);
273 void		ehci_print_regs(ehci_state_t		*ehcip);
274 void		ehci_print_qh(ehci_state_t		*ehcip,
275 				ehci_qh_t		*qh);
276 void		ehci_print_qtd(ehci_state_t		*ehcip,
277 				ehci_qtd_t		*qtd);
278 void		ehci_create_stats(ehci_state_t		*ehcip);
279 void		ehci_destroy_stats(ehci_state_t		*ehcip);
280 void		ehci_do_intrs_stats(ehci_state_t	*ehcip,
281 				int		val);
282 void		ehci_do_byte_stats(ehci_state_t		*ehcip,
283 				size_t		len,
284 				uint8_t		attr,
285 				uint8_t		addr);
286 
287 /*
288  * check if this ehci controller can support PM
289  */
290 int
291 ehci_hcdi_pm_support(dev_info_t *dip)
292 {
293 	ehci_state_t *ehcip = ddi_get_soft_state(ehci_statep,
294 				ddi_get_instance(dip));
295 
296 	if (((ehcip->ehci_vendor_id == PCI_VENDOR_NEC_COMBO) &&
297 	    (ehcip->ehci_device_id == PCI_DEVICE_NEC_COMBO)) ||
298 
299 	    ((ehcip->ehci_vendor_id == PCI_VENDOR_ULi_M1575) &&
300 	    (ehcip->ehci_device_id == PCI_DEVICE_ULi_M1575)) ||
301 
302 	    (ehcip->ehci_vendor_id == PCI_VENDOR_VIA)) {
303 
304 		return (USB_SUCCESS);
305 	}
306 
307 	return (USB_FAILURE);
308 }
309 
310 
311 /*
312  * Host Controller Driver (HCD) initialization functions
313  */
314 
315 /*
316  * ehci_set_dma_attributes:
317  *
318  * Set the limits in the DMA attributes structure. Most of the values used
319  * in the  DMA limit structures are the default values as specified by	the
320  * Writing PCI device drivers document.
321  */
322 void
323 ehci_set_dma_attributes(ehci_state_t	*ehcip)
324 {
325 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
326 	    "ehci_set_dma_attributes:");
327 
328 	/* Initialize the DMA attributes */
329 	ehcip->ehci_dma_attr.dma_attr_version = DMA_ATTR_V0;
330 	ehcip->ehci_dma_attr.dma_attr_addr_lo = 0x00000000ull;
331 	ehcip->ehci_dma_attr.dma_attr_addr_hi = 0xfffffffeull;
332 
333 	/* 32 bit addressing */
334 	ehcip->ehci_dma_attr.dma_attr_count_max = EHCI_DMA_ATTR_COUNT_MAX;
335 
336 	/* Byte alignment */
337 	ehcip->ehci_dma_attr.dma_attr_align = EHCI_DMA_ATTR_ALIGNMENT;
338 
339 	/*
340 	 * Since PCI  specification is byte alignment, the
341 	 * burst size field should be set to 1 for PCI devices.
342 	 */
343 	ehcip->ehci_dma_attr.dma_attr_burstsizes = 0x1;
344 
345 	ehcip->ehci_dma_attr.dma_attr_minxfer = 0x1;
346 	ehcip->ehci_dma_attr.dma_attr_maxxfer = EHCI_DMA_ATTR_MAX_XFER;
347 	ehcip->ehci_dma_attr.dma_attr_seg = 0xffffffffull;
348 	ehcip->ehci_dma_attr.dma_attr_sgllen = 1;
349 	ehcip->ehci_dma_attr.dma_attr_granular = EHCI_DMA_ATTR_GRANULAR;
350 	ehcip->ehci_dma_attr.dma_attr_flags = 0;
351 }
352 
353 
354 /*
355  * ehci_allocate_pools:
356  *
357  * Allocate the system memory for the Endpoint Descriptor (QH) and for the
358  * Transfer Descriptor (QTD) pools. Both QH and QTD structures must be aligned
359  * to a 16 byte boundary.
360  */
361 int
362 ehci_allocate_pools(ehci_state_t	*ehcip)
363 {
364 	ddi_device_acc_attr_t		dev_attr;
365 	size_t				real_length;
366 	int				result;
367 	uint_t				ccount;
368 	int				i;
369 
370 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
371 	    "ehci_allocate_pools:");
372 
373 	/* The host controller will be little endian */
374 	dev_attr.devacc_attr_version	= DDI_DEVICE_ATTR_V0;
375 	dev_attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
376 	dev_attr.devacc_attr_dataorder	= DDI_STRICTORDER_ACC;
377 
378 	/* Byte alignment */
379 	ehcip->ehci_dma_attr.dma_attr_align = EHCI_DMA_ATTR_TD_QH_ALIGNMENT;
380 
381 	/* Allocate the QTD pool DMA handle */
382 	if (ddi_dma_alloc_handle(ehcip->ehci_dip, &ehcip->ehci_dma_attr,
383 			DDI_DMA_SLEEP, 0,
384 			&ehcip->ehci_qtd_pool_dma_handle) != DDI_SUCCESS) {
385 
386 		goto failure;
387 	}
388 
389 	/* Allocate the memory for the QTD pool */
390 	if (ddi_dma_mem_alloc(ehcip->ehci_qtd_pool_dma_handle,
391 			ehci_qtd_pool_size * sizeof (ehci_qtd_t),
392 			&dev_attr,
393 			DDI_DMA_CONSISTENT,
394 			DDI_DMA_SLEEP,
395 			0,
396 			(caddr_t *)&ehcip->ehci_qtd_pool_addr,
397 			&real_length,
398 			&ehcip->ehci_qtd_pool_mem_handle)) {
399 
400 		goto failure;
401 	}
402 
403 	/* Map the QTD pool into the I/O address space */
404 	result = ddi_dma_addr_bind_handle(
405 			ehcip->ehci_qtd_pool_dma_handle,
406 			NULL,
407 			(caddr_t)ehcip->ehci_qtd_pool_addr,
408 			real_length,
409 			DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
410 			DDI_DMA_SLEEP,
411 			NULL,
412 			&ehcip->ehci_qtd_pool_cookie,
413 			&ccount);
414 
415 	bzero((void *)ehcip->ehci_qtd_pool_addr,
416 			ehci_qtd_pool_size * sizeof (ehci_qtd_t));
417 
418 	/* Process the result */
419 	if (result == DDI_DMA_MAPPED) {
420 		/* The cookie count should be 1 */
421 		if (ccount != 1) {
422 			USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
423 			    "ehci_allocate_pools: More than 1 cookie");
424 
425 		goto failure;
426 		}
427 	} else {
428 		USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
429 		    "ehci_allocate_pools: Result = %d", result);
430 
431 		ehci_decode_ddi_dma_addr_bind_handle_result(ehcip, result);
432 
433 		goto failure;
434 	}
435 
436 	/*
437 	 * DMA addresses for QTD pools are bound
438 	 */
439 	ehcip->ehci_dma_addr_bind_flag |= EHCI_QTD_POOL_BOUND;
440 
441 	/* Initialize the QTD pool */
442 	for (i = 0; i < ehci_qtd_pool_size; i ++) {
443 		Set_QTD(ehcip->ehci_qtd_pool_addr[i].
444 		    qtd_state, EHCI_QTD_FREE);
445 	}
446 
447 	/* Allocate the QTD pool DMA handle */
448 	if (ddi_dma_alloc_handle(ehcip->ehci_dip,
449 			&ehcip->ehci_dma_attr,
450 			DDI_DMA_SLEEP,
451 			0,
452 			&ehcip->ehci_qh_pool_dma_handle) != DDI_SUCCESS) {
453 
454 		goto failure;
455 	}
456 
457 	/* Allocate the memory for the QH pool */
458 	if (ddi_dma_mem_alloc(ehcip->ehci_qh_pool_dma_handle,
459 			ehci_qh_pool_size * sizeof (ehci_qh_t),
460 			&dev_attr,
461 			DDI_DMA_CONSISTENT,
462 			DDI_DMA_SLEEP,
463 			0,
464 			(caddr_t *)&ehcip->ehci_qh_pool_addr,
465 			&real_length,
466 			&ehcip->ehci_qh_pool_mem_handle) != DDI_SUCCESS) {
467 
468 		goto failure;
469 	}
470 
471 	result = ddi_dma_addr_bind_handle(ehcip->ehci_qh_pool_dma_handle,
472 			NULL,
473 			(caddr_t)ehcip->ehci_qh_pool_addr,
474 			real_length,
475 			DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
476 			DDI_DMA_SLEEP,
477 			NULL,
478 			&ehcip->ehci_qh_pool_cookie,
479 			&ccount);
480 
481 	bzero((void *)ehcip->ehci_qh_pool_addr,
482 			ehci_qh_pool_size * sizeof (ehci_qh_t));
483 
484 	/* Process the result */
485 	if (result == DDI_DMA_MAPPED) {
486 		/* The cookie count should be 1 */
487 		if (ccount != 1) {
488 			USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
489 			    "ehci_allocate_pools: More than 1 cookie");
490 
491 			goto failure;
492 		}
493 	} else {
494 		ehci_decode_ddi_dma_addr_bind_handle_result(ehcip, result);
495 
496 		goto failure;
497 	}
498 
499 	/*
500 	 * DMA addresses for QH pools are bound
501 	 */
502 	ehcip->ehci_dma_addr_bind_flag |= EHCI_QH_POOL_BOUND;
503 
504 	/* Initialize the QH pool */
505 	for (i = 0; i < ehci_qh_pool_size; i ++) {
506 		Set_QH(ehcip->ehci_qh_pool_addr[i].qh_state, EHCI_QH_FREE);
507 	}
508 
509 	/* Byte alignment */
510 	ehcip->ehci_dma_attr.dma_attr_align = EHCI_DMA_ATTR_ALIGNMENT;
511 
512 	return (DDI_SUCCESS);
513 
514 failure:
515 	/* Byte alignment */
516 	ehcip->ehci_dma_attr.dma_attr_align = EHCI_DMA_ATTR_ALIGNMENT;
517 
518 	return (DDI_FAILURE);
519 }
520 
521 
522 /*
523  * ehci_decode_ddi_dma_addr_bind_handle_result:
524  *
525  * Process the return values of ddi_dma_addr_bind_handle()
526  */
527 void
528 ehci_decode_ddi_dma_addr_bind_handle_result(
529 	ehci_state_t	*ehcip,
530 	int		result)
531 {
532 	USB_DPRINTF_L2(PRINT_MASK_ALLOC, ehcip->ehci_log_hdl,
533 	    "ehci_decode_ddi_dma_addr_bind_handle_result:");
534 
535 	switch (result) {
536 	case DDI_DMA_PARTIAL_MAP:
537 		USB_DPRINTF_L2(PRINT_MASK_ALL, ehcip->ehci_log_hdl,
538 		    "Partial transfers not allowed");
539 		break;
540 	case DDI_DMA_INUSE:
541 		USB_DPRINTF_L2(PRINT_MASK_ALL,	ehcip->ehci_log_hdl,
542 		    "Handle is in use");
543 		break;
544 	case DDI_DMA_NORESOURCES:
545 		USB_DPRINTF_L2(PRINT_MASK_ALL,	ehcip->ehci_log_hdl,
546 		    "No resources");
547 		break;
548 	case DDI_DMA_NOMAPPING:
549 		USB_DPRINTF_L2(PRINT_MASK_ALL,	ehcip->ehci_log_hdl,
550 		    "No mapping");
551 		break;
552 	case DDI_DMA_TOOBIG:
553 		USB_DPRINTF_L2(PRINT_MASK_ALL,	ehcip->ehci_log_hdl,
554 		    "Object is too big");
555 		break;
556 	default:
557 		USB_DPRINTF_L2(PRINT_MASK_ALL,	ehcip->ehci_log_hdl,
558 		    "Unknown dma error");
559 	}
560 }
561 
562 
563 /*
564  * ehci_map_regs:
565  *
566  * The Host Controller (HC) contains a set of on-chip operational registers
567  * and which should be mapped into a non-cacheable portion of the  system
568  * addressable space.
569  */
570 int
571 ehci_map_regs(ehci_state_t	*ehcip)
572 {
573 	ddi_device_acc_attr_t	attr;
574 	uint16_t		cmd_reg;
575 	uint_t			length;
576 
577 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl, "ehci_map_regs:");
578 
579 	/* Check to make sure we have memory access */
580 	if (pci_config_setup(ehcip->ehci_dip,
581 		&ehcip->ehci_config_handle) != DDI_SUCCESS) {
582 
583 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
584 		    "ehci_map_regs: Config error");
585 
586 		return (DDI_FAILURE);
587 	}
588 
589 	/* Make sure Memory Access Enable is set */
590 	cmd_reg = pci_config_get16(ehcip->ehci_config_handle, PCI_CONF_COMM);
591 
592 	if (!(cmd_reg & PCI_COMM_MAE)) {
593 
594 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
595 		    "ehci_map_regs: Memory base address access disabled");
596 
597 		return (DDI_FAILURE);
598 	}
599 
600 	/* The host controller will be little endian */
601 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
602 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
603 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
604 
605 	/* Map in EHCI Capability registers */
606 	if (ddi_regs_map_setup(ehcip->ehci_dip, 1,
607 	    (caddr_t *)&ehcip->ehci_capsp, 0,
608 	    sizeof (ehci_caps_t), &attr,
609 	    &ehcip->ehci_caps_handle) != DDI_SUCCESS) {
610 
611 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
612 		    "ehci_map_regs: Map setup error");
613 
614 		return (DDI_FAILURE);
615 	}
616 
617 	length = ddi_get8(ehcip->ehci_caps_handle,
618 	    (uint8_t *)&ehcip->ehci_capsp->ehci_caps_length);
619 
620 	/* Free the original mapping */
621 	ddi_regs_map_free(&ehcip->ehci_caps_handle);
622 
623 	/* Re-map in EHCI Capability and Operational registers */
624 	if (ddi_regs_map_setup(ehcip->ehci_dip, 1,
625 	    (caddr_t *)&ehcip->ehci_capsp, 0,
626 	    length + sizeof (ehci_regs_t), &attr,
627 	    &ehcip->ehci_caps_handle) != DDI_SUCCESS) {
628 
629 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
630 		    "ehci_map_regs: Map setup error");
631 
632 		return (DDI_FAILURE);
633 	}
634 
635 	/* Get the pointer to EHCI Operational Register */
636 	ehcip->ehci_regsp = (ehci_regs_t *)
637 	    ((uintptr_t)ehcip->ehci_capsp + length);
638 
639 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
640 	    "ehci_map_regs: Capsp 0x%p Regsp 0x%p\n",
641 	    ehcip->ehci_capsp, ehcip->ehci_regsp);
642 
643 	return (DDI_SUCCESS);
644 }
645 
646 /*
647  * The following simulated polling is for debugging purposes only.
648  * It is activated on x86 by setting usb-polling=true in GRUB or ehci.conf.
649  */
650 static int
651 ehci_is_polled(dev_info_t *dip)
652 {
653 	int ret;
654 	char *propval;
655 
656 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
657 	    "usb-polling", &propval) != DDI_SUCCESS)
658 
659 		return (0);
660 
661 	ret = (strcmp(propval, "true") == 0);
662 	ddi_prop_free(propval);
663 
664 	return (ret);
665 }
666 
667 static void
668 ehci_poll_intr(void *arg)
669 {
670 	/* poll every msec */
671 	for (;;) {
672 		(void) ehci_intr(arg, NULL);
673 		delay(drv_usectohz(1000));
674 	}
675 }
676 
677 /*
678  * ehci_register_intrs_and_init_mutex:
679  *
680  * Register interrupts and initialize each mutex and condition variables
681  */
682 int
683 ehci_register_intrs_and_init_mutex(ehci_state_t	*ehcip)
684 {
685 	int	intr_types;
686 
687 #if defined(__x86)
688 	uint8_t iline;
689 #endif
690 
691 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
692 	    "ehci_register_intrs_and_init_mutex:");
693 
694 	/*
695 	 * There is a known MSI hardware bug with the EHCI controller
696 	 * of ULI1575 southbridge. Hence MSI is disabled for this chip.
697 	 */
698 	if ((ehcip->ehci_vendor_id == PCI_VENDOR_ULi_M1575) &&
699 	    (ehcip->ehci_device_id == PCI_DEVICE_ULi_M1575)) {
700 		ehcip->ehci_msi_enabled = B_FALSE;
701 	} else {
702 		/* Set the MSI enable flag from the global EHCI MSI tunable */
703 		ehcip->ehci_msi_enabled = ehci_enable_msi;
704 	}
705 
706 	/* launch polling thread instead of enabling pci interrupt */
707 	if (ehci_is_polled(ehcip->ehci_dip)) {
708 		extern pri_t maxclsyspri;
709 
710 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
711 		    "ehci_register_intrs_and_init_mutex: "
712 		    "running in simulated polled mode");
713 
714 		(void) thread_create(NULL, 0, ehci_poll_intr, ehcip, 0, &p0,
715 		    TS_RUN, maxclsyspri);
716 
717 		goto skip_intr;
718 	}
719 
720 #if defined(__x86)
721 	/*
722 	 * Make sure that the interrupt pin is connected to the
723 	 * interrupt controller on x86.	 Interrupt line 255 means
724 	 * "unknown" or "not connected" (PCI spec 6.2.4, footnote 43).
725 	 * If we would return failure when interrupt line equals 255, then
726 	 * high speed devices will be routed to companion host controllers.
727 	 * However, it is not necessary to return failure here, and
728 	 * o/uhci codes don't check the interrupt line either.
729 	 * But it's good to log a message here for debug purposes.
730 	 */
731 	iline = pci_config_get8(ehcip->ehci_config_handle,
732 	    PCI_CONF_ILINE);
733 
734 	if (iline == 255) {
735 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
736 		    "ehci_register_intrs_and_init_mutex: "
737 		    "interrupt line value out of range (%d)",
738 		    iline);
739 	}
740 #endif	/* __x86 */
741 
742 	/* Get supported interrupt types */
743 	if (ddi_intr_get_supported_types(ehcip->ehci_dip,
744 	    &intr_types) != DDI_SUCCESS) {
745 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
746 		    "ehci_register_intrs_and_init_mutex: "
747 		    "ddi_intr_get_supported_types failed");
748 
749 		return (DDI_FAILURE);
750 	}
751 
752 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
753 	    "ehci_register_intrs_and_init_mutex: "
754 	    "supported interrupt types 0x%x", intr_types);
755 
756 	if ((intr_types & DDI_INTR_TYPE_MSI) && ehcip->ehci_msi_enabled) {
757 		if (ehci_add_intrs(ehcip, DDI_INTR_TYPE_MSI)
758 		    != DDI_SUCCESS) {
759 			USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
760 			    "ehci_register_intrs_and_init_mutex: MSI "
761 			    "registration failed, trying FIXED interrupt \n");
762 		} else {
763 			USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
764 			    "ehci_register_intrs_and_init_mutex: "
765 			    "Using MSI interrupt type\n");
766 
767 			ehcip->ehci_intr_type = DDI_INTR_TYPE_MSI;
768 			ehcip->ehci_flags |= EHCI_INTR;
769 		}
770 	}
771 
772 	if ((!(ehcip->ehci_flags & EHCI_INTR)) &&
773 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
774 		if (ehci_add_intrs(ehcip, DDI_INTR_TYPE_FIXED)
775 		    != DDI_SUCCESS) {
776 			USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
777 			    "ehci_register_intrs_and_init_mutex: "
778 			    "FIXED interrupt registration failed\n");
779 
780 			return (DDI_FAILURE);
781 		}
782 
783 		USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
784 		    "ehci_register_intrs_and_init_mutex: "
785 		    "Using FIXED interrupt type\n");
786 
787 		ehcip->ehci_intr_type = DDI_INTR_TYPE_FIXED;
788 		ehcip->ehci_flags |= EHCI_INTR;
789 	}
790 
791 skip_intr:
792 	/* Create prototype for advance on async schedule */
793 	cv_init(&ehcip->ehci_async_schedule_advance_cv,
794 	    NULL, CV_DRIVER, NULL);
795 
796 	return (DDI_SUCCESS);
797 }
798 
799 
800 /*
801  * ehci_add_intrs:
802  *
803  * Register FIXED or MSI interrupts.
804  */
805 static int
806 ehci_add_intrs(ehci_state_t	*ehcip,
807 		int		intr_type)
808 {
809 	int	actual, avail, intr_size, count = 0;
810 	int 	i, flag, ret;
811 
812 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
813 	    "ehci_add_intrs: interrupt type 0x%x", intr_type);
814 
815 	/* Get number of interrupts */
816 	ret = ddi_intr_get_nintrs(ehcip->ehci_dip, intr_type, &count);
817 	if ((ret != DDI_SUCCESS) || (count == 0)) {
818 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
819 		    "ehci_add_intrs: ddi_intr_get_nintrs() failure, "
820 		    "ret: %d, count: %d", ret, count);
821 
822 		return (DDI_FAILURE);
823 	}
824 
825 	/* Get number of available interrupts */
826 	ret = ddi_intr_get_navail(ehcip->ehci_dip, intr_type, &avail);
827 	if ((ret != DDI_SUCCESS) || (avail == 0)) {
828 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
829 		    "ehci_add_intrs: ddi_intr_get_navail() failure, "
830 		    "ret: %d, count: %d", ret, count);
831 
832 		return (DDI_FAILURE);
833 	}
834 
835 	if (avail < count) {
836 		USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
837 		    "ehci_add_intrs: ehci_add_intrs: nintrs () "
838 		    "returned %d, navail returned %d\n", count, avail);
839 	}
840 
841 	/* Allocate an array of interrupt handles */
842 	intr_size = count * sizeof (ddi_intr_handle_t);
843 	ehcip->ehci_htable = kmem_zalloc(intr_size, KM_SLEEP);
844 
845 	flag = (intr_type == DDI_INTR_TYPE_MSI) ?
846 	    DDI_INTR_ALLOC_STRICT:DDI_INTR_ALLOC_NORMAL;
847 
848 	/* call ddi_intr_alloc() */
849 	ret = ddi_intr_alloc(ehcip->ehci_dip, ehcip->ehci_htable,
850 	    intr_type, 0, count, &actual, flag);
851 
852 	if ((ret != DDI_SUCCESS) || (actual == 0)) {
853 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
854 		    "ehci_add_intrs: ddi_intr_alloc() failed %d", ret);
855 
856 		kmem_free(ehcip->ehci_htable, intr_size);
857 
858 		return (DDI_FAILURE);
859 	}
860 
861 	if (actual < count) {
862 		USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
863 		    "ehci_add_intrs: Requested: %d, Received: %d\n",
864 		    count, actual);
865 
866 		for (i = 0; i < actual; i++)
867 			(void) ddi_intr_free(ehcip->ehci_htable[i]);
868 
869 		kmem_free(ehcip->ehci_htable, intr_size);
870 
871 		return (DDI_FAILURE);
872 	}
873 
874 	ehcip->ehci_intr_cnt = actual;
875 
876 	if ((ret = ddi_intr_get_pri(ehcip->ehci_htable[0],
877 	    &ehcip->ehci_intr_pri)) != DDI_SUCCESS) {
878 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
879 		    "ehci_add_intrs: ddi_intr_get_pri() failed %d", ret);
880 
881 		for (i = 0; i < actual; i++)
882 			(void) ddi_intr_free(ehcip->ehci_htable[i]);
883 
884 		kmem_free(ehcip->ehci_htable, intr_size);
885 
886 		return (DDI_FAILURE);
887 	}
888 
889 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
890 	    "ehci_add_intrs: Supported Interrupt priority 0x%x",
891 	    ehcip->ehci_intr_pri);
892 
893 	/* Test for high level mutex */
894 	if (ehcip->ehci_intr_pri >= ddi_intr_get_hilevel_pri()) {
895 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
896 		    "ehci_add_intrs: Hi level interrupt not supported");
897 
898 		for (i = 0; i < actual; i++)
899 			(void) ddi_intr_free(ehcip->ehci_htable[i]);
900 
901 		kmem_free(ehcip->ehci_htable, intr_size);
902 
903 		return (DDI_FAILURE);
904 	}
905 
906 	/* Initialize the mutex */
907 	mutex_init(&ehcip->ehci_int_mutex, NULL, MUTEX_DRIVER,
908 	    DDI_INTR_PRI(ehcip->ehci_intr_pri));
909 
910 	/* Call ddi_intr_add_handler() */
911 	for (i = 0; i < actual; i++) {
912 		if ((ret = ddi_intr_add_handler(ehcip->ehci_htable[i],
913 		    ehci_intr, (caddr_t)ehcip,
914 		    (caddr_t)(uintptr_t)i)) != DDI_SUCCESS) {
915 			USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
916 			    "ehci_add_intrs:ddi_intr_add_handler() "
917 			    "failed %d", ret);
918 
919 			for (i = 0; i < actual; i++)
920 				(void) ddi_intr_free(ehcip->ehci_htable[i]);
921 
922 			mutex_destroy(&ehcip->ehci_int_mutex);
923 			kmem_free(ehcip->ehci_htable, intr_size);
924 
925 			return (DDI_FAILURE);
926 		}
927 	}
928 
929 	if ((ret = ddi_intr_get_cap(ehcip->ehci_htable[0],
930 	    &ehcip->ehci_intr_cap)) != DDI_SUCCESS) {
931 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
932 		    "ehci_add_intrs: ddi_intr_get_cap() failed %d", ret);
933 
934 		for (i = 0; i < actual; i++) {
935 			(void) ddi_intr_remove_handler(ehcip->ehci_htable[i]);
936 			(void) ddi_intr_free(ehcip->ehci_htable[i]);
937 		}
938 
939 		mutex_destroy(&ehcip->ehci_int_mutex);
940 		kmem_free(ehcip->ehci_htable, intr_size);
941 
942 		return (DDI_FAILURE);
943 	}
944 
945 	/* Enable all interrupts */
946 	if (ehcip->ehci_intr_cap & DDI_INTR_FLAG_BLOCK) {
947 		/* Call ddi_intr_block_enable() for MSI interrupts */
948 		(void) ddi_intr_block_enable(ehcip->ehci_htable,
949 		    ehcip->ehci_intr_cnt);
950 	} else {
951 		/* Call ddi_intr_enable for MSI or FIXED interrupts */
952 		for (i = 0; i < ehcip->ehci_intr_cnt; i++)
953 			(void) ddi_intr_enable(ehcip->ehci_htable[i]);
954 	}
955 
956 	return (DDI_SUCCESS);
957 }
958 
959 
960 /*
961  * ehci_init_hardware
962  *
963  * take control from BIOS, reset EHCI host controller, and check version, etc.
964  */
965 int
966 ehci_init_hardware(ehci_state_t	*ehcip)
967 {
968 	int			revision;
969 	uint16_t		cmd_reg;
970 	int			abort_on_BIOS_take_over_failure;
971 
972 	/* Take control from the BIOS */
973 	if (ehci_take_control(ehcip) != USB_SUCCESS) {
974 
975 		/* read .conf file properties */
976 		abort_on_BIOS_take_over_failure =
977 					ddi_prop_get_int(DDI_DEV_T_ANY,
978 					ehcip->ehci_dip, DDI_PROP_DONTPASS,
979 					"abort-on-BIOS-take-over-failure", 0);
980 
981 		if (abort_on_BIOS_take_over_failure) {
982 
983 			USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
984 			    "Unable to take control from BIOS.");
985 
986 			return (DDI_FAILURE);
987 		}
988 
989 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
990 		    "Unable to take control from BIOS. Failure is ignored.");
991 	}
992 
993 	/* set Memory Master Enable */
994 	cmd_reg = pci_config_get16(ehcip->ehci_config_handle, PCI_CONF_COMM);
995 	cmd_reg |= (PCI_COMM_MAE | PCI_COMM_ME);
996 	pci_config_put16(ehcip->ehci_config_handle, PCI_CONF_COMM, cmd_reg);
997 
998 	/* Reset the EHCI host controller */
999 	Set_OpReg(ehci_command,
1000 	    Get_OpReg(ehci_command) | EHCI_CMD_HOST_CTRL_RESET);
1001 
1002 	/* Wait 10ms for reset to complete */
1003 	drv_usecwait(EHCI_RESET_TIMEWAIT);
1004 
1005 	ASSERT(Get_OpReg(ehci_status) & EHCI_STS_HOST_CTRL_HALTED);
1006 
1007 	/* Verify the version number */
1008 	revision = Get_16Cap(ehci_version);
1009 
1010 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1011 	    "ehci_init_hardware: Revision 0x%x", revision);
1012 
1013 	/*
1014 	 * EHCI driver supports EHCI host controllers compliant to
1015 	 * 0.95 and higher revisions of EHCI specifications.
1016 	 */
1017 	if (revision < EHCI_REVISION_0_95) {
1018 
1019 		USB_DPRINTF_L0(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1020 		    "Revision 0x%x is not supported", revision);
1021 
1022 		return (DDI_FAILURE);
1023 	}
1024 
1025 	if (ehcip->ehci_hc_soft_state == EHCI_CTLR_INIT_STATE) {
1026 
1027 		/* Initialize the Frame list base address area */
1028 		if (ehci_init_periodic_frame_lst_table(ehcip) != DDI_SUCCESS) {
1029 
1030 			return (DDI_FAILURE);
1031 		}
1032 
1033 		/*
1034 		 * For performance reasons, do not insert anything into the
1035 		 * asynchronous list or activate the asynch list schedule until
1036 		 * there is a valid QH.
1037 		 */
1038 		ehcip->ehci_head_of_async_sched_list = NULL;
1039 
1040 		if ((ehcip->ehci_vendor_id == PCI_VENDOR_VIA) &&
1041 		    (ehci_vt62x2_workaround & EHCI_VIA_ASYNC_SCHEDULE)) {
1042 			/*
1043 			 * The driver is unable to reliably stop the asynch
1044 			 * list schedule on VIA VT6202 controllers, so we
1045 			 * always keep a dummy QH on the list.
1046 			 */
1047 			ehci_qh_t *dummy_async_qh =
1048 			    ehci_alloc_qh(ehcip, NULL, NULL);
1049 
1050 			Set_QH(dummy_async_qh->qh_link_ptr,
1051 			    ((ehci_qh_cpu_to_iommu(ehcip, dummy_async_qh) &
1052 			    EHCI_QH_LINK_PTR) | EHCI_QH_LINK_REF_QH));
1053 
1054 			/* Set this QH to be the "head" of the circular list */
1055 			Set_QH(dummy_async_qh->qh_ctrl,
1056 			    Get_QH(dummy_async_qh->qh_ctrl) |
1057 			    EHCI_QH_CTRL_RECLAIM_HEAD);
1058 
1059 			Set_QH(dummy_async_qh->qh_next_qtd,
1060 			    EHCI_QH_NEXT_QTD_PTR_VALID);
1061 			Set_QH(dummy_async_qh->qh_alt_next_qtd,
1062 			    EHCI_QH_ALT_NEXT_QTD_PTR_VALID);
1063 
1064 			ehcip->ehci_head_of_async_sched_list = dummy_async_qh;
1065 			ehcip->ehci_open_async_count++;
1066 		}
1067 	}
1068 
1069 	return (DDI_SUCCESS);
1070 }
1071 
1072 
1073 /*
1074  * ehci_init_workaround
1075  *
1076  * some workarounds during initializing ehci
1077  */
1078 int
1079 ehci_init_workaround(ehci_state_t	*ehcip)
1080 {
1081 	/*
1082 	 * Acer Labs Inc. M5273 EHCI controller does not send
1083 	 * interrupts unless the Root hub ports are routed to the EHCI
1084 	 * host controller; so route the ports now, before we test for
1085 	 * the presence of SOFs interrupts.
1086 	 */
1087 	if (ehcip->ehci_vendor_id == PCI_VENDOR_ALI) {
1088 	    /* Route all Root hub ports to EHCI host controller */
1089 	    Set_OpReg(ehci_config_flag, EHCI_CONFIG_FLAG_EHCI);
1090 	}
1091 
1092 	/*
1093 	 * VIA chips have some issues and may not work reliably.
1094 	 * Revisions >= 0x80 are part of a southbridge and appear
1095 	 * to be reliable with the workaround.
1096 	 * For revisions < 0x80, if we	were bound using class
1097 	 * complain, else proceed. This will allow the user to
1098 	 * bind ehci specifically to this chip and not have the
1099 	 * warnings
1100 	 */
1101 	if (ehcip->ehci_vendor_id == PCI_VENDOR_VIA) {
1102 
1103 	    if (ehcip->ehci_rev_id >= PCI_VIA_REVISION_6212) {
1104 
1105 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1106 		    "ehci_init_workaround: Applying VIA workarounds "
1107 		    "for the 6212 chip.");
1108 
1109 	    } else if (strcmp(DEVI(ehcip->ehci_dip)->devi_binding_name,
1110 		"pciclass,0c0320") == 0) {
1111 
1112 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1113 		    "Due to recently discovered incompatibilities");
1114 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1115 		    "with this USB controller, USB2.x transfer");
1116 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1117 		    "support has been disabled. This device will");
1118 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1119 		    "continue to function as a USB1.x controller.");
1120 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1121 		    "If you are interested in enabling USB2.x");
1122 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1123 		    "support please, refer to the ehci(7D) man page.");
1124 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1125 		    "Please also refer to www.sun.com/io for");
1126 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1127 		    "Solaris Ready products and to");
1128 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1129 		    "www.sun.com/bigadmin/hcl for additional");
1130 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1131 		    "compatible USB products.");
1132 
1133 		return (DDI_FAILURE);
1134 
1135 	    } else if (ehci_vt62x2_workaround) {
1136 
1137 		USB_DPRINTF_L1(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1138 		    "Applying VIA workarounds");
1139 	    }
1140 	}
1141 
1142 	return (DDI_SUCCESS);
1143 }
1144 
1145 
1146 /*
1147  * ehci_init_check_status
1148  *
1149  * Check if EHCI host controller is running
1150  */
1151 int
1152 ehci_init_check_status(ehci_state_t	*ehcip)
1153 {
1154 	clock_t			sof_time_wait;
1155 
1156 	/*
1157 	 * Get the number of clock ticks to wait.
1158 	 * This is based on the maximum time it takes for a frame list rollover
1159 	 * and maximum time wait for SOFs to begin.
1160 	 */
1161 	sof_time_wait = drv_usectohz((EHCI_NUM_PERIODIC_FRAME_LISTS * 1000) +
1162 	    EHCI_SOF_TIMEWAIT);
1163 
1164 	/* Tell the ISR to broadcast ehci_async_schedule_advance_cv */
1165 	ehcip->ehci_flags |= EHCI_CV_INTR;
1166 
1167 	/* We need to add a delay to allow the chip time to start running */
1168 	(void) cv_timedwait(&ehcip->ehci_async_schedule_advance_cv,
1169 	    &ehcip->ehci_int_mutex, ddi_get_lbolt() + sof_time_wait);
1170 
1171 	/*
1172 	 * Check EHCI host controller is running, otherwise return failure.
1173 	 */
1174 	if ((ehcip->ehci_flags & EHCI_CV_INTR) ||
1175 	    (Get_OpReg(ehci_status) & EHCI_STS_HOST_CTRL_HALTED)) {
1176 
1177 		USB_DPRINTF_L0(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1178 		    "No SOF interrupts have been received, this USB EHCI host"
1179 		    "controller is unusable");
1180 
1181 		/*
1182 		 * Route all Root hub ports to Classic host
1183 		 * controller, in case this is an unusable ALI M5273
1184 		 * EHCI controller.
1185 		 */
1186 		if (ehcip->ehci_vendor_id == PCI_VENDOR_ALI) {
1187 			Set_OpReg(ehci_config_flag, EHCI_CONFIG_FLAG_CLASSIC);
1188 		}
1189 
1190 		return (DDI_FAILURE);
1191 	}
1192 
1193 	return (DDI_SUCCESS);
1194 }
1195 
1196 
1197 /*
1198  * ehci_init_ctlr:
1199  *
1200  * Initialize the Host Controller (HC).
1201  */
1202 int
1203 ehci_init_ctlr(ehci_state_t	*ehcip,
1204 		int		init_type)
1205 {
1206 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl, "ehci_init_ctlr:");
1207 
1208 	if (init_type == EHCI_NORMAL_INITIALIZATION) {
1209 
1210 		if (ehci_init_hardware(ehcip) != DDI_SUCCESS) {
1211 
1212 			return (DDI_FAILURE);
1213 		}
1214 	}
1215 
1216 	/*
1217 	 * Check for Asynchronous schedule park capability feature. If this
1218 	 * feature is supported, then, program ehci command register with
1219 	 * appropriate values..
1220 	 */
1221 	if (Get_Cap(ehci_hcc_params) & EHCI_HCC_ASYNC_SCHED_PARK_CAP) {
1222 
1223 		USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1224 		    "ehci_init_ctlr: Async park mode is supported");
1225 
1226 		Set_OpReg(ehci_command, (Get_OpReg(ehci_command) |
1227 		    (EHCI_CMD_ASYNC_PARK_ENABLE |
1228 		    EHCI_CMD_ASYNC_PARK_COUNT_3)));
1229 	}
1230 
1231 	/*
1232 	 * Check for programmable periodic frame list feature. If this
1233 	 * feature is supported, then, program ehci command register with
1234 	 * 1024 frame list value.
1235 	 */
1236 	if (Get_Cap(ehci_hcc_params) & EHCI_HCC_PROG_FRAME_LIST_FLAG) {
1237 
1238 		USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1239 		    "ehci_init_ctlr: Variable programmable periodic "
1240 		    "frame list is supported");
1241 
1242 		Set_OpReg(ehci_command, (Get_OpReg(ehci_command) |
1243 		    EHCI_CMD_FRAME_1024_SIZE));
1244 	}
1245 
1246 	/*
1247 	 * Currently EHCI driver doesn't support 64 bit addressing.
1248 	 *
1249 	 * If we are using 64 bit addressing capability, then, program
1250 	 * ehci_ctrl_segment register with 4 Gigabyte segment where all
1251 	 * of the interface data structures are allocated.
1252 	 */
1253 	if (Get_Cap(ehci_hcc_params) & EHCI_HCC_64BIT_ADDR_CAP) {
1254 
1255 		USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1256 		    "ehci_init_ctlr: EHCI driver doesn't support "
1257 		    "64 bit addressing");
1258 	}
1259 
1260 	/* 64 bit addressing is not support */
1261 	Set_OpReg(ehci_ctrl_segment, 0x00000000);
1262 
1263 	/* Turn on/off the schedulers */
1264 	ehci_toggle_scheduler(ehcip);
1265 
1266 	/*
1267 	 * Set the Periodic Frame List Base Address register with the
1268 	 * starting physical address of the Periodic Frame List.
1269 	 */
1270 	Set_OpReg(ehci_periodic_list_base,
1271 	    (uint32_t)(ehcip->ehci_pflt_cookie.dmac_address &
1272 		EHCI_PERIODIC_LIST_BASE));
1273 
1274 	/*
1275 	 * Set ehci_interrupt to enable all interrupts except Root
1276 	 * Hub Status change interrupt.
1277 	 */
1278 	Set_OpReg(ehci_interrupt, EHCI_INTR_HOST_SYSTEM_ERROR |
1279 	    EHCI_INTR_FRAME_LIST_ROLLOVER | EHCI_INTR_USB_ERROR |
1280 	    EHCI_INTR_USB);
1281 
1282 	/*
1283 	 * Set the desired interrupt threshold and turn on EHCI host controller.
1284 	 */
1285 	Set_OpReg(ehci_command,
1286 	    ((Get_OpReg(ehci_command) & ~EHCI_CMD_INTR_THRESHOLD) |
1287 		(EHCI_CMD_01_INTR | EHCI_CMD_HOST_CTRL_RUN)));
1288 
1289 	ASSERT(Get_OpReg(ehci_command) & EHCI_CMD_HOST_CTRL_RUN);
1290 
1291 	if (init_type == EHCI_NORMAL_INITIALIZATION) {
1292 
1293 		if (ehci_init_workaround(ehcip) != DDI_SUCCESS) {
1294 
1295 			return (DDI_FAILURE);
1296 		}
1297 
1298 		if (ehci_init_check_status(ehcip) != DDI_SUCCESS) {
1299 
1300 			return (DDI_FAILURE);
1301 		}
1302 
1303 		USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1304 		    "ehci_init_ctlr: SOF's have started");
1305 	}
1306 
1307 	/* Route all Root hub ports to EHCI host controller */
1308 	Set_OpReg(ehci_config_flag, EHCI_CONFIG_FLAG_EHCI);
1309 
1310 	/* Set host controller soft state to operational */
1311 	ehcip->ehci_hc_soft_state = EHCI_CTLR_OPERATIONAL_STATE;
1312 
1313 	return (DDI_SUCCESS);
1314 }
1315 
1316 /*
1317  * ehci_take_control:
1318  *
1319  * Handshake to take EHCI control from BIOS if necessary.  Its only valid for
1320  * x86 machines, because sparc doesn't have a BIOS.
1321  * On x86 machine, the take control process includes
1322  *    o get the base address of the extended capability list
1323  *    o find out the capability for handoff synchronization in the list.
1324  *    o check if BIOS has owned the host controller.
1325  *    o set the OS Owned semaphore bit, ask the BIOS to release the ownership.
1326  *    o wait for a constant time and check if BIOS has relinquished control.
1327  */
1328 /* ARGSUSED */
1329 static int
1330 ehci_take_control(ehci_state_t *ehcip)
1331 {
1332 #if defined(__x86)
1333 	uint32_t		extended_cap;
1334 	uint32_t		extended_cap_offset;
1335 	uint32_t		extended_cap_id;
1336 	uint_t			retry;
1337 
1338 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1339 	    "ehci_take_control:");
1340 
1341 	/*
1342 	 * According EHCI Spec 2.2.4, get EECP base address from HCCPARAMS
1343 	 * register.
1344 	 */
1345 	extended_cap_offset = (Get_Cap(ehci_hcc_params) & EHCI_HCC_EECP) >>
1346 	    EHCI_HCC_EECP_SHIFT;
1347 
1348 	/*
1349 	 * According EHCI Spec 2.2.4, if the extended capability offset is
1350 	 * less than 40h then its not valid.  This means we don't need to
1351 	 * worry about BIOS handoff.
1352 	 */
1353 	if (extended_cap_offset < EHCI_HCC_EECP_MIN_OFFSET) {
1354 
1355 		USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1356 		    "ehci_take_control: Hardware doesn't support legacy.");
1357 
1358 		goto success;
1359 	}
1360 
1361 	/*
1362 	 * According EHCI Spec 2.1.7, A zero offset indicates the
1363 	 * end of the extended capability list.
1364 	 */
1365 	while (extended_cap_offset) {
1366 
1367 		/* Get the extended capability value. */
1368 		extended_cap = pci_config_get32(ehcip->ehci_config_handle,
1369 		    extended_cap_offset);
1370 
1371 		/* Get the capability ID */
1372 		extended_cap_id = (extended_cap & EHCI_EX_CAP_ID) >>
1373 		    EHCI_EX_CAP_ID_SHIFT;
1374 
1375 		/* Check if the card support legacy */
1376 		if (extended_cap_id == EHCI_EX_CAP_ID_BIOS_HANDOFF) {
1377 			break;
1378 		}
1379 
1380 		/* Get the offset of the next capability */
1381 		extended_cap_offset = (extended_cap & EHCI_EX_CAP_NEXT_PTR) >>
1382 		    EHCI_EX_CAP_NEXT_PTR_SHIFT;
1383 	}
1384 
1385 	/*
1386 	 * Unable to find legacy support in hardware's extended capability list.
1387 	 * This means we don't need to worry about BIOS handoff.
1388 	 */
1389 	if (extended_cap_id != EHCI_EX_CAP_ID_BIOS_HANDOFF) {
1390 
1391 		USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1392 		    "ehci_take_control: Hardware doesn't support legacy");
1393 
1394 		goto success;
1395 	}
1396 
1397 	/* Check if BIOS has owned it. */
1398 	if (!(extended_cap & EHCI_LEGSUP_BIOS_OWNED_SEM)) {
1399 
1400 		USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1401 		    "ehci_take_control: BIOS does not own EHCI");
1402 
1403 		goto success;
1404 	}
1405 
1406 	/*
1407 	 * According EHCI Spec 5.1, The OS driver initiates an ownership
1408 	 * request by setting the OS Owned semaphore to a one. The OS
1409 	 * waits for the BIOS Owned bit to go to a zero before attempting
1410 	 * to use the EHCI controller. The time that OS must wait for BIOS
1411 	 * to respond to the request for ownership is beyond the scope of
1412 	 * this specification.
1413 	 * It waits up to EHCI_TAKEOVER_WAIT_COUNT*EHCI_TAKEOVER_DELAY ms
1414 	 * for BIOS to release the ownership.
1415 	 */
1416 	extended_cap |= EHCI_LEGSUP_OS_OWNED_SEM;
1417 	pci_config_put32(ehcip->ehci_config_handle, extended_cap_offset,
1418 	    extended_cap);
1419 
1420 	for (retry = 0; retry < EHCI_TAKEOVER_WAIT_COUNT; retry++) {
1421 
1422 		/* wait a special interval */
1423 #ifndef __lock_lint
1424 		delay(drv_usectohz(EHCI_TAKEOVER_DELAY));
1425 #endif
1426 		/* Check to see if the BIOS has released the ownership */
1427 		extended_cap = pci_config_get32(
1428 		    ehcip->ehci_config_handle, extended_cap_offset);
1429 
1430 		if (!(extended_cap & EHCI_LEGSUP_BIOS_OWNED_SEM)) {
1431 
1432 			USB_DPRINTF_L3(PRINT_MASK_ATTA,
1433 			    ehcip->ehci_log_hdl,
1434 			    "ehci_take_control: BIOS has released "
1435 			    "the ownership. retry = %d", retry);
1436 
1437 			goto success;
1438 		}
1439 
1440 	}
1441 
1442 	USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1443 	    "ehci_take_control: take control from BIOS failed.");
1444 
1445 	return (USB_FAILURE);
1446 
1447 success:
1448 
1449 #endif	/* __x86 */
1450 	return (USB_SUCCESS);
1451 }
1452 
1453 
1454 /*
1455  * ehci_init_periodic_frame_list_table :
1456  *
1457  * Allocate the system memory and initialize Host Controller
1458  * Periodic Frame List table area. The starting of the Periodic
1459  * Frame List Table area must be 4096 byte aligned.
1460  */
1461 static int
1462 ehci_init_periodic_frame_lst_table(ehci_state_t *ehcip)
1463 {
1464 	ddi_device_acc_attr_t	dev_attr;
1465 	size_t			real_length;
1466 	uint_t			ccount;
1467 	int			result;
1468 
1469 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
1470 
1471 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1472 	    "ehci_init_periodic_frame_lst_table:");
1473 
1474 	/* The host controller will be little endian */
1475 	dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
1476 	dev_attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
1477 	dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1478 
1479 	/* Force the required 4K restrictive alignment */
1480 	ehcip->ehci_dma_attr.dma_attr_align = EHCI_DMA_ATTR_PFL_ALIGNMENT;
1481 
1482 	/* Create space for the Periodic Frame List */
1483 	if (ddi_dma_alloc_handle(ehcip->ehci_dip, &ehcip->ehci_dma_attr,
1484 	    DDI_DMA_SLEEP, 0, &ehcip->ehci_pflt_dma_handle) != DDI_SUCCESS) {
1485 
1486 		goto failure;
1487 	}
1488 
1489 	if (ddi_dma_mem_alloc(ehcip->ehci_pflt_dma_handle,
1490 	    sizeof (ehci_periodic_frame_list_t),
1491 	    &dev_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
1492 	    0, (caddr_t *)&ehcip->ehci_periodic_frame_list_tablep,
1493 	    &real_length, &ehcip->ehci_pflt_mem_handle)) {
1494 
1495 		goto failure;
1496 	}
1497 
1498 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1499 	    "ehci_init_periodic_frame_lst_table: "
1500 	    "Real length %lu", real_length);
1501 
1502 	/* Map the whole Periodic Frame List into the I/O address space */
1503 	result = ddi_dma_addr_bind_handle(ehcip->ehci_pflt_dma_handle,
1504 	    NULL, (caddr_t)ehcip->ehci_periodic_frame_list_tablep,
1505 	    real_length, DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
1506 	    DDI_DMA_SLEEP, NULL, &ehcip->ehci_pflt_cookie, &ccount);
1507 
1508 	if (result == DDI_DMA_MAPPED) {
1509 		/* The cookie count should be 1 */
1510 		if (ccount != 1) {
1511 			USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1512 			    "ehci_init_periodic_frame_lst_table: "
1513 			    "More than 1 cookie");
1514 
1515 			goto failure;
1516 		}
1517 	} else {
1518 		ehci_decode_ddi_dma_addr_bind_handle_result(ehcip, result);
1519 
1520 		goto failure;
1521 	}
1522 
1523 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1524 	    "ehci_init_periodic_frame_lst_table: virtual 0x%p physical 0x%x",
1525 	    (void *)ehcip->ehci_periodic_frame_list_tablep,
1526 	    ehcip->ehci_pflt_cookie.dmac_address);
1527 
1528 	/*
1529 	 * DMA addresses for Periodic Frame List are bound.
1530 	 */
1531 	ehcip->ehci_dma_addr_bind_flag |= EHCI_PFLT_DMA_BOUND;
1532 
1533 	bzero((void *)ehcip->ehci_periodic_frame_list_tablep, real_length);
1534 
1535 	/* Initialize the Periodic Frame List */
1536 	ehci_build_interrupt_lattice(ehcip);
1537 
1538 	/* Reset Byte Alignment to Default */
1539 	ehcip->ehci_dma_attr.dma_attr_align = EHCI_DMA_ATTR_ALIGNMENT;
1540 
1541 	return (DDI_SUCCESS);
1542 failure:
1543 	/* Byte alignment */
1544 	ehcip->ehci_dma_attr.dma_attr_align = EHCI_DMA_ATTR_ALIGNMENT;
1545 
1546 	return (DDI_FAILURE);
1547 }
1548 
1549 
1550 /*
1551  * ehci_build_interrupt_lattice:
1552  *
1553  * Construct the interrupt lattice tree using static Endpoint Descriptors
1554  * (QH). This interrupt lattice tree will have total of 32 interrupt  QH
1555  * lists and the Host Controller (HC) processes one interrupt QH list in
1556  * every frame. The Host Controller traverses the periodic schedule by
1557  * constructing an array offset reference from the Periodic List Base Address
1558  * register and bits 12 to 3 of Frame Index register. It fetches the element
1559  * and begins traversing the graph of linked schedule data structures.
1560  */
1561 static void
1562 ehci_build_interrupt_lattice(ehci_state_t	*ehcip)
1563 {
1564 	ehci_qh_t	*list_array = ehcip->ehci_qh_pool_addr;
1565 	ushort_t	ehci_index[EHCI_NUM_PERIODIC_FRAME_LISTS];
1566 	ehci_periodic_frame_list_t *periodic_frame_list =
1567 			    ehcip->ehci_periodic_frame_list_tablep;
1568 	ushort_t	*temp, num_of_nodes;
1569 	uintptr_t	addr;
1570 	int		i, j, k;
1571 
1572 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1573 	    "ehci_build_interrupt_lattice:");
1574 
1575 	/*
1576 	 * Reserve the first 63 Endpoint Descriptor (QH) structures
1577 	 * in the pool as static endpoints & these are required for
1578 	 * constructing interrupt lattice tree.
1579 	 */
1580 	for (i = 0; i < EHCI_NUM_STATIC_NODES; i++) {
1581 		Set_QH(list_array[i].qh_state, EHCI_QH_STATIC);
1582 		Set_QH(list_array[i].qh_status, EHCI_QH_STS_HALTED);
1583 		Set_QH(list_array[i].qh_next_qtd, EHCI_QH_NEXT_QTD_PTR_VALID);
1584 		Set_QH(list_array[i].qh_alt_next_qtd,
1585 		    EHCI_QH_ALT_NEXT_QTD_PTR_VALID);
1586 	}
1587 
1588 	/*
1589 	 * Make sure that last Endpoint on the periodic frame list terminates
1590 	 * periodic schedule.
1591 	 */
1592 	Set_QH(list_array[0].qh_link_ptr, EHCI_QH_LINK_PTR_VALID);
1593 
1594 	/* Build the interrupt lattice tree */
1595 	for (i = 0; i < (EHCI_NUM_STATIC_NODES / 2); i++) {
1596 		/*
1597 		 * The next  pointer in the host controller  endpoint
1598 		 * descriptor must contain an iommu address. Calculate
1599 		 * the offset into the cpu address and add this to the
1600 		 * starting iommu address.
1601 		 */
1602 		addr = ehci_qh_cpu_to_iommu(ehcip, (ehci_qh_t *)&list_array[i]);
1603 
1604 		Set_QH(list_array[2*i + 1].qh_link_ptr,
1605 		    addr | EHCI_QH_LINK_REF_QH);
1606 		Set_QH(list_array[2*i + 2].qh_link_ptr,
1607 		    addr | EHCI_QH_LINK_REF_QH);
1608 	}
1609 
1610 	/* Build the tree bottom */
1611 	temp = (unsigned short *)
1612 	    kmem_zalloc(EHCI_NUM_PERIODIC_FRAME_LISTS * 2, KM_SLEEP);
1613 
1614 	num_of_nodes = 1;
1615 
1616 	/*
1617 	 * Initialize the values which are used for setting up head pointers
1618 	 * for the 32ms scheduling lists which starts from the Periodic Frame
1619 	 * List.
1620 	 */
1621 	for (i = 0; i < ehci_log_2(EHCI_NUM_PERIODIC_FRAME_LISTS); i++) {
1622 		for (j = 0, k = 0; k < num_of_nodes; k++, j++) {
1623 			ehci_index[j++] = temp[k];
1624 			ehci_index[j]	= temp[k] + ehci_pow_2(i);
1625 		}
1626 
1627 		num_of_nodes *= 2;
1628 		for (k = 0; k < num_of_nodes; k++)
1629 			temp[k] = ehci_index[k];
1630 	}
1631 
1632 	kmem_free((void *)temp, (EHCI_NUM_PERIODIC_FRAME_LISTS * 2));
1633 
1634 	/*
1635 	 * Initialize the interrupt list in the Periodic Frame List Table
1636 	 * so that it points to the bottom of the tree.
1637 	 */
1638 	for (i = 0, j = 0; i < ehci_pow_2(TREE_HEIGHT); i++) {
1639 		addr = ehci_qh_cpu_to_iommu(ehcip, (ehci_qh_t *)
1640 		    (&list_array[((EHCI_NUM_STATIC_NODES + 1) / 2) + i - 1]));
1641 
1642 		ASSERT(addr);
1643 
1644 		for (k = 0; k < ehci_pow_2(TREE_HEIGHT); k++) {
1645 			Set_PFLT(periodic_frame_list->
1646 			    ehci_periodic_frame_list_table[ehci_index[j++]],
1647 			    (uint32_t)(addr | EHCI_QH_LINK_REF_QH));
1648 		}
1649 	}
1650 }
1651 
1652 
1653 /*
1654  * ehci_alloc_hcdi_ops:
1655  *
1656  * The HCDI interfaces or entry points are the software interfaces used by
1657  * the Universal Serial Bus Driver  (USBA) to  access the services of the
1658  * Host Controller Driver (HCD).  During HCD initialization, inform  USBA
1659  * about all available HCDI interfaces or entry points.
1660  */
1661 usba_hcdi_ops_t *
1662 ehci_alloc_hcdi_ops(ehci_state_t	*ehcip)
1663 {
1664 	usba_hcdi_ops_t			*usba_hcdi_ops;
1665 
1666 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1667 	    "ehci_alloc_hcdi_ops:");
1668 
1669 	usba_hcdi_ops = usba_alloc_hcdi_ops();
1670 
1671 	usba_hcdi_ops->usba_hcdi_ops_version = HCDI_OPS_VERSION;
1672 
1673 	usba_hcdi_ops->usba_hcdi_pm_support = ehci_hcdi_pm_support;
1674 	usba_hcdi_ops->usba_hcdi_pipe_open = ehci_hcdi_pipe_open;
1675 	usba_hcdi_ops->usba_hcdi_pipe_close = ehci_hcdi_pipe_close;
1676 
1677 	usba_hcdi_ops->usba_hcdi_pipe_reset = ehci_hcdi_pipe_reset;
1678 
1679 	usba_hcdi_ops->usba_hcdi_pipe_ctrl_xfer = ehci_hcdi_pipe_ctrl_xfer;
1680 	usba_hcdi_ops->usba_hcdi_pipe_bulk_xfer = ehci_hcdi_pipe_bulk_xfer;
1681 	usba_hcdi_ops->usba_hcdi_pipe_intr_xfer = ehci_hcdi_pipe_intr_xfer;
1682 	usba_hcdi_ops->usba_hcdi_pipe_isoc_xfer = ehci_hcdi_pipe_isoc_xfer;
1683 
1684 	usba_hcdi_ops->usba_hcdi_bulk_transfer_size =
1685 					ehci_hcdi_bulk_transfer_size;
1686 
1687 	usba_hcdi_ops->usba_hcdi_pipe_stop_intr_polling =
1688 					ehci_hcdi_pipe_stop_intr_polling;
1689 	usba_hcdi_ops->usba_hcdi_pipe_stop_isoc_polling =
1690 					ehci_hcdi_pipe_stop_isoc_polling;
1691 
1692 	usba_hcdi_ops->usba_hcdi_get_current_frame_number =
1693 					ehci_hcdi_get_current_frame_number;
1694 	usba_hcdi_ops->usba_hcdi_get_max_isoc_pkts =
1695 					ehci_hcdi_get_max_isoc_pkts;
1696 
1697 	usba_hcdi_ops->usba_hcdi_console_input_init =
1698 					ehci_hcdi_polled_input_init;
1699 	usba_hcdi_ops->usba_hcdi_console_input_enter =
1700 					ehci_hcdi_polled_input_enter;
1701 	usba_hcdi_ops->usba_hcdi_console_read =
1702 					ehci_hcdi_polled_read;
1703 	usba_hcdi_ops->usba_hcdi_console_input_exit =
1704 					ehci_hcdi_polled_input_exit;
1705 	usba_hcdi_ops->usba_hcdi_console_input_fini =
1706 					ehci_hcdi_polled_input_fini;
1707 	return (usba_hcdi_ops);
1708 }
1709 
1710 
1711 /*
1712  * Host Controller Driver (HCD) deinitialization functions
1713  */
1714 
1715 /*
1716  * ehci_cleanup:
1717  *
1718  * Cleanup on attach failure or detach
1719  */
1720 int
1721 ehci_cleanup(ehci_state_t	*ehcip)
1722 {
1723 	ehci_trans_wrapper_t	*tw;
1724 	ehci_pipe_private_t	*pp;
1725 	ehci_qtd_t		*qtd;
1726 	int			i, ctrl, rval;
1727 	int			flags = ehcip->ehci_flags;
1728 
1729 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl, "ehci_cleanup:");
1730 
1731 	if (flags & EHCI_RHREG) {
1732 		/* Unload the root hub driver */
1733 		if (ehci_unload_root_hub_driver(ehcip) != USB_SUCCESS) {
1734 
1735 			return (DDI_FAILURE);
1736 		}
1737 	}
1738 
1739 	if (flags & EHCI_USBAREG) {
1740 		/* Unregister this HCD instance with USBA */
1741 		usba_hcdi_unregister(ehcip->ehci_dip);
1742 	}
1743 
1744 	if (flags & EHCI_INTR) {
1745 
1746 		mutex_enter(&ehcip->ehci_int_mutex);
1747 
1748 		/* Disable all EHCI QH list processing */
1749 		Set_OpReg(ehci_command, (Get_OpReg(ehci_command) &
1750 		    ~(EHCI_CMD_ASYNC_SCHED_ENABLE |
1751 		    EHCI_CMD_PERIODIC_SCHED_ENABLE)));
1752 
1753 		/* Disable all EHCI interrupts */
1754 		Set_OpReg(ehci_interrupt, 0);
1755 
1756 		/* wait for the next SOF */
1757 		(void) ehci_wait_for_sof(ehcip);
1758 
1759 		/* Route all Root hub ports to Classic host controller */
1760 		Set_OpReg(ehci_config_flag, EHCI_CONFIG_FLAG_CLASSIC);
1761 
1762 		/* Stop the EHCI host controller */
1763 		Set_OpReg(ehci_command,
1764 		    Get_OpReg(ehci_command) & ~EHCI_CMD_HOST_CTRL_RUN);
1765 
1766 		/* Wait for sometime */
1767 		drv_usecwait(EHCI_TIMEWAIT);
1768 
1769 		mutex_exit(&ehcip->ehci_int_mutex);
1770 
1771 		ehci_rem_intrs(ehcip);
1772 	}
1773 
1774 	/* Unmap the EHCI registers */
1775 	if (ehcip->ehci_caps_handle) {
1776 		ddi_regs_map_free(&ehcip->ehci_caps_handle);
1777 	}
1778 
1779 	if (ehcip->ehci_config_handle) {
1780 		pci_config_teardown(&ehcip->ehci_config_handle);
1781 	}
1782 
1783 	/* Free all the buffers */
1784 	if (ehcip->ehci_qtd_pool_addr && ehcip->ehci_qtd_pool_mem_handle) {
1785 		for (i = 0; i < ehci_qtd_pool_size; i ++) {
1786 			qtd = &ehcip->ehci_qtd_pool_addr[i];
1787 			ctrl = Get_QTD(ehcip->
1788 			    ehci_qtd_pool_addr[i].qtd_state);
1789 
1790 			if ((ctrl != EHCI_QTD_FREE) &&
1791 			    (ctrl != EHCI_QTD_DUMMY) &&
1792 			    (qtd->qtd_trans_wrapper)) {
1793 
1794 				mutex_enter(&ehcip->ehci_int_mutex);
1795 
1796 				tw = (ehci_trans_wrapper_t *)
1797 					EHCI_LOOKUP_ID((uint32_t)
1798 					Get_QTD(qtd->qtd_trans_wrapper));
1799 
1800 				/* Obtain the pipe private structure */
1801 				pp = tw->tw_pipe_private;
1802 
1803 				/* Stop the the transfer timer */
1804 				ehci_stop_xfer_timer(ehcip, tw,
1805 						EHCI_REMOVE_XFER_ALWAYS);
1806 
1807 				ehci_deallocate_tw(ehcip, pp, tw);
1808 
1809 				mutex_exit(&ehcip->ehci_int_mutex);
1810 			}
1811 		}
1812 
1813 		/*
1814 		 * If EHCI_QTD_POOL_BOUND flag is set, then unbind
1815 		 * the handle for QTD pools.
1816 		 */
1817 		if ((ehcip->ehci_dma_addr_bind_flag &
1818 		    EHCI_QTD_POOL_BOUND) == EHCI_QTD_POOL_BOUND) {
1819 
1820 			rval = ddi_dma_unbind_handle(
1821 			    ehcip->ehci_qtd_pool_dma_handle);
1822 
1823 			ASSERT(rval == DDI_SUCCESS);
1824 		}
1825 		ddi_dma_mem_free(&ehcip->ehci_qtd_pool_mem_handle);
1826 	}
1827 
1828 	/* Free the QTD pool */
1829 	if (ehcip->ehci_qtd_pool_dma_handle) {
1830 		ddi_dma_free_handle(&ehcip->ehci_qtd_pool_dma_handle);
1831 	}
1832 
1833 	if (ehcip->ehci_qh_pool_addr && ehcip->ehci_qh_pool_mem_handle) {
1834 		/*
1835 		 * If EHCI_QH_POOL_BOUND flag is set, then unbind
1836 		 * the handle for QH pools.
1837 		 */
1838 		if ((ehcip->ehci_dma_addr_bind_flag &
1839 		    EHCI_QH_POOL_BOUND) == EHCI_QH_POOL_BOUND) {
1840 
1841 			rval = ddi_dma_unbind_handle(
1842 			    ehcip->ehci_qh_pool_dma_handle);
1843 
1844 			ASSERT(rval == DDI_SUCCESS);
1845 		}
1846 
1847 		ddi_dma_mem_free(&ehcip->ehci_qh_pool_mem_handle);
1848 	}
1849 
1850 	/* Free the QH pool */
1851 	if (ehcip->ehci_qh_pool_dma_handle) {
1852 		ddi_dma_free_handle(&ehcip->ehci_qh_pool_dma_handle);
1853 	}
1854 
1855 	/* Free the Periodic frame list table (PFLT) area */
1856 	if (ehcip->ehci_periodic_frame_list_tablep &&
1857 	    ehcip->ehci_pflt_mem_handle) {
1858 		/*
1859 		 * If EHCI_PFLT_DMA_BOUND flag is set, then unbind
1860 		 * the handle for PFLT.
1861 		 */
1862 		if ((ehcip->ehci_dma_addr_bind_flag &
1863 		    EHCI_PFLT_DMA_BOUND) == EHCI_PFLT_DMA_BOUND) {
1864 
1865 			rval = ddi_dma_unbind_handle(
1866 			    ehcip->ehci_pflt_dma_handle);
1867 
1868 			ASSERT(rval == DDI_SUCCESS);
1869 		}
1870 
1871 		ddi_dma_mem_free(&ehcip->ehci_pflt_mem_handle);
1872 	}
1873 
1874 	(void) ehci_isoc_cleanup(ehcip);
1875 
1876 	if (ehcip->ehci_pflt_dma_handle) {
1877 		ddi_dma_free_handle(&ehcip->ehci_pflt_dma_handle);
1878 	}
1879 
1880 	if (flags & EHCI_INTR) {
1881 		/* Destroy the mutex */
1882 		mutex_destroy(&ehcip->ehci_int_mutex);
1883 
1884 		/* Destroy the async schedule advance condition variable */
1885 		cv_destroy(&ehcip->ehci_async_schedule_advance_cv);
1886 	}
1887 
1888 	/* clean up kstat structs */
1889 	ehci_destroy_stats(ehcip);
1890 
1891 	/* Free ehci hcdi ops */
1892 	if (ehcip->ehci_hcdi_ops) {
1893 		usba_free_hcdi_ops(ehcip->ehci_hcdi_ops);
1894 	}
1895 
1896 	if (flags & EHCI_ZALLOC) {
1897 
1898 		usb_free_log_hdl(ehcip->ehci_log_hdl);
1899 
1900 		/* Remove all properties that might have been created */
1901 		ddi_prop_remove_all(ehcip->ehci_dip);
1902 
1903 		/* Free the soft state */
1904 		ddi_soft_state_free(ehci_statep,
1905 			ddi_get_instance(ehcip->ehci_dip));
1906 	}
1907 
1908 	return (DDI_SUCCESS);
1909 }
1910 
1911 
1912 /*
1913  * ehci_rem_intrs:
1914  *
1915  * Unregister FIXED or MSI interrupts
1916  */
1917 static void
1918 ehci_rem_intrs(ehci_state_t	*ehcip)
1919 {
1920 	int	i;
1921 
1922 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1923 	    "ehci_rem_intrs: interrupt type 0x%x", ehcip->ehci_intr_type);
1924 
1925 	/* Disable all interrupts */
1926 	if (ehcip->ehci_intr_cap & DDI_INTR_FLAG_BLOCK) {
1927 		(void) ddi_intr_block_disable(ehcip->ehci_htable,
1928 		    ehcip->ehci_intr_cnt);
1929 	} else {
1930 		for (i = 0; i < ehcip->ehci_intr_cnt; i++) {
1931 			(void) ddi_intr_disable(ehcip->ehci_htable[i]);
1932 		}
1933 	}
1934 
1935 	/* Call ddi_intr_remove_handler() */
1936 	for (i = 0; i < ehcip->ehci_intr_cnt; i++) {
1937 		(void) ddi_intr_remove_handler(ehcip->ehci_htable[i]);
1938 		(void) ddi_intr_free(ehcip->ehci_htable[i]);
1939 	}
1940 
1941 	kmem_free(ehcip->ehci_htable,
1942 	    ehcip->ehci_intr_cnt * sizeof (ddi_intr_handle_t));
1943 }
1944 
1945 
1946 /*
1947  * ehci_cpr_suspend
1948  */
1949 int
1950 ehci_cpr_suspend(ehci_state_t	*ehcip)
1951 {
1952 	int	i;
1953 
1954 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1955 	    "ehci_cpr_suspend:");
1956 
1957 	/* Call into the root hub and suspend it */
1958 	if (usba_hubdi_detach(ehcip->ehci_dip, DDI_SUSPEND) != DDI_SUCCESS) {
1959 
1960 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1961 			"ehci_cpr_suspend: root hub fails to suspend");
1962 
1963 		return (DDI_FAILURE);
1964 	}
1965 
1966 	/* Only root hub's intr pipe should be open at this time */
1967 	mutex_enter(&ehcip->ehci_int_mutex);
1968 
1969 	ASSERT(ehcip->ehci_open_pipe_count == 0);
1970 
1971 	/* Just wait till all resources are reclaimed */
1972 	i = 0;
1973 	while ((ehcip->ehci_reclaim_list != NULL) && (i++ < 3)) {
1974 		ehci_handle_endpoint_reclaimation(ehcip);
1975 		(void) ehci_wait_for_sof(ehcip);
1976 	}
1977 	ASSERT(ehcip->ehci_reclaim_list == NULL);
1978 
1979 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1980 	    "ehci_cpr_suspend: Disable HC QH list processing");
1981 
1982 	/* Disable all EHCI QH list processing */
1983 	Set_OpReg(ehci_command, (Get_OpReg(ehci_command) &
1984 	    ~(EHCI_CMD_ASYNC_SCHED_ENABLE | EHCI_CMD_PERIODIC_SCHED_ENABLE)));
1985 
1986 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1987 	    "ehci_cpr_suspend: Disable HC interrupts");
1988 
1989 	/* Disable all EHCI interrupts */
1990 	Set_OpReg(ehci_interrupt, 0);
1991 
1992 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1993 	    "ehci_cpr_suspend: Wait for the next SOF");
1994 
1995 	/* Wait for the next SOF */
1996 	if (ehci_wait_for_sof(ehcip) != USB_SUCCESS) {
1997 
1998 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
1999 		    "ehci_cpr_suspend: ehci host controller suspend failed");
2000 
2001 		mutex_exit(&ehcip->ehci_int_mutex);
2002 		return (DDI_FAILURE);
2003 	}
2004 
2005 	/*
2006 	 * Stop the ehci host controller
2007 	 * if usb keyboard is not connected.
2008 	 */
2009 	if (ehcip->ehci_polled_kbd_count == 0) {
2010 		Set_OpReg(ehci_command,
2011 		    Get_OpReg(ehci_command) & ~EHCI_CMD_HOST_CTRL_RUN);
2012 	}
2013 
2014 	/* Set host controller soft state to suspend */
2015 	ehcip->ehci_hc_soft_state = EHCI_CTLR_SUSPEND_STATE;
2016 
2017 	mutex_exit(&ehcip->ehci_int_mutex);
2018 
2019 	return (DDI_SUCCESS);
2020 }
2021 
2022 
2023 /*
2024  * ehci_cpr_resume
2025  */
2026 int
2027 ehci_cpr_resume(ehci_state_t	*ehcip)
2028 {
2029 	mutex_enter(&ehcip->ehci_int_mutex);
2030 
2031 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
2032 	    "ehci_cpr_resume: Restart the controller");
2033 
2034 	/* Cleanup ehci specific information across cpr */
2035 	ehci_cpr_cleanup(ehcip);
2036 
2037 	/* Restart the controller */
2038 	if (ehci_init_ctlr(ehcip, EHCI_NORMAL_INITIALIZATION) != DDI_SUCCESS) {
2039 
2040 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
2041 		    "ehci_cpr_resume: ehci host controller resume failed ");
2042 
2043 		mutex_exit(&ehcip->ehci_int_mutex);
2044 
2045 		return (DDI_FAILURE);
2046 	}
2047 
2048 	mutex_exit(&ehcip->ehci_int_mutex);
2049 
2050 	/* Now resume the root hub */
2051 	if (usba_hubdi_attach(ehcip->ehci_dip, DDI_RESUME) != DDI_SUCCESS) {
2052 
2053 		return (DDI_FAILURE);
2054 	}
2055 
2056 	return (DDI_SUCCESS);
2057 }
2058 
2059 
2060 /*
2061  * Bandwidth Allocation functions
2062  */
2063 
2064 /*
2065  * ehci_allocate_bandwidth:
2066  *
2067  * Figure out whether or not this interval may be supported. Return the index
2068  * into the  lattice if it can be supported.  Return allocation failure if it
2069  * can not be supported.
2070  */
2071 int
2072 ehci_allocate_bandwidth(
2073 	ehci_state_t		*ehcip,
2074 	usba_pipe_handle_data_t	*ph,
2075 	uint_t			*pnode,
2076 	uchar_t			*smask,
2077 	uchar_t			*cmask)
2078 {
2079 	int			error = USB_SUCCESS;
2080 
2081 	/* This routine is protected by the ehci_int_mutex */
2082 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
2083 
2084 	/* Reset the pnode to the last checked pnode */
2085 	*pnode = 0;
2086 
2087 	/* Allocate high speed bandwidth */
2088 	if ((error = ehci_allocate_high_speed_bandwidth(ehcip,
2089 	    ph, pnode, smask, cmask)) != USB_SUCCESS) {
2090 
2091 		return (error);
2092 	}
2093 
2094 	/*
2095 	 * For low/full speed usb devices, allocate classic TT bandwidth
2096 	 * in additional to high speed bandwidth.
2097 	 */
2098 	if (ph->p_usba_device->usb_port_status != USBA_HIGH_SPEED_DEV) {
2099 
2100 		/* Allocate classic TT bandwidth */
2101 		if ((error = ehci_allocate_classic_tt_bandwidth(
2102 		    ehcip, ph, *pnode)) != USB_SUCCESS) {
2103 
2104 			/* Deallocate high speed bandwidth */
2105 			ehci_deallocate_high_speed_bandwidth(
2106 			    ehcip, ph, *pnode, *smask, *cmask);
2107 		}
2108 	}
2109 
2110 	return (error);
2111 }
2112 
2113 
2114 /*
2115  * ehci_allocate_high_speed_bandwidth:
2116  *
2117  * Allocate high speed bandwidth for the low/full/high speed interrupt and
2118  * isochronous endpoints.
2119  */
2120 static int
2121 ehci_allocate_high_speed_bandwidth(
2122 	ehci_state_t		*ehcip,
2123 	usba_pipe_handle_data_t	*ph,
2124 	uint_t			*pnode,
2125 	uchar_t			*smask,
2126 	uchar_t			*cmask)
2127 {
2128 	uint_t			sbandwidth, cbandwidth;
2129 	int			interval;
2130 	usb_ep_descr_t		*endpoint = &ph->p_ep;
2131 	usba_device_t		*child_ud;
2132 	usb_port_status_t	port_status;
2133 	int			error;
2134 
2135 	/* This routine is protected by the ehci_int_mutex */
2136 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
2137 
2138 	/* Get child's usba device structure */
2139 	child_ud = ph->p_usba_device;
2140 
2141 	mutex_enter(&child_ud->usb_mutex);
2142 
2143 	/* Get the current usb device's port status */
2144 	port_status = ph->p_usba_device->usb_port_status;
2145 
2146 	mutex_exit(&child_ud->usb_mutex);
2147 
2148 	/*
2149 	 * Calculate the length in bytes of a transaction on this
2150 	 * periodic endpoint. Return failure if maximum packet is
2151 	 * zero.
2152 	 */
2153 	error = ehci_compute_high_speed_bandwidth(ehcip, endpoint,
2154 	    port_status, &sbandwidth, &cbandwidth);
2155 	if (error != USB_SUCCESS) {
2156 
2157 		return (error);
2158 	}
2159 
2160 	/*
2161 	 * Adjust polling interval to be a power of 2.
2162 	 * If this interval can't be supported, return
2163 	 * allocation failure.
2164 	 */
2165 	interval = ehci_adjust_polling_interval(ehcip, endpoint, port_status);
2166 	if (interval == USB_FAILURE) {
2167 
2168 		return (USB_FAILURE);
2169 	}
2170 
2171 	if (port_status == USBA_HIGH_SPEED_DEV) {
2172 		/* Allocate bandwidth for high speed devices */
2173 		if ((endpoint->bmAttributes & USB_EP_ATTR_MASK) ==
2174 		    USB_EP_ATTR_ISOCH) {
2175 			error = USB_SUCCESS;
2176 		} else {
2177 
2178 			error = ehci_find_bestfit_hs_mask(ehcip, smask, pnode,
2179 			    endpoint, sbandwidth, interval);
2180 		}
2181 
2182 		*cmask = 0x00;
2183 
2184 	} else {
2185 		if ((endpoint->bmAttributes & USB_EP_ATTR_MASK) ==
2186 		    USB_EP_ATTR_INTR) {
2187 
2188 			/* Allocate bandwidth for low speed interrupt */
2189 			error = ehci_find_bestfit_ls_intr_mask(ehcip,
2190 			    smask, cmask, pnode, sbandwidth, cbandwidth,
2191 			    interval);
2192 		} else {
2193 			if ((endpoint->bEndpointAddress &
2194 			    USB_EP_DIR_MASK) == USB_EP_DIR_IN) {
2195 
2196 				/* Allocate bandwidth for sitd in */
2197 				error = ehci_find_bestfit_sitd_in_mask(ehcip,
2198 				    smask, cmask, pnode, sbandwidth, cbandwidth,
2199 				    interval);
2200 			} else {
2201 
2202 				/* Allocate bandwidth for sitd out */
2203 				error = ehci_find_bestfit_sitd_out_mask(ehcip,
2204 				    smask, pnode, sbandwidth, interval);
2205 				*cmask = 0x00;
2206 			}
2207 		}
2208 	}
2209 
2210 	if (error != USB_SUCCESS) {
2211 		USB_DPRINTF_L2(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2212 		    "ehci_allocate_high_speed_bandwidth: Reached maximum "
2213 		    "bandwidth value and cannot allocate bandwidth for a "
2214 		    "given high-speed periodic endpoint");
2215 
2216 		return (USB_NO_BANDWIDTH);
2217 	}
2218 
2219 	return (error);
2220 }
2221 
2222 
2223 /*
2224  * ehci_allocate_classic_tt_speed_bandwidth:
2225  *
2226  * Allocate classic TT bandwidth for the low/full speed interrupt and
2227  * isochronous endpoints.
2228  */
2229 static int
2230 ehci_allocate_classic_tt_bandwidth(
2231 	ehci_state_t		*ehcip,
2232 	usba_pipe_handle_data_t	*ph,
2233 	uint_t			pnode)
2234 {
2235 	uint_t			bandwidth, min;
2236 	uint_t			height, leftmost, list;
2237 	usb_ep_descr_t		*endpoint = &ph->p_ep;
2238 	usba_device_t		*child_ud, *parent_ud;
2239 	usb_port_status_t	port_status;
2240 	int			i, interval;
2241 
2242 	/* This routine is protected by the ehci_int_mutex */
2243 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
2244 
2245 	/* Get child's usba device structure */
2246 	child_ud = ph->p_usba_device;
2247 
2248 	mutex_enter(&child_ud->usb_mutex);
2249 
2250 	/* Get the current usb device's port status */
2251 	port_status = child_ud->usb_port_status;
2252 
2253 	/* Get the parent high speed hub's usba device structure */
2254 	parent_ud = child_ud->usb_hs_hub_usba_dev;
2255 
2256 	mutex_exit(&child_ud->usb_mutex);
2257 
2258 	USB_DPRINTF_L3(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2259 	    "ehci_allocate_classic_tt_bandwidth: "
2260 	    "child_ud 0x%p parent_ud 0x%p", child_ud, parent_ud);
2261 
2262 	/*
2263 	 * Calculate the length in bytes of a transaction on this
2264 	 * periodic endpoint. Return failure if maximum packet is
2265 	 * zero.
2266 	 */
2267 	if (ehci_compute_classic_bandwidth(endpoint,
2268 	    port_status, &bandwidth) != USB_SUCCESS) {
2269 
2270 		USB_DPRINTF_L2(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2271 		    "ehci_allocate_classic_tt_bandwidth: Periodic endpoint "
2272 		    "with zero endpoint maximum packet size is not supported");
2273 
2274 		return (USB_NOT_SUPPORTED);
2275 	}
2276 
2277 	USB_DPRINTF_L3(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2278 	    "ehci_allocate_classic_tt_bandwidth: bandwidth %d", bandwidth);
2279 
2280 	mutex_enter(&parent_ud->usb_mutex);
2281 
2282 	/*
2283 	 * If the length in bytes plus the allocated bandwidth exceeds
2284 	 * the maximum, return bandwidth allocation failure.
2285 	 */
2286 	if ((parent_ud->usb_hs_hub_min_bandwidth + bandwidth) >
2287 	    FS_PERIODIC_BANDWIDTH) {
2288 
2289 		mutex_exit(&parent_ud->usb_mutex);
2290 
2291 		USB_DPRINTF_L2(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2292 		    "ehci_allocate_classic_tt_bandwidth: Reached maximum "
2293 		    "bandwidth value and cannot allocate bandwidth for a "
2294 		    "given low/full speed periodic endpoint");
2295 
2296 		return (USB_NO_BANDWIDTH);
2297 	}
2298 
2299 	mutex_exit(&parent_ud->usb_mutex);
2300 
2301 	/* Adjust polling interval to be a power of 2 */
2302 	interval = ehci_adjust_polling_interval(ehcip, endpoint, port_status);
2303 
2304 	/* Find the height in the tree */
2305 	height = ehci_lattice_height(interval);
2306 
2307 	/* Find the leftmost leaf in the subtree specified by the node. */
2308 	leftmost = ehci_leftmost_leaf(pnode, height);
2309 
2310 	mutex_enter(&parent_ud->usb_mutex);
2311 
2312 	for (i = 0; i < (EHCI_NUM_INTR_QH_LISTS/interval); i++) {
2313 		list = ehci_index[leftmost + i];
2314 
2315 		if ((parent_ud->usb_hs_hub_bandwidth[list] +
2316 		    bandwidth) > FS_PERIODIC_BANDWIDTH) {
2317 
2318 			mutex_exit(&parent_ud->usb_mutex);
2319 
2320 			USB_DPRINTF_L2(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2321 			    "ehci_allocate_classic_tt_bandwidth: Reached "
2322 			    "maximum bandwidth value and cannot allocate "
2323 			    "bandwidth for low/full periodic endpoint");
2324 
2325 			return (USB_NO_BANDWIDTH);
2326 		}
2327 	}
2328 
2329 	/*
2330 	 * All the leaves for this node must be updated with the bandwidth.
2331 	 */
2332 	for (i = 0; i < (EHCI_NUM_INTR_QH_LISTS/interval); i++) {
2333 		list = ehci_index[leftmost + i];
2334 		parent_ud->usb_hs_hub_bandwidth[list] += bandwidth;
2335 	}
2336 
2337 	/* Find the leaf with the smallest allocated bandwidth */
2338 	min = parent_ud->usb_hs_hub_bandwidth[0];
2339 
2340 	for (i = 1; i < EHCI_NUM_INTR_QH_LISTS; i++) {
2341 		if (parent_ud->usb_hs_hub_bandwidth[i] < min) {
2342 			min = parent_ud->usb_hs_hub_bandwidth[i];
2343 		}
2344 	}
2345 
2346 	/* Save the minimum for later use */
2347 	parent_ud->usb_hs_hub_min_bandwidth = min;
2348 
2349 	mutex_exit(&parent_ud->usb_mutex);
2350 
2351 	return (USB_SUCCESS);
2352 }
2353 
2354 
2355 /*
2356  * ehci_deallocate_bandwidth:
2357  *
2358  * Deallocate bandwidth for the given node in the lattice and the length
2359  * of transfer.
2360  */
2361 void
2362 ehci_deallocate_bandwidth(
2363 	ehci_state_t		*ehcip,
2364 	usba_pipe_handle_data_t	*ph,
2365 	uint_t			pnode,
2366 	uchar_t			smask,
2367 	uchar_t			cmask)
2368 {
2369 	/* This routine is protected by the ehci_int_mutex */
2370 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
2371 
2372 	ehci_deallocate_high_speed_bandwidth(ehcip, ph, pnode, smask, cmask);
2373 
2374 	/*
2375 	 * For low/full speed usb devices, deallocate classic TT bandwidth
2376 	 * in additional to high speed bandwidth.
2377 	 */
2378 	if (ph->p_usba_device->usb_port_status != USBA_HIGH_SPEED_DEV) {
2379 
2380 		/* Deallocate classic TT bandwidth */
2381 		ehci_deallocate_classic_tt_bandwidth(ehcip, ph, pnode);
2382 	}
2383 }
2384 
2385 
2386 /*
2387  * ehci_deallocate_high_speed_bandwidth:
2388  *
2389  * Deallocate high speed bandwidth of a interrupt or isochronous endpoint.
2390  */
2391 static void
2392 ehci_deallocate_high_speed_bandwidth(
2393 	ehci_state_t		*ehcip,
2394 	usba_pipe_handle_data_t	*ph,
2395 	uint_t			pnode,
2396 	uchar_t			smask,
2397 	uchar_t			cmask)
2398 {
2399 	uint_t			height, leftmost;
2400 	uint_t			list_count;
2401 	uint_t			sbandwidth, cbandwidth;
2402 	int			interval;
2403 	usb_ep_descr_t		*endpoint = &ph->p_ep;
2404 	usba_device_t		*child_ud;
2405 	usb_port_status_t	port_status;
2406 
2407 	/* This routine is protected by the ehci_int_mutex */
2408 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
2409 
2410 	/* Get child's usba device structure */
2411 	child_ud = ph->p_usba_device;
2412 
2413 	mutex_enter(&child_ud->usb_mutex);
2414 
2415 	/* Get the current usb device's port status */
2416 	port_status = ph->p_usba_device->usb_port_status;
2417 
2418 	mutex_exit(&child_ud->usb_mutex);
2419 
2420 	(void) ehci_compute_high_speed_bandwidth(ehcip, endpoint,
2421 	    port_status, &sbandwidth, &cbandwidth);
2422 
2423 	/* Adjust polling interval to be a power of 2 */
2424 	interval = ehci_adjust_polling_interval(ehcip, endpoint, port_status);
2425 
2426 	/* Find the height in the tree */
2427 	height = ehci_lattice_height(interval);
2428 
2429 	/*
2430 	 * Find the leftmost leaf in the subtree specified by the node
2431 	 */
2432 	leftmost = ehci_leftmost_leaf(pnode, height);
2433 
2434 	list_count = EHCI_NUM_INTR_QH_LISTS/interval;
2435 
2436 	/* Delete the bandwidth from the appropriate lists */
2437 	if (port_status == USBA_HIGH_SPEED_DEV) {
2438 
2439 		ehci_update_bw_availability(ehcip, -sbandwidth,
2440 		    leftmost, list_count, smask);
2441 	} else {
2442 		if ((endpoint->bmAttributes & USB_EP_ATTR_MASK) ==
2443 		    USB_EP_ATTR_INTR) {
2444 
2445 			ehci_update_bw_availability(ehcip, -sbandwidth,
2446 			    leftmost, list_count, smask);
2447 			ehci_update_bw_availability(ehcip, -cbandwidth,
2448 			    leftmost, list_count, cmask);
2449 		} else {
2450 			if ((endpoint->bEndpointAddress &
2451 			    USB_EP_DIR_MASK) == USB_EP_DIR_IN) {
2452 
2453 				ehci_update_bw_availability(ehcip, -sbandwidth,
2454 				    leftmost, list_count, smask);
2455 				ehci_update_bw_availability(ehcip,
2456 				    -MAX_UFRAME_SITD_XFER, leftmost,
2457 				    list_count, cmask);
2458 			} else {
2459 
2460 				ehci_update_bw_availability(ehcip,
2461 				    -MAX_UFRAME_SITD_XFER, leftmost,
2462 				    list_count, smask);
2463 			}
2464 		}
2465 	}
2466 }
2467 
2468 /*
2469  * ehci_deallocate_classic_tt_bandwidth:
2470  *
2471  * Deallocate high speed bandwidth of a interrupt or isochronous endpoint.
2472  */
2473 static void
2474 ehci_deallocate_classic_tt_bandwidth(
2475 	ehci_state_t		*ehcip,
2476 	usba_pipe_handle_data_t	*ph,
2477 	uint_t			pnode)
2478 {
2479 	uint_t			bandwidth, height, leftmost, list, min;
2480 	int			i, interval;
2481 	usb_ep_descr_t		*endpoint = &ph->p_ep;
2482 	usba_device_t		*child_ud, *parent_ud;
2483 	usb_port_status_t	port_status;
2484 
2485 	/* This routine is protected by the ehci_int_mutex */
2486 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
2487 
2488 	/* Get child's usba device structure */
2489 	child_ud = ph->p_usba_device;
2490 
2491 	mutex_enter(&child_ud->usb_mutex);
2492 
2493 	/* Get the current usb device's port status */
2494 	port_status = child_ud->usb_port_status;
2495 
2496 	/* Get the parent high speed hub's usba device structure */
2497 	parent_ud = child_ud->usb_hs_hub_usba_dev;
2498 
2499 	mutex_exit(&child_ud->usb_mutex);
2500 
2501 	/* Obtain the bandwidth */
2502 	(void) ehci_compute_classic_bandwidth(endpoint,
2503 	    port_status, &bandwidth);
2504 
2505 	/* Adjust polling interval to be a power of 2 */
2506 	interval = ehci_adjust_polling_interval(ehcip, endpoint, port_status);
2507 
2508 	/* Find the height in the tree */
2509 	height = ehci_lattice_height(interval);
2510 
2511 	/* Find the leftmost leaf in the subtree specified by the node */
2512 	leftmost = ehci_leftmost_leaf(pnode, height);
2513 
2514 	mutex_enter(&parent_ud->usb_mutex);
2515 
2516 	/* Delete the bandwidth from the appropriate lists */
2517 	for (i = 0; i < (EHCI_NUM_INTR_QH_LISTS/interval); i++) {
2518 		list = ehci_index[leftmost + i];
2519 		parent_ud->usb_hs_hub_bandwidth[list] -= bandwidth;
2520 	}
2521 
2522 	/* Find the leaf with the smallest allocated bandwidth */
2523 	min = parent_ud->usb_hs_hub_bandwidth[0];
2524 
2525 	for (i = 1; i < EHCI_NUM_INTR_QH_LISTS; i++) {
2526 		if (parent_ud->usb_hs_hub_bandwidth[i] < min) {
2527 			min = parent_ud->usb_hs_hub_bandwidth[i];
2528 		}
2529 	}
2530 
2531 	/* Save the minimum for later use */
2532 	parent_ud->usb_hs_hub_min_bandwidth = min;
2533 
2534 	mutex_exit(&parent_ud->usb_mutex);
2535 }
2536 
2537 
2538 /*
2539  * ehci_compute_high_speed_bandwidth:
2540  *
2541  * Given a periodic endpoint (interrupt or isochronous) determine the total
2542  * bandwidth for one transaction. The EHCI host controller traverses the
2543  * endpoint descriptor lists on a first-come-first-serve basis. When the HC
2544  * services an endpoint, only a single transaction attempt is made. The  HC
2545  * moves to the next Endpoint Descriptor after the first transaction attempt
2546  * rather than finishing the entire Transfer Descriptor. Therefore, when  a
2547  * Transfer Descriptor is inserted into the lattice, we will only count the
2548  * number of bytes for one transaction.
2549  *
2550  * The following are the formulas used for  calculating bandwidth in  terms
2551  * bytes and it is for the single USB high speed transaction.  The protocol
2552  * overheads will be different for each of type of USB transfer & all these
2553  * formulas & protocol overheads are derived from the 5.11.3 section of the
2554  * USB 2.0 Specification.
2555  *
2556  * High-Speed:
2557  *		Protocol overhead + ((MaxPktSz * 7)/6) + Host_Delay
2558  *
2559  * Split Transaction: (Low/Full speed devices connected behind usb2.0 hub)
2560  *
2561  *		Protocol overhead + Split transaction overhead +
2562  *			((MaxPktSz * 7)/6) + Host_Delay;
2563  */
2564 /* ARGSUSED */
2565 static int
2566 ehci_compute_high_speed_bandwidth(
2567 	ehci_state_t		*ehcip,
2568 	usb_ep_descr_t		*endpoint,
2569 	usb_port_status_t	port_status,
2570 	uint_t			*sbandwidth,
2571 	uint_t			*cbandwidth)
2572 {
2573 	ushort_t		maxpacketsize = endpoint->wMaxPacketSize;
2574 
2575 	/* Return failure if endpoint maximum packet is zero */
2576 	if (maxpacketsize == 0) {
2577 		USB_DPRINTF_L2(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2578 		    "ehci_allocate_high_speed_bandwidth: Periodic endpoint "
2579 		    "with zero endpoint maximum packet size is not supported");
2580 
2581 		return (USB_NOT_SUPPORTED);
2582 	}
2583 
2584 	/* Add bit-stuffing overhead */
2585 	maxpacketsize = (ushort_t)((maxpacketsize * 7) / 6);
2586 
2587 	/* Add Host Controller specific delay to required bandwidth */
2588 	*sbandwidth = EHCI_HOST_CONTROLLER_DELAY;
2589 
2590 	/* Add xfer specific protocol overheads */
2591 	if ((endpoint->bmAttributes &
2592 	    USB_EP_ATTR_MASK) == USB_EP_ATTR_INTR) {
2593 		/* High speed interrupt transaction */
2594 		*sbandwidth += HS_NON_ISOC_PROTO_OVERHEAD;
2595 	} else {
2596 		/* Isochronous transaction */
2597 		*sbandwidth += HS_ISOC_PROTO_OVERHEAD;
2598 	}
2599 
2600 	/*
2601 	 * For low/full speed devices, add split transaction specific
2602 	 * overheads.
2603 	 */
2604 	if (port_status != USBA_HIGH_SPEED_DEV) {
2605 		/*
2606 		 * Add start and complete split transaction
2607 		 * tokens overheads.
2608 		 */
2609 		*cbandwidth = *sbandwidth + COMPLETE_SPLIT_OVERHEAD;
2610 		*sbandwidth += START_SPLIT_OVERHEAD;
2611 
2612 		/* Add data overhead depending on data direction */
2613 		if ((endpoint->bEndpointAddress &
2614 		    USB_EP_DIR_MASK) == USB_EP_DIR_IN) {
2615 			*cbandwidth += maxpacketsize;
2616 		} else {
2617 			if ((endpoint->bmAttributes &
2618 			    USB_EP_ATTR_MASK) == USB_EP_ATTR_ISOCH) {
2619 				/* There is no compete splits for out */
2620 				*cbandwidth = 0;
2621 			}
2622 			*sbandwidth += maxpacketsize;
2623 		}
2624 	} else {
2625 		uint_t		xactions;
2626 
2627 		/* Get the max transactions per microframe */
2628 		xactions = ((maxpacketsize & USB_EP_MAX_XACTS_MASK) >>
2629 		    USB_EP_MAX_XACTS_SHIFT) + 1;
2630 
2631 		/* High speed transaction */
2632 		*sbandwidth += maxpacketsize;
2633 
2634 		/* Calculate bandwidth per micro-frame */
2635 		*sbandwidth *= xactions;
2636 
2637 		*cbandwidth = 0;
2638 	}
2639 
2640 	USB_DPRINTF_L4(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2641 	    "ehci_allocate_high_speed_bandwidth: "
2642 	    "Start split bandwidth %d Complete split bandwidth %d",
2643 	    *sbandwidth, *cbandwidth);
2644 
2645 	return (USB_SUCCESS);
2646 }
2647 
2648 
2649 /*
2650  * ehci_compute_classic_bandwidth:
2651  *
2652  * Given a periodic endpoint (interrupt or isochronous) determine the total
2653  * bandwidth for one transaction. The EHCI host controller traverses the
2654  * endpoint descriptor lists on a first-come-first-serve basis. When the HC
2655  * services an endpoint, only a single transaction attempt is made. The  HC
2656  * moves to the next Endpoint Descriptor after the first transaction attempt
2657  * rather than finishing the entire Transfer Descriptor. Therefore, when  a
2658  * Transfer Descriptor is inserted into the lattice, we will only count the
2659  * number of bytes for one transaction.
2660  *
2661  * The following are the formulas used for  calculating bandwidth in  terms
2662  * bytes and it is for the single USB high speed transaction.  The protocol
2663  * overheads will be different for each of type of USB transfer & all these
2664  * formulas & protocol overheads are derived from the 5.11.3 section of the
2665  * USB 2.0 Specification.
2666  *
2667  * Low-Speed:
2668  *		Protocol overhead + Hub LS overhead +
2669  *		(Low Speed clock * ((MaxPktSz * 7)/6)) + TT_Delay
2670  *
2671  * Full-Speed:
2672  *		Protocol overhead + ((MaxPktSz * 7)/6) + TT_Delay
2673  */
2674 /* ARGSUSED */
2675 static int
2676 ehci_compute_classic_bandwidth(
2677 	usb_ep_descr_t		*endpoint,
2678 	usb_port_status_t	port_status,
2679 	uint_t			*bandwidth)
2680 {
2681 	ushort_t		maxpacketsize = endpoint->wMaxPacketSize;
2682 
2683 	/*
2684 	 * If endpoint maximum packet is zero, then return immediately.
2685 	 */
2686 	if (maxpacketsize == 0) {
2687 
2688 		return (USB_NOT_SUPPORTED);
2689 	}
2690 
2691 	/* Add TT delay to required bandwidth */
2692 	*bandwidth = TT_DELAY;
2693 
2694 	/* Add bit-stuffing overhead */
2695 	maxpacketsize = (ushort_t)((maxpacketsize * 7) / 6);
2696 
2697 	switch (port_status) {
2698 	case USBA_LOW_SPEED_DEV:
2699 		/* Low speed interrupt transaction */
2700 		*bandwidth += (LOW_SPEED_PROTO_OVERHEAD +
2701 		    HUB_LOW_SPEED_PROTO_OVERHEAD +
2702 		    (LOW_SPEED_CLOCK * maxpacketsize));
2703 		break;
2704 	case USBA_FULL_SPEED_DEV:
2705 		/* Full speed transaction */
2706 		*bandwidth += maxpacketsize;
2707 
2708 		/* Add xfer specific protocol overheads */
2709 		if ((endpoint->bmAttributes &
2710 		    USB_EP_ATTR_MASK) == USB_EP_ATTR_INTR) {
2711 			/* Full speed interrupt transaction */
2712 			*bandwidth += FS_NON_ISOC_PROTO_OVERHEAD;
2713 		} else {
2714 			/* Isochronous and input transaction */
2715 			if ((endpoint->bEndpointAddress &
2716 			    USB_EP_DIR_MASK) == USB_EP_DIR_IN) {
2717 				*bandwidth += FS_ISOC_INPUT_PROTO_OVERHEAD;
2718 			} else {
2719 				/* Isochronous and output transaction */
2720 				*bandwidth += FS_ISOC_OUTPUT_PROTO_OVERHEAD;
2721 			}
2722 		}
2723 		break;
2724 	}
2725 
2726 	return (USB_SUCCESS);
2727 }
2728 
2729 
2730 /*
2731  * ehci_adjust_polling_interval:
2732  *
2733  * Adjust bandwidth according usb device speed.
2734  */
2735 /* ARGSUSED */
2736 int
2737 ehci_adjust_polling_interval(
2738 	ehci_state_t		*ehcip,
2739 	usb_ep_descr_t		*endpoint,
2740 	usb_port_status_t	port_status)
2741 {
2742 	uint_t			interval;
2743 	int			i = 0;
2744 
2745 	/* Get the polling interval */
2746 	interval = endpoint->bInterval;
2747 
2748 	USB_DPRINTF_L4(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2749 	    "ehci_adjust_polling_interval: Polling interval 0x%x", interval);
2750 
2751 	/*
2752 	 * According USB 2.0 Specifications, a high-speed endpoint's
2753 	 * polling intervals are specified interms of 125us or micro
2754 	 * frame, where as full/low endpoint's polling intervals are
2755 	 * specified in milliseconds.
2756 	 *
2757 	 * A high speed interrupt/isochronous endpoints can specify
2758 	 * desired polling interval between 1 to 16 micro-frames,
2759 	 * where as full/low endpoints can specify between 1 to 255
2760 	 * milliseconds.
2761 	 */
2762 	switch (port_status) {
2763 	case USBA_LOW_SPEED_DEV:
2764 		/*
2765 		 * Low speed  endpoints are limited to	specifying
2766 		 * only 8ms to 255ms in this driver. If a device
2767 		 * reports a polling interval that is less than 8ms,
2768 		 * it will use 8 ms instead.
2769 		 */
2770 		if (interval < LS_MIN_POLL_INTERVAL) {
2771 
2772 			USB_DPRINTF_L1(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2773 			    "Low speed endpoint's poll interval of %d ms "
2774 			    "is below threshold. Rounding up to %d ms",
2775 			    interval, LS_MIN_POLL_INTERVAL);
2776 
2777 			interval = LS_MIN_POLL_INTERVAL;
2778 		}
2779 
2780 		/*
2781 		 * Return an error if the polling interval is greater
2782 		 * than 255ms.
2783 		 */
2784 		if (interval > LS_MAX_POLL_INTERVAL) {
2785 
2786 			USB_DPRINTF_L1(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2787 			    "Low speed endpoint's poll interval is "
2788 			    "greater than %d ms", LS_MAX_POLL_INTERVAL);
2789 
2790 			return (USB_FAILURE);
2791 		}
2792 		break;
2793 
2794 	case USBA_FULL_SPEED_DEV:
2795 		/*
2796 		 * Return an error if the polling interval is less
2797 		 * than 1ms and greater than 255ms.
2798 		 */
2799 		if ((interval < FS_MIN_POLL_INTERVAL) &&
2800 		    (interval > FS_MAX_POLL_INTERVAL)) {
2801 
2802 			USB_DPRINTF_L1(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2803 			    "Full speed endpoint's poll interval must "
2804 			    "be between %d and %d ms", FS_MIN_POLL_INTERVAL,
2805 			    FS_MAX_POLL_INTERVAL);
2806 
2807 			return (USB_FAILURE);
2808 		}
2809 		break;
2810 	case USBA_HIGH_SPEED_DEV:
2811 		/*
2812 		 * Return an error if the polling interval is less 1
2813 		 * and greater than 16. Convert this value to 125us
2814 		 * units using 2^(bInterval -1). refer usb 2.0 spec
2815 		 * page 51 for details.
2816 		 */
2817 		if ((interval < HS_MIN_POLL_INTERVAL) &&
2818 		    (interval > HS_MAX_POLL_INTERVAL)) {
2819 
2820 			USB_DPRINTF_L1(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2821 			    "High speed endpoint's poll interval "
2822 			    "must be between %d and %d units",
2823 			    HS_MIN_POLL_INTERVAL, HS_MAX_POLL_INTERVAL);
2824 
2825 			return (USB_FAILURE);
2826 		}
2827 
2828 		/* Adjust high speed device polling interval */
2829 		interval =
2830 		    ehci_adjust_high_speed_polling_interval(ehcip, endpoint);
2831 
2832 		break;
2833 	}
2834 
2835 	/*
2836 	 * If polling interval is greater than 32ms,
2837 	 * adjust polling interval equal to 32ms.
2838 	 */
2839 	if (interval > EHCI_NUM_INTR_QH_LISTS) {
2840 		interval = EHCI_NUM_INTR_QH_LISTS;
2841 	}
2842 
2843 	/*
2844 	 * Find the nearest power of 2 that's less
2845 	 * than interval.
2846 	 */
2847 	while ((ehci_pow_2(i)) <= interval) {
2848 		i++;
2849 	}
2850 
2851 	return (ehci_pow_2((i - 1)));
2852 }
2853 
2854 
2855 /*
2856  * ehci_adjust_high_speed_polling_interval:
2857  */
2858 /* ARGSUSED */
2859 static int
2860 ehci_adjust_high_speed_polling_interval(
2861 	ehci_state_t		*ehcip,
2862 	usb_ep_descr_t		*endpoint)
2863 {
2864 	uint_t			interval;
2865 
2866 	/* Get the polling interval */
2867 	interval = ehci_pow_2(endpoint->bInterval - 1);
2868 
2869 	/*
2870 	 * Convert polling interval from micro seconds
2871 	 * to milli seconds.
2872 	 */
2873 	if (interval <= EHCI_MAX_UFRAMES) {
2874 		interval = 1;
2875 	} else {
2876 		interval = interval/EHCI_MAX_UFRAMES;
2877 	}
2878 
2879 	USB_DPRINTF_L4(PRINT_MASK_BW, ehcip->ehci_log_hdl,
2880 	    "ehci_adjust_high_speed_polling_interval: "
2881 	    "High speed adjusted interval 0x%x", interval);
2882 
2883 	return (interval);
2884 }
2885 
2886 
2887 /*
2888  * ehci_lattice_height:
2889  *
2890  * Given the requested bandwidth, find the height in the tree at which the
2891  * nodes for this bandwidth fall.  The height is measured as the number of
2892  * nodes from the leaf to the level specified by bandwidth The root of the
2893  * tree is at height TREE_HEIGHT.
2894  */
2895 static uint_t
2896 ehci_lattice_height(uint_t interval)
2897 {
2898 	return (TREE_HEIGHT - (ehci_log_2(interval)));
2899 }
2900 
2901 
2902 /*
2903  * ehci_lattice_parent:
2904  *
2905  * Given a node in the lattice, find the index of the parent node
2906  */
2907 static uint_t
2908 ehci_lattice_parent(uint_t node)
2909 {
2910 	if ((node % 2) == 0) {
2911 
2912 		return ((node/2) - 1);
2913 	} else {
2914 
2915 		return ((node + 1)/2 - 1);
2916 	}
2917 }
2918 
2919 
2920 /*
2921  * ehci_find_periodic_node:
2922  *
2923  * Based on the "real" array leaf node and interval, get the periodic node.
2924  */
2925 static uint_t
2926 ehci_find_periodic_node(uint_t leaf, int interval) {
2927 	uint_t	lattice_leaf;
2928 	uint_t	height = ehci_lattice_height(interval);
2929 	uint_t	pnode;
2930 	int	i;
2931 
2932 	/* Get the leaf number in the lattice */
2933 	lattice_leaf = leaf + EHCI_NUM_INTR_QH_LISTS - 1;
2934 
2935 	/* Get the node in the lattice based on the height and leaf */
2936 	pnode = lattice_leaf;
2937 	for (i = 0; i < height; i++) {
2938 		pnode = ehci_lattice_parent(pnode);
2939 	}
2940 
2941 	return (pnode);
2942 }
2943 
2944 
2945 /*
2946  * ehci_leftmost_leaf:
2947  *
2948  * Find the leftmost leaf in the subtree specified by the node. Height refers
2949  * to number of nodes from the bottom of the tree to the node,	including the
2950  * node.
2951  *
2952  * The formula for a zero based tree is:
2953  *     2^H * Node + 2^H - 1
2954  * The leaf of the tree is an array, convert the number for the array.
2955  *     Subtract the size of nodes not in the array
2956  *     2^H * Node + 2^H - 1 - (EHCI_NUM_INTR_QH_LISTS - 1) =
2957  *     2^H * Node + 2^H - EHCI_NUM_INTR_QH_LISTS =
2958  *     2^H * (Node + 1) - EHCI_NUM_INTR_QH_LISTS
2959  *	   0
2960  *	 1   2
2961  *	0 1 2 3
2962  */
2963 static uint_t
2964 ehci_leftmost_leaf(
2965 	uint_t	node,
2966 	uint_t	height)
2967 {
2968 	return ((ehci_pow_2(height) * (node + 1)) - EHCI_NUM_INTR_QH_LISTS);
2969 }
2970 
2971 
2972 /*
2973  * ehci_pow_2:
2974  *
2975  * Compute 2 to the power
2976  */
2977 static uint_t
2978 ehci_pow_2(uint_t x)
2979 {
2980 	if (x == 0) {
2981 
2982 		return (1);
2983 	} else {
2984 
2985 		return (2 << (x - 1));
2986 	}
2987 }
2988 
2989 
2990 /*
2991  * ehci_log_2:
2992  *
2993  * Compute log base 2 of x
2994  */
2995 static uint_t
2996 ehci_log_2(uint_t x)
2997 {
2998 	int i = 0;
2999 
3000 	while (x != 1) {
3001 		x = x >> 1;
3002 		i++;
3003 	}
3004 
3005 	return (i);
3006 }
3007 
3008 
3009 /*
3010  * ehci_find_bestfit_hs_mask:
3011  *
3012  * Find the smask and cmask in the bandwidth allocation, and update the
3013  * bandwidth allocation.
3014  */
3015 static int
3016 ehci_find_bestfit_hs_mask(
3017 	ehci_state_t	*ehcip,
3018 	uchar_t		*smask,
3019 	uint_t		*pnode,
3020 	usb_ep_descr_t	*endpoint,
3021 	uint_t		bandwidth,
3022 	int		interval)
3023 {
3024 	int		i;
3025 	uint_t		elements, index;
3026 	int		array_leaf, best_array_leaf;
3027 	uint_t		node_bandwidth, best_node_bandwidth;
3028 	uint_t		leaf_count;
3029 	uchar_t		bw_mask;
3030 	uchar_t		best_smask;
3031 
3032 	USB_DPRINTF_L4(PRINT_MASK_BW, ehcip->ehci_log_hdl,
3033 	    "ehci_find_bestfit_hs_mask: ");
3034 
3035 	/* Get all the valid smasks */
3036 	switch (ehci_pow_2(endpoint->bInterval - 1)) {
3037 	case EHCI_INTR_1US_POLL:
3038 		index = EHCI_1US_MASK_INDEX;
3039 		elements = EHCI_INTR_1US_POLL;
3040 		break;
3041 	case EHCI_INTR_2US_POLL:
3042 		index = EHCI_2US_MASK_INDEX;
3043 		elements = EHCI_INTR_2US_POLL;
3044 		break;
3045 	case EHCI_INTR_4US_POLL:
3046 		index = EHCI_4US_MASK_INDEX;
3047 		elements = EHCI_INTR_4US_POLL;
3048 		break;
3049 	case EHCI_INTR_XUS_POLL:
3050 	default:
3051 		index = EHCI_XUS_MASK_INDEX;
3052 		elements = EHCI_INTR_XUS_POLL;
3053 		break;
3054 	}
3055 
3056 	leaf_count = EHCI_NUM_INTR_QH_LISTS/interval;
3057 
3058 	/*
3059 	 * Because of the way the leaves are setup, we will automatically
3060 	 * hit the leftmost leaf of every possible node with this interval.
3061 	 */
3062 	best_smask = 0x00;
3063 	best_node_bandwidth = 0;
3064 	for (array_leaf = 0; array_leaf < interval; array_leaf++) {
3065 		/* Find the bandwidth mask */
3066 		node_bandwidth = ehci_calculate_bw_availability_mask(ehcip,
3067 		    bandwidth, ehci_index[array_leaf], leaf_count, &bw_mask);
3068 
3069 		/*
3070 		 * If this node cannot support our requirements skip to the
3071 		 * next leaf.
3072 		 */
3073 		if (bw_mask == 0x00) {
3074 			continue;
3075 		}
3076 
3077 		/*
3078 		 * Now make sure our bandwidth requirements can be
3079 		 * satisfied with one of smasks in this node.
3080 		 */
3081 		*smask = 0x00;
3082 		for (i = index; i < (index + elements); i++) {
3083 			/* Check the start split mask value */
3084 			if (ehci_start_split_mask[index] & bw_mask) {
3085 				*smask = ehci_start_split_mask[index];
3086 				break;
3087 			}
3088 		}
3089 
3090 		/*
3091 		 * If an appropriate smask is found save the information if:
3092 		 * o best_smask has not been found yet.
3093 		 * - or -
3094 		 * o This is the node with the least amount of bandwidth
3095 		 */
3096 		if ((*smask != 0x00) &&
3097 		    ((best_smask == 0x00) ||
3098 			(best_node_bandwidth > node_bandwidth))) {
3099 
3100 			best_node_bandwidth = node_bandwidth;
3101 			best_array_leaf = array_leaf;
3102 			best_smask = *smask;
3103 		}
3104 	}
3105 
3106 	/*
3107 	 * If we find node that can handle the bandwidth populate the
3108 	 * appropriate variables and return success.
3109 	 */
3110 	if (best_smask) {
3111 		*smask = best_smask;
3112 		*pnode = ehci_find_periodic_node(ehci_index[best_array_leaf],
3113 		    interval);
3114 		ehci_update_bw_availability(ehcip, bandwidth,
3115 		    ehci_index[best_array_leaf], leaf_count, best_smask);
3116 
3117 		return (USB_SUCCESS);
3118 	}
3119 
3120 	return (USB_FAILURE);
3121 }
3122 
3123 
3124 /*
3125  * ehci_find_bestfit_ls_intr_mask:
3126  *
3127  * Find the smask and cmask in the bandwidth allocation.
3128  */
3129 static int
3130 ehci_find_bestfit_ls_intr_mask(
3131 	ehci_state_t	*ehcip,
3132 	uchar_t		*smask,
3133 	uchar_t		*cmask,
3134 	uint_t		*pnode,
3135 	uint_t		sbandwidth,
3136 	uint_t		cbandwidth,
3137 	int		interval)
3138 {
3139 	int		i;
3140 	uint_t		elements, index;
3141 	int		array_leaf, best_array_leaf;
3142 	uint_t		node_sbandwidth, node_cbandwidth;
3143 	uint_t		best_node_bandwidth;
3144 	uint_t		leaf_count;
3145 	uchar_t		bw_smask, bw_cmask;
3146 	uchar_t		best_smask, best_cmask;
3147 
3148 	USB_DPRINTF_L4(PRINT_MASK_BW, ehcip->ehci_log_hdl,
3149 	    "ehci_find_bestfit_ls_intr_mask: ");
3150 
3151 	/* For low and full speed devices */
3152 	index = EHCI_XUS_MASK_INDEX;
3153 	elements = EHCI_INTR_4MS_POLL;
3154 
3155 	leaf_count = EHCI_NUM_INTR_QH_LISTS/interval;
3156 
3157 	/*
3158 	 * Because of the way the leaves are setup, we will automatically
3159 	 * hit the leftmost leaf of every possible node with this interval.
3160 	 */
3161 	best_smask = 0x00;
3162 	best_node_bandwidth = 0;
3163 	for (array_leaf = 0; array_leaf < interval; array_leaf++) {
3164 		/* Find the bandwidth mask */
3165 		node_sbandwidth = ehci_calculate_bw_availability_mask(ehcip,
3166 		    sbandwidth, ehci_index[array_leaf], leaf_count, &bw_smask);
3167 		node_cbandwidth = ehci_calculate_bw_availability_mask(ehcip,
3168 		    cbandwidth, ehci_index[array_leaf], leaf_count, &bw_cmask);
3169 
3170 		/*
3171 		 * If this node cannot support our requirements skip to the
3172 		 * next leaf.
3173 		 */
3174 		if ((bw_smask == 0x00) || (bw_cmask == 0x00)) {
3175 			continue;
3176 		}
3177 
3178 		/*
3179 		 * Now make sure our bandwidth requirements can be
3180 		 * satisfied with one of smasks in this node.
3181 		 */
3182 		*smask = 0x00;
3183 		*cmask = 0x00;
3184 		for (i = index; i < (index + elements); i++) {
3185 			/* Check the start split mask value */
3186 			if ((ehci_start_split_mask[index] & bw_smask) &&
3187 			    (ehci_intr_complete_split_mask[index] & bw_cmask)) {
3188 				*smask = ehci_start_split_mask[index];
3189 				*cmask = ehci_intr_complete_split_mask[index];
3190 				break;
3191 			}
3192 		}
3193 
3194 		/*
3195 		 * If an appropriate smask is found save the information if:
3196 		 * o best_smask has not been found yet.
3197 		 * - or -
3198 		 * o This is the node with the least amount of bandwidth
3199 		 */
3200 		if ((*smask != 0x00) &&
3201 		    ((best_smask == 0x00) ||
3202 			(best_node_bandwidth >
3203 			    (node_sbandwidth + node_cbandwidth)))) {
3204 			best_node_bandwidth = node_sbandwidth + node_cbandwidth;
3205 			best_array_leaf = array_leaf;
3206 			best_smask = *smask;
3207 			best_cmask = *cmask;
3208 		}
3209 	}
3210 
3211 	/*
3212 	 * If we find node that can handle the bandwidth populate the
3213 	 * appropriate variables and return success.
3214 	 */
3215 	if (best_smask) {
3216 		*smask = best_smask;
3217 		*cmask = best_cmask;
3218 		*pnode = ehci_find_periodic_node(ehci_index[best_array_leaf],
3219 		    interval);
3220 		ehci_update_bw_availability(ehcip, sbandwidth,
3221 		    ehci_index[best_array_leaf], leaf_count, best_smask);
3222 		ehci_update_bw_availability(ehcip, cbandwidth,
3223 		    ehci_index[best_array_leaf], leaf_count, best_cmask);
3224 
3225 		return (USB_SUCCESS);
3226 	}
3227 
3228 	return (USB_FAILURE);
3229 }
3230 
3231 
3232 /*
3233  * ehci_find_bestfit_sitd_in_mask:
3234  *
3235  * Find the smask and cmask in the bandwidth allocation.
3236  */
3237 static int
3238 ehci_find_bestfit_sitd_in_mask(
3239 	ehci_state_t	*ehcip,
3240 	uchar_t		*smask,
3241 	uchar_t		*cmask,
3242 	uint_t		*pnode,
3243 	uint_t		sbandwidth,
3244 	uint_t		cbandwidth,
3245 	int		interval)
3246 {
3247 	int		i, uFrames, found;
3248 	int		array_leaf, best_array_leaf;
3249 	uint_t		node_sbandwidth, node_cbandwidth;
3250 	uint_t		best_node_bandwidth;
3251 	uint_t		leaf_count;
3252 	uchar_t		bw_smask, bw_cmask;
3253 	uchar_t		best_smask, best_cmask;
3254 
3255 	USB_DPRINTF_L4(PRINT_MASK_BW, ehcip->ehci_log_hdl,
3256 	    "ehci_find_bestfit_sitd_in_mask: ");
3257 
3258 	leaf_count = EHCI_NUM_INTR_QH_LISTS/interval;
3259 
3260 	/*
3261 	 * Because of the way the leaves are setup, we will automatically
3262 	 * hit the leftmost leaf of every possible node with this interval.
3263 	 * You may only send MAX_UFRAME_SITD_XFER raw bits per uFrame.
3264 	 */
3265 	/*
3266 	 * Need to add an additional 2 uFrames, if the "L"ast
3267 	 * complete split is before uFrame 6.  See section
3268 	 * 11.8.4 in USB 2.0 Spec.  Currently we do not support
3269 	 * the "Back Ptr" which means we support on IN of
3270 	 * ~4*MAX_UFRAME_SITD_XFER bandwidth/
3271 	 */
3272 	uFrames = (cbandwidth / MAX_UFRAME_SITD_XFER) + 2;
3273 	if (cbandwidth % MAX_UFRAME_SITD_XFER) {
3274 		uFrames++;
3275 	}
3276 	if (uFrames > 6) {
3277 
3278 		return (USB_FAILURE);
3279 	}
3280 	*smask = 0x1;
3281 	*cmask = 0x00;
3282 	for (i = 0; i < uFrames; i++) {
3283 		*cmask = *cmask << 1;
3284 		*cmask |= 0x1;
3285 	}
3286 	/* cmask must start 2 frames after the smask */
3287 	*cmask = *cmask << 2;
3288 
3289 	found = 0;
3290 	best_smask = 0x00;
3291 	best_node_bandwidth = 0;
3292 	for (array_leaf = 0; array_leaf < interval; array_leaf++) {
3293 		node_sbandwidth = ehci_calculate_bw_availability_mask(ehcip,
3294 		    sbandwidth, ehci_index[array_leaf], leaf_count, &bw_smask);
3295 		node_cbandwidth = ehci_calculate_bw_availability_mask(ehcip,
3296 		    MAX_UFRAME_SITD_XFER, ehci_index[array_leaf], leaf_count,
3297 		    &bw_cmask);
3298 
3299 		/*
3300 		 * If this node cannot support our requirements skip to the
3301 		 * next leaf.
3302 		 */
3303 		if ((bw_smask == 0x00) || (bw_cmask == 0x00)) {
3304 			continue;
3305 		}
3306 
3307 		for (i = 0; i < (EHCI_MAX_UFRAMES - uFrames - 2); i++) {
3308 			if ((*smask & bw_smask) && (*cmask & bw_cmask)) {
3309 				found = 1;
3310 				break;
3311 			}
3312 			*smask = *smask << 1;
3313 			*cmask = *cmask << 1;
3314 		}
3315 
3316 		/*
3317 		 * If an appropriate smask is found save the information if:
3318 		 * o best_smask has not been found yet.
3319 		 * - or -
3320 		 * o This is the node with the least amount of bandwidth
3321 		 */
3322 		if (found &&
3323 		    ((best_smask == 0x00) ||
3324 			(best_node_bandwidth >
3325 			    (node_sbandwidth + node_cbandwidth)))) {
3326 			best_node_bandwidth = node_sbandwidth + node_cbandwidth;
3327 			best_array_leaf = array_leaf;
3328 			best_smask = *smask;
3329 			best_cmask = *cmask;
3330 		}
3331 	}
3332 
3333 	/*
3334 	 * If we find node that can handle the bandwidth populate the
3335 	 * appropriate variables and return success.
3336 	 */
3337 	if (best_smask) {
3338 		*smask = best_smask;
3339 		*cmask = best_cmask;
3340 		*pnode = ehci_find_periodic_node(ehci_index[best_array_leaf],
3341 		    interval);
3342 		ehci_update_bw_availability(ehcip, sbandwidth,
3343 		    ehci_index[best_array_leaf], leaf_count, best_smask);
3344 		ehci_update_bw_availability(ehcip, MAX_UFRAME_SITD_XFER,
3345 		    ehci_index[best_array_leaf], leaf_count, best_cmask);
3346 
3347 		return (USB_SUCCESS);
3348 	}
3349 
3350 	return (USB_FAILURE);
3351 }
3352 
3353 
3354 /*
3355  * ehci_find_bestfit_sitd_out_mask:
3356  *
3357  * Find the smask in the bandwidth allocation.
3358  */
3359 static int
3360 ehci_find_bestfit_sitd_out_mask(
3361 	ehci_state_t	*ehcip,
3362 	uchar_t		*smask,
3363 	uint_t		*pnode,
3364 	uint_t		sbandwidth,
3365 	int		interval)
3366 {
3367 	int		i, uFrames, found;
3368 	int		array_leaf, best_array_leaf;
3369 	uint_t		node_sbandwidth;
3370 	uint_t		best_node_bandwidth;
3371 	uint_t		leaf_count;
3372 	uchar_t		bw_smask;
3373 	uchar_t		best_smask;
3374 
3375 	USB_DPRINTF_L4(PRINT_MASK_BW, ehcip->ehci_log_hdl,
3376 	    "ehci_find_bestfit_sitd_out_mask: ");
3377 
3378 	leaf_count = EHCI_NUM_INTR_QH_LISTS/interval;
3379 
3380 	/*
3381 	 * Because of the way the leaves are setup, we will automatically
3382 	 * hit the leftmost leaf of every possible node with this interval.
3383 	 * You may only send MAX_UFRAME_SITD_XFER raw bits per uFrame.
3384 	 */
3385 	*smask = 0x00;
3386 	uFrames = sbandwidth / MAX_UFRAME_SITD_XFER;
3387 	if (sbandwidth % MAX_UFRAME_SITD_XFER) {
3388 		uFrames++;
3389 	}
3390 	for (i = 0; i < uFrames; i++) {
3391 		*smask = *smask << 1;
3392 		*smask |= 0x1;
3393 	}
3394 
3395 	found = 0;
3396 	best_smask = 0x00;
3397 	best_node_bandwidth = 0;
3398 	for (array_leaf = 0; array_leaf < interval; array_leaf++) {
3399 		node_sbandwidth = ehci_calculate_bw_availability_mask(ehcip,
3400 		    MAX_UFRAME_SITD_XFER, ehci_index[array_leaf], leaf_count,
3401 		    &bw_smask);
3402 
3403 		/*
3404 		 * If this node cannot support our requirements skip to the
3405 		 * next leaf.
3406 		 */
3407 		if (bw_smask == 0x00) {
3408 			continue;
3409 		}
3410 
3411 		/* You cannot have a start split on the 8th uFrame */
3412 		for (i = 0; (*smask & 0x80) == 0; i++) {
3413 			if (*smask & bw_smask) {
3414 				found = 1;
3415 				break;
3416 			}
3417 			*smask = *smask << 1;
3418 		}
3419 
3420 		/*
3421 		 * If an appropriate smask is found save the information if:
3422 		 * o best_smask has not been found yet.
3423 		 * - or -
3424 		 * o This is the node with the least amount of bandwidth
3425 		 */
3426 		if (found &&
3427 		    ((best_smask == 0x00) ||
3428 			(best_node_bandwidth > node_sbandwidth))) {
3429 			best_node_bandwidth = node_sbandwidth;
3430 			best_array_leaf = array_leaf;
3431 			best_smask = *smask;
3432 		}
3433 	}
3434 
3435 	/*
3436 	 * If we find node that can handle the bandwidth populate the
3437 	 * appropriate variables and return success.
3438 	 */
3439 	if (best_smask) {
3440 		*smask = best_smask;
3441 		*pnode = ehci_find_periodic_node(ehci_index[best_array_leaf],
3442 		    interval);
3443 		ehci_update_bw_availability(ehcip, MAX_UFRAME_SITD_XFER,
3444 		    ehci_index[best_array_leaf], leaf_count, best_smask);
3445 
3446 		return (USB_SUCCESS);
3447 	}
3448 
3449 	return (USB_FAILURE);
3450 }
3451 
3452 
3453 /*
3454  * ehci_calculate_bw_availability_mask:
3455  *
3456  * Returns the "total bandwidth used" in this node.
3457  * Populates bw_mask with the uFrames that can support the bandwidth.
3458  *
3459  * If all the Frames cannot support this bandwidth, then bw_mask
3460  * will return 0x00 and the "total bandwidth used" will be invalid.
3461  */
3462 static uint_t
3463 ehci_calculate_bw_availability_mask(
3464 	ehci_state_t	*ehcip,
3465 	uint_t		bandwidth,
3466 	int		leaf,
3467 	int		leaf_count,
3468 	uchar_t		*bw_mask)
3469 {
3470 	int			i, j;
3471 	uchar_t			bw_uframe;
3472 	int			uframe_total;
3473 	ehci_frame_bandwidth_t	*fbp;
3474 	uint_t			total_bandwidth = 0;
3475 
3476 	USB_DPRINTF_L4(PRINT_MASK_BW, ehcip->ehci_log_hdl,
3477 	    "ehci_calculate_bw_availability_mask: leaf %d leaf count %d",
3478 	    leaf, leaf_count);
3479 
3480 	/* Start by saying all uFrames are available */
3481 	*bw_mask = 0xFF;
3482 
3483 	for (i = 0; (i < leaf_count) || (*bw_mask == 0x00); i++) {
3484 		fbp = &ehcip->ehci_frame_bandwidth[leaf + i];
3485 
3486 		total_bandwidth += fbp->ehci_allocated_frame_bandwidth;
3487 
3488 		for (j = 0; j < EHCI_MAX_UFRAMES; j++) {
3489 			/*
3490 			 * If the uFrame in bw_mask is available check to see if
3491 			 * it can support the additional bandwidth.
3492 			 */
3493 			bw_uframe = (*bw_mask & (0x1 << j));
3494 			uframe_total =
3495 			    fbp->ehci_micro_frame_bandwidth[j] +
3496 			    bandwidth;
3497 			if ((bw_uframe) &&
3498 			    (uframe_total > HS_PERIODIC_BANDWIDTH)) {
3499 				*bw_mask = *bw_mask & ~bw_uframe;
3500 			}
3501 		}
3502 	}
3503 
3504 	USB_DPRINTF_L4(PRINT_MASK_BW, ehcip->ehci_log_hdl,
3505 	    "ehci_calculate_bw_availability_mask: bandwidth mask 0x%x",
3506 	    *bw_mask);
3507 
3508 	return (total_bandwidth);
3509 }
3510 
3511 
3512 /*
3513  * ehci_update_bw_availability:
3514  *
3515  * The leftmost leaf needs to be in terms of array position and
3516  * not the actual lattice position.
3517  */
3518 static void
3519 ehci_update_bw_availability(
3520 	ehci_state_t	*ehcip,
3521 	int		bandwidth,
3522 	int		leftmost_leaf,
3523 	int		leaf_count,
3524 	uchar_t		mask)
3525 {
3526 	int			i, j;
3527 	ehci_frame_bandwidth_t	*fbp;
3528 	int			uFrame_bandwidth[8];
3529 
3530 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
3531 	    "ehci_update_bw_availability: "
3532 	    "leaf %d count %d bandwidth 0x%x mask 0x%x",
3533 	    leftmost_leaf, leaf_count, bandwidth, mask);
3534 
3535 	ASSERT(leftmost_leaf < 32);
3536 	ASSERT(leftmost_leaf >= 0);
3537 
3538 	for (j = 0; j < EHCI_MAX_UFRAMES; j++) {
3539 		if (mask & 0x1) {
3540 			uFrame_bandwidth[j] = bandwidth;
3541 		} else {
3542 			uFrame_bandwidth[j] = 0;
3543 		}
3544 
3545 		mask = mask >> 1;
3546 	}
3547 
3548 	/* Updated all the effected leafs with the bandwidth */
3549 	for (i = 0; i < leaf_count; i++) {
3550 		fbp = &ehcip->ehci_frame_bandwidth[leftmost_leaf + i];
3551 
3552 		for (j = 0; j < EHCI_MAX_UFRAMES; j++) {
3553 			fbp->ehci_micro_frame_bandwidth[j] +=
3554 			    uFrame_bandwidth[j];
3555 			fbp->ehci_allocated_frame_bandwidth +=
3556 			    uFrame_bandwidth[j];
3557 		}
3558 	}
3559 }
3560 
3561 /*
3562  * Miscellaneous functions
3563  */
3564 
3565 /*
3566  * ehci_obtain_state:
3567  *
3568  * NOTE: This function is also called from POLLED MODE.
3569  */
3570 ehci_state_t *
3571 ehci_obtain_state(dev_info_t	*dip)
3572 {
3573 	int			instance = ddi_get_instance(dip);
3574 
3575 	ehci_state_t *state = ddi_get_soft_state(ehci_statep, instance);
3576 
3577 	ASSERT(state != NULL);
3578 
3579 	return (state);
3580 }
3581 
3582 
3583 /*
3584  * ehci_state_is_operational:
3585  *
3586  * Check the Host controller state and return proper values.
3587  */
3588 int
3589 ehci_state_is_operational(ehci_state_t	*ehcip)
3590 {
3591 	int	val;
3592 
3593 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
3594 
3595 	switch (ehcip->ehci_hc_soft_state) {
3596 	case EHCI_CTLR_INIT_STATE:
3597 	case EHCI_CTLR_SUSPEND_STATE:
3598 		val = USB_FAILURE;
3599 		break;
3600 	case EHCI_CTLR_OPERATIONAL_STATE:
3601 		val = USB_SUCCESS;
3602 		break;
3603 	case EHCI_CTLR_ERROR_STATE:
3604 		val = USB_HC_HARDWARE_ERROR;
3605 		break;
3606 	default:
3607 		val = USB_FAILURE;
3608 		break;
3609 	}
3610 
3611 	return (val);
3612 }
3613 
3614 
3615 /*
3616  * ehci_do_soft_reset
3617  *
3618  * Do soft reset of ehci host controller.
3619  */
3620 int
3621 ehci_do_soft_reset(ehci_state_t	*ehcip)
3622 {
3623 	usb_frame_number_t	before_frame_number, after_frame_number;
3624 	ehci_regs_t		*ehci_save_regs;
3625 
3626 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
3627 
3628 	/* Increment host controller error count */
3629 	ehcip->ehci_hc_error++;
3630 
3631 	USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
3632 	    "ehci_do_soft_reset:"
3633 	    "Reset ehci host controller 0x%x", ehcip->ehci_hc_error);
3634 
3635 	/*
3636 	 * Allocate space for saving current Host Controller
3637 	 * registers. Don't do any recovery if allocation
3638 	 * fails.
3639 	 */
3640 	ehci_save_regs = (ehci_regs_t *)
3641 	    kmem_zalloc(sizeof (ehci_regs_t), KM_NOSLEEP);
3642 
3643 	if (ehci_save_regs == NULL) {
3644 		USB_DPRINTF_L2(PRINT_MASK_INTR,  ehcip->ehci_log_hdl,
3645 		    "ehci_do_soft_reset: kmem_zalloc failed");
3646 
3647 		return (USB_FAILURE);
3648 	}
3649 
3650 	/* Save current ehci registers */
3651 	ehci_save_regs->ehci_command = Get_OpReg(ehci_command);
3652 	ehci_save_regs->ehci_interrupt = Get_OpReg(ehci_interrupt);
3653 	ehci_save_regs->ehci_ctrl_segment = Get_OpReg(ehci_ctrl_segment);
3654 	ehci_save_regs->ehci_async_list_addr = Get_OpReg(ehci_async_list_addr);
3655 	ehci_save_regs->ehci_config_flag = Get_OpReg(ehci_config_flag);
3656 	ehci_save_regs->ehci_periodic_list_base =
3657 	    Get_OpReg(ehci_periodic_list_base);
3658 
3659 	USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
3660 	    "ehci_do_soft_reset: Save reg = 0x%p", ehci_save_regs);
3661 
3662 	/* Disable all list processing and interrupts */
3663 	Set_OpReg(ehci_command, Get_OpReg(ehci_command) &
3664 	    ~(EHCI_CMD_ASYNC_SCHED_ENABLE | EHCI_CMD_PERIODIC_SCHED_ENABLE));
3665 
3666 	/* Disable all EHCI interrupts */
3667 	Set_OpReg(ehci_interrupt, 0);
3668 
3669 	/* Wait for few milliseconds */
3670 	drv_usecwait(EHCI_SOF_TIMEWAIT);
3671 
3672 	/* Do light soft reset of ehci host controller */
3673 	Set_OpReg(ehci_command,
3674 	    Get_OpReg(ehci_command) | EHCI_CMD_LIGHT_HC_RESET);
3675 
3676 	USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
3677 	    "ehci_do_soft_reset: Reset in progress");
3678 
3679 	/* Wait for reset to complete */
3680 	drv_usecwait(EHCI_RESET_TIMEWAIT);
3681 
3682 	/*
3683 	 * Restore previous saved EHCI register value
3684 	 * into the current EHCI registers.
3685 	 */
3686 	Set_OpReg(ehci_ctrl_segment, (uint32_t)
3687 		ehci_save_regs->ehci_ctrl_segment);
3688 
3689 	Set_OpReg(ehci_periodic_list_base, (uint32_t)
3690 		ehci_save_regs->ehci_periodic_list_base);
3691 
3692 	Set_OpReg(ehci_async_list_addr, (uint32_t)
3693 		ehci_save_regs->ehci_async_list_addr);
3694 
3695 	/*
3696 	 * For some reason this register might get nulled out by
3697 	 * the Uli M1575 South Bridge. To workaround the hardware
3698 	 * problem, check the value after write and retry if the
3699 	 * last write fails.
3700 	 */
3701 	if ((ehcip->ehci_vendor_id == PCI_VENDOR_ULi_M1575) &&
3702 	    (ehcip->ehci_device_id == PCI_DEVICE_ULi_M1575) &&
3703 	    (ehci_save_regs->ehci_async_list_addr !=
3704 	    Get_OpReg(ehci_async_list_addr))) {
3705 		int retry = 0;
3706 
3707 		Set_OpRegRetry(ehci_async_list_addr, (uint32_t)
3708 			ehci_save_regs->ehci_async_list_addr, retry);
3709 		if (retry >= EHCI_MAX_RETRY) {
3710 			USB_DPRINTF_L0(PRINT_MASK_ATTA,
3711 			    ehcip->ehci_log_hdl, "ehci_do_soft_reset:"
3712 			    " ASYNCLISTADDR write failed.");
3713 
3714 			return (USB_FAILURE);
3715 		}
3716 		USB_DPRINTF_L2(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
3717 		    "ehci_do_soft_reset: ASYNCLISTADDR "
3718 			"write failed, retry=%d", retry);
3719 	}
3720 
3721 	Set_OpReg(ehci_config_flag, (uint32_t)
3722 		ehci_save_regs->ehci_config_flag);
3723 
3724 	/* Enable both Asynchronous and Periodic Schedule if necessary */
3725 	ehci_toggle_scheduler(ehcip);
3726 
3727 	/*
3728 	 * Set ehci_interrupt to enable all interrupts except Root
3729 	 * Hub Status change and frame list rollover interrupts.
3730 	 */
3731 	Set_OpReg(ehci_interrupt, EHCI_INTR_HOST_SYSTEM_ERROR |
3732 	    EHCI_INTR_FRAME_LIST_ROLLOVER |
3733 	    EHCI_INTR_USB_ERROR |
3734 	    EHCI_INTR_USB);
3735 
3736 	/*
3737 	 * Deallocate the space that allocated for saving
3738 	 * HC registers.
3739 	 */
3740 	kmem_free((void *) ehci_save_regs, sizeof (ehci_regs_t));
3741 
3742 	/*
3743 	 * Set the desired interrupt threshold, frame list size (if
3744 	 * applicable) and turn EHCI host controller.
3745 	 */
3746 	Set_OpReg(ehci_command, ((Get_OpReg(ehci_command) &
3747 	    ~EHCI_CMD_INTR_THRESHOLD) |
3748 	    (EHCI_CMD_01_INTR | EHCI_CMD_HOST_CTRL_RUN)));
3749 
3750 	/* Wait 10ms for EHCI to start sending SOF */
3751 	drv_usecwait(EHCI_RESET_TIMEWAIT);
3752 
3753 	/*
3754 	 * Get the current usb frame number before waiting for
3755 	 * few milliseconds.
3756 	 */
3757 	before_frame_number = ehci_get_current_frame_number(ehcip);
3758 
3759 	/* Wait for few milliseconds */
3760 	drv_usecwait(EHCI_SOF_TIMEWAIT);
3761 
3762 	/*
3763 	 * Get the current usb frame number after waiting for
3764 	 * few milliseconds.
3765 	 */
3766 	after_frame_number = ehci_get_current_frame_number(ehcip);
3767 
3768 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
3769 	    "ehci_do_soft_reset: Before Frame Number 0x%llx "
3770 	    "After Frame Number 0x%llx",
3771 	    before_frame_number, after_frame_number);
3772 
3773 	if ((after_frame_number <= before_frame_number) &&
3774 	    (Get_OpReg(ehci_status) & EHCI_STS_HOST_CTRL_HALTED)) {
3775 
3776 		USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
3777 		    "ehci_do_soft_reset: Soft reset failed");
3778 
3779 		return (USB_FAILURE);
3780 	}
3781 
3782 	return (USB_SUCCESS);
3783 }
3784 
3785 
3786 /*
3787  * ehci_get_xfer_attrs:
3788  *
3789  * Get the attributes of a particular xfer.
3790  *
3791  * NOTE: This function is also called from POLLED MODE.
3792  */
3793 usb_req_attrs_t
3794 ehci_get_xfer_attrs(
3795 	ehci_state_t		*ehcip,
3796 	ehci_pipe_private_t	*pp,
3797 	ehci_trans_wrapper_t	*tw)
3798 {
3799 	usb_ep_descr_t		*eptd = &pp->pp_pipe_handle->p_ep;
3800 	usb_req_attrs_t		attrs = USB_ATTRS_NONE;
3801 
3802 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
3803 	    "ehci_get_xfer_attrs:");
3804 
3805 	switch (eptd->bmAttributes & USB_EP_ATTR_MASK) {
3806 	case USB_EP_ATTR_CONTROL:
3807 		attrs = ((usb_ctrl_req_t *)
3808 		    tw->tw_curr_xfer_reqp)->ctrl_attributes;
3809 		break;
3810 	case USB_EP_ATTR_BULK:
3811 		attrs = ((usb_bulk_req_t *)
3812 		    tw->tw_curr_xfer_reqp)->bulk_attributes;
3813 		break;
3814 	case USB_EP_ATTR_INTR:
3815 		attrs = ((usb_intr_req_t *)
3816 		    tw->tw_curr_xfer_reqp)->intr_attributes;
3817 		break;
3818 	}
3819 
3820 	return (attrs);
3821 }
3822 
3823 
3824 /*
3825  * ehci_get_current_frame_number:
3826  *
3827  * Get the current software based usb frame number.
3828  */
3829 usb_frame_number_t
3830 ehci_get_current_frame_number(ehci_state_t *ehcip)
3831 {
3832 	usb_frame_number_t	usb_frame_number;
3833 	usb_frame_number_t	ehci_fno, micro_frame_number;
3834 
3835 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
3836 
3837 	ehci_fno = ehcip->ehci_fno;
3838 	micro_frame_number = Get_OpReg(ehci_frame_index) & 0x3FFF;
3839 
3840 	/*
3841 	 * Calculate current software based usb frame number.
3842 	 *
3843 	 * This code accounts for the fact that frame number is
3844 	 * updated by the Host Controller before the ehci driver
3845 	 * gets an FrameListRollover interrupt that will adjust
3846 	 * Frame higher part.
3847 	 *
3848 	 * Refer ehci specification 1.0, section 2.3.2, page 21.
3849 	 */
3850 	micro_frame_number = ((micro_frame_number & 0x1FFF) |
3851 	    ehci_fno) + (((micro_frame_number & 0x3FFF) ^
3852 	    ehci_fno) & 0x2000);
3853 
3854 	/*
3855 	 * Micro Frame number is equivalent to 125 usec. Eight
3856 	 * Micro Frame numbers are equivalent to one millsecond
3857 	 * or one usb frame number.
3858 	 */
3859 	usb_frame_number = micro_frame_number >>
3860 	    EHCI_uFRAMES_PER_USB_FRAME_SHIFT;
3861 
3862 	USB_DPRINTF_L4(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
3863 	    "ehci_get_current_frame_number: "
3864 	    "Current usb uframe number = 0x%llx "
3865 	    "Current usb frame number  = 0x%llx",
3866 	    micro_frame_number, usb_frame_number);
3867 
3868 	return (usb_frame_number);
3869 }
3870 
3871 
3872 /*
3873  * ehci_cpr_cleanup:
3874  *
3875  * Cleanup ehci state and other ehci specific informations across
3876  * Check Point Resume (CPR).
3877  */
3878 static	void
3879 ehci_cpr_cleanup(ehci_state_t *ehcip)
3880 {
3881 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
3882 
3883 	/* Reset software part of usb frame number */
3884 	ehcip->ehci_fno = 0;
3885 }
3886 
3887 
3888 /*
3889  * ehci_wait_for_sof:
3890  *
3891  * Wait for couple of SOF interrupts
3892  */
3893 int
3894 ehci_wait_for_sof(ehci_state_t	*ehcip)
3895 {
3896 	usb_frame_number_t	before_frame_number, after_frame_number;
3897 	int			error = USB_SUCCESS;
3898 
3899 	USB_DPRINTF_L4(PRINT_MASK_LISTS,
3900 	    ehcip->ehci_log_hdl, "ehci_wait_for_sof");
3901 
3902 	ASSERT(mutex_owned(&ehcip->ehci_int_mutex));
3903 
3904 	error = ehci_state_is_operational(ehcip);
3905 
3906 	if (error != USB_SUCCESS) {
3907 
3908 		return (error);
3909 	}
3910 
3911 	/* Get the current usb frame number before waiting for two SOFs */
3912 	before_frame_number = ehci_get_current_frame_number(ehcip);
3913 
3914 	mutex_exit(&ehcip->ehci_int_mutex);
3915 
3916 	/* Wait for few milliseconds */
3917 	delay(drv_usectohz(EHCI_SOF_TIMEWAIT));
3918 
3919 	mutex_enter(&ehcip->ehci_int_mutex);
3920 
3921 	/* Get the current usb frame number after woken up */
3922 	after_frame_number = ehci_get_current_frame_number(ehcip);
3923 
3924 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
3925 	    "ehci_wait_for_sof: framenumber: before 0x%llx "
3926 	    "after 0x%llx", before_frame_number, after_frame_number);
3927 
3928 	/* Return failure, if usb frame number has not been changed */
3929 	if (after_frame_number <= before_frame_number) {
3930 
3931 		if ((ehci_do_soft_reset(ehcip)) != USB_SUCCESS) {
3932 
3933 			USB_DPRINTF_L0(PRINT_MASK_LISTS,
3934 			    ehcip->ehci_log_hdl, "No SOF interrupts");
3935 
3936 			/* Set host controller soft state to error */
3937 			ehcip->ehci_hc_soft_state = EHCI_CTLR_ERROR_STATE;
3938 
3939 			return (USB_FAILURE);
3940 		}
3941 
3942 		/* Get new usb frame number */
3943 		after_frame_number = before_frame_number =
3944 		    ehci_get_current_frame_number(ehcip);
3945 	}
3946 
3947 	ASSERT(after_frame_number > before_frame_number);
3948 
3949 	return (USB_SUCCESS);
3950 }
3951 
3952 
3953 /*
3954  * ehci_toggle_scheduler:
3955  *
3956  * Turn scheduler based on pipe open count.
3957  */
3958 void
3959 ehci_toggle_scheduler(ehci_state_t *ehcip) {
3960 	uint_t	temp_reg, cmd_reg;
3961 
3962 	cmd_reg = Get_OpReg(ehci_command);
3963 	temp_reg = cmd_reg;
3964 
3965 	/*
3966 	 * Enable/Disable asynchronous scheduler, and
3967 	 * turn on/off async list door bell
3968 	 */
3969 	if (ehcip->ehci_open_async_count) {
3970 		if (!(cmd_reg & EHCI_CMD_ASYNC_SCHED_ENABLE)) {
3971 			/*
3972 			 * For some reason this address might get nulled out by
3973 			 * the ehci chip. Set it here just in case it is null.
3974 			 */
3975 			Set_OpReg(ehci_async_list_addr,
3976 			    ehci_qh_cpu_to_iommu(ehcip,
3977 				ehcip->ehci_head_of_async_sched_list));
3978 
3979 			/*
3980 			 * For some reason this register might get nulled out by
3981 			 * the Uli M1575 Southbridge. To workaround the HW
3982 			 * problem, check the value after write and retry if the
3983 			 * last write fails.
3984 			 *
3985 			 * If the ASYNCLISTADDR remains "stuck" after
3986 			 * EHCI_MAX_RETRY retries, then the M1575 is broken
3987 			 * and is stuck in an inconsistent state and is about
3988 			 * to crash the machine with a trn_oor panic when it
3989 			 * does a DMA read from 0x0.  It is better to panic
3990 			 * now rather than wait for the trn_oor crash; this
3991 			 * way Customer Service will have a clean signature
3992 			 * that indicts the M1575 chip rather than a
3993 			 * mysterious and hard-to-diagnose trn_oor panic.
3994 			 */
3995 			if ((ehcip->ehci_vendor_id == PCI_VENDOR_ULi_M1575) &&
3996 			    (ehcip->ehci_device_id == PCI_DEVICE_ULi_M1575) &&
3997 			    (ehci_qh_cpu_to_iommu(ehcip,
3998 			    ehcip->ehci_head_of_async_sched_list) !=
3999 			    Get_OpReg(ehci_async_list_addr))) {
4000 				int retry = 0;
4001 
4002 				Set_OpRegRetry(ehci_async_list_addr,
4003 				    ehci_qh_cpu_to_iommu(ehcip,
4004 				    ehcip->ehci_head_of_async_sched_list),
4005 				    retry);
4006 				if (retry >= EHCI_MAX_RETRY)
4007 					cmn_err(CE_PANIC,
4008 					    "ehci_toggle_scheduler: "
4009 					    "ASYNCLISTADDR write failed.");
4010 
4011 				USB_DPRINTF_L2(PRINT_MASK_ATTA,
4012 				    ehcip->ehci_log_hdl,
4013 				    "ehci_toggle_scheduler: ASYNCLISTADDR "
4014 					"write failed, retry=%d", retry);
4015 			}
4016 		}
4017 		cmd_reg |= EHCI_CMD_ASYNC_SCHED_ENABLE;
4018 	} else {
4019 		cmd_reg &= ~EHCI_CMD_ASYNC_SCHED_ENABLE;
4020 	}
4021 
4022 	if (ehcip->ehci_open_periodic_count) {
4023 		if (!(cmd_reg & EHCI_CMD_PERIODIC_SCHED_ENABLE)) {
4024 			/*
4025 			 * For some reason this address get's nulled out by
4026 			 * the ehci chip. Set it here just in case it is null.
4027 			 */
4028 			Set_OpReg(ehci_periodic_list_base,
4029 			    (uint32_t)(ehcip->ehci_pflt_cookie.dmac_address &
4030 				0xFFFFF000));
4031 		}
4032 		cmd_reg |= EHCI_CMD_PERIODIC_SCHED_ENABLE;
4033 	} else {
4034 		cmd_reg &= ~EHCI_CMD_PERIODIC_SCHED_ENABLE;
4035 	}
4036 
4037 	/* Just an optimization */
4038 	if (temp_reg != cmd_reg) {
4039 		Set_OpReg(ehci_command, cmd_reg);
4040 	}
4041 }
4042 
4043 /*
4044  * ehci print functions
4045  */
4046 
4047 /*
4048  * ehci_print_caps:
4049  */
4050 void
4051 ehci_print_caps(ehci_state_t	*ehcip)
4052 {
4053 	uint_t			i;
4054 
4055 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4056 	    "\n\tUSB 2.0 Host Controller Characteristics\n");
4057 
4058 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4059 	    "Caps Length: 0x%x Version: 0x%x\n",
4060 	    Get_8Cap(ehci_caps_length), Get_16Cap(ehci_version));
4061 
4062 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4063 	    "Structural Parameters\n");
4064 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4065 	    "Port indicators: %s", (Get_Cap(ehci_hcs_params) &
4066 	    EHCI_HCS_PORT_INDICATOR) ? "Yes" : "No");
4067 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4068 	    "No of Classic host controllers: 0x%x",
4069 	    (Get_Cap(ehci_hcs_params) & EHCI_HCS_NUM_COMP_CTRLS)
4070 	    >> EHCI_HCS_NUM_COMP_CTRL_SHIFT);
4071 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4072 	    "No of ports per Classic host controller: 0x%x",
4073 	    (Get_Cap(ehci_hcs_params) & EHCI_HCS_NUM_PORTS_CC)
4074 	    >> EHCI_HCS_NUM_PORTS_CC_SHIFT);
4075 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4076 	    "Port routing rules: %s", (Get_Cap(ehci_hcs_params) &
4077 	    EHCI_HCS_PORT_ROUTING_RULES) ? "Yes" : "No");
4078 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4079 	    "Port power control: %s", (Get_Cap(ehci_hcs_params) &
4080 	    EHCI_HCS_PORT_POWER_CONTROL) ? "Yes" : "No");
4081 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4082 	    "No of root hub ports: 0x%x\n",
4083 	    Get_Cap(ehci_hcs_params) & EHCI_HCS_NUM_PORTS);
4084 
4085 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4086 	    "Capability Parameters\n");
4087 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4088 	    "EHCI extended capability: %s", (Get_Cap(ehci_hcc_params) &
4089 	    EHCI_HCC_EECP) ? "Yes" : "No");
4090 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4091 	    "Isoch schedule threshold: 0x%x",
4092 	    Get_Cap(ehci_hcc_params) & EHCI_HCC_ISOCH_SCHED_THRESHOLD);
4093 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4094 	    "Async schedule park capability: %s", (Get_Cap(ehci_hcc_params) &
4095 	    EHCI_HCC_ASYNC_SCHED_PARK_CAP) ? "Yes" : "No");
4096 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4097 	    "Programmable frame list flag: %s", (Get_Cap(ehci_hcc_params) &
4098 	    EHCI_HCC_PROG_FRAME_LIST_FLAG) ? "256/512/1024" : "1024");
4099 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4100 	    "64bit addressing capability: %s\n", (Get_Cap(ehci_hcc_params) &
4101 	    EHCI_HCC_64BIT_ADDR_CAP) ? "Yes" : "No");
4102 
4103 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4104 	    "Classic Port Route Description");
4105 
4106 	for (i = 0; i < (Get_Cap(ehci_hcs_params) & EHCI_HCS_NUM_PORTS); i++) {
4107 		USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4108 		    "\tPort Route 0x%x: 0x%x", i, Get_8Cap(ehci_port_route[i]));
4109 	}
4110 }
4111 
4112 
4113 /*
4114  * ehci_print_regs:
4115  */
4116 void
4117 ehci_print_regs(ehci_state_t	*ehcip)
4118 {
4119 	uint_t			i;
4120 
4121 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4122 	    "\n\tEHCI%d Operational Registers\n",
4123 	    ddi_get_instance(ehcip->ehci_dip));
4124 
4125 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4126 	    "Command: 0x%x Status: 0x%x",
4127 	    Get_OpReg(ehci_command), Get_OpReg(ehci_status));
4128 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4129 	    "Interrupt: 0x%x Frame Index: 0x%x",
4130 	    Get_OpReg(ehci_interrupt), Get_OpReg(ehci_frame_index));
4131 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4132 	    "Control Segment: 0x%x Periodic List Base: 0x%x",
4133 	    Get_OpReg(ehci_ctrl_segment), Get_OpReg(ehci_periodic_list_base));
4134 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4135 	    "Async List Addr: 0x%x Config Flag: 0x%x",
4136 	    Get_OpReg(ehci_async_list_addr), Get_OpReg(ehci_config_flag));
4137 
4138 	USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4139 	    "Root Hub Port Status");
4140 
4141 	for (i = 0; i < (Get_Cap(ehci_hcs_params) & EHCI_HCS_NUM_PORTS); i++) {
4142 		USB_DPRINTF_L3(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
4143 		    "\tPort Status 0x%x: 0x%x ", i,
4144 		    Get_OpReg(ehci_rh_port_status[i]));
4145 	}
4146 }
4147 
4148 
4149 /*
4150  * ehci_print_qh:
4151  */
4152 void
4153 ehci_print_qh(
4154 	ehci_state_t	*ehcip,
4155 	ehci_qh_t	*qh)
4156 {
4157 	uint_t		i;
4158 
4159 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4160 	    "ehci_print_qh: qh = 0x%p", (void *)qh);
4161 
4162 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4163 	    "\tqh_link_ptr: 0x%x ", Get_QH(qh->qh_link_ptr));
4164 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4165 	    "\tqh_ctrl: 0x%x ", Get_QH(qh->qh_ctrl));
4166 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4167 	    "\tqh_split_ctrl: 0x%x ", Get_QH(qh->qh_split_ctrl));
4168 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4169 	    "\tqh_curr_qtd: 0x%x ", Get_QH(qh->qh_curr_qtd));
4170 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4171 	    "\tqh_next_qtd: 0x%x ", Get_QH(qh->qh_next_qtd));
4172 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4173 	    "\tqh_alt_next_qtd: 0x%x ", Get_QH(qh->qh_alt_next_qtd));
4174 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4175 	    "\tqh_status: 0x%x ", Get_QH(qh->qh_status));
4176 
4177 	for (i = 0; i < 5; i++) {
4178 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4179 		    "\tqh_buf[%d]: 0x%x ", i, Get_QH(qh->qh_buf[i]));
4180 	}
4181 
4182 	for (i = 0; i < 5; i++) {
4183 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4184 		    "\tqh_buf_high[%d]: 0x%x ",
4185 		    i, Get_QH(qh->qh_buf_high[i]));
4186 	}
4187 
4188 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4189 	    "\tqh_dummy_qtd: 0x%x ", Get_QH(qh->qh_dummy_qtd));
4190 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4191 	    "\tqh_prev: 0x%x ", Get_QH(qh->qh_prev));
4192 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4193 	    "\tqh_state: 0x%x ", Get_QH(qh->qh_state));
4194 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4195 	    "\tqh_reclaim_next: 0x%x ", Get_QH(qh->qh_reclaim_next));
4196 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4197 	    "\tqh_reclaim_frame: 0x%x ", Get_QH(qh->qh_reclaim_frame));
4198 }
4199 
4200 
4201 /*
4202  * ehci_print_qtd:
4203  */
4204 void
4205 ehci_print_qtd(
4206 	ehci_state_t	*ehcip,
4207 	ehci_qtd_t	*qtd)
4208 {
4209 	uint_t		i;
4210 
4211 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4212 	    "ehci_print_qtd: qtd = 0x%p", (void *)qtd);
4213 
4214 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4215 	    "\tqtd_next_qtd: 0x%x ", Get_QTD(qtd->qtd_next_qtd));
4216 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4217 	    "\tqtd_alt_next_qtd: 0x%x ", Get_QTD(qtd->qtd_alt_next_qtd));
4218 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4219 	    "\tqtd_ctrl: 0x%x ", Get_QTD(qtd->qtd_ctrl));
4220 
4221 	for (i = 0; i < 5; i++) {
4222 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4223 		    "\tqtd_buf[%d]: 0x%x ", i, Get_QTD(qtd->qtd_buf[i]));
4224 	}
4225 
4226 	for (i = 0; i < 5; i++) {
4227 		USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4228 		    "\tqtd_buf_high[%d]: 0x%x ",
4229 		    i, Get_QTD(qtd->qtd_buf_high[i]));
4230 	}
4231 
4232 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4233 	    "\tqtd_trans_wrapper: 0x%x ", Get_QTD(qtd->qtd_trans_wrapper));
4234 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4235 	    "\tqtd_tw_next_qtd: 0x%x ", Get_QTD(qtd->qtd_tw_next_qtd));
4236 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4237 	    "\tqtd_active_qtd_next: 0x%x ", Get_QTD(qtd->qtd_active_qtd_next));
4238 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4239 	    "\tqtd_active_qtd_prev: 0x%x ", Get_QTD(qtd->qtd_active_qtd_prev));
4240 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4241 	    "\tqtd_state: 0x%x ", Get_QTD(qtd->qtd_state));
4242 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4243 	    "\tqtd_ctrl_phase: 0x%x ", Get_QTD(qtd->qtd_ctrl_phase));
4244 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4245 	    "\tqtd_xfer_offs: 0x%x ", Get_QTD(qtd->qtd_xfer_offs));
4246 	USB_DPRINTF_L3(PRINT_MASK_LISTS, ehcip->ehci_log_hdl,
4247 	    "\tqtd_xfer_len: 0x%x ", Get_QTD(qtd->qtd_xfer_len));
4248 }
4249 
4250 /*
4251  * ehci kstat functions
4252  */
4253 
4254 /*
4255  * ehci_create_stats:
4256  *
4257  * Allocate and initialize the ehci kstat structures
4258  */
4259 void
4260 ehci_create_stats(ehci_state_t	*ehcip)
4261 {
4262 	char			kstatname[KSTAT_STRLEN];
4263 	const char		*dname = ddi_driver_name(ehcip->ehci_dip);
4264 	char			*usbtypes[USB_N_COUNT_KSTATS] =
4265 				    {"ctrl", "isoch", "bulk", "intr"};
4266 	uint_t			instance = ehcip->ehci_instance;
4267 	ehci_intrs_stats_t	*isp;
4268 	int			i;
4269 
4270 	if (EHCI_INTRS_STATS(ehcip) == NULL) {
4271 		(void) snprintf(kstatname, KSTAT_STRLEN, "%s%d,intrs",
4272 		    dname, instance);
4273 		EHCI_INTRS_STATS(ehcip) = kstat_create("usba", instance,
4274 		    kstatname, "usb_interrupts", KSTAT_TYPE_NAMED,
4275 		    sizeof (ehci_intrs_stats_t) / sizeof (kstat_named_t),
4276 		    KSTAT_FLAG_PERSISTENT);
4277 
4278 		if (EHCI_INTRS_STATS(ehcip)) {
4279 			isp = EHCI_INTRS_STATS_DATA(ehcip);
4280 			kstat_named_init(&isp->ehci_sts_total,
4281 			    "Interrupts Total", KSTAT_DATA_UINT64);
4282 			kstat_named_init(&isp->ehci_sts_not_claimed,
4283 			    "Not Claimed", KSTAT_DATA_UINT64);
4284 			kstat_named_init(&isp->ehci_sts_async_sched_status,
4285 			    "Async schedule status", KSTAT_DATA_UINT64);
4286 			kstat_named_init(&isp->ehci_sts_periodic_sched_status,
4287 			    "Periodic sched status", KSTAT_DATA_UINT64);
4288 			kstat_named_init(&isp->ehci_sts_empty_async_schedule,
4289 			    "Empty async schedule", KSTAT_DATA_UINT64);
4290 			kstat_named_init(&isp->ehci_sts_host_ctrl_halted,
4291 			    "Host controller Halted", KSTAT_DATA_UINT64);
4292 			kstat_named_init(&isp->ehci_sts_async_advance_intr,
4293 			    "Intr on async advance", KSTAT_DATA_UINT64);
4294 			kstat_named_init(&isp->ehci_sts_host_system_error_intr,
4295 			    "Host system error", KSTAT_DATA_UINT64);
4296 			kstat_named_init(&isp->ehci_sts_frm_list_rollover_intr,
4297 			    "Frame list rollover", KSTAT_DATA_UINT64);
4298 			kstat_named_init(&isp->ehci_sts_rh_port_change_intr,
4299 			    "Port change detect", KSTAT_DATA_UINT64);
4300 			kstat_named_init(&isp->ehci_sts_usb_error_intr,
4301 			    "USB error interrupt", KSTAT_DATA_UINT64);
4302 			kstat_named_init(&isp->ehci_sts_usb_intr,
4303 			    "USB interrupt", KSTAT_DATA_UINT64);
4304 
4305 			EHCI_INTRS_STATS(ehcip)->ks_private = ehcip;
4306 			EHCI_INTRS_STATS(ehcip)->ks_update = nulldev;
4307 			kstat_install(EHCI_INTRS_STATS(ehcip));
4308 		}
4309 	}
4310 
4311 	if (EHCI_TOTAL_STATS(ehcip) == NULL) {
4312 		(void) snprintf(kstatname, KSTAT_STRLEN, "%s%d,total",
4313 		    dname, instance);
4314 		EHCI_TOTAL_STATS(ehcip) = kstat_create("usba", instance,
4315 		    kstatname, "usb_byte_count", KSTAT_TYPE_IO, 1,
4316 		    KSTAT_FLAG_PERSISTENT);
4317 
4318 		if (EHCI_TOTAL_STATS(ehcip)) {
4319 			kstat_install(EHCI_TOTAL_STATS(ehcip));
4320 		}
4321 	}
4322 
4323 	for (i = 0; i < USB_N_COUNT_KSTATS; i++) {
4324 		if (ehcip->ehci_count_stats[i] == NULL) {
4325 			(void) snprintf(kstatname, KSTAT_STRLEN, "%s%d,%s",
4326 			    dname, instance, usbtypes[i]);
4327 			ehcip->ehci_count_stats[i] = kstat_create("usba",
4328 			    instance, kstatname, "usb_byte_count",
4329 			    KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
4330 
4331 			if (ehcip->ehci_count_stats[i]) {
4332 				kstat_install(ehcip->ehci_count_stats[i]);
4333 			}
4334 		}
4335 	}
4336 }
4337 
4338 
4339 /*
4340  * ehci_destroy_stats:
4341  *
4342  * Clean up ehci kstat structures
4343  */
4344 void
4345 ehci_destroy_stats(ehci_state_t	*ehcip)
4346 {
4347 	int	i;
4348 
4349 	if (EHCI_INTRS_STATS(ehcip)) {
4350 		kstat_delete(EHCI_INTRS_STATS(ehcip));
4351 		EHCI_INTRS_STATS(ehcip) = NULL;
4352 	}
4353 
4354 	if (EHCI_TOTAL_STATS(ehcip)) {
4355 		kstat_delete(EHCI_TOTAL_STATS(ehcip));
4356 		EHCI_TOTAL_STATS(ehcip) = NULL;
4357 	}
4358 
4359 	for (i = 0; i < USB_N_COUNT_KSTATS; i++) {
4360 		if (ehcip->ehci_count_stats[i]) {
4361 			kstat_delete(ehcip->ehci_count_stats[i]);
4362 			ehcip->ehci_count_stats[i] = NULL;
4363 		}
4364 	}
4365 }
4366 
4367 
4368 /*
4369  * ehci_do_intrs_stats:
4370  *
4371  * ehci status information
4372  */
4373 void
4374 ehci_do_intrs_stats(
4375 	ehci_state_t	*ehcip,
4376 	int		val)
4377 {
4378 	if (EHCI_INTRS_STATS(ehcip)) {
4379 		EHCI_INTRS_STATS_DATA(ehcip)->ehci_sts_total.value.ui64++;
4380 		switch (val) {
4381 		case EHCI_STS_ASYNC_SCHED_STATUS:
4382 			EHCI_INTRS_STATS_DATA(ehcip)->
4383 			    ehci_sts_async_sched_status.value.ui64++;
4384 			break;
4385 		case EHCI_STS_PERIODIC_SCHED_STATUS:
4386 			EHCI_INTRS_STATS_DATA(ehcip)->
4387 			    ehci_sts_periodic_sched_status.value.ui64++;
4388 			break;
4389 		case EHCI_STS_EMPTY_ASYNC_SCHEDULE:
4390 			EHCI_INTRS_STATS_DATA(ehcip)->
4391 			    ehci_sts_empty_async_schedule.value.ui64++;
4392 			break;
4393 		case EHCI_STS_HOST_CTRL_HALTED:
4394 			EHCI_INTRS_STATS_DATA(ehcip)->
4395 			    ehci_sts_host_ctrl_halted.value.ui64++;
4396 			break;
4397 		case EHCI_STS_ASYNC_ADVANCE_INTR:
4398 			EHCI_INTRS_STATS_DATA(ehcip)->
4399 			    ehci_sts_async_advance_intr.value.ui64++;
4400 			break;
4401 		case EHCI_STS_HOST_SYSTEM_ERROR_INTR:
4402 			EHCI_INTRS_STATS_DATA(ehcip)->
4403 			    ehci_sts_host_system_error_intr.value.ui64++;
4404 			break;
4405 		case EHCI_STS_FRM_LIST_ROLLOVER_INTR:
4406 			EHCI_INTRS_STATS_DATA(ehcip)->
4407 			    ehci_sts_frm_list_rollover_intr.value.ui64++;
4408 			break;
4409 		case EHCI_STS_RH_PORT_CHANGE_INTR:
4410 			EHCI_INTRS_STATS_DATA(ehcip)->
4411 			    ehci_sts_rh_port_change_intr.value.ui64++;
4412 			break;
4413 		case EHCI_STS_USB_ERROR_INTR:
4414 			EHCI_INTRS_STATS_DATA(ehcip)->
4415 			    ehci_sts_usb_error_intr.value.ui64++;
4416 			break;
4417 		case EHCI_STS_USB_INTR:
4418 			EHCI_INTRS_STATS_DATA(ehcip)->
4419 			    ehci_sts_usb_intr.value.ui64++;
4420 			break;
4421 		default:
4422 			EHCI_INTRS_STATS_DATA(ehcip)->
4423 			    ehci_sts_not_claimed.value.ui64++;
4424 			break;
4425 		}
4426 	}
4427 }
4428 
4429 
4430 /*
4431  * ehci_do_byte_stats:
4432  *
4433  * ehci data xfer information
4434  */
4435 void
4436 ehci_do_byte_stats(
4437 	ehci_state_t	*ehcip,
4438 	size_t		len,
4439 	uint8_t		attr,
4440 	uint8_t		addr)
4441 {
4442 	uint8_t 	type = attr & USB_EP_ATTR_MASK;
4443 	uint8_t 	dir = addr & USB_EP_DIR_MASK;
4444 
4445 	if (dir == USB_EP_DIR_IN) {
4446 		EHCI_TOTAL_STATS_DATA(ehcip)->reads++;
4447 		EHCI_TOTAL_STATS_DATA(ehcip)->nread += len;
4448 		switch (type) {
4449 			case USB_EP_ATTR_CONTROL:
4450 				EHCI_CTRL_STATS(ehcip)->reads++;
4451 				EHCI_CTRL_STATS(ehcip)->nread += len;
4452 				break;
4453 			case USB_EP_ATTR_BULK:
4454 				EHCI_BULK_STATS(ehcip)->reads++;
4455 				EHCI_BULK_STATS(ehcip)->nread += len;
4456 				break;
4457 			case USB_EP_ATTR_INTR:
4458 				EHCI_INTR_STATS(ehcip)->reads++;
4459 				EHCI_INTR_STATS(ehcip)->nread += len;
4460 				break;
4461 			case USB_EP_ATTR_ISOCH:
4462 				EHCI_ISOC_STATS(ehcip)->reads++;
4463 				EHCI_ISOC_STATS(ehcip)->nread += len;
4464 				break;
4465 		}
4466 	} else if (dir == USB_EP_DIR_OUT) {
4467 		EHCI_TOTAL_STATS_DATA(ehcip)->writes++;
4468 		EHCI_TOTAL_STATS_DATA(ehcip)->nwritten += len;
4469 		switch (type) {
4470 			case USB_EP_ATTR_CONTROL:
4471 				EHCI_CTRL_STATS(ehcip)->writes++;
4472 				EHCI_CTRL_STATS(ehcip)->nwritten += len;
4473 				break;
4474 			case USB_EP_ATTR_BULK:
4475 				EHCI_BULK_STATS(ehcip)->writes++;
4476 				EHCI_BULK_STATS(ehcip)->nwritten += len;
4477 				break;
4478 			case USB_EP_ATTR_INTR:
4479 				EHCI_INTR_STATS(ehcip)->writes++;
4480 				EHCI_INTR_STATS(ehcip)->nwritten += len;
4481 				break;
4482 			case USB_EP_ATTR_ISOCH:
4483 				EHCI_ISOC_STATS(ehcip)->writes++;
4484 				EHCI_ISOC_STATS(ehcip)->nwritten += len;
4485 				break;
4486 		}
4487 	}
4488 }
4489