main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. struct mwifiex_adapter *g_adapter;
  26. EXPORT_SYMBOL_GPL(g_adapter);
  27. static struct mwifiex_bss_attr mwifiex_bss_sta[] = {
  28. {MWIFIEX_BSS_TYPE_STA, MWIFIEX_DATA_FRAME_TYPE_ETH_II, true, 0, 0},
  29. };
  30. static int drv_mode = DRV_MODE_STA;
  31. static char fw_name[32] = DEFAULT_FW_NAME;
  32. /* Supported drv_mode table */
  33. static struct mwifiex_drv_mode mwifiex_drv_mode_tbl[] = {
  34. {
  35. .drv_mode = DRV_MODE_STA,
  36. .intf_num = ARRAY_SIZE(mwifiex_bss_sta),
  37. .bss_attr = mwifiex_bss_sta,
  38. },
  39. };
  40. /*
  41. * This function registers the device and performs all the necessary
  42. * initializations.
  43. *
  44. * The following initialization operations are performed -
  45. * - Allocate adapter structure
  46. * - Save interface specific operations table in adapter
  47. * - Call interface specific initialization routine
  48. * - Allocate private structures
  49. * - Set default adapter structure parameters
  50. * - Initialize locks
  51. *
  52. * In case of any errors during inittialization, this function also ensures
  53. * proper cleanup before exiting.
  54. */
  55. static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
  56. struct mwifiex_drv_mode *drv_mode_ptr)
  57. {
  58. struct mwifiex_adapter *adapter;
  59. int i;
  60. adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
  61. if (!adapter)
  62. return -ENOMEM;
  63. g_adapter = adapter;
  64. adapter->card = card;
  65. /* Save interface specific operations in adapter */
  66. memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
  67. /* card specific initialization has been deferred until now .. */
  68. if (adapter->if_ops.init_if(adapter))
  69. goto error;
  70. adapter->priv_num = 0;
  71. for (i = 0; i < drv_mode_ptr->intf_num; i++) {
  72. adapter->priv[i] = NULL;
  73. if (!drv_mode_ptr->bss_attr[i].active)
  74. continue;
  75. /* Allocate memory for private structure */
  76. adapter->priv[i] = kzalloc(sizeof(struct mwifiex_private),
  77. GFP_KERNEL);
  78. if (!adapter->priv[i]) {
  79. dev_err(adapter->dev, "%s: failed to alloc priv[%d]\n",
  80. __func__, i);
  81. goto error;
  82. }
  83. adapter->priv_num++;
  84. adapter->priv[i]->adapter = adapter;
  85. /* Save bss_type, frame_type & bss_priority */
  86. adapter->priv[i]->bss_type = drv_mode_ptr->bss_attr[i].bss_type;
  87. adapter->priv[i]->frame_type =
  88. drv_mode_ptr->bss_attr[i].frame_type;
  89. adapter->priv[i]->bss_priority =
  90. drv_mode_ptr->bss_attr[i].bss_priority;
  91. if (drv_mode_ptr->bss_attr[i].bss_type == MWIFIEX_BSS_TYPE_STA)
  92. adapter->priv[i]->bss_role = MWIFIEX_BSS_ROLE_STA;
  93. else if (drv_mode_ptr->bss_attr[i].bss_type ==
  94. MWIFIEX_BSS_TYPE_UAP)
  95. adapter->priv[i]->bss_role = MWIFIEX_BSS_ROLE_UAP;
  96. /* Save bss_index & bss_num */
  97. adapter->priv[i]->bss_index = i;
  98. adapter->priv[i]->bss_num = drv_mode_ptr->bss_attr[i].bss_num;
  99. }
  100. adapter->drv_mode = drv_mode_ptr;
  101. if (mwifiex_init_lock_list(adapter))
  102. goto error;
  103. init_timer(&adapter->cmd_timer);
  104. adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
  105. adapter->cmd_timer.data = (unsigned long) adapter;
  106. return 0;
  107. error:
  108. dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
  109. mwifiex_free_lock_list(adapter);
  110. for (i = 0; i < drv_mode_ptr->intf_num; i++)
  111. kfree(adapter->priv[i]);
  112. kfree(adapter);
  113. return -1;
  114. }
  115. /*
  116. * This function unregisters the device and performs all the necessary
  117. * cleanups.
  118. *
  119. * The following cleanup operations are performed -
  120. * - Free the timers
  121. * - Free beacon buffers
  122. * - Free private structures
  123. * - Free adapter structure
  124. */
  125. static int mwifiex_unregister(struct mwifiex_adapter *adapter)
  126. {
  127. s32 i;
  128. del_timer(&adapter->cmd_timer);
  129. /* Free private structures */
  130. for (i = 0; i < adapter->priv_num; i++) {
  131. if (adapter->priv[i]) {
  132. mwifiex_free_curr_bcn(adapter->priv[i]);
  133. kfree(adapter->priv[i]);
  134. }
  135. }
  136. kfree(adapter);
  137. return 0;
  138. }
  139. /*
  140. * The main process.
  141. *
  142. * This function is the main procedure of the driver and handles various driver
  143. * operations. It runs in a loop and provides the core functionalities.
  144. *
  145. * The main responsibilities of this function are -
  146. * - Ensure concurrency control
  147. * - Handle pending interrupts and call interrupt handlers
  148. * - Wake up the card if required
  149. * - Handle command responses and call response handlers
  150. * - Handle events and call event handlers
  151. * - Execute pending commands
  152. * - Transmit pending data packets
  153. */
  154. int mwifiex_main_process(struct mwifiex_adapter *adapter)
  155. {
  156. int ret = 0;
  157. unsigned long flags;
  158. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  159. /* Check if already processing */
  160. if (adapter->mwifiex_processing) {
  161. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  162. goto exit_main_proc;
  163. } else {
  164. adapter->mwifiex_processing = true;
  165. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  166. }
  167. process_start:
  168. do {
  169. if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
  170. (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
  171. break;
  172. /* Handle pending interrupt if any */
  173. if (adapter->int_status) {
  174. if (adapter->hs_activated)
  175. mwifiex_process_hs_config(adapter);
  176. adapter->if_ops.process_int_status(adapter);
  177. }
  178. /* Need to wake up the card ? */
  179. if ((adapter->ps_state == PS_STATE_SLEEP) &&
  180. (adapter->pm_wakeup_card_req &&
  181. !adapter->pm_wakeup_fw_try) &&
  182. (is_command_pending(adapter)
  183. || !mwifiex_wmm_lists_empty(adapter))) {
  184. adapter->pm_wakeup_fw_try = true;
  185. adapter->if_ops.wakeup(adapter);
  186. continue;
  187. }
  188. if (IS_CARD_RX_RCVD(adapter)) {
  189. adapter->pm_wakeup_fw_try = false;
  190. if (adapter->ps_state == PS_STATE_SLEEP)
  191. adapter->ps_state = PS_STATE_AWAKE;
  192. } else {
  193. /* We have tried to wakeup the card already */
  194. if (adapter->pm_wakeup_fw_try)
  195. break;
  196. if (adapter->ps_state != PS_STATE_AWAKE ||
  197. adapter->tx_lock_flag)
  198. break;
  199. if (adapter->scan_processing || adapter->data_sent
  200. || mwifiex_wmm_lists_empty(adapter)) {
  201. if (adapter->cmd_sent || adapter->curr_cmd
  202. || (!is_command_pending(adapter)))
  203. break;
  204. }
  205. }
  206. /* Check for Cmd Resp */
  207. if (adapter->cmd_resp_received) {
  208. adapter->cmd_resp_received = false;
  209. mwifiex_process_cmdresp(adapter);
  210. /* call mwifiex back when init_fw is done */
  211. if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
  212. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  213. mwifiex_init_fw_complete(adapter);
  214. }
  215. }
  216. /* Check for event */
  217. if (adapter->event_received) {
  218. adapter->event_received = false;
  219. mwifiex_process_event(adapter);
  220. }
  221. /* Check if we need to confirm Sleep Request
  222. received previously */
  223. if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
  224. if (!adapter->cmd_sent && !adapter->curr_cmd)
  225. mwifiex_check_ps_cond(adapter);
  226. }
  227. /* * The ps_state may have been changed during processing of
  228. * Sleep Request event.
  229. */
  230. if ((adapter->ps_state == PS_STATE_SLEEP)
  231. || (adapter->ps_state == PS_STATE_PRE_SLEEP)
  232. || (adapter->ps_state == PS_STATE_SLEEP_CFM)
  233. || adapter->tx_lock_flag)
  234. continue;
  235. if (!adapter->cmd_sent && !adapter->curr_cmd) {
  236. if (mwifiex_exec_next_cmd(adapter) == -1) {
  237. ret = -1;
  238. break;
  239. }
  240. }
  241. if (!adapter->scan_processing && !adapter->data_sent &&
  242. !mwifiex_wmm_lists_empty(adapter)) {
  243. mwifiex_wmm_process_tx(adapter);
  244. if (adapter->hs_activated) {
  245. adapter->is_hs_configured = false;
  246. mwifiex_hs_activated_event
  247. (mwifiex_get_priv
  248. (adapter, MWIFIEX_BSS_ROLE_ANY),
  249. false);
  250. }
  251. }
  252. if (adapter->delay_null_pkt && !adapter->cmd_sent &&
  253. !adapter->curr_cmd && !is_command_pending(adapter)
  254. && mwifiex_wmm_lists_empty(adapter)) {
  255. if (!mwifiex_send_null_packet
  256. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
  257. MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
  258. MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
  259. adapter->delay_null_pkt = false;
  260. adapter->ps_state = PS_STATE_SLEEP;
  261. }
  262. break;
  263. }
  264. } while (true);
  265. if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter))
  266. goto process_start;
  267. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  268. adapter->mwifiex_processing = false;
  269. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  270. exit_main_proc:
  271. if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
  272. mwifiex_shutdown_drv(adapter);
  273. return ret;
  274. }
  275. /*
  276. * This function initializes the software.
  277. *
  278. * The main work includes allocating and initializing the adapter structure
  279. * and initializing the private structures.
  280. */
  281. static int
  282. mwifiex_init_sw(void *card, struct mwifiex_if_ops *if_ops)
  283. {
  284. int i;
  285. struct mwifiex_drv_mode *drv_mode_ptr;
  286. /* find mwifiex_drv_mode entry from mwifiex_drv_mode_tbl */
  287. drv_mode_ptr = NULL;
  288. for (i = 0; i < ARRAY_SIZE(mwifiex_drv_mode_tbl); i++) {
  289. if (mwifiex_drv_mode_tbl[i].drv_mode == drv_mode) {
  290. drv_mode_ptr = &mwifiex_drv_mode_tbl[i];
  291. break;
  292. }
  293. }
  294. if (!drv_mode_ptr) {
  295. pr_err("invalid drv_mode=%d\n", drv_mode);
  296. return -1;
  297. }
  298. if (mwifiex_register(card, if_ops, drv_mode_ptr))
  299. return -1;
  300. return 0;
  301. }
  302. /*
  303. * This function frees the adapter structure.
  304. *
  305. * Additionally, this closes the netlink socket, frees the timers
  306. * and private structures.
  307. */
  308. static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
  309. {
  310. if (!adapter) {
  311. pr_err("%s: adapter is NULL\n", __func__);
  312. return;
  313. }
  314. mwifiex_unregister(adapter);
  315. pr_debug("info: %s: free adapter\n", __func__);
  316. }
  317. /*
  318. * This function initializes the hardware and firmware.
  319. *
  320. * The main initialization steps followed are -
  321. * - Download the correct firmware to card
  322. * - Allocate and initialize the adapter structure
  323. * - Initialize the private structures
  324. * - Issue the init commands to firmware
  325. */
  326. static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
  327. {
  328. int ret, err;
  329. struct mwifiex_fw_image fw;
  330. memset(&fw, 0, sizeof(struct mwifiex_fw_image));
  331. switch (adapter->revision_id) {
  332. case SD8787_W0:
  333. case SD8787_W1:
  334. strcpy(fw_name, SD8787_W1_FW_NAME);
  335. break;
  336. case SD8787_A0:
  337. case SD8787_A1:
  338. strcpy(fw_name, SD8787_AX_FW_NAME);
  339. break;
  340. default:
  341. break;
  342. }
  343. err = request_firmware(&adapter->firmware, fw_name, adapter->dev);
  344. if (err < 0) {
  345. dev_err(adapter->dev, "request_firmware() returned"
  346. " error code %#x\n", err);
  347. ret = -1;
  348. goto done;
  349. }
  350. fw.fw_buf = (u8 *) adapter->firmware->data;
  351. fw.fw_len = adapter->firmware->size;
  352. ret = mwifiex_dnld_fw(adapter, &fw);
  353. if (ret == -1)
  354. goto done;
  355. dev_notice(adapter->dev, "WLAN FW is active\n");
  356. adapter->init_wait_q_woken = false;
  357. ret = mwifiex_init_fw(adapter);
  358. if (ret == -1) {
  359. goto done;
  360. } else if (!ret) {
  361. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  362. goto done;
  363. }
  364. /* Wait for mwifiex_init to complete */
  365. wait_event_interruptible(adapter->init_wait_q,
  366. adapter->init_wait_q_woken);
  367. if (adapter->hw_status != MWIFIEX_HW_STATUS_READY) {
  368. ret = -1;
  369. goto done;
  370. }
  371. ret = 0;
  372. done:
  373. if (adapter->firmware)
  374. release_firmware(adapter->firmware);
  375. if (ret)
  376. ret = -1;
  377. return ret;
  378. }
  379. /*
  380. * This function fills a driver buffer.
  381. *
  382. * The function associates a given SKB with the provided driver buffer
  383. * and also updates some of the SKB parameters, including IP header,
  384. * priority and timestamp.
  385. */
  386. static void
  387. mwifiex_fill_buffer(struct sk_buff *skb)
  388. {
  389. struct ethhdr *eth;
  390. struct iphdr *iph;
  391. struct timeval tv;
  392. u8 tid = 0;
  393. eth = (struct ethhdr *) skb->data;
  394. switch (eth->h_proto) {
  395. case __constant_htons(ETH_P_IP):
  396. iph = ip_hdr(skb);
  397. tid = IPTOS_PREC(iph->tos);
  398. pr_debug("data: packet type ETH_P_IP: %04x, tid=%#x prio=%#x\n",
  399. eth->h_proto, tid, skb->priority);
  400. break;
  401. case __constant_htons(ETH_P_ARP):
  402. pr_debug("data: ARP packet: %04x\n", eth->h_proto);
  403. default:
  404. break;
  405. }
  406. /* Offset for TOS field in the IP header */
  407. #define IPTOS_OFFSET 5
  408. tid = (tid >> IPTOS_OFFSET);
  409. skb->priority = tid;
  410. /* Record the current time the packet was queued; used to
  411. determine the amount of time the packet was queued in
  412. the driver before it was sent to the firmware.
  413. The delay is then sent along with the packet to the
  414. firmware for aggregate delay calculation for stats and
  415. MSDU lifetime expiry.
  416. */
  417. do_gettimeofday(&tv);
  418. skb->tstamp = timeval_to_ktime(tv);
  419. }
  420. /*
  421. * CFG802.11 network device handler for open.
  422. *
  423. * Starts the data queue.
  424. */
  425. static int
  426. mwifiex_open(struct net_device *dev)
  427. {
  428. netif_start_queue(dev);
  429. return 0;
  430. }
  431. /*
  432. * CFG802.11 network device handler for close.
  433. */
  434. static int
  435. mwifiex_close(struct net_device *dev)
  436. {
  437. return 0;
  438. }
  439. /*
  440. * CFG802.11 network device handler for data transmission.
  441. */
  442. static int
  443. mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  444. {
  445. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  446. struct sk_buff *new_skb;
  447. struct mwifiex_txinfo *tx_info;
  448. dev_dbg(priv->adapter->dev, "data: %lu BSS(%d): Data <= kernel\n",
  449. jiffies, priv->bss_index);
  450. if (priv->adapter->surprise_removed) {
  451. kfree_skb(skb);
  452. priv->stats.tx_dropped++;
  453. return 0;
  454. }
  455. if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
  456. dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
  457. kfree_skb(skb);
  458. priv->stats.tx_dropped++;
  459. return 0;
  460. }
  461. if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
  462. dev_dbg(priv->adapter->dev,
  463. "data: Tx: insufficient skb headroom %d\n",
  464. skb_headroom(skb));
  465. /* Insufficient skb headroom - allocate a new skb */
  466. new_skb =
  467. skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
  468. if (unlikely(!new_skb)) {
  469. dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
  470. kfree_skb(skb);
  471. priv->stats.tx_dropped++;
  472. return 0;
  473. }
  474. kfree_skb(skb);
  475. skb = new_skb;
  476. dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
  477. skb_headroom(skb));
  478. }
  479. tx_info = MWIFIEX_SKB_TXCB(skb);
  480. tx_info->bss_index = priv->bss_index;
  481. mwifiex_fill_buffer(skb);
  482. mwifiex_wmm_add_buf_txqueue(priv->adapter, skb);
  483. atomic_inc(&priv->adapter->tx_pending);
  484. if (atomic_read(&priv->adapter->tx_pending) >= MAX_TX_PENDING) {
  485. netif_stop_queue(priv->netdev);
  486. dev->trans_start = jiffies;
  487. }
  488. queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
  489. return 0;
  490. }
  491. /*
  492. * CFG802.11 network device handler for setting MAC address.
  493. */
  494. static int
  495. mwifiex_set_mac_address(struct net_device *dev, void *addr)
  496. {
  497. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  498. struct sockaddr *hw_addr = (struct sockaddr *) addr;
  499. int ret;
  500. memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
  501. /* Send request to firmware */
  502. ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
  503. HostCmd_ACT_GEN_SET, 0, NULL);
  504. if (!ret)
  505. memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
  506. else
  507. dev_err(priv->adapter->dev, "set mac address failed: ret=%d"
  508. "\n", ret);
  509. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  510. return ret;
  511. }
  512. /*
  513. * CFG802.11 network device handler for setting multicast list.
  514. */
  515. static void mwifiex_set_multicast_list(struct net_device *dev)
  516. {
  517. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  518. struct mwifiex_multicast_list mcast_list;
  519. if (dev->flags & IFF_PROMISC) {
  520. mcast_list.mode = MWIFIEX_PROMISC_MODE;
  521. } else if (dev->flags & IFF_ALLMULTI ||
  522. netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
  523. mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
  524. } else {
  525. mcast_list.mode = MWIFIEX_MULTICAST_MODE;
  526. if (netdev_mc_count(dev))
  527. mcast_list.num_multicast_addr =
  528. mwifiex_copy_mcast_addr(&mcast_list, dev);
  529. }
  530. mwifiex_request_set_multicast_list(priv, &mcast_list);
  531. }
  532. /*
  533. * CFG802.11 network device handler for transmission timeout.
  534. */
  535. static void
  536. mwifiex_tx_timeout(struct net_device *dev)
  537. {
  538. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  539. dev_err(priv->adapter->dev, "%lu : Tx timeout, bss_index=%d\n",
  540. jiffies, priv->bss_index);
  541. dev->trans_start = jiffies;
  542. priv->num_tx_timeout++;
  543. }
  544. /*
  545. * CFG802.11 network device handler for statistics retrieval.
  546. */
  547. static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
  548. {
  549. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  550. return &priv->stats;
  551. }
  552. /* Network device handlers */
  553. static const struct net_device_ops mwifiex_netdev_ops = {
  554. .ndo_open = mwifiex_open,
  555. .ndo_stop = mwifiex_close,
  556. .ndo_start_xmit = mwifiex_hard_start_xmit,
  557. .ndo_set_mac_address = mwifiex_set_mac_address,
  558. .ndo_tx_timeout = mwifiex_tx_timeout,
  559. .ndo_get_stats = mwifiex_get_stats,
  560. .ndo_set_multicast_list = mwifiex_set_multicast_list,
  561. };
  562. /*
  563. * This function initializes the private structure parameters.
  564. *
  565. * The following wait queues are initialized -
  566. * - IOCTL wait queue
  567. * - Command wait queue
  568. * - Statistics wait queue
  569. *
  570. * ...and the following default parameters are set -
  571. * - Current key index : Set to 0
  572. * - Rate index : Set to auto
  573. * - Media connected : Set to disconnected
  574. * - Adhoc link sensed : Set to false
  575. * - Nick name : Set to null
  576. * - Number of Tx timeout : Set to 0
  577. * - Device address : Set to current address
  578. *
  579. * In addition, the CFG80211 work queue is also created.
  580. */
  581. static void
  582. mwifiex_init_priv_params(struct mwifiex_private *priv, struct net_device *dev)
  583. {
  584. dev->netdev_ops = &mwifiex_netdev_ops;
  585. /* Initialize private structure */
  586. priv->current_key_index = 0;
  587. priv->media_connected = false;
  588. memset(&priv->nick_name, 0, sizeof(priv->nick_name));
  589. priv->num_tx_timeout = 0;
  590. priv->workqueue = create_singlethread_workqueue("cfg80211_wq");
  591. INIT_WORK(&priv->cfg_workqueue, mwifiex_cfg80211_results);
  592. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  593. }
  594. /*
  595. * This function adds a new logical interface.
  596. *
  597. * It allocates, initializes and registers the interface by performing
  598. * the following opearations -
  599. * - Allocate a new net device structure
  600. * - Assign device name
  601. * - Register the new device with CFG80211 subsystem
  602. * - Initialize semaphore and private structure
  603. * - Register the new device with kernel
  604. * - Create the complete debug FS structure if configured
  605. */
  606. static struct mwifiex_private *mwifiex_add_interface(
  607. struct mwifiex_adapter *adapter,
  608. u8 bss_index, u8 bss_type)
  609. {
  610. struct net_device *dev;
  611. struct mwifiex_private *priv;
  612. void *mdev_priv;
  613. dev = alloc_netdev_mq(sizeof(struct mwifiex_private *), "mlan%d",
  614. ether_setup, 1);
  615. if (!dev) {
  616. dev_err(adapter->dev, "no memory available for netdevice\n");
  617. goto error;
  618. }
  619. if (mwifiex_register_cfg80211(dev, adapter->priv[bss_index]->curr_addr,
  620. adapter->priv[bss_index]) != 0) {
  621. dev_err(adapter->dev, "cannot register netdevice with cfg80211\n");
  622. goto error;
  623. }
  624. /* Save the priv pointer in netdev */
  625. priv = adapter->priv[bss_index];
  626. mdev_priv = netdev_priv(dev);
  627. *((unsigned long *) mdev_priv) = (unsigned long) priv;
  628. priv->netdev = dev;
  629. sema_init(&priv->async_sem, 1);
  630. priv->scan_pending_on_block = false;
  631. mwifiex_init_priv_params(priv, dev);
  632. SET_NETDEV_DEV(dev, adapter->dev);
  633. /* Register network device */
  634. if (register_netdev(dev)) {
  635. dev_err(adapter->dev, "cannot register virtual network device\n");
  636. goto error;
  637. }
  638. dev_dbg(adapter->dev, "info: %s: Marvell 802.11 Adapter\n", dev->name);
  639. #ifdef CONFIG_DEBUG_FS
  640. mwifiex_dev_debugfs_init(priv);
  641. #endif
  642. return priv;
  643. error:
  644. if (dev)
  645. free_netdev(dev);
  646. return NULL;
  647. }
  648. /*
  649. * This function removes a logical interface.
  650. *
  651. * It deregisters, resets and frees the interface by performing
  652. * the following operations -
  653. * - Disconnect the device if connected, send wireless event to
  654. * notify applications.
  655. * - Remove the debug FS structure if configured
  656. * - Unregister the device from kernel
  657. * - Free the net device structure
  658. * - Cancel all works and destroy work queue
  659. * - Unregister and free the wireless device from CFG80211 subsystem
  660. */
  661. static void
  662. mwifiex_remove_interface(struct mwifiex_adapter *adapter, u8 bss_index)
  663. {
  664. struct net_device *dev;
  665. struct mwifiex_private *priv = adapter->priv[bss_index];
  666. if (!priv)
  667. return;
  668. dev = priv->netdev;
  669. if (priv->media_connected)
  670. priv->media_connected = false;
  671. #ifdef CONFIG_DEBUG_FS
  672. mwifiex_dev_debugfs_remove(priv);
  673. #endif
  674. /* Last reference is our one */
  675. dev_dbg(adapter->dev, "info: %s: refcnt = %d\n",
  676. dev->name, netdev_refcnt_read(dev));
  677. if (dev->reg_state == NETREG_REGISTERED)
  678. unregister_netdev(dev);
  679. /* Clear the priv in adapter */
  680. priv->netdev = NULL;
  681. if (dev)
  682. free_netdev(dev);
  683. cancel_work_sync(&priv->cfg_workqueue);
  684. flush_workqueue(priv->workqueue);
  685. destroy_workqueue(priv->workqueue);
  686. wiphy_unregister(priv->wdev->wiphy);
  687. wiphy_free(priv->wdev->wiphy);
  688. kfree(priv->wdev);
  689. }
  690. /*
  691. * This function check if command is pending.
  692. */
  693. int is_command_pending(struct mwifiex_adapter *adapter)
  694. {
  695. unsigned long flags;
  696. int is_cmd_pend_q_empty;
  697. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  698. is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
  699. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  700. return !is_cmd_pend_q_empty;
  701. }
  702. /*
  703. * This function returns the correct private structure pointer based
  704. * upon the BSS number.
  705. */
  706. struct mwifiex_private *
  707. mwifiex_bss_index_to_priv(struct mwifiex_adapter *adapter, u8 bss_index)
  708. {
  709. if (!adapter || (bss_index >= adapter->priv_num))
  710. return NULL;
  711. return adapter->priv[bss_index];
  712. }
  713. /*
  714. * This is the main work queue function.
  715. *
  716. * It handles the main process, which in turn handles the complete
  717. * driver operations.
  718. */
  719. static void mwifiex_main_work_queue(struct work_struct *work)
  720. {
  721. struct mwifiex_adapter *adapter =
  722. container_of(work, struct mwifiex_adapter, main_work);
  723. if (adapter->surprise_removed)
  724. return;
  725. mwifiex_main_process(adapter);
  726. }
  727. /*
  728. * This function cancels all works in the queue and destroys
  729. * the main workqueue.
  730. */
  731. static void
  732. mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
  733. {
  734. flush_workqueue(adapter->workqueue);
  735. destroy_workqueue(adapter->workqueue);
  736. adapter->workqueue = NULL;
  737. }
  738. /*
  739. * This function adds the card.
  740. *
  741. * This function follows the following major steps to set up the device -
  742. * - Initialize software. This includes probing the card, registering
  743. * the interface operations table, and allocating/initializing the
  744. * adapter structure
  745. * - Set up the netlink socket
  746. * - Create and start the main work queue
  747. * - Register the device
  748. * - Initialize firmware and hardware
  749. * - Add logical interfaces
  750. */
  751. int
  752. mwifiex_add_card(void *card, struct semaphore *sem,
  753. struct mwifiex_if_ops *if_ops)
  754. {
  755. int i;
  756. struct mwifiex_adapter *adapter;
  757. if (down_interruptible(sem))
  758. goto exit_sem_err;
  759. if (mwifiex_init_sw(card, if_ops)) {
  760. pr_err("%s: software init failed\n", __func__);
  761. goto err_init_sw;
  762. }
  763. adapter = g_adapter;
  764. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  765. adapter->surprise_removed = false;
  766. init_waitqueue_head(&adapter->init_wait_q);
  767. adapter->is_suspended = false;
  768. adapter->hs_activated = false;
  769. init_waitqueue_head(&adapter->hs_activate_wait_q);
  770. adapter->cmd_wait_q_required = false;
  771. init_waitqueue_head(&adapter->cmd_wait_q.wait);
  772. adapter->cmd_wait_q.condition = false;
  773. adapter->cmd_wait_q.status = 0;
  774. adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
  775. if (!adapter->workqueue)
  776. goto err_kmalloc;
  777. INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
  778. /* Register the device. Fill up the private data structure with relevant
  779. information from the card and request for the required IRQ. */
  780. if (adapter->if_ops.register_dev(adapter)) {
  781. pr_err("%s: failed to register mwifiex device\n", __func__);
  782. goto err_registerdev;
  783. }
  784. if (mwifiex_init_hw_fw(adapter)) {
  785. pr_err("%s: firmware init failed\n", __func__);
  786. goto err_init_fw;
  787. }
  788. /* Add interfaces */
  789. for (i = 0; i < adapter->drv_mode->intf_num; i++) {
  790. if (!mwifiex_add_interface(adapter, i,
  791. adapter->drv_mode->bss_attr[i].bss_type)) {
  792. goto err_add_intf;
  793. }
  794. }
  795. up(sem);
  796. return 0;
  797. err_add_intf:
  798. for (i = 0; i < adapter->priv_num; i++)
  799. mwifiex_remove_interface(adapter, i);
  800. err_init_fw:
  801. pr_debug("info: %s: unregister device\n", __func__);
  802. adapter->if_ops.unregister_dev(adapter);
  803. err_registerdev:
  804. adapter->surprise_removed = true;
  805. mwifiex_terminate_workqueue(adapter);
  806. err_kmalloc:
  807. if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
  808. (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
  809. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  810. adapter->init_wait_q_woken = false;
  811. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  812. wait_event_interruptible(adapter->init_wait_q,
  813. adapter->init_wait_q_woken);
  814. }
  815. mwifiex_free_adapter(adapter);
  816. err_init_sw:
  817. up(sem);
  818. exit_sem_err:
  819. return -1;
  820. }
  821. EXPORT_SYMBOL_GPL(mwifiex_add_card);
  822. /*
  823. * This function removes the card.
  824. *
  825. * This function follows the following major steps to remove the device -
  826. * - Stop data traffic
  827. * - Shutdown firmware
  828. * - Remove the logical interfaces
  829. * - Terminate the work queue
  830. * - Unregister the device
  831. * - Free the adapter structure
  832. */
  833. int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
  834. {
  835. struct mwifiex_private *priv = NULL;
  836. int i;
  837. if (down_interruptible(sem))
  838. goto exit_sem_err;
  839. if (!adapter)
  840. goto exit_remove;
  841. adapter->surprise_removed = true;
  842. /* Stop data */
  843. for (i = 0; i < adapter->priv_num; i++) {
  844. priv = adapter->priv[i];
  845. if (priv) {
  846. if (!netif_queue_stopped(priv->netdev))
  847. netif_stop_queue(priv->netdev);
  848. if (netif_carrier_ok(priv->netdev))
  849. netif_carrier_off(priv->netdev);
  850. }
  851. }
  852. dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
  853. adapter->init_wait_q_woken = false;
  854. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  855. wait_event_interruptible(adapter->init_wait_q,
  856. adapter->init_wait_q_woken);
  857. dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
  858. if (atomic_read(&adapter->rx_pending) ||
  859. atomic_read(&adapter->tx_pending) ||
  860. atomic_read(&adapter->cmd_pending)) {
  861. dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
  862. "cmd_pending=%d\n",
  863. atomic_read(&adapter->rx_pending),
  864. atomic_read(&adapter->tx_pending),
  865. atomic_read(&adapter->cmd_pending));
  866. }
  867. /* Remove interface */
  868. for (i = 0; i < adapter->priv_num; i++)
  869. mwifiex_remove_interface(adapter, i);
  870. mwifiex_terminate_workqueue(adapter);
  871. /* Unregister device */
  872. dev_dbg(adapter->dev, "info: unregister device\n");
  873. adapter->if_ops.unregister_dev(adapter);
  874. /* Free adapter structure */
  875. dev_dbg(adapter->dev, "info: free adapter\n");
  876. mwifiex_free_adapter(adapter);
  877. exit_remove:
  878. up(sem);
  879. exit_sem_err:
  880. return 0;
  881. }
  882. EXPORT_SYMBOL_GPL(mwifiex_remove_card);
  883. /*
  884. * This function initializes the module.
  885. *
  886. * The debug FS is also initialized if configured.
  887. */
  888. static int
  889. mwifiex_init_module(void)
  890. {
  891. #ifdef CONFIG_DEBUG_FS
  892. mwifiex_debugfs_init();
  893. #endif
  894. return 0;
  895. }
  896. /*
  897. * This function cleans up the module.
  898. *
  899. * The debug FS is removed if available.
  900. */
  901. static void
  902. mwifiex_cleanup_module(void)
  903. {
  904. #ifdef CONFIG_DEBUG_FS
  905. mwifiex_debugfs_remove();
  906. #endif
  907. }
  908. module_init(mwifiex_init_module);
  909. module_exit(mwifiex_cleanup_module);
  910. MODULE_AUTHOR("Marvell International Ltd.");
  911. MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
  912. MODULE_VERSION(VERSION);
  913. MODULE_LICENSE("GPL v2");