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 51865Sdilpreet * Common Development and Distribution License (the "License"). 61865Sdilpreet * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*10053SEvan.Yan@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* 280Sstevel@tonic-gate * PCI nexus driver interface 290Sstevel@tonic-gate */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <sys/types.h> 320Sstevel@tonic-gate #include <sys/conf.h> /* nulldev */ 330Sstevel@tonic-gate #include <sys/stat.h> /* devctl */ 340Sstevel@tonic-gate #include <sys/kmem.h> 350Sstevel@tonic-gate #include <sys/async.h> /* ecc_flt for pci_ecc.h */ 360Sstevel@tonic-gate #include <sys/sunddi.h> 370Sstevel@tonic-gate #include <sys/sunndi.h> 380Sstevel@tonic-gate #include <sys/ndifm.h> 390Sstevel@tonic-gate #include <sys/ontrap.h> 400Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 410Sstevel@tonic-gate #include <sys/ddi_subrdefs.h> 420Sstevel@tonic-gate #include <sys/epm.h> 430Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h> 44777Sschwartz #include <sys/pci/pci_tools_ext.h> 450Sstevel@tonic-gate #include <sys/spl.h> 460Sstevel@tonic-gate #include <sys/pci/pci_obj.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate /*LINTLIBRARY*/ 490Sstevel@tonic-gate 500Sstevel@tonic-gate /* 510Sstevel@tonic-gate * function prototype for hotplug routine: 520Sstevel@tonic-gate */ 530Sstevel@tonic-gate static void 540Sstevel@tonic-gate pci_init_hotplug(struct pci *); 550Sstevel@tonic-gate 560Sstevel@tonic-gate /* 570Sstevel@tonic-gate * function prototypes for dev ops routines: 580Sstevel@tonic-gate */ 590Sstevel@tonic-gate static int pci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 600Sstevel@tonic-gate static int pci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 610Sstevel@tonic-gate static int pci_info(dev_info_t *dip, ddi_info_cmd_t infocmd, 620Sstevel@tonic-gate void *arg, void **result); 630Sstevel@tonic-gate static int pci_ctlops_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args); 640Sstevel@tonic-gate static int pci_ctlops_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args, 650Sstevel@tonic-gate void *result); 660Sstevel@tonic-gate static off_t get_reg_set_size(dev_info_t *child, int rnumber); 670Sstevel@tonic-gate 680Sstevel@tonic-gate /* 690Sstevel@tonic-gate * bus ops and dev ops structures: 700Sstevel@tonic-gate */ 710Sstevel@tonic-gate static struct bus_ops pci_bus_ops = { 720Sstevel@tonic-gate BUSO_REV, 730Sstevel@tonic-gate pci_map, 740Sstevel@tonic-gate 0, 750Sstevel@tonic-gate 0, 760Sstevel@tonic-gate 0, 770Sstevel@tonic-gate i_ddi_map_fault, 780Sstevel@tonic-gate pci_dma_setup, 790Sstevel@tonic-gate pci_dma_allochdl, 800Sstevel@tonic-gate pci_dma_freehdl, 810Sstevel@tonic-gate pci_dma_bindhdl, 820Sstevel@tonic-gate pci_dma_unbindhdl, 830Sstevel@tonic-gate pci_dma_sync, 840Sstevel@tonic-gate pci_dma_win, 850Sstevel@tonic-gate pci_dma_ctlops, 860Sstevel@tonic-gate pci_ctlops, 870Sstevel@tonic-gate ddi_bus_prop_op, 880Sstevel@tonic-gate ndi_busop_get_eventcookie, /* (*bus_get_eventcookie)(); */ 890Sstevel@tonic-gate ndi_busop_add_eventcall, /* (*bus_add_eventcall)(); */ 900Sstevel@tonic-gate ndi_busop_remove_eventcall, /* (*bus_remove_eventcall)(); */ 910Sstevel@tonic-gate ndi_post_event, /* (*bus_post_event)(); */ 920Sstevel@tonic-gate NULL, /* (*bus_intr_ctl)(); */ 930Sstevel@tonic-gate NULL, /* (*bus_config)(); */ 940Sstevel@tonic-gate NULL, /* (*bus_unconfig)(); */ 950Sstevel@tonic-gate pci_fm_init_child, /* (*bus_fm_init)(); */ 960Sstevel@tonic-gate NULL, /* (*bus_fm_fini)(); */ 970Sstevel@tonic-gate pci_bus_enter, /* (*bus_fm_access_enter)(); */ 980Sstevel@tonic-gate pci_bus_exit, /* (*bus_fm_access_fini)(); */ 990Sstevel@tonic-gate NULL, /* (*bus_power)(); */ 1000Sstevel@tonic-gate pci_intr_ops /* (*bus_intr_op)(); */ 1010Sstevel@tonic-gate }; 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate extern struct cb_ops pci_cb_ops; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate static struct dev_ops pci_ops = { 1060Sstevel@tonic-gate DEVO_REV, 1070Sstevel@tonic-gate 0, 1080Sstevel@tonic-gate pci_info, 1090Sstevel@tonic-gate nulldev, 1100Sstevel@tonic-gate 0, 1110Sstevel@tonic-gate pci_attach, 1120Sstevel@tonic-gate pci_detach, 1130Sstevel@tonic-gate nodev, 1140Sstevel@tonic-gate &pci_cb_ops, 1150Sstevel@tonic-gate &pci_bus_ops, 1167656SSherry.Moore@Sun.COM 0, 1177656SSherry.Moore@Sun.COM ddi_quiesce_not_supported, /* devo_quiesce */ 1180Sstevel@tonic-gate }; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * module definitions: 1220Sstevel@tonic-gate */ 1230Sstevel@tonic-gate #include <sys/modctl.h> 1240Sstevel@tonic-gate extern struct mod_ops mod_driverops; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate static struct modldrv modldrv = { 1270Sstevel@tonic-gate &mod_driverops, /* Type of module - driver */ 1287656SSherry.Moore@Sun.COM "PCI Bus nexus driver", /* Name of module. */ 1290Sstevel@tonic-gate &pci_ops, /* driver ops */ 1300Sstevel@tonic-gate }; 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate static struct modlinkage modlinkage = { 1330Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL 1340Sstevel@tonic-gate }; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* 1370Sstevel@tonic-gate * driver global data: 1380Sstevel@tonic-gate */ 1390Sstevel@tonic-gate void *per_pci_state; /* per-pbm soft state pointer */ 1400Sstevel@tonic-gate void *per_pci_common_state; /* per-psycho soft state pointer */ 1410Sstevel@tonic-gate kmutex_t pci_global_mutex; /* attach/detach common struct lock */ 1420Sstevel@tonic-gate errorq_t *pci_ecc_queue = NULL; /* per-system ecc handling queue */ 1431865Sdilpreet extern errorq_t *pci_target_queue; 144162Sschwartz struct cb_ops *pcihp_ops = NULL; /* hotplug module cb ops */ 145162Sschwartz 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, 1657656SSherry.Moore@Sun.COM 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: 258946Smathue *result = (void *)(uintptr_t)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", 2927656SSherry.Moore@Sun.COM 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 /* 337117Sschwartz * Create pcitool nodes for register access and interrupt 338117Sschwartz * routing. 339117Sschwartz */ 340117Sschwartz if (pcitool_init(dip) != DDI_SUCCESS) { 341117Sschwartz goto err_bad_pcitool_nodes; 342117Sschwartz } 343117Sschwartz 344117Sschwartz /* 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 357117Sschwartz err_bad_pcitool_nodes: 358117Sschwartz if (pci_p->hotplug_capable == B_FALSE) 359117Sschwartz ddi_remove_minor_node(dip, "devctl"); 360117Sschwartz else 361117Sschwartz (void) pcihp_uninit(dip); 362117Sschwartz err_bad_devctl_node: 363117Sschwartz 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 437117Sschwartz pcitool_uninit(dip); 438117Sschwartz 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 4517656SSherry.Moore@Sun.COM /* Free the interrupt-priorities prop if we created it. */ 4527656SSherry.Moore@Sun.COM { 4530Sstevel@tonic-gate int len; 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate if (ddi_getproplen(DDI_DEV_T_ANY, dip, 4560Sstevel@tonic-gate DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 4570Sstevel@tonic-gate "interrupt-priorities", &len) == DDI_PROP_SUCCESS) 4580Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 4590Sstevel@tonic-gate "interrupt-priorities"); 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate return (DDI_SUCCESS); 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate case DDI_SUSPEND: 4640Sstevel@tonic-gate pci_child_cfg_save(dip); 4650Sstevel@tonic-gate pci_obj_suspend(pci_p); 4660Sstevel@tonic-gate pci_p->pci_state = PCI_SUSPENDED; 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 4690Sstevel@tonic-gate return (DDI_SUCCESS); 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate default: 4720Sstevel@tonic-gate DEBUG0(DBG_DETACH, dip, "unsupported detach op\n"); 4730Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 4740Sstevel@tonic-gate return (DDI_FAILURE); 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate /* bus driver entry points */ 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate /* 4820Sstevel@tonic-gate * bus map entry point: 4830Sstevel@tonic-gate * 4840Sstevel@tonic-gate * if map request is for an rnumber 4850Sstevel@tonic-gate * get the corresponding regspec from device node 4860Sstevel@tonic-gate * build a new regspec in our parent's format 4870Sstevel@tonic-gate * build a new map_req with the new regspec 4880Sstevel@tonic-gate * call up the tree to complete the mapping 4890Sstevel@tonic-gate */ 4900Sstevel@tonic-gate int 4910Sstevel@tonic-gate pci_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp, 4920Sstevel@tonic-gate off_t off, off_t len, caddr_t *addrp) 4930Sstevel@tonic-gate { 4940Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 4950Sstevel@tonic-gate struct regspec p_regspec; 4960Sstevel@tonic-gate ddi_map_req_t p_mapreq; 4970Sstevel@tonic-gate int reglen, rval, r_no; 4980Sstevel@tonic-gate pci_regspec_t reloc_reg, *rp = &reloc_reg; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate DEBUG2(DBG_MAP, dip, "rdip=%s%d:", 5017656SSherry.Moore@Sun.COM ddi_driver_name(rdip), ddi_get_instance(rdip)); 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate if (mp->map_flags & DDI_MF_USER_MAPPING) 5040Sstevel@tonic-gate return (DDI_ME_UNIMPLEMENTED); 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate switch (mp->map_type) { 5070Sstevel@tonic-gate case DDI_MT_REGSPEC: 5080Sstevel@tonic-gate reloc_reg = *(pci_regspec_t *)mp->map_obj.rp; /* dup whole */ 5090Sstevel@tonic-gate break; 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate case DDI_MT_RNUMBER: 5120Sstevel@tonic-gate r_no = mp->map_obj.rnumber; 5130Sstevel@tonic-gate DEBUG1(DBG_MAP | DBG_CONT, dip, " r#=%x", r_no); 5140Sstevel@tonic-gate 515506Scth if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS, 5167656SSherry.Moore@Sun.COM "reg", (caddr_t)&rp, ®len) != DDI_SUCCESS) 5170Sstevel@tonic-gate return (DDI_ME_RNUMBER_RANGE); 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate if (r_no < 0 || r_no >= reglen / sizeof (pci_regspec_t)) { 5200Sstevel@tonic-gate kmem_free(rp, reglen); 5210Sstevel@tonic-gate return (DDI_ME_RNUMBER_RANGE); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate rp += r_no; 5240Sstevel@tonic-gate break; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate default: 5270Sstevel@tonic-gate return (DDI_ME_INVAL); 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate DEBUG0(DBG_MAP | DBG_CONT, dip, "\n"); 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate /* use "assigned-addresses" to relocate regspec within pci space */ 5320Sstevel@tonic-gate if (rval = pci_reloc_reg(dip, rdip, pci_p, rp)) 5330Sstevel@tonic-gate goto done; 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate if (len) /* adjust regspec according to mapping request */ 5360Sstevel@tonic-gate rp->pci_size_low = len; 5370Sstevel@tonic-gate rp->pci_phys_low += off; 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate /* use "ranges" to translate relocated pci regspec into parent space */ 5400Sstevel@tonic-gate if (rval = pci_xlate_reg(pci_p, rp, &p_regspec)) 5410Sstevel@tonic-gate goto done; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate p_mapreq = *mp; /* dup the whole structure */ 5440Sstevel@tonic-gate p_mapreq.map_type = DDI_MT_REGSPEC; 5450Sstevel@tonic-gate p_mapreq.map_obj.rp = &p_regspec; 5460Sstevel@tonic-gate rval = ddi_map(dip, &p_mapreq, 0, 0, addrp); 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate if (rval == DDI_SUCCESS) { 5490Sstevel@tonic-gate /* 5500Sstevel@tonic-gate * Set-up access functions for FM access error capable drivers. 5510Sstevel@tonic-gate * The axq workaround prevents fault management support 5520Sstevel@tonic-gate */ 5530Sstevel@tonic-gate if (DDI_FM_ACC_ERR_CAP(pci_p->pci_fm_cap) && 5540Sstevel@tonic-gate DDI_FM_ACC_ERR_CAP(ddi_fm_capable(rdip)) && 5550Sstevel@tonic-gate mp->map_handlep->ah_acc.devacc_attr_access != 5560Sstevel@tonic-gate DDI_DEFAULT_ACC) 5570Sstevel@tonic-gate pci_fm_acc_setup(mp, rdip); 5580Sstevel@tonic-gate pci_axq_setup(mp, pci_p->pci_pbm_p); 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate done: 5620Sstevel@tonic-gate if (mp->map_type == DDI_MT_RNUMBER) 5630Sstevel@tonic-gate kmem_free(rp - r_no, reglen); 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate return (rval); 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate /* 5690Sstevel@tonic-gate * bus dma map entry point 5700Sstevel@tonic-gate * return value: 5710Sstevel@tonic-gate * DDI_DMA_PARTIAL_MAP 1 5720Sstevel@tonic-gate * DDI_DMA_MAPOK 0 5730Sstevel@tonic-gate * DDI_DMA_MAPPED 0 5740Sstevel@tonic-gate * DDI_DMA_NORESOURCES -1 5750Sstevel@tonic-gate * DDI_DMA_NOMAPPING -2 5760Sstevel@tonic-gate * DDI_DMA_TOOBIG -3 5770Sstevel@tonic-gate */ 5780Sstevel@tonic-gate int 5790Sstevel@tonic-gate pci_dma_setup(dev_info_t *dip, dev_info_t *rdip, ddi_dma_req_t *dmareq, 5800Sstevel@tonic-gate ddi_dma_handle_t *handlep) 5810Sstevel@tonic-gate { 5820Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 5830Sstevel@tonic-gate iommu_t *iommu_p = pci_p->pci_iommu_p; 5840Sstevel@tonic-gate ddi_dma_impl_t *mp; 5850Sstevel@tonic-gate int ret; 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate DEBUG3(DBG_DMA_MAP, dip, "mapping - rdip=%s%d type=%s\n", 5887656SSherry.Moore@Sun.COM ddi_driver_name(rdip), ddi_get_instance(rdip), 5897656SSherry.Moore@Sun.COM handlep ? "alloc" : "advisory"); 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate if (!(mp = pci_dma_lmts2hdl(dip, rdip, iommu_p, dmareq))) 5920Sstevel@tonic-gate return (DDI_DMA_NORESOURCES); 5930Sstevel@tonic-gate if (mp == (ddi_dma_impl_t *)DDI_DMA_NOMAPPING) 5940Sstevel@tonic-gate return (DDI_DMA_NOMAPPING); 5950Sstevel@tonic-gate if (ret = pci_dma_type(pci_p, dmareq, mp)) 5960Sstevel@tonic-gate goto freehandle; 5970Sstevel@tonic-gate if (ret = pci_dma_pfn(pci_p, dmareq, mp)) 5980Sstevel@tonic-gate goto freehandle; 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate switch (PCI_DMA_TYPE(mp)) { 6010Sstevel@tonic-gate case DMAI_FLAGS_DVMA: /* LINTED E_EQUALITY_NOT_ASSIGNMENT */ 6020Sstevel@tonic-gate if ((ret = pci_dvma_win(pci_p, dmareq, mp)) || !handlep) 6030Sstevel@tonic-gate goto freehandle; 6040Sstevel@tonic-gate if (!PCI_DMA_CANCACHE(mp)) { /* try fast track */ 6050Sstevel@tonic-gate if (PCI_DMA_CANFAST(mp)) { 6060Sstevel@tonic-gate if (!pci_dvma_map_fast(iommu_p, mp)) 6070Sstevel@tonic-gate break; 6080Sstevel@tonic-gate /* LINTED E_NOP_ELSE_STMT */ 6090Sstevel@tonic-gate } else { 6100Sstevel@tonic-gate PCI_DVMA_FASTTRAK_PROF(mp); 6110Sstevel@tonic-gate } 6120Sstevel@tonic-gate } 6130Sstevel@tonic-gate if (ret = pci_dvma_map(mp, dmareq, iommu_p)) 6140Sstevel@tonic-gate goto freehandle; 6150Sstevel@tonic-gate break; 6160Sstevel@tonic-gate case DMAI_FLAGS_PEER_TO_PEER: /* LINTED E_EQUALITY_NOT_ASSIGNMENT */ 6170Sstevel@tonic-gate if ((ret = pci_dma_physwin(pci_p, dmareq, mp)) || !handlep) 6180Sstevel@tonic-gate goto freehandle; 6190Sstevel@tonic-gate break; 6200Sstevel@tonic-gate case DMAI_FLAGS_BYPASS: 6210Sstevel@tonic-gate default: 6220Sstevel@tonic-gate panic("%s%d: pci_dma_setup: bad dma type 0x%x", 6230Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), 6240Sstevel@tonic-gate PCI_DMA_TYPE(mp)); 6250Sstevel@tonic-gate /*NOTREACHED*/ 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate *handlep = (ddi_dma_handle_t)mp; 6280Sstevel@tonic-gate mp->dmai_flags |= (DMAI_FLAGS_INUSE | DMAI_FLAGS_MAPPED); 6290Sstevel@tonic-gate dump_dma_handle(DBG_DMA_MAP, dip, mp); 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate return ((mp->dmai_nwin == 1) ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP); 6320Sstevel@tonic-gate freehandle: 6330Sstevel@tonic-gate if (ret == DDI_DMA_NORESOURCES) 6340Sstevel@tonic-gate pci_dma_freemp(mp); /* don't run_callback() */ 6350Sstevel@tonic-gate else 6360Sstevel@tonic-gate (void) pci_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp); 6370Sstevel@tonic-gate return (ret); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * bus dma alloc handle entry point: 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate int 6450Sstevel@tonic-gate pci_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attrp, 6460Sstevel@tonic-gate int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep) 6470Sstevel@tonic-gate { 6480Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 6490Sstevel@tonic-gate ddi_dma_impl_t *mp; 6500Sstevel@tonic-gate int rval; 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate DEBUG2(DBG_DMA_ALLOCH, dip, "rdip=%s%d\n", 6537656SSherry.Moore@Sun.COM ddi_driver_name(rdip), ddi_get_instance(rdip)); 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate if (attrp->dma_attr_version != DMA_ATTR_V0) 6560Sstevel@tonic-gate return (DDI_DMA_BADATTR); 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate if (!(mp = pci_dma_allocmp(dip, rdip, waitfp, arg))) 6590Sstevel@tonic-gate return (DDI_DMA_NORESOURCES); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate /* 6620Sstevel@tonic-gate * Save requestor's information 6630Sstevel@tonic-gate */ 6640Sstevel@tonic-gate mp->dmai_attr = *attrp; /* whole object - augmented later */ 6650Sstevel@tonic-gate *DEV_ATTR(mp) = *attrp; /* whole object - device orig attr */ 6660Sstevel@tonic-gate DEBUG1(DBG_DMA_ALLOCH, dip, "mp=%p\n", mp); 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate /* check and convert dma attributes to handle parameters */ 6690Sstevel@tonic-gate if (rval = pci_dma_attr2hdl(pci_p, mp)) { 6700Sstevel@tonic-gate pci_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp); 6710Sstevel@tonic-gate *handlep = NULL; 6720Sstevel@tonic-gate return (rval); 6730Sstevel@tonic-gate } 6740Sstevel@tonic-gate *handlep = (ddi_dma_handle_t)mp; 6750Sstevel@tonic-gate return (DDI_SUCCESS); 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate /* 6800Sstevel@tonic-gate * bus dma free handle entry point: 6810Sstevel@tonic-gate */ 6820Sstevel@tonic-gate /*ARGSUSED*/ 6830Sstevel@tonic-gate int 6840Sstevel@tonic-gate pci_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle) 6850Sstevel@tonic-gate { 6860Sstevel@tonic-gate DEBUG3(DBG_DMA_FREEH, dip, "rdip=%s%d mp=%p\n", 6877656SSherry.Moore@Sun.COM ddi_driver_name(rdip), ddi_get_instance(rdip), handle); 6880Sstevel@tonic-gate pci_dma_freemp((ddi_dma_impl_t *)handle); 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate if (pci_kmem_clid) { 6910Sstevel@tonic-gate DEBUG0(DBG_DMA_FREEH, dip, "run handle callback\n"); 6920Sstevel@tonic-gate ddi_run_callback(&pci_kmem_clid); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate return (DDI_SUCCESS); 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate /* 6990Sstevel@tonic-gate * bus dma bind handle entry point: 7000Sstevel@tonic-gate */ 7010Sstevel@tonic-gate int 7020Sstevel@tonic-gate pci_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip, 7030Sstevel@tonic-gate ddi_dma_handle_t handle, ddi_dma_req_t *dmareq, 7040Sstevel@tonic-gate ddi_dma_cookie_t *cookiep, uint_t *ccountp) 7050Sstevel@tonic-gate { 7060Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 7070Sstevel@tonic-gate iommu_t *iommu_p = pci_p->pci_iommu_p; 7080Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle; 7090Sstevel@tonic-gate int ret; 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate DEBUG4(DBG_DMA_BINDH, dip, "rdip=%s%d mp=%p dmareq=%p\n", 7127656SSherry.Moore@Sun.COM ddi_driver_name(rdip), ddi_get_instance(rdip), mp, dmareq); 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate if (mp->dmai_flags & DMAI_FLAGS_INUSE) 7150Sstevel@tonic-gate return (DDI_DMA_INUSE); 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate ASSERT((mp->dmai_flags & ~DMAI_FLAGS_PRESERVE) == 0); 7180Sstevel@tonic-gate mp->dmai_flags |= DMAI_FLAGS_INUSE; 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate if (ret = pci_dma_type(pci_p, dmareq, mp)) 7210Sstevel@tonic-gate goto err; 7220Sstevel@tonic-gate if (ret = pci_dma_pfn(pci_p, dmareq, mp)) 7230Sstevel@tonic-gate goto err; 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate switch (PCI_DMA_TYPE(mp)) { 7260Sstevel@tonic-gate case DMAI_FLAGS_DVMA: 7270Sstevel@tonic-gate if (ret = pci_dvma_win(pci_p, dmareq, mp)) 7280Sstevel@tonic-gate goto map_err; 7290Sstevel@tonic-gate if (!PCI_DMA_CANCACHE(mp)) { /* try fast track */ 7300Sstevel@tonic-gate if (PCI_DMA_CANFAST(mp)) { 7310Sstevel@tonic-gate if (!pci_dvma_map_fast(iommu_p, mp)) 7320Sstevel@tonic-gate goto mapped; /*LINTED E_NOP_ELSE_STMT*/ 7330Sstevel@tonic-gate } else { 7340Sstevel@tonic-gate PCI_DVMA_FASTTRAK_PROF(mp); 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate if (ret = pci_dvma_map(mp, dmareq, iommu_p)) 7380Sstevel@tonic-gate goto map_err; 7390Sstevel@tonic-gate mapped: 7400Sstevel@tonic-gate *ccountp = 1; 7410Sstevel@tonic-gate MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, mp->dmai_size); 7420Sstevel@tonic-gate break; 7430Sstevel@tonic-gate case DMAI_FLAGS_BYPASS: 7440Sstevel@tonic-gate case DMAI_FLAGS_PEER_TO_PEER: 7450Sstevel@tonic-gate if (ret = pci_dma_physwin(pci_p, dmareq, mp)) 7460Sstevel@tonic-gate goto map_err; 7470Sstevel@tonic-gate *ccountp = WINLST(mp)->win_ncookies; 7480Sstevel@tonic-gate *cookiep = *(ddi_dma_cookie_t *)(WINLST(mp) + 1); /* wholeobj */ 7490Sstevel@tonic-gate break; 7500Sstevel@tonic-gate default: 7510Sstevel@tonic-gate panic("%s%d: pci_dma_bindhdl(%p): bad dma type", 7520Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), mp); 7530Sstevel@tonic-gate /*NOTREACHED*/ 7540Sstevel@tonic-gate } 7550Sstevel@tonic-gate DEBUG2(DBG_DMA_BINDH, dip, "cookie %x+%x\n", cookiep->dmac_address, 7560Sstevel@tonic-gate cookiep->dmac_size); 7570Sstevel@tonic-gate dump_dma_handle(DBG_DMA_MAP, dip, mp); 7580Sstevel@tonic-gate 7591865Sdilpreet if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) { 7600Sstevel@tonic-gate (void) ndi_fmc_insert(rdip, DMA_HANDLE, mp, NULL); 7611865Sdilpreet mp->dmai_error.err_cf = impl_dma_check; 7621865Sdilpreet } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate mp->dmai_flags |= DMAI_FLAGS_MAPPED; 7650Sstevel@tonic-gate return (mp->dmai_nwin == 1 ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP); 7660Sstevel@tonic-gate map_err: 7670Sstevel@tonic-gate pci_dvma_unregister_callbacks(pci_p, mp); 7680Sstevel@tonic-gate pci_dma_freepfn(mp); 7690Sstevel@tonic-gate err: 7700Sstevel@tonic-gate mp->dmai_flags &= DMAI_FLAGS_PRESERVE; 7710Sstevel@tonic-gate return (ret); 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate /* 7750Sstevel@tonic-gate * bus dma unbind handle entry point: 7760Sstevel@tonic-gate */ 7770Sstevel@tonic-gate /*ARGSUSED*/ 7780Sstevel@tonic-gate int 7790Sstevel@tonic-gate pci_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle) 7800Sstevel@tonic-gate { 7810Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle; 7820Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 7830Sstevel@tonic-gate iommu_t *iommu_p = pci_p->pci_iommu_p; 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate DEBUG3(DBG_DMA_UNBINDH, dip, "rdip=%s%d, mp=%p\n", 7867656SSherry.Moore@Sun.COM ddi_driver_name(rdip), ddi_get_instance(rdip), handle); 7870Sstevel@tonic-gate if ((mp->dmai_flags & DMAI_FLAGS_INUSE) == 0) { 7880Sstevel@tonic-gate DEBUG0(DBG_DMA_UNBINDH, dip, "handle not in use\n"); 7890Sstevel@tonic-gate return (DDI_FAILURE); 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate mp->dmai_flags &= ~DMAI_FLAGS_MAPPED; 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate switch (PCI_DMA_TYPE(mp)) { 7950Sstevel@tonic-gate case DMAI_FLAGS_DVMA: 7960Sstevel@tonic-gate pci_dvma_unregister_callbacks(pci_p, mp); 7970Sstevel@tonic-gate pci_dma_sync_unmap(dip, rdip, mp); 7980Sstevel@tonic-gate pci_dvma_unmap(iommu_p, mp); 7990Sstevel@tonic-gate pci_dma_freepfn(mp); 8000Sstevel@tonic-gate break; 8010Sstevel@tonic-gate case DMAI_FLAGS_BYPASS: 8020Sstevel@tonic-gate case DMAI_FLAGS_PEER_TO_PEER: 8030Sstevel@tonic-gate pci_dma_freewin(mp); 8040Sstevel@tonic-gate break; 8050Sstevel@tonic-gate default: 8060Sstevel@tonic-gate panic("%s%d: pci_dma_unbindhdl:bad dma type %p", 8070Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), mp); 8080Sstevel@tonic-gate /*NOTREACHED*/ 8090Sstevel@tonic-gate } 8100Sstevel@tonic-gate if (iommu_p->iommu_dvma_clid != 0) { 8110Sstevel@tonic-gate DEBUG0(DBG_DMA_UNBINDH, dip, "run dvma callback\n"); 8120Sstevel@tonic-gate ddi_run_callback(&iommu_p->iommu_dvma_clid); 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate if (pci_kmem_clid) { 8150Sstevel@tonic-gate DEBUG0(DBG_DMA_UNBINDH, dip, "run handle callback\n"); 8160Sstevel@tonic-gate ddi_run_callback(&pci_kmem_clid); 8170Sstevel@tonic-gate } 8180Sstevel@tonic-gate mp->dmai_flags &= DMAI_FLAGS_PRESERVE; 8190Sstevel@tonic-gate SYNC_BUF_PA(mp) = 0; 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) { 8220Sstevel@tonic-gate if (DEVI(rdip)->devi_fmhdl != NULL && 8230Sstevel@tonic-gate DDI_FM_DMA_ERR_CAP(DEVI(rdip)->devi_fmhdl->fh_cap)) { 8240Sstevel@tonic-gate (void) ndi_fmc_remove(rdip, DMA_HANDLE, mp); 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate return (DDI_SUCCESS); 8290Sstevel@tonic-gate } 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate /* 8330Sstevel@tonic-gate * bus dma win entry point: 8340Sstevel@tonic-gate */ 8350Sstevel@tonic-gate int 8360Sstevel@tonic-gate pci_dma_win(dev_info_t *dip, dev_info_t *rdip, 8370Sstevel@tonic-gate ddi_dma_handle_t handle, uint_t win, off_t *offp, 8380Sstevel@tonic-gate size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp) 8390Sstevel@tonic-gate { 8400Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle; 8410Sstevel@tonic-gate DEBUG2(DBG_DMA_WIN, dip, "rdip=%s%d\n", 8420Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip)); 8430Sstevel@tonic-gate dump_dma_handle(DBG_DMA_WIN, dip, mp); 8440Sstevel@tonic-gate if (win >= mp->dmai_nwin) { 8450Sstevel@tonic-gate DEBUG1(DBG_DMA_WIN, dip, "%x out of range\n", win); 8460Sstevel@tonic-gate return (DDI_FAILURE); 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate switch (PCI_DMA_TYPE(mp)) { 8500Sstevel@tonic-gate case DMAI_FLAGS_DVMA: 8510Sstevel@tonic-gate if (win != PCI_DMA_CURWIN(mp)) { 8520Sstevel@tonic-gate pci_t *pci_p = 8537656SSherry.Moore@Sun.COM get_pci_soft_state(ddi_get_instance(dip)); 8540Sstevel@tonic-gate pci_dma_sync_unmap(dip, rdip, mp); 8550Sstevel@tonic-gate /* map_window sets dmai_mapping/size/offset */ 8560Sstevel@tonic-gate iommu_map_window(pci_p->pci_iommu_p, mp, win); 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate if (cookiep) 8590Sstevel@tonic-gate MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, 8607656SSherry.Moore@Sun.COM mp->dmai_size); 8610Sstevel@tonic-gate if (ccountp) 8620Sstevel@tonic-gate *ccountp = 1; 8630Sstevel@tonic-gate break; 8640Sstevel@tonic-gate case DMAI_FLAGS_PEER_TO_PEER: 8650Sstevel@tonic-gate case DMAI_FLAGS_BYPASS: { 8660Sstevel@tonic-gate int i; 8670Sstevel@tonic-gate ddi_dma_cookie_t *ck_p; 8680Sstevel@tonic-gate pci_dma_win_t *win_p = mp->dmai_winlst; 8690Sstevel@tonic-gate 8707656SSherry.Moore@Sun.COM for (i = 0; i < win; win_p = win_p->win_next, i++) 8717656SSherry.Moore@Sun.COM ; 8720Sstevel@tonic-gate ck_p = (ddi_dma_cookie_t *)(win_p + 1); 8730Sstevel@tonic-gate *cookiep = *ck_p; 8740Sstevel@tonic-gate mp->dmai_offset = win_p->win_offset; 8750Sstevel@tonic-gate mp->dmai_size = win_p->win_size; 8760Sstevel@tonic-gate mp->dmai_mapping = ck_p->dmac_laddress; 8770Sstevel@tonic-gate mp->dmai_cookie = ck_p + 1; 8780Sstevel@tonic-gate win_p->win_curseg = 0; 8790Sstevel@tonic-gate if (ccountp) 8800Sstevel@tonic-gate *ccountp = win_p->win_ncookies; 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate break; 8830Sstevel@tonic-gate default: 8840Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: pci_dma_win:bad dma type 0x%x", 8850Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), 8860Sstevel@tonic-gate PCI_DMA_TYPE(mp)); 8870Sstevel@tonic-gate return (DDI_FAILURE); 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate if (cookiep) 8900Sstevel@tonic-gate DEBUG2(DBG_DMA_WIN, dip, 8917656SSherry.Moore@Sun.COM "cookie - dmac_address=%x dmac_size=%x\n", 8927656SSherry.Moore@Sun.COM cookiep->dmac_address, cookiep->dmac_size); 8930Sstevel@tonic-gate if (offp) 8940Sstevel@tonic-gate *offp = (off_t)mp->dmai_offset; 8950Sstevel@tonic-gate if (lenp) 8960Sstevel@tonic-gate *lenp = mp->dmai_size; 8970Sstevel@tonic-gate return (DDI_SUCCESS); 8980Sstevel@tonic-gate } 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate #ifdef DEBUG 9010Sstevel@tonic-gate static char *pci_dmactl_str[] = { 9020Sstevel@tonic-gate "DDI_DMA_FREE", 9030Sstevel@tonic-gate "DDI_DMA_SYNC", 9040Sstevel@tonic-gate "DDI_DMA_HTOC", 9050Sstevel@tonic-gate "DDI_DMA_KVADDR", 9060Sstevel@tonic-gate "DDI_DMA_MOVWIN", 9070Sstevel@tonic-gate "DDI_DMA_REPWIN", 9080Sstevel@tonic-gate "DDI_DMA_GETERR", 9090Sstevel@tonic-gate "DDI_DMA_COFF", 9100Sstevel@tonic-gate "DDI_DMA_NEXTWIN", 9110Sstevel@tonic-gate "DDI_DMA_NEXTSEG", 9120Sstevel@tonic-gate "DDI_DMA_SEGTOC", 9130Sstevel@tonic-gate "DDI_DMA_RESERVE", 9140Sstevel@tonic-gate "DDI_DMA_RELEASE", 9150Sstevel@tonic-gate "DDI_DMA_RESETH", 9160Sstevel@tonic-gate "DDI_DMA_CKSYNC", 9170Sstevel@tonic-gate "DDI_DMA_IOPB_ALLOC", 9180Sstevel@tonic-gate "DDI_DMA_IOPB_FREE", 9190Sstevel@tonic-gate "DDI_DMA_SMEM_ALLOC", 9200Sstevel@tonic-gate "DDI_DMA_SMEM_FREE", 9210Sstevel@tonic-gate "DDI_DMA_SET_SBUS64", 9220Sstevel@tonic-gate "DDI_DMA_REMAP" 9230Sstevel@tonic-gate }; 9240Sstevel@tonic-gate #endif 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate /* 9270Sstevel@tonic-gate * bus dma control entry point: 9280Sstevel@tonic-gate */ 9290Sstevel@tonic-gate int 9300Sstevel@tonic-gate pci_dma_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle, 9310Sstevel@tonic-gate enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp, 9320Sstevel@tonic-gate uint_t cache_flags) 9330Sstevel@tonic-gate { 9340Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle; 9350Sstevel@tonic-gate DEBUG3(DBG_DMA_CTL, dip, "%s: rdip=%s%d\n", pci_dmactl_str[cmd], 9360Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip)); 9370Sstevel@tonic-gate 9380Sstevel@tonic-gate switch (cmd) { 9390Sstevel@tonic-gate case DDI_DMA_FREE: 9400Sstevel@tonic-gate (void) pci_dma_unbindhdl(dip, rdip, handle); 9410Sstevel@tonic-gate (void) pci_dma_freehdl(dip, rdip, handle); 9420Sstevel@tonic-gate return (DDI_SUCCESS); 9430Sstevel@tonic-gate case DDI_DMA_RESERVE: { 9440Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 9450Sstevel@tonic-gate return (pci_fdvma_reserve(dip, rdip, pci_p, 9467656SSherry.Moore@Sun.COM (ddi_dma_req_t *)offp, (ddi_dma_handle_t *)objp)); 9470Sstevel@tonic-gate } 9480Sstevel@tonic-gate case DDI_DMA_RELEASE: { 9490Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 9500Sstevel@tonic-gate return (pci_fdvma_release(dip, pci_p, mp)); 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate default: 9530Sstevel@tonic-gate break; 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate switch (PCI_DMA_TYPE(mp)) { 9570Sstevel@tonic-gate case DMAI_FLAGS_DVMA: 9580Sstevel@tonic-gate return (pci_dvma_ctl(dip, rdip, mp, cmd, offp, lenp, objp, 9597656SSherry.Moore@Sun.COM cache_flags)); 9600Sstevel@tonic-gate case DMAI_FLAGS_PEER_TO_PEER: 9610Sstevel@tonic-gate case DMAI_FLAGS_BYPASS: 9620Sstevel@tonic-gate return (pci_dma_ctl(dip, rdip, mp, cmd, offp, lenp, objp, 9637656SSherry.Moore@Sun.COM cache_flags)); 9640Sstevel@tonic-gate default: 9650Sstevel@tonic-gate panic("%s%d: pci_dma_ctlops(%x):bad dma type %x", 9660Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), cmd, 9670Sstevel@tonic-gate mp->dmai_flags); 9680Sstevel@tonic-gate /*NOTREACHED*/ 9690Sstevel@tonic-gate } 9700Sstevel@tonic-gate } 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate #ifdef DEBUG 9730Sstevel@tonic-gate int pci_peekfault_cnt = 0; 9740Sstevel@tonic-gate int pci_pokefault_cnt = 0; 9750Sstevel@tonic-gate #endif /* DEBUG */ 9760Sstevel@tonic-gate 9770Sstevel@tonic-gate static int 9780Sstevel@tonic-gate pci_do_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args) 9790Sstevel@tonic-gate { 9800Sstevel@tonic-gate pbm_t *pbm_p = pci_p->pci_pbm_p; 9810Sstevel@tonic-gate int err = DDI_SUCCESS; 9820Sstevel@tonic-gate on_trap_data_t otd; 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate mutex_enter(&pbm_p->pbm_pokefault_mutex); 9850Sstevel@tonic-gate pbm_p->pbm_ontrap_data = &otd; 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate /* Set up protected environment. */ 9880Sstevel@tonic-gate if (!on_trap(&otd, OT_DATA_ACCESS)) { 9890Sstevel@tonic-gate uintptr_t tramp = otd.ot_trampoline; 9900Sstevel@tonic-gate 9910Sstevel@tonic-gate otd.ot_trampoline = (uintptr_t)&poke_fault; 9920Sstevel@tonic-gate err = do_poke(in_args->size, (void *)in_args->dev_addr, 9930Sstevel@tonic-gate (void *)in_args->host_addr); 9940Sstevel@tonic-gate otd.ot_trampoline = tramp; 9950Sstevel@tonic-gate } else 9960Sstevel@tonic-gate err = DDI_FAILURE; 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate /* 9990Sstevel@tonic-gate * Read the async fault register for the PBM to see it sees 10000Sstevel@tonic-gate * a master-abort. 10010Sstevel@tonic-gate */ 10020Sstevel@tonic-gate pbm_clear_error(pbm_p); 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate if (otd.ot_trap & OT_DATA_ACCESS) 10050Sstevel@tonic-gate err = DDI_FAILURE; 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate /* Take down protected environment. */ 10080Sstevel@tonic-gate no_trap(); 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate pbm_p->pbm_ontrap_data = NULL; 10110Sstevel@tonic-gate mutex_exit(&pbm_p->pbm_pokefault_mutex); 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate #ifdef DEBUG 10140Sstevel@tonic-gate if (err == DDI_FAILURE) 10150Sstevel@tonic-gate pci_pokefault_cnt++; 10160Sstevel@tonic-gate #endif 10170Sstevel@tonic-gate return (err); 10180Sstevel@tonic-gate } 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate static int 10220Sstevel@tonic-gate pci_do_caut_put(pci_t *pci_p, peekpoke_ctlops_t *cautacc_ctlops_arg) 10230Sstevel@tonic-gate { 10240Sstevel@tonic-gate size_t size = cautacc_ctlops_arg->size; 10250Sstevel@tonic-gate uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr; 10260Sstevel@tonic-gate uintptr_t host_addr = cautacc_ctlops_arg->host_addr; 10270Sstevel@tonic-gate ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle; 10280Sstevel@tonic-gate size_t repcount = cautacc_ctlops_arg->repcount; 10290Sstevel@tonic-gate uint_t flags = cautacc_ctlops_arg->flags; 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED; 10320Sstevel@tonic-gate 10330Sstevel@tonic-gate /* 10340Sstevel@tonic-gate * Note that i_ndi_busop_access_enter ends up grabbing the pokefault 10350Sstevel@tonic-gate * mutex. 10360Sstevel@tonic-gate */ 10370Sstevel@tonic-gate i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp); 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) { 10400Sstevel@tonic-gate for (; repcount; repcount--) { 10410Sstevel@tonic-gate switch (size) { 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate case sizeof (uint8_t): 10440Sstevel@tonic-gate i_ddi_put8(hp, (uint8_t *)dev_addr, 10450Sstevel@tonic-gate *(uint8_t *)host_addr); 10460Sstevel@tonic-gate break; 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate case sizeof (uint16_t): 10490Sstevel@tonic-gate i_ddi_put16(hp, (uint16_t *)dev_addr, 10500Sstevel@tonic-gate *(uint16_t *)host_addr); 10510Sstevel@tonic-gate break; 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate case sizeof (uint32_t): 10540Sstevel@tonic-gate i_ddi_put32(hp, (uint32_t *)dev_addr, 10550Sstevel@tonic-gate *(uint32_t *)host_addr); 10560Sstevel@tonic-gate break; 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate case sizeof (uint64_t): 10590Sstevel@tonic-gate i_ddi_put64(hp, (uint64_t *)dev_addr, 10600Sstevel@tonic-gate *(uint64_t *)host_addr); 10610Sstevel@tonic-gate break; 10620Sstevel@tonic-gate } 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate host_addr += size; 10650Sstevel@tonic-gate 10660Sstevel@tonic-gate if (flags == DDI_DEV_AUTOINCR) 10670Sstevel@tonic-gate dev_addr += size; 10680Sstevel@tonic-gate 10690Sstevel@tonic-gate } 10700Sstevel@tonic-gate } 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate i_ddi_notrap((ddi_acc_handle_t)hp); 10730Sstevel@tonic-gate i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp); 10740Sstevel@tonic-gate hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED; 10750Sstevel@tonic-gate 1076815Sdilpreet if (hp->ahi_err->err_status != DDI_FM_OK) { 1077815Sdilpreet /* Clear the expected fault from the handle before returning */ 1078815Sdilpreet hp->ahi_err->err_status = DDI_FM_OK; 1079815Sdilpreet return (DDI_FAILURE); 1080815Sdilpreet } 1081815Sdilpreet 1082815Sdilpreet return (DDI_SUCCESS); 10830Sstevel@tonic-gate } 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate static int 10870Sstevel@tonic-gate pci_ctlops_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args) 10880Sstevel@tonic-gate { 10890Sstevel@tonic-gate return (in_args->handle ? pci_do_caut_put(pci_p, in_args) : 10900Sstevel@tonic-gate pci_do_poke(pci_p, in_args)); 10910Sstevel@tonic-gate } 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate 10940Sstevel@tonic-gate static int 10950Sstevel@tonic-gate pci_do_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args) 10960Sstevel@tonic-gate { 10970Sstevel@tonic-gate int err = DDI_SUCCESS; 10980Sstevel@tonic-gate on_trap_data_t otd; 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate if (!on_trap(&otd, OT_DATA_ACCESS)) { 11010Sstevel@tonic-gate uintptr_t tramp = otd.ot_trampoline; 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate otd.ot_trampoline = (uintptr_t)&peek_fault; 11040Sstevel@tonic-gate err = do_peek(in_args->size, (void *)in_args->dev_addr, 11050Sstevel@tonic-gate (void *)in_args->host_addr); 11060Sstevel@tonic-gate otd.ot_trampoline = tramp; 11070Sstevel@tonic-gate } else 11080Sstevel@tonic-gate err = DDI_FAILURE; 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate no_trap(); 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate #ifdef DEBUG 11130Sstevel@tonic-gate if (err == DDI_FAILURE) 11140Sstevel@tonic-gate pci_peekfault_cnt++; 11150Sstevel@tonic-gate #endif 11160Sstevel@tonic-gate return (err); 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate 11190Sstevel@tonic-gate static int 11200Sstevel@tonic-gate pci_do_caut_get(pci_t *pci_p, peekpoke_ctlops_t *cautacc_ctlops_arg) 11210Sstevel@tonic-gate { 11220Sstevel@tonic-gate size_t size = cautacc_ctlops_arg->size; 11230Sstevel@tonic-gate uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr; 11240Sstevel@tonic-gate uintptr_t host_addr = cautacc_ctlops_arg->host_addr; 11250Sstevel@tonic-gate ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle; 11260Sstevel@tonic-gate size_t repcount = cautacc_ctlops_arg->repcount; 11270Sstevel@tonic-gate uint_t flags = cautacc_ctlops_arg->flags; 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate int err = DDI_SUCCESS; 11300Sstevel@tonic-gate 11310Sstevel@tonic-gate hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED; 11320Sstevel@tonic-gate i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp); 11330Sstevel@tonic-gate 1134815Sdilpreet if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) { 1135815Sdilpreet for (; repcount; repcount--) { 11360Sstevel@tonic-gate i_ddi_caut_get(size, (void *)dev_addr, 11370Sstevel@tonic-gate (void *)host_addr); 1138815Sdilpreet 1139815Sdilpreet host_addr += size; 11400Sstevel@tonic-gate 1141815Sdilpreet if (flags == DDI_DEV_AUTOINCR) 1142815Sdilpreet dev_addr += size; 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate } else { 1145815Sdilpreet int i; 1146815Sdilpreet uint8_t *ff_addr = (uint8_t *)host_addr; 1147815Sdilpreet for (i = 0; i < size; i++) 1148815Sdilpreet *ff_addr++ = 0xff; 11490Sstevel@tonic-gate 1150815Sdilpreet err = DDI_FAILURE; 11510Sstevel@tonic-gate } 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate i_ddi_notrap((ddi_acc_handle_t)hp); 11540Sstevel@tonic-gate i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp); 11550Sstevel@tonic-gate hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED; 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate return (err); 11580Sstevel@tonic-gate } 11590Sstevel@tonic-gate 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate static int 11620Sstevel@tonic-gate pci_ctlops_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args, void *result) 11630Sstevel@tonic-gate { 11640Sstevel@tonic-gate result = (void *)in_args->host_addr; 11650Sstevel@tonic-gate return (in_args->handle ? pci_do_caut_get(pci_p, in_args) : 11660Sstevel@tonic-gate pci_do_peek(pci_p, in_args)); 11670Sstevel@tonic-gate } 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate /* 11700Sstevel@tonic-gate * get_reg_set_size 11710Sstevel@tonic-gate * 11720Sstevel@tonic-gate * Given a dev info pointer to a pci child and a register number, this 11730Sstevel@tonic-gate * routine returns the size element of that reg set property. 11740Sstevel@tonic-gate * return value: size of reg set on success, -1 on error 11750Sstevel@tonic-gate */ 11760Sstevel@tonic-gate static off_t 11770Sstevel@tonic-gate get_reg_set_size(dev_info_t *child, int rnumber) 11780Sstevel@tonic-gate { 11790Sstevel@tonic-gate pci_regspec_t *pci_rp; 11800Sstevel@tonic-gate off_t size; 11810Sstevel@tonic-gate int i; 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate if (rnumber < 0) 11840Sstevel@tonic-gate return (-1); 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate /* 11870Sstevel@tonic-gate * Get the reg property for the device. 11880Sstevel@tonic-gate */ 11890Sstevel@tonic-gate if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg", 11900Sstevel@tonic-gate (caddr_t)&pci_rp, &i) != DDI_SUCCESS) 11910Sstevel@tonic-gate return (-1); 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate if (rnumber >= (i / (int)sizeof (pci_regspec_t))) { 11940Sstevel@tonic-gate kmem_free(pci_rp, i); 11950Sstevel@tonic-gate return (-1); 11960Sstevel@tonic-gate } 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate size = pci_rp[rnumber].pci_size_low | 11997656SSherry.Moore@Sun.COM ((uint64_t)pci_rp[rnumber].pci_size_hi << 32); 12000Sstevel@tonic-gate kmem_free(pci_rp, i); 12010Sstevel@tonic-gate return (size); 12020Sstevel@tonic-gate } 12030Sstevel@tonic-gate 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate /* 12060Sstevel@tonic-gate * control ops entry point: 12070Sstevel@tonic-gate * 12080Sstevel@tonic-gate * Requests handled completely: 12090Sstevel@tonic-gate * DDI_CTLOPS_INITCHILD see init_child() for details 12100Sstevel@tonic-gate * DDI_CTLOPS_UNINITCHILD 12110Sstevel@tonic-gate * DDI_CTLOPS_REPORTDEV see report_dev() for details 12120Sstevel@tonic-gate * DDI_CTLOPS_IOMIN cache line size if streaming otherwise 1 12130Sstevel@tonic-gate * DDI_CTLOPS_REGSIZE 12140Sstevel@tonic-gate * DDI_CTLOPS_NREGS 12150Sstevel@tonic-gate * DDI_CTLOPS_DVMAPAGESIZE 12160Sstevel@tonic-gate * DDI_CTLOPS_POKE 12170Sstevel@tonic-gate * DDI_CTLOPS_PEEK 12180Sstevel@tonic-gate * DDI_CTLOPS_QUIESCE 12190Sstevel@tonic-gate * DDI_CTLOPS_UNQUIESCE 12200Sstevel@tonic-gate * 12210Sstevel@tonic-gate * All others passed to parent. 12220Sstevel@tonic-gate */ 12230Sstevel@tonic-gate int 12240Sstevel@tonic-gate pci_ctlops(dev_info_t *dip, dev_info_t *rdip, 12250Sstevel@tonic-gate ddi_ctl_enum_t op, void *arg, void *result) 12260Sstevel@tonic-gate { 12270Sstevel@tonic-gate pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate switch (op) { 12300Sstevel@tonic-gate case DDI_CTLOPS_INITCHILD: 12310Sstevel@tonic-gate return (init_child(pci_p, (dev_info_t *)arg)); 12320Sstevel@tonic-gate 12330Sstevel@tonic-gate case DDI_CTLOPS_UNINITCHILD: 12340Sstevel@tonic-gate return (uninit_child(pci_p, (dev_info_t *)arg)); 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate case DDI_CTLOPS_REPORTDEV: 12370Sstevel@tonic-gate return (report_dev(rdip)); 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate case DDI_CTLOPS_IOMIN: 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate /* 12420Sstevel@tonic-gate * If we are using the streaming cache, align at 12430Sstevel@tonic-gate * least on a cache line boundary. Otherwise use 12440Sstevel@tonic-gate * whatever alignment is passed in. 12450Sstevel@tonic-gate */ 12460Sstevel@tonic-gate 1247946Smathue if ((uintptr_t)arg) { 12480Sstevel@tonic-gate int val = *((int *)result); 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate val = maxbit(val, PCI_SBUF_LINE_SIZE); 12510Sstevel@tonic-gate *((int *)result) = val; 12520Sstevel@tonic-gate } 12530Sstevel@tonic-gate return (DDI_SUCCESS); 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate case DDI_CTLOPS_REGSIZE: 12560Sstevel@tonic-gate *((off_t *)result) = get_reg_set_size(rdip, *((int *)arg)); 12570Sstevel@tonic-gate return (*((off_t *)result) == -1 ? DDI_FAILURE : DDI_SUCCESS); 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate case DDI_CTLOPS_NREGS: 12600Sstevel@tonic-gate *((uint_t *)result) = get_nreg_set(rdip); 12610Sstevel@tonic-gate return (DDI_SUCCESS); 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate case DDI_CTLOPS_DVMAPAGESIZE: 12640Sstevel@tonic-gate *((ulong_t *)result) = IOMMU_PAGE_SIZE; 12650Sstevel@tonic-gate return (DDI_SUCCESS); 12660Sstevel@tonic-gate 12670Sstevel@tonic-gate case DDI_CTLOPS_POKE: 12680Sstevel@tonic-gate return (pci_ctlops_poke(pci_p, (peekpoke_ctlops_t *)arg)); 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate case DDI_CTLOPS_PEEK: 12710Sstevel@tonic-gate return (pci_ctlops_peek(pci_p, (peekpoke_ctlops_t *)arg, 12720Sstevel@tonic-gate result)); 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate case DDI_CTLOPS_AFFINITY: 12750Sstevel@tonic-gate break; 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate case DDI_CTLOPS_QUIESCE: 12780Sstevel@tonic-gate return (pci_bus_quiesce(pci_p, rdip, result)); 12790Sstevel@tonic-gate 12800Sstevel@tonic-gate case DDI_CTLOPS_UNQUIESCE: 12810Sstevel@tonic-gate return (pci_bus_unquiesce(pci_p, rdip, result)); 12820Sstevel@tonic-gate 12830Sstevel@tonic-gate default: 12840Sstevel@tonic-gate break; 12850Sstevel@tonic-gate } 12860Sstevel@tonic-gate 12870Sstevel@tonic-gate /* 12880Sstevel@tonic-gate * Now pass the request up to our parent. 12890Sstevel@tonic-gate */ 12900Sstevel@tonic-gate DEBUG2(DBG_CTLOPS, dip, "passing request to parent: rdip=%s%d\n", 12917656SSherry.Moore@Sun.COM ddi_driver_name(rdip), ddi_get_instance(rdip)); 12920Sstevel@tonic-gate return (ddi_ctlops(dip, rdip, op, arg, result)); 12930Sstevel@tonic-gate } 12940Sstevel@tonic-gate 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate /* ARGSUSED */ 12970Sstevel@tonic-gate int 12980Sstevel@tonic-gate pci_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op, 12990Sstevel@tonic-gate ddi_intr_handle_impl_t *hdlp, void *result) 13000Sstevel@tonic-gate { 1301*10053SEvan.Yan@Sun.COM pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip)); 1302*10053SEvan.Yan@Sun.COM ib_ino_t ino; 13030Sstevel@tonic-gate int ret = DDI_SUCCESS; 13040Sstevel@tonic-gate 13050Sstevel@tonic-gate switch (intr_op) { 13060Sstevel@tonic-gate case DDI_INTROP_GETCAP: 13070Sstevel@tonic-gate /* GetCap will always fail for all non PCI devices */ 13080Sstevel@tonic-gate (void) pci_intx_get_cap(rdip, (int *)result); 13090Sstevel@tonic-gate break; 13100Sstevel@tonic-gate case DDI_INTROP_SETCAP: 13110Sstevel@tonic-gate ret = DDI_ENOTSUP; 13120Sstevel@tonic-gate break; 13130Sstevel@tonic-gate case DDI_INTROP_ALLOC: 13140Sstevel@tonic-gate *(int *)result = hdlp->ih_scratch1; 13150Sstevel@tonic-gate break; 13160Sstevel@tonic-gate case DDI_INTROP_FREE: 13170Sstevel@tonic-gate break; 13180Sstevel@tonic-gate case DDI_INTROP_GETPRI: 1319693Sgovinda *(int *)result = hdlp->ih_pri ? 1320693Sgovinda hdlp->ih_pri : pci_class_to_pil(rdip); 13210Sstevel@tonic-gate break; 13220Sstevel@tonic-gate case DDI_INTROP_SETPRI: 13230Sstevel@tonic-gate break; 13240Sstevel@tonic-gate case DDI_INTROP_ADDISR: 13250Sstevel@tonic-gate ret = pci_add_intr(dip, rdip, hdlp); 13260Sstevel@tonic-gate break; 13270Sstevel@tonic-gate case DDI_INTROP_REMISR: 13280Sstevel@tonic-gate ret = pci_remove_intr(dip, rdip, hdlp); 13290Sstevel@tonic-gate break; 1330*10053SEvan.Yan@Sun.COM case DDI_INTROP_GETTARGET: 1331*10053SEvan.Yan@Sun.COM ino = IB_MONDO_TO_INO(pci_xlate_intr(dip, rdip, 1332*10053SEvan.Yan@Sun.COM pci_p->pci_ib_p, IB_MONDO_TO_INO(hdlp->ih_vector))); 1333*10053SEvan.Yan@Sun.COM ret = ib_get_intr_target(pci_p, ino, (int *)result); 1334*10053SEvan.Yan@Sun.COM break; 1335*10053SEvan.Yan@Sun.COM case DDI_INTROP_SETTARGET: 1336*10053SEvan.Yan@Sun.COM ret = DDI_ENOTSUP; 1337*10053SEvan.Yan@Sun.COM break; 13380Sstevel@tonic-gate case DDI_INTROP_ENABLE: 13390Sstevel@tonic-gate ret = ib_update_intr_state(pci_p, rdip, hdlp, 13400Sstevel@tonic-gate PCI_INTR_STATE_ENABLE); 13410Sstevel@tonic-gate break; 13420Sstevel@tonic-gate case DDI_INTROP_DISABLE: 13430Sstevel@tonic-gate ret = ib_update_intr_state(pci_p, rdip, hdlp, 13440Sstevel@tonic-gate PCI_INTR_STATE_DISABLE); 13450Sstevel@tonic-gate break; 13460Sstevel@tonic-gate case DDI_INTROP_SETMASK: 13470Sstevel@tonic-gate ret = pci_intx_set_mask(rdip); 13480Sstevel@tonic-gate break; 13490Sstevel@tonic-gate case DDI_INTROP_CLRMASK: 13500Sstevel@tonic-gate ret = pci_intx_clr_mask(rdip); 13510Sstevel@tonic-gate break; 13520Sstevel@tonic-gate case DDI_INTROP_GETPENDING: 13530Sstevel@tonic-gate ret = pci_intx_get_pending(rdip, (int *)result); 13540Sstevel@tonic-gate break; 13550Sstevel@tonic-gate case DDI_INTROP_NINTRS: 13560Sstevel@tonic-gate case DDI_INTROP_NAVAIL: 13572580Sanish *(int *)result = i_ddi_get_intx_nintrs(rdip); 13580Sstevel@tonic-gate break; 13590Sstevel@tonic-gate case DDI_INTROP_SUPPORTED_TYPES: 13600Sstevel@tonic-gate /* PCI nexus driver supports only fixed interrupts */ 13612580Sanish *(int *)result = i_ddi_get_intx_nintrs(rdip) ? 13620Sstevel@tonic-gate DDI_INTR_TYPE_FIXED : 0; 13630Sstevel@tonic-gate break; 13640Sstevel@tonic-gate default: 13650Sstevel@tonic-gate ret = DDI_ENOTSUP; 13660Sstevel@tonic-gate break; 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate 13690Sstevel@tonic-gate return (ret); 13700Sstevel@tonic-gate } 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate static void 13730Sstevel@tonic-gate pci_init_hotplug(struct pci *pci_p) 13740Sstevel@tonic-gate { 13750Sstevel@tonic-gate pci_bus_range_t bus_range; 13760Sstevel@tonic-gate dev_info_t *dip; 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate /* 13790Sstevel@tonic-gate * Before initializing hotplug - open up 13800Sstevel@tonic-gate * bus range. The busra module will 13810Sstevel@tonic-gate * initialize its pool of bus numbers from 13820Sstevel@tonic-gate * this. "busra" will be the agent that keeps 13830Sstevel@tonic-gate * track of them during hotplug. Also, note, 13840Sstevel@tonic-gate * that busra will remove any bus numbers 13850Sstevel@tonic-gate * already in use from boot time. 13860Sstevel@tonic-gate */ 13870Sstevel@tonic-gate bus_range.lo = 0x0; 13880Sstevel@tonic-gate bus_range.hi = 0xff; 13890Sstevel@tonic-gate dip = pci_p->pci_dip; 13900Sstevel@tonic-gate pci_p->hotplug_capable = B_FALSE; 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate /* 13930Sstevel@tonic-gate * If this property exists, this nexus has hot-plug 13940Sstevel@tonic-gate * slots. 13950Sstevel@tonic-gate */ 13960Sstevel@tonic-gate if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 13977656SSherry.Moore@Sun.COM "hotplug-capable")) { 13980Sstevel@tonic-gate if (ndi_prop_update_int_array(DDI_DEV_T_NONE, 13997656SSherry.Moore@Sun.COM dip, "bus-range", 14007656SSherry.Moore@Sun.COM (int *)&bus_range, 14017656SSherry.Moore@Sun.COM 2) != DDI_PROP_SUCCESS) { 14020Sstevel@tonic-gate return; 14030Sstevel@tonic-gate } 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate if (pcihp_init(dip) != DDI_SUCCESS) { 14060Sstevel@tonic-gate return; 14070Sstevel@tonic-gate } 14080Sstevel@tonic-gate 1409162Sschwartz if ((pcihp_ops = pcihp_get_cb_ops()) != NULL) { 14100Sstevel@tonic-gate DEBUG2(DBG_ATTACH, dip, "%s%d hotplug enabled", 14110Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 1412162Sschwartz pci_p->hotplug_capable = B_TRUE; 14130Sstevel@tonic-gate } 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate } 1416