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