cmdevt.c 47 KB

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