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