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