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.
56 #include <linux/kernel.h>
57 #include <linux/init.h>
58 #include <linux/module.h>
59 #include <asm/string.h>
62 #include "sci_controller_constants.h"
63 #include "scic_remote_device.h"
64 #include "sci_environment.h"
66 static struct scsi_transport_template *isci_transport_template;
67 struct kmem_cache *isci_kmem_cache;
69 static DEFINE_PCI_DEVICE_TABLE(isci_id_table) = {
70 { PCI_VDEVICE(INTEL, 0x1D61),},
71 { PCI_VDEVICE(INTEL, 0x1D63),},
72 { PCI_VDEVICE(INTEL, 0x1D65),},
73 { PCI_VDEVICE(INTEL, 0x1D67),},
74 { PCI_VDEVICE(INTEL, 0x1D69),},
75 { PCI_VDEVICE(INTEL, 0x1D6B),},
76 { PCI_VDEVICE(INTEL, 0x1D60),},
77 { PCI_VDEVICE(INTEL, 0x1D62),},
78 { PCI_VDEVICE(INTEL, 0x1D64),},
79 { PCI_VDEVICE(INTEL, 0x1D66),},
80 { PCI_VDEVICE(INTEL, 0x1D68),},
81 { PCI_VDEVICE(INTEL, 0x1D6A),},
85 static int __devinit isci_pci_probe(
87 const struct pci_device_id *device_id_p);
89 static void __devexit isci_pci_remove(struct pci_dev *pdev);
91 MODULE_DEVICE_TABLE(pci, isci_id_table);
93 static struct pci_driver isci_pci_driver = {
95 .id_table = isci_id_table,
96 .probe = isci_pci_probe,
97 .remove = __devexit_p(isci_pci_remove),
100 /* linux isci specific settings */
102 module_param(loglevel, int, S_IRUGO | S_IWUSR);
104 #if defined(CONFIG_PBG_HBA_A0)
105 int isci_si_rev = ISCI_SI_REVA0;
106 #elif defined(CONFIG_PBG_HBA_A2)
107 int isci_si_rev = ISCI_SI_REVA2;
109 int isci_si_rev = ISCI_SI_REVB0;
111 module_param(isci_si_rev, int, S_IRUGO | S_IWUSR);
112 MODULE_PARM_DESC(isci_si_rev, "override default si rev (0: A0 1: A2 2: B0)");
114 static struct scsi_host_template isci_sht = {
116 .module = THIS_MODULE,
118 .queuecommand = sas_queuecommand,
119 .target_alloc = sas_target_alloc,
120 .slave_configure = sas_slave_configure,
121 .slave_destroy = sas_slave_destroy,
122 .scan_finished = isci_host_scan_finished,
123 .scan_start = isci_host_scan_start,
124 .change_queue_depth = sas_change_queue_depth,
125 .change_queue_type = sas_change_queue_type,
126 .bios_param = sas_bios_param,
127 .can_queue = ISCI_CAN_QUEUE_VAL,
130 .sg_tablesize = SG_ALL,
131 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
132 .use_clustering = ENABLE_CLUSTERING,
133 .eh_device_reset_handler = sas_eh_device_reset_handler,
134 .eh_bus_reset_handler = isci_bus_reset_handler,
135 .slave_alloc = sas_slave_alloc,
136 .target_destroy = sas_target_destroy,
140 static struct sas_domain_function_template isci_transport_ops = {
142 /* The class calls these to notify the LLDD of an event. */
143 .lldd_port_formed = isci_port_formed,
144 .lldd_port_deformed = isci_port_deformed,
146 /* The class calls these when a device is found or gone. */
147 .lldd_dev_found = isci_remote_device_found,
148 .lldd_dev_gone = isci_remote_device_gone,
150 .lldd_execute_task = isci_task_execute_task,
151 /* Task Management Functions. Must be called from process context. */
152 .lldd_abort_task = isci_task_abort_task,
153 .lldd_abort_task_set = isci_task_abort_task_set,
154 .lldd_clear_aca = isci_task_clear_aca,
155 .lldd_clear_task_set = isci_task_clear_task_set,
156 .lldd_I_T_nexus_reset = isci_task_I_T_nexus_reset,
157 .lldd_lu_reset = isci_task_lu_reset,
158 .lldd_query_task = isci_task_query_task,
160 /* Port and Adapter management */
161 .lldd_clear_nexus_port = isci_task_clear_nexus_port,
162 .lldd_clear_nexus_ha = isci_task_clear_nexus_ha,
165 .lldd_control_phy = isci_phy_control,
169 /******************************************************************************
170 * P R O T E C T E D M E T H O D S
171 ******************************************************************************/
176 * isci_register_sas_ha() - This method initializes various lldd
177 * specific members of the sas_ha struct and calls the libsas
178 * sas_register_ha() function.
179 * @isci_host: This parameter specifies the lldd specific wrapper for the
180 * libsas sas_ha struct.
182 * This method returns an error code indicating sucess or failure. The user
183 * should check for possible memory allocation error return otherwise, a zero
186 static int isci_register_sas_ha(struct isci_host *isci_host)
189 struct sas_ha_struct *sas_ha = &(isci_host->sas_ha);
190 struct asd_sas_phy **sas_phys;
191 struct asd_sas_port **sas_ports;
193 sas_phys = devm_kzalloc(&isci_host->pdev->dev,
194 SCI_MAX_PHYS * sizeof(void *),
199 sas_ports = devm_kzalloc(&isci_host->pdev->dev,
200 SCI_MAX_PORTS * sizeof(void *),
205 /*----------------- Libsas Initialization Stuff----------------------
206 * Set various fields in the sas_ha struct:
209 sas_ha->sas_ha_name = DRV_NAME;
210 sas_ha->lldd_module = THIS_MODULE;
211 sas_ha->sas_addr = &(isci_host->sas_addr[0]);
213 /* set the array of phy and port structs. */
214 for (i = 0; i < SCI_MAX_PHYS; i++) {
215 sas_phys[i] = &(isci_host->phys[i].sas_phy);
216 sas_ports[i] = &(isci_host->sas_ports[i]);
219 sas_ha->sas_phy = sas_phys;
220 sas_ha->sas_port = sas_ports;
221 sas_ha->num_phys = SCI_MAX_PHYS;
223 sas_ha->lldd_queue_size = ISCI_CAN_QUEUE_VAL;
224 sas_ha->lldd_max_execute_num = 1;
225 sas_ha->strict_wide_ports = 1;
227 sas_register_ha(sas_ha);
232 static void isci_unregister_sas_ha(struct isci_host *isci_host)
237 sas_unregister_ha(&(isci_host->sas_ha));
239 sas_remove_host(isci_host->shost);
240 scsi_remove_host(isci_host->shost);
241 scsi_host_put(isci_host->shost);
244 static int __devinit isci_pci_init(struct pci_dev *pdev)
246 int err, bar_num, bar_mask;
247 void __iomem * const *iomap;
249 err = pcim_enable_device(pdev);
252 "failed enable PCI device %s!\n",
257 for (bar_num = 0; bar_num < SCI_PCI_BAR_COUNT; bar_num++)
258 bar_mask |= 1 << (bar_num * 2);
260 err = pcim_iomap_regions(pdev, bar_mask, DRV_NAME);
264 iomap = pcim_iomap_table(pdev);
268 pci_set_master(pdev);
270 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
272 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
277 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
279 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
287 static struct isci_host *isci_host_by_id(struct pci_dev *pdev, int id)
291 for_each_isci_host(h, pdev)
297 static int num_controllers(struct pci_dev *pdev)
299 /* bar size alone can tell us if we are running with a dual controller
300 * part, no need to trust revision ids that might be under broken firmware
303 resource_size_t scu_bar_size = pci_resource_len(pdev, SCI_SCU_BAR*2);
304 resource_size_t smu_bar_size = pci_resource_len(pdev, SCI_SMU_BAR*2);
306 if (scu_bar_size >= SCI_SCU_BAR_SIZE*SCI_MAX_CONTROLLERS &&
307 smu_bar_size >= SCI_SMU_BAR_SIZE*SCI_MAX_CONTROLLERS)
308 return SCI_MAX_CONTROLLERS;
313 static int isci_setup_interrupts(struct pci_dev *pdev)
315 int err, i, num_msix;
316 struct isci_pci_info *pci_info = to_pci_info(pdev);
319 * Determine the number of vectors associated with this
322 num_msix = num_controllers(pdev) * SCI_NUM_MSI_X_INT;
324 for (i = 0; i < num_msix; i++)
325 pci_info->msix_entries[i].entry = i;
327 err = pci_enable_msix(pdev, pci_info->msix_entries, num_msix);
331 for (i = 0; i < num_msix; i++) {
332 int id = i / SCI_NUM_MSI_X_INT;
333 struct msix_entry *msix = &pci_info->msix_entries[i];
334 struct isci_host *isci_host = isci_host_by_id(pdev, id);
338 /* @todo: need to handle error case. */
339 err = devm_request_irq(&pdev->dev, msix->vector, isci_isr, 0,
340 DRV_NAME"-msix", isci_host);
344 dev_info(&pdev->dev, "msix setup failed falling back to intx\n");
346 id = i / SCI_NUM_MSI_X_INT;
347 isci_host = isci_host_by_id(pdev, id);
348 msix = &pci_info->msix_entries[i];
349 devm_free_irq(&pdev->dev, msix->vector, isci_host);
351 pci_disable_msix(pdev);
358 err = devm_request_irq(&pdev->dev, pdev->irq, isci_legacy_isr,
359 IRQF_SHARED, DRV_NAME"-intx", pdev);
365 * isci_parse_oem_parameters() - This method will take OEM parameters
366 * from the module init parameters and copy them to oem_params. This will
367 * only copy values that are not set to the module parameter default values
368 * @oem_parameters: This parameter specifies the controller default OEM
369 * parameters. It is expected that this has been initialized to the default
370 * parameters for the controller
374 enum sci_status isci_parse_oem_parameters(union scic_oem_parameters *oem_params,
376 struct isci_firmware *fw)
380 /* check for valid inputs */
382 && scu_index < SCI_MAX_CONTROLLERS
383 && oem_params != NULL)) {
387 for (i = 0; i < SCI_MAX_PHYS; i++) {
388 int array_idx = i + (SCI_MAX_PHYS * scu_index);
389 u64 sas_addr = fw->sas_addrs[array_idx];
392 oem_params->sds1.phys[i].sas_address.low =
393 (u32)(sas_addr & 0xffffffff);
394 oem_params->sds1.phys[i].sas_address.high =
395 (u32)((sas_addr >> 32) & 0xffffffff);
399 for (i = 0; i < SCI_MAX_PORTS; i++) {
400 int array_idx = i + (SCI_MAX_PORTS * scu_index);
401 u32 pmask = fw->phy_masks[array_idx];
403 oem_params->sds1.ports[i].phy_mask = pmask;
410 * isci_parse_user_parameters() - This method will take user parameters
411 * from the module init parameters and copy them to user_params. This will
412 * only copy values that are not set to the module parameter default values
413 * @user_parameters: This parameter specifies the controller default user
414 * parameters. It is expected that this has been initialized to the default
415 * parameters for the controller
419 enum sci_status isci_parse_user_parameters(
420 union scic_user_parameters *user_params,
422 struct isci_firmware *fw)
427 && scu_index < SCI_MAX_CONTROLLERS
428 && user_params != NULL)) {
432 for (i = 0; i < SCI_MAX_PORTS; i++) {
433 int array_idx = i + (SCI_MAX_PORTS * scu_index);
434 u32 gen = fw->phy_gens[array_idx];
436 user_params->sds1.phys[i].max_speed_generation = gen;
443 static struct isci_host *isci_host_alloc(struct pci_dev *pdev, int id)
445 struct isci_host *isci_host;
446 struct Scsi_Host *shost;
449 isci_host = devm_kzalloc(&pdev->dev, sizeof(*isci_host), GFP_KERNEL);
453 isci_host->pdev = pdev;
456 shost = scsi_host_alloc(&isci_sht, sizeof(void *));
459 isci_host->shost = shost;
461 err = isci_host_init(isci_host);
465 SHOST_TO_SAS_HA(shost) = &isci_host->sas_ha;
466 isci_host->sas_ha.core.shost = shost;
467 shost->transportt = isci_transport_template;
471 shost->max_cmd_len = MAX_COMMAND_SIZE;
473 err = scsi_add_host(shost, &pdev->dev);
477 err = isci_register_sas_ha(isci_host);
479 goto err_shost_remove;
484 scsi_remove_host(shost);
486 scsi_host_put(shost);
491 static void check_si_rev(struct pci_dev *pdev)
493 if (num_controllers(pdev) > 1)
494 isci_si_rev = ISCI_SI_REVB0;
496 switch (pdev->revision) {
499 /* if the id is ambiguous don't update isci_si_rev */
502 isci_si_rev = ISCI_SI_REVA2;
506 isci_si_rev = ISCI_SI_REVB0;
511 dev_info(&pdev->dev, "driver configured for %s silicon (rev: %d)\n",
512 isci_si_rev == ISCI_SI_REVA0 ? "A0" :
513 isci_si_rev == ISCI_SI_REVA2 ? "A2" : "B0", pdev->revision);
517 static int __devinit isci_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
519 struct isci_pci_info *pci_info;
521 struct isci_host *isci_host;
525 pci_info = devm_kzalloc(&pdev->dev, sizeof(*pci_info), GFP_KERNEL);
528 pci_set_drvdata(pdev, pci_info);
530 err = isci_pci_init(pdev);
534 for (i = 0; i < num_controllers(pdev); i++) {
535 struct isci_host *h = isci_host_alloc(pdev, i);
542 h->next = pci_info->hosts;
546 err = isci_setup_interrupts(pdev);
550 for_each_isci_host(isci_host, pdev)
551 scsi_scan_host(isci_host->shost);
556 for_each_isci_host(isci_host, pdev)
557 isci_unregister_sas_ha(isci_host);
561 static void __devexit isci_pci_remove(struct pci_dev *pdev)
563 struct isci_host *isci_host;
565 for_each_isci_host(isci_host, pdev) {
566 isci_unregister_sas_ha(isci_host);
567 isci_host_deinit(isci_host);
568 scic_controller_disable_interrupts(isci_host->core_controller);
572 static __init int isci_init(void)
576 pr_info("%s: Intel(R) C600 SAS Controller Driver\n", DRV_NAME);
578 isci_kmem_cache = kmem_cache_create(DRV_NAME,
579 sizeof(struct isci_remote_device) +
580 scic_remote_device_get_object_size(),
582 if (!isci_kmem_cache)
585 isci_transport_template = sas_domain_attach_transport(&isci_transport_ops);
586 if (!isci_transport_template)
589 err = pci_register_driver(&isci_pci_driver);
596 sas_release_transport(isci_transport_template);
598 kmem_cache_destroy(isci_kmem_cache);
603 static __exit void isci_exit(void)
605 pci_unregister_driver(&isci_pci_driver);
606 sas_release_transport(isci_transport_template);
607 kmem_cache_destroy(isci_kmem_cache);
610 MODULE_LICENSE("Dual BSD/GPL");
611 MODULE_FIRMWARE(ISCI_FW_NAME);
612 module_init(isci_init);
613 module_exit(isci_exit);