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 unsigned long flags; 31 32 if (priv->connect_status != LBS_CONNECTED) 33 return; 34 35 lbs_deb_enter(LBS_DEB_ASSOC); 36 37 /* 38 * Cisco AP sends EAP failure and de-auth in less than 0.5 ms. 39 * It causes problem in the Supplicant 40 */ 41 msleep_interruptible(1000); 42 43 if (priv->wdev->iftype == NL80211_IFTYPE_STATION) 44 lbs_send_disconnect_notification(priv, locally_generated); 45 46 /* report disconnect to upper layer */ 47 netif_stop_queue(priv->dev); 48 netif_carrier_off(priv->dev); 49 50 /* Free Tx and Rx packets */ 51 spin_lock_irqsave(&priv->driver_lock, flags); 52 kfree_skb(priv->currenttxskb); 53 priv->currenttxskb = NULL; 54 priv->tx_pending_len = 0; 55 spin_unlock_irqrestore(&priv->driver_lock, flags); 56 57 priv->connect_status = LBS_DISCONNECTED; 58 59 if (priv->psstate != PS_STATE_FULL_POWER) { 60 /* make firmware to exit PS mode */ 61 lbs_deb_cmd("disconnected, so exit PS mode\n"); 62 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false); 63 } 64 lbs_deb_leave(LBS_DEB_ASSOC); 65 } 66 67 int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len) 68 { 69 uint16_t respcmd, curcmd; 70 struct cmd_header *resp; 71 int ret = 0; 72 unsigned long flags; 73 uint16_t result; 74 75 lbs_deb_enter(LBS_DEB_HOST); 76 77 mutex_lock(&priv->lock); 78 spin_lock_irqsave(&priv->driver_lock, flags); 79 80 if (!priv->cur_cmd) { 81 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n"); 82 ret = -1; 83 spin_unlock_irqrestore(&priv->driver_lock, flags); 84 goto done; 85 } 86 87 resp = (void *)data; 88 curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command); 89 respcmd = le16_to_cpu(resp->command); 90 result = le16_to_cpu(resp->result); 91 92 lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n", 93 respcmd, le16_to_cpu(resp->seqnum), len); 94 lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len); 95 96 if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) { 97 netdev_info(priv->dev, 98 "Received CMD_RESP with invalid sequence %d (expected %d)\n", 99 le16_to_cpu(resp->seqnum), 100 le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum)); 101 spin_unlock_irqrestore(&priv->driver_lock, flags); 102 ret = -1; 103 goto done; 104 } 105 if (respcmd != CMD_RET(curcmd) && 106 respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) { 107 netdev_info(priv->dev, "Invalid CMD_RESP %x to command %x!\n", 108 respcmd, curcmd); 109 spin_unlock_irqrestore(&priv->driver_lock, flags); 110 ret = -1; 111 goto done; 112 } 113 114 if (resp->result == cpu_to_le16(0x0004)) { 115 /* 0x0004 means -EAGAIN. Drop the response, let it time out 116 and be resubmitted */ 117 netdev_info(priv->dev, 118 "Firmware returns DEFER to command %x. Will let it time out...\n", 119 le16_to_cpu(resp->command)); 120 spin_unlock_irqrestore(&priv->driver_lock, flags); 121 ret = -1; 122 goto done; 123 } 124 125 /* Now we got response from FW, cancel the command timer */ 126 del_timer(&priv->command_timer); 127 priv->cmd_timed_out = 0; 128 129 if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) { 130 /* struct cmd_ds_802_11_ps_mode also contains 131 * the header 132 */ 133 struct cmd_ds_802_11_ps_mode *psmode = (void *)resp; 134 u16 action = le16_to_cpu(psmode->action); 135 136 lbs_deb_host( 137 "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n", 138 result, action); 139 140 if (result) { 141 lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n", 142 result); 143 /* 144 * We should not re-try enter-ps command in 145 * ad-hoc mode. It takes place in 146 * lbs_execute_next_command(). 147 */ 148 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR && 149 action == PS_MODE_ACTION_ENTER_PS) 150 priv->psmode = LBS802_11POWERMODECAM; 151 } else if (action == PS_MODE_ACTION_ENTER_PS) { 152 priv->needtowakeup = 0; 153 priv->psstate = PS_STATE_AWAKE; 154 155 lbs_deb_host("CMD_RESP: ENTER_PS command response\n"); 156 if (priv->connect_status != LBS_CONNECTED) { 157 /* 158 * When Deauth Event received before Enter_PS command 159 * response, We need to wake up the firmware. 160 */ 161 lbs_deb_host( 162 "disconnected, invoking lbs_ps_wakeup\n"); 163 164 spin_unlock_irqrestore(&priv->driver_lock, flags); 165 mutex_unlock(&priv->lock); 166 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, 167 false); 168 mutex_lock(&priv->lock); 169 spin_lock_irqsave(&priv->driver_lock, flags); 170 } 171 } else if (action == PS_MODE_ACTION_EXIT_PS) { 172 priv->needtowakeup = 0; 173 priv->psstate = PS_STATE_FULL_POWER; 174 lbs_deb_host("CMD_RESP: EXIT_PS command response\n"); 175 } else { 176 lbs_deb_host("CMD_RESP: PS action 0x%X\n", action); 177 } 178 179 __lbs_complete_command(priv, priv->cur_cmd, result); 180 spin_unlock_irqrestore(&priv->driver_lock, flags); 181 182 ret = 0; 183 goto done; 184 } 185 186 /* If the command is not successful, cleanup and return failure */ 187 if ((result != 0 || !(respcmd & 0x8000))) { 188 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n", 189 result, respcmd); 190 /* 191 * Handling errors here 192 */ 193 switch (respcmd) { 194 case CMD_RET(CMD_GET_HW_SPEC): 195 case CMD_RET(CMD_802_11_RESET): 196 lbs_deb_host("CMD_RESP: reset failed\n"); 197 break; 198 199 } 200 __lbs_complete_command(priv, priv->cur_cmd, result); 201 spin_unlock_irqrestore(&priv->driver_lock, flags); 202 203 ret = -1; 204 goto done; 205 } 206 207 spin_unlock_irqrestore(&priv->driver_lock, flags); 208 209 if (priv->cur_cmd && priv->cur_cmd->callback) { 210 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg, 211 resp); 212 } 213 214 spin_lock_irqsave(&priv->driver_lock, flags); 215 216 if (priv->cur_cmd) { 217 /* Clean up and Put current command back to cmdfreeq */ 218 __lbs_complete_command(priv, priv->cur_cmd, result); 219 } 220 spin_unlock_irqrestore(&priv->driver_lock, flags); 221 222 done: 223 mutex_unlock(&priv->lock); 224 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret); 225 return ret; 226 } 227 228 int lbs_process_event(struct lbs_private *priv, u32 event) 229 { 230 int ret = 0; 231 struct cmd_header cmd; 232 233 lbs_deb_enter(LBS_DEB_CMD); 234 235 switch (event) { 236 case MACREG_INT_CODE_LINK_SENSED: 237 lbs_deb_cmd("EVENT: link sensed\n"); 238 break; 239 240 case MACREG_INT_CODE_DEAUTHENTICATED: 241 lbs_deb_cmd("EVENT: deauthenticated\n"); 242 lbs_mac_event_disconnected(priv, false); 243 break; 244 245 case MACREG_INT_CODE_DISASSOCIATED: 246 lbs_deb_cmd("EVENT: disassociated\n"); 247 lbs_mac_event_disconnected(priv, false); 248 break; 249 250 case MACREG_INT_CODE_LINK_LOST_NO_SCAN: 251 lbs_deb_cmd("EVENT: link lost\n"); 252 lbs_mac_event_disconnected(priv, true); 253 break; 254 255 case MACREG_INT_CODE_PS_SLEEP: 256 lbs_deb_cmd("EVENT: ps sleep\n"); 257 258 /* handle unexpected PS SLEEP event */ 259 if (priv->psstate == PS_STATE_FULL_POWER) { 260 lbs_deb_cmd( 261 "EVENT: in FULL POWER mode, ignoring PS_SLEEP\n"); 262 break; 263 } 264 if (!list_empty(&priv->cmdpendingq)) { 265 lbs_deb_cmd("EVENT: commands in queue, do not sleep\n"); 266 break; 267 } 268 priv->psstate = PS_STATE_PRE_SLEEP; 269 270 lbs_ps_confirm_sleep(priv); 271 272 break; 273 274 case MACREG_INT_CODE_HOST_AWAKE: 275 lbs_deb_cmd("EVENT: host awake\n"); 276 if (priv->reset_deep_sleep_wakeup) 277 priv->reset_deep_sleep_wakeup(priv); 278 priv->is_deep_sleep = 0; 279 lbs_cmd_async(priv, CMD_802_11_WAKEUP_CONFIRM, &cmd, 280 sizeof(cmd)); 281 priv->is_host_sleep_activated = 0; 282 wake_up_interruptible(&priv->host_sleep_q); 283 break; 284 285 case MACREG_INT_CODE_DEEP_SLEEP_AWAKE: 286 if (priv->reset_deep_sleep_wakeup) 287 priv->reset_deep_sleep_wakeup(priv); 288 lbs_deb_cmd("EVENT: ds awake\n"); 289 priv->is_deep_sleep = 0; 290 priv->wakeup_dev_required = 0; 291 wake_up_interruptible(&priv->ds_awake_q); 292 break; 293 294 case MACREG_INT_CODE_PS_AWAKE: 295 lbs_deb_cmd("EVENT: ps awake\n"); 296 /* handle unexpected PS AWAKE event */ 297 if (priv->psstate == PS_STATE_FULL_POWER) { 298 lbs_deb_cmd( 299 "EVENT: In FULL POWER mode - ignore PS AWAKE\n"); 300 break; 301 } 302 303 priv->psstate = PS_STATE_AWAKE; 304 305 if (priv->needtowakeup) { 306 /* 307 * wait for the command processing to finish 308 * before resuming sending 309 * priv->needtowakeup will be set to FALSE 310 * in lbs_ps_wakeup() 311 */ 312 lbs_deb_cmd("waking up ...\n"); 313 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false); 314 } 315 break; 316 317 case MACREG_INT_CODE_MIC_ERR_UNICAST: 318 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n"); 319 lbs_send_mic_failureevent(priv, event); 320 break; 321 322 case MACREG_INT_CODE_MIC_ERR_MULTICAST: 323 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n"); 324 lbs_send_mic_failureevent(priv, event); 325 break; 326 327 case MACREG_INT_CODE_MIB_CHANGED: 328 lbs_deb_cmd("EVENT: MIB CHANGED\n"); 329 break; 330 case MACREG_INT_CODE_INIT_DONE: 331 lbs_deb_cmd("EVENT: INIT DONE\n"); 332 break; 333 case MACREG_INT_CODE_ADHOC_BCN_LOST: 334 lbs_deb_cmd("EVENT: ADHOC beacon lost\n"); 335 break; 336 case MACREG_INT_CODE_RSSI_LOW: 337 netdev_alert(priv->dev, "EVENT: rssi low\n"); 338 break; 339 case MACREG_INT_CODE_SNR_LOW: 340 netdev_alert(priv->dev, "EVENT: snr low\n"); 341 break; 342 case MACREG_INT_CODE_MAX_FAIL: 343 netdev_alert(priv->dev, "EVENT: max fail\n"); 344 break; 345 case MACREG_INT_CODE_RSSI_HIGH: 346 netdev_alert(priv->dev, "EVENT: rssi high\n"); 347 break; 348 case MACREG_INT_CODE_SNR_HIGH: 349 netdev_alert(priv->dev, "EVENT: snr high\n"); 350 break; 351 352 case MACREG_INT_CODE_MESH_AUTO_STARTED: 353 /* Ignore spurious autostart events */ 354 netdev_info(priv->dev, "EVENT: MESH_AUTO_STARTED (ignoring)\n"); 355 break; 356 357 default: 358 netdev_alert(priv->dev, "EVENT: unknown event id %d\n", event); 359 break; 360 } 361 362 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret); 363 return ret; 364 } 365