xref: /linux/include/linux/usb/mctp-usb.h (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * mctp-usb.h - MCTP USB transport binding: common definitions,
4  * based on DMTF0283 specification:
5  * https://www.dmtf.org/sites/default/files/standards/documents/DSP0283_1.1.0.pdf
6  *
7  * These are protocol-level definitions, that may be shared between host
8  * and gadget drivers.
9  *
10  * Copyright (C) 2024-2025 Code Construct Pty Ltd
11  */
12 
13 #ifndef __LINUX_USB_MCTP_USB_H
14 #define __LINUX_USB_MCTP_USB_H
15 
16 #include <linux/netdevice.h>
17 #include <linux/skbuff.h>
18 #include <linux/types.h>
19 
20 /*
21  * MCTP-over-USB transport header. DSP0283 v1.0 has an 8-bit length field
22  * (preceded by 8 reserved bits), v1.1 has a 13-bit length field (preceded by
23  * 3 reserved bits). We use a be16 for our length to handle the larger v1.1
24  * representation, and mask as appropriate.
25  */
26 struct mctp_usb_hdr {
27 	__be16	id;
28 	__be16	len;
29 } __packed;
30 
31 /* max transfer size for DSP0283 v1.0 */
32 #define MCTP_USB_1_0_XFER_SIZE	512
33 #define MCTP_USB_BTU		68
34 #define MCTP_USB_MTU_MIN	MCTP_USB_BTU
35 #define MCTP_USB_1_0_PKTLEN_MAX	U8_MAX
36 #define MCTP_USB_1_0_MTU_MAX	(MCTP_USB_1_0_PKTLEN_MAX - sizeof(struct mctp_usb_hdr))
37 #define MCTP_USB_1_1_PKTLEN_MAX	GENMASK(12, 0)
38 #define MCTP_USB_1_1_MTU_MAX	(MCTP_USB_1_1_PKTLEN_MAX - sizeof(struct mctp_usb_hdr))
39 #define MCTP_USB_DMTF_ID	0x1ab4
40 
41 /* mctp-usblib */
42 
43 /*
44  * RX handle: drivers will typically create one on init, which persists for
45  * the life of the driver. The same handle is used for progressive
46  * prepare -> complete operations (for each incoming USB transfer), which
47  * result in netif_rx()-ing the MCTP packets received
48  */
49 struct mctp_usblib_rx {
50 	struct sk_buff *skb;
51 	u16 ep_pktlen;
52 	bool span;
53 };
54 
55 int mctp_usblib_rx_init(struct mctp_usblib_rx *rx, u16 ep_pktlen, bool span);
56 void mctp_usblib_rx_fini(struct mctp_usblib_rx *rx);
57 
58 int mctp_usblib_rx_prepare(struct net_device *netdev,
59 			   struct mctp_usblib_rx *rx,
60 			   void **bufp, size_t *lenp, gfp_t gfp);
61 
62 int mctp_usblib_rx_complete(struct net_device *netdev,
63 			    struct mctp_usblib_rx *rx, size_t len);
64 
65 void mctp_usblib_rx_cancel(struct mctp_usblib_rx *rx);
66 
67 /*
68  * TX handle: created by mctp_usblib_tx_push() during the tx path, and
69  * may persist across multiple packet transmits.
70  */
71 struct mctp_usblib_tx_ctx;
72 
73 struct mctp_usblib_tx_ops {
74 	/* Start a USB TX for @data. On returning success, the implementation
75 	 * must arrange for mctp_usblib_tx_send_complete() to be called at some
76 	 * later point (eg., on urb completion).
77 	 */
78 	int (*send)(struct mctp_usblib_tx_ctx *tx_ctx, void *data, size_t len);
79 };
80 
81 struct mctp_usblib_tx {
82 	struct mctp_usblib_tx_ops ops;
83 	void *priv;
84 	bool span;
85 	/* protects access to cur_ctx */
86 	spinlock_t lock;
87 	/* context to which we are adding packets, cleared on send */
88 	struct mctp_usblib_tx_ctx *cur_ctx;
89 };
90 
91 void mctp_usblib_tx_init(struct mctp_usblib_tx *tx,
92 			 const struct mctp_usblib_tx_ops *ops, void *priv,
93 			 bool span);
94 void mctp_usblib_tx_fini(struct mctp_usblib_tx *tx);
95 
96 void *mctp_usblib_tx_ctx_priv(struct mctp_usblib_tx_ctx *tx_ctx);
97 
98 int mctp_usblib_tx_push(struct net_device *dev,
99 			struct mctp_usblib_tx *tx,
100 			struct sk_buff *skb, bool more);
101 
102 void mctp_usblib_tx_send_complete(struct mctp_usblib_tx_ctx *tx_ctx,
103 				  struct net_device *dev, bool ok);
104 
105 void mctp_usblib_tx_cancel(struct mctp_usblib_tx *tx, struct net_device *dev,
106 			   enum skb_drop_reason reason);
107 
108 #endif /*  __LINUX_USB_MCTP_USB_H */
109