cmdevt.c 43 KB

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