1 /* $OpenBSD: rtw.c,v 1.103 2022/04/21 21:03:02 stsp Exp $ */
2 /* $NetBSD: rtw.c,v 1.29 2004/12/27 19:49:16 dyoung Exp $ */
3
4 /*-
5 * Copyright (c) 2004, 2005 David Young. All rights reserved.
6 *
7 * Programmed for NetBSD by David Young.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of David Young may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
25 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 */
34 /*
35 * Device driver for the Realtek RTL8180 802.11 MAC/BBP.
36 */
37
38 #include "bpfilter.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 #include <sys/endian.h>
49
50 #include <machine/bus.h>
51 #include <machine/intr.h> /* splnet */
52
53 #include <net/if.h>
54 #include <net/if_media.h>
55
56 #if NBPFILTER > 0
57 #include <net/bpf.h>
58 #endif
59
60 #include <netinet/in.h>
61 #include <netinet/if_ether.h>
62
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_radiotap.h>
65
66 #include <dev/ic/rtwreg.h>
67 #include <dev/ic/rtwvar.h>
68 #include <dev/ic/max2820reg.h>
69 #include <dev/ic/sa2400reg.h>
70 #include <dev/ic/si4136reg.h>
71 #include <dev/ic/rtl8225reg.h>
72 #include <dev/ic/smc93cx6var.h>
73
74 int rtw_rfprog_fallback = 0;
75 int rtw_do_chip_reset = 0;
76 int rtw_dwelltime = 200; /* milliseconds per channel */
77 int rtw_macbangbits_timeout = 100;
78
79 #ifdef RTW_DEBUG
80 int rtw_debug = 0;
81 int rtw_rxbufs_limit = RTW_RXQLEN;
82 #endif /* RTW_DEBUG */
83
84 void rtw_start(struct ifnet *);
85 void rtw_txdesc_blk_init_all(struct rtw_txdesc_blk *);
86 void rtw_txsoft_blk_init_all(struct rtw_txsoft_blk *);
87 void rtw_txdesc_blk_init(struct rtw_txdesc_blk *);
88 void rtw_txdescs_sync(struct rtw_txdesc_blk *, u_int, u_int, int);
89 void rtw_txring_fixup(struct rtw_softc *);
90 void rtw_rxbufs_release(bus_dma_tag_t, struct rtw_rxsoft *);
91 void rtw_rxdesc_init(struct rtw_rxdesc_blk *, struct rtw_rxsoft *, int, int);
92 void rtw_rxring_fixup(struct rtw_softc *);
93 void rtw_io_enable(struct rtw_regs *, u_int8_t, int);
94 void rtw_intr_rx(struct rtw_softc *, u_int16_t);
95 #ifndef IEEE80211_STA_ONLY
96 void rtw_intr_beacon(struct rtw_softc *, u_int16_t);
97 void rtw_intr_atim(struct rtw_softc *);
98 #endif
99 void rtw_transmit_config(struct rtw_softc *);
100 void rtw_pktfilt_load(struct rtw_softc *);
101 void rtw_start(struct ifnet *);
102 void rtw_watchdog(struct ifnet *);
103 void rtw_next_scan(void *);
104 #ifndef IEEE80211_STA_ONLY
105 void rtw_recv_mgmt(struct ieee80211com *, struct mbuf *,
106 struct ieee80211_node *, struct ieee80211_rxinfo *, int);
107 #endif
108 struct ieee80211_node *rtw_node_alloc(struct ieee80211com *);
109 void rtw_node_free(struct ieee80211com *, struct ieee80211_node *);
110 void rtw_media_status(struct ifnet *, struct ifmediareq *);
111 void rtw_txsoft_blk_cleanup_all(struct rtw_softc *);
112 void rtw_txdesc_blk_setup(struct rtw_txdesc_blk *, struct rtw_txdesc *,
113 u_int, bus_addr_t, bus_addr_t);
114 void rtw_txdesc_blk_setup_all(struct rtw_softc *);
115 void rtw_intr_tx(struct rtw_softc *, u_int16_t);
116 void rtw_intr_ioerror(struct rtw_softc *, u_int16_t);
117 void rtw_intr_timeout(struct rtw_softc *);
118 void rtw_stop(struct ifnet *, int);
119 void rtw_maxim_pwrstate(struct rtw_regs *, enum rtw_pwrstate, int, int);
120 void rtw_philips_pwrstate(struct rtw_regs *, enum rtw_pwrstate, int, int);
121 void rtw_rtl_pwrstate(struct rtw_regs *, enum rtw_pwrstate, int, int);
122 void rtw_pwrstate0(struct rtw_softc *, enum rtw_pwrstate, int, int);
123 void rtw_join_bss(struct rtw_softc *, u_int8_t *, u_int16_t);
124 void rtw_set_access1(struct rtw_regs *, enum rtw_access);
125 int rtw_srom_parse(struct rtw_softc *);
126 int rtw_srom_read(struct rtw_regs *, u_int32_t, struct rtw_srom *,
127 const char *);
128 void rtw_set_rfprog(struct rtw_regs *, int, const char *);
129 u_int8_t rtw_chan2txpower(struct rtw_srom *, struct ieee80211com *,
130 struct ieee80211_channel *);
131 int rtw_txsoft_blk_init(struct rtw_txsoft_blk *);
132 int rtw_rxsoft_init_all(bus_dma_tag_t, struct rtw_rxsoft *,
133 int *, const char *);
134 void rtw_txsoft_release(bus_dma_tag_t, struct ieee80211com *,
135 struct rtw_txsoft *);
136 void rtw_txsofts_release(bus_dma_tag_t, struct ieee80211com *,
137 struct rtw_txsoft_blk *);
138 void rtw_hwring_setup(struct rtw_softc *);
139 int rtw_swring_setup(struct rtw_softc *);
140 void rtw_txdescs_reset(struct rtw_softc *);
141 void rtw_rfmd_pwrstate(struct rtw_regs *, enum rtw_pwrstate, int, int);
142 int rtw_pwrstate(struct rtw_softc *, enum rtw_pwrstate);
143 int rtw_tune(struct rtw_softc *);
144 void rtw_set_nettype(struct rtw_softc *, enum ieee80211_opmode);
145 int rtw_compute_duration1(int, int, uint32_t, int, struct rtw_duration *);
146 int rtw_compute_duration(struct ieee80211_frame *, int, uint32_t, int,
147 int, struct rtw_duration *, struct rtw_duration *, int *, int);
148 int rtw_init(struct ifnet *);
149 int rtw_ioctl(struct ifnet *, u_long, caddr_t);
150 int rtw_seg_too_short(bus_dmamap_t);
151 struct mbuf *rtw_dmamap_load_txbuf(bus_dma_tag_t, bus_dmamap_t, struct mbuf *,
152 u_int, short *, const char *);
153 int rtw_newstate(struct ieee80211com *, enum ieee80211_state, int);
154 int rtw_media_change(struct ifnet *);
155 int rtw_txsoft_blk_setup_all(struct rtw_softc *);
156 int rtw_rf_attach(struct rtw_softc *, int);
157 u_int8_t rtw_check_phydelay(struct rtw_regs *, u_int32_t);
158 int rtw_chip_reset1(struct rtw_regs *, const char *);
159 int rtw_chip_reset(struct rtw_regs *, const char *);
160 int rtw_recall_eeprom(struct rtw_regs *, const char *);
161 int rtw_reset(struct rtw_softc *);
162 void rtw_reset_oactive(struct rtw_softc *);
163 int rtw_txdesc_dmamaps_create(bus_dma_tag_t, struct rtw_txsoft *, u_int);
164 int rtw_rxdesc_dmamaps_create(bus_dma_tag_t, struct rtw_rxsoft *, u_int);
165 void rtw_rxdesc_dmamaps_destroy(bus_dma_tag_t, struct rtw_rxsoft *, u_int);
166 void rtw_txdesc_dmamaps_destroy(bus_dma_tag_t, struct rtw_txsoft *, u_int);
167 void rtw_identify_country(struct rtw_regs *, enum rtw_locale *);
168 int rtw_identify_sta(struct rtw_regs *, u_int8_t (*)[], const char *);
169 void rtw_rxdescs_sync(struct rtw_rxdesc_blk *, int, int, int);
170 int rtw_rxsoft_alloc(bus_dma_tag_t, struct rtw_rxsoft *);
171 void rtw_collect_txpkt(struct rtw_softc *, struct rtw_txdesc_blk *,
172 struct rtw_txsoft *, int);
173 void rtw_collect_txring(struct rtw_softc *, struct rtw_txsoft_blk *,
174 struct rtw_txdesc_blk *, int);
175 void rtw_suspend_ticks(struct rtw_softc *);
176 void rtw_resume_ticks(struct rtw_softc *);
177 void rtw_enable_interrupts(struct rtw_softc *);
178 int rtw_dequeue(struct ifnet *, struct rtw_txsoft_blk **,
179 struct rtw_txdesc_blk **, struct mbuf **,
180 struct ieee80211_node **);
181 int rtw_txsoft_blk_setup(struct rtw_txsoft_blk *, u_int);
182 void rtw_rxdesc_init_all(struct rtw_rxdesc_blk *, struct rtw_rxsoft *,
183 int);
184 int rtw_txring_choose(struct rtw_softc *, struct rtw_txsoft_blk **,
185 struct rtw_txdesc_blk **, int);
186 u_int rtw_txring_next(struct rtw_regs *, struct rtw_txdesc_blk *);
187 struct mbuf *rtw_80211_dequeue(struct rtw_softc *, struct mbuf_queue *, int,
188 struct rtw_txsoft_blk **, struct rtw_txdesc_blk **,
189 struct ieee80211_node **);
190 uint64_t rtw_tsf_extend(struct rtw_regs *, u_int32_t);
191 #ifndef IEEE80211_STA_ONLY
192 void rtw_ibss_merge(struct rtw_softc *, struct ieee80211_node *,
193 u_int32_t);
194 #endif
195 void rtw_idle(struct rtw_regs *);
196 void rtw_led_attach(struct rtw_led_state *, void *);
197 void rtw_led_init(struct rtw_regs *);
198 void rtw_led_slowblink(void *);
199 void rtw_led_fastblink(void *);
200 void rtw_led_set(struct rtw_led_state *, struct rtw_regs *, u_int);
201 void rtw_led_newstate(struct rtw_softc *, enum ieee80211_state);
202
203 int rtw_phy_init(struct rtw_softc *);
204 int rtw_bbp_preinit(struct rtw_regs *, u_int, int, u_int);
205 int rtw_bbp_init(struct rtw_regs *, struct rtw_bbpset *, int,
206 int, u_int8_t, u_int);
207 void rtw_verify_syna(u_int, u_int32_t);
208 int rtw_sa2400_pwrstate(struct rtw_softc *, enum rtw_pwrstate);
209 int rtw_sa2400_txpower(struct rtw_softc *, u_int8_t);
210 int rtw_sa2400_tune(struct rtw_softc *, u_int);
211 int rtw_sa2400_vcocal_start(struct rtw_softc *, int);
212 int rtw_sa2400_vco_calibration(struct rtw_softc *);
213 int rtw_sa2400_filter_calibration(struct rtw_softc *);
214 int rtw_sa2400_dc_calibration(struct rtw_softc *);
215 int rtw_sa2400_calibrate(struct rtw_softc *, u_int);
216 int rtw_sa2400_init(struct rtw_softc *, u_int, u_int8_t,
217 enum rtw_pwrstate);
218 int rtw_max2820_pwrstate(struct rtw_softc *, enum rtw_pwrstate);
219 int rtw_max2820_init(struct rtw_softc *, u_int, u_int8_t,
220 enum rtw_pwrstate);
221 int rtw_max2820_txpower(struct rtw_softc *, u_int8_t);
222 int rtw_max2820_tune(struct rtw_softc *, u_int);
223 int rtw_rtl8225_pwrstate(struct rtw_softc *, enum rtw_pwrstate);
224 int rtw_rtl8225_init(struct rtw_softc *, u_int, u_int8_t,
225 enum rtw_pwrstate);
226 int rtw_rtl8225_txpower(struct rtw_softc *, u_int8_t);
227 int rtw_rtl8225_tune(struct rtw_softc *, u_int);
228 int rtw_rtl8255_pwrstate(struct rtw_softc *, enum rtw_pwrstate);
229 int rtw_rtl8255_init(struct rtw_softc *, u_int, u_int8_t,
230 enum rtw_pwrstate);
231 int rtw_rtl8255_txpower(struct rtw_softc *, u_int8_t);
232 int rtw_rtl8255_tune(struct rtw_softc *, u_int);
233 int rtw_grf5101_pwrstate(struct rtw_softc *, enum rtw_pwrstate);
234 int rtw_grf5101_init(struct rtw_softc *, u_int, u_int8_t,
235 enum rtw_pwrstate);
236 int rtw_grf5101_txpower(struct rtw_softc *, u_int8_t);
237 int rtw_grf5101_tune(struct rtw_softc *, u_int);
238 int rtw_rf_hostwrite(struct rtw_softc *, u_int, u_int32_t);
239 int rtw_rf_macwrite(struct rtw_softc *, u_int, u_int32_t);
240 int rtw_bbp_write(struct rtw_regs *, u_int, u_int);
241 u_int32_t rtw_grf5101_host_crypt(u_int, u_int32_t);
242 u_int32_t rtw_maxim_swizzle(u_int, uint32_t);
243 u_int32_t rtw_grf5101_mac_crypt(u_int, u_int32_t);
244 void rtw_rf_hostbangbits(struct rtw_regs *, u_int32_t, int, u_int);
245 void rtw_rf_rtl8225_hostbangbits(struct rtw_regs *, u_int32_t, int, u_int);
246 int rtw_rf_macbangbits(struct rtw_regs *, u_int32_t);
247
248 u_int8_t rtw_read8(void *, u_int32_t);
249 u_int16_t rtw_read16(void *, u_int32_t);
250 u_int32_t rtw_read32(void *, u_int32_t);
251 void rtw_write8(void *, u_int32_t, u_int8_t);
252 void rtw_write16(void *, u_int32_t, u_int16_t);
253 void rtw_write32(void *, u_int32_t, u_int32_t);
254 void rtw_barrier(void *, u_int32_t, u_int32_t, int);
255
256 #ifdef RTW_DEBUG
257 void rtw_print_txdesc(struct rtw_softc *, const char *,
258 struct rtw_txsoft *, struct rtw_txdesc_blk *, int);
259 const char *rtw_access_string(enum rtw_access);
260 void rtw_dump_rings(struct rtw_softc *);
261 void rtw_print_txdesc(struct rtw_softc *, const char *,
262 struct rtw_txsoft *, struct rtw_txdesc_blk *, int);
263 #endif
264
265 struct cfdriver rtw_cd = {
266 NULL, "rtw", DV_IFNET
267 };
268
269 void
rtw_continuous_tx_enable(struct rtw_softc * sc,int enable)270 rtw_continuous_tx_enable(struct rtw_softc *sc, int enable)
271 {
272 struct rtw_regs *regs = &sc->sc_regs;
273
274 u_int32_t tcr;
275 tcr = RTW_READ(regs, RTW_TCR);
276 tcr &= ~RTW_TCR_LBK_MASK;
277 if (enable)
278 tcr |= RTW_TCR_LBK_CONT;
279 else
280 tcr |= RTW_TCR_LBK_NORMAL;
281 RTW_WRITE(regs, RTW_TCR, tcr);
282 RTW_SYNC(regs, RTW_TCR, RTW_TCR);
283 rtw_set_access(regs, RTW_ACCESS_ANAPARM);
284 rtw_txdac_enable(sc, !enable);
285 rtw_set_access(regs, RTW_ACCESS_ANAPARM);/* XXX Voodoo from Linux. */
286 rtw_set_access(regs, RTW_ACCESS_NONE);
287 }
288
289 #ifdef RTW_DEBUG
290 const char *
rtw_access_string(enum rtw_access access)291 rtw_access_string(enum rtw_access access)
292 {
293 switch (access) {
294 case RTW_ACCESS_NONE:
295 return "none";
296 case RTW_ACCESS_CONFIG:
297 return "config";
298 case RTW_ACCESS_ANAPARM:
299 return "anaparm";
300 default:
301 return "unknown";
302 }
303 }
304 #endif
305
306 void
rtw_set_access1(struct rtw_regs * regs,enum rtw_access naccess)307 rtw_set_access1(struct rtw_regs *regs, enum rtw_access naccess)
308 {
309 KASSERT(naccess >= RTW_ACCESS_NONE && naccess <= RTW_ACCESS_ANAPARM);
310 KASSERT(regs->r_access >= RTW_ACCESS_NONE &&
311 regs->r_access <= RTW_ACCESS_ANAPARM);
312
313 if (naccess == regs->r_access)
314 return;
315
316 switch (naccess) {
317 case RTW_ACCESS_NONE:
318 switch (regs->r_access) {
319 case RTW_ACCESS_ANAPARM:
320 rtw_anaparm_enable(regs, 0);
321 /*FALLTHROUGH*/
322 case RTW_ACCESS_CONFIG:
323 rtw_config0123_enable(regs, 0);
324 /*FALLTHROUGH*/
325 case RTW_ACCESS_NONE:
326 break;
327 }
328 break;
329 case RTW_ACCESS_CONFIG:
330 switch (regs->r_access) {
331 case RTW_ACCESS_NONE:
332 rtw_config0123_enable(regs, 1);
333 /*FALLTHROUGH*/
334 case RTW_ACCESS_CONFIG:
335 break;
336 case RTW_ACCESS_ANAPARM:
337 rtw_anaparm_enable(regs, 0);
338 break;
339 }
340 break;
341 case RTW_ACCESS_ANAPARM:
342 switch (regs->r_access) {
343 case RTW_ACCESS_NONE:
344 rtw_config0123_enable(regs, 1);
345 /*FALLTHROUGH*/
346 case RTW_ACCESS_CONFIG:
347 rtw_anaparm_enable(regs, 1);
348 /*FALLTHROUGH*/
349 case RTW_ACCESS_ANAPARM:
350 break;
351 }
352 break;
353 }
354 }
355
356 void
rtw_set_access(struct rtw_regs * regs,enum rtw_access access)357 rtw_set_access(struct rtw_regs *regs, enum rtw_access access)
358 {
359 rtw_set_access1(regs, access);
360 RTW_DPRINTF(RTW_DEBUG_ACCESS,
361 ("%s: access %s -> %s\n",__func__,
362 rtw_access_string(regs->r_access),
363 rtw_access_string(access)));
364 regs->r_access = access;
365 }
366
367 /*
368 * Enable registers, switch register banks.
369 */
370 void
rtw_config0123_enable(struct rtw_regs * regs,int enable)371 rtw_config0123_enable(struct rtw_regs *regs, int enable)
372 {
373 u_int8_t ecr;
374 ecr = RTW_READ8(regs, RTW_9346CR);
375 ecr &= ~(RTW_9346CR_EEM_MASK | RTW_9346CR_EECS | RTW_9346CR_EESK);
376 if (enable)
377 ecr |= RTW_9346CR_EEM_CONFIG;
378 else {
379 RTW_WBW(regs, RTW_9346CR, MAX(RTW_CONFIG0, RTW_CONFIG3));
380 ecr |= RTW_9346CR_EEM_NORMAL;
381 }
382 RTW_WRITE8(regs, RTW_9346CR, ecr);
383 RTW_SYNC(regs, RTW_9346CR, RTW_9346CR);
384 }
385
386 /* requires rtw_config0123_enable(, 1) */
387 void
rtw_anaparm_enable(struct rtw_regs * regs,int enable)388 rtw_anaparm_enable(struct rtw_regs *regs, int enable)
389 {
390 u_int8_t cfg3;
391
392 cfg3 = RTW_READ8(regs, RTW_CONFIG3);
393 cfg3 |= RTW_CONFIG3_CLKRUNEN;
394 if (enable)
395 cfg3 |= RTW_CONFIG3_PARMEN;
396 else
397 cfg3 &= ~RTW_CONFIG3_PARMEN;
398 RTW_WRITE8(regs, RTW_CONFIG3, cfg3);
399 RTW_SYNC(regs, RTW_CONFIG3, RTW_CONFIG3);
400 }
401
402 /* requires rtw_anaparm_enable(, 1) */
403 void
rtw_txdac_enable(struct rtw_softc * sc,int enable)404 rtw_txdac_enable(struct rtw_softc *sc, int enable)
405 {
406 u_int32_t anaparm;
407 struct rtw_regs *regs = &sc->sc_regs;
408
409 anaparm = RTW_READ(regs, RTW_ANAPARM_0);
410 if (enable)
411 anaparm &= ~RTW_ANAPARM_TXDACOFF;
412 else
413 anaparm |= RTW_ANAPARM_TXDACOFF;
414 RTW_WRITE(regs, RTW_ANAPARM_0, anaparm);
415 RTW_SYNC(regs, RTW_ANAPARM_0, RTW_ANAPARM_0);
416 }
417
418 int
rtw_chip_reset1(struct rtw_regs * regs,const char * dvname)419 rtw_chip_reset1(struct rtw_regs *regs, const char *dvname)
420 {
421 u_int8_t cr;
422 int i;
423
424 RTW_WRITE8(regs, RTW_CR, RTW_CR_RST);
425
426 RTW_WBR(regs, RTW_CR, RTW_CR);
427
428 for (i = 0; i < 1000; i++) {
429 if ((cr = RTW_READ8(regs, RTW_CR) & RTW_CR_RST) == 0) {
430 RTW_DPRINTF(RTW_DEBUG_RESET,
431 ("%s: reset in %dus\n", dvname, i));
432 return 0;
433 }
434 RTW_RBR(regs, RTW_CR, RTW_CR);
435 DELAY(10); /* 10us */
436 }
437
438 printf("\n%s: reset failed\n", dvname);
439 return ETIMEDOUT;
440 }
441
442 int
rtw_chip_reset(struct rtw_regs * regs,const char * dvname)443 rtw_chip_reset(struct rtw_regs *regs, const char *dvname)
444 {
445 uint32_t tcr;
446
447 /* from Linux driver */
448 tcr = RTW_TCR_CWMIN | RTW_TCR_MXDMA_2048 |
449 LSHIFT(7, RTW_TCR_SRL_MASK) | LSHIFT(7, RTW_TCR_LRL_MASK);
450
451 RTW_WRITE(regs, RTW_TCR, tcr);
452
453 RTW_WBW(regs, RTW_CR, RTW_TCR);
454
455 return rtw_chip_reset1(regs, dvname);
456 }
457
458 int
rtw_recall_eeprom(struct rtw_regs * regs,const char * dvname)459 rtw_recall_eeprom(struct rtw_regs *regs, const char *dvname)
460 {
461 int i;
462 u_int8_t ecr;
463
464 ecr = RTW_READ8(regs, RTW_9346CR);
465 ecr = (ecr & ~RTW_9346CR_EEM_MASK) | RTW_9346CR_EEM_AUTOLOAD;
466 RTW_WRITE8(regs, RTW_9346CR, ecr);
467
468 RTW_WBR(regs, RTW_9346CR, RTW_9346CR);
469
470 /* wait 10ms for completion */
471 for (i = 0; i < 50; i++) {
472 ecr = RTW_READ8(regs, RTW_9346CR);
473 if ((ecr & RTW_9346CR_EEM_MASK) == RTW_9346CR_EEM_NORMAL) {
474 RTW_DPRINTF(RTW_DEBUG_RESET,
475 ("%s: recall EEPROM in %dus\n", dvname, i * 200));
476 return (0);
477 }
478 RTW_RBR(regs, RTW_9346CR, RTW_9346CR);
479 DELAY(200);
480 }
481
482 printf("\n%s: could not recall EEPROM in %dus\n", dvname, i * 200);
483
484 return (ETIMEDOUT);
485 }
486
487 int
rtw_reset(struct rtw_softc * sc)488 rtw_reset(struct rtw_softc *sc)
489 {
490 int rc;
491 uint8_t config1;
492
493 if ((rc = rtw_chip_reset(&sc->sc_regs, sc->sc_dev.dv_xname)) != 0)
494 return rc;
495
496 if ((rc = rtw_recall_eeprom(&sc->sc_regs, sc->sc_dev.dv_xname)) != 0)
497 ;
498
499 config1 = RTW_READ8(&sc->sc_regs, RTW_CONFIG1);
500 RTW_WRITE8(&sc->sc_regs, RTW_CONFIG1, config1 & ~RTW_CONFIG1_PMEN);
501 /* TBD turn off maximum power saving? */
502
503 return 0;
504 }
505
506 int
rtw_txdesc_dmamaps_create(bus_dma_tag_t dmat,struct rtw_txsoft * descs,u_int ndescs)507 rtw_txdesc_dmamaps_create(bus_dma_tag_t dmat, struct rtw_txsoft *descs,
508 u_int ndescs)
509 {
510 int i, rc = 0;
511 for (i = 0; i < ndescs; i++) {
512 rc = bus_dmamap_create(dmat, MCLBYTES, RTW_MAXPKTSEGS, MCLBYTES,
513 0, 0, &descs[i].ts_dmamap);
514 if (rc != 0)
515 break;
516 }
517 return rc;
518 }
519
520 int
rtw_rxdesc_dmamaps_create(bus_dma_tag_t dmat,struct rtw_rxsoft * descs,u_int ndescs)521 rtw_rxdesc_dmamaps_create(bus_dma_tag_t dmat, struct rtw_rxsoft *descs,
522 u_int ndescs)
523 {
524 int i, rc = 0;
525 for (i = 0; i < ndescs; i++) {
526 rc = bus_dmamap_create(dmat, MCLBYTES, 1, MCLBYTES, 0, 0,
527 &descs[i].rs_dmamap);
528 if (rc != 0)
529 break;
530 }
531 return rc;
532 }
533
534 void
rtw_rxdesc_dmamaps_destroy(bus_dma_tag_t dmat,struct rtw_rxsoft * descs,u_int ndescs)535 rtw_rxdesc_dmamaps_destroy(bus_dma_tag_t dmat, struct rtw_rxsoft *descs,
536 u_int ndescs)
537 {
538 int i;
539 for (i = 0; i < ndescs; i++) {
540 if (descs[i].rs_dmamap != NULL)
541 bus_dmamap_destroy(dmat, descs[i].rs_dmamap);
542 }
543 }
544
545 void
rtw_txdesc_dmamaps_destroy(bus_dma_tag_t dmat,struct rtw_txsoft * descs,u_int ndescs)546 rtw_txdesc_dmamaps_destroy(bus_dma_tag_t dmat, struct rtw_txsoft *descs,
547 u_int ndescs)
548 {
549 int i;
550 for (i = 0; i < ndescs; i++) {
551 if (descs[i].ts_dmamap != NULL)
552 bus_dmamap_destroy(dmat, descs[i].ts_dmamap);
553 }
554 }
555
556 int
rtw_srom_parse(struct rtw_softc * sc)557 rtw_srom_parse(struct rtw_softc *sc)
558 {
559 int i;
560 struct rtw_srom *sr = &sc->sc_srom;
561 u_int32_t *flags = &sc->sc_flags;
562 u_int8_t *cs_threshold = &sc->sc_csthr;
563 int *rfchipid = &sc->sc_rfchipid;
564 u_int32_t *rcr = &sc->sc_rcr;
565 enum rtw_locale *locale = &sc->sc_locale;
566 u_int16_t version;
567 u_int8_t mac[IEEE80211_ADDR_LEN];
568
569 *flags &= ~(RTW_F_DIGPHY|RTW_F_DFLANTB|RTW_F_ANTDIV);
570 *rcr &= ~(RTW_RCR_ENCS1 | RTW_RCR_ENCS2);
571
572 version = RTW_SR_GET16(sr, RTW_SR_VERSION);
573 RTW_DPRINTF(RTW_DEBUG_ATTACH,
574 ("%s: SROM %d.%d\n", sc->sc_dev.dv_xname, version >> 8,
575 version & 0xff));
576
577 if (version <= 0x0101) {
578 printf(" is not understood, limping along with defaults ");
579 *flags |= (RTW_F_DIGPHY|RTW_F_ANTDIV);
580 *cs_threshold = RTW_SR_ENERGYDETTHR_DEFAULT;
581 *rcr |= RTW_RCR_ENCS1;
582 *rfchipid = RTW_RFCHIPID_PHILIPS;
583 return 0;
584 }
585
586 for (i = 0; i < IEEE80211_ADDR_LEN; i++)
587 mac[i] = RTW_SR_GET(sr, RTW_SR_MAC + i);
588
589 RTW_DPRINTF(RTW_DEBUG_ATTACH,
590 ("%s: EEPROM MAC %s\n", sc->sc_dev.dv_xname, ether_sprintf(mac)));
591
592 *cs_threshold = RTW_SR_GET(sr, RTW_SR_ENERGYDETTHR);
593
594 if ((RTW_SR_GET(sr, RTW_SR_CONFIG2) & RTW8180_CONFIG2_ANT) != 0)
595 *flags |= RTW_F_ANTDIV;
596
597 /* Note well: the sense of the RTW_SR_RFPARM_DIGPHY bit seems
598 * to be reversed.
599 */
600 if ((RTW_SR_GET(sr, RTW_SR_RFPARM) & RTW_SR_RFPARM_DIGPHY) == 0)
601 *flags |= RTW_F_DIGPHY;
602 if ((RTW_SR_GET(sr, RTW_SR_RFPARM) & RTW_SR_RFPARM_DFLANTB) != 0)
603 *flags |= RTW_F_DFLANTB;
604
605 *rcr |= LSHIFT(MASK_AND_RSHIFT(RTW_SR_GET(sr, RTW_SR_RFPARM),
606 RTW_SR_RFPARM_CS_MASK), RTW_RCR_ENCS1);
607
608 *rfchipid = RTW_SR_GET(sr, RTW_SR_RFCHIPID);
609
610 if (sc->sc_flags & RTW_F_RTL8185) {
611 *locale = RTW_LOCALE_UNKNOWN;
612 return (0);
613 }
614
615 switch (RTW_SR_GET(sr, RTW_SR_CONFIG0) & RTW8180_CONFIG0_GL_MASK) {
616 case RTW8180_CONFIG0_GL_USA:
617 *locale = RTW_LOCALE_USA;
618 break;
619 case RTW8180_CONFIG0_GL_EUROPE:
620 *locale = RTW_LOCALE_EUROPE;
621 break;
622 case RTW8180_CONFIG0_GL_JAPAN:
623 case RTW8180_CONFIG0_GL_JAPAN2:
624 *locale = RTW_LOCALE_JAPAN;
625 break;
626 default:
627 *locale = RTW_LOCALE_UNKNOWN;
628 break;
629 }
630 return 0;
631 }
632
633 /* Returns -1 on failure. */
634 int
rtw_srom_read(struct rtw_regs * regs,u_int32_t flags,struct rtw_srom * sr,const char * dvname)635 rtw_srom_read(struct rtw_regs *regs, u_int32_t flags, struct rtw_srom *sr,
636 const char *dvname)
637 {
638 int rc;
639 struct seeprom_descriptor sd;
640 u_int8_t ecr;
641
642 bzero(&sd, sizeof(sd));
643
644 ecr = RTW_READ8(regs, RTW_9346CR);
645
646 if ((flags & RTW_F_9356SROM) != 0) {
647 RTW_DPRINTF(RTW_DEBUG_ATTACH, ("%s: 93c56 SROM\n", dvname));
648 sr->sr_size = 256;
649 sd.sd_chip = C56_66;
650 } else {
651 RTW_DPRINTF(RTW_DEBUG_ATTACH, ("%s: 93c46 SROM\n", dvname));
652 sr->sr_size = 128;
653 sd.sd_chip = C46;
654 }
655
656 ecr &= ~(RTW_9346CR_EEDI | RTW_9346CR_EEDO | RTW_9346CR_EESK |
657 RTW_9346CR_EEM_MASK | RTW_9346CR_EECS);
658 ecr |= RTW_9346CR_EEM_PROGRAM;
659
660 RTW_WRITE8(regs, RTW_9346CR, ecr);
661
662 sr->sr_content = malloc(sr->sr_size, M_DEVBUF, M_NOWAIT | M_ZERO);
663
664 if (sr->sr_content == NULL) {
665 printf("%s: unable to allocate SROM buffer\n", dvname);
666 return ENOMEM;
667 }
668
669 /* RTL8180 has a single 8-bit register for controlling the
670 * 93cx6 SROM. There is no "ready" bit. The RTL8180
671 * input/output sense is the reverse of read_seeprom's.
672 */
673 sd.sd_tag = regs->r_bt;
674 sd.sd_bsh = regs->r_bh;
675 sd.sd_regsize = 1;
676 sd.sd_control_offset = RTW_9346CR;
677 sd.sd_status_offset = RTW_9346CR;
678 sd.sd_dataout_offset = RTW_9346CR;
679 sd.sd_CK = RTW_9346CR_EESK;
680 sd.sd_CS = RTW_9346CR_EECS;
681 sd.sd_DI = RTW_9346CR_EEDO;
682 sd.sd_DO = RTW_9346CR_EEDI;
683 /* make read_seeprom enter EEPROM read/write mode */
684 sd.sd_MS = ecr;
685 sd.sd_RDY = 0;
686
687 /* TBD bus barriers */
688 if (!read_seeprom(&sd, sr->sr_content, 0, sr->sr_size/2)) {
689 printf("\n%s: could not read SROM\n", dvname);
690 free(sr->sr_content, M_DEVBUF, 0);
691 sr->sr_content = NULL;
692 return -1; /* XXX */
693 }
694
695 /* end EEPROM read/write mode */
696 RTW_WRITE8(regs, RTW_9346CR,
697 (ecr & ~RTW_9346CR_EEM_MASK) | RTW_9346CR_EEM_NORMAL);
698 RTW_WBRW(regs, RTW_9346CR, RTW_9346CR);
699
700 if ((rc = rtw_recall_eeprom(regs, dvname)) != 0)
701 return rc;
702
703 #ifdef RTW_DEBUG
704 {
705 int i;
706 RTW_DPRINTF(RTW_DEBUG_ATTACH,
707 ("\n%s: serial ROM:\n\t", dvname));
708 for (i = 0; i < sr->sr_size/2; i++) {
709 if (((i % 8) == 0) && (i != 0))
710 RTW_DPRINTF(RTW_DEBUG_ATTACH, ("\n\t"));
711 RTW_DPRINTF(RTW_DEBUG_ATTACH,
712 (" %04x", sr->sr_content[i]));
713 }
714 RTW_DPRINTF(RTW_DEBUG_ATTACH, ("\n"));
715 }
716 #endif /* RTW_DEBUG */
717 return 0;
718 }
719
720 void
rtw_set_rfprog(struct rtw_regs * regs,int rfchipid,const char * dvname)721 rtw_set_rfprog(struct rtw_regs *regs, int rfchipid,
722 const char *dvname)
723 {
724 u_int8_t cfg4;
725 const char *method;
726
727 cfg4 = RTW_READ8(regs, RTW_CONFIG4) & ~RTW_CONFIG4_RFTYPE_MASK;
728
729 switch (rfchipid) {
730 default:
731 cfg4 |= LSHIFT(rtw_rfprog_fallback, RTW_CONFIG4_RFTYPE_MASK);
732 method = "fallback";
733 break;
734 case RTW_RFCHIPID_INTERSIL:
735 cfg4 |= RTW_CONFIG4_RFTYPE_INTERSIL;
736 method = "Intersil";
737 break;
738 case RTW_RFCHIPID_PHILIPS:
739 cfg4 |= RTW_CONFIG4_RFTYPE_PHILIPS;
740 method = "Philips";
741 break;
742 case RTW_RFCHIPID_RFMD2948:
743 cfg4 |= RTW_CONFIG4_RFTYPE_RFMD;
744 method = "RFMD";
745 break;
746 }
747
748 RTW_WRITE8(regs, RTW_CONFIG4, cfg4);
749
750 RTW_WBR(regs, RTW_CONFIG4, RTW_CONFIG4);
751
752 RTW_DPRINTF(RTW_DEBUG_INIT,
753 ("%s: %s RF programming method, %#02x\n", dvname, method,
754 RTW_READ8(regs, RTW_CONFIG4)));
755 }
756
757 void
rtw_identify_country(struct rtw_regs * regs,enum rtw_locale * locale)758 rtw_identify_country(struct rtw_regs *regs, enum rtw_locale *locale)
759 {
760 u_int8_t cfg0 = RTW_READ8(regs, RTW_CONFIG0);
761
762 switch (cfg0 & RTW8180_CONFIG0_GL_MASK) {
763 case RTW8180_CONFIG0_GL_USA:
764 *locale = RTW_LOCALE_USA;
765 break;
766 case RTW8180_CONFIG0_GL_JAPAN:
767 case RTW8180_CONFIG0_GL_JAPAN2:
768 *locale = RTW_LOCALE_JAPAN;
769 break;
770 case RTW8180_CONFIG0_GL_EUROPE:
771 *locale = RTW_LOCALE_EUROPE;
772 break;
773 default:
774 *locale = RTW_LOCALE_UNKNOWN;
775 break;
776 }
777 }
778
779 int
rtw_identify_sta(struct rtw_regs * regs,u_int8_t (* addr)[IEEE80211_ADDR_LEN],const char * dvname)780 rtw_identify_sta(struct rtw_regs *regs, u_int8_t (*addr)[IEEE80211_ADDR_LEN],
781 const char *dvname)
782 {
783 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
784 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
785 };
786 u_int32_t idr0 = RTW_READ(regs, RTW_IDR0),
787 idr1 = RTW_READ(regs, RTW_IDR1);
788
789 (*addr)[0] = MASK_AND_RSHIFT(idr0, 0xff);
790 (*addr)[1] = MASK_AND_RSHIFT(idr0, 0xff00);
791 (*addr)[2] = MASK_AND_RSHIFT(idr0, 0xff0000);
792 (*addr)[3] = MASK_AND_RSHIFT(idr0, 0xff000000);
793
794 (*addr)[4] = MASK_AND_RSHIFT(idr1, 0xff);
795 (*addr)[5] = MASK_AND_RSHIFT(idr1, 0xff00);
796
797 if (IEEE80211_ADDR_EQ(addr, empty_macaddr)) {
798 printf("\n%s: could not get mac address, attach failed\n",
799 dvname);
800 return ENXIO;
801 }
802
803 printf("address %s\n", ether_sprintf(*addr));
804
805 return 0;
806 }
807
808 u_int8_t
rtw_chan2txpower(struct rtw_srom * sr,struct ieee80211com * ic,struct ieee80211_channel * chan)809 rtw_chan2txpower(struct rtw_srom *sr, struct ieee80211com *ic,
810 struct ieee80211_channel *chan)
811 {
812 u_int idx = RTW_SR_TXPOWER1 + ieee80211_chan2ieee(ic, chan) - 1;
813 KASSERT2(idx >= RTW_SR_TXPOWER1 && idx <= RTW_SR_TXPOWER14,
814 ("%s: channel %d out of range", __func__,
815 idx - RTW_SR_TXPOWER1 + 1));
816 return RTW_SR_GET(sr, idx);
817 }
818
819 void
rtw_txdesc_blk_init_all(struct rtw_txdesc_blk * tdb)820 rtw_txdesc_blk_init_all(struct rtw_txdesc_blk *tdb)
821 {
822 int pri;
823 /* nfree: the number of free descriptors in each ring.
824 * The beacon ring is a special case: I do not let the
825 * driver use all of the descriptors on the beacon ring.
826 * The reasons are two-fold:
827 *
828 * (1) A BEACON descriptor's OWN bit is (apparently) not
829 * updated, so the driver cannot easily know if the descriptor
830 * belongs to it, or if it is racing the NIC. If the NIC
831 * does not OWN every descriptor, then the driver can safely
832 * update the descriptors when RTW_TBDA points at tdb_next.
833 *
834 * (2) I hope that the NIC will process more than one BEACON
835 * descriptor in a single beacon interval, since that will
836 * enable multiple-BSS support. Since the NIC does not
837 * clear the OWN bit, there is no natural place for it to
838 * stop processing BEACON descriptors. Maybe it will *not*
839 * stop processing them! I do not want to chance the NIC
840 * looping around and around a saturated beacon ring, so
841 * I will leave one descriptor unOWNed at all times.
842 */
843 u_int nfree[RTW_NTXPRI] =
844 {RTW_NTXDESCLO, RTW_NTXDESCMD, RTW_NTXDESCHI,
845 RTW_NTXDESCBCN - 1};
846
847 for (pri = 0; pri < RTW_NTXPRI; pri++) {
848 tdb[pri].tdb_nfree = nfree[pri];
849 tdb[pri].tdb_next = 0;
850 }
851 }
852
853 int
rtw_txsoft_blk_init(struct rtw_txsoft_blk * tsb)854 rtw_txsoft_blk_init(struct rtw_txsoft_blk *tsb)
855 {
856 int i;
857 struct rtw_txsoft *ts;
858
859 SIMPLEQ_INIT(&tsb->tsb_dirtyq);
860 SIMPLEQ_INIT(&tsb->tsb_freeq);
861 for (i = 0; i < tsb->tsb_ndesc; i++) {
862 ts = &tsb->tsb_desc[i];
863 ts->ts_mbuf = NULL;
864 SIMPLEQ_INSERT_TAIL(&tsb->tsb_freeq, ts, ts_q);
865 }
866 tsb->tsb_tx_timer = 0;
867 return 0;
868 }
869
870 void
rtw_txsoft_blk_init_all(struct rtw_txsoft_blk * tsb)871 rtw_txsoft_blk_init_all(struct rtw_txsoft_blk *tsb)
872 {
873 int pri;
874 for (pri = 0; pri < RTW_NTXPRI; pri++)
875 rtw_txsoft_blk_init(&tsb[pri]);
876 }
877
878 void
rtw_rxdescs_sync(struct rtw_rxdesc_blk * rdb,int desc0,int nsync,int ops)879 rtw_rxdescs_sync(struct rtw_rxdesc_blk *rdb, int desc0, int nsync, int ops)
880 {
881 KASSERT(nsync <= rdb->rdb_ndesc);
882 /* sync to end of ring */
883 if (desc0 + nsync > rdb->rdb_ndesc) {
884 bus_dmamap_sync(rdb->rdb_dmat, rdb->rdb_dmamap,
885 offsetof(struct rtw_descs, hd_rx[desc0]),
886 sizeof(struct rtw_rxdesc) * (rdb->rdb_ndesc - desc0), ops);
887 nsync -= (rdb->rdb_ndesc - desc0);
888 desc0 = 0;
889 }
890
891 KASSERT(desc0 < rdb->rdb_ndesc);
892 KASSERT(nsync <= rdb->rdb_ndesc);
893 KASSERT(desc0 + nsync <= rdb->rdb_ndesc);
894
895 /* sync what remains */
896 bus_dmamap_sync(rdb->rdb_dmat, rdb->rdb_dmamap,
897 offsetof(struct rtw_descs, hd_rx[desc0]),
898 sizeof(struct rtw_rxdesc) * nsync, ops);
899 }
900
901 void
rtw_txdescs_sync(struct rtw_txdesc_blk * tdb,u_int desc0,u_int nsync,int ops)902 rtw_txdescs_sync(struct rtw_txdesc_blk *tdb, u_int desc0, u_int nsync, int ops)
903 {
904 /* sync to end of ring */
905 if (desc0 + nsync > tdb->tdb_ndesc) {
906 bus_dmamap_sync(tdb->tdb_dmat, tdb->tdb_dmamap,
907 tdb->tdb_ofs + sizeof(struct rtw_txdesc) * desc0,
908 sizeof(struct rtw_txdesc) * (tdb->tdb_ndesc - desc0),
909 ops);
910 nsync -= (tdb->tdb_ndesc - desc0);
911 desc0 = 0;
912 }
913
914 /* sync what remains */
915 bus_dmamap_sync(tdb->tdb_dmat, tdb->tdb_dmamap,
916 tdb->tdb_ofs + sizeof(struct rtw_txdesc) * desc0,
917 sizeof(struct rtw_txdesc) * nsync, ops);
918 }
919
920 void
rtw_rxbufs_release(bus_dma_tag_t dmat,struct rtw_rxsoft * desc)921 rtw_rxbufs_release(bus_dma_tag_t dmat, struct rtw_rxsoft *desc)
922 {
923 int i;
924 struct rtw_rxsoft *rs;
925
926 for (i = 0; i < RTW_RXQLEN; i++) {
927 rs = &desc[i];
928 if (rs->rs_mbuf == NULL)
929 continue;
930 bus_dmamap_sync(dmat, rs->rs_dmamap, 0,
931 rs->rs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
932 bus_dmamap_unload(dmat, rs->rs_dmamap);
933 m_freem(rs->rs_mbuf);
934 rs->rs_mbuf = NULL;
935 }
936 }
937
938 int
rtw_rxsoft_alloc(bus_dma_tag_t dmat,struct rtw_rxsoft * rs)939 rtw_rxsoft_alloc(bus_dma_tag_t dmat, struct rtw_rxsoft *rs)
940 {
941 int rc;
942 struct mbuf *m;
943
944 MGETHDR(m, M_DONTWAIT, MT_DATA);
945 if (m == NULL)
946 return ENOBUFS;
947
948 MCLGET(m, M_DONTWAIT);
949 if ((m->m_flags & M_EXT) == 0) {
950 m_freem(m);
951 return ENOBUFS;
952 }
953
954 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
955
956 if (rs->rs_mbuf != NULL)
957 bus_dmamap_unload(dmat, rs->rs_dmamap);
958
959 rs->rs_mbuf = NULL;
960
961 rc = bus_dmamap_load_mbuf(dmat, rs->rs_dmamap, m, BUS_DMA_NOWAIT);
962 if (rc != 0) {
963 m_freem(m);
964 return -1;
965 }
966
967 rs->rs_mbuf = m;
968
969 return 0;
970 }
971
972 int
rtw_rxsoft_init_all(bus_dma_tag_t dmat,struct rtw_rxsoft * desc,int * ndesc,const char * dvname)973 rtw_rxsoft_init_all(bus_dma_tag_t dmat, struct rtw_rxsoft *desc,
974 int *ndesc, const char *dvname)
975 {
976 int i, rc = 0;
977 struct rtw_rxsoft *rs;
978
979 for (i = 0; i < RTW_RXQLEN; i++) {
980 rs = &desc[i];
981 /* we're in rtw_init, so there should be no mbufs allocated */
982 KASSERT(rs->rs_mbuf == NULL);
983 #ifdef RTW_DEBUG
984 if (i == rtw_rxbufs_limit) {
985 printf("%s: TEST hit %d-buffer limit\n", dvname, i);
986 rc = ENOBUFS;
987 break;
988 }
989 #endif /* RTW_DEBUG */
990 if ((rc = rtw_rxsoft_alloc(dmat, rs)) != 0) {
991 printf("%s: rtw_rxsoft_alloc failed, %d buffers, "
992 "rc %d\n", dvname, i, rc);
993 break;
994 }
995 }
996 *ndesc = i;
997 return rc;
998 }
999
1000 void
rtw_rxdesc_init(struct rtw_rxdesc_blk * rdb,struct rtw_rxsoft * rs,int idx,int kick)1001 rtw_rxdesc_init(struct rtw_rxdesc_blk *rdb, struct rtw_rxsoft *rs,
1002 int idx, int kick)
1003 {
1004 int is_last = (idx == rdb->rdb_ndesc - 1);
1005 uint32_t ctl, octl, obuf;
1006 struct rtw_rxdesc *rd = &rdb->rdb_desc[idx];
1007
1008 obuf = rd->rd_buf;
1009 rd->rd_buf = htole32(rs->rs_dmamap->dm_segs[0].ds_addr);
1010
1011 ctl = LSHIFT(rs->rs_mbuf->m_len, RTW_RXCTL_LENGTH_MASK) |
1012 RTW_RXCTL_OWN | RTW_RXCTL_FS | RTW_RXCTL_LS;
1013
1014 if (is_last)
1015 ctl |= RTW_RXCTL_EOR;
1016
1017 octl = rd->rd_ctl;
1018 rd->rd_ctl = htole32(ctl);
1019
1020 RTW_DPRINTF(kick ? (RTW_DEBUG_RECV_DESC | RTW_DEBUG_IO_KICK)
1021 : RTW_DEBUG_RECV_DESC,
1022 ("%s: rd %p buf %08x -> %08x ctl %08x -> %08x\n", __func__, rd,
1023 letoh32(obuf), letoh32(rd->rd_buf), letoh32(octl),
1024 letoh32(rd->rd_ctl)));
1025
1026 /* sync the mbuf */
1027 bus_dmamap_sync(rdb->rdb_dmat, rs->rs_dmamap, 0,
1028 rs->rs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1029
1030 /* sync the descriptor */
1031 bus_dmamap_sync(rdb->rdb_dmat, rdb->rdb_dmamap,
1032 RTW_DESC_OFFSET(hd_rx, idx), sizeof(struct rtw_rxdesc),
1033 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1034 }
1035
1036 void
rtw_rxdesc_init_all(struct rtw_rxdesc_blk * rdb,struct rtw_rxsoft * ctl,int kick)1037 rtw_rxdesc_init_all(struct rtw_rxdesc_blk *rdb, struct rtw_rxsoft *ctl,
1038 int kick)
1039 {
1040 int i;
1041 struct rtw_rxsoft *rs;
1042
1043 for (i = 0; i < rdb->rdb_ndesc; i++) {
1044 rs = &ctl[i];
1045 rtw_rxdesc_init(rdb, rs, i, kick);
1046 }
1047 }
1048
1049 void
rtw_io_enable(struct rtw_regs * regs,u_int8_t flags,int enable)1050 rtw_io_enable(struct rtw_regs *regs, u_int8_t flags, int enable)
1051 {
1052 u_int8_t cr;
1053
1054 RTW_DPRINTF(RTW_DEBUG_IOSTATE, ("%s: %s 0x%02x\n", __func__,
1055 enable ? "enable" : "disable", flags));
1056
1057 cr = RTW_READ8(regs, RTW_CR);
1058
1059 /* XXX reference source does not enable MULRW */
1060 #if 0
1061 /* enable PCI Read/Write Multiple */
1062 cr |= RTW_CR_MULRW;
1063 #endif
1064
1065 RTW_RBW(regs, RTW_CR, RTW_CR); /* XXX paranoia? */
1066 if (enable)
1067 cr |= flags;
1068 else
1069 cr &= ~flags;
1070 RTW_WRITE8(regs, RTW_CR, cr);
1071 RTW_SYNC(regs, RTW_CR, RTW_CR);
1072 }
1073
1074 void
rtw_intr_rx(struct rtw_softc * sc,u_int16_t isr)1075 rtw_intr_rx(struct rtw_softc *sc, u_int16_t isr)
1076 {
1077 #define IS_BEACON(__fc0) \
1078 ((__fc0 & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==\
1079 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_BEACON))
1080
1081 static const int ratetbl[4] = {2, 4, 11, 22}; /* convert rates:
1082 * hardware -> net80211
1083 */
1084 struct mbuf_list ml = MBUF_LIST_INITIALIZER();
1085 u_int next, nproc = 0;
1086 int hwrate, len, rate, rssi, sq;
1087 u_int32_t hrssi, hstat, htsfth, htsftl;
1088 struct rtw_rxdesc *rd;
1089 struct rtw_rxsoft *rs;
1090 struct rtw_rxdesc_blk *rdb;
1091 struct mbuf *m;
1092 struct ieee80211_rxinfo rxi;
1093 struct ieee80211_node *ni;
1094 struct ieee80211_frame *wh;
1095
1096 rdb = &sc->sc_rxdesc_blk;
1097
1098 KASSERT(rdb->rdb_next < rdb->rdb_ndesc);
1099
1100 for (next = rdb->rdb_next; ; next = (next + 1) % rdb->rdb_ndesc) {
1101 rtw_rxdescs_sync(rdb, next, 1,
1102 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1103 rd = &rdb->rdb_desc[next];
1104 rs = &sc->sc_rxsoft[next];
1105
1106 hstat = letoh32(rd->rd_stat);
1107 hrssi = letoh32(rd->rd_rssi);
1108 htsfth = letoh32(rd->rd_tsfth);
1109 htsftl = letoh32(rd->rd_tsftl);
1110
1111 RTW_DPRINTF(RTW_DEBUG_RECV_DESC,
1112 ("%s: rxdesc[%d] hstat %08x hrssi %08x htsft %08x%08x\n",
1113 __func__, next, hstat, hrssi, htsfth, htsftl));
1114
1115 ++nproc;
1116
1117 /* still belongs to NIC */
1118 if ((hstat & RTW_RXSTAT_OWN) != 0) {
1119 if (nproc > 1)
1120 break;
1121
1122 /* sometimes the NIC skips to the 0th descriptor */
1123 rtw_rxdescs_sync(rdb, 0, 1,
1124 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1125 rd = &rdb->rdb_desc[0];
1126 if ((rd->rd_stat & htole32(RTW_RXSTAT_OWN)) != 0)
1127 break;
1128 RTW_DPRINTF(RTW_DEBUG_BUGS,
1129 ("%s: NIC skipped from rxdesc[%u] to rxdesc[0]\n",
1130 sc->sc_dev.dv_xname, next));
1131 next = rdb->rdb_ndesc - 1;
1132 continue;
1133 }
1134
1135 #ifdef RTW_DEBUG
1136 #define PRINTSTAT(flag) do { \
1137 if ((hstat & flag) != 0) { \
1138 printf("%s" #flag, delim); \
1139 delim = ","; \
1140 } \
1141 } while (0)
1142 if ((rtw_debug & RTW_DEBUG_RECV_DESC) != 0) {
1143 const char *delim = "<";
1144 printf("%s: ", sc->sc_dev.dv_xname);
1145 if ((hstat & RTW_RXSTAT_DEBUG) != 0) {
1146 printf("status %08x", hstat);
1147 PRINTSTAT(RTW_RXSTAT_SPLCP);
1148 PRINTSTAT(RTW_RXSTAT_MAR);
1149 PRINTSTAT(RTW_RXSTAT_PAR);
1150 PRINTSTAT(RTW_RXSTAT_BAR);
1151 PRINTSTAT(RTW_RXSTAT_PWRMGT);
1152 PRINTSTAT(RTW_RXSTAT_CRC32);
1153 PRINTSTAT(RTW_RXSTAT_ICV);
1154 printf(">, ");
1155 }
1156 }
1157 #undef PRINTSTAT
1158 #endif /* RTW_DEBUG */
1159
1160 if ((hstat & RTW_RXSTAT_IOERROR) != 0) {
1161 printf("%s: DMA error/FIFO overflow %08x, "
1162 "rx descriptor %d\n", sc->sc_dev.dv_xname,
1163 hstat & RTW_RXSTAT_IOERROR, next);
1164 sc->sc_if.if_ierrors++;
1165 goto next;
1166 }
1167
1168 len = MASK_AND_RSHIFT(hstat, RTW_RXSTAT_LENGTH_MASK);
1169 if (len < IEEE80211_MIN_LEN) {
1170 sc->sc_ic.ic_stats.is_rx_tooshort++;
1171 goto next;
1172 }
1173
1174 /* CRC is included with the packet; trim it off. */
1175 len -= IEEE80211_CRC_LEN;
1176
1177 hwrate = MASK_AND_RSHIFT(hstat, RTW_RXSTAT_RATE_MASK);
1178 if (hwrate >= sizeof(ratetbl) / sizeof(ratetbl[0])) {
1179 printf("%s: unknown rate #%d\n", sc->sc_dev.dv_xname,
1180 MASK_AND_RSHIFT(hstat, RTW_RXSTAT_RATE_MASK));
1181 sc->sc_if.if_ierrors++;
1182 goto next;
1183 }
1184 rate = ratetbl[hwrate];
1185
1186 #ifdef RTW_DEBUG
1187 RTW_DPRINTF(RTW_DEBUG_RECV_DESC,
1188 ("rate %d.%d Mb/s, time %08x%08x\n", (rate * 5) / 10,
1189 (rate * 5) % 10, htsfth, htsftl));
1190 #endif /* RTW_DEBUG */
1191
1192 if ((hstat & RTW_RXSTAT_RES) != 0 &&
1193 sc->sc_ic.ic_opmode != IEEE80211_M_MONITOR)
1194 goto next;
1195
1196 /* if bad flags, skip descriptor */
1197 if ((hstat & RTW_RXSTAT_ONESEG) != RTW_RXSTAT_ONESEG) {
1198 printf("%s: too many rx segments\n",
1199 sc->sc_dev.dv_xname);
1200 goto next;
1201 }
1202
1203 bus_dmamap_sync(sc->sc_dmat, rs->rs_dmamap, 0,
1204 rs->rs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1205
1206 m = rs->rs_mbuf;
1207
1208 /* if temporarily out of memory, re-use mbuf */
1209 switch (rtw_rxsoft_alloc(sc->sc_dmat, rs)) {
1210 case 0:
1211 break;
1212 case ENOBUFS:
1213 printf("%s: rtw_rxsoft_alloc(, %d) failed, "
1214 "dropping this packet\n", sc->sc_dev.dv_xname,
1215 next);
1216 goto next;
1217 default:
1218 /* XXX shorten rx ring, instead? */
1219 panic("%s: could not load DMA map",
1220 sc->sc_dev.dv_xname);
1221 }
1222
1223 if (sc->sc_rfchipid == RTW_RFCHIPID_PHILIPS)
1224 rssi = MASK_AND_RSHIFT(hrssi, RTW_RXRSSI_RSSI);
1225 else {
1226 rssi = MASK_AND_RSHIFT(hrssi, RTW_RXRSSI_IMR_RSSI);
1227 /* TBD find out each front-end's LNA gain in the
1228 * front-end's units
1229 */
1230 if ((hrssi & RTW_RXRSSI_IMR_LNA) == 0)
1231 rssi |= 0x80;
1232 }
1233
1234 sq = MASK_AND_RSHIFT(hrssi, RTW_RXRSSI_SQ);
1235
1236 /*
1237 * Note well: now we cannot recycle the rs_mbuf unless
1238 * we restore its original length.
1239 */
1240 m->m_pkthdr.len = m->m_len = len;
1241
1242 wh = mtod(m, struct ieee80211_frame *);
1243
1244 if (!IS_BEACON(wh->i_fc[0]))
1245 sc->sc_led_state.ls_event |= RTW_LED_S_RX;
1246 /* TBD use _MAR, _BAR, _PAR flags as hints to _find_rxnode? */
1247 ni = ieee80211_find_rxnode(&sc->sc_ic, wh);
1248
1249 sc->sc_tsfth = htsfth;
1250
1251 #ifdef RTW_DEBUG
1252 if ((sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) ==
1253 (IFF_DEBUG|IFF_LINK2)) {
1254 ieee80211_dump_pkt(mtod(m, uint8_t *), m->m_pkthdr.len,
1255 rate, rssi);
1256 }
1257 #endif /* RTW_DEBUG */
1258
1259 #if NBPFILTER > 0
1260 if (sc->sc_radiobpf != NULL) {
1261 struct mbuf mb;
1262 struct ieee80211com *ic = &sc->sc_ic;
1263 struct rtw_rx_radiotap_header *rr = &sc->sc_rxtap;
1264
1265 rr->rr_tsft =
1266 htole64(((uint64_t)htsfth << 32) | htsftl);
1267
1268 rr->rr_flags = 0;
1269 if ((hstat & RTW_RXSTAT_SPLCP) != 0)
1270 rr->rr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1271
1272 rr->rr_rate = rate;
1273 rr->rr_chan_freq =
1274 htole16(ic->ic_bss->ni_chan->ic_freq);
1275 rr->rr_chan_flags =
1276 htole16(ic->ic_bss->ni_chan->ic_flags);
1277 rr->rr_antsignal = rssi;
1278 rr->rr_barker_lock = htole16(sq);
1279
1280 mb.m_data = (caddr_t)rr;
1281 mb.m_len = sizeof(sc->sc_rxtapu);
1282 mb.m_next = m;
1283 mb.m_nextpkt = NULL;
1284 mb.m_type = 0;
1285 mb.m_flags = 0;
1286 bpf_mtap(sc->sc_radiobpf, &mb, BPF_DIRECTION_IN);
1287 }
1288 #endif /* NBPFILTER > 0 */
1289
1290 memset(&rxi, 0, sizeof(rxi));
1291 rxi.rxi_rssi = rssi;
1292 rxi.rxi_tstamp = htsftl;
1293 ieee80211_inputm(&sc->sc_if, m, ni, &rxi, &ml);
1294 ieee80211_release_node(&sc->sc_ic, ni);
1295 next:
1296 rtw_rxdesc_init(rdb, rs, next, 0);
1297 }
1298 if_input(&sc->sc_if, &ml);
1299 rdb->rdb_next = next;
1300
1301 KASSERT(rdb->rdb_next < rdb->rdb_ndesc);
1302
1303 return;
1304 #undef IS_BEACON
1305 }
1306
1307 void
rtw_txsoft_release(bus_dma_tag_t dmat,struct ieee80211com * ic,struct rtw_txsoft * ts)1308 rtw_txsoft_release(bus_dma_tag_t dmat, struct ieee80211com *ic,
1309 struct rtw_txsoft *ts)
1310 {
1311 struct mbuf *m;
1312 struct ieee80211_node *ni;
1313
1314 m = ts->ts_mbuf;
1315 ni = ts->ts_ni;
1316 KASSERT(m != NULL);
1317 KASSERT(ni != NULL);
1318 ts->ts_mbuf = NULL;
1319 ts->ts_ni = NULL;
1320
1321 bus_dmamap_sync(dmat, ts->ts_dmamap, 0, ts->ts_dmamap->dm_mapsize,
1322 BUS_DMASYNC_POSTWRITE);
1323 bus_dmamap_unload(dmat, ts->ts_dmamap);
1324 m_freem(m);
1325 ieee80211_release_node(ic, ni);
1326 }
1327
1328 void
rtw_txsofts_release(bus_dma_tag_t dmat,struct ieee80211com * ic,struct rtw_txsoft_blk * tsb)1329 rtw_txsofts_release(bus_dma_tag_t dmat, struct ieee80211com *ic,
1330 struct rtw_txsoft_blk *tsb)
1331 {
1332 struct rtw_txsoft *ts;
1333
1334 while ((ts = SIMPLEQ_FIRST(&tsb->tsb_dirtyq)) != NULL) {
1335 rtw_txsoft_release(dmat, ic, ts);
1336 SIMPLEQ_REMOVE_HEAD(&tsb->tsb_dirtyq, ts_q);
1337 SIMPLEQ_INSERT_TAIL(&tsb->tsb_freeq, ts, ts_q);
1338 }
1339 tsb->tsb_tx_timer = 0;
1340 }
1341
1342 void
rtw_collect_txpkt(struct rtw_softc * sc,struct rtw_txdesc_blk * tdb,struct rtw_txsoft * ts,int ndesc)1343 rtw_collect_txpkt(struct rtw_softc *sc, struct rtw_txdesc_blk *tdb,
1344 struct rtw_txsoft *ts, int ndesc)
1345 {
1346 uint32_t hstat;
1347 int data_retry, rts_retry;
1348 struct rtw_txdesc *tdn;
1349 const char *condstring;
1350
1351 rtw_txsoft_release(sc->sc_dmat, &sc->sc_ic, ts);
1352
1353 tdb->tdb_nfree += ndesc;
1354
1355 tdn = &tdb->tdb_desc[ts->ts_last];
1356
1357 hstat = letoh32(tdn->td_stat);
1358 rts_retry = MASK_AND_RSHIFT(hstat, RTW_TXSTAT_RTSRETRY_MASK);
1359 data_retry = MASK_AND_RSHIFT(hstat, RTW_TXSTAT_DRC_MASK);
1360
1361 sc->sc_if.if_collisions += rts_retry + data_retry;
1362
1363 if ((hstat & RTW_TXSTAT_TOK) != 0)
1364 condstring = "ok";
1365 else {
1366 sc->sc_if.if_oerrors++;
1367 condstring = "error";
1368 }
1369
1370 DPRINTF(sc, RTW_DEBUG_XMIT_DESC,
1371 ("%s: ts %p txdesc[%d, %d] %s tries rts %u data %u\n",
1372 sc->sc_dev.dv_xname, ts, ts->ts_first, ts->ts_last,
1373 condstring, rts_retry, data_retry));
1374 }
1375
1376 void
rtw_reset_oactive(struct rtw_softc * sc)1377 rtw_reset_oactive(struct rtw_softc *sc)
1378 {
1379 int oactive;
1380 int pri;
1381 struct rtw_txsoft_blk *tsb;
1382 struct rtw_txdesc_blk *tdb;
1383 oactive = ifq_is_oactive(&sc->sc_if.if_snd);
1384 for (pri = 0; pri < RTW_NTXPRI; pri++) {
1385 tsb = &sc->sc_txsoft_blk[pri];
1386 tdb = &sc->sc_txdesc_blk[pri];
1387 if (!SIMPLEQ_EMPTY(&tsb->tsb_freeq) && tdb->tdb_nfree > 0)
1388 ifq_set_oactive(&sc->sc_if.if_snd);
1389 }
1390 if (oactive != ifq_is_oactive(&sc->sc_if.if_snd)) {
1391 DPRINTF(sc, RTW_DEBUG_OACTIVE,
1392 ("%s: reset OACTIVE\n", __func__));
1393 }
1394 }
1395
1396 /* Collect transmitted packets. */
1397 void
rtw_collect_txring(struct rtw_softc * sc,struct rtw_txsoft_blk * tsb,struct rtw_txdesc_blk * tdb,int force)1398 rtw_collect_txring(struct rtw_softc *sc, struct rtw_txsoft_blk *tsb,
1399 struct rtw_txdesc_blk *tdb, int force)
1400 {
1401 int ndesc;
1402 struct rtw_txsoft *ts;
1403
1404 while ((ts = SIMPLEQ_FIRST(&tsb->tsb_dirtyq)) != NULL) {
1405 ndesc = 1 + ts->ts_last - ts->ts_first;
1406 if (ts->ts_last < ts->ts_first)
1407 ndesc += tdb->tdb_ndesc;
1408
1409 KASSERT(ndesc > 0);
1410
1411 rtw_txdescs_sync(tdb, ts->ts_first, ndesc,
1412 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1413
1414 if (force) {
1415 int i;
1416 for (i = ts->ts_first; ; i = RTW_NEXT_IDX(tdb, i)) {
1417 tdb->tdb_desc[i].td_stat &=
1418 ~htole32(RTW_TXSTAT_OWN);
1419 if (i == ts->ts_last)
1420 break;
1421 }
1422 rtw_txdescs_sync(tdb, ts->ts_first, ndesc,
1423 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1424 } else if ((tdb->tdb_desc[ts->ts_last].td_stat &
1425 htole32(RTW_TXSTAT_OWN)) != 0)
1426 break;
1427
1428 rtw_collect_txpkt(sc, tdb, ts, ndesc);
1429 SIMPLEQ_REMOVE_HEAD(&tsb->tsb_dirtyq, ts_q);
1430 SIMPLEQ_INSERT_TAIL(&tsb->tsb_freeq, ts, ts_q);
1431 }
1432 /* no more pending transmissions, cancel watchdog */
1433 if (ts == NULL)
1434 tsb->tsb_tx_timer = 0;
1435 rtw_reset_oactive(sc);
1436 }
1437
1438 void
rtw_intr_tx(struct rtw_softc * sc,u_int16_t isr)1439 rtw_intr_tx(struct rtw_softc *sc, u_int16_t isr)
1440 {
1441 int pri;
1442 struct rtw_txsoft_blk *tsb;
1443 struct rtw_txdesc_blk *tdb;
1444
1445 for (pri = 0; pri < RTW_NTXPRI; pri++) {
1446 tsb = &sc->sc_txsoft_blk[pri];
1447 tdb = &sc->sc_txdesc_blk[pri];
1448
1449 rtw_collect_txring(sc, tsb, tdb, 0);
1450
1451 }
1452
1453 if ((isr & RTW_INTR_TX) != 0)
1454 rtw_start(&sc->sc_if);
1455 }
1456
1457 #ifndef IEEE80211_STA_ONLY
1458 void
rtw_intr_beacon(struct rtw_softc * sc,u_int16_t isr)1459 rtw_intr_beacon(struct rtw_softc *sc, u_int16_t isr)
1460 {
1461 u_int next;
1462 uint32_t tsfth, tsftl;
1463 struct ieee80211com *ic;
1464 struct rtw_txdesc_blk *tdb = &sc->sc_txdesc_blk[RTW_TXPRIBCN];
1465 struct rtw_txsoft_blk *tsb = &sc->sc_txsoft_blk[RTW_TXPRIBCN];
1466 struct mbuf *m;
1467
1468 tsfth = RTW_READ(&sc->sc_regs, RTW_TSFTRH);
1469 tsftl = RTW_READ(&sc->sc_regs, RTW_TSFTRL);
1470
1471 if ((isr & (RTW_INTR_TBDOK|RTW_INTR_TBDER)) != 0) {
1472 next = rtw_txring_next(&sc->sc_regs, tdb);
1473 RTW_DPRINTF(RTW_DEBUG_BEACON,
1474 ("%s: beacon ring %sprocessed, isr = %#04hx"
1475 ", next %u expected %u, %llu\n", __func__,
1476 (next == tdb->tdb_next) ? "" : "un", isr, next,
1477 tdb->tdb_next, (uint64_t)tsfth << 32 | tsftl));
1478 if ((RTW_READ8(&sc->sc_regs, RTW_TPPOLL) & RTW_TPPOLL_BQ) == 0){
1479 rtw_collect_txring(sc, tsb, tdb, 1);
1480 tdb->tdb_next = 0;
1481 }
1482 }
1483 /* Start beacon transmission. */
1484
1485 if ((isr & RTW_INTR_BCNINT) != 0 &&
1486 sc->sc_ic.ic_state == IEEE80211_S_RUN &&
1487 SIMPLEQ_EMPTY(&tsb->tsb_dirtyq)) {
1488 RTW_DPRINTF(RTW_DEBUG_BEACON,
1489 ("%s: beacon prep. time, isr = %#04hx"
1490 ", %16llu\n", __func__, isr,
1491 (uint64_t)tsfth << 32 | tsftl));
1492 ic = &sc->sc_ic;
1493 if ((m = ieee80211_beacon_alloc(ic, ic->ic_bss)) != NULL) {
1494 RTW_DPRINTF(RTW_DEBUG_BEACON,
1495 ("%s: m %p len %u\n", __func__, m, m->m_len));
1496 }
1497
1498 if (m == NULL) {
1499 printf("%s: could not allocate beacon\n",
1500 sc->sc_dev.dv_xname);
1501 return;
1502 }
1503 m->m_pkthdr.ph_cookie = ieee80211_ref_node(ic->ic_bss);
1504 mq_enqueue(&sc->sc_beaconq, m);
1505 rtw_start(&sc->sc_if);
1506 }
1507 }
1508
1509 void
rtw_intr_atim(struct rtw_softc * sc)1510 rtw_intr_atim(struct rtw_softc *sc)
1511 {
1512 /* TBD */
1513 return;
1514 }
1515 #endif /* IEEE80211_STA_ONLY */
1516
1517 #ifdef RTW_DEBUG
1518 void
rtw_dump_rings(struct rtw_softc * sc)1519 rtw_dump_rings(struct rtw_softc *sc)
1520 {
1521 struct rtw_txdesc_blk *tdb;
1522 struct rtw_rxdesc *rd;
1523 struct rtw_rxdesc_blk *rdb;
1524 int desc, pri;
1525
1526 if ((rtw_debug & RTW_DEBUG_IO_KICK) == 0)
1527 return;
1528
1529 for (pri = 0; pri < RTW_NTXPRI; pri++) {
1530 tdb = &sc->sc_txdesc_blk[pri];
1531 printf("%s: txpri %d ndesc %d nfree %d\n", __func__, pri,
1532 tdb->tdb_ndesc, tdb->tdb_nfree);
1533 for (desc = 0; desc < tdb->tdb_ndesc; desc++)
1534 rtw_print_txdesc(sc, ".", NULL, tdb, desc);
1535 }
1536
1537 rdb = &sc->sc_rxdesc_blk;
1538
1539 for (desc = 0; desc < RTW_RXQLEN; desc++) {
1540 rd = &rdb->rdb_desc[desc];
1541 printf("%s: %sctl %08x rsvd0/rssi %08x buf/tsftl %08x "
1542 "rsvd1/tsfth %08x\n", __func__,
1543 (desc >= rdb->rdb_ndesc) ? "UNUSED " : "",
1544 letoh32(rd->rd_ctl), letoh32(rd->rd_rssi),
1545 letoh32(rd->rd_buf), letoh32(rd->rd_tsfth));
1546 }
1547 }
1548 #endif /* RTW_DEBUG */
1549
1550 void
rtw_hwring_setup(struct rtw_softc * sc)1551 rtw_hwring_setup(struct rtw_softc *sc)
1552 {
1553 int pri;
1554 struct rtw_regs *regs = &sc->sc_regs;
1555 struct rtw_txdesc_blk *tdb;
1556
1557 sc->sc_txdesc_blk[RTW_TXPRILO].tdb_basereg = RTW_TLPDA;
1558 sc->sc_txdesc_blk[RTW_TXPRILO].tdb_base = RTW_RING_BASE(sc, hd_txlo);
1559 sc->sc_txdesc_blk[RTW_TXPRIMD].tdb_basereg = RTW_TNPDA;
1560 sc->sc_txdesc_blk[RTW_TXPRIMD].tdb_base = RTW_RING_BASE(sc, hd_txmd);
1561 sc->sc_txdesc_blk[RTW_TXPRIHI].tdb_basereg = RTW_THPDA;
1562 sc->sc_txdesc_blk[RTW_TXPRIHI].tdb_base = RTW_RING_BASE(sc, hd_txhi);
1563 sc->sc_txdesc_blk[RTW_TXPRIBCN].tdb_basereg = RTW_TBDA;
1564 sc->sc_txdesc_blk[RTW_TXPRIBCN].tdb_base = RTW_RING_BASE(sc, hd_bcn);
1565
1566 for (pri = 0; pri < RTW_NTXPRI; pri++) {
1567 tdb = &sc->sc_txdesc_blk[pri];
1568 RTW_WRITE(regs, tdb->tdb_basereg, tdb->tdb_base);
1569 RTW_DPRINTF(RTW_DEBUG_XMIT_DESC,
1570 ("%s: reg[tdb->tdb_basereg] <- %lx\n", __func__,
1571 (u_int *)tdb->tdb_base));
1572 }
1573
1574 RTW_WRITE(regs, RTW_RDSAR, RTW_RING_BASE(sc, hd_rx));
1575
1576 RTW_DPRINTF(RTW_DEBUG_RECV_DESC,
1577 ("%s: reg[RDSAR] <- %lx\n", __func__,
1578 (u_int *)RTW_RING_BASE(sc, hd_rx)));
1579
1580 RTW_SYNC(regs, RTW_TLPDA, RTW_RDSAR);
1581 }
1582
1583 int
rtw_swring_setup(struct rtw_softc * sc)1584 rtw_swring_setup(struct rtw_softc *sc)
1585 {
1586 int rc, pri;
1587 struct rtw_rxdesc_blk *rdb;
1588 struct rtw_txdesc_blk *tdb;
1589
1590 rtw_txdesc_blk_init_all(&sc->sc_txdesc_blk[0]);
1591
1592 rtw_txsoft_blk_init_all(&sc->sc_txsoft_blk[0]);
1593
1594 rdb = &sc->sc_rxdesc_blk;
1595 if ((rc = rtw_rxsoft_init_all(sc->sc_dmat, sc->sc_rxsoft,
1596 &rdb->rdb_ndesc, sc->sc_dev.dv_xname)) != 0 &&
1597 rdb->rdb_ndesc == 0) {
1598 printf("%s: could not allocate rx buffers\n",
1599 sc->sc_dev.dv_xname);
1600 return rc;
1601 }
1602
1603 rdb = &sc->sc_rxdesc_blk;
1604 rtw_rxdescs_sync(rdb, 0, rdb->rdb_ndesc,
1605 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1606 rtw_rxdesc_init_all(rdb, sc->sc_rxsoft, 1);
1607 rdb->rdb_next = 0;
1608
1609 tdb = &sc->sc_txdesc_blk[0];
1610 for (pri = 0; pri < RTW_NTXPRI; pri++) {
1611 rtw_txdescs_sync(&tdb[pri], 0, tdb[pri].tdb_ndesc,
1612 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1613 }
1614 return 0;
1615 }
1616
1617 void
rtw_txdesc_blk_init(struct rtw_txdesc_blk * tdb)1618 rtw_txdesc_blk_init(struct rtw_txdesc_blk *tdb)
1619 {
1620 int i;
1621
1622 bzero(tdb->tdb_desc, sizeof(tdb->tdb_desc[0]) * tdb->tdb_ndesc);
1623 for (i = 0; i < tdb->tdb_ndesc; i++)
1624 tdb->tdb_desc[i].td_next = htole32(RTW_NEXT_DESC(tdb, i));
1625 }
1626
1627 u_int
rtw_txring_next(struct rtw_regs * regs,struct rtw_txdesc_blk * tdb)1628 rtw_txring_next(struct rtw_regs *regs, struct rtw_txdesc_blk *tdb)
1629 {
1630 return (letoh32(RTW_READ(regs, tdb->tdb_basereg)) - tdb->tdb_base) /
1631 sizeof(struct rtw_txdesc);
1632 }
1633
1634 void
rtw_txring_fixup(struct rtw_softc * sc)1635 rtw_txring_fixup(struct rtw_softc *sc)
1636 {
1637 int pri;
1638 u_int next;
1639 struct rtw_txdesc_blk *tdb;
1640 struct rtw_regs *regs = &sc->sc_regs;
1641
1642 for (pri = 0; pri < RTW_NTXPRI; pri++) {
1643 tdb = &sc->sc_txdesc_blk[pri];
1644 next = rtw_txring_next(regs, tdb);
1645 if (tdb->tdb_next == next)
1646 continue;
1647 RTW_DPRINTF(RTW_DEBUG_BUGS,
1648 ("%s: tx-ring %d expected next %u, read %u\n", __func__,
1649 pri, tdb->tdb_next, next));
1650 tdb->tdb_next = MIN(next, tdb->tdb_ndesc - 1);
1651 }
1652 }
1653
1654 void
rtw_rxring_fixup(struct rtw_softc * sc)1655 rtw_rxring_fixup(struct rtw_softc *sc)
1656 {
1657 u_int next;
1658 uint32_t rdsar;
1659 struct rtw_rxdesc_blk *rdb;
1660
1661 rdsar = letoh32(RTW_READ(&sc->sc_regs, RTW_RDSAR));
1662 next = (rdsar - RTW_RING_BASE(sc, hd_rx)) / sizeof(struct rtw_rxdesc);
1663
1664 rdb = &sc->sc_rxdesc_blk;
1665 if (rdb->rdb_next != next) {
1666 RTW_DPRINTF(RTW_DEBUG_BUGS,
1667 ("%s: rx-ring expected next %u, read %u\n", __func__,
1668 rdb->rdb_next, next));
1669 rdb->rdb_next = MIN(next, rdb->rdb_ndesc - 1);
1670 }
1671 }
1672
1673 void
rtw_txdescs_reset(struct rtw_softc * sc)1674 rtw_txdescs_reset(struct rtw_softc *sc)
1675 {
1676 int pri;
1677
1678 for (pri = 0; pri < RTW_NTXPRI; pri++) {
1679 rtw_collect_txring(sc, &sc->sc_txsoft_blk[pri],
1680 &sc->sc_txdesc_blk[pri], 1);
1681 }
1682 }
1683
1684 void
rtw_intr_ioerror(struct rtw_softc * sc,u_int16_t isr)1685 rtw_intr_ioerror(struct rtw_softc *sc, u_int16_t isr)
1686 {
1687 uint8_t cr = 0;
1688 int xmtr = 0, rcvr = 0;
1689 struct rtw_regs *regs = &sc->sc_regs;
1690
1691 if ((isr & RTW_INTR_TXFOVW) != 0) {
1692 RTW_DPRINTF(RTW_DEBUG_BUGS,
1693 ("%s: tx fifo underflow\n", sc->sc_dev.dv_xname));
1694 rcvr = xmtr = 1;
1695 cr |= RTW_CR_TE | RTW_CR_RE;
1696 }
1697
1698 if ((isr & (RTW_INTR_RDU|RTW_INTR_RXFOVW)) != 0) {
1699 cr |= RTW_CR_RE;
1700 rcvr = 1;
1701 }
1702
1703 RTW_DPRINTF(RTW_DEBUG_BUGS, ("%s: restarting xmit/recv, isr %hx"
1704 "\n", sc->sc_dev.dv_xname, isr));
1705
1706 #ifdef RTW_DEBUG
1707 rtw_dump_rings(sc);
1708 #endif /* RTW_DEBUG */
1709
1710 rtw_io_enable(regs, cr, 0);
1711
1712 /* Collect rx'd packets. Refresh rx buffers. */
1713 if (rcvr)
1714 rtw_intr_rx(sc, 0);
1715 /* Collect tx'd packets. XXX let's hope this stops the transmit
1716 * timeouts.
1717 */
1718 if (xmtr)
1719 rtw_txdescs_reset(sc);
1720
1721 RTW_WRITE16(regs, RTW_IMR, 0);
1722 RTW_SYNC(regs, RTW_IMR, RTW_IMR);
1723
1724 if (rtw_do_chip_reset) {
1725 rtw_chip_reset1(regs, sc->sc_dev.dv_xname);
1726 }
1727
1728 rtw_rxdesc_init_all(&sc->sc_rxdesc_blk, &sc->sc_rxsoft[0], 1);
1729
1730 #ifdef RTW_DEBUG
1731 rtw_dump_rings(sc);
1732 #endif /* RTW_DEBUG */
1733
1734 RTW_WRITE16(regs, RTW_IMR, sc->sc_inten);
1735 RTW_SYNC(regs, RTW_IMR, RTW_IMR);
1736 if (rcvr)
1737 rtw_rxring_fixup(sc);
1738 rtw_io_enable(regs, cr, 1);
1739 if (xmtr)
1740 rtw_txring_fixup(sc);
1741 }
1742
1743 void
rtw_suspend_ticks(struct rtw_softc * sc)1744 rtw_suspend_ticks(struct rtw_softc *sc)
1745 {
1746 RTW_DPRINTF(RTW_DEBUG_TIMEOUT,
1747 ("%s: suspending ticks\n", sc->sc_dev.dv_xname));
1748 sc->sc_do_tick = 0;
1749 }
1750
1751 void
rtw_resume_ticks(struct rtw_softc * sc)1752 rtw_resume_ticks(struct rtw_softc *sc)
1753 {
1754 u_int32_t tsftrl0, tsftrl1, next_tick;
1755
1756 tsftrl0 = RTW_READ(&sc->sc_regs, RTW_TSFTRL);
1757
1758 tsftrl1 = RTW_READ(&sc->sc_regs, RTW_TSFTRL);
1759 next_tick = tsftrl1 + 1000000;
1760 RTW_WRITE(&sc->sc_regs, RTW_TINT, next_tick);
1761
1762 sc->sc_do_tick = 1;
1763
1764 RTW_DPRINTF(RTW_DEBUG_TIMEOUT,
1765 ("%s: resume ticks delta %#08x now %#08x next %#08x\n",
1766 sc->sc_dev.dv_xname, tsftrl1 - tsftrl0, tsftrl1, next_tick));
1767 }
1768
1769 void
rtw_intr_timeout(struct rtw_softc * sc)1770 rtw_intr_timeout(struct rtw_softc *sc)
1771 {
1772 RTW_DPRINTF(RTW_DEBUG_TIMEOUT, ("%s: timeout\n", sc->sc_dev.dv_xname));
1773 if (sc->sc_do_tick)
1774 rtw_resume_ticks(sc);
1775 return;
1776 }
1777
1778 int
rtw_intr(void * arg)1779 rtw_intr(void *arg)
1780 {
1781 int i;
1782 struct rtw_softc *sc = arg;
1783 struct rtw_regs *regs = &sc->sc_regs;
1784 u_int16_t isr;
1785
1786 /*
1787 * If the interface isn't running, the interrupt couldn't
1788 * possibly have come from us.
1789 */
1790 if ((sc->sc_flags & RTW_F_ENABLED) == 0 ||
1791 (sc->sc_if.if_flags & IFF_RUNNING) == 0 ||
1792 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
1793 RTW_DPRINTF(RTW_DEBUG_INTR, ("%s: stray interrupt\n",
1794 sc->sc_dev.dv_xname));
1795 return (0);
1796 }
1797
1798 for (i = 0; i < 10; i++) {
1799 isr = RTW_READ16(regs, RTW_ISR);
1800
1801 RTW_WRITE16(regs, RTW_ISR, isr);
1802 RTW_WBR(regs, RTW_ISR, RTW_ISR);
1803
1804 if (sc->sc_intr_ack != NULL)
1805 (*sc->sc_intr_ack)(regs);
1806
1807 if (isr == 0)
1808 break;
1809
1810 #ifdef RTW_DEBUG
1811 #define PRINTINTR(flag) do { \
1812 if ((isr & flag) != 0) { \
1813 printf("%s" #flag, delim); \
1814 delim = ","; \
1815 } \
1816 } while (0)
1817
1818 if ((rtw_debug & RTW_DEBUG_INTR) != 0 && isr != 0) {
1819 const char *delim = "<";
1820
1821 printf("%s: reg[ISR] = %x", sc->sc_dev.dv_xname, isr);
1822
1823 PRINTINTR(RTW_INTR_TXFOVW);
1824 PRINTINTR(RTW_INTR_TIMEOUT);
1825 PRINTINTR(RTW_INTR_BCNINT);
1826 PRINTINTR(RTW_INTR_ATIMINT);
1827 PRINTINTR(RTW_INTR_TBDER);
1828 PRINTINTR(RTW_INTR_TBDOK);
1829 PRINTINTR(RTW_INTR_THPDER);
1830 PRINTINTR(RTW_INTR_THPDOK);
1831 PRINTINTR(RTW_INTR_TNPDER);
1832 PRINTINTR(RTW_INTR_TNPDOK);
1833 PRINTINTR(RTW_INTR_RXFOVW);
1834 PRINTINTR(RTW_INTR_RDU);
1835 PRINTINTR(RTW_INTR_TLPDER);
1836 PRINTINTR(RTW_INTR_TLPDOK);
1837 PRINTINTR(RTW_INTR_RER);
1838 PRINTINTR(RTW_INTR_ROK);
1839
1840 printf(">\n");
1841 }
1842 #undef PRINTINTR
1843 #endif /* RTW_DEBUG */
1844
1845 if ((isr & RTW_INTR_RX) != 0)
1846 rtw_intr_rx(sc, isr & RTW_INTR_RX);
1847 if ((isr & RTW_INTR_TX) != 0)
1848 rtw_intr_tx(sc, isr & RTW_INTR_TX);
1849 #ifndef IEEE80211_STA_ONLY
1850 if ((isr & RTW_INTR_BEACON) != 0)
1851 rtw_intr_beacon(sc, isr & RTW_INTR_BEACON);
1852 if ((isr & RTW_INTR_ATIMINT) != 0)
1853 rtw_intr_atim(sc);
1854 #endif
1855 if ((isr & RTW_INTR_IOERROR) != 0)
1856 rtw_intr_ioerror(sc, isr & RTW_INTR_IOERROR);
1857 if ((isr & RTW_INTR_TIMEOUT) != 0)
1858 rtw_intr_timeout(sc);
1859 }
1860
1861 return 1;
1862 }
1863
1864 /* Must be called at splnet. */
1865 void
rtw_stop(struct ifnet * ifp,int disable)1866 rtw_stop(struct ifnet *ifp, int disable)
1867 {
1868 int pri;
1869 struct rtw_softc *sc = (struct rtw_softc *)ifp->if_softc;
1870 struct ieee80211com *ic = &sc->sc_ic;
1871 struct rtw_regs *regs = &sc->sc_regs;
1872
1873 if ((sc->sc_flags & RTW_F_ENABLED) == 0)
1874 return;
1875
1876 rtw_suspend_ticks(sc);
1877
1878 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1879
1880 if ((sc->sc_flags & RTW_F_INVALID) == 0) {
1881 /* Disable interrupts. */
1882 RTW_WRITE16(regs, RTW_IMR, 0);
1883
1884 RTW_WBW(regs, RTW_TPPOLL, RTW_IMR);
1885
1886 /* Stop the transmit and receive processes. First stop DMA,
1887 * then disable receiver and transmitter.
1888 */
1889 RTW_WRITE8(regs, RTW_TPPOLL, RTW_TPPOLL_SALL);
1890
1891 RTW_SYNC(regs, RTW_TPPOLL, RTW_IMR);
1892
1893 rtw_io_enable(&sc->sc_regs, RTW_CR_RE|RTW_CR_TE, 0);
1894 }
1895
1896 for (pri = 0; pri < RTW_NTXPRI; pri++) {
1897 rtw_txsofts_release(sc->sc_dmat, &sc->sc_ic,
1898 &sc->sc_txsoft_blk[pri]);
1899 }
1900
1901 rtw_rxbufs_release(sc->sc_dmat, &sc->sc_rxsoft[0]);
1902
1903 if (disable)
1904 rtw_disable(sc);
1905
1906 /* Mark the interface as not running. Cancel the watchdog timer. */
1907 ifp->if_flags &= ~IFF_RUNNING;
1908 ifq_clr_oactive(&ifp->if_snd);
1909 ifp->if_timer = 0;
1910
1911 return;
1912 }
1913
1914 #ifdef RTW_DEBUG
1915 const char *
rtw_pwrstate_string(enum rtw_pwrstate power)1916 rtw_pwrstate_string(enum rtw_pwrstate power)
1917 {
1918 switch (power) {
1919 case RTW_ON:
1920 return "on";
1921 case RTW_SLEEP:
1922 return "sleep";
1923 case RTW_OFF:
1924 return "off";
1925 default:
1926 return "unknown";
1927 }
1928 }
1929 #endif
1930
1931 /* XXX For Maxim, I am using the RFMD settings gleaned from the
1932 * reference driver, plus a magic Maxim "ON" value that comes from
1933 * the Realtek document "Windows PG for Rtl8180."
1934 */
1935 void
rtw_maxim_pwrstate(struct rtw_regs * regs,enum rtw_pwrstate power,int before_rf,int digphy)1936 rtw_maxim_pwrstate(struct rtw_regs *regs, enum rtw_pwrstate power,
1937 int before_rf, int digphy)
1938 {
1939 u_int32_t anaparm;
1940
1941 anaparm = RTW_READ(regs, RTW_ANAPARM_0);
1942 anaparm &= ~(RTW_ANAPARM_RFPOW_MASK | RTW_ANAPARM_TXDACOFF);
1943
1944 switch (power) {
1945 case RTW_OFF:
1946 if (before_rf)
1947 return;
1948 anaparm |= RTW_ANAPARM_RFPOW_MAXIM_OFF;
1949 anaparm |= RTW_ANAPARM_TXDACOFF;
1950 break;
1951 case RTW_SLEEP:
1952 if (!before_rf)
1953 return;
1954 anaparm |= RTW_ANAPARM_RFPOW_MAXIM_SLEEP;
1955 anaparm |= RTW_ANAPARM_TXDACOFF;
1956 break;
1957 case RTW_ON:
1958 if (!before_rf)
1959 return;
1960 anaparm |= RTW_ANAPARM_RFPOW_MAXIM_ON;
1961 break;
1962 }
1963 RTW_DPRINTF(RTW_DEBUG_PWR,
1964 ("%s: power state %s, %s RF, reg[ANAPARM] <- %08x\n",
1965 __func__, rtw_pwrstate_string(power),
1966 (before_rf) ? "before" : "after", anaparm));
1967
1968 RTW_WRITE(regs, RTW_ANAPARM_0, anaparm);
1969 RTW_SYNC(regs, RTW_ANAPARM_0, RTW_ANAPARM_0);
1970 }
1971
1972 /* XXX I am using the RFMD settings gleaned from the reference
1973 * driver. They agree
1974 */
1975 void
rtw_rfmd_pwrstate(struct rtw_regs * regs,enum rtw_pwrstate power,int before_rf,int digphy)1976 rtw_rfmd_pwrstate(struct rtw_regs *regs, enum rtw_pwrstate power,
1977 int before_rf, int digphy)
1978 {
1979 u_int32_t anaparm;
1980
1981 anaparm = RTW_READ(regs, RTW_ANAPARM_0);
1982 anaparm &= ~(RTW_ANAPARM_RFPOW_MASK | RTW_ANAPARM_TXDACOFF);
1983
1984 switch (power) {
1985 case RTW_OFF:
1986 if (before_rf)
1987 return;
1988 anaparm |= RTW_ANAPARM_RFPOW_RFMD_OFF;
1989 anaparm |= RTW_ANAPARM_TXDACOFF;
1990 break;
1991 case RTW_SLEEP:
1992 if (!before_rf)
1993 return;
1994 anaparm |= RTW_ANAPARM_RFPOW_RFMD_SLEEP;
1995 anaparm |= RTW_ANAPARM_TXDACOFF;
1996 break;
1997 case RTW_ON:
1998 if (!before_rf)
1999 return;
2000 anaparm |= RTW_ANAPARM_RFPOW_RFMD_ON;
2001 break;
2002 }
2003 RTW_DPRINTF(RTW_DEBUG_PWR,
2004 ("%s: power state %s, %s RF, reg[ANAPARM] <- %08x\n",
2005 __func__, rtw_pwrstate_string(power),
2006 (before_rf) ? "before" : "after", anaparm));
2007
2008 RTW_WRITE(regs, RTW_ANAPARM_0, anaparm);
2009 RTW_SYNC(regs, RTW_ANAPARM_0, RTW_ANAPARM_0);
2010 }
2011
2012 void
rtw_philips_pwrstate(struct rtw_regs * regs,enum rtw_pwrstate power,int before_rf,int digphy)2013 rtw_philips_pwrstate(struct rtw_regs *regs, enum rtw_pwrstate power,
2014 int before_rf, int digphy)
2015 {
2016 u_int32_t anaparm;
2017
2018 anaparm = RTW_READ(regs, RTW_ANAPARM_0);
2019 anaparm &= ~(RTW_ANAPARM_RFPOW_MASK | RTW_ANAPARM_TXDACOFF);
2020
2021 switch (power) {
2022 case RTW_OFF:
2023 if (before_rf)
2024 return;
2025 anaparm |= RTW_ANAPARM_RFPOW_PHILIPS_OFF;
2026 anaparm |= RTW_ANAPARM_TXDACOFF;
2027 break;
2028 case RTW_SLEEP:
2029 if (!before_rf)
2030 return;
2031 anaparm |= RTW_ANAPARM_RFPOW_PHILIPS_SLEEP;
2032 anaparm |= RTW_ANAPARM_TXDACOFF;
2033 break;
2034 case RTW_ON:
2035 if (!before_rf)
2036 return;
2037 if (digphy) {
2038 anaparm |= RTW_ANAPARM_RFPOW_DIG_PHILIPS_ON;
2039 /* XXX guess */
2040 anaparm |= RTW_ANAPARM_TXDACOFF;
2041 } else
2042 anaparm |= RTW_ANAPARM_RFPOW_ANA_PHILIPS_ON;
2043 break;
2044 }
2045 RTW_DPRINTF(RTW_DEBUG_PWR,
2046 ("%s: power state %s, %s RF, reg[ANAPARM] <- %08x\n",
2047 __func__, rtw_pwrstate_string(power),
2048 (before_rf) ? "before" : "after", anaparm));
2049
2050 RTW_WRITE(regs, RTW_ANAPARM_0, anaparm);
2051 RTW_SYNC(regs, RTW_ANAPARM_0, RTW_ANAPARM_0);
2052 }
2053
2054 void
rtw_rtl_pwrstate(struct rtw_regs * regs,enum rtw_pwrstate power,int before_rf,int digphy)2055 rtw_rtl_pwrstate(struct rtw_regs *regs, enum rtw_pwrstate power,
2056 int before_rf, int digphy)
2057 {
2058 /* empty */
2059 }
2060
2061 void
rtw_pwrstate0(struct rtw_softc * sc,enum rtw_pwrstate power,int before_rf,int digphy)2062 rtw_pwrstate0(struct rtw_softc *sc, enum rtw_pwrstate power, int before_rf,
2063 int digphy)
2064 {
2065 struct rtw_regs *regs = &sc->sc_regs;
2066
2067 rtw_set_access(regs, RTW_ACCESS_ANAPARM);
2068
2069 (*sc->sc_pwrstate_cb)(regs, power, before_rf, digphy);
2070
2071 rtw_set_access(regs, RTW_ACCESS_NONE);
2072
2073 return;
2074 }
2075
2076 int
rtw_pwrstate(struct rtw_softc * sc,enum rtw_pwrstate power)2077 rtw_pwrstate(struct rtw_softc *sc, enum rtw_pwrstate power)
2078 {
2079 int rc;
2080
2081 RTW_DPRINTF(RTW_DEBUG_PWR,
2082 ("%s: %s->%s\n", __func__,
2083 rtw_pwrstate_string(sc->sc_pwrstate), rtw_pwrstate_string(power)));
2084
2085 if (sc->sc_pwrstate == power)
2086 return 0;
2087
2088 rtw_pwrstate0(sc, power, 1, sc->sc_flags & RTW_F_DIGPHY);
2089 rc = (*sc->sc_rf_pwrstate)(sc, power);
2090 rtw_pwrstate0(sc, power, 0, sc->sc_flags & RTW_F_DIGPHY);
2091
2092 switch (power) {
2093 case RTW_ON:
2094 /* TBD set LEDs */
2095 break;
2096 case RTW_SLEEP:
2097 /* TBD */
2098 break;
2099 case RTW_OFF:
2100 /* TBD */
2101 break;
2102 }
2103 if (rc == 0)
2104 sc->sc_pwrstate = power;
2105 else
2106 sc->sc_pwrstate = RTW_OFF;
2107 return rc;
2108 }
2109
2110 int
rtw_tune(struct rtw_softc * sc)2111 rtw_tune(struct rtw_softc *sc)
2112 {
2113 struct ieee80211com *ic = &sc->sc_ic;
2114 u_int chan, idx;
2115 u_int8_t txpower;
2116 int rc;
2117
2118 KASSERT(ic->ic_bss->ni_chan != NULL);
2119
2120 chan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
2121 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2122 return 0;
2123
2124 if (chan == sc->sc_cur_chan) {
2125 RTW_DPRINTF(RTW_DEBUG_TUNE,
2126 ("%s: already tuned chan #%d\n", __func__, chan));
2127 return 0;
2128 }
2129
2130 rtw_suspend_ticks(sc);
2131
2132 rtw_io_enable(&sc->sc_regs, RTW_CR_RE | RTW_CR_TE, 0);
2133
2134 /* TBD wait for Tx to complete */
2135
2136 KASSERT((sc->sc_flags & RTW_F_ENABLED) != 0);
2137
2138 idx = RTW_SR_TXPOWER1 +
2139 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan) - 1;
2140 KASSERT2(idx >= RTW_SR_TXPOWER1 && idx <= RTW_SR_TXPOWER14,
2141 ("%s: channel %d out of range", __func__,
2142 idx - RTW_SR_TXPOWER1 + 1));
2143 txpower = RTW_SR_GET(&sc->sc_srom, idx);
2144
2145 if ((rc = rtw_phy_init(sc)) != 0) {
2146 /* XXX condition on powersaving */
2147 printf("%s: phy init failed\n", sc->sc_dev.dv_xname);
2148 }
2149
2150 sc->sc_cur_chan = chan;
2151
2152 rtw_io_enable(&sc->sc_regs, RTW_CR_RE | RTW_CR_TE, 1);
2153
2154 rtw_resume_ticks(sc);
2155
2156 return rc;
2157 }
2158
2159 void
rtw_disable(struct rtw_softc * sc)2160 rtw_disable(struct rtw_softc *sc)
2161 {
2162 int rc;
2163
2164 if ((sc->sc_flags & RTW_F_ENABLED) == 0)
2165 return;
2166
2167 /* turn off PHY */
2168 if ((sc->sc_flags & RTW_F_INVALID) == 0 &&
2169 (rc = rtw_pwrstate(sc, RTW_OFF)) != 0) {
2170 printf("%s: failed to turn off PHY (%d)\n",
2171 sc->sc_dev.dv_xname, rc);
2172 }
2173
2174 if (sc->sc_disable != NULL)
2175 (*sc->sc_disable)(sc);
2176
2177 sc->sc_flags &= ~RTW_F_ENABLED;
2178 }
2179
2180 int
rtw_enable(struct rtw_softc * sc)2181 rtw_enable(struct rtw_softc *sc)
2182 {
2183 if ((sc->sc_flags & RTW_F_ENABLED) == 0) {
2184 if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
2185 printf("%s: device enable failed\n",
2186 sc->sc_dev.dv_xname);
2187 return (EIO);
2188 }
2189 sc->sc_flags |= RTW_F_ENABLED;
2190 }
2191 return (0);
2192 }
2193
2194 void
rtw_transmit_config(struct rtw_softc * sc)2195 rtw_transmit_config(struct rtw_softc *sc)
2196 {
2197 struct rtw_regs *regs = &sc->sc_regs;
2198 u_int32_t tcr;
2199
2200 tcr = RTW_READ(regs, RTW_TCR);
2201
2202 tcr |= RTW_TCR_CWMIN;
2203 tcr &= ~RTW_TCR_MXDMA_MASK;
2204 tcr |= RTW_TCR_MXDMA_256;
2205 if ((sc->sc_flags & RTW_F_RTL8185) == 0)
2206 tcr |= RTW8180_TCR_SAT; /* send ACK as fast as possible */
2207 tcr &= ~RTW_TCR_LBK_MASK;
2208 tcr |= RTW_TCR_LBK_NORMAL; /* normal operating mode */
2209
2210 /* set short/long retry limits */
2211 tcr &= ~(RTW_TCR_SRL_MASK|RTW_TCR_LRL_MASK);
2212 tcr |= LSHIFT(4, RTW_TCR_SRL_MASK) | LSHIFT(4, RTW_TCR_LRL_MASK);
2213
2214 tcr &= ~RTW_TCR_CRC; /* NIC appends CRC32 */
2215
2216 RTW_WRITE(regs, RTW_TCR, tcr);
2217 RTW_SYNC(regs, RTW_TCR, RTW_TCR);
2218 }
2219
2220 void
rtw_enable_interrupts(struct rtw_softc * sc)2221 rtw_enable_interrupts(struct rtw_softc *sc)
2222 {
2223 struct rtw_regs *regs = &sc->sc_regs;
2224
2225 sc->sc_inten = RTW_INTR_RX|RTW_INTR_TX|RTW_INTR_BEACON|RTW_INTR_ATIMINT;
2226 sc->sc_inten |= RTW_INTR_IOERROR|RTW_INTR_TIMEOUT;
2227
2228 RTW_WRITE16(regs, RTW_IMR, sc->sc_inten);
2229 RTW_WBW(regs, RTW_IMR, RTW_ISR);
2230 RTW_WRITE16(regs, RTW_ISR, 0xffff);
2231 RTW_SYNC(regs, RTW_IMR, RTW_ISR);
2232
2233 /* XXX necessary? */
2234 if (sc->sc_intr_ack != NULL)
2235 (*sc->sc_intr_ack)(regs);
2236 }
2237
2238 void
rtw_set_nettype(struct rtw_softc * sc,enum ieee80211_opmode opmode)2239 rtw_set_nettype(struct rtw_softc *sc, enum ieee80211_opmode opmode)
2240 {
2241 uint8_t msr;
2242
2243 /* I'm guessing that MSR is protected as CONFIG[0123] are. */
2244 rtw_set_access(&sc->sc_regs, RTW_ACCESS_CONFIG);
2245
2246 msr = RTW_READ8(&sc->sc_regs, RTW_MSR) & ~RTW_MSR_NETYPE_MASK;
2247
2248 switch (opmode) {
2249 #ifndef IEEE80211_STA_ONLY
2250 case IEEE80211_M_AHDEMO:
2251 case IEEE80211_M_IBSS:
2252 msr |= RTW_MSR_NETYPE_ADHOC_OK;
2253 break;
2254 case IEEE80211_M_HOSTAP:
2255 msr |= RTW_MSR_NETYPE_AP_OK;
2256 break;
2257 #endif
2258 case IEEE80211_M_MONITOR:
2259 /* XXX */
2260 msr |= RTW_MSR_NETYPE_NOLINK;
2261 break;
2262 case IEEE80211_M_STA:
2263 msr |= RTW_MSR_NETYPE_INFRA_OK;
2264 break;
2265 default:
2266 break;
2267 }
2268 RTW_WRITE8(&sc->sc_regs, RTW_MSR, msr);
2269
2270 rtw_set_access(&sc->sc_regs, RTW_ACCESS_NONE);
2271 }
2272
2273 void
rtw_pktfilt_load(struct rtw_softc * sc)2274 rtw_pktfilt_load(struct rtw_softc *sc)
2275 {
2276 struct rtw_regs *regs = &sc->sc_regs;
2277 struct ieee80211com *ic = &sc->sc_ic;
2278 struct arpcom *ac = &ic->ic_ac;
2279 struct ifnet *ifp = &sc->sc_ic.ic_if;
2280 int hash;
2281 u_int32_t hashes[2] = { 0, 0 };
2282 struct ether_multi *enm;
2283 struct ether_multistep step;
2284
2285 /* XXX might be necessary to stop Rx/Tx engines while setting filters */
2286
2287 sc->sc_rcr &= ~RTW_RCR_PKTFILTER_MASK;
2288 sc->sc_rcr &= ~(RTW_RCR_MXDMA_MASK | RTW8180_RCR_RXFTH_MASK);
2289
2290 sc->sc_rcr |= RTW_RCR_PKTFILTER_DEFAULT;
2291 /* MAC auto-reset PHY (huh?) */
2292 sc->sc_rcr |= RTW_RCR_ENMARP;
2293 /* DMA whole Rx packets, only. Set Tx DMA burst size to 1024 bytes. */
2294 sc->sc_rcr |= RTW_RCR_MXDMA_1024 | RTW8180_RCR_RXFTH_WHOLE;
2295
2296 switch (ic->ic_opmode) {
2297 case IEEE80211_M_MONITOR:
2298 sc->sc_rcr |= RTW_RCR_MONITOR;
2299 break;
2300 #ifndef IEEE80211_STA_ONLY
2301 case IEEE80211_M_AHDEMO:
2302 case IEEE80211_M_IBSS:
2303 /* receive broadcasts in our BSS */
2304 sc->sc_rcr |= RTW_RCR_ADD3;
2305 break;
2306 #endif
2307 default:
2308 break;
2309 }
2310
2311 ifp->if_flags &= ~IFF_ALLMULTI;
2312
2313 /* XXX accept all broadcast if scanning */
2314 if ((ifp->if_flags & IFF_BROADCAST) != 0)
2315 sc->sc_rcr |= RTW_RCR_AB; /* accept all broadcast */
2316
2317 if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0) {
2318 if (ifp->if_flags & IFF_PROMISC)
2319 sc->sc_rcr |= RTW_RCR_AB; /* accept all broadcast */
2320 allmulti:
2321 ifp->if_flags |= IFF_ALLMULTI;
2322 goto setit;
2323 }
2324
2325 /*
2326 * Program the 64-bit multicast hash filter.
2327 */
2328 ETHER_FIRST_MULTI(step, ac, enm);
2329 while (enm != NULL) {
2330 hash = ether_crc32_be((enm->enm_addrlo),
2331 IEEE80211_ADDR_LEN) >> 26;
2332 hashes[hash >> 5] |= (1 << (hash & 0x1f));
2333 sc->sc_rcr |= RTW_RCR_AM;
2334 ETHER_NEXT_MULTI(step, enm);
2335 }
2336
2337 /* all bits set => hash is useless */
2338 if (~(hashes[0] & hashes[1]) == 0)
2339 goto allmulti;
2340
2341 setit:
2342 if (ifp->if_flags & IFF_ALLMULTI) {
2343 sc->sc_rcr |= RTW_RCR_AM; /* accept all multicast */
2344 hashes[0] = hashes[1] = 0xffffffff;
2345 }
2346
2347 RTW_WRITE(regs, RTW_MAR0, hashes[0]);
2348 RTW_WRITE(regs, RTW_MAR1, hashes[1]);
2349 RTW_WRITE(regs, RTW_RCR, sc->sc_rcr);
2350 RTW_SYNC(regs, RTW_MAR0, RTW_RCR); /* RTW_MAR0 < RTW_MAR1 < RTW_RCR */
2351
2352 DPRINTF(sc, RTW_DEBUG_PKTFILT,
2353 ("%s: RTW_MAR0 %08x RTW_MAR1 %08x RTW_RCR %08x\n",
2354 sc->sc_dev.dv_xname, RTW_READ(regs, RTW_MAR0),
2355 RTW_READ(regs, RTW_MAR1), RTW_READ(regs, RTW_RCR)));
2356
2357 return;
2358 }
2359
2360 /* Must be called at splnet. */
2361 int
rtw_init(struct ifnet * ifp)2362 rtw_init(struct ifnet *ifp)
2363 {
2364 struct rtw_softc *sc = (struct rtw_softc *)ifp->if_softc;
2365 struct ieee80211com *ic = &sc->sc_ic;
2366 struct rtw_regs *regs = &sc->sc_regs;
2367 int rc = 0;
2368
2369 if ((rc = rtw_enable(sc)) != 0)
2370 goto out;
2371
2372 /* Cancel pending I/O and reset. */
2373 rtw_stop(ifp, 0);
2374
2375 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2376 DPRINTF(sc, RTW_DEBUG_TUNE, ("%s: channel %d freq %d flags 0x%04x\n",
2377 __func__, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
2378 ic->ic_bss->ni_chan->ic_freq, ic->ic_bss->ni_chan->ic_flags));
2379
2380 if ((rc = rtw_pwrstate(sc, RTW_OFF)) != 0)
2381 goto out;
2382
2383 if ((rc = rtw_swring_setup(sc)) != 0)
2384 goto out;
2385
2386 rtw_transmit_config(sc);
2387
2388 rtw_set_access(regs, RTW_ACCESS_CONFIG);
2389
2390 RTW_WRITE8(regs, RTW_MSR, 0x0); /* no link */
2391 RTW_WBW(regs, RTW_MSR, RTW_BRSR);
2392
2393 /* long PLCP header, 1Mb/2Mb basic rate */
2394 if (sc->sc_flags & RTW_F_RTL8185)
2395 RTW_WRITE16(regs, RTW_BRSR, RTW8185_BRSR_MBR_2MBPS);
2396 else
2397 RTW_WRITE16(regs, RTW_BRSR, RTW8180_BRSR_MBR_2MBPS);
2398 RTW_SYNC(regs, RTW_BRSR, RTW_BRSR);
2399
2400 rtw_set_access(regs, RTW_ACCESS_ANAPARM);
2401 rtw_set_access(regs, RTW_ACCESS_NONE);
2402
2403 /* XXX from reference sources */
2404 RTW_WRITE(regs, RTW_FEMR, 0xffff);
2405 RTW_SYNC(regs, RTW_FEMR, RTW_FEMR);
2406
2407 rtw_set_rfprog(regs, sc->sc_rfchipid, sc->sc_dev.dv_xname);
2408
2409 RTW_WRITE8(regs, RTW_PHYDELAY, sc->sc_phydelay);
2410 /* from Linux driver */
2411 RTW_WRITE8(regs, RTW_CRCOUNT, RTW_CRCOUNT_MAGIC);
2412
2413 RTW_SYNC(regs, RTW_PHYDELAY, RTW_CRCOUNT);
2414
2415 rtw_enable_interrupts(sc);
2416
2417 rtw_pktfilt_load(sc);
2418
2419 rtw_hwring_setup(sc);
2420
2421 rtw_io_enable(regs, RTW_CR_RE|RTW_CR_TE, 1);
2422
2423 ifp->if_flags |= IFF_RUNNING;
2424 ic->ic_state = IEEE80211_S_INIT;
2425
2426 RTW_WRITE16(regs, RTW_BSSID16, 0x0);
2427 RTW_WRITE(regs, RTW_BSSID32, 0x0);
2428
2429 rtw_resume_ticks(sc);
2430
2431 rtw_set_nettype(sc, IEEE80211_M_MONITOR);
2432
2433 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2434 return ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2435 else
2436 return ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2437
2438 out:
2439 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
2440 return rc;
2441 }
2442
2443 void
rtw_led_init(struct rtw_regs * regs)2444 rtw_led_init(struct rtw_regs *regs)
2445 {
2446 u_int8_t cfg0, cfg1;
2447
2448 rtw_set_access(regs, RTW_ACCESS_CONFIG);
2449
2450 cfg0 = RTW_READ8(regs, RTW_CONFIG0);
2451 cfg0 |= RTW8180_CONFIG0_LEDGPOEN;
2452 RTW_WRITE8(regs, RTW_CONFIG0, cfg0);
2453
2454 cfg1 = RTW_READ8(regs, RTW_CONFIG1);
2455 RTW_DPRINTF(RTW_DEBUG_LED,
2456 ("%s: read % from reg[CONFIG1]\n", __func__, cfg1));
2457
2458 cfg1 &= ~RTW_CONFIG1_LEDS_MASK;
2459 cfg1 |= RTW_CONFIG1_LEDS_TX_RX;
2460 RTW_WRITE8(regs, RTW_CONFIG1, cfg1);
2461
2462 rtw_set_access(regs, RTW_ACCESS_NONE);
2463 }
2464
2465 /*
2466 * IEEE80211_S_INIT: LED1 off
2467 *
2468 * IEEE80211_S_AUTH,
2469 * IEEE80211_S_ASSOC,
2470 * IEEE80211_S_SCAN: LED1 blinks @ 1 Hz, blinks at 5Hz for tx/rx
2471 *
2472 * IEEE80211_S_RUN: LED1 on, blinks @ 5Hz for tx/rx
2473 */
2474 void
rtw_led_newstate(struct rtw_softc * sc,enum ieee80211_state nstate)2475 rtw_led_newstate(struct rtw_softc *sc, enum ieee80211_state nstate)
2476 {
2477 struct rtw_led_state *ls;
2478
2479 ls = &sc->sc_led_state;
2480
2481 switch (nstate) {
2482 case IEEE80211_S_INIT:
2483 rtw_led_init(&sc->sc_regs);
2484 timeout_del(&ls->ls_slow_ch);
2485 timeout_del(&ls->ls_fast_ch);
2486 ls->ls_slowblink = 0;
2487 ls->ls_actblink = 0;
2488 ls->ls_default = 0;
2489 break;
2490 case IEEE80211_S_SCAN:
2491 timeout_add_msec(&ls->ls_slow_ch, RTW_LED_SLOW_MSEC);
2492 timeout_add_msec(&ls->ls_fast_ch, RTW_LED_FAST_MSEC);
2493 /*FALLTHROUGH*/
2494 case IEEE80211_S_AUTH:
2495 case IEEE80211_S_ASSOC:
2496 ls->ls_default = RTW_LED1;
2497 ls->ls_actblink = RTW_LED1;
2498 ls->ls_slowblink = RTW_LED1;
2499 break;
2500 case IEEE80211_S_RUN:
2501 ls->ls_slowblink = 0;
2502 break;
2503 }
2504 rtw_led_set(ls, &sc->sc_regs, sc->sc_hwverid);
2505 }
2506
2507 void
rtw_led_set(struct rtw_led_state * ls,struct rtw_regs * regs,u_int hwverid)2508 rtw_led_set(struct rtw_led_state *ls, struct rtw_regs *regs, u_int hwverid)
2509 {
2510 u_int8_t led_condition;
2511 bus_size_t ofs;
2512 u_int8_t mask, newval, val;
2513
2514 led_condition = ls->ls_default;
2515
2516 if (ls->ls_state & RTW_LED_S_SLOW)
2517 led_condition ^= ls->ls_slowblink;
2518 if (ls->ls_state & (RTW_LED_S_RX|RTW_LED_S_TX))
2519 led_condition ^= ls->ls_actblink;
2520
2521 RTW_DPRINTF(RTW_DEBUG_LED,
2522 ("%s: LED condition %\n", __func__, led_condition));
2523
2524 switch (hwverid) {
2525 default:
2526 case RTW_TCR_HWVERID_RTL8180F:
2527 ofs = RTW_PSR;
2528 newval = mask = RTW_PSR_LEDGPO0 | RTW_PSR_LEDGPO1;
2529 if (led_condition & RTW_LED0)
2530 newval &= ~RTW_PSR_LEDGPO0;
2531 if (led_condition & RTW_LED1)
2532 newval &= ~RTW_PSR_LEDGPO1;
2533 break;
2534 case RTW_TCR_HWVERID_RTL8180D:
2535 ofs = RTW_9346CR;
2536 mask = RTW_9346CR_EEM_MASK | RTW_9346CR_EEDI | RTW_9346CR_EECS;
2537 newval = RTW_9346CR_EEM_PROGRAM;
2538 if (led_condition & RTW_LED0)
2539 newval |= RTW_9346CR_EEDI;
2540 if (led_condition & RTW_LED1)
2541 newval |= RTW_9346CR_EECS;
2542 break;
2543 }
2544 val = RTW_READ8(regs, ofs);
2545 RTW_DPRINTF(RTW_DEBUG_LED,
2546 ("%s: read % from reg[%#02]\n", __func__, val,
2547 (u_int *)ofs));
2548 val &= ~mask;
2549 val |= newval;
2550 RTW_WRITE8(regs, ofs, val);
2551 RTW_DPRINTF(RTW_DEBUG_LED,
2552 ("%s: wrote % to reg[%#02]\n", __func__, val,
2553 (u_int *)ofs));
2554 RTW_SYNC(regs, ofs, ofs);
2555 }
2556
2557 void
rtw_led_fastblink(void * arg)2558 rtw_led_fastblink(void *arg)
2559 {
2560 int ostate, s;
2561 struct rtw_softc *sc = (struct rtw_softc *)arg;
2562 struct rtw_led_state *ls = &sc->sc_led_state;
2563
2564 s = splnet();
2565 ostate = ls->ls_state;
2566 ls->ls_state ^= ls->ls_event;
2567
2568 if ((ls->ls_event & RTW_LED_S_TX) == 0)
2569 ls->ls_state &= ~RTW_LED_S_TX;
2570
2571 if ((ls->ls_event & RTW_LED_S_RX) == 0)
2572 ls->ls_state &= ~RTW_LED_S_RX;
2573
2574 ls->ls_event = 0;
2575
2576 if (ostate != ls->ls_state)
2577 rtw_led_set(ls, &sc->sc_regs, sc->sc_hwverid);
2578 splx(s);
2579
2580 timeout_add_msec(&ls->ls_fast_ch, RTW_LED_FAST_MSEC);
2581 }
2582
2583 void
rtw_led_slowblink(void * arg)2584 rtw_led_slowblink(void *arg)
2585 {
2586 int s;
2587 struct rtw_softc *sc = (struct rtw_softc *)arg;
2588 struct rtw_led_state *ls = &sc->sc_led_state;
2589
2590 s = splnet();
2591 ls->ls_state ^= RTW_LED_S_SLOW;
2592 rtw_led_set(ls, &sc->sc_regs, sc->sc_hwverid);
2593 splx(s);
2594 timeout_add_msec(&ls->ls_slow_ch, RTW_LED_SLOW_MSEC);
2595 }
2596
2597 void
rtw_led_attach(struct rtw_led_state * ls,void * arg)2598 rtw_led_attach(struct rtw_led_state *ls, void *arg)
2599 {
2600 timeout_set(&ls->ls_fast_ch, rtw_led_fastblink, arg);
2601 timeout_set(&ls->ls_slow_ch, rtw_led_slowblink, arg);
2602 }
2603
2604 int
rtw_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)2605 rtw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2606 {
2607 struct rtw_softc *sc = ifp->if_softc;
2608 struct ifreq *ifr = (struct ifreq *)data;
2609 int rc = 0, s;
2610
2611 s = splnet();
2612
2613 switch (cmd) {
2614 case SIOCSIFADDR:
2615 ifp->if_flags |= IFF_UP;
2616 /* FALLTHROUGH */
2617
2618 case SIOCSIFFLAGS:
2619 if ((ifp->if_flags & IFF_UP) != 0) {
2620 if ((sc->sc_flags & RTW_F_ENABLED) != 0) {
2621 rtw_pktfilt_load(sc);
2622 } else
2623 rc = rtw_init(ifp);
2624 } else if ((sc->sc_flags & RTW_F_ENABLED) != 0)
2625 rtw_stop(ifp, 1);
2626 break;
2627
2628 case SIOCADDMULTI:
2629 case SIOCDELMULTI:
2630 if (cmd == SIOCADDMULTI)
2631 rc = ether_addmulti(ifr, &sc->sc_ic.ic_ac);
2632 else
2633 rc = ether_delmulti(ifr, &sc->sc_ic.ic_ac);
2634 if (rc != ENETRESET)
2635 break;
2636 if (ifp->if_flags & IFF_RUNNING)
2637 rtw_pktfilt_load(sc);
2638 rc = 0;
2639 break;
2640
2641 default:
2642 if ((rc = ieee80211_ioctl(ifp, cmd, data)) == ENETRESET) {
2643 if ((sc->sc_flags & RTW_F_ENABLED) != 0)
2644 rc = rtw_init(ifp);
2645 else
2646 rc = 0;
2647 }
2648 break;
2649 }
2650
2651 splx(s);
2652 return rc;
2653 }
2654
2655 /* Select a transmit ring with at least one h/w and s/w descriptor free.
2656 * Return 0 on success, -1 on failure.
2657 */
2658 int
rtw_txring_choose(struct rtw_softc * sc,struct rtw_txsoft_blk ** tsbp,struct rtw_txdesc_blk ** tdbp,int pri)2659 rtw_txring_choose(struct rtw_softc *sc, struct rtw_txsoft_blk **tsbp,
2660 struct rtw_txdesc_blk **tdbp, int pri)
2661 {
2662 struct rtw_txsoft_blk *tsb;
2663 struct rtw_txdesc_blk *tdb;
2664
2665 KASSERT(pri >= 0 && pri < RTW_NTXPRI);
2666
2667 tsb = &sc->sc_txsoft_blk[pri];
2668 tdb = &sc->sc_txdesc_blk[pri];
2669
2670 if (SIMPLEQ_EMPTY(&tsb->tsb_freeq) || tdb->tdb_nfree == 0) {
2671 if (tsb->tsb_tx_timer == 0)
2672 tsb->tsb_tx_timer = 5;
2673 *tsbp = NULL;
2674 *tdbp = NULL;
2675 return -1;
2676 }
2677 *tsbp = tsb;
2678 *tdbp = tdb;
2679 return 0;
2680 }
2681
2682 struct mbuf *
rtw_80211_dequeue(struct rtw_softc * sc,struct mbuf_queue * ifq,int pri,struct rtw_txsoft_blk ** tsbp,struct rtw_txdesc_blk ** tdbp,struct ieee80211_node ** nip)2683 rtw_80211_dequeue(struct rtw_softc *sc, struct mbuf_queue *ifq, int pri,
2684 struct rtw_txsoft_blk **tsbp, struct rtw_txdesc_blk **tdbp,
2685 struct ieee80211_node **nip)
2686 {
2687 struct mbuf *m;
2688
2689 if (mq_empty(ifq))
2690 return NULL;
2691 if (rtw_txring_choose(sc, tsbp, tdbp, pri) == -1) {
2692 DPRINTF(sc, RTW_DEBUG_XMIT_RSRC, ("%s: no ring %d descriptor\n",
2693 __func__, pri));
2694 ifq_set_oactive(&sc->sc_if.if_snd);
2695 sc->sc_if.if_timer = 1;
2696 return NULL;
2697 }
2698 m = mq_dequeue(ifq);
2699 *nip = m->m_pkthdr.ph_cookie;
2700 return m;
2701 }
2702
2703 /* Point *mp at the next 802.11 frame to transmit. Point *tsbp
2704 * at the driver's selection of transmit control block for the packet.
2705 */
2706 int
rtw_dequeue(struct ifnet * ifp,struct rtw_txsoft_blk ** tsbp,struct rtw_txdesc_blk ** tdbp,struct mbuf ** mp,struct ieee80211_node ** nip)2707 rtw_dequeue(struct ifnet *ifp, struct rtw_txsoft_blk **tsbp,
2708 struct rtw_txdesc_blk **tdbp, struct mbuf **mp,
2709 struct ieee80211_node **nip)
2710 {
2711 struct ieee80211com *ic;
2712 struct ieee80211_frame *wh;
2713 struct ieee80211_key *k;
2714 struct mbuf *m0;
2715 struct rtw_softc *sc;
2716
2717 sc = (struct rtw_softc *)ifp->if_softc;
2718 ic = &sc->sc_ic;
2719
2720 DPRINTF(sc, RTW_DEBUG_XMIT,
2721 ("%s: enter %s\n", sc->sc_dev.dv_xname, __func__));
2722
2723 if (ic->ic_state == IEEE80211_S_RUN &&
2724 (*mp = rtw_80211_dequeue(sc, &sc->sc_beaconq, RTW_TXPRIBCN, tsbp,
2725 tdbp, nip)) != NULL) {
2726 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: dequeue beacon frame\n",
2727 __func__));
2728 return 0;
2729 }
2730
2731 if ((*mp = rtw_80211_dequeue(sc, &ic->ic_mgtq, RTW_TXPRIMD, tsbp,
2732 tdbp, nip)) != NULL) {
2733 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: dequeue mgt frame\n",
2734 __func__));
2735 return 0;
2736 }
2737
2738 if (sc->sc_ic.ic_state != IEEE80211_S_RUN) {
2739 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: not running\n", __func__));
2740 return 0;
2741 }
2742
2743 if ((*mp = rtw_80211_dequeue(sc, &ic->ic_pwrsaveq, RTW_TXPRIHI,
2744 tsbp, tdbp, nip)) != NULL) {
2745 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: dequeue pwrsave frame\n",
2746 __func__));
2747 return 0;
2748 }
2749
2750 if (ic->ic_state != IEEE80211_S_RUN) {
2751 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: not running\n", __func__));
2752 return 0;
2753 }
2754
2755 *mp = NULL;
2756
2757 m0 = ifq_deq_begin(&ifp->if_snd);
2758 if (m0 == NULL) {
2759 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: no frame ready\n",
2760 __func__));
2761 return 0;
2762 }
2763
2764 if (rtw_txring_choose(sc, tsbp, tdbp, RTW_TXPRIMD) == -1) {
2765 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: no descriptor\n", __func__));
2766 ifq_deq_rollback(&ifp->if_snd, m0);
2767 ifq_set_oactive(&ifp->if_snd);
2768 sc->sc_if.if_timer = 1;
2769 return 0;
2770 }
2771
2772 ifq_deq_commit(&ifp->if_snd, m0);
2773 if (m0 == NULL) {
2774 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: no frame/ring ready\n",
2775 __func__));
2776 return 0;
2777 }
2778 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: dequeue data frame\n", __func__));
2779 #if NBPFILTER > 0
2780 if (ifp->if_bpf)
2781 bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
2782 #endif
2783 if ((m0 = ieee80211_encap(ifp, m0, nip)) == NULL) {
2784 DPRINTF(sc, RTW_DEBUG_XMIT,
2785 ("%s: encap error\n", __func__));
2786 ifp->if_oerrors++;
2787 return -1;
2788 }
2789
2790 /* XXX should do WEP in hardware */
2791 if (ic->ic_flags & IEEE80211_F_WEPON) {
2792 wh = mtod(m0, struct ieee80211_frame *);
2793 k = ieee80211_get_txkey(ic, wh, *nip);
2794 if ((m0 = ieee80211_encrypt(ic, m0, k)) == NULL)
2795 return -1;
2796 }
2797
2798 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: leave\n", __func__));
2799 *mp = m0;
2800 return 0;
2801 }
2802
2803 int
rtw_seg_too_short(bus_dmamap_t dmamap)2804 rtw_seg_too_short(bus_dmamap_t dmamap)
2805 {
2806 int i;
2807 for (i = 0; i < dmamap->dm_nsegs; i++) {
2808 if (dmamap->dm_segs[i].ds_len < 4) {
2809 printf("%s: segment too short\n", __func__);
2810 return 1;
2811 }
2812 }
2813 return 0;
2814 }
2815
2816 /* TBD factor with atw_start */
2817 struct mbuf *
rtw_dmamap_load_txbuf(bus_dma_tag_t dmat,bus_dmamap_t dmam,struct mbuf * chain,u_int ndescfree,short * ifflagsp,const char * dvname)2818 rtw_dmamap_load_txbuf(bus_dma_tag_t dmat, bus_dmamap_t dmam, struct mbuf *chain,
2819 u_int ndescfree, short *ifflagsp, const char *dvname)
2820 {
2821 int first, rc;
2822 struct mbuf *m, *m0;
2823
2824 m0 = chain;
2825
2826 /*
2827 * Load the DMA map. Copy and try (once) again if the packet
2828 * didn't fit in the allotted number of segments.
2829 */
2830 for (first = 1;
2831 ((rc = bus_dmamap_load_mbuf(dmat, dmam, m0,
2832 BUS_DMA_WRITE|BUS_DMA_NOWAIT)) != 0 ||
2833 dmam->dm_nsegs > ndescfree || rtw_seg_too_short(dmam)) && first;
2834 first = 0) {
2835 if (rc == 0)
2836 bus_dmamap_unload(dmat, dmam);
2837 MGETHDR(m, M_DONTWAIT, MT_DATA);
2838 if (m == NULL) {
2839 printf("%s: unable to allocate Tx mbuf\n",
2840 dvname);
2841 break;
2842 }
2843 if (m0->m_pkthdr.len > MHLEN) {
2844 MCLGET(m, M_DONTWAIT);
2845 if ((m->m_flags & M_EXT) == 0) {
2846 printf("%s: cannot allocate Tx cluster\n",
2847 dvname);
2848 m_freem(m);
2849 break;
2850 }
2851 }
2852 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
2853 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
2854 m_freem(m0);
2855 m0 = m;
2856 m = NULL;
2857 }
2858 if (rc != 0) {
2859 printf("%s: cannot load Tx buffer, rc = %d\n", dvname, rc);
2860 m_freem(m0);
2861 return NULL;
2862 } else if (rtw_seg_too_short(dmam)) {
2863 printf("%s: cannot load Tx buffer, segment too short\n",
2864 dvname);
2865 bus_dmamap_unload(dmat, dmam);
2866 m_freem(m0);
2867 return NULL;
2868 } else if (dmam->dm_nsegs > ndescfree) {
2869 printf("%s: too many tx segments\n", dvname);
2870 bus_dmamap_unload(dmat, dmam);
2871 m_freem(m0);
2872 return NULL;
2873 }
2874 return m0;
2875 }
2876
2877
2878 /*
2879 * Arguments in:
2880 *
2881 * paylen: payload length (no FCS, no WEP header)
2882 *
2883 * hdrlen: header length
2884 *
2885 * rate: MSDU speed, units 500kb/s
2886 *
2887 * flags: IEEE80211_F_SHPREAMBLE (use short preamble),
2888 * IEEE80211_F_SHSLOT (use short slot length)
2889 *
2890 * Arguments out:
2891 *
2892 * d: 802.11 Duration field for RTS,
2893 * 802.11 Duration field for data frame,
2894 * PLCP Length for data frame,
2895 * residual octets at end of data slot
2896 */
2897 int
rtw_compute_duration1(int len,int use_ack,uint32_t flags,int rate,struct rtw_duration * d)2898 rtw_compute_duration1(int len, int use_ack, uint32_t flags, int rate,
2899 struct rtw_duration *d)
2900 {
2901 int pre, ctsrate;
2902 int ack, bitlen, data_dur, remainder;
2903
2904 /* RTS reserves medium for SIFS | CTS | SIFS | (DATA) | SIFS | ACK
2905 * DATA reserves medium for SIFS | ACK
2906 *
2907 * XXXMYC: no ACK on multicast/broadcast or control packets
2908 */
2909
2910 bitlen = len * 8;
2911
2912 pre = IEEE80211_DUR_DS_SIFS;
2913 if ((flags & IEEE80211_F_SHPREAMBLE) != 0)
2914 pre += IEEE80211_DUR_DS_SHORT_PREAMBLE +
2915 IEEE80211_DUR_DS_FAST_PLCPHDR;
2916 else
2917 pre += IEEE80211_DUR_DS_LONG_PREAMBLE +
2918 IEEE80211_DUR_DS_SLOW_PLCPHDR;
2919
2920 d->d_residue = 0;
2921 data_dur = (bitlen * 2) / rate;
2922 remainder = (bitlen * 2) % rate;
2923 if (remainder != 0) {
2924 d->d_residue = (rate - remainder) / 16;
2925 data_dur++;
2926 }
2927
2928 switch (rate) {
2929 case 2: /* 1 Mb/s */
2930 case 4: /* 2 Mb/s */
2931 /* 1 - 2 Mb/s WLAN: send ACK/CTS at 1 Mb/s */
2932 ctsrate = 2;
2933 break;
2934 case 11: /* 5.5 Mb/s */
2935 case 22: /* 11 Mb/s */
2936 case 44: /* 22 Mb/s */
2937 /* 5.5 - 11 Mb/s WLAN: send ACK/CTS at 2 Mb/s */
2938 ctsrate = 4;
2939 break;
2940 default:
2941 /* TBD */
2942 return -1;
2943 }
2944
2945 d->d_plcp_len = data_dur;
2946
2947 ack = (use_ack) ? pre + (IEEE80211_DUR_DS_SLOW_ACK * 2) / ctsrate : 0;
2948
2949 d->d_rts_dur =
2950 pre + (IEEE80211_DUR_DS_SLOW_CTS * 2) / ctsrate +
2951 pre + data_dur +
2952 ack;
2953
2954 d->d_data_dur = ack;
2955
2956 return 0;
2957 }
2958
2959 /*
2960 * Arguments in:
2961 *
2962 * wh: 802.11 header
2963 *
2964 * len: packet length
2965 *
2966 * rate: MSDU speed, units 500kb/s
2967 *
2968 * fraglen: fragment length, set to maximum (or higher) for no
2969 * fragmentation
2970 *
2971 * flags: IEEE80211_F_WEPON (hardware adds WEP),
2972 * IEEE80211_F_SHPREAMBLE (use short preamble),
2973 * IEEE80211_F_SHSLOT (use short slot length)
2974 *
2975 * Arguments out:
2976 *
2977 * d0: 802.11 Duration fields (RTS/Data), PLCP Length, Service fields
2978 * of first/only fragment
2979 *
2980 * dn: 802.11 Duration fields (RTS/Data), PLCP Length, Service fields
2981 * of first/only fragment
2982 */
2983 int
rtw_compute_duration(struct ieee80211_frame * wh,int len,uint32_t flags,int fraglen,int rate,struct rtw_duration * d0,struct rtw_duration * dn,int * npktp,int debug)2984 rtw_compute_duration(struct ieee80211_frame *wh, int len, uint32_t flags,
2985 int fraglen, int rate, struct rtw_duration *d0, struct rtw_duration *dn,
2986 int *npktp, int debug)
2987 {
2988 int ack, rc;
2989 int firstlen, hdrlen, lastlen, lastlen0, npkt, overlen, paylen;
2990
2991 if (ieee80211_has_addr4(wh))
2992 hdrlen = sizeof(struct ieee80211_frame_addr4);
2993 else
2994 hdrlen = sizeof(struct ieee80211_frame);
2995
2996 paylen = len - hdrlen;
2997
2998 if ((flags & IEEE80211_F_WEPON) != 0)
2999 overlen = IEEE80211_WEP_TOTLEN + IEEE80211_CRC_LEN;
3000 else
3001 overlen = IEEE80211_CRC_LEN;
3002
3003 npkt = paylen / fraglen;
3004 lastlen0 = paylen % fraglen;
3005
3006 if (npkt == 0) /* no fragments */
3007 lastlen = paylen + overlen;
3008 else if (lastlen0 != 0) { /* a short "tail" fragment */
3009 lastlen = lastlen0 + overlen;
3010 npkt++;
3011 } else /* full-length "tail" fragment */
3012 lastlen = fraglen + overlen;
3013
3014 if (npktp != NULL)
3015 *npktp = npkt;
3016
3017 if (npkt > 1)
3018 firstlen = fraglen + overlen;
3019 else
3020 firstlen = paylen + overlen;
3021
3022 if (debug) {
3023 printf("%s: npkt %d firstlen %d lastlen0 %d lastlen %d "
3024 "fraglen %d overlen %d len %d rate %d flags %08x\n",
3025 __func__, npkt, firstlen, lastlen0, lastlen, fraglen,
3026 overlen, len, rate, flags);
3027 }
3028
3029 ack = !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3030 (wh->i_fc[1] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL;
3031
3032 rc = rtw_compute_duration1(firstlen + hdrlen, ack, flags, rate, d0);
3033 if (rc == -1)
3034 return rc;
3035
3036 if (npkt <= 1) {
3037 *dn = *d0;
3038 return 0;
3039 }
3040 return rtw_compute_duration1(lastlen + hdrlen, ack, flags, rate, dn);
3041 }
3042
3043 #ifdef RTW_DEBUG
3044 void
rtw_print_txdesc(struct rtw_softc * sc,const char * action,struct rtw_txsoft * ts,struct rtw_txdesc_blk * tdb,int desc)3045 rtw_print_txdesc(struct rtw_softc *sc, const char *action,
3046 struct rtw_txsoft *ts, struct rtw_txdesc_blk *tdb, int desc)
3047 {
3048 struct rtw_txdesc *td = &tdb->tdb_desc[desc];
3049 DPRINTF(sc, RTW_DEBUG_XMIT_DESC, ("%s: %p %s txdesc[%d] next %#08x "
3050 "buf %#08x ctl0 %#08x ctl1 %#08x len %#08x\n",
3051 sc->sc_dev.dv_xname, ts, action, desc,
3052 letoh32(td->td_buf), letoh32(td->td_next),
3053 letoh32(td->td_ctl0), letoh32(td->td_ctl1),
3054 letoh32(td->td_len)));
3055 }
3056 #endif /* RTW_DEBUG */
3057
3058 void
rtw_start(struct ifnet * ifp)3059 rtw_start(struct ifnet *ifp)
3060 {
3061 uint8_t tppoll;
3062 int desc, i, lastdesc, npkt, rate;
3063 uint32_t proto_ctl0, ctl0, ctl1;
3064 bus_dmamap_t dmamap;
3065 struct ieee80211com *ic;
3066 struct ieee80211_frame *wh;
3067 struct ieee80211_node *ni;
3068 struct mbuf *m0;
3069 struct rtw_softc *sc;
3070 struct rtw_duration *d0;
3071 struct rtw_txsoft_blk *tsb;
3072 struct rtw_txdesc_blk *tdb;
3073 struct rtw_txsoft *ts;
3074 struct rtw_txdesc *td;
3075
3076 sc = (struct rtw_softc *)ifp->if_softc;
3077 ic = &sc->sc_ic;
3078
3079 DPRINTF(sc, RTW_DEBUG_XMIT,
3080 ("%s: enter %s\n", sc->sc_dev.dv_xname, __func__));
3081
3082 if (!(ifp->if_flags & IFF_RUNNING) || ifq_is_oactive(&ifp->if_snd))
3083 goto out;
3084
3085 /* XXX do real rate control */
3086 proto_ctl0 = RTW_TXCTL0_RTSRATE_1MBPS;
3087
3088 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0)
3089 proto_ctl0 |= RTW_TXCTL0_SPLCP;
3090
3091 for (;;) {
3092 if (rtw_dequeue(ifp, &tsb, &tdb, &m0, &ni) == -1)
3093 continue;
3094 if (m0 == NULL)
3095 break;
3096 ts = SIMPLEQ_FIRST(&tsb->tsb_freeq);
3097
3098 dmamap = ts->ts_dmamap;
3099
3100 m0 = rtw_dmamap_load_txbuf(sc->sc_dmat, dmamap, m0,
3101 tdb->tdb_nfree, &ifp->if_flags, sc->sc_dev.dv_xname);
3102
3103 if (m0 == NULL || dmamap->dm_nsegs == 0) {
3104 DPRINTF(sc, RTW_DEBUG_XMIT,
3105 ("%s: fail dmamap load\n", __func__));
3106 goto post_dequeue_err;
3107 }
3108
3109 wh = mtod(m0, struct ieee80211_frame *);
3110
3111 /* XXX do real rate control */
3112 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
3113 IEEE80211_FC0_TYPE_MGT)
3114 rate = 2;
3115 else
3116 rate = MAX(2, ieee80211_get_rate(ic));
3117
3118 #ifdef RTW_DEBUG
3119 if ((sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) ==
3120 (IFF_DEBUG|IFF_LINK2)) {
3121 ieee80211_dump_pkt(mtod(m0, uint8_t *),
3122 (dmamap->dm_nsegs == 1) ? m0->m_pkthdr.len
3123 : sizeof(wh), rate, 0);
3124 }
3125 #endif /* RTW_DEBUG */
3126 ctl0 = proto_ctl0 |
3127 LSHIFT(m0->m_pkthdr.len, RTW_TXCTL0_TPKTSIZE_MASK);
3128
3129 switch (rate) {
3130 default:
3131 case 2:
3132 ctl0 |= RTW_TXCTL0_RATE_1MBPS;
3133 break;
3134 case 4:
3135 ctl0 |= RTW_TXCTL0_RATE_2MBPS;
3136 break;
3137 case 11:
3138 ctl0 |= RTW_TXCTL0_RATE_5MBPS;
3139 break;
3140 case 22:
3141 ctl0 |= RTW_TXCTL0_RATE_11MBPS;
3142 break;
3143 }
3144
3145 /* XXX >= ? Compare after fragmentation? */
3146 if (m0->m_pkthdr.len > ic->ic_rtsthreshold)
3147 ctl0 |= RTW_TXCTL0_RTSEN;
3148
3149 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
3150 IEEE80211_FC0_TYPE_MGT) {
3151 ctl0 &= ~(RTW_TXCTL0_SPLCP | RTW_TXCTL0_RTSEN);
3152 if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
3153 IEEE80211_FC0_SUBTYPE_BEACON)
3154 ctl0 |= RTW_TXCTL0_BEACON;
3155 }
3156
3157 if (rtw_compute_duration(wh, m0->m_pkthdr.len,
3158 ic->ic_flags & ~IEEE80211_F_WEPON, ic->ic_fragthreshold,
3159 rate, &ts->ts_d0, &ts->ts_dn, &npkt,
3160 (sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) ==
3161 (IFF_DEBUG|IFF_LINK2)) == -1) {
3162 DPRINTF(sc, RTW_DEBUG_XMIT,
3163 ("%s: fail compute duration\n", __func__));
3164 goto post_load_err;
3165 }
3166
3167 d0 = &ts->ts_d0;
3168
3169 *(uint16_t*)wh->i_dur = htole16(d0->d_data_dur);
3170
3171 ctl1 = LSHIFT(d0->d_plcp_len, RTW_TXCTL1_LENGTH_MASK) |
3172 LSHIFT(d0->d_rts_dur, RTW_TXCTL1_RTSDUR_MASK);
3173
3174 if (d0->d_residue)
3175 ctl1 |= RTW_TXCTL1_LENGEXT;
3176
3177 /* TBD fragmentation */
3178
3179 ts->ts_first = tdb->tdb_next;
3180
3181 rtw_txdescs_sync(tdb, ts->ts_first, dmamap->dm_nsegs,
3182 BUS_DMASYNC_PREWRITE);
3183
3184 KASSERT(ts->ts_first < tdb->tdb_ndesc);
3185
3186 #if NBPFILTER > 0
3187 if (ic->ic_rawbpf != NULL)
3188 bpf_mtap((caddr_t)ic->ic_rawbpf, m0,
3189 BPF_DIRECTION_OUT);
3190
3191 if (sc->sc_radiobpf != NULL) {
3192 struct mbuf mb;
3193 struct rtw_tx_radiotap_header *rt = &sc->sc_txtap;
3194
3195 rt->rt_flags = 0;
3196 rt->rt_rate = rate;
3197 rt->rt_chan_freq =
3198 htole16(ic->ic_bss->ni_chan->ic_freq);
3199 rt->rt_chan_flags =
3200 htole16(ic->ic_bss->ni_chan->ic_flags);
3201
3202 mb.m_data = (caddr_t)rt;
3203 mb.m_len = sizeof(sc->sc_txtapu);
3204 mb.m_next = m0;
3205 mb.m_nextpkt = NULL;
3206 mb.m_type = 0;
3207 mb.m_flags = 0;
3208 bpf_mtap(sc->sc_radiobpf, &mb, BPF_DIRECTION_OUT);
3209
3210 }
3211 #endif /* NBPFILTER > 0 */
3212
3213 for (i = 0, lastdesc = desc = ts->ts_first;
3214 i < dmamap->dm_nsegs;
3215 i++, desc = RTW_NEXT_IDX(tdb, desc)) {
3216 if (dmamap->dm_segs[i].ds_len > RTW_TXLEN_LENGTH_MASK) {
3217 DPRINTF(sc, RTW_DEBUG_XMIT_DESC,
3218 ("%s: seg too long\n", __func__));
3219 goto post_load_err;
3220 }
3221 td = &tdb->tdb_desc[desc];
3222 td->td_ctl0 = htole32(ctl0);
3223 if (i != 0)
3224 td->td_ctl0 |= htole32(RTW_TXCTL0_OWN);
3225 td->td_ctl1 = htole32(ctl1);
3226 td->td_buf = htole32(dmamap->dm_segs[i].ds_addr);
3227 td->td_len = htole32(dmamap->dm_segs[i].ds_len);
3228 lastdesc = desc;
3229 #ifdef RTW_DEBUG
3230 rtw_print_txdesc(sc, "load", ts, tdb, desc);
3231 #endif /* RTW_DEBUG */
3232 }
3233
3234 KASSERT(desc < tdb->tdb_ndesc);
3235
3236 ts->ts_ni = ni;
3237 ts->ts_mbuf = m0;
3238 ts->ts_last = lastdesc;
3239 tdb->tdb_desc[ts->ts_last].td_ctl0 |= htole32(RTW_TXCTL0_LS);
3240 tdb->tdb_desc[ts->ts_first].td_ctl0 |=
3241 htole32(RTW_TXCTL0_FS);
3242
3243 #ifdef RTW_DEBUG
3244 rtw_print_txdesc(sc, "FS on", ts, tdb, ts->ts_first);
3245 rtw_print_txdesc(sc, "LS on", ts, tdb, ts->ts_last);
3246 #endif /* RTW_DEBUG */
3247
3248 tdb->tdb_nfree -= dmamap->dm_nsegs;
3249 tdb->tdb_next = desc;
3250
3251 rtw_txdescs_sync(tdb, ts->ts_first, dmamap->dm_nsegs,
3252 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3253
3254 tdb->tdb_desc[ts->ts_first].td_ctl0 |=
3255 htole32(RTW_TXCTL0_OWN);
3256
3257 #ifdef RTW_DEBUG
3258 rtw_print_txdesc(sc, "OWN on", ts, tdb, ts->ts_first);
3259 #endif /* RTW_DEBUG */
3260
3261 rtw_txdescs_sync(tdb, ts->ts_first, 1,
3262 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3263
3264 SIMPLEQ_REMOVE_HEAD(&tsb->tsb_freeq, ts_q);
3265 SIMPLEQ_INSERT_TAIL(&tsb->tsb_dirtyq, ts, ts_q);
3266
3267 if (tsb != &sc->sc_txsoft_blk[RTW_TXPRIBCN])
3268 sc->sc_led_state.ls_event |= RTW_LED_S_TX;
3269 tsb->tsb_tx_timer = 5;
3270 ifp->if_timer = 1;
3271 tppoll = RTW_READ8(&sc->sc_regs, RTW_TPPOLL);
3272 tppoll &= ~RTW_TPPOLL_SALL;
3273 tppoll |= tsb->tsb_poll & RTW_TPPOLL_ALL;
3274 RTW_WRITE8(&sc->sc_regs, RTW_TPPOLL, tppoll);
3275 RTW_SYNC(&sc->sc_regs, RTW_TPPOLL, RTW_TPPOLL);
3276 }
3277 out:
3278 DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: leave\n", __func__));
3279 return;
3280 post_load_err:
3281 bus_dmamap_unload(sc->sc_dmat, dmamap);
3282 m_freem(m0);
3283 post_dequeue_err:
3284 ieee80211_release_node(&sc->sc_ic, ni);
3285 return;
3286 }
3287
3288 void
rtw_idle(struct rtw_regs * regs)3289 rtw_idle(struct rtw_regs *regs)
3290 {
3291 int active;
3292
3293 /* request stop DMA; wait for packets to stop transmitting. */
3294
3295 RTW_WRITE8(regs, RTW_TPPOLL, RTW_TPPOLL_SALL);
3296 RTW_WBR(regs, RTW_TPPOLL, RTW_TPPOLL);
3297
3298 for (active = 0; active < 300 &&
3299 (RTW_READ8(regs, RTW_TPPOLL) & RTW_TPPOLL_ACTIVE) != 0; active++)
3300 DELAY(10);
3301 RTW_DPRINTF(RTW_DEBUG_BUGS,
3302 ("%s: transmit DMA idle in %dus\n", __func__, active * 10));
3303 }
3304
3305 void
rtw_watchdog(struct ifnet * ifp)3306 rtw_watchdog(struct ifnet *ifp)
3307 {
3308 int pri, tx_timeouts = 0;
3309 struct rtw_softc *sc;
3310 struct rtw_txsoft_blk *tsb;
3311
3312 sc = ifp->if_softc;
3313
3314 ifp->if_timer = 0;
3315
3316 if ((sc->sc_flags & RTW_F_ENABLED) == 0)
3317 return;
3318
3319 for (pri = 0; pri < RTW_NTXPRI; pri++) {
3320 tsb = &sc->sc_txsoft_blk[pri];
3321
3322 if (tsb->tsb_tx_timer == 0)
3323 continue;
3324 else if (--tsb->tsb_tx_timer == 0) {
3325 if (SIMPLEQ_EMPTY(&tsb->tsb_dirtyq))
3326 continue;
3327 RTW_DPRINTF(RTW_DEBUG_BUGS,
3328 ("%s: transmit timeout, priority %d\n",
3329 ifp->if_xname, pri));
3330 ifp->if_oerrors++;
3331 tx_timeouts++;
3332 } else
3333 ifp->if_timer = 1;
3334 }
3335
3336 if (tx_timeouts > 0) {
3337 /* Stop Tx DMA, disable xmtr, flush Tx rings, enable xmtr,
3338 * reset s/w tx-ring pointers, and start transmission.
3339 *
3340 * TBD Stop/restart just the broken rings?
3341 */
3342 rtw_idle(&sc->sc_regs);
3343 rtw_io_enable(&sc->sc_regs, RTW_CR_TE, 0);
3344 rtw_txdescs_reset(sc);
3345 rtw_io_enable(&sc->sc_regs, RTW_CR_TE, 1);
3346 rtw_txring_fixup(sc);
3347 rtw_start(ifp);
3348 }
3349 ieee80211_watchdog(ifp);
3350 }
3351
3352 void
rtw_next_scan(void * arg)3353 rtw_next_scan(void *arg)
3354 {
3355 struct rtw_softc *sc = arg;
3356 struct ieee80211com *ic = &sc->sc_ic;
3357 struct ifnet *ifp = &ic->ic_if;
3358 int s;
3359
3360 /* don't call rtw_start w/o network interrupts blocked */
3361 s = splnet();
3362 if (ic->ic_state == IEEE80211_S_SCAN)
3363 ieee80211_next_scan(ifp);
3364 splx(s);
3365 }
3366
3367 void
rtw_join_bss(struct rtw_softc * sc,u_int8_t * bssid,u_int16_t intval0)3368 rtw_join_bss(struct rtw_softc *sc, u_int8_t *bssid, u_int16_t intval0)
3369 {
3370 uint16_t bcnitv, bintritv, intval;
3371 int i;
3372 struct rtw_regs *regs = &sc->sc_regs;
3373
3374 for (i = 0; i < IEEE80211_ADDR_LEN; i++)
3375 RTW_WRITE8(regs, RTW_BSSID + i, bssid[i]);
3376
3377 RTW_SYNC(regs, RTW_BSSID16, RTW_BSSID32);
3378
3379 rtw_set_access(regs, RTW_ACCESS_CONFIG);
3380
3381 intval = MIN(intval0, PRESHIFT(RTW_BCNITV_BCNITV_MASK));
3382
3383 bcnitv = RTW_READ16(regs, RTW_BCNITV) & ~RTW_BCNITV_BCNITV_MASK;
3384 bcnitv |= LSHIFT(intval, RTW_BCNITV_BCNITV_MASK);
3385 RTW_WRITE16(regs, RTW_BCNITV, bcnitv);
3386 /* interrupt host 1ms before the TBTT */
3387 bintritv = RTW_READ16(regs, RTW_BINTRITV) & ~RTW_BINTRITV_BINTRITV;
3388 bintritv |= LSHIFT(1000, RTW_BINTRITV_BINTRITV);
3389 RTW_WRITE16(regs, RTW_BINTRITV, bintritv);
3390 /* magic from Linux */
3391 RTW_WRITE16(regs, RTW_ATIMWND, LSHIFT(1, RTW_ATIMWND_ATIMWND));
3392 RTW_WRITE16(regs, RTW_ATIMTRITV, LSHIFT(2, RTW_ATIMTRITV_ATIMTRITV));
3393 rtw_set_access(regs, RTW_ACCESS_NONE);
3394
3395 /* TBD WEP */
3396 RTW_WRITE8(regs, RTW8180_SCR, 0);
3397
3398 rtw_io_enable(regs, RTW_CR_RE | RTW_CR_TE, 1);
3399 }
3400
3401 /* Synchronize the hardware state with the software state. */
3402 int
rtw_newstate(struct ieee80211com * ic,enum ieee80211_state nstate,int arg)3403 rtw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
3404 {
3405 struct ifnet *ifp = &ic->ic_if;
3406 struct rtw_softc *sc = ifp->if_softc;
3407 enum ieee80211_state ostate;
3408 int error;
3409
3410 ostate = ic->ic_state;
3411
3412 rtw_led_newstate(sc, nstate);
3413
3414 if (nstate == IEEE80211_S_INIT) {
3415 timeout_del(&sc->sc_scan_to);
3416 sc->sc_cur_chan = IEEE80211_CHAN_ANY;
3417 return (*sc->sc_mtbl.mt_newstate)(ic, nstate, arg);
3418 }
3419
3420 if (ostate == IEEE80211_S_INIT && nstate != IEEE80211_S_INIT)
3421 rtw_pwrstate(sc, RTW_ON);
3422
3423 if ((error = rtw_tune(sc)) != 0)
3424 return error;
3425
3426 switch (nstate) {
3427 case IEEE80211_S_INIT:
3428 panic("%s: unexpected state IEEE80211_S_INIT", __func__);
3429 break;
3430 case IEEE80211_S_SCAN:
3431 if (ostate != IEEE80211_S_SCAN) {
3432 bzero(ic->ic_bss->ni_bssid, IEEE80211_ADDR_LEN);
3433 rtw_set_nettype(sc, IEEE80211_M_MONITOR);
3434 }
3435
3436 timeout_add_msec(&sc->sc_scan_to, rtw_dwelltime);
3437
3438 break;
3439 case IEEE80211_S_RUN:
3440 switch (ic->ic_opmode) {
3441 #ifndef IEEE80211_STA_ONLY
3442 case IEEE80211_M_HOSTAP:
3443 case IEEE80211_M_IBSS:
3444 rtw_set_nettype(sc, IEEE80211_M_MONITOR);
3445 /*FALLTHROUGH*/
3446 case IEEE80211_M_AHDEMO:
3447 #endif
3448 case IEEE80211_M_STA:
3449 rtw_join_bss(sc, ic->ic_bss->ni_bssid,
3450 ic->ic_bss->ni_intval);
3451 break;
3452 default:
3453 break;
3454 }
3455 rtw_set_nettype(sc, ic->ic_opmode);
3456 break;
3457 case IEEE80211_S_ASSOC:
3458 case IEEE80211_S_AUTH:
3459 break;
3460 }
3461
3462 if (nstate != IEEE80211_S_SCAN)
3463 timeout_del(&sc->sc_scan_to);
3464
3465 return (*sc->sc_mtbl.mt_newstate)(ic, nstate, arg);
3466 }
3467
3468 /* Extend a 32-bit TSF timestamp to a 64-bit timestamp. */
3469 uint64_t
rtw_tsf_extend(struct rtw_regs * regs,u_int32_t rstamp)3470 rtw_tsf_extend(struct rtw_regs *regs, u_int32_t rstamp)
3471 {
3472 u_int32_t tsftl, tsfth;
3473
3474 tsfth = RTW_READ(regs, RTW_TSFTRH);
3475 tsftl = RTW_READ(regs, RTW_TSFTRL);
3476 if (tsftl < rstamp) /* Compensate for rollover. */
3477 tsfth--;
3478 return ((u_int64_t)tsfth << 32) | rstamp;
3479 }
3480
3481 #ifndef IEEE80211_STA_ONLY
3482 void
rtw_ibss_merge(struct rtw_softc * sc,struct ieee80211_node * ni,u_int32_t rstamp)3483 rtw_ibss_merge(struct rtw_softc *sc, struct ieee80211_node *ni,
3484 u_int32_t rstamp)
3485 {
3486 u_int8_t tppoll;
3487 struct ieee80211com *ic = &sc->sc_ic;
3488
3489 if (ieee80211_ibss_merge(ic, ni,
3490 rtw_tsf_extend(&sc->sc_regs, rstamp)) == ENETRESET) {
3491 /* Stop beacon queue. Kick state machine to synchronize
3492 * with the new IBSS.
3493 */
3494 tppoll = RTW_READ8(&sc->sc_regs, RTW_TPPOLL);
3495 tppoll |= RTW_TPPOLL_SBQ;
3496 RTW_WRITE8(&sc->sc_regs, RTW_TPPOLL, tppoll);
3497 (void)ieee80211_new_state(&sc->sc_ic, IEEE80211_S_RUN, -1);
3498 }
3499 return;
3500 }
3501
3502 void
rtw_recv_mgmt(struct ieee80211com * ic,struct mbuf * m,struct ieee80211_node * ni,struct ieee80211_rxinfo * rxi,int subtype)3503 rtw_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
3504 struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, int subtype)
3505 {
3506 struct rtw_softc *sc = (struct rtw_softc*)ic->ic_softc;
3507
3508 (*sc->sc_mtbl.mt_recv_mgmt)(ic, m, ni, rxi, subtype);
3509
3510 switch (subtype) {
3511 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
3512 case IEEE80211_FC0_SUBTYPE_BEACON:
3513 if (ic->ic_opmode != IEEE80211_M_IBSS ||
3514 ic->ic_state != IEEE80211_S_RUN)
3515 return;
3516 rtw_ibss_merge(sc, ni, rxi->rxi_tstamp);
3517 break;
3518 default:
3519 break;
3520 }
3521 return;
3522 }
3523 #endif /* IEEE80211_STA_ONLY */
3524
3525 struct ieee80211_node *
rtw_node_alloc(struct ieee80211com * ic)3526 rtw_node_alloc(struct ieee80211com *ic)
3527 {
3528 struct rtw_softc *sc = (struct rtw_softc *)ic->ic_if.if_softc;
3529 struct ieee80211_node *ni = (*sc->sc_mtbl.mt_node_alloc)(ic);
3530
3531 DPRINTF(sc, RTW_DEBUG_NODE,
3532 ("%s: alloc node %p\n", sc->sc_dev.dv_xname, ni));
3533 return ni;
3534 }
3535
3536 void
rtw_node_free(struct ieee80211com * ic,struct ieee80211_node * ni)3537 rtw_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
3538 {
3539 struct rtw_softc *sc = (struct rtw_softc *)ic->ic_if.if_softc;
3540
3541 DPRINTF(sc, RTW_DEBUG_NODE,
3542 ("%s: freeing node %p %s\n", sc->sc_dev.dv_xname, ni,
3543 ether_sprintf(ni->ni_bssid)));
3544 (*sc->sc_mtbl.mt_node_free)(ic, ni);
3545 }
3546
3547 int
rtw_media_change(struct ifnet * ifp)3548 rtw_media_change(struct ifnet *ifp)
3549 {
3550 int error;
3551
3552 error = ieee80211_media_change(ifp);
3553 if (error == ENETRESET) {
3554 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
3555 (IFF_RUNNING|IFF_UP))
3556 rtw_init(ifp); /* XXX lose error */
3557 error = 0;
3558 }
3559 return error;
3560 }
3561
3562 void
rtw_media_status(struct ifnet * ifp,struct ifmediareq * imr)3563 rtw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
3564 {
3565 struct rtw_softc *sc = ifp->if_softc;
3566
3567 if ((sc->sc_flags & RTW_F_ENABLED) == 0) {
3568 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
3569 imr->ifm_status = 0;
3570 return;
3571 }
3572 ieee80211_media_status(ifp, imr);
3573 }
3574
3575 int
rtw_activate(struct device * self,int act)3576 rtw_activate(struct device *self, int act)
3577 {
3578 struct rtw_softc *sc = (struct rtw_softc *)self;
3579 struct ifnet *ifp = &sc->sc_ic.ic_if;
3580
3581 switch (act) {
3582 case DVACT_SUSPEND:
3583 if (ifp->if_flags & IFF_RUNNING) {
3584 rtw_stop(ifp, 1);
3585 if (sc->sc_power != NULL)
3586 (*sc->sc_power)(sc, act);
3587 }
3588 break;
3589 case DVACT_RESUME:
3590 if (ifp->if_flags & IFF_UP) {
3591 if (sc->sc_power != NULL)
3592 (*sc->sc_power)(sc, act);
3593 rtw_init(ifp);
3594 }
3595 break;
3596 }
3597 return 0;
3598 }
3599
3600 int
rtw_txsoft_blk_setup(struct rtw_txsoft_blk * tsb,u_int qlen)3601 rtw_txsoft_blk_setup(struct rtw_txsoft_blk *tsb, u_int qlen)
3602 {
3603 SIMPLEQ_INIT(&tsb->tsb_dirtyq);
3604 SIMPLEQ_INIT(&tsb->tsb_freeq);
3605 tsb->tsb_ndesc = qlen;
3606 tsb->tsb_desc = mallocarray(qlen, sizeof(*tsb->tsb_desc), M_DEVBUF,
3607 M_NOWAIT);
3608 if (tsb->tsb_desc == NULL)
3609 return ENOMEM;
3610 return 0;
3611 }
3612
3613 void
rtw_txsoft_blk_cleanup_all(struct rtw_softc * sc)3614 rtw_txsoft_blk_cleanup_all(struct rtw_softc *sc)
3615 {
3616 int pri;
3617 struct rtw_txsoft_blk *tsb;
3618
3619 for (pri = 0; pri < RTW_NTXPRI; pri++) {
3620 tsb = &sc->sc_txsoft_blk[pri];
3621 free(tsb->tsb_desc, M_DEVBUF, 0);
3622 tsb->tsb_desc = NULL;
3623 }
3624 }
3625
3626 int
rtw_txsoft_blk_setup_all(struct rtw_softc * sc)3627 rtw_txsoft_blk_setup_all(struct rtw_softc *sc)
3628 {
3629 int pri, rc = 0;
3630 int qlen[RTW_NTXPRI] =
3631 {RTW_TXQLENLO, RTW_TXQLENMD, RTW_TXQLENHI, RTW_TXQLENBCN};
3632 struct rtw_txsoft_blk *tsbs;
3633
3634 tsbs = sc->sc_txsoft_blk;
3635
3636 for (pri = 0; pri < RTW_NTXPRI; pri++) {
3637 rc = rtw_txsoft_blk_setup(&tsbs[pri], qlen[pri]);
3638 if (rc != 0)
3639 break;
3640 }
3641 tsbs[RTW_TXPRILO].tsb_poll = RTW_TPPOLL_LPQ | RTW_TPPOLL_SLPQ;
3642 tsbs[RTW_TXPRIMD].tsb_poll = RTW_TPPOLL_NPQ | RTW_TPPOLL_SNPQ;
3643 tsbs[RTW_TXPRIHI].tsb_poll = RTW_TPPOLL_HPQ | RTW_TPPOLL_SHPQ;
3644 tsbs[RTW_TXPRIBCN].tsb_poll = RTW_TPPOLL_BQ | RTW_TPPOLL_SBQ;
3645 return rc;
3646 }
3647
3648 void
rtw_txdesc_blk_setup(struct rtw_txdesc_blk * tdb,struct rtw_txdesc * desc,u_int ndesc,bus_addr_t ofs,bus_addr_t physbase)3649 rtw_txdesc_blk_setup(struct rtw_txdesc_blk *tdb, struct rtw_txdesc *desc,
3650 u_int ndesc, bus_addr_t ofs, bus_addr_t physbase)
3651 {
3652 tdb->tdb_ndesc = ndesc;
3653 tdb->tdb_desc = desc;
3654 tdb->tdb_physbase = physbase;
3655 tdb->tdb_ofs = ofs;
3656
3657 bzero(tdb->tdb_desc, sizeof(tdb->tdb_desc[0]) * tdb->tdb_ndesc);
3658
3659 rtw_txdesc_blk_init(tdb);
3660 tdb->tdb_next = 0;
3661 }
3662
3663 void
rtw_txdesc_blk_setup_all(struct rtw_softc * sc)3664 rtw_txdesc_blk_setup_all(struct rtw_softc *sc)
3665 {
3666 rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRILO],
3667 &sc->sc_descs->hd_txlo[0], RTW_NTXDESCLO,
3668 RTW_RING_OFFSET(hd_txlo), RTW_RING_BASE(sc, hd_txlo));
3669
3670 rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRIMD],
3671 &sc->sc_descs->hd_txmd[0], RTW_NTXDESCMD,
3672 RTW_RING_OFFSET(hd_txmd), RTW_RING_BASE(sc, hd_txmd));
3673
3674 rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRIHI],
3675 &sc->sc_descs->hd_txhi[0], RTW_NTXDESCHI,
3676 RTW_RING_OFFSET(hd_txhi), RTW_RING_BASE(sc, hd_txhi));
3677
3678 rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRIBCN],
3679 &sc->sc_descs->hd_bcn[0], RTW_NTXDESCBCN,
3680 RTW_RING_OFFSET(hd_bcn), RTW_RING_BASE(sc, hd_bcn));
3681 }
3682
3683 int
rtw_rf_attach(struct rtw_softc * sc,int rfchipid)3684 rtw_rf_attach(struct rtw_softc *sc, int rfchipid)
3685 {
3686 struct rtw_bbpset *bb = &sc->sc_bbpset;
3687 int notsup = 0;
3688 const char *rfname, *paname = NULL;
3689 char scratch[sizeof("unknown 0xXX")];
3690
3691 switch (rfchipid) {
3692 case RTW_RFCHIPID_RTL8225:
3693 rfname = "RTL8225";
3694 sc->sc_pwrstate_cb = rtw_rtl_pwrstate;
3695 sc->sc_rf_init = rtw_rtl8255_init;
3696 sc->sc_rf_pwrstate = rtw_rtl8225_pwrstate;
3697 sc->sc_rf_tune = rtw_rtl8225_tune;
3698 sc->sc_rf_txpower = rtw_rtl8225_txpower;
3699 break;
3700 case RTW_RFCHIPID_RTL8255:
3701 rfname = "RTL8255";
3702 sc->sc_pwrstate_cb = rtw_rtl_pwrstate;
3703 sc->sc_rf_init = rtw_rtl8255_init;
3704 sc->sc_rf_pwrstate = rtw_rtl8255_pwrstate;
3705 sc->sc_rf_tune = rtw_rtl8255_tune;
3706 sc->sc_rf_txpower = rtw_rtl8255_txpower;
3707 break;
3708 case RTW_RFCHIPID_MAXIM2820:
3709 rfname = "MAX2820"; /* guess */
3710 paname = "MAX2422"; /* guess */
3711 /* XXX magic */
3712 bb->bb_antatten = RTW_BBP_ANTATTEN_MAXIM_MAGIC;
3713 bb->bb_chestlim = 0x00;
3714 bb->bb_chsqlim = 0x9f;
3715 bb->bb_ifagcdet = 0x64;
3716 bb->bb_ifagcini = 0x90;
3717 bb->bb_ifagclimit = 0x1a;
3718 bb->bb_lnadet = 0xf8;
3719 bb->bb_sys1 = 0x88;
3720 bb->bb_sys2 = 0x47;
3721 bb->bb_sys3 = 0x9b;
3722 bb->bb_trl = 0x88;
3723 bb->bb_txagc = 0x08;
3724 sc->sc_pwrstate_cb = rtw_maxim_pwrstate;
3725 sc->sc_rf_init = rtw_max2820_init;
3726 sc->sc_rf_pwrstate = rtw_max2820_pwrstate;
3727 sc->sc_rf_tune = rtw_max2820_tune;
3728 sc->sc_rf_txpower = rtw_max2820_txpower;
3729 break;
3730 case RTW_RFCHIPID_PHILIPS:
3731 rfname = "SA2400A";
3732 paname = "SA2411";
3733 /* XXX magic */
3734 bb->bb_antatten = RTW_BBP_ANTATTEN_PHILIPS_MAGIC;
3735 bb->bb_chestlim = 0x00;
3736 bb->bb_chsqlim = 0xa0;
3737 bb->bb_ifagcdet = 0x64;
3738 bb->bb_ifagcini = 0x90;
3739 bb->bb_ifagclimit = 0x1a;
3740 bb->bb_lnadet = 0xe0;
3741 bb->bb_sys1 = 0x98;
3742 bb->bb_sys2 = 0x47;
3743 bb->bb_sys3 = 0x90;
3744 bb->bb_trl = 0x88;
3745 bb->bb_txagc = 0x38;
3746 sc->sc_pwrstate_cb = rtw_philips_pwrstate;
3747 sc->sc_rf_init = rtw_sa2400_init;
3748 sc->sc_rf_pwrstate = rtw_sa2400_pwrstate;
3749 sc->sc_rf_tune = rtw_sa2400_tune;
3750 sc->sc_rf_txpower = rtw_sa2400_txpower;
3751 break;
3752 case RTW_RFCHIPID_RFMD2948:
3753 /* this is the same front-end as an atw(4)! */
3754 rfname = "RFMD RF2948B, " /* mentioned in Realtek docs */
3755 "LNA: RFMD RF2494, " /* mentioned in Realtek docs */
3756 "SYN: Silicon Labs Si4126"; /* inferred from
3757 * reference driver
3758 */
3759 paname = "RF2189"; /* mentioned in Realtek docs */
3760 /* XXX RFMD has no RF constructor */
3761 sc->sc_pwrstate_cb = rtw_rfmd_pwrstate;
3762 notsup = 1;
3763 break;
3764 case RTW_RFCHIPID_GCT: /* this combo seen in the wild */
3765 rfname = "GRF5101";
3766 paname = "WS9901";
3767 /* XXX magic */
3768 bb->bb_antatten = RTW_BBP_ANTATTEN_GCT_MAGIC;
3769 bb->bb_chestlim = 0x00;
3770 bb->bb_chsqlim = 0xa0;
3771 bb->bb_ifagcdet = 0x64;
3772 bb->bb_ifagcini = 0x90;
3773 bb->bb_ifagclimit = 0x1e;
3774 bb->bb_lnadet = 0xc0;
3775 bb->bb_sys1 = 0xa8;
3776 bb->bb_sys2 = 0x47;
3777 bb->bb_sys3 = 0x9b;
3778 bb->bb_trl = 0x88;
3779 bb->bb_txagc = 0x08;
3780 sc->sc_pwrstate_cb = rtw_maxim_pwrstate;
3781 sc->sc_rf_init = rtw_grf5101_init;
3782 sc->sc_rf_pwrstate = rtw_grf5101_pwrstate;
3783 sc->sc_rf_tune = rtw_grf5101_tune;
3784 sc->sc_rf_txpower = rtw_grf5101_txpower;
3785 break;
3786 case RTW_RFCHIPID_INTERSIL:
3787 rfname = "HFA3873"; /* guess */
3788 paname = "Intersil <unknown>";
3789 notsup = 1;
3790 break;
3791 default:
3792 snprintf(scratch, sizeof(scratch), "unknown 0x%02x", rfchipid);
3793 rfname = scratch;
3794 notsup = 1;
3795 }
3796
3797 printf("radio %s, ", rfname);
3798 if (paname != NULL)
3799 printf("amp %s, ", paname);
3800
3801 return (notsup);
3802 }
3803
3804 /* Revision C and later use a different PHY delay setting than
3805 * revisions A and B.
3806 */
3807 u_int8_t
rtw_check_phydelay(struct rtw_regs * regs,u_int32_t rcr0)3808 rtw_check_phydelay(struct rtw_regs *regs, u_int32_t rcr0)
3809 {
3810 #define REVAB (RTW_RCR_MXDMA_UNLIMITED | RTW_RCR_AICV)
3811 #define REVC (REVAB | RTW8180_RCR_RXFTH_WHOLE)
3812
3813 u_int8_t phydelay = LSHIFT(0x6, RTW_PHYDELAY_PHYDELAY);
3814
3815 RTW_WRITE(regs, RTW_RCR, REVAB);
3816 RTW_WBW(regs, RTW_RCR, RTW_RCR);
3817 RTW_WRITE(regs, RTW_RCR, REVC);
3818
3819 RTW_WBR(regs, RTW_RCR, RTW_RCR);
3820 if ((RTW_READ(regs, RTW_RCR) & REVC) == REVC)
3821 phydelay |= RTW_PHYDELAY_REVC_MAGIC;
3822
3823 RTW_WRITE(regs, RTW_RCR, rcr0); /* restore RCR */
3824 RTW_SYNC(regs, RTW_RCR, RTW_RCR);
3825
3826 return phydelay;
3827 #undef REVC
3828 }
3829
3830 void
rtw_attach(struct rtw_softc * sc)3831 rtw_attach(struct rtw_softc *sc)
3832 {
3833 struct ieee80211com *ic = &sc->sc_ic;
3834 struct rtw_txsoft_blk *tsb;
3835 struct rtw_mtbl *mtbl;
3836 struct rtw_srom *sr;
3837 const char *vername;
3838 struct ifnet *ifp;
3839 char scratch[sizeof("unknown 0xXXXXXXXX")];
3840 int pri, rc, i;
3841
3842
3843 /* Use default DMA memory access */
3844 if (sc->sc_regs.r_read8 == NULL) {
3845 sc->sc_regs.r_read8 = rtw_read8;
3846 sc->sc_regs.r_read16 = rtw_read16;
3847 sc->sc_regs.r_read32 = rtw_read32;
3848 sc->sc_regs.r_write8 = rtw_write8;
3849 sc->sc_regs.r_write16 = rtw_write16;
3850 sc->sc_regs.r_write32 = rtw_write32;
3851 sc->sc_regs.r_barrier = rtw_barrier;
3852 }
3853
3854 sc->sc_hwverid = RTW_READ(&sc->sc_regs, RTW_TCR) & RTW_TCR_HWVERID_MASK;
3855 switch (sc->sc_hwverid) {
3856 case RTW_TCR_HWVERID_RTL8185:
3857 vername = "RTL8185";
3858 sc->sc_flags |= RTW_F_RTL8185;
3859 break;
3860 case RTW_TCR_HWVERID_RTL8180F:
3861 vername = "RTL8180F";
3862 break;
3863 case RTW_TCR_HWVERID_RTL8180D:
3864 vername = "RTL8180D";
3865 break;
3866 default:
3867 snprintf(scratch, sizeof(scratch), "unknown 0x%08x",
3868 sc->sc_hwverid);
3869 vername = scratch;
3870 break;
3871 }
3872
3873 printf("%s: ver %s, ", sc->sc_dev.dv_xname, vername);
3874
3875 rc = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct rtw_descs),
3876 RTW_DESC_ALIGNMENT, 0, &sc->sc_desc_segs, 1, &sc->sc_desc_nsegs,
3877 0);
3878
3879 if (rc != 0) {
3880 printf("\n%s: could not allocate hw descriptors, error %d\n",
3881 sc->sc_dev.dv_xname, rc);
3882 goto fail0;
3883 }
3884
3885 rc = bus_dmamem_map(sc->sc_dmat, &sc->sc_desc_segs,
3886 sc->sc_desc_nsegs, sizeof(struct rtw_descs),
3887 (caddr_t*)&sc->sc_descs, BUS_DMA_COHERENT);
3888
3889 if (rc != 0) {
3890 printf("\n%s: can't map hw descriptors, error %d\n",
3891 sc->sc_dev.dv_xname, rc);
3892 goto fail1;
3893 }
3894
3895 rc = bus_dmamap_create(sc->sc_dmat, sizeof(struct rtw_descs), 1,
3896 sizeof(struct rtw_descs), 0, 0, &sc->sc_desc_dmamap);
3897
3898 if (rc != 0) {
3899 printf("\n%s: could not create DMA map for hw descriptors, "
3900 "error %d\n", sc->sc_dev.dv_xname, rc);
3901 goto fail2;
3902 }
3903
3904 sc->sc_rxdesc_blk.rdb_dmat = sc->sc_dmat;
3905 sc->sc_rxdesc_blk.rdb_dmamap = sc->sc_desc_dmamap;
3906
3907 for (pri = 0; pri < RTW_NTXPRI; pri++) {
3908 sc->sc_txdesc_blk[pri].tdb_dmat = sc->sc_dmat;
3909 sc->sc_txdesc_blk[pri].tdb_dmamap = sc->sc_desc_dmamap;
3910 }
3911
3912 rc = bus_dmamap_load(sc->sc_dmat, sc->sc_desc_dmamap, sc->sc_descs,
3913 sizeof(struct rtw_descs), NULL, 0);
3914
3915 if (rc != 0) {
3916 printf("\n%s: could not load DMA map for hw descriptors, "
3917 "error %d\n", sc->sc_dev.dv_xname, rc);
3918 goto fail3;
3919 }
3920
3921 if (rtw_txsoft_blk_setup_all(sc) != 0)
3922 goto fail4;
3923
3924 rtw_txdesc_blk_setup_all(sc);
3925
3926 sc->sc_rxdesc_blk.rdb_desc = &sc->sc_descs->hd_rx[0];
3927
3928 for (pri = 0; pri < RTW_NTXPRI; pri++) {
3929 tsb = &sc->sc_txsoft_blk[pri];
3930
3931 if ((rc = rtw_txdesc_dmamaps_create(sc->sc_dmat,
3932 &tsb->tsb_desc[0], tsb->tsb_ndesc)) != 0) {
3933 printf("\n%s: could not load DMA map for "
3934 "hw tx descriptors, error %d\n",
3935 sc->sc_dev.dv_xname, rc);
3936 goto fail5;
3937 }
3938 }
3939
3940 if ((rc = rtw_rxdesc_dmamaps_create(sc->sc_dmat, &sc->sc_rxsoft[0],
3941 RTW_RXQLEN)) != 0) {
3942 printf("\n%s: could not load DMA map for hw rx descriptors, "
3943 "error %d\n", sc->sc_dev.dv_xname, rc);
3944 goto fail6;
3945 }
3946
3947 /* Reset the chip to a known state. */
3948 if (rtw_reset(sc) != 0)
3949 goto fail7;
3950
3951 sc->sc_rcr = RTW_READ(&sc->sc_regs, RTW_RCR);
3952
3953 if ((sc->sc_rcr & RTW_RCR_9356SEL) != 0)
3954 sc->sc_flags |= RTW_F_9356SROM;
3955
3956 if (rtw_srom_read(&sc->sc_regs, sc->sc_flags, &sc->sc_srom,
3957 sc->sc_dev.dv_xname) != 0)
3958 goto fail7;
3959
3960 if (rtw_srom_parse(sc) != 0) {
3961 printf("\n%s: attach failed, malformed serial ROM\n",
3962 sc->sc_dev.dv_xname);
3963 goto fail8;
3964 }
3965
3966 RTW_DPRINTF(RTW_DEBUG_ATTACH, ("%s: %s PHY\n", sc->sc_dev.dv_xname,
3967 ((sc->sc_flags & RTW_F_DIGPHY) != 0) ? "digital" : "analog"));
3968
3969 RTW_DPRINTF(RTW_DEBUG_ATTACH, ("%s: CS threshold %u\n",
3970 sc->sc_dev.dv_xname, sc->sc_csthr));
3971
3972 if ((rtw_rf_attach(sc, sc->sc_rfchipid)) != 0) {
3973 printf("\n%s: attach failed, could not attach RF\n",
3974 sc->sc_dev.dv_xname);
3975 goto fail8;
3976 }
3977
3978 sc->sc_phydelay = rtw_check_phydelay(&sc->sc_regs, sc->sc_rcr);
3979
3980 RTW_DPRINTF(RTW_DEBUG_ATTACH,
3981 ("%s: PHY delay %d\n", sc->sc_dev.dv_xname, sc->sc_phydelay));
3982
3983 if (sc->sc_locale == RTW_LOCALE_UNKNOWN)
3984 rtw_identify_country(&sc->sc_regs, &sc->sc_locale);
3985
3986 for (i = 1; i <= 14; i++) {
3987 sc->sc_ic.ic_channels[i].ic_flags = IEEE80211_CHAN_B;
3988 sc->sc_ic.ic_channels[i].ic_freq =
3989 ieee80211_ieee2mhz(i, sc->sc_ic.ic_channels[i].ic_flags);
3990 }
3991
3992 if (rtw_identify_sta(&sc->sc_regs, &sc->sc_ic.ic_myaddr,
3993 sc->sc_dev.dv_xname) != 0)
3994 goto fail8;
3995
3996 ifp = &sc->sc_if;
3997 (void)memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
3998 ifp->if_softc = sc;
3999 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
4000 ifp->if_ioctl = rtw_ioctl;
4001 ifp->if_start = rtw_start;
4002 ifp->if_watchdog = rtw_watchdog;
4003
4004
4005 ic->ic_phytype = IEEE80211_T_DS;
4006 ic->ic_opmode = IEEE80211_M_STA;
4007 ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_MONITOR | IEEE80211_C_WEP;
4008 #ifndef IEEE80211_STA_ONLY
4009 ic->ic_caps |= IEEE80211_C_HOSTAP | IEEE80211_C_IBSS;
4010 #endif
4011 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
4012
4013 rtw_led_attach(&sc->sc_led_state, (void *)sc);
4014
4015 /*
4016 * Call MI attach routines.
4017 */
4018 if_attach(&sc->sc_if);
4019 ieee80211_ifattach(&sc->sc_if);
4020
4021 mtbl = &sc->sc_mtbl;
4022 mtbl->mt_newstate = ic->ic_newstate;
4023 ic->ic_newstate = rtw_newstate;
4024
4025 #ifndef IEEE80211_STA_ONLY
4026 mtbl->mt_recv_mgmt = ic->ic_recv_mgmt;
4027 ic->ic_recv_mgmt = rtw_recv_mgmt;
4028 #endif
4029
4030 mtbl->mt_node_free = ic->ic_node_free;
4031 ic->ic_node_free = rtw_node_free;
4032
4033 mtbl->mt_node_alloc = ic->ic_node_alloc;
4034 ic->ic_node_alloc = rtw_node_alloc;
4035
4036 /* possibly we should fill in our own sc_send_prresp, since
4037 * the RTL8180 is probably sending probe responses in ad hoc
4038 * mode.
4039 */
4040
4041 /* complete initialization */
4042 ieee80211_media_init(&sc->sc_if, rtw_media_change, rtw_media_status);
4043 timeout_set(&sc->sc_scan_to, rtw_next_scan, sc);
4044
4045 #if NBPFILTER > 0
4046 bzero(&sc->sc_rxtapu, sizeof(sc->sc_rxtapu));
4047 sc->sc_rxtap.rr_ihdr.it_len = sizeof(sc->sc_rxtapu);
4048 sc->sc_rxtap.rr_ihdr.it_present = RTW_RX_RADIOTAP_PRESENT;
4049
4050 bzero(&sc->sc_txtapu, sizeof(sc->sc_txtapu));
4051 sc->sc_txtap.rt_ihdr.it_len = sizeof(sc->sc_txtapu);
4052 sc->sc_txtap.rt_ihdr.it_present = RTW_TX_RADIOTAP_PRESENT;
4053
4054 bpfattach(&sc->sc_radiobpf, &sc->sc_ic.ic_if, DLT_IEEE802_11_RADIO,
4055 sizeof(struct ieee80211_frame) + 64);
4056 #endif
4057 return;
4058
4059 fail8:
4060 sr = &sc->sc_srom;
4061 if (sr->sr_content != NULL) {
4062 free(sr->sr_content, M_DEVBUF, sr->sr_size);
4063 sr->sr_content = NULL;
4064 }
4065 sr->sr_size = 0;
4066
4067 fail7:
4068 rtw_rxdesc_dmamaps_destroy(sc->sc_dmat, &sc->sc_rxsoft[0],
4069 RTW_RXQLEN);
4070
4071 fail6:
4072 for (pri = 0; pri < RTW_NTXPRI; pri++) {
4073 rtw_txdesc_dmamaps_destroy(sc->sc_dmat,
4074 sc->sc_txsoft_blk[pri].tsb_desc,
4075 sc->sc_txsoft_blk[pri].tsb_ndesc);
4076 }
4077
4078 fail5:
4079 rtw_txsoft_blk_cleanup_all(sc);
4080
4081 fail4:
4082 bus_dmamap_unload(sc->sc_dmat, sc->sc_desc_dmamap);
4083 fail3:
4084 bus_dmamap_destroy(sc->sc_dmat, sc->sc_desc_dmamap);
4085 fail2:
4086 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_descs,
4087 sizeof(struct rtw_descs));
4088 fail1:
4089 bus_dmamem_free(sc->sc_dmat, &sc->sc_desc_segs,
4090 sc->sc_desc_nsegs);
4091 fail0:
4092 return;
4093 }
4094
4095 int
rtw_detach(struct rtw_softc * sc)4096 rtw_detach(struct rtw_softc *sc)
4097 {
4098 sc->sc_flags |= RTW_F_INVALID;
4099
4100 timeout_del(&sc->sc_scan_to);
4101
4102 rtw_stop(&sc->sc_if, 1);
4103
4104 ieee80211_ifdetach(&sc->sc_if);
4105 if_detach(&sc->sc_if);
4106
4107 return 0;
4108 }
4109
4110 /*
4111 * PHY specific functions
4112 */
4113
4114 int
rtw_bbp_preinit(struct rtw_regs * regs,u_int antatten0,int dflantb,u_int freq)4115 rtw_bbp_preinit(struct rtw_regs *regs, u_int antatten0, int dflantb,
4116 u_int freq)
4117 {
4118 u_int antatten = antatten0;
4119 if (dflantb)
4120 antatten |= RTW_BBP_ANTATTEN_DFLANTB;
4121 if (freq == 2484) /* channel 14 */
4122 antatten |= RTW_BBP_ANTATTEN_CHAN14;
4123 return rtw_bbp_write(regs, RTW_BBP_ANTATTEN, antatten);
4124 }
4125
4126 int
rtw_bbp_init(struct rtw_regs * regs,struct rtw_bbpset * bb,int antdiv,int dflantb,u_int8_t cs_threshold,u_int freq)4127 rtw_bbp_init(struct rtw_regs *regs, struct rtw_bbpset *bb, int antdiv,
4128 int dflantb, u_int8_t cs_threshold, u_int freq)
4129 {
4130 int rc;
4131 u_int32_t sys2, sys3;
4132
4133 sys2 = bb->bb_sys2;
4134 if (antdiv)
4135 sys2 |= RTW_BBP_SYS2_ANTDIV;
4136 sys3 = bb->bb_sys3 |
4137 LSHIFT(cs_threshold, RTW_BBP_SYS3_CSTHRESH_MASK);
4138
4139 #define RTW_BBP_WRITE_OR_RETURN(reg, val) \
4140 if ((rc = rtw_bbp_write(regs, reg, val)) != 0) \
4141 return rc;
4142
4143 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_SYS1, bb->bb_sys1);
4144 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_TXAGC, bb->bb_txagc);
4145 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_LNADET, bb->bb_lnadet);
4146 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_IFAGCINI, bb->bb_ifagcini);
4147 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_IFAGCLIMIT, bb->bb_ifagclimit);
4148 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_IFAGCDET, bb->bb_ifagcdet);
4149
4150 if ((rc = rtw_bbp_preinit(regs, bb->bb_antatten, dflantb, freq)) != 0)
4151 return rc;
4152
4153 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_TRL, bb->bb_trl);
4154 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_SYS2, sys2);
4155 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_SYS3, sys3);
4156 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_CHESTLIM, bb->bb_chestlim);
4157 RTW_BBP_WRITE_OR_RETURN(RTW_BBP_CHSQLIM, bb->bb_chsqlim);
4158 return 0;
4159 }
4160
4161 int
rtw_sa2400_txpower(struct rtw_softc * sc,u_int8_t opaque_txpower)4162 rtw_sa2400_txpower(struct rtw_softc *sc, u_int8_t opaque_txpower)
4163 {
4164 return rtw_rf_macwrite(sc, SA2400_TX, opaque_txpower);
4165 }
4166
4167 /* make sure we're using the same settings as the reference driver */
4168 void
rtw_verify_syna(u_int freq,u_int32_t val)4169 rtw_verify_syna(u_int freq, u_int32_t val)
4170 {
4171 u_int32_t expected_val = ~val;
4172
4173 switch (freq) {
4174 case 2412:
4175 expected_val = 0x0000096c; /* ch 1 */
4176 break;
4177 case 2417:
4178 expected_val = 0x00080970; /* ch 2 */
4179 break;
4180 case 2422:
4181 expected_val = 0x00100974; /* ch 3 */
4182 break;
4183 case 2427:
4184 expected_val = 0x00180978; /* ch 4 */
4185 break;
4186 case 2432:
4187 expected_val = 0x00000980; /* ch 5 */
4188 break;
4189 case 2437:
4190 expected_val = 0x00080984; /* ch 6 */
4191 break;
4192 case 2442:
4193 expected_val = 0x00100988; /* ch 7 */
4194 break;
4195 case 2447:
4196 expected_val = 0x0018098c; /* ch 8 */
4197 break;
4198 case 2452:
4199 expected_val = 0x00000994; /* ch 9 */
4200 break;
4201 case 2457:
4202 expected_val = 0x00080998; /* ch 10 */
4203 break;
4204 case 2462:
4205 expected_val = 0x0010099c; /* ch 11 */
4206 break;
4207 case 2467:
4208 expected_val = 0x001809a0; /* ch 12 */
4209 break;
4210 case 2472:
4211 expected_val = 0x000009a8; /* ch 13 */
4212 break;
4213 case 2484:
4214 expected_val = 0x000009b4; /* ch 14 */
4215 break;
4216 }
4217 KASSERT(val == expected_val);
4218 }
4219
4220 /* freq is in MHz */
4221 int
rtw_sa2400_tune(struct rtw_softc * sc,u_int freq)4222 rtw_sa2400_tune(struct rtw_softc *sc, u_int freq)
4223 {
4224 int rc;
4225 u_int32_t syna, synb, sync;
4226
4227 /* XO = 44MHz, R = 11, hence N is in units of XO / R = 4MHz.
4228 *
4229 * The channel spacing (5MHz) is not divisible by 4MHz, so
4230 * we set the fractional part of N to compensate.
4231 */
4232 int n = freq / 4, nf = (freq % 4) * 2;
4233
4234 syna = LSHIFT(nf, SA2400_SYNA_NF_MASK) | LSHIFT(n, SA2400_SYNA_N_MASK);
4235 rtw_verify_syna(freq, syna);
4236
4237 /* Divide the 44MHz crystal down to 4MHz. Set the fractional
4238 * compensation charge pump value to agree with the fractional
4239 * modulus.
4240 */
4241 synb = LSHIFT(11, SA2400_SYNB_R_MASK) | SA2400_SYNB_L_NORMAL |
4242 SA2400_SYNB_ON | SA2400_SYNB_ONE |
4243 LSHIFT(80, SA2400_SYNB_FC_MASK); /* agrees w/ SA2400_SYNA_FM = 0 */
4244
4245 sync = SA2400_SYNC_CP_NORMAL;
4246
4247 if ((rc = rtw_rf_macwrite(sc, SA2400_SYNA, syna)) != 0)
4248 return rc;
4249 if ((rc = rtw_rf_macwrite(sc, SA2400_SYNB, synb)) != 0)
4250 return rc;
4251 if ((rc = rtw_rf_macwrite(sc, SA2400_SYNC, sync)) != 0)
4252 return rc;
4253 return rtw_rf_macwrite(sc, SA2400_SYND, 0x0);
4254 }
4255
4256 int
rtw_sa2400_pwrstate(struct rtw_softc * sc,enum rtw_pwrstate power)4257 rtw_sa2400_pwrstate(struct rtw_softc *sc, enum rtw_pwrstate power)
4258 {
4259 u_int32_t opmode;
4260 opmode = SA2400_OPMODE_DEFAULTS;
4261 switch (power) {
4262 case RTW_ON:
4263 opmode |= SA2400_OPMODE_MODE_TXRX;
4264 break;
4265 case RTW_SLEEP:
4266 opmode |= SA2400_OPMODE_MODE_WAIT;
4267 break;
4268 case RTW_OFF:
4269 opmode |= SA2400_OPMODE_MODE_SLEEP;
4270 break;
4271 }
4272
4273 if (sc->sc_flags & RTW_F_DIGPHY)
4274 opmode |= SA2400_OPMODE_DIGIN;
4275
4276 return rtw_rf_macwrite(sc, SA2400_OPMODE, opmode);
4277 }
4278
4279 int
rtw_sa2400_vcocal_start(struct rtw_softc * sc,int start)4280 rtw_sa2400_vcocal_start(struct rtw_softc *sc, int start)
4281 {
4282 u_int32_t opmode;
4283
4284 opmode = SA2400_OPMODE_DEFAULTS;
4285 if (start)
4286 opmode |= SA2400_OPMODE_MODE_VCOCALIB;
4287 else
4288 opmode |= SA2400_OPMODE_MODE_SLEEP;
4289
4290 if (sc->sc_flags & RTW_F_DIGPHY)
4291 opmode |= SA2400_OPMODE_DIGIN;
4292
4293 return rtw_rf_macwrite(sc, SA2400_OPMODE, opmode);
4294 }
4295
4296 int
rtw_sa2400_vco_calibration(struct rtw_softc * sc)4297 rtw_sa2400_vco_calibration(struct rtw_softc *sc)
4298 {
4299 int rc;
4300 /* calibrate VCO */
4301 if ((rc = rtw_sa2400_vcocal_start(sc, 1)) != 0)
4302 return rc;
4303 DELAY(2200); /* 2.2 milliseconds */
4304 /* XXX superfluous: SA2400 automatically entered SLEEP mode. */
4305 return rtw_sa2400_vcocal_start(sc, 0);
4306 }
4307
4308 int
rtw_sa2400_filter_calibration(struct rtw_softc * sc)4309 rtw_sa2400_filter_calibration(struct rtw_softc *sc)
4310 {
4311 u_int32_t opmode;
4312
4313 opmode = SA2400_OPMODE_DEFAULTS | SA2400_OPMODE_MODE_FCALIB;
4314 if (sc->sc_flags & RTW_F_DIGPHY)
4315 opmode |= SA2400_OPMODE_DIGIN;
4316
4317 return rtw_rf_macwrite(sc, SA2400_OPMODE, opmode);
4318 }
4319
4320 int
rtw_sa2400_dc_calibration(struct rtw_softc * sc)4321 rtw_sa2400_dc_calibration(struct rtw_softc *sc)
4322 {
4323 int rc;
4324 u_int32_t dccal;
4325
4326 rtw_continuous_tx_enable(sc, 1);
4327
4328 dccal = SA2400_OPMODE_DEFAULTS | SA2400_OPMODE_MODE_TXRX;
4329
4330 rc = rtw_rf_macwrite(sc, SA2400_OPMODE, dccal);
4331
4332 if (rc != 0)
4333 return rc;
4334
4335 DELAY(5); /* DCALIB after being in Tx mode for 5
4336 * microseconds
4337 */
4338
4339 dccal &= ~SA2400_OPMODE_MODE_MASK;
4340 dccal |= SA2400_OPMODE_MODE_DCALIB;
4341
4342 rc = rtw_rf_macwrite(sc, SA2400_OPMODE, dccal);
4343 if (rc != 0)
4344 return rc;
4345
4346 DELAY(20); /* calibration takes at most 20 microseconds */
4347
4348 rtw_continuous_tx_enable(sc, 0);
4349
4350 return 0;
4351 }
4352
4353 int
rtw_sa2400_calibrate(struct rtw_softc * sc,u_int freq)4354 rtw_sa2400_calibrate(struct rtw_softc *sc, u_int freq)
4355 {
4356 int i, rc;
4357
4358 /* XXX reference driver calibrates VCO twice. Is it a bug? */
4359 for (i = 0; i < 2; i++) {
4360 if ((rc = rtw_sa2400_vco_calibration(sc)) != 0)
4361 return rc;
4362 }
4363 /* VCO calibration erases synthesizer registers, so re-tune */
4364 if ((rc = rtw_sa2400_tune(sc, freq)) != 0)
4365 return rc;
4366 if ((rc = rtw_sa2400_filter_calibration(sc)) != 0)
4367 return rc;
4368 /* analog PHY needs DC calibration */
4369 if (!(sc->sc_flags & RTW_F_DIGPHY))
4370 return rtw_sa2400_dc_calibration(sc);
4371 return 0;
4372 }
4373
4374 int
rtw_sa2400_init(struct rtw_softc * sc,u_int freq,u_int8_t opaque_txpower,enum rtw_pwrstate power)4375 rtw_sa2400_init(struct rtw_softc *sc, u_int freq, u_int8_t opaque_txpower,
4376 enum rtw_pwrstate power)
4377 {
4378 int rc;
4379 u_int32_t agc, manrx;
4380
4381 if ((rc = rtw_sa2400_txpower(sc, opaque_txpower)) != 0)
4382 return rc;
4383
4384 /* skip configuration if it's time to sleep or to power-down. */
4385 if (power == RTW_SLEEP || power == RTW_OFF)
4386 return rtw_sa2400_pwrstate(sc, power);
4387
4388 /* go to sleep for configuration */
4389 if ((rc = rtw_sa2400_pwrstate(sc, RTW_SLEEP)) != 0)
4390 return rc;
4391
4392 if ((rc = rtw_sa2400_tune(sc, freq)) != 0)
4393 return rc;
4394
4395 agc = LSHIFT(25, SA2400_AGC_MAXGAIN_MASK);
4396 agc |= LSHIFT(7, SA2400_AGC_BBPDELAY_MASK);
4397 agc |= LSHIFT(15, SA2400_AGC_LNADELAY_MASK);
4398 agc |= LSHIFT(27, SA2400_AGC_RXONDELAY_MASK);
4399
4400 if ((rc = rtw_rf_macwrite(sc, SA2400_AGC, agc)) != 0)
4401 return rc;
4402
4403 /* XXX we are not supposed to be in RXMGC mode when we do this? */
4404 manrx = SA2400_MANRX_AHSN;
4405 manrx |= SA2400_MANRX_TEN;
4406 manrx |= LSHIFT(1023, SA2400_MANRX_RXGAIN_MASK);
4407
4408 if ((rc = rtw_rf_macwrite(sc, SA2400_MANRX, manrx)) != 0)
4409 return rc;
4410
4411 if ((rc = rtw_sa2400_calibrate(sc, freq)) != 0)
4412 return rc;
4413
4414 /* enter Tx/Rx mode */
4415 return rtw_sa2400_pwrstate(sc, power);
4416 }
4417
4418 /* freq is in MHz */
4419 int
rtw_max2820_tune(struct rtw_softc * sc,u_int freq)4420 rtw_max2820_tune(struct rtw_softc *sc, u_int freq)
4421 {
4422 if (freq < 2400 || freq > 2499)
4423 return -1;
4424
4425 return rtw_rf_hostwrite(sc, MAX2820_CHANNEL,
4426 LSHIFT(freq - 2400, MAX2820_CHANNEL_CF_MASK));
4427 }
4428
4429 int
rtw_max2820_init(struct rtw_softc * sc,u_int freq,u_int8_t opaque_txpower,enum rtw_pwrstate power)4430 rtw_max2820_init(struct rtw_softc *sc, u_int freq, u_int8_t opaque_txpower,
4431 enum rtw_pwrstate power)
4432 {
4433 int rc;
4434
4435 if ((rc = rtw_rf_hostwrite(sc, MAX2820_TEST,
4436 MAX2820_TEST_DEFAULT)) != 0)
4437 return rc;
4438
4439 if ((rc = rtw_rf_hostwrite(sc, MAX2820_ENABLE,
4440 MAX2820_ENABLE_DEFAULT)) != 0)
4441 return rc;
4442
4443 /* skip configuration if it's time to sleep or to power-down. */
4444 if ((rc = rtw_max2820_pwrstate(sc, power)) != 0)
4445 return rc;
4446 else if (power == RTW_OFF || power == RTW_SLEEP)
4447 return 0;
4448
4449 if ((rc = rtw_rf_hostwrite(sc, MAX2820_SYNTH,
4450 MAX2820_SYNTH_R_44MHZ)) != 0)
4451 return rc;
4452
4453 if ((rc = rtw_max2820_tune(sc, freq)) != 0)
4454 return rc;
4455
4456 /* XXX The MAX2820 datasheet indicates that 1C and 2C should not
4457 * be changed from 7, however, the reference driver sets them
4458 * to 4 and 1, respectively.
4459 */
4460 if ((rc = rtw_rf_hostwrite(sc, MAX2820_RECEIVE,
4461 MAX2820_RECEIVE_DL_DEFAULT |
4462 LSHIFT(4, MAX2820A_RECEIVE_1C_MASK) |
4463 LSHIFT(1, MAX2820A_RECEIVE_2C_MASK))) != 0)
4464 return rc;
4465
4466 return rtw_rf_hostwrite(sc, MAX2820_TRANSMIT,
4467 MAX2820_TRANSMIT_PA_DEFAULT);
4468 }
4469
4470 int
rtw_max2820_txpower(struct rtw_softc * sc,u_int8_t opaque_txpower)4471 rtw_max2820_txpower(struct rtw_softc *sc, u_int8_t opaque_txpower)
4472 {
4473 /* TBD */
4474 return 0;
4475 }
4476
4477 int
rtw_max2820_pwrstate(struct rtw_softc * sc,enum rtw_pwrstate power)4478 rtw_max2820_pwrstate(struct rtw_softc *sc, enum rtw_pwrstate power)
4479 {
4480 uint32_t enable;
4481
4482 switch (power) {
4483 case RTW_OFF:
4484 case RTW_SLEEP:
4485 default:
4486 enable = 0x0;
4487 break;
4488 case RTW_ON:
4489 enable = MAX2820_ENABLE_DEFAULT;
4490 break;
4491 }
4492 return rtw_rf_hostwrite(sc, MAX2820_ENABLE, enable);
4493 }
4494
4495 int
rtw_grf5101_init(struct rtw_softc * sc,u_int freq,u_int8_t opaque_txpower,enum rtw_pwrstate power)4496 rtw_grf5101_init(struct rtw_softc *sc, u_int freq, u_int8_t opaque_txpower,
4497 enum rtw_pwrstate power)
4498 {
4499 int rc;
4500
4501 /*
4502 * These values have been derived from the rtl8180-sa2400 Linux driver.
4503 * It is unknown what they all do, GCT refuse to release any documentation
4504 * so these are more than likely sub optimal settings
4505 */
4506
4507 rtw_rf_macwrite(sc, 0x01, 0x1a23);
4508 rtw_rf_macwrite(sc, 0x02, 0x4971);
4509 rtw_rf_macwrite(sc, 0x03, 0x41de);
4510 rtw_rf_macwrite(sc, 0x04, 0x2d80);
4511
4512 rtw_rf_macwrite(sc, 0x05, 0x61ff);
4513
4514 rtw_rf_macwrite(sc, 0x06, 0x0);
4515
4516 rtw_rf_macwrite(sc, 0x08, 0x7533);
4517 rtw_rf_macwrite(sc, 0x09, 0xc401);
4518 rtw_rf_macwrite(sc, 0x0a, 0x0);
4519 rtw_rf_macwrite(sc, 0x0c, 0x1c7);
4520 rtw_rf_macwrite(sc, 0x0d, 0x29d3);
4521 rtw_rf_macwrite(sc, 0x0e, 0x2e8);
4522 rtw_rf_macwrite(sc, 0x10, 0x192);
4523 rtw_rf_macwrite(sc, 0x11, 0x248);
4524 rtw_rf_macwrite(sc, 0x12, 0x0);
4525 rtw_rf_macwrite(sc, 0x13, 0x20c4);
4526 rtw_rf_macwrite(sc, 0x14, 0xf4fc);
4527 rtw_rf_macwrite(sc, 0x15, 0x0);
4528 rtw_rf_macwrite(sc, 0x16, 0x1500);
4529
4530 if ((rc = rtw_grf5101_txpower(sc, opaque_txpower)) != 0)
4531 return rc;
4532
4533 if ((rc = rtw_grf5101_tune(sc, freq)) != 0)
4534 return rc;
4535
4536 return (0);
4537 }
4538
4539 int
rtw_grf5101_tune(struct rtw_softc * sc,u_int freq)4540 rtw_grf5101_tune(struct rtw_softc *sc, u_int freq)
4541 {
4542 struct ieee80211com *ic = &sc->sc_ic;
4543 u_int channel = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
4544
4545 /* set channel */
4546 rtw_rf_macwrite(sc, 0x07, 0);
4547 rtw_rf_macwrite(sc, 0x0b, channel - 1);
4548 rtw_rf_macwrite(sc, 0x07, 0x1000);
4549
4550 return (0);
4551 }
4552
4553 int
rtw_grf5101_txpower(struct rtw_softc * sc,u_int8_t opaque_txpower)4554 rtw_grf5101_txpower(struct rtw_softc *sc, u_int8_t opaque_txpower)
4555 {
4556 rtw_rf_macwrite(sc, 0x15, 0);
4557 rtw_rf_macwrite(sc, 0x06, opaque_txpower);
4558 rtw_rf_macwrite(sc, 0x15, 0x10);
4559 rtw_rf_macwrite(sc, 0x15, 0x00);
4560
4561 return (0);
4562 }
4563
4564 int
rtw_grf5101_pwrstate(struct rtw_softc * sc,enum rtw_pwrstate power)4565 rtw_grf5101_pwrstate(struct rtw_softc *sc, enum rtw_pwrstate power)
4566 {
4567 switch (power) {
4568 case RTW_OFF:
4569 /* FALLTHROUGH */
4570 case RTW_SLEEP:
4571 rtw_rf_macwrite(sc, 0x07, 0x0000);
4572 rtw_rf_macwrite(sc, 0x1f, 0x0045);
4573 rtw_rf_macwrite(sc, 0x1f, 0x0005);
4574 rtw_rf_macwrite(sc, 0x00, 0x08e4);
4575 break;
4576 case RTW_ON:
4577 rtw_rf_macwrite(sc, 0x1f, 0x0001);
4578 DELAY(10);
4579 rtw_rf_macwrite(sc, 0x1f, 0x0001);
4580 DELAY(10);
4581 rtw_rf_macwrite(sc, 0x1f, 0x0041);
4582 DELAY(10);
4583 rtw_rf_macwrite(sc, 0x1f, 0x0061);
4584 DELAY(10);
4585 rtw_rf_macwrite(sc, 0x00, 0x0ae4);
4586 DELAY(10);
4587 rtw_rf_macwrite(sc, 0x07, 0x1000);
4588 DELAY(100);
4589 break;
4590 }
4591
4592 return 0;
4593 }
4594
4595 int
rtw_rtl8225_pwrstate(struct rtw_softc * sc,enum rtw_pwrstate power)4596 rtw_rtl8225_pwrstate(struct rtw_softc *sc, enum rtw_pwrstate power)
4597 {
4598 return (0);
4599 }
4600
4601 int
rtw_rtl8225_init(struct rtw_softc * sc,u_int freq,u_int8_t opaque_txpower,enum rtw_pwrstate power)4602 rtw_rtl8225_init(struct rtw_softc *sc, u_int freq, u_int8_t opaque_txpower,
4603 enum rtw_pwrstate power)
4604 {
4605 return (0);
4606 }
4607
4608 int
rtw_rtl8225_txpower(struct rtw_softc * sc,u_int8_t opaque_txpower)4609 rtw_rtl8225_txpower(struct rtw_softc *sc, u_int8_t opaque_txpower)
4610 {
4611 return (0);
4612 }
4613
4614 int
rtw_rtl8225_tune(struct rtw_softc * sc,u_int freq)4615 rtw_rtl8225_tune(struct rtw_softc *sc, u_int freq)
4616 {
4617 return (0);
4618 }
4619
4620 int
rtw_rtl8255_pwrstate(struct rtw_softc * sc,enum rtw_pwrstate power)4621 rtw_rtl8255_pwrstate(struct rtw_softc *sc, enum rtw_pwrstate power)
4622 {
4623 return (0);
4624 }
4625
4626 int
rtw_rtl8255_init(struct rtw_softc * sc,u_int freq,u_int8_t opaque_txpower,enum rtw_pwrstate power)4627 rtw_rtl8255_init(struct rtw_softc *sc, u_int freq, u_int8_t opaque_txpower,
4628 enum rtw_pwrstate power)
4629 {
4630 return (0);
4631 }
4632
4633 int
rtw_rtl8255_txpower(struct rtw_softc * sc,u_int8_t opaque_txpower)4634 rtw_rtl8255_txpower(struct rtw_softc *sc, u_int8_t opaque_txpower)
4635 {
4636 return (0);
4637 }
4638
4639 int
rtw_rtl8255_tune(struct rtw_softc * sc,u_int freq)4640 rtw_rtl8255_tune(struct rtw_softc *sc, u_int freq)
4641 {
4642 return (0);
4643 }
4644
4645 int
rtw_phy_init(struct rtw_softc * sc)4646 rtw_phy_init(struct rtw_softc *sc)
4647 {
4648 int rc;
4649 struct ieee80211com *ic = &sc->sc_ic;
4650 struct rtw_regs *regs = &sc->sc_regs;
4651 int antdiv = sc->sc_flags & RTW_F_ANTDIV;
4652 int dflantb = sc->sc_flags & RTW_F_DFLANTB;
4653 u_int freq = ic->ic_bss->ni_chan->ic_freq; /* freq is in MHz */
4654 u_int8_t opaque_txpower = rtw_chan2txpower(&sc->sc_srom, ic,
4655 ic->ic_bss->ni_chan);
4656 u_int8_t cs_threshold = sc->sc_csthr;
4657 enum rtw_pwrstate power = RTW_ON;
4658
4659 RTW_DPRINTF(RTW_DEBUG_PHY,
4660 ("%s: txpower %u csthresh %u freq %u antdiv %u dflantb %u "
4661 "pwrstate %s\n", __func__, opaque_txpower, cs_threshold, freq,
4662 antdiv, dflantb, rtw_pwrstate_string(power)));
4663
4664 /* XXX is this really necessary? */
4665 if ((rc = (*sc->sc_rf_txpower)(sc, opaque_txpower)) != 0)
4666 return rc;
4667 if ((rc = rtw_bbp_preinit(regs, sc->sc_bbpset.bb_antatten, dflantb,
4668 freq)) != 0)
4669 return rc;
4670 if ((rc = (*sc->sc_rf_tune)(sc, freq)) != 0)
4671 return rc;
4672 /* initialize RF */
4673 if ((rc = (*sc->sc_rf_init)(sc, freq, opaque_txpower, power)) != 0)
4674 return rc;
4675 #if 0 /* what is this redundant tx power setting here for? */
4676 if ((rc = (*sc->sc_rf_txpower)(sc, opaque_txpower)) != 0)
4677 return rc;
4678 #endif
4679 return rtw_bbp_init(regs, &sc->sc_bbpset, antdiv, dflantb,
4680 cs_threshold, freq);
4681 }
4682
4683 /*
4684 * Generic PHY I/O functions
4685 */
4686
4687 int
rtw_bbp_write(struct rtw_regs * regs,u_int addr,u_int val)4688 rtw_bbp_write(struct rtw_regs *regs, u_int addr, u_int val)
4689 {
4690 #define BBP_WRITE_ITERS 50
4691 #define BBP_WRITE_DELAY 1
4692 int i;
4693 u_int32_t wrbbp, rdbbp;
4694
4695 RTW_DPRINTF(RTW_DEBUG_PHYIO,
4696 ("%s: bbp[%u] <- %u\n", __func__, addr, val));
4697
4698 KASSERT((addr & ~PRESHIFT(RTW_BB_ADDR_MASK)) == 0);
4699 KASSERT((val & ~PRESHIFT(RTW_BB_WR_MASK)) == 0);
4700
4701 wrbbp = LSHIFT(addr, RTW_BB_ADDR_MASK) | RTW_BB_WREN |
4702 LSHIFT(val, RTW_BB_WR_MASK) | RTW_BB_RD_MASK,
4703
4704 rdbbp = LSHIFT(addr, RTW_BB_ADDR_MASK) |
4705 RTW_BB_WR_MASK | RTW_BB_RD_MASK;
4706
4707 RTW_DPRINTF(RTW_DEBUG_PHYIO,
4708 ("%s: rdbbp = %#08x, wrbbp = %#08x\n", __func__, rdbbp, wrbbp));
4709
4710 for (i = BBP_WRITE_ITERS; --i >= 0; ) {
4711 RTW_RBW(regs, RTW_BB, RTW_BB);
4712 RTW_WRITE(regs, RTW_BB, wrbbp);
4713 RTW_SYNC(regs, RTW_BB, RTW_BB);
4714 RTW_WRITE(regs, RTW_BB, rdbbp);
4715 RTW_SYNC(regs, RTW_BB, RTW_BB);
4716 delay(BBP_WRITE_DELAY); /* 1 microsecond */
4717 if (MASK_AND_RSHIFT(RTW_READ(regs, RTW_BB),
4718 RTW_BB_RD_MASK) == val) {
4719 RTW_DPRINTF(RTW_DEBUG_PHYIO,
4720 ("%s: finished in %dus\n", __func__,
4721 BBP_WRITE_DELAY * (BBP_WRITE_ITERS - i)));
4722 return 0;
4723 }
4724 delay(BBP_WRITE_DELAY); /* again */
4725 }
4726 printf("%s: timeout\n", __func__);
4727 return -1;
4728 }
4729
4730 /* Help rtw_rf_hostwrite bang bits to RF over 3-wire interface. */
4731 void
rtw_rf_hostbangbits(struct rtw_regs * regs,u_int32_t bits,int lo_to_hi,u_int nbits)4732 rtw_rf_hostbangbits(struct rtw_regs *regs, u_int32_t bits, int lo_to_hi,
4733 u_int nbits)
4734 {
4735 int i;
4736 u_int32_t mask, reg;
4737
4738 KASSERT(nbits <= 32);
4739
4740 RTW_DPRINTF(RTW_DEBUG_PHYIO,
4741 ("%s: %u bits, %#08x, %s\n", __func__, nbits, bits,
4742 (lo_to_hi) ? "lo to hi" : "hi to lo"));
4743
4744 reg = RTW8180_PHYCFG_HST;
4745 RTW_WRITE(regs, RTW8180_PHYCFG, reg);
4746 RTW_SYNC(regs, RTW8180_PHYCFG, RTW8180_PHYCFG);
4747
4748 if (lo_to_hi)
4749 mask = 0x1;
4750 else
4751 mask = 1 << (nbits - 1);
4752
4753 for (i = 0; i < nbits; i++) {
4754 RTW_DPRINTF(RTW_DEBUG_PHYBITIO,
4755 ("%s: bits %#08x mask %#08x -> bit %#08x\n",
4756 __func__, bits, mask, bits & mask));
4757
4758 if ((bits & mask) != 0)
4759 reg |= RTW8180_PHYCFG_HST_DATA;
4760 else
4761 reg &= ~RTW8180_PHYCFG_HST_DATA;
4762
4763 reg |= RTW8180_PHYCFG_HST_CLK;
4764 RTW_WRITE(regs, RTW8180_PHYCFG, reg);
4765 RTW_SYNC(regs, RTW8180_PHYCFG, RTW8180_PHYCFG);
4766
4767 DELAY(2); /* arbitrary delay */
4768
4769 reg &= ~RTW8180_PHYCFG_HST_CLK;
4770 RTW_WRITE(regs, RTW8180_PHYCFG, reg);
4771 RTW_SYNC(regs, RTW8180_PHYCFG, RTW8180_PHYCFG);
4772
4773 if (lo_to_hi)
4774 mask <<= 1;
4775 else
4776 mask >>= 1;
4777 }
4778
4779 reg |= RTW8180_PHYCFG_HST_EN;
4780 KASSERT((reg & RTW8180_PHYCFG_HST_CLK) == 0);
4781 RTW_WRITE(regs, RTW8180_PHYCFG, reg);
4782 RTW_SYNC(regs, RTW8180_PHYCFG, RTW8180_PHYCFG);
4783 }
4784
4785 #if 0
4786 void
4787 rtw_rf_rtl8225_hostbangbits(struct rtw_regs *regs, u_int32_t bits, int lo_to_hi,
4788 u_int nbits)
4789 {
4790 int i;
4791 u_int8_t page;
4792 u_int16_t reg0, reg1, reg2;
4793 u_int32_t mask;
4794
4795 /* enable page 0 */
4796 page = RTW_READ8(regs, RTW_PSR);
4797 RTW_WRITE8(regs, RTW_PSR, page & ~RTW_PSR_PSEN);
4798
4799 /* enable RF access */
4800 reg0 = RTW_READ16(regs, RTW8185_RFPINSOUTPUT) &
4801 RTW8185_RFPINSOUTPUT_MASK;
4802 reg1 = RTW_READ16(regs, RTW8185_RFPINSENABLE);
4803 RTW_WRITE16(regs, RTW8185_RFPINSENABLE,
4804 RTW8185_RFPINSENABLE_ENABLE | reg0);
4805 reg2 = RTW_READ16(regs, RTW8185_RFPINSSELECT);
4806 RTW_WRITE16(regs, RTW8185_RFPINSSELECT,
4807 RTW8185_RFPINSSELECT_ENABLE | reg1 /* XXX | SW_GPIO_CTL */);
4808 DELAY(10);
4809
4810 RTW_WRITE16(regs, RTW8185_RFPINSOUTPUT, reg0);
4811 DELAY(10);
4812
4813 if (lo_to_hi)
4814 mask = 0x1;
4815 else
4816 mask = 1 << (nbits - 1);
4817
4818 for (i = 0; i < nbits; i++) {
4819 RTW_DPRINTF(RTW_DEBUG_PHYBITIO,
4820 ("%s: bits %#08x mask %#08x -> bit %#08x\n",
4821 __func__, bits, mask, bits & mask));
4822
4823 if ((bits & mask) != 0)
4824 reg |= RTW8180_PHYCFG_HST_DATA;
4825 else
4826 reg &= ~RTW8180_PHYCFG_HST_DATA;
4827
4828 reg |= RTW8180_PHYCFG_HST_CLK;
4829 RTW_WRITE(regs, RTW8180_PHYCFG, reg);
4830 RTW_SYNC(regs, RTW8180_PHYCFG, RTW8180_PHYCFG);
4831
4832 DELAY(2); /* arbitrary delay */
4833
4834 reg &= ~RTW8180_PHYCFG_HST_CLK;
4835 RTW_WRITE(regs, RTW8180_PHYCFG, reg);
4836 RTW_SYNC(regs, RTW8180_PHYCFG, RTW8180_PHYCFG);
4837
4838 if (lo_to_hi)
4839 mask <<= 1;
4840 else
4841 mask >>= 1;
4842 }
4843
4844 /* reset the page */
4845 RTW_WRITE8(regs, RTW_PSR, page);
4846 }
4847 #endif
4848
4849 /* Help rtw_rf_macwrite: tell MAC to bang bits to RF over the 3-wire
4850 * interface.
4851 */
4852 int
rtw_rf_macbangbits(struct rtw_regs * regs,u_int32_t reg)4853 rtw_rf_macbangbits(struct rtw_regs *regs, u_int32_t reg)
4854 {
4855 int i;
4856
4857 RTW_DPRINTF(RTW_DEBUG_PHY, ("%s: %#08x\n", __func__, reg));
4858
4859 RTW_WRITE(regs, RTW8180_PHYCFG, RTW8180_PHYCFG_MAC_POLL | reg);
4860
4861 RTW_WBR(regs, RTW8180_PHYCFG, RTW8180_PHYCFG);
4862
4863 for (i = rtw_macbangbits_timeout; --i >= 0; delay(1)) {
4864 if ((RTW_READ(regs, RTW8180_PHYCFG) &
4865 RTW8180_PHYCFG_MAC_POLL) == 0) {
4866 RTW_DPRINTF(RTW_DEBUG_PHY,
4867 ("%s: finished in %dus\n", __func__,
4868 rtw_macbangbits_timeout - i));
4869 return 0;
4870 }
4871 RTW_RBR(regs, RTW8180_PHYCFG, RTW8180_PHYCFG);
4872 }
4873
4874 printf("%s: RTW8180_PHYCFG_MAC_POLL still set.\n", __func__);
4875 return -1;
4876 }
4877
4878 u_int32_t
rtw_grf5101_host_crypt(u_int addr,u_int32_t val)4879 rtw_grf5101_host_crypt(u_int addr, u_int32_t val)
4880 {
4881 /* TBD */
4882 return 0;
4883 }
4884
4885 u_int32_t
rtw_grf5101_mac_crypt(u_int addr,u_int32_t val)4886 rtw_grf5101_mac_crypt(u_int addr, u_int32_t val)
4887 {
4888 u_int32_t data_and_addr;
4889 #define EXTRACT_NIBBLE(d, which) (((d) >> (4 * (which))) & 0xf)
4890 static u_int8_t caesar[16] = {
4891 0x0, 0x8, 0x4, 0xc,
4892 0x2, 0xa, 0x6, 0xe,
4893 0x1, 0x9, 0x5, 0xd,
4894 0x3, 0xb, 0x7, 0xf
4895 };
4896 data_and_addr =
4897 caesar[EXTRACT_NIBBLE(val, 2)] |
4898 (caesar[EXTRACT_NIBBLE(val, 1)] << 4) |
4899 (caesar[EXTRACT_NIBBLE(val, 0)] << 8) |
4900 (caesar[(addr >> 1) & 0xf] << 12) |
4901 ((addr & 0x1) << 16) |
4902 (caesar[EXTRACT_NIBBLE(val, 3)] << 24);
4903 return LSHIFT(data_and_addr, RTW8180_PHYCFG_MAC_PHILIPS_ADDR_MASK |
4904 RTW8180_PHYCFG_MAC_PHILIPS_DATA_MASK);
4905 #undef EXTRACT_NIBBLE
4906 }
4907
4908 /* Bang bits over the 3-wire interface. */
4909 int
rtw_rf_hostwrite(struct rtw_softc * sc,u_int addr,u_int32_t val)4910 rtw_rf_hostwrite(struct rtw_softc *sc, u_int addr, u_int32_t val)
4911 {
4912 u_int nbits;
4913 int lo_to_hi;
4914 u_int32_t bits;
4915 void(*rf_bangbits)(struct rtw_regs *, u_int32_t, int, u_int) =
4916 rtw_rf_hostbangbits;
4917
4918 RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: [%u] <- %#08x\n", __func__,
4919 addr, val));
4920
4921 switch (sc->sc_rfchipid) {
4922 case RTW_RFCHIPID_MAXIM2820:
4923 nbits = 16;
4924 lo_to_hi = 0;
4925 bits = LSHIFT(val, MAX2820_TWI_DATA_MASK) |
4926 LSHIFT(addr, MAX2820_TWI_ADDR_MASK);
4927 break;
4928 case RTW_RFCHIPID_PHILIPS:
4929 KASSERT((addr & ~PRESHIFT(SA2400_TWI_ADDR_MASK)) == 0);
4930 KASSERT((val & ~PRESHIFT(SA2400_TWI_DATA_MASK)) == 0);
4931 bits = LSHIFT(val, SA2400_TWI_DATA_MASK) |
4932 LSHIFT(addr, SA2400_TWI_ADDR_MASK) | SA2400_TWI_WREN;
4933 nbits = 32;
4934 lo_to_hi = 1;
4935 break;
4936 case RTW_RFCHIPID_GCT:
4937 KASSERT((addr & ~PRESHIFT(SI4126_TWI_ADDR_MASK)) == 0);
4938 KASSERT((val & ~PRESHIFT(SI4126_TWI_DATA_MASK)) == 0);
4939 bits = rtw_grf5101_host_crypt(addr, val);
4940 nbits = 21;
4941 lo_to_hi = 1;
4942 break;
4943 case RTW_RFCHIPID_RFMD2948:
4944 KASSERT((addr & ~PRESHIFT(SI4126_TWI_ADDR_MASK)) == 0);
4945 KASSERT((val & ~PRESHIFT(SI4126_TWI_DATA_MASK)) == 0);
4946 bits = LSHIFT(val, SI4126_TWI_DATA_MASK) |
4947 LSHIFT(addr, SI4126_TWI_ADDR_MASK);
4948 nbits = 22;
4949 lo_to_hi = 0;
4950 break;
4951 case RTW_RFCHIPID_RTL8225:
4952 case RTW_RFCHIPID_RTL8255:
4953 nbits = 16;
4954 lo_to_hi = 0;
4955 bits = LSHIFT(val, RTL8225_TWI_DATA_MASK) |
4956 LSHIFT(addr, RTL8225_TWI_ADDR_MASK);
4957
4958 /* the RTL8225 uses a slightly modified RF interface */
4959 rf_bangbits = rtw_rf_hostbangbits;
4960 break;
4961 case RTW_RFCHIPID_INTERSIL:
4962 default:
4963 printf("%s: unknown rfchipid %d\n", __func__, sc->sc_rfchipid);
4964 return -1;
4965 }
4966
4967 (*rf_bangbits)(&sc->sc_regs, bits, lo_to_hi, nbits);
4968
4969 return 0;
4970 }
4971
4972 u_int32_t
rtw_maxim_swizzle(u_int addr,u_int32_t val)4973 rtw_maxim_swizzle(u_int addr, u_int32_t val)
4974 {
4975 u_int32_t hidata, lodata;
4976
4977 KASSERT((val & ~(RTW_MAXIM_LODATA_MASK|RTW_MAXIM_HIDATA_MASK)) == 0);
4978 lodata = MASK_AND_RSHIFT(val, RTW_MAXIM_LODATA_MASK);
4979 hidata = MASK_AND_RSHIFT(val, RTW_MAXIM_HIDATA_MASK);
4980 return LSHIFT(lodata, RTW8180_PHYCFG_MAC_MAXIM_LODATA_MASK) |
4981 LSHIFT(hidata, RTW8180_PHYCFG_MAC_MAXIM_HIDATA_MASK) |
4982 LSHIFT(addr, RTW8180_PHYCFG_MAC_MAXIM_ADDR_MASK);
4983 }
4984
4985 /* Tell the MAC what to bang over the 3-wire interface. */
4986 int
rtw_rf_macwrite(struct rtw_softc * sc,u_int addr,u_int32_t val)4987 rtw_rf_macwrite(struct rtw_softc *sc, u_int addr, u_int32_t val)
4988 {
4989 u_int32_t reg;
4990
4991 RTW_DPRINTF(RTW_DEBUG_PHYIO, ("%s: %s[%u] <- %#08x\n", __func__,
4992 addr, val));
4993
4994 switch (sc->sc_rfchipid) {
4995 case RTW_RFCHIPID_GCT:
4996 reg = rtw_grf5101_mac_crypt(addr, val);
4997 break;
4998 case RTW_RFCHIPID_MAXIM2820:
4999 reg = rtw_maxim_swizzle(addr, val);
5000 break;
5001 default: /* XXX */
5002 case RTW_RFCHIPID_PHILIPS:
5003 KASSERT((addr &
5004 ~PRESHIFT(RTW8180_PHYCFG_MAC_PHILIPS_ADDR_MASK)) == 0);
5005 KASSERT((val &
5006 ~PRESHIFT(RTW8180_PHYCFG_MAC_PHILIPS_DATA_MASK)) == 0);
5007
5008 reg = LSHIFT(addr, RTW8180_PHYCFG_MAC_PHILIPS_ADDR_MASK) |
5009 LSHIFT(val, RTW8180_PHYCFG_MAC_PHILIPS_DATA_MASK);
5010 }
5011
5012 switch (sc->sc_rfchipid) {
5013 case RTW_RFCHIPID_GCT:
5014 case RTW_RFCHIPID_MAXIM2820:
5015 case RTW_RFCHIPID_RFMD2948:
5016 reg |= RTW8180_PHYCFG_MAC_RFTYPE_RFMD;
5017 break;
5018 case RTW_RFCHIPID_INTERSIL:
5019 reg |= RTW8180_PHYCFG_MAC_RFTYPE_INTERSIL;
5020 break;
5021 case RTW_RFCHIPID_PHILIPS:
5022 reg |= RTW8180_PHYCFG_MAC_RFTYPE_PHILIPS;
5023 break;
5024 default:
5025 printf("%s: unknown rfchipid %d\n", __func__, sc->sc_rfchipid);
5026 return -1;
5027 }
5028
5029 return rtw_rf_macbangbits(&sc->sc_regs, reg);
5030 }
5031
5032
5033 u_int8_t
rtw_read8(void * arg,u_int32_t off)5034 rtw_read8(void *arg, u_int32_t off)
5035 {
5036 struct rtw_regs *regs = (struct rtw_regs *)arg;
5037 return (bus_space_read_1(regs->r_bt, regs->r_bh, off));
5038 }
5039
5040 u_int16_t
rtw_read16(void * arg,u_int32_t off)5041 rtw_read16(void *arg, u_int32_t off)
5042 {
5043 struct rtw_regs *regs = (struct rtw_regs *)arg;
5044 return (bus_space_read_2(regs->r_bt, regs->r_bh, off));
5045 }
5046
5047 u_int32_t
rtw_read32(void * arg,u_int32_t off)5048 rtw_read32(void *arg, u_int32_t off)
5049 {
5050 struct rtw_regs *regs = (struct rtw_regs *)arg;
5051 return (bus_space_read_4(regs->r_bt, regs->r_bh, off));
5052 }
5053
5054 void
rtw_write8(void * arg,u_int32_t off,u_int8_t val)5055 rtw_write8(void *arg, u_int32_t off, u_int8_t val)
5056 {
5057 struct rtw_regs *regs = (struct rtw_regs *)arg;
5058 bus_space_write_1(regs->r_bt, regs->r_bh, off, val);
5059 }
5060
5061 void
rtw_write16(void * arg,u_int32_t off,u_int16_t val)5062 rtw_write16(void *arg, u_int32_t off, u_int16_t val)
5063 {
5064 struct rtw_regs *regs = (struct rtw_regs *)arg;
5065 bus_space_write_2(regs->r_bt, regs->r_bh, off, val);
5066 }
5067
5068 void
rtw_write32(void * arg,u_int32_t off,u_int32_t val)5069 rtw_write32(void *arg, u_int32_t off, u_int32_t val)
5070 {
5071 struct rtw_regs *regs = (struct rtw_regs *)arg;
5072 bus_space_write_4(regs->r_bt, regs->r_bh, off, val);
5073 }
5074
5075 void
rtw_barrier(void * arg,u_int32_t reg0,u_int32_t reg1,int flags)5076 rtw_barrier(void *arg, u_int32_t reg0, u_int32_t reg1, int flags)
5077 {
5078 struct rtw_regs *regs = (struct rtw_regs *)arg;
5079 bus_space_barrier(regs->r_bt, regs->r_bh, MIN(reg0, reg1),
5080 MAX(reg0, reg1) - MIN(reg0, reg1) + 4, flags);
5081 }
5082