1 // SPDX-License-Identifier: GPL-2.0
3 * This file contains the handling of command
4 * responses as well as events generated by firmware.
7 #include <linux/hardirq.h>
8 #include <linux/slab.h>
9 #include <linux/delay.h>
10 #include <linux/sched.h>
11 #include <asm/unaligned.h>
12 #include <net/cfg80211.h>
18 * lbs_mac_event_disconnected - handles disconnect event. It
19 * reports disconnect to upper layer, clean tx/rx packets,
20 * reset link state etc.
22 * @priv: A pointer to struct lbs_private structure
23 * @locally_generated: indicates disconnect was requested locally
24 * (usually by userspace)
28 void lbs_mac_event_disconnected(struct lbs_private
*priv
,
29 bool locally_generated
)
33 if (priv
->connect_status
!= LBS_CONNECTED
)
37 * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
38 * It causes problem in the Supplicant
40 msleep_interruptible(1000);
42 if (priv
->wdev
->iftype
== NL80211_IFTYPE_STATION
)
43 lbs_send_disconnect_notification(priv
, locally_generated
);
45 /* report disconnect to upper layer */
46 netif_stop_queue(priv
->dev
);
47 netif_carrier_off(priv
->dev
);
49 /* Free Tx and Rx packets */
50 spin_lock_irqsave(&priv
->driver_lock
, flags
);
51 kfree_skb(priv
->currenttxskb
);
52 priv
->currenttxskb
= NULL
;
53 priv
->tx_pending_len
= 0;
54 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
56 priv
->connect_status
= LBS_DISCONNECTED
;
58 if (priv
->psstate
!= PS_STATE_FULL_POWER
) {
59 /* make firmware to exit PS mode */
60 lbs_deb_cmd("disconnected, so exit PS mode\n");
61 lbs_set_ps_mode(priv
, PS_MODE_ACTION_EXIT_PS
, false);
65 int lbs_process_command_response(struct lbs_private
*priv
, u8
*data
, u32 len
)
67 uint16_t respcmd
, curcmd
;
68 struct cmd_header
*resp
;
73 mutex_lock(&priv
->lock
);
74 spin_lock_irqsave(&priv
->driver_lock
, flags
);
77 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
79 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
84 curcmd
= le16_to_cpu(priv
->cur_cmd
->cmdbuf
->command
);
85 respcmd
= le16_to_cpu(resp
->command
);
86 result
= le16_to_cpu(resp
->result
);
88 lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
89 respcmd
, le16_to_cpu(resp
->seqnum
), len
);
90 lbs_deb_hex(LBS_DEB_CMD
, "CMD_RESP", (void *) resp
, len
);
92 if (resp
->seqnum
!= priv
->cur_cmd
->cmdbuf
->seqnum
) {
93 netdev_info(priv
->dev
,
94 "Received CMD_RESP with invalid sequence %d (expected %d)\n",
95 le16_to_cpu(resp
->seqnum
),
96 le16_to_cpu(priv
->cur_cmd
->cmdbuf
->seqnum
));
97 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
101 if (respcmd
!= CMD_RET(curcmd
) &&
102 respcmd
!= CMD_RET_802_11_ASSOCIATE
&& curcmd
!= CMD_802_11_ASSOCIATE
) {
103 netdev_info(priv
->dev
, "Invalid CMD_RESP %x to command %x!\n",
105 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
110 if (resp
->result
== cpu_to_le16(0x0004)) {
111 /* 0x0004 means -EAGAIN. Drop the response, let it time out
112 and be resubmitted */
113 netdev_info(priv
->dev
,
114 "Firmware returns DEFER to command %x. Will let it time out...\n",
115 le16_to_cpu(resp
->command
));
116 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
121 /* Now we got response from FW, cancel the command timer */
122 del_timer(&priv
->command_timer
);
123 priv
->cmd_timed_out
= 0;
125 if (respcmd
== CMD_RET(CMD_802_11_PS_MODE
)) {
126 /* struct cmd_ds_802_11_ps_mode also contains
129 struct cmd_ds_802_11_ps_mode
*psmode
= (void *)resp
;
130 u16 action
= le16_to_cpu(psmode
->action
);
133 "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
137 lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
140 * We should not re-try enter-ps command in
141 * ad-hoc mode. It takes place in
142 * lbs_execute_next_command().
144 if (priv
->wdev
->iftype
== NL80211_IFTYPE_MONITOR
&&
145 action
== PS_MODE_ACTION_ENTER_PS
)
146 priv
->psmode
= LBS802_11POWERMODECAM
;
147 } else if (action
== PS_MODE_ACTION_ENTER_PS
) {
148 priv
->needtowakeup
= 0;
149 priv
->psstate
= PS_STATE_AWAKE
;
151 lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
152 if (priv
->connect_status
!= LBS_CONNECTED
) {
154 * When Deauth Event received before Enter_PS command
155 * response, We need to wake up the firmware.
158 "disconnected, invoking lbs_ps_wakeup\n");
160 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
161 mutex_unlock(&priv
->lock
);
162 lbs_set_ps_mode(priv
, PS_MODE_ACTION_EXIT_PS
,
164 mutex_lock(&priv
->lock
);
165 spin_lock_irqsave(&priv
->driver_lock
, flags
);
167 } else if (action
== PS_MODE_ACTION_EXIT_PS
) {
168 priv
->needtowakeup
= 0;
169 priv
->psstate
= PS_STATE_FULL_POWER
;
170 lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
172 lbs_deb_host("CMD_RESP: PS action 0x%X\n", action
);
175 __lbs_complete_command(priv
, priv
->cur_cmd
, result
);
176 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
182 /* If the command is not successful, cleanup and return failure */
183 if ((result
!= 0 || !(respcmd
& 0x8000))) {
184 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
187 * Handling errors here
190 case CMD_RET(CMD_GET_HW_SPEC
):
191 case CMD_RET(CMD_802_11_RESET
):
192 lbs_deb_host("CMD_RESP: reset failed\n");
196 __lbs_complete_command(priv
, priv
->cur_cmd
, result
);
197 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
203 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
205 if (priv
->cur_cmd
&& priv
->cur_cmd
->callback
) {
206 ret
= priv
->cur_cmd
->callback(priv
, priv
->cur_cmd
->callback_arg
,
210 spin_lock_irqsave(&priv
->driver_lock
, flags
);
213 /* Clean up and Put current command back to cmdfreeq */
214 __lbs_complete_command(priv
, priv
->cur_cmd
, result
);
216 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
219 mutex_unlock(&priv
->lock
);
223 void lbs_process_event(struct lbs_private
*priv
, u32 event
)
225 struct cmd_header cmd
;
228 case MACREG_INT_CODE_LINK_SENSED
:
229 lbs_deb_cmd("EVENT: link sensed\n");
232 case MACREG_INT_CODE_DEAUTHENTICATED
:
233 lbs_deb_cmd("EVENT: deauthenticated\n");
234 lbs_mac_event_disconnected(priv
, false);
237 case MACREG_INT_CODE_DISASSOCIATED
:
238 lbs_deb_cmd("EVENT: disassociated\n");
239 lbs_mac_event_disconnected(priv
, false);
242 case MACREG_INT_CODE_LINK_LOST_NO_SCAN
:
243 lbs_deb_cmd("EVENT: link lost\n");
244 lbs_mac_event_disconnected(priv
, true);
247 case MACREG_INT_CODE_PS_SLEEP
:
248 lbs_deb_cmd("EVENT: ps sleep\n");
250 /* handle unexpected PS SLEEP event */
251 if (priv
->psstate
== PS_STATE_FULL_POWER
) {
253 "EVENT: in FULL POWER mode, ignoring PS_SLEEP\n");
256 if (!list_empty(&priv
->cmdpendingq
)) {
257 lbs_deb_cmd("EVENT: commands in queue, do not sleep\n");
260 priv
->psstate
= PS_STATE_PRE_SLEEP
;
262 lbs_ps_confirm_sleep(priv
);
266 case MACREG_INT_CODE_HOST_AWAKE
:
267 lbs_deb_cmd("EVENT: host awake\n");
268 if (priv
->reset_deep_sleep_wakeup
)
269 priv
->reset_deep_sleep_wakeup(priv
);
270 priv
->is_deep_sleep
= 0;
271 lbs_cmd_async(priv
, CMD_802_11_WAKEUP_CONFIRM
, &cmd
,
273 priv
->is_host_sleep_activated
= 0;
274 wake_up_interruptible(&priv
->host_sleep_q
);
277 case MACREG_INT_CODE_DEEP_SLEEP_AWAKE
:
278 if (priv
->reset_deep_sleep_wakeup
)
279 priv
->reset_deep_sleep_wakeup(priv
);
280 lbs_deb_cmd("EVENT: ds awake\n");
281 priv
->is_deep_sleep
= 0;
282 priv
->wakeup_dev_required
= 0;
283 wake_up_interruptible(&priv
->ds_awake_q
);
286 case MACREG_INT_CODE_PS_AWAKE
:
287 lbs_deb_cmd("EVENT: ps awake\n");
288 /* handle unexpected PS AWAKE event */
289 if (priv
->psstate
== PS_STATE_FULL_POWER
) {
291 "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
295 priv
->psstate
= PS_STATE_AWAKE
;
297 if (priv
->needtowakeup
) {
299 * wait for the command processing to finish
300 * before resuming sending
301 * priv->needtowakeup will be set to FALSE
304 lbs_deb_cmd("waking up ...\n");
305 lbs_set_ps_mode(priv
, PS_MODE_ACTION_EXIT_PS
, false);
309 case MACREG_INT_CODE_MIC_ERR_UNICAST
:
310 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
311 lbs_send_mic_failureevent(priv
, event
);
314 case MACREG_INT_CODE_MIC_ERR_MULTICAST
:
315 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
316 lbs_send_mic_failureevent(priv
, event
);
319 case MACREG_INT_CODE_MIB_CHANGED
:
320 lbs_deb_cmd("EVENT: MIB CHANGED\n");
322 case MACREG_INT_CODE_INIT_DONE
:
323 lbs_deb_cmd("EVENT: INIT DONE\n");
325 case MACREG_INT_CODE_ADHOC_BCN_LOST
:
326 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
328 case MACREG_INT_CODE_RSSI_LOW
:
329 netdev_alert(priv
->dev
, "EVENT: rssi low\n");
331 case MACREG_INT_CODE_SNR_LOW
:
332 netdev_alert(priv
->dev
, "EVENT: snr low\n");
334 case MACREG_INT_CODE_MAX_FAIL
:
335 netdev_alert(priv
->dev
, "EVENT: max fail\n");
337 case MACREG_INT_CODE_RSSI_HIGH
:
338 netdev_alert(priv
->dev
, "EVENT: rssi high\n");
340 case MACREG_INT_CODE_SNR_HIGH
:
341 netdev_alert(priv
->dev
, "EVENT: snr high\n");
344 case MACREG_INT_CODE_MESH_AUTO_STARTED
:
345 /* Ignore spurious autostart events */
346 netdev_info(priv
->dev
, "EVENT: MESH_AUTO_STARTED (ignoring)\n");
350 netdev_alert(priv
->dev
, "EVENT: unknown event id %d\n", event
);