stub_tx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  4. */
  5. #include <linux/kthread.h>
  6. #include <linux/minmax.h>
  7. #include <linux/socket.h>
  8. #include <linux/scatterlist.h>
  9. #include "usbip_common.h"
  10. #include "stub.h"
  11. /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
  12. void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
  13. __u32 status)
  14. {
  15. struct stub_unlink *unlink;
  16. unlink = kzalloc_obj(struct stub_unlink, GFP_ATOMIC);
  17. if (!unlink) {
  18. usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
  19. return;
  20. }
  21. unlink->seqnum = seqnum;
  22. unlink->status = status;
  23. list_add_tail(&unlink->list, &sdev->unlink_tx);
  24. }
  25. /**
  26. * stub_complete - completion handler of a usbip urb
  27. * @urb: pointer to the urb completed
  28. *
  29. * When a urb has completed, the USB core driver calls this function mostly in
  30. * the interrupt context. To return the result of a urb, the completed urb is
  31. * linked to the pending list of returning.
  32. *
  33. */
  34. void stub_complete(struct urb *urb)
  35. {
  36. struct stub_priv *priv = (struct stub_priv *) urb->context;
  37. struct stub_device *sdev = priv->sdev;
  38. unsigned long flags;
  39. usbip_dbg_stub_tx("complete! status %d\n", urb->status);
  40. switch (urb->status) {
  41. case 0:
  42. /* OK */
  43. break;
  44. case -ENOENT:
  45. dev_info(&urb->dev->dev,
  46. "stopped by a call to usb_kill_urb() because of cleaning up a virtual connection\n");
  47. return;
  48. case -ECONNRESET:
  49. dev_dbg(&urb->dev->dev,
  50. "unlinked by a call to usb_unlink_urb()\n");
  51. break;
  52. case -EPIPE:
  53. dev_info(&urb->dev->dev, "endpoint %d is stalled\n",
  54. usb_pipeendpoint(urb->pipe));
  55. break;
  56. case -ESHUTDOWN:
  57. dev_info(&urb->dev->dev, "device removed?\n");
  58. break;
  59. default:
  60. dev_info(&urb->dev->dev,
  61. "urb completion with non-zero status %d\n",
  62. urb->status);
  63. break;
  64. }
  65. /*
  66. * If the server breaks single SG request into the several URBs, the
  67. * URBs must be reassembled before sending completed URB to the vhci.
  68. * Don't wake up the tx thread until all the URBs are completed.
  69. */
  70. if (priv->sgl) {
  71. priv->completed_urbs++;
  72. /* Only save the first error status */
  73. if (urb->status && !priv->urb_status)
  74. priv->urb_status = urb->status;
  75. if (priv->completed_urbs < priv->num_urbs)
  76. return;
  77. }
  78. /* link a urb to the queue of tx. */
  79. spin_lock_irqsave(&sdev->priv_lock, flags);
  80. if (sdev->ud.tcp_socket == NULL) {
  81. usbip_dbg_stub_tx("ignore urb for closed connection\n");
  82. /* It will be freed in stub_device_cleanup_urbs(). */
  83. } else if (priv->unlinking) {
  84. stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
  85. stub_free_priv_and_urb(priv);
  86. } else {
  87. list_move_tail(&priv->list, &sdev->priv_tx);
  88. }
  89. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  90. /* wake up tx_thread */
  91. wake_up(&sdev->tx_waitq);
  92. }
  93. static inline void setup_base_pdu(struct usbip_header_basic *base,
  94. __u32 command, __u32 seqnum)
  95. {
  96. base->command = command;
  97. base->seqnum = seqnum;
  98. base->devid = 0;
  99. base->ep = 0;
  100. base->direction = 0;
  101. }
  102. static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
  103. {
  104. struct stub_priv *priv = (struct stub_priv *) urb->context;
  105. setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
  106. usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
  107. }
  108. static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
  109. struct stub_unlink *unlink)
  110. {
  111. setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
  112. rpdu->u.ret_unlink.status = unlink->status;
  113. }
  114. static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
  115. {
  116. unsigned long flags;
  117. struct stub_priv *priv, *tmp;
  118. spin_lock_irqsave(&sdev->priv_lock, flags);
  119. list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
  120. list_move_tail(&priv->list, &sdev->priv_free);
  121. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  122. return priv;
  123. }
  124. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  125. return NULL;
  126. }
  127. static int stub_send_ret_submit(struct stub_device *sdev)
  128. {
  129. unsigned long flags;
  130. struct stub_priv *priv, *tmp;
  131. struct msghdr msg;
  132. size_t txsize;
  133. size_t total_size = 0;
  134. while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
  135. struct urb *urb = priv->urbs[0];
  136. struct usbip_header pdu_header;
  137. struct usbip_iso_packet_descriptor *iso_buffer = NULL;
  138. struct kvec *iov = NULL;
  139. struct scatterlist *sg;
  140. u32 actual_length = 0;
  141. int iovnum = 0;
  142. int ret;
  143. int i;
  144. txsize = 0;
  145. memset(&pdu_header, 0, sizeof(pdu_header));
  146. memset(&msg, 0, sizeof(msg));
  147. if (urb->actual_length > 0 && !urb->transfer_buffer &&
  148. !urb->num_sgs) {
  149. dev_err(&sdev->udev->dev,
  150. "urb: actual_length %d transfer_buffer null\n",
  151. urb->actual_length);
  152. return -1;
  153. }
  154. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  155. iovnum = 2 + urb->number_of_packets;
  156. else if (usb_pipein(urb->pipe) && urb->actual_length > 0 &&
  157. urb->num_sgs)
  158. iovnum = 1 + urb->num_sgs;
  159. else if (usb_pipein(urb->pipe) && priv->sgl)
  160. iovnum = 1 + priv->num_urbs;
  161. else
  162. iovnum = 2;
  163. iov = kzalloc_objs(struct kvec, iovnum);
  164. if (!iov) {
  165. usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
  166. return -1;
  167. }
  168. iovnum = 0;
  169. /* 1. setup usbip_header */
  170. setup_ret_submit_pdu(&pdu_header, urb);
  171. usbip_dbg_stub_tx("setup txdata seqnum: %u\n",
  172. pdu_header.base.seqnum);
  173. if (priv->sgl) {
  174. for (i = 0; i < priv->num_urbs; i++)
  175. actual_length += priv->urbs[i]->actual_length;
  176. pdu_header.u.ret_submit.status = priv->urb_status;
  177. pdu_header.u.ret_submit.actual_length = actual_length;
  178. }
  179. usbip_header_correct_endian(&pdu_header, 1);
  180. iov[iovnum].iov_base = &pdu_header;
  181. iov[iovnum].iov_len = sizeof(pdu_header);
  182. iovnum++;
  183. txsize += sizeof(pdu_header);
  184. /* 2. setup transfer buffer */
  185. if (usb_pipein(urb->pipe) && priv->sgl) {
  186. /* If the server split a single SG request into several
  187. * URBs because the server's HCD doesn't support SG,
  188. * reassemble the split URB buffers into a single
  189. * return command.
  190. */
  191. for (i = 0; i < priv->num_urbs; i++) {
  192. iov[iovnum].iov_base =
  193. priv->urbs[i]->transfer_buffer;
  194. iov[iovnum].iov_len =
  195. priv->urbs[i]->actual_length;
  196. iovnum++;
  197. }
  198. txsize += actual_length;
  199. } else if (usb_pipein(urb->pipe) &&
  200. usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS &&
  201. urb->actual_length > 0) {
  202. if (urb->num_sgs) {
  203. unsigned int copy = urb->actual_length;
  204. unsigned int size;
  205. for_each_sg(urb->sg, sg, urb->num_sgs, i) {
  206. if (copy == 0)
  207. break;
  208. size = min(copy, sg->length);
  209. iov[iovnum].iov_base = sg_virt(sg);
  210. iov[iovnum].iov_len = size;
  211. iovnum++;
  212. copy -= size;
  213. }
  214. } else {
  215. iov[iovnum].iov_base = urb->transfer_buffer;
  216. iov[iovnum].iov_len = urb->actual_length;
  217. iovnum++;
  218. }
  219. txsize += urb->actual_length;
  220. } else if (usb_pipein(urb->pipe) &&
  221. usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  222. /*
  223. * For isochronous packets: actual length is the sum of
  224. * the actual length of the individual, packets, but as
  225. * the packet offsets are not changed there will be
  226. * padding between the packets. To optimally use the
  227. * bandwidth the padding is not transmitted.
  228. */
  229. int i;
  230. for (i = 0; i < urb->number_of_packets; i++) {
  231. iov[iovnum].iov_base = urb->transfer_buffer +
  232. urb->iso_frame_desc[i].offset;
  233. iov[iovnum].iov_len =
  234. urb->iso_frame_desc[i].actual_length;
  235. iovnum++;
  236. txsize += urb->iso_frame_desc[i].actual_length;
  237. }
  238. if (txsize != sizeof(pdu_header) + urb->actual_length) {
  239. dev_err(&sdev->udev->dev,
  240. "actual length of urb %d does not match iso packet sizes %zu\n",
  241. urb->actual_length,
  242. txsize-sizeof(pdu_header));
  243. kfree(iov);
  244. usbip_event_add(&sdev->ud,
  245. SDEV_EVENT_ERROR_TCP);
  246. return -1;
  247. }
  248. }
  249. /* 3. setup iso_packet_descriptor */
  250. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  251. ssize_t len = 0;
  252. iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
  253. if (!iso_buffer) {
  254. usbip_event_add(&sdev->ud,
  255. SDEV_EVENT_ERROR_MALLOC);
  256. kfree(iov);
  257. return -1;
  258. }
  259. iov[iovnum].iov_base = iso_buffer;
  260. iov[iovnum].iov_len = len;
  261. txsize += len;
  262. iovnum++;
  263. }
  264. ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
  265. iov, iovnum, txsize);
  266. if (ret != txsize) {
  267. dev_err(&sdev->udev->dev,
  268. "sendmsg failed!, retval %d for %zd\n",
  269. ret, txsize);
  270. kfree(iov);
  271. kfree(iso_buffer);
  272. usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
  273. return -1;
  274. }
  275. kfree(iov);
  276. kfree(iso_buffer);
  277. total_size += txsize;
  278. }
  279. spin_lock_irqsave(&sdev->priv_lock, flags);
  280. list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
  281. stub_free_priv_and_urb(priv);
  282. }
  283. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  284. return total_size;
  285. }
  286. static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
  287. {
  288. unsigned long flags;
  289. struct stub_unlink *unlink, *tmp;
  290. spin_lock_irqsave(&sdev->priv_lock, flags);
  291. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
  292. list_move_tail(&unlink->list, &sdev->unlink_free);
  293. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  294. return unlink;
  295. }
  296. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  297. return NULL;
  298. }
  299. static int stub_send_ret_unlink(struct stub_device *sdev)
  300. {
  301. unsigned long flags;
  302. struct stub_unlink *unlink, *tmp;
  303. struct msghdr msg;
  304. struct kvec iov[1];
  305. size_t txsize;
  306. size_t total_size = 0;
  307. while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
  308. int ret;
  309. struct usbip_header pdu_header;
  310. txsize = 0;
  311. memset(&pdu_header, 0, sizeof(pdu_header));
  312. memset(&msg, 0, sizeof(msg));
  313. memset(&iov, 0, sizeof(iov));
  314. usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
  315. /* 1. setup usbip_header */
  316. setup_ret_unlink_pdu(&pdu_header, unlink);
  317. usbip_header_correct_endian(&pdu_header, 1);
  318. iov[0].iov_base = &pdu_header;
  319. iov[0].iov_len = sizeof(pdu_header);
  320. txsize += sizeof(pdu_header);
  321. ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
  322. 1, txsize);
  323. if (ret != txsize) {
  324. dev_err(&sdev->udev->dev,
  325. "sendmsg failed!, retval %d for %zd\n",
  326. ret, txsize);
  327. usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
  328. return -1;
  329. }
  330. usbip_dbg_stub_tx("send txdata\n");
  331. total_size += txsize;
  332. }
  333. spin_lock_irqsave(&sdev->priv_lock, flags);
  334. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
  335. list_del(&unlink->list);
  336. kfree(unlink);
  337. }
  338. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  339. return total_size;
  340. }
  341. int stub_tx_loop(void *data)
  342. {
  343. struct usbip_device *ud = data;
  344. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  345. while (!kthread_should_stop()) {
  346. if (usbip_event_happened(ud))
  347. break;
  348. /*
  349. * send_ret_submit comes earlier than send_ret_unlink. stub_rx
  350. * looks at only priv_init queue. If the completion of a URB is
  351. * earlier than the receive of CMD_UNLINK, priv is moved to
  352. * priv_tx queue and stub_rx does not find the target priv. In
  353. * this case, vhci_rx receives the result of the submit request
  354. * and then receives the result of the unlink request. The
  355. * result of the submit is given back to the usbcore as the
  356. * completion of the unlink request. The request of the
  357. * unlink is ignored. This is ok because a driver who calls
  358. * usb_unlink_urb() understands the unlink was too late by
  359. * getting the status of the given-backed URB which has the
  360. * status of usb_submit_urb().
  361. */
  362. if (stub_send_ret_submit(sdev) < 0)
  363. break;
  364. if (stub_send_ret_unlink(sdev) < 0)
  365. break;
  366. wait_event_interruptible(sdev->tx_waitq,
  367. (!list_empty(&sdev->priv_tx) ||
  368. !list_empty(&sdev->unlink_tx) ||
  369. kthread_should_stop()));
  370. }
  371. return 0;
  372. }