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);
95 * isci_host_start_complete() - This function is called by the core library,
96 * through the ISCI Module, to indicate controller start status.
97 * @isci_host: This parameter specifies the ISCI host object
98 * @completion_status: This parameter specifies the completion status from the
102 void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
104 if (completion_status != SCI_SUCCESS)
105 dev_info(&ihost->pdev->dev,
106 "controller start timed out, continuing...\n");
107 isci_host_change_state(ihost, isci_ready);
108 clear_bit(IHOST_START_PENDING, &ihost->flags);
109 wake_up(&ihost->eventq);
112 int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
114 struct isci_host *ihost = isci_host_from_sas_ha(SHOST_TO_SAS_HA(shost));
117 * check interrupt_handler's status and call completion_handler if true,
118 * link_up events should be coming from the scu core lib, as phy's come
119 * online. for each link_up from the core, call
120 * get_received_identify_address_frame, copy the frame into the
121 * sas_phy object and call libsas notify_port_event(PORTE_BYTES_DMAED).
122 * continue to return zero from thee scan_finished routine until
123 * the scic_cb_controller_start_complete() call comes from the core.
125 if (scic_sds_controller_isr(ihost->core_controller))
126 scic_sds_controller_completion_handler(ihost->core_controller);
128 if (test_bit(IHOST_START_PENDING, &ihost->flags) && time < HZ*10) {
129 dev_dbg(&ihost->pdev->dev,
130 "%s: ihost->status = %d, time = %ld\n",
131 __func__, isci_host_get_state(ihost), time);
136 dev_dbg(&ihost->pdev->dev,
137 "%s: ihost->status = %d, time = %ld\n",
138 __func__, isci_host_get_state(ihost), time);
140 scic_controller_enable_interrupts(ihost->core_controller);
146 void isci_host_scan_start(struct Scsi_Host *shost)
148 struct isci_host *ihost = isci_host_from_sas_ha(SHOST_TO_SAS_HA(shost));
149 struct scic_sds_controller *scic = ihost->core_controller;
150 unsigned long tmo = scic_controller_get_suggested_start_timeout(scic);
152 set_bit(IHOST_START_PENDING, &ihost->flags);
153 scic_controller_disable_interrupts(ihost->core_controller);
154 scic_controller_start(scic, tmo);
157 void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
159 isci_host_change_state(ihost, isci_stopped);
160 scic_controller_disable_interrupts(ihost->core_controller);
161 clear_bit(IHOST_STOP_PENDING, &ihost->flags);
162 wake_up(&ihost->eventq);
165 static struct coherent_memory_info *isci_host_alloc_mdl_struct(
166 struct isci_host *isci_host,
169 struct coherent_memory_info *mdl_struct;
170 void *uncached_address = NULL;
173 mdl_struct = devm_kzalloc(&isci_host->pdev->dev,
179 INIT_LIST_HEAD(&mdl_struct->node);
181 uncached_address = dmam_alloc_coherent(&isci_host->pdev->dev,
183 &mdl_struct->dma_handle,
185 if (!uncached_address)
188 /* memset the whole memory area. */
189 memset((char *)uncached_address, 0, size);
190 mdl_struct->vaddr = uncached_address;
191 mdl_struct->size = (size_t)size;
196 static void isci_host_build_mde(
197 struct sci_physical_memory_descriptor *mde_struct,
198 struct coherent_memory_info *mdl_struct)
200 unsigned long address = 0;
201 dma_addr_t dma_addr = 0;
203 address = (unsigned long)mdl_struct->vaddr;
204 dma_addr = mdl_struct->dma_handle;
206 /* to satisfy the alignment. */
207 if ((address % mde_struct->constant_memory_alignment) != 0) {
209 = (mde_struct->constant_memory_alignment
210 - (address % mde_struct->constant_memory_alignment));
211 address += align_offset;
212 dma_addr += align_offset;
215 mde_struct->virtual_address = (void *)address;
216 mde_struct->physical_address = dma_addr;
217 mdl_struct->mde = mde_struct;
220 static int isci_host_mdl_allocate_coherent(
221 struct isci_host *isci_host)
223 struct sci_physical_memory_descriptor *current_mde;
224 struct coherent_memory_info *mdl_struct;
227 struct sci_base_memory_descriptor_list *mdl_handle
228 = sci_controller_get_memory_descriptor_list_handle(
229 isci_host->core_controller);
231 sci_mdl_first_entry(mdl_handle);
233 current_mde = sci_mdl_get_current_entry(mdl_handle);
235 while (current_mde != NULL) {
237 size = (current_mde->constant_memory_size
238 + current_mde->constant_memory_alignment);
240 mdl_struct = isci_host_alloc_mdl_struct(isci_host, size);
244 list_add_tail(&mdl_struct->node, &isci_host->mdl_struct_list);
246 isci_host_build_mde(current_mde, mdl_struct);
248 sci_mdl_next_entry(mdl_handle);
249 current_mde = sci_mdl_get_current_entry(mdl_handle);
257 * isci_host_completion_routine() - This function is the delayed service
258 * routine that calls the sci core library's completion handler. It's
259 * scheduled as a tasklet from the interrupt service routine when interrupts
260 * in use, or set as the timeout function in polled mode.
261 * @data: This parameter specifies the ISCI host object
264 static void isci_host_completion_routine(unsigned long data)
266 struct isci_host *isci_host = (struct isci_host *)data;
267 struct list_head completed_request_list;
268 struct list_head aborted_request_list;
269 struct list_head *current_position;
270 struct list_head *next_position;
271 struct isci_request *request;
272 struct isci_request *next_request;
273 struct sas_task *task;
275 INIT_LIST_HEAD(&completed_request_list);
276 INIT_LIST_HEAD(&aborted_request_list);
278 spin_lock_irq(&isci_host->scic_lock);
280 scic_sds_controller_completion_handler(isci_host->core_controller);
282 /* Take the lists of completed I/Os from the host. */
283 list_splice_init(&isci_host->requests_to_complete,
284 &completed_request_list);
286 list_splice_init(&isci_host->requests_to_abort,
287 &aborted_request_list);
289 spin_unlock_irq(&isci_host->scic_lock);
291 /* Process any completions in the lists. */
292 list_for_each_safe(current_position, next_position,
293 &completed_request_list) {
295 request = list_entry(current_position, struct isci_request,
297 task = isci_request_access_task(request);
299 /* Normal notification (task_done) */
300 dev_dbg(&isci_host->pdev->dev,
301 "%s: Normal - request/task = %p/%p\n",
306 task->task_done(task);
307 task->lldd_task = NULL;
309 /* Free the request object. */
310 isci_request_free(isci_host, request);
312 list_for_each_entry_safe(request, next_request, &aborted_request_list,
315 task = isci_request_access_task(request);
317 /* Use sas_task_abort */
318 dev_warn(&isci_host->pdev->dev,
319 "%s: Error - request/task = %p/%p\n",
324 /* Put the task into the abort path. */
325 sas_task_abort(task);
330 void isci_host_deinit(struct isci_host *ihost)
332 struct scic_sds_controller *scic = ihost->core_controller;
335 isci_host_change_state(ihost, isci_stopping);
336 for (i = 0; i < SCI_MAX_PORTS; i++) {
337 struct isci_port *port = &ihost->isci_ports[i];
338 struct isci_remote_device *idev, *d;
340 list_for_each_entry_safe(idev, d, &port->remote_dev_list, node) {
341 isci_remote_device_change_state(idev, isci_stopping);
342 isci_remote_device_stop(idev);
346 set_bit(IHOST_STOP_PENDING, &ihost->flags);
347 scic_controller_stop(scic, SCIC_CONTROLLER_STOP_TIMEOUT);
348 wait_for_stop(ihost);
349 scic_controller_reset(scic);
352 static int isci_verify_firmware(const struct firmware *fw,
353 struct isci_firmware *isci_fw)
357 if (fw->size < ISCI_FIRMWARE_MIN_SIZE)
362 /* 12th char should be the NULL terminate for the ID string */
366 if (strncmp("#SCU MAGIC#", tmp, 11) != 0)
370 isci_fw->version = fw->data[ISCI_FW_VER_OFS];
371 isci_fw->subversion = fw->data[ISCI_FW_SUBVER_OFS];
373 tmp = fw->data + ISCI_FW_DATA_OFS;
375 while (*tmp != ISCI_FW_HDR_EOF) {
377 case ISCI_FW_HDR_PHYMASK:
379 isci_fw->phy_masks_size = *tmp;
381 isci_fw->phy_masks = (const u32 *)tmp;
382 tmp += sizeof(u32) * isci_fw->phy_masks_size;
385 case ISCI_FW_HDR_PHYGEN:
387 isci_fw->phy_gens_size = *tmp;
389 isci_fw->phy_gens = (const u32 *)tmp;
390 tmp += sizeof(u32) * isci_fw->phy_gens_size;
393 case ISCI_FW_HDR_SASADDR:
395 isci_fw->sas_addrs_size = *tmp;
397 isci_fw->sas_addrs = (const u64 *)tmp;
398 tmp += sizeof(u64) * isci_fw->sas_addrs_size;
402 pr_err("bad field in firmware binary blob\n");
407 pr_info("isci firmware v%u.%u loaded.\n",
408 isci_fw->version, isci_fw->subversion);
413 static void __iomem *scu_base(struct isci_host *isci_host)
415 struct pci_dev *pdev = isci_host->pdev;
416 int id = isci_host->id;
418 return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
421 static void __iomem *smu_base(struct isci_host *isci_host)
423 struct pci_dev *pdev = isci_host->pdev;
424 int id = isci_host->id;
426 return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
429 #define SCI_MAX_TIMER_COUNT 25
431 int isci_host_init(struct isci_host *isci_host)
435 enum sci_status status;
436 struct scic_sds_controller *controller;
437 struct scic_sds_port *scic_port;
438 union scic_oem_parameters scic_oem_params;
439 union scic_user_parameters scic_user_params;
440 const struct firmware *fw = NULL;
441 struct isci_firmware *isci_fw = NULL;
443 INIT_LIST_HEAD(&isci_host->timer_list_struct.timers);
444 isci_timer_list_construct(
445 &isci_host->timer_list_struct,
449 controller = scic_controller_alloc(&isci_host->pdev->dev);
453 dev_err(&isci_host->pdev->dev, "%s: failed (%d)\n", __func__, err);
457 isci_host->core_controller = controller;
458 spin_lock_init(&isci_host->state_lock);
459 spin_lock_init(&isci_host->scic_lock);
460 spin_lock_init(&isci_host->queue_lock);
461 init_waitqueue_head(&isci_host->eventq);
463 isci_host_change_state(isci_host, isci_starting);
464 isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
466 status = scic_controller_construct(controller, scu_base(isci_host),
467 smu_base(isci_host));
469 if (status != SCI_SUCCESS) {
470 dev_err(&isci_host->pdev->dev,
471 "%s: scic_controller_construct failed - status = %x\n",
478 isci_host->sas_ha.dev = &isci_host->pdev->dev;
479 isci_host->sas_ha.lldd_ha = isci_host;
481 /*----------- SCIC controller Initialization Stuff ------------------
482 * set association host adapter struct in core controller.
484 sci_object_set_association(isci_host->core_controller,
488 /* grab initial values stored in the controller object for OEM and USER
490 scic_oem_parameters_get(controller, &scic_oem_params);
491 scic_user_parameters_get(controller, &scic_user_params);
493 isci_fw = devm_kzalloc(&isci_host->pdev->dev,
494 sizeof(struct isci_firmware),
497 dev_warn(&isci_host->pdev->dev,
498 "allocating firmware struct failed\n");
499 dev_warn(&isci_host->pdev->dev,
500 "Default OEM configuration being used:"
501 " 4 narrow ports, and default SAS Addresses\n");
502 goto set_default_params;
505 status = request_firmware(&fw, ISCI_FW_NAME, &isci_host->pdev->dev);
507 dev_warn(&isci_host->pdev->dev,
508 "Loading firmware failed, using default values\n");
509 dev_warn(&isci_host->pdev->dev,
510 "Default OEM configuration being used:"
511 " 4 narrow ports, and default SAS Addresses\n");
512 goto set_default_params;
515 status = isci_verify_firmware(fw, isci_fw);
516 if (status != SCI_SUCCESS) {
517 dev_warn(&isci_host->pdev->dev,
518 "firmware verification failed\n");
519 dev_warn(&isci_host->pdev->dev,
520 "Default OEM configuration being used:"
521 " 4 narrow ports, and default SAS "
523 goto set_default_params;
526 /* grab any OEM and USER parameters specified at module load */
527 status = isci_parse_oem_parameters(&scic_oem_params,
528 isci_host->id, isci_fw);
529 if (status != SCI_SUCCESS) {
530 dev_warn(&isci_host->pdev->dev,
531 "parsing firmware oem parameters failed\n");
536 status = isci_parse_user_parameters(&scic_user_params,
537 isci_host->id, isci_fw);
538 if (status != SCI_SUCCESS) {
539 dev_warn(&isci_host->pdev->dev,
540 "%s: isci_parse_user_parameters"
541 " failed\n", __func__);
549 status = scic_oem_parameters_set(isci_host->core_controller,
553 if (status != SCI_SUCCESS) {
554 dev_warn(&isci_host->pdev->dev,
555 "%s: scic_oem_parameters_set failed\n",
562 status = scic_user_parameters_set(isci_host->core_controller,
566 if (status != SCI_SUCCESS) {
567 dev_warn(&isci_host->pdev->dev,
568 "%s: scic_user_parameters_set failed\n",
574 status = scic_controller_initialize(isci_host->core_controller);
575 if (status != SCI_SUCCESS) {
576 dev_warn(&isci_host->pdev->dev,
577 "%s: scic_controller_initialize failed -"
584 tasklet_init(&isci_host->completion_tasklet,
585 isci_host_completion_routine, (unsigned long)isci_host);
587 INIT_LIST_HEAD(&(isci_host->mdl_struct_list));
589 INIT_LIST_HEAD(&isci_host->requests_to_complete);
590 INIT_LIST_HEAD(&isci_host->requests_to_abort);
592 /* populate mdl with dma memory. scu_mdl_allocate_coherent() */
593 err = isci_host_mdl_allocate_coherent(isci_host);
599 * keep the pool alloc size around, will use it for a bounds checking
600 * when trying to convert virtual addresses to physical addresses
602 isci_host->dma_pool_alloc_size = sizeof(struct isci_request) +
603 scic_io_request_get_object_size();
604 isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
605 isci_host->dma_pool_alloc_size,
606 SLAB_HWCACHE_ALIGN, 0);
608 if (!isci_host->dma_pool) {
610 goto req_obj_err_out;
613 for (index = 0; index < SCI_MAX_PORTS; index++) {
614 isci_port_init(&isci_host->isci_ports[index],
618 for (index = 0; index < SCI_MAX_PHYS; index++)
619 isci_phy_init(&isci_host->phys[index], isci_host, index);
621 /* Why are we doing this? Is this even necessary? */
622 memcpy(&isci_host->sas_addr[0], &isci_host->phys[0].sas_addr[0],
625 /* Start the ports */
626 for (index = 0; index < SCI_MAX_PORTS; index++) {
628 scic_controller_get_port_handle(controller, index, &scic_port);
629 scic_port_start(scic_port);
634 /* SPB_Debug: destroy request object cache */
636 /* SPB_Debug: destroy remote object cache */
638 /* SPB_Debug: undo controller init, construct and alloc, remove from parent
639 * controller list. */
642 release_firmware(fw);