if_iwi.c revision 1.27 1 /* $NetBSD: if_iwi.c,v 1.27 2005/09/25 15:18:21 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.27 2005/09/25 15:18:21 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 /* Disable interrupts */
1268 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
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 /* Acknowledge interrupts */
1297 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1298
1299 /* Re-enable interrupts */
1300 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1301
1302 return 1;
1303 }
1304
1305 static int
1306 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len,
1307 int async)
1308 {
1309 struct iwi_cmd_desc *desc;
1310
1311 desc = &sc->cmdq.desc[sc->cmdq.cur];
1312
1313 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1314 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1315 desc->type = type;
1316 desc->len = len;
1317 memcpy(desc->data, data, len);
1318
1319 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
1320 sc->cmdq.cur * IWI_CMD_DESC_SIZE,
1321 IWI_CMD_DESC_SIZE, BUS_DMASYNC_PREWRITE);
1322
1323 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1324 type, len));
1325
1326 sc->cmdq.cur = (sc->cmdq.cur + 1) % sc->cmdq.count;
1327 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1328
1329 return async ? 0 : tsleep(sc, 0, "iwicmd", hz);
1330 }
1331
1332 static int
1333 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1334 {
1335 struct iwi_softc *sc = ifp->if_softc;
1336 struct ieee80211com *ic = &sc->sc_ic;
1337 struct ieee80211_frame wh;
1338 struct ieee80211_key *k;
1339 struct iwi_tx_data *data;
1340 struct iwi_tx_desc *desc;
1341 struct mbuf *mnew;
1342 int error, i;
1343
1344 (void)memcpy(&wh, mtod(m0, struct ieee80211_frame *), sizeof(wh));
1345 if (wh.i_fc[1] & IEEE80211_FC1_WEP) {
1346 k = ieee80211_crypto_encap(ic, ni, m0);
1347 if (k == NULL) {
1348 m_freem(m0);
1349 return ENOBUFS;
1350 }
1351 }
1352
1353 #if NBPFILTER > 0
1354 if (sc->sc_drvbpf != NULL) {
1355 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1356
1357 tap->wt_flags = 0;
1358 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1359 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1360
1361 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1362 }
1363 #endif
1364
1365 data = &sc->txq.data[sc->txq.cur];
1366 desc = &sc->txq.desc[sc->txq.cur];
1367
1368 /* trim IEEE802.11 header */
1369 m_adj(m0, sizeof (struct ieee80211_frame));
1370
1371 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, BUS_DMA_NOWAIT);
1372 if (error != 0 && error != EFBIG) {
1373 aprint_error("%s: could not map mbuf (error %d)\n",
1374 sc->sc_dev.dv_xname, error);
1375 m_freem(m0);
1376 return error;
1377 }
1378 if (error != 0) {
1379 /* too many fragments, linearize */
1380
1381 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1382 if (mnew == NULL) {
1383 m_freem(m0);
1384 return ENOMEM;
1385 }
1386
1387 M_COPY_PKTHDR(mnew, m0);
1388 MCLGET(mnew, M_DONTWAIT);
1389 if (!(mnew->m_flags & M_EXT)) {
1390 m_freem(m0);
1391 m_freem(mnew);
1392 return ENOMEM;
1393 }
1394
1395 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, caddr_t));
1396 m_freem(m0);
1397 mnew->m_len = mnew->m_pkthdr.len;
1398 m0 = mnew;
1399
1400 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1401 BUS_DMA_NOWAIT);
1402 if (error != 0) {
1403 aprint_error("%s: could not map mbuf (error %d)\n",
1404 sc->sc_dev.dv_xname, error);
1405 m_freem(m0);
1406 return error;
1407 }
1408 }
1409
1410 data->m = m0;
1411 data->ni = ni;
1412
1413 desc->hdr.type = IWI_HDR_TYPE_DATA;
1414 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1415 desc->cmd = IWI_DATA_CMD_TX;
1416 desc->len = htole16(m0->m_pkthdr.len);
1417 (void)memcpy(&desc->wh, &wh, sizeof (struct ieee80211_frame));
1418 desc->flags = 0;
1419 if (!IEEE80211_IS_MULTICAST(wh.i_addr1))
1420 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1421
1422 #if 0
1423 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1424 wh.i_fc[1] |= IEEE80211_FC1_WEP;
1425 desc->wep_txkey = ic->ic_crypto.cs_def_txkey;
1426 } else
1427 #endif
1428 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1429
1430 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1431 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1432
1433 desc->nseg = htole32(data->map->dm_nsegs);
1434 for (i = 0; i < data->map->dm_nsegs; i++) {
1435 desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr);
1436 desc->seg_len[i] = htole32(data->map->dm_segs[i].ds_len);
1437 }
1438
1439 bus_dmamap_sync(sc->sc_dmat, sc->txq.desc_map,
1440 sc->txq.cur * IWI_TX_DESC_SIZE,
1441 IWI_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
1442
1443 bus_dmamap_sync(sc->sc_dmat, data->map, 0, MCLBYTES,
1444 BUS_DMASYNC_PREWRITE);
1445
1446 DPRINTFN(5, ("sending data frame len=%u nseg=%u\n",
1447 desc->len, desc->nseg));
1448
1449 /* Inform firmware about this new packet */
1450 sc->txq.queued++;
1451 sc->txq.cur = (sc->txq.cur + 1) % sc->txq.count;
1452 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur);
1453
1454 return 0;
1455 }
1456
1457 static void
1458 iwi_start(struct ifnet *ifp)
1459 {
1460 struct iwi_softc *sc = ifp->if_softc;
1461 struct ieee80211com *ic = &sc->sc_ic;
1462 struct mbuf *m0;
1463 struct ether_header *eh;
1464 struct ieee80211_node *ni;
1465
1466 if (ic->ic_state != IEEE80211_S_RUN)
1467 return;
1468
1469 for (;;) {
1470 IF_DEQUEUE(&ifp->if_snd, m0);
1471 if (m0 == NULL)
1472 break;
1473
1474 if (sc->txq.queued >= sc->txq.count - 4) {
1475 IF_PREPEND(&ifp->if_snd, m0);
1476 ifp->if_flags |= IFF_OACTIVE;
1477 break;
1478 }
1479
1480 if (m0->m_len < sizeof (struct ether_header) &&
1481 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL)
1482 continue;
1483
1484 #if NBPFILTER > 0
1485 if (ifp->if_bpf != NULL)
1486 bpf_mtap(ifp->if_bpf, m0);
1487 #endif
1488
1489 eh = mtod(m0, struct ether_header *);
1490 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1491 if (ni == NULL) {
1492 m_freem(m0);
1493 continue;
1494 }
1495
1496 m0 = ieee80211_encap(ic, m0, ni);
1497 if (m0 == NULL) {
1498 ieee80211_free_node(ni);
1499 continue;
1500 }
1501
1502 #if NBPFILTER > 0
1503 if (ic->ic_rawbpf != NULL)
1504 bpf_mtap(ic->ic_rawbpf, m0);
1505 #endif
1506
1507 if (iwi_tx_start(ifp, m0, ni) != 0) {
1508 ieee80211_free_node(ni);
1509 ifp->if_oerrors++;
1510 break;
1511 }
1512
1513 /* start watchdog timer */
1514 sc->sc_tx_timer = 5;
1515 ifp->if_timer = 1;
1516 }
1517 }
1518
1519 static void
1520 iwi_watchdog(struct ifnet *ifp)
1521 {
1522 struct iwi_softc *sc = ifp->if_softc;
1523
1524 ifp->if_timer = 0;
1525
1526 if (sc->sc_tx_timer > 0) {
1527 if (--sc->sc_tx_timer == 0) {
1528 aprint_error("%s: device timeout\n",
1529 sc->sc_dev.dv_xname);
1530 ifp->if_oerrors++;
1531 ifp->if_flags &= ~IFF_UP;
1532 iwi_stop(ifp, 1);
1533 return;
1534 }
1535 ifp->if_timer = 1;
1536 }
1537
1538 ieee80211_watchdog(&sc->sc_ic);
1539 }
1540
1541 static int
1542 iwi_get_table0(struct iwi_softc *sc, uint32_t *tbl)
1543 {
1544 uint32_t size, buf[128];
1545
1546 if (!(sc->flags & IWI_FLAG_FW_INITED)) {
1547 memset(buf, 0, sizeof buf);
1548 return copyout(buf, tbl, sizeof buf);
1549 }
1550
1551 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
1552 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
1553
1554 return copyout(buf, tbl, sizeof buf);
1555 }
1556
1557 static int
1558 iwi_get_radio(struct iwi_softc *sc, int *ret)
1559 {
1560 int val;
1561
1562 val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0;
1563 return copyout(&val, ret, sizeof val);
1564 }
1565
1566 static int
1567 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1568 {
1569 struct iwi_softc *sc = ifp->if_softc;
1570 struct ifreq *ifr;
1571 int s, error = 0;
1572
1573 s = splnet();
1574
1575 switch (cmd) {
1576 case SIOCSIFFLAGS:
1577 if (ifp->if_flags & IFF_UP) {
1578 if (!(ifp->if_flags & IFF_RUNNING))
1579 iwi_init(ifp);
1580 } else {
1581 if (ifp->if_flags & IFF_RUNNING)
1582 iwi_stop(ifp, 1);
1583 }
1584 break;
1585
1586 case SIOCGTABLE0:
1587 ifr = (struct ifreq *)data;
1588 error = iwi_get_table0(sc, (uint32_t *)ifr->ifr_data);
1589 break;
1590
1591 case SIOCGRADIO:
1592 ifr = (struct ifreq *)data;
1593 error = iwi_get_radio(sc, (int *)ifr->ifr_data);
1594 break;
1595
1596 case SIOCSLOADFW:
1597 /* only super-user can do that! */
1598 if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
1599 break;
1600
1601 ifr = (struct ifreq *)data;
1602 error = iwi_cache_firmware(sc, ifr->ifr_data);
1603 break;
1604
1605 case SIOCSKILLFW:
1606 /* only super-user can do that! */
1607 if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
1608 break;
1609
1610 ifp->if_flags &= ~IFF_UP;
1611 iwi_stop(ifp, 1);
1612 iwi_free_firmware(sc);
1613 break;
1614
1615 default:
1616 error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
1617 }
1618
1619 if (error == ENETRESET && cmd != SIOCADDMULTI) {
1620 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1621 (IFF_UP | IFF_RUNNING))
1622 iwi_init(ifp);
1623 error = 0;
1624 }
1625
1626 splx(s);
1627 return error;
1628 }
1629
1630 static void
1631 iwi_stop_master(struct iwi_softc *sc)
1632 {
1633 int ntries;
1634
1635 /* Disable interrupts */
1636 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1637
1638 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1639 for (ntries = 0; ntries < 5; ntries++) {
1640 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1641 break;
1642 DELAY(10);
1643 }
1644 if (ntries == 5)
1645 aprint_error("%s: timeout waiting for master\n",
1646 sc->sc_dev.dv_xname);
1647
1648 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1649 IWI_RST_PRINCETON_RESET);
1650
1651 sc->flags &= ~IWI_FLAG_FW_INITED;
1652 }
1653
1654 static int
1655 iwi_reset(struct iwi_softc *sc)
1656 {
1657 int i, ntries;
1658
1659 iwi_stop_master(sc);
1660
1661 /* Move adapter to D0 state */
1662 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1663 IWI_CTL_INIT);
1664
1665 /* Initialize Phase-Locked Level (PLL) */
1666 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
1667
1668 /* Wait for clock stabilization */
1669 for (ntries = 0; ntries < 1000; ntries++) {
1670 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
1671 break;
1672 DELAY(200);
1673 }
1674 if (ntries == 1000) {
1675 aprint_error("%s: timeout waiting for clock stabilization\n",
1676 sc->sc_dev.dv_xname);
1677 return EIO;
1678 }
1679
1680 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1681 IWI_RST_SW_RESET);
1682
1683 DELAY(10);
1684
1685 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1686 IWI_CTL_INIT);
1687
1688 /* Clear NIC memory */
1689 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
1690 for (i = 0; i < 0xc000; i++)
1691 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1692
1693 return 0;
1694 }
1695
1696 static int
1697 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
1698 {
1699 uint16_t *w;
1700 int ntries, i;
1701
1702 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1703 IWI_RST_STOP_MASTER);
1704 for (ntries = 0; ntries < 5; ntries++) {
1705 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1706 break;
1707 DELAY(10);
1708 }
1709 if (ntries == 5) {
1710 aprint_error("%s: timeout waiting for master\n",
1711 sc->sc_dev.dv_xname);
1712 return EIO;
1713 }
1714
1715 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1716 DELAY(5000);
1717 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
1718 ~IWI_RST_PRINCETON_RESET);
1719 DELAY(5000);
1720 MEM_WRITE_4(sc, 0x3000e0, 0);
1721 DELAY(1000);
1722 MEM_WRITE_4(sc, 0x300004, 1);
1723 DELAY(1000);
1724 MEM_WRITE_4(sc, 0x300004, 0);
1725 DELAY(1000);
1726 MEM_WRITE_1(sc, 0x200000, 0x00);
1727 MEM_WRITE_1(sc, 0x200000, 0x40);
1728 DELAY(1000);
1729
1730 /* Adapter is buggy, we must set the address for each word */
1731 for (w = uc; size > 0; w++, size -= 2)
1732 MEM_WRITE_2(sc, 0x200010, *w);
1733
1734 MEM_WRITE_1(sc, 0x200000, 0x00);
1735 MEM_WRITE_1(sc, 0x200000, 0x80);
1736
1737 /* Wait until we get a response in the uc queue */
1738 for (ntries = 0; ntries < 100; ntries++) {
1739 if (MEM_READ_1(sc, 0x200000) & 1)
1740 break;
1741 DELAY(100);
1742 }
1743 if (ntries == 100) {
1744 aprint_error("%s: timeout waiting for ucode to initialize\n",
1745 sc->sc_dev.dv_xname);
1746 return EIO;
1747 }
1748
1749 /* Empty the uc queue or the firmware will not initialize properly */
1750 for (i = 0; i < 7; i++)
1751 MEM_READ_4(sc, 0x200004);
1752
1753 MEM_WRITE_1(sc, 0x200000, 0x00);
1754
1755 return 0;
1756 }
1757
1758 /* macro to handle unaligned little endian data in firmware image */
1759 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1760 static int
1761 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
1762 {
1763 bus_dmamap_t map;
1764 u_char *p, *end;
1765 uint32_t sentinel, ctl, sum;
1766 uint32_t cs, sl, cd, cl;
1767 int ntries, nsegs, error;
1768 int sn;
1769
1770
1771 nsegs = (size + PAGE_SIZE - 1) / PAGE_SIZE;
1772
1773 /* Create a DMA map memory for the firmware image */
1774 error = bus_dmamap_create(sc->sc_dmat, size, nsegs, size, 0,
1775 BUS_DMA_NOWAIT, &map);
1776 if (error != 0) {
1777 aprint_error("%s: could not create firmware DMA map\n",
1778 sc->sc_dev.dv_xname);
1779 goto fail1;
1780 }
1781
1782 error = bus_dmamap_load(sc->sc_dmat, map, fw, size, NULL,
1783 BUS_DMA_NOWAIT | BUS_DMA_WRITE);
1784 if (error != 0) {
1785 aprint_error("%s: could not load fw dma map(%d)\n",
1786 sc->sc_dev.dv_xname, error);
1787 goto fail2;
1788 }
1789
1790 /* Make sure the adapter will get up-to-date values */
1791 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
1792
1793 /* Tell the adapter where the command blocks are stored */
1794 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
1795
1796 /*
1797 * Store command blocks into adapter's internal memory using register
1798 * indirections. The adapter will read the firmware image through DMA
1799 * using information stored in command blocks.
1800 */
1801 p = fw;
1802 end = p + size;
1803 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
1804
1805 sn = 0;
1806 sl = cl = 0;
1807 cs = cd = 0;
1808 while (p < end) {
1809 if (sl == 0) {
1810 cs = map->dm_segs[sn].ds_addr;
1811 sl = map->dm_segs[sn].ds_len;
1812 sn++;
1813 }
1814 if (cl == 0) {
1815 cd = GETLE32(p); p += 4; cs += 4; sl -= 4;
1816 cl = GETLE32(p); p += 4; cs += 4; sl -= 4;
1817 }
1818 while (sl > 0 && cl > 0) {
1819 int len = min(cl, sl);
1820
1821 sl -= len;
1822 cl -= len;
1823 p += len;
1824
1825 while (len > 0) {
1826 int mlen = min(len, IWI_CB_MAXDATALEN);
1827
1828 ctl = IWI_CB_DEFAULT_CTL | mlen;
1829 sum = ctl ^ cs ^ cd;
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, cs);
1834 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cd);
1835 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
1836
1837 cs += mlen;
1838 cd += mlen;
1839 len -= mlen;
1840 }
1841 }
1842 }
1843
1844 /* Write a fictive final command block (sentinel) */
1845 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
1846 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1847
1848 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
1849 ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER));
1850
1851 /* Tell the adapter to start processing command blocks */
1852 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
1853
1854 /* Wait until the adapter has processed all command blocks */
1855 for (ntries = 0; ntries < 400; ntries++) {
1856 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
1857 break;
1858 DELAY(100);
1859 }
1860 if (ntries == 400) {
1861 aprint_error("%s: timeout processing cb\n",
1862 sc->sc_dev.dv_xname);
1863 error = EIO;
1864 goto fail2;
1865 }
1866
1867 /* We're done with command blocks processing */
1868 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
1869
1870 /* Allow interrupts so we know when the firmware is inited */
1871 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1872
1873 /* Tell the adapter to initialize the firmware */
1874 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
1875 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1876 IWI_CTL_ALLOW_STANDBY);
1877
1878 /* Wait at most one second for firmware initialization to complete */
1879 if ((error = tsleep(sc, 0, "iwiinit", hz)) != 0) {
1880 aprint_error("%s: timeout waiting for firmware initialization "
1881 "to complete\n", sc->sc_dev.dv_xname);
1882 goto fail3;
1883 }
1884
1885 fail3:
1886 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
1887 bus_dmamap_unload(sc->sc_dmat, map);
1888 fail2:
1889 bus_dmamap_destroy(sc->sc_dmat, map);
1890
1891 fail1:
1892 return error;
1893 }
1894
1895 /*
1896 * Store firmware into kernel memory so we can download it when we need to,
1897 * e.g when the adapter wakes up from suspend mode.
1898 */
1899 static int
1900 iwi_cache_firmware(struct iwi_softc *sc, void *data)
1901 {
1902 struct iwi_firmware *kfw = &sc->fw;
1903 struct iwi_firmware ufw;
1904 int error;
1905
1906 iwi_free_firmware(sc);
1907
1908 if ((error = copyin(data, &ufw, sizeof ufw)) != 0)
1909 goto fail1;
1910
1911 kfw->boot_size = ufw.boot_size;
1912 kfw->ucode_size = ufw.ucode_size;
1913 kfw->main_size = ufw.main_size;
1914
1915 kfw->boot = malloc(kfw->boot_size, M_DEVBUF, M_NOWAIT);
1916 if (kfw->boot == NULL) {
1917 error = ENOMEM;
1918 goto fail1;
1919 }
1920
1921 kfw->ucode = malloc(kfw->ucode_size, M_DEVBUF, M_NOWAIT);
1922 if (kfw->ucode == NULL) {
1923 error = ENOMEM;
1924 goto fail2;
1925 }
1926
1927 kfw->main = malloc(kfw->main_size, M_DEVBUF, M_NOWAIT);
1928 if (kfw->main == NULL) {
1929 error = ENOMEM;
1930 goto fail3;
1931 }
1932
1933 if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0)
1934 goto fail4;
1935
1936 if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0)
1937 goto fail4;
1938
1939 if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0)
1940 goto fail4;
1941
1942 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
1943 kfw->boot_size, kfw->ucode_size, kfw->main_size));
1944
1945 sc->flags |= IWI_FLAG_FW_CACHED;
1946
1947 return 0;
1948
1949 fail4: free(kfw->boot, M_DEVBUF);
1950 fail3: free(kfw->ucode, M_DEVBUF);
1951 fail2: free(kfw->main, M_DEVBUF);
1952 fail1:
1953 return error;
1954 }
1955
1956 static void
1957 iwi_free_firmware(struct iwi_softc *sc)
1958 {
1959 if (!(sc->flags & IWI_FLAG_FW_CACHED))
1960 return;
1961
1962 free(sc->fw.boot, M_DEVBUF);
1963 free(sc->fw.ucode, M_DEVBUF);
1964 free(sc->fw.main, M_DEVBUF);
1965
1966 sc->flags &= ~IWI_FLAG_FW_CACHED;
1967 }
1968
1969 static int
1970 iwi_config(struct iwi_softc *sc)
1971 {
1972 struct ieee80211com *ic = &sc->sc_ic;
1973 struct ifnet *ifp = &sc->sc_if;
1974 struct iwi_configuration config;
1975 struct iwi_rateset rs;
1976 struct iwi_txpower power;
1977 struct ieee80211_key *wk;
1978 struct iwi_wep_key wepkey;
1979 uint32_t data;
1980 int error, i;
1981
1982 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1983 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
1984 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
1985 IEEE80211_ADDR_LEN, 0);
1986 if (error != 0)
1987 return error;
1988
1989 memset(&config, 0, sizeof config);
1990 config.bluetooth_coexistence = sc->bluetooth;
1991 config.antenna = sc->antenna;
1992 config.multicast_enabled = 1;
1993 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
1994 config.disable_unicast_decryption = 1;
1995 config.disable_multicast_decryption = 1;
1996 DPRINTF(("Configuring adapter\n"));
1997 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, sizeof config,
1998 0);
1999 if (error != 0)
2000 return error;
2001
2002 data = htole32(IWI_POWER_MODE_CAM);
2003 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2004 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
2005 if (error != 0)
2006 return error;
2007
2008 data = htole32(ic->ic_rtsthreshold);
2009 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2010 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
2011 if (error != 0)
2012 return error;
2013
2014 data = htole32(ic->ic_fragthreshold);
2015 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2016 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
2017 if (error != 0)
2018 return error;
2019
2020 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2021 power.mode = IWI_MODE_11B;
2022 power.nchan = 11;
2023 for (i = 0; i < 11; i++) {
2024 power.chan[i].chan = i + 1;
2025 power.chan[i].power = IWI_TXPOWER_MAX;
2026 }
2027 DPRINTF(("Setting .11b 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 power.mode = IWI_MODE_11G;
2034 DPRINTF(("Setting .11g channels tx power\n"));
2035 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2036 0);
2037 if (error != 0)
2038 return error;
2039 }
2040
2041 rs.mode = IWI_MODE_11G;
2042 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2043 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2044 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2045 rs.nrates);
2046 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2047 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2048 if (error != 0)
2049 return error;
2050
2051 rs.mode = IWI_MODE_11A;
2052 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2053 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2054 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2055 rs.nrates);
2056 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2057 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2058 if (error != 0)
2059 return error;
2060
2061 data = htole32(arc4random());
2062 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2063 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
2064 if (error != 0)
2065 return error;
2066
2067 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2068 wk = &ic->ic_crypto.cs_nw_keys[i];
2069
2070 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2071 wepkey.idx = i;
2072 wepkey.len = wk->wk_keylen;
2073 memset(wepkey.key, 0, sizeof wepkey.key);
2074 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2075 DPRINTF(("Setting wep key index %u len %u\n",
2076 wepkey.idx, wepkey.len));
2077 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2078 sizeof wepkey, 0);
2079 if (error != 0)
2080 return error;
2081 }
2082
2083 /* Enable adapter */
2084 DPRINTF(("Enabling adapter\n"));
2085 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
2086 }
2087
2088 static int
2089 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
2090 {
2091 struct ieee80211com *ic = &sc->sc_ic;
2092 struct iwi_scan scan;
2093
2094 (void)memset(&scan, 0, sizeof scan);
2095 scan.type = IWI_SCAN_TYPE_PASSIVE;
2096 scan.dwelltime = htole16(2000);
2097 scan.channels[0] = 1 | (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ :
2098 IWI_CHAN_2GHZ);
2099 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2100
2101 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
2102 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2103 }
2104
2105 static int
2106 iwi_scan(struct iwi_softc *sc)
2107 {
2108 struct ieee80211com *ic = &sc->sc_ic;
2109 struct iwi_scan_v2 scan;
2110 uint32_t type;
2111 uint8_t *p;
2112 int i, count, idx;
2113
2114 (void)memset(&scan, 0, sizeof scan);
2115 scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BROADCAST] =
2116 htole16(sc->dwelltime);
2117 scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BDIRECT] =
2118 htole16(sc->dwelltime);
2119
2120 /* tell the firmware about the desired essid */
2121 if (ic->ic_des_esslen) {
2122 int error;
2123
2124 DPRINTF(("%s: Setting adapter desired ESSID to %s\n",
2125 __func__, ic->ic_des_essid));
2126
2127 error = iwi_cmd(sc, IWI_CMD_SET_ESSID,
2128 ic->ic_des_essid, ic->ic_des_esslen, 1);
2129 if (error)
2130 return error;
2131
2132 type = IWI_SCAN_TYPE_ACTIVE_BDIRECT;
2133 } else {
2134 type = IWI_SCAN_TYPE_ACTIVE_BROADCAST;
2135 }
2136
2137 p = &scan.channels[0];
2138 count = idx = 0;
2139 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2140 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
2141 isset(ic->ic_chan_active, i)) {
2142 *++p = i;
2143 count++;
2144 idx++;
2145 iwi_scan_type_set(scan, idx, type);
2146 }
2147 }
2148 if (count) {
2149 *(p - count) = IWI_CHAN_5GHZ | count;
2150 p++;
2151 }
2152
2153 count = 0;
2154 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2155 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
2156 isset(ic->ic_chan_active, i)) {
2157 *++p = i;
2158 count++;
2159 idx++;
2160 iwi_scan_type_set(scan, idx, type);
2161 }
2162 }
2163 *(p - count) = IWI_CHAN_2GHZ | count;
2164
2165 DPRINTF(("Start scanning\n"));
2166 return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1);
2167 }
2168
2169 static int
2170 iwi_auth_and_assoc(struct iwi_softc *sc)
2171 {
2172 struct ieee80211com *ic = &sc->sc_ic;
2173 struct ieee80211_node *ni = ic->ic_bss;
2174 struct ifnet *ifp = &sc->sc_if;
2175 struct iwi_configuration config;
2176 struct iwi_associate assoc;
2177 struct iwi_rateset rs;
2178 uint16_t capinfo;
2179 uint32_t data;
2180 int error;
2181
2182 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2183 memset(&config, 0, sizeof config);
2184 config.bluetooth_coexistence = sc->bluetooth;
2185 config.antenna = sc->antenna;
2186 config.multicast_enabled = 1;
2187 config.use_protection = 1;
2188 config.answer_pbreq =
2189 (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2190 config.disable_unicast_decryption = 1;
2191 config.disable_multicast_decryption = 1;
2192 DPRINTF(("Configuring adapter\n"));
2193 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config,
2194 sizeof config, 1);
2195 if (error != 0)
2196 return error;
2197 }
2198
2199 #ifdef IWI_DEBUG
2200 if (iwi_debug > 0) {
2201 printf("Setting ESSID to ");
2202 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2203 printf("\n");
2204 }
2205 #endif
2206 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2207 if (error != 0)
2208 return error;
2209
2210 /* the rate set has already been "negotiated" */
2211 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2212 IWI_MODE_11G;
2213 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2214 rs.nrates = ni->ni_rates.rs_nrates;
2215 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2216 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
2217 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2218 if (error != 0)
2219 return error;
2220
2221 if (ic->ic_opt_ie != NULL) {
2222 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
2223 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
2224 ic->ic_opt_ie_len, 1);
2225 if (error != 0)
2226 return error;
2227 }
2228 data = htole32(ni->ni_rssi);
2229 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2230 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2231 if (error != 0)
2232 return error;
2233
2234 memset(&assoc, 0, sizeof assoc);
2235 assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2236 IWI_MODE_11G;
2237 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2238 if (ni->ni_authmode == IEEE80211_AUTH_SHARED)
2239 assoc.auth = (ic->ic_crypto.cs_def_txkey << 4) | IWI_AUTH_SHARED;
2240 if (ic->ic_opt_ie != NULL)
2241 assoc.policy |= htole16(IWI_POLICY_OPTIE);
2242 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
2243
2244 if (ic->ic_opmode == IEEE80211_M_IBSS)
2245 capinfo = IEEE80211_CAPINFO_IBSS;
2246 else
2247 capinfo = IEEE80211_CAPINFO_ESS;
2248 if (ic->ic_flags & IEEE80211_F_PRIVACY)
2249 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2250 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2251 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2252 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2253 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2254 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2255 assoc.capinfo = htole16(capinfo);
2256
2257 assoc.lintval = htole16(ic->ic_lintval);
2258 assoc.intval = htole16(ni->ni_intval);
2259 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
2260 if (ic->ic_opmode == IEEE80211_M_IBSS)
2261 IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr);
2262 else
2263 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
2264 DPRINTF(("Trying to associate to %s channel %u auth %u\n",
2265 ether_sprintf(assoc.bssid), assoc.chan, assoc.auth));
2266 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
2267 }
2268
2269 static int
2270 iwi_init(struct ifnet *ifp)
2271 {
2272 struct iwi_softc *sc = ifp->if_softc;
2273 struct ieee80211com *ic = &sc->sc_ic;
2274 struct iwi_firmware *fw = &sc->fw;
2275 int i, error;
2276
2277 /* exit immediately if firmware has not been ioctl'd */
2278 if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
2279 if (!(sc->flags & IWI_FLAG_FW_WARNED))
2280 aprint_error("%s: Firmware not loaded\n",
2281 sc->sc_dev.dv_xname);
2282 sc->flags |= IWI_FLAG_FW_WARNED;
2283 ifp->if_flags &= ~IFF_UP;
2284 return EIO;
2285 }
2286
2287 iwi_stop(ifp, 0);
2288
2289 if ((error = iwi_reset(sc)) != 0) {
2290 aprint_error("%s: could not reset adapter\n",
2291 sc->sc_dev.dv_xname);
2292 goto fail;
2293 }
2294
2295 if ((error = iwi_load_firmware(sc, fw->boot, fw->boot_size)) != 0) {
2296 aprint_error("%s: could not load boot firmware\n",
2297 sc->sc_dev.dv_xname);
2298 goto fail;
2299 }
2300
2301 if ((error = iwi_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) {
2302 aprint_error("%s: could not load microcode\n",
2303 sc->sc_dev.dv_xname);
2304 goto fail;
2305 }
2306
2307 iwi_stop_master(sc);
2308
2309 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.desc_map->dm_segs[0].ds_addr);
2310 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
2311 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
2312
2313 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
2314 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq.count);
2315 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur);
2316
2317 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
2318 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq.count);
2319 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq.cur);
2320
2321 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
2322 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq.count);
2323 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq.cur);
2324
2325 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
2326 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq.count);
2327 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq.cur);
2328
2329 for (i = 0; i < sc->rxq.count; i++)
2330 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4,
2331 sc->rxq.data[i].map->dm_segs[0].ds_addr);
2332
2333 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count -1);
2334
2335 if ((error = iwi_load_firmware(sc, fw->main, fw->main_size)) != 0) {
2336 aprint_error("%s: could not load main firmware\n",
2337 sc->sc_dev.dv_xname);
2338 goto fail;
2339 }
2340
2341 sc->flags |= IWI_FLAG_FW_INITED;
2342
2343 if ((error = iwi_config(sc)) != 0) {
2344 aprint_error("%s: device configuration failed\n",
2345 sc->sc_dev.dv_xname);
2346 goto fail;
2347 }
2348
2349 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2350 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2351 else
2352 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2353
2354 ifp->if_flags &= ~IFF_OACTIVE;
2355 ifp->if_flags |= IFF_RUNNING;
2356
2357 return 0;
2358
2359 fail: ifp->if_flags &= ~IFF_UP;
2360 iwi_stop(ifp, 0);
2361
2362 return error;
2363 }
2364
2365 static void
2366 iwi_stop(struct ifnet *ifp, int disable)
2367 {
2368 struct iwi_softc *sc = ifp->if_softc;
2369 struct ieee80211com *ic = &sc->sc_ic;
2370
2371 iwi_stop_master(sc);
2372 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
2373
2374 /* reset rings */
2375 iwi_reset_cmd_ring(sc, &sc->cmdq);
2376 iwi_reset_tx_ring(sc, &sc->txq);
2377 iwi_reset_rx_ring(sc, &sc->rxq);
2378
2379 ifp->if_timer = 0;
2380 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2381
2382 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2383 }
2384