cmdevt.c 45 KB

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