cmdevt.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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. ret = mwifiex_process_sta_event(priv);
  399. adapter->event_cause = 0;
  400. adapter->event_skb = NULL;
  401. adapter->if_ops.event_complete(adapter, skb);
  402. return ret;
  403. }
  404. /*
  405. * This function is used to send synchronous command to the firmware.
  406. *
  407. * it allocates a wait queue for the command and wait for the command
  408. * response.
  409. */
  410. int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
  411. u16 cmd_action, u32 cmd_oid, void *data_buf)
  412. {
  413. int ret = 0;
  414. struct mwifiex_adapter *adapter = priv->adapter;
  415. adapter->cmd_wait_q_required = true;
  416. ret = mwifiex_send_cmd_async(priv, cmd_no, cmd_action, cmd_oid,
  417. data_buf);
  418. if (!ret)
  419. ret = mwifiex_wait_queue_complete(adapter);
  420. return ret;
  421. }
  422. /*
  423. * This function prepares a command and asynchronously send it to the firmware.
  424. *
  425. * Preparation includes -
  426. * - Sanity tests to make sure the card is still present or the FW
  427. * is not reset
  428. * - Getting a new command node from the command free queue
  429. * - Initializing the command node for default parameters
  430. * - Fill up the non-default parameters and buffer pointers
  431. * - Add the command to pending queue
  432. */
  433. int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
  434. u16 cmd_action, u32 cmd_oid, void *data_buf)
  435. {
  436. int ret;
  437. struct mwifiex_adapter *adapter = priv->adapter;
  438. struct cmd_ctrl_node *cmd_node;
  439. struct host_cmd_ds_command *cmd_ptr;
  440. if (!adapter) {
  441. pr_err("PREP_CMD: adapter is NULL\n");
  442. return -1;
  443. }
  444. if (adapter->is_suspended) {
  445. dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
  446. return -1;
  447. }
  448. if (adapter->surprise_removed) {
  449. dev_err(adapter->dev, "PREP_CMD: card is removed\n");
  450. return -1;
  451. }
  452. if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
  453. if (cmd_no != HostCmd_CMD_FUNC_INIT) {
  454. dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
  455. return -1;
  456. }
  457. }
  458. /* Get a new command node */
  459. cmd_node = mwifiex_get_cmd_node(adapter);
  460. if (!cmd_node) {
  461. dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
  462. return -1;
  463. }
  464. /* Initialize the command node */
  465. mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
  466. if (!cmd_node->cmd_skb) {
  467. dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
  468. return -1;
  469. }
  470. memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
  471. 0, sizeof(struct host_cmd_ds_command));
  472. cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  473. cmd_ptr->command = cpu_to_le16(cmd_no);
  474. cmd_ptr->result = 0;
  475. /* Prepare command */
  476. if (cmd_no) {
  477. switch (cmd_no) {
  478. case HostCmd_CMD_UAP_SYS_CONFIG:
  479. case HostCmd_CMD_UAP_BSS_START:
  480. case HostCmd_CMD_UAP_BSS_STOP:
  481. ret = mwifiex_uap_prepare_cmd(priv, cmd_no, cmd_action,
  482. cmd_oid, data_buf,
  483. cmd_ptr);
  484. break;
  485. default:
  486. ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
  487. cmd_oid, data_buf,
  488. cmd_ptr);
  489. break;
  490. }
  491. } else {
  492. ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
  493. cmd_node->cmd_flag |= CMD_F_HOSTCMD;
  494. }
  495. /* Return error, since the command preparation failed */
  496. if (ret) {
  497. dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
  498. cmd_no);
  499. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  500. return -1;
  501. }
  502. /* Send command */
  503. if (cmd_no == HostCmd_CMD_802_11_SCAN) {
  504. mwifiex_queue_scan_cmd(priv, cmd_node);
  505. } else {
  506. adapter->cmd_queued = cmd_node;
  507. mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
  508. queue_work(adapter->workqueue, &adapter->main_work);
  509. }
  510. return ret;
  511. }
  512. /*
  513. * This function returns a command to the command free queue.
  514. *
  515. * The function also calls the completion callback if required, before
  516. * cleaning the command node and re-inserting it into the free queue.
  517. */
  518. void
  519. mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
  520. struct cmd_ctrl_node *cmd_node)
  521. {
  522. unsigned long flags;
  523. if (!cmd_node)
  524. return;
  525. if (cmd_node->wait_q_enabled)
  526. mwifiex_complete_cmd(adapter, cmd_node);
  527. /* Clean the node */
  528. mwifiex_clean_cmd_node(adapter, cmd_node);
  529. /* Insert node into cmd_free_q */
  530. spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
  531. list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
  532. spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
  533. }
  534. /*
  535. * This function queues a command to the command pending queue.
  536. *
  537. * This in effect adds the command to the command list to be executed.
  538. * Exit PS command is handled specially, by placing it always to the
  539. * front of the command queue.
  540. */
  541. void
  542. mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
  543. struct cmd_ctrl_node *cmd_node, u32 add_tail)
  544. {
  545. struct host_cmd_ds_command *host_cmd = NULL;
  546. u16 command;
  547. unsigned long flags;
  548. host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  549. if (!host_cmd) {
  550. dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
  551. return;
  552. }
  553. command = le16_to_cpu(host_cmd->command);
  554. /* Exit_PS command needs to be queued in the header always. */
  555. if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
  556. struct host_cmd_ds_802_11_ps_mode_enh *pm =
  557. &host_cmd->params.psmode_enh;
  558. if ((le16_to_cpu(pm->action) == DIS_PS) ||
  559. (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
  560. if (adapter->ps_state != PS_STATE_AWAKE)
  561. add_tail = false;
  562. }
  563. }
  564. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  565. if (add_tail)
  566. list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
  567. else
  568. list_add(&cmd_node->list, &adapter->cmd_pending_q);
  569. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  570. dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
  571. }
  572. /*
  573. * This function executes the next command in command pending queue.
  574. *
  575. * This function will fail if a command is already in processing stage,
  576. * otherwise it will dequeue the first command from the command pending
  577. * queue and send to the firmware.
  578. *
  579. * If the device is currently in host sleep mode, any commands, except the
  580. * host sleep configuration command will de-activate the host sleep. For PS
  581. * mode, the function will put the firmware back to sleep if applicable.
  582. */
  583. int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
  584. {
  585. struct mwifiex_private *priv;
  586. struct cmd_ctrl_node *cmd_node;
  587. int ret = 0;
  588. struct host_cmd_ds_command *host_cmd;
  589. unsigned long cmd_flags;
  590. unsigned long cmd_pending_q_flags;
  591. /* Check if already in processing */
  592. if (adapter->curr_cmd) {
  593. dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
  594. return -1;
  595. }
  596. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  597. /* Check if any command is pending */
  598. spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
  599. if (list_empty(&adapter->cmd_pending_q)) {
  600. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  601. cmd_pending_q_flags);
  602. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  603. return 0;
  604. }
  605. cmd_node = list_first_entry(&adapter->cmd_pending_q,
  606. struct cmd_ctrl_node, list);
  607. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  608. cmd_pending_q_flags);
  609. host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
  610. priv = cmd_node->priv;
  611. if (adapter->ps_state != PS_STATE_AWAKE) {
  612. dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
  613. " this should not happen\n", __func__);
  614. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  615. return ret;
  616. }
  617. spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
  618. list_del(&cmd_node->list);
  619. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
  620. cmd_pending_q_flags);
  621. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  622. ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
  623. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  624. /* Any command sent to the firmware when host is in sleep
  625. * mode should de-configure host sleep. We should skip the
  626. * host sleep configuration command itself though
  627. */
  628. if (priv && (host_cmd->command !=
  629. cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
  630. if (adapter->hs_activated) {
  631. adapter->is_hs_configured = false;
  632. mwifiex_hs_activated_event(priv, false);
  633. }
  634. }
  635. return ret;
  636. }
  637. /*
  638. * This function handles the command response.
  639. *
  640. * After processing, the function cleans the command node and puts
  641. * it back to the command free queue.
  642. */
  643. int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
  644. {
  645. struct host_cmd_ds_command *resp;
  646. struct mwifiex_private *priv =
  647. mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  648. int ret = 0;
  649. uint16_t orig_cmdresp_no;
  650. uint16_t cmdresp_no;
  651. uint16_t cmdresp_result;
  652. struct timeval tstamp;
  653. unsigned long flags;
  654. /* Now we got response from FW, cancel the command timer */
  655. del_timer(&adapter->cmd_timer);
  656. if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
  657. resp = (struct host_cmd_ds_command *) adapter->upld_buf;
  658. dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
  659. le16_to_cpu(resp->command));
  660. return -1;
  661. }
  662. adapter->num_cmd_timeout = 0;
  663. resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
  664. if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
  665. dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
  666. le16_to_cpu(resp->command));
  667. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  668. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  669. adapter->curr_cmd = NULL;
  670. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  671. return -1;
  672. }
  673. if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
  674. /* Copy original response back to response buffer */
  675. struct mwifiex_ds_misc_cmd *hostcmd;
  676. uint16_t size = le16_to_cpu(resp->size);
  677. dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
  678. size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
  679. if (adapter->curr_cmd->data_buf) {
  680. hostcmd = adapter->curr_cmd->data_buf;
  681. hostcmd->len = size;
  682. memcpy(hostcmd->cmd, resp, size);
  683. }
  684. }
  685. orig_cmdresp_no = le16_to_cpu(resp->command);
  686. /* Get BSS number and corresponding priv */
  687. priv = mwifiex_get_priv_by_id(adapter,
  688. HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
  689. HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
  690. if (!priv)
  691. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  692. /* Clear RET_BIT from HostCmd */
  693. resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
  694. cmdresp_no = le16_to_cpu(resp->command);
  695. cmdresp_result = le16_to_cpu(resp->result);
  696. /* Save the last command response to debug log */
  697. adapter->dbg.last_cmd_resp_index =
  698. (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
  699. adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
  700. orig_cmdresp_no;
  701. do_gettimeofday(&tstamp);
  702. dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
  703. " len %d, seqno 0x%x\n",
  704. tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
  705. le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
  706. if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
  707. dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
  708. if (adapter->curr_cmd->wait_q_enabled)
  709. adapter->cmd_wait_q.status = -1;
  710. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  711. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  712. adapter->curr_cmd = NULL;
  713. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  714. return -1;
  715. }
  716. if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
  717. adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
  718. if ((cmdresp_result == HostCmd_RESULT_OK) &&
  719. (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
  720. ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
  721. } else {
  722. /* handle response */
  723. ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
  724. }
  725. /* Check init command response */
  726. if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
  727. if (ret) {
  728. dev_err(adapter->dev, "%s: cmd %#x failed during "
  729. "initialization\n", __func__, cmdresp_no);
  730. mwifiex_init_fw_complete(adapter);
  731. return -1;
  732. } else if (adapter->last_init_cmd == cmdresp_no)
  733. adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
  734. }
  735. if (adapter->curr_cmd) {
  736. if (adapter->curr_cmd->wait_q_enabled)
  737. adapter->cmd_wait_q.status = ret;
  738. /* Clean up and put current command back to cmd_free_q */
  739. mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
  740. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  741. adapter->curr_cmd = NULL;
  742. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  743. }
  744. return ret;
  745. }
  746. /*
  747. * This function handles the timeout of command sending.
  748. *
  749. * It will re-send the same command again.
  750. */
  751. void
  752. mwifiex_cmd_timeout_func(unsigned long function_context)
  753. {
  754. struct mwifiex_adapter *adapter =
  755. (struct mwifiex_adapter *) function_context;
  756. struct cmd_ctrl_node *cmd_node;
  757. struct timeval tstamp;
  758. adapter->num_cmd_timeout++;
  759. adapter->dbg.num_cmd_timeout++;
  760. if (!adapter->curr_cmd) {
  761. dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
  762. return;
  763. }
  764. cmd_node = adapter->curr_cmd;
  765. if (cmd_node->wait_q_enabled)
  766. adapter->cmd_wait_q.status = -ETIMEDOUT;
  767. if (cmd_node) {
  768. adapter->dbg.timeout_cmd_id =
  769. adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
  770. adapter->dbg.timeout_cmd_act =
  771. adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
  772. do_gettimeofday(&tstamp);
  773. dev_err(adapter->dev,
  774. "%s: Timeout cmd id (%lu.%lu) = %#x, act = %#x\n",
  775. __func__, tstamp.tv_sec, tstamp.tv_usec,
  776. adapter->dbg.timeout_cmd_id,
  777. adapter->dbg.timeout_cmd_act);
  778. dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
  779. adapter->dbg.num_tx_host_to_card_failure);
  780. dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
  781. adapter->dbg.num_cmd_host_to_card_failure);
  782. dev_err(adapter->dev, "num_cmd_timeout = %d\n",
  783. adapter->dbg.num_cmd_timeout);
  784. dev_err(adapter->dev, "num_tx_timeout = %d\n",
  785. adapter->dbg.num_tx_timeout);
  786. dev_err(adapter->dev, "last_cmd_index = %d\n",
  787. adapter->dbg.last_cmd_index);
  788. print_hex_dump_bytes("last_cmd_id: ", DUMP_PREFIX_OFFSET,
  789. adapter->dbg.last_cmd_id, DBG_CMD_NUM);
  790. print_hex_dump_bytes("last_cmd_act: ", DUMP_PREFIX_OFFSET,
  791. adapter->dbg.last_cmd_act, DBG_CMD_NUM);
  792. dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
  793. adapter->dbg.last_cmd_resp_index);
  794. print_hex_dump_bytes("last_cmd_resp_id: ", DUMP_PREFIX_OFFSET,
  795. adapter->dbg.last_cmd_resp_id,
  796. DBG_CMD_NUM);
  797. dev_err(adapter->dev, "last_event_index = %d\n",
  798. adapter->dbg.last_event_index);
  799. print_hex_dump_bytes("last_event: ", DUMP_PREFIX_OFFSET,
  800. adapter->dbg.last_event, DBG_CMD_NUM);
  801. dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
  802. adapter->data_sent, adapter->cmd_sent);
  803. dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
  804. adapter->ps_mode, adapter->ps_state);
  805. }
  806. if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
  807. mwifiex_init_fw_complete(adapter);
  808. }
  809. /*
  810. * This function cancels all the pending commands.
  811. *
  812. * The current command, all commands in command pending queue and all scan
  813. * commands in scan pending queue are cancelled. All the completion callbacks
  814. * are called with failure status to ensure cleanup.
  815. */
  816. void
  817. mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
  818. {
  819. struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
  820. unsigned long flags;
  821. /* Cancel current cmd */
  822. if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
  823. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  824. adapter->curr_cmd->wait_q_enabled = false;
  825. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  826. adapter->cmd_wait_q.status = -1;
  827. mwifiex_complete_cmd(adapter, adapter->curr_cmd);
  828. }
  829. /* Cancel all pending command */
  830. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  831. list_for_each_entry_safe(cmd_node, tmp_node,
  832. &adapter->cmd_pending_q, list) {
  833. list_del(&cmd_node->list);
  834. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  835. if (cmd_node->wait_q_enabled) {
  836. adapter->cmd_wait_q.status = -1;
  837. mwifiex_complete_cmd(adapter, cmd_node);
  838. cmd_node->wait_q_enabled = false;
  839. }
  840. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  841. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  842. }
  843. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  844. /* Cancel all pending scan command */
  845. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  846. list_for_each_entry_safe(cmd_node, tmp_node,
  847. &adapter->scan_pending_q, list) {
  848. list_del(&cmd_node->list);
  849. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  850. cmd_node->wait_q_enabled = false;
  851. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  852. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  853. }
  854. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  855. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  856. adapter->scan_processing = false;
  857. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  858. }
  859. /*
  860. * This function cancels all pending commands that matches with
  861. * the given IOCTL request.
  862. *
  863. * Both the current command buffer and the pending command queue are
  864. * searched for matching IOCTL request. The completion callback of
  865. * the matched command is called with failure status to ensure cleanup.
  866. * In case of scan commands, all pending commands in scan pending queue
  867. * are cancelled.
  868. */
  869. void
  870. mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
  871. {
  872. struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
  873. unsigned long cmd_flags;
  874. unsigned long scan_pending_q_flags;
  875. uint16_t cancel_scan_cmd = false;
  876. if ((adapter->curr_cmd) &&
  877. (adapter->curr_cmd->wait_q_enabled)) {
  878. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  879. cmd_node = adapter->curr_cmd;
  880. cmd_node->wait_q_enabled = false;
  881. cmd_node->cmd_flag |= CMD_F_CANCELED;
  882. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  883. mwifiex_complete_cmd(adapter, adapter->curr_cmd);
  884. adapter->curr_cmd = NULL;
  885. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  886. }
  887. /* Cancel all pending scan command */
  888. spin_lock_irqsave(&adapter->scan_pending_q_lock,
  889. scan_pending_q_flags);
  890. list_for_each_entry_safe(cmd_node, tmp_node,
  891. &adapter->scan_pending_q, list) {
  892. list_del(&cmd_node->list);
  893. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  894. scan_pending_q_flags);
  895. cmd_node->wait_q_enabled = false;
  896. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  897. spin_lock_irqsave(&adapter->scan_pending_q_lock,
  898. scan_pending_q_flags);
  899. cancel_scan_cmd = true;
  900. }
  901. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  902. scan_pending_q_flags);
  903. if (cancel_scan_cmd) {
  904. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
  905. adapter->scan_processing = false;
  906. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
  907. }
  908. adapter->cmd_wait_q.status = -1;
  909. }
  910. /*
  911. * This function sends the sleep confirm command to firmware, if
  912. * possible.
  913. *
  914. * The sleep confirm command cannot be issued if command response,
  915. * data response or event response is awaiting handling, or if we
  916. * are in the middle of sending a command, or expecting a command
  917. * response.
  918. */
  919. void
  920. mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
  921. {
  922. if (!adapter->cmd_sent &&
  923. !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
  924. mwifiex_dnld_sleep_confirm_cmd(adapter);
  925. else
  926. dev_dbg(adapter->dev,
  927. "cmd: Delay Sleep Confirm (%s%s%s)\n",
  928. (adapter->cmd_sent) ? "D" : "",
  929. (adapter->curr_cmd) ? "C" : "",
  930. (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
  931. }
  932. /*
  933. * This function sends a Host Sleep activated event to applications.
  934. *
  935. * This event is generated by the driver, with a blank event body.
  936. */
  937. void
  938. mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
  939. {
  940. if (activated) {
  941. if (priv->adapter->is_hs_configured) {
  942. priv->adapter->hs_activated = true;
  943. dev_dbg(priv->adapter->dev, "event: hs_activated\n");
  944. priv->adapter->hs_activate_wait_q_woken = true;
  945. wake_up_interruptible(
  946. &priv->adapter->hs_activate_wait_q);
  947. } else {
  948. dev_dbg(priv->adapter->dev, "event: HS not configured\n");
  949. }
  950. } else {
  951. dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
  952. priv->adapter->hs_activated = false;
  953. }
  954. }
  955. /*
  956. * This function handles the command response of a Host Sleep configuration
  957. * command.
  958. *
  959. * Handling includes changing the header fields into CPU format
  960. * and setting the current host sleep activation status in driver.
  961. *
  962. * In case host sleep status change, the function generates an event to
  963. * notify the applications.
  964. */
  965. int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
  966. struct host_cmd_ds_command *resp)
  967. {
  968. struct mwifiex_adapter *adapter = priv->adapter;
  969. struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
  970. &resp->params.opt_hs_cfg;
  971. uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
  972. if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
  973. adapter->iface_type == MWIFIEX_SDIO) {
  974. mwifiex_hs_activated_event(priv, true);
  975. return 0;
  976. } else {
  977. dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
  978. " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
  979. resp->result, conditions,
  980. phs_cfg->params.hs_config.gpio,
  981. phs_cfg->params.hs_config.gap);
  982. }
  983. if (conditions != HOST_SLEEP_CFG_CANCEL) {
  984. adapter->is_hs_configured = true;
  985. if (adapter->iface_type == MWIFIEX_USB ||
  986. adapter->iface_type == MWIFIEX_PCIE)
  987. mwifiex_hs_activated_event(priv, true);
  988. } else {
  989. adapter->is_hs_configured = false;
  990. if (adapter->hs_activated)
  991. mwifiex_hs_activated_event(priv, false);
  992. }
  993. return 0;
  994. }
  995. /*
  996. * This function wakes up the adapter and generates a Host Sleep
  997. * cancel event on receiving the power up interrupt.
  998. */
  999. void
  1000. mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
  1001. {
  1002. dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
  1003. " since there is interrupt from the firmware\n", __func__);
  1004. adapter->if_ops.wakeup(adapter);
  1005. adapter->hs_activated = false;
  1006. adapter->is_hs_configured = false;
  1007. mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
  1008. MWIFIEX_BSS_ROLE_ANY),
  1009. false);
  1010. }
  1011. EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
  1012. /*
  1013. * This function handles the command response of a sleep confirm command.
  1014. *
  1015. * The function sets the card state to SLEEP if the response indicates success.
  1016. */
  1017. void
  1018. mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
  1019. u8 *pbuf, u32 upld_len)
  1020. {
  1021. struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
  1022. struct mwifiex_private *priv =
  1023. mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  1024. uint16_t result = le16_to_cpu(cmd->result);
  1025. uint16_t command = le16_to_cpu(cmd->command);
  1026. uint16_t seq_num = le16_to_cpu(cmd->seq_num);
  1027. if (!upld_len) {
  1028. dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
  1029. return;
  1030. }
  1031. /* Get BSS number and corresponding priv */
  1032. priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
  1033. HostCmd_GET_BSS_TYPE(seq_num));
  1034. if (!priv)
  1035. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  1036. /* Update sequence number */
  1037. seq_num = HostCmd_GET_SEQ_NO(seq_num);
  1038. /* Clear RET_BIT from HostCmd */
  1039. command &= HostCmd_CMD_ID_MASK;
  1040. if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
  1041. dev_err(adapter->dev,
  1042. "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
  1043. __func__, command, result);
  1044. return;
  1045. }
  1046. if (result) {
  1047. dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
  1048. __func__);
  1049. adapter->pm_wakeup_card_req = false;
  1050. adapter->ps_state = PS_STATE_AWAKE;
  1051. return;
  1052. }
  1053. adapter->pm_wakeup_card_req = true;
  1054. if (adapter->is_hs_configured)
  1055. mwifiex_hs_activated_event(mwifiex_get_priv
  1056. (adapter, MWIFIEX_BSS_ROLE_ANY),
  1057. true);
  1058. adapter->ps_state = PS_STATE_SLEEP;
  1059. cmd->command = cpu_to_le16(command);
  1060. cmd->seq_num = cpu_to_le16(seq_num);
  1061. }
  1062. EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
  1063. /*
  1064. * This function prepares an enhanced power mode command.
  1065. *
  1066. * This function can be used to disable power save or to configure
  1067. * power save with auto PS or STA PS or auto deep sleep.
  1068. *
  1069. * Preparation includes -
  1070. * - Setting command ID, action and proper size
  1071. * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
  1072. * auto deep sleep TLV (as required)
  1073. * - Ensuring correct endian-ness
  1074. */
  1075. int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
  1076. struct host_cmd_ds_command *cmd,
  1077. u16 cmd_action, uint16_t ps_bitmap,
  1078. struct mwifiex_ds_auto_ds *auto_ds)
  1079. {
  1080. struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
  1081. &cmd->params.psmode_enh;
  1082. u8 *tlv;
  1083. u16 cmd_size = 0;
  1084. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
  1085. if (cmd_action == DIS_AUTO_PS) {
  1086. psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
  1087. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1088. cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
  1089. sizeof(psmode_enh->params.ps_bitmap));
  1090. } else if (cmd_action == GET_PS) {
  1091. psmode_enh->action = cpu_to_le16(GET_PS);
  1092. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1093. cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
  1094. sizeof(psmode_enh->params.ps_bitmap));
  1095. } else if (cmd_action == EN_AUTO_PS) {
  1096. psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
  1097. psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
  1098. cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
  1099. sizeof(psmode_enh->params.ps_bitmap);
  1100. tlv = (u8 *) cmd + cmd_size;
  1101. if (ps_bitmap & BITMAP_STA_PS) {
  1102. struct mwifiex_adapter *adapter = priv->adapter;
  1103. struct mwifiex_ie_types_ps_param *ps_tlv =
  1104. (struct mwifiex_ie_types_ps_param *) tlv;
  1105. struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
  1106. ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
  1107. ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
  1108. sizeof(struct mwifiex_ie_types_header));
  1109. cmd_size += sizeof(*ps_tlv);
  1110. tlv += sizeof(*ps_tlv);
  1111. dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
  1112. ps_mode->null_pkt_interval =
  1113. cpu_to_le16(adapter->null_pkt_interval);
  1114. ps_mode->multiple_dtims =
  1115. cpu_to_le16(adapter->multiple_dtim);
  1116. ps_mode->bcn_miss_timeout =
  1117. cpu_to_le16(adapter->bcn_miss_time_out);
  1118. ps_mode->local_listen_interval =
  1119. cpu_to_le16(adapter->local_listen_interval);
  1120. ps_mode->adhoc_wake_period =
  1121. cpu_to_le16(adapter->adhoc_awake_period);
  1122. ps_mode->delay_to_ps =
  1123. cpu_to_le16(adapter->delay_to_ps);
  1124. ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
  1125. }
  1126. if (ps_bitmap & BITMAP_AUTO_DS) {
  1127. struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
  1128. (struct mwifiex_ie_types_auto_ds_param *) tlv;
  1129. u16 idletime = 0;
  1130. auto_ds_tlv->header.type =
  1131. cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
  1132. auto_ds_tlv->header.len =
  1133. cpu_to_le16(sizeof(*auto_ds_tlv) -
  1134. sizeof(struct mwifiex_ie_types_header));
  1135. cmd_size += sizeof(*auto_ds_tlv);
  1136. tlv += sizeof(*auto_ds_tlv);
  1137. if (auto_ds)
  1138. idletime = auto_ds->idle_time;
  1139. dev_dbg(priv->adapter->dev,
  1140. "cmd: PS Command: Enter Auto Deep Sleep\n");
  1141. auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
  1142. }
  1143. cmd->size = cpu_to_le16(cmd_size);
  1144. }
  1145. return 0;
  1146. }
  1147. /*
  1148. * This function handles the command response of an enhanced power mode
  1149. * command.
  1150. *
  1151. * Handling includes changing the header fields into CPU format
  1152. * and setting the current enhanced power mode in driver.
  1153. */
  1154. int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
  1155. struct host_cmd_ds_command *resp,
  1156. struct mwifiex_ds_pm_cfg *pm_cfg)
  1157. {
  1158. struct mwifiex_adapter *adapter = priv->adapter;
  1159. struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
  1160. &resp->params.psmode_enh;
  1161. uint16_t action = le16_to_cpu(ps_mode->action);
  1162. uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
  1163. uint16_t auto_ps_bitmap =
  1164. le16_to_cpu(ps_mode->params.ps_bitmap);
  1165. dev_dbg(adapter->dev,
  1166. "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
  1167. __func__, resp->result, action);
  1168. if (action == EN_AUTO_PS) {
  1169. if (auto_ps_bitmap & BITMAP_AUTO_DS) {
  1170. dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
  1171. priv->adapter->is_deep_sleep = true;
  1172. }
  1173. if (auto_ps_bitmap & BITMAP_STA_PS) {
  1174. dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
  1175. if (adapter->sleep_period.period)
  1176. dev_dbg(adapter->dev,
  1177. "cmd: set to uapsd/pps mode\n");
  1178. }
  1179. } else if (action == DIS_AUTO_PS) {
  1180. if (ps_bitmap & BITMAP_AUTO_DS) {
  1181. priv->adapter->is_deep_sleep = false;
  1182. dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
  1183. }
  1184. if (ps_bitmap & BITMAP_STA_PS) {
  1185. dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
  1186. if (adapter->sleep_period.period) {
  1187. adapter->delay_null_pkt = false;
  1188. adapter->tx_lock_flag = false;
  1189. adapter->pps_uapsd_mode = false;
  1190. }
  1191. }
  1192. } else if (action == GET_PS) {
  1193. if (ps_bitmap & BITMAP_STA_PS)
  1194. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
  1195. else
  1196. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
  1197. dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
  1198. if (pm_cfg) {
  1199. /* This section is for get power save mode */
  1200. if (ps_bitmap & BITMAP_STA_PS)
  1201. pm_cfg->param.ps_mode = 1;
  1202. else
  1203. pm_cfg->param.ps_mode = 0;
  1204. }
  1205. }
  1206. return 0;
  1207. }
  1208. /*
  1209. * This function prepares command to get hardware specifications.
  1210. *
  1211. * Preparation includes -
  1212. * - Setting command ID, action and proper size
  1213. * - Setting permanent address parameter
  1214. * - Ensuring correct endian-ness
  1215. */
  1216. int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
  1217. struct host_cmd_ds_command *cmd)
  1218. {
  1219. struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
  1220. cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
  1221. cmd->size =
  1222. cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
  1223. memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
  1224. return 0;
  1225. }
  1226. /*
  1227. * This function handles the command response of get hardware
  1228. * specifications.
  1229. *
  1230. * Handling includes changing the header fields into CPU format
  1231. * and saving/updating the following parameters in driver -
  1232. * - Firmware capability information
  1233. * - Firmware band settings
  1234. * - Ad-hoc start band and channel
  1235. * - Ad-hoc 11n activation status
  1236. * - Firmware release number
  1237. * - Number of antennas
  1238. * - Hardware address
  1239. * - Hardware interface version
  1240. * - Firmware version
  1241. * - Region code
  1242. * - 11n capabilities
  1243. * - MCS support fields
  1244. * - MP end port
  1245. */
  1246. int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
  1247. struct host_cmd_ds_command *resp)
  1248. {
  1249. struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
  1250. struct mwifiex_adapter *adapter = priv->adapter;
  1251. int i;
  1252. adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
  1253. if (IS_SUPPORT_MULTI_BANDS(adapter))
  1254. adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
  1255. else
  1256. adapter->fw_bands = BAND_B;
  1257. adapter->config_bands = adapter->fw_bands;
  1258. if (adapter->fw_bands & BAND_A) {
  1259. if (adapter->fw_bands & BAND_GN) {
  1260. adapter->config_bands |= BAND_AN;
  1261. adapter->fw_bands |= BAND_AN;
  1262. }
  1263. if (adapter->fw_bands & BAND_AN) {
  1264. adapter->adhoc_start_band = BAND_A | BAND_AN;
  1265. adapter->adhoc_11n_enabled = true;
  1266. } else {
  1267. adapter->adhoc_start_band = BAND_A;
  1268. }
  1269. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
  1270. } else if (adapter->fw_bands & BAND_GN) {
  1271. adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
  1272. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1273. adapter->adhoc_11n_enabled = true;
  1274. } else if (adapter->fw_bands & BAND_G) {
  1275. adapter->adhoc_start_band = BAND_G | BAND_B;
  1276. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1277. } else if (adapter->fw_bands & BAND_B) {
  1278. adapter->adhoc_start_band = BAND_B;
  1279. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  1280. }
  1281. adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
  1282. adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
  1283. dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
  1284. adapter->fw_release_number);
  1285. dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
  1286. hw_spec->permanent_addr);
  1287. dev_dbg(adapter->dev,
  1288. "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
  1289. le16_to_cpu(hw_spec->hw_if_version),
  1290. le16_to_cpu(hw_spec->version));
  1291. if (priv->curr_addr[0] == 0xff)
  1292. memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
  1293. adapter->region_code = le16_to_cpu(hw_spec->region_code);
  1294. for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
  1295. /* Use the region code to search for the index */
  1296. if (adapter->region_code == region_code_index[i])
  1297. break;
  1298. /* If it's unidentified region code, use the default (USA) */
  1299. if (i >= MWIFIEX_MAX_REGION_CODE) {
  1300. adapter->region_code = 0x10;
  1301. dev_dbg(adapter->dev,
  1302. "cmd: unknown region code, use default (USA)\n");
  1303. }
  1304. adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
  1305. adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
  1306. if (adapter->if_ops.update_mp_end_port)
  1307. adapter->if_ops.update_mp_end_port(adapter,
  1308. le16_to_cpu(hw_spec->mp_end_port));
  1309. return 0;
  1310. }