/*-1* Copyright (c) 2015-2016 Landon Fuller <[email protected]>2* Copyright (c) 2017 The FreeBSD Foundation3* All rights reserved.4*5* Portions of this software were developed by Landon Fuller6* under sponsorship from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer,13* without modification.14* 2. Redistributions in binary form must reproduce at minimum a disclaimer15* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any16* redistribution must be conditioned upon including a substantially17* similar Disclaimer requirement for further binary redistribution.18*19* NO WARRANTY20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY23* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL24* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,25* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER28* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF30* THE POSSIBILITY OF SUCH DAMAGES.31*/3233#include <sys/param.h>34#include <sys/kernel.h>35#include <sys/limits.h>3637#include "bhndb_private.h"38#include "bhndbvar.h"3940static int bhndb_dma_tag_create(device_t dev, bus_dma_tag_t parent_dmat,41const struct bhnd_dma_translation *translation,42bus_dma_tag_t *dmat);4344/**45* Attach a BHND bridge device to @p parent.46*47* @param parent A parent PCI device.48* @param[out] bhndb On success, the probed and attached bhndb bridge device.49* @param unit The device unit number, or -1 to select the next available unit50* number.51*52* @retval 0 success53* @retval non-zero Failed to attach the bhndb device.54*/55int56bhndb_attach_bridge(device_t parent, device_t *bhndb, int unit)57{58int error;5960*bhndb = device_add_child(parent, "bhndb", unit);61if (*bhndb == NULL)62return (ENXIO);6364if (!(error = device_probe_and_attach(*bhndb)))65return (0);6667if ((device_delete_child(parent, *bhndb)))68device_printf(parent, "failed to detach bhndb child\n");6970return (error);71}7273/*74* Call BHNDB_SUSPEND_RESOURCE() for all resources in @p rl.75*/76static void77bhndb_do_suspend_resources(device_t dev, struct resource_list *rl)78{79struct resource_list_entry *rle;8081/* Suspend all child resources. */82STAILQ_FOREACH(rle, rl, link) {83/* Skip non-allocated resources */84if (rle->res == NULL)85continue;8687BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev, rle->type,88rle->res);89}90}9192/**93* Helper function for implementing BUS_RESUME_CHILD() on bridged94* bhnd(4) buses.95*96* This implementation of BUS_RESUME_CHILD() uses BUS_GET_RESOURCE_LIST()97* to find the child's resources and call BHNDB_SUSPEND_RESOURCE() for all98* child resources, ensuring that the device's allocated bridge resources99* will be available to other devices during bus resumption.100*101* Before suspending any resources, @p child is suspended by102* calling bhnd_generic_suspend_child().103*104* If @p child is not a direct child of @p dev, suspension is delegated to105* the @p dev parent.106*/107int108bhnd_generic_br_suspend_child(device_t dev, device_t child)109{110struct resource_list *rl;111int error;112113if (device_get_parent(child) != dev)114BUS_SUSPEND_CHILD(device_get_parent(dev), child);115116if (device_is_suspended(child))117return (EBUSY);118119/* Suspend the child device */120if ((error = bhnd_generic_suspend_child(dev, child)))121return (error);122123/* Fetch the resource list. If none, there's nothing else to do */124rl = BUS_GET_RESOURCE_LIST(device_get_parent(child), child);125if (rl == NULL)126return (0);127128/* Suspend all child resources. */129bhndb_do_suspend_resources(dev, rl);130131return (0);132}133134/**135* Helper function for implementing BUS_RESUME_CHILD() on bridged136* bhnd(4) bus devices.137*138* This implementation of BUS_RESUME_CHILD() uses BUS_GET_RESOURCE_LIST()139* to find the child's resources and call BHNDB_RESUME_RESOURCE() for all140* child resources, before delegating to bhnd_generic_resume_child().141*142* If resource resumption fails, @p child will not be resumed.143*144* If @p child is not a direct child of @p dev, suspension is delegated to145* the @p dev parent.146*/147int148bhnd_generic_br_resume_child(device_t dev, device_t child)149{150struct resource_list *rl;151struct resource_list_entry *rle;152int error;153154if (device_get_parent(child) != dev)155BUS_RESUME_CHILD(device_get_parent(dev), child);156157if (!device_is_suspended(child))158return (EBUSY);159160/* Fetch the resource list. If none, there's nothing else to do */161rl = BUS_GET_RESOURCE_LIST(device_get_parent(child), child);162if (rl == NULL)163return (bhnd_generic_resume_child(dev, child));164165/* Resume all resources */166STAILQ_FOREACH(rle, rl, link) {167/* Skip non-allocated resources */168if (rle->res == NULL)169continue;170171error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,172rle->type, rle->res);173if (error) {174/* Put all resources back into a suspend state */175bhndb_do_suspend_resources(dev, rl);176return (error);177}178}179180/* Now that all resources are resumed, resume child */181if ((error = bhnd_generic_resume_child(dev, child))) {182/* Put all resources back into a suspend state */183bhndb_do_suspend_resources(dev, rl);184}185186return (error);187}188189/**190* Find a host resource of @p type that maps the given range.191*192* @param hr The resource state to search.193* @param type The resource type to search for (see SYS_RES_*).194* @param start The start address of the range to search for.195* @param count The size of the range to search for.196*197* @retval resource the host resource containing the requested range.198* @retval NULL if no resource containing the requested range can be found.199*/200struct resource *201bhndb_host_resource_for_range(struct bhndb_host_resources *hr, int type,202rman_res_t start, rman_res_t count)203{204for (u_int i = 0; hr->resource_specs[i].type != -1; i++) {205struct resource *r = hr->resources[i];206207if (hr->resource_specs[i].type != type)208continue;209210/* Verify range */211if (rman_get_start(r) > start)212continue;213214if (rman_get_end(r) < (start + count - 1))215continue;216217return (r);218}219220return (NULL);221}222223/**224* Find a host resource of that matches the given register window definition.225*226* @param hr The resource state to search.227* @param win A register window definition.228*229* @retval resource the host resource corresponding to @p win.230* @retval NULL if no resource corresponding to @p win can be found.231*/232struct resource *233bhndb_host_resource_for_regwin(struct bhndb_host_resources *hr,234const struct bhndb_regwin *win)235{236const struct resource_spec *rspecs;237238rspecs = hr->resource_specs;239for (u_int i = 0; rspecs[i].type != -1; i++) {240if (win->res.type != rspecs[i].type)241continue;242243if (win->res.rid != rspecs[i].rid)244continue;245246/* Found declared resource */247return (hr->resources[i]);248}249250device_printf(hr->owner, "missing regwin resource spec "251"(type=%d, rid=%d)\n", win->res.type, win->res.rid);252253return (NULL);254}255256/**257* Allocate and initialize a new resource state structure.258*259* @param dev The bridge device.260* @param parent_dev The parent device from which host resources should be261* allocated.262* @param cfg The hardware configuration to be used.263*/264struct bhndb_resources *265bhndb_alloc_resources(device_t dev, device_t parent_dev,266const struct bhndb_hwcfg *cfg)267{268struct bhndb_resources *r;269const struct bhndb_regwin *win;270bus_size_t last_window_size;271int rnid;272int error;273bool free_ht_mem, free_br_mem, free_br_irq;274275free_ht_mem = false;276free_br_mem = false;277free_br_irq = false;278279r = malloc(sizeof(*r), M_BHND, M_NOWAIT|M_ZERO);280if (r == NULL)281return (NULL);282283/* Basic initialization */284r->dev = dev;285r->cfg = cfg;286r->res = NULL;287r->min_prio = BHNDB_PRIORITY_NONE;288STAILQ_INIT(&r->bus_regions);289STAILQ_INIT(&r->bus_intrs);290291mtx_init(&r->dw_steal_mtx, device_get_nameunit(dev),292"bhndb dwa_steal lock", MTX_SPIN);293294/* Initialize host address space resource manager. */295r->ht_mem_rman.rm_start = 0;296r->ht_mem_rman.rm_end = ~0;297r->ht_mem_rman.rm_type = RMAN_ARRAY;298r->ht_mem_rman.rm_descr = "BHNDB host memory";299if ((error = rman_init(&r->ht_mem_rman))) {300device_printf(r->dev, "could not initialize ht_mem_rman\n");301goto failed;302}303free_ht_mem = true;304305/* Initialize resource manager for the bridged address space. */306r->br_mem_rman.rm_start = 0;307r->br_mem_rman.rm_end = BUS_SPACE_MAXADDR_32BIT;308r->br_mem_rman.rm_type = RMAN_ARRAY;309r->br_mem_rman.rm_descr = "BHNDB bridged memory";310311if ((error = rman_init(&r->br_mem_rman))) {312device_printf(r->dev, "could not initialize br_mem_rman\n");313goto failed;314}315free_br_mem = true;316317error = rman_manage_region(&r->br_mem_rman, 0, BUS_SPACE_MAXADDR_32BIT);318if (error) {319device_printf(r->dev, "could not configure br_mem_rman\n");320goto failed;321}322323/* Initialize resource manager for the bridged interrupt controller. */324r->br_irq_rman.rm_start = 0;325r->br_irq_rman.rm_end = RM_MAX_END;326r->br_irq_rman.rm_type = RMAN_ARRAY;327r->br_irq_rman.rm_descr = "BHNDB bridged interrupts";328329if ((error = rman_init(&r->br_irq_rman))) {330device_printf(r->dev, "could not initialize br_irq_rman\n");331goto failed;332}333free_br_irq = true;334335error = rman_manage_region(&r->br_irq_rman, 0, RM_MAX_END);336if (error) {337device_printf(r->dev, "could not configure br_irq_rman\n");338goto failed;339}340341/* Fetch the dynamic regwin count and verify that it does not exceed342* what is representable via our freelist bitstring. */343r->dwa_count = bhndb_regwin_count(cfg->register_windows,344BHNDB_REGWIN_T_DYN);345if (r->dwa_count >= INT_MAX) {346device_printf(r->dev, "max dynamic regwin count exceeded\n");347goto failed;348}349350/* Allocate the dynamic window allocation table. */351r->dw_alloc = malloc(sizeof(r->dw_alloc[0]) * r->dwa_count, M_BHND,352M_NOWAIT);353if (r->dw_alloc == NULL)354goto failed;355356/* Allocate the dynamic window allocation freelist */357r->dwa_freelist = bit_alloc(r->dwa_count, M_BHND, M_NOWAIT);358if (r->dwa_freelist == NULL)359goto failed;360361/* Initialize the dynamic window table */362rnid = 0;363last_window_size = 0;364for (win = cfg->register_windows;365win->win_type != BHNDB_REGWIN_T_INVALID; win++)366{367struct bhndb_dw_alloc *dwa;368369/* Skip non-DYN windows */370if (win->win_type != BHNDB_REGWIN_T_DYN)371continue;372373/* Validate the window size */374if (win->win_size == 0) {375device_printf(r->dev, "ignoring zero-length dynamic "376"register window\n");377continue;378} else if (last_window_size == 0) {379last_window_size = win->win_size;380} else if (last_window_size != win->win_size) {381/*382* No existing hardware should trigger this.383*384* If you run into this in the future, the dynamic385* window allocator and the resource priority system386* will need to be extended to support multiple register387* window allocation pools.388*/389device_printf(r->dev, "devices that vend multiple "390"dynamic register window sizes are not currently "391"supported\n");392goto failed;393}394395dwa = &r->dw_alloc[rnid];396dwa->win = win;397dwa->parent_res = NULL;398dwa->rnid = rnid;399dwa->target = 0x0;400401LIST_INIT(&dwa->refs);402rnid++;403}404405/* Allocate host resources */406error = bhndb_alloc_host_resources(&r->res, dev, parent_dev, r->cfg);407if (error) {408device_printf(r->dev,409"could not allocate host resources on %s: %d\n",410device_get_nameunit(parent_dev), error);411goto failed;412}413414/* Populate (and validate) parent resource references for all415* dynamic windows */416for (size_t i = 0; i < r->dwa_count; i++) {417struct bhndb_dw_alloc *dwa;418const struct bhndb_regwin *win;419420dwa = &r->dw_alloc[i];421win = dwa->win;422423/* Find and validate corresponding resource. */424dwa->parent_res = bhndb_host_resource_for_regwin(r->res, win);425if (dwa->parent_res == NULL) {426device_printf(r->dev, "no host resource found for %u "427"register window with offset %#jx and "428"size %#jx\n",429win->win_type,430(uintmax_t)win->win_offset,431(uintmax_t)win->win_size);432433error = ENXIO;434goto failed;435}436437if (rman_get_size(dwa->parent_res) < win->win_offset +438win->win_size)439{440device_printf(r->dev, "resource %d too small for "441"register window with offset %llx and size %llx\n",442rman_get_rid(dwa->parent_res),443(unsigned long long) win->win_offset,444(unsigned long long) win->win_size);445446error = EINVAL;447goto failed;448}449}450451/* Add allocated memory resources to our host memory resource manager */452for (u_int i = 0; r->res->resource_specs[i].type != -1; i++) {453struct resource *res;454455/* skip non-memory resources */456if (r->res->resource_specs[i].type != SYS_RES_MEMORY)457continue;458459/* add host resource to set of managed regions */460res = r->res->resources[i];461error = rman_manage_region(&r->ht_mem_rman,462rman_get_start(res), rman_get_end(res));463if (error) {464device_printf(r->dev,465"could not register host memory region with "466"ht_mem_rman: %d\n", error);467goto failed;468}469}470471return (r);472473failed:474if (free_ht_mem)475rman_fini(&r->ht_mem_rman);476477if (free_br_mem)478rman_fini(&r->br_mem_rman);479480if (free_br_irq)481rman_fini(&r->br_irq_rman);482483if (r->dw_alloc != NULL)484free(r->dw_alloc, M_BHND);485486if (r->dwa_freelist != NULL)487free(r->dwa_freelist, M_BHND);488489if (r->res != NULL)490bhndb_release_host_resources(r->res);491492mtx_destroy(&r->dw_steal_mtx);493494free(r, M_BHND);495496return (NULL);497}498499/**500* Create a new DMA tag for the given @p translation.501*502* @param dev The bridge device.503* @param parent_dmat The parent DMA tag, or NULL if none.504* @param translation The DMA translation for which a DMA tag will505* be created.506* @param[out] dmat On success, the newly created DMA tag.507*508* @retval 0 success509* @retval non-zero if creating the new DMA tag otherwise fails, a regular510* unix error code will be returned.511*/512static int513bhndb_dma_tag_create(device_t dev, bus_dma_tag_t parent_dmat,514const struct bhnd_dma_translation *translation, bus_dma_tag_t *dmat)515{516bus_dma_tag_t translation_tag;517bhnd_addr_t dt_mask;518bus_addr_t lowaddr, highaddr;519bus_size_t maxsegsz;520int error;521522highaddr = BUS_SPACE_MAXADDR;523maxsegsz = BUS_SPACE_MAXSIZE;524525/* Determine full addressable mask */526dt_mask = (translation->addr_mask | translation->addrext_mask);527KASSERT(dt_mask != 0, ("DMA addr_mask invalid: %#jx",528(uintmax_t)dt_mask));529530/* (addr_mask|addrext_mask) is our maximum supported address */531lowaddr = MIN(dt_mask, BUS_SPACE_MAXADDR);532533/* Constrain to translation window size */534if (translation->addr_mask < maxsegsz)535maxsegsz = translation->addr_mask;536537/* Create our DMA tag */538error = bus_dma_tag_create(parent_dmat,5391, 0, /* alignment, boundary */540lowaddr, highaddr,541NULL, NULL, /* filter, filterarg */542BUS_SPACE_MAXSIZE, 0, /* maxsize, nsegments */543maxsegsz, 0, /* maxsegsize, flags */544NULL, NULL, /* lockfunc, lockarg */545&translation_tag);546if (error) {547device_printf(dev, "failed to create bridge DMA tag: %d\n",548error);549return (error);550}551552*dmat = translation_tag;553return (0);554}555556/**557* Deallocate the given bridge resource structure and any associated resources.558*559* @param br Resource state to be deallocated.560*/561void562bhndb_free_resources(struct bhndb_resources *br)563{564struct bhndb_region *region, *r_next;565struct bhndb_dw_alloc *dwa;566struct bhndb_dw_rentry *dwr, *dwr_next;567struct bhndb_intr_handler *ih;568bool leaked_regions, leaked_intrs;569570leaked_regions = false;571leaked_intrs = false;572573/* No window regions may still be held */574if (!bhndb_dw_all_free(br)) {575for (int i = 0; i < br->dwa_count; i++) {576dwa = &br->dw_alloc[i];577578/* Skip free dynamic windows */579if (bhndb_dw_is_free(br, dwa))580continue;581582device_printf(br->dev,583"leaked dynamic register window %d\n", dwa->rnid);584leaked_regions = true;585}586}587588/* There should be no interrupt handlers still registered */589STAILQ_FOREACH(ih, &br->bus_intrs, ih_link) {590device_printf(br->dev, "interrupt handler leaked %p\n",591ih->ih_cookiep);592}593594if (leaked_intrs || leaked_regions) {595panic("leaked%s%s", leaked_intrs ? " active interrupts" : "",596leaked_regions ? " active register windows" : "");597}598599/* Release host resources allocated through our parent. */600if (br->res != NULL)601bhndb_release_host_resources(br->res);602603/* Clean up resource reservations */604for (size_t i = 0; i < br->dwa_count; i++) {605dwa = &br->dw_alloc[i];606607LIST_FOREACH_SAFE(dwr, &dwa->refs, dw_link, dwr_next) {608LIST_REMOVE(dwr, dw_link);609free(dwr, M_BHND);610}611}612613/* Release bus regions */614STAILQ_FOREACH_SAFE(region, &br->bus_regions, link, r_next) {615STAILQ_REMOVE(&br->bus_regions, region, bhndb_region, link);616free(region, M_BHND);617}618619/* Release our resource managers */620rman_fini(&br->ht_mem_rman);621rman_fini(&br->br_mem_rman);622rman_fini(&br->br_irq_rman);623624free(br->dw_alloc, M_BHND);625free(br->dwa_freelist, M_BHND);626627mtx_destroy(&br->dw_steal_mtx);628629free(br, M_BHND);630}631632/**633* Allocate host bus resources defined by @p hwcfg.634*635* On success, the caller assumes ownership of the allocated host resources,636* which must be freed via bhndb_release_host_resources().637*638* @param[out] resources On success, the allocated host resources.639* @param dev The bridge device.640* @param parent_dev The parent device from which host resources641* should be allocated (e.g. via642* bus_alloc_resources()).643* @param hwcfg The hardware configuration defining the host644* resources to be allocated645*/646int647bhndb_alloc_host_resources(struct bhndb_host_resources **resources,648device_t dev, device_t parent_dev, const struct bhndb_hwcfg *hwcfg)649{650struct bhndb_host_resources *hr;651const struct bhnd_dma_translation *dt;652bus_dma_tag_t parent_dmat;653size_t nres, ndt;654int error;655656parent_dmat = bus_get_dma_tag(parent_dev);657658hr = malloc(sizeof(*hr), M_BHND, M_WAITOK);659hr->owner = parent_dev;660hr->cfg = hwcfg;661hr->resource_specs = NULL;662hr->resources = NULL;663hr->dma_tags = NULL;664hr->num_dma_tags = 0;665666/* Determine our bridge resource count from the hardware config. */667nres = 0;668for (size_t i = 0; hwcfg->resource_specs[i].type != -1; i++)669nres++;670671/* Determine the total count and validate our DMA translation table. */672ndt = 0;673for (dt = hwcfg->dma_translations; dt != NULL &&674!BHND_DMA_IS_TRANSLATION_TABLE_END(dt); dt++)675{676/* Validate the defined translation */677if ((dt->base_addr & dt->addr_mask) != 0) {678device_printf(dev, "invalid DMA translation; base "679"address %#jx overlaps address mask %#jx",680(uintmax_t)dt->base_addr, (uintmax_t)dt->addr_mask);681682error = EINVAL;683goto failed;684}685686if ((dt->addrext_mask & dt->addr_mask) != 0) {687device_printf(dev, "invalid DMA translation; addrext "688"mask %#jx overlaps address mask %#jx",689(uintmax_t)dt->addrext_mask,690(uintmax_t)dt->addr_mask);691692error = EINVAL;693goto failed;694}695696/* Increment our entry count */697ndt++;698}699700/* Allocate our DMA tags */701hr->dma_tags = malloc(sizeof(*hr->dma_tags) * ndt, M_BHND,702M_WAITOK|M_ZERO);703for (size_t i = 0; i < ndt; i++) {704error = bhndb_dma_tag_create(dev, parent_dmat,705&hwcfg->dma_translations[i], &hr->dma_tags[i]);706if (error)707goto failed;708709hr->num_dma_tags++;710}711712/* Allocate space for a non-const copy of our resource_spec713* table; this will be updated with the RIDs assigned by714* bus_alloc_resources. */715hr->resource_specs = malloc(sizeof(hr->resource_specs[0]) * (nres + 1),716M_BHND, M_WAITOK);717718/* Initialize and terminate the table */719for (size_t i = 0; i < nres; i++)720hr->resource_specs[i] = hwcfg->resource_specs[i];721722hr->resource_specs[nres].type = -1;723724/* Allocate space for our resource references */725hr->resources = malloc(sizeof(hr->resources[0]) * nres, M_BHND,726M_WAITOK);727728/* Allocate host resources */729error = bus_alloc_resources(hr->owner, hr->resource_specs,730hr->resources);731if (error) {732device_printf(dev, "could not allocate bridge resources via "733"%s: %d\n", device_get_nameunit(parent_dev), error);734goto failed;735}736737*resources = hr;738return (0);739740failed:741if (hr->resource_specs != NULL)742free(hr->resource_specs, M_BHND);743744if (hr->resources != NULL)745free(hr->resources, M_BHND);746747for (size_t i = 0; i < hr->num_dma_tags; i++)748bus_dma_tag_destroy(hr->dma_tags[i]);749750if (hr->dma_tags != NULL)751free(hr->dma_tags, M_BHND);752753free(hr, M_BHND);754755return (error);756}757758/**759* Deallocate a set of bridge host resources.760*761* @param hr The resources to be freed.762*/763void764bhndb_release_host_resources(struct bhndb_host_resources *hr)765{766bus_release_resources(hr->owner, hr->resource_specs, hr->resources);767768for (size_t i = 0; i < hr->num_dma_tags; i++)769bus_dma_tag_destroy(hr->dma_tags[i]);770771free(hr->resources, M_BHND);772free(hr->resource_specs, M_BHND);773free(hr->dma_tags, M_BHND);774free(hr, M_BHND);775}776777/**778* Search @p cores for the core serving as the bhnd host bridge.779*780* This function uses a heuristic valid on all known PCI/PCIe/PCMCIA-bridged781* bhnd(4) devices to determine the hostb core:782*783* - The core must have a Broadcom vendor ID.784* - The core devclass must match the bridge type.785* - The core must be the first device on the bus with the bridged device786* class.787*788* @param cores The core table to search.789* @param ncores The number of cores in @p cores.790* @param bridge_devclass The expected device class of the bridge core.791* @param[out] core If found, the matching host bridge core info.792*793* @retval 0 success794* @retval ENOENT not found795*/796int797bhndb_find_hostb_core(struct bhnd_core_info *cores, u_int ncores,798bhnd_devclass_t bridge_devclass, struct bhnd_core_info *core)799{800struct bhnd_core_match md;801struct bhnd_core_info *match;802u_int match_core_idx;803804/* Set up a match descriptor for the required device class. */805md = (struct bhnd_core_match) {806BHND_MATCH_CORE_CLASS(bridge_devclass),807BHND_MATCH_CORE_UNIT(0)808};809810/* Find the matching core with the lowest core index */811match = NULL;812match_core_idx = UINT_MAX;813814for (u_int i = 0; i < ncores; i++) {815if (!bhnd_core_matches(&cores[i], &md))816continue;817818/* Lower core indices take precedence */819if (match != NULL && match_core_idx < match->core_idx)820continue;821822match = &cores[i];823match_core_idx = match->core_idx;824}825826if (match == NULL)827return (ENOENT);828829*core = *match;830return (0);831}832833/**834* Allocate a host interrupt source and its backing SYS_RES_IRQ host resource.835*836* @param owner The device to be used to allocate a SYS_RES_IRQ837* resource with @p rid.838* @param rid The resource ID of the IRQ to be allocated.839* @param start The start value to be passed to bus_alloc_resource().840* @param end The end value to be passed to bus_alloc_resource().841* @param count The count to be passed to bus_alloc_resource().842* @param flags The flags to be passed to bus_alloc_resource().843*844* @retval non-NULL success845* @retval NULL if allocation fails.846*/847struct bhndb_intr_isrc *848bhndb_alloc_intr_isrc(device_t owner, int rid, rman_res_t start, rman_res_t end,849rman_res_t count, u_int flags)850{851struct bhndb_intr_isrc *isrc;852853isrc = malloc(sizeof(*isrc), M_BHND, M_NOWAIT);854if (isrc == NULL)855return (NULL);856857isrc->is_owner = owner;858isrc->is_rid = rid;859isrc->is_res = bus_alloc_resource(owner, SYS_RES_IRQ, &isrc->is_rid,860start, end, count, flags);861if (isrc->is_res == NULL) {862free(isrc, M_BHND);863return (NULL);864}865866return (isrc);867}868869/**870* Free a host interrupt source and its backing host resource.871*872* @param isrc The interrupt source to be freed.873*/874void875bhndb_free_intr_isrc(struct bhndb_intr_isrc *isrc)876{877bus_release_resource(isrc->is_owner, SYS_RES_IRQ, isrc->is_rid,878isrc->is_res);879free(isrc, M_BHND);880}881882/**883* Allocate and initialize a new interrupt handler entry.884*885* @param owner The child device that owns this entry.886* @param r The child's interrupt resource.887* @param isrc The isrc mapped for this entry.888*889* @retval non-NULL success890* @retval NULL if allocation fails.891*/892struct bhndb_intr_handler *893bhndb_alloc_intr_handler(device_t owner, struct resource *r,894struct bhndb_intr_isrc *isrc)895{896struct bhndb_intr_handler *ih;897898ih = malloc(sizeof(*ih), M_BHND, M_NOWAIT | M_ZERO);899ih->ih_owner = owner;900ih->ih_res = r;901ih->ih_isrc = isrc;902ih->ih_cookiep = NULL;903ih->ih_active = false;904905return (ih);906}907908/**909* Free an interrupt handler entry.910*911* @param br The resource state owning @p ih.912* @param ih The interrupt handler entry to be removed.913*/914void915bhndb_free_intr_handler(struct bhndb_intr_handler *ih)916{917KASSERT(!ih->ih_active, ("free of active interrupt handler %p",918ih->ih_cookiep));919920free(ih, M_BHND);921}922923/**924* Add an active interrupt handler to the given resource state.925*926* @param br The resource state to be modified.927* @param ih The interrupt handler entry to be added.928*/929void930bhndb_register_intr_handler(struct bhndb_resources *br,931struct bhndb_intr_handler *ih)932{933KASSERT(!ih->ih_active, ("duplicate registration of interrupt "934"handler %p", ih->ih_cookiep));935KASSERT(ih->ih_cookiep != NULL, ("missing cookiep"));936937ih->ih_active = true;938STAILQ_INSERT_HEAD(&br->bus_intrs, ih, ih_link);939}940941/**942* Remove an interrupt handler from the given resource state.943*944* @param br The resource state containing @p ih.945* @param ih The interrupt handler entry to be removed.946*/947void948bhndb_deregister_intr_handler(struct bhndb_resources *br,949struct bhndb_intr_handler *ih)950{951KASSERT(ih->ih_active, ("duplicate deregistration of interrupt "952"handler %p", ih->ih_cookiep));953954KASSERT(bhndb_find_intr_handler(br, ih) == ih,955("unknown interrupt handler %p", ih));956957STAILQ_REMOVE(&br->bus_intrs, ih, bhndb_intr_handler, ih_link);958ih->ih_active = false;959}960961/**962* Return the interrupt handler entry corresponding to @p cookiep, or NULL963* if no entry is found.964*965* @param br The resource state to search for the given @p cookiep.966* @param cookiep The interrupt handler's bus-assigned cookiep value.967*/968struct bhndb_intr_handler *969bhndb_find_intr_handler(struct bhndb_resources *br, void *cookiep)970{971struct bhndb_intr_handler *ih;972973STAILQ_FOREACH(ih, &br->bus_intrs, ih_link) {974if (ih == cookiep)975return (ih);976}977978/* Not found */979return (NULL);980}981982/**983* Find the maximum start and end limits of the bridged resource @p r.984*985* If the resource is not currently mapped by the bridge, ENOENT will be986* returned.987*988* @param br The resource state to search.989* @param r The resource to search for in @p br.990* @param[out] start On success, the minimum supported start address.991* @param[out] end On success, the maximum supported end address.992*993* @retval 0 success994* @retval ENOENT no active mapping found for @p r of @p type995*/996int997bhndb_find_resource_limits(struct bhndb_resources *br,998struct resource *r, rman_res_t *start, rman_res_t *end)999{1000struct bhndb_dw_alloc *dynamic;1001struct bhndb_region *sregion;1002struct bhndb_intr_handler *ih;10031004switch (rman_get_type(r)) {1005case SYS_RES_IRQ:1006/* Is this one of ours? */1007STAILQ_FOREACH(ih, &br->bus_intrs, ih_link) {1008if (ih->ih_res == r)1009continue;10101011/* We don't support adjusting IRQ resource limits */1012*start = rman_get_start(r);1013*end = rman_get_end(r);1014return (0);1015}10161017/* Not found */1018return (ENOENT);10191020case SYS_RES_MEMORY: {1021/* Check for an enclosing dynamic register window */1022if ((dynamic = bhndb_dw_find_resource(br, r))) {1023*start = dynamic->target;1024*end = dynamic->target + dynamic->win->win_size - 1;1025return (0);1026}10271028/* Check for a static region */1029sregion = bhndb_find_resource_region(br, rman_get_start(r),1030rman_get_size(r));1031if (sregion != NULL && sregion->static_regwin != NULL) {1032*start = sregion->addr;1033*end = sregion->addr + sregion->size - 1;10341035return (0);1036}10371038/* Not found */1039return (ENOENT);1040}10411042default:1043device_printf(br->dev, "unknown resource type: %d\n",1044rman_get_type(r));1045return (ENOENT);1046}1047}10481049/**1050* Add a bus region entry to @p r for the given base @p addr and @p size.1051*1052* @param br The resource state to which the bus region entry will be added.1053* @param addr The base address of this region.1054* @param size The size of this region.1055* @param priority The resource priority to be assigned to allocations1056* made within this bus region.1057* @param alloc_flags resource allocation flags (@see bhndb_alloc_flags)1058* @param static_regwin If available, a static register window mapping this1059* bus region entry. If not available, NULL.1060*1061* @retval 0 success1062* @retval non-zero if adding the bus region fails.1063*/1064int1065bhndb_add_resource_region(struct bhndb_resources *br, bhnd_addr_t addr,1066bhnd_size_t size, bhndb_priority_t priority, uint32_t alloc_flags,1067const struct bhndb_regwin *static_regwin)1068{1069struct bhndb_region *reg;10701071/* Insert in the bus resource list */1072reg = malloc(sizeof(*reg), M_BHND, M_NOWAIT);1073if (reg == NULL)1074return (ENOMEM);10751076*reg = (struct bhndb_region) {1077.addr = addr,1078.size = size,1079.priority = priority,1080.alloc_flags = alloc_flags,1081.static_regwin = static_regwin1082};10831084STAILQ_INSERT_HEAD(&br->bus_regions, reg, link);10851086return (0);1087}10881089/**1090* Return true if a mapping of @p size bytes at @p addr is provided by either1091* one contiguous bus region, or by multiple discontiguous regions.1092*1093* @param br The resource state to query.1094* @param addr The requested starting address.1095* @param size The requested size.1096*/1097bool1098bhndb_has_static_region_mapping(struct bhndb_resources *br,1099bhnd_addr_t addr, bhnd_size_t size)1100{1101struct bhndb_region *region;1102bhnd_addr_t r_addr;11031104r_addr = addr;1105while ((region = bhndb_find_resource_region(br, r_addr, 1)) != NULL) {1106/* Must be backed by a static register window */1107if (region->static_regwin == NULL)1108return (false);11091110/* Adjust the search offset */1111r_addr += region->size;11121113/* Have we traversed a complete (if discontiguous) mapping? */1114if (r_addr == addr + size)1115return (true);1116}11171118/* No complete mapping found */1119return (false);1120}11211122/**1123* Find the bus region that maps @p size bytes at @p addr.1124*1125* @param br The resource state to search.1126* @param addr The requested starting address.1127* @param size The requested size.1128*1129* @retval bhndb_region A region that fully contains the requested range.1130* @retval NULL If no mapping region can be found.1131*/1132struct bhndb_region *1133bhndb_find_resource_region(struct bhndb_resources *br, bhnd_addr_t addr,1134bhnd_size_t size)1135{1136struct bhndb_region *region;11371138STAILQ_FOREACH(region, &br->bus_regions, link) {1139/* Request must fit within the region's mapping */1140if (addr < region->addr)1141continue;11421143if (addr + size > region->addr + region->size)1144continue;11451146return (region);1147}11481149/* Not found */1150return (NULL);1151}11521153/**1154* Find the entry matching @p r in @p dwa's references, if any.1155*1156* @param dwa The dynamic window allocation to search1157* @param r The resource to search for in @p dwa.1158*/1159static struct bhndb_dw_rentry *1160bhndb_dw_find_resource_entry(struct bhndb_dw_alloc *dwa, struct resource *r)1161{1162struct bhndb_dw_rentry *rentry;11631164LIST_FOREACH(rentry, &dwa->refs, dw_link) {1165struct resource *dw_res = rentry->dw_res;11661167/* Match dev/rid/addr/size */1168if (rman_get_device(dw_res) != rman_get_device(r) ||1169rman_get_rid(dw_res) != rman_get_rid(r) ||1170rman_get_start(dw_res) != rman_get_start(r) ||1171rman_get_size(dw_res) != rman_get_size(r))1172{1173continue;1174}11751176/* Matching allocation found */1177return (rentry);1178}11791180return (NULL);1181}11821183/**1184* Find the dynamic region allocated for @p r, if any.1185*1186* @param br The resource state to search.1187* @param r The resource to search for.1188*1189* @retval bhndb_dw_alloc The allocation record for @p r.1190* @retval NULL if no dynamic window is allocated for @p r.1191*/1192struct bhndb_dw_alloc *1193bhndb_dw_find_resource(struct bhndb_resources *br, struct resource *r)1194{1195struct bhndb_dw_alloc *dwa;11961197for (size_t i = 0; i < br->dwa_count; i++) {1198dwa = &br->dw_alloc[i];11991200/* Skip free dynamic windows */1201if (bhndb_dw_is_free(br, dwa))1202continue;12031204/* Matching allocation found? */1205if (bhndb_dw_find_resource_entry(dwa, r) != NULL)1206return (dwa);1207}12081209return (NULL);1210}12111212/**1213* Find an existing dynamic window mapping @p size bytes1214* at @p addr. The window may or may not be free.1215*1216* @param br The resource state to search.1217* @param addr The requested starting address.1218* @param size The requested size.1219*1220* @retval bhndb_dw_alloc A window allocation that fully contains the requested1221* range.1222* @retval NULL If no mapping region can be found.1223*/1224struct bhndb_dw_alloc *1225bhndb_dw_find_mapping(struct bhndb_resources *br, bhnd_addr_t addr,1226bhnd_size_t size)1227{1228struct bhndb_dw_alloc *dwr;1229const struct bhndb_regwin *win;12301231/* Search for an existing dynamic mapping of this address range. */1232for (size_t i = 0; i < br->dwa_count; i++) {1233dwr = &br->dw_alloc[i];1234win = dwr->win;12351236/* Verify the range */1237if (addr < dwr->target)1238continue;12391240if (addr + size > dwr->target + win->win_size)1241continue;12421243/* Found a usable mapping */1244return (dwr);1245}12461247/* not found */1248return (NULL);1249}12501251/**1252* Retain a reference to @p dwa for use by @p res.1253*1254* @param br The resource state owning @p dwa.1255* @param dwa The allocation record to be retained.1256* @param res The resource that will own a reference to @p dwa.1257*1258* @retval 0 success1259* @retval ENOMEM Failed to allocate a new reference structure.1260*/1261int1262bhndb_dw_retain(struct bhndb_resources *br, struct bhndb_dw_alloc *dwa,1263struct resource *res)1264{1265struct bhndb_dw_rentry *rentry;12661267KASSERT(bhndb_dw_find_resource_entry(dwa, res) == NULL,1268("double-retain of dynamic window for same resource"));12691270/* Insert a reference entry; we use M_NOWAIT to allow use from1271* within a non-sleepable lock */1272rentry = malloc(sizeof(*rentry), M_BHND, M_NOWAIT);1273if (rentry == NULL)1274return (ENOMEM);12751276rentry->dw_res = res;1277LIST_INSERT_HEAD(&dwa->refs, rentry, dw_link);12781279/* Update the free list */1280bit_set(br->dwa_freelist, dwa->rnid);12811282return (0);1283}12841285/**1286* Release a reference to @p dwa previously retained by @p res. If the1287* reference count of @p dwa reaches zero, it will be added to the1288* free list.1289*1290* @param br The resource state owning @p dwa.1291* @param dwa The allocation record to be released.1292* @param res The resource that currently owns a reference to @p dwa.1293*/1294void1295bhndb_dw_release(struct bhndb_resources *br, struct bhndb_dw_alloc *dwa,1296struct resource *r)1297{1298struct bhndb_dw_rentry *rentry;12991300/* Find the rentry */1301rentry = bhndb_dw_find_resource_entry(dwa, r);1302KASSERT(rentry != NULL, ("over release of resource entry"));13031304LIST_REMOVE(rentry, dw_link);1305free(rentry, M_BHND);13061307/* If this was the last reference, update the free list */1308if (LIST_EMPTY(&dwa->refs))1309bit_clear(br->dwa_freelist, dwa->rnid);1310}13111312/**1313* Attempt to set (or reset) the target address of @p dwa to map @p size bytes1314* at @p addr.1315*1316* This will apply any necessary window alignment and verify that1317* the window is capable of mapping the requested range prior to modifying1318* therecord.1319*1320* @param dev The device on which to issue the BHNDB_SET_WINDOW_ADDR() request.1321* @param br The resource state owning @p dwa.1322* @param dwa The allocation record to be configured.1323* @param addr The address to be mapped via @p dwa.1324* @param size The number of bytes to be mapped at @p addr.1325*1326* @retval 0 success1327* @retval non-zero no usable register window available.1328*/1329int1330bhndb_dw_set_addr(device_t dev, struct bhndb_resources *br,1331struct bhndb_dw_alloc *dwa, bus_addr_t addr, bus_size_t size)1332{1333const struct bhndb_regwin *rw;1334bus_addr_t offset;1335int error;13361337rw = dwa->win;13381339KASSERT(bhndb_dw_is_free(br, dwa) || mtx_owned(&br->dw_steal_mtx),1340("attempting to set the target address on an in-use window"));13411342/* Page-align the target address */1343offset = addr % rw->win_size;1344dwa->target = addr - offset;13451346/* Verify that the window is large enough for the full target */1347if (rw->win_size - offset < size)1348return (ENOMEM);13491350/* Update the window target */1351error = BHNDB_SET_WINDOW_ADDR(dev, dwa->win, dwa->target);1352if (error) {1353dwa->target = 0x0;1354return (error);1355}13561357return (0);1358}13591360/**1361* Steal an in-use allocation record from @p br, returning the record's current1362* target in @p saved on success.1363*1364* This function acquires a mutex and disables interrupts; callers should1365* avoid holding a stolen window longer than required to issue an I/O1366* request.1367*1368* A successful call to bhndb_dw_steal() must be balanced with a call to1369* bhndb_dw_return_stolen().1370*1371* @param br The resource state from which a window should be stolen.1372* @param saved The stolen window's saved target address.1373*1374* @retval non-NULL success1375* @retval NULL no dynamic window regions are defined.1376*/1377struct bhndb_dw_alloc *1378bhndb_dw_steal(struct bhndb_resources *br, bus_addr_t *saved)1379{1380struct bhndb_dw_alloc *dw_stolen;13811382KASSERT(bhndb_dw_next_free(br) == NULL,1383("attempting to steal an in-use window while free windows remain"));13841385/* Nothing to steal from? */1386if (br->dwa_count == 0)1387return (NULL);13881389/*1390* Acquire our steal spinlock; this will be released in1391* bhndb_dw_return_stolen().1392*1393* Acquiring also disables interrupts, which is required when one is1394* stealing an in-use existing register window.1395*/1396mtx_lock_spin(&br->dw_steal_mtx);13971398dw_stolen = &br->dw_alloc[0];1399*saved = dw_stolen->target;1400return (dw_stolen);1401}14021403/**1404* Return an allocation record previously stolen using bhndb_dw_steal().1405*1406* @param dev The device on which to issue a BHNDB_SET_WINDOW_ADDR() request.1407* @param br The resource state owning @p dwa.1408* @param dwa The allocation record to be returned.1409* @param saved The original target address provided by bhndb_dw_steal().1410*/1411void1412bhndb_dw_return_stolen(device_t dev, struct bhndb_resources *br,1413struct bhndb_dw_alloc *dwa, bus_addr_t saved)1414{1415int error;14161417mtx_assert(&br->dw_steal_mtx, MA_OWNED);14181419error = bhndb_dw_set_addr(dev, br, dwa, saved, 0);1420if (error) {1421panic("failed to restore register window target %#jx: %d\n",1422(uintmax_t)saved, error);1423}14241425mtx_unlock_spin(&br->dw_steal_mtx);1426}14271428/**1429* Return the count of @p type register windows in @p table.1430*1431* @param table The table to search.1432* @param type The required window type, or BHNDB_REGWIN_T_INVALID to1433* count all register window types.1434*/1435size_t1436bhndb_regwin_count(const struct bhndb_regwin *table,1437bhndb_regwin_type_t type)1438{1439const struct bhndb_regwin *rw;1440size_t count;14411442count = 0;1443for (rw = table; rw->win_type != BHNDB_REGWIN_T_INVALID; rw++) {1444if (type == BHNDB_REGWIN_T_INVALID || rw->win_type == type)1445count++;1446}14471448return (count);1449}14501451/**1452* Search @p table for the first window with the given @p type.1453*1454* @param table The table to search.1455* @param type The required window type.1456* @param min_size The minimum window size.1457*1458* @retval bhndb_regwin The first matching window.1459* @retval NULL If no window of the requested type could be found.1460*/1461const struct bhndb_regwin *1462bhndb_regwin_find_type(const struct bhndb_regwin *table,1463bhndb_regwin_type_t type, bus_size_t min_size)1464{1465const struct bhndb_regwin *rw;14661467for (rw = table; rw->win_type != BHNDB_REGWIN_T_INVALID; rw++)1468{1469if (rw->win_type == type && rw->win_size >= min_size)1470return (rw);1471}14721473return (NULL);1474}14751476/**1477* Search @p windows for the first matching core window.1478*1479* @param table The table to search.1480* @param class The required core class.1481* @param unit The required core unit, or -1.1482* @param port_type The required port type.1483* @param port The required port.1484* @param region The required region.1485* @param offset The required readable core register block offset.1486* @param min_size The required minimum readable size at @p offset.1487*1488* @retval bhndb_regwin The first matching window.1489* @retval NULL If no matching window was found.1490*/1491const struct bhndb_regwin *1492bhndb_regwin_find_core(const struct bhndb_regwin *table, bhnd_devclass_t class,1493int unit, bhnd_port_type port_type, u_int port, u_int region,1494bus_size_t offset, bus_size_t min_size)1495{1496const struct bhndb_regwin *rw;14971498for (rw = table; rw->win_type != BHNDB_REGWIN_T_INVALID; rw++)1499{1500bus_size_t rw_offset;15011502/* Match on core, port, and region attributes */1503if (rw->win_type != BHNDB_REGWIN_T_CORE)1504continue;15051506if (rw->d.core.class != class)1507continue;15081509if (unit != -1 && rw->d.core.unit != unit)1510continue;15111512if (rw->d.core.port_type != port_type)1513continue;15141515if (rw->d.core.port != port)1516continue;15171518if (rw->d.core.region != region)1519continue;15201521/* Verify that the requested range is mapped within1522* this register window */1523if (rw->d.core.offset > offset)1524continue;15251526rw_offset = offset - rw->d.core.offset;15271528if (rw->win_size < rw_offset)1529continue;15301531if (rw->win_size - rw_offset < min_size)1532continue;15331534return (rw);1535}15361537return (NULL);1538}15391540/**1541* Search @p windows for the best available window of at least @p min_size.1542*1543* Search order:1544* - BHND_REGWIN_T_CORE1545* - BHND_REGWIN_T_DYN1546*1547* @param table The table to search.1548* @param class The required core class.1549* @param unit The required core unit, or -1.1550* @param port_type The required port type.1551* @param port The required port.1552* @param region The required region.1553* @param offset The required readable core register block offset.1554* @param min_size The required minimum readable size at @p offset.1555*1556* @retval bhndb_regwin The first matching window.1557* @retval NULL If no matching window was found.1558*/1559const struct bhndb_regwin *1560bhndb_regwin_find_best(const struct bhndb_regwin *table,1561bhnd_devclass_t class, int unit, bhnd_port_type port_type, u_int port,1562u_int region, bus_size_t offset, bus_size_t min_size)1563{1564const struct bhndb_regwin *rw;15651566/* Prefer a fixed core mapping */1567rw = bhndb_regwin_find_core(table, class, unit, port_type,1568port, region, offset, min_size);1569if (rw != NULL)1570return (rw);15711572/* Fall back on a generic dynamic window */1573return (bhndb_regwin_find_type(table, BHNDB_REGWIN_T_DYN, min_size));1574}15751576/**1577* Return true if @p regw defines a BHNDB_REGWIN_T_CORE register window1578* that matches against @p core.1579*1580* @param regw A register window to match against.1581* @param core The bhnd(4) core info to match against @p regw.1582*/1583bool1584bhndb_regwin_match_core(const struct bhndb_regwin *regw,1585struct bhnd_core_info *core)1586{1587/* Only core windows are supported */1588if (regw->win_type != BHNDB_REGWIN_T_CORE)1589return (false);15901591/* Device class must match */1592if (bhnd_core_class(core) != regw->d.core.class)1593return (false);15941595/* Device unit must match */1596if (core->unit != regw->d.core.unit)1597return (false);15981599/* Matches */1600return (true);1601}16021603/**1604* Search for a core resource priority descriptor in @p table that matches1605* @p core.1606*1607* @param table The table to search.1608* @param core The core to match against @p table.1609*/1610const struct bhndb_hw_priority *1611bhndb_hw_priority_find_core(const struct bhndb_hw_priority *table,1612struct bhnd_core_info *core)1613{1614const struct bhndb_hw_priority *hp;16151616for (hp = table; hp->ports != NULL; hp++) {1617if (bhnd_core_matches(core, &hp->match))1618return (hp);1619}16201621/* not found */1622return (NULL);1623}16241625/**1626* Search for a port resource priority descriptor in @p table.1627*1628* @param table The table to search.1629* @param core The core to match against @p table.1630* @param port_type The required port type.1631* @param port The required port.1632* @param region The required region.1633*/1634const struct bhndb_port_priority *1635bhndb_hw_priorty_find_port(const struct bhndb_hw_priority *table,1636struct bhnd_core_info *core, bhnd_port_type port_type, u_int port,1637u_int region)1638{1639const struct bhndb_hw_priority *hp;16401641if ((hp = bhndb_hw_priority_find_core(table, core)) == NULL)1642return (NULL);16431644for (u_int i = 0; i < hp->num_ports; i++) {1645const struct bhndb_port_priority *pp = &hp->ports[i];16461647if (pp->type != port_type)1648continue;16491650if (pp->port != port)1651continue;16521653if (pp->region != region)1654continue;16551656return (pp);1657}16581659/* not found */1660return (NULL);1661}166216631664