init.c 22 KB

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