Merge tag 'staging-5.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-microblaze.git] / drivers / staging / wilc1000 / netdev.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/irq.h>
8 #include <linux/kthread.h>
9 #include <linux/firmware.h>
10 #include <linux/netdevice.h>
11 #include <linux/inetdevice.h>
12
13 #include "cfg80211.h"
14 #include "wlan_cfg.h"
15
16 #define WILC_MULTICAST_TABLE_SIZE       8
17
18 static irqreturn_t isr_uh_routine(int irq, void *user_data)
19 {
20         struct net_device *dev = user_data;
21         struct wilc_vif *vif = netdev_priv(dev);
22         struct wilc *wilc = vif->wilc;
23
24         if (wilc->close) {
25                 netdev_err(dev, "Can't handle UH interrupt\n");
26                 return IRQ_HANDLED;
27         }
28         return IRQ_WAKE_THREAD;
29 }
30
31 static irqreturn_t isr_bh_routine(int irq, void *userdata)
32 {
33         struct net_device *dev = userdata;
34         struct wilc_vif *vif = netdev_priv(userdata);
35         struct wilc *wilc = vif->wilc;
36
37         if (wilc->close) {
38                 netdev_err(dev, "Can't handle BH interrupt\n");
39                 return IRQ_HANDLED;
40         }
41
42         wilc_handle_isr(wilc);
43
44         return IRQ_HANDLED;
45 }
46
47 static int init_irq(struct net_device *dev)
48 {
49         int ret = 0;
50         struct wilc_vif *vif = netdev_priv(dev);
51         struct wilc *wl = vif->wilc;
52
53         ret = gpiod_direction_input(wl->gpio_irq);
54         if (ret) {
55                 netdev_err(dev, "could not obtain gpio for WILC_INTR\n");
56                 return ret;
57         }
58
59         wl->dev_irq_num = gpiod_to_irq(wl->gpio_irq);
60
61         ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
62                                    isr_bh_routine,
63                                    IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
64                                    "WILC_IRQ", dev);
65         if (ret < 0)
66                 netdev_err(dev, "Failed to request IRQ\n");
67         else
68                 netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n",
69                            wl->dev_irq_num);
70
71         return ret;
72 }
73
74 static void deinit_irq(struct net_device *dev)
75 {
76         struct wilc_vif *vif = netdev_priv(dev);
77         struct wilc *wilc = vif->wilc;
78
79         /* Deinitialize IRQ */
80         if (wilc->dev_irq_num)
81                 free_irq(wilc->dev_irq_num, wilc);
82 }
83
84 void wilc_mac_indicate(struct wilc *wilc)
85 {
86         s8 status;
87
88         wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
89         if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
90                 wilc->mac_status = status;
91                 complete(&wilc->sync_event);
92         } else {
93                 wilc->mac_status = status;
94         }
95 }
96
97 static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
98 {
99         struct net_device *ndev = NULL;
100         struct wilc_vif *vif;
101         struct ieee80211_hdr *h = (struct ieee80211_hdr *)mac_header;
102
103         list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
104                 if (vif->mode == WILC_STATION_MODE)
105                         if (ether_addr_equal_unaligned(h->addr2, vif->bssid)) {
106                                 ndev = vif->ndev;
107                                 goto out;
108                         }
109                 if (vif->mode == WILC_AP_MODE)
110                         if (ether_addr_equal_unaligned(h->addr1, vif->bssid)) {
111                                 ndev = vif->ndev;
112                                 goto out;
113                         }
114         }
115 out:
116         return ndev;
117 }
118
119 void wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
120 {
121         struct wilc_vif *vif = netdev_priv(wilc_netdev);
122
123         if (bssid)
124                 ether_addr_copy(vif->bssid, bssid);
125         else
126                 eth_zero_addr(vif->bssid);
127
128         vif->mode = mode;
129 }
130
131 int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
132 {
133         int srcu_idx;
134         u8 ret_val = 0;
135         struct wilc_vif *vif;
136
137         srcu_idx = srcu_read_lock(&wilc->srcu);
138         list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
139                 if (!is_zero_ether_addr(vif->bssid))
140                         ret_val++;
141         }
142         srcu_read_unlock(&wilc->srcu, srcu_idx);
143         return ret_val;
144 }
145
146 static int wilc_txq_task(void *vp)
147 {
148         int ret;
149         u32 txq_count;
150         struct wilc *wl = vp;
151
152         complete(&wl->txq_thread_started);
153         while (1) {
154                 wait_for_completion(&wl->txq_event);
155
156                 if (wl->close) {
157                         complete(&wl->txq_thread_started);
158
159                         while (!kthread_should_stop())
160                                 schedule();
161                         break;
162                 }
163                 do {
164                         ret = wilc_wlan_handle_txq(wl, &txq_count);
165                         if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
166                                 int srcu_idx;
167                                 struct wilc_vif *ifc;
168
169                                 srcu_idx = srcu_read_lock(&wl->srcu);
170                                 list_for_each_entry_rcu(ifc, &wl->vif_list,
171                                                         list) {
172                                         if (ifc->mac_opened && ifc->ndev)
173                                                 netif_wake_queue(ifc->ndev);
174                                 }
175                                 srcu_read_unlock(&wl->srcu, srcu_idx);
176                         }
177                 } while (ret == WILC_VMM_ENTRY_FULL_RETRY && !wl->close);
178         }
179         return 0;
180 }
181
182 static int wilc_wlan_get_firmware(struct net_device *dev)
183 {
184         struct wilc_vif *vif = netdev_priv(dev);
185         struct wilc *wilc = vif->wilc;
186         int chip_id;
187         const struct firmware *wilc_firmware;
188         char *firmware;
189
190         chip_id = wilc_get_chipid(wilc, false);
191
192         if (chip_id < 0x1003a0)
193                 firmware = FIRMWARE_1002;
194         else
195                 firmware = FIRMWARE_1003;
196
197         netdev_info(dev, "loading firmware %s\n", firmware);
198
199         if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
200                 netdev_err(dev, "%s - firmware not available\n", firmware);
201                 return -EINVAL;
202         }
203         wilc->firmware = wilc_firmware;
204
205         return 0;
206 }
207
208 static int wilc_start_firmware(struct net_device *dev)
209 {
210         struct wilc_vif *vif = netdev_priv(dev);
211         struct wilc *wilc = vif->wilc;
212         int ret = 0;
213
214         ret = wilc_wlan_start(wilc);
215         if (ret)
216                 return ret;
217
218         if (!wait_for_completion_timeout(&wilc->sync_event,
219                                          msecs_to_jiffies(5000)))
220                 return -ETIME;
221
222         return 0;
223 }
224
225 static int wilc1000_firmware_download(struct net_device *dev)
226 {
227         struct wilc_vif *vif = netdev_priv(dev);
228         struct wilc *wilc = vif->wilc;
229         int ret = 0;
230
231         if (!wilc->firmware) {
232                 netdev_err(dev, "Firmware buffer is NULL\n");
233                 return -ENOBUFS;
234         }
235
236         ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
237                                           wilc->firmware->size);
238         if (ret)
239                 return ret;
240
241         release_firmware(wilc->firmware);
242         wilc->firmware = NULL;
243
244         netdev_dbg(dev, "Download Succeeded\n");
245
246         return 0;
247 }
248
249 static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
250 {
251         struct wilc_priv *priv = &vif->priv;
252         struct host_if_drv *hif_drv;
253         u8 b;
254         u16 hw;
255         u32 w;
256
257         netdev_dbg(dev, "Start configuring Firmware\n");
258         hif_drv = (struct host_if_drv *)priv->hif_drv;
259         netdev_dbg(dev, "Host = %p\n", hif_drv);
260
261         w = vif->iftype;
262         cpu_to_le32s(&w);
263         if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
264                                0, 0))
265                 goto fail;
266
267         b = WILC_FW_BSS_TYPE_INFRA;
268         if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
269                 goto fail;
270
271         b = WILC_FW_TX_RATE_AUTO;
272         if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
273                 goto fail;
274
275         b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
276         if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
277                 goto fail;
278
279         b = WILC_FW_PREAMBLE_SHORT;
280         if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
281                 goto fail;
282
283         b = WILC_FW_11N_PROT_AUTO;
284         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
285                 goto fail;
286
287         b = WILC_FW_ACTIVE_SCAN;
288         if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
289                 goto fail;
290
291         b = WILC_FW_SITE_SURVEY_OFF;
292         if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
293                 goto fail;
294
295         hw = 0xffff;
296         cpu_to_le16s(&hw);
297         if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
298                 goto fail;
299
300         hw = 2346;
301         cpu_to_le16s(&hw);
302         if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
303                 goto fail;
304
305         b = 0;
306         if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
307                 goto fail;
308
309         b = 1;
310         if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
311                 goto fail;
312
313         b = WILC_FW_NO_POWERSAVE;
314         if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
315                 goto fail;
316
317         b = WILC_FW_SEC_NO;
318         if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
319                 goto fail;
320
321         b = WILC_FW_AUTH_OPEN_SYSTEM;
322         if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
323                 goto fail;
324
325         b = 3;
326         if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
327                 goto fail;
328
329         b = 3;
330         if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
331                 goto fail;
332
333         b = WILC_FW_ACK_POLICY_NORMAL;
334         if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
335                 goto fail;
336
337         b = 0;
338         if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
339                                0, 0))
340                 goto fail;
341
342         b = 48;
343         if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
344                 goto fail;
345
346         b = 28;
347         if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
348                 goto fail;
349
350         hw = 100;
351         cpu_to_le16s(&hw);
352         if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
353                 goto fail;
354
355         b = WILC_FW_REKEY_POLICY_DISABLE;
356         if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
357                 goto fail;
358
359         w = 84600;
360         cpu_to_le32s(&w);
361         if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
362                 goto fail;
363
364         w = 500;
365         cpu_to_le32s(&w);
366         if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
367                                0))
368                 goto fail;
369
370         b = 1;
371         if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
372                                0))
373                 goto fail;
374
375         b = WILC_FW_ERP_PROT_SELF_CTS;
376         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
377                 goto fail;
378
379         b = 1;
380         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
381                 goto fail;
382
383         b = WILC_FW_11N_OP_MODE_HT_MIXED;
384         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
385                 goto fail;
386
387         b = 1;
388         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
389                 goto fail;
390
391         b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
392         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
393                                0, 0))
394                 goto fail;
395
396         b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
397         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
398                 goto fail;
399
400         b = 0;
401         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
402                                0))
403                 goto fail;
404
405         b = 7;
406         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
407                 goto fail;
408
409         b = 1;
410         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
411                                1, 1))
412                 goto fail;
413
414         return 0;
415
416 fail:
417         return -EINVAL;
418 }
419
420 static void wlan_deinitialize_threads(struct net_device *dev)
421 {
422         struct wilc_vif *vif = netdev_priv(dev);
423         struct wilc *wl = vif->wilc;
424
425         wl->close = 1;
426
427         complete(&wl->txq_event);
428
429         if (wl->txq_thread) {
430                 kthread_stop(wl->txq_thread);
431                 wl->txq_thread = NULL;
432         }
433 }
434
435 static void wilc_wlan_deinitialize(struct net_device *dev)
436 {
437         struct wilc_vif *vif = netdev_priv(dev);
438         struct wilc *wl = vif->wilc;
439
440         if (!wl) {
441                 netdev_err(dev, "wl is NULL\n");
442                 return;
443         }
444
445         if (wl->initialized) {
446                 netdev_info(dev, "Deinitializing wilc1000...\n");
447
448                 if (!wl->dev_irq_num &&
449                     wl->hif_func->disable_interrupt) {
450                         mutex_lock(&wl->hif_cs);
451                         wl->hif_func->disable_interrupt(wl);
452                         mutex_unlock(&wl->hif_cs);
453                 }
454                 complete(&wl->txq_event);
455
456                 wlan_deinitialize_threads(dev);
457                 deinit_irq(dev);
458
459                 wilc_wlan_stop(wl, vif);
460                 wilc_wlan_cleanup(dev);
461
462                 wl->initialized = false;
463
464                 netdev_dbg(dev, "wilc1000 deinitialization Done\n");
465         } else {
466                 netdev_dbg(dev, "wilc1000 is not initialized\n");
467         }
468 }
469
470 static int wlan_initialize_threads(struct net_device *dev)
471 {
472         struct wilc_vif *vif = netdev_priv(dev);
473         struct wilc *wilc = vif->wilc;
474
475         wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
476                                        "K_TXQ_TASK");
477         if (IS_ERR(wilc->txq_thread)) {
478                 netdev_err(dev, "couldn't create TXQ thread\n");
479                 wilc->close = 0;
480                 return PTR_ERR(wilc->txq_thread);
481         }
482         wait_for_completion(&wilc->txq_thread_started);
483
484         return 0;
485 }
486
487 static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
488 {
489         int ret = 0;
490         struct wilc *wl = vif->wilc;
491
492         if (!wl->initialized) {
493                 wl->mac_status = WILC_MAC_STATUS_INIT;
494                 wl->close = 0;
495
496                 ret = wilc_wlan_init(dev);
497                 if (ret)
498                         return ret;
499
500                 ret = wlan_initialize_threads(dev);
501                 if (ret)
502                         goto fail_wilc_wlan;
503
504                 if (wl->gpio_irq && init_irq(dev)) {
505                         ret = -EIO;
506                         goto fail_threads;
507                 }
508
509                 if (!wl->dev_irq_num &&
510                     wl->hif_func->enable_interrupt &&
511                     wl->hif_func->enable_interrupt(wl)) {
512                         ret = -EIO;
513                         goto fail_irq_init;
514                 }
515
516                 ret = wilc_wlan_get_firmware(dev);
517                 if (ret)
518                         goto fail_irq_enable;
519
520                 ret = wilc1000_firmware_download(dev);
521                 if (ret)
522                         goto fail_irq_enable;
523
524                 ret = wilc_start_firmware(dev);
525                 if (ret)
526                         goto fail_irq_enable;
527
528                 if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
529                         int size;
530                         char firmware_ver[20];
531
532                         size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
533                                                      firmware_ver,
534                                                      sizeof(firmware_ver));
535                         firmware_ver[size] = '\0';
536                         netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
537                 }
538
539                 ret = wilc_init_fw_config(dev, vif);
540                 if (ret) {
541                         netdev_err(dev, "Failed to configure firmware\n");
542                         goto fail_fw_start;
543                 }
544                 wl->initialized = true;
545                 return 0;
546
547 fail_fw_start:
548                 wilc_wlan_stop(wl, vif);
549
550 fail_irq_enable:
551                 if (!wl->dev_irq_num &&
552                     wl->hif_func->disable_interrupt)
553                         wl->hif_func->disable_interrupt(wl);
554 fail_irq_init:
555                 if (wl->dev_irq_num)
556                         deinit_irq(dev);
557 fail_threads:
558                 wlan_deinitialize_threads(dev);
559 fail_wilc_wlan:
560                 wilc_wlan_cleanup(dev);
561                 netdev_err(dev, "WLAN initialization FAILED\n");
562         } else {
563                 netdev_dbg(dev, "wilc1000 already initialized\n");
564         }
565         return ret;
566 }
567
568 static int mac_init_fn(struct net_device *ndev)
569 {
570         netif_start_queue(ndev);
571         netif_stop_queue(ndev);
572
573         return 0;
574 }
575
576 static int wilc_mac_open(struct net_device *ndev)
577 {
578         struct wilc_vif *vif = netdev_priv(ndev);
579         struct wilc *wl = vif->wilc;
580         struct wilc_priv *priv = wdev_priv(vif->ndev->ieee80211_ptr);
581         unsigned char mac_add[ETH_ALEN] = {0};
582         int ret = 0;
583
584         if (!wl || !wl->dev) {
585                 netdev_err(ndev, "device not ready\n");
586                 return -ENODEV;
587         }
588
589         netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
590
591         ret = wilc_init_host_int(ndev);
592         if (ret)
593                 return ret;
594
595         ret = wilc_wlan_initialize(ndev, vif);
596         if (ret) {
597                 wilc_deinit_host_int(ndev);
598                 return ret;
599         }
600
601         wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
602                                 vif->idx);
603         wilc_get_mac_address(vif, mac_add);
604         netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
605         ether_addr_copy(ndev->dev_addr, mac_add);
606
607         if (!is_valid_ether_addr(ndev->dev_addr)) {
608                 netdev_err(ndev, "Wrong MAC address\n");
609                 wilc_deinit_host_int(ndev);
610                 wilc_wlan_deinitialize(ndev);
611                 return -EINVAL;
612         }
613
614         wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
615                                  vif->ndev->ieee80211_ptr,
616                                  vif->frame_reg[0].type,
617                                  vif->frame_reg[0].reg);
618         wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
619                                  vif->ndev->ieee80211_ptr,
620                                  vif->frame_reg[1].type,
621                                  vif->frame_reg[1].reg);
622         netif_wake_queue(ndev);
623         wl->open_ifcs++;
624         priv->p2p.local_random = 0x01;
625         vif->mac_opened = 1;
626         return 0;
627 }
628
629 static struct net_device_stats *mac_stats(struct net_device *dev)
630 {
631         struct wilc_vif *vif = netdev_priv(dev);
632
633         return &vif->netstats;
634 }
635
636 static void wilc_set_multicast_list(struct net_device *dev)
637 {
638         struct netdev_hw_addr *ha;
639         struct wilc_vif *vif = netdev_priv(dev);
640         int i;
641         u8 *mc_list;
642         u8 *cur_mc;
643
644         if (dev->flags & IFF_PROMISC)
645                 return;
646
647         if (dev->flags & IFF_ALLMULTI ||
648             dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
649                 wilc_setup_multicast_filter(vif, 0, 0, NULL);
650                 return;
651         }
652
653         if (dev->mc.count == 0) {
654                 wilc_setup_multicast_filter(vif, 1, 0, NULL);
655                 return;
656         }
657
658         mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
659         if (!mc_list)
660                 return;
661
662         cur_mc = mc_list;
663         i = 0;
664         netdev_for_each_mc_addr(ha, dev) {
665                 memcpy(cur_mc, ha->addr, ETH_ALEN);
666                 netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
667                 i++;
668                 cur_mc += ETH_ALEN;
669         }
670
671         if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
672                 kfree(mc_list);
673 }
674
675 static void wilc_tx_complete(void *priv, int status)
676 {
677         struct tx_complete_data *pv_data = priv;
678
679         dev_kfree_skb(pv_data->skb);
680         kfree(pv_data);
681 }
682
683 netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
684 {
685         struct wilc_vif *vif = netdev_priv(ndev);
686         struct wilc *wilc = vif->wilc;
687         struct tx_complete_data *tx_data = NULL;
688         int queue_count;
689
690         if (skb->dev != ndev) {
691                 netdev_err(ndev, "Packet not destined to this device\n");
692                 return 0;
693         }
694
695         tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
696         if (!tx_data) {
697                 dev_kfree_skb(skb);
698                 netif_wake_queue(ndev);
699                 return 0;
700         }
701
702         tx_data->buff = skb->data;
703         tx_data->size = skb->len;
704         tx_data->skb  = skb;
705
706         vif->netstats.tx_packets++;
707         vif->netstats.tx_bytes += tx_data->size;
708         queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
709                                                 tx_data->buff, tx_data->size,
710                                                 wilc_tx_complete);
711
712         if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
713                 int srcu_idx;
714                 struct wilc_vif *vif;
715
716                 srcu_idx = srcu_read_lock(&wilc->srcu);
717                 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
718                         if (vif->mac_opened)
719                                 netif_stop_queue(vif->ndev);
720                 }
721                 srcu_read_unlock(&wilc->srcu, srcu_idx);
722         }
723
724         return 0;
725 }
726
727 static int wilc_mac_close(struct net_device *ndev)
728 {
729         struct wilc_vif *vif = netdev_priv(ndev);
730         struct wilc *wl = vif->wilc;
731
732         netdev_dbg(ndev, "Mac close\n");
733
734         if (wl->open_ifcs > 0)
735                 wl->open_ifcs--;
736         else
737                 return 0;
738
739         if (vif->ndev) {
740                 netif_stop_queue(vif->ndev);
741
742                 wilc_deinit_host_int(vif->ndev);
743         }
744
745         if (wl->open_ifcs == 0) {
746                 netdev_dbg(ndev, "Deinitializing wilc1000\n");
747                 wl->close = 1;
748                 wilc_wlan_deinitialize(ndev);
749         }
750
751         vif->mac_opened = 0;
752
753         return 0;
754 }
755
756 void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
757                        u32 pkt_offset)
758 {
759         unsigned int frame_len = 0;
760         int stats;
761         unsigned char *buff_to_send = NULL;
762         struct sk_buff *skb;
763         struct net_device *wilc_netdev;
764         struct wilc_vif *vif;
765
766         if (!wilc)
767                 return;
768
769         wilc_netdev = get_if_handler(wilc, buff);
770         if (!wilc_netdev)
771                 return;
772
773         buff += pkt_offset;
774         vif = netdev_priv(wilc_netdev);
775
776         if (size > 0) {
777                 frame_len = size;
778                 buff_to_send = buff;
779
780                 skb = dev_alloc_skb(frame_len);
781                 if (!skb)
782                         return;
783
784                 skb->dev = wilc_netdev;
785
786                 skb_put_data(skb, buff_to_send, frame_len);
787
788                 skb->protocol = eth_type_trans(skb, wilc_netdev);
789                 vif->netstats.rx_packets++;
790                 vif->netstats.rx_bytes += frame_len;
791                 skb->ip_summed = CHECKSUM_UNNECESSARY;
792                 stats = netif_rx(skb);
793                 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
794         }
795 }
796
797 void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
798 {
799         int srcu_idx;
800         struct wilc_vif *vif;
801
802         srcu_idx = srcu_read_lock(&wilc->srcu);
803         list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
804                 u16 type = le16_to_cpup((__le16 *)buff);
805
806                 if (vif->priv.p2p_listen_state &&
807                     ((type == vif->frame_reg[0].type && vif->frame_reg[0].reg) ||
808                      (type == vif->frame_reg[1].type && vif->frame_reg[1].reg)))
809                         wilc_wfi_p2p_rx(vif, buff, size);
810
811                 if (vif->monitor_flag)
812                         wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
813         }
814         srcu_read_unlock(&wilc->srcu, srcu_idx);
815 }
816
817 static const struct net_device_ops wilc_netdev_ops = {
818         .ndo_init = mac_init_fn,
819         .ndo_open = wilc_mac_open,
820         .ndo_stop = wilc_mac_close,
821         .ndo_start_xmit = wilc_mac_xmit,
822         .ndo_get_stats = mac_stats,
823         .ndo_set_rx_mode  = wilc_set_multicast_list,
824 };
825
826 void wilc_netdev_cleanup(struct wilc *wilc)
827 {
828         struct wilc_vif *vif;
829         int srcu_idx, ifc_cnt = 0;
830
831         if (!wilc)
832                 return;
833
834         if (wilc->firmware) {
835                 release_firmware(wilc->firmware);
836                 wilc->firmware = NULL;
837         }
838
839         srcu_idx = srcu_read_lock(&wilc->srcu);
840         list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
841                 if (vif->ndev)
842                         unregister_netdev(vif->ndev);
843         }
844         srcu_read_unlock(&wilc->srcu, srcu_idx);
845
846         wilc_wfi_deinit_mon_interface(wilc, false);
847         flush_workqueue(wilc->hif_workqueue);
848         destroy_workqueue(wilc->hif_workqueue);
849
850         while (ifc_cnt < WILC_NUM_CONCURRENT_IFC) {
851                 mutex_lock(&wilc->vif_mutex);
852                 if (wilc->vif_num <= 0) {
853                         mutex_unlock(&wilc->vif_mutex);
854                         break;
855                 }
856                 vif = wilc_get_wl_to_vif(wilc);
857                 if (!IS_ERR(vif))
858                         list_del_rcu(&vif->list);
859
860                 wilc->vif_num--;
861                 mutex_unlock(&wilc->vif_mutex);
862                 synchronize_srcu(&wilc->srcu);
863                 ifc_cnt++;
864         }
865
866         wilc_wlan_cfg_deinit(wilc);
867         wlan_deinit_locks(wilc);
868         kfree(wilc->bus_data);
869         wiphy_unregister(wilc->wiphy);
870         wiphy_free(wilc->wiphy);
871 }
872 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
873
874 static u8 wilc_get_available_idx(struct wilc *wl)
875 {
876         int idx = 0;
877         struct wilc_vif *vif;
878         int srcu_idx;
879
880         srcu_idx = srcu_read_lock(&wl->srcu);
881         list_for_each_entry_rcu(vif, &wl->vif_list, list) {
882                 if (vif->idx == 0)
883                         idx = 1;
884                 else
885                         idx = 0;
886         }
887         srcu_read_unlock(&wl->srcu, srcu_idx);
888         return idx;
889 }
890
891 struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
892                                       int vif_type, enum nl80211_iftype type,
893                                       bool rtnl_locked)
894 {
895         struct net_device *ndev;
896         struct wilc_vif *vif;
897         int ret;
898
899         ndev = alloc_etherdev(sizeof(*vif));
900         if (!ndev)
901                 return ERR_PTR(-ENOMEM);
902
903         vif = netdev_priv(ndev);
904         ndev->ieee80211_ptr = &vif->priv.wdev;
905         strcpy(ndev->name, name);
906         vif->wilc = wl;
907         vif->ndev = ndev;
908         ndev->ml_priv = vif;
909
910         ndev->netdev_ops = &wilc_netdev_ops;
911
912         SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
913
914         vif->priv.wdev.wiphy = wl->wiphy;
915         vif->priv.wdev.netdev = ndev;
916         vif->priv.wdev.iftype = type;
917         vif->priv.dev = ndev;
918
919         if (rtnl_locked)
920                 ret = register_netdevice(ndev);
921         else
922                 ret = register_netdev(ndev);
923
924         if (ret) {
925                 free_netdev(ndev);
926                 return ERR_PTR(-EFAULT);
927         }
928
929         ndev->needs_free_netdev = true;
930         vif->iftype = vif_type;
931         vif->idx = wilc_get_available_idx(wl);
932         vif->mac_opened = 0;
933         mutex_lock(&wl->vif_mutex);
934         list_add_tail_rcu(&vif->list, &wl->vif_list);
935         wl->vif_num += 1;
936         mutex_unlock(&wl->vif_mutex);
937         synchronize_srcu(&wl->srcu);
938
939         return vif;
940 }
941
942 MODULE_LICENSE("GPL");