blob: 4d6decb6d08ce75033a5bf10d6cd9e71409d57cd [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
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.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
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.
12 *
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.
17 *
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.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
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
38 * distribution.
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.
42 *
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.
54 */
55
56#include <linux/kernel.h>
57#include <linux/init.h>
58#include <linux/module.h>
59#include <asm/string.h>
60#include "isci.h"
61#include "task.h"
62#include "sci_controller_constants.h"
63#include "scic_remote_device.h"
64#include "sci_environment.h"
65
66static struct scsi_transport_template *isci_transport_template;
67struct kmem_cache *isci_kmem_cache;
68
69static 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),},
82 {}
83};
84
85static int __devinit isci_pci_probe(
86 struct pci_dev *pdev,
87 const struct pci_device_id *device_id_p);
88
89static void __devexit isci_pci_remove(struct pci_dev *pdev);
90
91MODULE_DEVICE_TABLE(pci, isci_id_table);
92
93static struct pci_driver isci_pci_driver = {
94 .name = DRV_NAME,
95 .id_table = isci_id_table,
96 .probe = isci_pci_probe,
97 .remove = __devexit_p(isci_pci_remove),
98};
99
100/* linux isci specific settings */
Dan Williams6f231dd2011-07-02 22:56:22 -0700101
102#if defined(CONFIG_PBG_HBA_A0)
103int isci_si_rev = ISCI_SI_REVA0;
104#elif defined(CONFIG_PBG_HBA_A2)
105int isci_si_rev = ISCI_SI_REVA2;
106#else
107int isci_si_rev = ISCI_SI_REVB0;
108#endif
109module_param(isci_si_rev, int, S_IRUGO | S_IWUSR);
110MODULE_PARM_DESC(isci_si_rev, "override default si rev (0: A0 1: A2 2: B0)");
111
112static struct scsi_host_template isci_sht = {
113
114 .module = THIS_MODULE,
115 .name = DRV_NAME,
116 .queuecommand = sas_queuecommand,
117 .target_alloc = sas_target_alloc,
118 .slave_configure = sas_slave_configure,
119 .slave_destroy = sas_slave_destroy,
120 .scan_finished = isci_host_scan_finished,
121 .scan_start = isci_host_scan_start,
122 .change_queue_depth = sas_change_queue_depth,
123 .change_queue_type = sas_change_queue_type,
124 .bios_param = sas_bios_param,
125 .can_queue = ISCI_CAN_QUEUE_VAL,
126 .cmd_per_lun = 1,
127 .this_id = -1,
128 .sg_tablesize = SG_ALL,
129 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
130 .use_clustering = ENABLE_CLUSTERING,
131 .eh_device_reset_handler = sas_eh_device_reset_handler,
132 .eh_bus_reset_handler = isci_bus_reset_handler,
133 .slave_alloc = sas_slave_alloc,
134 .target_destroy = sas_target_destroy,
135 .ioctl = sas_ioctl,
136};
137
138static struct sas_domain_function_template isci_transport_ops = {
139
140 /* The class calls these to notify the LLDD of an event. */
141 .lldd_port_formed = isci_port_formed,
142 .lldd_port_deformed = isci_port_deformed,
143
144 /* The class calls these when a device is found or gone. */
145 .lldd_dev_found = isci_remote_device_found,
146 .lldd_dev_gone = isci_remote_device_gone,
147
148 .lldd_execute_task = isci_task_execute_task,
149 /* Task Management Functions. Must be called from process context. */
150 .lldd_abort_task = isci_task_abort_task,
151 .lldd_abort_task_set = isci_task_abort_task_set,
152 .lldd_clear_aca = isci_task_clear_aca,
153 .lldd_clear_task_set = isci_task_clear_task_set,
154 .lldd_I_T_nexus_reset = isci_task_I_T_nexus_reset,
155 .lldd_lu_reset = isci_task_lu_reset,
156 .lldd_query_task = isci_task_query_task,
157
158 /* Port and Adapter management */
159 .lldd_clear_nexus_port = isci_task_clear_nexus_port,
160 .lldd_clear_nexus_ha = isci_task_clear_nexus_ha,
161
162 /* Phy management */
163 .lldd_control_phy = isci_phy_control,
164};
165
166
167/******************************************************************************
168* P R O T E C T E D M E T H O D S
169******************************************************************************/
170
171
172
173/**
174 * isci_register_sas_ha() - This method initializes various lldd
175 * specific members of the sas_ha struct and calls the libsas
176 * sas_register_ha() function.
177 * @isci_host: This parameter specifies the lldd specific wrapper for the
178 * libsas sas_ha struct.
179 *
180 * This method returns an error code indicating sucess or failure. The user
181 * should check for possible memory allocation error return otherwise, a zero
182 * indicates success.
183 */
184static int isci_register_sas_ha(struct isci_host *isci_host)
185{
186 int i;
187 struct sas_ha_struct *sas_ha = &(isci_host->sas_ha);
188 struct asd_sas_phy **sas_phys;
189 struct asd_sas_port **sas_ports;
190
191 sas_phys = devm_kzalloc(&isci_host->pdev->dev,
192 SCI_MAX_PHYS * sizeof(void *),
193 GFP_KERNEL);
194 if (!sas_phys)
195 return -ENOMEM;
196
197 sas_ports = devm_kzalloc(&isci_host->pdev->dev,
198 SCI_MAX_PORTS * sizeof(void *),
199 GFP_KERNEL);
200 if (!sas_ports)
201 return -ENOMEM;
202
203 /*----------------- Libsas Initialization Stuff----------------------
204 * Set various fields in the sas_ha struct:
205 */
206
207 sas_ha->sas_ha_name = DRV_NAME;
208 sas_ha->lldd_module = THIS_MODULE;
209 sas_ha->sas_addr = &(isci_host->sas_addr[0]);
210
211 /* set the array of phy and port structs. */
212 for (i = 0; i < SCI_MAX_PHYS; i++) {
213 sas_phys[i] = &(isci_host->phys[i].sas_phy);
214 sas_ports[i] = &(isci_host->sas_ports[i]);
215 }
216
217 sas_ha->sas_phy = sas_phys;
218 sas_ha->sas_port = sas_ports;
219 sas_ha->num_phys = SCI_MAX_PHYS;
220
221 sas_ha->lldd_queue_size = ISCI_CAN_QUEUE_VAL;
222 sas_ha->lldd_max_execute_num = 1;
223 sas_ha->strict_wide_ports = 1;
224
225 sas_register_ha(sas_ha);
226
227 return 0;
228}
229
230static void isci_unregister_sas_ha(struct isci_host *isci_host)
231{
232 if (!isci_host)
233 return;
234
235 sas_unregister_ha(&(isci_host->sas_ha));
236
237 sas_remove_host(isci_host->shost);
238 scsi_remove_host(isci_host->shost);
239 scsi_host_put(isci_host->shost);
240}
241
242static int __devinit isci_pci_init(struct pci_dev *pdev)
243{
244 int err, bar_num, bar_mask;
245 void __iomem * const *iomap;
246
247 err = pcim_enable_device(pdev);
248 if (err) {
249 dev_err(&pdev->dev,
250 "failed enable PCI device %s!\n",
251 pci_name(pdev));
252 return err;
253 }
254
255 for (bar_num = 0; bar_num < SCI_PCI_BAR_COUNT; bar_num++)
256 bar_mask |= 1 << (bar_num * 2);
257
258 err = pcim_iomap_regions(pdev, bar_mask, DRV_NAME);
259 if (err)
260 return err;
261
262 iomap = pcim_iomap_table(pdev);
263 if (!iomap)
264 return -ENOMEM;
265
266 pci_set_master(pdev);
267
268 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
269 if (err) {
270 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
271 if (err)
272 return err;
273 }
274
275 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
276 if (err) {
277 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
278 if (err)
279 return err;
280 }
281
282 return 0;
283}
284
285static struct isci_host *isci_host_by_id(struct pci_dev *pdev, int id)
286{
287 struct isci_host *h;
288
289 for_each_isci_host(h, pdev)
290 if (h->id == id)
291 return h;
292 return NULL;
293}
294
295static int num_controllers(struct pci_dev *pdev)
296{
297 /* bar size alone can tell us if we are running with a dual controller
298 * part, no need to trust revision ids that might be under broken firmware
299 * control
300 */
301 resource_size_t scu_bar_size = pci_resource_len(pdev, SCI_SCU_BAR*2);
302 resource_size_t smu_bar_size = pci_resource_len(pdev, SCI_SMU_BAR*2);
303
304 if (scu_bar_size >= SCI_SCU_BAR_SIZE*SCI_MAX_CONTROLLERS &&
305 smu_bar_size >= SCI_SMU_BAR_SIZE*SCI_MAX_CONTROLLERS)
306 return SCI_MAX_CONTROLLERS;
307 else
308 return 1;
309}
310
311static int isci_setup_interrupts(struct pci_dev *pdev)
312{
313 int err, i, num_msix;
314 struct isci_pci_info *pci_info = to_pci_info(pdev);
315
316 /*
317 * Determine the number of vectors associated with this
318 * PCI function.
319 */
320 num_msix = num_controllers(pdev) * SCI_NUM_MSI_X_INT;
321
322 for (i = 0; i < num_msix; i++)
323 pci_info->msix_entries[i].entry = i;
324
325 err = pci_enable_msix(pdev, pci_info->msix_entries, num_msix);
326 if (err)
327 goto intx;
328
329 for (i = 0; i < num_msix; i++) {
330 int id = i / SCI_NUM_MSI_X_INT;
331 struct msix_entry *msix = &pci_info->msix_entries[i];
332 struct isci_host *isci_host = isci_host_by_id(pdev, id);
Dan Williams92f4f0f2011-02-18 09:25:11 -0800333 irq_handler_t isr;
334
335 /* odd numbered vectors are error interrupts */
336 if (i & 1)
337 isr = isci_error_isr;
338 else
339 isr = isci_msix_isr;
Dan Williams6f231dd2011-07-02 22:56:22 -0700340
341 BUG_ON(!isci_host);
342
Dan Williams92f4f0f2011-02-18 09:25:11 -0800343 err = devm_request_irq(&pdev->dev, msix->vector, isr, 0,
Dan Williams6f231dd2011-07-02 22:56:22 -0700344 DRV_NAME"-msix", isci_host);
345 if (!err)
346 continue;
347
348 dev_info(&pdev->dev, "msix setup failed falling back to intx\n");
349 while (i--) {
350 id = i / SCI_NUM_MSI_X_INT;
351 isci_host = isci_host_by_id(pdev, id);
352 msix = &pci_info->msix_entries[i];
353 devm_free_irq(&pdev->dev, msix->vector, isci_host);
354 }
355 pci_disable_msix(pdev);
356 goto intx;
357 }
358
359 return 0;
360
361 intx:
Dan Williamsc7ef4032011-02-18 09:25:05 -0800362 err = devm_request_irq(&pdev->dev, pdev->irq, isci_intx_isr,
Dan Williams6f231dd2011-07-02 22:56:22 -0700363 IRQF_SHARED, DRV_NAME"-intx", pdev);
364
365 return err;
366}
367
368/**
369 * isci_parse_oem_parameters() - This method will take OEM parameters
370 * from the module init parameters and copy them to oem_params. This will
371 * only copy values that are not set to the module parameter default values
372 * @oem_parameters: This parameter specifies the controller default OEM
373 * parameters. It is expected that this has been initialized to the default
374 * parameters for the controller
375 *
376 *
377 */
378enum sci_status isci_parse_oem_parameters(union scic_oem_parameters *oem_params,
379 int scu_index,
380 struct isci_firmware *fw)
381{
382 int i;
383
384 /* check for valid inputs */
385 if (!(scu_index >= 0
386 && scu_index < SCI_MAX_CONTROLLERS
387 && oem_params != NULL)) {
388 return SCI_FAILURE;
389 }
390
391 for (i = 0; i < SCI_MAX_PHYS; i++) {
392 int array_idx = i + (SCI_MAX_PHYS * scu_index);
393 u64 sas_addr = fw->sas_addrs[array_idx];
394
395 if (sas_addr != 0) {
396 oem_params->sds1.phys[i].sas_address.low =
397 (u32)(sas_addr & 0xffffffff);
398 oem_params->sds1.phys[i].sas_address.high =
399 (u32)((sas_addr >> 32) & 0xffffffff);
400 }
401 }
402
403 for (i = 0; i < SCI_MAX_PORTS; i++) {
404 int array_idx = i + (SCI_MAX_PORTS * scu_index);
405 u32 pmask = fw->phy_masks[array_idx];
406
407 oem_params->sds1.ports[i].phy_mask = pmask;
408 }
409
410 return SCI_SUCCESS;
411}
412
413/**
414 * isci_parse_user_parameters() - This method will take user parameters
415 * from the module init parameters and copy them to user_params. This will
416 * only copy values that are not set to the module parameter default values
417 * @user_parameters: This parameter specifies the controller default user
418 * parameters. It is expected that this has been initialized to the default
419 * parameters for the controller
420 *
421 *
422 */
423enum sci_status isci_parse_user_parameters(
424 union scic_user_parameters *user_params,
425 int scu_index,
426 struct isci_firmware *fw)
427{
428 int i;
429
430 if (!(scu_index >= 0
431 && scu_index < SCI_MAX_CONTROLLERS
432 && user_params != NULL)) {
433 return SCI_FAILURE;
434 }
435
436 for (i = 0; i < SCI_MAX_PORTS; i++) {
437 int array_idx = i + (SCI_MAX_PORTS * scu_index);
438 u32 gen = fw->phy_gens[array_idx];
439
440 user_params->sds1.phys[i].max_speed_generation = gen;
441
442 }
443
444 return SCI_SUCCESS;
445}
446
447static struct isci_host *isci_host_alloc(struct pci_dev *pdev, int id)
448{
449 struct isci_host *isci_host;
450 struct Scsi_Host *shost;
451 int err;
452
453 isci_host = devm_kzalloc(&pdev->dev, sizeof(*isci_host), GFP_KERNEL);
454 if (!isci_host)
455 return NULL;
456
457 isci_host->pdev = pdev;
458 isci_host->id = id;
459
460 shost = scsi_host_alloc(&isci_sht, sizeof(void *));
461 if (!shost)
462 return NULL;
463 isci_host->shost = shost;
464
465 err = isci_host_init(isci_host);
466 if (err)
467 goto err_shost;
468
469 SHOST_TO_SAS_HA(shost) = &isci_host->sas_ha;
470 isci_host->sas_ha.core.shost = shost;
471 shost->transportt = isci_transport_template;
472
473 shost->max_id = ~0;
474 shost->max_lun = ~0;
475 shost->max_cmd_len = MAX_COMMAND_SIZE;
476
477 err = scsi_add_host(shost, &pdev->dev);
478 if (err)
479 goto err_shost;
480
481 err = isci_register_sas_ha(isci_host);
482 if (err)
483 goto err_shost_remove;
484
485 return isci_host;
486
487 err_shost_remove:
488 scsi_remove_host(shost);
489 err_shost:
490 scsi_host_put(shost);
491
492 return NULL;
493}
494
495static void check_si_rev(struct pci_dev *pdev)
496{
497 if (num_controllers(pdev) > 1)
498 isci_si_rev = ISCI_SI_REVB0;
499 else {
500 switch (pdev->revision) {
501 case 0:
502 case 1:
503 /* if the id is ambiguous don't update isci_si_rev */
504 break;
505 case 3:
506 isci_si_rev = ISCI_SI_REVA2;
507 break;
508 default:
509 case 4:
510 isci_si_rev = ISCI_SI_REVB0;
511 break;
512 }
513 }
514
515 dev_info(&pdev->dev, "driver configured for %s silicon (rev: %d)\n",
516 isci_si_rev == ISCI_SI_REVA0 ? "A0" :
517 isci_si_rev == ISCI_SI_REVA2 ? "A2" : "B0", pdev->revision);
518
519}
520
521static int __devinit isci_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
522{
523 struct isci_pci_info *pci_info;
524 int err, i;
525 struct isci_host *isci_host;
526
527 check_si_rev(pdev);
528
529 pci_info = devm_kzalloc(&pdev->dev, sizeof(*pci_info), GFP_KERNEL);
530 if (!pci_info)
531 return -ENOMEM;
532 pci_set_drvdata(pdev, pci_info);
533
534 err = isci_pci_init(pdev);
535 if (err)
536 return err;
537
538 for (i = 0; i < num_controllers(pdev); i++) {
539 struct isci_host *h = isci_host_alloc(pdev, i);
540
541 if (!h) {
542 err = -ENOMEM;
543 goto err_host_alloc;
544 }
545
546 h->next = pci_info->hosts;
547 pci_info->hosts = h;
548 }
549
550 err = isci_setup_interrupts(pdev);
551 if (err)
552 goto err_host_alloc;
553
554 for_each_isci_host(isci_host, pdev)
555 scsi_scan_host(isci_host->shost);
556
557 return 0;
558
559 err_host_alloc:
560 for_each_isci_host(isci_host, pdev)
561 isci_unregister_sas_ha(isci_host);
562 return err;
563}
564
565static void __devexit isci_pci_remove(struct pci_dev *pdev)
566{
567 struct isci_host *isci_host;
568
569 for_each_isci_host(isci_host, pdev) {
570 isci_unregister_sas_ha(isci_host);
571 isci_host_deinit(isci_host);
572 scic_controller_disable_interrupts(isci_host->core_controller);
573 }
574}
575
576static __init int isci_init(void)
577{
578 int err = -ENOMEM;
579
580 pr_info("%s: Intel(R) C600 SAS Controller Driver\n", DRV_NAME);
581
582 isci_kmem_cache = kmem_cache_create(DRV_NAME,
583 sizeof(struct isci_remote_device) +
584 scic_remote_device_get_object_size(),
585 0, 0, NULL);
586 if (!isci_kmem_cache)
587 return err;
588
589 isci_transport_template = sas_domain_attach_transport(&isci_transport_ops);
590 if (!isci_transport_template)
591 goto err_kmem;
592
593 err = pci_register_driver(&isci_pci_driver);
594 if (err)
595 goto err_sas;
596
597 return 0;
598
599 err_sas:
600 sas_release_transport(isci_transport_template);
601 err_kmem:
602 kmem_cache_destroy(isci_kmem_cache);
603
604 return err;
605}
606
607static __exit void isci_exit(void)
608{
609 pci_unregister_driver(&isci_pci_driver);
610 sas_release_transport(isci_transport_template);
611 kmem_cache_destroy(isci_kmem_cache);
612}
613
614MODULE_LICENSE("Dual BSD/GPL");
615MODULE_FIRMWARE(ISCI_FW_NAME);
616module_init(isci_init);
617module_exit(isci_exit);