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