main.c 28 KB

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