cmdevt.c 46 KB

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