cmdevt.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
  412. ret = mwifiex_process_uap_event(priv);
  413. else
  414. ret = mwifiex_process_sta_event(priv);
  415. adapter->event_cause = 0;
  416. adapter->event_skb = NULL;
  417. adapter->if_ops.event_complete(adapter, skb);
  418. return ret;
  419. }
  420. /*
  421. * This function is used to send synchronous command to the firmware.
  422. *
  423. * it allocates a wait queue for the command and wait for the command
  424. * response.
  425. */
  426. int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
  427. u16 cmd_action, u32 cmd_oid, void *data_buf)
  428. {
  429. int ret = 0;
  430. struct mwifiex_adapter *adapter = priv->adapter;
  431. adapter->cmd_wait_q_required = true;
  432. ret = mwifiex_send_cmd_async(priv, cmd_no, cmd_action, cmd_oid,
  433. data_buf);
  434. if (!ret)
  435. ret = mwifiex_wait_queue_complete(adapter);
  436. return ret;
  437. }
  438. /*
  439. * This function prepares a command and asynchronously send it to the firmware.
  440. *
  441. * Preparation includes -
  442. * - Sanity tests to make sure the card is still present or the FW
  443. * is not reset
  444. * - Getting a new command node from the command free queue
  445. * - Initializing the command node for default parameters
  446. * - Fill up the non-default parameters and buffer pointers
  447. * - Add the command to pending queue
  448. */
  449. int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
  450. u16 cmd_action, u32 cmd_oid, void *data_buf)
  451. {
  452. int ret;
  453. struct mwifiex_adapter *adapter = priv->adapter;
  454. struct cmd_ctrl_node *cmd_node;
  455. struct host_cmd_ds_command *cmd_ptr;
  456. if (!adapter) {
  457. pr_err("PREP_CMD: adapter is NULL\n");
  458. return -1;
  459. }
  460. if (adapter->is_suspended) {
  461. dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
  462. return -1;
  463. }
  464. if (adapter->surprise_removed) {
  465. dev_err(adapter->dev, "PREP_CMD: card is removed\n");
  466. return -1;
  467. }
  468. if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
  469. if (cmd_no != HostCmd_CMD_FUNC_INIT) {
  470. dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
  471. return -1;
  472. }
  473. }
  474. /* Get a new command node */
  475. cmd_node = mwifiex_get_cmd_node(adapter);
  476. if (!cmd_node) {
  477. dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
  478. return -1;
  479. }
  480. /* Initialize the command node */
  481. mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
  482. if (!cmd_node->cmd_skb) {
  483. dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
  484. return -1;
  485. }
  486. memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
  487. 0, sizeof(struct host_cmd_ds_command));
  488. cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  489. cmd_ptr->command = cpu_to_le16(cmd_no);
  490. cmd_ptr->result = 0;
  491. /* Prepare command */
  492. if (cmd_no) {
  493. switch (cmd_no) {
  494. case HostCmd_CMD_UAP_SYS_CONFIG:
  495. case HostCmd_CMD_UAP_BSS_START:
  496. case HostCmd_CMD_UAP_BSS_STOP:
  497. ret = mwifiex_uap_prepare_cmd(priv, cmd_no, cmd_action,
  498. cmd_oid, data_buf,
  499. cmd_ptr);
  500. break;
  501. default:
  502. ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
  503. cmd_oid, data_buf,
  504. cmd_ptr);
  505. break;
  506. }
  507. } else {
  508. ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
  509. cmd_node->cmd_flag |= CMD_F_HOSTCMD;
  510. }
  511. /* Return error, since the command preparation failed */
  512. if (ret) {
  513. dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
  514. cmd_no);
  515. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  516. return -1;
  517. }
  518. /* Send command */
  519. if (cmd_no == HostCmd_CMD_802_11_SCAN) {
  520. mwifiex_queue_scan_cmd(priv, cmd_node);
  521. } else {
  522. adapter->cmd_queued = cmd_node;
  523. mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
  524. queue_work(adapter->workqueue, &adapter->main_work);
  525. }
  526. return ret;
  527. }
  528. /*
  529. * This function returns a command to the command free queue.
  530. *
  531. * The function also calls the completion callback if required, before
  532. * cleaning the command node and re-inserting it into the free queue.
  533. */
  534. void
  535. mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
  536. struct cmd_ctrl_node *cmd_node)
  537. {
  538. unsigned long flags;
  539. if (!cmd_node)
  540. return;
  541. if (cmd_node->wait_q_enabled)
  542. mwifiex_complete_cmd(adapter, cmd_node);
  543. /* Clean the node */
  544. mwifiex_clean_cmd_node(adapter, cmd_node);
  545. /* Insert node into cmd_free_q */
  546. spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
  547. list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
  548. spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
  549. }
  550. /*
  551. * This function queues a command to the command pending queue.
  552. *
  553. * This in effect adds the command to the command list to be executed.
  554. * Exit PS command is handled specially, by placing it always to the
  555. * front of the command queue.
  556. */
  557. void
  558. mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
  559. struct cmd_ctrl_node *cmd_node, u32 add_tail)
  560. {
  561. struct host_cmd_ds_command *host_cmd = NULL;
  562. u16 command;
  563. unsigned long flags;
  564. host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  565. if (!host_cmd) {
  566. dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
  567. return;
  568. }
  569. command = le16_to_cpu(host_cmd->command);
  570. /* Exit_PS command needs to be queued in the header always. */
  571. if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
  572. struct host_cmd_ds_802_11_ps_mode_enh *pm =
  573. &host_cmd->params.psmode_enh;
  574. if ((le16_to_cpu(pm->action) == DIS_PS) ||
  575. (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
  576. if (adapter->ps_state != PS_STATE_AWAKE)
  577. add_tail = false;
  578. }
  579. }
  580. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  581. if (add_tail)
  582. list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
  583. else
  584. list_add(&cmd_node->list, &adapter->cmd_pending_q);
  585. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  586. dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
  587. }
  588. /*
  589. * This function executes the next command in command pending queue.
  590. *
  591. * This function will fail if a command is already in processing stage,
  592. * otherwise it will dequeue the first command from the command pending
  593. * queue and send to the firmware.
  594. *
  595. * If the device is currently in host sleep mode, any commands, except the
  596. * host sleep configuration command will de-activate the host sleep. For PS
  597. * mode, the function will put the firmware back to sleep if applicable.
  598. */
  599. int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
  600. {
  601. struct mwifiex_private *priv;
  602. struct cmd_ctrl_node *cmd_node;
  603. int ret = 0;
  604. struct host_cmd_ds_command *host_cmd;
  605. unsigned long cmd_flags;
  606. unsigned long cmd_pending_q_flags;
  607. /* Check if already in processing */
  608. if (adapter->curr_cmd) {
  609. dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
  610. return -1;
  611. }
  612. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  613. /* Check if any command is pending */
  614. spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
  615. if (list_empty(&adapter->cmd_pending_q)) {
  616. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  617. cmd_pending_q_flags);
  618. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  619. return 0;
  620. }
  621. cmd_node = list_first_entry(&adapter->cmd_pending_q,
  622. struct cmd_ctrl_node, list);
  623. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  624. cmd_pending_q_flags);
  625. host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  626. priv = cmd_node->priv;
  627. if (adapter->ps_state != PS_STATE_AWAKE) {
  628. dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
  629. " this should not happen\n", __func__);
  630. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  631. return ret;
  632. }
  633. spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
  634. list_del(&cmd_node->list);
  635. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  636. cmd_pending_q_flags);
  637. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  638. ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
  639. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  640. /* Any command sent to the firmware when host is in sleep
  641. * mode should de-configure host sleep. We should skip the
  642. * host sleep configuration command itself though
  643. */
  644. if (priv && (host_cmd->command !=
  645. cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
  646. if (adapter->hs_activated) {
  647. adapter->is_hs_configured = false;
  648. mwifiex_hs_activated_event(priv, false);
  649. }
  650. }
  651. return ret;
  652. }
  653. /*
  654. * This function handles the command response.
  655. *
  656. * After processing, the function cleans the command node and puts
  657. * it back to the command free queue.
  658. */
  659. int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
  660. {
  661. struct host_cmd_ds_command *resp;
  662. struct mwifiex_private *priv =
  663. mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  664. int ret = 0;
  665. uint16_t orig_cmdresp_no;
  666. uint16_t cmdresp_no;
  667. uint16_t cmdresp_result;
  668. struct timeval tstamp;
  669. unsigned long flags;
  670. /* Now we got response from FW, cancel the command timer */
  671. del_timer(&adapter->cmd_timer);
  672. if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
  673. resp = (struct host_cmd_ds_command *) adapter->upld_buf;
  674. dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
  675. le16_to_cpu(resp->command));
  676. return -1;
  677. }
  678. adapter->num_cmd_timeout = 0;
  679. resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
  680. if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
  681. dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
  682. le16_to_cpu(resp->command));
  683. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  684. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  685. adapter->curr_cmd = NULL;
  686. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  687. return -1;
  688. }
  689. if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
  690. /* Copy original response back to response buffer */
  691. struct mwifiex_ds_misc_cmd *hostcmd;
  692. uint16_t size = le16_to_cpu(resp->size);
  693. dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
  694. size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
  695. if (adapter->curr_cmd->data_buf) {
  696. hostcmd = adapter->curr_cmd->data_buf;
  697. hostcmd->len = size;
  698. memcpy(hostcmd->cmd, resp, size);
  699. }
  700. }
  701. orig_cmdresp_no = le16_to_cpu(resp->command);
  702. /* Get BSS number and corresponding priv */
  703. priv = mwifiex_get_priv_by_id(adapter,
  704. HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
  705. HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
  706. if (!priv)
  707. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  708. /* Clear RET_BIT from HostCmd */
  709. resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
  710. cmdresp_no = le16_to_cpu(resp->command);
  711. cmdresp_result = le16_to_cpu(resp->result);
  712. /* Save the last command response to debug log */
  713. adapter->dbg.last_cmd_resp_index =
  714. (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
  715. adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
  716. orig_cmdresp_no;
  717. do_gettimeofday(&tstamp);
  718. dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
  719. " len %d, seqno 0x%x\n",
  720. tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
  721. le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
  722. if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
  723. dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
  724. if (adapter->curr_cmd->wait_q_enabled)
  725. adapter->cmd_wait_q.status = -1;
  726. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  727. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  728. adapter->curr_cmd = NULL;
  729. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  730. return -1;
  731. }
  732. if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
  733. adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
  734. if ((cmdresp_result == HostCmd_RESULT_OK) &&
  735. (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
  736. ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
  737. } else {
  738. /* handle response */
  739. ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
  740. }
  741. /* Check init command response */
  742. if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
  743. if (ret) {
  744. dev_err(adapter->dev, "%s: cmd %#x failed during "
  745. "initialization\n", __func__, cmdresp_no);
  746. mwifiex_init_fw_complete(adapter);
  747. return -1;
  748. } else if (adapter->last_init_cmd == cmdresp_no)
  749. adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
  750. }
  751. if (adapter->curr_cmd) {
  752. if (adapter->curr_cmd->wait_q_enabled)
  753. adapter->cmd_wait_q.status = ret;
  754. /* Clean up and put current command back to cmd_free_q */
  755. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  756. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  757. adapter->curr_cmd = NULL;
  758. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  759. }
  760. return ret;
  761. }
  762. /*
  763. * This function handles the timeout of command sending.
  764. *
  765. * It will re-send the same command again.
  766. */
  767. void
  768. mwifiex_cmd_timeout_func(unsigned long function_context)
  769. {
  770. struct mwifiex_adapter *adapter =
  771. (struct mwifiex_adapter *) function_context;
  772. struct cmd_ctrl_node *cmd_node;
  773. struct timeval tstamp;
  774. adapter->num_cmd_timeout++;
  775. adapter->dbg.num_cmd_timeout++;
  776. if (!adapter->curr_cmd) {
  777. dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
  778. return;
  779. }
  780. cmd_node = adapter->curr_cmd;
  781. if (cmd_node) {
  782. adapter->dbg.timeout_cmd_id =
  783. adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
  784. adapter->dbg.timeout_cmd_act =
  785. adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
  786. do_gettimeofday(&tstamp);
  787. dev_err(adapter->dev,
  788. "%s: Timeout cmd id (%lu.%lu) = %#x, act = %#x\n",
  789. __func__, tstamp.tv_sec, tstamp.tv_usec,
  790. adapter->dbg.timeout_cmd_id,
  791. adapter->dbg.timeout_cmd_act);
  792. dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
  793. adapter->dbg.num_tx_host_to_card_failure);
  794. dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
  795. adapter->dbg.num_cmd_host_to_card_failure);
  796. dev_err(adapter->dev, "num_cmd_timeout = %d\n",
  797. adapter->dbg.num_cmd_timeout);
  798. dev_err(adapter->dev, "num_tx_timeout = %d\n",
  799. adapter->dbg.num_tx_timeout);
  800. dev_err(adapter->dev, "last_cmd_index = %d\n",
  801. adapter->dbg.last_cmd_index);
  802. print_hex_dump_bytes("last_cmd_id: ", DUMP_PREFIX_OFFSET,
  803. adapter->dbg.last_cmd_id, DBG_CMD_NUM);
  804. print_hex_dump_bytes("last_cmd_act: ", DUMP_PREFIX_OFFSET,
  805. adapter->dbg.last_cmd_act, DBG_CMD_NUM);
  806. dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
  807. adapter->dbg.last_cmd_resp_index);
  808. print_hex_dump_bytes("last_cmd_resp_id: ", DUMP_PREFIX_OFFSET,
  809. adapter->dbg.last_cmd_resp_id,
  810. DBG_CMD_NUM);
  811. dev_err(adapter->dev, "last_event_index = %d\n",
  812. adapter->dbg.last_event_index);
  813. print_hex_dump_bytes("last_event: ", DUMP_PREFIX_OFFSET,
  814. adapter->dbg.last_event, DBG_CMD_NUM);
  815. dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
  816. adapter->data_sent, adapter->cmd_sent);
  817. dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
  818. adapter->ps_mode, adapter->ps_state);
  819. if (cmd_node->wait_q_enabled) {
  820. adapter->cmd_wait_q.status = -ETIMEDOUT;
  821. wake_up_interruptible(&adapter->cmd_wait_q.wait);
  822. mwifiex_cancel_pending_ioctl(adapter);
  823. /* reset cmd_sent flag to unblock new commands */
  824. adapter->cmd_sent = false;
  825. }
  826. }
  827. if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
  828. mwifiex_init_fw_complete(adapter);
  829. }
  830. /*
  831. * This function cancels all the pending commands.
  832. *
  833. * The current command, all commands in command pending queue and all scan
  834. * commands in scan pending queue are cancelled. All the completion callbacks
  835. * are called with failure status to ensure cleanup.
  836. */
  837. void
  838. mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
  839. {
  840. struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
  841. unsigned long flags;
  842. /* Cancel current cmd */
  843. if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
  844. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  845. adapter->curr_cmd->wait_q_enabled = false;
  846. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  847. adapter->cmd_wait_q.status = -1;
  848. mwifiex_complete_cmd(adapter, adapter->curr_cmd);
  849. }
  850. /* Cancel all pending command */
  851. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  852. list_for_each_entry_safe(cmd_node, tmp_node,
  853. &adapter->cmd_pending_q, list) {
  854. list_del(&cmd_node->list);
  855. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  856. if (cmd_node->wait_q_enabled) {
  857. adapter->cmd_wait_q.status = -1;
  858. mwifiex_complete_cmd(adapter, cmd_node);
  859. cmd_node->wait_q_enabled = false;
  860. }
  861. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  862. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  863. }
  864. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  865. /* Cancel all pending scan command */
  866. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  867. list_for_each_entry_safe(cmd_node, tmp_node,
  868. &adapter->scan_pending_q, list) {
  869. list_del(&cmd_node->list);
  870. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  871. cmd_node->wait_q_enabled = false;
  872. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  873. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  874. }
  875. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  876. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  877. adapter->scan_processing = false;
  878. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  879. }
  880. /*
  881. * This function cancels all pending commands that matches with
  882. * the given IOCTL request.
  883. *
  884. * Both the current command buffer and the pending command queue are
  885. * searched for matching IOCTL request. The completion callback of
  886. * the matched command is called with failure status to ensure cleanup.
  887. * In case of scan commands, all pending commands in scan pending queue
  888. * are cancelled.
  889. */
  890. void
  891. mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
  892. {
  893. struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
  894. unsigned long cmd_flags;
  895. unsigned long scan_pending_q_flags;
  896. uint16_t cancel_scan_cmd = false;
  897. if ((adapter->curr_cmd) &&
  898. (adapter->curr_cmd->wait_q_enabled)) {
  899. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  900. cmd_node = adapter->curr_cmd;
  901. cmd_node->wait_q_enabled = false;
  902. cmd_node->cmd_flag |= CMD_F_CANCELED;
  903. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  904. mwifiex_complete_cmd(adapter, adapter->curr_cmd);
  905. adapter->curr_cmd = NULL;
  906. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  907. }
  908. /* Cancel all pending scan command */
  909. spin_lock_irqsave(&adapter->scan_pending_q_lock,
  910. scan_pending_q_flags);
  911. list_for_each_entry_safe(cmd_node, tmp_node,
  912. &adapter->scan_pending_q, list) {
  913. list_del(&cmd_node->list);
  914. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  915. scan_pending_q_flags);
  916. cmd_node->wait_q_enabled = false;
  917. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  918. spin_lock_irqsave(&adapter->scan_pending_q_lock,
  919. scan_pending_q_flags);
  920. cancel_scan_cmd = true;
  921. }
  922. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  923. scan_pending_q_flags);
  924. if (cancel_scan_cmd) {
  925. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  926. adapter->scan_processing = false;
  927. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  928. }
  929. adapter->cmd_wait_q.status = -1;
  930. }
  931. /*
  932. * This function sends the sleep confirm command to firmware, if
  933. * possible.
  934. *
  935. * The sleep confirm command cannot be issued if command response,
  936. * data response or event response is awaiting handling, or if we
  937. * are in the middle of sending a command, or expecting a command
  938. * response.
  939. */
  940. void
  941. mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
  942. {
  943. if (!adapter->cmd_sent &&
  944. !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
  945. mwifiex_dnld_sleep_confirm_cmd(adapter);
  946. else
  947. dev_dbg(adapter->dev,
  948. "cmd: Delay Sleep Confirm (%s%s%s)\n",
  949. (adapter->cmd_sent) ? "D" : "",
  950. (adapter->curr_cmd) ? "C" : "",
  951. (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
  952. }
  953. /*
  954. * This function sends a Host Sleep activated event to applications.
  955. *
  956. * This event is generated by the driver, with a blank event body.
  957. */
  958. void
  959. mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
  960. {
  961. if (activated) {
  962. if (priv->adapter->is_hs_configured) {
  963. priv->adapter->hs_activated = true;
  964. mwifiex_update_rxreor_flags(priv->adapter,
  965. RXREOR_FORCE_NO_DROP);
  966. dev_dbg(priv->adapter->dev, "event: hs_activated\n");
  967. priv->adapter->hs_activate_wait_q_woken = true;
  968. wake_up_interruptible(
  969. &priv->adapter->hs_activate_wait_q);
  970. } else {
  971. dev_dbg(priv->adapter->dev, "event: HS not configured\n");
  972. }
  973. } else {
  974. dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
  975. priv->adapter->hs_activated = false;
  976. }
  977. }
  978. /*
  979. * This function handles the command response of a Host Sleep configuration
  980. * command.
  981. *
  982. * Handling includes changing the header fields into CPU format
  983. * and setting the current host sleep activation status in driver.
  984. *
  985. * In case host sleep status change, the function generates an event to
  986. * notify the applications.
  987. */
  988. int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
  989. struct host_cmd_ds_command *resp)
  990. {
  991. struct mwifiex_adapter *adapter = priv->adapter;
  992. struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
  993. &resp->params.opt_hs_cfg;
  994. uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
  995. if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
  996. adapter->iface_type == MWIFIEX_SDIO) {
  997. mwifiex_hs_activated_event(priv, true);
  998. return 0;
  999. } else {
  1000. dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
  1001. " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
  1002. resp->result, conditions,
  1003. phs_cfg->params.hs_config.gpio,
  1004. phs_cfg->params.hs_config.gap);
  1005. }
  1006. if (conditions != HOST_SLEEP_CFG_CANCEL) {
  1007. adapter->is_hs_configured = true;
  1008. if (adapter->iface_type == MWIFIEX_USB ||
  1009. adapter->iface_type == MWIFIEX_PCIE)
  1010. mwifiex_hs_activated_event(priv, true);
  1011. } else {
  1012. adapter->is_hs_configured = false;
  1013. if (adapter->hs_activated)
  1014. mwifiex_hs_activated_event(priv, false);
  1015. }
  1016. return 0;
  1017. }
  1018. /*
  1019. * This function wakes up the adapter and generates a Host Sleep
  1020. * cancel event on receiving the power up interrupt.
  1021. */
  1022. void
  1023. mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
  1024. {
  1025. dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
  1026. " since there is interrupt from the firmware\n", __func__);
  1027. adapter->if_ops.wakeup(adapter);
  1028. adapter->hs_activated = false;
  1029. adapter->is_hs_configured = false;
  1030. mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
  1031. MWIFIEX_BSS_ROLE_ANY),
  1032. false);
  1033. }
  1034. EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
  1035. /*
  1036. * This function handles the command response of a sleep confirm command.
  1037. *
  1038. * The function sets the card state to SLEEP if the response indicates success.
  1039. */
  1040. void
  1041. mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
  1042. u8 *pbuf, u32 upld_len)
  1043. {
  1044. struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
  1045. struct mwifiex_private *priv =
  1046. mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  1047. uint16_t result = le16_to_cpu(cmd->result);
  1048. uint16_t command = le16_to_cpu(cmd->command);
  1049. uint16_t seq_num = le16_to_cpu(cmd->seq_num);
  1050. if (!upld_len) {
  1051. dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
  1052. return;
  1053. }
  1054. /* Get BSS number and corresponding priv */
  1055. priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
  1056. HostCmd_GET_BSS_TYPE(seq_num));
  1057. if (!priv)
  1058. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  1059. /* Update sequence number */
  1060. seq_num = HostCmd_GET_SEQ_NO(seq_num);
  1061. /* Clear RET_BIT from HostCmd */
  1062. command &= HostCmd_CMD_ID_MASK;
  1063. if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
  1064. dev_err(adapter->dev,
  1065. "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
  1066. __func__, command, result);
  1067. return;
  1068. }
  1069. if (result) {
  1070. dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
  1071. __func__);
  1072. adapter->pm_wakeup_card_req = false;
  1073. adapter->ps_state = PS_STATE_AWAKE;
  1074. return;
  1075. }
  1076. adapter->pm_wakeup_card_req = true;
  1077. if (adapter->is_hs_configured)
  1078. mwifiex_hs_activated_event(mwifiex_get_priv
  1079. (adapter, MWIFIEX_BSS_ROLE_ANY),
  1080. true);
  1081. adapter->ps_state = PS_STATE_SLEEP;
  1082. cmd->command = cpu_to_le16(command);
  1083. cmd->seq_num = cpu_to_le16(seq_num);
  1084. }
  1085. EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
  1086. /*
  1087. * This function prepares an enhanced power mode command.
  1088. *
  1089. * This function can be used to disable power save or to configure
  1090. * power save with auto PS or STA PS or auto deep sleep.
  1091. *
  1092. * Preparation includes -
  1093. * - Setting command ID, action and proper size
  1094. * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
  1095. * auto deep sleep TLV (as required)
  1096. * - Ensuring correct endian-ness
  1097. */
  1098. int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
  1099. struct host_cmd_ds_command *cmd,
  1100. u16 cmd_action, uint16_t ps_bitmap,
  1101. struct mwifiex_ds_auto_ds *auto_ds)
  1102. {
  1103. struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
  1104. &cmd->params.psmode_enh;
  1105. u8 *tlv;
  1106. u16 cmd_size = 0;
  1107. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
  1108. if (cmd_action == DIS_AUTO_PS) {
  1109. psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
  1110. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1111. cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
  1112. sizeof(psmode_enh->params.ps_bitmap));
  1113. } else if (cmd_action == GET_PS) {
  1114. psmode_enh->action = cpu_to_le16(GET_PS);
  1115. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1116. cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
  1117. sizeof(psmode_enh->params.ps_bitmap));
  1118. } else if (cmd_action == EN_AUTO_PS) {
  1119. psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
  1120. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1121. cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
  1122. sizeof(psmode_enh->params.ps_bitmap);
  1123. tlv = (u8 *) cmd + cmd_size;
  1124. if (ps_bitmap & BITMAP_STA_PS) {
  1125. struct mwifiex_adapter *adapter = priv->adapter;
  1126. struct mwifiex_ie_types_ps_param *ps_tlv =
  1127. (struct mwifiex_ie_types_ps_param *) tlv;
  1128. struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
  1129. ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
  1130. ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
  1131. sizeof(struct mwifiex_ie_types_header));
  1132. cmd_size += sizeof(*ps_tlv);
  1133. tlv += sizeof(*ps_tlv);
  1134. dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
  1135. ps_mode->null_pkt_interval =
  1136. cpu_to_le16(adapter->null_pkt_interval);
  1137. ps_mode->multiple_dtims =
  1138. cpu_to_le16(adapter->multiple_dtim);
  1139. ps_mode->bcn_miss_timeout =
  1140. cpu_to_le16(adapter->bcn_miss_time_out);
  1141. ps_mode->local_listen_interval =
  1142. cpu_to_le16(adapter->local_listen_interval);
  1143. ps_mode->adhoc_wake_period =
  1144. cpu_to_le16(adapter->adhoc_awake_period);
  1145. ps_mode->delay_to_ps =
  1146. cpu_to_le16(adapter->delay_to_ps);
  1147. ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
  1148. }
  1149. if (ps_bitmap & BITMAP_AUTO_DS) {
  1150. struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
  1151. (struct mwifiex_ie_types_auto_ds_param *) tlv;
  1152. u16 idletime = 0;
  1153. auto_ds_tlv->header.type =
  1154. cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
  1155. auto_ds_tlv->header.len =
  1156. cpu_to_le16(sizeof(*auto_ds_tlv) -
  1157. sizeof(struct mwifiex_ie_types_header));
  1158. cmd_size += sizeof(*auto_ds_tlv);
  1159. tlv += sizeof(*auto_ds_tlv);
  1160. if (auto_ds)
  1161. idletime = auto_ds->idle_time;
  1162. dev_dbg(priv->adapter->dev,
  1163. "cmd: PS Command: Enter Auto Deep Sleep\n");
  1164. auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
  1165. }
  1166. cmd->size = cpu_to_le16(cmd_size);
  1167. }
  1168. return 0;
  1169. }
  1170. /*
  1171. * This function handles the command response of an enhanced power mode
  1172. * command.
  1173. *
  1174. * Handling includes changing the header fields into CPU format
  1175. * and setting the current enhanced power mode in driver.
  1176. */
  1177. int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
  1178. struct host_cmd_ds_command *resp,
  1179. struct mwifiex_ds_pm_cfg *pm_cfg)
  1180. {
  1181. struct mwifiex_adapter *adapter = priv->adapter;
  1182. struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
  1183. &resp->params.psmode_enh;
  1184. uint16_t action = le16_to_cpu(ps_mode->action);
  1185. uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
  1186. uint16_t auto_ps_bitmap =
  1187. le16_to_cpu(ps_mode->params.ps_bitmap);
  1188. dev_dbg(adapter->dev,
  1189. "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
  1190. __func__, resp->result, action);
  1191. if (action == EN_AUTO_PS) {
  1192. if (auto_ps_bitmap & BITMAP_AUTO_DS) {
  1193. dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
  1194. priv->adapter->is_deep_sleep = true;
  1195. }
  1196. if (auto_ps_bitmap & BITMAP_STA_PS) {
  1197. dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
  1198. if (adapter->sleep_period.period)
  1199. dev_dbg(adapter->dev,
  1200. "cmd: set to uapsd/pps mode\n");
  1201. }
  1202. } else if (action == DIS_AUTO_PS) {
  1203. if (ps_bitmap & BITMAP_AUTO_DS) {
  1204. priv->adapter->is_deep_sleep = false;
  1205. dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
  1206. }
  1207. if (ps_bitmap & BITMAP_STA_PS) {
  1208. dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
  1209. if (adapter->sleep_period.period) {
  1210. adapter->delay_null_pkt = false;
  1211. adapter->tx_lock_flag = false;
  1212. adapter->pps_uapsd_mode = false;
  1213. }
  1214. }
  1215. } else if (action == GET_PS) {
  1216. if (ps_bitmap & BITMAP_STA_PS)
  1217. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
  1218. else
  1219. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
  1220. dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
  1221. if (pm_cfg) {
  1222. /* This section is for get power save mode */
  1223. if (ps_bitmap & BITMAP_STA_PS)
  1224. pm_cfg->param.ps_mode = 1;
  1225. else
  1226. pm_cfg->param.ps_mode = 0;
  1227. }
  1228. }
  1229. return 0;
  1230. }
  1231. /*
  1232. * This function prepares command to get hardware specifications.
  1233. *
  1234. * Preparation includes -
  1235. * - Setting command ID, action and proper size
  1236. * - Setting permanent address parameter
  1237. * - Ensuring correct endian-ness
  1238. */
  1239. int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
  1240. struct host_cmd_ds_command *cmd)
  1241. {
  1242. struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
  1243. cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
  1244. cmd->size =
  1245. cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
  1246. memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
  1247. return 0;
  1248. }
  1249. /*
  1250. * This function handles the command response of get hardware
  1251. * specifications.
  1252. *
  1253. * Handling includes changing the header fields into CPU format
  1254. * and saving/updating the following parameters in driver -
  1255. * - Firmware capability information
  1256. * - Firmware band settings
  1257. * - Ad-hoc start band and channel
  1258. * - Ad-hoc 11n activation status
  1259. * - Firmware release number
  1260. * - Number of antennas
  1261. * - Hardware address
  1262. * - Hardware interface version
  1263. * - Firmware version
  1264. * - Region code
  1265. * - 11n capabilities
  1266. * - MCS support fields
  1267. * - MP end port
  1268. */
  1269. int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
  1270. struct host_cmd_ds_command *resp)
  1271. {
  1272. struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
  1273. struct mwifiex_adapter *adapter = priv->adapter;
  1274. int i;
  1275. adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
  1276. if (IS_SUPPORT_MULTI_BANDS(adapter))
  1277. adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
  1278. else
  1279. adapter->fw_bands = BAND_B;
  1280. adapter->config_bands = adapter->fw_bands;
  1281. if (adapter->fw_bands & BAND_A) {
  1282. if (adapter->fw_bands & BAND_GN) {
  1283. adapter->config_bands |= BAND_AN;
  1284. adapter->fw_bands |= BAND_AN;
  1285. }
  1286. if (adapter->fw_bands & BAND_AN) {
  1287. adapter->adhoc_start_band = BAND_A | BAND_AN;
  1288. adapter->adhoc_11n_enabled = true;
  1289. } else {
  1290. adapter->adhoc_start_band = BAND_A;
  1291. }
  1292. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
  1293. } else if (adapter->fw_bands & BAND_GN) {
  1294. adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
  1295. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1296. adapter->adhoc_11n_enabled = true;
  1297. } else if (adapter->fw_bands & BAND_G) {
  1298. adapter->adhoc_start_band = BAND_G | BAND_B;
  1299. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1300. } else if (adapter->fw_bands & BAND_B) {
  1301. adapter->adhoc_start_band = BAND_B;
  1302. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1303. }
  1304. adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
  1305. adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
  1306. dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
  1307. adapter->fw_release_number);
  1308. dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
  1309. hw_spec->permanent_addr);
  1310. dev_dbg(adapter->dev,
  1311. "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
  1312. le16_to_cpu(hw_spec->hw_if_version),
  1313. le16_to_cpu(hw_spec->version));
  1314. if (priv->curr_addr[0] == 0xff)
  1315. memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
  1316. adapter->region_code = le16_to_cpu(hw_spec->region_code);
  1317. for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
  1318. /* Use the region code to search for the index */
  1319. if (adapter->region_code == region_code_index[i])
  1320. break;
  1321. /* If it's unidentified region code, use the default (USA) */
  1322. if (i >= MWIFIEX_MAX_REGION_CODE) {
  1323. adapter->region_code = 0x10;
  1324. dev_dbg(adapter->dev,
  1325. "cmd: unknown region code, use default (USA)\n");
  1326. }
  1327. adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
  1328. adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
  1329. if (adapter->if_ops.update_mp_end_port)
  1330. adapter->if_ops.update_mp_end_port(adapter,
  1331. le16_to_cpu(hw_spec->mp_end_port));
  1332. return 0;
  1333. }