cmdresp.c 9.4 KB

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