1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <dev/isci/isci.h> 35 36 #include <sys/sysctl.h> 37 #include <sys/malloc.h> 38 39 #include <cam/cam_periph.h> 40 41 #include <dev/pci/pcireg.h> 42 #include <dev/pci/pcivar.h> 43 44 #include <dev/isci/scil/scic_logger.h> 45 #include <dev/isci/scil/scic_library.h> 46 #include <dev/isci/scil/scic_user_callback.h> 47 48 #include <dev/isci/scil/scif_controller.h> 49 #include <dev/isci/scil/scif_library.h> 50 #include <dev/isci/scil/scif_logger.h> 51 #include <dev/isci/scil/scif_user_callback.h> 52 53 MALLOC_DEFINE(M_ISCI, "isci", "isci driver memory allocations"); 54 55 struct isci_softc *g_isci; 56 uint32_t g_isci_debug_level = 0; 57 58 static int isci_probe(device_t); 59 static int isci_attach(device_t); 60 static int isci_detach(device_t); 61 62 int isci_initialize(struct isci_softc *isci); 63 64 void isci_allocate_dma_buffer_callback(void *arg, bus_dma_segment_t *seg, 65 int nseg, int error); 66 67 static devclass_t isci_devclass; 68 69 static device_method_t isci_pci_methods[] = { 70 /* Device interface */ 71 DEVMETHOD(device_probe, isci_probe), 72 DEVMETHOD(device_attach, isci_attach), 73 DEVMETHOD(device_detach, isci_detach), 74 { 0, 0 } 75 }; 76 77 static driver_t isci_pci_driver = { 78 "isci", 79 isci_pci_methods, 80 sizeof(struct isci_softc), 81 }; 82 83 DRIVER_MODULE(isci, pci, isci_pci_driver, isci_devclass, 0, 0); 84 85 static struct _pcsid 86 { 87 u_int32_t type; 88 const char *desc; 89 } pci_ids[] = { 90 { 0x1d608086, "Intel(R) C600 Series Chipset SAS Controller" }, 91 { 0x1d618086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" }, 92 { 0x1d628086, "Intel(R) C600 Series Chipset SAS Controller" }, 93 { 0x1d638086, "Intel(R) C600 Series Chipset SAS Controller" }, 94 { 0x1d648086, "Intel(R) C600 Series Chipset SAS Controller" }, 95 { 0x1d658086, "Intel(R) C600 Series Chipset SAS Controller" }, 96 { 0x1d668086, "Intel(R) C600 Series Chipset SAS Controller" }, 97 { 0x1d678086, "Intel(R) C600 Series Chipset SAS Controller" }, 98 { 0x1d688086, "Intel(R) C600 Series Chipset SAS Controller" }, 99 { 0x1d698086, "Intel(R) C600 Series Chipset SAS Controller" }, 100 { 0x1d6a8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" }, 101 { 0x1d6b8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" }, 102 { 0x00000000, NULL } 103 }; 104 105 static int 106 isci_probe (device_t device) 107 { 108 u_int32_t type = pci_get_devid(device); 109 struct _pcsid *ep = pci_ids; 110 111 while (ep->type && ep->type != type) 112 ++ep; 113 114 if (ep->desc) 115 { 116 device_set_desc(device, ep->desc); 117 return (0); 118 } 119 else 120 return (ENXIO); 121 } 122 123 static int 124 isci_allocate_pci_memory(struct isci_softc *isci) 125 { 126 int i; 127 128 for (i = 0; i < ISCI_NUM_PCI_BARS; i++) 129 { 130 struct ISCI_PCI_BAR *pci_bar = &isci->pci_bar[i]; 131 132 pci_bar->resource_id = PCIR_BAR(i*2); 133 pci_bar->resource = bus_alloc_resource(isci->device, 134 SYS_RES_MEMORY, &pci_bar->resource_id, 0, ~0, 1, 135 RF_ACTIVE); 136 137 if(pci_bar->resource == NULL) 138 isci_log_message(0, "ISCI", 139 "unable to allocate pci resource\n"); 140 else { 141 pci_bar->bus_tag = rman_get_bustag(pci_bar->resource); 142 pci_bar->bus_handle = 143 rman_get_bushandle(pci_bar->resource); 144 } 145 } 146 147 return (0); 148 } 149 150 static int 151 isci_attach(device_t device) 152 { 153 int error; 154 struct isci_softc *isci = DEVICE2SOFTC(device); 155 156 g_isci = isci; 157 isci->device = device; 158 159 isci_allocate_pci_memory(isci); 160 161 error = isci_initialize(isci); 162 163 if (error) 164 { 165 isci_detach(device); 166 return (error); 167 } 168 169 isci_interrupt_setup(isci); 170 isci_sysctl_initialize(isci); 171 172 return (0); 173 } 174 175 static int 176 isci_detach(device_t device) 177 { 178 struct isci_softc *isci = DEVICE2SOFTC(device); 179 int i; 180 181 for (i = 0; i < isci->controller_count; i++) { 182 struct ISCI_CONTROLLER *controller = &isci->controllers[i]; 183 SCI_STATUS status; 184 185 if (controller->scif_controller_handle != NULL) { 186 scic_controller_disable_interrupts( 187 scif_controller_get_scic_handle(controller->scif_controller_handle)); 188 189 mtx_lock(&controller->lock); 190 status = scif_controller_stop(controller->scif_controller_handle, 0); 191 mtx_unlock(&controller->lock); 192 193 while (controller->is_started == TRUE) { 194 /* Now poll for interrupts until the controller stop complete 195 * callback is received. 196 */ 197 mtx_lock(&controller->lock); 198 isci_interrupt_poll_handler(controller); 199 mtx_unlock(&controller->lock); 200 pause("isci", 1); 201 } 202 203 if(controller->sim != NULL) { 204 mtx_lock(&controller->lock); 205 xpt_free_path(controller->path); 206 xpt_bus_deregister(cam_sim_path(controller->sim)); 207 cam_sim_free(controller->sim, TRUE); 208 mtx_unlock(&controller->lock); 209 } 210 } 211 212 if (controller->timer_memory != NULL) 213 free(controller->timer_memory, M_ISCI); 214 215 if (controller->remote_device_memory != NULL) 216 free(controller->remote_device_memory, M_ISCI); 217 } 218 219 /* The SCIF controllers have been stopped, so we can now 220 * free the SCI library memory. 221 */ 222 if (isci->sci_library_memory != NULL) 223 free(isci->sci_library_memory, M_ISCI); 224 225 for (i = 0; i < ISCI_NUM_PCI_BARS; i++) 226 { 227 struct ISCI_PCI_BAR *pci_bar = &isci->pci_bar[i]; 228 229 if (pci_bar->resource != NULL) 230 bus_release_resource(device, SYS_RES_MEMORY, 231 pci_bar->resource_id, pci_bar->resource); 232 } 233 234 for (i = 0; i < isci->num_interrupts; i++) 235 { 236 struct ISCI_INTERRUPT_INFO *interrupt_info; 237 238 interrupt_info = &isci->interrupt_info[i]; 239 240 if(interrupt_info->tag != NULL) 241 bus_teardown_intr(device, interrupt_info->res, 242 interrupt_info->tag); 243 244 if(interrupt_info->res != NULL) 245 bus_release_resource(device, SYS_RES_IRQ, 246 rman_get_rid(interrupt_info->res), 247 interrupt_info->res); 248 249 pci_release_msi(device); 250 } 251 252 return (0); 253 } 254 255 int 256 isci_initialize(struct isci_softc *isci) 257 { 258 int error; 259 uint32_t status = 0; 260 uint32_t library_object_size; 261 uint32_t verbosity_mask; 262 uint32_t scic_log_object_mask; 263 uint32_t scif_log_object_mask; 264 uint8_t *header_buffer; 265 266 library_object_size = scif_library_get_object_size(SCI_MAX_CONTROLLERS); 267 268 isci->sci_library_memory = 269 malloc(library_object_size, M_ISCI, M_NOWAIT | M_ZERO ); 270 271 isci->sci_library_handle = scif_library_construct( 272 isci->sci_library_memory, SCI_MAX_CONTROLLERS); 273 274 sci_object_set_association( isci->sci_library_handle, (void *)isci); 275 276 verbosity_mask = (1<<SCI_LOG_VERBOSITY_ERROR) | 277 (1<<SCI_LOG_VERBOSITY_WARNING) | (1<<SCI_LOG_VERBOSITY_INFO) | 278 (1<<SCI_LOG_VERBOSITY_TRACE); 279 280 scic_log_object_mask = 0xFFFFFFFF; 281 scic_log_object_mask &= ~SCIC_LOG_OBJECT_COMPLETION_QUEUE; 282 scic_log_object_mask &= ~SCIC_LOG_OBJECT_SSP_IO_REQUEST; 283 scic_log_object_mask &= ~SCIC_LOG_OBJECT_STP_IO_REQUEST; 284 scic_log_object_mask &= ~SCIC_LOG_OBJECT_SMP_IO_REQUEST; 285 scic_log_object_mask &= ~SCIC_LOG_OBJECT_CONTROLLER; 286 287 scif_log_object_mask = 0xFFFFFFFF; 288 scif_log_object_mask &= ~SCIF_LOG_OBJECT_CONTROLLER; 289 scif_log_object_mask &= ~SCIF_LOG_OBJECT_IO_REQUEST; 290 291 TUNABLE_INT_FETCH("hw.isci.debug_level", &g_isci_debug_level); 292 293 sci_logger_enable(sci_object_get_logger(isci->sci_library_handle), 294 scif_log_object_mask, verbosity_mask); 295 296 sci_logger_enable(sci_object_get_logger( 297 scif_library_get_scic_handle(isci->sci_library_handle)), 298 scic_log_object_mask, verbosity_mask); 299 300 header_buffer = (uint8_t *)&isci->pci_common_header; 301 for (uint8_t i = 0; i < sizeof(isci->pci_common_header); i++) 302 header_buffer[i] = pci_read_config(isci->device, i, 1); 303 304 scic_library_set_pci_info( 305 scif_library_get_scic_handle(isci->sci_library_handle), 306 &isci->pci_common_header); 307 308 isci->oem_parameters_found = FALSE; 309 310 isci_get_oem_parameters(isci); 311 312 /* trigger interrupt if 32 completions occur before timeout expires */ 313 isci->coalesce_number = 32; 314 315 /* trigger interrupt if 2 microseconds elapse after a completion occurs, 316 * regardless if "coalesce_number" completions have occurred 317 */ 318 isci->coalesce_timeout = 2; 319 320 isci->controller_count = scic_library_get_pci_device_controller_count( 321 scif_library_get_scic_handle(isci->sci_library_handle)); 322 323 for (int index = 0; index < isci->controller_count; index++) { 324 struct ISCI_CONTROLLER *controller = &isci->controllers[index]; 325 SCI_CONTROLLER_HANDLE_T scif_controller_handle; 326 327 controller->index = index; 328 isci_controller_construct(controller, isci); 329 330 scif_controller_handle = controller->scif_controller_handle; 331 332 status = isci_controller_initialize(controller); 333 334 if(status != SCI_SUCCESS) { 335 isci_log_message(0, "ISCI", 336 "isci_controller_initialize FAILED: %x\n", 337 status); 338 return (status); 339 } 340 341 error = isci_controller_allocate_memory(controller); 342 343 if (error != 0) 344 return (error); 345 346 scif_controller_set_interrupt_coalescence( 347 scif_controller_handle, isci->coalesce_number, 348 isci->coalesce_timeout); 349 } 350 351 /* FreeBSD provides us a hook to ensure we get a chance to start 352 * our controllers and complete initial domain discovery before 353 * it searches for the boot device. Once we're done, we'll 354 * disestablish the hook, signaling the kernel that is can proceed 355 * with the boot process. 356 */ 357 isci->config_hook.ich_func = &isci_controller_start; 358 isci->config_hook.ich_arg = &isci->controllers[0]; 359 360 if (config_intrhook_establish(&isci->config_hook) != 0) 361 isci_log_message(0, "ISCI", 362 "config_intrhook_establish failed!\n"); 363 364 return (status); 365 } 366 367 void 368 isci_allocate_dma_buffer_callback(void *arg, bus_dma_segment_t *seg, 369 int nseg, int error) 370 { 371 struct ISCI_MEMORY *memory = (struct ISCI_MEMORY *)arg; 372 373 memory->error = error; 374 375 if (nseg != 1 || error != 0) 376 isci_log_message(0, "ISCI", 377 "Failed to allocate physically contiguous memory!\n"); 378 else 379 memory->physical_address = seg->ds_addr; 380 } 381 382 int 383 isci_allocate_dma_buffer(device_t device, struct ISCI_MEMORY *memory) 384 { 385 uint32_t status; 386 387 status = bus_dma_tag_create(bus_get_dma_tag(device), 388 0x40 /* cacheline alignment */, 0x0, BUS_SPACE_MAXADDR, 389 BUS_SPACE_MAXADDR, NULL, NULL, memory->size, 390 0x1 /* we want physically contiguous */, 391 memory->size, 0, NULL, NULL, &memory->dma_tag); 392 393 if(status == ENOMEM) { 394 isci_log_message(0, "ISCI", "bus_dma_tag_create failed\n"); 395 return (status); 396 } 397 398 status = bus_dmamem_alloc(memory->dma_tag, 399 (void **)&memory->virtual_address, BUS_DMA_ZERO, &memory->dma_map); 400 401 if(status == ENOMEM) 402 { 403 isci_log_message(0, "ISCI", "bus_dmamem_alloc failed\n"); 404 return (status); 405 } 406 407 status = bus_dmamap_load(memory->dma_tag, memory->dma_map, 408 (void *)memory->virtual_address, memory->size, 409 isci_allocate_dma_buffer_callback, memory, 0); 410 411 if(status == EINVAL) 412 { 413 isci_log_message(0, "ISCI", "bus_dmamap_load failed\n"); 414 return (status); 415 } 416 417 return (0); 418 } 419 420 /** 421 * @brief This callback method asks the user to associate the supplied 422 * lock with an operating environment specific locking construct. 423 * 424 * @param[in] controller This parameter specifies the controller with 425 * which this lock is to be associated. 426 * @param[in] lock This parameter specifies the lock for which the 427 * user should associate an operating environment specific 428 * locking object. 429 * 430 * @see The SCI_LOCK_LEVEL enumeration for more information. 431 * 432 * @return none. 433 */ 434 void 435 scif_cb_lock_associate(SCI_CONTROLLER_HANDLE_T controller, 436 SCI_LOCK_HANDLE_T lock) 437 { 438 439 } 440 441 /** 442 * @brief This callback method asks the user to de-associate the supplied 443 * lock with an operating environment specific locking construct. 444 * 445 * @param[in] controller This parameter specifies the controller with 446 * which this lock is to be de-associated. 447 * @param[in] lock This parameter specifies the lock for which the 448 * user should de-associate an operating environment specific 449 * locking object. 450 * 451 * @see The SCI_LOCK_LEVEL enumeration for more information. 452 * 453 * @return none. 454 */ 455 void 456 scif_cb_lock_disassociate(SCI_CONTROLLER_HANDLE_T controller, 457 SCI_LOCK_HANDLE_T lock) 458 { 459 460 } 461 462 463 /** 464 * @brief This callback method asks the user to acquire/get the lock. 465 * This method should pend until the lock has been acquired. 466 * 467 * @param[in] controller This parameter specifies the controller with 468 * which this lock is associated. 469 * @param[in] lock This parameter specifies the lock to be acquired. 470 * 471 * @return none 472 */ 473 void 474 scif_cb_lock_acquire(SCI_CONTROLLER_HANDLE_T controller, 475 SCI_LOCK_HANDLE_T lock) 476 { 477 478 } 479 480 /** 481 * @brief This callback method asks the user to release a lock. 482 * 483 * @param[in] controller This parameter specifies the controller with 484 * which this lock is associated. 485 * @param[in] lock This parameter specifies the lock to be released. 486 * 487 * @return none 488 */ 489 void 490 scif_cb_lock_release(SCI_CONTROLLER_HANDLE_T controller, 491 SCI_LOCK_HANDLE_T lock) 492 { 493 } 494 495 /** 496 * @brief This callback method creates an OS specific deferred task 497 * for internal usage. The handler to deferred task is stored by OS 498 * driver. 499 * 500 * @param[in] controller This parameter specifies the controller object 501 * with which this callback is associated. 502 * 503 * @return none 504 */ 505 void 506 scif_cb_start_internal_io_task_create(SCI_CONTROLLER_HANDLE_T controller) 507 { 508 509 } 510 511 /** 512 * @brief This callback method schedules a OS specific deferred task. 513 * 514 * @param[in] controller This parameter specifies the controller 515 * object with which this callback is associated. 516 * @param[in] start_internal_io_task_routine This parameter specifies the 517 * sci start_internal_io routine. 518 * @param[in] context This parameter specifies a handle to a parameter 519 * that will be passed into the "start_internal_io_task_routine" 520 * when it is invoked. 521 * 522 * @return none 523 */ 524 void 525 scif_cb_start_internal_io_task_schedule(SCI_CONTROLLER_HANDLE_T scif_controller, 526 FUNCPTR start_internal_io_task_routine, void *context) 527 { 528 /** @todo Use FreeBSD tasklet to defer this routine to a later time, 529 * rather than calling the routine inline. 530 */ 531 SCI_START_INTERNAL_IO_ROUTINE sci_start_internal_io_routine = 532 (SCI_START_INTERNAL_IO_ROUTINE)start_internal_io_task_routine; 533 534 sci_start_internal_io_routine(context); 535 } 536 537 /** 538 * @brief In this method the user must write to PCI memory via access. 539 * This method is used for access to memory space and IO space. 540 * 541 * @param[in] controller The controller for which to read a DWORD. 542 * @param[in] address This parameter depicts the address into 543 * which to write. 544 * @param[out] write_value This parameter depicts the value being written 545 * into the PCI memory location. 546 * 547 * @todo These PCI memory access calls likely needs to be optimized into macros? 548 */ 549 void 550 scic_cb_pci_write_dword(SCI_CONTROLLER_HANDLE_T scic_controller, 551 void *address, uint32_t write_value) 552 { 553 SCI_CONTROLLER_HANDLE_T scif_controller = 554 (SCI_CONTROLLER_HANDLE_T) sci_object_get_association(scic_controller); 555 struct ISCI_CONTROLLER *isci_controller = 556 (struct ISCI_CONTROLLER *) sci_object_get_association(scif_controller); 557 struct isci_softc *isci = isci_controller->isci; 558 uint32_t bar = (uint32_t)(((POINTER_UINT)address & 0xF0000000) >> 28); 559 bus_size_t offset = (bus_size_t)((POINTER_UINT)address & 0x0FFFFFFF); 560 561 bus_space_write_4(isci->pci_bar[bar].bus_tag, 562 isci->pci_bar[bar].bus_handle, offset, write_value); 563 } 564 565 /** 566 * @brief In this method the user must read from PCI memory via access. 567 * This method is used for access to memory space and IO space. 568 * 569 * @param[in] controller The controller for which to read a DWORD. 570 * @param[in] address This parameter depicts the address from 571 * which to read. 572 * 573 * @return The value being returned from the PCI memory location. 574 * 575 * @todo This PCI memory access calls likely need to be optimized into macro? 576 */ 577 uint32_t 578 scic_cb_pci_read_dword(SCI_CONTROLLER_HANDLE_T scic_controller, void *address) 579 { 580 SCI_CONTROLLER_HANDLE_T scif_controller = 581 (SCI_CONTROLLER_HANDLE_T)sci_object_get_association(scic_controller); 582 struct ISCI_CONTROLLER *isci_controller = 583 (struct ISCI_CONTROLLER *)sci_object_get_association(scif_controller); 584 struct isci_softc *isci = isci_controller->isci; 585 uint32_t bar = (uint32_t)(((POINTER_UINT)address & 0xF0000000) >> 28); 586 bus_size_t offset = (bus_size_t)((POINTER_UINT)address & 0x0FFFFFFF); 587 588 return (bus_space_read_4(isci->pci_bar[bar].bus_tag, 589 isci->pci_bar[bar].bus_handle, offset)); 590 } 591 592 /** 593 * @brief This method is called when the core requires the OS driver 594 * to stall execution. This method is utilized during initialization 595 * or non-performance paths only. 596 * 597 * @param[in] microseconds This parameter specifies the number of 598 * microseconds for which to stall. The operating system driver 599 * is allowed to round this value up where necessary. 600 * 601 * @return none. 602 */ 603 void 604 scic_cb_stall_execution(uint32_t microseconds) 605 { 606 607 DELAY(microseconds); 608 } 609 610 /** 611 * @brief In this method the user must return the base address register (BAR) 612 * value for the supplied base address register number. 613 * 614 * @param[in] controller The controller for which to retrieve the bar number. 615 * @param[in] bar_number This parameter depicts the BAR index/number to be read. 616 * 617 * @return Return a pointer value indicating the contents of the BAR. 618 * @retval NULL indicates an invalid BAR index/number was specified. 619 * @retval All other values indicate a valid VIRTUAL address from the BAR. 620 */ 621 void * 622 scic_cb_pci_get_bar(SCI_CONTROLLER_HANDLE_T controller, 623 uint16_t bar_number) 624 { 625 626 return ((void *)(POINTER_UINT)((uint32_t)bar_number << 28)); 627 } 628 629 /** 630 * @brief This method informs the SCI Core user that a phy/link became 631 * ready, but the phy is not allowed in the port. In some 632 * situations the underlying hardware only allows for certain phy 633 * to port mappings. If these mappings are violated, then this 634 * API is invoked. 635 * 636 * @param[in] controller This parameter represents the controller which 637 * contains the port. 638 * @param[in] port This parameter specifies the SCI port object for which 639 * the callback is being invoked. 640 * @param[in] phy This parameter specifies the phy that came ready, but the 641 * phy can't be a valid member of the port. 642 * 643 * @return none 644 */ 645 void 646 scic_cb_port_invalid_link_up(SCI_CONTROLLER_HANDLE_T controller, 647 SCI_PORT_HANDLE_T port, SCI_PHY_HANDLE_T phy) 648 { 649 650 } 651