2 * Copyright (c) 2004, 2005
3 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: if_ipw.c,v 1.7.2.1 2005/01/13 20:01:03 damien Exp $
29 * $FreeBSD: src/sys/dev/ipw/if_ipw.c,v 1.7.2.4 2006/01/29 15:13:01 damien Exp $
30 * $DragonFly: src/sys/dev/netif/ipw/Attic/if_ipw.c,v 1.14 2006/05/18 13:51:45 sephe Exp $
34 * Intel(R) PRO/Wireless 2100 MiniPCI driver
35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
38 #include <sys/param.h>
39 #include <sys/sysctl.h>
40 #include <sys/sockio.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
48 #include <sys/endian.h>
50 #include <sys/ucred.h>
51 #include <sys/serialize.h>
52 #include <sys/thread2.h>
54 #include <machine/bus.h>
55 #include <machine/resource.h>
56 #include <machine/clock.h>
59 #include <bus/pci/pcireg.h>
60 #include <bus/pci/pcivar.h>
61 #include <bus/pci/pcidevs.h>
65 #include <net/if_arp.h>
66 #include <net/ethernet.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69 #include <net/if_types.h>
70 #include <net/ifq_var.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #include <netinet/if_ether.h>
78 #include <netproto/802_11/ieee80211_var.h>
79 #include <netproto/802_11/ieee80211_ioctl.h>
80 #include <netproto/802_11/ieee80211_radiotap.h>
81 #include <netproto/802_11/if_wavelan_ieee.h>
83 #include <dev/netif/ipw/if_ipwreg.h>
84 #include <dev/netif/ipw/if_ipwvar.h>
87 #define DPRINTF(x) if (ipw_debug > 0) printf x
88 #define DPRINTFN(n, x) if (ipw_debug >= (n)) printf x
90 SYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
93 #define DPRINTFN(n, x)
96 MODULE_DEPEND(ipw, pci, 1, 1, 1);
97 MODULE_DEPEND(ipw, wlan, 1, 1, 1);
105 static const struct ipw_ident ipw_ident_table[] = {
106 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2100,
107 "Intel(R) PRO/Wireless 2100 MiniPCI" },
112 static const struct ieee80211_rateset ipw_rateset_11b =
113 { 4, { 2, 4, 11, 22 } };
115 static int ipw_dma_alloc(device_t);
116 static void ipw_release(struct ipw_softc *);
117 static int ipw_media_change(struct ifnet *);
118 static void ipw_media_status(struct ifnet *, struct ifmediareq *);
119 static int ipw_newstate(struct ieee80211com *,
120 enum ieee80211_state, int);
121 static u_int16_t ipw_read_prom_word(struct ipw_softc *, u_int8_t);
122 static void ipw_command_intr(struct ipw_softc *,
123 struct ipw_soft_buf *);
124 static void ipw_newstate_intr(struct ipw_softc *,
125 struct ipw_soft_buf *);
126 static void ipw_data_intr(struct ipw_softc *, struct ipw_status *,
127 struct ipw_soft_bd *, struct ipw_soft_buf *);
128 static void ipw_notification_intr(struct ipw_softc *,
129 struct ipw_soft_buf *);
130 static void ipw_rx_intr(struct ipw_softc *);
131 static void ipw_release_sbd(struct ipw_softc *,
132 struct ipw_soft_bd *);
133 static void ipw_tx_intr(struct ipw_softc *);
134 static void ipw_intr(void *);
135 static void ipw_dma_map_txbuf(void *, bus_dma_segment_t *, int,
137 static void ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
138 static int ipw_cmd(struct ipw_softc *, u_int32_t, void *,
140 static int ipw_tx_start(struct ifnet *, struct mbuf *,
141 struct ieee80211_node *);
142 static void ipw_start(struct ifnet *);
143 static void ipw_watchdog(struct ifnet *);
144 static int ipw_ioctl(struct ifnet *, u_long, caddr_t,
146 static void ipw_stop_master(struct ipw_softc *);
147 static int ipw_reset(struct ipw_softc *);
148 static int ipw_load_ucode(struct ipw_softc *, u_char *, int);
149 static int ipw_load_firmware(struct ipw_softc *, u_char *, int);
150 static int ipw_cache_firmware(struct ipw_softc *, void *);
151 static void ipw_free_firmware(struct ipw_softc *);
152 static int ipw_config(struct ipw_softc *);
153 static void ipw_init(void *);
154 static void ipw_stop(void *);
155 static int ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
156 static int ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
157 static u_int32_t ipw_read_table1(struct ipw_softc *, u_int32_t);
158 static void ipw_write_table1(struct ipw_softc *, u_int32_t,
160 static int ipw_read_table2(struct ipw_softc *, u_int32_t, void *,
162 static void ipw_read_mem_1(struct ipw_softc *, bus_size_t,
163 u_int8_t *, bus_size_t);
164 static void ipw_write_mem_1(struct ipw_softc *, bus_size_t,
165 u_int8_t *, bus_size_t);
167 static __inline u_int8_t MEM_READ_1(struct ipw_softc *sc, u_int32_t addr)
169 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
170 return CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA);
173 static __inline u_int32_t MEM_READ_4(struct ipw_softc *sc, u_int32_t addr)
175 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
176 return CSR_READ_4(sc, IPW_CSR_INDIRECT_DATA);
179 static int ipw_probe(device_t);
180 static int ipw_attach(device_t);
181 static int ipw_detach(device_t);
182 static int ipw_shutdown(device_t);
183 static int ipw_suspend(device_t);
184 static int ipw_resume(device_t);
186 static device_method_t ipw_methods[] = {
187 /* Device interface */
188 DEVMETHOD(device_probe, ipw_probe),
189 DEVMETHOD(device_attach, ipw_attach),
190 DEVMETHOD(device_detach, ipw_detach),
191 DEVMETHOD(device_shutdown, ipw_shutdown),
192 DEVMETHOD(device_suspend, ipw_suspend),
193 DEVMETHOD(device_resume, ipw_resume),
198 static DEFINE_CLASS_0(ipw, ipw_driver, ipw_methods, sizeof(struct ipw_softc));
199 static devclass_t ipw_devclass;
201 DRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0);
204 ipw_probe(device_t dev)
206 const struct ipw_ident *ident;
208 for (ident = ipw_ident_table; ident->name != NULL; ident++) {
209 if (pci_get_vendor(dev) == ident->vendor &&
210 pci_get_device(dev) == ident->device) {
211 device_set_desc(dev, ident->name);
218 /* Base Address Register */
219 #define IPW_PCI_BAR0 0x10
222 ipw_attach(device_t dev)
224 struct ipw_softc *sc = device_get_softc(dev);
225 struct ieee80211com *ic = &sc->sc_ic;
226 struct ifnet *ifp = &ic->ic_if;
227 struct sysctl_oid *sysctl_tree;
231 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
232 device_printf(dev, "chip is in D%d power mode "
233 "-- setting to D0\n", pci_get_powerstate(dev));
234 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
237 pci_write_config(dev, 0x41, 0, 1);
239 /* enable bus-mastering */
240 pci_enable_busmaster(dev);
242 /* map the register window */
243 sc->mem_rid = IPW_PCI_BAR0;
244 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
246 if (sc->mem == NULL) {
247 device_printf(dev, "could not allocate memory resource\n");
251 sc->sc_st = rman_get_bustag(sc->mem);
252 sc->sc_sh = rman_get_bushandle(sc->mem);
255 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
256 RF_ACTIVE | RF_SHAREABLE);
257 if (sc->irq == NULL) {
258 device_printf(dev, "could not allocate interrupt resource\n");
262 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
264 if (ipw_reset(sc) != 0) {
265 device_printf(dev, "could not reset adapter\n");
269 sysctl_ctx_init(&sc->sysctl_ctx);
270 sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
271 SYSCTL_STATIC_CHILDREN(_hw),
273 device_get_nameunit(dev),
277 if (ipw_dma_alloc(dev) != 0) {
278 device_printf(dev, "could not allocate DMA resources\n");
282 ic->ic_phytype = IEEE80211_T_DS;
283 ic->ic_opmode = IEEE80211_M_STA;
284 ic->ic_state = IEEE80211_S_INIT;
286 /* set device capabilities */
287 ic->ic_caps = IEEE80211_C_SHPREAMBLE | IEEE80211_C_TXPMGT |
288 IEEE80211_C_PMGT | IEEE80211_C_IBSS | IEEE80211_C_MONITOR |
291 /* read MAC address from EEPROM */
292 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
293 ic->ic_myaddr[0] = val >> 8;
294 ic->ic_myaddr[1] = val & 0xff;
295 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
296 ic->ic_myaddr[2] = val >> 8;
297 ic->ic_myaddr[3] = val & 0xff;
298 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
299 ic->ic_myaddr[4] = val >> 8;
300 ic->ic_myaddr[5] = val & 0xff;
302 /* set supported .11b rates */
303 ic->ic_sup_rates[IEEE80211_MODE_11B] = ipw_rateset_11b;
305 /* set supported .11b channels (read from EEPROM) */
306 if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
307 val = 0x7ff; /* default to channels 1-11 */
309 for (i = 1; i < 16; i++) {
310 if (val & (1 << i)) {
311 ic->ic_channels[i].ic_freq =
312 ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
313 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
317 /* check support for radio transmitter switch in EEPROM */
318 if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
319 sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
321 /* IBSS channel undefined for now */
322 ic->ic_ibss_chan = &ic->ic_channels[0];
325 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
326 ifp->if_init = ipw_init;
327 ifp->if_ioctl = ipw_ioctl;
328 ifp->if_start = ipw_start;
329 ifp->if_watchdog = ipw_watchdog;
330 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
331 ifq_set_ready(&ifp->if_snd);
333 ieee80211_ifattach(ic);
334 /* override state transition machine */
335 sc->sc_newstate = ic->ic_newstate;
336 ic->ic_newstate = ipw_newstate;
337 ieee80211_media_init(ic, ipw_media_change, ipw_media_status);
339 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
340 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
342 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
343 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
344 sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
346 sc->sc_txtap_len = sizeof sc->sc_txtapu;
347 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
348 sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
352 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
353 SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, "radio",
354 CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
355 "Radio transmitter switch");
357 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
358 SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, "stats",
359 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
362 SYSCTL_ADD_INT(&sc->sysctl_ctx,
363 SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, "dwell",
364 CTLFLAG_RW, &sc->dwelltime, 0,
365 "Channel dwell time (ms) for AP/station scanning");
368 * Hook our interrupt after all initialization is complete
370 error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE,
371 ipw_intr, sc, &sc->sc_ih, ifp->if_serializer);
373 device_printf(dev, "could not set up interrupt\n");
375 ieee80211_ifdetach(ic);
380 ieee80211_announce(ic);
390 ipw_detach(device_t dev)
392 struct ipw_softc *sc = device_get_softc(dev);
393 struct ifnet *ifp = &sc->sc_ic.ic_if;
395 if (device_is_attached(dev)) {
396 lwkt_serialize_enter(ifp->if_serializer);
398 ipw_free_firmware(sc);
399 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
400 lwkt_serialize_exit(ifp->if_serializer);
403 ieee80211_ifdetach(&sc->sc_ic);
409 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
412 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
414 sysctl_ctx_free(&sc->sysctl_ctx);
420 ipw_dma_alloc(device_t dev)
422 struct ipw_soft_bd *sbd;
423 struct ipw_soft_hdr *shdr;
424 struct ipw_soft_buf *sbuf;
427 struct ipw_softc *sc;
429 sc = device_get_softc(dev);
431 * Allocate and map tx ring
433 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
434 BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0,
437 device_printf(dev, "could not create tx ring DMA tag\n");
441 error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
442 BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->tbd_map);
444 device_printf(dev, "could not allocate tx ring DMA memory\n");
448 error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
449 IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
451 device_printf(dev, "could not map tx ring DMA memory\n");
456 * Allocate and map rx ring
458 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
459 BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0,
462 device_printf(dev, "could not create rx ring DMA tag\n");
466 error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
467 BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->rbd_map);
469 device_printf(dev, "could not allocate rx ring DMA memory\n");
473 error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
474 IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
476 device_printf(dev, "could not map rx ring DMA memory\n");
481 * Allocate and map status ring
483 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
484 BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
487 device_printf(dev, "could not create status ring DMA tag\n");
491 error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
492 BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->status_map);
495 "could not allocate status ring DMA memory\n");
499 error = bus_dmamap_load(sc->status_dmat, sc->status_map,
500 sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
503 device_printf(dev, "could not map status ring DMA memory\n");
508 * Allocate command DMA map
510 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
511 BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
512 sizeof (struct ipw_cmd), 0, &sc->cmd_dmat);
514 device_printf(dev, "could not create command DMA tag\n");
518 error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
520 device_printf(dev, "could not create command DMA map\n");
525 * Allocate headers DMA maps
527 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
528 BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
529 sizeof (struct ipw_hdr), 0, &sc->hdr_dmat);
531 device_printf(dev, "could not create header DMA tag\n");
535 SLIST_INIT(&sc->free_shdr);
536 for (i = 0; i < IPW_NDATA; i++) {
537 shdr = &sc->shdr_list[i];
538 error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
540 device_printf(dev, "could not create header DMA map\n");
543 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
547 * Allocate tx buffers DMA maps
549 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
550 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
553 device_printf(dev, "could not create tx DMA tag\n");
557 SLIST_INIT(&sc->free_sbuf);
558 for (i = 0; i < IPW_NDATA; i++) {
559 sbuf = &sc->tx_sbuf_list[i];
560 error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
562 device_printf(dev, "could not create tx DMA map\n");
565 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
571 for (i = 0; i < IPW_NTBD; i++) {
572 sbd = &sc->stbd_list[i];
573 sbd->bd = &sc->tbd_list[i];
574 sbd->type = IPW_SBD_TYPE_NOASSOC;
578 * Pre-allocate rx buffers and DMA maps
580 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
581 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_NRBD, MCLBYTES, 0,
584 device_printf(dev, "could not create rx DMA tag\n");
588 for (i = 0; i < IPW_NRBD; i++) {
589 sbd = &sc->srbd_list[i];
590 sbuf = &sc->rx_sbuf_list[i];
591 sbd->bd = &sc->rbd_list[i];
593 sbuf->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
594 if (sbuf->m == NULL) {
595 device_printf(dev, "could not allocate rx mbuf\n");
600 error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
602 device_printf(dev, "could not create rx DMA map\n");
606 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
607 mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
610 device_printf(dev, "could not map rx DMA memory\n");
614 sbd->type = IPW_SBD_TYPE_DATA;
616 sbd->bd->physaddr = htole32(physaddr);
617 sbd->bd->len = htole32(MCLBYTES);
620 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
624 fail: ipw_release(sc);
629 ipw_release(struct ipw_softc *sc)
631 struct ipw_soft_buf *sbuf;
634 if (sc->tbd_dmat != NULL) {
635 if (sc->stbd_list != NULL) {
636 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map,
637 BUS_DMASYNC_POSTWRITE);
638 bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
639 bus_dmamem_free(sc->tbd_dmat, sc->tbd_list,
642 bus_dma_tag_destroy(sc->tbd_dmat);
645 if (sc->rbd_dmat != NULL) {
646 if (sc->rbd_list != NULL) {
647 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map,
648 BUS_DMASYNC_POSTWRITE);
649 bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
650 bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
653 bus_dma_tag_destroy(sc->rbd_dmat);
656 if (sc->status_dmat != NULL) {
657 if (sc->status_list != NULL) {
658 bus_dmamap_sync(sc->status_dmat, sc->status_map,
659 BUS_DMASYNC_POSTWRITE);
660 bus_dmamap_unload(sc->status_dmat, sc->status_map);
661 bus_dmamem_free(sc->status_dmat, sc->status_list,
664 bus_dma_tag_destroy(sc->status_dmat);
667 for (i = 0; i < IPW_NTBD; i++)
668 ipw_release_sbd(sc, &sc->stbd_list[i]);
670 if (sc->cmd_dmat != NULL) {
671 bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
672 bus_dma_tag_destroy(sc->cmd_dmat);
675 if (sc->hdr_dmat != NULL) {
676 for (i = 0; i < IPW_NDATA; i++)
677 bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
678 bus_dma_tag_destroy(sc->hdr_dmat);
681 if (sc->txbuf_dmat != NULL) {
682 for (i = 0; i < IPW_NDATA; i++) {
683 bus_dmamap_destroy(sc->txbuf_dmat,
684 sc->tx_sbuf_list[i].map);
686 bus_dma_tag_destroy(sc->txbuf_dmat);
689 if (sc->rxbuf_dmat != NULL) {
690 for (i = 0; i < IPW_NRBD; i++) {
691 sbuf = &sc->rx_sbuf_list[i];
692 if (sbuf->m != NULL) {
693 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
694 BUS_DMASYNC_POSTREAD);
695 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
698 bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
700 bus_dma_tag_destroy(sc->rxbuf_dmat);
705 ipw_shutdown(device_t dev)
707 struct ipw_softc *sc = device_get_softc(dev);
708 struct ifnet *ifp = &sc->sc_ic.ic_if;
710 lwkt_serialize_enter(ifp->if_serializer);
712 lwkt_serialize_exit(ifp->if_serializer);
718 ipw_suspend(device_t dev)
720 struct ipw_softc *sc = device_get_softc(dev);
721 struct ifnet *ifp = &sc->sc_ic.ic_if;
723 lwkt_serialize_enter(ifp->if_serializer);
725 lwkt_serialize_exit(ifp->if_serializer);
731 ipw_resume(device_t dev)
733 struct ipw_softc *sc = device_get_softc(dev);
734 struct ifnet *ifp = &sc->sc_ic.ic_if;
736 lwkt_serialize_enter(ifp->if_serializer);
737 pci_write_config(dev, 0x41, 0, 1);
739 if (ifp->if_flags & IFF_UP) {
740 ifp->if_init(ifp->if_softc);
741 if (ifp->if_flags & IFF_RUNNING)
744 lwkt_serialize_exit(ifp->if_serializer);
750 ipw_media_change(struct ifnet *ifp)
752 struct ipw_softc *sc = ifp->if_softc;
755 error = ieee80211_media_change(ifp);
756 if (error != ENETRESET)
759 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
766 ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
768 struct ipw_softc *sc = ifp->if_softc;
769 struct ieee80211com *ic = &sc->sc_ic;
770 #define N(a) (sizeof (a) / sizeof (a[0]))
771 static const struct {
777 { IPW_RATE_DS5, 11 },
778 { IPW_RATE_DS11, 22 },
783 imr->ifm_status = IFM_AVALID;
784 imr->ifm_active = IFM_IEEE80211;
785 if (ic->ic_state == IEEE80211_S_RUN)
786 imr->ifm_status |= IFM_ACTIVE;
788 /* read current transmission rate from adapter */
789 val = ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf;
791 /* convert rate to 802.11 rate */
792 for (i = 0; i < N(rates) && rates[i].val != val; i++);
793 rate = (i < N(rates)) ? rates[i].rate : 0;
795 imr->ifm_active |= IFM_IEEE80211_11B;
796 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
797 switch (ic->ic_opmode) {
798 case IEEE80211_M_STA:
801 case IEEE80211_M_IBSS:
802 imr->ifm_active |= IFM_IEEE80211_IBSS;
805 case IEEE80211_M_MONITOR:
806 imr->ifm_active |= IFM_IEEE80211_MONITOR;
809 case IEEE80211_M_AHDEMO:
810 case IEEE80211_M_HOSTAP:
811 /* should not get there */
818 ipw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg __unused)
820 struct ipw_softc *sc = ic->ic_if.if_softc;
821 struct ieee80211_node *ni;
822 uint8_t macaddr[IEEE80211_ADDR_LEN];
826 case IEEE80211_S_RUN:
827 DELAY(200); /* firmware needs a short delay here */
829 len = IEEE80211_ADDR_LEN;
830 ipw_read_table2(sc, IPW_INFO_CURRENT_BSSID, macaddr, &len);
831 ni = ieee80211_find_node(&ic->ic_scan, macaddr);
835 ieee80211_ref_node(ni);
836 ieee80211_sta_join(ic, ni);
837 ieee80211_node_authorize(ni);
839 if (ic->ic_opmode == IEEE80211_M_STA)
840 ieee80211_notify_node_join(ic, ni, 1);
843 case IEEE80211_S_INIT:
844 case IEEE80211_S_SCAN:
845 case IEEE80211_S_AUTH:
846 case IEEE80211_S_ASSOC:
850 ic->ic_state = nstate;
855 * Read 16 bits at address 'addr' from the Microwire EEPROM.
856 * DON'T PLAY WITH THIS CODE UNLESS YOU KNOW *EXACTLY* WHAT YOU'RE DOING!
859 ipw_read_prom_word(struct ipw_softc *sc, u_int8_t addr)
865 /* Clock C once before the first command */
866 IPW_EEPROM_CTL(sc, 0);
867 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
868 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
869 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
871 /* Write start bit (1) */
872 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
873 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
875 /* Write READ opcode (10) */
876 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
877 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
878 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
879 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
881 /* Write address A7-A0 */
882 for (n = 7; n >= 0; n--) {
883 IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
884 (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
885 IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
886 (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
889 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
891 /* Read data Q15-Q0 */
893 for (n = 15; n >= 0; n--) {
894 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
895 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
896 tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
897 val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
900 IPW_EEPROM_CTL(sc, 0);
902 /* Clear Chip Select and clock C */
903 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
904 IPW_EEPROM_CTL(sc, 0);
905 IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
911 ipw_command_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
915 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
917 cmd = mtod(sbuf->m, struct ipw_cmd *);
919 DPRINTFN(2, ("RX!CMD!%u!%u!%u!%u!%u\n",
920 le32toh(cmd->type), le32toh(cmd->subtype), le32toh(cmd->seq),
921 le32toh(cmd->len), le32toh(cmd->status)));
927 ipw_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
929 struct ieee80211com *ic = &sc->sc_ic;
932 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
934 state = le32toh(*mtod(sbuf->m, u_int32_t *));
936 DPRINTFN(2, ("RX!NEWSTATE!%u\n", state));
939 case IPW_STATE_ASSOCIATED:
940 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
943 case IPW_STATE_SCANNING:
944 /* don't leave run state on background scan */
945 if (ic->ic_state != IEEE80211_S_RUN)
946 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
948 ic->ic_flags |= IEEE80211_F_SCAN;
951 case IPW_STATE_SCAN_COMPLETE:
952 ieee80211_notify_scan_done(ic);
953 ic->ic_flags &= ~IEEE80211_F_SCAN;
956 case IPW_STATE_ASSOCIATION_LOST:
957 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
960 case IPW_STATE_RADIO_DISABLED:
961 ic->ic_if.if_flags &= ~IFF_UP;
968 * XXX: Hack to set the current channel to the value advertised in beacons or
969 * probe responses. Only used during AP detection.
972 ipw_fix_channel(struct ieee80211com *ic, struct mbuf *m)
974 struct ieee80211_frame *wh;
978 wh = mtod(m, struct ieee80211_frame *);
980 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
983 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
985 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
986 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
989 frm = (uint8_t *)(wh + 1);
990 efrm = mtod(m, uint8_t *) + m->m_len;
992 frm += 12; /* skip tstamp, bintval and capinfo fields */
994 if (*frm == IEEE80211_ELEMID_DSPARMS)
995 #if IEEE80211_CHAN_MAX < 255
996 if (frm[2] <= IEEE80211_CHAN_MAX)
998 ic->ic_curchan = &ic->ic_channels[frm[2]];
1005 ipw_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1006 struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1008 struct ieee80211com *ic = &sc->sc_ic;
1009 struct ifnet *ifp = &ic->ic_if;
1010 struct mbuf *mnew, *m;
1011 struct ieee80211_frame *wh;
1012 struct ieee80211_node *ni;
1013 bus_addr_t physaddr;
1016 DPRINTFN(5, ("RX!DATA!%u!%u\n", le32toh(status->len), status->rssi));
1018 if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1019 le32toh(status->len) > MCLBYTES) {
1020 if_printf(ifp, "bad frame length\n");
1025 * Try to allocate a new mbuf for this ring element and load it before
1026 * processing the current mbuf. If the ring element cannot be loaded,
1027 * drop the received packet and reuse the old mbuf. In the unlikely
1028 * case that the old mbuf can't be reloaded either, explicitly panic.
1030 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1036 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1037 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1039 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1040 MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1044 /* try to reload the old mbuf */
1045 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1046 mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1048 if (error != 0) /* XXX very unlikely that it will fail... */
1049 panic("%s: could not load old rx mbuf", ifp->if_xname);
1055 * New mbuf successfully loaded, update Rx ring and continue
1060 sbd->bd->physaddr = htole32(physaddr);
1063 m->m_pkthdr.rcvif = ifp;
1064 m->m_pkthdr.len = m->m_len = le32toh(status->len);
1066 if (sc->sc_drvbpf != NULL) {
1067 struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1070 tap->wr_antsignal = status->rssi;
1071 tap->wr_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1072 tap->wr_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1074 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1077 if (ic->ic_state == IEEE80211_S_SCAN)
1078 ipw_fix_channel(ic, m);
1080 wh = mtod(m, struct ieee80211_frame *);
1081 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1083 /* send the frame to the 802.11 layer */
1084 ieee80211_input(ic, m, ni, status->rssi, 0);
1086 /* node is no longer needed */
1087 ieee80211_free_node(ni);
1089 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1093 ipw_notification_intr(struct ipw_softc *sc __unused, struct ipw_soft_buf *sbuf __unused)
1095 DPRINTFN(2, ("RX!NOTIFICATION\n"));
1099 ipw_rx_intr(struct ipw_softc *sc)
1101 struct ipw_status *status;
1102 struct ipw_soft_bd *sbd;
1103 struct ipw_soft_buf *sbuf;
1106 if (!(sc->flags & IPW_FLAG_FW_INITED))
1109 r = CSR_READ_4(sc, IPW_CSR_RX_READ_INDEX);
1111 bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1113 for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1115 status = &sc->status_list[i];
1116 sbd = &sc->srbd_list[i];
1119 switch (le16toh(status->code) & 0xf) {
1120 case IPW_STATUS_CODE_COMMAND:
1121 ipw_command_intr(sc, sbuf);
1124 case IPW_STATUS_CODE_NEWSTATE:
1125 ipw_newstate_intr(sc, sbuf);
1128 case IPW_STATUS_CODE_DATA_802_3:
1129 case IPW_STATUS_CODE_DATA_802_11:
1130 ipw_data_intr(sc, status, sbd, sbuf);
1133 case IPW_STATUS_CODE_NOTIFICATION:
1134 ipw_notification_intr(sc, sbuf);
1138 if_printf(&sc->sc_ic.ic_if, "unknown status code %u\n",
1139 le16toh(status->code));
1142 /* firmware was killed, stop processing received frames */
1143 if (!(sc->flags & IPW_FLAG_FW_INITED))
1148 /* Some buffer descriptors may have changed */
1149 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1151 /* Tell the firmware what we have processed */
1152 sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1153 CSR_WRITE_4(sc, IPW_CSR_RX_WRITE_INDEX, sc->rxcur);
1157 ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1159 struct ipw_soft_hdr *shdr;
1160 struct ipw_soft_buf *sbuf;
1162 switch (sbd->type) {
1163 case IPW_SBD_TYPE_COMMAND:
1164 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1165 BUS_DMASYNC_POSTWRITE);
1166 bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1169 case IPW_SBD_TYPE_HEADER:
1171 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1172 bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1173 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1176 case IPW_SBD_TYPE_DATA:
1178 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1179 BUS_DMASYNC_POSTWRITE);
1180 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1181 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1184 ieee80211_free_node(sbuf->ni);
1186 /* kill watchdog timer */
1187 sc->sc_tx_timer = 0;
1190 sbd->type = IPW_SBD_TYPE_NOASSOC;
1194 ipw_tx_intr(struct ipw_softc *sc)
1196 struct ifnet *ifp = &sc->sc_ic.ic_if;
1199 if (!(sc->flags & IPW_FLAG_FW_INITED))
1202 r = CSR_READ_4(sc, IPW_CSR_TX_READ_INDEX);
1204 for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1205 struct ipw_soft_bd *sbd;
1207 sbd = &sc->stbd_list[i];
1208 if (sbd->type == IPW_SBD_TYPE_DATA)
1211 ipw_release_sbd(sc, sbd);
1215 /* Remember what the firmware has processed */
1216 sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1218 /* Call start() since some buffer descriptors have been released */
1219 ifp->if_flags &= ~IFF_OACTIVE;
1220 (*ifp->if_start)(ifp);
1226 struct ipw_softc *sc = arg;
1229 if ((r = CSR_READ_4(sc, IPW_CSR_INTR)) == 0 || r == 0xffffffff)
1232 /* Disable interrupts */
1233 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1235 DPRINTFN(8, ("INTR!0x%08x\n", r));
1237 if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1238 if_printf(&sc->sc_ic.ic_if, "fatal error\n");
1239 sc->sc_ic.ic_if.if_flags &= ~IFF_UP;
1243 if (r & IPW_INTR_FW_INIT_DONE) {
1244 if (!(r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)))
1248 if (r & IPW_INTR_RX_TRANSFER)
1251 if (r & IPW_INTR_TX_TRANSFER)
1254 /* Acknowledge interrupts */
1255 CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1257 /* Re-enable interrupts */
1258 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1262 ipw_dma_map_txbuf(void *arg, bus_dma_segment_t *segs, int nseg,
1263 bus_size_t mapsize, int error)
1265 struct ipw_dma_mapping *map = arg;
1270 KASSERT(nseg <= IPW_MAX_NSEG, ("too many DMA segments %d", nseg));
1272 bcopy(segs, map->segs, nseg * sizeof (bus_dma_segment_t));
1274 map->mapsize = mapsize;
1278 ipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg __unused, int error)
1283 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1285 *(bus_addr_t *)arg = segs[0].ds_addr;
1289 ipw_cmd(struct ipw_softc *sc, u_int32_t type, void *data, u_int32_t len)
1291 struct ifnet *ifp = &sc->sc_ic.ic_if;
1292 struct ipw_soft_bd *sbd;
1293 bus_addr_t physaddr;
1296 sbd = &sc->stbd_list[sc->txcur];
1298 error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1299 sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1301 if_printf(&sc->sc_ic.ic_if,
1302 "could not map command DMA memory\n");
1306 sc->cmd.type = htole32(type);
1307 sc->cmd.subtype = htole32(0);
1308 sc->cmd.len = htole32(len);
1309 sc->cmd.seq = htole32(0);
1311 bcopy(data, sc->cmd.data, len);
1313 sbd->type = IPW_SBD_TYPE_COMMAND;
1314 sbd->bd->physaddr = htole32(physaddr);
1315 sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1317 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1318 IPW_BD_FLAG_TX_LAST_FRAGMENT;
1320 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1321 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1324 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1326 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
1329 * This is kinda messy. Since we may be MP, a combination of
1330 * a critical section for a local cpu interrupt and
1331 * tsleep_interlock() for a remote cpu interrupt is required to
1332 * avoid command completion racing the tsleep.
1335 tsleep_interlock(sc);
1336 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
1337 lwkt_serialize_exit(ifp->if_serializer);
1338 error = tsleep(sc, 0, "ipwcmd", hz);
1340 lwkt_serialize_enter(ifp->if_serializer);
1345 ipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1347 struct ipw_softc *sc = ifp->if_softc;
1348 struct ieee80211com *ic = &sc->sc_ic;
1349 struct ieee80211_frame *wh;
1350 struct ipw_dma_mapping map;
1351 struct ipw_soft_bd *sbd;
1352 struct ipw_soft_hdr *shdr;
1353 struct ipw_soft_buf *sbuf;
1355 bus_addr_t physaddr;
1358 wh = mtod(m0, struct ieee80211_frame *);
1359 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1360 if (ieee80211_crypto_encap(ic, ni, m0) == NULL) {
1365 /* packet header may have moved, reset our local pointer */
1366 wh = mtod(m0, struct ieee80211_frame *);
1369 if (sc->sc_drvbpf != NULL) {
1370 struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1373 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1374 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1376 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1379 shdr = SLIST_FIRST(&sc->free_shdr);
1380 sbuf = SLIST_FIRST(&sc->free_sbuf);
1381 KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1383 shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1384 shdr->hdr.subtype = htole32(0);
1385 shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1386 shdr->hdr.encrypt = 0;
1387 shdr->hdr.keyidx = 0;
1388 shdr->hdr.keysz = 0;
1389 shdr->hdr.fragmentsz = htole16(0);
1390 IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1391 if (ic->ic_opmode == IEEE80211_M_STA)
1392 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1394 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1396 /* trim IEEE802.11 header */
1397 m_adj(m0, sizeof (struct ieee80211_frame));
1399 error = bus_dmamap_load_mbuf(sc->txbuf_dmat, sbuf->map, m0,
1400 ipw_dma_map_txbuf, &map, 0);
1401 if (error != 0 && error != EFBIG) {
1402 if_printf(ifp, "could not map mbuf (error %d)\n", error);
1407 mnew = m_defrag(m0, MB_DONTWAIT);
1409 if_printf(ifp, "could not defragment mbuf\n");
1415 error = bus_dmamap_load_mbuf(sc->txbuf_dmat, sbuf->map, m0,
1416 ipw_dma_map_txbuf, &map, 0);
1419 "could not map mbuf (error %d)\n", error);
1425 error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1426 sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1428 if_printf(ifp, "could not map header DMA memory\n");
1429 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1434 SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1435 SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1437 sbd = &sc->stbd_list[sc->txcur];
1438 sbd->type = IPW_SBD_TYPE_HEADER;
1440 sbd->bd->physaddr = htole32(physaddr);
1441 sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1442 sbd->bd->nfrag = 1 + map.nseg;
1443 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1444 IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1446 DPRINTFN(5, ("TX!HDR!%u!%u!%u!%u!%6D!%6D\n", shdr->hdr.type,
1447 shdr->hdr.subtype, shdr->hdr.encrypted, shdr->hdr.encrypt,
1448 shdr->hdr.src_addr, ":", shdr->hdr.dst_addr, ":"));
1449 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1455 for (i = 0; i < map.nseg; i++) {
1456 sbd = &sc->stbd_list[sc->txcur];
1458 sbd->bd->physaddr = htole32(map.segs[i].ds_addr);
1459 sbd->bd->len = htole32(map.segs[i].ds_len);
1460 sbd->bd->nfrag = 0; /* used only in first bd */
1461 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1462 if (i == map.nseg - 1) {
1463 sbd->type = IPW_SBD_TYPE_DATA;
1465 sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1467 sbd->type = IPW_SBD_TYPE_NOASSOC;
1468 sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1471 DPRINTFN(5, ("TX!FRAG!%d!%d\n", i, map.segs[i].ds_len));
1472 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1476 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1477 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1478 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1480 /* Inform firmware about this new packet */
1481 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
1487 ipw_start(struct ifnet *ifp)
1489 struct ipw_softc *sc = ifp->if_softc;
1490 struct ieee80211com *ic = &sc->sc_ic;
1492 struct ieee80211_node *ni;
1494 if (ic->ic_state != IEEE80211_S_RUN) {
1499 struct ether_header *eh;
1501 m0 = ifq_poll(&ifp->if_snd);
1504 if (sc->txfree < 1 + IPW_MAX_NSEG) {
1505 ifp->if_flags |= IFF_OACTIVE;
1508 ifq_dequeue(&ifp->if_snd, m0);
1510 if (m0->m_len < sizeof (struct ether_header) &&
1511 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL)
1514 eh = mtod(m0, struct ether_header *);
1515 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1523 m0 = ieee80211_encap(ic, m0, ni);
1525 ieee80211_free_node(ni);
1529 if (ic->ic_rawbpf != NULL)
1530 bpf_mtap(ic->ic_rawbpf, m0);
1532 if (ipw_tx_start(ifp, m0, ni) != 0) {
1533 ieee80211_free_node(ni);
1538 /* start watchdog timer */
1539 sc->sc_tx_timer = 5;
1545 ipw_watchdog(struct ifnet *ifp)
1547 struct ipw_softc *sc = ifp->if_softc;
1551 if (sc->sc_tx_timer > 0) {
1552 if (--sc->sc_tx_timer == 0) {
1553 if_printf(ifp, "device timeout\n");
1555 ifp->if_flags &= ~IFF_UP;
1562 ieee80211_watchdog(&sc->sc_ic);
1566 ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1568 struct ipw_softc *sc = ifp->if_softc;
1569 struct ieee80211com *ic = &sc->sc_ic;
1575 if (ifp->if_flags & IFF_UP) {
1576 if (!(ifp->if_flags & IFF_RUNNING))
1579 if (ifp->if_flags & IFF_RUNNING)
1585 /* only super-user can do that! */
1586 if ((error = suser(curthread)) != 0)
1589 ifr = (struct ifreq *)data;
1590 error = ipw_cache_firmware(sc, ifr->ifr_data);
1594 /* only super-user can do that! */
1595 if ((error = suser(curthread)) != 0)
1598 ifp->if_flags &= ~IFF_UP;
1600 ipw_free_firmware(sc);
1604 error = ieee80211_ioctl(ic, cmd, data, cr);
1607 if (error == ENETRESET) {
1608 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1609 (IFF_UP | IFF_RUNNING))
1618 ipw_stop_master(struct ipw_softc *sc)
1622 /* Disable interrupts */
1623 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1625 CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1626 for (ntries = 0; ntries < 50; ntries++) {
1627 if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1632 if_printf(&sc->sc_ic.ic_if, "timeout waiting for master\n");
1634 CSR_WRITE_4(sc, IPW_CSR_RST, CSR_READ_4(sc, IPW_CSR_RST) |
1635 IPW_RST_PRINCETON_RESET);
1637 sc->flags &= ~IPW_FLAG_FW_INITED;
1641 ipw_reset(struct ipw_softc *sc)
1645 ipw_stop_master(sc);
1647 /* Move adapter to D0 state */
1648 CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
1651 /* Wait for clock stabilization */
1652 for (ntries = 0; ntries < 1000; ntries++) {
1653 if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1660 CSR_WRITE_4(sc, IPW_CSR_RST, CSR_READ_4(sc, IPW_CSR_RST) |
1665 CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
1672 ipw_load_ucode(struct ipw_softc *sc, u_char *uc, int size)
1676 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1677 CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1679 MEM_WRITE_2(sc, 0x220000, 0x0703);
1680 MEM_WRITE_2(sc, 0x220000, 0x0707);
1682 MEM_WRITE_1(sc, 0x210014, 0x72);
1683 MEM_WRITE_1(sc, 0x210014, 0x72);
1685 MEM_WRITE_1(sc, 0x210000, 0x40);
1686 MEM_WRITE_1(sc, 0x210000, 0x00);
1687 MEM_WRITE_1(sc, 0x210000, 0x40);
1689 MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1691 MEM_WRITE_1(sc, 0x210000, 0x00);
1692 MEM_WRITE_1(sc, 0x210000, 0x00);
1693 MEM_WRITE_1(sc, 0x210000, 0x80);
1695 MEM_WRITE_2(sc, 0x220000, 0x0703);
1696 MEM_WRITE_2(sc, 0x220000, 0x0707);
1698 MEM_WRITE_1(sc, 0x210014, 0x72);
1699 MEM_WRITE_1(sc, 0x210014, 0x72);
1701 MEM_WRITE_1(sc, 0x210000, 0x00);
1702 MEM_WRITE_1(sc, 0x210000, 0x80);
1704 for (ntries = 0; ntries < 100; ntries++) {
1705 if (MEM_READ_1(sc, 0x210000) & 1)
1709 if (ntries == 100) {
1710 if_printf(&sc->sc_ic.ic_if,
1711 "timeout waiting for ucode to initialize\n");
1715 MEM_WRITE_4(sc, 0x3000e0, 0);
1720 /* set of macros to handle unaligned little endian data in firmware image */
1721 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1722 #define GETLE16(p) ((p)[0] | (p)[1] << 8)
1724 ipw_load_firmware(struct ipw_softc *sc, u_char *fw, int size)
1726 struct ifnet *ifp = &sc->sc_ic.ic_if;
1738 dst = GETLE32(p); p += 4;
1739 len = GETLE16(p); p += 2;
1744 ipw_write_mem_1(sc, dst, p, len);
1748 CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
1751 /* Allow interrupts so we know when the firmware is inited */
1752 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1755 * Tell the adapter to initialize the firmware.
1757 * This is kinda messy. Since we may be MP, a combination of
1758 * a critical section for a local cpu interrupt and
1759 * tsleep_interlock() for a remote cpu interrupt is required to
1760 * avoid command completion racing the tsleep.
1763 CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1764 CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
1765 IPW_CTL_ALLOW_STANDBY);
1767 tsleep_interlock(sc);
1768 lwkt_serialize_exit(ifp->if_serializer);
1769 error = tsleep(sc, 0, "ipwinit", hz);
1771 lwkt_serialize_enter(ifp->if_serializer);
1773 if_printf(&sc->sc_ic.ic_if, "timeout waiting for firmware "
1774 "initialization to complete\n");
1778 CSR_WRITE_4(sc, IPW_CSR_IO, CSR_READ_4(sc, IPW_CSR_IO) |
1779 IPW_IO_GPIO1_MASK | IPW_IO_GPIO3_MASK);
1785 * Store firmware into kernel memory so we can download it when we need to,
1786 * e.g when the adapter wakes up from suspend mode.
1789 ipw_cache_firmware(struct ipw_softc *sc, void *data)
1791 struct ipw_firmware *fw = &sc->fw;
1792 struct ipw_firmware_hdr hdr;
1796 ipw_free_firmware(sc);
1799 * mutex(9): no mutexes should be held across functions which access
1800 * memory in userspace, such as copyin(9) [...]
1803 if ((error = copyin(data, &hdr, sizeof hdr)) != 0)
1806 fw->main_size = le32toh(hdr.main_size);
1807 fw->ucode_size = le32toh(hdr.ucode_size);
1810 fw->main = malloc(fw->main_size, M_DEVBUF, M_WAITOK);
1811 if (fw->main == NULL) {
1816 fw->ucode = malloc(fw->ucode_size, M_DEVBUF, M_WAITOK);
1817 if (fw->ucode == NULL) {
1822 if ((error = copyin(p, fw->main, fw->main_size)) != 0)
1826 if ((error = copyin(p, fw->ucode, fw->ucode_size)) != 0)
1829 DPRINTF(("Firmware cached: main %u, ucode %u\n", fw->main_size,
1832 sc->flags |= IPW_FLAG_FW_CACHED;
1836 fail3: free(fw->ucode, M_DEVBUF);
1837 fail2: free(fw->main, M_DEVBUF);
1844 ipw_free_firmware(struct ipw_softc *sc)
1846 if (!(sc->flags & IPW_FLAG_FW_CACHED))
1849 free(sc->fw.main, M_DEVBUF);
1850 free(sc->fw.ucode, M_DEVBUF);
1852 sc->flags &= ~IPW_FLAG_FW_CACHED;
1856 ipw_config(struct ipw_softc *sc)
1858 struct ieee80211com *ic = &sc->sc_ic;
1859 struct ifnet *ifp = &ic->ic_if;
1860 struct ipw_security security;
1861 struct ieee80211_key *k;
1862 struct ipw_wep_key wepkey;
1863 struct ipw_scan_options options;
1864 struct ipw_configuration config;
1868 switch (ic->ic_opmode) {
1869 case IEEE80211_M_STA:
1870 case IEEE80211_M_HOSTAP:
1871 data = htole32(IPW_MODE_BSS);
1874 case IEEE80211_M_IBSS:
1875 case IEEE80211_M_AHDEMO:
1876 data = htole32(IPW_MODE_IBSS);
1879 case IEEE80211_M_MONITOR:
1880 data = htole32(IPW_MODE_MONITOR);
1883 DPRINTF(("Setting mode to %u\n", le32toh(data)));
1884 error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
1888 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1889 ic->ic_opmode == IEEE80211_M_MONITOR) {
1890 data = htole32(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
1891 DPRINTF(("Setting channel to %u\n", le32toh(data)));
1892 error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
1897 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
1898 DPRINTF(("Enabling adapter\n"));
1899 return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1902 IEEE80211_ADDR_COPY(((struct arpcom *)ifp)->ac_enaddr, ic->ic_myaddr);
1903 IEEE80211_ADDR_COPY(IF_LLADDR(ifp), ic->ic_myaddr);
1904 DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":"));
1905 error = ipw_cmd(sc, IPW_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
1906 IEEE80211_ADDR_LEN);
1910 config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
1911 IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
1912 if (ic->ic_opmode == IEEE80211_M_IBSS)
1913 config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
1914 if (ifp->if_flags & IFF_PROMISC)
1915 config.flags |= htole32(IPW_CFG_PROMISCUOUS);
1916 config.bss_chan = htole32(0x3fff); /* channels 1-14 */
1917 config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
1918 DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
1919 error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
1923 data = htole32(0x3); /* 1, 2 */
1924 DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
1925 error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
1929 data = htole32(0xf); /* 1, 2, 5.5, 11 */
1930 DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
1931 error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
1935 data = htole32(IPW_POWER_MODE_CAM);
1936 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
1937 error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
1941 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1942 data = htole32(32); /* default value */
1943 DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
1944 error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
1950 data = htole32(ic->ic_rtsthreshold);
1951 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
1952 error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
1956 data = htole32(ic->ic_fragthreshold);
1957 DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
1958 error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
1963 if (ipw_debug > 0) {
1964 printf("Setting ESSID to ");
1965 ieee80211_print_essid(ic->ic_des_essid, ic->ic_des_esslen);
1969 error = ipw_cmd(sc, IPW_CMD_SET_ESSID, ic->ic_des_essid,
1974 /* no mandatory BSSID */
1975 DPRINTF(("Setting mandatory BSSID to null\n"));
1976 error = ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
1980 if (ic->ic_flags & IEEE80211_F_DESBSSID) {
1981 DPRINTF(("Setting desired BSSID to %6D\n", ic->ic_des_bssid,
1983 error = ipw_cmd(sc, IPW_CMD_SET_DESIRED_BSSID,
1984 ic->ic_des_bssid, IEEE80211_ADDR_LEN);
1989 bzero(&security, sizeof security);
1990 security.authmode = (ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED) ?
1991 IPW_AUTH_SHARED : IPW_AUTH_OPEN;
1992 security.ciphers = htole32(IPW_CIPHER_NONE);
1993 DPRINTF(("Setting authmode to %u\n", security.authmode));
1994 error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFORMATION, &security,
1999 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2000 k = ic->ic_crypto.cs_nw_keys;
2001 for (i = 0; i < IEEE80211_WEP_NKID; i++, k++) {
2002 if (k->wk_keylen == 0)
2006 wepkey.len = k->wk_keylen;
2007 bzero(wepkey.key, sizeof wepkey.key);
2008 bcopy(k->wk_key, wepkey.key, k->wk_keylen);
2009 DPRINTF(("Setting wep key index %u len %u\n",
2010 wepkey.idx, wepkey.len));
2011 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2017 data = htole32(ic->ic_crypto.cs_def_txkey);
2018 DPRINTF(("Setting wep tx key index to %u\n", le32toh(data)));
2019 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2025 data = htole32((ic->ic_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2026 DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2027 error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2032 struct ipw_wpa_ie ie;
2034 memset(&ie, 0, sizeof ie);
2035 ie.len = htole32(sizeof (struct ieee80211_ie_wpa));
2036 DPRINTF(("Setting wpa ie\n"));
2037 error = ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &ie, sizeof ie);
2042 if (ic->ic_opmode == IEEE80211_M_IBSS ||
2043 ic->ic_opmode == IEEE80211_M_HOSTAP) {
2044 data = htole32(ic->ic_bintval);
2045 DPRINTF(("Setting beacon interval to %u\n", le32toh(data)));
2046 error = ipw_cmd(sc, IPW_CMD_SET_BEACON_INTERVAL, &data,
2052 options.flags = htole32(0);
2053 options.channels = htole32(0x3fff); /* scan channels 1-14 */
2054 DPRINTF(("Setting scan options to 0x%x\n", le32toh(options.flags)));
2055 error = ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &options, sizeof options);
2059 /* finally, enable adapter (start scanning for an access point) */
2060 DPRINTF(("Enabling adapter\n"));
2061 return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
2065 ipw_init(void *priv)
2067 struct ipw_softc *sc = priv;
2068 struct ieee80211com *ic = &sc->sc_ic;
2069 struct ifnet *ifp = &ic->ic_if;
2070 struct ipw_firmware *fw = &sc->fw;
2072 /* exit immediately if firmware has not been ioctl'd */
2073 if (!(sc->flags & IPW_FLAG_FW_CACHED)) {
2074 if (!(sc->flags & IPW_FLAG_FW_WARNED))
2075 if_printf(ifp, "Please load firmware\n");
2076 sc->flags |= IPW_FLAG_FW_WARNED;
2077 ifp->if_flags &= ~IFF_UP;
2083 if (ipw_reset(sc) != 0) {
2084 if_printf(ifp, "could not reset adapter\n");
2088 if (ipw_load_ucode(sc, fw->ucode, fw->ucode_size) != 0) {
2089 if_printf(ifp, "could not load microcode\n");
2093 ipw_stop_master(sc);
2096 * Setup tx, rx and status rings
2098 CSR_WRITE_4(sc, IPW_CSR_TX_BD_BASE, sc->tbd_phys);
2099 CSR_WRITE_4(sc, IPW_CSR_TX_BD_SIZE, IPW_NTBD);
2100 CSR_WRITE_4(sc, IPW_CSR_TX_READ_INDEX, 0);
2101 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, 0);
2102 sc->txold = IPW_NTBD - 1; /* latest bd index ack'ed by firmware */
2103 sc->txcur = 0; /* bd index to write to */
2104 sc->txfree = IPW_NTBD - 2;
2106 CSR_WRITE_4(sc, IPW_CSR_RX_BD_BASE, sc->rbd_phys);
2107 CSR_WRITE_4(sc, IPW_CSR_RX_BD_SIZE, IPW_NRBD);
2108 CSR_WRITE_4(sc, IPW_CSR_RX_READ_INDEX, 0);
2109 CSR_WRITE_4(sc, IPW_CSR_RX_WRITE_INDEX, IPW_NRBD - 1);
2110 sc->rxcur = IPW_NRBD - 1; /* latest bd index I've read */
2112 CSR_WRITE_4(sc, IPW_CSR_RX_STATUS_BASE, sc->status_phys);
2114 if (ipw_load_firmware(sc, fw->main, fw->main_size) != 0) {
2115 if_printf(ifp, "could not load firmware\n");
2119 sc->flags |= IPW_FLAG_FW_INITED;
2121 /* Retrieve information tables base addresses */
2122 sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2123 sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2125 ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2127 if (ipw_config(sc) != 0) {
2128 if_printf(ifp, "device configuration failed\n");
2132 ifp->if_flags &= ~IFF_OACTIVE;
2133 ifp->if_flags |= IFF_RUNNING;
2137 fail: ifp->if_flags &= ~IFF_UP;
2142 ipw_stop(void *priv)
2144 struct ipw_softc *sc = priv;
2145 struct ieee80211com *ic = &sc->sc_ic;
2146 struct ifnet *ifp = &ic->ic_if;
2149 ipw_stop_master(sc);
2150 CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2153 * Release tx buffers
2155 for (i = 0; i < IPW_NTBD; i++)
2156 ipw_release_sbd(sc, &sc->stbd_list[i]);
2158 sc->sc_tx_timer = 0;
2160 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2162 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2166 ipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2168 struct ipw_softc *sc = arg1;
2169 struct ifnet *ifp = &sc->sc_ic.ic_if;
2170 u_int32_t i, size, buf[256];
2172 (void)arg2; /* silence WARNS == 6 */
2173 (void)oidp; /* silence WARNS == 6 */
2175 lwkt_serialize_enter(ifp->if_serializer);
2177 if (!(sc->flags & IPW_FLAG_FW_INITED)) {
2178 bzero(buf, sizeof buf);
2182 CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2184 size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2185 for (i = 1; i < size; i++)
2186 buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2188 lwkt_serialize_exit(ifp->if_serializer);
2189 return SYSCTL_OUT(req, buf, sizeof buf);
2193 ipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2195 struct ipw_softc *sc = arg1;
2196 struct ifnet *ifp = &sc->sc_ic.ic_if;
2199 (void)arg2; /* silence WARNS == 6 */
2200 (void)oidp; /* silence WARNS == 6 */
2202 lwkt_serialize_enter(ifp->if_serializer);
2203 val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2204 (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2205 lwkt_serialize_exit(ifp->if_serializer);
2207 return SYSCTL_OUT(req, &val, sizeof val);
2211 ipw_read_table1(struct ipw_softc *sc, u_int32_t off)
2213 return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2217 ipw_write_table1(struct ipw_softc *sc, u_int32_t off, u_int32_t info)
2219 MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2223 ipw_read_table2(struct ipw_softc *sc, u_int32_t off, void *buf, u_int32_t *len)
2225 u_int32_t addr, info;
2226 u_int16_t count, size;
2229 /* addr[4] + count[2] + size[2] */
2230 addr = MEM_READ_4(sc, sc->table2_base + off);
2231 info = MEM_READ_4(sc, sc->table2_base + off + 4);
2234 size = info & 0xffff;
2235 total = count * size;
2243 ipw_read_mem_1(sc, addr, buf, total);
2249 ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, u_int8_t *datap,
2252 for (; count > 0; offset++, datap++, count--) {
2253 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2254 *datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2259 ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, u_int8_t *datap,
2262 for (; count > 0; offset++, datap++, count--) {
2263 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2264 CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);