summaryrefslogtreecommitdiff
path: root/drivers/net/mctp/mctp-usb.c
blob: 542a570c76cc98d6715fdd3fc72e0343a9e0a5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// SPDX-License-Identifier: GPL-2.0
/*
 * mctp-usb.c - MCTP-over-USB (DMTF DSP0283) transport binding driver.
 *
 * DSP0283 is available at:
 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0283_1.1.0.pdf
 *
 * Copyright (C) 2024-2026 Code Construct Pty Ltd
 */

#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/usb.h>
#include <linux/usb/mctp-usb.h>

#include <net/mctp.h>
#include <net/mctpdevice.h>
#include <net/pkt_sched.h>

#include <uapi/linux/if_arp.h>

struct mctp_usb {
	struct usb_device *usbdev;
	struct usb_interface *intf;
	bool span;

	struct net_device *netdev;

	u8 ep_in;
	u8 ep_out;

	struct mctp_usblib_rx rx;
	struct urb *rx_urb;
	int in_err_count;
	int in_err_orig;
	bool clear_halt;

	/* enforces atomic access to rx_stopped and requeuing the retry work */
	spinlock_t rx_lock;
	bool rx_stopped;
	struct delayed_work rx_retry_work;

	struct mctp_usblib_tx tx;
	struct usb_anchor tx_anchor;
	/* serialises tx_qmem updates to netdev queue states */
	spinlock_t tx_qmem_lock;
	int tx_qmem;
};

enum {
	MCTP_USB_SUBCLASS_BASE = 0x00,
	MCTP_USB_SUBCLASS_SPAN = 0x02,
};

/* We use a total-size limit for outstanding URBs, as the transfer counts
 * may vary a lot between spanning- and non-spanning modes. In spanning mode,
 * this will allow for a couple of max-sized transfers to be in flight. In
 * non-spanning mode, 32.
 *
 * We want to avoid disabling the tx queue if possible; doing so will end up
 * requeueing to gso_skb, and we only dequeue from that one skb at a time,
 * so can no longer perform transfer packing.
 */
static const unsigned int TX_QMEM_MAX = 16384;

static void mctp_usb_out_complete(struct urb *urb)
{
	struct mctp_usblib_tx_ctx *tx_ctx = urb->context;
	struct mctp_usb *mctp_usb = mctp_usblib_tx_ctx_priv(tx_ctx);
	unsigned int len = urb->transfer_buffer_length;
	struct net_device *netdev = mctp_usb->netdev;
	unsigned long flags;

	mctp_usblib_tx_send_complete(tx_ctx, netdev, urb->status == 0);

	usb_free_urb(urb);

	spin_lock_irqsave(&mctp_usb->tx_qmem_lock, flags);
	mctp_usb->tx_qmem -= len;
	if (mctp_usb->tx_qmem < TX_QMEM_MAX && netif_running(netdev))
		netif_wake_queue(netdev);
	spin_unlock_irqrestore(&mctp_usb->tx_qmem_lock, flags);
}

static int mctp_usb_tx_send(struct mctp_usblib_tx_ctx *tx_ctx,
			    void *data, size_t len)
{
	struct mctp_usb *mctp_usb = mctp_usblib_tx_ctx_priv(tx_ctx);
	unsigned long flags;
	struct urb *urb;
	int rc;

	urb = usb_alloc_urb(0, GFP_ATOMIC);
	if (!urb)
		return -ENOMEM;

	usb_fill_bulk_urb(urb, mctp_usb->usbdev,
			  usb_sndbulkpipe(mctp_usb->usbdev, mctp_usb->ep_out),
			  data, len, mctp_usb_out_complete, tx_ctx);

	if (mctp_usb->span)
		urb->transfer_flags |= URB_ZERO_PACKET;

	usb_anchor_urb(urb, &mctp_usb->tx_anchor);

	rc = usb_submit_urb(urb, GFP_ATOMIC);
	if (rc) {
		netdev_dbg(mctp_usb->netdev, "TX urb submit failed, %d\n", rc);
		usb_unanchor_urb(urb);
		usb_free_urb(urb);
	} else {
		spin_lock_irqsave(&mctp_usb->tx_qmem_lock, flags);
		mctp_usb->tx_qmem += len;
		if (mctp_usb->tx_qmem >= TX_QMEM_MAX)
			netif_stop_queue(mctp_usb->netdev);
		spin_unlock_irqrestore(&mctp_usb->tx_qmem_lock, flags);
	}

	return rc;
}

static const struct mctp_usblib_tx_ops tx_ops = {
	.send = mctp_usb_tx_send,
};

static netdev_tx_t mctp_usb_start_xmit(struct sk_buff *skb,
				       struct net_device *dev)
{
	struct mctp_usb *mctp_usb = netdev_priv(dev);
	bool more = netdev_xmit_more();

	mctp_usblib_tx_push(dev, &mctp_usb->tx, skb, more);

	return NETDEV_TX_OK;
}

static void mctp_usb_in_complete(struct urb *urb);

/* If we fail to queue an in urb atomically (either due to skb allocation or
 * urb submission), we will schedule a rx queue in nonatomic context
 * after a delay, specified in jiffies
 */
static const unsigned long RX_RETRY_DELAY = HZ / 4;

static int mctp_usb_rx_queue(struct mctp_usb *mctp_usb, gfp_t gfp)
{
	unsigned long flags;
	size_t len;
	void *buf;
	int rc;

	rc = mctp_usblib_rx_prepare(mctp_usb->netdev, &mctp_usb->rx,
				    &buf, &len, gfp);
	if (rc)
		goto err_retry;

	usb_fill_bulk_urb(mctp_usb->rx_urb, mctp_usb->usbdev,
			  usb_rcvbulkpipe(mctp_usb->usbdev, mctp_usb->ep_in),
			  buf, len, mctp_usb_in_complete, mctp_usb);

	rc = usb_submit_urb(mctp_usb->rx_urb, gfp);
	if (rc) {
		netdev_dbg(mctp_usb->netdev, "rx urb submit failure: %d\n", rc);
		mctp_usblib_rx_cancel(&mctp_usb->rx);
		if (rc == -ENOMEM)
			goto err_retry;
	}

	return rc;

err_retry:
	spin_lock_irqsave(&mctp_usb->rx_lock, flags);
	if (!mctp_usb->rx_stopped)
		schedule_delayed_work(&mctp_usb->rx_retry_work, RX_RETRY_DELAY);
	spin_unlock_irqrestore(&mctp_usb->rx_lock, flags);
	return 0;
}

static const unsigned int rx_err_max = 10;

/* Returns -1 if we have hit excessive errors, zero otherwise. */
static int mctp_usb_in_urb_err(struct mctp_usb *mctp_usb, int status,
			       bool stalled)
{
	mctp_usblib_rx_cancel(&mctp_usb->rx);

	if (!mctp_usb->in_err_count++)
		mctp_usb->in_err_orig = status;

	if (mctp_usb->in_err_count >= rx_err_max) {
		netdev_err(mctp_usb->netdev,
			   "excessive errors from%s IN EP, first: %d\n",
			   stalled ? " (stalled)" : "",
			   mctp_usb->in_err_orig);
		return -1;
	}

	return 0;
}

static void mctp_usb_in_complete(struct urb *urb)
{
	struct mctp_usb *mctp_usb = urb->context;
	struct net_device *netdev = mctp_usb->netdev;
	unsigned long flags;
	int rc, status;

	status = urb->status;

	switch (status) {
	case -ENOENT:
	case -ECONNRESET:
	case -ESHUTDOWN:
		/* device shutdown, don't resubmit */
		mctp_usblib_rx_cancel(&mctp_usb->rx);
		return;

	case -EPIPE:
		/* endpoint stall: clear halt, which will cause a resubmit */
		rc = mctp_usb_in_urb_err(mctp_usb, status, true);
		if (rc)
			return;

		mctp_usb->clear_halt = true;
		spin_lock_irqsave(&mctp_usb->rx_lock, flags);
		if (!mctp_usb->rx_stopped)
			schedule_delayed_work(&mctp_usb->rx_retry_work,
					      RX_RETRY_DELAY);
		spin_unlock_irqrestore(&mctp_usb->rx_lock, flags);
		return;

	default:
		netdev_dbg(netdev, "unexpected rx urb status: %d\n", status);
		fallthrough;
	case -ETIME:
	case -EPROTO:
	case -EILSEQ:
	case -EOVERFLOW:
		/* possibly transient; record first failure, resubmit */
		rc = mctp_usb_in_urb_err(mctp_usb, status, false);
		if (rc)
			return;
		break;

	case 0:
		mctp_usblib_rx_complete(netdev, &mctp_usb->rx, urb->actual_length);
		mctp_usb->in_err_count = 0;
		break;
	}

	mctp_usb_rx_queue(mctp_usb, GFP_ATOMIC);
}

static void mctp_usb_rx_retry_work(struct work_struct *work)
{
	struct mctp_usb *mctp_usb = container_of(work, struct mctp_usb,
						 rx_retry_work.work);
	unsigned long flags;
	int rc;

	/* We are only called when rx completions are suspended */
	if (mctp_usb->clear_halt) {
		int pipe = usb_rcvbulkpipe(mctp_usb->usbdev, mctp_usb->ep_in);

		rc = usb_clear_halt(mctp_usb->usbdev, pipe);
		if (rc) {
			netdev_err(mctp_usb->netdev,
				   "can't clear IN EP halt: %d\n", rc);

			if (++mctp_usb->in_err_count >= rx_err_max)
				return;

			spin_lock_irqsave(&mctp_usb->rx_lock, flags);
			if (!mctp_usb->rx_stopped)
				schedule_delayed_work(&mctp_usb->rx_retry_work,
						      RX_RETRY_DELAY);
			spin_unlock_irqrestore(&mctp_usb->rx_lock, flags);
			return;
		}
		mctp_usb->clear_halt = false;
	}

	mctp_usb_rx_queue(mctp_usb, GFP_KERNEL);
}

static int mctp_usb_open(struct net_device *dev)
{
	struct mctp_usb *mctp_usb = netdev_priv(dev);

	WRITE_ONCE(mctp_usb->rx_stopped, false);
	mctp_usb->clear_halt = false;
	mctp_usb->in_err_count = 0;

	netif_start_queue(dev);

	return mctp_usb_rx_queue(mctp_usb, GFP_KERNEL);
}

static int mctp_usb_stop(struct net_device *dev)
{
	struct mctp_usb *mctp_usb = netdev_priv(dev);
	unsigned long flags;

	netif_stop_queue(dev);

	/* prevent RX submission retry */
	spin_lock_irqsave(&mctp_usb->rx_lock, flags);
	mctp_usb->rx_stopped = true;
	cancel_delayed_work(&mctp_usb->rx_retry_work);
	spin_unlock_irqrestore(&mctp_usb->rx_lock, flags);

	flush_delayed_work(&mctp_usb->rx_retry_work);

	usb_kill_urb(mctp_usb->rx_urb);

	usb_kill_anchored_urbs(&mctp_usb->tx_anchor);

	mctp_usblib_tx_cancel(&mctp_usb->tx, dev, SKB_DROP_REASON_DEV_READY);
	mctp_usblib_rx_cancel(&mctp_usb->rx);

	return 0;
}

static const struct net_device_ops mctp_usb_netdev_ops = {
	.ndo_start_xmit = mctp_usb_start_xmit,
	.ndo_open = mctp_usb_open,
	.ndo_stop = mctp_usb_stop,
};

static void mctp_usb_netdev_setup(struct net_device *dev)
{
	dev->type = ARPHRD_MCTP;

	dev->mtu = MCTP_USB_MTU_MIN;
	dev->min_mtu = MCTP_USB_MTU_MIN;
	dev->max_mtu = MCTP_USB_1_0_MTU_MAX;

	dev->hard_header_len = sizeof(struct mctp_usb_hdr);
	dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
	dev->flags = IFF_NOARP;
	dev->netdev_ops = &mctp_usb_netdev_ops;
	dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS;
}

static int mctp_usb_probe(struct usb_interface *intf,
			  const struct usb_device_id *id)
{
	struct usb_endpoint_descriptor *ep_in, *ep_out;
	struct usb_host_interface *iface_desc;
	struct net_device *netdev;
	struct mctp_usb *dev;
	bool span;
	int rc;

	/* only one alternate */
	iface_desc = intf->cur_altsetting;

	rc = usb_find_common_endpoints(iface_desc, &ep_in, &ep_out, NULL, NULL);
	if (rc) {
		dev_err(&intf->dev, "invalid endpoints on device?\n");
		return rc;
	}

	span = iface_desc->desc.bInterfaceSubClass == MCTP_USB_SUBCLASS_SPAN;

	netdev = alloc_netdev(sizeof(*dev), "mctpusb%d", NET_NAME_ENUM,
			      mctp_usb_netdev_setup);
	if (!netdev)
		return -ENOMEM;

	SET_NETDEV_DEV(netdev, &intf->dev);
	dev = netdev_priv(netdev);
	dev->span = span;
	dev->netdev = netdev;
	dev->usbdev = interface_to_usbdev(intf);
	dev->intf = intf;
	spin_lock_init(&dev->rx_lock);
	if (dev->span)
		netdev->max_mtu = MCTP_USB_1_1_MTU_MAX;
	spin_lock_init(&dev->tx_qmem_lock);
	usb_set_intfdata(intf, dev);

	rc = mctp_usblib_rx_init(&dev->rx, le16_to_cpu(ep_in->wMaxPacketSize),
				 dev->span);
	if (rc)
		goto err_free_netdev;
	mctp_usblib_tx_init(&dev->tx, &tx_ops, dev, dev->span);
	init_usb_anchor(&dev->tx_anchor);

	dev->ep_in = ep_in->bEndpointAddress;
	dev->ep_out = ep_out->bEndpointAddress;

	dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!dev->rx_urb) {
		rc = -ENOMEM;
		goto err_fini_rxtx;
	}

	INIT_DELAYED_WORK(&dev->rx_retry_work, mctp_usb_rx_retry_work);

	rc = mctp_register_netdev(netdev, NULL, MCTP_PHYS_BINDING_USB);
	if (rc)
		goto err_free_urb;

	return 0;

err_free_urb:
	usb_free_urb(dev->rx_urb);
err_fini_rxtx:
	mctp_usblib_tx_fini(&dev->tx);
	mctp_usblib_rx_fini(&dev->rx);
err_free_netdev:
	free_netdev(netdev);
	return rc;
}

static void mctp_usb_disconnect(struct usb_interface *intf)
{
	struct mctp_usb *dev = usb_get_intfdata(intf);

	mctp_unregister_netdev(dev->netdev);
	mctp_usblib_rx_fini(&dev->rx);
	mctp_usblib_tx_fini(&dev->tx);
	usb_free_urb(dev->rx_urb);
	free_netdev(dev->netdev);
}

static const struct usb_device_id mctp_usb_devices[] = {
	{ USB_INTERFACE_INFO(USB_CLASS_MCTP, MCTP_USB_SUBCLASS_BASE, 0x1) },
	{ USB_INTERFACE_INFO(USB_CLASS_MCTP, MCTP_USB_SUBCLASS_SPAN, 0x1) },
	{ 0 },
};

MODULE_DEVICE_TABLE(usb, mctp_usb_devices);

static struct usb_driver mctp_usb_driver = {
	.name		= "mctp-usb",
	.id_table	= mctp_usb_devices,
	.probe		= mctp_usb_probe,
	.disconnect	= mctp_usb_disconnect,
};

module_usb_driver(mctp_usb_driver)

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jeremy Kerr <jk@codeconstruct.com.au>");
MODULE_DESCRIPTION("MCTP USB transport");