Merge from vendor branch BIND:
[dragonfly.git] / sys / dev / netif / acx / acxcmd.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/acxcmd.c,v 1.6 2007/02/15 09:05:11 sephe Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/rman.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/if_media.h>
48
49 #include <netproto/802_11/ieee80211_var.h>
50
51 #define ACX_DEBUG
52
53 #include "if_acxreg.h"
54 #include "if_acxvar.h"
55 #include "acxcmd.h"
56
57 #define CMDPRM_WRITE_REGION_1(sc, r, rlen)              \
58         bus_space_write_region_1((sc)->sc_mem2_bt,      \
59                                  (sc)->sc_mem2_bh,      \
60                                  (sc)->sc_cmd_param,    \
61                                  (const uint8_t *)(r), (rlen))
62
63 #define CMDPRM_READ_REGION_1(sc, r, rlen)                               \
64         bus_space_read_region_1((sc)->sc_mem2_bt, (sc)->sc_mem2_bh,     \
65                                 (sc)->sc_cmd_param, (uint8_t *)(r), (rlen))
66
67 /*
68  * This will clear previous command's
69  * execution status too
70  */
71 #define CMD_WRITE_4(sc, val)                                    \
72         bus_space_write_4((sc)->sc_mem2_bt, (sc)->sc_mem2_bh,   \
73                           (sc)->sc_cmd, (val))
74 #define CMD_READ_4(sc)          \
75         bus_space_read_4((sc)->sc_mem2_bt, (sc)->sc_mem2_bh, (sc)->sc_cmd)
76
77 /*
78  * acx command register layerout:
79  * upper 16bits are command execution status
80  * lower 16bits are command to be executed
81  */
82 #define ACX_CMD_STATUS_SHIFT    16
83 #define ACX_CMD_STATUS_OK       1
84
85 struct radio_init {
86         uint32_t        radio_ofs;      /* radio firmware offset */
87         uint32_t        radio_len;      /* radio firmware length */
88 } __packed;
89
90 struct bss_join_hdr {
91         uint8_t         bssid[IEEE80211_ADDR_LEN];
92         uint16_t        beacon_intvl;
93         uint8_t         chip_spec[3];
94         uint8_t         ndata_txrate;   /* see ACX_NDATA_TXRATE_ */
95         uint8_t         ndata_txopt;    /* see ACX_NDATA_TXOPT_ */
96         uint8_t         mode;           /* see ACX_MODE_ */
97         uint8_t         channel;
98         uint8_t         esslen;
99         char            essid[1];
100 } __packed;
101
102 /*
103  * non-data frame tx rate
104  */
105 #define ACX_NDATA_TXRATE_2              20      /* 2Mbits/s */
106
107 /*
108  * non-data frame tx options
109  */
110 #define ACX_NDATA_TXOPT_PBCC            0x40
111 #define ACX_NDATA_TXOPT_OFDM            0x20
112 #define ACX_NDATA_TXOPT_SHORT_PREAMBLE  0x10
113
114 #define BSS_JOIN_BUFLEN         \
115         (sizeof(struct bss_join_hdr) + IEEE80211_NWID_LEN - 1)
116 #define BSS_JOIN_PARAM_SIZE(bj) \
117         (sizeof(struct bss_join_hdr) + (bj)->esslen)
118
119 void
120 acx_init_cmd_reg(struct acx_softc *sc)
121 {
122         sc->sc_cmd = CSR_READ_4(sc, ACXREG_CMD_REG_OFFSET);
123         sc->sc_cmd_param = sc->sc_cmd + ACX_CMD_REG_SIZE;
124
125         /* Clear command & status */
126         CMD_WRITE_4(sc, 0);
127 }
128
129 int
130 acx_join_bss(struct acx_softc *sc, uint8_t mode, struct ieee80211_node *node,
131              uint8_t chan)
132 {
133         uint8_t bj_buf[BSS_JOIN_BUFLEN];
134         struct bss_join_hdr *bj;
135         int i, dtim_intvl;
136
137         bzero(bj_buf, sizeof(bj_buf));
138         bj = (struct bss_join_hdr *)bj_buf;
139
140         for (i = 0; i < IEEE80211_ADDR_LEN; ++i)
141                 bj->bssid[i] = node->ni_bssid[IEEE80211_ADDR_LEN - i - 1];
142
143         bj->beacon_intvl = htole16(node->ni_intval);
144
145         dtim_intvl = sc->sc_ic.ic_dtim_period;
146         sc->chip_set_bss_join_param(sc, bj->chip_spec, dtim_intvl);
147
148         bj->ndata_txrate = ACX_NDATA_TXRATE_2;
149         bj->ndata_txopt = 0;
150         bj->mode = mode;
151         bj->channel = chan;
152         bj->esslen = node->ni_esslen;
153         bcopy(node->ni_essid, bj->essid, node->ni_esslen);
154
155         DPRINTF((&sc->sc_ic.ic_if, "join BSS/IBSS on channel %d\n", bj->channel));
156         return acx_exec_command(sc, ACXCMD_JOIN_BSS,
157                                 bj, BSS_JOIN_PARAM_SIZE(bj), NULL, 0);
158 }
159
160 int
161 acx_enable_txchan(struct acx_softc *sc, uint8_t chan)
162 {
163         return acx_exec_command(sc, ACXCMD_ENABLE_TXCHAN, &chan, sizeof(chan),
164                                 NULL, 0);
165 }
166
167 int
168 acx_enable_rxchan(struct acx_softc *sc, uint8_t chan)
169 {
170         return acx_exec_command(sc, ACXCMD_ENABLE_RXCHAN, &chan, sizeof(chan),
171                                 NULL, 0);
172 }
173
174 int
175 acx_get_conf(struct acx_softc *sc, uint16_t conf_id, void *conf,
176              uint16_t conf_len)
177 {
178         struct acx_conf *confcom;
179
180         if (conf_len < sizeof(*confcom)) {
181                 if_printf(&sc->sc_ic.ic_if, "%s configure data is too short\n",
182                           __func__);
183                 return 1;
184         }
185
186         confcom = conf;
187         confcom->conf_id = htole16(conf_id);
188         confcom->conf_data_len = htole16(conf_len - sizeof(*confcom));
189
190         return acx_exec_command(sc, ACXCMD_GET_CONF, confcom, sizeof(*confcom),
191                                 conf, conf_len);
192 }
193
194 int
195 acx_set_conf(struct acx_softc *sc, uint16_t conf_id, void *conf,
196              uint16_t conf_len)
197 {
198         struct acx_conf *confcom;
199
200         if (conf_len < sizeof(*confcom)) {
201                 if_printf(&sc->sc_ic.ic_if, "%s configure data is too short\n",
202                           __func__);
203                 return 1;
204         }
205
206         confcom = conf;
207         confcom->conf_id = htole16(conf_id);
208         confcom->conf_data_len = htole16(conf_len - sizeof(*confcom));
209
210         return acx_exec_command(sc, ACXCMD_SET_CONF, conf, conf_len, NULL, 0);
211 }
212
213 int
214 acx_set_tmplt(struct acx_softc *sc, uint16_t cmd, void *tmplt,
215               uint16_t tmplt_len)
216 {
217         uint16_t *size;
218
219         if (tmplt_len < sizeof(*size)) {
220                 if_printf(&sc->sc_ic.ic_if, "%s template is too short\n",
221                           __func__);
222                 return 1;
223         }
224
225         size = tmplt;
226         *size = htole16(tmplt_len - sizeof(*size));
227
228         return acx_exec_command(sc, cmd, tmplt, tmplt_len, NULL, 0);
229 }
230
231 int
232 acx_init_radio(struct acx_softc *sc, uint32_t radio_ofs, uint32_t radio_len)
233 {
234         struct radio_init r;
235
236         r.radio_ofs = htole32(radio_ofs);
237         r.radio_len = htole32(radio_len);
238         return acx_exec_command(sc, ACXCMD_INIT_RADIO, &r, sizeof(r), NULL, 0);
239 }
240
241 int
242 acx_exec_command(struct acx_softc *sc, uint16_t cmd, void *param,
243                  uint16_t param_len, void *result, uint16_t result_len)
244 {
245         uint16_t status;
246         int i, ret;
247
248         ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
249
250         if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0) {
251                 if_printf(&sc->sc_ic.ic_if, "cmd 0x%04x failed (base firmware "
252                           "not loaded)", cmd);
253                 return 1;
254         }
255
256         ret = 0;
257
258         if (param != NULL && param_len != 0) {
259                 /* Set command param */
260                 CMDPRM_WRITE_REGION_1(sc, param, param_len);
261         }
262
263         /* Set command */
264         CMD_WRITE_4(sc, cmd);
265
266         /* Exec command */
267         CSR_WRITE_2(sc, ACXREG_INTR_TRIG, ACXRV_TRIG_CMD_FINI);
268         DELAY(50);      /* XXX maybe 100 */
269
270         /* Wait for command to complete */
271         if (cmd == ACXCMD_INIT_RADIO) {
272                 /* XXX radio initialization is extremely long */
273                 tsleep(&cmd, 0, "rdinit", (150 * hz) / 1000);   /* 150ms */
274         }
275
276 #define CMDWAIT_RETRY_MAX       1000
277         for (i = 0; i < CMDWAIT_RETRY_MAX; ++i) {
278                 uint16_t reg;
279
280                 reg = CSR_READ_2(sc, ACXREG_INTR_STATUS);
281                 if (reg & ACXRV_INTR_CMD_FINI) {
282                         CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_CMD_FINI);
283                         break;
284                 }
285                 DELAY(50);
286         }
287         if (i == CMDWAIT_RETRY_MAX) {
288                 if_printf(&sc->sc_ic.ic_if, "cmd %04x failed (timeout)\n", cmd);
289                 ret = 1;
290                 goto back;
291         }
292 #undef CMDWAIT_RETRY_MAX
293
294         /* Get command exec status */
295         status = (CMD_READ_4(sc) >> ACX_CMD_STATUS_SHIFT);
296         if (status != ACX_CMD_STATUS_OK) {
297                 if_printf(&sc->sc_ic.ic_if, "cmd %04x failed\n", cmd);
298                 ret = 1;
299                 goto back;
300         }
301
302         if (result != NULL && result_len != 0) {
303                 /* Get command result */
304                 CMDPRM_READ_REGION_1(sc, result, result_len);
305         }
306
307 back:
308         CMD_WRITE_4(sc, 0);
309         return ret;
310 }