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