1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * xHCI host controller driver 4 * 5 * Copyright (C) 2008 Intel Corp. 6 * 7 * Author: Sarah Sharp 8 * Some code borrowed from the Linux EHCI driver. 9 */ 10 11 #include <linux/usb.h> 12 #include <linux/overflow.h> 13 #include <linux/pci.h> 14 #include <linux/slab.h> 15 #include <linux/dmapool.h> 16 #include <linux/dma-mapping.h> 17 #include <linux/bitfield.h> 18 19 #include "xhci.h" 20 #include "xhci-trace.h" 21 #include "xhci-debugfs.h" 22 23 /* 24 * Allocates a generic ring segment from the ring pool, sets the dma address, 25 * initializes the segment to zero, and sets the private next pointer to NULL. 26 * 27 * Section 4.11.1.1: 28 * "All components of all Command and Transfer TRBs shall be initialized to '0'" 29 */ 30 static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, 31 unsigned int max_packet, 32 unsigned int num, 33 gfp_t flags) 34 { 35 struct xhci_segment *seg; 36 dma_addr_t dma; 37 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 38 39 seg = kzalloc_node(sizeof(*seg), flags, dev_to_node(dev)); 40 if (!seg) 41 return NULL; 42 43 seg->trbs = dma_pool_zalloc(xhci->segment_pool, flags, &dma); 44 if (!seg->trbs) { 45 kfree(seg); 46 return NULL; 47 } 48 49 if (max_packet) { 50 seg->bounce_buf = kzalloc_node(max_packet, flags, 51 dev_to_node(dev)); 52 if (!seg->bounce_buf) { 53 dma_pool_free(xhci->segment_pool, seg->trbs, dma); 54 kfree(seg); 55 return NULL; 56 } 57 } 58 seg->num = num; 59 seg->dma = dma; 60 seg->next = NULL; 61 62 return seg; 63 } 64 65 static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg) 66 { 67 if (seg->trbs) { 68 dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma); 69 seg->trbs = NULL; 70 } 71 kfree(seg->bounce_buf); 72 kfree(seg); 73 } 74 75 static void xhci_ring_segments_free(struct xhci_hcd *xhci, struct xhci_ring *ring) 76 { 77 struct xhci_segment *seg, *next; 78 79 ring->last_seg->next = NULL; 80 seg = ring->first_seg; 81 82 while (seg) { 83 next = seg->next; 84 xhci_segment_free(xhci, seg); 85 seg = next; 86 } 87 } 88 89 /* 90 * Only for transfer and command rings where driver is the producer, not for 91 * event rings. 92 * 93 * Change the last TRB in the segment to be a Link TRB which points to the 94 * DMA address of the next segment. The caller needs to set any Link TRB 95 * related flags, such as End TRB, Toggle Cycle, and no snoop. 96 */ 97 static void xhci_set_link_trb(struct xhci_segment *seg, bool chain_links) 98 { 99 union xhci_trb *trb; 100 u32 val; 101 102 if (!seg || !seg->next) 103 return; 104 105 trb = &seg->trbs[TRBS_PER_SEGMENT - 1]; 106 107 /* Set the last TRB in the segment to have a TRB type ID of Link TRB */ 108 val = le32_to_cpu(trb->link.control); 109 val &= ~TRB_TYPE_BITMASK; 110 val |= TRB_TYPE(TRB_LINK); 111 if (chain_links) 112 val |= TRB_CHAIN; 113 trb->link.control = cpu_to_le32(val); 114 trb->link.segment_ptr = cpu_to_le64(seg->next->dma); 115 } 116 117 static void xhci_initialize_ring_segments(struct xhci_hcd *xhci, struct xhci_ring *ring) 118 { 119 struct xhci_segment *seg; 120 bool chain_links; 121 122 if (ring->type == TYPE_EVENT) 123 return; 124 125 chain_links = xhci_link_chain_quirk(xhci, ring->type); 126 xhci_for_each_ring_seg(ring->first_seg, seg) 127 xhci_set_link_trb(seg, chain_links); 128 129 /* See section 4.9.2.1 and 6.4.4.1 */ 130 ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |= cpu_to_le32(LINK_TOGGLE); 131 } 132 133 void xhci_ring_init(struct xhci_hcd *xhci, struct xhci_ring *ring) 134 { 135 xhci_initialize_ring_segments(xhci, ring); 136 xhci_initialize_ring_info(ring); 137 trace_xhci_ring_alloc(ring); 138 } 139 140 /* 141 * Link the src ring segments to the dst ring. 142 * Set Toggle Cycle for the new ring if needed. 143 */ 144 static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *src, struct xhci_ring *dst) 145 { 146 struct xhci_segment *seg; 147 bool chain_links; 148 149 if (!src || !dst) 150 return; 151 152 /* If the cycle state is 0, set the cycle bit to 1 for all the TRBs */ 153 if (dst->cycle_state == 0) { 154 xhci_for_each_ring_seg(src->first_seg, seg) { 155 for (int i = 0; i < TRBS_PER_SEGMENT; i++) 156 seg->trbs[i].link.control |= cpu_to_le32(TRB_CYCLE); 157 } 158 } 159 160 src->last_seg->next = dst->enq_seg->next; 161 dst->enq_seg->next = src->first_seg; 162 if (dst->type != TYPE_EVENT) { 163 chain_links = xhci_link_chain_quirk(xhci, dst->type); 164 xhci_set_link_trb(dst->enq_seg, chain_links); 165 xhci_set_link_trb(src->last_seg, chain_links); 166 } 167 dst->num_segs += src->num_segs; 168 169 if (dst->enq_seg == dst->last_seg) { 170 if (dst->type != TYPE_EVENT) 171 dst->last_seg->trbs[TRBS_PER_SEGMENT-1].link.control 172 &= ~cpu_to_le32(LINK_TOGGLE); 173 174 dst->last_seg = src->last_seg; 175 } else if (dst->type != TYPE_EVENT) { 176 src->last_seg->trbs[TRBS_PER_SEGMENT-1].link.control &= ~cpu_to_le32(LINK_TOGGLE); 177 } 178 179 for (seg = dst->enq_seg; seg != dst->last_seg; seg = seg->next) 180 seg->next->num = seg->num + 1; 181 } 182 183 /* 184 * We need a radix tree for mapping physical addresses of TRBs to which stream 185 * ID they belong to. We need to do this because the host controller won't tell 186 * us which stream ring the TRB came from. We could store the stream ID in an 187 * event data TRB, but that doesn't help us for the cancellation case, since the 188 * endpoint may stop before it reaches that event data TRB. 189 * 190 * The radix tree maps the upper portion of the TRB DMA address to a ring 191 * segment that has the same upper portion of DMA addresses. For example, say I 192 * have segments of size 1KB, that are always 1KB aligned. A segment may 193 * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the 194 * key to the stream ID is 0x43244. I can use the DMA address of the TRB to 195 * pass the radix tree a key to get the right stream ID: 196 * 197 * 0x10c90fff >> 10 = 0x43243 198 * 0x10c912c0 >> 10 = 0x43244 199 * 0x10c91400 >> 10 = 0x43245 200 * 201 * Obviously, only those TRBs with DMA addresses that are within the segment 202 * will make the radix tree return the stream ID for that ring. 203 * 204 * Caveats for the radix tree: 205 * 206 * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an 207 * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be 208 * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the 209 * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit 210 * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit 211 * extended systems (where the DMA address can be bigger than 32-bits), 212 * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that. 213 */ 214 static int xhci_insert_segment_mapping(struct radix_tree_root *trb_address_map, 215 struct xhci_ring *ring, 216 struct xhci_segment *seg, 217 gfp_t mem_flags) 218 { 219 unsigned long key; 220 int ret; 221 222 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT); 223 /* Skip any segments that were already added. */ 224 if (radix_tree_lookup(trb_address_map, key)) 225 return 0; 226 227 ret = radix_tree_maybe_preload(mem_flags); 228 if (ret) 229 return ret; 230 ret = radix_tree_insert(trb_address_map, 231 key, ring); 232 radix_tree_preload_end(); 233 return ret; 234 } 235 236 static void xhci_remove_segment_mapping(struct radix_tree_root *trb_address_map, 237 struct xhci_segment *seg) 238 { 239 unsigned long key; 240 241 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT); 242 if (radix_tree_lookup(trb_address_map, key)) 243 radix_tree_delete(trb_address_map, key); 244 } 245 246 static int xhci_update_stream_segment_mapping( 247 struct radix_tree_root *trb_address_map, 248 struct xhci_ring *ring, 249 struct xhci_segment *first_seg, 250 gfp_t mem_flags) 251 { 252 struct xhci_segment *seg; 253 struct xhci_segment *failed_seg; 254 int ret; 255 256 if (WARN_ON_ONCE(trb_address_map == NULL)) 257 return 0; 258 259 xhci_for_each_ring_seg(first_seg, seg) { 260 ret = xhci_insert_segment_mapping(trb_address_map, 261 ring, seg, mem_flags); 262 if (ret) 263 goto remove_streams; 264 } 265 266 return 0; 267 268 remove_streams: 269 failed_seg = seg; 270 xhci_for_each_ring_seg(first_seg, seg) { 271 xhci_remove_segment_mapping(trb_address_map, seg); 272 if (seg == failed_seg) 273 return ret; 274 } 275 276 return ret; 277 } 278 279 static void xhci_remove_stream_mapping(struct xhci_ring *ring) 280 { 281 struct xhci_segment *seg; 282 283 if (WARN_ON_ONCE(ring->trb_address_map == NULL)) 284 return; 285 286 xhci_for_each_ring_seg(ring->first_seg, seg) 287 xhci_remove_segment_mapping(ring->trb_address_map, seg); 288 } 289 290 static int xhci_update_stream_mapping(struct xhci_ring *ring, gfp_t mem_flags) 291 { 292 return xhci_update_stream_segment_mapping(ring->trb_address_map, ring, 293 ring->first_seg, mem_flags); 294 } 295 296 /* XXX: Do we need the hcd structure in all these functions? */ 297 void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring) 298 { 299 if (!ring) 300 return; 301 302 trace_xhci_ring_free(ring); 303 304 if (ring->first_seg) { 305 if (ring->type == TYPE_STREAM) 306 xhci_remove_stream_mapping(ring); 307 xhci_ring_segments_free(xhci, ring); 308 } 309 310 kfree(ring); 311 } 312 313 void xhci_initialize_ring_info(struct xhci_ring *ring) 314 { 315 /* The ring is empty, so the enqueue pointer == dequeue pointer */ 316 ring->enqueue = ring->first_seg->trbs; 317 ring->enq_seg = ring->first_seg; 318 ring->dequeue = ring->enqueue; 319 ring->deq_seg = ring->first_seg; 320 /* The ring is initialized to 0. The producer must write 1 to the cycle 321 * bit to handover ownership of the TRB, so PCS = 1. The consumer must 322 * compare CCS to the cycle bit to check ownership, so CCS = 1. 323 * 324 * New rings are initialized with cycle state equal to 1; if we are 325 * handling ring expansion, set the cycle state equal to the old ring. 326 */ 327 ring->cycle_state = 1; 328 } 329 EXPORT_SYMBOL_GPL(xhci_initialize_ring_info); 330 331 /* Allocate segments and link them for a ring */ 332 static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci, struct xhci_ring *ring, gfp_t flags) 333 { 334 struct xhci_segment *prev; 335 unsigned int num = 0; 336 337 prev = xhci_segment_alloc(xhci, ring->bounce_buf_len, num, flags); 338 if (!prev) 339 return -ENOMEM; 340 num++; 341 342 ring->first_seg = prev; 343 while (num < ring->num_segs) { 344 struct xhci_segment *next; 345 346 next = xhci_segment_alloc(xhci, ring->bounce_buf_len, num, flags); 347 if (!next) 348 goto free_segments; 349 350 prev->next = next; 351 prev = next; 352 num++; 353 } 354 ring->last_seg = prev; 355 356 ring->last_seg->next = ring->first_seg; 357 return 0; 358 359 free_segments: 360 ring->last_seg = prev; 361 xhci_ring_segments_free(xhci, ring); 362 return -ENOMEM; 363 } 364 365 /* 366 * Create a new ring with zero or more segments. 367 * 368 * Link each segment together into a ring. 369 * Set the end flag and the cycle toggle bit on the last segment. 370 * See section 4.9.1 and figures 15 and 16. 371 */ 372 struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci, unsigned int num_segs, 373 enum xhci_ring_type type, unsigned int max_packet, gfp_t flags) 374 { 375 struct xhci_ring *ring; 376 int ret; 377 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 378 379 ring = kzalloc_node(sizeof(*ring), flags, dev_to_node(dev)); 380 if (!ring) 381 return NULL; 382 383 ring->num_segs = num_segs; 384 ring->bounce_buf_len = max_packet; 385 INIT_LIST_HEAD(&ring->td_list); 386 ring->type = type; 387 if (num_segs == 0) 388 return ring; 389 390 ret = xhci_alloc_segments_for_ring(xhci, ring, flags); 391 if (ret) 392 goto fail; 393 394 return ring; 395 396 fail: 397 kfree(ring); 398 return NULL; 399 } 400 401 void xhci_free_endpoint_ring(struct xhci_hcd *xhci, 402 struct xhci_virt_device *virt_dev, 403 unsigned int ep_index) 404 { 405 xhci_ring_free(xhci, virt_dev->eps[ep_index].ring); 406 virt_dev->eps[ep_index].ring = NULL; 407 } 408 409 /* 410 * Expand an existing ring. 411 * Allocate a new ring which has same segment numbers and link the two rings. 412 */ 413 int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring, 414 unsigned int num_new_segs, gfp_t flags) 415 { 416 struct xhci_ring new_ring; 417 int ret; 418 419 if (num_new_segs == 0) 420 return 0; 421 422 new_ring.num_segs = num_new_segs; 423 new_ring.bounce_buf_len = ring->bounce_buf_len; 424 new_ring.type = ring->type; 425 ret = xhci_alloc_segments_for_ring(xhci, &new_ring, flags); 426 if (ret) 427 return -ENOMEM; 428 429 xhci_initialize_ring_segments(xhci, &new_ring); 430 431 if (ring->type == TYPE_STREAM) { 432 ret = xhci_update_stream_segment_mapping(ring->trb_address_map, ring, 433 new_ring.first_seg, flags); 434 if (ret) 435 goto free_segments; 436 } 437 438 xhci_link_rings(xhci, &new_ring, ring); 439 trace_xhci_ring_expansion(ring); 440 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion, 441 "ring expansion succeed, now has %d segments", 442 ring->num_segs); 443 444 return 0; 445 446 free_segments: 447 xhci_ring_segments_free(xhci, &new_ring); 448 return ret; 449 } 450 451 struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci, 452 int type, gfp_t flags) 453 { 454 struct xhci_container_ctx *ctx; 455 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 456 457 if ((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT)) 458 return NULL; 459 460 ctx = kzalloc_node(sizeof(*ctx), flags, dev_to_node(dev)); 461 if (!ctx) 462 return NULL; 463 464 ctx->type = type; 465 ctx->size = xhci->hcc_params & HCC_64BYTE_CONTEXT ? 2048 : 1024; 466 if (type == XHCI_CTX_TYPE_INPUT) 467 ctx->size += CTX_SIZE(xhci->hcc_params); 468 469 ctx->bytes = dma_pool_zalloc(xhci->device_pool, flags, &ctx->dma); 470 if (!ctx->bytes) { 471 kfree(ctx); 472 return NULL; 473 } 474 return ctx; 475 } 476 477 void xhci_free_container_ctx(struct xhci_hcd *xhci, 478 struct xhci_container_ctx *ctx) 479 { 480 if (!ctx) 481 return; 482 dma_pool_free(xhci->device_pool, ctx->bytes, ctx->dma); 483 kfree(ctx); 484 } 485 486 struct xhci_container_ctx *xhci_alloc_port_bw_ctx(struct xhci_hcd *xhci, 487 gfp_t flags) 488 { 489 struct xhci_container_ctx *ctx; 490 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 491 492 ctx = kzalloc_node(sizeof(*ctx), flags, dev_to_node(dev)); 493 if (!ctx) 494 return NULL; 495 496 ctx->size = GET_PORT_BW_ARRAY_SIZE; 497 498 ctx->bytes = dma_pool_zalloc(xhci->port_bw_pool, flags, &ctx->dma); 499 if (!ctx->bytes) { 500 kfree(ctx); 501 return NULL; 502 } 503 return ctx; 504 } 505 506 void xhci_free_port_bw_ctx(struct xhci_hcd *xhci, 507 struct xhci_container_ctx *ctx) 508 { 509 if (!ctx) 510 return; 511 dma_pool_free(xhci->port_bw_pool, ctx->bytes, ctx->dma); 512 kfree(ctx); 513 } 514 515 struct xhci_input_control_ctx *xhci_get_input_control_ctx( 516 struct xhci_container_ctx *ctx) 517 { 518 if (ctx->type != XHCI_CTX_TYPE_INPUT) 519 return NULL; 520 521 return (struct xhci_input_control_ctx *)ctx->bytes; 522 } 523 524 struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci, 525 struct xhci_container_ctx *ctx) 526 { 527 if (ctx->type == XHCI_CTX_TYPE_DEVICE) 528 return (struct xhci_slot_ctx *)ctx->bytes; 529 530 return (struct xhci_slot_ctx *) 531 (ctx->bytes + CTX_SIZE(xhci->hcc_params)); 532 } 533 534 struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci, 535 struct xhci_container_ctx *ctx, 536 unsigned int ep_index) 537 { 538 /* increment ep index by offset of start of ep ctx array */ 539 ep_index++; 540 if (ctx->type == XHCI_CTX_TYPE_INPUT) 541 ep_index++; 542 543 return (struct xhci_ep_ctx *) 544 (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params))); 545 } 546 EXPORT_SYMBOL_GPL(xhci_get_ep_ctx); 547 548 /***************** Streams structures manipulation *************************/ 549 550 static void xhci_free_stream_ctx(struct xhci_hcd *xhci, 551 unsigned int num_stream_ctxs, 552 struct xhci_stream_ctx *stream_ctx, dma_addr_t dma) 553 { 554 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 555 size_t size = array_size(sizeof(struct xhci_stream_ctx), num_stream_ctxs); 556 557 if (size > MEDIUM_STREAM_ARRAY_SIZE) 558 dma_free_coherent(dev, size, stream_ctx, dma); 559 else if (size > SMALL_STREAM_ARRAY_SIZE) 560 dma_pool_free(xhci->medium_streams_pool, stream_ctx, dma); 561 else 562 dma_pool_free(xhci->small_streams_pool, stream_ctx, dma); 563 } 564 565 /* 566 * The stream context array for each endpoint with bulk streams enabled can 567 * vary in size, based on: 568 * - how many streams the endpoint supports, 569 * - the maximum primary stream array size the host controller supports, 570 * - and how many streams the device driver asks for. 571 * 572 * The stream context array must be a power of 2, and can be as small as 573 * 64 bytes or as large as 1MB. 574 */ 575 static struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci, 576 unsigned int num_stream_ctxs, dma_addr_t *dma, 577 gfp_t mem_flags) 578 { 579 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 580 size_t size = array_size(sizeof(struct xhci_stream_ctx), num_stream_ctxs); 581 582 if (size > MEDIUM_STREAM_ARRAY_SIZE) 583 return dma_alloc_coherent(dev, size, dma, mem_flags); 584 if (size > SMALL_STREAM_ARRAY_SIZE) 585 return dma_pool_zalloc(xhci->medium_streams_pool, mem_flags, dma); 586 else 587 return dma_pool_zalloc(xhci->small_streams_pool, mem_flags, dma); 588 } 589 590 struct xhci_ring *xhci_dma_to_transfer_ring( 591 struct xhci_virt_ep *ep, 592 u64 address) 593 { 594 if (ep->ep_state & EP_HAS_STREAMS) 595 return radix_tree_lookup(&ep->stream_info->trb_address_map, 596 address >> TRB_SEGMENT_SHIFT); 597 return ep->ring; 598 } 599 600 /* 601 * Change an endpoint's internal structure so it supports stream IDs. The 602 * number of requested streams includes stream 0, which cannot be used by device 603 * drivers. 604 * 605 * The number of stream contexts in the stream context array may be bigger than 606 * the number of streams the driver wants to use. This is because the number of 607 * stream context array entries must be a power of two. 608 */ 609 struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci, 610 unsigned int num_stream_ctxs, 611 unsigned int num_streams, 612 unsigned int max_packet, gfp_t mem_flags) 613 { 614 struct xhci_stream_info *stream_info; 615 u32 cur_stream; 616 struct xhci_ring *cur_ring; 617 u64 addr; 618 int ret; 619 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 620 621 xhci_dbg(xhci, "Allocating %u streams and %u stream context array entries.\n", 622 num_streams, num_stream_ctxs); 623 if (xhci->cmd_ring_reserved_trbs == MAX_RSVD_CMD_TRBS) { 624 xhci_dbg(xhci, "Command ring has no reserved TRBs available\n"); 625 return NULL; 626 } 627 xhci->cmd_ring_reserved_trbs++; 628 629 stream_info = kzalloc_node(sizeof(*stream_info), mem_flags, 630 dev_to_node(dev)); 631 if (!stream_info) 632 goto cleanup_trbs; 633 634 stream_info->num_streams = num_streams; 635 stream_info->num_stream_ctxs = num_stream_ctxs; 636 637 /* Initialize the array of virtual pointers to stream rings. */ 638 stream_info->stream_rings = kcalloc_node( 639 num_streams, sizeof(struct xhci_ring *), mem_flags, 640 dev_to_node(dev)); 641 if (!stream_info->stream_rings) 642 goto cleanup_info; 643 644 /* Initialize the array of DMA addresses for stream rings for the HW. */ 645 stream_info->stream_ctx_array = xhci_alloc_stream_ctx(xhci, 646 num_stream_ctxs, &stream_info->ctx_array_dma, 647 mem_flags); 648 if (!stream_info->stream_ctx_array) 649 goto cleanup_ring_array; 650 651 /* Allocate everything needed to free the stream rings later */ 652 stream_info->free_streams_command = 653 xhci_alloc_command_with_ctx(xhci, true, mem_flags); 654 if (!stream_info->free_streams_command) 655 goto cleanup_ctx; 656 657 INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC); 658 659 /* Allocate rings for all the streams that the driver will use, 660 * and add their segment DMA addresses to the radix tree. 661 * Stream 0 is reserved. 662 */ 663 664 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) { 665 stream_info->stream_rings[cur_stream] = 666 xhci_ring_alloc(xhci, 2, TYPE_STREAM, max_packet, mem_flags); 667 cur_ring = stream_info->stream_rings[cur_stream]; 668 if (!cur_ring) 669 goto cleanup_rings; 670 671 xhci_ring_init(xhci, cur_ring); 672 cur_ring->stream_id = cur_stream; 673 cur_ring->trb_address_map = &stream_info->trb_address_map; 674 /* Set deq ptr, cycle bit, and stream context type */ 675 addr = cur_ring->first_seg->dma | 676 SCT_FOR_CTX(SCT_PRI_TR) | 677 cur_ring->cycle_state; 678 stream_info->stream_ctx_array[cur_stream].stream_ring = 679 cpu_to_le64(addr); 680 xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n", cur_stream, addr); 681 682 ret = xhci_update_stream_mapping(cur_ring, mem_flags); 683 684 trace_xhci_alloc_stream_info_ctx(stream_info, cur_stream); 685 if (ret) { 686 xhci_ring_free(xhci, cur_ring); 687 stream_info->stream_rings[cur_stream] = NULL; 688 goto cleanup_rings; 689 } 690 } 691 /* Leave the other unused stream ring pointers in the stream context 692 * array initialized to zero. This will cause the xHC to give us an 693 * error if the device asks for a stream ID we don't have setup (if it 694 * was any other way, the host controller would assume the ring is 695 * "empty" and wait forever for data to be queued to that stream ID). 696 */ 697 698 return stream_info; 699 700 cleanup_rings: 701 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) { 702 cur_ring = stream_info->stream_rings[cur_stream]; 703 if (cur_ring) { 704 xhci_ring_free(xhci, cur_ring); 705 stream_info->stream_rings[cur_stream] = NULL; 706 } 707 } 708 xhci_free_command(xhci, stream_info->free_streams_command); 709 cleanup_ctx: 710 xhci_free_stream_ctx(xhci, 711 stream_info->num_stream_ctxs, 712 stream_info->stream_ctx_array, 713 stream_info->ctx_array_dma); 714 cleanup_ring_array: 715 kfree(stream_info->stream_rings); 716 cleanup_info: 717 kfree(stream_info); 718 cleanup_trbs: 719 xhci->cmd_ring_reserved_trbs--; 720 return NULL; 721 } 722 /* 723 * Sets the MaxPStreams field and the Linear Stream Array field. 724 * Sets the dequeue pointer to the stream context array. 725 */ 726 void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci, 727 struct xhci_ep_ctx *ep_ctx, 728 struct xhci_stream_info *stream_info) 729 { 730 u32 max_primary_streams; 731 /* MaxPStreams is the number of stream context array entries, not the 732 * number we're actually using. Must be in 2^(MaxPstreams + 1) format. 733 * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc. 734 */ 735 max_primary_streams = fls(stream_info->num_stream_ctxs) - 2; 736 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, 737 "Setting number of stream ctx array entries to %u", 738 1 << (max_primary_streams + 1)); 739 ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK); 740 ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams) 741 | EP_HAS_LSA); 742 ep_ctx->deq = cpu_to_le64(stream_info->ctx_array_dma); 743 } 744 745 /* 746 * Sets the MaxPStreams field and the Linear Stream Array field to 0. 747 * Reinstalls the "normal" endpoint ring (at its previous dequeue mark, 748 * not at the beginning of the ring). 749 */ 750 void xhci_setup_no_streams_ep_input_ctx(struct xhci_ep_ctx *ep_ctx, 751 struct xhci_virt_ep *ep) 752 { 753 dma_addr_t addr; 754 ep_ctx->ep_info &= cpu_to_le32(~(EP_MAXPSTREAMS_MASK | EP_HAS_LSA)); 755 addr = xhci_trb_virt_to_dma(ep->ring->deq_seg, ep->ring->dequeue); 756 ep_ctx->deq = cpu_to_le64(addr | ep->ring->cycle_state); 757 } 758 759 /* Frees all stream contexts associated with the endpoint, 760 * 761 * Caller should fix the endpoint context streams fields. 762 */ 763 void xhci_free_stream_info(struct xhci_hcd *xhci, 764 struct xhci_stream_info *stream_info) 765 { 766 int cur_stream; 767 struct xhci_ring *cur_ring; 768 769 if (!stream_info) 770 return; 771 772 for (cur_stream = 1; cur_stream < stream_info->num_streams; 773 cur_stream++) { 774 cur_ring = stream_info->stream_rings[cur_stream]; 775 if (cur_ring) { 776 xhci_ring_free(xhci, cur_ring); 777 stream_info->stream_rings[cur_stream] = NULL; 778 } 779 } 780 xhci_free_command(xhci, stream_info->free_streams_command); 781 xhci->cmd_ring_reserved_trbs--; 782 if (stream_info->stream_ctx_array) 783 xhci_free_stream_ctx(xhci, 784 stream_info->num_stream_ctxs, 785 stream_info->stream_ctx_array, 786 stream_info->ctx_array_dma); 787 788 kfree(stream_info->stream_rings); 789 kfree(stream_info); 790 } 791 792 793 /***************** Device context manipulation *************************/ 794 795 static void xhci_free_tt_info(struct xhci_hcd *xhci, 796 struct xhci_virt_device *virt_dev, 797 int slot_id) 798 { 799 struct list_head *tt_list_head; 800 struct xhci_tt_bw_info *tt_info, *next; 801 bool slot_found = false; 802 803 /* If the device never made it past the Set Address stage, 804 * it may not have the root hub port pointer set correctly. 805 */ 806 if (!virt_dev->rhub_port) { 807 xhci_dbg(xhci, "Bad rhub port.\n"); 808 return; 809 } 810 811 tt_list_head = &(xhci->rh_bw[virt_dev->rhub_port->hw_portnum].tts); 812 list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) { 813 /* Multi-TT hubs will have more than one entry */ 814 if (tt_info->slot_id == slot_id) { 815 slot_found = true; 816 list_del(&tt_info->tt_list); 817 kfree(tt_info); 818 } else if (slot_found) { 819 break; 820 } 821 } 822 } 823 824 int xhci_alloc_tt_info(struct xhci_hcd *xhci, 825 struct xhci_virt_device *virt_dev, 826 struct usb_device *hdev, 827 struct usb_tt *tt, gfp_t mem_flags) 828 { 829 struct xhci_tt_bw_info *tt_info; 830 unsigned int num_ports; 831 int i, j; 832 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 833 834 if (!tt->multi) 835 num_ports = 1; 836 else 837 num_ports = hdev->maxchild; 838 839 for (i = 0; i < num_ports; i++, tt_info++) { 840 struct xhci_interval_bw_table *bw_table; 841 842 tt_info = kzalloc_node(sizeof(*tt_info), mem_flags, 843 dev_to_node(dev)); 844 if (!tt_info) 845 goto free_tts; 846 INIT_LIST_HEAD(&tt_info->tt_list); 847 list_add(&tt_info->tt_list, 848 &xhci->rh_bw[virt_dev->rhub_port->hw_portnum].tts); 849 tt_info->slot_id = virt_dev->udev->slot_id; 850 if (tt->multi) 851 tt_info->ttport = i+1; 852 bw_table = &tt_info->bw_table; 853 for (j = 0; j < XHCI_MAX_INTERVAL; j++) 854 INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints); 855 } 856 return 0; 857 858 free_tts: 859 xhci_free_tt_info(xhci, virt_dev, virt_dev->udev->slot_id); 860 return -ENOMEM; 861 } 862 863 864 /* All the xhci_tds in the ring's TD list should be freed at this point. 865 * Should be called with xhci->lock held if there is any chance the TT lists 866 * will be manipulated by the configure endpoint, allocate device, or update 867 * hub functions while this function is removing the TT entries from the list. 868 */ 869 void xhci_free_virt_device(struct xhci_hcd *xhci, struct xhci_virt_device *dev, 870 int slot_id) 871 { 872 int i; 873 int old_active_eps = 0; 874 875 /* Slot ID 0 is reserved */ 876 if (slot_id == 0 || !dev) 877 return; 878 879 /* If device ctx array still points to _this_ device, clear it */ 880 if (dev->out_ctx && 881 xhci->dcbaa.ctx_array[slot_id] == cpu_to_le64(dev->out_ctx->dma)) 882 xhci->dcbaa.ctx_array[slot_id] = 0; 883 884 trace_xhci_free_virt_device(dev); 885 886 if (dev->tt_info) 887 old_active_eps = dev->tt_info->active_eps; 888 889 for (i = 0; i < 31; i++) { 890 if (dev->eps[i].ring) 891 xhci_ring_free(xhci, dev->eps[i].ring); 892 if (dev->eps[i].stream_info) 893 xhci_free_stream_info(xhci, 894 dev->eps[i].stream_info); 895 /* 896 * Endpoints are normally deleted from the bandwidth list when 897 * endpoints are dropped, before device is freed. 898 * If host is dying or being removed then endpoints aren't 899 * dropped cleanly, so delete the endpoint from list here. 900 * Only applicable for hosts with software bandwidth checking. 901 */ 902 903 if (!list_empty(&dev->eps[i].bw_endpoint_list)) { 904 list_del_init(&dev->eps[i].bw_endpoint_list); 905 xhci_dbg(xhci, "Slot %u endpoint %u not removed from BW list!\n", 906 slot_id, i); 907 } 908 } 909 /* If this is a hub, free the TT(s) from the TT list */ 910 xhci_free_tt_info(xhci, dev, slot_id); 911 /* If necessary, update the number of active TTs on this root port */ 912 xhci_update_tt_active_eps(xhci, dev, old_active_eps); 913 914 if (dev->in_ctx) 915 xhci_free_container_ctx(xhci, dev->in_ctx); 916 if (dev->out_ctx) 917 xhci_free_container_ctx(xhci, dev->out_ctx); 918 919 if (dev->udev && dev->udev->slot_id) 920 dev->udev->slot_id = 0; 921 if (dev->rhub_port && dev->rhub_port->slot_id == slot_id) 922 dev->rhub_port->slot_id = 0; 923 if (xhci->devs[slot_id] == dev) 924 xhci->devs[slot_id] = NULL; 925 kfree(dev); 926 } 927 928 /* 929 * Free a virt_device structure. 930 * If the virt_device added a tt_info (a hub) and has children pointing to 931 * that tt_info, then free the child first. Recursive. 932 * We can't rely on udev at this point to find child-parent relationships. 933 */ 934 void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id) 935 { 936 struct xhci_virt_device *vdev; 937 struct list_head *tt_list_head; 938 struct xhci_tt_bw_info *tt_info, *next; 939 int i; 940 941 vdev = xhci->devs[slot_id]; 942 if (!vdev) 943 return; 944 945 if (!vdev->rhub_port) { 946 xhci_dbg(xhci, "Bad rhub port.\n"); 947 goto out; 948 } 949 950 tt_list_head = &(xhci->rh_bw[vdev->rhub_port->hw_portnum].tts); 951 list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) { 952 /* is this a hub device that added a tt_info to the tts list */ 953 if (tt_info->slot_id == slot_id) { 954 /* are any devices using this tt_info? */ 955 for (i = 1; i < xhci->max_slots; i++) { 956 vdev = xhci->devs[i]; 957 if (vdev && (vdev->tt_info == tt_info)) 958 xhci_free_virt_devices_depth_first( 959 xhci, i); 960 } 961 } 962 } 963 out: 964 /* we are now at a leaf device */ 965 xhci_debugfs_remove_slot(xhci, slot_id); 966 xhci_free_virt_device(xhci, xhci->devs[slot_id], slot_id); 967 } 968 969 int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id, 970 struct usb_device *udev, gfp_t flags) 971 { 972 struct xhci_virt_device *dev; 973 int i; 974 975 /* Slot ID 0 is reserved */ 976 if (slot_id == 0 || xhci->devs[slot_id]) { 977 xhci_warn(xhci, "Bad Slot ID %d\n", slot_id); 978 return 0; 979 } 980 981 dev = kzalloc_obj(*dev, flags); 982 if (!dev) 983 return 0; 984 985 dev->slot_id = slot_id; 986 987 /* Allocate the (output) device context that will be used in the HC. */ 988 dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags); 989 if (!dev->out_ctx) 990 goto fail; 991 992 xhci_dbg(xhci, "Slot %d output ctx = %pad (dma)\n", slot_id, &dev->out_ctx->dma); 993 994 /* Allocate the (input) device context for address device command */ 995 dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags); 996 if (!dev->in_ctx) 997 goto fail; 998 999 xhci_dbg(xhci, "Slot %d input ctx = %pad (dma)\n", slot_id, &dev->in_ctx->dma); 1000 1001 /* Initialize the cancellation and bandwidth list for each ep */ 1002 for (i = 0; i < 31; i++) { 1003 dev->eps[i].ep_index = i; 1004 dev->eps[i].vdev = dev; 1005 INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list); 1006 INIT_LIST_HEAD(&dev->eps[i].bw_endpoint_list); 1007 } 1008 1009 /* Allocate endpoint 0 ring */ 1010 dev->eps[0].ring = xhci_ring_alloc(xhci, 2, TYPE_CTRL, 0, flags); 1011 if (!dev->eps[0].ring) 1012 goto fail; 1013 1014 xhci_ring_init(xhci, dev->eps[0].ring); 1015 1016 dev->udev = udev; 1017 1018 /* Point to output device context in dcbaa. */ 1019 xhci->dcbaa.ctx_array[slot_id] = cpu_to_le64(dev->out_ctx->dma); 1020 xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n", 1021 slot_id, 1022 &xhci->dcbaa.ctx_array[slot_id], 1023 le64_to_cpu(xhci->dcbaa.ctx_array[slot_id])); 1024 1025 trace_xhci_alloc_virt_device(dev); 1026 1027 xhci->devs[slot_id] = dev; 1028 1029 return 1; 1030 fail: 1031 1032 if (dev->in_ctx) 1033 xhci_free_container_ctx(xhci, dev->in_ctx); 1034 if (dev->out_ctx) 1035 xhci_free_container_ctx(xhci, dev->out_ctx); 1036 kfree(dev); 1037 1038 return 0; 1039 } 1040 1041 void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci, 1042 struct usb_device *udev) 1043 { 1044 struct xhci_virt_device *virt_dev; 1045 struct xhci_ep_ctx *ep0_ctx; 1046 struct xhci_ring *ep_ring; 1047 1048 virt_dev = xhci->devs[udev->slot_id]; 1049 ep0_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, 0); 1050 ep_ring = virt_dev->eps[0].ring; 1051 /* 1052 * FIXME we don't keep track of the dequeue pointer very well after a 1053 * Set TR dequeue pointer, so we're setting the dequeue pointer of the 1054 * host to our enqueue pointer. This should only be called after a 1055 * configured device has reset, so all control transfers should have 1056 * been completed or cancelled before the reset. 1057 */ 1058 ep0_ctx->deq = cpu_to_le64(xhci_trb_virt_to_dma(ep_ring->enq_seg, 1059 ep_ring->enqueue) 1060 | ep_ring->cycle_state); 1061 } 1062 1063 /* 1064 * The xHCI roothub may have ports of differing speeds in any order in the port 1065 * status registers. 1066 * 1067 * The xHCI hardware wants to know the roothub port that the USB device 1068 * is attached to (or the roothub port its ancestor hub is attached to). All we 1069 * know is the index of that port under either the USB 2.0 or the USB 3.0 1070 * roothub, but that doesn't give us the real index into the HW port status 1071 * registers. 1072 */ 1073 static struct xhci_port *xhci_find_rhub_port(struct xhci_hcd *xhci, struct usb_device *udev) 1074 { 1075 struct usb_device *top_dev; 1076 struct xhci_hub *rhub; 1077 struct usb_hcd *hcd; 1078 1079 if (udev->speed >= USB_SPEED_SUPER) 1080 hcd = xhci_get_usb3_hcd(xhci); 1081 else 1082 hcd = xhci->main_hcd; 1083 1084 for (top_dev = udev; top_dev->parent && top_dev->parent->parent; 1085 top_dev = top_dev->parent) 1086 /* Found device below root hub */; 1087 1088 rhub = xhci_get_rhub(hcd); 1089 return rhub->ports[top_dev->portnum - 1]; 1090 } 1091 1092 /* Setup an xHCI virtual device for a Set Address command */ 1093 int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev) 1094 { 1095 struct xhci_virt_device *dev; 1096 struct xhci_ep_ctx *ep0_ctx; 1097 struct xhci_slot_ctx *slot_ctx; 1098 u32 max_packets; 1099 1100 dev = xhci->devs[udev->slot_id]; 1101 /* Slot ID 0 is reserved */ 1102 if (udev->slot_id == 0 || !dev) { 1103 xhci_warn(xhci, "Slot ID %d is not assigned to this device\n", 1104 udev->slot_id); 1105 return -EINVAL; 1106 } 1107 ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0); 1108 slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx); 1109 1110 /* 3) Only the control endpoint is valid - one endpoint context */ 1111 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | udev->route); 1112 switch (udev->speed) { 1113 case USB_SPEED_SUPER_PLUS: 1114 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SSP); 1115 max_packets = MAX_PACKET(512); 1116 break; 1117 case USB_SPEED_SUPER: 1118 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS); 1119 max_packets = MAX_PACKET(512); 1120 break; 1121 case USB_SPEED_HIGH: 1122 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS); 1123 max_packets = MAX_PACKET(64); 1124 break; 1125 /* USB core guesses at a 64-byte max packet first for FS devices */ 1126 case USB_SPEED_FULL: 1127 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS); 1128 max_packets = MAX_PACKET(64); 1129 break; 1130 case USB_SPEED_LOW: 1131 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS); 1132 max_packets = MAX_PACKET(8); 1133 break; 1134 default: 1135 /* Speed was set earlier, this shouldn't happen. */ 1136 return -EINVAL; 1137 } 1138 /* Find the root hub port this device is under */ 1139 dev->rhub_port = xhci_find_rhub_port(xhci, udev); 1140 if (!dev->rhub_port) 1141 return -EINVAL; 1142 /* Slot ID is set to the device directly below the root hub */ 1143 if (!udev->parent->parent) 1144 dev->rhub_port->slot_id = udev->slot_id; 1145 slot_ctx->dev_info2 |= cpu_to_le32(ROOT_HUB_PORT(dev->rhub_port->hw_portnum + 1)); 1146 xhci_dbg(xhci, "Slot ID %d: HW portnum %d, hcd portnum %d\n", 1147 udev->slot_id, dev->rhub_port->hw_portnum, dev->rhub_port->hcd_portnum); 1148 1149 /* Find the right bandwidth table that this device will be a part of. 1150 * If this is a full speed device attached directly to a root port (or a 1151 * decendent of one), it counts as a primary bandwidth domain, not a 1152 * secondary bandwidth domain under a TT. An xhci_tt_info structure 1153 * will never be created for the HS root hub. 1154 */ 1155 if (!udev->tt || !udev->tt->hub->parent) { 1156 dev->bw_table = &xhci->rh_bw[dev->rhub_port->hw_portnum].bw_table; 1157 } else { 1158 struct xhci_root_port_bw_info *rh_bw; 1159 struct xhci_tt_bw_info *tt_bw; 1160 1161 rh_bw = &xhci->rh_bw[dev->rhub_port->hw_portnum]; 1162 /* Find the right TT. */ 1163 list_for_each_entry(tt_bw, &rh_bw->tts, tt_list) { 1164 if (tt_bw->slot_id != udev->tt->hub->slot_id) 1165 continue; 1166 1167 if (!dev->udev->tt->multi || 1168 (udev->tt->multi && 1169 tt_bw->ttport == dev->udev->ttport)) { 1170 dev->bw_table = &tt_bw->bw_table; 1171 dev->tt_info = tt_bw; 1172 break; 1173 } 1174 } 1175 if (!dev->tt_info) 1176 xhci_warn(xhci, "WARN: Didn't find a matching TT\n"); 1177 } 1178 1179 /* Is this a LS/FS device under an external HS hub? */ 1180 if (udev->tt && udev->tt->hub->parent) { 1181 slot_ctx->tt_info = cpu_to_le32(udev->tt->hub->slot_id | 1182 (udev->ttport << 8)); 1183 if (udev->tt->multi) 1184 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); 1185 } 1186 xhci_dbg(xhci, "udev->tt = %p\n", udev->tt); 1187 xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport); 1188 1189 /* Step 4 - ring already allocated */ 1190 /* Step 5 */ 1191 ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP)); 1192 1193 /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */ 1194 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3) | 1195 max_packets); 1196 1197 ep0_ctx->deq = cpu_to_le64(dev->eps[0].ring->first_seg->dma | 1198 dev->eps[0].ring->cycle_state); 1199 1200 ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(8)); 1201 1202 trace_xhci_setup_addressable_virt_device(dev); 1203 1204 /* Steps 7 and 8 were done in xhci_alloc_virt_device() */ 1205 1206 return 0; 1207 } 1208 1209 /* 1210 * Convert interval expressed as 2^(bInterval - 1) == interval into 1211 * straight exponent value 2^n == interval. 1212 * 1213 */ 1214 static unsigned int xhci_parse_exponent_interval(struct usb_device *udev, 1215 struct usb_host_endpoint *ep) 1216 { 1217 unsigned int interval; 1218 1219 interval = clamp_val(ep->desc.bInterval, 1, 16) - 1; 1220 if (interval != ep->desc.bInterval - 1) 1221 dev_warn(&udev->dev, 1222 "ep %#x - rounding interval to %d %sframes\n", 1223 ep->desc.bEndpointAddress, 1224 1 << interval, 1225 udev->speed == USB_SPEED_FULL ? "" : "micro"); 1226 1227 if (udev->speed == USB_SPEED_FULL) { 1228 /* 1229 * Full speed isoc endpoints specify interval in frames, 1230 * not microframes. We are using microframes everywhere, 1231 * so adjust accordingly. 1232 */ 1233 interval += 3; /* 1 frame = 2^3 uframes */ 1234 } 1235 1236 return interval; 1237 } 1238 1239 /* 1240 * Convert bInterval expressed in microframes (in 1-255 range) to exponent of 1241 * microframes, rounded down to nearest power of 2. 1242 */ 1243 static unsigned int xhci_microframes_to_exponent(struct usb_device *udev, 1244 struct usb_host_endpoint *ep, unsigned int desc_interval, 1245 unsigned int min_exponent, unsigned int max_exponent) 1246 { 1247 unsigned int interval; 1248 1249 interval = fls(desc_interval) - 1; 1250 interval = clamp_val(interval, min_exponent, max_exponent); 1251 if ((1 << interval) != desc_interval) 1252 dev_dbg(&udev->dev, 1253 "ep %#x - rounding interval to %d microframes, ep desc says %d microframes\n", 1254 ep->desc.bEndpointAddress, 1255 1 << interval, 1256 desc_interval); 1257 1258 return interval; 1259 } 1260 1261 static unsigned int xhci_parse_microframe_interval(struct usb_device *udev, 1262 struct usb_host_endpoint *ep) 1263 { 1264 if (ep->desc.bInterval == 0) 1265 return 0; 1266 return xhci_microframes_to_exponent(udev, ep, 1267 ep->desc.bInterval, 0, 15); 1268 } 1269 1270 1271 static unsigned int xhci_parse_frame_interval(struct usb_device *udev, 1272 struct usb_host_endpoint *ep) 1273 { 1274 return xhci_microframes_to_exponent(udev, ep, 1275 ep->desc.bInterval * 8, 3, 10); 1276 } 1277 1278 /* Return the polling or NAK interval. 1279 * 1280 * The polling interval is expressed in "microframes". If xHCI's Interval field 1281 * is set to N, it will service the endpoint every 2^(Interval)*125us. 1282 * 1283 * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval 1284 * is set to 0. 1285 */ 1286 static unsigned int xhci_get_endpoint_interval(struct usb_device *udev, 1287 struct usb_host_endpoint *ep) 1288 { 1289 unsigned int interval = 0; 1290 1291 switch (udev->speed) { 1292 case USB_SPEED_HIGH: 1293 /* Max NAK rate */ 1294 if (usb_endpoint_xfer_control(&ep->desc) || 1295 usb_endpoint_xfer_bulk(&ep->desc)) { 1296 interval = xhci_parse_microframe_interval(udev, ep); 1297 break; 1298 } 1299 fallthrough; /* SS and HS isoc/int have same decoding */ 1300 1301 case USB_SPEED_SUPER_PLUS: 1302 case USB_SPEED_SUPER: 1303 if (usb_endpoint_xfer_int(&ep->desc) || 1304 usb_endpoint_xfer_isoc(&ep->desc)) { 1305 interval = xhci_parse_exponent_interval(udev, ep); 1306 } 1307 break; 1308 1309 case USB_SPEED_FULL: 1310 if (usb_endpoint_xfer_isoc(&ep->desc)) { 1311 interval = xhci_parse_exponent_interval(udev, ep); 1312 break; 1313 } 1314 /* 1315 * Fall through for interrupt endpoint interval decoding 1316 * since it uses the same rules as low speed interrupt 1317 * endpoints. 1318 */ 1319 fallthrough; 1320 1321 case USB_SPEED_LOW: 1322 if (usb_endpoint_xfer_int(&ep->desc) || 1323 usb_endpoint_xfer_isoc(&ep->desc)) { 1324 1325 interval = xhci_parse_frame_interval(udev, ep); 1326 } 1327 break; 1328 1329 default: 1330 BUG(); 1331 } 1332 return interval; 1333 } 1334 1335 /* 1336 * xHCs without LEC use the "Mult" field in the endpoint context for SuperSpeed 1337 * isoc eps, and High speed isoc eps that support bandwidth doubling. Standard 1338 * High speed endpoint descriptors can define "the number of additional 1339 * transaction opportunities per microframe", but that goes in the Max Burst 1340 * endpoint context field. 1341 */ 1342 static u32 xhci_get_endpoint_mult(struct xhci_hcd *xhci, 1343 struct usb_device *udev, 1344 struct usb_host_endpoint *ep) 1345 { 1346 bool lec; 1347 1348 /* xHCI 1.1 with LEC set does not use mult field, except intel eUSB2 */ 1349 lec = xhci->hci_version > 0x100 && (xhci->hcc_params2 & HCC2_LEC); 1350 1351 /* eUSB2 double isoc bw devices are the only USB2 devices using mult */ 1352 if (usb_endpoint_is_hs_isoc_double(udev, ep) && 1353 (!lec || xhci->quirks & XHCI_INTEL_HOST)) 1354 return 1; 1355 1356 /* SuperSpeed isoc transfers on hosts without LEC uses mult field */ 1357 if (udev->speed >= USB_SPEED_SUPER && 1358 usb_endpoint_xfer_isoc(&ep->desc) && !lec) 1359 return ep->ss_ep_comp.bmAttributes; 1360 1361 return 0; 1362 } 1363 1364 static u32 xhci_get_endpoint_max_burst(struct usb_device *udev, 1365 struct usb_host_endpoint *ep) 1366 { 1367 /* Super speed and Plus have max burst in ep companion desc */ 1368 if (udev->speed >= USB_SPEED_SUPER) 1369 return ep->ss_ep_comp.bMaxBurst; 1370 1371 if (udev->speed == USB_SPEED_HIGH && 1372 (usb_endpoint_xfer_isoc(&ep->desc) || 1373 usb_endpoint_xfer_int(&ep->desc))) { 1374 /* 1375 * USB 2 Isochronous Double IN Bandwidth ECN uses fixed burst 1376 * size and max packets bits 12:11 are invalid. 1377 */ 1378 if (usb_endpoint_is_hs_isoc_double(udev, ep)) 1379 return 2; 1380 1381 return usb_endpoint_maxp_mult(&ep->desc) - 1; 1382 } 1383 1384 return 0; 1385 } 1386 1387 static u32 xhci_get_endpoint_type(struct usb_host_endpoint *ep) 1388 { 1389 int in; 1390 1391 in = usb_endpoint_dir_in(&ep->desc); 1392 1393 switch (usb_endpoint_type(&ep->desc)) { 1394 case USB_ENDPOINT_XFER_CONTROL: 1395 return CTRL_EP; 1396 case USB_ENDPOINT_XFER_BULK: 1397 return in ? BULK_IN_EP : BULK_OUT_EP; 1398 case USB_ENDPOINT_XFER_ISOC: 1399 return in ? ISOC_IN_EP : ISOC_OUT_EP; 1400 case USB_ENDPOINT_XFER_INT: 1401 return in ? INT_IN_EP : INT_OUT_EP; 1402 } 1403 return 0; 1404 } 1405 1406 /* Set up an endpoint with one ring segment. Do not allocate stream rings. 1407 * Drivers will have to call usb_alloc_streams() to do that. 1408 */ 1409 int xhci_endpoint_init(struct xhci_hcd *xhci, 1410 struct xhci_virt_device *virt_dev, 1411 struct usb_device *udev, 1412 struct usb_host_endpoint *ep, 1413 gfp_t mem_flags) 1414 { 1415 unsigned int ep_index; 1416 struct xhci_ep_ctx *ep_ctx; 1417 struct xhci_ring *ep_ring; 1418 unsigned int max_packet; 1419 enum xhci_ring_type ring_type; 1420 u32 max_esit_payload; 1421 u32 endpoint_type; 1422 unsigned int max_burst; 1423 unsigned int interval; 1424 unsigned int mult; 1425 unsigned int avg_trb_len; 1426 unsigned int err_count = 0; 1427 1428 ep_index = xhci_get_endpoint_index(&ep->desc); 1429 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); 1430 1431 endpoint_type = xhci_get_endpoint_type(ep); 1432 if (!endpoint_type) 1433 return -EINVAL; 1434 1435 ring_type = usb_endpoint_type(&ep->desc); 1436 1437 /* Ensure host supports double isoc bandwidth for eUSB2 devices */ 1438 if (usb_endpoint_is_hs_isoc_double(udev, ep) && !(xhci->hcc_params2 & HCC2_EUSB2_DIC)) { 1439 dev_dbg(&udev->dev, "Double Isoc Bandwidth not supported by xhci\n"); 1440 return -EINVAL; 1441 } 1442 1443 /* 1444 * Get values to fill the endpoint context, mostly from ep descriptor. 1445 * The average TRB buffer lengt for bulk endpoints is unclear as we 1446 * have no clue on scatter gather list entry size. For Isoc and Int, 1447 * set it to max available. See xHCI 1.1 spec 4.14.1.1 for details. 1448 */ 1449 max_esit_payload = usb_endpoint_max_periodic_payload(udev, ep); 1450 interval = xhci_get_endpoint_interval(udev, ep); 1451 1452 /* Periodic endpoint bInterval limit quirk */ 1453 if (usb_endpoint_xfer_int(&ep->desc) || 1454 usb_endpoint_xfer_isoc(&ep->desc)) { 1455 if ((xhci->quirks & XHCI_LIMIT_ENDPOINT_INTERVAL_9) && 1456 interval >= 9) { 1457 interval = 8; 1458 } 1459 if ((xhci->quirks & XHCI_LIMIT_ENDPOINT_INTERVAL_7) && 1460 udev->speed >= USB_SPEED_HIGH && 1461 interval >= 7) { 1462 interval = 6; 1463 } 1464 } 1465 1466 mult = xhci_get_endpoint_mult(xhci, udev, ep); 1467 max_packet = xhci_usb_endpoint_maxp(udev, ep); 1468 max_burst = xhci_get_endpoint_max_burst(udev, ep); 1469 avg_trb_len = max_esit_payload; 1470 1471 /* FIXME dig Mult and streams info out of ep companion desc */ 1472 1473 /* Allow 3 retries for everything but isoc, set CErr = 3 */ 1474 if (!usb_endpoint_xfer_isoc(&ep->desc)) 1475 err_count = 3; 1476 /* HS bulk max packet should be 512, FS bulk supports 8, 16, 32 or 64 */ 1477 if (usb_endpoint_xfer_bulk(&ep->desc)) { 1478 if (udev->speed == USB_SPEED_HIGH) 1479 max_packet = 512; 1480 if (udev->speed == USB_SPEED_FULL) { 1481 max_packet = rounddown_pow_of_two(max_packet); 1482 max_packet = clamp_val(max_packet, 8, 64); 1483 } 1484 } 1485 /* xHCI 1.0 and 1.1 indicates that ctrl ep avg TRB Length should be 8 */ 1486 if (usb_endpoint_xfer_control(&ep->desc) && xhci->hci_version >= 0x100) 1487 avg_trb_len = 8; 1488 1489 /* Set up the endpoint ring */ 1490 virt_dev->eps[ep_index].new_ring = 1491 xhci_ring_alloc(xhci, 2, ring_type, max_packet, mem_flags); 1492 if (!virt_dev->eps[ep_index].new_ring) 1493 return -ENOMEM; 1494 1495 virt_dev->eps[ep_index].skip = false; 1496 virt_dev->eps[ep_index].next_uframe = -1; 1497 ep_ring = virt_dev->eps[ep_index].new_ring; 1498 xhci_ring_init(xhci, ep_ring); 1499 1500 /* Fill the endpoint context */ 1501 ep_ctx->ep_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) | 1502 EP_INTERVAL(interval) | 1503 EP_MULT(mult)); 1504 ep_ctx->ep_info2 = cpu_to_le32(EP_TYPE(endpoint_type) | 1505 MAX_PACKET(max_packet) | 1506 MAX_BURST(max_burst) | 1507 ERROR_COUNT(err_count)); 1508 ep_ctx->deq = cpu_to_le64(ep_ring->first_seg->dma | 1509 ep_ring->cycle_state); 1510 1511 ep_ctx->tx_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) | 1512 EP_AVG_TRB_LENGTH(avg_trb_len)); 1513 1514 return 0; 1515 } 1516 1517 void xhci_endpoint_zero(struct xhci_hcd *xhci, 1518 struct xhci_virt_device *virt_dev, 1519 struct usb_host_endpoint *ep) 1520 { 1521 unsigned int ep_index; 1522 struct xhci_ep_ctx *ep_ctx; 1523 1524 ep_index = xhci_get_endpoint_index(&ep->desc); 1525 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); 1526 1527 ep_ctx->ep_info = 0; 1528 ep_ctx->ep_info2 = 0; 1529 ep_ctx->deq = 0; 1530 ep_ctx->tx_info = 0; 1531 /* Don't free the endpoint ring until the set interface or configuration 1532 * request succeeds. 1533 */ 1534 } 1535 1536 void xhci_clear_endpoint_bw_info(struct xhci_bw_info *bw_info) 1537 { 1538 bw_info->ep_interval = 0; 1539 bw_info->mult = 0; 1540 bw_info->num_packets = 0; 1541 bw_info->max_packet_size = 0; 1542 bw_info->type = 0; 1543 bw_info->max_esit_payload = 0; 1544 } 1545 1546 void xhci_update_bw_info(struct xhci_hcd *xhci, 1547 struct xhci_container_ctx *in_ctx, 1548 struct xhci_input_control_ctx *ctrl_ctx, 1549 struct xhci_virt_device *virt_dev) 1550 { 1551 struct xhci_bw_info *bw_info; 1552 struct xhci_ep_ctx *ep_ctx; 1553 unsigned int ep_type; 1554 int i; 1555 1556 for (i = 1; i < 31; i++) { 1557 bw_info = &virt_dev->eps[i].bw_info; 1558 1559 /* We can't tell what endpoint type is being dropped, but 1560 * unconditionally clearing the bandwidth info for non-periodic 1561 * endpoints should be harmless because the info will never be 1562 * set in the first place. 1563 */ 1564 if (!EP_IS_ADDED(ctrl_ctx, i) && EP_IS_DROPPED(ctrl_ctx, i)) { 1565 /* Dropped endpoint */ 1566 xhci_clear_endpoint_bw_info(bw_info); 1567 continue; 1568 } 1569 1570 if (EP_IS_ADDED(ctrl_ctx, i)) { 1571 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, i); 1572 ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2)); 1573 1574 /* Ignore non-periodic endpoints */ 1575 if (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP && 1576 ep_type != ISOC_IN_EP && 1577 ep_type != INT_IN_EP) 1578 continue; 1579 1580 /* Added or changed endpoint */ 1581 bw_info->ep_interval = CTX_TO_EP_INTERVAL( 1582 le32_to_cpu(ep_ctx->ep_info)); 1583 /* Number of packets and mult are zero-based in the 1584 * input context, but we want one-based for the 1585 * interval table. 1586 */ 1587 bw_info->mult = CTX_TO_EP_MULT( 1588 le32_to_cpu(ep_ctx->ep_info)) + 1; 1589 bw_info->num_packets = CTX_TO_MAX_BURST( 1590 le32_to_cpu(ep_ctx->ep_info2)) + 1; 1591 bw_info->max_packet_size = MAX_PACKET_DECODED( 1592 le32_to_cpu(ep_ctx->ep_info2)); 1593 bw_info->type = ep_type; 1594 bw_info->max_esit_payload = CTX_TO_MAX_ESIT_PAYLOAD( 1595 le32_to_cpu(ep_ctx->tx_info)); 1596 } 1597 } 1598 } 1599 1600 /* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy. 1601 * Useful when you want to change one particular aspect of the endpoint and then 1602 * issue a configure endpoint command. 1603 */ 1604 void xhci_endpoint_copy(struct xhci_hcd *xhci, 1605 struct xhci_container_ctx *in_ctx, 1606 struct xhci_container_ctx *out_ctx, 1607 unsigned int ep_index) 1608 { 1609 struct xhci_ep_ctx *out_ep_ctx; 1610 struct xhci_ep_ctx *in_ep_ctx; 1611 1612 out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); 1613 in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index); 1614 1615 in_ep_ctx->ep_info = out_ep_ctx->ep_info; 1616 in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2; 1617 in_ep_ctx->deq = out_ep_ctx->deq; 1618 in_ep_ctx->tx_info = out_ep_ctx->tx_info; 1619 if (xhci->quirks & XHCI_MTK_HOST) { 1620 in_ep_ctx->reserved[0] = out_ep_ctx->reserved[0]; 1621 in_ep_ctx->reserved[1] = out_ep_ctx->reserved[1]; 1622 } 1623 } 1624 1625 /* Copy output xhci_slot_ctx to the input xhci_slot_ctx. 1626 * Useful when you want to change one particular aspect of the endpoint and then 1627 * issue a configure endpoint command. Only the context entries field matters, 1628 * but we'll copy the whole thing anyway. 1629 */ 1630 void xhci_slot_copy(struct xhci_hcd *xhci, 1631 struct xhci_container_ctx *in_ctx, 1632 struct xhci_container_ctx *out_ctx) 1633 { 1634 struct xhci_slot_ctx *in_slot_ctx; 1635 struct xhci_slot_ctx *out_slot_ctx; 1636 1637 in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx); 1638 out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx); 1639 1640 in_slot_ctx->dev_info = out_slot_ctx->dev_info; 1641 in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2; 1642 in_slot_ctx->tt_info = out_slot_ctx->tt_info; 1643 in_slot_ctx->dev_state = out_slot_ctx->dev_state; 1644 } 1645 1646 /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */ 1647 static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags) 1648 { 1649 int i; 1650 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 1651 int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2); 1652 1653 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 1654 "Allocating %d scratchpad buffers", num_sp); 1655 1656 if (!num_sp) 1657 return 0; 1658 1659 xhci->scratchpad = kzalloc_node(sizeof(*xhci->scratchpad), flags, 1660 dev_to_node(dev)); 1661 if (!xhci->scratchpad) 1662 goto fail_sp; 1663 1664 xhci->scratchpad->sp_array = dma_alloc_coherent(dev, 1665 array_size(sizeof(u64), num_sp), 1666 &xhci->scratchpad->sp_dma, flags); 1667 if (!xhci->scratchpad->sp_array) 1668 goto fail_sp2; 1669 1670 xhci->scratchpad->sp_buffers = kcalloc_node(num_sp, sizeof(void *), 1671 flags, dev_to_node(dev)); 1672 if (!xhci->scratchpad->sp_buffers) 1673 goto fail_sp3; 1674 1675 xhci->dcbaa.ctx_array[0] = cpu_to_le64(xhci->scratchpad->sp_dma); 1676 for (i = 0; i < num_sp; i++) { 1677 dma_addr_t dma; 1678 void *buf = dma_alloc_coherent(dev, xhci->page_size, &dma, 1679 flags); 1680 if (!buf) 1681 goto fail_sp4; 1682 1683 xhci->scratchpad->sp_array[i] = dma; 1684 xhci->scratchpad->sp_buffers[i] = buf; 1685 } 1686 1687 return 0; 1688 1689 fail_sp4: 1690 while (i--) 1691 dma_free_coherent(dev, xhci->page_size, 1692 xhci->scratchpad->sp_buffers[i], 1693 xhci->scratchpad->sp_array[i]); 1694 1695 kfree(xhci->scratchpad->sp_buffers); 1696 1697 fail_sp3: 1698 dma_free_coherent(dev, array_size(sizeof(u64), num_sp), 1699 xhci->scratchpad->sp_array, 1700 xhci->scratchpad->sp_dma); 1701 1702 fail_sp2: 1703 kfree(xhci->scratchpad); 1704 xhci->scratchpad = NULL; 1705 1706 fail_sp: 1707 return -ENOMEM; 1708 } 1709 1710 static void scratchpad_free(struct xhci_hcd *xhci) 1711 { 1712 int num_sp; 1713 int i; 1714 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 1715 1716 if (!xhci->scratchpad) 1717 return; 1718 1719 num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2); 1720 1721 for (i = 0; i < num_sp; i++) { 1722 dma_free_coherent(dev, xhci->page_size, 1723 xhci->scratchpad->sp_buffers[i], 1724 xhci->scratchpad->sp_array[i]); 1725 } 1726 kfree(xhci->scratchpad->sp_buffers); 1727 dma_free_coherent(dev, array_size(sizeof(u64), num_sp), 1728 xhci->scratchpad->sp_array, 1729 xhci->scratchpad->sp_dma); 1730 kfree(xhci->scratchpad); 1731 xhci->scratchpad = NULL; 1732 } 1733 1734 struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci, 1735 bool allocate_completion, gfp_t mem_flags) 1736 { 1737 struct xhci_command *command; 1738 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 1739 1740 command = kzalloc_node(sizeof(*command), mem_flags, dev_to_node(dev)); 1741 if (!command) 1742 return NULL; 1743 1744 if (allocate_completion) { 1745 command->completion = 1746 kzalloc_node(sizeof(struct completion), mem_flags, 1747 dev_to_node(dev)); 1748 if (!command->completion) { 1749 kfree(command); 1750 return NULL; 1751 } 1752 init_completion(command->completion); 1753 } 1754 1755 command->status = 0; 1756 /* set default timeout to 5000 ms */ 1757 command->timeout_ms = XHCI_CMD_DEFAULT_TIMEOUT; 1758 INIT_LIST_HEAD(&command->cmd_list); 1759 return command; 1760 } 1761 1762 struct xhci_command *xhci_alloc_command_with_ctx(struct xhci_hcd *xhci, 1763 bool allocate_completion, gfp_t mem_flags) 1764 { 1765 struct xhci_command *command; 1766 1767 command = xhci_alloc_command(xhci, allocate_completion, mem_flags); 1768 if (!command) 1769 return NULL; 1770 1771 command->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, 1772 mem_flags); 1773 if (!command->in_ctx) { 1774 kfree(command->completion); 1775 kfree(command); 1776 return NULL; 1777 } 1778 return command; 1779 } 1780 1781 void xhci_urb_free_priv(struct urb_priv *urb_priv) 1782 { 1783 kfree(urb_priv); 1784 } 1785 1786 void xhci_free_command(struct xhci_hcd *xhci, 1787 struct xhci_command *command) 1788 { 1789 xhci_free_container_ctx(xhci, 1790 command->in_ctx); 1791 kfree(command->completion); 1792 kfree(command); 1793 } 1794 1795 static int xhci_alloc_erst(struct xhci_hcd *xhci, 1796 struct xhci_ring *evt_ring, 1797 struct xhci_erst *erst, 1798 gfp_t flags) 1799 { 1800 size_t size; 1801 unsigned int val; 1802 struct xhci_segment *seg; 1803 struct xhci_erst_entry *entry; 1804 1805 size = array_size(sizeof(struct xhci_erst_entry), evt_ring->num_segs); 1806 erst->entries = dma_alloc_coherent(xhci_to_hcd(xhci)->self.sysdev, 1807 size, &erst->erst_dma_addr, flags); 1808 if (!erst->entries) 1809 return -ENOMEM; 1810 1811 erst->num_entries = evt_ring->num_segs; 1812 1813 seg = evt_ring->first_seg; 1814 for (val = 0; val < evt_ring->num_segs; val++) { 1815 entry = &erst->entries[val]; 1816 entry->seg_addr = cpu_to_le64(seg->dma); 1817 entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT); 1818 entry->rsvd = 0; 1819 seg = seg->next; 1820 } 1821 1822 return 0; 1823 } 1824 1825 static void 1826 xhci_remove_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir) 1827 { 1828 u32 tmp; 1829 1830 if (!ir) 1831 return; 1832 1833 /* 1834 * Clean out interrupter registers except ERSTBA. Clearing either the 1835 * low or high 32 bits of ERSTBA immediately causes the controller to 1836 * dereference the partially cleared 64 bit address, causing IOMMU error. 1837 */ 1838 if (ir->ir_set) { 1839 tmp = readl(&ir->ir_set->erst_size); 1840 tmp &= ~ERST_SIZE_MASK; 1841 writel(tmp, &ir->ir_set->erst_size); 1842 1843 xhci_update_erst_dequeue(xhci, ir, true); 1844 } 1845 } 1846 1847 static void 1848 xhci_free_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir) 1849 { 1850 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 1851 size_t erst_size; 1852 1853 if (!ir) 1854 return; 1855 1856 erst_size = array_size(sizeof(struct xhci_erst_entry), ir->erst.num_entries); 1857 if (ir->erst.entries) 1858 dma_free_coherent(dev, erst_size, 1859 ir->erst.entries, 1860 ir->erst.erst_dma_addr); 1861 ir->erst.entries = NULL; 1862 1863 /* free interrupter event ring */ 1864 if (ir->event_ring) 1865 xhci_ring_free(xhci, ir->event_ring); 1866 1867 ir->event_ring = NULL; 1868 1869 kfree(ir); 1870 } 1871 1872 void xhci_remove_secondary_interrupter(struct usb_hcd *hcd, struct xhci_interrupter *ir) 1873 { 1874 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 1875 unsigned int intr_num; 1876 1877 spin_lock_irq(&xhci->lock); 1878 1879 /* interrupter 0 is primary interrupter, don't touch it */ 1880 if (!ir || !ir->intr_num || ir->intr_num >= xhci->max_interrupters) { 1881 xhci_dbg(xhci, "Invalid secondary interrupter, can't remove\n"); 1882 spin_unlock_irq(&xhci->lock); 1883 return; 1884 } 1885 1886 /* 1887 * Cleanup secondary interrupter to ensure there are no pending events. 1888 * This also updates event ring dequeue pointer back to the start. 1889 */ 1890 xhci_skip_sec_intr_events(xhci, ir->event_ring, ir); 1891 intr_num = ir->intr_num; 1892 1893 xhci_remove_interrupter(xhci, ir); 1894 xhci->interrupters[intr_num] = NULL; 1895 1896 spin_unlock_irq(&xhci->lock); 1897 1898 xhci_free_interrupter(xhci, ir); 1899 } 1900 EXPORT_SYMBOL_GPL(xhci_remove_secondary_interrupter); 1901 1902 /* Cleanup roothub bandwidth data */ 1903 void xhci_rh_bw_cleanup(struct xhci_hcd *xhci) 1904 { 1905 struct xhci_root_port_bw_info *rh_bw; 1906 struct xhci_tt_bw_info *tt_info, *tt_next; 1907 struct list_head *eps, *ep, *ep_next; 1908 1909 for (int i = 0; i < xhci->max_ports; i++) { 1910 rh_bw = &xhci->rh_bw[i]; 1911 1912 /* Clear and free all TT bandwidth entries */ 1913 list_for_each_entry_safe(tt_info, tt_next, &rh_bw->tts, tt_list) { 1914 list_del(&tt_info->tt_list); 1915 kfree(tt_info); 1916 } 1917 1918 /* Clear per-interval endpoint lists */ 1919 for (int j = 0; j < XHCI_MAX_INTERVAL; j++) { 1920 eps = &rh_bw->bw_table.interval_bw[j].endpoints; 1921 1922 list_for_each_safe(ep, ep_next, eps) 1923 list_del_init(ep); 1924 } 1925 } 1926 } 1927 1928 void xhci_mem_cleanup(struct xhci_hcd *xhci) 1929 { 1930 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 1931 struct xhci_device_context_array *dcbaa; 1932 int i; 1933 1934 cancel_delayed_work_sync(&xhci->cmd_timer); 1935 1936 for (i = 0; xhci->interrupters && i < xhci->max_interrupters; i++) { 1937 if (xhci->interrupters[i]) { 1938 xhci_remove_interrupter(xhci, xhci->interrupters[i]); 1939 xhci_free_interrupter(xhci, xhci->interrupters[i]); 1940 xhci->interrupters[i] = NULL; 1941 } 1942 } 1943 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed interrupters"); 1944 1945 if (xhci->cmd_ring) 1946 xhci_ring_free(xhci, xhci->cmd_ring); 1947 xhci->cmd_ring = NULL; 1948 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed command ring"); 1949 xhci_cleanup_command_queue(xhci); 1950 1951 if (xhci->devs) { 1952 for (i = xhci->max_slots; i > 0; i--) 1953 xhci_free_virt_devices_depth_first(xhci, i); 1954 kfree(xhci->devs); 1955 } 1956 1957 dma_pool_destroy(xhci->segment_pool); 1958 xhci->segment_pool = NULL; 1959 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed segment pool"); 1960 1961 dma_pool_destroy(xhci->device_pool); 1962 xhci->device_pool = NULL; 1963 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed device context pool"); 1964 1965 dma_pool_destroy(xhci->small_streams_pool); 1966 xhci->small_streams_pool = NULL; 1967 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 1968 "Freed small stream array pool"); 1969 1970 dma_pool_destroy(xhci->port_bw_pool); 1971 xhci->port_bw_pool = NULL; 1972 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 1973 "Freed xhci port bw array pool"); 1974 1975 dma_pool_destroy(xhci->medium_streams_pool); 1976 xhci->medium_streams_pool = NULL; 1977 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 1978 "Freed medium stream array pool"); 1979 1980 dcbaa = &xhci->dcbaa; 1981 if (dcbaa->ctx_array) { 1982 dma_free_coherent(dev, array_size(sizeof(*dcbaa->ctx_array), xhci->max_slots + 1), 1983 dcbaa->ctx_array, dcbaa->dma); 1984 dcbaa->ctx_array = NULL; 1985 } 1986 1987 scratchpad_free(xhci); 1988 1989 if (xhci->rh_bw) 1990 xhci_rh_bw_cleanup(xhci); 1991 1992 xhci->cmd_ring_reserved_trbs = 0; 1993 xhci->usb2_rhub.num_ports = 0; 1994 xhci->usb3_rhub.num_ports = 0; 1995 xhci->num_active_eps = 0; 1996 kfree(xhci->usb2_rhub.ports); 1997 kfree(xhci->usb3_rhub.ports); 1998 kfree(xhci->hw_ports); 1999 kfree(xhci->rh_bw); 2000 for (i = 0; i < xhci->num_port_caps; i++) 2001 kfree(xhci->port_caps[i].psi); 2002 kfree(xhci->port_caps); 2003 kfree(xhci->interrupters); 2004 xhci->num_port_caps = 0; 2005 2006 xhci->usb2_rhub.ports = NULL; 2007 xhci->usb3_rhub.ports = NULL; 2008 xhci->hw_ports = NULL; 2009 xhci->rh_bw = NULL; 2010 xhci->port_caps = NULL; 2011 xhci->interrupters = NULL; 2012 xhci->devs = NULL; 2013 2014 xhci->usb2_rhub.bus_state.bus_suspended = 0; 2015 xhci->usb3_rhub.bus_state.bus_suspended = 0; 2016 } 2017 2018 static void xhci_set_hc_event_deq(struct xhci_hcd *xhci, struct xhci_interrupter *ir) 2019 { 2020 dma_addr_t deq; 2021 2022 deq = xhci_trb_virt_to_dma(ir->event_ring->deq_seg, 2023 ir->event_ring->dequeue); 2024 if (!deq) 2025 xhci_warn(xhci, "WARN something wrong with SW event ring dequeue ptr.\n"); 2026 /* Update HC event ring dequeue pointer */ 2027 /* Don't clear the EHB bit (which is RW1C) because 2028 * there might be more events to service. 2029 */ 2030 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 2031 "// Write event ring dequeue pointer, preserving EHB bit"); 2032 xhci_write_64(xhci, deq & ERST_PTR_MASK, &ir->ir_set->erst_dequeue); 2033 } 2034 2035 static void xhci_add_in_port(struct xhci_hcd *xhci, unsigned int num_ports, 2036 __le32 __iomem *addr, int max_caps) 2037 { 2038 u32 temp, port_offset, port_count; 2039 int i; 2040 u8 major_revision, minor_revision, tmp_minor_revision; 2041 struct xhci_hub *rhub; 2042 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 2043 struct xhci_port_cap *port_cap; 2044 2045 temp = readl(addr); 2046 major_revision = XHCI_EXT_PORT_MAJOR(temp); 2047 minor_revision = XHCI_EXT_PORT_MINOR(temp); 2048 2049 if (major_revision == 0x03) { 2050 rhub = &xhci->usb3_rhub; 2051 /* 2052 * Some hosts incorrectly use sub-minor version for minor 2053 * version (i.e. 0x02 instead of 0x20 for bcdUSB 0x320 and 0x01 2054 * for bcdUSB 0x310). Since there is no USB release with sub 2055 * minor version 0x301 to 0x309, we can assume that they are 2056 * incorrect and fix it here. 2057 */ 2058 if (minor_revision > 0x00 && minor_revision < 0x10) 2059 minor_revision <<= 4; 2060 /* 2061 * Some zhaoxin's xHCI controller that follow usb3.1 spec 2062 * but only support Gen1. 2063 */ 2064 if (xhci->quirks & XHCI_ZHAOXIN_HOST) { 2065 tmp_minor_revision = minor_revision; 2066 minor_revision = 0; 2067 } 2068 2069 } else if (major_revision <= 0x02) { 2070 rhub = &xhci->usb2_rhub; 2071 } else { 2072 xhci_warn(xhci, "Ignoring unknown port speed, Ext Cap %p, revision = 0x%x\n", 2073 addr, major_revision); 2074 /* Ignoring port protocol we can't understand. FIXME */ 2075 return; 2076 } 2077 2078 /* Port offset and count in the third dword, see section 7.2 */ 2079 temp = readl(addr + 2); 2080 port_offset = XHCI_EXT_PORT_OFF(temp); 2081 port_count = XHCI_EXT_PORT_COUNT(temp); 2082 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 2083 "Ext Cap %p, port offset = %u, count = %u, revision = 0x%x", 2084 addr, port_offset, port_count, major_revision); 2085 /* Port count includes the current port offset */ 2086 if (port_offset == 0 || (port_offset + port_count - 1) > num_ports) 2087 /* WTF? "Valid values are '1' to MaxPorts" */ 2088 return; 2089 2090 port_cap = &xhci->port_caps[xhci->num_port_caps++]; 2091 if (xhci->num_port_caps > max_caps) 2092 return; 2093 2094 port_cap->psi_count = XHCI_EXT_PORT_PSIC(temp); 2095 2096 if (port_cap->psi_count) { 2097 port_cap->psi = kcalloc_node(port_cap->psi_count, 2098 sizeof(*port_cap->psi), 2099 GFP_KERNEL, dev_to_node(dev)); 2100 if (!port_cap->psi) 2101 port_cap->psi_count = 0; 2102 2103 port_cap->psi_uid_count++; 2104 for (i = 0; i < port_cap->psi_count; i++) { 2105 port_cap->psi[i] = readl(addr + 4 + i); 2106 2107 /* count unique ID values, two consecutive entries can 2108 * have the same ID if link is assymetric 2109 */ 2110 if (i && (XHCI_EXT_PORT_PSIV(port_cap->psi[i]) != 2111 XHCI_EXT_PORT_PSIV(port_cap->psi[i - 1]))) 2112 port_cap->psi_uid_count++; 2113 2114 if (xhci->quirks & XHCI_ZHAOXIN_HOST && 2115 major_revision == 0x03 && 2116 XHCI_EXT_PORT_PSIV(port_cap->psi[i]) >= 5) 2117 minor_revision = tmp_minor_revision; 2118 2119 xhci_dbg(xhci, "PSIV:%d PSIE:%d PLT:%d PFD:%d LP:%d PSIM:%d\n", 2120 XHCI_EXT_PORT_PSIV(port_cap->psi[i]), 2121 XHCI_EXT_PORT_PSIE(port_cap->psi[i]), 2122 XHCI_EXT_PORT_PLT(port_cap->psi[i]), 2123 XHCI_EXT_PORT_PFD(port_cap->psi[i]), 2124 XHCI_EXT_PORT_LP(port_cap->psi[i]), 2125 XHCI_EXT_PORT_PSIM(port_cap->psi[i])); 2126 } 2127 } 2128 2129 rhub->maj_rev = major_revision; 2130 2131 if (rhub->min_rev < minor_revision) 2132 rhub->min_rev = minor_revision; 2133 2134 port_cap->maj_rev = major_revision; 2135 port_cap->min_rev = minor_revision; 2136 port_cap->protocol_caps = temp; 2137 2138 if ((xhci->hci_version >= 0x100) && (major_revision != 0x03) && 2139 (temp & XHCI_HLC)) { 2140 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 2141 "xHCI 1.0: support USB2 hardware lpm"); 2142 xhci->hw_lpm_support = 1; 2143 } 2144 2145 port_offset--; 2146 for (i = port_offset; i < (port_offset + port_count); i++) { 2147 struct xhci_port *hw_port = &xhci->hw_ports[i]; 2148 /* Duplicate entry. Ignore the port if the revisions differ. */ 2149 if (hw_port->rhub) { 2150 xhci_warn(xhci, "Duplicate port entry, Ext Cap %p, port %u\n", addr, i); 2151 xhci_warn(xhci, "Port was marked as USB %u, duplicated as USB %u\n", 2152 hw_port->rhub->maj_rev, major_revision); 2153 /* Only adjust the roothub port counts if we haven't 2154 * found a similar duplicate. 2155 */ 2156 if (hw_port->rhub != rhub && 2157 hw_port->hcd_portnum != DUPLICATE_ENTRY) { 2158 hw_port->rhub->num_ports--; 2159 hw_port->hcd_portnum = DUPLICATE_ENTRY; 2160 } 2161 continue; 2162 } 2163 hw_port->rhub = rhub; 2164 hw_port->port_cap = port_cap; 2165 rhub->num_ports++; 2166 } 2167 /* FIXME: Should we disable ports not in the Extended Capabilities? */ 2168 } 2169 2170 static void xhci_create_rhub_port_array(struct xhci_hcd *xhci, struct xhci_hub *rhub, 2171 unsigned int max_ports, gfp_t flags) 2172 { 2173 int port_index = 0; 2174 int i; 2175 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 2176 2177 if (!rhub->num_ports) { 2178 xhci_info(xhci, "USB%u root hub has no ports\n", rhub->maj_rev); 2179 return; 2180 } 2181 2182 /* 2183 * Place limits on the number of roothub ports so that the hub 2184 * descriptors aren't longer than the USB core will allocate. 2185 */ 2186 if (rhub->num_ports > max_ports) { 2187 xhci->usb3_rhub.num_ports = max_ports; 2188 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Limiting USB%u root hub ports to %u", 2189 rhub->maj_rev, max_ports); 2190 } 2191 2192 rhub->ports = kcalloc_node(rhub->num_ports, sizeof(*rhub->ports), 2193 flags, dev_to_node(dev)); 2194 if (!rhub->ports) 2195 return; 2196 2197 for (i = 0; i < xhci->max_ports; i++) { 2198 if (xhci->hw_ports[i].rhub != rhub || 2199 xhci->hw_ports[i].hcd_portnum == DUPLICATE_ENTRY) 2200 continue; 2201 xhci->hw_ports[i].hcd_portnum = port_index; 2202 rhub->ports[port_index] = &xhci->hw_ports[i]; 2203 port_index++; 2204 if (port_index == rhub->num_ports) 2205 break; 2206 } 2207 } 2208 2209 /* 2210 * Scan the Extended Capabilities for the "Supported Protocol Capabilities" that 2211 * specify what speeds each port is supposed to be. We can't count on the port 2212 * speed bits in the PORTSC register being correct until a device is connected, 2213 * but we need to set up the two fake roothubs with the correct number of USB 2214 * 3.0 and USB 2.0 ports at host controller initialization time. 2215 */ 2216 static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags) 2217 { 2218 void __iomem *base; 2219 u32 offset; 2220 int i, j; 2221 int cap_count = 0; 2222 u32 cap_start; 2223 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 2224 2225 xhci->hw_ports = kcalloc_node(xhci->max_ports, sizeof(*xhci->hw_ports), 2226 flags, dev_to_node(dev)); 2227 if (!xhci->hw_ports) 2228 return -ENOMEM; 2229 2230 for (i = 0; i < xhci->max_ports; i++) { 2231 xhci->hw_ports[i].port_reg = &xhci->op_regs->port_regs[i]; 2232 xhci->hw_ports[i].hw_portnum = i; 2233 2234 init_completion(&xhci->hw_ports[i].rexit_done); 2235 init_completion(&xhci->hw_ports[i].u3exit_done); 2236 } 2237 2238 xhci->rh_bw = kcalloc_node(xhci->max_ports, sizeof(*xhci->rh_bw), flags, dev_to_node(dev)); 2239 if (!xhci->rh_bw) 2240 return -ENOMEM; 2241 for (i = 0; i < xhci->max_ports; i++) { 2242 struct xhci_interval_bw_table *bw_table; 2243 2244 INIT_LIST_HEAD(&xhci->rh_bw[i].tts); 2245 bw_table = &xhci->rh_bw[i].bw_table; 2246 for (j = 0; j < XHCI_MAX_INTERVAL; j++) 2247 INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints); 2248 } 2249 base = &xhci->cap_regs->hc_capbase; 2250 2251 cap_start = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_PROTOCOL); 2252 if (!cap_start) { 2253 xhci_err(xhci, "No Extended Capability registers, unable to set up roothub\n"); 2254 return -ENODEV; 2255 } 2256 2257 offset = cap_start; 2258 /* count extended protocol capability entries for later caching */ 2259 while (offset) { 2260 cap_count++; 2261 offset = xhci_find_next_ext_cap(base, offset, 2262 XHCI_EXT_CAPS_PROTOCOL); 2263 } 2264 2265 xhci->port_caps = kcalloc_node(cap_count, sizeof(*xhci->port_caps), 2266 flags, dev_to_node(dev)); 2267 if (!xhci->port_caps) 2268 return -ENOMEM; 2269 2270 offset = cap_start; 2271 2272 while (offset) { 2273 xhci_add_in_port(xhci, xhci->max_ports, base + offset, cap_count); 2274 if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports == xhci->max_ports) 2275 break; 2276 offset = xhci_find_next_ext_cap(base, offset, 2277 XHCI_EXT_CAPS_PROTOCOL); 2278 } 2279 if (xhci->usb2_rhub.num_ports == 0 && xhci->usb3_rhub.num_ports == 0) { 2280 xhci_warn(xhci, "No ports on the roothubs?\n"); 2281 return -ENODEV; 2282 } 2283 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 2284 "Found %u USB 2.0 ports and %u USB 3.0 ports.", 2285 xhci->usb2_rhub.num_ports, xhci->usb3_rhub.num_ports); 2286 2287 xhci_create_rhub_port_array(xhci, &xhci->usb2_rhub, USB_MAXCHILDREN, flags); 2288 xhci_create_rhub_port_array(xhci, &xhci->usb3_rhub, USB_SS_MAXPORTS, flags); 2289 2290 return 0; 2291 } 2292 2293 static struct xhci_interrupter * 2294 xhci_alloc_interrupter(struct xhci_hcd *xhci, unsigned int segs, gfp_t flags) 2295 { 2296 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 2297 struct xhci_interrupter *ir; 2298 unsigned int max_segs; 2299 int ret; 2300 2301 if (!segs) 2302 segs = ERST_DEFAULT_SEGS; 2303 2304 max_segs = FIELD_GET(HCS_ERST_MAX, xhci->hcs_params2) << 2; 2305 segs = min(segs, max_segs); 2306 2307 ir = kzalloc_node(sizeof(*ir), flags, dev_to_node(dev)); 2308 if (!ir) 2309 return NULL; 2310 2311 ir->event_ring = xhci_ring_alloc(xhci, segs, TYPE_EVENT, 0, flags); 2312 if (!ir->event_ring) { 2313 xhci_warn(xhci, "Failed to allocate interrupter event ring\n"); 2314 kfree(ir); 2315 return NULL; 2316 } 2317 2318 ret = xhci_alloc_erst(xhci, ir->event_ring, &ir->erst, flags); 2319 if (ret) { 2320 xhci_warn(xhci, "Failed to allocate interrupter erst\n"); 2321 xhci_ring_free(xhci, ir->event_ring); 2322 kfree(ir); 2323 return NULL; 2324 } 2325 2326 return ir; 2327 } 2328 2329 void xhci_add_interrupter(struct xhci_hcd *xhci, unsigned int intr_num) 2330 { 2331 struct xhci_interrupter *ir; 2332 u64 erst_base; 2333 u32 erst_size; 2334 2335 ir = xhci->interrupters[intr_num]; 2336 ir->intr_num = intr_num; 2337 ir->ir_set = &xhci->run_regs->ir_set[intr_num]; 2338 2339 /* set ERST count with the number of entries in the segment table */ 2340 erst_size = readl(&ir->ir_set->erst_size); 2341 erst_size &= ~ERST_SIZE_MASK; 2342 erst_size |= ir->event_ring->num_segs; 2343 writel(erst_size, &ir->ir_set->erst_size); 2344 2345 erst_base = xhci_read_64(xhci, &ir->ir_set->erst_base); 2346 erst_base &= ~ERST_BASE_ADDRESS_MASK; 2347 erst_base |= ir->erst.erst_dma_addr & ERST_BASE_ADDRESS_MASK; 2348 if (xhci->quirks & XHCI_WRITE_64_HI_LO) 2349 hi_lo_writeq(erst_base, &ir->ir_set->erst_base); 2350 else 2351 xhci_write_64(xhci, erst_base, &ir->ir_set->erst_base); 2352 2353 /* Set the event ring dequeue address of this interrupter */ 2354 xhci_set_hc_event_deq(xhci, ir); 2355 } 2356 2357 struct xhci_interrupter * 2358 xhci_create_secondary_interrupter(struct usb_hcd *hcd, unsigned int segs, 2359 u32 imod_interval, unsigned int intr_num) 2360 { 2361 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 2362 struct xhci_interrupter *ir; 2363 unsigned int i; 2364 int err = -ENOSPC; 2365 2366 if (!xhci->interrupters || xhci->max_interrupters <= 1 || 2367 intr_num >= xhci->max_interrupters) 2368 return NULL; 2369 2370 ir = xhci_alloc_interrupter(xhci, segs, GFP_KERNEL); 2371 if (!ir) 2372 return NULL; 2373 2374 xhci_ring_init(xhci, ir->event_ring); 2375 2376 spin_lock_irq(&xhci->lock); 2377 if (!intr_num) { 2378 /* Find available secondary interrupter, interrupter 0 is reserved for primary */ 2379 for (i = 1; i < xhci->max_interrupters; i++) { 2380 if (!xhci->interrupters[i]) { 2381 xhci->interrupters[i] = ir; 2382 xhci_add_interrupter(xhci, i); 2383 err = 0; 2384 break; 2385 } 2386 } 2387 } else { 2388 if (!xhci->interrupters[intr_num]) { 2389 xhci->interrupters[intr_num] = ir; 2390 xhci_add_interrupter(xhci, intr_num); 2391 err = 0; 2392 } 2393 } 2394 spin_unlock_irq(&xhci->lock); 2395 2396 if (err) { 2397 xhci_warn(xhci, "Failed to add secondary interrupter, max interrupters %d\n", 2398 xhci->max_interrupters); 2399 xhci_free_interrupter(xhci, ir); 2400 return NULL; 2401 } 2402 2403 xhci_set_interrupter_moderation(ir, imod_interval); 2404 2405 xhci_dbg(xhci, "Add secondary interrupter %d, max interrupters %d\n", 2406 ir->intr_num, xhci->max_interrupters); 2407 2408 return ir; 2409 } 2410 EXPORT_SYMBOL_GPL(xhci_create_secondary_interrupter); 2411 2412 int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) 2413 { 2414 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 2415 struct xhci_device_context_array *dcbaa = &xhci->dcbaa; 2416 2417 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Starting %s", __func__); 2418 2419 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Allocating internal virtual device array"); 2420 xhci->devs = kcalloc_node(xhci->max_slots + 1, sizeof(*xhci->devs), flags, 2421 dev_to_node(dev)); 2422 if (!xhci->devs) 2423 goto fail; 2424 2425 xhci->dcbaa.ctx_array = 2426 dma_alloc_coherent(dev, array_size(sizeof(*dcbaa->ctx_array), xhci->max_slots + 1), 2427 &dcbaa->dma, flags); 2428 if (!dcbaa->ctx_array) 2429 goto fail; 2430 2431 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 2432 "Device context base array address = %pad (DMA), %p (virt)", 2433 &dcbaa->dma, dcbaa->ctx_array); 2434 2435 /* 2436 * Initialize the ring segment pool. The ring must be a contiguous 2437 * structure comprised of TRBs. The TRBs must be 16 byte aligned, 2438 * however, the command ring segment needs 64-byte aligned segments 2439 * and our use of dma addresses in the trb_address_map radix tree needs 2440 * TRB_SEGMENT_SIZE alignment, so we pick the greater alignment need. 2441 */ 2442 if (xhci->quirks & XHCI_TRB_OVERFETCH) 2443 /* Buggy HC prefetches beyond segment bounds - allocate dummy space at the end */ 2444 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev, 2445 TRB_SEGMENT_SIZE * 2, TRB_SEGMENT_SIZE * 2, xhci->page_size * 2); 2446 else 2447 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev, 2448 TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE, xhci->page_size); 2449 if (!xhci->segment_pool) 2450 goto fail; 2451 2452 /* See Table 46 and Note on Figure 55 */ 2453 xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev, 2112, 64, 2454 xhci->page_size); 2455 if (!xhci->device_pool) 2456 goto fail; 2457 2458 /* 2459 * Linear stream context arrays don't have any boundary restrictions, 2460 * and only need to be 16-byte aligned. 2461 */ 2462 xhci->small_streams_pool = dma_pool_create("xHCI 256 byte stream ctx arrays", 2463 dev, SMALL_STREAM_ARRAY_SIZE, 16, 0); 2464 if (!xhci->small_streams_pool) 2465 goto fail; 2466 2467 /* 2468 * Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE will be 2469 * allocated with dma_alloc_coherent(). 2470 */ 2471 2472 xhci->medium_streams_pool = dma_pool_create("xHCI 1KB stream ctx arrays", 2473 dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0); 2474 if (!xhci->medium_streams_pool) 2475 goto fail; 2476 2477 /* 2478 * refer to xhci rev1_2 protocol 5.3.3 max ports is 255. 2479 * refer to xhci rev1_2 protocol 6.4.3.14 port bandwidth buffer need 2480 * to be 16-byte aligned. 2481 */ 2482 xhci->port_bw_pool = dma_pool_create("xHCI 256 port bw ctx arrays", 2483 dev, GET_PORT_BW_ARRAY_SIZE, 16, 0); 2484 if (!xhci->port_bw_pool) 2485 goto fail; 2486 2487 /* Set up the command ring to have one segments for now. */ 2488 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, TYPE_COMMAND, 0, flags); 2489 if (!xhci->cmd_ring) 2490 goto fail; 2491 2492 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Allocated command ring at %p", xhci->cmd_ring); 2493 2494 /* Allocate and set up primary interrupter 0 with an event ring. */ 2495 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Allocating primary event ring"); 2496 xhci->interrupters = kcalloc_node(xhci->max_interrupters, sizeof(*xhci->interrupters), 2497 flags, dev_to_node(dev)); 2498 if (!xhci->interrupters) 2499 goto fail; 2500 2501 xhci->interrupters[0] = xhci_alloc_interrupter(xhci, 0, flags); 2502 if (!xhci->interrupters[0]) 2503 goto fail; 2504 2505 if (scratchpad_alloc(xhci, flags)) 2506 goto fail; 2507 2508 if (xhci_setup_port_arrays(xhci, flags)) 2509 goto fail; 2510 2511 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished %s", __func__); 2512 return 0; 2513 2514 fail: 2515 xhci_halt(xhci); 2516 xhci_reset(xhci, XHCI_RESET_SHORT_USEC); 2517 xhci_mem_cleanup(xhci); 2518 return -ENOMEM; 2519 } 2520