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/led/led.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
46 #include <dev/isci/scil/scic_logger.h>
47 #include <dev/isci/scil/scic_library.h>
48 #include <dev/isci/scil/scic_sgpio.h>
49 #include <dev/isci/scil/scic_user_callback.h>
51 #include <dev/isci/scil/scif_controller.h>
52 #include <dev/isci/scil/scif_library.h>
53 #include <dev/isci/scil/scif_logger.h>
54 #include <dev/isci/scil/scif_user_callback.h>
56 MALLOC_DEFINE(M_ISCI, "isci", "isci driver memory allocations");
58 struct isci_softc *g_isci;
59 uint32_t g_isci_debug_level = 0;
61 static int isci_probe(device_t);
62 static int isci_attach(device_t);
63 static int isci_detach(device_t);
65 int isci_initialize(struct isci_softc *isci);
67 void isci_allocate_dma_buffer_callback(void *arg, bus_dma_segment_t *seg,
70 static devclass_t isci_devclass;
72 static device_method_t isci_pci_methods[] = {
73 /* Device interface */
74 DEVMETHOD(device_probe, isci_probe),
75 DEVMETHOD(device_attach, isci_attach),
76 DEVMETHOD(device_detach, isci_detach),
80 static driver_t isci_pci_driver = {
83 sizeof(struct isci_softc),
86 DRIVER_MODULE(isci, pci, isci_pci_driver, isci_devclass, 0, 0);
93 { 0x1d608086, "Intel(R) C600 Series Chipset SAS Controller" },
94 { 0x1d618086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" },
95 { 0x1d628086, "Intel(R) C600 Series Chipset SAS Controller" },
96 { 0x1d638086, "Intel(R) C600 Series Chipset SAS Controller" },
97 { 0x1d648086, "Intel(R) C600 Series Chipset SAS Controller" },
98 { 0x1d658086, "Intel(R) C600 Series Chipset SAS Controller" },
99 { 0x1d668086, "Intel(R) C600 Series Chipset SAS Controller" },
100 { 0x1d678086, "Intel(R) C600 Series Chipset SAS Controller" },
101 { 0x1d688086, "Intel(R) C600 Series Chipset SAS Controller" },
102 { 0x1d698086, "Intel(R) C600 Series Chipset SAS Controller" },
103 { 0x1d6a8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" },
104 { 0x1d6b8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" },
105 { 0x1d6c8086, "Intel(R) C600 Series Chipset SAS Controller" },
106 { 0x1d6d8086, "Intel(R) C600 Series Chipset SAS Controller" },
107 { 0x1d6e8086, "Intel(R) C600 Series Chipset SAS Controller" },
108 { 0x1d6f8086, "Intel(R) C600 Series Chipset SAS Controller (SATA mode)" },
113 isci_probe (device_t device)
115 u_int32_t type = pci_get_devid(device);
116 struct _pcsid *ep = pci_ids;
118 while (ep->type && ep->type != type)
123 device_set_desc(device, ep->desc);
124 return (BUS_PROBE_DEFAULT);
131 isci_allocate_pci_memory(struct isci_softc *isci)
135 for (i = 0; i < ISCI_NUM_PCI_BARS; i++)
137 struct ISCI_PCI_BAR *pci_bar = &isci->pci_bar[i];
139 pci_bar->resource_id = PCIR_BAR(i*2);
140 pci_bar->resource = bus_alloc_resource(isci->device,
141 SYS_RES_MEMORY, &pci_bar->resource_id, 0, ~0, 1,
144 if(pci_bar->resource == NULL)
145 isci_log_message(0, "ISCI",
146 "unable to allocate pci resource\n");
148 pci_bar->bus_tag = rman_get_bustag(pci_bar->resource);
149 pci_bar->bus_handle =
150 rman_get_bushandle(pci_bar->resource);
158 isci_attach(device_t device)
161 struct isci_softc *isci = DEVICE2SOFTC(device);
164 isci->device = device;
166 isci_allocate_pci_memory(isci);
168 error = isci_initialize(isci);
176 isci_interrupt_setup(isci);
177 isci_sysctl_initialize(isci);
183 isci_detach(device_t device)
185 struct isci_softc *isci = DEVICE2SOFTC(device);
188 for (i = 0; i < isci->controller_count; i++) {
189 struct ISCI_CONTROLLER *controller = &isci->controllers[i];
193 if (controller->scif_controller_handle != NULL) {
194 scic_controller_disable_interrupts(
195 scif_controller_get_scic_handle(controller->scif_controller_handle));
197 mtx_lock(&controller->lock);
198 status = scif_controller_stop(controller->scif_controller_handle, 0);
199 mtx_unlock(&controller->lock);
201 while (controller->is_started == TRUE) {
202 /* Now poll for interrupts until the controller stop complete
203 * callback is received.
205 mtx_lock(&controller->lock);
206 isci_interrupt_poll_handler(controller);
207 mtx_unlock(&controller->lock);
211 if(controller->sim != NULL) {
212 mtx_lock(&controller->lock);
213 xpt_free_path(controller->path);
214 xpt_bus_deregister(cam_sim_path(controller->sim));
215 cam_sim_free(controller->sim, TRUE);
216 mtx_unlock(&controller->lock);
220 if (controller->timer_memory != NULL)
221 free(controller->timer_memory, M_ISCI);
223 if (controller->remote_device_memory != NULL)
224 free(controller->remote_device_memory, M_ISCI);
226 for (phy = 0; phy < SCI_MAX_PHYS; phy++) {
227 if (controller->phys[phy].cdev_fault)
228 led_destroy(controller->phys[phy].cdev_fault);
230 if (controller->phys[phy].cdev_locate)
231 led_destroy(controller->phys[phy].cdev_locate);
235 sci_pool_get(controller->unmap_buffer_pool, unmap_buffer);
236 if (unmap_buffer == NULL)
238 contigfree(unmap_buffer, PAGE_SIZE, M_ISCI);
242 /* The SCIF controllers have been stopped, so we can now
243 * free the SCI library memory.
245 if (isci->sci_library_memory != NULL)
246 free(isci->sci_library_memory, M_ISCI);
248 for (i = 0; i < ISCI_NUM_PCI_BARS; i++)
250 struct ISCI_PCI_BAR *pci_bar = &isci->pci_bar[i];
252 if (pci_bar->resource != NULL)
253 bus_release_resource(device, SYS_RES_MEMORY,
254 pci_bar->resource_id, pci_bar->resource);
257 for (i = 0; i < isci->num_interrupts; i++)
259 struct ISCI_INTERRUPT_INFO *interrupt_info;
261 interrupt_info = &isci->interrupt_info[i];
263 if(interrupt_info->tag != NULL)
264 bus_teardown_intr(device, interrupt_info->res,
265 interrupt_info->tag);
267 if(interrupt_info->res != NULL)
268 bus_release_resource(device, SYS_RES_IRQ,
269 rman_get_rid(interrupt_info->res),
270 interrupt_info->res);
272 pci_release_msi(device);
279 isci_initialize(struct isci_softc *isci)
283 uint32_t library_object_size;
284 uint32_t verbosity_mask;
285 uint32_t scic_log_object_mask;
286 uint32_t scif_log_object_mask;
287 uint8_t *header_buffer;
289 library_object_size = scif_library_get_object_size(SCI_MAX_CONTROLLERS);
291 isci->sci_library_memory =
292 malloc(library_object_size, M_ISCI, M_NOWAIT | M_ZERO );
294 isci->sci_library_handle = scif_library_construct(
295 isci->sci_library_memory, SCI_MAX_CONTROLLERS);
297 sci_object_set_association( isci->sci_library_handle, (void *)isci);
299 verbosity_mask = (1<<SCI_LOG_VERBOSITY_ERROR) |
300 (1<<SCI_LOG_VERBOSITY_WARNING) | (1<<SCI_LOG_VERBOSITY_INFO) |
301 (1<<SCI_LOG_VERBOSITY_TRACE);
303 scic_log_object_mask = 0xFFFFFFFF;
304 scic_log_object_mask &= ~SCIC_LOG_OBJECT_COMPLETION_QUEUE;
305 scic_log_object_mask &= ~SCIC_LOG_OBJECT_SSP_IO_REQUEST;
306 scic_log_object_mask &= ~SCIC_LOG_OBJECT_STP_IO_REQUEST;
307 scic_log_object_mask &= ~SCIC_LOG_OBJECT_SMP_IO_REQUEST;
308 scic_log_object_mask &= ~SCIC_LOG_OBJECT_CONTROLLER;
310 scif_log_object_mask = 0xFFFFFFFF;
311 scif_log_object_mask &= ~SCIF_LOG_OBJECT_CONTROLLER;
312 scif_log_object_mask &= ~SCIF_LOG_OBJECT_IO_REQUEST;
314 TUNABLE_INT_FETCH("hw.isci.debug_level", &g_isci_debug_level);
316 sci_logger_enable(sci_object_get_logger(isci->sci_library_handle),
317 scif_log_object_mask, verbosity_mask);
319 sci_logger_enable(sci_object_get_logger(
320 scif_library_get_scic_handle(isci->sci_library_handle)),
321 scic_log_object_mask, verbosity_mask);
323 header_buffer = (uint8_t *)&isci->pci_common_header;
324 for (uint8_t i = 0; i < sizeof(isci->pci_common_header); i++)
325 header_buffer[i] = pci_read_config(isci->device, i, 1);
327 scic_library_set_pci_info(
328 scif_library_get_scic_handle(isci->sci_library_handle),
329 &isci->pci_common_header);
331 isci->oem_parameters_found = FALSE;
333 isci_get_oem_parameters(isci);
335 /* trigger interrupt if 32 completions occur before timeout expires */
336 isci->coalesce_number = 32;
338 /* trigger interrupt if 2 microseconds elapse after a completion occurs,
339 * regardless if "coalesce_number" completions have occurred
341 isci->coalesce_timeout = 2;
343 isci->controller_count = scic_library_get_pci_device_controller_count(
344 scif_library_get_scic_handle(isci->sci_library_handle));
346 for (int index = 0; index < isci->controller_count; index++) {
347 struct ISCI_CONTROLLER *controller = &isci->controllers[index];
348 SCI_CONTROLLER_HANDLE_T scif_controller_handle;
350 controller->index = index;
351 isci_controller_construct(controller, isci);
353 scif_controller_handle = controller->scif_controller_handle;
355 status = isci_controller_initialize(controller);
357 if(status != SCI_SUCCESS) {
358 isci_log_message(0, "ISCI",
359 "isci_controller_initialize FAILED: %x\n",
364 error = isci_controller_allocate_memory(controller);
369 scif_controller_set_interrupt_coalescence(
370 scif_controller_handle, isci->coalesce_number,
371 isci->coalesce_timeout);
374 /* FreeBSD provides us a hook to ensure we get a chance to start
375 * our controllers and complete initial domain discovery before
376 * it searches for the boot device. Once we're done, we'll
377 * disestablish the hook, signaling the kernel that is can proceed
378 * with the boot process.
380 isci->config_hook.ich_func = &isci_controller_start;
381 isci->config_hook.ich_arg = &isci->controllers[0];
383 if (config_intrhook_establish(&isci->config_hook) != 0)
384 isci_log_message(0, "ISCI",
385 "config_intrhook_establish failed!\n");
391 isci_allocate_dma_buffer_callback(void *arg, bus_dma_segment_t *seg,
394 struct ISCI_MEMORY *memory = (struct ISCI_MEMORY *)arg;
396 memory->error = error;
398 if (nseg != 1 || error != 0)
399 isci_log_message(0, "ISCI",
400 "Failed to allocate physically contiguous memory!\n");
402 memory->physical_address = seg->ds_addr;
406 isci_allocate_dma_buffer(device_t device, struct ISCI_MEMORY *memory)
410 status = bus_dma_tag_create(bus_get_dma_tag(device),
411 0x40 /* cacheline alignment */, 0x0, BUS_SPACE_MAXADDR,
412 BUS_SPACE_MAXADDR, NULL, NULL, memory->size,
413 0x1 /* we want physically contiguous */,
414 memory->size, 0, NULL, NULL, &memory->dma_tag);
416 if(status == ENOMEM) {
417 isci_log_message(0, "ISCI", "bus_dma_tag_create failed\n");
421 status = bus_dmamem_alloc(memory->dma_tag,
422 (void **)&memory->virtual_address, BUS_DMA_ZERO, &memory->dma_map);
426 isci_log_message(0, "ISCI", "bus_dmamem_alloc failed\n");
430 status = bus_dmamap_load(memory->dma_tag, memory->dma_map,
431 (void *)memory->virtual_address, memory->size,
432 isci_allocate_dma_buffer_callback, memory, 0);
436 isci_log_message(0, "ISCI", "bus_dmamap_load failed\n");
444 * @brief This callback method asks the user to associate the supplied
445 * lock with an operating environment specific locking construct.
447 * @param[in] controller This parameter specifies the controller with
448 * which this lock is to be associated.
449 * @param[in] lock This parameter specifies the lock for which the
450 * user should associate an operating environment specific
453 * @see The SCI_LOCK_LEVEL enumeration for more information.
458 scif_cb_lock_associate(SCI_CONTROLLER_HANDLE_T controller,
459 SCI_LOCK_HANDLE_T lock)
465 * @brief This callback method asks the user to de-associate the supplied
466 * lock with an operating environment specific locking construct.
468 * @param[in] controller This parameter specifies the controller with
469 * which this lock is to be de-associated.
470 * @param[in] lock This parameter specifies the lock for which the
471 * user should de-associate an operating environment specific
474 * @see The SCI_LOCK_LEVEL enumeration for more information.
479 scif_cb_lock_disassociate(SCI_CONTROLLER_HANDLE_T controller,
480 SCI_LOCK_HANDLE_T lock)
487 * @brief This callback method asks the user to acquire/get the lock.
488 * This method should pend until the lock has been acquired.
490 * @param[in] controller This parameter specifies the controller with
491 * which this lock is associated.
492 * @param[in] lock This parameter specifies the lock to be acquired.
497 scif_cb_lock_acquire(SCI_CONTROLLER_HANDLE_T controller,
498 SCI_LOCK_HANDLE_T lock)
504 * @brief This callback method asks the user to release a lock.
506 * @param[in] controller This parameter specifies the controller with
507 * which this lock is associated.
508 * @param[in] lock This parameter specifies the lock to be released.
513 scif_cb_lock_release(SCI_CONTROLLER_HANDLE_T controller,
514 SCI_LOCK_HANDLE_T lock)
519 * @brief This callback method creates an OS specific deferred task
520 * for internal usage. The handler to deferred task is stored by OS
523 * @param[in] controller This parameter specifies the controller object
524 * with which this callback is associated.
529 scif_cb_start_internal_io_task_create(SCI_CONTROLLER_HANDLE_T controller)
535 * @brief This callback method schedules a OS specific deferred task.
537 * @param[in] controller This parameter specifies the controller
538 * object with which this callback is associated.
539 * @param[in] start_internal_io_task_routine This parameter specifies the
540 * sci start_internal_io routine.
541 * @param[in] context This parameter specifies a handle to a parameter
542 * that will be passed into the "start_internal_io_task_routine"
543 * when it is invoked.
548 scif_cb_start_internal_io_task_schedule(SCI_CONTROLLER_HANDLE_T scif_controller,
549 FUNCPTR start_internal_io_task_routine, void *context)
551 /** @todo Use FreeBSD tasklet to defer this routine to a later time,
552 * rather than calling the routine inline.
554 SCI_START_INTERNAL_IO_ROUTINE sci_start_internal_io_routine =
555 (SCI_START_INTERNAL_IO_ROUTINE)start_internal_io_task_routine;
557 sci_start_internal_io_routine(context);
561 * @brief In this method the user must write to PCI memory via access.
562 * This method is used for access to memory space and IO space.
564 * @param[in] controller The controller for which to read a DWORD.
565 * @param[in] address This parameter depicts the address into
567 * @param[out] write_value This parameter depicts the value being written
568 * into the PCI memory location.
570 * @todo These PCI memory access calls likely needs to be optimized into macros?
573 scic_cb_pci_write_dword(SCI_CONTROLLER_HANDLE_T scic_controller,
574 void *address, uint32_t write_value)
576 SCI_CONTROLLER_HANDLE_T scif_controller =
577 (SCI_CONTROLLER_HANDLE_T) sci_object_get_association(scic_controller);
578 struct ISCI_CONTROLLER *isci_controller =
579 (struct ISCI_CONTROLLER *) sci_object_get_association(scif_controller);
580 struct isci_softc *isci = isci_controller->isci;
581 uint32_t bar = (uint32_t)(((POINTER_UINT)address & 0xF0000000) >> 28);
582 bus_size_t offset = (bus_size_t)((POINTER_UINT)address & 0x0FFFFFFF);
584 bus_space_write_4(isci->pci_bar[bar].bus_tag,
585 isci->pci_bar[bar].bus_handle, offset, write_value);
589 * @brief In this method the user must read from PCI memory via access.
590 * This method is used for access to memory space and IO space.
592 * @param[in] controller The controller for which to read a DWORD.
593 * @param[in] address This parameter depicts the address from
596 * @return The value being returned from the PCI memory location.
598 * @todo This PCI memory access calls likely need to be optimized into macro?
601 scic_cb_pci_read_dword(SCI_CONTROLLER_HANDLE_T scic_controller, void *address)
603 SCI_CONTROLLER_HANDLE_T scif_controller =
604 (SCI_CONTROLLER_HANDLE_T)sci_object_get_association(scic_controller);
605 struct ISCI_CONTROLLER *isci_controller =
606 (struct ISCI_CONTROLLER *)sci_object_get_association(scif_controller);
607 struct isci_softc *isci = isci_controller->isci;
608 uint32_t bar = (uint32_t)(((POINTER_UINT)address & 0xF0000000) >> 28);
609 bus_size_t offset = (bus_size_t)((POINTER_UINT)address & 0x0FFFFFFF);
611 return (bus_space_read_4(isci->pci_bar[bar].bus_tag,
612 isci->pci_bar[bar].bus_handle, offset));
616 * @brief This method is called when the core requires the OS driver
617 * to stall execution. This method is utilized during initialization
618 * or non-performance paths only.
620 * @param[in] microseconds This parameter specifies the number of
621 * microseconds for which to stall. The operating system driver
622 * is allowed to round this value up where necessary.
627 scic_cb_stall_execution(uint32_t microseconds)
634 * @brief In this method the user must return the base address register (BAR)
635 * value for the supplied base address register number.
637 * @param[in] controller The controller for which to retrieve the bar number.
638 * @param[in] bar_number This parameter depicts the BAR index/number to be read.
640 * @return Return a pointer value indicating the contents of the BAR.
641 * @retval NULL indicates an invalid BAR index/number was specified.
642 * @retval All other values indicate a valid VIRTUAL address from the BAR.
645 scic_cb_pci_get_bar(SCI_CONTROLLER_HANDLE_T controller,
649 return ((void *)(POINTER_UINT)((uint32_t)bar_number << 28));
653 * @brief This method informs the SCI Core user that a phy/link became
654 * ready, but the phy is not allowed in the port. In some
655 * situations the underlying hardware only allows for certain phy
656 * to port mappings. If these mappings are violated, then this
659 * @param[in] controller This parameter represents the controller which
661 * @param[in] port This parameter specifies the SCI port object for which
662 * the callback is being invoked.
663 * @param[in] phy This parameter specifies the phy that came ready, but the
664 * phy can't be a valid member of the port.
669 scic_cb_port_invalid_link_up(SCI_CONTROLLER_HANDLE_T controller,
670 SCI_PORT_HANDLE_T port, SCI_PHY_HANDLE_T phy)