main.c 28 KB

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