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