main.c 27 KB

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