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