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