main.c 25 KB

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