Merge branch 'v4l_for_linus' of git://linuxtv.org/mchehab/for_linus
[pandora-kernel.git] / drivers / scsi / isci / init.c
1 /*
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 <linux/firmware.h>
60 #include <linux/efi.h>
61 #include <asm/string.h>
62 #include <scsi/scsi_host.h>
63 #include "isci.h"
64 #include "task.h"
65 #include "probe_roms.h"
66
67 #define MAJ 1
68 #define MIN 0
69 #define BUILD 0
70 #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
71         __stringify(BUILD)
72
73 MODULE_VERSION(DRV_VERSION);
74
75 static struct scsi_transport_template *isci_transport_template;
76
77 static DEFINE_PCI_DEVICE_TABLE(isci_id_table) = {
78         { PCI_VDEVICE(INTEL, 0x1D61),},
79         { PCI_VDEVICE(INTEL, 0x1D63),},
80         { PCI_VDEVICE(INTEL, 0x1D65),},
81         { PCI_VDEVICE(INTEL, 0x1D67),},
82         { PCI_VDEVICE(INTEL, 0x1D69),},
83         { PCI_VDEVICE(INTEL, 0x1D6B),},
84         { PCI_VDEVICE(INTEL, 0x1D60),},
85         { PCI_VDEVICE(INTEL, 0x1D62),},
86         { PCI_VDEVICE(INTEL, 0x1D64),},
87         { PCI_VDEVICE(INTEL, 0x1D66),},
88         { PCI_VDEVICE(INTEL, 0x1D68),},
89         { PCI_VDEVICE(INTEL, 0x1D6A),},
90         {}
91 };
92
93 MODULE_DEVICE_TABLE(pci, isci_id_table);
94
95 /* linux isci specific settings */
96
97 unsigned char no_outbound_task_to = 20;
98 module_param(no_outbound_task_to, byte, 0);
99 MODULE_PARM_DESC(no_outbound_task_to, "No Outbound Task Timeout (1us incr)");
100
101 u16 ssp_max_occ_to = 20;
102 module_param(ssp_max_occ_to, ushort, 0);
103 MODULE_PARM_DESC(ssp_max_occ_to, "SSP Max occupancy timeout (100us incr)");
104
105 u16 stp_max_occ_to = 5;
106 module_param(stp_max_occ_to, ushort, 0);
107 MODULE_PARM_DESC(stp_max_occ_to, "STP Max occupancy timeout (100us incr)");
108
109 u16 ssp_inactive_to = 5;
110 module_param(ssp_inactive_to, ushort, 0);
111 MODULE_PARM_DESC(ssp_inactive_to, "SSP inactivity timeout (100us incr)");
112
113 u16 stp_inactive_to = 5;
114 module_param(stp_inactive_to, ushort, 0);
115 MODULE_PARM_DESC(stp_inactive_to, "STP inactivity timeout (100us incr)");
116
117 unsigned char phy_gen = 3;
118 module_param(phy_gen, byte, 0);
119 MODULE_PARM_DESC(phy_gen, "PHY generation (1: 1.5Gbps 2: 3.0Gbps 3: 6.0Gbps)");
120
121 unsigned char max_concurr_spinup = 1;
122 module_param(max_concurr_spinup, byte, 0);
123 MODULE_PARM_DESC(max_concurr_spinup, "Max concurrent device spinup");
124
125 static ssize_t isci_show_id(struct device *dev, struct device_attribute *attr, char *buf)
126 {
127         struct Scsi_Host *shost = container_of(dev, typeof(*shost), shost_dev);
128         struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
129         struct isci_host *ihost = container_of(sas_ha, typeof(*ihost), sas_ha);
130
131         return snprintf(buf, PAGE_SIZE, "%d\n", ihost->id);
132 }
133
134 static DEVICE_ATTR(isci_id, S_IRUGO, isci_show_id, NULL);
135
136 struct device_attribute *isci_host_attrs[] = {
137         &dev_attr_isci_id,
138         NULL
139 };
140
141 static struct scsi_host_template isci_sht = {
142
143         .module                         = THIS_MODULE,
144         .name                           = DRV_NAME,
145         .proc_name                      = DRV_NAME,
146         .queuecommand                   = sas_queuecommand,
147         .target_alloc                   = sas_target_alloc,
148         .slave_configure                = sas_slave_configure,
149         .slave_destroy                  = sas_slave_destroy,
150         .scan_finished                  = isci_host_scan_finished,
151         .scan_start                     = isci_host_scan_start,
152         .change_queue_depth             = sas_change_queue_depth,
153         .change_queue_type              = sas_change_queue_type,
154         .bios_param                     = sas_bios_param,
155         .can_queue                      = ISCI_CAN_QUEUE_VAL,
156         .cmd_per_lun                    = 1,
157         .this_id                        = -1,
158         .sg_tablesize                   = SG_ALL,
159         .max_sectors                    = SCSI_DEFAULT_MAX_SECTORS,
160         .use_clustering                 = ENABLE_CLUSTERING,
161         .eh_device_reset_handler        = sas_eh_device_reset_handler,
162         .eh_bus_reset_handler           = isci_bus_reset_handler,
163         .slave_alloc                    = sas_slave_alloc,
164         .target_destroy                 = sas_target_destroy,
165         .ioctl                          = sas_ioctl,
166         .shost_attrs                    = isci_host_attrs,
167 };
168
169 static struct sas_domain_function_template isci_transport_ops  = {
170
171         /* The class calls these to notify the LLDD of an event. */
172         .lldd_port_formed       = isci_port_formed,
173         .lldd_port_deformed     = isci_port_deformed,
174
175         /* The class calls these when a device is found or gone. */
176         .lldd_dev_found         = isci_remote_device_found,
177         .lldd_dev_gone          = isci_remote_device_gone,
178
179         .lldd_execute_task      = isci_task_execute_task,
180         /* Task Management Functions. Must be called from process context. */
181         .lldd_abort_task        = isci_task_abort_task,
182         .lldd_abort_task_set    = isci_task_abort_task_set,
183         .lldd_clear_aca         = isci_task_clear_aca,
184         .lldd_clear_task_set    = isci_task_clear_task_set,
185         .lldd_I_T_nexus_reset   = isci_task_I_T_nexus_reset,
186         .lldd_lu_reset          = isci_task_lu_reset,
187         .lldd_query_task        = isci_task_query_task,
188
189         /* Port and Adapter management */
190         .lldd_clear_nexus_port  = isci_task_clear_nexus_port,
191         .lldd_clear_nexus_ha    = isci_task_clear_nexus_ha,
192
193         /* Phy management */
194         .lldd_control_phy       = isci_phy_control,
195 };
196
197
198 /******************************************************************************
199 * P R O T E C T E D  M E T H O D S
200 ******************************************************************************/
201
202
203
204 /**
205  * isci_register_sas_ha() - This method initializes various lldd
206  *    specific members of the sas_ha struct and calls the libsas
207  *    sas_register_ha() function.
208  * @isci_host: This parameter specifies the lldd specific wrapper for the
209  *    libsas sas_ha struct.
210  *
211  * This method returns an error code indicating sucess or failure. The user
212  * should check for possible memory allocation error return otherwise, a zero
213  * indicates success.
214  */
215 static int isci_register_sas_ha(struct isci_host *isci_host)
216 {
217         int i;
218         struct sas_ha_struct *sas_ha = &(isci_host->sas_ha);
219         struct asd_sas_phy **sas_phys;
220         struct asd_sas_port **sas_ports;
221
222         sas_phys = devm_kzalloc(&isci_host->pdev->dev,
223                                 SCI_MAX_PHYS * sizeof(void *),
224                                 GFP_KERNEL);
225         if (!sas_phys)
226                 return -ENOMEM;
227
228         sas_ports = devm_kzalloc(&isci_host->pdev->dev,
229                                  SCI_MAX_PORTS * sizeof(void *),
230                                  GFP_KERNEL);
231         if (!sas_ports)
232                 return -ENOMEM;
233
234         /*----------------- Libsas Initialization Stuff----------------------
235          * Set various fields in the sas_ha struct:
236          */
237
238         sas_ha->sas_ha_name = DRV_NAME;
239         sas_ha->lldd_module = THIS_MODULE;
240         sas_ha->sas_addr    = &isci_host->phys[0].sas_addr[0];
241
242         /* set the array of phy and port structs.  */
243         for (i = 0; i < SCI_MAX_PHYS; i++) {
244                 sas_phys[i] = &isci_host->phys[i].sas_phy;
245                 sas_ports[i] = &isci_host->ports[i].sas_port;
246         }
247
248         sas_ha->sas_phy  = sas_phys;
249         sas_ha->sas_port = sas_ports;
250         sas_ha->num_phys = SCI_MAX_PHYS;
251
252         sas_ha->lldd_queue_size = ISCI_CAN_QUEUE_VAL;
253         sas_ha->lldd_max_execute_num = 1;
254         sas_ha->strict_wide_ports = 1;
255
256         sas_register_ha(sas_ha);
257
258         return 0;
259 }
260
261 static void isci_unregister(struct isci_host *isci_host)
262 {
263         struct Scsi_Host *shost;
264
265         if (!isci_host)
266                 return;
267
268         shost = isci_host->shost;
269
270         sas_unregister_ha(&isci_host->sas_ha);
271
272         sas_remove_host(isci_host->shost);
273         scsi_remove_host(isci_host->shost);
274         scsi_host_put(isci_host->shost);
275 }
276
277 static int __devinit isci_pci_init(struct pci_dev *pdev)
278 {
279         int err, bar_num, bar_mask = 0;
280         void __iomem * const *iomap;
281
282         err = pcim_enable_device(pdev);
283         if (err) {
284                 dev_err(&pdev->dev,
285                         "failed enable PCI device %s!\n",
286                         pci_name(pdev));
287                 return err;
288         }
289
290         for (bar_num = 0; bar_num < SCI_PCI_BAR_COUNT; bar_num++)
291                 bar_mask |= 1 << (bar_num * 2);
292
293         err = pcim_iomap_regions(pdev, bar_mask, DRV_NAME);
294         if (err)
295                 return err;
296
297         iomap = pcim_iomap_table(pdev);
298         if (!iomap)
299                 return -ENOMEM;
300
301         pci_set_master(pdev);
302
303         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
304         if (err) {
305                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
306                 if (err)
307                         return err;
308         }
309
310         err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
311         if (err) {
312                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
313                 if (err)
314                         return err;
315         }
316
317         return 0;
318 }
319
320 static int num_controllers(struct pci_dev *pdev)
321 {
322         /* bar size alone can tell us if we are running with a dual controller
323          * part, no need to trust revision ids that might be under broken firmware
324          * control
325          */
326         resource_size_t scu_bar_size = pci_resource_len(pdev, SCI_SCU_BAR*2);
327         resource_size_t smu_bar_size = pci_resource_len(pdev, SCI_SMU_BAR*2);
328
329         if (scu_bar_size >= SCI_SCU_BAR_SIZE*SCI_MAX_CONTROLLERS &&
330             smu_bar_size >= SCI_SMU_BAR_SIZE*SCI_MAX_CONTROLLERS)
331                 return SCI_MAX_CONTROLLERS;
332         else
333                 return 1;
334 }
335
336 static int isci_setup_interrupts(struct pci_dev *pdev)
337 {
338         int err, i, num_msix;
339         struct isci_host *ihost;
340         struct isci_pci_info *pci_info = to_pci_info(pdev);
341
342         /*
343          *  Determine the number of vectors associated with this
344          *  PCI function.
345          */
346         num_msix = num_controllers(pdev) * SCI_NUM_MSI_X_INT;
347
348         for (i = 0; i < num_msix; i++)
349                 pci_info->msix_entries[i].entry = i;
350
351         err = pci_enable_msix(pdev, pci_info->msix_entries, num_msix);
352         if (err)
353                 goto intx;
354
355         for (i = 0; i < num_msix; i++) {
356                 int id = i / SCI_NUM_MSI_X_INT;
357                 struct msix_entry *msix = &pci_info->msix_entries[i];
358                 irq_handler_t isr;
359
360                 ihost = pci_info->hosts[id];
361                 /* odd numbered vectors are error interrupts */
362                 if (i & 1)
363                         isr = isci_error_isr;
364                 else
365                         isr = isci_msix_isr;
366
367                 err = devm_request_irq(&pdev->dev, msix->vector, isr, 0,
368                                        DRV_NAME"-msix", ihost);
369                 if (!err)
370                         continue;
371
372                 dev_info(&pdev->dev, "msix setup failed falling back to intx\n");
373                 while (i--) {
374                         id = i / SCI_NUM_MSI_X_INT;
375                         ihost = pci_info->hosts[id];
376                         msix = &pci_info->msix_entries[i];
377                         devm_free_irq(&pdev->dev, msix->vector, ihost);
378                 }
379                 pci_disable_msix(pdev);
380                 goto intx;
381         }
382         return 0;
383
384  intx:
385         for_each_isci_host(i, ihost, pdev) {
386                 err = devm_request_irq(&pdev->dev, pdev->irq, isci_intx_isr,
387                                        IRQF_SHARED, DRV_NAME"-intx", ihost);
388                 if (err)
389                         break;
390         }
391         return err;
392 }
393
394 static struct isci_host *isci_host_alloc(struct pci_dev *pdev, int id)
395 {
396         struct isci_host *isci_host;
397         struct Scsi_Host *shost;
398         int err;
399
400         isci_host = devm_kzalloc(&pdev->dev, sizeof(*isci_host), GFP_KERNEL);
401         if (!isci_host)
402                 return NULL;
403
404         isci_host->pdev = pdev;
405         isci_host->id = id;
406
407         shost = scsi_host_alloc(&isci_sht, sizeof(void *));
408         if (!shost)
409                 return NULL;
410         isci_host->shost = shost;
411
412         err = isci_host_init(isci_host);
413         if (err)
414                 goto err_shost;
415
416         SHOST_TO_SAS_HA(shost) = &isci_host->sas_ha;
417         isci_host->sas_ha.core.shost = shost;
418         shost->transportt = isci_transport_template;
419
420         shost->max_id = ~0;
421         shost->max_lun = ~0;
422         shost->max_cmd_len = MAX_COMMAND_SIZE;
423
424         err = scsi_add_host(shost, &pdev->dev);
425         if (err)
426                 goto err_shost;
427
428         err = isci_register_sas_ha(isci_host);
429         if (err)
430                 goto err_shost_remove;
431
432         return isci_host;
433
434  err_shost_remove:
435         scsi_remove_host(shost);
436  err_shost:
437         scsi_host_put(shost);
438
439         return NULL;
440 }
441
442 static int __devinit isci_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
443 {
444         struct isci_pci_info *pci_info;
445         int err, i;
446         struct isci_host *isci_host;
447         const struct firmware *fw = NULL;
448         struct isci_orom *orom = NULL;
449         char *source = "(platform)";
450
451         dev_info(&pdev->dev, "driver configured for rev: %d silicon\n",
452                  pdev->revision);
453
454         pci_info = devm_kzalloc(&pdev->dev, sizeof(*pci_info), GFP_KERNEL);
455         if (!pci_info)
456                 return -ENOMEM;
457         pci_set_drvdata(pdev, pci_info);
458
459         if (efi_enabled)
460                 orom = isci_get_efi_var(pdev);
461
462         if (!orom)
463                 orom = isci_request_oprom(pdev);
464
465         for (i = 0; orom && i < ARRAY_SIZE(orom->ctrl); i++) {
466                 if (sci_oem_parameters_validate(&orom->ctrl[i])) {
467                         dev_warn(&pdev->dev,
468                                  "[%d]: invalid oem parameters detected, falling back to firmware\n", i);
469                         devm_kfree(&pdev->dev, orom);
470                         orom = NULL;
471                         break;
472                 }
473         }
474
475         if (!orom) {
476                 source = "(firmware)";
477                 orom = isci_request_firmware(pdev, fw);
478                 if (!orom) {
479                         /* TODO convert this to WARN_TAINT_ONCE once the
480                          * orom/efi parameter support is widely available
481                          */
482                         dev_warn(&pdev->dev,
483                                  "Loading user firmware failed, using default "
484                                  "values\n");
485                         dev_warn(&pdev->dev,
486                                  "Default OEM configuration being used: 4 "
487                                  "narrow ports, and default SAS Addresses\n");
488                 }
489         }
490
491         if (orom)
492                 dev_info(&pdev->dev,
493                          "OEM SAS parameters (version: %u.%u) loaded %s\n",
494                          (orom->hdr.version & 0xf0) >> 4,
495                          (orom->hdr.version & 0xf), source);
496
497         pci_info->orom = orom;
498
499         err = isci_pci_init(pdev);
500         if (err)
501                 return err;
502
503         for (i = 0; i < num_controllers(pdev); i++) {
504                 struct isci_host *h = isci_host_alloc(pdev, i);
505
506                 if (!h) {
507                         err = -ENOMEM;
508                         goto err_host_alloc;
509                 }
510                 pci_info->hosts[i] = h;
511         }
512
513         err = isci_setup_interrupts(pdev);
514         if (err)
515                 goto err_host_alloc;
516
517         for_each_isci_host(i, isci_host, pdev)
518                 scsi_scan_host(isci_host->shost);
519
520         return 0;
521
522  err_host_alloc:
523         for_each_isci_host(i, isci_host, pdev)
524                 isci_unregister(isci_host);
525         return err;
526 }
527
528 static void __devexit isci_pci_remove(struct pci_dev *pdev)
529 {
530         struct isci_host *ihost;
531         int i;
532
533         for_each_isci_host(i, ihost, pdev) {
534                 isci_unregister(ihost);
535                 isci_host_deinit(ihost);
536                 sci_controller_disable_interrupts(ihost);
537         }
538 }
539
540 static struct pci_driver isci_pci_driver = {
541         .name           = DRV_NAME,
542         .id_table       = isci_id_table,
543         .probe          = isci_pci_probe,
544         .remove         = __devexit_p(isci_pci_remove),
545 };
546
547 static __init int isci_init(void)
548 {
549         int err;
550
551         pr_info("%s: Intel(R) C600 SAS Controller Driver - version %s\n",
552                 DRV_NAME, DRV_VERSION);
553
554         isci_transport_template = sas_domain_attach_transport(&isci_transport_ops);
555         if (!isci_transport_template)
556                 return -ENOMEM;
557
558         err = pci_register_driver(&isci_pci_driver);
559         if (err)
560                 sas_release_transport(isci_transport_template);
561
562         return err;
563 }
564
565 static __exit void isci_exit(void)
566 {
567         pci_unregister_driver(&isci_pci_driver);
568         sas_release_transport(isci_transport_template);
569 }
570
571 MODULE_LICENSE("Dual BSD/GPL");
572 MODULE_FIRMWARE(ISCI_FW_NAME);
573 module_init(isci_init);
574 module_exit(isci_exit);