Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / dev / netif / iwi / if_iwi.c
1 /*
2  * Copyright (c) 2004, 2005
3  *      Damien Bergamini <damien.bergamini@free.fr>.
4  * Copyright (c) 2004, 2005
5  *      Andrew Atrens <atrens@nortelnetworks.com>.
6  *
7  * All rights reserved.
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  * $DragonFly: src/sys/dev/netif/iwi/if_iwi.c,v 1.5 2005/06/27 11:28:54 corecode Exp $
32  */
33
34 #include "opt_inet.h"
35
36 #include <sys/cdefs.h>
37
38 /*-
39  * Intel(R) PRO/Wireless 2200BG/2915ABG driver
40  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
41  */
42
43 #include <sys/param.h>
44 #include <sys/sysctl.h>
45 #include <sys/sockio.h>
46 #include <sys/mbuf.h>
47 #include <sys/kernel.h>
48 #include <sys/kthread.h>
49 #include <sys/socket.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <sys/bus.h>
54 #include <sys/endian.h>
55 #include <sys/proc.h>
56 #include <sys/ucred.h>
57 #include <sys/thread2.h>
58
59 #include <machine/bus.h>
60 #include <machine/resource.h>
61 #include <machine/clock.h>
62 #include <sys/rman.h>
63
64 #include <bus/pci/pcireg.h>
65 #include <bus/pci/pcivar.h>
66
67 #include <net/bpf.h>
68 #include <net/if.h>
69 #include <net/if_arp.h>
70 #include <net/ifq_var.h>
71 #include <net/ethernet.h>
72 #include <net/if_dl.h>
73 #include <net/if_media.h>
74 #include <net/if_types.h>
75 #include <net/ifq_var.h>
76
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/if_ether.h>
82
83 #ifdef IPX
84 #include <netproto/ipx/ipx.h>
85 #include <netproto/ipx/ipx_if.h>
86 #endif
87
88 #include <netproto/802_11/ieee80211_var.h>
89 #include <netproto/802_11/ieee80211_ioctl.h>
90 #include <netproto/802_11/ieee80211_radiotap.h>
91 #include <netproto/802_11/if_wavelan_ieee.h>
92
93 #include "if_iwireg.h"
94 #include "if_iwivar.h"
95
96 #ifdef IWI_DEBUG
97 #define DPRINTF(x)      if (sc->debug_level > 0) printf x
98 #define DPRINTFN(n, x)  if (sc->debug_level >= (n)) printf x
99
100 #else
101 #define DPRINTF(x)
102 #define DPRINTFN(n, x)
103 #endif
104
105 MODULE_DEPEND(iwi, pci,  1, 1, 1);
106 MODULE_DEPEND(iwi, wlan, 1, 1, 1);
107
108 struct iwi_dump_buffer {
109         u_int32_t buf[128];
110 };
111
112 struct iwi_ident {
113         u_int16_t       vendor;
114         u_int16_t       device;
115         const char      *name;
116 };
117
118 static const struct iwi_ident iwi_ident_table[] = {
119         { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG MiniPCI" },
120         { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG MiniPCI" },
121         { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG MiniPCI" },
122
123         { 0, 0, NULL }
124 };
125
126 static const struct ieee80211_rateset iwi_rateset_11a =
127         { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
128
129 static const struct ieee80211_rateset iwi_rateset_11b =
130         { 4, { 2, 4, 11, 22 } };
131
132 static const struct ieee80211_rateset iwi_rateset_11g =
133         { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
134
135 static int              iwi_dma_alloc(struct iwi_softc *);
136 static void             iwi_release(struct iwi_softc *);
137 static int              iwi_media_change(struct ifnet *);
138 static void             iwi_media_status(struct ifnet *, struct ifmediareq *);
139 static u_int16_t        iwi_read_prom_word(struct iwi_softc *, u_int8_t);
140 static int              iwi_newstate(struct ieee80211com *,
141                             enum ieee80211_state, int);
142 static void             iwi_fix_channel(struct iwi_softc *, struct mbuf *);
143 static void             iwi_frame_intr(struct iwi_softc *,
144                             struct iwi_rx_buf *, int, struct iwi_frame *);
145 static void             iwi_notification_intr(struct iwi_softc *,
146                             struct iwi_notif *);
147 static void             iwi_rx_intr(struct iwi_softc *);
148 static void             iwi_tx_intr(struct iwi_softc *);
149 static void             iwi_intr(void *);
150 static void             iwi_dma_map_buf(void *, bus_dma_segment_t *, int,
151                             bus_size_t, int);
152 static void             iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
153 static int              iwi_cmd(struct iwi_softc *, u_int8_t, void *, u_int8_t,
154                             int);
155 static int              iwi_tx_start(struct ifnet *, struct mbuf *,
156                             struct ieee80211_node *);
157 static void             iwi_start(struct ifnet *);
158 static void             iwi_watchdog(struct ifnet *);
159 static int              iwi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *cr);
160 static void             iwi_stop_master(struct iwi_softc *);
161 static int              iwi_reset(struct iwi_softc *);
162 static int              iwi_load_ucode(struct iwi_softc *, void *, int);
163 static int              iwi_load_firmware(struct iwi_softc *, void *, int);
164 static int              iwi_cache_firmware(struct iwi_softc *, void *, int);
165 static void             iwi_free_firmware(struct iwi_softc *);
166 static int              iwi_config(struct iwi_softc *);
167 static int              iwi_scan(struct iwi_softc *);
168 static int              iwi_auth_and_assoc(struct iwi_softc *);
169 static void             iwi_init(void *);
170 static void             iwi_init_locked(void *);
171 static void             iwi_stop(void *);
172 static void             iwi_dump_fw_event_log(struct iwi_softc *sc);
173 static void             iwi_dump_fw_error_log(struct iwi_softc *sc);
174 static u_int8_t         iwi_find_station(struct iwi_softc *sc, u_int8_t *mac);
175 static int8_t           iwi_cache_station(struct iwi_softc *sc, u_int8_t *mac);
176 static int              iwi_adapter_config(struct iwi_softc *sc, int is_a, int cmd_wait);
177
178 static int              iwi_sysctl_bt_coexist(SYSCTL_HANDLER_ARGS);
179 static int              iwi_sysctl_bg_autodetect(SYSCTL_HANDLER_ARGS);
180 static int              iwi_sysctl_cts_to_self(SYSCTL_HANDLER_ARGS);
181 static int              iwi_sysctl_antenna_diversity(SYSCTL_HANDLER_ARGS);
182 static int              iwi_sysctl_radio(SYSCTL_HANDLER_ARGS);
183 static int              iwi_sysctl_stats(SYSCTL_HANDLER_ARGS);
184 static int              iwi_sysctl_dump_logs(SYSCTL_HANDLER_ARGS);
185 static int              iwi_sysctl_neg_best_rates_first(SYSCTL_HANDLER_ARGS);
186 static int              iwi_sysctl_disable_unicast_decryption(SYSCTL_HANDLER_ARGS);
187 static int              iwi_sysctl_disable_multicast_decryption(SYSCTL_HANDLER_ARGS);
188
189 static __inline u_int8_t MEM_READ_1(struct iwi_softc *sc, u_int32_t addr)
190 {
191         CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
192         return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
193 }
194
195 static __inline u_int32_t MEM_READ_4(struct iwi_softc *sc, u_int32_t addr)
196 {
197         CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
198         return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
199 }
200
201 static int iwi_probe(device_t);
202 static int iwi_attach(device_t);
203 static int iwi_detach(device_t);
204 static int iwi_shutdown(device_t);
205 static int iwi_suspend(device_t);
206 static int iwi_resume(device_t);
207
208 static device_method_t iwi_methods[] = {
209         /* Device interface */
210         DEVMETHOD(device_probe,         iwi_probe),
211         DEVMETHOD(device_attach,        iwi_attach),
212         DEVMETHOD(device_detach,        iwi_detach),
213         DEVMETHOD(device_shutdown,      iwi_shutdown),
214         DEVMETHOD(device_suspend,       iwi_suspend),
215         DEVMETHOD(device_resume,        iwi_resume),
216
217         { 0, 0 }
218 };
219
220 static driver_t iwi_driver = {
221         "iwi",
222         iwi_methods,
223         sizeof (struct iwi_softc),
224         0, /* baseclasses */
225         0, /* refs */
226         0  /* ops */
227 };
228
229 static devclass_t iwi_devclass;
230
231 DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, 0, 0);
232
233 static int
234 iwi_probe(device_t dev)
235 {
236         const struct iwi_ident *ident;
237
238         for (ident = iwi_ident_table; ident->name != NULL; ident++) {
239                 if (pci_get_vendor(dev) == ident->vendor &&
240                     pci_get_device(dev) == ident->device) {
241                         device_set_desc(dev, ident->name);
242                         return 0;
243                 }
244         }
245         return ENXIO;
246 }
247
248 static void
249 iwi_fw_monitor(void *arg)
250 {
251         struct iwi_softc *sc = (struct iwi_softc *)arg;
252         int error, boff;
253         for ( ;; ) {
254                 error = tsleep(IWI_FW_WAKE_MONITOR(sc), 0, "iwifwm", 0 );
255                 if ( error == 0 ) {
256                         if ( sc->flags & IWI_FLAG_EXIT ) {
257                                 sc->flags &= ~( IWI_FLAG_EXIT );
258                                 break;
259                         } else if ( sc->flags & IWI_FLAG_RESET ) {
260                                 device_printf(sc->sc_dev, "firmware reset\n");
261                                 for ( boff = 1; sc->flags & IWI_FLAG_RESET ; boff++ ) {
262                                         if ( sc->debug_level > 0 )
263                                                 iwi_dump_fw_error_log(sc);
264                                         iwi_init_locked(sc);
265                                         if ((sc->flags & IWI_FLAG_FW_INITED))
266                                                 sc->flags &= ~( IWI_FLAG_RESET );
267                                         error = tsleep( IWI_FW_CMD_ACKED(sc), 0,
268                                                        "iwirun", boff * hz );
269                                 }
270                         }
271                 }
272         }
273         wakeup(IWI_FW_MON_EXIT(sc));
274         kthread_exit();
275 }
276
277 static int
278 iwi_start_fw_monitor_thread( struct iwi_softc *sc )
279 {
280        if (kthread_create(iwi_fw_monitor, sc, &sc->event_thread,
281                 "%s%d:fw-monitor", device_get_name(sc->sc_dev),
282                 device_get_unit(sc->sc_dev))) {
283                 device_printf (sc->sc_dev,
284                    "unable to create firmware monitor thread.\n");
285                 return -1;
286         }
287         return 0;
288 }
289
290 /* Base Address Register */
291 #define IWI_PCI_BAR0    0x10
292
293 static int
294 iwi_attach(device_t dev)
295 {
296         struct iwi_softc *sc = device_get_softc(dev);
297         struct ieee80211com *ic = &sc->sc_ic;
298         struct ifnet *ifp = &ic->ic_if;
299         u_int16_t val;
300         int error, rid, i;
301
302         sc->sc_dev = dev;
303
304         IWI_LOCK_INIT( &sc->sc_lock );
305         IWI_LOCK_INIT( &sc->sc_intrlock );
306
307         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
308                 device_printf(dev, "chip is in D%d power mode "
309                     "-- setting to D0\n", pci_get_powerstate(dev));
310                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
311         }
312
313         pci_write_config(dev, 0x41, 0, 1);
314
315         /* enable bus-mastering */
316         pci_enable_busmaster(dev);
317
318         sc->num_stations = 0;
319
320         /* map the register window */
321         rid = IWI_PCI_BAR0;
322         sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
323         if (sc->mem == NULL) {
324                 device_printf(dev, "could not allocate memory resource\n");
325                 goto fail;
326         }
327
328         sc->sc_st = rman_get_bustag(sc->mem);
329         sc->sc_sh = rman_get_bushandle(sc->mem);
330
331         rid = 0;
332         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
333             RF_SHAREABLE);
334         if (sc->irq == NULL) {
335                 device_printf(dev, "could not allocate interrupt resource\n");
336                 goto fail;
337         }
338
339         if (iwi_reset(sc) != 0) {
340                 device_printf(dev, "could not reset adapter\n");
341                 goto fail;
342         }
343
344         if (iwi_start_fw_monitor_thread(sc) ) {
345                 device_printf(dev, "could not start f/w reset thread\n");
346                 goto fail;
347         }
348
349         if (iwi_dma_alloc(sc) != 0) {
350                 device_printf(dev, "could not allocate DMA resources\n");
351                 goto fail;
352         }
353
354         ic->ic_phytype = IEEE80211_T_OFDM;
355         ic->ic_opmode  = IEEE80211_M_STA;
356         ic->ic_state   = IEEE80211_S_INIT;
357
358         /* set device capabilities */
359         ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_PMGT | IEEE80211_C_WEP |
360             IEEE80211_C_TXPMGT | IEEE80211_C_SHPREAMBLE;
361
362         /* read MAC address from EEPROM */
363         val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
364         ic->ic_myaddr[0] = val >> 8;
365         ic->ic_myaddr[1] = val & 0xff;
366         val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
367         ic->ic_myaddr[2] = val >> 8;
368         ic->ic_myaddr[3] = val & 0xff;
369         val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
370         ic->ic_myaddr[4] = val >> 8;
371         ic->ic_myaddr[5] = val & 0xff;
372
373         if (pci_get_device(dev) != 0x4220) {
374                 /* set supported .11a rates */
375                 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
376
377                 /* set supported .11a channels */
378                 for (i = 36; i <= 64; i += 4) {
379                         ic->ic_channels[i].ic_freq =
380                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
381                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
382                 }
383                 for (i = 149; i <= 165; i += 4) {
384                         ic->ic_channels[i].ic_freq =
385                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
386                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
387                 }
388         }
389
390         /* set supported .11b and .11g rates */
391         ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
392         ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
393
394         /* set supported .11b and .11g channels (1 through 14) */
395         for (i = 1; i <= 14; i++) {
396                 ic->ic_channels[i].ic_freq =
397                     ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
398                 ic->ic_channels[i].ic_flags =
399                     IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
400                     IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
401         }
402
403         /* default to authmode OPEN */
404         sc->authmode = IEEE80211_AUTH_OPEN;
405
406         /* IBSS channel undefined for now */
407         ic->ic_ibss_chan = &ic->ic_channels[0];
408
409         ifp->if_softc = sc;
410         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
411         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
412         ifp->if_init = iwi_init_locked;
413         ifp->if_ioctl = iwi_ioctl;
414         ifp->if_start = iwi_start;
415         ifp->if_watchdog = iwi_watchdog;
416         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
417         ifq_set_ready(&ifp->if_snd);
418
419         ieee80211_ifattach(ifp);
420         /* override state transition machine */
421         sc->sc_newstate = ic->ic_newstate;
422         ic->ic_newstate = iwi_newstate;
423         ieee80211_media_init(ifp, iwi_media_change, iwi_media_status);
424
425         bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
426             sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
427
428         sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
429         sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
430         sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
431
432         sc->sc_txtap_len = sizeof sc->sc_txtapu;
433         sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
434         sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
435
436         /*
437          * Hook our interrupt after all initialization is complete
438          */
439         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
440                                iwi_intr, sc, &sc->sc_ih, NULL);
441         if (error != 0) {
442                 device_printf(dev, "could not set up interrupt\n");
443                 goto fail;
444         }
445
446         /*
447          * Add sysctl knobs
448          * 
449          * use -1 to indicate 'default / not set'
450          */
451
452         sc->enable_bg_autodetect         = -1;
453         sc->enable_bt_coexist            = -1;
454         sc->enable_cts_to_self           = -1;
455         sc->antenna_diversity            = -1;
456         sc->enable_neg_best_first        = -1;
457         sc->disable_unicast_decryption   = -1;
458         sc->disable_multicast_decryption = -1;
459
460         sysctl_ctx_init(&sc->sysctl_ctx);
461         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
462                                 SYSCTL_STATIC_CHILDREN(_hw),
463                                 OID_AUTO,
464                                 device_get_nameunit(dev),
465                                 CTLFLAG_RD,
466                                 0, "");
467
468         if (sc->sysctl_tree == NULL) {
469                 error = EIO;
470                 goto fail;
471         }
472
473         SYSCTL_ADD_INT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
474                        OID_AUTO, "debug", CTLFLAG_RW, &sc->debug_level, 0,
475                       "Set driver debug level (0 = off)");
476
477         SYSCTL_ADD_PROC(&sc->sysctl_ctx,
478                         SYSCTL_CHILDREN(sc->sysctl_tree),
479                         OID_AUTO, "cts_to_self", CTLTYPE_INT|CTLFLAG_RW,
480                         (void *)sc, 0, iwi_sysctl_cts_to_self, "I", 
481                         "Enable cts to self [0 = Off] [1 = On] [-1 = Auto]" );
482
483         SYSCTL_ADD_PROC(&sc->sysctl_ctx,
484                         SYSCTL_CHILDREN(sc->sysctl_tree),
485                         OID_AUTO, "antenna_diversity", CTLTYPE_INT|CTLFLAG_RW,
486                         (void *)sc, 0, iwi_sysctl_antenna_diversity,
487                         "I", "Set antenna diversity [0 = Both] "
488                         "[1 = Antenna A] [3 = Antenna B] [-1 = Auto]" );
489
490         SYSCTL_ADD_PROC(&sc->sysctl_ctx,
491                         SYSCTL_CHILDREN(sc->sysctl_tree),
492                         OID_AUTO, "bluetooth_coexist", CTLTYPE_INT|CTLFLAG_RW,
493                         (void *)sc, 0, iwi_sysctl_bt_coexist,
494                         "I", "Enable bluetooth coexistence heuristics "
495                         "[0 = Off] [1 = On] [-1 = Auto]" );
496
497         SYSCTL_ADD_PROC(&sc->sysctl_ctx,
498                         SYSCTL_CHILDREN(sc->sysctl_tree),
499                         OID_AUTO, "bg_autodetect", CTLTYPE_INT|CTLFLAG_RW,
500                         (void *)sc, 0, iwi_sysctl_bg_autodetect,
501                         "I", "Set b/g autodetect [0 = Off] [1 = On] [-1 = Auto]" );
502
503
504         SYSCTL_ADD_PROC(&sc->sysctl_ctx,
505                         SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "radio",
506                         CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I",
507                         "Radio transmitter switch");
508
509         SYSCTL_ADD_PROC(&sc->sysctl_ctx,
510                         SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "stats",
511                         CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, 
512                         "S,iwi_dump_buffer", "statistics");
513
514         SYSCTL_ADD_PROC(&sc->sysctl_ctx,
515                         SYSCTL_CHILDREN(sc->sysctl_tree),
516                         OID_AUTO, "firmware_logs", CTLTYPE_INT|CTLFLAG_RW,
517                         (void *)sc, 0, iwi_sysctl_dump_logs, "I", "Dump firmware logs");
518
519         SYSCTL_ADD_PROC(&sc->sysctl_ctx,
520                         SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
521                         "neg_best_rates_first",
522                         CTLTYPE_INT | CTLFLAG_RW, sc, 0,
523                         iwi_sysctl_neg_best_rates_first, "I",
524                          "Negotiate highest rates first.");
525
526         SYSCTL_ADD_PROC(&sc->sysctl_ctx,
527                         SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
528                         "disable_unicast_decrypt",
529                         CTLTYPE_INT | CTLFLAG_RW, sc, 0,
530                         iwi_sysctl_disable_unicast_decryption, "I",
531                          "Disable unicast decryption.");
532
533         SYSCTL_ADD_PROC(&sc->sysctl_ctx,
534                         SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
535                         "disable_multicast_decrypt",
536                         CTLTYPE_INT | CTLFLAG_RW, sc, 0,
537                         iwi_sysctl_disable_multicast_decryption, "I",
538                          "Disable multicast decryption.");
539
540         return 0;
541
542 fail:   iwi_detach(dev);
543         return ENXIO;
544 }
545
546 static int
547 iwi_detach(device_t dev)
548 {
549         struct iwi_softc *sc = device_get_softc(dev);
550         struct ifnet *ifp = &sc->sc_ic.ic_if;
551         IWI_LOCK_INFO;
552         IWI_IPLLOCK_INFO;
553
554         sc->flags |= IWI_FLAG_EXIT;
555         wakeup(IWI_FW_WAKE_MONITOR(sc)); /* Stop firmware monitor. */
556
557         (void) tsleep(IWI_FW_MON_EXIT(sc), 0, "iwiexi", 10 * hz );
558
559         IWI_LOCK(sc);
560         IWI_IPLLOCK(sc);
561
562         iwi_stop(sc);
563         iwi_free_firmware(sc);
564
565         if ( sc->sysctl_tree ) {
566                 crit_enter();
567                 sysctl_ctx_free(&sc->sysctl_ctx);
568                 crit_exit();
569                 sc->sysctl_tree = 0;
570         }
571
572         IWI_IPLUNLOCK(sc);
573         IWI_UNLOCK(sc);
574
575         bpfdetach(ifp);
576
577         ieee80211_ifdetach(ifp);
578
579         iwi_release(sc);
580
581         if (sc->irq != NULL) {
582                 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
583                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
584         }
585
586         if (sc->mem != NULL)
587                 bus_release_resource(dev, SYS_RES_MEMORY, IWI_PCI_BAR0,
588                     sc->mem);
589
590         IWI_LOCK_DESTROY(&(sc->sc_lock));
591         IWI_LOCK_DESTROY(&(sc->sc_intrlock));
592
593         return 0;
594 }
595
596 static int
597 iwi_dma_alloc(struct iwi_softc *sc)
598 {
599         int i, error;
600
601         error = bus_dma_tag_create(NULL, /* parent */
602                                    1, 0,
603                                    BUS_SPACE_MAXADDR_32BIT,
604                                    BUS_SPACE_MAXADDR,
605                                    NULL, NULL,
606                                    MAXBSIZE, 128,
607                                    BUS_SPACE_MAXSIZE_32BIT,
608                                    BUS_DMA_ALLOCNOW,
609                                    &sc->iwi_parent_tag );
610         if (error != 0) {
611                 device_printf(sc->sc_dev, "could not create parent tag\n");
612                 goto fail;
613         }
614         /*
615          * Allocate and map Tx ring
616          */
617         error = bus_dma_tag_create(sc->iwi_parent_tag, 4, 0, BUS_SPACE_MAXADDR_32BIT,
618             BUS_SPACE_MAXADDR, NULL, NULL,
619             sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE, 1,
620             sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE,
621             BUS_DMA_ALLOCNOW, &sc->tx_ring_dmat);
622         if (error != 0) {
623                 device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
624                 goto fail;
625         }
626
627         error = bus_dmamem_alloc(sc->tx_ring_dmat,(void **) &sc->tx_desc,
628             BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->tx_ring_map);
629         if (error != 0) {
630                 device_printf(sc->sc_dev,
631                     "could not allocate tx ring DMA memory\n");
632                 goto fail;
633         }
634
635         error = bus_dmamap_load(sc->tx_ring_dmat, sc->tx_ring_map,
636             sc->tx_desc, sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE,
637             iwi_dma_map_addr, &sc->tx_ring_pa, 0);
638         if (error != 0) {
639                 device_printf(sc->sc_dev, "could not load tx ring DMA map\n");
640                 goto fail;
641         }
642
643         /*
644          * Allocate and map command ring
645          */
646         error = bus_dma_tag_create(sc->iwi_parent_tag, 4, 0, BUS_SPACE_MAXADDR_32BIT,
647             BUS_SPACE_MAXADDR, NULL, NULL,
648             sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE, 1,
649             sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE, 
650             BUS_DMA_ALLOCNOW,
651             &sc->cmd_ring_dmat);
652         if (error != 0) {
653                 device_printf(sc->sc_dev,
654                     "could not create command ring DMA tag\n");
655                 goto fail;
656         }
657
658         error = bus_dmamem_alloc(sc->cmd_ring_dmat, (void **)&sc->cmd_desc,
659             BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->cmd_ring_map);
660         if (error != 0) {
661                 device_printf(sc->sc_dev,
662                     "could not allocate command ring DMA memory\n");
663                 goto fail;
664         }
665
666         error = bus_dmamap_load(sc->cmd_ring_dmat, sc->cmd_ring_map,
667             sc->cmd_desc, sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE,
668             iwi_dma_map_addr, &sc->cmd_ring_pa, 0);
669         if (error != 0) {
670                 device_printf(sc->sc_dev,
671                     "could not load command ring DMA map\n");
672                 goto fail;
673         }
674
675         /*
676          * Allocate Tx buffers DMA maps
677          */
678         error = bus_dma_tag_create(sc->iwi_parent_tag, 1, 0, BUS_SPACE_MAXADDR_32BIT,
679             BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IWI_MAX_NSEG, MCLBYTES,
680             BUS_DMA_ALLOCNOW, &sc->tx_buf_dmat);
681         if (error != 0) {
682                 device_printf(sc->sc_dev, "could not create tx buf DMA tag\n");
683                 goto fail;
684         }
685
686         for (i = 0; i < IWI_TX_RING_SIZE; i++) {
687                 error = bus_dmamap_create(sc->tx_buf_dmat, 0,
688                     &sc->tx_buf[i].map);
689                 if (error != 0) {
690                         device_printf(sc->sc_dev,
691                             "could not create tx buf DMA map");
692                         goto fail;
693                 }
694         }
695
696         /*
697          * Allocate and map Rx buffers
698          */
699         error = bus_dma_tag_create(sc->iwi_parent_tag, 1, 0, BUS_SPACE_MAXADDR_32BIT,
700             BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES,
701             BUS_DMA_ALLOCNOW, &sc->rx_buf_dmat);
702         if (error != 0) {
703                 device_printf(sc->sc_dev, "could not create rx buf DMA tag\n");
704                 goto fail;
705         }
706
707         for (i = 0; i < IWI_RX_RING_SIZE; i++) {
708
709                 error = bus_dmamap_create(sc->rx_buf_dmat, 0,
710                     &sc->rx_buf[i].map);
711                 if (error != 0) {
712                         device_printf(sc->sc_dev,
713                             "could not create rx buf DMA map");
714                         goto fail;
715                 }
716
717                 sc->rx_buf[i].m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
718                 if (sc->rx_buf[i].m == NULL) {
719                         device_printf(sc->sc_dev,
720                             "could not allocate rx mbuf\n");
721                         error = ENOMEM;
722                         goto fail;
723                 }
724
725                 error = bus_dmamap_load(sc->rx_buf_dmat, sc->rx_buf[i].map,
726                     mtod(sc->rx_buf[i].m, void *), MCLBYTES, iwi_dma_map_addr,
727                     &sc->rx_buf[i].physaddr, 0);
728                 if (error != 0) {
729                         device_printf(sc->sc_dev,
730                             "could not load rx buf DMA map");
731                         goto fail;
732                 }
733         }
734
735         return 0;
736
737 fail:   iwi_release(sc);
738         return error;
739 }
740
741 static void
742 iwi_release(struct iwi_softc *sc)
743 {
744         int i;
745
746         if (sc->tx_ring_dmat != NULL) {
747                 if (sc->tx_desc != NULL) {
748                         bus_dmamap_sync(sc->tx_ring_dmat, sc->tx_ring_map,
749                             BUS_DMASYNC_POSTWRITE);
750                         bus_dmamap_unload(sc->tx_ring_dmat, sc->tx_ring_map);
751                         bus_dmamem_free(sc->tx_ring_dmat, sc->tx_desc,
752                             sc->tx_ring_map);
753                 }
754                 bus_dma_tag_destroy(sc->tx_ring_dmat);
755         }
756
757         if (sc->cmd_ring_dmat != NULL) {
758                 if (sc->cmd_desc != NULL) {
759                         bus_dmamap_sync(sc->cmd_ring_dmat, sc->cmd_ring_map,
760                             BUS_DMASYNC_POSTWRITE);
761                         bus_dmamap_unload(sc->cmd_ring_dmat, sc->cmd_ring_map);
762                         bus_dmamem_free(sc->cmd_ring_dmat, sc->cmd_desc,
763                             sc->cmd_ring_map);
764                 }
765                 bus_dma_tag_destroy(sc->cmd_ring_dmat);
766         }
767
768         if (sc->tx_buf_dmat != NULL) {
769                 for (i = 0; i < IWI_TX_RING_SIZE; i++) {
770                         if (sc->tx_buf[i].m != NULL) {
771                                 bus_dmamap_sync(sc->tx_buf_dmat,
772                                     sc->tx_buf[i].map, BUS_DMASYNC_POSTWRITE);
773                                 bus_dmamap_unload(sc->tx_buf_dmat,
774                                     sc->tx_buf[i].map);
775                                 m_freem(sc->tx_buf[i].m);
776                         }
777                         bus_dmamap_destroy(sc->tx_buf_dmat, sc->tx_buf[i].map);
778                 }
779                 bus_dma_tag_destroy(sc->tx_buf_dmat);
780         }
781
782         if (sc->rx_buf_dmat != NULL) {
783                 for (i = 0; i < IWI_RX_RING_SIZE; i++) {
784                         if (sc->rx_buf[i].m != NULL) {
785                                 bus_dmamap_sync(sc->rx_buf_dmat,
786                                     sc->rx_buf[i].map, BUS_DMASYNC_POSTREAD);
787                                 bus_dmamap_unload(sc->rx_buf_dmat,
788                                     sc->rx_buf[i].map);
789                                 m_freem(sc->rx_buf[i].m);
790                         }
791                         bus_dmamap_destroy(sc->rx_buf_dmat, sc->rx_buf[i].map);
792                 }
793                 bus_dma_tag_destroy(sc->rx_buf_dmat);
794         }
795         if ( sc->iwi_parent_tag != NULL ) {
796                 bus_dma_tag_destroy(sc->iwi_parent_tag);
797         }
798 }
799
800 static int
801 iwi_shutdown(device_t dev)
802 {
803         struct iwi_softc *sc = device_get_softc(dev);
804         IWI_LOCK_INFO;
805
806         IWI_LOCK(sc);
807
808         iwi_stop(sc);
809
810         IWI_UNLOCK(sc);
811
812         return 0;
813 }
814
815 static int
816 iwi_suspend(device_t dev)
817 {
818         struct iwi_softc *sc = device_get_softc(dev);
819
820         IWI_LOCK_INFO;
821
822         IWI_LOCK(sc);
823
824         iwi_stop(sc);
825
826         IWI_UNLOCK(sc);
827
828         return 0;
829 }
830
831 static int
832 iwi_resume(device_t dev)
833 {
834         struct iwi_softc *sc = device_get_softc(dev);
835         struct ifnet *ifp = &sc->sc_ic.ic_if;
836         IWI_LOCK_INFO;
837
838         IWI_LOCK(sc);
839
840         pci_write_config(dev, 0x41, 0, 1);
841
842         if (ifp->if_flags & IFF_UP) {
843                 ifp->if_init(ifp->if_softc);
844                 if (ifp->if_flags & IFF_RUNNING)
845                         ifp->if_start(ifp);
846         }
847
848         IWI_UNLOCK(sc);
849
850         return 0;
851 }
852
853 static int
854 iwi_media_change(struct ifnet *ifp)
855 {
856         struct iwi_softc *sc = ifp->if_softc;
857         int error = 0;
858         IWI_LOCK_INFO;
859
860         IWI_LOCK(sc);
861
862         error = ieee80211_media_change(ifp);
863         if (error != ENETRESET) {
864                 IWI_UNLOCK(sc);
865                 return error;
866         }
867         error = 0; /* clear ENETRESET */
868
869         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)){
870                 iwi_init(sc);
871                 error = tsleep( IWI_FW_CMD_ACKED(sc), 0, "iwirun", hz );
872         }
873
874
875         IWI_UNLOCK(sc);
876
877         return error;
878 }
879
880 static void
881 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
882 {
883         struct iwi_softc *sc = ifp->if_softc;
884         struct ieee80211com *ic = &sc->sc_ic;
885 #define N(a)    (sizeof (a) / sizeof (a[0]))
886         static const struct {
887                 u_int32_t       val;
888                 int             rate;
889         } rates[] = {
890                 { IWI_RATE_DS1,      2 },
891                 { IWI_RATE_DS2,      4 },
892                 { IWI_RATE_DS5,     11 },
893                 { IWI_RATE_DS11,    22 },
894                 { IWI_RATE_OFDM6,   12 },
895                 { IWI_RATE_OFDM9,   18 },
896                 { IWI_RATE_OFDM12,  24 },
897                 { IWI_RATE_OFDM18,  36 },
898                 { IWI_RATE_OFDM24,  48 },
899                 { IWI_RATE_OFDM36,  72 },
900                 { IWI_RATE_OFDM48,  96 },
901                 { IWI_RATE_OFDM54, 108 },
902         };
903         u_int32_t val, i;
904         int rate;
905
906         imr->ifm_status = IFM_AVALID;
907         imr->ifm_active = IFM_IEEE80211;
908         if (ic->ic_state == IEEE80211_S_RUN)
909                 imr->ifm_status |= IFM_ACTIVE;
910
911         /* read current transmission rate from adapter */
912         val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
913
914         /* convert rate to 802.11 rate */
915         for (i = 0; i < N(rates) && rates[i].val != val ; i++);
916         rate = (i < N(rates)) ? rates[i].rate : 0;
917
918         imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
919         switch (ic->ic_opmode) {
920         case IEEE80211_M_STA:
921                 break;
922
923         case IEEE80211_M_IBSS:
924                 imr->ifm_active |= IFM_IEEE80211_ADHOC;
925                 break;
926
927         case IEEE80211_M_MONITOR:
928                 imr->ifm_active |= IFM_IEEE80211_MONITOR;
929                 break;
930
931         case IEEE80211_M_AHDEMO:
932         case IEEE80211_M_HOSTAP:
933                 /* should not get there */
934                 break;
935         }
936 #undef N
937 }
938
939 static int
940 iwi_disassociate( struct iwi_softc *sc  )
941 {
942         sc->assoc.type = 2; /* DISASSOCIATE */
943         return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &sc->assoc, sizeof sc->assoc, 0);
944 }
945
946
947 static int
948 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg __unused)
949 {
950         struct iwi_softc *sc = ic->ic_softc;
951
952         switch (nstate) {
953         case IEEE80211_S_SCAN:
954                 if ( sc->flags & IWI_FLAG_ASSOCIATED ) {
955                         sc->flags &= ~( IWI_FLAG_ASSOCIATED );
956                         iwi_disassociate(sc);
957                         (void) tsleep( IWI_FW_DEASSOCIATED(sc), 
958                                         0, "iwisca", hz );
959                         
960                 }
961                 if ( !(sc->flags & IWI_FLAG_SCANNING) &&
962                      !(sc->flags & IWI_FLAG_RF_DISABLED) ) {
963                         iwi_scan(sc);
964                 }
965                 break;
966
967         case IEEE80211_S_AUTH:
968                 if ( sc->flags & IWI_FLAG_ASSOCIATED ) {
969                         sc->flags &= ~( IWI_FLAG_ASSOCIATED );
970                         iwi_disassociate(sc);
971                         (void) tsleep( IWI_FW_DEASSOCIATED(sc), 0,
972                                        "iwiaut", hz );
973                         
974                 }
975                 if ( iwi_auth_and_assoc(sc) != 0 )
976                         ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
977                 break;
978
979         case IEEE80211_S_RUN:
980                 if (sc->flags & IWI_FLAG_SCAN_COMPLETE) {
981                         sc->flags &= ~(IWI_FLAG_SCAN_COMPLETE);
982                         if (ic->ic_opmode == IEEE80211_M_IBSS ||
983                             ic->ic_opmode == IEEE80211_M_MONITOR ) {
984                                 /* 
985                                  * In IBSS mode, following an end_scan
986                                  * the ieee80211 stack state machine transitions
987                                  * straight to 'run' state. This is out of
988                                  * step with the firmware which requires
989                                  * an association first. Flip our state from
990                                  * RUN back to AUTH to allow us to tell the
991                                  * firmware to associate.
992                                  */
993                                 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
994                         }
995                 }
996                 break;
997
998         case IEEE80211_S_ASSOC:
999                 break;
1000         case IEEE80211_S_INIT:
1001                 sc->flags &= ~( IWI_FLAG_SCANNING | IWI_FLAG_ASSOCIATED );
1002                 break;
1003         }
1004
1005         ic->ic_state = nstate;
1006         return 0;
1007 }
1008
1009 /*
1010  * Read 16 bits at address 'addr' from the serial EEPROM.
1011  * DON'T PLAY WITH THIS CODE UNLESS YOU KNOW *EXACTLY* WHAT YOU'RE DOING!
1012  */
1013 static u_int16_t
1014 iwi_read_prom_word(struct iwi_softc *sc, u_int8_t addr)
1015 {
1016         u_int32_t tmp;
1017         u_int16_t val;
1018         int n;
1019
1020         /* Clock C once before the first command */
1021         IWI_EEPROM_CTL(sc, 0);
1022         IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1023         IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1024         IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1025
1026         /* Write start bit (1) */
1027         IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1028         IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1029
1030         /* Write READ opcode (10) */
1031         IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1032         IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1033         IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1034         IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1035
1036         /* Write address A7-A0 */
1037         for (n = 7; n >= 0; n--) {
1038                 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1039                     (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1040                 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1041                     (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1042         }
1043
1044         IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1045
1046         /* Read data Q15-Q0 */
1047         val = 0;
1048         for (n = 15; n >= 0; n--) {
1049                 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1050                 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1051                 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1052                 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1053         }
1054
1055         IWI_EEPROM_CTL(sc, 0);
1056
1057         /* Clear Chip Select and clock C */
1058         IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1059         IWI_EEPROM_CTL(sc, 0);
1060         IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1061
1062         return be16toh(val);
1063 }
1064
1065 /*
1066  * XXX: Hack to set the current channel to the value advertised in beacons or
1067  * probe responses. Only used during AP detection.
1068  */
1069 static void
1070 iwi_fix_channel(struct iwi_softc *sc, struct mbuf *m)
1071 {
1072         struct ieee80211com *ic = &sc->sc_ic;
1073         struct ieee80211_frame *wh;
1074         u_int8_t subtype;
1075         u_int8_t *frm, *efrm;
1076
1077         wh = mtod(m, struct ieee80211_frame *);
1078
1079         if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1080                 return;
1081
1082         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1083
1084         if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1085             subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1086                 return;
1087
1088         /*
1089          * Cache station entries from beacons and probes.
1090          */
1091         if ( iwi_find_station(sc, wh->i_addr2) == 0xff )
1092                 iwi_cache_station(sc, wh->i_addr2);
1093
1094         frm = (u_int8_t *)(wh + 1);
1095         efrm = mtod(m, u_int8_t *) + m->m_len;
1096
1097         frm += 12;      /* skip tstamp, bintval and capinfo fields */
1098 #if 0
1099         { /* XXX - debugging code */
1100                 u_int8_t *ptr;
1101                 u_int32_t cnt;
1102                 printf("Frame -->");
1103                 for ( ptr = frm, cnt = 0 ; ptr < efrm ; ptr++, cnt++ ) {
1104                         if ( cnt % 8 == 0 )
1105                                 printf("\n");
1106                         printf("0x%-2.2x ", *ptr);
1107                 }
1108                 printf("<-- End Frame\n");
1109         }
1110 #endif
1111
1112         while (frm < efrm) {
1113                 if (*frm == IEEE80211_ELEMID_DSPARMS)
1114 #if IEEE80211_CHAN_MAX < 255
1115                 if (frm[2] <= IEEE80211_CHAN_MAX)
1116 #endif
1117                         ic->ic_bss->ni_chan = &ic->ic_channels[frm[2]];
1118
1119                 frm += frm[1] + 2; /* advance to the next tag */
1120         }
1121 }
1122
1123 static void
1124 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_buf *buf, int i,
1125     struct iwi_frame *frame)
1126 {
1127         struct ieee80211com *ic = &sc->sc_ic;
1128         struct ifnet *ifp = &ic->ic_if;
1129         struct mbuf *m;
1130         struct ieee80211_frame *wh;
1131         struct ieee80211_node *ni;
1132         int error;
1133
1134         DPRINTFN(5, ("RX!DATA!%u!%u!%u\n", le16toh(frame->len), frame->chan,
1135             frame->rssi_dbm));
1136
1137         if (le16toh(frame->len) < sizeof (struct ieee80211_frame_min) ||
1138             le16toh(frame->len) > MCLBYTES) {
1139                 device_printf(sc->sc_dev, "bad frame length\n");
1140                 return;
1141         }
1142
1143         bus_dmamap_unload(sc->rx_buf_dmat, buf->map);
1144
1145         /* Finalize mbuf */
1146         m = buf->m;
1147         m->m_pkthdr.rcvif = ifp;
1148         m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1149             sizeof (struct iwi_frame) + le16toh(frame->len);
1150
1151         m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1152
1153         wh = mtod(m, struct ieee80211_frame *);
1154         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1155                 /*
1156                  * Hardware decrypts the frame itself but leaves the WEP bit
1157                  * set in the 802.11 header and don't remove the iv and crc
1158                  * fields
1159                  */
1160                 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1161                 bcopy(wh, (char *)wh + IEEE80211_WEP_IVLEN +
1162                     IEEE80211_WEP_KIDLEN, sizeof (struct ieee80211_frame));
1163                 m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
1164                 m_adj(m, -IEEE80211_WEP_CRCLEN);
1165                 wh = mtod(m, struct ieee80211_frame *);
1166         }
1167
1168         if (sc->sc_drvbpf != NULL) {
1169                 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1170
1171                 tap->wr_flags = 0;
1172                 tap->wr_rate = frame->rate;
1173                 tap->wr_chan_freq =
1174                     htole16(ic->ic_channels[frame->chan].ic_freq);
1175                 tap->wr_chan_flags =
1176                     htole16(ic->ic_channels[frame->chan].ic_flags);
1177                 tap->wr_antsignal = frame->signal;
1178                 tap->wr_antnoise = frame->noise;
1179                 tap->wr_antenna = frame->antenna;
1180
1181                 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1182         }
1183
1184         if (ic->ic_state == IEEE80211_S_SCAN)
1185                 iwi_fix_channel(sc, m);
1186
1187         if (ic->ic_opmode != IEEE80211_M_STA) {
1188                 ni = ieee80211_find_node(ic, wh->i_addr2);
1189                 if (ni == NULL)
1190                         ni = ieee80211_ref_node(ic->ic_bss);
1191         } else
1192                 ni = ieee80211_ref_node(ic->ic_bss);
1193
1194         /* Send the frame to the upper layer */
1195         ieee80211_input(ifp, m, ni, IWI_RSSIDBM2RAW(frame->rssi_dbm), 0);
1196
1197         if (ni == ic->ic_bss)
1198                 ieee80211_unref_node(&ni);
1199         else
1200                 ieee80211_free_node(ic, ni);
1201
1202         buf->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1203         if (buf->m == NULL) {
1204                 device_printf(sc->sc_dev, "could not allocate rx mbuf\n");
1205                 return;
1206         }
1207
1208         error = bus_dmamap_load(sc->rx_buf_dmat, buf->map, mtod(buf->m, void *),
1209             MCLBYTES, iwi_dma_map_addr, &buf->physaddr, 0);
1210         if (error != 0) {
1211                 device_printf(sc->sc_dev, "could not load rx buf DMA map\n");
1212                 m_freem(buf->m);
1213                 buf->m = NULL;
1214                 return;
1215         }
1216
1217         CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, buf->physaddr);
1218 }
1219
1220 static void
1221 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1222 {
1223         struct ieee80211com *ic = &sc->sc_ic;
1224         struct ifnet *ifp = &ic->ic_if;
1225         struct iwi_notif_scan_channel *chan;
1226         struct iwi_notif_scan_complete *scan;
1227         struct iwi_notif_authentication *auth;
1228         struct iwi_notif_association *assoc;
1229
1230         switch (notif->type) {
1231         case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1232                 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1233
1234                 DPRINTFN(2, ("Scan channel (%u)\n", chan->nchan));
1235                 break;
1236
1237         case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1238                 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1239
1240                 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1241                     scan->status));
1242
1243                 sc->flags &= ~(IWI_FLAG_SCANNING);
1244                 sc->flags |= IWI_FLAG_SCAN_COMPLETE;
1245
1246                 if ( sc->flags & IWI_FLAG_SCAN_ABORT ) {
1247                         sc->flags &= ~(IWI_FLAG_SCAN_ABORT);
1248                         wakeup(IWI_FW_SCAN_COMPLETED(sc));
1249                 } else {
1250                         ieee80211_end_scan(ifp);
1251                         wakeup(IWI_FW_SCAN_COMPLETED(sc));
1252                 }
1253                 break;
1254
1255         case IWI_NOTIF_TYPE_AUTHENTICATION:
1256                 auth = (struct iwi_notif_authentication *)(notif + 1);
1257
1258                 DPRINTFN(2, ("Authentication (%u)\n", auth->state));
1259
1260                 switch (auth->state) {
1261                 case IWI_AUTHENTICATED:
1262                         ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1263                         break;
1264
1265                 case IWI_DEAUTHENTICATED:
1266                         ieee80211_begin_scan(ifp);/* not necessary */
1267                         break;
1268
1269                 default:
1270                         device_printf(sc->sc_dev,
1271                             "unknown authentication state %u\n", auth->state);
1272                 }
1273                 break;
1274
1275         case IWI_NOTIF_TYPE_ASSOCIATION:
1276                 assoc = (struct iwi_notif_association *)(notif + 1);
1277
1278                 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
1279                     assoc->status));
1280
1281                 switch (assoc->state) {
1282                 case IWI_ASSOCIATED:
1283                         sc->flags |= IWI_FLAG_ASSOCIATED;
1284                         ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1285                         break;
1286
1287                 case IWI_DEASSOCIATED:
1288                         sc->flags &= ~(IWI_FLAG_ASSOCIATED);
1289                         wakeup(IWI_FW_DEASSOCIATED(sc));
1290                         ieee80211_begin_scan(ifp);/* probably not necessary */
1291                         break;
1292
1293                 default:
1294                         device_printf(sc->sc_dev,
1295                             "unknown association state %u\n", assoc->state);
1296                 }
1297                 break;
1298
1299         case IWI_NOTIF_TYPE_CALIBRATION:
1300                 DPRINTFN(5, ("Notification calib (%u)\n", notif->type));
1301                 break;
1302         case IWI_NOTIF_TYPE_BEACON:
1303                 DPRINTFN(5, ("Notification beacon (%u)\n", notif->type));
1304                 break;
1305         case IWI_NOTIF_TYPE_NOISE:
1306                 DPRINTFN(5, ("Notification noise (%u)\n", notif->type));
1307                 break;
1308
1309         default:
1310                 device_printf(sc->sc_dev, "unknown notification type %u\n",
1311                     notif->type);
1312         }
1313 }
1314
1315 static void
1316 iwi_rx_intr(struct iwi_softc *sc)
1317 {
1318         struct iwi_rx_buf *buf;
1319         struct iwi_hdr *hdr;
1320         u_int32_t r, i;
1321
1322         r = CSR_READ_4(sc, IWI_CSR_RX_READ_INDEX);
1323
1324         for (i = (sc->rx_cur + 1) % IWI_RX_RING_SIZE; i != r;
1325              i = (i + 1) % IWI_RX_RING_SIZE) {
1326
1327                 buf = &sc->rx_buf[i];
1328
1329                 bus_dmamap_sync(sc->rx_buf_dmat, buf->map,
1330                     BUS_DMASYNC_POSTREAD);
1331
1332                 hdr = mtod(buf->m, struct iwi_hdr *);
1333
1334                 switch (hdr->type) {
1335                 case IWI_HDR_TYPE_FRAME:
1336                         iwi_frame_intr(sc, buf, i,
1337                             (struct iwi_frame *)(hdr + 1));
1338                         break;
1339
1340                 case IWI_HDR_TYPE_NOTIF:
1341                         iwi_notification_intr(sc,
1342                             (struct iwi_notif *)(hdr + 1));
1343                         break;
1344
1345                 default:
1346                         device_printf(sc->sc_dev, "unknown hdr type %u\n",
1347                             hdr->type);
1348                 }
1349         }
1350
1351         /* Tell the firmware what we have processed */
1352         sc->rx_cur = (r == 0) ? IWI_RX_RING_SIZE - 1 : r - 1;
1353         CSR_WRITE_4(sc, IWI_CSR_RX_WRITE_INDEX, sc->rx_cur);
1354 }
1355
1356 static void
1357 iwi_tx_intr(struct iwi_softc *sc)
1358 {
1359         struct ieee80211com *ic = &sc->sc_ic;
1360         struct ifnet *ifp = &ic->ic_if;
1361         struct iwi_tx_buf *buf;
1362         u_int32_t r, i;
1363
1364         r = CSR_READ_4(sc, IWI_CSR_TX1_READ_INDEX);
1365 #if  notyet
1366         bus_dmamap_sync(sc->tx_ring_dmat, sc->tx_ring_map, BUS_DMASYNC_POSTWRITE);
1367 #endif
1368
1369         for (i = (sc->tx_old + 1) % IWI_TX_RING_SIZE; i != r;
1370              i = (i + 1) % IWI_TX_RING_SIZE) {
1371
1372                 buf = &sc->tx_buf[i];
1373
1374                 bus_dmamap_sync(sc->tx_buf_dmat, buf->map,
1375                     BUS_DMASYNC_POSTWRITE);
1376                 bus_dmamap_unload(sc->tx_buf_dmat, buf->map);
1377                 m_freem(buf->m);
1378                 buf->m = NULL;
1379                 if (buf->ni != ic->ic_bss)
1380                         ieee80211_free_node(ic, buf->ni);
1381                 buf->ni = NULL;
1382
1383                 sc->tx_queued--;
1384
1385                 /* kill watchdog timer */
1386                 sc->sc_tx_timer = 0;
1387         }
1388
1389         /* Remember what the firmware has processed */
1390         sc->tx_old = (r == 0) ? IWI_TX_RING_SIZE - 1 : r - 1;
1391
1392         /* Call start() since some buffer descriptors have been released */
1393         ifp->if_flags &= ~IFF_OACTIVE;
1394         (*ifp->if_start)(ifp);
1395 }
1396
1397 static void
1398 iwi_intr(void *arg)
1399 {
1400         struct iwi_softc *sc = arg;
1401         struct ieee80211com *ic = &sc->sc_ic;
1402         struct ifnet *ifp = &ic->ic_if;
1403         u_int32_t r;
1404         IWI_LOCK_INFO;
1405         IWI_IPLLOCK_INFO;
1406
1407         IWI_IPLLOCK(sc);
1408
1409         if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) {
1410                 IWI_IPLUNLOCK(sc);
1411                 return;
1412         }
1413
1414         /* Disable interrupts */
1415         CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1416
1417         DPRINTFN(8, ("INTR!0x%08x\n", r));
1418
1419         sc->flags &= ~(IWI_FLAG_RF_DISABLED);
1420
1421         if ( r & IWI_INTR_FATAL_ERROR ) {
1422                 if ( !(sc->flags & (IWI_FLAG_RESET | IWI_FLAG_EXIT))) {
1423                         sc->flags |= IWI_FLAG_RESET;
1424                         wakeup(IWI_FW_WAKE_MONITOR(sc));
1425                 }
1426         }
1427
1428         if (r & IWI_INTR_PARITY_ERROR) {
1429                         device_printf(sc->sc_dev, "fatal error\n");
1430                         sc->sc_ic.ic_if.if_flags &= ~IFF_UP;
1431                         IWI_LOCK(sc);
1432                         iwi_stop(sc);
1433                         IWI_UNLOCK(sc);
1434         }
1435
1436         if (r & IWI_INTR_FW_INITED) {
1437                 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1438                         wakeup(IWI_FW_INITIALIZED(sc));
1439         }
1440
1441         if (r & IWI_INTR_RADIO_OFF) {
1442                 DPRINTF(("radio transmitter off\n"));
1443                 sc->sc_ic.ic_if.if_flags &= ~IFF_UP;
1444                 IWI_LOCK(sc);
1445                 iwi_stop(sc);
1446                 IWI_UNLOCK(sc);
1447                 sc->flags |= IWI_FLAG_RF_DISABLED;
1448         }
1449
1450         if (r & IWI_INTR_RX_TRANSFER)
1451                 iwi_rx_intr(sc);
1452
1453         if (r & IWI_INTR_CMD_TRANSFER)
1454                 wakeup(IWI_FW_CMD_ACKED(sc));
1455
1456         if (r & IWI_INTR_TX1_TRANSFER)
1457                 iwi_tx_intr(sc);
1458
1459         if (r & ~(IWI_HANDLED_INTR_MASK))
1460                 device_printf(sc->sc_dev, 
1461                               "unhandled interrupt(s) INTR!0x%08x\n",
1462                               r & ~(IWI_HANDLED_INTR_MASK));
1463
1464         /* Acknowledge interrupts */
1465         CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1466
1467         /* Re-enable interrupts */
1468         CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1469
1470         IWI_IPLUNLOCK(sc);
1471
1472         if ((ifp->if_flags & IFF_RUNNING) && !ifq_is_empty(&ifp->if_snd))
1473                 iwi_start(ifp);
1474 }
1475
1476 struct iwi_dma_mapping {
1477         bus_dma_segment_t segs[IWI_MAX_NSEG];
1478         int nseg;
1479         bus_size_t mapsize;
1480 };
1481
1482 static void
1483 iwi_dma_map_buf(void *arg, bus_dma_segment_t *segs, int nseg,
1484     bus_size_t mapsize, int error)
1485 {
1486         struct iwi_dma_mapping *map = arg;
1487
1488         if (error != 0)
1489                 return;
1490
1491         KASSERT(nseg <= IWI_MAX_NSEG, ("too many DMA segments %d", nseg));
1492
1493         bcopy(segs, map->segs, nseg * sizeof (bus_dma_segment_t));
1494         map->nseg = nseg;
1495         map->mapsize = mapsize;
1496 }
1497
1498 static void
1499 iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg __unused, int error)
1500 {
1501         if (error != 0) {
1502                 printf("iwi: fatal DMA mapping error !!!\n");
1503                 return;
1504         }
1505
1506         KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1507
1508         *(bus_addr_t *)arg = segs[0].ds_addr;
1509 }
1510
1511 static int
1512 iwi_cmd(struct iwi_softc *sc, u_int8_t type, void *data, u_int8_t len,
1513     int async)
1514 {
1515         struct iwi_cmd_desc *desc;
1516
1517         DPRINTFN(2, ("TX!CMD!%u!%u\n", type, len));
1518
1519         desc = &sc->cmd_desc[sc->cmd_cur];
1520         desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1521         desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1522         desc->type = type;
1523         desc->len = len;
1524         bcopy(data, desc->data, len);
1525
1526         bus_dmamap_sync(sc->cmd_ring_dmat, sc->cmd_ring_map,
1527             BUS_DMASYNC_PREWRITE);
1528
1529         sc->cmd_cur = (sc->cmd_cur + 1) % IWI_CMD_RING_SIZE;
1530         CSR_WRITE_4(sc, IWI_CSR_CMD_WRITE_INDEX, sc->cmd_cur);
1531
1532         return async ? 0 : tsleep( IWI_FW_CMD_ACKED(sc), 0, "iwicmd", hz);
1533 }
1534
1535 static int
1536 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1537 {
1538         struct iwi_softc *sc = ifp->if_softc;
1539         struct ieee80211com *ic = &sc->sc_ic;
1540         struct ieee80211_frame *wh;
1541         struct iwi_tx_buf *buf;
1542         struct iwi_tx_desc *desc;
1543         struct iwi_dma_mapping map;
1544         struct mbuf *mnew;
1545         u_int32_t id = 0;
1546         int error, i;
1547         IWI_IPLLOCK_INFO; /* XXX still need old ipl locking mech. here */
1548         IWI_IPLLOCK(sc);
1549
1550         if (sc->sc_drvbpf != NULL) {
1551                 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1552
1553                 tap->wt_flags = 0;
1554                 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1555                 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1556
1557                 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1558         }
1559
1560         buf = &sc->tx_buf[sc->tx_cur];
1561         desc = &sc->tx_desc[sc->tx_cur];
1562
1563         wh = mtod(m0, struct ieee80211_frame *);
1564
1565         if ( (id = iwi_find_station( sc, wh->i_addr1 ) ) == 0xff )
1566                 id = iwi_cache_station( sc, wh->i_addr1 );
1567
1568         bzero( desc, sizeof (struct iwi_tx_desc) );
1569         desc->station_number = id;
1570
1571         /* trim IEEE802.11 header */
1572         m_adj(m0, sizeof (struct ieee80211_frame));
1573
1574         error = bus_dmamap_load_mbuf(sc->tx_buf_dmat, buf->map, m0,
1575             iwi_dma_map_buf, &map, BUS_DMA_NOWAIT);
1576         if (error != 0 && error != EFBIG) {
1577                 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1578                     error);
1579                 m_freem(m0);
1580                 IWI_IPLUNLOCK(sc);
1581                 return error;
1582         }
1583         if (error != 0) {
1584                 mnew = m_defrag(m0, MB_DONTWAIT);
1585                 if (mnew == NULL) {
1586                         device_printf(sc->sc_dev,
1587                             "could not defragment mbuf\n");
1588                         m_freem(m0);
1589                         IWI_IPLUNLOCK(sc);
1590                         return ENOBUFS;
1591                 }
1592                 m0 = mnew;
1593
1594                 error = bus_dmamap_load_mbuf(sc->tx_buf_dmat, buf->map, m0,
1595                     iwi_dma_map_buf, &map, BUS_DMA_NOWAIT);
1596                 if (error != 0) {
1597                         device_printf(sc->sc_dev,
1598                             "could not map mbuf (error %d)\n", error);
1599                         m_freem(m0);
1600                         IWI_IPLUNLOCK(sc);
1601                         return error;
1602                 }
1603         }
1604
1605         buf->m = m0;
1606         buf->ni = ni;
1607
1608         desc->hdr.type = IWI_HDR_TYPE_DATA;
1609         desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1610         desc->cmd = IWI_DATA_CMD_TX;
1611         desc->len = htole16(m0->m_pkthdr.len);
1612         desc->flags = 0;
1613         if (ic->ic_opmode == IEEE80211_M_IBSS) {
1614                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
1615                         desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1616         } else if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
1617                 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1618
1619         if (ic->ic_flags & IEEE80211_F_WEPON) {
1620                 wh->i_fc[1] |= IEEE80211_FC1_WEP;
1621                 desc->wep_txkey = ic->ic_wep_txkey;
1622         } else
1623                 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1624
1625         if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1626                 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1627
1628         bcopy(wh, &desc->wh, sizeof (struct ieee80211_frame));
1629         desc->nseg = htole32(map.nseg);
1630         for (i = 0; i < map.nseg; i++) {
1631                 desc->seg_addr[i] = htole32(map.segs[i].ds_addr);
1632                 desc->seg_len[i]  = htole32(map.segs[i].ds_len);
1633         }
1634
1635         bus_dmamap_sync(sc->tx_buf_dmat, buf->map, BUS_DMASYNC_PREWRITE);
1636         bus_dmamap_sync(sc->tx_ring_dmat, sc->tx_ring_map,
1637             BUS_DMASYNC_PREWRITE);
1638
1639         DPRINTFN(5, ("TX!DATA!%u!%u\n", desc->len, desc->nseg));
1640
1641         /* Inform firmware about this new packet */
1642         sc->tx_queued++;
1643         sc->tx_cur = (sc->tx_cur + 1) % IWI_TX_RING_SIZE;
1644         CSR_WRITE_4(sc, IWI_CSR_TX1_WRITE_INDEX, sc->tx_cur);
1645
1646         IWI_IPLUNLOCK(sc);
1647         return 0;
1648 }
1649
1650 static void
1651 iwi_start(struct ifnet *ifp)
1652 {
1653         struct iwi_softc *sc = ifp->if_softc;
1654         struct ieee80211com *ic = &sc->sc_ic;
1655         struct mbuf *m0;
1656         struct ieee80211_node *ni;
1657
1658         if (ic->ic_state != IEEE80211_S_RUN) {
1659                 return;
1660         }
1661
1662         for (;;) {
1663                 m0 = ifq_poll(&ifp->if_snd);
1664                 if (m0 == NULL)
1665                         break;
1666
1667                 if (sc->tx_queued >= IWI_TX_RING_SIZE - 4) {
1668                         ifp->if_flags |= IFF_OACTIVE;
1669                         break;
1670                 }
1671  
1672                 m0 = ifq_dequeue(&ifp->if_snd);
1673
1674                 BPF_MTAP(ifp, m0);
1675
1676                 m0 = ieee80211_encap(ifp, m0, &ni);
1677                 if (m0 == NULL)
1678                         continue;
1679
1680                 if (ic->ic_rawbpf != NULL)
1681                         bpf_mtap(ic->ic_rawbpf, m0);
1682
1683                 if (iwi_tx_start(ifp, m0, ni) != 0) {
1684                         if (ni != NULL && ni != ic->ic_bss)
1685                                 ieee80211_free_node(ic, ni);
1686                         break;
1687                 }
1688
1689                 /* start watchdog timer */
1690                 sc->sc_tx_timer = 5;
1691                 ifp->if_timer = 1;
1692         }
1693
1694 }
1695
1696 static void
1697 iwi_watchdog(struct ifnet *ifp)
1698 {
1699         struct iwi_softc *sc = ifp->if_softc;
1700
1701         ifp->if_timer = 0;
1702
1703         if (sc->sc_tx_timer > 0) {
1704                 if (--sc->sc_tx_timer == 0) {
1705                         if_printf(ifp, "device timeout\n");
1706                         wakeup(IWI_FW_WAKE_MONITOR(sc));
1707                         return;
1708                 }
1709                 ifp->if_timer = 1;
1710         }
1711
1712         ieee80211_watchdog(ifp);
1713 }
1714
1715
1716 static int
1717 iwi_wi_ioctl_get(struct ifnet *ifp, caddr_t data)
1718 {
1719         struct wi_req           wreq;
1720         struct ifreq            *ifr;
1721         struct iwi_softc        *sc;
1722         int                     error;
1723
1724         sc = ifp->if_softc;
1725         ifr = (struct ifreq *)data;
1726         error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1727         if (error)
1728                 return (error);
1729
1730         switch (wreq.wi_type) {
1731         case WI_RID_READ_APS:
1732                 ieee80211_begin_scan(ifp);
1733                 (void) tsleep(IWI_FW_SCAN_COMPLETED(sc), 
1734                         PCATCH, "ssidscan", hz * 2);
1735                 ieee80211_end_scan(ifp);
1736                 break;
1737         default:
1738                 error = ENOTTY;
1739                 break;
1740         }
1741         return (error);
1742 }
1743
1744
1745
1746
1747 static int
1748 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1749 {
1750         struct iwi_softc *sc = ifp->if_softc;
1751         struct ifreq *ifr;
1752         struct ieee80211req *ireq;
1753         struct ifaddr *ifa;
1754         int error = 0;
1755         IWI_LOCK_INFO;
1756
1757         IWI_LOCK(sc);
1758
1759         switch (cmd) {
1760        case SIOCSIFADDR:
1761                 /*
1762                  * Handle this here instead of in net80211_ioctl.c
1763                  * so that we can lock (IWI_LOCK) the call to
1764                  * iwi_init().
1765                  */
1766                 ifa = (struct ifaddr *) data;
1767                 switch (ifa->ifa_addr->sa_family) {
1768 #ifdef INET
1769                 case AF_INET:
1770                         if ((ifp->if_flags & IFF_UP) == 0) {
1771                                 ifp->if_flags |= IFF_UP;
1772                                 ifp->if_init(ifp->if_softc);
1773                         }
1774                         arp_ifinit(ifp, ifa);
1775                         break;
1776 #endif
1777 #ifdef IPX
1778 #warning "IPX support has not been tested"
1779                 /*
1780                  * XXX - This code is probably wrong,
1781                  *       but has been copied many times.
1782                  */
1783                 case AF_IPX: {
1784                         struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1785                         struct arpcom *ac = (struct arpcom *)ifp;
1786
1787                         if (ipx_nullhost(*ina))
1788                                 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
1789                         else
1790                                 bcopy((caddr_t) ina->x_host.c_host,
1791                                       (caddr_t) ac->ac_enaddr,
1792                                       sizeof(ac->ac_enaddr));
1793                         /* fall thru... */
1794                 }
1795 #endif
1796                 default:
1797                         if ((ifp->if_flags & IFF_UP) == 0) {
1798                                 ifp->if_flags |= IFF_UP;
1799                                 ifp->if_init(ifp->if_softc);
1800                         }
1801                         break;
1802                 }
1803                 break;
1804
1805         case SIOCSIFFLAGS:
1806                 if (ifp->if_flags & IFF_UP) {
1807                         if (!(ifp->if_flags & IFF_RUNNING)) {
1808                                 iwi_init(sc);
1809                                 error = tsleep(IWI_FW_CMD_ACKED(sc), 0,
1810                                                  "iwirun", hz);
1811                         }
1812                 } else {
1813                         if (ifp->if_flags & IFF_RUNNING) {
1814                                 iwi_stop(sc);
1815                         }
1816                 }
1817                 break;
1818
1819         case SIOCSLOADFW:
1820         case SIOCSLOADIBSSFW:
1821                 /* only super-user can do that! */
1822                 if ((error = suser(curthread)) != 0)
1823                         break;
1824
1825                 ifr = (struct ifreq *)data;
1826                 error = iwi_cache_firmware(sc, ifr->ifr_data,
1827                                 (cmd == SIOCSLOADIBSSFW) ? 1 : 0);
1828                 break;
1829
1830         case SIOCSKILLFW:
1831                 /* only super-user can do that! */
1832                 if ((error = suser(curthread)) != 0)
1833                         break;
1834
1835                 ifp->if_flags &= ~IFF_UP;
1836                 iwi_stop(sc);
1837                 iwi_free_firmware(sc);
1838                 break;
1839
1840         case SIOCG80211:
1841                 ireq = (struct ieee80211req *)data;
1842                 switch (ireq->i_type) {
1843                 case IEEE80211_IOC_AUTHMODE:
1844                         ireq->i_val = sc->authmode;
1845                         break;
1846
1847                 default:
1848                         error = ieee80211_ioctl(ifp, cmd, data, cr);
1849                 }
1850                 break;
1851
1852         case SIOCS80211:
1853                 /* only super-user can do that! */
1854                 if ((error = suser(curthread)) != 0)
1855                         break;
1856
1857                 ireq = (struct ieee80211req *)data;
1858                 switch (ireq->i_type) {
1859                 case IEEE80211_IOC_AUTHMODE:
1860                         sc->authmode = ireq->i_val;
1861                         break;
1862
1863                 default:
1864                         error = ieee80211_ioctl(ifp, cmd, data, cr);
1865                 }
1866                 break;
1867         case SIOCGIFGENERIC:
1868                 if (sc->flags & IWI_FLAG_FW_INITED) {
1869                         error = iwi_wi_ioctl_get(ifp, data);
1870                         if (! error)
1871                                 error = ieee80211_ioctl(ifp, cmd, data, cr);
1872                 } else
1873                         error = ENOTTY;
1874                 if (error != ENOTTY)
1875                         break;
1876
1877         default:
1878                 error = ieee80211_ioctl(ifp, cmd, data, cr);
1879         }
1880
1881         if (error == ENETRESET) {
1882                 error = 0;
1883                 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1884                     (IFF_UP | IFF_RUNNING)) {
1885                         iwi_init(sc);
1886                         error = tsleep(IWI_FW_CMD_ACKED(sc), 0, "iwirun", hz);
1887                 }
1888         }
1889
1890         IWI_UNLOCK(sc);
1891
1892         return error;
1893 }
1894
1895 static int
1896 iwi_abort_scan( struct iwi_softc *sc  )
1897 {
1898         sc->flags |= IWI_FLAG_SCAN_ABORT;
1899         return iwi_cmd(sc, IWI_CMD_SCAN_ABORT, NULL, 0, 1);
1900 }
1901
1902 static void
1903 iwi_stop_master(struct iwi_softc *sc)
1904 {
1905         int ntries;
1906
1907         /*
1908          * If the master is busy scanning, we will occasionally 
1909          * timeout waiting for it (the master) to stop. Make the
1910          * 'stopping' process more robust by ceasing all scans
1911          * prior to asking for the stop.
1912          */
1913         if ( ( sc->flags & IWI_FLAG_SCANNING ) && 
1914              !( sc->flags & IWI_FLAG_RF_DISABLED ) ) {
1915                 iwi_abort_scan(sc);
1916                 if (( sc->flags & IWI_FLAG_SCAN_ABORT ) && 
1917                     !( sc->flags & IWI_FLAG_RF_DISABLED )) {
1918                         (void) tsleep(IWI_FW_SCAN_COMPLETED(sc), 0,
1919                                          "iwiabr", hz);
1920                 }
1921         }
1922         /* Disable interrupts */
1923
1924         CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1925
1926         CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1927         for (ntries = 0; ntries < 5; ntries++) {
1928                 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1929                         break;
1930                 DELAY(10);
1931         }
1932         if (ntries == 5 && sc->debug_level > 0) 
1933                 device_printf(sc->sc_dev, "timeout waiting for master\n");
1934
1935         CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1936             IWI_RST_PRINCETON_RESET);
1937
1938         sc->flags &= ~IWI_FLAG_FW_INITED;
1939 }
1940
1941 static int
1942 iwi_reset(struct iwi_softc *sc)
1943 {
1944         int i, ntries;
1945
1946         iwi_stop_master(sc);
1947
1948         /* Move adapter to D0 state */
1949         CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1950             IWI_CTL_INIT);
1951
1952         /* Initialize Phase-Locked Level  (PLL) */
1953         CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
1954
1955         /* Wait for clock stabilization */
1956         for (ntries = 0; ntries < 1000; ntries++) {
1957                 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
1958                         break;
1959                 DELAY(200);
1960         }
1961         if (ntries == 1000) {
1962                 return EIO;
1963         }
1964
1965         CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1966             IWI_RST_SW_RESET);
1967
1968         DELAY(10);
1969
1970         CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1971             IWI_CTL_INIT);
1972
1973
1974         /* Clear NIC memory */
1975         CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
1976
1977         for (i = 0; i < 0xc000; i++) {
1978                 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1979         }
1980
1981         sc->num_stations = 0;
1982         return 0;
1983 }
1984
1985 static int
1986 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
1987 {
1988         u_int16_t *w;
1989         int ntries, i;
1990
1991         CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1992             IWI_RST_STOP_MASTER);
1993         for (ntries = 0; ntries < 5; ntries++) {
1994                 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1995                         break;
1996                 DELAY(10);
1997         }
1998         if (ntries == 5) {
1999                 device_printf(sc->sc_dev, "timeout waiting for master\n");
2000                 return EIO;
2001         }
2002
2003         MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2004         DELAY(5000);
2005         CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
2006             ~IWI_RST_PRINCETON_RESET);
2007         DELAY(5000);
2008         MEM_WRITE_4(sc, 0x3000e0, 0);
2009         DELAY(1000);
2010         MEM_WRITE_4(sc, 0x300004, 1);
2011         DELAY(1000);
2012         MEM_WRITE_4(sc, 0x300004, 0);
2013         DELAY(1000);
2014         MEM_WRITE_1(sc, 0x200000, 0x00);
2015         MEM_WRITE_1(sc, 0x200000, 0x40);
2016         DELAY(1000);
2017
2018         /* Adapter is buggy, we must set the address for each word */
2019         for (w = uc; size > 0; w++, size -= 2)
2020                 MEM_WRITE_2(sc, 0x200010, *w);
2021
2022         MEM_WRITE_1(sc, 0x200000, 0x00);
2023         MEM_WRITE_1(sc, 0x200000, 0x80);
2024
2025         /* Wait until we get a response in the uc queue */
2026         for (ntries = 0; ntries < 100; ntries++) {
2027                 if (MEM_READ_1(sc, 0x200000) & 1)
2028                         break;
2029                 DELAY(100);
2030         }
2031         if (ntries == 100) {
2032                 device_printf(sc->sc_dev,
2033                     "timeout waiting for ucode to initialize\n");
2034                 return EIO;
2035         }
2036
2037         /* Empty the uc queue or the firmware will not initialize properly */
2038         for (i = 0; i < 7; i++)
2039                 MEM_READ_4(sc, 0x200004);
2040
2041         MEM_WRITE_1(sc, 0x200000, 0x00);
2042
2043         return 0;
2044 }
2045
2046 /* macro to handle unaligned little endian data in firmware image */
2047 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2048 static int
2049 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
2050 {
2051         bus_dma_tag_t dmat;
2052         bus_dmamap_t map;
2053         bus_addr_t physaddr;
2054         void *virtaddr;
2055         u_char *p, *end;
2056         u_int32_t sentinel, ctl, src, dst, sum, len, mlen;
2057         int ntries, error = 0;
2058
2059         sc->flags &= ~(IWI_FLAG_FW_INITED);
2060
2061         /* Allocate DMA memory for storing firmware image */
2062         error = bus_dma_tag_create(sc->iwi_parent_tag, 1, 0, BUS_SPACE_MAXADDR_32BIT,
2063             BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size, BUS_DMA_ALLOCNOW, &dmat);
2064         if (error != 0) {
2065                 device_printf(sc->sc_dev,
2066                     "could not create firmware DMA tag\n");
2067                 goto fail1;
2068         }
2069
2070         /*
2071          * We cannot map fw directly because of some hardware constraints on
2072          * the mapping address.
2073          */
2074         error = bus_dmamem_alloc(dmat, &virtaddr, BUS_DMA_WAITOK, &map);
2075         if (error != 0) {
2076                 device_printf(sc->sc_dev,
2077                     "could not allocate firmware DMA memory\n");
2078                 goto fail2;
2079         }
2080
2081         error = bus_dmamap_load(dmat, map, virtaddr, size, iwi_dma_map_addr,
2082             &physaddr, 0);
2083         if (error != 0) {
2084                 device_printf(sc->sc_dev, "could not load firmware DMA map\n");
2085                 goto fail3;
2086         }
2087
2088         /* Copy firmware image to DMA memory */
2089         bcopy(fw, virtaddr, size);
2090
2091         /* Make sure the adapter will get up-to-date values */
2092         bus_dmamap_sync(dmat, map, BUS_DMASYNC_PREWRITE);
2093
2094         /* Tell the adapter where the command blocks are stored */
2095         MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2096
2097         /*
2098          * Store command blocks into adapter's internal memory using register
2099          * indirections. The adapter will read the firmware image through DMA
2100          * using information stored in command blocks.
2101          */
2102         src = physaddr;
2103         p = virtaddr;
2104         end = p + size;
2105         CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2106
2107         while (p < end) {
2108                 dst = GETLE32(p); p += 4; src += 4;
2109                 len = GETLE32(p); p += 4; src += 4;
2110                 p += len;
2111
2112                 while (len > 0) {
2113                         mlen = min(len, IWI_CB_MAXDATALEN);
2114
2115                         ctl = IWI_CB_DEFAULT_CTL | mlen;
2116                         sum = ctl ^ src ^ dst;
2117
2118                         /* Write a command block */
2119                         CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2120                         CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2121                         CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2122                         CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2123
2124                         src += mlen;
2125                         dst += mlen;
2126                         len -= mlen;
2127                 }
2128         }
2129
2130         /* Write a fictive final command block (sentinel) */
2131         sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2132         CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2133
2134         CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
2135             ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER));
2136
2137         /* Tell the adapter to start processing command blocks */
2138         MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2139
2140         /* Wait until the adapter has processed all command blocks */
2141         for (ntries = 0; ntries < 400; ntries++) {
2142                 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2143                         break;
2144                 DELAY(100);
2145         }
2146         if (ntries == 400) {
2147                 device_printf(sc->sc_dev,
2148                     "timeout processing command blocks\n");
2149                 error = EIO;
2150                 goto fail4;
2151         }
2152
2153
2154         /* We're done with command blocks processing */
2155         MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2156
2157         /* Allow interrupts so we know when the firmware is inited */
2158         CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2159
2160         /* Tell the adapter to initialize the firmware */
2161         CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2162         CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
2163             IWI_CTL_ALLOW_STANDBY);
2164
2165         /* Wait at most one second for firmware initialization to complete */
2166         if ((error = tsleep(IWI_FW_INITIALIZED(sc), 0, "iwiini", hz)) != 0) {
2167                 device_printf(sc->sc_dev, "timeout waiting for firmware "
2168                     "initialization to complete\n");
2169                 goto fail4;
2170         }
2171
2172 fail4:  bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTWRITE);
2173         bus_dmamap_unload(dmat, map);
2174 fail3:  bus_dmamem_free(dmat, virtaddr, map);
2175 fail2:  bus_dma_tag_destroy(dmat);
2176 fail1:
2177         return error;
2178 }
2179
2180 /*
2181  * Store firmware into kernel memory so we can download it when we need to,
2182  * e.g when the adapter wakes up from suspend mode.
2183  */
2184 static int
2185 iwi_cache_firmware(struct iwi_softc *sc, void *data, int is_ibss)
2186 {
2187         struct iwi_firmware *kfw = &sc->fw;
2188         struct iwi_firmware ufw;
2189         int error;
2190
2191         iwi_free_firmware(sc);
2192
2193         /*
2194          * mutex(9): no mutexes should be held across functions which access
2195          * memory in userspace, such as copyin(9) [...]
2196          */
2197
2198         if ((error = copyin(data, &ufw, sizeof ufw)) != 0)
2199                 goto fail1;
2200
2201         kfw->boot_size  = ufw.boot_size;
2202         kfw->ucode_size = ufw.ucode_size;
2203         kfw->main_size  = ufw.main_size;
2204
2205         kfw->boot = malloc(kfw->boot_size, M_DEVBUF, M_WAITOK);
2206         if (kfw->boot == NULL) {
2207                 error = ENOMEM;
2208                 goto fail1;
2209         }
2210
2211         kfw->ucode = malloc(kfw->ucode_size, M_DEVBUF, M_WAITOK);
2212         if (kfw->ucode == NULL) {
2213                 error = ENOMEM;
2214                 goto fail2;
2215         }
2216
2217         kfw->main = malloc(kfw->main_size, M_DEVBUF, M_WAITOK);
2218         if (kfw->main == NULL) {
2219                 error = ENOMEM;
2220                 goto fail3;
2221         }
2222
2223         if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0)
2224                 goto fail4;
2225
2226         if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0)
2227                 goto fail4;
2228
2229         if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0)
2230                 goto fail4;
2231
2232         DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
2233             kfw->boot_size, kfw->ucode_size, kfw->main_size));
2234
2235
2236         sc->flags |= IWI_FLAG_FW_CACHED;
2237         sc->flags |= is_ibss ? IWI_FLAG_FW_IBSS : 0;
2238         return 0;
2239
2240 fail4:  free(kfw->boot, M_DEVBUF);
2241 fail3:  free(kfw->ucode, M_DEVBUF);
2242 fail2:  free(kfw->main, M_DEVBUF);
2243 fail1:
2244
2245         return error;
2246 }
2247
2248 static void
2249 iwi_free_firmware(struct iwi_softc *sc)
2250 {
2251         if (!(sc->flags & IWI_FLAG_FW_CACHED))
2252                 return;
2253
2254         free(sc->fw.boot, M_DEVBUF);
2255         free(sc->fw.ucode, M_DEVBUF);
2256         free(sc->fw.main, M_DEVBUF);
2257
2258         sc->flags &= ~( IWI_FLAG_FW_CACHED | IWI_FLAG_FW_IBSS );
2259 }
2260
2261 static int
2262 iwi_adapter_config(struct iwi_softc *sc, int is_a, int cmd_wait)
2263 {
2264         struct iwi_configuration config;
2265
2266         bzero(&config, sizeof config);
2267         config.enable_multicast = 1;
2268         config.noise_reported = 1;
2269
2270         config.bg_autodetect = 
2271                 ( !(is_a) &&
2272                   ( sc->enable_bg_autodetect != 0 ) ) ? 1 : 0;  /* default: on  */
2273
2274         config.bluetooth_coexistence =
2275                 ( sc->enable_bt_coexist != 0 ) ? 1 : 0;         /* default: on  */
2276
2277         config.enable_cts_to_self =
2278                 ( sc->enable_cts_to_self > 0 ) ? 1 : 0;         /* default: off */
2279
2280         if (sc->antenna_diversity > 0 ) {                       /* default: BOTH */
2281                 switch( sc->antenna_diversity ) {
2282                         case 1: case 3:
2283                                 config.antenna_diversity = sc->antenna_diversity;
2284                 }
2285         }
2286
2287         config.disable_unicast_decryption =
2288                 ( sc->disable_unicast_decryption != 0 ) ? 1 : 0; /* default: on */
2289
2290         config.disable_multicast_decryption =
2291                 ( sc->disable_multicast_decryption != 0 ) ? 1 : 0;/* default: on */
2292
2293
2294         if ( sc->debug_level > 0 ) {
2295                 printf("config.bluetooth_coexistence = %d\n",
2296                          config.bluetooth_coexistence );
2297                 printf("config.bg_autodetect = %d\n",
2298                          config.bg_autodetect );
2299                 printf("config.enable_cts_to_self = %d\n",
2300                          config.enable_cts_to_self );
2301                 printf("config.antenna_diversity = %d\n",
2302                          config.antenna_diversity );
2303                 printf("config.disable_unicast_decryption = %d\n",
2304                          config.disable_unicast_decryption );
2305                 printf("config.disable_multicast_decryption = %d\n",
2306                          config.disable_multicast_decryption );
2307                 printf("config.neg_best_rates_first = %d\n",
2308                          sc->enable_neg_best_first );
2309         }
2310
2311         return iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config,
2312                  sizeof config, cmd_wait );
2313 }
2314
2315 static int
2316 iwi_config(struct iwi_softc *sc)
2317 {
2318         struct ieee80211com *ic = &sc->sc_ic;
2319         struct ifnet *ifp = &ic->ic_if;
2320         struct iwi_rateset rs;
2321         struct iwi_txpower power;
2322         struct ieee80211_wepkey *k;
2323         struct iwi_wep_key wepkey;
2324         u_int32_t data;
2325         int error, i;
2326
2327         IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2328         DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":"));
2329         error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2330             IEEE80211_ADDR_LEN, 0);
2331         if (error != 0)
2332                 return error;
2333
2334         DPRINTF(("Configuring adapter\n"));
2335         if ((error = iwi_adapter_config(sc, 1, 0)) != 0)
2336                 return error;
2337
2338         data = htole32(IWI_POWER_MODE_CAM);
2339         DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2340         error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
2341         if (error != 0)
2342                 return error;
2343
2344         data = htole32(ic->ic_rtsthreshold);
2345         DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2346         error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
2347         if (error != 0)
2348                 return error;
2349
2350         if (ic->ic_opmode == IEEE80211_M_IBSS) {
2351                 power.mode = IWI_MODE_11B;
2352                 power.nchan = 11;
2353                 for (i = 0; i < 11; i++) {
2354                         power.chan[i].chan = i + 1;
2355                         power.chan[i].power = IWI_TXPOWER_MAX;
2356                 }
2357                 DPRINTF(("Setting .11b channels tx power\n"));
2358                 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2359                     0);
2360                 if (error != 0)
2361                         return error;
2362
2363                 power.mode = IWI_MODE_11G;
2364                 DPRINTF(("Setting .11g channels tx power\n"));
2365                 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2366                     0);
2367                 if (error != 0)
2368                         return error;
2369         }
2370
2371         rs.mode = IWI_MODE_11G;
2372         rs.type = IWI_RATESET_TYPE_SUPPORTED;
2373         rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2374         bcopy(ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates, rs.rates,
2375             rs.nrates);
2376         DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2377         error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2378         if (error != 0)
2379                 return error;
2380
2381         rs.mode = IWI_MODE_11A;
2382         rs.type = IWI_RATESET_TYPE_SUPPORTED;
2383         rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2384         bcopy(ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates, rs.rates,
2385             rs.nrates);
2386         DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2387         error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2388         if (error != 0)
2389                 return error;
2390
2391         data = htole32(arc4random());
2392         DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2393         error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
2394         if (error != 0)
2395                 return error;
2396
2397         if (ic->ic_flags & IEEE80211_F_WEPON) {
2398                 k = ic->ic_nw_keys;
2399                 for (i = 0; i < IEEE80211_WEP_NKID; i++, k++) {
2400                         wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2401                         wepkey.idx = i;
2402                         wepkey.len = k->wk_len;
2403                         bzero(wepkey.key, sizeof wepkey.key);
2404                         bcopy(k->wk_key, wepkey.key, k->wk_len);
2405                         DPRINTF(("Setting wep key index %u len %u\n",
2406                             wepkey.idx, wepkey.len));
2407                         error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2408                             sizeof wepkey, 0);
2409                         if (error != 0)
2410                                 return error;
2411                 }
2412         }
2413
2414         /* Enable adapter */
2415         DPRINTF(("Enabling adapter\n"));
2416         return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
2417 }
2418
2419 static int
2420 iwi_scan(struct iwi_softc *sc)
2421 {
2422         struct ieee80211com *ic = &sc->sc_ic;
2423         struct iwi_scan scan;
2424         u_int8_t *p;
2425         int i, count;
2426         int do_5ghz_scan = 0;
2427
2428         sc->scan_counter++; /* track the number of scans started */
2429
2430         sc->flags |= IWI_FLAG_SCANNING;
2431
2432         bzero(&scan, sizeof scan);
2433
2434         /*
2435          * Alternate two broadcast scans with
2436          * two broadcast/direct scans.
2437          */
2438         if ( sc->scan_counter & 2 ) {
2439                 scan.type = IWI_SCAN_TYPE_BROADCAST_AND_DIRECT;
2440                 scan.intval = htole16(100);
2441         } else {
2442                 scan.type = IWI_SCAN_TYPE_BROADCAST;
2443                 scan.intval = htole16(40);
2444         }
2445
2446         p = scan.channels;
2447
2448         /*
2449          * If we have .11a capable adapter, and
2450          *      - we are in .11a mode, or
2451          *      - we are in auto mode and this is an odd numbered scan
2452          * then do a 5GHz scan, otherwise do a 2GHz scan.
2453          */
2454         if ( ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates > 0 ) {
2455                 if (( ic->ic_curmode == IEEE80211_MODE_11A ) ||
2456                     (( ic->ic_curmode == IEEE80211_MODE_AUTO ) &&
2457                      ( sc->scan_counter & 1)))
2458                         do_5ghz_scan = 1;
2459         }
2460         count = 0;
2461         if ( do_5ghz_scan ) {
2462                 DPRINTF(("Scanning 5GHz band\n"));
2463                 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2464                         if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
2465                                         isset(ic->ic_chan_active, i)) {
2466                                 *++p = i;
2467                                 count++;
2468                         }
2469                 }
2470                 *(p - count) = IWI_CHAN_5GHZ | count;
2471         } else {
2472                 DPRINTF(("Scanning 2GHz band\n"));
2473                 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2474                         if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
2475                                         isset(ic->ic_chan_active, i)) {
2476                                 *++p = i;
2477                                 count++;
2478                         }
2479                 }
2480                 *(p - count) = IWI_CHAN_2GHZ | count;
2481         }
2482         return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2483 }
2484
2485 static int
2486 iwi_auth_and_assoc(struct iwi_softc *sc)
2487 {
2488         struct ieee80211com *ic = &sc->sc_ic;
2489         struct ifnet *ifp = &ic->ic_if;
2490         struct ieee80211_node *ni = ic->ic_bss;
2491         struct iwi_rateset rs;
2492         u_int32_t data;
2493         int error, x;
2494
2495         if ( ( sc->flags & IWI_FLAG_FW_IBSS ) &&
2496              !( ni->ni_capinfo & IEEE80211_CAPINFO_IBSS ) ) {
2497                 return -1; /* IBSS F/W requires network ibss capability */
2498         }
2499
2500         DPRINTF(("Configuring adapter\n"));
2501         if ((error = iwi_adapter_config(sc, 
2502                 IEEE80211_IS_CHAN_5GHZ(ni->ni_chan), 1)) != 0)
2503                         return error;
2504
2505 #ifdef IWI_DEBUG
2506         if (sc->debug_level > 0) {
2507                 printf("Setting ESSID to ");
2508                 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2509                 printf("\n");
2510         }
2511 #endif
2512         error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2513         if (error != 0)
2514                 return error;
2515
2516         /* the rate set has already been "negotiated" */
2517         rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2518             IWI_MODE_11G;
2519         rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2520         rs.nrates = ni->ni_rates.rs_nrates;
2521         if ( sc->enable_neg_best_first != 1 ) {
2522                 bcopy(ni->ni_rates.rs_rates, rs.rates, rs.nrates);
2523         } else {
2524                 for ( x = 0 ; x < rs.nrates; x++ ) {
2525                         /*
2526                          * Present the firmware with the most favourable
2527                          * of the negotiated rates first. 
2528                          */
2529                         rs.rates[rs.nrates-x-1] = ni->ni_rates.rs_rates[x];
2530                 }
2531         }
2532
2533         if ( sc->debug_level > 0 ) {
2534                 printf("Setting negotiated rates (%u) : ", rs.nrates);
2535                 for ( x = 0 ; x < rs.nrates; x++ ) {
2536                         printf("%d ", rs.rates[x]);
2537                 }
2538                 printf("\n");
2539         }
2540
2541         error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2542         if (error != 0)
2543                 return error;
2544
2545         data = htole32(ni->ni_rssi);
2546         DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2547         error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2548         if (error != 0)
2549                 return error;
2550
2551         bzero(&sc->assoc, sizeof sc->assoc);
2552         sc->assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2553             IWI_MODE_11G;
2554         sc->assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2555         if (sc->authmode == IEEE80211_AUTH_SHARED)
2556                 sc->assoc.auth = (ic->ic_wep_txkey << 4) | IWI_AUTH_SHARED;
2557         bcopy(ni->ni_tstamp, sc->assoc.tstamp, 8);
2558         sc->assoc.capinfo = htole16(ni->ni_capinfo);
2559         sc->assoc.lintval = htole16(ic->ic_lintval);
2560         sc->assoc.intval = htole16(ni->ni_intval);
2561         IEEE80211_ADDR_COPY(sc->assoc.bssid, ni->ni_bssid);
2562         if ( ic->ic_opmode == IEEE80211_M_IBSS )
2563                 IEEE80211_ADDR_COPY(sc->assoc.dst, ifp->if_broadcastaddr);
2564         else
2565                 IEEE80211_ADDR_COPY(sc->assoc.dst, ni->ni_bssid);
2566
2567         DPRINTF(("Trying to associate to %6D channel %u auth %u\n",
2568             sc->assoc.bssid, ":", sc->assoc.chan, sc->assoc.auth));
2569         return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &sc->assoc, sizeof sc->assoc, 1);
2570 }
2571
2572 static void
2573 iwi_init(void *priv)
2574 {
2575         struct iwi_softc *sc = priv;
2576         struct ieee80211com *ic = &sc->sc_ic;
2577         struct ifnet *ifp = &ic->ic_if;
2578         struct iwi_firmware *fw = &sc->fw;
2579         int i;
2580
2581         /* exit immediately if firmware has not been ioctl'd */
2582         if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
2583                 ifp->if_flags &= ~IFF_UP;
2584                 return;
2585         }
2586
2587         iwi_stop(sc);
2588
2589         if (iwi_reset(sc) != 0) {
2590                 device_printf(sc->sc_dev, "could not reset adapter\n");
2591                 goto fail;
2592         }
2593
2594         if (iwi_load_firmware(sc, fw->boot, fw->boot_size) != 0) {
2595                 device_printf(sc->sc_dev, "could not load boot firmware\n");
2596                 goto fail;
2597         }
2598
2599         if (iwi_load_ucode(sc, fw->ucode, fw->ucode_size) != 0) {
2600                 device_printf(sc->sc_dev, "could not load microcode\n");
2601                 goto fail;
2602         }
2603
2604         iwi_stop_master(sc);
2605
2606         sc->tx_cur = 0;
2607         sc->tx_queued = 0;
2608         sc->tx_old = IWI_TX_RING_SIZE - 1;
2609         sc->cmd_cur = 0;
2610         sc->rx_cur = IWI_RX_RING_SIZE - 1;
2611
2612         CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmd_ring_pa);
2613         CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, IWI_CMD_RING_SIZE);
2614         CSR_WRITE_4(sc, IWI_CSR_CMD_READ_INDEX, 0);
2615         CSR_WRITE_4(sc, IWI_CSR_CMD_WRITE_INDEX, sc->cmd_cur);
2616
2617         CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->tx_ring_pa);
2618         CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, IWI_TX_RING_SIZE);
2619         CSR_WRITE_4(sc, IWI_CSR_TX1_READ_INDEX, 0);
2620         CSR_WRITE_4(sc, IWI_CSR_TX1_WRITE_INDEX, sc->tx_cur);
2621
2622         CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->tx_ring_pa);
2623         CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, IWI_TX_RING_SIZE);
2624         CSR_WRITE_4(sc, IWI_CSR_TX2_READ_INDEX, 0);
2625         CSR_WRITE_4(sc, IWI_CSR_TX2_WRITE_INDEX, 0);
2626
2627         CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->tx_ring_pa);
2628         CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, IWI_TX_RING_SIZE);
2629         CSR_WRITE_4(sc, IWI_CSR_TX3_READ_INDEX, 0);
2630         CSR_WRITE_4(sc, IWI_CSR_TX3_WRITE_INDEX, 0);
2631
2632         CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->tx_ring_pa);
2633         CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, IWI_TX_RING_SIZE);
2634         CSR_WRITE_4(sc, IWI_CSR_TX4_READ_INDEX, 0);
2635         CSR_WRITE_4(sc, IWI_CSR_TX4_WRITE_INDEX, 0);
2636
2637         for (i = 0; i < IWI_RX_RING_SIZE; i++)
2638                 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4,
2639                     sc->rx_buf[i].physaddr);
2640
2641         /*
2642          * Kick Rx
2643          */
2644         CSR_WRITE_4(sc, IWI_CSR_RX_WRITE_INDEX, sc->rx_cur);
2645         CSR_WRITE_4(sc, IWI_CSR_RX_READ_INDEX, 0);
2646
2647         if (iwi_load_firmware(sc, fw->main, fw->main_size) != 0) {
2648                 device_printf(sc->sc_dev, "could not load main firmware\n");
2649                 goto fail;
2650         }
2651
2652         /*
2653          * Force the opmode based on what firmware is loaded. This
2654          * stops folks from killing the firmware by asking it to
2655          * do something it doesn't support.
2656          */
2657         if ( ic->ic_opmode != IEEE80211_M_MONITOR ) {
2658                 ic->ic_opmode = ( sc->flags & IWI_FLAG_FW_IBSS )
2659                         ? IEEE80211_M_IBSS : IEEE80211_M_STA;
2660         }
2661
2662         sc->flags |= IWI_FLAG_FW_INITED;
2663
2664         sc->flags &= ~( IWI_FLAG_SCANNING |
2665                         IWI_FLAG_SCAN_COMPLETE |
2666                         IWI_FLAG_SCAN_ABORT |
2667                         IWI_FLAG_ASSOCIATED );
2668
2669         if (iwi_config(sc) != 0) {
2670                 device_printf(sc->sc_dev, "device configuration failed\n");
2671                 goto fail;
2672         }
2673
2674         if ( ic->ic_opmode != IEEE80211_M_MONITOR ) {
2675                 ieee80211_begin_scan(ifp);
2676                 ifp->if_flags &= ~IFF_OACTIVE;
2677                 ifp->if_flags |= IFF_RUNNING;
2678         } else {
2679                 ieee80211_begin_scan(ifp);
2680                 ifp->if_flags &= ~IFF_OACTIVE;
2681                 ifp->if_flags |= IFF_RUNNING;
2682         }
2683
2684         return;
2685
2686 fail:   
2687         if ( !(sc->flags & IWI_FLAG_RESET) )
2688                 ifp->if_flags &= ~IFF_UP;
2689         iwi_stop(sc);
2690 }
2691
2692 static void
2693 iwi_init_locked(void *priv) 
2694 {
2695         struct iwi_softc *sc = priv;
2696         IWI_LOCK_INFO;
2697         IWI_LOCK(sc);
2698         iwi_init(sc);
2699         IWI_UNLOCK(sc);
2700 }
2701
2702 static void
2703 iwi_stop(void *priv)
2704 {
2705         struct iwi_softc *sc = priv;
2706         struct ieee80211com *ic = &sc->sc_ic;
2707         struct ifnet *ifp = &ic->ic_if;
2708         struct iwi_tx_buf *buf;
2709         int i;
2710
2711         iwi_stop_master(sc);
2712         CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
2713
2714         /*
2715          * Release Tx buffers
2716          */
2717         for (i = 0; i < IWI_TX_RING_SIZE; i++) {
2718                 buf = &sc->tx_buf[i];
2719
2720                 if (buf->m != NULL) {
2721                         bus_dmamap_sync(sc->tx_buf_dmat, buf->map,
2722                             BUS_DMASYNC_POSTWRITE);
2723                         bus_dmamap_unload(sc->tx_buf_dmat, buf->map);
2724                         m_freem(buf->m);
2725                         buf->m = NULL;
2726
2727                         if (buf->ni != NULL) {
2728                                 if (buf->ni != ic->ic_bss)
2729                                         ieee80211_free_node(ic, buf->ni);
2730                                 buf->ni = NULL;
2731                         }
2732                 }
2733         }
2734
2735         ifp->if_timer = 0;
2736         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2737
2738         ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2739 }
2740
2741 static int8_t
2742 iwi_cache_station(struct iwi_softc *sc, u_int8_t *mac)
2743 {
2744         int i, x, base, elemsize = sizeof(struct iwi_fw_station);
2745         for (i = 0; i < sc->num_stations; i++)
2746                 if (!memcmp(sc->stations[i], mac, IEEE80211_ADDR_LEN))
2747                         break;
2748         if (i == IWI_FW_MAX_STATIONS)
2749                 return 0xff;
2750         memcpy(sc->stations[i], mac, IEEE80211_ADDR_LEN);
2751         for (x = 0, base = IWI_STATION_TABLE + (i * elemsize) ; 
2752              x < IEEE80211_ADDR_LEN ; x++ ) {
2753                 CSR_WRITE_1(sc, base + x, mac[x]);
2754         }
2755         if ( (i + 1) > sc->num_stations )
2756                 sc->num_stations++;
2757         return i;
2758 }
2759
2760 static u_int8_t
2761 iwi_find_station(struct iwi_softc *sc, u_int8_t *mac)
2762 {
2763         u_int8_t i;
2764         for (i = 0; i < sc->num_stations; i++)
2765                 if (!memcmp(sc->stations[i], mac, IEEE80211_ADDR_LEN))
2766                         return i;
2767         return 0xff;
2768 }
2769
2770 static const char *
2771 iwi_error_desc(u_int32_t val)
2772 {
2773         switch (val) {
2774         case IWI_FW_ERROR_OK:
2775                 return "OK";
2776         case IWI_FW_ERROR_FAIL:
2777                 return "FAIL";
2778         case IWI_FW_ERROR_MEMORY_UNDERFLOW:
2779                 return "MEMORY_UNDERFLOW";
2780         case IWI_FW_ERROR_MEMORY_OVERFLOW:
2781                 return "MEMORY_OVERFLOW";
2782         case IWI_FW_ERROR_BAD_PARAM:
2783                 return "BAD_PARAMETER";
2784         case IWI_FW_ERROR_BAD_CHECKSUM:
2785                 return "BAD_CHECKSUM";
2786         case IWI_FW_ERROR_NMI_INTERRUPT:
2787                 return "NMI_INTERRUPT";
2788         case IWI_FW_ERROR_BAD_DATABASE:
2789                 return "BAD_DATABASE";
2790         case IWI_FW_ERROR_ALLOC_FAIL:
2791                 return "ALLOC_FAIL";
2792         case IWI_FW_ERROR_DMA_UNDERRUN:
2793                 return "DMA_UNDERRUN";
2794         case IWI_FW_ERROR_DMA_STATUS:
2795                 return "DMA_STATUS";
2796         case IWI_FW_ERROR_DINOSTATUS_ERROR:
2797                 return "DINOSTATUS_ERROR";
2798         case IWI_FW_ERROR_EEPROMSTATUS_ERROR:
2799                 return "EEPROMSTATUS_ERROR";
2800         case IWI_FW_ERROR_SYSASSERT:
2801                 return "SYSASSERT";
2802         case IWI_FW_ERROR_FATAL_ERROR:
2803                 return "FATAL";
2804         default:
2805                 return "UNKNOWN_ERROR";
2806         }
2807 }
2808
2809 static void
2810 iwi_dump_fw_event_log(struct iwi_softc *sc)
2811 {
2812         u_int32_t ev, time, data, i, count, base;
2813         base = CSR_READ_4(sc, IWI_FW_EVENT_LOG);
2814         count = MEM_READ_4(sc, base);
2815         if ( count > 0 && (sc->flags & IWI_FLAG_FW_INITED) ) {
2816                 printf("Reading %d event log entries from base address 0x%x.\n",
2817                         count,  base);
2818                 if (IWI_FW_EVENT_START_OFFSET <= count * IWI_FW_EVENT_ELEM_SIZE)
2819                         device_printf(sc->sc_dev,"Start IWI Event Log Dump:\n");
2820                 for (i = IWI_FW_EVENT_START_OFFSET;
2821                                 i <= count * IWI_FW_EVENT_ELEM_SIZE;
2822                                 i += IWI_FW_EVENT_ELEM_SIZE) {
2823                         ev = MEM_READ_4(sc, base + i);
2824                         time  = MEM_READ_4(sc, base + i + 1 * sizeof(u_int32_t));
2825                         data  = MEM_READ_4(sc, base + i + 2 * sizeof(u_int32_t));
2826                         printf("%d %8p %8.8d\n", time, (void *) data, ev);
2827                 }
2828         } else {
2829                 printf("There are no entries in the firmware event log.\n");
2830         }
2831 }
2832
2833 static void
2834 iwi_dump_fw_error_log(struct iwi_softc *sc)
2835 {
2836         u_int32_t i = 0;
2837         int32_t count, base;
2838         base = CSR_READ_4(sc, IWI_FW_ERROR_LOG);
2839         count = MEM_READ_4(sc, base);
2840         if ( count > 0 && (sc->flags & IWI_FLAG_FW_INITED) ) {
2841                 printf("Reading %d error log entries "
2842                     "from base address 0x%p.\n", count,  (void *)base);
2843                 for ( i = IWI_FW_ERROR_START_OFFSET; 
2844                         i <= count * IWI_FW_EVENT_ELEM_SIZE;
2845                         i += IWI_FW_ERROR_ELEM_SIZE ) {
2846                         u_int32_t elems;
2847                         printf("%15.15s",
2848                             iwi_error_desc(MEM_READ_4(sc, base + i)));
2849                         printf(" time(%8.8d)", MEM_READ_4(sc, base + i + 4));
2850                         for ( elems = 2 ; elems < 7 ; elems++ ) {
2851                                 printf(" %8p", (void *)
2852                                     MEM_READ_4(sc, base + i + (4 * elems)));
2853                         }
2854                         printf("\n");
2855                 }
2856         } 
2857 }
2858
2859 static int
2860 iwi_sysctl_cts_to_self(SYSCTL_HANDLER_ARGS)
2861 {
2862         struct iwi_softc *sc = (void *)arg1;
2863         int cts_to_self = sc->enable_cts_to_self;
2864         int error = sysctl_handle_int(oidp, &cts_to_self, 0, req);
2865
2866         (void)arg2; /* silence WARNS == 6 */
2867
2868         if ( !error && req->newptr && cts_to_self != sc->enable_cts_to_self ) {
2869                 switch ( cts_to_self ) {
2870                         case -1: case 0: case 1:
2871                                 sc->enable_cts_to_self = cts_to_self;
2872                                 error = iwi_adapter_config(sc, 0, 0);
2873                         break;
2874                 }
2875         }
2876         return error;
2877 }
2878
2879
2880 static int
2881 iwi_sysctl_antenna_diversity(SYSCTL_HANDLER_ARGS)
2882 {
2883         struct iwi_softc *sc = (void *)arg1;
2884         int antenna_diversity = sc->antenna_diversity;
2885         int error = sysctl_handle_int(oidp, &antenna_diversity, 0, req);
2886
2887         (void)arg2; /* silence WARNS == 6 */
2888
2889         if ( !error && req->newptr && antenna_diversity != sc->antenna_diversity ) {
2890                 switch ( antenna_diversity ) {
2891                         case 1: case 3: case 0: case -1:
2892                                 sc->antenna_diversity = antenna_diversity;
2893                                 error = iwi_adapter_config(sc, 0, 0);
2894                         break;
2895                 }
2896         }
2897         return error;
2898 }
2899
2900 static int
2901 iwi_sysctl_bg_autodetect(SYSCTL_HANDLER_ARGS)
2902 {
2903         struct iwi_softc *sc = (void *)arg1;
2904         int bg_autodetect = sc->enable_bg_autodetect;
2905         int error = sysctl_handle_int(oidp, &bg_autodetect, 0, req);
2906
2907         (void)arg2; /* silence WARNS == 6 */
2908
2909         if ( !error && req->newptr && bg_autodetect != sc->enable_bg_autodetect ) {
2910                 switch ( bg_autodetect ) {
2911                         case 1: case 0: case -1:
2912                                 sc->enable_bg_autodetect = bg_autodetect;
2913                                 error = iwi_adapter_config(sc, 0, 0);
2914                         break;
2915                 }
2916         }
2917         return error;
2918 }
2919
2920 static int
2921 iwi_sysctl_bt_coexist(SYSCTL_HANDLER_ARGS)
2922 {
2923         struct iwi_softc *sc = (void *)arg1;
2924         int bt_coexist = sc->enable_bt_coexist;
2925         int error = sysctl_handle_int(oidp, &bt_coexist, 0, req);
2926
2927         (void)arg2; /* silence WARNS == 6 */
2928
2929         if ( !error && req->newptr && bt_coexist != sc->enable_bt_coexist ) {
2930                 switch ( bt_coexist ) {
2931                         case 1: case 0: case -1:
2932                                 sc->enable_bt_coexist = bt_coexist;
2933                                 error = iwi_adapter_config(sc, 0, 0);
2934                         break;
2935                 }
2936         }
2937         return error;
2938 }
2939
2940 static int
2941 iwi_sysctl_dump_logs(SYSCTL_HANDLER_ARGS)
2942 {
2943         struct iwi_softc *sc = arg1;
2944         int result = -1;
2945         int error = sysctl_handle_int(oidp, &result, 0, req);
2946
2947         (void)arg2; /* silence WARNS == 6 */
2948
2949         if (!error && req->newptr && result == 1) {
2950                 iwi_dump_fw_event_log(sc);
2951                 iwi_dump_fw_error_log(sc);
2952         }
2953         return error;
2954 }
2955
2956 static int
2957 iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
2958 {
2959         struct iwi_softc *sc = arg1;
2960         u_int32_t size;
2961         struct iwi_dump_buffer dump;
2962
2963         (void)arg2; /* silence WARNS == 6 */
2964         (void)oidp; /* silence WARNS == 6 */
2965
2966         if (!(sc->flags & IWI_FLAG_FW_INITED)) {
2967                 bzero(dump.buf, sizeof dump.buf);
2968                 return SYSCTL_OUT(req, &dump, sizeof dump);
2969         }
2970
2971         size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
2972         CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &dump.buf[1], size);
2973
2974         return SYSCTL_OUT(req, &dump, sizeof dump);
2975 }
2976
2977 static int
2978 iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
2979 {
2980         struct iwi_softc *sc = arg1;
2981         int val;
2982
2983         (void)arg2; /* silence WARNS == 6 */
2984         (void)oidp; /* silence WARNS == 6 */
2985
2986         val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0;
2987         return SYSCTL_OUT(req, &val, sizeof val);
2988 }
2989
2990 static int
2991 iwi_sysctl_neg_best_rates_first(SYSCTL_HANDLER_ARGS)
2992 {
2993         struct iwi_softc *sc = arg1;
2994         int best_first = sc->enable_neg_best_first;
2995         int error = sysctl_handle_int(oidp, &best_first, 0, req);
2996
2997         (void)arg2; /* silence WARNS == 6 */
2998         (void)oidp; /* silence WARNS == 6 */
2999
3000         if ( !error && req->newptr && best_first != sc->enable_neg_best_first ) {
3001                 switch ( best_first ) {
3002                         case 1: case 0: case -1:
3003                                 sc->enable_neg_best_first = best_first;
3004                         break;
3005                 }
3006         }
3007         return error;
3008 }
3009
3010 static int
3011 iwi_sysctl_disable_unicast_decryption(SYSCTL_HANDLER_ARGS)
3012 {
3013         struct iwi_softc *sc = arg1;
3014         int disable_uni = sc->disable_unicast_decryption;
3015         int error = sysctl_handle_int(oidp, &disable_uni, 0, req);
3016
3017         (void)arg2; /* silence WARNS == 6 */
3018         (void)oidp; /* silence WARNS == 6 */
3019
3020         if (!error && req->newptr && disable_uni != sc->disable_unicast_decryption) {
3021                 switch ( disable_uni ) {
3022                         case 1: case 0: case -1:
3023                                 sc->disable_unicast_decryption = disable_uni;
3024                         break;
3025                 }
3026         }
3027         return error;
3028 }
3029
3030 static int
3031 iwi_sysctl_disable_multicast_decryption(SYSCTL_HANDLER_ARGS)
3032 {
3033         struct iwi_softc *sc = arg1;
3034         int disable_mul = sc->disable_multicast_decryption;
3035         int error = sysctl_handle_int(oidp, &disable_mul, 0, req);
3036
3037         (void)arg2; /* silence WARNS == 6 */
3038         (void)oidp; /* silence WARNS == 6 */
3039
3040         if (!error && req->newptr && disable_mul!=sc->disable_multicast_decryption){
3041                 switch ( disable_mul ) {
3042                         case 1: case 0: case -1:
3043                                 sc->disable_multicast_decryption = disable_mul;
3044                         break;
3045                 }
3046         }
3047         return error;
3048 }
3049
3050