2 * Atheros AR9170 driver
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, see
20 * http://www.gnu.org/licenses/.
22 * This file incorporates work covered by the following copyright and
24 * Copyright (c) 2007-2008 Atheros Communications, Inc.
26 * Permission to use, copy, modify, and/or distribute this software for any
27 * purpose with or without fee is hereby granted, provided that the above
28 * copyright notice and this permission notice appear in all copies.
30 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
31 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
32 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
33 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
34 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
35 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
36 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
39 #include <asm/unaligned.h>
44 int ar9170_set_dyn_sifs_ack(struct ar9170
*ar
)
48 if (conf_is_ht40(&ar
->hw
->conf
))
51 if (ar
->hw
->conf
.channel
->band
== IEEE80211_BAND_2GHZ
)
57 return ar9170_write_reg(ar
, AR9170_MAC_REG_DYNAMIC_SIFS_ACK
, val
);
60 int ar9170_set_slot_time(struct ar9170
*ar
)
67 if ((ar
->hw
->conf
.channel
->band
== IEEE80211_BAND_5GHZ
) ||
68 ar
->vif
->bss_conf
.use_short_slot
)
71 return ar9170_write_reg(ar
, AR9170_MAC_REG_SLOT_TIME
, slottime
<< 10);
74 int ar9170_set_basic_rates(struct ar9170
*ar
)
81 ofdm
= ar
->vif
->bss_conf
.basic_rates
>> 4;
83 /* FIXME: is still necessary? */
84 if (ar
->hw
->conf
.channel
->band
== IEEE80211_BAND_5GHZ
)
87 cck
= ar
->vif
->bss_conf
.basic_rates
& 0xf;
89 return ar9170_write_reg(ar
, AR9170_MAC_REG_BASIC_RATE
,
93 int ar9170_set_qos(struct ar9170
*ar
)
95 ar9170_regwrite_begin(ar
);
97 ar9170_regwrite(AR9170_MAC_REG_AC0_CW
, ar
->edcf
[0].cw_min
|
98 (ar
->edcf
[0].cw_max
<< 16));
99 ar9170_regwrite(AR9170_MAC_REG_AC1_CW
, ar
->edcf
[1].cw_min
|
100 (ar
->edcf
[1].cw_max
<< 16));
101 ar9170_regwrite(AR9170_MAC_REG_AC2_CW
, ar
->edcf
[2].cw_min
|
102 (ar
->edcf
[2].cw_max
<< 16));
103 ar9170_regwrite(AR9170_MAC_REG_AC3_CW
, ar
->edcf
[3].cw_min
|
104 (ar
->edcf
[3].cw_max
<< 16));
105 ar9170_regwrite(AR9170_MAC_REG_AC4_CW
, ar
->edcf
[4].cw_min
|
106 (ar
->edcf
[4].cw_max
<< 16));
108 ar9170_regwrite(AR9170_MAC_REG_AC1_AC0_AIFS
,
109 ((ar
->edcf
[0].aifs
* 9 + 10)) |
110 ((ar
->edcf
[1].aifs
* 9 + 10) << 12) |
111 ((ar
->edcf
[2].aifs
* 9 + 10) << 24));
112 ar9170_regwrite(AR9170_MAC_REG_AC3_AC2_AIFS
,
113 ((ar
->edcf
[2].aifs
* 9 + 10) >> 8) |
114 ((ar
->edcf
[3].aifs
* 9 + 10) << 4) |
115 ((ar
->edcf
[4].aifs
* 9 + 10) << 16));
117 ar9170_regwrite(AR9170_MAC_REG_AC1_AC0_TXOP
,
118 ar
->edcf
[0].txop
| ar
->edcf
[1].txop
<< 16);
119 ar9170_regwrite(AR9170_MAC_REG_AC3_AC2_TXOP
,
120 ar
->edcf
[2].txop
| ar
->edcf
[3].txop
<< 16);
122 ar9170_regwrite_finish();
124 return ar9170_regwrite_result();
127 static int ar9170_set_ampdu_density(struct ar9170
*ar
, u8 mpdudensity
)
131 /* don't allow AMPDU density > 8us */
135 /* Watch out! Otus uses slightly different density values. */
136 val
= 0x140a00 | (mpdudensity
? (mpdudensity
+ 1) : 0);
138 ar9170_regwrite_begin(ar
);
139 ar9170_regwrite(AR9170_MAC_REG_AMPDU_DENSITY
, val
);
140 ar9170_regwrite_finish();
142 return ar9170_regwrite_result();
145 int ar9170_init_mac(struct ar9170
*ar
)
147 ar9170_regwrite_begin(ar
);
149 ar9170_regwrite(AR9170_MAC_REG_ACK_EXTENSION
, 0x40);
151 ar9170_regwrite(AR9170_MAC_REG_RETRY_MAX
, 0);
154 ar9170_regwrite(AR9170_MAC_REG_SNIFFER
,
155 AR9170_MAC_REG_SNIFFER_DEFAULTS
);
157 ar9170_regwrite(AR9170_MAC_REG_RX_THRESHOLD
, 0xc1f80);
159 ar9170_regwrite(AR9170_MAC_REG_RX_PE_DELAY
, 0x70);
160 ar9170_regwrite(AR9170_MAC_REG_EIFS_AND_SIFS
, 0xa144000);
161 ar9170_regwrite(AR9170_MAC_REG_SLOT_TIME
, 9 << 10);
164 ar9170_regwrite(0x1c3b2c, 0x19000000);
166 /* NAV protects ACK only (in TXOP) */
167 ar9170_regwrite(0x1c3b38, 0x201);
169 /* Set Beacon PHY CTRL's TPC to 0x7, TA1=1 */
170 /* OTUS set AM to 0x1 */
171 ar9170_regwrite(AR9170_MAC_REG_BCN_HT1
, 0x8000170);
173 ar9170_regwrite(AR9170_MAC_REG_BACKOFF_PROTECT
, 0x105);
176 /* Aggregation MAX number and timeout */
177 ar9170_regwrite(0x1c3b9c, 0x10000a);
179 ar9170_regwrite(AR9170_MAC_REG_FRAMETYPE_FILTER
,
180 AR9170_MAC_REG_FTF_DEFAULTS
);
182 /* Enable deaggregator, response in sniffer mode */
183 ar9170_regwrite(0x1c3c40, 0x1 | 1<<30);
186 ar9170_regwrite(AR9170_MAC_REG_BASIC_RATE
, 0x150f);
187 ar9170_regwrite(AR9170_MAC_REG_MANDATORY_RATE
, 0x150f);
188 ar9170_regwrite(AR9170_MAC_REG_RTS_CTS_RATE
, 0x10b01bb);
190 /* MIMO response control */
191 ar9170_regwrite(0x1c3694, 0x4003C1E);/* bit 26~28 otus-AM */
193 /* switch MAC to OTUS interface */
194 ar9170_regwrite(0x1c3600, 0x3);
196 ar9170_regwrite(AR9170_MAC_REG_AMPDU_RX_THRESH
, 0xffff);
198 /* set PHY register read timeout (??) */
199 ar9170_regwrite(AR9170_MAC_REG_MISC_680
, 0xf00008);
201 /* Disable Rx TimeOut, workaround for BB. */
202 ar9170_regwrite(AR9170_MAC_REG_RX_TIMEOUT
, 0x0);
204 /* Set CPU clock frequency to 88/80MHz */
205 ar9170_regwrite(AR9170_PWR_REG_CLOCK_SEL
,
206 AR9170_PWR_CLK_AHB_80_88MHZ
|
207 AR9170_PWR_CLK_DAC_160_INV_DLY
);
209 /* Set WLAN DMA interrupt mode: generate int per packet */
210 ar9170_regwrite(AR9170_MAC_REG_TXRX_MPI
, 0x110011);
212 ar9170_regwrite(AR9170_MAC_REG_FCS_SELECT
,
213 AR9170_MAC_FCS_FIFO_PROT
);
215 /* Disables the CF_END frame, undocumented register */
216 ar9170_regwrite(AR9170_MAC_REG_TXOP_NOT_ENOUGH_IND
,
219 ar9170_regwrite_finish();
221 return ar9170_regwrite_result();
224 static int ar9170_set_mac_reg(struct ar9170
*ar
, const u32 reg
, const u8
*mac
)
226 static const u8 zero
[ETH_ALEN
] = { 0 };
231 ar9170_regwrite_begin(ar
);
233 ar9170_regwrite(reg
, get_unaligned_le32(mac
));
234 ar9170_regwrite(reg
+ 4, get_unaligned_le16(mac
+ 4));
236 ar9170_regwrite_finish();
238 return ar9170_regwrite_result();
241 int ar9170_update_multicast(struct ar9170
*ar
, const u64 mc_hash
)
245 ar9170_regwrite_begin(ar
);
246 ar9170_regwrite(AR9170_MAC_REG_GROUP_HASH_TBL_H
, mc_hash
>> 32);
247 ar9170_regwrite(AR9170_MAC_REG_GROUP_HASH_TBL_L
, mc_hash
);
248 ar9170_regwrite_finish();
249 err
= ar9170_regwrite_result();
253 ar
->cur_mc_hash
= mc_hash
;
257 int ar9170_update_frame_filter(struct ar9170
*ar
, const u32 filter
)
261 err
= ar9170_write_reg(ar
, AR9170_MAC_REG_FRAMETYPE_FILTER
, filter
);
265 ar
->cur_filter
= filter
;
269 static int ar9170_set_promiscouous(struct ar9170
*ar
)
271 u32 encr_mode
, sniffer
;
274 err
= ar9170_read_reg(ar
, AR9170_MAC_REG_SNIFFER
, &sniffer
);
278 err
= ar9170_read_reg(ar
, AR9170_MAC_REG_ENCRYPTION
, &encr_mode
);
282 if (ar
->sniffer_enabled
) {
283 sniffer
|= AR9170_MAC_REG_SNIFFER_ENABLE_PROMISC
;
286 * Rx decryption works in place.
288 * If we don't disable it, the hardware will render all
289 * encrypted frames which are encrypted with an unknown
293 encr_mode
|= AR9170_MAC_REG_ENCRYPTION_RX_SOFTWARE
;
294 ar
->sniffer_enabled
= true;
296 sniffer
&= ~AR9170_MAC_REG_SNIFFER_ENABLE_PROMISC
;
298 if (ar
->rx_software_decryption
)
299 encr_mode
|= AR9170_MAC_REG_ENCRYPTION_RX_SOFTWARE
;
301 encr_mode
&= ~AR9170_MAC_REG_ENCRYPTION_RX_SOFTWARE
;
304 ar9170_regwrite_begin(ar
);
305 ar9170_regwrite(AR9170_MAC_REG_ENCRYPTION
, encr_mode
);
306 ar9170_regwrite(AR9170_MAC_REG_SNIFFER
, sniffer
);
307 ar9170_regwrite_finish();
309 return ar9170_regwrite_result();
312 int ar9170_set_operating_mode(struct ar9170
*ar
)
314 struct ath_common
*common
= &ar
->common
;
315 u32 pm_mode
= AR9170_MAC_REG_POWERMGT_DEFAULTS
;
316 u8
*mac_addr
, *bssid
;
320 mac_addr
= common
->macaddr
;
321 bssid
= common
->curbssid
;
323 switch (ar
->vif
->type
) {
324 case NL80211_IFTYPE_MESH_POINT
:
325 case NL80211_IFTYPE_ADHOC
:
326 pm_mode
|= AR9170_MAC_REG_POWERMGT_IBSS
;
328 case NL80211_IFTYPE_AP
:
329 pm_mode
|= AR9170_MAC_REG_POWERMGT_AP
;
331 case NL80211_IFTYPE_WDS
:
332 pm_mode
|= AR9170_MAC_REG_POWERMGT_AP_WDS
;
334 case NL80211_IFTYPE_MONITOR
:
335 ar
->sniffer_enabled
= true;
336 ar
->rx_software_decryption
= true;
339 pm_mode
|= AR9170_MAC_REG_POWERMGT_STA
;
347 err
= ar9170_set_mac_reg(ar
, AR9170_MAC_REG_MAC_ADDR_L
, mac_addr
);
351 err
= ar9170_set_mac_reg(ar
, AR9170_MAC_REG_BSSID_L
, bssid
);
355 err
= ar9170_set_promiscouous(ar
);
359 /* set AMPDU density to 8us. */
360 err
= ar9170_set_ampdu_density(ar
, 6);
364 ar9170_regwrite_begin(ar
);
366 ar9170_regwrite(AR9170_MAC_REG_POWERMANAGEMENT
, pm_mode
);
367 ar9170_regwrite_finish();
369 return ar9170_regwrite_result();
372 int ar9170_set_hwretry_limit(struct ar9170
*ar
, unsigned int max_retry
)
374 u32 tmp
= min_t(u32
, 0x33333, max_retry
* 0x11111);
376 return ar9170_write_reg(ar
, AR9170_MAC_REG_RETRY_MAX
, tmp
);
379 int ar9170_set_beacon_timers(struct ar9170
*ar
)
385 v
|= ar
->vif
->bss_conf
.beacon_int
;
387 if (ar
->enable_beacon
) {
388 switch (ar
->vif
->type
) {
389 case NL80211_IFTYPE_MESH_POINT
:
390 case NL80211_IFTYPE_ADHOC
:
393 case NL80211_IFTYPE_AP
:
395 pretbtt
= (ar
->vif
->bss_conf
.beacon_int
- 6) <<
403 v
|= ar
->vif
->bss_conf
.dtim_period
<< 16;
406 ar9170_regwrite_begin(ar
);
407 ar9170_regwrite(AR9170_MAC_REG_PRETBTT
, pretbtt
);
408 ar9170_regwrite(AR9170_MAC_REG_BCN_PERIOD
, v
);
409 ar9170_regwrite_finish();
410 return ar9170_regwrite_result();
413 int ar9170_update_beacon(struct ar9170
*ar
)
416 __le32
*data
, *old
= NULL
;
420 skb
= ieee80211_beacon_get(ar
->hw
, ar
->vif
);
424 data
= (__le32
*)skb
->data
;
426 old
= (__le32
*)ar
->beacon
->data
;
428 ar9170_regwrite_begin(ar
);
429 for (i
= 0; i
< DIV_ROUND_UP(skb
->len
, 4); i
++) {
431 * XXX: This accesses beyond skb data for up
432 * to the last 3 bytes!!
435 if (old
&& (data
[i
] == old
[i
]))
438 word
= le32_to_cpu(data
[i
]);
439 ar9170_regwrite(AR9170_BEACON_BUFFER_ADDRESS
+ 4 * i
, word
);
442 /* XXX: use skb->cb info */
443 if (ar
->hw
->conf
.channel
->band
== IEEE80211_BAND_2GHZ
)
444 ar9170_regwrite(AR9170_MAC_REG_BCN_PLCP
,
445 ((skb
->len
+ 4) << (3 + 16)) + 0x0400);
447 ar9170_regwrite(AR9170_MAC_REG_BCN_PLCP
,
448 ((skb
->len
+ 4) << 16) + 0x001b);
450 ar9170_regwrite(AR9170_MAC_REG_BCN_LENGTH
, skb
->len
+ 4);
451 ar9170_regwrite(AR9170_MAC_REG_BCN_ADDR
, AR9170_BEACON_BUFFER_ADDRESS
);
452 ar9170_regwrite(AR9170_MAC_REG_BCN_CTRL
, 1);
454 ar9170_regwrite_finish();
456 dev_kfree_skb(ar
->beacon
);
459 return ar9170_regwrite_result();
462 void ar9170_new_beacon(struct work_struct
*work
)
464 struct ar9170
*ar
= container_of(work
, struct ar9170
,
468 if (unlikely(!IS_STARTED(ar
)))
471 mutex_lock(&ar
->mutex
);
476 ar9170_update_beacon(ar
);
479 while ((skb
= ieee80211_get_buffered_bc(ar
->hw
, ar
->vif
)))
480 ar9170_op_tx(ar
->hw
, skb
);
485 mutex_unlock(&ar
->mutex
);
488 int ar9170_upload_key(struct ar9170
*ar
, u8 id
, const u8
*mac
, u8 ktype
,
489 u8 keyidx
, u8
*keydata
, int keylen
)
492 static const u8 bcast
[ETH_ALEN
] =
493 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
498 vals
[0] = cpu_to_le32((keyidx
<< 16) + id
);
499 vals
[1] = cpu_to_le32(mac
[1] << 24 | mac
[0] << 16 | ktype
);
500 vals
[2] = cpu_to_le32(mac
[5] << 24 | mac
[4] << 16 |
501 mac
[3] << 8 | mac
[2]);
502 memset(&vals
[3], 0, 16);
504 memcpy(&vals
[3], keydata
, keylen
);
506 return ar
->exec_cmd(ar
, AR9170_CMD_EKEY
,
507 sizeof(vals
), (u8
*)vals
,
511 int ar9170_disable_key(struct ar9170
*ar
, u8 id
)
513 __le32 val
= cpu_to_le32(id
);
516 return ar
->exec_cmd(ar
, AR9170_CMD_EKEY
,
517 sizeof(val
), (u8
*)&val
,