cmdevt.c 45 KB

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