4 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <dev/isci/isci.h>
36 #include <sys/sysctl.h>
37 #include <sys/malloc.h>
39 #include <cam/cam_periph.h>
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
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>
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>
53 MALLOC_DEFINE(M_ISCI, "isci", "isci driver memory allocations");
55 struct isci_softc *g_isci;
56 uint32_t g_isci_debug_level = 0;
58 static int isci_probe(device_t);
59 static int isci_attach(device_t);
60 static int isci_detach(device_t);
62 int isci_initialize(struct isci_softc *isci);
64 void isci_allocate_dma_buffer_callback(void *arg, bus_dma_segment_t *seg,
67 static devclass_t isci_devclass;
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),
77 static driver_t isci_pci_driver = {
80 sizeof(struct isci_softc),
83 DRIVER_MODULE(isci, pci, isci_pci_driver, isci_devclass, 0, 0);
84 MODULE_DEPEND(isci, cam, 1, 1, 1);
91 { 0x1d608086, "Intel(R) C600 Series Chipset SAS Controller" },
92 { 0x1d618086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" },
93 { 0x1d628086, "Intel(R) C600 Series Chipset SAS Controller" },
94 { 0x1d638086, "Intel(R) C600 Series Chipset SAS Controller" },
95 { 0x1d648086, "Intel(R) C600 Series Chipset SAS Controller" },
96 { 0x1d658086, "Intel(R) C600 Series Chipset SAS Controller" },
97 { 0x1d668086, "Intel(R) C600 Series Chipset SAS Controller" },
98 { 0x1d678086, "Intel(R) C600 Series Chipset SAS Controller" },
99 { 0x1d688086, "Intel(R) C600 Series Chipset SAS Controller" },
100 { 0x1d698086, "Intel(R) C600 Series Chipset SAS Controller" },
101 { 0x1d6a8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" },
102 { 0x1d6b8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" },
103 { 0x1d6c8086, "Intel(R) C600 Series Chipset SAS Controller" },
104 { 0x1d6d8086, "Intel(R) C600 Series Chipset SAS Controller" },
105 { 0x1d6e8086, "Intel(R) C600 Series Chipset SAS Controller" },
106 { 0x1d6f8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" },
111 isci_probe (device_t device)
113 u_int32_t type = pci_get_devid(device);
114 struct _pcsid *ep = pci_ids;
116 while (ep->type && ep->type != type)
121 device_set_desc(device, ep->desc);
122 return (BUS_PROBE_DEFAULT);
129 isci_allocate_pci_memory(struct isci_softc *isci)
133 for (i = 0; i < ISCI_NUM_PCI_BARS; i++)
135 struct ISCI_PCI_BAR *pci_bar = &isci->pci_bar[i];
137 pci_bar->resource_id = PCIR_BAR(i*2);
138 pci_bar->resource = bus_alloc_resource(isci->device,
139 SYS_RES_MEMORY, &pci_bar->resource_id, 0, ~0, 1,
142 if(pci_bar->resource == NULL)
143 isci_log_message(0, "ISCI",
144 "unable to allocate pci resource\n");
146 pci_bar->bus_tag = rman_get_bustag(pci_bar->resource);
147 pci_bar->bus_handle =
148 rman_get_bushandle(pci_bar->resource);
156 isci_attach(device_t device)
159 struct isci_softc *isci = DEVICE2SOFTC(device);
162 isci->device = device;
164 isci_allocate_pci_memory(isci);
166 error = isci_initialize(isci);
174 isci_interrupt_setup(isci);
175 isci_sysctl_initialize(isci);
181 isci_detach(device_t device)
183 struct isci_softc *isci = DEVICE2SOFTC(device);
186 for (i = 0; i < isci->controller_count; i++) {
187 struct ISCI_CONTROLLER *controller = &isci->controllers[i];
191 if (controller->scif_controller_handle != NULL) {
192 scic_controller_disable_interrupts(
193 scif_controller_get_scic_handle(controller->scif_controller_handle));
195 mtx_lock(&controller->lock);
196 status = scif_controller_stop(controller->scif_controller_handle, 0);
197 mtx_unlock(&controller->lock);
199 while (controller->is_started == TRUE) {
200 /* Now poll for interrupts until the controller stop complete
201 * callback is received.
203 mtx_lock(&controller->lock);
204 isci_interrupt_poll_handler(controller);
205 mtx_unlock(&controller->lock);
209 if(controller->sim != NULL) {
210 mtx_lock(&controller->lock);
211 xpt_free_path(controller->path);
212 xpt_bus_deregister(cam_sim_path(controller->sim));
213 cam_sim_free(controller->sim, TRUE);
214 mtx_unlock(&controller->lock);
218 if (controller->timer_memory != NULL)
219 free(controller->timer_memory, M_ISCI);
221 if (controller->remote_device_memory != NULL)
222 free(controller->remote_device_memory, M_ISCI);
225 sci_pool_get(controller->unmap_buffer_pool, unmap_buffer);
226 if (unmap_buffer == NULL)
228 contigfree(unmap_buffer, PAGE_SIZE, M_ISCI);
232 /* The SCIF controllers have been stopped, so we can now
233 * free the SCI library memory.
235 if (isci->sci_library_memory != NULL)
236 free(isci->sci_library_memory, M_ISCI);
238 for (i = 0; i < ISCI_NUM_PCI_BARS; i++)
240 struct ISCI_PCI_BAR *pci_bar = &isci->pci_bar[i];
242 if (pci_bar->resource != NULL)
243 bus_release_resource(device, SYS_RES_MEMORY,
244 pci_bar->resource_id, pci_bar->resource);
247 for (i = 0; i < isci->num_interrupts; i++)
249 struct ISCI_INTERRUPT_INFO *interrupt_info;
251 interrupt_info = &isci->interrupt_info[i];
253 if(interrupt_info->tag != NULL)
254 bus_teardown_intr(device, interrupt_info->res,
255 interrupt_info->tag);
257 if(interrupt_info->res != NULL)
258 bus_release_resource(device, SYS_RES_IRQ,
259 rman_get_rid(interrupt_info->res),
260 interrupt_info->res);
262 pci_release_msi(device);
269 isci_initialize(struct isci_softc *isci)
273 uint32_t library_object_size;
274 uint32_t verbosity_mask;
275 uint32_t scic_log_object_mask;
276 uint32_t scif_log_object_mask;
277 uint8_t *header_buffer;
279 library_object_size = scif_library_get_object_size(SCI_MAX_CONTROLLERS);
281 isci->sci_library_memory =
282 malloc(library_object_size, M_ISCI, M_NOWAIT | M_ZERO );
284 isci->sci_library_handle = scif_library_construct(
285 isci->sci_library_memory, SCI_MAX_CONTROLLERS);
287 sci_object_set_association( isci->sci_library_handle, (void *)isci);
289 verbosity_mask = (1<<SCI_LOG_VERBOSITY_ERROR) |
290 (1<<SCI_LOG_VERBOSITY_WARNING) | (1<<SCI_LOG_VERBOSITY_INFO) |
291 (1<<SCI_LOG_VERBOSITY_TRACE);
293 scic_log_object_mask = 0xFFFFFFFF;
294 scic_log_object_mask &= ~SCIC_LOG_OBJECT_COMPLETION_QUEUE;
295 scic_log_object_mask &= ~SCIC_LOG_OBJECT_SSP_IO_REQUEST;
296 scic_log_object_mask &= ~SCIC_LOG_OBJECT_STP_IO_REQUEST;
297 scic_log_object_mask &= ~SCIC_LOG_OBJECT_SMP_IO_REQUEST;
298 scic_log_object_mask &= ~SCIC_LOG_OBJECT_CONTROLLER;
300 scif_log_object_mask = 0xFFFFFFFF;
301 scif_log_object_mask &= ~SCIF_LOG_OBJECT_CONTROLLER;
302 scif_log_object_mask &= ~SCIF_LOG_OBJECT_IO_REQUEST;
304 TUNABLE_INT_FETCH("hw.isci.debug_level", &g_isci_debug_level);
306 sci_logger_enable(sci_object_get_logger(isci->sci_library_handle),
307 scif_log_object_mask, verbosity_mask);
309 sci_logger_enable(sci_object_get_logger(
310 scif_library_get_scic_handle(isci->sci_library_handle)),
311 scic_log_object_mask, verbosity_mask);
313 header_buffer = (uint8_t *)&isci->pci_common_header;
314 for (uint8_t i = 0; i < sizeof(isci->pci_common_header); i++)
315 header_buffer[i] = pci_read_config(isci->device, i, 1);
317 scic_library_set_pci_info(
318 scif_library_get_scic_handle(isci->sci_library_handle),
319 &isci->pci_common_header);
321 isci->oem_parameters_found = FALSE;
323 isci_get_oem_parameters(isci);
325 /* trigger interrupt if 32 completions occur before timeout expires */
326 isci->coalesce_number = 32;
328 /* trigger interrupt if 2 microseconds elapse after a completion occurs,
329 * regardless if "coalesce_number" completions have occurred
331 isci->coalesce_timeout = 2;
333 isci->controller_count = scic_library_get_pci_device_controller_count(
334 scif_library_get_scic_handle(isci->sci_library_handle));
336 for (int index = 0; index < isci->controller_count; index++) {
337 struct ISCI_CONTROLLER *controller = &isci->controllers[index];
338 SCI_CONTROLLER_HANDLE_T scif_controller_handle;
340 controller->index = index;
341 isci_controller_construct(controller, isci);
343 scif_controller_handle = controller->scif_controller_handle;
345 status = isci_controller_initialize(controller);
347 if(status != SCI_SUCCESS) {
348 isci_log_message(0, "ISCI",
349 "isci_controller_initialize FAILED: %x\n",
354 error = isci_controller_allocate_memory(controller);
359 scif_controller_set_interrupt_coalescence(
360 scif_controller_handle, isci->coalesce_number,
361 isci->coalesce_timeout);
364 /* FreeBSD provides us a hook to ensure we get a chance to start
365 * our controllers and complete initial domain discovery before
366 * it searches for the boot device. Once we're done, we'll
367 * disestablish the hook, signaling the kernel that is can proceed
368 * with the boot process.
370 isci->config_hook.ich_func = &isci_controller_start;
371 isci->config_hook.ich_arg = &isci->controllers[0];
373 if (config_intrhook_establish(&isci->config_hook) != 0)
374 isci_log_message(0, "ISCI",
375 "config_intrhook_establish failed!\n");
381 isci_allocate_dma_buffer_callback(void *arg, bus_dma_segment_t *seg,
384 struct ISCI_MEMORY *memory = (struct ISCI_MEMORY *)arg;
386 memory->error = error;
388 if (nseg != 1 || error != 0)
389 isci_log_message(0, "ISCI",
390 "Failed to allocate physically contiguous memory!\n");
392 memory->physical_address = seg->ds_addr;
396 isci_allocate_dma_buffer(device_t device, struct ISCI_MEMORY *memory)
400 status = bus_dma_tag_create(bus_get_dma_tag(device),
401 0x40 /* cacheline alignment */, 0x0, BUS_SPACE_MAXADDR,
402 BUS_SPACE_MAXADDR, NULL, NULL, memory->size,
403 0x1 /* we want physically contiguous */,
404 memory->size, 0, NULL, NULL, &memory->dma_tag);
406 if(status == ENOMEM) {
407 isci_log_message(0, "ISCI", "bus_dma_tag_create failed\n");
411 status = bus_dmamem_alloc(memory->dma_tag,
412 (void **)&memory->virtual_address, BUS_DMA_ZERO, &memory->dma_map);
416 isci_log_message(0, "ISCI", "bus_dmamem_alloc failed\n");
420 status = bus_dmamap_load(memory->dma_tag, memory->dma_map,
421 (void *)memory->virtual_address, memory->size,
422 isci_allocate_dma_buffer_callback, memory, 0);
426 isci_log_message(0, "ISCI", "bus_dmamap_load failed\n");
434 * @brief This callback method asks the user to associate the supplied
435 * lock with an operating environment specific locking construct.
437 * @param[in] controller This parameter specifies the controller with
438 * which this lock is to be associated.
439 * @param[in] lock This parameter specifies the lock for which the
440 * user should associate an operating environment specific
443 * @see The SCI_LOCK_LEVEL enumeration for more information.
448 scif_cb_lock_associate(SCI_CONTROLLER_HANDLE_T controller,
449 SCI_LOCK_HANDLE_T lock)
455 * @brief This callback method asks the user to de-associate the supplied
456 * lock with an operating environment specific locking construct.
458 * @param[in] controller This parameter specifies the controller with
459 * which this lock is to be de-associated.
460 * @param[in] lock This parameter specifies the lock for which the
461 * user should de-associate an operating environment specific
464 * @see The SCI_LOCK_LEVEL enumeration for more information.
469 scif_cb_lock_disassociate(SCI_CONTROLLER_HANDLE_T controller,
470 SCI_LOCK_HANDLE_T lock)
477 * @brief This callback method asks the user to acquire/get the lock.
478 * This method should pend until the lock has been acquired.
480 * @param[in] controller This parameter specifies the controller with
481 * which this lock is associated.
482 * @param[in] lock This parameter specifies the lock to be acquired.
487 scif_cb_lock_acquire(SCI_CONTROLLER_HANDLE_T controller,
488 SCI_LOCK_HANDLE_T lock)
494 * @brief This callback method asks the user to release a lock.
496 * @param[in] controller This parameter specifies the controller with
497 * which this lock is associated.
498 * @param[in] lock This parameter specifies the lock to be released.
503 scif_cb_lock_release(SCI_CONTROLLER_HANDLE_T controller,
504 SCI_LOCK_HANDLE_T lock)
509 * @brief This callback method creates an OS specific deferred task
510 * for internal usage. The handler to deferred task is stored by OS
513 * @param[in] controller This parameter specifies the controller object
514 * with which this callback is associated.
519 scif_cb_start_internal_io_task_create(SCI_CONTROLLER_HANDLE_T controller)
525 * @brief This callback method schedules a OS specific deferred task.
527 * @param[in] controller This parameter specifies the controller
528 * object with which this callback is associated.
529 * @param[in] start_internal_io_task_routine This parameter specifies the
530 * sci start_internal_io routine.
531 * @param[in] context This parameter specifies a handle to a parameter
532 * that will be passed into the "start_internal_io_task_routine"
533 * when it is invoked.
538 scif_cb_start_internal_io_task_schedule(SCI_CONTROLLER_HANDLE_T scif_controller,
539 FUNCPTR start_internal_io_task_routine, void *context)
541 /** @todo Use FreeBSD tasklet to defer this routine to a later time,
542 * rather than calling the routine inline.
544 SCI_START_INTERNAL_IO_ROUTINE sci_start_internal_io_routine =
545 (SCI_START_INTERNAL_IO_ROUTINE)start_internal_io_task_routine;
547 sci_start_internal_io_routine(context);
551 * @brief In this method the user must write to PCI memory via access.
552 * This method is used for access to memory space and IO space.
554 * @param[in] controller The controller for which to read a DWORD.
555 * @param[in] address This parameter depicts the address into
557 * @param[out] write_value This parameter depicts the value being written
558 * into the PCI memory location.
560 * @todo These PCI memory access calls likely needs to be optimized into macros?
563 scic_cb_pci_write_dword(SCI_CONTROLLER_HANDLE_T scic_controller,
564 void *address, uint32_t write_value)
566 SCI_CONTROLLER_HANDLE_T scif_controller =
567 (SCI_CONTROLLER_HANDLE_T) sci_object_get_association(scic_controller);
568 struct ISCI_CONTROLLER *isci_controller =
569 (struct ISCI_CONTROLLER *) sci_object_get_association(scif_controller);
570 struct isci_softc *isci = isci_controller->isci;
571 uint32_t bar = (uint32_t)(((POINTER_UINT)address & 0xF0000000) >> 28);
572 bus_size_t offset = (bus_size_t)((POINTER_UINT)address & 0x0FFFFFFF);
574 bus_space_write_4(isci->pci_bar[bar].bus_tag,
575 isci->pci_bar[bar].bus_handle, offset, write_value);
579 * @brief In this method the user must read from PCI memory via access.
580 * This method is used for access to memory space and IO space.
582 * @param[in] controller The controller for which to read a DWORD.
583 * @param[in] address This parameter depicts the address from
586 * @return The value being returned from the PCI memory location.
588 * @todo This PCI memory access calls likely need to be optimized into macro?
591 scic_cb_pci_read_dword(SCI_CONTROLLER_HANDLE_T scic_controller, void *address)
593 SCI_CONTROLLER_HANDLE_T scif_controller =
594 (SCI_CONTROLLER_HANDLE_T)sci_object_get_association(scic_controller);
595 struct ISCI_CONTROLLER *isci_controller =
596 (struct ISCI_CONTROLLER *)sci_object_get_association(scif_controller);
597 struct isci_softc *isci = isci_controller->isci;
598 uint32_t bar = (uint32_t)(((POINTER_UINT)address & 0xF0000000) >> 28);
599 bus_size_t offset = (bus_size_t)((POINTER_UINT)address & 0x0FFFFFFF);
601 return (bus_space_read_4(isci->pci_bar[bar].bus_tag,
602 isci->pci_bar[bar].bus_handle, offset));
606 * @brief This method is called when the core requires the OS driver
607 * to stall execution. This method is utilized during initialization
608 * or non-performance paths only.
610 * @param[in] microseconds This parameter specifies the number of
611 * microseconds for which to stall. The operating system driver
612 * is allowed to round this value up where necessary.
617 scic_cb_stall_execution(uint32_t microseconds)
624 * @brief In this method the user must return the base address register (BAR)
625 * value for the supplied base address register number.
627 * @param[in] controller The controller for which to retrieve the bar number.
628 * @param[in] bar_number This parameter depicts the BAR index/number to be read.
630 * @return Return a pointer value indicating the contents of the BAR.
631 * @retval NULL indicates an invalid BAR index/number was specified.
632 * @retval All other values indicate a valid VIRTUAL address from the BAR.
635 scic_cb_pci_get_bar(SCI_CONTROLLER_HANDLE_T controller,
639 return ((void *)(POINTER_UINT)((uint32_t)bar_number << 28));
643 * @brief This method informs the SCI Core user that a phy/link became
644 * ready, but the phy is not allowed in the port. In some
645 * situations the underlying hardware only allows for certain phy
646 * to port mappings. If these mappings are violated, then this
649 * @param[in] controller This parameter represents the controller which
651 * @param[in] port This parameter specifies the SCI port object for which
652 * the callback is being invoked.
653 * @param[in] phy This parameter specifies the phy that came ready, but the
654 * phy can't be a valid member of the port.
659 scic_cb_port_invalid_link_up(SCI_CONTROLLER_HANDLE_T controller,
660 SCI_PORT_HANDLE_T port, SCI_PHY_HANDLE_T phy)