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