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