GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / staging / gdm724x / gdm_lte.c
1 /*
2  * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/etherdevice.h>
17 #include <linux/ip.h>
18 #include <linux/ipv6.h>
19 #include <linux/udp.h>
20 #include <linux/in.h>
21 #include <linux/if_arp.h>
22 #include <linux/if_ether.h>
23 #include <linux/if_vlan.h>
24 #include <linux/in6.h>
25 #include <linux/tcp.h>
26 #include <linux/icmp.h>
27 #include <linux/icmpv6.h>
28 #include <linux/uaccess.h>
29 #include <net/ndisc.h>
30
31 #include "gdm_lte.h"
32 #include "netlink_k.h"
33 #include "hci.h"
34 #include "hci_packet.h"
35 #include "gdm_endian.h"
36
37 /*
38  * Netlink protocol number
39  */
40 #define NETLINK_LTE 30
41
42 /*
43  * Default MTU Size
44  */
45 #define DEFAULT_MTU_SIZE 1500
46
47 #define IP_VERSION_4    4
48 #define IP_VERSION_6    6
49
50 static struct {
51         int ref_cnt;
52         struct sock *sock;
53 } lte_event;
54
55 static struct device_type wwan_type = {
56         .name   = "wwan",
57 };
58
59 static int gdm_lte_open(struct net_device *dev)
60 {
61         netif_start_queue(dev);
62         return 0;
63 }
64
65 static int gdm_lte_close(struct net_device *dev)
66 {
67         netif_stop_queue(dev);
68         return 0;
69 }
70
71 static int gdm_lte_set_config(struct net_device *dev, struct ifmap *map)
72 {
73         if (dev->flags & IFF_UP)
74                 return -EBUSY;
75         return 0;
76 }
77
78 static void tx_complete(void *arg)
79 {
80         struct nic *nic = arg;
81
82         if (netif_queue_stopped(nic->netdev))
83                 netif_wake_queue(nic->netdev);
84 }
85
86 static int gdm_lte_rx(struct sk_buff *skb, struct nic *nic, int nic_type)
87 {
88         int ret, len;
89
90         len = skb->len + ETH_HLEN;
91         ret = netif_rx_ni(skb);
92         if (ret == NET_RX_DROP) {
93                 nic->stats.rx_dropped++;
94         } else {
95                 nic->stats.rx_packets++;
96                 nic->stats.rx_bytes += len;
97         }
98
99         return 0;
100 }
101
102 static int gdm_lte_emulate_arp(struct sk_buff *skb_in, u32 nic_type)
103 {
104         struct nic *nic = netdev_priv(skb_in->dev);
105         struct sk_buff *skb_out;
106         struct ethhdr eth;
107         struct vlan_ethhdr vlan_eth;
108         struct arphdr *arp_in;
109         struct arphdr *arp_out;
110         struct arpdata {
111                 u8 ar_sha[ETH_ALEN];
112                 u8 ar_sip[4];
113                 u8 ar_tha[ETH_ALEN];
114                 u8 ar_tip[4];
115         };
116         struct arpdata *arp_data_in;
117         struct arpdata *arp_data_out;
118         u8 arp_temp[60];
119         void *mac_header_data;
120         u32 mac_header_len;
121
122         /* Format the mac header so that it can be put to skb */
123         if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
124                 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
125                 mac_header_data = &vlan_eth;
126                 mac_header_len = VLAN_ETH_HLEN;
127         } else {
128                 memcpy(&eth, skb_in->data, sizeof(struct ethhdr));
129                 mac_header_data = &eth;
130                 mac_header_len = ETH_HLEN;
131         }
132
133         /* Get the pointer of the original request */
134         arp_in = (struct arphdr *)(skb_in->data + mac_header_len);
135         arp_data_in = (struct arpdata *)(skb_in->data + mac_header_len +
136                                         sizeof(struct arphdr));
137
138         /* Get the pointer of the outgoing response */
139         arp_out = (struct arphdr *)arp_temp;
140         arp_data_out = (struct arpdata *)(arp_temp + sizeof(struct arphdr));
141
142         /* Copy the arp header */
143         memcpy(arp_out, arp_in, sizeof(struct arphdr));
144         arp_out->ar_op = htons(ARPOP_REPLY);
145
146         /* Copy the arp payload: based on 2 bytes of mac and fill the IP */
147         arp_data_out->ar_sha[0] = arp_data_in->ar_sha[0];
148         arp_data_out->ar_sha[1] = arp_data_in->ar_sha[1];
149         memcpy(&arp_data_out->ar_sha[2], &arp_data_in->ar_tip[0], 4);
150         memcpy(&arp_data_out->ar_sip[0], &arp_data_in->ar_tip[0], 4);
151         memcpy(&arp_data_out->ar_tha[0], &arp_data_in->ar_sha[0], 6);
152         memcpy(&arp_data_out->ar_tip[0], &arp_data_in->ar_sip[0], 4);
153
154         /* Fill the destination mac with source mac of the received packet */
155         memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
156         /* Fill the source mac with nic's source mac */
157         memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
158
159         /* Alloc skb and reserve align */
160         skb_out = dev_alloc_skb(skb_in->len);
161         if (!skb_out)
162                 return -ENOMEM;
163         skb_reserve(skb_out, NET_IP_ALIGN);
164
165         memcpy(skb_put(skb_out, mac_header_len), mac_header_data,
166                mac_header_len);
167         memcpy(skb_put(skb_out, sizeof(struct arphdr)), arp_out,
168                sizeof(struct arphdr));
169         memcpy(skb_put(skb_out, sizeof(struct arpdata)), arp_data_out,
170                sizeof(struct arpdata));
171
172         skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
173         skb_out->dev = skb_in->dev;
174         skb_reset_mac_header(skb_out);
175         skb_pull(skb_out, ETH_HLEN);
176
177         gdm_lte_rx(skb_out, nic, nic_type);
178
179         return 0;
180 }
181
182 static int icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
183 {
184         unsigned short *w = ptr;
185         int sum = 0;
186         int i;
187
188         union {
189                 struct {
190                         u8 ph_src[16];
191                         u8 ph_dst[16];
192                         u32 ph_len;
193                         u8 ph_zero[3];
194                         u8 ph_nxt;
195                 } ph __packed;
196                 u16 pa[20];
197         } pseudo_header;
198
199         memset(&pseudo_header, 0, sizeof(pseudo_header));
200         memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
201         memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
202         pseudo_header.ph.ph_len = ipv6->payload_len;
203         pseudo_header.ph.ph_nxt = ipv6->nexthdr;
204
205         w = (u16 *)&pseudo_header;
206         for (i = 0; i < ARRAY_SIZE(pseudo_header.pa); i++)
207                 sum += pseudo_header.pa[i];
208
209         w = ptr;
210         while (len > 1) {
211                 sum += *w++;
212                 len -= 2;
213         }
214
215         sum = (sum >> 16) + (sum & 0xFFFF);
216         sum += (sum >> 16);
217         sum = ~sum & 0xffff;
218
219         return sum;
220 }
221
222 static int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
223 {
224         struct nic *nic = netdev_priv(skb_in->dev);
225         struct sk_buff *skb_out;
226         struct ethhdr eth;
227         struct vlan_ethhdr vlan_eth;
228         struct neighbour_advertisement {
229                 u8 target_address[16];
230                 u8 type;
231                 u8 length;
232                 u8 link_layer_address[6];
233         };
234         struct neighbour_advertisement na;
235         struct neighbour_solicitation {
236                 u8 target_address[16];
237         };
238         struct neighbour_solicitation *ns;
239         struct ipv6hdr *ipv6_in;
240         struct ipv6hdr ipv6_out;
241         struct icmp6hdr *icmp6_in;
242         struct icmp6hdr icmp6_out;
243
244         void *mac_header_data;
245         u32 mac_header_len;
246
247         /* Format the mac header so that it can be put to skb */
248         if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
249                 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
250                 if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
251                         return -1;
252                 mac_header_data = &vlan_eth;
253                 mac_header_len = VLAN_ETH_HLEN;
254         } else {
255                 memcpy(&eth, skb_in->data, sizeof(struct ethhdr));
256                 if (ntohs(eth.h_proto) != ETH_P_IPV6)
257                         return -1;
258                 mac_header_data = &eth;
259                 mac_header_len = ETH_HLEN;
260         }
261
262         /* Check if this is IPv6 ICMP packet */
263         ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
264         if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
265                 return -1;
266
267         /* Check if this is NDP packet */
268         icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len +
269                                         sizeof(struct ipv6hdr));
270         if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
271                 return -1;
272         } else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
273                 /* Check NS */
274                 u8 icmp_na[sizeof(struct icmp6hdr) +
275                         sizeof(struct neighbour_advertisement)];
276                 u8 zero_addr8[16] = {0,};
277
278                 if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
279                         /* Duplicate Address Detection: Source IP is all zero */
280                         return 0;
281
282                 icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
283                 icmp6_out.icmp6_code = 0;
284                 icmp6_out.icmp6_cksum = 0;
285                 /* R=0, S=1, O=1 */
286                 icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000);
287
288                 ns = (struct neighbour_solicitation *)
289                         (skb_in->data + mac_header_len +
290                          sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
291                 memcpy(&na.target_address, ns->target_address, 16);
292                 na.type = 0x02;
293                 na.length = 1;
294                 na.link_layer_address[0] = 0x00;
295                 na.link_layer_address[1] = 0x0a;
296                 na.link_layer_address[2] = 0x3b;
297                 na.link_layer_address[3] = 0xaf;
298                 na.link_layer_address[4] = 0x63;
299                 na.link_layer_address[5] = 0xc7;
300
301                 memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
302                 memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
303                 memcpy(ipv6_out.daddr.in6_u.u6_addr8,
304                        ipv6_in->saddr.in6_u.u6_addr8, 16);
305                 ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) +
306                                 sizeof(struct neighbour_advertisement));
307
308                 memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
309                 memcpy(icmp_na + sizeof(struct icmp6hdr), &na,
310                        sizeof(struct neighbour_advertisement));
311
312                 icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out,
313                                         (u16 *)icmp_na, sizeof(icmp_na));
314         } else {
315                 return -1;
316         }
317
318         /* Fill the destination mac with source mac of the received packet */
319         memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
320         /* Fill the source mac with nic's source mac */
321         memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
322
323         /* Alloc skb and reserve align */
324         skb_out = dev_alloc_skb(skb_in->len);
325         if (!skb_out)
326                 return -ENOMEM;
327         skb_reserve(skb_out, NET_IP_ALIGN);
328
329         memcpy(skb_put(skb_out, mac_header_len), mac_header_data,
330                mac_header_len);
331         memcpy(skb_put(skb_out, sizeof(struct ipv6hdr)), &ipv6_out,
332                sizeof(struct ipv6hdr));
333         memcpy(skb_put(skb_out, sizeof(struct icmp6hdr)), &icmp6_out,
334                sizeof(struct icmp6hdr));
335         memcpy(skb_put(skb_out, sizeof(struct neighbour_advertisement)), &na,
336                sizeof(struct neighbour_advertisement));
337
338         skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
339         skb_out->dev = skb_in->dev;
340         skb_reset_mac_header(skb_out);
341         skb_pull(skb_out, ETH_HLEN);
342
343         gdm_lte_rx(skb_out, nic, nic_type);
344
345         return 0;
346 }
347
348 static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
349 {
350         struct nic *nic = netdev_priv(dev);
351         struct ethhdr *eth;
352         struct vlan_ethhdr *vlan_eth;
353         struct iphdr *ip;
354         struct ipv6hdr *ipv6;
355         int mac_proto;
356         void *network_data;
357         u32 nic_type = 0;
358
359         /* NIC TYPE is based on the nic_id of this net_device */
360         nic_type = 0x00000010 | nic->nic_id;
361
362         /* Get ethernet protocol */
363         eth = (struct ethhdr *)skb->data;
364         if (ntohs(eth->h_proto) == ETH_P_8021Q) {
365                 vlan_eth = (struct vlan_ethhdr *)skb->data;
366                 mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
367                 network_data = skb->data + VLAN_ETH_HLEN;
368                 nic_type |= NIC_TYPE_F_VLAN;
369         } else {
370                 mac_proto = ntohs(eth->h_proto);
371                 network_data = skb->data + ETH_HLEN;
372         }
373
374         /* Process packet for nic type */
375         switch (mac_proto) {
376         case ETH_P_ARP:
377                 nic_type |= NIC_TYPE_ARP;
378                 break;
379         case ETH_P_IP:
380                 nic_type |= NIC_TYPE_F_IPV4;
381                 ip = network_data;
382
383                 /* Check DHCPv4 */
384                 if (ip->protocol == IPPROTO_UDP) {
385                         struct udphdr *udp =
386                                         network_data + sizeof(struct iphdr);
387                         if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
388                                 nic_type |= NIC_TYPE_F_DHCP;
389                 }
390                 break;
391         case ETH_P_IPV6:
392                 nic_type |= NIC_TYPE_F_IPV6;
393                 ipv6 = network_data;
394
395                 if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
396                         struct icmp6hdr *icmp6 =
397                                         network_data + sizeof(struct ipv6hdr);
398                         if (icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
399                                 nic_type |= NIC_TYPE_ICMPV6;
400                 } else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
401                         struct udphdr *udp =
402                                         network_data + sizeof(struct ipv6hdr);
403                         if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
404                                 nic_type |= NIC_TYPE_F_DHCP;
405                 }
406                 break;
407         default:
408                 break;
409         }
410
411         return nic_type;
412 }
413
414 static int gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
415 {
416         struct nic *nic = netdev_priv(dev);
417         u32 nic_type;
418         void *data_buf;
419         int data_len;
420         int idx;
421         int ret = 0;
422
423         nic_type = gdm_lte_tx_nic_type(dev, skb);
424         if (nic_type == 0) {
425                 netdev_err(dev, "tx - invalid nic_type\n");
426                 return -1;
427         }
428
429         if (nic_type & NIC_TYPE_ARP) {
430                 if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
431                         dev_kfree_skb(skb);
432                         return 0;
433                 }
434         }
435
436         if (nic_type & NIC_TYPE_ICMPV6) {
437                 if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
438                         dev_kfree_skb(skb);
439                         return 0;
440                 }
441         }
442
443         /*
444          * Need byte shift (that is, remove VLAN tag) if there is one
445          * For the case of ARP, this breaks the offset as vlan_ethhdr+4
446          * is treated as ethhdr However, it shouldn't be a problem as
447          * the response starts from arp_hdr and ethhdr is created by this
448          * driver based on the NIC mac
449          */
450         if (nic_type & NIC_TYPE_F_VLAN) {
451                 struct vlan_ethhdr *vlan_eth = (struct vlan_ethhdr *)skb->data;
452
453                 nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
454                 data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
455                 data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
456         } else {
457                 nic->vlan_id = 0;
458                 data_buf = skb->data;
459                 data_len = skb->len;
460         }
461
462         /* If it is a ICMPV6 packet, clear all the other bits :
463          * for backward compatibility with the firmware
464          */
465         if (nic_type & NIC_TYPE_ICMPV6)
466                 nic_type = NIC_TYPE_ICMPV6;
467
468         /* If it is not a dhcp packet, clear all the flag bits :
469          * original NIC, otherwise the special flag (IPVX | DHCP)
470          */
471         if (!(nic_type & NIC_TYPE_F_DHCP))
472                 nic_type &= NIC_TYPE_MASK;
473
474         ret = sscanf(dev->name, "lte%d", &idx);
475         if (ret != 1) {
476                 dev_kfree_skb(skb);
477                 return -EINVAL;
478         }
479
480         ret = nic->phy_dev->send_sdu_func(nic->phy_dev->priv_dev,
481                                           data_buf, data_len,
482                                           nic->pdn_table.dft_eps_id, 0,
483                                           tx_complete, nic, idx,
484                                           nic_type);
485
486         if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
487                 netif_stop_queue(dev);
488                 if (ret == TX_NO_BUFFER)
489                         ret = 0;
490                 else
491                         ret = -ENOSPC;
492         } else if (ret == TX_NO_DEV) {
493                 ret = -ENODEV;
494         }
495
496         /* Updates tx stats */
497         if (ret) {
498                 nic->stats.tx_dropped++;
499         } else {
500                 nic->stats.tx_packets++;
501                 nic->stats.tx_bytes += data_len;
502         }
503         dev_kfree_skb(skb);
504
505         return 0;
506 }
507
508 static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
509 {
510         struct nic *nic = netdev_priv(dev);
511
512         return &nic->stats;
513 }
514
515 static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
516 {
517         struct nic *nic = netdev_priv(dev);
518         struct hci_packet *hci = (struct hci_packet *)buf;
519         int idx;
520         int ret;
521
522         ret = sscanf(dev->name, "lte%d", &idx);
523         if (ret != 1)
524                 return -EINVAL;
525
526         return netlink_send(lte_event.sock, idx, 0, buf,
527                             gdm_dev16_to_cpu(
528                                     nic->phy_dev->get_endian(
529                                             nic->phy_dev->priv_dev), hci->len)
530                             + HCI_HEADER_SIZE);
531 }
532
533 static void gdm_lte_event_rcv(struct net_device *dev, u16 type,
534                               void *msg, int len)
535 {
536         struct nic *nic = netdev_priv(dev);
537
538         nic->phy_dev->send_hci_func(nic->phy_dev->priv_dev, msg, len, NULL,
539                                     NULL);
540 }
541
542 int gdm_lte_event_init(void)
543 {
544         if (lte_event.ref_cnt == 0)
545                 lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
546
547         if (lte_event.sock) {
548                 lte_event.ref_cnt++;
549                 return 0;
550         }
551
552         pr_err("event init failed\n");
553         return -1;
554 }
555
556 void gdm_lte_event_exit(void)
557 {
558         if (lte_event.sock && --lte_event.ref_cnt == 0) {
559                 sock_release(lte_event.sock->sk_socket);
560                 lte_event.sock = NULL;
561         }
562 }
563
564 static u8 find_dev_index(u32 nic_type)
565 {
566         u8 index;
567
568         index = (u8)(nic_type & 0x0000000f);
569         if (index > MAX_NIC_TYPE)
570                 index = 0;
571
572         return index;
573 }
574
575 static void gdm_lte_netif_rx(struct net_device *dev, char *buf,
576                              int len, int flagged_nic_type)
577 {
578         u32 nic_type;
579         struct nic *nic;
580         struct sk_buff *skb;
581         struct ethhdr eth;
582         struct vlan_ethhdr vlan_eth;
583         void *mac_header_data;
584         u32 mac_header_len;
585         char ip_version = 0;
586
587         nic_type = flagged_nic_type & NIC_TYPE_MASK;
588         nic = netdev_priv(dev);
589
590         if (flagged_nic_type & NIC_TYPE_F_DHCP) {
591                 /* Change the destination mac address
592                  * with the one requested the IP
593                  */
594                 if (flagged_nic_type & NIC_TYPE_F_IPV4) {
595                         struct dhcp_packet {
596                                 u8 op;      /* BOOTREQUEST or BOOTREPLY */
597                                 u8 htype;   /* hardware address type.
598                                              * 1 = 10mb ethernet
599                                              */
600                                 u8 hlen;    /* hardware address length */
601                                 u8 hops;    /* used by relay agents only */
602                                 u32 xid;    /* unique id */
603                                 u16 secs;   /* elapsed since client began
604                                              * acquisition/renewal
605                                              */
606                                 u16 flags;  /* only one flag so far: */
607                                 #define BROADCAST_FLAG 0x8000
608                                 /* "I need broadcast replies" */
609                                 u32 ciaddr; /* client IP (if client is in
610                                              * BOUND, RENEW or REBINDING state)
611                                              */
612                                 u32 yiaddr; /* 'your' (client) IP address */
613                                 /* IP address of next server to use in
614                                  * bootstrap, returned in DHCPOFFER,
615                                  * DHCPACK by server
616                                  */
617                                 u32 siaddr_nip;
618                                 u32 gateway_nip; /* relay agent IP address */
619                                 u8 chaddr[16];   /* link-layer client hardware
620                                                   * address (MAC)
621                                                   */
622                                 u8 sname[64];    /* server host name (ASCIZ) */
623                                 u8 file[128];    /* boot file name (ASCIZ) */
624                                 u32 cookie;      /* fixed first four option
625                                                   * bytes (99,130,83,99 dec)
626                                                   */
627                         } __packed;
628                         int offset = sizeof(struct iphdr) +
629                                      sizeof(struct udphdr) +
630                                      offsetof(struct dhcp_packet, chaddr);
631                         if (offset + ETH_ALEN > len)
632                                 return;
633                         ether_addr_copy(nic->dest_mac_addr, buf + offset);
634                 }
635         }
636
637         if (nic->vlan_id > 0) {
638                 mac_header_data = (void *)&vlan_eth;
639                 mac_header_len = VLAN_ETH_HLEN;
640         } else {
641                 mac_header_data = (void *)&eth;
642                 mac_header_len = ETH_HLEN;
643         }
644
645         /* Format the data so that it can be put to skb */
646         ether_addr_copy(mac_header_data, nic->dest_mac_addr);
647         memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
648
649         vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
650         vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
651
652         if (nic_type == NIC_TYPE_ARP) {
653                 /* Should be response: Only happens because
654                  * there was a request from the host
655                  */
656                 eth.h_proto = htons(ETH_P_ARP);
657                 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
658         } else {
659                 ip_version = buf[0] >> 4;
660                 if (ip_version == IP_VERSION_4) {
661                         eth.h_proto = htons(ETH_P_IP);
662                         vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
663                 } else if (ip_version == IP_VERSION_6) {
664                         eth.h_proto = htons(ETH_P_IPV6);
665                         vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
666                 } else {
667                         netdev_err(dev, "Unknown IP version %d\n", ip_version);
668                         return;
669                 }
670         }
671
672         /* Alloc skb and reserve align */
673         skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
674         if (!skb)
675                 return;
676         skb_reserve(skb, NET_IP_ALIGN);
677
678         memcpy(skb_put(skb, mac_header_len), mac_header_data, mac_header_len);
679         memcpy(skb_put(skb, len), buf, len);
680
681         skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
682         skb->dev = dev;
683         skb_reset_mac_header(skb);
684         skb_pull(skb, ETH_HLEN);
685
686         gdm_lte_rx(skb, nic, nic_type);
687 }
688
689 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
690 {
691         struct net_device *dev;
692         struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
693         struct sdu *sdu = NULL;
694         u8 *data = (u8 *)multi_sdu->data;
695         int copied;
696         u16 i = 0;
697         u16 num_packet;
698         u16 hci_len;
699         u16 cmd_evt;
700         u32 nic_type;
701         u8 index;
702
703         hci_len = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
704                                    multi_sdu->len);
705         num_packet = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
706                                       multi_sdu->num_packet);
707
708         for (i = 0; i < num_packet; i++) {
709                 copied = data - multi_sdu->data;
710                 if (len < copied + sizeof(*sdu)) {
711                         pr_err("rx prevent buffer overflow");
712                         return;
713                 }
714
715                 sdu = (struct sdu *)data;
716
717                 cmd_evt = gdm_dev16_to_cpu(phy_dev->
718                                 get_endian(phy_dev->priv_dev), sdu->cmd_evt);
719                 hci_len = gdm_dev16_to_cpu(phy_dev->
720                                 get_endian(phy_dev->priv_dev), sdu->len);
721                 nic_type = gdm_dev32_to_cpu(phy_dev->
722                                 get_endian(phy_dev->priv_dev), sdu->nic_type);
723
724                 if (cmd_evt != LTE_RX_SDU) {
725                         pr_err("rx sdu wrong hci %04x\n", cmd_evt);
726                         return;
727                 }
728                 if (hci_len < 12 ||
729                     len < copied + sizeof(*sdu) + (hci_len - 12)) {
730                         pr_err("rx sdu invalid len %d\n", hci_len);
731                         return;
732                 }
733
734                 index = find_dev_index(nic_type);
735                 if (index < MAX_NIC_TYPE) {
736                         dev = phy_dev->dev[index];
737                         gdm_lte_netif_rx(dev, (char *)sdu->data,
738                                          (int)(hci_len - 12), nic_type);
739                 } else {
740                         pr_err("rx sdu invalid nic_type :%x\n", nic_type);
741                 }
742
743                 data += ((hci_len + 3) & 0xfffc) + HCI_HEADER_SIZE;
744         }
745 }
746
747 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
748 {
749         struct nic *nic = netdev_priv(dev);
750         struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
751
752         if (pdn_table->activate) {
753                 nic->pdn_table.activate = pdn_table->activate;
754                 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(
755                                                 nic->phy_dev->get_endian(
756                                                         nic->phy_dev->priv_dev),
757                                                 pdn_table->dft_eps_id);
758                 nic->pdn_table.nic_type = gdm_dev32_to_cpu(
759                                                 nic->phy_dev->get_endian(
760                                                         nic->phy_dev->priv_dev),
761                                                 pdn_table->nic_type);
762
763                 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
764                             nic->pdn_table.nic_type);
765         } else {
766                 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
767                 netdev_info(dev, "pdn deactivated\n");
768         }
769 }
770
771 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
772 {
773         struct hci_packet *hci = (struct hci_packet *)buf;
774         struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
775         struct sdu *sdu;
776         struct net_device *dev;
777         int ret = 0;
778         u16 cmd_evt;
779         u32 nic_type;
780         u8 index;
781
782         if (!len)
783                 return ret;
784
785         cmd_evt = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
786                                    hci->cmd_evt);
787
788         dev = phy_dev->dev[0];
789         if (!dev)
790                 return 0;
791
792         switch (cmd_evt) {
793         case LTE_RX_SDU:
794                 sdu = (struct sdu *)hci->data;
795                 nic_type = gdm_dev32_to_cpu(phy_dev->
796                                 get_endian(phy_dev->priv_dev), sdu->nic_type);
797                 index = find_dev_index(nic_type);
798                 dev = phy_dev->dev[index];
799                 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
800                 break;
801         case LTE_RX_MULTI_SDU:
802                 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
803                 break;
804         case LTE_LINK_ON_OFF_INDICATION:
805                 netdev_info(dev, "link %s\n",
806                             ((struct hci_connect_ind *)buf)->connect
807                             ? "on" : "off");
808                 break;
809         case LTE_PDN_TABLE_IND:
810                 pdn_table = (struct hci_pdn_table_ind *)buf;
811                 nic_type = gdm_dev32_to_cpu(phy_dev->
812                                 get_endian(phy_dev->priv_dev),
813                                 pdn_table->nic_type);
814                 index = find_dev_index(nic_type);
815                 dev = phy_dev->dev[index];
816                 gdm_lte_pdn_table(dev, buf, len);
817                 /* Fall through */
818         default:
819                 ret = gdm_lte_event_send(dev, buf, len);
820                 break;
821         }
822
823         return ret;
824 }
825
826 static int rx_complete(void *arg, void *data, int len, int context)
827 {
828         struct phy_dev *phy_dev = arg;
829
830         return gdm_lte_receive_pkt(phy_dev, data, len);
831 }
832
833 void start_rx_proc(struct phy_dev *phy_dev)
834 {
835         int i;
836
837         for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
838                 phy_dev->rcv_func(phy_dev->priv_dev,
839                                 rx_complete, phy_dev, USB_COMPLETE);
840 }
841
842 static const struct net_device_ops gdm_netdev_ops = {
843         .ndo_open                       = gdm_lte_open,
844         .ndo_stop                       = gdm_lte_close,
845         .ndo_set_config                 = gdm_lte_set_config,
846         .ndo_start_xmit                 = gdm_lte_tx,
847         .ndo_get_stats                  = gdm_lte_stats,
848 };
849
850 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
851
852 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
853                              u8 *mac_address, u8 index)
854 {
855         /* Form the dev_addr */
856         if (!mac_address)
857                 ether_addr_copy(dev_addr, gdm_lte_macaddr);
858         else
859                 ether_addr_copy(dev_addr, mac_address);
860
861         /* The last byte of the mac address
862          * should be less than or equal to 0xFC
863          */
864         dev_addr[ETH_ALEN - 1] += index;
865
866         /* Create random nic src and copy the first
867          * 3 bytes to be the same as dev_addr
868          */
869         eth_random_addr(nic_src);
870         memcpy(nic_src, dev_addr, 3);
871
872         /* Copy the nic_dest from dev_addr*/
873         ether_addr_copy(nic_dest, dev_addr);
874 }
875
876 static void validate_mac_address(u8 *mac_address)
877 {
878         /* if zero address or multicast bit set, restore the default value */
879         if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
880                 pr_err("MAC invalid, restoring default\n");
881                 memcpy(mac_address, gdm_lte_macaddr, 6);
882         }
883 }
884
885 int register_lte_device(struct phy_dev *phy_dev,
886                         struct device *dev, u8 *mac_address)
887 {
888         struct nic *nic;
889         struct net_device *net;
890         char pdn_dev_name[16];
891         int ret = 0;
892         u8 index;
893
894         validate_mac_address(mac_address);
895
896         for (index = 0; index < MAX_NIC_TYPE; index++) {
897                 /* Create device name lteXpdnX */
898                 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
899
900                 /* Allocate netdev */
901                 net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
902                                    NET_NAME_UNKNOWN, ether_setup);
903                 if (!net) {
904                         pr_err("alloc_netdev failed\n");
905                         ret = -ENOMEM;
906                         goto err;
907                 }
908                 net->netdev_ops = &gdm_netdev_ops;
909                 net->flags &= ~IFF_MULTICAST;
910                 net->mtu = DEFAULT_MTU_SIZE;
911
912                 nic = netdev_priv(net);
913                 memset(nic, 0, sizeof(struct nic));
914                 nic->netdev = net;
915                 nic->phy_dev = phy_dev;
916                 nic->nic_id = index;
917
918                 form_mac_address(
919                                 net->dev_addr,
920                                 nic->src_mac_addr,
921                                 nic->dest_mac_addr,
922                                 mac_address,
923                                 index);
924
925                 SET_NETDEV_DEV(net, dev);
926                 SET_NETDEV_DEVTYPE(net, &wwan_type);
927
928                 ret = register_netdev(net);
929                 if (ret)
930                         goto err;
931
932                 netif_carrier_on(net);
933
934                 phy_dev->dev[index] = net;
935         }
936
937         return 0;
938
939 err:
940         unregister_lte_device(phy_dev);
941
942         return ret;
943 }
944
945 void unregister_lte_device(struct phy_dev *phy_dev)
946 {
947         struct net_device *net;
948         int index;
949
950         for (index = 0; index < MAX_NIC_TYPE; index++) {
951                 net = phy_dev->dev[index];
952                 if (!net)
953                         continue;
954
955                 unregister_netdev(net);
956                 free_netdev(net);
957         }
958 }