main.c 28 KB

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