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 int lbs_process_event(struct lbs_private
*priv
, u32 event
)
226 struct cmd_header cmd
;
229 case MACREG_INT_CODE_LINK_SENSED
:
230 lbs_deb_cmd("EVENT: link sensed\n");
233 case MACREG_INT_CODE_DEAUTHENTICATED
:
234 lbs_deb_cmd("EVENT: deauthenticated\n");
235 lbs_mac_event_disconnected(priv
, false);
238 case MACREG_INT_CODE_DISASSOCIATED
:
239 lbs_deb_cmd("EVENT: disassociated\n");
240 lbs_mac_event_disconnected(priv
, false);
243 case MACREG_INT_CODE_LINK_LOST_NO_SCAN
:
244 lbs_deb_cmd("EVENT: link lost\n");
245 lbs_mac_event_disconnected(priv
, true);
248 case MACREG_INT_CODE_PS_SLEEP
:
249 lbs_deb_cmd("EVENT: ps sleep\n");
251 /* handle unexpected PS SLEEP event */
252 if (priv
->psstate
== PS_STATE_FULL_POWER
) {
254 "EVENT: in FULL POWER mode, ignoring PS_SLEEP\n");
257 if (!list_empty(&priv
->cmdpendingq
)) {
258 lbs_deb_cmd("EVENT: commands in queue, do not sleep\n");
261 priv
->psstate
= PS_STATE_PRE_SLEEP
;
263 lbs_ps_confirm_sleep(priv
);
267 case MACREG_INT_CODE_HOST_AWAKE
:
268 lbs_deb_cmd("EVENT: host awake\n");
269 if (priv
->reset_deep_sleep_wakeup
)
270 priv
->reset_deep_sleep_wakeup(priv
);
271 priv
->is_deep_sleep
= 0;
272 lbs_cmd_async(priv
, CMD_802_11_WAKEUP_CONFIRM
, &cmd
,
274 priv
->is_host_sleep_activated
= 0;
275 wake_up_interruptible(&priv
->host_sleep_q
);
278 case MACREG_INT_CODE_DEEP_SLEEP_AWAKE
:
279 if (priv
->reset_deep_sleep_wakeup
)
280 priv
->reset_deep_sleep_wakeup(priv
);
281 lbs_deb_cmd("EVENT: ds awake\n");
282 priv
->is_deep_sleep
= 0;
283 priv
->wakeup_dev_required
= 0;
284 wake_up_interruptible(&priv
->ds_awake_q
);
287 case MACREG_INT_CODE_PS_AWAKE
:
288 lbs_deb_cmd("EVENT: ps awake\n");
289 /* handle unexpected PS AWAKE event */
290 if (priv
->psstate
== PS_STATE_FULL_POWER
) {
292 "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
296 priv
->psstate
= PS_STATE_AWAKE
;
298 if (priv
->needtowakeup
) {
300 * wait for the command processing to finish
301 * before resuming sending
302 * priv->needtowakeup will be set to FALSE
305 lbs_deb_cmd("waking up ...\n");
306 lbs_set_ps_mode(priv
, PS_MODE_ACTION_EXIT_PS
, false);
310 case MACREG_INT_CODE_MIC_ERR_UNICAST
:
311 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
312 lbs_send_mic_failureevent(priv
, event
);
315 case MACREG_INT_CODE_MIC_ERR_MULTICAST
:
316 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
317 lbs_send_mic_failureevent(priv
, event
);
320 case MACREG_INT_CODE_MIB_CHANGED
:
321 lbs_deb_cmd("EVENT: MIB CHANGED\n");
323 case MACREG_INT_CODE_INIT_DONE
:
324 lbs_deb_cmd("EVENT: INIT DONE\n");
326 case MACREG_INT_CODE_ADHOC_BCN_LOST
:
327 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
329 case MACREG_INT_CODE_RSSI_LOW
:
330 netdev_alert(priv
->dev
, "EVENT: rssi low\n");
332 case MACREG_INT_CODE_SNR_LOW
:
333 netdev_alert(priv
->dev
, "EVENT: snr low\n");
335 case MACREG_INT_CODE_MAX_FAIL
:
336 netdev_alert(priv
->dev
, "EVENT: max fail\n");
338 case MACREG_INT_CODE_RSSI_HIGH
:
339 netdev_alert(priv
->dev
, "EVENT: rssi high\n");
341 case MACREG_INT_CODE_SNR_HIGH
:
342 netdev_alert(priv
->dev
, "EVENT: snr high\n");
345 case MACREG_INT_CODE_MESH_AUTO_STARTED
:
346 /* Ignore spurious autostart events */
347 netdev_info(priv
->dev
, "EVENT: MESH_AUTO_STARTED (ignoring)\n");
351 netdev_alert(priv
->dev
, "EVENT: unknown event id %d\n", event
);