2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 #include "scic_io_request.h"
58 #include "scic_remote_device.h"
59 #include "scic_port.h"
65 irqreturn_t isci_msix_isr(int vec, void *data)
67 struct isci_host *ihost = data;
68 struct scic_sds_controller *scic = ihost->core_controller;
70 if (scic_sds_controller_isr(scic))
71 tasklet_schedule(&ihost->completion_tasklet);
76 irqreturn_t isci_intx_isr(int vec, void *data)
78 struct pci_dev *pdev = data;
79 struct isci_host *ihost;
80 irqreturn_t ret = IRQ_NONE;
82 for_each_isci_host(ihost, pdev) {
83 struct scic_sds_controller *scic = ihost->core_controller;
85 if (scic_sds_controller_isr(scic)) {
86 tasklet_schedule(&ihost->completion_tasklet);
88 } else if (scic_sds_controller_error_isr(scic)) {
89 spin_lock(&ihost->scic_lock);
90 scic_sds_controller_error_handler(scic);
91 spin_unlock(&ihost->scic_lock);
99 irqreturn_t isci_error_isr(int vec, void *data)
101 struct isci_host *ihost = data;
102 struct scic_sds_controller *scic = ihost->core_controller;
104 if (scic_sds_controller_error_isr(scic))
105 scic_sds_controller_error_handler(scic);
111 * isci_host_start_complete() - This function is called by the core library,
112 * through the ISCI Module, to indicate controller start status.
113 * @isci_host: This parameter specifies the ISCI host object
114 * @completion_status: This parameter specifies the completion status from the
118 void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
120 if (completion_status != SCI_SUCCESS)
121 dev_info(&ihost->pdev->dev,
122 "controller start timed out, continuing...\n");
123 isci_host_change_state(ihost, isci_ready);
124 clear_bit(IHOST_START_PENDING, &ihost->flags);
125 wake_up(&ihost->eventq);
128 int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
130 struct isci_host *ihost = isci_host_from_sas_ha(SHOST_TO_SAS_HA(shost));
132 if (test_bit(IHOST_START_PENDING, &ihost->flags))
135 /* todo: use sas_flush_discovery once it is upstream */
136 scsi_flush_work(shost);
138 scsi_flush_work(shost);
140 dev_dbg(&ihost->pdev->dev,
141 "%s: ihost->status = %d, time = %ld\n",
142 __func__, isci_host_get_state(ihost), time);
148 void isci_host_scan_start(struct Scsi_Host *shost)
150 struct isci_host *ihost = isci_host_from_sas_ha(SHOST_TO_SAS_HA(shost));
151 struct scic_sds_controller *scic = ihost->core_controller;
152 unsigned long tmo = scic_controller_get_suggested_start_timeout(scic);
154 set_bit(IHOST_START_PENDING, &ihost->flags);
156 spin_lock_irq(&ihost->scic_lock);
157 scic_controller_start(scic, tmo);
158 scic_controller_enable_interrupts(scic);
159 spin_unlock_irq(&ihost->scic_lock);
162 void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
164 isci_host_change_state(ihost, isci_stopped);
165 scic_controller_disable_interrupts(ihost->core_controller);
166 clear_bit(IHOST_STOP_PENDING, &ihost->flags);
167 wake_up(&ihost->eventq);
170 static struct coherent_memory_info *isci_host_alloc_mdl_struct(
171 struct isci_host *isci_host,
174 struct coherent_memory_info *mdl_struct;
175 void *uncached_address = NULL;
178 mdl_struct = devm_kzalloc(&isci_host->pdev->dev,
184 INIT_LIST_HEAD(&mdl_struct->node);
186 uncached_address = dmam_alloc_coherent(&isci_host->pdev->dev,
188 &mdl_struct->dma_handle,
190 if (!uncached_address)
193 /* memset the whole memory area. */
194 memset((char *)uncached_address, 0, size);
195 mdl_struct->vaddr = uncached_address;
196 mdl_struct->size = (size_t)size;
201 static void isci_host_build_mde(
202 struct sci_physical_memory_descriptor *mde_struct,
203 struct coherent_memory_info *mdl_struct)
205 unsigned long address = 0;
206 dma_addr_t dma_addr = 0;
208 address = (unsigned long)mdl_struct->vaddr;
209 dma_addr = mdl_struct->dma_handle;
211 /* to satisfy the alignment. */
212 if ((address % mde_struct->constant_memory_alignment) != 0) {
214 = (mde_struct->constant_memory_alignment
215 - (address % mde_struct->constant_memory_alignment));
216 address += align_offset;
217 dma_addr += align_offset;
220 mde_struct->virtual_address = (void *)address;
221 mde_struct->physical_address = dma_addr;
222 mdl_struct->mde = mde_struct;
225 static int isci_host_mdl_allocate_coherent(
226 struct isci_host *isci_host)
228 struct sci_physical_memory_descriptor *current_mde;
229 struct coherent_memory_info *mdl_struct;
232 struct sci_base_memory_descriptor_list *mdl_handle
233 = sci_controller_get_memory_descriptor_list_handle(
234 isci_host->core_controller);
236 sci_mdl_first_entry(mdl_handle);
238 current_mde = sci_mdl_get_current_entry(mdl_handle);
240 while (current_mde != NULL) {
242 size = (current_mde->constant_memory_size
243 + current_mde->constant_memory_alignment);
245 mdl_struct = isci_host_alloc_mdl_struct(isci_host, size);
249 list_add_tail(&mdl_struct->node, &isci_host->mdl_struct_list);
251 isci_host_build_mde(current_mde, mdl_struct);
253 sci_mdl_next_entry(mdl_handle);
254 current_mde = sci_mdl_get_current_entry(mdl_handle);
262 * isci_host_completion_routine() - This function is the delayed service
263 * routine that calls the sci core library's completion handler. It's
264 * scheduled as a tasklet from the interrupt service routine when interrupts
265 * in use, or set as the timeout function in polled mode.
266 * @data: This parameter specifies the ISCI host object
269 static void isci_host_completion_routine(unsigned long data)
271 struct isci_host *isci_host = (struct isci_host *)data;
272 struct list_head completed_request_list;
273 struct list_head aborted_request_list;
274 struct list_head *current_position;
275 struct list_head *next_position;
276 struct isci_request *request;
277 struct isci_request *next_request;
278 struct sas_task *task;
280 INIT_LIST_HEAD(&completed_request_list);
281 INIT_LIST_HEAD(&aborted_request_list);
283 spin_lock_irq(&isci_host->scic_lock);
285 scic_sds_controller_completion_handler(isci_host->core_controller);
287 /* Take the lists of completed I/Os from the host. */
288 list_splice_init(&isci_host->requests_to_complete,
289 &completed_request_list);
291 list_splice_init(&isci_host->requests_to_abort,
292 &aborted_request_list);
294 spin_unlock_irq(&isci_host->scic_lock);
296 /* Process any completions in the lists. */
297 list_for_each_safe(current_position, next_position,
298 &completed_request_list) {
300 request = list_entry(current_position, struct isci_request,
302 task = isci_request_access_task(request);
304 /* Normal notification (task_done) */
305 dev_dbg(&isci_host->pdev->dev,
306 "%s: Normal - request/task = %p/%p\n",
311 task->task_done(task);
312 task->lldd_task = NULL;
314 /* Free the request object. */
315 isci_request_free(isci_host, request);
317 list_for_each_entry_safe(request, next_request, &aborted_request_list,
320 task = isci_request_access_task(request);
322 /* Use sas_task_abort */
323 dev_warn(&isci_host->pdev->dev,
324 "%s: Error - request/task = %p/%p\n",
329 /* Put the task into the abort path. */
330 sas_task_abort(task);
335 void isci_host_deinit(struct isci_host *ihost)
337 struct scic_sds_controller *scic = ihost->core_controller;
340 isci_host_change_state(ihost, isci_stopping);
341 for (i = 0; i < SCI_MAX_PORTS; i++) {
342 struct isci_port *port = &ihost->isci_ports[i];
343 struct isci_remote_device *idev, *d;
345 list_for_each_entry_safe(idev, d, &port->remote_dev_list, node) {
346 isci_remote_device_change_state(idev, isci_stopping);
347 isci_remote_device_stop(idev);
351 set_bit(IHOST_STOP_PENDING, &ihost->flags);
352 scic_controller_stop(scic, SCIC_CONTROLLER_STOP_TIMEOUT);
353 wait_for_stop(ihost);
354 scic_controller_reset(scic);
357 static int isci_verify_firmware(const struct firmware *fw,
358 struct isci_firmware *isci_fw)
362 if (fw->size < ISCI_FIRMWARE_MIN_SIZE)
367 /* 12th char should be the NULL terminate for the ID string */
371 if (strncmp("#SCU MAGIC#", tmp, 11) != 0)
375 isci_fw->version = fw->data[ISCI_FW_VER_OFS];
376 isci_fw->subversion = fw->data[ISCI_FW_SUBVER_OFS];
378 tmp = fw->data + ISCI_FW_DATA_OFS;
380 while (*tmp != ISCI_FW_HDR_EOF) {
382 case ISCI_FW_HDR_PHYMASK:
384 isci_fw->phy_masks_size = *tmp;
386 isci_fw->phy_masks = (const u32 *)tmp;
387 tmp += sizeof(u32) * isci_fw->phy_masks_size;
390 case ISCI_FW_HDR_PHYGEN:
392 isci_fw->phy_gens_size = *tmp;
394 isci_fw->phy_gens = (const u32 *)tmp;
395 tmp += sizeof(u32) * isci_fw->phy_gens_size;
398 case ISCI_FW_HDR_SASADDR:
400 isci_fw->sas_addrs_size = *tmp;
402 isci_fw->sas_addrs = (const u64 *)tmp;
403 tmp += sizeof(u64) * isci_fw->sas_addrs_size;
407 pr_err("bad field in firmware binary blob\n");
412 pr_info("isci firmware v%u.%u loaded.\n",
413 isci_fw->version, isci_fw->subversion);
418 static void __iomem *scu_base(struct isci_host *isci_host)
420 struct pci_dev *pdev = isci_host->pdev;
421 int id = isci_host->id;
423 return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
426 static void __iomem *smu_base(struct isci_host *isci_host)
428 struct pci_dev *pdev = isci_host->pdev;
429 int id = isci_host->id;
431 return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
434 #define SCI_MAX_TIMER_COUNT 25
436 int isci_host_init(struct isci_host *isci_host)
440 enum sci_status status;
441 struct scic_sds_controller *controller;
442 struct scic_sds_port *scic_port;
443 union scic_oem_parameters scic_oem_params;
444 union scic_user_parameters scic_user_params;
445 const struct firmware *fw = NULL;
446 struct isci_firmware *isci_fw = NULL;
448 INIT_LIST_HEAD(&isci_host->timer_list_struct.timers);
449 isci_timer_list_construct(
450 &isci_host->timer_list_struct,
454 controller = scic_controller_alloc(&isci_host->pdev->dev);
458 dev_err(&isci_host->pdev->dev, "%s: failed (%d)\n", __func__, err);
462 isci_host->core_controller = controller;
463 spin_lock_init(&isci_host->state_lock);
464 spin_lock_init(&isci_host->scic_lock);
465 spin_lock_init(&isci_host->queue_lock);
466 init_waitqueue_head(&isci_host->eventq);
468 isci_host_change_state(isci_host, isci_starting);
469 isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
471 status = scic_controller_construct(controller, scu_base(isci_host),
472 smu_base(isci_host));
474 if (status != SCI_SUCCESS) {
475 dev_err(&isci_host->pdev->dev,
476 "%s: scic_controller_construct failed - status = %x\n",
483 isci_host->sas_ha.dev = &isci_host->pdev->dev;
484 isci_host->sas_ha.lldd_ha = isci_host;
486 /*----------- SCIC controller Initialization Stuff ------------------
487 * set association host adapter struct in core controller.
489 sci_object_set_association(isci_host->core_controller,
493 /* grab initial values stored in the controller object for OEM and USER
495 scic_oem_parameters_get(controller, &scic_oem_params);
496 scic_user_parameters_get(controller, &scic_user_params);
498 isci_fw = devm_kzalloc(&isci_host->pdev->dev,
499 sizeof(struct isci_firmware),
502 dev_warn(&isci_host->pdev->dev,
503 "allocating firmware struct failed\n");
504 dev_warn(&isci_host->pdev->dev,
505 "Default OEM configuration being used:"
506 " 4 narrow ports, and default SAS Addresses\n");
507 goto set_default_params;
510 status = request_firmware(&fw, ISCI_FW_NAME, &isci_host->pdev->dev);
512 dev_warn(&isci_host->pdev->dev,
513 "Loading firmware failed, using default values\n");
514 dev_warn(&isci_host->pdev->dev,
515 "Default OEM configuration being used:"
516 " 4 narrow ports, and default SAS Addresses\n");
517 goto set_default_params;
520 status = isci_verify_firmware(fw, isci_fw);
521 if (status != SCI_SUCCESS) {
522 dev_warn(&isci_host->pdev->dev,
523 "firmware verification failed\n");
524 dev_warn(&isci_host->pdev->dev,
525 "Default OEM configuration being used:"
526 " 4 narrow ports, and default SAS "
528 goto set_default_params;
531 /* grab any OEM and USER parameters specified at module load */
532 status = isci_parse_oem_parameters(&scic_oem_params,
533 isci_host->id, isci_fw);
534 if (status != SCI_SUCCESS) {
535 dev_warn(&isci_host->pdev->dev,
536 "parsing firmware oem parameters failed\n");
541 status = isci_parse_user_parameters(&scic_user_params,
542 isci_host->id, isci_fw);
543 if (status != SCI_SUCCESS) {
544 dev_warn(&isci_host->pdev->dev,
545 "%s: isci_parse_user_parameters"
546 " failed\n", __func__);
554 status = scic_oem_parameters_set(isci_host->core_controller,
558 if (status != SCI_SUCCESS) {
559 dev_warn(&isci_host->pdev->dev,
560 "%s: scic_oem_parameters_set failed\n",
567 status = scic_user_parameters_set(isci_host->core_controller,
571 if (status != SCI_SUCCESS) {
572 dev_warn(&isci_host->pdev->dev,
573 "%s: scic_user_parameters_set failed\n",
579 status = scic_controller_initialize(isci_host->core_controller);
580 if (status != SCI_SUCCESS) {
581 dev_warn(&isci_host->pdev->dev,
582 "%s: scic_controller_initialize failed -"
589 tasklet_init(&isci_host->completion_tasklet,
590 isci_host_completion_routine, (unsigned long)isci_host);
592 INIT_LIST_HEAD(&(isci_host->mdl_struct_list));
594 INIT_LIST_HEAD(&isci_host->requests_to_complete);
595 INIT_LIST_HEAD(&isci_host->requests_to_abort);
597 /* populate mdl with dma memory. scu_mdl_allocate_coherent() */
598 err = isci_host_mdl_allocate_coherent(isci_host);
604 * keep the pool alloc size around, will use it for a bounds checking
605 * when trying to convert virtual addresses to physical addresses
607 isci_host->dma_pool_alloc_size = sizeof(struct isci_request) +
608 scic_io_request_get_object_size();
609 isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
610 isci_host->dma_pool_alloc_size,
611 SLAB_HWCACHE_ALIGN, 0);
613 if (!isci_host->dma_pool) {
615 goto req_obj_err_out;
618 for (index = 0; index < SCI_MAX_PORTS; index++) {
619 isci_port_init(&isci_host->isci_ports[index],
623 for (index = 0; index < SCI_MAX_PHYS; index++)
624 isci_phy_init(&isci_host->phys[index], isci_host, index);
626 /* Why are we doing this? Is this even necessary? */
627 memcpy(&isci_host->sas_addr[0], &isci_host->phys[0].sas_addr[0],
630 /* Start the ports */
631 for (index = 0; index < SCI_MAX_PORTS; index++) {
633 scic_controller_get_port_handle(controller, index, &scic_port);
634 scic_port_start(scic_port);
639 /* SPB_Debug: destroy request object cache */
641 /* SPB_Debug: destroy remote object cache */
643 /* SPB_Debug: undo controller init, construct and alloc, remove from parent
644 * controller list. */
647 release_firmware(fw);