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