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