cmdevt.c 45 KB

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