main.c 31 KB

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