cmdevt.c 44 KB

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