init.c 22 KB

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