if_ipw.c revision 1.9 1 1.9 dyoung /* $NetBSD: if_ipw.c,v 1.9 2005/01/18 04:31:09 dyoung Exp $ */
2 1.1 lukem
3 1.1 lukem /*-
4 1.1 lukem * Copyright (c) 2004
5 1.1 lukem * Damien Bergamini <damien.bergamini (at) free.fr>. All rights reserved.
6 1.1 lukem *
7 1.1 lukem * Redistribution and use in source and binary forms, with or without
8 1.1 lukem * modification, are permitted provided that the following conditions
9 1.1 lukem * are met:
10 1.1 lukem * 1. Redistributions of source code must retain the above copyright
11 1.1 lukem * notice unmodified, this list of conditions, and the following
12 1.1 lukem * disclaimer.
13 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 lukem * notice, this list of conditions and the following disclaimer in the
15 1.1 lukem * documentation and/or other materials provided with the distribution.
16 1.1 lukem *
17 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 lukem * SUCH DAMAGE.
28 1.1 lukem */
29 1.1 lukem
30 1.1 lukem #include <sys/cdefs.h>
31 1.9 dyoung __KERNEL_RCSID(0, "$NetBSD: if_ipw.c,v 1.9 2005/01/18 04:31:09 dyoung Exp $");
32 1.1 lukem
33 1.1 lukem /*-
34 1.1 lukem * Intel(R) PRO/Wireless 2100 MiniPCI driver
35 1.1 lukem * http://www.intel.com/products/mobiletechnology/prowireless.htm
36 1.1 lukem */
37 1.1 lukem
38 1.1 lukem #include "bpfilter.h"
39 1.1 lukem
40 1.1 lukem #include <sys/param.h>
41 1.1 lukem #include <sys/sockio.h>
42 1.1 lukem #include <sys/sysctl.h>
43 1.1 lukem #include <sys/mbuf.h>
44 1.1 lukem #include <sys/kernel.h>
45 1.1 lukem #include <sys/socket.h>
46 1.1 lukem #include <sys/systm.h>
47 1.1 lukem #include <sys/malloc.h>
48 1.1 lukem #include <sys/conf.h>
49 1.1 lukem
50 1.1 lukem #include <machine/bus.h>
51 1.1 lukem #include <machine/endian.h>
52 1.1 lukem #include <machine/intr.h>
53 1.1 lukem
54 1.1 lukem #include <dev/pci/pcireg.h>
55 1.1 lukem #include <dev/pci/pcivar.h>
56 1.1 lukem #include <dev/pci/pcidevs.h>
57 1.1 lukem
58 1.1 lukem #if NBPFILTER > 0
59 1.1 lukem #include <net/bpf.h>
60 1.1 lukem #endif
61 1.1 lukem #include <net/if.h>
62 1.1 lukem #include <net/if_arp.h>
63 1.1 lukem #include <net/if_dl.h>
64 1.1 lukem #include <net/if_ether.h>
65 1.1 lukem #include <net/if_media.h>
66 1.1 lukem #include <net/if_types.h>
67 1.1 lukem
68 1.1 lukem #include <net80211/ieee80211_var.h>
69 1.4 lukem #include <net80211/ieee80211_radiotap.h>
70 1.1 lukem
71 1.1 lukem #include <netinet/in.h>
72 1.1 lukem #include <netinet/in_systm.h>
73 1.1 lukem #include <netinet/in_var.h>
74 1.1 lukem #include <netinet/ip.h>
75 1.1 lukem
76 1.3 lukem #include <dev/pci/if_ipwreg.h>
77 1.3 lukem #include <dev/pci/if_ipwvar.h>
78 1.1 lukem
79 1.1 lukem static int ipw_match(struct device *, struct cfdata *, void *);
80 1.1 lukem static void ipw_attach(struct device *, struct device *, void *);
81 1.1 lukem static int ipw_detach(struct device *, int);
82 1.1 lukem static int ipw_media_change(struct ifnet *);
83 1.1 lukem static int ipw_newstate(struct ieee80211com *, enum ieee80211_state, int);
84 1.1 lukem static void ipw_command_intr(struct ipw_softc *, struct ipw_soft_buf *);
85 1.1 lukem static void ipw_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
86 1.8 lukem static void ipw_data_intr(struct ipw_softc *, struct ipw_status *,
87 1.1 lukem struct ipw_soft_bd *, struct ipw_soft_buf *);
88 1.1 lukem static void ipw_notification_intr(struct ipw_softc *, struct ipw_soft_buf *);
89 1.1 lukem static void ipw_rx_intr(struct ipw_softc *);
90 1.1 lukem static void ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
91 1.1 lukem static void ipw_tx_intr(struct ipw_softc *);
92 1.1 lukem static int ipw_intr(void *);
93 1.1 lukem static int ipw_cmd(struct ipw_softc *, u_int32_t, void *, u_int32_t);
94 1.1 lukem static int ipw_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *);
95 1.1 lukem static void ipw_start(struct ifnet *);
96 1.1 lukem static void ipw_watchdog(struct ifnet *);
97 1.1 lukem static int ipw_get_table1(struct ipw_softc *, u_int32_t *);
98 1.1 lukem static int ipw_get_radio(struct ipw_softc *, int *);
99 1.1 lukem static int ipw_ioctl(struct ifnet *, u_long, caddr_t);
100 1.1 lukem static u_int32_t ipw_read_table1(struct ipw_softc *, u_int32_t);
101 1.1 lukem static void ipw_write_table1(struct ipw_softc *, u_int32_t, u_int32_t);
102 1.1 lukem static int ipw_read_table2(struct ipw_softc *, u_int32_t, void *, u_int32_t *);
103 1.1 lukem static int ipw_tx_init(struct ipw_softc *);
104 1.1 lukem static void ipw_tx_stop(struct ipw_softc *);
105 1.1 lukem static int ipw_rx_init(struct ipw_softc *);
106 1.1 lukem static void ipw_rx_stop(struct ipw_softc *);
107 1.1 lukem static void ipw_reset(struct ipw_softc *);
108 1.1 lukem static int ipw_clock_sync(struct ipw_softc *);
109 1.1 lukem static int ipw_load_ucode(struct ipw_softc *, u_char *, int);
110 1.1 lukem static int ipw_load_firmware(struct ipw_softc *, u_char *, int);
111 1.1 lukem static int ipw_firmware_init(struct ipw_softc *, u_char *);
112 1.1 lukem static int ipw_config(struct ipw_softc *);
113 1.1 lukem static int ipw_init(struct ifnet *);
114 1.1 lukem static void ipw_stop(struct ifnet *, int);
115 1.8 lukem static void ipw_read_mem_1(struct ipw_softc *, bus_size_t, u_int8_t *,
116 1.1 lukem bus_size_t);
117 1.8 lukem static void ipw_write_mem_1(struct ipw_softc *, bus_size_t, u_int8_t *,
118 1.1 lukem bus_size_t);
119 1.1 lukem static void ipw_zero_mem_4(struct ipw_softc *, bus_size_t, bus_size_t);
120 1.1 lukem
121 1.1 lukem static inline u_int8_t MEM_READ_1(struct ipw_softc *sc, u_int32_t addr)
122 1.1 lukem {
123 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
124 1.1 lukem return CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA);
125 1.1 lukem }
126 1.1 lukem
127 1.1 lukem static inline u_int16_t MEM_READ_2(struct ipw_softc *sc, u_int32_t addr)
128 1.1 lukem {
129 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
130 1.1 lukem return CSR_READ_2(sc, IPW_CSR_INDIRECT_DATA);
131 1.1 lukem }
132 1.1 lukem
133 1.1 lukem static inline u_int32_t MEM_READ_4(struct ipw_softc *sc, u_int32_t addr)
134 1.1 lukem {
135 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
136 1.1 lukem return CSR_READ_4(sc, IPW_CSR_INDIRECT_DATA);
137 1.1 lukem }
138 1.1 lukem
139 1.1 lukem #ifdef IPW_DEBUG
140 1.1 lukem #define DPRINTF(x) if (ipw_debug > 0) printf x
141 1.1 lukem #define DPRINTFN(n, x) if (ipw_debug >= (n)) printf x
142 1.1 lukem int ipw_debug = 0;
143 1.1 lukem #else
144 1.1 lukem #define DPRINTF(x)
145 1.1 lukem #define DPRINTFN(n, x)
146 1.1 lukem #endif
147 1.1 lukem
148 1.8 lukem CFATTACH_DECL(ipw, sizeof (struct ipw_softc), ipw_match, ipw_attach,
149 1.1 lukem ipw_detach, NULL);
150 1.1 lukem
151 1.1 lukem static int
152 1.1 lukem ipw_match(struct device *parent, struct cfdata *match, void *aux)
153 1.1 lukem {
154 1.1 lukem struct pci_attach_args *pa = aux;
155 1.1 lukem
156 1.8 lukem if (PCI_VENDOR (pa->pa_id) == PCI_VENDOR_INTEL &&
157 1.1 lukem PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2100)
158 1.1 lukem return 1;
159 1.1 lukem
160 1.1 lukem return 0;
161 1.1 lukem }
162 1.1 lukem
163 1.1 lukem /* Base Address Register */
164 1.1 lukem #define IPW_PCI_BAR0 0x10
165 1.1 lukem
166 1.1 lukem static void
167 1.1 lukem ipw_attach(struct device *parent, struct device *self, void *aux)
168 1.1 lukem {
169 1.1 lukem struct ipw_softc *sc = (struct ipw_softc *)self;
170 1.1 lukem struct ieee80211com *ic = &sc->sc_ic;
171 1.1 lukem struct ifnet *ifp = &ic->ic_if;
172 1.1 lukem struct ieee80211_rateset *rs;
173 1.1 lukem struct pci_attach_args *pa = aux;
174 1.1 lukem const char *intrstr;
175 1.1 lukem char devinfo[256];
176 1.1 lukem bus_space_tag_t memt;
177 1.1 lukem bus_space_handle_t memh;
178 1.1 lukem bus_addr_t base;
179 1.1 lukem pci_intr_handle_t ih;
180 1.1 lukem u_int32_t data;
181 1.1 lukem int i, revision, error;
182 1.1 lukem
183 1.1 lukem sc->sc_pct = pa->pa_pc;
184 1.1 lukem
185 1.1 lukem pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
186 1.1 lukem revision = PCI_REVISION(pa->pa_class);
187 1.1 lukem aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
188 1.1 lukem
189 1.1 lukem /* enable bus-mastering */
190 1.1 lukem data = pci_conf_read(sc->sc_pct, pa->pa_tag, PCI_COMMAND_STATUS_REG);
191 1.1 lukem data |= PCI_COMMAND_MASTER_ENABLE;
192 1.1 lukem pci_conf_write(sc->sc_pct, pa->pa_tag, PCI_COMMAND_STATUS_REG, data);
193 1.1 lukem
194 1.1 lukem /* map the register window */
195 1.8 lukem error = pci_mapreg_map(pa, IPW_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
196 1.1 lukem PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz);
197 1.1 lukem if (error != 0) {
198 1.8 lukem aprint_error("%s: could not map memory space\n",
199 1.1 lukem sc->sc_dev.dv_xname);
200 1.1 lukem return;
201 1.1 lukem }
202 1.1 lukem
203 1.1 lukem sc->sc_st = memt;
204 1.1 lukem sc->sc_sh = memh;
205 1.1 lukem sc->sc_dmat = pa->pa_dmat;
206 1.1 lukem
207 1.1 lukem /* disable interrupts */
208 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
209 1.1 lukem
210 1.1 lukem if (pci_intr_map(pa, &ih) != 0) {
211 1.8 lukem aprint_error("%s: could not map interrupt\n",
212 1.1 lukem sc->sc_dev.dv_xname);
213 1.1 lukem return;
214 1.1 lukem }
215 1.1 lukem
216 1.1 lukem intrstr = pci_intr_string(sc->sc_pct, ih);
217 1.1 lukem sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, ipw_intr, sc);
218 1.1 lukem if (sc->sc_ih == NULL) {
219 1.8 lukem aprint_error("%s: could not establish interrupt",
220 1.1 lukem sc->sc_dev.dv_xname);
221 1.1 lukem if (intrstr != NULL)
222 1.1 lukem aprint_error(" at %s", intrstr);
223 1.1 lukem aprint_error("\n");
224 1.1 lukem return;
225 1.1 lukem }
226 1.1 lukem aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
227 1.1 lukem
228 1.1 lukem ic->ic_phytype = IEEE80211_T_DS;
229 1.1 lukem ic->ic_opmode = IEEE80211_M_STA;
230 1.1 lukem ic->ic_state = IEEE80211_S_INIT;
231 1.1 lukem
232 1.1 lukem /* set device capabilities */
233 1.8 lukem ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_MONITOR |
234 1.1 lukem IEEE80211_C_PMGT | IEEE80211_C_TXPMGT | IEEE80211_C_WEP;
235 1.1 lukem
236 1.1 lukem /* set supported 11.b rates */
237 1.1 lukem rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
238 1.1 lukem rs->rs_nrates = 4;
239 1.1 lukem rs->rs_rates[0] = 2; /* 1Mbps */
240 1.1 lukem rs->rs_rates[1] = 4; /* 2Mbps */
241 1.1 lukem rs->rs_rates[2] = 11; /* 5.5Mbps */
242 1.8 lukem rs->rs_rates[3] = 22; /* 11Mbps */
243 1.1 lukem
244 1.1 lukem /* set supported 11.b channels (1 through 14) */
245 1.1 lukem for (i = 1; i <= 14; i++) {
246 1.8 lukem ic->ic_channels[i].ic_freq =
247 1.1 lukem ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
248 1.1 lukem ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
249 1.1 lukem }
250 1.1 lukem
251 1.1 lukem ic->ic_ibss_chan = &ic->ic_channels[0];
252 1.1 lukem
253 1.1 lukem ifp->if_softc = sc;
254 1.1 lukem ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
255 1.1 lukem ifp->if_init = ipw_init;
256 1.1 lukem ifp->if_stop = ipw_stop;
257 1.1 lukem ifp->if_ioctl = ipw_ioctl;
258 1.1 lukem ifp->if_start = ipw_start;
259 1.1 lukem ifp->if_watchdog = ipw_watchdog;
260 1.1 lukem IFQ_SET_READY(&ifp->if_snd);
261 1.1 lukem bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
262 1.1 lukem
263 1.1 lukem if_attach(ifp);
264 1.1 lukem ieee80211_ifattach(ifp);
265 1.1 lukem /* override state transition machine */
266 1.1 lukem sc->sc_newstate = ic->ic_newstate;
267 1.1 lukem ic->ic_newstate = ipw_newstate;
268 1.1 lukem
269 1.1 lukem ieee80211_media_init(ifp, ipw_media_change, ieee80211_media_status);
270 1.4 lukem
271 1.4 lukem #if NBPFILTER > 0
272 1.8 lukem bpfattach2(ifp, DLT_IEEE802_11_RADIO,
273 1.4 lukem sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
274 1.4 lukem
275 1.4 lukem sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
276 1.4 lukem sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
277 1.4 lukem sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
278 1.4 lukem
279 1.4 lukem sc->sc_txtap_len = sizeof sc->sc_txtapu;
280 1.4 lukem sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
281 1.4 lukem sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
282 1.4 lukem #endif
283 1.1 lukem }
284 1.1 lukem
285 1.1 lukem static int
286 1.1 lukem ipw_detach(struct device* self, int flags)
287 1.1 lukem {
288 1.1 lukem struct ipw_softc *sc = (struct ipw_softc *)self;
289 1.1 lukem struct ifnet *ifp = &sc->sc_ic.ic_if;
290 1.1 lukem
291 1.1 lukem ipw_reset(sc);
292 1.1 lukem
293 1.4 lukem #if NBPFILTER > 0
294 1.4 lukem bpfdetach(ifp);
295 1.4 lukem #endif
296 1.1 lukem ieee80211_ifdetach(ifp);
297 1.1 lukem if_detach(ifp);
298 1.1 lukem
299 1.1 lukem if (sc->sc_ih != NULL) {
300 1.1 lukem pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
301 1.1 lukem sc->sc_ih = NULL;
302 1.1 lukem }
303 1.1 lukem
304 1.1 lukem bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
305 1.1 lukem
306 1.1 lukem return 0;
307 1.1 lukem }
308 1.1 lukem
309 1.1 lukem static int
310 1.1 lukem ipw_media_change(struct ifnet *ifp)
311 1.1 lukem {
312 1.1 lukem int error;
313 1.1 lukem
314 1.1 lukem error = ieee80211_media_change(ifp);
315 1.1 lukem if (error != ENETRESET)
316 1.1 lukem return error;
317 1.1 lukem
318 1.1 lukem if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
319 1.1 lukem ipw_init(ifp);
320 1.1 lukem
321 1.1 lukem return 0;
322 1.1 lukem }
323 1.1 lukem
324 1.1 lukem static int
325 1.1 lukem ipw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
326 1.1 lukem {
327 1.1 lukem struct ifnet *ifp = &ic->ic_if;
328 1.1 lukem struct ipw_softc *sc = ifp->if_softc;
329 1.1 lukem struct ieee80211_node *ni = ic->ic_bss;
330 1.1 lukem u_int32_t val, len;
331 1.1 lukem
332 1.1 lukem switch (nstate) {
333 1.1 lukem case IEEE80211_S_INIT:
334 1.1 lukem break;
335 1.1 lukem
336 1.1 lukem case IEEE80211_S_RUN:
337 1.1 lukem len = IEEE80211_NWID_LEN;
338 1.1 lukem ipw_read_table2(sc, IPW_INFO_CURRENT_SSID, ni->ni_essid, &len);
339 1.1 lukem ni->ni_esslen = len;
340 1.1 lukem
341 1.1 lukem val = ipw_read_table1(sc, IPW_INFO_CURRENT_CHANNEL);
342 1.1 lukem ni->ni_chan = &ic->ic_channels[val];
343 1.1 lukem
344 1.1 lukem DELAY(100); /* firmware needs a short delay here */
345 1.1 lukem
346 1.1 lukem len = IEEE80211_ADDR_LEN;
347 1.1 lukem ipw_read_table2(sc, IPW_INFO_CURRENT_BSSID, ni->ni_bssid, &len);
348 1.1 lukem break;
349 1.1 lukem
350 1.1 lukem case IEEE80211_S_SCAN:
351 1.1 lukem case IEEE80211_S_AUTH:
352 1.1 lukem case IEEE80211_S_ASSOC:
353 1.1 lukem break;
354 1.1 lukem }
355 1.1 lukem
356 1.1 lukem ic->ic_state = nstate;
357 1.1 lukem return 0;
358 1.1 lukem }
359 1.1 lukem
360 1.1 lukem static void
361 1.1 lukem ipw_command_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
362 1.1 lukem {
363 1.1 lukem struct ipw_cmd *cmd;
364 1.1 lukem
365 1.1 lukem bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof (struct ipw_cmd),
366 1.1 lukem BUS_DMASYNC_POSTREAD);
367 1.1 lukem
368 1.1 lukem cmd = mtod(sbuf->m, struct ipw_cmd *);
369 1.1 lukem
370 1.8 lukem DPRINTFN(2, ("RX!CMD!%u!%u!%u!%u!%u\n",
371 1.1 lukem le32toh(cmd->type), le32toh(cmd->subtype), le32toh(cmd->seq),
372 1.1 lukem le32toh(cmd->len), le32toh(cmd->status)));
373 1.1 lukem
374 1.8 lukem /*
375 1.8 lukem * Wake up processes waiting for command ack. In the case of the
376 1.1 lukem * IPW_CMD_DISABLE command, wake up the process only when the adapter
377 1.1 lukem * enters the IPW_STATE_DISABLED state. This is notified in
378 1.1 lukem * ipw_newstate_intr().
379 1.1 lukem */
380 1.1 lukem if (le32toh(cmd->type) != IPW_CMD_DISABLE)
381 1.1 lukem wakeup(sc->cmd);
382 1.1 lukem }
383 1.1 lukem
384 1.1 lukem static void
385 1.1 lukem ipw_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
386 1.1 lukem {
387 1.1 lukem struct ieee80211com *ic = &sc->sc_ic;
388 1.1 lukem u_int32_t state;
389 1.1 lukem
390 1.1 lukem bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof state,
391 1.1 lukem BUS_DMASYNC_POSTREAD);
392 1.1 lukem
393 1.1 lukem state = le32toh(*mtod(sbuf->m, u_int32_t *));
394 1.1 lukem
395 1.1 lukem DPRINTFN(2, ("RX!NEWSTATE!%u\n", state));
396 1.1 lukem
397 1.1 lukem switch (state) {
398 1.1 lukem case IPW_STATE_ASSOCIATED:
399 1.1 lukem ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
400 1.1 lukem break;
401 1.1 lukem
402 1.1 lukem case IPW_STATE_SCANNING:
403 1.1 lukem ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
404 1.1 lukem break;
405 1.1 lukem
406 1.1 lukem case IPW_STATE_ASSOCIATION_LOST:
407 1.1 lukem ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
408 1.1 lukem break;
409 1.1 lukem
410 1.1 lukem case IPW_STATE_DISABLED:
411 1.1 lukem wakeup(sc->cmd);
412 1.1 lukem break;
413 1.1 lukem
414 1.1 lukem case IPW_STATE_RADIO_DISABLED:
415 1.1 lukem /* XXX should turn the interface down */
416 1.1 lukem break;
417 1.1 lukem }
418 1.1 lukem }
419 1.1 lukem
420 1.1 lukem static void
421 1.8 lukem ipw_data_intr(struct ipw_softc *sc, struct ipw_status *status,
422 1.1 lukem struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
423 1.1 lukem {
424 1.1 lukem struct ieee80211com *ic = &sc->sc_ic;
425 1.1 lukem struct ifnet *ifp = &ic->ic_if;
426 1.1 lukem struct mbuf *m;
427 1.1 lukem struct ieee80211_frame *wh;
428 1.1 lukem struct ieee80211_node *ni;
429 1.1 lukem int error;
430 1.1 lukem
431 1.1 lukem DPRINTFN(5, ("RX!DATA!%u!%u\n", le32toh(status->len), status->rssi));
432 1.1 lukem
433 1.1 lukem bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, le32toh(status->len),
434 1.1 lukem BUS_DMASYNC_POSTREAD);
435 1.1 lukem
436 1.1 lukem bus_dmamap_unload(sc->sc_dmat, sbuf->map);
437 1.1 lukem
438 1.1 lukem /* Finalize mbuf */
439 1.1 lukem m = sbuf->m;
440 1.1 lukem m->m_pkthdr.rcvif = ifp;
441 1.1 lukem m->m_pkthdr.len = m->m_len = le32toh(status->len);
442 1.1 lukem
443 1.4 lukem #if NBPFILTER > 0
444 1.4 lukem if (sc->sc_drvbpf != NULL) {
445 1.4 lukem struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
446 1.4 lukem
447 1.4 lukem tap->wr_flags = 0;
448 1.4 lukem tap->wr_antsignal = status->rssi;
449 1.4 lukem tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
450 1.4 lukem tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
451 1.4 lukem
452 1.4 lukem bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
453 1.4 lukem }
454 1.4 lukem #endif
455 1.4 lukem
456 1.1 lukem wh = mtod(m, struct ieee80211_frame *);
457 1.1 lukem
458 1.7 lukem ni = ieee80211_find_rxnode(ic, wh);
459 1.1 lukem
460 1.1 lukem /* Send it up to the upper layer */
461 1.1 lukem ieee80211_input(ifp, m, ni, status->rssi, 0/*rstamp*/);
462 1.1 lukem
463 1.1 lukem ieee80211_release_node(ic, ni);
464 1.1 lukem
465 1.1 lukem MGETHDR(m, M_DONTWAIT, MT_DATA);
466 1.1 lukem if (m == NULL) {
467 1.8 lukem aprint_error("%s: could not allocate rx mbuf\n",
468 1.1 lukem sc->sc_dev.dv_xname);
469 1.1 lukem return;
470 1.1 lukem }
471 1.1 lukem MCLGET(m, M_DONTWAIT);
472 1.1 lukem if (!(m->m_flags & M_EXT)) {
473 1.1 lukem m_freem(m);
474 1.8 lukem aprint_error("%s: could not allocate rx mbuf cluster\n",
475 1.1 lukem sc->sc_dev.dv_xname);
476 1.1 lukem return;
477 1.1 lukem }
478 1.1 lukem
479 1.8 lukem error = bus_dmamap_load(sc->sc_dmat, sbuf->map, mtod(m, void *),
480 1.1 lukem MCLBYTES, NULL, BUS_DMA_NOWAIT);
481 1.1 lukem if (error != 0) {
482 1.8 lukem aprint_error("%s: could not map rxbuf dma memory\n",
483 1.1 lukem sc->sc_dev.dv_xname);
484 1.1 lukem m_freem(m);
485 1.1 lukem return;
486 1.1 lukem }
487 1.1 lukem
488 1.1 lukem sbuf->m = m;
489 1.1 lukem sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
490 1.1 lukem }
491 1.1 lukem
492 1.1 lukem static void
493 1.1 lukem ipw_notification_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
494 1.1 lukem {
495 1.1 lukem DPRINTFN(2, ("RX!NOTIFICATION\n"));
496 1.1 lukem }
497 1.1 lukem
498 1.1 lukem static void
499 1.1 lukem ipw_rx_intr(struct ipw_softc *sc)
500 1.1 lukem {
501 1.1 lukem struct ipw_status *status;
502 1.1 lukem struct ipw_soft_bd *sbd;
503 1.1 lukem struct ipw_soft_buf *sbuf;
504 1.1 lukem u_int32_t r, i;
505 1.1 lukem
506 1.1 lukem r = CSR_READ_4(sc, IPW_CSR_RX_READ_INDEX);
507 1.1 lukem
508 1.1 lukem for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
509 1.1 lukem
510 1.8 lukem bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
511 1.8 lukem i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
512 1.1 lukem BUS_DMASYNC_POSTREAD);
513 1.1 lukem
514 1.8 lukem bus_dmamap_sync(sc->sc_dmat, sc->status_map,
515 1.8 lukem i * sizeof (struct ipw_status), sizeof (struct ipw_status),
516 1.1 lukem BUS_DMASYNC_POSTREAD);
517 1.1 lukem
518 1.1 lukem status = &sc->status_list[i];
519 1.1 lukem sbd = &sc->srbd_list[i];
520 1.1 lukem sbuf = sbd->priv;
521 1.1 lukem
522 1.1 lukem switch (le16toh(status->code) & 0xf) {
523 1.1 lukem case IPW_STATUS_CODE_COMMAND:
524 1.1 lukem ipw_command_intr(sc, sbuf);
525 1.1 lukem break;
526 1.1 lukem
527 1.1 lukem case IPW_STATUS_CODE_NEWSTATE:
528 1.1 lukem ipw_newstate_intr(sc, sbuf);
529 1.1 lukem break;
530 1.1 lukem
531 1.1 lukem case IPW_STATUS_CODE_DATA_802_3:
532 1.1 lukem case IPW_STATUS_CODE_DATA_802_11:
533 1.1 lukem ipw_data_intr(sc, status, sbd, sbuf);
534 1.1 lukem break;
535 1.1 lukem
536 1.1 lukem case IPW_STATUS_CODE_NOTIFICATION:
537 1.1 lukem ipw_notification_intr(sc, sbuf);
538 1.1 lukem break;
539 1.1 lukem
540 1.1 lukem default:
541 1.8 lukem aprint_debug("%s: unknown status code %u\n",
542 1.1 lukem sc->sc_dev.dv_xname, le16toh(status->code));
543 1.1 lukem }
544 1.1 lukem sbd->bd->flags = 0;
545 1.1 lukem
546 1.8 lukem bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
547 1.8 lukem i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
548 1.1 lukem BUS_DMASYNC_PREWRITE);
549 1.1 lukem }
550 1.1 lukem
551 1.1 lukem /* Tell the firmware what we have processed */
552 1.1 lukem sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
553 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_RX_WRITE_INDEX, sc->rxcur);
554 1.1 lukem }
555 1.1 lukem
556 1.1 lukem static void
557 1.1 lukem ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
558 1.1 lukem {
559 1.1 lukem struct ieee80211com *ic;
560 1.1 lukem struct ipw_soft_hdr *shdr;
561 1.1 lukem struct ipw_soft_buf *sbuf;
562 1.1 lukem
563 1.1 lukem switch (sbd->type) {
564 1.1 lukem case IPW_SBD_TYPE_COMMAND:
565 1.1 lukem bus_dmamap_unload(sc->sc_dmat, sc->cmd_map);
566 1.1 lukem break;
567 1.1 lukem
568 1.1 lukem case IPW_SBD_TYPE_HEADER:
569 1.1 lukem shdr = sbd->priv;
570 1.1 lukem bus_dmamap_unload(sc->sc_dmat, shdr->map);
571 1.1 lukem TAILQ_INSERT_TAIL(&sc->sc_free_shdr, shdr, next);
572 1.1 lukem break;
573 1.1 lukem
574 1.1 lukem case IPW_SBD_TYPE_DATA:
575 1.1 lukem ic = &sc->sc_ic;
576 1.1 lukem sbuf = sbd->priv;
577 1.1 lukem bus_dmamap_unload(sc->sc_dmat, sbuf->map);
578 1.1 lukem m_freem(sbuf->m);
579 1.1 lukem if (sbuf->ni != NULL)
580 1.1 lukem ieee80211_release_node(ic, sbuf->ni);
581 1.1 lukem /* kill watchdog timer */
582 1.1 lukem sc->sc_tx_timer = 0;
583 1.1 lukem TAILQ_INSERT_TAIL(&sc->sc_free_sbuf, sbuf, next);
584 1.1 lukem break;
585 1.1 lukem }
586 1.1 lukem sbd->type = IPW_SBD_TYPE_NOASSOC;
587 1.1 lukem }
588 1.1 lukem
589 1.1 lukem static void
590 1.1 lukem ipw_tx_intr(struct ipw_softc *sc)
591 1.1 lukem {
592 1.1 lukem struct ifnet *ifp = &sc->sc_ic.ic_if;
593 1.1 lukem u_int32_t r, i;
594 1.1 lukem
595 1.1 lukem r = CSR_READ_4(sc, IPW_CSR_TX_READ_INDEX);
596 1.1 lukem
597 1.1 lukem for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD)
598 1.1 lukem ipw_release_sbd(sc, &sc->stbd_list[i]);
599 1.1 lukem
600 1.1 lukem /* Remember what the firmware has processed */
601 1.1 lukem sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
602 1.1 lukem
603 1.1 lukem /* Call start() since some buffer descriptors have been released */
604 1.1 lukem ifp->if_flags &= ~IFF_OACTIVE;
605 1.1 lukem (*ifp->if_start)(ifp);
606 1.1 lukem }
607 1.1 lukem
608 1.1 lukem static int
609 1.1 lukem ipw_intr(void *arg)
610 1.1 lukem {
611 1.1 lukem struct ipw_softc *sc = arg;
612 1.1 lukem u_int32_t r;
613 1.1 lukem
614 1.1 lukem if ((r = CSR_READ_4(sc, IPW_CSR_INTR)) == 0)
615 1.1 lukem return 0;
616 1.1 lukem
617 1.1 lukem /* Disable interrupts */
618 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
619 1.1 lukem
620 1.1 lukem DPRINTFN(8, ("INTR!0x%08x\n", r));
621 1.1 lukem
622 1.1 lukem if (r & IPW_INTR_RX_TRANSFER)
623 1.1 lukem ipw_rx_intr(sc);
624 1.1 lukem
625 1.1 lukem if (r & IPW_INTR_TX_TRANSFER)
626 1.1 lukem ipw_tx_intr(sc);
627 1.1 lukem
628 1.1 lukem if (r & IPW_INTR_FW_INIT_DONE) {
629 1.1 lukem if (!(r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)))
630 1.1 lukem wakeup(sc);
631 1.1 lukem }
632 1.1 lukem
633 1.1 lukem /* Acknowledge interrupts */
634 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INTR, r);
635 1.1 lukem
636 1.1 lukem /* Re-enable interrupts */
637 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
638 1.1 lukem
639 1.1 lukem return 0;
640 1.1 lukem }
641 1.1 lukem
642 1.1 lukem static int
643 1.1 lukem ipw_cmd(struct ipw_softc *sc, u_int32_t type, void *data, u_int32_t len)
644 1.1 lukem {
645 1.1 lukem struct ipw_soft_bd *sbd;
646 1.1 lukem int error;
647 1.1 lukem
648 1.1 lukem sbd = &sc->stbd_list[sc->txcur];
649 1.1 lukem
650 1.8 lukem error = bus_dmamap_load(sc->sc_dmat, sc->cmd_map, sc->cmd,
651 1.1 lukem sizeof (struct ipw_cmd), NULL, BUS_DMA_NOWAIT);
652 1.1 lukem if (error != 0) {
653 1.8 lukem aprint_error("%s: could not map cmd dma memory\n",
654 1.1 lukem sc->sc_dev.dv_xname);
655 1.1 lukem return error;
656 1.1 lukem }
657 1.1 lukem
658 1.1 lukem sc->cmd->type = htole32(type);
659 1.1 lukem sc->cmd->subtype = htole32(0);
660 1.1 lukem sc->cmd->len = htole32(len);
661 1.1 lukem sc->cmd->seq = htole32(0);
662 1.1 lukem if (data != NULL)
663 1.1 lukem bcopy(data, sc->cmd->data, len);
664 1.1 lukem
665 1.1 lukem sbd->type = IPW_SBD_TYPE_COMMAND;
666 1.1 lukem sbd->bd->physaddr = htole32(sc->cmd_map->dm_segs[0].ds_addr);
667 1.1 lukem sbd->bd->len = htole32(sizeof (struct ipw_cmd));
668 1.1 lukem sbd->bd->nfrag = 1;
669 1.8 lukem sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
670 1.1 lukem IPW_BD_FLAG_TX_LAST_FRAGMENT;
671 1.1 lukem
672 1.1 lukem bus_dmamap_sync(sc->sc_dmat, sc->cmd_map, 0, sizeof (struct ipw_cmd),
673 1.1 lukem BUS_DMASYNC_PREWRITE);
674 1.8 lukem
675 1.8 lukem bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
676 1.1 lukem sc->txcur * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
677 1.1 lukem BUS_DMASYNC_PREWRITE);
678 1.1 lukem
679 1.1 lukem sc->txcur = (sc->txcur + 1) % IPW_NTBD;
680 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
681 1.1 lukem
682 1.1 lukem DPRINTFN(2, ("TX!CMD!%u!%u!%u!%u\n", type, 0, 0, len));
683 1.1 lukem
684 1.1 lukem /* Wait at most two seconds for command to complete */
685 1.1 lukem return tsleep(sc->cmd, 0, "ipwcmd", 2 * hz);
686 1.1 lukem }
687 1.1 lukem
688 1.1 lukem static int
689 1.1 lukem ipw_tx_start(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node *ni)
690 1.1 lukem {
691 1.1 lukem struct ipw_softc *sc = ifp->if_softc;
692 1.1 lukem struct ieee80211com *ic = &sc->sc_ic;
693 1.1 lukem struct ieee80211_frame *wh;
694 1.1 lukem struct ipw_soft_bd *sbd;
695 1.1 lukem struct ipw_soft_hdr *shdr;
696 1.1 lukem struct ipw_soft_buf *sbuf;
697 1.1 lukem int error, i;
698 1.1 lukem
699 1.1 lukem if (ic->ic_flags & IEEE80211_F_PRIVACY) {
700 1.1 lukem m = ieee80211_wep_crypt(ifp, m, 1);
701 1.1 lukem if (m == NULL)
702 1.1 lukem return ENOBUFS;
703 1.1 lukem }
704 1.1 lukem
705 1.4 lukem #if NBPFILTER > 0
706 1.4 lukem if (sc->sc_drvbpf != NULL) {
707 1.4 lukem struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
708 1.4 lukem
709 1.4 lukem tap->wt_flags = 0;
710 1.4 lukem tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
711 1.4 lukem tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
712 1.4 lukem
713 1.4 lukem bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
714 1.4 lukem }
715 1.4 lukem #endif
716 1.4 lukem
717 1.1 lukem wh = mtod(m, struct ieee80211_frame *);
718 1.1 lukem
719 1.1 lukem shdr = TAILQ_FIRST(&sc->sc_free_shdr);
720 1.1 lukem sbuf = TAILQ_FIRST(&sc->sc_free_sbuf);
721 1.1 lukem
722 1.1 lukem shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
723 1.1 lukem shdr->hdr.subtype = htole32(0);
724 1.1 lukem shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
725 1.1 lukem shdr->hdr.encrypt = 0;
726 1.1 lukem shdr->hdr.keyidx = 0;
727 1.1 lukem shdr->hdr.keysz = 0;
728 1.1 lukem shdr->hdr.fragmentsz = htole16(0);
729 1.1 lukem IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
730 1.1 lukem if (ic->ic_opmode == IEEE80211_M_STA)
731 1.1 lukem IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
732 1.1 lukem else
733 1.1 lukem IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
734 1.1 lukem
735 1.1 lukem /* trim IEEE802.11 header */
736 1.1 lukem m_adj(m, sizeof (struct ieee80211_frame));
737 1.1 lukem
738 1.1 lukem /*
739 1.8 lukem * We need to map the mbuf first to know how many buffer descriptors
740 1.1 lukem * are needed for this transfer.
741 1.1 lukem */
742 1.1 lukem error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, m, BUS_DMA_NOWAIT);
743 1.1 lukem if (error != 0) {
744 1.8 lukem aprint_error("%s: could not map mbuf (error %d)\n",
745 1.1 lukem sc->sc_dev.dv_xname, error);
746 1.1 lukem m_freem(m);
747 1.1 lukem return error;
748 1.1 lukem }
749 1.1 lukem
750 1.1 lukem error = bus_dmamap_load(sc->sc_dmat, shdr->map, &shdr->hdr,
751 1.1 lukem sizeof (struct ipw_hdr), NULL, BUS_DMA_NOWAIT);
752 1.1 lukem if (error != 0) {
753 1.8 lukem aprint_error("%s: could not map hdr (error %d)\n",
754 1.1 lukem sc->sc_dev.dv_xname, error);
755 1.1 lukem bus_dmamap_unload(sc->sc_dmat, sbuf->map);
756 1.1 lukem m_freem(m);
757 1.1 lukem return error;
758 1.1 lukem }
759 1.1 lukem
760 1.1 lukem TAILQ_REMOVE(&sc->sc_free_sbuf, sbuf, next);
761 1.1 lukem TAILQ_REMOVE(&sc->sc_free_shdr, shdr, next);
762 1.1 lukem
763 1.1 lukem sbd = &sc->stbd_list[sc->txcur];
764 1.1 lukem sbd->type = IPW_SBD_TYPE_HEADER;
765 1.1 lukem sbd->priv = shdr;
766 1.1 lukem sbd->bd->physaddr = htole32(shdr->map->dm_segs[0].ds_addr);
767 1.1 lukem sbd->bd->len = htole32(sizeof (struct ipw_hdr));
768 1.1 lukem sbd->bd->nfrag = 1 + sbuf->map->dm_nsegs;
769 1.1 lukem sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
770 1.1 lukem IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
771 1.1 lukem
772 1.8 lukem DPRINTFN(5, ("TX!HDR!%u!%u!%u!%u\n", shdr->hdr.type, shdr->hdr.subtype,
773 1.8 lukem shdr->hdr.encrypted, shdr->hdr.encrypt));
774 1.8 lukem DPRINTFN(5, ("!%s", ether_sprintf(shdr->hdr.src_addr)));
775 1.1 lukem DPRINTFN(5, ("!%s\n", ether_sprintf(shdr->hdr.dst_addr)));
776 1.6 lukem
777 1.8 lukem bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
778 1.8 lukem sc->txcur * sizeof (struct ipw_bd),
779 1.6 lukem sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
780 1.6 lukem
781 1.1 lukem sc->txcur = (sc->txcur + 1) % IPW_NTBD;
782 1.1 lukem
783 1.1 lukem sbuf->m = m;
784 1.1 lukem sbuf->ni = ni;
785 1.1 lukem
786 1.1 lukem for (i = 0; i < sbuf->map->dm_nsegs; i++) {
787 1.1 lukem sbd = &sc->stbd_list[sc->txcur];
788 1.1 lukem sbd->bd->physaddr = htole32(sbuf->map->dm_segs[i].ds_addr);
789 1.1 lukem sbd->bd->len = htole32(sbuf->map->dm_segs[i].ds_len);
790 1.1 lukem sbd->bd->nfrag = 0; /* used only in first bd */
791 1.1 lukem sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
792 1.1 lukem if (i == sbuf->map->dm_nsegs - 1) {
793 1.1 lukem sbd->type = IPW_SBD_TYPE_DATA;
794 1.1 lukem sbd->priv = sbuf;
795 1.1 lukem sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
796 1.1 lukem } else {
797 1.1 lukem sbd->type = IPW_SBD_TYPE_NOASSOC;
798 1.1 lukem sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
799 1.1 lukem }
800 1.1 lukem
801 1.8 lukem DPRINTFN(5, ("TX!FRAG!%d!%ld\n", i,
802 1.1 lukem sbuf->map->dm_segs[i].ds_len));
803 1.1 lukem
804 1.8 lukem bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
805 1.8 lukem sc->txcur * sizeof (struct ipw_bd),
806 1.1 lukem sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
807 1.1 lukem
808 1.1 lukem sc->txcur = (sc->txcur + 1) % IPW_NTBD;
809 1.1 lukem }
810 1.1 lukem
811 1.8 lukem bus_dmamap_sync(sc->sc_dmat, shdr->map, 0, sizeof (struct ipw_hdr),
812 1.1 lukem BUS_DMASYNC_PREWRITE);
813 1.1 lukem
814 1.8 lukem bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, MCLBYTES,
815 1.1 lukem BUS_DMASYNC_PREWRITE);
816 1.1 lukem
817 1.1 lukem /* Inform firmware about this new packet */
818 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
819 1.1 lukem
820 1.1 lukem return 0;
821 1.1 lukem }
822 1.1 lukem
823 1.1 lukem static void
824 1.1 lukem ipw_start(struct ifnet *ifp)
825 1.1 lukem {
826 1.1 lukem struct ipw_softc *sc = ifp->if_softc;
827 1.1 lukem struct ieee80211com *ic = &sc->sc_ic;
828 1.1 lukem struct mbuf *m;
829 1.1 lukem struct ieee80211_node *ni;
830 1.1 lukem
831 1.1 lukem for (;;) {
832 1.9 dyoung IFQ_DEQUEUE(&ifp->if_snd, m);
833 1.1 lukem if (m == NULL)
834 1.1 lukem break;
835 1.1 lukem
836 1.1 lukem #if NBPFILTER > 0
837 1.1 lukem if (ifp->if_bpf != NULL)
838 1.1 lukem bpf_mtap(ifp->if_bpf, m);
839 1.1 lukem #endif
840 1.1 lukem
841 1.1 lukem m = ieee80211_encap(ifp, m, &ni);
842 1.1 lukem if (m == NULL)
843 1.1 lukem continue;
844 1.1 lukem
845 1.1 lukem #if NBPFILTER > 0
846 1.1 lukem if (ic->ic_rawbpf != NULL)
847 1.1 lukem bpf_mtap(ic->ic_rawbpf, m);
848 1.1 lukem #endif
849 1.1 lukem
850 1.1 lukem if (ipw_tx_start(ifp, m, ni) != 0) {
851 1.1 lukem if (ni != NULL)
852 1.1 lukem ieee80211_release_node(ic, ni);
853 1.1 lukem break;
854 1.1 lukem }
855 1.1 lukem
856 1.1 lukem /* start watchdog timer */
857 1.1 lukem sc->sc_tx_timer = 5;
858 1.1 lukem ifp->if_timer = 1;
859 1.1 lukem }
860 1.1 lukem }
861 1.1 lukem
862 1.1 lukem static void
863 1.1 lukem ipw_watchdog(struct ifnet *ifp)
864 1.1 lukem {
865 1.1 lukem struct ipw_softc *sc = ifp->if_softc;
866 1.1 lukem
867 1.1 lukem ifp->if_timer = 0;
868 1.1 lukem
869 1.1 lukem if (sc->sc_tx_timer > 0) {
870 1.1 lukem if (--sc->sc_tx_timer == 0) {
871 1.8 lukem aprint_error("%s: device timeout\n",
872 1.1 lukem sc->sc_dev.dv_xname);
873 1.1 lukem #ifdef notyet
874 1.1 lukem ipw_init(ifp);
875 1.1 lukem #endif
876 1.1 lukem return;
877 1.1 lukem }
878 1.1 lukem ifp->if_timer = 1;
879 1.1 lukem }
880 1.1 lukem
881 1.1 lukem ieee80211_watchdog(ifp);
882 1.1 lukem }
883 1.1 lukem
884 1.1 lukem static int
885 1.1 lukem ipw_get_table1(struct ipw_softc *sc, u_int32_t *tbl)
886 1.1 lukem {
887 1.1 lukem u_int32_t addr, size, i;
888 1.1 lukem
889 1.1 lukem if (!(sc->flags & IPW_FLAG_FW_INITED))
890 1.1 lukem return ENOTTY;
891 1.1 lukem
892 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
893 1.1 lukem
894 1.1 lukem size = CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA);
895 1.1 lukem if (suword(tbl, size) != 0)
896 1.1 lukem return EFAULT;
897 1.1 lukem
898 1.1 lukem for (i = 1, ++tbl; i < size; i++, tbl++) {
899 1.1 lukem addr = CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA);
900 1.1 lukem if (suword(tbl, MEM_READ_4(sc, addr)) != 0)
901 1.1 lukem return EFAULT;
902 1.1 lukem }
903 1.1 lukem return 0;
904 1.1 lukem }
905 1.1 lukem
906 1.1 lukem static int
907 1.1 lukem ipw_get_radio(struct ipw_softc *sc, int *ret)
908 1.1 lukem {
909 1.1 lukem u_int32_t addr;
910 1.1 lukem
911 1.1 lukem if (!(sc->flags & IPW_FLAG_FW_INITED))
912 1.1 lukem return ENOTTY;
913 1.1 lukem
914 1.1 lukem addr = ipw_read_table1(sc, IPW_INFO_EEPROM_ADDRESS);
915 1.1 lukem if ((MEM_READ_4(sc, addr + 32) >> 24) & 1) {
916 1.1 lukem suword(ret, -1);
917 1.1 lukem return 0;
918 1.1 lukem }
919 1.1 lukem
920 1.1 lukem if (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED)
921 1.1 lukem suword(ret, 0);
922 1.1 lukem else
923 1.1 lukem suword(ret, 1);
924 1.1 lukem
925 1.1 lukem return 0;
926 1.1 lukem }
927 1.1 lukem
928 1.1 lukem static int
929 1.1 lukem ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
930 1.1 lukem {
931 1.1 lukem struct ipw_softc *sc = ifp->if_softc;
932 1.1 lukem struct ifreq *ifr;
933 1.1 lukem int s, error = 0;
934 1.1 lukem
935 1.1 lukem s = splnet();
936 1.1 lukem
937 1.1 lukem switch (cmd) {
938 1.1 lukem case SIOCSIFFLAGS:
939 1.1 lukem if (ifp->if_flags & IFF_UP) {
940 1.1 lukem if (!(ifp->if_flags & IFF_RUNNING))
941 1.1 lukem ipw_init(ifp);
942 1.1 lukem } else {
943 1.1 lukem if (ifp->if_flags & IFF_RUNNING)
944 1.1 lukem ipw_stop(ifp, 1);
945 1.1 lukem }
946 1.1 lukem break;
947 1.1 lukem
948 1.1 lukem case SIOCGTABLE1:
949 1.1 lukem ifr = (struct ifreq *)data;
950 1.1 lukem error = ipw_get_table1(sc, (u_int32_t *)ifr->ifr_data);
951 1.1 lukem break;
952 1.1 lukem
953 1.1 lukem case SIOCGRADIO:
954 1.1 lukem ifr = (struct ifreq *)data;
955 1.1 lukem error = ipw_get_radio(sc, (int *)ifr->ifr_data);
956 1.1 lukem break;
957 1.1 lukem
958 1.1 lukem case SIOCSLOADFW:
959 1.1 lukem /* only super-user can do that! */
960 1.1 lukem if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
961 1.1 lukem break;
962 1.1 lukem
963 1.1 lukem ifr = (struct ifreq *)data;
964 1.1 lukem error = ipw_firmware_init(sc, (u_char *)ifr->ifr_data);
965 1.1 lukem break;
966 1.1 lukem
967 1.1 lukem case SIOCSKILLFW:
968 1.1 lukem /* only super-user can do that! */
969 1.1 lukem if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
970 1.1 lukem break;
971 1.1 lukem
972 1.1 lukem ipw_reset(sc);
973 1.1 lukem break;
974 1.1 lukem
975 1.1 lukem default:
976 1.1 lukem error = ieee80211_ioctl(ifp, cmd, data);
977 1.1 lukem if (error != ENETRESET)
978 1.1 lukem break;
979 1.1 lukem
980 1.8 lukem if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
981 1.1 lukem (IFF_UP | IFF_RUNNING))
982 1.1 lukem ipw_init(ifp);
983 1.1 lukem error = 0;
984 1.1 lukem }
985 1.1 lukem
986 1.1 lukem splx(s);
987 1.1 lukem return error;
988 1.1 lukem }
989 1.1 lukem
990 1.1 lukem static u_int32_t
991 1.1 lukem ipw_read_table1(struct ipw_softc *sc, u_int32_t off)
992 1.1 lukem {
993 1.1 lukem return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
994 1.1 lukem }
995 1.1 lukem
996 1.1 lukem static void
997 1.1 lukem ipw_write_table1(struct ipw_softc *sc, u_int32_t off, u_int32_t info)
998 1.1 lukem {
999 1.1 lukem MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
1000 1.1 lukem }
1001 1.1 lukem
1002 1.1 lukem static int
1003 1.1 lukem ipw_read_table2(struct ipw_softc *sc, u_int32_t off, void *buf, u_int32_t *len)
1004 1.1 lukem {
1005 1.1 lukem u_int32_t addr, info;
1006 1.1 lukem u_int16_t count, size;
1007 1.1 lukem u_int32_t total;
1008 1.1 lukem
1009 1.1 lukem /* addr[4] + count[2] + size[2] */
1010 1.1 lukem addr = MEM_READ_4(sc, sc->table2_base + off);
1011 1.1 lukem info = MEM_READ_4(sc, sc->table2_base + off + 4);
1012 1.1 lukem
1013 1.1 lukem count = info >> 16;
1014 1.1 lukem size = info & 0xffff;
1015 1.1 lukem total = count * size;
1016 1.1 lukem
1017 1.1 lukem if (total > *len) {
1018 1.1 lukem *len = total;
1019 1.1 lukem return EINVAL;
1020 1.1 lukem }
1021 1.1 lukem
1022 1.1 lukem *len = total;
1023 1.1 lukem ipw_read_mem_1(sc, addr, buf, total);
1024 1.1 lukem
1025 1.1 lukem return 0;
1026 1.1 lukem }
1027 1.1 lukem
1028 1.1 lukem static int
1029 1.1 lukem ipw_tx_init(struct ipw_softc *sc)
1030 1.1 lukem {
1031 1.1 lukem char *errmsg;
1032 1.1 lukem struct ipw_bd *bd;
1033 1.1 lukem struct ipw_soft_bd *sbd;
1034 1.1 lukem struct ipw_soft_hdr *shdr;
1035 1.1 lukem struct ipw_soft_buf *sbuf;
1036 1.1 lukem int error, i, nsegs;
1037 1.1 lukem
1038 1.1 lukem /* Allocate transmission buffer descriptors */
1039 1.8 lukem error = bus_dmamap_create(sc->sc_dmat, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0,
1040 1.1 lukem BUS_DMA_NOWAIT, &sc->tbd_map);
1041 1.1 lukem if (error != 0) {
1042 1.1 lukem errmsg = "could not create tbd dma map";
1043 1.1 lukem goto fail;
1044 1.1 lukem }
1045 1.1 lukem
1046 1.8 lukem error = bus_dmamem_alloc(sc->sc_dmat, IPW_TBD_SZ, PAGE_SIZE, 0,
1047 1.1 lukem &sc->tbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
1048 1.1 lukem if (error != 0) {
1049 1.1 lukem errmsg = "could not allocate tbd dma memory";
1050 1.1 lukem goto fail;
1051 1.1 lukem }
1052 1.1 lukem
1053 1.8 lukem error = bus_dmamem_map(sc->sc_dmat, &sc->tbd_seg, nsegs, IPW_TBD_SZ,
1054 1.1 lukem (caddr_t *)&sc->tbd_list, BUS_DMA_NOWAIT);
1055 1.1 lukem if (error != 0) {
1056 1.1 lukem errmsg = "could not map tbd dma memory";
1057 1.1 lukem goto fail;
1058 1.1 lukem }
1059 1.1 lukem
1060 1.8 lukem error = bus_dmamap_load(sc->sc_dmat, sc->tbd_map, sc->tbd_list,
1061 1.1 lukem IPW_TBD_SZ, NULL, BUS_DMA_NOWAIT);
1062 1.1 lukem if (error != 0) {
1063 1.1 lukem errmsg = "could not load tbd dma memory";
1064 1.1 lukem goto fail;
1065 1.1 lukem }
1066 1.1 lukem
1067 1.8 lukem sc->stbd_list = malloc(IPW_NTBD * sizeof (struct ipw_soft_bd),
1068 1.1 lukem M_DEVBUF, M_NOWAIT);
1069 1.1 lukem if (sc->stbd_list == NULL) {
1070 1.1 lukem errmsg = "could not allocate soft tbd";
1071 1.1 lukem error = ENOMEM;
1072 1.1 lukem goto fail;
1073 1.1 lukem }
1074 1.1 lukem sbd = sc->stbd_list;
1075 1.1 lukem bd = sc->tbd_list;
1076 1.1 lukem for (i = 0; i < IPW_NTBD; i++, sbd++, bd++) {
1077 1.1 lukem sbd->type = IPW_SBD_TYPE_NOASSOC;
1078 1.1 lukem sbd->bd = bd;
1079 1.1 lukem }
1080 1.1 lukem
1081 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_TX_BD_BASE, sc->tbd_map->dm_segs[0].ds_addr);
1082 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_TX_BD_SIZE, IPW_NTBD);
1083 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_TX_READ_INDEX, 0);
1084 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, 0);
1085 1.1 lukem sc->txold = IPW_NTBD - 1; /* latest bd index ack'ed by firmware */
1086 1.1 lukem sc->txcur = 0; /* bd index to write to */
1087 1.1 lukem
1088 1.1 lukem /* Allocate a DMA-able command */
1089 1.8 lukem error = bus_dmamap_create(sc->sc_dmat, sizeof (struct ipw_cmd), 1,
1090 1.1 lukem sizeof (struct ipw_cmd), 0, BUS_DMA_NOWAIT, &sc->cmd_map);
1091 1.1 lukem if (error != 0) {
1092 1.1 lukem errmsg = "could not create cmd dma map";
1093 1.1 lukem goto fail;
1094 1.1 lukem }
1095 1.1 lukem
1096 1.8 lukem error = bus_dmamem_alloc(sc->sc_dmat, sizeof (struct ipw_cmd),
1097 1.1 lukem PAGE_SIZE, 0, &sc->cmd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
1098 1.1 lukem if (error != 0) {
1099 1.1 lukem errmsg = "could not allocate cmd dma memory";
1100 1.1 lukem goto fail;
1101 1.1 lukem }
1102 1.1 lukem
1103 1.8 lukem error = bus_dmamem_map(sc->sc_dmat, &sc->cmd_seg, nsegs,
1104 1.1 lukem sizeof (struct ipw_cmd), (caddr_t *)&sc->cmd, BUS_DMA_NOWAIT);
1105 1.1 lukem if (error != 0) {
1106 1.1 lukem errmsg = "could not map cmd dma memory";
1107 1.1 lukem goto fail;
1108 1.1 lukem }
1109 1.1 lukem
1110 1.1 lukem /* Allocate a pool of DMA-able headers */
1111 1.1 lukem sc->shdr_list = malloc(IPW_NDATA * sizeof (struct ipw_soft_hdr),
1112 1.1 lukem M_DEVBUF, M_NOWAIT);
1113 1.1 lukem if (sc->shdr_list == NULL) {
1114 1.1 lukem errmsg = "could not allocate soft hdr";
1115 1.1 lukem error = ENOMEM;
1116 1.1 lukem goto fail;
1117 1.1 lukem }
1118 1.1 lukem TAILQ_INIT(&sc->sc_free_shdr);
1119 1.1 lukem for (i = 0, shdr = sc->shdr_list; i < IPW_NDATA; i++, shdr++) {
1120 1.8 lukem error = bus_dmamap_create(sc->sc_dmat,
1121 1.8 lukem sizeof (struct ipw_soft_hdr), 1,
1122 1.8 lukem sizeof (struct ipw_soft_hdr), 0, BUS_DMA_NOWAIT,
1123 1.1 lukem &shdr->map);
1124 1.1 lukem if (error != 0) {
1125 1.1 lukem errmsg = "could not create hdr dma map";
1126 1.1 lukem goto fail;
1127 1.1 lukem }
1128 1.1 lukem TAILQ_INSERT_TAIL(&sc->sc_free_shdr, shdr, next);
1129 1.1 lukem }
1130 1.1 lukem
1131 1.1 lukem /* Allocate a pool of DMA-able buffers */
1132 1.1 lukem sc->tx_sbuf_list = malloc(IPW_NDATA * sizeof (struct ipw_soft_buf),
1133 1.1 lukem M_DEVBUF, M_NOWAIT);
1134 1.1 lukem if (sc->tx_sbuf_list == NULL) {
1135 1.1 lukem errmsg = "could not allocate soft txbuf";
1136 1.1 lukem error = ENOMEM;
1137 1.1 lukem goto fail;
1138 1.1 lukem }
1139 1.1 lukem TAILQ_INIT(&sc->sc_free_sbuf);
1140 1.1 lukem for (i = 0, sbuf = sc->tx_sbuf_list; i < IPW_NDATA; i++, sbuf++) {
1141 1.8 lukem error = bus_dmamap_create(sc->sc_dmat, IPW_NDATA * MCLBYTES,
1142 1.1 lukem IPW_NDATA, MCLBYTES, 0, BUS_DMA_NOWAIT, &sbuf->map);
1143 1.1 lukem if (error != 0) {
1144 1.1 lukem errmsg = "could not create txbuf dma map";
1145 1.1 lukem goto fail;
1146 1.1 lukem }
1147 1.1 lukem TAILQ_INSERT_TAIL(&sc->sc_free_sbuf, sbuf, next);
1148 1.1 lukem }
1149 1.1 lukem
1150 1.1 lukem return 0;
1151 1.1 lukem
1152 1.1 lukem fail: aprint_error("%s: %s\n", sc->sc_dev.dv_xname, errmsg);
1153 1.1 lukem ipw_tx_stop(sc);
1154 1.1 lukem
1155 1.1 lukem return error;
1156 1.1 lukem }
1157 1.1 lukem
1158 1.1 lukem static void
1159 1.1 lukem ipw_tx_stop(struct ipw_softc *sc)
1160 1.1 lukem {
1161 1.1 lukem struct ipw_soft_hdr *shdr;
1162 1.1 lukem struct ipw_soft_buf *sbuf;
1163 1.1 lukem int i;
1164 1.1 lukem
1165 1.1 lukem if (sc->tbd_map != NULL) {
1166 1.1 lukem if (sc->tbd_list != NULL) {
1167 1.1 lukem bus_dmamap_unload(sc->sc_dmat, sc->tbd_map);
1168 1.8 lukem bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->tbd_list,
1169 1.1 lukem IPW_TBD_SZ);
1170 1.1 lukem bus_dmamem_free(sc->sc_dmat, &sc->tbd_seg, 1);
1171 1.1 lukem sc->tbd_list = NULL;
1172 1.1 lukem }
1173 1.1 lukem bus_dmamap_destroy(sc->sc_dmat, sc->tbd_map);
1174 1.1 lukem sc->tbd_map = NULL;
1175 1.1 lukem }
1176 1.1 lukem
1177 1.1 lukem if (sc->stbd_list != NULL) {
1178 1.1 lukem for (i = 0; i < IPW_NTBD; i++)
1179 1.1 lukem ipw_release_sbd(sc, &sc->stbd_list[i]);
1180 1.1 lukem free(sc->stbd_list, M_DEVBUF);
1181 1.1 lukem sc->stbd_list = NULL;
1182 1.1 lukem }
1183 1.1 lukem
1184 1.1 lukem if (sc->cmd_map != NULL) {
1185 1.1 lukem if (sc->cmd != NULL) {
1186 1.8 lukem bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->cmd,
1187 1.1 lukem sizeof (struct ipw_cmd));
1188 1.1 lukem bus_dmamem_free(sc->sc_dmat, &sc->cmd_seg, 1);
1189 1.1 lukem sc->cmd = NULL;
1190 1.1 lukem }
1191 1.1 lukem bus_dmamap_destroy(sc->sc_dmat, sc->cmd_map);
1192 1.1 lukem sc->cmd_map = NULL;
1193 1.1 lukem }
1194 1.1 lukem
1195 1.1 lukem if (sc->shdr_list != NULL) {
1196 1.1 lukem TAILQ_FOREACH(shdr, &sc->sc_free_shdr, next)
1197 1.1 lukem bus_dmamap_destroy(sc->sc_dmat, shdr->map);
1198 1.1 lukem free(sc->shdr_list, M_DEVBUF);
1199 1.1 lukem sc->shdr_list = NULL;
1200 1.1 lukem }
1201 1.1 lukem
1202 1.1 lukem
1203 1.1 lukem if (sc->tx_sbuf_list != NULL) {
1204 1.1 lukem TAILQ_FOREACH(sbuf, &sc->sc_free_sbuf, next)
1205 1.1 lukem bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
1206 1.1 lukem free(sc->tx_sbuf_list, M_DEVBUF);
1207 1.1 lukem sc->tx_sbuf_list = NULL;
1208 1.1 lukem }
1209 1.1 lukem }
1210 1.1 lukem
1211 1.1 lukem static int
1212 1.1 lukem ipw_rx_init(struct ipw_softc *sc)
1213 1.1 lukem {
1214 1.1 lukem char *errmsg;
1215 1.1 lukem struct ipw_bd *bd;
1216 1.1 lukem struct ipw_soft_bd *sbd;
1217 1.1 lukem struct ipw_soft_buf *sbuf;
1218 1.1 lukem int error, i, nsegs;
1219 1.1 lukem
1220 1.1 lukem /* Allocate reception buffer descriptors */
1221 1.8 lukem error = bus_dmamap_create(sc->sc_dmat, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0,
1222 1.1 lukem BUS_DMA_NOWAIT, &sc->rbd_map);
1223 1.1 lukem if (error != 0) {
1224 1.1 lukem errmsg = "could not create rbd dma map";
1225 1.1 lukem goto fail;
1226 1.1 lukem }
1227 1.1 lukem
1228 1.8 lukem error = bus_dmamem_alloc(sc->sc_dmat, IPW_RBD_SZ, PAGE_SIZE, 0,
1229 1.1 lukem &sc->rbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
1230 1.1 lukem if (error != 0) {
1231 1.1 lukem errmsg = "could not allocate rbd dma memory";
1232 1.1 lukem goto fail;
1233 1.1 lukem }
1234 1.1 lukem
1235 1.8 lukem error = bus_dmamem_map(sc->sc_dmat, &sc->rbd_seg, nsegs, IPW_RBD_SZ,
1236 1.1 lukem (caddr_t *)&sc->rbd_list, BUS_DMA_NOWAIT);
1237 1.1 lukem if (error != 0) {
1238 1.1 lukem errmsg = "could not map rbd dma memory";
1239 1.1 lukem goto fail;
1240 1.1 lukem }
1241 1.1 lukem
1242 1.8 lukem error = bus_dmamap_load(sc->sc_dmat, sc->rbd_map, sc->rbd_list,
1243 1.1 lukem IPW_RBD_SZ, NULL, BUS_DMA_NOWAIT);
1244 1.1 lukem if (error != 0) {
1245 1.1 lukem errmsg = "could not load rbd dma memory";
1246 1.1 lukem goto fail;
1247 1.1 lukem }
1248 1.1 lukem
1249 1.8 lukem sc->srbd_list = malloc(IPW_NRBD * sizeof (struct ipw_soft_bd),
1250 1.1 lukem M_DEVBUF, M_NOWAIT);
1251 1.1 lukem if (sc->srbd_list == NULL) {
1252 1.1 lukem errmsg = "could not allocate soft rbd";
1253 1.1 lukem error = ENOMEM;
1254 1.1 lukem goto fail;
1255 1.1 lukem }
1256 1.1 lukem sbd = sc->srbd_list;
1257 1.1 lukem bd = sc->rbd_list;
1258 1.1 lukem for (i = 0; i < IPW_NRBD; i++, sbd++, bd++) {
1259 1.1 lukem sbd->type = IPW_SBD_TYPE_NOASSOC;
1260 1.1 lukem sbd->bd = bd;
1261 1.1 lukem }
1262 1.1 lukem
1263 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_RX_BD_BASE, sc->rbd_map->dm_segs[0].ds_addr);
1264 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_RX_BD_SIZE, IPW_NRBD);
1265 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_RX_READ_INDEX, 0);
1266 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_RX_WRITE_INDEX, IPW_NRBD - 1);
1267 1.1 lukem sc->rxcur = IPW_NRBD - 1; /* latest bd index I've read */
1268 1.1 lukem
1269 1.1 lukem /* Allocate status descriptors */
1270 1.8 lukem error = bus_dmamap_create(sc->sc_dmat, IPW_STATUS_SZ, 1, IPW_STATUS_SZ,
1271 1.1 lukem 0, BUS_DMA_NOWAIT, &sc->status_map);
1272 1.1 lukem if (error != 0) {
1273 1.1 lukem errmsg = "could not create status dma map";
1274 1.1 lukem goto fail;
1275 1.1 lukem }
1276 1.1 lukem
1277 1.8 lukem error = bus_dmamem_alloc(sc->sc_dmat, IPW_STATUS_SZ, PAGE_SIZE, 0,
1278 1.1 lukem &sc->status_seg, 1, &nsegs, BUS_DMA_NOWAIT);
1279 1.1 lukem if (error != 0) {
1280 1.1 lukem errmsg = "could not allocate status dma memory";
1281 1.1 lukem goto fail;
1282 1.1 lukem }
1283 1.1 lukem
1284 1.8 lukem error = bus_dmamem_map(sc->sc_dmat, &sc->status_seg, nsegs,
1285 1.1 lukem IPW_STATUS_SZ, (caddr_t *)&sc->status_list, BUS_DMA_NOWAIT);
1286 1.1 lukem if (error != 0) {
1287 1.1 lukem errmsg = "could not map status dma memory";
1288 1.1 lukem goto fail;
1289 1.1 lukem }
1290 1.1 lukem
1291 1.8 lukem error = bus_dmamap_load(sc->sc_dmat, sc->status_map, sc->status_list,
1292 1.1 lukem IPW_STATUS_SZ, NULL, BUS_DMA_NOWAIT);
1293 1.1 lukem if (error != 0) {
1294 1.1 lukem errmsg = "could not load status dma memory";
1295 1.1 lukem goto fail;
1296 1.1 lukem }
1297 1.1 lukem
1298 1.8 lukem CSR_WRITE_4(sc, IPW_CSR_RX_STATUS_BASE,
1299 1.1 lukem sc->status_map->dm_segs[0].ds_addr);
1300 1.1 lukem
1301 1.1 lukem sc->rx_sbuf_list = malloc(IPW_NRBD * sizeof (struct ipw_soft_buf),
1302 1.1 lukem M_DEVBUF, M_NOWAIT);
1303 1.1 lukem if (sc->rx_sbuf_list == NULL) {
1304 1.1 lukem errmsg = "could not allocate soft rxbuf";
1305 1.1 lukem error = ENOMEM;
1306 1.1 lukem goto fail;
1307 1.1 lukem }
1308 1.1 lukem
1309 1.1 lukem sbuf = sc->rx_sbuf_list;
1310 1.1 lukem sbd = sc->srbd_list;
1311 1.1 lukem for (i = 0; i < IPW_NRBD; i++, sbuf++, sbd++) {
1312 1.1 lukem
1313 1.1 lukem MGETHDR(sbuf->m, M_DONTWAIT, MT_DATA);
1314 1.1 lukem if (sbuf->m == NULL) {
1315 1.1 lukem errmsg = "could not allocate rx mbuf";
1316 1.1 lukem error = ENOMEM;
1317 1.1 lukem goto fail;
1318 1.1 lukem }
1319 1.1 lukem MCLGET(sbuf->m, M_DONTWAIT);
1320 1.1 lukem if (!(sbuf->m->m_flags & M_EXT)) {
1321 1.1 lukem m_freem(sbuf->m);
1322 1.1 lukem errmsg = "could not allocate rx mbuf cluster";
1323 1.1 lukem error = ENOMEM;
1324 1.1 lukem goto fail;
1325 1.1 lukem }
1326 1.1 lukem
1327 1.8 lukem error = bus_dmamap_create(sc->sc_dmat, IPW_NRBD * MCLBYTES,
1328 1.1 lukem IPW_NRBD, MCLBYTES, 0, BUS_DMA_NOWAIT, &sbuf->map);
1329 1.1 lukem if (error != 0) {
1330 1.1 lukem m_freem(sbuf->m);
1331 1.1 lukem errmsg = "could not create rxbuf dma map";
1332 1.1 lukem goto fail;
1333 1.1 lukem }
1334 1.8 lukem error = bus_dmamap_load(sc->sc_dmat, sbuf->map,
1335 1.1 lukem mtod(sbuf->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
1336 1.1 lukem if (error != 0) {
1337 1.1 lukem bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
1338 1.1 lukem m_freem(sbuf->m);
1339 1.1 lukem errmsg = "could not map rxbuf dma memory";
1340 1.1 lukem goto fail;
1341 1.1 lukem }
1342 1.1 lukem sbd->type = IPW_SBD_TYPE_DATA;
1343 1.1 lukem sbd->priv = sbuf;
1344 1.1 lukem sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
1345 1.1 lukem sbd->bd->len = htole32(MCLBYTES);
1346 1.1 lukem }
1347 1.1 lukem
1348 1.1 lukem return 0;
1349 1.1 lukem
1350 1.1 lukem fail: aprint_error("%s: %s\n", sc->sc_dev.dv_xname, errmsg);
1351 1.1 lukem ipw_rx_stop(sc);
1352 1.1 lukem
1353 1.1 lukem return error;
1354 1.1 lukem }
1355 1.1 lukem
1356 1.1 lukem static void
1357 1.1 lukem ipw_rx_stop(struct ipw_softc *sc)
1358 1.1 lukem {
1359 1.1 lukem struct ipw_soft_bd *sbd;
1360 1.1 lukem struct ipw_soft_buf *sbuf;
1361 1.1 lukem int i;
1362 1.1 lukem
1363 1.1 lukem if (sc->rbd_map != NULL) {
1364 1.1 lukem if (sc->rbd_list != NULL) {
1365 1.1 lukem bus_dmamap_unload(sc->sc_dmat, sc->rbd_map);
1366 1.8 lukem bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rbd_list,
1367 1.1 lukem IPW_RBD_SZ);
1368 1.1 lukem bus_dmamem_free(sc->sc_dmat, &sc->rbd_seg, 1);
1369 1.1 lukem sc->rbd_list = NULL;
1370 1.1 lukem }
1371 1.1 lukem bus_dmamap_destroy(sc->sc_dmat, sc->rbd_map);
1372 1.1 lukem sc->rbd_map = NULL;
1373 1.1 lukem }
1374 1.1 lukem
1375 1.1 lukem if (sc->status_map != NULL) {
1376 1.1 lukem if (sc->status_list != NULL) {
1377 1.1 lukem bus_dmamap_unload(sc->sc_dmat, sc->status_map);
1378 1.8 lukem bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->status_list,
1379 1.1 lukem IPW_STATUS_SZ);
1380 1.1 lukem bus_dmamem_free(sc->sc_dmat, &sc->status_seg, 1);
1381 1.1 lukem sc->status_list = NULL;
1382 1.1 lukem }
1383 1.1 lukem bus_dmamap_destroy(sc->sc_dmat, sc->status_map);
1384 1.1 lukem sc->status_map = NULL;
1385 1.1 lukem }
1386 1.1 lukem
1387 1.1 lukem if (sc->srbd_list != NULL) {
1388 1.1 lukem for (i = 0, sbd = sc->srbd_list; i < IPW_NRBD; i++, sbd++) {
1389 1.1 lukem if (sbd->type == IPW_SBD_TYPE_NOASSOC)
1390 1.1 lukem continue;
1391 1.1 lukem
1392 1.1 lukem sbuf = sbd->priv;
1393 1.1 lukem bus_dmamap_unload(sc->sc_dmat, sbuf->map);
1394 1.1 lukem bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
1395 1.1 lukem m_freem(sbuf->m);
1396 1.1 lukem }
1397 1.1 lukem free(sc->srbd_list, M_DEVBUF);
1398 1.1 lukem sc->srbd_list = NULL;
1399 1.1 lukem }
1400 1.1 lukem
1401 1.1 lukem if (sc->rx_sbuf_list != NULL) {
1402 1.1 lukem free(sc->rx_sbuf_list, M_DEVBUF);
1403 1.1 lukem sc->rx_sbuf_list = NULL;
1404 1.1 lukem }
1405 1.1 lukem }
1406 1.1 lukem
1407 1.1 lukem static void
1408 1.1 lukem ipw_reset(struct ipw_softc *sc)
1409 1.1 lukem {
1410 1.1 lukem struct ifnet *ifp = &sc->sc_ic.ic_if;
1411 1.1 lukem int ntries;
1412 1.1 lukem
1413 1.1 lukem ipw_stop(ifp, 1);
1414 1.1 lukem
1415 1.1 lukem if (sc->flags & IPW_FLAG_FW_INITED) {
1416 1.1 lukem ipw_cmd(sc, IPW_CMD_DISABLE_PHY, NULL, 0);
1417 1.1 lukem ipw_cmd(sc, IPW_CMD_PREPARE_POWER_DOWN, NULL, 0);
1418 1.1 lukem
1419 1.1 lukem sc->flags &= ~IPW_FLAG_FW_INITED;
1420 1.1 lukem }
1421 1.1 lukem
1422 1.1 lukem /* Disable interrupts */
1423 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1424 1.1 lukem
1425 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1426 1.1 lukem for (ntries = 0; ntries < 5; ntries++) {
1427 1.1 lukem if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1428 1.1 lukem break;
1429 1.1 lukem DELAY(10);
1430 1.1 lukem }
1431 1.1 lukem
1432 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
1433 1.1 lukem
1434 1.1 lukem ipw_rx_stop(sc);
1435 1.1 lukem ipw_tx_stop(sc);
1436 1.1 lukem
1437 1.1 lukem ifp->if_flags &= ~IFF_UP;
1438 1.1 lukem }
1439 1.1 lukem
1440 1.1 lukem static int
1441 1.1 lukem ipw_clock_sync(struct ipw_softc *sc)
1442 1.1 lukem {
1443 1.1 lukem int ntries;
1444 1.1 lukem u_int32_t r;
1445 1.1 lukem
1446 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
1447 1.1 lukem for (ntries = 0; ntries < 1000; ntries++) {
1448 1.1 lukem if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_PRINCETON_RESET)
1449 1.1 lukem break;
1450 1.1 lukem DELAY(10);
1451 1.1 lukem }
1452 1.1 lukem if (ntries == 1000)
1453 1.1 lukem return EIO;
1454 1.1 lukem
1455 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_CTL, IPW_CTL_INIT_DONE);
1456 1.1 lukem for (ntries = 0; ntries < 1000; ntries++) {
1457 1.1 lukem if ((r = CSR_READ_4(sc, IPW_CSR_CTL)) & IPW_CTL_CLOCK_READY)
1458 1.1 lukem break;
1459 1.1 lukem DELAY(200);
1460 1.1 lukem }
1461 1.1 lukem if (ntries == 1000)
1462 1.1 lukem return EIO;
1463 1.1 lukem
1464 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_CTL, r | IPW_CTL_ALLOW_STANDBY);
1465 1.1 lukem
1466 1.1 lukem return 0;
1467 1.1 lukem }
1468 1.1 lukem
1469 1.1 lukem static int
1470 1.1 lukem ipw_load_ucode(struct ipw_softc *sc, u_char *uc, int size)
1471 1.1 lukem {
1472 1.1 lukem int ntries;
1473 1.1 lukem
1474 1.1 lukem MEM_WRITE_2(sc, 0x220000, 0x0703);
1475 1.1 lukem MEM_WRITE_2(sc, 0x220000, 0x0707);
1476 1.1 lukem
1477 1.1 lukem MEM_WRITE_1(sc, 0x210014, 0x72);
1478 1.1 lukem MEM_WRITE_1(sc, 0x210014, 0x72);
1479 1.1 lukem
1480 1.1 lukem MEM_WRITE_1(sc, 0x210000, 0x40);
1481 1.1 lukem MEM_WRITE_1(sc, 0x210000, 0x00);
1482 1.1 lukem MEM_WRITE_1(sc, 0x210000, 0x40);
1483 1.1 lukem
1484 1.1 lukem MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1485 1.1 lukem
1486 1.1 lukem MEM_WRITE_1(sc, 0x210000, 0x00);
1487 1.1 lukem MEM_WRITE_1(sc, 0x210000, 0x00);
1488 1.1 lukem MEM_WRITE_1(sc, 0x210000, 0x80);
1489 1.1 lukem
1490 1.1 lukem MEM_WRITE_2(sc, 0x220000, 0x0703);
1491 1.1 lukem MEM_WRITE_2(sc, 0x220000, 0x0707);
1492 1.1 lukem
1493 1.1 lukem MEM_WRITE_1(sc, 0x210014, 0x72);
1494 1.1 lukem MEM_WRITE_1(sc, 0x210014, 0x72);
1495 1.1 lukem
1496 1.1 lukem MEM_WRITE_1(sc, 0x210000, 0x00);
1497 1.1 lukem MEM_WRITE_1(sc, 0x210000, 0x80);
1498 1.1 lukem
1499 1.1 lukem for (ntries = 0; ntries < 10; ntries++) {
1500 1.1 lukem if (MEM_READ_1(sc, 0x210000) & 1)
1501 1.1 lukem break;
1502 1.1 lukem DELAY(10);
1503 1.1 lukem }
1504 1.1 lukem if (ntries == 10)
1505 1.1 lukem return EIO;
1506 1.1 lukem
1507 1.1 lukem return 0;
1508 1.1 lukem }
1509 1.1 lukem
1510 1.1 lukem /* set of macros to handle unaligned little endian data in firmware image */
1511 1.1 lukem #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1512 1.1 lukem #define GETLE16(p) ((p)[0] | (p)[1] << 8)
1513 1.1 lukem static int
1514 1.1 lukem ipw_load_firmware(struct ipw_softc *sc, u_char *fw, int size)
1515 1.1 lukem {
1516 1.1 lukem u_char *p, *end;
1517 1.1 lukem u_int32_t addr;
1518 1.1 lukem u_int16_t len;
1519 1.1 lukem
1520 1.1 lukem p = fw;
1521 1.1 lukem end = fw + size;
1522 1.1 lukem while (p < end) {
1523 1.1 lukem if (p + 6 > end)
1524 1.1 lukem return EINVAL;
1525 1.1 lukem
1526 1.1 lukem addr = GETLE32(p);
1527 1.1 lukem p += 4;
1528 1.1 lukem len = GETLE16(p);
1529 1.1 lukem p += 2;
1530 1.1 lukem
1531 1.1 lukem if (p + len > end)
1532 1.1 lukem return EINVAL;
1533 1.1 lukem
1534 1.1 lukem ipw_write_mem_1(sc, addr, p, len);
1535 1.1 lukem p += len;
1536 1.1 lukem }
1537 1.1 lukem return 0;
1538 1.1 lukem }
1539 1.1 lukem
1540 1.1 lukem static int
1541 1.1 lukem ipw_firmware_init(struct ipw_softc *sc, u_char *data)
1542 1.1 lukem {
1543 1.1 lukem struct ieee80211com *ic = &sc->sc_ic;
1544 1.1 lukem struct ifnet *ifp = &ic->ic_if;
1545 1.1 lukem struct ipw_fw_hdr hdr;
1546 1.1 lukem u_int32_t r, len, fw_size, uc_size;
1547 1.1 lukem u_char *fw, *uc;
1548 1.1 lukem int error;
1549 1.1 lukem
1550 1.1 lukem ipw_reset(sc);
1551 1.1 lukem
1552 1.1 lukem if ((error = copyin(data, &hdr, sizeof hdr)) != 0)
1553 1.1 lukem goto fail1;
1554 1.1 lukem
1555 1.1 lukem fw_size = le32toh(hdr.fw_size);
1556 1.1 lukem uc_size = le32toh(hdr.uc_size);
1557 1.1 lukem data += sizeof hdr;
1558 1.1 lukem
1559 1.1 lukem if ((fw = malloc(fw_size, M_DEVBUF, M_NOWAIT)) == NULL) {
1560 1.1 lukem error = ENOMEM;
1561 1.1 lukem goto fail1;
1562 1.1 lukem }
1563 1.1 lukem
1564 1.1 lukem if ((error = copyin(data, fw, fw_size)) != 0)
1565 1.1 lukem goto fail2;
1566 1.1 lukem
1567 1.1 lukem data += fw_size;
1568 1.1 lukem
1569 1.1 lukem if ((uc = malloc(uc_size, M_DEVBUF, M_NOWAIT)) == NULL) {
1570 1.1 lukem error = ENOMEM;
1571 1.1 lukem goto fail2;
1572 1.1 lukem }
1573 1.1 lukem
1574 1.1 lukem if ((error = copyin(data, uc, uc_size)) != 0)
1575 1.1 lukem goto fail3;
1576 1.1 lukem
1577 1.1 lukem if ((error = ipw_clock_sync(sc)) != 0) {
1578 1.1 lukem aprint_error("%s: clock synchronization failed\n",
1579 1.1 lukem sc->sc_dev.dv_xname);
1580 1.1 lukem goto fail3;
1581 1.1 lukem }
1582 1.1 lukem
1583 1.1 lukem MEM_WRITE_4(sc, 0x003000e0, 0x80000000);
1584 1.1 lukem
1585 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1586 1.1 lukem
1587 1.1 lukem if ((error = ipw_load_ucode(sc, uc, uc_size)) != 0) {
1588 1.1 lukem aprint_error("%s: could not load microcode\n",
1589 1.1 lukem sc->sc_dev.dv_xname);
1590 1.1 lukem goto fail3;
1591 1.1 lukem }
1592 1.1 lukem
1593 1.1 lukem MEM_WRITE_4(sc, 0x003000e0, 0);
1594 1.1 lukem
1595 1.1 lukem if ((error = ipw_clock_sync(sc)) != 0) {
1596 1.1 lukem aprint_error("%s: clock synchronization failed\n",
1597 1.1 lukem sc->sc_dev.dv_xname);
1598 1.1 lukem goto fail3;
1599 1.1 lukem }
1600 1.1 lukem
1601 1.1 lukem if ((error = ipw_load_firmware(sc, fw, fw_size))) {
1602 1.8 lukem aprint_error("%s: could not load firmware\n",
1603 1.1 lukem sc->sc_dev.dv_xname);
1604 1.1 lukem goto fail3;
1605 1.1 lukem }
1606 1.1 lukem
1607 1.1 lukem ipw_zero_mem_4(sc, 0x0002f200, 196);
1608 1.1 lukem ipw_zero_mem_4(sc, 0x0002f610, 8);
1609 1.1 lukem ipw_zero_mem_4(sc, 0x0002fa00, 8);
1610 1.1 lukem ipw_zero_mem_4(sc, 0x0002fc00, 4);
1611 1.1 lukem ipw_zero_mem_4(sc, 0x0002ff80, 32);
1612 1.1 lukem
1613 1.1 lukem if ((error = ipw_rx_init(sc)) != 0) {
1614 1.1 lukem aprint_error("%s: could not initialize rx queue\n",
1615 1.1 lukem sc->sc_dev.dv_xname);
1616 1.1 lukem goto fail3;
1617 1.1 lukem }
1618 1.1 lukem
1619 1.1 lukem if ((error = ipw_tx_init(sc)) != 0) {
1620 1.1 lukem aprint_error("%s: could not initialize tx queue\n",
1621 1.1 lukem sc->sc_dev.dv_xname);
1622 1.1 lukem goto fail3;
1623 1.1 lukem }
1624 1.1 lukem
1625 1.8 lukem CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
1626 1.1 lukem IPW_IO_LED_OFF);
1627 1.1 lukem
1628 1.1 lukem /* Enable interrupts */
1629 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1630 1.1 lukem
1631 1.1 lukem /* Let's go! */
1632 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1633 1.1 lukem
1634 1.1 lukem /* Wait at most 5 seconds for firmware initialization to complete */
1635 1.1 lukem if ((error = tsleep(sc, 0, "ipwinit", 5 * hz)) != 0) {
1636 1.1 lukem aprint_error("%s: timeout waiting for firmware initialization "
1637 1.1 lukem "to complete\n", sc->sc_dev.dv_xname);
1638 1.1 lukem goto fail3;
1639 1.1 lukem }
1640 1.1 lukem
1641 1.1 lukem /* Firmware initialization completed */
1642 1.1 lukem sc->flags |= IPW_FLAG_FW_INITED;
1643 1.1 lukem
1644 1.1 lukem free(uc, M_DEVBUF);
1645 1.1 lukem free(fw, M_DEVBUF);
1646 1.1 lukem
1647 1.1 lukem r = CSR_READ_4(sc, IPW_CSR_IO);
1648 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_IO, r | IPW_IO_GPIO1_MASK | IPW_IO_GPIO3_MASK);
1649 1.1 lukem
1650 1.1 lukem /* Retrieve information tables base addresses */
1651 1.1 lukem sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
1652 1.1 lukem sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
1653 1.1 lukem
1654 1.1 lukem ipw_write_table1(sc, IPW_INFO_LOCK, 0);
1655 1.1 lukem
1656 1.1 lukem /* Retrieve adapter MAC address */
1657 1.1 lukem len = IEEE80211_ADDR_LEN;
1658 1.1 lukem ipw_read_table2(sc, IPW_INFO_ADAPTER_MAC, ic->ic_myaddr, &len);
1659 1.1 lukem
1660 1.1 lukem IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), ic->ic_myaddr);
1661 1.1 lukem
1662 1.1 lukem return 0;
1663 1.1 lukem
1664 1.1 lukem fail3: free(uc, M_DEVBUF);
1665 1.1 lukem fail2: free(fw, M_DEVBUF);
1666 1.1 lukem fail1: ipw_reset(sc);
1667 1.1 lukem
1668 1.1 lukem return error;
1669 1.1 lukem }
1670 1.1 lukem
1671 1.1 lukem static int
1672 1.1 lukem ipw_config(struct ipw_softc *sc)
1673 1.1 lukem {
1674 1.1 lukem struct ieee80211com *ic = &sc->sc_ic;
1675 1.1 lukem struct ifnet *ifp = &ic->ic_if;
1676 1.1 lukem struct ipw_security security;
1677 1.1 lukem struct ieee80211_wepkey *k;
1678 1.1 lukem struct ipw_wep_key wepkey;
1679 1.1 lukem struct ipw_scan_options options;
1680 1.1 lukem struct ipw_configuration config;
1681 1.1 lukem u_int32_t data;
1682 1.1 lukem int error, i;
1683 1.1 lukem
1684 1.1 lukem switch (ic->ic_opmode) {
1685 1.1 lukem case IEEE80211_M_STA:
1686 1.1 lukem case IEEE80211_M_HOSTAP:
1687 1.1 lukem data = htole32(IPW_MODE_BSS);
1688 1.1 lukem break;
1689 1.1 lukem
1690 1.1 lukem case IEEE80211_M_IBSS:
1691 1.1 lukem case IEEE80211_M_AHDEMO:
1692 1.1 lukem data = htole32(IPW_MODE_IBSS);
1693 1.1 lukem break;
1694 1.1 lukem
1695 1.1 lukem case IEEE80211_M_MONITOR:
1696 1.1 lukem data = htole32(IPW_MODE_MONITOR);
1697 1.1 lukem break;
1698 1.1 lukem }
1699 1.1 lukem DPRINTF(("Setting adapter mode to %u\n", data));
1700 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
1701 1.1 lukem if (error != 0)
1702 1.1 lukem return error;
1703 1.1 lukem
1704 1.8 lukem if (ic->ic_opmode == IEEE80211_M_IBSS ||
1705 1.1 lukem ic->ic_opmode == IEEE80211_M_MONITOR) {
1706 1.1 lukem data = htole32(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
1707 1.1 lukem DPRINTF(("Setting adapter channel to %u\n", data));
1708 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
1709 1.1 lukem if (error != 0)
1710 1.1 lukem return error;
1711 1.1 lukem }
1712 1.1 lukem
1713 1.5 lukem if (ic->ic_opmode == IEEE80211_M_MONITOR) {
1714 1.5 lukem DPRINTF(("Enabling adapter\n"));
1715 1.5 lukem return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1716 1.5 lukem }
1717 1.5 lukem
1718 1.5 lukem DPRINTF(("Setting adapter MAC to %s\n", ether_sprintf(ic->ic_myaddr)));
1719 1.5 lukem IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), ic->ic_myaddr);
1720 1.8 lukem error = ipw_cmd(sc, IPW_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
1721 1.5 lukem IEEE80211_ADDR_LEN);
1722 1.5 lukem if (error != 0)
1723 1.5 lukem return error;
1724 1.5 lukem
1725 1.8 lukem config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
1726 1.1 lukem IPW_CFG_PREAMBLE_LEN | IPW_CFG_802_1x_ENABLE);
1727 1.1 lukem if (ic->ic_opmode == IEEE80211_M_IBSS)
1728 1.1 lukem config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
1729 1.1 lukem if (ifp->if_flags & IFF_PROMISC)
1730 1.1 lukem config.flags |= htole32(IPW_CFG_PROMISCUOUS);
1731 1.1 lukem config.channels = htole32(0x3fff); /* channels 1-14 */
1732 1.1 lukem config.ibss_chan = htole32(0x7ff);
1733 1.1 lukem DPRINTF(("Setting adapter configuration 0x%08x\n", config.flags));
1734 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
1735 1.1 lukem if (error != 0)
1736 1.1 lukem return error;
1737 1.1 lukem
1738 1.1 lukem data = htole32(0x3); /* 1, 2 */
1739 1.1 lukem DPRINTF(("Setting adapter basic tx rates to 0x%x\n", data));
1740 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
1741 1.1 lukem if (error != 0)
1742 1.1 lukem return error;
1743 1.1 lukem
1744 1.1 lukem data = htole32(0xf); /* 1, 2, 5.5, 11 */
1745 1.1 lukem DPRINTF(("Setting adapter tx rates to 0x%x\n", data));
1746 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
1747 1.1 lukem if (error != 0)
1748 1.1 lukem return error;
1749 1.1 lukem
1750 1.1 lukem data = htole32(IPW_POWER_MODE_CAM);
1751 1.1 lukem DPRINTF(("Setting adapter power mode to %u\n", data));
1752 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
1753 1.1 lukem if (error != 0)
1754 1.1 lukem return error;
1755 1.1 lukem
1756 1.1 lukem if (ic->ic_opmode == IEEE80211_M_IBSS) {
1757 1.1 lukem data = htole32(ic->ic_txpower);
1758 1.1 lukem DPRINTF(("Setting adapter tx power index to %u\n", data));
1759 1.8 lukem error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
1760 1.1 lukem sizeof data);
1761 1.1 lukem if (error != 0)
1762 1.1 lukem return error;
1763 1.1 lukem }
1764 1.1 lukem
1765 1.1 lukem data = htole32(ic->ic_rtsthreshold);
1766 1.1 lukem DPRINTF(("Setting adapter RTS threshold to %u\n", data));
1767 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
1768 1.1 lukem if (error != 0)
1769 1.1 lukem return error;
1770 1.1 lukem
1771 1.1 lukem data = htole32(ic->ic_fragthreshold);
1772 1.1 lukem DPRINTF(("Setting adapter frag threshold to %u\n", data));
1773 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
1774 1.1 lukem if (error != 0)
1775 1.1 lukem return error;
1776 1.1 lukem
1777 1.1 lukem #ifdef IPW_DEBUG
1778 1.1 lukem if (ipw_debug > 0) {
1779 1.1 lukem printf("Setting adapter ESSID to ");
1780 1.1 lukem ieee80211_print_essid(ic->ic_des_essid, ic->ic_des_esslen);
1781 1.1 lukem printf("\n");
1782 1.1 lukem }
1783 1.1 lukem #endif
1784 1.8 lukem error = ipw_cmd(sc, IPW_CMD_SET_ESSID, ic->ic_des_essid,
1785 1.1 lukem ic->ic_des_esslen);
1786 1.1 lukem if (error != 0)
1787 1.1 lukem return error;
1788 1.1 lukem
1789 1.1 lukem /* no mandatory BSSID */
1790 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
1791 1.1 lukem if (error != 0)
1792 1.1 lukem return error;
1793 1.1 lukem
1794 1.1 lukem if (ic->ic_flags & IEEE80211_F_DESBSSID) {
1795 1.8 lukem DPRINTF(("Setting adapter desired BSSID to %s\n",
1796 1.1 lukem ether_sprintf(ic->ic_des_bssid)));
1797 1.8 lukem error = ipw_cmd(sc, IPW_CMD_SET_DESIRED_BSSID,
1798 1.1 lukem ic->ic_des_bssid, IEEE80211_ADDR_LEN);
1799 1.1 lukem if (error != 0)
1800 1.1 lukem return error;
1801 1.1 lukem }
1802 1.1 lukem
1803 1.1 lukem security.authmode = IPW_AUTH_OPEN;
1804 1.1 lukem security.ciphers = htole32(IPW_CIPHER_NONE);
1805 1.1 lukem security.version = htole16(0);
1806 1.1 lukem security.replay_counters_number = 0;
1807 1.1 lukem security.unicast_using_group = 0;
1808 1.1 lukem DPRINTF(("Setting adapter authmode to %u\n", security.authmode));
1809 1.8 lukem error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFORMATION, &security,
1810 1.1 lukem sizeof security);
1811 1.1 lukem if (error != 0)
1812 1.1 lukem return error;
1813 1.1 lukem
1814 1.1 lukem if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1815 1.1 lukem k = ic->ic_nw_keys;
1816 1.1 lukem for (i = 0; i < IEEE80211_WEP_NKID; i++, k++) {
1817 1.1 lukem if (k->wk_len == 0)
1818 1.1 lukem continue;
1819 1.1 lukem
1820 1.1 lukem wepkey.idx = i;
1821 1.1 lukem wepkey.len = k->wk_len;
1822 1.1 lukem bzero(wepkey.key, sizeof wepkey.key);
1823 1.1 lukem bcopy(k->wk_key, wepkey.key, k->wk_len);
1824 1.8 lukem DPRINTF(("Setting wep key index %d len %d\n",
1825 1.1 lukem wepkey.idx, wepkey.len));
1826 1.8 lukem error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
1827 1.1 lukem sizeof wepkey);
1828 1.1 lukem if (error != 0)
1829 1.1 lukem return error;
1830 1.1 lukem }
1831 1.1 lukem
1832 1.1 lukem data = htole32(ic->ic_wep_txkey);
1833 1.1 lukem DPRINTF(("Setting adapter tx key index to %u\n", data));
1834 1.8 lukem error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
1835 1.1 lukem sizeof data);
1836 1.1 lukem if (error != 0)
1837 1.1 lukem return error;
1838 1.1 lukem }
1839 1.1 lukem
1840 1.1 lukem data = htole32((sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) ? 0x8 : 0);
1841 1.1 lukem DPRINTF(("Setting adapter wep flags to 0x%x\n", data));
1842 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
1843 1.1 lukem if (error != 0)
1844 1.1 lukem return error;
1845 1.1 lukem
1846 1.8 lukem if (ic->ic_opmode == IEEE80211_M_IBSS ||
1847 1.1 lukem ic->ic_opmode == IEEE80211_M_HOSTAP) {
1848 1.1 lukem data = htole32(ic->ic_lintval);
1849 1.1 lukem DPRINTF(("Setting adapter beacon interval to %u\n", data));
1850 1.8 lukem error = ipw_cmd(sc, IPW_CMD_SET_BEACON_INTERVAL, &data,
1851 1.1 lukem sizeof data);
1852 1.1 lukem if (error != 0)
1853 1.1 lukem return error;
1854 1.1 lukem }
1855 1.1 lukem
1856 1.1 lukem options.flags = htole32(0);
1857 1.1 lukem options.channels = htole32(0x3fff); /* scan channels 1-14 */
1858 1.1 lukem error = ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &options, sizeof options);
1859 1.1 lukem if (error != 0)
1860 1.1 lukem return error;
1861 1.1 lukem
1862 1.1 lukem /* finally, enable adapter (start scanning for an access point) */
1863 1.1 lukem DPRINTF(("Enabling adapter\n"));
1864 1.1 lukem error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1865 1.1 lukem if (error != 0)
1866 1.1 lukem return error;
1867 1.1 lukem
1868 1.1 lukem return 0;
1869 1.1 lukem }
1870 1.1 lukem
1871 1.8 lukem static int
1872 1.1 lukem ipw_init(struct ifnet *ifp)
1873 1.1 lukem {
1874 1.1 lukem struct ipw_softc *sc = ifp->if_softc;
1875 1.1 lukem struct ieee80211com *ic = &sc->sc_ic;
1876 1.1 lukem
1877 1.1 lukem /* exit immediately if firmware has not been ioctl'd */
1878 1.1 lukem if (!(sc->flags & IPW_FLAG_FW_INITED)) {
1879 1.1 lukem ifp->if_flags &= ~IFF_UP;
1880 1.1 lukem return EIO;
1881 1.1 lukem }
1882 1.1 lukem
1883 1.1 lukem ipw_stop(ifp, 0);
1884 1.1 lukem
1885 1.1 lukem if (ipw_config(sc) != 0) {
1886 1.1 lukem aprint_error("%s: device configuration failed\n",
1887 1.1 lukem sc->sc_dev.dv_xname);
1888 1.1 lukem goto fail;
1889 1.1 lukem }
1890 1.1 lukem
1891 1.1 lukem ifp->if_flags &= ~IFF_OACTIVE;
1892 1.1 lukem ifp->if_flags |= IFF_RUNNING;
1893 1.1 lukem
1894 1.1 lukem ic->ic_bss->ni_chan = ic->ic_channels;
1895 1.1 lukem
1896 1.1 lukem return 0;
1897 1.1 lukem
1898 1.1 lukem fail: ipw_stop(ifp, 0);
1899 1.1 lukem
1900 1.1 lukem return EIO;
1901 1.1 lukem }
1902 1.1 lukem
1903 1.1 lukem static void
1904 1.1 lukem ipw_stop(struct ifnet *ifp, int disable)
1905 1.1 lukem {
1906 1.1 lukem struct ipw_softc *sc = ifp->if_softc;
1907 1.1 lukem struct ieee80211com *ic = &sc->sc_ic;
1908 1.1 lukem
1909 1.1 lukem if (ifp->if_flags & IFF_RUNNING) {
1910 1.1 lukem DPRINTF(("Disabling adapter\n"));
1911 1.1 lukem ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1912 1.1 lukem }
1913 1.1 lukem
1914 1.1 lukem ifp->if_timer = 0;
1915 1.1 lukem ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1916 1.1 lukem
1917 1.1 lukem ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1918 1.1 lukem }
1919 1.1 lukem
1920 1.1 lukem static void
1921 1.8 lukem ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, u_int8_t *datap,
1922 1.1 lukem bus_size_t count)
1923 1.1 lukem {
1924 1.1 lukem for (; count > 0; offset++, datap++, count--) {
1925 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
1926 1.1 lukem *datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
1927 1.1 lukem }
1928 1.1 lukem }
1929 1.1 lukem
1930 1.1 lukem static void
1931 1.8 lukem ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, u_int8_t *datap,
1932 1.1 lukem bus_size_t count)
1933 1.1 lukem {
1934 1.1 lukem for (; count > 0; offset++, datap++, count--) {
1935 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
1936 1.1 lukem CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
1937 1.1 lukem }
1938 1.1 lukem }
1939 1.1 lukem
1940 1.1 lukem static void
1941 1.1 lukem ipw_zero_mem_4(struct ipw_softc *sc, bus_size_t offset, bus_size_t count)
1942 1.1 lukem {
1943 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, offset);
1944 1.1 lukem while (count-- > 0)
1945 1.1 lukem CSR_WRITE_4(sc, IPW_CSR_AUTOINC_DATA, 0);
1946 1.1 lukem }
1947 1.1 lukem
1948