PageRenderTime 196ms CodeModel.GetById 67ms RepoModel.GetById 0ms app.codeStats 2ms

/drivers/net/wireless/mwl8k.c

http://github.com/mirrors/linux
C | 6168 lines | 4529 code | 1064 blank | 575 comment | 750 complexity | 6c9d05b4b610157d9cc960eae79b8366 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * drivers/net/wireless/mwl8k.c
  3. * Driver for Marvell TOPDOG 802.11 Wireless cards
  4. *
  5. * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/list.h>
  18. #include <linux/pci.h>
  19. #include <linux/delay.h>
  20. #include <linux/completion.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/slab.h>
  23. #include <net/mac80211.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/firmware.h>
  26. #include <linux/workqueue.h>
  27. #define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
  28. #define MWL8K_NAME KBUILD_MODNAME
  29. #define MWL8K_VERSION "0.13"
  30. /* Module parameters */
  31. static bool ap_mode_default;
  32. module_param(ap_mode_default, bool, 0);
  33. MODULE_PARM_DESC(ap_mode_default,
  34. "Set to 1 to make ap mode the default instead of sta mode");
  35. /* Register definitions */
  36. #define MWL8K_HIU_GEN_PTR 0x00000c10
  37. #define MWL8K_MODE_STA 0x0000005a
  38. #define MWL8K_MODE_AP 0x000000a5
  39. #define MWL8K_HIU_INT_CODE 0x00000c14
  40. #define MWL8K_FWSTA_READY 0xf0f1f2f4
  41. #define MWL8K_FWAP_READY 0xf1f2f4a5
  42. #define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
  43. #define MWL8K_HIU_SCRATCH 0x00000c40
  44. /* Host->device communications */
  45. #define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
  46. #define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
  47. #define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
  48. #define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
  49. #define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
  50. #define MWL8K_H2A_INT_DUMMY (1 << 20)
  51. #define MWL8K_H2A_INT_RESET (1 << 15)
  52. #define MWL8K_H2A_INT_DOORBELL (1 << 1)
  53. #define MWL8K_H2A_INT_PPA_READY (1 << 0)
  54. /* Device->host communications */
  55. #define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
  56. #define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
  57. #define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
  58. #define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
  59. #define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
  60. #define MWL8K_A2H_INT_DUMMY (1 << 20)
  61. #define MWL8K_A2H_INT_BA_WATCHDOG (1 << 14)
  62. #define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
  63. #define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
  64. #define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
  65. #define MWL8K_A2H_INT_RADIO_ON (1 << 6)
  66. #define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
  67. #define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
  68. #define MWL8K_A2H_INT_OPC_DONE (1 << 2)
  69. #define MWL8K_A2H_INT_RX_READY (1 << 1)
  70. #define MWL8K_A2H_INT_TX_DONE (1 << 0)
  71. /* HW micro second timer register
  72. * located at offset 0xA600. This
  73. * will be used to timestamp tx
  74. * packets.
  75. */
  76. #define MWL8K_HW_TIMER_REGISTER 0x0000a600
  77. #define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
  78. MWL8K_A2H_INT_CHNL_SWITCHED | \
  79. MWL8K_A2H_INT_QUEUE_EMPTY | \
  80. MWL8K_A2H_INT_RADAR_DETECT | \
  81. MWL8K_A2H_INT_RADIO_ON | \
  82. MWL8K_A2H_INT_RADIO_OFF | \
  83. MWL8K_A2H_INT_MAC_EVENT | \
  84. MWL8K_A2H_INT_OPC_DONE | \
  85. MWL8K_A2H_INT_RX_READY | \
  86. MWL8K_A2H_INT_TX_DONE | \
  87. MWL8K_A2H_INT_BA_WATCHDOG)
  88. #define MWL8K_RX_QUEUES 1
  89. #define MWL8K_TX_WMM_QUEUES 4
  90. #define MWL8K_MAX_AMPDU_QUEUES 8
  91. #define MWL8K_MAX_TX_QUEUES (MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
  92. #define mwl8k_tx_queues(priv) (MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
  93. /* txpriorities are mapped with hw queues.
  94. * Each hw queue has a txpriority.
  95. */
  96. #define TOTAL_HW_TX_QUEUES 8
  97. /* Each HW queue can have one AMPDU stream.
  98. * But, because one of the hw queue is reserved,
  99. * maximum AMPDU queues that can be created are
  100. * one short of total tx queues.
  101. */
  102. #define MWL8K_NUM_AMPDU_STREAMS (TOTAL_HW_TX_QUEUES - 1)
  103. struct rxd_ops {
  104. int rxd_size;
  105. void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
  106. void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
  107. int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
  108. __le16 *qos, s8 *noise);
  109. };
  110. struct mwl8k_device_info {
  111. char *part_name;
  112. char *helper_image;
  113. char *fw_image_sta;
  114. char *fw_image_ap;
  115. struct rxd_ops *ap_rxd_ops;
  116. u32 fw_api_ap;
  117. };
  118. struct mwl8k_rx_queue {
  119. int rxd_count;
  120. /* hw receives here */
  121. int head;
  122. /* refill descs here */
  123. int tail;
  124. void *rxd;
  125. dma_addr_t rxd_dma;
  126. struct {
  127. struct sk_buff *skb;
  128. DEFINE_DMA_UNMAP_ADDR(dma);
  129. } *buf;
  130. };
  131. struct mwl8k_tx_queue {
  132. /* hw transmits here */
  133. int head;
  134. /* sw appends here */
  135. int tail;
  136. unsigned int len;
  137. struct mwl8k_tx_desc *txd;
  138. dma_addr_t txd_dma;
  139. struct sk_buff **skb;
  140. };
  141. enum {
  142. AMPDU_NO_STREAM,
  143. AMPDU_STREAM_NEW,
  144. AMPDU_STREAM_IN_PROGRESS,
  145. AMPDU_STREAM_ACTIVE,
  146. };
  147. struct mwl8k_ampdu_stream {
  148. struct ieee80211_sta *sta;
  149. u8 tid;
  150. u8 state;
  151. u8 idx;
  152. };
  153. struct mwl8k_priv {
  154. struct ieee80211_hw *hw;
  155. struct pci_dev *pdev;
  156. int irq;
  157. struct mwl8k_device_info *device_info;
  158. void __iomem *sram;
  159. void __iomem *regs;
  160. /* firmware */
  161. const struct firmware *fw_helper;
  162. const struct firmware *fw_ucode;
  163. /* hardware/firmware parameters */
  164. bool ap_fw;
  165. struct rxd_ops *rxd_ops;
  166. struct ieee80211_supported_band band_24;
  167. struct ieee80211_channel channels_24[14];
  168. struct ieee80211_rate rates_24[13];
  169. struct ieee80211_supported_band band_50;
  170. struct ieee80211_channel channels_50[4];
  171. struct ieee80211_rate rates_50[8];
  172. u32 ap_macids_supported;
  173. u32 sta_macids_supported;
  174. /* Ampdu stream information */
  175. u8 num_ampdu_queues;
  176. spinlock_t stream_lock;
  177. struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
  178. struct work_struct watchdog_ba_handle;
  179. /* firmware access */
  180. struct mutex fw_mutex;
  181. struct task_struct *fw_mutex_owner;
  182. struct task_struct *hw_restart_owner;
  183. int fw_mutex_depth;
  184. struct completion *hostcmd_wait;
  185. atomic_t watchdog_event_pending;
  186. /* lock held over TX and TX reap */
  187. spinlock_t tx_lock;
  188. /* TX quiesce completion, protected by fw_mutex and tx_lock */
  189. struct completion *tx_wait;
  190. /* List of interfaces. */
  191. u32 macids_used;
  192. struct list_head vif_list;
  193. /* power management status cookie from firmware */
  194. u32 *cookie;
  195. dma_addr_t cookie_dma;
  196. u16 num_mcaddrs;
  197. u8 hw_rev;
  198. u32 fw_rev;
  199. u32 caps;
  200. /*
  201. * Running count of TX packets in flight, to avoid
  202. * iterating over the transmit rings each time.
  203. */
  204. int pending_tx_pkts;
  205. struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
  206. struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
  207. u32 txq_offset[MWL8K_MAX_TX_QUEUES];
  208. bool radio_on;
  209. bool radio_short_preamble;
  210. bool sniffer_enabled;
  211. bool wmm_enabled;
  212. /* XXX need to convert this to handle multiple interfaces */
  213. bool capture_beacon;
  214. u8 capture_bssid[ETH_ALEN];
  215. struct sk_buff *beacon_skb;
  216. /*
  217. * This FJ worker has to be global as it is scheduled from the
  218. * RX handler. At this point we don't know which interface it
  219. * belongs to until the list of bssids waiting to complete join
  220. * is checked.
  221. */
  222. struct work_struct finalize_join_worker;
  223. /* Tasklet to perform TX reclaim. */
  224. struct tasklet_struct poll_tx_task;
  225. /* Tasklet to perform RX. */
  226. struct tasklet_struct poll_rx_task;
  227. /* Most recently reported noise in dBm */
  228. s8 noise;
  229. /*
  230. * preserve the queue configurations so they can be restored if/when
  231. * the firmware image is swapped.
  232. */
  233. struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
  234. /* To perform the task of reloading the firmware */
  235. struct work_struct fw_reload;
  236. bool hw_restart_in_progress;
  237. /* async firmware loading state */
  238. unsigned fw_state;
  239. char *fw_pref;
  240. char *fw_alt;
  241. bool is_8764;
  242. struct completion firmware_loading_complete;
  243. /* bitmap of running BSSes */
  244. u32 running_bsses;
  245. };
  246. #define MAX_WEP_KEY_LEN 13
  247. #define NUM_WEP_KEYS 4
  248. /* Per interface specific private data */
  249. struct mwl8k_vif {
  250. struct list_head list;
  251. struct ieee80211_vif *vif;
  252. /* Firmware macid for this vif. */
  253. int macid;
  254. /* Non AMPDU sequence number assigned by driver. */
  255. u16 seqno;
  256. /* Saved WEP keys */
  257. struct {
  258. u8 enabled;
  259. u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
  260. } wep_key_conf[NUM_WEP_KEYS];
  261. /* BSSID */
  262. u8 bssid[ETH_ALEN];
  263. /* A flag to indicate is HW crypto is enabled for this bssid */
  264. bool is_hw_crypto_enabled;
  265. };
  266. #define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
  267. #define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
  268. struct tx_traffic_info {
  269. u32 start_time;
  270. u32 pkts;
  271. };
  272. #define MWL8K_MAX_TID 8
  273. struct mwl8k_sta {
  274. /* Index into station database. Returned by UPDATE_STADB. */
  275. u8 peer_id;
  276. u8 is_ampdu_allowed;
  277. struct tx_traffic_info tx_stats[MWL8K_MAX_TID];
  278. };
  279. #define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
  280. static const struct ieee80211_channel mwl8k_channels_24[] = {
  281. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2412, .hw_value = 1, },
  282. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2417, .hw_value = 2, },
  283. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2422, .hw_value = 3, },
  284. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2427, .hw_value = 4, },
  285. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2432, .hw_value = 5, },
  286. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2437, .hw_value = 6, },
  287. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2442, .hw_value = 7, },
  288. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2447, .hw_value = 8, },
  289. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2452, .hw_value = 9, },
  290. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2457, .hw_value = 10, },
  291. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2462, .hw_value = 11, },
  292. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2467, .hw_value = 12, },
  293. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2472, .hw_value = 13, },
  294. { .band = IEEE80211_BAND_2GHZ, .center_freq = 2484, .hw_value = 14, },
  295. };
  296. static const struct ieee80211_rate mwl8k_rates_24[] = {
  297. { .bitrate = 10, .hw_value = 2, },
  298. { .bitrate = 20, .hw_value = 4, },
  299. { .bitrate = 55, .hw_value = 11, },
  300. { .bitrate = 110, .hw_value = 22, },
  301. { .bitrate = 220, .hw_value = 44, },
  302. { .bitrate = 60, .hw_value = 12, },
  303. { .bitrate = 90, .hw_value = 18, },
  304. { .bitrate = 120, .hw_value = 24, },
  305. { .bitrate = 180, .hw_value = 36, },
  306. { .bitrate = 240, .hw_value = 48, },
  307. { .bitrate = 360, .hw_value = 72, },
  308. { .bitrate = 480, .hw_value = 96, },
  309. { .bitrate = 540, .hw_value = 108, },
  310. };
  311. static const struct ieee80211_channel mwl8k_channels_50[] = {
  312. { .band = IEEE80211_BAND_5GHZ, .center_freq = 5180, .hw_value = 36, },
  313. { .band = IEEE80211_BAND_5GHZ, .center_freq = 5200, .hw_value = 40, },
  314. { .band = IEEE80211_BAND_5GHZ, .center_freq = 5220, .hw_value = 44, },
  315. { .band = IEEE80211_BAND_5GHZ, .center_freq = 5240, .hw_value = 48, },
  316. };
  317. static const struct ieee80211_rate mwl8k_rates_50[] = {
  318. { .bitrate = 60, .hw_value = 12, },
  319. { .bitrate = 90, .hw_value = 18, },
  320. { .bitrate = 120, .hw_value = 24, },
  321. { .bitrate = 180, .hw_value = 36, },
  322. { .bitrate = 240, .hw_value = 48, },
  323. { .bitrate = 360, .hw_value = 72, },
  324. { .bitrate = 480, .hw_value = 96, },
  325. { .bitrate = 540, .hw_value = 108, },
  326. };
  327. /* Set or get info from Firmware */
  328. #define MWL8K_CMD_GET 0x0000
  329. #define MWL8K_CMD_SET 0x0001
  330. #define MWL8K_CMD_SET_LIST 0x0002
  331. /* Firmware command codes */
  332. #define MWL8K_CMD_CODE_DNLD 0x0001
  333. #define MWL8K_CMD_GET_HW_SPEC 0x0003
  334. #define MWL8K_CMD_SET_HW_SPEC 0x0004
  335. #define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
  336. #define MWL8K_CMD_GET_STAT 0x0014
  337. #define MWL8K_CMD_RADIO_CONTROL 0x001c
  338. #define MWL8K_CMD_RF_TX_POWER 0x001e
  339. #define MWL8K_CMD_TX_POWER 0x001f
  340. #define MWL8K_CMD_RF_ANTENNA 0x0020
  341. #define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
  342. #define MWL8K_CMD_SET_PRE_SCAN 0x0107
  343. #define MWL8K_CMD_SET_POST_SCAN 0x0108
  344. #define MWL8K_CMD_SET_RF_CHANNEL 0x010a
  345. #define MWL8K_CMD_SET_AID 0x010d
  346. #define MWL8K_CMD_SET_RATE 0x0110
  347. #define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
  348. #define MWL8K_CMD_RTS_THRESHOLD 0x0113
  349. #define MWL8K_CMD_SET_SLOT 0x0114
  350. #define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
  351. #define MWL8K_CMD_SET_WMM_MODE 0x0123
  352. #define MWL8K_CMD_MIMO_CONFIG 0x0125
  353. #define MWL8K_CMD_USE_FIXED_RATE 0x0126
  354. #define MWL8K_CMD_ENABLE_SNIFFER 0x0150
  355. #define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
  356. #define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
  357. #define MWL8K_CMD_GET_WATCHDOG_BITMAP 0x0205
  358. #define MWL8K_CMD_DEL_MAC_ADDR 0x0206 /* per-vif */
  359. #define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
  360. #define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
  361. #define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
  362. #define MWL8K_CMD_UPDATE_STADB 0x1123
  363. #define MWL8K_CMD_BASTREAM 0x1125
  364. static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
  365. {
  366. u16 command = le16_to_cpu(cmd);
  367. #define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
  368. snprintf(buf, bufsize, "%s", #x);\
  369. return buf;\
  370. } while (0)
  371. switch (command & ~0x8000) {
  372. MWL8K_CMDNAME(CODE_DNLD);
  373. MWL8K_CMDNAME(GET_HW_SPEC);
  374. MWL8K_CMDNAME(SET_HW_SPEC);
  375. MWL8K_CMDNAME(MAC_MULTICAST_ADR);
  376. MWL8K_CMDNAME(GET_STAT);
  377. MWL8K_CMDNAME(RADIO_CONTROL);
  378. MWL8K_CMDNAME(RF_TX_POWER);
  379. MWL8K_CMDNAME(TX_POWER);
  380. MWL8K_CMDNAME(RF_ANTENNA);
  381. MWL8K_CMDNAME(SET_BEACON);
  382. MWL8K_CMDNAME(SET_PRE_SCAN);
  383. MWL8K_CMDNAME(SET_POST_SCAN);
  384. MWL8K_CMDNAME(SET_RF_CHANNEL);
  385. MWL8K_CMDNAME(SET_AID);
  386. MWL8K_CMDNAME(SET_RATE);
  387. MWL8K_CMDNAME(SET_FINALIZE_JOIN);
  388. MWL8K_CMDNAME(RTS_THRESHOLD);
  389. MWL8K_CMDNAME(SET_SLOT);
  390. MWL8K_CMDNAME(SET_EDCA_PARAMS);
  391. MWL8K_CMDNAME(SET_WMM_MODE);
  392. MWL8K_CMDNAME(MIMO_CONFIG);
  393. MWL8K_CMDNAME(USE_FIXED_RATE);
  394. MWL8K_CMDNAME(ENABLE_SNIFFER);
  395. MWL8K_CMDNAME(SET_MAC_ADDR);
  396. MWL8K_CMDNAME(SET_RATEADAPT_MODE);
  397. MWL8K_CMDNAME(BSS_START);
  398. MWL8K_CMDNAME(SET_NEW_STN);
  399. MWL8K_CMDNAME(UPDATE_ENCRYPTION);
  400. MWL8K_CMDNAME(UPDATE_STADB);
  401. MWL8K_CMDNAME(BASTREAM);
  402. MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
  403. default:
  404. snprintf(buf, bufsize, "0x%x", cmd);
  405. }
  406. #undef MWL8K_CMDNAME
  407. return buf;
  408. }
  409. /* Hardware and firmware reset */
  410. static void mwl8k_hw_reset(struct mwl8k_priv *priv)
  411. {
  412. iowrite32(MWL8K_H2A_INT_RESET,
  413. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  414. iowrite32(MWL8K_H2A_INT_RESET,
  415. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  416. msleep(20);
  417. }
  418. /* Release fw image */
  419. static void mwl8k_release_fw(const struct firmware **fw)
  420. {
  421. if (*fw == NULL)
  422. return;
  423. release_firmware(*fw);
  424. *fw = NULL;
  425. }
  426. static void mwl8k_release_firmware(struct mwl8k_priv *priv)
  427. {
  428. mwl8k_release_fw(&priv->fw_ucode);
  429. mwl8k_release_fw(&priv->fw_helper);
  430. }
  431. /* states for asynchronous f/w loading */
  432. static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
  433. enum {
  434. FW_STATE_INIT = 0,
  435. FW_STATE_LOADING_PREF,
  436. FW_STATE_LOADING_ALT,
  437. FW_STATE_ERROR,
  438. };
  439. /* Request fw image */
  440. static int mwl8k_request_fw(struct mwl8k_priv *priv,
  441. const char *fname, const struct firmware **fw,
  442. bool nowait)
  443. {
  444. /* release current image */
  445. if (*fw != NULL)
  446. mwl8k_release_fw(fw);
  447. if (nowait)
  448. return request_firmware_nowait(THIS_MODULE, 1, fname,
  449. &priv->pdev->dev, GFP_KERNEL,
  450. priv, mwl8k_fw_state_machine);
  451. else
  452. return request_firmware(fw, fname, &priv->pdev->dev);
  453. }
  454. static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
  455. bool nowait)
  456. {
  457. struct mwl8k_device_info *di = priv->device_info;
  458. int rc;
  459. if (di->helper_image != NULL) {
  460. if (nowait)
  461. rc = mwl8k_request_fw(priv, di->helper_image,
  462. &priv->fw_helper, true);
  463. else
  464. rc = mwl8k_request_fw(priv, di->helper_image,
  465. &priv->fw_helper, false);
  466. if (rc)
  467. printk(KERN_ERR "%s: Error requesting helper fw %s\n",
  468. pci_name(priv->pdev), di->helper_image);
  469. if (rc || nowait)
  470. return rc;
  471. }
  472. if (nowait) {
  473. /*
  474. * if we get here, no helper image is needed. Skip the
  475. * FW_STATE_INIT state.
  476. */
  477. priv->fw_state = FW_STATE_LOADING_PREF;
  478. rc = mwl8k_request_fw(priv, fw_image,
  479. &priv->fw_ucode,
  480. true);
  481. } else
  482. rc = mwl8k_request_fw(priv, fw_image,
  483. &priv->fw_ucode, false);
  484. if (rc) {
  485. printk(KERN_ERR "%s: Error requesting firmware file %s\n",
  486. pci_name(priv->pdev), fw_image);
  487. mwl8k_release_fw(&priv->fw_helper);
  488. return rc;
  489. }
  490. return 0;
  491. }
  492. struct mwl8k_cmd_pkt {
  493. __le16 code;
  494. __le16 length;
  495. __u8 seq_num;
  496. __u8 macid;
  497. __le16 result;
  498. char payload[0];
  499. } __packed;
  500. /*
  501. * Firmware loading.
  502. */
  503. static int
  504. mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
  505. {
  506. void __iomem *regs = priv->regs;
  507. dma_addr_t dma_addr;
  508. int loops;
  509. dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
  510. if (pci_dma_mapping_error(priv->pdev, dma_addr))
  511. return -ENOMEM;
  512. iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
  513. iowrite32(0, regs + MWL8K_HIU_INT_CODE);
  514. iowrite32(MWL8K_H2A_INT_DOORBELL,
  515. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  516. iowrite32(MWL8K_H2A_INT_DUMMY,
  517. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  518. loops = 1000;
  519. do {
  520. u32 int_code;
  521. if (priv->is_8764) {
  522. int_code = ioread32(regs +
  523. MWL8K_HIU_H2A_INTERRUPT_STATUS);
  524. if (int_code == 0)
  525. break;
  526. } else {
  527. int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
  528. if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
  529. iowrite32(0, regs + MWL8K_HIU_INT_CODE);
  530. break;
  531. }
  532. }
  533. cond_resched();
  534. udelay(1);
  535. } while (--loops);
  536. pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
  537. return loops ? 0 : -ETIMEDOUT;
  538. }
  539. static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
  540. const u8 *data, size_t length)
  541. {
  542. struct mwl8k_cmd_pkt *cmd;
  543. int done;
  544. int rc = 0;
  545. cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
  546. if (cmd == NULL)
  547. return -ENOMEM;
  548. cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
  549. cmd->seq_num = 0;
  550. cmd->macid = 0;
  551. cmd->result = 0;
  552. done = 0;
  553. while (length) {
  554. int block_size = length > 256 ? 256 : length;
  555. memcpy(cmd->payload, data + done, block_size);
  556. cmd->length = cpu_to_le16(block_size);
  557. rc = mwl8k_send_fw_load_cmd(priv, cmd,
  558. sizeof(*cmd) + block_size);
  559. if (rc)
  560. break;
  561. done += block_size;
  562. length -= block_size;
  563. }
  564. if (!rc) {
  565. cmd->length = 0;
  566. rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
  567. }
  568. kfree(cmd);
  569. return rc;
  570. }
  571. static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
  572. const u8 *data, size_t length)
  573. {
  574. unsigned char *buffer;
  575. int may_continue, rc = 0;
  576. u32 done, prev_block_size;
  577. buffer = kmalloc(1024, GFP_KERNEL);
  578. if (buffer == NULL)
  579. return -ENOMEM;
  580. done = 0;
  581. prev_block_size = 0;
  582. may_continue = 1000;
  583. while (may_continue > 0) {
  584. u32 block_size;
  585. block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
  586. if (block_size & 1) {
  587. block_size &= ~1;
  588. may_continue--;
  589. } else {
  590. done += prev_block_size;
  591. length -= prev_block_size;
  592. }
  593. if (block_size > 1024 || block_size > length) {
  594. rc = -EOVERFLOW;
  595. break;
  596. }
  597. if (length == 0) {
  598. rc = 0;
  599. break;
  600. }
  601. if (block_size == 0) {
  602. rc = -EPROTO;
  603. may_continue--;
  604. udelay(1);
  605. continue;
  606. }
  607. prev_block_size = block_size;
  608. memcpy(buffer, data + done, block_size);
  609. rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
  610. if (rc)
  611. break;
  612. }
  613. if (!rc && length != 0)
  614. rc = -EREMOTEIO;
  615. kfree(buffer);
  616. return rc;
  617. }
  618. static int mwl8k_load_firmware(struct ieee80211_hw *hw)
  619. {
  620. struct mwl8k_priv *priv = hw->priv;
  621. const struct firmware *fw = priv->fw_ucode;
  622. int rc;
  623. int loops;
  624. if (!memcmp(fw->data, "\x01\x00\x00\x00", 4) && !priv->is_8764) {
  625. const struct firmware *helper = priv->fw_helper;
  626. if (helper == NULL) {
  627. printk(KERN_ERR "%s: helper image needed but none "
  628. "given\n", pci_name(priv->pdev));
  629. return -EINVAL;
  630. }
  631. rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
  632. if (rc) {
  633. printk(KERN_ERR "%s: unable to load firmware "
  634. "helper image\n", pci_name(priv->pdev));
  635. return rc;
  636. }
  637. msleep(20);
  638. rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
  639. } else {
  640. if (priv->is_8764)
  641. rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
  642. else
  643. rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
  644. }
  645. if (rc) {
  646. printk(KERN_ERR "%s: unable to load firmware image\n",
  647. pci_name(priv->pdev));
  648. return rc;
  649. }
  650. iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
  651. loops = 500000;
  652. do {
  653. u32 ready_code;
  654. ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
  655. if (ready_code == MWL8K_FWAP_READY) {
  656. priv->ap_fw = true;
  657. break;
  658. } else if (ready_code == MWL8K_FWSTA_READY) {
  659. priv->ap_fw = false;
  660. break;
  661. }
  662. cond_resched();
  663. udelay(1);
  664. } while (--loops);
  665. return loops ? 0 : -ETIMEDOUT;
  666. }
  667. /* DMA header used by firmware and hardware. */
  668. struct mwl8k_dma_data {
  669. __le16 fwlen;
  670. struct ieee80211_hdr wh;
  671. char data[0];
  672. } __packed;
  673. /* Routines to add/remove DMA header from skb. */
  674. static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
  675. {
  676. struct mwl8k_dma_data *tr;
  677. int hdrlen;
  678. tr = (struct mwl8k_dma_data *)skb->data;
  679. hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
  680. if (hdrlen != sizeof(tr->wh)) {
  681. if (ieee80211_is_data_qos(tr->wh.frame_control)) {
  682. memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
  683. *((__le16 *)(tr->data - 2)) = qos;
  684. } else {
  685. memmove(tr->data - hdrlen, &tr->wh, hdrlen);
  686. }
  687. }
  688. if (hdrlen != sizeof(*tr))
  689. skb_pull(skb, sizeof(*tr) - hdrlen);
  690. }
  691. #define REDUCED_TX_HEADROOM 8
  692. static void
  693. mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
  694. int head_pad, int tail_pad)
  695. {
  696. struct ieee80211_hdr *wh;
  697. int hdrlen;
  698. int reqd_hdrlen;
  699. struct mwl8k_dma_data *tr;
  700. /*
  701. * Add a firmware DMA header; the firmware requires that we
  702. * present a 2-byte payload length followed by a 4-address
  703. * header (without QoS field), followed (optionally) by any
  704. * WEP/ExtIV header (but only filled in for CCMP).
  705. */
  706. wh = (struct ieee80211_hdr *)skb->data;
  707. hdrlen = ieee80211_hdrlen(wh->frame_control);
  708. /*
  709. * Check if skb_resize is required because of
  710. * tx_headroom adjustment.
  711. */
  712. if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
  713. + REDUCED_TX_HEADROOM))) {
  714. if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
  715. wiphy_err(priv->hw->wiphy,
  716. "Failed to reallocate TX buffer\n");
  717. return;
  718. }
  719. skb->truesize += REDUCED_TX_HEADROOM;
  720. }
  721. reqd_hdrlen = sizeof(*tr) + head_pad;
  722. if (hdrlen != reqd_hdrlen)
  723. skb_push(skb, reqd_hdrlen - hdrlen);
  724. if (ieee80211_is_data_qos(wh->frame_control))
  725. hdrlen -= IEEE80211_QOS_CTL_LEN;
  726. tr = (struct mwl8k_dma_data *)skb->data;
  727. if (wh != &tr->wh)
  728. memmove(&tr->wh, wh, hdrlen);
  729. if (hdrlen != sizeof(tr->wh))
  730. memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
  731. /*
  732. * Firmware length is the length of the fully formed "802.11
  733. * payload". That is, everything except for the 802.11 header.
  734. * This includes all crypto material including the MIC.
  735. */
  736. tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
  737. }
  738. static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
  739. struct sk_buff *skb)
  740. {
  741. struct ieee80211_hdr *wh;
  742. struct ieee80211_tx_info *tx_info;
  743. struct ieee80211_key_conf *key_conf;
  744. int data_pad;
  745. int head_pad = 0;
  746. wh = (struct ieee80211_hdr *)skb->data;
  747. tx_info = IEEE80211_SKB_CB(skb);
  748. key_conf = NULL;
  749. if (ieee80211_is_data(wh->frame_control))
  750. key_conf = tx_info->control.hw_key;
  751. /*
  752. * Make sure the packet header is in the DMA header format (4-address
  753. * without QoS), and add head & tail padding when HW crypto is enabled.
  754. *
  755. * We have the following trailer padding requirements:
  756. * - WEP: 4 trailer bytes (ICV)
  757. * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
  758. * - CCMP: 8 trailer bytes (MIC)
  759. */
  760. data_pad = 0;
  761. if (key_conf != NULL) {
  762. head_pad = key_conf->iv_len;
  763. switch (key_conf->cipher) {
  764. case WLAN_CIPHER_SUITE_WEP40:
  765. case WLAN_CIPHER_SUITE_WEP104:
  766. data_pad = 4;
  767. break;
  768. case WLAN_CIPHER_SUITE_TKIP:
  769. data_pad = 12;
  770. break;
  771. case WLAN_CIPHER_SUITE_CCMP:
  772. data_pad = 8;
  773. break;
  774. }
  775. }
  776. mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
  777. }
  778. /*
  779. * Packet reception for 88w8366/88w8764 AP firmware.
  780. */
  781. struct mwl8k_rxd_ap {
  782. __le16 pkt_len;
  783. __u8 sq2;
  784. __u8 rate;
  785. __le32 pkt_phys_addr;
  786. __le32 next_rxd_phys_addr;
  787. __le16 qos_control;
  788. __le16 htsig2;
  789. __le32 hw_rssi_info;
  790. __le32 hw_noise_floor_info;
  791. __u8 noise_floor;
  792. __u8 pad0[3];
  793. __u8 rssi;
  794. __u8 rx_status;
  795. __u8 channel;
  796. __u8 rx_ctrl;
  797. } __packed;
  798. #define MWL8K_AP_RATE_INFO_MCS_FORMAT 0x80
  799. #define MWL8K_AP_RATE_INFO_40MHZ 0x40
  800. #define MWL8K_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
  801. #define MWL8K_AP_RX_CTRL_OWNED_BY_HOST 0x80
  802. /* 8366/8764 AP rx_status bits */
  803. #define MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
  804. #define MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
  805. #define MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
  806. #define MWL8K_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
  807. #define MWL8K_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
  808. static void mwl8k_rxd_ap_init(void *_rxd, dma_addr_t next_dma_addr)
  809. {
  810. struct mwl8k_rxd_ap *rxd = _rxd;
  811. rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
  812. rxd->rx_ctrl = MWL8K_AP_RX_CTRL_OWNED_BY_HOST;
  813. }
  814. static void mwl8k_rxd_ap_refill(void *_rxd, dma_addr_t addr, int len)
  815. {
  816. struct mwl8k_rxd_ap *rxd = _rxd;
  817. rxd->pkt_len = cpu_to_le16(len);
  818. rxd->pkt_phys_addr = cpu_to_le32(addr);
  819. wmb();
  820. rxd->rx_ctrl = 0;
  821. }
  822. static int
  823. mwl8k_rxd_ap_process(void *_rxd, struct ieee80211_rx_status *status,
  824. __le16 *qos, s8 *noise)
  825. {
  826. struct mwl8k_rxd_ap *rxd = _rxd;
  827. if (!(rxd->rx_ctrl & MWL8K_AP_RX_CTRL_OWNED_BY_HOST))
  828. return -1;
  829. rmb();
  830. memset(status, 0, sizeof(*status));
  831. status->signal = -rxd->rssi;
  832. *noise = -rxd->noise_floor;
  833. if (rxd->rate & MWL8K_AP_RATE_INFO_MCS_FORMAT) {
  834. status->flag |= RX_FLAG_HT;
  835. if (rxd->rate & MWL8K_AP_RATE_INFO_40MHZ)
  836. status->flag |= RX_FLAG_40MHZ;
  837. status->rate_idx = MWL8K_AP_RATE_INFO_RATEID(rxd->rate);
  838. } else {
  839. int i;
  840. for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
  841. if (mwl8k_rates_24[i].hw_value == rxd->rate) {
  842. status->rate_idx = i;
  843. break;
  844. }
  845. }
  846. }
  847. if (rxd->channel > 14) {
  848. status->band = IEEE80211_BAND_5GHZ;
  849. if (!(status->flag & RX_FLAG_HT))
  850. status->rate_idx -= 5;
  851. } else {
  852. status->band = IEEE80211_BAND_2GHZ;
  853. }
  854. status->freq = ieee80211_channel_to_frequency(rxd->channel,
  855. status->band);
  856. *qos = rxd->qos_control;
  857. if ((rxd->rx_status != MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
  858. (rxd->rx_status & MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK) &&
  859. (rxd->rx_status & MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
  860. status->flag |= RX_FLAG_MMIC_ERROR;
  861. return le16_to_cpu(rxd->pkt_len);
  862. }
  863. static struct rxd_ops rxd_ap_ops = {
  864. .rxd_size = sizeof(struct mwl8k_rxd_ap),
  865. .rxd_init = mwl8k_rxd_ap_init,
  866. .rxd_refill = mwl8k_rxd_ap_refill,
  867. .rxd_process = mwl8k_rxd_ap_process,
  868. };
  869. /*
  870. * Packet reception for STA firmware.
  871. */
  872. struct mwl8k_rxd_sta {
  873. __le16 pkt_len;
  874. __u8 link_quality;
  875. __u8 noise_level;
  876. __le32 pkt_phys_addr;
  877. __le32 next_rxd_phys_addr;
  878. __le16 qos_control;
  879. __le16 rate_info;
  880. __le32 pad0[4];
  881. __u8 rssi;
  882. __u8 channel;
  883. __le16 pad1;
  884. __u8 rx_ctrl;
  885. __u8 rx_status;
  886. __u8 pad2[2];
  887. } __packed;
  888. #define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
  889. #define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
  890. #define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
  891. #define MWL8K_STA_RATE_INFO_40MHZ 0x0004
  892. #define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
  893. #define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
  894. #define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
  895. #define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
  896. /* ICV=0 or MIC=1 */
  897. #define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
  898. /* Key is uploaded only in failure case */
  899. #define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
  900. static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
  901. {
  902. struct mwl8k_rxd_sta *rxd = _rxd;
  903. rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
  904. rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
  905. }
  906. static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
  907. {
  908. struct mwl8k_rxd_sta *rxd = _rxd;
  909. rxd->pkt_len = cpu_to_le16(len);
  910. rxd->pkt_phys_addr = cpu_to_le32(addr);
  911. wmb();
  912. rxd->rx_ctrl = 0;
  913. }
  914. static int
  915. mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
  916. __le16 *qos, s8 *noise)
  917. {
  918. struct mwl8k_rxd_sta *rxd = _rxd;
  919. u16 rate_info;
  920. if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
  921. return -1;
  922. rmb();
  923. rate_info = le16_to_cpu(rxd->rate_info);
  924. memset(status, 0, sizeof(*status));
  925. status->signal = -rxd->rssi;
  926. *noise = -rxd->noise_level;
  927. status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
  928. status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
  929. if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
  930. status->flag |= RX_FLAG_SHORTPRE;
  931. if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
  932. status->flag |= RX_FLAG_40MHZ;
  933. if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
  934. status->flag |= RX_FLAG_SHORT_GI;
  935. if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
  936. status->flag |= RX_FLAG_HT;
  937. if (rxd->channel > 14) {
  938. status->band = IEEE80211_BAND_5GHZ;
  939. if (!(status->flag & RX_FLAG_HT))
  940. status->rate_idx -= 5;
  941. } else {
  942. status->band = IEEE80211_BAND_2GHZ;
  943. }
  944. status->freq = ieee80211_channel_to_frequency(rxd->channel,
  945. status->band);
  946. *qos = rxd->qos_control;
  947. if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
  948. (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
  949. status->flag |= RX_FLAG_MMIC_ERROR;
  950. return le16_to_cpu(rxd->pkt_len);
  951. }
  952. static struct rxd_ops rxd_sta_ops = {
  953. .rxd_size = sizeof(struct mwl8k_rxd_sta),
  954. .rxd_init = mwl8k_rxd_sta_init,
  955. .rxd_refill = mwl8k_rxd_sta_refill,
  956. .rxd_process = mwl8k_rxd_sta_process,
  957. };
  958. #define MWL8K_RX_DESCS 256
  959. #define MWL8K_RX_MAXSZ 3800
  960. static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
  961. {
  962. struct mwl8k_priv *priv = hw->priv;
  963. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  964. int size;
  965. int i;
  966. rxq->rxd_count = 0;
  967. rxq->head = 0;
  968. rxq->tail = 0;
  969. size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
  970. rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
  971. if (rxq->rxd == NULL) {
  972. wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
  973. return -ENOMEM;
  974. }
  975. memset(rxq->rxd, 0, size);
  976. rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
  977. if (rxq->buf == NULL) {
  978. pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
  979. return -ENOMEM;
  980. }
  981. for (i = 0; i < MWL8K_RX_DESCS; i++) {
  982. int desc_size;
  983. void *rxd;
  984. int nexti;
  985. dma_addr_t next_dma_addr;
  986. desc_size = priv->rxd_ops->rxd_size;
  987. rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
  988. nexti = i + 1;
  989. if (nexti == MWL8K_RX_DESCS)
  990. nexti = 0;
  991. next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
  992. priv->rxd_ops->rxd_init(rxd, next_dma_addr);
  993. }
  994. return 0;
  995. }
  996. static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
  997. {
  998. struct mwl8k_priv *priv = hw->priv;
  999. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  1000. int refilled;
  1001. refilled = 0;
  1002. while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
  1003. struct sk_buff *skb;
  1004. dma_addr_t addr;
  1005. int rx;
  1006. void *rxd;
  1007. skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
  1008. if (skb == NULL)
  1009. break;
  1010. addr = pci_map_single(priv->pdev, skb->data,
  1011. MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
  1012. rxq->rxd_count++;
  1013. rx = rxq->tail++;
  1014. if (rxq->tail == MWL8K_RX_DESCS)
  1015. rxq->tail = 0;
  1016. rxq->buf[rx].skb = skb;
  1017. dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
  1018. rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
  1019. priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
  1020. refilled++;
  1021. }
  1022. return refilled;
  1023. }
  1024. /* Must be called only when the card's reception is completely halted */
  1025. static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
  1026. {
  1027. struct mwl8k_priv *priv = hw->priv;
  1028. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  1029. int i;
  1030. if (rxq->rxd == NULL)
  1031. return;
  1032. for (i = 0; i < MWL8K_RX_DESCS; i++) {
  1033. if (rxq->buf[i].skb != NULL) {
  1034. pci_unmap_single(priv->pdev,
  1035. dma_unmap_addr(&rxq->buf[i], dma),
  1036. MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
  1037. dma_unmap_addr_set(&rxq->buf[i], dma, 0);
  1038. kfree_skb(rxq->buf[i].skb);
  1039. rxq->buf[i].skb = NULL;
  1040. }
  1041. }
  1042. kfree(rxq->buf);
  1043. rxq->buf = NULL;
  1044. pci_free_consistent(priv->pdev,
  1045. MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
  1046. rxq->rxd, rxq->rxd_dma);
  1047. rxq->rxd = NULL;
  1048. }
  1049. /*
  1050. * Scan a list of BSSIDs to process for finalize join.
  1051. * Allows for extension to process multiple BSSIDs.
  1052. */
  1053. static inline int
  1054. mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
  1055. {
  1056. return priv->capture_beacon &&
  1057. ieee80211_is_beacon(wh->frame_control) &&
  1058. ether_addr_equal(wh->addr3, priv->capture_bssid);
  1059. }
  1060. static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
  1061. struct sk_buff *skb)
  1062. {
  1063. struct mwl8k_priv *priv = hw->priv;
  1064. priv->capture_beacon = false;
  1065. memset(priv->capture_bssid, 0, ETH_ALEN);
  1066. /*
  1067. * Use GFP_ATOMIC as rxq_process is called from
  1068. * the primary interrupt handler, memory allocation call
  1069. * must not sleep.
  1070. */
  1071. priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
  1072. if (priv->beacon_skb != NULL)
  1073. ieee80211_queue_work(hw, &priv->finalize_join_worker);
  1074. }
  1075. static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
  1076. u8 *bssid)
  1077. {
  1078. struct mwl8k_vif *mwl8k_vif;
  1079. list_for_each_entry(mwl8k_vif,
  1080. vif_list, list) {
  1081. if (memcmp(bssid, mwl8k_vif->bssid,
  1082. ETH_ALEN) == 0)
  1083. return mwl8k_vif;
  1084. }
  1085. return NULL;
  1086. }
  1087. static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
  1088. {
  1089. struct mwl8k_priv *priv = hw->priv;
  1090. struct mwl8k_vif *mwl8k_vif = NULL;
  1091. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  1092. int processed;
  1093. processed = 0;
  1094. while (rxq->rxd_count && limit--) {
  1095. struct sk_buff *skb;
  1096. void *rxd;
  1097. int pkt_len;
  1098. struct ieee80211_rx_status status;
  1099. struct ieee80211_hdr *wh;
  1100. __le16 qos;
  1101. skb = rxq->buf[rxq->head].skb;
  1102. if (skb == NULL)
  1103. break;
  1104. rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
  1105. pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
  1106. &priv->noise);
  1107. if (pkt_len < 0)
  1108. break;
  1109. rxq->buf[rxq->head].skb = NULL;
  1110. pci_unmap_single(priv->pdev,
  1111. dma_unmap_addr(&rxq->buf[rxq->head], dma),
  1112. MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
  1113. dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
  1114. rxq->head++;
  1115. if (rxq->head == MWL8K_RX_DESCS)
  1116. rxq->head = 0;
  1117. rxq->rxd_count--;
  1118. wh = &((struct mwl8k_dma_data *)skb->data)->wh;
  1119. /*
  1120. * Check for a pending join operation. Save a
  1121. * copy of the beacon and schedule a tasklet to
  1122. * send a FINALIZE_JOIN command to the firmware.
  1123. */
  1124. if (mwl8k_capture_bssid(priv, (void *)skb->data))
  1125. mwl8k_save_beacon(hw, skb);
  1126. if (ieee80211_has_protected(wh->frame_control)) {
  1127. /* Check if hw crypto has been enabled for
  1128. * this bss. If yes, set the status flags
  1129. * accordingly
  1130. */
  1131. mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
  1132. wh->addr1);
  1133. if (mwl8k_vif != NULL &&
  1134. mwl8k_vif->is_hw_crypto_enabled) {
  1135. /*
  1136. * When MMIC ERROR is encountered
  1137. * by the firmware, payload is
  1138. * dropped and only 32 bytes of
  1139. * mwl8k Firmware header is sent
  1140. * to the host.
  1141. *
  1142. * We need to add four bytes of
  1143. * key information. In it
  1144. * MAC80211 expects keyidx set to
  1145. * 0 for triggering Counter
  1146. * Measure of MMIC failure.
  1147. */
  1148. if (status.flag & RX_FLAG_MMIC_ERROR) {
  1149. struct mwl8k_dma_data *tr;
  1150. tr = (struct mwl8k_dma_data *)skb->data;
  1151. memset((void *)&(tr->data), 0, 4);
  1152. pkt_len += 4;
  1153. }
  1154. if (!ieee80211_is_auth(wh->frame_control))
  1155. status.flag |= RX_FLAG_IV_STRIPPED |
  1156. RX_FLAG_DECRYPTED |
  1157. RX_FLAG_MMIC_STRIPPED;
  1158. }
  1159. }
  1160. skb_put(skb, pkt_len);
  1161. mwl8k_remove_dma_header(skb, qos);
  1162. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  1163. ieee80211_rx_irqsafe(hw, skb);
  1164. processed++;
  1165. }
  1166. return processed;
  1167. }
  1168. /*
  1169. * Packet transmission.
  1170. */
  1171. #define MWL8K_TXD_STATUS_OK 0x00000001
  1172. #define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
  1173. #define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
  1174. #define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
  1175. #define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
  1176. #define MWL8K_QOS_QLEN_UNSPEC 0xff00
  1177. #define MWL8K_QOS_ACK_POLICY_MASK 0x0060
  1178. #define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
  1179. #define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
  1180. #define MWL8K_QOS_EOSP 0x0010
  1181. struct mwl8k_tx_desc {
  1182. __le32 status;
  1183. __u8 data_rate;
  1184. __u8 tx_priority;
  1185. __le16 qos_control;
  1186. __le32 pkt_phys_addr;
  1187. __le16 pkt_len;
  1188. __u8 dest_MAC_addr[ETH_ALEN];
  1189. __le32 next_txd_phys_addr;
  1190. __le32 timestamp;
  1191. __le16 rate_info;
  1192. __u8 peer_id;
  1193. __u8 tx_frag_cnt;
  1194. } __packed;
  1195. #define MWL8K_TX_DESCS 128
  1196. static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
  1197. {
  1198. struct mwl8k_priv *priv = hw->priv;
  1199. struct mwl8k_tx_queue *txq = priv->txq + index;
  1200. int size;
  1201. int i;
  1202. txq->len = 0;
  1203. txq->head = 0;
  1204. txq->tail = 0;
  1205. size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
  1206. txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
  1207. if (txq->txd == NULL) {
  1208. wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
  1209. return -ENOMEM;
  1210. }
  1211. memset(txq->txd, 0, size);
  1212. txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
  1213. if (txq->skb == NULL) {
  1214. pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
  1215. return -ENOMEM;
  1216. }
  1217. for (i = 0; i < MWL8K_TX_DESCS; i++) {
  1218. struct mwl8k_tx_desc *tx_desc;
  1219. int nexti;
  1220. tx_desc = txq->txd + i;
  1221. nexti = (i + 1) % MWL8K_TX_DESCS;
  1222. tx_desc->status = 0;
  1223. tx_desc->next_txd_phys_addr =
  1224. cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
  1225. }
  1226. return 0;
  1227. }
  1228. static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
  1229. {
  1230. iowrite32(MWL8K_H2A_INT_PPA_READY,
  1231. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1232. iowrite32(MWL8K_H2A_INT_DUMMY,
  1233. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1234. ioread32(priv->regs + MWL8K_HIU_INT_CODE);
  1235. }
  1236. static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
  1237. {
  1238. struct mwl8k_priv *priv = hw->priv;
  1239. int i;
  1240. for (i = 0; i < mwl8k_tx_queues(priv); i++) {
  1241. struct mwl8k_tx_queue *txq = priv->txq + i;
  1242. int fw_owned = 0;
  1243. int drv_owned = 0;
  1244. int unused = 0;
  1245. int desc;
  1246. for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
  1247. struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
  1248. u32 status;
  1249. status = le32_to_cpu(tx_desc->status);
  1250. if (status & MWL8K_TXD_STATUS_FW_OWNED)
  1251. fw_owned++;
  1252. else
  1253. drv_owned++;
  1254. if (tx_desc->pkt_len == 0)
  1255. unused++;
  1256. }
  1257. wiphy_err(hw->wiphy,
  1258. "txq[%d] len=%d head=%d tail=%d "
  1259. "fw_owned=%d drv_owned=%d unused=%d\n",
  1260. i,
  1261. txq->len, txq->head, txq->tail,
  1262. fw_owned, drv_owned, unused);
  1263. }
  1264. }
  1265. /*
  1266. * Must be called with priv->fw_mutex held and tx queues stopped.
  1267. */
  1268. #define MWL8K_TX_WAIT_TIMEOUT_MS 5000
  1269. static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
  1270. {
  1271. struct mwl8k_priv *priv = hw->priv;
  1272. DECLARE_COMPLETION_ONSTACK(tx_wait);
  1273. int retry;
  1274. int rc;
  1275. might_sleep();
  1276. /* Since fw restart is in progress, allow only the firmware
  1277. * commands from the restart code and block the other
  1278. * commands since they are going to fail in any case since
  1279. * the firmware has crashed
  1280. */
  1281. if (priv->hw_restart_in_progress) {
  1282. if (priv->hw_restart_owner == current)
  1283. return 0;
  1284. else
  1285. return -EBUSY;
  1286. }
  1287. if (atomic_read(&priv->watchdog_event_pending))
  1288. return 0;
  1289. /*
  1290. * The TX queues are stopped at this point, so this test
  1291. * doesn't need to take ->tx_lock.
  1292. */
  1293. if (!priv->pending_tx_pkts)
  1294. return 0;
  1295. retry = 1;
  1296. rc = 0;
  1297. spin_lock_bh(&priv->tx_lock);
  1298. priv->tx_wait = &tx_wait;
  1299. while (!rc) {
  1300. int oldcount;
  1301. unsigned long timeout;
  1302. oldcount = priv->pending_tx_pkts;
  1303. spin_unlock_bh(&priv->tx_lock);
  1304. timeout = wait_for_completion_timeout(&tx_wait,
  1305. msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
  1306. if (atomic_read(&priv->watchdog_event_pending)) {
  1307. spin_lock_bh(&priv->tx_lock);
  1308. priv->tx_wait = NULL;
  1309. spin_unlock_bh(&priv->tx_lock);
  1310. return 0;
  1311. }
  1312. spin_lock_bh(&priv->tx_lock);
  1313. if (timeout || !priv->pending_tx_pkts) {
  1314. WARN_ON(priv->pending_tx_pkts);
  1315. if (retry)
  1316. wiphy_notice(hw->wiphy, "tx rings drained\n");
  1317. break;
  1318. }
  1319. if (retry) {
  1320. mwl8k_tx_start(priv);
  1321. retry = 0;
  1322. continue;
  1323. }
  1324. if (priv->pending_tx_pkts < oldcount) {
  1325. wiphy_notice(hw->wiphy,
  1326. "waiting for tx rings to drain (%d -> %d pkts)\n",
  1327. oldcount, priv->pending_tx_pkts);
  1328. retry = 1;
  1329. continue;
  1330. }
  1331. priv->tx_wait = NULL;
  1332. wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
  1333. MWL8K_TX_WAIT_TIMEOUT_MS);
  1334. mwl8k_dump_tx_rings(hw);
  1335. priv->hw_restart_in_progress = true;
  1336. ieee80211_queue_work(hw, &priv->fw_reload);
  1337. rc = -ETIMEDOUT;
  1338. }
  1339. priv->tx_wait = NULL;
  1340. spin_unlock_bh(&priv->tx_lock);
  1341. return rc;
  1342. }
  1343. #define MWL8K_TXD_SUCCESS(status) \
  1344. ((status) & (MWL8K_TXD_STATUS_OK | \
  1345. MWL8K_TXD_STATUS_OK_RETRY | \
  1346. MWL8K_TXD_STATUS_OK_MORE_RETRY))
  1347. static int mwl8k_tid_queue_mapping(u8 tid)
  1348. {
  1349. BUG_ON(tid > 7);
  1350. switch (tid) {
  1351. case 0:
  1352. case 3:
  1353. return IEEE80211_AC_BE;
  1354. break;
  1355. case 1:
  1356. case 2:
  1357. return IEEE80211_AC_BK;
  1358. break;
  1359. case 4:
  1360. case 5:
  1361. return IEEE80211_AC_VI;
  1362. break;
  1363. case 6:
  1364. case 7:
  1365. return IEEE80211_AC_VO;
  1366. break;
  1367. default:
  1368. return -1;
  1369. break;
  1370. }
  1371. }
  1372. /* The firmware will fill in the rate information
  1373. * for each packet that gets queued in the hardware
  1374. * and these macros will interpret that info.
  1375. */
  1376. #define RI_FORMAT(a) (a & 0x0001)
  1377. #define RI_RATE_ID_MCS(a) ((a & 0x01f8) >> 3)
  1378. static int
  1379. mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
  1380. {
  1381. struct mwl8k_priv *priv = hw->priv;
  1382. struct mwl8k_tx_queue *txq = priv->txq + index;
  1383. int processed;
  1384. processed = 0;
  1385. while (txq->len > 0 && limit--) {
  1386. int tx;
  1387. struct mwl8k_tx_desc *tx_desc;
  1388. unsigned long addr;
  1389. int size;
  1390. struct sk_buff *skb;
  1391. struct ieee80211_tx_info *info;
  1392. u32 status;
  1393. struct ieee80211_sta *sta;
  1394. struct mwl8k_sta *sta_info = NULL;
  1395. u16 rate_info;
  1396. struct ieee80211_hdr *wh;
  1397. tx = txq->head;
  1398. tx_desc = txq->txd + tx;
  1399. status = le32_to_cpu(tx_desc->status);
  1400. if (status & MWL8K_TXD_STATUS_FW_OWNED) {
  1401. if (!force)
  1402. break;
  1403. tx_desc->status &=
  1404. ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
  1405. }
  1406. txq->head = (tx + 1) % MWL8K_TX_DESCS;
  1407. BUG_ON(txq->len == 0);
  1408. txq->len--;
  1409. priv->pending_tx_pkts--;
  1410. addr = le32_to_cpu(tx_desc->pkt_phys_addr);
  1411. size = le16_to_cpu(tx_desc->pkt_len);
  1412. skb = txq->skb[tx];
  1413. txq->skb[tx] = NULL;
  1414. BUG_ON(skb == NULL);
  1415. pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
  1416. mwl8k_remove_dma_header(skb, tx_desc->qos_control);
  1417. wh = (struct ieee80211_hdr *) skb->data;
  1418. /* Mark descriptor as unused */
  1419. tx_desc->pkt_phys_addr = 0;
  1420. tx_desc->pkt_len = 0;
  1421. info = IEEE80211_SKB_CB(skb);
  1422. if (ieee80211_is_data(wh->frame_control)) {
  1423. rcu_read_lock();
  1424. sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
  1425. wh->addr2);
  1426. if (sta) {
  1427. sta_info = MWL8K_STA(sta);
  1428. BUG_ON(sta_info == NULL);
  1429. rate_info = le16_to_cpu(tx_desc->rate_info);
  1430. /* If rate is < 6.5 Mpbs for an ht station
  1431. * do not form an ampdu. If the station is a
  1432. * legacy station (format = 0), do not form an
  1433. * ampdu
  1434. */
  1435. if (RI_RATE_ID_MCS(rate_info) < 1 ||
  1436. RI_FORMAT(rate_info) == 0) {
  1437. sta_info->is_ampdu_allowed = false;
  1438. } else {
  1439. sta_info->is_ampdu_allowed = true;
  1440. }
  1441. }
  1442. rcu_read_unlock();
  1443. }
  1444. ieee80211_tx_info_clear_status(info);
  1445. /* Rate control is happening in the firmware.
  1446. * Ensure no tx rate is being reported.
  1447. */
  1448. info->status.rates[0].idx = -1;
  1449. info->status.rates[0].count = 1;
  1450. if (MWL8K_TXD_SUCCESS(status))
  1451. info->flags |= IEEE80211_TX_STAT_ACK;
  1452. ieee80211_tx_status_irqsafe(hw, skb);
  1453. processed++;
  1454. }
  1455. return processed;
  1456. }
  1457. /* must be called only when the card's transmit is completely halted */
  1458. static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
  1459. {
  1460. struct mwl8k_priv *priv = hw->priv;
  1461. struct mwl8k_tx_queue *txq = priv->txq + index;
  1462. if (txq->txd == NULL)
  1463. return;
  1464. mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
  1465. kfree(txq->skb);
  1466. txq->skb = NULL;
  1467. pci_free_consistent(priv->pdev,
  1468. MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
  1469. txq->txd, txq->txd_dma);
  1470. txq->txd = NULL;
  1471. }
  1472. /* caller must hold priv->stream_lock when calling the stream functions */
  1473. static struct mwl8k_ampdu_stream *
  1474. mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
  1475. {
  1476. struct mwl8k_ampdu_stream *stream;
  1477. struct mwl8k_priv *priv = hw->priv;
  1478. int i;
  1479. for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
  1480. stream = &priv->ampdu[i];
  1481. if (stream->state == AMPDU_NO_STREAM) {
  1482. stream->sta = sta;
  1483. stream->state = AMPDU_STREAM_NEW;
  1484. stream->tid = tid;
  1485. stream->idx = i;
  1486. wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
  1487. sta->addr, tid);
  1488. return stream;
  1489. }
  1490. }
  1491. return NULL;
  1492. }
  1493. static int
  1494. mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
  1495. {
  1496. int ret;
  1497. /* if the stream has already been started, don't start it again */
  1498. if (stream->state != AMPDU_STREAM_NEW)
  1499. return 0;
  1500. ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
  1501. if (ret)
  1502. wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
  1503. "%d\n", stream->sta->addr, stream->tid, ret);
  1504. else
  1505. wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
  1506. stream->sta->addr, stream->tid);
  1507. return ret;
  1508. }
  1509. static void
  1510. mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
  1511. {
  1512. wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
  1513. stream->tid);
  1514. memset(stream, 0, sizeof(*stream));
  1515. }
  1516. static struct mwl8k_ampdu_stream *
  1517. mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
  1518. {
  1519. struct mwl8k_priv *priv = hw->priv;
  1520. int i;
  1521. for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
  1522. struct mwl8k_ampdu_stream *stream;
  1523. stream = &priv->ampdu[i];
  1524. if (stream->state == AMPDU_NO_STREAM)
  1525. continue;
  1526. if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
  1527. stream->tid == tid)
  1528. return stream;
  1529. }
  1530. return NULL;
  1531. }
  1532. #define MWL8K_AMPDU_PACKET_THRESHOLD 64
  1533. static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
  1534. {
  1535. struct mwl8k_sta *sta_info = MWL8K_STA(sta);
  1536. struct tx_traffic_info *tx_stats;
  1537. BUG_ON(tid >= MWL8K_MAX_TID);
  1538. tx_stats = &sta_info->tx_stats[tid];
  1539. return sta_info->is_ampdu_allowed &&
  1540. tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
  1541. }
  1542. static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
  1543. {
  1544. struct mwl8k_sta *sta_info = MWL8K_STA(sta);
  1545. struct tx_traffic_info *tx_stats;
  1546. BUG_ON(tid >= MWL8K_MAX_TID);
  1547. tx_stats = &sta_info->tx_stats[tid];
  1548. if (tx_stats->start_time == 0)
  1549. tx_stats->start_time = jiffies;
  1550. /* reset the packet count after each second elapses. If the number of
  1551. * packets ever exceeds the ampdu_min_traffic threshold, we will allow
  1552. * an ampdu stream to be started.
  1553. */
  1554. if (jiffies - tx_stats->start_time > HZ) {
  1555. tx_stats->pkts = 0;
  1556. tx_stats->start_time = 0;
  1557. } else
  1558. tx_stats->pkts++;
  1559. }
  1560. /* The hardware ampdu queues start from 5.
  1561. * txpriorities for ampdu queues are
  1562. * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
  1563. * and queue 3 is lowest (queue 4 is reserved)
  1564. */
  1565. #define BA_QUEUE 5
  1566. static void
  1567. mwl8k_txq_xmit(struct ieee80211_hw *hw,
  1568. int index,
  1569. struct ieee80211_sta *sta,
  1570. struct sk_buff *skb)
  1571. {
  1572. struct mwl8k_priv *priv = hw->priv;
  1573. struct ieee80211_tx_info *tx_info;
  1574. struct mwl8k_vif *mwl8k_vif;
  1575. struct ieee80211_hdr *wh;
  1576. struct mwl8k_tx_queue *txq;
  1577. struct mwl8k_tx_desc *tx;
  1578. dma_addr_t dma;
  1579. u32 txstatus;
  1580. u8 txdatarate;
  1581. u16 qos;
  1582. int txpriority;
  1583. u8 tid = 0;
  1584. struct mwl8k_ampdu_stream *stream = NULL;
  1585. bool start_ba_session = false;
  1586. bool mgmtframe = false;
  1587. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
  1588. bool eapol_frame = false;
  1589. wh = (struct ieee80211_hdr *)skb->data;
  1590. if (ieee80211_is_data_qos(wh->frame_control))
  1591. qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
  1592. else
  1593. qos = 0;
  1594. if (skb->protocol == cpu_to_be16(ETH_P_PAE))
  1595. eapol_frame = true;
  1596. if (ieee80211_is_mgmt(wh->frame_control))
  1597. mgmtframe = true;
  1598. if (priv->ap_fw)
  1599. mwl8k_encapsulate_tx_frame(priv, skb);
  1600. else
  1601. mwl8k_add_dma_header(priv, skb, 0, 0);
  1602. wh = &((struct mwl8k_dma_data *)skb->data)->wh;
  1603. tx_info = IEEE80211_SKB_CB(skb);
  1604. mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
  1605. if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1606. wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1607. wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
  1608. mwl8k_vif->seqno += 0x10;
  1609. }
  1610. /* Setup firmware control bit fields for each frame type. */
  1611. txstatus = 0;
  1612. txdatarate = 0;
  1613. if (ieee80211_is_mgmt(wh->frame_control) ||
  1614. ieee80211_is_ctl(wh->frame_control)) {
  1615. txdatarate = 0;
  1616. qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
  1617. } else if (ieee80211_is_data(wh->frame_control)) {
  1618. txdatarate = 1;
  1619. if (is_multicast_ether_addr(wh->addr1))
  1620. txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
  1621. qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
  1622. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
  1623. qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
  1624. else
  1625. qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
  1626. }
  1627. /* Queue ADDBA request in the respective data queue. While setting up
  1628. * the ampdu stream, mac80211 queues further packets for that
  1629. * particular ra/tid pair. However, packets piled up in the hardware
  1630. * for that ra/tid pair will still go out. ADDBA request and the
  1631. * related data packets going out from different queues asynchronously
  1632. * will cause a shift in the receiver window which might result in
  1633. * ampdu packets getting dropped at the receiver after the stream has
  1634. * been setup.
  1635. */
  1636. if (unlikely(ieee80211_is_action(wh->frame_control) &&
  1637. mgmt->u.action.category == WLAN_CATEGORY_BACK &&
  1638. mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
  1639. priv->ap_fw)) {
  1640. u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
  1641. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  1642. index = mwl8k_tid_queue_mapping(tid);
  1643. }
  1644. txpriority = index;
  1645. if (priv->ap_fw && sta && sta->ht_cap.ht_supported && !eapol_frame &&
  1646. ieee80211_is_data_qos(wh->frame_control)) {
  1647. tid = qos & 0xf;
  1648. mwl8k_tx_count_packet(sta, tid);
  1649. spin_lock(&priv->stream_lock);
  1650. stream = mwl8k_lookup_stream(hw, sta->addr, tid);
  1651. if (stream != NULL) {
  1652. if (stream->state == AMPDU_STREAM_ACTIVE) {
  1653. WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
  1654. txpriority = (BA_QUEUE + stream->idx) %
  1655. TOTAL_HW_TX_QUEUES;
  1656. if (stream->idx <= 1)
  1657. index = stream->idx +
  1658. MWL8K_TX_WMM_QUEUES;
  1659. } else if (stream->state == AMPDU_STREAM_NEW) {
  1660. /* We get here if the driver sends us packets
  1661. * after we've initiated a stream, but before
  1662. * our ampdu_action routine has been called
  1663. * with IEEE80211_AMPDU_TX_START to get the SSN
  1664. * for the ADDBA request. So this packet can
  1665. * go out with no risk of sequence number
  1666. * mismatch. No special handling is required.
  1667. */
  1668. } else {
  1669. /* Drop packets that would go out after the
  1670. * ADDBA request was sent but before the ADDBA
  1671. * response is received. If we don't do this,
  1672. * the recipient would probably receive it
  1673. * after the ADDBA request with SSN 0. This
  1674. * will cause the recipient's BA receive window
  1675. * to shift, which would cause the subsequent
  1676. * packets in the BA stream to be discarded.
  1677. * mac80211 queues our packets for us in this
  1678. * case, so this is really just a safety check.
  1679. */
  1680. wiphy_warn(hw->wiphy,
  1681. "Cannot send packet while ADDBA "
  1682. "dialog is underway.\n");
  1683. spin_unlock(&priv->stream_lock);
  1684. dev_kfree_skb(skb);
  1685. return;
  1686. }
  1687. } else {
  1688. /* Defer calling mwl8k_start_stream so that the current
  1689. * skb can go out before the ADDBA request. This
  1690. * prevents sequence number mismatch at the recepient
  1691. * as described above.
  1692. */
  1693. if (mwl8k_ampdu_allowed(sta, tid)) {
  1694. stream = mwl8k_add_stream(hw, sta, tid);
  1695. if (stream != NULL)
  1696. start_ba_session = true;
  1697. }
  1698. }
  1699. spin_unlock(&priv->stream_lock);
  1700. } else {
  1701. qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
  1702. qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
  1703. }
  1704. dma = pci_map_single(priv->pdev, skb->data,
  1705. skb->len, PCI_DMA_TODEVICE);
  1706. if (pci_dma_mapping_error(priv->pdev, dma)) {
  1707. wiphy_debug(hw->wiphy,
  1708. "failed to dma map skb, dropping TX frame.\n");
  1709. if (start_ba_session) {
  1710. spin_lock(&priv->stream_lock);
  1711. mwl8k_remove_stream(hw, stream);
  1712. spin_unlock(&priv->stream_lock);
  1713. }
  1714. dev_kfree_skb(skb);
  1715. return;
  1716. }
  1717. spin_lock_bh(&priv->tx_lock);
  1718. txq = priv->txq + index;
  1719. /* Mgmt frames that go out frequently are probe
  1720. * responses. Other mgmt frames got out relatively
  1721. * infrequently. Hence reserve 2 buffers so that
  1722. * other mgmt frames do not get dropped due to an
  1723. * already queued probe response in one of the
  1724. * reserved buffers.
  1725. */
  1726. if (txq->len >= MWL8K_TX_DESCS - 2) {
  1727. if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
  1728. if (start_ba_session) {
  1729. spin_lock(&priv->stream_lock);
  1730. mwl8k_remove_stream(hw, stream);
  1731. spin_unlock(&priv->stream_lock);
  1732. }
  1733. mwl8k_tx_start(priv);
  1734. spin_unlock_bh(&priv->tx_lock);
  1735. pci_unmap_single(priv->pdev, dma, skb->len,
  1736. PCI_DMA_TODEVICE);
  1737. dev_kfree_skb(skb);
  1738. return;
  1739. }
  1740. }
  1741. BUG_ON(txq->skb[txq->tail] != NULL);
  1742. txq->skb[txq->tail] = skb;
  1743. tx = txq->txd + txq->tail;
  1744. tx->data_rate = txdatarate;
  1745. tx->tx_priority = txpriority;
  1746. tx->qos_control = cpu_to_le16(qos);
  1747. tx->pkt_phys_addr = cpu_to_le32(dma);
  1748. tx->pkt_len = cpu_to_le16(skb->len);
  1749. tx->rate_info = 0;
  1750. if (!priv->ap_fw && sta != NULL)
  1751. tx->peer_id = MWL8K_STA(sta)->peer_id;
  1752. else
  1753. tx->peer_id = 0;
  1754. if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
  1755. tx->timestamp = cpu_to_le32(ioread32(priv->regs +
  1756. MWL8K_HW_TIMER_REGISTER));
  1757. else
  1758. tx->timestamp = 0;
  1759. wmb();
  1760. tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
  1761. txq->len++;
  1762. priv->pending_tx_pkts++;
  1763. txq->tail++;
  1764. if (txq->tail == MWL8K_TX_DESCS)
  1765. txq->tail = 0;
  1766. mwl8k_tx_start(priv);
  1767. spin_unlock_bh(&priv->tx_lock);
  1768. /* Initiate the ampdu session here */
  1769. if (start_ba_session) {
  1770. spin_lock(&priv->stream_lock);
  1771. if (mwl8k_start_stream(hw, stream))
  1772. mwl8k_remove_stream(hw, stream);
  1773. spin_unlock(&priv->stream_lock);
  1774. }
  1775. }
  1776. /*
  1777. * Firmware access.
  1778. *
  1779. * We have the following requirements for issuing firmware commands:
  1780. * - Some commands require that the packet transmit path is idle when
  1781. * the command is issued. (For simplicity, we'll just quiesce the
  1782. * transmit path for every command.)
  1783. * - There are certain sequences of commands that need to be issued to
  1784. * the hardware sequentially, with no other intervening commands.
  1785. *
  1786. * This leads to an implementation of a "firmware lock" as a mutex that
  1787. * can be taken recursively, and which is taken by both the low-level
  1788. * command submission function (mwl8k_post_cmd) as well as any users of
  1789. * that function that require issuing of an atomic sequence of commands,
  1790. * and quiesces the transmit path whenever it's taken.
  1791. */
  1792. static int mwl8k_fw_lock(struct ieee80211_hw *hw)
  1793. {
  1794. struct mwl8k_priv *priv = hw->priv;
  1795. if (priv->fw_mutex_owner != current) {
  1796. int rc;
  1797. mutex_lock(&priv->fw_mutex);
  1798. ieee80211_stop_queues(hw);
  1799. rc = mwl8k_tx_wait_empty(hw);
  1800. if (rc) {
  1801. if (!priv->hw_restart_in_progress)
  1802. ieee80211_wake_queues(hw);
  1803. mutex_unlock(&priv->fw_mutex);
  1804. return rc;
  1805. }
  1806. priv->fw_mutex_owner = current;
  1807. }
  1808. priv->fw_mutex_depth++;
  1809. return 0;
  1810. }
  1811. static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
  1812. {
  1813. struct mwl8k_priv *priv = hw->priv;
  1814. if (!--priv->fw_mutex_depth) {
  1815. if (!priv->hw_restart_in_progress)
  1816. ieee80211_wake_queues(hw);
  1817. priv->fw_mutex_owner = NULL;
  1818. mutex_unlock(&priv->fw_mutex);
  1819. }
  1820. }
  1821. static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable,
  1822. u32 bitmap);
  1823. /*
  1824. * Command processing.
  1825. */
  1826. /* Timeout firmware commands after 10s */
  1827. #define MWL8K_CMD_TIMEOUT_MS 10000
  1828. static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
  1829. {
  1830. DECLARE_COMPLETION_ONSTACK(cmd_wait);
  1831. struct mwl8k_priv *priv = hw->priv;
  1832. void __iomem *regs = priv->regs;
  1833. dma_addr_t dma_addr;
  1834. unsigned int dma_size;
  1835. int rc;
  1836. unsigned long timeout = 0;
  1837. u8 buf[32];
  1838. u32 bitmap = 0;
  1839. wiphy_dbg(hw->wiphy, "Posting %s [%d]\n",
  1840. mwl8k_cmd_name(cmd->code, buf, sizeof(buf)), cmd->macid);
  1841. /* Before posting firmware commands that could change the hardware
  1842. * characteristics, make sure that all BSSes are stopped temporary.
  1843. * Enable these stopped BSSes after completion of the commands
  1844. */
  1845. rc = mwl8k_fw_lock(hw);
  1846. if (rc)
  1847. return rc;
  1848. if (priv->ap_fw && priv->running_bsses) {
  1849. switch (le16_to_cpu(cmd->code)) {
  1850. case MWL8K_CMD_SET_RF_CHANNEL:
  1851. case MWL8K_CMD_RADIO_CONTROL:
  1852. case MWL8K_CMD_RF_TX_POWER:
  1853. case MWL8K_CMD_TX_POWER:
  1854. case MWL8K_CMD_RF_ANTENNA:
  1855. case MWL8K_CMD_RTS_THRESHOLD:
  1856. case MWL8K_CMD_MIMO_CONFIG:
  1857. bitmap = priv->running_bsses;
  1858. mwl8k_enable_bsses(hw, false, bitmap);
  1859. break;
  1860. }
  1861. }
  1862. cmd->result = (__force __le16) 0xffff;
  1863. dma_size = le16_to_cpu(cmd->length);
  1864. dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
  1865. PCI_DMA_BIDIRECTIONAL);
  1866. if (pci_dma_mapping_error(priv->pdev, dma_addr))
  1867. return -ENOMEM;
  1868. priv->hostcmd_wait = &cmd_wait;
  1869. iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
  1870. iowrite32(MWL8K_H2A_INT_DOORBELL,
  1871. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1872. iowrite32(MWL8K_H2A_INT_DUMMY,
  1873. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1874. timeout = wait_for_completion_timeout(&cmd_wait,
  1875. msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
  1876. priv->hostcmd_wait = NULL;
  1877. pci_unmap_single(priv->pdev, dma_addr, dma_size,
  1878. PCI_DMA_BIDIRECTIONAL);
  1879. if (!timeout) {
  1880. wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
  1881. mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
  1882. MWL8K_CMD_TIMEOUT_MS);
  1883. rc = -ETIMEDOUT;
  1884. } else {
  1885. int ms;
  1886. ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
  1887. rc = cmd->result ? -EINVAL : 0;
  1888. if (rc)
  1889. wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
  1890. mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
  1891. le16_to_cpu(cmd->result));
  1892. else if (ms > 2000)
  1893. wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
  1894. mwl8k_cmd_name(cmd->code,
  1895. buf, sizeof(buf)),
  1896. ms);
  1897. }
  1898. if (bitmap)
  1899. mwl8k_enable_bsses(hw, true, bitmap);
  1900. mwl8k_fw_unlock(hw);
  1901. return rc;
  1902. }
  1903. static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
  1904. struct ieee80211_vif *vif,
  1905. struct mwl8k_cmd_pkt *cmd)
  1906. {
  1907. if (vif != NULL)
  1908. cmd->macid = MWL8K_VIF(vif)->macid;
  1909. return mwl8k_post_cmd(hw, cmd);
  1910. }
  1911. /*
  1912. * Setup code shared between STA and AP firmware images.
  1913. */
  1914. static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
  1915. {
  1916. struct mwl8k_priv *priv = hw->priv;
  1917. BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
  1918. memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
  1919. BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
  1920. memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
  1921. priv->band_24.band = IEEE80211_BAND_2GHZ;
  1922. priv->band_24.channels = priv->channels_24;
  1923. priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
  1924. priv->band_24.bitrates = priv->rates_24;
  1925. priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
  1926. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
  1927. }
  1928. static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
  1929. {
  1930. struct mwl8k_priv *priv = hw->priv;
  1931. BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
  1932. memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
  1933. BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
  1934. memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
  1935. priv->band_50.band = IEEE80211_BAND_5GHZ;
  1936. priv->band_50.channels = priv->channels_50;
  1937. priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
  1938. priv->band_50.bitrates = priv->rates_50;
  1939. priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
  1940. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
  1941. }
  1942. /*
  1943. * CMD_GET_HW_SPEC (STA version).
  1944. */
  1945. struct mwl8k_cmd_get_hw_spec_sta {
  1946. struct mwl8k_cmd_pkt header;
  1947. __u8 hw_rev;
  1948. __u8 host_interface;
  1949. __le16 num_mcaddrs;
  1950. __u8 perm_addr[ETH_ALEN];
  1951. __le16 region_code;
  1952. __le32 fw_rev;
  1953. __le32 ps_cookie;
  1954. __le32 caps;
  1955. __u8 mcs_bitmap[16];
  1956. __le32 rx_queue_ptr;
  1957. __le32 num_tx_queues;
  1958. __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
  1959. __le32 caps2;
  1960. __le32 num_tx_desc_per_queue;
  1961. __le32 total_rxd;
  1962. } __packed;
  1963. #define MWL8K_CAP_MAX_AMSDU 0x20000000
  1964. #define MWL8K_CAP_GREENFIELD 0x08000000
  1965. #define MWL8K_CAP_AMPDU 0x04000000
  1966. #define MWL8K_CAP_RX_STBC 0x01000000
  1967. #define MWL8K_CAP_TX_STBC 0x00800000
  1968. #define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
  1969. #define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
  1970. #define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
  1971. #define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
  1972. #define MWL8K_CAP_DELAY_BA 0x00003000
  1973. #define MWL8K_CAP_MIMO 0x00000200
  1974. #define MWL8K_CAP_40MHZ 0x00000100
  1975. #define MWL8K_CAP_BAND_MASK 0x00000007
  1976. #define MWL8K_CAP_5GHZ 0x00000004
  1977. #define MWL8K_CAP_2GHZ4 0x00000001
  1978. static void
  1979. mwl8k_set_ht_caps(struct ieee80211_hw *hw,
  1980. struct ieee80211_supported_band *band, u32 cap)
  1981. {
  1982. int rx_streams;
  1983. int tx_streams;
  1984. band->ht_cap.ht_supported = 1;
  1985. if (cap & MWL8K_CAP_MAX_AMSDU)
  1986. band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  1987. if (cap & MWL8K_CAP_GREENFIELD)
  1988. band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
  1989. if (cap & MWL8K_CAP_AMPDU) {
  1990. hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
  1991. band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  1992. band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
  1993. }
  1994. if (cap & MWL8K_CAP_RX_STBC)
  1995. band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
  1996. if (cap & MWL8K_CAP_TX_STBC)
  1997. band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
  1998. if (cap & MWL8K_CAP_SHORTGI_40MHZ)
  1999. band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
  2000. if (cap & MWL8K_CAP_SHORTGI_20MHZ)
  2001. band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
  2002. if (cap & MWL8K_CAP_DELAY_BA)
  2003. band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
  2004. if (cap & MWL8K_CAP_40MHZ)
  2005. band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  2006. rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
  2007. tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
  2008. band->ht_cap.mcs.rx_mask[0] = 0xff;
  2009. if (rx_streams >= 2)
  2010. band->ht_cap.mcs.rx_mask[1] = 0xff;
  2011. if (rx_streams >= 3)
  2012. band->ht_cap.mcs.rx_mask[2] = 0xff;
  2013. band->ht_cap.mcs.rx_mask[4] = 0x01;
  2014. band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  2015. if (rx_streams != tx_streams) {
  2016. band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  2017. band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
  2018. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  2019. }
  2020. }
  2021. static void
  2022. mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
  2023. {
  2024. struct mwl8k_priv *priv = hw->priv;
  2025. if (priv->caps)
  2026. return;
  2027. if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
  2028. mwl8k_setup_2ghz_band(hw);
  2029. if (caps & MWL8K_CAP_MIMO)
  2030. mwl8k_set_ht_caps(hw, &priv->band_24, caps);
  2031. }
  2032. if (caps & MWL8K_CAP_5GHZ) {
  2033. mwl8k_setup_5ghz_band(hw);
  2034. if (caps & MWL8K_CAP_MIMO)
  2035. mwl8k_set_ht_caps(hw, &priv->band_50, caps);
  2036. }
  2037. priv->caps = caps;
  2038. }
  2039. static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
  2040. {
  2041. struct mwl8k_priv *priv = hw->priv;
  2042. struct mwl8k_cmd_get_hw_spec_sta *cmd;
  2043. int rc;
  2044. int i;
  2045. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2046. if (cmd == NULL)
  2047. return -ENOMEM;
  2048. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
  2049. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2050. memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
  2051. cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
  2052. cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
  2053. cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
  2054. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  2055. cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
  2056. cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
  2057. cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
  2058. rc = mwl8k_post_cmd(hw, &cmd->header);
  2059. if (!rc) {
  2060. SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
  2061. priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
  2062. priv->fw_rev = le32_to_cpu(cmd->fw_rev);
  2063. priv->hw_rev = cmd->hw_rev;
  2064. mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
  2065. priv->ap_macids_supported = 0x00000000;
  2066. priv->sta_macids_supported = 0x00000001;
  2067. }
  2068. kfree(cmd);
  2069. return rc;
  2070. }
  2071. /*
  2072. * CMD_GET_HW_SPEC (AP version).
  2073. */
  2074. struct mwl8k_cmd_get_hw_spec_ap {
  2075. struct mwl8k_cmd_pkt header;
  2076. __u8 hw_rev;
  2077. __u8 host_interface;
  2078. __le16 num_wcb;
  2079. __le16 num_mcaddrs;
  2080. __u8 perm_addr[ETH_ALEN];
  2081. __le16 region_code;
  2082. __le16 num_antenna;
  2083. __le32 fw_rev;
  2084. __le32 wcbbase0;
  2085. __le32 rxwrptr;
  2086. __le32 rxrdptr;
  2087. __le32 ps_cookie;
  2088. __le32 wcbbase1;
  2089. __le32 wcbbase2;
  2090. __le32 wcbbase3;
  2091. __le32 fw_api_version;
  2092. __le32 caps;
  2093. __le32 num_of_ampdu_queues;
  2094. __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
  2095. } __packed;
  2096. static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
  2097. {
  2098. struct mwl8k_priv *priv = hw->priv;
  2099. struct mwl8k_cmd_get_hw_spec_ap *cmd;
  2100. int rc, i;
  2101. u32 api_version;
  2102. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2103. if (cmd == NULL)
  2104. return -ENOMEM;
  2105. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
  2106. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2107. memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
  2108. cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
  2109. rc = mwl8k_post_cmd(hw, &cmd->header);
  2110. if (!rc) {
  2111. int off;
  2112. api_version = le32_to_cpu(cmd->fw_api_version);
  2113. if (priv->device_info->fw_api_ap != api_version) {
  2114. printk(KERN_ERR "%s: Unsupported fw API version for %s."
  2115. " Expected %d got %d.\n", MWL8K_NAME,
  2116. priv->device_info->part_name,
  2117. priv->device_info->fw_api_ap,
  2118. api_version);
  2119. rc = -EINVAL;
  2120. goto done;
  2121. }
  2122. SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
  2123. priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
  2124. priv->fw_rev = le32_to_cpu(cmd->fw_rev);
  2125. priv->hw_rev = cmd->hw_rev;
  2126. mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
  2127. priv->ap_macids_supported = 0x000000ff;
  2128. priv->sta_macids_supported = 0x00000100;
  2129. priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
  2130. if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
  2131. wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
  2132. " but we only support %d.\n",
  2133. priv->num_ampdu_queues,
  2134. MWL8K_MAX_AMPDU_QUEUES);
  2135. priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
  2136. }
  2137. off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
  2138. iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
  2139. off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
  2140. iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
  2141. priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
  2142. priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
  2143. priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
  2144. priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
  2145. for (i = 0; i < priv->num_ampdu_queues; i++)
  2146. priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
  2147. le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
  2148. }
  2149. done:
  2150. kfree(cmd);
  2151. return rc;
  2152. }
  2153. /*
  2154. * CMD_SET_HW_SPEC.
  2155. */
  2156. struct mwl8k_cmd_set_hw_spec {
  2157. struct mwl8k_cmd_pkt header;
  2158. __u8 hw_rev;
  2159. __u8 host_interface;
  2160. __le16 num_mcaddrs;
  2161. __u8 perm_addr[ETH_ALEN];
  2162. __le16 region_code;
  2163. __le32 fw_rev;
  2164. __le32 ps_cookie;
  2165. __le32 caps;
  2166. __le32 rx_queue_ptr;
  2167. __le32 num_tx_queues;
  2168. __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
  2169. __le32 flags;
  2170. __le32 num_tx_desc_per_queue;
  2171. __le32 total_rxd;
  2172. } __packed;
  2173. /* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
  2174. * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
  2175. * the packets that are queued for more than 500ms, will be dropped in the
  2176. * hardware. This helps minimizing the issues caused due to head-of-line
  2177. * blocking where a slow client can hog the bandwidth and affect traffic to a
  2178. * faster client.
  2179. */
  2180. #define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
  2181. #define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR 0x00000200
  2182. #define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
  2183. #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
  2184. #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
  2185. static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
  2186. {
  2187. struct mwl8k_priv *priv = hw->priv;
  2188. struct mwl8k_cmd_set_hw_spec *cmd;
  2189. int rc;
  2190. int i;
  2191. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2192. if (cmd == NULL)
  2193. return -ENOMEM;
  2194. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
  2195. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2196. cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
  2197. cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
  2198. cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
  2199. /*
  2200. * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
  2201. * that order. Firmware has Q3 as highest priority and Q0 as lowest
  2202. * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
  2203. * priority is interpreted the right way in firmware.
  2204. */
  2205. for (i = 0; i < mwl8k_tx_queues(priv); i++) {
  2206. int j = mwl8k_tx_queues(priv) - 1 - i;
  2207. cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
  2208. }
  2209. cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
  2210. MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
  2211. MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
  2212. MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
  2213. MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
  2214. cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
  2215. cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
  2216. rc = mwl8k_post_cmd(hw, &cmd->header);
  2217. kfree(cmd);
  2218. return rc;
  2219. }
  2220. /*
  2221. * CMD_MAC_MULTICAST_ADR.
  2222. */
  2223. struct mwl8k_cmd_mac_multicast_adr {
  2224. struct mwl8k_cmd_pkt header;
  2225. __le16 action;
  2226. __le16 numaddr;
  2227. __u8 addr[0][ETH_ALEN];
  2228. };
  2229. #define MWL8K_ENABLE_RX_DIRECTED 0x0001
  2230. #define MWL8K_ENABLE_RX_MULTICAST 0x0002
  2231. #define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
  2232. #define MWL8K_ENABLE_RX_BROADCAST 0x0008
  2233. static struct mwl8k_cmd_pkt *
  2234. __mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
  2235. struct netdev_hw_addr_list *mc_list)
  2236. {
  2237. struct mwl8k_priv *priv = hw->priv;
  2238. struct mwl8k_cmd_mac_multicast_adr *cmd;
  2239. int size;
  2240. int mc_count = 0;
  2241. if (mc_list)
  2242. mc_count = netdev_hw_addr_list_count(mc_list);
  2243. if (allmulti || mc_count > priv->num_mcaddrs) {
  2244. allmulti = 1;
  2245. mc_count = 0;
  2246. }
  2247. size = sizeof(*cmd) + mc_count * ETH_ALEN;
  2248. cmd = kzalloc(size, GFP_ATOMIC);
  2249. if (cmd == NULL)
  2250. return NULL;
  2251. cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
  2252. cmd->header.length = cpu_to_le16(size);
  2253. cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
  2254. MWL8K_ENABLE_RX_BROADCAST);
  2255. if (allmulti) {
  2256. cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
  2257. } else if (mc_count) {
  2258. struct netdev_hw_addr *ha;
  2259. int i = 0;
  2260. cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
  2261. cmd->numaddr = cpu_to_le16(mc_count);
  2262. netdev_hw_addr_list_for_each(ha, mc_list) {
  2263. memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
  2264. }
  2265. }
  2266. return &cmd->header;
  2267. }
  2268. /*
  2269. * CMD_GET_STAT.
  2270. */
  2271. struct mwl8k_cmd_get_stat {
  2272. struct mwl8k_cmd_pkt header;
  2273. __le32 stats[64];
  2274. } __packed;
  2275. #define MWL8K_STAT_ACK_FAILURE 9
  2276. #define MWL8K_STAT_RTS_FAILURE 12
  2277. #define MWL8K_STAT_FCS_ERROR 24
  2278. #define MWL8K_STAT_RTS_SUCCESS 11
  2279. static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
  2280. struct ieee80211_low_level_stats *stats)
  2281. {
  2282. struct mwl8k_cmd_get_stat *cmd;
  2283. int rc;
  2284. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2285. if (cmd == NULL)
  2286. return -ENOMEM;
  2287. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
  2288. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2289. rc = mwl8k_post_cmd(hw, &cmd->header);
  2290. if (!rc) {
  2291. stats->dot11ACKFailureCount =
  2292. le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
  2293. stats->dot11RTSFailureCount =
  2294. le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
  2295. stats->dot11FCSErrorCount =
  2296. le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
  2297. stats->dot11RTSSuccessCount =
  2298. le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
  2299. }
  2300. kfree(cmd);
  2301. return rc;
  2302. }
  2303. /*
  2304. * CMD_RADIO_CONTROL.
  2305. */
  2306. struct mwl8k_cmd_radio_control {
  2307. struct mwl8k_cmd_pkt header;
  2308. __le16 action;
  2309. __le16 control;
  2310. __le16 radio_on;
  2311. } __packed;
  2312. static int
  2313. mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
  2314. {
  2315. struct mwl8k_priv *priv = hw->priv;
  2316. struct mwl8k_cmd_radio_control *cmd;
  2317. int rc;
  2318. if (enable == priv->radio_on && !force)
  2319. return 0;
  2320. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2321. if (cmd == NULL)
  2322. return -ENOMEM;
  2323. cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
  2324. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2325. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2326. cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
  2327. cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
  2328. rc = mwl8k_post_cmd(hw, &cmd->header);
  2329. kfree(cmd);
  2330. if (!rc)
  2331. priv->radio_on = enable;
  2332. return rc;
  2333. }
  2334. static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
  2335. {
  2336. return mwl8k_cmd_radio_control(hw, 0, 0);
  2337. }
  2338. static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
  2339. {
  2340. return mwl8k_cmd_radio_control(hw, 1, 0);
  2341. }
  2342. static int
  2343. mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
  2344. {
  2345. struct mwl8k_priv *priv = hw->priv;
  2346. priv->radio_short_preamble = short_preamble;
  2347. return mwl8k_cmd_radio_control(hw, 1, 1);
  2348. }
  2349. /*
  2350. * CMD_RF_TX_POWER.
  2351. */
  2352. #define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
  2353. struct mwl8k_cmd_rf_tx_power {
  2354. struct mwl8k_cmd_pkt header;
  2355. __le16 action;
  2356. __le16 support_level;
  2357. __le16 current_level;
  2358. __le16 reserved;
  2359. __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
  2360. } __packed;
  2361. static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
  2362. {
  2363. struct mwl8k_cmd_rf_tx_power *cmd;
  2364. int rc;
  2365. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2366. if (cmd == NULL)
  2367. return -ENOMEM;
  2368. cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
  2369. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2370. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2371. cmd->support_level = cpu_to_le16(dBm);
  2372. rc = mwl8k_post_cmd(hw, &cmd->header);
  2373. kfree(cmd);
  2374. return rc;
  2375. }
  2376. /*
  2377. * CMD_TX_POWER.
  2378. */
  2379. #define MWL8K_TX_POWER_LEVEL_TOTAL 12
  2380. struct mwl8k_cmd_tx_power {
  2381. struct mwl8k_cmd_pkt header;
  2382. __le16 action;
  2383. __le16 band;
  2384. __le16 channel;
  2385. __le16 bw;
  2386. __le16 sub_ch;
  2387. __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
  2388. } __packed;
  2389. static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
  2390. struct ieee80211_conf *conf,
  2391. unsigned short pwr)
  2392. {
  2393. struct ieee80211_channel *channel = conf->chandef.chan;
  2394. enum nl80211_channel_type channel_type =
  2395. cfg80211_get_chandef_type(&conf->chandef);
  2396. struct mwl8k_cmd_tx_power *cmd;
  2397. int rc;
  2398. int i;
  2399. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2400. if (cmd == NULL)
  2401. return -ENOMEM;
  2402. cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
  2403. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2404. cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
  2405. if (channel->band == IEEE80211_BAND_2GHZ)
  2406. cmd->band = cpu_to_le16(0x1);
  2407. else if (channel->band == IEEE80211_BAND_5GHZ)
  2408. cmd->band = cpu_to_le16(0x4);
  2409. cmd->channel = cpu_to_le16(channel->hw_value);
  2410. if (channel_type == NL80211_CHAN_NO_HT ||
  2411. channel_type == NL80211_CHAN_HT20) {
  2412. cmd->bw = cpu_to_le16(0x2);
  2413. } else {
  2414. cmd->bw = cpu_to_le16(0x4);
  2415. if (channel_type == NL80211_CHAN_HT40MINUS)
  2416. cmd->sub_ch = cpu_to_le16(0x3);
  2417. else if (channel_type == NL80211_CHAN_HT40PLUS)
  2418. cmd->sub_ch = cpu_to_le16(0x1);
  2419. }
  2420. for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
  2421. cmd->power_level_list[i] = cpu_to_le16(pwr);
  2422. rc = mwl8k_post_cmd(hw, &cmd->header);
  2423. kfree(cmd);
  2424. return rc;
  2425. }
  2426. /*
  2427. * CMD_RF_ANTENNA.
  2428. */
  2429. struct mwl8k_cmd_rf_antenna {
  2430. struct mwl8k_cmd_pkt header;
  2431. __le16 antenna;
  2432. __le16 mode;
  2433. } __packed;
  2434. #define MWL8K_RF_ANTENNA_RX 1
  2435. #define MWL8K_RF_ANTENNA_TX 2
  2436. static int
  2437. mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
  2438. {
  2439. struct mwl8k_cmd_rf_antenna *cmd;
  2440. int rc;
  2441. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2442. if (cmd == NULL)
  2443. return -ENOMEM;
  2444. cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
  2445. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2446. cmd->antenna = cpu_to_le16(antenna);
  2447. cmd->mode = cpu_to_le16(mask);
  2448. rc = mwl8k_post_cmd(hw, &cmd->header);
  2449. kfree(cmd);
  2450. return rc;
  2451. }
  2452. /*
  2453. * CMD_SET_BEACON.
  2454. */
  2455. struct mwl8k_cmd_set_beacon {
  2456. struct mwl8k_cmd_pkt header;
  2457. __le16 beacon_len;
  2458. __u8 beacon[0];
  2459. };
  2460. static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
  2461. struct ieee80211_vif *vif, u8 *beacon, int len)
  2462. {
  2463. struct mwl8k_cmd_set_beacon *cmd;
  2464. int rc;
  2465. cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
  2466. if (cmd == NULL)
  2467. return -ENOMEM;
  2468. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
  2469. cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
  2470. cmd->beacon_len = cpu_to_le16(len);
  2471. memcpy(cmd->beacon, beacon, len);
  2472. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  2473. kfree(cmd);
  2474. return rc;
  2475. }
  2476. /*
  2477. * CMD_SET_PRE_SCAN.
  2478. */
  2479. struct mwl8k_cmd_set_pre_scan {
  2480. struct mwl8k_cmd_pkt header;
  2481. } __packed;
  2482. static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
  2483. {
  2484. struct mwl8k_cmd_set_pre_scan *cmd;
  2485. int rc;
  2486. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2487. if (cmd == NULL)
  2488. return -ENOMEM;
  2489. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
  2490. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2491. rc = mwl8k_post_cmd(hw, &cmd->header);
  2492. kfree(cmd);
  2493. return rc;
  2494. }
  2495. /*
  2496. * CMD_SET_POST_SCAN.
  2497. */
  2498. struct mwl8k_cmd_set_post_scan {
  2499. struct mwl8k_cmd_pkt header;
  2500. __le32 isibss;
  2501. __u8 bssid[ETH_ALEN];
  2502. } __packed;
  2503. static int
  2504. mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
  2505. {
  2506. struct mwl8k_cmd_set_post_scan *cmd;
  2507. int rc;
  2508. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2509. if (cmd == NULL)
  2510. return -ENOMEM;
  2511. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
  2512. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2513. cmd->isibss = 0;
  2514. memcpy(cmd->bssid, mac, ETH_ALEN);
  2515. rc = mwl8k_post_cmd(hw, &cmd->header);
  2516. kfree(cmd);
  2517. return rc;
  2518. }
  2519. /*
  2520. * CMD_SET_RF_CHANNEL.
  2521. */
  2522. struct mwl8k_cmd_set_rf_channel {
  2523. struct mwl8k_cmd_pkt header;
  2524. __le16 action;
  2525. __u8 current_channel;
  2526. __le32 channel_flags;
  2527. } __packed;
  2528. static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
  2529. struct ieee80211_conf *conf)
  2530. {
  2531. struct ieee80211_channel *channel = conf->chandef.chan;
  2532. enum nl80211_channel_type channel_type =
  2533. cfg80211_get_chandef_type(&conf->chandef);
  2534. struct mwl8k_cmd_set_rf_channel *cmd;
  2535. int rc;
  2536. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2537. if (cmd == NULL)
  2538. return -ENOMEM;
  2539. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
  2540. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2541. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2542. cmd->current_channel = channel->hw_value;
  2543. if (channel->band == IEEE80211_BAND_2GHZ)
  2544. cmd->channel_flags |= cpu_to_le32(0x00000001);
  2545. else if (channel->band == IEEE80211_BAND_5GHZ)
  2546. cmd->channel_flags |= cpu_to_le32(0x00000004);
  2547. if (channel_type == NL80211_CHAN_NO_HT ||
  2548. channel_type == NL80211_CHAN_HT20)
  2549. cmd->channel_flags |= cpu_to_le32(0x00000080);
  2550. else if (channel_type == NL80211_CHAN_HT40MINUS)
  2551. cmd->channel_flags |= cpu_to_le32(0x000001900);
  2552. else if (channel_type == NL80211_CHAN_HT40PLUS)
  2553. cmd->channel_flags |= cpu_to_le32(0x000000900);
  2554. rc = mwl8k_post_cmd(hw, &cmd->header);
  2555. kfree(cmd);
  2556. return rc;
  2557. }
  2558. /*
  2559. * CMD_SET_AID.
  2560. */
  2561. #define MWL8K_FRAME_PROT_DISABLED 0x00
  2562. #define MWL8K_FRAME_PROT_11G 0x07
  2563. #define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
  2564. #define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
  2565. struct mwl8k_cmd_update_set_aid {
  2566. struct mwl8k_cmd_pkt header;
  2567. __le16 aid;
  2568. /* AP's MAC address (BSSID) */
  2569. __u8 bssid[ETH_ALEN];
  2570. __le16 protection_mode;
  2571. __u8 supp_rates[14];
  2572. } __packed;
  2573. static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
  2574. {
  2575. int i;
  2576. int j;
  2577. /*
  2578. * Clear nonstandard rate 4.
  2579. */
  2580. mask &= 0x1fef;
  2581. for (i = 0, j = 0; i < 13; i++) {
  2582. if (mask & (1 << i))
  2583. rates[j++] = mwl8k_rates_24[i].hw_value;
  2584. }
  2585. }
  2586. static int
  2587. mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
  2588. struct ieee80211_vif *vif, u32 legacy_rate_mask)
  2589. {
  2590. struct mwl8k_cmd_update_set_aid *cmd;
  2591. u16 prot_mode;
  2592. int rc;
  2593. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2594. if (cmd == NULL)
  2595. return -ENOMEM;
  2596. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
  2597. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2598. cmd->aid = cpu_to_le16(vif->bss_conf.aid);
  2599. memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
  2600. if (vif->bss_conf.use_cts_prot) {
  2601. prot_mode = MWL8K_FRAME_PROT_11G;
  2602. } else {
  2603. switch (vif->bss_conf.ht_operation_mode &
  2604. IEEE80211_HT_OP_MODE_PROTECTION) {
  2605. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  2606. prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
  2607. break;
  2608. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  2609. prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
  2610. break;
  2611. default:
  2612. prot_mode = MWL8K_FRAME_PROT_DISABLED;
  2613. break;
  2614. }
  2615. }
  2616. cmd->protection_mode = cpu_to_le16(prot_mode);
  2617. legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
  2618. rc = mwl8k_post_cmd(hw, &cmd->header);
  2619. kfree(cmd);
  2620. return rc;
  2621. }
  2622. /*
  2623. * CMD_SET_RATE.
  2624. */
  2625. struct mwl8k_cmd_set_rate {
  2626. struct mwl8k_cmd_pkt header;
  2627. __u8 legacy_rates[14];
  2628. /* Bitmap for supported MCS codes. */
  2629. __u8 mcs_set[16];
  2630. __u8 reserved[16];
  2631. } __packed;
  2632. static int
  2633. mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2634. u32 legacy_rate_mask, u8 *mcs_rates)
  2635. {
  2636. struct mwl8k_cmd_set_rate *cmd;
  2637. int rc;
  2638. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2639. if (cmd == NULL)
  2640. return -ENOMEM;
  2641. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
  2642. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2643. legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
  2644. memcpy(cmd->mcs_set, mcs_rates, 16);
  2645. rc = mwl8k_post_cmd(hw, &cmd->header);
  2646. kfree(cmd);
  2647. return rc;
  2648. }
  2649. /*
  2650. * CMD_FINALIZE_JOIN.
  2651. */
  2652. #define MWL8K_FJ_BEACON_MAXLEN 128
  2653. struct mwl8k_cmd_finalize_join {
  2654. struct mwl8k_cmd_pkt header;
  2655. __le32 sleep_interval; /* Number of beacon periods to sleep */
  2656. __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
  2657. } __packed;
  2658. static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
  2659. int framelen, int dtim)
  2660. {
  2661. struct mwl8k_cmd_finalize_join *cmd;
  2662. struct ieee80211_mgmt *payload = frame;
  2663. int payload_len;
  2664. int rc;
  2665. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2666. if (cmd == NULL)
  2667. return -ENOMEM;
  2668. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
  2669. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2670. cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
  2671. payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
  2672. if (payload_len < 0)
  2673. payload_len = 0;
  2674. else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
  2675. payload_len = MWL8K_FJ_BEACON_MAXLEN;
  2676. memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
  2677. rc = mwl8k_post_cmd(hw, &cmd->header);
  2678. kfree(cmd);
  2679. return rc;
  2680. }
  2681. /*
  2682. * CMD_SET_RTS_THRESHOLD.
  2683. */
  2684. struct mwl8k_cmd_set_rts_threshold {
  2685. struct mwl8k_cmd_pkt header;
  2686. __le16 action;
  2687. __le16 threshold;
  2688. } __packed;
  2689. static int
  2690. mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
  2691. {
  2692. struct mwl8k_cmd_set_rts_threshold *cmd;
  2693. int rc;
  2694. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2695. if (cmd == NULL)
  2696. return -ENOMEM;
  2697. cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
  2698. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2699. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2700. cmd->threshold = cpu_to_le16(rts_thresh);
  2701. rc = mwl8k_post_cmd(hw, &cmd->header);
  2702. kfree(cmd);
  2703. return rc;
  2704. }
  2705. /*
  2706. * CMD_SET_SLOT.
  2707. */
  2708. struct mwl8k_cmd_set_slot {
  2709. struct mwl8k_cmd_pkt header;
  2710. __le16 action;
  2711. __u8 short_slot;
  2712. } __packed;
  2713. static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
  2714. {
  2715. struct mwl8k_cmd_set_slot *cmd;
  2716. int rc;
  2717. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2718. if (cmd == NULL)
  2719. return -ENOMEM;
  2720. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
  2721. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2722. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2723. cmd->short_slot = short_slot_time;
  2724. rc = mwl8k_post_cmd(hw, &cmd->header);
  2725. kfree(cmd);
  2726. return rc;
  2727. }
  2728. /*
  2729. * CMD_SET_EDCA_PARAMS.
  2730. */
  2731. struct mwl8k_cmd_set_edca_params {
  2732. struct mwl8k_cmd_pkt header;
  2733. /* See MWL8K_SET_EDCA_XXX below */
  2734. __le16 action;
  2735. /* TX opportunity in units of 32 us */
  2736. __le16 txop;
  2737. union {
  2738. struct {
  2739. /* Log exponent of max contention period: 0...15 */
  2740. __le32 log_cw_max;
  2741. /* Log exponent of min contention period: 0...15 */
  2742. __le32 log_cw_min;
  2743. /* Adaptive interframe spacing in units of 32us */
  2744. __u8 aifs;
  2745. /* TX queue to configure */
  2746. __u8 txq;
  2747. } ap;
  2748. struct {
  2749. /* Log exponent of max contention period: 0...15 */
  2750. __u8 log_cw_max;
  2751. /* Log exponent of min contention period: 0...15 */
  2752. __u8 log_cw_min;
  2753. /* Adaptive interframe spacing in units of 32us */
  2754. __u8 aifs;
  2755. /* TX queue to configure */
  2756. __u8 txq;
  2757. } sta;
  2758. };
  2759. } __packed;
  2760. #define MWL8K_SET_EDCA_CW 0x01
  2761. #define MWL8K_SET_EDCA_TXOP 0x02
  2762. #define MWL8K_SET_EDCA_AIFS 0x04
  2763. #define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
  2764. MWL8K_SET_EDCA_TXOP | \
  2765. MWL8K_SET_EDCA_AIFS)
  2766. static int
  2767. mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
  2768. __u16 cw_min, __u16 cw_max,
  2769. __u8 aifs, __u16 txop)
  2770. {
  2771. struct mwl8k_priv *priv = hw->priv;
  2772. struct mwl8k_cmd_set_edca_params *cmd;
  2773. int rc;
  2774. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2775. if (cmd == NULL)
  2776. return -ENOMEM;
  2777. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
  2778. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2779. cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
  2780. cmd->txop = cpu_to_le16(txop);
  2781. if (priv->ap_fw) {
  2782. cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
  2783. cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
  2784. cmd->ap.aifs = aifs;
  2785. cmd->ap.txq = qnum;
  2786. } else {
  2787. cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
  2788. cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
  2789. cmd->sta.aifs = aifs;
  2790. cmd->sta.txq = qnum;
  2791. }
  2792. rc = mwl8k_post_cmd(hw, &cmd->header);
  2793. kfree(cmd);
  2794. return rc;
  2795. }
  2796. /*
  2797. * CMD_SET_WMM_MODE.
  2798. */
  2799. struct mwl8k_cmd_set_wmm_mode {
  2800. struct mwl8k_cmd_pkt header;
  2801. __le16 action;
  2802. } __packed;
  2803. static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
  2804. {
  2805. struct mwl8k_priv *priv = hw->priv;
  2806. struct mwl8k_cmd_set_wmm_mode *cmd;
  2807. int rc;
  2808. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2809. if (cmd == NULL)
  2810. return -ENOMEM;
  2811. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
  2812. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2813. cmd->action = cpu_to_le16(!!enable);
  2814. rc = mwl8k_post_cmd(hw, &cmd->header);
  2815. kfree(cmd);
  2816. if (!rc)
  2817. priv->wmm_enabled = enable;
  2818. return rc;
  2819. }
  2820. /*
  2821. * CMD_MIMO_CONFIG.
  2822. */
  2823. struct mwl8k_cmd_mimo_config {
  2824. struct mwl8k_cmd_pkt header;
  2825. __le32 action;
  2826. __u8 rx_antenna_map;
  2827. __u8 tx_antenna_map;
  2828. } __packed;
  2829. static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
  2830. {
  2831. struct mwl8k_cmd_mimo_config *cmd;
  2832. int rc;
  2833. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2834. if (cmd == NULL)
  2835. return -ENOMEM;
  2836. cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
  2837. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2838. cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
  2839. cmd->rx_antenna_map = rx;
  2840. cmd->tx_antenna_map = tx;
  2841. rc = mwl8k_post_cmd(hw, &cmd->header);
  2842. kfree(cmd);
  2843. return rc;
  2844. }
  2845. /*
  2846. * CMD_USE_FIXED_RATE (STA version).
  2847. */
  2848. struct mwl8k_cmd_use_fixed_rate_sta {
  2849. struct mwl8k_cmd_pkt header;
  2850. __le32 action;
  2851. __le32 allow_rate_drop;
  2852. __le32 num_rates;
  2853. struct {
  2854. __le32 is_ht_rate;
  2855. __le32 enable_retry;
  2856. __le32 rate;
  2857. __le32 retry_count;
  2858. } rate_entry[8];
  2859. __le32 rate_type;
  2860. __le32 reserved1;
  2861. __le32 reserved2;
  2862. } __packed;
  2863. #define MWL8K_USE_AUTO_RATE 0x0002
  2864. #define MWL8K_UCAST_RATE 0
  2865. static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
  2866. {
  2867. struct mwl8k_cmd_use_fixed_rate_sta *cmd;
  2868. int rc;
  2869. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2870. if (cmd == NULL)
  2871. return -ENOMEM;
  2872. cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
  2873. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2874. cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
  2875. cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
  2876. rc = mwl8k_post_cmd(hw, &cmd->header);
  2877. kfree(cmd);
  2878. return rc;
  2879. }
  2880. /*
  2881. * CMD_USE_FIXED_RATE (AP version).
  2882. */
  2883. struct mwl8k_cmd_use_fixed_rate_ap {
  2884. struct mwl8k_cmd_pkt header;
  2885. __le32 action;
  2886. __le32 allow_rate_drop;
  2887. __le32 num_rates;
  2888. struct mwl8k_rate_entry_ap {
  2889. __le32 is_ht_rate;
  2890. __le32 enable_retry;
  2891. __le32 rate;
  2892. __le32 retry_count;
  2893. } rate_entry[4];
  2894. u8 multicast_rate;
  2895. u8 multicast_rate_type;
  2896. u8 management_rate;
  2897. } __packed;
  2898. static int
  2899. mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
  2900. {
  2901. struct mwl8k_cmd_use_fixed_rate_ap *cmd;
  2902. int rc;
  2903. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2904. if (cmd == NULL)
  2905. return -ENOMEM;
  2906. cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
  2907. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2908. cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
  2909. cmd->multicast_rate = mcast;
  2910. cmd->management_rate = mgmt;
  2911. rc = mwl8k_post_cmd(hw, &cmd->header);
  2912. kfree(cmd);
  2913. return rc;
  2914. }
  2915. /*
  2916. * CMD_ENABLE_SNIFFER.
  2917. */
  2918. struct mwl8k_cmd_enable_sniffer {
  2919. struct mwl8k_cmd_pkt header;
  2920. __le32 action;
  2921. } __packed;
  2922. static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
  2923. {
  2924. struct mwl8k_cmd_enable_sniffer *cmd;
  2925. int rc;
  2926. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2927. if (cmd == NULL)
  2928. return -ENOMEM;
  2929. cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
  2930. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2931. cmd->action = cpu_to_le32(!!enable);
  2932. rc = mwl8k_post_cmd(hw, &cmd->header);
  2933. kfree(cmd);
  2934. return rc;
  2935. }
  2936. struct mwl8k_cmd_update_mac_addr {
  2937. struct mwl8k_cmd_pkt header;
  2938. union {
  2939. struct {
  2940. __le16 mac_type;
  2941. __u8 mac_addr[ETH_ALEN];
  2942. } mbss;
  2943. __u8 mac_addr[ETH_ALEN];
  2944. };
  2945. } __packed;
  2946. #define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
  2947. #define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
  2948. #define MWL8K_MAC_TYPE_PRIMARY_AP 2
  2949. #define MWL8K_MAC_TYPE_SECONDARY_AP 3
  2950. static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
  2951. struct ieee80211_vif *vif, u8 *mac, bool set)
  2952. {
  2953. struct mwl8k_priv *priv = hw->priv;
  2954. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  2955. struct mwl8k_cmd_update_mac_addr *cmd;
  2956. int mac_type;
  2957. int rc;
  2958. mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
  2959. if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
  2960. if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
  2961. if (priv->ap_fw)
  2962. mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
  2963. else
  2964. mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
  2965. else
  2966. mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
  2967. } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
  2968. if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
  2969. mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
  2970. else
  2971. mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
  2972. }
  2973. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2974. if (cmd == NULL)
  2975. return -ENOMEM;
  2976. if (set)
  2977. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
  2978. else
  2979. cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
  2980. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2981. if (priv->ap_fw) {
  2982. cmd->mbss.mac_type = cpu_to_le16(mac_type);
  2983. memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
  2984. } else {
  2985. memcpy(cmd->mac_addr, mac, ETH_ALEN);
  2986. }
  2987. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  2988. kfree(cmd);
  2989. return rc;
  2990. }
  2991. /*
  2992. * MWL8K_CMD_SET_MAC_ADDR.
  2993. */
  2994. static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
  2995. struct ieee80211_vif *vif, u8 *mac)
  2996. {
  2997. return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
  2998. }
  2999. /*
  3000. * MWL8K_CMD_DEL_MAC_ADDR.
  3001. */
  3002. static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
  3003. struct ieee80211_vif *vif, u8 *mac)
  3004. {
  3005. return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
  3006. }
  3007. /*
  3008. * CMD_SET_RATEADAPT_MODE.
  3009. */
  3010. struct mwl8k_cmd_set_rate_adapt_mode {
  3011. struct mwl8k_cmd_pkt header;
  3012. __le16 action;
  3013. __le16 mode;
  3014. } __packed;
  3015. static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
  3016. {
  3017. struct mwl8k_cmd_set_rate_adapt_mode *cmd;
  3018. int rc;
  3019. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3020. if (cmd == NULL)
  3021. return -ENOMEM;
  3022. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
  3023. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3024. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  3025. cmd->mode = cpu_to_le16(mode);
  3026. rc = mwl8k_post_cmd(hw, &cmd->header);
  3027. kfree(cmd);
  3028. return rc;
  3029. }
  3030. /*
  3031. * CMD_GET_WATCHDOG_BITMAP.
  3032. */
  3033. struct mwl8k_cmd_get_watchdog_bitmap {
  3034. struct mwl8k_cmd_pkt header;
  3035. u8 bitmap;
  3036. } __packed;
  3037. static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
  3038. {
  3039. struct mwl8k_cmd_get_watchdog_bitmap *cmd;
  3040. int rc;
  3041. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3042. if (cmd == NULL)
  3043. return -ENOMEM;
  3044. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
  3045. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3046. rc = mwl8k_post_cmd(hw, &cmd->header);
  3047. if (!rc)
  3048. *bitmap = cmd->bitmap;
  3049. kfree(cmd);
  3050. return rc;
  3051. }
  3052. #define MWL8K_WMM_QUEUE_NUMBER 3
  3053. static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
  3054. u8 idx);
  3055. static void mwl8k_watchdog_ba_events(struct work_struct *work)
  3056. {
  3057. int rc;
  3058. u8 bitmap = 0, stream_index;
  3059. struct mwl8k_ampdu_stream *streams;
  3060. struct mwl8k_priv *priv =
  3061. container_of(work, struct mwl8k_priv, watchdog_ba_handle);
  3062. struct ieee80211_hw *hw = priv->hw;
  3063. int i;
  3064. u32 status = 0;
  3065. mwl8k_fw_lock(hw);
  3066. rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
  3067. if (rc)
  3068. goto done;
  3069. spin_lock(&priv->stream_lock);
  3070. /* the bitmap is the hw queue number. Map it to the ampdu queue. */
  3071. for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
  3072. if (bitmap & (1 << i)) {
  3073. stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
  3074. TOTAL_HW_TX_QUEUES;
  3075. streams = &priv->ampdu[stream_index];
  3076. if (streams->state == AMPDU_STREAM_ACTIVE) {
  3077. ieee80211_stop_tx_ba_session(streams->sta,
  3078. streams->tid);
  3079. spin_unlock(&priv->stream_lock);
  3080. mwl8k_destroy_ba(hw, stream_index);
  3081. spin_lock(&priv->stream_lock);
  3082. }
  3083. }
  3084. }
  3085. spin_unlock(&priv->stream_lock);
  3086. done:
  3087. atomic_dec(&priv->watchdog_event_pending);
  3088. status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3089. iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
  3090. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3091. mwl8k_fw_unlock(hw);
  3092. return;
  3093. }
  3094. /*
  3095. * CMD_BSS_START.
  3096. */
  3097. struct mwl8k_cmd_bss_start {
  3098. struct mwl8k_cmd_pkt header;
  3099. __le32 enable;
  3100. } __packed;
  3101. static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
  3102. struct ieee80211_vif *vif, int enable)
  3103. {
  3104. struct mwl8k_cmd_bss_start *cmd;
  3105. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3106. struct mwl8k_priv *priv = hw->priv;
  3107. int rc;
  3108. if (enable && (priv->running_bsses & (1 << mwl8k_vif->macid)))
  3109. return 0;
  3110. if (!enable && !(priv->running_bsses & (1 << mwl8k_vif->macid)))
  3111. return 0;
  3112. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3113. if (cmd == NULL)
  3114. return -ENOMEM;
  3115. cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
  3116. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3117. cmd->enable = cpu_to_le32(enable);
  3118. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3119. kfree(cmd);
  3120. if (!rc) {
  3121. if (enable)
  3122. priv->running_bsses |= (1 << mwl8k_vif->macid);
  3123. else
  3124. priv->running_bsses &= ~(1 << mwl8k_vif->macid);
  3125. }
  3126. return rc;
  3127. }
  3128. static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable, u32 bitmap)
  3129. {
  3130. struct mwl8k_priv *priv = hw->priv;
  3131. struct mwl8k_vif *mwl8k_vif, *tmp_vif;
  3132. struct ieee80211_vif *vif;
  3133. list_for_each_entry_safe(mwl8k_vif, tmp_vif, &priv->vif_list, list) {
  3134. vif = mwl8k_vif->vif;
  3135. if (!(bitmap & (1 << mwl8k_vif->macid)))
  3136. continue;
  3137. if (vif->type == NL80211_IFTYPE_AP)
  3138. mwl8k_cmd_bss_start(hw, vif, enable);
  3139. }
  3140. }
  3141. /*
  3142. * CMD_BASTREAM.
  3143. */
  3144. /*
  3145. * UPSTREAM is tx direction
  3146. */
  3147. #define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
  3148. #define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
  3149. enum ba_stream_action_type {
  3150. MWL8K_BA_CREATE,
  3151. MWL8K_BA_UPDATE,
  3152. MWL8K_BA_DESTROY,
  3153. MWL8K_BA_FLUSH,
  3154. MWL8K_BA_CHECK,
  3155. };
  3156. struct mwl8k_create_ba_stream {
  3157. __le32 flags;
  3158. __le32 idle_thrs;
  3159. __le32 bar_thrs;
  3160. __le32 window_size;
  3161. u8 peer_mac_addr[6];
  3162. u8 dialog_token;
  3163. u8 tid;
  3164. u8 queue_id;
  3165. u8 param_info;
  3166. __le32 ba_context;
  3167. u8 reset_seq_no_flag;
  3168. __le16 curr_seq_no;
  3169. u8 sta_src_mac_addr[6];
  3170. } __packed;
  3171. struct mwl8k_destroy_ba_stream {
  3172. __le32 flags;
  3173. __le32 ba_context;
  3174. } __packed;
  3175. struct mwl8k_cmd_bastream {
  3176. struct mwl8k_cmd_pkt header;
  3177. __le32 action;
  3178. union {
  3179. struct mwl8k_create_ba_stream create_params;
  3180. struct mwl8k_destroy_ba_stream destroy_params;
  3181. };
  3182. } __packed;
  3183. static int
  3184. mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
  3185. struct ieee80211_vif *vif)
  3186. {
  3187. struct mwl8k_cmd_bastream *cmd;
  3188. int rc;
  3189. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3190. if (cmd == NULL)
  3191. return -ENOMEM;
  3192. cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
  3193. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3194. cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
  3195. cmd->create_params.queue_id = stream->idx;
  3196. memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
  3197. ETH_ALEN);
  3198. cmd->create_params.tid = stream->tid;
  3199. cmd->create_params.flags =
  3200. cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
  3201. cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
  3202. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3203. kfree(cmd);
  3204. return rc;
  3205. }
  3206. static int
  3207. mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
  3208. u8 buf_size, struct ieee80211_vif *vif)
  3209. {
  3210. struct mwl8k_cmd_bastream *cmd;
  3211. int rc;
  3212. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3213. if (cmd == NULL)
  3214. return -ENOMEM;
  3215. cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
  3216. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3217. cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
  3218. cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
  3219. cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
  3220. cmd->create_params.queue_id = stream->idx;
  3221. memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
  3222. cmd->create_params.tid = stream->tid;
  3223. cmd->create_params.curr_seq_no = cpu_to_le16(0);
  3224. cmd->create_params.reset_seq_no_flag = 1;
  3225. cmd->create_params.param_info =
  3226. (stream->sta->ht_cap.ampdu_factor &
  3227. IEEE80211_HT_AMPDU_PARM_FACTOR) |
  3228. ((stream->sta->ht_cap.ampdu_density << 2) &
  3229. IEEE80211_HT_AMPDU_PARM_DENSITY);
  3230. cmd->create_params.flags =
  3231. cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
  3232. BASTREAM_FLAG_DIRECTION_UPSTREAM);
  3233. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3234. wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
  3235. stream->sta->addr, stream->tid);
  3236. kfree(cmd);
  3237. return rc;
  3238. }
  3239. static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
  3240. u8 idx)
  3241. {
  3242. struct mwl8k_cmd_bastream *cmd;
  3243. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3244. if (cmd == NULL)
  3245. return;
  3246. cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
  3247. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3248. cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
  3249. cmd->destroy_params.ba_context = cpu_to_le32(idx);
  3250. mwl8k_post_cmd(hw, &cmd->header);
  3251. wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
  3252. kfree(cmd);
  3253. }
  3254. /*
  3255. * CMD_SET_NEW_STN.
  3256. */
  3257. struct mwl8k_cmd_set_new_stn {
  3258. struct mwl8k_cmd_pkt header;
  3259. __le16 aid;
  3260. __u8 mac_addr[6];
  3261. __le16 stn_id;
  3262. __le16 action;
  3263. __le16 rsvd;
  3264. __le32 legacy_rates;
  3265. __u8 ht_rates[4];
  3266. __le16 cap_info;
  3267. __le16 ht_capabilities_info;
  3268. __u8 mac_ht_param_info;
  3269. __u8 rev;
  3270. __u8 control_channel;
  3271. __u8 add_channel;
  3272. __le16 op_mode;
  3273. __le16 stbc;
  3274. __u8 add_qos_info;
  3275. __u8 is_qos_sta;
  3276. __le32 fw_sta_ptr;
  3277. } __packed;
  3278. #define MWL8K_STA_ACTION_ADD 0
  3279. #define MWL8K_STA_ACTION_REMOVE 2
  3280. static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
  3281. struct ieee80211_vif *vif,
  3282. struct ieee80211_sta *sta)
  3283. {
  3284. struct mwl8k_cmd_set_new_stn *cmd;
  3285. u32 rates;
  3286. int rc;
  3287. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3288. if (cmd == NULL)
  3289. return -ENOMEM;
  3290. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
  3291. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3292. cmd->aid = cpu_to_le16(sta->aid);
  3293. memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
  3294. cmd->stn_id = cpu_to_le16(sta->aid);
  3295. cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
  3296. if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
  3297. rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
  3298. else
  3299. rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
  3300. cmd->legacy_rates = cpu_to_le32(rates);
  3301. if (sta->ht_cap.ht_supported) {
  3302. cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
  3303. cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
  3304. cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
  3305. cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
  3306. cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
  3307. cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
  3308. ((sta->ht_cap.ampdu_density & 7) << 2);
  3309. cmd->is_qos_sta = 1;
  3310. }
  3311. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3312. kfree(cmd);
  3313. return rc;
  3314. }
  3315. static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
  3316. struct ieee80211_vif *vif)
  3317. {
  3318. struct mwl8k_cmd_set_new_stn *cmd;
  3319. int rc;
  3320. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3321. if (cmd == NULL)
  3322. return -ENOMEM;
  3323. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
  3324. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3325. memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
  3326. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3327. kfree(cmd);
  3328. return rc;
  3329. }
  3330. static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
  3331. struct ieee80211_vif *vif, u8 *addr)
  3332. {
  3333. struct mwl8k_cmd_set_new_stn *cmd;
  3334. struct mwl8k_priv *priv = hw->priv;
  3335. int rc, i;
  3336. u8 idx;
  3337. spin_lock(&priv->stream_lock);
  3338. /* Destroy any active ampdu streams for this sta */
  3339. for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
  3340. struct mwl8k_ampdu_stream *s;
  3341. s = &priv->ampdu[i];
  3342. if (s->state != AMPDU_NO_STREAM) {
  3343. if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
  3344. if (s->state == AMPDU_STREAM_ACTIVE) {
  3345. idx = s->idx;
  3346. spin_unlock(&priv->stream_lock);
  3347. mwl8k_destroy_ba(hw, idx);
  3348. spin_lock(&priv->stream_lock);
  3349. } else if (s->state == AMPDU_STREAM_NEW) {
  3350. mwl8k_remove_stream(hw, s);
  3351. }
  3352. }
  3353. }
  3354. }
  3355. spin_unlock(&priv->stream_lock);
  3356. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3357. if (cmd == NULL)
  3358. return -ENOMEM;
  3359. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
  3360. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3361. memcpy(cmd->mac_addr, addr, ETH_ALEN);
  3362. cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
  3363. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3364. kfree(cmd);
  3365. return rc;
  3366. }
  3367. /*
  3368. * CMD_UPDATE_ENCRYPTION.
  3369. */
  3370. #define MAX_ENCR_KEY_LENGTH 16
  3371. #define MIC_KEY_LENGTH 8
  3372. struct mwl8k_cmd_update_encryption {
  3373. struct mwl8k_cmd_pkt header;
  3374. __le32 action;
  3375. __le32 reserved;
  3376. __u8 mac_addr[6];
  3377. __u8 encr_type;
  3378. } __packed;
  3379. struct mwl8k_cmd_set_key {
  3380. struct mwl8k_cmd_pkt header;
  3381. __le32 action;
  3382. __le32 reserved;
  3383. __le16 length;
  3384. __le16 key_type_id;
  3385. __le32 key_info;
  3386. __le32 key_id;
  3387. __le16 key_len;
  3388. __u8 key_material[MAX_ENCR_KEY_LENGTH];
  3389. __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
  3390. __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
  3391. __le16 tkip_rsc_low;
  3392. __le32 tkip_rsc_high;
  3393. __le16 tkip_tsc_low;
  3394. __le32 tkip_tsc_high;
  3395. __u8 mac_addr[6];
  3396. } __packed;
  3397. enum {
  3398. MWL8K_ENCR_ENABLE,
  3399. MWL8K_ENCR_SET_KEY,
  3400. MWL8K_ENCR_REMOVE_KEY,
  3401. MWL8K_ENCR_SET_GROUP_KEY,
  3402. };
  3403. #define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
  3404. #define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
  3405. #define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
  3406. #define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
  3407. #define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
  3408. enum {
  3409. MWL8K_ALG_WEP,
  3410. MWL8K_ALG_TKIP,
  3411. MWL8K_ALG_CCMP,
  3412. };
  3413. #define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
  3414. #define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
  3415. #define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
  3416. #define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
  3417. #define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
  3418. static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
  3419. struct ieee80211_vif *vif,
  3420. u8 *addr,
  3421. u8 encr_type)
  3422. {
  3423. struct mwl8k_cmd_update_encryption *cmd;
  3424. int rc;
  3425. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3426. if (cmd == NULL)
  3427. return -ENOMEM;
  3428. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
  3429. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3430. cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
  3431. memcpy(cmd->mac_addr, addr, ETH_ALEN);
  3432. cmd->encr_type = encr_type;
  3433. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3434. kfree(cmd);
  3435. return rc;
  3436. }
  3437. static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
  3438. u8 *addr,
  3439. struct ieee80211_key_conf *key)
  3440. {
  3441. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
  3442. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3443. cmd->length = cpu_to_le16(sizeof(*cmd) -
  3444. offsetof(struct mwl8k_cmd_set_key, length));
  3445. cmd->key_id = cpu_to_le32(key->keyidx);
  3446. cmd->key_len = cpu_to_le16(key->keylen);
  3447. memcpy(cmd->mac_addr, addr, ETH_ALEN);
  3448. switch (key->cipher) {
  3449. case WLAN_CIPHER_SUITE_WEP40:
  3450. case WLAN_CIPHER_SUITE_WEP104:
  3451. cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
  3452. if (key->keyidx == 0)
  3453. cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
  3454. break;
  3455. case WLAN_CIPHER_SUITE_TKIP:
  3456. cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
  3457. cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  3458. ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
  3459. : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
  3460. cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
  3461. | MWL8K_KEY_FLAG_TSC_VALID);
  3462. break;
  3463. case WLAN_CIPHER_SUITE_CCMP:
  3464. cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
  3465. cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  3466. ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
  3467. : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
  3468. break;
  3469. default:
  3470. return -ENOTSUPP;
  3471. }
  3472. return 0;
  3473. }
  3474. static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
  3475. struct ieee80211_vif *vif,
  3476. u8 *addr,
  3477. struct ieee80211_key_conf *key)
  3478. {
  3479. struct mwl8k_cmd_set_key *cmd;
  3480. int rc;
  3481. int keymlen;
  3482. u32 action;
  3483. u8 idx;
  3484. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3485. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3486. if (cmd == NULL)
  3487. return -ENOMEM;
  3488. rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
  3489. if (rc < 0)
  3490. goto done;
  3491. idx = key->keyidx;
  3492. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  3493. action = MWL8K_ENCR_SET_KEY;
  3494. else
  3495. action = MWL8K_ENCR_SET_GROUP_KEY;
  3496. switch (key->cipher) {
  3497. case WLAN_CIPHER_SUITE_WEP40:
  3498. case WLAN_CIPHER_SUITE_WEP104:
  3499. if (!mwl8k_vif->wep_key_conf[idx].enabled) {
  3500. memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
  3501. sizeof(*key) + key->keylen);
  3502. mwl8k_vif->wep_key_conf[idx].enabled = 1;
  3503. }
  3504. keymlen = key->keylen;
  3505. action = MWL8K_ENCR_SET_KEY;
  3506. break;
  3507. case WLAN_CIPHER_SUITE_TKIP:
  3508. keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
  3509. break;
  3510. case WLAN_CIPHER_SUITE_CCMP:
  3511. keymlen = key->keylen;
  3512. break;
  3513. default:
  3514. rc = -ENOTSUPP;
  3515. goto done;
  3516. }
  3517. memcpy(cmd->key_material, key->key, keymlen);
  3518. cmd->action = cpu_to_le32(action);
  3519. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3520. done:
  3521. kfree(cmd);
  3522. return rc;
  3523. }
  3524. static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
  3525. struct ieee80211_vif *vif,
  3526. u8 *addr,
  3527. struct ieee80211_key_conf *key)
  3528. {
  3529. struct mwl8k_cmd_set_key *cmd;
  3530. int rc;
  3531. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3532. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3533. if (cmd == NULL)
  3534. return -ENOMEM;
  3535. rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
  3536. if (rc < 0)
  3537. goto done;
  3538. if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  3539. key->cipher == WLAN_CIPHER_SUITE_WEP104)
  3540. mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
  3541. cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
  3542. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3543. done:
  3544. kfree(cmd);
  3545. return rc;
  3546. }
  3547. static int mwl8k_set_key(struct ieee80211_hw *hw,
  3548. enum set_key_cmd cmd_param,
  3549. struct ieee80211_vif *vif,
  3550. struct ieee80211_sta *sta,
  3551. struct ieee80211_key_conf *key)
  3552. {
  3553. int rc = 0;
  3554. u8 encr_type;
  3555. u8 *addr;
  3556. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3557. struct mwl8k_priv *priv = hw->priv;
  3558. if (vif->type == NL80211_IFTYPE_STATION && !priv->ap_fw)
  3559. return -EOPNOTSUPP;
  3560. if (sta == NULL)
  3561. addr = vif->addr;
  3562. else
  3563. addr = sta->addr;
  3564. if (cmd_param == SET_KEY) {
  3565. rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
  3566. if (rc)
  3567. goto out;
  3568. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
  3569. || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
  3570. encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
  3571. else
  3572. encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
  3573. rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
  3574. encr_type);
  3575. if (rc)
  3576. goto out;
  3577. mwl8k_vif->is_hw_crypto_enabled = true;
  3578. } else {
  3579. rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
  3580. if (rc)
  3581. goto out;
  3582. }
  3583. out:
  3584. return rc;
  3585. }
  3586. /*
  3587. * CMD_UPDATE_STADB.
  3588. */
  3589. struct ewc_ht_info {
  3590. __le16 control1;
  3591. __le16 control2;
  3592. __le16 control3;
  3593. } __packed;
  3594. struct peer_capability_info {
  3595. /* Peer type - AP vs. STA. */
  3596. __u8 peer_type;
  3597. /* Basic 802.11 capabilities from assoc resp. */
  3598. __le16 basic_caps;
  3599. /* Set if peer supports 802.11n high throughput (HT). */
  3600. __u8 ht_support;
  3601. /* Valid if HT is supported. */
  3602. __le16 ht_caps;
  3603. __u8 extended_ht_caps;
  3604. struct ewc_ht_info ewc_info;
  3605. /* Legacy rate table. Intersection of our rates and peer rates. */
  3606. __u8 legacy_rates[12];
  3607. /* HT rate table. Intersection of our rates and peer rates. */
  3608. __u8 ht_rates[16];
  3609. __u8 pad[16];
  3610. /* If set, interoperability mode, no proprietary extensions. */
  3611. __u8 interop;
  3612. __u8 pad2;
  3613. __u8 station_id;
  3614. __le16 amsdu_enabled;
  3615. } __packed;
  3616. struct mwl8k_cmd_update_stadb {
  3617. struct mwl8k_cmd_pkt header;
  3618. /* See STADB_ACTION_TYPE */
  3619. __le32 action;
  3620. /* Peer MAC address */
  3621. __u8 peer_addr[ETH_ALEN];
  3622. __le32 reserved;
  3623. /* Peer info - valid during add/update. */
  3624. struct peer_capability_info peer_info;
  3625. } __packed;
  3626. #define MWL8K_STA_DB_MODIFY_ENTRY 1
  3627. #define MWL8K_STA_DB_DEL_ENTRY 2
  3628. /* Peer Entry flags - used to define the type of the peer node */
  3629. #define MWL8K_PEER_TYPE_ACCESSPOINT 2
  3630. static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
  3631. struct ieee80211_vif *vif,
  3632. struct ieee80211_sta *sta)
  3633. {
  3634. struct mwl8k_cmd_update_stadb *cmd;
  3635. struct peer_capability_info *p;
  3636. u32 rates;
  3637. int rc;
  3638. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3639. if (cmd == NULL)
  3640. return -ENOMEM;
  3641. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
  3642. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3643. cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
  3644. memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
  3645. p = &cmd->peer_info;
  3646. p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
  3647. p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
  3648. p->ht_support = sta->ht_cap.ht_supported;
  3649. p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
  3650. p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
  3651. ((sta->ht_cap.ampdu_density & 7) << 2);
  3652. if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
  3653. rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
  3654. else
  3655. rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
  3656. legacy_rate_mask_to_array(p->legacy_rates, rates);
  3657. memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
  3658. p->interop = 1;
  3659. p->amsdu_enabled = 0;
  3660. rc = mwl8k_post_cmd(hw, &cmd->header);
  3661. if (!rc)
  3662. rc = p->station_id;
  3663. kfree(cmd);
  3664. return rc;
  3665. }
  3666. static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
  3667. struct ieee80211_vif *vif, u8 *addr)
  3668. {
  3669. struct mwl8k_cmd_update_stadb *cmd;
  3670. int rc;
  3671. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3672. if (cmd == NULL)
  3673. return -ENOMEM;
  3674. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
  3675. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3676. cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
  3677. memcpy(cmd->peer_addr, addr, ETH_ALEN);
  3678. rc = mwl8k_post_cmd(hw, &cmd->header);
  3679. kfree(cmd);
  3680. return rc;
  3681. }
  3682. /*
  3683. * Interrupt handling.
  3684. */
  3685. static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
  3686. {
  3687. struct ieee80211_hw *hw = dev_id;
  3688. struct mwl8k_priv *priv = hw->priv;
  3689. u32 status;
  3690. status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3691. if (!status)
  3692. return IRQ_NONE;
  3693. if (status & MWL8K_A2H_INT_TX_DONE) {
  3694. status &= ~MWL8K_A2H_INT_TX_DONE;
  3695. tasklet_schedule(&priv->poll_tx_task);
  3696. }
  3697. if (status & MWL8K_A2H_INT_RX_READY) {
  3698. status &= ~MWL8K_A2H_INT_RX_READY;
  3699. tasklet_schedule(&priv->poll_rx_task);
  3700. }
  3701. if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
  3702. iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
  3703. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3704. atomic_inc(&priv->watchdog_event_pending);
  3705. status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
  3706. ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
  3707. }
  3708. if (status)
  3709. iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3710. if (status & MWL8K_A2H_INT_OPC_DONE) {
  3711. if (priv->hostcmd_wait != NULL)
  3712. complete(priv->hostcmd_wait);
  3713. }
  3714. if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
  3715. if (!mutex_is_locked(&priv->fw_mutex) &&
  3716. priv->radio_on && priv->pending_tx_pkts)
  3717. mwl8k_tx_start(priv);
  3718. }
  3719. return IRQ_HANDLED;
  3720. }
  3721. static void mwl8k_tx_poll(unsigned long data)
  3722. {
  3723. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  3724. struct mwl8k_priv *priv = hw->priv;
  3725. int limit;
  3726. int i;
  3727. limit = 32;
  3728. spin_lock_bh(&priv->tx_lock);
  3729. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  3730. limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
  3731. if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
  3732. complete(priv->tx_wait);
  3733. priv->tx_wait = NULL;
  3734. }
  3735. spin_unlock_bh(&priv->tx_lock);
  3736. if (limit) {
  3737. writel(~MWL8K_A2H_INT_TX_DONE,
  3738. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3739. } else {
  3740. tasklet_schedule(&priv->poll_tx_task);
  3741. }
  3742. }
  3743. static void mwl8k_rx_poll(unsigned long data)
  3744. {
  3745. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  3746. struct mwl8k_priv *priv = hw->priv;
  3747. int limit;
  3748. limit = 32;
  3749. limit -= rxq_process(hw, 0, limit);
  3750. limit -= rxq_refill(hw, 0, limit);
  3751. if (limit) {
  3752. writel(~MWL8K_A2H_INT_RX_READY,
  3753. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3754. } else {
  3755. tasklet_schedule(&priv->poll_rx_task);
  3756. }
  3757. }
  3758. /*
  3759. * Core driver operations.
  3760. */
  3761. static void mwl8k_tx(struct ieee80211_hw *hw,
  3762. struct ieee80211_tx_control *control,
  3763. struct sk_buff *skb)
  3764. {
  3765. struct mwl8k_priv *priv = hw->priv;
  3766. int index = skb_get_queue_mapping(skb);
  3767. if (!priv->radio_on) {
  3768. wiphy_debug(hw->wiphy,
  3769. "dropped TX frame since radio disabled\n");
  3770. dev_kfree_skb(skb);
  3771. return;
  3772. }
  3773. mwl8k_txq_xmit(hw, index, control->sta, skb);
  3774. }
  3775. static int mwl8k_start(struct ieee80211_hw *hw)
  3776. {
  3777. struct mwl8k_priv *priv = hw->priv;
  3778. int rc;
  3779. rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
  3780. IRQF_SHARED, MWL8K_NAME, hw);
  3781. if (rc) {
  3782. priv->irq = -1;
  3783. wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
  3784. return -EIO;
  3785. }
  3786. priv->irq = priv->pdev->irq;
  3787. /* Enable TX reclaim and RX tasklets. */
  3788. tasklet_enable(&priv->poll_tx_task);
  3789. tasklet_enable(&priv->poll_rx_task);
  3790. /* Enable interrupts */
  3791. iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  3792. iowrite32(MWL8K_A2H_EVENTS,
  3793. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3794. rc = mwl8k_fw_lock(hw);
  3795. if (!rc) {
  3796. rc = mwl8k_cmd_radio_enable(hw);
  3797. if (!priv->ap_fw) {
  3798. if (!rc)
  3799. rc = mwl8k_cmd_enable_sniffer(hw, 0);
  3800. if (!rc)
  3801. rc = mwl8k_cmd_set_pre_scan(hw);
  3802. if (!rc)
  3803. rc = mwl8k_cmd_set_post_scan(hw,
  3804. "\x00\x00\x00\x00\x00\x00");
  3805. }
  3806. if (!rc)
  3807. rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
  3808. if (!rc)
  3809. rc = mwl8k_cmd_set_wmm_mode(hw, 0);
  3810. mwl8k_fw_unlock(hw);
  3811. }
  3812. if (rc) {
  3813. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  3814. free_irq(priv->pdev->irq, hw);
  3815. priv->irq = -1;
  3816. tasklet_disable(&priv->poll_tx_task);
  3817. tasklet_disable(&priv->poll_rx_task);
  3818. } else {
  3819. ieee80211_wake_queues(hw);
  3820. }
  3821. return rc;
  3822. }
  3823. static void mwl8k_stop(struct ieee80211_hw *hw)
  3824. {
  3825. struct mwl8k_priv *priv = hw->priv;
  3826. int i;
  3827. if (!priv->hw_restart_in_progress)
  3828. mwl8k_cmd_radio_disable(hw);
  3829. ieee80211_stop_queues(hw);
  3830. /* Disable interrupts */
  3831. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  3832. if (priv->irq != -1) {
  3833. free_irq(priv->pdev->irq, hw);
  3834. priv->irq = -1;
  3835. }
  3836. /* Stop finalize join worker */
  3837. cancel_work_sync(&priv->finalize_join_worker);
  3838. cancel_work_sync(&priv->watchdog_ba_handle);
  3839. if (priv->beacon_skb != NULL)
  3840. dev_kfree_skb(priv->beacon_skb);
  3841. /* Stop TX reclaim and RX tasklets. */
  3842. tasklet_disable(&priv->poll_tx_task);
  3843. tasklet_disable(&priv->poll_rx_task);
  3844. /* Return all skbs to mac80211 */
  3845. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  3846. mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
  3847. }
  3848. static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
  3849. static int mwl8k_add_interface(struct ieee80211_hw *hw,
  3850. struct ieee80211_vif *vif)
  3851. {
  3852. struct mwl8k_priv *priv = hw->priv;
  3853. struct mwl8k_vif *mwl8k_vif;
  3854. u32 macids_supported;
  3855. int macid, rc;
  3856. struct mwl8k_device_info *di;
  3857. /*
  3858. * Reject interface creation if sniffer mode is active, as
  3859. * STA operation is mutually exclusive with hardware sniffer
  3860. * mode. (Sniffer mode is only used on STA firmware.)
  3861. */
  3862. if (priv->sniffer_enabled) {
  3863. wiphy_info(hw->wiphy,
  3864. "unable to create STA interface because sniffer mode is enabled\n");
  3865. return -EINVAL;
  3866. }
  3867. di = priv->device_info;
  3868. switch (vif->type) {
  3869. case NL80211_IFTYPE_AP:
  3870. if (!priv->ap_fw && di->fw_image_ap) {
  3871. /* we must load the ap fw to meet this request */
  3872. if (!list_empty(&priv->vif_list))
  3873. return -EBUSY;
  3874. rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
  3875. if (rc)
  3876. return rc;
  3877. }
  3878. macids_supported = priv->ap_macids_supported;
  3879. break;
  3880. case NL80211_IFTYPE_STATION:
  3881. if (priv->ap_fw && di->fw_image_sta) {
  3882. if (!list_empty(&priv->vif_list)) {
  3883. wiphy_warn(hw->wiphy, "AP interface is running.\n"
  3884. "Adding STA interface for WDS");
  3885. } else {
  3886. /* we must load the sta fw to
  3887. * meet this request.
  3888. */
  3889. rc = mwl8k_reload_firmware(hw,
  3890. di->fw_image_sta);
  3891. if (rc)
  3892. return rc;
  3893. }
  3894. }
  3895. macids_supported = priv->sta_macids_supported;
  3896. break;
  3897. default:
  3898. return -EINVAL;
  3899. }
  3900. macid = ffs(macids_supported & ~priv->macids_used);
  3901. if (!macid--)
  3902. return -EBUSY;
  3903. /* Setup driver private area. */
  3904. mwl8k_vif = MWL8K_VIF(vif);
  3905. memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
  3906. mwl8k_vif->vif = vif;
  3907. mwl8k_vif->macid = macid;
  3908. mwl8k_vif->seqno = 0;
  3909. memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
  3910. mwl8k_vif->is_hw_crypto_enabled = false;
  3911. /* Set the mac address. */
  3912. mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
  3913. if (vif->type == NL80211_IFTYPE_AP)
  3914. mwl8k_cmd_set_new_stn_add_self(hw, vif);
  3915. priv->macids_used |= 1 << mwl8k_vif->macid;
  3916. list_add_tail(&mwl8k_vif->list, &priv->vif_list);
  3917. return 0;
  3918. }
  3919. static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
  3920. {
  3921. /* Has ieee80211_restart_hw re-added the removed interfaces? */
  3922. if (!priv->macids_used)
  3923. return;
  3924. priv->macids_used &= ~(1 << vif->macid);
  3925. list_del(&vif->list);
  3926. }
  3927. static void mwl8k_remove_interface(struct ieee80211_hw *hw,
  3928. struct ieee80211_vif *vif)
  3929. {
  3930. struct mwl8k_priv *priv = hw->priv;
  3931. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3932. if (vif->type == NL80211_IFTYPE_AP)
  3933. mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
  3934. mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
  3935. mwl8k_remove_vif(priv, mwl8k_vif);
  3936. }
  3937. static void mwl8k_hw_restart_work(struct work_struct *work)
  3938. {
  3939. struct mwl8k_priv *priv =
  3940. container_of(work, struct mwl8k_priv, fw_reload);
  3941. struct ieee80211_hw *hw = priv->hw;
  3942. struct mwl8k_device_info *di;
  3943. int rc;
  3944. /* If some command is waiting for a response, clear it */
  3945. if (priv->hostcmd_wait != NULL) {
  3946. complete(priv->hostcmd_wait);
  3947. priv->hostcmd_wait = NULL;
  3948. }
  3949. priv->hw_restart_owner = current;
  3950. di = priv->device_info;
  3951. mwl8k_fw_lock(hw);
  3952. if (priv->ap_fw)
  3953. rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
  3954. else
  3955. rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
  3956. if (rc)
  3957. goto fail;
  3958. priv->hw_restart_owner = NULL;
  3959. priv->hw_restart_in_progress = false;
  3960. /*
  3961. * This unlock will wake up the queues and
  3962. * also opens the command path for other
  3963. * commands
  3964. */
  3965. mwl8k_fw_unlock(hw);
  3966. ieee80211_restart_hw(hw);
  3967. wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
  3968. return;
  3969. fail:
  3970. mwl8k_fw_unlock(hw);
  3971. wiphy_err(hw->wiphy, "Firmware restart failed\n");
  3972. }
  3973. static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
  3974. {
  3975. struct ieee80211_conf *conf = &hw->conf;
  3976. struct mwl8k_priv *priv = hw->priv;
  3977. int rc;
  3978. rc = mwl8k_fw_lock(hw);
  3979. if (rc)
  3980. return rc;
  3981. if (conf->flags & IEEE80211_CONF_IDLE)
  3982. rc = mwl8k_cmd_radio_disable(hw);
  3983. else
  3984. rc = mwl8k_cmd_radio_enable(hw);
  3985. if (rc)
  3986. goto out;
  3987. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  3988. rc = mwl8k_cmd_set_rf_channel(hw, conf);
  3989. if (rc)
  3990. goto out;
  3991. }
  3992. if (conf->power_level > 18)
  3993. conf->power_level = 18;
  3994. if (priv->ap_fw) {
  3995. if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
  3996. rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
  3997. if (rc)
  3998. goto out;
  3999. }
  4000. } else {
  4001. rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
  4002. if (rc)
  4003. goto out;
  4004. rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
  4005. }
  4006. out:
  4007. mwl8k_fw_unlock(hw);
  4008. return rc;
  4009. }
  4010. static void
  4011. mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4012. struct ieee80211_bss_conf *info, u32 changed)
  4013. {
  4014. struct mwl8k_priv *priv = hw->priv;
  4015. u32 ap_legacy_rates = 0;
  4016. u8 ap_mcs_rates[16];
  4017. int rc;
  4018. if (mwl8k_fw_lock(hw))
  4019. return;
  4020. /*
  4021. * No need to capture a beacon if we're no longer associated.
  4022. */
  4023. if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
  4024. priv->capture_beacon = false;
  4025. /*
  4026. * Get the AP's legacy and MCS rates.
  4027. */
  4028. if (vif->bss_conf.assoc) {
  4029. struct ieee80211_sta *ap;
  4030. rcu_read_lock();
  4031. ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
  4032. if (ap == NULL) {
  4033. rcu_read_unlock();
  4034. goto out;
  4035. }
  4036. if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ) {
  4037. ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
  4038. } else {
  4039. ap_legacy_rates =
  4040. ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
  4041. }
  4042. memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
  4043. rcu_read_unlock();
  4044. }
  4045. if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
  4046. !priv->ap_fw) {
  4047. rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
  4048. if (rc)
  4049. goto out;
  4050. rc = mwl8k_cmd_use_fixed_rate_sta(hw);
  4051. if (rc)
  4052. goto out;
  4053. } else {
  4054. if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
  4055. priv->ap_fw) {
  4056. int idx;
  4057. int rate;
  4058. /* Use AP firmware specific rate command.
  4059. */
  4060. idx = ffs(vif->bss_conf.basic_rates);
  4061. if (idx)
  4062. idx--;
  4063. if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
  4064. rate = mwl8k_rates_24[idx].hw_value;
  4065. else
  4066. rate = mwl8k_rates_50[idx].hw_value;
  4067. mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
  4068. }
  4069. }
  4070. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  4071. rc = mwl8k_set_radio_preamble(hw,
  4072. vif->bss_conf.use_short_preamble);
  4073. if (rc)
  4074. goto out;
  4075. }
  4076. if ((changed & BSS_CHANGED_ERP_SLOT) && !priv->ap_fw) {
  4077. rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
  4078. if (rc)
  4079. goto out;
  4080. }
  4081. if (vif->bss_conf.assoc && !priv->ap_fw &&
  4082. (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
  4083. BSS_CHANGED_HT))) {
  4084. rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
  4085. if (rc)
  4086. goto out;
  4087. }
  4088. if (vif->bss_conf.assoc &&
  4089. (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
  4090. /*
  4091. * Finalize the join. Tell rx handler to process
  4092. * next beacon from our BSSID.
  4093. */
  4094. memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
  4095. priv->capture_beacon = true;
  4096. }
  4097. out:
  4098. mwl8k_fw_unlock(hw);
  4099. }
  4100. static void
  4101. mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4102. struct ieee80211_bss_conf *info, u32 changed)
  4103. {
  4104. int rc;
  4105. if (mwl8k_fw_lock(hw))
  4106. return;
  4107. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  4108. rc = mwl8k_set_radio_preamble(hw,
  4109. vif->bss_conf.use_short_preamble);
  4110. if (rc)
  4111. goto out;
  4112. }
  4113. if (changed & BSS_CHANGED_BASIC_RATES) {
  4114. int idx;
  4115. int rate;
  4116. /*
  4117. * Use lowest supported basic rate for multicasts
  4118. * and management frames (such as probe responses --
  4119. * beacons will always go out at 1 Mb/s).
  4120. */
  4121. idx = ffs(vif->bss_conf.basic_rates);
  4122. if (idx)
  4123. idx--;
  4124. if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
  4125. rate = mwl8k_rates_24[idx].hw_value;
  4126. else
  4127. rate = mwl8k_rates_50[idx].hw_value;
  4128. mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
  4129. }
  4130. if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
  4131. struct sk_buff *skb;
  4132. skb = ieee80211_beacon_get(hw, vif);
  4133. if (skb != NULL) {
  4134. mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
  4135. kfree_skb(skb);
  4136. }
  4137. }
  4138. if (changed & BSS_CHANGED_BEACON_ENABLED)
  4139. mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
  4140. out:
  4141. mwl8k_fw_unlock(hw);
  4142. }
  4143. static void
  4144. mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4145. struct ieee80211_bss_conf *info, u32 changed)
  4146. {
  4147. if (vif->type == NL80211_IFTYPE_STATION)
  4148. mwl8k_bss_info_changed_sta(hw, vif, info, changed);
  4149. if (vif->type == NL80211_IFTYPE_AP)
  4150. mwl8k_bss_info_changed_ap(hw, vif, info, changed);
  4151. }
  4152. static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
  4153. struct netdev_hw_addr_list *mc_list)
  4154. {
  4155. struct mwl8k_cmd_pkt *cmd;
  4156. /*
  4157. * Synthesize and return a command packet that programs the
  4158. * hardware multicast address filter. At this point we don't
  4159. * know whether FIF_ALLMULTI is being requested, but if it is,
  4160. * we'll end up throwing this packet away and creating a new
  4161. * one in mwl8k_configure_filter().
  4162. */
  4163. cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
  4164. return (unsigned long)cmd;
  4165. }
  4166. static int
  4167. mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
  4168. unsigned int changed_flags,
  4169. unsigned int *total_flags)
  4170. {
  4171. struct mwl8k_priv *priv = hw->priv;
  4172. /*
  4173. * Hardware sniffer mode is mutually exclusive with STA
  4174. * operation, so refuse to enable sniffer mode if a STA
  4175. * interface is active.
  4176. */
  4177. if (!list_empty(&priv->vif_list)) {
  4178. if (net_ratelimit())
  4179. wiphy_info(hw->wiphy,
  4180. "not enabling sniffer mode because STA interface is active\n");
  4181. return 0;
  4182. }
  4183. if (!priv->sniffer_enabled) {
  4184. if (mwl8k_cmd_enable_sniffer(hw, 1))
  4185. return 0;
  4186. priv->sniffer_enabled = true;
  4187. }
  4188. *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
  4189. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
  4190. FIF_OTHER_BSS;
  4191. return 1;
  4192. }
  4193. static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
  4194. {
  4195. if (!list_empty(&priv->vif_list))
  4196. return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
  4197. return NULL;
  4198. }
  4199. static void mwl8k_configure_filter(struct ieee80211_hw *hw,
  4200. unsigned int changed_flags,
  4201. unsigned int *total_flags,
  4202. u64 multicast)
  4203. {
  4204. struct mwl8k_priv *priv = hw->priv;
  4205. struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
  4206. /*
  4207. * AP firmware doesn't allow fine-grained control over
  4208. * the receive filter.
  4209. */
  4210. if (priv->ap_fw) {
  4211. *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
  4212. kfree(cmd);
  4213. return;
  4214. }
  4215. /*
  4216. * Enable hardware sniffer mode if FIF_CONTROL or
  4217. * FIF_OTHER_BSS is requested.
  4218. */
  4219. if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
  4220. mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
  4221. kfree(cmd);
  4222. return;
  4223. }
  4224. /* Clear unsupported feature flags */
  4225. *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
  4226. if (mwl8k_fw_lock(hw)) {
  4227. kfree(cmd);
  4228. return;
  4229. }
  4230. if (priv->sniffer_enabled) {
  4231. mwl8k_cmd_enable_sniffer(hw, 0);
  4232. priv->sniffer_enabled = false;
  4233. }
  4234. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  4235. if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
  4236. /*
  4237. * Disable the BSS filter.
  4238. */
  4239. mwl8k_cmd_set_pre_scan(hw);
  4240. } else {
  4241. struct mwl8k_vif *mwl8k_vif;
  4242. const u8 *bssid;
  4243. /*
  4244. * Enable the BSS filter.
  4245. *
  4246. * If there is an active STA interface, use that
  4247. * interface's BSSID, otherwise use a dummy one
  4248. * (where the OUI part needs to be nonzero for
  4249. * the BSSID to be accepted by POST_SCAN).
  4250. */
  4251. mwl8k_vif = mwl8k_first_vif(priv);
  4252. if (mwl8k_vif != NULL)
  4253. bssid = mwl8k_vif->vif->bss_conf.bssid;
  4254. else
  4255. bssid = "\x01\x00\x00\x00\x00\x00";
  4256. mwl8k_cmd_set_post_scan(hw, bssid);
  4257. }
  4258. }
  4259. /*
  4260. * If FIF_ALLMULTI is being requested, throw away the command
  4261. * packet that ->prepare_multicast() built and replace it with
  4262. * a command packet that enables reception of all multicast
  4263. * packets.
  4264. */
  4265. if (*total_flags & FIF_ALLMULTI) {
  4266. kfree(cmd);
  4267. cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
  4268. }
  4269. if (cmd != NULL) {
  4270. mwl8k_post_cmd(hw, cmd);
  4271. kfree(cmd);
  4272. }
  4273. mwl8k_fw_unlock(hw);
  4274. }
  4275. static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  4276. {
  4277. return mwl8k_cmd_set_rts_threshold(hw, value);
  4278. }
  4279. static int mwl8k_sta_remove(struct ieee80211_hw *hw,
  4280. struct ieee80211_vif *vif,
  4281. struct ieee80211_sta *sta)
  4282. {
  4283. struct mwl8k_priv *priv = hw->priv;
  4284. if (priv->ap_fw)
  4285. return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
  4286. else
  4287. return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
  4288. }
  4289. static int mwl8k_sta_add(struct ieee80211_hw *hw,
  4290. struct ieee80211_vif *vif,
  4291. struct ieee80211_sta *sta)
  4292. {
  4293. struct mwl8k_priv *priv = hw->priv;
  4294. int ret;
  4295. int i;
  4296. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  4297. struct ieee80211_key_conf *key;
  4298. if (!priv->ap_fw) {
  4299. ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
  4300. if (ret >= 0) {
  4301. MWL8K_STA(sta)->peer_id = ret;
  4302. if (sta->ht_cap.ht_supported)
  4303. MWL8K_STA(sta)->is_ampdu_allowed = true;
  4304. ret = 0;
  4305. }
  4306. } else {
  4307. ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
  4308. }
  4309. for (i = 0; i < NUM_WEP_KEYS; i++) {
  4310. key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
  4311. if (mwl8k_vif->wep_key_conf[i].enabled)
  4312. mwl8k_set_key(hw, SET_KEY, vif, sta, key);
  4313. }
  4314. return ret;
  4315. }
  4316. static int mwl8k_conf_tx(struct ieee80211_hw *hw,
  4317. struct ieee80211_vif *vif, u16 queue,
  4318. const struct ieee80211_tx_queue_params *params)
  4319. {
  4320. struct mwl8k_priv *priv = hw->priv;
  4321. int rc;
  4322. rc = mwl8k_fw_lock(hw);
  4323. if (!rc) {
  4324. BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
  4325. memcpy(&priv->wmm_params[queue], params, sizeof(*params));
  4326. if (!priv->wmm_enabled)
  4327. rc = mwl8k_cmd_set_wmm_mode(hw, 1);
  4328. if (!rc) {
  4329. int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
  4330. rc = mwl8k_cmd_set_edca_params(hw, q,
  4331. params->cw_min,
  4332. params->cw_max,
  4333. params->aifs,
  4334. params->txop);
  4335. }
  4336. mwl8k_fw_unlock(hw);
  4337. }
  4338. return rc;
  4339. }
  4340. static int mwl8k_get_stats(struct ieee80211_hw *hw,
  4341. struct ieee80211_low_level_stats *stats)
  4342. {
  4343. return mwl8k_cmd_get_stat(hw, stats);
  4344. }
  4345. static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
  4346. struct survey_info *survey)
  4347. {
  4348. struct mwl8k_priv *priv = hw->priv;
  4349. struct ieee80211_conf *conf = &hw->conf;
  4350. if (idx != 0)
  4351. return -ENOENT;
  4352. survey->channel = conf->chandef.chan;
  4353. survey->filled = SURVEY_INFO_NOISE_DBM;
  4354. survey->noise = priv->noise;
  4355. return 0;
  4356. }
  4357. #define MAX_AMPDU_ATTEMPTS 5
  4358. static int
  4359. mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4360. enum ieee80211_ampdu_mlme_action action,
  4361. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  4362. u8 buf_size)
  4363. {
  4364. int i, rc = 0;
  4365. struct mwl8k_priv *priv = hw->priv;
  4366. struct mwl8k_ampdu_stream *stream;
  4367. u8 *addr = sta->addr, idx;
  4368. struct mwl8k_sta *sta_info = MWL8K_STA(sta);
  4369. if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
  4370. return -ENOTSUPP;
  4371. spin_lock(&priv->stream_lock);
  4372. stream = mwl8k_lookup_stream(hw, addr, tid);
  4373. switch (action) {
  4374. case IEEE80211_AMPDU_RX_START:
  4375. case IEEE80211_AMPDU_RX_STOP:
  4376. break;
  4377. case IEEE80211_AMPDU_TX_START:
  4378. /* By the time we get here the hw queues may contain outgoing
  4379. * packets for this RA/TID that are not part of this BA
  4380. * session. The hw will assign sequence numbers to these
  4381. * packets as they go out. So if we query the hw for its next
  4382. * sequence number and use that for the SSN here, it may end up
  4383. * being wrong, which will lead to sequence number mismatch at
  4384. * the recipient. To avoid this, we reset the sequence number
  4385. * to O for the first MPDU in this BA stream.
  4386. */
  4387. *ssn = 0;
  4388. if (stream == NULL) {
  4389. /* This means that somebody outside this driver called
  4390. * ieee80211_start_tx_ba_session. This is unexpected
  4391. * because we do our own rate control. Just warn and
  4392. * move on.
  4393. */
  4394. wiphy_warn(hw->wiphy, "Unexpected call to %s. "
  4395. "Proceeding anyway.\n", __func__);
  4396. stream = mwl8k_add_stream(hw, sta, tid);
  4397. }
  4398. if (stream == NULL) {
  4399. wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
  4400. rc = -EBUSY;
  4401. break;
  4402. }
  4403. stream->state = AMPDU_STREAM_IN_PROGRESS;
  4404. /* Release the lock before we do the time consuming stuff */
  4405. spin_unlock(&priv->stream_lock);
  4406. for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
  4407. /* Check if link is still valid */
  4408. if (!sta_info->is_ampdu_allowed) {
  4409. spin_lock(&priv->stream_lock);
  4410. mwl8k_remove_stream(hw, stream);
  4411. spin_unlock(&priv->stream_lock);
  4412. return -EBUSY;
  4413. }
  4414. rc = mwl8k_check_ba(hw, stream, vif);
  4415. /* If HW restart is in progress mwl8k_post_cmd will
  4416. * return -EBUSY. Avoid retrying mwl8k_check_ba in
  4417. * such cases
  4418. */
  4419. if (!rc || rc == -EBUSY)
  4420. break;
  4421. /*
  4422. * HW queues take time to be flushed, give them
  4423. * sufficient time
  4424. */
  4425. msleep(1000);
  4426. }
  4427. spin_lock(&priv->stream_lock);
  4428. if (rc) {
  4429. wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
  4430. " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
  4431. mwl8k_remove_stream(hw, stream);
  4432. rc = -EBUSY;
  4433. break;
  4434. }
  4435. ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
  4436. break;
  4437. case IEEE80211_AMPDU_TX_STOP_CONT:
  4438. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  4439. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  4440. if (stream) {
  4441. if (stream->state == AMPDU_STREAM_ACTIVE) {
  4442. idx = stream->idx;
  4443. spin_unlock(&priv->stream_lock);
  4444. mwl8k_destroy_ba(hw, idx);
  4445. spin_lock(&priv->stream_lock);
  4446. }
  4447. mwl8k_remove_stream(hw, stream);
  4448. }
  4449. ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
  4450. break;
  4451. case IEEE80211_AMPDU_TX_OPERATIONAL:
  4452. BUG_ON(stream == NULL);
  4453. BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
  4454. spin_unlock(&priv->stream_lock);
  4455. rc = mwl8k_create_ba(hw, stream, buf_size, vif);
  4456. spin_lock(&priv->stream_lock);
  4457. if (!rc)
  4458. stream->state = AMPDU_STREAM_ACTIVE;
  4459. else {
  4460. idx = stream->idx;
  4461. spin_unlock(&priv->stream_lock);
  4462. mwl8k_destroy_ba(hw, idx);
  4463. spin_lock(&priv->stream_lock);
  4464. wiphy_debug(hw->wiphy,
  4465. "Failed adding stream for sta %pM tid %d\n",
  4466. addr, tid);
  4467. mwl8k_remove_stream(hw, stream);
  4468. }
  4469. break;
  4470. default:
  4471. rc = -ENOTSUPP;
  4472. }
  4473. spin_unlock(&priv->stream_lock);
  4474. return rc;
  4475. }
  4476. static const struct ieee80211_ops mwl8k_ops = {
  4477. .tx = mwl8k_tx,
  4478. .start = mwl8k_start,
  4479. .stop = mwl8k_stop,
  4480. .add_interface = mwl8k_add_interface,
  4481. .remove_interface = mwl8k_remove_interface,
  4482. .config = mwl8k_config,
  4483. .bss_info_changed = mwl8k_bss_info_changed,
  4484. .prepare_multicast = mwl8k_prepare_multicast,
  4485. .configure_filter = mwl8k_configure_filter,
  4486. .set_key = mwl8k_set_key,
  4487. .set_rts_threshold = mwl8k_set_rts_threshold,
  4488. .sta_add = mwl8k_sta_add,
  4489. .sta_remove = mwl8k_sta_remove,
  4490. .conf_tx = mwl8k_conf_tx,
  4491. .get_stats = mwl8k_get_stats,
  4492. .get_survey = mwl8k_get_survey,
  4493. .ampdu_action = mwl8k_ampdu_action,
  4494. };
  4495. static void mwl8k_finalize_join_worker(struct work_struct *work)
  4496. {
  4497. struct mwl8k_priv *priv =
  4498. container_of(work, struct mwl8k_priv, finalize_join_worker);
  4499. struct sk_buff *skb = priv->beacon_skb;
  4500. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  4501. int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
  4502. const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
  4503. mgmt->u.beacon.variable, len);
  4504. int dtim_period = 1;
  4505. if (tim && tim[1] >= 2)
  4506. dtim_period = tim[3];
  4507. mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
  4508. dev_kfree_skb(skb);
  4509. priv->beacon_skb = NULL;
  4510. }
  4511. enum {
  4512. MWL8363 = 0,
  4513. MWL8687,
  4514. MWL8366,
  4515. MWL8764,
  4516. };
  4517. #define MWL8K_8366_AP_FW_API 3
  4518. #define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
  4519. #define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
  4520. #define MWL8K_8764_AP_FW_API 1
  4521. #define _MWL8K_8764_AP_FW(api) "mwl8k/fmimage_8764_ap-" #api ".fw"
  4522. #define MWL8K_8764_AP_FW(api) _MWL8K_8764_AP_FW(api)
  4523. static struct mwl8k_device_info mwl8k_info_tbl[] = {
  4524. [MWL8363] = {
  4525. .part_name = "88w8363",
  4526. .helper_image = "mwl8k/helper_8363.fw",
  4527. .fw_image_sta = "mwl8k/fmimage_8363.fw",
  4528. },
  4529. [MWL8687] = {
  4530. .part_name = "88w8687",
  4531. .helper_image = "mwl8k/helper_8687.fw",
  4532. .fw_image_sta = "mwl8k/fmimage_8687.fw",
  4533. },
  4534. [MWL8366] = {
  4535. .part_name = "88w8366",
  4536. .helper_image = "mwl8k/helper_8366.fw",
  4537. .fw_image_sta = "mwl8k/fmimage_8366.fw",
  4538. .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
  4539. .fw_api_ap = MWL8K_8366_AP_FW_API,
  4540. .ap_rxd_ops = &rxd_ap_ops,
  4541. },
  4542. [MWL8764] = {
  4543. .part_name = "88w8764",
  4544. .fw_image_ap = MWL8K_8764_AP_FW(MWL8K_8764_AP_FW_API),
  4545. .fw_api_ap = MWL8K_8764_AP_FW_API,
  4546. .ap_rxd_ops = &rxd_ap_ops,
  4547. },
  4548. };
  4549. MODULE_FIRMWARE("mwl8k/helper_8363.fw");
  4550. MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
  4551. MODULE_FIRMWARE("mwl8k/helper_8687.fw");
  4552. MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
  4553. MODULE_FIRMWARE("mwl8k/helper_8366.fw");
  4554. MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
  4555. MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
  4556. static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
  4557. { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
  4558. { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
  4559. { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
  4560. { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
  4561. { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
  4562. { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
  4563. { PCI_VDEVICE(MARVELL, 0x2a41), .driver_data = MWL8366, },
  4564. { PCI_VDEVICE(MARVELL, 0x2a42), .driver_data = MWL8366, },
  4565. { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
  4566. { PCI_VDEVICE(MARVELL, 0x2b36), .driver_data = MWL8764, },
  4567. { },
  4568. };
  4569. MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
  4570. static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
  4571. {
  4572. int rc;
  4573. printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
  4574. "Trying alternative firmware %s\n", pci_name(priv->pdev),
  4575. priv->fw_pref, priv->fw_alt);
  4576. rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
  4577. if (rc) {
  4578. printk(KERN_ERR "%s: Error requesting alt fw %s\n",
  4579. pci_name(priv->pdev), priv->fw_alt);
  4580. return rc;
  4581. }
  4582. return 0;
  4583. }
  4584. static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
  4585. static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
  4586. {
  4587. struct mwl8k_priv *priv = context;
  4588. struct mwl8k_device_info *di = priv->device_info;
  4589. int rc;
  4590. switch (priv->fw_state) {
  4591. case FW_STATE_INIT:
  4592. if (!fw) {
  4593. printk(KERN_ERR "%s: Error requesting helper fw %s\n",
  4594. pci_name(priv->pdev), di->helper_image);
  4595. goto fail;
  4596. }
  4597. priv->fw_helper = fw;
  4598. rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
  4599. true);
  4600. if (rc && priv->fw_alt) {
  4601. rc = mwl8k_request_alt_fw(priv);
  4602. if (rc)
  4603. goto fail;
  4604. priv->fw_state = FW_STATE_LOADING_ALT;
  4605. } else if (rc)
  4606. goto fail;
  4607. else
  4608. priv->fw_state = FW_STATE_LOADING_PREF;
  4609. break;
  4610. case FW_STATE_LOADING_PREF:
  4611. if (!fw) {
  4612. if (priv->fw_alt) {
  4613. rc = mwl8k_request_alt_fw(priv);
  4614. if (rc)
  4615. goto fail;
  4616. priv->fw_state = FW_STATE_LOADING_ALT;
  4617. } else
  4618. goto fail;
  4619. } else {
  4620. priv->fw_ucode = fw;
  4621. rc = mwl8k_firmware_load_success(priv);
  4622. if (rc)
  4623. goto fail;
  4624. else
  4625. complete(&priv->firmware_loading_complete);
  4626. }
  4627. break;
  4628. case FW_STATE_LOADING_ALT:
  4629. if (!fw) {
  4630. printk(KERN_ERR "%s: Error requesting alt fw %s\n",
  4631. pci_name(priv->pdev), di->helper_image);
  4632. goto fail;
  4633. }
  4634. priv->fw_ucode = fw;
  4635. rc = mwl8k_firmware_load_success(priv);
  4636. if (rc)
  4637. goto fail;
  4638. else
  4639. complete(&priv->firmware_loading_complete);
  4640. break;
  4641. default:
  4642. printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
  4643. MWL8K_NAME, priv->fw_state);
  4644. BUG_ON(1);
  4645. }
  4646. return;
  4647. fail:
  4648. priv->fw_state = FW_STATE_ERROR;
  4649. complete(&priv->firmware_loading_complete);
  4650. device_release_driver(&priv->pdev->dev);
  4651. mwl8k_release_firmware(priv);
  4652. }
  4653. #define MAX_RESTART_ATTEMPTS 1
  4654. static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
  4655. bool nowait)
  4656. {
  4657. struct mwl8k_priv *priv = hw->priv;
  4658. int rc;
  4659. int count = MAX_RESTART_ATTEMPTS;
  4660. retry:
  4661. /* Reset firmware and hardware */
  4662. mwl8k_hw_reset(priv);
  4663. /* Ask userland hotplug daemon for the device firmware */
  4664. rc = mwl8k_request_firmware(priv, fw_image, nowait);
  4665. if (rc) {
  4666. wiphy_err(hw->wiphy, "Firmware files not found\n");
  4667. return rc;
  4668. }
  4669. if (nowait)
  4670. return rc;
  4671. /* Load firmware into hardware */
  4672. rc = mwl8k_load_firmware(hw);
  4673. if (rc)
  4674. wiphy_err(hw->wiphy, "Cannot start firmware\n");
  4675. /* Reclaim memory once firmware is successfully loaded */
  4676. mwl8k_release_firmware(priv);
  4677. if (rc && count) {
  4678. /* FW did not start successfully;
  4679. * lets try one more time
  4680. */
  4681. count--;
  4682. wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
  4683. msleep(20);
  4684. goto retry;
  4685. }
  4686. return rc;
  4687. }
  4688. static int mwl8k_init_txqs(struct ieee80211_hw *hw)
  4689. {
  4690. struct mwl8k_priv *priv = hw->priv;
  4691. int rc = 0;
  4692. int i;
  4693. for (i = 0; i < mwl8k_tx_queues(priv); i++) {
  4694. rc = mwl8k_txq_init(hw, i);
  4695. if (rc)
  4696. break;
  4697. if (priv->ap_fw)
  4698. iowrite32(priv->txq[i].txd_dma,
  4699. priv->sram + priv->txq_offset[i]);
  4700. }
  4701. return rc;
  4702. }
  4703. /* initialize hw after successfully loading a firmware image */
  4704. static int mwl8k_probe_hw(struct ieee80211_hw *hw)
  4705. {
  4706. struct mwl8k_priv *priv = hw->priv;
  4707. int rc = 0;
  4708. int i;
  4709. if (priv->ap_fw) {
  4710. priv->rxd_ops = priv->device_info->ap_rxd_ops;
  4711. if (priv->rxd_ops == NULL) {
  4712. wiphy_err(hw->wiphy,
  4713. "Driver does not have AP firmware image support for this hardware\n");
  4714. rc = -ENOENT;
  4715. goto err_stop_firmware;
  4716. }
  4717. } else {
  4718. priv->rxd_ops = &rxd_sta_ops;
  4719. }
  4720. priv->sniffer_enabled = false;
  4721. priv->wmm_enabled = false;
  4722. priv->pending_tx_pkts = 0;
  4723. atomic_set(&priv->watchdog_event_pending, 0);
  4724. rc = mwl8k_rxq_init(hw, 0);
  4725. if (rc)
  4726. goto err_stop_firmware;
  4727. rxq_refill(hw, 0, INT_MAX);
  4728. /* For the sta firmware, we need to know the dma addresses of tx queues
  4729. * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
  4730. * prior to issuing this command. But for the AP case, we learn the
  4731. * total number of queues from the result CMD_GET_HW_SPEC, so for this
  4732. * case we must initialize the tx queues after.
  4733. */
  4734. priv->num_ampdu_queues = 0;
  4735. if (!priv->ap_fw) {
  4736. rc = mwl8k_init_txqs(hw);
  4737. if (rc)
  4738. goto err_free_queues;
  4739. }
  4740. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  4741. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4742. iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
  4743. MWL8K_A2H_INT_BA_WATCHDOG,
  4744. priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
  4745. iowrite32(MWL8K_A2H_INT_OPC_DONE,
  4746. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  4747. rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
  4748. IRQF_SHARED, MWL8K_NAME, hw);
  4749. if (rc) {
  4750. wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
  4751. goto err_free_queues;
  4752. }
  4753. /*
  4754. * When hw restart is requested,
  4755. * mac80211 will take care of clearing
  4756. * the ampdu streams, so do not clear
  4757. * the ampdu state here
  4758. */
  4759. if (!priv->hw_restart_in_progress)
  4760. memset(priv->ampdu, 0, sizeof(priv->ampdu));
  4761. /*
  4762. * Temporarily enable interrupts. Initial firmware host
  4763. * commands use interrupts and avoid polling. Disable
  4764. * interrupts when done.
  4765. */
  4766. iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4767. /* Get config data, mac addrs etc */
  4768. if (priv->ap_fw) {
  4769. rc = mwl8k_cmd_get_hw_spec_ap(hw);
  4770. if (!rc)
  4771. rc = mwl8k_init_txqs(hw);
  4772. if (!rc)
  4773. rc = mwl8k_cmd_set_hw_spec(hw);
  4774. } else {
  4775. rc = mwl8k_cmd_get_hw_spec_sta(hw);
  4776. }
  4777. if (rc) {
  4778. wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
  4779. goto err_free_irq;
  4780. }
  4781. /* Turn radio off */
  4782. rc = mwl8k_cmd_radio_disable(hw);
  4783. if (rc) {
  4784. wiphy_err(hw->wiphy, "Cannot disable\n");
  4785. goto err_free_irq;
  4786. }
  4787. /* Clear MAC address */
  4788. rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
  4789. if (rc) {
  4790. wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
  4791. goto err_free_irq;
  4792. }
  4793. /* Configure Antennas */
  4794. rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
  4795. if (rc)
  4796. wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
  4797. rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
  4798. if (rc)
  4799. wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
  4800. /* Disable interrupts */
  4801. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4802. free_irq(priv->pdev->irq, hw);
  4803. wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
  4804. priv->device_info->part_name,
  4805. priv->hw_rev, hw->wiphy->perm_addr,
  4806. priv->ap_fw ? "AP" : "STA",
  4807. (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
  4808. (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
  4809. return 0;
  4810. err_free_irq:
  4811. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4812. free_irq(priv->pdev->irq, hw);
  4813. err_free_queues:
  4814. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  4815. mwl8k_txq_deinit(hw, i);
  4816. mwl8k_rxq_deinit(hw, 0);
  4817. err_stop_firmware:
  4818. mwl8k_hw_reset(priv);
  4819. return rc;
  4820. }
  4821. /*
  4822. * invoke mwl8k_reload_firmware to change the firmware image after the device
  4823. * has already been registered
  4824. */
  4825. static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
  4826. {
  4827. int i, rc = 0;
  4828. struct mwl8k_priv *priv = hw->priv;
  4829. struct mwl8k_vif *vif, *tmp_vif;
  4830. mwl8k_stop(hw);
  4831. mwl8k_rxq_deinit(hw, 0);
  4832. /*
  4833. * All the existing interfaces are re-added by the ieee80211_reconfig;
  4834. * which means driver should remove existing interfaces before calling
  4835. * ieee80211_restart_hw
  4836. */
  4837. if (priv->hw_restart_in_progress)
  4838. list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
  4839. mwl8k_remove_vif(priv, vif);
  4840. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  4841. mwl8k_txq_deinit(hw, i);
  4842. rc = mwl8k_init_firmware(hw, fw_image, false);
  4843. if (rc)
  4844. goto fail;
  4845. rc = mwl8k_probe_hw(hw);
  4846. if (rc)
  4847. goto fail;
  4848. if (priv->hw_restart_in_progress)
  4849. return rc;
  4850. rc = mwl8k_start(hw);
  4851. if (rc)
  4852. goto fail;
  4853. rc = mwl8k_config(hw, ~0);
  4854. if (rc)
  4855. goto fail;
  4856. for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
  4857. rc = mwl8k_conf_tx(hw, NULL, i, &priv->wmm_params[i]);
  4858. if (rc)
  4859. goto fail;
  4860. }
  4861. return rc;
  4862. fail:
  4863. printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
  4864. return rc;
  4865. }
  4866. static const struct ieee80211_iface_limit ap_if_limits[] = {
  4867. { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
  4868. { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
  4869. };
  4870. static const struct ieee80211_iface_combination ap_if_comb = {
  4871. .limits = ap_if_limits,
  4872. .n_limits = ARRAY_SIZE(ap_if_limits),
  4873. .max_interfaces = 8,
  4874. .num_different_channels = 1,
  4875. };
  4876. static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
  4877. {
  4878. struct ieee80211_hw *hw = priv->hw;
  4879. int i, rc;
  4880. rc = mwl8k_load_firmware(hw);
  4881. mwl8k_release_firmware(priv);
  4882. if (rc) {
  4883. wiphy_err(hw->wiphy, "Cannot start firmware\n");
  4884. return rc;
  4885. }
  4886. /*
  4887. * Extra headroom is the size of the required DMA header
  4888. * minus the size of the smallest 802.11 frame (CTS frame).
  4889. */
  4890. hw->extra_tx_headroom =
  4891. sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
  4892. hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
  4893. hw->channel_change_time = 10;
  4894. hw->queues = MWL8K_TX_WMM_QUEUES;
  4895. /* Set rssi values to dBm */
  4896. hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
  4897. /*
  4898. * Ask mac80211 to not to trigger PS mode
  4899. * based on PM bit of incoming frames.
  4900. */
  4901. if (priv->ap_fw)
  4902. hw->flags |= IEEE80211_HW_AP_LINK_PS;
  4903. hw->vif_data_size = sizeof(struct mwl8k_vif);
  4904. hw->sta_data_size = sizeof(struct mwl8k_sta);
  4905. priv->macids_used = 0;
  4906. INIT_LIST_HEAD(&priv->vif_list);
  4907. /* Set default radio state and preamble */
  4908. priv->radio_on = false;
  4909. priv->radio_short_preamble = false;
  4910. /* Finalize join worker */
  4911. INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
  4912. /* Handle watchdog ba events */
  4913. INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
  4914. /* To reload the firmware if it crashes */
  4915. INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
  4916. /* TX reclaim and RX tasklets. */
  4917. tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
  4918. tasklet_disable(&priv->poll_tx_task);
  4919. tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
  4920. tasklet_disable(&priv->poll_rx_task);
  4921. /* Power management cookie */
  4922. priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
  4923. if (priv->cookie == NULL)
  4924. return -ENOMEM;
  4925. mutex_init(&priv->fw_mutex);
  4926. priv->fw_mutex_owner = NULL;
  4927. priv->fw_mutex_depth = 0;
  4928. priv->hostcmd_wait = NULL;
  4929. spin_lock_init(&priv->tx_lock);
  4930. spin_lock_init(&priv->stream_lock);
  4931. priv->tx_wait = NULL;
  4932. rc = mwl8k_probe_hw(hw);
  4933. if (rc)
  4934. goto err_free_cookie;
  4935. hw->wiphy->interface_modes = 0;
  4936. if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
  4937. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
  4938. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
  4939. hw->wiphy->iface_combinations = &ap_if_comb;
  4940. hw->wiphy->n_iface_combinations = 1;
  4941. }
  4942. if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
  4943. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
  4944. rc = ieee80211_register_hw(hw);
  4945. if (rc) {
  4946. wiphy_err(hw->wiphy, "Cannot register device\n");
  4947. goto err_unprobe_hw;
  4948. }
  4949. return 0;
  4950. err_unprobe_hw:
  4951. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  4952. mwl8k_txq_deinit(hw, i);
  4953. mwl8k_rxq_deinit(hw, 0);
  4954. err_free_cookie:
  4955. if (priv->cookie != NULL)
  4956. pci_free_consistent(priv->pdev, 4,
  4957. priv->cookie, priv->cookie_dma);
  4958. return rc;
  4959. }
  4960. static int mwl8k_probe(struct pci_dev *pdev,
  4961. const struct pci_device_id *id)
  4962. {
  4963. static int printed_version;
  4964. struct ieee80211_hw *hw;
  4965. struct mwl8k_priv *priv;
  4966. struct mwl8k_device_info *di;
  4967. int rc;
  4968. if (!printed_version) {
  4969. printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
  4970. printed_version = 1;
  4971. }
  4972. rc = pci_enable_device(pdev);
  4973. if (rc) {
  4974. printk(KERN_ERR "%s: Cannot enable new PCI device\n",
  4975. MWL8K_NAME);
  4976. return rc;
  4977. }
  4978. rc = pci_request_regions(pdev, MWL8K_NAME);
  4979. if (rc) {
  4980. printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
  4981. MWL8K_NAME);
  4982. goto err_disable_device;
  4983. }
  4984. pci_set_master(pdev);
  4985. hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
  4986. if (hw == NULL) {
  4987. printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
  4988. rc = -ENOMEM;
  4989. goto err_free_reg;
  4990. }
  4991. SET_IEEE80211_DEV(hw, &pdev->dev);
  4992. pci_set_drvdata(pdev, hw);
  4993. priv = hw->priv;
  4994. priv->hw = hw;
  4995. priv->pdev = pdev;
  4996. priv->device_info = &mwl8k_info_tbl[id->driver_data];
  4997. if (id->driver_data == MWL8764)
  4998. priv->is_8764 = true;
  4999. priv->sram = pci_iomap(pdev, 0, 0x10000);
  5000. if (priv->sram == NULL) {
  5001. wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
  5002. rc = -EIO;
  5003. goto err_iounmap;
  5004. }
  5005. /*
  5006. * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
  5007. * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
  5008. */
  5009. priv->regs = pci_iomap(pdev, 1, 0x10000);
  5010. if (priv->regs == NULL) {
  5011. priv->regs = pci_iomap(pdev, 2, 0x10000);
  5012. if (priv->regs == NULL) {
  5013. wiphy_err(hw->wiphy, "Cannot map device registers\n");
  5014. rc = -EIO;
  5015. goto err_iounmap;
  5016. }
  5017. }
  5018. /*
  5019. * Choose the initial fw image depending on user input. If a second
  5020. * image is available, make it the alternative image that will be
  5021. * loaded if the first one fails.
  5022. */
  5023. init_completion(&priv->firmware_loading_complete);
  5024. di = priv->device_info;
  5025. if (ap_mode_default && di->fw_image_ap) {
  5026. priv->fw_pref = di->fw_image_ap;
  5027. priv->fw_alt = di->fw_image_sta;
  5028. } else if (!ap_mode_default && di->fw_image_sta) {
  5029. priv->fw_pref = di->fw_image_sta;
  5030. priv->fw_alt = di->fw_image_ap;
  5031. } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
  5032. printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
  5033. priv->fw_pref = di->fw_image_sta;
  5034. } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
  5035. printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
  5036. priv->fw_pref = di->fw_image_ap;
  5037. }
  5038. rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
  5039. if (rc)
  5040. goto err_stop_firmware;
  5041. priv->hw_restart_in_progress = false;
  5042. priv->running_bsses = 0;
  5043. return rc;
  5044. err_stop_firmware:
  5045. mwl8k_hw_reset(priv);
  5046. err_iounmap:
  5047. if (priv->regs != NULL)
  5048. pci_iounmap(pdev, priv->regs);
  5049. if (priv->sram != NULL)
  5050. pci_iounmap(pdev, priv->sram);
  5051. pci_set_drvdata(pdev, NULL);
  5052. ieee80211_free_hw(hw);
  5053. err_free_reg:
  5054. pci_release_regions(pdev);
  5055. err_disable_device:
  5056. pci_disable_device(pdev);
  5057. return rc;
  5058. }
  5059. static void mwl8k_remove(struct pci_dev *pdev)
  5060. {
  5061. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  5062. struct mwl8k_priv *priv;
  5063. int i;
  5064. if (hw == NULL)
  5065. return;
  5066. priv = hw->priv;
  5067. wait_for_completion(&priv->firmware_loading_complete);
  5068. if (priv->fw_state == FW_STATE_ERROR) {
  5069. mwl8k_hw_reset(priv);
  5070. goto unmap;
  5071. }
  5072. ieee80211_stop_queues(hw);
  5073. ieee80211_unregister_hw(hw);
  5074. /* Remove TX reclaim and RX tasklets. */
  5075. tasklet_kill(&priv->poll_tx_task);
  5076. tasklet_kill(&priv->poll_rx_task);
  5077. /* Stop hardware */
  5078. mwl8k_hw_reset(priv);
  5079. /* Return all skbs to mac80211 */
  5080. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  5081. mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
  5082. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  5083. mwl8k_txq_deinit(hw, i);
  5084. mwl8k_rxq_deinit(hw, 0);
  5085. pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
  5086. unmap:
  5087. pci_iounmap(pdev, priv->regs);
  5088. pci_iounmap(pdev, priv->sram);
  5089. pci_set_drvdata(pdev, NULL);
  5090. ieee80211_free_hw(hw);
  5091. pci_release_regions(pdev);
  5092. pci_disable_device(pdev);
  5093. }
  5094. static struct pci_driver mwl8k_driver = {
  5095. .name = MWL8K_NAME,
  5096. .id_table = mwl8k_pci_id_table,
  5097. .probe = mwl8k_probe,
  5098. .remove = mwl8k_remove,
  5099. };
  5100. module_pci_driver(mwl8k_driver);
  5101. MODULE_DESCRIPTION(MWL8K_DESC);
  5102. MODULE_VERSION(MWL8K_VERSION);
  5103. MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
  5104. MODULE_LICENSE("GPL");