raid5-ppl: PPL support for disks with write-back cache enabled
[linux-2.6-block.git] / drivers / usb / usbip / stub_dev.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2003-2008 Takahiro Hirofuchi
4  */
5
6 #include <linux/device.h>
7 #include <linux/file.h>
8 #include <linux/kthread.h>
9 #include <linux/module.h>
10
11 #include "usbip_common.h"
12 #include "stub.h"
13
14 /*
15  * usbip_status shows the status of usbip-host as long as this driver is bound
16  * to the target device.
17  */
18 static ssize_t usbip_status_show(struct device *dev,
19                                  struct device_attribute *attr, char *buf)
20 {
21         struct stub_device *sdev = dev_get_drvdata(dev);
22         int status;
23
24         if (!sdev) {
25                 dev_err(dev, "sdev is null\n");
26                 return -ENODEV;
27         }
28
29         spin_lock_irq(&sdev->ud.lock);
30         status = sdev->ud.status;
31         spin_unlock_irq(&sdev->ud.lock);
32
33         return snprintf(buf, PAGE_SIZE, "%d\n", status);
34 }
35 static DEVICE_ATTR_RO(usbip_status);
36
37 /*
38  * usbip_sockfd gets a socket descriptor of an established TCP connection that
39  * is used to transfer usbip requests by kernel threads. -1 is a magic number
40  * by which usbip connection is finished.
41  */
42 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
43                             const char *buf, size_t count)
44 {
45         struct stub_device *sdev = dev_get_drvdata(dev);
46         int sockfd = 0;
47         struct socket *socket;
48         int rv;
49
50         if (!sdev) {
51                 dev_err(dev, "sdev is null\n");
52                 return -ENODEV;
53         }
54
55         rv = sscanf(buf, "%d", &sockfd);
56         if (rv != 1)
57                 return -EINVAL;
58
59         if (sockfd != -1) {
60                 int err;
61
62                 dev_info(dev, "stub up\n");
63
64                 spin_lock_irq(&sdev->ud.lock);
65
66                 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
67                         dev_err(dev, "not ready\n");
68                         goto err;
69                 }
70
71                 socket = sockfd_lookup(sockfd, &err);
72                 if (!socket)
73                         goto err;
74
75                 sdev->ud.tcp_socket = socket;
76
77                 spin_unlock_irq(&sdev->ud.lock);
78
79                 sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
80                                                   "stub_rx");
81                 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
82                                                   "stub_tx");
83
84                 spin_lock_irq(&sdev->ud.lock);
85                 sdev->ud.status = SDEV_ST_USED;
86                 spin_unlock_irq(&sdev->ud.lock);
87
88         } else {
89                 dev_info(dev, "stub down\n");
90
91                 spin_lock_irq(&sdev->ud.lock);
92                 if (sdev->ud.status != SDEV_ST_USED)
93                         goto err;
94
95                 spin_unlock_irq(&sdev->ud.lock);
96
97                 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
98         }
99
100         return count;
101
102 err:
103         spin_unlock_irq(&sdev->ud.lock);
104         return -EINVAL;
105 }
106 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
107
108 static int stub_add_files(struct device *dev)
109 {
110         int err = 0;
111
112         err = device_create_file(dev, &dev_attr_usbip_status);
113         if (err)
114                 goto err_status;
115
116         err = device_create_file(dev, &dev_attr_usbip_sockfd);
117         if (err)
118                 goto err_sockfd;
119
120         err = device_create_file(dev, &dev_attr_usbip_debug);
121         if (err)
122                 goto err_debug;
123
124         return 0;
125
126 err_debug:
127         device_remove_file(dev, &dev_attr_usbip_sockfd);
128 err_sockfd:
129         device_remove_file(dev, &dev_attr_usbip_status);
130 err_status:
131         return err;
132 }
133
134 static void stub_remove_files(struct device *dev)
135 {
136         device_remove_file(dev, &dev_attr_usbip_status);
137         device_remove_file(dev, &dev_attr_usbip_sockfd);
138         device_remove_file(dev, &dev_attr_usbip_debug);
139 }
140
141 static void stub_shutdown_connection(struct usbip_device *ud)
142 {
143         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
144
145         /*
146          * When removing an exported device, kernel panic sometimes occurred
147          * and then EIP was sk_wait_data of stub_rx thread. Is this because
148          * sk_wait_data returned though stub_rx thread was already finished by
149          * step 1?
150          */
151         if (ud->tcp_socket) {
152                 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
153                         ud->tcp_socket);
154                 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
155         }
156
157         /* 1. stop threads */
158         if (ud->tcp_rx) {
159                 kthread_stop_put(ud->tcp_rx);
160                 ud->tcp_rx = NULL;
161         }
162         if (ud->tcp_tx) {
163                 kthread_stop_put(ud->tcp_tx);
164                 ud->tcp_tx = NULL;
165         }
166
167         /*
168          * 2. close the socket
169          *
170          * tcp_socket is freed after threads are killed so that usbip_xmit does
171          * not touch NULL socket.
172          */
173         if (ud->tcp_socket) {
174                 sockfd_put(ud->tcp_socket);
175                 ud->tcp_socket = NULL;
176         }
177
178         /* 3. free used data */
179         stub_device_cleanup_urbs(sdev);
180
181         /* 4. free stub_unlink */
182         {
183                 unsigned long flags;
184                 struct stub_unlink *unlink, *tmp;
185
186                 spin_lock_irqsave(&sdev->priv_lock, flags);
187                 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
188                         list_del(&unlink->list);
189                         kfree(unlink);
190                 }
191                 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
192                                          list) {
193                         list_del(&unlink->list);
194                         kfree(unlink);
195                 }
196                 spin_unlock_irqrestore(&sdev->priv_lock, flags);
197         }
198 }
199
200 static void stub_device_reset(struct usbip_device *ud)
201 {
202         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
203         struct usb_device *udev = sdev->udev;
204         int ret;
205
206         dev_dbg(&udev->dev, "device reset");
207
208         ret = usb_lock_device_for_reset(udev, NULL);
209         if (ret < 0) {
210                 dev_err(&udev->dev, "lock for reset\n");
211                 spin_lock_irq(&ud->lock);
212                 ud->status = SDEV_ST_ERROR;
213                 spin_unlock_irq(&ud->lock);
214                 return;
215         }
216
217         /* try to reset the device */
218         ret = usb_reset_device(udev);
219         usb_unlock_device(udev);
220
221         spin_lock_irq(&ud->lock);
222         if (ret) {
223                 dev_err(&udev->dev, "device reset\n");
224                 ud->status = SDEV_ST_ERROR;
225         } else {
226                 dev_info(&udev->dev, "device reset\n");
227                 ud->status = SDEV_ST_AVAILABLE;
228         }
229         spin_unlock_irq(&ud->lock);
230 }
231
232 static void stub_device_unusable(struct usbip_device *ud)
233 {
234         spin_lock_irq(&ud->lock);
235         ud->status = SDEV_ST_ERROR;
236         spin_unlock_irq(&ud->lock);
237 }
238
239 /**
240  * stub_device_alloc - allocate a new stub_device struct
241  * @udev: usb_device of a new device
242  *
243  * Allocates and initializes a new stub_device struct.
244  */
245 static struct stub_device *stub_device_alloc(struct usb_device *udev)
246 {
247         struct stub_device *sdev;
248         int busnum = udev->bus->busnum;
249         int devnum = udev->devnum;
250
251         dev_dbg(&udev->dev, "allocating stub device");
252
253         /* yes, it's a new device */
254         sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
255         if (!sdev)
256                 return NULL;
257
258         sdev->udev = usb_get_dev(udev);
259
260         /*
261          * devid is defined with devnum when this driver is first allocated.
262          * devnum may change later if a device is reset. However, devid never
263          * changes during a usbip connection.
264          */
265         sdev->devid             = (busnum << 16) | devnum;
266         sdev->ud.side           = USBIP_STUB;
267         sdev->ud.status         = SDEV_ST_AVAILABLE;
268         spin_lock_init(&sdev->ud.lock);
269         sdev->ud.tcp_socket     = NULL;
270
271         INIT_LIST_HEAD(&sdev->priv_init);
272         INIT_LIST_HEAD(&sdev->priv_tx);
273         INIT_LIST_HEAD(&sdev->priv_free);
274         INIT_LIST_HEAD(&sdev->unlink_free);
275         INIT_LIST_HEAD(&sdev->unlink_tx);
276         spin_lock_init(&sdev->priv_lock);
277
278         init_waitqueue_head(&sdev->tx_waitq);
279
280         sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
281         sdev->ud.eh_ops.reset    = stub_device_reset;
282         sdev->ud.eh_ops.unusable = stub_device_unusable;
283
284         usbip_start_eh(&sdev->ud);
285
286         dev_dbg(&udev->dev, "register new device\n");
287
288         return sdev;
289 }
290
291 static void stub_device_free(struct stub_device *sdev)
292 {
293         kfree(sdev);
294 }
295
296 static int stub_probe(struct usb_device *udev)
297 {
298         struct stub_device *sdev = NULL;
299         const char *udev_busid = dev_name(&udev->dev);
300         struct bus_id_priv *busid_priv;
301         int rc;
302
303         dev_dbg(&udev->dev, "Enter\n");
304
305         /* check we should claim or not by busid_table */
306         busid_priv = get_busid_priv(udev_busid);
307         if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
308             (busid_priv->status == STUB_BUSID_OTHER)) {
309                 dev_info(&udev->dev,
310                         "%s is not in match_busid table... skip!\n",
311                         udev_busid);
312
313                 /*
314                  * Return value should be ENODEV or ENOXIO to continue trying
315                  * other matched drivers by the driver core.
316                  * See driver_probe_device() in driver/base/dd.c
317                  */
318                 return -ENODEV;
319         }
320
321         if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
322                 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
323                          udev_busid);
324                 return -ENODEV;
325         }
326
327         if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
328                 dev_dbg(&udev->dev,
329                         "%s is attached on vhci_hcd... skip!\n",
330                         udev_busid);
331
332                 return -ENODEV;
333         }
334
335         /* ok, this is my device */
336         sdev = stub_device_alloc(udev);
337         if (!sdev)
338                 return -ENOMEM;
339
340         dev_info(&udev->dev,
341                 "usbip-host: register new device (bus %u dev %u)\n",
342                 udev->bus->busnum, udev->devnum);
343
344         busid_priv->shutdown_busid = 0;
345
346         /* set private data to usb_device */
347         dev_set_drvdata(&udev->dev, sdev);
348         busid_priv->sdev = sdev;
349         busid_priv->udev = udev;
350
351         /*
352          * Claim this hub port.
353          * It doesn't matter what value we pass as owner
354          * (struct dev_state) as long as it is unique.
355          */
356         rc = usb_hub_claim_port(udev->parent, udev->portnum,
357                         (struct usb_dev_state *) udev);
358         if (rc) {
359                 dev_dbg(&udev->dev, "unable to claim port\n");
360                 goto err_port;
361         }
362
363         rc = stub_add_files(&udev->dev);
364         if (rc) {
365                 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
366                 goto err_files;
367         }
368         busid_priv->status = STUB_BUSID_ALLOC;
369
370         return 0;
371 err_files:
372         usb_hub_release_port(udev->parent, udev->portnum,
373                              (struct usb_dev_state *) udev);
374 err_port:
375         dev_set_drvdata(&udev->dev, NULL);
376         usb_put_dev(udev);
377
378         busid_priv->sdev = NULL;
379         stub_device_free(sdev);
380         return rc;
381 }
382
383 static void shutdown_busid(struct bus_id_priv *busid_priv)
384 {
385         if (busid_priv->sdev && !busid_priv->shutdown_busid) {
386                 busid_priv->shutdown_busid = 1;
387                 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
388
389                 /* wait for the stop of the event handler */
390                 usbip_stop_eh(&busid_priv->sdev->ud);
391         }
392 }
393
394 /*
395  * called in usb_disconnect() or usb_deregister()
396  * but only if actconfig(active configuration) exists
397  */
398 static void stub_disconnect(struct usb_device *udev)
399 {
400         struct stub_device *sdev;
401         const char *udev_busid = dev_name(&udev->dev);
402         struct bus_id_priv *busid_priv;
403         int rc;
404
405         dev_dbg(&udev->dev, "Enter\n");
406
407         busid_priv = get_busid_priv(udev_busid);
408         if (!busid_priv) {
409                 BUG();
410                 return;
411         }
412
413         sdev = dev_get_drvdata(&udev->dev);
414
415         /* get stub_device */
416         if (!sdev) {
417                 dev_err(&udev->dev, "could not get device");
418                 return;
419         }
420
421         dev_set_drvdata(&udev->dev, NULL);
422
423         /*
424          * NOTE: rx/tx threads are invoked for each usb_device.
425          */
426         stub_remove_files(&udev->dev);
427
428         /* release port */
429         rc = usb_hub_release_port(udev->parent, udev->portnum,
430                                   (struct usb_dev_state *) udev);
431         if (rc) {
432                 dev_dbg(&udev->dev, "unable to release port\n");
433                 return;
434         }
435
436         /* If usb reset is called from event handler */
437         if (usbip_in_eh(current))
438                 return;
439
440         /* shutdown the current connection */
441         shutdown_busid(busid_priv);
442
443         usb_put_dev(sdev->udev);
444
445         /* free sdev */
446         busid_priv->sdev = NULL;
447         stub_device_free(sdev);
448
449         if (busid_priv->status == STUB_BUSID_ALLOC) {
450                 busid_priv->status = STUB_BUSID_ADDED;
451         } else {
452                 busid_priv->status = STUB_BUSID_OTHER;
453                 del_match_busid((char *)udev_busid);
454         }
455 }
456
457 #ifdef CONFIG_PM
458
459 /* These functions need usb_port_suspend and usb_port_resume,
460  * which reside in drivers/usb/core/usb.h. Skip for now. */
461
462 static int stub_suspend(struct usb_device *udev, pm_message_t message)
463 {
464         dev_dbg(&udev->dev, "stub_suspend\n");
465
466         return 0;
467 }
468
469 static int stub_resume(struct usb_device *udev, pm_message_t message)
470 {
471         dev_dbg(&udev->dev, "stub_resume\n");
472
473         return 0;
474 }
475
476 #endif  /* CONFIG_PM */
477
478 struct usb_device_driver stub_driver = {
479         .name           = "usbip-host",
480         .probe          = stub_probe,
481         .disconnect     = stub_disconnect,
482 #ifdef CONFIG_PM
483         .suspend        = stub_suspend,
484         .resume         = stub_resume,
485 #endif
486         .supports_autosuspend   =       0,
487 };