Merge from vendor branch WPA_SUPPLICANT:
[dragonfly.git] / sys / dev / netif / acx / acx100.c
1 /*
2  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
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  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/dev/netif/acx/acx100.c,v 1.3 2006/06/17 10:31:59 sephe Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/endian.h>
40 #include <sys/rman.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46
47 #include <net/if.h>
48 #include <net/if_arp.h>
49 #include <net/if_media.h>
50
51 #include <netproto/802_11/ieee80211.h>
52 #include <netproto/802_11/ieee80211_var.h>
53
54 #include <bus/pci/pcireg.h>
55
56 #define ACX_DEBUG
57
58 #include "if_acxreg.h"
59 #include "if_acxvar.h"
60 #include "acxcmd.h"
61
62 #define ACX100_CONF_FW_RING     0x0003
63 #define ACX100_CONF_MEMOPT      0x0005
64
65 #define ACX100_INTR_ENABLE      (ACXRV_INTR_TX_FINI | ACXRV_INTR_RX_FINI)
66 /*
67  * XXX do we really care about following interrupts?
68  *
69  * ACXRV_INTR_INFO | ACXRV_INTR_SCAN_FINI
70  */
71
72 #define ACX100_INTR_DISABLE     (uint16_t)~(ACXRV_INTR_UNKN)
73
74 #define ACX100_RATE(rate)       ((rate) * 5)
75
76 #define ACX100_TXPOWER          18
77 #define ACX100_GPIO_POWER_LED   0x0800
78 #define ACX100_EE_EADDR_OFS     0x1a
79
80 #define ACX100_FW_TXRING_SIZE   (ACX_TX_DESC_CNT * sizeof(struct acx_fw_txdesc))
81 #define ACX100_FW_RXRING_SIZE   (ACX_RX_DESC_CNT * sizeof(struct acx_fw_rxdesc))
82
83 /*
84  * NOTE:
85  * Following structs' fields are little endian
86  */
87
88 struct acx100_bss_join {
89         uint8_t dtim_intvl;
90         uint8_t basic_rates;
91         uint8_t all_rates;
92 } __packed;
93
94 struct acx100_conf_fw_ring {
95         struct acx_conf confcom;
96         uint32_t        fw_ring_size;   /* total size of fw (tx + rx) ring */
97         uint32_t        fw_rxring_addr; /* start phyaddr of fw rx desc */
98         uint8_t         opt;            /* see ACX100_RINGOPT_ */
99         uint8_t         fw_txring_num;  /* num of TX ring */
100         uint8_t         fw_rxdesc_num;  /* num of fw rx desc */
101         uint8_t         reserved0;
102         uint32_t        fw_ring_end[2]; /* see ACX100_SET_RING_END() */
103         uint32_t        fw_txring_addr; /* start phyaddr of fw tx desc */
104         uint8_t         fw_txring_prio; /* see ACX100_TXRING_PRIO_ */
105         uint8_t         fw_txdesc_num;  /* num of fw tx desc */
106         uint16_t        reserved1;
107 } __packed;
108
109 #define ACX100_RINGOPT_AUTO_RESET       0x1
110 #define ACX100_TXRING_PRIO_DEFAULT      0
111 #define ACX100_SET_RING_END(conf, end)                  \
112 do {                                                    \
113         (conf)->fw_ring_end[0] = htole32(end);          \
114         (conf)->fw_ring_end[1] = htole32(end + 8);      \
115 } while (0)
116
117 struct acx100_conf_memblk_size {
118         struct acx_conf confcom;
119         uint16_t        memblk_size;    /* size of each mem block */
120 } __packed;
121
122 struct acx100_conf_mem {
123         struct acx_conf confcom;
124         uint32_t        opt;            /* see ACX100_MEMOPT_ */
125         uint32_t        h_rxring_paddr; /* host rx desc start phyaddr */
126
127         /*
128          * Memory blocks are controled by hardware
129          * once after they are initialized
130          */
131         uint32_t        rx_memblk_addr; /* start addr of rx mem blocks */
132         uint32_t        tx_memblk_addr; /* start addr of tx mem blocks */
133         uint16_t        rx_memblk_num;  /* num of RX mem block */
134         uint16_t        tx_memblk_num;  /* num of TX mem block */
135 } __packed;
136
137 #define ACX100_MEMOPT_MEM_INSTR         0x00000000 /* memory access instruct */
138 #define ACX100_MEMOPT_HOSTDESC          0x00010000 /* host indirect desc */
139 #define ACX100_MEMOPT_MEMBLOCK          0x00020000 /* local mem block list */
140 #define ACX100_MEMOPT_IO_INSTR          0x00040000 /* IO instruct */
141 #define ACX100_MEMOPT_PCICONF           0x00080000 /* PCI conf space */
142
143 #define ACX100_MEMBLK_ALIGN             0x20
144
145 struct acx100_conf_cca_mode {
146         struct acx_conf confcom;
147         uint8_t         cca_mode;
148         uint8_t         unknown;
149 } __packed;
150
151 struct acx100_conf_ed_thresh {
152         struct acx_conf confcom;
153         uint8_t         ed_thresh;
154         uint8_t         unknown[3];
155 } __packed;
156
157 struct acx100_conf_wepkey {
158         struct acx_conf confcom;
159         uint8_t         action; /* see ACX100_WEPKEY_ACT_ */
160         uint8_t         key_len;
161         uint8_t         key_idx;
162 #define ACX100_WEPKEY_LEN       29
163         uint8_t         key[ACX100_WEPKEY_LEN];
164 } __packed;
165
166 #define ACX100_WEPKEY_ACT_ADD   1
167
168 #define ACX100_CONF_FUNC(sg, name)      _ACX_CONF_FUNC(sg, name, 100)
169 #define ACX_CONF_fw_ring                ACX100_CONF_FW_RING
170 #define ACX_CONF_memblk_size            ACX_CONF_MEMBLK_SIZE
171 #define ACX_CONF_mem                    ACX100_CONF_MEMOPT
172 #define ACX_CONF_cca_mode               ACX_CONF_CCA_MODE
173 #define ACX_CONF_ed_thresh              ACX_CONF_ED_THRESH
174 #define ACX_CONF_wepkey                 ACX_CONF_WEPKEY
175 ACX100_CONF_FUNC(set, fw_ring);
176 ACX100_CONF_FUNC(set, memblk_size);
177 ACX100_CONF_FUNC(set, mem);
178 ACX100_CONF_FUNC(get, cca_mode);
179 ACX100_CONF_FUNC(set, cca_mode);
180 ACX100_CONF_FUNC(get, ed_thresh);
181 ACX100_CONF_FUNC(set, ed_thresh);
182 ACX100_CONF_FUNC(set, wepkey);
183
184 #define ACXCMD_init_mem                 ACXCMD_INIT_MEM
185 ACX_NOARG_FUNC(init_mem);
186
187 static const uint16_t   acx100_reg[ACXREG_MAX] = {
188         ACXREG(SOFT_RESET,              0x0000),
189
190         ACXREG(FWMEM_ADDR,              0x0014),
191         ACXREG(FWMEM_DATA,              0x0018),
192         ACXREG(FWMEM_CTRL,              0x001c),
193         ACXREG(FWMEM_START,             0x0020),
194
195         ACXREG(EVENT_MASK,              0x0034),
196
197         ACXREG(INTR_TRIG,               0x007c),
198         ACXREG(INTR_MASK,               0x0098),
199         ACXREG(INTR_STATUS,             0x00a4),
200         ACXREG(INTR_STATUS_CLR,         0x00a8),
201         ACXREG(INTR_ACK,                0x00ac),
202
203         ACXREG(HINTR_TRIG,              0x00b0),
204         ACXREG(RADIO_ENABLE,            0x0104),
205
206         ACXREG(EEPROM_INIT,             0x02d0),
207         ACXREG(EEPROM_CTRL,             0x0250),
208         ACXREG(EEPROM_ADDR,             0x0254),
209         ACXREG(EEPROM_DATA,             0x0258),
210         ACXREG(EEPROM_CONF,             0x025c),
211         ACXREG(EEPROM_INFO,             0x02ac),
212
213         ACXREG(PHY_ADDR,                0x0268),
214         ACXREG(PHY_DATA,                0x026c),
215         ACXREG(PHY_CTRL,                0x0270),
216
217         ACXREG(GPIO_OUT_ENABLE,         0x0290),
218         ACXREG(GPIO_OUT,                0x0298),
219
220         ACXREG(CMD_REG_OFFSET,          0x02a4),
221         ACXREG(INFO_REG_OFFSET,         0x02a8),
222
223         ACXREG(RESET_SENSE,             0x02d4),
224         ACXREG(ECPU_CTRL,               0x02d8) 
225 };
226
227 static const uint8_t    acx100_txpower_maxim[21] = {
228         63, 63, 63, 62,
229         61, 61, 60, 60,
230         59, 58, 57, 55,
231         53, 50, 47, 43,
232         38, 31, 23, 13,
233         0
234 };
235
236 static const uint8_t    acx100_txpower_rfmd[21] = {
237          0,  0,  0,  1,
238          2,  2,  3,  3,
239          4,  5,  6,  8,
240         10, 13, 16, 20,
241         25, 32, 41, 50,
242         63
243 };
244
245 static int      acx100_init(struct acx_softc *);
246 static int      acx100_init_wep(struct acx_softc *);
247 static int      acx100_init_tmplt(struct acx_softc *);
248 static int      acx100_init_fw_ring(struct acx_softc *);
249 static int      acx100_init_memory(struct acx_softc *);
250
251 static void     acx100_init_fw_txring(struct acx_softc *, uint32_t);
252 static void     acx100_init_fw_rxring(struct acx_softc *, uint32_t);
253
254 static int      acx100_read_config(struct acx_softc *, struct acx_config *);
255 static int      acx100_write_config(struct acx_softc *, struct acx_config *);
256
257 static int      acx100_set_txpower(struct acx_softc *);
258
259 static void     acx100_set_fw_txdesc_rate(struct acx_softc *,
260                                           struct acx_txbuf *, int);
261 static void     acx100_set_bss_join_param(struct acx_softc *, void *, int);
262
263 static int      acx100_set_wepkey(struct acx_softc *, struct ieee80211_key *,
264                                   int);
265
266 static void     acx100_proc_wep_rxbuf(struct acx_softc *, struct mbuf *, int *);
267
268 void
269 acx100_set_param(device_t dev)
270 {
271         struct acx_softc *sc = device_get_softc(dev);
272
273         sc->chip_mem1_rid = PCIR_BAR(1);
274         sc->chip_mem2_rid = PCIR_BAR(2);
275         sc->chip_ioreg = acx100_reg;
276         sc->chip_hw_crypt = 1;
277         sc->chip_intr_enable = ACX100_INTR_ENABLE;
278         sc->chip_intr_disable = ACX100_INTR_DISABLE;
279         sc->chip_gpio_pled = ACX100_GPIO_POWER_LED;
280         sc->chip_ee_eaddr_ofs = ACX100_EE_EADDR_OFS;
281         sc->chip_txdesc1_len = ACX_FRAME_HDRLEN;
282         sc->chip_fw_txdesc_ctrl = DESC_CTRL_AUTODMA |
283                                   DESC_CTRL_RECLAIM |
284                                   DESC_CTRL_FIRST_FRAG;
285
286         sc->chip_phymode = IEEE80211_MODE_11B;
287         sc->chip_chan_flags = IEEE80211_CHAN_B;
288         sc->sc_ic.ic_phytype = IEEE80211_T_DS;
289         sc->sc_ic.ic_sup_rates[IEEE80211_MODE_11B] = acx_rates_11b;
290
291         sc->chip_init = acx100_init;
292         sc->chip_set_wepkey = acx100_set_wepkey;
293         sc->chip_read_config = acx100_read_config;
294         sc->chip_write_config = acx100_write_config;
295         sc->chip_set_fw_txdesc_rate = acx100_set_fw_txdesc_rate;
296         sc->chip_set_bss_join_param = acx100_set_bss_join_param;
297         sc->chip_proc_wep_rxbuf = acx100_proc_wep_rxbuf;
298 }
299
300 static int
301 acx100_init(struct acx_softc *sc)
302 {
303         /*
304          * NOTE:
305          * Order of initialization:
306          * 1) WEP
307          * 2) Templates
308          * 3) Firmware TX/RX ring
309          * 4) Hardware memory
310          * Above order is critical to get a correct memory map
311          */
312
313         if (acx100_init_wep(sc) != 0) {
314                 if_printf(&sc->sc_ic.ic_if, "%s can't initialize wep\n",
315                           __func__);
316                 return ENXIO;
317         }
318
319         if (acx100_init_tmplt(sc) != 0) {
320                 if_printf(&sc->sc_ic.ic_if, "%s can't initialize templates\n",
321                           __func__);
322                 return ENXIO;
323         }
324
325         if (acx100_init_fw_ring(sc) != 0) {
326                 if_printf(&sc->sc_ic.ic_if, "%s can't initialize fw ring\n",
327                           __func__);
328                 return ENXIO;
329         }
330
331         if (acx100_init_memory(sc) != 0) {
332                 if_printf(&sc->sc_ic.ic_if, "%s can't initialize hw memory\n",
333                           __func__);
334                 return ENXIO;
335         }
336         return 0;
337 }
338
339 static int
340 acx100_init_wep(struct acx_softc *sc)
341 {
342         struct acx_conf_wepopt wep_opt;
343         struct acx_conf_mmap mem_map;
344
345         /* Set WEP cache start/end address */
346         if (acx_get_mmap_conf(sc, &mem_map) != 0) {
347                 if_printf(&sc->sc_ic.ic_if, "can't get mmap\n");
348                 return 1;
349         }
350
351         mem_map.wep_cache_start = htole32(le32toh(mem_map.code_end) + 4);
352         mem_map.wep_cache_end = htole32(le32toh(mem_map.code_end) + 4);
353         if (acx_set_mmap_conf(sc, &mem_map) != 0) {
354                 if_printf(&sc->sc_ic.ic_if, "can't set mmap\n");
355                 return 1;
356         }
357
358         /* Set WEP options */
359         wep_opt.nkey = htole16(IEEE80211_WEP_NKID + 10);
360         wep_opt.opt = WEPOPT_HDWEP;
361         if (acx_set_wepopt_conf(sc, &wep_opt) != 0) {
362                 if_printf(&sc->sc_ic.ic_if, "can't set wep opt\n");
363                 return 1;
364         }
365         return 0;
366 }
367
368 static int
369 acx100_init_tmplt(struct acx_softc *sc)
370 {
371         struct acx_conf_mmap mem_map;
372         struct acx_tmplt_tim tim;
373
374         /* Set templates start address */
375         if (acx_get_mmap_conf(sc, &mem_map) != 0) {
376                 if_printf(&sc->sc_ic.ic_if, "can't get mmap\n");
377                 return 1;
378         }
379
380         mem_map.pkt_tmplt_start = mem_map.wep_cache_end;
381         if (acx_set_mmap_conf(sc, &mem_map) != 0) {
382                 if_printf(&sc->sc_ic.ic_if, "can't set mmap\n");
383                 return 1;
384         }
385
386         /* Initialize various packet templates */
387         if (acx_init_tmplt_ordered(sc) != 0) {
388                 if_printf(&sc->sc_ic.ic_if, "can't init tmplt\n");
389                 return 1;
390         }
391
392         /* Setup TIM template */
393         bzero(&tim, sizeof(tim));
394         tim.tim_eid = IEEE80211_ELEMID_TIM;
395         tim.tim_len = ACX_TIM_LEN(ACX_TIM_BITMAP_LEN);
396         if (_acx_set_tim_tmplt(sc, &tim,
397                                ACX_TMPLT_TIM_SIZ(ACX_TIM_BITMAP_LEN)) != 0) {
398                 if_printf(&sc->sc_ic.ic_if, "can't set tim tmplt\n");
399                 return 1;
400         }
401         return 0;
402 }
403
404 static int
405 acx100_init_fw_ring(struct acx_softc *sc)
406 {
407         struct acx100_conf_fw_ring ring;
408         struct acx_conf_mmap mem_map;
409         uint32_t txring_start, rxring_start, ring_end;
410
411         /* Set firmware descriptor ring start address */
412         if (acx_get_mmap_conf(sc, &mem_map) != 0) {
413                 if_printf(&sc->sc_ic.ic_if, "can't get mmap\n");
414                 return 1;
415         }
416
417         txring_start = le32toh(mem_map.pkt_tmplt_end) + 4;
418         rxring_start = txring_start + ACX100_FW_TXRING_SIZE;
419         ring_end = rxring_start + ACX100_FW_RXRING_SIZE;
420
421         mem_map.fw_desc_start = htole32(txring_start);
422         if (acx_set_mmap_conf(sc, &mem_map) != 0) {
423                 if_printf(&sc->sc_ic.ic_if, "can't set mmap\n");
424                 return 1;
425         }
426
427         /* Set firmware descriptor ring configure */
428         bzero(&ring, sizeof(ring));
429         ring.fw_ring_size = htole32(ACX100_FW_TXRING_SIZE +
430                                     ACX100_FW_RXRING_SIZE + 8);
431
432         ring.fw_txring_num = 1;
433         ring.fw_txring_addr = htole32(txring_start);
434         ring.fw_txring_prio = ACX100_TXRING_PRIO_DEFAULT;
435         ring.fw_txdesc_num = 0; /* XXX ignored?? */
436
437         ring.fw_rxring_addr = htole32(rxring_start);
438         ring.fw_rxdesc_num = 0; /* XXX ignored?? */
439
440         ring.opt = ACX100_RINGOPT_AUTO_RESET;
441         ACX100_SET_RING_END(&ring, ring_end);
442         if (acx100_set_fw_ring_conf(sc, &ring) != 0) {
443                 if_printf(&sc->sc_ic.ic_if, "can't set fw ring configure\n");
444                 return 1;
445         }
446
447         /* Setup firmware TX/RX descriptor ring */
448         acx100_init_fw_txring(sc, txring_start);
449         acx100_init_fw_rxring(sc, rxring_start);
450
451         return 0;
452 }
453
454 #define MEMBLK_ALIGN(addr)      \
455         (((addr) + (ACX100_MEMBLK_ALIGN - 1)) & ~(ACX100_MEMBLK_ALIGN - 1))
456
457 static int
458 acx100_init_memory(struct acx_softc *sc)
459 {
460         struct acx100_conf_memblk_size memblk_sz;
461         struct acx100_conf_mem mem;
462         struct acx_conf_mmap mem_map;
463         uint32_t memblk_start, memblk_end;
464         int total_memblk, txblk_num, rxblk_num;
465
466         /* Set memory block start address */
467         if (acx_get_mmap_conf(sc, &mem_map) != 0) {
468                 if_printf(&sc->sc_ic.ic_if, "can't get mmap\n");
469                 return 1;
470         }
471
472         mem_map.memblk_start =
473                 htole32(MEMBLK_ALIGN(le32toh(mem_map.fw_desc_end) + 4));
474
475         if (acx_set_mmap_conf(sc, &mem_map) != 0) {
476                 if_printf(&sc->sc_ic.ic_if, "can't set mmap\n");
477                 return 1;
478         }
479
480         /* Set memory block size */
481         memblk_sz.memblk_size = htole16(ACX_MEMBLOCK_SIZE);
482         if (acx100_set_memblk_size_conf(sc, &memblk_sz) != 0) {
483                 if_printf(&sc->sc_ic.ic_if, "can't set mem block size\n");
484                 return 1;
485         }
486
487         /* Get memory map after setting it */
488         if (acx_get_mmap_conf(sc, &mem_map) != 0) {
489                 if_printf(&sc->sc_ic.ic_if, "can't get mmap again\n");
490                 return 1;
491         }
492         memblk_start = le32toh(mem_map.memblk_start);
493         memblk_end = le32toh(mem_map.memblk_end);
494
495         /* Set memory options */
496         mem.opt = htole32(ACX100_MEMOPT_MEMBLOCK | ACX100_MEMOPT_HOSTDESC);
497         mem.h_rxring_paddr = htole32(sc->sc_ring_data.rx_ring_paddr);
498
499         total_memblk = (memblk_end - memblk_start) / ACX_MEMBLOCK_SIZE;
500
501         rxblk_num = total_memblk / 2;           /* 50% */
502         txblk_num = total_memblk - rxblk_num;   /* 50% */
503
504         DPRINTF((&sc->sc_ic.ic_if, "\ttotal memory blocks\t%d\n"
505                                    "\trx memory blocks\t%d\n"
506                                    "\ttx memory blocks\t%d\n",
507                                    total_memblk, rxblk_num, txblk_num));
508
509         mem.rx_memblk_num = htole16(rxblk_num);
510         mem.tx_memblk_num = htole16(txblk_num);
511
512         mem.rx_memblk_addr = htole32(MEMBLK_ALIGN(memblk_start));
513         mem.tx_memblk_addr =
514                 htole32(MEMBLK_ALIGN(memblk_start +
515                                      (ACX_MEMBLOCK_SIZE * rxblk_num)));
516
517         if (acx100_set_mem_conf(sc, &mem) != 0) {
518                 if_printf(&sc->sc_ic.ic_if, "can't set mem options\n");
519                 return 1;
520         }
521
522         /* Initialize memory */
523         if (acx_init_mem(sc) != 0) {
524                 if_printf(&sc->sc_ic.ic_if, "can't init mem\n");
525                 return 1;
526         }
527         return 0;
528 }
529
530 #undef MEMBLK_ALIGN
531
532 static void
533 acx100_init_fw_txring(struct acx_softc *sc, uint32_t fw_txdesc_start)
534 {
535         struct acx_fw_txdesc fw_desc;
536         struct acx_txbuf *tx_buf;
537         uint32_t desc_paddr, fw_desc_offset;
538         int i;
539
540         bzero(&fw_desc, sizeof(fw_desc));
541         fw_desc.f_tx_ctrl = DESC_CTRL_HOSTOWN |
542                             DESC_CTRL_RECLAIM |
543                             DESC_CTRL_AUTODMA |
544                             DESC_CTRL_FIRST_FRAG;
545
546         tx_buf = sc->sc_buf_data.tx_buf;
547         fw_desc_offset = fw_txdesc_start;
548         desc_paddr = sc->sc_ring_data.tx_ring_paddr;
549
550         for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
551                 fw_desc.f_tx_host_desc = htole32(desc_paddr);
552
553                 if (i == ACX_TX_DESC_CNT - 1) {
554                         fw_desc.f_tx_next_desc = htole32(fw_txdesc_start);
555                 } else {
556                         fw_desc.f_tx_next_desc =
557                                 htole32(fw_desc_offset +
558                                         sizeof(struct acx_fw_txdesc));
559                 }
560
561                 tx_buf[i].tb_fwdesc_ofs = fw_desc_offset;
562                 DESC_WRITE_REGION_1(sc, fw_desc_offset, &fw_desc,
563                                     sizeof(fw_desc));
564
565                 desc_paddr += (2 * sizeof(struct acx_host_desc));
566                 fw_desc_offset += sizeof(fw_desc);
567         }
568 }
569
570 static void
571 acx100_init_fw_rxring(struct acx_softc *sc, uint32_t fw_rxdesc_start)
572 {
573         struct acx_fw_rxdesc fw_desc;
574         uint32_t fw_desc_offset;
575         int i;
576
577         bzero(&fw_desc, sizeof(fw_desc));
578         fw_desc.f_rx_ctrl = DESC_CTRL_RECLAIM | DESC_CTRL_AUTODMA;
579
580         fw_desc_offset = fw_rxdesc_start;
581
582         for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
583                 if (i == ACX_RX_DESC_CNT - 1) {
584                         fw_desc.f_rx_next_desc = htole32(fw_rxdesc_start);
585                 } else {
586                         fw_desc.f_rx_next_desc =
587                                 htole32(fw_desc_offset +
588                                         sizeof(struct acx_fw_rxdesc));
589                 }
590
591                 DESC_WRITE_REGION_1(sc, fw_desc_offset, &fw_desc,
592                                     sizeof(fw_desc));
593
594                 fw_desc_offset += sizeof(fw_desc);
595         }
596 }
597
598 static int
599 acx100_read_config(struct acx_softc *sc, struct acx_config *conf)
600 {
601         struct acx100_conf_cca_mode cca;
602         struct acx100_conf_ed_thresh ed;
603
604         /*
605          * NOTE:
606          * CCA mode and ED threshold MUST be read during initialization
607          * or the acx100 card won't work as expected
608          */
609
610         /* Get CCA mode */
611         if (acx100_get_cca_mode_conf(sc, &cca) != 0) {
612                 if_printf(&sc->sc_ic.ic_if, "%s can't get cca mode\n",
613                           __func__);
614                 return ENXIO;
615         }
616         conf->cca_mode = cca.cca_mode;
617         DPRINTF((&sc->sc_ic.ic_if, "cca mode %02x\n", cca.cca_mode));
618
619         /* Get ED threshold */
620         if (acx100_get_ed_thresh_conf(sc, &ed) != 0) {
621                 if_printf(&sc->sc_ic.ic_if, "%s can't get ed threshold\n",
622                           __func__);
623                 return ENXIO;
624         }
625         conf->ed_thresh = ed.ed_thresh;
626         DPRINTF((&sc->sc_ic.ic_if, "ed threshold %02x\n", ed.ed_thresh));
627
628         return 0;
629 }
630
631 static int
632 acx100_write_config(struct acx_softc *sc, struct acx_config *conf)
633 {
634         struct acx100_conf_cca_mode cca;
635         struct acx100_conf_ed_thresh ed;
636
637         /* Set CCA mode */
638         cca.cca_mode = conf->cca_mode;
639         if (acx100_set_cca_mode_conf(sc, &cca) != 0) {
640                 if_printf(&sc->sc_ic.ic_if, "%s can't set cca mode\n",
641                           __func__);
642                 return ENXIO;
643         }
644
645         /* Set ED threshold */
646         ed.ed_thresh = conf->ed_thresh;
647         if (acx100_set_ed_thresh_conf(sc, &ed) != 0) {
648                 if_printf(&sc->sc_ic.ic_if, "%s can't set ed threshold\n",
649                           __func__);
650                 return ENXIO;
651         }
652
653         /* Set TX power */
654         acx100_set_txpower(sc); /* ignore return value */
655
656         return 0;
657 }
658
659 static int
660 acx100_set_txpower(struct acx_softc *sc)
661 {
662         const uint8_t *map;
663
664         switch (sc->sc_radio_type) {
665         case ACX_RADIO_TYPE_MAXIM:
666                 map = acx100_txpower_maxim;
667                 break;
668         case ACX_RADIO_TYPE_RFMD:
669         case ACX_RADIO_TYPE_RALINK:
670                 map = acx100_txpower_rfmd;
671                 break;
672         default:
673                 if_printf(&sc->sc_ic.ic_if, "TX power for radio type 0x%02x "
674                           "can't be set yet\n", sc->sc_radio_type);
675                 return 1;
676         }
677
678         acx_write_phyreg(sc, ACXRV_PHYREG_TXPOWER, map[ACX100_TXPOWER]);
679         return 0;
680 }
681
682 static void
683 acx100_set_fw_txdesc_rate(struct acx_softc *sc, struct acx_txbuf *tx_buf,
684                           int rate)
685 {
686         FW_TXDESC_SETFIELD_1(sc, tx_buf, f_tx_rate100, ACX100_RATE(rate));
687 }
688
689 static void
690 acx100_set_bss_join_param(struct acx_softc *sc, void *param, int dtim_intvl)
691 {
692         struct acx100_bss_join *bj = param;
693
694         bj->dtim_intvl = dtim_intvl;
695         bj->basic_rates = 15;   /* XXX */
696         bj->all_rates = 31;     /* XXX */
697 }
698
699 static int
700 acx100_set_wepkey(struct acx_softc *sc, struct ieee80211_key *wk, int wk_idx)
701 {
702         struct acx100_conf_wepkey conf_wk;
703
704         if (wk->wk_keylen > ACX100_WEPKEY_LEN) {
705                 if_printf(&sc->sc_ic.ic_if, "%dth WEP key size beyond %d\n",
706                           wk_idx, ACX100_WEPKEY_LEN);
707                 return EINVAL;
708         }
709
710         conf_wk.action = ACX100_WEPKEY_ACT_ADD;
711         conf_wk.key_len = wk->wk_keylen;
712         conf_wk.key_idx = wk_idx;
713         bcopy(wk->wk_key, conf_wk.key, wk->wk_keylen);
714         if (acx100_set_wepkey_conf(sc, &conf_wk) != 0) {
715                 if_printf(&sc->sc_ic.ic_if, "%s set %dth WEP key failed\n",
716                           __func__, wk_idx);
717                 return ENXIO;
718         }
719         return 0;
720 }
721
722 static void
723 acx100_proc_wep_rxbuf(struct acx_softc *sc, struct mbuf *m, int *len)
724 {
725         int mac_hdrlen;
726         struct ieee80211_frame *f;
727
728         /*
729          * Strip leading IV and KID, and trailing CRC
730          */
731
732         f = mtod(m, struct ieee80211_frame *);
733
734         if ((f->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
735                 mac_hdrlen = sizeof(struct ieee80211_frame_addr4);
736         else
737                 mac_hdrlen = sizeof(struct ieee80211_frame);
738
739 #define IEEEWEP_IVLEN   (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
740 #define IEEEWEP_EXLEN   (IEEEWEP_IVLEN + IEEE80211_WEP_CRCLEN)
741
742         *len = *len - IEEEWEP_EXLEN;
743
744         /* Move MAC header toward frame body */
745         ovbcopy(f, (uint8_t *)f + IEEEWEP_IVLEN, mac_hdrlen);
746         m_adj(m, IEEEWEP_IVLEN);
747
748 #undef IEEEWEP_EXLEN
749 #undef IEEEWEP_IVLEN
750 }