main.c 28 KB

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