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