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.
41 int ar9170_set_dyn_sifs_ack(struct ar9170
*ar
)
45 if (conf_is_ht40(&ar
->hw
->conf
))
48 if (ar
->hw
->conf
.channel
->band
== IEEE80211_BAND_2GHZ
)
54 return ar9170_write_reg(ar
, AR9170_MAC_REG_DYNAMIC_SIFS_ACK
, val
);
57 int ar9170_set_slot_time(struct ar9170
*ar
)
64 if ((ar
->hw
->conf
.channel
->band
== IEEE80211_BAND_5GHZ
) ||
65 ar
->vif
->bss_conf
.use_short_slot
)
68 return ar9170_write_reg(ar
, AR9170_MAC_REG_SLOT_TIME
, slottime
<< 10);
71 int ar9170_set_basic_rates(struct ar9170
*ar
)
78 ofdm
= ar
->vif
->bss_conf
.basic_rates
>> 4;
80 /* FIXME: is still necessary? */
81 if (ar
->hw
->conf
.channel
->band
== IEEE80211_BAND_5GHZ
)
84 cck
= ar
->vif
->bss_conf
.basic_rates
& 0xf;
86 return ar9170_write_reg(ar
, AR9170_MAC_REG_BASIC_RATE
,
90 int ar9170_set_qos(struct ar9170
*ar
)
92 ar9170_regwrite_begin(ar
);
94 ar9170_regwrite(AR9170_MAC_REG_AC0_CW
, ar
->edcf
[0].cw_min
|
95 (ar
->edcf
[0].cw_max
<< 16));
96 ar9170_regwrite(AR9170_MAC_REG_AC1_CW
, ar
->edcf
[1].cw_min
|
97 (ar
->edcf
[1].cw_max
<< 16));
98 ar9170_regwrite(AR9170_MAC_REG_AC2_CW
, ar
->edcf
[2].cw_min
|
99 (ar
->edcf
[2].cw_max
<< 16));
100 ar9170_regwrite(AR9170_MAC_REG_AC3_CW
, ar
->edcf
[3].cw_min
|
101 (ar
->edcf
[3].cw_max
<< 16));
102 ar9170_regwrite(AR9170_MAC_REG_AC4_CW
, ar
->edcf
[4].cw_min
|
103 (ar
->edcf
[4].cw_max
<< 16));
105 ar9170_regwrite(AR9170_MAC_REG_AC1_AC0_AIFS
,
106 ((ar
->edcf
[0].aifs
* 9 + 10)) |
107 ((ar
->edcf
[1].aifs
* 9 + 10) << 12) |
108 ((ar
->edcf
[2].aifs
* 9 + 10) << 24));
109 ar9170_regwrite(AR9170_MAC_REG_AC3_AC2_AIFS
,
110 ((ar
->edcf
[2].aifs
* 9 + 10) >> 8) |
111 ((ar
->edcf
[3].aifs
* 9 + 10) << 4) |
112 ((ar
->edcf
[4].aifs
* 9 + 10) << 16));
114 ar9170_regwrite(AR9170_MAC_REG_AC1_AC0_TXOP
,
115 ar
->edcf
[0].txop
| ar
->edcf
[1].txop
<< 16);
116 ar9170_regwrite(AR9170_MAC_REG_AC3_AC2_TXOP
,
117 ar
->edcf
[1].txop
| ar
->edcf
[3].txop
<< 16);
119 ar9170_regwrite_finish();
121 return ar9170_regwrite_result();
124 static int ar9170_set_ampdu_density(struct ar9170
*ar
, u8 mpdudensity
)
128 /* don't allow AMPDU density > 8us */
132 /* Watch out! Otus uses slightly different density values. */
133 val
= 0x140a00 | (mpdudensity
? (mpdudensity
+ 1) : 0);
135 ar9170_regwrite_begin(ar
);
136 ar9170_regwrite(AR9170_MAC_REG_AMPDU_DENSITY
, val
);
137 ar9170_regwrite_finish();
139 return ar9170_regwrite_result();
142 int ar9170_init_mac(struct ar9170
*ar
)
144 ar9170_regwrite_begin(ar
);
146 ar9170_regwrite(AR9170_MAC_REG_ACK_EXTENSION
, 0x40);
148 ar9170_regwrite(AR9170_MAC_REG_RETRY_MAX
, 0);
151 ar9170_regwrite(AR9170_MAC_REG_SNIFFER
,
152 AR9170_MAC_REG_SNIFFER_DEFAULTS
);
154 ar9170_regwrite(AR9170_MAC_REG_RX_THRESHOLD
, 0xc1f80);
156 ar9170_regwrite(AR9170_MAC_REG_RX_PE_DELAY
, 0x70);
157 ar9170_regwrite(AR9170_MAC_REG_EIFS_AND_SIFS
, 0xa144000);
158 ar9170_regwrite(AR9170_MAC_REG_SLOT_TIME
, 9 << 10);
161 ar9170_regwrite(0x1c3b2c, 0x19000000);
163 /* NAV protects ACK only (in TXOP) */
164 ar9170_regwrite(0x1c3b38, 0x201);
166 /* Set Beacon PHY CTRL's TPC to 0x7, TA1=1 */
167 /* OTUS set AM to 0x1 */
168 ar9170_regwrite(AR9170_MAC_REG_BCN_HT1
, 0x8000170);
170 ar9170_regwrite(AR9170_MAC_REG_BACKOFF_PROTECT
, 0x105);
173 /* Aggregation MAX number and timeout */
174 ar9170_regwrite(0x1c3b9c, 0x10000a);
176 ar9170_regwrite(AR9170_MAC_REG_FRAMETYPE_FILTER
,
177 AR9170_MAC_REG_FTF_DEFAULTS
);
179 /* Enable deaggregator, response in sniffer mode */
180 ar9170_regwrite(0x1c3c40, 0x1 | 1<<30);
183 ar9170_regwrite(AR9170_MAC_REG_BASIC_RATE
, 0x150f);
184 ar9170_regwrite(AR9170_MAC_REG_MANDATORY_RATE
, 0x150f);
185 ar9170_regwrite(AR9170_MAC_REG_RTS_CTS_RATE
, 0x10b01bb);
187 /* MIMO response control */
188 ar9170_regwrite(0x1c3694, 0x4003C1E);/* bit 26~28 otus-AM */
190 /* switch MAC to OTUS interface */
191 ar9170_regwrite(0x1c3600, 0x3);
193 ar9170_regwrite(AR9170_MAC_REG_AMPDU_RX_THRESH
, 0xffff);
195 /* set PHY register read timeout (??) */
196 ar9170_regwrite(AR9170_MAC_REG_MISC_680
, 0xf00008);
198 /* Disable Rx TimeOut, workaround for BB. */
199 ar9170_regwrite(AR9170_MAC_REG_RX_TIMEOUT
, 0x0);
201 /* Set CPU clock frequency to 88/80MHz */
202 ar9170_regwrite(AR9170_PWR_REG_CLOCK_SEL
,
203 AR9170_PWR_CLK_AHB_80_88MHZ
|
204 AR9170_PWR_CLK_DAC_160_INV_DLY
);
206 /* Set WLAN DMA interrupt mode: generate int per packet */
207 ar9170_regwrite(AR9170_MAC_REG_TXRX_MPI
, 0x110011);
209 ar9170_regwrite(AR9170_MAC_REG_FCS_SELECT
,
210 AR9170_MAC_FCS_FIFO_PROT
);
212 /* Disables the CF_END frame, undocumented register */
213 ar9170_regwrite(AR9170_MAC_REG_TXOP_NOT_ENOUGH_IND
,
216 ar9170_regwrite_finish();
218 return ar9170_regwrite_result();
221 static int ar9170_set_mac_reg(struct ar9170
*ar
, const u32 reg
, const u8
*mac
)
223 static const u8 zero
[ETH_ALEN
] = { 0 };
228 ar9170_regwrite_begin(ar
);
231 (mac
[3] << 24) | (mac
[2] << 16) |
232 (mac
[1] << 8) | mac
[0]);
234 ar9170_regwrite(reg
+ 4, (mac
[5] << 8) | 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 u32 pm_mode
= AR9170_MAC_REG_POWERMGT_DEFAULTS
;
315 u8
*mac_addr
, *bssid
;
319 mac_addr
= ar
->mac_addr
;
322 switch (ar
->vif
->type
) {
323 case NL80211_IFTYPE_MESH_POINT
:
324 case NL80211_IFTYPE_ADHOC
:
325 pm_mode
|= AR9170_MAC_REG_POWERMGT_IBSS
;
327 case NL80211_IFTYPE_AP
:
328 pm_mode
|= AR9170_MAC_REG_POWERMGT_AP
;
330 case NL80211_IFTYPE_WDS
:
331 pm_mode
|= AR9170_MAC_REG_POWERMGT_AP_WDS
;
333 case NL80211_IFTYPE_MONITOR
:
334 ar
->sniffer_enabled
= true;
335 ar
->rx_software_decryption
= true;
338 pm_mode
|= AR9170_MAC_REG_POWERMGT_STA
;
346 err
= ar9170_set_mac_reg(ar
, AR9170_MAC_REG_MAC_ADDR_L
, mac_addr
);
350 err
= ar9170_set_mac_reg(ar
, AR9170_MAC_REG_BSSID_L
, bssid
);
354 err
= ar9170_set_promiscouous(ar
);
358 /* set AMPDU density to 8us. */
359 err
= ar9170_set_ampdu_density(ar
, 6);
363 ar9170_regwrite_begin(ar
);
365 ar9170_regwrite(AR9170_MAC_REG_POWERMANAGEMENT
, pm_mode
);
366 ar9170_regwrite_finish();
368 return ar9170_regwrite_result();
371 int ar9170_set_hwretry_limit(struct ar9170
*ar
, unsigned int max_retry
)
373 u32 tmp
= min_t(u32
, 0x33333, max_retry
* 0x11111);
375 return ar9170_write_reg(ar
, AR9170_MAC_REG_RETRY_MAX
, tmp
);
378 int ar9170_set_beacon_timers(struct ar9170
*ar
)
384 v
|= ar
->vif
->bss_conf
.beacon_int
;
386 if (ar
->enable_beacon
) {
387 switch (ar
->vif
->type
) {
388 case NL80211_IFTYPE_MESH_POINT
:
389 case NL80211_IFTYPE_ADHOC
:
392 case NL80211_IFTYPE_AP
:
394 pretbtt
= (ar
->vif
->bss_conf
.beacon_int
- 6) <<
402 v
|= ar
->vif
->bss_conf
.dtim_period
<< 16;
405 ar9170_regwrite_begin(ar
);
406 ar9170_regwrite(AR9170_MAC_REG_PRETBTT
, pretbtt
);
407 ar9170_regwrite(AR9170_MAC_REG_BCN_PERIOD
, v
);
408 ar9170_regwrite_finish();
409 return ar9170_regwrite_result();
412 int ar9170_update_beacon(struct ar9170
*ar
)
415 __le32
*data
, *old
= NULL
;
419 skb
= ieee80211_beacon_get(ar
->hw
, ar
->vif
);
423 data
= (__le32
*)skb
->data
;
425 old
= (__le32
*)ar
->beacon
->data
;
427 ar9170_regwrite_begin(ar
);
428 for (i
= 0; i
< DIV_ROUND_UP(skb
->len
, 4); i
++) {
430 * XXX: This accesses beyond skb data for up
431 * to the last 3 bytes!!
434 if (old
&& (data
[i
] == old
[i
]))
437 word
= le32_to_cpu(data
[i
]);
438 ar9170_regwrite(AR9170_BEACON_BUFFER_ADDRESS
+ 4 * i
, word
);
441 /* XXX: use skb->cb info */
442 if (ar
->hw
->conf
.channel
->band
== IEEE80211_BAND_2GHZ
)
443 ar9170_regwrite(AR9170_MAC_REG_BCN_PLCP
,
444 ((skb
->len
+ 4) << (3 + 16)) + 0x0400);
446 ar9170_regwrite(AR9170_MAC_REG_BCN_PLCP
,
447 ((skb
->len
+ 4) << 16) + 0x001b);
449 ar9170_regwrite(AR9170_MAC_REG_BCN_LENGTH
, skb
->len
+ 4);
450 ar9170_regwrite(AR9170_MAC_REG_BCN_ADDR
, AR9170_BEACON_BUFFER_ADDRESS
);
451 ar9170_regwrite(AR9170_MAC_REG_BCN_CTRL
, 1);
453 ar9170_regwrite_finish();
455 dev_kfree_skb(ar
->beacon
);
458 return ar9170_regwrite_result();
461 void ar9170_new_beacon(struct work_struct
*work
)
463 struct ar9170
*ar
= container_of(work
, struct ar9170
,
467 if (unlikely(!IS_STARTED(ar
)))
470 mutex_lock(&ar
->mutex
);
475 ar9170_update_beacon(ar
);
478 while ((skb
= ieee80211_get_buffered_bc(ar
->hw
, ar
->vif
)))
479 ar9170_op_tx(ar
->hw
, skb
);
484 mutex_unlock(&ar
->mutex
);
487 int ar9170_upload_key(struct ar9170
*ar
, u8 id
, const u8
*mac
, u8 ktype
,
488 u8 keyidx
, u8
*keydata
, int keylen
)
491 static const u8 bcast
[ETH_ALEN
] =
492 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
497 vals
[0] = cpu_to_le32((keyidx
<< 16) + id
);
498 vals
[1] = cpu_to_le32(mac
[1] << 24 | mac
[0] << 16 | ktype
);
499 vals
[2] = cpu_to_le32(mac
[5] << 24 | mac
[4] << 16 |
500 mac
[3] << 8 | mac
[2]);
501 memset(&vals
[3], 0, 16);
503 memcpy(&vals
[3], keydata
, keylen
);
505 return ar
->exec_cmd(ar
, AR9170_CMD_EKEY
,
506 sizeof(vals
), (u8
*)vals
,
510 int ar9170_disable_key(struct ar9170
*ar
, u8 id
)
512 __le32 val
= cpu_to_le32(id
);
515 return ar
->exec_cmd(ar
, AR9170_CMD_EKEY
,
516 sizeof(val
), (u8
*)&val
,