cmdevt.c 45 KB

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