cmdevt.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /*
  2. * Marvell Wireless LAN device driver: commands and events
  3. *
  4. * Copyright (C) 2011, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include "decl.h"
  20. #include "ioctl.h"
  21. #include "util.h"
  22. #include "fw.h"
  23. #include "main.h"
  24. #include "wmm.h"
  25. #include "11n.h"
  26. /*
  27. * This function initializes a command node.
  28. *
  29. * The actual allocation of the node is not done by this function. It only
  30. * initiates a node by filling it with default parameters. Similarly,
  31. * allocation of the different buffers used (IOCTL buffer, data buffer) are
  32. * not done by this function either.
  33. */
  34. static void
  35. mwifiex_init_cmd_node(struct mwifiex_private *priv,
  36. struct cmd_ctrl_node *cmd_node,
  37. u32 cmd_oid, void *data_buf)
  38. {
  39. cmd_node->priv = priv;
  40. cmd_node->cmd_oid = cmd_oid;
  41. cmd_node->wait_q_enabled = priv->adapter->cmd_wait_q_required;
  42. priv->adapter->cmd_wait_q_required = false;
  43. cmd_node->data_buf = data_buf;
  44. cmd_node->cmd_skb = cmd_node->skb;
  45. }
  46. /*
  47. * This function returns a command node from the free queue depending upon
  48. * availability.
  49. */
  50. static struct cmd_ctrl_node *
  51. mwifiex_get_cmd_node(struct mwifiex_adapter *adapter)
  52. {
  53. struct cmd_ctrl_node *cmd_node;
  54. unsigned long flags;
  55. spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
  56. if (list_empty(&adapter->cmd_free_q)) {
  57. dev_err(adapter->dev, "GET_CMD_NODE: cmd node not available\n");
  58. spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
  59. return NULL;
  60. }
  61. cmd_node = list_first_entry(&adapter->cmd_free_q,
  62. struct cmd_ctrl_node, list);
  63. list_del(&cmd_node->list);
  64. spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
  65. return cmd_node;
  66. }
  67. /*
  68. * This function cleans up a command node.
  69. *
  70. * The function resets the fields including the buffer pointers.
  71. * This function does not try to free the buffers. They must be
  72. * freed before calling this function.
  73. *
  74. * This function will however call the receive completion callback
  75. * in case a response buffer is still available before resetting
  76. * the pointer.
  77. */
  78. static void
  79. mwifiex_clean_cmd_node(struct mwifiex_adapter *adapter,
  80. struct cmd_ctrl_node *cmd_node)
  81. {
  82. cmd_node->cmd_oid = 0;
  83. cmd_node->cmd_flag = 0;
  84. cmd_node->data_buf = NULL;
  85. cmd_node->wait_q_enabled = false;
  86. if (cmd_node->resp_skb) {
  87. dev_kfree_skb_any(cmd_node->resp_skb);
  88. cmd_node->resp_skb = NULL;
  89. }
  90. }
  91. /*
  92. * This function sends a host command to the firmware.
  93. *
  94. * The function copies the host command into the driver command
  95. * buffer, which will be transferred to the firmware later by the
  96. * main thread.
  97. */
  98. static int mwifiex_cmd_host_cmd(struct mwifiex_private *priv,
  99. struct host_cmd_ds_command *cmd,
  100. struct mwifiex_ds_misc_cmd *pcmd_ptr)
  101. {
  102. /* Copy the HOST command to command buffer */
  103. memcpy(cmd, pcmd_ptr->cmd, pcmd_ptr->len);
  104. dev_dbg(priv->adapter->dev, "cmd: host cmd size = %d\n", pcmd_ptr->len);
  105. return 0;
  106. }
  107. /*
  108. * This function downloads a command to the firmware.
  109. *
  110. * The function performs sanity tests, sets the command sequence
  111. * number and size, converts the header fields to CPU format before
  112. * sending. Afterwards, it logs the command ID and action for debugging
  113. * and sets up the command timeout timer.
  114. */
  115. static int mwifiex_dnld_cmd_to_fw(struct mwifiex_private *priv,
  116. struct cmd_ctrl_node *cmd_node)
  117. {
  118. struct mwifiex_adapter *adapter = priv->adapter;
  119. int ret;
  120. struct host_cmd_ds_command *host_cmd;
  121. uint16_t cmd_code;
  122. uint16_t cmd_size;
  123. struct timeval tstamp;
  124. unsigned long flags;
  125. if (!adapter || !cmd_node)
  126. return -1;
  127. host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  128. /* Sanity test */
  129. if (host_cmd == NULL || host_cmd->size == 0) {
  130. dev_err(adapter->dev, "DNLD_CMD: host_cmd is null"
  131. " or cmd size is 0, not sending\n");
  132. if (cmd_node->wait_q_enabled)
  133. adapter->cmd_wait_q.status = -1;
  134. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  135. return -1;
  136. }
  137. /* Set command sequence number */
  138. adapter->seq_num++;
  139. host_cmd->seq_num = cpu_to_le16(HostCmd_SET_SEQ_NO_BSS_INFO
  140. (adapter->seq_num, cmd_node->priv->bss_num,
  141. cmd_node->priv->bss_type));
  142. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  143. adapter->curr_cmd = cmd_node;
  144. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  145. cmd_code = le16_to_cpu(host_cmd->command);
  146. cmd_size = le16_to_cpu(host_cmd->size);
  147. skb_trim(cmd_node->cmd_skb, cmd_size);
  148. do_gettimeofday(&tstamp);
  149. dev_dbg(adapter->dev, "cmd: DNLD_CMD: (%lu.%lu): %#x, act %#x, len %d,"
  150. " seqno %#x\n",
  151. tstamp.tv_sec, tstamp.tv_usec, cmd_code,
  152. le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN)), cmd_size,
  153. le16_to_cpu(host_cmd->seq_num));
  154. skb_push(cmd_node->cmd_skb, INTF_HEADER_LEN);
  155. ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
  156. cmd_node->cmd_skb->data,
  157. cmd_node->cmd_skb->len, NULL);
  158. skb_pull(cmd_node->cmd_skb, INTF_HEADER_LEN);
  159. if (ret == -1) {
  160. dev_err(adapter->dev, "DNLD_CMD: host to card failed\n");
  161. if (cmd_node->wait_q_enabled)
  162. adapter->cmd_wait_q.status = -1;
  163. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  164. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  165. adapter->curr_cmd = NULL;
  166. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  167. adapter->dbg.num_cmd_host_to_card_failure++;
  168. return -1;
  169. }
  170. /* Save the last command id and action to debug log */
  171. adapter->dbg.last_cmd_index =
  172. (adapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
  173. adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index] = cmd_code;
  174. adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index] =
  175. le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN));
  176. /* Clear BSS_NO_BITS from HostCmd */
  177. cmd_code &= HostCmd_CMD_ID_MASK;
  178. /* Setup the timer after transmit command */
  179. mod_timer(&adapter->cmd_timer,
  180. jiffies + (MWIFIEX_TIMER_10S * HZ) / 1000);
  181. return 0;
  182. }
  183. /*
  184. * This function downloads a sleep confirm command to the firmware.
  185. *
  186. * The function performs sanity tests, sets the command sequence
  187. * number and size, converts the header fields to CPU format before
  188. * sending.
  189. *
  190. * No responses are needed for sleep confirm command.
  191. */
  192. static int mwifiex_dnld_sleep_confirm_cmd(struct mwifiex_adapter *adapter)
  193. {
  194. int ret;
  195. struct mwifiex_private *priv;
  196. struct mwifiex_opt_sleep_confirm *sleep_cfm_buf =
  197. (struct mwifiex_opt_sleep_confirm *)
  198. adapter->sleep_cfm->data;
  199. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  200. sleep_cfm_buf->seq_num =
  201. cpu_to_le16((HostCmd_SET_SEQ_NO_BSS_INFO
  202. (adapter->seq_num, priv->bss_num,
  203. priv->bss_type)));
  204. adapter->seq_num++;
  205. skb_push(adapter->sleep_cfm, INTF_HEADER_LEN);
  206. ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
  207. adapter->sleep_cfm->data,
  208. adapter->sleep_cfm->len, NULL);
  209. skb_pull(adapter->sleep_cfm, INTF_HEADER_LEN);
  210. if (ret == -1) {
  211. dev_err(adapter->dev, "SLEEP_CFM: failed\n");
  212. adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure++;
  213. return -1;
  214. }
  215. if (GET_BSS_ROLE(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY))
  216. == MWIFIEX_BSS_ROLE_STA) {
  217. if (!sleep_cfm_buf->resp_ctrl)
  218. /* Response is not needed for sleep
  219. confirm command */
  220. adapter->ps_state = PS_STATE_SLEEP;
  221. else
  222. adapter->ps_state = PS_STATE_SLEEP_CFM;
  223. if (!sleep_cfm_buf->resp_ctrl
  224. && (adapter->is_hs_configured
  225. && !adapter->sleep_period.period)) {
  226. adapter->pm_wakeup_card_req = true;
  227. mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
  228. MWIFIEX_BSS_ROLE_STA), true);
  229. }
  230. }
  231. return ret;
  232. }
  233. /*
  234. * This function allocates the command buffers and links them to
  235. * the command free queue.
  236. *
  237. * The driver uses a pre allocated number of command buffers, which
  238. * are created at driver initializations and freed at driver cleanup.
  239. * Every command needs to obtain a command buffer from this pool before
  240. * it can be issued. The command free queue lists the command buffers
  241. * currently free to use, while the command pending queue lists the
  242. * command buffers already in use and awaiting handling. Command buffers
  243. * are returned to the free queue after use.
  244. */
  245. int mwifiex_alloc_cmd_buffer(struct mwifiex_adapter *adapter)
  246. {
  247. struct cmd_ctrl_node *cmd_array;
  248. u32 buf_size;
  249. u32 i;
  250. /* Allocate and initialize struct cmd_ctrl_node */
  251. buf_size = sizeof(struct cmd_ctrl_node) * MWIFIEX_NUM_OF_CMD_BUFFER;
  252. cmd_array = kzalloc(buf_size, GFP_KERNEL);
  253. if (!cmd_array) {
  254. dev_err(adapter->dev, "%s: failed to alloc cmd_array\n",
  255. __func__);
  256. return -ENOMEM;
  257. }
  258. adapter->cmd_pool = cmd_array;
  259. memset(adapter->cmd_pool, 0, buf_size);
  260. /* Allocate and initialize command buffers */
  261. for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
  262. cmd_array[i].skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
  263. if (!cmd_array[i].skb) {
  264. dev_err(adapter->dev, "ALLOC_CMD_BUF: out of memory\n");
  265. return -1;
  266. }
  267. }
  268. for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++)
  269. mwifiex_insert_cmd_to_free_q(adapter, &cmd_array[i]);
  270. return 0;
  271. }
  272. /*
  273. * This function frees the command buffers.
  274. *
  275. * The function calls the completion callback for all the command
  276. * buffers that still have response buffers associated with them.
  277. */
  278. int mwifiex_free_cmd_buffer(struct mwifiex_adapter *adapter)
  279. {
  280. struct cmd_ctrl_node *cmd_array;
  281. u32 i;
  282. /* Need to check if cmd pool is allocated or not */
  283. if (!adapter->cmd_pool) {
  284. dev_dbg(adapter->dev, "info: FREE_CMD_BUF: cmd_pool is null\n");
  285. return 0;
  286. }
  287. cmd_array = adapter->cmd_pool;
  288. /* Release shared memory buffers */
  289. for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
  290. if (cmd_array[i].skb) {
  291. dev_dbg(adapter->dev, "cmd: free cmd buffer %d\n", i);
  292. dev_kfree_skb_any(cmd_array[i].skb);
  293. }
  294. if (!cmd_array[i].resp_skb)
  295. continue;
  296. dev_kfree_skb_any(cmd_array[i].resp_skb);
  297. }
  298. /* Release struct cmd_ctrl_node */
  299. if (adapter->cmd_pool) {
  300. dev_dbg(adapter->dev, "cmd: free cmd pool\n");
  301. kfree(adapter->cmd_pool);
  302. adapter->cmd_pool = NULL;
  303. }
  304. return 0;
  305. }
  306. /*
  307. * This function handles events generated by firmware.
  308. *
  309. * Event body of events received from firmware are not used (though they are
  310. * saved), only the event ID is used. Some events are re-invoked by
  311. * the driver, with a new event body.
  312. *
  313. * After processing, the function calls the completion callback
  314. * for cleanup.
  315. */
  316. int mwifiex_process_event(struct mwifiex_adapter *adapter)
  317. {
  318. int ret;
  319. struct mwifiex_private *priv =
  320. mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  321. struct sk_buff *skb = adapter->event_skb;
  322. u32 eventcause = adapter->event_cause;
  323. struct timeval tstamp;
  324. struct mwifiex_rxinfo *rx_info;
  325. /* Save the last event to debug log */
  326. adapter->dbg.last_event_index =
  327. (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
  328. adapter->dbg.last_event[adapter->dbg.last_event_index] =
  329. (u16) eventcause;
  330. /* Get BSS number and corresponding priv */
  331. priv = mwifiex_get_priv_by_id(adapter, EVENT_GET_BSS_NUM(eventcause),
  332. EVENT_GET_BSS_TYPE(eventcause));
  333. if (!priv)
  334. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  335. /* Clear BSS_NO_BITS from event */
  336. eventcause &= EVENT_ID_MASK;
  337. adapter->event_cause = eventcause;
  338. if (skb) {
  339. rx_info = MWIFIEX_SKB_RXCB(skb);
  340. rx_info->bss_index = priv->bss_index;
  341. }
  342. if (eventcause != EVENT_PS_SLEEP && eventcause != EVENT_PS_AWAKE) {
  343. do_gettimeofday(&tstamp);
  344. dev_dbg(adapter->dev, "event: %lu.%lu: cause: %#x\n",
  345. tstamp.tv_sec, tstamp.tv_usec, eventcause);
  346. }
  347. ret = mwifiex_process_sta_event(priv);
  348. adapter->event_cause = 0;
  349. adapter->event_skb = NULL;
  350. dev_kfree_skb_any(skb);
  351. return ret;
  352. }
  353. /*
  354. * This function is used to send synchronous command to the firmware.
  355. *
  356. * it allocates a wait queue for the command and wait for the command
  357. * response.
  358. */
  359. int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
  360. u16 cmd_action, u32 cmd_oid, void *data_buf)
  361. {
  362. int ret = 0;
  363. struct mwifiex_adapter *adapter = priv->adapter;
  364. adapter->cmd_wait_q_required = true;
  365. adapter->cmd_wait_q.condition = false;
  366. ret = mwifiex_send_cmd_async(priv, cmd_no, cmd_action, cmd_oid,
  367. data_buf);
  368. if (!ret)
  369. ret = mwifiex_wait_queue_complete(adapter);
  370. return ret;
  371. }
  372. /*
  373. * This function prepares a command and asynchronously send it to the firmware.
  374. *
  375. * Preparation includes -
  376. * - Sanity tests to make sure the card is still present or the FW
  377. * is not reset
  378. * - Getting a new command node from the command free queue
  379. * - Initializing the command node for default parameters
  380. * - Fill up the non-default parameters and buffer pointers
  381. * - Add the command to pending queue
  382. */
  383. int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
  384. u16 cmd_action, u32 cmd_oid, void *data_buf)
  385. {
  386. int ret;
  387. struct mwifiex_adapter *adapter = priv->adapter;
  388. struct cmd_ctrl_node *cmd_node;
  389. struct host_cmd_ds_command *cmd_ptr;
  390. if (!adapter) {
  391. pr_err("PREP_CMD: adapter is NULL\n");
  392. return -1;
  393. }
  394. if (adapter->is_suspended) {
  395. dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
  396. return -1;
  397. }
  398. if (adapter->surprise_removed) {
  399. dev_err(adapter->dev, "PREP_CMD: card is removed\n");
  400. return -1;
  401. }
  402. if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
  403. if (cmd_no != HostCmd_CMD_FUNC_INIT) {
  404. dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
  405. return -1;
  406. }
  407. }
  408. /* Get a new command node */
  409. cmd_node = mwifiex_get_cmd_node(adapter);
  410. if (!cmd_node) {
  411. dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
  412. return -1;
  413. }
  414. /* Initialize the command node */
  415. mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
  416. if (!cmd_node->cmd_skb) {
  417. dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
  418. return -1;
  419. }
  420. memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
  421. 0, sizeof(struct host_cmd_ds_command));
  422. cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  423. cmd_ptr->command = cpu_to_le16(cmd_no);
  424. cmd_ptr->result = 0;
  425. /* Prepare command */
  426. if (cmd_no) {
  427. ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
  428. cmd_oid, data_buf, cmd_ptr);
  429. } else {
  430. ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
  431. cmd_node->cmd_flag |= CMD_F_HOSTCMD;
  432. }
  433. /* Return error, since the command preparation failed */
  434. if (ret) {
  435. dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
  436. cmd_no);
  437. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  438. return -1;
  439. }
  440. /* Send command */
  441. if (cmd_no == HostCmd_CMD_802_11_SCAN)
  442. mwifiex_queue_scan_cmd(priv, cmd_node);
  443. else
  444. mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
  445. return ret;
  446. }
  447. /*
  448. * This function returns a command to the command free queue.
  449. *
  450. * The function also calls the completion callback if required, before
  451. * cleaning the command node and re-inserting it into the free queue.
  452. */
  453. void
  454. mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
  455. struct cmd_ctrl_node *cmd_node)
  456. {
  457. unsigned long flags;
  458. if (!cmd_node)
  459. return;
  460. if (cmd_node->wait_q_enabled)
  461. mwifiex_complete_cmd(adapter);
  462. /* Clean the node */
  463. mwifiex_clean_cmd_node(adapter, cmd_node);
  464. /* Insert node into cmd_free_q */
  465. spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
  466. list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
  467. spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
  468. }
  469. /*
  470. * This function queues a command to the command pending queue.
  471. *
  472. * This in effect adds the command to the command list to be executed.
  473. * Exit PS command is handled specially, by placing it always to the
  474. * front of the command queue.
  475. */
  476. void
  477. mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
  478. struct cmd_ctrl_node *cmd_node, u32 add_tail)
  479. {
  480. struct host_cmd_ds_command *host_cmd = NULL;
  481. u16 command;
  482. unsigned long flags;
  483. host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  484. if (!host_cmd) {
  485. dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
  486. return;
  487. }
  488. command = le16_to_cpu(host_cmd->command);
  489. /* Exit_PS command needs to be queued in the header always. */
  490. if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
  491. struct host_cmd_ds_802_11_ps_mode_enh *pm =
  492. &host_cmd->params.psmode_enh;
  493. if ((le16_to_cpu(pm->action) == DIS_PS)
  494. || (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
  495. if (adapter->ps_state != PS_STATE_AWAKE)
  496. add_tail = false;
  497. }
  498. }
  499. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  500. if (add_tail)
  501. list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
  502. else
  503. list_add(&cmd_node->list, &adapter->cmd_pending_q);
  504. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  505. dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
  506. }
  507. /*
  508. * This function executes the next command in command pending queue.
  509. *
  510. * This function will fail if a command is already in processing stage,
  511. * otherwise it will dequeue the first command from the command pending
  512. * queue and send to the firmware.
  513. *
  514. * If the device is currently in host sleep mode, any commands, except the
  515. * host sleep configuration command will de-activate the host sleep. For PS
  516. * mode, the function will put the firmware back to sleep if applicable.
  517. */
  518. int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
  519. {
  520. struct mwifiex_private *priv;
  521. struct cmd_ctrl_node *cmd_node;
  522. int ret = 0;
  523. struct host_cmd_ds_command *host_cmd;
  524. unsigned long cmd_flags;
  525. unsigned long cmd_pending_q_flags;
  526. /* Check if already in processing */
  527. if (adapter->curr_cmd) {
  528. dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
  529. return -1;
  530. }
  531. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  532. /* Check if any command is pending */
  533. spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
  534. if (list_empty(&adapter->cmd_pending_q)) {
  535. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  536. cmd_pending_q_flags);
  537. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  538. return 0;
  539. }
  540. cmd_node = list_first_entry(&adapter->cmd_pending_q,
  541. struct cmd_ctrl_node, list);
  542. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  543. cmd_pending_q_flags);
  544. host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  545. priv = cmd_node->priv;
  546. if (adapter->ps_state != PS_STATE_AWAKE) {
  547. dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
  548. " this should not happen\n", __func__);
  549. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  550. return ret;
  551. }
  552. spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
  553. list_del(&cmd_node->list);
  554. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  555. cmd_pending_q_flags);
  556. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  557. ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
  558. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  559. /* Any command sent to the firmware when host is in sleep
  560. * mode should de-configure host sleep. We should skip the
  561. * host sleep configuration command itself though
  562. */
  563. if (priv && (host_cmd->command !=
  564. cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
  565. if (adapter->hs_activated) {
  566. adapter->is_hs_configured = false;
  567. mwifiex_hs_activated_event(priv, false);
  568. }
  569. }
  570. return ret;
  571. }
  572. /*
  573. * This function handles the command response.
  574. *
  575. * After processing, the function cleans the command node and puts
  576. * it back to the command free queue.
  577. */
  578. int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
  579. {
  580. struct host_cmd_ds_command *resp;
  581. struct mwifiex_private *priv =
  582. mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  583. int ret = 0;
  584. uint16_t orig_cmdresp_no;
  585. uint16_t cmdresp_no;
  586. uint16_t cmdresp_result;
  587. struct timeval tstamp;
  588. unsigned long flags;
  589. /* Now we got response from FW, cancel the command timer */
  590. del_timer(&adapter->cmd_timer);
  591. if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
  592. resp = (struct host_cmd_ds_command *) adapter->upld_buf;
  593. dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
  594. le16_to_cpu(resp->command));
  595. return -1;
  596. }
  597. adapter->num_cmd_timeout = 0;
  598. resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
  599. if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
  600. dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
  601. le16_to_cpu(resp->command));
  602. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  603. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  604. adapter->curr_cmd = NULL;
  605. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  606. return -1;
  607. }
  608. if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
  609. /* Copy original response back to response buffer */
  610. struct mwifiex_ds_misc_cmd *hostcmd;
  611. uint16_t size = le16_to_cpu(resp->size);
  612. dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
  613. size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
  614. if (adapter->curr_cmd->data_buf) {
  615. hostcmd = adapter->curr_cmd->data_buf;
  616. hostcmd->len = size;
  617. memcpy(hostcmd->cmd, resp, size);
  618. }
  619. }
  620. orig_cmdresp_no = le16_to_cpu(resp->command);
  621. /* Get BSS number and corresponding priv */
  622. priv = mwifiex_get_priv_by_id(adapter,
  623. HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
  624. HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
  625. if (!priv)
  626. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  627. /* Clear RET_BIT from HostCmd */
  628. resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
  629. cmdresp_no = le16_to_cpu(resp->command);
  630. cmdresp_result = le16_to_cpu(resp->result);
  631. /* Save the last command response to debug log */
  632. adapter->dbg.last_cmd_resp_index =
  633. (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
  634. adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
  635. orig_cmdresp_no;
  636. do_gettimeofday(&tstamp);
  637. dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
  638. " len %d, seqno 0x%x\n",
  639. tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
  640. le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
  641. if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
  642. dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
  643. if (adapter->curr_cmd->wait_q_enabled)
  644. adapter->cmd_wait_q.status = -1;
  645. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  646. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  647. adapter->curr_cmd = NULL;
  648. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  649. return -1;
  650. }
  651. if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
  652. adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
  653. if ((cmdresp_result == HostCmd_RESULT_OK)
  654. && (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
  655. ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
  656. } else {
  657. /* handle response */
  658. ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
  659. }
  660. /* Check init command response */
  661. if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
  662. if (ret == -1) {
  663. dev_err(adapter->dev, "%s: cmd %#x failed during "
  664. "initialization\n", __func__, cmdresp_no);
  665. mwifiex_init_fw_complete(adapter);
  666. return -1;
  667. } else if (adapter->last_init_cmd == cmdresp_no)
  668. adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
  669. }
  670. if (adapter->curr_cmd) {
  671. if (adapter->curr_cmd->wait_q_enabled && (!ret))
  672. adapter->cmd_wait_q.status = 0;
  673. else if (adapter->curr_cmd->wait_q_enabled && (ret == -1))
  674. adapter->cmd_wait_q.status = -1;
  675. /* Clean up and put current command back to cmd_free_q */
  676. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  677. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  678. adapter->curr_cmd = NULL;
  679. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  680. }
  681. return ret;
  682. }
  683. /*
  684. * This function handles the timeout of command sending.
  685. *
  686. * It will re-send the same command again.
  687. */
  688. void
  689. mwifiex_cmd_timeout_func(unsigned long function_context)
  690. {
  691. struct mwifiex_adapter *adapter =
  692. (struct mwifiex_adapter *) function_context;
  693. struct cmd_ctrl_node *cmd_node;
  694. struct timeval tstamp;
  695. adapter->num_cmd_timeout++;
  696. adapter->dbg.num_cmd_timeout++;
  697. if (!adapter->curr_cmd) {
  698. dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
  699. return;
  700. }
  701. cmd_node = adapter->curr_cmd;
  702. if (cmd_node->wait_q_enabled)
  703. adapter->cmd_wait_q.status = -ETIMEDOUT;
  704. if (cmd_node) {
  705. adapter->dbg.timeout_cmd_id =
  706. adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
  707. adapter->dbg.timeout_cmd_act =
  708. adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
  709. do_gettimeofday(&tstamp);
  710. dev_err(adapter->dev, "%s: Timeout cmd id (%lu.%lu) = %#x,"
  711. " act = %#x\n", __func__,
  712. tstamp.tv_sec, tstamp.tv_usec,
  713. adapter->dbg.timeout_cmd_id,
  714. adapter->dbg.timeout_cmd_act);
  715. dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
  716. adapter->dbg.num_tx_host_to_card_failure);
  717. dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
  718. adapter->dbg.num_cmd_host_to_card_failure);
  719. dev_err(adapter->dev, "num_cmd_timeout = %d\n",
  720. adapter->dbg.num_cmd_timeout);
  721. dev_err(adapter->dev, "num_tx_timeout = %d\n",
  722. adapter->dbg.num_tx_timeout);
  723. dev_err(adapter->dev, "last_cmd_index = %d\n",
  724. adapter->dbg.last_cmd_index);
  725. print_hex_dump_bytes("last_cmd_id: ", DUMP_PREFIX_OFFSET,
  726. adapter->dbg.last_cmd_id, DBG_CMD_NUM);
  727. print_hex_dump_bytes("last_cmd_act: ", DUMP_PREFIX_OFFSET,
  728. adapter->dbg.last_cmd_act, DBG_CMD_NUM);
  729. dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
  730. adapter->dbg.last_cmd_resp_index);
  731. print_hex_dump_bytes("last_cmd_resp_id: ", DUMP_PREFIX_OFFSET,
  732. adapter->dbg.last_cmd_resp_id, DBG_CMD_NUM);
  733. dev_err(adapter->dev, "last_event_index = %d\n",
  734. adapter->dbg.last_event_index);
  735. print_hex_dump_bytes("last_event: ", DUMP_PREFIX_OFFSET,
  736. adapter->dbg.last_event, DBG_CMD_NUM);
  737. dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
  738. adapter->data_sent, adapter->cmd_sent);
  739. dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
  740. adapter->ps_mode, adapter->ps_state);
  741. }
  742. if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
  743. mwifiex_init_fw_complete(adapter);
  744. }
  745. /*
  746. * This function cancels all the pending commands.
  747. *
  748. * The current command, all commands in command pending queue and all scan
  749. * commands in scan pending queue are cancelled. All the completion callbacks
  750. * are called with failure status to ensure cleanup.
  751. */
  752. void
  753. mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
  754. {
  755. struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
  756. unsigned long flags;
  757. /* Cancel current cmd */
  758. if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
  759. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  760. adapter->curr_cmd->wait_q_enabled = false;
  761. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  762. adapter->cmd_wait_q.status = -1;
  763. mwifiex_complete_cmd(adapter);
  764. }
  765. /* Cancel all pending command */
  766. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  767. list_for_each_entry_safe(cmd_node, tmp_node,
  768. &adapter->cmd_pending_q, list) {
  769. list_del(&cmd_node->list);
  770. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  771. if (cmd_node->wait_q_enabled) {
  772. adapter->cmd_wait_q.status = -1;
  773. mwifiex_complete_cmd(adapter);
  774. cmd_node->wait_q_enabled = false;
  775. }
  776. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  777. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  778. }
  779. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  780. /* Cancel all pending scan command */
  781. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  782. list_for_each_entry_safe(cmd_node, tmp_node,
  783. &adapter->scan_pending_q, list) {
  784. list_del(&cmd_node->list);
  785. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  786. cmd_node->wait_q_enabled = false;
  787. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  788. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  789. }
  790. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  791. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  792. adapter->scan_processing = false;
  793. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  794. }
  795. /*
  796. * This function cancels all pending commands that matches with
  797. * the given IOCTL request.
  798. *
  799. * Both the current command buffer and the pending command queue are
  800. * searched for matching IOCTL request. The completion callback of
  801. * the matched command is called with failure status to ensure cleanup.
  802. * In case of scan commands, all pending commands in scan pending queue
  803. * are cancelled.
  804. */
  805. void
  806. mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
  807. {
  808. struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
  809. unsigned long cmd_flags;
  810. unsigned long cmd_pending_q_flags;
  811. unsigned long scan_pending_q_flags;
  812. uint16_t cancel_scan_cmd = false;
  813. if ((adapter->curr_cmd) &&
  814. (adapter->curr_cmd->wait_q_enabled)) {
  815. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  816. cmd_node = adapter->curr_cmd;
  817. cmd_node->wait_q_enabled = false;
  818. cmd_node->cmd_flag |= CMD_F_CANCELED;
  819. spin_lock_irqsave(&adapter->cmd_pending_q_lock,
  820. cmd_pending_q_flags);
  821. list_del(&cmd_node->list);
  822. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  823. cmd_pending_q_flags);
  824. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  825. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  826. }
  827. /* Cancel all pending scan command */
  828. spin_lock_irqsave(&adapter->scan_pending_q_lock,
  829. scan_pending_q_flags);
  830. list_for_each_entry_safe(cmd_node, tmp_node,
  831. &adapter->scan_pending_q, list) {
  832. list_del(&cmd_node->list);
  833. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  834. scan_pending_q_flags);
  835. cmd_node->wait_q_enabled = false;
  836. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  837. spin_lock_irqsave(&adapter->scan_pending_q_lock,
  838. scan_pending_q_flags);
  839. cancel_scan_cmd = true;
  840. }
  841. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  842. scan_pending_q_flags);
  843. if (cancel_scan_cmd) {
  844. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  845. adapter->scan_processing = false;
  846. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  847. }
  848. adapter->cmd_wait_q.status = -1;
  849. mwifiex_complete_cmd(adapter);
  850. }
  851. /*
  852. * This function sends the sleep confirm command to firmware, if
  853. * possible.
  854. *
  855. * The sleep confirm command cannot be issued if command response,
  856. * data response or event response is awaiting handling, or if we
  857. * are in the middle of sending a command, or expecting a command
  858. * response.
  859. */
  860. void
  861. mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
  862. {
  863. if (!adapter->cmd_sent &&
  864. !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
  865. mwifiex_dnld_sleep_confirm_cmd(adapter);
  866. else
  867. dev_dbg(adapter->dev,
  868. "cmd: Delay Sleep Confirm (%s%s%s)\n",
  869. (adapter->cmd_sent) ? "D" : "",
  870. (adapter->curr_cmd) ? "C" : "",
  871. (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
  872. }
  873. /*
  874. * This function sends a Host Sleep activated event to applications.
  875. *
  876. * This event is generated by the driver, with a blank event body.
  877. */
  878. void
  879. mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
  880. {
  881. if (activated) {
  882. if (priv->adapter->is_hs_configured) {
  883. priv->adapter->hs_activated = true;
  884. dev_dbg(priv->adapter->dev, "event: hs_activated\n");
  885. priv->adapter->hs_activate_wait_q_woken = true;
  886. wake_up_interruptible(
  887. &priv->adapter->hs_activate_wait_q);
  888. } else {
  889. dev_dbg(priv->adapter->dev, "event: HS not configured\n");
  890. }
  891. } else {
  892. dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
  893. priv->adapter->hs_activated = false;
  894. }
  895. }
  896. /*
  897. * This function handles the command response of a Host Sleep configuration
  898. * command.
  899. *
  900. * Handling includes changing the header fields into CPU format
  901. * and setting the current host sleep activation status in driver.
  902. *
  903. * In case host sleep status change, the function generates an event to
  904. * notify the applications.
  905. */
  906. int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
  907. struct host_cmd_ds_command *resp)
  908. {
  909. struct mwifiex_adapter *adapter = priv->adapter;
  910. struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
  911. &resp->params.opt_hs_cfg;
  912. uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
  913. if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE)) {
  914. mwifiex_hs_activated_event(priv, true);
  915. return 0;
  916. } else {
  917. dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
  918. " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
  919. resp->result, conditions,
  920. phs_cfg->params.hs_config.gpio,
  921. phs_cfg->params.hs_config.gap);
  922. }
  923. if (conditions != HOST_SLEEP_CFG_CANCEL) {
  924. adapter->is_hs_configured = true;
  925. } else {
  926. adapter->is_hs_configured = false;
  927. if (adapter->hs_activated)
  928. mwifiex_hs_activated_event(priv, false);
  929. }
  930. return 0;
  931. }
  932. /*
  933. * This function wakes up the adapter and generates a Host Sleep
  934. * cancel event on receiving the power up interrupt.
  935. */
  936. void
  937. mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
  938. {
  939. dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
  940. " since there is interrupt from the firmware\n", __func__);
  941. adapter->if_ops.wakeup(adapter);
  942. adapter->hs_activated = false;
  943. adapter->is_hs_configured = false;
  944. mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
  945. MWIFIEX_BSS_ROLE_ANY), false);
  946. }
  947. /*
  948. * This function handles the command response of a sleep confirm command.
  949. *
  950. * The function sets the card state to SLEEP if the response indicates success.
  951. */
  952. void
  953. mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
  954. u8 *pbuf, u32 upld_len)
  955. {
  956. struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
  957. struct mwifiex_private *priv =
  958. mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  959. uint16_t result = le16_to_cpu(cmd->result);
  960. uint16_t command = le16_to_cpu(cmd->command);
  961. uint16_t seq_num = le16_to_cpu(cmd->seq_num);
  962. if (!upld_len) {
  963. dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
  964. return;
  965. }
  966. /* Get BSS number and corresponding priv */
  967. priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
  968. HostCmd_GET_BSS_TYPE(seq_num));
  969. if (!priv)
  970. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  971. /* Update sequence number */
  972. seq_num = HostCmd_GET_SEQ_NO(seq_num);
  973. /* Clear RET_BIT from HostCmd */
  974. command &= HostCmd_CMD_ID_MASK;
  975. if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
  976. dev_err(adapter->dev, "%s: received unexpected response for"
  977. " cmd %x, result = %x\n", __func__, command, result);
  978. return;
  979. }
  980. if (result) {
  981. dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
  982. __func__);
  983. adapter->pm_wakeup_card_req = false;
  984. adapter->ps_state = PS_STATE_AWAKE;
  985. return;
  986. }
  987. adapter->pm_wakeup_card_req = true;
  988. if (adapter->is_hs_configured)
  989. mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
  990. MWIFIEX_BSS_ROLE_ANY), true);
  991. adapter->ps_state = PS_STATE_SLEEP;
  992. cmd->command = cpu_to_le16(command);
  993. cmd->seq_num = cpu_to_le16(seq_num);
  994. }
  995. EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
  996. /*
  997. * This function prepares an enhanced power mode command.
  998. *
  999. * This function can be used to disable power save or to configure
  1000. * power save with auto PS or STA PS or auto deep sleep.
  1001. *
  1002. * Preparation includes -
  1003. * - Setting command ID, action and proper size
  1004. * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
  1005. * auto deep sleep TLV (as required)
  1006. * - Ensuring correct endian-ness
  1007. */
  1008. int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
  1009. struct host_cmd_ds_command *cmd,
  1010. u16 cmd_action, uint16_t ps_bitmap,
  1011. struct mwifiex_ds_auto_ds *auto_ds)
  1012. {
  1013. struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
  1014. &cmd->params.psmode_enh;
  1015. u8 *tlv;
  1016. u16 cmd_size = 0;
  1017. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
  1018. if (cmd_action == DIS_AUTO_PS) {
  1019. psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
  1020. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1021. cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
  1022. sizeof(psmode_enh->params.ps_bitmap));
  1023. } else if (cmd_action == GET_PS) {
  1024. psmode_enh->action = cpu_to_le16(GET_PS);
  1025. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1026. cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
  1027. sizeof(psmode_enh->params.ps_bitmap));
  1028. } else if (cmd_action == EN_AUTO_PS) {
  1029. psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
  1030. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1031. cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
  1032. sizeof(psmode_enh->params.ps_bitmap);
  1033. tlv = (u8 *) cmd + cmd_size;
  1034. if (ps_bitmap & BITMAP_STA_PS) {
  1035. struct mwifiex_adapter *adapter = priv->adapter;
  1036. struct mwifiex_ie_types_ps_param *ps_tlv =
  1037. (struct mwifiex_ie_types_ps_param *) tlv;
  1038. struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
  1039. ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
  1040. ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
  1041. sizeof(struct mwifiex_ie_types_header));
  1042. cmd_size += sizeof(*ps_tlv);
  1043. tlv += sizeof(*ps_tlv);
  1044. dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
  1045. ps_mode->null_pkt_interval =
  1046. cpu_to_le16(adapter->null_pkt_interval);
  1047. ps_mode->multiple_dtims =
  1048. cpu_to_le16(adapter->multiple_dtim);
  1049. ps_mode->bcn_miss_timeout =
  1050. cpu_to_le16(adapter->bcn_miss_time_out);
  1051. ps_mode->local_listen_interval =
  1052. cpu_to_le16(adapter->local_listen_interval);
  1053. ps_mode->adhoc_wake_period =
  1054. cpu_to_le16(adapter->adhoc_awake_period);
  1055. ps_mode->delay_to_ps =
  1056. cpu_to_le16(adapter->delay_to_ps);
  1057. ps_mode->mode =
  1058. cpu_to_le16(adapter->enhanced_ps_mode);
  1059. }
  1060. if (ps_bitmap & BITMAP_AUTO_DS) {
  1061. struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
  1062. (struct mwifiex_ie_types_auto_ds_param *) tlv;
  1063. u16 idletime = 0;
  1064. auto_ds_tlv->header.type =
  1065. cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
  1066. auto_ds_tlv->header.len =
  1067. cpu_to_le16(sizeof(*auto_ds_tlv) -
  1068. sizeof(struct mwifiex_ie_types_header));
  1069. cmd_size += sizeof(*auto_ds_tlv);
  1070. tlv += sizeof(*auto_ds_tlv);
  1071. if (auto_ds)
  1072. idletime = auto_ds->idle_time;
  1073. dev_dbg(priv->adapter->dev,
  1074. "cmd: PS Command: Enter Auto Deep Sleep\n");
  1075. auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
  1076. }
  1077. cmd->size = cpu_to_le16(cmd_size);
  1078. }
  1079. return 0;
  1080. }
  1081. /*
  1082. * This function handles the command response of an enhanced power mode
  1083. * command.
  1084. *
  1085. * Handling includes changing the header fields into CPU format
  1086. * and setting the current enhanced power mode in driver.
  1087. */
  1088. int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
  1089. struct host_cmd_ds_command *resp,
  1090. struct mwifiex_ds_pm_cfg *pm_cfg)
  1091. {
  1092. struct mwifiex_adapter *adapter = priv->adapter;
  1093. struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
  1094. &resp->params.psmode_enh;
  1095. uint16_t action = le16_to_cpu(ps_mode->action);
  1096. uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
  1097. uint16_t auto_ps_bitmap =
  1098. le16_to_cpu(ps_mode->params.ps_bitmap);
  1099. dev_dbg(adapter->dev, "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
  1100. __func__, resp->result, action);
  1101. if (action == EN_AUTO_PS) {
  1102. if (auto_ps_bitmap & BITMAP_AUTO_DS) {
  1103. dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
  1104. priv->adapter->is_deep_sleep = true;
  1105. }
  1106. if (auto_ps_bitmap & BITMAP_STA_PS) {
  1107. dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
  1108. if (adapter->sleep_period.period)
  1109. dev_dbg(adapter->dev, "cmd: set to uapsd/pps mode\n");
  1110. }
  1111. } else if (action == DIS_AUTO_PS) {
  1112. if (ps_bitmap & BITMAP_AUTO_DS) {
  1113. priv->adapter->is_deep_sleep = false;
  1114. dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
  1115. }
  1116. if (ps_bitmap & BITMAP_STA_PS) {
  1117. dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
  1118. if (adapter->sleep_period.period) {
  1119. adapter->delay_null_pkt = false;
  1120. adapter->tx_lock_flag = false;
  1121. adapter->pps_uapsd_mode = false;
  1122. }
  1123. }
  1124. } else if (action == GET_PS) {
  1125. if (ps_bitmap & BITMAP_STA_PS)
  1126. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
  1127. else
  1128. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
  1129. dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
  1130. if (pm_cfg) {
  1131. /* This section is for get power save mode */
  1132. if (ps_bitmap & BITMAP_STA_PS)
  1133. pm_cfg->param.ps_mode = 1;
  1134. else
  1135. pm_cfg->param.ps_mode = 0;
  1136. }
  1137. }
  1138. return 0;
  1139. }
  1140. /*
  1141. * This function prepares command to get hardware specifications.
  1142. *
  1143. * Preparation includes -
  1144. * - Setting command ID, action and proper size
  1145. * - Setting permanent address parameter
  1146. * - Ensuring correct endian-ness
  1147. */
  1148. int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
  1149. struct host_cmd_ds_command *cmd)
  1150. {
  1151. struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
  1152. cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
  1153. cmd->size =
  1154. cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
  1155. memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
  1156. return 0;
  1157. }
  1158. /*
  1159. * This function handles the command response of get hardware
  1160. * specifications.
  1161. *
  1162. * Handling includes changing the header fields into CPU format
  1163. * and saving/updating the following parameters in driver -
  1164. * - Firmware capability information
  1165. * - Firmware band settings
  1166. * - Ad-hoc start band and channel
  1167. * - Ad-hoc 11n activation status
  1168. * - Firmware release number
  1169. * - Number of antennas
  1170. * - Hardware address
  1171. * - Hardware interface version
  1172. * - Firmware version
  1173. * - Region code
  1174. * - 11n capabilities
  1175. * - MCS support fields
  1176. * - MP end port
  1177. */
  1178. int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
  1179. struct host_cmd_ds_command *resp)
  1180. {
  1181. struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
  1182. struct mwifiex_adapter *adapter = priv->adapter;
  1183. int i;
  1184. adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
  1185. if (IS_SUPPORT_MULTI_BANDS(adapter))
  1186. adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
  1187. else
  1188. adapter->fw_bands = BAND_B;
  1189. adapter->config_bands = adapter->fw_bands;
  1190. if (adapter->fw_bands & BAND_A) {
  1191. if (adapter->fw_bands & BAND_GN) {
  1192. adapter->config_bands |= BAND_AN;
  1193. adapter->fw_bands |= BAND_AN;
  1194. }
  1195. if (adapter->fw_bands & BAND_AN) {
  1196. adapter->adhoc_start_band = BAND_A | BAND_AN;
  1197. adapter->adhoc_11n_enabled = true;
  1198. } else {
  1199. adapter->adhoc_start_band = BAND_A;
  1200. }
  1201. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
  1202. } else if (adapter->fw_bands & BAND_GN) {
  1203. adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
  1204. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1205. adapter->adhoc_11n_enabled = true;
  1206. } else if (adapter->fw_bands & BAND_G) {
  1207. adapter->adhoc_start_band = BAND_G | BAND_B;
  1208. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1209. } else if (adapter->fw_bands & BAND_B) {
  1210. adapter->adhoc_start_band = BAND_B;
  1211. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1212. }
  1213. adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
  1214. adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
  1215. dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
  1216. adapter->fw_release_number);
  1217. dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
  1218. hw_spec->permanent_addr);
  1219. dev_dbg(adapter->dev, "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
  1220. le16_to_cpu(hw_spec->hw_if_version),
  1221. le16_to_cpu(hw_spec->version));
  1222. if (priv->curr_addr[0] == 0xff)
  1223. memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
  1224. adapter->region_code = le16_to_cpu(hw_spec->region_code);
  1225. for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
  1226. /* Use the region code to search for the index */
  1227. if (adapter->region_code == region_code_index[i])
  1228. break;
  1229. /* If it's unidentified region code, use the default (USA) */
  1230. if (i >= MWIFIEX_MAX_REGION_CODE) {
  1231. adapter->region_code = 0x10;
  1232. dev_dbg(adapter->dev, "cmd: unknown region code, use default (USA)\n");
  1233. }
  1234. adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
  1235. adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
  1236. if (adapter->if_ops.update_mp_end_port)
  1237. adapter->if_ops.update_mp_end_port(adapter,
  1238. le16_to_cpu(hw_spec->mp_end_port));
  1239. return 0;
  1240. }