1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * This file contains the major functions in WLAN 4 * driver. It includes init, exit, open, close and main 5 * thread etc.. 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/module.h> 11 #include <linux/delay.h> 12 #include <linux/etherdevice.h> 13 #include <linux/hardirq.h> 14 #include <linux/netdevice.h> 15 #include <linux/if_arp.h> 16 #include <linux/kthread.h> 17 #include <linux/kfifo.h> 18 #include <linux/slab.h> 19 #include <net/cfg80211.h> 20 21 #include "host.h" 22 #include "decl.h" 23 #include "dev.h" 24 #include "cfg.h" 25 #include "debugfs.h" 26 #include "cmd.h" 27 #include "mesh.h" 28 29 #define DRIVER_RELEASE_VERSION "323.p0" 30 const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION 31 #ifdef DEBUG 32 "-dbg" 33 #endif 34 ""; 35 36 37 /* Module parameters */ 38 unsigned int lbs_debug; 39 EXPORT_SYMBOL_GPL(lbs_debug); 40 module_param_named(libertas_debug, lbs_debug, int, 0644); 41 42 static unsigned int lbs_disablemesh; 43 module_param_named(libertas_disablemesh, lbs_disablemesh, int, 0644); 44 45 46 /* 47 * This global structure is used to send the confirm_sleep command as 48 * fast as possible down to the firmware. 49 */ 50 struct cmd_confirm_sleep confirm_sleep; 51 52 53 /* 54 * the table to keep region code 55 */ 56 u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] = 57 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 }; 58 59 /* 60 * FW rate table. FW refers to rates by their index in this table, not by the 61 * rate value itself. Values of 0x00 are 62 * reserved positions. 63 */ 64 static u8 fw_data_rates[MAX_RATES] = 65 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12, 66 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00 67 }; 68 69 /** 70 * lbs_fw_index_to_data_rate - use index to get the data rate 71 * 72 * @idx: The index of data rate 73 * returns: data rate or 0 74 */ 75 u32 lbs_fw_index_to_data_rate(u8 idx) 76 { 77 if (idx >= sizeof(fw_data_rates)) 78 idx = 0; 79 return fw_data_rates[idx]; 80 } 81 82 int lbs_set_iface_type(struct lbs_private *priv, enum nl80211_iftype type) 83 { 84 int ret = 0; 85 86 switch (type) { 87 case NL80211_IFTYPE_MONITOR: 88 ret = lbs_set_monitor_mode(priv, 1); 89 break; 90 case NL80211_IFTYPE_STATION: 91 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR) 92 ret = lbs_set_monitor_mode(priv, 0); 93 if (!ret) 94 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 1); 95 break; 96 case NL80211_IFTYPE_ADHOC: 97 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR) 98 ret = lbs_set_monitor_mode(priv, 0); 99 if (!ret) 100 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 2); 101 break; 102 default: 103 ret = -ENOTSUPP; 104 } 105 return ret; 106 } 107 108 int lbs_start_iface(struct lbs_private *priv) 109 { 110 struct cmd_ds_802_11_mac_address cmd; 111 int ret; 112 113 if (priv->power_restore) { 114 ret = priv->power_restore(priv); 115 if (ret) 116 return ret; 117 } 118 119 cmd.hdr.size = cpu_to_le16(sizeof(cmd)); 120 cmd.action = cpu_to_le16(CMD_ACT_SET); 121 memcpy(cmd.macadd, priv->current_addr, ETH_ALEN); 122 123 ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd); 124 if (ret) { 125 lbs_deb_net("set MAC address failed\n"); 126 goto err; 127 } 128 129 ret = lbs_set_iface_type(priv, priv->wdev->iftype); 130 if (ret) { 131 lbs_deb_net("set iface type failed\n"); 132 goto err; 133 } 134 135 ret = lbs_set_11d_domain_info(priv); 136 if (ret) { 137 lbs_deb_net("set 11d domain info failed\n"); 138 goto err; 139 } 140 141 lbs_update_channel(priv); 142 143 priv->iface_running = true; 144 return 0; 145 146 err: 147 if (priv->power_save) 148 priv->power_save(priv); 149 return ret; 150 } 151 152 /** 153 * lbs_dev_open - open the ethX interface 154 * 155 * @dev: A pointer to &net_device structure 156 * returns: 0 or -EBUSY if monitor mode active 157 */ 158 static int lbs_dev_open(struct net_device *dev) 159 { 160 struct lbs_private *priv = dev->ml_priv; 161 int ret = 0; 162 163 if (!priv->iface_running) { 164 ret = lbs_start_iface(priv); 165 if (ret) 166 goto out; 167 } 168 169 spin_lock_irq(&priv->driver_lock); 170 171 netif_carrier_off(dev); 172 173 if (!priv->tx_pending_len) 174 netif_wake_queue(dev); 175 176 spin_unlock_irq(&priv->driver_lock); 177 178 out: 179 return ret; 180 } 181 182 static bool lbs_command_queue_empty(struct lbs_private *priv) 183 { 184 unsigned long flags; 185 bool ret; 186 spin_lock_irqsave(&priv->driver_lock, flags); 187 ret = priv->cur_cmd == NULL && list_empty(&priv->cmdpendingq); 188 spin_unlock_irqrestore(&priv->driver_lock, flags); 189 return ret; 190 } 191 192 int lbs_stop_iface(struct lbs_private *priv) 193 { 194 unsigned long flags; 195 int ret = 0; 196 197 spin_lock_irqsave(&priv->driver_lock, flags); 198 priv->iface_running = false; 199 dev_kfree_skb_irq(priv->currenttxskb); 200 priv->currenttxskb = NULL; 201 priv->tx_pending_len = 0; 202 spin_unlock_irqrestore(&priv->driver_lock, flags); 203 204 cancel_work_sync(&priv->mcast_work); 205 timer_delete_sync(&priv->tx_lockup_timer); 206 207 /* Disable command processing, and wait for all commands to complete */ 208 lbs_deb_main("waiting for commands to complete\n"); 209 wait_event(priv->waitq, lbs_command_queue_empty(priv)); 210 lbs_deb_main("all commands completed\n"); 211 212 if (priv->power_save) 213 ret = priv->power_save(priv); 214 215 return ret; 216 } 217 218 /** 219 * lbs_eth_stop - close the ethX interface 220 * 221 * @dev: A pointer to &net_device structure 222 * returns: 0 223 */ 224 static int lbs_eth_stop(struct net_device *dev) 225 { 226 struct lbs_private *priv = dev->ml_priv; 227 228 if (priv->connect_status == LBS_CONNECTED) 229 lbs_disconnect(priv, WLAN_REASON_DEAUTH_LEAVING); 230 231 spin_lock_irq(&priv->driver_lock); 232 netif_stop_queue(dev); 233 spin_unlock_irq(&priv->driver_lock); 234 235 lbs_update_mcast(priv); 236 cancel_delayed_work_sync(&priv->scan_work); 237 if (priv->scan_req) 238 lbs_scan_done(priv); 239 240 netif_carrier_off(priv->dev); 241 242 if (!lbs_iface_active(priv)) 243 lbs_stop_iface(priv); 244 245 return 0; 246 } 247 248 void lbs_host_to_card_done(struct lbs_private *priv) 249 { 250 unsigned long flags; 251 252 spin_lock_irqsave(&priv->driver_lock, flags); 253 timer_delete(&priv->tx_lockup_timer); 254 255 priv->dnld_sent = DNLD_RES_RECEIVED; 256 257 /* Wake main thread if commands are pending */ 258 if (!priv->cur_cmd || priv->tx_pending_len > 0) { 259 wake_up(&priv->waitq); 260 } 261 262 spin_unlock_irqrestore(&priv->driver_lock, flags); 263 } 264 EXPORT_SYMBOL_GPL(lbs_host_to_card_done); 265 266 int lbs_set_mac_address(struct net_device *dev, void *addr) 267 { 268 int ret = 0; 269 struct lbs_private *priv = dev->ml_priv; 270 struct sockaddr *phwaddr = addr; 271 272 /* 273 * Can only set MAC address when all interfaces are down, to be written 274 * to the hardware when one of them is brought up. 275 */ 276 if (lbs_iface_active(priv)) 277 return -EBUSY; 278 279 /* In case it was called from the mesh device */ 280 dev = priv->dev; 281 282 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN); 283 eth_hw_addr_set(dev, phwaddr->sa_data); 284 if (priv->mesh_dev) 285 eth_hw_addr_set(priv->mesh_dev, phwaddr->sa_data); 286 287 return ret; 288 } 289 290 291 static inline int mac_in_list(unsigned char *list, int list_len, 292 unsigned char *mac) 293 { 294 while (list_len) { 295 if (!memcmp(list, mac, ETH_ALEN)) 296 return 1; 297 list += ETH_ALEN; 298 list_len--; 299 } 300 return 0; 301 } 302 303 304 static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd, 305 struct net_device *dev, int nr_addrs) 306 { 307 int i = nr_addrs; 308 struct netdev_hw_addr *ha; 309 int cnt; 310 311 if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST)) 312 return nr_addrs; 313 314 netif_addr_lock_bh(dev); 315 cnt = netdev_mc_count(dev); 316 netdev_for_each_mc_addr(ha, dev) { 317 if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) { 318 lbs_deb_net("mcast address %s:%pM skipped\n", dev->name, 319 ha->addr); 320 cnt--; 321 continue; 322 } 323 324 if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE) 325 break; 326 memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN); 327 lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name, 328 ha->addr); 329 i++; 330 cnt--; 331 } 332 netif_addr_unlock_bh(dev); 333 if (cnt) 334 return -EOVERFLOW; 335 336 return i; 337 } 338 339 void lbs_update_mcast(struct lbs_private *priv) 340 { 341 struct cmd_ds_mac_multicast_adr mcast_cmd; 342 int dev_flags = 0; 343 int nr_addrs; 344 int old_mac_control = priv->mac_control; 345 346 if (netif_running(priv->dev)) 347 dev_flags |= priv->dev->flags; 348 if (priv->mesh_dev && netif_running(priv->mesh_dev)) 349 dev_flags |= priv->mesh_dev->flags; 350 351 if (dev_flags & IFF_PROMISC) { 352 priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE; 353 priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE | 354 CMD_ACT_MAC_MULTICAST_ENABLE); 355 goto out_set_mac_control; 356 } else if (dev_flags & IFF_ALLMULTI) { 357 do_allmulti: 358 priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE; 359 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE | 360 CMD_ACT_MAC_MULTICAST_ENABLE); 361 goto out_set_mac_control; 362 } 363 364 /* Once for priv->dev, again for priv->mesh_dev if it exists */ 365 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0); 366 if (nr_addrs >= 0 && priv->mesh_dev) 367 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs); 368 if (nr_addrs < 0) 369 goto do_allmulti; 370 371 if (nr_addrs) { 372 int size = offsetof(struct cmd_ds_mac_multicast_adr, 373 maclist[6*nr_addrs]); 374 375 mcast_cmd.action = cpu_to_le16(CMD_ACT_SET); 376 mcast_cmd.hdr.size = cpu_to_le16(size); 377 mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs); 378 379 lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size); 380 381 priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE; 382 } else 383 priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE; 384 385 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE | 386 CMD_ACT_MAC_ALL_MULTICAST_ENABLE); 387 out_set_mac_control: 388 if (priv->mac_control != old_mac_control) 389 lbs_set_mac_control(priv); 390 } 391 392 static void lbs_set_mcast_worker(struct work_struct *work) 393 { 394 struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work); 395 lbs_update_mcast(priv); 396 } 397 398 void lbs_set_multicast_list(struct net_device *dev) 399 { 400 struct lbs_private *priv = dev->ml_priv; 401 402 schedule_work(&priv->mcast_work); 403 } 404 405 /** 406 * lbs_thread - handles the major jobs in the LBS driver. 407 * It handles all events generated by firmware, RX data received 408 * from firmware and TX data sent from kernel. 409 * 410 * @data: A pointer to &lbs_thread structure 411 * returns: 0 412 */ 413 static int lbs_thread(void *data) 414 { 415 struct net_device *dev = data; 416 struct lbs_private *priv = dev->ml_priv; 417 wait_queue_entry_t wait; 418 419 init_waitqueue_entry(&wait, current); 420 421 for (;;) { 422 int shouldsleep; 423 u8 resp_idx; 424 425 lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n", 426 priv->currenttxskb, priv->dnld_sent); 427 428 add_wait_queue(&priv->waitq, &wait); 429 set_current_state(TASK_INTERRUPTIBLE); 430 spin_lock_irq(&priv->driver_lock); 431 432 if (kthread_should_stop()) 433 shouldsleep = 0; /* Bye */ 434 else if (priv->surpriseremoved) 435 shouldsleep = 1; /* We need to wait until we're _told_ to die */ 436 else if (priv->psstate == PS_STATE_SLEEP) 437 shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */ 438 else if (priv->cmd_timed_out) 439 shouldsleep = 0; /* Command timed out. Recover */ 440 else if (!priv->fw_ready) 441 shouldsleep = 1; /* Firmware not ready. We're waiting for it */ 442 else if (priv->dnld_sent) 443 shouldsleep = 1; /* Something is en route to the device already */ 444 else if (priv->tx_pending_len > 0) 445 shouldsleep = 0; /* We've a packet to send */ 446 else if (priv->resp_len[priv->resp_idx]) 447 shouldsleep = 0; /* We have a command response */ 448 else if (priv->cur_cmd) 449 shouldsleep = 1; /* Can't send a command; one already running */ 450 else if (!list_empty(&priv->cmdpendingq)) 451 shouldsleep = 0; /* We have a command to send */ 452 else if (kfifo_len(&priv->event_fifo)) 453 shouldsleep = 0; /* We have an event to process */ 454 else 455 shouldsleep = 1; /* No command */ 456 457 if (shouldsleep) { 458 lbs_deb_thread("sleeping, connect_status %d, " 459 "psmode %d, psstate %d\n", 460 priv->connect_status, 461 priv->psmode, priv->psstate); 462 spin_unlock_irq(&priv->driver_lock); 463 schedule(); 464 } else 465 spin_unlock_irq(&priv->driver_lock); 466 467 lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n", 468 priv->currenttxskb, priv->dnld_sent); 469 470 set_current_state(TASK_RUNNING); 471 remove_wait_queue(&priv->waitq, &wait); 472 473 lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n", 474 priv->currenttxskb, priv->dnld_sent); 475 476 if (kthread_should_stop()) { 477 lbs_deb_thread("break from main thread\n"); 478 break; 479 } 480 481 if (priv->surpriseremoved) { 482 lbs_deb_thread("adapter removed; waiting to die...\n"); 483 continue; 484 } 485 486 lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n", 487 priv->currenttxskb, priv->dnld_sent); 488 489 /* Process any pending command response */ 490 spin_lock_irq(&priv->driver_lock); 491 resp_idx = priv->resp_idx; 492 if (priv->resp_len[resp_idx]) { 493 spin_unlock_irq(&priv->driver_lock); 494 lbs_process_command_response(priv, 495 priv->resp_buf[resp_idx], 496 priv->resp_len[resp_idx]); 497 spin_lock_irq(&priv->driver_lock); 498 priv->resp_len[resp_idx] = 0; 499 } 500 spin_unlock_irq(&priv->driver_lock); 501 502 /* Process hardware events, e.g. card removed, link lost */ 503 spin_lock_irq(&priv->driver_lock); 504 while (kfifo_len(&priv->event_fifo)) { 505 u32 event; 506 507 if (kfifo_out(&priv->event_fifo, 508 (unsigned char *) &event, sizeof(event)) != 509 sizeof(event)) 510 break; 511 spin_unlock_irq(&priv->driver_lock); 512 lbs_process_event(priv, event); 513 spin_lock_irq(&priv->driver_lock); 514 } 515 spin_unlock_irq(&priv->driver_lock); 516 517 /* command timeout stuff */ 518 if (priv->cmd_timed_out && priv->cur_cmd) { 519 struct cmd_ctrl_node *cmdnode = priv->cur_cmd; 520 521 netdev_info(dev, "Timeout submitting command 0x%04x\n", 522 le16_to_cpu(cmdnode->cmdbuf->command)); 523 lbs_complete_command(priv, cmdnode, -ETIMEDOUT); 524 525 /* Reset card, but only when it isn't in the process 526 * of being shutdown anyway. */ 527 if (!dev->dismantle && priv->reset_card) 528 priv->reset_card(priv); 529 } 530 priv->cmd_timed_out = 0; 531 532 if (!priv->fw_ready) 533 continue; 534 535 /* Check if we need to confirm Sleep Request received previously */ 536 if (priv->psstate == PS_STATE_PRE_SLEEP && 537 !priv->dnld_sent && !priv->cur_cmd) { 538 if (priv->connect_status == LBS_CONNECTED) { 539 lbs_deb_thread("pre-sleep, currenttxskb %p, " 540 "dnld_sent %d, cur_cmd %p\n", 541 priv->currenttxskb, priv->dnld_sent, 542 priv->cur_cmd); 543 544 lbs_ps_confirm_sleep(priv); 545 } else { 546 /* workaround for firmware sending 547 * deauth/linkloss event immediately 548 * after sleep request; remove this 549 * after firmware fixes it 550 */ 551 priv->psstate = PS_STATE_AWAKE; 552 netdev_alert(dev, 553 "ignore PS_SleepConfirm in non-connected state\n"); 554 } 555 } 556 557 /* The PS state is changed during processing of Sleep Request 558 * event above 559 */ 560 if ((priv->psstate == PS_STATE_SLEEP) || 561 (priv->psstate == PS_STATE_PRE_SLEEP)) 562 continue; 563 564 if (priv->is_deep_sleep) 565 continue; 566 567 /* Execute the next command */ 568 if (!priv->dnld_sent && !priv->cur_cmd) 569 lbs_execute_next_command(priv); 570 571 spin_lock_irq(&priv->driver_lock); 572 if (!priv->dnld_sent && priv->tx_pending_len > 0) { 573 int ret = priv->hw_host_to_card(priv, MVMS_DAT, 574 priv->tx_pending_buf, 575 priv->tx_pending_len); 576 if (ret) { 577 lbs_deb_tx("host_to_card failed %d\n", ret); 578 priv->dnld_sent = DNLD_RES_RECEIVED; 579 } else { 580 mod_timer(&priv->tx_lockup_timer, 581 jiffies + (HZ * 5)); 582 } 583 priv->tx_pending_len = 0; 584 if (!priv->currenttxskb) { 585 /* We can wake the queues immediately if we aren't 586 waiting for TX feedback */ 587 if (priv->connect_status == LBS_CONNECTED) 588 netif_wake_queue(priv->dev); 589 if (priv->mesh_dev && 590 netif_running(priv->mesh_dev)) 591 netif_wake_queue(priv->mesh_dev); 592 } 593 } 594 spin_unlock_irq(&priv->driver_lock); 595 } 596 597 timer_delete(&priv->command_timer); 598 timer_delete(&priv->tx_lockup_timer); 599 600 return 0; 601 } 602 603 /** 604 * lbs_setup_firmware - gets the HW spec from the firmware and sets 605 * some basic parameters 606 * 607 * @priv: A pointer to &struct lbs_private structure 608 * returns: 0 or -1 609 */ 610 static int lbs_setup_firmware(struct lbs_private *priv) 611 { 612 int ret = -1; 613 s16 curlevel = 0, minlevel = 0, maxlevel = 0; 614 615 /* Read MAC address from firmware */ 616 eth_broadcast_addr(priv->current_addr); 617 ret = lbs_update_hw_spec(priv); 618 if (ret) 619 goto done; 620 621 /* Read power levels if available */ 622 ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel); 623 if (ret == 0) { 624 priv->txpower_cur = curlevel; 625 priv->txpower_min = minlevel; 626 priv->txpower_max = maxlevel; 627 } 628 629 /* Send cmd to FW to enable 11D function */ 630 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1); 631 if (ret) 632 goto done; 633 634 ret = lbs_set_mac_control_sync(priv); 635 done: 636 return ret; 637 } 638 639 int lbs_suspend(struct lbs_private *priv) 640 { 641 int ret; 642 643 if (priv->is_deep_sleep) { 644 ret = lbs_set_deep_sleep(priv, 0); 645 if (ret) { 646 netdev_err(priv->dev, 647 "deep sleep cancellation failed: %d\n", ret); 648 return ret; 649 } 650 priv->deep_sleep_required = 1; 651 } 652 653 ret = lbs_set_host_sleep(priv, 1); 654 655 netif_device_detach(priv->dev); 656 if (priv->mesh_dev) 657 netif_device_detach(priv->mesh_dev); 658 659 return ret; 660 } 661 EXPORT_SYMBOL_GPL(lbs_suspend); 662 663 int lbs_resume(struct lbs_private *priv) 664 { 665 int ret; 666 667 ret = lbs_set_host_sleep(priv, 0); 668 669 netif_device_attach(priv->dev); 670 if (priv->mesh_dev) 671 netif_device_attach(priv->mesh_dev); 672 673 if (priv->deep_sleep_required) { 674 priv->deep_sleep_required = 0; 675 ret = lbs_set_deep_sleep(priv, 1); 676 if (ret) 677 netdev_err(priv->dev, 678 "deep sleep activation failed: %d\n", ret); 679 } 680 681 if (priv->setup_fw_on_resume) 682 ret = lbs_setup_firmware(priv); 683 684 return ret; 685 } 686 EXPORT_SYMBOL_GPL(lbs_resume); 687 688 /** 689 * lbs_cmd_timeout_handler - handles the timeout of command sending. 690 * It will re-send the same command again. 691 * 692 * @t: Context from which to retrieve a &struct lbs_private pointer 693 */ 694 static void lbs_cmd_timeout_handler(struct timer_list *t) 695 { 696 struct lbs_private *priv = from_timer(priv, t, command_timer); 697 unsigned long flags; 698 699 spin_lock_irqsave(&priv->driver_lock, flags); 700 701 if (!priv->cur_cmd) 702 goto out; 703 704 netdev_info(priv->dev, "command 0x%04x timed out\n", 705 le16_to_cpu(priv->cur_cmd->cmdbuf->command)); 706 707 priv->cmd_timed_out = 1; 708 709 /* 710 * If the device didn't even acknowledge the command, reset the state 711 * so that we don't block all future commands due to this one timeout. 712 */ 713 if (priv->dnld_sent == DNLD_CMD_SENT) 714 priv->dnld_sent = DNLD_RES_RECEIVED; 715 716 wake_up(&priv->waitq); 717 out: 718 spin_unlock_irqrestore(&priv->driver_lock, flags); 719 } 720 721 /** 722 * lbs_tx_lockup_handler - handles the timeout of the passing of TX frames 723 * to the hardware. This is known to frequently happen with SD8686 when 724 * waking up after a Wake-on-WLAN-triggered resume. 725 * 726 * @t: Context from which to retrieve a &struct lbs_private pointer 727 */ 728 static void lbs_tx_lockup_handler(struct timer_list *t) 729 { 730 struct lbs_private *priv = from_timer(priv, t, tx_lockup_timer); 731 unsigned long flags; 732 733 spin_lock_irqsave(&priv->driver_lock, flags); 734 735 netdev_info(priv->dev, "TX lockup detected\n"); 736 if (priv->reset_card) 737 priv->reset_card(priv); 738 739 priv->dnld_sent = DNLD_RES_RECEIVED; 740 wake_up_interruptible(&priv->waitq); 741 742 spin_unlock_irqrestore(&priv->driver_lock, flags); 743 } 744 745 static int lbs_init_adapter(struct lbs_private *priv) 746 { 747 int ret; 748 749 eth_broadcast_addr(priv->current_addr); 750 751 priv->connect_status = LBS_DISCONNECTED; 752 priv->channel = DEFAULT_AD_HOC_CHANNEL; 753 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON; 754 priv->radio_on = 1; 755 priv->psmode = LBS802_11POWERMODECAM; 756 priv->psstate = PS_STATE_FULL_POWER; 757 priv->is_deep_sleep = 0; 758 priv->deep_sleep_required = 0; 759 init_waitqueue_head(&priv->ds_awake_q); 760 init_waitqueue_head(&priv->scan_q); 761 priv->authtype_auto = 1; 762 priv->is_host_sleep_configured = 0; 763 priv->is_host_sleep_activated = 0; 764 init_waitqueue_head(&priv->host_sleep_q); 765 init_waitqueue_head(&priv->fw_waitq); 766 mutex_init(&priv->lock); 767 768 timer_setup(&priv->command_timer, lbs_cmd_timeout_handler, 0); 769 timer_setup(&priv->tx_lockup_timer, lbs_tx_lockup_handler, 0); 770 771 INIT_LIST_HEAD(&priv->cmdfreeq); 772 INIT_LIST_HEAD(&priv->cmdpendingq); 773 774 spin_lock_init(&priv->driver_lock); 775 776 /* Allocate the command buffers */ 777 if (lbs_allocate_cmd_buffer(priv)) { 778 pr_err("Out of memory allocating command buffers\n"); 779 ret = -ENOMEM; 780 goto out; 781 } 782 priv->resp_idx = 0; 783 priv->resp_len[0] = priv->resp_len[1] = 0; 784 785 /* Create the event FIFO */ 786 ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL); 787 if (ret) { 788 pr_err("Out of memory allocating event FIFO buffer\n"); 789 lbs_free_cmd_buffer(priv); 790 goto out; 791 } 792 793 out: 794 return ret; 795 } 796 797 static void lbs_free_adapter(struct lbs_private *priv) 798 { 799 lbs_free_cmd_buffer(priv); 800 kfifo_free(&priv->event_fifo); 801 timer_delete(&priv->command_timer); 802 timer_delete(&priv->tx_lockup_timer); 803 } 804 805 static const struct net_device_ops lbs_netdev_ops = { 806 .ndo_open = lbs_dev_open, 807 .ndo_stop = lbs_eth_stop, 808 .ndo_start_xmit = lbs_hard_start_xmit, 809 .ndo_set_mac_address = lbs_set_mac_address, 810 .ndo_set_rx_mode = lbs_set_multicast_list, 811 .ndo_validate_addr = eth_validate_addr, 812 }; 813 814 /** 815 * lbs_add_card - adds the card. It will probe the 816 * card, allocate the lbs_priv and initialize the device. 817 * 818 * @card: A pointer to card 819 * @dmdev: A pointer to &struct device 820 * returns: A pointer to &struct lbs_private structure 821 */ 822 struct lbs_private *lbs_add_card(void *card, struct device *dmdev) 823 { 824 struct net_device *dev; 825 struct wireless_dev *wdev; 826 struct lbs_private *priv = NULL; 827 int err; 828 829 /* Allocate an Ethernet device and register it */ 830 wdev = lbs_cfg_alloc(dmdev); 831 if (IS_ERR(wdev)) { 832 err = PTR_ERR(wdev); 833 pr_err("cfg80211 init failed\n"); 834 goto err_cfg; 835 } 836 837 wdev->iftype = NL80211_IFTYPE_STATION; 838 priv = wdev_priv(wdev); 839 priv->wdev = wdev; 840 841 err = lbs_init_adapter(priv); 842 if (err) { 843 pr_err("failed to initialize adapter structure\n"); 844 goto err_wdev; 845 } 846 847 dev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, ether_setup); 848 if (!dev) { 849 err = -ENOMEM; 850 dev_err(dmdev, "no memory for network device instance\n"); 851 goto err_adapter; 852 } 853 854 dev->ieee80211_ptr = wdev; 855 dev->ml_priv = priv; 856 SET_NETDEV_DEV(dev, dmdev); 857 wdev->netdev = dev; 858 priv->dev = dev; 859 860 dev->netdev_ops = &lbs_netdev_ops; 861 dev->watchdog_timeo = 5 * HZ; 862 dev->ethtool_ops = &lbs_ethtool_ops; 863 dev->flags |= IFF_BROADCAST | IFF_MULTICAST; 864 865 priv->card = card; 866 867 strcpy(dev->name, "wlan%d"); 868 869 lbs_deb_thread("Starting main thread...\n"); 870 init_waitqueue_head(&priv->waitq); 871 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main"); 872 if (IS_ERR(priv->main_thread)) { 873 err = PTR_ERR(priv->main_thread); 874 lbs_deb_thread("Error creating main thread.\n"); 875 goto err_ndev; 876 } 877 878 priv->work_thread = create_singlethread_workqueue("lbs_worker"); 879 INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker); 880 881 priv->wol_criteria = EHS_REMOVE_WAKEUP; 882 priv->wol_gpio = 0xff; 883 priv->wol_gap = 20; 884 priv->ehs_remove_supported = true; 885 886 return priv; 887 888 err_ndev: 889 free_netdev(dev); 890 891 err_adapter: 892 lbs_free_adapter(priv); 893 894 err_wdev: 895 lbs_cfg_free(priv); 896 897 err_cfg: 898 return ERR_PTR(err); 899 } 900 EXPORT_SYMBOL_GPL(lbs_add_card); 901 902 903 void lbs_remove_card(struct lbs_private *priv) 904 { 905 struct net_device *dev = priv->dev; 906 907 lbs_remove_mesh(priv); 908 909 if (priv->wiphy_registered) 910 lbs_scan_deinit(priv); 911 912 lbs_wait_for_firmware_load(priv); 913 914 /* worker thread destruction blocks on the in-flight command which 915 * should have been cleared already in lbs_stop_card(). 916 */ 917 lbs_deb_main("destroying worker thread\n"); 918 destroy_workqueue(priv->work_thread); 919 lbs_deb_main("done destroying worker thread\n"); 920 921 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) { 922 priv->psmode = LBS802_11POWERMODECAM; 923 /* no need to wakeup if already woken up, 924 * on suspend, this exit ps command is not processed 925 * the driver hangs 926 */ 927 if (priv->psstate != PS_STATE_FULL_POWER) 928 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true); 929 } 930 931 if (priv->is_deep_sleep) { 932 priv->is_deep_sleep = 0; 933 wake_up_interruptible(&priv->ds_awake_q); 934 } 935 936 priv->is_host_sleep_configured = 0; 937 priv->is_host_sleep_activated = 0; 938 wake_up_interruptible(&priv->host_sleep_q); 939 940 /* Stop the thread servicing the interrupts */ 941 priv->surpriseremoved = 1; 942 kthread_stop(priv->main_thread); 943 944 lbs_free_adapter(priv); 945 lbs_cfg_free(priv); 946 free_netdev(dev); 947 } 948 EXPORT_SYMBOL_GPL(lbs_remove_card); 949 950 951 int lbs_rtap_supported(struct lbs_private *priv) 952 { 953 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5) 954 return 1; 955 956 /* newer firmware use a capability mask */ 957 return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) && 958 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK)); 959 } 960 961 962 int lbs_start_card(struct lbs_private *priv) 963 { 964 struct net_device *dev = priv->dev; 965 int ret; 966 967 /* poke the firmware */ 968 ret = lbs_setup_firmware(priv); 969 if (ret) 970 goto done; 971 972 if (!lbs_disablemesh) 973 lbs_init_mesh(priv); 974 else 975 pr_info("%s: mesh disabled\n", dev->name); 976 977 ret = lbs_cfg_register(priv); 978 if (ret) { 979 pr_err("cannot register device\n"); 980 goto done; 981 } 982 983 if (lbs_mesh_activated(priv)) 984 lbs_start_mesh(priv); 985 986 lbs_debugfs_init_one(priv, dev); 987 988 netdev_info(dev, "Marvell WLAN 802.11 adapter\n"); 989 990 ret = 0; 991 992 done: 993 return ret; 994 } 995 EXPORT_SYMBOL_GPL(lbs_start_card); 996 997 998 void lbs_stop_card(struct lbs_private *priv) 999 { 1000 struct net_device *dev; 1001 1002 if (!priv) 1003 return; 1004 dev = priv->dev; 1005 1006 /* If the netdev isn't registered, it means that lbs_start_card() was 1007 * never called so we have nothing to do here. */ 1008 if (dev->reg_state != NETREG_REGISTERED) 1009 return; 1010 1011 netif_stop_queue(dev); 1012 netif_carrier_off(dev); 1013 1014 lbs_debugfs_remove_one(priv); 1015 lbs_deinit_mesh(priv); 1016 unregister_netdev(dev); 1017 } 1018 EXPORT_SYMBOL_GPL(lbs_stop_card); 1019 1020 1021 void lbs_queue_event(struct lbs_private *priv, u32 event) 1022 { 1023 unsigned long flags; 1024 1025 spin_lock_irqsave(&priv->driver_lock, flags); 1026 1027 if (priv->psstate == PS_STATE_SLEEP) 1028 priv->psstate = PS_STATE_AWAKE; 1029 1030 kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32)); 1031 1032 wake_up(&priv->waitq); 1033 1034 spin_unlock_irqrestore(&priv->driver_lock, flags); 1035 } 1036 EXPORT_SYMBOL_GPL(lbs_queue_event); 1037 1038 void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx) 1039 { 1040 if (priv->psstate == PS_STATE_SLEEP) 1041 priv->psstate = PS_STATE_AWAKE; 1042 1043 /* Swap buffers by flipping the response index */ 1044 BUG_ON(resp_idx > 1); 1045 priv->resp_idx = resp_idx; 1046 1047 wake_up(&priv->waitq); 1048 } 1049 EXPORT_SYMBOL_GPL(lbs_notify_command_response); 1050 1051 static int __init lbs_init_module(void) 1052 { 1053 memset(&confirm_sleep, 0, sizeof(confirm_sleep)); 1054 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE); 1055 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep)); 1056 confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED); 1057 lbs_debugfs_init(); 1058 1059 return 0; 1060 } 1061 1062 static void __exit lbs_exit_module(void) 1063 { 1064 lbs_debugfs_remove(); 1065 } 1066 1067 module_init(lbs_init_module); 1068 module_exit(lbs_exit_module); 1069 1070 MODULE_DESCRIPTION("Libertas WLAN Driver Library"); 1071 MODULE_AUTHOR("Marvell International Ltd."); 1072 MODULE_LICENSE("GPL"); 1073