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