cmdevt.c 45 KB

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