vhci_rx.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  4. */
  5. #include <linux/kthread.h>
  6. #include <linux/slab.h>
  7. #include "usbip_common.h"
  8. #include "vhci.h"
  9. /* get URB from transmitted urb queue. caller must hold vdev->priv_lock */
  10. struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum)
  11. {
  12. struct vhci_priv *priv, *tmp;
  13. struct urb *urb = NULL;
  14. int status;
  15. list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
  16. if (priv->seqnum != seqnum)
  17. continue;
  18. urb = priv->urb;
  19. status = urb->status;
  20. usbip_dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
  21. urb, priv, seqnum);
  22. switch (status) {
  23. case -ENOENT:
  24. /* fall through */
  25. case -ECONNRESET:
  26. dev_info(&urb->dev->dev,
  27. "urb %p was unlinked %ssynchronuously.\n", urb,
  28. status == -ENOENT ? "" : "a");
  29. break;
  30. case -EINPROGRESS:
  31. /* no info output */
  32. break;
  33. default:
  34. dev_info(&urb->dev->dev,
  35. "urb %p may be in a error, status %d\n", urb,
  36. status);
  37. }
  38. list_del(&priv->list);
  39. kfree(priv);
  40. urb->hcpriv = NULL;
  41. break;
  42. }
  43. return urb;
  44. }
  45. static void vhci_recv_ret_submit(struct vhci_device *vdev,
  46. struct usbip_header *pdu)
  47. {
  48. struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
  49. struct vhci *vhci = vhci_hcd->vhci;
  50. struct usbip_device *ud = &vdev->ud;
  51. struct urb *urb;
  52. unsigned long flags;
  53. spin_lock_irqsave(&vdev->priv_lock, flags);
  54. urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
  55. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  56. if (!urb) {
  57. pr_err("cannot find a urb of seqnum %u\n", pdu->base.seqnum);
  58. pr_info("max seqnum %d\n",
  59. atomic_read(&vhci_hcd->seqnum));
  60. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  61. return;
  62. }
  63. /* unpack the pdu to a urb */
  64. usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
  65. /* recv transfer buffer */
  66. if (usbip_recv_xbuff(ud, urb) < 0)
  67. return;
  68. /* recv iso_packet_descriptor */
  69. if (usbip_recv_iso(ud, urb) < 0)
  70. return;
  71. /* restore the padding in iso packets */
  72. usbip_pad_iso(ud, urb);
  73. if (usbip_dbg_flag_vhci_rx)
  74. usbip_dump_urb(urb);
  75. usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
  76. spin_lock_irqsave(&vhci->lock, flags);
  77. usb_hcd_unlink_urb_from_ep(vhci_hcd_to_hcd(vhci_hcd), urb);
  78. spin_unlock_irqrestore(&vhci->lock, flags);
  79. usb_hcd_giveback_urb(vhci_hcd_to_hcd(vhci_hcd), urb, urb->status);
  80. usbip_dbg_vhci_rx("Leave\n");
  81. }
  82. static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
  83. struct usbip_header *pdu)
  84. {
  85. struct vhci_unlink *unlink, *tmp;
  86. unsigned long flags;
  87. spin_lock_irqsave(&vdev->priv_lock, flags);
  88. list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
  89. pr_info("unlink->seqnum %lu\n", unlink->seqnum);
  90. if (unlink->seqnum == pdu->base.seqnum) {
  91. usbip_dbg_vhci_rx("found pending unlink, %lu\n",
  92. unlink->seqnum);
  93. list_del(&unlink->list);
  94. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  95. return unlink;
  96. }
  97. }
  98. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  99. return NULL;
  100. }
  101. static void vhci_recv_ret_unlink(struct vhci_device *vdev,
  102. struct usbip_header *pdu)
  103. {
  104. struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
  105. struct vhci *vhci = vhci_hcd->vhci;
  106. struct vhci_unlink *unlink;
  107. struct urb *urb;
  108. unsigned long flags;
  109. usbip_dump_header(pdu);
  110. unlink = dequeue_pending_unlink(vdev, pdu);
  111. if (!unlink) {
  112. pr_info("cannot find the pending unlink %u\n",
  113. pdu->base.seqnum);
  114. return;
  115. }
  116. spin_lock_irqsave(&vdev->priv_lock, flags);
  117. urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
  118. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  119. if (!urb) {
  120. /*
  121. * I get the result of a unlink request. But, it seems that I
  122. * already received the result of its submit result and gave
  123. * back the URB.
  124. */
  125. pr_info("the urb (seqnum %d) was already given back\n",
  126. pdu->base.seqnum);
  127. } else {
  128. usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
  129. /* If unlink is successful, status is -ECONNRESET */
  130. urb->status = pdu->u.ret_unlink.status;
  131. pr_info("urb->status %d\n", urb->status);
  132. spin_lock_irqsave(&vhci->lock, flags);
  133. usb_hcd_unlink_urb_from_ep(vhci_hcd_to_hcd(vhci_hcd), urb);
  134. spin_unlock_irqrestore(&vhci->lock, flags);
  135. usb_hcd_giveback_urb(vhci_hcd_to_hcd(vhci_hcd), urb, urb->status);
  136. }
  137. kfree(unlink);
  138. }
  139. static int vhci_priv_tx_empty(struct vhci_device *vdev)
  140. {
  141. int empty = 0;
  142. unsigned long flags;
  143. spin_lock_irqsave(&vdev->priv_lock, flags);
  144. empty = list_empty(&vdev->priv_rx);
  145. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  146. return empty;
  147. }
  148. /* recv a pdu */
  149. static void vhci_rx_pdu(struct usbip_device *ud)
  150. {
  151. int ret;
  152. struct usbip_header pdu;
  153. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  154. usbip_dbg_vhci_rx("Enter\n");
  155. memset(&pdu, 0, sizeof(pdu));
  156. /* receive a pdu header */
  157. ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
  158. if (ret < 0) {
  159. if (ret == -ECONNRESET)
  160. pr_info("connection reset by peer\n");
  161. else if (ret == -EAGAIN) {
  162. /* ignore if connection was idle */
  163. if (vhci_priv_tx_empty(vdev))
  164. return;
  165. pr_info("connection timed out with pending urbs\n");
  166. } else if (ret != -ERESTARTSYS)
  167. pr_info("xmit failed %d\n", ret);
  168. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  169. return;
  170. }
  171. if (ret == 0) {
  172. pr_info("connection closed");
  173. usbip_event_add(ud, VDEV_EVENT_DOWN);
  174. return;
  175. }
  176. if (ret != sizeof(pdu)) {
  177. pr_err("received pdu size is %d, should be %d\n", ret,
  178. (unsigned int)sizeof(pdu));
  179. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  180. return;
  181. }
  182. usbip_header_correct_endian(&pdu, 0);
  183. if (usbip_dbg_flag_vhci_rx)
  184. usbip_dump_header(&pdu);
  185. switch (pdu.base.command) {
  186. case USBIP_RET_SUBMIT:
  187. vhci_recv_ret_submit(vdev, &pdu);
  188. break;
  189. case USBIP_RET_UNLINK:
  190. vhci_recv_ret_unlink(vdev, &pdu);
  191. break;
  192. default:
  193. /* NOT REACHED */
  194. pr_err("unknown pdu %u\n", pdu.base.command);
  195. usbip_dump_header(&pdu);
  196. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  197. break;
  198. }
  199. }
  200. int vhci_rx_loop(void *data)
  201. {
  202. struct usbip_device *ud = data;
  203. while (!kthread_should_stop()) {
  204. if (usbip_event_happened(ud))
  205. break;
  206. vhci_rx_pdu(ud);
  207. }
  208. return 0;
  209. }