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_snmp_mib(struct lbs_private *priv,
150 struct cmd_ds_command *resp)
152 struct cmd_ds_802_11_snmp_mib *smib = &resp->params.smib;
153 u16 oid = le16_to_cpu(smib->oid);
154 u16 querytype = le16_to_cpu(smib->querytype);
156 lbs_deb_enter(LBS_DEB_CMD);
158 lbs_deb_cmd("SNMP_RESP: oid 0x%x, querytype 0x%x\n", oid,
160 lbs_deb_cmd("SNMP_RESP: Buf size %d\n", le16_to_cpu(smib->bufsize));
162 if (querytype == CMD_ACT_GET) {
166 le16_to_cpu(*((__le16 *)(smib->value)));
167 lbs_deb_cmd("SNMP_RESP: frag threshold %u\n",
172 le16_to_cpu(*((__le16 *)(smib->value)));
173 lbs_deb_cmd("SNMP_RESP: rts threshold %u\n",
176 case SHORT_RETRYLIM_I:
178 le16_to_cpu(*((__le16 *)(smib->value)));
179 lbs_deb_cmd("SNMP_RESP: tx retry count %u\n",
187 lbs_deb_enter(LBS_DEB_CMD);
191 static int lbs_ret_802_11_rf_tx_power(struct lbs_private *priv,
192 struct cmd_ds_command *resp)
194 struct cmd_ds_802_11_rf_tx_power *rtp = &resp->params.txp;
196 lbs_deb_enter(LBS_DEB_CMD);
198 priv->txpowerlevel = le16_to_cpu(rtp->currentlevel);
200 lbs_deb_cmd("TX power currently %d\n", priv->txpowerlevel);
202 lbs_deb_leave(LBS_DEB_CMD);
206 static int lbs_ret_802_11_rssi(struct lbs_private *priv,
207 struct cmd_ds_command *resp)
209 struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;
211 lbs_deb_enter(LBS_DEB_CMD);
213 /* store the non average value */
214 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->SNR);
215 priv->NF[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->noisefloor);
217 priv->SNR[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgSNR);
218 priv->NF[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgnoisefloor);
220 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] =
221 CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_NOAVG],
222 priv->NF[TYPE_BEACON][TYPE_NOAVG]);
224 priv->RSSI[TYPE_BEACON][TYPE_AVG] =
225 CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
226 priv->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);
228 lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
229 priv->RSSI[TYPE_BEACON][TYPE_NOAVG],
230 priv->RSSI[TYPE_BEACON][TYPE_AVG]);
232 lbs_deb_leave(LBS_DEB_CMD);
236 static int lbs_ret_802_11_bcn_ctrl(struct lbs_private * priv,
237 struct cmd_ds_command *resp)
239 struct cmd_ds_802_11_beacon_control *bcn_ctrl =
240 &resp->params.bcn_ctrl;
242 lbs_deb_enter(LBS_DEB_CMD);
244 if (bcn_ctrl->action == CMD_ACT_GET) {
245 priv->beacon_enable = (u8) le16_to_cpu(bcn_ctrl->beacon_enable);
246 priv->beacon_period = le16_to_cpu(bcn_ctrl->beacon_period);
249 lbs_deb_enter(LBS_DEB_CMD);
253 static inline int handle_cmd_response(struct lbs_private *priv,
254 struct cmd_header *cmd_response)
256 struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
259 uint16_t respcmd = le16_to_cpu(resp->command);
261 lbs_deb_enter(LBS_DEB_HOST);
264 case CMD_RET(CMD_MAC_REG_ACCESS):
265 case CMD_RET(CMD_BBP_REG_ACCESS):
266 case CMD_RET(CMD_RF_REG_ACCESS):
267 ret = lbs_ret_reg_access(priv, respcmd, resp);
270 case CMD_RET_802_11_ASSOCIATE:
271 case CMD_RET(CMD_802_11_ASSOCIATE):
272 case CMD_RET(CMD_802_11_REASSOCIATE):
273 ret = lbs_ret_80211_associate(priv, resp);
276 case CMD_RET(CMD_802_11_DISASSOCIATE):
277 case CMD_RET(CMD_802_11_DEAUTHENTICATE):
278 ret = lbs_ret_80211_disassociate(priv);
281 case CMD_RET(CMD_802_11_AD_HOC_START):
282 case CMD_RET(CMD_802_11_AD_HOC_JOIN):
283 ret = lbs_ret_80211_ad_hoc_start(priv, resp);
286 case CMD_RET(CMD_802_11_SNMP_MIB):
287 ret = lbs_ret_802_11_snmp_mib(priv, resp);
290 case CMD_RET(CMD_802_11_RF_TX_POWER):
291 ret = lbs_ret_802_11_rf_tx_power(priv, resp);
294 case CMD_RET(CMD_802_11_SET_AFC):
295 case CMD_RET(CMD_802_11_GET_AFC):
296 spin_lock_irqsave(&priv->driver_lock, flags);
297 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.afc,
298 sizeof(struct cmd_ds_802_11_afc));
299 spin_unlock_irqrestore(&priv->driver_lock, flags);
303 case CMD_RET(CMD_802_11_RESET):
304 case CMD_RET(CMD_802_11_AUTHENTICATE):
305 case CMD_RET(CMD_802_11_BEACON_STOP):
308 case CMD_RET(CMD_802_11_RSSI):
309 ret = lbs_ret_802_11_rssi(priv, resp);
312 case CMD_RET(CMD_802_11_AD_HOC_STOP):
313 ret = lbs_ret_80211_ad_hoc_stop(priv);
316 case CMD_RET(CMD_802_11D_DOMAIN_INFO):
317 ret = lbs_ret_802_11d_domain_info(resp);
320 case CMD_RET(CMD_802_11_TPC_CFG):
321 spin_lock_irqsave(&priv->driver_lock, flags);
322 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
323 sizeof(struct cmd_ds_802_11_tpc_cfg));
324 spin_unlock_irqrestore(&priv->driver_lock, flags);
326 case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
327 spin_lock_irqsave(&priv->driver_lock, flags);
328 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.ledgpio,
329 sizeof(struct cmd_ds_802_11_led_ctrl));
330 spin_unlock_irqrestore(&priv->driver_lock, flags);
333 case CMD_RET(CMD_GET_TSF):
334 spin_lock_irqsave(&priv->driver_lock, flags);
335 memcpy((void *)priv->cur_cmd->callback_arg,
336 &resp->params.gettsf.tsfvalue, sizeof(u64));
337 spin_unlock_irqrestore(&priv->driver_lock, flags);
339 case CMD_RET(CMD_BT_ACCESS):
340 spin_lock_irqsave(&priv->driver_lock, flags);
341 if (priv->cur_cmd->callback_arg)
342 memcpy((void *)priv->cur_cmd->callback_arg,
343 &resp->params.bt.addr1, 2 * ETH_ALEN);
344 spin_unlock_irqrestore(&priv->driver_lock, flags);
346 case CMD_RET(CMD_FWT_ACCESS):
347 spin_lock_irqsave(&priv->driver_lock, flags);
348 if (priv->cur_cmd->callback_arg)
349 memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
350 sizeof(resp->params.fwt));
351 spin_unlock_irqrestore(&priv->driver_lock, flags);
353 case CMD_RET(CMD_802_11_BEACON_CTRL):
354 ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
358 lbs_pr_err("CMD_RESP: unknown cmd response 0x%04x\n",
359 le16_to_cpu(resp->command));
362 lbs_deb_leave(LBS_DEB_HOST);
366 int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
368 uint16_t respcmd, curcmd;
369 struct cmd_header *resp;
374 lbs_deb_enter(LBS_DEB_HOST);
376 mutex_lock(&priv->lock);
377 spin_lock_irqsave(&priv->driver_lock, flags);
379 if (!priv->cur_cmd) {
380 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
382 spin_unlock_irqrestore(&priv->driver_lock, flags);
387 curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
388 respcmd = le16_to_cpu(resp->command);
389 result = le16_to_cpu(resp->result);
391 lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
392 respcmd, le16_to_cpu(resp->seqnum), len);
393 lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len);
395 if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
396 lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
397 le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
398 spin_unlock_irqrestore(&priv->driver_lock, flags);
402 if (respcmd != CMD_RET(curcmd) &&
403 respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
404 lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
405 spin_unlock_irqrestore(&priv->driver_lock, flags);
410 if (resp->result == cpu_to_le16(0x0004)) {
411 /* 0x0004 means -EAGAIN. Drop the response, let it time out
412 and be resubmitted */
413 lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
414 le16_to_cpu(resp->command));
415 spin_unlock_irqrestore(&priv->driver_lock, flags);
420 /* Now we got response from FW, cancel the command timer */
421 del_timer(&priv->command_timer);
422 priv->cmd_timed_out = 0;
423 if (priv->nr_retries) {
424 lbs_pr_info("Received result %x to command %x after %d retries\n",
425 result, curcmd, priv->nr_retries);
426 priv->nr_retries = 0;
429 /* Store the response code to cur_cmd_retcode. */
430 priv->cur_cmd_retcode = result;
432 if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
433 struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
434 u16 action = le16_to_cpu(psmode->action);
437 "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
441 lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
444 * We should not re-try enter-ps command in
445 * ad-hoc mode. It takes place in
446 * lbs_execute_next_command().
448 if (priv->mode == IW_MODE_ADHOC &&
449 action == CMD_SUBCMD_ENTER_PS)
450 priv->psmode = LBS802_11POWERMODECAM;
451 } else if (action == CMD_SUBCMD_ENTER_PS) {
452 priv->needtowakeup = 0;
453 priv->psstate = PS_STATE_AWAKE;
455 lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
456 if (priv->connect_status != LBS_CONNECTED) {
458 * When Deauth Event received before Enter_PS command
459 * response, We need to wake up the firmware.
462 "disconnected, invoking lbs_ps_wakeup\n");
464 spin_unlock_irqrestore(&priv->driver_lock, flags);
465 mutex_unlock(&priv->lock);
466 lbs_ps_wakeup(priv, 0);
467 mutex_lock(&priv->lock);
468 spin_lock_irqsave(&priv->driver_lock, flags);
470 } else if (action == CMD_SUBCMD_EXIT_PS) {
471 priv->needtowakeup = 0;
472 priv->psstate = PS_STATE_FULL_POWER;
473 lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
475 lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
478 lbs_complete_command(priv, priv->cur_cmd, result);
479 spin_unlock_irqrestore(&priv->driver_lock, flags);
485 /* If the command is not successful, cleanup and return failure */
486 if ((result != 0 || !(respcmd & 0x8000))) {
487 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
490 * Handling errors here
493 case CMD_RET(CMD_GET_HW_SPEC):
494 case CMD_RET(CMD_802_11_RESET):
495 lbs_deb_host("CMD_RESP: reset failed\n");
499 lbs_complete_command(priv, priv->cur_cmd, result);
500 spin_unlock_irqrestore(&priv->driver_lock, flags);
506 spin_unlock_irqrestore(&priv->driver_lock, flags);
508 if (priv->cur_cmd && priv->cur_cmd->callback) {
509 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
512 ret = handle_cmd_response(priv, resp);
514 spin_lock_irqsave(&priv->driver_lock, flags);
517 /* Clean up and Put current command back to cmdfreeq */
518 lbs_complete_command(priv, priv->cur_cmd, result);
520 spin_unlock_irqrestore(&priv->driver_lock, flags);
523 mutex_unlock(&priv->lock);
524 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
528 static int lbs_send_confirmwake(struct lbs_private *priv)
530 struct cmd_header cmd;
533 lbs_deb_enter(LBS_DEB_HOST);
535 cmd.command = cpu_to_le16(CMD_802_11_WAKEUP_CONFIRM);
536 cmd.size = cpu_to_le16(sizeof(cmd));
537 cmd.seqnum = cpu_to_le16(++priv->seqnum);
540 lbs_deb_hex(LBS_DEB_HOST, "wake confirm", (u8 *) &cmd,
543 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &cmd, sizeof(cmd));
545 lbs_pr_alert("SEND_WAKEC_CMD: Host to Card failed for Confirm Wake\n");
547 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
551 int lbs_process_event(struct lbs_private *priv, u32 event)
555 lbs_deb_enter(LBS_DEB_CMD);
558 case MACREG_INT_CODE_LINK_SENSED:
559 lbs_deb_cmd("EVENT: link sensed\n");
562 case MACREG_INT_CODE_DEAUTHENTICATED:
563 lbs_deb_cmd("EVENT: deauthenticated\n");
564 lbs_mac_event_disconnected(priv);
567 case MACREG_INT_CODE_DISASSOCIATED:
568 lbs_deb_cmd("EVENT: disassociated\n");
569 lbs_mac_event_disconnected(priv);
572 case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
573 lbs_deb_cmd("EVENT: link lost\n");
574 lbs_mac_event_disconnected(priv);
577 case MACREG_INT_CODE_PS_SLEEP:
578 lbs_deb_cmd("EVENT: ps sleep\n");
580 /* handle unexpected PS SLEEP event */
581 if (priv->psstate == PS_STATE_FULL_POWER) {
583 "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
586 priv->psstate = PS_STATE_PRE_SLEEP;
588 lbs_ps_confirm_sleep(priv);
592 case MACREG_INT_CODE_HOST_AWAKE:
593 lbs_deb_cmd("EVENT: host awake\n");
594 lbs_send_confirmwake(priv);
597 case MACREG_INT_CODE_PS_AWAKE:
598 lbs_deb_cmd("EVENT: ps awake\n");
599 /* handle unexpected PS AWAKE event */
600 if (priv->psstate == PS_STATE_FULL_POWER) {
602 "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
606 priv->psstate = PS_STATE_AWAKE;
608 if (priv->needtowakeup) {
610 * wait for the command processing to finish
611 * before resuming sending
612 * priv->needtowakeup will be set to FALSE
615 lbs_deb_cmd("waking up ...\n");
616 lbs_ps_wakeup(priv, 0);
620 case MACREG_INT_CODE_MIC_ERR_UNICAST:
621 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
622 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
625 case MACREG_INT_CODE_MIC_ERR_MULTICAST:
626 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
627 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
630 case MACREG_INT_CODE_MIB_CHANGED:
631 lbs_deb_cmd("EVENT: MIB CHANGED\n");
633 case MACREG_INT_CODE_INIT_DONE:
634 lbs_deb_cmd("EVENT: INIT DONE\n");
636 case MACREG_INT_CODE_ADHOC_BCN_LOST:
637 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
639 case MACREG_INT_CODE_RSSI_LOW:
640 lbs_pr_alert("EVENT: rssi low\n");
642 case MACREG_INT_CODE_SNR_LOW:
643 lbs_pr_alert("EVENT: snr low\n");
645 case MACREG_INT_CODE_MAX_FAIL:
646 lbs_pr_alert("EVENT: max fail\n");
648 case MACREG_INT_CODE_RSSI_HIGH:
649 lbs_pr_alert("EVENT: rssi high\n");
651 case MACREG_INT_CODE_SNR_HIGH:
652 lbs_pr_alert("EVENT: snr high\n");
655 case MACREG_INT_CODE_MESH_AUTO_STARTED:
656 /* Ignore spurious autostart events if autostart is disabled */
657 if (!priv->mesh_autostart_enabled) {
658 lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
661 lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
662 priv->mesh_connect_status = LBS_CONNECTED;
663 if (priv->mesh_open) {
664 netif_carrier_on(priv->mesh_dev);
665 if (!priv->tx_pending_len)
666 netif_wake_queue(priv->mesh_dev);
668 priv->mode = IW_MODE_ADHOC;
669 schedule_work(&priv->sync_channel);
673 lbs_pr_alert("EVENT: unknown event id %d\n", event);
677 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);