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