init.c 21 KB

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