trb_in_td() has the task of checking whether a given TRB belongs to a
given TD. Currently implementation is far too complex and used wrongly
in some places.

Let's simplify the implementation to the point that we can assume it to
be working always, this means we can remove xhci_test_trb_in_td() and
xhci_check_trb_in_td_math() because we can assume those will always
work.

While at that, also remove two calls for trb_in_td() which existed for
the sole purpose of printing out debugging messages!!

Signed-off-by: Felipe Balbi <felipe.ba...@linux.intel.com>
---
 drivers/usb/host/xhci-mem.c  | 160 -------------------------------------------
 drivers/usb/host/xhci-ring.c |  67 ++++--------------
 drivers/usb/host/xhci.h      |   5 +-
 3 files changed, 15 insertions(+), 217 deletions(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index e5ead94847e1..9ec426d2ed10 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -1890,163 +1890,6 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
        xhci->bus_state[1].bus_suspended = 0;
 }
 
-static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
-               struct xhci_segment *input_seg,
-               union xhci_trb *start_trb,
-               union xhci_trb *end_trb,
-               dma_addr_t input_dma,
-               struct xhci_segment *result_seg,
-               char *test_name, int test_number)
-{
-       unsigned long long start_dma;
-       unsigned long long end_dma;
-       struct xhci_segment *seg;
-
-       start_dma = xhci_trb_virt_to_dma(input_seg, start_trb);
-       end_dma = xhci_trb_virt_to_dma(input_seg, end_trb);
-
-       seg = trb_in_td(xhci, input_seg, start_trb, end_trb, input_dma);
-       if (seg != result_seg) {
-               xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n",
-                               test_name, test_number);
-               xhci_warn(xhci, "Tested TRB math w/ seg %p and "
-                               "input DMA 0x%llx\n",
-                               input_seg,
-                               (unsigned long long) input_dma);
-               xhci_warn(xhci, "starting TRB %p (0x%llx DMA), "
-                               "ending TRB %p (0x%llx DMA)\n",
-                               start_trb, start_dma,
-                               end_trb, end_dma);
-               xhci_warn(xhci, "Expected seg %p, got seg %p\n",
-                               result_seg, seg);
-               trb_in_td(xhci, input_seg, start_trb, end_trb, input_dma);
-               return -1;
-       }
-       return 0;
-}
-
-/* TRB math checks for xhci_trb_in_td(), using the command and event rings. */
-static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci)
-{
-       struct {
-               dma_addr_t              input_dma;
-               struct xhci_segment     *result_seg;
-       } simple_test_vector [] = {
-               /* A zeroed DMA field should fail */
-               { 0, NULL },
-               /* One TRB before the ring start should fail */
-               { xhci->event_ring->first_seg->dma - 16, NULL },
-               /* One byte before the ring start should fail */
-               { xhci->event_ring->first_seg->dma - 1, NULL },
-               /* Starting TRB should succeed */
-               { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg 
},
-               /* Ending TRB should succeed */
-               { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16,
-                       xhci->event_ring->first_seg },
-               /* One byte after the ring end should fail */
-               { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 
+ 1, NULL },
-               /* One TRB after the ring end should fail */
-               { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, 
NULL },
-               /* An address of all ones should fail */
-               { (dma_addr_t) (~0), NULL },
-       };
-       struct {
-               struct xhci_segment     *input_seg;
-               union xhci_trb          *start_trb;
-               union xhci_trb          *end_trb;
-               dma_addr_t              input_dma;
-               struct xhci_segment     *result_seg;
-       } complex_test_vector [] = {
-               /* Test feeding a valid DMA address from a different ring */
-               {       .input_seg = xhci->event_ring->first_seg,
-                       .start_trb = xhci->event_ring->first_seg->trbs,
-                       .end_trb = 
&xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
-                       .input_dma = xhci->cmd_ring->first_seg->dma,
-                       .result_seg = NULL,
-               },
-               /* Test feeding a valid end TRB from a different ring */
-               {       .input_seg = xhci->event_ring->first_seg,
-                       .start_trb = xhci->event_ring->first_seg->trbs,
-                       .end_trb = 
&xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
-                       .input_dma = xhci->cmd_ring->first_seg->dma,
-                       .result_seg = NULL,
-               },
-               /* Test feeding a valid start and end TRB from a different ring 
*/
-               {       .input_seg = xhci->event_ring->first_seg,
-                       .start_trb = xhci->cmd_ring->first_seg->trbs,
-                       .end_trb = 
&xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
-                       .input_dma = xhci->cmd_ring->first_seg->dma,
-                       .result_seg = NULL,
-               },
-               /* TRB in this ring, but after this TD */
-               {       .input_seg = xhci->event_ring->first_seg,
-                       .start_trb = &xhci->event_ring->first_seg->trbs[0],
-                       .end_trb = &xhci->event_ring->first_seg->trbs[3],
-                       .input_dma = xhci->event_ring->first_seg->dma + 4*16,
-                       .result_seg = NULL,
-               },
-               /* TRB in this ring, but before this TD */
-               {       .input_seg = xhci->event_ring->first_seg,
-                       .start_trb = &xhci->event_ring->first_seg->trbs[3],
-                       .end_trb = &xhci->event_ring->first_seg->trbs[6],
-                       .input_dma = xhci->event_ring->first_seg->dma + 2*16,
-                       .result_seg = NULL,
-               },
-               /* TRB in this ring, but after this wrapped TD */
-               {       .input_seg = xhci->event_ring->first_seg,
-                       .start_trb = 
&xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
-                       .end_trb = &xhci->event_ring->first_seg->trbs[1],
-                       .input_dma = xhci->event_ring->first_seg->dma + 2*16,
-                       .result_seg = NULL,
-               },
-               /* TRB in this ring, but before this wrapped TD */
-               {       .input_seg = xhci->event_ring->first_seg,
-                       .start_trb = 
&xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
-                       .end_trb = &xhci->event_ring->first_seg->trbs[1],
-                       .input_dma = xhci->event_ring->first_seg->dma + 
(TRBS_PER_SEGMENT - 4)*16,
-                       .result_seg = NULL,
-               },
-               /* TRB not in this ring, and we have a wrapped TD */
-               {       .input_seg = xhci->event_ring->first_seg,
-                       .start_trb = 
&xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
-                       .end_trb = &xhci->event_ring->first_seg->trbs[1],
-                       .input_dma = xhci->cmd_ring->first_seg->dma + 2*16,
-                       .result_seg = NULL,
-               },
-       };
-
-       unsigned int num_tests;
-       int i, ret;
-
-       num_tests = ARRAY_SIZE(simple_test_vector);
-       for (i = 0; i < num_tests; i++) {
-               ret = xhci_test_trb_in_td(xhci,
-                               xhci->event_ring->first_seg,
-                               xhci->event_ring->first_seg->trbs,
-                               
&xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
-                               simple_test_vector[i].input_dma,
-                               simple_test_vector[i].result_seg,
-                               "Simple", i);
-               if (ret < 0)
-                       return ret;
-       }
-
-       num_tests = ARRAY_SIZE(complex_test_vector);
-       for (i = 0; i < num_tests; i++) {
-               ret = xhci_test_trb_in_td(xhci,
-                               complex_test_vector[i].input_seg,
-                               complex_test_vector[i].start_trb,
-                               complex_test_vector[i].end_trb,
-                               complex_test_vector[i].input_dma,
-                               complex_test_vector[i].result_seg,
-                               "Complex", i);
-               if (ret < 0)
-                       return ret;
-       }
-       xhci_dbg(xhci, "TRB math tests passed.\n");
-       return 0;
-}
-
 static void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
 {
        u64 temp;
@@ -2473,9 +2316,6 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
                                        0, flags);
        if (!xhci->event_ring)
                goto fail;
-       if (xhci_check_trb_in_td_math(xhci) < 0)
-               goto fail;
-
        xhci->erst.entries = dma_alloc_coherent(dev,
                        sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS, &dma,
                        flags);
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 973182ee6954..266feabff11a 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -1684,60 +1684,23 @@ static void handle_port_status(struct xhci_hcd *xhci,
  * returns 0.
  */
 struct xhci_segment *trb_in_td(struct xhci_hcd *xhci,
-               struct xhci_segment *start_seg,
-               union xhci_trb  *start_trb,
-               union xhci_trb  *end_trb,
-               dma_addr_t      suspect_dma)
+               struct xhci_td *td, dma_addr_t suspect_dma)
 {
-       dma_addr_t start_dma;
-       dma_addr_t end_seg_dma;
-       dma_addr_t end_trb_dma;
-       struct xhci_segment *cur_seg;
-
-       start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
-       cur_seg = start_seg;
+       struct xhci_segment *cur_seg = td->start_seg;
 
        do {
-               if (start_dma == 0)
-                       return NULL;
-               /* We may get an event for a Link TRB in the middle of a TD */
-               end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
+               dma_addr_t start_dma;
+               dma_addr_t end_dma;
+
+               start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
+               end_dma = xhci_trb_virt_to_dma(cur_seg,
                                &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
-               /* If the end TRB isn't in this segment, this is set to 0 */
-               end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
-
-               xhci_dbg(xhci,
-                               "Looking for event-dma %016llx trb-start 
%016llx trb-end %016llx seg-start %016llx seg-end %016llx\n",
-                               (unsigned long long)suspect_dma,
-                               (unsigned long long)start_dma,
-                               (unsigned long long)end_trb_dma,
-                               (unsigned long long)cur_seg->dma,
-                               (unsigned long long)end_seg_dma);
-
-               if (end_trb_dma > 0) {
-                       /* The end TRB is in this segment, so suspect should be 
here */
-                       if (start_dma <= end_trb_dma) {
-                               if (suspect_dma >= start_dma && suspect_dma <= 
end_trb_dma)
-                                       return cur_seg;
-                       } else {
-                               /* Case for one segment with
-                                * a TD wrapped around to the top
-                                */
-                               if ((suspect_dma >= start_dma &&
-                                                       suspect_dma <= 
end_seg_dma) ||
-                                               (suspect_dma >= cur_seg->dma &&
-                                                suspect_dma <= end_trb_dma))
-                                       return cur_seg;
-                       }
-                       return NULL;
-               } else {
-                       /* Might still be somewhere in this segment */
-                       if (suspect_dma >= start_dma && suspect_dma <= 
end_seg_dma)
-                               return cur_seg;
-               }
+
+               if (suspect_dma >= start_dma && suspect_dma <= end_dma)
+                       return cur_seg;
+
                cur_seg = cur_seg->next;
-               start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
-       } while (cur_seg != start_seg);
+       } while (cur_seg != td->start_seg);
 
        return NULL;
 }
@@ -2352,8 +2315,7 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                        td_num--;
 
                /* Is this a TRB in the currently executing TD? */
-               ep_seg = trb_in_td(xhci, ep_ring->deq_seg, ep_ring->dequeue,
-                               td->last_trb, ep_trb_dma);
+               ep_seg = trb_in_td(xhci, td, ep_trb_dma);
 
                /*
                 * Skip the Force Stopped Event. The event_trb(event_dma) of FSE
@@ -2386,9 +2348,6 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                                        "part of current TD ep_index %d "
                                        "comp_code %u\n", ep_index,
                                        trb_comp_code);
-                               trb_in_td(xhci, ep_ring->deq_seg,
-                                         ep_ring->dequeue, td->last_trb,
-                                         ep_trb_dma);
                                return -ESHUTDOWN;
                        }
 
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 3320b9c240a9..8fc77bea8580 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -1856,9 +1856,8 @@ void xhci_reset_bandwidth(struct usb_hcd *hcd, struct 
usb_device *udev);
 
 /* xHCI ring, segment, TRB, and TD functions */
 dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, union xhci_trb *trb);
-struct xhci_segment *trb_in_td(struct xhci_hcd *xhci,
-               struct xhci_segment *start_seg, union xhci_trb *start_trb,
-               union xhci_trb *end_trb, dma_addr_t suspect_dma);
+struct xhci_segment *trb_in_td(struct xhci_hcd *xhci, struct xhci_td *td,
+               dma_addr_t suspect_dma);
 int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int 
trb_comp_code);
 void xhci_ring_cmd_db(struct xhci_hcd *xhci);
 int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd,
-- 
2.10.1

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to