generic: cdc_ncm: Add support for moving NDP to end of NCM frame
[librecmc/librecmc.git] / target / linux / generic / patches-3.18 / 190-cdc_ncm_add_support_for_moving_ndp_to_end_of_ncm_frame.patch
1 diff -u a/drivers/net/usb/cdc_mbim.c b/drivers/net/usb/cdc_mbim.c
2 --- a/drivers/net/usb/cdc_mbim.c        2015-06-28 17:40:40.000000000 +0000
3 +++ b/drivers/net/usb/cdc_mbim.c        2015-07-04 15:05:14.546901702 +0000
4 @@ -158,7 +158,7 @@
5         if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
6                 goto err;
7
8 -       ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
9 +       ret = cdc_ncm_bind_common(dev, intf, data_altsetting, 0);
10         if (ret)
11                 goto err;
12
13 diff -u a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c
14 --- a/drivers/net/usb/cdc_ncm.c 2015-06-28 17:40:40.000000000 +0000
15 +++ b/drivers/net/usb/cdc_ncm.c 2015-07-09 08:43:01.658770535 +0000
16 @@ -684,10 +684,11 @@
17                 ctx->tx_curr_skb = NULL;
18         }
19
20 +       kfree(ctx->delayed_ndp16);
21         kfree(ctx);
22  }
23
24 -int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
25 +int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting, int drvflags)
26  {
27         const struct usb_cdc_union_desc *union_desc = NULL;
28         struct cdc_ncm_ctx *ctx;
29 @@ -855,6 +856,17 @@
30         /* finish setting up the device specific data */
31         cdc_ncm_setup(dev);
32
33 +       /* Device-specific flags */
34 +       ctx->drvflags = drvflags;
35 +
36 +       /* Allocate the delayed NDP if needed. */
37 +       if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
38 +               ctx->delayed_ndp16 = kzalloc(ctx->max_ndp_size, GFP_KERNEL);
39 +               if (!ctx->delayed_ndp16)
40 +                       goto error2;
41 +               dev_info(&intf->dev, "NDP will be placed at end of frame for this device.");
42 +       }
43 +
44         /* override ethtool_ops */
45         dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
46
47 @@ -954,8 +966,11 @@
48         if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM)
49                 return -ENODEV;
50
51 -       /* The NCM data altsetting is fixed */
52 -       ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM);
53 +       /* The NCM data altsetting is fixed, so we hard-coded it.
54 +        * Additionally, generic NCM devices are assumed to accept arbitrarily
55 +        * placed NDP.
56 +        */
57 +       ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM, 0);
58
59         /*
60          * We should get an event when network connection is "connected" or
61 @@ -986,6 +1001,14 @@
62         struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
63         size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
64
65 +       /* If NDP should be moved to the end of the NCM package, we can't follow the
66 +       * NTH16 header as we would normally do. NDP isn't written to the SKB yet, and
67 +       * the wNdpIndex field in the header is actually not consistent with reality. It will be later.
68 +       */
69 +       if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END)
70 +               if (ctx->delayed_ndp16->dwSignature == sign)
71 +                       return ctx->delayed_ndp16;
72 +
73         /* follow the chain of NDPs, looking for a match */
74         while (ndpoffset) {
75                 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
76 @@ -995,7 +1018,8 @@
77         }
78
79         /* align new NDP */
80 -       cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
81 +       if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
82 +               cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
83
84         /* verify that there is room for the NDP and the datagram (reserve) */
85         if ((ctx->tx_max - skb->len - reserve) < ctx->max_ndp_size)
86 @@ -1008,7 +1032,11 @@
87                 nth16->wNdpIndex = cpu_to_le16(skb->len);
88
89         /* push a new empty NDP */
90 -       ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, ctx->max_ndp_size), 0, ctx->max_ndp_size);
91 +       if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
92 +               ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, ctx->max_ndp_size), 0, ctx->max_ndp_size);
93 +       else
94 +               ndp16 = ctx->delayed_ndp16;
95 +
96         ndp16->dwSignature = sign;
97         ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
98         return ndp16;
99 @@ -1023,6 +1051,15 @@
100         struct sk_buff *skb_out;
101         u16 n = 0, index, ndplen;
102         u8 ready2send = 0;
103 +       u32 delayed_ndp_size;
104 +
105 +       /* When our NDP gets written in cdc_ncm_ndp(), then skb_out->len gets updated
106 +        * accordingly. Otherwise, we should check here.
107 +        */
108 +       if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END)
109 +               delayed_ndp_size = ctx->max_ndp_size;
110 +       else
111 +               delayed_ndp_size = 0;
112
113         /* if there is a remaining skb, it gets priority */
114         if (skb != NULL) {
115 @@ -1077,7 +1114,7 @@
116                 cdc_ncm_align_tail(skb_out,  ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
117
118                 /* check if we had enough room left for both NDP and frame */
119 -               if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
120 +               if (!ndp16 || skb_out->len + skb->len + delayed_ndp_size > ctx->tx_max) {
121                         if (n == 0) {
122                                 /* won't fit, MTU problem? */
123                                 dev_kfree_skb_any(skb);
124 @@ -1150,6 +1187,17 @@
125                 /* variables will be reset at next call */
126         }
127
128 +       /* If requested, put NDP at end of frame. */
129 +       if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
130 +               nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
131 +               cdc_ncm_align_tail(skb_out, ctx->tx_ndp_modulus, 0, ctx->tx_max);
132 +               nth16->wNdpIndex = cpu_to_le16(skb_out->len);
133 +               memcpy(skb_put(skb_out, ctx->max_ndp_size), ctx->delayed_ndp16, ctx->max_ndp_size);
134 +
135 +               /* Zero out delayed NDP - signature checking will naturally fail. */
136 +               ndp16 = memset(ctx->delayed_ndp16, 0, ctx->max_ndp_size);
137 +       }
138 +
139         /* If collected data size is less or equal ctx->min_tx_pkt
140          * bytes, we send buffers as it is. If we get more data, it
141          * would be more efficient for USB HS mobile device with DMA
142 diff -u a/drivers/net/usb/huawei_cdc_ncm.c b/drivers/net/usb/huawei_cdc_ncm.c
143 --- a/drivers/net/usb/huawei_cdc_ncm.c  2015-06-28 17:40:40.000000000 +0000
144 +++ b/drivers/net/usb/huawei_cdc_ncm.c  2015-07-04 15:23:25.779014586 +0000
145 @@ -73,11 +73,14 @@
146         struct usb_driver *subdriver = ERR_PTR(-ENODEV);
147         int ret = -ENODEV;
148         struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
149 +       int drvflags = 0;
150
151         /* altsetting should always be 1 for NCM devices - so we hard-coded
152 -        * it here
153 +        * it here. Some huawei devices will need the NDP part of the NCM package to
154 +        * be at the end of the frame.
155          */
156 -       ret = cdc_ncm_bind_common(usbnet_dev, intf, 1);
157 +       drvflags |= CDC_NCM_FLAG_NDP_TO_END;
158 +       ret = cdc_ncm_bind_common(usbnet_dev, intf, 1, drvflags);
159         if (ret)
160                 goto err;
161
162 diff -u a/include/linux/usb/cdc_ncm.h b/include/linux/usb/cdc_ncm.h
163 --- a/include/linux/usb/cdc_ncm.h       2015-06-28 17:40:40.000000000 +0000
164 +++ b/include/linux/usb/cdc_ncm.h       2015-07-04 15:27:52.171388014 +0000
165 @@ -80,6 +80,9 @@
166  #define CDC_NCM_TIMER_INTERVAL_MIN             5UL
167  #define CDC_NCM_TIMER_INTERVAL_MAX             (U32_MAX / NSEC_PER_USEC)
168
169 +/* Driver flags */
170 +#define CDC_NCM_FLAG_NDP_TO_END                        0x02    /* NDP is placed at end of frame */
171 +
172  #define cdc_ncm_comm_intf_is_mbim(x)  ((x)->desc.bInterfaceSubClass == USB_CDC_SUBCLASS_MBIM && \
173                                        (x)->desc.bInterfaceProtocol == USB_CDC_PROTO_NONE)
174  #define cdc_ncm_data_intf_is_mbim(x)  ((x)->desc.bInterfaceProtocol == USB_CDC_MBIM_PROTO_NTB)
175 @@ -103,9 +106,11 @@
176
177         spinlock_t mtx;
178         atomic_t stop;
179 +       int drvflags;
180
181         u32 timer_interval;
182         u32 max_ndp_size;
183 +       struct usb_cdc_ncm_ndp16 *delayed_ndp16;
184
185         u32 tx_timer_pending;
186         u32 tx_curr_frame_num;
187 @@ -133,7 +138,7 @@
188  };
189
190  u8 cdc_ncm_select_altsetting(struct usb_interface *intf);
191 -int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting);
192 +int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting, int drvflags);
193  void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf);
194  struct sk_buff *cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign);
195  int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in);