cmdresp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /**
  2. * This file contains the handling of command
  3. * responses as well as events generated by firmware.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/sched.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/netdevice.h>
  9. #include <asm/unaligned.h>
  10. #include <net/iw_handler.h>
  11. #include "host.h"
  12. #include "decl.h"
  13. #include "cmd.h"
  14. #include "defs.h"
  15. #include "dev.h"
  16. #include "assoc.h"
  17. #include "wext.h"
  18. /**
  19. * @brief This function handles disconnect event. it
  20. * reports disconnect to upper layer, clean tx/rx packets,
  21. * reset link state etc.
  22. *
  23. * @param priv A pointer to struct lbs_private structure
  24. * @return n/a
  25. */
  26. void lbs_mac_event_disconnected(struct lbs_private *priv)
  27. {
  28. union iwreq_data wrqu;
  29. if (priv->connect_status != LBS_CONNECTED)
  30. return;
  31. lbs_deb_enter(LBS_DEB_ASSOC);
  32. memset(wrqu.ap_addr.sa_data, 0x00, ETH_ALEN);
  33. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  34. /*
  35. * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
  36. * It causes problem in the Supplicant
  37. */
  38. msleep_interruptible(1000);
  39. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  40. /* report disconnect to upper layer */
  41. netif_stop_queue(priv->dev);
  42. netif_carrier_off(priv->dev);
  43. /* Free Tx and Rx packets */
  44. kfree_skb(priv->currenttxskb);
  45. priv->currenttxskb = NULL;
  46. priv->tx_pending_len = 0;
  47. /* reset SNR/NF/RSSI values */
  48. memset(priv->SNR, 0x00, sizeof(priv->SNR));
  49. memset(priv->NF, 0x00, sizeof(priv->NF));
  50. memset(priv->RSSI, 0x00, sizeof(priv->RSSI));
  51. memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
  52. memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
  53. priv->nextSNRNF = 0;
  54. priv->numSNRNF = 0;
  55. priv->connect_status = LBS_DISCONNECTED;
  56. /* Clear out associated SSID and BSSID since connection is
  57. * no longer valid.
  58. */
  59. memset(&priv->curbssparams.bssid, 0, ETH_ALEN);
  60. memset(&priv->curbssparams.ssid, 0, IEEE80211_MAX_SSID_LEN);
  61. priv->curbssparams.ssid_len = 0;
  62. if (priv->psstate != PS_STATE_FULL_POWER) {
  63. /* make firmware to exit PS mode */
  64. lbs_deb_cmd("disconnected, so exit PS mode\n");
  65. lbs_ps_wakeup(priv, 0);
  66. }
  67. lbs_deb_leave(LBS_DEB_ASSOC);
  68. }
  69. /**
  70. * @brief This function handles MIC failure event.
  71. *
  72. * @param priv A pointer to struct lbs_private structure
  73. * @para event the event id
  74. * @return n/a
  75. */
  76. static void handle_mic_failureevent(struct lbs_private *priv, u32 event)
  77. {
  78. char buf[50];
  79. lbs_deb_enter(LBS_DEB_CMD);
  80. memset(buf, 0, sizeof(buf));
  81. sprintf(buf, "%s", "MLME-MICHAELMICFAILURE.indication ");
  82. if (event == MACREG_INT_CODE_MIC_ERR_UNICAST) {
  83. strcat(buf, "unicast ");
  84. } else {
  85. strcat(buf, "multicast ");
  86. }
  87. lbs_send_iwevcustom_event(priv, buf);
  88. lbs_deb_leave(LBS_DEB_CMD);
  89. }
  90. static int lbs_ret_reg_access(struct lbs_private *priv,
  91. u16 type, struct cmd_ds_command *resp)
  92. {
  93. int ret = 0;
  94. lbs_deb_enter(LBS_DEB_CMD);
  95. switch (type) {
  96. case CMD_RET(CMD_MAC_REG_ACCESS):
  97. {
  98. struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
  99. priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
  100. priv->offsetvalue.value = le32_to_cpu(reg->value);
  101. break;
  102. }
  103. case CMD_RET(CMD_BBP_REG_ACCESS):
  104. {
  105. struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
  106. priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
  107. priv->offsetvalue.value = reg->value;
  108. break;
  109. }
  110. case CMD_RET(CMD_RF_REG_ACCESS):
  111. {
  112. struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
  113. priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
  114. priv->offsetvalue.value = reg->value;
  115. break;
  116. }
  117. default:
  118. ret = -1;
  119. }
  120. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  121. return ret;
  122. }
  123. static inline int handle_cmd_response(struct lbs_private *priv,
  124. struct cmd_header *cmd_response)
  125. {
  126. struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
  127. int ret = 0;
  128. unsigned long flags;
  129. uint16_t respcmd = le16_to_cpu(resp->command);
  130. lbs_deb_enter(LBS_DEB_HOST);
  131. switch (respcmd) {
  132. case CMD_RET(CMD_MAC_REG_ACCESS):
  133. case CMD_RET(CMD_BBP_REG_ACCESS):
  134. case CMD_RET(CMD_RF_REG_ACCESS):
  135. ret = lbs_ret_reg_access(priv, respcmd, resp);
  136. break;
  137. case CMD_RET(CMD_802_11_SET_AFC):
  138. case CMD_RET(CMD_802_11_GET_AFC):
  139. spin_lock_irqsave(&priv->driver_lock, flags);
  140. memmove((void *)priv->cur_cmd->callback_arg, &resp->params.afc,
  141. sizeof(struct cmd_ds_802_11_afc));
  142. spin_unlock_irqrestore(&priv->driver_lock, flags);
  143. break;
  144. case CMD_RET(CMD_802_11_BEACON_STOP):
  145. break;
  146. case CMD_RET(CMD_802_11_RSSI):
  147. ret = lbs_ret_802_11_rssi(priv, resp);
  148. break;
  149. case CMD_RET(CMD_802_11_TPC_CFG):
  150. spin_lock_irqsave(&priv->driver_lock, flags);
  151. memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
  152. sizeof(struct cmd_ds_802_11_tpc_cfg));
  153. spin_unlock_irqrestore(&priv->driver_lock, flags);
  154. break;
  155. case CMD_RET(CMD_GET_TSF):
  156. spin_lock_irqsave(&priv->driver_lock, flags);
  157. memcpy((void *)priv->cur_cmd->callback_arg,
  158. &resp->params.gettsf.tsfvalue, sizeof(u64));
  159. spin_unlock_irqrestore(&priv->driver_lock, flags);
  160. break;
  161. case CMD_RET(CMD_BT_ACCESS):
  162. spin_lock_irqsave(&priv->driver_lock, flags);
  163. if (priv->cur_cmd->callback_arg)
  164. memcpy((void *)priv->cur_cmd->callback_arg,
  165. &resp->params.bt.addr1, 2 * ETH_ALEN);
  166. spin_unlock_irqrestore(&priv->driver_lock, flags);
  167. break;
  168. case CMD_RET(CMD_FWT_ACCESS):
  169. spin_lock_irqsave(&priv->driver_lock, flags);
  170. if (priv->cur_cmd->callback_arg)
  171. memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
  172. sizeof(resp->params.fwt));
  173. spin_unlock_irqrestore(&priv->driver_lock, flags);
  174. break;
  175. case CMD_RET(CMD_802_11_BEACON_CTRL):
  176. ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
  177. break;
  178. default:
  179. lbs_pr_err("CMD_RESP: unknown cmd response 0x%04x\n",
  180. le16_to_cpu(resp->command));
  181. break;
  182. }
  183. lbs_deb_leave(LBS_DEB_HOST);
  184. return ret;
  185. }
  186. int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
  187. {
  188. uint16_t respcmd, curcmd;
  189. struct cmd_header *resp;
  190. int ret = 0;
  191. unsigned long flags;
  192. uint16_t result;
  193. lbs_deb_enter(LBS_DEB_HOST);
  194. mutex_lock(&priv->lock);
  195. spin_lock_irqsave(&priv->driver_lock, flags);
  196. if (!priv->cur_cmd) {
  197. lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
  198. ret = -1;
  199. spin_unlock_irqrestore(&priv->driver_lock, flags);
  200. goto done;
  201. }
  202. resp = (void *)data;
  203. curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
  204. respcmd = le16_to_cpu(resp->command);
  205. result = le16_to_cpu(resp->result);
  206. lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
  207. respcmd, le16_to_cpu(resp->seqnum), len);
  208. lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len);
  209. if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
  210. lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
  211. le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
  212. spin_unlock_irqrestore(&priv->driver_lock, flags);
  213. ret = -1;
  214. goto done;
  215. }
  216. if (respcmd != CMD_RET(curcmd) &&
  217. respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
  218. lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
  219. spin_unlock_irqrestore(&priv->driver_lock, flags);
  220. ret = -1;
  221. goto done;
  222. }
  223. if (resp->result == cpu_to_le16(0x0004)) {
  224. /* 0x0004 means -EAGAIN. Drop the response, let it time out
  225. and be resubmitted */
  226. lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
  227. le16_to_cpu(resp->command));
  228. spin_unlock_irqrestore(&priv->driver_lock, flags);
  229. ret = -1;
  230. goto done;
  231. }
  232. /* Now we got response from FW, cancel the command timer */
  233. del_timer(&priv->command_timer);
  234. priv->cmd_timed_out = 0;
  235. if (priv->nr_retries) {
  236. lbs_pr_info("Received result %x to command %x after %d retries\n",
  237. result, curcmd, priv->nr_retries);
  238. priv->nr_retries = 0;
  239. }
  240. /* Store the response code to cur_cmd_retcode. */
  241. priv->cur_cmd_retcode = result;
  242. if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
  243. struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
  244. u16 action = le16_to_cpu(psmode->action);
  245. lbs_deb_host(
  246. "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
  247. result, action);
  248. if (result) {
  249. lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
  250. result);
  251. /*
  252. * We should not re-try enter-ps command in
  253. * ad-hoc mode. It takes place in
  254. * lbs_execute_next_command().
  255. */
  256. if (priv->mode == IW_MODE_ADHOC &&
  257. action == CMD_SUBCMD_ENTER_PS)
  258. priv->psmode = LBS802_11POWERMODECAM;
  259. } else if (action == CMD_SUBCMD_ENTER_PS) {
  260. priv->needtowakeup = 0;
  261. priv->psstate = PS_STATE_AWAKE;
  262. lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
  263. if (priv->connect_status != LBS_CONNECTED) {
  264. /*
  265. * When Deauth Event received before Enter_PS command
  266. * response, We need to wake up the firmware.
  267. */
  268. lbs_deb_host(
  269. "disconnected, invoking lbs_ps_wakeup\n");
  270. spin_unlock_irqrestore(&priv->driver_lock, flags);
  271. mutex_unlock(&priv->lock);
  272. lbs_ps_wakeup(priv, 0);
  273. mutex_lock(&priv->lock);
  274. spin_lock_irqsave(&priv->driver_lock, flags);
  275. }
  276. } else if (action == CMD_SUBCMD_EXIT_PS) {
  277. priv->needtowakeup = 0;
  278. priv->psstate = PS_STATE_FULL_POWER;
  279. lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
  280. } else {
  281. lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
  282. }
  283. lbs_complete_command(priv, priv->cur_cmd, result);
  284. spin_unlock_irqrestore(&priv->driver_lock, flags);
  285. ret = 0;
  286. goto done;
  287. }
  288. /* If the command is not successful, cleanup and return failure */
  289. if ((result != 0 || !(respcmd & 0x8000))) {
  290. lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
  291. result, respcmd);
  292. /*
  293. * Handling errors here
  294. */
  295. switch (respcmd) {
  296. case CMD_RET(CMD_GET_HW_SPEC):
  297. case CMD_RET(CMD_802_11_RESET):
  298. lbs_deb_host("CMD_RESP: reset failed\n");
  299. break;
  300. }
  301. lbs_complete_command(priv, priv->cur_cmd, result);
  302. spin_unlock_irqrestore(&priv->driver_lock, flags);
  303. ret = -1;
  304. goto done;
  305. }
  306. spin_unlock_irqrestore(&priv->driver_lock, flags);
  307. if (priv->cur_cmd && priv->cur_cmd->callback) {
  308. ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
  309. resp);
  310. } else
  311. ret = handle_cmd_response(priv, resp);
  312. spin_lock_irqsave(&priv->driver_lock, flags);
  313. if (priv->cur_cmd) {
  314. /* Clean up and Put current command back to cmdfreeq */
  315. lbs_complete_command(priv, priv->cur_cmd, result);
  316. }
  317. spin_unlock_irqrestore(&priv->driver_lock, flags);
  318. done:
  319. mutex_unlock(&priv->lock);
  320. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  321. return ret;
  322. }
  323. static int lbs_send_confirmwake(struct lbs_private *priv)
  324. {
  325. struct cmd_header cmd;
  326. int ret = 0;
  327. lbs_deb_enter(LBS_DEB_HOST);
  328. cmd.command = cpu_to_le16(CMD_802_11_WAKEUP_CONFIRM);
  329. cmd.size = cpu_to_le16(sizeof(cmd));
  330. cmd.seqnum = cpu_to_le16(++priv->seqnum);
  331. cmd.result = 0;
  332. lbs_deb_hex(LBS_DEB_HOST, "wake confirm", (u8 *) &cmd,
  333. sizeof(cmd));
  334. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &cmd, sizeof(cmd));
  335. if (ret)
  336. lbs_pr_alert("SEND_WAKEC_CMD: Host to Card failed for Confirm Wake\n");
  337. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  338. return ret;
  339. }
  340. int lbs_process_event(struct lbs_private *priv, u32 event)
  341. {
  342. int ret = 0;
  343. lbs_deb_enter(LBS_DEB_CMD);
  344. switch (event) {
  345. case MACREG_INT_CODE_LINK_SENSED:
  346. lbs_deb_cmd("EVENT: link sensed\n");
  347. break;
  348. case MACREG_INT_CODE_DEAUTHENTICATED:
  349. lbs_deb_cmd("EVENT: deauthenticated\n");
  350. lbs_mac_event_disconnected(priv);
  351. break;
  352. case MACREG_INT_CODE_DISASSOCIATED:
  353. lbs_deb_cmd("EVENT: disassociated\n");
  354. lbs_mac_event_disconnected(priv);
  355. break;
  356. case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
  357. lbs_deb_cmd("EVENT: link lost\n");
  358. lbs_mac_event_disconnected(priv);
  359. break;
  360. case MACREG_INT_CODE_PS_SLEEP:
  361. lbs_deb_cmd("EVENT: ps sleep\n");
  362. /* handle unexpected PS SLEEP event */
  363. if (priv->psstate == PS_STATE_FULL_POWER) {
  364. lbs_deb_cmd(
  365. "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
  366. break;
  367. }
  368. priv->psstate = PS_STATE_PRE_SLEEP;
  369. lbs_ps_confirm_sleep(priv);
  370. break;
  371. case MACREG_INT_CODE_HOST_AWAKE:
  372. lbs_deb_cmd("EVENT: host awake\n");
  373. if (priv->reset_deep_sleep_wakeup)
  374. priv->reset_deep_sleep_wakeup(priv);
  375. priv->is_deep_sleep = 0;
  376. lbs_send_confirmwake(priv);
  377. break;
  378. case MACREG_INT_CODE_DEEP_SLEEP_AWAKE:
  379. if (priv->reset_deep_sleep_wakeup)
  380. priv->reset_deep_sleep_wakeup(priv);
  381. lbs_deb_cmd("EVENT: ds awake\n");
  382. priv->is_deep_sleep = 0;
  383. priv->wakeup_dev_required = 0;
  384. wake_up_interruptible(&priv->ds_awake_q);
  385. break;
  386. case MACREG_INT_CODE_PS_AWAKE:
  387. lbs_deb_cmd("EVENT: ps awake\n");
  388. /* handle unexpected PS AWAKE event */
  389. if (priv->psstate == PS_STATE_FULL_POWER) {
  390. lbs_deb_cmd(
  391. "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
  392. break;
  393. }
  394. priv->psstate = PS_STATE_AWAKE;
  395. if (priv->needtowakeup) {
  396. /*
  397. * wait for the command processing to finish
  398. * before resuming sending
  399. * priv->needtowakeup will be set to FALSE
  400. * in lbs_ps_wakeup()
  401. */
  402. lbs_deb_cmd("waking up ...\n");
  403. lbs_ps_wakeup(priv, 0);
  404. }
  405. break;
  406. case MACREG_INT_CODE_MIC_ERR_UNICAST:
  407. lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
  408. handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
  409. break;
  410. case MACREG_INT_CODE_MIC_ERR_MULTICAST:
  411. lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
  412. handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
  413. break;
  414. case MACREG_INT_CODE_MIB_CHANGED:
  415. lbs_deb_cmd("EVENT: MIB CHANGED\n");
  416. break;
  417. case MACREG_INT_CODE_INIT_DONE:
  418. lbs_deb_cmd("EVENT: INIT DONE\n");
  419. break;
  420. case MACREG_INT_CODE_ADHOC_BCN_LOST:
  421. lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
  422. break;
  423. case MACREG_INT_CODE_RSSI_LOW:
  424. lbs_pr_alert("EVENT: rssi low\n");
  425. break;
  426. case MACREG_INT_CODE_SNR_LOW:
  427. lbs_pr_alert("EVENT: snr low\n");
  428. break;
  429. case MACREG_INT_CODE_MAX_FAIL:
  430. lbs_pr_alert("EVENT: max fail\n");
  431. break;
  432. case MACREG_INT_CODE_RSSI_HIGH:
  433. lbs_pr_alert("EVENT: rssi high\n");
  434. break;
  435. case MACREG_INT_CODE_SNR_HIGH:
  436. lbs_pr_alert("EVENT: snr high\n");
  437. break;
  438. case MACREG_INT_CODE_MESH_AUTO_STARTED:
  439. /* Ignore spurious autostart events if autostart is disabled */
  440. if (!priv->mesh_autostart_enabled) {
  441. lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
  442. break;
  443. }
  444. lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
  445. priv->mesh_connect_status = LBS_CONNECTED;
  446. if (priv->mesh_open) {
  447. netif_carrier_on(priv->mesh_dev);
  448. if (!priv->tx_pending_len)
  449. netif_wake_queue(priv->mesh_dev);
  450. }
  451. priv->mode = IW_MODE_ADHOC;
  452. schedule_work(&priv->sync_channel);
  453. break;
  454. default:
  455. lbs_pr_alert("EVENT: unknown event id %d\n", event);
  456. break;
  457. }
  458. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  459. return ret;
  460. }