2
0

combined-packet.c 6.4 KB

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