Index: head/sys/dev/isci/isci.c =================================================================== --- head/sys/dev/isci/isci.c (revision 345069) +++ head/sys/dev/isci/isci.c (revision 345070) @@ -1,678 +1,680 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * BSD LICENSE * * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include MALLOC_DEFINE(M_ISCI, "isci", "isci driver memory allocations"); struct isci_softc *g_isci; uint32_t g_isci_debug_level = 0; static int isci_probe(device_t); static int isci_attach(device_t); static int isci_detach(device_t); int isci_initialize(struct isci_softc *isci); void isci_allocate_dma_buffer_callback(void *arg, bus_dma_segment_t *seg, int nseg, int error); static devclass_t isci_devclass; static device_method_t isci_pci_methods[] = { /* Device interface */ DEVMETHOD(device_probe, isci_probe), DEVMETHOD(device_attach, isci_attach), DEVMETHOD(device_detach, isci_detach), { 0, 0 } }; static driver_t isci_pci_driver = { "isci", isci_pci_methods, sizeof(struct isci_softc), }; DRIVER_MODULE(isci, pci, isci_pci_driver, isci_devclass, 0, 0); MODULE_DEPEND(isci, cam, 1, 1, 1); static struct _pcsid { u_int32_t type; const char *desc; } pci_ids[] = { { 0x1d608086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d618086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" }, { 0x1d628086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d638086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d648086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d658086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d668086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d678086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d688086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d698086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d6a8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" }, { 0x1d6b8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" }, { 0x1d6c8086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d6d8086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d6e8086, "Intel(R) C600 Series Chipset SAS Controller" }, { 0x1d6f8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" }, { 0x00000000, NULL } }; static int isci_probe (device_t device) { u_int32_t type = pci_get_devid(device); struct _pcsid *ep = pci_ids; while (ep->type && ep->type != type) ++ep; if (ep->desc) { device_set_desc(device, ep->desc); return (BUS_PROBE_DEFAULT); } else return (ENXIO); } static int isci_allocate_pci_memory(struct isci_softc *isci) { int i; for (i = 0; i < ISCI_NUM_PCI_BARS; i++) { struct ISCI_PCI_BAR *pci_bar = &isci->pci_bar[i]; pci_bar->resource_id = PCIR_BAR(i*2); pci_bar->resource = bus_alloc_resource_any(isci->device, SYS_RES_MEMORY, &pci_bar->resource_id, RF_ACTIVE); if(pci_bar->resource == NULL) isci_log_message(0, "ISCI", "unable to allocate pci resource\n"); else { pci_bar->bus_tag = rman_get_bustag(pci_bar->resource); pci_bar->bus_handle = rman_get_bushandle(pci_bar->resource); } } return (0); } static int isci_attach(device_t device) { int error; struct isci_softc *isci = DEVICE2SOFTC(device); g_isci = isci; isci->device = device; pci_enable_busmaster(device); isci_allocate_pci_memory(isci); error = isci_initialize(isci); if (error) { isci_detach(device); return (error); } isci_interrupt_setup(isci); isci_sysctl_initialize(isci); return (0); } static int isci_detach(device_t device) { struct isci_softc *isci = DEVICE2SOFTC(device); int i, phy; for (i = 0; i < isci->controller_count; i++) { struct ISCI_CONTROLLER *controller = &isci->controllers[i]; SCI_STATUS status; void *unmap_buffer; if (controller->scif_controller_handle != NULL) { scic_controller_disable_interrupts( scif_controller_get_scic_handle(controller->scif_controller_handle)); mtx_lock(&controller->lock); status = scif_controller_stop(controller->scif_controller_handle, 0); mtx_unlock(&controller->lock); while (controller->is_started == TRUE) { /* Now poll for interrupts until the controller stop complete * callback is received. */ mtx_lock(&controller->lock); isci_interrupt_poll_handler(controller); mtx_unlock(&controller->lock); pause("isci", 1); } if(controller->sim != NULL) { mtx_lock(&controller->lock); xpt_free_path(controller->path); xpt_bus_deregister(cam_sim_path(controller->sim)); cam_sim_free(controller->sim, TRUE); mtx_unlock(&controller->lock); } } if (controller->timer_memory != NULL) free(controller->timer_memory, M_ISCI); if (controller->remote_device_memory != NULL) free(controller->remote_device_memory, M_ISCI); for (phy = 0; phy < SCI_MAX_PHYS; phy++) { if (controller->phys[phy].cdev_fault) led_destroy(controller->phys[phy].cdev_fault); if (controller->phys[phy].cdev_locate) led_destroy(controller->phys[phy].cdev_locate); } while (1) { sci_pool_get(controller->unmap_buffer_pool, unmap_buffer); if (unmap_buffer == NULL) break; contigfree(unmap_buffer, PAGE_SIZE, M_ISCI); } } /* The SCIF controllers have been stopped, so we can now * free the SCI library memory. */ if (isci->sci_library_memory != NULL) free(isci->sci_library_memory, M_ISCI); for (i = 0; i < ISCI_NUM_PCI_BARS; i++) { struct ISCI_PCI_BAR *pci_bar = &isci->pci_bar[i]; if (pci_bar->resource != NULL) bus_release_resource(device, SYS_RES_MEMORY, pci_bar->resource_id, pci_bar->resource); } for (i = 0; i < isci->num_interrupts; i++) { struct ISCI_INTERRUPT_INFO *interrupt_info; interrupt_info = &isci->interrupt_info[i]; if(interrupt_info->tag != NULL) bus_teardown_intr(device, interrupt_info->res, interrupt_info->tag); if(interrupt_info->res != NULL) bus_release_resource(device, SYS_RES_IRQ, rman_get_rid(interrupt_info->res), interrupt_info->res); pci_release_msi(device); } pci_disable_busmaster(device); return (0); } int isci_initialize(struct isci_softc *isci) { int error; uint32_t status = 0; uint32_t library_object_size; uint32_t verbosity_mask; uint32_t scic_log_object_mask; uint32_t scif_log_object_mask; uint8_t *header_buffer; library_object_size = scif_library_get_object_size(SCI_MAX_CONTROLLERS); isci->sci_library_memory = malloc(library_object_size, M_ISCI, M_NOWAIT | M_ZERO ); isci->sci_library_handle = scif_library_construct( isci->sci_library_memory, SCI_MAX_CONTROLLERS); sci_object_set_association( isci->sci_library_handle, (void *)isci); verbosity_mask = (1<sci_library_handle), scif_log_object_mask, verbosity_mask); sci_logger_enable(sci_object_get_logger( scif_library_get_scic_handle(isci->sci_library_handle)), scic_log_object_mask, verbosity_mask); header_buffer = (uint8_t *)&isci->pci_common_header; for (uint8_t i = 0; i < sizeof(isci->pci_common_header); i++) header_buffer[i] = pci_read_config(isci->device, i, 1); scic_library_set_pci_info( scif_library_get_scic_handle(isci->sci_library_handle), &isci->pci_common_header); isci->oem_parameters_found = FALSE; isci_get_oem_parameters(isci); /* trigger interrupt if 32 completions occur before timeout expires */ isci->coalesce_number = 32; /* trigger interrupt if 2 microseconds elapse after a completion occurs, * regardless if "coalesce_number" completions have occurred */ isci->coalesce_timeout = 2; isci->controller_count = scic_library_get_pci_device_controller_count( scif_library_get_scic_handle(isci->sci_library_handle)); for (int index = 0; index < isci->controller_count; index++) { struct ISCI_CONTROLLER *controller = &isci->controllers[index]; SCI_CONTROLLER_HANDLE_T scif_controller_handle; controller->index = index; isci_controller_construct(controller, isci); scif_controller_handle = controller->scif_controller_handle; status = isci_controller_initialize(controller); if(status != SCI_SUCCESS) { isci_log_message(0, "ISCI", "isci_controller_initialize FAILED: %x\n", status); return (status); } error = isci_controller_allocate_memory(controller); if (error != 0) return (error); scif_controller_set_interrupt_coalescence( scif_controller_handle, isci->coalesce_number, isci->coalesce_timeout); } /* FreeBSD provides us a hook to ensure we get a chance to start * our controllers and complete initial domain discovery before * it searches for the boot device. Once we're done, we'll * disestablish the hook, signaling the kernel that is can proceed * with the boot process. */ isci->config_hook.ich_func = &isci_controller_start; isci->config_hook.ich_arg = &isci->controllers[0]; if (config_intrhook_establish(&isci->config_hook) != 0) isci_log_message(0, "ISCI", "config_intrhook_establish failed!\n"); return (status); } void isci_allocate_dma_buffer_callback(void *arg, bus_dma_segment_t *seg, int nseg, int error) { struct ISCI_MEMORY *memory = (struct ISCI_MEMORY *)arg; memory->error = error; if (nseg != 1 || error != 0) isci_log_message(0, "ISCI", "Failed to allocate physically contiguous memory!\n"); else memory->physical_address = seg->ds_addr; } int -isci_allocate_dma_buffer(device_t device, struct ISCI_MEMORY *memory) +isci_allocate_dma_buffer(device_t device, struct ISCI_CONTROLLER *controller, + struct ISCI_MEMORY *memory) { uint32_t status; status = bus_dma_tag_create(bus_get_dma_tag(device), 0x40 /* cacheline alignment */, 0x0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, memory->size, 0x1 /* we want physically contiguous */, - memory->size, 0, NULL, NULL, &memory->dma_tag); + memory->size, 0, busdma_lock_mutex, &controller->lock, + &memory->dma_tag); if(status == ENOMEM) { isci_log_message(0, "ISCI", "bus_dma_tag_create failed\n"); return (status); } status = bus_dmamem_alloc(memory->dma_tag, (void **)&memory->virtual_address, BUS_DMA_ZERO, &memory->dma_map); if(status == ENOMEM) { isci_log_message(0, "ISCI", "bus_dmamem_alloc failed\n"); return (status); } status = bus_dmamap_load(memory->dma_tag, memory->dma_map, (void *)memory->virtual_address, memory->size, isci_allocate_dma_buffer_callback, memory, 0); if(status == EINVAL) { isci_log_message(0, "ISCI", "bus_dmamap_load failed\n"); return (status); } return (0); } /** * @brief This callback method asks the user to associate the supplied * lock with an operating environment specific locking construct. * * @param[in] controller This parameter specifies the controller with * which this lock is to be associated. * @param[in] lock This parameter specifies the lock for which the * user should associate an operating environment specific * locking object. * * @see The SCI_LOCK_LEVEL enumeration for more information. * * @return none. */ void scif_cb_lock_associate(SCI_CONTROLLER_HANDLE_T controller, SCI_LOCK_HANDLE_T lock) { } /** * @brief This callback method asks the user to de-associate the supplied * lock with an operating environment specific locking construct. * * @param[in] controller This parameter specifies the controller with * which this lock is to be de-associated. * @param[in] lock This parameter specifies the lock for which the * user should de-associate an operating environment specific * locking object. * * @see The SCI_LOCK_LEVEL enumeration for more information. * * @return none. */ void scif_cb_lock_disassociate(SCI_CONTROLLER_HANDLE_T controller, SCI_LOCK_HANDLE_T lock) { } /** * @brief This callback method asks the user to acquire/get the lock. * This method should pend until the lock has been acquired. * * @param[in] controller This parameter specifies the controller with * which this lock is associated. * @param[in] lock This parameter specifies the lock to be acquired. * * @return none */ void scif_cb_lock_acquire(SCI_CONTROLLER_HANDLE_T controller, SCI_LOCK_HANDLE_T lock) { } /** * @brief This callback method asks the user to release a lock. * * @param[in] controller This parameter specifies the controller with * which this lock is associated. * @param[in] lock This parameter specifies the lock to be released. * * @return none */ void scif_cb_lock_release(SCI_CONTROLLER_HANDLE_T controller, SCI_LOCK_HANDLE_T lock) { } /** * @brief This callback method creates an OS specific deferred task * for internal usage. The handler to deferred task is stored by OS * driver. * * @param[in] controller This parameter specifies the controller object * with which this callback is associated. * * @return none */ void scif_cb_start_internal_io_task_create(SCI_CONTROLLER_HANDLE_T controller) { } /** * @brief This callback method schedules a OS specific deferred task. * * @param[in] controller This parameter specifies the controller * object with which this callback is associated. * @param[in] start_internal_io_task_routine This parameter specifies the * sci start_internal_io routine. * @param[in] context This parameter specifies a handle to a parameter * that will be passed into the "start_internal_io_task_routine" * when it is invoked. * * @return none */ void scif_cb_start_internal_io_task_schedule(SCI_CONTROLLER_HANDLE_T scif_controller, FUNCPTR start_internal_io_task_routine, void *context) { /** @todo Use FreeBSD tasklet to defer this routine to a later time, * rather than calling the routine inline. */ SCI_START_INTERNAL_IO_ROUTINE sci_start_internal_io_routine = (SCI_START_INTERNAL_IO_ROUTINE)start_internal_io_task_routine; sci_start_internal_io_routine(context); } /** * @brief In this method the user must write to PCI memory via access. * This method is used for access to memory space and IO space. * * @param[in] controller The controller for which to read a DWORD. * @param[in] address This parameter depicts the address into * which to write. * @param[out] write_value This parameter depicts the value being written * into the PCI memory location. * * @todo These PCI memory access calls likely needs to be optimized into macros? */ void scic_cb_pci_write_dword(SCI_CONTROLLER_HANDLE_T scic_controller, void *address, uint32_t write_value) { SCI_CONTROLLER_HANDLE_T scif_controller = (SCI_CONTROLLER_HANDLE_T) sci_object_get_association(scic_controller); struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) sci_object_get_association(scif_controller); struct isci_softc *isci = isci_controller->isci; uint32_t bar = (uint32_t)(((POINTER_UINT)address & 0xF0000000) >> 28); bus_size_t offset = (bus_size_t)((POINTER_UINT)address & 0x0FFFFFFF); bus_space_write_4(isci->pci_bar[bar].bus_tag, isci->pci_bar[bar].bus_handle, offset, write_value); } /** * @brief In this method the user must read from PCI memory via access. * This method is used for access to memory space and IO space. * * @param[in] controller The controller for which to read a DWORD. * @param[in] address This parameter depicts the address from * which to read. * * @return The value being returned from the PCI memory location. * * @todo This PCI memory access calls likely need to be optimized into macro? */ uint32_t scic_cb_pci_read_dword(SCI_CONTROLLER_HANDLE_T scic_controller, void *address) { SCI_CONTROLLER_HANDLE_T scif_controller = (SCI_CONTROLLER_HANDLE_T)sci_object_get_association(scic_controller); struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *)sci_object_get_association(scif_controller); struct isci_softc *isci = isci_controller->isci; uint32_t bar = (uint32_t)(((POINTER_UINT)address & 0xF0000000) >> 28); bus_size_t offset = (bus_size_t)((POINTER_UINT)address & 0x0FFFFFFF); return (bus_space_read_4(isci->pci_bar[bar].bus_tag, isci->pci_bar[bar].bus_handle, offset)); } /** * @brief This method is called when the core requires the OS driver * to stall execution. This method is utilized during initialization * or non-performance paths only. * * @param[in] microseconds This parameter specifies the number of * microseconds for which to stall. The operating system driver * is allowed to round this value up where necessary. * * @return none. */ void scic_cb_stall_execution(uint32_t microseconds) { DELAY(microseconds); } /** * @brief In this method the user must return the base address register (BAR) * value for the supplied base address register number. * * @param[in] controller The controller for which to retrieve the bar number. * @param[in] bar_number This parameter depicts the BAR index/number to be read. * * @return Return a pointer value indicating the contents of the BAR. * @retval NULL indicates an invalid BAR index/number was specified. * @retval All other values indicate a valid VIRTUAL address from the BAR. */ void * scic_cb_pci_get_bar(SCI_CONTROLLER_HANDLE_T controller, uint16_t bar_number) { return ((void *)(POINTER_UINT)((uint32_t)bar_number << 28)); } /** * @brief This method informs the SCI Core user that a phy/link became * ready, but the phy is not allowed in the port. In some * situations the underlying hardware only allows for certain phy * to port mappings. If these mappings are violated, then this * API is invoked. * * @param[in] controller This parameter represents the controller which * contains the port. * @param[in] port This parameter specifies the SCI port object for which * the callback is being invoked. * @param[in] phy This parameter specifies the phy that came ready, but the * phy can't be a valid member of the port. * * @return none */ void scic_cb_port_invalid_link_up(SCI_CONTROLLER_HANDLE_T controller, SCI_PORT_HANDLE_T port, SCI_PHY_HANDLE_T phy) { } Index: head/sys/dev/isci/isci.h =================================================================== --- head/sys/dev/isci/isci.h (revision 345069) +++ head/sys/dev/isci/isci.h (revision 345070) @@ -1,350 +1,351 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * BSD LICENSE * * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _ISCI_H #define _ISCI_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEVICE2SOFTC(dev) ((struct isci_softc *) device_get_softc(dev)) #define DEVICE_TIMEOUT 1000 #define SCI_MAX_TIMERS 32 #define ISCI_NUM_PCI_BARS 2 #define ISCI_MAX_LUN 8 MALLOC_DECLARE(M_ISCI); struct ISCI_TIMER { struct callout callout; SCI_TIMER_CALLBACK_T callback; void *cookie; BOOL is_started; }; struct ISCI_REMOTE_DEVICE { uint32_t index; struct ISCI_DOMAIN *domain; SCI_REMOTE_DEVICE_HANDLE_T sci_object; BOOL is_resetting; uint32_t frozen_lun_mask; SCI_FAST_LIST_ELEMENT_T pending_device_reset_element; /* * This queue maintains CCBs that have been returned with * SCI_IO_FAILURE_INVALID_STATE from the SCI layer. These CCBs * need to be retried, but we cannot return CAM_REQUEUE_REQ because * this status gets passed all the way back up to users of the pass(4) * interface and breaks things like smartctl. So instead, we queue * these CCBs internally. */ TAILQ_HEAD(,ccb_hdr) queued_ccbs; /* * Marker denoting this remote device needs its first queued ccb to * be retried. */ BOOL release_queued_ccb; /* * Points to a CCB in the queue that is currently being processed by * SCIL. This allows us to keep in flight CCBs in the queue so as to * maintain ordering (i.e. in case we retry an I/O and then find out * it needs to be retried again - it just keeps its same place in the * queue. */ union ccb * queued_ccb_in_progress; }; struct ISCI_DOMAIN { struct ISCI_CONTROLLER *controller; SCI_DOMAIN_HANDLE_T sci_object; uint8_t index; struct ISCI_REMOTE_DEVICE *da_remote_device; }; struct ISCI_MEMORY { bus_addr_t physical_address; bus_dma_tag_t dma_tag; bus_dmamap_t dma_map; POINTER_UINT virtual_address; uint32_t size; int error; }; struct ISCI_INTERRUPT_INFO { SCIC_CONTROLLER_HANDLER_METHODS_T *handlers; void *interrupt_target_handle; struct resource *res; int rid; void *tag; }; struct ISCI_PHY { struct cdev *cdev_fault; struct cdev *cdev_locate; SCI_CONTROLLER_HANDLE_T handle; int index; int led_fault; int led_locate; }; struct ISCI_CONTROLLER { struct isci_softc *isci; uint8_t index; SCI_CONTROLLER_HANDLE_T scif_controller_handle; struct ISCI_DOMAIN domain[SCI_MAX_DOMAINS]; BOOL is_started; BOOL has_been_scanned; uint32_t initial_discovery_mask; BOOL is_frozen; BOOL release_queued_ccbs; BOOL fail_on_task_timeout; uint8_t *remote_device_memory; struct ISCI_MEMORY cached_controller_memory; struct ISCI_MEMORY uncached_controller_memory; struct ISCI_MEMORY request_memory; bus_dma_tag_t buffer_dma_tag; struct mtx lock; struct cam_sim *sim; struct cam_path *path; struct ISCI_REMOTE_DEVICE *remote_device[SCI_MAX_REMOTE_DEVICES]; void *timer_memory; SCIC_OEM_PARAMETERS_T oem_parameters; uint32_t oem_parameters_version; uint32_t queue_depth; uint32_t sim_queue_depth; SCI_FAST_LIST_T pending_device_reset_list; struct ISCI_PHY phys[SCI_MAX_PHYS]; SCI_MEMORY_DESCRIPTOR_LIST_HANDLE_T mdl; SCI_POOL_CREATE(remote_device_pool, struct ISCI_REMOTE_DEVICE *, SCI_MAX_REMOTE_DEVICES); SCI_POOL_CREATE(request_pool, struct ISCI_REQUEST *, SCI_MAX_IO_REQUESTS); SCI_POOL_CREATE(timer_pool, struct ISCI_TIMER *, SCI_MAX_TIMERS); SCI_POOL_CREATE(unmap_buffer_pool, void *, SCI_MAX_REMOTE_DEVICES); }; struct ISCI_REQUEST { SCI_CONTROLLER_HANDLE_T controller_handle; SCI_REMOTE_DEVICE_HANDLE_T remote_device_handle; bus_dma_tag_t dma_tag; bus_dmamap_t dma_map; SCI_PHYSICAL_ADDRESS physical_address; struct callout timer; }; struct ISCI_IO_REQUEST { struct ISCI_REQUEST parent; SCI_IO_REQUEST_HANDLE_T sci_object; union ccb *ccb; uint32_t num_segments; uint32_t current_sge_index; bus_dma_segment_t *sge; }; struct ISCI_TASK_REQUEST { struct ISCI_REQUEST parent; struct scsi_sense_data sense_data; SCI_TASK_REQUEST_HANDLE_T sci_object; union ccb *ccb; }; struct ISCI_PCI_BAR { bus_space_tag_t bus_tag; bus_space_handle_t bus_handle; int resource_id; struct resource *resource; }; /* * One of these per allocated PCI device. */ struct isci_softc { struct ISCI_PCI_BAR pci_bar[ISCI_NUM_PCI_BARS]; struct ISCI_CONTROLLER controllers[SCI_MAX_CONTROLLERS]; SCI_LIBRARY_HANDLE_T sci_library_handle; void * sci_library_memory; SCIC_CONTROLLER_HANDLER_METHODS_T handlers[4]; struct ISCI_INTERRUPT_INFO interrupt_info[4]; uint32_t controller_count; uint32_t num_interrupts; uint32_t coalesce_number; uint32_t coalesce_timeout; device_t device; SCI_PCI_COMMON_HEADER_T pci_common_header; BOOL oem_parameters_found; struct intr_config_hook config_hook; }; int isci_allocate_resources(device_t device); -int isci_allocate_dma_buffer(device_t device, struct ISCI_MEMORY *memory); +int isci_allocate_dma_buffer(device_t device, struct ISCI_CONTROLLER *lock, + struct ISCI_MEMORY *memory); void isci_remote_device_reset(struct ISCI_REMOTE_DEVICE *remote_device, union ccb *ccb); /** * Returns the negotiated link rate (in KB/s) for the associated * remote device. Used to fill out bitrate field for GET_TRANS_SETTINGS. * Will match the negotiated link rate for the lowest numbered local phy * in the port/domain containing this remote device. */ uint32_t isci_remote_device_get_bitrate( struct ISCI_REMOTE_DEVICE *remote_device); void isci_remote_device_freeze_lun_queue( struct ISCI_REMOTE_DEVICE *remote_device, lun_id_t lun); void isci_remote_device_release_lun_queue( struct ISCI_REMOTE_DEVICE *remote_device, lun_id_t lun); void isci_remote_device_release_device_queue( struct ISCI_REMOTE_DEVICE * remote_device); void isci_request_construct(struct ISCI_REQUEST *request, SCI_CONTROLLER_HANDLE_T scif_controller_handle, bus_dma_tag_t io_buffer_dma_tag, bus_addr_t physical_address); #define isci_io_request_get_max_io_size() \ ((SCI_MAX_SCATTER_GATHER_ELEMENTS - 1) * PAGE_SIZE) #define isci_task_request_get_object_size() \ (sizeof(struct ISCI_TASK_REQUEST) + scif_task_request_get_object_size()) #define isci_io_request_get_object_size() \ (sizeof(struct ISCI_IO_REQUEST) + scif_io_request_get_object_size()) #define isci_request_get_object_size() \ max( \ isci_task_request_get_object_size(), \ isci_io_request_get_object_size() \ ) void isci_io_request_execute_scsi_io(union ccb *ccb, struct ISCI_CONTROLLER *controller); #if __FreeBSD_version >= 900026 void isci_io_request_execute_smp_io( union ccb *ccb, struct ISCI_CONTROLLER *controller); #endif void isci_io_request_timeout(void *); void isci_get_oem_parameters(struct isci_softc *isci); void isci_io_request_complete( SCI_CONTROLLER_HANDLE_T scif_controller, SCI_REMOTE_DEVICE_HANDLE_T remote_device, struct ISCI_IO_REQUEST * isci_request, SCI_IO_STATUS completion_status); void isci_task_request_complete( SCI_CONTROLLER_HANDLE_T scif_controller, SCI_REMOTE_DEVICE_HANDLE_T remote_device, SCI_TASK_REQUEST_HANDLE_T io_request, SCI_TASK_STATUS completion_status); void isci_sysctl_initialize(struct isci_softc *isci); void isci_controller_construct(struct ISCI_CONTROLLER *controller, struct isci_softc *isci); SCI_STATUS isci_controller_initialize(struct ISCI_CONTROLLER *controller); int isci_controller_allocate_memory(struct ISCI_CONTROLLER *controller); void isci_controller_domain_discovery_complete( struct ISCI_CONTROLLER *isci_controller, struct ISCI_DOMAIN *isci_domain); int isci_controller_attach_to_cam(struct ISCI_CONTROLLER *controller); void isci_controller_start(void *controller); void isci_controller_release_queued_ccbs(struct ISCI_CONTROLLER *controller); void isci_domain_construct(struct ISCI_DOMAIN *domain, uint32_t domain_index, struct ISCI_CONTROLLER *controller); void isci_interrupt_setup(struct isci_softc *isci); void isci_interrupt_poll_handler(struct ISCI_CONTROLLER *controller); void isci_log_message(uint32_t verbosity, char *log_message_prefix, char *log_message, ...); extern uint32_t g_isci_debug_level; #endif /* #ifndef _ISCI_H */ Index: head/sys/dev/isci/isci_controller.c =================================================================== --- head/sys/dev/isci/isci_controller.c (revision 345069) +++ head/sys/dev/isci/isci_controller.c (revision 345070) @@ -1,840 +1,843 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * BSD LICENSE * * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include void isci_action(struct cam_sim *sim, union ccb *ccb); void isci_poll(struct cam_sim *sim); #define ccb_sim_ptr sim_priv.entries[0].ptr /** * @brief This user callback will inform the user that the controller has * had a serious unexpected error. The user should not the error, * disable interrupts, and wait for current ongoing processing to * complete. Subsequently, the user should reset the controller. * * @param[in] controller This parameter specifies the controller that had * an error. * * @return none */ void scif_cb_controller_error(SCI_CONTROLLER_HANDLE_T controller, SCI_CONTROLLER_ERROR error) { isci_log_message(0, "ISCI", "scif_cb_controller_error: 0x%x\n", error); } /** * @brief This user callback will inform the user that the controller has * finished the start process. * * @param[in] controller This parameter specifies the controller that was * started. * @param[in] completion_status This parameter specifies the results of * the start operation. SCI_SUCCESS indicates successful * completion. * * @return none */ void scif_cb_controller_start_complete(SCI_CONTROLLER_HANDLE_T controller, SCI_STATUS completion_status) { uint32_t index; struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) sci_object_get_association(controller); isci_controller->is_started = TRUE; /* Set bits for all domains. We will clear them one-by-one once * the domains complete discovery, or return error when calling * scif_domain_discover. Once all bits are clear, we will register * the controller with CAM. */ isci_controller->initial_discovery_mask = (1 << SCI_MAX_DOMAINS) - 1; for(index = 0; index < SCI_MAX_DOMAINS; index++) { SCI_STATUS status; SCI_DOMAIN_HANDLE_T domain = isci_controller->domain[index].sci_object; status = scif_domain_discover( domain, scif_domain_get_suggested_discover_timeout(domain), DEVICE_TIMEOUT ); if (status != SCI_SUCCESS) { isci_controller_domain_discovery_complete( isci_controller, &isci_controller->domain[index]); } } } /** * @brief This user callback will inform the user that the controller has * finished the stop process. Note, after user calls * scif_controller_stop(), before user receives this controller stop * complete callback, user should not expect any callback from * framework, such like scif_cb_domain_change_notification(). * * @param[in] controller This parameter specifies the controller that was * stopped. * @param[in] completion_status This parameter specifies the results of * the stop operation. SCI_SUCCESS indicates successful * completion. * * @return none */ void scif_cb_controller_stop_complete(SCI_CONTROLLER_HANDLE_T controller, SCI_STATUS completion_status) { struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) sci_object_get_association(controller); isci_controller->is_started = FALSE; } static void isci_single_map(void *arg, bus_dma_segment_t *seg, int nseg, int error) { SCI_PHYSICAL_ADDRESS *phys_addr = arg; *phys_addr = seg[0].ds_addr; } /** * @brief This method will be invoked to allocate memory dynamically. * * @param[in] controller This parameter represents the controller * object for which to allocate memory. * @param[out] mde This parameter represents the memory descriptor to * be filled in by the user that will reference the newly * allocated memory. * * @return none */ void scif_cb_controller_allocate_memory(SCI_CONTROLLER_HANDLE_T controller, SCI_PHYSICAL_MEMORY_DESCRIPTOR_T *mde) { struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) sci_object_get_association(controller); /* * Note this routine is only used for buffers needed to translate * SCSI UNMAP commands to ATA DSM commands for SATA disks. * * We first try to pull a buffer from the controller's pool, and only * call contigmalloc if one isn't there. */ if (!sci_pool_empty(isci_controller->unmap_buffer_pool)) { sci_pool_get(isci_controller->unmap_buffer_pool, mde->virtual_address); } else mde->virtual_address = contigmalloc(PAGE_SIZE, M_ISCI, M_NOWAIT, 0, BUS_SPACE_MAXADDR, mde->constant_memory_alignment, 0); if (mde->virtual_address != NULL) bus_dmamap_load(isci_controller->buffer_dma_tag, NULL, mde->virtual_address, PAGE_SIZE, isci_single_map, &mde->physical_address, BUS_DMA_NOWAIT); } /** * @brief This method will be invoked to allocate memory dynamically. * * @param[in] controller This parameter represents the controller * object for which to allocate memory. * @param[out] mde This parameter represents the memory descriptor to * be filled in by the user that will reference the newly * allocated memory. * * @return none */ void scif_cb_controller_free_memory(SCI_CONTROLLER_HANDLE_T controller, SCI_PHYSICAL_MEMORY_DESCRIPTOR_T * mde) { struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) sci_object_get_association(controller); /* * Put the buffer back into the controller's buffer pool, rather * than invoking configfree. This helps reduce chance we won't * have buffers available when system is under memory pressure. */ sci_pool_put(isci_controller->unmap_buffer_pool, mde->virtual_address); } void isci_controller_construct(struct ISCI_CONTROLLER *controller, struct isci_softc *isci) { SCI_CONTROLLER_HANDLE_T scif_controller_handle; scif_library_allocate_controller(isci->sci_library_handle, &scif_controller_handle); scif_controller_construct(isci->sci_library_handle, scif_controller_handle, NULL); controller->isci = isci; controller->scif_controller_handle = scif_controller_handle; /* This allows us to later use * sci_object_get_association(scif_controller_handle) * inside of a callback routine to get our struct ISCI_CONTROLLER object */ sci_object_set_association(scif_controller_handle, (void *)controller); controller->is_started = FALSE; controller->is_frozen = FALSE; controller->release_queued_ccbs = FALSE; controller->sim = NULL; controller->initial_discovery_mask = 0; sci_fast_list_init(&controller->pending_device_reset_list); mtx_init(&controller->lock, "isci", NULL, MTX_DEF); uint32_t domain_index; for(domain_index = 0; domain_index < SCI_MAX_DOMAINS; domain_index++) { isci_domain_construct( &controller->domain[domain_index], domain_index, controller); } controller->timer_memory = malloc( sizeof(struct ISCI_TIMER) * SCI_MAX_TIMERS, M_ISCI, M_NOWAIT | M_ZERO); sci_pool_initialize(controller->timer_pool); struct ISCI_TIMER *timer = (struct ISCI_TIMER *) controller->timer_memory; for ( int i = 0; i < SCI_MAX_TIMERS; i++ ) { sci_pool_put(controller->timer_pool, timer++); } sci_pool_initialize(controller->unmap_buffer_pool); } static void isci_led_fault_func(void *priv, int onoff) { struct ISCI_PHY *phy = priv; /* map onoff to the fault LED */ phy->led_fault = onoff; scic_sgpio_update_led_state(phy->handle, 1 << phy->index, phy->led_fault, phy->led_locate, 0); } static void isci_led_locate_func(void *priv, int onoff) { struct ISCI_PHY *phy = priv; /* map onoff to the locate LED */ phy->led_locate = onoff; scic_sgpio_update_led_state(phy->handle, 1 << phy->index, phy->led_fault, phy->led_locate, 0); } SCI_STATUS isci_controller_initialize(struct ISCI_CONTROLLER *controller) { SCIC_USER_PARAMETERS_T scic_user_parameters; SCI_CONTROLLER_HANDLE_T scic_controller_handle; char led_name[64]; unsigned long tunable; uint32_t io_shortage; uint32_t fail_on_timeout; int i; scic_controller_handle = scif_controller_get_scic_handle(controller->scif_controller_handle); if (controller->isci->oem_parameters_found == TRUE) { scic_oem_parameters_set( scic_controller_handle, &controller->oem_parameters, (uint8_t)(controller->oem_parameters_version)); } scic_user_parameters_get(scic_controller_handle, &scic_user_parameters); if (TUNABLE_ULONG_FETCH("hw.isci.no_outbound_task_timeout", &tunable)) scic_user_parameters.sds1.no_outbound_task_timeout = (uint8_t)tunable; if (TUNABLE_ULONG_FETCH("hw.isci.ssp_max_occupancy_timeout", &tunable)) scic_user_parameters.sds1.ssp_max_occupancy_timeout = (uint16_t)tunable; if (TUNABLE_ULONG_FETCH("hw.isci.stp_max_occupancy_timeout", &tunable)) scic_user_parameters.sds1.stp_max_occupancy_timeout = (uint16_t)tunable; if (TUNABLE_ULONG_FETCH("hw.isci.ssp_inactivity_timeout", &tunable)) scic_user_parameters.sds1.ssp_inactivity_timeout = (uint16_t)tunable; if (TUNABLE_ULONG_FETCH("hw.isci.stp_inactivity_timeout", &tunable)) scic_user_parameters.sds1.stp_inactivity_timeout = (uint16_t)tunable; if (TUNABLE_ULONG_FETCH("hw.isci.max_speed_generation", &tunable)) for (i = 0; i < SCI_MAX_PHYS; i++) scic_user_parameters.sds1.phys[i].max_speed_generation = (uint8_t)tunable; scic_user_parameters_set(scic_controller_handle, &scic_user_parameters); /* Scheduler bug in SCU requires SCIL to reserve some task contexts as a * a workaround - one per domain. */ controller->queue_depth = SCI_MAX_IO_REQUESTS - SCI_MAX_DOMAINS; if (TUNABLE_INT_FETCH("hw.isci.controller_queue_depth", &controller->queue_depth)) { controller->queue_depth = max(1, min(controller->queue_depth, SCI_MAX_IO_REQUESTS - SCI_MAX_DOMAINS)); } /* Reserve one request so that we can ensure we have one available TC * to do internal device resets. */ controller->sim_queue_depth = controller->queue_depth - 1; /* Although we save one TC to do internal device resets, it is possible * we could end up using several TCs for simultaneous device resets * while at the same time having CAM fill our controller queue. To * simulate this condition, and how our driver handles it, we can set * this io_shortage parameter, which will tell CAM that we have a * large queue depth than we really do. */ io_shortage = 0; TUNABLE_INT_FETCH("hw.isci.io_shortage", &io_shortage); controller->sim_queue_depth += io_shortage; fail_on_timeout = 1; TUNABLE_INT_FETCH("hw.isci.fail_on_task_timeout", &fail_on_timeout); controller->fail_on_task_timeout = fail_on_timeout; /* Attach to CAM using xpt_bus_register now, then immediately freeze * the simq. It will get released later when initial domain discovery * is complete. */ controller->has_been_scanned = FALSE; mtx_lock(&controller->lock); isci_controller_attach_to_cam(controller); xpt_freeze_simq(controller->sim, 1); mtx_unlock(&controller->lock); for (i = 0; i < SCI_MAX_PHYS; i++) { controller->phys[i].handle = scic_controller_handle; controller->phys[i].index = i; /* fault */ controller->phys[i].led_fault = 0; sprintf(led_name, "isci.bus%d.port%d.fault", controller->index, i); controller->phys[i].cdev_fault = led_create(isci_led_fault_func, &controller->phys[i], led_name); /* locate */ controller->phys[i].led_locate = 0; sprintf(led_name, "isci.bus%d.port%d.locate", controller->index, i); controller->phys[i].cdev_locate = led_create(isci_led_locate_func, &controller->phys[i], led_name); } return (scif_controller_initialize(controller->scif_controller_handle)); } int isci_controller_allocate_memory(struct ISCI_CONTROLLER *controller) { int error; device_t device = controller->isci->device; uint32_t max_segment_size = isci_io_request_get_max_io_size(); uint32_t status = 0; struct ISCI_MEMORY *uncached_controller_memory = &controller->uncached_controller_memory; struct ISCI_MEMORY *cached_controller_memory = &controller->cached_controller_memory; struct ISCI_MEMORY *request_memory = &controller->request_memory; POINTER_UINT virtual_address; bus_addr_t physical_address; controller->mdl = sci_controller_get_memory_descriptor_list_handle( controller->scif_controller_handle); uncached_controller_memory->size = sci_mdl_decorator_get_memory_size( controller->mdl, SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS); - error = isci_allocate_dma_buffer(device, uncached_controller_memory); + error = isci_allocate_dma_buffer(device, controller, + uncached_controller_memory); if (error != 0) return (error); sci_mdl_decorator_assign_memory( controller->mdl, SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS, uncached_controller_memory->virtual_address, uncached_controller_memory->physical_address); cached_controller_memory->size = sci_mdl_decorator_get_memory_size( controller->mdl, SCI_MDE_ATTRIBUTE_CACHEABLE | SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS ); - error = isci_allocate_dma_buffer(device, cached_controller_memory); + error = isci_allocate_dma_buffer(device, controller, + cached_controller_memory); if (error != 0) return (error); sci_mdl_decorator_assign_memory(controller->mdl, SCI_MDE_ATTRIBUTE_CACHEABLE | SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS, cached_controller_memory->virtual_address, cached_controller_memory->physical_address); request_memory->size = controller->queue_depth * isci_io_request_get_object_size(); - error = isci_allocate_dma_buffer(device, request_memory); + error = isci_allocate_dma_buffer(device, controller, request_memory); if (error != 0) return (error); /* For STP PIO testing, we want to ensure we can force multiple SGLs * since this has been a problem area in SCIL. This tunable parameter * will allow us to force DMA segments to a smaller size, ensuring * that even if a physically contiguous buffer is attached to this * I/O, the DMA subsystem will pass us multiple segments in our DMA * load callback. */ TUNABLE_INT_FETCH("hw.isci.max_segment_size", &max_segment_size); /* Create DMA tag for our I/O requests. Then we can create DMA maps based off * of this tag and store them in each of our ISCI_IO_REQUEST objects. This * will enable better performance than creating the DMA maps every time we get * an I/O. */ status = bus_dma_tag_create(bus_get_dma_tag(device), 0x1, 0x0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, isci_io_request_get_max_io_size(), - SCI_MAX_SCATTER_GATHER_ELEMENTS, max_segment_size, 0, NULL, NULL, + SCI_MAX_SCATTER_GATHER_ELEMENTS, max_segment_size, 0, + busdma_lock_mutex, &controller->lock, &controller->buffer_dma_tag); sci_pool_initialize(controller->request_pool); virtual_address = request_memory->virtual_address; physical_address = request_memory->physical_address; for (int i = 0; i < controller->queue_depth; i++) { struct ISCI_REQUEST *request = (struct ISCI_REQUEST *)virtual_address; isci_request_construct(request, controller->scif_controller_handle, controller->buffer_dma_tag, physical_address); sci_pool_put(controller->request_pool, request); virtual_address += isci_request_get_object_size(); physical_address += isci_request_get_object_size(); } uint32_t remote_device_size = sizeof(struct ISCI_REMOTE_DEVICE) + scif_remote_device_get_object_size(); controller->remote_device_memory = (uint8_t *) malloc( remote_device_size * SCI_MAX_REMOTE_DEVICES, M_ISCI, M_NOWAIT | M_ZERO); sci_pool_initialize(controller->remote_device_pool); uint8_t *remote_device_memory_ptr = controller->remote_device_memory; for (int i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) { struct ISCI_REMOTE_DEVICE *remote_device = (struct ISCI_REMOTE_DEVICE *)remote_device_memory_ptr; controller->remote_device[i] = NULL; remote_device->index = i; remote_device->is_resetting = FALSE; remote_device->frozen_lun_mask = 0; sci_fast_list_element_init(remote_device, &remote_device->pending_device_reset_element); TAILQ_INIT(&remote_device->queued_ccbs); remote_device->release_queued_ccb = FALSE; remote_device->queued_ccb_in_progress = NULL; /* * For the first SCI_MAX_DOMAINS device objects, do not put * them in the pool, rather assign them to each domain. This * ensures that any device attached directly to port "i" will * always get CAM target id "i". */ if (i < SCI_MAX_DOMAINS) controller->domain[i].da_remote_device = remote_device; else sci_pool_put(controller->remote_device_pool, remote_device); remote_device_memory_ptr += remote_device_size; } return (0); } void isci_controller_start(void *controller_handle) { struct ISCI_CONTROLLER *controller = (struct ISCI_CONTROLLER *)controller_handle; SCI_CONTROLLER_HANDLE_T scif_controller_handle = controller->scif_controller_handle; scif_controller_start(scif_controller_handle, scif_controller_get_suggested_start_timeout(scif_controller_handle)); scic_controller_enable_interrupts( scif_controller_get_scic_handle(controller->scif_controller_handle)); } void isci_controller_domain_discovery_complete( struct ISCI_CONTROLLER *isci_controller, struct ISCI_DOMAIN *isci_domain) { if (!isci_controller->has_been_scanned) { /* Controller has not been scanned yet. We'll clear * the discovery bit for this domain, then check if all bits * are now clear. That would indicate that all domains are * done with discovery and we can then proceed with initial * scan. */ isci_controller->initial_discovery_mask &= ~(1 << isci_domain->index); if (isci_controller->initial_discovery_mask == 0) { struct isci_softc *driver = isci_controller->isci; uint8_t next_index = isci_controller->index + 1; isci_controller->has_been_scanned = TRUE; /* Unfreeze simq to allow initial scan to proceed. */ xpt_release_simq(isci_controller->sim, TRUE); #if __FreeBSD_version < 800000 /* When driver is loaded after boot, we need to * explicitly rescan here for versions <8.0, because * CAM only automatically scans new buses at boot * time. */ union ccb *ccb = xpt_alloc_ccb_nowait(); xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(isci_controller->sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); xpt_rescan(ccb); #endif if (next_index < driver->controller_count) { /* There are more controllers that need to * start. So start the next one. */ isci_controller_start( &driver->controllers[next_index]); } else { /* All controllers have been started and completed discovery. * Disestablish the config hook while will signal to the * kernel during boot that it is safe to try to find and * mount the root partition. */ config_intrhook_disestablish( &driver->config_hook); } } } } int isci_controller_attach_to_cam(struct ISCI_CONTROLLER *controller) { struct isci_softc *isci = controller->isci; device_t parent = device_get_parent(isci->device); int unit = device_get_unit(isci->device); struct cam_devq *isci_devq = cam_simq_alloc(controller->sim_queue_depth); if(isci_devq == NULL) { isci_log_message(0, "ISCI", "isci_devq is NULL \n"); return (-1); } controller->sim = cam_sim_alloc(isci_action, isci_poll, "isci", controller, unit, &controller->lock, controller->sim_queue_depth, controller->sim_queue_depth, isci_devq); if(controller->sim == NULL) { isci_log_message(0, "ISCI", "cam_sim_alloc... fails\n"); cam_simq_free(isci_devq); return (-1); } if(xpt_bus_register(controller->sim, parent, controller->index) != CAM_SUCCESS) { isci_log_message(0, "ISCI", "xpt_bus_register...fails \n"); cam_sim_free(controller->sim, TRUE); mtx_unlock(&controller->lock); return (-1); } if(xpt_create_path(&controller->path, NULL, cam_sim_path(controller->sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { isci_log_message(0, "ISCI", "xpt_create_path....fails\n"); xpt_bus_deregister(cam_sim_path(controller->sim)); cam_sim_free(controller->sim, TRUE); mtx_unlock(&controller->lock); return (-1); } return (0); } void isci_poll(struct cam_sim *sim) { struct ISCI_CONTROLLER *controller = (struct ISCI_CONTROLLER *)cam_sim_softc(sim); isci_interrupt_poll_handler(controller); } void isci_action(struct cam_sim *sim, union ccb *ccb) { struct ISCI_CONTROLLER *controller = (struct ISCI_CONTROLLER *)cam_sim_softc(sim); switch ( ccb->ccb_h.func_code ) { case XPT_PATH_INQ: { struct ccb_pathinq *cpi = &ccb->cpi; int bus = cam_sim_bus(sim); ccb->ccb_h.ccb_sim_ptr = sim; cpi->version_num = 1; cpi->hba_inquiry = PI_TAG_ABLE; cpi->target_sprt = 0; cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN | PIM_UNMAPPED; cpi->hba_eng_cnt = 0; cpi->max_target = SCI_MAX_REMOTE_DEVICES - 1; cpi->max_lun = ISCI_MAX_LUN; #if __FreeBSD_version >= 800102 cpi->maxio = isci_io_request_get_max_io_size(); #endif cpi->unit_number = cam_sim_unit(sim); cpi->bus_id = bus; cpi->initiator_id = SCI_MAX_REMOTE_DEVICES; cpi->base_transfer_speed = 300000; strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); strlcpy(cpi->hba_vid, "Intel Corp.", HBA_IDLEN); strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); cpi->transport = XPORT_SAS; cpi->transport_version = 0; cpi->protocol = PROTO_SCSI; cpi->protocol_version = SCSI_REV_SPC2; cpi->ccb_h.status = CAM_REQ_CMP; xpt_done(ccb); } break; case XPT_GET_TRAN_SETTINGS: { struct ccb_trans_settings *general_settings = &ccb->cts; struct ccb_trans_settings_sas *sas_settings = &general_settings->xport_specific.sas; struct ccb_trans_settings_scsi *scsi_settings = &general_settings->proto_specific.scsi; struct ISCI_REMOTE_DEVICE *remote_device; remote_device = controller->remote_device[ccb->ccb_h.target_id]; if (remote_device == NULL) { ccb->ccb_h.status &= ~CAM_SIM_QUEUED; ccb->ccb_h.status &= ~CAM_STATUS_MASK; ccb->ccb_h.status |= CAM_DEV_NOT_THERE; xpt_done(ccb); break; } general_settings->protocol = PROTO_SCSI; general_settings->transport = XPORT_SAS; general_settings->protocol_version = SCSI_REV_SPC2; general_settings->transport_version = 0; scsi_settings->valid = CTS_SCSI_VALID_TQ; scsi_settings->flags = CTS_SCSI_FLAGS_TAG_ENB; ccb->ccb_h.status &= ~CAM_STATUS_MASK; ccb->ccb_h.status |= CAM_REQ_CMP; sas_settings->bitrate = isci_remote_device_get_bitrate(remote_device); if (sas_settings->bitrate != 0) sas_settings->valid = CTS_SAS_VALID_SPEED; xpt_done(ccb); } break; case XPT_SCSI_IO: if (ccb->ccb_h.flags & CAM_CDB_PHYS) { ccb->ccb_h.status = CAM_REQ_INVALID; xpt_done(ccb); break; } isci_io_request_execute_scsi_io(ccb, controller); break; #if __FreeBSD_version >= 900026 case XPT_SMP_IO: isci_io_request_execute_smp_io(ccb, controller); break; #endif case XPT_SET_TRAN_SETTINGS: ccb->ccb_h.status &= ~CAM_STATUS_MASK; ccb->ccb_h.status |= CAM_REQ_CMP; xpt_done(ccb); break; case XPT_CALC_GEOMETRY: cam_calc_geometry(&ccb->ccg, /*extended*/1); xpt_done(ccb); break; case XPT_RESET_DEV: { struct ISCI_REMOTE_DEVICE *remote_device = controller->remote_device[ccb->ccb_h.target_id]; if (remote_device != NULL) isci_remote_device_reset(remote_device, ccb); else { ccb->ccb_h.status &= ~CAM_SIM_QUEUED; ccb->ccb_h.status &= ~CAM_STATUS_MASK; ccb->ccb_h.status |= CAM_DEV_NOT_THERE; xpt_done(ccb); } } break; case XPT_RESET_BUS: ccb->ccb_h.status = CAM_REQ_CMP; xpt_done(ccb); break; default: isci_log_message(0, "ISCI", "Unhandled func_code 0x%x\n", ccb->ccb_h.func_code); ccb->ccb_h.status &= ~CAM_SIM_QUEUED; ccb->ccb_h.status &= ~CAM_STATUS_MASK; ccb->ccb_h.status |= CAM_REQ_INVALID; xpt_done(ccb); break; } } /* * Unfortunately, SCIL doesn't cleanly handle retry conditions. * CAM_REQUEUE_REQ works only when no one is using the pass(4) interface. So * when SCIL denotes an I/O needs to be retried (typically because of mixing * tagged/non-tagged ATA commands, or running out of NCQ slots), we queue * these I/O internally. Once SCIL completes an I/O to this device, or we get * a ready notification, we will retry the first I/O on the queue. * Unfortunately, SCIL also doesn't cleanly handle starting the new I/O within * the context of the completion handler, so we need to retry these I/O after * the completion handler is done executing. */ void isci_controller_release_queued_ccbs(struct ISCI_CONTROLLER *controller) { struct ISCI_REMOTE_DEVICE *dev; struct ccb_hdr *ccb_h; uint8_t *ptr; int dev_idx; KASSERT(mtx_owned(&controller->lock), ("controller lock not owned")); controller->release_queued_ccbs = FALSE; for (dev_idx = 0; dev_idx < SCI_MAX_REMOTE_DEVICES; dev_idx++) { dev = controller->remote_device[dev_idx]; if (dev != NULL && dev->release_queued_ccb == TRUE && dev->queued_ccb_in_progress == NULL) { dev->release_queued_ccb = FALSE; ccb_h = TAILQ_FIRST(&dev->queued_ccbs); if (ccb_h == NULL) continue; ptr = scsiio_cdb_ptr(&((union ccb *)ccb_h)->csio); isci_log_message(1, "ISCI", "release %p %x\n", ccb_h, *ptr); dev->queued_ccb_in_progress = (union ccb *)ccb_h; isci_io_request_execute_scsi_io( (union ccb *)ccb_h, controller); } } }