Skip to content

Commit 8e51adc

Browse files
Andiry Xugregkh
authored andcommitted
USB: xHCI: Introduce urb_priv structure
Add urb_priv data structure to xHCI driver. This structure allows multiple xhci TDs to be linked to one urb, which is essential for isochronous transfer. For non-isochronous urb, only one TD is needed for one urb; for isochronous urb, the TD number for the urb is equal to urb->number_of_packets. The length field of urb_priv indicates the number of TDs in the urb. The td_cnt field indicates the number of TDs already processed by xHC. When td_cnt matches length, the urb can be given back to usbcore. When an urb is dequeued or cancelled, add all the unprocessed TDs to the endpoint's cancelled_td_list. When process a cancelled TD, increase td_cnt field. When td_cnt equals urb_priv->length, giveback the cancelled urb. Signed-off-by: Andiry Xu <[email protected]> Signed-off-by: Sarah Sharp <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent d18240d commit 8e51adc

File tree

4 files changed

+125
-34
lines changed

4 files changed

+125
-34
lines changed

drivers/usb/host/xhci-mem.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,22 @@ struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
13901390
return command;
13911391
}
13921392

1393+
void xhci_urb_free_priv(struct xhci_hcd *xhci, struct urb_priv *urb_priv)
1394+
{
1395+
int last;
1396+
1397+
if (!urb_priv)
1398+
return;
1399+
1400+
last = urb_priv->length - 1;
1401+
if (last >= 0) {
1402+
int i;
1403+
for (i = 0; i <= last; i++)
1404+
kfree(urb_priv->td[i]);
1405+
}
1406+
kfree(urb_priv);
1407+
}
1408+
13931409
void xhci_free_command(struct xhci_hcd *xhci,
13941410
struct xhci_command *command)
13951411
{

drivers/usb/host/xhci-ring.c

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -578,16 +578,24 @@ static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
578578
struct xhci_td *cur_td, int status, char *adjective)
579579
{
580580
struct usb_hcd *hcd = xhci_to_hcd(xhci);
581+
struct urb *urb;
582+
struct urb_priv *urb_priv;
581583

582-
cur_td->urb->hcpriv = NULL;
583-
usb_hcd_unlink_urb_from_ep(hcd, cur_td->urb);
584-
xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, cur_td->urb);
584+
urb = cur_td->urb;
585+
urb_priv = urb->hcpriv;
586+
urb_priv->td_cnt++;
585587

586-
spin_unlock(&xhci->lock);
587-
usb_hcd_giveback_urb(hcd, cur_td->urb, status);
588-
kfree(cur_td);
589-
spin_lock(&xhci->lock);
590-
xhci_dbg(xhci, "%s URB given back\n", adjective);
588+
/* Only giveback urb when this is the last td in urb */
589+
if (urb_priv->td_cnt == urb_priv->length) {
590+
usb_hcd_unlink_urb_from_ep(hcd, urb);
591+
xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, urb);
592+
593+
spin_unlock(&xhci->lock);
594+
usb_hcd_giveback_urb(hcd, urb, status);
595+
xhci_urb_free_priv(xhci, urb_priv);
596+
spin_lock(&xhci->lock);
597+
xhci_dbg(xhci, "%s URB given back\n", adjective);
598+
}
591599
}
592600

593601
/*
@@ -1272,6 +1280,7 @@ static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
12721280
struct urb *urb = NULL;
12731281
struct xhci_ep_ctx *ep_ctx;
12741282
int ret = 0;
1283+
struct urb_priv *urb_priv;
12751284
u32 trb_comp_code;
12761285

12771286
slot_id = TRB_TO_SLOT_ID(event->flags);
@@ -1325,6 +1334,7 @@ static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
13251334
td_cleanup:
13261335
/* Clean up the endpoint's TD list */
13271336
urb = td->urb;
1337+
urb_priv = urb->hcpriv;
13281338

13291339
/* Do one last check of the actual transfer length.
13301340
* If the host controller said we transferred more data than
@@ -1349,7 +1359,10 @@ static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
13491359
if (!list_empty(&td->cancelled_td_list))
13501360
list_del(&td->cancelled_td_list);
13511361

1352-
ret = 1;
1362+
urb_priv->td_cnt++;
1363+
/* Giveback the urb when all the tds are completed */
1364+
if (urb_priv->td_cnt == urb_priv->length)
1365+
ret = 1;
13531366
}
13541367

13551368
return ret;
@@ -1588,6 +1601,7 @@ static int handle_tx_event(struct xhci_hcd *xhci,
15881601
union xhci_trb *event_trb;
15891602
struct urb *urb = NULL;
15901603
int status = -EINPROGRESS;
1604+
struct urb_priv *urb_priv;
15911605
struct xhci_ep_ctx *ep_ctx;
15921606
u32 trb_comp_code;
15931607
int ret = 0;
@@ -1770,6 +1784,7 @@ static int handle_tx_event(struct xhci_hcd *xhci,
17701784

17711785
if (ret) {
17721786
urb = td->urb;
1787+
urb_priv = urb->hcpriv;
17731788
/* Leave the TD around for the reset endpoint function
17741789
* to use(but only if it's not a control endpoint,
17751790
* since we already queued the Set TR dequeue pointer
@@ -1778,7 +1793,7 @@ static int handle_tx_event(struct xhci_hcd *xhci,
17781793
if (usb_endpoint_xfer_control(&urb->ep->desc) ||
17791794
(trb_comp_code != COMP_STALL &&
17801795
trb_comp_code != COMP_BABBLE))
1781-
kfree(td);
1796+
xhci_urb_free_priv(xhci, urb_priv);
17821797

17831798
usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
17841799
xhci_dbg(xhci, "Giveback URB %p, len = %d, "
@@ -1979,10 +1994,12 @@ static int prepare_transfer(struct xhci_hcd *xhci,
19791994
unsigned int stream_id,
19801995
unsigned int num_trbs,
19811996
struct urb *urb,
1982-
struct xhci_td **td,
1997+
unsigned int td_index,
19831998
gfp_t mem_flags)
19841999
{
19852000
int ret;
2001+
struct urb_priv *urb_priv;
2002+
struct xhci_td *td;
19862003
struct xhci_ring *ep_ring;
19872004
struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
19882005

@@ -1998,24 +2015,29 @@ static int prepare_transfer(struct xhci_hcd *xhci,
19982015
num_trbs, mem_flags);
19992016
if (ret)
20002017
return ret;
2001-
*td = kzalloc(sizeof(struct xhci_td), mem_flags);
2002-
if (!*td)
2003-
return -ENOMEM;
2004-
INIT_LIST_HEAD(&(*td)->td_list);
2005-
INIT_LIST_HEAD(&(*td)->cancelled_td_list);
20062018

2007-
ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
2008-
if (unlikely(ret)) {
2009-
kfree(*td);
2010-
return ret;
2019+
urb_priv = urb->hcpriv;
2020+
td = urb_priv->td[td_index];
2021+
2022+
INIT_LIST_HEAD(&td->td_list);
2023+
INIT_LIST_HEAD(&td->cancelled_td_list);
2024+
2025+
if (td_index == 0) {
2026+
ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
2027+
if (unlikely(ret)) {
2028+
xhci_urb_free_priv(xhci, urb_priv);
2029+
urb->hcpriv = NULL;
2030+
return ret;
2031+
}
20112032
}
20122033

2013-
(*td)->urb = urb;
2014-
urb->hcpriv = (void *) (*td);
2034+
td->urb = urb;
20152035
/* Add this TD to the tail of the endpoint ring's TD list */
2016-
list_add_tail(&(*td)->td_list, &ep_ring->td_list);
2017-
(*td)->start_seg = ep_ring->enq_seg;
2018-
(*td)->first_trb = ep_ring->enqueue;
2036+
list_add_tail(&td->td_list, &ep_ring->td_list);
2037+
td->start_seg = ep_ring->enq_seg;
2038+
td->first_trb = ep_ring->enqueue;
2039+
2040+
urb_priv->td[td_index] = td;
20192041

20202042
return 0;
20212043
}
@@ -2154,6 +2176,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
21542176
{
21552177
struct xhci_ring *ep_ring;
21562178
unsigned int num_trbs;
2179+
struct urb_priv *urb_priv;
21572180
struct xhci_td *td;
21582181
struct scatterlist *sg;
21592182
int num_sgs;
@@ -2174,9 +2197,13 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
21742197

21752198
trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
21762199
ep_index, urb->stream_id,
2177-
num_trbs, urb, &td, mem_flags);
2200+
num_trbs, urb, 0, mem_flags);
21782201
if (trb_buff_len < 0)
21792202
return trb_buff_len;
2203+
2204+
urb_priv = urb->hcpriv;
2205+
td = urb_priv->td[0];
2206+
21802207
/*
21812208
* Don't give the first TRB to the hardware (by toggling the cycle bit)
21822209
* until we've finished creating all the other TRBs. The ring's cycle
@@ -2297,6 +2324,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
22972324
struct urb *urb, int slot_id, unsigned int ep_index)
22982325
{
22992326
struct xhci_ring *ep_ring;
2327+
struct urb_priv *urb_priv;
23002328
struct xhci_td *td;
23012329
int num_trbs;
23022330
struct xhci_generic_trb *start_trb;
@@ -2342,10 +2370,13 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
23422370

23432371
ret = prepare_transfer(xhci, xhci->devs[slot_id],
23442372
ep_index, urb->stream_id,
2345-
num_trbs, urb, &td, mem_flags);
2373+
num_trbs, urb, 0, mem_flags);
23462374
if (ret < 0)
23472375
return ret;
23482376

2377+
urb_priv = urb->hcpriv;
2378+
td = urb_priv->td[0];
2379+
23492380
/*
23502381
* Don't give the first TRB to the hardware (by toggling the cycle bit)
23512382
* until we've finished creating all the other TRBs. The ring's cycle
@@ -2431,6 +2462,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
24312462
struct xhci_generic_trb *start_trb;
24322463
int start_cycle;
24332464
u32 field, length_field;
2465+
struct urb_priv *urb_priv;
24342466
struct xhci_td *td;
24352467

24362468
ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
@@ -2458,10 +2490,13 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
24582490
num_trbs++;
24592491
ret = prepare_transfer(xhci, xhci->devs[slot_id],
24602492
ep_index, urb->stream_id,
2461-
num_trbs, urb, &td, mem_flags);
2493+
num_trbs, urb, 0, mem_flags);
24622494
if (ret < 0)
24632495
return ret;
24642496

2497+
urb_priv = urb->hcpriv;
2498+
td = urb_priv->td[0];
2499+
24652500
/*
24662501
* Don't give the first TRB to the hardware (by toggling the cycle bit)
24672502
* until we've finished creating all the other TRBs. The ring's cycle

drivers/usb/host/xhci.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,8 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
804804
unsigned long flags;
805805
int ret = 0;
806806
unsigned int slot_id, ep_index;
807-
807+
struct urb_priv *urb_priv;
808+
int size, i;
808809

809810
if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
810811
return -EINVAL;
@@ -824,6 +825,30 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
824825
ret = -ESHUTDOWN;
825826
goto exit;
826827
}
828+
829+
if (usb_endpoint_xfer_isoc(&urb->ep->desc))
830+
size = urb->number_of_packets;
831+
else
832+
size = 1;
833+
834+
urb_priv = kzalloc(sizeof(struct urb_priv) +
835+
size * sizeof(struct xhci_td *), mem_flags);
836+
if (!urb_priv)
837+
return -ENOMEM;
838+
839+
for (i = 0; i < size; i++) {
840+
urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags);
841+
if (!urb_priv->td[i]) {
842+
urb_priv->length = i;
843+
xhci_urb_free_priv(xhci, urb_priv);
844+
return -ENOMEM;
845+
}
846+
}
847+
848+
urb_priv->length = size;
849+
urb_priv->td_cnt = 0;
850+
urb->hcpriv = urb_priv;
851+
827852
if (usb_endpoint_xfer_control(&urb->ep->desc)) {
828853
/* Check to see if the max packet size for the default control
829854
* endpoint changed during FS device enumeration
@@ -877,6 +902,8 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
877902
exit:
878903
return ret;
879904
dying:
905+
xhci_urb_free_priv(xhci, urb_priv);
906+
urb->hcpriv = NULL;
880907
xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
881908
"non-responsive xHCI host.\n",
882909
urb->ep->desc.bEndpointAddress, urb);
@@ -918,9 +945,10 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
918945
int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
919946
{
920947
unsigned long flags;
921-
int ret;
948+
int ret, i;
922949
u32 temp;
923950
struct xhci_hcd *xhci;
951+
struct urb_priv *urb_priv;
924952
struct xhci_td *td;
925953
unsigned int ep_index;
926954
struct xhci_ring *ep_ring;
@@ -935,12 +963,12 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
935963
temp = xhci_readl(xhci, &xhci->op_regs->status);
936964
if (temp == 0xffffffff) {
937965
xhci_dbg(xhci, "HW died, freeing TD.\n");
938-
td = (struct xhci_td *) urb->hcpriv;
966+
urb_priv = urb->hcpriv;
939967

940968
usb_hcd_unlink_urb_from_ep(hcd, urb);
941969
spin_unlock_irqrestore(&xhci->lock, flags);
942970
usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN);
943-
kfree(td);
971+
xhci_urb_free_priv(xhci, urb_priv);
944972
return ret;
945973
}
946974
if (xhci->xhc_state & XHCI_STATE_DYING) {
@@ -968,9 +996,14 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
968996

969997
xhci_dbg(xhci, "Endpoint ring:\n");
970998
xhci_debug_ring(xhci, ep_ring);
971-
td = (struct xhci_td *) urb->hcpriv;
972999

973-
list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1000+
urb_priv = urb->hcpriv;
1001+
1002+
for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1003+
td = urb_priv->td[i];
1004+
list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1005+
}
1006+
9741007
/* Queue a stop endpoint command, but only if this is
9751008
* the first cancellation to be handled.
9761009
*/

drivers/usb/host/xhci.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,12 @@ struct xhci_scratchpad {
10901090
dma_addr_t *sp_dma_buffers;
10911091
};
10921092

1093+
struct urb_priv {
1094+
int length;
1095+
int td_cnt;
1096+
struct xhci_td *td[0];
1097+
};
1098+
10931099
/*
10941100
* Each segment table entry is 4*32bits long. 1K seems like an ok size:
10951101
* (1K bytes * 8bytes/bit) / (4*32 bits) = 64 segment entries in the table,
@@ -1347,6 +1353,7 @@ struct xhci_ring *xhci_stream_id_to_ring(
13471353
struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
13481354
bool allocate_in_ctx, bool allocate_completion,
13491355
gfp_t mem_flags);
1356+
void xhci_urb_free_priv(struct xhci_hcd *xhci, struct urb_priv *urb_priv);
13501357
void xhci_free_command(struct xhci_hcd *xhci,
13511358
struct xhci_command *command);
13521359

0 commit comments

Comments
 (0)