init.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * Marvell Wireless LAN device driver: HW/FW Initialization
  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 adds a BSS priority table to the table list.
  28. *
  29. * The function allocates a new BSS priority table node and adds it to
  30. * the end of BSS priority table list, kept in driver memory.
  31. */
  32. static int mwifiex_add_bss_prio_tbl(struct mwifiex_private *priv)
  33. {
  34. struct mwifiex_adapter *adapter = priv->adapter;
  35. struct mwifiex_bss_prio_node *bss_prio;
  36. struct mwifiex_bss_prio_tbl *tbl = adapter->bss_prio_tbl;
  37. unsigned long flags;
  38. bss_prio = kzalloc(sizeof(struct mwifiex_bss_prio_node), GFP_KERNEL);
  39. if (!bss_prio) {
  40. dev_err(adapter->dev, "%s: failed to alloc bss_prio\n",
  41. __func__);
  42. return -ENOMEM;
  43. }
  44. bss_prio->priv = priv;
  45. INIT_LIST_HEAD(&bss_prio->list);
  46. if (!tbl[priv->bss_priority].bss_prio_cur)
  47. tbl[priv->bss_priority].bss_prio_cur = bss_prio;
  48. spin_lock_irqsave(&tbl[priv->bss_priority].bss_prio_lock, flags);
  49. list_add_tail(&bss_prio->list, &tbl[priv->bss_priority].bss_prio_head);
  50. spin_unlock_irqrestore(&tbl[priv->bss_priority].bss_prio_lock, flags);
  51. return 0;
  52. }
  53. static void scan_delay_timer_fn(unsigned long data)
  54. {
  55. struct mwifiex_private *priv = (struct mwifiex_private *)data;
  56. struct mwifiex_adapter *adapter = priv->adapter;
  57. struct cmd_ctrl_node *cmd_node, *tmp_node;
  58. unsigned long flags;
  59. if (!mwifiex_wmm_lists_empty(adapter)) {
  60. if (adapter->scan_delay_cnt == MWIFIEX_MAX_SCAN_DELAY_CNT) {
  61. /*
  62. * Abort scan operation by cancelling all pending scan
  63. * command
  64. */
  65. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  66. list_for_each_entry_safe(cmd_node, tmp_node,
  67. &adapter->scan_pending_q,
  68. list) {
  69. list_del(&cmd_node->list);
  70. cmd_node->wait_q_enabled = false;
  71. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  72. }
  73. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  74. flags);
  75. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  76. adapter->scan_processing = false;
  77. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock,
  78. flags);
  79. if (priv->user_scan_cfg) {
  80. dev_dbg(priv->adapter->dev,
  81. "info: %s: scan aborted\n", __func__);
  82. cfg80211_scan_done(priv->scan_request, 1);
  83. priv->scan_request = NULL;
  84. kfree(priv->user_scan_cfg);
  85. priv->user_scan_cfg = NULL;
  86. }
  87. } else {
  88. /*
  89. * Tx data queue is still not empty, delay scan
  90. * operation further by 20msec.
  91. */
  92. mod_timer(&priv->scan_delay_timer, jiffies +
  93. msecs_to_jiffies(MWIFIEX_SCAN_DELAY_MSEC));
  94. adapter->scan_delay_cnt++;
  95. }
  96. queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
  97. } else {
  98. /*
  99. * Tx data queue is empty. Get scan command from scan_pending_q
  100. * and put to cmd_pending_q to resume scan operation
  101. */
  102. adapter->scan_delay_cnt = 0;
  103. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  104. cmd_node = list_first_entry(&adapter->scan_pending_q,
  105. struct cmd_ctrl_node, list);
  106. list_del(&cmd_node->list);
  107. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  108. mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
  109. }
  110. }
  111. /*
  112. * This function initializes the private structure and sets default
  113. * values to the members.
  114. *
  115. * Additionally, it also initializes all the locks and sets up all the
  116. * lists.
  117. */
  118. static int mwifiex_init_priv(struct mwifiex_private *priv)
  119. {
  120. u32 i;
  121. priv->media_connected = false;
  122. memset(priv->curr_addr, 0xff, ETH_ALEN);
  123. priv->pkt_tx_ctrl = 0;
  124. priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
  125. priv->data_rate = 0; /* Initially indicate the rate as auto */
  126. priv->is_data_rate_auto = true;
  127. priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
  128. priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
  129. priv->sec_info.wep_enabled = 0;
  130. priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
  131. priv->sec_info.encryption_mode = 0;
  132. for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
  133. memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
  134. priv->wep_key_curr_index = 0;
  135. priv->curr_pkt_filter = HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
  136. HostCmd_ACT_MAC_ETHERNETII_ENABLE;
  137. priv->beacon_period = 100; /* beacon interval */ ;
  138. priv->attempted_bss_desc = NULL;
  139. memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
  140. priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
  141. memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
  142. memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
  143. memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
  144. priv->assoc_rsp_size = 0;
  145. priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
  146. priv->atim_window = 0;
  147. priv->adhoc_state = ADHOC_IDLE;
  148. priv->tx_power_level = 0;
  149. priv->max_tx_power_level = 0;
  150. priv->min_tx_power_level = 0;
  151. priv->tx_rate = 0;
  152. priv->rxpd_htinfo = 0;
  153. priv->rxpd_rate = 0;
  154. priv->rate_bitmap = 0;
  155. priv->data_rssi_last = 0;
  156. priv->data_rssi_avg = 0;
  157. priv->data_nf_avg = 0;
  158. priv->data_nf_last = 0;
  159. priv->bcn_rssi_last = 0;
  160. priv->bcn_rssi_avg = 0;
  161. priv->bcn_nf_avg = 0;
  162. priv->bcn_nf_last = 0;
  163. memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
  164. memset(&priv->aes_key, 0, sizeof(priv->aes_key));
  165. priv->wpa_ie_len = 0;
  166. priv->wpa_is_gtk_set = false;
  167. memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
  168. priv->assoc_tlv_buf_len = 0;
  169. memset(&priv->wps, 0, sizeof(priv->wps));
  170. memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
  171. priv->gen_ie_buf_len = 0;
  172. memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
  173. priv->wmm_required = true;
  174. priv->wmm_enabled = false;
  175. priv->wmm_qosinfo = 0;
  176. priv->curr_bcn_buf = NULL;
  177. priv->curr_bcn_size = 0;
  178. priv->wps_ie = NULL;
  179. priv->wps_ie_len = 0;
  180. priv->scan_block = false;
  181. setup_timer(&priv->scan_delay_timer, scan_delay_timer_fn,
  182. (unsigned long)priv);
  183. return mwifiex_add_bss_prio_tbl(priv);
  184. }
  185. /*
  186. * This function allocates buffers for members of the adapter
  187. * structure.
  188. *
  189. * The memory allocated includes scan table, command buffers, and
  190. * sleep confirm command buffer. In addition, the queues are
  191. * also initialized.
  192. */
  193. static int mwifiex_allocate_adapter(struct mwifiex_adapter *adapter)
  194. {
  195. int ret;
  196. /* Allocate command buffer */
  197. ret = mwifiex_alloc_cmd_buffer(adapter);
  198. if (ret) {
  199. dev_err(adapter->dev, "%s: failed to alloc cmd buffer\n",
  200. __func__);
  201. return -1;
  202. }
  203. adapter->sleep_cfm =
  204. dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
  205. + INTF_HEADER_LEN);
  206. if (!adapter->sleep_cfm) {
  207. dev_err(adapter->dev, "%s: failed to alloc sleep cfm"
  208. " cmd buffer\n", __func__);
  209. return -1;
  210. }
  211. skb_reserve(adapter->sleep_cfm, INTF_HEADER_LEN);
  212. return 0;
  213. }
  214. /*
  215. * This function initializes the adapter structure and sets default
  216. * values to the members of adapter.
  217. *
  218. * This also initializes the WMM related parameters in the driver private
  219. * structures.
  220. */
  221. static void mwifiex_init_adapter(struct mwifiex_adapter *adapter)
  222. {
  223. struct mwifiex_opt_sleep_confirm *sleep_cfm_buf = NULL;
  224. skb_put(adapter->sleep_cfm, sizeof(struct mwifiex_opt_sleep_confirm));
  225. adapter->cmd_sent = false;
  226. if (adapter->iface_type == MWIFIEX_SDIO)
  227. adapter->data_sent = true;
  228. else
  229. adapter->data_sent = false;
  230. adapter->cmd_resp_received = false;
  231. adapter->event_received = false;
  232. adapter->data_received = false;
  233. adapter->surprise_removed = false;
  234. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  235. adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
  236. adapter->ps_state = PS_STATE_AWAKE;
  237. adapter->need_to_wakeup = false;
  238. adapter->scan_mode = HostCmd_BSS_MODE_ANY;
  239. adapter->specific_scan_time = MWIFIEX_SPECIFIC_SCAN_CHAN_TIME;
  240. adapter->active_scan_time = MWIFIEX_ACTIVE_SCAN_CHAN_TIME;
  241. adapter->passive_scan_time = MWIFIEX_PASSIVE_SCAN_CHAN_TIME;
  242. adapter->scan_probes = 1;
  243. adapter->multiple_dtim = 1;
  244. adapter->local_listen_interval = 0; /* default value in firmware
  245. will be used */
  246. adapter->is_deep_sleep = false;
  247. adapter->delay_null_pkt = false;
  248. adapter->delay_to_ps = 1000;
  249. adapter->enhanced_ps_mode = PS_MODE_AUTO;
  250. adapter->gen_null_pkt = false; /* Disable NULL Pkg generation by
  251. default */
  252. adapter->pps_uapsd_mode = false; /* Disable pps/uapsd mode by
  253. default */
  254. adapter->pm_wakeup_card_req = false;
  255. adapter->pm_wakeup_fw_try = false;
  256. adapter->max_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
  257. adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
  258. adapter->curr_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
  259. adapter->is_hs_configured = false;
  260. adapter->hs_cfg.conditions = cpu_to_le32(HOST_SLEEP_CFG_COND_DEF);
  261. adapter->hs_cfg.gpio = HOST_SLEEP_CFG_GPIO_DEF;
  262. adapter->hs_cfg.gap = HOST_SLEEP_CFG_GAP_DEF;
  263. adapter->hs_activated = false;
  264. memset(adapter->event_body, 0, sizeof(adapter->event_body));
  265. adapter->hw_dot_11n_dev_cap = 0;
  266. adapter->hw_dev_mcs_support = 0;
  267. adapter->sec_chan_offset = 0;
  268. adapter->adhoc_11n_enabled = false;
  269. mwifiex_wmm_init(adapter);
  270. if (adapter->sleep_cfm) {
  271. sleep_cfm_buf = (struct mwifiex_opt_sleep_confirm *)
  272. adapter->sleep_cfm->data;
  273. memset(sleep_cfm_buf, 0, adapter->sleep_cfm->len);
  274. sleep_cfm_buf->command =
  275. cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
  276. sleep_cfm_buf->size =
  277. cpu_to_le16(adapter->sleep_cfm->len);
  278. sleep_cfm_buf->result = 0;
  279. sleep_cfm_buf->action = cpu_to_le16(SLEEP_CONFIRM);
  280. sleep_cfm_buf->resp_ctrl = cpu_to_le16(RESP_NEEDED);
  281. }
  282. memset(&adapter->sleep_params, 0, sizeof(adapter->sleep_params));
  283. memset(&adapter->sleep_period, 0, sizeof(adapter->sleep_period));
  284. adapter->tx_lock_flag = false;
  285. adapter->null_pkt_interval = 0;
  286. adapter->fw_bands = 0;
  287. adapter->config_bands = 0;
  288. adapter->adhoc_start_band = 0;
  289. adapter->scan_channels = NULL;
  290. adapter->fw_release_number = 0;
  291. adapter->fw_cap_info = 0;
  292. memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
  293. adapter->event_cause = 0;
  294. adapter->region_code = 0;
  295. adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
  296. adapter->adhoc_awake_period = 0;
  297. memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
  298. adapter->arp_filter_size = 0;
  299. adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
  300. }
  301. /*
  302. * This function sets trans_start per tx_queue
  303. */
  304. void mwifiex_set_trans_start(struct net_device *dev)
  305. {
  306. int i;
  307. for (i = 0; i < dev->num_tx_queues; i++)
  308. netdev_get_tx_queue(dev, i)->trans_start = jiffies;
  309. dev->trans_start = jiffies;
  310. }
  311. /*
  312. * This function wakes up all queues in net_device
  313. */
  314. void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
  315. struct mwifiex_adapter *adapter)
  316. {
  317. unsigned long dev_queue_flags;
  318. spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
  319. netif_tx_wake_all_queues(netdev);
  320. spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
  321. }
  322. /*
  323. * This function stops all queues in net_device
  324. */
  325. void mwifiex_stop_net_dev_queue(struct net_device *netdev,
  326. struct mwifiex_adapter *adapter)
  327. {
  328. unsigned long dev_queue_flags;
  329. spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
  330. netif_tx_stop_all_queues(netdev);
  331. spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
  332. }
  333. /*
  334. * This function releases the lock variables and frees the locks and
  335. * associated locks.
  336. */
  337. static void mwifiex_free_lock_list(struct mwifiex_adapter *adapter)
  338. {
  339. struct mwifiex_private *priv;
  340. s32 i, j;
  341. /* Free lists */
  342. list_del(&adapter->cmd_free_q);
  343. list_del(&adapter->cmd_pending_q);
  344. list_del(&adapter->scan_pending_q);
  345. for (i = 0; i < adapter->priv_num; i++)
  346. list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
  347. for (i = 0; i < adapter->priv_num; i++) {
  348. if (adapter->priv[i]) {
  349. priv = adapter->priv[i];
  350. for (j = 0; j < MAX_NUM_TID; ++j)
  351. list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
  352. list_del(&priv->tx_ba_stream_tbl_ptr);
  353. list_del(&priv->rx_reorder_tbl_ptr);
  354. }
  355. }
  356. }
  357. /*
  358. * This function frees the adapter structure.
  359. *
  360. * The freeing operation is done recursively, by canceling all
  361. * pending commands, freeing the member buffers previously
  362. * allocated (command buffers, scan table buffer, sleep confirm
  363. * command buffer), stopping the timers and calling the cleanup
  364. * routines for every interface, before the actual adapter
  365. * structure is freed.
  366. */
  367. static void
  368. mwifiex_free_adapter(struct mwifiex_adapter *adapter)
  369. {
  370. if (!adapter) {
  371. pr_err("%s: adapter is NULL\n", __func__);
  372. return;
  373. }
  374. mwifiex_cancel_all_pending_cmd(adapter);
  375. /* Free lock variables */
  376. mwifiex_free_lock_list(adapter);
  377. /* Free command buffer */
  378. dev_dbg(adapter->dev, "info: free cmd buffer\n");
  379. mwifiex_free_cmd_buffer(adapter);
  380. del_timer(&adapter->cmd_timer);
  381. dev_dbg(adapter->dev, "info: free scan table\n");
  382. if (adapter->if_ops.cleanup_if)
  383. adapter->if_ops.cleanup_if(adapter);
  384. if (adapter->sleep_cfm)
  385. dev_kfree_skb_any(adapter->sleep_cfm);
  386. }
  387. /*
  388. * This function intializes the lock variables and
  389. * the list heads.
  390. */
  391. int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
  392. {
  393. struct mwifiex_private *priv;
  394. s32 i, j;
  395. spin_lock_init(&adapter->mwifiex_lock);
  396. spin_lock_init(&adapter->int_lock);
  397. spin_lock_init(&adapter->main_proc_lock);
  398. spin_lock_init(&adapter->mwifiex_cmd_lock);
  399. spin_lock_init(&adapter->queue_lock);
  400. for (i = 0; i < adapter->priv_num; i++) {
  401. if (adapter->priv[i]) {
  402. priv = adapter->priv[i];
  403. spin_lock_init(&priv->rx_pkt_lock);
  404. spin_lock_init(&priv->wmm.ra_list_spinlock);
  405. spin_lock_init(&priv->curr_bcn_buf_lock);
  406. }
  407. }
  408. /* Initialize cmd_free_q */
  409. INIT_LIST_HEAD(&adapter->cmd_free_q);
  410. /* Initialize cmd_pending_q */
  411. INIT_LIST_HEAD(&adapter->cmd_pending_q);
  412. /* Initialize scan_pending_q */
  413. INIT_LIST_HEAD(&adapter->scan_pending_q);
  414. spin_lock_init(&adapter->cmd_free_q_lock);
  415. spin_lock_init(&adapter->cmd_pending_q_lock);
  416. spin_lock_init(&adapter->scan_pending_q_lock);
  417. skb_queue_head_init(&adapter->usb_rx_data_q);
  418. for (i = 0; i < adapter->priv_num; ++i) {
  419. INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
  420. adapter->bss_prio_tbl[i].bss_prio_cur = NULL;
  421. spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
  422. }
  423. for (i = 0; i < adapter->priv_num; i++) {
  424. if (!adapter->priv[i])
  425. continue;
  426. priv = adapter->priv[i];
  427. for (j = 0; j < MAX_NUM_TID; ++j) {
  428. INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
  429. spin_lock_init(&priv->wmm.tid_tbl_ptr[j].tid_tbl_lock);
  430. }
  431. INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
  432. INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
  433. spin_lock_init(&priv->tx_ba_stream_tbl_lock);
  434. spin_lock_init(&priv->rx_reorder_tbl_lock);
  435. }
  436. return 0;
  437. }
  438. /*
  439. * This function initializes the firmware.
  440. *
  441. * The following operations are performed sequentially -
  442. * - Allocate adapter structure
  443. * - Initialize the adapter structure
  444. * - Initialize the private structure
  445. * - Add BSS priority tables to the adapter structure
  446. * - For each interface, send the init commands to firmware
  447. * - Send the first command in command pending queue, if available
  448. */
  449. int mwifiex_init_fw(struct mwifiex_adapter *adapter)
  450. {
  451. int ret;
  452. struct mwifiex_private *priv;
  453. u8 i, first_sta = true;
  454. int is_cmd_pend_q_empty;
  455. unsigned long flags;
  456. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  457. /* Allocate memory for member of adapter structure */
  458. ret = mwifiex_allocate_adapter(adapter);
  459. if (ret)
  460. return -1;
  461. /* Initialize adapter structure */
  462. mwifiex_init_adapter(adapter);
  463. for (i = 0; i < adapter->priv_num; i++) {
  464. if (adapter->priv[i]) {
  465. priv = adapter->priv[i];
  466. /* Initialize private structure */
  467. ret = mwifiex_init_priv(priv);
  468. if (ret)
  469. return -1;
  470. }
  471. }
  472. for (i = 0; i < adapter->priv_num; i++) {
  473. if (adapter->priv[i]) {
  474. ret = mwifiex_sta_init_cmd(adapter->priv[i], first_sta);
  475. if (ret == -1)
  476. return -1;
  477. first_sta = false;
  478. }
  479. }
  480. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  481. is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
  482. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  483. if (!is_cmd_pend_q_empty) {
  484. /* Send the first command in queue and return */
  485. if (mwifiex_main_process(adapter) != -1)
  486. ret = -EINPROGRESS;
  487. } else {
  488. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  489. }
  490. return ret;
  491. }
  492. /*
  493. * This function deletes the BSS priority tables.
  494. *
  495. * The function traverses through all the allocated BSS priority nodes
  496. * in every BSS priority table and frees them.
  497. */
  498. static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
  499. {
  500. int i;
  501. struct mwifiex_adapter *adapter = priv->adapter;
  502. struct mwifiex_bss_prio_node *bssprio_node, *tmp_node, **cur;
  503. struct list_head *head;
  504. spinlock_t *lock; /* bss priority lock */
  505. unsigned long flags;
  506. for (i = 0; i < adapter->priv_num; ++i) {
  507. head = &adapter->bss_prio_tbl[i].bss_prio_head;
  508. cur = &adapter->bss_prio_tbl[i].bss_prio_cur;
  509. lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
  510. dev_dbg(adapter->dev, "info: delete BSS priority table,"
  511. " bss_type = %d, bss_num = %d, i = %d,"
  512. " head = %p, cur = %p\n",
  513. priv->bss_type, priv->bss_num, i, head, *cur);
  514. if (*cur) {
  515. spin_lock_irqsave(lock, flags);
  516. if (list_empty(head)) {
  517. spin_unlock_irqrestore(lock, flags);
  518. continue;
  519. }
  520. bssprio_node = list_first_entry(head,
  521. struct mwifiex_bss_prio_node, list);
  522. spin_unlock_irqrestore(lock, flags);
  523. list_for_each_entry_safe(bssprio_node, tmp_node, head,
  524. list) {
  525. if (bssprio_node->priv == priv) {
  526. dev_dbg(adapter->dev, "info: Delete "
  527. "node %p, next = %p\n",
  528. bssprio_node, tmp_node);
  529. spin_lock_irqsave(lock, flags);
  530. list_del(&bssprio_node->list);
  531. spin_unlock_irqrestore(lock, flags);
  532. kfree(bssprio_node);
  533. }
  534. }
  535. *cur = (struct mwifiex_bss_prio_node *)head;
  536. }
  537. }
  538. }
  539. /*
  540. * This function is used to shutdown the driver.
  541. *
  542. * The following operations are performed sequentially -
  543. * - Check if already shut down
  544. * - Make sure the main process has stopped
  545. * - Clean up the Tx and Rx queues
  546. * - Delete BSS priority tables
  547. * - Free the adapter
  548. * - Notify completion
  549. */
  550. int
  551. mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
  552. {
  553. int ret = -EINPROGRESS;
  554. struct mwifiex_private *priv;
  555. s32 i;
  556. unsigned long flags;
  557. struct sk_buff *skb;
  558. /* mwifiex already shutdown */
  559. if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
  560. return 0;
  561. adapter->hw_status = MWIFIEX_HW_STATUS_CLOSING;
  562. /* wait for mwifiex_process to complete */
  563. if (adapter->mwifiex_processing) {
  564. dev_warn(adapter->dev, "main process is still running\n");
  565. return ret;
  566. }
  567. /* shut down mwifiex */
  568. dev_dbg(adapter->dev, "info: shutdown mwifiex...\n");
  569. /* Clean up Tx/Rx queues and delete BSS priority table */
  570. for (i = 0; i < adapter->priv_num; i++) {
  571. if (adapter->priv[i]) {
  572. priv = adapter->priv[i];
  573. mwifiex_clean_txrx(priv);
  574. mwifiex_delete_bss_prio_tbl(priv);
  575. }
  576. }
  577. spin_lock_irqsave(&adapter->mwifiex_lock, flags);
  578. if (adapter->if_ops.data_complete) {
  579. while ((skb = skb_dequeue(&adapter->usb_rx_data_q))) {
  580. struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
  581. priv = adapter->priv[rx_info->bss_num];
  582. if (priv)
  583. priv->stats.rx_dropped++;
  584. adapter->if_ops.data_complete(adapter, skb);
  585. }
  586. }
  587. /* Free adapter structure */
  588. mwifiex_free_adapter(adapter);
  589. spin_unlock_irqrestore(&adapter->mwifiex_lock, flags);
  590. /* Notify completion */
  591. ret = mwifiex_shutdown_fw_complete(adapter);
  592. return ret;
  593. }
  594. /*
  595. * This function downloads the firmware to the card.
  596. *
  597. * The actual download is preceded by two sanity checks -
  598. * - Check if firmware is already running
  599. * - Check if the interface is the winner to download the firmware
  600. *
  601. * ...and followed by another -
  602. * - Check if the firmware is downloaded successfully
  603. *
  604. * After download is successfully completed, the host interrupts are enabled.
  605. */
  606. int mwifiex_dnld_fw(struct mwifiex_adapter *adapter,
  607. struct mwifiex_fw_image *pmfw)
  608. {
  609. int ret;
  610. u32 poll_num = 1;
  611. if (adapter->if_ops.check_fw_status) {
  612. adapter->winner = 0;
  613. /* check if firmware is already running */
  614. ret = adapter->if_ops.check_fw_status(adapter, poll_num);
  615. if (!ret) {
  616. dev_notice(adapter->dev,
  617. "WLAN FW already running! Skip FW dnld\n");
  618. goto done;
  619. }
  620. poll_num = MAX_FIRMWARE_POLL_TRIES;
  621. /* check if we are the winner for downloading FW */
  622. if (!adapter->winner) {
  623. dev_notice(adapter->dev,
  624. "FW already running! Skip FW dnld\n");
  625. poll_num = MAX_MULTI_INTERFACE_POLL_TRIES;
  626. goto poll_fw;
  627. }
  628. }
  629. if (pmfw) {
  630. /* Download firmware with helper */
  631. ret = adapter->if_ops.prog_fw(adapter, pmfw);
  632. if (ret) {
  633. dev_err(adapter->dev, "prog_fw failed ret=%#x\n", ret);
  634. return ret;
  635. }
  636. }
  637. poll_fw:
  638. /* Check if the firmware is downloaded successfully or not */
  639. ret = adapter->if_ops.check_fw_status(adapter, poll_num);
  640. if (ret) {
  641. dev_err(adapter->dev, "FW failed to be active in time\n");
  642. return -1;
  643. }
  644. done:
  645. /* re-enable host interrupt for mwifiex after fw dnld is successful */
  646. if (adapter->if_ops.enable_int)
  647. adapter->if_ops.enable_int(adapter);
  648. return ret;
  649. }