blob: fca2fe947d7d80874011c58b6fb6d2359cda0d59 [file] [log] [blame]
Michael Buesche4d6b792007-09-18 15:39:42 -04001/*
2
3 Broadcom B43 wireless driver
4
5 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
Stefano Brivio1f21ad22007-11-06 22:49:20 +01006 Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
Michael Buesch060210f2009-01-25 15:49:59 +01007 Copyright (c) 2005-2009 Michael Buesch <mb@bu3sch.de>
Michael Buesche4d6b792007-09-18 15:39:42 -04008 Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
9 Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
10
11 Some parts of the code in this file are derived from the ipw2200
12 driver Copyright(c) 2003 - 2004 Intel Corporation.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
27 Boston, MA 02110-1301, USA.
28
29*/
30
31#include <linux/delay.h>
32#include <linux/init.h>
33#include <linux/moduleparam.h>
34#include <linux/if_arp.h>
35#include <linux/etherdevice.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040036#include <linux/firmware.h>
37#include <linux/wireless.h>
38#include <linux/workqueue.h>
39#include <linux/skbuff.h>
Andrew Morton96cf49a2008-02-04 22:27:19 -080040#include <linux/io.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040041#include <linux/dma-mapping.h>
42#include <asm/unaligned.h>
43
44#include "b43.h"
45#include "main.h"
46#include "debugfs.h"
Michael Bueschef1a6282008-08-27 18:53:02 +020047#include "phy_common.h"
48#include "phy_g.h"
Michael Buesch3d0da752008-08-30 02:27:19 +020049#include "phy_n.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040050#include "dma.h"
Michael Buesch5100d5a2008-03-29 21:01:16 +010051#include "pio.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040052#include "sysfs.h"
53#include "xmit.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040054#include "lo.h"
55#include "pcmcia.h"
56
57MODULE_DESCRIPTION("Broadcom B43 wireless driver");
58MODULE_AUTHOR("Martin Langer");
59MODULE_AUTHOR("Stefano Brivio");
60MODULE_AUTHOR("Michael Buesch");
Gábor Stefanik0136e512009-08-28 22:32:17 +020061MODULE_AUTHOR("Gábor Stefanik");
Michael Buesche4d6b792007-09-18 15:39:42 -040062MODULE_LICENSE("GPL");
63
Michael Buesch9c7d99d2008-02-09 10:23:49 +010064MODULE_FIRMWARE(B43_SUPPORTED_FIRMWARE_ID);
65
Michael Buesche4d6b792007-09-18 15:39:42 -040066
67static int modparam_bad_frames_preempt;
68module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
69MODULE_PARM_DESC(bad_frames_preempt,
70 "enable(1) / disable(0) Bad Frames Preemption");
71
Michael Buesche4d6b792007-09-18 15:39:42 -040072static char modparam_fwpostfix[16];
73module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
74MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
75
Michael Buesche4d6b792007-09-18 15:39:42 -040076static int modparam_hwpctl;
77module_param_named(hwpctl, modparam_hwpctl, int, 0444);
78MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
79
80static int modparam_nohwcrypt;
81module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
82MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
83
gregor kowski035d0242009-08-19 22:35:45 +020084static int modparam_hwtkip;
85module_param_named(hwtkip, modparam_hwtkip, int, 0444);
86MODULE_PARM_DESC(hwtkip, "Enable hardware tkip.");
87
Michael Buesch403a3a12009-06-08 21:04:57 +020088static int modparam_qos = 1;
89module_param_named(qos, modparam_qos, int, 0444);
Michael Buesche6f5b932008-03-05 21:18:49 +010090MODULE_PARM_DESC(qos, "Enable QOS support (default on)");
91
Michael Buesch1855ba72008-04-18 20:51:41 +020092static int modparam_btcoex = 1;
93module_param_named(btcoex, modparam_btcoex, int, 0444);
Gábor Stefanikc71dbd32009-08-28 22:34:21 +020094MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistence (default on)");
Michael Buesch1855ba72008-04-18 20:51:41 +020095
Michael Buesch060210f2009-01-25 15:49:59 +010096int b43_modparam_verbose = B43_VERBOSITY_DEFAULT;
97module_param_named(verbose, b43_modparam_verbose, int, 0644);
98MODULE_PARM_DESC(verbose, "Log message verbosity: 0=error, 1=warn, 2=info(default), 3=debug");
99
Michael Buesche6f5b932008-03-05 21:18:49 +0100100
Michael Buesche4d6b792007-09-18 15:39:42 -0400101static const struct ssb_device_id b43_ssb_tbl[] = {
102 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
103 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
104 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
105 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
106 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
Michael Bueschd5c71e42008-01-04 17:06:29 +0100107 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
Larry Finger013978b2007-11-26 10:29:47 -0600108 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
Michael Buesch6b1c7c62008-12-25 00:39:28 +0100109 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 15),
Johannes Berg92d61282008-12-24 12:44:09 +0100110 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 16),
Michael Buesche4d6b792007-09-18 15:39:42 -0400111 SSB_DEVTABLE_END
112};
113
114MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
115
116/* Channel and ratetables are shared for all devices.
117 * They can't be const, because ieee80211 puts some precalculated
118 * data in there. This data is the same for all devices, so we don't
119 * get concurrency issues */
120#define RATETAB_ENT(_rateid, _flags) \
Johannes Berg8318d782008-01-24 19:38:38 +0100121 { \
122 .bitrate = B43_RATE_TO_BASE100KBPS(_rateid), \
123 .hw_value = (_rateid), \
124 .flags = (_flags), \
Michael Buesche4d6b792007-09-18 15:39:42 -0400125 }
Johannes Berg8318d782008-01-24 19:38:38 +0100126
127/*
128 * NOTE: When changing this, sync with xmit.c's
129 * b43_plcp_get_bitrate_idx_* functions!
130 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400131static struct ieee80211_rate __b43_ratetable[] = {
Johannes Berg8318d782008-01-24 19:38:38 +0100132 RATETAB_ENT(B43_CCK_RATE_1MB, 0),
133 RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
134 RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
135 RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
136 RATETAB_ENT(B43_OFDM_RATE_6MB, 0),
137 RATETAB_ENT(B43_OFDM_RATE_9MB, 0),
138 RATETAB_ENT(B43_OFDM_RATE_12MB, 0),
139 RATETAB_ENT(B43_OFDM_RATE_18MB, 0),
140 RATETAB_ENT(B43_OFDM_RATE_24MB, 0),
141 RATETAB_ENT(B43_OFDM_RATE_36MB, 0),
142 RATETAB_ENT(B43_OFDM_RATE_48MB, 0),
143 RATETAB_ENT(B43_OFDM_RATE_54MB, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400144};
145
146#define b43_a_ratetable (__b43_ratetable + 4)
147#define b43_a_ratetable_size 8
148#define b43_b_ratetable (__b43_ratetable + 0)
149#define b43_b_ratetable_size 4
150#define b43_g_ratetable (__b43_ratetable + 0)
151#define b43_g_ratetable_size 12
152
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100153#define CHAN4G(_channel, _freq, _flags) { \
154 .band = IEEE80211_BAND_2GHZ, \
155 .center_freq = (_freq), \
156 .hw_value = (_channel), \
157 .flags = (_flags), \
158 .max_antenna_gain = 0, \
159 .max_power = 30, \
160}
Michael Buesch96c755a2008-01-06 00:09:46 +0100161static struct ieee80211_channel b43_2ghz_chantable[] = {
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100162 CHAN4G(1, 2412, 0),
163 CHAN4G(2, 2417, 0),
164 CHAN4G(3, 2422, 0),
165 CHAN4G(4, 2427, 0),
166 CHAN4G(5, 2432, 0),
167 CHAN4G(6, 2437, 0),
168 CHAN4G(7, 2442, 0),
169 CHAN4G(8, 2447, 0),
170 CHAN4G(9, 2452, 0),
171 CHAN4G(10, 2457, 0),
172 CHAN4G(11, 2462, 0),
173 CHAN4G(12, 2467, 0),
174 CHAN4G(13, 2472, 0),
175 CHAN4G(14, 2484, 0),
176};
177#undef CHAN4G
178
179#define CHAN5G(_channel, _flags) { \
180 .band = IEEE80211_BAND_5GHZ, \
181 .center_freq = 5000 + (5 * (_channel)), \
182 .hw_value = (_channel), \
183 .flags = (_flags), \
184 .max_antenna_gain = 0, \
185 .max_power = 30, \
186}
187static struct ieee80211_channel b43_5ghz_nphy_chantable[] = {
188 CHAN5G(32, 0), CHAN5G(34, 0),
189 CHAN5G(36, 0), CHAN5G(38, 0),
190 CHAN5G(40, 0), CHAN5G(42, 0),
191 CHAN5G(44, 0), CHAN5G(46, 0),
192 CHAN5G(48, 0), CHAN5G(50, 0),
193 CHAN5G(52, 0), CHAN5G(54, 0),
194 CHAN5G(56, 0), CHAN5G(58, 0),
195 CHAN5G(60, 0), CHAN5G(62, 0),
196 CHAN5G(64, 0), CHAN5G(66, 0),
197 CHAN5G(68, 0), CHAN5G(70, 0),
198 CHAN5G(72, 0), CHAN5G(74, 0),
199 CHAN5G(76, 0), CHAN5G(78, 0),
200 CHAN5G(80, 0), CHAN5G(82, 0),
201 CHAN5G(84, 0), CHAN5G(86, 0),
202 CHAN5G(88, 0), CHAN5G(90, 0),
203 CHAN5G(92, 0), CHAN5G(94, 0),
204 CHAN5G(96, 0), CHAN5G(98, 0),
205 CHAN5G(100, 0), CHAN5G(102, 0),
206 CHAN5G(104, 0), CHAN5G(106, 0),
207 CHAN5G(108, 0), CHAN5G(110, 0),
208 CHAN5G(112, 0), CHAN5G(114, 0),
209 CHAN5G(116, 0), CHAN5G(118, 0),
210 CHAN5G(120, 0), CHAN5G(122, 0),
211 CHAN5G(124, 0), CHAN5G(126, 0),
212 CHAN5G(128, 0), CHAN5G(130, 0),
213 CHAN5G(132, 0), CHAN5G(134, 0),
214 CHAN5G(136, 0), CHAN5G(138, 0),
215 CHAN5G(140, 0), CHAN5G(142, 0),
216 CHAN5G(144, 0), CHAN5G(145, 0),
217 CHAN5G(146, 0), CHAN5G(147, 0),
218 CHAN5G(148, 0), CHAN5G(149, 0),
219 CHAN5G(150, 0), CHAN5G(151, 0),
220 CHAN5G(152, 0), CHAN5G(153, 0),
221 CHAN5G(154, 0), CHAN5G(155, 0),
222 CHAN5G(156, 0), CHAN5G(157, 0),
223 CHAN5G(158, 0), CHAN5G(159, 0),
224 CHAN5G(160, 0), CHAN5G(161, 0),
225 CHAN5G(162, 0), CHAN5G(163, 0),
226 CHAN5G(164, 0), CHAN5G(165, 0),
227 CHAN5G(166, 0), CHAN5G(168, 0),
228 CHAN5G(170, 0), CHAN5G(172, 0),
229 CHAN5G(174, 0), CHAN5G(176, 0),
230 CHAN5G(178, 0), CHAN5G(180, 0),
231 CHAN5G(182, 0), CHAN5G(184, 0),
232 CHAN5G(186, 0), CHAN5G(188, 0),
233 CHAN5G(190, 0), CHAN5G(192, 0),
234 CHAN5G(194, 0), CHAN5G(196, 0),
235 CHAN5G(198, 0), CHAN5G(200, 0),
236 CHAN5G(202, 0), CHAN5G(204, 0),
237 CHAN5G(206, 0), CHAN5G(208, 0),
238 CHAN5G(210, 0), CHAN5G(212, 0),
239 CHAN5G(214, 0), CHAN5G(216, 0),
240 CHAN5G(218, 0), CHAN5G(220, 0),
241 CHAN5G(222, 0), CHAN5G(224, 0),
242 CHAN5G(226, 0), CHAN5G(228, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400243};
244
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100245static struct ieee80211_channel b43_5ghz_aphy_chantable[] = {
246 CHAN5G(34, 0), CHAN5G(36, 0),
247 CHAN5G(38, 0), CHAN5G(40, 0),
248 CHAN5G(42, 0), CHAN5G(44, 0),
249 CHAN5G(46, 0), CHAN5G(48, 0),
250 CHAN5G(52, 0), CHAN5G(56, 0),
251 CHAN5G(60, 0), CHAN5G(64, 0),
252 CHAN5G(100, 0), CHAN5G(104, 0),
253 CHAN5G(108, 0), CHAN5G(112, 0),
254 CHAN5G(116, 0), CHAN5G(120, 0),
255 CHAN5G(124, 0), CHAN5G(128, 0),
256 CHAN5G(132, 0), CHAN5G(136, 0),
257 CHAN5G(140, 0), CHAN5G(149, 0),
258 CHAN5G(153, 0), CHAN5G(157, 0),
259 CHAN5G(161, 0), CHAN5G(165, 0),
260 CHAN5G(184, 0), CHAN5G(188, 0),
261 CHAN5G(192, 0), CHAN5G(196, 0),
262 CHAN5G(200, 0), CHAN5G(204, 0),
263 CHAN5G(208, 0), CHAN5G(212, 0),
264 CHAN5G(216, 0),
265};
266#undef CHAN5G
267
268static struct ieee80211_supported_band b43_band_5GHz_nphy = {
269 .band = IEEE80211_BAND_5GHZ,
270 .channels = b43_5ghz_nphy_chantable,
271 .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable),
272 .bitrates = b43_a_ratetable,
273 .n_bitrates = b43_a_ratetable_size,
Michael Buesche4d6b792007-09-18 15:39:42 -0400274};
Johannes Berg8318d782008-01-24 19:38:38 +0100275
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100276static struct ieee80211_supported_band b43_band_5GHz_aphy = {
277 .band = IEEE80211_BAND_5GHZ,
278 .channels = b43_5ghz_aphy_chantable,
279 .n_channels = ARRAY_SIZE(b43_5ghz_aphy_chantable),
280 .bitrates = b43_a_ratetable,
281 .n_bitrates = b43_a_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100282};
Michael Buesche4d6b792007-09-18 15:39:42 -0400283
Johannes Berg8318d782008-01-24 19:38:38 +0100284static struct ieee80211_supported_band b43_band_2GHz = {
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100285 .band = IEEE80211_BAND_2GHZ,
286 .channels = b43_2ghz_chantable,
287 .n_channels = ARRAY_SIZE(b43_2ghz_chantable),
288 .bitrates = b43_g_ratetable,
289 .n_bitrates = b43_g_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100290};
291
Michael Buesche4d6b792007-09-18 15:39:42 -0400292static void b43_wireless_core_exit(struct b43_wldev *dev);
293static int b43_wireless_core_init(struct b43_wldev *dev);
Michael Buesch36dbd952009-09-04 22:51:29 +0200294static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400295static int b43_wireless_core_start(struct b43_wldev *dev);
296
297static int b43_ratelimit(struct b43_wl *wl)
298{
299 if (!wl || !wl->current_dev)
300 return 1;
301 if (b43_status(wl->current_dev) < B43_STAT_STARTED)
302 return 1;
303 /* We are up and running.
304 * Ratelimit the messages to avoid DoS over the net. */
305 return net_ratelimit();
306}
307
308void b43info(struct b43_wl *wl, const char *fmt, ...)
309{
310 va_list args;
311
Michael Buesch060210f2009-01-25 15:49:59 +0100312 if (b43_modparam_verbose < B43_VERBOSITY_INFO)
313 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400314 if (!b43_ratelimit(wl))
315 return;
316 va_start(args, fmt);
317 printk(KERN_INFO "b43-%s: ",
318 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
319 vprintk(fmt, args);
320 va_end(args);
321}
322
323void b43err(struct b43_wl *wl, const char *fmt, ...)
324{
325 va_list args;
326
Michael Buesch060210f2009-01-25 15:49:59 +0100327 if (b43_modparam_verbose < B43_VERBOSITY_ERROR)
328 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400329 if (!b43_ratelimit(wl))
330 return;
331 va_start(args, fmt);
332 printk(KERN_ERR "b43-%s ERROR: ",
333 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
334 vprintk(fmt, args);
335 va_end(args);
336}
337
338void b43warn(struct b43_wl *wl, const char *fmt, ...)
339{
340 va_list args;
341
Michael Buesch060210f2009-01-25 15:49:59 +0100342 if (b43_modparam_verbose < B43_VERBOSITY_WARN)
343 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400344 if (!b43_ratelimit(wl))
345 return;
346 va_start(args, fmt);
347 printk(KERN_WARNING "b43-%s warning: ",
348 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
349 vprintk(fmt, args);
350 va_end(args);
351}
352
Michael Buesche4d6b792007-09-18 15:39:42 -0400353void b43dbg(struct b43_wl *wl, const char *fmt, ...)
354{
355 va_list args;
356
Michael Buesch060210f2009-01-25 15:49:59 +0100357 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
358 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400359 va_start(args, fmt);
360 printk(KERN_DEBUG "b43-%s debug: ",
361 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
362 vprintk(fmt, args);
363 va_end(args);
364}
Michael Buesche4d6b792007-09-18 15:39:42 -0400365
366static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
367{
368 u32 macctl;
369
370 B43_WARN_ON(offset % 4 != 0);
371
372 macctl = b43_read32(dev, B43_MMIO_MACCTL);
373 if (macctl & B43_MACCTL_BE)
374 val = swab32(val);
375
376 b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
377 mmiowb();
378 b43_write32(dev, B43_MMIO_RAM_DATA, val);
379}
380
Michael Buesch280d0e12007-12-26 18:26:17 +0100381static inline void b43_shm_control_word(struct b43_wldev *dev,
382 u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400383{
384 u32 control;
385
386 /* "offset" is the WORD offset. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400387 control = routing;
388 control <<= 16;
389 control |= offset;
390 b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
391}
392
Michael Buesch6bbc3212008-06-19 19:33:51 +0200393u32 __b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400394{
395 u32 ret;
396
397 if (routing == B43_SHM_SHARED) {
398 B43_WARN_ON(offset & 0x0001);
399 if (offset & 0x0003) {
400 /* Unaligned access */
401 b43_shm_control_word(dev, routing, offset >> 2);
402 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
Michael Buesche4d6b792007-09-18 15:39:42 -0400403 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200404 ret |= ((u32)b43_read16(dev, B43_MMIO_SHM_DATA)) << 16;
Michael Buesche4d6b792007-09-18 15:39:42 -0400405
Michael Buesch280d0e12007-12-26 18:26:17 +0100406 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400407 }
408 offset >>= 2;
409 }
410 b43_shm_control_word(dev, routing, offset);
411 ret = b43_read32(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100412out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200413 return ret;
414}
415
416u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
417{
418 struct b43_wl *wl = dev->wl;
419 unsigned long flags;
420 u32 ret;
421
422 spin_lock_irqsave(&wl->shm_lock, flags);
423 ret = __b43_shm_read32(dev, routing, offset);
Michael Buesch280d0e12007-12-26 18:26:17 +0100424 spin_unlock_irqrestore(&wl->shm_lock, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -0400425
426 return ret;
427}
428
Michael Buesch6bbc3212008-06-19 19:33:51 +0200429u16 __b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400430{
431 u16 ret;
432
433 if (routing == B43_SHM_SHARED) {
434 B43_WARN_ON(offset & 0x0001);
435 if (offset & 0x0003) {
436 /* Unaligned access */
437 b43_shm_control_word(dev, routing, offset >> 2);
438 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
439
Michael Buesch280d0e12007-12-26 18:26:17 +0100440 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400441 }
442 offset >>= 2;
443 }
444 b43_shm_control_word(dev, routing, offset);
445 ret = b43_read16(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100446out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200447 return ret;
448}
449
450u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
451{
452 struct b43_wl *wl = dev->wl;
453 unsigned long flags;
454 u16 ret;
455
456 spin_lock_irqsave(&wl->shm_lock, flags);
457 ret = __b43_shm_read16(dev, routing, offset);
Michael Buesch280d0e12007-12-26 18:26:17 +0100458 spin_unlock_irqrestore(&wl->shm_lock, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -0400459
460 return ret;
461}
462
Michael Buesch6bbc3212008-06-19 19:33:51 +0200463void __b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400464{
465 if (routing == B43_SHM_SHARED) {
466 B43_WARN_ON(offset & 0x0001);
467 if (offset & 0x0003) {
468 /* Unaligned access */
469 b43_shm_control_word(dev, routing, offset >> 2);
Michael Buesche4d6b792007-09-18 15:39:42 -0400470 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200471 value & 0xFFFF);
Michael Buesche4d6b792007-09-18 15:39:42 -0400472 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200473 b43_write16(dev, B43_MMIO_SHM_DATA,
474 (value >> 16) & 0xFFFF);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200475 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400476 }
477 offset >>= 2;
478 }
479 b43_shm_control_word(dev, routing, offset);
Michael Buesche4d6b792007-09-18 15:39:42 -0400480 b43_write32(dev, B43_MMIO_SHM_DATA, value);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200481}
482
483void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
484{
485 struct b43_wl *wl = dev->wl;
486 unsigned long flags;
487
488 spin_lock_irqsave(&wl->shm_lock, flags);
489 __b43_shm_write32(dev, routing, offset, value);
Michael Buesch280d0e12007-12-26 18:26:17 +0100490 spin_unlock_irqrestore(&wl->shm_lock, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -0400491}
492
Michael Buesch6bbc3212008-06-19 19:33:51 +0200493void __b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
494{
495 if (routing == B43_SHM_SHARED) {
496 B43_WARN_ON(offset & 0x0001);
497 if (offset & 0x0003) {
498 /* Unaligned access */
499 b43_shm_control_word(dev, routing, offset >> 2);
500 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
501 return;
502 }
503 offset >>= 2;
504 }
505 b43_shm_control_word(dev, routing, offset);
506 b43_write16(dev, B43_MMIO_SHM_DATA, value);
507}
508
Michael Buesche4d6b792007-09-18 15:39:42 -0400509void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
510{
Michael Buesch280d0e12007-12-26 18:26:17 +0100511 struct b43_wl *wl = dev->wl;
512 unsigned long flags;
513
514 spin_lock_irqsave(&wl->shm_lock, flags);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200515 __b43_shm_write16(dev, routing, offset, value);
Michael Buesch280d0e12007-12-26 18:26:17 +0100516 spin_unlock_irqrestore(&wl->shm_lock, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -0400517}
518
519/* Read HostFlags */
John Daiker99da1852009-02-24 02:16:42 -0800520u64 b43_hf_read(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400521{
Michael Buesch35f0d352008-02-13 14:31:08 +0100522 u64 ret;
Michael Buesche4d6b792007-09-18 15:39:42 -0400523
524 ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI);
525 ret <<= 16;
Michael Buesch35f0d352008-02-13 14:31:08 +0100526 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFMI);
527 ret <<= 16;
Michael Buesche4d6b792007-09-18 15:39:42 -0400528 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO);
529
530 return ret;
531}
532
533/* Write HostFlags */
Michael Buesch35f0d352008-02-13 14:31:08 +0100534void b43_hf_write(struct b43_wldev *dev, u64 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400535{
Michael Buesch35f0d352008-02-13 14:31:08 +0100536 u16 lo, mi, hi;
537
538 lo = (value & 0x00000000FFFFULL);
539 mi = (value & 0x0000FFFF0000ULL) >> 16;
540 hi = (value & 0xFFFF00000000ULL) >> 32;
541 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO, lo);
542 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFMI, mi);
543 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI, hi);
Michael Buesche4d6b792007-09-18 15:39:42 -0400544}
545
Michael Buesch403a3a12009-06-08 21:04:57 +0200546/* Read the firmware capabilities bitmask (Opensource firmware only) */
547static u16 b43_fwcapa_read(struct b43_wldev *dev)
548{
549 B43_WARN_ON(!dev->fw.opensource);
550 return b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_FWCAPA);
551}
552
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100553void b43_tsf_read(struct b43_wldev *dev, u64 *tsf)
Michael Buesche4d6b792007-09-18 15:39:42 -0400554{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100555 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400556
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100557 B43_WARN_ON(dev->dev->id.revision < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400558
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100559 /* The hardware guarantees us an atomic read, if we
560 * read the low register first. */
561 low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
562 high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
Michael Buesche4d6b792007-09-18 15:39:42 -0400563
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100564 *tsf = high;
565 *tsf <<= 32;
566 *tsf |= low;
Michael Buesche4d6b792007-09-18 15:39:42 -0400567}
568
569static void b43_time_lock(struct b43_wldev *dev)
570{
571 u32 macctl;
572
573 macctl = b43_read32(dev, B43_MMIO_MACCTL);
574 macctl |= B43_MACCTL_TBTTHOLD;
575 b43_write32(dev, B43_MMIO_MACCTL, macctl);
576 /* Commit the write */
577 b43_read32(dev, B43_MMIO_MACCTL);
578}
579
580static void b43_time_unlock(struct b43_wldev *dev)
581{
582 u32 macctl;
583
584 macctl = b43_read32(dev, B43_MMIO_MACCTL);
585 macctl &= ~B43_MACCTL_TBTTHOLD;
586 b43_write32(dev, B43_MMIO_MACCTL, macctl);
587 /* Commit the write */
588 b43_read32(dev, B43_MMIO_MACCTL);
589}
590
591static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
592{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100593 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400594
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100595 B43_WARN_ON(dev->dev->id.revision < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400596
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100597 low = tsf;
598 high = (tsf >> 32);
599 /* The hardware guarantees us an atomic write, if we
600 * write the low register first. */
601 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, low);
602 mmiowb();
603 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, high);
604 mmiowb();
Michael Buesche4d6b792007-09-18 15:39:42 -0400605}
606
607void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
608{
609 b43_time_lock(dev);
610 b43_tsf_write_locked(dev, tsf);
611 b43_time_unlock(dev);
612}
613
614static
John Daiker99da1852009-02-24 02:16:42 -0800615void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 *mac)
Michael Buesche4d6b792007-09-18 15:39:42 -0400616{
617 static const u8 zero_addr[ETH_ALEN] = { 0 };
618 u16 data;
619
620 if (!mac)
621 mac = zero_addr;
622
623 offset |= 0x0020;
624 b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
625
626 data = mac[0];
627 data |= mac[1] << 8;
628 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
629 data = mac[2];
630 data |= mac[3] << 8;
631 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
632 data = mac[4];
633 data |= mac[5] << 8;
634 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
635}
636
637static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
638{
639 const u8 *mac;
640 const u8 *bssid;
641 u8 mac_bssid[ETH_ALEN * 2];
642 int i;
643 u32 tmp;
644
645 bssid = dev->wl->bssid;
646 mac = dev->wl->mac_addr;
647
648 b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
649
650 memcpy(mac_bssid, mac, ETH_ALEN);
651 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
652
653 /* Write our MAC address and BSSID to template ram */
654 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
655 tmp = (u32) (mac_bssid[i + 0]);
656 tmp |= (u32) (mac_bssid[i + 1]) << 8;
657 tmp |= (u32) (mac_bssid[i + 2]) << 16;
658 tmp |= (u32) (mac_bssid[i + 3]) << 24;
659 b43_ram_write(dev, 0x20 + i, tmp);
660 }
661}
662
Johannes Berg4150c572007-09-17 01:29:23 -0400663static void b43_upload_card_macaddress(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400664{
Michael Buesche4d6b792007-09-18 15:39:42 -0400665 b43_write_mac_bssid_templates(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400666 b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
Michael Buesche4d6b792007-09-18 15:39:42 -0400667}
668
669static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
670{
671 /* slot_time is in usec. */
672 if (dev->phy.type != B43_PHYTYPE_G)
673 return;
674 b43_write16(dev, 0x684, 510 + slot_time);
675 b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
676}
677
678static void b43_short_slot_timing_enable(struct b43_wldev *dev)
679{
680 b43_set_slot_time(dev, 9);
Michael Buesche4d6b792007-09-18 15:39:42 -0400681}
682
683static void b43_short_slot_timing_disable(struct b43_wldev *dev)
684{
685 b43_set_slot_time(dev, 20);
Michael Buesche4d6b792007-09-18 15:39:42 -0400686}
687
Michael Buesche4d6b792007-09-18 15:39:42 -0400688/* DummyTransmission function, as documented on
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200689 * http://bcm-v4.sipsolutions.net/802.11/DummyTransmission
Michael Buesche4d6b792007-09-18 15:39:42 -0400690 */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200691void b43_dummy_transmission(struct b43_wldev *dev, bool ofdm, bool pa_on)
Michael Buesche4d6b792007-09-18 15:39:42 -0400692{
693 struct b43_phy *phy = &dev->phy;
694 unsigned int i, max_loop;
695 u16 value;
696 u32 buffer[5] = {
697 0x00000000,
698 0x00D40000,
699 0x00000000,
700 0x01000000,
701 0x00000000,
702 };
703
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200704 if (ofdm) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400705 max_loop = 0x1E;
706 buffer[0] = 0x000201CC;
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200707 } else {
Michael Buesche4d6b792007-09-18 15:39:42 -0400708 max_loop = 0xFA;
709 buffer[0] = 0x000B846E;
Michael Buesche4d6b792007-09-18 15:39:42 -0400710 }
711
712 for (i = 0; i < 5; i++)
713 b43_ram_write(dev, i * 4, buffer[i]);
714
Michael Buesche4d6b792007-09-18 15:39:42 -0400715 b43_write16(dev, 0x0568, 0x0000);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200716 if (dev->dev->id.revision < 11)
717 b43_write16(dev, 0x07C0, 0x0000);
718 else
719 b43_write16(dev, 0x07C0, 0x0100);
720 value = (ofdm ? 0x41 : 0x40);
Michael Buesche4d6b792007-09-18 15:39:42 -0400721 b43_write16(dev, 0x050C, value);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200722 if ((phy->type == B43_PHYTYPE_N) || (phy->type == B43_PHYTYPE_LP))
723 b43_write16(dev, 0x0514, 0x1A02);
Michael Buesche4d6b792007-09-18 15:39:42 -0400724 b43_write16(dev, 0x0508, 0x0000);
725 b43_write16(dev, 0x050A, 0x0000);
726 b43_write16(dev, 0x054C, 0x0000);
727 b43_write16(dev, 0x056A, 0x0014);
728 b43_write16(dev, 0x0568, 0x0826);
729 b43_write16(dev, 0x0500, 0x0000);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200730 if (!pa_on && (phy->type == B43_PHYTYPE_N)) {
731 //SPEC TODO
732 }
733
734 switch (phy->type) {
735 case B43_PHYTYPE_N:
736 b43_write16(dev, 0x0502, 0x00D0);
737 break;
738 case B43_PHYTYPE_LP:
739 b43_write16(dev, 0x0502, 0x0050);
740 break;
741 default:
742 b43_write16(dev, 0x0502, 0x0030);
743 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400744
745 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
746 b43_radio_write16(dev, 0x0051, 0x0017);
747 for (i = 0x00; i < max_loop; i++) {
748 value = b43_read16(dev, 0x050E);
749 if (value & 0x0080)
750 break;
751 udelay(10);
752 }
753 for (i = 0x00; i < 0x0A; i++) {
754 value = b43_read16(dev, 0x050E);
755 if (value & 0x0400)
756 break;
757 udelay(10);
758 }
Larry Finger1d280dd2008-09-29 14:19:29 -0500759 for (i = 0x00; i < 0x19; i++) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400760 value = b43_read16(dev, 0x0690);
761 if (!(value & 0x0100))
762 break;
763 udelay(10);
764 }
765 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
766 b43_radio_write16(dev, 0x0051, 0x0037);
767}
768
769static void key_write(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -0800770 u8 index, u8 algorithm, const u8 *key)
Michael Buesche4d6b792007-09-18 15:39:42 -0400771{
772 unsigned int i;
773 u32 offset;
774 u16 value;
775 u16 kidx;
776
777 /* Key index/algo block */
778 kidx = b43_kidx_to_fw(dev, index);
779 value = ((kidx << 4) | algorithm);
780 b43_shm_write16(dev, B43_SHM_SHARED,
781 B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
782
783 /* Write the key to the Key Table Pointer offset */
784 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
785 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
786 value = key[i];
787 value |= (u16) (key[i + 1]) << 8;
788 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
789 }
790}
791
John Daiker99da1852009-02-24 02:16:42 -0800792static void keymac_write(struct b43_wldev *dev, u8 index, const u8 *addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400793{
794 u32 addrtmp[2] = { 0, 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200795 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400796
797 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200798 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400799
Michael Buesch66d2d082009-08-06 10:36:50 +0200800 B43_WARN_ON(index < pairwise_keys_start);
801 /* We have four default TX keys and possibly four default RX keys.
Michael Buesche4d6b792007-09-18 15:39:42 -0400802 * Physical mac 0 is mapped to physical key 4 or 8, depending
803 * on the firmware version.
804 * So we must adjust the index here.
805 */
Michael Buesch66d2d082009-08-06 10:36:50 +0200806 index -= pairwise_keys_start;
807 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400808
809 if (addr) {
810 addrtmp[0] = addr[0];
811 addrtmp[0] |= ((u32) (addr[1]) << 8);
812 addrtmp[0] |= ((u32) (addr[2]) << 16);
813 addrtmp[0] |= ((u32) (addr[3]) << 24);
814 addrtmp[1] = addr[4];
815 addrtmp[1] |= ((u32) (addr[5]) << 8);
816 }
817
Michael Buesch66d2d082009-08-06 10:36:50 +0200818 /* Receive match transmitter address (RCMTA) mechanism */
819 b43_shm_write32(dev, B43_SHM_RCMTA,
820 (index * 2) + 0, addrtmp[0]);
821 b43_shm_write16(dev, B43_SHM_RCMTA,
822 (index * 2) + 1, addrtmp[1]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400823}
824
gregor kowski035d0242009-08-19 22:35:45 +0200825/* The ucode will use phase1 key with TEK key to decrypt rx packets.
826 * When a packet is received, the iv32 is checked.
827 * - if it doesn't the packet is returned without modification (and software
828 * decryption can be done). That's what happen when iv16 wrap.
829 * - if it does, the rc4 key is computed, and decryption is tried.
830 * Either it will success and B43_RX_MAC_DEC is returned,
831 * either it fails and B43_RX_MAC_DEC|B43_RX_MAC_DECERR is returned
832 * and the packet is not usable (it got modified by the ucode).
833 * So in order to never have B43_RX_MAC_DECERR, we should provide
834 * a iv32 and phase1key that match. Because we drop packets in case of
835 * B43_RX_MAC_DECERR, if we have a correct iv32 but a wrong phase1key, all
836 * packets will be lost without higher layer knowing (ie no resync possible
837 * until next wrap).
838 *
839 * NOTE : this should support 50 key like RCMTA because
840 * (B43_SHM_SH_KEYIDXBLOCK - B43_SHM_SH_TKIPTSCTTAK)/14 = 50
841 */
842static void rx_tkip_phase1_write(struct b43_wldev *dev, u8 index, u32 iv32,
843 u16 *phase1key)
844{
845 unsigned int i;
846 u32 offset;
847 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
848
849 if (!modparam_hwtkip)
850 return;
851
852 if (b43_new_kidx_api(dev))
853 pairwise_keys_start = B43_NR_GROUP_KEYS;
854
855 B43_WARN_ON(index < pairwise_keys_start);
856 /* We have four default TX keys and possibly four default RX keys.
857 * Physical mac 0 is mapped to physical key 4 or 8, depending
858 * on the firmware version.
859 * So we must adjust the index here.
860 */
861 index -= pairwise_keys_start;
862 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
863
864 if (b43_debug(dev, B43_DBG_KEYS)) {
865 b43dbg(dev->wl, "rx_tkip_phase1_write : idx 0x%x, iv32 0x%x\n",
866 index, iv32);
867 }
868 /* Write the key to the RX tkip shared mem */
869 offset = B43_SHM_SH_TKIPTSCTTAK + index * (10 + 4);
870 for (i = 0; i < 10; i += 2) {
871 b43_shm_write16(dev, B43_SHM_SHARED, offset + i,
872 phase1key ? phase1key[i / 2] : 0);
873 }
874 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, iv32);
875 b43_shm_write16(dev, B43_SHM_SHARED, offset + i + 2, iv32 >> 16);
876}
877
878static void b43_op_update_tkip_key(struct ieee80211_hw *hw,
879 struct ieee80211_key_conf *keyconf, const u8 *addr,
880 u32 iv32, u16 *phase1key)
881{
882 struct b43_wl *wl = hw_to_b43_wl(hw);
883 struct b43_wldev *dev;
884 int index = keyconf->hw_key_idx;
885
886 if (B43_WARN_ON(!modparam_hwtkip))
887 return;
888
889 mutex_lock(&wl->mutex);
890
891 dev = wl->current_dev;
892 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
893 goto out_unlock;
894
895 keymac_write(dev, index, NULL); /* First zero out mac to avoid race */
896
897 rx_tkip_phase1_write(dev, index, iv32, phase1key);
898 keymac_write(dev, index, addr);
899
900out_unlock:
901 mutex_unlock(&wl->mutex);
902}
903
Michael Buesche4d6b792007-09-18 15:39:42 -0400904static void do_key_write(struct b43_wldev *dev,
905 u8 index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -0800906 const u8 *key, size_t key_len, const u8 *mac_addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400907{
908 u8 buf[B43_SEC_KEYSIZE] = { 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200909 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400910
911 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200912 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400913
Michael Buesch66d2d082009-08-06 10:36:50 +0200914 B43_WARN_ON(index >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400915 B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
916
Michael Buesch66d2d082009-08-06 10:36:50 +0200917 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400918 keymac_write(dev, index, NULL); /* First zero out mac. */
gregor kowski035d0242009-08-19 22:35:45 +0200919 if (algorithm == B43_SEC_ALGO_TKIP) {
920 /*
921 * We should provide an initial iv32, phase1key pair.
922 * We could start with iv32=0 and compute the corresponding
923 * phase1key, but this means calling ieee80211_get_tkip_key
924 * with a fake skb (or export other tkip function).
925 * Because we are lazy we hope iv32 won't start with
926 * 0xffffffff and let's b43_op_update_tkip_key provide a
927 * correct pair.
928 */
929 rx_tkip_phase1_write(dev, index, 0xffffffff, (u16*)buf);
930 } else if (index >= pairwise_keys_start) /* clear it */
931 rx_tkip_phase1_write(dev, index, 0, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -0400932 if (key)
933 memcpy(buf, key, key_len);
934 key_write(dev, index, algorithm, buf);
Michael Buesch66d2d082009-08-06 10:36:50 +0200935 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400936 keymac_write(dev, index, mac_addr);
937
938 dev->key[index].algorithm = algorithm;
939}
940
941static int b43_key_write(struct b43_wldev *dev,
942 int index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -0800943 const u8 *key, size_t key_len,
944 const u8 *mac_addr,
Michael Buesche4d6b792007-09-18 15:39:42 -0400945 struct ieee80211_key_conf *keyconf)
946{
947 int i;
Michael Buesch66d2d082009-08-06 10:36:50 +0200948 int pairwise_keys_start;
Michael Buesche4d6b792007-09-18 15:39:42 -0400949
gregor kowski035d0242009-08-19 22:35:45 +0200950 /* For ALG_TKIP the key is encoded as a 256-bit (32 byte) data block:
951 * - Temporal Encryption Key (128 bits)
952 * - Temporal Authenticator Tx MIC Key (64 bits)
953 * - Temporal Authenticator Rx MIC Key (64 bits)
954 *
955 * Hardware only store TEK
956 */
957 if (algorithm == B43_SEC_ALGO_TKIP && key_len == 32)
958 key_len = 16;
Michael Buesche4d6b792007-09-18 15:39:42 -0400959 if (key_len > B43_SEC_KEYSIZE)
960 return -EINVAL;
Michael Buesch66d2d082009-08-06 10:36:50 +0200961 for (i = 0; i < ARRAY_SIZE(dev->key); i++) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400962 /* Check that we don't already have this key. */
963 B43_WARN_ON(dev->key[i].keyconf == keyconf);
964 }
965 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +0100966 /* Pairwise key. Get an empty slot for the key. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400967 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200968 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400969 else
Michael Buesch66d2d082009-08-06 10:36:50 +0200970 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
971 for (i = pairwise_keys_start;
972 i < pairwise_keys_start + B43_NR_PAIRWISE_KEYS;
973 i++) {
974 B43_WARN_ON(i >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400975 if (!dev->key[i].keyconf) {
976 /* found empty */
977 index = i;
978 break;
979 }
980 }
981 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +0100982 b43warn(dev->wl, "Out of hardware key memory\n");
Michael Buesche4d6b792007-09-18 15:39:42 -0400983 return -ENOSPC;
984 }
985 } else
986 B43_WARN_ON(index > 3);
987
988 do_key_write(dev, index, algorithm, key, key_len, mac_addr);
989 if ((index <= 3) && !b43_new_kidx_api(dev)) {
990 /* Default RX key */
991 B43_WARN_ON(mac_addr);
992 do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
993 }
994 keyconf->hw_key_idx = index;
995 dev->key[index].keyconf = keyconf;
996
997 return 0;
998}
999
1000static int b43_key_clear(struct b43_wldev *dev, int index)
1001{
Michael Buesch66d2d082009-08-06 10:36:50 +02001002 if (B43_WARN_ON((index < 0) || (index >= ARRAY_SIZE(dev->key))))
Michael Buesche4d6b792007-09-18 15:39:42 -04001003 return -EINVAL;
1004 do_key_write(dev, index, B43_SEC_ALGO_NONE,
1005 NULL, B43_SEC_KEYSIZE, NULL);
1006 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1007 do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
1008 NULL, B43_SEC_KEYSIZE, NULL);
1009 }
1010 dev->key[index].keyconf = NULL;
1011
1012 return 0;
1013}
1014
1015static void b43_clear_keys(struct b43_wldev *dev)
1016{
Michael Buesch66d2d082009-08-06 10:36:50 +02001017 int i, count;
Michael Buesche4d6b792007-09-18 15:39:42 -04001018
Michael Buesch66d2d082009-08-06 10:36:50 +02001019 if (b43_new_kidx_api(dev))
1020 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1021 else
1022 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1023 for (i = 0; i < count; i++)
Michael Buesche4d6b792007-09-18 15:39:42 -04001024 b43_key_clear(dev, i);
1025}
1026
Michael Buesch9cf7f242008-12-19 20:24:30 +01001027static void b43_dump_keymemory(struct b43_wldev *dev)
1028{
Michael Buesch66d2d082009-08-06 10:36:50 +02001029 unsigned int i, index, count, offset, pairwise_keys_start;
Michael Buesch9cf7f242008-12-19 20:24:30 +01001030 u8 mac[ETH_ALEN];
1031 u16 algo;
1032 u32 rcmta0;
1033 u16 rcmta1;
1034 u64 hf;
1035 struct b43_key *key;
1036
1037 if (!b43_debug(dev, B43_DBG_KEYS))
1038 return;
1039
1040 hf = b43_hf_read(dev);
1041 b43dbg(dev->wl, "Hardware key memory dump: USEDEFKEYS=%u\n",
1042 !!(hf & B43_HF_USEDEFKEYS));
Michael Buesch66d2d082009-08-06 10:36:50 +02001043 if (b43_new_kidx_api(dev)) {
1044 pairwise_keys_start = B43_NR_GROUP_KEYS;
1045 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1046 } else {
1047 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1048 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1049 }
1050 for (index = 0; index < count; index++) {
Michael Buesch9cf7f242008-12-19 20:24:30 +01001051 key = &(dev->key[index]);
1052 printk(KERN_DEBUG "Key slot %02u: %s",
1053 index, (key->keyconf == NULL) ? " " : "*");
1054 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
1055 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
1056 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1057 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1058 }
1059
1060 algo = b43_shm_read16(dev, B43_SHM_SHARED,
1061 B43_SHM_SH_KEYIDXBLOCK + (index * 2));
1062 printk(" Algo: %04X/%02X", algo, key->algorithm);
1063
Michael Buesch66d2d082009-08-06 10:36:50 +02001064 if (index >= pairwise_keys_start) {
gregor kowski035d0242009-08-19 22:35:45 +02001065 if (key->algorithm == B43_SEC_ALGO_TKIP) {
1066 printk(" TKIP: ");
1067 offset = B43_SHM_SH_TKIPTSCTTAK + (index - 4) * (10 + 4);
1068 for (i = 0; i < 14; i += 2) {
1069 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1070 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1071 }
1072 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01001073 rcmta0 = b43_shm_read32(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001074 ((index - pairwise_keys_start) * 2) + 0);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001075 rcmta1 = b43_shm_read16(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001076 ((index - pairwise_keys_start) * 2) + 1);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001077 *((__le32 *)(&mac[0])) = cpu_to_le32(rcmta0);
1078 *((__le16 *)(&mac[4])) = cpu_to_le16(rcmta1);
Johannes Berge91d8332009-07-15 17:21:41 +02001079 printk(" MAC: %pM", mac);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001080 } else
1081 printk(" DEFAULT KEY");
1082 printk("\n");
1083 }
1084}
1085
Michael Buesche4d6b792007-09-18 15:39:42 -04001086void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
1087{
1088 u32 macctl;
1089 u16 ucstat;
1090 bool hwps;
1091 bool awake;
1092 int i;
1093
1094 B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
1095 (ps_flags & B43_PS_DISABLED));
1096 B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
1097
1098 if (ps_flags & B43_PS_ENABLED) {
1099 hwps = 1;
1100 } else if (ps_flags & B43_PS_DISABLED) {
1101 hwps = 0;
1102 } else {
1103 //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
1104 // and thus is not an AP and we are associated, set bit 25
1105 }
1106 if (ps_flags & B43_PS_AWAKE) {
1107 awake = 1;
1108 } else if (ps_flags & B43_PS_ASLEEP) {
1109 awake = 0;
1110 } else {
1111 //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
1112 // or we are associated, or FIXME, or the latest PS-Poll packet sent was
1113 // successful, set bit26
1114 }
1115
1116/* FIXME: For now we force awake-on and hwps-off */
1117 hwps = 0;
1118 awake = 1;
1119
1120 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1121 if (hwps)
1122 macctl |= B43_MACCTL_HWPS;
1123 else
1124 macctl &= ~B43_MACCTL_HWPS;
1125 if (awake)
1126 macctl |= B43_MACCTL_AWAKE;
1127 else
1128 macctl &= ~B43_MACCTL_AWAKE;
1129 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1130 /* Commit write */
1131 b43_read32(dev, B43_MMIO_MACCTL);
1132 if (awake && dev->dev->id.revision >= 5) {
1133 /* Wait for the microcode to wake up. */
1134 for (i = 0; i < 100; i++) {
1135 ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
1136 B43_SHM_SH_UCODESTAT);
1137 if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
1138 break;
1139 udelay(10);
1140 }
1141 }
1142}
1143
Michael Buesche4d6b792007-09-18 15:39:42 -04001144void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
1145{
1146 u32 tmslow;
1147 u32 macctl;
1148
1149 flags |= B43_TMSLOW_PHYCLKEN;
1150 flags |= B43_TMSLOW_PHYRESET;
1151 ssb_device_enable(dev->dev, flags);
1152 msleep(2); /* Wait for the PLL to turn on. */
1153
1154 /* Now take the PHY out of Reset again */
1155 tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
1156 tmslow |= SSB_TMSLOW_FGC;
1157 tmslow &= ~B43_TMSLOW_PHYRESET;
1158 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
1159 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
1160 msleep(1);
1161 tmslow &= ~SSB_TMSLOW_FGC;
1162 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
1163 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
1164 msleep(1);
1165
Michael Bueschfb111372008-09-02 13:00:34 +02001166 /* Turn Analog ON, but only if we already know the PHY-type.
1167 * This protects against very early setup where we don't know the
1168 * PHY-type, yet. wireless_core_reset will be called once again later,
1169 * when we know the PHY-type. */
1170 if (dev->phy.ops)
Michael Bueschcb24f572008-09-03 12:12:20 +02001171 dev->phy.ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001172
1173 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1174 macctl &= ~B43_MACCTL_GMODE;
1175 if (flags & B43_TMSLOW_GMODE)
1176 macctl |= B43_MACCTL_GMODE;
1177 macctl |= B43_MACCTL_IHR_ENABLED;
1178 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1179}
1180
1181static void handle_irq_transmit_status(struct b43_wldev *dev)
1182{
1183 u32 v0, v1;
1184 u16 tmp;
1185 struct b43_txstatus stat;
1186
1187 while (1) {
1188 v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1189 if (!(v0 & 0x00000001))
1190 break;
1191 v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1192
1193 stat.cookie = (v0 >> 16);
1194 stat.seq = (v1 & 0x0000FFFF);
1195 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
1196 tmp = (v0 & 0x0000FFFF);
1197 stat.frame_count = ((tmp & 0xF000) >> 12);
1198 stat.rts_count = ((tmp & 0x0F00) >> 8);
1199 stat.supp_reason = ((tmp & 0x001C) >> 2);
1200 stat.pm_indicated = !!(tmp & 0x0080);
1201 stat.intermediate = !!(tmp & 0x0040);
1202 stat.for_ampdu = !!(tmp & 0x0020);
1203 stat.acked = !!(tmp & 0x0002);
1204
1205 b43_handle_txstatus(dev, &stat);
1206 }
1207}
1208
1209static void drain_txstatus_queue(struct b43_wldev *dev)
1210{
1211 u32 dummy;
1212
1213 if (dev->dev->id.revision < 5)
1214 return;
1215 /* Read all entries from the microcode TXstatus FIFO
1216 * and throw them away.
1217 */
1218 while (1) {
1219 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1220 if (!(dummy & 0x00000001))
1221 break;
1222 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1223 }
1224}
1225
1226static u32 b43_jssi_read(struct b43_wldev *dev)
1227{
1228 u32 val = 0;
1229
1230 val = b43_shm_read16(dev, B43_SHM_SHARED, 0x08A);
1231 val <<= 16;
1232 val |= b43_shm_read16(dev, B43_SHM_SHARED, 0x088);
1233
1234 return val;
1235}
1236
1237static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
1238{
1239 b43_shm_write16(dev, B43_SHM_SHARED, 0x088, (jssi & 0x0000FFFF));
1240 b43_shm_write16(dev, B43_SHM_SHARED, 0x08A, (jssi & 0xFFFF0000) >> 16);
1241}
1242
1243static void b43_generate_noise_sample(struct b43_wldev *dev)
1244{
1245 b43_jssi_write(dev, 0x7F7F7F7F);
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001246 b43_write32(dev, B43_MMIO_MACCMD,
1247 b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001248}
1249
1250static void b43_calculate_link_quality(struct b43_wldev *dev)
1251{
1252 /* Top half of Link Quality calculation. */
1253
Michael Bueschef1a6282008-08-27 18:53:02 +02001254 if (dev->phy.type != B43_PHYTYPE_G)
1255 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001256 if (dev->noisecalc.calculation_running)
1257 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001258 dev->noisecalc.calculation_running = 1;
1259 dev->noisecalc.nr_samples = 0;
1260
1261 b43_generate_noise_sample(dev);
1262}
1263
1264static void handle_irq_noise(struct b43_wldev *dev)
1265{
Michael Bueschef1a6282008-08-27 18:53:02 +02001266 struct b43_phy_g *phy = dev->phy.g;
Michael Buesche4d6b792007-09-18 15:39:42 -04001267 u16 tmp;
1268 u8 noise[4];
1269 u8 i, j;
1270 s32 average;
1271
1272 /* Bottom half of Link Quality calculation. */
1273
Michael Bueschef1a6282008-08-27 18:53:02 +02001274 if (dev->phy.type != B43_PHYTYPE_G)
1275 return;
1276
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001277 /* Possible race condition: It might be possible that the user
1278 * changed to a different channel in the meantime since we
1279 * started the calculation. We ignore that fact, since it's
1280 * not really that much of a problem. The background noise is
1281 * an estimation only anyway. Slightly wrong results will get damped
1282 * by the averaging of the 8 sample rounds. Additionally the
1283 * value is shortlived. So it will be replaced by the next noise
1284 * calculation round soon. */
1285
Michael Buesche4d6b792007-09-18 15:39:42 -04001286 B43_WARN_ON(!dev->noisecalc.calculation_running);
Michael Buesch1a094042007-09-20 11:13:40 -07001287 *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
Michael Buesche4d6b792007-09-18 15:39:42 -04001288 if (noise[0] == 0x7F || noise[1] == 0x7F ||
1289 noise[2] == 0x7F || noise[3] == 0x7F)
1290 goto generate_new;
1291
1292 /* Get the noise samples. */
1293 B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
1294 i = dev->noisecalc.nr_samples;
Harvey Harrisoncdbf0842008-05-02 13:47:48 -07001295 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1296 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1297 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1298 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001299 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
1300 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
1301 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
1302 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
1303 dev->noisecalc.nr_samples++;
1304 if (dev->noisecalc.nr_samples == 8) {
1305 /* Calculate the Link Quality by the noise samples. */
1306 average = 0;
1307 for (i = 0; i < 8; i++) {
1308 for (j = 0; j < 4; j++)
1309 average += dev->noisecalc.samples[i][j];
1310 }
1311 average /= (8 * 4);
1312 average *= 125;
1313 average += 64;
1314 average /= 128;
1315 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
1316 tmp = (tmp / 128) & 0x1F;
1317 if (tmp >= 8)
1318 average += 2;
1319 else
1320 average -= 25;
1321 if (tmp == 8)
1322 average -= 72;
1323 else
1324 average -= 48;
1325
1326 dev->stats.link_noise = average;
Michael Buesche4d6b792007-09-18 15:39:42 -04001327 dev->noisecalc.calculation_running = 0;
1328 return;
1329 }
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001330generate_new:
Michael Buesche4d6b792007-09-18 15:39:42 -04001331 b43_generate_noise_sample(dev);
1332}
1333
1334static void handle_irq_tbtt_indication(struct b43_wldev *dev)
1335{
Johannes Berg05c914f2008-09-11 00:01:58 +02001336 if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001337 ///TODO: PS TBTT
1338 } else {
1339 if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
1340 b43_power_saving_ctl_bits(dev, 0);
1341 }
Johannes Berg05c914f2008-09-11 00:01:58 +02001342 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001343 dev->dfq_valid = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04001344}
1345
1346static void handle_irq_atim_end(struct b43_wldev *dev)
1347{
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001348 if (dev->dfq_valid) {
1349 b43_write32(dev, B43_MMIO_MACCMD,
1350 b43_read32(dev, B43_MMIO_MACCMD)
1351 | B43_MACCMD_DFQ_VALID);
1352 dev->dfq_valid = 0;
1353 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001354}
1355
1356static void handle_irq_pmq(struct b43_wldev *dev)
1357{
1358 u32 tmp;
1359
1360 //TODO: AP mode.
1361
1362 while (1) {
1363 tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
1364 if (!(tmp & 0x00000008))
1365 break;
1366 }
1367 /* 16bit write is odd, but correct. */
1368 b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
1369}
1370
1371static void b43_write_template_common(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -08001372 const u8 *data, u16 size,
Michael Buesche4d6b792007-09-18 15:39:42 -04001373 u16 ram_offset,
1374 u16 shm_size_offset, u8 rate)
1375{
1376 u32 i, tmp;
1377 struct b43_plcp_hdr4 plcp;
1378
1379 plcp.data = 0;
1380 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
1381 b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
1382 ram_offset += sizeof(u32);
1383 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
1384 * So leave the first two bytes of the next write blank.
1385 */
1386 tmp = (u32) (data[0]) << 16;
1387 tmp |= (u32) (data[1]) << 24;
1388 b43_ram_write(dev, ram_offset, tmp);
1389 ram_offset += sizeof(u32);
1390 for (i = 2; i < size; i += sizeof(u32)) {
1391 tmp = (u32) (data[i + 0]);
1392 if (i + 1 < size)
1393 tmp |= (u32) (data[i + 1]) << 8;
1394 if (i + 2 < size)
1395 tmp |= (u32) (data[i + 2]) << 16;
1396 if (i + 3 < size)
1397 tmp |= (u32) (data[i + 3]) << 24;
1398 b43_ram_write(dev, ram_offset + i - 2, tmp);
1399 }
1400 b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
1401 size + sizeof(struct b43_plcp_hdr6));
1402}
1403
Michael Buesch5042c502008-04-05 15:05:00 +02001404/* Check if the use of the antenna that ieee80211 told us to
1405 * use is possible. This will fall back to DEFAULT.
1406 * "antenna_nr" is the antenna identifier we got from ieee80211. */
1407u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
1408 u8 antenna_nr)
1409{
1410 u8 antenna_mask;
1411
1412 if (antenna_nr == 0) {
1413 /* Zero means "use default antenna". That's always OK. */
1414 return 0;
1415 }
1416
1417 /* Get the mask of available antennas. */
1418 if (dev->phy.gmode)
1419 antenna_mask = dev->dev->bus->sprom.ant_available_bg;
1420 else
1421 antenna_mask = dev->dev->bus->sprom.ant_available_a;
1422
1423 if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
1424 /* This antenna is not available. Fall back to default. */
1425 return 0;
1426 }
1427
1428 return antenna_nr;
1429}
1430
Michael Buesch5042c502008-04-05 15:05:00 +02001431/* Convert a b43 antenna number value to the PHY TX control value. */
1432static u16 b43_antenna_to_phyctl(int antenna)
1433{
1434 switch (antenna) {
1435 case B43_ANTENNA0:
1436 return B43_TXH_PHY_ANT0;
1437 case B43_ANTENNA1:
1438 return B43_TXH_PHY_ANT1;
1439 case B43_ANTENNA2:
1440 return B43_TXH_PHY_ANT2;
1441 case B43_ANTENNA3:
1442 return B43_TXH_PHY_ANT3;
Gábor Stefanik64e368b2009-08-27 22:49:49 +02001443 case B43_ANTENNA_AUTO0:
1444 case B43_ANTENNA_AUTO1:
Michael Buesch5042c502008-04-05 15:05:00 +02001445 return B43_TXH_PHY_ANT01AUTO;
1446 }
1447 B43_WARN_ON(1);
1448 return 0;
1449}
1450
Michael Buesche4d6b792007-09-18 15:39:42 -04001451static void b43_write_beacon_template(struct b43_wldev *dev,
1452 u16 ram_offset,
Michael Buesch5042c502008-04-05 15:05:00 +02001453 u16 shm_size_offset)
Michael Buesche4d6b792007-09-18 15:39:42 -04001454{
Michael Buesch47f76ca2007-12-27 22:15:11 +01001455 unsigned int i, len, variable_len;
Michael Buesche66fee62007-12-26 17:47:10 +01001456 const struct ieee80211_mgmt *bcn;
1457 const u8 *ie;
1458 bool tim_found = 0;
Michael Buesch5042c502008-04-05 15:05:00 +02001459 unsigned int rate;
1460 u16 ctl;
1461 int antenna;
Johannes Berge039fa42008-05-15 12:55:29 +02001462 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
Michael Buesche4d6b792007-09-18 15:39:42 -04001463
Michael Buesche66fee62007-12-26 17:47:10 +01001464 bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
1465 len = min((size_t) dev->wl->current_beacon->len,
Michael Buesche4d6b792007-09-18 15:39:42 -04001466 0x200 - sizeof(struct b43_plcp_hdr6));
Johannes Berge039fa42008-05-15 12:55:29 +02001467 rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
Michael Buesche66fee62007-12-26 17:47:10 +01001468
1469 b43_write_template_common(dev, (const u8 *)bcn,
Michael Buesche4d6b792007-09-18 15:39:42 -04001470 len, ram_offset, shm_size_offset, rate);
Michael Buesche66fee62007-12-26 17:47:10 +01001471
Michael Buesch5042c502008-04-05 15:05:00 +02001472 /* Write the PHY TX control parameters. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02001473 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch5042c502008-04-05 15:05:00 +02001474 antenna = b43_antenna_to_phyctl(antenna);
1475 ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
1476 /* We can't send beacons with short preamble. Would get PHY errors. */
1477 ctl &= ~B43_TXH_PHY_SHORTPRMBL;
1478 ctl &= ~B43_TXH_PHY_ANT;
1479 ctl &= ~B43_TXH_PHY_ENC;
1480 ctl |= antenna;
1481 if (b43_is_cck_rate(rate))
1482 ctl |= B43_TXH_PHY_ENC_CCK;
1483 else
1484 ctl |= B43_TXH_PHY_ENC_OFDM;
1485 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
1486
Michael Buesche66fee62007-12-26 17:47:10 +01001487 /* Find the position of the TIM and the DTIM_period value
1488 * and write them to SHM. */
1489 ie = bcn->u.beacon.variable;
Michael Buesch47f76ca2007-12-27 22:15:11 +01001490 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
1491 for (i = 0; i < variable_len - 2; ) {
Michael Buesche66fee62007-12-26 17:47:10 +01001492 uint8_t ie_id, ie_len;
1493
1494 ie_id = ie[i];
1495 ie_len = ie[i + 1];
1496 if (ie_id == 5) {
1497 u16 tim_position;
1498 u16 dtim_period;
1499 /* This is the TIM Information Element */
1500
1501 /* Check whether the ie_len is in the beacon data range. */
Michael Buesch47f76ca2007-12-27 22:15:11 +01001502 if (variable_len < ie_len + 2 + i)
Michael Buesche66fee62007-12-26 17:47:10 +01001503 break;
1504 /* A valid TIM is at least 4 bytes long. */
1505 if (ie_len < 4)
1506 break;
1507 tim_found = 1;
1508
1509 tim_position = sizeof(struct b43_plcp_hdr6);
1510 tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
1511 tim_position += i;
1512
1513 dtim_period = ie[i + 3];
1514
1515 b43_shm_write16(dev, B43_SHM_SHARED,
1516 B43_SHM_SH_TIMBPOS, tim_position);
1517 b43_shm_write16(dev, B43_SHM_SHARED,
1518 B43_SHM_SH_DTIMPER, dtim_period);
1519 break;
1520 }
1521 i += ie_len + 2;
1522 }
1523 if (!tim_found) {
Johannes Berg04dea132008-05-20 12:10:49 +02001524 /*
1525 * If ucode wants to modify TIM do it behind the beacon, this
1526 * will happen, for example, when doing mesh networking.
1527 */
1528 b43_shm_write16(dev, B43_SHM_SHARED,
1529 B43_SHM_SH_TIMBPOS,
1530 len + sizeof(struct b43_plcp_hdr6));
1531 b43_shm_write16(dev, B43_SHM_SHARED,
1532 B43_SHM_SH_DTIMPER, 0);
1533 }
1534 b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
Michael Buesche4d6b792007-09-18 15:39:42 -04001535}
1536
Michael Buesch6b4bec012008-05-20 12:16:28 +02001537static void b43_upload_beacon0(struct b43_wldev *dev)
1538{
1539 struct b43_wl *wl = dev->wl;
1540
1541 if (wl->beacon0_uploaded)
1542 return;
1543 b43_write_beacon_template(dev, 0x68, 0x18);
Michael Buesch6b4bec012008-05-20 12:16:28 +02001544 wl->beacon0_uploaded = 1;
1545}
1546
1547static void b43_upload_beacon1(struct b43_wldev *dev)
1548{
1549 struct b43_wl *wl = dev->wl;
1550
1551 if (wl->beacon1_uploaded)
1552 return;
1553 b43_write_beacon_template(dev, 0x468, 0x1A);
1554 wl->beacon1_uploaded = 1;
1555}
1556
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001557static void handle_irq_beacon(struct b43_wldev *dev)
1558{
1559 struct b43_wl *wl = dev->wl;
1560 u32 cmd, beacon0_valid, beacon1_valid;
1561
Johannes Berg05c914f2008-09-11 00:01:58 +02001562 if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
1563 !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001564 return;
1565
1566 /* This is the bottom half of the asynchronous beacon update. */
1567
1568 /* Ignore interrupt in the future. */
Michael Buesch13790722009-04-08 21:26:27 +02001569 dev->irq_mask &= ~B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001570
1571 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1572 beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
1573 beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
1574
1575 /* Schedule interrupt manually, if busy. */
1576 if (beacon0_valid && beacon1_valid) {
1577 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
Michael Buesch13790722009-04-08 21:26:27 +02001578 dev->irq_mask |= B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001579 return;
1580 }
1581
Michael Buesch6b4bec012008-05-20 12:16:28 +02001582 if (unlikely(wl->beacon_templates_virgin)) {
1583 /* We never uploaded a beacon before.
1584 * Upload both templates now, but only mark one valid. */
1585 wl->beacon_templates_virgin = 0;
1586 b43_upload_beacon0(dev);
1587 b43_upload_beacon1(dev);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001588 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1589 cmd |= B43_MACCMD_BEACON0_VALID;
1590 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Buesch6b4bec012008-05-20 12:16:28 +02001591 } else {
1592 if (!beacon0_valid) {
1593 b43_upload_beacon0(dev);
1594 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1595 cmd |= B43_MACCMD_BEACON0_VALID;
1596 b43_write32(dev, B43_MMIO_MACCMD, cmd);
1597 } else if (!beacon1_valid) {
1598 b43_upload_beacon1(dev);
1599 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1600 cmd |= B43_MACCMD_BEACON1_VALID;
1601 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001602 }
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001603 }
1604}
1605
Michael Buesch36dbd952009-09-04 22:51:29 +02001606static void b43_do_beacon_update_trigger_work(struct b43_wldev *dev)
1607{
1608 u32 old_irq_mask = dev->irq_mask;
1609
1610 /* update beacon right away or defer to irq */
1611 handle_irq_beacon(dev);
1612 if (old_irq_mask != dev->irq_mask) {
1613 /* The handler updated the IRQ mask. */
1614 B43_WARN_ON(!dev->irq_mask);
1615 if (b43_read32(dev, B43_MMIO_GEN_IRQ_MASK)) {
1616 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
1617 } else {
1618 /* Device interrupts are currently disabled. That means
1619 * we just ran the hardirq handler and scheduled the
1620 * IRQ thread. The thread will write the IRQ mask when
1621 * it finished, so there's nothing to do here. Writing
1622 * the mask _here_ would incorrectly re-enable IRQs. */
1623 }
1624 }
1625}
1626
Michael Buescha82d9922008-04-04 21:40:06 +02001627static void b43_beacon_update_trigger_work(struct work_struct *work)
1628{
1629 struct b43_wl *wl = container_of(work, struct b43_wl,
1630 beacon_update_trigger);
1631 struct b43_wldev *dev;
1632
1633 mutex_lock(&wl->mutex);
1634 dev = wl->current_dev;
1635 if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
Michael Buesch36dbd952009-09-04 22:51:29 +02001636 if (0 /*FIXME dev->dev->bus->bustype == SSB_BUSTYPE_SDIO*/) {
1637 /* wl->mutex is enough. */
1638 b43_do_beacon_update_trigger_work(dev);
1639 mmiowb();
1640 } else {
1641 spin_lock_irq(&wl->hardirq_lock);
1642 b43_do_beacon_update_trigger_work(dev);
1643 mmiowb();
1644 spin_unlock_irq(&wl->hardirq_lock);
1645 }
Michael Buescha82d9922008-04-04 21:40:06 +02001646 }
1647 mutex_unlock(&wl->mutex);
1648}
1649
Michael Bueschd4df6f12007-12-26 18:04:14 +01001650/* Asynchronously update the packet templates in template RAM.
Michael Buesch36dbd952009-09-04 22:51:29 +02001651 * Locking: Requires wl->mutex to be locked. */
Johannes Berg9d139c82008-07-09 14:40:37 +02001652static void b43_update_templates(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04001653{
Johannes Berg9d139c82008-07-09 14:40:37 +02001654 struct sk_buff *beacon;
1655
Michael Buesche66fee62007-12-26 17:47:10 +01001656 /* This is the top half of the ansynchronous beacon update.
1657 * The bottom half is the beacon IRQ.
1658 * Beacon update must be asynchronous to avoid sending an
1659 * invalid beacon. This can happen for example, if the firmware
1660 * transmits a beacon while we are updating it. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001661
Johannes Berg9d139c82008-07-09 14:40:37 +02001662 /* We could modify the existing beacon and set the aid bit in
1663 * the TIM field, but that would probably require resizing and
1664 * moving of data within the beacon template.
1665 * Simply request a new beacon and let mac80211 do the hard work. */
1666 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1667 if (unlikely(!beacon))
1668 return;
1669
Michael Buesche66fee62007-12-26 17:47:10 +01001670 if (wl->current_beacon)
1671 dev_kfree_skb_any(wl->current_beacon);
1672 wl->current_beacon = beacon;
1673 wl->beacon0_uploaded = 0;
1674 wl->beacon1_uploaded = 0;
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001675 ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
Michael Buesche4d6b792007-09-18 15:39:42 -04001676}
1677
Michael Buesche4d6b792007-09-18 15:39:42 -04001678static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
1679{
1680 b43_time_lock(dev);
1681 if (dev->dev->id.revision >= 3) {
Michael Buescha82d9922008-04-04 21:40:06 +02001682 b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
1683 b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
Michael Buesche4d6b792007-09-18 15:39:42 -04001684 } else {
1685 b43_write16(dev, 0x606, (beacon_int >> 6));
1686 b43_write16(dev, 0x610, beacon_int);
1687 }
1688 b43_time_unlock(dev);
Michael Buescha82d9922008-04-04 21:40:06 +02001689 b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
Michael Buesche4d6b792007-09-18 15:39:42 -04001690}
1691
Michael Bueschafa83e22008-05-19 23:51:37 +02001692static void b43_handle_firmware_panic(struct b43_wldev *dev)
1693{
1694 u16 reason;
1695
1696 /* Read the register that contains the reason code for the panic. */
1697 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
1698 b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
1699
1700 switch (reason) {
1701 default:
1702 b43dbg(dev->wl, "The panic reason is unknown.\n");
1703 /* fallthrough */
1704 case B43_FWPANIC_DIE:
1705 /* Do not restart the controller or firmware.
1706 * The device is nonfunctional from now on.
1707 * Restarting would result in this panic to trigger again,
1708 * so we avoid that recursion. */
1709 break;
1710 case B43_FWPANIC_RESTART:
1711 b43_controller_restart(dev, "Microcode panic");
1712 break;
1713 }
1714}
1715
Michael Buesche4d6b792007-09-18 15:39:42 -04001716static void handle_irq_ucode_debug(struct b43_wldev *dev)
1717{
Michael Buesche48b0ee2008-05-17 22:44:35 +02001718 unsigned int i, cnt;
Michael Buesch53c06852008-05-20 00:24:36 +02001719 u16 reason, marker_id, marker_line;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001720 __le16 *buf;
1721
1722 /* The proprietary firmware doesn't have this IRQ. */
1723 if (!dev->fw.opensource)
1724 return;
1725
Michael Bueschafa83e22008-05-19 23:51:37 +02001726 /* Read the register that contains the reason code for this IRQ. */
1727 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
1728
Michael Buesche48b0ee2008-05-17 22:44:35 +02001729 switch (reason) {
1730 case B43_DEBUGIRQ_PANIC:
Michael Bueschafa83e22008-05-19 23:51:37 +02001731 b43_handle_firmware_panic(dev);
Michael Buesche48b0ee2008-05-17 22:44:35 +02001732 break;
1733 case B43_DEBUGIRQ_DUMP_SHM:
1734 if (!B43_DEBUG)
1735 break; /* Only with driver debugging enabled. */
1736 buf = kmalloc(4096, GFP_ATOMIC);
1737 if (!buf) {
1738 b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
1739 goto out;
1740 }
1741 for (i = 0; i < 4096; i += 2) {
1742 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
1743 buf[i / 2] = cpu_to_le16(tmp);
1744 }
1745 b43info(dev->wl, "Shared memory dump:\n");
1746 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
1747 16, 2, buf, 4096, 1);
1748 kfree(buf);
1749 break;
1750 case B43_DEBUGIRQ_DUMP_REGS:
1751 if (!B43_DEBUG)
1752 break; /* Only with driver debugging enabled. */
1753 b43info(dev->wl, "Microcode register dump:\n");
1754 for (i = 0, cnt = 0; i < 64; i++) {
1755 u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
1756 if (cnt == 0)
1757 printk(KERN_INFO);
1758 printk("r%02u: 0x%04X ", i, tmp);
1759 cnt++;
1760 if (cnt == 6) {
1761 printk("\n");
1762 cnt = 0;
1763 }
1764 }
1765 printk("\n");
1766 break;
Michael Buesch53c06852008-05-20 00:24:36 +02001767 case B43_DEBUGIRQ_MARKER:
1768 if (!B43_DEBUG)
1769 break; /* Only with driver debugging enabled. */
1770 marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
1771 B43_MARKER_ID_REG);
1772 marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
1773 B43_MARKER_LINE_REG);
1774 b43info(dev->wl, "The firmware just executed the MARKER(%u) "
1775 "at line number %u\n",
1776 marker_id, marker_line);
1777 break;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001778 default:
1779 b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
1780 reason);
1781 }
1782out:
Michael Bueschafa83e22008-05-19 23:51:37 +02001783 /* Acknowledge the debug-IRQ, so the firmware can continue. */
1784 b43_shm_write16(dev, B43_SHM_SCRATCH,
1785 B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
Michael Buesche4d6b792007-09-18 15:39:42 -04001786}
1787
Michael Buesch36dbd952009-09-04 22:51:29 +02001788static void b43_do_interrupt_thread(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001789{
1790 u32 reason;
1791 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1792 u32 merged_dma_reason = 0;
Michael Buesch21954c32007-09-27 15:31:40 +02001793 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -04001794
Michael Buesch36dbd952009-09-04 22:51:29 +02001795 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
1796 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001797
1798 reason = dev->irq_reason;
1799 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1800 dma_reason[i] = dev->dma_reason[i];
1801 merged_dma_reason |= dma_reason[i];
1802 }
1803
1804 if (unlikely(reason & B43_IRQ_MAC_TXERR))
1805 b43err(dev->wl, "MAC transmission error\n");
1806
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001807 if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001808 b43err(dev->wl, "PHY transmission error\n");
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001809 rmb();
1810 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1811 atomic_set(&dev->phy.txerr_cnt,
1812 B43_PHY_TX_BADNESS_LIMIT);
1813 b43err(dev->wl, "Too many PHY TX errors, "
1814 "restarting the controller\n");
1815 b43_controller_restart(dev, "PHY TX errors");
1816 }
1817 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001818
1819 if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK |
1820 B43_DMAIRQ_NONFATALMASK))) {
1821 if (merged_dma_reason & B43_DMAIRQ_FATALMASK) {
1822 b43err(dev->wl, "Fatal DMA error: "
1823 "0x%08X, 0x%08X, 0x%08X, "
1824 "0x%08X, 0x%08X, 0x%08X\n",
1825 dma_reason[0], dma_reason[1],
1826 dma_reason[2], dma_reason[3],
1827 dma_reason[4], dma_reason[5]);
1828 b43_controller_restart(dev, "DMA error");
Michael Buesche4d6b792007-09-18 15:39:42 -04001829 return;
1830 }
1831 if (merged_dma_reason & B43_DMAIRQ_NONFATALMASK) {
1832 b43err(dev->wl, "DMA error: "
1833 "0x%08X, 0x%08X, 0x%08X, "
1834 "0x%08X, 0x%08X, 0x%08X\n",
1835 dma_reason[0], dma_reason[1],
1836 dma_reason[2], dma_reason[3],
1837 dma_reason[4], dma_reason[5]);
1838 }
1839 }
1840
1841 if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
1842 handle_irq_ucode_debug(dev);
1843 if (reason & B43_IRQ_TBTT_INDI)
1844 handle_irq_tbtt_indication(dev);
1845 if (reason & B43_IRQ_ATIM_END)
1846 handle_irq_atim_end(dev);
1847 if (reason & B43_IRQ_BEACON)
1848 handle_irq_beacon(dev);
1849 if (reason & B43_IRQ_PMQ)
1850 handle_irq_pmq(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02001851 if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
1852 ;/* TODO */
1853 if (reason & B43_IRQ_NOISESAMPLE_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04001854 handle_irq_noise(dev);
1855
1856 /* Check the DMA reason registers for received data. */
Michael Buesch5100d5a2008-03-29 21:01:16 +01001857 if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
1858 if (b43_using_pio_transfers(dev))
1859 b43_pio_rx(dev->pio.rx_queue);
1860 else
1861 b43_dma_rx(dev->dma.rx_ring);
1862 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001863 B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
1864 B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
Michael Bueschb27faf82008-03-06 16:32:46 +01001865 B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001866 B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
1867 B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
1868
Michael Buesch21954c32007-09-27 15:31:40 +02001869 if (reason & B43_IRQ_TX_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04001870 handle_irq_transmit_status(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04001871
Michael Buesch36dbd952009-09-04 22:51:29 +02001872 /* Re-enable interrupts on the device by restoring the current interrupt mask. */
Michael Buesch13790722009-04-08 21:26:27 +02001873 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesche4d6b792007-09-18 15:39:42 -04001874}
1875
Michael Buesch36dbd952009-09-04 22:51:29 +02001876/* Interrupt thread handler. Handles device interrupts in thread context. */
1877static irqreturn_t b43_interrupt_thread_handler(int irq, void *dev_id)
Michael Buesche4d6b792007-09-18 15:39:42 -04001878{
Michael Buesche4d6b792007-09-18 15:39:42 -04001879 struct b43_wldev *dev = dev_id;
Michael Buesch36dbd952009-09-04 22:51:29 +02001880
1881 mutex_lock(&dev->wl->mutex);
1882 b43_do_interrupt_thread(dev);
1883 mmiowb();
1884 mutex_unlock(&dev->wl->mutex);
1885
1886 return IRQ_HANDLED;
1887}
1888
1889static irqreturn_t b43_do_interrupt(struct b43_wldev *dev)
1890{
Michael Buesche4d6b792007-09-18 15:39:42 -04001891 u32 reason;
1892
Michael Buesch36dbd952009-09-04 22:51:29 +02001893 /* This code runs under wl->hardirq_lock, but _only_ on non-SDIO busses.
1894 * On SDIO, this runs under wl->mutex. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001895
Michael Buesche4d6b792007-09-18 15:39:42 -04001896 reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
1897 if (reason == 0xffffffff) /* shared IRQ */
Michael Buesch36dbd952009-09-04 22:51:29 +02001898 return IRQ_NONE;
Michael Buesch13790722009-04-08 21:26:27 +02001899 reason &= dev->irq_mask;
Michael Buesche4d6b792007-09-18 15:39:42 -04001900 if (!reason)
Michael Buesch36dbd952009-09-04 22:51:29 +02001901 return IRQ_HANDLED;
Michael Buesche4d6b792007-09-18 15:39:42 -04001902
1903 dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
1904 & 0x0001DC00;
1905 dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
1906 & 0x0000DC00;
1907 dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
1908 & 0x0000DC00;
1909 dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
1910 & 0x0001DC00;
1911 dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
1912 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02001913/* Unused ring
Michael Buesche4d6b792007-09-18 15:39:42 -04001914 dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
1915 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02001916*/
Michael Buesche4d6b792007-09-18 15:39:42 -04001917
Michael Buesch36dbd952009-09-04 22:51:29 +02001918 /* ACK the interrupt. */
1919 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
1920 b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
1921 b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
1922 b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
1923 b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
1924 b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
1925/* Unused ring
1926 b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
1927*/
1928
1929 /* Disable IRQs on the device. The IRQ thread handler will re-enable them. */
Michael Buesch13790722009-04-08 21:26:27 +02001930 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
Michael Buesch36dbd952009-09-04 22:51:29 +02001931 /* Save the reason bitmasks for the IRQ thread handler. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001932 dev->irq_reason = reason;
Michael Buesch36dbd952009-09-04 22:51:29 +02001933
1934 return IRQ_WAKE_THREAD;
1935}
1936
1937/* Interrupt handler top-half. This runs with interrupts disabled. */
1938static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
1939{
1940 struct b43_wldev *dev = dev_id;
1941 irqreturn_t ret;
1942
1943 if (unlikely(b43_status(dev) < B43_STAT_STARTED))
1944 return IRQ_NONE;
1945
1946 spin_lock(&dev->wl->hardirq_lock);
1947 ret = b43_do_interrupt(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04001948 mmiowb();
Michael Buesch36dbd952009-09-04 22:51:29 +02001949 spin_unlock(&dev->wl->hardirq_lock);
Michael Buesche4d6b792007-09-18 15:39:42 -04001950
1951 return ret;
1952}
1953
Michael Buesch1a9f5092009-01-23 21:21:51 +01001954void b43_do_release_fw(struct b43_firmware_file *fw)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01001955{
1956 release_firmware(fw->data);
1957 fw->data = NULL;
1958 fw->filename = NULL;
1959}
1960
Michael Buesche4d6b792007-09-18 15:39:42 -04001961static void b43_release_firmware(struct b43_wldev *dev)
1962{
Michael Buesch1a9f5092009-01-23 21:21:51 +01001963 b43_do_release_fw(&dev->fw.ucode);
1964 b43_do_release_fw(&dev->fw.pcm);
1965 b43_do_release_fw(&dev->fw.initvals);
1966 b43_do_release_fw(&dev->fw.initvals_band);
Michael Buesche4d6b792007-09-18 15:39:42 -04001967}
1968
Michael Buescheb189d8b2008-01-28 14:47:41 -08001969static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
Michael Buesche4d6b792007-09-18 15:39:42 -04001970{
Hannes Ederfc68ed42009-02-14 11:50:06 +00001971 const char text[] =
1972 "You must go to " \
1973 "http://wireless.kernel.org/en/users/Drivers/b43#devicefirmware " \
1974 "and download the correct firmware for this driver version. " \
1975 "Please carefully read all instructions on this website.\n";
Michael Buescheb189d8b2008-01-28 14:47:41 -08001976
Michael Buescheb189d8b2008-01-28 14:47:41 -08001977 if (error)
1978 b43err(wl, text);
1979 else
1980 b43warn(wl, text);
Michael Buesche4d6b792007-09-18 15:39:42 -04001981}
1982
Michael Buesch1a9f5092009-01-23 21:21:51 +01001983int b43_do_request_fw(struct b43_request_fw_context *ctx,
1984 const char *name,
1985 struct b43_firmware_file *fw)
Michael Buesche4d6b792007-09-18 15:39:42 -04001986{
Michael Buesch61cb5dd2008-01-21 19:55:09 +01001987 const struct firmware *blob;
Michael Buesche4d6b792007-09-18 15:39:42 -04001988 struct b43_fw_header *hdr;
1989 u32 size;
1990 int err;
1991
Michael Buesch61cb5dd2008-01-21 19:55:09 +01001992 if (!name) {
1993 /* Don't fetch anything. Free possibly cached firmware. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01001994 /* FIXME: We should probably keep it anyway, to save some headache
1995 * on suspend/resume with multiband devices. */
1996 b43_do_release_fw(fw);
Michael Buesche4d6b792007-09-18 15:39:42 -04001997 return 0;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01001998 }
1999 if (fw->filename) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002000 if ((fw->type == ctx->req_type) &&
2001 (strcmp(fw->filename, name) == 0))
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002002 return 0; /* Already have this fw. */
2003 /* Free the cached firmware first. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002004 /* FIXME: We should probably do this later after we successfully
2005 * got the new fw. This could reduce headache with multiband devices.
2006 * We could also redesign this to cache the firmware for all possible
2007 * bands all the time. */
2008 b43_do_release_fw(fw);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002009 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002010
Michael Buesch1a9f5092009-01-23 21:21:51 +01002011 switch (ctx->req_type) {
2012 case B43_FWTYPE_PROPRIETARY:
2013 snprintf(ctx->fwname, sizeof(ctx->fwname),
2014 "b43%s/%s.fw",
2015 modparam_fwpostfix, name);
2016 break;
2017 case B43_FWTYPE_OPENSOURCE:
2018 snprintf(ctx->fwname, sizeof(ctx->fwname),
2019 "b43-open%s/%s.fw",
2020 modparam_fwpostfix, name);
2021 break;
2022 default:
2023 B43_WARN_ON(1);
2024 return -ENOSYS;
2025 }
2026 err = request_firmware(&blob, ctx->fwname, ctx->dev->dev->dev);
Michael Buesch68217832008-05-17 23:43:57 +02002027 if (err == -ENOENT) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002028 snprintf(ctx->errors[ctx->req_type],
2029 sizeof(ctx->errors[ctx->req_type]),
2030 "Firmware file \"%s\" not found\n", ctx->fwname);
Michael Buesch68217832008-05-17 23:43:57 +02002031 return err;
2032 } else if (err) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002033 snprintf(ctx->errors[ctx->req_type],
2034 sizeof(ctx->errors[ctx->req_type]),
2035 "Firmware file \"%s\" request failed (err=%d)\n",
2036 ctx->fwname, err);
Michael Buesche4d6b792007-09-18 15:39:42 -04002037 return err;
2038 }
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002039 if (blob->size < sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002040 goto err_format;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002041 hdr = (struct b43_fw_header *)(blob->data);
Michael Buesche4d6b792007-09-18 15:39:42 -04002042 switch (hdr->type) {
2043 case B43_FW_TYPE_UCODE:
2044 case B43_FW_TYPE_PCM:
2045 size = be32_to_cpu(hdr->size);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002046 if (size != blob->size - sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002047 goto err_format;
2048 /* fallthrough */
2049 case B43_FW_TYPE_IV:
2050 if (hdr->ver != 1)
2051 goto err_format;
2052 break;
2053 default:
2054 goto err_format;
2055 }
2056
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002057 fw->data = blob;
2058 fw->filename = name;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002059 fw->type = ctx->req_type;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002060
2061 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04002062
2063err_format:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002064 snprintf(ctx->errors[ctx->req_type],
2065 sizeof(ctx->errors[ctx->req_type]),
2066 "Firmware file \"%s\" format error.\n", ctx->fwname);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002067 release_firmware(blob);
2068
Michael Buesche4d6b792007-09-18 15:39:42 -04002069 return -EPROTO;
2070}
2071
Michael Buesch1a9f5092009-01-23 21:21:51 +01002072static int b43_try_request_fw(struct b43_request_fw_context *ctx)
Michael Buesche4d6b792007-09-18 15:39:42 -04002073{
Michael Buesch1a9f5092009-01-23 21:21:51 +01002074 struct b43_wldev *dev = ctx->dev;
2075 struct b43_firmware *fw = &ctx->dev->fw;
2076 const u8 rev = ctx->dev->dev->id.revision;
Michael Buesche4d6b792007-09-18 15:39:42 -04002077 const char *filename;
2078 u32 tmshigh;
2079 int err;
2080
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002081 /* Get microcode */
Michael Buesche4d6b792007-09-18 15:39:42 -04002082 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002083 if ((rev >= 5) && (rev <= 10))
2084 filename = "ucode5";
2085 else if ((rev >= 11) && (rev <= 12))
2086 filename = "ucode11";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002087 else if (rev == 13)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002088 filename = "ucode13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002089 else if (rev == 14)
2090 filename = "ucode14";
2091 else if (rev >= 15)
2092 filename = "ucode15";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002093 else
2094 goto err_no_ucode;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002095 err = b43_do_request_fw(ctx, filename, &fw->ucode);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002096 if (err)
2097 goto err_load;
2098
2099 /* Get PCM code */
2100 if ((rev >= 5) && (rev <= 10))
2101 filename = "pcm5";
2102 else if (rev >= 11)
2103 filename = NULL;
2104 else
2105 goto err_no_pcm;
Michael Buesch68217832008-05-17 23:43:57 +02002106 fw->pcm_request_failed = 0;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002107 err = b43_do_request_fw(ctx, filename, &fw->pcm);
Michael Buesch68217832008-05-17 23:43:57 +02002108 if (err == -ENOENT) {
2109 /* We did not find a PCM file? Not fatal, but
2110 * core rev <= 10 must do without hwcrypto then. */
2111 fw->pcm_request_failed = 1;
2112 } else if (err)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002113 goto err_load;
2114
2115 /* Get initvals */
2116 switch (dev->phy.type) {
2117 case B43_PHYTYPE_A:
2118 if ((rev >= 5) && (rev <= 10)) {
2119 if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
2120 filename = "a0g1initvals5";
2121 else
2122 filename = "a0g0initvals5";
2123 } else
2124 goto err_no_initvals;
2125 break;
2126 case B43_PHYTYPE_G:
Michael Buesche4d6b792007-09-18 15:39:42 -04002127 if ((rev >= 5) && (rev <= 10))
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002128 filename = "b0g0initvals5";
Michael Buesche4d6b792007-09-18 15:39:42 -04002129 else if (rev >= 13)
Larry.Finger@lwfinger.nete9304882008-05-15 14:07:36 -05002130 filename = "b0g0initvals13";
Michael Buesche4d6b792007-09-18 15:39:42 -04002131 else
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002132 goto err_no_initvals;
2133 break;
2134 case B43_PHYTYPE_N:
2135 if ((rev >= 11) && (rev <= 12))
2136 filename = "n0initvals11";
2137 else
2138 goto err_no_initvals;
2139 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002140 case B43_PHYTYPE_LP:
2141 if (rev == 13)
2142 filename = "lp0initvals13";
2143 else if (rev == 14)
2144 filename = "lp0initvals14";
2145 else if (rev >= 15)
2146 filename = "lp0initvals15";
2147 else
2148 goto err_no_initvals;
2149 break;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002150 default:
2151 goto err_no_initvals;
Michael Buesche4d6b792007-09-18 15:39:42 -04002152 }
Michael Buesch1a9f5092009-01-23 21:21:51 +01002153 err = b43_do_request_fw(ctx, filename, &fw->initvals);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002154 if (err)
2155 goto err_load;
2156
2157 /* Get bandswitch initvals */
2158 switch (dev->phy.type) {
2159 case B43_PHYTYPE_A:
2160 if ((rev >= 5) && (rev <= 10)) {
2161 if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
2162 filename = "a0g1bsinitvals5";
2163 else
2164 filename = "a0g0bsinitvals5";
2165 } else if (rev >= 11)
2166 filename = NULL;
2167 else
2168 goto err_no_initvals;
2169 break;
2170 case B43_PHYTYPE_G:
Michael Buesche4d6b792007-09-18 15:39:42 -04002171 if ((rev >= 5) && (rev <= 10))
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002172 filename = "b0g0bsinitvals5";
Michael Buesche4d6b792007-09-18 15:39:42 -04002173 else if (rev >= 11)
2174 filename = NULL;
2175 else
Michael Buesche4d6b792007-09-18 15:39:42 -04002176 goto err_no_initvals;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002177 break;
2178 case B43_PHYTYPE_N:
2179 if ((rev >= 11) && (rev <= 12))
2180 filename = "n0bsinitvals11";
2181 else
Michael Buesche4d6b792007-09-18 15:39:42 -04002182 goto err_no_initvals;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002183 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002184 case B43_PHYTYPE_LP:
2185 if (rev == 13)
2186 filename = "lp0bsinitvals13";
2187 else if (rev == 14)
2188 filename = "lp0bsinitvals14";
2189 else if (rev >= 15)
2190 filename = "lp0bsinitvals15";
2191 else
2192 goto err_no_initvals;
2193 break;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002194 default:
2195 goto err_no_initvals;
Michael Buesche4d6b792007-09-18 15:39:42 -04002196 }
Michael Buesch1a9f5092009-01-23 21:21:51 +01002197 err = b43_do_request_fw(ctx, filename, &fw->initvals_band);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002198 if (err)
2199 goto err_load;
Michael Buesche4d6b792007-09-18 15:39:42 -04002200
2201 return 0;
2202
Michael Buesche4d6b792007-09-18 15:39:42 -04002203err_no_ucode:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002204 err = ctx->fatal_failure = -EOPNOTSUPP;
2205 b43err(dev->wl, "The driver does not know which firmware (ucode) "
2206 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002207 goto error;
2208
2209err_no_pcm:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002210 err = ctx->fatal_failure = -EOPNOTSUPP;
2211 b43err(dev->wl, "The driver does not know which firmware (PCM) "
2212 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002213 goto error;
2214
2215err_no_initvals:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002216 err = ctx->fatal_failure = -EOPNOTSUPP;
2217 b43err(dev->wl, "The driver does not know which firmware (initvals) "
2218 "is required for your device (wl-core rev %u)\n", rev);
2219 goto error;
2220
2221err_load:
2222 /* We failed to load this firmware image. The error message
2223 * already is in ctx->errors. Return and let our caller decide
2224 * what to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002225 goto error;
2226
2227error:
2228 b43_release_firmware(dev);
2229 return err;
2230}
2231
Michael Buesch1a9f5092009-01-23 21:21:51 +01002232static int b43_request_firmware(struct b43_wldev *dev)
2233{
2234 struct b43_request_fw_context *ctx;
2235 unsigned int i;
2236 int err;
2237 const char *errmsg;
2238
2239 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2240 if (!ctx)
2241 return -ENOMEM;
2242 ctx->dev = dev;
2243
2244 ctx->req_type = B43_FWTYPE_PROPRIETARY;
2245 err = b43_try_request_fw(ctx);
2246 if (!err)
2247 goto out; /* Successfully loaded it. */
2248 err = ctx->fatal_failure;
2249 if (err)
2250 goto out;
2251
2252 ctx->req_type = B43_FWTYPE_OPENSOURCE;
2253 err = b43_try_request_fw(ctx);
2254 if (!err)
2255 goto out; /* Successfully loaded it. */
2256 err = ctx->fatal_failure;
2257 if (err)
2258 goto out;
2259
2260 /* Could not find a usable firmware. Print the errors. */
2261 for (i = 0; i < B43_NR_FWTYPES; i++) {
2262 errmsg = ctx->errors[i];
2263 if (strlen(errmsg))
2264 b43err(dev->wl, errmsg);
2265 }
2266 b43_print_fw_helptext(dev->wl, 1);
2267 err = -ENOENT;
2268
2269out:
2270 kfree(ctx);
2271 return err;
2272}
2273
Michael Buesche4d6b792007-09-18 15:39:42 -04002274static int b43_upload_microcode(struct b43_wldev *dev)
2275{
2276 const size_t hdr_len = sizeof(struct b43_fw_header);
2277 const __be32 *data;
2278 unsigned int i, len;
2279 u16 fwrev, fwpatch, fwdate, fwtime;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002280 u32 tmp, macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04002281 int err = 0;
2282
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002283 /* Jump the microcode PSM to offset 0 */
2284 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2285 B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
2286 macctl |= B43_MACCTL_PSM_JMP0;
2287 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2288 /* Zero out all microcode PSM registers and shared memory. */
2289 for (i = 0; i < 64; i++)
2290 b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
2291 for (i = 0; i < 4096; i += 2)
2292 b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
2293
Michael Buesche4d6b792007-09-18 15:39:42 -04002294 /* Upload Microcode. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002295 data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
2296 len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002297 b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
2298 for (i = 0; i < len; i++) {
2299 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2300 udelay(10);
2301 }
2302
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002303 if (dev->fw.pcm.data) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002304 /* Upload PCM data. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002305 data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
2306 len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002307 b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
2308 b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
2309 /* No need for autoinc bit in SHM_HW */
2310 b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
2311 for (i = 0; i < len; i++) {
2312 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2313 udelay(10);
2314 }
2315 }
2316
2317 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002318
2319 /* Start the microcode PSM */
2320 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2321 macctl &= ~B43_MACCTL_PSM_JMP0;
2322 macctl |= B43_MACCTL_PSM_RUN;
2323 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesche4d6b792007-09-18 15:39:42 -04002324
2325 /* Wait for the microcode to load and respond */
2326 i = 0;
2327 while (1) {
2328 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2329 if (tmp == B43_IRQ_MAC_SUSPENDED)
2330 break;
2331 i++;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002332 if (i >= 20) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002333 b43err(dev->wl, "Microcode not responding\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002334 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002335 err = -ENODEV;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002336 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002337 }
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002338 msleep_interruptible(50);
2339 if (signal_pending(current)) {
2340 err = -EINTR;
2341 goto error;
2342 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002343 }
2344 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
2345
2346 /* Get and check the revisions. */
2347 fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
2348 fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
2349 fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
2350 fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
2351
2352 if (fwrev <= 0x128) {
2353 b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
2354 "binary drivers older than version 4.x is unsupported. "
2355 "You must upgrade your firmware files.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002356 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002357 err = -EOPNOTSUPP;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002358 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002359 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002360 dev->fw.rev = fwrev;
2361 dev->fw.patch = fwpatch;
Michael Buesche48b0ee2008-05-17 22:44:35 +02002362 dev->fw.opensource = (fwdate == 0xFFFF);
2363
Michael Buesch403a3a12009-06-08 21:04:57 +02002364 /* Default to use-all-queues. */
2365 dev->wl->hw->queues = dev->wl->mac80211_initially_registered_queues;
2366 dev->qos_enabled = !!modparam_qos;
2367 /* Default to firmware/hardware crypto acceleration. */
2368 dev->hwcrypto_enabled = 1;
2369
Michael Buesche48b0ee2008-05-17 22:44:35 +02002370 if (dev->fw.opensource) {
Michael Buesch403a3a12009-06-08 21:04:57 +02002371 u16 fwcapa;
2372
Michael Buesche48b0ee2008-05-17 22:44:35 +02002373 /* Patchlevel info is encoded in the "time" field. */
2374 dev->fw.patch = fwtime;
Michael Buesch403a3a12009-06-08 21:04:57 +02002375 b43info(dev->wl, "Loading OpenSource firmware version %u.%u\n",
2376 dev->fw.rev, dev->fw.patch);
2377
2378 fwcapa = b43_fwcapa_read(dev);
2379 if (!(fwcapa & B43_FWCAPA_HWCRYPTO) || dev->fw.pcm_request_failed) {
2380 b43info(dev->wl, "Hardware crypto acceleration not supported by firmware\n");
2381 /* Disable hardware crypto and fall back to software crypto. */
2382 dev->hwcrypto_enabled = 0;
2383 }
2384 if (!(fwcapa & B43_FWCAPA_QOS)) {
2385 b43info(dev->wl, "QoS not supported by firmware\n");
2386 /* Disable QoS. Tweak hw->queues to 1. It will be restored before
2387 * ieee80211_unregister to make sure the networking core can
2388 * properly free possible resources. */
2389 dev->wl->hw->queues = 1;
2390 dev->qos_enabled = 0;
2391 }
Michael Buesche48b0ee2008-05-17 22:44:35 +02002392 } else {
2393 b43info(dev->wl, "Loading firmware version %u.%u "
2394 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
2395 fwrev, fwpatch,
2396 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
2397 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
Michael Buesch68217832008-05-17 23:43:57 +02002398 if (dev->fw.pcm_request_failed) {
2399 b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
2400 "Hardware accelerated cryptography is disabled.\n");
2401 b43_print_fw_helptext(dev->wl, 0);
2402 }
Michael Buesche48b0ee2008-05-17 22:44:35 +02002403 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002404
Michael Buescheb189d8b2008-01-28 14:47:41 -08002405 if (b43_is_old_txhdr_format(dev)) {
Michael Bueschc5572892008-12-27 18:26:39 +01002406 /* We're over the deadline, but we keep support for old fw
2407 * until it turns out to be in major conflict with something new. */
Michael Buescheb189d8b2008-01-28 14:47:41 -08002408 b43warn(dev->wl, "You are using an old firmware image. "
Michael Bueschc5572892008-12-27 18:26:39 +01002409 "Support for old firmware will be removed soon "
2410 "(official deadline was July 2008).\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002411 b43_print_fw_helptext(dev->wl, 0);
2412 }
2413
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002414 return 0;
2415
2416error:
2417 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2418 macctl &= ~B43_MACCTL_PSM_RUN;
2419 macctl |= B43_MACCTL_PSM_JMP0;
2420 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2421
Michael Buesche4d6b792007-09-18 15:39:42 -04002422 return err;
2423}
2424
2425static int b43_write_initvals(struct b43_wldev *dev,
2426 const struct b43_iv *ivals,
2427 size_t count,
2428 size_t array_size)
2429{
2430 const struct b43_iv *iv;
2431 u16 offset;
2432 size_t i;
2433 bool bit32;
2434
2435 BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
2436 iv = ivals;
2437 for (i = 0; i < count; i++) {
2438 if (array_size < sizeof(iv->offset_size))
2439 goto err_format;
2440 array_size -= sizeof(iv->offset_size);
2441 offset = be16_to_cpu(iv->offset_size);
2442 bit32 = !!(offset & B43_IV_32BIT);
2443 offset &= B43_IV_OFFSET_MASK;
2444 if (offset >= 0x1000)
2445 goto err_format;
2446 if (bit32) {
2447 u32 value;
2448
2449 if (array_size < sizeof(iv->data.d32))
2450 goto err_format;
2451 array_size -= sizeof(iv->data.d32);
2452
Harvey Harrison533dd1b2008-04-29 01:03:36 -07002453 value = get_unaligned_be32(&iv->data.d32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002454 b43_write32(dev, offset, value);
2455
2456 iv = (const struct b43_iv *)((const uint8_t *)iv +
2457 sizeof(__be16) +
2458 sizeof(__be32));
2459 } else {
2460 u16 value;
2461
2462 if (array_size < sizeof(iv->data.d16))
2463 goto err_format;
2464 array_size -= sizeof(iv->data.d16);
2465
2466 value = be16_to_cpu(iv->data.d16);
2467 b43_write16(dev, offset, value);
2468
2469 iv = (const struct b43_iv *)((const uint8_t *)iv +
2470 sizeof(__be16) +
2471 sizeof(__be16));
2472 }
2473 }
2474 if (array_size)
2475 goto err_format;
2476
2477 return 0;
2478
2479err_format:
2480 b43err(dev->wl, "Initial Values Firmware file-format error.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002481 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002482
2483 return -EPROTO;
2484}
2485
2486static int b43_upload_initvals(struct b43_wldev *dev)
2487{
2488 const size_t hdr_len = sizeof(struct b43_fw_header);
2489 const struct b43_fw_header *hdr;
2490 struct b43_firmware *fw = &dev->fw;
2491 const struct b43_iv *ivals;
2492 size_t count;
2493 int err;
2494
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002495 hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
2496 ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002497 count = be32_to_cpu(hdr->size);
2498 err = b43_write_initvals(dev, ivals, count,
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002499 fw->initvals.data->size - hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002500 if (err)
2501 goto out;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002502 if (fw->initvals_band.data) {
2503 hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
2504 ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002505 count = be32_to_cpu(hdr->size);
2506 err = b43_write_initvals(dev, ivals, count,
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002507 fw->initvals_band.data->size - hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002508 if (err)
2509 goto out;
2510 }
2511out:
2512
2513 return err;
2514}
2515
2516/* Initialize the GPIOs
2517 * http://bcm-specs.sipsolutions.net/GPIO
2518 */
2519static int b43_gpio_init(struct b43_wldev *dev)
2520{
2521 struct ssb_bus *bus = dev->dev->bus;
2522 struct ssb_device *gpiodev, *pcidev = NULL;
2523 u32 mask, set;
2524
2525 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
2526 & ~B43_MACCTL_GPOUTSMSK);
2527
Michael Buesche4d6b792007-09-18 15:39:42 -04002528 b43_write16(dev, B43_MMIO_GPIO_MASK, b43_read16(dev, B43_MMIO_GPIO_MASK)
2529 | 0x000F);
2530
2531 mask = 0x0000001F;
2532 set = 0x0000000F;
2533 if (dev->dev->bus->chip_id == 0x4301) {
2534 mask |= 0x0060;
2535 set |= 0x0060;
2536 }
2537 if (0 /* FIXME: conditional unknown */ ) {
2538 b43_write16(dev, B43_MMIO_GPIO_MASK,
2539 b43_read16(dev, B43_MMIO_GPIO_MASK)
2540 | 0x0100);
2541 mask |= 0x0180;
2542 set |= 0x0180;
2543 }
Larry Finger95de2842007-11-09 16:57:18 -06002544 if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002545 b43_write16(dev, B43_MMIO_GPIO_MASK,
2546 b43_read16(dev, B43_MMIO_GPIO_MASK)
2547 | 0x0200);
2548 mask |= 0x0200;
2549 set |= 0x0200;
2550 }
2551 if (dev->dev->id.revision >= 2)
2552 mask |= 0x0010; /* FIXME: This is redundant. */
2553
2554#ifdef CONFIG_SSB_DRIVER_PCICORE
2555 pcidev = bus->pcicore.dev;
2556#endif
2557 gpiodev = bus->chipco.dev ? : pcidev;
2558 if (!gpiodev)
2559 return 0;
2560 ssb_write32(gpiodev, B43_GPIO_CONTROL,
2561 (ssb_read32(gpiodev, B43_GPIO_CONTROL)
2562 & mask) | set);
2563
2564 return 0;
2565}
2566
2567/* Turn off all GPIO stuff. Call this on module unload, for example. */
2568static void b43_gpio_cleanup(struct b43_wldev *dev)
2569{
2570 struct ssb_bus *bus = dev->dev->bus;
2571 struct ssb_device *gpiodev, *pcidev = NULL;
2572
2573#ifdef CONFIG_SSB_DRIVER_PCICORE
2574 pcidev = bus->pcicore.dev;
2575#endif
2576 gpiodev = bus->chipco.dev ? : pcidev;
2577 if (!gpiodev)
2578 return;
2579 ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
2580}
2581
2582/* http://bcm-specs.sipsolutions.net/EnableMac */
Michael Bueschf5eda472008-04-20 16:03:32 +02002583void b43_mac_enable(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002584{
Michael Buesch923fd702008-06-20 18:02:08 +02002585 if (b43_debug(dev, B43_DBG_FIRMWARE)) {
2586 u16 fwstate;
2587
2588 fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
2589 B43_SHM_SH_UCODESTAT);
2590 if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
2591 (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
2592 b43err(dev->wl, "b43_mac_enable(): The firmware "
2593 "should be suspended, but current state is %u\n",
2594 fwstate);
2595 }
2596 }
2597
Michael Buesche4d6b792007-09-18 15:39:42 -04002598 dev->mac_suspended--;
2599 B43_WARN_ON(dev->mac_suspended < 0);
2600 if (dev->mac_suspended == 0) {
2601 b43_write32(dev, B43_MMIO_MACCTL,
2602 b43_read32(dev, B43_MMIO_MACCTL)
2603 | B43_MACCTL_ENABLED);
2604 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
2605 B43_IRQ_MAC_SUSPENDED);
2606 /* Commit writes */
2607 b43_read32(dev, B43_MMIO_MACCTL);
2608 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2609 b43_power_saving_ctl_bits(dev, 0);
2610 }
2611}
2612
2613/* http://bcm-specs.sipsolutions.net/SuspendMAC */
Michael Bueschf5eda472008-04-20 16:03:32 +02002614void b43_mac_suspend(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002615{
2616 int i;
2617 u32 tmp;
2618
Michael Buesch05b64b32007-09-28 16:19:03 +02002619 might_sleep();
Michael Buesche4d6b792007-09-18 15:39:42 -04002620 B43_WARN_ON(dev->mac_suspended < 0);
Michael Buesch05b64b32007-09-28 16:19:03 +02002621
Michael Buesche4d6b792007-09-18 15:39:42 -04002622 if (dev->mac_suspended == 0) {
2623 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
2624 b43_write32(dev, B43_MMIO_MACCTL,
2625 b43_read32(dev, B43_MMIO_MACCTL)
2626 & ~B43_MACCTL_ENABLED);
2627 /* force pci to flush the write */
2628 b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschba380012008-04-15 21:13:36 +02002629 for (i = 35; i; i--) {
2630 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2631 if (tmp & B43_IRQ_MAC_SUSPENDED)
2632 goto out;
2633 udelay(10);
2634 }
2635 /* Hm, it seems this will take some time. Use msleep(). */
Michael Buesch05b64b32007-09-28 16:19:03 +02002636 for (i = 40; i; i--) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002637 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2638 if (tmp & B43_IRQ_MAC_SUSPENDED)
2639 goto out;
Michael Buesch05b64b32007-09-28 16:19:03 +02002640 msleep(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002641 }
2642 b43err(dev->wl, "MAC suspend failed\n");
2643 }
Michael Buesch05b64b32007-09-28 16:19:03 +02002644out:
Michael Buesche4d6b792007-09-18 15:39:42 -04002645 dev->mac_suspended++;
2646}
2647
2648static void b43_adjust_opmode(struct b43_wldev *dev)
2649{
2650 struct b43_wl *wl = dev->wl;
2651 u32 ctl;
2652 u16 cfp_pretbtt;
2653
2654 ctl = b43_read32(dev, B43_MMIO_MACCTL);
2655 /* Reset status to STA infrastructure mode. */
2656 ctl &= ~B43_MACCTL_AP;
2657 ctl &= ~B43_MACCTL_KEEP_CTL;
2658 ctl &= ~B43_MACCTL_KEEP_BADPLCP;
2659 ctl &= ~B43_MACCTL_KEEP_BAD;
2660 ctl &= ~B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04002661 ctl &= ~B43_MACCTL_BEACPROMISC;
Michael Buesche4d6b792007-09-18 15:39:42 -04002662 ctl |= B43_MACCTL_INFRA;
2663
Johannes Berg05c914f2008-09-11 00:01:58 +02002664 if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
2665 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
Johannes Berg4150c572007-09-17 01:29:23 -04002666 ctl |= B43_MACCTL_AP;
Johannes Berg05c914f2008-09-11 00:01:58 +02002667 else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Johannes Berg4150c572007-09-17 01:29:23 -04002668 ctl &= ~B43_MACCTL_INFRA;
2669
2670 if (wl->filter_flags & FIF_CONTROL)
Michael Buesche4d6b792007-09-18 15:39:42 -04002671 ctl |= B43_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04002672 if (wl->filter_flags & FIF_FCSFAIL)
2673 ctl |= B43_MACCTL_KEEP_BAD;
2674 if (wl->filter_flags & FIF_PLCPFAIL)
2675 ctl |= B43_MACCTL_KEEP_BADPLCP;
2676 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
Michael Buesche4d6b792007-09-18 15:39:42 -04002677 ctl |= B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04002678 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
2679 ctl |= B43_MACCTL_BEACPROMISC;
2680
Michael Buesche4d6b792007-09-18 15:39:42 -04002681 /* Workaround: On old hardware the HW-MAC-address-filter
2682 * doesn't work properly, so always run promisc in filter
2683 * it in software. */
2684 if (dev->dev->id.revision <= 4)
2685 ctl |= B43_MACCTL_PROMISC;
2686
2687 b43_write32(dev, B43_MMIO_MACCTL, ctl);
2688
2689 cfp_pretbtt = 2;
2690 if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
2691 if (dev->dev->bus->chip_id == 0x4306 &&
2692 dev->dev->bus->chip_rev == 3)
2693 cfp_pretbtt = 100;
2694 else
2695 cfp_pretbtt = 50;
2696 }
2697 b43_write16(dev, 0x612, cfp_pretbtt);
2698}
2699
2700static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
2701{
2702 u16 offset;
2703
2704 if (is_ofdm) {
2705 offset = 0x480;
2706 offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
2707 } else {
2708 offset = 0x4C0;
2709 offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
2710 }
2711 b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
2712 b43_shm_read16(dev, B43_SHM_SHARED, offset));
2713}
2714
2715static void b43_rate_memory_init(struct b43_wldev *dev)
2716{
2717 switch (dev->phy.type) {
2718 case B43_PHYTYPE_A:
2719 case B43_PHYTYPE_G:
Michael Buesch53a6e232008-01-13 21:23:44 +01002720 case B43_PHYTYPE_N:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02002721 case B43_PHYTYPE_LP:
Michael Buesche4d6b792007-09-18 15:39:42 -04002722 b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
2723 b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
2724 b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
2725 b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
2726 b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
2727 b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
2728 b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
2729 if (dev->phy.type == B43_PHYTYPE_A)
2730 break;
2731 /* fallthrough */
2732 case B43_PHYTYPE_B:
2733 b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
2734 b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
2735 b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
2736 b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
2737 break;
2738 default:
2739 B43_WARN_ON(1);
2740 }
2741}
2742
Michael Buesch5042c502008-04-05 15:05:00 +02002743/* Set the default values for the PHY TX Control Words. */
2744static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
2745{
2746 u16 ctl = 0;
2747
2748 ctl |= B43_TXH_PHY_ENC_CCK;
2749 ctl |= B43_TXH_PHY_ANT01AUTO;
2750 ctl |= B43_TXH_PHY_TXPWR;
2751
2752 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
2753 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
2754 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
2755}
2756
Michael Buesche4d6b792007-09-18 15:39:42 -04002757/* Set the TX-Antenna for management frames sent by firmware. */
2758static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
2759{
Michael Buesch5042c502008-04-05 15:05:00 +02002760 u16 ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04002761 u16 tmp;
2762
Michael Buesch5042c502008-04-05 15:05:00 +02002763 ant = b43_antenna_to_phyctl(antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04002764
Michael Buesche4d6b792007-09-18 15:39:42 -04002765 /* For ACK/CTS */
2766 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08002767 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04002768 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
2769 /* For Probe Resposes */
2770 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08002771 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04002772 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
2773}
2774
2775/* This is the opposite of b43_chip_init() */
2776static void b43_chip_exit(struct b43_wldev *dev)
2777{
Michael Bueschfb111372008-09-02 13:00:34 +02002778 b43_phy_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002779 b43_gpio_cleanup(dev);
2780 /* firmware is released later */
2781}
2782
2783/* Initialize the chip
2784 * http://bcm-specs.sipsolutions.net/ChipInit
2785 */
2786static int b43_chip_init(struct b43_wldev *dev)
2787{
2788 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +02002789 int err;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002790 u32 value32, macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04002791 u16 value16;
2792
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002793 /* Initialize the MAC control */
2794 macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
2795 if (dev->phy.gmode)
2796 macctl |= B43_MACCTL_GMODE;
2797 macctl |= B43_MACCTL_INFRA;
2798 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesche4d6b792007-09-18 15:39:42 -04002799
2800 err = b43_request_firmware(dev);
2801 if (err)
2802 goto out;
2803 err = b43_upload_microcode(dev);
2804 if (err)
2805 goto out; /* firmware is released later */
2806
2807 err = b43_gpio_init(dev);
2808 if (err)
2809 goto out; /* firmware is released later */
Michael Buesch21954c32007-09-27 15:31:40 +02002810
Michael Buesche4d6b792007-09-18 15:39:42 -04002811 err = b43_upload_initvals(dev);
2812 if (err)
Larry Finger1a8d1222007-12-14 13:59:11 +01002813 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04002814
Michael Buesch0b7dcd92008-09-03 12:31:54 +02002815 /* Turn the Analog on and initialize the PHY. */
2816 phy->ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002817 err = b43_phy_init(dev);
2818 if (err)
Michael Bueschef1a6282008-08-27 18:53:02 +02002819 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04002820
Michael Bueschef1a6282008-08-27 18:53:02 +02002821 /* Disable Interference Mitigation. */
2822 if (phy->ops->interf_mitigation)
2823 phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04002824
Michael Bueschef1a6282008-08-27 18:53:02 +02002825 /* Select the antennae */
2826 if (phy->ops->set_rx_antenna)
2827 phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
Michael Buesche4d6b792007-09-18 15:39:42 -04002828 b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
2829
2830 if (phy->type == B43_PHYTYPE_B) {
2831 value16 = b43_read16(dev, 0x005E);
2832 value16 |= 0x0004;
2833 b43_write16(dev, 0x005E, value16);
2834 }
2835 b43_write32(dev, 0x0100, 0x01000000);
2836 if (dev->dev->id.revision < 5)
2837 b43_write32(dev, 0x010C, 0x01000000);
2838
2839 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
2840 & ~B43_MACCTL_INFRA);
2841 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
2842 | B43_MACCTL_INFRA);
Michael Buesche4d6b792007-09-18 15:39:42 -04002843
Michael Buesche4d6b792007-09-18 15:39:42 -04002844 /* Probe Response Timeout value */
2845 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
2846 b43_shm_write16(dev, B43_SHM_SHARED, 0x0074, 0x0000);
2847
2848 /* Initially set the wireless operation mode. */
2849 b43_adjust_opmode(dev);
2850
2851 if (dev->dev->id.revision < 3) {
2852 b43_write16(dev, 0x060E, 0x0000);
2853 b43_write16(dev, 0x0610, 0x8000);
2854 b43_write16(dev, 0x0604, 0x0000);
2855 b43_write16(dev, 0x0606, 0x0200);
2856 } else {
2857 b43_write32(dev, 0x0188, 0x80000000);
2858 b43_write32(dev, 0x018C, 0x02000000);
2859 }
2860 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
2861 b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
2862 b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
2863 b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
2864 b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
2865 b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
2866 b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
2867
2868 value32 = ssb_read32(dev->dev, SSB_TMSLOW);
2869 value32 |= 0x00100000;
2870 ssb_write32(dev->dev, SSB_TMSLOW, value32);
2871
2872 b43_write16(dev, B43_MMIO_POWERUP_DELAY,
2873 dev->dev->bus->chipco.fast_pwrup_delay);
2874
2875 err = 0;
2876 b43dbg(dev->wl, "Chip initialized\n");
Michael Buesch21954c32007-09-27 15:31:40 +02002877out:
Michael Buesche4d6b792007-09-18 15:39:42 -04002878 return err;
2879
Larry Finger1a8d1222007-12-14 13:59:11 +01002880err_gpio_clean:
Michael Buesche4d6b792007-09-18 15:39:42 -04002881 b43_gpio_cleanup(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02002882 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04002883}
2884
Michael Buesche4d6b792007-09-18 15:39:42 -04002885static void b43_periodic_every60sec(struct b43_wldev *dev)
2886{
Michael Bueschef1a6282008-08-27 18:53:02 +02002887 const struct b43_phy_operations *ops = dev->phy.ops;
Michael Buesche4d6b792007-09-18 15:39:42 -04002888
Michael Bueschef1a6282008-08-27 18:53:02 +02002889 if (ops->pwork_60sec)
2890 ops->pwork_60sec(dev);
Michael Buesch18c8ade2008-08-28 19:33:40 +02002891
2892 /* Force check the TX power emission now. */
2893 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
Michael Buesche4d6b792007-09-18 15:39:42 -04002894}
2895
2896static void b43_periodic_every30sec(struct b43_wldev *dev)
2897{
2898 /* Update device statistics. */
2899 b43_calculate_link_quality(dev);
2900}
2901
2902static void b43_periodic_every15sec(struct b43_wldev *dev)
2903{
2904 struct b43_phy *phy = &dev->phy;
Michael Buesch9b839a72008-06-20 17:44:02 +02002905 u16 wdr;
2906
2907 if (dev->fw.opensource) {
2908 /* Check if the firmware is still alive.
2909 * It will reset the watchdog counter to 0 in its idle loop. */
2910 wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
2911 if (unlikely(wdr)) {
2912 b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
2913 b43_controller_restart(dev, "Firmware watchdog");
2914 return;
2915 } else {
2916 b43_shm_write16(dev, B43_SHM_SCRATCH,
2917 B43_WATCHDOG_REG, 1);
2918 }
2919 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002920
Michael Bueschef1a6282008-08-27 18:53:02 +02002921 if (phy->ops->pwork_15sec)
2922 phy->ops->pwork_15sec(dev);
2923
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01002924 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
2925 wmb();
Michael Buesche4d6b792007-09-18 15:39:42 -04002926}
2927
Michael Buesche4d6b792007-09-18 15:39:42 -04002928static void do_periodic_work(struct b43_wldev *dev)
2929{
2930 unsigned int state;
2931
2932 state = dev->periodic_state;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02002933 if (state % 4 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04002934 b43_periodic_every60sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02002935 if (state % 2 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04002936 b43_periodic_every30sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02002937 b43_periodic_every15sec(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002938}
2939
Michael Buesch05b64b32007-09-28 16:19:03 +02002940/* Periodic work locking policy:
2941 * The whole periodic work handler is protected by
2942 * wl->mutex. If another lock is needed somewhere in the
2943 * pwork callchain, it's aquired in-place, where it's needed.
Michael Buesche4d6b792007-09-18 15:39:42 -04002944 */
Michael Buesche4d6b792007-09-18 15:39:42 -04002945static void b43_periodic_work_handler(struct work_struct *work)
2946{
Michael Buesch05b64b32007-09-28 16:19:03 +02002947 struct b43_wldev *dev = container_of(work, struct b43_wldev,
2948 periodic_work.work);
2949 struct b43_wl *wl = dev->wl;
2950 unsigned long delay;
Michael Buesche4d6b792007-09-18 15:39:42 -04002951
Michael Buesch05b64b32007-09-28 16:19:03 +02002952 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04002953
2954 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
2955 goto out;
2956 if (b43_debug(dev, B43_DBG_PWORK_STOP))
2957 goto out_requeue;
2958
Michael Buesch05b64b32007-09-28 16:19:03 +02002959 do_periodic_work(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002960
Michael Buesche4d6b792007-09-18 15:39:42 -04002961 dev->periodic_state++;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02002962out_requeue:
Michael Buesche4d6b792007-09-18 15:39:42 -04002963 if (b43_debug(dev, B43_DBG_PWORK_FAST))
2964 delay = msecs_to_jiffies(50);
2965 else
Anton Blanchard82cd6822007-10-15 00:42:23 -05002966 delay = round_jiffies_relative(HZ * 15);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002967 ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02002968out:
Michael Buesch05b64b32007-09-28 16:19:03 +02002969 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04002970}
2971
2972static void b43_periodic_tasks_setup(struct b43_wldev *dev)
2973{
2974 struct delayed_work *work = &dev->periodic_work;
2975
2976 dev->periodic_state = 0;
2977 INIT_DELAYED_WORK(work, b43_periodic_work_handler);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002978 ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04002979}
2980
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01002981/* Check if communication with the device works correctly. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002982static int b43_validate_chipaccess(struct b43_wldev *dev)
2983{
Michael Bueschf62ae6c2009-07-31 20:51:41 +02002984 u32 v, backup0, backup4;
Michael Buesche4d6b792007-09-18 15:39:42 -04002985
Michael Bueschf62ae6c2009-07-31 20:51:41 +02002986 backup0 = b43_shm_read32(dev, B43_SHM_SHARED, 0);
2987 backup4 = b43_shm_read32(dev, B43_SHM_SHARED, 4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01002988
2989 /* Check for read/write and endianness problems. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002990 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
2991 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
2992 goto error;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01002993 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
2994 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
Michael Buesche4d6b792007-09-18 15:39:42 -04002995 goto error;
2996
Michael Bueschf62ae6c2009-07-31 20:51:41 +02002997 /* Check if unaligned 32bit SHM_SHARED access works properly.
2998 * However, don't bail out on failure, because it's noncritical. */
2999 b43_shm_write16(dev, B43_SHM_SHARED, 0, 0x1122);
3000 b43_shm_write16(dev, B43_SHM_SHARED, 2, 0x3344);
3001 b43_shm_write16(dev, B43_SHM_SHARED, 4, 0x5566);
3002 b43_shm_write16(dev, B43_SHM_SHARED, 6, 0x7788);
3003 if (b43_shm_read32(dev, B43_SHM_SHARED, 2) != 0x55663344)
3004 b43warn(dev->wl, "Unaligned 32bit SHM read access is broken\n");
3005 b43_shm_write32(dev, B43_SHM_SHARED, 2, 0xAABBCCDD);
3006 if (b43_shm_read16(dev, B43_SHM_SHARED, 0) != 0x1122 ||
3007 b43_shm_read16(dev, B43_SHM_SHARED, 2) != 0xCCDD ||
3008 b43_shm_read16(dev, B43_SHM_SHARED, 4) != 0xAABB ||
3009 b43_shm_read16(dev, B43_SHM_SHARED, 6) != 0x7788)
3010 b43warn(dev->wl, "Unaligned 32bit SHM write access is broken\n");
3011
3012 b43_shm_write32(dev, B43_SHM_SHARED, 0, backup0);
3013 b43_shm_write32(dev, B43_SHM_SHARED, 4, backup4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003014
3015 if ((dev->dev->id.revision >= 3) && (dev->dev->id.revision <= 10)) {
3016 /* The 32bit register shadows the two 16bit registers
3017 * with update sideeffects. Validate this. */
3018 b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
3019 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
3020 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
3021 goto error;
3022 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
3023 goto error;
3024 }
3025 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
3026
3027 v = b43_read32(dev, B43_MMIO_MACCTL);
3028 v |= B43_MACCTL_GMODE;
3029 if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
Michael Buesche4d6b792007-09-18 15:39:42 -04003030 goto error;
3031
3032 return 0;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003033error:
Michael Buesche4d6b792007-09-18 15:39:42 -04003034 b43err(dev->wl, "Failed to validate the chipaccess\n");
3035 return -ENODEV;
3036}
3037
3038static void b43_security_init(struct b43_wldev *dev)
3039{
Michael Buesche4d6b792007-09-18 15:39:42 -04003040 dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
3041 /* KTP is a word address, but we address SHM bytewise.
3042 * So multiply by two.
3043 */
3044 dev->ktp *= 2;
Michael Buesch66d2d082009-08-06 10:36:50 +02003045 /* Number of RCMTA address slots */
3046 b43_write16(dev, B43_MMIO_RCMTA_COUNT, B43_NR_PAIRWISE_KEYS);
3047 /* Clear the key memory. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003048 b43_clear_keys(dev);
3049}
3050
Michael Buesch616de352009-03-29 13:19:31 +02003051#ifdef CONFIG_B43_HWRNG
John Daiker99da1852009-02-24 02:16:42 -08003052static int b43_rng_read(struct hwrng *rng, u32 *data)
Michael Buesche4d6b792007-09-18 15:39:42 -04003053{
3054 struct b43_wl *wl = (struct b43_wl *)rng->priv;
Michael Buesche4d6b792007-09-18 15:39:42 -04003055
Michael Buesch36dbd952009-09-04 22:51:29 +02003056 /* FIXME: We need to take wl->mutex here to make sure the device
3057 * is not going away from under our ass. However it could deadlock
3058 * with hwrng internal locking. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003059
Michael Buesche4d6b792007-09-18 15:39:42 -04003060 *data = b43_read16(wl->current_dev, B43_MMIO_RNG);
Michael Buesche4d6b792007-09-18 15:39:42 -04003061
3062 return (sizeof(u16));
3063}
Michael Buesch616de352009-03-29 13:19:31 +02003064#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003065
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003066static void b43_rng_exit(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04003067{
Michael Buesch616de352009-03-29 13:19:31 +02003068#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003069 if (wl->rng_initialized)
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003070 hwrng_unregister(&wl->rng);
Michael Buesch616de352009-03-29 13:19:31 +02003071#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003072}
3073
3074static int b43_rng_init(struct b43_wl *wl)
3075{
Michael Buesch616de352009-03-29 13:19:31 +02003076 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003077
Michael Buesch616de352009-03-29 13:19:31 +02003078#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003079 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
3080 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
3081 wl->rng.name = wl->rng_name;
3082 wl->rng.data_read = b43_rng_read;
3083 wl->rng.priv = (unsigned long)wl;
3084 wl->rng_initialized = 1;
3085 err = hwrng_register(&wl->rng);
3086 if (err) {
3087 wl->rng_initialized = 0;
3088 b43err(wl, "Failed to register the random "
3089 "number generator (%d)\n", err);
3090 }
Michael Buesch616de352009-03-29 13:19:31 +02003091#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003092
3093 return err;
3094}
3095
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003096static void b43_tx_work(struct work_struct *work)
Michael Buesche4d6b792007-09-18 15:39:42 -04003097{
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003098 struct b43_wl *wl = container_of(work, struct b43_wl, tx_work);
3099 struct b43_wldev *dev;
3100 struct sk_buff *skb;
3101 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003102
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003103 mutex_lock(&wl->mutex);
3104 dev = wl->current_dev;
3105 if (unlikely(!dev || b43_status(dev) < B43_STAT_STARTED)) {
3106 mutex_unlock(&wl->mutex);
3107 return;
Michael Buesch5100d5a2008-03-29 21:01:16 +01003108 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003109
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003110 while (skb_queue_len(&wl->tx_queue)) {
3111 skb = skb_dequeue(&wl->tx_queue);
Michael Buesch21a75d72008-04-25 19:29:08 +02003112
Michael Buesch21a75d72008-04-25 19:29:08 +02003113 if (b43_using_pio_transfers(dev))
Johannes Berge039fa42008-05-15 12:55:29 +02003114 err = b43_pio_tx(dev, skb);
Michael Buesch21a75d72008-04-25 19:29:08 +02003115 else
Johannes Berge039fa42008-05-15 12:55:29 +02003116 err = b43_dma_tx(dev, skb);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003117 if (unlikely(err))
3118 dev_kfree_skb(skb); /* Drop it */
Michael Buesch21a75d72008-04-25 19:29:08 +02003119 }
3120
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003121 mutex_unlock(&wl->mutex);
3122}
Michael Buesch21a75d72008-04-25 19:29:08 +02003123
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003124static int b43_op_tx(struct ieee80211_hw *hw,
3125 struct sk_buff *skb)
3126{
3127 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc9e8eae2008-06-15 15:17:29 +02003128
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003129 if (unlikely(skb->len < 2 + 2 + 6)) {
3130 /* Too short, this can't be a valid frame. */
3131 dev_kfree_skb_any(skb);
3132 return NETDEV_TX_OK;
3133 }
3134 B43_WARN_ON(skb_shinfo(skb)->nr_frags);
3135
3136 skb_queue_tail(&wl->tx_queue, skb);
3137 ieee80211_queue_work(wl->hw, &wl->tx_work);
3138
Michael Buesche4d6b792007-09-18 15:39:42 -04003139 return NETDEV_TX_OK;
3140}
3141
Michael Buesche6f5b932008-03-05 21:18:49 +01003142/* Locking: wl->irq_lock */
3143static void b43_qos_params_upload(struct b43_wldev *dev,
3144 const struct ieee80211_tx_queue_params *p,
3145 u16 shm_offset)
3146{
3147 u16 params[B43_NR_QOSPARAMS];
Johannes Berg0b576642008-07-15 02:08:24 -07003148 int bslots, tmp;
Michael Buesche6f5b932008-03-05 21:18:49 +01003149 unsigned int i;
3150
Johannes Berg0b576642008-07-15 02:08:24 -07003151 bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
Michael Buesche6f5b932008-03-05 21:18:49 +01003152
3153 memset(&params, 0, sizeof(params));
3154
3155 params[B43_QOSPARAM_TXOP] = p->txop * 32;
Johannes Berg0b576642008-07-15 02:08:24 -07003156 params[B43_QOSPARAM_CWMIN] = p->cw_min;
3157 params[B43_QOSPARAM_CWMAX] = p->cw_max;
3158 params[B43_QOSPARAM_CWCUR] = p->cw_min;
3159 params[B43_QOSPARAM_AIFS] = p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003160 params[B43_QOSPARAM_BSLOTS] = bslots;
Johannes Berg0b576642008-07-15 02:08:24 -07003161 params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003162
3163 for (i = 0; i < ARRAY_SIZE(params); i++) {
3164 if (i == B43_QOSPARAM_STATUS) {
3165 tmp = b43_shm_read16(dev, B43_SHM_SHARED,
3166 shm_offset + (i * 2));
3167 /* Mark the parameters as updated. */
3168 tmp |= 0x100;
3169 b43_shm_write16(dev, B43_SHM_SHARED,
3170 shm_offset + (i * 2),
3171 tmp);
3172 } else {
3173 b43_shm_write16(dev, B43_SHM_SHARED,
3174 shm_offset + (i * 2),
3175 params[i]);
3176 }
3177 }
3178}
3179
Michael Bueschc40c1122008-09-06 16:21:47 +02003180/* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
3181static const u16 b43_qos_shm_offsets[] = {
3182 /* [mac80211-queue-nr] = SHM_OFFSET, */
3183 [0] = B43_QOS_VOICE,
3184 [1] = B43_QOS_VIDEO,
3185 [2] = B43_QOS_BESTEFFORT,
3186 [3] = B43_QOS_BACKGROUND,
3187};
3188
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003189/* Update all QOS parameters in hardware. */
3190static void b43_qos_upload_all(struct b43_wldev *dev)
Michael Buesche6f5b932008-03-05 21:18:49 +01003191{
3192 struct b43_wl *wl = dev->wl;
3193 struct b43_qos_params *params;
Michael Buesche6f5b932008-03-05 21:18:49 +01003194 unsigned int i;
3195
Michael Bueschc40c1122008-09-06 16:21:47 +02003196 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3197 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003198
3199 b43_mac_suspend(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003200 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3201 params = &(wl->qos_params[i]);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003202 b43_qos_params_upload(dev, &(params->p),
3203 b43_qos_shm_offsets[i]);
Michael Buesche6f5b932008-03-05 21:18:49 +01003204 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003205 b43_mac_enable(dev);
3206}
3207
3208static void b43_qos_clear(struct b43_wl *wl)
3209{
3210 struct b43_qos_params *params;
3211 unsigned int i;
3212
Michael Bueschc40c1122008-09-06 16:21:47 +02003213 /* Initialize QoS parameters to sane defaults. */
3214
3215 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3216 ARRAY_SIZE(wl->qos_params));
3217
Michael Buesche6f5b932008-03-05 21:18:49 +01003218 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3219 params = &(wl->qos_params[i]);
3220
Michael Bueschc40c1122008-09-06 16:21:47 +02003221 switch (b43_qos_shm_offsets[i]) {
3222 case B43_QOS_VOICE:
3223 params->p.txop = 0;
3224 params->p.aifs = 2;
3225 params->p.cw_min = 0x0001;
3226 params->p.cw_max = 0x0001;
3227 break;
3228 case B43_QOS_VIDEO:
3229 params->p.txop = 0;
3230 params->p.aifs = 2;
3231 params->p.cw_min = 0x0001;
3232 params->p.cw_max = 0x0001;
3233 break;
3234 case B43_QOS_BESTEFFORT:
3235 params->p.txop = 0;
3236 params->p.aifs = 3;
3237 params->p.cw_min = 0x0001;
3238 params->p.cw_max = 0x03FF;
3239 break;
3240 case B43_QOS_BACKGROUND:
3241 params->p.txop = 0;
3242 params->p.aifs = 7;
3243 params->p.cw_min = 0x0001;
3244 params->p.cw_max = 0x03FF;
3245 break;
3246 default:
3247 B43_WARN_ON(1);
3248 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003249 }
3250}
3251
3252/* Initialize the core's QOS capabilities */
3253static void b43_qos_init(struct b43_wldev *dev)
3254{
Michael Buesche6f5b932008-03-05 21:18:49 +01003255 /* Upload the current QOS parameters. */
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003256 b43_qos_upload_all(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003257
3258 /* Enable QOS support. */
3259 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
3260 b43_write16(dev, B43_MMIO_IFSCTL,
3261 b43_read16(dev, B43_MMIO_IFSCTL)
3262 | B43_MMIO_IFSCTL_USE_EDCF);
3263}
3264
Johannes Berge100bb62008-04-30 18:51:21 +02003265static int b43_op_conf_tx(struct ieee80211_hw *hw, u16 _queue,
Michael Buesch40faacc2007-10-28 16:29:32 +01003266 const struct ieee80211_tx_queue_params *params)
Michael Buesche4d6b792007-09-18 15:39:42 -04003267{
Michael Buesche6f5b932008-03-05 21:18:49 +01003268 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003269 struct b43_wldev *dev;
Michael Buesche6f5b932008-03-05 21:18:49 +01003270 unsigned int queue = (unsigned int)_queue;
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003271 int err = -ENODEV;
Michael Buesche6f5b932008-03-05 21:18:49 +01003272
3273 if (queue >= ARRAY_SIZE(wl->qos_params)) {
3274 /* Queue not available or don't support setting
3275 * params on this queue. Return success to not
3276 * confuse mac80211. */
3277 return 0;
3278 }
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003279 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3280 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003281
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003282 mutex_lock(&wl->mutex);
3283 dev = wl->current_dev;
3284 if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
3285 goto out_unlock;
Michael Buesche6f5b932008-03-05 21:18:49 +01003286
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003287 memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
3288 b43_mac_suspend(dev);
3289 b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
3290 b43_qos_shm_offsets[queue]);
3291 b43_mac_enable(dev);
3292 err = 0;
Michael Buesche6f5b932008-03-05 21:18:49 +01003293
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003294out_unlock:
3295 mutex_unlock(&wl->mutex);
3296
3297 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003298}
3299
Michael Buesch40faacc2007-10-28 16:29:32 +01003300static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
3301 struct ieee80211_tx_queue_stats *stats)
Michael Buesche4d6b792007-09-18 15:39:42 -04003302{
3303 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch36dbd952009-09-04 22:51:29 +02003304 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04003305 int err = -ENODEV;
3306
Michael Buesch36dbd952009-09-04 22:51:29 +02003307 mutex_lock(&wl->mutex);
3308 dev = wl->current_dev;
3309 if (dev && b43_status(dev) >= B43_STAT_STARTED) {
Michael Buesch5100d5a2008-03-29 21:01:16 +01003310 if (b43_using_pio_transfers(dev))
3311 b43_pio_get_tx_stats(dev, stats);
3312 else
3313 b43_dma_get_tx_stats(dev, stats);
Michael Buesche4d6b792007-09-18 15:39:42 -04003314 err = 0;
3315 }
Michael Buesch36dbd952009-09-04 22:51:29 +02003316 mutex_unlock(&wl->mutex);
3317
Michael Buesche4d6b792007-09-18 15:39:42 -04003318 return err;
3319}
3320
Michael Buesch40faacc2007-10-28 16:29:32 +01003321static int b43_op_get_stats(struct ieee80211_hw *hw,
3322 struct ieee80211_low_level_stats *stats)
Michael Buesche4d6b792007-09-18 15:39:42 -04003323{
3324 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04003325
Michael Buesch36dbd952009-09-04 22:51:29 +02003326 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003327 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
Michael Buesch36dbd952009-09-04 22:51:29 +02003328 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003329
3330 return 0;
3331}
3332
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003333static u64 b43_op_get_tsf(struct ieee80211_hw *hw)
3334{
3335 struct b43_wl *wl = hw_to_b43_wl(hw);
3336 struct b43_wldev *dev;
3337 u64 tsf;
3338
3339 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003340 dev = wl->current_dev;
3341
3342 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3343 b43_tsf_read(dev, &tsf);
3344 else
3345 tsf = 0;
3346
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003347 mutex_unlock(&wl->mutex);
3348
3349 return tsf;
3350}
3351
3352static void b43_op_set_tsf(struct ieee80211_hw *hw, u64 tsf)
3353{
3354 struct b43_wl *wl = hw_to_b43_wl(hw);
3355 struct b43_wldev *dev;
3356
3357 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003358 dev = wl->current_dev;
3359
3360 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3361 b43_tsf_write(dev, tsf);
3362
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003363 mutex_unlock(&wl->mutex);
3364}
3365
Michael Buesche4d6b792007-09-18 15:39:42 -04003366static void b43_put_phy_into_reset(struct b43_wldev *dev)
3367{
3368 struct ssb_device *sdev = dev->dev;
3369 u32 tmslow;
3370
3371 tmslow = ssb_read32(sdev, SSB_TMSLOW);
3372 tmslow &= ~B43_TMSLOW_GMODE;
3373 tmslow |= B43_TMSLOW_PHYRESET;
3374 tmslow |= SSB_TMSLOW_FGC;
3375 ssb_write32(sdev, SSB_TMSLOW, tmslow);
3376 msleep(1);
3377
3378 tmslow = ssb_read32(sdev, SSB_TMSLOW);
3379 tmslow &= ~SSB_TMSLOW_FGC;
3380 tmslow |= B43_TMSLOW_PHYRESET;
3381 ssb_write32(sdev, SSB_TMSLOW, tmslow);
3382 msleep(1);
3383}
3384
John Daiker99da1852009-02-24 02:16:42 -08003385static const char *band_to_string(enum ieee80211_band band)
Michael Buesche4d6b792007-09-18 15:39:42 -04003386{
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003387 switch (band) {
3388 case IEEE80211_BAND_5GHZ:
3389 return "5";
3390 case IEEE80211_BAND_2GHZ:
3391 return "2.4";
3392 default:
3393 break;
3394 }
3395 B43_WARN_ON(1);
3396 return "";
3397}
3398
3399/* Expects wl->mutex locked */
3400static int b43_switch_band(struct b43_wl *wl, struct ieee80211_channel *chan)
3401{
3402 struct b43_wldev *up_dev = NULL;
Michael Buesche4d6b792007-09-18 15:39:42 -04003403 struct b43_wldev *down_dev;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003404 struct b43_wldev *d;
Michael Buesche4d6b792007-09-18 15:39:42 -04003405 int err;
John W. Linville922d8a02009-01-12 14:40:20 -05003406 bool uninitialized_var(gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04003407 int prev_status;
3408
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003409 /* Find a device and PHY which supports the band. */
3410 list_for_each_entry(d, &wl->devlist, list) {
3411 switch (chan->band) {
3412 case IEEE80211_BAND_5GHZ:
3413 if (d->phy.supports_5ghz) {
3414 up_dev = d;
3415 gmode = 0;
3416 }
3417 break;
3418 case IEEE80211_BAND_2GHZ:
3419 if (d->phy.supports_2ghz) {
3420 up_dev = d;
3421 gmode = 1;
3422 }
3423 break;
3424 default:
3425 B43_WARN_ON(1);
3426 return -EINVAL;
3427 }
3428 if (up_dev)
3429 break;
3430 }
3431 if (!up_dev) {
3432 b43err(wl, "Could not find a device for %s-GHz band operation\n",
3433 band_to_string(chan->band));
3434 return -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003435 }
3436 if ((up_dev == wl->current_dev) &&
3437 (!!wl->current_dev->phy.gmode == !!gmode)) {
3438 /* This device is already running. */
3439 return 0;
3440 }
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003441 b43dbg(wl, "Switching to %s-GHz band\n",
3442 band_to_string(chan->band));
Michael Buesche4d6b792007-09-18 15:39:42 -04003443 down_dev = wl->current_dev;
3444
3445 prev_status = b43_status(down_dev);
3446 /* Shutdown the currently running core. */
3447 if (prev_status >= B43_STAT_STARTED)
Michael Buesch36dbd952009-09-04 22:51:29 +02003448 down_dev = b43_wireless_core_stop(down_dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003449 if (prev_status >= B43_STAT_INITIALIZED)
3450 b43_wireless_core_exit(down_dev);
3451
3452 if (down_dev != up_dev) {
3453 /* We switch to a different core, so we put PHY into
3454 * RESET on the old core. */
3455 b43_put_phy_into_reset(down_dev);
3456 }
3457
3458 /* Now start the new core. */
3459 up_dev->phy.gmode = gmode;
3460 if (prev_status >= B43_STAT_INITIALIZED) {
3461 err = b43_wireless_core_init(up_dev);
3462 if (err) {
3463 b43err(wl, "Fatal: Could not initialize device for "
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003464 "selected %s-GHz band\n",
3465 band_to_string(chan->band));
Michael Buesche4d6b792007-09-18 15:39:42 -04003466 goto init_failure;
3467 }
3468 }
3469 if (prev_status >= B43_STAT_STARTED) {
3470 err = b43_wireless_core_start(up_dev);
3471 if (err) {
3472 b43err(wl, "Fatal: Coult not start device for "
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003473 "selected %s-GHz band\n",
3474 band_to_string(chan->band));
Michael Buesche4d6b792007-09-18 15:39:42 -04003475 b43_wireless_core_exit(up_dev);
3476 goto init_failure;
3477 }
3478 }
3479 B43_WARN_ON(b43_status(up_dev) != prev_status);
3480
3481 wl->current_dev = up_dev;
3482
3483 return 0;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003484init_failure:
Michael Buesche4d6b792007-09-18 15:39:42 -04003485 /* Whoops, failed to init the new core. No core is operating now. */
3486 wl->current_dev = NULL;
3487 return err;
3488}
3489
Johannes Berg9124b072008-10-14 19:17:54 +02003490/* Write the short and long frame retry limit values. */
3491static void b43_set_retry_limits(struct b43_wldev *dev,
3492 unsigned int short_retry,
3493 unsigned int long_retry)
3494{
3495 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3496 * the chip-internal counter. */
3497 short_retry = min(short_retry, (unsigned int)0xF);
3498 long_retry = min(long_retry, (unsigned int)0xF);
3499
3500 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
3501 short_retry);
3502 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
3503 long_retry);
3504}
3505
Johannes Berge8975582008-10-09 12:18:51 +02003506static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
Michael Buesche4d6b792007-09-18 15:39:42 -04003507{
3508 struct b43_wl *wl = hw_to_b43_wl(hw);
3509 struct b43_wldev *dev;
3510 struct b43_phy *phy;
Johannes Berge8975582008-10-09 12:18:51 +02003511 struct ieee80211_conf *conf = &hw->conf;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003512 int antenna;
Michael Buesche4d6b792007-09-18 15:39:42 -04003513 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003514
Michael Buesche4d6b792007-09-18 15:39:42 -04003515 mutex_lock(&wl->mutex);
3516
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003517 /* Switch the band (if necessary). This might change the active core. */
3518 err = b43_switch_band(wl, conf->channel);
Michael Buesche4d6b792007-09-18 15:39:42 -04003519 if (err)
3520 goto out_unlock_mutex;
3521 dev = wl->current_dev;
3522 phy = &dev->phy;
3523
Michael Bueschd10d0e52008-12-18 22:13:39 +01003524 b43_mac_suspend(dev);
3525
Johannes Berg9124b072008-10-14 19:17:54 +02003526 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
3527 b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
3528 conf->long_frame_max_tx_count);
3529 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
3530 if (!changed)
Michael Bueschd10d0e52008-12-18 22:13:39 +01003531 goto out_mac_enable;
Michael Buesche4d6b792007-09-18 15:39:42 -04003532
3533 /* Switch to the requested channel.
3534 * The firmware takes care of races with the TX handler. */
Johannes Berg8318d782008-01-24 19:38:38 +01003535 if (conf->channel->hw_value != phy->channel)
Michael Bueschef1a6282008-08-27 18:53:02 +02003536 b43_switch_channel(dev, conf->channel->hw_value);
Michael Buesche4d6b792007-09-18 15:39:42 -04003537
Johannes Bergd42ce842007-11-23 14:50:51 +01003538 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
3539
Michael Buesche4d6b792007-09-18 15:39:42 -04003540 /* Adjust the desired TX power level. */
3541 if (conf->power_level != 0) {
Michael Buesch18c8ade2008-08-28 19:33:40 +02003542 if (conf->power_level != phy->desired_txpower) {
3543 phy->desired_txpower = conf->power_level;
3544 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
3545 B43_TXPWR_IGNORE_TSSI);
Michael Buesche4d6b792007-09-18 15:39:42 -04003546 }
3547 }
3548
3549 /* Antennas for RX and management frame TX. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02003550 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003551 b43_mgmtframe_txantenna(dev, antenna);
Johannes Berg0f4ac382008-10-09 12:18:04 +02003552 antenna = B43_ANTENNA_DEFAULT;
Michael Bueschef1a6282008-08-27 18:53:02 +02003553 if (phy->ops->set_rx_antenna)
3554 phy->ops->set_rx_antenna(dev, antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003555
Larry Fingerfd4973c2009-06-20 12:58:11 -05003556 if (wl->radio_enabled != phy->radio_on) {
3557 if (wl->radio_enabled) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003558 b43_software_rfkill(dev, false);
Michael Bueschfda9abc2007-09-20 22:14:18 +02003559 b43info(dev->wl, "Radio turned on by software\n");
3560 if (!dev->radio_hw_enable) {
3561 b43info(dev->wl, "The hardware RF-kill button "
3562 "still turns the radio physically off. "
3563 "Press the button to turn it on.\n");
3564 }
3565 } else {
Johannes Berg19d337d2009-06-02 13:01:37 +02003566 b43_software_rfkill(dev, true);
Michael Bueschfda9abc2007-09-20 22:14:18 +02003567 b43info(dev->wl, "Radio turned off by software\n");
3568 }
3569 }
3570
Michael Bueschd10d0e52008-12-18 22:13:39 +01003571out_mac_enable:
3572 b43_mac_enable(dev);
3573out_unlock_mutex:
Michael Buesche4d6b792007-09-18 15:39:42 -04003574 mutex_unlock(&wl->mutex);
3575
3576 return err;
3577}
3578
Johannes Berg881d9482009-01-21 15:13:48 +01003579static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003580{
3581 struct ieee80211_supported_band *sband =
3582 dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
3583 struct ieee80211_rate *rate;
3584 int i;
3585 u16 basic, direct, offset, basic_offset, rateptr;
3586
3587 for (i = 0; i < sband->n_bitrates; i++) {
3588 rate = &sband->bitrates[i];
3589
3590 if (b43_is_cck_rate(rate->hw_value)) {
3591 direct = B43_SHM_SH_CCKDIRECT;
3592 basic = B43_SHM_SH_CCKBASIC;
3593 offset = b43_plcp_get_ratecode_cck(rate->hw_value);
3594 offset &= 0xF;
3595 } else {
3596 direct = B43_SHM_SH_OFDMDIRECT;
3597 basic = B43_SHM_SH_OFDMBASIC;
3598 offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
3599 offset &= 0xF;
3600 }
3601
3602 rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
3603
3604 if (b43_is_cck_rate(rate->hw_value)) {
3605 basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
3606 basic_offset &= 0xF;
3607 } else {
3608 basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
3609 basic_offset &= 0xF;
3610 }
3611
3612 /*
3613 * Get the pointer that we need to point to
3614 * from the direct map
3615 */
3616 rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
3617 direct + 2 * basic_offset);
3618 /* and write it to the basic map */
3619 b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
3620 rateptr);
3621 }
3622}
3623
3624static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
3625 struct ieee80211_vif *vif,
3626 struct ieee80211_bss_conf *conf,
3627 u32 changed)
3628{
3629 struct b43_wl *wl = hw_to_b43_wl(hw);
3630 struct b43_wldev *dev;
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003631
3632 mutex_lock(&wl->mutex);
3633
3634 dev = wl->current_dev;
Michael Bueschd10d0e52008-12-18 22:13:39 +01003635 if (!dev || b43_status(dev) < B43_STAT_STARTED)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003636 goto out_unlock_mutex;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003637
3638 B43_WARN_ON(wl->vif != vif);
3639
3640 if (changed & BSS_CHANGED_BSSID) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003641 if (conf->bssid)
3642 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
3643 else
3644 memset(wl->bssid, 0, ETH_ALEN);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003645 }
3646
Johannes Berg3f0d8432009-05-18 10:53:18 +02003647 if (b43_status(dev) >= B43_STAT_INITIALIZED) {
3648 if (changed & BSS_CHANGED_BEACON &&
3649 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3650 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
3651 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
3652 b43_update_templates(wl);
3653
3654 if (changed & BSS_CHANGED_BSSID)
3655 b43_write_mac_bssid_templates(dev);
3656 }
Johannes Berg3f0d8432009-05-18 10:53:18 +02003657
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003658 b43_mac_suspend(dev);
3659
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003660 /* Update templates for AP/mesh mode. */
3661 if (changed & BSS_CHANGED_BEACON_INT &&
3662 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3663 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
3664 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
3665 b43_set_beacon_int(dev, conf->beacon_int);
3666
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003667 if (changed & BSS_CHANGED_BASIC_RATES)
3668 b43_update_basic_rates(dev, conf->basic_rates);
3669
3670 if (changed & BSS_CHANGED_ERP_SLOT) {
3671 if (conf->use_short_slot)
3672 b43_short_slot_timing_enable(dev);
3673 else
3674 b43_short_slot_timing_disable(dev);
3675 }
3676
3677 b43_mac_enable(dev);
Michael Bueschd10d0e52008-12-18 22:13:39 +01003678out_unlock_mutex:
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003679 mutex_unlock(&wl->mutex);
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003680}
3681
Michael Buesch40faacc2007-10-28 16:29:32 +01003682static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003683 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3684 struct ieee80211_key_conf *key)
Michael Buesche4d6b792007-09-18 15:39:42 -04003685{
3686 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01003687 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04003688 u8 algorithm;
3689 u8 index;
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01003690 int err;
Michael Buesch060210f2009-01-25 15:49:59 +01003691 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Michael Buesche4d6b792007-09-18 15:39:42 -04003692
3693 if (modparam_nohwcrypt)
3694 return -ENOSPC; /* User disabled HW-crypto */
3695
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01003696 mutex_lock(&wl->mutex);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01003697
3698 dev = wl->current_dev;
3699 err = -ENODEV;
3700 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
3701 goto out_unlock;
3702
Michael Buesch403a3a12009-06-08 21:04:57 +02003703 if (dev->fw.pcm_request_failed || !dev->hwcrypto_enabled) {
Michael Buesch68217832008-05-17 23:43:57 +02003704 /* We don't have firmware for the crypto engine.
3705 * Must use software-crypto. */
3706 err = -EOPNOTSUPP;
3707 goto out_unlock;
3708 }
3709
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01003710 err = -EINVAL;
Michael Buesche4d6b792007-09-18 15:39:42 -04003711 switch (key->alg) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003712 case ALG_WEP:
Zhu Yie31a16d2009-05-21 21:47:03 +08003713 if (key->keylen == WLAN_KEY_LEN_WEP40)
Michael Buesche4d6b792007-09-18 15:39:42 -04003714 algorithm = B43_SEC_ALGO_WEP40;
3715 else
3716 algorithm = B43_SEC_ALGO_WEP104;
3717 break;
3718 case ALG_TKIP:
3719 algorithm = B43_SEC_ALGO_TKIP;
3720 break;
3721 case ALG_CCMP:
3722 algorithm = B43_SEC_ALGO_AES;
3723 break;
3724 default:
3725 B43_WARN_ON(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003726 goto out_unlock;
3727 }
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01003728 index = (u8) (key->keyidx);
3729 if (index > 3)
3730 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04003731
3732 switch (cmd) {
3733 case SET_KEY:
gregor kowski035d0242009-08-19 22:35:45 +02003734 if (algorithm == B43_SEC_ALGO_TKIP &&
3735 (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3736 !modparam_hwtkip)) {
3737 /* We support only pairwise key */
Michael Buesche4d6b792007-09-18 15:39:42 -04003738 err = -EOPNOTSUPP;
3739 goto out_unlock;
3740 }
3741
Michael Buesche808e582008-12-19 21:30:52 +01003742 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
Johannes Bergdc822b52008-12-29 12:55:09 +01003743 if (WARN_ON(!sta)) {
3744 err = -EOPNOTSUPP;
3745 goto out_unlock;
3746 }
Michael Buesche808e582008-12-19 21:30:52 +01003747 /* Pairwise key with an assigned MAC address. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003748 err = b43_key_write(dev, -1, algorithm,
Johannes Bergdc822b52008-12-29 12:55:09 +01003749 key->key, key->keylen,
3750 sta->addr, key);
Michael Buesche808e582008-12-19 21:30:52 +01003751 } else {
3752 /* Group key */
3753 err = b43_key_write(dev, index, algorithm,
3754 key->key, key->keylen, NULL, key);
Michael Buesche4d6b792007-09-18 15:39:42 -04003755 }
3756 if (err)
3757 goto out_unlock;
3758
3759 if (algorithm == B43_SEC_ALGO_WEP40 ||
3760 algorithm == B43_SEC_ALGO_WEP104) {
3761 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
3762 } else {
3763 b43_hf_write(dev,
3764 b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
3765 }
3766 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
gregor kowski035d0242009-08-19 22:35:45 +02003767 if (algorithm == B43_SEC_ALGO_TKIP)
3768 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Michael Buesche4d6b792007-09-18 15:39:42 -04003769 break;
3770 case DISABLE_KEY: {
3771 err = b43_key_clear(dev, key->hw_key_idx);
3772 if (err)
3773 goto out_unlock;
3774 break;
3775 }
3776 default:
3777 B43_WARN_ON(1);
3778 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01003779
Michael Buesche4d6b792007-09-18 15:39:42 -04003780out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04003781 if (!err) {
3782 b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
Johannes Berge1749612008-10-27 15:59:26 -07003783 "mac: %pM\n",
Michael Buesche4d6b792007-09-18 15:39:42 -04003784 cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
Larry Fingera1d882102009-01-14 11:15:25 -06003785 sta ? sta->addr : bcast_addr);
Michael Buesch9cf7f242008-12-19 20:24:30 +01003786 b43_dump_keymemory(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003787 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01003788 mutex_unlock(&wl->mutex);
3789
Michael Buesche4d6b792007-09-18 15:39:42 -04003790 return err;
3791}
3792
Michael Buesch40faacc2007-10-28 16:29:32 +01003793static void b43_op_configure_filter(struct ieee80211_hw *hw,
3794 unsigned int changed, unsigned int *fflags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003795 u64 multicast)
Michael Buesche4d6b792007-09-18 15:39:42 -04003796{
3797 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch36dbd952009-09-04 22:51:29 +02003798 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04003799
Michael Buesch36dbd952009-09-04 22:51:29 +02003800 mutex_lock(&wl->mutex);
3801 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04003802 if (!dev) {
3803 *fflags = 0;
Michael Buesch36dbd952009-09-04 22:51:29 +02003804 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04003805 }
Johannes Berg4150c572007-09-17 01:29:23 -04003806
Johannes Berg4150c572007-09-17 01:29:23 -04003807 *fflags &= FIF_PROMISC_IN_BSS |
3808 FIF_ALLMULTI |
3809 FIF_FCSFAIL |
3810 FIF_PLCPFAIL |
3811 FIF_CONTROL |
3812 FIF_OTHER_BSS |
3813 FIF_BCN_PRBRESP_PROMISC;
3814
3815 changed &= FIF_PROMISC_IN_BSS |
3816 FIF_ALLMULTI |
3817 FIF_FCSFAIL |
3818 FIF_PLCPFAIL |
3819 FIF_CONTROL |
3820 FIF_OTHER_BSS |
3821 FIF_BCN_PRBRESP_PROMISC;
3822
3823 wl->filter_flags = *fflags;
3824
3825 if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
3826 b43_adjust_opmode(dev);
Michael Buesch36dbd952009-09-04 22:51:29 +02003827
3828out_unlock:
3829 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003830}
3831
Michael Buesch36dbd952009-09-04 22:51:29 +02003832/* Locking: wl->mutex
3833 * Returns the current dev. This might be different from the passed in dev,
3834 * because the core might be gone away while we unlocked the mutex. */
3835static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04003836{
3837 struct b43_wl *wl = dev->wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02003838 struct b43_wldev *orig_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04003839
Michael Buesch36dbd952009-09-04 22:51:29 +02003840redo:
3841 if (!dev || b43_status(dev) < B43_STAT_STARTED)
3842 return dev;
Stefano Brivioa19d12d2007-11-07 18:16:11 +01003843
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003844 /* Cancel work. Unlock to avoid deadlocks. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003845 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003846 cancel_delayed_work_sync(&dev->periodic_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003847 cancel_work_sync(&wl->tx_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04003848 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02003849 dev = wl->current_dev;
3850 if (!dev || b43_status(dev) < B43_STAT_STARTED) {
3851 /* Whoops, aliens ate up the device while we were unlocked. */
3852 return dev;
3853 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003854
Michael Buesch36dbd952009-09-04 22:51:29 +02003855 /* Disable interrupts on the device. */
3856 b43_set_status(dev, B43_STAT_INITIALIZED);
3857 if (0 /*FIXME dev->dev->bus->bustype == SSB_BUSTYPE_SDIO*/) {
3858 /* wl->mutex is locked. That is enough. */
3859 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
3860 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
3861 } else {
3862 spin_lock_irq(&wl->hardirq_lock);
3863 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
3864 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
3865 spin_unlock_irq(&wl->hardirq_lock);
3866 }
3867 /* Synchronize the interrupt handlers. Unlock to avoid deadlocks. */
3868 orig_dev = dev;
3869 mutex_unlock(&wl->mutex);
3870 synchronize_irq(dev->dev->irq);
3871 mutex_lock(&wl->mutex);
3872 dev = wl->current_dev;
3873 if (!dev)
3874 return dev;
3875 if (dev != orig_dev) {
3876 if (b43_status(dev) >= B43_STAT_STARTED)
3877 goto redo;
3878 return dev;
3879 }
3880 B43_WARN_ON(b43_read32(dev, B43_MMIO_GEN_IRQ_MASK));
3881
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003882 /* Drain the TX queue */
3883 while (skb_queue_len(&wl->tx_queue))
3884 dev_kfree_skb(skb_dequeue(&wl->tx_queue));
3885
Michael Buesch36dbd952009-09-04 22:51:29 +02003886 b43_pio_stop(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003887 b43_mac_suspend(dev);
3888 free_irq(dev->dev->irq, dev);
3889 b43dbg(wl, "Wireless interface stopped\n");
Michael Buesch36dbd952009-09-04 22:51:29 +02003890
3891 return dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04003892}
3893
3894/* Locking: wl->mutex */
3895static int b43_wireless_core_start(struct b43_wldev *dev)
3896{
3897 int err;
3898
3899 B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
3900
3901 drain_txstatus_queue(dev);
Michael Buesch36dbd952009-09-04 22:51:29 +02003902 err = request_threaded_irq(dev->dev->irq, b43_interrupt_handler,
3903 b43_interrupt_thread_handler,
3904 IRQF_SHARED, KBUILD_MODNAME, dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003905 if (err) {
3906 b43err(dev->wl, "Cannot request IRQ-%d\n", dev->dev->irq);
3907 goto out;
3908 }
3909
3910 /* We are ready to run. */
3911 b43_set_status(dev, B43_STAT_STARTED);
3912
3913 /* Start data flow (TX/RX). */
3914 b43_mac_enable(dev);
Michael Buesch13790722009-04-08 21:26:27 +02003915 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesche4d6b792007-09-18 15:39:42 -04003916
3917 /* Start maintainance work */
3918 b43_periodic_tasks_setup(dev);
3919
3920 b43dbg(dev->wl, "Wireless interface started\n");
3921 out:
3922 return err;
3923}
3924
3925/* Get PHY and RADIO versioning numbers */
3926static int b43_phy_versioning(struct b43_wldev *dev)
3927{
3928 struct b43_phy *phy = &dev->phy;
3929 u32 tmp;
3930 u8 analog_type;
3931 u8 phy_type;
3932 u8 phy_rev;
3933 u16 radio_manuf;
3934 u16 radio_ver;
3935 u16 radio_rev;
3936 int unsupported = 0;
3937
3938 /* Get PHY versioning */
3939 tmp = b43_read16(dev, B43_MMIO_PHY_VER);
3940 analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
3941 phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
3942 phy_rev = (tmp & B43_PHYVER_VERSION);
3943 switch (phy_type) {
3944 case B43_PHYTYPE_A:
3945 if (phy_rev >= 4)
3946 unsupported = 1;
3947 break;
3948 case B43_PHYTYPE_B:
3949 if (phy_rev != 2 && phy_rev != 4 && phy_rev != 6
3950 && phy_rev != 7)
3951 unsupported = 1;
3952 break;
3953 case B43_PHYTYPE_G:
Larry Finger013978b2007-11-26 10:29:47 -06003954 if (phy_rev > 9)
Michael Buesche4d6b792007-09-18 15:39:42 -04003955 unsupported = 1;
3956 break;
Michael Bueschd5c71e42008-01-04 17:06:29 +01003957#ifdef CONFIG_B43_NPHY
3958 case B43_PHYTYPE_N:
Johannes Bergbb519be2008-12-24 15:26:40 +01003959 if (phy_rev > 4)
Michael Bueschd5c71e42008-01-04 17:06:29 +01003960 unsupported = 1;
3961 break;
3962#endif
Michael Buesch6b1c7c62008-12-25 00:39:28 +01003963#ifdef CONFIG_B43_PHY_LP
3964 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02003965 if (phy_rev > 2)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01003966 unsupported = 1;
3967 break;
3968#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003969 default:
3970 unsupported = 1;
3971 };
3972 if (unsupported) {
3973 b43err(dev->wl, "FOUND UNSUPPORTED PHY "
3974 "(Analog %u, Type %u, Revision %u)\n",
3975 analog_type, phy_type, phy_rev);
3976 return -EOPNOTSUPP;
3977 }
3978 b43dbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
3979 analog_type, phy_type, phy_rev);
3980
3981 /* Get RADIO versioning */
3982 if (dev->dev->bus->chip_id == 0x4317) {
3983 if (dev->dev->bus->chip_rev == 0)
3984 tmp = 0x3205017F;
3985 else if (dev->dev->bus->chip_rev == 1)
3986 tmp = 0x4205017F;
3987 else
3988 tmp = 0x5205017F;
3989 } else {
3990 b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
Michael Buesch243dcfc2008-01-13 14:12:44 +01003991 tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
Michael Buesche4d6b792007-09-18 15:39:42 -04003992 b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
Michael Buesch243dcfc2008-01-13 14:12:44 +01003993 tmp |= (u32)b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH) << 16;
Michael Buesche4d6b792007-09-18 15:39:42 -04003994 }
3995 radio_manuf = (tmp & 0x00000FFF);
3996 radio_ver = (tmp & 0x0FFFF000) >> 12;
3997 radio_rev = (tmp & 0xF0000000) >> 28;
Michael Buesch96c755a2008-01-06 00:09:46 +01003998 if (radio_manuf != 0x17F /* Broadcom */)
3999 unsupported = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004000 switch (phy_type) {
4001 case B43_PHYTYPE_A:
4002 if (radio_ver != 0x2060)
4003 unsupported = 1;
4004 if (radio_rev != 1)
4005 unsupported = 1;
4006 if (radio_manuf != 0x17F)
4007 unsupported = 1;
4008 break;
4009 case B43_PHYTYPE_B:
4010 if ((radio_ver & 0xFFF0) != 0x2050)
4011 unsupported = 1;
4012 break;
4013 case B43_PHYTYPE_G:
4014 if (radio_ver != 0x2050)
4015 unsupported = 1;
4016 break;
Michael Buesch96c755a2008-01-06 00:09:46 +01004017 case B43_PHYTYPE_N:
Johannes Bergbb519be2008-12-24 15:26:40 +01004018 if (radio_ver != 0x2055 && radio_ver != 0x2056)
Michael Buesch96c755a2008-01-06 00:09:46 +01004019 unsupported = 1;
4020 break;
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004021 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004022 if (radio_ver != 0x2062 && radio_ver != 0x2063)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004023 unsupported = 1;
4024 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04004025 default:
4026 B43_WARN_ON(1);
4027 }
4028 if (unsupported) {
4029 b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
4030 "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
4031 radio_manuf, radio_ver, radio_rev);
4032 return -EOPNOTSUPP;
4033 }
4034 b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
4035 radio_manuf, radio_ver, radio_rev);
4036
4037 phy->radio_manuf = radio_manuf;
4038 phy->radio_ver = radio_ver;
4039 phy->radio_rev = radio_rev;
4040
4041 phy->analog = analog_type;
4042 phy->type = phy_type;
4043 phy->rev = phy_rev;
4044
4045 return 0;
4046}
4047
4048static void setup_struct_phy_for_init(struct b43_wldev *dev,
4049 struct b43_phy *phy)
4050{
Michael Buesche4d6b792007-09-18 15:39:42 -04004051 phy->hardware_power_control = !!modparam_hwpctl;
Michael Buesch18c8ade2008-08-28 19:33:40 +02004052 phy->next_txpwr_check_time = jiffies;
Michael Buesch8ed7fc42007-12-09 22:34:59 +01004053 /* PHY TX errors counter. */
4054 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
Michael Buesch591f3dc2009-03-31 12:27:32 +02004055
4056#if B43_DEBUG
4057 phy->phy_locked = 0;
4058 phy->radio_locked = 0;
4059#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004060}
4061
4062static void setup_struct_wldev_for_init(struct b43_wldev *dev)
4063{
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01004064 dev->dfq_valid = 0;
4065
Michael Buesch6a724d62007-09-20 22:12:58 +02004066 /* Assume the radio is enabled. If it's not enabled, the state will
4067 * immediately get fixed on the first periodic work run. */
4068 dev->radio_hw_enable = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004069
4070 /* Stats */
4071 memset(&dev->stats, 0, sizeof(dev->stats));
4072
4073 setup_struct_phy_for_init(dev, &dev->phy);
4074
4075 /* IRQ related flags */
4076 dev->irq_reason = 0;
4077 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
Michael Buesch13790722009-04-08 21:26:27 +02004078 dev->irq_mask = B43_IRQ_MASKTEMPLATE;
Michael Buesch3e3ccb32009-03-19 19:27:21 +01004079 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
Michael Buesch13790722009-04-08 21:26:27 +02004080 dev->irq_mask &= ~B43_IRQ_PHY_TXERR;
Michael Buesche4d6b792007-09-18 15:39:42 -04004081
4082 dev->mac_suspended = 1;
4083
4084 /* Noise calculation context */
4085 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
4086}
4087
4088static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
4089{
4090 struct ssb_sprom *sprom = &dev->dev->bus->sprom;
Michael Buescha259d6a2008-04-18 21:06:37 +02004091 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004092
Michael Buesch1855ba72008-04-18 20:51:41 +02004093 if (!modparam_btcoex)
4094 return;
Larry Finger95de2842007-11-09 16:57:18 -06004095 if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
Michael Buesche4d6b792007-09-18 15:39:42 -04004096 return;
4097 if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
4098 return;
4099
4100 hf = b43_hf_read(dev);
Larry Finger95de2842007-11-09 16:57:18 -06004101 if (sprom->boardflags_lo & B43_BFL_BTCMOD)
Michael Buesche4d6b792007-09-18 15:39:42 -04004102 hf |= B43_HF_BTCOEXALT;
4103 else
4104 hf |= B43_HF_BTCOEX;
4105 b43_hf_write(dev, hf);
Michael Buesche4d6b792007-09-18 15:39:42 -04004106}
4107
4108static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
Michael Buesch1855ba72008-04-18 20:51:41 +02004109{
4110 if (!modparam_btcoex)
4111 return;
4112 //TODO
Michael Buesche4d6b792007-09-18 15:39:42 -04004113}
4114
4115static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
4116{
4117#ifdef CONFIG_SSB_DRIVER_PCICORE
4118 struct ssb_bus *bus = dev->dev->bus;
4119 u32 tmp;
4120
4121 if (bus->pcicore.dev &&
4122 bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
4123 bus->pcicore.dev->id.revision <= 5) {
4124 /* IMCFGLO timeouts workaround. */
4125 tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
4126 tmp &= ~SSB_IMCFGLO_REQTO;
4127 tmp &= ~SSB_IMCFGLO_SERTO;
4128 switch (bus->bustype) {
4129 case SSB_BUSTYPE_PCI:
4130 case SSB_BUSTYPE_PCMCIA:
4131 tmp |= 0x32;
4132 break;
4133 case SSB_BUSTYPE_SSB:
4134 tmp |= 0x53;
4135 break;
4136 }
4137 ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
4138 }
4139#endif /* CONFIG_SSB_DRIVER_PCICORE */
4140}
4141
Michael Bueschd59f7202008-04-03 18:56:19 +02004142static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
4143{
4144 u16 pu_delay;
4145
4146 /* The time value is in microseconds. */
4147 if (dev->phy.type == B43_PHYTYPE_A)
4148 pu_delay = 3700;
4149 else
4150 pu_delay = 1050;
Johannes Berg05c914f2008-09-11 00:01:58 +02004151 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
Michael Bueschd59f7202008-04-03 18:56:19 +02004152 pu_delay = 500;
4153 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
4154 pu_delay = max(pu_delay, (u16)2400);
4155
4156 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
4157}
4158
4159/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
4160static void b43_set_pretbtt(struct b43_wldev *dev)
4161{
4162 u16 pretbtt;
4163
4164 /* The time value is in microseconds. */
Johannes Berg05c914f2008-09-11 00:01:58 +02004165 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
Michael Bueschd59f7202008-04-03 18:56:19 +02004166 pretbtt = 2;
4167 } else {
4168 if (dev->phy.type == B43_PHYTYPE_A)
4169 pretbtt = 120;
4170 else
4171 pretbtt = 250;
4172 }
4173 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
4174 b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
4175}
4176
Michael Buesche4d6b792007-09-18 15:39:42 -04004177/* Shutdown a wireless core */
4178/* Locking: wl->mutex */
4179static void b43_wireless_core_exit(struct b43_wldev *dev)
4180{
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004181 u32 macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04004182
Michael Buesch36dbd952009-09-04 22:51:29 +02004183 B43_WARN_ON(dev && b43_status(dev) > B43_STAT_INITIALIZED);
4184 if (!dev || b43_status(dev) != B43_STAT_INITIALIZED)
Michael Buesche4d6b792007-09-18 15:39:42 -04004185 return;
4186 b43_set_status(dev, B43_STAT_UNINIT);
4187
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004188 /* Stop the microcode PSM. */
4189 macctl = b43_read32(dev, B43_MMIO_MACCTL);
4190 macctl &= ~B43_MACCTL_PSM_RUN;
4191 macctl |= B43_MACCTL_PSM_JMP0;
4192 b43_write32(dev, B43_MMIO_MACCTL, macctl);
4193
Rafael J. Wysocki3506e0c2008-02-04 22:30:15 -08004194 if (!dev->suspend_in_progress) {
4195 b43_leds_exit(dev);
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01004196 b43_rng_exit(dev->wl);
Rafael J. Wysocki3506e0c2008-02-04 22:30:15 -08004197 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004198 b43_dma_free(dev);
Michael Buesch5100d5a2008-03-29 21:01:16 +01004199 b43_pio_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004200 b43_chip_exit(dev);
Michael Bueschcb24f572008-09-03 12:12:20 +02004201 dev->phy.ops->switch_analog(dev, 0);
Michael Buesche66fee62007-12-26 17:47:10 +01004202 if (dev->wl->current_beacon) {
4203 dev_kfree_skb_any(dev->wl->current_beacon);
4204 dev->wl->current_beacon = NULL;
4205 }
4206
Michael Buesche4d6b792007-09-18 15:39:42 -04004207 ssb_device_disable(dev->dev, 0);
4208 ssb_bus_may_powerdown(dev->dev->bus);
4209}
4210
4211/* Initialize a wireless core */
4212static int b43_wireless_core_init(struct b43_wldev *dev)
4213{
4214 struct b43_wl *wl = dev->wl;
4215 struct ssb_bus *bus = dev->dev->bus;
4216 struct ssb_sprom *sprom = &bus->sprom;
4217 struct b43_phy *phy = &dev->phy;
4218 int err;
Michael Buescha259d6a2008-04-18 21:06:37 +02004219 u64 hf;
4220 u32 tmp;
Michael Buesche4d6b792007-09-18 15:39:42 -04004221
4222 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4223
4224 err = ssb_bus_powerup(bus, 0);
4225 if (err)
4226 goto out;
4227 if (!ssb_device_is_enabled(dev->dev)) {
4228 tmp = phy->gmode ? B43_TMSLOW_GMODE : 0;
4229 b43_wireless_core_reset(dev, tmp);
4230 }
4231
Michael Bueschfb111372008-09-02 13:00:34 +02004232 /* Reset all data structures. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004233 setup_struct_wldev_for_init(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004234 phy->ops->prepare_structs(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004235
4236 /* Enable IRQ routing to this device. */
4237 ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
4238
4239 b43_imcfglo_timeouts_workaround(dev);
4240 b43_bluetooth_coext_disable(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004241 if (phy->ops->prepare_hardware) {
4242 err = phy->ops->prepare_hardware(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004243 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004244 goto err_busdown;
Michael Bueschef1a6282008-08-27 18:53:02 +02004245 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004246 err = b43_chip_init(dev);
4247 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004248 goto err_busdown;
Michael Buesche4d6b792007-09-18 15:39:42 -04004249 b43_shm_write16(dev, B43_SHM_SHARED,
4250 B43_SHM_SH_WLCOREREV, dev->dev->id.revision);
4251 hf = b43_hf_read(dev);
4252 if (phy->type == B43_PHYTYPE_G) {
4253 hf |= B43_HF_SYMW;
4254 if (phy->rev == 1)
4255 hf |= B43_HF_GDCW;
Larry Finger95de2842007-11-09 16:57:18 -06004256 if (sprom->boardflags_lo & B43_BFL_PACTRL)
Michael Buesche4d6b792007-09-18 15:39:42 -04004257 hf |= B43_HF_OFDMPABOOST;
Michael Buesch969d15c2009-02-20 14:27:15 +01004258 }
4259 if (phy->radio_ver == 0x2050) {
4260 if (phy->radio_rev == 6)
4261 hf |= B43_HF_4318TSSI;
4262 if (phy->radio_rev < 6)
4263 hf |= B43_HF_VCORECALC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004264 }
Michael Buesch1cc8f472009-02-20 14:47:56 +01004265 if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
4266 hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
Michael Buesch1a777332009-03-04 16:41:10 +01004267#ifdef CONFIG_SSB_DRIVER_PCICORE
Michael Buesch88219052009-02-20 14:58:59 +01004268 if ((bus->bustype == SSB_BUSTYPE_PCI) &&
4269 (bus->pcicore.dev->id.revision <= 10))
4270 hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
Michael Buesch1a777332009-03-04 16:41:10 +01004271#endif
Michael Buesch25d3ef52009-02-20 15:39:21 +01004272 hf &= ~B43_HF_SKCFPUP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004273 b43_hf_write(dev, hf);
4274
Michael Buesch74cfdba2007-10-28 16:19:44 +01004275 b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
4276 B43_DEFAULT_LONG_RETRY_LIMIT);
Michael Buesche4d6b792007-09-18 15:39:42 -04004277 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
4278 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
4279
4280 /* Disable sending probe responses from firmware.
4281 * Setting the MaxTime to one usec will always trigger
4282 * a timeout, so we never send any probe resp.
4283 * A timeout of zero is infinite. */
4284 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
4285
4286 b43_rate_memory_init(dev);
Michael Buesch5042c502008-04-05 15:05:00 +02004287 b43_set_phytxctl_defaults(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004288
4289 /* Minimum Contention Window */
4290 if (phy->type == B43_PHYTYPE_B) {
4291 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
4292 } else {
4293 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
4294 }
4295 /* Maximum Contention Window */
4296 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
4297
Michael Buesch5100d5a2008-03-29 21:01:16 +01004298 if ((dev->dev->bus->bustype == SSB_BUSTYPE_PCMCIA) || B43_FORCE_PIO) {
4299 dev->__using_pio_transfers = 1;
4300 err = b43_pio_init(dev);
4301 } else {
4302 dev->__using_pio_transfers = 0;
4303 err = b43_dma_init(dev);
4304 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004305 if (err)
4306 goto err_chip_exit;
Michael Buesch03b29772007-12-26 14:41:30 +01004307 b43_qos_init(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004308 b43_set_synth_pu_delay(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004309 b43_bluetooth_coext_enable(dev);
4310
Michael Buesch1cc8f472009-02-20 14:47:56 +01004311 ssb_bus_powerup(bus, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
Johannes Berg4150c572007-09-17 01:29:23 -04004312 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004313 b43_security_init(dev);
Rafael J. Wysocki3506e0c2008-02-04 22:30:15 -08004314 if (!dev->suspend_in_progress)
4315 b43_rng_init(wl);
Michael Buesche4d6b792007-09-18 15:39:42 -04004316
4317 b43_set_status(dev, B43_STAT_INITIALIZED);
4318
Rafael J. Wysocki3506e0c2008-02-04 22:30:15 -08004319 if (!dev->suspend_in_progress)
4320 b43_leds_init(dev);
Larry Finger1a8d1222007-12-14 13:59:11 +01004321out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004322 return err;
4323
Michael Bueschef1a6282008-08-27 18:53:02 +02004324err_chip_exit:
Michael Buesche4d6b792007-09-18 15:39:42 -04004325 b43_chip_exit(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004326err_busdown:
Michael Buesche4d6b792007-09-18 15:39:42 -04004327 ssb_bus_may_powerdown(bus);
4328 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4329 return err;
4330}
4331
Michael Buesch40faacc2007-10-28 16:29:32 +01004332static int b43_op_add_interface(struct ieee80211_hw *hw,
4333 struct ieee80211_if_init_conf *conf)
Michael Buesche4d6b792007-09-18 15:39:42 -04004334{
4335 struct b43_wl *wl = hw_to_b43_wl(hw);
4336 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004337 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04004338
4339 /* TODO: allow WDS/AP devices to coexist */
4340
Johannes Berg05c914f2008-09-11 00:01:58 +02004341 if (conf->type != NL80211_IFTYPE_AP &&
4342 conf->type != NL80211_IFTYPE_MESH_POINT &&
4343 conf->type != NL80211_IFTYPE_STATION &&
4344 conf->type != NL80211_IFTYPE_WDS &&
4345 conf->type != NL80211_IFTYPE_ADHOC)
Johannes Berg4150c572007-09-17 01:29:23 -04004346 return -EOPNOTSUPP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004347
4348 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004349 if (wl->operating)
Michael Buesche4d6b792007-09-18 15:39:42 -04004350 goto out_mutex_unlock;
4351
4352 b43dbg(wl, "Adding Interface type %d\n", conf->type);
4353
4354 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04004355 wl->operating = 1;
Johannes Berg32bfd352007-12-19 01:31:26 +01004356 wl->vif = conf->vif;
Johannes Berg4150c572007-09-17 01:29:23 -04004357 wl->if_type = conf->type;
4358 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
Michael Buesche4d6b792007-09-18 15:39:42 -04004359
Michael Buesche4d6b792007-09-18 15:39:42 -04004360 b43_adjust_opmode(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004361 b43_set_pretbtt(dev);
4362 b43_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04004363 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004364
4365 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004366 out_mutex_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004367 mutex_unlock(&wl->mutex);
4368
4369 return err;
4370}
4371
Michael Buesch40faacc2007-10-28 16:29:32 +01004372static void b43_op_remove_interface(struct ieee80211_hw *hw,
4373 struct ieee80211_if_init_conf *conf)
Michael Buesche4d6b792007-09-18 15:39:42 -04004374{
4375 struct b43_wl *wl = hw_to_b43_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04004376 struct b43_wldev *dev = wl->current_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004377
4378 b43dbg(wl, "Removing Interface type %d\n", conf->type);
4379
4380 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004381
4382 B43_WARN_ON(!wl->operating);
Johannes Berg32bfd352007-12-19 01:31:26 +01004383 B43_WARN_ON(wl->vif != conf->vif);
4384 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04004385
4386 wl->operating = 0;
4387
Johannes Berg4150c572007-09-17 01:29:23 -04004388 b43_adjust_opmode(dev);
4389 memset(wl->mac_addr, 0, ETH_ALEN);
4390 b43_upload_card_macaddress(dev);
Johannes Berg4150c572007-09-17 01:29:23 -04004391
4392 mutex_unlock(&wl->mutex);
4393}
4394
Michael Buesch40faacc2007-10-28 16:29:32 +01004395static int b43_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004396{
4397 struct b43_wl *wl = hw_to_b43_wl(hw);
4398 struct b43_wldev *dev = wl->current_dev;
4399 int did_init = 0;
WANG Cong923403b2007-10-16 14:29:38 -07004400 int err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004401
Michael Buesch7be1bb62008-01-23 21:10:56 +01004402 /* Kill all old instance specific information to make sure
4403 * the card won't use it in the short timeframe between start
4404 * and mac80211 reconfiguring it. */
4405 memset(wl->bssid, 0, ETH_ALEN);
4406 memset(wl->mac_addr, 0, ETH_ALEN);
4407 wl->filter_flags = 0;
4408 wl->radiotap_enabled = 0;
Michael Buesche6f5b932008-03-05 21:18:49 +01004409 b43_qos_clear(wl);
Michael Buesch6b4bec012008-05-20 12:16:28 +02004410 wl->beacon0_uploaded = 0;
4411 wl->beacon1_uploaded = 0;
4412 wl->beacon_templates_virgin = 1;
Larry Fingerfd4973c2009-06-20 12:58:11 -05004413 wl->radio_enabled = 1;
Michael Buesch7be1bb62008-01-23 21:10:56 +01004414
Johannes Berg4150c572007-09-17 01:29:23 -04004415 mutex_lock(&wl->mutex);
4416
4417 if (b43_status(dev) < B43_STAT_INITIALIZED) {
4418 err = b43_wireless_core_init(dev);
Johannes Bergf41f3f32009-06-07 12:30:34 -05004419 if (err)
Johannes Berg4150c572007-09-17 01:29:23 -04004420 goto out_mutex_unlock;
4421 did_init = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004422 }
4423
Johannes Berg4150c572007-09-17 01:29:23 -04004424 if (b43_status(dev) < B43_STAT_STARTED) {
4425 err = b43_wireless_core_start(dev);
4426 if (err) {
4427 if (did_init)
4428 b43_wireless_core_exit(dev);
4429 goto out_mutex_unlock;
4430 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004431 }
Johannes Berg4150c572007-09-17 01:29:23 -04004432
Johannes Bergf41f3f32009-06-07 12:30:34 -05004433 /* XXX: only do if device doesn't support rfkill irq */
4434 wiphy_rfkill_start_polling(hw->wiphy);
4435
Johannes Berg4150c572007-09-17 01:29:23 -04004436 out_mutex_unlock:
4437 mutex_unlock(&wl->mutex);
4438
4439 return err;
4440}
4441
Michael Buesch40faacc2007-10-28 16:29:32 +01004442static void b43_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004443{
4444 struct b43_wl *wl = hw_to_b43_wl(hw);
4445 struct b43_wldev *dev = wl->current_dev;
4446
Michael Buescha82d9922008-04-04 21:40:06 +02004447 cancel_work_sync(&(wl->beacon_update_trigger));
Larry Finger1a8d1222007-12-14 13:59:11 +01004448
Johannes Berg4150c572007-09-17 01:29:23 -04004449 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004450 if (b43_status(dev) >= B43_STAT_STARTED) {
4451 dev = b43_wireless_core_stop(dev);
4452 if (!dev)
4453 goto out_unlock;
4454 }
Johannes Berg4150c572007-09-17 01:29:23 -04004455 b43_wireless_core_exit(dev);
Larry Fingerfd4973c2009-06-20 12:58:11 -05004456 wl->radio_enabled = 0;
Michael Buesch36dbd952009-09-04 22:51:29 +02004457
4458out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004459 mutex_unlock(&wl->mutex);
Michael Buesch18c8ade2008-08-28 19:33:40 +02004460
4461 cancel_work_sync(&(wl->txpower_adjust_work));
Michael Buesche4d6b792007-09-18 15:39:42 -04004462}
4463
Johannes Berg17741cd2008-09-11 00:02:02 +02004464static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
4465 struct ieee80211_sta *sta, bool set)
Michael Buesche66fee62007-12-26 17:47:10 +01004466{
4467 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche66fee62007-12-26 17:47:10 +01004468
Michael Buesch36dbd952009-09-04 22:51:29 +02004469 mutex_lock(&wl->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02004470 b43_update_templates(wl);
Michael Buesch36dbd952009-09-04 22:51:29 +02004471 mutex_unlock(&wl->mutex);
Michael Buesche66fee62007-12-26 17:47:10 +01004472
4473 return 0;
4474}
4475
Johannes Berg38968d02008-02-25 16:27:50 +01004476static void b43_op_sta_notify(struct ieee80211_hw *hw,
4477 struct ieee80211_vif *vif,
4478 enum sta_notify_cmd notify_cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02004479 struct ieee80211_sta *sta)
Johannes Berg38968d02008-02-25 16:27:50 +01004480{
4481 struct b43_wl *wl = hw_to_b43_wl(hw);
4482
4483 B43_WARN_ON(!vif || wl->vif != vif);
4484}
4485
Michael Buesch25d3ef52009-02-20 15:39:21 +01004486static void b43_op_sw_scan_start_notifier(struct ieee80211_hw *hw)
4487{
4488 struct b43_wl *wl = hw_to_b43_wl(hw);
4489 struct b43_wldev *dev;
4490
4491 mutex_lock(&wl->mutex);
4492 dev = wl->current_dev;
4493 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
4494 /* Disable CFP update during scan on other channels. */
4495 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
4496 }
4497 mutex_unlock(&wl->mutex);
4498}
4499
4500static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw)
4501{
4502 struct b43_wl *wl = hw_to_b43_wl(hw);
4503 struct b43_wldev *dev;
4504
4505 mutex_lock(&wl->mutex);
4506 dev = wl->current_dev;
4507 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
4508 /* Re-enable CFP update. */
4509 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
4510 }
4511 mutex_unlock(&wl->mutex);
4512}
4513
Michael Buesche4d6b792007-09-18 15:39:42 -04004514static const struct ieee80211_ops b43_hw_ops = {
Michael Buesch40faacc2007-10-28 16:29:32 +01004515 .tx = b43_op_tx,
4516 .conf_tx = b43_op_conf_tx,
4517 .add_interface = b43_op_add_interface,
4518 .remove_interface = b43_op_remove_interface,
4519 .config = b43_op_config,
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004520 .bss_info_changed = b43_op_bss_info_changed,
Michael Buesch40faacc2007-10-28 16:29:32 +01004521 .configure_filter = b43_op_configure_filter,
4522 .set_key = b43_op_set_key,
gregor kowski035d0242009-08-19 22:35:45 +02004523 .update_tkip_key = b43_op_update_tkip_key,
Michael Buesch40faacc2007-10-28 16:29:32 +01004524 .get_stats = b43_op_get_stats,
4525 .get_tx_stats = b43_op_get_tx_stats,
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01004526 .get_tsf = b43_op_get_tsf,
4527 .set_tsf = b43_op_set_tsf,
Michael Buesch40faacc2007-10-28 16:29:32 +01004528 .start = b43_op_start,
4529 .stop = b43_op_stop,
Michael Buesche66fee62007-12-26 17:47:10 +01004530 .set_tim = b43_op_beacon_set_tim,
Johannes Berg38968d02008-02-25 16:27:50 +01004531 .sta_notify = b43_op_sta_notify,
Michael Buesch25d3ef52009-02-20 15:39:21 +01004532 .sw_scan_start = b43_op_sw_scan_start_notifier,
4533 .sw_scan_complete = b43_op_sw_scan_complete_notifier,
Johannes Bergf41f3f32009-06-07 12:30:34 -05004534 .rfkill_poll = b43_rfkill_poll,
Michael Buesche4d6b792007-09-18 15:39:42 -04004535};
4536
4537/* Hard-reset the chip. Do not call this directly.
4538 * Use b43_controller_restart()
4539 */
4540static void b43_chip_reset(struct work_struct *work)
4541{
4542 struct b43_wldev *dev =
4543 container_of(work, struct b43_wldev, restart_work);
4544 struct b43_wl *wl = dev->wl;
4545 int err = 0;
4546 int prev_status;
4547
4548 mutex_lock(&wl->mutex);
4549
4550 prev_status = b43_status(dev);
4551 /* Bring the device down... */
Michael Buesch36dbd952009-09-04 22:51:29 +02004552 if (prev_status >= B43_STAT_STARTED) {
4553 dev = b43_wireless_core_stop(dev);
4554 if (!dev) {
4555 err = -ENODEV;
4556 goto out;
4557 }
4558 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004559 if (prev_status >= B43_STAT_INITIALIZED)
4560 b43_wireless_core_exit(dev);
4561
4562 /* ...and up again. */
4563 if (prev_status >= B43_STAT_INITIALIZED) {
4564 err = b43_wireless_core_init(dev);
4565 if (err)
4566 goto out;
4567 }
4568 if (prev_status >= B43_STAT_STARTED) {
4569 err = b43_wireless_core_start(dev);
4570 if (err) {
4571 b43_wireless_core_exit(dev);
4572 goto out;
4573 }
4574 }
Michael Buesch3bf0a322008-05-22 16:32:16 +02004575out:
4576 if (err)
4577 wl->current_dev = NULL; /* Failed to init the dev. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004578 mutex_unlock(&wl->mutex);
4579 if (err)
4580 b43err(wl, "Controller restart FAILED\n");
4581 else
4582 b43info(wl, "Controller restarted\n");
4583}
4584
Michael Bueschbb1eeff2008-02-09 12:08:58 +01004585static int b43_setup_bands(struct b43_wldev *dev,
Michael Buesch96c755a2008-01-06 00:09:46 +01004586 bool have_2ghz_phy, bool have_5ghz_phy)
Michael Buesche4d6b792007-09-18 15:39:42 -04004587{
4588 struct ieee80211_hw *hw = dev->wl->hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04004589
Michael Bueschbb1eeff2008-02-09 12:08:58 +01004590 if (have_2ghz_phy)
4591 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &b43_band_2GHz;
4592 if (dev->phy.type == B43_PHYTYPE_N) {
4593 if (have_5ghz_phy)
4594 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_nphy;
4595 } else {
4596 if (have_5ghz_phy)
4597 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
4598 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004599
Michael Bueschbb1eeff2008-02-09 12:08:58 +01004600 dev->phy.supports_2ghz = have_2ghz_phy;
4601 dev->phy.supports_5ghz = have_5ghz_phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04004602
4603 return 0;
4604}
4605
4606static void b43_wireless_core_detach(struct b43_wldev *dev)
4607{
4608 /* We release firmware that late to not be required to re-request
4609 * is all the time when we reinit the core. */
4610 b43_release_firmware(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004611 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004612}
4613
4614static int b43_wireless_core_attach(struct b43_wldev *dev)
4615{
4616 struct b43_wl *wl = dev->wl;
4617 struct ssb_bus *bus = dev->dev->bus;
4618 struct pci_dev *pdev = bus->host_pci;
4619 int err;
Michael Buesch96c755a2008-01-06 00:09:46 +01004620 bool have_2ghz_phy = 0, have_5ghz_phy = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04004621 u32 tmp;
4622
4623 /* Do NOT do any device initialization here.
4624 * Do it in wireless_core_init() instead.
4625 * This function is for gathering basic information about the HW, only.
4626 * Also some structs may be set up here. But most likely you want to have
4627 * that in core_init(), too.
4628 */
4629
4630 err = ssb_bus_powerup(bus, 0);
4631 if (err) {
4632 b43err(wl, "Bus powerup failed\n");
4633 goto out;
4634 }
4635 /* Get the PHY type. */
4636 if (dev->dev->id.revision >= 5) {
4637 u32 tmshigh;
4638
4639 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
Michael Buesch96c755a2008-01-06 00:09:46 +01004640 have_2ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY);
4641 have_5ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_5GHZ_PHY);
Michael Buesche4d6b792007-09-18 15:39:42 -04004642 } else
Michael Buesch96c755a2008-01-06 00:09:46 +01004643 B43_WARN_ON(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004644
Michael Buesch96c755a2008-01-06 00:09:46 +01004645 dev->phy.gmode = have_2ghz_phy;
Larry Fingerfd4973c2009-06-20 12:58:11 -05004646 dev->phy.radio_on = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004647 tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
4648 b43_wireless_core_reset(dev, tmp);
4649
4650 err = b43_phy_versioning(dev);
4651 if (err)
Michael Buesch21954c32007-09-27 15:31:40 +02004652 goto err_powerdown;
Michael Buesche4d6b792007-09-18 15:39:42 -04004653 /* Check if this device supports multiband. */
4654 if (!pdev ||
4655 (pdev->device != 0x4312 &&
4656 pdev->device != 0x4319 && pdev->device != 0x4324)) {
4657 /* No multiband support. */
Michael Buesch96c755a2008-01-06 00:09:46 +01004658 have_2ghz_phy = 0;
4659 have_5ghz_phy = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04004660 switch (dev->phy.type) {
4661 case B43_PHYTYPE_A:
Michael Buesch96c755a2008-01-06 00:09:46 +01004662 have_5ghz_phy = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004663 break;
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004664 case B43_PHYTYPE_LP: //FIXME not always!
Gábor Stefanik86b28922009-08-16 20:22:41 +02004665#if 0 //FIXME enabling 5GHz causes a NULL pointer dereference
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004666 have_5ghz_phy = 1;
Gábor Stefanik86b28922009-08-16 20:22:41 +02004667#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004668 case B43_PHYTYPE_G:
Michael Buesch96c755a2008-01-06 00:09:46 +01004669 case B43_PHYTYPE_N:
4670 have_2ghz_phy = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004671 break;
4672 default:
4673 B43_WARN_ON(1);
4674 }
4675 }
Michael Buesch96c755a2008-01-06 00:09:46 +01004676 if (dev->phy.type == B43_PHYTYPE_A) {
4677 /* FIXME */
4678 b43err(wl, "IEEE 802.11a devices are unsupported\n");
4679 err = -EOPNOTSUPP;
4680 goto err_powerdown;
4681 }
Michael Buesch2e35af12008-04-27 19:06:18 +02004682 if (1 /* disable A-PHY */) {
4683 /* FIXME: For now we disable the A-PHY on multi-PHY devices. */
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004684 if (dev->phy.type != B43_PHYTYPE_N &&
4685 dev->phy.type != B43_PHYTYPE_LP) {
Michael Buesch2e35af12008-04-27 19:06:18 +02004686 have_2ghz_phy = 1;
4687 have_5ghz_phy = 0;
4688 }
4689 }
4690
Michael Bueschfb111372008-09-02 13:00:34 +02004691 err = b43_phy_allocate(dev);
4692 if (err)
4693 goto err_powerdown;
4694
Michael Buesch96c755a2008-01-06 00:09:46 +01004695 dev->phy.gmode = have_2ghz_phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04004696 tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
4697 b43_wireless_core_reset(dev, tmp);
4698
4699 err = b43_validate_chipaccess(dev);
4700 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004701 goto err_phy_free;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01004702 err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
Michael Buesche4d6b792007-09-18 15:39:42 -04004703 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004704 goto err_phy_free;
Michael Buesche4d6b792007-09-18 15:39:42 -04004705
4706 /* Now set some default "current_dev" */
4707 if (!wl->current_dev)
4708 wl->current_dev = dev;
4709 INIT_WORK(&dev->restart_work, b43_chip_reset);
4710
Michael Bueschcb24f572008-09-03 12:12:20 +02004711 dev->phy.ops->switch_analog(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04004712 ssb_device_disable(dev->dev, 0);
4713 ssb_bus_may_powerdown(bus);
4714
4715out:
4716 return err;
4717
Michael Bueschfb111372008-09-02 13:00:34 +02004718err_phy_free:
4719 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004720err_powerdown:
4721 ssb_bus_may_powerdown(bus);
4722 return err;
4723}
4724
4725static void b43_one_core_detach(struct ssb_device *dev)
4726{
4727 struct b43_wldev *wldev;
4728 struct b43_wl *wl;
4729
Michael Buesch3bf0a322008-05-22 16:32:16 +02004730 /* Do not cancel ieee80211-workqueue based work here.
4731 * See comment in b43_remove(). */
4732
Michael Buesche4d6b792007-09-18 15:39:42 -04004733 wldev = ssb_get_drvdata(dev);
4734 wl = wldev->wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04004735 b43_debugfs_remove_device(wldev);
4736 b43_wireless_core_detach(wldev);
4737 list_del(&wldev->list);
4738 wl->nr_devs--;
4739 ssb_set_drvdata(dev, NULL);
4740 kfree(wldev);
4741}
4742
4743static int b43_one_core_attach(struct ssb_device *dev, struct b43_wl *wl)
4744{
4745 struct b43_wldev *wldev;
4746 struct pci_dev *pdev;
4747 int err = -ENOMEM;
4748
4749 if (!list_empty(&wl->devlist)) {
4750 /* We are not the first core on this chip. */
4751 pdev = dev->bus->host_pci;
4752 /* Only special chips support more than one wireless
4753 * core, although some of the other chips have more than
4754 * one wireless core as well. Check for this and
4755 * bail out early.
4756 */
4757 if (!pdev ||
4758 ((pdev->device != 0x4321) &&
4759 (pdev->device != 0x4313) && (pdev->device != 0x431A))) {
4760 b43dbg(wl, "Ignoring unconnected 802.11 core\n");
4761 return -ENODEV;
4762 }
4763 }
4764
4765 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
4766 if (!wldev)
4767 goto out;
4768
4769 wldev->dev = dev;
4770 wldev->wl = wl;
4771 b43_set_status(wldev, B43_STAT_UNINIT);
4772 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
Michael Buesche4d6b792007-09-18 15:39:42 -04004773 INIT_LIST_HEAD(&wldev->list);
4774
4775 err = b43_wireless_core_attach(wldev);
4776 if (err)
4777 goto err_kfree_wldev;
4778
4779 list_add(&wldev->list, &wl->devlist);
4780 wl->nr_devs++;
4781 ssb_set_drvdata(dev, wldev);
4782 b43_debugfs_add_device(wldev);
4783
4784 out:
4785 return err;
4786
4787 err_kfree_wldev:
4788 kfree(wldev);
4789 return err;
4790}
4791
Michael Buesch9fc38452008-04-19 16:53:00 +02004792#define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
4793 (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
4794 (pdev->device == _device) && \
4795 (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
4796 (pdev->subsystem_device == _subdevice) )
4797
Michael Buesche4d6b792007-09-18 15:39:42 -04004798static void b43_sprom_fixup(struct ssb_bus *bus)
4799{
Michael Buesch1855ba72008-04-18 20:51:41 +02004800 struct pci_dev *pdev;
4801
Michael Buesche4d6b792007-09-18 15:39:42 -04004802 /* boardflags workarounds */
4803 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
4804 bus->chip_id == 0x4301 && bus->boardinfo.rev == 0x74)
Larry Finger95de2842007-11-09 16:57:18 -06004805 bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
Michael Buesche4d6b792007-09-18 15:39:42 -04004806 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
4807 bus->boardinfo.type == 0x4E && bus->boardinfo.rev > 0x40)
Larry Finger95de2842007-11-09 16:57:18 -06004808 bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
Michael Buesch1855ba72008-04-18 20:51:41 +02004809 if (bus->bustype == SSB_BUSTYPE_PCI) {
4810 pdev = bus->host_pci;
Michael Buesch9fc38452008-04-19 16:53:00 +02004811 if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
Larry Finger430cd472008-08-14 18:57:11 -05004812 IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
Larry Finger570bdfb2008-09-26 08:23:00 -05004813 IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
Michael Buesch9fc38452008-04-19 16:53:00 +02004814 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
Larry Fingera58d4522008-08-10 10:19:33 -05004815 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
Larry Finger3bb91bf2008-09-19 14:47:38 -05004816 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
4817 IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
Michael Buesch1855ba72008-04-18 20:51:41 +02004818 bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
4819 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004820}
4821
4822static void b43_wireless_exit(struct ssb_device *dev, struct b43_wl *wl)
4823{
4824 struct ieee80211_hw *hw = wl->hw;
4825
4826 ssb_set_devtypedata(dev, NULL);
4827 ieee80211_free_hw(hw);
4828}
4829
4830static int b43_wireless_init(struct ssb_device *dev)
4831{
4832 struct ssb_sprom *sprom = &dev->bus->sprom;
4833 struct ieee80211_hw *hw;
4834 struct b43_wl *wl;
4835 int err = -ENOMEM;
4836
4837 b43_sprom_fixup(dev->bus);
4838
4839 hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
4840 if (!hw) {
4841 b43err(NULL, "Could not allocate ieee80211 device\n");
4842 goto out;
4843 }
Michael Buesch403a3a12009-06-08 21:04:57 +02004844 wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004845
4846 /* fill hw info */
Johannes Berg605a0bd2008-07-15 10:10:01 +02004847 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Bruno Randolf566bfe52008-05-08 19:15:40 +02004848 IEEE80211_HW_SIGNAL_DBM |
4849 IEEE80211_HW_NOISE_DBM;
4850
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07004851 hw->wiphy->interface_modes =
4852 BIT(NL80211_IFTYPE_AP) |
4853 BIT(NL80211_IFTYPE_MESH_POINT) |
4854 BIT(NL80211_IFTYPE_STATION) |
4855 BIT(NL80211_IFTYPE_WDS) |
4856 BIT(NL80211_IFTYPE_ADHOC);
4857
Michael Buesch403a3a12009-06-08 21:04:57 +02004858 hw->queues = modparam_qos ? 4 : 1;
4859 wl->mac80211_initially_registered_queues = hw->queues;
Johannes Berge6a98542008-10-21 12:40:02 +02004860 hw->max_rates = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -04004861 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger95de2842007-11-09 16:57:18 -06004862 if (is_valid_ether_addr(sprom->et1mac))
4863 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04004864 else
Larry Finger95de2842007-11-09 16:57:18 -06004865 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04004866
Michael Buesch403a3a12009-06-08 21:04:57 +02004867 /* Initialize struct b43_wl */
Michael Buesche4d6b792007-09-18 15:39:42 -04004868 wl->hw = hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04004869 spin_lock_init(&wl->leds_lock);
Michael Buesch280d0e12007-12-26 18:26:17 +01004870 spin_lock_init(&wl->shm_lock);
Michael Buesche4d6b792007-09-18 15:39:42 -04004871 mutex_init(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004872 spin_lock_init(&wl->hardirq_lock);
Michael Buesche4d6b792007-09-18 15:39:42 -04004873 INIT_LIST_HEAD(&wl->devlist);
Michael Buescha82d9922008-04-04 21:40:06 +02004874 INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +02004875 INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004876 INIT_WORK(&wl->tx_work, b43_tx_work);
4877 skb_queue_head_init(&wl->tx_queue);
Michael Buesche4d6b792007-09-18 15:39:42 -04004878
4879 ssb_set_devtypedata(dev, wl);
Michael Buesch060210f2009-01-25 15:49:59 +01004880 b43info(wl, "Broadcom %04X WLAN found (core revision %u)\n",
4881 dev->bus->chip_id, dev->id.revision);
Michael Buesche4d6b792007-09-18 15:39:42 -04004882 err = 0;
Michael Buesch060210f2009-01-25 15:49:59 +01004883out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004884 return err;
4885}
4886
4887static int b43_probe(struct ssb_device *dev, const struct ssb_device_id *id)
4888{
4889 struct b43_wl *wl;
4890 int err;
4891 int first = 0;
4892
4893 wl = ssb_get_devtypedata(dev);
4894 if (!wl) {
4895 /* Probing the first core. Must setup common struct b43_wl */
4896 first = 1;
4897 err = b43_wireless_init(dev);
4898 if (err)
4899 goto out;
4900 wl = ssb_get_devtypedata(dev);
4901 B43_WARN_ON(!wl);
4902 }
4903 err = b43_one_core_attach(dev, wl);
4904 if (err)
4905 goto err_wireless_exit;
4906
4907 if (first) {
4908 err = ieee80211_register_hw(wl->hw);
4909 if (err)
4910 goto err_one_core_detach;
4911 }
4912
4913 out:
4914 return err;
4915
4916 err_one_core_detach:
4917 b43_one_core_detach(dev);
4918 err_wireless_exit:
4919 if (first)
4920 b43_wireless_exit(dev, wl);
4921 return err;
4922}
4923
4924static void b43_remove(struct ssb_device *dev)
4925{
4926 struct b43_wl *wl = ssb_get_devtypedata(dev);
4927 struct b43_wldev *wldev = ssb_get_drvdata(dev);
4928
Michael Buesch3bf0a322008-05-22 16:32:16 +02004929 /* We must cancel any work here before unregistering from ieee80211,
4930 * as the ieee80211 unreg will destroy the workqueue. */
4931 cancel_work_sync(&wldev->restart_work);
4932
Michael Buesche4d6b792007-09-18 15:39:42 -04004933 B43_WARN_ON(!wl);
Michael Buesch403a3a12009-06-08 21:04:57 +02004934 if (wl->current_dev == wldev) {
4935 /* Restore the queues count before unregistering, because firmware detect
4936 * might have modified it. Restoring is important, so the networking
4937 * stack can properly free resources. */
4938 wl->hw->queues = wl->mac80211_initially_registered_queues;
Michael Buesche4d6b792007-09-18 15:39:42 -04004939 ieee80211_unregister_hw(wl->hw);
Michael Buesch403a3a12009-06-08 21:04:57 +02004940 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004941
4942 b43_one_core_detach(dev);
4943
4944 if (list_empty(&wl->devlist)) {
4945 /* Last core on the chip unregistered.
4946 * We can destroy common struct b43_wl.
4947 */
4948 b43_wireless_exit(dev, wl);
4949 }
4950}
4951
4952/* Perform a hardware reset. This can be called from any context. */
4953void b43_controller_restart(struct b43_wldev *dev, const char *reason)
4954{
4955 /* Must avoid requeueing, if we are in shutdown. */
4956 if (b43_status(dev) < B43_STAT_INITIALIZED)
4957 return;
4958 b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04004959 ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04004960}
4961
4962#ifdef CONFIG_PM
4963
4964static int b43_suspend(struct ssb_device *dev, pm_message_t state)
4965{
4966 struct b43_wldev *wldev = ssb_get_drvdata(dev);
4967 struct b43_wl *wl = wldev->wl;
4968
4969 b43dbg(wl, "Suspending...\n");
4970
4971 mutex_lock(&wl->mutex);
Rafael J. Wysocki3506e0c2008-02-04 22:30:15 -08004972 wldev->suspend_in_progress = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04004973 wldev->suspend_init_status = b43_status(wldev);
4974 if (wldev->suspend_init_status >= B43_STAT_STARTED)
Michael Buesch36dbd952009-09-04 22:51:29 +02004975 wldev = b43_wireless_core_stop(wldev);
4976 if (wldev && wldev->suspend_init_status >= B43_STAT_INITIALIZED)
Michael Buesche4d6b792007-09-18 15:39:42 -04004977 b43_wireless_core_exit(wldev);
4978 mutex_unlock(&wl->mutex);
4979
4980 b43dbg(wl, "Device suspended.\n");
4981
4982 return 0;
4983}
4984
4985static int b43_resume(struct ssb_device *dev)
4986{
4987 struct b43_wldev *wldev = ssb_get_drvdata(dev);
4988 struct b43_wl *wl = wldev->wl;
4989 int err = 0;
4990
4991 b43dbg(wl, "Resuming...\n");
4992
4993 mutex_lock(&wl->mutex);
4994 if (wldev->suspend_init_status >= B43_STAT_INITIALIZED) {
4995 err = b43_wireless_core_init(wldev);
4996 if (err) {
4997 b43err(wl, "Resume failed at core init\n");
4998 goto out;
4999 }
5000 }
5001 if (wldev->suspend_init_status >= B43_STAT_STARTED) {
5002 err = b43_wireless_core_start(wldev);
5003 if (err) {
Rafael J. Wysocki3506e0c2008-02-04 22:30:15 -08005004 b43_leds_exit(wldev);
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01005005 b43_rng_exit(wldev->wl);
Michael Buesche4d6b792007-09-18 15:39:42 -04005006 b43_wireless_core_exit(wldev);
5007 b43err(wl, "Resume failed at core start\n");
5008 goto out;
5009 }
5010 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005011 b43dbg(wl, "Device resumed.\n");
Rafael J. Wysocki3506e0c2008-02-04 22:30:15 -08005012 out:
5013 wldev->suspend_in_progress = false;
5014 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04005015 return err;
5016}
5017
5018#else /* CONFIG_PM */
5019# define b43_suspend NULL
5020# define b43_resume NULL
5021#endif /* CONFIG_PM */
5022
5023static struct ssb_driver b43_ssb_driver = {
5024 .name = KBUILD_MODNAME,
5025 .id_table = b43_ssb_tbl,
5026 .probe = b43_probe,
5027 .remove = b43_remove,
5028 .suspend = b43_suspend,
5029 .resume = b43_resume,
5030};
5031
Michael Buesch26bc7832008-02-09 00:18:35 +01005032static void b43_print_driverinfo(void)
5033{
5034 const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
Johannes Bergf41f3f32009-06-07 12:30:34 -05005035 *feat_leds = "";
Michael Buesch26bc7832008-02-09 00:18:35 +01005036
5037#ifdef CONFIG_B43_PCI_AUTOSELECT
5038 feat_pci = "P";
5039#endif
5040#ifdef CONFIG_B43_PCMCIA
5041 feat_pcmcia = "M";
5042#endif
5043#ifdef CONFIG_B43_NPHY
5044 feat_nphy = "N";
5045#endif
5046#ifdef CONFIG_B43_LEDS
5047 feat_leds = "L";
5048#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005049 printk(KERN_INFO "Broadcom 43xx driver loaded "
Johannes Bergf41f3f32009-06-07 12:30:34 -05005050 "[ Features: %s%s%s%s, Firmware-ID: "
Michael Buesch26bc7832008-02-09 00:18:35 +01005051 B43_SUPPORTED_FIRMWARE_ID " ]\n",
5052 feat_pci, feat_pcmcia, feat_nphy,
Johannes Bergf41f3f32009-06-07 12:30:34 -05005053 feat_leds);
Michael Buesch26bc7832008-02-09 00:18:35 +01005054}
5055
Michael Buesche4d6b792007-09-18 15:39:42 -04005056static int __init b43_init(void)
5057{
5058 int err;
5059
5060 b43_debugfs_init();
5061 err = b43_pcmcia_init();
5062 if (err)
5063 goto err_dfs_exit;
5064 err = ssb_driver_register(&b43_ssb_driver);
5065 if (err)
5066 goto err_pcmcia_exit;
Michael Buesch26bc7832008-02-09 00:18:35 +01005067 b43_print_driverinfo();
Michael Buesche4d6b792007-09-18 15:39:42 -04005068
5069 return err;
5070
5071err_pcmcia_exit:
5072 b43_pcmcia_exit();
5073err_dfs_exit:
5074 b43_debugfs_exit();
5075 return err;
5076}
5077
5078static void __exit b43_exit(void)
5079{
5080 ssb_driver_unregister(&b43_ssb_driver);
5081 b43_pcmcia_exit();
5082 b43_debugfs_exit();
5083}
5084
5085module_init(b43_init)
5086module_exit(b43_exit)