main.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. /*
  2. * Marvell Wireless LAN device driver: major functions
  3. *
  4. * Copyright (C) 2011-2014, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include "main.h"
  20. #include "wmm.h"
  21. #include "cfg80211.h"
  22. #include "11n.h"
  23. #define VERSION "1.0"
  24. static unsigned int debug_mask = MWIFIEX_DEFAULT_DEBUG_MASK;
  25. module_param(debug_mask, uint, 0);
  26. MODULE_PARM_DESC(debug_mask, "bitmap for debug flags");
  27. const char driver_version[] = "mwifiex " VERSION " (%s) ";
  28. static char *cal_data_cfg;
  29. module_param(cal_data_cfg, charp, 0);
  30. static unsigned short driver_mode;
  31. module_param(driver_mode, ushort, 0);
  32. MODULE_PARM_DESC(driver_mode,
  33. "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
  34. /*
  35. * This function registers the device and performs all the necessary
  36. * initializations.
  37. *
  38. * The following initialization operations are performed -
  39. * - Allocate adapter structure
  40. * - Save interface specific operations table in adapter
  41. * - Call interface specific initialization routine
  42. * - Allocate private structures
  43. * - Set default adapter structure parameters
  44. * - Initialize locks
  45. *
  46. * In case of any errors during inittialization, this function also ensures
  47. * proper cleanup before exiting.
  48. */
  49. static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
  50. void **padapter)
  51. {
  52. struct mwifiex_adapter *adapter;
  53. int i;
  54. adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
  55. if (!adapter)
  56. return -ENOMEM;
  57. *padapter = adapter;
  58. adapter->card = card;
  59. /* Save interface specific operations in adapter */
  60. memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
  61. adapter->debug_mask = debug_mask;
  62. /* card specific initialization has been deferred until now .. */
  63. if (adapter->if_ops.init_if)
  64. if (adapter->if_ops.init_if(adapter))
  65. goto error;
  66. adapter->priv_num = 0;
  67. for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
  68. /* Allocate memory for private structure */
  69. adapter->priv[i] =
  70. kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
  71. if (!adapter->priv[i])
  72. goto error;
  73. adapter->priv[i]->adapter = adapter;
  74. adapter->priv_num++;
  75. }
  76. mwifiex_init_lock_list(adapter);
  77. setup_timer(&adapter->cmd_timer, mwifiex_cmd_timeout_func,
  78. (unsigned long)adapter);
  79. return 0;
  80. error:
  81. mwifiex_dbg(adapter, ERROR,
  82. "info: leave mwifiex_register with error\n");
  83. for (i = 0; i < adapter->priv_num; i++)
  84. kfree(adapter->priv[i]);
  85. kfree(adapter);
  86. return -1;
  87. }
  88. /*
  89. * This function unregisters the device and performs all the necessary
  90. * cleanups.
  91. *
  92. * The following cleanup operations are performed -
  93. * - Free the timers
  94. * - Free beacon buffers
  95. * - Free private structures
  96. * - Free adapter structure
  97. */
  98. static int mwifiex_unregister(struct mwifiex_adapter *adapter)
  99. {
  100. s32 i;
  101. if (adapter->if_ops.cleanup_if)
  102. adapter->if_ops.cleanup_if(adapter);
  103. del_timer_sync(&adapter->cmd_timer);
  104. /* Free private structures */
  105. for (i = 0; i < adapter->priv_num; i++) {
  106. if (adapter->priv[i]) {
  107. mwifiex_free_curr_bcn(adapter->priv[i]);
  108. kfree(adapter->priv[i]);
  109. }
  110. }
  111. vfree(adapter->chan_stats);
  112. kfree(adapter);
  113. return 0;
  114. }
  115. void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
  116. {
  117. unsigned long flags;
  118. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  119. if (adapter->mwifiex_processing) {
  120. adapter->more_task_flag = true;
  121. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  122. } else {
  123. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  124. queue_work(adapter->workqueue, &adapter->main_work);
  125. }
  126. }
  127. EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
  128. static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
  129. {
  130. unsigned long flags;
  131. spin_lock_irqsave(&adapter->rx_proc_lock, flags);
  132. if (adapter->rx_processing) {
  133. spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
  134. } else {
  135. spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
  136. queue_work(adapter->rx_workqueue, &adapter->rx_work);
  137. }
  138. }
  139. static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
  140. {
  141. unsigned long flags;
  142. struct sk_buff *skb;
  143. struct mwifiex_rxinfo *rx_info;
  144. spin_lock_irqsave(&adapter->rx_proc_lock, flags);
  145. if (adapter->rx_processing || adapter->rx_locked) {
  146. spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
  147. goto exit_rx_proc;
  148. } else {
  149. adapter->rx_processing = true;
  150. spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
  151. }
  152. /* Check for Rx data */
  153. while ((skb = skb_dequeue(&adapter->rx_data_q))) {
  154. atomic_dec(&adapter->rx_pending);
  155. if ((adapter->delay_main_work ||
  156. adapter->iface_type == MWIFIEX_USB) &&
  157. (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
  158. if (adapter->if_ops.submit_rem_rx_urbs)
  159. adapter->if_ops.submit_rem_rx_urbs(adapter);
  160. adapter->delay_main_work = false;
  161. mwifiex_queue_main_work(adapter);
  162. }
  163. rx_info = MWIFIEX_SKB_RXCB(skb);
  164. if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
  165. if (adapter->if_ops.deaggr_pkt)
  166. adapter->if_ops.deaggr_pkt(adapter, skb);
  167. dev_kfree_skb_any(skb);
  168. } else {
  169. mwifiex_handle_rx_packet(adapter, skb);
  170. }
  171. }
  172. spin_lock_irqsave(&adapter->rx_proc_lock, flags);
  173. adapter->rx_processing = false;
  174. spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
  175. exit_rx_proc:
  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. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  198. /* Check if already processing */
  199. if (adapter->mwifiex_processing || adapter->main_locked) {
  200. adapter->more_task_flag = true;
  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. /* For non-USB interfaces, If we process interrupts first, it
  213. * would increase RX pending even further. Avoid this by
  214. * checking if rx_pending has crossed high threshold and
  215. * schedule rx work queue and then process interrupts.
  216. * For USB interface, there are no interrupts. We already have
  217. * HIGH_RX_PENDING check in usb.c
  218. */
  219. if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
  220. adapter->iface_type != MWIFIEX_USB) {
  221. adapter->delay_main_work = true;
  222. mwifiex_queue_rx_work(adapter);
  223. break;
  224. }
  225. /* Handle pending interrupt if any */
  226. if (adapter->int_status) {
  227. if (adapter->hs_activated)
  228. mwifiex_process_hs_config(adapter);
  229. if (adapter->if_ops.process_int_status)
  230. adapter->if_ops.process_int_status(adapter);
  231. }
  232. if (adapter->rx_work_enabled && adapter->data_received)
  233. mwifiex_queue_rx_work(adapter);
  234. /* Need to wake up the card ? */
  235. if ((adapter->ps_state == PS_STATE_SLEEP) &&
  236. (adapter->pm_wakeup_card_req &&
  237. !adapter->pm_wakeup_fw_try) &&
  238. (is_command_pending(adapter) ||
  239. !skb_queue_empty(&adapter->tx_data_q) ||
  240. !mwifiex_bypass_txlist_empty(adapter) ||
  241. !mwifiex_wmm_lists_empty(adapter))) {
  242. adapter->pm_wakeup_fw_try = true;
  243. mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
  244. adapter->if_ops.wakeup(adapter);
  245. continue;
  246. }
  247. if (IS_CARD_RX_RCVD(adapter)) {
  248. adapter->data_received = false;
  249. adapter->pm_wakeup_fw_try = false;
  250. del_timer(&adapter->wakeup_timer);
  251. if (adapter->ps_state == PS_STATE_SLEEP)
  252. adapter->ps_state = PS_STATE_AWAKE;
  253. } else {
  254. /* We have tried to wakeup the card already */
  255. if (adapter->pm_wakeup_fw_try)
  256. break;
  257. if (adapter->ps_state != PS_STATE_AWAKE ||
  258. adapter->tx_lock_flag)
  259. break;
  260. if ((!adapter->scan_chan_gap_enabled &&
  261. adapter->scan_processing) || adapter->data_sent ||
  262. mwifiex_is_tdls_chan_switching
  263. (mwifiex_get_priv(adapter,
  264. MWIFIEX_BSS_ROLE_STA)) ||
  265. (mwifiex_wmm_lists_empty(adapter) &&
  266. mwifiex_bypass_txlist_empty(adapter) &&
  267. skb_queue_empty(&adapter->tx_data_q))) {
  268. if (adapter->cmd_sent || adapter->curr_cmd ||
  269. !mwifiex_is_send_cmd_allowed
  270. (mwifiex_get_priv(adapter,
  271. MWIFIEX_BSS_ROLE_STA)) ||
  272. (!is_command_pending(adapter)))
  273. break;
  274. }
  275. }
  276. /* Check for event */
  277. if (adapter->event_received) {
  278. adapter->event_received = false;
  279. mwifiex_process_event(adapter);
  280. }
  281. /* Check for Cmd Resp */
  282. if (adapter->cmd_resp_received) {
  283. adapter->cmd_resp_received = false;
  284. mwifiex_process_cmdresp(adapter);
  285. /* call mwifiex back when init_fw is done */
  286. if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
  287. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  288. mwifiex_init_fw_complete(adapter);
  289. }
  290. }
  291. /* Check if we need to confirm Sleep Request
  292. received previously */
  293. if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
  294. if (!adapter->cmd_sent && !adapter->curr_cmd)
  295. mwifiex_check_ps_cond(adapter);
  296. }
  297. /* * The ps_state may have been changed during processing of
  298. * Sleep Request event.
  299. */
  300. if ((adapter->ps_state == PS_STATE_SLEEP) ||
  301. (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
  302. (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
  303. adapter->tx_lock_flag){
  304. continue;
  305. }
  306. if (!adapter->cmd_sent && !adapter->curr_cmd &&
  307. mwifiex_is_send_cmd_allowed
  308. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
  309. if (mwifiex_exec_next_cmd(adapter) == -1) {
  310. ret = -1;
  311. break;
  312. }
  313. }
  314. if ((adapter->scan_chan_gap_enabled ||
  315. !adapter->scan_processing) &&
  316. !adapter->data_sent &&
  317. !skb_queue_empty(&adapter->tx_data_q)) {
  318. mwifiex_process_tx_queue(adapter);
  319. if (adapter->hs_activated) {
  320. adapter->is_hs_configured = false;
  321. mwifiex_hs_activated_event
  322. (mwifiex_get_priv
  323. (adapter, MWIFIEX_BSS_ROLE_ANY),
  324. false);
  325. }
  326. }
  327. if ((adapter->scan_chan_gap_enabled ||
  328. !adapter->scan_processing) &&
  329. !adapter->data_sent &&
  330. !mwifiex_bypass_txlist_empty(adapter) &&
  331. !mwifiex_is_tdls_chan_switching
  332. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
  333. mwifiex_process_bypass_tx(adapter);
  334. if (adapter->hs_activated) {
  335. adapter->is_hs_configured = false;
  336. mwifiex_hs_activated_event
  337. (mwifiex_get_priv
  338. (adapter, MWIFIEX_BSS_ROLE_ANY),
  339. false);
  340. }
  341. }
  342. if ((adapter->scan_chan_gap_enabled ||
  343. !adapter->scan_processing) &&
  344. !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
  345. !mwifiex_is_tdls_chan_switching
  346. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
  347. mwifiex_wmm_process_tx(adapter);
  348. if (adapter->hs_activated) {
  349. adapter->is_hs_configured = false;
  350. mwifiex_hs_activated_event
  351. (mwifiex_get_priv
  352. (adapter, MWIFIEX_BSS_ROLE_ANY),
  353. false);
  354. }
  355. }
  356. if (adapter->delay_null_pkt && !adapter->cmd_sent &&
  357. !adapter->curr_cmd && !is_command_pending(adapter) &&
  358. (mwifiex_wmm_lists_empty(adapter) &&
  359. mwifiex_bypass_txlist_empty(adapter) &&
  360. skb_queue_empty(&adapter->tx_data_q))) {
  361. if (!mwifiex_send_null_packet
  362. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
  363. MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
  364. MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
  365. adapter->delay_null_pkt = false;
  366. adapter->ps_state = PS_STATE_SLEEP;
  367. }
  368. break;
  369. }
  370. } while (true);
  371. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  372. if (adapter->more_task_flag) {
  373. adapter->more_task_flag = false;
  374. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  375. goto process_start;
  376. }
  377. adapter->mwifiex_processing = false;
  378. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  379. exit_main_proc:
  380. if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
  381. mwifiex_shutdown_drv(adapter);
  382. return ret;
  383. }
  384. EXPORT_SYMBOL_GPL(mwifiex_main_process);
  385. /*
  386. * This function frees the adapter structure.
  387. *
  388. * Additionally, this closes the netlink socket, frees the timers
  389. * and private structures.
  390. */
  391. static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
  392. {
  393. if (!adapter) {
  394. pr_err("%s: adapter is NULL\n", __func__);
  395. return;
  396. }
  397. mwifiex_unregister(adapter);
  398. pr_debug("info: %s: free adapter\n", __func__);
  399. }
  400. /*
  401. * This function cancels all works in the queue and destroys
  402. * the main workqueue.
  403. */
  404. static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
  405. {
  406. flush_workqueue(adapter->workqueue);
  407. destroy_workqueue(adapter->workqueue);
  408. adapter->workqueue = NULL;
  409. if (adapter->rx_workqueue) {
  410. flush_workqueue(adapter->rx_workqueue);
  411. destroy_workqueue(adapter->rx_workqueue);
  412. adapter->rx_workqueue = NULL;
  413. }
  414. }
  415. /*
  416. * This function gets firmware and initializes it.
  417. *
  418. * The main initialization steps followed are -
  419. * - Download the correct firmware to card
  420. * - Issue the init commands to firmware
  421. */
  422. static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
  423. {
  424. int ret;
  425. char fmt[64];
  426. struct mwifiex_private *priv;
  427. struct mwifiex_adapter *adapter = context;
  428. struct mwifiex_fw_image fw;
  429. struct semaphore *sem = adapter->card_sem;
  430. bool init_failed = false;
  431. struct wireless_dev *wdev;
  432. if (!firmware) {
  433. mwifiex_dbg(adapter, ERROR,
  434. "Failed to get firmware %s\n", adapter->fw_name);
  435. goto err_dnld_fw;
  436. }
  437. memset(&fw, 0, sizeof(struct mwifiex_fw_image));
  438. adapter->firmware = firmware;
  439. fw.fw_buf = (u8 *) adapter->firmware->data;
  440. fw.fw_len = adapter->firmware->size;
  441. if (adapter->if_ops.dnld_fw)
  442. ret = adapter->if_ops.dnld_fw(adapter, &fw);
  443. else
  444. ret = mwifiex_dnld_fw(adapter, &fw);
  445. if (ret == -1)
  446. goto err_dnld_fw;
  447. mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
  448. if (cal_data_cfg) {
  449. if ((request_firmware(&adapter->cal_data, cal_data_cfg,
  450. adapter->dev)) < 0)
  451. mwifiex_dbg(adapter, ERROR,
  452. "Cal data request_firmware() failed\n");
  453. }
  454. /* enable host interrupt after fw dnld is successful */
  455. if (adapter->if_ops.enable_int) {
  456. if (adapter->if_ops.enable_int(adapter))
  457. goto err_dnld_fw;
  458. }
  459. adapter->init_wait_q_woken = false;
  460. ret = mwifiex_init_fw(adapter);
  461. if (ret == -1) {
  462. goto err_init_fw;
  463. } else if (!ret) {
  464. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  465. goto done;
  466. }
  467. /* Wait for mwifiex_init to complete */
  468. wait_event_interruptible(adapter->init_wait_q,
  469. adapter->init_wait_q_woken);
  470. if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
  471. goto err_init_fw;
  472. priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
  473. if (mwifiex_register_cfg80211(adapter)) {
  474. mwifiex_dbg(adapter, ERROR,
  475. "cannot register with cfg80211\n");
  476. goto err_init_fw;
  477. }
  478. if (mwifiex_init_channel_scan_gap(adapter)) {
  479. mwifiex_dbg(adapter, ERROR,
  480. "could not init channel stats table\n");
  481. goto err_init_fw;
  482. }
  483. if (driver_mode) {
  484. driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
  485. driver_mode |= MWIFIEX_DRIVER_MODE_STA;
  486. }
  487. rtnl_lock();
  488. /* Create station interface by default */
  489. wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
  490. NL80211_IFTYPE_STATION, NULL, NULL);
  491. if (IS_ERR(wdev)) {
  492. mwifiex_dbg(adapter, ERROR,
  493. "cannot create default STA interface\n");
  494. rtnl_unlock();
  495. goto err_add_intf;
  496. }
  497. if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
  498. wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
  499. NL80211_IFTYPE_AP, NULL, NULL);
  500. if (IS_ERR(wdev)) {
  501. mwifiex_dbg(adapter, ERROR,
  502. "cannot create AP interface\n");
  503. rtnl_unlock();
  504. goto err_add_intf;
  505. }
  506. }
  507. if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
  508. wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
  509. NL80211_IFTYPE_P2P_CLIENT, NULL,
  510. NULL);
  511. if (IS_ERR(wdev)) {
  512. mwifiex_dbg(adapter, ERROR,
  513. "cannot create p2p client interface\n");
  514. rtnl_unlock();
  515. goto err_add_intf;
  516. }
  517. }
  518. rtnl_unlock();
  519. mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
  520. mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
  521. goto done;
  522. err_add_intf:
  523. wiphy_unregister(adapter->wiphy);
  524. wiphy_free(adapter->wiphy);
  525. err_init_fw:
  526. if (adapter->if_ops.disable_int)
  527. adapter->if_ops.disable_int(adapter);
  528. err_dnld_fw:
  529. mwifiex_dbg(adapter, ERROR,
  530. "info: %s: unregister device\n", __func__);
  531. if (adapter->if_ops.unregister_dev)
  532. adapter->if_ops.unregister_dev(adapter);
  533. if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
  534. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  535. adapter->init_wait_q_woken = false;
  536. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  537. wait_event_interruptible(adapter->init_wait_q,
  538. adapter->init_wait_q_woken);
  539. }
  540. adapter->surprise_removed = true;
  541. mwifiex_terminate_workqueue(adapter);
  542. init_failed = true;
  543. done:
  544. if (adapter->cal_data) {
  545. release_firmware(adapter->cal_data);
  546. adapter->cal_data = NULL;
  547. }
  548. if (adapter->firmware) {
  549. release_firmware(adapter->firmware);
  550. adapter->firmware = NULL;
  551. }
  552. if (init_failed)
  553. mwifiex_free_adapter(adapter);
  554. up(sem);
  555. return;
  556. }
  557. /*
  558. * This function initializes the hardware and gets firmware.
  559. */
  560. static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
  561. {
  562. int ret;
  563. ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
  564. adapter->dev, GFP_KERNEL, adapter,
  565. mwifiex_fw_dpc);
  566. if (ret < 0)
  567. mwifiex_dbg(adapter, ERROR,
  568. "request_firmware_nowait error %d\n", ret);
  569. return ret;
  570. }
  571. /*
  572. * CFG802.11 network device handler for open.
  573. *
  574. * Starts the data queue.
  575. */
  576. static int
  577. mwifiex_open(struct net_device *dev)
  578. {
  579. netif_carrier_off(dev);
  580. return 0;
  581. }
  582. /*
  583. * CFG802.11 network device handler for close.
  584. */
  585. static int
  586. mwifiex_close(struct net_device *dev)
  587. {
  588. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  589. if (priv->scan_request) {
  590. mwifiex_dbg(priv->adapter, INFO,
  591. "aborting scan on ndo_stop\n");
  592. cfg80211_scan_done(priv->scan_request, 1);
  593. priv->scan_request = NULL;
  594. priv->scan_aborting = true;
  595. }
  596. return 0;
  597. }
  598. static bool
  599. mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
  600. struct sk_buff *skb)
  601. {
  602. struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
  603. if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
  604. mwifiex_is_skb_mgmt_frame(skb) ||
  605. (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
  606. ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
  607. (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
  608. mwifiex_dbg(priv->adapter, DATA,
  609. "bypass txqueue; eth type %#x, mgmt %d\n",
  610. ntohs(eth_hdr->h_proto),
  611. mwifiex_is_skb_mgmt_frame(skb));
  612. return true;
  613. }
  614. return false;
  615. }
  616. /*
  617. * Add buffer into wmm tx queue and queue work to transmit it.
  618. */
  619. int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
  620. {
  621. struct netdev_queue *txq;
  622. int index = mwifiex_1d_to_wmm_queue[skb->priority];
  623. if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
  624. txq = netdev_get_tx_queue(priv->netdev, index);
  625. if (!netif_tx_queue_stopped(txq)) {
  626. netif_tx_stop_queue(txq);
  627. mwifiex_dbg(priv->adapter, DATA,
  628. "stop queue: %d\n", index);
  629. }
  630. }
  631. if (mwifiex_bypass_tx_queue(priv, skb)) {
  632. atomic_inc(&priv->adapter->tx_pending);
  633. atomic_inc(&priv->adapter->bypass_tx_pending);
  634. mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
  635. } else {
  636. atomic_inc(&priv->adapter->tx_pending);
  637. mwifiex_wmm_add_buf_txqueue(priv, skb);
  638. }
  639. mwifiex_queue_main_work(priv->adapter);
  640. return 0;
  641. }
  642. struct sk_buff *
  643. mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
  644. struct sk_buff *skb, u8 flag, u64 *cookie)
  645. {
  646. struct sk_buff *orig_skb = skb;
  647. struct mwifiex_txinfo *tx_info, *orig_tx_info;
  648. skb = skb_clone(skb, GFP_ATOMIC);
  649. if (skb) {
  650. unsigned long flags;
  651. int id;
  652. spin_lock_irqsave(&priv->ack_status_lock, flags);
  653. id = idr_alloc(&priv->ack_status_frames, orig_skb,
  654. 1, 0xff, GFP_ATOMIC);
  655. spin_unlock_irqrestore(&priv->ack_status_lock, flags);
  656. if (id >= 0) {
  657. tx_info = MWIFIEX_SKB_TXCB(skb);
  658. tx_info->ack_frame_id = id;
  659. tx_info->flags |= flag;
  660. orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
  661. orig_tx_info->ack_frame_id = id;
  662. orig_tx_info->flags |= flag;
  663. if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
  664. orig_tx_info->cookie = *cookie;
  665. } else if (skb_shared(skb)) {
  666. kfree_skb(orig_skb);
  667. } else {
  668. kfree_skb(skb);
  669. skb = orig_skb;
  670. }
  671. } else {
  672. /* couldn't clone -- lose tx status ... */
  673. skb = orig_skb;
  674. }
  675. return skb;
  676. }
  677. /*
  678. * CFG802.11 network device handler for data transmission.
  679. */
  680. static int
  681. mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  682. {
  683. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  684. struct sk_buff *new_skb;
  685. struct mwifiex_txinfo *tx_info;
  686. bool multicast;
  687. mwifiex_dbg(priv->adapter, DATA,
  688. "data: %lu BSS(%d-%d): Data <= kernel\n",
  689. jiffies, priv->bss_type, priv->bss_num);
  690. if (priv->adapter->surprise_removed) {
  691. kfree_skb(skb);
  692. priv->stats.tx_dropped++;
  693. return 0;
  694. }
  695. if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
  696. mwifiex_dbg(priv->adapter, ERROR,
  697. "Tx: bad skb len %d\n", skb->len);
  698. kfree_skb(skb);
  699. priv->stats.tx_dropped++;
  700. return 0;
  701. }
  702. if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
  703. mwifiex_dbg(priv->adapter, DATA,
  704. "data: Tx: insufficient skb headroom %d\n",
  705. skb_headroom(skb));
  706. /* Insufficient skb headroom - allocate a new skb */
  707. new_skb =
  708. skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
  709. if (unlikely(!new_skb)) {
  710. mwifiex_dbg(priv->adapter, ERROR,
  711. "Tx: cannot alloca new_skb\n");
  712. kfree_skb(skb);
  713. priv->stats.tx_dropped++;
  714. return 0;
  715. }
  716. kfree_skb(skb);
  717. skb = new_skb;
  718. mwifiex_dbg(priv->adapter, INFO,
  719. "info: new skb headroomd %d\n",
  720. skb_headroom(skb));
  721. }
  722. tx_info = MWIFIEX_SKB_TXCB(skb);
  723. memset(tx_info, 0, sizeof(*tx_info));
  724. tx_info->bss_num = priv->bss_num;
  725. tx_info->bss_type = priv->bss_type;
  726. tx_info->pkt_len = skb->len;
  727. multicast = is_multicast_ether_addr(skb->data);
  728. if (unlikely(!multicast && skb->sk &&
  729. skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
  730. priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
  731. skb = mwifiex_clone_skb_for_tx_status(priv,
  732. skb,
  733. MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
  734. /* Record the current time the packet was queued; used to
  735. * determine the amount of time the packet was queued in
  736. * the driver before it was sent to the firmware.
  737. * The delay is then sent along with the packet to the
  738. * firmware for aggregate delay calculation for stats and
  739. * MSDU lifetime expiry.
  740. */
  741. __net_timestamp(skb);
  742. if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
  743. priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
  744. !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
  745. if (priv->adapter->auto_tdls && priv->check_tdls_tx)
  746. mwifiex_tdls_check_tx(priv, skb);
  747. }
  748. mwifiex_queue_tx_pkt(priv, skb);
  749. return 0;
  750. }
  751. /*
  752. * CFG802.11 network device handler for setting MAC address.
  753. */
  754. static int
  755. mwifiex_set_mac_address(struct net_device *dev, void *addr)
  756. {
  757. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  758. struct sockaddr *hw_addr = addr;
  759. int ret;
  760. memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
  761. /* Send request to firmware */
  762. ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
  763. HostCmd_ACT_GEN_SET, 0, NULL, true);
  764. if (!ret)
  765. memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
  766. else
  767. mwifiex_dbg(priv->adapter, ERROR,
  768. "set mac address failed: ret=%d\n", ret);
  769. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  770. return ret;
  771. }
  772. /*
  773. * CFG802.11 network device handler for setting multicast list.
  774. */
  775. static void mwifiex_set_multicast_list(struct net_device *dev)
  776. {
  777. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  778. struct mwifiex_multicast_list mcast_list;
  779. if (dev->flags & IFF_PROMISC) {
  780. mcast_list.mode = MWIFIEX_PROMISC_MODE;
  781. } else if (dev->flags & IFF_ALLMULTI ||
  782. netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
  783. mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
  784. } else {
  785. mcast_list.mode = MWIFIEX_MULTICAST_MODE;
  786. mcast_list.num_multicast_addr =
  787. mwifiex_copy_mcast_addr(&mcast_list, dev);
  788. }
  789. mwifiex_request_set_multicast_list(priv, &mcast_list);
  790. }
  791. /*
  792. * CFG802.11 network device handler for transmission timeout.
  793. */
  794. static void
  795. mwifiex_tx_timeout(struct net_device *dev)
  796. {
  797. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  798. priv->num_tx_timeout++;
  799. priv->tx_timeout_cnt++;
  800. mwifiex_dbg(priv->adapter, ERROR,
  801. "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
  802. jiffies, priv->tx_timeout_cnt, priv->bss_type,
  803. priv->bss_num);
  804. mwifiex_set_trans_start(dev);
  805. if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
  806. priv->adapter->if_ops.card_reset) {
  807. mwifiex_dbg(priv->adapter, ERROR,
  808. "tx_timeout_cnt exceeds threshold.\t"
  809. "Triggering card reset!\n");
  810. priv->adapter->if_ops.card_reset(priv->adapter);
  811. }
  812. }
  813. void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
  814. {
  815. void *p;
  816. char drv_version[64];
  817. struct usb_card_rec *cardp;
  818. struct sdio_mmc_card *sdio_card;
  819. struct mwifiex_private *priv;
  820. int i, idx;
  821. struct netdev_queue *txq;
  822. struct mwifiex_debug_info *debug_info;
  823. if (adapter->drv_info_dump) {
  824. vfree(adapter->drv_info_dump);
  825. adapter->drv_info_dump = NULL;
  826. adapter->drv_info_size = 0;
  827. }
  828. mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
  829. adapter->drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
  830. if (!adapter->drv_info_dump)
  831. return;
  832. p = (char *)(adapter->drv_info_dump);
  833. p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
  834. mwifiex_drv_get_driver_version(adapter, drv_version,
  835. sizeof(drv_version) - 1);
  836. p += sprintf(p, "driver_version = %s\n", drv_version);
  837. if (adapter->iface_type == MWIFIEX_USB) {
  838. cardp = (struct usb_card_rec *)adapter->card;
  839. p += sprintf(p, "tx_cmd_urb_pending = %d\n",
  840. atomic_read(&cardp->tx_cmd_urb_pending));
  841. p += sprintf(p, "tx_data_urb_pending = %d\n",
  842. atomic_read(&cardp->tx_data_urb_pending));
  843. p += sprintf(p, "rx_cmd_urb_pending = %d\n",
  844. atomic_read(&cardp->rx_cmd_urb_pending));
  845. p += sprintf(p, "rx_data_urb_pending = %d\n",
  846. atomic_read(&cardp->rx_data_urb_pending));
  847. }
  848. p += sprintf(p, "tx_pending = %d\n",
  849. atomic_read(&adapter->tx_pending));
  850. p += sprintf(p, "rx_pending = %d\n",
  851. atomic_read(&adapter->rx_pending));
  852. if (adapter->iface_type == MWIFIEX_SDIO) {
  853. sdio_card = (struct sdio_mmc_card *)adapter->card;
  854. p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
  855. sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
  856. p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
  857. sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
  858. }
  859. for (i = 0; i < adapter->priv_num; i++) {
  860. if (!adapter->priv[i] || !adapter->priv[i]->netdev)
  861. continue;
  862. priv = adapter->priv[i];
  863. p += sprintf(p, "\n[interface : \"%s\"]\n",
  864. priv->netdev->name);
  865. p += sprintf(p, "wmm_tx_pending[0] = %d\n",
  866. atomic_read(&priv->wmm_tx_pending[0]));
  867. p += sprintf(p, "wmm_tx_pending[1] = %d\n",
  868. atomic_read(&priv->wmm_tx_pending[1]));
  869. p += sprintf(p, "wmm_tx_pending[2] = %d\n",
  870. atomic_read(&priv->wmm_tx_pending[2]));
  871. p += sprintf(p, "wmm_tx_pending[3] = %d\n",
  872. atomic_read(&priv->wmm_tx_pending[3]));
  873. p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
  874. "Disconnected" : "Connected");
  875. p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
  876. ? "on" : "off"));
  877. for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
  878. txq = netdev_get_tx_queue(priv->netdev, idx);
  879. p += sprintf(p, "tx queue %d:%s ", idx,
  880. netif_tx_queue_stopped(txq) ?
  881. "stopped" : "started");
  882. }
  883. p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
  884. priv->netdev->name, priv->num_tx_timeout);
  885. }
  886. if (adapter->iface_type == MWIFIEX_SDIO) {
  887. p += sprintf(p, "\n=== SDIO register dump===\n");
  888. if (adapter->if_ops.reg_dump)
  889. p += adapter->if_ops.reg_dump(adapter, p);
  890. }
  891. p += sprintf(p, "\n=== more debug information\n");
  892. debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
  893. if (debug_info) {
  894. for (i = 0; i < adapter->priv_num; i++) {
  895. if (!adapter->priv[i] || !adapter->priv[i]->netdev)
  896. continue;
  897. priv = adapter->priv[i];
  898. mwifiex_get_debug_info(priv, debug_info);
  899. p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
  900. break;
  901. }
  902. kfree(debug_info);
  903. }
  904. adapter->drv_info_size = p - adapter->drv_info_dump;
  905. mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
  906. }
  907. EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
  908. void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
  909. {
  910. u8 idx, *dump_data, *fw_dump_ptr;
  911. u32 dump_len;
  912. dump_len = (strlen("========Start dump driverinfo========\n") +
  913. adapter->drv_info_size +
  914. strlen("\n========End dump========\n"));
  915. for (idx = 0; idx < adapter->num_mem_types; idx++) {
  916. struct memory_type_mapping *entry =
  917. &adapter->mem_type_mapping_tbl[idx];
  918. if (entry->mem_ptr) {
  919. dump_len += (strlen("========Start dump ") +
  920. strlen(entry->mem_name) +
  921. strlen("========\n") +
  922. (entry->mem_size + 1) +
  923. strlen("\n========End dump========\n"));
  924. }
  925. }
  926. dump_data = vzalloc(dump_len + 1);
  927. if (!dump_data)
  928. goto done;
  929. fw_dump_ptr = dump_data;
  930. /* Dump all the memory data into single file, a userspace script will
  931. * be used to split all the memory data to multiple files
  932. */
  933. mwifiex_dbg(adapter, MSG,
  934. "== mwifiex dump information to /sys/class/devcoredump start");
  935. strcpy(fw_dump_ptr, "========Start dump driverinfo========\n");
  936. fw_dump_ptr += strlen("========Start dump driverinfo========\n");
  937. memcpy(fw_dump_ptr, adapter->drv_info_dump, adapter->drv_info_size);
  938. fw_dump_ptr += adapter->drv_info_size;
  939. strcpy(fw_dump_ptr, "\n========End dump========\n");
  940. fw_dump_ptr += strlen("\n========End dump========\n");
  941. for (idx = 0; idx < adapter->num_mem_types; idx++) {
  942. struct memory_type_mapping *entry =
  943. &adapter->mem_type_mapping_tbl[idx];
  944. if (entry->mem_ptr) {
  945. strcpy(fw_dump_ptr, "========Start dump ");
  946. fw_dump_ptr += strlen("========Start dump ");
  947. strcpy(fw_dump_ptr, entry->mem_name);
  948. fw_dump_ptr += strlen(entry->mem_name);
  949. strcpy(fw_dump_ptr, "========\n");
  950. fw_dump_ptr += strlen("========\n");
  951. memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
  952. fw_dump_ptr += entry->mem_size;
  953. strcpy(fw_dump_ptr, "\n========End dump========\n");
  954. fw_dump_ptr += strlen("\n========End dump========\n");
  955. }
  956. }
  957. /* device dump data will be free in device coredump release function
  958. * after 5 min
  959. */
  960. dev_coredumpv(adapter->dev, dump_data, dump_len, GFP_KERNEL);
  961. mwifiex_dbg(adapter, MSG,
  962. "== mwifiex dump information to /sys/class/devcoredump end");
  963. done:
  964. for (idx = 0; idx < adapter->num_mem_types; idx++) {
  965. struct memory_type_mapping *entry =
  966. &adapter->mem_type_mapping_tbl[idx];
  967. if (entry->mem_ptr) {
  968. vfree(entry->mem_ptr);
  969. entry->mem_ptr = NULL;
  970. }
  971. entry->mem_size = 0;
  972. }
  973. if (adapter->drv_info_dump) {
  974. vfree(adapter->drv_info_dump);
  975. adapter->drv_info_dump = NULL;
  976. adapter->drv_info_size = 0;
  977. }
  978. }
  979. EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
  980. /*
  981. * CFG802.11 network device handler for statistics retrieval.
  982. */
  983. static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
  984. {
  985. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  986. return &priv->stats;
  987. }
  988. static u16
  989. mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
  990. void *accel_priv, select_queue_fallback_t fallback)
  991. {
  992. skb->priority = cfg80211_classify8021d(skb, NULL);
  993. return mwifiex_1d_to_wmm_queue[skb->priority];
  994. }
  995. /* Network device handlers */
  996. static const struct net_device_ops mwifiex_netdev_ops = {
  997. .ndo_open = mwifiex_open,
  998. .ndo_stop = mwifiex_close,
  999. .ndo_start_xmit = mwifiex_hard_start_xmit,
  1000. .ndo_set_mac_address = mwifiex_set_mac_address,
  1001. .ndo_tx_timeout = mwifiex_tx_timeout,
  1002. .ndo_get_stats = mwifiex_get_stats,
  1003. .ndo_set_rx_mode = mwifiex_set_multicast_list,
  1004. .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
  1005. };
  1006. /*
  1007. * This function initializes the private structure parameters.
  1008. *
  1009. * The following wait queues are initialized -
  1010. * - IOCTL wait queue
  1011. * - Command wait queue
  1012. * - Statistics wait queue
  1013. *
  1014. * ...and the following default parameters are set -
  1015. * - Current key index : Set to 0
  1016. * - Rate index : Set to auto
  1017. * - Media connected : Set to disconnected
  1018. * - Adhoc link sensed : Set to false
  1019. * - Nick name : Set to null
  1020. * - Number of Tx timeout : Set to 0
  1021. * - Device address : Set to current address
  1022. * - Rx histogram statistc : Set to 0
  1023. *
  1024. * In addition, the CFG80211 work queue is also created.
  1025. */
  1026. void mwifiex_init_priv_params(struct mwifiex_private *priv,
  1027. struct net_device *dev)
  1028. {
  1029. dev->netdev_ops = &mwifiex_netdev_ops;
  1030. dev->destructor = free_netdev;
  1031. /* Initialize private structure */
  1032. priv->current_key_index = 0;
  1033. priv->media_connected = false;
  1034. memset(priv->mgmt_ie, 0,
  1035. sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
  1036. priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
  1037. priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
  1038. priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
  1039. priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
  1040. priv->num_tx_timeout = 0;
  1041. ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
  1042. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  1043. if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
  1044. GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
  1045. priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
  1046. if (priv->hist_data)
  1047. mwifiex_hist_data_reset(priv);
  1048. }
  1049. }
  1050. /*
  1051. * This function check if command is pending.
  1052. */
  1053. int is_command_pending(struct mwifiex_adapter *adapter)
  1054. {
  1055. unsigned long flags;
  1056. int is_cmd_pend_q_empty;
  1057. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  1058. is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
  1059. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  1060. return !is_cmd_pend_q_empty;
  1061. }
  1062. /*
  1063. * This is the RX work queue function.
  1064. *
  1065. * It handles the RX operations.
  1066. */
  1067. static void mwifiex_rx_work_queue(struct work_struct *work)
  1068. {
  1069. struct mwifiex_adapter *adapter =
  1070. container_of(work, struct mwifiex_adapter, rx_work);
  1071. if (adapter->surprise_removed)
  1072. return;
  1073. mwifiex_process_rx(adapter);
  1074. }
  1075. /*
  1076. * This is the main work queue function.
  1077. *
  1078. * It handles the main process, which in turn handles the complete
  1079. * driver operations.
  1080. */
  1081. static void mwifiex_main_work_queue(struct work_struct *work)
  1082. {
  1083. struct mwifiex_adapter *adapter =
  1084. container_of(work, struct mwifiex_adapter, main_work);
  1085. if (adapter->surprise_removed)
  1086. return;
  1087. mwifiex_main_process(adapter);
  1088. }
  1089. /*
  1090. * This function adds the card.
  1091. *
  1092. * This function follows the following major steps to set up the device -
  1093. * - Initialize software. This includes probing the card, registering
  1094. * the interface operations table, and allocating/initializing the
  1095. * adapter structure
  1096. * - Set up the netlink socket
  1097. * - Create and start the main work queue
  1098. * - Register the device
  1099. * - Initialize firmware and hardware
  1100. * - Add logical interfaces
  1101. */
  1102. int
  1103. mwifiex_add_card(void *card, struct semaphore *sem,
  1104. struct mwifiex_if_ops *if_ops, u8 iface_type)
  1105. {
  1106. struct mwifiex_adapter *adapter;
  1107. if (down_interruptible(sem))
  1108. goto exit_sem_err;
  1109. if (mwifiex_register(card, if_ops, (void **)&adapter)) {
  1110. pr_err("%s: software init failed\n", __func__);
  1111. goto err_init_sw;
  1112. }
  1113. adapter->iface_type = iface_type;
  1114. adapter->card_sem = sem;
  1115. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  1116. adapter->surprise_removed = false;
  1117. init_waitqueue_head(&adapter->init_wait_q);
  1118. adapter->is_suspended = false;
  1119. adapter->hs_activated = false;
  1120. init_waitqueue_head(&adapter->hs_activate_wait_q);
  1121. init_waitqueue_head(&adapter->cmd_wait_q.wait);
  1122. adapter->cmd_wait_q.status = 0;
  1123. adapter->scan_wait_q_woken = false;
  1124. if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
  1125. adapter->rx_work_enabled = true;
  1126. pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
  1127. }
  1128. adapter->workqueue =
  1129. alloc_workqueue("MWIFIEX_WORK_QUEUE",
  1130. WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  1131. if (!adapter->workqueue)
  1132. goto err_kmalloc;
  1133. INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
  1134. if (adapter->rx_work_enabled) {
  1135. adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
  1136. WQ_HIGHPRI |
  1137. WQ_MEM_RECLAIM |
  1138. WQ_UNBOUND, 1);
  1139. if (!adapter->rx_workqueue)
  1140. goto err_kmalloc;
  1141. INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
  1142. }
  1143. /* Register the device. Fill up the private data structure with relevant
  1144. information from the card. */
  1145. if (adapter->if_ops.register_dev(adapter)) {
  1146. pr_err("%s: failed to register mwifiex device\n", __func__);
  1147. goto err_registerdev;
  1148. }
  1149. if (mwifiex_init_hw_fw(adapter)) {
  1150. pr_err("%s: firmware init failed\n", __func__);
  1151. goto err_init_fw;
  1152. }
  1153. return 0;
  1154. err_init_fw:
  1155. pr_debug("info: %s: unregister device\n", __func__);
  1156. if (adapter->if_ops.unregister_dev)
  1157. adapter->if_ops.unregister_dev(adapter);
  1158. if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
  1159. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  1160. adapter->init_wait_q_woken = false;
  1161. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  1162. wait_event_interruptible(adapter->init_wait_q,
  1163. adapter->init_wait_q_woken);
  1164. }
  1165. err_registerdev:
  1166. adapter->surprise_removed = true;
  1167. mwifiex_terminate_workqueue(adapter);
  1168. err_kmalloc:
  1169. mwifiex_free_adapter(adapter);
  1170. err_init_sw:
  1171. up(sem);
  1172. exit_sem_err:
  1173. return -1;
  1174. }
  1175. EXPORT_SYMBOL_GPL(mwifiex_add_card);
  1176. /*
  1177. * This function removes the card.
  1178. *
  1179. * This function follows the following major steps to remove the device -
  1180. * - Stop data traffic
  1181. * - Shutdown firmware
  1182. * - Remove the logical interfaces
  1183. * - Terminate the work queue
  1184. * - Unregister the device
  1185. * - Free the adapter structure
  1186. */
  1187. int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
  1188. {
  1189. struct mwifiex_private *priv = NULL;
  1190. int i;
  1191. if (down_interruptible(sem))
  1192. goto exit_sem_err;
  1193. if (!adapter)
  1194. goto exit_remove;
  1195. /* We can no longer handle interrupts once we start doing the teardown
  1196. * below. */
  1197. if (adapter->if_ops.disable_int)
  1198. adapter->if_ops.disable_int(adapter);
  1199. adapter->surprise_removed = true;
  1200. mwifiex_terminate_workqueue(adapter);
  1201. /* Stop data */
  1202. for (i = 0; i < adapter->priv_num; i++) {
  1203. priv = adapter->priv[i];
  1204. if (priv && priv->netdev) {
  1205. mwifiex_stop_net_dev_queue(priv->netdev, adapter);
  1206. if (netif_carrier_ok(priv->netdev))
  1207. netif_carrier_off(priv->netdev);
  1208. }
  1209. }
  1210. mwifiex_dbg(adapter, CMD,
  1211. "cmd: calling mwifiex_shutdown_drv...\n");
  1212. adapter->init_wait_q_woken = false;
  1213. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  1214. wait_event_interruptible(adapter->init_wait_q,
  1215. adapter->init_wait_q_woken);
  1216. mwifiex_dbg(adapter, CMD,
  1217. "cmd: mwifiex_shutdown_drv done\n");
  1218. if (atomic_read(&adapter->rx_pending) ||
  1219. atomic_read(&adapter->tx_pending) ||
  1220. atomic_read(&adapter->cmd_pending)) {
  1221. mwifiex_dbg(adapter, ERROR,
  1222. "rx_pending=%d, tx_pending=%d,\t"
  1223. "cmd_pending=%d\n",
  1224. atomic_read(&adapter->rx_pending),
  1225. atomic_read(&adapter->tx_pending),
  1226. atomic_read(&adapter->cmd_pending));
  1227. }
  1228. for (i = 0; i < adapter->priv_num; i++) {
  1229. priv = adapter->priv[i];
  1230. if (!priv)
  1231. continue;
  1232. rtnl_lock();
  1233. if (priv->netdev &&
  1234. priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
  1235. mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
  1236. rtnl_unlock();
  1237. }
  1238. wiphy_unregister(adapter->wiphy);
  1239. wiphy_free(adapter->wiphy);
  1240. /* Unregister device */
  1241. mwifiex_dbg(adapter, INFO,
  1242. "info: unregister device\n");
  1243. if (adapter->if_ops.unregister_dev)
  1244. adapter->if_ops.unregister_dev(adapter);
  1245. /* Free adapter structure */
  1246. mwifiex_dbg(adapter, INFO,
  1247. "info: free adapter\n");
  1248. mwifiex_free_adapter(adapter);
  1249. exit_remove:
  1250. up(sem);
  1251. exit_sem_err:
  1252. return 0;
  1253. }
  1254. EXPORT_SYMBOL_GPL(mwifiex_remove_card);
  1255. /*
  1256. * This function initializes the module.
  1257. *
  1258. * The debug FS is also initialized if configured.
  1259. */
  1260. static int
  1261. mwifiex_init_module(void)
  1262. {
  1263. #ifdef CONFIG_DEBUG_FS
  1264. mwifiex_debugfs_init();
  1265. #endif
  1266. return 0;
  1267. }
  1268. /*
  1269. * This function cleans up the module.
  1270. *
  1271. * The debug FS is removed if available.
  1272. */
  1273. static void
  1274. mwifiex_cleanup_module(void)
  1275. {
  1276. #ifdef CONFIG_DEBUG_FS
  1277. mwifiex_debugfs_remove();
  1278. #endif
  1279. }
  1280. module_init(mwifiex_init_module);
  1281. module_exit(mwifiex_cleanup_module);
  1282. MODULE_AUTHOR("Marvell International Ltd.");
  1283. MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
  1284. MODULE_VERSION(VERSION);
  1285. MODULE_LICENSE("GPL v2");