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 */
lbs_fw_index_to_data_rate(u8 idx)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
lbs_set_iface_type(struct lbs_private * priv,enum nl80211_iftype type)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
lbs_start_iface(struct lbs_private * priv)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 */
lbs_dev_open(struct net_device * dev)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
lbs_command_queue_empty(struct lbs_private * priv)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
lbs_stop_iface(struct lbs_private * priv)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 */
lbs_eth_stop(struct net_device * dev)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
lbs_host_to_card_done(struct lbs_private * priv)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
lbs_set_mac_address(struct net_device * dev,void * addr)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
mac_in_list(unsigned char * list,int list_len,unsigned char * mac)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
lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr * cmd,struct net_device * dev,int nr_addrs)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
lbs_update_mcast(struct lbs_private * priv)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
lbs_set_mcast_worker(struct work_struct * work)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
lbs_set_multicast_list(struct net_device * dev)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 */
lbs_thread(void * data)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 */
lbs_setup_firmware(struct lbs_private * priv)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
lbs_suspend(struct lbs_private * priv)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
lbs_resume(struct lbs_private * priv)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 */
lbs_cmd_timeout_handler(struct timer_list * t)694 static void lbs_cmd_timeout_handler(struct timer_list *t)
695 {
696 struct lbs_private *priv = timer_container_of(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 */
lbs_tx_lockup_handler(struct timer_list * t)728 static void lbs_tx_lockup_handler(struct timer_list *t)
729 {
730 struct lbs_private *priv = timer_container_of(priv, t,
731 tx_lockup_timer);
732 unsigned long flags;
733
734 spin_lock_irqsave(&priv->driver_lock, flags);
735
736 netdev_info(priv->dev, "TX lockup detected\n");
737 if (priv->reset_card)
738 priv->reset_card(priv);
739
740 priv->dnld_sent = DNLD_RES_RECEIVED;
741 wake_up_interruptible(&priv->waitq);
742
743 spin_unlock_irqrestore(&priv->driver_lock, flags);
744 }
745
lbs_init_adapter(struct lbs_private * priv)746 static int lbs_init_adapter(struct lbs_private *priv)
747 {
748 int ret;
749
750 eth_broadcast_addr(priv->current_addr);
751
752 priv->connect_status = LBS_DISCONNECTED;
753 priv->channel = DEFAULT_AD_HOC_CHANNEL;
754 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
755 priv->radio_on = 1;
756 priv->psmode = LBS802_11POWERMODECAM;
757 priv->psstate = PS_STATE_FULL_POWER;
758 priv->is_deep_sleep = 0;
759 priv->deep_sleep_required = 0;
760 init_waitqueue_head(&priv->ds_awake_q);
761 init_waitqueue_head(&priv->scan_q);
762 priv->authtype_auto = 1;
763 priv->is_host_sleep_configured = 0;
764 priv->is_host_sleep_activated = 0;
765 init_waitqueue_head(&priv->host_sleep_q);
766 init_waitqueue_head(&priv->fw_waitq);
767 mutex_init(&priv->lock);
768
769 timer_setup(&priv->command_timer, lbs_cmd_timeout_handler, 0);
770 timer_setup(&priv->tx_lockup_timer, lbs_tx_lockup_handler, 0);
771
772 INIT_LIST_HEAD(&priv->cmdfreeq);
773 INIT_LIST_HEAD(&priv->cmdpendingq);
774
775 spin_lock_init(&priv->driver_lock);
776
777 /* Allocate the command buffers */
778 if (lbs_allocate_cmd_buffer(priv)) {
779 pr_err("Out of memory allocating command buffers\n");
780 ret = -ENOMEM;
781 goto out;
782 }
783 priv->resp_idx = 0;
784 priv->resp_len[0] = priv->resp_len[1] = 0;
785
786 /* Create the event FIFO */
787 ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
788 if (ret) {
789 pr_err("Out of memory allocating event FIFO buffer\n");
790 lbs_free_cmd_buffer(priv);
791 goto out;
792 }
793
794 out:
795 return ret;
796 }
797
lbs_free_adapter(struct lbs_private * priv)798 static void lbs_free_adapter(struct lbs_private *priv)
799 {
800 lbs_free_cmd_buffer(priv);
801 kfifo_free(&priv->event_fifo);
802 timer_delete(&priv->command_timer);
803 timer_delete(&priv->tx_lockup_timer);
804 }
805
806 static const struct net_device_ops lbs_netdev_ops = {
807 .ndo_open = lbs_dev_open,
808 .ndo_stop = lbs_eth_stop,
809 .ndo_start_xmit = lbs_hard_start_xmit,
810 .ndo_set_mac_address = lbs_set_mac_address,
811 .ndo_set_rx_mode = lbs_set_multicast_list,
812 .ndo_validate_addr = eth_validate_addr,
813 };
814
815 /**
816 * lbs_add_card - adds the card. It will probe the
817 * card, allocate the lbs_priv and initialize the device.
818 *
819 * @card: A pointer to card
820 * @dmdev: A pointer to &struct device
821 * returns: A pointer to &struct lbs_private structure
822 */
lbs_add_card(void * card,struct device * dmdev)823 struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
824 {
825 struct net_device *dev;
826 struct wireless_dev *wdev;
827 struct lbs_private *priv = NULL;
828 int err;
829
830 /* Allocate an Ethernet device and register it */
831 wdev = lbs_cfg_alloc(dmdev);
832 if (IS_ERR(wdev)) {
833 err = PTR_ERR(wdev);
834 pr_err("cfg80211 init failed\n");
835 goto err_cfg;
836 }
837
838 wdev->iftype = NL80211_IFTYPE_STATION;
839 priv = wdev_priv(wdev);
840 priv->wdev = wdev;
841
842 err = lbs_init_adapter(priv);
843 if (err) {
844 pr_err("failed to initialize adapter structure\n");
845 goto err_wdev;
846 }
847
848 dev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, ether_setup);
849 if (!dev) {
850 err = -ENOMEM;
851 dev_err(dmdev, "no memory for network device instance\n");
852 goto err_adapter;
853 }
854
855 dev->ieee80211_ptr = wdev;
856 dev->ml_priv = priv;
857 SET_NETDEV_DEV(dev, dmdev);
858 wdev->netdev = dev;
859 priv->dev = dev;
860
861 dev->netdev_ops = &lbs_netdev_ops;
862 dev->watchdog_timeo = 5 * HZ;
863 dev->ethtool_ops = &lbs_ethtool_ops;
864 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
865
866 priv->card = card;
867
868 strcpy(dev->name, "wlan%d");
869
870 lbs_deb_thread("Starting main thread...\n");
871 init_waitqueue_head(&priv->waitq);
872 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
873 if (IS_ERR(priv->main_thread)) {
874 err = PTR_ERR(priv->main_thread);
875 lbs_deb_thread("Error creating main thread.\n");
876 goto err_ndev;
877 }
878
879 priv->work_thread = create_singlethread_workqueue("lbs_worker");
880 INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
881
882 priv->wol_criteria = EHS_REMOVE_WAKEUP;
883 priv->wol_gpio = 0xff;
884 priv->wol_gap = 20;
885 priv->ehs_remove_supported = true;
886
887 return priv;
888
889 err_ndev:
890 free_netdev(dev);
891
892 err_adapter:
893 lbs_free_adapter(priv);
894
895 err_wdev:
896 lbs_cfg_free(priv);
897
898 err_cfg:
899 return ERR_PTR(err);
900 }
901 EXPORT_SYMBOL_GPL(lbs_add_card);
902
903
lbs_remove_card(struct lbs_private * priv)904 void lbs_remove_card(struct lbs_private *priv)
905 {
906 struct net_device *dev = priv->dev;
907
908 lbs_remove_mesh(priv);
909
910 if (priv->wiphy_registered)
911 lbs_scan_deinit(priv);
912
913 lbs_wait_for_firmware_load(priv);
914
915 /* worker thread destruction blocks on the in-flight command which
916 * should have been cleared already in lbs_stop_card().
917 */
918 lbs_deb_main("destroying worker thread\n");
919 destroy_workqueue(priv->work_thread);
920 lbs_deb_main("done destroying worker thread\n");
921
922 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
923 priv->psmode = LBS802_11POWERMODECAM;
924 /* no need to wakeup if already woken up,
925 * on suspend, this exit ps command is not processed
926 * the driver hangs
927 */
928 if (priv->psstate != PS_STATE_FULL_POWER)
929 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
930 }
931
932 if (priv->is_deep_sleep) {
933 priv->is_deep_sleep = 0;
934 wake_up_interruptible(&priv->ds_awake_q);
935 }
936
937 priv->is_host_sleep_configured = 0;
938 priv->is_host_sleep_activated = 0;
939 wake_up_interruptible(&priv->host_sleep_q);
940
941 /* Stop the thread servicing the interrupts */
942 priv->surpriseremoved = 1;
943 kthread_stop(priv->main_thread);
944
945 lbs_free_adapter(priv);
946 lbs_cfg_free(priv);
947 free_netdev(dev);
948 }
949 EXPORT_SYMBOL_GPL(lbs_remove_card);
950
951
lbs_rtap_supported(struct lbs_private * priv)952 int lbs_rtap_supported(struct lbs_private *priv)
953 {
954 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
955 return 1;
956
957 /* newer firmware use a capability mask */
958 return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
959 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
960 }
961
962
lbs_start_card(struct lbs_private * priv)963 int lbs_start_card(struct lbs_private *priv)
964 {
965 struct net_device *dev = priv->dev;
966 int ret;
967
968 /* poke the firmware */
969 ret = lbs_setup_firmware(priv);
970 if (ret)
971 goto done;
972
973 if (!lbs_disablemesh)
974 lbs_init_mesh(priv);
975 else
976 pr_info("%s: mesh disabled\n", dev->name);
977
978 ret = lbs_cfg_register(priv);
979 if (ret) {
980 pr_err("cannot register device\n");
981 goto done;
982 }
983
984 if (lbs_mesh_activated(priv))
985 lbs_start_mesh(priv);
986
987 lbs_debugfs_init_one(priv, dev);
988
989 netdev_info(dev, "Marvell WLAN 802.11 adapter\n");
990
991 ret = 0;
992
993 done:
994 return ret;
995 }
996 EXPORT_SYMBOL_GPL(lbs_start_card);
997
998
lbs_stop_card(struct lbs_private * priv)999 void lbs_stop_card(struct lbs_private *priv)
1000 {
1001 struct net_device *dev;
1002
1003 if (!priv)
1004 return;
1005 dev = priv->dev;
1006
1007 /* If the netdev isn't registered, it means that lbs_start_card() was
1008 * never called so we have nothing to do here. */
1009 if (dev->reg_state != NETREG_REGISTERED)
1010 return;
1011
1012 netif_stop_queue(dev);
1013 netif_carrier_off(dev);
1014
1015 lbs_debugfs_remove_one(priv);
1016 lbs_deinit_mesh(priv);
1017 unregister_netdev(dev);
1018 }
1019 EXPORT_SYMBOL_GPL(lbs_stop_card);
1020
1021
lbs_queue_event(struct lbs_private * priv,u32 event)1022 void lbs_queue_event(struct lbs_private *priv, u32 event)
1023 {
1024 unsigned long flags;
1025
1026 spin_lock_irqsave(&priv->driver_lock, flags);
1027
1028 if (priv->psstate == PS_STATE_SLEEP)
1029 priv->psstate = PS_STATE_AWAKE;
1030
1031 kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
1032
1033 wake_up(&priv->waitq);
1034
1035 spin_unlock_irqrestore(&priv->driver_lock, flags);
1036 }
1037 EXPORT_SYMBOL_GPL(lbs_queue_event);
1038
lbs_notify_command_response(struct lbs_private * priv,u8 resp_idx)1039 void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
1040 {
1041 if (priv->psstate == PS_STATE_SLEEP)
1042 priv->psstate = PS_STATE_AWAKE;
1043
1044 /* Swap buffers by flipping the response index */
1045 BUG_ON(resp_idx > 1);
1046 priv->resp_idx = resp_idx;
1047
1048 wake_up(&priv->waitq);
1049 }
1050 EXPORT_SYMBOL_GPL(lbs_notify_command_response);
1051
lbs_init_module(void)1052 static int __init lbs_init_module(void)
1053 {
1054 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1055 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1056 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1057 confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
1058 lbs_debugfs_init();
1059
1060 return 0;
1061 }
1062
lbs_exit_module(void)1063 static void __exit lbs_exit_module(void)
1064 {
1065 lbs_debugfs_remove();
1066 }
1067
1068 module_init(lbs_init_module);
1069 module_exit(lbs_exit_module);
1070
1071 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1072 MODULE_AUTHOR("Marvell International Ltd.");
1073 MODULE_LICENSE("GPL");
1074