main.c 28 KB

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