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