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 592 if (!wl || !wl->dev) { 593 netdev_err(ndev, "device not ready\n"); 594 return -ENODEV; 595 } 596 597 netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev); 598 599 ret = wilc_init_host_int(ndev); 600 if (ret) 601 return ret; 602 603 ret = wilc_wlan_initialize(ndev, vif); 604 if (ret) { 605 wilc_deinit_host_int(ndev); 606 return ret; 607 } 608 609 netdev_dbg(ndev, "Mac address: %pM\n", ndev->dev_addr); 610 ret = wilc_set_mac_address(vif, ndev->dev_addr); 611 if (ret) { 612 netdev_err(ndev, "Failed to enforce MAC address in chip"); 613 wilc_deinit_host_int(ndev); 614 if (!wl->open_ifcs) 615 wilc_wlan_deinitialize(ndev); 616 return ret; 617 } 618 619 wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype, 620 vif->idx); 621 622 mgmt_regs.interface_stypes = vif->mgmt_reg_stypes; 623 /* so we detect a change */ 624 vif->mgmt_reg_stypes = 0; 625 wilc_update_mgmt_frame_registrations(vif->ndev->ieee80211_ptr->wiphy, 626 vif->ndev->ieee80211_ptr, 627 &mgmt_regs); 628 netif_wake_queue(ndev); 629 wl->open_ifcs++; 630 vif->mac_opened = 1; 631 return 0; 632 } 633 634 static struct net_device_stats *mac_stats(struct net_device *dev) 635 { 636 struct wilc_vif *vif = netdev_priv(dev); 637 638 return &vif->netstats; 639 } 640 641 static int wilc_set_mac_addr(struct net_device *dev, void *p) 642 { 643 int result; 644 struct wilc_vif *vif = netdev_priv(dev); 645 struct wilc *wilc = vif->wilc; 646 struct sockaddr *addr = (struct sockaddr *)p; 647 unsigned char mac_addr[ETH_ALEN]; 648 struct wilc_vif *tmp_vif; 649 650 if (!is_valid_ether_addr(addr->sa_data)) 651 return -EADDRNOTAVAIL; 652 653 if (!vif->mac_opened) { 654 eth_commit_mac_addr_change(dev, p); 655 return 0; 656 } 657 658 /* Verify MAC Address is not already in use: */ 659 660 rcu_read_lock(); 661 wilc_for_each_vif(wilc, tmp_vif) { 662 wilc_get_mac_address(tmp_vif, mac_addr); 663 if (ether_addr_equal(addr->sa_data, mac_addr)) { 664 if (vif != tmp_vif) { 665 rcu_read_unlock(); 666 return -EADDRNOTAVAIL; 667 } 668 rcu_read_unlock(); 669 return 0; 670 } 671 } 672 rcu_read_unlock(); 673 674 result = wilc_set_mac_address(vif, addr->sa_data); 675 if (result) 676 return result; 677 678 eth_commit_mac_addr_change(dev, p); 679 return result; 680 } 681 682 static void wilc_set_multicast_list(struct net_device *dev) 683 { 684 struct netdev_hw_addr *ha; 685 struct wilc_vif *vif = netdev_priv(dev); 686 int i; 687 u8 *mc_list; 688 u8 *cur_mc; 689 690 if (dev->flags & IFF_PROMISC) 691 return; 692 693 if (dev->flags & IFF_ALLMULTI || 694 dev->mc.count > WILC_MULTICAST_TABLE_SIZE) { 695 wilc_setup_multicast_filter(vif, 0, 0, NULL); 696 return; 697 } 698 699 if (dev->mc.count == 0) { 700 wilc_setup_multicast_filter(vif, 1, 0, NULL); 701 return; 702 } 703 704 mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC); 705 if (!mc_list) 706 return; 707 708 cur_mc = mc_list; 709 i = 0; 710 netdev_for_each_mc_addr(ha, dev) { 711 memcpy(cur_mc, ha->addr, ETH_ALEN); 712 netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc); 713 i++; 714 cur_mc += ETH_ALEN; 715 } 716 717 if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list)) 718 kfree(mc_list); 719 } 720 721 static void wilc_tx_complete(void *priv, int status) 722 { 723 struct tx_complete_data *pv_data = priv; 724 725 dev_kfree_skb(pv_data->skb); 726 kfree(pv_data); 727 } 728 729 netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev) 730 { 731 struct wilc_vif *vif = netdev_priv(ndev); 732 struct wilc *wilc = vif->wilc; 733 struct tx_complete_data *tx_data = NULL; 734 int queue_count; 735 736 if (skb->dev != ndev) { 737 netdev_err(ndev, "Packet not destined to this device\n"); 738 dev_kfree_skb(skb); 739 return NETDEV_TX_OK; 740 } 741 742 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC); 743 if (!tx_data) { 744 dev_kfree_skb(skb); 745 netif_wake_queue(ndev); 746 return NETDEV_TX_OK; 747 } 748 749 tx_data->buff = skb->data; 750 tx_data->size = skb->len; 751 tx_data->skb = skb; 752 753 vif->netstats.tx_packets++; 754 vif->netstats.tx_bytes += tx_data->size; 755 queue_count = wilc_wlan_txq_add_net_pkt(ndev, tx_data, 756 tx_data->buff, tx_data->size, 757 wilc_tx_complete); 758 759 if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) { 760 struct wilc_vif *vif; 761 762 rcu_read_lock(); 763 wilc_for_each_vif(wilc, vif) { 764 if (vif->mac_opened) 765 netif_stop_queue(vif->ndev); 766 } 767 rcu_read_unlock(); 768 } 769 770 return NETDEV_TX_OK; 771 } 772 773 static int wilc_mac_close(struct net_device *ndev) 774 { 775 struct wilc_vif *vif = netdev_priv(ndev); 776 struct wilc *wl = vif->wilc; 777 778 netdev_dbg(ndev, "Mac close\n"); 779 780 if (wl->open_ifcs > 0) 781 wl->open_ifcs--; 782 else 783 return 0; 784 785 if (vif->ndev) { 786 netif_stop_queue(vif->ndev); 787 788 wilc_handle_disconnect(vif); 789 wilc_deinit_host_int(vif->ndev); 790 } 791 792 if (wl->open_ifcs == 0) { 793 netdev_dbg(ndev, "Deinitializing wilc1000\n"); 794 wl->close = 1; 795 wilc_wlan_deinitialize(ndev); 796 } 797 798 vif->mac_opened = 0; 799 800 return 0; 801 } 802 803 void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size, 804 u32 pkt_offset) 805 { 806 unsigned char *buff_to_send = NULL; 807 struct net_device *wilc_netdev; 808 unsigned int frame_len = 0; 809 struct wilc_vif *vif; 810 struct sk_buff *skb; 811 int stats; 812 813 if (!wilc) 814 return; 815 816 rcu_read_lock(); 817 wilc_netdev = get_if_handler(wilc, buff); 818 if (!wilc_netdev) 819 goto out; 820 821 buff += pkt_offset; 822 vif = netdev_priv(wilc_netdev); 823 824 if (size > 0) { 825 frame_len = size; 826 buff_to_send = buff; 827 828 skb = dev_alloc_skb(frame_len); 829 if (!skb) 830 goto out; 831 832 skb->dev = wilc_netdev; 833 834 skb_put_data(skb, buff_to_send, frame_len); 835 836 skb->protocol = eth_type_trans(skb, wilc_netdev); 837 vif->netstats.rx_packets++; 838 vif->netstats.rx_bytes += frame_len; 839 skb->ip_summed = CHECKSUM_UNNECESSARY; 840 stats = netif_rx(skb); 841 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats); 842 } 843 out: 844 rcu_read_unlock(); 845 } 846 847 void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size, bool is_auth) 848 { 849 struct wilc_vif *vif; 850 851 rcu_read_lock(); 852 wilc_for_each_vif(wilc, vif) { 853 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buff; 854 u16 type = le16_to_cpup((__le16 *)buff); 855 u32 type_bit = BIT(type >> 4); 856 u32 auth_bit = BIT(IEEE80211_STYPE_AUTH >> 4); 857 858 if ((vif->mgmt_reg_stypes & auth_bit && 859 ieee80211_is_auth(mgmt->frame_control)) && 860 vif->iftype == WILC_STATION_MODE && is_auth) { 861 wilc_wfi_mgmt_frame_rx(vif, buff, size); 862 break; 863 } 864 865 if (vif->priv.p2p_listen_state && 866 vif->mgmt_reg_stypes & type_bit) 867 wilc_wfi_p2p_rx(vif, buff, size); 868 869 if (vif->monitor_flag) 870 wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size); 871 } 872 rcu_read_unlock(); 873 } 874 875 static const struct net_device_ops wilc_netdev_ops = { 876 .ndo_init = mac_init_fn, 877 .ndo_open = wilc_mac_open, 878 .ndo_stop = wilc_mac_close, 879 .ndo_set_mac_address = wilc_set_mac_addr, 880 .ndo_start_xmit = wilc_mac_xmit, 881 .ndo_get_stats = mac_stats, 882 .ndo_set_rx_mode = wilc_set_multicast_list, 883 }; 884 885 void wilc_netdev_cleanup(struct wilc *wilc) 886 { 887 struct wilc_vif *vif, *vif_tmp; 888 889 if (!wilc) 890 return; 891 892 if (wilc->firmware) { 893 release_firmware(wilc->firmware); 894 wilc->firmware = NULL; 895 } 896 897 list_for_each_entry_safe(vif, vif_tmp, &wilc->vif_list, list) { 898 mutex_lock(&wilc->vif_mutex); 899 list_del_rcu(&vif->list); 900 wilc->vif_num--; 901 mutex_unlock(&wilc->vif_mutex); 902 synchronize_rcu(); 903 if (vif->ndev) 904 unregister_netdev(vif->ndev); 905 } 906 907 wilc_wfi_deinit_mon_interface(wilc, false); 908 destroy_workqueue(wilc->hif_workqueue); 909 910 wilc_wlan_cfg_deinit(wilc); 911 wlan_deinit_locks(wilc); 912 wiphy_unregister(wilc->wiphy); 913 wiphy_free(wilc->wiphy); 914 } 915 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup); 916 917 static u8 wilc_get_available_idx(struct wilc *wl) 918 { 919 int idx = 0; 920 struct wilc_vif *vif; 921 922 rcu_read_lock(); 923 wilc_for_each_vif(wl, vif) { 924 if (vif->idx == 0) 925 idx = 1; 926 else 927 idx = 0; 928 } 929 rcu_read_unlock(); 930 return idx; 931 } 932 933 struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name, 934 int vif_type, enum nl80211_iftype type, 935 bool rtnl_locked) 936 { 937 u8 mac_address[ETH_ALEN]; 938 struct net_device *ndev; 939 struct wilc_vif *vif; 940 int ret; 941 942 ndev = alloc_etherdev(sizeof(*vif)); 943 if (!ndev) 944 return ERR_PTR(-ENOMEM); 945 946 vif = netdev_priv(ndev); 947 ndev->ieee80211_ptr = &vif->priv.wdev; 948 strcpy(ndev->name, name); 949 vif->wilc = wl; 950 vif->ndev = ndev; 951 ndev->ml_priv = vif; 952 953 ndev->netdev_ops = &wilc_netdev_ops; 954 955 SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy)); 956 957 vif->priv.wdev.wiphy = wl->wiphy; 958 vif->priv.wdev.netdev = ndev; 959 vif->priv.wdev.iftype = type; 960 vif->priv.dev = ndev; 961 962 ndev->needs_free_netdev = true; 963 vif->iftype = vif_type; 964 vif->idx = wilc_get_available_idx(wl); 965 vif->mac_opened = 0; 966 967 memcpy(mac_address, wl->nv_mac_address, ETH_ALEN); 968 /* WILC firmware uses locally administered MAC address for the 969 * second virtual interface (bit 1 of first byte set), but 970 * since it is possibly not loaded/running yet, reproduce this behavior 971 * in the driver during interface creation. 972 */ 973 if (vif->idx) 974 mac_address[0] |= 0x2; 975 976 eth_hw_addr_set(vif->ndev, mac_address); 977 978 mutex_lock(&wl->vif_mutex); 979 list_add_tail_rcu(&vif->list, &wl->vif_list); 980 wl->vif_num += 1; 981 mutex_unlock(&wl->vif_mutex); 982 synchronize_rcu(); 983 984 if (rtnl_locked) 985 ret = cfg80211_register_netdevice(ndev); 986 else 987 ret = register_netdev(ndev); 988 989 if (ret) { 990 ret = -EFAULT; 991 goto error_remove_vif; 992 } 993 994 return vif; 995 996 error_remove_vif: 997 mutex_lock(&wl->vif_mutex); 998 list_del_rcu(&vif->list); 999 wl->vif_num -= 1; 1000 mutex_unlock(&wl->vif_mutex); 1001 synchronize_rcu(); 1002 free_netdev(ndev); 1003 return ERR_PTR(ret); 1004 } 1005 EXPORT_SYMBOL_GPL(wilc_netdev_ifc_init); 1006 1007 MODULE_DESCRIPTION("Atmel WILC1000 core wireless driver"); 1008 MODULE_LICENSE("GPL"); 1009 MODULE_FIRMWARE(WILC1000_FW(WILC1000_API_VER)); 1010