0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/dma-mapping.h>
0014 #include <linux/dmapool.h>
0015 #include <linux/slab.h>
0016 #include <linux/usb.h>
0017
0018 #include "cdnsp-gadget.h"
0019 #include "cdnsp-trace.h"
0020
0021 static void cdnsp_free_stream_info(struct cdnsp_device *pdev,
0022 struct cdnsp_ep *pep);
0023
0024
0025
0026
0027
0028
0029 static struct cdnsp_segment *cdnsp_segment_alloc(struct cdnsp_device *pdev,
0030 unsigned int cycle_state,
0031 unsigned int max_packet,
0032 gfp_t flags)
0033 {
0034 struct cdnsp_segment *seg;
0035 dma_addr_t dma;
0036 int i;
0037
0038 seg = kzalloc(sizeof(*seg), flags);
0039 if (!seg)
0040 return NULL;
0041
0042 seg->trbs = dma_pool_zalloc(pdev->segment_pool, flags, &dma);
0043 if (!seg->trbs) {
0044 kfree(seg);
0045 return NULL;
0046 }
0047
0048 if (max_packet) {
0049 seg->bounce_buf = kzalloc(max_packet, flags | GFP_DMA);
0050 if (!seg->bounce_buf)
0051 goto free_dma;
0052 }
0053
0054
0055 if (cycle_state == 0) {
0056 for (i = 0; i < TRBS_PER_SEGMENT; i++)
0057 seg->trbs[i].link.control |= cpu_to_le32(TRB_CYCLE);
0058 }
0059 seg->dma = dma;
0060 seg->next = NULL;
0061
0062 return seg;
0063
0064 free_dma:
0065 dma_pool_free(pdev->segment_pool, seg->trbs, dma);
0066 kfree(seg);
0067
0068 return NULL;
0069 }
0070
0071 static void cdnsp_segment_free(struct cdnsp_device *pdev,
0072 struct cdnsp_segment *seg)
0073 {
0074 if (seg->trbs)
0075 dma_pool_free(pdev->segment_pool, seg->trbs, seg->dma);
0076
0077 kfree(seg->bounce_buf);
0078 kfree(seg);
0079 }
0080
0081 static void cdnsp_free_segments_for_ring(struct cdnsp_device *pdev,
0082 struct cdnsp_segment *first)
0083 {
0084 struct cdnsp_segment *seg;
0085
0086 seg = first->next;
0087
0088 while (seg != first) {
0089 struct cdnsp_segment *next = seg->next;
0090
0091 cdnsp_segment_free(pdev, seg);
0092 seg = next;
0093 }
0094
0095 cdnsp_segment_free(pdev, first);
0096 }
0097
0098
0099
0100
0101
0102
0103
0104
0105 static void cdnsp_link_segments(struct cdnsp_device *pdev,
0106 struct cdnsp_segment *prev,
0107 struct cdnsp_segment *next,
0108 enum cdnsp_ring_type type)
0109 {
0110 struct cdnsp_link_trb *link;
0111 u32 val;
0112
0113 if (!prev || !next)
0114 return;
0115
0116 prev->next = next;
0117 if (type != TYPE_EVENT) {
0118 link = &prev->trbs[TRBS_PER_SEGMENT - 1].link;
0119 link->segment_ptr = cpu_to_le64(next->dma);
0120
0121
0122
0123
0124
0125 val = le32_to_cpu(link->control);
0126 val &= ~TRB_TYPE_BITMASK;
0127 val |= TRB_TYPE(TRB_LINK);
0128 link->control = cpu_to_le32(val);
0129 }
0130 }
0131
0132
0133
0134
0135
0136 static void cdnsp_link_rings(struct cdnsp_device *pdev,
0137 struct cdnsp_ring *ring,
0138 struct cdnsp_segment *first,
0139 struct cdnsp_segment *last,
0140 unsigned int num_segs)
0141 {
0142 struct cdnsp_segment *next;
0143
0144 if (!ring || !first || !last)
0145 return;
0146
0147 next = ring->enq_seg->next;
0148 cdnsp_link_segments(pdev, ring->enq_seg, first, ring->type);
0149 cdnsp_link_segments(pdev, last, next, ring->type);
0150 ring->num_segs += num_segs;
0151 ring->num_trbs_free += (TRBS_PER_SEGMENT - 1) * num_segs;
0152
0153 if (ring->type != TYPE_EVENT && ring->enq_seg == ring->last_seg) {
0154 ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
0155 ~cpu_to_le32(LINK_TOGGLE);
0156 last->trbs[TRBS_PER_SEGMENT - 1].link.control |=
0157 cpu_to_le32(LINK_TOGGLE);
0158 ring->last_seg = last;
0159 }
0160 }
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193 static int cdnsp_insert_segment_mapping(struct radix_tree_root *trb_address_map,
0194 struct cdnsp_ring *ring,
0195 struct cdnsp_segment *seg,
0196 gfp_t mem_flags)
0197 {
0198 unsigned long key;
0199 int ret;
0200
0201 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
0202
0203
0204 if (radix_tree_lookup(trb_address_map, key))
0205 return 0;
0206
0207 ret = radix_tree_maybe_preload(mem_flags);
0208 if (ret)
0209 return ret;
0210
0211 ret = radix_tree_insert(trb_address_map, key, ring);
0212 radix_tree_preload_end();
0213
0214 return ret;
0215 }
0216
0217 static void cdnsp_remove_segment_mapping(struct radix_tree_root *trb_address_map,
0218 struct cdnsp_segment *seg)
0219 {
0220 unsigned long key;
0221
0222 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
0223 if (radix_tree_lookup(trb_address_map, key))
0224 radix_tree_delete(trb_address_map, key);
0225 }
0226
0227 static int cdnsp_update_stream_segment_mapping(struct radix_tree_root *trb_address_map,
0228 struct cdnsp_ring *ring,
0229 struct cdnsp_segment *first_seg,
0230 struct cdnsp_segment *last_seg,
0231 gfp_t mem_flags)
0232 {
0233 struct cdnsp_segment *failed_seg;
0234 struct cdnsp_segment *seg;
0235 int ret;
0236
0237 seg = first_seg;
0238 do {
0239 ret = cdnsp_insert_segment_mapping(trb_address_map, ring, seg,
0240 mem_flags);
0241 if (ret)
0242 goto remove_streams;
0243 if (seg == last_seg)
0244 return 0;
0245 seg = seg->next;
0246 } while (seg != first_seg);
0247
0248 return 0;
0249
0250 remove_streams:
0251 failed_seg = seg;
0252 seg = first_seg;
0253 do {
0254 cdnsp_remove_segment_mapping(trb_address_map, seg);
0255 if (seg == failed_seg)
0256 return ret;
0257 seg = seg->next;
0258 } while (seg != first_seg);
0259
0260 return ret;
0261 }
0262
0263 static void cdnsp_remove_stream_mapping(struct cdnsp_ring *ring)
0264 {
0265 struct cdnsp_segment *seg;
0266
0267 seg = ring->first_seg;
0268 do {
0269 cdnsp_remove_segment_mapping(ring->trb_address_map, seg);
0270 seg = seg->next;
0271 } while (seg != ring->first_seg);
0272 }
0273
0274 static int cdnsp_update_stream_mapping(struct cdnsp_ring *ring)
0275 {
0276 return cdnsp_update_stream_segment_mapping(ring->trb_address_map, ring,
0277 ring->first_seg, ring->last_seg, GFP_ATOMIC);
0278 }
0279
0280 static void cdnsp_ring_free(struct cdnsp_device *pdev, struct cdnsp_ring *ring)
0281 {
0282 if (!ring)
0283 return;
0284
0285 trace_cdnsp_ring_free(ring);
0286
0287 if (ring->first_seg) {
0288 if (ring->type == TYPE_STREAM)
0289 cdnsp_remove_stream_mapping(ring);
0290
0291 cdnsp_free_segments_for_ring(pdev, ring->first_seg);
0292 }
0293
0294 kfree(ring);
0295 }
0296
0297 void cdnsp_initialize_ring_info(struct cdnsp_ring *ring)
0298 {
0299 ring->enqueue = ring->first_seg->trbs;
0300 ring->enq_seg = ring->first_seg;
0301 ring->dequeue = ring->enqueue;
0302 ring->deq_seg = ring->first_seg;
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312 ring->cycle_state = 1;
0313
0314
0315
0316
0317
0318 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
0319 }
0320
0321
0322 static int cdnsp_alloc_segments_for_ring(struct cdnsp_device *pdev,
0323 struct cdnsp_segment **first,
0324 struct cdnsp_segment **last,
0325 unsigned int num_segs,
0326 unsigned int cycle_state,
0327 enum cdnsp_ring_type type,
0328 unsigned int max_packet,
0329 gfp_t flags)
0330 {
0331 struct cdnsp_segment *prev;
0332
0333
0334 prev = cdnsp_segment_alloc(pdev, cycle_state, max_packet, flags);
0335 if (!prev)
0336 return -ENOMEM;
0337
0338 num_segs--;
0339 *first = prev;
0340
0341
0342 while (num_segs > 0) {
0343 struct cdnsp_segment *next;
0344
0345 next = cdnsp_segment_alloc(pdev, cycle_state,
0346 max_packet, flags);
0347 if (!next) {
0348 cdnsp_free_segments_for_ring(pdev, *first);
0349 return -ENOMEM;
0350 }
0351
0352 cdnsp_link_segments(pdev, prev, next, type);
0353
0354 prev = next;
0355 num_segs--;
0356 }
0357
0358 cdnsp_link_segments(pdev, prev, *first, type);
0359 *last = prev;
0360
0361 return 0;
0362 }
0363
0364
0365
0366
0367
0368
0369
0370 static struct cdnsp_ring *cdnsp_ring_alloc(struct cdnsp_device *pdev,
0371 unsigned int num_segs,
0372 enum cdnsp_ring_type type,
0373 unsigned int max_packet,
0374 gfp_t flags)
0375 {
0376 struct cdnsp_ring *ring;
0377 int ret;
0378
0379 ring = kzalloc(sizeof *(ring), flags);
0380 if (!ring)
0381 return NULL;
0382
0383 ring->num_segs = num_segs;
0384 ring->bounce_buf_len = max_packet;
0385 INIT_LIST_HEAD(&ring->td_list);
0386 ring->type = type;
0387
0388 if (num_segs == 0)
0389 return ring;
0390
0391 ret = cdnsp_alloc_segments_for_ring(pdev, &ring->first_seg,
0392 &ring->last_seg, num_segs,
0393 1, type, max_packet, flags);
0394 if (ret)
0395 goto fail;
0396
0397
0398 if (type != TYPE_EVENT)
0399 ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |=
0400 cpu_to_le32(LINK_TOGGLE);
0401
0402 cdnsp_initialize_ring_info(ring);
0403 trace_cdnsp_ring_alloc(ring);
0404 return ring;
0405 fail:
0406 kfree(ring);
0407 return NULL;
0408 }
0409
0410 void cdnsp_free_endpoint_rings(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
0411 {
0412 cdnsp_ring_free(pdev, pep->ring);
0413 pep->ring = NULL;
0414 cdnsp_free_stream_info(pdev, pep);
0415 }
0416
0417
0418
0419
0420
0421 int cdnsp_ring_expansion(struct cdnsp_device *pdev,
0422 struct cdnsp_ring *ring,
0423 unsigned int num_trbs,
0424 gfp_t flags)
0425 {
0426 unsigned int num_segs_needed;
0427 struct cdnsp_segment *first;
0428 struct cdnsp_segment *last;
0429 unsigned int num_segs;
0430 int ret;
0431
0432 num_segs_needed = (num_trbs + (TRBS_PER_SEGMENT - 1) - 1) /
0433 (TRBS_PER_SEGMENT - 1);
0434
0435
0436 num_segs = max(ring->num_segs, num_segs_needed);
0437
0438 ret = cdnsp_alloc_segments_for_ring(pdev, &first, &last, num_segs,
0439 ring->cycle_state, ring->type,
0440 ring->bounce_buf_len, flags);
0441 if (ret)
0442 return -ENOMEM;
0443
0444 if (ring->type == TYPE_STREAM)
0445 ret = cdnsp_update_stream_segment_mapping(ring->trb_address_map,
0446 ring, first,
0447 last, flags);
0448
0449 if (ret) {
0450 cdnsp_free_segments_for_ring(pdev, first);
0451
0452 return ret;
0453 }
0454
0455 cdnsp_link_rings(pdev, ring, first, last, num_segs);
0456 trace_cdnsp_ring_expansion(ring);
0457
0458 return 0;
0459 }
0460
0461 static int cdnsp_init_device_ctx(struct cdnsp_device *pdev)
0462 {
0463 int size = HCC_64BYTE_CONTEXT(pdev->hcc_params) ? 2048 : 1024;
0464
0465 pdev->out_ctx.type = CDNSP_CTX_TYPE_DEVICE;
0466 pdev->out_ctx.size = size;
0467 pdev->out_ctx.ctx_size = CTX_SIZE(pdev->hcc_params);
0468 pdev->out_ctx.bytes = dma_pool_zalloc(pdev->device_pool, GFP_ATOMIC,
0469 &pdev->out_ctx.dma);
0470
0471 if (!pdev->out_ctx.bytes)
0472 return -ENOMEM;
0473
0474 pdev->in_ctx.type = CDNSP_CTX_TYPE_INPUT;
0475 pdev->in_ctx.ctx_size = pdev->out_ctx.ctx_size;
0476 pdev->in_ctx.size = size + pdev->out_ctx.ctx_size;
0477 pdev->in_ctx.bytes = dma_pool_zalloc(pdev->device_pool, GFP_ATOMIC,
0478 &pdev->in_ctx.dma);
0479
0480 if (!pdev->in_ctx.bytes) {
0481 dma_pool_free(pdev->device_pool, pdev->out_ctx.bytes,
0482 pdev->out_ctx.dma);
0483 return -ENOMEM;
0484 }
0485
0486 return 0;
0487 }
0488
0489 struct cdnsp_input_control_ctx
0490 *cdnsp_get_input_control_ctx(struct cdnsp_container_ctx *ctx)
0491 {
0492 if (ctx->type != CDNSP_CTX_TYPE_INPUT)
0493 return NULL;
0494
0495 return (struct cdnsp_input_control_ctx *)ctx->bytes;
0496 }
0497
0498 struct cdnsp_slot_ctx *cdnsp_get_slot_ctx(struct cdnsp_container_ctx *ctx)
0499 {
0500 if (ctx->type == CDNSP_CTX_TYPE_DEVICE)
0501 return (struct cdnsp_slot_ctx *)ctx->bytes;
0502
0503 return (struct cdnsp_slot_ctx *)(ctx->bytes + ctx->ctx_size);
0504 }
0505
0506 struct cdnsp_ep_ctx *cdnsp_get_ep_ctx(struct cdnsp_container_ctx *ctx,
0507 unsigned int ep_index)
0508 {
0509
0510 ep_index++;
0511 if (ctx->type == CDNSP_CTX_TYPE_INPUT)
0512 ep_index++;
0513
0514 return (struct cdnsp_ep_ctx *)(ctx->bytes + (ep_index * ctx->ctx_size));
0515 }
0516
0517 static void cdnsp_free_stream_ctx(struct cdnsp_device *pdev,
0518 struct cdnsp_ep *pep)
0519 {
0520 dma_pool_free(pdev->device_pool, pep->stream_info.stream_ctx_array,
0521 pep->stream_info.ctx_array_dma);
0522 }
0523
0524
0525 static struct cdnsp_stream_ctx
0526 *cdnsp_alloc_stream_ctx(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
0527 {
0528 size_t size = sizeof(struct cdnsp_stream_ctx) *
0529 pep->stream_info.num_stream_ctxs;
0530
0531 if (size > CDNSP_CTX_SIZE)
0532 return NULL;
0533
0534
0535
0536
0537
0538
0539 return dma_pool_zalloc(pdev->device_pool, GFP_DMA32 | GFP_ATOMIC,
0540 &pep->stream_info.ctx_array_dma);
0541 }
0542
0543 struct cdnsp_ring *cdnsp_dma_to_transfer_ring(struct cdnsp_ep *pep, u64 address)
0544 {
0545 if (pep->ep_state & EP_HAS_STREAMS)
0546 return radix_tree_lookup(&pep->stream_info.trb_address_map,
0547 address >> TRB_SEGMENT_SHIFT);
0548
0549 return pep->ring;
0550 }
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561 int cdnsp_alloc_stream_info(struct cdnsp_device *pdev,
0562 struct cdnsp_ep *pep,
0563 unsigned int num_stream_ctxs,
0564 unsigned int num_streams)
0565 {
0566 struct cdnsp_stream_info *stream_info;
0567 struct cdnsp_ring *cur_ring;
0568 u32 cur_stream;
0569 u64 addr;
0570 int ret;
0571 int mps;
0572
0573 stream_info = &pep->stream_info;
0574 stream_info->num_streams = num_streams;
0575 stream_info->num_stream_ctxs = num_stream_ctxs;
0576
0577
0578 stream_info->stream_rings = kcalloc(num_streams,
0579 sizeof(struct cdnsp_ring *),
0580 GFP_ATOMIC);
0581 if (!stream_info->stream_rings)
0582 return -ENOMEM;
0583
0584
0585 stream_info->stream_ctx_array = cdnsp_alloc_stream_ctx(pdev, pep);
0586 if (!stream_info->stream_ctx_array)
0587 goto cleanup_stream_rings;
0588
0589 memset(stream_info->stream_ctx_array, 0,
0590 sizeof(struct cdnsp_stream_ctx) * num_stream_ctxs);
0591 INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC);
0592 mps = usb_endpoint_maxp(pep->endpoint.desc);
0593
0594
0595
0596
0597
0598
0599 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
0600 cur_ring = cdnsp_ring_alloc(pdev, 2, TYPE_STREAM, mps,
0601 GFP_ATOMIC);
0602 stream_info->stream_rings[cur_stream] = cur_ring;
0603
0604 if (!cur_ring)
0605 goto cleanup_rings;
0606
0607 cur_ring->stream_id = cur_stream;
0608 cur_ring->trb_address_map = &stream_info->trb_address_map;
0609
0610
0611 addr = cur_ring->first_seg->dma | SCT_FOR_CTX(SCT_PRI_TR) |
0612 cur_ring->cycle_state;
0613
0614 stream_info->stream_ctx_array[cur_stream].stream_ring =
0615 cpu_to_le64(addr);
0616
0617 trace_cdnsp_set_stream_ring(cur_ring);
0618
0619 ret = cdnsp_update_stream_mapping(cur_ring);
0620 if (ret)
0621 goto cleanup_rings;
0622 }
0623
0624 return 0;
0625
0626 cleanup_rings:
0627 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
0628 cur_ring = stream_info->stream_rings[cur_stream];
0629 if (cur_ring) {
0630 cdnsp_ring_free(pdev, cur_ring);
0631 stream_info->stream_rings[cur_stream] = NULL;
0632 }
0633 }
0634
0635 cleanup_stream_rings:
0636 kfree(pep->stream_info.stream_rings);
0637
0638 return -ENOMEM;
0639 }
0640
0641
0642 static void cdnsp_free_stream_info(struct cdnsp_device *pdev,
0643 struct cdnsp_ep *pep)
0644 {
0645 struct cdnsp_stream_info *stream_info = &pep->stream_info;
0646 struct cdnsp_ring *cur_ring;
0647 int cur_stream;
0648
0649 if (!(pep->ep_state & EP_HAS_STREAMS))
0650 return;
0651
0652 for (cur_stream = 1; cur_stream < stream_info->num_streams;
0653 cur_stream++) {
0654 cur_ring = stream_info->stream_rings[cur_stream];
0655 if (cur_ring) {
0656 cdnsp_ring_free(pdev, cur_ring);
0657 stream_info->stream_rings[cur_stream] = NULL;
0658 }
0659 }
0660
0661 if (stream_info->stream_ctx_array)
0662 cdnsp_free_stream_ctx(pdev, pep);
0663
0664 kfree(stream_info->stream_rings);
0665 pep->ep_state &= ~EP_HAS_STREAMS;
0666 }
0667
0668
0669 static void cdnsp_free_priv_device(struct cdnsp_device *pdev)
0670 {
0671 pdev->dcbaa->dev_context_ptrs[1] = 0;
0672
0673 cdnsp_free_endpoint_rings(pdev, &pdev->eps[0]);
0674
0675 if (pdev->in_ctx.bytes)
0676 dma_pool_free(pdev->device_pool, pdev->in_ctx.bytes,
0677 pdev->in_ctx.dma);
0678
0679 if (pdev->out_ctx.bytes)
0680 dma_pool_free(pdev->device_pool, pdev->out_ctx.bytes,
0681 pdev->out_ctx.dma);
0682
0683 pdev->in_ctx.bytes = NULL;
0684 pdev->out_ctx.bytes = NULL;
0685 }
0686
0687 static int cdnsp_alloc_priv_device(struct cdnsp_device *pdev)
0688 {
0689 int ret;
0690
0691 ret = cdnsp_init_device_ctx(pdev);
0692 if (ret)
0693 return ret;
0694
0695
0696 pdev->eps[0].ring = cdnsp_ring_alloc(pdev, 2, TYPE_CTRL, 0, GFP_ATOMIC);
0697 if (!pdev->eps[0].ring)
0698 goto fail;
0699
0700
0701 pdev->dcbaa->dev_context_ptrs[1] = cpu_to_le64(pdev->out_ctx.dma);
0702 pdev->cmd.in_ctx = &pdev->in_ctx;
0703
0704 trace_cdnsp_alloc_priv_device(pdev);
0705 return 0;
0706 fail:
0707 dma_pool_free(pdev->device_pool, pdev->out_ctx.bytes,
0708 pdev->out_ctx.dma);
0709 dma_pool_free(pdev->device_pool, pdev->in_ctx.bytes,
0710 pdev->in_ctx.dma);
0711
0712 return ret;
0713 }
0714
0715 void cdnsp_copy_ep0_dequeue_into_input_ctx(struct cdnsp_device *pdev)
0716 {
0717 struct cdnsp_ep_ctx *ep0_ctx = pdev->eps[0].in_ctx;
0718 struct cdnsp_ring *ep_ring = pdev->eps[0].ring;
0719 dma_addr_t dma;
0720
0721 dma = cdnsp_trb_virt_to_dma(ep_ring->enq_seg, ep_ring->enqueue);
0722 ep0_ctx->deq = cpu_to_le64(dma | ep_ring->cycle_state);
0723 }
0724
0725
0726 int cdnsp_setup_addressable_priv_dev(struct cdnsp_device *pdev)
0727 {
0728 struct cdnsp_slot_ctx *slot_ctx;
0729 struct cdnsp_ep_ctx *ep0_ctx;
0730 u32 max_packets, port;
0731
0732 ep0_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, 0);
0733 slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
0734
0735
0736 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
0737
0738 switch (pdev->gadget.speed) {
0739 case USB_SPEED_SUPER_PLUS:
0740 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SSP);
0741 max_packets = MAX_PACKET(512);
0742 break;
0743 case USB_SPEED_SUPER:
0744 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
0745 max_packets = MAX_PACKET(512);
0746 break;
0747 case USB_SPEED_HIGH:
0748 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
0749 max_packets = MAX_PACKET(64);
0750 break;
0751 case USB_SPEED_FULL:
0752 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
0753 max_packets = MAX_PACKET(64);
0754 break;
0755 default:
0756
0757 return -EINVAL;
0758 }
0759
0760 port = DEV_PORT(pdev->active_port->port_num);
0761 slot_ctx->dev_port |= cpu_to_le32(port);
0762 slot_ctx->dev_state = cpu_to_le32((pdev->device_address &
0763 DEV_ADDR_MASK));
0764 ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(0x8));
0765 ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP));
0766 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3) |
0767 max_packets);
0768
0769 ep0_ctx->deq = cpu_to_le64(pdev->eps[0].ring->first_seg->dma |
0770 pdev->eps[0].ring->cycle_state);
0771
0772 trace_cdnsp_setup_addressable_priv_device(pdev);
0773
0774 return 0;
0775 }
0776
0777
0778
0779
0780
0781 static unsigned int cdnsp_parse_exponent_interval(struct usb_gadget *g,
0782 struct cdnsp_ep *pep)
0783 {
0784 unsigned int interval;
0785
0786 interval = clamp_val(pep->endpoint.desc->bInterval, 1, 16) - 1;
0787 if (interval != pep->endpoint.desc->bInterval - 1)
0788 dev_warn(&g->dev, "ep %s - rounding interval to %d %sframes\n",
0789 pep->name, 1 << interval,
0790 g->speed == USB_SPEED_FULL ? "" : "micro");
0791
0792
0793
0794
0795
0796
0797 if (g->speed == USB_SPEED_FULL)
0798 interval += 3;
0799
0800
0801 if (interval > 12)
0802 interval = 12;
0803
0804 return interval;
0805 }
0806
0807
0808
0809
0810
0811 static unsigned int cdnsp_microframes_to_exponent(struct usb_gadget *g,
0812 struct cdnsp_ep *pep,
0813 unsigned int desc_interval,
0814 unsigned int min_exponent,
0815 unsigned int max_exponent)
0816 {
0817 unsigned int interval;
0818
0819 interval = fls(desc_interval) - 1;
0820 return clamp_val(interval, min_exponent, max_exponent);
0821 }
0822
0823
0824
0825
0826
0827
0828
0829 static unsigned int cdnsp_get_endpoint_interval(struct usb_gadget *g,
0830 struct cdnsp_ep *pep)
0831 {
0832 unsigned int interval = 0;
0833
0834 switch (g->speed) {
0835 case USB_SPEED_HIGH:
0836 case USB_SPEED_SUPER_PLUS:
0837 case USB_SPEED_SUPER:
0838 if (usb_endpoint_xfer_int(pep->endpoint.desc) ||
0839 usb_endpoint_xfer_isoc(pep->endpoint.desc))
0840 interval = cdnsp_parse_exponent_interval(g, pep);
0841 break;
0842 case USB_SPEED_FULL:
0843 if (usb_endpoint_xfer_isoc(pep->endpoint.desc)) {
0844 interval = cdnsp_parse_exponent_interval(g, pep);
0845 } else if (usb_endpoint_xfer_int(pep->endpoint.desc)) {
0846 interval = pep->endpoint.desc->bInterval << 3;
0847 interval = cdnsp_microframes_to_exponent(g, pep,
0848 interval,
0849 3, 10);
0850 }
0851
0852 break;
0853 default:
0854 WARN_ON(1);
0855 }
0856
0857 return interval;
0858 }
0859
0860
0861
0862
0863
0864
0865
0866 static u32 cdnsp_get_endpoint_mult(struct usb_gadget *g, struct cdnsp_ep *pep)
0867 {
0868 if (g->speed < USB_SPEED_SUPER ||
0869 !usb_endpoint_xfer_isoc(pep->endpoint.desc))
0870 return 0;
0871
0872 return pep->endpoint.comp_desc->bmAttributes;
0873 }
0874
0875 static u32 cdnsp_get_endpoint_max_burst(struct usb_gadget *g,
0876 struct cdnsp_ep *pep)
0877 {
0878
0879 if (g->speed >= USB_SPEED_SUPER)
0880 return pep->endpoint.comp_desc->bMaxBurst;
0881
0882 if (g->speed == USB_SPEED_HIGH &&
0883 (usb_endpoint_xfer_isoc(pep->endpoint.desc) ||
0884 usb_endpoint_xfer_int(pep->endpoint.desc)))
0885 return usb_endpoint_maxp_mult(pep->endpoint.desc) - 1;
0886
0887 return 0;
0888 }
0889
0890 static u32 cdnsp_get_endpoint_type(const struct usb_endpoint_descriptor *desc)
0891 {
0892 int in;
0893
0894 in = usb_endpoint_dir_in(desc);
0895
0896 switch (usb_endpoint_type(desc)) {
0897 case USB_ENDPOINT_XFER_CONTROL:
0898 return CTRL_EP;
0899 case USB_ENDPOINT_XFER_BULK:
0900 return in ? BULK_IN_EP : BULK_OUT_EP;
0901 case USB_ENDPOINT_XFER_ISOC:
0902 return in ? ISOC_IN_EP : ISOC_OUT_EP;
0903 case USB_ENDPOINT_XFER_INT:
0904 return in ? INT_IN_EP : INT_OUT_EP;
0905 }
0906
0907 return 0;
0908 }
0909
0910
0911
0912
0913
0914
0915 static u32 cdnsp_get_max_esit_payload(struct usb_gadget *g,
0916 struct cdnsp_ep *pep)
0917 {
0918 int max_packet;
0919 int max_burst;
0920
0921
0922 if (usb_endpoint_xfer_control(pep->endpoint.desc) ||
0923 usb_endpoint_xfer_bulk(pep->endpoint.desc))
0924 return 0;
0925
0926
0927 if (g->speed >= USB_SPEED_SUPER_PLUS &&
0928 USB_SS_SSP_ISOC_COMP(pep->endpoint.desc->bmAttributes))
0929 return le16_to_cpu(pep->endpoint.comp_desc->wBytesPerInterval);
0930
0931 else if (g->speed >= USB_SPEED_SUPER)
0932 return le16_to_cpu(pep->endpoint.comp_desc->wBytesPerInterval);
0933
0934 max_packet = usb_endpoint_maxp(pep->endpoint.desc);
0935 max_burst = usb_endpoint_maxp_mult(pep->endpoint.desc);
0936
0937
0938 return max_packet * max_burst;
0939 }
0940
0941 int cdnsp_endpoint_init(struct cdnsp_device *pdev,
0942 struct cdnsp_ep *pep,
0943 gfp_t mem_flags)
0944 {
0945 enum cdnsp_ring_type ring_type;
0946 struct cdnsp_ep_ctx *ep_ctx;
0947 unsigned int err_count = 0;
0948 unsigned int avg_trb_len;
0949 unsigned int max_packet;
0950 unsigned int max_burst;
0951 unsigned int interval;
0952 u32 max_esit_payload;
0953 unsigned int mult;
0954 u32 endpoint_type;
0955 int ret;
0956
0957 ep_ctx = pep->in_ctx;
0958
0959 endpoint_type = cdnsp_get_endpoint_type(pep->endpoint.desc);
0960 if (!endpoint_type)
0961 return -EINVAL;
0962
0963 ring_type = usb_endpoint_type(pep->endpoint.desc);
0964
0965
0966
0967
0968
0969
0970
0971 max_esit_payload = cdnsp_get_max_esit_payload(&pdev->gadget, pep);
0972 interval = cdnsp_get_endpoint_interval(&pdev->gadget, pep);
0973 mult = cdnsp_get_endpoint_mult(&pdev->gadget, pep);
0974 max_packet = usb_endpoint_maxp(pep->endpoint.desc);
0975 max_burst = cdnsp_get_endpoint_max_burst(&pdev->gadget, pep);
0976 avg_trb_len = max_esit_payload;
0977
0978
0979 if (!usb_endpoint_xfer_isoc(pep->endpoint.desc))
0980 err_count = 3;
0981 if (usb_endpoint_xfer_bulk(pep->endpoint.desc) &&
0982 pdev->gadget.speed == USB_SPEED_HIGH)
0983 max_packet = 512;
0984
0985 if (usb_endpoint_xfer_control(pep->endpoint.desc))
0986 avg_trb_len = 8;
0987
0988
0989 pep->ring = cdnsp_ring_alloc(pdev, 2, ring_type, max_packet, mem_flags);
0990 if (!pep->ring)
0991 return -ENOMEM;
0992
0993 pep->skip = false;
0994
0995
0996 ep_ctx->ep_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
0997 EP_INTERVAL(interval) | EP_MULT(mult));
0998 ep_ctx->ep_info2 = cpu_to_le32(EP_TYPE(endpoint_type) |
0999 MAX_PACKET(max_packet) | MAX_BURST(max_burst) |
1000 ERROR_COUNT(err_count));
1001 ep_ctx->deq = cpu_to_le64(pep->ring->first_seg->dma |
1002 pep->ring->cycle_state);
1003
1004 ep_ctx->tx_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
1005 EP_AVG_TRB_LENGTH(avg_trb_len));
1006
1007 if (usb_endpoint_xfer_bulk(pep->endpoint.desc) &&
1008 pdev->gadget.speed > USB_SPEED_HIGH) {
1009 ret = cdnsp_alloc_streams(pdev, pep);
1010 if (ret < 0)
1011 return ret;
1012 }
1013
1014 return 0;
1015 }
1016
1017 void cdnsp_endpoint_zero(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
1018 {
1019 pep->in_ctx->ep_info = 0;
1020 pep->in_ctx->ep_info2 = 0;
1021 pep->in_ctx->deq = 0;
1022 pep->in_ctx->tx_info = 0;
1023 }
1024
1025 static int cdnsp_alloc_erst(struct cdnsp_device *pdev,
1026 struct cdnsp_ring *evt_ring,
1027 struct cdnsp_erst *erst)
1028 {
1029 struct cdnsp_erst_entry *entry;
1030 struct cdnsp_segment *seg;
1031 unsigned int val;
1032 size_t size;
1033
1034 size = sizeof(struct cdnsp_erst_entry) * evt_ring->num_segs;
1035 erst->entries = dma_alloc_coherent(pdev->dev, size,
1036 &erst->erst_dma_addr, GFP_KERNEL);
1037 if (!erst->entries)
1038 return -ENOMEM;
1039
1040 erst->num_entries = evt_ring->num_segs;
1041
1042 seg = evt_ring->first_seg;
1043 for (val = 0; val < evt_ring->num_segs; val++) {
1044 entry = &erst->entries[val];
1045 entry->seg_addr = cpu_to_le64(seg->dma);
1046 entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
1047 entry->rsvd = 0;
1048 seg = seg->next;
1049 }
1050
1051 return 0;
1052 }
1053
1054 static void cdnsp_free_erst(struct cdnsp_device *pdev, struct cdnsp_erst *erst)
1055 {
1056 size_t size = sizeof(struct cdnsp_erst_entry) * (erst->num_entries);
1057 struct device *dev = pdev->dev;
1058
1059 if (erst->entries)
1060 dma_free_coherent(dev, size, erst->entries,
1061 erst->erst_dma_addr);
1062
1063 erst->entries = NULL;
1064 }
1065
1066 void cdnsp_mem_cleanup(struct cdnsp_device *pdev)
1067 {
1068 struct device *dev = pdev->dev;
1069
1070 cdnsp_free_priv_device(pdev);
1071 cdnsp_free_erst(pdev, &pdev->erst);
1072
1073 if (pdev->event_ring)
1074 cdnsp_ring_free(pdev, pdev->event_ring);
1075
1076 pdev->event_ring = NULL;
1077
1078 if (pdev->cmd_ring)
1079 cdnsp_ring_free(pdev, pdev->cmd_ring);
1080
1081 pdev->cmd_ring = NULL;
1082
1083 dma_pool_destroy(pdev->segment_pool);
1084 pdev->segment_pool = NULL;
1085 dma_pool_destroy(pdev->device_pool);
1086 pdev->device_pool = NULL;
1087
1088 dma_free_coherent(dev, sizeof(*pdev->dcbaa),
1089 pdev->dcbaa, pdev->dcbaa->dma);
1090
1091 pdev->dcbaa = NULL;
1092
1093 pdev->usb2_port.exist = 0;
1094 pdev->usb3_port.exist = 0;
1095 pdev->usb2_port.port_num = 0;
1096 pdev->usb3_port.port_num = 0;
1097 pdev->active_port = NULL;
1098 }
1099
1100 static void cdnsp_set_event_deq(struct cdnsp_device *pdev)
1101 {
1102 dma_addr_t deq;
1103 u64 temp;
1104
1105 deq = cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg,
1106 pdev->event_ring->dequeue);
1107
1108
1109 temp = cdnsp_read_64(&pdev->ir_set->erst_dequeue);
1110 temp &= ERST_PTR_MASK;
1111
1112
1113
1114
1115
1116 temp &= ~ERST_EHB;
1117
1118 cdnsp_write_64(((u64)deq & (u64)~ERST_PTR_MASK) | temp,
1119 &pdev->ir_set->erst_dequeue);
1120 }
1121
1122 static void cdnsp_add_in_port(struct cdnsp_device *pdev,
1123 struct cdnsp_port *port,
1124 __le32 __iomem *addr)
1125 {
1126 u32 temp, port_offset, port_count;
1127
1128 temp = readl(addr);
1129 port->maj_rev = CDNSP_EXT_PORT_MAJOR(temp);
1130 port->min_rev = CDNSP_EXT_PORT_MINOR(temp);
1131
1132
1133 temp = readl(addr + 2);
1134 port_offset = CDNSP_EXT_PORT_OFF(temp);
1135 port_count = CDNSP_EXT_PORT_COUNT(temp);
1136
1137 trace_cdnsp_port_info(addr, port_offset, port_count, port->maj_rev);
1138
1139 port->port_num = port_offset;
1140 port->exist = 1;
1141 }
1142
1143
1144
1145
1146
1147 static int cdnsp_setup_port_arrays(struct cdnsp_device *pdev)
1148 {
1149 void __iomem *base;
1150 u32 offset;
1151 int i;
1152
1153 base = &pdev->cap_regs->hc_capbase;
1154 offset = cdnsp_find_next_ext_cap(base, 0,
1155 EXT_CAP_CFG_DEV_20PORT_CAP_ID);
1156 pdev->port20_regs = base + offset;
1157
1158 offset = cdnsp_find_next_ext_cap(base, 0, D_XEC_CFG_3XPORT_CAP);
1159 pdev->port3x_regs = base + offset;
1160
1161 offset = 0;
1162 base = &pdev->cap_regs->hc_capbase;
1163
1164
1165 for (i = 0; i < 2; i++) {
1166 u32 temp;
1167
1168 offset = cdnsp_find_next_ext_cap(base, offset,
1169 EXT_CAPS_PROTOCOL);
1170 temp = readl(base + offset);
1171
1172 if (CDNSP_EXT_PORT_MAJOR(temp) == 0x03 &&
1173 !pdev->usb3_port.port_num)
1174 cdnsp_add_in_port(pdev, &pdev->usb3_port,
1175 base + offset);
1176
1177 if (CDNSP_EXT_PORT_MAJOR(temp) == 0x02 &&
1178 !pdev->usb2_port.port_num)
1179 cdnsp_add_in_port(pdev, &pdev->usb2_port,
1180 base + offset);
1181 }
1182
1183 if (!pdev->usb2_port.exist || !pdev->usb3_port.exist) {
1184 dev_err(pdev->dev, "Error: Only one port detected\n");
1185 return -ENODEV;
1186 }
1187
1188 trace_cdnsp_init("Found USB 2.0 ports and USB 3.0 ports.");
1189
1190 pdev->usb2_port.regs = (struct cdnsp_port_regs __iomem *)
1191 (&pdev->op_regs->port_reg_base + NUM_PORT_REGS *
1192 (pdev->usb2_port.port_num - 1));
1193
1194 pdev->usb3_port.regs = (struct cdnsp_port_regs __iomem *)
1195 (&pdev->op_regs->port_reg_base + NUM_PORT_REGS *
1196 (pdev->usb3_port.port_num - 1));
1197
1198 return 0;
1199 }
1200
1201
1202
1203
1204
1205
1206
1207
1208 int cdnsp_mem_init(struct cdnsp_device *pdev)
1209 {
1210 struct device *dev = pdev->dev;
1211 int ret = -ENOMEM;
1212 unsigned int val;
1213 dma_addr_t dma;
1214 u32 page_size;
1215 u64 val_64;
1216
1217
1218
1219
1220
1221 page_size = 1 << 12;
1222
1223 val = readl(&pdev->op_regs->config_reg);
1224 val |= ((val & ~MAX_DEVS) | CDNSP_DEV_MAX_SLOTS) | CONFIG_U3E;
1225 writel(val, &pdev->op_regs->config_reg);
1226
1227
1228
1229
1230
1231 pdev->dcbaa = dma_alloc_coherent(dev, sizeof(*pdev->dcbaa),
1232 &dma, GFP_KERNEL);
1233 if (!pdev->dcbaa)
1234 return -ENOMEM;
1235
1236 pdev->dcbaa->dma = dma;
1237
1238 cdnsp_write_64(dma, &pdev->op_regs->dcbaa_ptr);
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248 pdev->segment_pool = dma_pool_create("CDNSP ring segments", dev,
1249 TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE,
1250 page_size);
1251 if (!pdev->segment_pool)
1252 goto release_dcbaa;
1253
1254 pdev->device_pool = dma_pool_create("CDNSP input/output contexts", dev,
1255 CDNSP_CTX_SIZE, 64, page_size);
1256 if (!pdev->device_pool)
1257 goto destroy_segment_pool;
1258
1259
1260
1261 pdev->cmd_ring = cdnsp_ring_alloc(pdev, 1, TYPE_COMMAND, 0, GFP_KERNEL);
1262 if (!pdev->cmd_ring)
1263 goto destroy_device_pool;
1264
1265
1266 val_64 = cdnsp_read_64(&pdev->op_regs->cmd_ring);
1267 val_64 = (val_64 & (u64)CMD_RING_RSVD_BITS) |
1268 (pdev->cmd_ring->first_seg->dma & (u64)~CMD_RING_RSVD_BITS) |
1269 pdev->cmd_ring->cycle_state;
1270 cdnsp_write_64(val_64, &pdev->op_regs->cmd_ring);
1271
1272 val = readl(&pdev->cap_regs->db_off);
1273 val &= DBOFF_MASK;
1274 pdev->dba = (void __iomem *)pdev->cap_regs + val;
1275
1276
1277 pdev->ir_set = &pdev->run_regs->ir_set[0];
1278
1279
1280
1281
1282
1283 pdev->event_ring = cdnsp_ring_alloc(pdev, ERST_NUM_SEGS, TYPE_EVENT,
1284 0, GFP_KERNEL);
1285 if (!pdev->event_ring)
1286 goto free_cmd_ring;
1287
1288 ret = cdnsp_alloc_erst(pdev, pdev->event_ring, &pdev->erst);
1289 if (ret)
1290 goto free_event_ring;
1291
1292
1293 val = readl(&pdev->ir_set->erst_size);
1294 val &= ERST_SIZE_MASK;
1295 val |= ERST_NUM_SEGS;
1296 writel(val, &pdev->ir_set->erst_size);
1297
1298
1299 val_64 = cdnsp_read_64(&pdev->ir_set->erst_base);
1300 val_64 &= ERST_PTR_MASK;
1301 val_64 |= (pdev->erst.erst_dma_addr & (u64)~ERST_PTR_MASK);
1302 cdnsp_write_64(val_64, &pdev->ir_set->erst_base);
1303
1304
1305 cdnsp_set_event_deq(pdev);
1306
1307 ret = cdnsp_setup_port_arrays(pdev);
1308 if (ret)
1309 goto free_erst;
1310
1311 ret = cdnsp_alloc_priv_device(pdev);
1312 if (ret) {
1313 dev_err(pdev->dev,
1314 "Could not allocate cdnsp_device data structures\n");
1315 goto free_erst;
1316 }
1317
1318 return 0;
1319
1320 free_erst:
1321 cdnsp_free_erst(pdev, &pdev->erst);
1322 free_event_ring:
1323 cdnsp_ring_free(pdev, pdev->event_ring);
1324 free_cmd_ring:
1325 cdnsp_ring_free(pdev, pdev->cmd_ring);
1326 destroy_device_pool:
1327 dma_pool_destroy(pdev->device_pool);
1328 destroy_segment_pool:
1329 dma_pool_destroy(pdev->segment_pool);
1330 release_dcbaa:
1331 dma_free_coherent(dev, sizeof(*pdev->dcbaa), pdev->dcbaa,
1332 pdev->dcbaa->dma);
1333
1334 cdnsp_reset(pdev);
1335
1336 return ret;
1337 }