xref: /freebsd/sys/dev/rtwn/pci/rtwn_pci_attach.c (revision 5c831a5bd61576cacb48b39f8eeb47b92707a355)
1 /*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6  * Copyright (c) 2016 Andriy Voskoboinyk <avos@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include <sys/param.h>
25 #include <sys/sysctl.h>
26 #include <sys/lock.h>
27 #include <sys/mutex.h>
28 #include <sys/mbuf.h>
29 #include <sys/kernel.h>
30 #include <sys/socket.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 #include <sys/linker.h>
37 #include <sys/kdb.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
45 
46 #include <net/if.h>
47 #include <net/ethernet.h>
48 #include <net/if_media.h>
49 
50 #include <net80211/ieee80211_var.h>
51 
52 #include <dev/rtwn/if_rtwnreg.h>
53 #include <dev/rtwn/if_rtwnvar.h>
54 #include <dev/rtwn/if_rtwn_nop.h>
55 #include <dev/rtwn/if_rtwn_debug.h>
56 
57 #include <dev/rtwn/pci/rtwn_pci_var.h>
58 
59 #include <dev/rtwn/pci/rtwn_pci_attach.h>
60 #include <dev/rtwn/pci/rtwn_pci_reg.h>
61 #include <dev/rtwn/pci/rtwn_pci_rx.h>
62 #include <dev/rtwn/pci/rtwn_pci_tx.h>
63 
64 #include <dev/rtwn/rtl8192c/pci/r92ce_reg.h>
65 
66 
67 static device_probe_t	rtwn_pci_probe;
68 static device_attach_t	rtwn_pci_attach;
69 static device_detach_t	rtwn_pci_detach;
70 static device_shutdown_t rtwn_pci_shutdown;
71 static device_suspend_t	rtwn_pci_suspend;
72 static device_resume_t	rtwn_pci_resume;
73 
74 static int	rtwn_pci_alloc_rx_list(struct rtwn_softc *);
75 static void	rtwn_pci_reset_rx_list(struct rtwn_softc *);
76 static void	rtwn_pci_free_rx_list(struct rtwn_softc *);
77 static int	rtwn_pci_alloc_tx_list(struct rtwn_softc *, int);
78 static void	rtwn_pci_reset_tx_ring_stopped(struct rtwn_softc *, int);
79 static void	rtwn_pci_reset_beacon_ring(struct rtwn_softc *, int);
80 static void	rtwn_pci_reset_tx_list(struct rtwn_softc *,
81 		    struct ieee80211vap *, int);
82 static void	rtwn_pci_free_tx_list(struct rtwn_softc *, int);
83 static void	rtwn_pci_reset_lists(struct rtwn_softc *,
84 		    struct ieee80211vap *);
85 static int	rtwn_pci_fw_write_block(struct rtwn_softc *,
86 		    const uint8_t *, uint16_t, int);
87 static uint16_t	rtwn_pci_get_qmap(struct rtwn_softc *);
88 static void	rtwn_pci_set_desc_addr(struct rtwn_softc *);
89 static void	rtwn_pci_beacon_update_begin(struct rtwn_softc *,
90 		    struct ieee80211vap *);
91 static void	rtwn_pci_beacon_update_end(struct rtwn_softc *,
92 		    struct ieee80211vap *);
93 static void	rtwn_pci_attach_methods(struct rtwn_softc *);
94 
95 
96 static const struct rtwn_pci_ident *
97 rtwn_pci_probe_sub(device_t dev)
98 {
99 	const struct rtwn_pci_ident *ident;
100 	int vendor_id, device_id;
101 
102 	vendor_id = pci_get_vendor(dev);
103 	device_id = pci_get_device(dev);
104 
105 	for (ident = rtwn_pci_ident_table; ident->name != NULL; ident++)
106 		if (vendor_id == ident->vendor && device_id == ident->device)
107 			return (ident);
108 
109 	return (NULL);
110 }
111 
112 static int
113 rtwn_pci_probe(device_t dev)
114 {
115 	const struct rtwn_pci_ident *ident;
116 
117 	ident = rtwn_pci_probe_sub(dev);
118 	if (ident != NULL) {
119 		device_set_desc(dev, ident->name);
120 		return (BUS_PROBE_DEFAULT);
121 	}
122 	return (ENXIO);
123 }
124 
125 static int
126 rtwn_pci_alloc_rx_list(struct rtwn_softc *sc)
127 {
128 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
129 	struct rtwn_rx_ring *rx_ring = &pc->rx_ring;
130 	struct rtwn_rx_data *rx_data;
131 	bus_size_t size;
132 	int i, error;
133 
134 	/* Allocate Rx descriptors. */
135 	size = sizeof(struct rtwn_rx_stat_pci) * RTWN_PCI_RX_LIST_COUNT;
136 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
137 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
138 	    size, 1, size, 0, NULL, NULL, &rx_ring->desc_dmat);
139 	if (error != 0) {
140 		device_printf(sc->sc_dev, "could not create rx desc DMA tag\n");
141 		goto fail;
142 	}
143 
144 	error = bus_dmamem_alloc(rx_ring->desc_dmat, (void **)&rx_ring->desc,
145 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT,
146 	    &rx_ring->desc_map);
147 	if (error != 0) {
148 		device_printf(sc->sc_dev, "could not allocate rx desc\n");
149 		goto fail;
150 	}
151 	error = bus_dmamap_load(rx_ring->desc_dmat, rx_ring->desc_map,
152 	    rx_ring->desc, size, rtwn_pci_dma_map_addr, &rx_ring->paddr, 0);
153 	if (error != 0) {
154 		device_printf(sc->sc_dev, "could not load rx desc DMA map\n");
155 		goto fail;
156 	}
157 	bus_dmamap_sync(rx_ring->desc_dmat, rx_ring->desc_map,
158 	    BUS_DMASYNC_PREWRITE);
159 
160 	/* Create RX buffer DMA tag. */
161 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
162 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
163 	    MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL, &rx_ring->data_dmat);
164 	if (error != 0) {
165 		device_printf(sc->sc_dev, "could not create rx buf DMA tag\n");
166 		goto fail;
167 	}
168 
169 	/* Allocate Rx buffers. */
170 	for (i = 0; i < RTWN_PCI_RX_LIST_COUNT; i++) {
171 		rx_data = &rx_ring->rx_data[i];
172 		error = bus_dmamap_create(rx_ring->data_dmat, 0, &rx_data->map);
173 		if (error != 0) {
174 			device_printf(sc->sc_dev,
175 			    "could not create rx buf DMA map\n");
176 			goto fail;
177 		}
178 
179 		rx_data->m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
180 		    MJUMPAGESIZE);
181 		if (rx_data->m == NULL) {
182 			device_printf(sc->sc_dev,
183 			    "could not allocate rx mbuf\n");
184 			error = ENOMEM;
185 			goto fail;
186 		}
187 
188 		error = bus_dmamap_load(rx_ring->data_dmat, rx_data->map,
189 		    mtod(rx_data->m, void *), MJUMPAGESIZE,
190 		    rtwn_pci_dma_map_addr, &rx_data->paddr, BUS_DMA_NOWAIT);
191 		if (error != 0) {
192 			device_printf(sc->sc_dev,
193 			    "could not load rx buf DMA map");
194 			goto fail;
195 		}
196 
197 		rtwn_pci_setup_rx_desc(pc, &rx_ring->desc[i], rx_data->paddr,
198 		    MJUMPAGESIZE, i);
199 	}
200 	rx_ring->cur = 0;
201 
202 	return (0);
203 
204 fail:
205 	rtwn_pci_free_rx_list(sc);
206 	return (error);
207 }
208 
209 static void
210 rtwn_pci_reset_rx_list(struct rtwn_softc *sc)
211 {
212 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
213 	struct rtwn_rx_ring *rx_ring = &pc->rx_ring;
214 	struct rtwn_rx_data *rx_data;
215 	int i;
216 
217 	for (i = 0; i < RTWN_PCI_RX_LIST_COUNT; i++) {
218 		rx_data = &rx_ring->rx_data[i];
219 		rtwn_pci_setup_rx_desc(pc, &rx_ring->desc[i],
220 		    rx_data->paddr, MJUMPAGESIZE, i);
221 	}
222 	rx_ring->cur = 0;
223 }
224 
225 static void
226 rtwn_pci_free_rx_list(struct rtwn_softc *sc)
227 {
228 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
229 	struct rtwn_rx_ring *rx_ring = &pc->rx_ring;
230 	struct rtwn_rx_data *rx_data;
231 	int i;
232 
233 	if (rx_ring->desc_dmat != NULL) {
234 		if (rx_ring->desc != NULL) {
235 			bus_dmamap_sync(rx_ring->desc_dmat,
236 			    rx_ring->desc_map,
237 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
238 			bus_dmamap_unload(rx_ring->desc_dmat,
239 			    rx_ring->desc_map);
240 			bus_dmamem_free(rx_ring->desc_dmat, rx_ring->desc,
241 			    rx_ring->desc_map);
242 			rx_ring->desc = NULL;
243 		}
244 		bus_dma_tag_destroy(rx_ring->desc_dmat);
245 		rx_ring->desc_dmat = NULL;
246 	}
247 
248 	for (i = 0; i < RTWN_PCI_RX_LIST_COUNT; i++) {
249 		rx_data = &rx_ring->rx_data[i];
250 
251 		if (rx_data->m != NULL) {
252 			bus_dmamap_sync(rx_ring->data_dmat,
253 			    rx_data->map, BUS_DMASYNC_POSTREAD);
254 			bus_dmamap_unload(rx_ring->data_dmat, rx_data->map);
255 			m_freem(rx_data->m);
256 			rx_data->m = NULL;
257 		}
258 		bus_dmamap_destroy(rx_ring->data_dmat, rx_data->map);
259 		rx_data->map = NULL;
260 	}
261 	if (rx_ring->data_dmat != NULL) {
262 		bus_dma_tag_destroy(rx_ring->data_dmat);
263 		rx_ring->data_dmat = NULL;
264 	}
265 }
266 
267 static int
268 rtwn_pci_alloc_tx_list(struct rtwn_softc *sc, int qid)
269 {
270 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
271 	struct rtwn_tx_ring *tx_ring = &pc->tx_ring[qid];
272 	bus_size_t size;
273 	int i, error;
274 
275 	size = sc->txdesc_len * RTWN_PCI_TX_LIST_COUNT;
276 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), PAGE_SIZE, 0,
277 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
278 	    size, 1, size, 0, NULL, NULL, &tx_ring->desc_dmat);
279 	if (error != 0) {
280 		device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
281 		goto fail;
282 	}
283 
284 	error = bus_dmamem_alloc(tx_ring->desc_dmat, &tx_ring->desc,
285 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &tx_ring->desc_map);
286 	if (error != 0) {
287 		device_printf(sc->sc_dev, "can't map tx ring DMA memory\n");
288 		goto fail;
289 	}
290 	error = bus_dmamap_load(tx_ring->desc_dmat, tx_ring->desc_map,
291 	    tx_ring->desc, size, rtwn_pci_dma_map_addr, &tx_ring->paddr,
292 	    BUS_DMA_NOWAIT);
293 	if (error != 0) {
294 		device_printf(sc->sc_dev, "could not load desc DMA map\n");
295 		goto fail;
296 	}
297 	bus_dmamap_sync(tx_ring->desc_dmat, tx_ring->desc_map,
298 	    BUS_DMASYNC_PREWRITE);
299 
300 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
301 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
302 	    MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL, &tx_ring->data_dmat);
303 	if (error != 0) {
304 		device_printf(sc->sc_dev, "could not create tx buf DMA tag\n");
305 		goto fail;
306 	}
307 
308 	for (i = 0; i < RTWN_PCI_TX_LIST_COUNT; i++) {
309 		struct rtwn_tx_data *tx_data = &tx_ring->tx_data[i];
310 		void *tx_desc = (uint8_t *)tx_ring->desc + sc->txdesc_len * i;
311 		uint32_t next_desc_addr = tx_ring->paddr +
312 		    sc->txdesc_len * ((i + 1) % RTWN_PCI_TX_LIST_COUNT);
313 
314 		rtwn_pci_setup_tx_desc(pc, tx_desc, next_desc_addr);
315 
316 		error = bus_dmamap_create(tx_ring->data_dmat, 0, &tx_data->map);
317 		if (error != 0) {
318 			device_printf(sc->sc_dev,
319 			    "could not create tx buf DMA map\n");
320 			return (error);
321 		}
322 		tx_data->m = NULL;
323 		tx_data->ni = NULL;
324 	}
325 	return (0);
326 
327 fail:
328 	rtwn_pci_free_tx_list(sc, qid);
329 	return (error);
330 }
331 
332 static void
333 rtwn_pci_reset_tx_ring_stopped(struct rtwn_softc *sc, int qid)
334 {
335 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
336 	struct rtwn_tx_ring *ring = &pc->tx_ring[qid];
337 	int i;
338 
339 	for (i = 0; i < RTWN_PCI_TX_LIST_COUNT; i++) {
340 		struct rtwn_tx_data *data = &ring->tx_data[i];
341 		void *desc = (uint8_t *)ring->desc + sc->txdesc_len * i;
342 
343 		rtwn_pci_copy_tx_desc(pc, desc, NULL);
344 
345 		if (data->m != NULL) {
346 			bus_dmamap_sync(ring->data_dmat, data->map,
347 			    BUS_DMASYNC_POSTWRITE);
348 			bus_dmamap_unload(ring->data_dmat, data->map);
349 			m_freem(data->m);
350 			data->m = NULL;
351 		}
352 		if (data->ni != NULL) {
353 			ieee80211_free_node(data->ni);
354 			data->ni = NULL;
355 		}
356 	}
357 
358 	bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
359 	    BUS_DMASYNC_POSTWRITE);
360 
361 	sc->qfullmsk &= ~(1 << qid);
362 	ring->queued = 0;
363 	ring->last = ring->cur = 0;
364 }
365 
366 /*
367  * Clear entry 0 (or 1) in the beacon queue (other are not used).
368  */
369 static void
370 rtwn_pci_reset_beacon_ring(struct rtwn_softc *sc, int id)
371 {
372 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
373 	struct rtwn_tx_ring *ring = &pc->tx_ring[RTWN_PCI_BEACON_QUEUE];
374 	struct rtwn_tx_data *data = &ring->tx_data[id];
375 	struct rtwn_tx_desc_common *txd = (struct rtwn_tx_desc_common *)
376 	    ((uint8_t *)ring->desc + id * sc->txdesc_len);
377 
378 	bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_POSTREAD);
379 	if (txd->flags0 & RTWN_FLAGS0_OWN) {
380 		/* Clear OWN bit. */
381 		txd->flags0 &= ~RTWN_FLAGS0_OWN;
382 		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
383 		    BUS_DMASYNC_PREWRITE);
384 
385 		/* Unload mbuf. */
386 		bus_dmamap_sync(ring->data_dmat, data->map,
387 		    BUS_DMASYNC_POSTWRITE);
388 		bus_dmamap_unload(ring->data_dmat, data->map);
389 	}
390 }
391 
392 /*
393  * Drop stale entries from Tx ring before the vap will be deleted.
394  * In case if vap is NULL just free everything and reset cur / last pointers.
395  */
396 static void
397 rtwn_pci_reset_tx_list(struct rtwn_softc *sc, struct ieee80211vap *vap,
398     int qid)
399 {
400 	int i;
401 
402 	if (vap == NULL) {
403 		if (qid != RTWN_PCI_BEACON_QUEUE) {
404 			/*
405 			 * Device was stopped; just clear all entries.
406 			 */
407 			rtwn_pci_reset_tx_ring_stopped(sc, qid);
408 		} else {
409 			for (i = 0; i < RTWN_PORT_COUNT; i++)
410 				rtwn_pci_reset_beacon_ring(sc, i);
411 		}
412 	} else if (qid == RTWN_PCI_BEACON_QUEUE &&
413 		   (vap->iv_opmode == IEEE80211_M_HOSTAP ||
414 		    vap->iv_opmode == IEEE80211_M_IBSS)) {
415 		struct rtwn_vap *uvp = RTWN_VAP(vap);
416 
417 		rtwn_pci_reset_beacon_ring(sc, uvp->id);
418 	} else {
419 		struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
420 		struct rtwn_tx_ring *ring = &pc->tx_ring[qid];
421 
422 		for (i = 0; i < RTWN_PCI_TX_LIST_COUNT; i++) {
423 			struct rtwn_tx_data *data = &ring->tx_data[i];
424 			if (data->ni != NULL && data->ni->ni_vap == vap) {
425 				/*
426 				 * NB: if some vap is still running
427 				 * rtwn_pci_tx_done() will free the mbuf;
428 				 * otherwise, rtwn_stop() will reset all rings
429 				 * after device shutdown.
430 				 */
431 				ieee80211_free_node(data->ni);
432 				data->ni = NULL;
433 			}
434 		}
435 	}
436 }
437 
438 static void
439 rtwn_pci_free_tx_list(struct rtwn_softc *sc, int qid)
440 {
441 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
442 	struct rtwn_tx_ring *tx_ring = &pc->tx_ring[qid];
443 	struct rtwn_tx_data *tx_data;
444 	int i;
445 
446 	if (tx_ring->desc_dmat != NULL) {
447 		if (tx_ring->desc != NULL) {
448 			bus_dmamap_sync(tx_ring->desc_dmat,
449 			    tx_ring->desc_map, BUS_DMASYNC_POSTWRITE);
450 			bus_dmamap_unload(tx_ring->desc_dmat,
451 			    tx_ring->desc_map);
452 			bus_dmamem_free(tx_ring->desc_dmat, tx_ring->desc,
453 			    tx_ring->desc_map);
454 		}
455 		bus_dma_tag_destroy(tx_ring->desc_dmat);
456 	}
457 
458 	for (i = 0; i < RTWN_PCI_TX_LIST_COUNT; i++) {
459 		tx_data = &tx_ring->tx_data[i];
460 
461 		if (tx_data->m != NULL) {
462 			bus_dmamap_sync(tx_ring->data_dmat, tx_data->map,
463 			    BUS_DMASYNC_POSTWRITE);
464 			bus_dmamap_unload(tx_ring->data_dmat, tx_data->map);
465 			m_freem(tx_data->m);
466 			tx_data->m = NULL;
467 		}
468 	}
469 	if (tx_ring->data_dmat != NULL) {
470 		bus_dma_tag_destroy(tx_ring->data_dmat);
471 		tx_ring->data_dmat = NULL;
472 	}
473 
474 	sc->qfullmsk &= ~(1 << qid);
475 	tx_ring->queued = 0;
476 	tx_ring->last = tx_ring->cur = 0;
477 }
478 
479 static void
480 rtwn_pci_reset_lists(struct rtwn_softc *sc, struct ieee80211vap *vap)
481 {
482 	int i;
483 
484 	for (i = 0; i < RTWN_PCI_NTXQUEUES; i++)
485 		rtwn_pci_reset_tx_list(sc, vap, i);
486 
487 	if (vap == NULL) {
488 		sc->qfullmsk = 0;
489 		rtwn_pci_reset_rx_list(sc);
490 	}
491 }
492 
493 static int
494 rtwn_pci_fw_write_block(struct rtwn_softc *sc, const uint8_t *buf,
495     uint16_t reg, int mlen)
496 {
497 	int i;
498 
499 	for (i = 0; i < mlen; i++)
500 		rtwn_pci_write_1(sc, reg++, buf[i]);
501 
502 	/* NB: cannot fail */
503 	return (0);
504 }
505 
506 static uint16_t
507 rtwn_pci_get_qmap(struct rtwn_softc *sc)
508 {
509 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
510 
511 	KASSERT(pc->pc_qmap != 0, ("%s: qmap is not set!\n", __func__));
512 
513 	return (pc->pc_qmap);
514 }
515 
516 static void
517 rtwn_pci_set_desc_addr(struct rtwn_softc *sc)
518 {
519 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
520 
521 	RTWN_DPRINTF(sc, RTWN_DEBUG_RESET, "%s: addresses:\n"
522 	    "bk: %08jX, be: %08jX, vi: %08jX, vo: %08jX\n"
523 	    "bcn: %08jX, mgt: %08jX, high: %08jX, rx: %08jX\n",
524 	    __func__, (uintmax_t)pc->tx_ring[RTWN_PCI_BK_QUEUE].paddr,
525 	    (uintmax_t)pc->tx_ring[RTWN_PCI_BE_QUEUE].paddr,
526 	    (uintmax_t)pc->tx_ring[RTWN_PCI_VI_QUEUE].paddr,
527 	    (uintmax_t)pc->tx_ring[RTWN_PCI_VO_QUEUE].paddr,
528 	    (uintmax_t)pc->tx_ring[RTWN_PCI_BEACON_QUEUE].paddr,
529 	    (uintmax_t)pc->tx_ring[RTWN_PCI_MGNT_QUEUE].paddr,
530 	    (uintmax_t)pc->tx_ring[RTWN_PCI_HIGH_QUEUE].paddr,
531 	    (uintmax_t)pc->rx_ring.paddr);
532 
533 	/* Set Tx Configuration Register. */
534 	rtwn_pci_write_4(sc, R92C_TCR, pc->tcr);
535 
536 	/* Configure Tx DMA. */
537 	rtwn_pci_write_4(sc, R92C_BKQ_DESA,
538 	    pc->tx_ring[RTWN_PCI_BK_QUEUE].paddr);
539 	rtwn_pci_write_4(sc, R92C_BEQ_DESA,
540 	    pc->tx_ring[RTWN_PCI_BE_QUEUE].paddr);
541 	rtwn_pci_write_4(sc, R92C_VIQ_DESA,
542 	    pc->tx_ring[RTWN_PCI_VI_QUEUE].paddr);
543 	rtwn_pci_write_4(sc, R92C_VOQ_DESA,
544 	    pc->tx_ring[RTWN_PCI_VO_QUEUE].paddr);
545 	rtwn_pci_write_4(sc, R92C_BCNQ_DESA,
546 	    pc->tx_ring[RTWN_PCI_BEACON_QUEUE].paddr);
547 	rtwn_pci_write_4(sc, R92C_MGQ_DESA,
548 	    pc->tx_ring[RTWN_PCI_MGNT_QUEUE].paddr);
549 	rtwn_pci_write_4(sc, R92C_HQ_DESA,
550 	    pc->tx_ring[RTWN_PCI_HIGH_QUEUE].paddr);
551 
552 	/* Configure Rx DMA. */
553 	rtwn_pci_write_4(sc, R92C_RX_DESA, pc->rx_ring.paddr);
554 }
555 
556 static void
557 rtwn_pci_beacon_update_begin(struct rtwn_softc *sc, struct ieee80211vap *vap)
558 {
559 	struct rtwn_vap *rvp = RTWN_VAP(vap);
560 
561 	RTWN_ASSERT_LOCKED(sc);
562 
563 	rtwn_beacon_enable(sc, rvp->id, 0);
564 }
565 
566 static void
567 rtwn_pci_beacon_update_end(struct rtwn_softc *sc, struct ieee80211vap *vap)
568 {
569 	struct rtwn_vap *rvp = RTWN_VAP(vap);
570 
571 	RTWN_ASSERT_LOCKED(sc);
572 
573 	if (rvp->curr_mode != R92C_MSR_NOLINK)
574 		rtwn_beacon_enable(sc, rvp->id, 1);
575 }
576 
577 static void
578 rtwn_pci_attach_methods(struct rtwn_softc *sc)
579 {
580 	sc->sc_write_1		= rtwn_pci_write_1;
581 	sc->sc_write_2		= rtwn_pci_write_2;
582 	sc->sc_write_4		= rtwn_pci_write_4;
583 	sc->sc_read_1		= rtwn_pci_read_1;
584 	sc->sc_read_2		= rtwn_pci_read_2;
585 	sc->sc_read_4		= rtwn_pci_read_4;
586 	sc->sc_delay		= rtwn_pci_delay;
587 	sc->sc_tx_start		= rtwn_pci_tx_start;
588 	sc->sc_reset_lists	= rtwn_pci_reset_lists;
589 	sc->sc_abort_xfers	= rtwn_nop_softc;
590 	sc->sc_fw_write_block	= rtwn_pci_fw_write_block;
591 	sc->sc_get_qmap		= rtwn_pci_get_qmap;
592 	sc->sc_set_desc_addr	= rtwn_pci_set_desc_addr;
593 	sc->sc_drop_incorrect_tx = rtwn_nop_softc;
594 	sc->sc_beacon_update_begin = rtwn_pci_beacon_update_begin;
595 	sc->sc_beacon_update_end = rtwn_pci_beacon_update_end;
596 	sc->sc_beacon_unload	= rtwn_pci_reset_beacon_ring;
597 
598 	sc->bcn_check_interval	= 25000;
599 }
600 
601 static int
602 rtwn_pci_attach(device_t dev)
603 {
604 	const struct rtwn_pci_ident *ident;
605 	struct rtwn_pci_softc *pc = device_get_softc(dev);
606 	struct rtwn_softc *sc = &pc->pc_sc;
607 	struct ieee80211com *ic = &sc->sc_ic;
608 	uint32_t lcsr;
609 	int cap_off, i, error, rid;
610 
611 	ident = rtwn_pci_probe_sub(dev);
612 	if (ident == NULL)
613 		return (ENXIO);
614 
615 	/*
616 	 * Get the offset of the PCI Express Capability Structure in PCI
617 	 * Configuration Space.
618 	 */
619 	error = pci_find_cap(dev, PCIY_EXPRESS, &cap_off);
620 	if (error != 0) {
621 		device_printf(dev, "PCIe capability structure not found!\n");
622 		return (error);
623 	}
624 
625 	/* Enable bus-mastering. */
626 	pci_enable_busmaster(dev);
627 
628 	rid = PCIR_BAR(2);
629 	pc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
630 	    RF_ACTIVE);
631 	if (pc->mem == NULL) {
632 		device_printf(dev, "can't map mem space\n");
633 		return (ENOMEM);
634 	}
635 	pc->pc_st = rman_get_bustag(pc->mem);
636 	pc->pc_sh = rman_get_bushandle(pc->mem);
637 
638 	/* Install interrupt handler. */
639 	rid = 1;
640 	if (pci_alloc_msi(dev, &rid) == 0)
641 		rid = 1;
642 	else
643 		rid = 0;
644 	pc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
645 	    (rid != 0 ? 0 : RF_SHAREABLE));
646 	if (pc->irq == NULL) {
647 		device_printf(dev, "can't map interrupt\n");
648 		goto detach;
649 	}
650 
651 	/* Disable PCIe Active State Power Management (ASPM). */
652 	lcsr = pci_read_config(dev, cap_off + PCIER_LINK_CTL, 4);
653 	lcsr &= ~PCIEM_LINK_CTL_ASPMC;
654 	pci_write_config(dev, cap_off + PCIER_LINK_CTL, lcsr, 4);
655 
656 	sc->sc_dev = dev;
657 	ic->ic_name = device_get_nameunit(dev);
658 
659 	/* Need to be initialized early. */
660 	rtwn_sysctlattach(sc);
661 	mtx_init(&sc->sc_mtx, ic->ic_name, MTX_NETWORK_LOCK, MTX_DEF);
662 
663 	rtwn_pci_attach_methods(sc);
664 	rtwn_pci_attach_private(pc, ident->chip);
665 
666 	/* Allocate Tx/Rx buffers. */
667 	error = rtwn_pci_alloc_rx_list(sc);
668 	if (error != 0) {
669 		device_printf(dev,
670 		    "could not allocate Rx buffers, error %d\n",
671 		    error);
672 		goto detach;
673 	}
674 	for (i = 0; i < RTWN_PCI_NTXQUEUES; i++) {
675 		error = rtwn_pci_alloc_tx_list(sc, i);
676 		if (error != 0) {
677 			device_printf(dev,
678 			    "could not allocate Tx buffers, error %d\n",
679 			    error);
680 			goto detach;
681 		}
682 	}
683 
684 	/* Generic attach. */
685 	error = rtwn_attach(sc);
686 	if (error != 0)
687 		goto detach;
688 
689 	/*
690 	 * Hook our interrupt after all initialization is complete.
691 	 */
692 	error = bus_setup_intr(dev, pc->irq, INTR_TYPE_NET | INTR_MPSAFE,
693 	    NULL, rtwn_pci_intr, sc, &pc->pc_ih);
694 	if (error != 0) {
695 		device_printf(dev, "can't establish interrupt, error %d\n",
696 		    error);
697 		goto detach;
698 	}
699 
700 	return (0);
701 
702 detach:
703 	rtwn_pci_detach(dev);		/* failure */
704 	return (ENXIO);
705 }
706 
707 static int
708 rtwn_pci_detach(device_t dev)
709 {
710 	struct rtwn_pci_softc *pc = device_get_softc(dev);
711 	struct rtwn_softc *sc = &pc->pc_sc;
712 	int i;
713 
714 	/* Generic detach. */
715 	rtwn_detach(sc);
716 
717 	/* Uninstall interrupt handler. */
718 	if (pc->irq != NULL) {
719 		bus_teardown_intr(dev, pc->irq, pc->pc_ih);
720 		bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(pc->irq),
721 		    pc->irq);
722 		pci_release_msi(dev);
723 	}
724 
725 	/* Free Tx/Rx buffers. */
726 	for (i = 0; i < RTWN_PCI_NTXQUEUES; i++)
727 		rtwn_pci_free_tx_list(sc, i);
728 	rtwn_pci_free_rx_list(sc);
729 
730 	if (pc->mem != NULL)
731 		bus_release_resource(dev, SYS_RES_MEMORY,
732 		    rman_get_rid(pc->mem), pc->mem);
733 
734 	rtwn_detach_private(sc);
735 	mtx_destroy(&sc->sc_mtx);
736 
737 	return (0);
738 }
739 
740 static int
741 rtwn_pci_shutdown(device_t self)
742 {
743 	struct rtwn_pci_softc *pc = device_get_softc(self);
744 
745 	ieee80211_stop_all(&pc->pc_sc.sc_ic);
746 	return (0);
747 }
748 
749 static int
750 rtwn_pci_suspend(device_t self)
751 {
752 	struct rtwn_pci_softc *pc = device_get_softc(self);
753 
754 	rtwn_suspend(&pc->pc_sc);
755 
756 	return (0);
757 }
758 
759 static int
760 rtwn_pci_resume(device_t self)
761 {
762 	struct rtwn_pci_softc *pc = device_get_softc(self);
763 
764 	rtwn_resume(&pc->pc_sc);
765 
766 	return (0);
767 }
768 
769 static device_method_t rtwn_pci_methods[] = {
770 	/* Device interface */
771 	DEVMETHOD(device_probe,		rtwn_pci_probe),
772 	DEVMETHOD(device_attach,	rtwn_pci_attach),
773 	DEVMETHOD(device_detach,	rtwn_pci_detach),
774 	DEVMETHOD(device_shutdown,	rtwn_pci_shutdown),
775 	DEVMETHOD(device_suspend,	rtwn_pci_suspend),
776 	DEVMETHOD(device_resume,	rtwn_pci_resume),
777 
778 	DEVMETHOD_END
779 };
780 
781 static driver_t rtwn_pci_driver = {
782 	"rtwn",
783 	rtwn_pci_methods,
784 	sizeof(struct rtwn_pci_softc)
785 };
786 
787 static devclass_t rtwn_pci_devclass;
788 
789 DRIVER_MODULE(rtwn_pci, pci, rtwn_pci_driver, rtwn_pci_devclass, NULL, NULL);
790 MODULE_VERSION(rtwn_pci, 1);
791 MODULE_DEPEND(rtwn_pci, pci, 1, 1, 1);
792 MODULE_DEPEND(rtwn_pci, wlan, 1, 1, 1);
793 MODULE_DEPEND(rtwn_pci, rtwn, 2, 2, 2);
794