Home | History | Annotate | Line # | Download | only in ic
rtwvar.h revision 1.23
      1 /* $NetBSD: rtwvar.h,v 1.23 2005/06/22 06:15:51 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		0x000001
     41 #define	RTW_DEBUG_PKTFILT	0x000002
     42 #define	RTW_DEBUG_XMIT		0x000004
     43 #define	RTW_DEBUG_XMIT_DESC	0x000008
     44 #define	RTW_DEBUG_NODE		0x000010
     45 #define	RTW_DEBUG_PWR		0x000020
     46 #define	RTW_DEBUG_ATTACH	0x000040
     47 #define	RTW_DEBUG_REGDUMP	0x000080
     48 #define	RTW_DEBUG_ACCESS	0x000100
     49 #define	RTW_DEBUG_RESET		0x000200
     50 #define	RTW_DEBUG_INIT		0x000400
     51 #define	RTW_DEBUG_IOSTATE	0x000800
     52 #define	RTW_DEBUG_RECV		0x001000
     53 #define	RTW_DEBUG_RECV_DESC	0x002000
     54 #define	RTW_DEBUG_IO_KICK	0x004000
     55 #define	RTW_DEBUG_INTR		0x008000
     56 #define	RTW_DEBUG_PHY		0x010000
     57 #define	RTW_DEBUG_PHYIO		0x020000
     58 #define	RTW_DEBUG_PHYBITIO	0x040000
     59 #define	RTW_DEBUG_TIMEOUT	0x080000
     60 #define	RTW_DEBUG_BUGS		0x100000
     61 #define	RTW_DEBUG_BEACON	0x200000
     62 #define	RTW_DEBUG_LED		0x400000
     63 #define	RTW_DEBUG_KEY		0x800000
     64 #define	RTW_DEBUG_MAX		0xffffff
     65 
     66 extern int rtw_debug;
     67 #define RTW_DPRINTF(__flags, __x)	\
     68 	if ((rtw_debug & (__flags)) != 0) printf __x
     69 #define	DPRINTF(__sc, __flags, __x)				\
     70 	if (((__sc)->sc_if.if_flags & IFF_DEBUG) != 0)	\
     71 		RTW_DPRINTF(__flags, __x)
     72 #define	RTW_PRINT_REGS(__regs, __dvname, __where)	\
     73 	rtw_print_regs((__regs), (__dvname), (__where))
     74 #else /* RTW_DEBUG */
     75 #define RTW_DPRINTF(__flags, __x)
     76 #define	DPRINTF(__sc, __flags, __x)
     77 #define	RTW_PRINT_REGS(__regs, __dvname, __where)
     78 #endif /* RTW_DEBUG */
     79 
     80 enum rtw_locale {
     81 	RTW_LOCALE_USA = 0,
     82 	RTW_LOCALE_EUROPE,
     83 	RTW_LOCALE_JAPAN,
     84 	RTW_LOCALE_UNKNOWN
     85 };
     86 
     87 enum rtw_rfchipid {
     88 	RTW_RFCHIPID_RESERVED = 0,
     89 	RTW_RFCHIPID_INTERSIL = 1,
     90 	RTW_RFCHIPID_RFMD = 2,
     91 	RTW_RFCHIPID_PHILIPS = 3,
     92 	RTW_RFCHIPID_MAXIM = 4,
     93 	RTW_RFCHIPID_GCT = 5
     94 };
     95 
     96 /* sc_flags */
     97 #define RTW_F_ENABLED		0x00000001	/* chip is enabled */
     98 #define RTW_F_DIGPHY		0x00000002	/* digital PHY */
     99 #define RTW_F_DFLANTB		0x00000004	/* B antenna is default */
    100 #define RTW_F_ANTDIV		0x00000010	/* h/w antenna diversity */
    101 #define RTW_F_9356SROM		0x00000020	/* 93c56 SROM */
    102 #define RTW_F_SLEEP		0x00000040	/* chip is asleep */
    103 #define RTW_F_INVALID		0x00000080	/* chip is absent */
    104 	/* all PHY flags */
    105 #define RTW_F_ALLPHY		(RTW_F_DIGPHY|RTW_F_DFLANTB|RTW_F_ANTDIV)
    106 
    107 enum rtw_access {RTW_ACCESS_NONE = 0,
    108 		 RTW_ACCESS_CONFIG = 1,
    109 		 RTW_ACCESS_ANAPARM = 2};
    110 
    111 struct rtw_regs {
    112 	bus_space_tag_t		r_bt;
    113 	bus_space_handle_t	r_bh;
    114 	enum rtw_access		r_access;
    115 };
    116 
    117 #define RTW_SR_GET(sr, ofs) \
    118     (((sr)->sr_content[(ofs)/2] >> (((ofs) % 2 == 0) ? 0 : 8)) & 0xff)
    119 
    120 #define RTW_SR_GET16(sr, ofs) \
    121     (RTW_SR_GET((sr), (ofs)) | (RTW_SR_GET((sr), (ofs) + 1) << 8))
    122 
    123 struct rtw_srom {
    124 	uint16_t		*sr_content;
    125 	uint16_t		sr_size;
    126 };
    127 
    128 struct rtw_rxsoft {
    129 	struct mbuf			*rs_mbuf;
    130 	bus_dmamap_t			rs_dmamap;
    131 };
    132 
    133 struct rtw_txsoft {
    134 	SIMPLEQ_ENTRY(rtw_txsoft)	ts_q;
    135 	struct mbuf			*ts_mbuf;
    136 	bus_dmamap_t			ts_dmamap;
    137 	struct ieee80211_node		*ts_ni;	/* destination node */
    138 	u_int				ts_first;	/* 1st hw descriptor */
    139 	u_int				ts_last;	/* last hw descriptor */
    140 	struct ieee80211_duration	ts_d0;
    141 	struct ieee80211_duration	ts_dn;
    142 };
    143 
    144 #define RTW_NTXPRI	4	/* number of Tx priorities */
    145 #define RTW_TXPRILO	0
    146 #define RTW_TXPRIMD	1
    147 #define RTW_TXPRIHI	2
    148 #define RTW_TXPRIBCN	3	/* beacon priority */
    149 
    150 #define RTW_MAXPKTSEGS		64	/* Max 64 segments per Tx packet */
    151 
    152 #define CASSERT(cond, complaint) complaint[(cond) ? 0 : -1] = complaint[(cond) ? 0 : -1]
    153 
    154 /* Note well: the descriptor rings must begin on RTW_DESC_ALIGNMENT
    155  * boundaries.  I allocate them consecutively from one buffer, so
    156  * just round up.
    157  */
    158 #define RTW_TXQLENLO	64	/* low-priority queue length */
    159 #define RTW_TXQLENMD	64	/* medium-priority */
    160 #define RTW_TXQLENHI	64	/* high-priority */
    161 #define RTW_TXQLENBCN	2	/* beacon */
    162 
    163 #define RTW_NTXDESCLO	RTW_TXQLENLO
    164 #define RTW_NTXDESCMD	RTW_TXQLENMD
    165 #define RTW_NTXDESCHI	RTW_TXQLENHI
    166 #define RTW_NTXDESCBCN	RTW_TXQLENBCN
    167 
    168 #define RTW_NTXDESCTOTAL	(RTW_NTXDESCLO + RTW_NTXDESCMD + \
    169 				 RTW_NTXDESCHI + RTW_NTXDESCBCN)
    170 
    171 #define RTW_RXQLEN	64
    172 
    173 struct rtw_rxdesc_blk {
    174 	struct rtw_rxdesc	*rdb_desc;
    175 	u_int			rdb_next;
    176 	u_int			rdb_ndesc;
    177 	bus_dma_tag_t		rdb_dmat;
    178 	bus_dmamap_t		rdb_dmamap;
    179 };
    180 
    181 struct rtw_txdesc_blk {
    182 	u_int			tdb_ndesc;
    183 	u_int			tdb_next;
    184 	u_int			tdb_nfree;
    185 	bus_dma_tag_t		tdb_dmat;
    186 	bus_dmamap_t		tdb_dmamap;
    187 	bus_addr_t		tdb_physbase;
    188 	bus_addr_t		tdb_ofs;
    189 	struct rtw_txdesc	*tdb_desc;
    190 };
    191 
    192 #define RTW_NEXT_IDX(__htc, __idx)	(((__idx) + 1) % (__htc)->tdb_ndesc)
    193 
    194 #define RTW_NEXT_DESC(__htc, __idx) \
    195     ((__htc)->tdb_physbase + \
    196      sizeof(struct rtw_txdesc) * RTW_NEXT_IDX((__htc), (__idx)))
    197 
    198 SIMPLEQ_HEAD(rtw_txq, rtw_txsoft);
    199 
    200 struct rtw_txsoft_blk {
    201 	/* dirty/free s/w descriptors */
    202 	struct rtw_txq		tsb_dirtyq;
    203 	struct rtw_txq		tsb_freeq;
    204 	u_int			tsb_ndesc;
    205 	int			tsb_tx_timer;
    206 	struct rtw_txsoft	*tsb_desc;
    207 	uint8_t			tsb_poll;
    208 };
    209 
    210 struct rtw_descs {
    211 	struct rtw_txdesc	hd_txlo[RTW_NTXDESCLO];
    212 	struct rtw_txdesc	hd_txmd[RTW_NTXDESCMD];
    213 	struct rtw_txdesc	hd_txhi[RTW_NTXDESCMD];
    214 	struct rtw_rxdesc	hd_rx[RTW_RXQLEN];
    215 	struct rtw_txdesc	hd_bcn[RTW_NTXDESCBCN];
    216 };
    217 #define RTW_DESC_OFFSET(ring, i)	offsetof(struct rtw_descs, ring[i])
    218 #define RTW_RING_OFFSET(ring)		RTW_DESC_OFFSET(ring, 0)
    219 #define RTW_RING_BASE(sc, ring)		((sc)->sc_desc_physaddr + \
    220 					 RTW_RING_OFFSET(ring))
    221 
    222 /* Radio capture format for RTL8180. */
    223 
    224 #define RTW_RX_RADIOTAP_PRESENT					\
    225 	((1 << IEEE80211_RADIOTAP_TSFT)			|	\
    226 	 (1 << IEEE80211_RADIOTAP_FLAGS)		|	\
    227 	 (1 << IEEE80211_RADIOTAP_RATE)			|	\
    228 	 (1 << IEEE80211_RADIOTAP_CHANNEL)		|	\
    229 	 (1 << IEEE80211_RADIOTAP_LOCK_QUALITY)		|	\
    230 	 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL)		|	\
    231 	 0)
    232 
    233 struct rtw_rx_radiotap_header {
    234 	struct ieee80211_radiotap_header	rr_ihdr;
    235 	uint64_t				rr_tsft;
    236 	uint8_t				rr_flags;
    237 	uint8_t				rr_rate;
    238 	uint16_t				rr_chan_freq;
    239 	uint16_t				rr_chan_flags;
    240 	uint16_t				rr_barker_lock;
    241 	uint8_t				rr_antsignal;
    242 } __attribute__((__packed__));
    243 
    244 #define RTW_TX_RADIOTAP_PRESENT				\
    245 	((1 << IEEE80211_RADIOTAP_FLAGS)	|	\
    246 	 (1 << IEEE80211_RADIOTAP_RATE)		|	\
    247 	 (1 << IEEE80211_RADIOTAP_CHANNEL)	|	\
    248 	 0)
    249 
    250 struct rtw_tx_radiotap_header {
    251 	struct ieee80211_radiotap_header	rt_ihdr;
    252 	uint8_t				rt_flags;
    253 	uint8_t				rt_rate;
    254 	uint16_t				rt_chan_freq;
    255 	uint16_t				rt_chan_flags;
    256 } __attribute__((__packed__));
    257 
    258 enum rtw_attach_state {FINISHED, FINISH_DESCMAP_LOAD, FINISH_DESCMAP_CREATE,
    259 	FINISH_DESC_MAP, FINISH_DESC_ALLOC, FINISH_RXMAPS_CREATE,
    260 	FINISH_TXMAPS_CREATE, FINISH_RESET, FINISH_READ_SROM, FINISH_PARSE_SROM,
    261 	FINISH_RF_ATTACH, FINISH_ID_STA, FINISH_TXDESCBLK_SETUP,
    262 	FINISH_TXCTLBLK_SETUP, DETACHED};
    263 
    264 struct rtw_hooks {
    265 	void			*rh_shutdown;	/* shutdown hook */
    266 	void			*rh_power;	/* power management hook */
    267 };
    268 
    269 struct rtw_mtbl {
    270 	int			(*mt_newstate)(struct ieee80211com *,
    271 					enum ieee80211_state, int);
    272 	void			(*mt_recv_mgmt)(struct ieee80211com *,
    273 				    struct mbuf *, struct ieee80211_node *,
    274 				    int, int, uint32_t);
    275 	struct ieee80211_node	*(*mt_node_alloc)(struct ieee80211_node_table*);
    276 	void			(*mt_node_free)(struct ieee80211_node *);
    277 };
    278 
    279 enum rtw_pwrstate { RTW_OFF = 0, RTW_SLEEP, RTW_ON };
    280 
    281 typedef void (*rtw_continuous_tx_cb_t)(void *arg, int);
    282 
    283 struct rtw_phy {
    284 	struct rtw_rf	*p_rf;
    285 	struct rtw_regs	*p_regs;
    286 };
    287 
    288 struct rtw_bbpset {
    289 	u_int	bb_antatten;
    290 	u_int	bb_chestlim;
    291 	u_int	bb_chsqlim;
    292 	u_int	bb_ifagcdet;
    293 	u_int	bb_ifagcini;
    294 	u_int	bb_ifagclimit;
    295 	u_int	bb_lnadet;
    296 	u_int	bb_sys1;
    297 	u_int	bb_sys2;
    298 	u_int	bb_sys3;
    299 	u_int	bb_trl;
    300 	u_int	bb_txagc;
    301 };
    302 
    303 struct rtw_rf {
    304 	void	(*rf_destroy)(struct rtw_rf *);
    305 	/* args: frequency, txpower, power state */
    306 	int	(*rf_init)(struct rtw_rf *, u_int, uint8_t,
    307 	                  enum rtw_pwrstate);
    308 	/* arg: power state */
    309 	int	(*rf_pwrstate)(struct rtw_rf *, enum rtw_pwrstate);
    310 	/* arg: frequency */
    311 	int	(*rf_tune)(struct rtw_rf *, u_int);
    312 	/* arg: txpower */
    313 	int	(*rf_txpower)(struct rtw_rf *, uint8_t);
    314 	rtw_continuous_tx_cb_t	rf_continuous_tx_cb;
    315 	void			*rf_continuous_tx_arg;
    316 	struct rtw_bbpset	rf_bbpset;
    317 };
    318 
    319 static __inline void
    320 rtw_rf_destroy(struct rtw_rf *rf)
    321 {
    322 	(*rf->rf_destroy)(rf);
    323 }
    324 
    325 static __inline int
    326 rtw_rf_init(struct rtw_rf *rf, u_int freq, uint8_t opaque_txpower,
    327     enum rtw_pwrstate power)
    328 {
    329 	return (*rf->rf_init)(rf, freq, opaque_txpower, power);
    330 }
    331 
    332 static __inline int
    333 rtw_rf_pwrstate(struct rtw_rf *rf, enum rtw_pwrstate power)
    334 {
    335 	return (*rf->rf_pwrstate)(rf, power);
    336 }
    337 
    338 static __inline int
    339 rtw_rf_tune(struct rtw_rf *rf, u_int freq)
    340 {
    341 	return (*rf->rf_tune)(rf, freq);
    342 }
    343 
    344 static __inline int
    345 rtw_rf_txpower(struct rtw_rf *rf, uint8_t opaque_txpower)
    346 {
    347 	return (*rf->rf_txpower)(rf, opaque_txpower);
    348 }
    349 
    350 typedef int (*rtw_rf_write_t)(struct rtw_regs *, enum rtw_rfchipid, u_int,
    351     uint32_t);
    352 
    353 struct rtw_rfbus {
    354 	struct rtw_regs		*b_regs;
    355 	rtw_rf_write_t		b_write;
    356 };
    357 
    358 static __inline int
    359 rtw_rfbus_write(struct rtw_rfbus *bus, enum rtw_rfchipid rfchipid, u_int addr,
    360     uint32_t val)
    361 {
    362 	return (*bus->b_write)(bus->b_regs, rfchipid, addr, val);
    363 }
    364 
    365 struct rtw_max2820 {
    366 	struct rtw_rf		mx_rf;
    367 	struct rtw_rfbus	mx_bus;
    368 	int			mx_is_a;	/* 1: MAX2820A/MAX2821A */
    369 };
    370 
    371 struct rtw_sa2400 {
    372 	struct rtw_rf		sa_rf;
    373 	struct rtw_rfbus	sa_bus;
    374 	int			sa_digphy;	/* 1: digital PHY */
    375 };
    376 
    377 typedef void (*rtw_pwrstate_t)(struct rtw_regs *, enum rtw_pwrstate, int, int);
    378 
    379 union rtw_keys {
    380 	uint8_t		rk_keys[4][16];
    381 	uint32_t	rk_words[16];
    382 };
    383 
    384 #define	RTW_LED_SLOW_TICKS	MAX(1, hz/2)
    385 #define	RTW_LED_FAST_TICKS	MAX(1, hz/10)
    386 
    387 struct rtw_led_state {
    388 #define	RTW_LED0	0x1
    389 #define	RTW_LED1	0x2
    390 	uint8_t		ls_slowblink:2;
    391 	uint8_t		ls_actblink:2;
    392 	uint8_t		ls_default:2;
    393 	uint8_t		ls_state;
    394 	uint8_t		ls_event;
    395 #define	RTW_LED_S_RX	0x1
    396 #define	RTW_LED_S_TX	0x2
    397 #define	RTW_LED_S_SLOW	0x4
    398 	struct callout	ls_slow_ch;
    399 	struct callout	ls_fast_ch;
    400 };
    401 
    402 struct rtw_softc {
    403 	struct device		sc_dev;
    404 	struct ethercom		sc_ec;
    405 	struct ieee80211com	sc_ic;
    406 	struct rtw_regs		sc_regs;
    407 	bus_dma_tag_t		sc_dmat;
    408 	uint32_t		sc_flags;
    409 
    410 	enum rtw_attach_state	sc_attach_state;
    411 	enum rtw_rfchipid	sc_rfchipid;
    412 	enum rtw_locale		sc_locale;
    413 	uint8_t		sc_phydelay;
    414 
    415 	/* s/w Tx/Rx descriptors */
    416 	struct rtw_txsoft_blk	sc_txsoft_blk[RTW_NTXPRI];
    417 	struct rtw_txdesc_blk	sc_txdesc_blk[RTW_NTXPRI];
    418 
    419 	struct rtw_rxsoft	sc_rxsoft[RTW_RXQLEN];
    420 	struct rtw_rxdesc_blk	sc_rxdesc_blk;
    421 
    422 	struct rtw_descs	*sc_descs;
    423 
    424 	bus_dma_segment_t	sc_desc_segs;
    425 	int			sc_desc_nsegs;
    426 	bus_dmamap_t		sc_desc_dmamap;
    427 #define	sc_desc_physaddr sc_desc_dmamap->dm_segs[0].ds_addr
    428 
    429 	struct rtw_srom		sc_srom;
    430 
    431 	enum rtw_pwrstate	sc_pwrstate;
    432 
    433 	rtw_pwrstate_t		sc_pwrstate_cb;
    434 
    435 	struct rtw_rf		*sc_rf;
    436 
    437 	uint16_t		sc_inten;
    438 
    439 	/* interrupt acknowledge hook */
    440 	void (*sc_intr_ack)(struct rtw_regs *);
    441 
    442 	int			(*sc_enable)(struct rtw_softc *);
    443 	void			(*sc_disable)(struct rtw_softc *);
    444 	void			(*sc_power)(struct rtw_softc *, int);
    445 	struct rtw_mtbl		sc_mtbl;
    446 	struct rtw_hooks	sc_hooks;
    447 
    448 	caddr_t			sc_radiobpf;
    449 
    450 	struct callout		sc_scan_ch;
    451 	u_int			sc_cur_chan;
    452 
    453 	uint32_t		sc_tsfth;	/* most significant TSFT bits */
    454 	uint32_t		sc_rcr;		/* RTW_RCR */
    455 	uint8_t		sc_csthr;	/* carrier-sense threshold */
    456 
    457 	int			sc_do_tick;	/* indicate 1s ticks */
    458 	struct timeval		sc_tick0;	/* first tick */
    459 
    460 	uint8_t			sc_rev;		/* PCI/Cardbus revision */
    461 
    462 	uint32_t		sc_anaparm;	/* register RTW_ANAPARM */
    463 
    464 	union {
    465 		struct rtw_rx_radiotap_header	tap;
    466 		uint8_t			pad[64];
    467 	} sc_rxtapu;
    468 	union {
    469 		struct rtw_tx_radiotap_header	tap;
    470 		uint8_t			pad[64];
    471 	} sc_txtapu;
    472 	union rtw_keys		sc_keys;
    473 	int			sc_txkey;
    474 	struct ifqueue		sc_beaconq;
    475 	struct rtw_led_state	sc_led_state;
    476 	int			sc_hwverid;
    477 };
    478 
    479 #define	sc_if		sc_ec.ec_if
    480 #define sc_rxtap	sc_rxtapu.tap
    481 #define sc_txtap	sc_txtapu.tap
    482 
    483 extern int rtw_host_rfio;
    484 
    485 void rtw_txdac_enable(struct rtw_softc *, int);
    486 void rtw_anaparm_enable(struct rtw_regs *, int);
    487 void rtw_config0123_enable(struct rtw_regs *, int);
    488 void rtw_continuous_tx_enable(struct rtw_softc *, int);
    489 void rtw_set_access(struct rtw_regs *, enum rtw_access);
    490 
    491 void rtw_attach(struct rtw_softc *);
    492 int rtw_detach(struct rtw_softc *);
    493 int rtw_intr(void *);
    494 
    495 void rtw_disable(struct rtw_softc *);
    496 int rtw_enable(struct rtw_softc *);
    497 
    498 int rtw_activate(struct device *, enum devact);
    499 void rtw_power(int, void *);
    500 void rtw_shutdown(void *);
    501 
    502 const char *rtw_pwrstate_string(enum rtw_pwrstate);
    503 
    504 #endif /* _DEV_IC_RTWVAR_H_ */
    505