cmdevt.c 43 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. 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,
  147. cmd_node->priv->bss_num,
  148. cmd_node->priv->bss_type));
  149. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  150. adapter->curr_cmd = cmd_node;
  151. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  152. cmd_code = le16_to_cpu(host_cmd->command);
  153. cmd_size = le16_to_cpu(host_cmd->size);
  154. skb_trim(cmd_node->cmd_skb, cmd_size);
  155. do_gettimeofday(&tstamp);
  156. dev_dbg(adapter->dev, "cmd: DNLD_CMD: (%lu.%lu): %#x, act %#x, len %d,"
  157. " seqno %#x\n",
  158. tstamp.tv_sec, tstamp.tv_usec, cmd_code,
  159. le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN)), cmd_size,
  160. le16_to_cpu(host_cmd->seq_num));
  161. skb_push(cmd_node->cmd_skb, INTF_HEADER_LEN);
  162. ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
  163. cmd_node->cmd_skb, NULL);
  164. skb_pull(cmd_node->cmd_skb, INTF_HEADER_LEN);
  165. if (ret == -1) {
  166. dev_err(adapter->dev, "DNLD_CMD: host to card failed\n");
  167. if (cmd_node->wait_q_enabled)
  168. adapter->cmd_wait_q.status = -1;
  169. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  170. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  171. adapter->curr_cmd = NULL;
  172. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  173. adapter->dbg.num_cmd_host_to_card_failure++;
  174. return -1;
  175. }
  176. /* Save the last command id and action to debug log */
  177. adapter->dbg.last_cmd_index =
  178. (adapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
  179. adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index] = cmd_code;
  180. adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index] =
  181. le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN));
  182. /* Clear BSS_NO_BITS from HostCmd */
  183. cmd_code &= HostCmd_CMD_ID_MASK;
  184. /* Setup the timer after transmit command */
  185. mod_timer(&adapter->cmd_timer,
  186. jiffies + (MWIFIEX_TIMER_10S * HZ) / 1000);
  187. return 0;
  188. }
  189. /*
  190. * This function downloads a sleep confirm command to the firmware.
  191. *
  192. * The function performs sanity tests, sets the command sequence
  193. * number and size, converts the header fields to CPU format before
  194. * sending.
  195. *
  196. * No responses are needed for sleep confirm command.
  197. */
  198. static int mwifiex_dnld_sleep_confirm_cmd(struct mwifiex_adapter *adapter)
  199. {
  200. int ret;
  201. struct mwifiex_private *priv;
  202. struct mwifiex_opt_sleep_confirm *sleep_cfm_buf =
  203. (struct mwifiex_opt_sleep_confirm *)
  204. adapter->sleep_cfm->data;
  205. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  206. sleep_cfm_buf->seq_num =
  207. cpu_to_le16((HostCmd_SET_SEQ_NO_BSS_INFO
  208. (adapter->seq_num, priv->bss_num,
  209. priv->bss_type)));
  210. adapter->seq_num++;
  211. skb_push(adapter->sleep_cfm, INTF_HEADER_LEN);
  212. ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
  213. adapter->sleep_cfm, NULL);
  214. skb_pull(adapter->sleep_cfm, INTF_HEADER_LEN);
  215. if (ret == -1) {
  216. dev_err(adapter->dev, "SLEEP_CFM: failed\n");
  217. adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure++;
  218. return -1;
  219. }
  220. if (GET_BSS_ROLE(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY))
  221. == MWIFIEX_BSS_ROLE_STA) {
  222. if (!sleep_cfm_buf->resp_ctrl)
  223. /* Response is not needed for sleep
  224. confirm command */
  225. adapter->ps_state = PS_STATE_SLEEP;
  226. else
  227. adapter->ps_state = PS_STATE_SLEEP_CFM;
  228. if (!sleep_cfm_buf->resp_ctrl &&
  229. (adapter->is_hs_configured &&
  230. !adapter->sleep_period.period)) {
  231. adapter->pm_wakeup_card_req = true;
  232. mwifiex_hs_activated_event(mwifiex_get_priv
  233. (adapter, MWIFIEX_BSS_ROLE_STA), true);
  234. }
  235. }
  236. return ret;
  237. }
  238. /*
  239. * This function allocates the command buffers and links them to
  240. * the command free queue.
  241. *
  242. * The driver uses a pre allocated number of command buffers, which
  243. * are created at driver initializations and freed at driver cleanup.
  244. * Every command needs to obtain a command buffer from this pool before
  245. * it can be issued. The command free queue lists the command buffers
  246. * currently free to use, while the command pending queue lists the
  247. * command buffers already in use and awaiting handling. Command buffers
  248. * are returned to the free queue after use.
  249. */
  250. int mwifiex_alloc_cmd_buffer(struct mwifiex_adapter *adapter)
  251. {
  252. struct cmd_ctrl_node *cmd_array;
  253. u32 buf_size;
  254. u32 i;
  255. /* Allocate and initialize struct cmd_ctrl_node */
  256. buf_size = sizeof(struct cmd_ctrl_node) * MWIFIEX_NUM_OF_CMD_BUFFER;
  257. cmd_array = kzalloc(buf_size, GFP_KERNEL);
  258. if (!cmd_array) {
  259. dev_err(adapter->dev, "%s: failed to alloc cmd_array\n",
  260. __func__);
  261. return -ENOMEM;
  262. }
  263. adapter->cmd_pool = cmd_array;
  264. memset(adapter->cmd_pool, 0, buf_size);
  265. /* Allocate and initialize command buffers */
  266. for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
  267. cmd_array[i].skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
  268. if (!cmd_array[i].skb) {
  269. dev_err(adapter->dev, "ALLOC_CMD_BUF: out of memory\n");
  270. return -1;
  271. }
  272. }
  273. for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++)
  274. mwifiex_insert_cmd_to_free_q(adapter, &cmd_array[i]);
  275. return 0;
  276. }
  277. /*
  278. * This function frees the command buffers.
  279. *
  280. * The function calls the completion callback for all the command
  281. * buffers that still have response buffers associated with them.
  282. */
  283. int mwifiex_free_cmd_buffer(struct mwifiex_adapter *adapter)
  284. {
  285. struct cmd_ctrl_node *cmd_array;
  286. u32 i;
  287. /* Need to check if cmd pool is allocated or not */
  288. if (!adapter->cmd_pool) {
  289. dev_dbg(adapter->dev, "info: FREE_CMD_BUF: cmd_pool is null\n");
  290. return 0;
  291. }
  292. cmd_array = adapter->cmd_pool;
  293. /* Release shared memory buffers */
  294. for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
  295. if (cmd_array[i].skb) {
  296. dev_dbg(adapter->dev, "cmd: free cmd buffer %d\n", i);
  297. dev_kfree_skb_any(cmd_array[i].skb);
  298. }
  299. if (!cmd_array[i].resp_skb)
  300. continue;
  301. dev_kfree_skb_any(cmd_array[i].resp_skb);
  302. }
  303. /* Release struct cmd_ctrl_node */
  304. if (adapter->cmd_pool) {
  305. dev_dbg(adapter->dev, "cmd: free cmd pool\n");
  306. kfree(adapter->cmd_pool);
  307. adapter->cmd_pool = NULL;
  308. }
  309. return 0;
  310. }
  311. /*
  312. * This function handles events generated by firmware.
  313. *
  314. * Event body of events received from firmware are not used (though they are
  315. * saved), only the event ID is used. Some events are re-invoked by
  316. * the driver, with a new event body.
  317. *
  318. * After processing, the function calls the completion callback
  319. * for cleanup.
  320. */
  321. int mwifiex_process_event(struct mwifiex_adapter *adapter)
  322. {
  323. int ret;
  324. struct mwifiex_private *priv =
  325. mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  326. struct sk_buff *skb = adapter->event_skb;
  327. u32 eventcause = adapter->event_cause;
  328. struct timeval tstamp;
  329. struct mwifiex_rxinfo *rx_info;
  330. /* Save the last event to debug log */
  331. adapter->dbg.last_event_index =
  332. (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
  333. adapter->dbg.last_event[adapter->dbg.last_event_index] =
  334. (u16) eventcause;
  335. /* Get BSS number and corresponding priv */
  336. priv = mwifiex_get_priv_by_id(adapter, EVENT_GET_BSS_NUM(eventcause),
  337. EVENT_GET_BSS_TYPE(eventcause));
  338. if (!priv)
  339. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  340. /* Clear BSS_NO_BITS from event */
  341. eventcause &= EVENT_ID_MASK;
  342. adapter->event_cause = eventcause;
  343. if (skb) {
  344. rx_info = MWIFIEX_SKB_RXCB(skb);
  345. rx_info->bss_num = priv->bss_num;
  346. rx_info->bss_type = priv->bss_type;
  347. }
  348. if (eventcause != EVENT_PS_SLEEP && eventcause != EVENT_PS_AWAKE) {
  349. do_gettimeofday(&tstamp);
  350. dev_dbg(adapter->dev, "event: %lu.%lu: cause: %#x\n",
  351. tstamp.tv_sec, tstamp.tv_usec, eventcause);
  352. }
  353. ret = mwifiex_process_sta_event(priv);
  354. adapter->event_cause = 0;
  355. adapter->event_skb = NULL;
  356. adapter->if_ops.event_complete(adapter, skb);
  357. return ret;
  358. }
  359. /*
  360. * This function is used to send synchronous command to the firmware.
  361. *
  362. * it allocates a wait queue for the command and wait for the command
  363. * response.
  364. */
  365. int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
  366. u16 cmd_action, u32 cmd_oid, void *data_buf)
  367. {
  368. int ret = 0;
  369. struct mwifiex_adapter *adapter = priv->adapter;
  370. adapter->cmd_wait_q_required = true;
  371. ret = mwifiex_send_cmd_async(priv, cmd_no, cmd_action, cmd_oid,
  372. data_buf);
  373. if (!ret)
  374. ret = mwifiex_wait_queue_complete(adapter);
  375. return ret;
  376. }
  377. /*
  378. * This function prepares a command and asynchronously send it to the firmware.
  379. *
  380. * Preparation includes -
  381. * - Sanity tests to make sure the card is still present or the FW
  382. * is not reset
  383. * - Getting a new command node from the command free queue
  384. * - Initializing the command node for default parameters
  385. * - Fill up the non-default parameters and buffer pointers
  386. * - Add the command to pending queue
  387. */
  388. int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
  389. u16 cmd_action, u32 cmd_oid, void *data_buf)
  390. {
  391. int ret;
  392. struct mwifiex_adapter *adapter = priv->adapter;
  393. struct cmd_ctrl_node *cmd_node;
  394. struct host_cmd_ds_command *cmd_ptr;
  395. if (!adapter) {
  396. pr_err("PREP_CMD: adapter is NULL\n");
  397. return -1;
  398. }
  399. if (adapter->is_suspended) {
  400. dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
  401. return -1;
  402. }
  403. if (adapter->surprise_removed) {
  404. dev_err(adapter->dev, "PREP_CMD: card is removed\n");
  405. return -1;
  406. }
  407. if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
  408. if (cmd_no != HostCmd_CMD_FUNC_INIT) {
  409. dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
  410. return -1;
  411. }
  412. }
  413. /* Get a new command node */
  414. cmd_node = mwifiex_get_cmd_node(adapter);
  415. if (!cmd_node) {
  416. dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
  417. return -1;
  418. }
  419. /* Initialize the command node */
  420. mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
  421. if (!cmd_node->cmd_skb) {
  422. dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
  423. return -1;
  424. }
  425. memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
  426. 0, sizeof(struct host_cmd_ds_command));
  427. cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  428. cmd_ptr->command = cpu_to_le16(cmd_no);
  429. cmd_ptr->result = 0;
  430. /* Prepare command */
  431. if (cmd_no) {
  432. ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
  433. cmd_oid, data_buf, cmd_ptr);
  434. } else {
  435. ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
  436. cmd_node->cmd_flag |= CMD_F_HOSTCMD;
  437. }
  438. /* Return error, since the command preparation failed */
  439. if (ret) {
  440. dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
  441. cmd_no);
  442. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  443. return -1;
  444. }
  445. /* Send command */
  446. if (cmd_no == HostCmd_CMD_802_11_SCAN) {
  447. mwifiex_queue_scan_cmd(priv, cmd_node);
  448. } else {
  449. adapter->cmd_queued = cmd_node;
  450. mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
  451. }
  452. return ret;
  453. }
  454. /*
  455. * This function returns a command to the command free queue.
  456. *
  457. * The function also calls the completion callback if required, before
  458. * cleaning the command node and re-inserting it into the free queue.
  459. */
  460. void
  461. mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
  462. struct cmd_ctrl_node *cmd_node)
  463. {
  464. unsigned long flags;
  465. if (!cmd_node)
  466. return;
  467. if (cmd_node->wait_q_enabled)
  468. mwifiex_complete_cmd(adapter, cmd_node);
  469. /* Clean the node */
  470. mwifiex_clean_cmd_node(adapter, cmd_node);
  471. /* Insert node into cmd_free_q */
  472. spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
  473. list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
  474. spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
  475. }
  476. /*
  477. * This function queues a command to the command pending queue.
  478. *
  479. * This in effect adds the command to the command list to be executed.
  480. * Exit PS command is handled specially, by placing it always to the
  481. * front of the command queue.
  482. */
  483. void
  484. mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
  485. struct cmd_ctrl_node *cmd_node, u32 add_tail)
  486. {
  487. struct host_cmd_ds_command *host_cmd = NULL;
  488. u16 command;
  489. unsigned long flags;
  490. host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  491. if (!host_cmd) {
  492. dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
  493. return;
  494. }
  495. command = le16_to_cpu(host_cmd->command);
  496. /* Exit_PS command needs to be queued in the header always. */
  497. if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
  498. struct host_cmd_ds_802_11_ps_mode_enh *pm =
  499. &host_cmd->params.psmode_enh;
  500. if ((le16_to_cpu(pm->action) == DIS_PS) ||
  501. (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
  502. if (adapter->ps_state != PS_STATE_AWAKE)
  503. add_tail = false;
  504. }
  505. }
  506. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  507. if (add_tail)
  508. list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
  509. else
  510. list_add(&cmd_node->list, &adapter->cmd_pending_q);
  511. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  512. dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
  513. }
  514. /*
  515. * This function executes the next command in command pending queue.
  516. *
  517. * This function will fail if a command is already in processing stage,
  518. * otherwise it will dequeue the first command from the command pending
  519. * queue and send to the firmware.
  520. *
  521. * If the device is currently in host sleep mode, any commands, except the
  522. * host sleep configuration command will de-activate the host sleep. For PS
  523. * mode, the function will put the firmware back to sleep if applicable.
  524. */
  525. int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
  526. {
  527. struct mwifiex_private *priv;
  528. struct cmd_ctrl_node *cmd_node;
  529. int ret = 0;
  530. struct host_cmd_ds_command *host_cmd;
  531. unsigned long cmd_flags;
  532. unsigned long cmd_pending_q_flags;
  533. /* Check if already in processing */
  534. if (adapter->curr_cmd) {
  535. dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
  536. return -1;
  537. }
  538. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  539. /* Check if any command is pending */
  540. spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
  541. if (list_empty(&adapter->cmd_pending_q)) {
  542. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  543. cmd_pending_q_flags);
  544. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  545. return 0;
  546. }
  547. cmd_node = list_first_entry(&adapter->cmd_pending_q,
  548. struct cmd_ctrl_node, list);
  549. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  550. cmd_pending_q_flags);
  551. host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  552. priv = cmd_node->priv;
  553. if (adapter->ps_state != PS_STATE_AWAKE) {
  554. dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
  555. " this should not happen\n", __func__);
  556. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  557. return ret;
  558. }
  559. spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
  560. list_del(&cmd_node->list);
  561. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  562. cmd_pending_q_flags);
  563. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  564. ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
  565. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  566. /* Any command sent to the firmware when host is in sleep
  567. * mode should de-configure host sleep. We should skip the
  568. * host sleep configuration command itself though
  569. */
  570. if (priv && (host_cmd->command !=
  571. cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
  572. if (adapter->hs_activated) {
  573. adapter->is_hs_configured = false;
  574. mwifiex_hs_activated_event(priv, false);
  575. }
  576. }
  577. return ret;
  578. }
  579. /*
  580. * This function handles the command response.
  581. *
  582. * After processing, the function cleans the command node and puts
  583. * it back to the command free queue.
  584. */
  585. int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
  586. {
  587. struct host_cmd_ds_command *resp;
  588. struct mwifiex_private *priv =
  589. mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  590. int ret = 0;
  591. uint16_t orig_cmdresp_no;
  592. uint16_t cmdresp_no;
  593. uint16_t cmdresp_result;
  594. struct timeval tstamp;
  595. unsigned long flags;
  596. /* Now we got response from FW, cancel the command timer */
  597. del_timer(&adapter->cmd_timer);
  598. if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
  599. resp = (struct host_cmd_ds_command *) adapter->upld_buf;
  600. dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
  601. le16_to_cpu(resp->command));
  602. return -1;
  603. }
  604. adapter->num_cmd_timeout = 0;
  605. resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
  606. if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
  607. dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
  608. le16_to_cpu(resp->command));
  609. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  610. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  611. adapter->curr_cmd = NULL;
  612. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  613. return -1;
  614. }
  615. if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
  616. /* Copy original response back to response buffer */
  617. struct mwifiex_ds_misc_cmd *hostcmd;
  618. uint16_t size = le16_to_cpu(resp->size);
  619. dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
  620. size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
  621. if (adapter->curr_cmd->data_buf) {
  622. hostcmd = adapter->curr_cmd->data_buf;
  623. hostcmd->len = size;
  624. memcpy(hostcmd->cmd, resp, size);
  625. }
  626. }
  627. orig_cmdresp_no = le16_to_cpu(resp->command);
  628. /* Get BSS number and corresponding priv */
  629. priv = mwifiex_get_priv_by_id(adapter,
  630. HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
  631. HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
  632. if (!priv)
  633. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  634. /* Clear RET_BIT from HostCmd */
  635. resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
  636. cmdresp_no = le16_to_cpu(resp->command);
  637. cmdresp_result = le16_to_cpu(resp->result);
  638. /* Save the last command response to debug log */
  639. adapter->dbg.last_cmd_resp_index =
  640. (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
  641. adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
  642. orig_cmdresp_no;
  643. do_gettimeofday(&tstamp);
  644. dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
  645. " len %d, seqno 0x%x\n",
  646. tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
  647. le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
  648. if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
  649. dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
  650. if (adapter->curr_cmd->wait_q_enabled)
  651. adapter->cmd_wait_q.status = -1;
  652. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  653. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  654. adapter->curr_cmd = NULL;
  655. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  656. return -1;
  657. }
  658. if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
  659. adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
  660. if ((cmdresp_result == HostCmd_RESULT_OK) &&
  661. (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
  662. ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
  663. } else {
  664. /* handle response */
  665. ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
  666. }
  667. /* Check init command response */
  668. if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
  669. if (ret) {
  670. dev_err(adapter->dev, "%s: cmd %#x failed during "
  671. "initialization\n", __func__, cmdresp_no);
  672. mwifiex_init_fw_complete(adapter);
  673. return -1;
  674. } else if (adapter->last_init_cmd == cmdresp_no)
  675. adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
  676. }
  677. if (adapter->curr_cmd) {
  678. if (adapter->curr_cmd->wait_q_enabled)
  679. adapter->cmd_wait_q.status = ret;
  680. /* Clean up and put current command back to cmd_free_q */
  681. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  682. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  683. adapter->curr_cmd = NULL;
  684. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  685. }
  686. return ret;
  687. }
  688. /*
  689. * This function handles the timeout of command sending.
  690. *
  691. * It will re-send the same command again.
  692. */
  693. void
  694. mwifiex_cmd_timeout_func(unsigned long function_context)
  695. {
  696. struct mwifiex_adapter *adapter =
  697. (struct mwifiex_adapter *) function_context;
  698. struct cmd_ctrl_node *cmd_node;
  699. struct timeval tstamp;
  700. adapter->num_cmd_timeout++;
  701. adapter->dbg.num_cmd_timeout++;
  702. if (!adapter->curr_cmd) {
  703. dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
  704. return;
  705. }
  706. cmd_node = adapter->curr_cmd;
  707. if (cmd_node->wait_q_enabled)
  708. adapter->cmd_wait_q.status = -ETIMEDOUT;
  709. if (cmd_node) {
  710. adapter->dbg.timeout_cmd_id =
  711. adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
  712. adapter->dbg.timeout_cmd_act =
  713. adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
  714. do_gettimeofday(&tstamp);
  715. dev_err(adapter->dev,
  716. "%s: Timeout cmd id (%lu.%lu) = %#x, act = %#x\n",
  717. __func__, tstamp.tv_sec, tstamp.tv_usec,
  718. adapter->dbg.timeout_cmd_id,
  719. adapter->dbg.timeout_cmd_act);
  720. dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
  721. adapter->dbg.num_tx_host_to_card_failure);
  722. dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
  723. adapter->dbg.num_cmd_host_to_card_failure);
  724. dev_err(adapter->dev, "num_cmd_timeout = %d\n",
  725. adapter->dbg.num_cmd_timeout);
  726. dev_err(adapter->dev, "num_tx_timeout = %d\n",
  727. adapter->dbg.num_tx_timeout);
  728. dev_err(adapter->dev, "last_cmd_index = %d\n",
  729. adapter->dbg.last_cmd_index);
  730. print_hex_dump_bytes("last_cmd_id: ", DUMP_PREFIX_OFFSET,
  731. adapter->dbg.last_cmd_id, DBG_CMD_NUM);
  732. print_hex_dump_bytes("last_cmd_act: ", DUMP_PREFIX_OFFSET,
  733. adapter->dbg.last_cmd_act, DBG_CMD_NUM);
  734. dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
  735. adapter->dbg.last_cmd_resp_index);
  736. print_hex_dump_bytes("last_cmd_resp_id: ", DUMP_PREFIX_OFFSET,
  737. adapter->dbg.last_cmd_resp_id,
  738. 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),
  947. 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,
  979. "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
  980. __func__, command, result);
  981. return;
  982. }
  983. if (result) {
  984. dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
  985. __func__);
  986. adapter->pm_wakeup_card_req = false;
  987. adapter->ps_state = PS_STATE_AWAKE;
  988. return;
  989. }
  990. adapter->pm_wakeup_card_req = true;
  991. if (adapter->is_hs_configured)
  992. mwifiex_hs_activated_event(mwifiex_get_priv
  993. (adapter, MWIFIEX_BSS_ROLE_ANY),
  994. true);
  995. adapter->ps_state = PS_STATE_SLEEP;
  996. cmd->command = cpu_to_le16(command);
  997. cmd->seq_num = cpu_to_le16(seq_num);
  998. }
  999. EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
  1000. /*
  1001. * This function prepares an enhanced power mode command.
  1002. *
  1003. * This function can be used to disable power save or to configure
  1004. * power save with auto PS or STA PS or auto deep sleep.
  1005. *
  1006. * Preparation includes -
  1007. * - Setting command ID, action and proper size
  1008. * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
  1009. * auto deep sleep TLV (as required)
  1010. * - Ensuring correct endian-ness
  1011. */
  1012. int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
  1013. struct host_cmd_ds_command *cmd,
  1014. u16 cmd_action, uint16_t ps_bitmap,
  1015. struct mwifiex_ds_auto_ds *auto_ds)
  1016. {
  1017. struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
  1018. &cmd->params.psmode_enh;
  1019. u8 *tlv;
  1020. u16 cmd_size = 0;
  1021. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
  1022. if (cmd_action == DIS_AUTO_PS) {
  1023. psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
  1024. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1025. cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
  1026. sizeof(psmode_enh->params.ps_bitmap));
  1027. } else if (cmd_action == GET_PS) {
  1028. psmode_enh->action = cpu_to_le16(GET_PS);
  1029. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1030. cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
  1031. sizeof(psmode_enh->params.ps_bitmap));
  1032. } else if (cmd_action == EN_AUTO_PS) {
  1033. psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
  1034. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1035. cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
  1036. sizeof(psmode_enh->params.ps_bitmap);
  1037. tlv = (u8 *) cmd + cmd_size;
  1038. if (ps_bitmap & BITMAP_STA_PS) {
  1039. struct mwifiex_adapter *adapter = priv->adapter;
  1040. struct mwifiex_ie_types_ps_param *ps_tlv =
  1041. (struct mwifiex_ie_types_ps_param *) tlv;
  1042. struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
  1043. ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
  1044. ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
  1045. sizeof(struct mwifiex_ie_types_header));
  1046. cmd_size += sizeof(*ps_tlv);
  1047. tlv += sizeof(*ps_tlv);
  1048. dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
  1049. ps_mode->null_pkt_interval =
  1050. cpu_to_le16(adapter->null_pkt_interval);
  1051. ps_mode->multiple_dtims =
  1052. cpu_to_le16(adapter->multiple_dtim);
  1053. ps_mode->bcn_miss_timeout =
  1054. cpu_to_le16(adapter->bcn_miss_time_out);
  1055. ps_mode->local_listen_interval =
  1056. cpu_to_le16(adapter->local_listen_interval);
  1057. ps_mode->adhoc_wake_period =
  1058. cpu_to_le16(adapter->adhoc_awake_period);
  1059. ps_mode->delay_to_ps =
  1060. cpu_to_le16(adapter->delay_to_ps);
  1061. ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
  1062. }
  1063. if (ps_bitmap & BITMAP_AUTO_DS) {
  1064. struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
  1065. (struct mwifiex_ie_types_auto_ds_param *) tlv;
  1066. u16 idletime = 0;
  1067. auto_ds_tlv->header.type =
  1068. cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
  1069. auto_ds_tlv->header.len =
  1070. cpu_to_le16(sizeof(*auto_ds_tlv) -
  1071. sizeof(struct mwifiex_ie_types_header));
  1072. cmd_size += sizeof(*auto_ds_tlv);
  1073. tlv += sizeof(*auto_ds_tlv);
  1074. if (auto_ds)
  1075. idletime = auto_ds->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. struct mwifiex_ds_pm_cfg *pm_cfg)
  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,
  1103. "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
  1104. __func__, resp->result, action);
  1105. if (action == EN_AUTO_PS) {
  1106. if (auto_ps_bitmap & BITMAP_AUTO_DS) {
  1107. dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
  1108. priv->adapter->is_deep_sleep = true;
  1109. }
  1110. if (auto_ps_bitmap & BITMAP_STA_PS) {
  1111. dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
  1112. if (adapter->sleep_period.period)
  1113. dev_dbg(adapter->dev,
  1114. "cmd: set to uapsd/pps mode\n");
  1115. }
  1116. } else if (action == DIS_AUTO_PS) {
  1117. if (ps_bitmap & BITMAP_AUTO_DS) {
  1118. priv->adapter->is_deep_sleep = false;
  1119. dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
  1120. }
  1121. if (ps_bitmap & BITMAP_STA_PS) {
  1122. dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
  1123. if (adapter->sleep_period.period) {
  1124. adapter->delay_null_pkt = false;
  1125. adapter->tx_lock_flag = false;
  1126. adapter->pps_uapsd_mode = false;
  1127. }
  1128. }
  1129. } else if (action == GET_PS) {
  1130. if (ps_bitmap & BITMAP_STA_PS)
  1131. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
  1132. else
  1133. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
  1134. dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
  1135. if (pm_cfg) {
  1136. /* This section is for get power save mode */
  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,
  1225. "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
  1226. le16_to_cpu(hw_spec->hw_if_version),
  1227. le16_to_cpu(hw_spec->version));
  1228. if (priv->curr_addr[0] == 0xff)
  1229. memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
  1230. adapter->region_code = le16_to_cpu(hw_spec->region_code);
  1231. for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
  1232. /* Use the region code to search for the index */
  1233. if (adapter->region_code == region_code_index[i])
  1234. break;
  1235. /* If it's unidentified region code, use the default (USA) */
  1236. if (i >= MWIFIEX_MAX_REGION_CODE) {
  1237. adapter->region_code = 0x10;
  1238. dev_dbg(adapter->dev,
  1239. "cmd: unknown region code, use default (USA)\n");
  1240. }
  1241. adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
  1242. adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
  1243. if (adapter->if_ops.update_mp_end_port)
  1244. adapter->if_ops.update_mp_end_port(adapter,
  1245. le16_to_cpu(hw_spec->mp_end_port));
  1246. return 0;
  1247. }