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