cmdevt.c 44 KB

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