cmdevt.c 44 KB

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