init.c 22 KB

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