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