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