1 #ifndef _NCM_H
2 #define _NCM_H
3 
4 /** @file
5  *
6  * CDC-NCM USB Ethernet driver
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <ipxe/usb.h>
14 #include <ipxe/cdc.h>
15 #include <byteswap.h>
16 #include "ecm.h"
17 
18 /** CDC-NCM subclass */
19 #define USB_SUBCLASS_CDC_NCM 0x0d
20 
21 /** Get NTB parameters */
22 #define NCM_GET_NTB_PARAMETERS						\
23 	( USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE |		\
24 	  USB_REQUEST_TYPE ( 0x80 ) )
25 
26 /** NTB datagram parameters */
27 struct ncm_ntb_datagram_parameters {
28 	/** Maximum size */
29 	uint32_t mtu;
30 	/** Alignment divisor */
31 	uint16_t divisor;
32 	/** Alignment remainder */
33 	uint16_t remainder;
34 	/** Alignment modulus */
35 	uint16_t modulus;
36 } __attribute__ (( packed ));
37 
38 /** NTB parameters */
39 struct ncm_ntb_parameters {
40 	/** Length */
41 	uint16_t len;
42 	/** Supported formats */
43 	uint16_t formats;
44 	/** IN datagram parameters */
45 	struct ncm_ntb_datagram_parameters in;
46 	/** Reserved */
47 	uint16_t reserved;
48 	/** OUT datagram parameters */
49 	struct ncm_ntb_datagram_parameters out;
50 	/** Maximum number of datagrams per OUT NTB */
51 	uint16_t max;
52 } __attribute__ (( packed ));
53 
54 /** Set MAC address */
55 #define NCM_SET_NET_ADDRESS						\
56 	( USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE |		\
57 	  USB_REQUEST_TYPE ( 0x82 ) )
58 
59 /** Set NTB input size */
60 #define NCM_SET_NTB_INPUT_SIZE						\
61 	( USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE |		\
62 	  USB_REQUEST_TYPE ( 0x86 ) )
63 
64 /** Set NTB input size */
65 struct ncm_set_ntb_input_size {
66 	/** Maximum size */
67 	uint32_t mtu;
68 } __attribute__ (( packed ));
69 
70 /** Minimum allowed NTB input size */
71 #define NCM_MIN_NTB_INPUT_SIZE 2048
72 
73 /** Maximum allowed NTB input size (16-bit) */
74 #define NCM_MAX_NTB_INPUT_SIZE 65536
75 
76 /** CDC-NCM transfer header (16-bit) */
77 struct ncm_transfer_header {
78 	/** Signature */
79 	uint32_t magic;
80 	/** Header length */
81 	uint16_t header_len;
82 	/** Sequence number */
83 	uint16_t sequence;
84 	/** Total length */
85 	uint16_t len;
86 	/** Offset of first datagram pointer */
87 	uint16_t offset;
88 } __attribute__ (( packed ));
89 
90 /** CDC-NCM transfer header magic */
91 #define NCM_TRANSFER_HEADER_MAGIC 0x484d434eUL
92 
93 /** CDC-NCM datagram descriptor (16-bit) */
94 struct ncm_datagram_descriptor {
95 	/** Starting offset */
96 	uint16_t offset;
97 	/** Length */
98 	uint16_t len;
99 } __attribute__ (( packed ));
100 
101 /** CDC-NCM datagram pointer (16-bit) */
102 struct ncm_datagram_pointer {
103 	/** Signature */
104 	uint32_t magic;
105 	/** Header length */
106 	uint16_t header_len;
107 	/** Offset of next datagram pointer */
108 	uint16_t offset;
109 	/** Datagram descriptors
110 	 *
111 	 * Must be terminated by an empty descriptor.
112 	 */
113 	struct ncm_datagram_descriptor desc[0];
114 } __attribute__ (( packed ));
115 
116 /** CDC-NCM datagram pointer magic */
117 #define NCM_DATAGRAM_POINTER_MAGIC 0x304d434eUL
118 
119 /** CDC-NCM datagram pointer CRC present flag */
120 #define NCM_DATAGRAM_POINTER_MAGIC_CRC 0x01000000UL
121 
122 /** NTB constructed for transmitted packets (excluding padding)
123  *
124  * This is a policy decision.
125  */
126 struct ncm_ntb_header {
127 	/** Transfer header */
128 	struct ncm_transfer_header nth;
129 	/** Datagram pointer */
130 	struct ncm_datagram_pointer ndp;
131 	/** Datagram descriptors */
132 	struct ncm_datagram_descriptor desc[2];
133 } __attribute__ (( packed ));
134 
135 /** A CDC-NCM network device */
136 struct ncm_device {
137 	/** USB device */
138 	struct usb_device *usb;
139 	/** USB bus */
140 	struct usb_bus *bus;
141 	/** Network device */
142 	struct net_device *netdev;
143 	/** USB network device */
144 	struct usbnet_device usbnet;
145 
146 	/** Maximum supported NTB input size */
147 	size_t mtu;
148 	/** Transmitted packet sequence number */
149 	uint16_t sequence;
150 	/** Alignment padding required on transmitted packets */
151 	size_t padding;
152 };
153 
154 /** Bulk IN ring minimum buffer count
155  *
156  * This is a policy decision.
157  */
158 #define NCM_IN_MIN_COUNT 3
159 
160 /** Bulk IN ring minimum total buffer size
161  *
162  * This is a policy decision.
163  */
164 #define NCM_IN_MIN_SIZE 16384
165 
166 /** Bulk IN ring maximum total buffer size
167  *
168  * This is a policy decision.
169  */
170 #define NCM_IN_MAX_SIZE 131072
171 
172 /** Interrupt ring buffer count
173  *
174  * This is a policy decision.
175  */
176 #define NCM_INTR_COUNT 2
177 
178 #endif /* _NCM_H */
179