main.c 28 KB

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