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