if_iwi.c revision 1.18 1 /* $NetBSD: if_iwi.c,v 1.18 2005/08/30 21:14:29 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2004, 2005
5 * Damien Bergamini <damien.bergamini (at) free.fr>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: if_iwi.c,v 1.18 2005/08/30 21:14:29 skrll Exp $");
32
33 /*-
34 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
36 */
37
38 #include "bpfilter.h"
39
40 #include <sys/param.h>
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43 #include <sys/mbuf.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/conf.h>
49
50 #include <machine/bus.h>
51 #include <machine/endian.h>
52 #include <machine/intr.h>
53
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/pcidevs.h>
57
58 #if NBPFILTER > 0
59 #include <net/bpf.h>
60 #endif
61 #include <net/if.h>
62 #include <net/if_arp.h>
63 #include <net/if_dl.h>
64 #include <net/if_ether.h>
65 #include <net/if_media.h>
66 #include <net/if_types.h>
67
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_radiotap.h>
70
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip.h>
75
76 #include <crypto/arc4/arc4.h>
77
78 #include <dev/pci/if_iwireg.h>
79 #include <dev/pci/if_iwivar.h>
80
81 static const struct ieee80211_rateset iwi_rateset_11a =
82 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
83
84 static const struct ieee80211_rateset iwi_rateset_11b =
85 { 4, { 2, 4, 11, 22 } };
86
87 static const struct ieee80211_rateset iwi_rateset_11g =
88 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
89
90 static int iwi_match(struct device *, struct cfdata *, void *);
91 static void iwi_attach(struct device *, struct device *, void *);
92 static int iwi_detach(struct device *, int);
93
94 static void iwi_shutdown(void *);
95 static int iwi_suspend(struct iwi_softc *);
96 static int iwi_resume(struct iwi_softc *);
97 static void iwi_powerhook(int, void *);
98
99 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
100 int);
101 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
102 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
103 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
104 int);
105 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
106 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
107 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
108 int);
109 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
110 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
111
112 static int iwi_media_change(struct ifnet *);
113 static void iwi_media_status(struct ifnet *, struct ifmediareq *);
114 static u_int16_t iwi_read_prom_word(struct iwi_softc *, u_int8_t);
115 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
116 static void iwi_fix_channel(struct ieee80211com *, struct mbuf *);
117 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
118 struct iwi_frame *);
119 static void iwi_notification_intr(struct iwi_softc *, struct iwi_rx_data *,
120 struct iwi_notif *);
121 static void iwi_rx_intr(struct iwi_softc *);
122 static void iwi_tx_intr(struct iwi_softc *);
123 static int iwi_intr(void *);
124 static int iwi_cmd(struct iwi_softc *, u_int8_t, void *, u_int8_t, int);
125 static int iwi_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *);
126 static void iwi_start(struct ifnet *);
127 static void iwi_watchdog(struct ifnet *);
128 static int iwi_get_table0(struct iwi_softc *, u_int32_t *);
129 static int iwi_get_radio(struct iwi_softc *, int *);
130 static int iwi_ioctl(struct ifnet *, u_long, caddr_t);
131 static void iwi_stop_master(struct iwi_softc *);
132 static int iwi_reset(struct iwi_softc *);
133 static int iwi_load_ucode(struct iwi_softc *, void *, int);
134 static int iwi_load_firmware(struct iwi_softc *, void *, int);
135 static int iwi_cache_firmware(struct iwi_softc *, void *);
136 static void iwi_free_firmware(struct iwi_softc *);
137 static int iwi_config(struct iwi_softc *);
138 static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
139 static int iwi_scan(struct iwi_softc *);
140 static int iwi_auth_and_assoc(struct iwi_softc *);
141 static int iwi_init(struct ifnet *);
142 static void iwi_stop(struct ifnet *, int);
143
144 static __inline u_int8_t
145 MEM_READ_1(struct iwi_softc *sc, u_int32_t addr)
146 {
147 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
148 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
149 }
150
151 static __inline u_int32_t
152 MEM_READ_4(struct iwi_softc *sc, u_int32_t addr)
153 {
154 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
155 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
156 }
157
158 #ifdef IWI_DEBUG
159 #define DPRINTF(x) if (iwi_debug > 0) printf x
160 #define DPRINTFN(n, x) if (iwi_debug >= (n)) printf x
161 int iwi_debug = 0;
162 #else
163 #define DPRINTF(x)
164 #define DPRINTFN(n, x)
165 #endif
166
167 CFATTACH_DECL(iwi, sizeof (struct iwi_softc), iwi_match, iwi_attach,
168 iwi_detach, NULL);
169
170 static int
171 iwi_match(struct device *parent, struct cfdata *match, void *aux)
172 {
173 struct pci_attach_args *pa = aux;
174
175 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
176 return 0;
177
178 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2200BG ||
179 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2225BG ||
180 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
181 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2)
182 return 1;
183
184 return 0;
185 }
186
187 /* Base Address Register */
188 #define IWI_PCI_BAR0 0x10
189
190 static void
191 iwi_attach(struct device *parent, struct device *self, void *aux)
192 {
193 struct iwi_softc *sc = (struct iwi_softc *)self;
194 struct ieee80211com *ic = &sc->sc_ic;
195 struct ifnet *ifp = &sc->sc_if;
196 struct pci_attach_args *pa = aux;
197 const char *intrstr;
198 char devinfo[256];
199 bus_space_tag_t memt;
200 bus_space_handle_t memh;
201 bus_addr_t base;
202 pci_intr_handle_t ih;
203 pcireg_t data;
204 u_int16_t val;
205 int error, revision, i;
206
207 sc->sc_pct = pa->pa_pc;
208 sc->sc_pcitag = pa->pa_tag;
209
210 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
211 revision = PCI_REVISION(pa->pa_class);
212 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
213
214 /* clear device specific PCI configuration register 0x41 */
215 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
216 data &= ~0x0000ff00;
217 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
218
219 /* enable bus-mastering */
220 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
221 data |= PCI_COMMAND_MASTER_ENABLE;
222 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
223
224 /* map the register window */
225 error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
226 PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz);
227 if (error != 0) {
228 aprint_error("%s: could not map memory space\n",
229 sc->sc_dev.dv_xname);
230 return;
231 }
232
233 sc->sc_st = memt;
234 sc->sc_sh = memh;
235 sc->sc_dmat = pa->pa_dmat;
236
237 /* disable interrupts */
238 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
239
240 if (pci_intr_map(pa, &ih) != 0) {
241 aprint_error("%s: could not map interrupt\n",
242 sc->sc_dev.dv_xname);
243 return;
244 }
245
246 intrstr = pci_intr_string(sc->sc_pct, ih);
247 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwi_intr, sc);
248 if (sc->sc_ih == NULL) {
249 aprint_error("%s: could not establish interrupt",
250 sc->sc_dev.dv_xname);
251 if (intrstr != NULL)
252 aprint_error(" at %s", intrstr);
253 aprint_error("\n");
254 return;
255 }
256 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
257
258 if (iwi_reset(sc) != 0) {
259 aprint_error("%s: could not reset adapter\n",
260 sc->sc_dev.dv_xname);
261 return;
262 }
263
264 /*
265 * Allocate rings.
266 */
267 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
268 aprint_error("%s: could not allocate command ring\n",
269 sc->sc_dev.dv_xname);
270 goto fail;
271 }
272
273 if (iwi_alloc_tx_ring(sc, &sc->txq, IWI_TX_RING_COUNT) != 0) {
274 aprint_error("%s: could not allocate Tx ring\n",
275 sc->sc_dev.dv_xname);
276 goto fail;
277 }
278
279 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
280 aprint_error("%s: could not allocate Rx ring\n",
281 sc->sc_dev.dv_xname);
282 goto fail;
283 }
284
285 ic->ic_ifp = ifp;
286 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
287 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
288 ic->ic_state = IEEE80211_S_INIT;
289
290 /* set device capabilities */
291 ic->ic_caps = IEEE80211_C_WPA | IEEE80211_C_PMGT | IEEE80211_C_TXPMGT |
292 IEEE80211_C_SHPREAMBLE | IEEE80211_C_MONITOR;
293
294 /* read MAC address from EEPROM */
295 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
296 ic->ic_myaddr[0] = val >> 8;
297 ic->ic_myaddr[1] = val & 0xff;
298 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
299 ic->ic_myaddr[2] = val >> 8;
300 ic->ic_myaddr[3] = val & 0xff;
301 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
302 ic->ic_myaddr[4] = val >> 8;
303 ic->ic_myaddr[5] = val & 0xff;
304
305 aprint_normal("%s: 802.11 address %s\n", sc->sc_dev.dv_xname,
306 ether_sprintf(ic->ic_myaddr));
307
308
309 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
310 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2) {
311 /* set supported .11a rates (2915ABG only) */
312 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
313
314 /* set supported .11a channels */
315 for (i = 36; i <= 64; i += 4) {
316 ic->ic_channels[i].ic_freq =
317 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
318 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
319 }
320 for (i = 149; i <= 165; i += 4) {
321 ic->ic_channels[i].ic_freq =
322 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
323 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
324 }
325 }
326
327 /* set supported .11b and .11g rates */
328 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
329 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
330
331 /* set supported .11b and .11g channels (1 through 14) */
332 for (i = 1; i <= 14; i++) {
333 ic->ic_channels[i].ic_freq =
334 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
335 ic->ic_channels[i].ic_flags =
336 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
337 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
338 }
339
340 ifp->if_softc = sc;
341 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
342 ifp->if_init = iwi_init;
343 ifp->if_stop = iwi_stop;
344 ifp->if_ioctl = iwi_ioctl;
345 ifp->if_start = iwi_start;
346 ifp->if_watchdog = iwi_watchdog;
347 IFQ_SET_READY(&ifp->if_snd);
348 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
349
350 if_attach(ifp);
351 ieee80211_ifattach(ic);
352 /* override state transition machine */
353 sc->sc_newstate = ic->ic_newstate;
354 ic->ic_newstate = iwi_newstate;
355 ieee80211_media_init(ic, iwi_media_change, iwi_media_status);
356
357 #if NBPFILTER > 0
358 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
359 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
360
361 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
362 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
363 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
364
365 sc->sc_txtap_len = sizeof sc->sc_txtapu;
366 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
367 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
368 #endif
369
370 /*
371 * Make sure the interface is shutdown during reboot.
372 */
373 sc->sc_sdhook = shutdownhook_establish(iwi_shutdown, sc);
374 if (sc->sc_sdhook == NULL)
375 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
376 sc->sc_dev.dv_xname);
377 sc->sc_powerhook = powerhook_establish(iwi_powerhook, sc);
378 if (sc->sc_powerhook == NULL)
379 printf("%s: WARNING: unable to establish power hook\n",
380 sc->sc_dev.dv_xname);
381
382 ieee80211_announce(ic);
383 /*
384 * Add a few sysctl knobs.
385 * XXX: Not yet.
386 */
387 sc->dwelltime = 100;
388 sc->bluetooth = 1;
389 sc->antenna = 0;
390
391 return;
392
393 fail: iwi_detach(self, 0);
394 }
395
396 static int
397 iwi_detach(struct device* self, int flags)
398 {
399 struct iwi_softc *sc = (struct iwi_softc *)self;
400 struct ifnet *ifp = &sc->sc_if;
401
402 iwi_stop(ifp, 1);
403 iwi_free_firmware(sc);
404
405 #if NBPFILTER > 0
406 bpfdetach(ifp);
407 #endif
408 ieee80211_ifdetach(&sc->sc_ic);
409 if_detach(ifp);
410
411 iwi_free_cmd_ring(sc, &sc->cmdq);
412 iwi_free_tx_ring(sc, &sc->txq);
413 iwi_free_rx_ring(sc, &sc->rxq);
414
415 if (sc->sc_ih != NULL) {
416 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
417 sc->sc_ih = NULL;
418 }
419
420 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
421
422 powerhook_disestablish(sc->sc_powerhook);
423 shutdownhook_disestablish(sc->sc_sdhook);
424
425 return 0;
426 }
427
428 static int
429 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring,
430 int count)
431 {
432 int error, nsegs;
433
434 ring->count = count;
435 ring->queued = 0;
436 ring->cur = ring->next = 0;
437
438 /*
439 * Allocate and map command ring
440 */
441 error = bus_dmamap_create(sc->sc_dmat,
442 sizeof (struct iwi_cmd_desc) * count, 1,
443 sizeof (struct iwi_cmd_desc) * count, 0,
444 BUS_DMA_NOWAIT, &ring->desc_map);
445 if (error != 0) {
446 aprint_error("%s: could not create command ring DMA map\n",
447 sc->sc_dev.dv_xname);
448 goto fail;
449 }
450
451 error = bus_dmamem_alloc(sc->sc_dmat,
452 sizeof (struct iwi_cmd_desc) * count, PAGE_SIZE, 0,
453 &sc->cmdq.desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
454 if (error != 0) {
455 aprint_error("%s: could not allocate command ring DMA memory\n",
456 sc->sc_dev.dv_xname);
457 goto fail;
458 }
459
460 error = bus_dmamem_map(sc->sc_dmat, &sc->cmdq.desc_seg, nsegs,
461 sizeof (struct iwi_cmd_desc) * count,
462 (caddr_t *)&sc->cmdq.desc, BUS_DMA_NOWAIT);
463 if (error != 0) {
464 aprint_error("%s: could not map command ring DMA memory\n",
465 sc->sc_dev.dv_xname);
466 goto fail;
467 }
468
469 error = bus_dmamap_load(sc->sc_dmat, sc->cmdq.desc_map, sc->cmdq.desc,
470 sizeof (struct iwi_cmd_desc) * count, NULL,
471 BUS_DMA_NOWAIT);
472 if (error != 0) {
473 aprint_error("%s: could not load command ring DMA map\n",
474 sc->sc_dev.dv_xname);
475 goto fail;
476 }
477
478 memset(sc->cmdq.desc, 0,
479 sizeof (struct iwi_cmd_desc) * count);
480
481 return 0;
482
483 fail: iwi_free_cmd_ring(sc, ring);
484 return error;
485 }
486
487 static void
488 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
489 {
490 ring->queued = 0;
491 ring->cur = ring->next = 0;
492 }
493
494 static void
495 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
496 {
497 if (ring->desc_map != NULL) {
498 if (ring->desc != NULL) {
499 bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
500 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
501 sizeof (struct iwi_cmd_desc) * ring->count);
502 bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
503 }
504 bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
505 }
506 }
507
508 static int
509 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring,
510 int count)
511 {
512 int i, error, nsegs;
513
514 ring->count = count;
515 ring->queued = 0;
516 ring->cur = ring->next = 0;
517
518 /*
519 * Allocate and map Tx ring
520 */
521 error = bus_dmamap_create(sc->sc_dmat,
522 sizeof (struct iwi_tx_desc) * count, 1,
523 sizeof (struct iwi_tx_desc) * count, 0, BUS_DMA_NOWAIT,
524 &ring->desc_map);
525 if (error != 0) {
526 aprint_error("%s: could not create tx ring DMA map\n",
527 sc->sc_dev.dv_xname);
528 goto fail;
529 }
530
531 error = bus_dmamem_alloc(sc->sc_dmat,
532 sizeof (struct iwi_tx_desc) * count, PAGE_SIZE, 0,
533 &ring->desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
534 if (error != 0) {
535 aprint_error("%s: could not allocate tx ring DMA memory\n",
536 sc->sc_dev.dv_xname);
537 goto fail;
538 }
539
540 error = bus_dmamem_map(sc->sc_dmat, &ring->desc_seg, nsegs,
541 sizeof (struct iwi_tx_desc) * count,
542 (caddr_t *)&ring->desc, BUS_DMA_NOWAIT);
543 if (error != 0) {
544 aprint_error("%s: could not map tx ring DMA memory\n",
545 sc->sc_dev.dv_xname);
546 goto fail;
547 }
548
549 error = bus_dmamap_load(sc->sc_dmat, ring->desc_map, ring->desc,
550 sizeof (struct iwi_tx_desc) * count, NULL,
551 BUS_DMA_NOWAIT);
552 if (error != 0) {
553 aprint_error("%s: could not load tx ring DMA map\n",
554 sc->sc_dev.dv_xname);
555 goto fail;
556 }
557
558 memset(ring->desc, 0, sizeof (struct iwi_tx_desc) * count);
559
560 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
561 M_NOWAIT | M_ZERO);
562 if (ring->data == NULL) {
563 aprint_error("%s: could not allocate soft data\n",
564 sc->sc_dev.dv_xname);
565 error = ENOMEM;
566 goto fail;
567 }
568
569 /*
570 * Allocate Tx buffers DMA maps
571 */
572 for (i = 0; i < count; i++) {
573 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, IWI_MAX_NSEG,
574 MCLBYTES, 0, BUS_DMA_NOWAIT, &ring->data[i].map);
575 if (error != 0) {
576 aprint_error("%s: could not create tx buf DMA map",
577 sc->sc_dev.dv_xname);
578 goto fail;
579 }
580 }
581 return 0;
582
583 fail: iwi_free_tx_ring(sc, ring);
584 return error;
585 }
586
587 static void
588 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
589 {
590 struct iwi_tx_data *data;
591 int i;
592
593 for (i = 0; i < ring->count; i++) {
594 data = &ring->data[i];
595
596 if (data->m != NULL) {
597 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
598 MCLBYTES, BUS_DMASYNC_POSTWRITE);
599 bus_dmamap_unload(sc->sc_dmat, data->map);
600 m_freem(data->m);
601 data->m = NULL;
602 }
603
604 if (data->ni != NULL) {
605 ieee80211_free_node(data->ni);
606 data->ni = NULL;
607 }
608 }
609
610 ring->queued = 0;
611 ring->cur = ring->next = 0;
612 }
613
614 static void
615 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
616 {
617 int i;
618
619 if (ring->desc_map != NULL) {
620 if (ring->desc != NULL) {
621 bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
622 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
623 sizeof (struct iwi_tx_desc) * ring->count);
624 bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
625 }
626 bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
627 }
628
629 for (i = 0; i < ring->count; i++) {
630 if (ring->data[i].m != NULL) {
631 bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
632 m_freem(ring->data[i].m);
633 }
634 bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
635 }
636 }
637
638 static int
639 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring,
640 int count)
641 {
642 int i, error;
643
644 ring->count = count;
645 ring->cur = 0;
646
647 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
648 M_NOWAIT | M_ZERO);
649 if (ring->data == NULL) {
650 aprint_error("%s: could not allocate soft data\n",
651 sc->sc_dev.dv_xname);
652 error = ENOMEM;
653 goto fail;
654 }
655
656 /*
657 * Allocate and map Rx buffers
658 */
659 for (i = 0; i < count; i++) {
660
661 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
662 0, BUS_DMA_NOWAIT, &ring->data[i].map);
663 if (error != 0) {
664 aprint_error("%s: could not create rx buf DMA map",
665 sc->sc_dev.dv_xname);
666 goto fail;
667 }
668
669 MGETHDR(ring->data[i].m, M_DONTWAIT, MT_DATA);
670 if (ring->data[i].m == NULL) {
671 aprint_error("%s: could not allocate rx mbuf\n",
672 sc->sc_dev.dv_xname);
673 error = ENOMEM;
674 goto fail;
675 }
676
677 MCLGET(ring->data[i].m, M_DONTWAIT);
678 if (!(ring->data[i].m->m_flags & M_EXT)) {
679 m_freem(ring->data[i].m);
680 aprint_error("%s: could not allocate rx mbuf cluster\n",
681 sc->sc_dev.dv_xname);
682 error = ENOMEM;
683 goto fail;
684 }
685
686 error = bus_dmamap_load(sc->sc_dmat, ring->data[i].map,
687 mtod(ring->data[i].m, void *), MCLBYTES, NULL,
688 BUS_DMA_NOWAIT);
689 if (error != 0) {
690 aprint_error("%s: could not load rx buffer DMA map\n",
691 sc->sc_dev.dv_xname);
692 goto fail;
693 }
694 }
695
696 return 0;
697
698 fail: iwi_free_rx_ring(sc, ring);
699 return error;
700 }
701
702 static void
703 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
704 {
705 ring->cur = 0;
706 }
707
708 static void
709 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
710 {
711 int i;
712
713 for (i = 0; i < ring->count; i++) {
714 if (ring->data[i].m != NULL) {
715 bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
716 m_freem(ring->data[i].m);
717 }
718 bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
719 }
720 }
721
722 static void
723 iwi_shutdown(void *arg)
724 {
725 struct iwi_softc *sc = (struct iwi_softc *)arg;
726 struct ifnet *ifp = sc->sc_ic.ic_ifp;
727
728 iwi_stop(ifp, 1);
729 }
730
731 static int
732 iwi_suspend(struct iwi_softc *sc)
733 {
734 struct ifnet *ifp = sc->sc_ic.ic_ifp;
735
736 iwi_stop(ifp, 1);
737
738 return 0;
739 }
740
741 static int
742 iwi_resume(struct iwi_softc *sc)
743 {
744 struct ifnet *ifp = sc->sc_ic.ic_ifp;
745 pcireg_t data;
746
747 /* clear device specific PCI configuration register 0x41 */
748 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
749 data &= ~0x0000ff00;
750 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
751
752 if (ifp->if_flags & IFF_UP) {
753 iwi_init(ifp);
754 if (ifp->if_flags & IFF_RUNNING)
755 iwi_start(ifp);
756 }
757
758 return 0;
759 }
760
761 static void
762 iwi_powerhook(int why, void *arg)
763 {
764 struct iwi_softc *sc = arg;
765 int s;
766
767 s = splnet();
768 switch (why) {
769 case PWR_SUSPEND:
770 case PWR_STANDBY:
771 iwi_suspend(sc);
772 break;
773 case PWR_RESUME:
774 iwi_resume(sc);
775 break;
776 case PWR_SOFTSUSPEND:
777 case PWR_SOFTSTANDBY:
778 case PWR_SOFTRESUME:
779 break;
780 }
781 splx(s);
782 }
783
784 static int
785 iwi_media_change(struct ifnet *ifp)
786 {
787 int error;
788
789 error = ieee80211_media_change(ifp);
790 if (error != ENETRESET)
791 return error;
792
793 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
794 iwi_init(ifp);
795
796 return 0;
797 }
798
799 static void
800 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
801 {
802 struct iwi_softc *sc = ifp->if_softc;
803 struct ieee80211com *ic = &sc->sc_ic;
804 #define N(a) (sizeof (a) / sizeof (a[0]))
805 static const struct {
806 u_int32_t val;
807 int rate;
808 } rates[] = {
809 { IWI_RATE_DS1, 2 },
810 { IWI_RATE_DS2, 4 },
811 { IWI_RATE_DS5, 11 },
812 { IWI_RATE_DS11, 22 },
813 { IWI_RATE_OFDM6, 12 },
814 { IWI_RATE_OFDM9, 18 },
815 { IWI_RATE_OFDM12, 24 },
816 { IWI_RATE_OFDM18, 36 },
817 { IWI_RATE_OFDM24, 48 },
818 { IWI_RATE_OFDM36, 72 },
819 { IWI_RATE_OFDM48, 96 },
820 { IWI_RATE_OFDM54, 108 },
821 };
822 u_int32_t val;
823 int rate, i;
824
825 imr->ifm_status = IFM_AVALID;
826 imr->ifm_active = IFM_IEEE80211;
827 if (ic->ic_state == IEEE80211_S_RUN)
828 imr->ifm_status |= IFM_ACTIVE;
829
830 /* read current transmission rate from adapter */
831 val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
832
833 /* convert rate to 802.11 rate */
834 for (i = 0; i < N(rates) && rates[i].val != val; i++);
835 rate = (i < N(rates)) ? rates[i].rate : 0;
836
837 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
838 switch (ic->ic_opmode) {
839 case IEEE80211_M_STA:
840 break;
841
842 case IEEE80211_M_IBSS:
843 imr->ifm_active |= IFM_IEEE80211_ADHOC;
844 break;
845
846 case IEEE80211_M_MONITOR:
847 imr->ifm_active |= IFM_IEEE80211_MONITOR;
848 break;
849
850 case IEEE80211_M_AHDEMO:
851 case IEEE80211_M_HOSTAP:
852 /* should not get there */
853 break;
854 }
855 #undef N
856 }
857
858 static int
859 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
860 {
861 struct iwi_softc *sc = ic->ic_ifp->if_softc;
862
863 switch (nstate) {
864 case IEEE80211_S_SCAN:
865 if (sc->flags & IWI_FLAG_SCANNING)
866 break;
867
868 ieee80211_node_table_reset(&ic->ic_scan);
869 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
870 sc->flags |= IWI_FLAG_SCANNING;
871 iwi_scan(sc);
872 break;
873
874 case IEEE80211_S_AUTH:
875 iwi_auth_and_assoc(sc);
876 break;
877
878 case IEEE80211_S_RUN:
879 if (ic->ic_opmode == IEEE80211_M_IBSS)
880 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
881 else if (ic->ic_opmode == IEEE80211_M_MONITOR)
882 iwi_set_chan(sc, ic->ic_ibss_chan);
883
884 return (*sc->sc_newstate)(ic, nstate,
885 IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
886
887 case IEEE80211_S_ASSOC:
888 break;
889
890 case IEEE80211_S_INIT:
891 sc->flags &= ~IWI_FLAG_SCANNING;
892 break;
893 }
894
895 ic->ic_state = nstate;
896 return 0;
897 }
898
899 /*
900 * Read 16 bits at address 'addr' from the serial EEPROM.
901 * DON'T PLAY WITH THIS CODE UNLESS YOU KNOW *EXACTLY* WHAT YOU'RE DOING!
902 */
903 static u_int16_t
904 iwi_read_prom_word(struct iwi_softc *sc, u_int8_t addr)
905 {
906 u_int32_t tmp;
907 u_int16_t val;
908 int n;
909
910 /* Clock C once before the first command */
911 IWI_EEPROM_CTL(sc, 0);
912 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
913 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
914 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
915
916 /* Write start bit (1) */
917 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
918 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
919
920 /* Write READ opcode (10) */
921 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
922 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
923 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
924 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
925
926 /* Write address A7-A0 */
927 for (n = 7; n >= 0; n--) {
928 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
929 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
930 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
931 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
932 }
933
934 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
935
936 /* Read data Q15-Q0 */
937 val = 0;
938 for (n = 15; n >= 0; n--) {
939 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
940 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
941 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
942 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
943 }
944
945 IWI_EEPROM_CTL(sc, 0);
946
947 /* Clear Chip Select and clock C */
948 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
949 IWI_EEPROM_CTL(sc, 0);
950 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
951
952 return be16toh(val);
953 }
954
955 /*
956 * XXX: Hack to set the current channel to the value advertised in beacons or
957 * probe responses. Only used during AP detection.
958 */
959 static void
960 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
961 {
962 struct ieee80211_frame *wh;
963 u_int8_t subtype;
964 u_int8_t *frm, *efrm;
965
966 wh = mtod(m, struct ieee80211_frame *);
967
968 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
969 return;
970
971 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
972
973 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
974 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
975 return;
976
977 frm = (u_int8_t *)(wh + 1);
978 efrm = mtod(m, u_int8_t *) + m->m_len;
979
980 frm += 12; /* skip tstamp, bintval and capinfo fields */
981 while (frm < efrm) {
982 if (*frm == IEEE80211_ELEMID_DSPARMS)
983 #if IEEE80211_CHAN_MAX < 255
984 if (frm[2] <= IEEE80211_CHAN_MAX)
985 #endif
986 ic->ic_bss->ni_chan = &ic->ic_channels[frm[2]];
987
988 frm += frm[1] + 2;
989 }
990 }
991
992 static void
993 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
994 struct iwi_frame *frame)
995 {
996 struct ieee80211com *ic = &sc->sc_ic;
997 struct ifnet *ifp = ic->ic_ifp;
998 struct mbuf *m;
999 struct ieee80211_frame *wh;
1000 struct ieee80211_node *ni;
1001 int error;
1002
1003 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
1004 le16toh(frame->len), frame->chan, frame->rssi_dbm));
1005
1006 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (struct iwi_hdr),
1007 sizeof (struct iwi_frame) + le16toh(frame->len),
1008 BUS_DMASYNC_POSTREAD);
1009
1010 if (le16toh(frame->len) < sizeof (struct ieee80211_frame) ||
1011 le16toh(frame->len) > MCLBYTES) {
1012 DPRINTF(("%s: bad frame length\n", sc->sc_dev.dv_xname));
1013 ifp->if_ierrors++;
1014 return;
1015 }
1016
1017 bus_dmamap_unload(sc->sc_dmat, data->map);
1018
1019 /* Finalize mbuf */
1020 m = data->m;
1021 m->m_pkthdr.rcvif = ifp;
1022 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1023 sizeof (struct iwi_frame) + le16toh(frame->len);
1024
1025 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1026
1027 if (ic->ic_state == IEEE80211_S_SCAN)
1028 iwi_fix_channel(ic, m);
1029
1030 #if NBPFILTER > 0
1031 if (sc->sc_drvbpf != NULL) {
1032 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1033
1034 tap->wr_flags = 0;
1035 tap->wr_rate = frame->rate;
1036 tap->wr_chan_freq =
1037 htole16(ic->ic_channels[frame->chan].ic_freq);
1038 tap->wr_chan_flags =
1039 htole16(ic->ic_channels[frame->chan].ic_flags);
1040 tap->wr_antsignal = frame->signal;
1041 tap->wr_antenna = frame->antenna;
1042
1043 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
1044 }
1045 #endif
1046
1047 wh = mtod(m, struct ieee80211_frame *);
1048 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1049
1050 /* Send the frame to the upper layer */
1051 ieee80211_input(ic, m, ni, frame->rssi_dbm, 0);
1052
1053 /* node is no longer needed */
1054 ieee80211_free_node(ni);
1055
1056 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
1057 if (data->m == NULL) {
1058 aprint_error("%s: could not allocate rx mbuf\n",
1059 sc->sc_dev.dv_xname);
1060 return;
1061 }
1062
1063 MCLGET(data->m, M_DONTWAIT);
1064 if (!(data->m->m_flags & M_EXT)) {
1065 aprint_error("%s: could not allocate rx mbuf cluster\n",
1066 sc->sc_dev.dv_xname);
1067 m_freem(data->m);
1068 data->m = NULL;
1069 return;
1070 }
1071
1072 error = bus_dmamap_load(sc->sc_dmat, data->map, mtod(data->m, void *),
1073 MCLBYTES, NULL, BUS_DMA_NOWAIT);
1074 if (error != 0) {
1075 aprint_error("%s: could not load rx buf DMA map\n",
1076 sc->sc_dev.dv_xname);
1077 m_freem(data->m);
1078 data->m = NULL;
1079 return;
1080 }
1081
1082 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, data->map->dm_segs[0].ds_addr);
1083 }
1084
1085 static void
1086 iwi_notification_intr(struct iwi_softc *sc, struct iwi_rx_data *buf,
1087 struct iwi_notif *notif)
1088 {
1089 struct ieee80211com *ic = &sc->sc_ic;
1090 struct iwi_notif_scan_channel *chan;
1091 struct iwi_notif_scan_complete *scan;
1092 struct iwi_notif_authentication *auth;
1093 struct iwi_notif_association *assoc;
1094
1095 bus_dmamap_sync(sc->sc_dmat, buf->map, sizeof (struct iwi_hdr),
1096 sizeof (struct iwi_notif) + le16toh(notif->len),
1097 BUS_DMASYNC_POSTREAD);
1098
1099 switch (notif->type) {
1100 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1101 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1102
1103 DPRINTFN(2, ("Scanning channel (%u)\n", chan->nchan));
1104 break;
1105
1106 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1107 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1108
1109 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1110 scan->status));
1111
1112 /* monitor mode uses scan to set the channel ... */
1113 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1114 sc->flags &= ~IWI_FLAG_SCANNING;
1115 ieee80211_end_scan(ic);
1116 } else
1117 iwi_set_chan(sc, ic->ic_ibss_chan);
1118 break;
1119
1120 case IWI_NOTIF_TYPE_AUTHENTICATION:
1121 auth = (struct iwi_notif_authentication *)(notif + 1);
1122
1123 DPRINTFN(2, ("Authentication (%u)\n", auth->state));
1124
1125 switch (auth->state) {
1126 case IWI_AUTHENTICATED:
1127 ieee80211_node_authorize(ic, ic->ic_bss);
1128 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1129 break;
1130
1131 case IWI_DEAUTHENTICATED:
1132 break;
1133
1134 default:
1135 aprint_error("%s: unknown authentication state %u\n",
1136 sc->sc_dev.dv_xname, auth->state);
1137 }
1138 break;
1139
1140 case IWI_NOTIF_TYPE_ASSOCIATION:
1141 assoc = (struct iwi_notif_association *)(notif + 1);
1142
1143 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
1144 assoc->status));
1145
1146 switch (assoc->state) {
1147 case IWI_AUTHENTICATED:
1148 /* re-association, do nothing */
1149 break;
1150
1151 case IWI_ASSOCIATED:
1152 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1153 break;
1154
1155 case IWI_DEASSOCIATED:
1156 ieee80211_begin_scan(ic, 1);
1157 break;
1158
1159 default:
1160 aprint_error("%s: unknown association state %u\n",
1161 sc->sc_dev.dv_xname, assoc->state);
1162 }
1163 break;
1164
1165 case IWI_NOTIF_TYPE_CALIBRATION:
1166 case IWI_NOTIF_TYPE_BEACON:
1167 case IWI_NOTIF_TYPE_NOISE:
1168 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1169 break;
1170
1171 default:
1172 aprint_error("%s: unknown notification type %u\n",
1173 sc->sc_dev.dv_xname, notif->type);
1174 }
1175 }
1176
1177 static void
1178 iwi_rx_intr(struct iwi_softc *sc)
1179 {
1180 struct iwi_rx_data *data;
1181 struct iwi_hdr *hdr;
1182 uint32_t hw;
1183
1184 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1185
1186 for (; sc->rxq.cur != hw;) {
1187 data = &sc->rxq.data[sc->rxq.cur];
1188
1189 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1190 sizeof (struct iwi_hdr), BUS_DMASYNC_POSTREAD);
1191
1192 hdr = mtod(data->m, struct iwi_hdr *);
1193
1194 switch (hdr->type) {
1195 case IWI_HDR_TYPE_FRAME:
1196 iwi_frame_intr(sc, data, sc->rxq.cur,
1197 (struct iwi_frame *)(hdr + 1));
1198 break;
1199
1200 case IWI_HDR_TYPE_NOTIF:
1201 iwi_notification_intr(sc, data,
1202 (struct iwi_notif *)(hdr + 1));
1203 break;
1204
1205 default:
1206 aprint_error("%s: unknown hdr type %u\n",
1207 sc->sc_dev.dv_xname, hdr->type);
1208 }
1209
1210 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1211
1212 sc->rxq.cur = (sc->rxq.cur + 1) % sc->rxq.count;
1213 }
1214
1215
1216 /* Tell the firmware what we have processed */
1217 hw = (hw == 0) ? sc->rxq.count - 1 : hw - 1;
1218 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1219 }
1220
1221 static void
1222 iwi_tx_intr(struct iwi_softc *sc)
1223 {
1224 struct ifnet *ifp = &sc->sc_if;
1225 struct iwi_tx_data *data;
1226 u_int32_t hw;
1227
1228 hw = CSR_READ_4(sc, IWI_CSR_TX1_RIDX);
1229
1230 for (; sc->txq.next != hw;) {
1231 data = &sc->txq.data[sc->txq.next];
1232
1233 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1234 MCLBYTES, BUS_DMASYNC_POSTWRITE);
1235 bus_dmamap_unload(sc->sc_dmat, data->map);
1236 m_freem(data->m);
1237 data->m = NULL;
1238 ieee80211_free_node(data->ni);
1239 data->ni = NULL;
1240
1241 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next));
1242
1243 ifp->if_opackets++;
1244
1245 sc->txq.queued--;
1246 sc->txq.next = (sc->txq.next + 1) % sc->txq.count;
1247 }
1248
1249 sc->sc_tx_timer = 0;
1250 ifp->if_flags &= ~IFF_OACTIVE;
1251
1252 /* Call start() since some buffer descriptors have been released */
1253 (*ifp->if_start)(ifp);
1254 }
1255
1256 static int
1257 iwi_intr(void *arg)
1258 {
1259 struct iwi_softc *sc = arg;
1260 u_int32_t r;
1261
1262 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff)
1263 return 0;
1264
1265 /* Disable interrupts */
1266 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1267
1268 if (r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)) {
1269 aprint_error("%s: fatal error\n", sc->sc_dev.dv_xname);
1270 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1271 iwi_stop(&sc->sc_if, 1);
1272 }
1273
1274 if (r & IWI_INTR_FW_INITED) {
1275 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1276 wakeup(sc);
1277 }
1278
1279 if (r & IWI_INTR_RADIO_OFF) {
1280 DPRINTF(("radio transmitter off\n"));
1281 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1282 iwi_stop(&sc->sc_if, 1);
1283 }
1284
1285 if (r & IWI_INTR_RX_DONE)
1286 iwi_rx_intr(sc);
1287
1288 if (r & IWI_INTR_CMD_DONE)
1289 wakeup(sc);
1290
1291 if (r & IWI_INTR_TX1_DONE)
1292 iwi_tx_intr(sc);
1293
1294 /* Acknowledge interrupts */
1295 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1296
1297 /* Re-enable interrupts */
1298 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1299
1300 return 1;
1301 }
1302
1303 static int
1304 iwi_cmd(struct iwi_softc *sc, u_int8_t type, void *data, u_int8_t len,
1305 int async)
1306 {
1307 struct iwi_cmd_desc *desc;
1308
1309 desc = &sc->cmdq.desc[sc->cmdq.cur];
1310
1311 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1312 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1313 desc->type = type;
1314 desc->len = len;
1315 memcpy(desc->data, data, len);
1316
1317 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
1318 sc->cmdq.cur * sizeof (struct iwi_cmd_desc),
1319 sizeof (struct iwi_cmd_desc), BUS_DMASYNC_PREWRITE);
1320
1321 DPRINTFN(2, ("sending command type=%u len=%u\n",
1322 type, len));
1323
1324 sc->cmdq.cur = (sc->cmdq.cur + 1) % sc->cmdq.count;
1325 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1326
1327 return async ? 0 : tsleep(sc, 0, "iwicmd", hz);
1328 }
1329
1330 static int
1331 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1332 {
1333 struct iwi_softc *sc = ifp->if_softc;
1334 struct ieee80211com *ic = &sc->sc_ic;
1335 struct ieee80211_frame wh;
1336 struct ieee80211_key *k;
1337 struct iwi_tx_data *data;
1338 struct iwi_tx_desc *desc;
1339 struct mbuf *mnew;
1340 int error, i;
1341
1342 (void)memcpy(&wh, mtod(m0, struct ieee80211_frame *), sizeof(wh));
1343 if (wh.i_fc[1] & IEEE80211_FC1_WEP) {
1344 k = ieee80211_crypto_encap(ic, ni, m0);
1345 if (k == NULL) {
1346 m_freem(m0);
1347 return ENOBUFS;
1348 }
1349 }
1350
1351 #if NBPFILTER > 0
1352 if (sc->sc_drvbpf != NULL) {
1353 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1354
1355 tap->wt_flags = 0;
1356 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1357 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1358
1359 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1360 }
1361 #endif
1362
1363 data = &sc->txq.data[sc->txq.cur];
1364 desc = &sc->txq.desc[sc->txq.cur];
1365
1366 /* trim IEEE802.11 header */
1367 m_adj(m0, sizeof (struct ieee80211_frame));
1368
1369 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, BUS_DMA_NOWAIT);
1370 if (error != 0 && error != EFBIG) {
1371 aprint_error("%s: could not map mbuf (error %d)\n",
1372 sc->sc_dev.dv_xname, error);
1373 m_freem(m0);
1374 return error;
1375 }
1376 if (error != 0) {
1377 /* too many fragments, linearize */
1378
1379 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1380 if (mnew == NULL) {
1381 m_freem(m0);
1382 return ENOMEM;
1383 }
1384
1385 M_COPY_PKTHDR(mnew, m0);
1386 MCLGET(mnew, M_DONTWAIT);
1387 if (!(mnew->m_flags & M_EXT)) {
1388 m_freem(m0);
1389 m_freem(mnew);
1390 return ENOMEM;
1391 }
1392
1393 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, caddr_t));
1394 m_freem(m0);
1395 mnew->m_len = mnew->m_pkthdr.len;
1396 m0 = mnew;
1397
1398 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1399 BUS_DMA_NOWAIT);
1400 if (error != 0) {
1401 aprint_error("%s: could not map mbuf (error %d)\n",
1402 sc->sc_dev.dv_xname, error);
1403 m_freem(m0);
1404 return error;
1405 }
1406 }
1407
1408 data->m = m0;
1409 data->ni = ni;
1410
1411 desc->hdr.type = IWI_HDR_TYPE_DATA;
1412 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1413 desc->cmd = IWI_DATA_CMD_TX;
1414 desc->len = htole16(m0->m_pkthdr.len);
1415 (void)memcpy(&desc->wh, &wh, sizeof (struct ieee80211_frame));
1416 desc->flags = 0;
1417 if (!IEEE80211_IS_MULTICAST(wh.i_addr1))
1418 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1419
1420 #if 0
1421 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1422 wh.i_fc[1] |= IEEE80211_FC1_WEP;
1423 desc->wep_txkey = ic->ic_crypto.cs_def_txkey;
1424 } else
1425 #endif
1426 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1427
1428 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1429 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1430
1431 desc->nseg = htole32(data->map->dm_nsegs);
1432 for (i = 0; i < data->map->dm_nsegs; i++) {
1433 desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr);
1434 desc->seg_len[i] = htole32(data->map->dm_segs[i].ds_len);
1435 }
1436
1437 bus_dmamap_sync(sc->sc_dmat, sc->txq.desc_map,
1438 sc->txq.cur * sizeof (struct iwi_tx_desc),
1439 sizeof (struct iwi_tx_desc), BUS_DMASYNC_PREWRITE);
1440
1441 bus_dmamap_sync(sc->sc_dmat, data->map, 0, MCLBYTES,
1442 BUS_DMASYNC_PREWRITE);
1443
1444 DPRINTFN(5, ("sending data frame len=%u nseg=%u\n",
1445 desc->len, desc->nseg));
1446
1447 /* Inform firmware about this new packet */
1448 sc->txq.queued++;
1449 sc->txq.cur = (sc->txq.cur + 1) % sc->txq.count;
1450 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur);
1451
1452 return 0;
1453 }
1454
1455 static void
1456 iwi_start(struct ifnet *ifp)
1457 {
1458 struct iwi_softc *sc = ifp->if_softc;
1459 struct ieee80211com *ic = &sc->sc_ic;
1460 struct mbuf *m0;
1461 struct ether_header *eh;
1462 struct ieee80211_node *ni;
1463
1464 if (ic->ic_state != IEEE80211_S_RUN)
1465 return;
1466
1467 for (;;) {
1468 IF_DEQUEUE(&ifp->if_snd, m0);
1469 if (m0 == NULL)
1470 break;
1471
1472 if (sc->txq.queued >= sc->txq.count - 4) {
1473 IF_PREPEND(&ifp->if_snd, m0);
1474 ifp->if_flags |= IFF_OACTIVE;
1475 break;
1476 }
1477
1478 if (m0->m_len < sizeof (struct ether_header) &&
1479 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL)
1480 continue;
1481
1482 #if NBPFILTER > 0
1483 if (ifp->if_bpf != NULL)
1484 bpf_mtap(ifp->if_bpf, m0);
1485 #endif
1486
1487 eh = mtod(m0, struct ether_header *);
1488 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1489 if (ni == NULL) {
1490 m_freem(m0);
1491 continue;
1492 }
1493
1494 #if NBPFILTER > 0
1495 if (ic->ic_rawbpf != NULL)
1496 bpf_mtap(ic->ic_rawbpf, m0);
1497 #endif
1498 m0 = ieee80211_encap(ic, m0, ni);
1499 if (m0 == NULL) {
1500 ieee80211_free_node(ni);
1501 continue;
1502 }
1503
1504 if (iwi_tx_start(ifp, m0, ni) != 0) {
1505 ieee80211_free_node(ni);
1506 ifp->if_oerrors++;
1507 break;
1508 }
1509
1510 /* start watchdog timer */
1511 sc->sc_tx_timer = 5;
1512 ifp->if_timer = 1;
1513 }
1514 }
1515
1516 static void
1517 iwi_watchdog(struct ifnet *ifp)
1518 {
1519 struct iwi_softc *sc = ifp->if_softc;
1520
1521 ifp->if_timer = 0;
1522
1523 if (sc->sc_tx_timer > 0) {
1524 if (--sc->sc_tx_timer == 0) {
1525 aprint_error("%s: device timeout\n",
1526 sc->sc_dev.dv_xname);
1527 ifp->if_oerrors++;
1528 ifp->if_flags &= ~IFF_UP;
1529 iwi_stop(ifp, 1);
1530 return;
1531 }
1532 ifp->if_timer = 1;
1533 }
1534
1535 ieee80211_watchdog(&sc->sc_ic);
1536 }
1537
1538 static int
1539 iwi_get_table0(struct iwi_softc *sc, u_int32_t *tbl)
1540 {
1541 u_int32_t size, buf[128];
1542
1543 if (!(sc->flags & IWI_FLAG_FW_INITED)) {
1544 memset(buf, 0, sizeof buf);
1545 return copyout(buf, tbl, sizeof buf);
1546 }
1547
1548 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
1549 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
1550
1551 return copyout(buf, tbl, sizeof buf);
1552 }
1553
1554 static int
1555 iwi_get_radio(struct iwi_softc *sc, int *ret)
1556 {
1557 int val;
1558
1559 val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0;
1560 return copyout(&val, ret, sizeof val);
1561 }
1562
1563 static int
1564 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1565 {
1566 struct iwi_softc *sc = ifp->if_softc;
1567 struct ifreq *ifr;
1568 int s, error = 0;
1569
1570 s = splnet();
1571
1572 switch (cmd) {
1573 case SIOCSIFFLAGS:
1574 if (ifp->if_flags & IFF_UP) {
1575 if (!(ifp->if_flags & IFF_RUNNING))
1576 iwi_init(ifp);
1577 } else {
1578 if (ifp->if_flags & IFF_RUNNING)
1579 iwi_stop(ifp, 1);
1580 }
1581 break;
1582
1583 case SIOCGTABLE0:
1584 ifr = (struct ifreq *)data;
1585 error = iwi_get_table0(sc, (u_int32_t *)ifr->ifr_data);
1586 break;
1587
1588 case SIOCGRADIO:
1589 ifr = (struct ifreq *)data;
1590 error = iwi_get_radio(sc, (int *)ifr->ifr_data);
1591 break;
1592
1593 case SIOCSLOADFW:
1594 /* only super-user can do that! */
1595 if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
1596 break;
1597
1598 ifr = (struct ifreq *)data;
1599 error = iwi_cache_firmware(sc, ifr->ifr_data);
1600 break;
1601
1602 case SIOCSKILLFW:
1603 /* only super-user can do that! */
1604 if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
1605 break;
1606
1607 ifp->if_flags &= ~IFF_UP;
1608 iwi_stop(ifp, 1);
1609 iwi_free_firmware(sc);
1610 break;
1611
1612 default:
1613 error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
1614 }
1615
1616 if (error == ENETRESET && cmd != SIOCADDMULTI) {
1617 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1618 (IFF_UP | IFF_RUNNING))
1619 iwi_init(ifp);
1620 error = 0;
1621 }
1622
1623 splx(s);
1624 return error;
1625 }
1626
1627 static void
1628 iwi_stop_master(struct iwi_softc *sc)
1629 {
1630 int ntries;
1631
1632 /* Disable interrupts */
1633 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1634
1635 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1636 for (ntries = 0; ntries < 5; ntries++) {
1637 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1638 break;
1639 DELAY(10);
1640 }
1641 if (ntries == 5)
1642 aprint_error("%s: timeout waiting for master\n",
1643 sc->sc_dev.dv_xname);
1644
1645 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1646 IWI_RST_PRINCETON_RESET);
1647
1648 sc->flags &= ~IWI_FLAG_FW_INITED;
1649 }
1650
1651 static int
1652 iwi_reset(struct iwi_softc *sc)
1653 {
1654 int i, ntries;
1655
1656 iwi_stop_master(sc);
1657
1658 /* Move adapter to D0 state */
1659 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1660 IWI_CTL_INIT);
1661
1662 /* Initialize Phase-Locked Level (PLL) */
1663 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
1664
1665 /* Wait for clock stabilization */
1666 for (ntries = 0; ntries < 1000; ntries++) {
1667 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
1668 break;
1669 DELAY(200);
1670 }
1671 if (ntries == 1000)
1672 return EIO;
1673
1674 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1675 IWI_RST_SW_RESET);
1676
1677 DELAY(10);
1678
1679 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1680 IWI_CTL_INIT);
1681
1682 /* Clear NIC memory */
1683 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
1684 for (i = 0; i < 0xc000; i++)
1685 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1686
1687 return 0;
1688 }
1689
1690 static int
1691 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
1692 {
1693 u_int16_t *w;
1694 int ntries, i;
1695
1696 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1697 IWI_RST_STOP_MASTER);
1698 for (ntries = 0; ntries < 5; ntries++) {
1699 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1700 break;
1701 DELAY(10);
1702 }
1703 if (ntries == 5) {
1704 aprint_error("%s: timeout waiting for master\n",
1705 sc->sc_dev.dv_xname);
1706 return EIO;
1707 }
1708
1709 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1710 DELAY(5000);
1711 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
1712 ~IWI_RST_PRINCETON_RESET);
1713 DELAY(5000);
1714 MEM_WRITE_4(sc, 0x3000e0, 0);
1715 DELAY(1000);
1716 MEM_WRITE_4(sc, 0x300004, 1);
1717 DELAY(1000);
1718 MEM_WRITE_4(sc, 0x300004, 0);
1719 DELAY(1000);
1720 MEM_WRITE_1(sc, 0x200000, 0x00);
1721 MEM_WRITE_1(sc, 0x200000, 0x40);
1722 DELAY(1000);
1723
1724 /* Adapter is buggy, we must set the address for each word */
1725 for (w = uc; size > 0; w++, size -= 2)
1726 MEM_WRITE_2(sc, 0x200010, *w);
1727
1728 MEM_WRITE_1(sc, 0x200000, 0x00);
1729 MEM_WRITE_1(sc, 0x200000, 0x80);
1730
1731 /* Wait until we get a response in the uc queue */
1732 for (ntries = 0; ntries < 100; ntries++) {
1733 if (MEM_READ_1(sc, 0x200000) & 1)
1734 break;
1735 DELAY(100);
1736 }
1737 if (ntries == 100) {
1738 aprint_error("%s: timeout waiting for ucode to initialize\n",
1739 sc->sc_dev.dv_xname);
1740 return EIO;
1741 }
1742
1743 /* Empty the uc queue or the firmware will not initialize properly */
1744 for (i = 0; i < 7; i++)
1745 MEM_READ_4(sc, 0x200004);
1746
1747 MEM_WRITE_1(sc, 0x200000, 0x00);
1748
1749 return 0;
1750 }
1751
1752 /* macro to handle unaligned little endian data in firmware image */
1753 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1754 static int
1755 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
1756 {
1757 bus_dmamap_t map;
1758 bus_dma_segment_t seg;
1759 caddr_t virtaddr;
1760 u_char *p, *end;
1761 u_int32_t sentinel, ctl, src, dst, sum, len, mlen;
1762 int ntries, nsegs, error;
1763
1764 /* Allocate DMA memory for storing firmware image */
1765 error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1766 BUS_DMA_NOWAIT, &map);
1767 if (error != 0) {
1768 aprint_error("%s: could not create firmware DMA map\n",
1769 sc->sc_dev.dv_xname);
1770 goto fail1;
1771 }
1772
1773 /*
1774 * We cannot map fw directly because of some hardware constraints on
1775 * the mapping address.
1776 */
1777 error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
1778 &nsegs, BUS_DMA_NOWAIT);
1779 if (error != 0) {
1780 aprint_error("%s: could not allocate firmware DMA memory\n",
1781 sc->sc_dev.dv_xname);
1782 goto fail2;
1783 }
1784
1785 error = bus_dmamem_map(sc->sc_dmat, &seg, nsegs, size, &virtaddr,
1786 BUS_DMA_NOWAIT);
1787 if (error != 0) {
1788 aprint_error("%s: could not load firmware DMA map\n",
1789 sc->sc_dev.dv_xname);
1790 goto fail3;
1791 }
1792
1793 error = bus_dmamap_load(sc->sc_dmat, map, virtaddr, size, NULL,
1794 BUS_DMA_NOWAIT);
1795 if (error != 0) {
1796 aprint_error("%s: could not load fw dma map\n",
1797 sc->sc_dev.dv_xname);
1798 goto fail4;
1799 }
1800
1801 /* Copy firmware image to DMA memory */
1802 memcpy(virtaddr, fw, size);
1803
1804 /* Make sure the adapter will get up-to-date values */
1805 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
1806
1807 /* Tell the adapter where the command blocks are stored */
1808 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
1809
1810 /*
1811 * Store command blocks into adapter's internal memory using register
1812 * indirections. The adapter will read the firmware image through DMA
1813 * using information stored in command blocks.
1814 */
1815 src = map->dm_segs[0].ds_addr;
1816 p = virtaddr;
1817 end = p + size;
1818 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
1819
1820 while (p < end) {
1821 dst = GETLE32(p); p += 4; src += 4;
1822 len = GETLE32(p); p += 4; src += 4;
1823 p += len;
1824
1825 while (len > 0) {
1826 mlen = min(len, IWI_CB_MAXDATALEN);
1827
1828 ctl = IWI_CB_DEFAULT_CTL | mlen;
1829 sum = ctl ^ src ^ dst;
1830
1831 /* Write a command block */
1832 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
1833 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
1834 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
1835 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
1836
1837 src += mlen;
1838 dst += mlen;
1839 len -= mlen;
1840 }
1841 }
1842
1843 /* Write a fictive final command block (sentinel) */
1844 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
1845 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1846
1847 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
1848 ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER));
1849
1850 /* Tell the adapter to start processing command blocks */
1851 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
1852
1853 /* Wait until the adapter has processed all command blocks */
1854 for (ntries = 0; ntries < 400; ntries++) {
1855 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
1856 break;
1857 DELAY(100);
1858 }
1859 if (ntries == 400) {
1860 aprint_error("%s: timeout processing cb\n",
1861 sc->sc_dev.dv_xname);
1862 error = EIO;
1863 goto fail5;
1864 }
1865
1866 /* We're done with command blocks processing */
1867 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
1868
1869 /* Allow interrupts so we know when the firmware is inited */
1870 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1871
1872 /* Tell the adapter to initialize the firmware */
1873 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
1874 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1875 IWI_CTL_ALLOW_STANDBY);
1876
1877 /* Wait at most one second for firmware initialization to complete */
1878 if ((error = tsleep(sc, 0, "iwiinit", hz)) != 0) {
1879 aprint_error("%s: timeout waiting for firmware initialization "
1880 "to complete\n", sc->sc_dev.dv_xname);
1881 goto fail5;
1882 }
1883
1884 fail5: bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
1885 bus_dmamap_unload(sc->sc_dmat, map);
1886 fail4: bus_dmamem_unmap(sc->sc_dmat, virtaddr, size);
1887 fail3: bus_dmamem_free(sc->sc_dmat, &seg, 1);
1888 fail2: bus_dmamap_destroy(sc->sc_dmat, map);
1889
1890 fail1: return error;
1891 }
1892
1893 /*
1894 * Store firmware into kernel memory so we can download it when we need to,
1895 * e.g when the adapter wakes up from suspend mode.
1896 */
1897 static int
1898 iwi_cache_firmware(struct iwi_softc *sc, void *data)
1899 {
1900 struct iwi_firmware *kfw = &sc->fw;
1901 struct iwi_firmware ufw;
1902 int error;
1903
1904 iwi_free_firmware(sc);
1905
1906 if ((error = copyin(data, &ufw, sizeof ufw)) != 0)
1907 goto fail1;
1908
1909 kfw->boot_size = ufw.boot_size;
1910 kfw->ucode_size = ufw.ucode_size;
1911 kfw->main_size = ufw.main_size;
1912
1913 kfw->boot = malloc(kfw->boot_size, M_DEVBUF, M_NOWAIT);
1914 if (kfw->boot == NULL) {
1915 error = ENOMEM;
1916 goto fail1;
1917 }
1918
1919 kfw->ucode = malloc(kfw->ucode_size, M_DEVBUF, M_NOWAIT);
1920 if (kfw->ucode == NULL) {
1921 error = ENOMEM;
1922 goto fail2;
1923 }
1924
1925 kfw->main = malloc(kfw->main_size, M_DEVBUF, M_NOWAIT);
1926 if (kfw->main == NULL) {
1927 error = ENOMEM;
1928 goto fail3;
1929 }
1930
1931 if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0)
1932 goto fail4;
1933
1934 if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0)
1935 goto fail4;
1936
1937 if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0)
1938 goto fail4;
1939
1940 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
1941 kfw->boot_size, kfw->ucode_size, kfw->main_size));
1942
1943 sc->flags |= IWI_FLAG_FW_CACHED;
1944
1945 return 0;
1946
1947 fail4: free(kfw->boot, M_DEVBUF);
1948 fail3: free(kfw->ucode, M_DEVBUF);
1949 fail2: free(kfw->main, M_DEVBUF);
1950 fail1:
1951 return error;
1952 }
1953
1954 static void
1955 iwi_free_firmware(struct iwi_softc *sc)
1956 {
1957 if (!(sc->flags & IWI_FLAG_FW_CACHED))
1958 return;
1959
1960 free(sc->fw.boot, M_DEVBUF);
1961 free(sc->fw.ucode, M_DEVBUF);
1962 free(sc->fw.main, M_DEVBUF);
1963
1964 sc->flags &= ~IWI_FLAG_FW_CACHED;
1965 }
1966
1967 static int
1968 iwi_config(struct iwi_softc *sc)
1969 {
1970 struct ieee80211com *ic = &sc->sc_ic;
1971 struct ifnet *ifp = &sc->sc_if;
1972 struct iwi_configuration config;
1973 struct iwi_rateset rs;
1974 struct iwi_txpower power;
1975 struct ieee80211_key *wk;
1976 struct iwi_wep_key wepkey;
1977 u_int32_t data;
1978 int error, i;
1979
1980 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1981 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
1982 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
1983 IEEE80211_ADDR_LEN, 0);
1984 if (error != 0)
1985 return error;
1986
1987 memset(&config, 0, sizeof config);
1988 config.bluetooth_coexistence = sc->bluetooth;
1989 config.antenna = sc->antenna;
1990 config.multicast_enabled = 1;
1991 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
1992 config.disable_unicast_decryption = 1;
1993 config.disable_multicast_decryption = 1;
1994 DPRINTF(("Configuring adapter\n"));
1995 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, sizeof config,
1996 0);
1997 if (error != 0)
1998 return error;
1999
2000 data = htole32(IWI_POWER_MODE_CAM);
2001 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2002 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
2003 if (error != 0)
2004 return error;
2005
2006 data = htole32(ic->ic_rtsthreshold);
2007 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2008 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
2009 if (error != 0)
2010 return error;
2011
2012 data = htole32(ic->ic_fragthreshold);
2013 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2014 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
2015 if (error != 0)
2016 return error;
2017
2018 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2019 power.mode = IWI_MODE_11B;
2020 power.nchan = 11;
2021 for (i = 0; i < 11; i++) {
2022 power.chan[i].chan = i + 1;
2023 power.chan[i].power = IWI_TXPOWER_MAX;
2024 }
2025 DPRINTF(("Setting .11b channels tx power\n"));
2026 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2027 0);
2028 if (error != 0)
2029 return error;
2030
2031 power.mode = IWI_MODE_11G;
2032 DPRINTF(("Setting .11g channels tx power\n"));
2033 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2034 0);
2035 if (error != 0)
2036 return error;
2037 }
2038
2039 rs.mode = IWI_MODE_11G;
2040 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2041 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2042 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2043 rs.nrates);
2044 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2045 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2046 if (error != 0)
2047 return error;
2048
2049 rs.mode = IWI_MODE_11A;
2050 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2051 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2052 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2053 rs.nrates);
2054 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2055 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2056 if (error != 0)
2057 return error;
2058
2059 data = htole32(arc4random());
2060 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2061 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
2062 if (error != 0)
2063 return error;
2064
2065 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2066 wk = &ic->ic_crypto.cs_nw_keys[i];
2067
2068 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2069 wepkey.idx = i;
2070 wepkey.len = wk->wk_keylen;
2071 memset(wepkey.key, 0, sizeof wepkey.key);
2072 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2073 DPRINTF(("Setting wep key index %u len %u\n",
2074 wepkey.idx, wepkey.len));
2075 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2076 sizeof wepkey, 0);
2077 if (error != 0)
2078 return error;
2079 }
2080
2081 /* Enable adapter */
2082 DPRINTF(("Enabling adapter\n"));
2083 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
2084 }
2085
2086 static int
2087 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
2088 {
2089 struct ieee80211com *ic = &sc->sc_ic;
2090 struct iwi_scan scan;
2091
2092 (void)memset(&scan, 0, sizeof scan);
2093 scan.type = IWI_SCAN_TYPE_PASSIVE;
2094 scan.dwelltime = htole16(2000);
2095 scan.channels[0] = 1 | (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ :
2096 IWI_CHAN_2GHZ);
2097 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2098
2099 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
2100 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2101 }
2102
2103 static int
2104 iwi_scan(struct iwi_softc *sc)
2105 {
2106 struct ieee80211com *ic = &sc->sc_ic;
2107 struct iwi_scan scan;
2108 u_int8_t *p;
2109 int i, count;
2110
2111 (void)memset(&scan, 0, sizeof scan);
2112 scan.type = IWI_SCAN_TYPE_BROADCAST;
2113 scan.dwelltime = htole16(sc->dwelltime);
2114
2115 p = scan.channels;
2116 count = 0;
2117 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2118 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
2119 isset(ic->ic_chan_active, i)) {
2120 *++p = i;
2121 count++;
2122 }
2123 }
2124 *(p - count) = IWI_CHAN_5GHZ | count;
2125
2126 count = 0;
2127 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2128 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
2129 isset(ic->ic_chan_active, i)) {
2130 *++p = i;
2131 count++;
2132 }
2133 }
2134 *(p - count) = IWI_CHAN_2GHZ | count;
2135
2136 DPRINTF(("Start scanning\n"));
2137 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2138 }
2139
2140 static int
2141 iwi_auth_and_assoc(struct iwi_softc *sc)
2142 {
2143 struct ieee80211com *ic = &sc->sc_ic;
2144 struct ieee80211_node *ni = ic->ic_bss;
2145 struct ifnet *ifp = &sc->sc_if;
2146 struct iwi_configuration config;
2147 struct iwi_associate assoc;
2148 struct iwi_rateset rs;
2149 u_int16_t capinfo;
2150 u_int32_t data;
2151 int error;
2152
2153 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2154 memset(&config, 0, sizeof config);
2155 config.bluetooth_coexistence = sc->bluetooth;
2156 config.antenna = sc->antenna;
2157 config.multicast_enabled = 1;
2158 config.use_protection = 1;
2159 config.answer_pbreq =
2160 (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2161 config.disable_unicast_decryption = 1;
2162 config.disable_multicast_decryption = 1;
2163 DPRINTF(("Configuring adapter\n"));
2164 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config,
2165 sizeof config, 1);
2166 if (error != 0)
2167 return error;
2168 }
2169
2170 #ifdef IWI_DEBUG
2171 if (iwi_debug > 0) {
2172 printf("Setting ESSID to ");
2173 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2174 printf("\n");
2175 }
2176 #endif
2177 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2178 if (error != 0)
2179 return error;
2180
2181 /* the rate set has already been "negociated" */
2182 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2183 IWI_MODE_11G;
2184 rs.type = IWI_RATESET_TYPE_NEGOCIATED;
2185 rs.nrates = ni->ni_rates.rs_nrates;
2186 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2187 DPRINTF(("Setting negociated rates (%u)\n", rs.nrates));
2188 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2189 if (error != 0)
2190 return error;
2191
2192 if (ic->ic_opt_ie != NULL) {
2193 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
2194 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
2195 ic->ic_opt_ie_len, 1);
2196 if (error != 0)
2197 return error;
2198 }
2199 data = htole32(ni->ni_rssi);
2200 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2201 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2202 if (error != 0)
2203 return error;
2204
2205 memset(&assoc, 0, sizeof assoc);
2206 assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2207 IWI_MODE_11G;
2208 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2209 if (ni->ni_authmode == IEEE80211_AUTH_SHARED)
2210 assoc.auth = (ic->ic_crypto.cs_def_txkey << 4) | IWI_AUTH_SHARED;
2211 if (ic->ic_opt_ie != NULL)
2212 assoc.policy |= htole16(IWI_POLICY_OPTIE);
2213 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
2214
2215 if (ic->ic_opmode == IEEE80211_M_IBSS)
2216 capinfo = IEEE80211_CAPINFO_IBSS;
2217 else
2218 capinfo = IEEE80211_CAPINFO_ESS;
2219 if (ic->ic_flags & IEEE80211_F_PRIVACY)
2220 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2221 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2222 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2223 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2224 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2225 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2226 assoc.capinfo = htole16(capinfo);
2227
2228 assoc.lintval = htole16(ic->ic_lintval);
2229 assoc.intval = htole16(ni->ni_intval);
2230 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
2231 if (ic->ic_opmode == IEEE80211_M_IBSS)
2232 IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr);
2233 else
2234 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
2235 DPRINTF(("Trying to associate to %s channel %u auth %u\n",
2236 ether_sprintf(assoc.bssid), assoc.chan, assoc.auth));
2237 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
2238 }
2239
2240 static int
2241 iwi_init(struct ifnet *ifp)
2242 {
2243 struct iwi_softc *sc = ifp->if_softc;
2244 struct ieee80211com *ic = &sc->sc_ic;
2245 struct iwi_firmware *fw = &sc->fw;
2246 int i, error;
2247
2248 /* exit immediately if firmware has not been ioctl'd */
2249 if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
2250 if (!(sc->flags & IWI_FLAG_FW_WARNED))
2251 aprint_error("%s: Firmware not loaded\n",
2252 sc->sc_dev.dv_xname);
2253 sc->flags |= IWI_FLAG_FW_WARNED;
2254 ifp->if_flags &= ~IFF_UP;
2255 return EIO;
2256 }
2257
2258 iwi_stop(ifp, 0);
2259
2260 if ((error = iwi_reset(sc)) != 0) {
2261 aprint_error("%s: could not reset adapter\n",
2262 sc->sc_dev.dv_xname);
2263 goto fail;
2264 }
2265
2266 if ((error = iwi_load_firmware(sc, fw->boot, fw->boot_size)) != 0) {
2267 aprint_error("%s: could not load boot firmware\n",
2268 sc->sc_dev.dv_xname);
2269 goto fail;
2270 }
2271
2272 if ((error = iwi_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) {
2273 aprint_error("%s: could not load microcode\n",
2274 sc->sc_dev.dv_xname);
2275 goto fail;
2276 }
2277
2278 iwi_stop_master(sc);
2279
2280 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.desc_map->dm_segs[0].ds_addr);
2281 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
2282 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
2283
2284 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
2285 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq.count);
2286 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur);
2287
2288 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
2289 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq.count);
2290 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq.cur);
2291
2292 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
2293 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq.count);
2294 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq.cur);
2295
2296 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
2297 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq.count);
2298 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq.cur);
2299
2300 for (i = 0; i < sc->rxq.count; i++)
2301 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4,
2302 sc->rxq.data[i].map->dm_segs[0].ds_addr);
2303
2304 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count -1);
2305
2306 if ((error = iwi_load_firmware(sc, fw->main, fw->main_size)) != 0) {
2307 aprint_error("%s: could not load main firmware\n",
2308 sc->sc_dev.dv_xname);
2309 goto fail;
2310 }
2311
2312 sc->flags |= IWI_FLAG_FW_INITED;
2313
2314 if ((error = iwi_config(sc)) != 0) {
2315 aprint_error("%s: device configuration failed\n",
2316 sc->sc_dev.dv_xname);
2317 goto fail;
2318 }
2319
2320 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2321 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2322 else
2323 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2324
2325 ifp->if_flags &= ~IFF_OACTIVE;
2326 ifp->if_flags |= IFF_RUNNING;
2327
2328 return 0;
2329
2330 fail: ifp->if_flags &= ~IFF_UP;
2331 iwi_stop(ifp, 0);
2332
2333 return error;
2334 }
2335
2336 static void
2337 iwi_stop(struct ifnet *ifp, int disable)
2338 {
2339 struct iwi_softc *sc = ifp->if_softc;
2340 struct ieee80211com *ic = &sc->sc_ic;
2341
2342 iwi_stop_master(sc);
2343 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
2344
2345 /* reset rings */
2346 iwi_reset_cmd_ring(sc, &sc->cmdq);
2347 iwi_reset_tx_ring(sc, &sc->txq);
2348 iwi_reset_rx_ring(sc, &sc->rxq);
2349
2350 ifp->if_timer = 0;
2351 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2352
2353 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2354 }
2355