cmdevt.c 46 KB

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