The number of trbs depends on the number of 64k boundaries crossed. Using a common function saves there being any typos in the 3 places it is used. Signed-off-by: David Laight <david.laight@xxxxxxxxxx> --- drivers/usb/host/xhci-ring.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c index 52ea564..9e523c5 100644 --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -73,6 +73,15 @@ static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev, struct xhci_event_cmd *event); +/* Return the number of TRBs needed for a single buffer. */ +static unsigned int num_trbs_for_buf(u64 addr, unsigned int len) +{ + unsigned int offset = addr & (TRB_MAX_BUFF_SIZE - 1); + + /* Accounts for 64k boundaries. If len is zero may return 0 or 1 */ + return DIV_ROUND_UP(len + offset, TRB_MAX_BUFF_SIZE); +} + /* * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA * address of the TRB. @@ -3151,8 +3160,7 @@ static unsigned int count_sg_trbs_needed(struct xhci_ring *ep_ring, /* 1 TRB + 1 for each 64k boundary crossed */ len = min_t(unsigned int, sg_dma_len(sg), len_left); - num_trbs += DIV_ROUND_UP(len + (sg_dma_address(sg) & - (TRB_MAX_BUFF_SIZE - 1)), TRB_MAX_BUFF_SIZE); + num_trbs += num_trbs_for_buf(sg_dma_address(sg), len); len_left -= len; if (len_left == 0) @@ -3295,8 +3303,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, sg = NULL; addr = urb->transfer_dma; this_sg_len = urb->transfer_buffer_length; - num_trbs = DIV_ROUND_UP((addr & (TRB_MAX_BUFF_SIZE - 1)) + - this_sg_len, TRB_MAX_BUFF_SIZE); + num_trbs = num_trbs_for_buf(addr, this_sg_len); if (num_trbs == 0) num_trbs = 1; } @@ -3555,8 +3562,7 @@ static int count_isoc_trbs_needed(struct xhci_hcd *xhci, addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset); td_len = urb->iso_frame_desc[i].length; - num_trbs = DIV_ROUND_UP(td_len + (addr & (TRB_MAX_BUFF_SIZE - 1)), - TRB_MAX_BUFF_SIZE); + num_trbs = num_trbs_for_buf(addr, td_len); if (num_trbs == 0) num_trbs++; -- 1.8.1.2 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html