2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
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, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Driver for the Atheros Wireless LAN controller.
36 * This software is derived from work of Atsushi Onoe; his contribution
37 * is greatly appreciated.
43 * This is needed for register operations which are performed
44 * by the driver - eg, calls to ath_hal_gettsf32().
46 * It's also required for any AH_DEBUG checks in here, eg the
47 * module dependencies.
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/sysctl.h>
56 #include <sys/malloc.h>
58 #include <sys/kernel.h>
59 #include <sys/socket.h>
60 #include <sys/sockio.h>
61 #include <sys/errno.h>
62 #include <sys/callout.h>
64 #include <sys/endian.h>
65 #include <sys/kthread.h>
66 #include <sys/taskqueue.h>
68 #include <sys/module.h>
71 #if defined(__DragonFly__)
74 #include <sys/smp.h> /* for mp_ncpus */
75 #include <machine/bus.h>
79 #include <net/if_var.h>
80 #include <net/if_dl.h>
81 #include <net/if_media.h>
82 #include <net/if_types.h>
83 #include <net/if_arp.h>
84 #include <net/ethernet.h>
85 #include <net/if_llc.h>
87 #include <netproto/802_11/ieee80211_var.h>
88 #include <netproto/802_11/ieee80211_regdomain.h>
89 #ifdef IEEE80211_SUPPORT_SUPERG
90 #include <netproto/802_11/ieee80211_superg.h>
92 #ifdef IEEE80211_SUPPORT_TDMA
93 #include <netproto/802_11/ieee80211_tdma.h>
99 #include <netinet/in.h>
100 #include <netinet/if_ether.h>
103 #include <dev/netif/ath/ath/if_athvar.h>
104 #include <dev/netif/ath/ath_hal/ah_devid.h> /* XXX for softled */
105 #include <dev/netif/ath/ath_hal/ah_diagcodes.h>
107 #include <dev/netif/ath/ath/if_ath_debug.h>
108 #include <dev/netif/ath/ath/if_ath_misc.h>
110 #include <dev/netif/ath/ath/if_ath_tsf.h>
111 #include <dev/netif/ath/ath/if_ath_tx.h>
112 #include <dev/netif/ath/ath/if_ath_sysctl.h>
113 #include <dev/netif/ath/ath/if_ath_led.h>
114 #include <dev/netif/ath/ath/if_ath_keycache.h>
115 #include <dev/netif/ath/ath/if_ath_rx.h>
116 #include <dev/netif/ath/ath/if_ath_rx_edma.h>
117 #include <dev/netif/ath/ath/if_ath_tx_edma.h>
118 #include <dev/netif/ath/ath/if_ath_beacon.h>
119 #include <dev/netif/ath/ath/if_ath_btcoex.h>
120 #include <dev/netif/ath/ath/if_ath_spectral.h>
121 #include <dev/netif/ath/ath/if_ath_lna_div.h>
122 #include <dev/netif/ath/ath/if_athdfs.h>
124 #include <dev/netif/ath/ath/if_ath_descdma.h>
126 MALLOC_DECLARE(M_ATHDEV);
129 * This is the descriptor setup / busdma memory intialisation and
134 ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
136 bus_addr_t *paddr = (bus_addr_t*) arg;
137 KASSERT(error == 0, ("error %u on bus_dma callback", error));
138 *paddr = segs->ds_addr;
142 * Allocate the descriptors and appropriate DMA tag/setup.
144 * For some situations (eg EDMA TX completion), there isn't a requirement
145 * for the ath_buf entries to be allocated.
148 ath_descdma_alloc_desc(struct ath_softc *sc,
149 struct ath_descdma *dd, ath_bufhead *head,
150 const char *name, int ds_size, int ndesc)
152 #define DS2PHYS(_dd, _ds) \
153 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
154 #define ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
155 ((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
158 dd->dd_descsize = ds_size;
160 DPRINTF(sc, ATH_DEBUG_RESET,
161 "%s: %s DMA: %u desc, %d bytes per descriptor\n",
162 __func__, name, ndesc, dd->dd_descsize);
165 dd->dd_desc_len = dd->dd_descsize * ndesc;
168 * Merlin work-around:
169 * Descriptors that cross the 4KB boundary can't be used.
170 * Assume one skipped descriptor per 4KB page.
172 if (! ath_hal_split4ktrans(sc->sc_ah)) {
173 int numpages = dd->dd_desc_len / 4096;
174 dd->dd_desc_len += ds_size * numpages;
178 * Setup DMA descriptor area.
180 * BUS_DMA_ALLOCNOW is not used; we never use bounce
181 * buffers for the descriptors themselves.
183 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */
184 PAGE_SIZE, 0, /* alignment, bounds */
185 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
186 BUS_SPACE_MAXADDR, /* highaddr */
187 NULL, NULL, /* filter, filterarg */
188 dd->dd_desc_len, /* maxsize */
190 dd->dd_desc_len, /* maxsegsize */
192 #if defined(__DragonFly__)
199 device_printf(sc->sc_dev,
200 "cannot allocate %s DMA tag\n", dd->dd_name);
204 /* allocate descriptors */
205 error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
206 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
209 device_printf(sc->sc_dev,
210 "unable to alloc memory for %u %s descriptors, error %u\n",
211 ndesc, dd->dd_name, error);
215 error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
216 dd->dd_desc, dd->dd_desc_len,
217 ath_load_cb, &dd->dd_desc_paddr,
220 device_printf(sc->sc_dev,
221 "unable to map %s descriptors, error %u\n",
226 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
227 __func__, dd->dd_name, (uint8_t *) dd->dd_desc,
228 (u_long) dd->dd_desc_len, (caddr_t) dd->dd_desc_paddr,
229 /*XXX*/ (u_long) dd->dd_desc_len);
234 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
236 bus_dma_tag_destroy(dd->dd_dmat);
237 memset(dd, 0, sizeof(*dd));
240 #undef ATH_DESC_4KB_BOUND_CHECK
244 ath_descdma_setup(struct ath_softc *sc,
245 struct ath_descdma *dd, ath_bufhead *head,
246 const char *name, int ds_size, int nbuf, int ndesc)
248 #define DS2PHYS(_dd, _ds) \
249 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
250 #define ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
251 ((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
256 /* Allocate descriptors */
257 error = ath_descdma_alloc_desc(sc, dd, head, name, ds_size,
260 /* Assume any errors during allocation were dealt with */
265 ds = (uint8_t *) dd->dd_desc;
267 /* allocate rx buffers */
268 bsize = sizeof(struct ath_buf) * nbuf;
269 bf = kmalloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
271 device_printf(sc->sc_dev,
272 "malloc of %s buffers failed, size %u\n",
279 for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * dd->dd_descsize)) {
280 bf->bf_desc = (struct ath_desc *) ds;
281 bf->bf_daddr = DS2PHYS(dd, ds);
282 if (! ath_hal_split4ktrans(sc->sc_ah)) {
284 * Merlin WAR: Skip descriptor addresses which
285 * cause 4KB boundary crossing along any point
288 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
290 /* Start at the next page */
291 ds += 0x1000 - (bf->bf_daddr & 0xFFF);
292 bf->bf_desc = (struct ath_desc *) ds;
293 bf->bf_daddr = DS2PHYS(dd, ds);
296 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
299 device_printf(sc->sc_dev, "unable to create dmamap "
300 "for %s buffer %u, error %u\n",
301 dd->dd_name, i, error);
302 ath_descdma_cleanup(sc, dd, head);
305 bf->bf_lastds = bf->bf_desc; /* Just an initial value */
306 TAILQ_INSERT_TAIL(head, bf, bf_list);
310 * XXX TODO: ensure that ds doesn't overflow the descriptor
311 * allocation otherwise weird stuff will occur and crash your
315 /* XXX this should likely just call ath_descdma_cleanup() */
317 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
318 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
319 bus_dma_tag_destroy(dd->dd_dmat);
320 memset(dd, 0, sizeof(*dd));
323 #undef ATH_DESC_4KB_BOUND_CHECK
327 * Allocate ath_buf entries but no descriptor contents.
329 * This is for RX EDMA where the descriptors are the header part of
333 ath_descdma_setup_rx_edma(struct ath_softc *sc,
334 struct ath_descdma *dd, ath_bufhead *head,
335 const char *name, int nbuf, int rx_status_len)
340 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers\n",
341 __func__, name, nbuf);
345 * This is (mostly) purely for show. We're not allocating any actual
346 * descriptors here as EDMA RX has the descriptor be part
349 * However, dd_desc_len is used by ath_descdma_free() to determine
350 * whether we have already freed this DMA mapping.
352 dd->dd_desc_len = rx_status_len * nbuf;
353 dd->dd_descsize = rx_status_len;
355 /* allocate rx buffers */
356 bsize = sizeof(struct ath_buf) * nbuf;
357 bf = kmalloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
359 device_printf(sc->sc_dev,
360 "malloc of %s buffers failed, size %u\n",
368 for (i = 0; i < nbuf; i++, bf++) {
371 bf->bf_lastds = NULL; /* Just an initial value */
373 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
376 device_printf(sc->sc_dev, "unable to create dmamap "
377 "for %s buffer %u, error %u\n",
378 dd->dd_name, i, error);
379 ath_descdma_cleanup(sc, dd, head);
382 TAILQ_INSERT_TAIL(head, bf, bf_list);
386 memset(dd, 0, sizeof(*dd));
391 ath_descdma_cleanup(struct ath_softc *sc,
392 struct ath_descdma *dd, ath_bufhead *head)
395 struct ieee80211_node *ni;
398 if (dd->dd_dmamap != 0) {
399 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
400 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
401 bus_dma_tag_destroy(dd->dd_dmat);
405 TAILQ_FOREACH(bf, head, bf_list) {
408 * XXX warn if there's buffers here.
409 * XXX it should have been freed by the
413 if (do_warning == 0) {
415 device_printf(sc->sc_dev,
416 "%s: %s: mbuf should've been"
417 " unmapped/freed!\n",
421 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
422 BUS_DMASYNC_POSTREAD);
423 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
427 if (bf->bf_dmamap != NULL) {
428 bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
429 bf->bf_dmamap = NULL;
435 * Reclaim node reference.
437 ieee80211_free_node(ni);
445 if (dd->dd_bufptr != NULL)
446 kfree(dd->dd_bufptr, M_ATHDEV);
447 memset(dd, 0, sizeof(*dd));