GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / staging / wilc1000 / wilc_wlan.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4  * All rights reserved.
5  */
6
7 #include <linux/if_ether.h>
8 #include <linux/ip.h>
9 #include "wilc_wfi_netdevice.h"
10 #include "wilc_wlan_cfg.h"
11
12 static enum chip_ps_states chip_ps_state = CHIP_WAKEDUP;
13
14 static inline bool is_wilc1000(u32 id)
15 {
16         return ((id & 0xfffff000) == 0x100000 ? true : false);
17 }
18
19 static inline void acquire_bus(struct wilc *wilc, enum bus_acquire acquire)
20 {
21         mutex_lock(&wilc->hif_cs);
22         if (acquire == ACQUIRE_AND_WAKEUP)
23                 chip_wakeup(wilc);
24 }
25
26 static inline void release_bus(struct wilc *wilc, enum bus_release release)
27 {
28         if (release == RELEASE_ALLOW_SLEEP)
29                 chip_allow_sleep(wilc);
30         mutex_unlock(&wilc->hif_cs);
31 }
32
33 static void wilc_wlan_txq_remove(struct wilc *wilc, struct txq_entry_t *tqe)
34 {
35         list_del(&tqe->list);
36         wilc->txq_entries -= 1;
37 }
38
39 static struct txq_entry_t *
40 wilc_wlan_txq_remove_from_head(struct net_device *dev)
41 {
42         struct txq_entry_t *tqe = NULL;
43         unsigned long flags;
44         struct wilc_vif *vif = netdev_priv(dev);
45         struct wilc *wilc = vif->wilc;
46
47         spin_lock_irqsave(&wilc->txq_spinlock, flags);
48
49         if (!list_empty(&wilc->txq_head.list)) {
50                 tqe = list_first_entry(&wilc->txq_head.list, struct txq_entry_t,
51                                        list);
52                 list_del(&tqe->list);
53                 wilc->txq_entries -= 1;
54         }
55         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
56         return tqe;
57 }
58
59 static void wilc_wlan_txq_add_to_tail(struct net_device *dev,
60                                       struct txq_entry_t *tqe)
61 {
62         unsigned long flags;
63         struct wilc_vif *vif = netdev_priv(dev);
64         struct wilc *wilc = vif->wilc;
65
66         spin_lock_irqsave(&wilc->txq_spinlock, flags);
67
68         list_add_tail(&tqe->list, &wilc->txq_head.list);
69         wilc->txq_entries += 1;
70
71         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
72
73         complete(&wilc->txq_event);
74 }
75
76 static int wilc_wlan_txq_add_to_head(struct wilc_vif *vif,
77                                      struct txq_entry_t *tqe)
78 {
79         unsigned long flags;
80         struct wilc *wilc = vif->wilc;
81
82         mutex_lock(&wilc->txq_add_to_head_cs);
83
84         spin_lock_irqsave(&wilc->txq_spinlock, flags);
85
86         list_add(&tqe->list, &wilc->txq_head.list);
87         wilc->txq_entries += 1;
88
89         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
90         mutex_unlock(&wilc->txq_add_to_head_cs);
91         complete(&wilc->txq_event);
92
93         return 0;
94 }
95
96 struct ack_session_info;
97 struct ack_session_info {
98         u32 seq_num;
99         u32 bigger_ack_num;
100         u16 src_port;
101         u16 dst_port;
102         u16 status;
103 };
104
105 struct pending_acks_info {
106         u32 ack_num;
107         u32 session_index;
108         struct txq_entry_t  *txqe;
109 };
110
111 #define NOT_TCP_ACK                     (-1)
112
113 #define MAX_TCP_SESSION         25
114 #define MAX_PENDING_ACKS                256
115 static struct ack_session_info ack_session_info[2 * MAX_TCP_SESSION];
116 static struct pending_acks_info pending_acks_info[MAX_PENDING_ACKS];
117
118 static u32 pending_base;
119 static u32 tcp_session;
120 static u32 pending_acks;
121
122 static inline int add_tcp_session(u32 src_prt, u32 dst_prt, u32 seq)
123 {
124         if (tcp_session < 2 * MAX_TCP_SESSION) {
125                 ack_session_info[tcp_session].seq_num = seq;
126                 ack_session_info[tcp_session].bigger_ack_num = 0;
127                 ack_session_info[tcp_session].src_port = src_prt;
128                 ack_session_info[tcp_session].dst_port = dst_prt;
129                 tcp_session++;
130         }
131         return 0;
132 }
133
134 static inline int update_tcp_session(u32 index, u32 ack)
135 {
136         if (index < 2 * MAX_TCP_SESSION &&
137             ack > ack_session_info[index].bigger_ack_num)
138                 ack_session_info[index].bigger_ack_num = ack;
139         return 0;
140 }
141
142 static inline int add_tcp_pending_ack(u32 ack, u32 session_index,
143                                       struct txq_entry_t *txqe)
144 {
145         u32 i = pending_base + pending_acks;
146
147         if (i < MAX_PENDING_ACKS) {
148                 pending_acks_info[i].ack_num = ack;
149                 pending_acks_info[i].txqe = txqe;
150                 pending_acks_info[i].session_index = session_index;
151                 txqe->tcp_pending_ack_idx = i;
152                 pending_acks++;
153         }
154         return 0;
155 }
156
157 static inline void tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
158 {
159         void *buffer = tqe->buffer;
160         const struct ethhdr *eth_hdr_ptr = buffer;
161         int i;
162         unsigned long flags;
163         struct wilc_vif *vif = netdev_priv(dev);
164         struct wilc *wilc = vif->wilc;
165
166         spin_lock_irqsave(&wilc->txq_spinlock, flags);
167
168         if (eth_hdr_ptr->h_proto == htons(ETH_P_IP)) {
169                 const struct iphdr *ip_hdr_ptr = buffer + ETH_HLEN;
170
171                 if (ip_hdr_ptr->protocol == IPPROTO_TCP) {
172                         const struct tcphdr *tcp_hdr_ptr;
173                         u32 IHL, total_length, data_offset;
174
175                         IHL = ip_hdr_ptr->ihl << 2;
176                         tcp_hdr_ptr = buffer + ETH_HLEN + IHL;
177                         total_length = ntohs(ip_hdr_ptr->tot_len);
178
179                         data_offset = tcp_hdr_ptr->doff << 2;
180                         if (total_length == (IHL + data_offset)) {
181                                 u32 seq_no, ack_no;
182
183                                 seq_no = ntohl(tcp_hdr_ptr->seq);
184                                 ack_no = ntohl(tcp_hdr_ptr->ack_seq);
185                                 for (i = 0; i < tcp_session; i++) {
186                                         u32 j = ack_session_info[i].seq_num;
187
188                                         if (i < 2 * MAX_TCP_SESSION &&
189                                             j == seq_no) {
190                                                 update_tcp_session(i, ack_no);
191                                                 break;
192                                         }
193                                 }
194                                 if (i == tcp_session)
195                                         add_tcp_session(0, 0, seq_no);
196
197                                 add_tcp_pending_ack(ack_no, i, tqe);
198                         }
199                 }
200         }
201         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
202 }
203
204 static int wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
205 {
206         struct wilc_vif *vif = netdev_priv(dev);
207         struct wilc *wilc = vif->wilc;
208         u32 i = 0;
209         u32 dropped = 0;
210         unsigned long flags;
211
212         spin_lock_irqsave(&wilc->txq_spinlock, flags);
213         for (i = pending_base; i < (pending_base + pending_acks); i++) {
214                 u32 session_index;
215                 u32 bigger_ack_num;
216
217                 if (i >= MAX_PENDING_ACKS)
218                         break;
219
220                 session_index = pending_acks_info[i].session_index;
221
222                 if (session_index >= 2 * MAX_TCP_SESSION)
223                         break;
224
225                 bigger_ack_num = ack_session_info[session_index].bigger_ack_num;
226
227                 if (pending_acks_info[i].ack_num < bigger_ack_num) {
228                         struct txq_entry_t *tqe;
229
230                         tqe = pending_acks_info[i].txqe;
231                         if (tqe) {
232                                 wilc_wlan_txq_remove(wilc, tqe);
233                                 tqe->status = 1;
234                                 if (tqe->tx_complete_func)
235                                         tqe->tx_complete_func(tqe->priv,
236                                                               tqe->status);
237                                 kfree(tqe);
238                                 dropped++;
239                         }
240                 }
241         }
242         pending_acks = 0;
243         tcp_session = 0;
244
245         if (pending_base == 0)
246                 pending_base = MAX_TCP_SESSION;
247         else
248                 pending_base = 0;
249
250         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
251
252         while (dropped > 0) {
253                 wait_for_completion_timeout(&wilc->txq_event,
254                                             msecs_to_jiffies(1));
255                 dropped--;
256         }
257
258         return 1;
259 }
260
261 static bool enabled;
262
263 void wilc_enable_tcp_ack_filter(bool value)
264 {
265         enabled = value;
266 }
267
268 static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
269                                      u32 buffer_size)
270 {
271         struct txq_entry_t *tqe;
272         struct wilc *wilc = vif->wilc;
273
274         netdev_dbg(vif->ndev, "Adding config packet ...\n");
275         if (wilc->quit) {
276                 netdev_dbg(vif->ndev, "Return due to clear function\n");
277                 complete(&wilc->cfg_event);
278                 return 0;
279         }
280
281         tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
282         if (!tqe)
283                 return 0;
284
285         tqe->type = WILC_CFG_PKT;
286         tqe->buffer = buffer;
287         tqe->buffer_size = buffer_size;
288         tqe->tx_complete_func = NULL;
289         tqe->priv = NULL;
290         tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
291
292         if (wilc_wlan_txq_add_to_head(vif, tqe)) {
293                 kfree(tqe);
294                 return 0;
295         }
296
297         return 1;
298 }
299
300 int wilc_wlan_txq_add_net_pkt(struct net_device *dev, void *priv, u8 *buffer,
301                               u32 buffer_size, wilc_tx_complete_func_t func)
302 {
303         struct txq_entry_t *tqe;
304         struct wilc_vif *vif = netdev_priv(dev);
305         struct wilc *wilc;
306
307         wilc = vif->wilc;
308
309         if (wilc->quit)
310                 return 0;
311
312         tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
313
314         if (!tqe)
315                 return 0;
316         tqe->type = WILC_NET_PKT;
317         tqe->buffer = buffer;
318         tqe->buffer_size = buffer_size;
319         tqe->tx_complete_func = func;
320         tqe->priv = priv;
321
322         tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
323         if (enabled)
324                 tcp_process(dev, tqe);
325         wilc_wlan_txq_add_to_tail(dev, tqe);
326         return wilc->txq_entries;
327 }
328
329 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
330                                u32 buffer_size, wilc_tx_complete_func_t func)
331 {
332         struct txq_entry_t *tqe;
333         struct wilc_vif *vif = netdev_priv(dev);
334         struct wilc *wilc;
335
336         wilc = vif->wilc;
337
338         if (wilc->quit)
339                 return 0;
340
341         tqe = kmalloc(sizeof(*tqe), GFP_KERNEL);
342
343         if (!tqe)
344                 return 0;
345         tqe->type = WILC_MGMT_PKT;
346         tqe->buffer = buffer;
347         tqe->buffer_size = buffer_size;
348         tqe->tx_complete_func = func;
349         tqe->priv = priv;
350         tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
351         wilc_wlan_txq_add_to_tail(dev, tqe);
352         return 1;
353 }
354
355 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc)
356 {
357         struct txq_entry_t *tqe = NULL;
358         unsigned long flags;
359
360         spin_lock_irqsave(&wilc->txq_spinlock, flags);
361
362         if (!list_empty(&wilc->txq_head.list))
363                 tqe = list_first_entry(&wilc->txq_head.list, struct txq_entry_t,
364                                        list);
365
366         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
367
368         return tqe;
369 }
370
371 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
372                                                   struct txq_entry_t *tqe)
373 {
374         unsigned long flags;
375
376         spin_lock_irqsave(&wilc->txq_spinlock, flags);
377
378         if (!list_is_last(&tqe->list, &wilc->txq_head.list))
379                 tqe = list_next_entry(tqe, list);
380         else
381                 tqe = NULL;
382         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
383
384         return tqe;
385 }
386
387 static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
388 {
389         if (wilc->quit)
390                 return;
391
392         mutex_lock(&wilc->rxq_cs);
393         list_add_tail(&rqe->list, &wilc->rxq_head.list);
394         mutex_unlock(&wilc->rxq_cs);
395 }
396
397 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
398 {
399         struct rxq_entry_t *rqe = NULL;
400
401         mutex_lock(&wilc->rxq_cs);
402         if (!list_empty(&wilc->rxq_head.list)) {
403                 rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
404                                        list);
405                 list_del(&rqe->list);
406         }
407         mutex_unlock(&wilc->rxq_cs);
408         return rqe;
409 }
410
411 void chip_allow_sleep(struct wilc *wilc)
412 {
413         u32 reg = 0;
414
415         wilc->hif_func->hif_read_reg(wilc, 0xf0, &reg);
416
417         wilc->hif_func->hif_write_reg(wilc, 0xf0, reg & ~BIT(0));
418         wilc->hif_func->hif_write_reg(wilc, 0xfa, 0);
419 }
420 EXPORT_SYMBOL_GPL(chip_allow_sleep);
421
422 void chip_wakeup(struct wilc *wilc)
423 {
424         u32 reg, clk_status_reg;
425
426         if ((wilc->io_type & 0x1) == HIF_SPI) {
427                 do {
428                         wilc->hif_func->hif_read_reg(wilc, 1, &reg);
429                         wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
430                         wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
431
432                         do {
433                                 usleep_range(2 * 1000, 2 * 1000);
434                                 wilc_get_chipid(wilc, true);
435                         } while (wilc_get_chipid(wilc, true) == 0);
436                 } while (wilc_get_chipid(wilc, true) == 0);
437         } else if ((wilc->io_type & 0x1) == HIF_SDIO) {
438                 wilc->hif_func->hif_write_reg(wilc, 0xfa, 1);
439                 udelay(200);
440                 wilc->hif_func->hif_read_reg(wilc, 0xf0, &reg);
441                 do {
442                         wilc->hif_func->hif_write_reg(wilc, 0xf0,
443                                                       reg | BIT(0));
444                         wilc->hif_func->hif_read_reg(wilc, 0xf1,
445                                                      &clk_status_reg);
446
447                         while ((clk_status_reg & 0x1) == 0) {
448                                 usleep_range(2 * 1000, 2 * 1000);
449
450                                 wilc->hif_func->hif_read_reg(wilc, 0xf1,
451                                                              &clk_status_reg);
452                         }
453                         if ((clk_status_reg & 0x1) == 0) {
454                                 wilc->hif_func->hif_write_reg(wilc, 0xf0,
455                                                               reg & (~BIT(0)));
456                         }
457                 } while ((clk_status_reg & 0x1) == 0);
458         }
459
460         if (chip_ps_state == CHIP_SLEEPING_MANUAL) {
461                 if (wilc_get_chipid(wilc, false) < 0x1002b0) {
462                         u32 val32;
463
464                         wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
465                         val32 |= BIT(6);
466                         wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
467
468                         wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
469                         val32 |= BIT(6);
470                         wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
471                 }
472         }
473         chip_ps_state = CHIP_WAKEDUP;
474 }
475 EXPORT_SYMBOL_GPL(chip_wakeup);
476
477 void wilc_chip_sleep_manually(struct wilc *wilc)
478 {
479         if (chip_ps_state != CHIP_WAKEDUP)
480                 return;
481         acquire_bus(wilc, ACQUIRE_ONLY);
482
483         chip_allow_sleep(wilc);
484         wilc->hif_func->hif_write_reg(wilc, 0x10a8, 1);
485
486         chip_ps_state = CHIP_SLEEPING_MANUAL;
487         release_bus(wilc, RELEASE_ONLY);
488 }
489 EXPORT_SYMBOL_GPL(wilc_chip_sleep_manually);
490
491 void host_wakeup_notify(struct wilc *wilc)
492 {
493         acquire_bus(wilc, ACQUIRE_ONLY);
494         wilc->hif_func->hif_write_reg(wilc, 0x10b0, 1);
495         release_bus(wilc, RELEASE_ONLY);
496 }
497 EXPORT_SYMBOL_GPL(host_wakeup_notify);
498
499 void host_sleep_notify(struct wilc *wilc)
500 {
501         acquire_bus(wilc, ACQUIRE_ONLY);
502         wilc->hif_func->hif_write_reg(wilc, 0x10ac, 1);
503         release_bus(wilc, RELEASE_ONLY);
504 }
505 EXPORT_SYMBOL_GPL(host_sleep_notify);
506
507 int wilc_wlan_handle_txq(struct net_device *dev, u32 *txq_count)
508 {
509         int i, entries = 0;
510         u32 sum;
511         u32 reg;
512         u32 offset = 0;
513         int vmm_sz = 0;
514         struct txq_entry_t *tqe;
515         int ret = 0;
516         int counter;
517         int timeout;
518         u32 vmm_table[WILC_VMM_TBL_SIZE];
519         struct wilc_vif *vif = netdev_priv(dev);
520         struct wilc *wilc = vif->wilc;
521         const struct wilc_hif_func *func;
522         u8 *txb = wilc->tx_buffer;
523
524         if (wilc->quit)
525                 goto out;
526
527         mutex_lock(&wilc->txq_add_to_head_cs);
528         wilc_wlan_txq_filter_dup_tcp_ack(dev);
529         tqe = wilc_wlan_txq_get_first(wilc);
530         i = 0;
531         sum = 0;
532         do {
533                 if (tqe && (i < (WILC_VMM_TBL_SIZE - 1))) {
534                         if (tqe->type == WILC_CFG_PKT)
535                                 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
536
537                         else if (tqe->type == WILC_NET_PKT)
538                                 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
539
540                         else
541                                 vmm_sz = HOST_HDR_OFFSET;
542
543                         vmm_sz += tqe->buffer_size;
544
545                         if (vmm_sz & 0x3)
546                                 vmm_sz = (vmm_sz + 4) & ~0x3;
547
548                         if ((sum + vmm_sz) > LINUX_TX_SIZE)
549                                 break;
550
551                         vmm_table[i] = vmm_sz / 4;
552                         if (tqe->type == WILC_CFG_PKT)
553                                 vmm_table[i] |= BIT(10);
554                         cpu_to_le32s(&vmm_table[i]);
555
556                         i++;
557                         sum += vmm_sz;
558                         tqe = wilc_wlan_txq_get_next(wilc, tqe);
559                 } else {
560                         break;
561                 }
562         } while (1);
563
564         if (i == 0)
565                 goto out;
566         vmm_table[i] = 0x0;
567
568         acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
569         counter = 0;
570         func = wilc->hif_func;
571         do {
572                 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, &reg);
573                 if (!ret)
574                         break;
575
576                 if ((reg & 0x1) == 0)
577                         break;
578
579                 counter++;
580                 if (counter > 200) {
581                         counter = 0;
582                         ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
583                         break;
584                 }
585         } while (!wilc->quit);
586
587         if (!ret)
588                 goto out_release_bus;
589
590         timeout = 200;
591         do {
592                 ret = func->hif_block_tx(wilc,
593                                          WILC_VMM_TBL_RX_SHADOW_BASE,
594                                          (u8 *)vmm_table,
595                                          ((i + 1) * 4));
596                 if (!ret)
597                         break;
598
599                 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
600                 if (!ret)
601                         break;
602
603                 do {
604                         ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, &reg);
605                         if (!ret)
606                                 break;
607                         if ((reg >> 2) & 0x1) {
608                                 entries = ((reg >> 3) & 0x3f);
609                                 break;
610                         }
611                         release_bus(wilc, RELEASE_ALLOW_SLEEP);
612                 } while (--timeout);
613                 if (timeout <= 0) {
614                         ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
615                         break;
616                 }
617
618                 if (!ret)
619                         break;
620
621                 if (entries == 0) {
622                         ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, &reg);
623                         if (!ret)
624                                 break;
625                         reg &= ~BIT(0);
626                         ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
627                         if (!ret)
628                                 break;
629                         break;
630                 }
631                 break;
632         } while (1);
633
634         if (!ret)
635                 goto out_release_bus;
636
637         if (entries == 0) {
638                 ret = WILC_TX_ERR_NO_BUF;
639                 goto out_release_bus;
640         }
641
642         release_bus(wilc, RELEASE_ALLOW_SLEEP);
643
644         offset = 0;
645         i = 0;
646         do {
647                 u32 header, buffer_offset;
648                 char *bssid;
649
650                 tqe = wilc_wlan_txq_remove_from_head(dev);
651                 if (!tqe)
652                         break;
653
654                 if (vmm_table[i] == 0)
655                         break;
656
657                 le32_to_cpus(&vmm_table[i]);
658                 vmm_sz = (vmm_table[i] & 0x3ff);
659                 vmm_sz *= 4;
660                 header = (tqe->type << 31) |
661                          (tqe->buffer_size << 15) |
662                          vmm_sz;
663                 if (tqe->type == WILC_MGMT_PKT)
664                         header |= BIT(30);
665                 else
666                         header &= ~BIT(30);
667
668                 cpu_to_le32s(&header);
669                 memcpy(&txb[offset], &header, 4);
670                 if (tqe->type == WILC_CFG_PKT) {
671                         buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
672                 } else if (tqe->type == WILC_NET_PKT) {
673                         bssid = ((struct tx_complete_data *)(tqe->priv))->bssid;
674
675                         buffer_offset = ETH_ETHERNET_HDR_OFFSET;
676                         memcpy(&txb[offset + 8], bssid, 6);
677                 } else {
678                         buffer_offset = HOST_HDR_OFFSET;
679                 }
680
681                 memcpy(&txb[offset + buffer_offset],
682                        tqe->buffer, tqe->buffer_size);
683                 offset += vmm_sz;
684                 i++;
685                 tqe->status = 1;
686                 if (tqe->tx_complete_func)
687                         tqe->tx_complete_func(tqe->priv, tqe->status);
688                 if (tqe->tcp_pending_ack_idx != NOT_TCP_ACK &&
689                     tqe->tcp_pending_ack_idx < MAX_PENDING_ACKS)
690                         pending_acks_info[tqe->tcp_pending_ack_idx].txqe = NULL;
691                 kfree(tqe);
692         } while (--entries);
693
694         acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
695
696         ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
697         if (!ret)
698                 goto out_release_bus;
699
700         ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
701
702 out_release_bus:
703         release_bus(wilc, RELEASE_ALLOW_SLEEP);
704
705 out:
706         mutex_unlock(&wilc->txq_add_to_head_cs);
707
708         *txq_count = wilc->txq_entries;
709         return ret;
710 }
711
712 static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
713 {
714         int offset = 0;
715         u32 header;
716         u32 pkt_len, pkt_offset, tp_len;
717         int is_cfg_packet;
718         u8 *buff_ptr;
719
720         do {
721                 buff_ptr = buffer + offset;
722                 memcpy(&header, buff_ptr, 4);
723                 le32_to_cpus(&header);
724
725                 is_cfg_packet = (header >> 31) & 0x1;
726                 pkt_offset = (header >> 22) & 0x1ff;
727                 tp_len = (header >> 11) & 0x7ff;
728                 pkt_len = header & 0x7ff;
729
730                 if (pkt_len == 0 || tp_len == 0)
731                         break;
732
733                 if (pkt_offset & IS_MANAGMEMENT) {
734                         pkt_offset &= ~(IS_MANAGMEMENT |
735                                         IS_MANAGMEMENT_CALLBACK |
736                                         IS_MGMT_STATUS_SUCCES);
737                         buff_ptr += HOST_HDR_OFFSET;
738                         wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len);
739                 } else {
740                         if (!is_cfg_packet) {
741                                 if (pkt_len > 0) {
742                                         wilc_frmw_to_linux(wilc, buff_ptr,
743                                                            pkt_len,
744                                                            pkt_offset);
745                                 }
746                         } else {
747                                 struct wilc_cfg_rsp rsp;
748
749                                 buff_ptr += pkt_offset;
750
751                                 wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
752                                                           pkt_len,
753                                                           &rsp);
754                                 if (rsp.type == WILC_CFG_RSP) {
755                                         if (wilc->cfg_seq_no == rsp.seq_no)
756                                                 complete(&wilc->cfg_event);
757                                 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
758                                         wilc_mac_indicate(wilc);
759                                 }
760                         }
761                 }
762                 offset += tp_len;
763                 if (offset >= size)
764                         break;
765         } while (1);
766 }
767
768 static void wilc_wlan_handle_rxq(struct wilc *wilc)
769 {
770         int size;
771         u8 *buffer;
772         struct rxq_entry_t *rqe;
773
774         do {
775                 if (wilc->quit) {
776                         complete(&wilc->cfg_event);
777                         break;
778                 }
779                 rqe = wilc_wlan_rxq_remove(wilc);
780                 if (!rqe)
781                         break;
782
783                 buffer = rqe->buffer;
784                 size = rqe->buffer_size;
785                 wilc_wlan_handle_rx_buff(wilc, buffer, size);
786
787                 kfree(rqe);
788         } while (1);
789 }
790
791 static void wilc_unknown_isr_ext(struct wilc *wilc)
792 {
793         wilc->hif_func->hif_clear_int_ext(wilc, 0);
794 }
795
796 static void wilc_pllupdate_isr_ext(struct wilc *wilc, u32 int_stats)
797 {
798         int trials = 10;
799
800         wilc->hif_func->hif_clear_int_ext(wilc, PLL_INT_CLR);
801
802         if (wilc->io_type == HIF_SDIO)
803                 mdelay(WILC_PLL_TO_SDIO);
804         else
805                 mdelay(WILC_PLL_TO_SPI);
806
807         while (!(is_wilc1000(wilc_get_chipid(wilc, true)) && --trials))
808                 mdelay(1);
809 }
810
811 static void wilc_sleeptimer_isr_ext(struct wilc *wilc, u32 int_stats1)
812 {
813         wilc->hif_func->hif_clear_int_ext(wilc, SLEEP_INT_CLR);
814 }
815
816 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
817 {
818         u32 offset = wilc->rx_buffer_offset;
819         u8 *buffer = NULL;
820         u32 size;
821         u32 retries = 0;
822         int ret = 0;
823         struct rxq_entry_t *rqe;
824
825         size = (int_status & 0x7fff) << 2;
826
827         while (!size && retries < 10) {
828                 wilc->hif_func->hif_read_size(wilc, &size);
829                 size = (size & 0x7fff) << 2;
830                 retries++;
831         }
832
833         if (size <= 0)
834                 return;
835
836         if (LINUX_RX_SIZE - offset < size)
837                 offset = 0;
838
839         buffer = &wilc->rx_buffer[offset];
840
841         wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
842         ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
843         if (!ret)
844                 return;
845
846         offset += size;
847         wilc->rx_buffer_offset = offset;
848         rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
849         if (!rqe)
850                 return;
851
852         rqe->buffer = buffer;
853         rqe->buffer_size = size;
854         wilc_wlan_rxq_add(wilc, rqe);
855         wilc_wlan_handle_rxq(wilc);
856 }
857
858 void wilc_handle_isr(struct wilc *wilc)
859 {
860         u32 int_status;
861
862         acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
863         wilc->hif_func->hif_read_int(wilc, &int_status);
864
865         if (int_status & PLL_INT_EXT)
866                 wilc_pllupdate_isr_ext(wilc, int_status);
867
868         if (int_status & DATA_INT_EXT)
869                 wilc_wlan_handle_isr_ext(wilc, int_status);
870
871         if (int_status & SLEEP_INT_EXT)
872                 wilc_sleeptimer_isr_ext(wilc, int_status);
873
874         if (!(int_status & (ALL_INT_EXT)))
875                 wilc_unknown_isr_ext(wilc);
876
877         release_bus(wilc, RELEASE_ALLOW_SLEEP);
878 }
879 EXPORT_SYMBOL_GPL(wilc_handle_isr);
880
881 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
882                                 u32 buffer_size)
883 {
884         u32 offset;
885         u32 addr, size, size2, blksz;
886         u8 *dma_buffer;
887         int ret = 0;
888
889         blksz = BIT(12);
890
891         dma_buffer = kmalloc(blksz, GFP_KERNEL);
892         if (!dma_buffer)
893                 return -EIO;
894
895         offset = 0;
896         do {
897                 memcpy(&addr, &buffer[offset], 4);
898                 memcpy(&size, &buffer[offset + 4], 4);
899                 le32_to_cpus(&addr);
900                 le32_to_cpus(&size);
901                 acquire_bus(wilc, ACQUIRE_ONLY);
902                 offset += 8;
903                 while (((int)size) && (offset < buffer_size)) {
904                         if (size <= blksz)
905                                 size2 = size;
906                         else
907                                 size2 = blksz;
908
909                         memcpy(dma_buffer, &buffer[offset], size2);
910                         ret = wilc->hif_func->hif_block_tx(wilc, addr,
911                                                            dma_buffer, size2);
912                         if (!ret)
913                                 break;
914
915                         addr += size2;
916                         offset += size2;
917                         size -= size2;
918                 }
919                 release_bus(wilc, RELEASE_ONLY);
920
921                 if (!ret) {
922                         ret = -EIO;
923                         goto fail;
924                 }
925         } while (offset < buffer_size);
926
927 fail:
928
929         kfree(dma_buffer);
930
931         return (ret < 0) ? ret : 0;
932 }
933
934 int wilc_wlan_start(struct wilc *wilc)
935 {
936         u32 reg = 0;
937         int ret;
938         u32 chipid;
939
940         if (wilc->io_type == HIF_SDIO) {
941                 reg = 0;
942                 reg |= BIT(3);
943         } else if (wilc->io_type == HIF_SPI) {
944                 reg = 1;
945         }
946         acquire_bus(wilc, ACQUIRE_ONLY);
947         ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
948         if (!ret) {
949                 release_bus(wilc, RELEASE_ONLY);
950                 return -EIO;
951         }
952         reg = 0;
953         if (wilc->io_type == HIF_SDIO && wilc->dev_irq_num)
954                 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
955
956 #ifdef WILC_DISABLE_PMU
957 #else
958         reg |= WILC_HAVE_USE_PMU;
959 #endif
960
961 #ifdef WILC_SLEEP_CLK_SRC_XO
962         reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
963 #elif defined WILC_SLEEP_CLK_SRC_RTC
964         reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
965 #endif
966
967 #ifdef WILC_EXT_PA_INV_TX_RX
968         reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
969 #endif
970         reg |= WILC_HAVE_USE_IRQ_AS_HOST_WAKE;
971         reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
972 #ifdef XTAL_24
973         reg |= WILC_HAVE_XTAL_24;
974 #endif
975 #ifdef DISABLE_WILC_UART
976         reg |= WILC_HAVE_DISABLE_WILC_UART;
977 #endif
978
979         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
980         if (!ret) {
981                 release_bus(wilc, RELEASE_ONLY);
982                 return -EIO;
983         }
984
985         wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
986
987         ret = wilc->hif_func->hif_read_reg(wilc, 0x1000, &chipid);
988         if (!ret) {
989                 release_bus(wilc, RELEASE_ONLY);
990                 return -EIO;
991         }
992
993         wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
994         if ((reg & BIT(10)) == BIT(10)) {
995                 reg &= ~BIT(10);
996                 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
997                 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
998         }
999
1000         reg |= BIT(10);
1001         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1002         wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1003         release_bus(wilc, RELEASE_ONLY);
1004
1005         return (ret < 0) ? ret : 0;
1006 }
1007
1008 int wilc_wlan_stop(struct wilc *wilc)
1009 {
1010         u32 reg = 0;
1011         int ret;
1012         u8 timeout = 10;
1013
1014         acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1015
1016         ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1017         if (!ret) {
1018                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1019                 return ret;
1020         }
1021
1022         reg &= ~BIT(10);
1023         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1024         if (!ret) {
1025                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1026                 return ret;
1027         }
1028
1029         do {
1030                 ret = wilc->hif_func->hif_read_reg(wilc,
1031                                                    WILC_GLB_RESET_0, &reg);
1032                 if (!ret) {
1033                         release_bus(wilc, RELEASE_ALLOW_SLEEP);
1034                         return ret;
1035                 }
1036
1037                 if ((reg & BIT(10))) {
1038                         reg &= ~BIT(10);
1039                         ret = wilc->hif_func->hif_write_reg(wilc,
1040                                                             WILC_GLB_RESET_0,
1041                                                             reg);
1042                         timeout--;
1043                 } else {
1044                         ret = wilc->hif_func->hif_read_reg(wilc,
1045                                                            WILC_GLB_RESET_0,
1046                                                            &reg);
1047                         if (!ret) {
1048                                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1049                                 return ret;
1050                         }
1051                         break;
1052                 }
1053
1054         } while (timeout);
1055         reg = (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(8) | BIT(9) | BIT(26) |
1056                BIT(29) | BIT(30) | BIT(31));
1057
1058         wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1059         reg = (u32)~BIT(10);
1060
1061         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1062
1063         release_bus(wilc, RELEASE_ALLOW_SLEEP);
1064
1065         return ret;
1066 }
1067
1068 void wilc_wlan_cleanup(struct net_device *dev)
1069 {
1070         struct txq_entry_t *tqe;
1071         struct rxq_entry_t *rqe;
1072         u32 reg = 0;
1073         int ret;
1074         struct wilc_vif *vif = netdev_priv(dev);
1075         struct wilc *wilc = vif->wilc;
1076
1077         wilc->quit = 1;
1078         do {
1079                 tqe = wilc_wlan_txq_remove_from_head(dev);
1080                 if (!tqe)
1081                         break;
1082                 if (tqe->tx_complete_func)
1083                         tqe->tx_complete_func(tqe->priv, 0);
1084                 kfree(tqe);
1085         } while (1);
1086
1087         do {
1088                 rqe = wilc_wlan_rxq_remove(wilc);
1089                 if (!rqe)
1090                         break;
1091                 kfree(rqe);
1092         } while (1);
1093
1094         kfree(wilc->rx_buffer);
1095         wilc->rx_buffer = NULL;
1096         kfree(wilc->tx_buffer);
1097         wilc->tx_buffer = NULL;
1098
1099         acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1100
1101         ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, &reg);
1102         if (!ret)
1103                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1104
1105         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1106                                         (reg | ABORT_INT));
1107         if (!ret)
1108                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1109
1110         release_bus(wilc, RELEASE_ALLOW_SLEEP);
1111         wilc->hif_func->hif_deinit(NULL);
1112 }
1113
1114 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1115                                 u32 drv_handler)
1116 {
1117         struct wilc *wilc = vif->wilc;
1118         struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1119         int total_len = wilc->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1120         int seq_no = wilc->cfg_seq_no % 256;
1121         int driver_handler = (u32)drv_handler;
1122
1123         if (type == WILC_CFG_SET)
1124                 cfg->wid_header[0] = 'W';
1125         else
1126                 cfg->wid_header[0] = 'Q';
1127         cfg->wid_header[1] = seq_no;
1128         cfg->wid_header[2] = (u8)total_len;
1129         cfg->wid_header[3] = (u8)(total_len >> 8);
1130         cfg->wid_header[4] = (u8)driver_handler;
1131         cfg->wid_header[5] = (u8)(driver_handler >> 8);
1132         cfg->wid_header[6] = (u8)(driver_handler >> 16);
1133         cfg->wid_header[7] = (u8)(driver_handler >> 24);
1134         wilc->cfg_seq_no = seq_no;
1135
1136         if (!wilc_wlan_txq_add_cfg_pkt(vif, &cfg->wid_header[0], total_len))
1137                 return -1;
1138
1139         return 0;
1140 }
1141
1142 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1143                       u32 buffer_size, int commit, u32 drv_handler)
1144 {
1145         u32 offset;
1146         int ret_size;
1147         struct wilc *wilc = vif->wilc;
1148
1149         if (wilc->cfg_frame_in_use)
1150                 return 0;
1151
1152         if (start)
1153                 wilc->cfg_frame_offset = 0;
1154
1155         offset = wilc->cfg_frame_offset;
1156         ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1157                                          wid, buffer, buffer_size);
1158         offset += ret_size;
1159         wilc->cfg_frame_offset = offset;
1160
1161         if (!commit)
1162                 return ret_size;
1163
1164         netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
1165         wilc->cfg_frame_in_use = 1;
1166
1167         if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1168                 ret_size = 0;
1169
1170         if (!wait_for_completion_timeout(&wilc->cfg_event,
1171                                          msecs_to_jiffies(CFG_PKTS_TIMEOUT))) {
1172                 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1173                 ret_size = 0;
1174         }
1175
1176         wilc->cfg_frame_in_use = 0;
1177         wilc->cfg_frame_offset = 0;
1178         wilc->cfg_seq_no += 1;
1179
1180         return ret_size;
1181 }
1182
1183 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1184                       u32 drv_handler)
1185 {
1186         u32 offset;
1187         int ret_size;
1188         struct wilc *wilc = vif->wilc;
1189
1190         if (wilc->cfg_frame_in_use)
1191                 return 0;
1192
1193         if (start)
1194                 wilc->cfg_frame_offset = 0;
1195
1196         offset = wilc->cfg_frame_offset;
1197         ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1198         offset += ret_size;
1199         wilc->cfg_frame_offset = offset;
1200
1201         if (!commit)
1202                 return ret_size;
1203
1204         wilc->cfg_frame_in_use = 1;
1205
1206         if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1207                 ret_size = 0;
1208
1209         if (!wait_for_completion_timeout(&wilc->cfg_event,
1210                                          msecs_to_jiffies(CFG_PKTS_TIMEOUT))) {
1211                 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1212                 ret_size = 0;
1213         }
1214         wilc->cfg_frame_in_use = 0;
1215         wilc->cfg_frame_offset = 0;
1216         wilc->cfg_seq_no += 1;
1217
1218         return ret_size;
1219 }
1220
1221 int wilc_wlan_cfg_get_val(u16 wid, u8 *buffer, u32 buffer_size)
1222 {
1223         return wilc_wlan_cfg_get_wid_value(wid, buffer, buffer_size);
1224 }
1225
1226 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1227                          u32 count, u32 drv)
1228 {
1229         int i;
1230         int ret = 0;
1231
1232         if (mode == GET_CFG) {
1233                 for (i = 0; i < count; i++) {
1234                         if (!wilc_wlan_cfg_get(vif, !i,
1235                                                wids[i].id,
1236                                                (i == count - 1),
1237                                                drv)) {
1238                                 ret = -ETIMEDOUT;
1239                                 break;
1240                         }
1241                 }
1242                 for (i = 0; i < count; i++) {
1243                         wids[i].size = wilc_wlan_cfg_get_val(wids[i].id,
1244                                                              wids[i].val,
1245                                                              wids[i].size);
1246                 }
1247         } else if (mode == SET_CFG) {
1248                 for (i = 0; i < count; i++) {
1249                         if (!wilc_wlan_cfg_set(vif, !i,
1250                                                wids[i].id,
1251                                                wids[i].val,
1252                                                wids[i].size,
1253                                                (i == count - 1),
1254                                                drv)) {
1255                                 ret = -ETIMEDOUT;
1256                                 break;
1257                         }
1258                 }
1259         }
1260
1261         return ret;
1262 }
1263
1264 static u32 init_chip(struct net_device *dev)
1265 {
1266         u32 chipid;
1267         u32 reg, ret = 0;
1268         struct wilc_vif *vif = netdev_priv(dev);
1269         struct wilc *wilc = vif->wilc;
1270
1271         acquire_bus(wilc, ACQUIRE_ONLY);
1272
1273         chipid = wilc_get_chipid(wilc, true);
1274
1275         if ((chipid & 0xfff) != 0xa0) {
1276                 ret = wilc->hif_func->hif_read_reg(wilc, 0x1118, &reg);
1277                 if (!ret) {
1278                         netdev_err(dev, "fail read reg 0x1118\n");
1279                         return ret;
1280                 }
1281                 reg |= BIT(0);
1282                 ret = wilc->hif_func->hif_write_reg(wilc, 0x1118, reg);
1283                 if (!ret) {
1284                         netdev_err(dev, "fail write reg 0x1118\n");
1285                         return ret;
1286                 }
1287                 ret = wilc->hif_func->hif_write_reg(wilc, 0xc0000, 0x71);
1288                 if (!ret) {
1289                         netdev_err(dev, "fail write reg 0xc0000\n");
1290                         return ret;
1291                 }
1292         }
1293
1294         release_bus(wilc, RELEASE_ONLY);
1295
1296         return ret;
1297 }
1298
1299 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1300 {
1301         static u32 chipid;
1302         u32 tempchipid = 0;
1303         u32 rfrevid = 0;
1304
1305         if (chipid == 0 || update) {
1306                 wilc->hif_func->hif_read_reg(wilc, 0x1000, &tempchipid);
1307                 wilc->hif_func->hif_read_reg(wilc, 0x13f4, &rfrevid);
1308                 if (!is_wilc1000(tempchipid)) {
1309                         chipid = 0;
1310                         return chipid;
1311                 }
1312                 if (tempchipid == 0x1002a0) {
1313                         if (rfrevid != 0x1)
1314                                 tempchipid = 0x1002a1;
1315                 } else if (tempchipid == 0x1002b0) {
1316                         if (rfrevid == 0x4)
1317                                 tempchipid = 0x1002b1;
1318                         else if (rfrevid != 0x3)
1319                                 tempchipid = 0x1002b2;
1320                 }
1321
1322                 chipid = tempchipid;
1323         }
1324         return chipid;
1325 }
1326
1327 int wilc_wlan_init(struct net_device *dev)
1328 {
1329         int ret = 0;
1330         struct wilc_vif *vif = netdev_priv(dev);
1331         struct wilc *wilc;
1332
1333         wilc = vif->wilc;
1334
1335         wilc->quit = 0;
1336
1337         if (!wilc->hif_func->hif_init(wilc, false)) {
1338                 ret = -EIO;
1339                 goto fail;
1340         }
1341
1342         if (!wilc_wlan_cfg_init()) {
1343                 ret = -ENOBUFS;
1344                 goto fail;
1345         }
1346
1347         if (!wilc->tx_buffer)
1348                 wilc->tx_buffer = kmalloc(LINUX_TX_SIZE, GFP_KERNEL);
1349
1350         if (!wilc->tx_buffer) {
1351                 ret = -ENOBUFS;
1352                 goto fail;
1353         }
1354
1355         if (!wilc->rx_buffer)
1356                 wilc->rx_buffer = kmalloc(LINUX_RX_SIZE, GFP_KERNEL);
1357
1358         if (!wilc->rx_buffer) {
1359                 ret = -ENOBUFS;
1360                 goto fail;
1361         }
1362
1363         if (!init_chip(dev)) {
1364                 ret = -EIO;
1365                 goto fail;
1366         }
1367
1368         return 1;
1369
1370 fail:
1371
1372         kfree(wilc->rx_buffer);
1373         wilc->rx_buffer = NULL;
1374         kfree(wilc->tx_buffer);
1375         wilc->tx_buffer = NULL;
1376
1377         return ret;
1378 }