kernel: cdc_ncm: Add support for moving NDP to end of NCM frame
[openwrt/staging/chunkeey.git] / target / linux / generic / patches-4.0 / 190-cdc_ncm_add_support_for_moving_ndp_to_end_of_ncm_frame.patch
1 From 4a0e3e989d66bb7204b163d9cfaa7fa96d0f2023 Mon Sep 17 00:00:00 2001
2 From: Enrico Mioso <mrkiko.rs@gmail.com>
3 Date: Wed, 8 Jul 2015 13:05:57 +0200
4 Subject: [PATCH] cdc_ncm: Add support for moving NDP to end of NCM frame
5
6 NCM specs are not actually mandating a specific position in the frame for
7 the NDP (Network Datagram Pointer). However, some Huawei devices will
8 ignore our aggregates if it is not placed after the datagrams it points
9 to. Add support for doing just this, in a per-device configurable way.
10 While at it, update NCM subdrivers, disabling this functionality in all of
11 them, except in huawei_cdc_ncm where it is enabled instead.
12 We aren't making any distinction between different Huawei NCM devices,
13 based on what the vendor driver does. Standard NCM devices are left
14 unaffected: if they are compliant, they should be always usable, still
15 stay on the safe side.
16
17 This change has been tested and working with a Huawei E3131 device (which
18 works regardless of NDP position), a Huawei E3531 (also working both
19 ways) and an E3372 (which mandates NDP to be after indexed datagrams).
20
21 V1->V2:
22 - corrected wrong NDP acronym definition
23 - fixed possible NULL pointer dereference
24 - patch cleanup
25 V2->V3:
26 - Properly account for the NDP size when writing new packets to SKB
27
28 Signed-off-by: Enrico Mioso <mrkiko.rs@gmail.com>
29 Signed-off-by: David S. Miller <davem@davemloft.net>
30 ---
31 drivers/net/usb/cdc_mbim.c | 2 +-
32 drivers/net/usb/cdc_ncm.c | 61 ++++++++++++++++++++++++++++++++++++----
33 drivers/net/usb/huawei_cdc_ncm.c | 7 +++--
34 include/linux/usb/cdc_ncm.h | 7 ++++-
35 4 files changed, 67 insertions(+), 10 deletions(-)
36
37 --- a/drivers/net/usb/cdc_mbim.c
38 +++ b/drivers/net/usb/cdc_mbim.c
39 @@ -158,7 +158,7 @@ static int cdc_mbim_bind(struct usbnet *
40 if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
41 goto err;
42
43 - ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
44 + ret = cdc_ncm_bind_common(dev, intf, data_altsetting, 0);
45 if (ret)
46 goto err;
47
48 --- a/drivers/net/usb/cdc_ncm.c
49 +++ b/drivers/net/usb/cdc_ncm.c
50 @@ -684,10 +684,12 @@ static void cdc_ncm_free(struct cdc_ncm_
51 ctx->tx_curr_skb = NULL;
52 }
53
54 + kfree(ctx->delayed_ndp16);
55 +
56 kfree(ctx);
57 }
58
59 -int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
60 +int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting, int drvflags)
61 {
62 const struct usb_cdc_union_desc *union_desc = NULL;
63 struct cdc_ncm_ctx *ctx;
64 @@ -855,6 +857,17 @@ advance:
65 /* finish setting up the device specific data */
66 cdc_ncm_setup(dev);
67
68 + /* Device-specific flags */
69 + ctx->drvflags = drvflags;
70 +
71 + /* Allocate the delayed NDP if needed. */
72 + if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
73 + ctx->delayed_ndp16 = kzalloc(ctx->max_ndp_size, GFP_KERNEL);
74 + if (!ctx->delayed_ndp16)
75 + goto error2;
76 + dev_info(&intf->dev, "NDP will be placed at end of frame for this device.");
77 + }
78 +
79 /* override ethtool_ops */
80 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
81
82 @@ -954,8 +967,11 @@ static int cdc_ncm_bind(struct usbnet *d
83 if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM)
84 return -ENODEV;
85
86 - /* The NCM data altsetting is fixed */
87 - ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM);
88 + /* The NCM data altsetting is fixed, so we hard-coded it.
89 + * Additionally, generic NCM devices are assumed to accept arbitrarily
90 + * placed NDP.
91 + */
92 + ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM, 0);
93
94 /*
95 * We should get an event when network connection is "connected" or
96 @@ -986,6 +1002,14 @@ static struct usb_cdc_ncm_ndp16 *cdc_ncm
97 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
98 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
99
100 + /* If NDP should be moved to the end of the NCM package, we can't follow the
101 + * NTH16 header as we would normally do. NDP isn't written to the SKB yet, and
102 + * the wNdpIndex field in the header is actually not consistent with reality. It will be later.
103 + */
104 + if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END)
105 + if (ctx->delayed_ndp16->dwSignature == sign)
106 + return ctx->delayed_ndp16;
107 +
108 /* follow the chain of NDPs, looking for a match */
109 while (ndpoffset) {
110 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
111 @@ -995,7 +1019,8 @@ static struct usb_cdc_ncm_ndp16 *cdc_ncm
112 }
113
114 /* align new NDP */
115 - cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
116 + if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
117 + cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
118
119 /* verify that there is room for the NDP and the datagram (reserve) */
120 if ((ctx->tx_max - skb->len - reserve) < ctx->max_ndp_size)
121 @@ -1008,7 +1033,11 @@ static struct usb_cdc_ncm_ndp16 *cdc_ncm
122 nth16->wNdpIndex = cpu_to_le16(skb->len);
123
124 /* push a new empty NDP */
125 - ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, ctx->max_ndp_size), 0, ctx->max_ndp_size);
126 + if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
127 + ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, ctx->max_ndp_size), 0, ctx->max_ndp_size);
128 + else
129 + ndp16 = ctx->delayed_ndp16;
130 +
131 ndp16->dwSignature = sign;
132 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
133 return ndp16;
134 @@ -1023,6 +1052,15 @@ cdc_ncm_fill_tx_frame(struct usbnet *dev
135 struct sk_buff *skb_out;
136 u16 n = 0, index, ndplen;
137 u8 ready2send = 0;
138 + u32 delayed_ndp_size;
139 +
140 + /* When our NDP gets written in cdc_ncm_ndp(), then skb_out->len gets updated
141 + * accordingly. Otherwise, we should check here.
142 + */
143 + if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END)
144 + delayed_ndp_size = ctx->max_ndp_size;
145 + else
146 + delayed_ndp_size = 0;
147
148 /* if there is a remaining skb, it gets priority */
149 if (skb != NULL) {
150 @@ -1077,7 +1115,7 @@ cdc_ncm_fill_tx_frame(struct usbnet *dev
151 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
152
153 /* check if we had enough room left for both NDP and frame */
154 - if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
155 + if (!ndp16 || skb_out->len + skb->len + delayed_ndp_size > ctx->tx_max) {
156 if (n == 0) {
157 /* won't fit, MTU problem? */
158 dev_kfree_skb_any(skb);
159 @@ -1150,6 +1188,17 @@ cdc_ncm_fill_tx_frame(struct usbnet *dev
160 /* variables will be reset at next call */
161 }
162
163 + /* If requested, put NDP at end of frame. */
164 + if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
165 + nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
166 + cdc_ncm_align_tail(skb_out, ctx->tx_ndp_modulus, 0, ctx->tx_max);
167 + nth16->wNdpIndex = cpu_to_le16(skb_out->len);
168 + memcpy(skb_put(skb_out, ctx->max_ndp_size), ctx->delayed_ndp16, ctx->max_ndp_size);
169 +
170 + /* Zero out delayed NDP - signature checking will naturally fail. */
171 + ndp16 = memset(ctx->delayed_ndp16, 0, ctx->max_ndp_size);
172 + }
173 +
174 /* If collected data size is less or equal ctx->min_tx_pkt
175 * bytes, we send buffers as it is. If we get more data, it
176 * would be more efficient for USB HS mobile device with DMA
177 --- a/drivers/net/usb/huawei_cdc_ncm.c
178 +++ b/drivers/net/usb/huawei_cdc_ncm.c
179 @@ -73,11 +73,14 @@ static int huawei_cdc_ncm_bind(struct us
180 struct usb_driver *subdriver = ERR_PTR(-ENODEV);
181 int ret = -ENODEV;
182 struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
183 + int drvflags = 0;
184
185 /* altsetting should always be 1 for NCM devices - so we hard-coded
186 - * it here
187 + * it here. Some huawei devices will need the NDP part of the NCM package to
188 + * be at the end of the frame.
189 */
190 - ret = cdc_ncm_bind_common(usbnet_dev, intf, 1);
191 + drvflags |= CDC_NCM_FLAG_NDP_TO_END;
192 + ret = cdc_ncm_bind_common(usbnet_dev, intf, 1, drvflags);
193 if (ret)
194 goto err;
195
196 --- a/include/linux/usb/cdc_ncm.h
197 +++ b/include/linux/usb/cdc_ncm.h
198 @@ -80,6 +80,9 @@
199 #define CDC_NCM_TIMER_INTERVAL_MIN 5UL
200 #define CDC_NCM_TIMER_INTERVAL_MAX (U32_MAX / NSEC_PER_USEC)
201
202 +/* Driver flags */
203 +#define CDC_NCM_FLAG_NDP_TO_END 0x02 /* NDP is placed at end of frame */
204 +
205 #define cdc_ncm_comm_intf_is_mbim(x) ((x)->desc.bInterfaceSubClass == USB_CDC_SUBCLASS_MBIM && \
206 (x)->desc.bInterfaceProtocol == USB_CDC_PROTO_NONE)
207 #define cdc_ncm_data_intf_is_mbim(x) ((x)->desc.bInterfaceProtocol == USB_CDC_MBIM_PROTO_NTB)
208 @@ -103,9 +106,11 @@ struct cdc_ncm_ctx {
209
210 spinlock_t mtx;
211 atomic_t stop;
212 + int drvflags;
213
214 u32 timer_interval;
215 u32 max_ndp_size;
216 + struct usb_cdc_ncm_ndp16 *delayed_ndp16;
217
218 u32 tx_timer_pending;
219 u32 tx_curr_frame_num;
220 @@ -133,7 +138,7 @@ struct cdc_ncm_ctx {
221 };
222
223 u8 cdc_ncm_select_altsetting(struct usb_interface *intf);
224 -int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting);
225 +int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting, int drvflags);
226 void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf);
227 struct sk_buff *cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign);
228 int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in);