cmdevt.c 46 KB

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