]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/ipw/if_ipw.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / ipw / if_ipw.c
1 /*      $FreeBSD$       */
2
3 /*-
4  * Copyright (c) 2004-2006
5  *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6  * Copyright (c) 2006 Sam Leffler, Errno Consulting
7  * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 /*-
36  * Intel(R) PRO/Wireless 2100 MiniPCI driver
37  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
38  */
39
40 #include <sys/param.h>
41 #include <sys/sysctl.h>
42 #include <sys/sockio.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/queue.h>
49 #include <sys/taskqueue.h>
50 #include <sys/module.h>
51 #include <sys/bus.h>
52 #include <sys/endian.h>
53 #include <sys/linker.h>
54 #include <sys/firmware.h>
55
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58 #include <sys/rman.h>
59
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
62
63 #include <net/bpf.h>
64 #include <net/if.h>
65 #include <net/if_arp.h>
66 #include <net/ethernet.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69 #include <net/if_types.h>
70
71 #include <net80211/ieee80211_var.h>
72 #include <net80211/ieee80211_radiotap.h>
73
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip.h>
78 #include <netinet/if_ether.h>
79
80 #include <dev/ipw/if_ipwreg.h>
81 #include <dev/ipw/if_ipwvar.h>
82
83 #define IPW_DEBUG
84 #ifdef IPW_DEBUG
85 #define DPRINTF(x)      do { if (ipw_debug > 0) printf x; } while (0)
86 #define DPRINTFN(n, x)  do { if (ipw_debug >= (n)) printf x; } while (0)
87 int ipw_debug = 0;
88 SYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
89 #else
90 #define DPRINTF(x)
91 #define DPRINTFN(n, x)
92 #endif
93
94 MODULE_DEPEND(ipw, pci,  1, 1, 1);
95 MODULE_DEPEND(ipw, wlan, 1, 1, 1);
96 MODULE_DEPEND(ipw, firmware, 1, 1, 1);
97
98 struct ipw_ident {
99         uint16_t        vendor;
100         uint16_t        device;
101         const char      *name;
102 };
103
104 static const struct ipw_ident ipw_ident_table[] = {
105         { 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
106
107         { 0, 0, NULL }
108 };
109
110 static int      ipw_dma_alloc(struct ipw_softc *);
111 static void     ipw_release(struct ipw_softc *);
112 static int      ipw_media_change(struct ifnet *);
113 static void     ipw_media_status(struct ifnet *, struct ifmediareq *);
114 static int      ipw_newstate(struct ieee80211com *, enum ieee80211_state, int);
115 static uint16_t ipw_read_prom_word(struct ipw_softc *, uint8_t);
116 static void     ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
117 static void     ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
118 static void     ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
119                     struct ipw_soft_bd *, struct ipw_soft_buf *);
120 static void     ipw_rx_intr(struct ipw_softc *);
121 static void     ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
122 static void     ipw_tx_intr(struct ipw_softc *);
123 static void     ipw_intr(void *);
124 static void     ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
125 static const char * ipw_cmdname(int);
126 static int      ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
127 static int      ipw_tx_start(struct ifnet *, struct mbuf *,
128                     struct ieee80211_node *);
129 static void     ipw_start(struct ifnet *);
130 static void     ipw_start_locked(struct ifnet *);
131 static void     ipw_watchdog(void *);
132 static int      ipw_ioctl(struct ifnet *, u_long, caddr_t);
133 static void     ipw_stop_master(struct ipw_softc *);
134 static int      ipw_enable(struct ipw_softc *);
135 static int      ipw_disable(struct ipw_softc *);
136 static int      ipw_reset(struct ipw_softc *);
137 static int      ipw_load_ucode(struct ipw_softc *, const char *, int);
138 static int      ipw_load_firmware(struct ipw_softc *, const char *, int);
139 static int      ipw_config(struct ipw_softc *);
140 static void     ipw_assoc_task(void *, int);
141 static int      ipw_auth_and_assoc(struct ipw_softc *);
142 static void     ipw_disassoc_task(void *, int);
143 static int      ipw_disassociate(struct ipw_softc *);
144 static void     ipw_init_task(void *, int);
145 static void     ipw_init(void *);
146 static void     ipw_init_locked(struct ipw_softc *, int);
147 static void     ipw_stop(void *);
148 static void     ipw_stop_locked(struct ipw_softc *);
149 static int      ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
150 static int      ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
151 static uint32_t ipw_read_table1(struct ipw_softc *, uint32_t);
152 static void     ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
153 #if 0
154 static int      ipw_read_table2(struct ipw_softc *, uint32_t, void *,
155                     uint32_t *);
156 static void     ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
157                     bus_size_t);
158 #endif
159 static void     ipw_write_mem_1(struct ipw_softc *, bus_size_t,
160                     const uint8_t *, bus_size_t);
161 static void     ipw_scan_task(void *, int);
162 static int      ipw_scan(struct ipw_softc *);
163 static void     ipw_scan_start(struct ieee80211com *);
164 static void     ipw_scan_end(struct ieee80211com *);
165 static void     ipw_set_channel(struct ieee80211com *);
166 static void     ipw_scan_curchan(struct ieee80211com *, unsigned long maxdwell);
167 static void     ipw_scan_mindwell(struct ieee80211com *);
168
169 static int ipw_probe(device_t);
170 static int ipw_attach(device_t);
171 static int ipw_detach(device_t);
172 static int ipw_shutdown(device_t);
173 static int ipw_suspend(device_t);
174 static int ipw_resume(device_t);
175
176 static device_method_t ipw_methods[] = {
177         /* Device interface */
178         DEVMETHOD(device_probe,         ipw_probe),
179         DEVMETHOD(device_attach,        ipw_attach),
180         DEVMETHOD(device_detach,        ipw_detach),
181         DEVMETHOD(device_shutdown,      ipw_shutdown),
182         DEVMETHOD(device_suspend,       ipw_suspend),
183         DEVMETHOD(device_resume,        ipw_resume),
184
185         { 0, 0 }
186 };
187
188 static driver_t ipw_driver = {
189         "ipw",
190         ipw_methods,
191         sizeof (struct ipw_softc)
192 };
193
194 static devclass_t ipw_devclass;
195
196 DRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0);
197 DRIVER_MODULE(ipw, cardbus, ipw_driver, ipw_devclass, 0, 0);
198
199 static int
200 ipw_probe(device_t dev)
201 {
202         const struct ipw_ident *ident;
203
204         for (ident = ipw_ident_table; ident->name != NULL; ident++) {
205                 if (pci_get_vendor(dev) == ident->vendor &&
206                     pci_get_device(dev) == ident->device) {
207                         device_set_desc(dev, ident->name);
208                         return 0;
209                 }
210         }
211         return ENXIO;
212 }
213
214 /* Base Address Register */
215 #define IPW_PCI_BAR0    0x10
216
217 static int
218 ipw_attach(device_t dev)
219 {
220         struct ipw_softc *sc = device_get_softc(dev);
221         struct ifnet *ifp;
222         struct ieee80211com *ic = &sc->sc_ic;
223         struct ieee80211_channel *c;
224         uint16_t val;
225         int error, i;
226
227         sc->sc_dev = dev;
228
229         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
230             MTX_DEF | MTX_RECURSE);
231
232         TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
233         TASK_INIT(&sc->sc_scan_task, 0, ipw_scan_task, sc);
234         TASK_INIT(&sc->sc_assoc_task, 0, ipw_assoc_task, sc);
235         TASK_INIT(&sc->sc_disassoc_task, 0, ipw_disassoc_task, sc);
236         callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
237
238         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
239                 device_printf(dev, "chip is in D%d power mode "
240                     "-- setting to D0\n", pci_get_powerstate(dev));
241                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
242         }
243
244         pci_write_config(dev, 0x41, 0, 1);
245
246         /* enable bus-mastering */
247         pci_enable_busmaster(dev);
248
249         sc->mem_rid = IPW_PCI_BAR0;
250         sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
251             RF_ACTIVE);
252         if (sc->mem == NULL) {
253                 device_printf(dev, "could not allocate memory resource\n");
254                 goto fail;
255         }
256
257         sc->sc_st = rman_get_bustag(sc->mem);
258         sc->sc_sh = rman_get_bushandle(sc->mem);
259
260         sc->irq_rid = 0;
261         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
262             RF_ACTIVE | RF_SHAREABLE);
263         if (sc->irq == NULL) {
264                 device_printf(dev, "could not allocate interrupt resource\n");
265                 goto fail;
266         }
267
268         if (ipw_reset(sc) != 0) {
269                 device_printf(dev, "could not reset adapter\n");
270                 goto fail;
271         }
272
273         if (ipw_dma_alloc(sc) != 0) {
274                 device_printf(dev, "could not allocate DMA resources\n");
275                 goto fail;
276         }
277
278         ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
279         if (ifp == NULL) {
280                 device_printf(dev, "can not if_alloc()\n");
281                 goto fail;
282         }
283
284         ifp->if_softc = sc;
285         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
286         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
287         ifp->if_init = ipw_init;
288         ifp->if_ioctl = ipw_ioctl;
289         ifp->if_start = ipw_start;
290         IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
291         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
292         IFQ_SET_READY(&ifp->if_snd);
293
294         ic->ic_ifp = ifp;
295         ic->ic_phytype = IEEE80211_T_DS;
296         ic->ic_opmode = IEEE80211_M_STA;
297         ic->ic_state = IEEE80211_S_INIT;
298
299         /* set device capabilities */
300         ic->ic_caps = IEEE80211_C_IBSS          /* IBSS mode supported */
301                 | IEEE80211_C_MONITOR           /* monitor mode supported */
302                 | IEEE80211_C_PMGT              /* power save supported */
303                 | IEEE80211_C_SHPREAMBLE        /* short preamble supported */
304                 | IEEE80211_C_WPA               /* 802.11i supported */
305                 ;
306
307         /* read MAC address from EEPROM */
308         val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
309         ic->ic_myaddr[0] = val >> 8;
310         ic->ic_myaddr[1] = val & 0xff;
311         val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
312         ic->ic_myaddr[2] = val >> 8;
313         ic->ic_myaddr[3] = val & 0xff;
314         val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
315         ic->ic_myaddr[4] = val >> 8;
316         ic->ic_myaddr[5] = val & 0xff;
317
318         /* set supported .11b channels (read from EEPROM) */
319         if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
320                 val = 0x7ff; /* default to channels 1-11 */
321         val <<= 1;
322         for (i = 1; i < 16; i++) {
323                 if (val & (1 << i)) {
324                         c = &ic->ic_channels[ic->ic_nchans++];
325                         c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
326                         c->ic_flags = IEEE80211_CHAN_B;
327                         c->ic_ieee = i;
328                 }
329         }
330
331         /* check support for radio transmitter switch in EEPROM */
332         if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
333                 sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
334
335         ieee80211_ifattach(ic);
336         /* override state transition machine */
337         sc->sc_newstate = ic->ic_newstate;
338         ic->ic_newstate = ipw_newstate;
339         ieee80211_media_init(ic, ipw_media_change, ipw_media_status);
340
341         ic->ic_scan_start = ipw_scan_start;
342         ic->ic_scan_end = ipw_scan_end;
343         ic->ic_set_channel = ipw_set_channel;
344         ic->ic_scan_curchan = ipw_scan_curchan;
345         ic->ic_scan_mindwell = ipw_scan_mindwell;
346
347         bpfattach2(ifp, DLT_IEEE802_11_RADIO,
348             sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap),
349             &sc->sc_drvbpf);
350
351         sc->sc_rxtap_len = sizeof sc->sc_rxtap;
352         sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
353         sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
354
355         sc->sc_txtap_len = sizeof sc->sc_txtap;
356         sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
357         sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
358
359         /*
360          * Add a few sysctl knobs.
361          */
362         sc->dwelltime = 100;
363
364         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
365             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
366             CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
367             "radio transmitter switch state (0=off, 1=on)");
368
369         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
370             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
371             CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
372             "statistics");
373
374         SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
375             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "dwell",
376             CTLFLAG_RW, &sc->dwelltime, 0,
377             "channel dwell time (ms) for AP/station scanning");
378
379         /*
380          * Hook our interrupt after all initialization is complete.
381          */
382         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
383             NULL, ipw_intr, sc, &sc->sc_ih);
384         if (error != 0) {
385                 device_printf(dev, "could not set up interrupt\n");
386                 goto fail;
387         }
388
389         if (bootverbose)
390                 ieee80211_announce(ic);
391
392         return 0;
393
394 fail:   ipw_detach(dev);
395         return ENXIO;
396 }
397
398 static int
399 ipw_detach(device_t dev)
400 {
401         struct ipw_softc *sc = device_get_softc(dev);
402         struct ieee80211com *ic = &sc->sc_ic;
403         struct ifnet *ifp = ic->ic_ifp;
404
405         ipw_stop(sc);
406         callout_drain(&sc->sc_wdtimer);
407         taskqueue_drain(taskqueue_fast, &sc->sc_init_task);
408         taskqueue_drain(taskqueue_fast, &sc->sc_scan_task);
409         taskqueue_drain(taskqueue_fast, &sc->sc_assoc_task);
410         taskqueue_drain(taskqueue_fast, &sc->sc_disassoc_task);
411
412         if (ifp != NULL) {
413                 bpfdetach(ifp);
414                 ieee80211_ifdetach(ic);
415         }
416
417         ipw_release(sc);
418
419         if (sc->irq != NULL) {
420                 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
421                 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
422         }
423
424         if (sc->mem != NULL)
425                 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
426
427         if (ifp != NULL)
428                 if_free(ifp);
429
430         if (sc->sc_firmware != NULL) {
431                 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
432                 sc->sc_firmware = NULL;
433         }
434
435         mtx_destroy(&sc->sc_mtx);
436
437         return 0;
438 }
439
440 static int
441 ipw_dma_alloc(struct ipw_softc *sc)
442 {
443         struct ipw_soft_bd *sbd;
444         struct ipw_soft_hdr *shdr;
445         struct ipw_soft_buf *sbuf;
446         bus_addr_t physaddr;
447         int error, i;
448
449         /*
450          * Allocate and map tx ring.
451          */
452         error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
453             BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
454             NULL, &sc->tbd_dmat);
455         if (error != 0) {
456                 device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
457                 goto fail;
458         }
459
460         error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
461             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
462         if (error != 0) {
463                 device_printf(sc->sc_dev,
464                     "could not allocate tx ring DMA memory\n");
465                 goto fail;
466         }
467
468         error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
469             IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
470         if (error != 0) {
471                 device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
472                 goto fail;
473         }
474
475         /*
476          * Allocate and map rx ring.
477          */
478         error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
479             BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
480             NULL, &sc->rbd_dmat);
481         if (error != 0) {
482                 device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
483                 goto fail;
484         }
485
486         error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
487             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
488         if (error != 0) {
489                 device_printf(sc->sc_dev,
490                     "could not allocate rx ring DMA memory\n");
491                 goto fail;
492         }
493
494         error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
495             IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
496         if (error != 0) {
497                 device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
498                 goto fail;
499         }
500
501         /*
502          * Allocate and map status ring.
503          */
504         error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
505             BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
506             NULL, NULL, &sc->status_dmat);
507         if (error != 0) {
508                 device_printf(sc->sc_dev,
509                     "could not create status ring DMA tag\n");
510                 goto fail;
511         }
512
513         error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
514             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
515         if (error != 0) {
516                 device_printf(sc->sc_dev,
517                     "could not allocate status ring DMA memory\n");
518                 goto fail;
519         }
520
521         error = bus_dmamap_load(sc->status_dmat, sc->status_map,
522             sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
523             0);
524         if (error != 0) {
525                 device_printf(sc->sc_dev,
526                     "could not map status ring DMA memory\n");
527                 goto fail;
528         }
529
530         /*
531          * Allocate command DMA map.
532          */
533         error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
534             BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
535             sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
536         if (error != 0) {
537                 device_printf(sc->sc_dev, "could not create command DMA tag\n");
538                 goto fail;
539         }
540
541         error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
542         if (error != 0) {
543                 device_printf(sc->sc_dev,
544                     "could not create command DMA map\n");
545                 goto fail;
546         }
547
548         /*
549          * Allocate headers DMA maps.
550          */
551         error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
552             BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
553             sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
554         if (error != 0) {
555                 device_printf(sc->sc_dev, "could not create header DMA tag\n");
556                 goto fail;
557         }
558
559         SLIST_INIT(&sc->free_shdr);
560         for (i = 0; i < IPW_NDATA; i++) {
561                 shdr = &sc->shdr_list[i];
562                 error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
563                 if (error != 0) {
564                         device_printf(sc->sc_dev,
565                             "could not create header DMA map\n");
566                         goto fail;
567                 }
568                 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
569         }
570
571         /*
572          * Allocate tx buffers DMA maps.
573          */
574         error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
575             BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
576             NULL, NULL, &sc->txbuf_dmat);
577         if (error != 0) {
578                 device_printf(sc->sc_dev, "could not create tx DMA tag\n");
579                 goto fail;
580         }
581
582         SLIST_INIT(&sc->free_sbuf);
583         for (i = 0; i < IPW_NDATA; i++) {
584                 sbuf = &sc->tx_sbuf_list[i];
585                 error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
586                 if (error != 0) {
587                         device_printf(sc->sc_dev,
588                             "could not create tx DMA map\n");
589                         goto fail;
590                 }
591                 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
592         }
593
594         /*
595          * Initialize tx ring.
596          */
597         for (i = 0; i < IPW_NTBD; i++) {
598                 sbd = &sc->stbd_list[i];
599                 sbd->bd = &sc->tbd_list[i];
600                 sbd->type = IPW_SBD_TYPE_NOASSOC;
601         }
602
603         /*
604          * Pre-allocate rx buffers and DMA maps.
605          */
606         error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
607             BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
608             NULL, &sc->rxbuf_dmat);
609         if (error != 0) {
610                 device_printf(sc->sc_dev, "could not create rx DMA tag\n");
611                 goto fail;
612         }
613
614         for (i = 0; i < IPW_NRBD; i++) {
615                 sbd = &sc->srbd_list[i];
616                 sbuf = &sc->rx_sbuf_list[i];
617                 sbd->bd = &sc->rbd_list[i];
618
619                 sbuf->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
620                 if (sbuf->m == NULL) {
621                         device_printf(sc->sc_dev,
622                             "could not allocate rx mbuf\n");
623                         error = ENOMEM;
624                         goto fail;
625                 }
626
627                 error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
628                 if (error != 0) {
629                         device_printf(sc->sc_dev,
630                             "could not create rx DMA map\n");
631                         goto fail;
632                 }
633
634                 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
635                     mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
636                     &physaddr, 0);
637                 if (error != 0) {
638                         device_printf(sc->sc_dev,
639                             "could not map rx DMA memory\n");
640                         goto fail;
641                 }
642
643                 sbd->type = IPW_SBD_TYPE_DATA;
644                 sbd->priv = sbuf;
645                 sbd->bd->physaddr = htole32(physaddr);
646                 sbd->bd->len = htole32(MCLBYTES);
647         }
648
649         bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
650
651         return 0;
652
653 fail:   ipw_release(sc);
654         return error;
655 }
656
657 static void
658 ipw_release(struct ipw_softc *sc)
659 {
660         struct ipw_soft_buf *sbuf;
661         int i;
662
663         if (sc->tbd_dmat != NULL) {
664                 if (sc->stbd_list != NULL) {
665                         bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
666                         bus_dmamem_free(sc->tbd_dmat, sc->tbd_list,
667                             sc->tbd_map);
668                 }
669                 bus_dma_tag_destroy(sc->tbd_dmat);
670         }
671
672         if (sc->rbd_dmat != NULL) {
673                 if (sc->rbd_list != NULL) {
674                         bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
675                         bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
676                             sc->rbd_map);
677                 }
678                 bus_dma_tag_destroy(sc->rbd_dmat);
679         }
680
681         if (sc->status_dmat != NULL) {
682                 if (sc->status_list != NULL) {
683                         bus_dmamap_unload(sc->status_dmat, sc->status_map);
684                         bus_dmamem_free(sc->status_dmat, sc->status_list,
685                             sc->status_map);
686                 }
687                 bus_dma_tag_destroy(sc->status_dmat);
688         }
689
690         for (i = 0; i < IPW_NTBD; i++)
691                 ipw_release_sbd(sc, &sc->stbd_list[i]);
692
693         if (sc->cmd_dmat != NULL) {
694                 bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
695                 bus_dma_tag_destroy(sc->cmd_dmat);
696         }
697
698         if (sc->hdr_dmat != NULL) {
699                 for (i = 0; i < IPW_NDATA; i++)
700                         bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
701                 bus_dma_tag_destroy(sc->hdr_dmat);
702         }
703
704         if (sc->txbuf_dmat != NULL) {
705                 for (i = 0; i < IPW_NDATA; i++) {
706                         bus_dmamap_destroy(sc->txbuf_dmat,
707                             sc->tx_sbuf_list[i].map);
708                 }
709                 bus_dma_tag_destroy(sc->txbuf_dmat);
710         }
711
712         if (sc->rxbuf_dmat != NULL) {
713                 for (i = 0; i < IPW_NRBD; i++) {
714                         sbuf = &sc->rx_sbuf_list[i];
715                         if (sbuf->m != NULL) {
716                                 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
717                                     BUS_DMASYNC_POSTREAD);
718                                 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
719                                 m_freem(sbuf->m);
720                         }
721                         bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
722                 }
723                 bus_dma_tag_destroy(sc->rxbuf_dmat);
724         }
725 }
726
727 static int
728 ipw_shutdown(device_t dev)
729 {
730         struct ipw_softc *sc = device_get_softc(dev);
731
732         ipw_stop(sc);
733
734         return 0;
735 }
736
737 static int
738 ipw_suspend(device_t dev)
739 {
740         struct ipw_softc *sc = device_get_softc(dev);
741
742         ipw_stop(sc);
743
744         return 0;
745 }
746
747 static int
748 ipw_resume(device_t dev)
749 {
750         struct ipw_softc *sc = device_get_softc(dev);
751         struct ifnet *ifp = sc->sc_ic.ic_ifp;
752         IPW_LOCK_DECL;
753
754         IPW_LOCK(sc);
755
756         pci_write_config(dev, 0x41, 0, 1);
757
758         if (ifp->if_flags & IFF_UP) {
759                 ipw_init_locked(sc, 0);
760                 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
761                         ipw_start_locked(ifp);
762         }
763
764         IPW_UNLOCK(sc);
765
766         return 0;
767 }
768
769 static int
770 ipw_media_change(struct ifnet *ifp)
771 {
772         struct ipw_softc *sc = ifp->if_softc;
773         int error;
774         IPW_LOCK_DECL;
775
776         IPW_LOCK(sc);
777         error = ieee80211_media_change(ifp);
778         if (error == ENETRESET) {
779                 if ((ifp->if_flags & IFF_UP) &&
780                     (ifp->if_drv_flags & IFF_DRV_RUNNING))
781                         ipw_init_locked(sc, 0);
782                 error = 0;
783         }
784         IPW_UNLOCK(sc);
785
786         return (error);
787 }
788
789 static int
790 ipw_cvtrate(int ipwrate)
791 {
792         switch (ipwrate) {
793         case IPW_RATE_DS1:      return 2;
794         case IPW_RATE_DS2:      return 4;
795         case IPW_RATE_DS5:      return 11;
796         case IPW_RATE_DS11:     return 22;
797         }
798         return 0;
799 }
800
801 /*
802  * The firmware automatically adapts the transmit speed. We report its current
803  * value here.
804  */
805 static void
806 ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
807 {
808         struct ipw_softc *sc = ifp->if_softc;
809         struct ieee80211com *ic = &sc->sc_ic;
810         int rate;
811
812         imr->ifm_status = IFM_AVALID;
813         imr->ifm_active = IFM_IEEE80211;
814         if (ic->ic_state == IEEE80211_S_RUN)
815                 imr->ifm_status |= IFM_ACTIVE;
816
817         /* read current transmission rate from adapter */
818         rate = ipw_cvtrate(ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
819         imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
820
821         switch (ic->ic_opmode) {
822         case IEEE80211_M_STA:
823                 break;
824
825         case IEEE80211_M_IBSS:
826                 imr->ifm_active |= IFM_IEEE80211_IBSS;
827                 break;
828
829         case IEEE80211_M_MONITOR:
830                 imr->ifm_active |= IFM_IEEE80211_MONITOR;
831                 break;
832
833         case IEEE80211_M_AHDEMO:
834         case IEEE80211_M_HOSTAP:
835         case IEEE80211_M_WDS:
836                 /* should not get there */
837                 break;
838         }
839 }
840
841 static int
842 ipw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
843 {
844         struct ifnet *ifp = ic->ic_ifp;
845         struct ipw_softc *sc = ifp->if_softc;
846
847         DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
848                 ieee80211_state_name[ic->ic_state],
849                 ieee80211_state_name[nstate], sc->flags));
850
851         switch (nstate) {
852         case IEEE80211_S_RUN:
853                 if (ic->ic_opmode == IEEE80211_M_IBSS) {
854                         /*
855                          * XXX when joining an ibss network we are called
856                          * with a SCAN -> RUN transition on scan complete.
857                          * Use that to call ipw_auth_and_assoc.  On completing
858                          * the join we are then called again with an
859                          * AUTH -> RUN transition and we want to do nothing.
860                          * This is all totally bogus and needs to be redone.
861                          */
862                         if (ic->ic_state == IEEE80211_S_SCAN)
863                                 taskqueue_enqueue_fast(taskqueue_fast,
864                                     &sc->sc_assoc_task);
865                 }
866                 break;
867
868         case IEEE80211_S_INIT:
869                 if (sc->flags & IPW_FLAG_ASSOCIATED)
870                         taskqueue_enqueue_fast(taskqueue_fast,
871                             &sc->sc_disassoc_task);
872                 break;
873
874         case IEEE80211_S_AUTH:
875                 taskqueue_enqueue_fast(taskqueue_fast, &sc->sc_assoc_task);
876                 break;
877
878         case IEEE80211_S_ASSOC:
879                 /*
880                  * If we are not transitioning from AUTH the resend the
881                  * association request.
882                  */
883                 if (ic->ic_state != IEEE80211_S_AUTH)
884                         taskqueue_enqueue_fast(taskqueue_fast,
885                             &sc->sc_assoc_task);
886                 break;
887
888         default:
889                 break;
890         }
891         return (*sc->sc_newstate)(ic, nstate, arg);
892 }
893
894 /*
895  * Read 16 bits at address 'addr' from the serial EEPROM.
896  */
897 static uint16_t
898 ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
899 {
900         uint32_t tmp;
901         uint16_t val;
902         int n;
903
904         /* clock C once before the first command */
905         IPW_EEPROM_CTL(sc, 0);
906         IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
907         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
908         IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
909
910         /* write start bit (1) */
911         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
912         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
913
914         /* write READ opcode (10) */
915         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
916         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
917         IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
918         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
919
920         /* write address A7-A0 */
921         for (n = 7; n >= 0; n--) {
922                 IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
923                     (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
924                 IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
925                     (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
926         }
927
928         IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
929
930         /* read data Q15-Q0 */
931         val = 0;
932         for (n = 15; n >= 0; n--) {
933                 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
934                 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
935                 tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
936                 val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
937         }
938
939         IPW_EEPROM_CTL(sc, 0);
940
941         /* clear Chip Select and clock C */
942         IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
943         IPW_EEPROM_CTL(sc, 0);
944         IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
945
946         return le16toh(val);
947 }
948
949 static void
950 ipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
951 {
952         struct ipw_cmd *cmd;
953
954         bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
955
956         cmd = mtod(sbuf->m, struct ipw_cmd *);
957
958         DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
959             ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
960             le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
961             le32toh(cmd->status)));
962
963         sc->flags &= ~IPW_FLAG_BUSY;
964         wakeup(sc);
965 }
966
967 static void
968 ipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
969 {
970 #define IEEESTATE(ic)   ieee80211_state_name[ic->ic_state]
971         struct ieee80211com *ic = &sc->sc_ic;
972         uint32_t state;
973
974         bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
975
976         state = le32toh(*mtod(sbuf->m, uint32_t *));
977
978         switch (state) {
979         case IPW_STATE_ASSOCIATED:
980                 DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
981                         IEEESTATE(ic), sc->flags));
982                 sc->flags |= IPW_FLAG_ASSOCIATED;
983                 /* XXX suppress state change in case the fw auto-associates */
984                 if (ic->ic_state != IEEE80211_S_ASSOC) {
985                         DPRINTF(("Unexpected association (state %u)\n",
986                                 ic->ic_state));
987                 } else
988                         ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
989                 break;
990
991         case IPW_STATE_SCANNING:
992                 DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
993                         IEEESTATE(ic), sc->flags));
994                 /*
995                  * NB: Check driver state for association on assoc
996                  * loss as the firmware will immediately start to
997                  * scan and we would treat it as a beacon miss if
998                  * we checked the 802.11 layer state.
999                  */
1000                 if (sc->flags & IPW_FLAG_ASSOCIATED)
1001                         ieee80211_beacon_miss(ic);
1002                 break;
1003
1004         case IPW_STATE_SCAN_COMPLETE:
1005                 /*
1006                  * XXX For some reason scan requests generate scan
1007                  * started + scan done events before any traffic is
1008                  * received (e.g. probe response frames).  We work
1009                  * around this by marking the HACK flag and skipping
1010                  * the first scan complete event.
1011                 */
1012                 if (sc->flags & IPW_FLAG_HACK) {
1013                         sc->flags &= ~IPW_FLAG_HACK;
1014                         break;
1015                 }
1016                 DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1017                             IEEESTATE(ic), sc->flags));
1018                 if (sc->flags & IPW_FLAG_SCANNING) {
1019                         ieee80211_scan_done(ic);
1020                         sc->flags &= ~IPW_FLAG_SCANNING;
1021                         sc->sc_scan_timer = 0;
1022                 }
1023                 break;
1024
1025         case IPW_STATE_ASSOCIATION_LOST:
1026                 DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1027                         IEEESTATE(ic), sc->flags));
1028                 sc->flags &= ~IPW_FLAG_ASSOCIATED;
1029                 if (ic->ic_state == IEEE80211_S_RUN)
1030                         ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1031                 break;
1032
1033         case IPW_STATE_DISABLED:
1034                 DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1035                         IEEESTATE(ic), sc->flags));
1036                 break;
1037
1038         case IPW_STATE_RADIO_DISABLED:
1039                 DPRINTFN(2, ("Radio off (%s flags 0x%x)\n",
1040                         IEEESTATE(ic), sc->flags));
1041                 ic->ic_ifp->if_flags &= ~IFF_UP;
1042                 ipw_stop_locked(sc);
1043                 break;
1044
1045         default:
1046                 DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1047                         __func__, state, IEEESTATE(ic), sc->flags));
1048                 break;
1049         }
1050 #undef IEEESTATE
1051 }
1052
1053 /*
1054  * Set driver state for current channel.
1055  */
1056 static void
1057 ipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1058 {
1059         struct ieee80211com *ic = &sc->sc_ic;
1060
1061         ic->ic_curchan = chan;
1062         sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
1063                 htole16(ic->ic_curchan->ic_freq);
1064         sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
1065                 htole16(ic->ic_curchan->ic_flags);
1066 }
1067
1068 /*
1069  * XXX: Hack to set the current channel to the value advertised in beacons or
1070  * probe responses. Only used during AP detection.
1071  */
1072 static void
1073 ipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1074 {
1075         struct ieee80211com *ic = &sc->sc_ic;
1076         struct ieee80211_channel *c;
1077         struct ieee80211_frame *wh;
1078         uint8_t subtype;
1079         uint8_t *frm, *efrm;
1080
1081         wh = mtod(m, struct ieee80211_frame *);
1082
1083         if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1084                 return;
1085
1086         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1087
1088         if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1089             subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1090                 return;
1091
1092         frm = (uint8_t *)(wh + 1);
1093         efrm = mtod(m, uint8_t *) + m->m_len;
1094
1095         frm += 12;      /* skip tstamp, bintval and capinfo fields */
1096         while (frm < efrm) {
1097                 if (*frm == IEEE80211_ELEMID_DSPARMS)
1098 #if IEEE80211_CHAN_MAX < 255
1099                 if (frm[2] <= IEEE80211_CHAN_MAX)
1100 #endif
1101                 {
1102                         DPRINTF(("Fixing channel to %d\n", frm[2]));
1103                         c = ieee80211_find_channel(ic,
1104                                 ieee80211_ieee2mhz(frm[2], 0),
1105                                 IEEE80211_CHAN_B);
1106                         if (c == NULL)
1107                                 c = &ic->ic_channels[0];
1108                         ipw_setcurchan(sc, c);
1109                 }
1110
1111                 frm += frm[1] + 2;
1112         }
1113 }
1114
1115 static void
1116 ipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1117     struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1118 {
1119         struct ieee80211com *ic = &sc->sc_ic;
1120         struct ifnet *ifp = ic->ic_ifp;
1121         struct mbuf *mnew, *m;
1122         struct ieee80211_frame *wh;
1123         struct ieee80211_node *ni;
1124         bus_addr_t physaddr;
1125         int error;
1126         IPW_LOCK_DECL;
1127
1128         DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1129             status->rssi));
1130
1131         if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1132             le32toh(status->len) > MCLBYTES)
1133                 return;
1134
1135         /*
1136          * Try to allocate a new mbuf for this ring element and load it before
1137          * processing the current mbuf. If the ring element cannot be loaded,
1138          * drop the received packet and reuse the old mbuf. In the unlikely
1139          * case that the old mbuf can't be reloaded either, explicitly panic.
1140          */
1141         mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1142         if (mnew == NULL) {
1143                 ifp->if_ierrors++;
1144                 return;
1145         }
1146
1147         bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1148         bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1149
1150         error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1151             MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1152         if (error != 0) {
1153                 m_freem(mnew);
1154
1155                 /* try to reload the old mbuf */
1156                 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1157                     mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1158                     &physaddr, 0);
1159                 if (error != 0) {
1160                         /* very unlikely that it will fail... */
1161                         panic("%s: could not load old rx mbuf",
1162                             device_get_name(sc->sc_dev));
1163                 }
1164                 ifp->if_ierrors++;
1165                 return;
1166         }
1167
1168         /*
1169          * New mbuf successfully loaded, update Rx ring and continue
1170          * processing.
1171          */
1172         m = sbuf->m;
1173         sbuf->m = mnew;
1174         sbd->bd->physaddr = htole32(physaddr);
1175
1176         /* finalize mbuf */
1177         m->m_pkthdr.rcvif = ifp;
1178         m->m_pkthdr.len = m->m_len = le32toh(status->len);
1179
1180         if (bpf_peers_present(sc->sc_drvbpf)) {
1181                 struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1182
1183                 tap->wr_flags = 0;
1184                 tap->wr_antsignal = status->rssi + IPW_RSSI_TO_DBM;
1185                 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1186                 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1187
1188                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1189         }
1190
1191         if (sc->flags & IPW_FLAG_SCANNING)
1192                 ipw_fix_channel(sc, m);
1193
1194         wh = mtod(m, struct ieee80211_frame *);
1195         IPW_UNLOCK(sc);
1196         ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1197
1198         /* send the frame to the 802.11 layer */
1199         ieee80211_input(ic, m, ni, status->rssi, -95/*XXX*/, 0);
1200
1201         /* node is no longer needed */
1202         ieee80211_free_node(ni);
1203         IPW_LOCK(sc);
1204
1205         bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1206 }
1207
1208 static void
1209 ipw_rx_intr(struct ipw_softc *sc)
1210 {
1211         struct ieee80211com *ic = &sc->sc_ic;
1212         struct ipw_status *status;
1213         struct ipw_soft_bd *sbd;
1214         struct ipw_soft_buf *sbuf;
1215         uint32_t r, i;
1216
1217         if (!(sc->flags & IPW_FLAG_FW_INITED))
1218                 return;
1219
1220         r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1221
1222         bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1223
1224         for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1225                 status = &sc->status_list[i];
1226                 sbd = &sc->srbd_list[i];
1227                 sbuf = sbd->priv;
1228
1229                 switch (le16toh(status->code) & 0xf) {
1230                 case IPW_STATUS_CODE_COMMAND:
1231                         ipw_rx_cmd_intr(sc, sbuf);
1232                         break;
1233
1234                 case IPW_STATUS_CODE_NEWSTATE:
1235                         ipw_rx_newstate_intr(sc, sbuf);
1236                         break;
1237
1238                 case IPW_STATUS_CODE_DATA_802_3:
1239                 case IPW_STATUS_CODE_DATA_802_11:
1240                         ipw_rx_data_intr(sc, status, sbd, sbuf);
1241                         break;
1242
1243                 case IPW_STATUS_CODE_NOTIFICATION:
1244                         DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1245                             le32toh(status->len), status->flags));
1246                         if (ic->ic_state == IEEE80211_S_AUTH) {
1247                                 /* XXX assume auth notification */
1248                                 ieee80211_node_authorize(ic->ic_bss);
1249                                 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1250                         }
1251                         break;
1252
1253                 default:
1254                         device_printf(sc->sc_dev, "unexpected status code %u\n",
1255                             le16toh(status->code));
1256                 }
1257
1258                 /* firmware was killed, stop processing received frames */
1259                 if (!(sc->flags & IPW_FLAG_FW_INITED))
1260                         return;
1261
1262                 sbd->bd->flags = 0;
1263         }
1264
1265         bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1266
1267         /* kick the firmware */
1268         sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1269         CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1270 }
1271
1272 static void
1273 ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1274 {
1275         struct ipw_soft_hdr *shdr;
1276         struct ipw_soft_buf *sbuf;
1277
1278         switch (sbd->type) {
1279         case IPW_SBD_TYPE_COMMAND:
1280                 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1281                     BUS_DMASYNC_POSTWRITE);
1282                 bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1283                 break;
1284
1285         case IPW_SBD_TYPE_HEADER:
1286                 shdr = sbd->priv;
1287                 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1288                 bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1289                 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1290                 break;
1291
1292         case IPW_SBD_TYPE_DATA:
1293                 sbuf = sbd->priv;
1294                 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1295                     BUS_DMASYNC_POSTWRITE);
1296                 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1297                 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1298
1299                 if (sbuf->m->m_flags & M_TXCB)
1300                         ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1301                 m_freem(sbuf->m);
1302                 ieee80211_free_node(sbuf->ni);
1303
1304                 sc->sc_tx_timer = 0;
1305                 break;
1306         }
1307
1308         sbd->type = IPW_SBD_TYPE_NOASSOC;
1309 }
1310
1311 static void
1312 ipw_tx_intr(struct ipw_softc *sc)
1313 {
1314         struct ifnet *ifp = sc->sc_ic.ic_ifp;
1315         struct ipw_soft_bd *sbd;
1316         uint32_t r, i;
1317
1318         if (!(sc->flags & IPW_FLAG_FW_INITED))
1319                 return;
1320
1321         r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1322
1323         for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1324                 sbd = &sc->stbd_list[i];
1325
1326                 if (sbd->type == IPW_SBD_TYPE_DATA)
1327                         ifp->if_opackets++;
1328
1329                 ipw_release_sbd(sc, sbd);
1330                 sc->txfree++;
1331         }
1332
1333         /* remember what the firmware has processed */
1334         sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1335
1336         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1337         ipw_start_locked(ifp);
1338 }
1339
1340 static void
1341 ipw_intr(void *arg)
1342 {
1343         struct ipw_softc *sc = arg;
1344         uint32_t r;
1345         IPW_LOCK_DECL;
1346
1347         IPW_LOCK(sc);
1348
1349         if ((r = CSR_READ_4(sc, IPW_CSR_INTR)) == 0 || r == 0xffffffff) {
1350                 IPW_UNLOCK(sc);
1351                 return;
1352         }
1353
1354         /* disable interrupts */
1355         CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1356
1357         /* acknowledge all interrupts */
1358         CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1359
1360         if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1361                 device_printf(sc->sc_dev, "firmware error\n");
1362                 taskqueue_enqueue_fast(taskqueue_fast, &sc->sc_init_task);
1363                 r = 0;  /* don't process more interrupts */
1364         }
1365
1366         if (r & IPW_INTR_FW_INIT_DONE)
1367                 wakeup(sc);
1368
1369         if (r & IPW_INTR_RX_TRANSFER)
1370                 ipw_rx_intr(sc);
1371
1372         if (r & IPW_INTR_TX_TRANSFER)
1373                 ipw_tx_intr(sc);
1374
1375         /* re-enable interrupts */
1376         CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1377
1378         IPW_UNLOCK(sc);
1379 }
1380
1381 static void
1382 ipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1383 {
1384         if (error != 0)
1385                 return;
1386
1387         KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1388
1389         *(bus_addr_t *)arg = segs[0].ds_addr;
1390 }
1391
1392 static const char *
1393 ipw_cmdname(int cmd)
1394 {
1395 #define N(a)    (sizeof(a) / sizeof(a[0]))
1396         static const struct {
1397                 int     cmd;
1398                 const char *name;
1399         } cmds[] = {
1400                 { IPW_CMD_ADD_MULTICAST,        "ADD_MULTICAST" },
1401                 { IPW_CMD_BROADCAST_SCAN,       "BROADCAST_SCAN" },
1402                 { IPW_CMD_DISABLE,              "DISABLE" },
1403                 { IPW_CMD_DISABLE_PHY,          "DISABLE_PHY" },
1404                 { IPW_CMD_ENABLE,               "ENABLE" },
1405                 { IPW_CMD_PREPARE_POWER_DOWN,   "PREPARE_POWER_DOWN" },
1406                 { IPW_CMD_SET_BASIC_TX_RATES,   "SET_BASIC_TX_RATES" },
1407                 { IPW_CMD_SET_BEACON_INTERVAL,  "SET_BEACON_INTERVAL" },
1408                 { IPW_CMD_SET_CHANNEL,          "SET_CHANNEL" },
1409                 { IPW_CMD_SET_CONFIGURATION,    "SET_CONFIGURATION" },
1410                 { IPW_CMD_SET_DESIRED_BSSID,    "SET_DESIRED_BSSID" },
1411                 { IPW_CMD_SET_ESSID,            "SET_ESSID" },
1412                 { IPW_CMD_SET_FRAG_THRESHOLD,   "SET_FRAG_THRESHOLD" },
1413                 { IPW_CMD_SET_MAC_ADDRESS,      "SET_MAC_ADDRESS" },
1414                 { IPW_CMD_SET_MANDATORY_BSSID,  "SET_MANDATORY_BSSID" },
1415                 { IPW_CMD_SET_MODE,             "SET_MODE" },
1416                 { IPW_CMD_SET_MSDU_TX_RATES,    "SET_MSDU_TX_RATES" },
1417                 { IPW_CMD_SET_POWER_MODE,       "SET_POWER_MODE" },
1418                 { IPW_CMD_SET_RTS_THRESHOLD,    "SET_RTS_THRESHOLD" },
1419                 { IPW_CMD_SET_SCAN_OPTIONS,     "SET_SCAN_OPTIONS" },
1420                 { IPW_CMD_SET_SECURITY_INFO,    "SET_SECURITY_INFO" },
1421                 { IPW_CMD_SET_TX_POWER_INDEX,   "SET_TX_POWER_INDEX" },
1422                 { IPW_CMD_SET_TX_RATES,         "SET_TX_RATES" },
1423                 { IPW_CMD_SET_WEP_FLAGS,        "SET_WEP_FLAGS" },
1424                 { IPW_CMD_SET_WEP_KEY,          "SET_WEP_KEY" },
1425                 { IPW_CMD_SET_WEP_KEY_INDEX,    "SET_WEP_KEY_INDEX" },
1426                 { IPW_CMD_SET_WPA_IE,           "SET_WPA_IE" },
1427
1428         };
1429         static char buf[12];
1430         int i;
1431
1432         for (i = 0; i < N(cmds); i++)
1433                 if (cmds[i].cmd == cmd)
1434                         return cmds[i].name;
1435         snprintf(buf, sizeof(buf), "%u", cmd);
1436         return buf;
1437 #undef N
1438 }
1439
1440 /*
1441  * Send a command to the firmware and wait for the acknowledgement.
1442  */
1443 static int
1444 ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1445 {
1446         struct ipw_soft_bd *sbd;
1447         bus_addr_t physaddr;
1448         int error;
1449
1450         if (sc->flags & IPW_FLAG_BUSY) {
1451                 device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1452                         __func__, ipw_cmdname(type));
1453                 return EAGAIN;
1454         }
1455         sc->flags |= IPW_FLAG_BUSY;
1456
1457         sbd = &sc->stbd_list[sc->txcur];
1458
1459         error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1460             sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1461         if (error != 0) {
1462                 device_printf(sc->sc_dev, "could not map command DMA memory\n");
1463                 sc->flags &= ~IPW_FLAG_BUSY;
1464                 return error;
1465         }
1466
1467         sc->cmd.type = htole32(type);
1468         sc->cmd.subtype = 0;
1469         sc->cmd.len = htole32(len);
1470         sc->cmd.seq = 0;
1471         memcpy(sc->cmd.data, data, len);
1472
1473         sbd->type = IPW_SBD_TYPE_COMMAND;
1474         sbd->bd->physaddr = htole32(physaddr);
1475         sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1476         sbd->bd->nfrag = 1;
1477         sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1478             IPW_BD_FLAG_TX_LAST_FRAGMENT;
1479
1480         bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1481         bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1482
1483 #ifdef IPW_DEBUG
1484         if (ipw_debug >= 4) {
1485                 printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1486                     0, 0, len);
1487                 /* Print the data buffer in the higher debug level */
1488                 if (ipw_debug >= 9 && len > 0) {
1489                         printf(" data: 0x");
1490                         for (int i = 1; i <= len; i++)
1491                                 printf("%1D", (char *)data + len - i, "");
1492                 }
1493                 printf("\n");
1494         }
1495 #endif
1496
1497         /* kick firmware */
1498         sc->txfree--;
1499         sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1500         CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1501
1502         /* wait at most one second for command to complete */
1503         error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1504         if (error != 0) {
1505                 device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1506                     __func__, ipw_cmdname(type), error);
1507                 sc->flags &= ~IPW_FLAG_BUSY;
1508                 return (error);
1509         }
1510         return (0);
1511 }
1512
1513 static int
1514 ipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1515 {
1516         struct ipw_softc *sc = ifp->if_softc;
1517         struct ieee80211com *ic = &sc->sc_ic;
1518         struct ieee80211_frame *wh;
1519         struct ipw_soft_bd *sbd;
1520         struct ipw_soft_hdr *shdr;
1521         struct ipw_soft_buf *sbuf;
1522         struct ieee80211_key *k;
1523         struct mbuf *mnew;
1524         bus_dma_segment_t segs[IPW_MAX_NSEG];
1525         bus_addr_t physaddr;
1526         int nsegs, error, i;
1527
1528         wh = mtod(m0, struct ieee80211_frame *);
1529
1530         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1531                 k = ieee80211_crypto_encap(ic, ni, m0);
1532                 if (k == NULL) {
1533                         m_freem(m0);
1534                         return ENOBUFS;
1535                 }
1536
1537                 /* packet header may have moved, reset our local pointer */
1538                 wh = mtod(m0, struct ieee80211_frame *);
1539         }
1540
1541         if (bpf_peers_present(sc->sc_drvbpf)) {
1542                 struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1543
1544                 tap->wt_flags = 0;
1545                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1546                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1547
1548                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1549         }
1550
1551         shdr = SLIST_FIRST(&sc->free_shdr);
1552         sbuf = SLIST_FIRST(&sc->free_sbuf);
1553         KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1554
1555         shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1556         shdr->hdr.subtype = 0;
1557         shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1558         shdr->hdr.encrypt = 0;
1559         shdr->hdr.keyidx = 0;
1560         shdr->hdr.keysz = 0;
1561         shdr->hdr.fragmentsz = 0;
1562         IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1563         if (ic->ic_opmode == IEEE80211_M_STA)
1564                 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1565         else
1566                 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1567
1568         /* trim IEEE802.11 header */
1569         m_adj(m0, sizeof (struct ieee80211_frame));
1570
1571         error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1572             &nsegs, 0);
1573         if (error != 0 && error != EFBIG) {
1574                 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1575                     error);
1576                 m_freem(m0);
1577                 return error;
1578         }
1579         if (error != 0) {
1580                 mnew = m_defrag(m0, M_DONTWAIT);
1581                 if (mnew == NULL) {
1582                         device_printf(sc->sc_dev,
1583                             "could not defragment mbuf\n");
1584                         m_freem(m0);
1585                         return ENOBUFS;
1586                 }
1587                 m0 = mnew;
1588
1589                 error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1590                     segs, &nsegs, 0);
1591                 if (error != 0) {
1592                         device_printf(sc->sc_dev,
1593                             "could not map mbuf (error %d)\n", error);
1594                         m_freem(m0);
1595                         return error;
1596                 }
1597         }
1598
1599         error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1600             sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1601         if (error != 0) {
1602                 device_printf(sc->sc_dev, "could not map header DMA memory\n");
1603                 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1604                 m_freem(m0);
1605                 return error;
1606         }
1607
1608         SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1609         SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1610
1611         sbd = &sc->stbd_list[sc->txcur];
1612         sbd->type = IPW_SBD_TYPE_HEADER;
1613         sbd->priv = shdr;
1614         sbd->bd->physaddr = htole32(physaddr);
1615         sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1616         sbd->bd->nfrag = 1 + nsegs;
1617         sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1618             IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1619
1620         DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1621             shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1622             shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1623             ":"));
1624
1625         sc->txfree--;
1626         sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1627
1628         sbuf->m = m0;
1629         sbuf->ni = ni;
1630
1631         for (i = 0; i < nsegs; i++) {
1632                 sbd = &sc->stbd_list[sc->txcur];
1633
1634                 sbd->bd->physaddr = htole32(segs[i].ds_addr);
1635                 sbd->bd->len = htole32(segs[i].ds_len);
1636                 sbd->bd->nfrag = 0;
1637                 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1638                 if (i == nsegs - 1) {
1639                         sbd->type = IPW_SBD_TYPE_DATA;
1640                         sbd->priv = sbuf;
1641                         sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1642                 } else {
1643                         sbd->type = IPW_SBD_TYPE_NOASSOC;
1644                         sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1645                 }
1646
1647                 DPRINTFN(5, ("sending fragment (%d)\n", i));
1648
1649                 sc->txfree--;
1650                 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1651         }
1652
1653         bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1654         bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1655         bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1656
1657         /* kick firmware */
1658         CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1659
1660         return 0;
1661 }
1662
1663 static void
1664 ipw_start(struct ifnet *ifp)
1665 {
1666         struct ipw_softc *sc = ifp->if_softc;
1667         IPW_LOCK_DECL;
1668
1669         IPW_LOCK(sc);
1670         ipw_start_locked(ifp);
1671         IPW_UNLOCK(sc);
1672 }
1673
1674 static void
1675 ipw_start_locked(struct ifnet *ifp)
1676 {
1677         struct ipw_softc *sc = ifp->if_softc;
1678         struct ieee80211com *ic = &sc->sc_ic;
1679         struct mbuf *m0;
1680         struct ether_header *eh;
1681         struct ieee80211_node *ni;
1682
1683         IPW_LOCK_ASSERT(sc);
1684
1685         if (ic->ic_state != IEEE80211_S_RUN)
1686                 return;
1687
1688         for (;;) {
1689                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
1690                 if (m0 == NULL)
1691                         break;
1692
1693                 if (sc->txfree < 1 + IPW_MAX_NSEG) {
1694                         IFQ_DRV_PREPEND(&ifp->if_snd, m0);
1695                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1696                         break;
1697                 }
1698
1699                 if (m0->m_len < sizeof (struct ether_header) &&
1700                     (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL)
1701                         continue;
1702
1703                 eh = mtod(m0, struct ether_header *);
1704                 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1705                 if (ni == NULL) {
1706                         m_freem(m0);
1707                         continue;
1708                 }
1709                 BPF_MTAP(ifp, m0);
1710
1711                 m0 = ieee80211_encap(ic, m0, ni);
1712                 if (m0 == NULL) {
1713                         ieee80211_free_node(ni);
1714                         continue;
1715                 }
1716
1717                 if (bpf_peers_present(ic->ic_rawbpf))
1718                         bpf_mtap(ic->ic_rawbpf, m0);
1719
1720                 if (ipw_tx_start(ifp, m0, ni) != 0) {
1721                         ieee80211_free_node(ni);
1722                         ifp->if_oerrors++;
1723                         break;
1724                 }
1725
1726                 /* start watchdog timer */
1727                 sc->sc_tx_timer = 5;
1728         }
1729 }
1730
1731 static void
1732 ipw_watchdog(void *arg)
1733 {
1734         struct ipw_softc *sc = arg;
1735         struct ieee80211com *ic = &sc->sc_ic;
1736         struct ifnet *ifp = sc->sc_ifp;
1737
1738         IPW_LOCK_ASSERT(sc);
1739
1740         if (sc->sc_tx_timer > 0) {
1741                 if (--sc->sc_tx_timer == 0) {
1742                         if_printf(ifp, "device timeout\n");
1743                         ifp->if_oerrors++;
1744                         taskqueue_enqueue_fast(taskqueue_fast,
1745                             &sc->sc_init_task);
1746                 }
1747         }
1748         if (sc->sc_scan_timer > 0) {
1749                 if (--sc->sc_scan_timer == 0) {
1750                         DPRINTFN(3, ("Scan timeout\n"));
1751                         /* End the scan */
1752                         if (sc->flags & IPW_FLAG_SCANNING) {
1753                                 ieee80211_scan_done(ic);
1754                                 sc->flags &= ~IPW_FLAG_SCANNING;
1755                         }
1756                 }
1757         }
1758         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1759                 callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1760 }
1761
1762 static int
1763 ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1764 {
1765         struct ipw_softc *sc = ifp->if_softc;
1766         struct ieee80211com *ic = &sc->sc_ic;
1767         int error = 0;
1768         IPW_LOCK_DECL;
1769
1770         IPW_LOCK(sc);
1771
1772         switch (cmd) {
1773         case SIOCSIFFLAGS:
1774                 if (ifp->if_flags & IFF_UP) {
1775                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1776                                 ipw_init_locked(sc, 0);
1777                 } else {
1778                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1779                                 ipw_stop_locked(sc);
1780                 }
1781                 break;
1782
1783         default:
1784                 error = ieee80211_ioctl(ic, cmd, data);
1785         }
1786
1787         if (error == ENETRESET) {
1788                 if ((ifp->if_flags & IFF_UP) &&
1789                     (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1790                     (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1791                         ipw_init_locked(sc, 0);
1792                 error = 0;
1793         }
1794
1795         IPW_UNLOCK(sc);
1796
1797         return error;
1798 }
1799
1800 static void
1801 ipw_stop_master(struct ipw_softc *sc)
1802 {
1803         uint32_t tmp;
1804         int ntries;
1805
1806         /* disable interrupts */
1807         CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1808
1809         CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1810         for (ntries = 0; ntries < 50; ntries++) {
1811                 if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1812                         break;
1813                 DELAY(10);
1814         }
1815         if (ntries == 50)
1816                 device_printf(sc->sc_dev, "timeout waiting for master\n");
1817
1818         tmp = CSR_READ_4(sc, IPW_CSR_RST);
1819         CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1820
1821         /* Clear all flags except the following */
1822         sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1823 }
1824
1825 static int
1826 ipw_reset(struct ipw_softc *sc)
1827 {
1828         uint32_t tmp;
1829         int ntries;
1830
1831         ipw_stop_master(sc);
1832
1833         /* move adapter to D0 state */
1834         tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1835         CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1836
1837         /* wait for clock stabilization */
1838         for (ntries = 0; ntries < 1000; ntries++) {
1839                 if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1840                         break;
1841                 DELAY(200);
1842         }
1843         if (ntries == 1000)
1844                 return EIO;
1845
1846         tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1847         CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1848
1849         DELAY(10);
1850
1851         tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1852         CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1853
1854         return 0;
1855 }
1856
1857 static int
1858 ipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1859 {
1860         int ms = hz < 1000 ? 1 : hz/10;
1861         int i, error;
1862
1863         for (i = 0; i < 100; i++) {
1864                 if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1865                         return 0;
1866                 error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1867                 if (error == 0 || error != EWOULDBLOCK)
1868                         return 0;
1869         }
1870         DPRINTF(("%s: timeout waiting for %s\n",
1871                 __func__, waitfor ? "disable" : "enable"));
1872         return ETIMEDOUT;
1873 }
1874
1875 static int
1876 ipw_enable(struct ipw_softc *sc)
1877 {
1878         int error;
1879
1880         if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1881                 DPRINTF(("Enable adapter\n"));
1882                 error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1883                 if (error != 0)
1884                         return error;
1885                 error = ipw_waitfordisable(sc, 0);
1886                 if (error != 0)
1887                         return error;
1888                 sc->flags |= IPW_FLAG_ENABLED;
1889         }
1890         return 0;
1891 }
1892
1893 static int
1894 ipw_disable(struct ipw_softc *sc)
1895 {
1896         int error;
1897
1898         if (sc->flags & IPW_FLAG_ENABLED) {
1899                 DPRINTF(("Disable adapter\n"));
1900                 error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1901                 if (error != 0)
1902                         return error;
1903                 error = ipw_waitfordisable(sc, 1);
1904                 if (error != 0)
1905                         return error;
1906                 sc->flags &= ~IPW_FLAG_ENABLED;
1907         }
1908         return 0;
1909 }
1910
1911 /*
1912  * Upload the microcode to the device.
1913  */
1914 static int
1915 ipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1916 {
1917         int ntries;
1918
1919         MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1920         CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1921
1922         MEM_WRITE_2(sc, 0x220000, 0x0703);
1923         MEM_WRITE_2(sc, 0x220000, 0x0707);
1924
1925         MEM_WRITE_1(sc, 0x210014, 0x72);
1926         MEM_WRITE_1(sc, 0x210014, 0x72);
1927
1928         MEM_WRITE_1(sc, 0x210000, 0x40);
1929         MEM_WRITE_1(sc, 0x210000, 0x00);
1930         MEM_WRITE_1(sc, 0x210000, 0x40);
1931
1932         MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1933
1934         MEM_WRITE_1(sc, 0x210000, 0x00);
1935         MEM_WRITE_1(sc, 0x210000, 0x00);
1936         MEM_WRITE_1(sc, 0x210000, 0x80);
1937
1938         MEM_WRITE_2(sc, 0x220000, 0x0703);
1939         MEM_WRITE_2(sc, 0x220000, 0x0707);
1940
1941         MEM_WRITE_1(sc, 0x210014, 0x72);
1942         MEM_WRITE_1(sc, 0x210014, 0x72);
1943
1944         MEM_WRITE_1(sc, 0x210000, 0x00);
1945         MEM_WRITE_1(sc, 0x210000, 0x80);
1946
1947         for (ntries = 0; ntries < 10; ntries++) {
1948                 if (MEM_READ_1(sc, 0x210000) & 1)
1949                         break;
1950                 DELAY(10);
1951         }
1952         if (ntries == 10) {
1953                 device_printf(sc->sc_dev,
1954                     "timeout waiting for ucode to initialize\n");
1955                 return EIO;
1956         }
1957
1958         MEM_WRITE_4(sc, 0x3000e0, 0);
1959
1960         return 0;
1961 }
1962
1963 /* set of macros to handle unaligned little endian data in firmware image */
1964 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1965 #define GETLE16(p) ((p)[0] | (p)[1] << 8)
1966 static int
1967 ipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
1968 {
1969         const uint8_t *p, *end;
1970         uint32_t tmp, dst;
1971         uint16_t len;
1972         int error;
1973
1974         p = fw;
1975         end = fw + size;
1976         while (p < end) {
1977                 dst = GETLE32(p); p += 4;
1978                 len = GETLE16(p); p += 2;
1979
1980                 ipw_write_mem_1(sc, dst, p, len);
1981                 p += len;
1982         }
1983
1984         CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
1985             IPW_IO_LED_OFF);
1986
1987         /* enable interrupts */
1988         CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1989
1990         /* kick the firmware */
1991         CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1992
1993         tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1994         CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
1995
1996         /* wait at most one second for firmware initialization to complete */
1997         if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
1998                 device_printf(sc->sc_dev, "timeout waiting for firmware "
1999                     "initialization to complete\n");
2000                 return error;
2001         }
2002
2003         tmp = CSR_READ_4(sc, IPW_CSR_IO);
2004         CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2005             IPW_IO_GPIO3_MASK);
2006
2007         return 0;
2008 }
2009
2010 static int
2011 ipw_setwepkeys(struct ipw_softc *sc)
2012 {
2013         struct ieee80211com *ic = &sc->sc_ic;
2014         struct ipw_wep_key wepkey;
2015         struct ieee80211_key *wk;
2016         int error, i;
2017
2018         for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2019                 wk = &ic->ic_crypto.cs_nw_keys[i];
2020
2021                 if (wk->wk_cipher == NULL ||
2022                     wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2023                         continue;
2024
2025                 wepkey.idx = i;
2026                 wepkey.len = wk->wk_keylen;
2027                 memset(wepkey.key, 0, sizeof wepkey.key);
2028                 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2029                 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2030                     wepkey.len));
2031                 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2032                     sizeof wepkey);
2033                 if (error != 0)
2034                         return error;
2035         }
2036         return 0;
2037 }
2038
2039 static int
2040 ipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2041 {
2042         struct ipw_wpa_ie wpaie;
2043
2044         memset(&wpaie, 0, sizeof(wpaie));
2045         wpaie.len = htole32(ielen);
2046         /* XXX verify length */
2047         memcpy(&wpaie.ie, ie, ielen);
2048         DPRINTF(("Setting WPA IE\n"));
2049         return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2050 }
2051
2052 static int
2053 ipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2054 {
2055         static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2056
2057         if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2058                 DPRINTF(("Setting mandatory BSSID to null\n"));
2059                 return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2060         } else {
2061                 DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2062                 return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2063                         bssid, IEEE80211_ADDR_LEN);
2064         }
2065 }
2066
2067 static int
2068 ipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2069 {
2070         if (ssidlen == 0) {
2071                 /*
2072                  * A bug in the firmware breaks the ``don't associate''
2073                  * bit in the scan options command.  To compensate for
2074                  * this install a bogus ssid when no ssid is specified
2075                  * so the firmware won't try to associate.
2076                  */
2077                 DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2078                 return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2079                         "\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2080                         "\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2081                         "\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2082                         "\x3c\x3d", IEEE80211_NWID_LEN);
2083         } else {
2084 #ifdef IPW_DEBUG
2085                 if (ipw_debug > 0) {
2086                         printf("Setting ESSID to ");
2087                         ieee80211_print_essid(ssid, ssidlen);
2088                         printf("\n");
2089                 }
2090 #endif
2091                 return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2092         }
2093 }
2094
2095 static int
2096 ipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2097 {
2098         struct ipw_scan_options opts;
2099
2100         DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2101         opts.channels = htole32(chanmask);
2102         opts.flags = htole32(flags);
2103         return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2104 }
2105
2106 /*
2107  * Handler for sc_scan_task.  This is a simple wrapper around ipw_scan().
2108  */
2109 static void
2110 ipw_scan_task(void *context, int pending)
2111 {
2112         struct ipw_softc *sc = context;
2113         IPW_LOCK_DECL;
2114
2115         IPW_LOCK(sc);
2116         ipw_scan(sc);
2117         IPW_UNLOCK(sc);
2118 }
2119
2120 static int
2121 ipw_scan(struct ipw_softc *sc)
2122 {
2123         uint32_t params;
2124         int error;
2125
2126         DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2127
2128         if (sc->flags & IPW_FLAG_SCANNING)
2129                 return (EBUSY);
2130         sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2131
2132         /* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2133         error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2134         if (error != 0)
2135                 goto done;
2136
2137         /*
2138          * Setup null/bogus ssid so firmware doesn't use any previous
2139          * ssid to try and associate.  This is because the ``don't
2140          * associate'' option bit is broken (sigh).
2141          */
2142         error = ipw_setssid(sc, NULL, 0);
2143         if (error != 0)
2144                 goto done;
2145
2146         /*
2147          * NB: the adapter may be disabled on association lost;
2148          *     if so just re-enable it to kick off scanning.
2149          */
2150         DPRINTF(("Starting scan\n"));
2151         sc->sc_scan_timer = 3;
2152         if (sc->flags & IPW_FLAG_ENABLED) {
2153                 params = 0;                             /* XXX? */
2154                 error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2155                                 &params, sizeof(params));
2156         } else
2157                 error = ipw_enable(sc);
2158 done:
2159         if (error != 0) {
2160                 DPRINTF(("Scan failed\n"));
2161                 sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2162         }
2163         return (error);
2164 }
2165
2166 static int
2167 ipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2168 {
2169         struct ieee80211com *ic = &sc->sc_ic;
2170         uint32_t data;
2171         int error;
2172
2173         data = htole32(ieee80211_chan2ieee(ic, chan));
2174         DPRINTF(("Setting channel to %u\n", le32toh(data)));
2175         error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2176         if (error == 0)
2177                 ipw_setcurchan(sc, chan);
2178         return error;
2179 }
2180
2181 static int
2182 ipw_config(struct ipw_softc *sc)
2183 {
2184         struct ieee80211com *ic = &sc->sc_ic;
2185         struct ifnet *ifp = ic->ic_ifp;
2186         struct ipw_security security;
2187         struct ipw_configuration config;
2188         uint32_t data;
2189         int error;
2190
2191         error = ipw_disable(sc);
2192         if (error != 0)
2193                 return error;
2194
2195         switch (ic->ic_opmode) {
2196         case IEEE80211_M_STA:
2197         case IEEE80211_M_HOSTAP:
2198         case IEEE80211_M_WDS:           /* XXX */
2199                 data = htole32(IPW_MODE_BSS);
2200                 break;
2201         case IEEE80211_M_IBSS:
2202         case IEEE80211_M_AHDEMO:
2203                 data = htole32(IPW_MODE_IBSS);
2204                 break;
2205         case IEEE80211_M_MONITOR:
2206                 data = htole32(IPW_MODE_MONITOR);
2207                 break;
2208         }
2209         DPRINTF(("Setting mode to %u\n", le32toh(data)));
2210         error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2211         if (error != 0)
2212                 return error;
2213
2214         if (ic->ic_opmode == IEEE80211_M_IBSS ||
2215             ic->ic_opmode == IEEE80211_M_MONITOR) {
2216                 error = ipw_setchannel(sc, ic->ic_curchan);
2217                 if (error != 0)
2218                         return error;
2219         }
2220
2221         if (ic->ic_opmode == IEEE80211_M_MONITOR)
2222                 return ipw_enable(sc);
2223
2224         IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2225         DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":"));
2226         error = ipw_cmd(sc, IPW_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2227             IEEE80211_ADDR_LEN);
2228         if (error != 0)
2229                 return error;
2230
2231         config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2232             IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2233         if (ic->ic_opmode == IEEE80211_M_IBSS)
2234                 config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2235         if (ifp->if_flags & IFF_PROMISC)
2236                 config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2237         config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2238         config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2239         DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2240         error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2241         if (error != 0)
2242                 return error;
2243
2244         data = htole32(0x3); /* 1, 2 */
2245         DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2246         error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2247         if (error != 0)
2248                 return error;
2249
2250         /* NB: use the same rate set */
2251         DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2252         error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2253         if (error != 0)
2254                 return error;
2255
2256         data = htole32(0xf); /* 1, 2, 5.5, 11 */
2257         DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2258         error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2259         if (error != 0)
2260                 return error;
2261
2262         data = htole32(IPW_POWER_MODE_CAM);
2263         DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2264         error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2265         if (error != 0)
2266                 return error;
2267
2268         if (ic->ic_opmode == IEEE80211_M_IBSS) {
2269                 data = htole32(32); /* default value */
2270                 DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2271                 error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2272                     sizeof data);
2273                 if (error != 0)
2274                         return error;
2275         }
2276
2277         data = htole32(ic->ic_rtsthreshold);
2278         DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2279         error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2280         if (error != 0)
2281                 return error;
2282
2283         data = htole32(ic->ic_fragthreshold);
2284         DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2285         error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2286         if (error != 0)
2287                 return error;
2288
2289         error = ipw_setssid(sc, ic->ic_des_ssid[0].ssid, ic->ic_des_ssid[0].len);
2290         if (error != 0)
2291                 return error;
2292
2293         error = ipw_setbssid(sc, NULL);
2294         if (error != 0)
2295                 return error;
2296
2297         if (ic->ic_flags & IEEE80211_F_DESBSSID) {
2298                 DPRINTF(("Setting desired BSSID to %6D\n", ic->ic_des_bssid,
2299                     ":"));
2300                 error = ipw_cmd(sc, IPW_CMD_SET_DESIRED_BSSID,
2301                     ic->ic_des_bssid, IEEE80211_ADDR_LEN);
2302                 if (error != 0)
2303                         return error;
2304         }
2305
2306         memset(&security, 0, sizeof security);
2307         security.authmode = (ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED) ?
2308             IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2309         security.ciphers = htole32(IPW_CIPHER_NONE);
2310         DPRINTF(("Setting authmode to %u\n", security.authmode));
2311         error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2312             sizeof security);
2313         if (error != 0)
2314                 return error;
2315
2316         if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2317                 error = ipw_setwepkeys(sc);
2318                 if (error != 0)
2319                         return error;
2320
2321                 if (ic->ic_crypto.cs_def_txkey != IEEE80211_KEYIX_NONE) {
2322                         data = htole32(ic->ic_crypto.cs_def_txkey);
2323                         DPRINTF(("Setting wep tx key index to %u\n",
2324                                 le32toh(data)));
2325                         error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2326                             sizeof data);
2327                         if (error != 0)
2328                                 return error;
2329                 }
2330         }
2331
2332         data = htole32((ic->ic_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2333         DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2334         error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2335         if (error != 0)
2336                 return error;
2337
2338         if (ic->ic_opt_ie != NULL) {
2339                 error = ipw_setwpaie(sc, ic->ic_opt_ie, ic->ic_opt_ie_len);
2340                 if (error != 0)
2341                         return error;
2342         }
2343
2344         if (ic->ic_opmode == IEEE80211_M_IBSS) {
2345                 data = htole32(ic->ic_bintval);
2346                 DPRINTF(("Setting beacon interval to %u\n", le32toh(data)));
2347                 error = ipw_cmd(sc, IPW_CMD_SET_BEACON_INTERVAL, &data,
2348                     sizeof data);
2349                 if (error != 0)
2350                         return error;
2351         }
2352
2353         error = ipw_setscanopts(sc, 0x3fff, 0);
2354         if (error != 0)
2355                 return error;
2356
2357         return (ipw_enable(sc));
2358 }
2359
2360 /*
2361  * Handler for sc_assoc_task.  This is a simple wrapper around
2362  * ipw_auth_and_assoc().
2363  */
2364 static void
2365 ipw_assoc_task(void *context, int pending)
2366 {
2367         struct ipw_softc *sc = context;
2368         IPW_LOCK_DECL;
2369
2370         IPW_LOCK(sc);
2371         ipw_auth_and_assoc(sc);
2372         IPW_UNLOCK(sc);
2373 }
2374
2375 static int
2376 ipw_auth_and_assoc(struct ipw_softc *sc)
2377 {
2378         struct ieee80211com *ic = &sc->sc_ic;
2379         struct ieee80211_node *ni = ic->ic_bss;
2380         struct ipw_security security;
2381         uint32_t data;
2382         int error;
2383
2384         error = ipw_disable(sc);
2385         if (error != 0)
2386                 return (error);
2387
2388         memset(&security, 0, sizeof security);
2389         security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2390             IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2391         security.ciphers = htole32(IPW_CIPHER_NONE);
2392         DPRINTF(("Setting authmode to %u\n", security.authmode));
2393         error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2394             sizeof security);
2395         if (error != 0)
2396                 return (error);
2397
2398         if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2399                 error = ipw_setwepkeys(sc);
2400                 if (error != 0)
2401                         return error;
2402
2403                 if (ic->ic_crypto.cs_def_txkey != IEEE80211_KEYIX_NONE) {
2404                         data = htole32(ic->ic_crypto.cs_def_txkey);
2405                         DPRINTF(("Setting wep tx key index to %u\n",
2406                                 le32toh(data)));
2407                         error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2408                             sizeof data);
2409                         if (error != 0)
2410                                 return error;
2411                 }
2412         }
2413
2414         data = htole32((ic->ic_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2415         DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2416         error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2417         if (error != 0)
2418                 return error;
2419
2420         error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2421         if (error != 0)
2422                 return (error);
2423
2424         error = ipw_setbssid(sc, ni->ni_bssid);
2425         if (error != 0)
2426                 return (error);
2427
2428         if (ic->ic_opt_ie != NULL) {
2429                 error = ipw_setwpaie(sc, ic->ic_opt_ie, ic->ic_opt_ie_len);
2430                 if (error != 0)
2431                         return error;
2432         }
2433         if (ic->ic_opmode == IEEE80211_M_IBSS) {
2434                 error = ipw_setchannel(sc, ni->ni_chan);
2435                 if (error != 0)
2436                         return (error);
2437         }
2438
2439         /* lock scan to ap's channel and enable associate */
2440         error = ipw_setscanopts(sc,
2441                         1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2442
2443         return ipw_enable(sc);          /* finally, enable adapter */
2444 }
2445
2446 /*
2447  * Handler for sc_disassoc_task.  This is a simple wrapper around
2448  * ipw_disassociate().
2449  */
2450 static void
2451 ipw_disassoc_task(void *context, int pending)
2452 {
2453         struct ipw_softc *sc = context;
2454         IPW_LOCK_DECL;
2455
2456         IPW_LOCK(sc);
2457         ipw_disassociate(sc);
2458         IPW_UNLOCK(sc);
2459 }
2460
2461 static int
2462 ipw_disassociate(struct ipw_softc *sc)
2463 {
2464         struct ieee80211com *ic = &sc->sc_ic;
2465         struct ieee80211_node *ni = ic->ic_bss;
2466
2467         DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2468
2469         /*
2470          * NB: don't try to do this if ipw_stop_master has
2471          *     shutdown the firmware and disabled interrupts.
2472          */
2473         if (!(sc->flags & IPW_FLAG_FW_INITED))
2474                 return (0);
2475
2476         sc->flags &= ~IPW_FLAG_ASSOCIATED;
2477         /*
2478          * NB: firmware currently ignores bssid parameter, but
2479          *     supply it in case this changes (follow linux driver).
2480          */
2481         return ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2482                 ni->ni_bssid, IEEE80211_ADDR_LEN);
2483 }
2484
2485 /*
2486  * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2487  * It is called on firmware panics or on watchdog timeouts.
2488  */
2489 static void
2490 ipw_init_task(void *context, int pending)
2491 {
2492         ipw_init(context);
2493 }
2494
2495 static void
2496 ipw_init(void *priv)
2497 {
2498         struct ipw_softc *sc = priv;
2499         IPW_LOCK_DECL;
2500
2501         IPW_LOCK(sc);
2502         ipw_init_locked(sc, 0);
2503         IPW_UNLOCK(sc);
2504 }
2505
2506 static void
2507 ipw_init_locked(struct ipw_softc *sc, int force)
2508 {
2509         struct ieee80211com *ic = &sc->sc_ic;
2510         struct ifnet *ifp = ic->ic_ifp;
2511         const struct firmware *fp;
2512         const struct ipw_firmware_hdr *hdr;
2513         const char *imagename, *fw;
2514         IPW_LOCK_DECL;
2515
2516         IPW_LOCK_ASSERT(sc);
2517
2518         DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2519                 ieee80211_state_name[ic->ic_state], sc->flags));
2520
2521         /*
2522          * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2523          * when loading the firmware and we don't want to be called during this
2524          * operation.
2525          */
2526         if (sc->flags & IPW_FLAG_INIT_LOCKED)
2527                 return;
2528         sc->flags |= IPW_FLAG_INIT_LOCKED;
2529
2530         ipw_stop_locked(sc);
2531
2532         if (ipw_reset(sc) != 0) {
2533                 device_printf(sc->sc_dev, "could not reset adapter\n");
2534                 goto fail1;
2535         }
2536
2537         switch (ic->ic_opmode) {
2538         case IEEE80211_M_STA:
2539                 imagename = "ipw_bss";
2540                 break;
2541         case IEEE80211_M_IBSS:
2542                 imagename = "ipw_ibss";
2543                 break;
2544         case IEEE80211_M_MONITOR:
2545                 imagename = "ipw_monitor";
2546                 break;
2547         default:
2548                 imagename = NULL;       /* should not get there */
2549         }
2550
2551         /*
2552          * Load firmware image using the firmware(9) subsystem.  We need to
2553          * release the driver's lock first.
2554          */
2555         if (sc->sc_firmware == NULL || strcmp(sc->sc_firmware->name,
2556             imagename) != 0) {
2557                 IPW_UNLOCK(sc);
2558                 if (sc->sc_firmware != NULL)
2559                         firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
2560                 sc->sc_firmware = firmware_get(imagename);
2561                 IPW_LOCK(sc);
2562         }
2563
2564         if (sc->sc_firmware == NULL) {
2565                 device_printf(sc->sc_dev,
2566                     "could not load firmware image '%s'\n", imagename);
2567                 goto fail1;
2568         }
2569
2570         fp = sc->sc_firmware;
2571         if (fp->datasize < sizeof *hdr) {
2572                 device_printf(sc->sc_dev,
2573                     "firmware image too short %zu\n", fp->datasize);
2574                 goto fail2;
2575         }
2576
2577         hdr = (const struct ipw_firmware_hdr *)fp->data;
2578
2579         if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
2580             le32toh(hdr->ucodesz)) {
2581                 device_printf(sc->sc_dev,
2582                     "firmware image too short %zu\n", fp->datasize);
2583                 goto fail2;
2584         }
2585
2586         DPRINTF(("Loading firmware image '%s'\n", imagename));
2587         fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2588         if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2589                 device_printf(sc->sc_dev, "could not load microcode\n");
2590                 goto fail2;
2591         }
2592
2593         ipw_stop_master(sc);
2594
2595         /*
2596          * Setup tx, rx and status rings.
2597          */
2598         sc->txold = IPW_NTBD - 1;
2599         sc->txcur = 0;
2600         sc->txfree = IPW_NTBD - 2;
2601         sc->rxcur = IPW_NRBD - 1;
2602
2603         CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2604         CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2605         CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2606         CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2607
2608         CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2609         CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2610         CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2611         CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2612
2613         CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2614
2615         fw = (const char *)fp->data + sizeof *hdr;
2616         if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2617                 device_printf(sc->sc_dev, "could not load firmware\n");
2618                 goto fail2;
2619         }
2620
2621         sc->flags |= IPW_FLAG_FW_INITED;
2622
2623         /* retrieve information tables base addresses */
2624         sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2625         sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2626
2627         ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2628
2629         if (ipw_config(sc) != 0) {
2630                 device_printf(sc->sc_dev, "device configuration failed\n");
2631                 goto fail1;
2632         }
2633
2634         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2635                 /*
2636                  * NB: When restarting the adapter clock the state
2637                  * machine regardless of the roaming mode; otherwise
2638                  * we need to notify user apps so they can manually
2639                  * get us going again.
2640                  */
2641                 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL || force)
2642                         ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
2643         } else
2644                 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2645
2646         callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2647         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2648         ifp->if_drv_flags |= IFF_DRV_RUNNING;
2649
2650         sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2651         return;
2652
2653 fail2:  firmware_put(fp, FIRMWARE_UNLOAD);
2654         sc->sc_firmware = NULL;
2655 fail1:  ifp->if_flags &= ~IFF_UP;
2656         ipw_stop_locked(sc);
2657         sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2658 }
2659
2660 static void
2661 ipw_stop(void *priv)
2662 {
2663         struct ipw_softc *sc = priv;
2664         IPW_LOCK_DECL;
2665
2666         IPW_LOCK(sc);
2667         ipw_stop_locked(sc);
2668         IPW_UNLOCK(sc);
2669 }
2670
2671 static void
2672 ipw_stop_locked(struct ipw_softc *sc)
2673 {
2674         struct ieee80211com *ic = &sc->sc_ic;
2675         struct ifnet *ifp = ic->ic_ifp;
2676         int i;
2677
2678         IPW_LOCK_ASSERT(sc);
2679
2680         ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2681
2682         callout_stop(&sc->sc_wdtimer);
2683         ipw_stop_master(sc);
2684
2685         CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2686
2687         /*
2688          * Release tx buffers.
2689          */
2690         for (i = 0; i < IPW_NTBD; i++)
2691                 ipw_release_sbd(sc, &sc->stbd_list[i]);
2692
2693         sc->sc_tx_timer = 0;
2694         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2695 }
2696
2697 static int
2698 ipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2699 {
2700         struct ipw_softc *sc = arg1;
2701         uint32_t i, size, buf[256];
2702
2703         memset(buf, 0, sizeof buf);
2704
2705         if (!(sc->flags & IPW_FLAG_FW_INITED))
2706                 return SYSCTL_OUT(req, buf, sizeof buf);
2707
2708         CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2709
2710         size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2711         for (i = 1; i < size; i++)
2712                 buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2713
2714         return SYSCTL_OUT(req, buf, size);
2715 }
2716
2717 static int
2718 ipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2719 {
2720         struct ipw_softc *sc = arg1;
2721         int val;
2722
2723         val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2724                 (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2725
2726         return SYSCTL_OUT(req, &val, sizeof val);
2727 }
2728
2729 static uint32_t
2730 ipw_read_table1(struct ipw_softc *sc, uint32_t off)
2731 {
2732         return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2733 }
2734
2735 static void
2736 ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2737 {
2738         MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2739 }
2740
2741 #if 0
2742 static int
2743 ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2744 {
2745         uint32_t addr, info;
2746         uint16_t count, size;
2747         uint32_t total;
2748
2749         /* addr[4] + count[2] + size[2] */
2750         addr = MEM_READ_4(sc, sc->table2_base + off);
2751         info = MEM_READ_4(sc, sc->table2_base + off + 4);
2752
2753         count = info >> 16;
2754         size = info & 0xffff;
2755         total = count * size;
2756
2757         if (total > *len) {
2758                 *len = total;
2759                 return EINVAL;
2760         }
2761
2762         *len = total;
2763         ipw_read_mem_1(sc, addr, buf, total);
2764
2765         return 0;
2766 }
2767
2768 static void
2769 ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2770     bus_size_t count)
2771 {
2772         for (; count > 0; offset++, datap++, count--) {
2773                 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2774                 *datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2775         }
2776 }
2777 #endif
2778
2779 static void
2780 ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2781     bus_size_t count)
2782 {
2783         for (; count > 0; offset++, datap++, count--) {
2784                 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2785                 CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2786         }
2787 }
2788
2789 static void
2790 ipw_scan_start(struct ieee80211com *ic)
2791 {
2792         struct ifnet *ifp = ic->ic_ifp;
2793         struct ipw_softc *sc = ifp->if_softc;
2794         IPW_LOCK_DECL;
2795
2796         IPW_LOCK(sc);
2797         if (!(sc->flags & IPW_FLAG_SCANNING))
2798                 taskqueue_enqueue_fast(taskqueue_fast, &sc->sc_scan_task);
2799         IPW_UNLOCK(sc);
2800 }
2801
2802 static void
2803 ipw_set_channel(struct ieee80211com *ic)
2804 {
2805         struct ifnet *ifp = ic->ic_ifp;
2806         struct ipw_softc *sc = ifp->if_softc;
2807         IPW_LOCK_DECL;
2808
2809         IPW_LOCK(sc);
2810         if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2811                 ipw_disable(sc);
2812                 ipw_setchannel(sc, ic->ic_curchan);
2813                 ipw_enable(sc);
2814         }
2815         IPW_UNLOCK(sc);
2816 }
2817
2818 static void
2819 ipw_scan_curchan(struct ieee80211com *ic, unsigned long maxdwell)
2820 {
2821         /* NB: all channels are scanned at once */
2822 }
2823
2824 static void
2825 ipw_scan_mindwell(struct ieee80211com *ic)
2826 {
2827         /* NB: don't try to abort scan; wait for firmware to finish */
2828 }
2829
2830 static void
2831 ipw_scan_end(struct ieee80211com *ic)
2832 {
2833         struct ifnet *ifp = ic->ic_ifp;
2834         struct ipw_softc *sc = ifp->if_softc;
2835         IPW_LOCK_DECL;
2836
2837         IPW_LOCK(sc);
2838         sc->flags &= ~IPW_FLAG_SCANNING;
2839         IPW_UNLOCK(sc);
2840 }