rtwvar.h revision 1.35 1 /* $NetBSD: rtwvar.h,v 1.35 2007/12/21 18:22:43 dyoung Exp $ */
2 /*-
3 * Copyright (c) 2004, 2005 David Young. All rights reserved.
4 *
5 * Driver for the Realtek RTL8180 802.11 MAC/BBP by David Young.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of David Young may not be used to endorse or promote
16 * products derived from this software without specific prior
17 * written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
23 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 */
32
33 #ifndef _DEV_IC_RTWVAR_H_
34 #define _DEV_IC_RTWVAR_H_
35
36 #include <sys/queue.h>
37 #include <sys/callout.h>
38
39 #ifdef RTW_DEBUG
40 #define RTW_DEBUG_TUNE 0x0000001
41 #define RTW_DEBUG_PKTFILT 0x0000002
42 #define RTW_DEBUG_XMIT 0x0000004
43 #define RTW_DEBUG_XMIT_DESC 0x0000008
44 #define RTW_DEBUG_NODE 0x0000010
45 #define RTW_DEBUG_PWR 0x0000020
46 #define RTW_DEBUG_ATTACH 0x0000040
47 #define RTW_DEBUG_REGDUMP 0x0000080
48 #define RTW_DEBUG_ACCESS 0x0000100
49 #define RTW_DEBUG_RESET 0x0000200
50 #define RTW_DEBUG_INIT 0x0000400
51 #define RTW_DEBUG_IOSTATE 0x0000800
52 #define RTW_DEBUG_RECV 0x0001000
53 #define RTW_DEBUG_RECV_DESC 0x0002000
54 #define RTW_DEBUG_IO_KICK 0x0004000
55 #define RTW_DEBUG_INTR 0x0008000
56 #define RTW_DEBUG_PHY 0x0010000
57 #define RTW_DEBUG_PHYIO 0x0020000
58 #define RTW_DEBUG_PHYBITIO 0x0040000
59 #define RTW_DEBUG_TIMEOUT 0x0080000
60 #define RTW_DEBUG_BUGS 0x0100000
61 #define RTW_DEBUG_BEACON 0x0200000
62 #define RTW_DEBUG_LED 0x0400000
63 #define RTW_DEBUG_KEY 0x0800000
64 #define RTW_DEBUG_XMIT_RSRC 0x1000000
65 #define RTW_DEBUG_OACTIVE 0x2000000
66 #define RTW_DEBUG_MAX 0x3ffffff
67
68 extern int rtw_debug;
69 #define RTW_DPRINTF(__flags, __x) \
70 if ((rtw_debug & (__flags)) != 0) printf __x
71 #define DPRINTF(__sc, __flags, __x) \
72 if (((__sc)->sc_if.if_flags & IFF_DEBUG) != 0) \
73 RTW_DPRINTF(__flags, __x)
74 #define RTW_PRINT_REGS(__regs, __dvname, __where) \
75 rtw_print_regs((__regs), (__dvname), (__where))
76 #else /* RTW_DEBUG */
77 #define RTW_DPRINTF(__flags, __x)
78 #define DPRINTF(__sc, __flags, __x)
79 #define RTW_PRINT_REGS(__regs, __dvname, __where)
80 #endif /* RTW_DEBUG */
81
82 enum rtw_locale {
83 RTW_LOCALE_USA = 0,
84 RTW_LOCALE_EUROPE,
85 RTW_LOCALE_JAPAN,
86 RTW_LOCALE_UNKNOWN
87 };
88
89 enum rtw_rfchipid {
90 RTW_RFCHIPID_RESERVED = 0,
91 RTW_RFCHIPID_INTERSIL = 1,
92 RTW_RFCHIPID_RFMD = 2,
93 RTW_RFCHIPID_PHILIPS = 3,
94 RTW_RFCHIPID_MAXIM = 4,
95 RTW_RFCHIPID_GCT = 5
96 };
97
98 /* sc_flags */
99 #define RTW_F_ENABLED 0x00000001 /* chip is enabled */
100 #define RTW_F_DIGPHY 0x00000002 /* digital PHY */
101 #define RTW_F_DFLANTB 0x00000004 /* B antenna is default */
102 #define RTW_F_ANTDIV 0x00000010 /* h/w antenna diversity */
103 #define RTW_F_9356SROM 0x00000020 /* 93c56 SROM */
104 #define RTW_F_SLEEP 0x00000040 /* chip is asleep */
105 #define RTW_F_INVALID 0x00000080 /* chip is absent */
106 #define RTW_F_DK_VALID 0x00000100 /* keys in DK0-DK3 are valid */
107 #define RTW_C_RXWEP_40 0x00000200 /* h/w decrypts 40-bit WEP */
108 #define RTW_C_RXWEP_104 0x00000400 /* h/w decrypts 104-bit WEP */
109 /* all PHY flags */
110 #define RTW_F_ALLPHY (RTW_F_DIGPHY|RTW_F_DFLANTB|RTW_F_ANTDIV)
111 enum rtw_access {RTW_ACCESS_NONE = 0,
112 RTW_ACCESS_CONFIG = 1,
113 RTW_ACCESS_ANAPARM = 2};
114
115 struct rtw_regs {
116 bus_space_tag_t r_bt;
117 bus_space_handle_t r_bh;
118 enum rtw_access r_access;
119 };
120
121 #define RTW_SR_GET(sr, ofs) \
122 (((sr)->sr_content[(ofs)/2] >> (((ofs) % 2 == 0) ? 0 : 8)) & 0xff)
123
124 #define RTW_SR_GET16(sr, ofs) \
125 (RTW_SR_GET((sr), (ofs)) | (RTW_SR_GET((sr), (ofs) + 1) << 8))
126
127 struct rtw_srom {
128 uint16_t *sr_content;
129 uint16_t sr_size;
130 };
131
132 struct rtw_rxsoft {
133 struct mbuf *rs_mbuf;
134 bus_dmamap_t rs_dmamap;
135 };
136
137 struct rtw_txsoft {
138 SIMPLEQ_ENTRY(rtw_txsoft) ts_q;
139 struct mbuf *ts_mbuf;
140 bus_dmamap_t ts_dmamap;
141 struct ieee80211_node *ts_ni; /* destination node */
142 u_int ts_first; /* 1st hw descriptor */
143 u_int ts_last; /* last hw descriptor */
144 struct ieee80211_duration ts_d0;
145 struct ieee80211_duration ts_dn;
146 };
147
148 #define RTW_NTXPRI 4 /* number of Tx priorities */
149 #define RTW_TXPRILO 0
150 #define RTW_TXPRIMD 1
151 #define RTW_TXPRIHI 2
152 #define RTW_TXPRIBCN 3 /* beacon priority */
153
154 #define RTW_MAXPKTSEGS 64 /* Max 64 segments per Tx packet */
155
156 #define CASSERT(cond, complaint) complaint[(cond) ? 0 : -1] = complaint[(cond) ? 0 : -1]
157
158 /* Note well: the descriptor rings must begin on RTW_DESC_ALIGNMENT
159 * boundaries. I allocate them consecutively from one buffer, so
160 * just round up.
161 */
162 #define RTW_TXQLENLO 64 /* low-priority queue length */
163 #define RTW_TXQLENMD 64 /* medium-priority */
164 #define RTW_TXQLENHI 64 /* high-priority */
165 #define RTW_TXQLENBCN 8 /* beacon */
166
167 #define RTW_NTXDESCLO RTW_TXQLENLO
168 #define RTW_NTXDESCMD RTW_TXQLENMD
169 #define RTW_NTXDESCHI RTW_TXQLENHI
170 #define RTW_NTXDESCBCN RTW_TXQLENBCN
171
172 #define RTW_NTXDESCTOTAL (RTW_NTXDESCLO + RTW_NTXDESCMD + \
173 RTW_NTXDESCHI + RTW_NTXDESCBCN)
174
175 #define RTW_RXQLEN 64
176
177 struct rtw_rxdesc_blk {
178 u_int rdb_ndesc;
179 u_int rdb_next;
180 bus_dma_tag_t rdb_dmat;
181 bus_dmamap_t rdb_dmamap;
182 struct rtw_rxdesc *rdb_desc;
183 };
184
185 struct rtw_txdesc_blk {
186 u_int tdb_ndesc;
187 u_int tdb_next;
188 u_int tdb_nfree;
189 bus_dma_tag_t tdb_dmat;
190 bus_dmamap_t tdb_dmamap;
191 bus_addr_t tdb_physbase;
192 bus_addr_t tdb_ofs;
193 bus_size_t tdb_basereg;
194 uint32_t tdb_base;
195 struct rtw_txdesc *tdb_desc;
196 };
197
198 #define RTW_NEXT_IDX(__htc, __idx) (((__idx) + 1) % (__htc)->tdb_ndesc)
199
200 #define RTW_NEXT_DESC(__htc, __idx) \
201 ((__htc)->tdb_physbase + \
202 sizeof(struct rtw_txdesc) * RTW_NEXT_IDX((__htc), (__idx)))
203
204 SIMPLEQ_HEAD(rtw_txq, rtw_txsoft);
205
206 struct rtw_txsoft_blk {
207 /* dirty/free s/w descriptors */
208 struct rtw_txq tsb_dirtyq;
209 struct rtw_txq tsb_freeq;
210 u_int tsb_ndesc;
211 int tsb_tx_timer;
212 struct rtw_txsoft *tsb_desc;
213 uint8_t tsb_poll;
214 };
215
216 struct rtw_descs {
217 struct rtw_txdesc hd_txlo[RTW_NTXDESCLO];
218 struct rtw_txdesc hd_txmd[RTW_NTXDESCMD];
219 struct rtw_txdesc hd_txhi[RTW_NTXDESCMD];
220 struct rtw_rxdesc hd_rx[RTW_RXQLEN];
221 struct rtw_txdesc hd_bcn[RTW_NTXDESCBCN];
222 };
223 #define RTW_DESC_OFFSET(ring, i) offsetof(struct rtw_descs, ring[i])
224 #define RTW_RING_OFFSET(ring) RTW_DESC_OFFSET(ring, 0)
225 #define RTW_RING_BASE(sc, ring) ((sc)->sc_desc_physaddr + \
226 RTW_RING_OFFSET(ring))
227
228 /* Radio capture format for RTL8180. */
229
230 #define RTW_RX_RADIOTAP_PRESENT \
231 ((1 << IEEE80211_RADIOTAP_TSFT) | \
232 (1 << IEEE80211_RADIOTAP_FLAGS) | \
233 (1 << IEEE80211_RADIOTAP_RATE) | \
234 (1 << IEEE80211_RADIOTAP_CHANNEL) | \
235 (1 << IEEE80211_RADIOTAP_LOCK_QUALITY) | \
236 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) | \
237 0)
238
239 #define RTW_PHILIPS_RX_RADIOTAP_PRESENT \
240 ((1 << IEEE80211_RADIOTAP_TSFT) | \
241 (1 << IEEE80211_RADIOTAP_FLAGS) | \
242 (1 << IEEE80211_RADIOTAP_RATE) | \
243 (1 << IEEE80211_RADIOTAP_CHANNEL) | \
244 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) | \
245 0)
246
247 struct rtw_rx_radiotap_header {
248 struct ieee80211_radiotap_header rr_ihdr;
249 uint64_t rr_tsft;
250 uint8_t rr_flags;
251 uint8_t rr_rate;
252 uint16_t rr_chan_freq;
253 uint16_t rr_chan_flags;
254 union {
255 struct {
256 uint16_t o_barker_lock;
257 uint8_t o_antsignal;
258 } u_other;
259 struct {
260 uint8_t p_antsignal;
261 } u_philips;
262 } rr_u;
263 } __attribute__((__packed__));
264
265 #define RTW_TX_RADIOTAP_PRESENT \
266 ((1 << IEEE80211_RADIOTAP_RATE) | \
267 (1 << IEEE80211_RADIOTAP_CHANNEL) | \
268 0)
269
270 struct rtw_tx_radiotap_header {
271 struct ieee80211_radiotap_header rt_ihdr;
272 uint8_t rt_rate;
273 uint8_t rt_pad;
274 uint16_t rt_chan_freq;
275 uint16_t rt_chan_flags;
276 } __attribute__((__packed__));
277
278 enum rtw_attach_state {FINISHED, FINISH_DESCMAP_LOAD, FINISH_DESCMAP_CREATE,
279 FINISH_DESC_MAP, FINISH_DESC_ALLOC, FINISH_RXMAPS_CREATE,
280 FINISH_TXMAPS_CREATE, FINISH_RESET, FINISH_READ_SROM, FINISH_PARSE_SROM,
281 FINISH_RF_ATTACH, FINISH_ID_STA, FINISH_TXDESCBLK_SETUP,
282 FINISH_TXCTLBLK_SETUP, DETACHED};
283
284 struct rtw_mtbl {
285 int (*mt_newstate)(struct ieee80211com *,
286 enum ieee80211_state, int);
287 void (*mt_recv_mgmt)(struct ieee80211com *,
288 struct mbuf *, struct ieee80211_node *,
289 int, int, uint32_t);
290 struct ieee80211_node *(*mt_node_alloc)(struct ieee80211_node_table*);
291 void (*mt_node_free)(struct ieee80211_node *);
292 };
293
294 enum rtw_pwrstate { RTW_OFF = 0, RTW_SLEEP, RTW_ON };
295
296 typedef void (*rtw_continuous_tx_cb_t)(void *arg, int);
297
298 struct rtw_phy {
299 struct rtw_rf *p_rf;
300 struct rtw_regs *p_regs;
301 };
302
303 struct rtw_bbpset {
304 u_int bb_antatten;
305 u_int bb_chestlim;
306 u_int bb_chsqlim;
307 u_int bb_ifagcdet;
308 u_int bb_ifagcini;
309 u_int bb_ifagclimit;
310 u_int bb_lnadet;
311 u_int bb_sys1;
312 u_int bb_sys2;
313 u_int bb_sys3;
314 u_int bb_trl;
315 u_int bb_txagc;
316 };
317
318 struct rtw_rf {
319 void (*rf_destroy)(struct rtw_rf *);
320 /* args: frequency, txpower, power state */
321 int (*rf_init)(struct rtw_rf *, u_int, uint8_t,
322 enum rtw_pwrstate);
323 /* arg: power state */
324 int (*rf_pwrstate)(struct rtw_rf *, enum rtw_pwrstate);
325 /* arg: frequency */
326 int (*rf_tune)(struct rtw_rf *, u_int);
327 /* arg: txpower */
328 int (*rf_txpower)(struct rtw_rf *, uint8_t);
329 rtw_continuous_tx_cb_t rf_continuous_tx_cb;
330 void *rf_continuous_tx_arg;
331 struct rtw_bbpset rf_bbpset;
332 };
333
334 static __inline void
335 rtw_rf_destroy(struct rtw_rf *rf)
336 {
337 (*rf->rf_destroy)(rf);
338 }
339
340 static __inline int
341 rtw_rf_init(struct rtw_rf *rf, u_int freq, uint8_t opaque_txpower,
342 enum rtw_pwrstate power)
343 {
344 return (*rf->rf_init)(rf, freq, opaque_txpower, power);
345 }
346
347 static __inline int
348 rtw_rf_pwrstate(struct rtw_rf *rf, enum rtw_pwrstate power)
349 {
350 return (*rf->rf_pwrstate)(rf, power);
351 }
352
353 static __inline int
354 rtw_rf_tune(struct rtw_rf *rf, u_int freq)
355 {
356 return (*rf->rf_tune)(rf, freq);
357 }
358
359 static __inline int
360 rtw_rf_txpower(struct rtw_rf *rf, uint8_t opaque_txpower)
361 {
362 return (*rf->rf_txpower)(rf, opaque_txpower);
363 }
364
365 typedef int (*rtw_rf_write_t)(struct rtw_regs *, enum rtw_rfchipid, u_int,
366 uint32_t);
367
368 struct rtw_rfbus {
369 struct rtw_regs *b_regs;
370 rtw_rf_write_t b_write;
371 };
372
373 static __inline int
374 rtw_rfbus_write(struct rtw_rfbus *bus, enum rtw_rfchipid rfchipid, u_int addr,
375 uint32_t val)
376 {
377 return (*bus->b_write)(bus->b_regs, rfchipid, addr, val);
378 }
379
380 struct rtw_max2820 {
381 struct rtw_rf mx_rf;
382 struct rtw_rfbus mx_bus;
383 int mx_is_a; /* 1: MAX2820A/MAX2821A */
384 };
385
386 struct rtw_grf5101 {
387 struct rtw_rf gr_rf;
388 struct rtw_rfbus gr_bus;
389 };
390
391 struct rtw_sa2400 {
392 struct rtw_rf sa_rf;
393 struct rtw_rfbus sa_bus;
394 int sa_digphy; /* 1: digital PHY */
395 };
396
397 typedef void (*rtw_pwrstate_t)(struct rtw_regs *, enum rtw_pwrstate, int, int);
398
399 union rtw_keys {
400 uint8_t rk_keys[4][16];
401 uint32_t rk_words[16];
402 };
403
404 #define RTW_LED_SLOW_TICKS MAX(1, hz/2)
405 #define RTW_LED_FAST_TICKS MAX(1, hz/10)
406
407 struct rtw_led_state {
408 #define RTW_LED0 0x1
409 #define RTW_LED1 0x2
410 uint8_t ls_slowblink:2;
411 uint8_t ls_actblink:2;
412 uint8_t ls_default:2;
413 uint8_t ls_state;
414 uint8_t ls_event;
415 #define RTW_LED_S_RX 0x1
416 #define RTW_LED_S_TX 0x2
417 #define RTW_LED_S_SLOW 0x4
418 struct callout ls_slow_ch;
419 struct callout ls_fast_ch;
420 };
421
422 struct rtw_softc {
423 device_t sc_dev;
424 struct ethercom sc_ec;
425 struct ieee80211com sc_ic;
426 struct rtw_regs sc_regs;
427 bus_dma_tag_t sc_dmat;
428 uint32_t sc_flags;
429
430 enum rtw_attach_state sc_attach_state;
431 enum rtw_rfchipid sc_rfchipid;
432 enum rtw_locale sc_locale;
433 uint8_t sc_phydelay;
434
435 /* s/w Tx/Rx descriptors */
436 struct rtw_txsoft_blk sc_txsoft_blk[RTW_NTXPRI];
437 struct rtw_txdesc_blk sc_txdesc_blk[RTW_NTXPRI];
438
439 struct rtw_rxsoft sc_rxsoft[RTW_RXQLEN];
440 struct rtw_rxdesc_blk sc_rxdesc_blk;
441
442 struct rtw_descs *sc_descs;
443
444 bus_dma_segment_t sc_desc_segs;
445 int sc_desc_nsegs;
446 bus_dmamap_t sc_desc_dmamap;
447 #define sc_desc_physaddr sc_desc_dmamap->dm_segs[0].ds_addr
448
449 struct rtw_srom sc_srom;
450
451 enum rtw_pwrstate sc_pwrstate;
452
453 rtw_pwrstate_t sc_pwrstate_cb;
454
455 struct rtw_rf *sc_rf;
456
457 uint16_t sc_inten;
458
459 /* interrupt acknowledge hook */
460 void (*sc_intr_ack)(struct rtw_regs *);
461
462 int (*sc_enable)(struct rtw_softc *);
463 void (*sc_disable)(struct rtw_softc *);
464 struct rtw_mtbl sc_mtbl;
465
466 void * sc_radiobpf;
467
468 struct callout sc_scan_ch;
469 u_int sc_cur_chan;
470
471 uint32_t sc_tsfth; /* most significant TSFT bits */
472 uint32_t sc_rcr; /* RTW_RCR */
473 uint8_t sc_csthr; /* carrier-sense threshold */
474
475 int sc_do_tick; /* indicate 1s ticks */
476 struct timeval sc_tick0; /* first tick */
477
478 uint8_t sc_rev; /* PCI/Cardbus revision */
479
480 uint32_t sc_anaparm; /* register RTW_ANAPARM */
481
482 union {
483 struct rtw_rx_radiotap_header tap;
484 uint8_t pad[64];
485 } sc_rxtapu;
486 union {
487 struct rtw_tx_radiotap_header tap;
488 uint8_t pad[64];
489 } sc_txtapu;
490 union rtw_keys sc_keys;
491 struct ifqueue sc_beaconq;
492 struct rtw_led_state sc_led_state;
493 int sc_hwverid;
494 };
495
496 #define sc_if sc_ec.ec_if
497 #define sc_rxtap sc_rxtapu.tap
498 #define sc_txtap sc_txtapu.tap
499
500 void rtw_txdac_enable(struct rtw_softc *, int);
501 void rtw_anaparm_enable(struct rtw_regs *, int);
502 void rtw_config0123_enable(struct rtw_regs *, int);
503 void rtw_continuous_tx_enable(struct rtw_softc *, int);
504 void rtw_set_access(struct rtw_regs *, enum rtw_access);
505
506 void rtw_attach(struct rtw_softc *);
507 int rtw_detach(struct rtw_softc *);
508 int rtw_intr(void *);
509
510 void rtw_disable(struct rtw_softc *);
511 int rtw_enable(struct rtw_softc *);
512
513 int rtw_activate(device_t, enum devact);
514 void rtw_shutdown(void *);
515
516 const char *rtw_pwrstate_string(enum rtw_pwrstate);
517
518 #endif /* _DEV_IC_RTWVAR_H_ */
519