usbnet.c 57.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds's avatar
Linus Torvalds committed
2
/*
3
 * USB Network driver infrastructure
4
 * Copyright (C) 2000-2005 by David Brownell
Linus Torvalds's avatar
Linus Torvalds committed
5
6
7
8
9
 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
 */

/*
 * This is a generic "USB networking" framework that works with several
10
11
 * kinds of full and high speed networking devices:  host-to-host cables,
 * smart usb peripherals, and actual Ethernet adapters.
Linus Torvalds's avatar
Linus Torvalds committed
12
 *
13
14
15
16
17
18
 * These devices usually differ in terms of control protocols (if they
 * even have one!) and sometimes they define new framing to wrap or batch
 * Ethernet packets.  Otherwise, they talk to USB pretty much the same,
 * so interface (un)binding, endpoint I/O queues, fault handling, and other
 * issues can usefully be addressed by this framework.
 */
Linus Torvalds's avatar
Linus Torvalds committed
19
20
21
22
23
24
25
26

// #define	DEBUG			// error path messages, extra info
// #define	VERBOSE			// more; success messages

#include <linux/module.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
27
#include <linux/ctype.h>
Linus Torvalds's avatar
Linus Torvalds committed
28
29
30
31
#include <linux/ethtool.h>
#include <linux/workqueue.h>
#include <linux/mii.h>
#include <linux/usb.h>
32
#include <linux/usb/usbnet.h>
33
#include <linux/slab.h>
34
#include <linux/kernel.h>
35
#include <linux/pm_runtime.h>
36
37

#define DRIVER_VERSION		"22-Aug-2005"
Linus Torvalds's avatar
Linus Torvalds committed
38
39
40
41
42
43
44
45
46
47


/*-------------------------------------------------------------------------*/

/*
 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
 * Several dozen bytes of IPv4 data can fit in two such transactions.
 * One maximum size Ethernet packet takes twenty four of them.
 * For high speed, each frame comfortably fits almost 36 max size
 * Ethernet packets (so queues should be bigger).
48
 *
49
50
51
 * The goal is to let the USB host controller be busy for 5msec or
 * more before an irq is required, under load.  Jumbograms change
 * the equation.
Linus Torvalds's avatar
Linus Torvalds committed
52
 */
53
54
55
#define	MAX_QUEUE_MEMORY	(60 * 1518)
#define	RX_QLEN(dev)		((dev)->rx_qlen)
#define	TX_QLEN(dev)		((dev)->tx_qlen)
Linus Torvalds's avatar
Linus Torvalds committed
56
57
58
59

// reawaken network queue this soon after stopping; else watchdog barks
#define TX_TIMEOUT_JIFFIES	(5*HZ)

60
61
62
/* throttle rx/tx briefly after some faults, so hub_wq might disconnect()
 * us (it polls at HZ/4 usually) before we report too many false errors.
 */
Linus Torvalds's avatar
Linus Torvalds committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#define THROTTLE_JIFFIES	(HZ/8)

// between wakeups
#define UNLINK_TIMEOUT_MS	3

/*-------------------------------------------------------------------------*/

// randomly generated ethernet address
static u8	node_id [ETH_ALEN];

/* use ethtool to change the level for any given device */
static int msg_level = -1;
module_param (msg_level, int, 0);
MODULE_PARM_DESC (msg_level, "Override default message level");

/*-------------------------------------------------------------------------*/

/* handles CDC Ethernet and many other network "bulk data" interfaces */
81
int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
Linus Torvalds's avatar
Linus Torvalds committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{
	int				tmp;
	struct usb_host_interface	*alt = NULL;
	struct usb_host_endpoint	*in = NULL, *out = NULL;
	struct usb_host_endpoint	*status = NULL;

	for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
		unsigned	ep;

		in = out = status = NULL;
		alt = intf->altsetting + tmp;

		/* take the first altsetting with in-bulk + out-bulk;
		 * remember any status endpoint, just in case;
96
		 * ignore other endpoints and altsettings.
Linus Torvalds's avatar
Linus Torvalds committed
97
98
99
100
101
102
		 */
		for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
			struct usb_host_endpoint	*e;
			int				intr = 0;

			e = alt->endpoint + ep;
103
104
105
106
107

			/* ignore endpoints which cannot transfer data */
			if (!usb_endpoint_maxp(&e->desc))
				continue;

Linus Torvalds's avatar
Linus Torvalds committed
108
109
			switch (e->desc.bmAttributes) {
			case USB_ENDPOINT_XFER_INT:
110
				if (!usb_endpoint_dir_in(&e->desc))
Linus Torvalds's avatar
Linus Torvalds committed
111
112
113
114
115
116
117
118
					continue;
				intr = 1;
				/* FALLTHROUGH */
			case USB_ENDPOINT_XFER_BULK:
				break;
			default:
				continue;
			}
119
			if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds's avatar
Linus Torvalds committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
				if (!intr && !in)
					in = e;
				else if (intr && !status)
					status = e;
			} else {
				if (!out)
					out = e;
			}
		}
		if (in && out)
			break;
	}
	if (!alt || !in || !out)
		return -EINVAL;

135
136
	if (alt->desc.bAlternateSetting != 0 ||
	    !(dev->driver_info->flags & FLAG_NO_SETINT)) {
Linus Torvalds's avatar
Linus Torvalds committed
137
138
139
140
141
		tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
				alt->desc.bAlternateSetting);
		if (tmp < 0)
			return tmp;
	}
142

Linus Torvalds's avatar
Linus Torvalds committed
143
144
145
146
147
148
149
	dev->in = usb_rcvbulkpipe (dev->udev,
			in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
	dev->out = usb_sndbulkpipe (dev->udev,
			out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
	dev->status = status;
	return 0;
}
150
EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
Linus Torvalds's avatar
Linus Torvalds committed
151

152
153
int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
{
154
	int 		tmp = -1, ret;
155
156
	unsigned char	buf [13];

157
158
159
160
	ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
	if (ret == 12)
		tmp = hex2bin(dev->net->dev_addr, buf, 6);
	if (tmp < 0) {
161
162
		dev_dbg(&dev->udev->dev,
			"bad MAC string %d fetch, %d\n", iMACAddress, tmp);
163
164
165
		if (ret >= 0)
			ret = -EINVAL;
		return ret;
166
167
168
169
170
	}
	return 0;
}
EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
static void intr_complete (struct urb *urb)
{
	struct usbnet	*dev = urb->context;
	int		status = urb->status;

	switch (status) {
	/* success */
	case 0:
		dev->driver_info->status(dev, urb);
		break;

	/* software-driven interface shutdown */
	case -ENOENT:		/* urb killed */
	case -ESHUTDOWN:	/* hardware gone */
		netif_dbg(dev, ifdown, dev->net,
			  "intr shutdown, code %d\n", status);
		return;

	/* NOTE:  not throttling like RX/TX, since this endpoint
	 * already polls infrequently
	 */
	default:
		netdev_dbg(dev->net, "intr status %d\n", status);
		break;
	}

	status = usb_submit_urb (urb, GFP_ATOMIC);
	if (status != 0)
		netif_err(dev, timer, dev->net,
			  "intr resubmit --> %d\n", status);
}
Linus Torvalds's avatar
Linus Torvalds committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

static int init_status (struct usbnet *dev, struct usb_interface *intf)
{
	char		*buf = NULL;
	unsigned	pipe = 0;
	unsigned	maxp;
	unsigned	period;

	if (!dev->driver_info->status)
		return 0;

	pipe = usb_rcvintpipe (dev->udev,
			dev->status->desc.bEndpointAddress
				& USB_ENDPOINT_NUMBER_MASK);
	maxp = usb_maxpacket (dev->udev, pipe, 0);

	/* avoid 1 msec chatter:  min 8 msec poll rate */
	period = max ((int) dev->status->desc.bInterval,
		(dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);

222
	buf = kmalloc (maxp, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
223
	if (buf) {
224
		dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
225
226
227
228
229
230
		if (!dev->interrupt) {
			kfree (buf);
			return -ENOMEM;
		} else {
			usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
				buf, maxp, intr_complete, dev, period);
231
			dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
Linus Torvalds's avatar
Linus Torvalds committed
232
233
234
235
236
			dev_dbg(&intf->dev,
				"status ep%din, %d bytes period %d\n",
				usb_pipeendpoint(pipe), maxp, period);
		}
	}
David Brownell's avatar
David Brownell committed
237
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
238
239
}

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Submit the interrupt URB if not previously submitted, increasing refcount */
int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
{
	int ret = 0;

	WARN_ON_ONCE(dev->interrupt == NULL);
	if (dev->interrupt) {
		mutex_lock(&dev->interrupt_mutex);

		if (++dev->interrupt_count == 1)
			ret = usb_submit_urb(dev->interrupt, mem_flags);

		dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
			dev->interrupt_count);
		mutex_unlock(&dev->interrupt_mutex);
	}
	return ret;
}
EXPORT_SYMBOL_GPL(usbnet_status_start);

/* For resume; submit interrupt URB if previously submitted */
static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
{
	int ret = 0;

	mutex_lock(&dev->interrupt_mutex);
	if (dev->interrupt_count) {
		ret = usb_submit_urb(dev->interrupt, mem_flags);
		dev_dbg(&dev->udev->dev,
			"submitted interrupt URB for resume\n");
	}
	mutex_unlock(&dev->interrupt_mutex);
	return ret;
}

/* Kill the interrupt URB if all submitters want it killed */
void usbnet_status_stop(struct usbnet *dev)
{
	if (dev->interrupt) {
		mutex_lock(&dev->interrupt_mutex);
		WARN_ON(dev->interrupt_count == 0);

		if (dev->interrupt_count && --dev->interrupt_count == 0)
			usb_kill_urb(dev->interrupt);

		dev_dbg(&dev->udev->dev,
			"decremented interrupt URB count to %d\n",
			dev->interrupt_count);
		mutex_unlock(&dev->interrupt_mutex);
	}
}
EXPORT_SYMBOL_GPL(usbnet_status_stop);

/* For suspend; always kill interrupt URB */
static void __usbnet_status_stop_force(struct usbnet *dev)
{
	if (dev->interrupt) {
		mutex_lock(&dev->interrupt_mutex);
		usb_kill_urb(dev->interrupt);
		dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
		mutex_unlock(&dev->interrupt_mutex);
	}
}

304
305
306
307
308
/* Passes this packet up the stack, updating its accounting.
 * Some link protocols batch packets, so their rx_fixup paths
 * can return clones as well as just modify the original skb.
 */
void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
309
{
310
	struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
311
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
312
313
	int	status;

Jussi Kivilinna's avatar
Jussi Kivilinna committed
314
315
316
317
318
	if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
		skb_queue_tail(&dev->rxq_pause, skb);
		return;
	}

319
320
321
322
	/* only update if unset to allow minidriver rx_fixup override */
	if (skb->protocol == 0)
		skb->protocol = eth_type_trans (skb, dev->net);

323
	flags = u64_stats_update_begin_irqsave(&stats64->syncp);
324
325
	stats64->rx_packets++;
	stats64->rx_bytes += skb->len;
326
	u64_stats_update_end_irqrestore(&stats64->syncp, flags);
Linus Torvalds's avatar
Linus Torvalds committed
327

328
329
	netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
		  skb->len + sizeof (struct ethhdr), skb->protocol);
Linus Torvalds's avatar
Linus Torvalds committed
330
	memset (skb->cb, 0, sizeof (struct skb_data));
331
332
333
334

	if (skb_defer_rx_timestamp(skb))
		return;

Linus Torvalds's avatar
Linus Torvalds committed
335
	status = netif_rx (skb);
336
337
338
	if (status != NET_RX_SUCCESS)
		netif_dbg(dev, rx_err, dev->net,
			  "netif_rx status %d\n", status);
Linus Torvalds's avatar
Linus Torvalds committed
339
}
340
EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds's avatar
Linus Torvalds committed
341

342
343
344
345
346
/* must be called if hard_mtu or rx_urb_size changed */
void usbnet_update_max_qlen(struct usbnet *dev)
{
	enum usb_device_speed speed = dev->udev->speed;

347
348
	if (!dev->rx_urb_size || !dev->hard_mtu)
		goto insanity;
349
350
351
352
353
	switch (speed) {
	case USB_SPEED_HIGH:
		dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
		dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
		break;
354
	case USB_SPEED_SUPER:
Oliver Neukum's avatar
Oliver Neukum committed
355
	case USB_SPEED_SUPER_PLUS:
356
357
358
359
360
361
362
363
		/*
		 * Not take default 5ms qlen for super speed HC to
		 * save memory, and iperf tests show 2.5ms qlen can
		 * work well
		 */
		dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
		dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
		break;
364
	default:
365
insanity:
366
367
368
369
370
		dev->rx_qlen = dev->tx_qlen = 4;
	}
}
EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);

Linus Torvalds's avatar
Linus Torvalds committed
371
372
373
374
375
376
377

/*-------------------------------------------------------------------------
 *
 * Network Device Driver (peer link to "Host Device", from USB host)
 *
 *-------------------------------------------------------------------------*/

378
int usbnet_change_mtu (struct net_device *net, int new_mtu)
Linus Torvalds's avatar
Linus Torvalds committed
379
380
{
	struct usbnet	*dev = netdev_priv(net);
381
	int		ll_mtu = new_mtu + net->hard_header_len;
382
383
	int		old_hard_mtu = dev->hard_mtu;
	int		old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds's avatar
Linus Torvalds committed
384
385

	// no second zero-length packet read wanted after mtu-sized packets
386
	if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
387
388
		return -EDOM;
	net->mtu = new_mtu;
389
390
391
392

	dev->hard_mtu = net->mtu + net->hard_header_len;
	if (dev->rx_urb_size == old_hard_mtu) {
		dev->rx_urb_size = dev->hard_mtu;
Soohoon Lee's avatar
Soohoon Lee committed
393
394
		if (dev->rx_urb_size > old_rx_urb_size) {
			usbnet_pause_rx(dev);
395
			usbnet_unlink_rx_urbs(dev);
Soohoon Lee's avatar
Soohoon Lee committed
396
397
			usbnet_resume_rx(dev);
		}
398
399
	}

400
401
402
	/* max qlen depend on hard_mtu and rx_urb_size */
	usbnet_update_max_qlen(dev);

Linus Torvalds's avatar
Linus Torvalds committed
403
404
	return 0;
}
405
EXPORT_SYMBOL_GPL(usbnet_change_mtu);
Linus Torvalds's avatar
Linus Torvalds committed
406

407
408
409
410
411
412
413
414
415
416
/* The caller must hold list->lock */
static void __usbnet_queue_skb(struct sk_buff_head *list,
			struct sk_buff *newsk, enum skb_state state)
{
	struct skb_data *entry = (struct skb_data *) newsk->cb;

	__skb_queue_tail(list, newsk);
	entry->state = state;
}

Linus Torvalds's avatar
Linus Torvalds committed
417
418
419
420
421
422
/*-------------------------------------------------------------------------*/

/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
 * completion callbacks.  2.5 should have fixed those bugs...
 */

423
424
static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
		struct sk_buff_head *list, enum skb_state state)
Linus Torvalds's avatar
Linus Torvalds committed
425
426
{
	unsigned long		flags;
427
428
	enum skb_state 		old_state;
	struct skb_data *entry = (struct skb_data *) skb->cb;
Linus Torvalds's avatar
Linus Torvalds committed
429

David S. Miller's avatar
David S. Miller committed
430
	spin_lock_irqsave(&list->lock, flags);
431
432
	old_state = entry->state;
	entry->state = state;
David S. Miller's avatar
David S. Miller committed
433
	__skb_unlink(skb, list);
434
435
436
437
438
439
440

	/* defer_bh() is never called with list == &dev->done.
	 * spin_lock_nested() tells lockdep that it is OK to take
	 * dev->done.lock here with list->lock held.
	 */
	spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);

David S. Miller's avatar
David S. Miller committed
441
	__skb_queue_tail(&dev->done, skb);
Linus Torvalds's avatar
Linus Torvalds committed
442
	if (dev->done.qlen == 1)
David S. Miller's avatar
David S. Miller committed
443
		tasklet_schedule(&dev->bh);
444
445
	spin_unlock(&dev->done.lock);
	spin_unlock_irqrestore(&list->lock, flags);
446
	return old_state;
Linus Torvalds's avatar
Linus Torvalds committed
447
448
449
450
451
452
453
}

/* some work can't be done in tasklets, so we use keventd
 *
 * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
 * but tasklet_schedule() doesn't.  hope the failure is rare.
 */
454
void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds's avatar
Linus Torvalds committed
455
456
{
	set_bit (work, &dev->flags);
457
458
459
	if (!schedule_work (&dev->kevent))
		netdev_dbg(dev->net, "kevent %d may have been dropped\n", work);
	else
460
		netdev_dbg(dev->net, "kevent %d scheduled\n", work);
Linus Torvalds's avatar
Linus Torvalds committed
461
}
462
EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds's avatar
Linus Torvalds committed
463
464
465

/*-------------------------------------------------------------------------*/

466
static void rx_complete (struct urb *urb);
Linus Torvalds's avatar
Linus Torvalds committed
467

468
static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds's avatar
Linus Torvalds committed
469
470
471
472
473
{
	struct sk_buff		*skb;
	struct skb_data		*entry;
	int			retval = 0;
	unsigned long		lockflags;
474
	size_t			size = dev->rx_urb_size;
Linus Torvalds's avatar
Linus Torvalds committed
475

476
477
478
479
480
481
	/* prevent rx skb allocation when error ratio is high */
	if (test_bit(EVENT_RX_KILL, &dev->flags)) {
		usb_free_urb(urb);
		return -ENOLINK;
	}

482
483
484
485
	if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags))
		skb = __netdev_alloc_skb(dev->net, size, flags);
	else
		skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
486
	if (!skb) {
487
		netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
488
		usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds's avatar
Linus Torvalds committed
489
		usb_free_urb (urb);
490
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
491
492
493
494
495
496
497
498
499
500
501
502
	}

	entry = (struct skb_data *) skb->cb;
	entry->urb = urb;
	entry->dev = dev;
	entry->length = 0;

	usb_fill_bulk_urb (urb, dev->udev, dev->in,
		skb->data, size, rx_complete, skb);

	spin_lock_irqsave (&dev->rxq.lock, lockflags);

503
504
	if (netif_running (dev->net) &&
	    netif_device_present (dev->net) &&
505
	    test_bit(EVENT_DEV_OPEN, &dev->flags) &&
506
507
	    !test_bit (EVENT_RX_HALT, &dev->flags) &&
	    !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
David Brownell's avatar
David Brownell committed
508
		switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
Linus Torvalds's avatar
Linus Torvalds committed
509
		case -EPIPE:
510
			usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds's avatar
Linus Torvalds committed
511
512
			break;
		case -ENOMEM:
513
			usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds's avatar
Linus Torvalds committed
514
515
			break;
		case -ENODEV:
516
			netif_dbg(dev, ifdown, dev->net, "device gone\n");
Linus Torvalds's avatar
Linus Torvalds committed
517
518
			netif_device_detach (dev->net);
			break;
519
520
521
		case -EHOSTUNREACH:
			retval = -ENOLINK;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
522
		default:
523
524
			netif_dbg(dev, rx_err, dev->net,
				  "rx submit, %d\n", retval);
Linus Torvalds's avatar
Linus Torvalds committed
525
526
527
			tasklet_schedule (&dev->bh);
			break;
		case 0:
528
			__usbnet_queue_skb(&dev->rxq, skb, rx_start);
Linus Torvalds's avatar
Linus Torvalds committed
529
530
		}
	} else {
531
		netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
Linus Torvalds's avatar
Linus Torvalds committed
532
533
534
535
536
537
538
		retval = -ENOLINK;
	}
	spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
	if (retval) {
		dev_kfree_skb_any (skb);
		usb_free_urb (urb);
	}
539
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
540
541
542
543
544
545
546
}


/*-------------------------------------------------------------------------*/

static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
{
547
	if (dev->driver_info->rx_fixup &&
548
549
550
551
552
553
	    !dev->driver_info->rx_fixup (dev, skb)) {
		/* With RX_ASSEMBLE, rx_fixup() must update counters */
		if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
			dev->net->stats.rx_errors++;
		goto done;
	}
Linus Torvalds's avatar
Linus Torvalds committed
554
555
	// else network stack removes extra byte if we forced a short packet

556
557
558
559
560
561
562
563
564
565
	/* all data was already cloned from skb inside the driver */
	if (dev->driver_info->flags & FLAG_MULTI_PACKET)
		goto done;

	if (skb->len < ETH_HLEN) {
		dev->net->stats.rx_errors++;
		dev->net->stats.rx_length_errors++;
		netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
	} else {
		usbnet_skb_return(dev, skb);
566
		return;
Linus Torvalds's avatar
Linus Torvalds committed
567
	}
568

569
done:
570
	skb_queue_tail(&dev->done, skb);
Linus Torvalds's avatar
Linus Torvalds committed
571
572
573
574
}

/*-------------------------------------------------------------------------*/

575
static void rx_complete (struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
576
577
578
579
580
{
	struct sk_buff		*skb = (struct sk_buff *) urb->context;
	struct skb_data		*entry = (struct skb_data *) skb->cb;
	struct usbnet		*dev = entry->dev;
	int			urb_status = urb->status;
581
	enum skb_state		state;
Linus Torvalds's avatar
Linus Torvalds committed
582
583

	skb_put (skb, urb->actual_length);
584
	state = rx_done;
Linus Torvalds's avatar
Linus Torvalds committed
585
586
587
	entry->urb = NULL;

	switch (urb_status) {
David Brownell's avatar
David Brownell committed
588
589
	/* success */
	case 0:
Linus Torvalds's avatar
Linus Torvalds committed
590
591
		break;

David Brownell's avatar
David Brownell committed
592
593
	/* stalls need manual reset. this is rare ... except that
	 * when going through USB 2.0 TTs, unplug appears this way.
594
	 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
David Brownell's avatar
David Brownell committed
595
596
597
	 * storm, recovering as needed.
	 */
	case -EPIPE:
598
		dev->net->stats.rx_errors++;
599
		usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds's avatar
Linus Torvalds committed
600
601
		// FALLTHROUGH

David Brownell's avatar
David Brownell committed
602
603
604
	/* software-driven interface shutdown */
	case -ECONNRESET:		/* async unlink */
	case -ESHUTDOWN:		/* hardware gone */
605
606
		netif_dbg(dev, ifdown, dev->net,
			  "rx shutdown, code %d\n", urb_status);
Linus Torvalds's avatar
Linus Torvalds committed
607
608
		goto block;

609
	/* we get controller i/o faults during hub_wq disconnect() delays.
David Brownell's avatar
David Brownell committed
610
	 * throttle down resubmits, to avoid log floods; just temporarily,
611
	 * so we still recover when the fault isn't a hub_wq delay.
David Brownell's avatar
David Brownell committed
612
613
614
615
	 */
	case -EPROTO:
	case -ETIME:
	case -EILSEQ:
616
		dev->net->stats.rx_errors++;
Linus Torvalds's avatar
Linus Torvalds committed
617
618
		if (!timer_pending (&dev->delay)) {
			mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
619
620
			netif_dbg(dev, link, dev->net,
				  "rx throttle %d\n", urb_status);
Linus Torvalds's avatar
Linus Torvalds committed
621
622
		}
block:
623
		state = rx_cleanup;
Linus Torvalds's avatar
Linus Torvalds committed
624
625
626
627
		entry->urb = urb;
		urb = NULL;
		break;

David Brownell's avatar
David Brownell committed
628
629
	/* data overrun ... flush fifo? */
	case -EOVERFLOW:
630
		dev->net->stats.rx_over_errors++;
Linus Torvalds's avatar
Linus Torvalds committed
631
		// FALLTHROUGH
632

David Brownell's avatar
David Brownell committed
633
	default:
634
		state = rx_cleanup;
635
		dev->net->stats.rx_errors++;
636
		netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
Linus Torvalds's avatar
Linus Torvalds committed
637
638
639
		break;
	}

640
641
642
643
644
645
646
647
648
649
650
	/* stop rx if packet error rate is high */
	if (++dev->pkt_cnt > 30) {
		dev->pkt_cnt = 0;
		dev->pkt_err = 0;
	} else {
		if (state == rx_cleanup)
			dev->pkt_err++;
		if (dev->pkt_err > 20)
			set_bit(EVENT_RX_KILL, &dev->flags);
	}

651
	state = defer_bh(dev, skb, &dev->rxq, state);
Linus Torvalds's avatar
Linus Torvalds committed
652
653

	if (urb) {
654
		if (netif_running (dev->net) &&
655
656
		    !test_bit (EVENT_RX_HALT, &dev->flags) &&
		    state != unlink_start) {
Linus Torvalds's avatar
Linus Torvalds committed
657
			rx_submit (dev, urb, GFP_ATOMIC);
658
			usb_mark_last_busy(dev->udev);
Linus Torvalds's avatar
Linus Torvalds committed
659
660
661
662
			return;
		}
		usb_free_urb (urb);
	}
663
	netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
Linus Torvalds's avatar
Linus Torvalds committed
664
665
}

Jussi Kivilinna's avatar
Jussi Kivilinna committed
666
667
668
669
670
/*-------------------------------------------------------------------------*/
void usbnet_pause_rx(struct usbnet *dev)
{
	set_bit(EVENT_RX_PAUSED, &dev->flags);

671
	netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
Jussi Kivilinna's avatar
Jussi Kivilinna committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
}
EXPORT_SYMBOL_GPL(usbnet_pause_rx);

void usbnet_resume_rx(struct usbnet *dev)
{
	struct sk_buff *skb;
	int num = 0;

	clear_bit(EVENT_RX_PAUSED, &dev->flags);

	while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
		usbnet_skb_return(dev, skb);
		num++;
	}

	tasklet_schedule(&dev->bh);

689
690
	netif_dbg(dev, rx_status, dev->net,
		  "paused rx queue disabled, %d skbs requeued\n", num);
Jussi Kivilinna's avatar
Jussi Kivilinna committed
691
692
693
694
695
696
697
698
699
}
EXPORT_SYMBOL_GPL(usbnet_resume_rx);

void usbnet_purge_paused_rxq(struct usbnet *dev)
{
	skb_queue_purge(&dev->rxq_pause);
}
EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);

Linus Torvalds's avatar
Linus Torvalds committed
700
701
702
703
704
705
706
/*-------------------------------------------------------------------------*/

// unlink pending rx/tx; completion handlers do all other cleanup

static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
{
	unsigned long		flags;
707
	struct sk_buff		*skb;
Linus Torvalds's avatar
Linus Torvalds committed
708
709
710
	int			count = 0;

	spin_lock_irqsave (&q->lock, flags);
711
	while (!skb_queue_empty(q)) {
Linus Torvalds's avatar
Linus Torvalds committed
712
713
714
715
		struct skb_data		*entry;
		struct urb		*urb;
		int			retval;

716
717
718
719
720
721
722
723
		skb_queue_walk(q, skb) {
			entry = (struct skb_data *) skb->cb;
			if (entry->state != unlink_start)
				goto found;
		}
		break;
found:
		entry->state = unlink_start;
Linus Torvalds's avatar
Linus Torvalds committed
724
725
		urb = entry->urb;

726
727
728
729
730
731
732
733
		/*
		 * Get reference count of the URB to avoid it to be
		 * freed during usb_unlink_urb, which may trigger
		 * use-after-free problem inside usb_unlink_urb since
		 * usb_unlink_urb is always racing with .complete
		 * handler(include defer_bh).
		 */
		usb_get_urb(urb);
734
		spin_unlock_irqrestore(&q->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
735
736
737
738
		// during some PM-driven resume scenarios,
		// these (async) unlinks complete immediately
		retval = usb_unlink_urb (urb);
		if (retval != -EINPROGRESS && retval != 0)
739
			netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
Linus Torvalds's avatar
Linus Torvalds committed
740
741
		else
			count++;
742
		usb_put_urb(urb);
743
		spin_lock_irqsave(&q->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
744
745
746
747
748
	}
	spin_unlock_irqrestore (&q->lock, flags);
	return count;
}

749
750
751
752
753
754
755
756
757
758
759
// Flush all pending rx urbs
// minidrivers may need to do this when the MTU changes

void usbnet_unlink_rx_urbs(struct usbnet *dev)
{
	if (netif_running(dev->net)) {
		(void) unlink_urbs (dev, &dev->rxq);
		tasklet_schedule(&dev->bh);
	}
}
EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
Linus Torvalds's avatar
Linus Torvalds committed
760
761
762

/*-------------------------------------------------------------------------*/

763
764
765
766
767
768
769
770
771
772
773
774
775
776
static void wait_skb_queue_empty(struct sk_buff_head *q)
{
	unsigned long flags;

	spin_lock_irqsave(&q->lock, flags);
	while (!skb_queue_empty(q)) {
		spin_unlock_irqrestore(&q->lock, flags);
		schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
		set_current_state(TASK_UNINTERRUPTIBLE);
		spin_lock_irqsave(&q->lock, flags);
	}
	spin_unlock_irqrestore(&q->lock, flags);
}

Linus Torvalds's avatar
Linus Torvalds committed
777
// precondition: never called in_interrupt
778
779
780
781
782
783
static void usbnet_terminate_urbs(struct usbnet *dev)
{
	DECLARE_WAITQUEUE(wait, current);
	int temp;

	/* ensure there are no more active urbs */
784
	add_wait_queue(&dev->wait, &wait);
785
786
787
788
789
	set_current_state(TASK_UNINTERRUPTIBLE);
	temp = unlink_urbs(dev, &dev->txq) +
		unlink_urbs(dev, &dev->rxq);

	/* maybe wait for deletions to finish. */
790
791
792
793
794
	wait_skb_queue_empty(&dev->rxq);
	wait_skb_queue_empty(&dev->txq);
	wait_skb_queue_empty(&dev->done);
	netif_dbg(dev, ifdown, dev->net,
		  "waited for %d urb completions\n", temp);
795
	set_current_state(TASK_RUNNING);
796
	remove_wait_queue(&dev->wait, &wait);
797
}
Linus Torvalds's avatar
Linus Torvalds committed
798

799
int usbnet_stop (struct net_device *net)
Linus Torvalds's avatar
Linus Torvalds committed
800
801
{
	struct usbnet		*dev = netdev_priv(net);
802
	const struct driver_info *info = dev->driver_info;
803
	int			retval, pm, mpn;
Linus Torvalds's avatar
Linus Torvalds committed
804

805
	clear_bit(EVENT_DEV_OPEN, &dev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
806
807
	netif_stop_queue (net);

808
	netif_info(dev, ifdown, dev->net,
809
		   "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
810
811
		   net->stats.rx_packets, net->stats.tx_packets,
		   net->stats.rx_errors, net->stats.tx_errors);
Linus Torvalds's avatar
Linus Torvalds committed
812

813
814
	/* to not race resume */
	pm = usb_autopm_get_interface(dev->intf);
815
816
817
818
	/* allow minidriver to stop correctly (wireless devices to turn off
	 * radio etc) */
	if (info->stop) {
		retval = info->stop(dev);
819
820
821
822
823
824
		if (retval < 0)
			netif_info(dev, ifdown, dev->net,
				   "stop fail (%d) usbnet usb-%s-%s, %s\n",
				   retval,
				   dev->udev->bus->bus_name, dev->udev->devpath,
				   info->description);
825
826
	}

827
828
	if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
		usbnet_terminate_urbs(dev);
Linus Torvalds's avatar
Linus Torvalds committed
829

830
	usbnet_status_stop(dev);
Linus Torvalds's avatar
Linus Torvalds committed
831

Jussi Kivilinna's avatar
Jussi Kivilinna committed
832
833
	usbnet_purge_paused_rxq(dev);

834
835
	mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);

Linus Torvalds's avatar
Linus Torvalds committed
836
837
838
839
840
841
842
	/* deferred work (task, timer, softirq) must also stop.
	 * can't flush_scheduled_work() until we drop rtnl (later),
	 * else workers could deadlock; so make workers a NOP.
	 */
	dev->flags = 0;
	del_timer_sync (&dev->delay);
	tasklet_kill (&dev->bh);
843
844
845
	if (!pm)
		usb_autopm_put_interface(dev->intf);

846
	if (info->manage_power && mpn)
847
848
849
		info->manage_power(dev, 0);
	else
		usb_autopm_put_interface(dev->intf);
Linus Torvalds's avatar
Linus Torvalds committed
850
851
852

	return 0;
}
853
EXPORT_SYMBOL_GPL(usbnet_stop);
Linus Torvalds's avatar
Linus Torvalds committed
854
855
856
857
858
859
860

/*-------------------------------------------------------------------------*/

// posts reads, and enables write queuing

// precondition: never called in_interrupt

861
int usbnet_open (struct net_device *net)
Linus Torvalds's avatar
Linus Torvalds committed
862
863
{
	struct usbnet		*dev = netdev_priv(net);
864
	int			retval;
865
	const struct driver_info *info = dev->driver_info;
Linus Torvalds's avatar
Linus Torvalds committed
866

867
	if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
868
869
870
871
872
873
		netif_info(dev, ifup, dev->net,
			   "resumption fail (%d) usbnet usb-%s-%s, %s\n",
			   retval,
			   dev->udev->bus->bus_name,
			   dev->udev->devpath,
			   info->description);
874
875
876
		goto done_nopm;
	}

Linus Torvalds's avatar
Linus Torvalds committed
877
878
	// put into "known safe" state
	if (info->reset && (retval = info->reset (dev)) < 0) {
879
880
881
882
883
884
		netif_info(dev, ifup, dev->net,
			   "open reset fail (%d) usbnet usb-%s-%s, %s\n",
			   retval,
			   dev->udev->bus->bus_name,
			   dev->udev->devpath,
			   info->description);
Linus Torvalds's avatar
Linus Torvalds committed
885
886
887
		goto done;
	}

888
889
890
	/* hard_mtu or rx_urb_size may change in reset() */
	usbnet_update_max_qlen(dev);

Linus Torvalds's avatar
Linus Torvalds committed
891
892
	// insist peer be connected
	if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
893
		netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
Linus Torvalds's avatar
Linus Torvalds committed
894
895
896
897
898
		goto done;
	}

	/* start any status interrupt transfer */
	if (dev->interrupt) {
899
		retval = usbnet_status_start(dev, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
900
		if (retval < 0) {
901
902
			netif_err(dev, ifup, dev->net,
				  "intr submit %d\n", retval);
Linus Torvalds's avatar
Linus Torvalds committed
903
904
905
906
			goto done;
		}
	}

907
	set_bit(EVENT_DEV_OPEN, &dev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
908
	netif_start_queue (net);
909
910
911
912
913
914
915
916
917
918
	netif_info(dev, ifup, dev->net,
		   "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
		   (int)RX_QLEN(dev), (int)TX_QLEN(dev),
		   dev->net->mtu,
		   (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
		   (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
		   (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
		   (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
		   (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
		   "simple");
Linus Torvalds's avatar
Linus Torvalds committed
919

920
921
922
923
924
	/* reset rx error state */
	dev->pkt_cnt = 0;
	dev->pkt_err = 0;
	clear_bit(EVENT_RX_KILL, &dev->flags);

Linus Torvalds's avatar
Linus Torvalds committed
925
926
	// delay posting reads until we're fully open
	tasklet_schedule (&dev->bh);
927
928
	if (info->manage_power) {
		retval = info->manage_power(dev, 1);
929
930
931
932
933
934
		if (retval < 0) {
			retval = 0;
			set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
		} else {
			usb_autopm_put_interface(dev->intf);
		}
935
	}
936
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
937
done:
938
939
	usb_autopm_put_interface(dev->intf);
done_nopm:
Linus Torvalds's avatar
Linus Torvalds committed
940
941
	return retval;
}
942
EXPORT_SYMBOL_GPL(usbnet_open);
Linus Torvalds's avatar
Linus Torvalds committed
943
944
945

/*-------------------------------------------------------------------------*/

946
947
948
949
/* ethtool methods; minidrivers may need to add some more, but
 * they'll probably want to use this base set.
 */

950
951
952
953
954
955
956
957
int usbnet_get_link_ksettings(struct net_device *net,
			      struct ethtool_link_ksettings *cmd)
{
	struct usbnet *dev = netdev_priv(net);

	if (!dev->mii.mdio_read)
		return -EOPNOTSUPP;

958
959
960
	mii_ethtool_get_link_ksettings(&dev->mii, cmd);

	return 0;
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
}
EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings);

int usbnet_set_link_ksettings(struct net_device *net,
			      const struct ethtool_link_ksettings *cmd)
{
	struct usbnet *dev = netdev_priv(net);
	int retval;

	if (!dev->mii.mdio_write)
		return -EOPNOTSUPP;

	retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd);

	/* link speed/duplex might have changed */
	if (dev->driver_info->link_reset)
		dev->driver_info->link_reset(dev);

	/* hard_mtu or rx_urb_size may change in link_reset() */
	usbnet_update_max_qlen(dev);

	return retval;
}
EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings);

986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
void usbnet_get_stats64(struct net_device *net, struct rtnl_link_stats64 *stats)
{
	struct usbnet *dev = netdev_priv(net);
	unsigned int start;
	int cpu;

	netdev_stats_to_stats64(stats, &net->stats);

	for_each_possible_cpu(cpu) {
		struct pcpu_sw_netstats *stats64;
		u64 rx_packets, rx_bytes;
		u64 tx_packets, tx_bytes;

		stats64 = per_cpu_ptr(dev->stats64, cpu);