xref: /linux/drivers/net/wireless/marvell/libertas/cmdresp.c (revision 0883c2c06fb5bcf5b9e008270827e63c09a88c1e)
1 /*
2  * This file contains the handling of command
3  * responses as well as events generated by firmware.
4  */
5 
6 #include <linux/hardirq.h>
7 #include <linux/slab.h>
8 #include <linux/delay.h>
9 #include <linux/sched.h>
10 #include <asm/unaligned.h>
11 #include <net/cfg80211.h>
12 
13 #include "cfg.h"
14 #include "cmd.h"
15 
16 /**
17  * lbs_mac_event_disconnected - handles disconnect event. It
18  * reports disconnect to upper layer, clean tx/rx packets,
19  * reset link state etc.
20  *
21  * @priv:	A pointer to struct lbs_private structure
22  * @locally_generated: indicates disconnect was requested locally
23  *		(usually by userspace)
24  *
25  * returns:	n/a
26  */
27 void lbs_mac_event_disconnected(struct lbs_private *priv,
28 				bool locally_generated)
29 {
30 	if (priv->connect_status != LBS_CONNECTED)
31 		return;
32 
33 	lbs_deb_enter(LBS_DEB_ASSOC);
34 
35 	/*
36 	 * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
37 	 * It causes problem in the Supplicant
38 	 */
39 	msleep_interruptible(1000);
40 
41 	if (priv->wdev->iftype == NL80211_IFTYPE_STATION)
42 		lbs_send_disconnect_notification(priv, locally_generated);
43 
44 	/* report disconnect to upper layer */
45 	netif_stop_queue(priv->dev);
46 	netif_carrier_off(priv->dev);
47 
48 	/* Free Tx and Rx packets */
49 	kfree_skb(priv->currenttxskb);
50 	priv->currenttxskb = NULL;
51 	priv->tx_pending_len = 0;
52 
53 	priv->connect_status = LBS_DISCONNECTED;
54 
55 	if (priv->psstate != PS_STATE_FULL_POWER) {
56 		/* make firmware to exit PS mode */
57 		lbs_deb_cmd("disconnected, so exit PS mode\n");
58 		lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false);
59 	}
60 	lbs_deb_leave(LBS_DEB_ASSOC);
61 }
62 
63 int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
64 {
65 	uint16_t respcmd, curcmd;
66 	struct cmd_header *resp;
67 	int ret = 0;
68 	unsigned long flags;
69 	uint16_t result;
70 
71 	lbs_deb_enter(LBS_DEB_HOST);
72 
73 	mutex_lock(&priv->lock);
74 	spin_lock_irqsave(&priv->driver_lock, flags);
75 
76 	if (!priv->cur_cmd) {
77 		lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
78 		ret = -1;
79 		spin_unlock_irqrestore(&priv->driver_lock, flags);
80 		goto done;
81 	}
82 
83 	resp = (void *)data;
84 	curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
85 	respcmd = le16_to_cpu(resp->command);
86 	result = le16_to_cpu(resp->result);
87 
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);
91 
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);
98 		ret = -1;
99 		goto done;
100 	}
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",
104 			    respcmd, curcmd);
105 		spin_unlock_irqrestore(&priv->driver_lock, flags);
106 		ret = -1;
107 		goto done;
108 	}
109 
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);
117 		ret = -1;
118 		goto done;
119 	}
120 
121 	/* Now we got response from FW, cancel the command timer */
122 	del_timer(&priv->command_timer);
123 	priv->cmd_timed_out = 0;
124 
125 	if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
126 		/* struct cmd_ds_802_11_ps_mode also contains
127 		 * the header
128 		 */
129 		struct cmd_ds_802_11_ps_mode *psmode = (void *)resp;
130 		u16 action = le16_to_cpu(psmode->action);
131 
132 		lbs_deb_host(
133 		       "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
134 		       result, action);
135 
136 		if (result) {
137 			lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
138 				    result);
139 			/*
140 			 * We should not re-try enter-ps command in
141 			 * ad-hoc mode. It takes place in
142 			 * lbs_execute_next_command().
143 			 */
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;
150 
151 			lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
152 			if (priv->connect_status != LBS_CONNECTED) {
153 				/*
154 				 * When Deauth Event received before Enter_PS command
155 				 * response, We need to wake up the firmware.
156 				 */
157 				lbs_deb_host(
158 				       "disconnected, invoking lbs_ps_wakeup\n");
159 
160 				spin_unlock_irqrestore(&priv->driver_lock, flags);
161 				mutex_unlock(&priv->lock);
162 				lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS,
163 						false);
164 				mutex_lock(&priv->lock);
165 				spin_lock_irqsave(&priv->driver_lock, flags);
166 			}
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");
171 		} else {
172 			lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
173 		}
174 
175 		__lbs_complete_command(priv, priv->cur_cmd, result);
176 		spin_unlock_irqrestore(&priv->driver_lock, flags);
177 
178 		ret = 0;
179 		goto done;
180 	}
181 
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",
185 		       result, respcmd);
186 		/*
187 		 * Handling errors here
188 		 */
189 		switch (respcmd) {
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");
193 			break;
194 
195 		}
196 		__lbs_complete_command(priv, priv->cur_cmd, result);
197 		spin_unlock_irqrestore(&priv->driver_lock, flags);
198 
199 		ret = -1;
200 		goto done;
201 	}
202 
203 	spin_unlock_irqrestore(&priv->driver_lock, flags);
204 
205 	if (priv->cur_cmd && priv->cur_cmd->callback) {
206 		ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
207 				resp);
208 	}
209 
210 	spin_lock_irqsave(&priv->driver_lock, flags);
211 
212 	if (priv->cur_cmd) {
213 		/* Clean up and Put current command back to cmdfreeq */
214 		__lbs_complete_command(priv, priv->cur_cmd, result);
215 	}
216 	spin_unlock_irqrestore(&priv->driver_lock, flags);
217 
218 done:
219 	mutex_unlock(&priv->lock);
220 	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
221 	return ret;
222 }
223 
224 int lbs_process_event(struct lbs_private *priv, u32 event)
225 {
226 	int ret = 0;
227 	struct cmd_header cmd;
228 
229 	lbs_deb_enter(LBS_DEB_CMD);
230 
231 	switch (event) {
232 	case MACREG_INT_CODE_LINK_SENSED:
233 		lbs_deb_cmd("EVENT: link sensed\n");
234 		break;
235 
236 	case MACREG_INT_CODE_DEAUTHENTICATED:
237 		lbs_deb_cmd("EVENT: deauthenticated\n");
238 		lbs_mac_event_disconnected(priv, false);
239 		break;
240 
241 	case MACREG_INT_CODE_DISASSOCIATED:
242 		lbs_deb_cmd("EVENT: disassociated\n");
243 		lbs_mac_event_disconnected(priv, false);
244 		break;
245 
246 	case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
247 		lbs_deb_cmd("EVENT: link lost\n");
248 		lbs_mac_event_disconnected(priv, true);
249 		break;
250 
251 	case MACREG_INT_CODE_PS_SLEEP:
252 		lbs_deb_cmd("EVENT: ps sleep\n");
253 
254 		/* handle unexpected PS SLEEP event */
255 		if (priv->psstate == PS_STATE_FULL_POWER) {
256 			lbs_deb_cmd(
257 			       "EVENT: in FULL POWER mode, ignoring PS_SLEEP\n");
258 			break;
259 		}
260 		if (!list_empty(&priv->cmdpendingq)) {
261 			lbs_deb_cmd("EVENT: commands in queue, do not sleep\n");
262 			break;
263 		}
264 		priv->psstate = PS_STATE_PRE_SLEEP;
265 
266 		lbs_ps_confirm_sleep(priv);
267 
268 		break;
269 
270 	case MACREG_INT_CODE_HOST_AWAKE:
271 		lbs_deb_cmd("EVENT: host awake\n");
272 		if (priv->reset_deep_sleep_wakeup)
273 			priv->reset_deep_sleep_wakeup(priv);
274 		priv->is_deep_sleep = 0;
275 		lbs_cmd_async(priv, CMD_802_11_WAKEUP_CONFIRM, &cmd,
276 				sizeof(cmd));
277 		priv->is_host_sleep_activated = 0;
278 		wake_up_interruptible(&priv->host_sleep_q);
279 		break;
280 
281 	case MACREG_INT_CODE_DEEP_SLEEP_AWAKE:
282 		if (priv->reset_deep_sleep_wakeup)
283 			priv->reset_deep_sleep_wakeup(priv);
284 		lbs_deb_cmd("EVENT: ds awake\n");
285 		priv->is_deep_sleep = 0;
286 		priv->wakeup_dev_required = 0;
287 		wake_up_interruptible(&priv->ds_awake_q);
288 		break;
289 
290 	case MACREG_INT_CODE_PS_AWAKE:
291 		lbs_deb_cmd("EVENT: ps awake\n");
292 		/* handle unexpected PS AWAKE event */
293 		if (priv->psstate == PS_STATE_FULL_POWER) {
294 			lbs_deb_cmd(
295 			       "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
296 			break;
297 		}
298 
299 		priv->psstate = PS_STATE_AWAKE;
300 
301 		if (priv->needtowakeup) {
302 			/*
303 			 * wait for the command processing to finish
304 			 * before resuming sending
305 			 * priv->needtowakeup will be set to FALSE
306 			 * in lbs_ps_wakeup()
307 			 */
308 			lbs_deb_cmd("waking up ...\n");
309 			lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false);
310 		}
311 		break;
312 
313 	case MACREG_INT_CODE_MIC_ERR_UNICAST:
314 		lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
315 		lbs_send_mic_failureevent(priv, event);
316 		break;
317 
318 	case MACREG_INT_CODE_MIC_ERR_MULTICAST:
319 		lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
320 		lbs_send_mic_failureevent(priv, event);
321 		break;
322 
323 	case MACREG_INT_CODE_MIB_CHANGED:
324 		lbs_deb_cmd("EVENT: MIB CHANGED\n");
325 		break;
326 	case MACREG_INT_CODE_INIT_DONE:
327 		lbs_deb_cmd("EVENT: INIT DONE\n");
328 		break;
329 	case MACREG_INT_CODE_ADHOC_BCN_LOST:
330 		lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
331 		break;
332 	case MACREG_INT_CODE_RSSI_LOW:
333 		netdev_alert(priv->dev, "EVENT: rssi low\n");
334 		break;
335 	case MACREG_INT_CODE_SNR_LOW:
336 		netdev_alert(priv->dev, "EVENT: snr low\n");
337 		break;
338 	case MACREG_INT_CODE_MAX_FAIL:
339 		netdev_alert(priv->dev, "EVENT: max fail\n");
340 		break;
341 	case MACREG_INT_CODE_RSSI_HIGH:
342 		netdev_alert(priv->dev, "EVENT: rssi high\n");
343 		break;
344 	case MACREG_INT_CODE_SNR_HIGH:
345 		netdev_alert(priv->dev, "EVENT: snr high\n");
346 		break;
347 
348 	case MACREG_INT_CODE_MESH_AUTO_STARTED:
349 		/* Ignore spurious autostart events */
350 		netdev_info(priv->dev, "EVENT: MESH_AUTO_STARTED (ignoring)\n");
351 		break;
352 
353 	default:
354 		netdev_alert(priv->dev, "EVENT: unknown event id %d\n", event);
355 		break;
356 	}
357 
358 	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
359 	return ret;
360 }
361