1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * mctp-usb.c - MCTP-over-USB (DMTF DSP0283) transport binding driver.
4 *
5 * DSP0283 is available at:
6 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0283_1.1.0.pdf
7 *
8 * Copyright (C) 2024-2026 Code Construct Pty Ltd
9 */
10
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/usb.h>
14 #include <linux/usb/mctp-usb.h>
15
16 #include <net/mctp.h>
17 #include <net/mctpdevice.h>
18 #include <net/pkt_sched.h>
19
20 #include <uapi/linux/if_arp.h>
21
22 struct mctp_usb {
23 struct usb_device *usbdev;
24 struct usb_interface *intf;
25 bool span;
26
27 struct net_device *netdev;
28
29 u8 ep_in;
30 u8 ep_out;
31
32 struct mctp_usblib_rx rx;
33 struct urb *rx_urb;
34 int in_err_count;
35 int in_err_orig;
36 bool clear_halt;
37
38 /* enforces atomic access to rx_stopped and requeuing the retry work */
39 spinlock_t rx_lock;
40 bool rx_stopped;
41 struct delayed_work rx_retry_work;
42
43 struct mctp_usblib_tx tx;
44 struct usb_anchor tx_anchor;
45 /* serialises tx_qmem updates to netdev queue states */
46 spinlock_t tx_qmem_lock;
47 int tx_qmem;
48 };
49
50 enum {
51 MCTP_USB_SUBCLASS_BASE = 0x00,
52 MCTP_USB_SUBCLASS_SPAN = 0x02,
53 };
54
55 /* We use a total-size limit for outstanding URBs, as the transfer counts
56 * may vary a lot between spanning- and non-spanning modes. In spanning mode,
57 * this will allow for a couple of max-sized transfers to be in flight. In
58 * non-spanning mode, 32.
59 *
60 * We want to avoid disabling the tx queue if possible; doing so will end up
61 * requeueing to gso_skb, and we only dequeue from that one skb at a time,
62 * so can no longer perform transfer packing.
63 */
64 static const unsigned int TX_QMEM_MAX = 16384;
65
mctp_usb_out_complete(struct urb * urb)66 static void mctp_usb_out_complete(struct urb *urb)
67 {
68 struct mctp_usblib_tx_ctx *tx_ctx = urb->context;
69 struct mctp_usb *mctp_usb = mctp_usblib_tx_ctx_priv(tx_ctx);
70 unsigned int len = urb->transfer_buffer_length;
71 struct net_device *netdev = mctp_usb->netdev;
72 unsigned long flags;
73
74 mctp_usblib_tx_send_complete(tx_ctx, netdev, urb->status == 0);
75
76 usb_free_urb(urb);
77
78 spin_lock_irqsave(&mctp_usb->tx_qmem_lock, flags);
79 mctp_usb->tx_qmem -= len;
80 if (mctp_usb->tx_qmem < TX_QMEM_MAX && netif_running(netdev))
81 netif_wake_queue(netdev);
82 spin_unlock_irqrestore(&mctp_usb->tx_qmem_lock, flags);
83 }
84
mctp_usb_tx_send(struct mctp_usblib_tx_ctx * tx_ctx,void * data,size_t len)85 static int mctp_usb_tx_send(struct mctp_usblib_tx_ctx *tx_ctx,
86 void *data, size_t len)
87 {
88 struct mctp_usb *mctp_usb = mctp_usblib_tx_ctx_priv(tx_ctx);
89 unsigned long flags;
90 struct urb *urb;
91 int rc;
92
93 urb = usb_alloc_urb(0, GFP_ATOMIC);
94 if (!urb)
95 return -ENOMEM;
96
97 usb_fill_bulk_urb(urb, mctp_usb->usbdev,
98 usb_sndbulkpipe(mctp_usb->usbdev, mctp_usb->ep_out),
99 data, len, mctp_usb_out_complete, tx_ctx);
100
101 if (mctp_usb->span)
102 urb->transfer_flags |= URB_ZERO_PACKET;
103
104 usb_anchor_urb(urb, &mctp_usb->tx_anchor);
105
106 rc = usb_submit_urb(urb, GFP_ATOMIC);
107 if (rc) {
108 netdev_dbg(mctp_usb->netdev, "TX urb submit failed, %d\n", rc);
109 usb_unanchor_urb(urb);
110 usb_free_urb(urb);
111 } else {
112 spin_lock_irqsave(&mctp_usb->tx_qmem_lock, flags);
113 mctp_usb->tx_qmem += len;
114 if (mctp_usb->tx_qmem >= TX_QMEM_MAX)
115 netif_stop_queue(mctp_usb->netdev);
116 spin_unlock_irqrestore(&mctp_usb->tx_qmem_lock, flags);
117 }
118
119 return rc;
120 }
121
122 static const struct mctp_usblib_tx_ops tx_ops = {
123 .send = mctp_usb_tx_send,
124 };
125
mctp_usb_start_xmit(struct sk_buff * skb,struct net_device * dev)126 static netdev_tx_t mctp_usb_start_xmit(struct sk_buff *skb,
127 struct net_device *dev)
128 {
129 struct mctp_usb *mctp_usb = netdev_priv(dev);
130 bool more = netdev_xmit_more();
131
132 mctp_usblib_tx_push(dev, &mctp_usb->tx, skb, more);
133
134 return NETDEV_TX_OK;
135 }
136
137 static void mctp_usb_in_complete(struct urb *urb);
138
139 /* If we fail to queue an in urb atomically (either due to skb allocation or
140 * urb submission), we will schedule a rx queue in nonatomic context
141 * after a delay, specified in jiffies
142 */
143 static const unsigned long RX_RETRY_DELAY = HZ / 4;
144
mctp_usb_rx_queue(struct mctp_usb * mctp_usb,gfp_t gfp)145 static int mctp_usb_rx_queue(struct mctp_usb *mctp_usb, gfp_t gfp)
146 {
147 unsigned long flags;
148 size_t len;
149 void *buf;
150 int rc;
151
152 rc = mctp_usblib_rx_prepare(mctp_usb->netdev, &mctp_usb->rx,
153 &buf, &len, gfp);
154 if (rc)
155 goto err_retry;
156
157 usb_fill_bulk_urb(mctp_usb->rx_urb, mctp_usb->usbdev,
158 usb_rcvbulkpipe(mctp_usb->usbdev, mctp_usb->ep_in),
159 buf, len, mctp_usb_in_complete, mctp_usb);
160
161 rc = usb_submit_urb(mctp_usb->rx_urb, gfp);
162 if (rc) {
163 netdev_dbg(mctp_usb->netdev, "rx urb submit failure: %d\n", rc);
164 mctp_usblib_rx_cancel(&mctp_usb->rx);
165 if (rc == -ENOMEM)
166 goto err_retry;
167 }
168
169 return rc;
170
171 err_retry:
172 spin_lock_irqsave(&mctp_usb->rx_lock, flags);
173 if (!mctp_usb->rx_stopped)
174 schedule_delayed_work(&mctp_usb->rx_retry_work, RX_RETRY_DELAY);
175 spin_unlock_irqrestore(&mctp_usb->rx_lock, flags);
176 return 0;
177 }
178
179 static const unsigned int rx_err_max = 10;
180
181 /* Returns -1 if we have hit excessive errors, zero otherwise. */
mctp_usb_in_urb_err(struct mctp_usb * mctp_usb,int status,bool stalled)182 static int mctp_usb_in_urb_err(struct mctp_usb *mctp_usb, int status,
183 bool stalled)
184 {
185 mctp_usblib_rx_cancel(&mctp_usb->rx);
186
187 if (!mctp_usb->in_err_count++)
188 mctp_usb->in_err_orig = status;
189
190 if (mctp_usb->in_err_count >= rx_err_max) {
191 netdev_err(mctp_usb->netdev,
192 "excessive errors from%s IN EP, first: %d\n",
193 stalled ? " (stalled)" : "",
194 mctp_usb->in_err_orig);
195 return -1;
196 }
197
198 return 0;
199 }
200
mctp_usb_in_complete(struct urb * urb)201 static void mctp_usb_in_complete(struct urb *urb)
202 {
203 struct mctp_usb *mctp_usb = urb->context;
204 struct net_device *netdev = mctp_usb->netdev;
205 unsigned long flags;
206 int rc, status;
207
208 status = urb->status;
209
210 switch (status) {
211 case -ENOENT:
212 case -ECONNRESET:
213 case -ESHUTDOWN:
214 /* device shutdown, don't resubmit */
215 mctp_usblib_rx_cancel(&mctp_usb->rx);
216 return;
217
218 case -EPIPE:
219 /* endpoint stall: clear halt, which will cause a resubmit */
220 rc = mctp_usb_in_urb_err(mctp_usb, status, true);
221 if (rc)
222 return;
223
224 mctp_usb->clear_halt = true;
225 spin_lock_irqsave(&mctp_usb->rx_lock, flags);
226 if (!mctp_usb->rx_stopped)
227 schedule_delayed_work(&mctp_usb->rx_retry_work,
228 RX_RETRY_DELAY);
229 spin_unlock_irqrestore(&mctp_usb->rx_lock, flags);
230 return;
231
232 default:
233 netdev_dbg(netdev, "unexpected rx urb status: %d\n", status);
234 fallthrough;
235 case -ETIME:
236 case -EPROTO:
237 case -EILSEQ:
238 case -EOVERFLOW:
239 /* possibly transient; record first failure, resubmit */
240 rc = mctp_usb_in_urb_err(mctp_usb, status, false);
241 if (rc)
242 return;
243 break;
244
245 case 0:
246 mctp_usblib_rx_complete(netdev, &mctp_usb->rx, urb->actual_length);
247 mctp_usb->in_err_count = 0;
248 break;
249 }
250
251 mctp_usb_rx_queue(mctp_usb, GFP_ATOMIC);
252 }
253
mctp_usb_rx_retry_work(struct work_struct * work)254 static void mctp_usb_rx_retry_work(struct work_struct *work)
255 {
256 struct mctp_usb *mctp_usb = container_of(work, struct mctp_usb,
257 rx_retry_work.work);
258 unsigned long flags;
259 int rc;
260
261 /* We are only called when rx completions are suspended */
262 if (mctp_usb->clear_halt) {
263 int pipe = usb_rcvbulkpipe(mctp_usb->usbdev, mctp_usb->ep_in);
264
265 rc = usb_clear_halt(mctp_usb->usbdev, pipe);
266 if (rc) {
267 netdev_err(mctp_usb->netdev,
268 "can't clear IN EP halt: %d\n", rc);
269
270 if (++mctp_usb->in_err_count >= rx_err_max)
271 return;
272
273 spin_lock_irqsave(&mctp_usb->rx_lock, flags);
274 if (!mctp_usb->rx_stopped)
275 schedule_delayed_work(&mctp_usb->rx_retry_work,
276 RX_RETRY_DELAY);
277 spin_unlock_irqrestore(&mctp_usb->rx_lock, flags);
278 return;
279 }
280 mctp_usb->clear_halt = false;
281 }
282
283 mctp_usb_rx_queue(mctp_usb, GFP_KERNEL);
284 }
285
mctp_usb_open(struct net_device * dev)286 static int mctp_usb_open(struct net_device *dev)
287 {
288 struct mctp_usb *mctp_usb = netdev_priv(dev);
289
290 WRITE_ONCE(mctp_usb->rx_stopped, false);
291 mctp_usb->clear_halt = false;
292 mctp_usb->in_err_count = 0;
293
294 netif_start_queue(dev);
295
296 return mctp_usb_rx_queue(mctp_usb, GFP_KERNEL);
297 }
298
mctp_usb_stop(struct net_device * dev)299 static int mctp_usb_stop(struct net_device *dev)
300 {
301 struct mctp_usb *mctp_usb = netdev_priv(dev);
302 unsigned long flags;
303
304 netif_stop_queue(dev);
305
306 /* prevent RX submission retry */
307 spin_lock_irqsave(&mctp_usb->rx_lock, flags);
308 mctp_usb->rx_stopped = true;
309 cancel_delayed_work(&mctp_usb->rx_retry_work);
310 spin_unlock_irqrestore(&mctp_usb->rx_lock, flags);
311
312 flush_delayed_work(&mctp_usb->rx_retry_work);
313
314 usb_kill_urb(mctp_usb->rx_urb);
315
316 usb_kill_anchored_urbs(&mctp_usb->tx_anchor);
317
318 mctp_usblib_tx_cancel(&mctp_usb->tx, dev, SKB_DROP_REASON_DEV_READY);
319 mctp_usblib_rx_cancel(&mctp_usb->rx);
320
321 return 0;
322 }
323
324 static const struct net_device_ops mctp_usb_netdev_ops = {
325 .ndo_start_xmit = mctp_usb_start_xmit,
326 .ndo_open = mctp_usb_open,
327 .ndo_stop = mctp_usb_stop,
328 };
329
mctp_usb_netdev_setup(struct net_device * dev)330 static void mctp_usb_netdev_setup(struct net_device *dev)
331 {
332 dev->type = ARPHRD_MCTP;
333
334 dev->mtu = MCTP_USB_MTU_MIN;
335 dev->min_mtu = MCTP_USB_MTU_MIN;
336 dev->max_mtu = MCTP_USB_1_0_MTU_MAX;
337
338 dev->hard_header_len = sizeof(struct mctp_usb_hdr);
339 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
340 dev->flags = IFF_NOARP;
341 dev->netdev_ops = &mctp_usb_netdev_ops;
342 dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS;
343 }
344
mctp_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)345 static int mctp_usb_probe(struct usb_interface *intf,
346 const struct usb_device_id *id)
347 {
348 struct usb_endpoint_descriptor *ep_in, *ep_out;
349 struct usb_host_interface *iface_desc;
350 struct net_device *netdev;
351 struct mctp_usb *dev;
352 bool span;
353 int rc;
354
355 /* only one alternate */
356 iface_desc = intf->cur_altsetting;
357
358 rc = usb_find_common_endpoints(iface_desc, &ep_in, &ep_out, NULL, NULL);
359 if (rc) {
360 dev_err(&intf->dev, "invalid endpoints on device?\n");
361 return rc;
362 }
363
364 span = iface_desc->desc.bInterfaceSubClass == MCTP_USB_SUBCLASS_SPAN;
365
366 netdev = alloc_netdev(sizeof(*dev), "mctpusb%d", NET_NAME_ENUM,
367 mctp_usb_netdev_setup);
368 if (!netdev)
369 return -ENOMEM;
370
371 SET_NETDEV_DEV(netdev, &intf->dev);
372 dev = netdev_priv(netdev);
373 dev->span = span;
374 dev->netdev = netdev;
375 dev->usbdev = interface_to_usbdev(intf);
376 dev->intf = intf;
377 spin_lock_init(&dev->rx_lock);
378 if (dev->span)
379 netdev->max_mtu = MCTP_USB_1_1_MTU_MAX;
380 spin_lock_init(&dev->tx_qmem_lock);
381 usb_set_intfdata(intf, dev);
382
383 rc = mctp_usblib_rx_init(&dev->rx, le16_to_cpu(ep_in->wMaxPacketSize),
384 dev->span);
385 if (rc)
386 goto err_free_netdev;
387 mctp_usblib_tx_init(&dev->tx, &tx_ops, dev, dev->span);
388 init_usb_anchor(&dev->tx_anchor);
389
390 dev->ep_in = ep_in->bEndpointAddress;
391 dev->ep_out = ep_out->bEndpointAddress;
392
393 dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
394 if (!dev->rx_urb) {
395 rc = -ENOMEM;
396 goto err_fini_rxtx;
397 }
398
399 INIT_DELAYED_WORK(&dev->rx_retry_work, mctp_usb_rx_retry_work);
400
401 rc = mctp_register_netdev(netdev, NULL, MCTP_PHYS_BINDING_USB);
402 if (rc)
403 goto err_free_urb;
404
405 return 0;
406
407 err_free_urb:
408 usb_free_urb(dev->rx_urb);
409 err_fini_rxtx:
410 mctp_usblib_tx_fini(&dev->tx);
411 mctp_usblib_rx_fini(&dev->rx);
412 err_free_netdev:
413 free_netdev(netdev);
414 return rc;
415 }
416
mctp_usb_disconnect(struct usb_interface * intf)417 static void mctp_usb_disconnect(struct usb_interface *intf)
418 {
419 struct mctp_usb *dev = usb_get_intfdata(intf);
420
421 mctp_unregister_netdev(dev->netdev);
422 mctp_usblib_rx_fini(&dev->rx);
423 mctp_usblib_tx_fini(&dev->tx);
424 usb_free_urb(dev->rx_urb);
425 free_netdev(dev->netdev);
426 }
427
428 static const struct usb_device_id mctp_usb_devices[] = {
429 { USB_INTERFACE_INFO(USB_CLASS_MCTP, MCTP_USB_SUBCLASS_BASE, 0x1) },
430 { USB_INTERFACE_INFO(USB_CLASS_MCTP, MCTP_USB_SUBCLASS_SPAN, 0x1) },
431 { 0 },
432 };
433
434 MODULE_DEVICE_TABLE(usb, mctp_usb_devices);
435
436 static struct usb_driver mctp_usb_driver = {
437 .name = "mctp-usb",
438 .id_table = mctp_usb_devices,
439 .probe = mctp_usb_probe,
440 .disconnect = mctp_usb_disconnect,
441 };
442
443 module_usb_driver(mctp_usb_driver)
444
445 MODULE_LICENSE("GPL");
446 MODULE_AUTHOR("Jeremy Kerr <jk@codeconstruct.com.au>");
447 MODULE_DESCRIPTION("MCTP USB transport");
448