vhci_rx.c 6.1 KB

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