cmdevt.c 44 KB

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