cmdevt.c 47 KB

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