main.c 28 KB

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