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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #ifndef _SYS_USB_UHCID_H 28 #define _SYS_USB_UHCID_H 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 /* 37 * Universal Host Controller Driver (UHCI) 38 * 39 * The UHCI driver is a driver which interfaces to the Universal 40 * Serial Bus Driver (USBA) and the Host Controller (HC). The interface to 41 * the Host Controller is defined by the Universal Host Controller Interface. 42 * 43 * This file contains the data structures for the UHCI driver. 44 */ 45 #include <sys/types.h> 46 #include <sys/pci.h> 47 #include <sys/kstat.h> 48 49 #include <sys/usb/usba/usbai_version.h> 50 #include <sys/usb/usba.h> 51 #include <sys/usb/usba/usba_types.h> 52 53 #include <sys/usb/usba/genconsole.h> 54 #include <sys/usb/usba/hcdi.h> 55 56 #include <sys/usb/hubd/hub.h> 57 #include <sys/usb/usba/hubdi.h> 58 #include <sys/usb/hubd/hubdvar.h> 59 60 #include <sys/usb/hcd/uhci/uhci.h> 61 62 /* limit the xfer size for bulk */ 63 #define UHCI_BULK_MAX_XFER_SIZE (124*1024) /* Max bulk xfer size */ 64 65 /* Maximum allowable data transfer size per transaction */ 66 #define UHCI_MAX_TD_XFER_SIZE 0x2000 /* Maximum data per transaction */ 67 68 /* 69 * Generic UHCI Macro definitions 70 */ 71 #define UHCI_UNDERRUN_OCCURRED 0x1234 72 #define UHCI_OVERRUN_OCCURRED 0x5678 73 #define UHCI_PROP_MASK 0x01000020 74 #define UHCI_RESET_DELAY 15000 75 #define UHCI_TIMEWAIT 10000 76 77 #define MAX_SOF_WAIT_COUNT 2 78 #define MAX_RH_PORTS 2 79 #define DISCONNECTED 2 80 #define POLLING_FREQ_7MS 7 81 #define PCI_CONF_IOBASE 0x20 82 #define PCI_CONF_IOBASE_MASK 0xffe0 83 84 #define UHCI_ONE_SECOND drv_usectohz(1000000) 85 #define UHCI_ONE_MS drv_usectohz(1000) 86 #define UHCI_32_MS drv_usectohz(32*1000) 87 #define UHCI_MAX_INSTS 4 88 89 #define POLLED_RAW_BUF_SIZE 8 90 91 /* Default time out values for bulk and ctrl commands */ 92 #define UHCI_CTRL_TIMEOUT 5 93 #define UHCI_BULK_TIMEOUT 60 94 95 /* UHCI root hub structure */ 96 typedef struct uhci_root_hub_info { 97 uint_t rh_status; /* Last RH status */ 98 uint_t rh_num_ports; /* #ports on the root */ 99 100 /* Last status of ports */ 101 uint_t rh_port_status[MAX_RH_PORTS]; 102 uint_t rh_port_changes[MAX_RH_PORTS]; 103 uint_t rh_port_state[MAX_RH_PORTS]; /* See below */ 104 105 usba_pipe_handle_data_t *rh_intr_pipe_handle; /* RH intr pipe hndle */ 106 usb_hub_descr_t rh_descr; /* RH descr's copy */ 107 uint_t rh_pipe_state; /* RH intr pipe state */ 108 109 usb_intr_req_t *rh_curr_intr_reqp; /* Current intr req */ 110 usb_intr_req_t *rh_client_intr_req; /* save IN request */ 111 } uhci_root_hub_info_t; 112 113 /* 114 * UHCI Host Controller per instance data structure 115 * 116 * The Host Controller Driver (HCD) maintains the state of Host Controller 117 * (HC). There is an uhci_state structure per instance of the UHCI 118 * host controller. 119 */ 120 typedef struct uhci_state { 121 dev_info_t *uhci_dip; /* dip of HC */ 122 uint_t uhci_instance; 123 usba_hcdi_ops_t *uhci_hcdi_ops; /* HCDI structure */ 124 125 uint_t uhci_dma_addr_bind_flag; 126 127 /* Flag of uhci controller initialization */ 128 boolean_t uhci_ctlr_init_flag; 129 130 hc_regs_t *uhci_regsp; /* Host ctlr regs */ 131 ddi_acc_handle_t uhci_regs_handle; /* Reg handle */ 132 133 ddi_acc_handle_t uhci_config_handle; /* Config space hndle */ 134 135 /* Frame interval reg */ 136 uint_t uhci_frame_interval; 137 ddi_dma_attr_t uhci_dma_attr; /* DMA attributes */ 138 139 ddi_intr_handle_t *uhci_htable; /* intr handle */ 140 kmutex_t uhci_int_mutex; /* Mutex for struct */ 141 142 frame_lst_table_t *uhci_frame_lst_tablep; /* Virtual HCCA ptr */ 143 uhci_td_t *uhci_isoc_q_tailp[NUM_FRAME_LST_ENTRIES]; 144 145 ddi_dma_cookie_t uhci_flt_cookie; /* DMA cookie */ 146 ddi_dma_handle_t uhci_flt_dma_handle; /* DMA handle */ 147 ddi_acc_handle_t uhci_flt_mem_handle; /* Memory handle */ 148 149 /* 150 * There are two pools of memory. One pool contains the memory for 151 * the transfer descriptors and other pool contains the memory for 152 * the Queue Head pointers. The advantage of the pools is that it's 153 * easy to go back and forth between the iommu and the cpu addresses. 154 * 155 * The pools are protected by the int_mutex because the memory 156 * in the pools may be accessed by either the host controller or the 157 * host controller driver. 158 */ 159 160 /* General transfer descriptor pool */ 161 uhci_td_t *uhci_td_pool_addr; /* Start of the pool */ 162 ddi_dma_cookie_t uhci_td_pool_cookie; /* DMA cookie */ 163 ddi_dma_handle_t uhci_td_pool_dma_handle; /* DMA hndle */ 164 ddi_acc_handle_t uhci_td_pool_mem_handle; /* Mem hndle */ 165 166 /* Endpoint descriptor pool */ 167 queue_head_t *uhci_qh_pool_addr; /* Start of the pool */ 168 ddi_dma_cookie_t uhci_qh_pool_cookie; /* DMA cookie */ 169 ddi_dma_handle_t uhci_qh_pool_dma_handle; /* DMA handle */ 170 ddi_acc_handle_t uhci_qh_pool_mem_handle; /* Mem handle */ 171 172 /* Semaphore to serialize opens and closes */ 173 ksema_t uhci_ocsem; 174 175 /* Timeout id of the root hub status change pipe handler */ 176 timeout_id_t uhci_timeout_id; 177 178 /* Timeout id of the ctrl/bulk/intr xfers timeout */ 179 timeout_id_t uhci_cmd_timeout_id; 180 181 /* 182 * Bandwidth fields 183 * 184 * The uhci_bandwidth array keeps track of the allocated bandwidth 185 * for this host controller. The uhci_bandwidth_isoch_sum field 186 * represents the sum of the allocated isochronous bandwidth. The 187 * total bandwidth allocated for least allocated list out of the 32 188 * interrupt lists is represented by the uhci_bandwdith_intr_min 189 * field. 190 */ 191 uint_t uhci_bandwidth[NUM_FRAME_LST_ENTRIES]; 192 uint_t uhci_bandwidth_isoch_sum; 193 uint_t uhci_bandwidth_intr_min; 194 195 uhci_root_hub_info_t uhci_root_hub; /* Root hub info */ 196 197 uhci_td_t *uhci_outst_tds_head; 198 uhci_td_t *uhci_outst_tds_tail; 199 200 queue_head_t *uhci_ctrl_xfers_q_head; 201 queue_head_t *uhci_ctrl_xfers_q_tail; 202 queue_head_t *uhci_bulk_xfers_q_head; 203 queue_head_t *uhci_bulk_xfers_q_tail; 204 205 kcondvar_t uhci_cv_SOF; 206 uchar_t uhci_cv_signal; 207 208 /* Polled I/O support */ 209 frame_lst_table_t uhci_polled_save_IntTble[1024]; 210 uint_t uhci_polled_count; 211 uint32_t uhci_polled_flag; 212 213 /* Software frame number */ 214 usb_frame_number_t uhci_sw_frnum; 215 216 /* Number of pending bulk commands */ 217 uint32_t uhci_pending_bulk_cmds; 218 219 /* logging support */ 220 usb_log_handle_t uhci_log_hdl; 221 222 /* 223 * TD's used for the generation of interrupt 224 */ 225 queue_head_t *uhci_isoc_qh; 226 uhci_td_t *uhci_sof_td; 227 uhci_td_t *uhci_isoc_td; 228 229 /* 230 * Keep io base address, for debugging purpose 231 */ 232 uint_t uhci_iobase; 233 234 /* 235 * kstat structures 236 */ 237 kstat_t *uhci_intrs_stats; 238 kstat_t *uhci_total_stats; 239 kstat_t *uhci_count_stats[USB_N_COUNT_KSTATS]; 240 } uhci_state_t; 241 242 243 /* 244 * uhci_dma_addr_bind_flag values 245 * 246 * This flag indicates if the various DMA addresses allocated by the UHCI 247 * have been bound to their respective handles. This is needed to recover 248 * without errors from uhci_cleanup when it calls ddi_dma_unbind_handle() 249 */ 250 #define UHCI_TD_POOL_BOUND 0x01 /* for TD pools */ 251 #define UHCI_QH_POOL_BOUND 0x02 /* for QH pools */ 252 #define UHCI_FLA_POOL_BOUND 0x04 /* for Host Ctrlr Framelist Area */ 253 254 /* 255 * Definitions for uhci_polled_flag 256 * The flag is set to UHCI_POLLED_FLAG_FALSE by default. The flags is 257 * set to UHCI_POLLED_FLAG_TD_COMPL when shifting from normal mode to 258 * polled mode and if the normal TD is completed at that time. And the 259 * flag is set to UHCI_POLLED_FLAG_TRUE while exiting from the polled 260 * mode. In the timeout handler for root hub status change, this flag 261 * is checked. If set to UHCI_POLLED_FLAG_TRUE, the routine 262 * uhci_process_submitted_td_queue() to process the completed TD. 263 */ 264 #define UHCI_POLLED_FLAG_FALSE 0 265 #define UHCI_POLLED_FLAG_TRUE 1 266 #define UHCI_POLLED_FLAG_TD_COMPL 2 267 268 /* 269 * Pipe private structure 270 * 271 * There is an instance of this structure per pipe. This structure holds 272 * HCD specific pipe information. A pointer to this structure is kept in 273 * the USBA pipe handle (usba_pipe_handle_data_t). 274 */ 275 typedef struct uhci_pipe_private { 276 usba_pipe_handle_data_t *pp_pipe_handle; /* Back ptr to pipe handle */ 277 queue_head_t *pp_qh; /* Pipe's ept */ 278 uint_t pp_state; /* See below */ 279 usb_pipe_policy_t pp_policy; /* Copy of the pipe policy */ 280 uint_t pp_node; /* Node in lattice */ 281 uchar_t pp_data_toggle; /* save data toggle bit */ 282 283 /* 284 * Each pipe may have multiple transfer wrappers. Each transfer 285 * wrapper represents a USB transfer on the bus. A transfer is 286 * made up of one or more transactions. 287 */ 288 struct uhci_trans_wrapper *pp_tw_head; /* Head of the list */ 289 struct uhci_trans_wrapper *pp_tw_tail; /* Tail of the list */ 290 291 /* 292 * Starting frame number at which next isoc TD will be inserted 293 * for this pipe 294 */ 295 uint64_t pp_frame_num; 296 297 /* 298 * HCD gets Interrupt/Isochronous IN polling request only once and 299 * it has to insert next polling requests after completion of first 300 * request until either stop polling/pipe close is called. So HCD 301 * has to take copy of the original Interrupt/Isochronous IN request. 302 */ 303 usb_opaque_t pp_client_periodic_in_reqp; 304 } uhci_pipe_private_t; 305 306 /* warlock directives, stable data */ 307 _NOTE(MUTEX_PROTECTS_DATA(uhci_state_t::uhci_int_mutex, uhci_pipe_private_t)) 308 _NOTE(LOCK_ORDER(uhci_state::uhci_int_mutex \ 309 usba_pipe_handle_data::p_mutex \ 310 usba_device::usb_mutex \ 311 usba_ph_impl::usba_ph_mutex)) 312 _NOTE(SCHEME_PROTECTS_DATA("private mutex", kstat_io)) 313 _NOTE(SCHEME_PROTECTS_DATA("unshared", usb_isoc_pkt_descr)) 314 315 /* 316 * Pipe states 317 * 318 * uhci pipe states will be similar to usba. Refer usbai.h. 319 */ 320 #define UHCI_PIPE_STATE_IDLE 1 /* Pipe has opened,ready state */ 321 #define UHCI_PIPE_STATE_ACTIVE 2 /* Polling the endpoint,busy state */ 322 323 /* 324 * to indicate if we are in close/reset so that we can issue callbacks to 325 * IN packets that are pending 326 */ 327 #define UHCI_IN_CLOSE 4 328 #define UHCI_IN_RESET 5 329 #define UHCI_IN_ERROR 6 330 331 /* Function prototype */ 332 typedef void (*uhci_handler_function_t) (uhci_state_t *uhcip, uhci_td_t *td); 333 334 /* 335 * Transfer wrapper 336 * 337 * The transfer wrapper represents a USB transfer on the bus and there 338 * is one instance per USB transfer. A transfer is made up of one or 339 * more transactions. 340 * 341 * Control and bulk pipes will have one transfer wrapper per transfer 342 * and where as Isochronous and Interrupt pipes will only have one 343 * transfer wrapper. The transfers wrapper are continually reused for 344 * the Interrupt and Isochronous pipes as those pipes are polled. 345 */ 346 typedef struct uhci_trans_wrapper { 347 struct uhci_trans_wrapper *tw_next; /* Next wrapper */ 348 uhci_pipe_private_t *tw_pipe_private; 349 size_t tw_length; /* Txfer length */ 350 uint_t tw_tmp; /* Temp variable */ 351 ddi_dma_handle_t tw_dmahandle; /* DMA handle */ 352 ddi_acc_handle_t tw_accesshandle; /* Acc hndle */ 353 char *tw_buf; /* Buffer for txfer */ 354 ddi_dma_cookie_t tw_cookie; /* DMA cookie */ 355 int tw_ctrl_state; /* See below */ 356 uhci_td_t *tw_hctd_head; /* Head TD */ 357 uhci_td_t *tw_hctd_tail; /* Tail TD */ 358 uint_t tw_direction; /* Direction of TD */ 359 usb_flags_t tw_flags; /* Flags */ 360 361 /* 362 * This is the function to call when this td is done. This way 363 * we don't have to look in the td to figure out what kind it is. 364 */ 365 uhci_handler_function_t tw_handle_td; 366 367 /* 368 * This is the callback value used when processing a done td. 369 */ 370 usb_opaque_t tw_handle_callback_value; 371 372 uint_t tw_bytes_xfered; 373 uint_t tw_bytes_pending; 374 375 /* Maximum amount of time for this command */ 376 uint_t tw_timeout_cnt; 377 378 usb_isoc_req_t *tw_isoc_req; 379 uhci_bulk_isoc_xfer_t tw_xfer_info; 380 381 /* This is used to avoid multiple tw deallocation */ 382 uint_t tw_claim; 383 384 /* 385 * Pointer to the data in case of send command 386 */ 387 mblk_t *tw_data; 388 389 /* save a copy of current request */ 390 usb_opaque_t tw_curr_xfer_reqp; 391 } uhci_trans_wrapper_t; 392 393 /* set timeout flag so as to decrement timeout_cnt only once */ 394 #define TW_TIMEOUT_FLAG 0x1000 395 396 /* Macro for changing the data toggle */ 397 #define ADJ_DATA_TOGGLE(pp) \ 398 (pp)->pp_data_toggle = ((pp)->pp_data_toggle == 0) ? 1 : 0; 399 400 /* 401 * Macros for setting/getting information 402 */ 403 #define Get_OpReg32(addr) ddi_get32(uhcip->uhci_regs_handle, \ 404 (uint32_t *)&uhcip->uhci_regsp->addr) 405 #define Get_OpReg16(addr) ddi_get16(uhcip->uhci_regs_handle, \ 406 (uint16_t *)&uhcip->uhci_regsp->addr) 407 #define Get_OpReg8(addr) ddi_get8(uhcip->uhci_regs_handle, \ 408 (uchar_t *)&uhcip->uhci_regsp->addr) 409 410 #define Set_OpReg32(addr, val) ddi_put32(uhcip->uhci_regs_handle, \ 411 ((uint32_t *)&uhcip->uhci_regsp->addr), \ 412 ((int32_t)(val))) 413 #define Set_OpReg16(addr, val) ddi_put16(uhcip->uhci_regs_handle, \ 414 ((uint16_t *)&uhcip->uhci_regsp->addr), \ 415 ((int16_t)(val))) 416 417 #define QH_PADDR(addr) \ 418 ((uint32_t)(uhcip->uhci_qh_pool_cookie.dmac_address + \ 419 (uint32_t)((uintptr_t)(addr) - \ 420 (uintptr_t)uhcip->uhci_qh_pool_addr))) 421 422 423 #define QH_VADDR(addr) \ 424 ((void *)(((uint32_t)(addr) - \ 425 (uint32_t)uhcip->uhci_qh_pool_cookie.dmac_address) + \ 426 (char *)uhcip->uhci_qh_pool_addr)) 427 428 #define TD_PADDR(addr) \ 429 ((uint32_t)uhcip->uhci_td_pool_cookie.dmac_address + \ 430 (uint32_t)((uintptr_t)(addr) - \ 431 (uintptr_t)(uhcip->uhci_td_pool_addr))) 432 433 #define BULKTD_PADDR(x, addr)\ 434 ((uint32_t)((uintptr_t)(addr) - (uintptr_t)x->pool_addr) + \ 435 (uint32_t)(x)->cookie.dmac_address) 436 437 #define BULKTD_VADDR(x, addr)\ 438 ((void *)(((uint32_t)(addr) - \ 439 (uint32_t)(x)->cookie.dmac_address) + \ 440 (char *)(x)->pool_addr)) 441 442 #define ISOCTD_PADDR(x, addr)\ 443 ((uint32_t)((uintptr_t)(addr) - (uintptr_t)(x)->pool_addr) + \ 444 (uint32_t)(x)->cookie.dmac_address) 445 446 #define TD_VADDR(addr) \ 447 ((void *)(((uint32_t)(addr) - \ 448 (uint32_t)uhcip->uhci_td_pool_cookie.dmac_address) + \ 449 (char *)uhcip->uhci_td_pool_addr)) 450 451 /* 452 * If the terminate bit is cleared, there shouldn't be any 453 * race condition problems. If the host controller reads the 454 * bit before the driver has a chance to set the bit, the bit 455 * will be reread on the next frame. 456 */ 457 #define UHCI_SET_TERMINATE_BIT(addr) \ 458 SetQH32(uhcip, addr, GetQH32(uhcip, (addr)) | HC_END_OF_LIST) 459 #define UHCI_CLEAR_TERMINATE_BIT(addr) \ 460 SetQH32(uhcip, addr, GetQH32(uhcip, (addr)) & ~HC_END_OF_LIST) 461 462 #define UHCI_XFER_TYPE(ept) ((ept)->bmAttributes & USB_EP_ATTR_MASK) 463 #define UHCI_XFER_DIR(ept) ((ept)->bEndpointAddress & \ 464 USB_EP_DIR_MASK) 465 466 /* 467 * for HCD based kstats: 468 * uhci_intrs_stats_t structure 469 */ 470 typedef struct uhci_intrs_stats { 471 struct kstat_named uhci_intrs_hc_halted; 472 struct kstat_named uhci_intrs_hc_process_err; 473 struct kstat_named uhci_intrs_host_sys_err; 474 struct kstat_named uhci_intrs_resume_detected; 475 struct kstat_named uhci_intrs_usb_err_intr; 476 struct kstat_named uhci_intrs_usb_intr; 477 struct kstat_named uhci_intrs_total; 478 struct kstat_named uhci_intrs_not_claimed; 479 } uhci_intrs_stats_t; 480 481 /* 482 * uhci defines for kstats 483 */ 484 #define UHCI_INTRS_STATS(uhci) ((uhci)->uhci_intrs_stats) 485 #define UHCI_INTRS_STATS_DATA(uhci) \ 486 ((uhci_intrs_stats_t *)UHCI_INTRS_STATS((uhci))->ks_data) 487 488 #define UHCI_TOTAL_STATS(uhci) ((uhci)->uhci_total_stats) 489 #define UHCI_TOTAL_STATS_DATA(uhci) (KSTAT_IO_PTR((uhci)->uhci_total_stats)) 490 #define UHCI_CTRL_STATS(uhci) \ 491 (KSTAT_IO_PTR((uhci)->uhci_count_stats[USB_EP_ATTR_CONTROL])) 492 #define UHCI_BULK_STATS(uhci) \ 493 (KSTAT_IO_PTR((uhci)->uhci_count_stats[USB_EP_ATTR_BULK])) 494 #define UHCI_INTR_STATS(uhci) \ 495 (KSTAT_IO_PTR((uhci)->uhci_count_stats[USB_EP_ATTR_INTR])) 496 #define UHCI_ISOC_STATS(uhci) \ 497 (KSTAT_IO_PTR((uhci)->uhci_count_stats[USB_EP_ATTR_ISOCH])) 498 499 #define UHCI_UNIT(dev) (getminor((dev)) & ~HUBD_IS_ROOT_HUB) 500 501 #define UHCI_PERIODIC_ENDPOINT(ept) \ 502 (((((ept)->bmAttributes) & USB_EP_ATTR_MASK) == USB_EP_ATTR_INTR) || \ 503 ((((ept)->bmAttributes) & USB_EP_ATTR_MASK) == USB_EP_ATTR_ISOCH)) 504 505 /* 506 * Debug printing Masks 507 */ 508 #define PRINT_MASK_ATTA 0x00000001 /* Attach time */ 509 #define PRINT_MASK_LISTS 0x00000002 /* List management */ 510 #define PRINT_MASK_ROOT_HUB 0x00000004 /* Root hub stuff */ 511 #define PRINT_MASK_ALLOC 0x00000008 /* Alloc/dealloc descr */ 512 #define PRINT_MASK_INTR 0x00000010 /* Interrupt handling */ 513 #define PRINT_MASK_BW 0x00000020 /* Bandwidth */ 514 #define PRINT_MASK_CBOPS 0x00000040 /* CB-OPS */ 515 #define PRINT_MASK_HCDI 0x00000080 /* HCDI entry points */ 516 #define PRINT_MASK_DUMPING 0x00000100 /* Dump HCD state info */ 517 #define PRINT_MASK_ISOC 0x00000200 /* For ISOC xfers */ 518 519 #define PRINT_MASK_ALL 0xFFFFFFFF 520 521 #ifdef __cplusplus 522 } 523 #endif 524 525 #endif /* _SYS_USB_UHCID_H */ 526