static int xhci_ring_trb_show(struct seq_file *s, void *unused)
 {
-       int                     i;
        struct xhci_ring        *ring = *(struct xhci_ring **)s->private;
        struct xhci_segment     *seg = ring->first_seg;
 
-       for (i = 0; i < ring->num_segs; i++) {
+       xhci_for_each_ring_seg(ring->first_seg, seg)
                xhci_ring_dump_segment(s, seg);
-               seg = seg->next;
-       }
 
        return 0;
 }
 
                struct radix_tree_root *trb_address_map,
                struct xhci_ring *ring,
                struct xhci_segment *first_seg,
-               struct xhci_segment *last_seg,
                gfp_t mem_flags)
 {
        struct xhci_segment *seg;
        if (WARN_ON_ONCE(trb_address_map == NULL))
                return 0;
 
-       seg = first_seg;
-       do {
+       xhci_for_each_ring_seg(first_seg, seg) {
                ret = xhci_insert_segment_mapping(trb_address_map,
                                ring, seg, mem_flags);
                if (ret)
                        goto remove_streams;
-               if (seg == last_seg)
-                       return 0;
-               seg = seg->next;
-       } while (seg != first_seg);
+       }
 
        return 0;
 
 remove_streams:
        failed_seg = seg;
-       seg = first_seg;
-       do {
+       xhci_for_each_ring_seg(first_seg, seg) {
                xhci_remove_segment_mapping(trb_address_map, seg);
                if (seg == failed_seg)
                        return ret;
-               seg = seg->next;
-       } while (seg != first_seg);
+       }
 
        return ret;
 }
        if (WARN_ON_ONCE(ring->trb_address_map == NULL))
                return;
 
-       seg = ring->first_seg;
-       do {
+       xhci_for_each_ring_seg(ring->first_seg, seg)
                xhci_remove_segment_mapping(ring->trb_address_map, seg);
-               seg = seg->next;
-       } while (seg != ring->first_seg);
 }
 
 static int xhci_update_stream_mapping(struct xhci_ring *ring, gfp_t mem_flags)
 {
        return xhci_update_stream_segment_mapping(ring->trb_address_map, ring,
-                       ring->first_seg, ring->last_seg, mem_flags);
+                       ring->first_seg, mem_flags);
 }
 
 /* XXX: Do we need the hcd structure in all these functions? */
 
        if (ring->type == TYPE_STREAM) {
                ret = xhci_update_stream_segment_mapping(ring->trb_address_map,
-                                               ring, first, last, flags);
+                                               ring, first, flags);
                if (ret)
                        goto free_segments;
        }
 
 
 static bool td_on_ring(struct xhci_td *td, struct xhci_ring *ring)
 {
-       struct xhci_segment *seg = ring->first_seg;
+       struct xhci_segment *seg;
 
        if (!td || !td->start_seg)
                return false;
-       do {
+
+       xhci_for_each_ring_seg(ring->first_seg, seg) {
                if (seg == td->start_seg)
                        return true;
-               seg = seg->next;
-       } while (seg && seg != ring->first_seg);
+       }
 
        return false;
 }
        struct xhci_segment *seg;
 
        ring = xhci->cmd_ring;
-       seg = ring->deq_seg;
-       do {
-               memset(seg->trbs, 0,
-                       sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
-               seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
-                       cpu_to_le32(~TRB_CYCLE);
-               seg = seg->next;
-       } while (seg != ring->deq_seg);
+       xhci_for_each_ring_seg(ring->deq_seg, seg) {
+               memset(seg->trbs, 0, sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
+               seg->trbs[TRBS_PER_SEGMENT - 1].link.control &= cpu_to_le32(~TRB_CYCLE);
+       }
 
        xhci_initialize_ring_info(ring, 1);
        /*