1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * NXP Wireless LAN device driver: major functions
4 *
5 * Copyright 2011-2024 NXP
6 */
7
8 #include <linux/suspend.h>
9
10 #include "main.h"
11 #include "cmdevt.h"
12 #include "wmm.h"
13 #include "cfg80211.h"
14 #include "11n.h"
15
16 #define VERSION "1.0"
17
18 static unsigned int debug_mask = NXPWIFI_DEFAULT_DEBUG_MASK;
19
20 char nxpwifi_driver_version[] = "nxpwifi " VERSION " (%s) ";
21
22 const u16 nxpwifi_1d_to_wmm_queue[8] = { 1, 0, 0, 1, 2, 2, 3, 3 };
23
24 /* Optional RF calibration data file */
25 static const char *cal_data_name = "nxp/cal_data.conf";
26
27 /* Register device; init adapter/privs/if_ops/locks; cleanup on fail. */
nxpwifi_register(void * card,struct device * dev,struct nxpwifi_if_ops * if_ops)28 static struct nxpwifi_adapter *nxpwifi_register(void *card, struct device *dev,
29 struct nxpwifi_if_ops *if_ops)
30 {
31 struct nxpwifi_adapter *adapter;
32 int ret = 0;
33 int i;
34
35 adapter = kzalloc_obj(*adapter, GFP_KERNEL);
36 if (!adapter)
37 return ERR_PTR(-ENOMEM);
38
39 adapter->dev = dev;
40 adapter->card = card;
41
42 /* Save interface specific operations in adapter */
43 memmove(&adapter->if_ops, if_ops, sizeof(struct nxpwifi_if_ops));
44 adapter->debug_mask = debug_mask;
45
46 /* card specific initialization has been deferred until now .. */
47 if (adapter->if_ops.init_if) {
48 ret = adapter->if_ops.init_if(adapter);
49 if (ret)
50 goto error;
51 }
52
53 adapter->priv_num = 0;
54
55 for (i = 0; i < NXPWIFI_MAX_BSS_NUM; i++) {
56 /* Allocate memory for private structure */
57 adapter->priv[i] =
58 kzalloc_obj(struct nxpwifi_private, GFP_KERNEL);
59 if (!adapter->priv[i]) {
60 ret = -ENOMEM;
61 goto error;
62 }
63
64 adapter->priv[i]->adapter = adapter;
65 adapter->priv_num++;
66 }
67 nxpwifi_init_lock_list(adapter);
68
69 timer_setup(&adapter->cmd_timer, nxpwifi_cmd_timeout_func, 0);
70
71 if (ret)
72 return ERR_PTR(ret);
73 else
74 return adapter;
75
76 error:
77 nxpwifi_dbg(adapter, ERROR,
78 "info: leave %s with error\n", __func__);
79
80 for (i = 0; i < adapter->priv_num; i++)
81 kfree(adapter->priv[i]);
82
83 kfree(adapter);
84
85 return ERR_PTR(ret);
86 }
87
88 /* Unregister device; free timers, beacons, privs, nd_info, adapter. */
nxpwifi_unregister(struct nxpwifi_adapter * adapter)89 static void nxpwifi_unregister(struct nxpwifi_adapter *adapter)
90 {
91 s32 i;
92
93 if (adapter->if_ops.cleanup_if)
94 adapter->if_ops.cleanup_if(adapter);
95
96 timer_delete_sync(&adapter->cmd_timer);
97
98 /* Free private structures */
99 for (i = 0; i < adapter->priv_num; i++) {
100 nxpwifi_free_curr_bcn(adapter->priv[i]);
101 kfree(adapter->priv[i]);
102 }
103
104 if (adapter->nd_info) {
105 for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
106 kfree(adapter->nd_info->matches[i]);
107 kfree(adapter->nd_info);
108 adapter->nd_info = NULL;
109 }
110
111 kfree(adapter->regd);
112
113 kfree(adapter);
114 }
115
nxpwifi_queue_rx_work(struct nxpwifi_adapter * adapter)116 static void nxpwifi_queue_rx_work(struct nxpwifi_adapter *adapter)
117 {
118 queue_work(adapter->rx_workqueue, &adapter->rx_work);
119 }
120
nxpwifi_process_rx(struct nxpwifi_adapter * adapter)121 static void nxpwifi_process_rx(struct nxpwifi_adapter *adapter)
122 {
123 struct sk_buff *skb;
124 struct nxpwifi_rxinfo *rx_info;
125
126 if (atomic_read(&adapter->iface_changing) ||
127 atomic_read(&adapter->rx_ba_teardown_pending))
128 return;
129
130 /* Check for Rx data */
131 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
132 atomic_dec(&adapter->rx_pending);
133 if (adapter->delay_main_work &&
134 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
135 adapter->delay_main_work = false;
136 nxpwifi_queue_work(adapter, &adapter->main_work);
137 }
138 rx_info = NXPWIFI_SKB_RXCB(skb);
139 if (rx_info->buf_type == NXPWIFI_TYPE_AGGR_DATA) {
140 if (adapter->if_ops.deaggr_pkt)
141 adapter->if_ops.deaggr_pkt(adapter, skb);
142 dev_kfree_skb_any(skb);
143 } else {
144 nxpwifi_handle_rx_packet(adapter, skb);
145 }
146 }
147 }
148
maybe_quirk_fw_disable_ds(struct nxpwifi_adapter * adapter)149 static void maybe_quirk_fw_disable_ds(struct nxpwifi_adapter *adapter)
150 {
151 struct nxpwifi_private *priv = nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_STA);
152 struct nxpwifi_ver_ext ver_ext;
153
154 if (test_and_set_bit(NXPWIFI_IS_REQUESTING_FW_VEREXT, &adapter->work_flags))
155 return;
156
157 memset(&ver_ext, 0, sizeof(ver_ext));
158 ver_ext.version_str_sel = 1;
159 if (nxpwifi_send_cmd(priv, HOST_CMD_VERSION_EXT,
160 HOST_ACT_GEN_GET, 0, &ver_ext, false)) {
161 nxpwifi_dbg(priv->adapter, MSG,
162 "Checking hardware revision failed.\n");
163 }
164 }
165
nxpwifi_handle_irq_status(struct nxpwifi_adapter * adapter,u8 istat)166 static void nxpwifi_handle_irq_status(struct nxpwifi_adapter *adapter, u8 istat)
167 {
168 if (adapter->hs_activated)
169 nxpwifi_process_hs_config(adapter);
170 if (adapter->if_ops.process_int_status)
171 adapter->if_ops.process_int_status(adapter, istat);
172 }
173
nxpwifi_drain_tx(struct nxpwifi_adapter * adapter)174 static bool nxpwifi_drain_tx(struct nxpwifi_adapter *adapter)
175 {
176 bool ret = false;
177
178 if ((adapter->scan_chan_gap_enabled || !adapter->scan_processing) &&
179 !adapter->data_sent && !skb_queue_empty(&adapter->tx_data_q)) {
180 if (adapter->hs_activated_manually) {
181 nxpwifi_cancel_hs(nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_ANY),
182 NXPWIFI_ASYNC_CMD);
183 adapter->hs_activated_manually = false;
184 }
185
186 nxpwifi_process_tx_queue(adapter);
187 if (adapter->hs_activated) {
188 clear_bit(NXPWIFI_IS_HS_CONFIGURED,
189 &adapter->work_flags);
190 nxpwifi_hs_activated_event
191 (nxpwifi_get_priv
192 (adapter, NXPWIFI_BSS_ROLE_ANY),
193 false);
194 }
195 ret = true;
196 }
197
198 if ((adapter->scan_chan_gap_enabled ||
199 !adapter->scan_processing) &&
200 !adapter->data_sent &&
201 !nxpwifi_bypass_txlist_empty(adapter)) {
202 if (adapter->hs_activated_manually) {
203 nxpwifi_cancel_hs(nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_ANY),
204 NXPWIFI_ASYNC_CMD);
205 adapter->hs_activated_manually = false;
206 }
207 nxpwifi_process_bypass_tx(adapter);
208 if (adapter->hs_activated) {
209 clear_bit(NXPWIFI_IS_HS_CONFIGURED,
210 &adapter->work_flags);
211 nxpwifi_hs_activated_event
212 (nxpwifi_get_priv
213 (adapter, NXPWIFI_BSS_ROLE_ANY),
214 false);
215 }
216 ret = true;
217 }
218
219 if ((adapter->scan_chan_gap_enabled ||
220 !adapter->scan_processing) &&
221 !adapter->data_sent && !nxpwifi_wmm_lists_empty(adapter)) {
222 if (adapter->hs_activated_manually) {
223 nxpwifi_cancel_hs(nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_ANY),
224 NXPWIFI_ASYNC_CMD);
225 adapter->hs_activated_manually = false;
226 }
227
228 nxpwifi_wmm_process_tx(adapter);
229 if (adapter->hs_activated) {
230 clear_bit(NXPWIFI_IS_HS_CONFIGURED,
231 &adapter->work_flags);
232 nxpwifi_hs_activated_event
233 (nxpwifi_get_priv
234 (adapter, NXPWIFI_BSS_ROLE_ANY),
235 false);
236 }
237 ret = true;
238 }
239
240 return ret;
241 }
242
nxpwifi_handle_rx(struct nxpwifi_adapter * adapter)243 static bool nxpwifi_handle_rx(struct nxpwifi_adapter *adapter)
244 {
245 if (adapter->rx_work_enabled && adapter->data_received) {
246 nxpwifi_queue_rx_work(adapter);
247 return true;
248 }
249
250 return false;
251 }
252
nxpwifi_handle_cmd_response(struct nxpwifi_adapter * adapter)253 static bool nxpwifi_handle_cmd_response(struct nxpwifi_adapter *adapter)
254 {
255 /* Check for Cmd Resp */
256 if (adapter->cmd_resp_received) {
257 adapter->cmd_resp_received = false;
258 nxpwifi_process_cmdresp(adapter);
259 return true;
260 }
261
262 return false;
263 }
264
nxpwifi_handle_events(struct nxpwifi_adapter * adapter)265 static bool nxpwifi_handle_events(struct nxpwifi_adapter *adapter)
266 {
267 if (adapter->event_received) {
268 adapter->event_received = false;
269 nxpwifi_process_event(adapter);
270 return true;
271 }
272
273 return false;
274 }
275
nxpwifi_tx_has_pending(struct nxpwifi_adapter * adapter)276 static bool nxpwifi_tx_has_pending(struct nxpwifi_adapter *adapter)
277 {
278 return !skb_queue_empty(&adapter->tx_data_q) ||
279 !nxpwifi_bypass_txlist_empty(adapter) ||
280 !nxpwifi_wmm_lists_empty(adapter);
281 }
282
nxpwifi_cmd_has_pending(struct nxpwifi_adapter * adapter)283 static bool nxpwifi_cmd_has_pending(struct nxpwifi_adapter *adapter)
284 {
285 return !list_empty(&adapter->cmd_pending_q);
286 }
287
nxpwifi_events_has_pending(struct nxpwifi_adapter * adapter)288 static bool nxpwifi_events_has_pending(struct nxpwifi_adapter *adapter)
289 {
290 return adapter->event_received;
291 }
292
nxpwifi_should_wakeup_card(struct nxpwifi_adapter * adapter)293 static bool nxpwifi_should_wakeup_card(struct nxpwifi_adapter *adapter)
294 {
295 if (adapter->ps_state != PS_STATE_SLEEP)
296 return false;
297
298 if (!adapter->pm_wakeup_card_req || adapter->pm_wakeup_fw_try)
299 return false;
300
301 return nxpwifi_is_command_pending(adapter) || nxpwifi_tx_has_pending(adapter);
302 }
303
nxpwifi_should_exit_main_loop(struct nxpwifi_adapter * adapter)304 static bool nxpwifi_should_exit_main_loop(struct nxpwifi_adapter *adapter)
305 {
306 if (adapter->pm_wakeup_fw_try)
307 return true;
308
309 if (adapter->ps_state == PS_STATE_PRE_SLEEP)
310 nxpwifi_check_ps_cond(adapter);
311
312 if (adapter->ps_state != PS_STATE_AWAKE)
313 return true;
314
315 if (adapter->tx_lock_flag)
316 return true;
317
318 if ((!adapter->scan_chan_gap_enabled && adapter->scan_processing) ||
319 adapter->data_sent || !nxpwifi_tx_has_pending(adapter)) {
320 if (adapter->cmd_sent || adapter->curr_cmd ||
321 !nxpwifi_is_command_pending(adapter))
322 return true;
323 }
324
325 return false;
326 }
327
nxpwifi_wakeup_card(struct nxpwifi_adapter * adapter)328 static void nxpwifi_wakeup_card(struct nxpwifi_adapter *adapter)
329 {
330 adapter->pm_wakeup_fw_try = true;
331 mod_timer(&adapter->wakeup_timer, jiffies + (HZ * 3));
332 adapter->if_ops.wakeup(adapter);
333 }
334
nxpwifi_handle_vdll_download(struct nxpwifi_adapter * adapter)335 static void nxpwifi_handle_vdll_download(struct nxpwifi_adapter *adapter)
336 {
337 if (!adapter->cmd_sent && adapter->vdll_ctrl.pending_block) {
338 struct vdll_dnld_ctrl *ctrl = &adapter->vdll_ctrl;
339
340 nxpwifi_download_vdll_block(adapter, ctrl->pending_block,
341 ctrl->pending_block_len);
342 ctrl->pending_block = NULL;
343 }
344 }
345
nxpwifi_finish_delayed_null_pkt(struct nxpwifi_adapter * adapter)346 static void nxpwifi_finish_delayed_null_pkt(struct nxpwifi_adapter *adapter)
347 {
348 if (!adapter->delay_null_pkt)
349 return;
350
351 if (adapter->cmd_sent || adapter->curr_cmd || nxpwifi_is_command_pending(adapter))
352 return;
353
354 if (nxpwifi_tx_has_pending(adapter))
355 return;
356
357 if (!nxpwifi_send_null_packet(nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_STA),
358 NXPWIFI_TxPD_POWER_MGMT_NULL_PACKET |
359 NXPWIFI_TxPD_POWER_MGMT_LAST_PACKET)) {
360 adapter->delay_null_pkt = false;
361 adapter->ps_state = PS_STATE_SLEEP;
362 }
363 }
364
nxpwifi_pump_command(struct nxpwifi_adapter * adapter)365 static bool nxpwifi_pump_command(struct nxpwifi_adapter *adapter)
366 {
367 if (!adapter->cmd_sent && !adapter->curr_cmd) {
368 if (!nxpwifi_exec_next_cmd(adapter))
369 return true;
370 }
371
372 return false;
373 }
374
375 /* Main loop: IRQ/RX/CMD/EVENT; wake card; TX; PS null; exit if idle. */
nxpwifi_main_process(struct nxpwifi_adapter * adapter)376 void nxpwifi_main_process(struct nxpwifi_adapter *adapter)
377 {
378 unsigned long flags;
379
380 /* Check if virtual interface changing */
381 if (atomic_read(&adapter->iface_changing)) {
382 nxpwifi_dbg(adapter,
383 INFO, "main_process skipped due to iface_changing");
384 return;
385 }
386
387 for (;;) {
388 bool did_work = false;
389 u8 istat = 0;
390
391 if (adapter->hw_status == NXPWIFI_HW_STATUS_NOT_READY)
392 break;
393
394 /*
395 * For non-USB interfaces, If we process interrupts first, it
396 * would increase RX pending even further. Avoid this by
397 * checking if rx_pending has crossed high threshold and
398 * schedule rx work queue and then process interrupts.
399 * For USB interface, there are no interrupts. We already have
400 * HIGH_RX_PENDING check in usb.c
401 */
402 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING) {
403 adapter->delay_main_work = true;
404 nxpwifi_queue_rx_work(adapter);
405 break;
406 }
407
408 /*
409 * Snapshot-and-clear the interrupt status.
410 *
411 * Take the same lock as the producer (nxpwifi_sdio_interrupt()) uses
412 * when OR-ing new bits into adapter->int_status. We atomically grab
413 * what has accumulated and clear it, so this consumer owns this batch.
414 */
415 spin_lock_irqsave(&adapter->int_lock, flags);
416 istat = adapter->int_status;
417 adapter->int_status = 0;
418 spin_unlock_irqrestore(&adapter->int_lock, flags);
419
420 /* Handle pending interrupt if any */
421 if (istat) {
422 nxpwifi_handle_irq_status(adapter, istat);
423 did_work = true;
424 }
425
426 did_work |= nxpwifi_handle_rx(adapter);
427
428 if (nxpwifi_should_wakeup_card(adapter)) {
429 nxpwifi_wakeup_card(adapter);
430 continue;
431 }
432
433 if (IS_CARD_RX_RCVD(adapter)) {
434 /* Card has responded, clear wakeup state and update power state */
435 adapter->data_received = false;
436 adapter->pm_wakeup_fw_try = false;
437 timer_delete(&adapter->wakeup_timer);
438 if (adapter->ps_state == PS_STATE_SLEEP)
439 adapter->ps_state = PS_STATE_AWAKE;
440 } else {
441 if (nxpwifi_should_exit_main_loop(adapter))
442 break;
443 }
444
445 did_work |= nxpwifi_handle_events(adapter);
446
447 did_work |= nxpwifi_handle_cmd_response(adapter);
448
449 /* Check if we need to confirm Sleep Request received previously */
450 if (adapter->ps_state == PS_STATE_PRE_SLEEP)
451 nxpwifi_check_ps_cond(adapter);
452
453 /*
454 * The ps_state may have been changed during processing of
455 * Sleep Request event.
456 */
457 if (adapter->ps_state != PS_STATE_AWAKE)
458 continue;
459
460 if (adapter->tx_lock_flag)
461 continue;
462
463 nxpwifi_handle_vdll_download(adapter);
464
465 did_work |= nxpwifi_pump_command(adapter);
466
467 did_work |= nxpwifi_drain_tx(adapter);
468
469 /*
470 * Attempt to send delayed null packet.
471 * If successful, firmware will enter sleep and ps_state will be updated.
472 * We check ps_state here to determine if main loop can safely exit.
473 */
474 nxpwifi_finish_delayed_null_pkt(adapter);
475
476 if (adapter->ps_state == PS_STATE_SLEEP)
477 break;
478 /*
479 * Step 3) Cooperative preemption point.
480 * cond_resched() yields ONLY if need_resched() is set. Placing it
481 * BEFORE the final check improves fairness: it lets ksdioirqd (RT/FIFO)
482 * or other producers run and set new int_status bits. Immediately
483 * after we return here, we perform the final "net cast" (Step 4) to
484 * decide if we should continue or return.
485 */
486
487 cond_resched();
488
489 /*
490 * Step 4) Exit decision with lost-kick closure.
491 *
492 * We consider exiting ONLY when this round did no real work.
493 * Rationale:
494 * - If did_work == true: we will loop anyway; at Step 1 we will
495 * re-snapshot int_status, so there's no need to re-check now.
496 * - If did_work == false: we appear idle and may return. But during
497 * our execution window, a producer may have just set int_status and
498 * queue_work(); since this work is still running, queue_work()
499 * returns false (no second instance queued). If we return now,
500 * we'd leave unprocessed status with no pending work => lost-kick.
501 *
502 * Therefore, perform a single final check: if *anything* is pending,
503 * continue looping; otherwise, break and return.
504 */
505 if (!did_work) {
506 bool more = false;
507 unsigned long flags;
508 /* 4a) New IRQ bits raced in while we were running? */
509 spin_lock_irqsave(&adapter->int_lock, flags);
510 more |= adapter->int_status != 0;
511 spin_unlock_irqrestore(&adapter->int_lock, flags);
512 /* 4b) Any other sources still pending? (driver-specific) */
513 more |= nxpwifi_tx_has_pending(adapter);
514 more |= nxpwifi_cmd_has_pending(adapter);
515 more |= nxpwifi_events_has_pending(adapter);
516
517 if (!more)
518 break; /* Truly quiescent now: safe to return. */
519 /* else: loop back to Step 1 to consume what just arrived. */
520 }
521 /* If did_work == true, we loop unconditionally and re-snapshot. */
522 };
523 }
524
525 /* Free adapter via nxpwifi_unregister(). */
nxpwifi_free_adapter(struct nxpwifi_adapter * adapter)526 static void nxpwifi_free_adapter(struct nxpwifi_adapter *adapter)
527 {
528 if (!adapter) {
529 pr_err("%s: adapter is NULL\n", __func__);
530 return;
531 }
532
533 nxpwifi_unregister(adapter);
534 pr_debug("info: %s: free adapter\n", __func__);
535 }
536
537 /* Destroy main and RX workqueues. */
nxpwifi_terminate_workqueue(struct nxpwifi_adapter * adapter)538 static void nxpwifi_terminate_workqueue(struct nxpwifi_adapter *adapter)
539 {
540 if (adapter->workqueue) {
541 destroy_workqueue(adapter->workqueue);
542 adapter->workqueue = NULL;
543 }
544
545 if (adapter->rx_workqueue) {
546 destroy_workqueue(adapter->rx_workqueue);
547 adapter->rx_workqueue = NULL;
548 }
549 }
550
551 /* FW bring-up: download, enable IRQ, init FW; cfg80211+ifaces; cleanup. */
_nxpwifi_fw_dpc(const struct firmware * firmware,void * context)552 static int _nxpwifi_fw_dpc(const struct firmware *firmware, void *context)
553 {
554 int ret = 0;
555 char fmt[64];
556 struct nxpwifi_adapter *adapter = context;
557 struct nxpwifi_fw_image fw;
558 bool init_failed = false;
559 struct wireless_dev *wdev;
560 struct completion *fw_done = adapter->fw_done;
561
562 if (!firmware) {
563 nxpwifi_dbg(adapter, ERROR,
564 "Failed to get firmware %s\n", adapter->fw_name);
565 ret = -EINVAL;
566 goto err_dnld_fw;
567 }
568
569 memset(&fw, 0, sizeof(struct nxpwifi_fw_image));
570 adapter->firmware = firmware;
571 fw.fw_buf = (u8 *)adapter->firmware->data;
572 fw.fw_len = adapter->firmware->size;
573
574 if (adapter->if_ops.dnld_fw)
575 ret = adapter->if_ops.dnld_fw(adapter, &fw);
576 else
577 ret = nxpwifi_dnld_fw(adapter, &fw);
578
579 if (ret)
580 goto err_dnld_fw;
581
582 nxpwifi_dbg(adapter, MSG, "WLAN FW is active\n");
583
584 /* Load optional calibration data */
585 ret = request_firmware(&adapter->cal_data, cal_data_name, adapter->dev);
586 if (ret) {
587 nxpwifi_dbg(adapter, INFO, "no %s, using default cal\n",
588 cal_data_name);
589 adapter->cal_data = NULL;
590 }
591
592 /* enable host interrupt after fw dnld is successful */
593 if (adapter->if_ops.enable_int) {
594 ret = adapter->if_ops.enable_int(adapter);
595 if (ret)
596 goto err_dnld_fw;
597 }
598
599 ret = nxpwifi_init_fw(adapter);
600 if (ret)
601 goto err_init_fw;
602
603 maybe_quirk_fw_disable_ds(adapter);
604
605 if (!adapter->wiphy) {
606 if (nxpwifi_register_cfg80211(adapter)) {
607 nxpwifi_dbg(adapter, ERROR,
608 "cannot register with cfg80211\n");
609 goto err_init_fw;
610 }
611 }
612
613 if (nxpwifi_init_channel_scan_gap(adapter)) {
614 nxpwifi_dbg(adapter, ERROR,
615 "could not init channel stats table\n");
616 goto err_init_chan_scan;
617 }
618
619 rtnl_lock();
620 /* Create station interface by default */
621 wdev = nxpwifi_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
622 NL80211_IFTYPE_STATION, NULL);
623 if (IS_ERR(wdev)) {
624 nxpwifi_dbg(adapter, ERROR,
625 "cannot create default STA interface\n");
626 rtnl_unlock();
627 goto err_add_intf;
628 }
629
630 wdev = nxpwifi_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
631 NL80211_IFTYPE_AP, NULL);
632 if (IS_ERR(wdev)) {
633 nxpwifi_dbg(adapter, ERROR,
634 "cannot create AP interface\n");
635 rtnl_unlock();
636 goto err_add_intf;
637 }
638
639 rtnl_unlock();
640
641 nxpwifi_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
642 nxpwifi_dbg(adapter, MSG, "driver_version = %s\n", fmt);
643 adapter->is_up = true;
644 goto done;
645
646 err_add_intf:
647 vfree(adapter->chan_stats);
648 err_init_chan_scan:
649 wiphy_unregister(adapter->wiphy);
650 wiphy_free(adapter->wiphy);
651 err_init_fw:
652 if (adapter->if_ops.disable_int)
653 adapter->if_ops.disable_int(adapter);
654 err_dnld_fw:
655 nxpwifi_dbg(adapter, ERROR,
656 "info: %s: unregister device\n", __func__);
657 if (adapter->if_ops.unregister_dev)
658 adapter->if_ops.unregister_dev(adapter);
659
660 set_bit(NXPWIFI_SURPRISE_REMOVED, &adapter->work_flags);
661 nxpwifi_terminate_workqueue(adapter);
662
663 if (adapter->hw_status == NXPWIFI_HW_STATUS_READY) {
664 pr_debug("info: %s: shutdown nxpwifi\n", __func__);
665 nxpwifi_shutdown_drv(adapter);
666 nxpwifi_free_cmd_buffers(adapter);
667 }
668
669 init_failed = true;
670 done:
671 if (adapter->cal_data) {
672 release_firmware(adapter->cal_data);
673 adapter->cal_data = NULL;
674 }
675 if (adapter->firmware) {
676 release_firmware(adapter->firmware);
677 adapter->firmware = NULL;
678 }
679 if (init_failed)
680 nxpwifi_free_adapter(adapter);
681
682 /* Tell all current and future waiters we're finished */
683 complete_all(fw_done);
684
685 return ret;
686 }
687
nxpwifi_fw_dpc(const struct firmware * firmware,void * context)688 static void nxpwifi_fw_dpc(const struct firmware *firmware, void *context)
689 {
690 _nxpwifi_fw_dpc(firmware, context);
691 }
692
693 /* Request firmware (sync/async) and start HW init. */
nxpwifi_init_hw_fw(struct nxpwifi_adapter * adapter,bool req_fw_nowait)694 static int nxpwifi_init_hw_fw(struct nxpwifi_adapter *adapter,
695 bool req_fw_nowait)
696 {
697 int ret;
698
699 if (req_fw_nowait) {
700 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
701 adapter->dev, GFP_KERNEL, adapter,
702 nxpwifi_fw_dpc);
703 } else {
704 ret = request_firmware(&adapter->firmware,
705 adapter->fw_name,
706 adapter->dev);
707 }
708
709 if (ret < 0)
710 nxpwifi_dbg(adapter, ERROR, "request_firmware%s error %d\n",
711 req_fw_nowait ? "_nowait" : "", ret);
712 return ret;
713 }
714
715 /* ndo_open: bring carrier down. */
716 static int
nxpwifi_open(struct net_device * dev)717 nxpwifi_open(struct net_device *dev)
718 {
719 netif_carrier_off(dev);
720
721 return 0;
722 }
723
724 /* ndo_stop: abort scan/sched-scan if running. */
725 static int
nxpwifi_close(struct net_device * dev)726 nxpwifi_close(struct net_device *dev)
727 {
728 struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
729
730 if (priv->scan_request) {
731 struct cfg80211_scan_info info = {
732 .aborted = true,
733 };
734
735 nxpwifi_dbg(priv->adapter, INFO,
736 "aborting scan on ndo_stop\n");
737 cfg80211_scan_done(priv->scan_request, &info);
738 priv->scan_request = NULL;
739 priv->scan_aborting = true;
740 }
741
742 if (priv->sched_scanning) {
743 nxpwifi_dbg(priv->adapter, INFO,
744 "aborting bgscan on ndo_stop\n");
745 nxpwifi_stop_bg_scan(priv);
746 cfg80211_sched_scan_stopped(priv->wdev.wiphy, 0);
747 }
748
749 return 0;
750 }
751
752 static bool
nxpwifi_bypass_tx_queue(struct nxpwifi_private * priv,struct sk_buff * skb)753 nxpwifi_bypass_tx_queue(struct nxpwifi_private *priv,
754 struct sk_buff *skb)
755 {
756 struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
757
758 if (eth_hdr->h_proto == htons(ETH_P_PAE) ||
759 nxpwifi_is_skb_mgmt_frame(skb)) {
760 nxpwifi_dbg(priv->adapter, DATA,
761 "bypass txqueue; eth type %#x, mgmt %d\n",
762 ntohs(eth_hdr->h_proto),
763 nxpwifi_is_skb_mgmt_frame(skb));
764 if (eth_hdr->h_proto == htons(ETH_P_PAE))
765 nxpwifi_dbg(priv->adapter, MSG,
766 "key: send EAPOL to %pM\n",
767 eth_hdr->h_dest);
768 return true;
769 }
770
771 return false;
772 }
773
774 /* Queue SKB (WMM or bypass) and schedule main work. */
nxpwifi_queue_tx_pkt(struct nxpwifi_private * priv,struct sk_buff * skb)775 void nxpwifi_queue_tx_pkt(struct nxpwifi_private *priv, struct sk_buff *skb)
776 {
777 struct nxpwifi_adapter *adapter = priv->adapter;
778 struct netdev_queue *txq;
779 int index = nxpwifi_1d_to_wmm_queue[skb->priority];
780
781 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
782 txq = netdev_get_tx_queue(priv->netdev, index);
783 if (!netif_tx_queue_stopped(txq)) {
784 netif_tx_stop_queue(txq);
785 nxpwifi_dbg(adapter, DATA,
786 "stop queue: %d\n", index);
787 }
788 }
789
790 if (nxpwifi_bypass_tx_queue(priv, skb)) {
791 atomic_inc(&adapter->tx_pending);
792 atomic_inc(&adapter->bypass_tx_pending);
793 nxpwifi_wmm_add_buf_bypass_txqueue(priv, skb);
794 } else {
795 atomic_inc(&adapter->tx_pending);
796 nxpwifi_wmm_add_buf_txqueue(priv, skb);
797 }
798
799 nxpwifi_queue_work(adapter, &adapter->main_work);
800 }
801
802 struct sk_buff *
nxpwifi_clone_skb_for_tx_status(struct nxpwifi_private * priv,struct sk_buff * skb,u8 flag,u64 * cookie)803 nxpwifi_clone_skb_for_tx_status(struct nxpwifi_private *priv,
804 struct sk_buff *skb, u8 flag, u64 *cookie)
805 {
806 struct sk_buff *orig_skb = skb;
807 struct nxpwifi_txinfo *tx_info, *orig_tx_info;
808 u32 id32 = 0;
809 int ret;
810
811 skb = skb_clone(skb, GFP_ATOMIC);
812 if (skb) {
813 spin_lock_bh(&priv->ack_status_lock);
814 /*
815 * Use XArray to allocate IDs in the range 1..0x0F.
816 * Limit ensures the allocated token ID is always within this
817 * range.
818 */
819 ret = xa_alloc(&priv->ack_status_frames, &id32, orig_skb,
820 XA_LIMIT(1, 0x0f), GFP_ATOMIC);
821 spin_unlock_bh(&priv->ack_status_lock);
822
823 if (ret == 0) {
824 tx_info = NXPWIFI_SKB_TXCB(skb);
825 tx_info->ack_frame_id = id32;
826 tx_info->flags |= flag;
827 orig_tx_info = NXPWIFI_SKB_TXCB(orig_skb);
828 orig_tx_info->ack_frame_id = id32;
829 orig_tx_info->flags |= flag;
830
831 if (flag == NXPWIFI_BUF_FLAG_ACTION_TX_STATUS && cookie)
832 orig_tx_info->cookie = *cookie;
833
834 } else if (skb_shared(skb)) {
835 kfree_skb(orig_skb);
836 } else {
837 kfree_skb(skb);
838 skb = orig_skb;
839 }
840 } else {
841 /* couldn't clone -- lose tx status ... */
842 skb = orig_skb;
843 }
844
845 return skb;
846 }
847
848 /* ndo_start_xmit: fix headroom, fill TXCB, timestamp, enqueue. */
849 static netdev_tx_t
nxpwifi_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)850 nxpwifi_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
851 {
852 struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
853 struct sk_buff *new_skb;
854 struct nxpwifi_txinfo *tx_info;
855 bool multicast;
856
857 nxpwifi_dbg(priv->adapter, DATA,
858 "data: %lu BSS(%d-%d): Data <= kernel\n",
859 jiffies, priv->bss_type, priv->bss_num);
860
861 if (test_bit(NXPWIFI_SURPRISE_REMOVED, &priv->adapter->work_flags)) {
862 kfree_skb(skb);
863 priv->stats.tx_dropped++;
864 return 0;
865 }
866 if (!skb->len || skb->len > ETH_FRAME_LEN) {
867 nxpwifi_dbg(priv->adapter, ERROR,
868 "Tx: bad skb len %d\n", skb->len);
869 kfree_skb(skb);
870 priv->stats.tx_dropped++;
871 return 0;
872 }
873 if (skb_headroom(skb) < NXPWIFI_MIN_DATA_HEADER_LEN) {
874 nxpwifi_dbg(priv->adapter, DATA,
875 "data: Tx: insufficient skb headroom %d\n",
876 skb_headroom(skb));
877 /* Insufficient skb headroom - allocate a new skb */
878 new_skb =
879 skb_realloc_headroom(skb, NXPWIFI_MIN_DATA_HEADER_LEN);
880 if (unlikely(!new_skb)) {
881 nxpwifi_dbg(priv->adapter, ERROR,
882 "Tx: cannot alloca new_skb\n");
883 kfree_skb(skb);
884 priv->stats.tx_dropped++;
885 return 0;
886 }
887 kfree_skb(skb);
888 skb = new_skb;
889 nxpwifi_dbg(priv->adapter, INFO,
890 "info: new skb headroomd %d\n",
891 skb_headroom(skb));
892 }
893
894 tx_info = NXPWIFI_SKB_TXCB(skb);
895 memset(tx_info, 0, sizeof(*tx_info));
896 tx_info->bss_num = priv->bss_num;
897 tx_info->bss_type = priv->bss_type;
898 tx_info->pkt_len = skb->len;
899
900 multicast = is_multicast_ether_addr(skb->data);
901
902 if (unlikely(!multicast && sk_requests_wifi_status(skb->sk) &&
903 priv->adapter->fw_api_ver == NXPWIFI_FW_V15))
904 skb = nxpwifi_clone_skb_for_tx_status(priv,
905 skb,
906 NXPWIFI_BUF_FLAG_EAPOL_TX_STATUS, NULL);
907
908 /*
909 * Record the current time the packet was queued; used to
910 * determine the amount of time the packet was queued in
911 * the driver before it was sent to the firmware.
912 * The delay is then sent along with the packet to the
913 * firmware for aggregate delay calculation for stats and
914 * MSDU lifetime expiry.
915 */
916 __net_timestamp(skb);
917
918 nxpwifi_queue_tx_pkt(priv, skb);
919
920 return 0;
921 }
922
nxpwifi_set_mac_address(struct nxpwifi_private * priv,struct net_device * dev,bool external,u8 * new_mac)923 int nxpwifi_set_mac_address(struct nxpwifi_private *priv,
924 struct net_device *dev, bool external,
925 u8 *new_mac)
926 {
927 int ret;
928 u64 mac_addr, old_mac_addr;
929
930 old_mac_addr = ether_addr_to_u64(priv->curr_addr);
931
932 if (external) {
933 mac_addr = ether_addr_to_u64(new_mac);
934 } else {
935 /* Internal mac address change */
936 if (priv->bss_type == NXPWIFI_BSS_TYPE_ANY)
937 return -EOPNOTSUPP;
938
939 mac_addr = old_mac_addr;
940
941 if (priv->adapter->priv[0] != priv) {
942 /* Set mac address based on bss_type/bss_num */
943 mac_addr ^= BIT_ULL(priv->bss_type + 8);
944 mac_addr += priv->bss_num;
945 }
946 }
947
948 u64_to_ether_addr(mac_addr, priv->curr_addr);
949
950 /* Send request to firmware */
951 ret = nxpwifi_send_cmd(priv, HOST_CMD_802_11_MAC_ADDRESS,
952 HOST_ACT_GEN_SET, 0, NULL, true);
953
954 if (ret) {
955 u64_to_ether_addr(old_mac_addr, priv->curr_addr);
956 nxpwifi_dbg(priv->adapter, ERROR,
957 "set mac address failed: ret=%d\n", ret);
958 return ret;
959 }
960
961 eth_hw_addr_set(dev, priv->curr_addr);
962 return 0;
963 }
964
965 /* ndo_set_mac_address: set MAC via firmware. */
966 static int
nxpwifi_ndo_set_mac_address(struct net_device * dev,void * addr)967 nxpwifi_ndo_set_mac_address(struct net_device *dev, void *addr)
968 {
969 struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
970 struct sockaddr *hw_addr = addr;
971
972 return nxpwifi_set_mac_address(priv, dev, true, hw_addr->sa_data);
973 }
974
975 /* ndo_set_rx_mode: promisc/allmulti or program multicast. */
nxpwifi_set_multicast_list(struct net_device * dev)976 static void nxpwifi_set_multicast_list(struct net_device *dev)
977 {
978 struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
979 struct nxpwifi_multicast_list mcast_list;
980
981 if (dev->flags & IFF_PROMISC) {
982 mcast_list.mode = NXPWIFI_PROMISC_MODE;
983 } else if (dev->flags & IFF_ALLMULTI ||
984 netdev_mc_count(dev) > NXPWIFI_MAX_MULTICAST_LIST_SIZE) {
985 mcast_list.mode = NXPWIFI_ALL_MULTI_MODE;
986 } else {
987 mcast_list.mode = NXPWIFI_MULTICAST_MODE;
988 mcast_list.num_multicast_addr =
989 nxpwifi_copy_mcast_addr(&mcast_list, dev);
990 }
991 nxpwifi_request_set_multicast_list(priv, &mcast_list);
992 }
993
994 /* ndo_tx_timeout: account; reset card on threshold. */
995 static void
nxpwifi_tx_timeout(struct net_device * dev,unsigned int txqueue)996 nxpwifi_tx_timeout(struct net_device *dev, unsigned int txqueue)
997 {
998 struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
999
1000 priv->num_tx_timeout++;
1001 priv->tx_timeout_cnt++;
1002 nxpwifi_dbg(priv->adapter, ERROR,
1003 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
1004 jiffies, priv->tx_timeout_cnt, priv->bss_type,
1005 priv->bss_num);
1006 nxpwifi_set_trans_start(dev);
1007
1008 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
1009 priv->adapter->if_ops.card_reset) {
1010 nxpwifi_dbg(priv->adapter, ERROR,
1011 "tx_timeout_cnt exceeds threshold.\t"
1012 "Triggering card reset!\n");
1013 priv->adapter->if_ops.card_reset(priv->adapter);
1014 }
1015 }
1016
nxpwifi_upload_device_dump(struct nxpwifi_adapter * adapter)1017 void nxpwifi_upload_device_dump(struct nxpwifi_adapter *adapter)
1018 {
1019 /*
1020 * Dump all the memory data into single file, a userspace script will
1021 * be used to split all the memory data to multiple files
1022 */
1023 nxpwifi_dbg(adapter, MSG,
1024 "== nxpwifi dump information to /sys/class/devcoredump start\n");
1025 dev_coredumpv(adapter->dev, adapter->devdump_data, adapter->devdump_len,
1026 GFP_KERNEL);
1027 nxpwifi_dbg(adapter, MSG,
1028 "== nxpwifi dump information to /sys/class/devcoredump end\n");
1029
1030 /*
1031 * Device dump data will be freed in device coredump release function
1032 * after 5 min. Here reset adapter->devdump_data and ->devdump_len
1033 * to avoid it been accidentally reused.
1034 */
1035 adapter->devdump_data = NULL;
1036 adapter->devdump_len = 0;
1037 }
1038 EXPORT_SYMBOL_GPL(nxpwifi_upload_device_dump);
1039
nxpwifi_drv_info_dump(struct nxpwifi_adapter * adapter)1040 void nxpwifi_drv_info_dump(struct nxpwifi_adapter *adapter)
1041 {
1042 char *p;
1043 char drv_version[64];
1044 struct sdio_mmc_card *sdio_card;
1045 struct nxpwifi_private *priv;
1046 int i, idx;
1047 struct netdev_queue *txq;
1048 struct nxpwifi_debug_info *debug_info;
1049
1050 nxpwifi_dbg(adapter, MSG, "===nxpwifi driverinfo dump start===\n");
1051
1052 p = adapter->devdump_data;
1053 strscpy(p, "========Start dump driverinfo========\n", NXPWIFI_FW_DUMP_SIZE);
1054 p += strlen("========Start dump driverinfo========\n");
1055 p += sprintf(p, "driver_name = ");
1056 p += sprintf(p, "\"nxpwifi\"\n");
1057
1058 nxpwifi_drv_get_driver_version(adapter, drv_version,
1059 sizeof(drv_version) - 1);
1060 p += sprintf(p, "driver_version = %s\n", drv_version);
1061
1062 p += sprintf(p, "tx_pending = %d\n",
1063 atomic_read(&adapter->tx_pending));
1064 p += sprintf(p, "rx_pending = %d\n",
1065 atomic_read(&adapter->rx_pending));
1066
1067 if (adapter->iface_type == NXPWIFI_SDIO) {
1068 sdio_card = (struct sdio_mmc_card *)adapter->card;
1069 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
1070 sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
1071 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
1072 sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
1073 }
1074
1075 for (i = 0; i < adapter->priv_num; i++) {
1076 if (!adapter->priv[i]->netdev)
1077 continue;
1078 priv = adapter->priv[i];
1079 p += sprintf(p, "\n[interface : \"%s\"]\n",
1080 priv->netdev->name);
1081 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
1082 atomic_read(&priv->wmm_tx_pending[0]));
1083 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
1084 atomic_read(&priv->wmm_tx_pending[1]));
1085 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
1086 atomic_read(&priv->wmm_tx_pending[2]));
1087 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1088 atomic_read(&priv->wmm_tx_pending[3]));
1089 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1090 "Disconnected" : "Connected");
1091 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1092 ? "on" : "off"));
1093 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1094 txq = netdev_get_tx_queue(priv->netdev, idx);
1095 p += sprintf(p, "tx queue %d:%s ", idx,
1096 netif_tx_queue_stopped(txq) ?
1097 "stopped" : "started");
1098 }
1099 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1100 priv->netdev->name, priv->num_tx_timeout);
1101 }
1102
1103 if (adapter->iface_type == NXPWIFI_SDIO) {
1104 p += sprintf(p, "\n=== %s register dump===\n", "SDIO");
1105 if (adapter->if_ops.reg_dump)
1106 p += adapter->if_ops.reg_dump(adapter, p);
1107 }
1108 p += sprintf(p, "\n=== more debug information\n");
1109 debug_info = kzalloc_obj(*debug_info, GFP_KERNEL);
1110 if (debug_info) {
1111 for (i = 0; i < adapter->priv_num; i++) {
1112 if (!adapter->priv[i]->netdev)
1113 continue;
1114 priv = adapter->priv[i];
1115 nxpwifi_get_debug_info(priv, debug_info);
1116 p += nxpwifi_debug_info_to_buffer(priv, p, debug_info);
1117 break;
1118 }
1119 kfree(debug_info);
1120 }
1121
1122 p += sprintf(p, "\n========End dump========\n");
1123 nxpwifi_dbg(adapter, MSG, "===nxpwifi driverinfo dump end===\n");
1124 adapter->devdump_len = p - (char *)adapter->devdump_data;
1125 }
1126 EXPORT_SYMBOL_GPL(nxpwifi_drv_info_dump);
1127
nxpwifi_prepare_fw_dump_info(struct nxpwifi_adapter * adapter)1128 void nxpwifi_prepare_fw_dump_info(struct nxpwifi_adapter *adapter)
1129 {
1130 u8 idx;
1131 char *fw_dump_ptr;
1132 u32 dump_len = 0;
1133
1134 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1135 struct memory_type_mapping *entry =
1136 &adapter->mem_type_mapping_tbl[idx];
1137
1138 if (entry->mem_ptr) {
1139 dump_len += (strlen("========Start dump ") +
1140 strlen(entry->mem_name) +
1141 strlen("========\n") +
1142 (entry->mem_size + 1) +
1143 strlen("\n========End dump========\n"));
1144 }
1145 }
1146
1147 if (dump_len + 1 + adapter->devdump_len > NXPWIFI_FW_DUMP_SIZE) {
1148 /* Realloc in case buffer overflow */
1149 fw_dump_ptr = vzalloc(dump_len + 1 + adapter->devdump_len);
1150 nxpwifi_dbg(adapter, MSG, "Realloc device dump data.\n");
1151 if (!fw_dump_ptr) {
1152 vfree(adapter->devdump_data);
1153 nxpwifi_dbg(adapter, ERROR,
1154 "vzalloc devdump data failure!\n");
1155 return;
1156 }
1157
1158 memmove(fw_dump_ptr, adapter->devdump_data,
1159 adapter->devdump_len);
1160 vfree(adapter->devdump_data);
1161 adapter->devdump_data = fw_dump_ptr;
1162 }
1163
1164 fw_dump_ptr = (char *)adapter->devdump_data + adapter->devdump_len;
1165
1166 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1167 struct memory_type_mapping *entry =
1168 &adapter->mem_type_mapping_tbl[idx];
1169
1170 if (entry->mem_ptr) {
1171 fw_dump_ptr += sprintf(fw_dump_ptr, "========Start dump ");
1172 fw_dump_ptr += sprintf(fw_dump_ptr, "%s", entry->mem_name);
1173 fw_dump_ptr += sprintf(fw_dump_ptr, "========\n");
1174 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1175 fw_dump_ptr += entry->mem_size;
1176 fw_dump_ptr += sprintf(fw_dump_ptr, "\n========End dump========\n");
1177 }
1178 }
1179
1180 adapter->devdump_len = fw_dump_ptr - (char *)adapter->devdump_data;
1181
1182 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1183 struct memory_type_mapping *entry =
1184 &adapter->mem_type_mapping_tbl[idx];
1185
1186 vfree(entry->mem_ptr);
1187 entry->mem_ptr = NULL;
1188 entry->mem_size = 0;
1189 }
1190 }
1191 EXPORT_SYMBOL_GPL(nxpwifi_prepare_fw_dump_info);
1192
1193 /* ndo_get_stats: return netdev stats. */
nxpwifi_get_stats(struct net_device * dev)1194 static struct net_device_stats *nxpwifi_get_stats(struct net_device *dev)
1195 {
1196 struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
1197
1198 return &priv->stats;
1199 }
1200
1201 static u16
nxpwifi_netdev_select_wmm_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)1202 nxpwifi_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
1203 struct net_device *sb_dev)
1204 {
1205 skb->priority = cfg80211_classify8021d(skb, NULL);
1206 return nxpwifi_1d_to_wmm_queue[skb->priority];
1207 }
1208
1209 /* Network device handlers */
1210 static const struct net_device_ops nxpwifi_netdev_ops = {
1211 .ndo_open = nxpwifi_open,
1212 .ndo_stop = nxpwifi_close,
1213 .ndo_start_xmit = nxpwifi_hard_start_xmit,
1214 .ndo_set_mac_address = nxpwifi_ndo_set_mac_address,
1215 .ndo_validate_addr = eth_validate_addr,
1216 .ndo_tx_timeout = nxpwifi_tx_timeout,
1217 .ndo_get_stats = nxpwifi_get_stats,
1218 .ndo_set_rx_mode = nxpwifi_set_multicast_list,
1219 .ndo_select_queue = nxpwifi_netdev_select_wmm_queue,
1220 };
1221
1222 /* Init per-interface defaults: ops, addrs, mgmt IEs, stats. */
nxpwifi_init_priv_params(struct nxpwifi_private * priv,struct net_device * dev)1223 void nxpwifi_init_priv_params(struct nxpwifi_private *priv,
1224 struct net_device *dev)
1225 {
1226 dev->netdev_ops = &nxpwifi_netdev_ops;
1227 dev->needs_free_netdev = true;
1228 /* Initialize private structure */
1229 priv->current_key_index = 0;
1230 priv->media_connected = false;
1231 memset(priv->mgmt_ie, 0,
1232 sizeof(struct nxpwifi_ie) * MAX_MGMT_IE_INDEX);
1233 priv->beacon_idx = NXPWIFI_AUTO_IDX_MASK;
1234 priv->proberesp_idx = NXPWIFI_AUTO_IDX_MASK;
1235 priv->assocresp_idx = NXPWIFI_AUTO_IDX_MASK;
1236 priv->gen_idx = NXPWIFI_AUTO_IDX_MASK;
1237 priv->num_tx_timeout = 0;
1238 if (is_valid_ether_addr(dev->dev_addr))
1239 ether_addr_copy(priv->curr_addr, dev->dev_addr);
1240 else
1241 ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
1242
1243 if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_STA ||
1244 GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP) {
1245 priv->hist_data = kmalloc_obj(*priv->hist_data, GFP_KERNEL);
1246 if (priv->hist_data)
1247 nxpwifi_hist_data_reset(priv);
1248 }
1249 }
1250
1251 /* Return true if any command is pending. */
nxpwifi_is_command_pending(struct nxpwifi_adapter * adapter)1252 int nxpwifi_is_command_pending(struct nxpwifi_adapter *adapter)
1253 {
1254 int is_cmd_pend_q_empty;
1255
1256 spin_lock_bh(&adapter->cmd_pending_q_lock);
1257 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1258 spin_unlock_bh(&adapter->cmd_pending_q_lock);
1259
1260 return !is_cmd_pend_q_empty;
1261 }
1262
1263 /* Host MLME work: deliver RX; handle assoc/link-loss. */
nxpwifi_host_mlme_work(struct wiphy * wiphy,struct wiphy_work * work)1264 static void nxpwifi_host_mlme_work(struct wiphy *wiphy, struct wiphy_work *work)
1265 {
1266 struct nxpwifi_adapter *adapter =
1267 container_of(work, struct nxpwifi_adapter, host_mlme_work);
1268 struct sk_buff *skb;
1269 struct nxpwifi_rxinfo *rx_info;
1270 struct nxpwifi_private *priv;
1271
1272 if (test_bit(NXPWIFI_SURPRISE_REMOVED, &adapter->work_flags))
1273 return;
1274
1275 while ((skb = skb_dequeue(&adapter->rx_mlme_q))) {
1276 rx_info = NXPWIFI_SKB_RXCB(skb);
1277 priv = adapter->priv[rx_info->bss_num];
1278 cfg80211_rx_mlme_mgmt(priv->netdev,
1279 skb->data,
1280 rx_info->pkt_len);
1281 }
1282
1283 /* Check for host mlme disconnection */
1284 if (adapter->host_mlme_link_lost) {
1285 if (adapter->priv_link_lost) {
1286 nxpwifi_reset_connect_state(adapter->priv_link_lost,
1287 WLAN_REASON_DEAUTH_LEAVING,
1288 true);
1289 adapter->priv_link_lost = NULL;
1290 }
1291 adapter->host_mlme_link_lost = false;
1292 }
1293
1294 /* Check for host mlme Assoc Resp */
1295 if (adapter->assoc_resp_received) {
1296 nxpwifi_process_assoc_resp(adapter);
1297 adapter->assoc_resp_received = false;
1298 }
1299 }
1300
1301 /* RX work: process RX queue. */
nxpwifi_rx_work(struct work_struct * work)1302 static void nxpwifi_rx_work(struct work_struct *work)
1303 {
1304 struct nxpwifi_adapter *adapter =
1305 container_of(work, struct nxpwifi_adapter, rx_work);
1306
1307 if (test_bit(NXPWIFI_SURPRISE_REMOVED, &adapter->work_flags))
1308 return;
1309 nxpwifi_process_rx(adapter);
1310 }
1311
1312 /* Main work: run nxpwifi_main_process(). */
nxpwifi_main_work(struct work_struct * work)1313 static void nxpwifi_main_work(struct work_struct *work)
1314 {
1315 struct nxpwifi_adapter *adapter =
1316 container_of(work, struct nxpwifi_adapter, main_work);
1317
1318 if (test_bit(NXPWIFI_SURPRISE_REMOVED, &adapter->work_flags))
1319 return;
1320 nxpwifi_main_process(adapter);
1321 }
1322
1323 /* Teardown: disable IRQs, stop queues, shutdown, remove ifaces, unreg. */
nxpwifi_uninit_sw(struct nxpwifi_adapter * adapter)1324 static void nxpwifi_uninit_sw(struct nxpwifi_adapter *adapter)
1325 {
1326 struct nxpwifi_private *priv;
1327 int i;
1328
1329 /*
1330 * We can no longer handle interrupts once we start doing the teardown
1331 * below.
1332 */
1333 if (adapter->if_ops.disable_int)
1334 adapter->if_ops.disable_int(adapter);
1335
1336 set_bit(NXPWIFI_SURPRISE_REMOVED, &adapter->work_flags);
1337 nxpwifi_terminate_workqueue(adapter);
1338 adapter->int_status = 0;
1339
1340 /* Stop data */
1341 for (i = 0; i < adapter->priv_num; i++) {
1342 priv = adapter->priv[i];
1343 if (priv->netdev) {
1344 nxpwifi_stop_net_dev_queue(priv->netdev, adapter);
1345 netif_carrier_off(priv->netdev);
1346 netif_device_detach(priv->netdev);
1347 }
1348 }
1349
1350 nxpwifi_dbg(adapter, CMD, "cmd: calling nxpwifi_shutdown_drv...\n");
1351 nxpwifi_shutdown_drv(adapter);
1352 nxpwifi_dbg(adapter, CMD, "cmd: nxpwifi_shutdown_drv done\n");
1353
1354 if (atomic_read(&adapter->rx_pending) ||
1355 atomic_read(&adapter->tx_pending) ||
1356 atomic_read(&adapter->cmd_pending)) {
1357 nxpwifi_dbg(adapter, ERROR,
1358 "rx_pending=%d, tx_pending=%d,\t"
1359 "cmd_pending=%d\n",
1360 atomic_read(&adapter->rx_pending),
1361 atomic_read(&adapter->tx_pending),
1362 atomic_read(&adapter->cmd_pending));
1363 }
1364
1365 for (i = 0; i < adapter->priv_num; i++) {
1366 priv = adapter->priv[i];
1367 rtnl_lock();
1368 if (priv->netdev &&
1369 priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
1370 /*
1371 * Close the netdev now, because if we do it later, the
1372 * netdev notifiers will need to acquire the wiphy lock
1373 * again --> deadlock.
1374 */
1375 dev_close(priv->wdev.netdev);
1376 wiphy_lock(adapter->wiphy);
1377 nxpwifi_del_virtual_intf(adapter->wiphy, &priv->wdev);
1378 wiphy_unlock(adapter->wiphy);
1379 }
1380 rtnl_unlock();
1381 }
1382
1383 wiphy_unregister(adapter->wiphy);
1384 wiphy_free(adapter->wiphy);
1385 adapter->wiphy = NULL;
1386
1387 vfree(adapter->chan_stats);
1388 nxpwifi_free_cmd_buffers(adapter);
1389 }
1390
1391 /* Shut down SW/FW and mark device down. */
nxpwifi_shutdown_sw(struct nxpwifi_adapter * adapter)1392 void nxpwifi_shutdown_sw(struct nxpwifi_adapter *adapter)
1393 {
1394 struct nxpwifi_private *priv;
1395
1396 if (!adapter)
1397 return;
1398
1399 wait_for_completion(adapter->fw_done);
1400 /* Caller should ensure we aren't suspending while this happens */
1401 reinit_completion(adapter->fw_done);
1402
1403 priv = nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_ANY);
1404 nxpwifi_deauthenticate(priv, NULL);
1405
1406 nxpwifi_init_shutdown_fw(priv, NXPWIFI_FUNC_SHUTDOWN);
1407
1408 nxpwifi_uninit_sw(adapter);
1409 adapter->is_up = false;
1410 }
1411 EXPORT_SYMBOL_GPL(nxpwifi_shutdown_sw);
1412
1413 /* Re-init adapter SW and bring device up. */
1414 int
nxpwifi_reinit_sw(struct nxpwifi_adapter * adapter)1415 nxpwifi_reinit_sw(struct nxpwifi_adapter *adapter)
1416 {
1417 int ret = 0;
1418
1419 nxpwifi_init_lock_list(adapter);
1420 if (adapter->if_ops.up_dev)
1421 adapter->if_ops.up_dev(adapter);
1422
1423 adapter->hw_status = NXPWIFI_HW_STATUS_INITIALIZING;
1424 clear_bit(NXPWIFI_SURPRISE_REMOVED, &adapter->work_flags);
1425 clear_bit(NXPWIFI_IS_SUSPENDED, &adapter->work_flags);
1426 adapter->hs_activated = false;
1427 clear_bit(NXPWIFI_IS_CMD_TIMEDOUT, &adapter->work_flags);
1428 init_waitqueue_head(&adapter->hs_activate_wait_q);
1429 init_waitqueue_head(&adapter->cmd_wait_q.wait);
1430 adapter->cmd_wait_q.status = 0;
1431 adapter->scan_wait_q_woken = false;
1432
1433 if (num_possible_cpus() > 1)
1434 adapter->rx_work_enabled = true;
1435
1436 adapter->workqueue =
1437 alloc_workqueue("NXPWIFI_WORK_QUEUE",
1438 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
1439 if (!adapter->workqueue) {
1440 ret = -ENOMEM;
1441 goto err_kmalloc;
1442 }
1443
1444 INIT_WORK(&adapter->main_work, nxpwifi_main_work);
1445
1446 if (adapter->rx_work_enabled) {
1447 adapter->rx_workqueue = alloc_workqueue("NXPWIFI_RX_WORK_QUEUE",
1448 WQ_HIGHPRI |
1449 WQ_MEM_RECLAIM |
1450 WQ_UNBOUND, 0);
1451 if (!adapter->rx_workqueue) {
1452 ret = -ENOMEM;
1453 goto err_kmalloc;
1454 }
1455 INIT_WORK(&adapter->rx_work, nxpwifi_rx_work);
1456 }
1457
1458 wiphy_work_init(&adapter->host_mlme_work, nxpwifi_host_mlme_work);
1459
1460 /*
1461 * Register the device. Fill up the private data structure with
1462 * relevant information from the card. Some code extracted from
1463 * nxpwifi_register_dev()
1464 */
1465 nxpwifi_dbg(adapter, INFO, "%s, nxpwifi_init_hw_fw()...\n", __func__);
1466
1467 ret = nxpwifi_init_hw_fw(adapter, false);
1468 if (ret) {
1469 nxpwifi_dbg(adapter, ERROR,
1470 "%s: firmware init failed\n", __func__);
1471 goto err_init_fw;
1472 }
1473
1474 /* _nxpwifi_fw_dpc() does its own cleanup */
1475 ret = _nxpwifi_fw_dpc(adapter->firmware, adapter);
1476 if (ret) {
1477 pr_err("Failed to bring up adapter: %d\n", ret);
1478 return ret;
1479 }
1480 nxpwifi_dbg(adapter, INFO, "%s, successful\n", __func__);
1481
1482 return ret;
1483
1484 err_init_fw:
1485 nxpwifi_dbg(adapter, ERROR, "info: %s: unregister device\n", __func__);
1486 if (adapter->if_ops.unregister_dev)
1487 adapter->if_ops.unregister_dev(adapter);
1488
1489 err_kmalloc:
1490 set_bit(NXPWIFI_SURPRISE_REMOVED, &adapter->work_flags);
1491 nxpwifi_terminate_workqueue(adapter);
1492 if (adapter->hw_status == NXPWIFI_HW_STATUS_READY) {
1493 nxpwifi_dbg(adapter, ERROR,
1494 "info: %s: shutdown nxpwifi\n", __func__);
1495 nxpwifi_shutdown_drv(adapter);
1496 nxpwifi_free_cmd_buffers(adapter);
1497 }
1498
1499 complete_all(adapter->fw_done);
1500 nxpwifi_dbg(adapter, INFO, "%s, error\n", __func__);
1501
1502 return ret;
1503 }
1504 EXPORT_SYMBOL_GPL(nxpwifi_reinit_sw);
1505
1506 /* Add card: register adapter, workqueues, device; request FW (async). */
1507 int
nxpwifi_add_card(void * card,struct completion * fw_done,struct nxpwifi_if_ops * if_ops,u8 iface_type,struct device * dev)1508 nxpwifi_add_card(void *card, struct completion *fw_done,
1509 struct nxpwifi_if_ops *if_ops, u8 iface_type,
1510 struct device *dev)
1511 {
1512 struct nxpwifi_adapter *adapter;
1513 int ret = 0;
1514
1515 adapter = nxpwifi_register(card, dev, if_ops);
1516 if (IS_ERR(adapter)) {
1517 ret = PTR_ERR(adapter);
1518 pr_err("%s: adapter register failed %d\n", __func__, ret);
1519 goto err_init_sw;
1520 }
1521
1522 adapter->iface_type = iface_type;
1523 adapter->fw_done = fw_done;
1524
1525 adapter->hw_status = NXPWIFI_HW_STATUS_INITIALIZING;
1526 clear_bit(NXPWIFI_SURPRISE_REMOVED, &adapter->work_flags);
1527 clear_bit(NXPWIFI_IS_SUSPENDED, &adapter->work_flags);
1528 adapter->hs_activated = false;
1529 init_waitqueue_head(&adapter->hs_activate_wait_q);
1530 init_waitqueue_head(&adapter->cmd_wait_q.wait);
1531 adapter->cmd_wait_q.status = 0;
1532 adapter->scan_wait_q_woken = false;
1533
1534 if (num_possible_cpus() > 1)
1535 adapter->rx_work_enabled = true;
1536
1537 adapter->workqueue =
1538 alloc_workqueue("NXPWIFI_WORK_QUEUE",
1539 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
1540 if (!adapter->workqueue) {
1541 ret = -ENOMEM;
1542 goto err_kmalloc;
1543 }
1544
1545 INIT_WORK(&adapter->main_work, nxpwifi_main_work);
1546
1547 if (adapter->rx_work_enabled) {
1548 adapter->rx_workqueue = alloc_workqueue("NXPWIFI_RX_WORK_QUEUE",
1549 WQ_HIGHPRI |
1550 WQ_MEM_RECLAIM |
1551 WQ_UNBOUND, 0);
1552 if (!adapter->rx_workqueue) {
1553 ret = -ENOMEM;
1554 goto err_kmalloc;
1555 }
1556
1557 INIT_WORK(&adapter->rx_work, nxpwifi_rx_work);
1558 }
1559
1560 wiphy_work_init(&adapter->host_mlme_work, nxpwifi_host_mlme_work);
1561
1562 /*
1563 * Register the device. Fill up the private data structure with relevant
1564 * information from the card.
1565 */
1566 ret = adapter->if_ops.register_dev(adapter);
1567 if (ret) {
1568 pr_err("%s: failed to register nxpwifi device\n", __func__);
1569 goto err_registerdev;
1570 }
1571
1572 ret = nxpwifi_init_hw_fw(adapter, true);
1573 if (ret) {
1574 pr_err("%s: firmware init failed\n", __func__);
1575 goto err_init_fw;
1576 }
1577
1578 return ret;
1579
1580 err_init_fw:
1581 pr_debug("info: %s: unregister device\n", __func__);
1582 if (adapter->if_ops.unregister_dev)
1583 adapter->if_ops.unregister_dev(adapter);
1584 err_registerdev:
1585 set_bit(NXPWIFI_SURPRISE_REMOVED, &adapter->work_flags);
1586
1587 err_kmalloc:
1588 nxpwifi_terminate_workqueue(adapter);
1589
1590 if (adapter->hw_status == NXPWIFI_HW_STATUS_READY) {
1591 pr_debug("info: %s: shutdown nxpwifi\n", __func__);
1592 nxpwifi_shutdown_drv(adapter);
1593 nxpwifi_free_cmd_buffers(adapter);
1594 }
1595
1596 nxpwifi_free_adapter(adapter);
1597
1598 err_init_sw:
1599
1600 return ret;
1601 }
1602 EXPORT_SYMBOL_GPL(nxpwifi_add_card);
1603
1604 /* Remove card: teardown SW, unregister device, free adapter. */
nxpwifi_remove_card(struct nxpwifi_adapter * adapter)1605 void nxpwifi_remove_card(struct nxpwifi_adapter *adapter)
1606 {
1607 if (!adapter)
1608 return;
1609
1610 if (adapter->is_up)
1611 nxpwifi_uninit_sw(adapter);
1612
1613 /* Unregister device */
1614 nxpwifi_dbg(adapter, INFO,
1615 "info: unregister device\n");
1616 if (adapter->if_ops.unregister_dev)
1617 adapter->if_ops.unregister_dev(adapter);
1618 /* Free adapter structure */
1619 nxpwifi_dbg(adapter, INFO,
1620 "info: free adapter\n");
1621 nxpwifi_free_adapter(adapter);
1622 }
1623 EXPORT_SYMBOL_GPL(nxpwifi_remove_card);
1624
_nxpwifi_dbg(const struct nxpwifi_adapter * adapter,int mask,const char * fmt,...)1625 void _nxpwifi_dbg(const struct nxpwifi_adapter *adapter, int mask,
1626 const char *fmt, ...)
1627 {
1628 struct va_format vaf;
1629 va_list args;
1630
1631 if (!(adapter->debug_mask & mask))
1632 return;
1633
1634 va_start(args, fmt);
1635
1636 vaf.fmt = fmt;
1637 vaf.va = &args;
1638
1639 if (adapter->dev)
1640 dev_info(adapter->dev, "%pV", &vaf);
1641 else
1642 pr_info("%pV", &vaf);
1643
1644 va_end(args);
1645 }
1646 EXPORT_SYMBOL_GPL(_nxpwifi_dbg);
1647
1648 /* Module init: init debugfs if enabled. */
1649 static int
nxpwifi_init_module(void)1650 nxpwifi_init_module(void)
1651 {
1652 #ifdef CONFIG_DEBUG_FS
1653 nxpwifi_debugfs_init();
1654 #endif
1655 return 0;
1656 }
1657
1658 /* Module exit: remove debugfs if enabled. */
1659 static void
nxpwifi_cleanup_module(void)1660 nxpwifi_cleanup_module(void)
1661 {
1662 #ifdef CONFIG_DEBUG_FS
1663 nxpwifi_debugfs_remove();
1664 #endif
1665 }
1666
1667 module_init(nxpwifi_init_module);
1668 module_exit(nxpwifi_cleanup_module);
1669
1670 MODULE_AUTHOR("NXP International Ltd.");
1671 MODULE_DESCRIPTION("NXP WiFi Driver version " VERSION);
1672 MODULE_VERSION(VERSION);
1673 MODULE_LICENSE("GPL");
1674