isci_controller.c revision 312850
1250199Sgrehan/*- 2250199Sgrehan * BSD LICENSE 3250199Sgrehan * 4250199Sgrehan * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. 5250199Sgrehan * All rights reserved. 6250199Sgrehan * 7250199Sgrehan * Redistribution and use in source and binary forms, with or without 8250199Sgrehan * modification, are permitted provided that the following conditions 9250199Sgrehan * are met: 10250199Sgrehan * 11250199Sgrehan * * Redistributions of source code must retain the above copyright 12250199Sgrehan * notice, this list of conditions and the following disclaimer. 13250199Sgrehan * * Redistributions in binary form must reproduce the above copyright 14250199Sgrehan * notice, this list of conditions and the following disclaimer in 15250199Sgrehan * the documentation and/or other materials provided with the 16250199Sgrehan * distribution. 17250199Sgrehan * 18250199Sgrehan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19250199Sgrehan * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20250199Sgrehan * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21250199Sgrehan * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22250199Sgrehan * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23250199Sgrehan * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24250199Sgrehan * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25250199Sgrehan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26250199Sgrehan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27250199Sgrehan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28250199Sgrehan * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29256276Sdim */ 30256276Sdim 31256276Sdim#include <sys/cdefs.h> 32250199Sgrehan__FBSDID("$FreeBSD: stable/10/sys/dev/isci/isci_controller.c 312850 2017-01-26 21:35:58Z mav $"); 33250199Sgrehan 34250199Sgrehan#include <dev/isci/isci.h> 35250199Sgrehan 36250199Sgrehan#include <sys/conf.h> 37250199Sgrehan#include <sys/malloc.h> 38250199Sgrehan 39250199Sgrehan#include <cam/cam_periph.h> 40250199Sgrehan#include <cam/cam_xpt_periph.h> 41250199Sgrehan 42250199Sgrehan#include <dev/isci/scil/sci_memory_descriptor_list.h> 43250199Sgrehan#include <dev/isci/scil/sci_memory_descriptor_list_decorator.h> 44250199Sgrehan 45250199Sgrehan#include <dev/isci/scil/scif_controller.h> 46250199Sgrehan#include <dev/isci/scil/scif_library.h> 47250199Sgrehan#include <dev/isci/scil/scif_io_request.h> 48250199Sgrehan#include <dev/isci/scil/scif_task_request.h> 49250199Sgrehan#include <dev/isci/scil/scif_remote_device.h> 50250199Sgrehan#include <dev/isci/scil/scif_domain.h> 51250199Sgrehan#include <dev/isci/scil/scif_user_callback.h> 52250199Sgrehan#include <dev/isci/scil/scic_sgpio.h> 53250199Sgrehan 54250199Sgrehan#include <dev/led/led.h> 55250199Sgrehan 56250199Sgrehanvoid isci_action(struct cam_sim *sim, union ccb *ccb); 57250199Sgrehanvoid isci_poll(struct cam_sim *sim); 58250199Sgrehan 59250199Sgrehan#define ccb_sim_ptr sim_priv.entries[0].ptr 60250199Sgrehan 61250199Sgrehan/** 62250199Sgrehan * @brief This user callback will inform the user that the controller has 63250199Sgrehan * had a serious unexpected error. The user should not the error, 64250199Sgrehan * disable interrupts, and wait for current ongoing processing to 65250199Sgrehan * complete. Subsequently, the user should reset the controller. 66250199Sgrehan * 67250199Sgrehan * @param[in] controller This parameter specifies the controller that had 68250199Sgrehan * an error. 69250199Sgrehan * 70250199Sgrehan * @return none 71250199Sgrehan */ 72250199Sgrehanvoid scif_cb_controller_error(SCI_CONTROLLER_HANDLE_T controller, 73250199Sgrehan SCI_CONTROLLER_ERROR error) 74250199Sgrehan{ 75250199Sgrehan 76256276Sdim isci_log_message(0, "ISCI", "scif_cb_controller_error: 0x%x\n", 77250199Sgrehan error); 78250199Sgrehan} 79250199Sgrehan 80250199Sgrehan/** 81250199Sgrehan * @brief This user callback will inform the user that the controller has 82250199Sgrehan * finished the start process. 83250199Sgrehan * 84250199Sgrehan * @param[in] controller This parameter specifies the controller that was 85250199Sgrehan * started. 86250199Sgrehan * @param[in] completion_status This parameter specifies the results of 87250199Sgrehan * the start operation. SCI_SUCCESS indicates successful 88250199Sgrehan * completion. 89250199Sgrehan * 90250199Sgrehan * @return none 91250199Sgrehan */ 92250199Sgrehanvoid scif_cb_controller_start_complete(SCI_CONTROLLER_HANDLE_T controller, 93250199Sgrehan SCI_STATUS completion_status) 94250199Sgrehan{ 95250199Sgrehan uint32_t index; 96250199Sgrehan struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) 97250199Sgrehan sci_object_get_association(controller); 98250199Sgrehan 99250199Sgrehan isci_controller->is_started = TRUE; 100250199Sgrehan 101250199Sgrehan /* Set bits for all domains. We will clear them one-by-one once 102250199Sgrehan * the domains complete discovery, or return error when calling 103250199Sgrehan * scif_domain_discover. Once all bits are clear, we will register 104250199Sgrehan * the controller with CAM. 105250199Sgrehan */ 106250199Sgrehan isci_controller->initial_discovery_mask = (1 << SCI_MAX_DOMAINS) - 1; 107256350Sgrehan 108250199Sgrehan for(index = 0; index < SCI_MAX_DOMAINS; index++) { 109250199Sgrehan SCI_STATUS status; 110250199Sgrehan SCI_DOMAIN_HANDLE_T domain = 111256350Sgrehan isci_controller->domain[index].sci_object; 112250199Sgrehan 113250199Sgrehan status = scif_domain_discover( 114250199Sgrehan domain, 115250199Sgrehan scif_domain_get_suggested_discover_timeout(domain), 116256350Sgrehan DEVICE_TIMEOUT 117256350Sgrehan ); 118256350Sgrehan 119256350Sgrehan if (status != SCI_SUCCESS) 120250199Sgrehan { 121250199Sgrehan isci_controller_domain_discovery_complete( 122250199Sgrehan isci_controller, &isci_controller->domain[index]); 123250199Sgrehan } 124250199Sgrehan } 125250199Sgrehan} 126250199Sgrehan 127250199Sgrehan/** 128250199Sgrehan * @brief This user callback will inform the user that the controller has 129250199Sgrehan * finished the stop process. Note, after user calls 130250199Sgrehan * scif_controller_stop(), before user receives this controller stop 131250199Sgrehan * complete callback, user should not expect any callback from 132250199Sgrehan * framework, such like scif_cb_domain_change_notification(). 133250199Sgrehan * 134250199Sgrehan * @param[in] controller This parameter specifies the controller that was 135250199Sgrehan * stopped. 136250199Sgrehan * @param[in] completion_status This parameter specifies the results of 137250199Sgrehan * the stop operation. SCI_SUCCESS indicates successful 138250199Sgrehan * completion. 139250199Sgrehan * 140250199Sgrehan * @return none 141250199Sgrehan */ 142250199Sgrehanvoid scif_cb_controller_stop_complete(SCI_CONTROLLER_HANDLE_T controller, 143250199Sgrehan SCI_STATUS completion_status) 144250199Sgrehan{ 145250199Sgrehan struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) 146250199Sgrehan sci_object_get_association(controller); 147250199Sgrehan 148250199Sgrehan isci_controller->is_started = FALSE; 149250199Sgrehan} 150250199Sgrehan 151250199Sgrehanstatic void 152250199Sgrehanisci_single_map(void *arg, bus_dma_segment_t *seg, int nseg, int error) 153250199Sgrehan{ 154250199Sgrehan SCI_PHYSICAL_ADDRESS *phys_addr = arg; 155250199Sgrehan 156250199Sgrehan *phys_addr = seg[0].ds_addr; 157250199Sgrehan} 158250199Sgrehan 159250199Sgrehan/** 160250199Sgrehan * @brief This method will be invoked to allocate memory dynamically. 161250199Sgrehan * 162250199Sgrehan * @param[in] controller This parameter represents the controller 163250199Sgrehan * object for which to allocate memory. 164250199Sgrehan * @param[out] mde This parameter represents the memory descriptor to 165250199Sgrehan * be filled in by the user that will reference the newly 166250199Sgrehan * allocated memory. 167250199Sgrehan * 168250199Sgrehan * @return none 169250199Sgrehan */ 170250199Sgrehanvoid scif_cb_controller_allocate_memory(SCI_CONTROLLER_HANDLE_T controller, 171250199Sgrehan SCI_PHYSICAL_MEMORY_DESCRIPTOR_T *mde) 172250199Sgrehan{ 173250199Sgrehan struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) 174250199Sgrehan sci_object_get_association(controller); 175250199Sgrehan 176250199Sgrehan /* 177250199Sgrehan * Note this routine is only used for buffers needed to translate 178250199Sgrehan * SCSI UNMAP commands to ATA DSM commands for SATA disks. 179250199Sgrehan * 180250199Sgrehan * We first try to pull a buffer from the controller's pool, and only 181250199Sgrehan * call contigmalloc if one isn't there. 182250199Sgrehan */ 183250199Sgrehan if (!sci_pool_empty(isci_controller->unmap_buffer_pool)) { 184250199Sgrehan sci_pool_get(isci_controller->unmap_buffer_pool, 185250199Sgrehan mde->virtual_address); 186250199Sgrehan } else 187250199Sgrehan mde->virtual_address = contigmalloc(PAGE_SIZE, 188250199Sgrehan M_ISCI, M_NOWAIT, 0, BUS_SPACE_MAXADDR, 189250199Sgrehan mde->constant_memory_alignment, 0); 190250199Sgrehan 191250199Sgrehan if (mde->virtual_address != NULL) 192250199Sgrehan bus_dmamap_load(isci_controller->buffer_dma_tag, 193250199Sgrehan NULL, mde->virtual_address, PAGE_SIZE, 194250199Sgrehan isci_single_map, &mde->physical_address, 195250199Sgrehan BUS_DMA_NOWAIT); 196250199Sgrehan} 197250199Sgrehan 198250199Sgrehan/** 199250199Sgrehan * @brief This method will be invoked to allocate memory dynamically. 200250199Sgrehan * 201250199Sgrehan * @param[in] controller This parameter represents the controller 202250199Sgrehan * object for which to allocate memory. 203250199Sgrehan * @param[out] mde This parameter represents the memory descriptor to 204250199Sgrehan * be filled in by the user that will reference the newly 205250199Sgrehan * allocated memory. 206250199Sgrehan * 207250199Sgrehan * @return none 208250199Sgrehan */ 209250199Sgrehanvoid scif_cb_controller_free_memory(SCI_CONTROLLER_HANDLE_T controller, 210250199Sgrehan SCI_PHYSICAL_MEMORY_DESCRIPTOR_T * mde) 211250199Sgrehan{ 212250199Sgrehan struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *) 213250199Sgrehan sci_object_get_association(controller); 214250199Sgrehan 215250199Sgrehan /* 216250199Sgrehan * Put the buffer back into the controller's buffer pool, rather 217250199Sgrehan * than invoking configfree. This helps reduce chance we won't 218250199Sgrehan * have buffers available when system is under memory pressure. 219250199Sgrehan */ 220250199Sgrehan sci_pool_put(isci_controller->unmap_buffer_pool, 221250199Sgrehan mde->virtual_address); 222250199Sgrehan} 223250199Sgrehan 224250199Sgrehanvoid isci_controller_construct(struct ISCI_CONTROLLER *controller, 225250199Sgrehan struct isci_softc *isci) 226250199Sgrehan{ 227250199Sgrehan SCI_CONTROLLER_HANDLE_T scif_controller_handle; 228250199Sgrehan 229250199Sgrehan scif_library_allocate_controller(isci->sci_library_handle, 230250199Sgrehan &scif_controller_handle); 231250199Sgrehan 232250199Sgrehan scif_controller_construct(isci->sci_library_handle, 233250199Sgrehan scif_controller_handle, NULL); 234250199Sgrehan 235250199Sgrehan controller->isci = isci; 236250199Sgrehan controller->scif_controller_handle = scif_controller_handle; 237250199Sgrehan 238250199Sgrehan /* This allows us to later use 239250199Sgrehan * sci_object_get_association(scif_controller_handle) 240250199Sgrehan * inside of a callback routine to get our struct ISCI_CONTROLLER object 241250199Sgrehan */ 242250199Sgrehan sci_object_set_association(scif_controller_handle, (void *)controller); 243250199Sgrehan 244250199Sgrehan controller->is_started = FALSE; 245250199Sgrehan controller->is_frozen = FALSE; 246250199Sgrehan controller->release_queued_ccbs = FALSE; 247250199Sgrehan controller->sim = NULL; 248250199Sgrehan controller->initial_discovery_mask = 0; 249250199Sgrehan 250250199Sgrehan sci_fast_list_init(&controller->pending_device_reset_list); 251250199Sgrehan 252250199Sgrehan mtx_init(&controller->lock, "isci", NULL, MTX_DEF); 253250199Sgrehan 254250199Sgrehan uint32_t domain_index; 255250199Sgrehan 256250199Sgrehan for(domain_index = 0; domain_index < SCI_MAX_DOMAINS; domain_index++) { 257250199Sgrehan isci_domain_construct( &controller->domain[domain_index], 258250199Sgrehan domain_index, controller); 259250199Sgrehan } 260250199Sgrehan 261250199Sgrehan controller->timer_memory = malloc( 262250199Sgrehan sizeof(struct ISCI_TIMER) * SCI_MAX_TIMERS, M_ISCI, 263250199Sgrehan M_NOWAIT | M_ZERO); 264250199Sgrehan 265250199Sgrehan sci_pool_initialize(controller->timer_pool); 266250199Sgrehan 267250199Sgrehan struct ISCI_TIMER *timer = (struct ISCI_TIMER *) 268250199Sgrehan controller->timer_memory; 269250199Sgrehan 270250199Sgrehan for ( int i = 0; i < SCI_MAX_TIMERS; i++ ) { 271250199Sgrehan sci_pool_put(controller->timer_pool, timer++); 272250199Sgrehan } 273250199Sgrehan 274250199Sgrehan sci_pool_initialize(controller->unmap_buffer_pool); 275250199Sgrehan} 276250199Sgrehan 277250199Sgrehanstatic void isci_led_fault_func(void *priv, int onoff) 278250199Sgrehan{ 279250199Sgrehan struct ISCI_PHY *phy = priv; 280250199Sgrehan 281250199Sgrehan /* map onoff to the fault LED */ 282250199Sgrehan phy->led_fault = onoff; 283250199Sgrehan scic_sgpio_update_led_state(phy->handle, 1 << phy->index, 284250199Sgrehan phy->led_fault, phy->led_locate, 0); 285250199Sgrehan} 286250199Sgrehan 287250199Sgrehanstatic void isci_led_locate_func(void *priv, int onoff) 288250199Sgrehan{ 289250199Sgrehan struct ISCI_PHY *phy = priv; 290250199Sgrehan 291250199Sgrehan /* map onoff to the locate LED */ 292250199Sgrehan phy->led_locate = onoff; 293250199Sgrehan scic_sgpio_update_led_state(phy->handle, 1 << phy->index, 294250199Sgrehan phy->led_fault, phy->led_locate, 0); 295250199Sgrehan} 296250199Sgrehan 297250199SgrehanSCI_STATUS isci_controller_initialize(struct ISCI_CONTROLLER *controller) 298250199Sgrehan{ 299250199Sgrehan SCIC_USER_PARAMETERS_T scic_user_parameters; 300250199Sgrehan SCI_CONTROLLER_HANDLE_T scic_controller_handle; 301250199Sgrehan char led_name[64]; 302250199Sgrehan unsigned long tunable; 303250199Sgrehan uint32_t io_shortage; 304250199Sgrehan uint32_t fail_on_timeout; 305250199Sgrehan int i; 306250199Sgrehan 307250199Sgrehan scic_controller_handle = 308250199Sgrehan scif_controller_get_scic_handle(controller->scif_controller_handle); 309250199Sgrehan 310250199Sgrehan if (controller->isci->oem_parameters_found == TRUE) 311250199Sgrehan { 312250199Sgrehan scic_oem_parameters_set( 313250199Sgrehan scic_controller_handle, 314250199Sgrehan &controller->oem_parameters, 315250199Sgrehan (uint8_t)(controller->oem_parameters_version)); 316250199Sgrehan } 317250199Sgrehan 318250199Sgrehan scic_user_parameters_get(scic_controller_handle, &scic_user_parameters); 319250199Sgrehan 320250199Sgrehan if (TUNABLE_ULONG_FETCH("hw.isci.no_outbound_task_timeout", &tunable)) 321250199Sgrehan scic_user_parameters.sds1.no_outbound_task_timeout = 322250199Sgrehan (uint8_t)tunable; 323250199Sgrehan 324250199Sgrehan if (TUNABLE_ULONG_FETCH("hw.isci.ssp_max_occupancy_timeout", &tunable)) 325250199Sgrehan scic_user_parameters.sds1.ssp_max_occupancy_timeout = 326250199Sgrehan (uint16_t)tunable; 327250199Sgrehan 328250199Sgrehan if (TUNABLE_ULONG_FETCH("hw.isci.stp_max_occupancy_timeout", &tunable)) 329250199Sgrehan scic_user_parameters.sds1.stp_max_occupancy_timeout = 330250199Sgrehan (uint16_t)tunable; 331250199Sgrehan 332250199Sgrehan if (TUNABLE_ULONG_FETCH("hw.isci.ssp_inactivity_timeout", &tunable)) 333250199Sgrehan scic_user_parameters.sds1.ssp_inactivity_timeout = 334250199Sgrehan (uint16_t)tunable; 335250199Sgrehan 336250199Sgrehan if (TUNABLE_ULONG_FETCH("hw.isci.stp_inactivity_timeout", &tunable)) 337250199Sgrehan scic_user_parameters.sds1.stp_inactivity_timeout = 338250199Sgrehan (uint16_t)tunable; 339250199Sgrehan 340250199Sgrehan if (TUNABLE_ULONG_FETCH("hw.isci.max_speed_generation", &tunable)) 341250199Sgrehan for (i = 0; i < SCI_MAX_PHYS; i++) 342250199Sgrehan scic_user_parameters.sds1.phys[i].max_speed_generation = 343250199Sgrehan (uint8_t)tunable; 344250199Sgrehan 345250199Sgrehan scic_user_parameters_set(scic_controller_handle, &scic_user_parameters); 346250199Sgrehan 347250199Sgrehan /* Scheduler bug in SCU requires SCIL to reserve some task contexts as a 348250199Sgrehan * a workaround - one per domain. 349250199Sgrehan */ 350250199Sgrehan controller->queue_depth = SCI_MAX_IO_REQUESTS - SCI_MAX_DOMAINS; 351250199Sgrehan 352250199Sgrehan if (TUNABLE_INT_FETCH("hw.isci.controller_queue_depth", 353250199Sgrehan &controller->queue_depth)) { 354250199Sgrehan controller->queue_depth = max(1, min(controller->queue_depth, 355250199Sgrehan SCI_MAX_IO_REQUESTS - SCI_MAX_DOMAINS)); 356250199Sgrehan } 357250199Sgrehan 358250199Sgrehan /* Reserve one request so that we can ensure we have one available TC 359250199Sgrehan * to do internal device resets. 360250199Sgrehan */ 361250199Sgrehan controller->sim_queue_depth = controller->queue_depth - 1; 362250199Sgrehan 363250199Sgrehan /* Although we save one TC to do internal device resets, it is possible 364250199Sgrehan * we could end up using several TCs for simultaneous device resets 365250199Sgrehan * while at the same time having CAM fill our controller queue. To 366250199Sgrehan * simulate this condition, and how our driver handles it, we can set 367250199Sgrehan * this io_shortage parameter, which will tell CAM that we have a 368250199Sgrehan * large queue depth than we really do. 369250199Sgrehan */ 370250199Sgrehan io_shortage = 0; 371250199Sgrehan TUNABLE_INT_FETCH("hw.isci.io_shortage", &io_shortage); 372250199Sgrehan controller->sim_queue_depth += io_shortage; 373250199Sgrehan 374250199Sgrehan fail_on_timeout = 1; 375250199Sgrehan TUNABLE_INT_FETCH("hw.isci.fail_on_task_timeout", &fail_on_timeout); 376250199Sgrehan controller->fail_on_task_timeout = fail_on_timeout; 377250199Sgrehan 378250199Sgrehan /* Attach to CAM using xpt_bus_register now, then immediately freeze 379250199Sgrehan * the simq. It will get released later when initial domain discovery 380250199Sgrehan * is complete. 381250199Sgrehan */ 382250199Sgrehan controller->has_been_scanned = FALSE; 383250199Sgrehan mtx_lock(&controller->lock); 384250199Sgrehan isci_controller_attach_to_cam(controller); 385250199Sgrehan xpt_freeze_simq(controller->sim, 1); 386250199Sgrehan mtx_unlock(&controller->lock); 387250199Sgrehan 388250199Sgrehan for (i = 0; i < SCI_MAX_PHYS; i++) { 389250199Sgrehan controller->phys[i].handle = scic_controller_handle; 390250199Sgrehan controller->phys[i].index = i; 391250199Sgrehan 392250199Sgrehan /* fault */ 393250199Sgrehan controller->phys[i].led_fault = 0; 394250199Sgrehan sprintf(led_name, "isci.bus%d.port%d.fault", controller->index, i); 395250199Sgrehan controller->phys[i].cdev_fault = led_create(isci_led_fault_func, 396250199Sgrehan &controller->phys[i], led_name); 397250199Sgrehan 398250199Sgrehan /* locate */ 399250199Sgrehan controller->phys[i].led_locate = 0; 400250199Sgrehan sprintf(led_name, "isci.bus%d.port%d.locate", controller->index, i); 401250199Sgrehan controller->phys[i].cdev_locate = led_create(isci_led_locate_func, 402250199Sgrehan &controller->phys[i], led_name); 403250199Sgrehan } 404250199Sgrehan 405250199Sgrehan return (scif_controller_initialize(controller->scif_controller_handle)); 406250199Sgrehan} 407250199Sgrehan 408250199Sgrehanint isci_controller_allocate_memory(struct ISCI_CONTROLLER *controller) 409250199Sgrehan{ 410250199Sgrehan int error; 411250199Sgrehan device_t device = controller->isci->device; 412250199Sgrehan uint32_t max_segment_size = isci_io_request_get_max_io_size(); 413250199Sgrehan uint32_t status = 0; 414250199Sgrehan struct ISCI_MEMORY *uncached_controller_memory = 415250199Sgrehan &controller->uncached_controller_memory; 416250199Sgrehan struct ISCI_MEMORY *cached_controller_memory = 417250199Sgrehan &controller->cached_controller_memory; 418250199Sgrehan struct ISCI_MEMORY *request_memory = 419250199Sgrehan &controller->request_memory; 420250199Sgrehan POINTER_UINT virtual_address; 421250199Sgrehan bus_addr_t physical_address; 422250199Sgrehan 423250199Sgrehan controller->mdl = sci_controller_get_memory_descriptor_list_handle( 424250199Sgrehan controller->scif_controller_handle); 425250199Sgrehan 426250199Sgrehan uncached_controller_memory->size = sci_mdl_decorator_get_memory_size( 427250199Sgrehan controller->mdl, SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS); 428250199Sgrehan 429250199Sgrehan error = isci_allocate_dma_buffer(device, uncached_controller_memory); 430250199Sgrehan 431250199Sgrehan if (error != 0) 432250199Sgrehan return (error); 433250199Sgrehan 434250199Sgrehan sci_mdl_decorator_assign_memory( controller->mdl, 435250199Sgrehan SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS, 436250199Sgrehan uncached_controller_memory->virtual_address, 437250199Sgrehan uncached_controller_memory->physical_address); 438250199Sgrehan 439250199Sgrehan cached_controller_memory->size = sci_mdl_decorator_get_memory_size( 440250199Sgrehan controller->mdl, 441250199Sgrehan SCI_MDE_ATTRIBUTE_CACHEABLE | SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS 442250199Sgrehan ); 443250199Sgrehan 444250199Sgrehan error = isci_allocate_dma_buffer(device, cached_controller_memory); 445250199Sgrehan 446250199Sgrehan if (error != 0) 447250199Sgrehan return (error); 448250199Sgrehan 449250199Sgrehan sci_mdl_decorator_assign_memory(controller->mdl, 450250199Sgrehan SCI_MDE_ATTRIBUTE_CACHEABLE | SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS, 451250199Sgrehan cached_controller_memory->virtual_address, 452250199Sgrehan cached_controller_memory->physical_address); 453250199Sgrehan 454250199Sgrehan request_memory->size = 455250199Sgrehan controller->queue_depth * isci_io_request_get_object_size(); 456250199Sgrehan 457250199Sgrehan error = isci_allocate_dma_buffer(device, request_memory); 458250199Sgrehan 459250199Sgrehan if (error != 0) 460250199Sgrehan return (error); 461250199Sgrehan 462250199Sgrehan /* For STP PIO testing, we want to ensure we can force multiple SGLs 463250199Sgrehan * since this has been a problem area in SCIL. This tunable parameter 464250199Sgrehan * will allow us to force DMA segments to a smaller size, ensuring 465250199Sgrehan * that even if a physically contiguous buffer is attached to this 466250199Sgrehan * I/O, the DMA subsystem will pass us multiple segments in our DMA 467250199Sgrehan * load callback. 468250199Sgrehan */ 469250199Sgrehan TUNABLE_INT_FETCH("hw.isci.max_segment_size", &max_segment_size); 470250199Sgrehan 471250199Sgrehan /* Create DMA tag for our I/O requests. Then we can create DMA maps based off 472250199Sgrehan * of this tag and store them in each of our ISCI_IO_REQUEST objects. This 473250199Sgrehan * will enable better performance than creating the DMA maps everytime we get 474250199Sgrehan * an I/O. 475250199Sgrehan */ 476250199Sgrehan status = bus_dma_tag_create(bus_get_dma_tag(device), 0x1, 0x0, 477250199Sgrehan BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 478250199Sgrehan isci_io_request_get_max_io_size(), 479250199Sgrehan SCI_MAX_SCATTER_GATHER_ELEMENTS, max_segment_size, 0, NULL, NULL, 480250199Sgrehan &controller->buffer_dma_tag); 481250199Sgrehan 482250199Sgrehan sci_pool_initialize(controller->request_pool); 483250199Sgrehan 484250199Sgrehan virtual_address = request_memory->virtual_address; 485250199Sgrehan physical_address = request_memory->physical_address; 486250199Sgrehan 487250199Sgrehan for (int i = 0; i < controller->queue_depth; i++) { 488250199Sgrehan struct ISCI_REQUEST *request = 489250199Sgrehan (struct ISCI_REQUEST *)virtual_address; 490250199Sgrehan 491250199Sgrehan isci_request_construct(request, 492250199Sgrehan controller->scif_controller_handle, 493250199Sgrehan controller->buffer_dma_tag, physical_address); 494250199Sgrehan 495250199Sgrehan sci_pool_put(controller->request_pool, request); 496250199Sgrehan 497250199Sgrehan virtual_address += isci_request_get_object_size(); 498250199Sgrehan physical_address += isci_request_get_object_size(); 499250199Sgrehan } 500250199Sgrehan 501250199Sgrehan uint32_t remote_device_size = sizeof(struct ISCI_REMOTE_DEVICE) + 502250199Sgrehan scif_remote_device_get_object_size(); 503250199Sgrehan 504250199Sgrehan controller->remote_device_memory = (uint8_t *) malloc( 505250199Sgrehan remote_device_size * SCI_MAX_REMOTE_DEVICES, M_ISCI, 506250199Sgrehan M_NOWAIT | M_ZERO); 507250199Sgrehan 508250199Sgrehan sci_pool_initialize(controller->remote_device_pool); 509250199Sgrehan 510250199Sgrehan uint8_t *remote_device_memory_ptr = controller->remote_device_memory; 511250199Sgrehan 512250199Sgrehan for (int i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) { 513250199Sgrehan struct ISCI_REMOTE_DEVICE *remote_device = 514250199Sgrehan (struct ISCI_REMOTE_DEVICE *)remote_device_memory_ptr; 515250199Sgrehan 516250199Sgrehan controller->remote_device[i] = NULL; 517250199Sgrehan remote_device->index = i; 518250199Sgrehan remote_device->is_resetting = FALSE; 519250199Sgrehan remote_device->frozen_lun_mask = 0; 520250199Sgrehan sci_fast_list_element_init(remote_device, 521250199Sgrehan &remote_device->pending_device_reset_element); 522250199Sgrehan TAILQ_INIT(&remote_device->queued_ccbs); 523250199Sgrehan remote_device->release_queued_ccb = FALSE; 524250199Sgrehan remote_device->queued_ccb_in_progress = NULL; 525250199Sgrehan 526250199Sgrehan /* 527250199Sgrehan * For the first SCI_MAX_DOMAINS device objects, do not put 528250199Sgrehan * them in the pool, rather assign them to each domain. This 529250199Sgrehan * ensures that any device attached directly to port "i" will 530250199Sgrehan * always get CAM target id "i". 531250199Sgrehan */ 532250199Sgrehan if (i < SCI_MAX_DOMAINS) 533250199Sgrehan controller->domain[i].da_remote_device = remote_device; 534250199Sgrehan else 535250199Sgrehan sci_pool_put(controller->remote_device_pool, 536250199Sgrehan remote_device); 537250199Sgrehan remote_device_memory_ptr += remote_device_size; 538250199Sgrehan } 539250199Sgrehan 540250199Sgrehan return (0); 541250199Sgrehan} 542250199Sgrehan 543250199Sgrehanvoid isci_controller_start(void *controller_handle) 544256350Sgrehan{ 545256350Sgrehan struct ISCI_CONTROLLER *controller = 546250199Sgrehan (struct ISCI_CONTROLLER *)controller_handle; 547250199Sgrehan SCI_CONTROLLER_HANDLE_T scif_controller_handle = 548250199Sgrehan controller->scif_controller_handle; 549250199Sgrehan 550250199Sgrehan scif_controller_start(scif_controller_handle, 551250199Sgrehan scif_controller_get_suggested_start_timeout(scif_controller_handle)); 552250199Sgrehan 553250199Sgrehan scic_controller_enable_interrupts( 554250199Sgrehan scif_controller_get_scic_handle(controller->scif_controller_handle)); 555250199Sgrehan} 556250199Sgrehan 557250199Sgrehanvoid isci_controller_domain_discovery_complete( 558250199Sgrehan struct ISCI_CONTROLLER *isci_controller, struct ISCI_DOMAIN *isci_domain) 559250199Sgrehan{ 560250199Sgrehan if (!isci_controller->has_been_scanned) 561250199Sgrehan { 562250199Sgrehan /* Controller has not been scanned yet. We'll clear 563250199Sgrehan * the discovery bit for this domain, then check if all bits 564250199Sgrehan * are now clear. That would indicate that all domains are 565250199Sgrehan * done with discovery and we can then proceed with initial 566250199Sgrehan * scan. 567250199Sgrehan */ 568250199Sgrehan 569250199Sgrehan isci_controller->initial_discovery_mask &= 570250199Sgrehan ~(1 << isci_domain->index); 571250199Sgrehan 572250199Sgrehan if (isci_controller->initial_discovery_mask == 0) { 573250199Sgrehan struct isci_softc *driver = isci_controller->isci; 574250199Sgrehan uint8_t next_index = isci_controller->index + 1; 575250199Sgrehan 576250199Sgrehan isci_controller->has_been_scanned = TRUE; 577250199Sgrehan 578250199Sgrehan /* Unfreeze simq to allow initial scan to proceed. */ 579250199Sgrehan xpt_release_simq(isci_controller->sim, TRUE); 580250199Sgrehan 581250199Sgrehan#if __FreeBSD_version < 800000 582250199Sgrehan /* When driver is loaded after boot, we need to 583250199Sgrehan * explicitly rescan here for versions <8.0, because 584250199Sgrehan * CAM only automatically scans new buses at boot 585250199Sgrehan * time. 586250199Sgrehan */ 587250199Sgrehan union ccb *ccb = xpt_alloc_ccb_nowait(); 588250199Sgrehan 589250199Sgrehan xpt_create_path(&ccb->ccb_h.path, NULL, 590250199Sgrehan cam_sim_path(isci_controller->sim), 591250199Sgrehan CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 592250199Sgrehan 593250199Sgrehan xpt_rescan(ccb); 594250199Sgrehan#endif 595250199Sgrehan 596250199Sgrehan if (next_index < driver->controller_count) { 597250199Sgrehan /* There are more controllers that need to 598250199Sgrehan * start. So start the next one. 599250199Sgrehan */ 600250199Sgrehan isci_controller_start( 601250199Sgrehan &driver->controllers[next_index]); 602250199Sgrehan } 603250199Sgrehan else 604250199Sgrehan { 605250199Sgrehan /* All controllers have been started and completed discovery. 606250199Sgrehan * Disestablish the config hook while will signal to the 607250199Sgrehan * kernel during boot that it is safe to try to find and 608250199Sgrehan * mount the root partition. 609250199Sgrehan */ 610250199Sgrehan config_intrhook_disestablish( 611250199Sgrehan &driver->config_hook); 612250199Sgrehan } 613250199Sgrehan } 614250199Sgrehan } 615250199Sgrehan} 616250199Sgrehan 617250199Sgrehanint isci_controller_attach_to_cam(struct ISCI_CONTROLLER *controller) 618250199Sgrehan{ 619250199Sgrehan struct isci_softc *isci = controller->isci; 620250199Sgrehan device_t parent = device_get_parent(isci->device); 621250199Sgrehan int unit = device_get_unit(isci->device); 622250199Sgrehan struct cam_devq *isci_devq = cam_simq_alloc(controller->sim_queue_depth); 623250199Sgrehan 624250199Sgrehan if(isci_devq == NULL) { 625250199Sgrehan isci_log_message(0, "ISCI", "isci_devq is NULL \n"); 626250199Sgrehan return (-1); 627250199Sgrehan } 628250199Sgrehan 629250199Sgrehan controller->sim = cam_sim_alloc(isci_action, isci_poll, "isci", 630250199Sgrehan controller, unit, &controller->lock, controller->sim_queue_depth, 631250199Sgrehan controller->sim_queue_depth, isci_devq); 632250199Sgrehan 633250199Sgrehan if(controller->sim == NULL) { 634250199Sgrehan isci_log_message(0, "ISCI", "cam_sim_alloc... fails\n"); 635250199Sgrehan cam_simq_free(isci_devq); 636250199Sgrehan return (-1); 637250199Sgrehan } 638250199Sgrehan 639250199Sgrehan if(xpt_bus_register(controller->sim, parent, controller->index) 640250199Sgrehan != CAM_SUCCESS) { 641250199Sgrehan isci_log_message(0, "ISCI", "xpt_bus_register...fails \n"); 642250199Sgrehan cam_sim_free(controller->sim, TRUE); 643250199Sgrehan mtx_unlock(&controller->lock); 644250199Sgrehan return (-1); 645250199Sgrehan } 646250199Sgrehan 647250199Sgrehan if(xpt_create_path(&controller->path, NULL, 648250199Sgrehan cam_sim_path(controller->sim), CAM_TARGET_WILDCARD, 649250199Sgrehan CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 650250199Sgrehan isci_log_message(0, "ISCI", "xpt_create_path....fails\n"); 651250199Sgrehan xpt_bus_deregister(cam_sim_path(controller->sim)); 652250199Sgrehan cam_sim_free(controller->sim, TRUE); 653250199Sgrehan mtx_unlock(&controller->lock); 654250199Sgrehan return (-1); 655250199Sgrehan } 656250199Sgrehan 657250199Sgrehan return (0); 658250199Sgrehan} 659250199Sgrehan 660250199Sgrehanvoid isci_poll(struct cam_sim *sim) 661250199Sgrehan{ 662250199Sgrehan struct ISCI_CONTROLLER *controller = 663250199Sgrehan (struct ISCI_CONTROLLER *)cam_sim_softc(sim); 664250199Sgrehan 665250199Sgrehan isci_interrupt_poll_handler(controller); 666250199Sgrehan} 667250199Sgrehan 668250199Sgrehanvoid isci_action(struct cam_sim *sim, union ccb *ccb) 669250199Sgrehan{ 670250199Sgrehan struct ISCI_CONTROLLER *controller = 671250199Sgrehan (struct ISCI_CONTROLLER *)cam_sim_softc(sim); 672250199Sgrehan 673250199Sgrehan switch ( ccb->ccb_h.func_code ) { 674250199Sgrehan case XPT_PATH_INQ: 675250199Sgrehan { 676250199Sgrehan struct ccb_pathinq *cpi = &ccb->cpi; 677250199Sgrehan int bus = cam_sim_bus(sim); 678250199Sgrehan ccb->ccb_h.ccb_sim_ptr = sim; 679250199Sgrehan cpi->version_num = 1; 680250199Sgrehan cpi->hba_inquiry = PI_TAG_ABLE; 681250199Sgrehan cpi->target_sprt = 0; 682250199Sgrehan cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN | 683250199Sgrehan PIM_UNMAPPED; 684250199Sgrehan cpi->hba_eng_cnt = 0; 685250199Sgrehan cpi->max_target = SCI_MAX_REMOTE_DEVICES - 1; 686250199Sgrehan cpi->max_lun = ISCI_MAX_LUN; 687250199Sgrehan#if __FreeBSD_version >= 800102 688250199Sgrehan cpi->maxio = isci_io_request_get_max_io_size(); 689250199Sgrehan#endif 690250199Sgrehan cpi->unit_number = cam_sim_unit(sim); 691250199Sgrehan cpi->bus_id = bus; 692250199Sgrehan cpi->initiator_id = SCI_MAX_REMOTE_DEVICES; 693250199Sgrehan cpi->base_transfer_speed = 300000; 694250199Sgrehan strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 695250199Sgrehan strncpy(cpi->hba_vid, "Intel Corp.", HBA_IDLEN); 696250199Sgrehan strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 697250199Sgrehan cpi->transport = XPORT_SAS; 698250199Sgrehan cpi->transport_version = 0; 699250199Sgrehan cpi->protocol = PROTO_SCSI; 700250199Sgrehan cpi->protocol_version = SCSI_REV_SPC2; 701250199Sgrehan cpi->ccb_h.status = CAM_REQ_CMP; 702250199Sgrehan xpt_done(ccb); 703250199Sgrehan } 704250199Sgrehan break; 705250199Sgrehan case XPT_GET_TRAN_SETTINGS: 706250199Sgrehan { 707250199Sgrehan struct ccb_trans_settings *general_settings = &ccb->cts; 708250199Sgrehan struct ccb_trans_settings_sas *sas_settings = 709250199Sgrehan &general_settings->xport_specific.sas; 710250199Sgrehan struct ccb_trans_settings_scsi *scsi_settings = 711250199Sgrehan &general_settings->proto_specific.scsi; 712250199Sgrehan struct ISCI_REMOTE_DEVICE *remote_device; 713250199Sgrehan 714250199Sgrehan remote_device = controller->remote_device[ccb->ccb_h.target_id]; 715250199Sgrehan 716250199Sgrehan if (remote_device == NULL) { 717250199Sgrehan ccb->ccb_h.status &= ~CAM_SIM_QUEUED; 718250199Sgrehan ccb->ccb_h.status &= ~CAM_STATUS_MASK; 719250199Sgrehan ccb->ccb_h.status |= CAM_DEV_NOT_THERE; 720250199Sgrehan xpt_done(ccb); 721250199Sgrehan break; 722250199Sgrehan } 723250199Sgrehan 724250199Sgrehan general_settings->protocol = PROTO_SCSI; 725250199Sgrehan general_settings->transport = XPORT_SAS; 726250199Sgrehan general_settings->protocol_version = SCSI_REV_SPC2; 727250199Sgrehan general_settings->transport_version = 0; 728250199Sgrehan scsi_settings->valid = CTS_SCSI_VALID_TQ; 729250199Sgrehan scsi_settings->flags = CTS_SCSI_FLAGS_TAG_ENB; 730250199Sgrehan ccb->ccb_h.status &= ~CAM_STATUS_MASK; 731250199Sgrehan ccb->ccb_h.status |= CAM_REQ_CMP; 732250199Sgrehan 733250199Sgrehan sas_settings->bitrate = 734250199Sgrehan isci_remote_device_get_bitrate(remote_device); 735250199Sgrehan 736250199Sgrehan if (sas_settings->bitrate != 0) 737250199Sgrehan sas_settings->valid = CTS_SAS_VALID_SPEED; 738250199Sgrehan 739250199Sgrehan xpt_done(ccb); 740250199Sgrehan } 741250199Sgrehan break; 742250199Sgrehan case XPT_SCSI_IO: 743250199Sgrehan if (ccb->ccb_h.flags & CAM_CDB_PHYS) { 744250199Sgrehan ccb->ccb_h.status = CAM_REQ_INVALID; 745250199Sgrehan xpt_done(ccb); 746250199Sgrehan break; 747250199Sgrehan } 748250199Sgrehan isci_io_request_execute_scsi_io(ccb, controller); 749250199Sgrehan break; 750250199Sgrehan#if __FreeBSD_version >= 900026 751250199Sgrehan case XPT_SMP_IO: 752250199Sgrehan isci_io_request_execute_smp_io(ccb, controller); 753250199Sgrehan break; 754250199Sgrehan#endif 755250199Sgrehan case XPT_SET_TRAN_SETTINGS: 756250199Sgrehan ccb->ccb_h.status &= ~CAM_STATUS_MASK; 757250199Sgrehan ccb->ccb_h.status |= CAM_REQ_CMP; 758250199Sgrehan xpt_done(ccb); 759250199Sgrehan break; 760250199Sgrehan case XPT_CALC_GEOMETRY: 761250199Sgrehan cam_calc_geometry(&ccb->ccg, /*extended*/1); 762250199Sgrehan xpt_done(ccb); 763250199Sgrehan break; 764250199Sgrehan case XPT_RESET_DEV: 765250199Sgrehan { 766250199Sgrehan struct ISCI_REMOTE_DEVICE *remote_device = 767250199Sgrehan controller->remote_device[ccb->ccb_h.target_id]; 768250199Sgrehan 769250199Sgrehan if (remote_device != NULL) 770250199Sgrehan isci_remote_device_reset(remote_device, ccb); 771250199Sgrehan else { 772250199Sgrehan ccb->ccb_h.status &= ~CAM_SIM_QUEUED; 773250199Sgrehan ccb->ccb_h.status &= ~CAM_STATUS_MASK; 774250199Sgrehan ccb->ccb_h.status |= CAM_DEV_NOT_THERE; 775250199Sgrehan xpt_done(ccb); 776250199Sgrehan } 777250199Sgrehan } 778250199Sgrehan break; 779250199Sgrehan case XPT_RESET_BUS: 780250199Sgrehan ccb->ccb_h.status = CAM_REQ_CMP; 781250199Sgrehan xpt_done(ccb); 782250199Sgrehan break; 783250199Sgrehan default: 784250199Sgrehan isci_log_message(0, "ISCI", "Unhandled func_code 0x%x\n", 785250199Sgrehan ccb->ccb_h.func_code); 786250199Sgrehan ccb->ccb_h.status &= ~CAM_SIM_QUEUED; 787250199Sgrehan ccb->ccb_h.status &= ~CAM_STATUS_MASK; 788250199Sgrehan ccb->ccb_h.status |= CAM_REQ_INVALID; 789250199Sgrehan xpt_done(ccb); 790250199Sgrehan break; 791250199Sgrehan } 792250199Sgrehan} 793250199Sgrehan 794250199Sgrehan/* 795250199Sgrehan * Unfortunately, SCIL doesn't cleanly handle retry conditions. 796250199Sgrehan * CAM_REQUEUE_REQ works only when no one is using the pass(4) interface. So 797250199Sgrehan * when SCIL denotes an I/O needs to be retried (typically because of mixing 798250199Sgrehan * tagged/non-tagged ATA commands, or running out of NCQ slots), we queue 799250199Sgrehan * these I/O internally. Once SCIL completes an I/O to this device, or we get 800250199Sgrehan * a ready notification, we will retry the first I/O on the queue. 801250199Sgrehan * Unfortunately, SCIL also doesn't cleanly handle starting the new I/O within 802250199Sgrehan * the context of the completion handler, so we need to retry these I/O after 803250199Sgrehan * the completion handler is done executing. 804250199Sgrehan */ 805250199Sgrehanvoid 806250199Sgrehanisci_controller_release_queued_ccbs(struct ISCI_CONTROLLER *controller) 807250199Sgrehan{ 808250199Sgrehan struct ISCI_REMOTE_DEVICE *dev; 809250199Sgrehan struct ccb_hdr *ccb_h; 810250199Sgrehan uint8_t *ptr; 811250199Sgrehan int dev_idx; 812250199Sgrehan 813250199Sgrehan KASSERT(mtx_owned(&controller->lock), ("controller lock not owned")); 814250199Sgrehan 815250199Sgrehan controller->release_queued_ccbs = FALSE; 816250199Sgrehan for (dev_idx = 0; 817250199Sgrehan dev_idx < SCI_MAX_REMOTE_DEVICES; 818250199Sgrehan dev_idx++) { 819250199Sgrehan 820250199Sgrehan dev = controller->remote_device[dev_idx]; 821250199Sgrehan if (dev != NULL && 822250199Sgrehan dev->release_queued_ccb == TRUE && 823250199Sgrehan dev->queued_ccb_in_progress == NULL) { 824250199Sgrehan dev->release_queued_ccb = FALSE; 825250199Sgrehan ccb_h = TAILQ_FIRST(&dev->queued_ccbs); 826250199Sgrehan 827250199Sgrehan if (ccb_h == NULL) 828250199Sgrehan continue; 829250199Sgrehan 830250199Sgrehan ptr = scsiio_cdb_ptr(&((union ccb *)ccb_h)->csio); 831250199Sgrehan isci_log_message(1, "ISCI", "release %p %x\n", ccb_h, *ptr); 832250199Sgrehan 833250199Sgrehan dev->queued_ccb_in_progress = (union ccb *)ccb_h; 834250199Sgrehan isci_io_request_execute_scsi_io( 835250199Sgrehan (union ccb *)ccb_h, controller); 836250199Sgrehan } 837250199Sgrehan } 838250199Sgrehan} 839250199Sgrehan