2
0

combined-packet.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * QEMU USB packet combining code (for input pipelining)
  3. *
  4. * Copyright(c) 2012 Red Hat, Inc.
  5. *
  6. * Red Hat Authors:
  7. * Hans de Goede <hdegoede@redhat.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or(at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "qemu-common.h"
  23. #include "hw/usb.h"
  24. #include "qemu/iov.h"
  25. #include "trace.h"
  26. static void usb_combined_packet_add(USBCombinedPacket *combined, USBPacket *p)
  27. {
  28. qemu_iovec_concat(&combined->iov, &p->iov, 0, p->iov.size);
  29. QTAILQ_INSERT_TAIL(&combined->packets, p, combined_entry);
  30. p->combined = combined;
  31. }
  32. /* Note will free combined when the last packet gets removed */
  33. static void usb_combined_packet_remove(USBCombinedPacket *combined,
  34. USBPacket *p)
  35. {
  36. assert(p->combined == combined);
  37. p->combined = NULL;
  38. QTAILQ_REMOVE(&combined->packets, p, combined_entry);
  39. if (QTAILQ_EMPTY(&combined->packets)) {
  40. g_free(combined);
  41. }
  42. }
  43. /* Also handles completion of non combined packets for pipelined input eps */
  44. void usb_combined_input_packet_complete(USBDevice *dev, USBPacket *p)
  45. {
  46. USBCombinedPacket *combined = p->combined;
  47. USBEndpoint *ep = p->ep;
  48. USBPacket *next;
  49. int status, actual_length;
  50. bool short_not_ok, done = false;
  51. if (combined == NULL) {
  52. usb_packet_complete_one(dev, p);
  53. goto leave;
  54. }
  55. assert(combined->first == p && p == QTAILQ_FIRST(&combined->packets));
  56. status = combined->first->status;
  57. actual_length = combined->first->actual_length;
  58. short_not_ok = QTAILQ_LAST(&combined->packets, packets_head)->short_not_ok;
  59. QTAILQ_FOREACH_SAFE(p, &combined->packets, combined_entry, next) {
  60. if (!done) {
  61. /* Distribute data over uncombined packets */
  62. if (actual_length >= p->iov.size) {
  63. p->actual_length = p->iov.size;
  64. } else {
  65. /* Send short or error packet to complete the transfer */
  66. p->actual_length = actual_length;
  67. done = true;
  68. }
  69. /* Report status on the last packet */
  70. if (done || next == NULL) {
  71. p->status = status;
  72. } else {
  73. p->status = USB_RET_SUCCESS;
  74. }
  75. p->short_not_ok = short_not_ok;
  76. /* Note will free combined when the last packet gets removed! */
  77. usb_combined_packet_remove(combined, p);
  78. usb_packet_complete_one(dev, p);
  79. actual_length -= p->actual_length;
  80. } else {
  81. /* Remove any leftover packets from the queue */
  82. p->status = USB_RET_REMOVE_FROM_QUEUE;
  83. /* Note will free combined on the last packet! */
  84. dev->port->ops->complete(dev->port, p);
  85. }
  86. }
  87. /* Do not use combined here, it has been freed! */
  88. leave:
  89. /* Check if there are packets in the queue waiting for our completion */
  90. usb_ep_combine_input_packets(ep);
  91. }
  92. /* May only be called for combined packets! */
  93. void usb_combined_packet_cancel(USBDevice *dev, USBPacket *p)
  94. {
  95. USBCombinedPacket *combined = p->combined;
  96. assert(combined != NULL);
  97. USBPacket *first = p->combined->first;
  98. /* Note will free combined on the last packet! */
  99. usb_combined_packet_remove(combined, p);
  100. if (p == first) {
  101. usb_device_cancel_packet(dev, p);
  102. }
  103. }
  104. /*
  105. * Large input transfers can get split into multiple input packets, this
  106. * function recombines them, removing the short_not_ok checks which all but
  107. * the last packet of such splits transfers have, thereby allowing input
  108. * transfer pipelining (which we cannot do on short_not_ok transfers)
  109. */
  110. void usb_ep_combine_input_packets(USBEndpoint *ep)
  111. {
  112. USBPacket *p, *u, *next, *prev = NULL, *first = NULL;
  113. USBPort *port = ep->dev->port;
  114. int totalsize;
  115. assert(ep->pipeline);
  116. assert(ep->pid == USB_TOKEN_IN);
  117. QTAILQ_FOREACH_SAFE(p, &ep->queue, queue, next) {
  118. /* Empty the queue on a halt */
  119. if (ep->halted) {
  120. p->status = USB_RET_REMOVE_FROM_QUEUE;
  121. port->ops->complete(port, p);
  122. continue;
  123. }
  124. /* Skip packets already submitted to the device */
  125. if (p->state == USB_PACKET_ASYNC) {
  126. prev = p;
  127. continue;
  128. }
  129. usb_packet_check_state(p, USB_PACKET_QUEUED);
  130. /*
  131. * If the previous (combined) packet has the short_not_ok flag set
  132. * stop, as we must not submit packets to the device after a transfer
  133. * ending with short_not_ok packet.
  134. */
  135. if (prev && prev->short_not_ok) {
  136. break;
  137. }
  138. if (first) {
  139. if (first->combined == NULL) {
  140. USBCombinedPacket *combined = g_new0(USBCombinedPacket, 1);
  141. combined->first = first;
  142. QTAILQ_INIT(&combined->packets);
  143. qemu_iovec_init(&combined->iov, 2);
  144. usb_combined_packet_add(combined, first);
  145. }
  146. usb_combined_packet_add(first->combined, p);
  147. } else {
  148. first = p;
  149. }
  150. /* Is this packet the last one of a (combined) transfer? */
  151. totalsize = (p->combined) ? p->combined->iov.size : p->iov.size;
  152. if ((p->iov.size % ep->max_packet_size) != 0 || !p->short_not_ok ||
  153. next == NULL ||
  154. /* Work around for Linux usbfs bulk splitting + migration */
  155. (totalsize == 16348 && p->int_req)) {
  156. usb_device_handle_data(ep->dev, first);
  157. assert(first->status == USB_RET_ASYNC);
  158. if (first->combined) {
  159. QTAILQ_FOREACH(u, &first->combined->packets, combined_entry) {
  160. usb_packet_set_state(u, USB_PACKET_ASYNC);
  161. }
  162. } else {
  163. usb_packet_set_state(first, USB_PACKET_ASYNC);
  164. }
  165. first = NULL;
  166. prev = p;
  167. }
  168. }
  169. }