Merge tag 'clk-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux
[linux-2.6-block.git] / drivers / usb / usbip / vhci_rx.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
04679b34
TH
2/*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
04679b34
TH
4 */
5
9720b4bc 6#include <linux/kthread.h>
7aaacb43 7#include <linux/slab.h>
5a0e3ad6 8
04679b34
TH
9#include "usbip_common.h"
10#include "vhci.h"
11
b92a5e23 12/* get URB from transmitted urb queue. caller must hold vdev->priv_lock */
5ef6aceb 13struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum)
04679b34
TH
14{
15 struct vhci_priv *priv, *tmp;
16 struct urb *urb = NULL;
17 int status;
18
04679b34 19 list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
2663d79b
SR
20 if (priv->seqnum != seqnum)
21 continue;
04679b34 22
2663d79b
SR
23 urb = priv->urb;
24 status = urb->status;
04679b34 25
8272d099 26 usbip_dbg_vhci_rx("find urb seqnum %u\n", seqnum);
04679b34 27
2663d79b
SR
28 switch (status) {
29 case -ENOENT:
30 /* fall through */
31 case -ECONNRESET:
8272d099 32 dev_dbg(&urb->dev->dev,
cb483264 33 "urb seq# %u was unlinked %ssynchronously\n",
8272d099 34 seqnum, status == -ENOENT ? "" : "a");
2663d79b
SR
35 break;
36 case -EINPROGRESS:
37 /* no info output */
04679b34 38 break;
2663d79b 39 default:
8272d099
SK
40 dev_dbg(&urb->dev->dev,
41 "urb seq# %u may be in a error, status %d\n",
42 seqnum, status);
04679b34 43 }
2663d79b
SR
44
45 list_del(&priv->list);
46 kfree(priv);
47 urb->hcpriv = NULL;
48
49 break;
04679b34
TH
50 }
51
04679b34
TH
52 return urb;
53}
54
55static void vhci_recv_ret_submit(struct vhci_device *vdev,
5ef6aceb 56 struct usbip_header *pdu)
04679b34 57{
03cd00d5
YD
58 struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
59 struct vhci *vhci = vhci_hcd->vhci;
04679b34
TH
60 struct usbip_device *ud = &vdev->ud;
61 struct urb *urb;
21619792 62 unsigned long flags;
04679b34 63
21619792 64 spin_lock_irqsave(&vdev->priv_lock, flags);
04679b34 65 urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
21619792 66 spin_unlock_irqrestore(&vdev->priv_lock, flags);
04679b34
TH
67
68 if (!urb) {
8272d099
SK
69 pr_err("cannot find a urb of seqnum %u max seqnum %d\n",
70 pdu->base.seqnum,
03cd00d5 71 atomic_read(&vhci_hcd->seqnum));
04679b34
TH
72 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
73 return;
74 }
75
04679b34
TH
76 /* unpack the pdu to a urb */
77 usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
78
04679b34
TH
79 /* recv transfer buffer */
80 if (usbip_recv_xbuff(ud, urb) < 0)
81 return;
82
04679b34
TH
83 /* recv iso_packet_descriptor */
84 if (usbip_recv_iso(ud, urb) < 0)
85 return;
86
28276a28 87 /* restore the padding in iso packets */
ac2b41ac 88 usbip_pad_iso(ud, urb);
04679b34 89
b8868e45 90 if (usbip_dbg_flag_vhci_rx)
04679b34
TH
91 usbip_dump_urb(urb);
92
8272d099 93 usbip_dbg_vhci_rx("now giveback urb %u\n", pdu->base.seqnum);
04679b34 94
0775a9cb 95 spin_lock_irqsave(&vhci->lock, flags);
03cd00d5 96 usb_hcd_unlink_urb_from_ep(vhci_hcd_to_hcd(vhci_hcd), urb);
0775a9cb 97 spin_unlock_irqrestore(&vhci->lock, flags);
04679b34 98
03cd00d5 99 usb_hcd_giveback_urb(vhci_hcd_to_hcd(vhci_hcd), urb, urb->status);
04679b34 100
b8868e45 101 usbip_dbg_vhci_rx("Leave\n");
04679b34
TH
102}
103
04679b34 104static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
5ef6aceb 105 struct usbip_header *pdu)
04679b34
TH
106{
107 struct vhci_unlink *unlink, *tmp;
21619792 108 unsigned long flags;
04679b34 109
21619792 110 spin_lock_irqsave(&vdev->priv_lock, flags);
04679b34
TH
111
112 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
1a4b6f66 113 pr_info("unlink->seqnum %lu\n", unlink->seqnum);
04679b34 114 if (unlink->seqnum == pdu->base.seqnum) {
b8868e45 115 usbip_dbg_vhci_rx("found pending unlink, %lu\n",
5ef6aceb 116 unlink->seqnum);
04679b34
TH
117 list_del(&unlink->list);
118
21619792 119 spin_unlock_irqrestore(&vdev->priv_lock, flags);
04679b34
TH
120 return unlink;
121 }
122 }
123
21619792 124 spin_unlock_irqrestore(&vdev->priv_lock, flags);
04679b34
TH
125
126 return NULL;
127}
128
04679b34 129static void vhci_recv_ret_unlink(struct vhci_device *vdev,
5ef6aceb 130 struct usbip_header *pdu)
04679b34 131{
03cd00d5
YD
132 struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
133 struct vhci *vhci = vhci_hcd->vhci;
04679b34
TH
134 struct vhci_unlink *unlink;
135 struct urb *urb;
21619792 136 unsigned long flags;
04679b34
TH
137
138 usbip_dump_header(pdu);
139
140 unlink = dequeue_pending_unlink(vdev, pdu);
141 if (!unlink) {
1a4b6f66 142 pr_info("cannot find the pending unlink %u\n",
143 pdu->base.seqnum);
04679b34
TH
144 return;
145 }
146
21619792 147 spin_lock_irqsave(&vdev->priv_lock, flags);
04679b34 148 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
21619792 149 spin_unlock_irqrestore(&vdev->priv_lock, flags);
b92a5e23 150
04679b34
TH
151 if (!urb) {
152 /*
153 * I get the result of a unlink request. But, it seems that I
154 * already received the result of its submit result and gave
155 * back the URB.
156 */
3567f979 157 pr_info("the urb (seqnum %d) was already given back\n",
1a4b6f66 158 pdu->base.seqnum);
04679b34 159 } else {
8272d099 160 usbip_dbg_vhci_rx("now giveback urb %d\n", pdu->base.seqnum);
04679b34 161
c7f00899 162 /* If unlink is successful, status is -ECONNRESET */
04679b34 163 urb->status = pdu->u.ret_unlink.status;
1a4b6f66 164 pr_info("urb->status %d\n", urb->status);
04679b34 165
0775a9cb 166 spin_lock_irqsave(&vhci->lock, flags);
03cd00d5 167 usb_hcd_unlink_urb_from_ep(vhci_hcd_to_hcd(vhci_hcd), urb);
0775a9cb 168 spin_unlock_irqrestore(&vhci->lock, flags);
04679b34 169
03cd00d5 170 usb_hcd_giveback_urb(vhci_hcd_to_hcd(vhci_hcd), urb, urb->status);
04679b34
TH
171 }
172
173 kfree(unlink);
04679b34
TH
174}
175
bd65f623
MV
176static int vhci_priv_tx_empty(struct vhci_device *vdev)
177{
178 int empty = 0;
21619792 179 unsigned long flags;
bd65f623 180
21619792 181 spin_lock_irqsave(&vdev->priv_lock, flags);
bd65f623 182 empty = list_empty(&vdev->priv_rx);
21619792 183 spin_unlock_irqrestore(&vdev->priv_lock, flags);
bd65f623
MV
184
185 return empty;
186}
187
04679b34
TH
188/* recv a pdu */
189static void vhci_rx_pdu(struct usbip_device *ud)
190{
191 int ret;
192 struct usbip_header pdu;
193 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
194
b8868e45 195 usbip_dbg_vhci_rx("Enter\n");
04679b34
TH
196
197 memset(&pdu, 0, sizeof(pdu));
198
77178807 199 /* receive a pdu header */
5a08c526 200 ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
7e249c8b
MV
201 if (ret < 0) {
202 if (ret == -ECONNRESET)
1a4b6f66 203 pr_info("connection reset by peer\n");
bd65f623
MV
204 else if (ret == -EAGAIN) {
205 /* ignore if connection was idle */
206 if (vhci_priv_tx_empty(vdev))
207 return;
1a4b6f66 208 pr_info("connection timed out with pending urbs\n");
bd65f623 209 } else if (ret != -ERESTARTSYS)
1a4b6f66 210 pr_info("xmit failed %d\n", ret);
bd65f623 211
7e249c8b
MV
212 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
213 return;
214 }
215 if (ret == 0) {
1a4b6f66 216 pr_info("connection closed");
7e249c8b
MV
217 usbip_event_add(ud, VDEV_EVENT_DOWN);
218 return;
219 }
04679b34 220 if (ret != sizeof(pdu)) {
1a4b6f66 221 pr_err("received pdu size is %d, should be %d\n", ret,
222 (unsigned int)sizeof(pdu));
04679b34
TH
223 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
224 return;
225 }
226
227 usbip_header_correct_endian(&pdu, 0);
228
b8868e45 229 if (usbip_dbg_flag_vhci_rx)
04679b34
TH
230 usbip_dump_header(&pdu);
231
232 switch (pdu.base.command) {
233 case USBIP_RET_SUBMIT:
234 vhci_recv_ret_submit(vdev, &pdu);
235 break;
236 case USBIP_RET_UNLINK:
237 vhci_recv_ret_unlink(vdev, &pdu);
238 break;
239 default:
49aecefc 240 /* NOT REACHED */
1a4b6f66 241 pr_err("unknown pdu %u\n", pdu.base.command);
04679b34
TH
242 usbip_dump_header(&pdu);
243 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
49aecefc 244 break;
04679b34
TH
245 }
246}
247
9720b4bc 248int vhci_rx_loop(void *data)
04679b34 249{
9720b4bc 250 struct usbip_device *ud = data;
04679b34 251
9720b4bc 252 while (!kthread_should_stop()) {
b8868e45 253 if (usbip_event_happened(ud))
04679b34
TH
254 break;
255
256 vhci_rx_pdu(ud);
257 }
04679b34 258
9720b4bc
AB
259 return 0;
260}