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