2 * This file contains the handling of command
3 * responses as well as events generated by firmware.
5 #include <linux/delay.h>
6 #include <linux/if_arp.h>
7 #include <linux/netdevice.h>
9 #include <net/iw_handler.h>
19 * @brief This function handles disconnect event. it
20 * reports disconnect to upper layer, clean tx/rx packets,
21 * reset link state etc.
23 * @param priv A pointer to struct lbs_private structure
26 void lbs_mac_event_disconnected(struct lbs_private *priv)
28 union iwreq_data wrqu;
30 if (priv->connect_status != LBS_CONNECTED)
33 lbs_deb_enter(LBS_DEB_ASSOC);
35 memset(wrqu.ap_addr.sa_data, 0x00, ETH_ALEN);
36 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
39 * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
40 * It causes problem in the Supplicant
43 msleep_interruptible(1000);
44 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
46 /* report disconnect to upper layer */
47 netif_stop_queue(priv->dev);
48 netif_carrier_off(priv->dev);
50 /* Free Tx and Rx packets */
51 kfree_skb(priv->currenttxskb);
52 priv->currenttxskb = NULL;
53 priv->tx_pending_len = 0;
55 /* reset SNR/NF/RSSI values */
56 memset(priv->SNR, 0x00, sizeof(priv->SNR));
57 memset(priv->NF, 0x00, sizeof(priv->NF));
58 memset(priv->RSSI, 0x00, sizeof(priv->RSSI));
59 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
60 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
63 priv->connect_status = LBS_DISCONNECTED;
65 /* Clear out associated SSID and BSSID since connection is
68 memset(&priv->curbssparams.bssid, 0, ETH_ALEN);
69 memset(&priv->curbssparams.ssid, 0, IW_ESSID_MAX_SIZE);
70 priv->curbssparams.ssid_len = 0;
72 if (priv->psstate != PS_STATE_FULL_POWER) {
73 /* make firmware to exit PS mode */
74 lbs_deb_cmd("disconnected, so exit PS mode\n");
75 lbs_ps_wakeup(priv, 0);
77 lbs_deb_leave(LBS_DEB_ASSOC);
81 * @brief This function handles MIC failure event.
83 * @param priv A pointer to struct lbs_private structure
84 * @para event the event id
87 static void handle_mic_failureevent(struct lbs_private *priv, u32 event)
91 lbs_deb_enter(LBS_DEB_CMD);
92 memset(buf, 0, sizeof(buf));
94 sprintf(buf, "%s", "MLME-MICHAELMICFAILURE.indication ");
96 if (event == MACREG_INT_CODE_MIC_ERR_UNICAST) {
97 strcat(buf, "unicast ");
99 strcat(buf, "multicast ");
102 lbs_send_iwevcustom_event(priv, buf);
103 lbs_deb_leave(LBS_DEB_CMD);
106 static int lbs_ret_reg_access(struct lbs_private *priv,
107 u16 type, struct cmd_ds_command *resp)
111 lbs_deb_enter(LBS_DEB_CMD);
114 case CMD_RET(CMD_MAC_REG_ACCESS):
116 struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
118 priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
119 priv->offsetvalue.value = le32_to_cpu(reg->value);
123 case CMD_RET(CMD_BBP_REG_ACCESS):
125 struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
127 priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
128 priv->offsetvalue.value = reg->value;
132 case CMD_RET(CMD_RF_REG_ACCESS):
134 struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
136 priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
137 priv->offsetvalue.value = reg->value;
145 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
149 static int lbs_ret_802_11_rssi(struct lbs_private *priv,
150 struct cmd_ds_command *resp)
152 struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;
154 lbs_deb_enter(LBS_DEB_CMD);
156 /* store the non average value */
157 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->SNR);
158 priv->NF[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->noisefloor);
160 priv->SNR[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgSNR);
161 priv->NF[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgnoisefloor);
163 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] =
164 CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_NOAVG],
165 priv->NF[TYPE_BEACON][TYPE_NOAVG]);
167 priv->RSSI[TYPE_BEACON][TYPE_AVG] =
168 CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
169 priv->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);
171 lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
172 priv->RSSI[TYPE_BEACON][TYPE_NOAVG],
173 priv->RSSI[TYPE_BEACON][TYPE_AVG]);
175 lbs_deb_leave(LBS_DEB_CMD);
179 static int lbs_ret_802_11_bcn_ctrl(struct lbs_private * priv,
180 struct cmd_ds_command *resp)
182 struct cmd_ds_802_11_beacon_control *bcn_ctrl =
183 &resp->params.bcn_ctrl;
185 lbs_deb_enter(LBS_DEB_CMD);
187 if (bcn_ctrl->action == CMD_ACT_GET) {
188 priv->beacon_enable = (u8) le16_to_cpu(bcn_ctrl->beacon_enable);
189 priv->beacon_period = le16_to_cpu(bcn_ctrl->beacon_period);
192 lbs_deb_enter(LBS_DEB_CMD);
196 static inline int handle_cmd_response(struct lbs_private *priv,
197 struct cmd_header *cmd_response)
199 struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
202 uint16_t respcmd = le16_to_cpu(resp->command);
204 lbs_deb_enter(LBS_DEB_HOST);
207 case CMD_RET(CMD_MAC_REG_ACCESS):
208 case CMD_RET(CMD_BBP_REG_ACCESS):
209 case CMD_RET(CMD_RF_REG_ACCESS):
210 ret = lbs_ret_reg_access(priv, respcmd, resp);
213 case CMD_RET_802_11_ASSOCIATE:
214 case CMD_RET(CMD_802_11_ASSOCIATE):
215 case CMD_RET(CMD_802_11_REASSOCIATE):
216 ret = lbs_ret_80211_associate(priv, resp);
219 case CMD_RET(CMD_802_11_SET_AFC):
220 case CMD_RET(CMD_802_11_GET_AFC):
221 spin_lock_irqsave(&priv->driver_lock, flags);
222 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.afc,
223 sizeof(struct cmd_ds_802_11_afc));
224 spin_unlock_irqrestore(&priv->driver_lock, flags);
228 case CMD_RET(CMD_802_11_AUTHENTICATE):
229 case CMD_RET(CMD_802_11_BEACON_STOP):
232 case CMD_RET(CMD_802_11_RSSI):
233 ret = lbs_ret_802_11_rssi(priv, resp);
236 case CMD_RET(CMD_802_11D_DOMAIN_INFO):
237 ret = lbs_ret_802_11d_domain_info(resp);
240 case CMD_RET(CMD_802_11_TPC_CFG):
241 spin_lock_irqsave(&priv->driver_lock, flags);
242 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
243 sizeof(struct cmd_ds_802_11_tpc_cfg));
244 spin_unlock_irqrestore(&priv->driver_lock, flags);
246 case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
247 spin_lock_irqsave(&priv->driver_lock, flags);
248 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.ledgpio,
249 sizeof(struct cmd_ds_802_11_led_ctrl));
250 spin_unlock_irqrestore(&priv->driver_lock, flags);
253 case CMD_RET(CMD_GET_TSF):
254 spin_lock_irqsave(&priv->driver_lock, flags);
255 memcpy((void *)priv->cur_cmd->callback_arg,
256 &resp->params.gettsf.tsfvalue, sizeof(u64));
257 spin_unlock_irqrestore(&priv->driver_lock, flags);
259 case CMD_RET(CMD_BT_ACCESS):
260 spin_lock_irqsave(&priv->driver_lock, flags);
261 if (priv->cur_cmd->callback_arg)
262 memcpy((void *)priv->cur_cmd->callback_arg,
263 &resp->params.bt.addr1, 2 * ETH_ALEN);
264 spin_unlock_irqrestore(&priv->driver_lock, flags);
266 case CMD_RET(CMD_FWT_ACCESS):
267 spin_lock_irqsave(&priv->driver_lock, flags);
268 if (priv->cur_cmd->callback_arg)
269 memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
270 sizeof(resp->params.fwt));
271 spin_unlock_irqrestore(&priv->driver_lock, flags);
273 case CMD_RET(CMD_802_11_BEACON_CTRL):
274 ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
278 lbs_pr_err("CMD_RESP: unknown cmd response 0x%04x\n",
279 le16_to_cpu(resp->command));
282 lbs_deb_leave(LBS_DEB_HOST);
286 int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
288 uint16_t respcmd, curcmd;
289 struct cmd_header *resp;
294 lbs_deb_enter(LBS_DEB_HOST);
296 mutex_lock(&priv->lock);
297 spin_lock_irqsave(&priv->driver_lock, flags);
299 if (!priv->cur_cmd) {
300 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
302 spin_unlock_irqrestore(&priv->driver_lock, flags);
307 curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
308 respcmd = le16_to_cpu(resp->command);
309 result = le16_to_cpu(resp->result);
311 lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
312 respcmd, le16_to_cpu(resp->seqnum), len);
313 lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len);
315 if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
316 lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
317 le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
318 spin_unlock_irqrestore(&priv->driver_lock, flags);
322 if (respcmd != CMD_RET(curcmd) &&
323 respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
324 lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
325 spin_unlock_irqrestore(&priv->driver_lock, flags);
330 if (resp->result == cpu_to_le16(0x0004)) {
331 /* 0x0004 means -EAGAIN. Drop the response, let it time out
332 and be resubmitted */
333 lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
334 le16_to_cpu(resp->command));
335 spin_unlock_irqrestore(&priv->driver_lock, flags);
340 /* Now we got response from FW, cancel the command timer */
341 del_timer(&priv->command_timer);
342 priv->cmd_timed_out = 0;
343 if (priv->nr_retries) {
344 lbs_pr_info("Received result %x to command %x after %d retries\n",
345 result, curcmd, priv->nr_retries);
346 priv->nr_retries = 0;
349 /* Store the response code to cur_cmd_retcode. */
350 priv->cur_cmd_retcode = result;
352 if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
353 struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
354 u16 action = le16_to_cpu(psmode->action);
357 "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
361 lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
364 * We should not re-try enter-ps command in
365 * ad-hoc mode. It takes place in
366 * lbs_execute_next_command().
368 if (priv->mode == IW_MODE_ADHOC &&
369 action == CMD_SUBCMD_ENTER_PS)
370 priv->psmode = LBS802_11POWERMODECAM;
371 } else if (action == CMD_SUBCMD_ENTER_PS) {
372 priv->needtowakeup = 0;
373 priv->psstate = PS_STATE_AWAKE;
375 lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
376 if (priv->connect_status != LBS_CONNECTED) {
378 * When Deauth Event received before Enter_PS command
379 * response, We need to wake up the firmware.
382 "disconnected, invoking lbs_ps_wakeup\n");
384 spin_unlock_irqrestore(&priv->driver_lock, flags);
385 mutex_unlock(&priv->lock);
386 lbs_ps_wakeup(priv, 0);
387 mutex_lock(&priv->lock);
388 spin_lock_irqsave(&priv->driver_lock, flags);
390 } else if (action == CMD_SUBCMD_EXIT_PS) {
391 priv->needtowakeup = 0;
392 priv->psstate = PS_STATE_FULL_POWER;
393 lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
395 lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
398 lbs_complete_command(priv, priv->cur_cmd, result);
399 spin_unlock_irqrestore(&priv->driver_lock, flags);
405 /* If the command is not successful, cleanup and return failure */
406 if ((result != 0 || !(respcmd & 0x8000))) {
407 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
410 * Handling errors here
413 case CMD_RET(CMD_GET_HW_SPEC):
414 case CMD_RET(CMD_802_11_RESET):
415 lbs_deb_host("CMD_RESP: reset failed\n");
419 lbs_complete_command(priv, priv->cur_cmd, result);
420 spin_unlock_irqrestore(&priv->driver_lock, flags);
426 spin_unlock_irqrestore(&priv->driver_lock, flags);
428 if (priv->cur_cmd && priv->cur_cmd->callback) {
429 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
432 ret = handle_cmd_response(priv, resp);
434 spin_lock_irqsave(&priv->driver_lock, flags);
437 /* Clean up and Put current command back to cmdfreeq */
438 lbs_complete_command(priv, priv->cur_cmd, result);
440 spin_unlock_irqrestore(&priv->driver_lock, flags);
443 mutex_unlock(&priv->lock);
444 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
448 static int lbs_send_confirmwake(struct lbs_private *priv)
450 struct cmd_header cmd;
453 lbs_deb_enter(LBS_DEB_HOST);
455 cmd.command = cpu_to_le16(CMD_802_11_WAKEUP_CONFIRM);
456 cmd.size = cpu_to_le16(sizeof(cmd));
457 cmd.seqnum = cpu_to_le16(++priv->seqnum);
460 lbs_deb_hex(LBS_DEB_HOST, "wake confirm", (u8 *) &cmd,
463 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &cmd, sizeof(cmd));
465 lbs_pr_alert("SEND_WAKEC_CMD: Host to Card failed for Confirm Wake\n");
467 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
471 int lbs_process_event(struct lbs_private *priv, u32 event)
475 lbs_deb_enter(LBS_DEB_CMD);
478 case MACREG_INT_CODE_LINK_SENSED:
479 lbs_deb_cmd("EVENT: link sensed\n");
482 case MACREG_INT_CODE_DEAUTHENTICATED:
483 lbs_deb_cmd("EVENT: deauthenticated\n");
484 lbs_mac_event_disconnected(priv);
487 case MACREG_INT_CODE_DISASSOCIATED:
488 lbs_deb_cmd("EVENT: disassociated\n");
489 lbs_mac_event_disconnected(priv);
492 case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
493 lbs_deb_cmd("EVENT: link lost\n");
494 lbs_mac_event_disconnected(priv);
497 case MACREG_INT_CODE_PS_SLEEP:
498 lbs_deb_cmd("EVENT: ps sleep\n");
500 /* handle unexpected PS SLEEP event */
501 if (priv->psstate == PS_STATE_FULL_POWER) {
503 "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
506 priv->psstate = PS_STATE_PRE_SLEEP;
508 lbs_ps_confirm_sleep(priv);
512 case MACREG_INT_CODE_HOST_AWAKE:
513 lbs_deb_cmd("EVENT: host awake\n");
514 lbs_send_confirmwake(priv);
517 case MACREG_INT_CODE_PS_AWAKE:
518 lbs_deb_cmd("EVENT: ps awake\n");
519 /* handle unexpected PS AWAKE event */
520 if (priv->psstate == PS_STATE_FULL_POWER) {
522 "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
526 priv->psstate = PS_STATE_AWAKE;
528 if (priv->needtowakeup) {
530 * wait for the command processing to finish
531 * before resuming sending
532 * priv->needtowakeup will be set to FALSE
535 lbs_deb_cmd("waking up ...\n");
536 lbs_ps_wakeup(priv, 0);
540 case MACREG_INT_CODE_MIC_ERR_UNICAST:
541 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
542 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
545 case MACREG_INT_CODE_MIC_ERR_MULTICAST:
546 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
547 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
550 case MACREG_INT_CODE_MIB_CHANGED:
551 lbs_deb_cmd("EVENT: MIB CHANGED\n");
553 case MACREG_INT_CODE_INIT_DONE:
554 lbs_deb_cmd("EVENT: INIT DONE\n");
556 case MACREG_INT_CODE_ADHOC_BCN_LOST:
557 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
559 case MACREG_INT_CODE_RSSI_LOW:
560 lbs_pr_alert("EVENT: rssi low\n");
562 case MACREG_INT_CODE_SNR_LOW:
563 lbs_pr_alert("EVENT: snr low\n");
565 case MACREG_INT_CODE_MAX_FAIL:
566 lbs_pr_alert("EVENT: max fail\n");
568 case MACREG_INT_CODE_RSSI_HIGH:
569 lbs_pr_alert("EVENT: rssi high\n");
571 case MACREG_INT_CODE_SNR_HIGH:
572 lbs_pr_alert("EVENT: snr high\n");
575 case MACREG_INT_CODE_MESH_AUTO_STARTED:
576 /* Ignore spurious autostart events if autostart is disabled */
577 if (!priv->mesh_autostart_enabled) {
578 lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
581 lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
582 priv->mesh_connect_status = LBS_CONNECTED;
583 if (priv->mesh_open) {
584 netif_carrier_on(priv->mesh_dev);
585 if (!priv->tx_pending_len)
586 netif_wake_queue(priv->mesh_dev);
588 priv->mode = IW_MODE_ADHOC;
589 schedule_work(&priv->sync_channel);
593 lbs_pr_alert("EVENT: unknown event id %d\n", event);
597 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);