10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * PCI nexus driver interface 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate #include <sys/conf.h> /* nulldev */ 350Sstevel@tonic-gate #include <sys/stat.h> /* devctl */ 360Sstevel@tonic-gate #include <sys/kmem.h> 370Sstevel@tonic-gate #include <sys/async.h> /* ecc_flt for pci_ecc.h */ 380Sstevel@tonic-gate #include <sys/sunddi.h> 390Sstevel@tonic-gate #include <sys/sunndi.h> 400Sstevel@tonic-gate #include <sys/ndifm.h> 410Sstevel@tonic-gate #include <sys/ontrap.h> 420Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 430Sstevel@tonic-gate #include <sys/ddi_subrdefs.h> 440Sstevel@tonic-gate #include <sys/epm.h> 450Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h> 46*117Sschwartz #include <sys/pci_tools_var.h> 470Sstevel@tonic-gate #include <sys/spl.h> 480Sstevel@tonic-gate #include <sys/pci/pci_obj.h> 490Sstevel@tonic-gate 500Sstevel@tonic-gate /*LINTLIBRARY*/ 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * function prototype for hotplug routine: 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate static void 560Sstevel@tonic-gate pci_init_hotplug(struct pci *); 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* 590Sstevel@tonic-gate * function prototypes for dev ops routines: 600Sstevel@tonic-gate */ 610Sstevel@tonic-gate static int pci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 620Sstevel@tonic-gate static int pci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 630Sstevel@tonic-gate static int pci_info(dev_info_t *dip, ddi_info_cmd_t infocmd, 640Sstevel@tonic-gate void *arg, void **result); 650Sstevel@tonic-gate static int pci_ctlops_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args); 660Sstevel@tonic-gate static int pci_ctlops_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args, 670Sstevel@tonic-gate void *result); 680Sstevel@tonic-gate static off_t get_reg_set_size(dev_info_t *child, int rnumber); 690Sstevel@tonic-gate 700Sstevel@tonic-gate /* 710Sstevel@tonic-gate * bus ops and dev ops structures: 720Sstevel@tonic-gate */ 730Sstevel@tonic-gate static struct bus_ops pci_bus_ops = { 740Sstevel@tonic-gate BUSO_REV, 750Sstevel@tonic-gate pci_map, 760Sstevel@tonic-gate 0, 770Sstevel@tonic-gate 0, 780Sstevel@tonic-gate 0, 790Sstevel@tonic-gate i_ddi_map_fault, 800Sstevel@tonic-gate pci_dma_setup, 810Sstevel@tonic-gate pci_dma_allochdl, 820Sstevel@tonic-gate pci_dma_freehdl, 830Sstevel@tonic-gate pci_dma_bindhdl, 840Sstevel@tonic-gate pci_dma_unbindhdl, 850Sstevel@tonic-gate pci_dma_sync, 860Sstevel@tonic-gate pci_dma_win, 870Sstevel@tonic-gate pci_dma_ctlops, 880Sstevel@tonic-gate pci_ctlops, 890Sstevel@tonic-gate ddi_bus_prop_op, 900Sstevel@tonic-gate ndi_busop_get_eventcookie, /* (*bus_get_eventcookie)(); */ 910Sstevel@tonic-gate ndi_busop_add_eventcall, /* (*bus_add_eventcall)(); */ 920Sstevel@tonic-gate ndi_busop_remove_eventcall, /* (*bus_remove_eventcall)(); */ 930Sstevel@tonic-gate ndi_post_event, /* (*bus_post_event)(); */ 940Sstevel@tonic-gate NULL, /* (*bus_intr_ctl)(); */ 950Sstevel@tonic-gate NULL, /* (*bus_config)(); */ 960Sstevel@tonic-gate NULL, /* (*bus_unconfig)(); */ 970Sstevel@tonic-gate pci_fm_init_child, /* (*bus_fm_init)(); */ 980Sstevel@tonic-gate NULL, /* (*bus_fm_fini)(); */ 990Sstevel@tonic-gate pci_bus_enter, /* (*bus_fm_access_enter)(); */ 1000Sstevel@tonic-gate pci_bus_exit, /* (*bus_fm_access_fini)(); */ 1010Sstevel@tonic-gate NULL, /* (*bus_power)(); */ 1020Sstevel@tonic-gate pci_intr_ops /* (*bus_intr_op)(); */ 1030Sstevel@tonic-gate }; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate extern struct cb_ops pci_cb_ops; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate static struct dev_ops pci_ops = { 1080Sstevel@tonic-gate DEVO_REV, 1090Sstevel@tonic-gate 0, 1100Sstevel@tonic-gate pci_info, 1110Sstevel@tonic-gate nulldev, 1120Sstevel@tonic-gate 0, 1130Sstevel@tonic-gate pci_attach, 1140Sstevel@tonic-gate pci_detach, 1150Sstevel@tonic-gate nodev, 1160Sstevel@tonic-gate &pci_cb_ops, 1170Sstevel@tonic-gate &pci_bus_ops, 1180Sstevel@tonic-gate 0 1190Sstevel@tonic-gate }; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * module definitions: 1230Sstevel@tonic-gate */ 1240Sstevel@tonic-gate #include <sys/modctl.h> 1250Sstevel@tonic-gate extern struct mod_ops mod_driverops; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate static struct modldrv modldrv = { 1280Sstevel@tonic-gate &mod_driverops, /* Type of module - driver */ 1290Sstevel@tonic-gate "PCI Bus nexus driver %I%", /* Name of module. */ 1300Sstevel@tonic-gate &pci_ops, /* driver ops */ 1310Sstevel@tonic-gate }; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate static struct modlinkage modlinkage = { 1340Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL 1350Sstevel@tonic-gate }; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate /* 1380Sstevel@tonic-gate * driver global data: 1390Sstevel@tonic-gate */ 1400Sstevel@tonic-gate void *per_pci_state; /* per-pbm soft state pointer */ 1410Sstevel@tonic-gate void *per_pci_common_state; /* per-psycho soft state pointer */ 1420Sstevel@tonic-gate kmutex_t pci_global_mutex; /* attach/detach common struct lock */ 1430Sstevel@tonic-gate errorq_t *pci_ecc_queue = NULL; /* per-system ecc handling queue */ 1440Sstevel@tonic-gate errorq_t *pci_target_queue = NULL; /* per-system target handling queue */ 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate extern void pci_child_cfg_save(dev_info_t *dip); 1470Sstevel@tonic-gate extern void pci_child_cfg_restore(dev_info_t *dip); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate int 1500Sstevel@tonic-gate _init(void) 1510Sstevel@tonic-gate { 1520Sstevel@tonic-gate int e; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* 1550Sstevel@tonic-gate * Initialize per-pci bus soft state pointer. 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate e = ddi_soft_state_init(&per_pci_state, sizeof (pci_t), 1); 1580Sstevel@tonic-gate if (e != 0) 1590Sstevel@tonic-gate return (e); 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate /* 1620Sstevel@tonic-gate * Initialize per-psycho soft state pointer. 1630Sstevel@tonic-gate */ 1640Sstevel@tonic-gate e = ddi_soft_state_init(&per_pci_common_state, 1650Sstevel@tonic-gate sizeof (pci_common_t), 1); 1660Sstevel@tonic-gate if (e != 0) { 1670Sstevel@tonic-gate ddi_soft_state_fini(&per_pci_state); 1680Sstevel@tonic-gate return (e); 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate /* 1720Sstevel@tonic-gate * Initialize global mutexes. 1730Sstevel@tonic-gate */ 1740Sstevel@tonic-gate mutex_init(&pci_global_mutex, NULL, MUTEX_DRIVER, NULL); 1750Sstevel@tonic-gate pci_reloc_init(); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* 1780Sstevel@tonic-gate * Create the performance kstats. 1790Sstevel@tonic-gate */ 1800Sstevel@tonic-gate pci_kstat_init(); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* 1830Sstevel@tonic-gate * Install the module. 1840Sstevel@tonic-gate */ 1850Sstevel@tonic-gate e = mod_install(&modlinkage); 1860Sstevel@tonic-gate if (e != 0) { 1870Sstevel@tonic-gate ddi_soft_state_fini(&per_pci_state); 1880Sstevel@tonic-gate ddi_soft_state_fini(&per_pci_common_state); 1890Sstevel@tonic-gate mutex_destroy(&pci_global_mutex); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate return (e); 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate int 1950Sstevel@tonic-gate _fini(void) 1960Sstevel@tonic-gate { 1970Sstevel@tonic-gate int e; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * Remove the module. 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate e = mod_remove(&modlinkage); 2030Sstevel@tonic-gate if (e != 0) 2040Sstevel@tonic-gate return (e); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * Destroy pci_ecc_queue, and set it to NULL. 2080Sstevel@tonic-gate */ 2090Sstevel@tonic-gate if (pci_ecc_queue) 2100Sstevel@tonic-gate errorq_destroy(pci_ecc_queue); 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate pci_ecc_queue = NULL; 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * Destroy pci_target_queue, and set it to NULL. 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate if (pci_target_queue) 2180Sstevel@tonic-gate errorq_destroy(pci_target_queue); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate pci_target_queue = NULL; 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate /* 2230Sstevel@tonic-gate * Destroy the performance kstats. 2240Sstevel@tonic-gate */ 2250Sstevel@tonic-gate pci_kstat_fini(); 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate /* 2280Sstevel@tonic-gate * Free the per-pci and per-psycho soft state info and destroy 2290Sstevel@tonic-gate * mutex for per-psycho soft state. 2300Sstevel@tonic-gate */ 2310Sstevel@tonic-gate ddi_soft_state_fini(&per_pci_state); 2320Sstevel@tonic-gate ddi_soft_state_fini(&per_pci_common_state); 2330Sstevel@tonic-gate mutex_destroy(&pci_global_mutex); 2340Sstevel@tonic-gate pci_reloc_fini(); 2350Sstevel@tonic-gate return (e); 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate int 2390Sstevel@tonic-gate _info(struct modinfo *modinfop) 2400Sstevel@tonic-gate { 2410Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /*ARGSUSED*/ 2450Sstevel@tonic-gate static int 2460Sstevel@tonic-gate pci_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2470Sstevel@tonic-gate { 2480Sstevel@tonic-gate int instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(getminor((dev_t)arg)); 2490Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(instance); 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* allow hotplug to deal with ones it manages */ 2520Sstevel@tonic-gate if (pci_p && (pci_p->hotplug_capable == B_TRUE)) 2530Sstevel@tonic-gate return (pcihp_info(dip, infocmd, arg, result)); 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate /* non-hotplug or not attached */ 2560Sstevel@tonic-gate switch (infocmd) { 2570Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 2580Sstevel@tonic-gate *result = (void *)instance; 2590Sstevel@tonic-gate return (DDI_SUCCESS); 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 2620Sstevel@tonic-gate if (pci_p == NULL) 2630Sstevel@tonic-gate return (DDI_FAILURE); 2640Sstevel@tonic-gate *result = (void *)pci_p->pci_dip; 2650Sstevel@tonic-gate return (DDI_SUCCESS); 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate default: 2680Sstevel@tonic-gate return (DDI_FAILURE); 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate /* device driver entry points */ 2740Sstevel@tonic-gate /* 2750Sstevel@tonic-gate * attach entry point: 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate static int 2780Sstevel@tonic-gate pci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2790Sstevel@tonic-gate { 2800Sstevel@tonic-gate pci_t *pci_p; /* per bus state pointer */ 2810Sstevel@tonic-gate int instance = ddi_get_instance(dip); 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate switch (cmd) { 2840Sstevel@tonic-gate case DDI_ATTACH: 2850Sstevel@tonic-gate DEBUG0(DBG_ATTACH, dip, "DDI_ATTACH\n"); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate /* 2880Sstevel@tonic-gate * Allocate and get the per-pci soft state structure. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate if (alloc_pci_soft_state(instance) != DDI_SUCCESS) { 2910Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: can't allocate pci state", 2920Sstevel@tonic-gate ddi_driver_name(dip), instance); 2930Sstevel@tonic-gate goto err_bad_pci_softstate; 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate pci_p = get_pci_soft_state(instance); 2960Sstevel@tonic-gate pci_p->pci_dip = dip; 2970Sstevel@tonic-gate mutex_init(&pci_p->pci_mutex, NULL, MUTEX_DRIVER, NULL); 2980Sstevel@tonic-gate pci_p->pci_soft_state = PCI_SOFT_STATE_CLOSED; 2990Sstevel@tonic-gate pci_p->pci_open_count = 0; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate /* 3020Sstevel@tonic-gate * Get key properties of the pci bridge node and 3030Sstevel@tonic-gate * determine it's type (psycho, schizo, etc ...). 3040Sstevel@tonic-gate */ 3050Sstevel@tonic-gate if (get_pci_properties(pci_p, dip) == DDI_FAILURE) 3060Sstevel@tonic-gate goto err_bad_pci_prop; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* 3090Sstevel@tonic-gate * Map in the registers. 3100Sstevel@tonic-gate */ 3110Sstevel@tonic-gate if (map_pci_registers(pci_p, dip) == DDI_FAILURE) 3120Sstevel@tonic-gate goto err_bad_reg_prop; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate if (pci_obj_setup(pci_p) != DDI_SUCCESS) 3150Sstevel@tonic-gate goto err_bad_objs; 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate /* 3180Sstevel@tonic-gate * If this PCI leaf has hotplug and this platform 3190Sstevel@tonic-gate * loads hotplug modules then initialize the 3200Sstevel@tonic-gate * hotplug framework. 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate pci_init_hotplug(pci_p); 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * Create the "devctl" node for hotplug support. 3260Sstevel@tonic-gate * For non-hotplug bus, we still need ":devctl" to 3270Sstevel@tonic-gate * support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls. 3280Sstevel@tonic-gate */ 3290Sstevel@tonic-gate if (pci_p->hotplug_capable == B_FALSE) { 3300Sstevel@tonic-gate if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 3310Sstevel@tonic-gate PCIHP_AP_MINOR_NUM(instance, PCIHP_DEVCTL_MINOR), 3320Sstevel@tonic-gate DDI_NT_NEXUS, 0) != DDI_SUCCESS) 3330Sstevel@tonic-gate goto err_bad_devctl_node; 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate /* 337*117Sschwartz * Create pcitool nodes for register access and interrupt 338*117Sschwartz * routing. 339*117Sschwartz */ 340*117Sschwartz if (pcitool_init(dip) != DDI_SUCCESS) { 341*117Sschwartz goto err_bad_pcitool_nodes; 342*117Sschwartz } 343*117Sschwartz 344*117Sschwartz /* 3450Sstevel@tonic-gate * Due to unresolved hardware issues, disable PCIPM until 3460Sstevel@tonic-gate * the problem is fully understood. 3470Sstevel@tonic-gate * 3480Sstevel@tonic-gate * pci_pwr_setup(pci_p, dip); 3490Sstevel@tonic-gate */ 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate ddi_report_dev(dip); 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate pci_p->pci_state = PCI_ATTACHED; 3540Sstevel@tonic-gate DEBUG0(DBG_ATTACH, dip, "attach success\n"); 3550Sstevel@tonic-gate break; 3560Sstevel@tonic-gate 357*117Sschwartz err_bad_pcitool_nodes: 358*117Sschwartz if (pci_p->hotplug_capable == B_FALSE) 359*117Sschwartz ddi_remove_minor_node(dip, "devctl"); 360*117Sschwartz else 361*117Sschwartz (void) pcihp_uninit(dip); 362*117Sschwartz err_bad_devctl_node: 363*117Sschwartz pci_obj_destroy(pci_p); 3640Sstevel@tonic-gate err_bad_objs: 3650Sstevel@tonic-gate unmap_pci_registers(pci_p); 3660Sstevel@tonic-gate err_bad_reg_prop: 3670Sstevel@tonic-gate free_pci_properties(pci_p); 3680Sstevel@tonic-gate err_bad_pci_prop: 3690Sstevel@tonic-gate mutex_destroy(&pci_p->pci_mutex); 3700Sstevel@tonic-gate free_pci_soft_state(instance); 3710Sstevel@tonic-gate err_bad_pci_softstate: 3720Sstevel@tonic-gate return (DDI_FAILURE); 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate case DDI_RESUME: 3750Sstevel@tonic-gate DEBUG0(DBG_ATTACH, dip, "DDI_RESUME\n"); 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate /* 3780Sstevel@tonic-gate * Make sure the Psycho control registers and IOMMU 3790Sstevel@tonic-gate * are configured properly. 3800Sstevel@tonic-gate */ 3810Sstevel@tonic-gate pci_p = get_pci_soft_state(instance); 3820Sstevel@tonic-gate mutex_enter(&pci_p->pci_mutex); 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate /* 3850Sstevel@tonic-gate * Make sure this instance has been suspended. 3860Sstevel@tonic-gate */ 3870Sstevel@tonic-gate if (pci_p->pci_state != PCI_SUSPENDED) { 3880Sstevel@tonic-gate DEBUG0(DBG_ATTACH, dip, "instance NOT suspended\n"); 3890Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 3900Sstevel@tonic-gate return (DDI_FAILURE); 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate pci_obj_resume(pci_p); 3930Sstevel@tonic-gate pci_p->pci_state = PCI_ATTACHED; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate pci_child_cfg_restore(dip); 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 3980Sstevel@tonic-gate break; 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate default: 4010Sstevel@tonic-gate DEBUG0(DBG_ATTACH, dip, "unsupported attach op\n"); 4020Sstevel@tonic-gate return (DDI_FAILURE); 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate return (DDI_SUCCESS); 4060Sstevel@tonic-gate } 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate /* 4090Sstevel@tonic-gate * detach entry point: 4100Sstevel@tonic-gate */ 4110Sstevel@tonic-gate static int 4120Sstevel@tonic-gate pci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 4130Sstevel@tonic-gate { 4140Sstevel@tonic-gate int instance = ddi_get_instance(dip); 4150Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(instance); 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate /* 4180Sstevel@tonic-gate * Make sure we are currently attached 4190Sstevel@tonic-gate */ 4200Sstevel@tonic-gate if (pci_p->pci_state != PCI_ATTACHED) { 4210Sstevel@tonic-gate DEBUG0(DBG_ATTACH, dip, "failed - instance not attached\n"); 4220Sstevel@tonic-gate return (DDI_FAILURE); 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate mutex_enter(&pci_p->pci_mutex); 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate switch (cmd) { 4280Sstevel@tonic-gate case DDI_DETACH: 4290Sstevel@tonic-gate DEBUG0(DBG_DETACH, dip, "DDI_DETACH\n"); 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate if (pci_p->hotplug_capable == B_TRUE) 4320Sstevel@tonic-gate if (pcihp_uninit(dip) == DDI_FAILURE) { 4330Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 4340Sstevel@tonic-gate return (DDI_FAILURE); 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate 437*117Sschwartz pcitool_uninit(dip); 438*117Sschwartz 4390Sstevel@tonic-gate pci_obj_destroy(pci_p); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate /* 4420Sstevel@tonic-gate * Free the pci soft state structure and the rest of the 4430Sstevel@tonic-gate * resources it's using. 4440Sstevel@tonic-gate */ 4450Sstevel@tonic-gate free_pci_properties(pci_p); 4460Sstevel@tonic-gate unmap_pci_registers(pci_p); 4470Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 4480Sstevel@tonic-gate mutex_destroy(&pci_p->pci_mutex); 4490Sstevel@tonic-gate free_pci_soft_state(instance); 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* Free the interrupt-priorities prop if we created it. */ { 4520Sstevel@tonic-gate int len; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate if (ddi_getproplen(DDI_DEV_T_ANY, dip, 4550Sstevel@tonic-gate DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 4560Sstevel@tonic-gate "interrupt-priorities", &len) == DDI_PROP_SUCCESS) 4570Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 4580Sstevel@tonic-gate "interrupt-priorities"); 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate return (DDI_SUCCESS); 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate case DDI_SUSPEND: 4630Sstevel@tonic-gate pci_child_cfg_save(dip); 4640Sstevel@tonic-gate pci_obj_suspend(pci_p); 4650Sstevel@tonic-gate pci_p->pci_state = PCI_SUSPENDED; 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 4680Sstevel@tonic-gate return (DDI_SUCCESS); 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate default: 4710Sstevel@tonic-gate DEBUG0(DBG_DETACH, dip, "unsupported detach op\n"); 4720Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 4730Sstevel@tonic-gate return (DDI_FAILURE); 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* bus driver entry points */ 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * bus map entry point: 4820Sstevel@tonic-gate * 4830Sstevel@tonic-gate * if map request is for an rnumber 4840Sstevel@tonic-gate * get the corresponding regspec from device node 4850Sstevel@tonic-gate * build a new regspec in our parent's format 4860Sstevel@tonic-gate * build a new map_req with the new regspec 4870Sstevel@tonic-gate * call up the tree to complete the mapping 4880Sstevel@tonic-gate */ 4890Sstevel@tonic-gate int 4900Sstevel@tonic-gate pci_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp, 4910Sstevel@tonic-gate off_t off, off_t len, caddr_t *addrp) 4920Sstevel@tonic-gate { 4930Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 4940Sstevel@tonic-gate struct regspec p_regspec; 4950Sstevel@tonic-gate ddi_map_req_t p_mapreq; 4960Sstevel@tonic-gate int reglen, rval, r_no; 4970Sstevel@tonic-gate pci_regspec_t reloc_reg, *rp = &reloc_reg; 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate DEBUG2(DBG_MAP, dip, "rdip=%s%d:", 5000Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip)); 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate if (mp->map_flags & DDI_MF_USER_MAPPING) 5030Sstevel@tonic-gate return (DDI_ME_UNIMPLEMENTED); 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate switch (mp->map_type) { 5060Sstevel@tonic-gate case DDI_MT_REGSPEC: 5070Sstevel@tonic-gate reloc_reg = *(pci_regspec_t *)mp->map_obj.rp; /* dup whole */ 5080Sstevel@tonic-gate break; 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate case DDI_MT_RNUMBER: 5110Sstevel@tonic-gate r_no = mp->map_obj.rnumber; 5120Sstevel@tonic-gate DEBUG1(DBG_MAP | DBG_CONT, dip, " r#=%x", r_no); 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate if (ddi_getlongprop(DDI_DEV_T_NONE, rdip, DDI_PROP_DONTPASS, 5150Sstevel@tonic-gate "reg", (caddr_t)&rp, ®len) != DDI_SUCCESS) 5160Sstevel@tonic-gate return (DDI_ME_RNUMBER_RANGE); 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate if (r_no < 0 || r_no >= reglen / sizeof (pci_regspec_t)) { 5190Sstevel@tonic-gate kmem_free(rp, reglen); 5200Sstevel@tonic-gate return (DDI_ME_RNUMBER_RANGE); 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate rp += r_no; 5230Sstevel@tonic-gate break; 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate default: 5260Sstevel@tonic-gate return (DDI_ME_INVAL); 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate DEBUG0(DBG_MAP | DBG_CONT, dip, "\n"); 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate /* use "assigned-addresses" to relocate regspec within pci space */ 5310Sstevel@tonic-gate if (rval = pci_reloc_reg(dip, rdip, pci_p, rp)) 5320Sstevel@tonic-gate goto done; 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate if (len) /* adjust regspec according to mapping request */ 5350Sstevel@tonic-gate rp->pci_size_low = len; 5360Sstevel@tonic-gate rp->pci_phys_low += off; 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate /* use "ranges" to translate relocated pci regspec into parent space */ 5390Sstevel@tonic-gate if (rval = pci_xlate_reg(pci_p, rp, &p_regspec)) 5400Sstevel@tonic-gate goto done; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate p_mapreq = *mp; /* dup the whole structure */ 5430Sstevel@tonic-gate p_mapreq.map_type = DDI_MT_REGSPEC; 5440Sstevel@tonic-gate p_mapreq.map_obj.rp = &p_regspec; 5450Sstevel@tonic-gate rval = ddi_map(dip, &p_mapreq, 0, 0, addrp); 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate if (rval == DDI_SUCCESS) { 5480Sstevel@tonic-gate /* 5490Sstevel@tonic-gate * Set-up access functions for FM access error capable drivers. 5500Sstevel@tonic-gate * The axq workaround prevents fault management support 5510Sstevel@tonic-gate */ 5520Sstevel@tonic-gate if (DDI_FM_ACC_ERR_CAP(pci_p->pci_fm_cap) && 5530Sstevel@tonic-gate DDI_FM_ACC_ERR_CAP(ddi_fm_capable(rdip)) && 5540Sstevel@tonic-gate mp->map_handlep->ah_acc.devacc_attr_access != 5550Sstevel@tonic-gate DDI_DEFAULT_ACC) 5560Sstevel@tonic-gate pci_fm_acc_setup(mp, rdip); 5570Sstevel@tonic-gate pci_axq_setup(mp, pci_p->pci_pbm_p); 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate done: 5610Sstevel@tonic-gate if (mp->map_type == DDI_MT_RNUMBER) 5620Sstevel@tonic-gate kmem_free(rp - r_no, reglen); 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate return (rval); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * bus dma map entry point 5690Sstevel@tonic-gate * return value: 5700Sstevel@tonic-gate * DDI_DMA_PARTIAL_MAP 1 5710Sstevel@tonic-gate * DDI_DMA_MAPOK 0 5720Sstevel@tonic-gate * DDI_DMA_MAPPED 0 5730Sstevel@tonic-gate * DDI_DMA_NORESOURCES -1 5740Sstevel@tonic-gate * DDI_DMA_NOMAPPING -2 5750Sstevel@tonic-gate * DDI_DMA_TOOBIG -3 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate int 5780Sstevel@tonic-gate pci_dma_setup(dev_info_t *dip, dev_info_t *rdip, ddi_dma_req_t *dmareq, 5790Sstevel@tonic-gate ddi_dma_handle_t *handlep) 5800Sstevel@tonic-gate { 5810Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 5820Sstevel@tonic-gate iommu_t *iommu_p = pci_p->pci_iommu_p; 5830Sstevel@tonic-gate ddi_dma_impl_t *mp; 5840Sstevel@tonic-gate int ret; 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate DEBUG3(DBG_DMA_MAP, dip, "mapping - rdip=%s%d type=%s\n", 5870Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), 5880Sstevel@tonic-gate handlep ? "alloc" : "advisory"); 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate if (!(mp = pci_dma_lmts2hdl(dip, rdip, iommu_p, dmareq))) 5910Sstevel@tonic-gate return (DDI_DMA_NORESOURCES); 5920Sstevel@tonic-gate if (mp == (ddi_dma_impl_t *)DDI_DMA_NOMAPPING) 5930Sstevel@tonic-gate return (DDI_DMA_NOMAPPING); 5940Sstevel@tonic-gate if (ret = pci_dma_type(pci_p, dmareq, mp)) 5950Sstevel@tonic-gate goto freehandle; 5960Sstevel@tonic-gate if (ret = pci_dma_pfn(pci_p, dmareq, mp)) 5970Sstevel@tonic-gate goto freehandle; 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate switch (PCI_DMA_TYPE(mp)) { 6000Sstevel@tonic-gate case DMAI_FLAGS_DVMA: /* LINTED E_EQUALITY_NOT_ASSIGNMENT */ 6010Sstevel@tonic-gate if ((ret = pci_dvma_win(pci_p, dmareq, mp)) || !handlep) 6020Sstevel@tonic-gate goto freehandle; 6030Sstevel@tonic-gate if (!PCI_DMA_CANCACHE(mp)) { /* try fast track */ 6040Sstevel@tonic-gate if (PCI_DMA_CANFAST(mp)) { 6050Sstevel@tonic-gate if (!pci_dvma_map_fast(iommu_p, mp)) 6060Sstevel@tonic-gate break; 6070Sstevel@tonic-gate /* LINTED E_NOP_ELSE_STMT */ 6080Sstevel@tonic-gate } else { 6090Sstevel@tonic-gate PCI_DVMA_FASTTRAK_PROF(mp); 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate } 6120Sstevel@tonic-gate if (ret = pci_dvma_map(mp, dmareq, iommu_p)) 6130Sstevel@tonic-gate goto freehandle; 6140Sstevel@tonic-gate break; 6150Sstevel@tonic-gate case DMAI_FLAGS_PEER_TO_PEER: /* LINTED E_EQUALITY_NOT_ASSIGNMENT */ 6160Sstevel@tonic-gate if ((ret = pci_dma_physwin(pci_p, dmareq, mp)) || !handlep) 6170Sstevel@tonic-gate goto freehandle; 6180Sstevel@tonic-gate break; 6190Sstevel@tonic-gate case DMAI_FLAGS_BYPASS: 6200Sstevel@tonic-gate default: 6210Sstevel@tonic-gate panic("%s%d: pci_dma_setup: bad dma type 0x%x", 6220Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), 6230Sstevel@tonic-gate PCI_DMA_TYPE(mp)); 6240Sstevel@tonic-gate /*NOTREACHED*/ 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate *handlep = (ddi_dma_handle_t)mp; 6270Sstevel@tonic-gate mp->dmai_flags |= (DMAI_FLAGS_INUSE | DMAI_FLAGS_MAPPED); 6280Sstevel@tonic-gate dump_dma_handle(DBG_DMA_MAP, dip, mp); 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate return ((mp->dmai_nwin == 1) ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP); 6310Sstevel@tonic-gate freehandle: 6320Sstevel@tonic-gate if (ret == DDI_DMA_NORESOURCES) 6330Sstevel@tonic-gate pci_dma_freemp(mp); /* don't run_callback() */ 6340Sstevel@tonic-gate else 6350Sstevel@tonic-gate (void) pci_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp); 6360Sstevel@tonic-gate return (ret); 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * bus dma alloc handle entry point: 6420Sstevel@tonic-gate */ 6430Sstevel@tonic-gate int 6440Sstevel@tonic-gate pci_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attrp, 6450Sstevel@tonic-gate int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep) 6460Sstevel@tonic-gate { 6470Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 6480Sstevel@tonic-gate ddi_dma_impl_t *mp; 6490Sstevel@tonic-gate int rval; 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate DEBUG2(DBG_DMA_ALLOCH, dip, "rdip=%s%d\n", 6520Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip)); 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate if (attrp->dma_attr_version != DMA_ATTR_V0) 6550Sstevel@tonic-gate return (DDI_DMA_BADATTR); 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate if (!(mp = pci_dma_allocmp(dip, rdip, waitfp, arg))) 6580Sstevel@tonic-gate return (DDI_DMA_NORESOURCES); 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate /* 6610Sstevel@tonic-gate * Save requestor's information 6620Sstevel@tonic-gate */ 6630Sstevel@tonic-gate mp->dmai_attr = *attrp; /* whole object - augmented later */ 6640Sstevel@tonic-gate *DEV_ATTR(mp) = *attrp; /* whole object - device orig attr */ 6650Sstevel@tonic-gate DEBUG1(DBG_DMA_ALLOCH, dip, "mp=%p\n", mp); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate /* check and convert dma attributes to handle parameters */ 6680Sstevel@tonic-gate if (rval = pci_dma_attr2hdl(pci_p, mp)) { 6690Sstevel@tonic-gate pci_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp); 6700Sstevel@tonic-gate *handlep = NULL; 6710Sstevel@tonic-gate return (rval); 6720Sstevel@tonic-gate } 6730Sstevel@tonic-gate *handlep = (ddi_dma_handle_t)mp; 6740Sstevel@tonic-gate return (DDI_SUCCESS); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* 6790Sstevel@tonic-gate * bus dma free handle entry point: 6800Sstevel@tonic-gate */ 6810Sstevel@tonic-gate /*ARGSUSED*/ 6820Sstevel@tonic-gate int 6830Sstevel@tonic-gate pci_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle) 6840Sstevel@tonic-gate { 6850Sstevel@tonic-gate DEBUG3(DBG_DMA_FREEH, dip, "rdip=%s%d mp=%p\n", 6860Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), handle); 6870Sstevel@tonic-gate pci_dma_freemp((ddi_dma_impl_t *)handle); 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate if (pci_kmem_clid) { 6900Sstevel@tonic-gate DEBUG0(DBG_DMA_FREEH, dip, "run handle callback\n"); 6910Sstevel@tonic-gate ddi_run_callback(&pci_kmem_clid); 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate return (DDI_SUCCESS); 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate /* 6980Sstevel@tonic-gate * bus dma bind handle entry point: 6990Sstevel@tonic-gate */ 7000Sstevel@tonic-gate int 7010Sstevel@tonic-gate pci_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip, 7020Sstevel@tonic-gate ddi_dma_handle_t handle, ddi_dma_req_t *dmareq, 7030Sstevel@tonic-gate ddi_dma_cookie_t *cookiep, uint_t *ccountp) 7040Sstevel@tonic-gate { 7050Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 7060Sstevel@tonic-gate iommu_t *iommu_p = pci_p->pci_iommu_p; 7070Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle; 7080Sstevel@tonic-gate int ret; 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate DEBUG4(DBG_DMA_BINDH, dip, "rdip=%s%d mp=%p dmareq=%p\n", 7110Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), mp, dmareq); 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate if (mp->dmai_flags & DMAI_FLAGS_INUSE) 7140Sstevel@tonic-gate return (DDI_DMA_INUSE); 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate ASSERT((mp->dmai_flags & ~DMAI_FLAGS_PRESERVE) == 0); 7170Sstevel@tonic-gate mp->dmai_flags |= DMAI_FLAGS_INUSE; 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate if (ret = pci_dma_type(pci_p, dmareq, mp)) 7200Sstevel@tonic-gate goto err; 7210Sstevel@tonic-gate if (ret = pci_dma_pfn(pci_p, dmareq, mp)) 7220Sstevel@tonic-gate goto err; 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate switch (PCI_DMA_TYPE(mp)) { 7250Sstevel@tonic-gate case DMAI_FLAGS_DVMA: 7260Sstevel@tonic-gate if (ret = pci_dvma_win(pci_p, dmareq, mp)) 7270Sstevel@tonic-gate goto map_err; 7280Sstevel@tonic-gate if (!PCI_DMA_CANCACHE(mp)) { /* try fast track */ 7290Sstevel@tonic-gate if (PCI_DMA_CANFAST(mp)) { 7300Sstevel@tonic-gate if (!pci_dvma_map_fast(iommu_p, mp)) 7310Sstevel@tonic-gate goto mapped; /*LINTED E_NOP_ELSE_STMT*/ 7320Sstevel@tonic-gate } else { 7330Sstevel@tonic-gate PCI_DVMA_FASTTRAK_PROF(mp); 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate if (ret = pci_dvma_map(mp, dmareq, iommu_p)) 7370Sstevel@tonic-gate goto map_err; 7380Sstevel@tonic-gate mapped: 7390Sstevel@tonic-gate *ccountp = 1; 7400Sstevel@tonic-gate MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, mp->dmai_size); 7410Sstevel@tonic-gate break; 7420Sstevel@tonic-gate case DMAI_FLAGS_BYPASS: 7430Sstevel@tonic-gate case DMAI_FLAGS_PEER_TO_PEER: 7440Sstevel@tonic-gate if (ret = pci_dma_physwin(pci_p, dmareq, mp)) 7450Sstevel@tonic-gate goto map_err; 7460Sstevel@tonic-gate *ccountp = WINLST(mp)->win_ncookies; 7470Sstevel@tonic-gate *cookiep = *(ddi_dma_cookie_t *)(WINLST(mp) + 1); /* wholeobj */ 7480Sstevel@tonic-gate break; 7490Sstevel@tonic-gate default: 7500Sstevel@tonic-gate panic("%s%d: pci_dma_bindhdl(%p): bad dma type", 7510Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), mp); 7520Sstevel@tonic-gate /*NOTREACHED*/ 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate DEBUG2(DBG_DMA_BINDH, dip, "cookie %x+%x\n", cookiep->dmac_address, 7550Sstevel@tonic-gate cookiep->dmac_size); 7560Sstevel@tonic-gate dump_dma_handle(DBG_DMA_MAP, dip, mp); 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) 7590Sstevel@tonic-gate (void) ndi_fmc_insert(rdip, DMA_HANDLE, mp, NULL); 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate mp->dmai_flags |= DMAI_FLAGS_MAPPED; 7620Sstevel@tonic-gate return (mp->dmai_nwin == 1 ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP); 7630Sstevel@tonic-gate map_err: 7640Sstevel@tonic-gate pci_dvma_unregister_callbacks(pci_p, mp); 7650Sstevel@tonic-gate pci_dma_freepfn(mp); 7660Sstevel@tonic-gate err: 7670Sstevel@tonic-gate mp->dmai_flags &= DMAI_FLAGS_PRESERVE; 7680Sstevel@tonic-gate return (ret); 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * bus dma unbind handle entry point: 7730Sstevel@tonic-gate */ 7740Sstevel@tonic-gate /*ARGSUSED*/ 7750Sstevel@tonic-gate int 7760Sstevel@tonic-gate pci_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle) 7770Sstevel@tonic-gate { 7780Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle; 7790Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 7800Sstevel@tonic-gate iommu_t *iommu_p = pci_p->pci_iommu_p; 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate DEBUG3(DBG_DMA_UNBINDH, dip, "rdip=%s%d, mp=%p\n", 7830Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), handle); 7840Sstevel@tonic-gate if ((mp->dmai_flags & DMAI_FLAGS_INUSE) == 0) { 7850Sstevel@tonic-gate DEBUG0(DBG_DMA_UNBINDH, dip, "handle not in use\n"); 7860Sstevel@tonic-gate return (DDI_FAILURE); 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate mp->dmai_flags &= ~DMAI_FLAGS_MAPPED; 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate switch (PCI_DMA_TYPE(mp)) { 7920Sstevel@tonic-gate case DMAI_FLAGS_DVMA: 7930Sstevel@tonic-gate pci_dvma_unregister_callbacks(pci_p, mp); 7940Sstevel@tonic-gate pci_dma_sync_unmap(dip, rdip, mp); 7950Sstevel@tonic-gate pci_dvma_unmap(iommu_p, mp); 7960Sstevel@tonic-gate pci_dma_freepfn(mp); 7970Sstevel@tonic-gate break; 7980Sstevel@tonic-gate case DMAI_FLAGS_BYPASS: 7990Sstevel@tonic-gate case DMAI_FLAGS_PEER_TO_PEER: 8000Sstevel@tonic-gate pci_dma_freewin(mp); 8010Sstevel@tonic-gate break; 8020Sstevel@tonic-gate default: 8030Sstevel@tonic-gate panic("%s%d: pci_dma_unbindhdl:bad dma type %p", 8040Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), mp); 8050Sstevel@tonic-gate /*NOTREACHED*/ 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate if (iommu_p->iommu_dvma_clid != 0) { 8080Sstevel@tonic-gate DEBUG0(DBG_DMA_UNBINDH, dip, "run dvma callback\n"); 8090Sstevel@tonic-gate ddi_run_callback(&iommu_p->iommu_dvma_clid); 8100Sstevel@tonic-gate } 8110Sstevel@tonic-gate if (pci_kmem_clid) { 8120Sstevel@tonic-gate DEBUG0(DBG_DMA_UNBINDH, dip, "run handle callback\n"); 8130Sstevel@tonic-gate ddi_run_callback(&pci_kmem_clid); 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate mp->dmai_flags &= DMAI_FLAGS_PRESERVE; 8160Sstevel@tonic-gate SYNC_BUF_PA(mp) = 0; 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) { 8190Sstevel@tonic-gate if (DEVI(rdip)->devi_fmhdl != NULL && 8200Sstevel@tonic-gate DDI_FM_DMA_ERR_CAP(DEVI(rdip)->devi_fmhdl->fh_cap)) { 8210Sstevel@tonic-gate (void) ndi_fmc_remove(rdip, DMA_HANDLE, mp); 8220Sstevel@tonic-gate } 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate return (DDI_SUCCESS); 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * bus dma win entry point: 8310Sstevel@tonic-gate */ 8320Sstevel@tonic-gate int 8330Sstevel@tonic-gate pci_dma_win(dev_info_t *dip, dev_info_t *rdip, 8340Sstevel@tonic-gate ddi_dma_handle_t handle, uint_t win, off_t *offp, 8350Sstevel@tonic-gate size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp) 8360Sstevel@tonic-gate { 8370Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle; 8380Sstevel@tonic-gate DEBUG2(DBG_DMA_WIN, dip, "rdip=%s%d\n", 8390Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip)); 8400Sstevel@tonic-gate dump_dma_handle(DBG_DMA_WIN, dip, mp); 8410Sstevel@tonic-gate if (win >= mp->dmai_nwin) { 8420Sstevel@tonic-gate DEBUG1(DBG_DMA_WIN, dip, "%x out of range\n", win); 8430Sstevel@tonic-gate return (DDI_FAILURE); 8440Sstevel@tonic-gate } 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate switch (PCI_DMA_TYPE(mp)) { 8470Sstevel@tonic-gate case DMAI_FLAGS_DVMA: 8480Sstevel@tonic-gate if (win != PCI_DMA_CURWIN(mp)) { 8490Sstevel@tonic-gate pci_t *pci_p = 8500Sstevel@tonic-gate get_pci_soft_state(ddi_get_instance(dip)); 8510Sstevel@tonic-gate pci_dma_sync_unmap(dip, rdip, mp); 8520Sstevel@tonic-gate /* map_window sets dmai_mapping/size/offset */ 8530Sstevel@tonic-gate iommu_map_window(pci_p->pci_iommu_p, mp, win); 8540Sstevel@tonic-gate } 8550Sstevel@tonic-gate if (cookiep) 8560Sstevel@tonic-gate MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, 8570Sstevel@tonic-gate mp->dmai_size); 8580Sstevel@tonic-gate if (ccountp) 8590Sstevel@tonic-gate *ccountp = 1; 8600Sstevel@tonic-gate break; 8610Sstevel@tonic-gate case DMAI_FLAGS_PEER_TO_PEER: 8620Sstevel@tonic-gate case DMAI_FLAGS_BYPASS: { 8630Sstevel@tonic-gate int i; 8640Sstevel@tonic-gate ddi_dma_cookie_t *ck_p; 8650Sstevel@tonic-gate pci_dma_win_t *win_p = mp->dmai_winlst; 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate for (i = 0; i < win; win_p = win_p->win_next, i++); 8680Sstevel@tonic-gate ck_p = (ddi_dma_cookie_t *)(win_p + 1); 8690Sstevel@tonic-gate *cookiep = *ck_p; 8700Sstevel@tonic-gate mp->dmai_offset = win_p->win_offset; 8710Sstevel@tonic-gate mp->dmai_size = win_p->win_size; 8720Sstevel@tonic-gate mp->dmai_mapping = ck_p->dmac_laddress; 8730Sstevel@tonic-gate mp->dmai_cookie = ck_p + 1; 8740Sstevel@tonic-gate win_p->win_curseg = 0; 8750Sstevel@tonic-gate if (ccountp) 8760Sstevel@tonic-gate *ccountp = win_p->win_ncookies; 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate break; 8790Sstevel@tonic-gate default: 8800Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: pci_dma_win:bad dma type 0x%x", 8810Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), 8820Sstevel@tonic-gate PCI_DMA_TYPE(mp)); 8830Sstevel@tonic-gate return (DDI_FAILURE); 8840Sstevel@tonic-gate } 8850Sstevel@tonic-gate if (cookiep) 8860Sstevel@tonic-gate DEBUG2(DBG_DMA_WIN, dip, 8870Sstevel@tonic-gate "cookie - dmac_address=%x dmac_size=%x\n", 8880Sstevel@tonic-gate cookiep->dmac_address, cookiep->dmac_size); 8890Sstevel@tonic-gate if (offp) 8900Sstevel@tonic-gate *offp = (off_t)mp->dmai_offset; 8910Sstevel@tonic-gate if (lenp) 8920Sstevel@tonic-gate *lenp = mp->dmai_size; 8930Sstevel@tonic-gate return (DDI_SUCCESS); 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate #ifdef DEBUG 8970Sstevel@tonic-gate static char *pci_dmactl_str[] = { 8980Sstevel@tonic-gate "DDI_DMA_FREE", 8990Sstevel@tonic-gate "DDI_DMA_SYNC", 9000Sstevel@tonic-gate "DDI_DMA_HTOC", 9010Sstevel@tonic-gate "DDI_DMA_KVADDR", 9020Sstevel@tonic-gate "DDI_DMA_MOVWIN", 9030Sstevel@tonic-gate "DDI_DMA_REPWIN", 9040Sstevel@tonic-gate "DDI_DMA_GETERR", 9050Sstevel@tonic-gate "DDI_DMA_COFF", 9060Sstevel@tonic-gate "DDI_DMA_NEXTWIN", 9070Sstevel@tonic-gate "DDI_DMA_NEXTSEG", 9080Sstevel@tonic-gate "DDI_DMA_SEGTOC", 9090Sstevel@tonic-gate "DDI_DMA_RESERVE", 9100Sstevel@tonic-gate "DDI_DMA_RELEASE", 9110Sstevel@tonic-gate "DDI_DMA_RESETH", 9120Sstevel@tonic-gate "DDI_DMA_CKSYNC", 9130Sstevel@tonic-gate "DDI_DMA_IOPB_ALLOC", 9140Sstevel@tonic-gate "DDI_DMA_IOPB_FREE", 9150Sstevel@tonic-gate "DDI_DMA_SMEM_ALLOC", 9160Sstevel@tonic-gate "DDI_DMA_SMEM_FREE", 9170Sstevel@tonic-gate "DDI_DMA_SET_SBUS64", 9180Sstevel@tonic-gate "DDI_DMA_REMAP" 9190Sstevel@tonic-gate }; 9200Sstevel@tonic-gate #endif 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate /* 9230Sstevel@tonic-gate * bus dma control entry point: 9240Sstevel@tonic-gate */ 9250Sstevel@tonic-gate int 9260Sstevel@tonic-gate pci_dma_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle, 9270Sstevel@tonic-gate enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp, 9280Sstevel@tonic-gate uint_t cache_flags) 9290Sstevel@tonic-gate { 9300Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle; 9310Sstevel@tonic-gate DEBUG3(DBG_DMA_CTL, dip, "%s: rdip=%s%d\n", pci_dmactl_str[cmd], 9320Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip)); 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate switch (cmd) { 9350Sstevel@tonic-gate case DDI_DMA_FREE: 9360Sstevel@tonic-gate (void) pci_dma_unbindhdl(dip, rdip, handle); 9370Sstevel@tonic-gate (void) pci_dma_freehdl(dip, rdip, handle); 9380Sstevel@tonic-gate return (DDI_SUCCESS); 9390Sstevel@tonic-gate case DDI_DMA_RESERVE: { 9400Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 9410Sstevel@tonic-gate return (pci_fdvma_reserve(dip, rdip, pci_p, 9420Sstevel@tonic-gate (ddi_dma_req_t *)offp, (ddi_dma_handle_t *)objp)); 9430Sstevel@tonic-gate } 9440Sstevel@tonic-gate case DDI_DMA_RELEASE: { 9450Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 9460Sstevel@tonic-gate return (pci_fdvma_release(dip, pci_p, mp)); 9470Sstevel@tonic-gate } 9480Sstevel@tonic-gate default: 9490Sstevel@tonic-gate break; 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate switch (PCI_DMA_TYPE(mp)) { 9530Sstevel@tonic-gate case DMAI_FLAGS_DVMA: 9540Sstevel@tonic-gate return (pci_dvma_ctl(dip, rdip, mp, cmd, offp, lenp, objp, 9550Sstevel@tonic-gate cache_flags)); 9560Sstevel@tonic-gate case DMAI_FLAGS_PEER_TO_PEER: 9570Sstevel@tonic-gate case DMAI_FLAGS_BYPASS: 9580Sstevel@tonic-gate return (pci_dma_ctl(dip, rdip, mp, cmd, offp, lenp, objp, 9590Sstevel@tonic-gate cache_flags)); 9600Sstevel@tonic-gate default: 9610Sstevel@tonic-gate panic("%s%d: pci_dma_ctlops(%x):bad dma type %x", 9620Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), cmd, 9630Sstevel@tonic-gate mp->dmai_flags); 9640Sstevel@tonic-gate /*NOTREACHED*/ 9650Sstevel@tonic-gate } 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate #ifdef DEBUG 9690Sstevel@tonic-gate int pci_peekfault_cnt = 0; 9700Sstevel@tonic-gate int pci_pokefault_cnt = 0; 9710Sstevel@tonic-gate #endif /* DEBUG */ 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate static int 9740Sstevel@tonic-gate pci_do_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args) 9750Sstevel@tonic-gate { 9760Sstevel@tonic-gate pbm_t *pbm_p = pci_p->pci_pbm_p; 9770Sstevel@tonic-gate int err = DDI_SUCCESS; 9780Sstevel@tonic-gate on_trap_data_t otd; 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate mutex_enter(&pbm_p->pbm_pokefault_mutex); 9810Sstevel@tonic-gate pbm_p->pbm_ontrap_data = &otd; 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate /* Set up protected environment. */ 9840Sstevel@tonic-gate if (!on_trap(&otd, OT_DATA_ACCESS)) { 9850Sstevel@tonic-gate uintptr_t tramp = otd.ot_trampoline; 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate otd.ot_trampoline = (uintptr_t)&poke_fault; 9880Sstevel@tonic-gate err = do_poke(in_args->size, (void *)in_args->dev_addr, 9890Sstevel@tonic-gate (void *)in_args->host_addr); 9900Sstevel@tonic-gate otd.ot_trampoline = tramp; 9910Sstevel@tonic-gate } else 9920Sstevel@tonic-gate err = DDI_FAILURE; 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate /* 9950Sstevel@tonic-gate * Read the async fault register for the PBM to see it sees 9960Sstevel@tonic-gate * a master-abort. 9970Sstevel@tonic-gate */ 9980Sstevel@tonic-gate pbm_clear_error(pbm_p); 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate if (otd.ot_trap & OT_DATA_ACCESS) 10010Sstevel@tonic-gate err = DDI_FAILURE; 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate /* Take down protected environment. */ 10040Sstevel@tonic-gate no_trap(); 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate pbm_p->pbm_ontrap_data = NULL; 10070Sstevel@tonic-gate mutex_exit(&pbm_p->pbm_pokefault_mutex); 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate #ifdef DEBUG 10100Sstevel@tonic-gate if (err == DDI_FAILURE) 10110Sstevel@tonic-gate pci_pokefault_cnt++; 10120Sstevel@tonic-gate #endif 10130Sstevel@tonic-gate return (err); 10140Sstevel@tonic-gate } 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate static int 10180Sstevel@tonic-gate pci_do_caut_put(pci_t *pci_p, peekpoke_ctlops_t *cautacc_ctlops_arg) 10190Sstevel@tonic-gate { 10200Sstevel@tonic-gate size_t size = cautacc_ctlops_arg->size; 10210Sstevel@tonic-gate uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr; 10220Sstevel@tonic-gate uintptr_t host_addr = cautacc_ctlops_arg->host_addr; 10230Sstevel@tonic-gate ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle; 10240Sstevel@tonic-gate size_t repcount = cautacc_ctlops_arg->repcount; 10250Sstevel@tonic-gate uint_t flags = cautacc_ctlops_arg->flags; 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate pbm_t *pbm_p = pci_p->pci_pbm_p; 10280Sstevel@tonic-gate int err = DDI_SUCCESS; 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate /* Use ontrap data in handle set up by FMA */ 10310Sstevel@tonic-gate pbm_p->pbm_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap; 10320Sstevel@tonic-gate 10330Sstevel@tonic-gate hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED; 10340Sstevel@tonic-gate 10350Sstevel@tonic-gate /* 10360Sstevel@tonic-gate * Note that i_ndi_busop_access_enter ends up grabbing the pokefault 10370Sstevel@tonic-gate * mutex. 10380Sstevel@tonic-gate */ 10390Sstevel@tonic-gate i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp); 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) { 10420Sstevel@tonic-gate for (; repcount; repcount--) { 10430Sstevel@tonic-gate switch (size) { 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate case sizeof (uint8_t): 10460Sstevel@tonic-gate i_ddi_put8(hp, (uint8_t *)dev_addr, 10470Sstevel@tonic-gate *(uint8_t *)host_addr); 10480Sstevel@tonic-gate break; 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate case sizeof (uint16_t): 10510Sstevel@tonic-gate i_ddi_put16(hp, (uint16_t *)dev_addr, 10520Sstevel@tonic-gate *(uint16_t *)host_addr); 10530Sstevel@tonic-gate break; 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate case sizeof (uint32_t): 10560Sstevel@tonic-gate i_ddi_put32(hp, (uint32_t *)dev_addr, 10570Sstevel@tonic-gate *(uint32_t *)host_addr); 10580Sstevel@tonic-gate break; 10590Sstevel@tonic-gate 10600Sstevel@tonic-gate case sizeof (uint64_t): 10610Sstevel@tonic-gate i_ddi_put64(hp, (uint64_t *)dev_addr, 10620Sstevel@tonic-gate *(uint64_t *)host_addr); 10630Sstevel@tonic-gate break; 10640Sstevel@tonic-gate } 10650Sstevel@tonic-gate 10660Sstevel@tonic-gate host_addr += size; 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate if (flags == DDI_DEV_AUTOINCR) 10690Sstevel@tonic-gate dev_addr += size; 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate /* 10720Sstevel@tonic-gate * Read the async fault register for the PBM to see if 10730Sstevel@tonic-gate * it sees a master-abort. 10740Sstevel@tonic-gate */ 10750Sstevel@tonic-gate pbm_clear_error(pbm_p); 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate if (pbm_p->pbm_ontrap_data->ot_trap & OT_DATA_ACCESS) { 10780Sstevel@tonic-gate err = DDI_FAILURE; 10790Sstevel@tonic-gate #ifdef DEBUG 10800Sstevel@tonic-gate pci_pokefault_cnt++; 10810Sstevel@tonic-gate #endif 10820Sstevel@tonic-gate break; 10830Sstevel@tonic-gate } 10840Sstevel@tonic-gate } 10850Sstevel@tonic-gate } 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate i_ddi_notrap((ddi_acc_handle_t)hp); 10880Sstevel@tonic-gate pbm_p->pbm_ontrap_data = NULL; 10890Sstevel@tonic-gate i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp); 10900Sstevel@tonic-gate hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED; 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate return (err); 10930Sstevel@tonic-gate } 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate static int 10970Sstevel@tonic-gate pci_ctlops_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args) 10980Sstevel@tonic-gate { 10990Sstevel@tonic-gate return (in_args->handle ? pci_do_caut_put(pci_p, in_args) : 11000Sstevel@tonic-gate pci_do_poke(pci_p, in_args)); 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate static int 11050Sstevel@tonic-gate pci_do_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args) 11060Sstevel@tonic-gate { 11070Sstevel@tonic-gate int err = DDI_SUCCESS; 11080Sstevel@tonic-gate on_trap_data_t otd; 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate if (!on_trap(&otd, OT_DATA_ACCESS)) { 11110Sstevel@tonic-gate uintptr_t tramp = otd.ot_trampoline; 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate otd.ot_trampoline = (uintptr_t)&peek_fault; 11140Sstevel@tonic-gate err = do_peek(in_args->size, (void *)in_args->dev_addr, 11150Sstevel@tonic-gate (void *)in_args->host_addr); 11160Sstevel@tonic-gate otd.ot_trampoline = tramp; 11170Sstevel@tonic-gate } else 11180Sstevel@tonic-gate err = DDI_FAILURE; 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate no_trap(); 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate #ifdef DEBUG 11230Sstevel@tonic-gate if (err == DDI_FAILURE) 11240Sstevel@tonic-gate pci_peekfault_cnt++; 11250Sstevel@tonic-gate #endif 11260Sstevel@tonic-gate return (err); 11270Sstevel@tonic-gate } 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate static int 11300Sstevel@tonic-gate pci_do_caut_get(pci_t *pci_p, peekpoke_ctlops_t *cautacc_ctlops_arg) 11310Sstevel@tonic-gate { 11320Sstevel@tonic-gate size_t size = cautacc_ctlops_arg->size; 11330Sstevel@tonic-gate uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr; 11340Sstevel@tonic-gate uintptr_t host_addr = cautacc_ctlops_arg->host_addr; 11350Sstevel@tonic-gate ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle; 11360Sstevel@tonic-gate size_t repcount = cautacc_ctlops_arg->repcount; 11370Sstevel@tonic-gate uint_t flags = cautacc_ctlops_arg->flags; 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate pbm_t *pbm_p = pci_p->pci_pbm_p; 11400Sstevel@tonic-gate int err = DDI_SUCCESS; 11410Sstevel@tonic-gate 11420Sstevel@tonic-gate hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED; 11430Sstevel@tonic-gate i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp); 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate /* Can this code be optimized? */ 11460Sstevel@tonic-gate 11470Sstevel@tonic-gate if (repcount == 1) { 11480Sstevel@tonic-gate if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) { 11490Sstevel@tonic-gate i_ddi_caut_get(size, (void *)dev_addr, 11500Sstevel@tonic-gate (void *)host_addr); 11510Sstevel@tonic-gate } else { 11520Sstevel@tonic-gate int i; 11530Sstevel@tonic-gate uint8_t *ff_addr = (uint8_t *)host_addr; 11540Sstevel@tonic-gate for (i = 0; i < size; i++) 11550Sstevel@tonic-gate *ff_addr++ = 0xff; 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate err = DDI_FAILURE; 11580Sstevel@tonic-gate #ifdef DEBUG 11590Sstevel@tonic-gate pci_peekfault_cnt++; 11600Sstevel@tonic-gate #endif 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate } else { 11630Sstevel@tonic-gate if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) { 11640Sstevel@tonic-gate for (; repcount; repcount--) { 11650Sstevel@tonic-gate i_ddi_caut_get(size, (void *)dev_addr, 11660Sstevel@tonic-gate (void *)host_addr); 11670Sstevel@tonic-gate 11680Sstevel@tonic-gate host_addr += size; 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate if (flags == DDI_DEV_AUTOINCR) 11710Sstevel@tonic-gate dev_addr += size; 11720Sstevel@tonic-gate } 11730Sstevel@tonic-gate } else { 11740Sstevel@tonic-gate err = DDI_FAILURE; 11750Sstevel@tonic-gate #ifdef DEBUG 11760Sstevel@tonic-gate pci_peekfault_cnt++; 11770Sstevel@tonic-gate #endif 11780Sstevel@tonic-gate } 11790Sstevel@tonic-gate } 11800Sstevel@tonic-gate 11810Sstevel@tonic-gate i_ddi_notrap((ddi_acc_handle_t)hp); 11820Sstevel@tonic-gate pbm_p->pbm_ontrap_data = NULL; 11830Sstevel@tonic-gate i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp); 11840Sstevel@tonic-gate hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED; 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate return (err); 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate static int 11910Sstevel@tonic-gate pci_ctlops_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args, void *result) 11920Sstevel@tonic-gate { 11930Sstevel@tonic-gate result = (void *)in_args->host_addr; 11940Sstevel@tonic-gate return (in_args->handle ? pci_do_caut_get(pci_p, in_args) : 11950Sstevel@tonic-gate pci_do_peek(pci_p, in_args)); 11960Sstevel@tonic-gate } 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate /* 11990Sstevel@tonic-gate * get_reg_set_size 12000Sstevel@tonic-gate * 12010Sstevel@tonic-gate * Given a dev info pointer to a pci child and a register number, this 12020Sstevel@tonic-gate * routine returns the size element of that reg set property. 12030Sstevel@tonic-gate * return value: size of reg set on success, -1 on error 12040Sstevel@tonic-gate */ 12050Sstevel@tonic-gate static off_t 12060Sstevel@tonic-gate get_reg_set_size(dev_info_t *child, int rnumber) 12070Sstevel@tonic-gate { 12080Sstevel@tonic-gate pci_regspec_t *pci_rp; 12090Sstevel@tonic-gate off_t size; 12100Sstevel@tonic-gate int i; 12110Sstevel@tonic-gate 12120Sstevel@tonic-gate if (rnumber < 0) 12130Sstevel@tonic-gate return (-1); 12140Sstevel@tonic-gate 12150Sstevel@tonic-gate /* 12160Sstevel@tonic-gate * Get the reg property for the device. 12170Sstevel@tonic-gate */ 12180Sstevel@tonic-gate if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg", 12190Sstevel@tonic-gate (caddr_t)&pci_rp, &i) != DDI_SUCCESS) 12200Sstevel@tonic-gate return (-1); 12210Sstevel@tonic-gate 12220Sstevel@tonic-gate if (rnumber >= (i / (int)sizeof (pci_regspec_t))) { 12230Sstevel@tonic-gate kmem_free(pci_rp, i); 12240Sstevel@tonic-gate return (-1); 12250Sstevel@tonic-gate } 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate size = pci_rp[rnumber].pci_size_low | 12280Sstevel@tonic-gate ((uint64_t)pci_rp[rnumber].pci_size_hi << 32); 12290Sstevel@tonic-gate kmem_free(pci_rp, i); 12300Sstevel@tonic-gate return (size); 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate 12330Sstevel@tonic-gate 12340Sstevel@tonic-gate /* 12350Sstevel@tonic-gate * control ops entry point: 12360Sstevel@tonic-gate * 12370Sstevel@tonic-gate * Requests handled completely: 12380Sstevel@tonic-gate * DDI_CTLOPS_INITCHILD see init_child() for details 12390Sstevel@tonic-gate * DDI_CTLOPS_UNINITCHILD 12400Sstevel@tonic-gate * DDI_CTLOPS_REPORTDEV see report_dev() for details 12410Sstevel@tonic-gate * DDI_CTLOPS_XLATE_INTRS nothing to do 12420Sstevel@tonic-gate * DDI_CTLOPS_IOMIN cache line size if streaming otherwise 1 12430Sstevel@tonic-gate * DDI_CTLOPS_REGSIZE 12440Sstevel@tonic-gate * DDI_CTLOPS_NREGS 12450Sstevel@tonic-gate * DDI_CTLOPS_NINTRS 12460Sstevel@tonic-gate * DDI_CTLOPS_DVMAPAGESIZE 12470Sstevel@tonic-gate * DDI_CTLOPS_POKE 12480Sstevel@tonic-gate * DDI_CTLOPS_PEEK 12490Sstevel@tonic-gate * DDI_CTLOPS_QUIESCE 12500Sstevel@tonic-gate * DDI_CTLOPS_UNQUIESCE 12510Sstevel@tonic-gate * 12520Sstevel@tonic-gate * All others passed to parent. 12530Sstevel@tonic-gate */ 12540Sstevel@tonic-gate int 12550Sstevel@tonic-gate pci_ctlops(dev_info_t *dip, dev_info_t *rdip, 12560Sstevel@tonic-gate ddi_ctl_enum_t op, void *arg, void *result) 12570Sstevel@tonic-gate { 12580Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 12590Sstevel@tonic-gate 12600Sstevel@tonic-gate switch (op) { 12610Sstevel@tonic-gate case DDI_CTLOPS_INITCHILD: 12620Sstevel@tonic-gate return (init_child(pci_p, (dev_info_t *)arg)); 12630Sstevel@tonic-gate 12640Sstevel@tonic-gate case DDI_CTLOPS_UNINITCHILD: 12650Sstevel@tonic-gate return (uninit_child(pci_p, (dev_info_t *)arg)); 12660Sstevel@tonic-gate 12670Sstevel@tonic-gate case DDI_CTLOPS_REPORTDEV: 12680Sstevel@tonic-gate return (report_dev(rdip)); 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate case DDI_CTLOPS_IOMIN: 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate /* 12730Sstevel@tonic-gate * If we are using the streaming cache, align at 12740Sstevel@tonic-gate * least on a cache line boundary. Otherwise use 12750Sstevel@tonic-gate * whatever alignment is passed in. 12760Sstevel@tonic-gate */ 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate if ((int)arg) { 12790Sstevel@tonic-gate int val = *((int *)result); 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate val = maxbit(val, PCI_SBUF_LINE_SIZE); 12820Sstevel@tonic-gate *((int *)result) = val; 12830Sstevel@tonic-gate } 12840Sstevel@tonic-gate return (DDI_SUCCESS); 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate case DDI_CTLOPS_REGSIZE: 12870Sstevel@tonic-gate *((off_t *)result) = get_reg_set_size(rdip, *((int *)arg)); 12880Sstevel@tonic-gate return (*((off_t *)result) == -1 ? DDI_FAILURE : DDI_SUCCESS); 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate case DDI_CTLOPS_NREGS: 12910Sstevel@tonic-gate *((uint_t *)result) = get_nreg_set(rdip); 12920Sstevel@tonic-gate return (DDI_SUCCESS); 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate case DDI_CTLOPS_DVMAPAGESIZE: 12950Sstevel@tonic-gate *((ulong_t *)result) = IOMMU_PAGE_SIZE; 12960Sstevel@tonic-gate return (DDI_SUCCESS); 12970Sstevel@tonic-gate 12980Sstevel@tonic-gate case DDI_CTLOPS_POKE: 12990Sstevel@tonic-gate return (pci_ctlops_poke(pci_p, (peekpoke_ctlops_t *)arg)); 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate case DDI_CTLOPS_PEEK: 13020Sstevel@tonic-gate return (pci_ctlops_peek(pci_p, (peekpoke_ctlops_t *)arg, 13030Sstevel@tonic-gate result)); 13040Sstevel@tonic-gate 13050Sstevel@tonic-gate case DDI_CTLOPS_AFFINITY: 13060Sstevel@tonic-gate break; 13070Sstevel@tonic-gate 13080Sstevel@tonic-gate case DDI_CTLOPS_QUIESCE: 13090Sstevel@tonic-gate return (pci_bus_quiesce(pci_p, rdip, result)); 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate case DDI_CTLOPS_UNQUIESCE: 13120Sstevel@tonic-gate return (pci_bus_unquiesce(pci_p, rdip, result)); 13130Sstevel@tonic-gate 13140Sstevel@tonic-gate default: 13150Sstevel@tonic-gate break; 13160Sstevel@tonic-gate } 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate /* 13190Sstevel@tonic-gate * Now pass the request up to our parent. 13200Sstevel@tonic-gate */ 13210Sstevel@tonic-gate DEBUG2(DBG_CTLOPS, dip, "passing request to parent: rdip=%s%d\n", 13220Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip)); 13230Sstevel@tonic-gate return (ddi_ctlops(dip, rdip, op, arg, result)); 13240Sstevel@tonic-gate } 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate 13270Sstevel@tonic-gate /* ARGSUSED */ 13280Sstevel@tonic-gate int 13290Sstevel@tonic-gate pci_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op, 13300Sstevel@tonic-gate ddi_intr_handle_impl_t *hdlp, void *result) 13310Sstevel@tonic-gate { 13320Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state( 13330Sstevel@tonic-gate ddi_get_instance(dip)); 13340Sstevel@tonic-gate ddi_ispec_t *ip = (ddi_ispec_t *)hdlp->ih_private; 13350Sstevel@tonic-gate int ret = DDI_SUCCESS; 13360Sstevel@tonic-gate 13370Sstevel@tonic-gate switch (intr_op) { 13380Sstevel@tonic-gate case DDI_INTROP_GETCAP: 13390Sstevel@tonic-gate /* GetCap will always fail for all non PCI devices */ 13400Sstevel@tonic-gate (void) pci_intx_get_cap(rdip, (int *)result); 13410Sstevel@tonic-gate break; 13420Sstevel@tonic-gate case DDI_INTROP_SETCAP: 13430Sstevel@tonic-gate ret = DDI_ENOTSUP; 13440Sstevel@tonic-gate break; 13450Sstevel@tonic-gate case DDI_INTROP_ALLOC: 13460Sstevel@tonic-gate *(int *)result = hdlp->ih_scratch1; 13470Sstevel@tonic-gate break; 13480Sstevel@tonic-gate case DDI_INTROP_FREE: 13490Sstevel@tonic-gate break; 13500Sstevel@tonic-gate case DDI_INTROP_GETPRI: 13510Sstevel@tonic-gate *(int *)result = ip->is_pil ? 13520Sstevel@tonic-gate ip->is_pil : pci_class_to_pil(rdip); 13530Sstevel@tonic-gate break; 13540Sstevel@tonic-gate case DDI_INTROP_SETPRI: 13550Sstevel@tonic-gate ip->is_pil = (*(int *)result); 13560Sstevel@tonic-gate break; 13570Sstevel@tonic-gate case DDI_INTROP_ADDISR: 13580Sstevel@tonic-gate hdlp->ih_vector = *ip->is_intr; 13590Sstevel@tonic-gate 13600Sstevel@tonic-gate ret = pci_add_intr(dip, rdip, hdlp); 13610Sstevel@tonic-gate break; 13620Sstevel@tonic-gate case DDI_INTROP_REMISR: 13630Sstevel@tonic-gate hdlp->ih_vector = *ip->is_intr; 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate ret = pci_remove_intr(dip, rdip, hdlp); 13660Sstevel@tonic-gate break; 13670Sstevel@tonic-gate case DDI_INTROP_ENABLE: 13680Sstevel@tonic-gate ret = ib_update_intr_state(pci_p, rdip, hdlp, 13690Sstevel@tonic-gate PCI_INTR_STATE_ENABLE); 13700Sstevel@tonic-gate break; 13710Sstevel@tonic-gate case DDI_INTROP_DISABLE: 13720Sstevel@tonic-gate ret = ib_update_intr_state(pci_p, rdip, hdlp, 13730Sstevel@tonic-gate PCI_INTR_STATE_DISABLE); 13740Sstevel@tonic-gate break; 13750Sstevel@tonic-gate case DDI_INTROP_SETMASK: 13760Sstevel@tonic-gate ret = pci_intx_set_mask(rdip); 13770Sstevel@tonic-gate break; 13780Sstevel@tonic-gate case DDI_INTROP_CLRMASK: 13790Sstevel@tonic-gate ret = pci_intx_clr_mask(rdip); 13800Sstevel@tonic-gate break; 13810Sstevel@tonic-gate case DDI_INTROP_GETPENDING: 13820Sstevel@tonic-gate ret = pci_intx_get_pending(rdip, (int *)result); 13830Sstevel@tonic-gate break; 13840Sstevel@tonic-gate case DDI_INTROP_NINTRS: 13850Sstevel@tonic-gate case DDI_INTROP_NAVAIL: 13860Sstevel@tonic-gate *(int *)result = i_ddi_get_nintrs(rdip); 13870Sstevel@tonic-gate break; 13880Sstevel@tonic-gate case DDI_INTROP_SUPPORTED_TYPES: 13890Sstevel@tonic-gate /* PCI nexus driver supports only fixed interrupts */ 13900Sstevel@tonic-gate *(int *)result = i_ddi_get_nintrs(rdip) ? 13910Sstevel@tonic-gate DDI_INTR_TYPE_FIXED : 0; 13920Sstevel@tonic-gate break; 13930Sstevel@tonic-gate default: 13940Sstevel@tonic-gate ret = DDI_ENOTSUP; 13950Sstevel@tonic-gate break; 13960Sstevel@tonic-gate } 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate return (ret); 13990Sstevel@tonic-gate } 14000Sstevel@tonic-gate 14010Sstevel@tonic-gate static void 14020Sstevel@tonic-gate pci_init_hotplug(struct pci *pci_p) 14030Sstevel@tonic-gate { 14040Sstevel@tonic-gate pci_bus_range_t bus_range; 14050Sstevel@tonic-gate dev_info_t *dip; 14060Sstevel@tonic-gate struct cb_ops *ops; 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate /* 14090Sstevel@tonic-gate * Before initializing hotplug - open up 14100Sstevel@tonic-gate * bus range. The busra module will 14110Sstevel@tonic-gate * initialize its pool of bus numbers from 14120Sstevel@tonic-gate * this. "busra" will be the agent that keeps 14130Sstevel@tonic-gate * track of them during hotplug. Also, note, 14140Sstevel@tonic-gate * that busra will remove any bus numbers 14150Sstevel@tonic-gate * already in use from boot time. 14160Sstevel@tonic-gate */ 14170Sstevel@tonic-gate bus_range.lo = 0x0; 14180Sstevel@tonic-gate bus_range.hi = 0xff; 14190Sstevel@tonic-gate dip = pci_p->pci_dip; 14200Sstevel@tonic-gate pci_p->hotplug_capable = B_FALSE; 14210Sstevel@tonic-gate 14220Sstevel@tonic-gate /* 14230Sstevel@tonic-gate * If this property exists, this nexus has hot-plug 14240Sstevel@tonic-gate * slots. 14250Sstevel@tonic-gate */ 14260Sstevel@tonic-gate if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 14270Sstevel@tonic-gate "hotplug-capable")) { 14280Sstevel@tonic-gate if (ndi_prop_update_int_array(DDI_DEV_T_NONE, 14290Sstevel@tonic-gate dip, "bus-range", 14300Sstevel@tonic-gate (int *)&bus_range, 14310Sstevel@tonic-gate 2) != DDI_PROP_SUCCESS) { 14320Sstevel@tonic-gate return; 14330Sstevel@tonic-gate } 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate if (pcihp_init(dip) != DDI_SUCCESS) { 14360Sstevel@tonic-gate return; 14370Sstevel@tonic-gate } 14380Sstevel@tonic-gate 14390Sstevel@tonic-gate if (ops = pcihp_get_cb_ops()) { 14400Sstevel@tonic-gate pci_ops.devo_cb_ops = ops; 14410Sstevel@tonic-gate DEBUG2(DBG_ATTACH, dip, "%s%d hotplug enabled", 14420Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 14430Sstevel@tonic-gate } else { 14440Sstevel@tonic-gate return; 14450Sstevel@tonic-gate } 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate pci_p->hotplug_capable = B_TRUE; 14480Sstevel@tonic-gate } 14490Sstevel@tonic-gate } 1450