1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * xhci-dbgcap.c - xHCI debug capability support
4 *
5 * Copyright (C) 2017 Intel Corporation
6 *
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
8 */
9 #include <linux/bug.h>
10 #include <linux/device.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/errno.h>
13 #include <linux/kstrtox.h>
14 #include <linux/list.h>
15 #include <linux/nls.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/string.h>
20 #include <linux/sysfs.h>
21 #include <linux/types.h>
22 #include <linux/workqueue.h>
23
24 #include <linux/io-64-nonatomic-lo-hi.h>
25
26 #include <asm/byteorder.h>
27
28 #include "xhci.h"
29 #include "xhci-trace.h"
30 #include "xhci-dbgcap.h"
31
32 static const struct dbc_str dbc_str_default = {
33 .manufacturer = "Linux Foundation",
34 .product = "Linux USB Debug Target",
35 .serial = "0001",
36 };
37
dbc_free_ctx(struct device * dev,struct xhci_container_ctx * ctx)38 static void dbc_free_ctx(struct device *dev, struct xhci_container_ctx *ctx)
39 {
40 if (!ctx)
41 return;
42 dma_free_coherent(dev, ctx->size, ctx->bytes, ctx->dma);
43 kfree(ctx);
44 }
45
46 /* we use only one segment for DbC rings */
dbc_ring_free(struct device * dev,struct xhci_ring * ring)47 static void dbc_ring_free(struct device *dev, struct xhci_ring *ring)
48 {
49 if (!ring)
50 return;
51
52 if (ring->first_seg) {
53 dma_free_coherent(dev, TRB_SEGMENT_SIZE,
54 ring->first_seg->trbs,
55 ring->first_seg->dma);
56 kfree(ring->first_seg);
57 }
58 kfree(ring);
59 }
60
xhci_dbc_init_ep_contexts(struct xhci_dbc * dbc)61 static void xhci_dbc_init_ep_contexts(struct xhci_dbc *dbc)
62 {
63 struct xhci_ep_ctx *ep_ctx;
64 unsigned int max_burst;
65 dma_addr_t deq;
66
67 max_burst = DBC_CTRL_MAXBURST(readl(&dbc->regs->control));
68
69 /* Populate bulk out endpoint context: */
70 ep_ctx = dbc_bulkout_ctx(dbc);
71 deq = dbc_bulkout_enq(dbc);
72 ep_ctx->ep_info = 0;
73 ep_ctx->ep_info2 = dbc_epctx_info2(BULK_OUT_EP, 1024, max_burst);
74 ep_ctx->deq = cpu_to_le64(deq | dbc->ring_out->cycle_state);
75
76 /* Populate bulk in endpoint context: */
77 ep_ctx = dbc_bulkin_ctx(dbc);
78 deq = dbc_bulkin_enq(dbc);
79 ep_ctx->ep_info = 0;
80 ep_ctx->ep_info2 = dbc_epctx_info2(BULK_IN_EP, 1024, max_burst);
81 ep_ctx->deq = cpu_to_le64(deq | dbc->ring_in->cycle_state);
82 }
83
get_str_desc_len(const char * desc)84 static u8 get_str_desc_len(const char *desc)
85 {
86 return ((struct usb_string_descriptor *)desc)->bLength;
87 }
88
dbc_prepare_info_context_str_len(struct dbc_str_descs * descs)89 static u32 dbc_prepare_info_context_str_len(struct dbc_str_descs *descs)
90 {
91 u32 len;
92
93 len = get_str_desc_len(descs->serial);
94 len <<= 8;
95 len += get_str_desc_len(descs->product);
96 len <<= 8;
97 len += get_str_desc_len(descs->manufacturer);
98 len <<= 8;
99 len += get_str_desc_len(descs->string0);
100
101 return len;
102 }
103
xhci_dbc_populate_str_desc(char * desc,const char * src)104 static int xhci_dbc_populate_str_desc(char *desc, const char *src)
105 {
106 struct usb_string_descriptor *s_desc;
107 int len;
108
109 s_desc = (struct usb_string_descriptor *)desc;
110
111 /* len holds number of 2 byte UTF-16 characters */
112 len = utf8s_to_utf16s(src, strlen(src), UTF16_LITTLE_ENDIAN,
113 (wchar_t *)s_desc->wData, USB_MAX_STRING_LEN * 2);
114 if (len < 0)
115 return len;
116
117 s_desc->bLength = len * 2 + 2;
118 s_desc->bDescriptorType = USB_DT_STRING;
119
120 return s_desc->bLength;
121 }
122
xhci_dbc_populate_str_descs(struct dbc_str_descs * str_descs,struct dbc_str * str)123 static void xhci_dbc_populate_str_descs(struct dbc_str_descs *str_descs,
124 struct dbc_str *str)
125 {
126 /* Serial string: */
127 xhci_dbc_populate_str_desc(str_descs->serial, str->serial);
128
129 /* Product string: */
130 xhci_dbc_populate_str_desc(str_descs->product, str->product);
131
132 /* Manufacturer string: */
133 xhci_dbc_populate_str_desc(str_descs->manufacturer, str->manufacturer);
134
135 /* String0: */
136 str_descs->string0[0] = 4;
137 str_descs->string0[1] = USB_DT_STRING;
138 str_descs->string0[2] = 0x09;
139 str_descs->string0[3] = 0x04;
140 }
141
xhci_dbc_init_contexts(struct xhci_dbc * dbc)142 static void xhci_dbc_init_contexts(struct xhci_dbc *dbc)
143 {
144 struct dbc_info_context *info;
145 u32 dev_info;
146 dma_addr_t dma;
147
148 if (!dbc)
149 return;
150
151 /* Populate info Context: */
152 info = (struct dbc_info_context *)dbc->ctx->bytes;
153 dma = dbc->str_descs_dma;
154 info->string0 = cpu_to_le64(dma);
155 info->manufacturer = cpu_to_le64(dma + USB_MAX_STRING_DESC_LEN);
156 info->product = cpu_to_le64(dma + USB_MAX_STRING_DESC_LEN * 2);
157 info->serial = cpu_to_le64(dma + USB_MAX_STRING_DESC_LEN * 3);
158 info->length = cpu_to_le32(dbc_prepare_info_context_str_len(dbc->str_descs));
159
160 /* Populate bulk in and out endpoint contexts: */
161 xhci_dbc_init_ep_contexts(dbc);
162
163 /* Set DbC context and info registers: */
164 lo_hi_writeq(dbc->ctx->dma, &dbc->regs->dccp);
165
166 dev_info = (dbc->idVendor << 16) | dbc->bInterfaceProtocol;
167 writel(dev_info, &dbc->regs->devinfo1);
168
169 dev_info = (dbc->bcdDevice << 16) | dbc->idProduct;
170 writel(dev_info, &dbc->regs->devinfo2);
171 }
172
xhci_dbc_giveback(struct dbc_request * req,int status)173 static void xhci_dbc_giveback(struct dbc_request *req, int status)
174 __releases(&dbc->lock)
175 __acquires(&dbc->lock)
176 {
177 struct xhci_dbc *dbc = req->dbc;
178 struct device *dev = dbc->dev;
179
180 list_del_init(&req->list_pending);
181 req->trb_dma = 0;
182 req->trb = NULL;
183
184 if (req->status == -EINPROGRESS)
185 req->status = status;
186
187 trace_xhci_dbc_giveback_request(req);
188
189 dma_unmap_single(dev,
190 req->dma,
191 req->length,
192 dbc_ep_dma_direction(req));
193
194 /* Give back the transfer request: */
195 spin_unlock(&dbc->lock);
196 req->complete(dbc, req);
197 spin_lock(&dbc->lock);
198 }
199
trb_to_noop(union xhci_trb * trb)200 static void trb_to_noop(union xhci_trb *trb)
201 {
202 trb->generic.field[0] = 0;
203 trb->generic.field[1] = 0;
204 trb->generic.field[2] = 0;
205 trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
206 trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(TRB_TR_NOOP));
207 }
208
xhci_dbc_flush_single_request(struct dbc_request * req)209 static void xhci_dbc_flush_single_request(struct dbc_request *req)
210 {
211 trb_to_noop(req->trb);
212 xhci_dbc_giveback(req, -ESHUTDOWN);
213 }
214
xhci_dbc_flush_endpoint_requests(struct dbc_ep * dep)215 static void xhci_dbc_flush_endpoint_requests(struct dbc_ep *dep)
216 {
217 struct dbc_request *req, *tmp;
218
219 list_for_each_entry_safe(req, tmp, &dep->list_pending, list_pending)
220 xhci_dbc_flush_single_request(req);
221 }
222
xhci_dbc_flush_requests(struct xhci_dbc * dbc)223 static void xhci_dbc_flush_requests(struct xhci_dbc *dbc)
224 {
225 xhci_dbc_flush_endpoint_requests(&dbc->eps[BULK_OUT]);
226 xhci_dbc_flush_endpoint_requests(&dbc->eps[BULK_IN]);
227 }
228
229 struct dbc_request *
dbc_alloc_request(struct xhci_dbc * dbc,unsigned int direction,gfp_t flags)230 dbc_alloc_request(struct xhci_dbc *dbc, unsigned int direction, gfp_t flags)
231 {
232 struct dbc_request *req;
233
234 if (direction != BULK_IN &&
235 direction != BULK_OUT)
236 return NULL;
237
238 if (!dbc)
239 return NULL;
240
241 req = kzalloc_obj(*req, flags);
242 if (!req)
243 return NULL;
244
245 req->dbc = dbc;
246 INIT_LIST_HEAD(&req->list_pending);
247 INIT_LIST_HEAD(&req->list_pool);
248 req->direction = direction;
249
250 trace_xhci_dbc_alloc_request(req);
251
252 return req;
253 }
254
255 void
dbc_free_request(struct dbc_request * req)256 dbc_free_request(struct dbc_request *req)
257 {
258 trace_xhci_dbc_free_request(req);
259
260 kfree(req);
261 }
262
263 static void
xhci_dbc_queue_trb(struct xhci_ring * ring,u32 field1,u32 field2,u32 field3,u32 field4)264 xhci_dbc_queue_trb(struct xhci_ring *ring, u32 field1,
265 u32 field2, u32 field3, u32 field4)
266 {
267 union xhci_trb *trb, *next;
268
269 trb = ring->enqueue;
270 trb->generic.field[0] = cpu_to_le32(field1);
271 trb->generic.field[1] = cpu_to_le32(field2);
272 trb->generic.field[2] = cpu_to_le32(field3);
273 trb->generic.field[3] = cpu_to_le32(field4);
274
275 trace_xhci_dbc_gadget_ep_queue(ring, &trb->generic,
276 xhci_trb_virt_to_dma(ring->enq_seg,
277 ring->enqueue));
278 next = ++(ring->enqueue);
279 if (TRB_TYPE_LINK_LE32(next->link.control)) {
280 next->link.control ^= cpu_to_le32(TRB_CYCLE);
281 ring->enqueue = ring->enq_seg->trbs;
282 ring->cycle_state ^= 1;
283 }
284 }
285
xhci_dbc_queue_bulk_tx(struct dbc_ep * dep,struct dbc_request * req)286 static int xhci_dbc_queue_bulk_tx(struct dbc_ep *dep,
287 struct dbc_request *req)
288 {
289 u64 addr;
290 union xhci_trb *trb;
291 unsigned int num_trbs;
292 struct xhci_dbc *dbc = req->dbc;
293 struct xhci_ring *ring = dep->ring;
294 u32 length, control, cycle;
295
296 num_trbs = count_trbs(req->dma, req->length);
297 WARN_ON(num_trbs != 1);
298 if (xhci_num_trbs_free(ring) <= num_trbs)
299 return -EBUSY;
300
301 addr = req->dma;
302 trb = ring->enqueue;
303 cycle = ring->cycle_state;
304 length = TRB_LEN(req->length);
305 control = TRB_TYPE(TRB_NORMAL) | TRB_IOC;
306
307 if (cycle)
308 control &= cpu_to_le32(~TRB_CYCLE);
309 else
310 control |= cpu_to_le32(TRB_CYCLE);
311
312 req->trb = ring->enqueue;
313 req->trb_dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
314 xhci_dbc_queue_trb(ring,
315 lower_32_bits(addr),
316 upper_32_bits(addr),
317 length, control);
318
319 /*
320 * Add a barrier between writes of trb fields and flipping
321 * the cycle bit:
322 */
323 wmb();
324
325 if (cycle)
326 trb->generic.field[3] |= cpu_to_le32(TRB_CYCLE);
327 else
328 trb->generic.field[3] &= cpu_to_le32(~TRB_CYCLE);
329
330 writel(DBC_DOOR_BELL_TARGET(dep->direction), &dbc->regs->doorbell);
331
332 return 0;
333 }
334
335 static int
dbc_ep_do_queue(struct dbc_request * req)336 dbc_ep_do_queue(struct dbc_request *req)
337 {
338 int ret;
339 struct xhci_dbc *dbc = req->dbc;
340 struct device *dev = dbc->dev;
341 struct dbc_ep *dep = &dbc->eps[req->direction];
342
343 if (!req->length || !req->buf)
344 return -EINVAL;
345
346 req->actual = 0;
347 req->status = -EINPROGRESS;
348
349 req->dma = dma_map_single(dev,
350 req->buf,
351 req->length,
352 dbc_ep_dma_direction(dep));
353 if (dma_mapping_error(dev, req->dma)) {
354 dev_err(dbc->dev, "failed to map buffer\n");
355 return -EFAULT;
356 }
357
358 ret = xhci_dbc_queue_bulk_tx(dep, req);
359 if (ret) {
360 dev_err(dbc->dev, "failed to queue trbs\n");
361 dma_unmap_single(dev,
362 req->dma,
363 req->length,
364 dbc_ep_dma_direction(dep));
365 return -EFAULT;
366 }
367
368 list_add_tail(&req->list_pending, &dep->list_pending);
369
370 return 0;
371 }
372
dbc_ep_queue(struct dbc_request * req)373 int dbc_ep_queue(struct dbc_request *req)
374 {
375 unsigned long flags;
376 struct xhci_dbc *dbc = req->dbc;
377 int ret = -ESHUTDOWN;
378
379 if (!dbc)
380 return -ENODEV;
381
382 if (req->direction != BULK_IN &&
383 req->direction != BULK_OUT)
384 return -EINVAL;
385
386 spin_lock_irqsave(&dbc->lock, flags);
387 if (dbc->state == DS_CONFIGURED)
388 ret = dbc_ep_do_queue(req);
389 spin_unlock_irqrestore(&dbc->lock, flags);
390
391 mod_delayed_work(system_percpu_wq, &dbc->event_work, 0);
392
393 trace_xhci_dbc_queue_request(req);
394
395 return ret;
396 }
397
xhci_dbc_do_eps_init(struct xhci_dbc * dbc,bool direction)398 static inline void xhci_dbc_do_eps_init(struct xhci_dbc *dbc, bool direction)
399 {
400 struct dbc_ep *dep;
401
402 dep = &dbc->eps[direction];
403 dep->dbc = dbc;
404 dep->direction = direction;
405 dep->ring = direction ? dbc->ring_in : dbc->ring_out;
406
407 INIT_LIST_HEAD(&dep->list_pending);
408 }
409
xhci_dbc_eps_init(struct xhci_dbc * dbc)410 static void xhci_dbc_eps_init(struct xhci_dbc *dbc)
411 {
412 xhci_dbc_do_eps_init(dbc, BULK_OUT);
413 xhci_dbc_do_eps_init(dbc, BULK_IN);
414 }
415
xhci_dbc_eps_exit(struct xhci_dbc * dbc)416 static void xhci_dbc_eps_exit(struct xhci_dbc *dbc)
417 {
418 memset(dbc->eps, 0, sizeof_field(struct xhci_dbc, eps));
419 }
420
dbc_erst_alloc(struct device * dev,struct xhci_ring * evt_ring,struct xhci_erst * erst,gfp_t flags)421 static int dbc_erst_alloc(struct device *dev, struct xhci_ring *evt_ring,
422 struct xhci_erst *erst, gfp_t flags)
423 {
424 erst->entries = dma_alloc_coherent(dev, sizeof(*erst->entries),
425 &erst->erst_dma_addr, flags);
426 if (!erst->entries)
427 return -ENOMEM;
428
429 erst->num_entries = 1;
430 erst->entries[0].seg_addr = cpu_to_le64(evt_ring->first_seg->dma);
431 erst->entries[0].seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
432 erst->entries[0].rsvd = 0;
433 return 0;
434 }
435
dbc_erst_free(struct device * dev,struct xhci_erst * erst)436 static void dbc_erst_free(struct device *dev, struct xhci_erst *erst)
437 {
438 dma_free_coherent(dev, sizeof(*erst->entries), erst->entries,
439 erst->erst_dma_addr);
440 erst->entries = NULL;
441 }
442
443 static struct xhci_container_ctx *
dbc_alloc_ctx(struct device * dev,gfp_t flags)444 dbc_alloc_ctx(struct device *dev, gfp_t flags)
445 {
446 struct xhci_container_ctx *ctx;
447
448 ctx = kzalloc_obj(*ctx, flags);
449 if (!ctx)
450 return NULL;
451
452 /* xhci 7.6.9, all three contexts; info, ep-out and ep-in. Each 64 bytes*/
453 ctx->size = 3 * DBC_CONTEXT_SIZE;
454 ctx->bytes = dma_alloc_coherent(dev, ctx->size, &ctx->dma, flags);
455 if (!ctx->bytes) {
456 kfree(ctx);
457 return NULL;
458 }
459 return ctx;
460 }
461
xhci_dbc_ring_init(struct xhci_ring * ring)462 static void xhci_dbc_ring_init(struct xhci_ring *ring)
463 {
464 struct xhci_segment *seg = ring->first_seg;
465
466 /* clear all trbs on ring in case of old ring */
467 memset(seg->trbs, 0, TRB_SEGMENT_SIZE);
468
469 /* Only event ring does not use link TRB */
470 if (ring->type != TYPE_EVENT) {
471 union xhci_trb *trb = &seg->trbs[TRBS_PER_SEGMENT - 1];
472
473 trb->link.segment_ptr = cpu_to_le64(ring->first_seg->dma);
474 trb->link.control = cpu_to_le32(LINK_TOGGLE | TRB_TYPE(TRB_LINK));
475 }
476 xhci_initialize_ring_info(ring);
477 }
478
xhci_dbc_reinit_ep_rings(struct xhci_dbc * dbc)479 static int xhci_dbc_reinit_ep_rings(struct xhci_dbc *dbc)
480 {
481 struct xhci_ring *in_ring = dbc->eps[BULK_IN].ring;
482 struct xhci_ring *out_ring = dbc->eps[BULK_OUT].ring;
483
484 if (!in_ring || !out_ring || !dbc->ctx) {
485 dev_warn(dbc->dev, "Can't re-init unallocated endpoints\n");
486 return -ENODEV;
487 }
488
489 xhci_dbc_ring_init(in_ring);
490 xhci_dbc_ring_init(out_ring);
491
492 /* set ep context enqueue, dequeue, and cycle to initial values */
493 xhci_dbc_init_ep_contexts(dbc);
494
495 return 0;
496 }
497
498 static struct xhci_ring *
xhci_dbc_ring_alloc(struct device * dev,enum xhci_ring_type type,gfp_t flags)499 xhci_dbc_ring_alloc(struct device *dev, enum xhci_ring_type type, gfp_t flags)
500 {
501 struct xhci_ring *ring;
502 struct xhci_segment *seg;
503 dma_addr_t dma;
504
505 ring = kzalloc_obj(*ring, flags);
506 if (!ring)
507 return NULL;
508
509 ring->num_segs = 1;
510 ring->type = type;
511
512 seg = kzalloc_obj(*seg, flags);
513 if (!seg)
514 goto seg_fail;
515
516 ring->first_seg = seg;
517 ring->last_seg = seg;
518 seg->next = seg;
519
520 seg->trbs = dma_alloc_coherent(dev, TRB_SEGMENT_SIZE, &dma, flags);
521 if (!seg->trbs)
522 goto dma_fail;
523
524 seg->dma = dma;
525
526 INIT_LIST_HEAD(&ring->td_list);
527
528 xhci_dbc_ring_init(ring);
529
530 return ring;
531 dma_fail:
532 kfree(seg);
533 seg_fail:
534 kfree(ring);
535 return NULL;
536 }
537
xhci_dbc_mem_init(struct xhci_dbc * dbc,gfp_t flags)538 static int xhci_dbc_mem_init(struct xhci_dbc *dbc, gfp_t flags)
539 {
540 int ret;
541 dma_addr_t deq;
542 struct device *dev = dbc->dev;
543
544 /* Allocate various rings for events and transfers: */
545 dbc->ring_evt = xhci_dbc_ring_alloc(dev, TYPE_EVENT, flags);
546 if (!dbc->ring_evt)
547 goto evt_fail;
548
549 dbc->ring_in = xhci_dbc_ring_alloc(dev, TYPE_BULK, flags);
550 if (!dbc->ring_in)
551 goto in_fail;
552
553 dbc->ring_out = xhci_dbc_ring_alloc(dev, TYPE_BULK, flags);
554 if (!dbc->ring_out)
555 goto out_fail;
556
557 /* Allocate and populate ERST: */
558 ret = dbc_erst_alloc(dev, dbc->ring_evt, &dbc->erst, flags);
559 if (ret)
560 goto erst_fail;
561
562 /* Allocate context data structure: */
563 dbc->ctx = dbc_alloc_ctx(dev, flags); /* was sysdev, and is still */
564 if (!dbc->ctx)
565 goto ctx_fail;
566
567 /* Allocate the string table: */
568 dbc->str_descs_size = sizeof(*dbc->str_descs);
569 dbc->str_descs = dma_alloc_coherent(dev, dbc->str_descs_size,
570 &dbc->str_descs_dma, flags);
571 if (!dbc->str_descs)
572 goto str_descs_fail;
573
574 /* Setup ERST register: */
575 writel(dbc->erst.num_entries, &dbc->regs->ersts);
576
577 lo_hi_writeq(dbc->erst.erst_dma_addr, &dbc->regs->erstba);
578 deq = xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
579 dbc->ring_evt->dequeue);
580 lo_hi_writeq(deq, &dbc->regs->erdp);
581
582 /* Setup string descriptors and contexts: */
583 xhci_dbc_populate_str_descs(dbc->str_descs, &dbc->str);
584 xhci_dbc_init_contexts(dbc);
585
586 xhci_dbc_eps_init(dbc);
587 dbc->state = DS_INITIALIZED;
588
589 return 0;
590
591 str_descs_fail:
592 dbc_free_ctx(dev, dbc->ctx);
593 dbc->ctx = NULL;
594 ctx_fail:
595 dbc_erst_free(dev, &dbc->erst);
596 erst_fail:
597 dbc_ring_free(dev, dbc->ring_out);
598 dbc->ring_out = NULL;
599 out_fail:
600 dbc_ring_free(dev, dbc->ring_in);
601 dbc->ring_in = NULL;
602 in_fail:
603 dbc_ring_free(dev, dbc->ring_evt);
604 dbc->ring_evt = NULL;
605 evt_fail:
606 return -ENOMEM;
607 }
608
xhci_dbc_mem_cleanup(struct xhci_dbc * dbc)609 static void xhci_dbc_mem_cleanup(struct xhci_dbc *dbc)
610 {
611 if (!dbc)
612 return;
613
614 xhci_dbc_eps_exit(dbc);
615
616 dma_free_coherent(dbc->dev, dbc->str_descs_size, dbc->str_descs, dbc->str_descs_dma);
617 dbc->str_descs = NULL;
618
619 dbc_free_ctx(dbc->dev, dbc->ctx);
620 dbc->ctx = NULL;
621
622 dbc_erst_free(dbc->dev, &dbc->erst);
623 dbc_ring_free(dbc->dev, dbc->ring_out);
624 dbc_ring_free(dbc->dev, dbc->ring_in);
625 dbc_ring_free(dbc->dev, dbc->ring_evt);
626 dbc->ring_in = NULL;
627 dbc->ring_out = NULL;
628 dbc->ring_evt = NULL;
629 }
630
xhci_dbc_enable_dce(struct xhci_dbc * dbc,bool enable)631 static int xhci_dbc_enable_dce(struct xhci_dbc *dbc, bool enable)
632 {
633 u32 done_state = 0;
634 u32 ctrl = 0;
635
636 if (enable) {
637 ctrl = readl(&dbc->regs->control);
638 ctrl |= DBC_CTRL_DBC_ENABLE | DBC_CTRL_PORT_ENABLE;
639 done_state = DBC_CTRL_DBC_ENABLE;
640 }
641
642 writel(ctrl, &dbc->regs->control);
643 return xhci_handshake(&dbc->regs->control, DBC_CTRL_DBC_ENABLE,
644 done_state, 1000);
645 }
646
xhci_dbc_set_state(struct xhci_dbc * dbc,enum dbc_state new_state)647 static void xhci_dbc_set_state(struct xhci_dbc *dbc, enum dbc_state new_state)
648 {
649 if (dbc->state == new_state)
650 return;
651
652 switch (new_state) {
653 case DS_ENABLED:
654 /*
655 * DbC pm usage is 1 here, both when moved from disconnect or
656 * configured states, or when setting initial DbC enable state.
657 * Just enable pending put
658 */
659 dev_dbg(dbc->dev, "DbC set pending_rpm_put = 1\n");
660 dbc->pending_rpm_put = 1;
661 break;
662 case DS_CONNECTED:
663 if (dbc->pending_rpm_put)
664 /* DbC pm usage still 1, just remove pending put */
665 dbc->pending_rpm_put = 0;
666 else
667 /* DbC pm usage was put to 0, call get */
668 pm_runtime_get(dbc->dev);
669 break;
670 default:
671 break;
672 }
673
674 dbc->state_timestamp = jiffies;
675 dbc->state = new_state;
676 }
677
xhci_do_dbc_start(struct xhci_dbc * dbc)678 static int xhci_do_dbc_start(struct xhci_dbc *dbc)
679 {
680 int ret;
681
682 if (dbc->state != DS_DISABLED)
683 return -EINVAL;
684
685 ret = xhci_dbc_enable_dce(dbc, false);
686 if (ret)
687 return ret;
688
689 ret = xhci_dbc_mem_init(dbc, GFP_ATOMIC);
690 if (ret)
691 return ret;
692
693 ret = xhci_dbc_enable_dce(dbc, true);
694 if (ret)
695 return ret;
696
697 xhci_dbc_set_state(dbc, DS_ENABLED);
698
699 return 0;
700 }
701
xhci_dbc_start(struct xhci_dbc * dbc)702 static int xhci_dbc_start(struct xhci_dbc *dbc)
703 {
704 int ret;
705 unsigned long flags;
706
707 WARN_ON(!dbc);
708
709 pm_runtime_get(dbc->dev);
710
711 spin_lock_irqsave(&dbc->lock, flags);
712 ret = xhci_do_dbc_start(dbc);
713 if (ret)
714 goto err_unlock;
715
716 spin_unlock_irqrestore(&dbc->lock, flags);
717
718 mod_delayed_work(system_percpu_wq, &dbc->event_work,
719 msecs_to_jiffies(dbc->poll_interval));
720
721 return 0;
722
723 err_unlock:
724
725 spin_unlock_irqrestore(&dbc->lock, flags);
726 pm_runtime_put(dbc->dev); /* note this was self.controller */
727
728 return ret;
729 }
730
xhci_dbc_stop(struct xhci_dbc * dbc)731 static void xhci_dbc_stop(struct xhci_dbc *dbc)
732 {
733 unsigned long flags;
734 bool need_rpm_put = false;
735
736 WARN_ON(!dbc);
737
738 spin_lock(&dbc->lock);
739
740 switch (dbc->state) {
741 case DS_DISABLED:
742 spin_unlock(&dbc->lock);
743 return;
744 case DS_CONFIGURED:
745 xhci_dbc_flush_requests(dbc);
746 spin_unlock(&dbc->lock);
747
748 if (dbc->driver->disconnect)
749 dbc->driver->disconnect(dbc);
750 break;
751 default:
752 spin_unlock(&dbc->lock);
753 break;
754 }
755
756 cancel_delayed_work_sync(&dbc->event_work);
757
758 spin_lock_irqsave(&dbc->lock, flags);
759 writel(0, &dbc->regs->control);
760
761 if (dbc->state == DS_CONNECTED || dbc->state == DS_CONFIGURED ||
762 dbc->pending_rpm_put)
763 need_rpm_put = true;
764
765 dbc->pending_rpm_put = 0;
766
767 xhci_dbc_set_state(dbc, DS_DISABLED);
768 spin_unlock_irqrestore(&dbc->lock, flags);
769
770 xhci_dbc_mem_cleanup(dbc);
771
772 if (need_rpm_put)
773 pm_runtime_put(dbc->dev);
774 }
775
776 static void
handle_ep_halt_changes(struct xhci_dbc * dbc,struct dbc_ep * dep,bool halted)777 handle_ep_halt_changes(struct xhci_dbc *dbc, struct dbc_ep *dep, bool halted)
778 {
779 if (halted) {
780 dev_info(dbc->dev, "DbC Endpoint halted\n");
781 dep->halted = 1;
782
783 } else if (dep->halted) {
784 dev_info(dbc->dev, "DbC Endpoint halt cleared\n");
785 dep->halted = 0;
786
787 if (!list_empty(&dep->list_pending))
788 writel(DBC_DOOR_BELL_TARGET(dep->direction),
789 &dbc->regs->doorbell);
790 }
791 }
792
793 static void
dbc_handle_port_status(struct xhci_dbc * dbc,union xhci_trb * event)794 dbc_handle_port_status(struct xhci_dbc *dbc, union xhci_trb *event)
795 {
796 u32 portsc;
797
798 portsc = readl(&dbc->regs->portsc);
799 if (portsc & DBC_PORTSC_CONN_CHANGE)
800 dev_info(dbc->dev, "DbC port connect change\n");
801
802 if (portsc & DBC_PORTSC_RESET_CHANGE)
803 dev_info(dbc->dev, "DbC port reset change\n");
804
805 if (portsc & DBC_PORTSC_LINK_CHANGE)
806 dev_info(dbc->dev, "DbC port link status change\n");
807
808 if (portsc & DBC_PORTSC_CONFIG_CHANGE)
809 dev_info(dbc->dev, "DbC config error change\n");
810
811 /* Port reset change bit will be cleared in other place: */
812 writel(portsc & ~DBC_PORTSC_RESET_CHANGE, &dbc->regs->portsc);
813 }
814
dbc_handle_xfer_event(struct xhci_dbc * dbc,union xhci_trb * event)815 static void dbc_handle_xfer_event(struct xhci_dbc *dbc, union xhci_trb *event)
816 {
817 struct dbc_ep *dep;
818 struct xhci_ring *ring;
819 int ep_id;
820 int status;
821 struct xhci_ep_ctx *ep_ctx;
822 u32 comp_code;
823 size_t remain_length;
824 struct dbc_request *req = NULL, *r;
825
826 comp_code = GET_COMP_CODE(le32_to_cpu(event->generic.field[2]));
827 remain_length = EVENT_TRB_LEN(le32_to_cpu(event->generic.field[2]));
828 ep_id = TRB_TO_EP_ID(le32_to_cpu(event->generic.field[3]));
829 dep = (ep_id == EPID_OUT) ?
830 get_out_ep(dbc) : get_in_ep(dbc);
831 ep_ctx = (ep_id == EPID_OUT) ?
832 dbc_bulkout_ctx(dbc) : dbc_bulkin_ctx(dbc);
833 ring = dep->ring;
834
835 /* Match the pending request: */
836 list_for_each_entry(r, &dep->list_pending, list_pending) {
837 if (r->trb_dma == event->trans_event.buffer) {
838 req = r;
839 break;
840 }
841 if (r->status == -COMP_STALL_ERROR) {
842 dev_warn(dbc->dev, "Give back stale stalled req\n");
843 xhci_dbc_giveback(r, 0);
844 }
845 }
846
847 if (!req) {
848 dev_warn(dbc->dev, "no matched request\n");
849 return;
850 }
851
852 trace_xhci_dbc_handle_transfer(ring, &req->trb->generic, req->trb_dma);
853
854 switch (comp_code) {
855 case COMP_SUCCESS:
856 remain_length = 0;
857 fallthrough;
858 case COMP_SHORT_PACKET:
859 status = 0;
860 break;
861 case COMP_TRB_ERROR:
862 case COMP_BABBLE_DETECTED_ERROR:
863 case COMP_USB_TRANSACTION_ERROR:
864 dev_warn(dbc->dev, "tx error %d detected\n", comp_code);
865 status = -comp_code;
866 break;
867 case COMP_STALL_ERROR:
868 dev_warn(dbc->dev, "Stall error at bulk TRB %llx, remaining %zu, ep deq %llx\n",
869 event->trans_event.buffer, remain_length, ep_ctx->deq);
870 status = 0;
871 dep->halted = 1;
872
873 /*
874 * xHC DbC may trigger a STALL bulk xfer event when host sends a
875 * ClearFeature(ENDPOINT_HALT) request even if there wasn't an
876 * active bulk transfer.
877 *
878 * Don't give back this transfer request as hardware will later
879 * start processing TRBs starting from this 'STALLED' TRB,
880 * causing TRBs and requests to be out of sync.
881 *
882 * If STALL event shows some bytes were transferred then assume
883 * it's an actual transfer issue and give back the request.
884 * In this case mark the TRB as No-Op to avoid hw from using the
885 * TRB again.
886 */
887
888 if ((ep_ctx->deq & ~TRB_CYCLE) == event->trans_event.buffer) {
889 dev_dbg(dbc->dev, "Ep stopped on Stalled TRB\n");
890 if (remain_length == req->length) {
891 dev_dbg(dbc->dev, "Spurious stall event, keep req\n");
892 req->status = -COMP_STALL_ERROR;
893 req->actual = 0;
894 return;
895 }
896 dev_dbg(dbc->dev, "Give back stalled req, but turn TRB to No-op\n");
897 trb_to_noop(req->trb);
898 }
899 break;
900
901 default:
902 dev_err(dbc->dev, "unknown tx error %d\n", comp_code);
903 status = -comp_code;
904 break;
905 }
906
907 req->actual = req->length - remain_length;
908 xhci_dbc_giveback(req, status);
909 }
910
inc_evt_deq(struct xhci_ring * ring)911 static void inc_evt_deq(struct xhci_ring *ring)
912 {
913 /* If on the last TRB of the segment go back to the beginning */
914 if (ring->dequeue == &ring->deq_seg->trbs[TRBS_PER_SEGMENT - 1]) {
915 ring->cycle_state ^= 1;
916 ring->dequeue = ring->deq_seg->trbs;
917 return;
918 }
919 ring->dequeue++;
920 }
921
xhci_dbc_do_handle_events(struct xhci_dbc * dbc)922 static enum evtreturn xhci_dbc_do_handle_events(struct xhci_dbc *dbc)
923 {
924 dma_addr_t deq;
925 union xhci_trb *evt;
926 enum evtreturn ret = EVT_DONE;
927 u32 ctrl, portsc;
928 bool update_erdp = false;
929
930 /* DbC state machine: */
931 switch (dbc->state) {
932 case DS_DISABLED:
933 case DS_INITIALIZED:
934
935 return EVT_ERR;
936 case DS_ENABLED:
937 portsc = readl(&dbc->regs->portsc);
938 ctrl = readl(&dbc->regs->control);
939
940 if (portsc & DBC_PORTSC_CONN_STATUS) {
941 xhci_dbc_set_state(dbc, DS_CONNECTED);
942 dev_info(dbc->dev, "DbC connected\n");
943 } else if (!(ctrl & DBC_CTRL_DBC_ENABLE)) {
944 dev_err(dbc->dev, "unexpected DbC disable, xHC reset?\n");
945 } else if (dbc->pending_rpm_put &&
946 time_is_before_jiffies(dbc->state_timestamp +
947 msecs_to_jiffies(DBC_AUTOSUSPEND_DELAY))) {
948 dbc->pending_rpm_put = 0;
949 dev_dbg(dbc->dev, "DbC Enabled state for 15 seconds, allow rpm suspend\n");
950 pm_runtime_put(dbc->dev);
951 }
952
953 return EVT_DONE;
954 case DS_CONNECTED:
955 ctrl = readl(&dbc->regs->control);
956 portsc = readl(&dbc->regs->portsc);
957 if (ctrl & DBC_CTRL_DBC_RUN) {
958 xhci_dbc_set_state(dbc, DS_CONFIGURED);
959 dev_info(dbc->dev, "DbC configured\n");
960 writel(portsc, &dbc->regs->portsc);
961 ret = EVT_GSER;
962 break;
963 }
964
965 /* Connection lost */
966 if (!(portsc & DBC_PORTSC_CONN_STATUS)) {
967 /* covers DCE == 0 as it also sets CONN_STATUS to 0 */
968 dev_warn(dbc->dev, "DbC connection lost mid enumeration\n");
969 xhci_dbc_set_state(dbc, DS_ENABLED);
970
971 return EVT_DONE;
972 }
973
974 /* Enumeration timeout */
975 if (time_is_before_jiffies(dbc->state_timestamp +
976 msecs_to_jiffies(DBC_ENUMERATION_TIMEOUT))) {
977 dev_err(dbc->dev, "DbC enumeration timeout, re-enabling DbC\n");
978 dev_dbg(dbc->dev, "dcctrl %x, dcportsc %x\n", ctrl, portsc);
979
980 /* Toggle DCE to retry enumeration */
981 ret = xhci_dbc_enable_dce(dbc, false);
982 udelay(100);
983 ret = xhci_dbc_enable_dce(dbc, true);
984 xhci_dbc_set_state(dbc, DS_ENABLED);
985 }
986
987 return EVT_DONE;
988 case DS_CONFIGURED:
989 /* Handle cable unplug event: */
990 portsc = readl(&dbc->regs->portsc);
991 if (!(portsc & DBC_PORTSC_PORT_ENABLED) &&
992 !(portsc & DBC_PORTSC_CONN_STATUS)) {
993 dev_info(dbc->dev, "DbC cable unplugged\n");
994 xhci_dbc_set_state(dbc, DS_ENABLED);
995 xhci_dbc_flush_requests(dbc);
996 xhci_dbc_reinit_ep_rings(dbc);
997 return EVT_DISC;
998 }
999
1000 /* Handle debug port reset event: */
1001 if (portsc & DBC_PORTSC_RESET_CHANGE) {
1002 dev_info(dbc->dev, "DbC port reset\n");
1003 writel(portsc, &dbc->regs->portsc);
1004 xhci_dbc_set_state(dbc, DS_ENABLED);
1005 xhci_dbc_flush_requests(dbc);
1006 xhci_dbc_reinit_ep_rings(dbc);
1007 return EVT_DISC;
1008 }
1009
1010 /* Check and handle changes in endpoint halt status */
1011 ctrl = readl(&dbc->regs->control);
1012 handle_ep_halt_changes(dbc, get_in_ep(dbc), ctrl & DBC_CTRL_HALT_IN_TR);
1013 handle_ep_halt_changes(dbc, get_out_ep(dbc), ctrl & DBC_CTRL_HALT_OUT_TR);
1014
1015 /* Clear DbC run change bit: */
1016 if (ctrl & DBC_CTRL_DBC_RUN_CHANGE) {
1017 writel(ctrl, &dbc->regs->control);
1018 ctrl = readl(&dbc->regs->control);
1019 }
1020 break;
1021 default:
1022 dev_err(dbc->dev, "Unknown DbC state %d\n", dbc->state);
1023 break;
1024 }
1025
1026 /* Handle the events in the event ring: */
1027 evt = dbc->ring_evt->dequeue;
1028 while ((le32_to_cpu(evt->event_cmd.flags) & TRB_CYCLE) ==
1029 dbc->ring_evt->cycle_state) {
1030 /*
1031 * Add a barrier between reading the cycle flag and any
1032 * reads of the event's flags/data below:
1033 */
1034 rmb();
1035
1036 trace_xhci_dbc_handle_event(dbc->ring_evt, &evt->generic,
1037 xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
1038 dbc->ring_evt->dequeue));
1039
1040 switch (le32_to_cpu(evt->event_cmd.flags) & TRB_TYPE_BITMASK) {
1041 case TRB_TYPE(TRB_PORT_STATUS):
1042 dbc_handle_port_status(dbc, evt);
1043 break;
1044 case TRB_TYPE(TRB_TRANSFER):
1045 dbc_handle_xfer_event(dbc, evt);
1046 if (ret != EVT_GSER)
1047 ret = EVT_XFER_DONE;
1048 break;
1049 default:
1050 break;
1051 }
1052
1053 inc_evt_deq(dbc->ring_evt);
1054
1055 evt = dbc->ring_evt->dequeue;
1056 update_erdp = true;
1057 }
1058
1059 /* Update event ring dequeue pointer: */
1060 if (update_erdp) {
1061 deq = xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
1062 dbc->ring_evt->dequeue);
1063 lo_hi_writeq(deq, &dbc->regs->erdp);
1064 }
1065
1066 return ret;
1067 }
1068
xhci_dbc_handle_events(struct work_struct * work)1069 static void xhci_dbc_handle_events(struct work_struct *work)
1070 {
1071 enum evtreturn evtr;
1072 struct xhci_dbc *dbc;
1073 unsigned long flags;
1074 unsigned int poll_interval;
1075 unsigned long busypoll_timelimit;
1076
1077 dbc = container_of(to_delayed_work(work), struct xhci_dbc, event_work);
1078 poll_interval = dbc->poll_interval;
1079
1080 spin_lock_irqsave(&dbc->lock, flags);
1081 evtr = xhci_dbc_do_handle_events(dbc);
1082 spin_unlock_irqrestore(&dbc->lock, flags);
1083
1084 switch (evtr) {
1085 case EVT_GSER:
1086 if (dbc->driver->configure)
1087 dbc->driver->configure(dbc);
1088 break;
1089 case EVT_DISC:
1090 if (dbc->driver->disconnect)
1091 dbc->driver->disconnect(dbc);
1092 break;
1093 case EVT_DONE:
1094 /*
1095 * Set fast poll rate if there are pending out transfers, or
1096 * a transfer was recently processed
1097 */
1098 busypoll_timelimit = dbc->xfer_timestamp +
1099 msecs_to_jiffies(DBC_XFER_INACTIVITY_TIMEOUT);
1100
1101 if (!list_empty(&dbc->eps[BULK_OUT].list_pending) ||
1102 time_is_after_jiffies(busypoll_timelimit))
1103 poll_interval = 0;
1104 break;
1105 case EVT_XFER_DONE:
1106 dbc->xfer_timestamp = jiffies;
1107 poll_interval = 0;
1108 break;
1109 default:
1110 dev_info(dbc->dev, "stop handling dbc events\n");
1111 return;
1112 }
1113
1114 mod_delayed_work(system_percpu_wq, &dbc->event_work,
1115 msecs_to_jiffies(poll_interval));
1116 }
1117
1118 static const char * const dbc_state_strings[DS_MAX] = {
1119 [DS_DISABLED] = "disabled",
1120 [DS_INITIALIZED] = "initialized",
1121 [DS_ENABLED] = "enabled",
1122 [DS_CONNECTED] = "connected",
1123 [DS_CONFIGURED] = "configured",
1124 };
1125
dbc_show(struct device * dev,struct device_attribute * attr,char * buf)1126 static ssize_t dbc_show(struct device *dev,
1127 struct device_attribute *attr,
1128 char *buf)
1129 {
1130 struct xhci_dbc *dbc;
1131 struct xhci_hcd *xhci;
1132
1133 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1134 dbc = xhci->dbc;
1135
1136 if (dbc->state >= ARRAY_SIZE(dbc_state_strings))
1137 return sysfs_emit(buf, "unknown\n");
1138
1139 if (dbc->resume_required)
1140 return sysfs_emit(buf, "suspended\n");
1141
1142 return sysfs_emit(buf, "%s\n", dbc_state_strings[dbc->state]);
1143 }
1144
dbc_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1145 static ssize_t dbc_store(struct device *dev,
1146 struct device_attribute *attr,
1147 const char *buf, size_t count)
1148 {
1149 struct xhci_hcd *xhci;
1150 struct xhci_dbc *dbc;
1151
1152 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1153 dbc = xhci->dbc;
1154
1155 if (sysfs_streq(buf, "enable")) {
1156 pm_runtime_get_sync(dbc->dev);
1157
1158 mutex_lock(&dbc->enable_mutex);
1159 /*
1160 * DbC may already be enabled here if xhci was suspended with
1161 * dbc->resume_required set, and resumed by pm_runtime_get_sync()
1162 * above. In this case we end up calling xhci_dbc_start() twice,
1163 * second time returns an error but is harmless
1164 */
1165 xhci_dbc_start(dbc);
1166
1167 mutex_unlock(&dbc->enable_mutex);
1168 pm_runtime_put(dbc->dev);
1169 } else if (sysfs_streq(buf, "disable")) {
1170 mutex_lock(&dbc->enable_mutex);
1171
1172 dbc->resume_required = 0;
1173 xhci_dbc_stop(dbc);
1174
1175 mutex_unlock(&dbc->enable_mutex);
1176 } else {
1177 return -EINVAL;
1178 }
1179
1180 return count;
1181 }
1182
dbc_idVendor_show(struct device * dev,struct device_attribute * attr,char * buf)1183 static ssize_t dbc_idVendor_show(struct device *dev,
1184 struct device_attribute *attr,
1185 char *buf)
1186 {
1187 struct xhci_dbc *dbc;
1188 struct xhci_hcd *xhci;
1189
1190 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1191 dbc = xhci->dbc;
1192
1193 return sysfs_emit(buf, "%04x\n", dbc->idVendor);
1194 }
1195
dbc_idVendor_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1196 static ssize_t dbc_idVendor_store(struct device *dev,
1197 struct device_attribute *attr,
1198 const char *buf, size_t size)
1199 {
1200 struct xhci_dbc *dbc;
1201 struct xhci_hcd *xhci;
1202 void __iomem *ptr;
1203 u16 value;
1204 u32 dev_info;
1205 int ret;
1206
1207 ret = kstrtou16(buf, 0, &value);
1208 if (ret)
1209 return ret;
1210
1211 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1212 dbc = xhci->dbc;
1213 if (dbc->state != DS_DISABLED)
1214 return -EBUSY;
1215
1216 dbc->idVendor = value;
1217 ptr = &dbc->regs->devinfo1;
1218 dev_info = readl(ptr);
1219 dev_info = (dev_info & ~(0xffffu << 16)) | (value << 16);
1220 writel(dev_info, ptr);
1221
1222 return size;
1223 }
1224
dbc_idProduct_show(struct device * dev,struct device_attribute * attr,char * buf)1225 static ssize_t dbc_idProduct_show(struct device *dev,
1226 struct device_attribute *attr,
1227 char *buf)
1228 {
1229 struct xhci_dbc *dbc;
1230 struct xhci_hcd *xhci;
1231
1232 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1233 dbc = xhci->dbc;
1234
1235 return sysfs_emit(buf, "%04x\n", dbc->idProduct);
1236 }
1237
dbc_idProduct_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1238 static ssize_t dbc_idProduct_store(struct device *dev,
1239 struct device_attribute *attr,
1240 const char *buf, size_t size)
1241 {
1242 struct xhci_dbc *dbc;
1243 struct xhci_hcd *xhci;
1244 void __iomem *ptr;
1245 u32 dev_info;
1246 u16 value;
1247 int ret;
1248
1249 ret = kstrtou16(buf, 0, &value);
1250 if (ret)
1251 return ret;
1252
1253 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1254 dbc = xhci->dbc;
1255 if (dbc->state != DS_DISABLED)
1256 return -EBUSY;
1257
1258 dbc->idProduct = value;
1259 ptr = &dbc->regs->devinfo2;
1260 dev_info = readl(ptr);
1261 dev_info = (dev_info & ~(0xffffu)) | value;
1262 writel(dev_info, ptr);
1263 return size;
1264 }
1265
dbc_bcdDevice_show(struct device * dev,struct device_attribute * attr,char * buf)1266 static ssize_t dbc_bcdDevice_show(struct device *dev,
1267 struct device_attribute *attr,
1268 char *buf)
1269 {
1270 struct xhci_dbc *dbc;
1271 struct xhci_hcd *xhci;
1272
1273 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1274 dbc = xhci->dbc;
1275
1276 return sysfs_emit(buf, "%04x\n", dbc->bcdDevice);
1277 }
1278
dbc_bcdDevice_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1279 static ssize_t dbc_bcdDevice_store(struct device *dev,
1280 struct device_attribute *attr,
1281 const char *buf, size_t size)
1282 {
1283 struct xhci_dbc *dbc;
1284 struct xhci_hcd *xhci;
1285 void __iomem *ptr;
1286 u32 dev_info;
1287 u16 value;
1288 int ret;
1289
1290 ret = kstrtou16(buf, 0, &value);
1291 if (ret)
1292 return ret;
1293
1294 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1295 dbc = xhci->dbc;
1296 if (dbc->state != DS_DISABLED)
1297 return -EBUSY;
1298
1299 dbc->bcdDevice = value;
1300 ptr = &dbc->regs->devinfo2;
1301 dev_info = readl(ptr);
1302 dev_info = (dev_info & ~(0xffffu << 16)) | (value << 16);
1303 writel(dev_info, ptr);
1304
1305 return size;
1306 }
1307
dbc_manufacturer_show(struct device * dev,struct device_attribute * attr,char * buf)1308 static ssize_t dbc_manufacturer_show(struct device *dev,
1309 struct device_attribute *attr,
1310 char *buf)
1311 {
1312 struct xhci_hcd *xhci = hcd_to_xhci(dev_get_drvdata(dev));
1313 struct xhci_dbc *dbc = xhci->dbc;
1314
1315 return sysfs_emit(buf, "%s\n", dbc->str.manufacturer);
1316 }
1317
dbc_manufacturer_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1318 static ssize_t dbc_manufacturer_store(struct device *dev,
1319 struct device_attribute *attr,
1320 const char *buf, size_t size)
1321 {
1322 struct xhci_hcd *xhci = hcd_to_xhci(dev_get_drvdata(dev));
1323 struct xhci_dbc *dbc = xhci->dbc;
1324 size_t len;
1325
1326 if (dbc->state != DS_DISABLED)
1327 return -EBUSY;
1328
1329 len = strcspn(buf, "\n");
1330 if (!len)
1331 return -EINVAL;
1332
1333 if (len > USB_MAX_STRING_LEN)
1334 return -E2BIG;
1335
1336 memcpy(dbc->str.manufacturer, buf, len);
1337 dbc->str.manufacturer[len] = '\0';
1338
1339 return size;
1340 }
1341
dbc_product_show(struct device * dev,struct device_attribute * attr,char * buf)1342 static ssize_t dbc_product_show(struct device *dev,
1343 struct device_attribute *attr,
1344 char *buf)
1345 {
1346 struct xhci_hcd *xhci = hcd_to_xhci(dev_get_drvdata(dev));
1347 struct xhci_dbc *dbc = xhci->dbc;
1348
1349 return sysfs_emit(buf, "%s\n", dbc->str.product);
1350 }
1351
dbc_product_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1352 static ssize_t dbc_product_store(struct device *dev,
1353 struct device_attribute *attr,
1354 const char *buf, size_t size)
1355 {
1356 struct xhci_hcd *xhci = hcd_to_xhci(dev_get_drvdata(dev));
1357 struct xhci_dbc *dbc = xhci->dbc;
1358 size_t len;
1359
1360 if (dbc->state != DS_DISABLED)
1361 return -EBUSY;
1362
1363 len = strcspn(buf, "\n");
1364 if (!len)
1365 return -EINVAL;
1366
1367 if (len > USB_MAX_STRING_LEN)
1368 return -E2BIG;
1369
1370 memcpy(dbc->str.product, buf, len);
1371 dbc->str.product[len] = '\0';
1372
1373 return size;
1374 }
1375
dbc_serial_show(struct device * dev,struct device_attribute * attr,char * buf)1376 static ssize_t dbc_serial_show(struct device *dev,
1377 struct device_attribute *attr,
1378 char *buf)
1379 {
1380 struct xhci_hcd *xhci = hcd_to_xhci(dev_get_drvdata(dev));
1381 struct xhci_dbc *dbc = xhci->dbc;
1382
1383 return sysfs_emit(buf, "%s\n", dbc->str.serial);
1384 }
1385
dbc_serial_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1386 static ssize_t dbc_serial_store(struct device *dev,
1387 struct device_attribute *attr,
1388 const char *buf, size_t size)
1389 {
1390 struct xhci_hcd *xhci = hcd_to_xhci(dev_get_drvdata(dev));
1391 struct xhci_dbc *dbc = xhci->dbc;
1392 size_t len;
1393
1394 if (dbc->state != DS_DISABLED)
1395 return -EBUSY;
1396
1397 len = strcspn(buf, "\n");
1398 if (!len)
1399 return -EINVAL;
1400
1401 if (len > USB_MAX_STRING_LEN)
1402 return -E2BIG;
1403
1404 memcpy(dbc->str.serial, buf, len);
1405 dbc->str.serial[len] = '\0';
1406
1407 return size;
1408 }
1409
dbc_bInterfaceProtocol_show(struct device * dev,struct device_attribute * attr,char * buf)1410 static ssize_t dbc_bInterfaceProtocol_show(struct device *dev,
1411 struct device_attribute *attr,
1412 char *buf)
1413 {
1414 struct xhci_dbc *dbc;
1415 struct xhci_hcd *xhci;
1416
1417 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1418 dbc = xhci->dbc;
1419
1420 return sysfs_emit(buf, "%02x\n", dbc->bInterfaceProtocol);
1421 }
1422
dbc_bInterfaceProtocol_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1423 static ssize_t dbc_bInterfaceProtocol_store(struct device *dev,
1424 struct device_attribute *attr,
1425 const char *buf, size_t size)
1426 {
1427 struct xhci_dbc *dbc;
1428 struct xhci_hcd *xhci;
1429 void __iomem *ptr;
1430 u32 dev_info;
1431 u8 value;
1432 int ret;
1433
1434 /* bInterfaceProtocol is 8 bit, but... */
1435 ret = kstrtou8(buf, 0, &value);
1436 if (ret)
1437 return ret;
1438
1439 /* ...xhci only supports values 0 and 1 */
1440 if (value > 1)
1441 return -EINVAL;
1442
1443 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1444 dbc = xhci->dbc;
1445 if (dbc->state != DS_DISABLED)
1446 return -EBUSY;
1447
1448 dbc->bInterfaceProtocol = value;
1449 ptr = &dbc->regs->devinfo1;
1450 dev_info = readl(ptr);
1451 dev_info = (dev_info & ~(0xffu)) | value;
1452 writel(dev_info, ptr);
1453
1454 return size;
1455 }
1456
dbc_poll_interval_ms_show(struct device * dev,struct device_attribute * attr,char * buf)1457 static ssize_t dbc_poll_interval_ms_show(struct device *dev,
1458 struct device_attribute *attr,
1459 char *buf)
1460 {
1461 struct xhci_dbc *dbc;
1462 struct xhci_hcd *xhci;
1463
1464 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1465 dbc = xhci->dbc;
1466
1467 return sysfs_emit(buf, "%u\n", dbc->poll_interval);
1468 }
1469
dbc_poll_interval_ms_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1470 static ssize_t dbc_poll_interval_ms_store(struct device *dev,
1471 struct device_attribute *attr,
1472 const char *buf, size_t size)
1473 {
1474 struct xhci_dbc *dbc;
1475 struct xhci_hcd *xhci;
1476 u32 value;
1477 int ret;
1478
1479 ret = kstrtou32(buf, 0, &value);
1480 if (ret || value > DBC_POLL_INTERVAL_MAX)
1481 return -EINVAL;
1482
1483 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1484 dbc = xhci->dbc;
1485
1486 dbc->poll_interval = value;
1487
1488 mod_delayed_work(system_percpu_wq, &dbc->event_work, 0);
1489
1490 return size;
1491 }
1492
1493 static DEVICE_ATTR_RW(dbc);
1494 static DEVICE_ATTR_RW(dbc_idVendor);
1495 static DEVICE_ATTR_RW(dbc_idProduct);
1496 static DEVICE_ATTR_RW(dbc_bcdDevice);
1497 static DEVICE_ATTR_RW(dbc_serial);
1498 static DEVICE_ATTR_RW(dbc_product);
1499 static DEVICE_ATTR_RW(dbc_manufacturer);
1500 static DEVICE_ATTR_RW(dbc_bInterfaceProtocol);
1501 static DEVICE_ATTR_RW(dbc_poll_interval_ms);
1502
1503 static struct attribute *dbc_dev_attrs[] = {
1504 &dev_attr_dbc.attr,
1505 &dev_attr_dbc_idVendor.attr,
1506 &dev_attr_dbc_idProduct.attr,
1507 &dev_attr_dbc_bcdDevice.attr,
1508 &dev_attr_dbc_serial.attr,
1509 &dev_attr_dbc_product.attr,
1510 &dev_attr_dbc_manufacturer.attr,
1511 &dev_attr_dbc_bInterfaceProtocol.attr,
1512 &dev_attr_dbc_poll_interval_ms.attr,
1513 NULL
1514 };
1515 ATTRIBUTE_GROUPS(dbc_dev);
1516
1517 struct xhci_dbc *
xhci_alloc_dbc(struct device * dev,void __iomem * base,const struct dbc_driver * driver)1518 xhci_alloc_dbc(struct device *dev, void __iomem *base, const struct dbc_driver *driver)
1519 {
1520 struct xhci_dbc *dbc;
1521 int ret;
1522
1523 dbc = kzalloc_obj(*dbc);
1524 if (!dbc)
1525 return NULL;
1526
1527 dbc->regs = base;
1528 dbc->dev = dev;
1529 dbc->driver = driver;
1530 dbc->idProduct = DBC_PRODUCT_ID;
1531 dbc->idVendor = DBC_VENDOR_ID;
1532 dbc->bcdDevice = DBC_DEVICE_REV;
1533 dbc->bInterfaceProtocol = DBC_PROTOCOL;
1534 dbc->poll_interval = DBC_POLL_INTERVAL_DEFAULT;
1535
1536 /* initialize serial, product and manufacturer with default values */
1537 dbc->str = dbc_str_default;
1538
1539 if (readl(&dbc->regs->control) & DBC_CTRL_DBC_ENABLE)
1540 goto err;
1541
1542 INIT_DELAYED_WORK(&dbc->event_work, xhci_dbc_handle_events);
1543 spin_lock_init(&dbc->lock);
1544 mutex_init(&dbc->enable_mutex);
1545
1546 ret = sysfs_create_groups(&dev->kobj, dbc_dev_groups);
1547 if (ret)
1548 goto err;
1549
1550 return dbc;
1551 err:
1552 kfree(dbc);
1553 return NULL;
1554 }
1555
1556 /* undo what xhci_alloc_dbc() did */
xhci_dbc_remove(struct xhci_dbc * dbc)1557 void xhci_dbc_remove(struct xhci_dbc *dbc)
1558 {
1559 if (!dbc)
1560 return;
1561 /* stop hw, stop wq and call dbc->ops->stop() */
1562 mutex_lock(&dbc->enable_mutex);
1563 xhci_dbc_stop(dbc);
1564 mutex_unlock(&dbc->enable_mutex);
1565 /* remove sysfs files */
1566 sysfs_remove_groups(&dbc->dev->kobj, dbc_dev_groups);
1567
1568 kfree(dbc);
1569 }
1570
1571
xhci_create_dbc_dev(struct xhci_hcd * xhci)1572 int xhci_create_dbc_dev(struct xhci_hcd *xhci)
1573 {
1574 struct device *dev;
1575 void __iomem *base;
1576 int ret;
1577 int dbc_cap_offs;
1578
1579 /* create all parameters needed resembling a dbc device */
1580 dev = xhci_to_hcd(xhci)->self.controller;
1581 base = &xhci->cap_regs->hc_capbase;
1582
1583 dbc_cap_offs = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_DEBUG);
1584 if (!dbc_cap_offs)
1585 return -ENODEV;
1586
1587 /* already allocated and in use */
1588 if (xhci->dbc)
1589 return -EBUSY;
1590
1591 ret = xhci_dbc_tty_probe(dev, base + dbc_cap_offs, xhci);
1592
1593 return ret;
1594 }
1595
xhci_remove_dbc_dev(struct xhci_hcd * xhci)1596 void xhci_remove_dbc_dev(struct xhci_hcd *xhci)
1597 {
1598 unsigned long flags;
1599
1600 if (!xhci->dbc)
1601 return;
1602
1603 xhci_dbc_tty_remove(xhci->dbc);
1604 spin_lock_irqsave(&xhci->lock, flags);
1605 xhci->dbc = NULL;
1606 spin_unlock_irqrestore(&xhci->lock, flags);
1607 }
1608
1609 #ifdef CONFIG_PM
xhci_dbc_suspend(struct xhci_hcd * xhci)1610 int xhci_dbc_suspend(struct xhci_hcd *xhci)
1611 {
1612 struct xhci_dbc *dbc = xhci->dbc;
1613
1614 if (!dbc)
1615 return 0;
1616
1617 mutex_lock(&dbc->enable_mutex);
1618
1619 switch (dbc->state) {
1620 case DS_ENABLED:
1621 case DS_CONNECTED:
1622 case DS_CONFIGURED:
1623 dbc->resume_required = 1;
1624 break;
1625 default:
1626 break;
1627 }
1628
1629 xhci_dbc_stop(dbc);
1630 mutex_unlock(&dbc->enable_mutex);
1631
1632 return 0;
1633 }
1634
xhci_dbc_resume(struct xhci_hcd * xhci)1635 int xhci_dbc_resume(struct xhci_hcd *xhci)
1636 {
1637 int ret = 0;
1638 struct xhci_dbc *dbc = xhci->dbc;
1639
1640 if (!dbc)
1641 return 0;
1642
1643 mutex_lock(&dbc->enable_mutex);
1644
1645 if (dbc->resume_required) {
1646 dbc->resume_required = 0;
1647 xhci_dbc_start(dbc);
1648 }
1649
1650 mutex_unlock(&dbc->enable_mutex);
1651
1652 return ret;
1653 }
1654 #endif /* CONFIG_PM */
1655
xhci_dbc_init(void)1656 int xhci_dbc_init(void)
1657 {
1658 return dbc_tty_init();
1659 }
1660
xhci_dbc_exit(void)1661 void xhci_dbc_exit(void)
1662 {
1663 dbc_tty_exit();
1664 }
1665