cmdevt.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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. dev_err(adapter->dev, "last_cmd_id: %*ph\n",
  803. (int)sizeof(adapter->dbg.last_cmd_id),
  804. adapter->dbg.last_cmd_id);
  805. dev_err(adapter->dev, "last_cmd_act: %*ph\n",
  806. (int)sizeof(adapter->dbg.last_cmd_act),
  807. adapter->dbg.last_cmd_act);
  808. dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
  809. adapter->dbg.last_cmd_resp_index);
  810. dev_err(adapter->dev, "last_cmd_resp_id: %*ph\n",
  811. (int)sizeof(adapter->dbg.last_cmd_resp_id),
  812. adapter->dbg.last_cmd_resp_id);
  813. dev_err(adapter->dev, "last_event_index = %d\n",
  814. adapter->dbg.last_event_index);
  815. dev_err(adapter->dev, "last_event: %*ph\n",
  816. (int)sizeof(adapter->dbg.last_event),
  817. adapter->dbg.last_event);
  818. dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
  819. adapter->data_sent, adapter->cmd_sent);
  820. dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
  821. adapter->ps_mode, adapter->ps_state);
  822. if (cmd_node->wait_q_enabled) {
  823. adapter->cmd_wait_q.status = -ETIMEDOUT;
  824. wake_up_interruptible(&adapter->cmd_wait_q.wait);
  825. mwifiex_cancel_pending_ioctl(adapter);
  826. /* reset cmd_sent flag to unblock new commands */
  827. adapter->cmd_sent = false;
  828. }
  829. }
  830. if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
  831. mwifiex_init_fw_complete(adapter);
  832. if (adapter->if_ops.card_reset)
  833. adapter->if_ops.card_reset(adapter);
  834. }
  835. /*
  836. * This function cancels all the pending commands.
  837. *
  838. * The current command, all commands in command pending queue and all scan
  839. * commands in scan pending queue are cancelled. All the completion callbacks
  840. * are called with failure status to ensure cleanup.
  841. */
  842. void
  843. mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
  844. {
  845. struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
  846. unsigned long flags;
  847. /* Cancel current cmd */
  848. if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
  849. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  850. adapter->curr_cmd->wait_q_enabled = false;
  851. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  852. adapter->cmd_wait_q.status = -1;
  853. mwifiex_complete_cmd(adapter, adapter->curr_cmd);
  854. }
  855. /* Cancel all pending command */
  856. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  857. list_for_each_entry_safe(cmd_node, tmp_node,
  858. &adapter->cmd_pending_q, list) {
  859. list_del(&cmd_node->list);
  860. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  861. if (cmd_node->wait_q_enabled) {
  862. adapter->cmd_wait_q.status = -1;
  863. mwifiex_complete_cmd(adapter, cmd_node);
  864. cmd_node->wait_q_enabled = false;
  865. }
  866. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  867. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  868. }
  869. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  870. /* Cancel all pending scan command */
  871. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  872. list_for_each_entry_safe(cmd_node, tmp_node,
  873. &adapter->scan_pending_q, list) {
  874. list_del(&cmd_node->list);
  875. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  876. cmd_node->wait_q_enabled = false;
  877. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  878. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  879. }
  880. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  881. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  882. adapter->scan_processing = false;
  883. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  884. }
  885. /*
  886. * This function cancels all pending commands that matches with
  887. * the given IOCTL request.
  888. *
  889. * Both the current command buffer and the pending command queue are
  890. * searched for matching IOCTL request. The completion callback of
  891. * the matched command is called with failure status to ensure cleanup.
  892. * In case of scan commands, all pending commands in scan pending queue
  893. * are cancelled.
  894. */
  895. void
  896. mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
  897. {
  898. struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
  899. unsigned long cmd_flags;
  900. unsigned long scan_pending_q_flags;
  901. uint16_t cancel_scan_cmd = false;
  902. if ((adapter->curr_cmd) &&
  903. (adapter->curr_cmd->wait_q_enabled)) {
  904. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  905. cmd_node = adapter->curr_cmd;
  906. cmd_node->wait_q_enabled = false;
  907. cmd_node->cmd_flag |= CMD_F_CANCELED;
  908. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  909. mwifiex_complete_cmd(adapter, adapter->curr_cmd);
  910. adapter->curr_cmd = NULL;
  911. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  912. }
  913. /* Cancel all pending scan command */
  914. spin_lock_irqsave(&adapter->scan_pending_q_lock,
  915. scan_pending_q_flags);
  916. list_for_each_entry_safe(cmd_node, tmp_node,
  917. &adapter->scan_pending_q, list) {
  918. list_del(&cmd_node->list);
  919. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  920. scan_pending_q_flags);
  921. cmd_node->wait_q_enabled = false;
  922. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  923. spin_lock_irqsave(&adapter->scan_pending_q_lock,
  924. scan_pending_q_flags);
  925. cancel_scan_cmd = true;
  926. }
  927. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  928. scan_pending_q_flags);
  929. if (cancel_scan_cmd) {
  930. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  931. adapter->scan_processing = false;
  932. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  933. }
  934. adapter->cmd_wait_q.status = -1;
  935. }
  936. /*
  937. * This function sends the sleep confirm command to firmware, if
  938. * possible.
  939. *
  940. * The sleep confirm command cannot be issued if command response,
  941. * data response or event response is awaiting handling, or if we
  942. * are in the middle of sending a command, or expecting a command
  943. * response.
  944. */
  945. void
  946. mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
  947. {
  948. if (!adapter->cmd_sent &&
  949. !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
  950. mwifiex_dnld_sleep_confirm_cmd(adapter);
  951. else
  952. dev_dbg(adapter->dev,
  953. "cmd: Delay Sleep Confirm (%s%s%s)\n",
  954. (adapter->cmd_sent) ? "D" : "",
  955. (adapter->curr_cmd) ? "C" : "",
  956. (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
  957. }
  958. /*
  959. * This function sends a Host Sleep activated event to applications.
  960. *
  961. * This event is generated by the driver, with a blank event body.
  962. */
  963. void
  964. mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
  965. {
  966. if (activated) {
  967. if (priv->adapter->is_hs_configured) {
  968. priv->adapter->hs_activated = true;
  969. mwifiex_update_rxreor_flags(priv->adapter,
  970. RXREOR_FORCE_NO_DROP);
  971. dev_dbg(priv->adapter->dev, "event: hs_activated\n");
  972. priv->adapter->hs_activate_wait_q_woken = true;
  973. wake_up_interruptible(
  974. &priv->adapter->hs_activate_wait_q);
  975. } else {
  976. dev_dbg(priv->adapter->dev, "event: HS not configured\n");
  977. }
  978. } else {
  979. dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
  980. priv->adapter->hs_activated = false;
  981. }
  982. }
  983. /*
  984. * This function handles the command response of a Host Sleep configuration
  985. * command.
  986. *
  987. * Handling includes changing the header fields into CPU format
  988. * and setting the current host sleep activation status in driver.
  989. *
  990. * In case host sleep status change, the function generates an event to
  991. * notify the applications.
  992. */
  993. int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
  994. struct host_cmd_ds_command *resp)
  995. {
  996. struct mwifiex_adapter *adapter = priv->adapter;
  997. struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
  998. &resp->params.opt_hs_cfg;
  999. uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
  1000. if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
  1001. adapter->iface_type == MWIFIEX_SDIO) {
  1002. mwifiex_hs_activated_event(priv, true);
  1003. return 0;
  1004. } else {
  1005. dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
  1006. " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
  1007. resp->result, conditions,
  1008. phs_cfg->params.hs_config.gpio,
  1009. phs_cfg->params.hs_config.gap);
  1010. }
  1011. if (conditions != HOST_SLEEP_CFG_CANCEL) {
  1012. adapter->is_hs_configured = true;
  1013. if (adapter->iface_type == MWIFIEX_USB ||
  1014. adapter->iface_type == MWIFIEX_PCIE)
  1015. mwifiex_hs_activated_event(priv, true);
  1016. } else {
  1017. adapter->is_hs_configured = false;
  1018. if (adapter->hs_activated)
  1019. mwifiex_hs_activated_event(priv, false);
  1020. }
  1021. return 0;
  1022. }
  1023. /*
  1024. * This function wakes up the adapter and generates a Host Sleep
  1025. * cancel event on receiving the power up interrupt.
  1026. */
  1027. void
  1028. mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
  1029. {
  1030. dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
  1031. " since there is interrupt from the firmware\n", __func__);
  1032. adapter->if_ops.wakeup(adapter);
  1033. adapter->hs_activated = false;
  1034. adapter->is_hs_configured = false;
  1035. mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
  1036. MWIFIEX_BSS_ROLE_ANY),
  1037. false);
  1038. }
  1039. EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
  1040. /*
  1041. * This function handles the command response of a sleep confirm command.
  1042. *
  1043. * The function sets the card state to SLEEP if the response indicates success.
  1044. */
  1045. void
  1046. mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
  1047. u8 *pbuf, u32 upld_len)
  1048. {
  1049. struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
  1050. struct mwifiex_private *priv =
  1051. mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  1052. uint16_t result = le16_to_cpu(cmd->result);
  1053. uint16_t command = le16_to_cpu(cmd->command);
  1054. uint16_t seq_num = le16_to_cpu(cmd->seq_num);
  1055. if (!upld_len) {
  1056. dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
  1057. return;
  1058. }
  1059. /* Get BSS number and corresponding priv */
  1060. priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
  1061. HostCmd_GET_BSS_TYPE(seq_num));
  1062. if (!priv)
  1063. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  1064. /* Update sequence number */
  1065. seq_num = HostCmd_GET_SEQ_NO(seq_num);
  1066. /* Clear RET_BIT from HostCmd */
  1067. command &= HostCmd_CMD_ID_MASK;
  1068. if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
  1069. dev_err(adapter->dev,
  1070. "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
  1071. __func__, command, result);
  1072. return;
  1073. }
  1074. if (result) {
  1075. dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
  1076. __func__);
  1077. adapter->pm_wakeup_card_req = false;
  1078. adapter->ps_state = PS_STATE_AWAKE;
  1079. return;
  1080. }
  1081. adapter->pm_wakeup_card_req = true;
  1082. if (adapter->is_hs_configured)
  1083. mwifiex_hs_activated_event(mwifiex_get_priv
  1084. (adapter, MWIFIEX_BSS_ROLE_ANY),
  1085. true);
  1086. adapter->ps_state = PS_STATE_SLEEP;
  1087. cmd->command = cpu_to_le16(command);
  1088. cmd->seq_num = cpu_to_le16(seq_num);
  1089. }
  1090. EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
  1091. /*
  1092. * This function prepares an enhanced power mode command.
  1093. *
  1094. * This function can be used to disable power save or to configure
  1095. * power save with auto PS or STA PS or auto deep sleep.
  1096. *
  1097. * Preparation includes -
  1098. * - Setting command ID, action and proper size
  1099. * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
  1100. * auto deep sleep TLV (as required)
  1101. * - Ensuring correct endian-ness
  1102. */
  1103. int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
  1104. struct host_cmd_ds_command *cmd,
  1105. u16 cmd_action, uint16_t ps_bitmap,
  1106. struct mwifiex_ds_auto_ds *auto_ds)
  1107. {
  1108. struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
  1109. &cmd->params.psmode_enh;
  1110. u8 *tlv;
  1111. u16 cmd_size = 0;
  1112. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
  1113. if (cmd_action == DIS_AUTO_PS) {
  1114. psmode_enh->action = cpu_to_le16(DIS_AUTO_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 == GET_PS) {
  1119. psmode_enh->action = cpu_to_le16(GET_PS);
  1120. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1121. cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
  1122. sizeof(psmode_enh->params.ps_bitmap));
  1123. } else if (cmd_action == EN_AUTO_PS) {
  1124. psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
  1125. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1126. cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
  1127. sizeof(psmode_enh->params.ps_bitmap);
  1128. tlv = (u8 *) cmd + cmd_size;
  1129. if (ps_bitmap & BITMAP_STA_PS) {
  1130. struct mwifiex_adapter *adapter = priv->adapter;
  1131. struct mwifiex_ie_types_ps_param *ps_tlv =
  1132. (struct mwifiex_ie_types_ps_param *) tlv;
  1133. struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
  1134. ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
  1135. ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
  1136. sizeof(struct mwifiex_ie_types_header));
  1137. cmd_size += sizeof(*ps_tlv);
  1138. tlv += sizeof(*ps_tlv);
  1139. dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
  1140. ps_mode->null_pkt_interval =
  1141. cpu_to_le16(adapter->null_pkt_interval);
  1142. ps_mode->multiple_dtims =
  1143. cpu_to_le16(adapter->multiple_dtim);
  1144. ps_mode->bcn_miss_timeout =
  1145. cpu_to_le16(adapter->bcn_miss_time_out);
  1146. ps_mode->local_listen_interval =
  1147. cpu_to_le16(adapter->local_listen_interval);
  1148. ps_mode->adhoc_wake_period =
  1149. cpu_to_le16(adapter->adhoc_awake_period);
  1150. ps_mode->delay_to_ps =
  1151. cpu_to_le16(adapter->delay_to_ps);
  1152. ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
  1153. }
  1154. if (ps_bitmap & BITMAP_AUTO_DS) {
  1155. struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
  1156. (struct mwifiex_ie_types_auto_ds_param *) tlv;
  1157. u16 idletime = 0;
  1158. auto_ds_tlv->header.type =
  1159. cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
  1160. auto_ds_tlv->header.len =
  1161. cpu_to_le16(sizeof(*auto_ds_tlv) -
  1162. sizeof(struct mwifiex_ie_types_header));
  1163. cmd_size += sizeof(*auto_ds_tlv);
  1164. tlv += sizeof(*auto_ds_tlv);
  1165. if (auto_ds)
  1166. idletime = auto_ds->idle_time;
  1167. dev_dbg(priv->adapter->dev,
  1168. "cmd: PS Command: Enter Auto Deep Sleep\n");
  1169. auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
  1170. }
  1171. cmd->size = cpu_to_le16(cmd_size);
  1172. }
  1173. return 0;
  1174. }
  1175. /*
  1176. * This function handles the command response of an enhanced power mode
  1177. * command.
  1178. *
  1179. * Handling includes changing the header fields into CPU format
  1180. * and setting the current enhanced power mode in driver.
  1181. */
  1182. int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
  1183. struct host_cmd_ds_command *resp,
  1184. struct mwifiex_ds_pm_cfg *pm_cfg)
  1185. {
  1186. struct mwifiex_adapter *adapter = priv->adapter;
  1187. struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
  1188. &resp->params.psmode_enh;
  1189. uint16_t action = le16_to_cpu(ps_mode->action);
  1190. uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
  1191. uint16_t auto_ps_bitmap =
  1192. le16_to_cpu(ps_mode->params.ps_bitmap);
  1193. dev_dbg(adapter->dev,
  1194. "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
  1195. __func__, resp->result, action);
  1196. if (action == EN_AUTO_PS) {
  1197. if (auto_ps_bitmap & BITMAP_AUTO_DS) {
  1198. dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
  1199. priv->adapter->is_deep_sleep = true;
  1200. }
  1201. if (auto_ps_bitmap & BITMAP_STA_PS) {
  1202. dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
  1203. if (adapter->sleep_period.period)
  1204. dev_dbg(adapter->dev,
  1205. "cmd: set to uapsd/pps mode\n");
  1206. }
  1207. } else if (action == DIS_AUTO_PS) {
  1208. if (ps_bitmap & BITMAP_AUTO_DS) {
  1209. priv->adapter->is_deep_sleep = false;
  1210. dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
  1211. }
  1212. if (ps_bitmap & BITMAP_STA_PS) {
  1213. dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
  1214. if (adapter->sleep_period.period) {
  1215. adapter->delay_null_pkt = false;
  1216. adapter->tx_lock_flag = false;
  1217. adapter->pps_uapsd_mode = false;
  1218. }
  1219. }
  1220. } else if (action == GET_PS) {
  1221. if (ps_bitmap & BITMAP_STA_PS)
  1222. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
  1223. else
  1224. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
  1225. dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
  1226. if (pm_cfg) {
  1227. /* This section is for get power save mode */
  1228. if (ps_bitmap & BITMAP_STA_PS)
  1229. pm_cfg->param.ps_mode = 1;
  1230. else
  1231. pm_cfg->param.ps_mode = 0;
  1232. }
  1233. }
  1234. return 0;
  1235. }
  1236. /*
  1237. * This function prepares command to get hardware specifications.
  1238. *
  1239. * Preparation includes -
  1240. * - Setting command ID, action and proper size
  1241. * - Setting permanent address parameter
  1242. * - Ensuring correct endian-ness
  1243. */
  1244. int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
  1245. struct host_cmd_ds_command *cmd)
  1246. {
  1247. struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
  1248. cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
  1249. cmd->size =
  1250. cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
  1251. memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
  1252. return 0;
  1253. }
  1254. /*
  1255. * This function handles the command response of get hardware
  1256. * specifications.
  1257. *
  1258. * Handling includes changing the header fields into CPU format
  1259. * and saving/updating the following parameters in driver -
  1260. * - Firmware capability information
  1261. * - Firmware band settings
  1262. * - Ad-hoc start band and channel
  1263. * - Ad-hoc 11n activation status
  1264. * - Firmware release number
  1265. * - Number of antennas
  1266. * - Hardware address
  1267. * - Hardware interface version
  1268. * - Firmware version
  1269. * - Region code
  1270. * - 11n capabilities
  1271. * - MCS support fields
  1272. * - MP end port
  1273. */
  1274. int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
  1275. struct host_cmd_ds_command *resp)
  1276. {
  1277. struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
  1278. struct mwifiex_adapter *adapter = priv->adapter;
  1279. int i;
  1280. adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
  1281. if (IS_SUPPORT_MULTI_BANDS(adapter))
  1282. adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
  1283. else
  1284. adapter->fw_bands = BAND_B;
  1285. adapter->config_bands = adapter->fw_bands;
  1286. if (adapter->fw_bands & BAND_A) {
  1287. if (adapter->fw_bands & BAND_GN) {
  1288. adapter->config_bands |= BAND_AN;
  1289. adapter->fw_bands |= BAND_AN;
  1290. }
  1291. if (adapter->fw_bands & BAND_AN) {
  1292. adapter->adhoc_start_band = BAND_A | BAND_AN;
  1293. adapter->adhoc_11n_enabled = true;
  1294. } else {
  1295. adapter->adhoc_start_band = BAND_A;
  1296. }
  1297. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
  1298. } else if (adapter->fw_bands & BAND_GN) {
  1299. adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
  1300. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1301. adapter->adhoc_11n_enabled = true;
  1302. } else if (adapter->fw_bands & BAND_G) {
  1303. adapter->adhoc_start_band = BAND_G | BAND_B;
  1304. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1305. } else if (adapter->fw_bands & BAND_B) {
  1306. adapter->adhoc_start_band = BAND_B;
  1307. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1308. }
  1309. adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
  1310. adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
  1311. dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
  1312. adapter->fw_release_number);
  1313. dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
  1314. hw_spec->permanent_addr);
  1315. dev_dbg(adapter->dev,
  1316. "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
  1317. le16_to_cpu(hw_spec->hw_if_version),
  1318. le16_to_cpu(hw_spec->version));
  1319. if (priv->curr_addr[0] == 0xff)
  1320. memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
  1321. adapter->region_code = le16_to_cpu(hw_spec->region_code);
  1322. for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
  1323. /* Use the region code to search for the index */
  1324. if (adapter->region_code == region_code_index[i])
  1325. break;
  1326. /* If it's unidentified region code, use the default (USA) */
  1327. if (i >= MWIFIEX_MAX_REGION_CODE) {
  1328. adapter->region_code = 0x10;
  1329. dev_dbg(adapter->dev,
  1330. "cmd: unknown region code, use default (USA)\n");
  1331. }
  1332. adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
  1333. adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
  1334. if (adapter->if_ops.update_mp_end_port)
  1335. adapter->if_ops.update_mp_end_port(adapter,
  1336. le16_to_cpu(hw_spec->mp_end_port));
  1337. return 0;
  1338. }