main.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /*
  2. * Marvell Wireless LAN device driver: major functions
  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 "main.h"
  20. #include "wmm.h"
  21. #include "cfg80211.h"
  22. #include "11n.h"
  23. #define VERSION "1.0"
  24. const char driver_version[] = "mwifiex " VERSION " (%s) ";
  25. static char *cal_data_cfg;
  26. module_param(cal_data_cfg, charp, 0);
  27. static void scan_delay_timer_fn(unsigned long data)
  28. {
  29. struct mwifiex_private *priv = (struct mwifiex_private *)data;
  30. struct mwifiex_adapter *adapter = priv->adapter;
  31. struct cmd_ctrl_node *cmd_node, *tmp_node;
  32. unsigned long flags;
  33. if (adapter->surprise_removed)
  34. return;
  35. if (adapter->scan_delay_cnt == MWIFIEX_MAX_SCAN_DELAY_CNT) {
  36. /*
  37. * Abort scan operation by cancelling all pending scan
  38. * commands
  39. */
  40. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  41. list_for_each_entry_safe(cmd_node, tmp_node,
  42. &adapter->scan_pending_q, list) {
  43. list_del(&cmd_node->list);
  44. mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
  45. }
  46. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  47. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  48. adapter->scan_processing = false;
  49. adapter->scan_delay_cnt = 0;
  50. adapter->empty_tx_q_cnt = 0;
  51. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  52. if (priv->scan_request) {
  53. dev_dbg(adapter->dev, "info: aborting scan\n");
  54. cfg80211_scan_done(priv->scan_request, 1);
  55. priv->scan_request = NULL;
  56. } else {
  57. priv->scan_aborting = false;
  58. dev_dbg(adapter->dev, "info: scan already aborted\n");
  59. }
  60. goto done;
  61. }
  62. if (!atomic_read(&priv->adapter->is_tx_received)) {
  63. adapter->empty_tx_q_cnt++;
  64. if (adapter->empty_tx_q_cnt == MWIFIEX_MAX_EMPTY_TX_Q_CNT) {
  65. /*
  66. * No Tx traffic for 200msec. Get scan command from
  67. * scan pending queue and put to cmd pending queue to
  68. * resume scan operation
  69. */
  70. adapter->scan_delay_cnt = 0;
  71. adapter->empty_tx_q_cnt = 0;
  72. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  73. cmd_node = list_first_entry(&adapter->scan_pending_q,
  74. struct cmd_ctrl_node, list);
  75. list_del(&cmd_node->list);
  76. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  77. flags);
  78. mwifiex_insert_cmd_to_pending_q(adapter, cmd_node,
  79. true);
  80. queue_work(adapter->workqueue, &adapter->main_work);
  81. goto done;
  82. }
  83. } else {
  84. adapter->empty_tx_q_cnt = 0;
  85. }
  86. /* Delay scan operation further by 20msec */
  87. mod_timer(&priv->scan_delay_timer, jiffies +
  88. msecs_to_jiffies(MWIFIEX_SCAN_DELAY_MSEC));
  89. adapter->scan_delay_cnt++;
  90. done:
  91. if (atomic_read(&priv->adapter->is_tx_received))
  92. atomic_set(&priv->adapter->is_tx_received, false);
  93. return;
  94. }
  95. /*
  96. * This function registers the device and performs all the necessary
  97. * initializations.
  98. *
  99. * The following initialization operations are performed -
  100. * - Allocate adapter structure
  101. * - Save interface specific operations table in adapter
  102. * - Call interface specific initialization routine
  103. * - Allocate private structures
  104. * - Set default adapter structure parameters
  105. * - Initialize locks
  106. *
  107. * In case of any errors during inittialization, this function also ensures
  108. * proper cleanup before exiting.
  109. */
  110. static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
  111. void **padapter)
  112. {
  113. struct mwifiex_adapter *adapter;
  114. int i;
  115. adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
  116. if (!adapter)
  117. return -ENOMEM;
  118. *padapter = adapter;
  119. adapter->card = card;
  120. /* Save interface specific operations in adapter */
  121. memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
  122. /* card specific initialization has been deferred until now .. */
  123. if (adapter->if_ops.init_if)
  124. if (adapter->if_ops.init_if(adapter))
  125. goto error;
  126. adapter->priv_num = 0;
  127. for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
  128. /* Allocate memory for private structure */
  129. adapter->priv[i] =
  130. kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
  131. if (!adapter->priv[i])
  132. goto error;
  133. adapter->priv[i]->adapter = adapter;
  134. adapter->priv_num++;
  135. setup_timer(&adapter->priv[i]->scan_delay_timer,
  136. scan_delay_timer_fn,
  137. (unsigned long)adapter->priv[i]);
  138. }
  139. mwifiex_init_lock_list(adapter);
  140. init_timer(&adapter->cmd_timer);
  141. adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
  142. adapter->cmd_timer.data = (unsigned long) adapter;
  143. return 0;
  144. error:
  145. dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
  146. for (i = 0; i < adapter->priv_num; i++)
  147. kfree(adapter->priv[i]);
  148. kfree(adapter);
  149. return -1;
  150. }
  151. /*
  152. * This function unregisters the device and performs all the necessary
  153. * cleanups.
  154. *
  155. * The following cleanup operations are performed -
  156. * - Free the timers
  157. * - Free beacon buffers
  158. * - Free private structures
  159. * - Free adapter structure
  160. */
  161. static int mwifiex_unregister(struct mwifiex_adapter *adapter)
  162. {
  163. s32 i;
  164. if (adapter->if_ops.cleanup_if)
  165. adapter->if_ops.cleanup_if(adapter);
  166. del_timer(&adapter->cmd_timer);
  167. /* Free private structures */
  168. for (i = 0; i < adapter->priv_num; i++) {
  169. if (adapter->priv[i]) {
  170. mwifiex_free_curr_bcn(adapter->priv[i]);
  171. del_timer_sync(&adapter->priv[i]->scan_delay_timer);
  172. kfree(adapter->priv[i]);
  173. }
  174. }
  175. kfree(adapter);
  176. return 0;
  177. }
  178. /*
  179. * The main process.
  180. *
  181. * This function is the main procedure of the driver and handles various driver
  182. * operations. It runs in a loop and provides the core functionalities.
  183. *
  184. * The main responsibilities of this function are -
  185. * - Ensure concurrency control
  186. * - Handle pending interrupts and call interrupt handlers
  187. * - Wake up the card if required
  188. * - Handle command responses and call response handlers
  189. * - Handle events and call event handlers
  190. * - Execute pending commands
  191. * - Transmit pending data packets
  192. */
  193. int mwifiex_main_process(struct mwifiex_adapter *adapter)
  194. {
  195. int ret = 0;
  196. unsigned long flags;
  197. struct sk_buff *skb;
  198. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  199. /* Check if already processing */
  200. if (adapter->mwifiex_processing) {
  201. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  202. goto exit_main_proc;
  203. } else {
  204. adapter->mwifiex_processing = true;
  205. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  206. }
  207. process_start:
  208. do {
  209. if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
  210. (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
  211. break;
  212. /* Handle pending interrupt if any */
  213. if (adapter->int_status) {
  214. if (adapter->hs_activated)
  215. mwifiex_process_hs_config(adapter);
  216. if (adapter->if_ops.process_int_status)
  217. adapter->if_ops.process_int_status(adapter);
  218. }
  219. /* Need to wake up the card ? */
  220. if ((adapter->ps_state == PS_STATE_SLEEP) &&
  221. (adapter->pm_wakeup_card_req &&
  222. !adapter->pm_wakeup_fw_try) &&
  223. (is_command_pending(adapter) ||
  224. !mwifiex_wmm_lists_empty(adapter))) {
  225. adapter->pm_wakeup_fw_try = true;
  226. adapter->if_ops.wakeup(adapter);
  227. continue;
  228. }
  229. if (IS_CARD_RX_RCVD(adapter)) {
  230. adapter->pm_wakeup_fw_try = false;
  231. if (adapter->ps_state == PS_STATE_SLEEP)
  232. adapter->ps_state = PS_STATE_AWAKE;
  233. } else {
  234. /* We have tried to wakeup the card already */
  235. if (adapter->pm_wakeup_fw_try)
  236. break;
  237. if (adapter->ps_state != PS_STATE_AWAKE ||
  238. adapter->tx_lock_flag)
  239. break;
  240. if ((adapter->scan_processing &&
  241. !adapter->scan_delay_cnt) || adapter->data_sent ||
  242. mwifiex_wmm_lists_empty(adapter)) {
  243. if (adapter->cmd_sent || adapter->curr_cmd ||
  244. (!is_command_pending(adapter)))
  245. break;
  246. }
  247. }
  248. /* Check Rx data for USB */
  249. if (adapter->iface_type == MWIFIEX_USB)
  250. while ((skb = skb_dequeue(&adapter->usb_rx_data_q)))
  251. mwifiex_handle_rx_packet(adapter, skb);
  252. /* Check for Cmd Resp */
  253. if (adapter->cmd_resp_received) {
  254. adapter->cmd_resp_received = false;
  255. mwifiex_process_cmdresp(adapter);
  256. /* call mwifiex back when init_fw is done */
  257. if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
  258. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  259. mwifiex_init_fw_complete(adapter);
  260. }
  261. }
  262. /* Check for event */
  263. if (adapter->event_received) {
  264. adapter->event_received = false;
  265. mwifiex_process_event(adapter);
  266. }
  267. /* Check if we need to confirm Sleep Request
  268. received previously */
  269. if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
  270. if (!adapter->cmd_sent && !adapter->curr_cmd)
  271. mwifiex_check_ps_cond(adapter);
  272. }
  273. /* * The ps_state may have been changed during processing of
  274. * Sleep Request event.
  275. */
  276. if ((adapter->ps_state == PS_STATE_SLEEP) ||
  277. (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
  278. (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
  279. adapter->tx_lock_flag)
  280. continue;
  281. if (!adapter->cmd_sent && !adapter->curr_cmd) {
  282. if (mwifiex_exec_next_cmd(adapter) == -1) {
  283. ret = -1;
  284. break;
  285. }
  286. }
  287. if ((!adapter->scan_processing || adapter->scan_delay_cnt) &&
  288. !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
  289. mwifiex_wmm_process_tx(adapter);
  290. if (adapter->hs_activated) {
  291. adapter->is_hs_configured = false;
  292. mwifiex_hs_activated_event
  293. (mwifiex_get_priv
  294. (adapter, MWIFIEX_BSS_ROLE_ANY),
  295. false);
  296. }
  297. }
  298. if (adapter->delay_null_pkt && !adapter->cmd_sent &&
  299. !adapter->curr_cmd && !is_command_pending(adapter) &&
  300. mwifiex_wmm_lists_empty(adapter)) {
  301. if (!mwifiex_send_null_packet
  302. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
  303. MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
  304. MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
  305. adapter->delay_null_pkt = false;
  306. adapter->ps_state = PS_STATE_SLEEP;
  307. }
  308. break;
  309. }
  310. } while (true);
  311. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  312. if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter)) {
  313. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  314. goto process_start;
  315. }
  316. adapter->mwifiex_processing = false;
  317. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  318. exit_main_proc:
  319. if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
  320. mwifiex_shutdown_drv(adapter);
  321. return ret;
  322. }
  323. EXPORT_SYMBOL_GPL(mwifiex_main_process);
  324. /*
  325. * This function frees the adapter structure.
  326. *
  327. * Additionally, this closes the netlink socket, frees the timers
  328. * and private structures.
  329. */
  330. static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
  331. {
  332. if (!adapter) {
  333. pr_err("%s: adapter is NULL\n", __func__);
  334. return;
  335. }
  336. mwifiex_unregister(adapter);
  337. pr_debug("info: %s: free adapter\n", __func__);
  338. }
  339. /*
  340. * This function cancels all works in the queue and destroys
  341. * the main workqueue.
  342. */
  343. static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
  344. {
  345. flush_workqueue(adapter->workqueue);
  346. destroy_workqueue(adapter->workqueue);
  347. adapter->workqueue = NULL;
  348. }
  349. /*
  350. * This function gets firmware and initializes it.
  351. *
  352. * The main initialization steps followed are -
  353. * - Download the correct firmware to card
  354. * - Issue the init commands to firmware
  355. */
  356. static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
  357. {
  358. int ret, i;
  359. char fmt[64];
  360. struct mwifiex_private *priv;
  361. struct mwifiex_adapter *adapter = context;
  362. struct mwifiex_fw_image fw;
  363. struct semaphore *sem = adapter->card_sem;
  364. bool init_failed = false;
  365. if (!firmware) {
  366. dev_err(adapter->dev,
  367. "Failed to get firmware %s\n", adapter->fw_name);
  368. goto err_dnld_fw;
  369. }
  370. memset(&fw, 0, sizeof(struct mwifiex_fw_image));
  371. adapter->firmware = firmware;
  372. fw.fw_buf = (u8 *) adapter->firmware->data;
  373. fw.fw_len = adapter->firmware->size;
  374. if (adapter->if_ops.dnld_fw)
  375. ret = adapter->if_ops.dnld_fw(adapter, &fw);
  376. else
  377. ret = mwifiex_dnld_fw(adapter, &fw);
  378. if (ret == -1)
  379. goto err_dnld_fw;
  380. dev_notice(adapter->dev, "WLAN FW is active\n");
  381. if (cal_data_cfg) {
  382. if ((request_firmware(&adapter->cal_data, cal_data_cfg,
  383. adapter->dev)) < 0)
  384. dev_err(adapter->dev,
  385. "Cal data request_firmware() failed\n");
  386. }
  387. /* enable host interrupt after fw dnld is successful */
  388. if (adapter->if_ops.enable_int) {
  389. if (adapter->if_ops.enable_int(adapter))
  390. goto err_dnld_fw;
  391. }
  392. adapter->init_wait_q_woken = false;
  393. ret = mwifiex_init_fw(adapter);
  394. if (ret == -1) {
  395. goto err_init_fw;
  396. } else if (!ret) {
  397. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  398. goto done;
  399. }
  400. /* Wait for mwifiex_init to complete */
  401. wait_event_interruptible(adapter->init_wait_q,
  402. adapter->init_wait_q_woken);
  403. if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
  404. goto err_init_fw;
  405. priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
  406. if (mwifiex_register_cfg80211(adapter)) {
  407. dev_err(adapter->dev, "cannot register with cfg80211\n");
  408. goto err_register_cfg80211;
  409. }
  410. rtnl_lock();
  411. /* Create station interface by default */
  412. if (!mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
  413. NL80211_IFTYPE_STATION, NULL, NULL)) {
  414. dev_err(adapter->dev, "cannot create default STA interface\n");
  415. goto err_add_intf;
  416. }
  417. rtnl_unlock();
  418. mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
  419. dev_notice(adapter->dev, "driver_version = %s\n", fmt);
  420. goto done;
  421. err_add_intf:
  422. for (i = 0; i < adapter->priv_num; i++) {
  423. priv = adapter->priv[i];
  424. if (!priv)
  425. continue;
  426. if (priv->wdev && priv->netdev)
  427. mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
  428. }
  429. rtnl_unlock();
  430. err_register_cfg80211:
  431. wiphy_unregister(adapter->wiphy);
  432. wiphy_free(adapter->wiphy);
  433. err_init_fw:
  434. if (adapter->if_ops.disable_int)
  435. adapter->if_ops.disable_int(adapter);
  436. err_dnld_fw:
  437. pr_debug("info: %s: unregister device\n", __func__);
  438. if (adapter->if_ops.unregister_dev)
  439. adapter->if_ops.unregister_dev(adapter);
  440. if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
  441. (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
  442. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  443. adapter->init_wait_q_woken = false;
  444. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  445. wait_event_interruptible(adapter->init_wait_q,
  446. adapter->init_wait_q_woken);
  447. }
  448. adapter->surprise_removed = true;
  449. mwifiex_terminate_workqueue(adapter);
  450. init_failed = true;
  451. done:
  452. if (adapter->cal_data) {
  453. release_firmware(adapter->cal_data);
  454. adapter->cal_data = NULL;
  455. }
  456. if (adapter->firmware) {
  457. release_firmware(adapter->firmware);
  458. adapter->firmware = NULL;
  459. }
  460. complete(&adapter->fw_load);
  461. if (init_failed)
  462. mwifiex_free_adapter(adapter);
  463. up(sem);
  464. return;
  465. }
  466. /*
  467. * This function initializes the hardware and gets firmware.
  468. */
  469. static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
  470. {
  471. int ret;
  472. init_completion(&adapter->fw_load);
  473. ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
  474. adapter->dev, GFP_KERNEL, adapter,
  475. mwifiex_fw_dpc);
  476. if (ret < 0)
  477. dev_err(adapter->dev,
  478. "request_firmware_nowait() returned error %d\n", ret);
  479. return ret;
  480. }
  481. /*
  482. * CFG802.11 network device handler for open.
  483. *
  484. * Starts the data queue.
  485. */
  486. static int
  487. mwifiex_open(struct net_device *dev)
  488. {
  489. netif_tx_start_all_queues(dev);
  490. return 0;
  491. }
  492. /*
  493. * CFG802.11 network device handler for close.
  494. */
  495. static int
  496. mwifiex_close(struct net_device *dev)
  497. {
  498. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  499. if (priv->scan_request) {
  500. dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
  501. cfg80211_scan_done(priv->scan_request, 1);
  502. priv->scan_request = NULL;
  503. priv->scan_aborting = true;
  504. }
  505. return 0;
  506. }
  507. /*
  508. * Add buffer into wmm tx queue and queue work to transmit it.
  509. */
  510. int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
  511. {
  512. struct netdev_queue *txq;
  513. int index = mwifiex_1d_to_wmm_queue[skb->priority];
  514. if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
  515. txq = netdev_get_tx_queue(priv->netdev, index);
  516. if (!netif_tx_queue_stopped(txq)) {
  517. netif_tx_stop_queue(txq);
  518. dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
  519. }
  520. }
  521. atomic_inc(&priv->adapter->tx_pending);
  522. mwifiex_wmm_add_buf_txqueue(priv, skb);
  523. if (priv->adapter->scan_delay_cnt)
  524. atomic_set(&priv->adapter->is_tx_received, true);
  525. queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
  526. return 0;
  527. }
  528. /*
  529. * CFG802.11 network device handler for data transmission.
  530. */
  531. static int
  532. mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  533. {
  534. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  535. struct sk_buff *new_skb;
  536. struct mwifiex_txinfo *tx_info;
  537. struct timeval tv;
  538. dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
  539. jiffies, priv->bss_type, priv->bss_num);
  540. if (priv->adapter->surprise_removed) {
  541. kfree_skb(skb);
  542. priv->stats.tx_dropped++;
  543. return 0;
  544. }
  545. if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
  546. dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
  547. kfree_skb(skb);
  548. priv->stats.tx_dropped++;
  549. return 0;
  550. }
  551. if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
  552. dev_dbg(priv->adapter->dev,
  553. "data: Tx: insufficient skb headroom %d\n",
  554. skb_headroom(skb));
  555. /* Insufficient skb headroom - allocate a new skb */
  556. new_skb =
  557. skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
  558. if (unlikely(!new_skb)) {
  559. dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
  560. kfree_skb(skb);
  561. priv->stats.tx_dropped++;
  562. return 0;
  563. }
  564. kfree_skb(skb);
  565. skb = new_skb;
  566. dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
  567. skb_headroom(skb));
  568. }
  569. tx_info = MWIFIEX_SKB_TXCB(skb);
  570. tx_info->bss_num = priv->bss_num;
  571. tx_info->bss_type = priv->bss_type;
  572. /* Record the current time the packet was queued; used to
  573. * determine the amount of time the packet was queued in
  574. * the driver before it was sent to the firmware.
  575. * The delay is then sent along with the packet to the
  576. * firmware for aggregate delay calculation for stats and
  577. * MSDU lifetime expiry.
  578. */
  579. do_gettimeofday(&tv);
  580. skb->tstamp = timeval_to_ktime(tv);
  581. mwifiex_queue_tx_pkt(priv, skb);
  582. return 0;
  583. }
  584. /*
  585. * CFG802.11 network device handler for setting MAC address.
  586. */
  587. static int
  588. mwifiex_set_mac_address(struct net_device *dev, void *addr)
  589. {
  590. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  591. struct sockaddr *hw_addr = addr;
  592. int ret;
  593. memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
  594. /* Send request to firmware */
  595. ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
  596. HostCmd_ACT_GEN_SET, 0, NULL);
  597. if (!ret)
  598. memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
  599. else
  600. dev_err(priv->adapter->dev,
  601. "set mac address failed: ret=%d\n", ret);
  602. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  603. return ret;
  604. }
  605. /*
  606. * CFG802.11 network device handler for setting multicast list.
  607. */
  608. static void mwifiex_set_multicast_list(struct net_device *dev)
  609. {
  610. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  611. struct mwifiex_multicast_list mcast_list;
  612. if (dev->flags & IFF_PROMISC) {
  613. mcast_list.mode = MWIFIEX_PROMISC_MODE;
  614. } else if (dev->flags & IFF_ALLMULTI ||
  615. netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
  616. mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
  617. } else {
  618. mcast_list.mode = MWIFIEX_MULTICAST_MODE;
  619. mcast_list.num_multicast_addr =
  620. mwifiex_copy_mcast_addr(&mcast_list, dev);
  621. }
  622. mwifiex_request_set_multicast_list(priv, &mcast_list);
  623. }
  624. /*
  625. * CFG802.11 network device handler for transmission timeout.
  626. */
  627. static void
  628. mwifiex_tx_timeout(struct net_device *dev)
  629. {
  630. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  631. priv->num_tx_timeout++;
  632. priv->tx_timeout_cnt++;
  633. dev_err(priv->adapter->dev,
  634. "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
  635. jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
  636. mwifiex_set_trans_start(dev);
  637. if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
  638. priv->adapter->if_ops.card_reset) {
  639. dev_err(priv->adapter->dev,
  640. "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
  641. priv->adapter->if_ops.card_reset(priv->adapter);
  642. }
  643. }
  644. /*
  645. * CFG802.11 network device handler for statistics retrieval.
  646. */
  647. static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
  648. {
  649. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  650. return &priv->stats;
  651. }
  652. static u16
  653. mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb)
  654. {
  655. skb->priority = cfg80211_classify8021d(skb);
  656. return mwifiex_1d_to_wmm_queue[skb->priority];
  657. }
  658. /* Network device handlers */
  659. static const struct net_device_ops mwifiex_netdev_ops = {
  660. .ndo_open = mwifiex_open,
  661. .ndo_stop = mwifiex_close,
  662. .ndo_start_xmit = mwifiex_hard_start_xmit,
  663. .ndo_set_mac_address = mwifiex_set_mac_address,
  664. .ndo_tx_timeout = mwifiex_tx_timeout,
  665. .ndo_get_stats = mwifiex_get_stats,
  666. .ndo_set_rx_mode = mwifiex_set_multicast_list,
  667. .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
  668. };
  669. /*
  670. * This function initializes the private structure parameters.
  671. *
  672. * The following wait queues are initialized -
  673. * - IOCTL wait queue
  674. * - Command wait queue
  675. * - Statistics wait queue
  676. *
  677. * ...and the following default parameters are set -
  678. * - Current key index : Set to 0
  679. * - Rate index : Set to auto
  680. * - Media connected : Set to disconnected
  681. * - Adhoc link sensed : Set to false
  682. * - Nick name : Set to null
  683. * - Number of Tx timeout : Set to 0
  684. * - Device address : Set to current address
  685. *
  686. * In addition, the CFG80211 work queue is also created.
  687. */
  688. void mwifiex_init_priv_params(struct mwifiex_private *priv,
  689. struct net_device *dev)
  690. {
  691. dev->netdev_ops = &mwifiex_netdev_ops;
  692. dev->destructor = free_netdev;
  693. /* Initialize private structure */
  694. priv->current_key_index = 0;
  695. priv->media_connected = false;
  696. memset(&priv->nick_name, 0, sizeof(priv->nick_name));
  697. memset(priv->mgmt_ie, 0,
  698. sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
  699. priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
  700. priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
  701. priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
  702. priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
  703. priv->num_tx_timeout = 0;
  704. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  705. }
  706. /*
  707. * This function check if command is pending.
  708. */
  709. int is_command_pending(struct mwifiex_adapter *adapter)
  710. {
  711. unsigned long flags;
  712. int is_cmd_pend_q_empty;
  713. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  714. is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
  715. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  716. return !is_cmd_pend_q_empty;
  717. }
  718. /*
  719. * This is the main work queue function.
  720. *
  721. * It handles the main process, which in turn handles the complete
  722. * driver operations.
  723. */
  724. static void mwifiex_main_work_queue(struct work_struct *work)
  725. {
  726. struct mwifiex_adapter *adapter =
  727. container_of(work, struct mwifiex_adapter, main_work);
  728. if (adapter->surprise_removed)
  729. return;
  730. mwifiex_main_process(adapter);
  731. }
  732. /*
  733. * This function adds the card.
  734. *
  735. * This function follows the following major steps to set up the device -
  736. * - Initialize software. This includes probing the card, registering
  737. * the interface operations table, and allocating/initializing the
  738. * adapter structure
  739. * - Set up the netlink socket
  740. * - Create and start the main work queue
  741. * - Register the device
  742. * - Initialize firmware and hardware
  743. * - Add logical interfaces
  744. */
  745. int
  746. mwifiex_add_card(void *card, struct semaphore *sem,
  747. struct mwifiex_if_ops *if_ops, u8 iface_type)
  748. {
  749. struct mwifiex_adapter *adapter;
  750. if (down_interruptible(sem))
  751. goto exit_sem_err;
  752. if (mwifiex_register(card, if_ops, (void **)&adapter)) {
  753. pr_err("%s: software init failed\n", __func__);
  754. goto err_init_sw;
  755. }
  756. adapter->iface_type = iface_type;
  757. adapter->card_sem = sem;
  758. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  759. adapter->surprise_removed = false;
  760. init_waitqueue_head(&adapter->init_wait_q);
  761. adapter->is_suspended = false;
  762. adapter->hs_activated = false;
  763. init_waitqueue_head(&adapter->hs_activate_wait_q);
  764. adapter->cmd_wait_q_required = false;
  765. init_waitqueue_head(&adapter->cmd_wait_q.wait);
  766. adapter->cmd_wait_q.status = 0;
  767. adapter->scan_wait_q_woken = false;
  768. adapter->workqueue =
  769. alloc_workqueue("MWIFIEX_WORK_QUEUE",
  770. WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  771. if (!adapter->workqueue)
  772. goto err_kmalloc;
  773. INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
  774. /* Register the device. Fill up the private data structure with relevant
  775. information from the card. */
  776. if (adapter->if_ops.register_dev(adapter)) {
  777. pr_err("%s: failed to register mwifiex device\n", __func__);
  778. goto err_registerdev;
  779. }
  780. if (mwifiex_init_hw_fw(adapter)) {
  781. pr_err("%s: firmware init failed\n", __func__);
  782. goto err_init_fw;
  783. }
  784. return 0;
  785. err_init_fw:
  786. pr_debug("info: %s: unregister device\n", __func__);
  787. if (adapter->if_ops.unregister_dev)
  788. adapter->if_ops.unregister_dev(adapter);
  789. if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
  790. (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
  791. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  792. adapter->init_wait_q_woken = false;
  793. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  794. wait_event_interruptible(adapter->init_wait_q,
  795. adapter->init_wait_q_woken);
  796. }
  797. err_registerdev:
  798. adapter->surprise_removed = true;
  799. mwifiex_terminate_workqueue(adapter);
  800. err_kmalloc:
  801. mwifiex_free_adapter(adapter);
  802. err_init_sw:
  803. up(sem);
  804. exit_sem_err:
  805. return -1;
  806. }
  807. EXPORT_SYMBOL_GPL(mwifiex_add_card);
  808. /*
  809. * This function removes the card.
  810. *
  811. * This function follows the following major steps to remove the device -
  812. * - Stop data traffic
  813. * - Shutdown firmware
  814. * - Remove the logical interfaces
  815. * - Terminate the work queue
  816. * - Unregister the device
  817. * - Free the adapter structure
  818. */
  819. int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
  820. {
  821. struct mwifiex_private *priv = NULL;
  822. int i;
  823. if (down_interruptible(sem))
  824. goto exit_sem_err;
  825. if (!adapter)
  826. goto exit_remove;
  827. /* We can no longer handle interrupts once we start doing the teardown
  828. * below. */
  829. if (adapter->if_ops.disable_int)
  830. adapter->if_ops.disable_int(adapter);
  831. adapter->surprise_removed = true;
  832. /* Stop data */
  833. for (i = 0; i < adapter->priv_num; i++) {
  834. priv = adapter->priv[i];
  835. if (priv && priv->netdev) {
  836. mwifiex_stop_net_dev_queue(priv->netdev, adapter);
  837. if (netif_carrier_ok(priv->netdev))
  838. netif_carrier_off(priv->netdev);
  839. }
  840. }
  841. dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
  842. adapter->init_wait_q_woken = false;
  843. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  844. wait_event_interruptible(adapter->init_wait_q,
  845. adapter->init_wait_q_woken);
  846. dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
  847. if (atomic_read(&adapter->rx_pending) ||
  848. atomic_read(&adapter->tx_pending) ||
  849. atomic_read(&adapter->cmd_pending)) {
  850. dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
  851. "cmd_pending=%d\n",
  852. atomic_read(&adapter->rx_pending),
  853. atomic_read(&adapter->tx_pending),
  854. atomic_read(&adapter->cmd_pending));
  855. }
  856. for (i = 0; i < adapter->priv_num; i++) {
  857. priv = adapter->priv[i];
  858. if (!priv)
  859. continue;
  860. rtnl_lock();
  861. if (priv->wdev && priv->netdev)
  862. mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
  863. rtnl_unlock();
  864. }
  865. priv = adapter->priv[0];
  866. if (!priv || !priv->wdev)
  867. goto exit_remove;
  868. wiphy_unregister(priv->wdev->wiphy);
  869. wiphy_free(priv->wdev->wiphy);
  870. for (i = 0; i < adapter->priv_num; i++) {
  871. priv = adapter->priv[i];
  872. if (priv)
  873. kfree(priv->wdev);
  874. }
  875. mwifiex_terminate_workqueue(adapter);
  876. /* Unregister device */
  877. dev_dbg(adapter->dev, "info: unregister device\n");
  878. if (adapter->if_ops.unregister_dev)
  879. adapter->if_ops.unregister_dev(adapter);
  880. /* Free adapter structure */
  881. dev_dbg(adapter->dev, "info: free adapter\n");
  882. mwifiex_free_adapter(adapter);
  883. exit_remove:
  884. up(sem);
  885. exit_sem_err:
  886. return 0;
  887. }
  888. EXPORT_SYMBOL_GPL(mwifiex_remove_card);
  889. /*
  890. * This function initializes the module.
  891. *
  892. * The debug FS is also initialized if configured.
  893. */
  894. static int
  895. mwifiex_init_module(void)
  896. {
  897. #ifdef CONFIG_DEBUG_FS
  898. mwifiex_debugfs_init();
  899. #endif
  900. return 0;
  901. }
  902. /*
  903. * This function cleans up the module.
  904. *
  905. * The debug FS is removed if available.
  906. */
  907. static void
  908. mwifiex_cleanup_module(void)
  909. {
  910. #ifdef CONFIG_DEBUG_FS
  911. mwifiex_debugfs_remove();
  912. #endif
  913. }
  914. module_init(mwifiex_init_module);
  915. module_exit(mwifiex_cleanup_module);
  916. MODULE_AUTHOR("Marvell International Ltd.");
  917. MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
  918. MODULE_VERSION(VERSION);
  919. MODULE_LICENSE("GPL v2");