cmdevt.c 46 KB

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