1dbf9bfe6Sjack wang /*
2e5742101SSakthivel K  * PMC-Sierra PM8001/8081/8088/8089 SAS/SATA based host adapters driver
3dbf9bfe6Sjack wang  *
4dbf9bfe6Sjack wang  * Copyright (c) 2008-2009 USI Co., Ltd.
5dbf9bfe6Sjack wang  * All rights reserved.
6dbf9bfe6Sjack wang  *
7dbf9bfe6Sjack wang  * Redistribution and use in source and binary forms, with or without
8dbf9bfe6Sjack wang  * modification, are permitted provided that the following conditions
9dbf9bfe6Sjack wang  * are met:
10dbf9bfe6Sjack wang  * 1. Redistributions of source code must retain the above copyright
11dbf9bfe6Sjack wang  *    notice, this list of conditions, and the following disclaimer,
12dbf9bfe6Sjack wang  *    without modification.
13dbf9bfe6Sjack wang  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14dbf9bfe6Sjack wang  *    substantially similar to the "NO WARRANTY" disclaimer below
15dbf9bfe6Sjack wang  *    ("Disclaimer") and any redistribution must be conditioned upon
16dbf9bfe6Sjack wang  *    including a substantially similar Disclaimer requirement for further
17dbf9bfe6Sjack wang  *    binary redistribution.
18dbf9bfe6Sjack wang  * 3. Neither the names of the above-listed copyright holders nor the names
19dbf9bfe6Sjack wang  *    of any contributors may be used to endorse or promote products derived
20dbf9bfe6Sjack wang  *    from this software without specific prior written permission.
21dbf9bfe6Sjack wang  *
22dbf9bfe6Sjack wang  * Alternatively, this software may be distributed under the terms of the
23dbf9bfe6Sjack wang  * GNU General Public License ("GPL") version 2 as published by the Free
24dbf9bfe6Sjack wang  * Software Foundation.
25dbf9bfe6Sjack wang  *
26dbf9bfe6Sjack wang  * NO WARRANTY
27dbf9bfe6Sjack wang  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28dbf9bfe6Sjack wang  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29dbf9bfe6Sjack wang  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30dbf9bfe6Sjack wang  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31dbf9bfe6Sjack wang  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32dbf9bfe6Sjack wang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33dbf9bfe6Sjack wang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34dbf9bfe6Sjack wang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35dbf9bfe6Sjack wang  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36dbf9bfe6Sjack wang  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37dbf9bfe6Sjack wang  * POSSIBILITY OF SUCH DAMAGES.
38dbf9bfe6Sjack wang  *
39dbf9bfe6Sjack wang  */
40dbf9bfe6Sjack wang 
415a0e3ad6STejun Heo #include <linux/slab.h>
42dbf9bfe6Sjack wang #include "pm8001_sas.h"
43dbf9bfe6Sjack wang #include "pm8001_chips.h"
443e253d96Speter chang #include "pm80xx_hwi.h"
45dbf9bfe6Sjack wang 
467370672dSpeter chang static ulong logging_level = PM8001_FAIL_LOGGING | PM8001_IOERR_LOGGING;
477370672dSpeter chang module_param(logging_level, ulong, 0644);
487370672dSpeter chang MODULE_PARM_DESC(logging_level, " bits for enabling logging info.");
497370672dSpeter chang 
503e253d96Speter chang static ulong link_rate = LINKRATE_15 | LINKRATE_30 | LINKRATE_60 | LINKRATE_120;
513e253d96Speter chang module_param(link_rate, ulong, 0644);
523e253d96Speter chang MODULE_PARM_DESC(link_rate, "Enable link rate.\n"
533e253d96Speter chang 		" 1: Link rate 1.5G\n"
543e253d96Speter chang 		" 2: Link rate 3.0G\n"
553e253d96Speter chang 		" 4: Link rate 6.0G\n"
563e253d96Speter chang 		" 8: Link rate 12.0G\n");
573e253d96Speter chang 
58dbf9bfe6Sjack wang static struct scsi_transport_template *pm8001_stt;
595a141315SViswas G static int pm8001_init_ccb_tag(struct pm8001_hba_info *, struct Scsi_Host *, struct pci_dev *);
60dbf9bfe6Sjack wang 
61e802fc43SLee Jones /*
62e5742101SSakthivel K  * chip info structure to identify chip key functionality as
63e5742101SSakthivel K  * encryption available/not, no of ports, hw specific function ref
64e5742101SSakthivel K  */
65dbf9bfe6Sjack wang static const struct pm8001_chip_info pm8001_chips[] = {
66e5742101SSakthivel K 	[chip_8001] = {0,  8, &pm8001_8001_dispatch,},
67f5860992SSakthivel K 	[chip_8008] = {0,  8, &pm8001_80xx_dispatch,},
68f5860992SSakthivel K 	[chip_8009] = {1,  8, &pm8001_80xx_dispatch,},
69f5860992SSakthivel K 	[chip_8018] = {0,  16, &pm8001_80xx_dispatch,},
70f5860992SSakthivel K 	[chip_8019] = {1,  16, &pm8001_80xx_dispatch,},
71a9a923e5SAnand Kumar Santhanam 	[chip_8074] = {0,  8, &pm8001_80xx_dispatch,},
72a9a923e5SAnand Kumar Santhanam 	[chip_8076] = {0,  16, &pm8001_80xx_dispatch,},
73a9a923e5SAnand Kumar Santhanam 	[chip_8077] = {0,  16, &pm8001_80xx_dispatch,},
74d8571b1eSSuresh Thiagarajan 	[chip_8006] = {0,  16, &pm8001_80xx_dispatch,},
75db9d4034SBenjamin Rood 	[chip_8070] = {0,  8, &pm8001_80xx_dispatch,},
76db9d4034SBenjamin Rood 	[chip_8072] = {0,  16, &pm8001_80xx_dispatch,},
77dbf9bfe6Sjack wang };
78dbf9bfe6Sjack wang static int pm8001_id;
79dbf9bfe6Sjack wang 
80dbf9bfe6Sjack wang LIST_HEAD(hba_list);
81dbf9bfe6Sjack wang 
82429305e4STejun Heo struct workqueue_struct *pm8001_wq;
83429305e4STejun Heo 
84e802fc43SLee Jones /*
85dbf9bfe6Sjack wang  * The main structure which LLDD must register for scsi core.
86dbf9bfe6Sjack wang  */
87dbf9bfe6Sjack wang static struct scsi_host_template pm8001_sht = {
88dbf9bfe6Sjack wang 	.module			= THIS_MODULE,
89dbf9bfe6Sjack wang 	.name			= DRV_NAME,
90dbf9bfe6Sjack wang 	.queuecommand		= sas_queuecommand,
91b8f1d1e0SChristoph Hellwig 	.dma_need_drain		= ata_scsi_dma_need_drain,
92dbf9bfe6Sjack wang 	.target_alloc		= sas_target_alloc,
9311e16364SDan Williams 	.slave_configure	= sas_slave_configure,
94dbf9bfe6Sjack wang 	.scan_finished		= pm8001_scan_finished,
95dbf9bfe6Sjack wang 	.scan_start		= pm8001_scan_start,
96dbf9bfe6Sjack wang 	.change_queue_depth	= sas_change_queue_depth,
97dbf9bfe6Sjack wang 	.bios_param		= sas_bios_param,
98dbf9bfe6Sjack wang 	.can_queue		= 1,
99dbf9bfe6Sjack wang 	.this_id		= -1,
10058bf14c1SPeter Chang 	.sg_tablesize		= PM8001_MAX_DMA_SG,
101dbf9bfe6Sjack wang 	.max_sectors		= SCSI_DEFAULT_MAX_SECTORS,
102dbf9bfe6Sjack wang 	.eh_device_reset_handler = sas_eh_device_reset_handler,
103cc199e78SHannes Reinecke 	.eh_target_reset_handler = sas_eh_target_reset_handler,
104dbf9bfe6Sjack wang 	.target_destroy		= sas_target_destroy,
105dbf9bfe6Sjack wang 	.ioctl			= sas_ioctl,
10675c0b0e1SArnd Bergmann #ifdef CONFIG_COMPAT
10775c0b0e1SArnd Bergmann 	.compat_ioctl		= sas_ioctl,
10875c0b0e1SArnd Bergmann #endif
109dbf9bfe6Sjack wang 	.shost_attrs		= pm8001_host_attrs,
110c40ecc12SChristoph Hellwig 	.track_queue_depth	= 1,
111dbf9bfe6Sjack wang };
112dbf9bfe6Sjack wang 
113e802fc43SLee Jones /*
114dbf9bfe6Sjack wang  * Sas layer call this function to execute specific task.
115dbf9bfe6Sjack wang  */
116dbf9bfe6Sjack wang static struct sas_domain_function_template pm8001_transport_ops = {
117dbf9bfe6Sjack wang 	.lldd_dev_found		= pm8001_dev_found,
118dbf9bfe6Sjack wang 	.lldd_dev_gone		= pm8001_dev_gone,
119dbf9bfe6Sjack wang 
120dbf9bfe6Sjack wang 	.lldd_execute_task	= pm8001_queue_command,
121dbf9bfe6Sjack wang 	.lldd_control_phy	= pm8001_phy_control,
122dbf9bfe6Sjack wang 
123dbf9bfe6Sjack wang 	.lldd_abort_task	= pm8001_abort_task,
124dbf9bfe6Sjack wang 	.lldd_abort_task_set	= pm8001_abort_task_set,
125dbf9bfe6Sjack wang 	.lldd_clear_aca		= pm8001_clear_aca,
126dbf9bfe6Sjack wang 	.lldd_clear_task_set	= pm8001_clear_task_set,
127dbf9bfe6Sjack wang 	.lldd_I_T_nexus_reset   = pm8001_I_T_nexus_reset,
128dbf9bfe6Sjack wang 	.lldd_lu_reset		= pm8001_lu_reset,
129dbf9bfe6Sjack wang 	.lldd_query_task	= pm8001_query_task,
130dbf9bfe6Sjack wang };
131dbf9bfe6Sjack wang 
132dbf9bfe6Sjack wang /**
133dbf9bfe6Sjack wang  * pm8001_phy_init - initiate our adapter phys
134dbf9bfe6Sjack wang  * @pm8001_ha: our hba structure.
135dbf9bfe6Sjack wang  * @phy_id: phy id.
136dbf9bfe6Sjack wang  */
1376f039790SGreg Kroah-Hartman static void pm8001_phy_init(struct pm8001_hba_info *pm8001_ha, int phy_id)
138dbf9bfe6Sjack wang {
139dbf9bfe6Sjack wang 	struct pm8001_phy *phy = &pm8001_ha->phy[phy_id];
140dbf9bfe6Sjack wang 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
141cd135754SDeepak Ukey 	phy->phy_state = PHY_LINK_DISABLE;
142dbf9bfe6Sjack wang 	phy->pm8001_ha = pm8001_ha;
143dbf9bfe6Sjack wang 	sas_phy->enabled = (phy_id < pm8001_ha->chip->n_phy) ? 1 : 0;
144dbf9bfe6Sjack wang 	sas_phy->class = SAS;
145dbf9bfe6Sjack wang 	sas_phy->iproto = SAS_PROTOCOL_ALL;
146dbf9bfe6Sjack wang 	sas_phy->tproto = 0;
147dbf9bfe6Sjack wang 	sas_phy->type = PHY_TYPE_PHYSICAL;
148dbf9bfe6Sjack wang 	sas_phy->role = PHY_ROLE_INITIATOR;
149dbf9bfe6Sjack wang 	sas_phy->oob_mode = OOB_NOT_CONNECTED;
150dbf9bfe6Sjack wang 	sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
151dbf9bfe6Sjack wang 	sas_phy->id = phy_id;
1526c85e4bcSViswas G 	sas_phy->sas_addr = (u8 *)&phy->dev_sas_addr;
153dbf9bfe6Sjack wang 	sas_phy->frame_rcvd = &phy->frame_rcvd[0];
154dbf9bfe6Sjack wang 	sas_phy->ha = (struct sas_ha_struct *)pm8001_ha->shost->hostdata;
155dbf9bfe6Sjack wang 	sas_phy->lldd_phy = phy;
156dbf9bfe6Sjack wang }
157dbf9bfe6Sjack wang 
158dbf9bfe6Sjack wang /**
159dbf9bfe6Sjack wang  * pm8001_free - free hba
160dbf9bfe6Sjack wang  * @pm8001_ha:	our hba structure.
161dbf9bfe6Sjack wang  */
162dbf9bfe6Sjack wang static void pm8001_free(struct pm8001_hba_info *pm8001_ha)
163dbf9bfe6Sjack wang {
164dbf9bfe6Sjack wang 	int i;
165dbf9bfe6Sjack wang 
166dbf9bfe6Sjack wang 	if (!pm8001_ha)
167dbf9bfe6Sjack wang 		return;
168dbf9bfe6Sjack wang 
169dbf9bfe6Sjack wang 	for (i = 0; i < USI_MAX_MEMCNT; i++) {
170dbf9bfe6Sjack wang 		if (pm8001_ha->memoryMap.region[i].virt_ptr != NULL) {
171f73bdebdSChristoph Hellwig 			dma_free_coherent(&pm8001_ha->pdev->dev,
172bfb4809fSSakthivel K 				(pm8001_ha->memoryMap.region[i].total_len +
173bfb4809fSSakthivel K 				pm8001_ha->memoryMap.region[i].alignment),
174dbf9bfe6Sjack wang 				pm8001_ha->memoryMap.region[i].virt_ptr,
175dbf9bfe6Sjack wang 				pm8001_ha->memoryMap.region[i].phys_addr);
176dbf9bfe6Sjack wang 			}
177dbf9bfe6Sjack wang 	}
178dbf9bfe6Sjack wang 	PM8001_CHIP_DISP->chip_iounmap(pm8001_ha);
179429305e4STejun Heo 	flush_workqueue(pm8001_wq);
180dbf9bfe6Sjack wang 	kfree(pm8001_ha->tags);
181dbf9bfe6Sjack wang 	kfree(pm8001_ha);
182dbf9bfe6Sjack wang }
183dbf9bfe6Sjack wang 
184dbf9bfe6Sjack wang #ifdef PM8001_USE_TASKLET
1851245ee59SSakthivel K 
1861245ee59SSakthivel K /**
187bd1050e1SLee Jones  * pm8001_tasklet() - tasklet for 64 msi-x interrupt handler
1881245ee59SSakthivel K  * @opaque: the passed general host adapter struct
1891245ee59SSakthivel K  * Note: pm8001_tasklet is common for pm8001 & pm80xx
1901245ee59SSakthivel K  */
191dbf9bfe6Sjack wang static void pm8001_tasklet(unsigned long opaque)
192dbf9bfe6Sjack wang {
193dbf9bfe6Sjack wang 	struct pm8001_hba_info *pm8001_ha;
1946cd60b37SNikith Ganigarakoppal 	struct isr_param *irq_vector;
1956cd60b37SNikith Ganigarakoppal 
1966cd60b37SNikith Ganigarakoppal 	irq_vector = (struct isr_param *)opaque;
1976cd60b37SNikith Ganigarakoppal 	pm8001_ha = irq_vector->drv_inst;
198dbf9bfe6Sjack wang 	if (unlikely(!pm8001_ha))
199dbf9bfe6Sjack wang 		BUG_ON(1);
2006cd60b37SNikith Ganigarakoppal 	PM8001_CHIP_DISP->isr(pm8001_ha, irq_vector->irq_id);
201dbf9bfe6Sjack wang }
202dbf9bfe6Sjack wang #endif
203dbf9bfe6Sjack wang 
204dbf9bfe6Sjack wang /**
2051245ee59SSakthivel K  * pm8001_interrupt_handler_msix - main MSIX interrupt handler.
2061245ee59SSakthivel K  * It obtains the vector number and calls the equivalent bottom
2071245ee59SSakthivel K  * half or services directly.
208e802fc43SLee Jones  * @irq: interrupt number
2091245ee59SSakthivel K  * @opaque: the passed outbound queue/vector. Host structure is
2101245ee59SSakthivel K  * retrieved from the same.
211dbf9bfe6Sjack wang  */
2121245ee59SSakthivel K static irqreturn_t pm8001_interrupt_handler_msix(int irq, void *opaque)
2131245ee59SSakthivel K {
2146cd60b37SNikith Ganigarakoppal 	struct isr_param *irq_vector;
2156cd60b37SNikith Ganigarakoppal 	struct pm8001_hba_info *pm8001_ha;
2161245ee59SSakthivel K 	irqreturn_t ret = IRQ_HANDLED;
2176cd60b37SNikith Ganigarakoppal 	irq_vector = (struct isr_param *)opaque;
2186cd60b37SNikith Ganigarakoppal 	pm8001_ha = irq_vector->drv_inst;
2196cd60b37SNikith Ganigarakoppal 
2201245ee59SSakthivel K 	if (unlikely(!pm8001_ha))
2211245ee59SSakthivel K 		return IRQ_NONE;
222f310a4eaSColin Ian King 	if (!PM8001_CHIP_DISP->is_our_interrupt(pm8001_ha))
2231245ee59SSakthivel K 		return IRQ_NONE;
2241245ee59SSakthivel K #ifdef PM8001_USE_TASKLET
2256cd60b37SNikith Ganigarakoppal 	tasklet_schedule(&pm8001_ha->tasklet[irq_vector->irq_id]);
2261245ee59SSakthivel K #else
2276cd60b37SNikith Ganigarakoppal 	ret = PM8001_CHIP_DISP->isr(pm8001_ha, irq_vector->irq_id);
2281245ee59SSakthivel K #endif
2291245ee59SSakthivel K 	return ret;
2301245ee59SSakthivel K }
2311245ee59SSakthivel K 
2321245ee59SSakthivel K /**
2331245ee59SSakthivel K  * pm8001_interrupt_handler_intx - main INTx interrupt handler.
234e802fc43SLee Jones  * @irq: interrupt number
2351245ee59SSakthivel K  * @dev_id: sas_ha structure. The HBA is retrieved from sas_has structure.
2361245ee59SSakthivel K  */
2371245ee59SSakthivel K 
2381245ee59SSakthivel K static irqreturn_t pm8001_interrupt_handler_intx(int irq, void *dev_id)
239dbf9bfe6Sjack wang {
240dbf9bfe6Sjack wang 	struct pm8001_hba_info *pm8001_ha;
241dbf9bfe6Sjack wang 	irqreturn_t ret = IRQ_HANDLED;
2421245ee59SSakthivel K 	struct sas_ha_struct *sha = dev_id;
243dbf9bfe6Sjack wang 	pm8001_ha = sha->lldd_ha;
244dbf9bfe6Sjack wang 	if (unlikely(!pm8001_ha))
245dbf9bfe6Sjack wang 		return IRQ_NONE;
246f310a4eaSColin Ian King 	if (!PM8001_CHIP_DISP->is_our_interrupt(pm8001_ha))
247dbf9bfe6Sjack wang 		return IRQ_NONE;
2481245ee59SSakthivel K 
249dbf9bfe6Sjack wang #ifdef PM8001_USE_TASKLET
2506cd60b37SNikith Ganigarakoppal 	tasklet_schedule(&pm8001_ha->tasklet[0]);
251dbf9bfe6Sjack wang #else
252f74cf271SSakthivel K 	ret = PM8001_CHIP_DISP->isr(pm8001_ha, 0);
253dbf9bfe6Sjack wang #endif
254dbf9bfe6Sjack wang 	return ret;
255dbf9bfe6Sjack wang }
256dbf9bfe6Sjack wang 
257d384be6eSVikram Auradkar static u32 pm8001_setup_irq(struct pm8001_hba_info *pm8001_ha);
258d384be6eSVikram Auradkar static u32 pm8001_request_irq(struct pm8001_hba_info *pm8001_ha);
259d384be6eSVikram Auradkar 
260dbf9bfe6Sjack wang /**
261dbf9bfe6Sjack wang  * pm8001_alloc - initiate our hba structure and 6 DMAs area.
262dbf9bfe6Sjack wang  * @pm8001_ha: our hba structure.
263e802fc43SLee Jones  * @ent: PCI device ID structure to match on
264dbf9bfe6Sjack wang  */
265e590adfdSSakthivel K static int pm8001_alloc(struct pm8001_hba_info *pm8001_ha,
266e590adfdSSakthivel K 			const struct pci_device_id *ent)
267dbf9bfe6Sjack wang {
26805c6c029SViswas G 	int i, count = 0, rc = 0;
26905c6c029SViswas G 	u32 ci_offset, ib_offset, ob_offset, pi_offset;
270*1f02beffSViswas G 	struct inbound_queue_table *ibq;
271*1f02beffSViswas G 	struct outbound_queue_table *obq;
27205c6c029SViswas G 
273dbf9bfe6Sjack wang 	spin_lock_init(&pm8001_ha->lock);
274646cdf00STomas Henzl 	spin_lock_init(&pm8001_ha->bitmap_lock);
2751b5d2793SJoe Perches 	pm8001_dbg(pm8001_ha, INIT, "pm8001_alloc: PHY:%x\n",
2761b5d2793SJoe Perches 		   pm8001_ha->chip->n_phy);
27705c6c029SViswas G 
27805c6c029SViswas G 	/* Setup Interrupt */
27905c6c029SViswas G 	rc = pm8001_setup_irq(pm8001_ha);
28005c6c029SViswas G 	if (rc) {
2811b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, FAIL,
2821b5d2793SJoe Perches 			   "pm8001_setup_irq failed [ret: %d]\n", rc);
28305c6c029SViswas G 		goto err_out_shost;
28405c6c029SViswas G 	}
28505c6c029SViswas G 	/* Request Interrupt */
28605c6c029SViswas G 	rc = pm8001_request_irq(pm8001_ha);
28705c6c029SViswas G 	if (rc)
28805c6c029SViswas G 		goto err_out_shost;
28905c6c029SViswas G 
29005c6c029SViswas G 	count = pm8001_ha->max_q_num;
29105c6c029SViswas G 	/* Queues are chosen based on the number of cores/msix availability */
29227bc43bdSViswas G 	ib_offset = pm8001_ha->ib_offset  = USI_MAX_MEMCNT_BASE;
29305c6c029SViswas G 	ci_offset = pm8001_ha->ci_offset  = ib_offset + count;
29405c6c029SViswas G 	ob_offset = pm8001_ha->ob_offset  = ci_offset + count;
29505c6c029SViswas G 	pi_offset = pm8001_ha->pi_offset  = ob_offset + count;
29605c6c029SViswas G 	pm8001_ha->max_memcnt = pi_offset + count;
29705c6c029SViswas G 
2981cc943aeSjack wang 	for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
299dbf9bfe6Sjack wang 		pm8001_phy_init(pm8001_ha, i);
3001cc943aeSjack wang 		pm8001_ha->port[i].wide_port_phymap = 0;
3011cc943aeSjack wang 		pm8001_ha->port[i].port_attached = 0;
3021cc943aeSjack wang 		pm8001_ha->port[i].port_state = 0;
3031cc943aeSjack wang 		INIT_LIST_HEAD(&pm8001_ha->port[i].list);
3041cc943aeSjack wang 	}
305dbf9bfe6Sjack wang 
306dbf9bfe6Sjack wang 	/* MPI Memory region 1 for AAP Event Log for fw */
307dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[AAP1].num_elements = 1;
308dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[AAP1].element_size = PM8001_EVENT_LOG_SIZE;
309dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[AAP1].total_len = PM8001_EVENT_LOG_SIZE;
310dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[AAP1].alignment = 32;
311dbf9bfe6Sjack wang 
312dbf9bfe6Sjack wang 	/* MPI Memory region 2 for IOP Event Log for fw */
313dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[IOP].num_elements = 1;
314dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[IOP].element_size = PM8001_EVENT_LOG_SIZE;
315dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[IOP].total_len = PM8001_EVENT_LOG_SIZE;
316dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[IOP].alignment = 32;
317dbf9bfe6Sjack wang 
31805c6c029SViswas G 	for (i = 0; i < count; i++) {
319*1f02beffSViswas G 		ibq = &pm8001_ha->inbnd_q_tbl[i];
320*1f02beffSViswas G 		spin_lock_init(&ibq->iq_lock);
321dbf9bfe6Sjack wang 		/* MPI Memory region 3 for consumer Index of inbound queues */
32205c6c029SViswas G 		pm8001_ha->memoryMap.region[ci_offset+i].num_elements = 1;
32305c6c029SViswas G 		pm8001_ha->memoryMap.region[ci_offset+i].element_size = 4;
32405c6c029SViswas G 		pm8001_ha->memoryMap.region[ci_offset+i].total_len = 4;
32505c6c029SViswas G 		pm8001_ha->memoryMap.region[ci_offset+i].alignment = 4;
326dbf9bfe6Sjack wang 
327e590adfdSSakthivel K 		if ((ent->driver_data) != chip_8001) {
328dbf9bfe6Sjack wang 			/* MPI Memory region 5 inbound queues */
32905c6c029SViswas G 			pm8001_ha->memoryMap.region[ib_offset+i].num_elements =
330e590adfdSSakthivel K 						PM8001_MPI_QUEUE;
33105c6c029SViswas G 			pm8001_ha->memoryMap.region[ib_offset+i].element_size
33205c6c029SViswas G 								= 128;
33305c6c029SViswas G 			pm8001_ha->memoryMap.region[ib_offset+i].total_len =
334e590adfdSSakthivel K 						PM8001_MPI_QUEUE * 128;
33505c6c029SViswas G 			pm8001_ha->memoryMap.region[ib_offset+i].alignment
33605c6c029SViswas G 								= 128;
337e590adfdSSakthivel K 		} else {
33805c6c029SViswas G 			pm8001_ha->memoryMap.region[ib_offset+i].num_elements =
339e590adfdSSakthivel K 						PM8001_MPI_QUEUE;
34005c6c029SViswas G 			pm8001_ha->memoryMap.region[ib_offset+i].element_size
34105c6c029SViswas G 								= 64;
34205c6c029SViswas G 			pm8001_ha->memoryMap.region[ib_offset+i].total_len =
343e590adfdSSakthivel K 						PM8001_MPI_QUEUE * 64;
34405c6c029SViswas G 			pm8001_ha->memoryMap.region[ib_offset+i].alignment = 64;
345e590adfdSSakthivel K 		}
346e590adfdSSakthivel K 	}
347dbf9bfe6Sjack wang 
34805c6c029SViswas G 	for (i = 0; i < count; i++) {
349*1f02beffSViswas G 		obq = &pm8001_ha->outbnd_q_tbl[i];
350*1f02beffSViswas G 		spin_lock_init(&obq->oq_lock);
351e590adfdSSakthivel K 		/* MPI Memory region 4 for producer Index of outbound queues */
35205c6c029SViswas G 		pm8001_ha->memoryMap.region[pi_offset+i].num_elements = 1;
35305c6c029SViswas G 		pm8001_ha->memoryMap.region[pi_offset+i].element_size = 4;
35405c6c029SViswas G 		pm8001_ha->memoryMap.region[pi_offset+i].total_len = 4;
35505c6c029SViswas G 		pm8001_ha->memoryMap.region[pi_offset+i].alignment = 4;
356dbf9bfe6Sjack wang 
357e590adfdSSakthivel K 		if (ent->driver_data != chip_8001) {
358e590adfdSSakthivel K 			/* MPI Memory region 6 Outbound queues */
35905c6c029SViswas G 			pm8001_ha->memoryMap.region[ob_offset+i].num_elements =
360e590adfdSSakthivel K 						PM8001_MPI_QUEUE;
36105c6c029SViswas G 			pm8001_ha->memoryMap.region[ob_offset+i].element_size
36205c6c029SViswas G 								= 128;
36305c6c029SViswas G 			pm8001_ha->memoryMap.region[ob_offset+i].total_len =
364e590adfdSSakthivel K 						PM8001_MPI_QUEUE * 128;
36505c6c029SViswas G 			pm8001_ha->memoryMap.region[ob_offset+i].alignment
36605c6c029SViswas G 								= 128;
367e590adfdSSakthivel K 		} else {
368e590adfdSSakthivel K 			/* MPI Memory region 6 Outbound queues */
36905c6c029SViswas G 			pm8001_ha->memoryMap.region[ob_offset+i].num_elements =
370e590adfdSSakthivel K 						PM8001_MPI_QUEUE;
37105c6c029SViswas G 			pm8001_ha->memoryMap.region[ob_offset+i].element_size
37205c6c029SViswas G 								= 64;
37305c6c029SViswas G 			pm8001_ha->memoryMap.region[ob_offset+i].total_len =
374e590adfdSSakthivel K 						PM8001_MPI_QUEUE * 64;
37505c6c029SViswas G 			pm8001_ha->memoryMap.region[ob_offset+i].alignment = 64;
376e590adfdSSakthivel K 		}
377e590adfdSSakthivel K 
378e590adfdSSakthivel K 	}
379dbf9bfe6Sjack wang 	/* Memory region write DMA*/
380dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[NVMD].num_elements = 1;
381dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[NVMD].element_size = 4096;
382dbf9bfe6Sjack wang 	pm8001_ha->memoryMap.region[NVMD].total_len = 4096;
383dbf9bfe6Sjack wang 
3841c75a679SSakthivel K 	/* Memory region for fw flash */
3851c75a679SSakthivel K 	pm8001_ha->memoryMap.region[FW_FLASH].total_len = 4096;
3861c75a679SSakthivel K 
387d078b511SAnand Kumar Santhanam 	pm8001_ha->memoryMap.region[FORENSIC_MEM].num_elements = 1;
388d078b511SAnand Kumar Santhanam 	pm8001_ha->memoryMap.region[FORENSIC_MEM].total_len = 0x10000;
389d078b511SAnand Kumar Santhanam 	pm8001_ha->memoryMap.region[FORENSIC_MEM].element_size = 0x10000;
390d078b511SAnand Kumar Santhanam 	pm8001_ha->memoryMap.region[FORENSIC_MEM].alignment = 0x10000;
39105c6c029SViswas G 	for (i = 0; i < pm8001_ha->max_memcnt; i++) {
3929aed578fSJoe Perches 		struct mpi_mem *region = &pm8001_ha->memoryMap.region[i];
3939aed578fSJoe Perches 
394dbf9bfe6Sjack wang 		if (pm8001_mem_alloc(pm8001_ha->pdev,
3959aed578fSJoe Perches 				     &region->virt_ptr,
3969aed578fSJoe Perches 				     &region->phys_addr,
3979aed578fSJoe Perches 				     &region->phys_addr_hi,
3989aed578fSJoe Perches 				     &region->phys_addr_lo,
3999aed578fSJoe Perches 				     region->total_len,
4009aed578fSJoe Perches 				     region->alignment) != 0) {
4019aed578fSJoe Perches 			pm8001_dbg(pm8001_ha, FAIL, "Mem%d alloc failed\n", i);
402dbf9bfe6Sjack wang 			goto err_out;
403dbf9bfe6Sjack wang 		}
404dbf9bfe6Sjack wang 	}
405dbf9bfe6Sjack wang 
40627bc43bdSViswas G 	/* Memory region for devices*/
40727bc43bdSViswas G 	pm8001_ha->devices = kzalloc(PM8001_MAX_DEVICES
40827bc43bdSViswas G 				* sizeof(struct pm8001_device), GFP_KERNEL);
40927bc43bdSViswas G 	if (!pm8001_ha->devices) {
41027bc43bdSViswas G 		rc = -ENOMEM;
41127bc43bdSViswas G 		goto err_out_nodev;
41227bc43bdSViswas G 	}
413dbf9bfe6Sjack wang 	for (i = 0; i < PM8001_MAX_DEVICES; i++) {
414aa9f8328SJames Bottomley 		pm8001_ha->devices[i].dev_type = SAS_PHY_UNUSED;
415dbf9bfe6Sjack wang 		pm8001_ha->devices[i].id = i;
416dbf9bfe6Sjack wang 		pm8001_ha->devices[i].device_id = PM8001_MAX_DEVICES;
4174a2efd4bSViswas G 		atomic_set(&pm8001_ha->devices[i].running_req, 0);
418dbf9bfe6Sjack wang 	}
419dbf9bfe6Sjack wang 	pm8001_ha->flags = PM8001F_INIT_TIME;
420dbf9bfe6Sjack wang 	/* Initialize tags */
421dbf9bfe6Sjack wang 	pm8001_tag_init(pm8001_ha);
422dbf9bfe6Sjack wang 	return 0;
42327bc43bdSViswas G 
42405c6c029SViswas G err_out_shost:
42505c6c029SViswas G 	scsi_remove_host(pm8001_ha->shost);
42627bc43bdSViswas G err_out_nodev:
42727bc43bdSViswas G 	for (i = 0; i < pm8001_ha->max_memcnt; i++) {
42827bc43bdSViswas G 		if (pm8001_ha->memoryMap.region[i].virt_ptr != NULL) {
4298e60a7deSChristophe JAILLET 			dma_free_coherent(&pm8001_ha->pdev->dev,
43027bc43bdSViswas G 				(pm8001_ha->memoryMap.region[i].total_len +
43127bc43bdSViswas G 				pm8001_ha->memoryMap.region[i].alignment),
43227bc43bdSViswas G 				pm8001_ha->memoryMap.region[i].virt_ptr,
43327bc43bdSViswas G 				pm8001_ha->memoryMap.region[i].phys_addr);
43427bc43bdSViswas G 		}
43527bc43bdSViswas G 	}
436dbf9bfe6Sjack wang err_out:
437dbf9bfe6Sjack wang 	return 1;
438dbf9bfe6Sjack wang }
439dbf9bfe6Sjack wang 
440dbf9bfe6Sjack wang /**
441dbf9bfe6Sjack wang  * pm8001_ioremap - remap the pci high physical address to kernal virtual
442dbf9bfe6Sjack wang  * address so that we can access them.
443dbf9bfe6Sjack wang  * @pm8001_ha:our hba structure.
444dbf9bfe6Sjack wang  */
445dbf9bfe6Sjack wang static int pm8001_ioremap(struct pm8001_hba_info *pm8001_ha)
446dbf9bfe6Sjack wang {
447dbf9bfe6Sjack wang 	u32 bar;
448dbf9bfe6Sjack wang 	u32 logicalBar = 0;
449dbf9bfe6Sjack wang 	struct pci_dev *pdev;
450dbf9bfe6Sjack wang 
451dbf9bfe6Sjack wang 	pdev = pm8001_ha->pdev;
452dbf9bfe6Sjack wang 	/* map pci mem (PMC pci base 0-3)*/
453c9c13ba4SDenis Efremov 	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
454dbf9bfe6Sjack wang 		/*
455dbf9bfe6Sjack wang 		** logical BARs for SPC:
456dbf9bfe6Sjack wang 		** bar 0 and 1 - logical BAR0
457dbf9bfe6Sjack wang 		** bar 2 and 3 - logical BAR1
458dbf9bfe6Sjack wang 		** bar4 - logical BAR2
459dbf9bfe6Sjack wang 		** bar5 - logical BAR3
460dbf9bfe6Sjack wang 		** Skip the appropriate assignments:
461dbf9bfe6Sjack wang 		*/
462dbf9bfe6Sjack wang 		if ((bar == 1) || (bar == 3))
463dbf9bfe6Sjack wang 			continue;
464dbf9bfe6Sjack wang 		if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
465dbf9bfe6Sjack wang 			pm8001_ha->io_mem[logicalBar].membase =
466dbf9bfe6Sjack wang 				pci_resource_start(pdev, bar);
467dbf9bfe6Sjack wang 			pm8001_ha->io_mem[logicalBar].memsize =
468dbf9bfe6Sjack wang 				pci_resource_len(pdev, bar);
469dbf9bfe6Sjack wang 			pm8001_ha->io_mem[logicalBar].memvirtaddr =
470dbf9bfe6Sjack wang 				ioremap(pm8001_ha->io_mem[logicalBar].membase,
471dbf9bfe6Sjack wang 				pm8001_ha->io_mem[logicalBar].memsize);
47295652f98Sakshatzen 			if (!pm8001_ha->io_mem[logicalBar].memvirtaddr) {
4731b5d2793SJoe Perches 				pm8001_dbg(pm8001_ha, INIT,
47495652f98Sakshatzen 					"Failed to ioremap bar %d, logicalBar %d",
4751b5d2793SJoe Perches 				   bar, logicalBar);
47695652f98Sakshatzen 				return -ENOMEM;
47795652f98Sakshatzen 			}
4781b5d2793SJoe Perches 			pm8001_dbg(pm8001_ha, INIT,
479e590adfdSSakthivel K 				   "base addr %llx virt_addr=%llx len=%d\n",
480e590adfdSSakthivel K 				   (u64)pm8001_ha->io_mem[logicalBar].membase,
481da1dccceSAnand Kumar Santhanam 				   (u64)(unsigned long)
482da1dccceSAnand Kumar Santhanam 				   pm8001_ha->io_mem[logicalBar].memvirtaddr,
4831b5d2793SJoe Perches 				   pm8001_ha->io_mem[logicalBar].memsize);
484dbf9bfe6Sjack wang 		} else {
485dbf9bfe6Sjack wang 			pm8001_ha->io_mem[logicalBar].membase	= 0;
486dbf9bfe6Sjack wang 			pm8001_ha->io_mem[logicalBar].memsize	= 0;
48762fb8b34SSaurav Girepunje 			pm8001_ha->io_mem[logicalBar].memvirtaddr = NULL;
488dbf9bfe6Sjack wang 		}
489dbf9bfe6Sjack wang 		logicalBar++;
490dbf9bfe6Sjack wang 	}
491dbf9bfe6Sjack wang 	return 0;
492dbf9bfe6Sjack wang }
493dbf9bfe6Sjack wang 
494dbf9bfe6Sjack wang /**
495dbf9bfe6Sjack wang  * pm8001_pci_alloc - initialize our ha card structure
496dbf9bfe6Sjack wang  * @pdev: pci device.
497dbf9bfe6Sjack wang  * @ent: ent
498dbf9bfe6Sjack wang  * @shost: scsi host struct which has been initialized before.
499dbf9bfe6Sjack wang  */
5006f039790SGreg Kroah-Hartman static struct pm8001_hba_info *pm8001_pci_alloc(struct pci_dev *pdev,
501e590adfdSSakthivel K 				 const struct pci_device_id *ent,
5026f039790SGreg Kroah-Hartman 				struct Scsi_Host *shost)
503e590adfdSSakthivel K 
504dbf9bfe6Sjack wang {
505dbf9bfe6Sjack wang 	struct pm8001_hba_info *pm8001_ha;
506dbf9bfe6Sjack wang 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
5076cd60b37SNikith Ganigarakoppal 	int j;
508dbf9bfe6Sjack wang 
509dbf9bfe6Sjack wang 	pm8001_ha = sha->lldd_ha;
510dbf9bfe6Sjack wang 	if (!pm8001_ha)
511dbf9bfe6Sjack wang 		return NULL;
512dbf9bfe6Sjack wang 
513dbf9bfe6Sjack wang 	pm8001_ha->pdev = pdev;
514dbf9bfe6Sjack wang 	pm8001_ha->dev = &pdev->dev;
515e590adfdSSakthivel K 	pm8001_ha->chip_id = ent->driver_data;
516dbf9bfe6Sjack wang 	pm8001_ha->chip = &pm8001_chips[pm8001_ha->chip_id];
517dbf9bfe6Sjack wang 	pm8001_ha->irq = pdev->irq;
518dbf9bfe6Sjack wang 	pm8001_ha->sas = sha;
519dbf9bfe6Sjack wang 	pm8001_ha->shost = shost;
520dbf9bfe6Sjack wang 	pm8001_ha->id = pm8001_id++;
5217370672dSpeter chang 	pm8001_ha->logging_level = logging_level;
522dba2cc03SDeepak Ukey 	pm8001_ha->non_fatal_count = 0;
5233e253d96Speter chang 	if (link_rate >= 1 && link_rate <= 15)
5243e253d96Speter chang 		pm8001_ha->link_rate = (link_rate << 8);
5253e253d96Speter chang 	else {
5263e253d96Speter chang 		pm8001_ha->link_rate = LINKRATE_15 | LINKRATE_30 |
5273e253d96Speter chang 			LINKRATE_60 | LINKRATE_120;
5281b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, FAIL,
5291b5d2793SJoe Perches 			   "Setting link rate to default value\n");
5303e253d96Speter chang 	}
531dbf9bfe6Sjack wang 	sprintf(pm8001_ha->name, "%s%d", DRV_NAME, pm8001_ha->id);
532f74cf271SSakthivel K 	/* IOMB size is 128 for 8088/89 controllers */
533f74cf271SSakthivel K 	if (pm8001_ha->chip_id != chip_8001)
534f74cf271SSakthivel K 		pm8001_ha->iomb_size = IOMB_SIZE_SPCV;
535f74cf271SSakthivel K 	else
536f74cf271SSakthivel K 		pm8001_ha->iomb_size = IOMB_SIZE_SPC;
537f74cf271SSakthivel K 
538dbf9bfe6Sjack wang #ifdef PM8001_USE_TASKLET
5396cd60b37SNikith Ganigarakoppal 	/* Tasklet for non msi-x interrupt handler */
540c913df3fSBenjamin Rood 	if ((!pdev->msix_cap || !pci_msi_enabled())
541c913df3fSBenjamin Rood 	    || (pm8001_ha->chip_id == chip_8001))
5426cd60b37SNikith Ganigarakoppal 		tasklet_init(&pm8001_ha->tasklet[0], pm8001_tasklet,
5436cd60b37SNikith Ganigarakoppal 			(unsigned long)&(pm8001_ha->irq_vector[0]));
5446cd60b37SNikith Ganigarakoppal 	else
5456cd60b37SNikith Ganigarakoppal 		for (j = 0; j < PM8001_MAX_MSIX_VEC; j++)
5466cd60b37SNikith Ganigarakoppal 			tasklet_init(&pm8001_ha->tasklet[j], pm8001_tasklet,
5476cd60b37SNikith Ganigarakoppal 				(unsigned long)&(pm8001_ha->irq_vector[j]));
548dbf9bfe6Sjack wang #endif
54995652f98Sakshatzen 	if (pm8001_ioremap(pm8001_ha))
55095652f98Sakshatzen 		goto failed_pci_alloc;
551e590adfdSSakthivel K 	if (!pm8001_alloc(pm8001_ha, ent))
552dbf9bfe6Sjack wang 		return pm8001_ha;
55395652f98Sakshatzen failed_pci_alloc:
554dbf9bfe6Sjack wang 	pm8001_free(pm8001_ha);
555dbf9bfe6Sjack wang 	return NULL;
556dbf9bfe6Sjack wang }
557dbf9bfe6Sjack wang 
558dbf9bfe6Sjack wang /**
559dbf9bfe6Sjack wang  * pci_go_44 - pm8001 specified, its DMA is 44 bit rather than 64 bit
560dbf9bfe6Sjack wang  * @pdev: pci device.
561dbf9bfe6Sjack wang  */
562dbf9bfe6Sjack wang static int pci_go_44(struct pci_dev *pdev)
563dbf9bfe6Sjack wang {
564dbf9bfe6Sjack wang 	int rc;
565dbf9bfe6Sjack wang 
566f73bdebdSChristoph Hellwig 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
567dbf9bfe6Sjack wang 	if (rc) {
568f73bdebdSChristoph Hellwig 		rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
569f73bdebdSChristoph Hellwig 		if (rc)
570dbf9bfe6Sjack wang 			dev_printk(KERN_ERR, &pdev->dev,
571dbf9bfe6Sjack wang 				"32-bit DMA enable failed\n");
572dbf9bfe6Sjack wang 	}
573dbf9bfe6Sjack wang 	return rc;
574dbf9bfe6Sjack wang }
575dbf9bfe6Sjack wang 
576dbf9bfe6Sjack wang /**
577dbf9bfe6Sjack wang  * pm8001_prep_sas_ha_init - allocate memory in general hba struct && init them.
578dbf9bfe6Sjack wang  * @shost: scsi host which has been allocated outside.
579dbf9bfe6Sjack wang  * @chip_info: our ha struct.
580dbf9bfe6Sjack wang  */
5816f039790SGreg Kroah-Hartman static int pm8001_prep_sas_ha_init(struct Scsi_Host *shost,
582dbf9bfe6Sjack wang 				   const struct pm8001_chip_info *chip_info)
583dbf9bfe6Sjack wang {
584dbf9bfe6Sjack wang 	int phy_nr, port_nr;
585dbf9bfe6Sjack wang 	struct asd_sas_phy **arr_phy;
586dbf9bfe6Sjack wang 	struct asd_sas_port **arr_port;
587dbf9bfe6Sjack wang 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
588dbf9bfe6Sjack wang 
589dbf9bfe6Sjack wang 	phy_nr = chip_info->n_phy;
590dbf9bfe6Sjack wang 	port_nr = phy_nr;
591dbf9bfe6Sjack wang 	memset(sha, 0x00, sizeof(*sha));
592dbf9bfe6Sjack wang 	arr_phy = kcalloc(phy_nr, sizeof(void *), GFP_KERNEL);
593dbf9bfe6Sjack wang 	if (!arr_phy)
594dbf9bfe6Sjack wang 		goto exit;
595dbf9bfe6Sjack wang 	arr_port = kcalloc(port_nr, sizeof(void *), GFP_KERNEL);
596dbf9bfe6Sjack wang 	if (!arr_port)
597dbf9bfe6Sjack wang 		goto exit_free2;
598dbf9bfe6Sjack wang 
599dbf9bfe6Sjack wang 	sha->sas_phy = arr_phy;
600dbf9bfe6Sjack wang 	sha->sas_port = arr_port;
601dbf9bfe6Sjack wang 	sha->lldd_ha = kzalloc(sizeof(struct pm8001_hba_info), GFP_KERNEL);
602dbf9bfe6Sjack wang 	if (!sha->lldd_ha)
603dbf9bfe6Sjack wang 		goto exit_free1;
604dbf9bfe6Sjack wang 
605dbf9bfe6Sjack wang 	shost->transportt = pm8001_stt;
606dbf9bfe6Sjack wang 	shost->max_id = PM8001_MAX_DEVICES;
607dbf9bfe6Sjack wang 	shost->max_lun = 8;
608dbf9bfe6Sjack wang 	shost->max_channel = 0;
609dbf9bfe6Sjack wang 	shost->unique_id = pm8001_id;
610dbf9bfe6Sjack wang 	shost->max_cmd_len = 16;
611dbf9bfe6Sjack wang 	shost->can_queue = PM8001_CAN_QUEUE;
612dbf9bfe6Sjack wang 	shost->cmd_per_lun = 32;
613dbf9bfe6Sjack wang 	return 0;
614dbf9bfe6Sjack wang exit_free1:
615dbf9bfe6Sjack wang 	kfree(arr_port);
616dbf9bfe6Sjack wang exit_free2:
617dbf9bfe6Sjack wang 	kfree(arr_phy);
618dbf9bfe6Sjack wang exit:
619dbf9bfe6Sjack wang 	return -1;
620dbf9bfe6Sjack wang }
621dbf9bfe6Sjack wang 
622dbf9bfe6Sjack wang /**
623dbf9bfe6Sjack wang  * pm8001_post_sas_ha_init - initialize general hba struct defined in libsas
624dbf9bfe6Sjack wang  * @shost: scsi host which has been allocated outside
625dbf9bfe6Sjack wang  * @chip_info: our ha struct.
626dbf9bfe6Sjack wang  */
6276f039790SGreg Kroah-Hartman static void  pm8001_post_sas_ha_init(struct Scsi_Host *shost,
628dbf9bfe6Sjack wang 				     const struct pm8001_chip_info *chip_info)
629dbf9bfe6Sjack wang {
630dbf9bfe6Sjack wang 	int i = 0;
631dbf9bfe6Sjack wang 	struct pm8001_hba_info *pm8001_ha;
632dbf9bfe6Sjack wang 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
633dbf9bfe6Sjack wang 
634dbf9bfe6Sjack wang 	pm8001_ha = sha->lldd_ha;
635dbf9bfe6Sjack wang 	for (i = 0; i < chip_info->n_phy; i++) {
636dbf9bfe6Sjack wang 		sha->sas_phy[i] = &pm8001_ha->phy[i].sas_phy;
637dbf9bfe6Sjack wang 		sha->sas_port[i] = &pm8001_ha->port[i].sas_port;
6386c85e4bcSViswas G 		sha->sas_phy[i]->sas_addr =
6396c85e4bcSViswas G 			(u8 *)&pm8001_ha->phy[i].dev_sas_addr;
640dbf9bfe6Sjack wang 	}
641dbf9bfe6Sjack wang 	sha->sas_ha_name = DRV_NAME;
642dbf9bfe6Sjack wang 	sha->dev = pm8001_ha->dev;
6436c85e4bcSViswas G 	sha->strict_wide_ports = 1;
644dbf9bfe6Sjack wang 	sha->lldd_module = THIS_MODULE;
645dbf9bfe6Sjack wang 	sha->sas_addr = &pm8001_ha->sas_addr[0];
646dbf9bfe6Sjack wang 	sha->num_phys = chip_info->n_phy;
647dbf9bfe6Sjack wang 	sha->core.shost = shost;
648dbf9bfe6Sjack wang }
649dbf9bfe6Sjack wang 
650dbf9bfe6Sjack wang /**
651dbf9bfe6Sjack wang  * pm8001_init_sas_add - initialize sas address
652e802fc43SLee Jones  * @pm8001_ha: our ha struct.
653dbf9bfe6Sjack wang  *
654dbf9bfe6Sjack wang  * Currently we just set the fixed SAS address to our HBA,for manufacture,
655dbf9bfe6Sjack wang  * it should read from the EEPROM
656dbf9bfe6Sjack wang  */
657dbf9bfe6Sjack wang static void pm8001_init_sas_add(struct pm8001_hba_info *pm8001_ha)
658dbf9bfe6Sjack wang {
659a33a0155SSakthivel K 	u8 i, j;
6606c85e4bcSViswas G 	u8 sas_add[8];
661dbf9bfe6Sjack wang #ifdef PM8001_READ_VPD
662a33a0155SSakthivel K 	/* For new SPC controllers WWN is stored in flash vpd
663a33a0155SSakthivel K 	*  For SPC/SPCve controllers WWN is stored in EEPROM
664a33a0155SSakthivel K 	*  For Older SPC WWN is stored in NVMD
665a33a0155SSakthivel K 	*/
666dbf9bfe6Sjack wang 	DECLARE_COMPLETION_ONSTACK(completion);
6677c8356d9Sjack wang 	struct pm8001_ioctl_payload payload;
668a33a0155SSakthivel K 	u16 deviceid;
6695b4ce882STomas Henzl 	int rc;
6705b4ce882STomas Henzl 
671a33a0155SSakthivel K 	pci_read_config_word(pm8001_ha->pdev, PCI_DEVICE_ID, &deviceid);
672dbf9bfe6Sjack wang 	pm8001_ha->nvmd_completion = &completion;
673a33a0155SSakthivel K 
674a33a0155SSakthivel K 	if (pm8001_ha->chip_id == chip_8001) {
675f49d2132SBradley Grove 		if (deviceid == 0x8081 || deviceid == 0x0042) {
676a33a0155SSakthivel K 			payload.minor_function = 4;
6779b889846SViswas G 			payload.rd_length = 4096;
678a33a0155SSakthivel K 		} else {
6797c8356d9Sjack wang 			payload.minor_function = 0;
6809b889846SViswas G 			payload.rd_length = 128;
681a33a0155SSakthivel K 		}
68210efa460SBenjamin Rood 	} else if ((pm8001_ha->chip_id == chip_8070 ||
68310efa460SBenjamin Rood 			pm8001_ha->chip_id == chip_8072) &&
68410efa460SBenjamin Rood 			pm8001_ha->pdev->subsystem_vendor == PCI_VENDOR_ID_ATTO) {
68510efa460SBenjamin Rood 		payload.minor_function = 4;
6869b889846SViswas G 		payload.rd_length = 4096;
687a33a0155SSakthivel K 	} else {
688a33a0155SSakthivel K 		payload.minor_function = 1;
6899b889846SViswas G 		payload.rd_length = 4096;
690a33a0155SSakthivel K 	}
691a33a0155SSakthivel K 	payload.offset = 0;
6929b889846SViswas G 	payload.func_specific = kzalloc(payload.rd_length, GFP_KERNEL);
6935b4ce882STomas Henzl 	if (!payload.func_specific) {
6941b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, INIT, "mem alloc fail\n");
6955b4ce882STomas Henzl 		return;
6965b4ce882STomas Henzl 	}
6975b4ce882STomas Henzl 	rc = PM8001_CHIP_DISP->get_nvmd_req(pm8001_ha, &payload);
6985b4ce882STomas Henzl 	if (rc) {
6995b4ce882STomas Henzl 		kfree(payload.func_specific);
7001b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, INIT, "nvmd failed\n");
7015b4ce882STomas Henzl 		return;
7025b4ce882STomas Henzl 	}
703dbf9bfe6Sjack wang 	wait_for_completion(&completion);
704a33a0155SSakthivel K 
705a33a0155SSakthivel K 	for (i = 0, j = 0; i <= 7; i++, j++) {
706a33a0155SSakthivel K 		if (pm8001_ha->chip_id == chip_8001) {
707a33a0155SSakthivel K 			if (deviceid == 0x8081)
708a33a0155SSakthivel K 				pm8001_ha->sas_addr[j] =
709a33a0155SSakthivel K 					payload.func_specific[0x704 + i];
710f49d2132SBradley Grove 			else if (deviceid == 0x0042)
711f49d2132SBradley Grove 				pm8001_ha->sas_addr[j] =
712f49d2132SBradley Grove 					payload.func_specific[0x010 + i];
71310efa460SBenjamin Rood 		} else if ((pm8001_ha->chip_id == chip_8070 ||
71410efa460SBenjamin Rood 				pm8001_ha->chip_id == chip_8072) &&
71510efa460SBenjamin Rood 				pm8001_ha->pdev->subsystem_vendor == PCI_VENDOR_ID_ATTO) {
71610efa460SBenjamin Rood 			pm8001_ha->sas_addr[j] =
71710efa460SBenjamin Rood 					payload.func_specific[0x010 + i];
718a33a0155SSakthivel K 		} else
719a33a0155SSakthivel K 			pm8001_ha->sas_addr[j] =
720a33a0155SSakthivel K 					payload.func_specific[0x804 + i];
721a33a0155SSakthivel K 	}
7226c85e4bcSViswas G 	memcpy(sas_add, pm8001_ha->sas_addr, SAS_ADDR_SIZE);
723dbf9bfe6Sjack wang 	for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
7246c85e4bcSViswas G 		if (i && ((i % 4) == 0))
7256c85e4bcSViswas G 			sas_add[7] = sas_add[7] + 4;
726a33a0155SSakthivel K 		memcpy(&pm8001_ha->phy[i].dev_sas_addr,
7276c85e4bcSViswas G 			sas_add, SAS_ADDR_SIZE);
7281b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, INIT, "phy %d sas_addr = %016llx\n", i,
7291b5d2793SJoe Perches 			   pm8001_ha->phy[i].dev_sas_addr);
730dbf9bfe6Sjack wang 	}
7315b4ce882STomas Henzl 	kfree(payload.func_specific);
732dbf9bfe6Sjack wang #else
733dbf9bfe6Sjack wang 	for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
7347c8356d9Sjack wang 		pm8001_ha->phy[i].dev_sas_addr = 0x50010c600047f9d0ULL;
735dbf9bfe6Sjack wang 		pm8001_ha->phy[i].dev_sas_addr =
736dbf9bfe6Sjack wang 			cpu_to_be64((u64)
737dbf9bfe6Sjack wang 				(*(u64 *)&pm8001_ha->phy[i].dev_sas_addr));
738dbf9bfe6Sjack wang 	}
739dbf9bfe6Sjack wang 	memcpy(pm8001_ha->sas_addr, &pm8001_ha->phy[0].dev_sas_addr,
740dbf9bfe6Sjack wang 		SAS_ADDR_SIZE);
741dbf9bfe6Sjack wang #endif
742dbf9bfe6Sjack wang }
743dbf9bfe6Sjack wang 
74427909407SAnand Kumar Santhanam /*
74527909407SAnand Kumar Santhanam  * pm8001_get_phy_settings_info : Read phy setting values.
74627909407SAnand Kumar Santhanam  * @pm8001_ha : our hba.
74727909407SAnand Kumar Santhanam  */
748f2c6f180SMaurizio Lombardi static int pm8001_get_phy_settings_info(struct pm8001_hba_info *pm8001_ha)
74927909407SAnand Kumar Santhanam {
75027909407SAnand Kumar Santhanam 
75127909407SAnand Kumar Santhanam #ifdef PM8001_READ_VPD
75227909407SAnand Kumar Santhanam 	/*OPTION ROM FLASH read for the SPC cards */
75327909407SAnand Kumar Santhanam 	DECLARE_COMPLETION_ONSTACK(completion);
75427909407SAnand Kumar Santhanam 	struct pm8001_ioctl_payload payload;
7555b4ce882STomas Henzl 	int rc;
75627909407SAnand Kumar Santhanam 
75727909407SAnand Kumar Santhanam 	pm8001_ha->nvmd_completion = &completion;
75827909407SAnand Kumar Santhanam 	/* SAS ADDRESS read from flash / EEPROM */
75927909407SAnand Kumar Santhanam 	payload.minor_function = 6;
76027909407SAnand Kumar Santhanam 	payload.offset = 0;
7619b889846SViswas G 	payload.rd_length = 4096;
76227909407SAnand Kumar Santhanam 	payload.func_specific = kzalloc(4096, GFP_KERNEL);
763f2c6f180SMaurizio Lombardi 	if (!payload.func_specific)
764f2c6f180SMaurizio Lombardi 		return -ENOMEM;
76527909407SAnand Kumar Santhanam 	/* Read phy setting values from flash */
7665b4ce882STomas Henzl 	rc = PM8001_CHIP_DISP->get_nvmd_req(pm8001_ha, &payload);
7675b4ce882STomas Henzl 	if (rc) {
7685b4ce882STomas Henzl 		kfree(payload.func_specific);
7691b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, INIT, "nvmd failed\n");
7705b4ce882STomas Henzl 		return -ENOMEM;
7715b4ce882STomas Henzl 	}
77227909407SAnand Kumar Santhanam 	wait_for_completion(&completion);
77327909407SAnand Kumar Santhanam 	pm8001_set_phy_profile(pm8001_ha, sizeof(u8), payload.func_specific);
774f2c6f180SMaurizio Lombardi 	kfree(payload.func_specific);
77527909407SAnand Kumar Santhanam #endif
776f2c6f180SMaurizio Lombardi 	return 0;
77727909407SAnand Kumar Santhanam }
77827909407SAnand Kumar Santhanam 
779c5614df7SBenjamin Rood struct pm8001_mpi3_phy_pg_trx_config {
780c5614df7SBenjamin Rood 	u32 LaneLosCfg;
781c5614df7SBenjamin Rood 	u32 LanePgaCfg1;
782c5614df7SBenjamin Rood 	u32 LanePisoCfg1;
783c5614df7SBenjamin Rood 	u32 LanePisoCfg2;
784c5614df7SBenjamin Rood 	u32 LanePisoCfg3;
785c5614df7SBenjamin Rood 	u32 LanePisoCfg4;
786c5614df7SBenjamin Rood 	u32 LanePisoCfg5;
787c5614df7SBenjamin Rood 	u32 LanePisoCfg6;
788c5614df7SBenjamin Rood 	u32 LaneBctCtrl;
789c5614df7SBenjamin Rood };
790c5614df7SBenjamin Rood 
791c5614df7SBenjamin Rood /**
792c5614df7SBenjamin Rood  * pm8001_get_internal_phy_settings : Retrieves the internal PHY settings
793c5614df7SBenjamin Rood  * @pm8001_ha : our adapter
794c5614df7SBenjamin Rood  * @phycfg : PHY config page to populate
795c5614df7SBenjamin Rood  */
796c5614df7SBenjamin Rood static
797c5614df7SBenjamin Rood void pm8001_get_internal_phy_settings(struct pm8001_hba_info *pm8001_ha,
798c5614df7SBenjamin Rood 		struct pm8001_mpi3_phy_pg_trx_config *phycfg)
799c5614df7SBenjamin Rood {
800c5614df7SBenjamin Rood 	phycfg->LaneLosCfg   = 0x00000132;
801c5614df7SBenjamin Rood 	phycfg->LanePgaCfg1  = 0x00203949;
802c5614df7SBenjamin Rood 	phycfg->LanePisoCfg1 = 0x000000FF;
803c5614df7SBenjamin Rood 	phycfg->LanePisoCfg2 = 0xFF000001;
804c5614df7SBenjamin Rood 	phycfg->LanePisoCfg3 = 0xE7011300;
805c5614df7SBenjamin Rood 	phycfg->LanePisoCfg4 = 0x631C40C0;
806c5614df7SBenjamin Rood 	phycfg->LanePisoCfg5 = 0xF8102036;
807c5614df7SBenjamin Rood 	phycfg->LanePisoCfg6 = 0xF74A1000;
808c5614df7SBenjamin Rood 	phycfg->LaneBctCtrl  = 0x00FB33F8;
809c5614df7SBenjamin Rood }
810c5614df7SBenjamin Rood 
811c5614df7SBenjamin Rood /**
812c5614df7SBenjamin Rood  * pm8001_get_external_phy_settings : Retrieves the external PHY settings
813c5614df7SBenjamin Rood  * @pm8001_ha : our adapter
814c5614df7SBenjamin Rood  * @phycfg : PHY config page to populate
815c5614df7SBenjamin Rood  */
816c5614df7SBenjamin Rood static
817c5614df7SBenjamin Rood void pm8001_get_external_phy_settings(struct pm8001_hba_info *pm8001_ha,
818c5614df7SBenjamin Rood 		struct pm8001_mpi3_phy_pg_trx_config *phycfg)
819c5614df7SBenjamin Rood {
820c5614df7SBenjamin Rood 	phycfg->LaneLosCfg   = 0x00000132;
821c5614df7SBenjamin Rood 	phycfg->LanePgaCfg1  = 0x00203949;
822c5614df7SBenjamin Rood 	phycfg->LanePisoCfg1 = 0x000000FF;
823c5614df7SBenjamin Rood 	phycfg->LanePisoCfg2 = 0xFF000001;
824c5614df7SBenjamin Rood 	phycfg->LanePisoCfg3 = 0xE7011300;
825c5614df7SBenjamin Rood 	phycfg->LanePisoCfg4 = 0x63349140;
826c5614df7SBenjamin Rood 	phycfg->LanePisoCfg5 = 0xF8102036;
827c5614df7SBenjamin Rood 	phycfg->LanePisoCfg6 = 0xF80D9300;
828c5614df7SBenjamin Rood 	phycfg->LaneBctCtrl  = 0x00FB33F8;
829c5614df7SBenjamin Rood }
830c5614df7SBenjamin Rood 
831c5614df7SBenjamin Rood /**
832c5614df7SBenjamin Rood  * pm8001_get_phy_mask : Retrieves the mask that denotes if a PHY is int/ext
833c5614df7SBenjamin Rood  * @pm8001_ha : our adapter
834c5614df7SBenjamin Rood  * @phymask : The PHY mask
835c5614df7SBenjamin Rood  */
836c5614df7SBenjamin Rood static
837c5614df7SBenjamin Rood void pm8001_get_phy_mask(struct pm8001_hba_info *pm8001_ha, int *phymask)
838c5614df7SBenjamin Rood {
839c5614df7SBenjamin Rood 	switch (pm8001_ha->pdev->subsystem_device) {
840c5614df7SBenjamin Rood 	case 0x0070: /* H1280 - 8 external 0 internal */
841c5614df7SBenjamin Rood 	case 0x0072: /* H12F0 - 16 external 0 internal */
842c5614df7SBenjamin Rood 		*phymask = 0x0000;
843c5614df7SBenjamin Rood 		break;
844c5614df7SBenjamin Rood 
845c5614df7SBenjamin Rood 	case 0x0071: /* H1208 - 0 external 8 internal */
846c5614df7SBenjamin Rood 	case 0x0073: /* H120F - 0 external 16 internal */
847c5614df7SBenjamin Rood 		*phymask = 0xFFFF;
848c5614df7SBenjamin Rood 		break;
849c5614df7SBenjamin Rood 
850c5614df7SBenjamin Rood 	case 0x0080: /* H1244 - 4 external 4 internal */
851c5614df7SBenjamin Rood 		*phymask = 0x00F0;
852c5614df7SBenjamin Rood 		break;
853c5614df7SBenjamin Rood 
854c5614df7SBenjamin Rood 	case 0x0081: /* H1248 - 4 external 8 internal */
855c5614df7SBenjamin Rood 		*phymask = 0x0FF0;
856c5614df7SBenjamin Rood 		break;
857c5614df7SBenjamin Rood 
858c5614df7SBenjamin Rood 	case 0x0082: /* H1288 - 8 external 8 internal */
859c5614df7SBenjamin Rood 		*phymask = 0xFF00;
860c5614df7SBenjamin Rood 		break;
861c5614df7SBenjamin Rood 
862c5614df7SBenjamin Rood 	default:
8631b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, INIT,
8641b5d2793SJoe Perches 			   "Unknown subsystem device=0x%.04x\n",
8651b5d2793SJoe Perches 			   pm8001_ha->pdev->subsystem_device);
866c5614df7SBenjamin Rood 	}
867c5614df7SBenjamin Rood }
868c5614df7SBenjamin Rood 
869c5614df7SBenjamin Rood /**
870bd1050e1SLee Jones  * pm8001_set_phy_settings_ven_117c_12G() : Configure ATTO 12Gb PHY settings
871c5614df7SBenjamin Rood  * @pm8001_ha : our adapter
872c5614df7SBenjamin Rood  */
873c5614df7SBenjamin Rood static
874c5614df7SBenjamin Rood int pm8001_set_phy_settings_ven_117c_12G(struct pm8001_hba_info *pm8001_ha)
875c5614df7SBenjamin Rood {
876c5614df7SBenjamin Rood 	struct pm8001_mpi3_phy_pg_trx_config phycfg_int;
877c5614df7SBenjamin Rood 	struct pm8001_mpi3_phy_pg_trx_config phycfg_ext;
878c5614df7SBenjamin Rood 	int phymask = 0;
879c5614df7SBenjamin Rood 	int i = 0;
880c5614df7SBenjamin Rood 
881c5614df7SBenjamin Rood 	memset(&phycfg_int, 0, sizeof(phycfg_int));
882c5614df7SBenjamin Rood 	memset(&phycfg_ext, 0, sizeof(phycfg_ext));
883c5614df7SBenjamin Rood 
884c5614df7SBenjamin Rood 	pm8001_get_internal_phy_settings(pm8001_ha, &phycfg_int);
885c5614df7SBenjamin Rood 	pm8001_get_external_phy_settings(pm8001_ha, &phycfg_ext);
886c5614df7SBenjamin Rood 	pm8001_get_phy_mask(pm8001_ha, &phymask);
887c5614df7SBenjamin Rood 
888c5614df7SBenjamin Rood 	for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
889c5614df7SBenjamin Rood 		if (phymask & (1 << i)) {/* Internal PHY */
890c5614df7SBenjamin Rood 			pm8001_set_phy_profile_single(pm8001_ha, i,
891c5614df7SBenjamin Rood 					sizeof(phycfg_int) / sizeof(u32),
892c5614df7SBenjamin Rood 					(u32 *)&phycfg_int);
893c5614df7SBenjamin Rood 
894c5614df7SBenjamin Rood 		} else { /* External PHY */
895c5614df7SBenjamin Rood 			pm8001_set_phy_profile_single(pm8001_ha, i,
896c5614df7SBenjamin Rood 					sizeof(phycfg_ext) / sizeof(u32),
897c5614df7SBenjamin Rood 					(u32 *)&phycfg_ext);
898c5614df7SBenjamin Rood 		}
899c5614df7SBenjamin Rood 	}
900c5614df7SBenjamin Rood 
901c5614df7SBenjamin Rood 	return 0;
902c5614df7SBenjamin Rood }
903c5614df7SBenjamin Rood 
904da2dd618SBenjamin Rood /**
905da2dd618SBenjamin Rood  * pm8001_configure_phy_settings : Configures PHY settings based on vendor ID.
906da2dd618SBenjamin Rood  * @pm8001_ha : our hba.
907da2dd618SBenjamin Rood  */
908da2dd618SBenjamin Rood static int pm8001_configure_phy_settings(struct pm8001_hba_info *pm8001_ha)
909da2dd618SBenjamin Rood {
910da2dd618SBenjamin Rood 	switch (pm8001_ha->pdev->subsystem_vendor) {
911da2dd618SBenjamin Rood 	case PCI_VENDOR_ID_ATTO:
912c5614df7SBenjamin Rood 		if (pm8001_ha->pdev->device == 0x0042) /* 6Gb */
913c5614df7SBenjamin Rood 			return 0;
914c5614df7SBenjamin Rood 		else
915c5614df7SBenjamin Rood 			return pm8001_set_phy_settings_ven_117c_12G(pm8001_ha);
916c5614df7SBenjamin Rood 
917da2dd618SBenjamin Rood 	case PCI_VENDOR_ID_ADAPTEC2:
918da2dd618SBenjamin Rood 	case 0:
919da2dd618SBenjamin Rood 		return 0;
920da2dd618SBenjamin Rood 
921da2dd618SBenjamin Rood 	default:
922da2dd618SBenjamin Rood 		return pm8001_get_phy_settings_info(pm8001_ha);
923da2dd618SBenjamin Rood 	}
924da2dd618SBenjamin Rood }
925da2dd618SBenjamin Rood 
926dbf9bfe6Sjack wang #ifdef PM8001_USE_MSIX
927dbf9bfe6Sjack wang /**
928dbf9bfe6Sjack wang  * pm8001_setup_msix - enable MSI-X interrupt
929e802fc43SLee Jones  * @pm8001_ha: our ha struct.
930dbf9bfe6Sjack wang  */
9311245ee59SSakthivel K static u32 pm8001_setup_msix(struct pm8001_hba_info *pm8001_ha)
932dbf9bfe6Sjack wang {
9331245ee59SSakthivel K 	u32 number_of_intr;
93405c6c029SViswas G 	int rc, cpu_online_count;
93505c6c029SViswas G 	unsigned int allocated_irq_vectors;
9361245ee59SSakthivel K 
9371245ee59SSakthivel K 	/* SPCv controllers supports 64 msi-x */
9381245ee59SSakthivel K 	if (pm8001_ha->chip_id == chip_8001) {
9391245ee59SSakthivel K 		number_of_intr = 1;
9401245ee59SSakthivel K 	} else {
9411245ee59SSakthivel K 		number_of_intr = PM8001_MAX_MSIX_VEC;
9421245ee59SSakthivel K 	}
9431245ee59SSakthivel K 
94405c6c029SViswas G 	cpu_online_count = num_online_cpus();
94505c6c029SViswas G 	number_of_intr = min_t(int, cpu_online_count, number_of_intr);
946a76037ffSChristoph Hellwig 	rc = pci_alloc_irq_vectors(pm8001_ha->pdev, number_of_intr,
947a76037ffSChristoph Hellwig 			number_of_intr, PCI_IRQ_MSIX);
94805c6c029SViswas G 	allocated_irq_vectors = rc;
949a76037ffSChristoph Hellwig 	if (rc < 0)
950b4d511e5SAlexander Gordeev 		return rc;
95105c6c029SViswas G 
95205c6c029SViswas G 	/* Assigns the number of interrupts */
95305c6c029SViswas G 	number_of_intr = min_t(int, allocated_irq_vectors, number_of_intr);
954a76037ffSChristoph Hellwig 	pm8001_ha->number_of_intr = number_of_intr;
9551245ee59SSakthivel K 
95605c6c029SViswas G 	/* Maximum queue number updating in HBA structure */
95705c6c029SViswas G 	pm8001_ha->max_q_num = number_of_intr;
95805c6c029SViswas G 
9591b5d2793SJoe Perches 	pm8001_dbg(pm8001_ha, INIT,
960a76037ffSChristoph Hellwig 		   "pci_alloc_irq_vectors request ret:%d no of intr %d\n",
9611b5d2793SJoe Perches 		   rc, pm8001_ha->number_of_intr);
962d384be6eSVikram Auradkar 	return 0;
963d384be6eSVikram Auradkar }
9641245ee59SSakthivel K 
965d384be6eSVikram Auradkar static u32 pm8001_request_msix(struct pm8001_hba_info *pm8001_ha)
966d384be6eSVikram Auradkar {
967d384be6eSVikram Auradkar 	u32 i = 0, j = 0;
968d384be6eSVikram Auradkar 	int flag = 0, rc = 0;
969c2255eceSArnd Bergmann 	int nr_irqs = pm8001_ha->number_of_intr;
970d384be6eSVikram Auradkar 
971d384be6eSVikram Auradkar 	if (pm8001_ha->chip_id != chip_8001)
972d384be6eSVikram Auradkar 		flag &= ~IRQF_SHARED;
973d384be6eSVikram Auradkar 
9741b5d2793SJoe Perches 	pm8001_dbg(pm8001_ha, INIT,
9751b5d2793SJoe Perches 		   "pci_enable_msix request number of intr %d\n",
9761b5d2793SJoe Perches 		   pm8001_ha->number_of_intr);
977d384be6eSVikram Auradkar 
978c2255eceSArnd Bergmann 	if (nr_irqs > ARRAY_SIZE(pm8001_ha->intr_drvname))
979c2255eceSArnd Bergmann 		nr_irqs = ARRAY_SIZE(pm8001_ha->intr_drvname);
980c2255eceSArnd Bergmann 
981c2255eceSArnd Bergmann 	for (i = 0; i < nr_irqs; i++) {
98272954936SVikram Auradkar 		snprintf(pm8001_ha->intr_drvname[i],
98372954936SVikram Auradkar 			sizeof(pm8001_ha->intr_drvname[0]),
98472954936SVikram Auradkar 			"%s-%d", pm8001_ha->name, i);
9856cd60b37SNikith Ganigarakoppal 		pm8001_ha->irq_vector[i].irq_id = i;
9866cd60b37SNikith Ganigarakoppal 		pm8001_ha->irq_vector[i].drv_inst = pm8001_ha;
9876cd60b37SNikith Ganigarakoppal 
988a76037ffSChristoph Hellwig 		rc = request_irq(pci_irq_vector(pm8001_ha->pdev, i),
9891245ee59SSakthivel K 			pm8001_interrupt_handler_msix, flag,
99072954936SVikram Auradkar 			pm8001_ha->intr_drvname[i],
99172954936SVikram Auradkar 			&(pm8001_ha->irq_vector[i]));
9925607de73SAlexander Gordeev 		if (rc) {
993b4d511e5SAlexander Gordeev 			for (j = 0; j < i; j++) {
994a76037ffSChristoph Hellwig 				free_irq(pci_irq_vector(pm8001_ha->pdev, i),
9956cd60b37SNikith Ganigarakoppal 					&(pm8001_ha->irq_vector[i]));
996b4d511e5SAlexander Gordeev 			}
997a76037ffSChristoph Hellwig 			pci_free_irq_vectors(pm8001_ha->pdev);
998dbf9bfe6Sjack wang 			break;
999dbf9bfe6Sjack wang 		}
1000dbf9bfe6Sjack wang 	}
1001b4d511e5SAlexander Gordeev 
1002dbf9bfe6Sjack wang 	return rc;
1003dbf9bfe6Sjack wang }
1004dbf9bfe6Sjack wang #endif
1005dbf9bfe6Sjack wang 
1006d384be6eSVikram Auradkar static u32 pm8001_setup_irq(struct pm8001_hba_info *pm8001_ha)
1007d384be6eSVikram Auradkar {
1008d384be6eSVikram Auradkar 	struct pci_dev *pdev;
1009d384be6eSVikram Auradkar 
1010d384be6eSVikram Auradkar 	pdev = pm8001_ha->pdev;
1011d384be6eSVikram Auradkar 
1012d384be6eSVikram Auradkar #ifdef PM8001_USE_MSIX
1013d384be6eSVikram Auradkar 	if (pci_find_capability(pdev, PCI_CAP_ID_MSIX))
1014d384be6eSVikram Auradkar 		return pm8001_setup_msix(pm8001_ha);
10151b5d2793SJoe Perches 	pm8001_dbg(pm8001_ha, INIT, "MSIX not supported!!!\n");
1016d384be6eSVikram Auradkar #endif
1017d384be6eSVikram Auradkar 	return 0;
1018d384be6eSVikram Auradkar }
1019d384be6eSVikram Auradkar 
1020dbf9bfe6Sjack wang /**
1021dbf9bfe6Sjack wang  * pm8001_request_irq - register interrupt
1022e802fc43SLee Jones  * @pm8001_ha: our ha struct.
1023dbf9bfe6Sjack wang  */
1024dbf9bfe6Sjack wang static u32 pm8001_request_irq(struct pm8001_hba_info *pm8001_ha)
1025dbf9bfe6Sjack wang {
1026dbf9bfe6Sjack wang 	struct pci_dev *pdev;
102797ee2088Sjack_wang 	int rc;
1028dbf9bfe6Sjack wang 
1029dbf9bfe6Sjack wang 	pdev = pm8001_ha->pdev;
1030dbf9bfe6Sjack wang 
1031dbf9bfe6Sjack wang #ifdef PM8001_USE_MSIX
1032c913df3fSBenjamin Rood 	if (pdev->msix_cap && pci_msi_enabled())
1033d384be6eSVikram Auradkar 		return pm8001_request_msix(pm8001_ha);
10341245ee59SSakthivel K 	else {
10351b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, INIT, "MSIX not supported!!!\n");
1036dbf9bfe6Sjack wang 		goto intx;
10371245ee59SSakthivel K 	}
1038dbf9bfe6Sjack wang #endif
1039dbf9bfe6Sjack wang 
1040dbf9bfe6Sjack wang intx:
1041b595076aSUwe Kleine-König 	/* initialize the INT-X interrupt */
1042c913df3fSBenjamin Rood 	pm8001_ha->irq_vector[0].irq_id = 0;
1043c913df3fSBenjamin Rood 	pm8001_ha->irq_vector[0].drv_inst = pm8001_ha;
10441245ee59SSakthivel K 	rc = request_irq(pdev->irq, pm8001_interrupt_handler_intx, IRQF_SHARED,
104572954936SVikram Auradkar 		pm8001_ha->name, SHOST_TO_SAS_HA(pm8001_ha->shost));
1046dbf9bfe6Sjack wang 	return rc;
1047dbf9bfe6Sjack wang }
1048dbf9bfe6Sjack wang 
1049dbf9bfe6Sjack wang /**
1050dbf9bfe6Sjack wang  * pm8001_pci_probe - probe supported device
1051dbf9bfe6Sjack wang  * @pdev: pci device which kernel has been prepared for.
1052dbf9bfe6Sjack wang  * @ent: pci device id
1053dbf9bfe6Sjack wang  *
1054dbf9bfe6Sjack wang  * This function is the main initialization function, when register a new
1055dbf9bfe6Sjack wang  * pci driver it is invoked, all struct an hardware initilization should be done
1056dbf9bfe6Sjack wang  * here, also, register interrupt
1057dbf9bfe6Sjack wang  */
10586f039790SGreg Kroah-Hartman static int pm8001_pci_probe(struct pci_dev *pdev,
1059dbf9bfe6Sjack wang 			    const struct pci_device_id *ent)
1060dbf9bfe6Sjack wang {
1061dbf9bfe6Sjack wang 	unsigned int rc;
1062dbf9bfe6Sjack wang 	u32	pci_reg;
10631245ee59SSakthivel K 	u8	i = 0;
1064dbf9bfe6Sjack wang 	struct pm8001_hba_info *pm8001_ha;
1065dbf9bfe6Sjack wang 	struct Scsi_Host *shost = NULL;
1066dbf9bfe6Sjack wang 	const struct pm8001_chip_info *chip;
1067b40f2882SPeter Chang 	struct sas_ha_struct *sha;
1068dbf9bfe6Sjack wang 
1069dbf9bfe6Sjack wang 	dev_printk(KERN_INFO, &pdev->dev,
1070a70b8fc3SSakthivel K 		"pm80xx: driver version %s\n", DRV_VERSION);
1071dbf9bfe6Sjack wang 	rc = pci_enable_device(pdev);
1072dbf9bfe6Sjack wang 	if (rc)
1073dbf9bfe6Sjack wang 		goto err_out_enable;
1074dbf9bfe6Sjack wang 	pci_set_master(pdev);
1075dbf9bfe6Sjack wang 	/*
1076dbf9bfe6Sjack wang 	 * Enable pci slot busmaster by setting pci command register.
1077dbf9bfe6Sjack wang 	 * This is required by FW for Cyclone card.
1078dbf9bfe6Sjack wang 	 */
1079dbf9bfe6Sjack wang 
1080dbf9bfe6Sjack wang 	pci_read_config_dword(pdev, PCI_COMMAND, &pci_reg);
1081dbf9bfe6Sjack wang 	pci_reg |= 0x157;
1082dbf9bfe6Sjack wang 	pci_write_config_dword(pdev, PCI_COMMAND, pci_reg);
1083dbf9bfe6Sjack wang 	rc = pci_request_regions(pdev, DRV_NAME);
1084dbf9bfe6Sjack wang 	if (rc)
1085dbf9bfe6Sjack wang 		goto err_out_disable;
1086dbf9bfe6Sjack wang 	rc = pci_go_44(pdev);
1087dbf9bfe6Sjack wang 	if (rc)
1088dbf9bfe6Sjack wang 		goto err_out_regions;
1089dbf9bfe6Sjack wang 
1090dbf9bfe6Sjack wang 	shost = scsi_host_alloc(&pm8001_sht, sizeof(void *));
1091dbf9bfe6Sjack wang 	if (!shost) {
1092dbf9bfe6Sjack wang 		rc = -ENOMEM;
1093dbf9bfe6Sjack wang 		goto err_out_regions;
1094dbf9bfe6Sjack wang 	}
1095dbf9bfe6Sjack wang 	chip = &pm8001_chips[ent->driver_data];
1096b40f2882SPeter Chang 	sha = kzalloc(sizeof(struct sas_ha_struct), GFP_KERNEL);
1097b40f2882SPeter Chang 	if (!sha) {
1098dbf9bfe6Sjack wang 		rc = -ENOMEM;
1099dbf9bfe6Sjack wang 		goto err_out_free_host;
1100dbf9bfe6Sjack wang 	}
1101b40f2882SPeter Chang 	SHOST_TO_SAS_HA(shost) = sha;
1102dbf9bfe6Sjack wang 
1103dbf9bfe6Sjack wang 	rc = pm8001_prep_sas_ha_init(shost, chip);
1104dbf9bfe6Sjack wang 	if (rc) {
1105dbf9bfe6Sjack wang 		rc = -ENOMEM;
1106dbf9bfe6Sjack wang 		goto err_out_free;
1107dbf9bfe6Sjack wang 	}
1108dbf9bfe6Sjack wang 	pci_set_drvdata(pdev, SHOST_TO_SAS_HA(shost));
1109e590adfdSSakthivel K 	/* ent->driver variable is used to differentiate between controllers */
1110e590adfdSSakthivel K 	pm8001_ha = pm8001_pci_alloc(pdev, ent, shost);
1111dbf9bfe6Sjack wang 	if (!pm8001_ha) {
1112dbf9bfe6Sjack wang 		rc = -ENOMEM;
1113dbf9bfe6Sjack wang 		goto err_out_free;
1114dbf9bfe6Sjack wang 	}
1115b40f2882SPeter Chang 
1116f5860992SSakthivel K 	PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha);
1117dbf9bfe6Sjack wang 	rc = PM8001_CHIP_DISP->chip_init(pm8001_ha);
1118a70b8fc3SSakthivel K 	if (rc) {
11191b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, FAIL,
11201b5d2793SJoe Perches 			   "chip_init failed [ret: %d]\n", rc);
1121dbf9bfe6Sjack wang 		goto err_out_ha_free;
1122a70b8fc3SSakthivel K 	}
1123dbf9bfe6Sjack wang 
11245a141315SViswas G 	rc = pm8001_init_ccb_tag(pm8001_ha, shost, pdev);
11255a141315SViswas G 	if (rc)
11265a141315SViswas G 		goto err_out_enable;
11275a141315SViswas G 
1128dbf9bfe6Sjack wang 	rc = scsi_add_host(shost, &pdev->dev);
1129dbf9bfe6Sjack wang 	if (rc)
1130dbf9bfe6Sjack wang 		goto err_out_ha_free;
1131dbf9bfe6Sjack wang 
1132f74cf271SSakthivel K 	PM8001_CHIP_DISP->interrupt_enable(pm8001_ha, 0);
11331245ee59SSakthivel K 	if (pm8001_ha->chip_id != chip_8001) {
11341245ee59SSakthivel K 		for (i = 1; i < pm8001_ha->number_of_intr; i++)
11351245ee59SSakthivel K 			PM8001_CHIP_DISP->interrupt_enable(pm8001_ha, i);
1136a6cb3d01SSakthivel K 		/* setup thermal configuration. */
1137a6cb3d01SSakthivel K 		pm80xx_set_thermal_config(pm8001_ha);
11381245ee59SSakthivel K 	}
11391245ee59SSakthivel K 
1140dbf9bfe6Sjack wang 	pm8001_init_sas_add(pm8001_ha);
114127909407SAnand Kumar Santhanam 	/* phy setting support for motherboard controller */
114297031ccfSZhang Qilong 	rc = pm8001_configure_phy_settings(pm8001_ha);
114397031ccfSZhang Qilong 	if (rc)
1144f2c6f180SMaurizio Lombardi 		goto err_out_shost;
1145da2dd618SBenjamin Rood 
1146dbf9bfe6Sjack wang 	pm8001_post_sas_ha_init(shost, chip);
1147dbf9bfe6Sjack wang 	rc = sas_register_ha(SHOST_TO_SAS_HA(shost));
1148b40f2882SPeter Chang 	if (rc) {
11491b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, FAIL,
11501b5d2793SJoe Perches 			   "sas_register_ha failed [ret: %d]\n", rc);
1151dbf9bfe6Sjack wang 		goto err_out_shost;
1152b40f2882SPeter Chang 	}
1153b40f2882SPeter Chang 	list_add_tail(&pm8001_ha->list, &hba_list);
1154dbf9bfe6Sjack wang 	scsi_scan_host(pm8001_ha->shost);
1155cd135754SDeepak Ukey 	pm8001_ha->flags = PM8001F_RUN_TIME;
1156dbf9bfe6Sjack wang 	return 0;
1157dbf9bfe6Sjack wang 
1158dbf9bfe6Sjack wang err_out_shost:
1159dbf9bfe6Sjack wang 	scsi_remove_host(pm8001_ha->shost);
1160dbf9bfe6Sjack wang err_out_ha_free:
1161dbf9bfe6Sjack wang 	pm8001_free(pm8001_ha);
1162dbf9bfe6Sjack wang err_out_free:
1163b40f2882SPeter Chang 	kfree(sha);
1164dbf9bfe6Sjack wang err_out_free_host:
1165bc1371c1SPan Bian 	scsi_host_put(shost);
1166dbf9bfe6Sjack wang err_out_regions:
1167dbf9bfe6Sjack wang 	pci_release_regions(pdev);
1168dbf9bfe6Sjack wang err_out_disable:
1169dbf9bfe6Sjack wang 	pci_disable_device(pdev);
1170dbf9bfe6Sjack wang err_out_enable:
1171dbf9bfe6Sjack wang 	return rc;
1172dbf9bfe6Sjack wang }
1173dbf9bfe6Sjack wang 
11745a141315SViswas G /*
11755a141315SViswas G  * pm8001_init_ccb_tag - allocate memory to CCB and tag.
11765a141315SViswas G  * @pm8001_ha: our hba card information.
11775a141315SViswas G  * @shost: scsi host which has been allocated outside.
11785a141315SViswas G  */
11795a141315SViswas G static int
11805a141315SViswas G pm8001_init_ccb_tag(struct pm8001_hba_info *pm8001_ha, struct Scsi_Host *shost,
11815a141315SViswas G 			struct pci_dev *pdev)
11825a141315SViswas G {
11835a141315SViswas G 	int i = 0;
11845a141315SViswas G 	u32 max_out_io, ccb_count;
11855a141315SViswas G 	u32 can_queue;
11865a141315SViswas G 
11875a141315SViswas G 	max_out_io = pm8001_ha->main_cfg_tbl.pm80xx_tbl.max_out_io;
11885a141315SViswas G 	ccb_count = min_t(int, PM8001_MAX_CCB, max_out_io);
11895a141315SViswas G 
11905a141315SViswas G 	/* Update to the scsi host*/
11915a141315SViswas G 	can_queue = ccb_count - PM8001_RESERVE_SLOT;
11925a141315SViswas G 	shost->can_queue = can_queue;
11935a141315SViswas G 
11945a141315SViswas G 	pm8001_ha->tags = kzalloc(ccb_count, GFP_KERNEL);
11955a141315SViswas G 	if (!pm8001_ha->tags)
11965a141315SViswas G 		goto err_out;
11975a141315SViswas G 
11985a141315SViswas G 	/* Memory region for ccb_info*/
119927a34943SXu Wang 	pm8001_ha->ccb_info =
12005a141315SViswas G 		kcalloc(ccb_count, sizeof(struct pm8001_ccb_info), GFP_KERNEL);
12015a141315SViswas G 	if (!pm8001_ha->ccb_info) {
12021b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, FAIL,
12031b5d2793SJoe Perches 			   "Unable to allocate memory for ccb\n");
12045a141315SViswas G 		goto err_out_noccb;
12055a141315SViswas G 	}
12065a141315SViswas G 	for (i = 0; i < ccb_count; i++) {
12078e60a7deSChristophe JAILLET 		pm8001_ha->ccb_info[i].buf_prd = dma_alloc_coherent(&pdev->dev,
12085a141315SViswas G 				sizeof(struct pm8001_prd) * PM8001_MAX_DMA_SG,
12098e60a7deSChristophe JAILLET 				&pm8001_ha->ccb_info[i].ccb_dma_handle,
12108e60a7deSChristophe JAILLET 				GFP_KERNEL);
12115a141315SViswas G 		if (!pm8001_ha->ccb_info[i].buf_prd) {
12121b5d2793SJoe Perches 			pm8001_dbg(pm8001_ha, FAIL,
12138e60a7deSChristophe JAILLET 				   "ccb prd memory allocation error\n");
12145a141315SViswas G 			goto err_out;
12155a141315SViswas G 		}
12165a141315SViswas G 		pm8001_ha->ccb_info[i].task = NULL;
12175a141315SViswas G 		pm8001_ha->ccb_info[i].ccb_tag = 0xffffffff;
12185a141315SViswas G 		pm8001_ha->ccb_info[i].device = NULL;
12195a141315SViswas G 		++pm8001_ha->tags_num;
12205a141315SViswas G 	}
12215a141315SViswas G 	return 0;
12225a141315SViswas G 
12235a141315SViswas G err_out_noccb:
12245a141315SViswas G 	kfree(pm8001_ha->devices);
12255a141315SViswas G err_out:
12265a141315SViswas G 	return -ENOMEM;
12275a141315SViswas G }
12285a141315SViswas G 
12296f039790SGreg Kroah-Hartman static void pm8001_pci_remove(struct pci_dev *pdev)
1230dbf9bfe6Sjack wang {
1231dbf9bfe6Sjack wang 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
1232dbf9bfe6Sjack wang 	struct pm8001_hba_info *pm8001_ha;
12336cd60b37SNikith Ganigarakoppal 	int i, j;
1234dbf9bfe6Sjack wang 	pm8001_ha = sha->lldd_ha;
1235dbf9bfe6Sjack wang 	sas_unregister_ha(sha);
1236dbf9bfe6Sjack wang 	sas_remove_host(pm8001_ha->shost);
1237dbf9bfe6Sjack wang 	list_del(&pm8001_ha->list);
12381245ee59SSakthivel K 	PM8001_CHIP_DISP->interrupt_disable(pm8001_ha, 0xFF);
1239f5860992SSakthivel K 	PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha);
1240dbf9bfe6Sjack wang 
1241dbf9bfe6Sjack wang #ifdef PM8001_USE_MSIX
1242dbf9bfe6Sjack wang 	for (i = 0; i < pm8001_ha->number_of_intr; i++)
1243a76037ffSChristoph Hellwig 		synchronize_irq(pci_irq_vector(pdev, i));
1244dbf9bfe6Sjack wang 	for (i = 0; i < pm8001_ha->number_of_intr; i++)
1245a76037ffSChristoph Hellwig 		free_irq(pci_irq_vector(pdev, i), &pm8001_ha->irq_vector[i]);
1246a76037ffSChristoph Hellwig 	pci_free_irq_vectors(pdev);
1247dbf9bfe6Sjack wang #else
1248dbf9bfe6Sjack wang 	free_irq(pm8001_ha->irq, sha);
1249dbf9bfe6Sjack wang #endif
1250dbf9bfe6Sjack wang #ifdef PM8001_USE_TASKLET
12516cd60b37SNikith Ganigarakoppal 	/* For non-msix and msix interrupts */
1252c913df3fSBenjamin Rood 	if ((!pdev->msix_cap || !pci_msi_enabled()) ||
1253c913df3fSBenjamin Rood 	    (pm8001_ha->chip_id == chip_8001))
12546cd60b37SNikith Ganigarakoppal 		tasklet_kill(&pm8001_ha->tasklet[0]);
12556cd60b37SNikith Ganigarakoppal 	else
12566cd60b37SNikith Ganigarakoppal 		for (j = 0; j < PM8001_MAX_MSIX_VEC; j++)
12576cd60b37SNikith Ganigarakoppal 			tasklet_kill(&pm8001_ha->tasklet[j]);
1258dbf9bfe6Sjack wang #endif
1259bc1371c1SPan Bian 	scsi_host_put(pm8001_ha->shost);
1260dbf9bfe6Sjack wang 	pm8001_free(pm8001_ha);
1261dbf9bfe6Sjack wang 	kfree(sha->sas_phy);
1262dbf9bfe6Sjack wang 	kfree(sha->sas_port);
1263dbf9bfe6Sjack wang 	kfree(sha);
1264dbf9bfe6Sjack wang 	pci_release_regions(pdev);
1265dbf9bfe6Sjack wang 	pci_disable_device(pdev);
1266dbf9bfe6Sjack wang }
1267dbf9bfe6Sjack wang 
1268dbf9bfe6Sjack wang /**
1269dbf9bfe6Sjack wang  * pm8001_pci_suspend - power management suspend main entry point
127047c37c4dSVaibhav Gupta  * @dev: Device struct
1271dbf9bfe6Sjack wang  *
1272dbf9bfe6Sjack wang  * Returns 0 success, anything else error.
1273dbf9bfe6Sjack wang  */
127447c37c4dSVaibhav Gupta static int __maybe_unused pm8001_pci_suspend(struct device *dev)
1275dbf9bfe6Sjack wang {
127647c37c4dSVaibhav Gupta 	struct pci_dev *pdev = to_pci_dev(dev);
1277dbf9bfe6Sjack wang 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
127847c37c4dSVaibhav Gupta 	struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
12796cd60b37SNikith Ganigarakoppal 	int  i, j;
12809f176099SBradley Grove 	sas_suspend_ha(sha);
1281429305e4STejun Heo 	flush_workqueue(pm8001_wq);
1282dbf9bfe6Sjack wang 	scsi_block_requests(pm8001_ha->shost);
1283c8a2ba3fSYijing Wang 	if (!pdev->pm_cap) {
128447c37c4dSVaibhav Gupta 		dev_err(dev, " PCI PM not supported\n");
1285dbf9bfe6Sjack wang 		return -ENODEV;
1286dbf9bfe6Sjack wang 	}
12871245ee59SSakthivel K 	PM8001_CHIP_DISP->interrupt_disable(pm8001_ha, 0xFF);
1288f5860992SSakthivel K 	PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha);
1289dbf9bfe6Sjack wang #ifdef PM8001_USE_MSIX
1290dbf9bfe6Sjack wang 	for (i = 0; i < pm8001_ha->number_of_intr; i++)
1291a76037ffSChristoph Hellwig 		synchronize_irq(pci_irq_vector(pdev, i));
1292dbf9bfe6Sjack wang 	for (i = 0; i < pm8001_ha->number_of_intr; i++)
1293a76037ffSChristoph Hellwig 		free_irq(pci_irq_vector(pdev, i), &pm8001_ha->irq_vector[i]);
1294a76037ffSChristoph Hellwig 	pci_free_irq_vectors(pdev);
1295dbf9bfe6Sjack wang #else
1296dbf9bfe6Sjack wang 	free_irq(pm8001_ha->irq, sha);
1297dbf9bfe6Sjack wang #endif
1298dbf9bfe6Sjack wang #ifdef PM8001_USE_TASKLET
12996cd60b37SNikith Ganigarakoppal 	/* For non-msix and msix interrupts */
1300c913df3fSBenjamin Rood 	if ((!pdev->msix_cap || !pci_msi_enabled()) ||
1301c913df3fSBenjamin Rood 	    (pm8001_ha->chip_id == chip_8001))
13026cd60b37SNikith Ganigarakoppal 		tasklet_kill(&pm8001_ha->tasklet[0]);
13036cd60b37SNikith Ganigarakoppal 	else
13046cd60b37SNikith Ganigarakoppal 		for (j = 0; j < PM8001_MAX_MSIX_VEC; j++)
13056cd60b37SNikith Ganigarakoppal 			tasklet_kill(&pm8001_ha->tasklet[j]);
1306dbf9bfe6Sjack wang #endif
13072ce6e200SJoe Perches 	pm8001_info(pm8001_ha, "pdev=0x%p, slot=%s, entering "
130847c37c4dSVaibhav Gupta 		      "suspended state\n", pdev,
130947c37c4dSVaibhav Gupta 		      pm8001_ha->name);
1310dbf9bfe6Sjack wang 	return 0;
1311dbf9bfe6Sjack wang }
1312dbf9bfe6Sjack wang 
1313dbf9bfe6Sjack wang /**
1314dbf9bfe6Sjack wang  * pm8001_pci_resume - power management resume main entry point
131547c37c4dSVaibhav Gupta  * @dev: Device struct
1316dbf9bfe6Sjack wang  *
1317dbf9bfe6Sjack wang  * Returns 0 success, anything else error.
1318dbf9bfe6Sjack wang  */
131947c37c4dSVaibhav Gupta static int __maybe_unused pm8001_pci_resume(struct device *dev)
1320dbf9bfe6Sjack wang {
132147c37c4dSVaibhav Gupta 	struct pci_dev *pdev = to_pci_dev(dev);
1322dbf9bfe6Sjack wang 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
1323dbf9bfe6Sjack wang 	struct pm8001_hba_info *pm8001_ha;
1324dbf9bfe6Sjack wang 	int rc;
13256cd60b37SNikith Ganigarakoppal 	u8 i = 0, j;
1326dbf9bfe6Sjack wang 	u32 device_state;
13279f176099SBradley Grove 	DECLARE_COMPLETION_ONSTACK(completion);
1328dbf9bfe6Sjack wang 	pm8001_ha = sha->lldd_ha;
1329dbf9bfe6Sjack wang 	device_state = pdev->current_state;
1330dbf9bfe6Sjack wang 
13312ce6e200SJoe Perches 	pm8001_info(pm8001_ha, "pdev=0x%p, slot=%s, resuming from previous operating state [D%d]\n",
133289eddb40SJoe Perches 		      pdev, pm8001_ha->name, device_state);
1333dbf9bfe6Sjack wang 
1334dbf9bfe6Sjack wang 	rc = pci_go_44(pdev);
1335dbf9bfe6Sjack wang 	if (rc)
1336dbf9bfe6Sjack wang 		goto err_out_disable;
13379f176099SBradley Grove 	sas_prep_resume_ha(sha);
1338f5860992SSakthivel K 	/* chip soft rst only for spc */
1339f5860992SSakthivel K 	if (pm8001_ha->chip_id == chip_8001) {
1340f5860992SSakthivel K 		PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha);
13411b5d2793SJoe Perches 		pm8001_dbg(pm8001_ha, INIT, "chip soft reset successful\n");
1342f5860992SSakthivel K 	}
1343dbf9bfe6Sjack wang 	rc = PM8001_CHIP_DISP->chip_init(pm8001_ha);
1344dbf9bfe6Sjack wang 	if (rc)
1345dbf9bfe6Sjack wang 		goto err_out_disable;
13461245ee59SSakthivel K 
13471245ee59SSakthivel K 	/* disable all the interrupt bits */
13481245ee59SSakthivel K 	PM8001_CHIP_DISP->interrupt_disable(pm8001_ha, 0xFF);
13491245ee59SSakthivel K 
1350dbf9bfe6Sjack wang 	rc = pm8001_request_irq(pm8001_ha);
1351dbf9bfe6Sjack wang 	if (rc)
1352dbf9bfe6Sjack wang 		goto err_out_disable;
1353dbf9bfe6Sjack wang #ifdef PM8001_USE_TASKLET
13546cd60b37SNikith Ganigarakoppal 	/*  Tasklet for non msi-x interrupt handler */
1355c913df3fSBenjamin Rood 	if ((!pdev->msix_cap || !pci_msi_enabled()) ||
1356c913df3fSBenjamin Rood 	    (pm8001_ha->chip_id == chip_8001))
13576cd60b37SNikith Ganigarakoppal 		tasklet_init(&pm8001_ha->tasklet[0], pm8001_tasklet,
13586cd60b37SNikith Ganigarakoppal 			(unsigned long)&(pm8001_ha->irq_vector[0]));
13596cd60b37SNikith Ganigarakoppal 	else
13606cd60b37SNikith Ganigarakoppal 		for (j = 0; j < PM8001_MAX_MSIX_VEC; j++)
13616cd60b37SNikith Ganigarakoppal 			tasklet_init(&pm8001_ha->tasklet[j], pm8001_tasklet,
13626cd60b37SNikith Ganigarakoppal 				(unsigned long)&(pm8001_ha->irq_vector[j]));
1363dbf9bfe6Sjack wang #endif
1364f74cf271SSakthivel K 	PM8001_CHIP_DISP->interrupt_enable(pm8001_ha, 0);
13651245ee59SSakthivel K 	if (pm8001_ha->chip_id != chip_8001) {
13661245ee59SSakthivel K 		for (i = 1; i < pm8001_ha->number_of_intr; i++)
13671245ee59SSakthivel K 			PM8001_CHIP_DISP->interrupt_enable(pm8001_ha, i);
13681245ee59SSakthivel K 	}
1369b650a880SBenjamin Rood 
1370b650a880SBenjamin Rood 	/* Chip documentation for the 8070 and 8072 SPCv    */
1371b650a880SBenjamin Rood 	/* states that a 500ms minimum delay is required    */
1372014e8ba7SJulia Lawall 	/* before issuing commands. Otherwise, the firmware */
1373b650a880SBenjamin Rood 	/* will enter an unrecoverable state.               */
1374b650a880SBenjamin Rood 
1375b650a880SBenjamin Rood 	if (pm8001_ha->chip_id == chip_8070 ||
1376b650a880SBenjamin Rood 		pm8001_ha->chip_id == chip_8072) {
1377b650a880SBenjamin Rood 		mdelay(500);
1378b650a880SBenjamin Rood 	}
1379b650a880SBenjamin Rood 
1380b650a880SBenjamin Rood 	/* Spin up the PHYs */
1381b650a880SBenjamin Rood 
13829f176099SBradley Grove 	pm8001_ha->flags = PM8001F_RUN_TIME;
13839f176099SBradley Grove 	for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
13849f176099SBradley Grove 		pm8001_ha->phy[i].enable_completion = &completion;
13859f176099SBradley Grove 		PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
13869f176099SBradley Grove 		wait_for_completion(&completion);
13879f176099SBradley Grove 	}
13889f176099SBradley Grove 	sas_resume_ha(sha);
1389dbf9bfe6Sjack wang 	return 0;
1390dbf9bfe6Sjack wang 
1391dbf9bfe6Sjack wang err_out_disable:
1392dbf9bfe6Sjack wang 	scsi_remove_host(pm8001_ha->shost);
139347c37c4dSVaibhav Gupta 
1394dbf9bfe6Sjack wang 	return rc;
1395dbf9bfe6Sjack wang }
1396dbf9bfe6Sjack wang 
1397e5742101SSakthivel K /* update of pci device, vendor id and driver data with
1398e5742101SSakthivel K  * unique value for each of the controller
1399e5742101SSakthivel K  */
14006f039790SGreg Kroah-Hartman static struct pci_device_id pm8001_pci_table[] = {
1401e5742101SSakthivel K 	{ PCI_VDEVICE(PMC_Sierra, 0x8001), chip_8001 },
1402d8571b1eSSuresh Thiagarajan 	{ PCI_VDEVICE(PMC_Sierra, 0x8006), chip_8006 },
1403d8571b1eSSuresh Thiagarajan 	{ PCI_VDEVICE(ADAPTEC2, 0x8006), chip_8006 },
1404f49d2132SBradley Grove 	{ PCI_VDEVICE(ATTO, 0x0042), chip_8001 },
1405e5742101SSakthivel K 	/* Support for SPC/SPCv/SPCve controllers */
1406e5742101SSakthivel K 	{ PCI_VDEVICE(ADAPTEC2, 0x8001), chip_8001 },
1407e5742101SSakthivel K 	{ PCI_VDEVICE(PMC_Sierra, 0x8008), chip_8008 },
1408e5742101SSakthivel K 	{ PCI_VDEVICE(ADAPTEC2, 0x8008), chip_8008 },
1409e5742101SSakthivel K 	{ PCI_VDEVICE(PMC_Sierra, 0x8018), chip_8018 },
1410e5742101SSakthivel K 	{ PCI_VDEVICE(ADAPTEC2, 0x8018), chip_8018 },
1411e5742101SSakthivel K 	{ PCI_VDEVICE(PMC_Sierra, 0x8009), chip_8009 },
1412e5742101SSakthivel K 	{ PCI_VDEVICE(ADAPTEC2, 0x8009), chip_8009 },
1413e5742101SSakthivel K 	{ PCI_VDEVICE(PMC_Sierra, 0x8019), chip_8019 },
1414e5742101SSakthivel K 	{ PCI_VDEVICE(ADAPTEC2, 0x8019), chip_8019 },
1415a9a923e5SAnand Kumar Santhanam 	{ PCI_VDEVICE(PMC_Sierra, 0x8074), chip_8074 },
1416a9a923e5SAnand Kumar Santhanam 	{ PCI_VDEVICE(ADAPTEC2, 0x8074), chip_8074 },
1417a9a923e5SAnand Kumar Santhanam 	{ PCI_VDEVICE(PMC_Sierra, 0x8076), chip_8076 },
1418a9a923e5SAnand Kumar Santhanam 	{ PCI_VDEVICE(ADAPTEC2, 0x8076), chip_8076 },
1419a9a923e5SAnand Kumar Santhanam 	{ PCI_VDEVICE(PMC_Sierra, 0x8077), chip_8077 },
1420a9a923e5SAnand Kumar Santhanam 	{ PCI_VDEVICE(ADAPTEC2, 0x8077), chip_8077 },
1421e5742101SSakthivel K 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8081,
1422e5742101SSakthivel K 		PCI_VENDOR_ID_ADAPTEC2, 0x0400, 0, 0, chip_8001 },
1423e5742101SSakthivel K 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8081,
1424e5742101SSakthivel K 		PCI_VENDOR_ID_ADAPTEC2, 0x0800, 0, 0, chip_8001 },
1425e5742101SSakthivel K 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8088,
1426e5742101SSakthivel K 		PCI_VENDOR_ID_ADAPTEC2, 0x0008, 0, 0, chip_8008 },
1427e5742101SSakthivel K 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8088,
1428e5742101SSakthivel K 		PCI_VENDOR_ID_ADAPTEC2, 0x0800, 0, 0, chip_8008 },
1429e5742101SSakthivel K 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8089,
1430e5742101SSakthivel K 		PCI_VENDOR_ID_ADAPTEC2, 0x0008, 0, 0, chip_8009 },
1431e5742101SSakthivel K 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8089,
1432e5742101SSakthivel K 		PCI_VENDOR_ID_ADAPTEC2, 0x0800, 0, 0, chip_8009 },
1433e5742101SSakthivel K 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8088,
1434e5742101SSakthivel K 		PCI_VENDOR_ID_ADAPTEC2, 0x0016, 0, 0, chip_8018 },
1435e5742101SSakthivel K 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8088,
1436e5742101SSakthivel K 		PCI_VENDOR_ID_ADAPTEC2, 0x1600, 0, 0, chip_8018 },
1437e5742101SSakthivel K 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8089,
1438e5742101SSakthivel K 		PCI_VENDOR_ID_ADAPTEC2, 0x0016, 0, 0, chip_8019 },
1439e5742101SSakthivel K 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8089,
1440e5742101SSakthivel K 		PCI_VENDOR_ID_ADAPTEC2, 0x1600, 0, 0, chip_8019 },
1441a9a923e5SAnand Kumar Santhanam 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8074,
1442a9a923e5SAnand Kumar Santhanam 		PCI_VENDOR_ID_ADAPTEC2, 0x0800, 0, 0, chip_8074 },
1443a9a923e5SAnand Kumar Santhanam 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8076,
1444a9a923e5SAnand Kumar Santhanam 		PCI_VENDOR_ID_ADAPTEC2, 0x1600, 0, 0, chip_8076 },
1445a9a923e5SAnand Kumar Santhanam 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8077,
1446a9a923e5SAnand Kumar Santhanam 		PCI_VENDOR_ID_ADAPTEC2, 0x1600, 0, 0, chip_8077 },
1447a9a923e5SAnand Kumar Santhanam 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8074,
1448a9a923e5SAnand Kumar Santhanam 		PCI_VENDOR_ID_ADAPTEC2, 0x0008, 0, 0, chip_8074 },
1449a9a923e5SAnand Kumar Santhanam 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8076,
1450a9a923e5SAnand Kumar Santhanam 		PCI_VENDOR_ID_ADAPTEC2, 0x0016, 0, 0, chip_8076 },
1451a9a923e5SAnand Kumar Santhanam 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8077,
1452a9a923e5SAnand Kumar Santhanam 		PCI_VENDOR_ID_ADAPTEC2, 0x0016, 0, 0, chip_8077 },
1453a9a923e5SAnand Kumar Santhanam 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8076,
1454a9a923e5SAnand Kumar Santhanam 		PCI_VENDOR_ID_ADAPTEC2, 0x0808, 0, 0, chip_8076 },
1455a9a923e5SAnand Kumar Santhanam 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8077,
1456a9a923e5SAnand Kumar Santhanam 		PCI_VENDOR_ID_ADAPTEC2, 0x0808, 0, 0, chip_8077 },
1457a9a923e5SAnand Kumar Santhanam 	{ PCI_VENDOR_ID_ADAPTEC2, 0x8074,
1458a9a923e5SAnand Kumar Santhanam 		PCI_VENDOR_ID_ADAPTEC2, 0x0404, 0, 0, chip_8074 },
1459b2dece48SBenjamin Rood 	{ PCI_VENDOR_ID_ATTO, 0x8070,
1460b2dece48SBenjamin Rood 		PCI_VENDOR_ID_ATTO, 0x0070, 0, 0, chip_8070 },
1461b2dece48SBenjamin Rood 	{ PCI_VENDOR_ID_ATTO, 0x8070,
1462b2dece48SBenjamin Rood 		PCI_VENDOR_ID_ATTO, 0x0071, 0, 0, chip_8070 },
1463b2dece48SBenjamin Rood 	{ PCI_VENDOR_ID_ATTO, 0x8072,
1464b2dece48SBenjamin Rood 		PCI_VENDOR_ID_ATTO, 0x0072, 0, 0, chip_8072 },
1465b2dece48SBenjamin Rood 	{ PCI_VENDOR_ID_ATTO, 0x8072,
1466b2dece48SBenjamin Rood 		PCI_VENDOR_ID_ATTO, 0x0073, 0, 0, chip_8072 },
1467b2dece48SBenjamin Rood 	{ PCI_VENDOR_ID_ATTO, 0x8070,
1468b2dece48SBenjamin Rood 		PCI_VENDOR_ID_ATTO, 0x0080, 0, 0, chip_8070 },
1469b2dece48SBenjamin Rood 	{ PCI_VENDOR_ID_ATTO, 0x8072,
1470b2dece48SBenjamin Rood 		PCI_VENDOR_ID_ATTO, 0x0081, 0, 0, chip_8072 },
1471b2dece48SBenjamin Rood 	{ PCI_VENDOR_ID_ATTO, 0x8072,
1472b2dece48SBenjamin Rood 		PCI_VENDOR_ID_ATTO, 0x0082, 0, 0, chip_8072 },
1473dbf9bfe6Sjack wang 	{} /* terminate list */
1474dbf9bfe6Sjack wang };
1475dbf9bfe6Sjack wang 
147647c37c4dSVaibhav Gupta static SIMPLE_DEV_PM_OPS(pm8001_pci_pm_ops,
147747c37c4dSVaibhav Gupta 			 pm8001_pci_suspend,
147847c37c4dSVaibhav Gupta 			 pm8001_pci_resume);
147947c37c4dSVaibhav Gupta 
1480dbf9bfe6Sjack wang static struct pci_driver pm8001_pci_driver = {
1481dbf9bfe6Sjack wang 	.name		= DRV_NAME,
1482dbf9bfe6Sjack wang 	.id_table	= pm8001_pci_table,
1483dbf9bfe6Sjack wang 	.probe		= pm8001_pci_probe,
14846f039790SGreg Kroah-Hartman 	.remove		= pm8001_pci_remove,
148547c37c4dSVaibhav Gupta 	.driver.pm	= &pm8001_pci_pm_ops,
1486dbf9bfe6Sjack wang };
1487dbf9bfe6Sjack wang 
1488dbf9bfe6Sjack wang /**
1489dbf9bfe6Sjack wang  *	pm8001_init - initialize scsi transport template
1490dbf9bfe6Sjack wang  */
1491dbf9bfe6Sjack wang static int __init pm8001_init(void)
1492dbf9bfe6Sjack wang {
1493429305e4STejun Heo 	int rc = -ENOMEM;
1494429305e4STejun Heo 
1495a70b8fc3SSakthivel K 	pm8001_wq = alloc_workqueue("pm80xx", 0, 0);
1496429305e4STejun Heo 	if (!pm8001_wq)
1497429305e4STejun Heo 		goto err;
1498429305e4STejun Heo 
1499dbf9bfe6Sjack wang 	pm8001_id = 0;
1500dbf9bfe6Sjack wang 	pm8001_stt = sas_domain_attach_transport(&pm8001_transport_ops);
1501dbf9bfe6Sjack wang 	if (!pm8001_stt)
1502429305e4STejun Heo 		goto err_wq;
1503dbf9bfe6Sjack wang 	rc = pci_register_driver(&pm8001_pci_driver);
1504dbf9bfe6Sjack wang 	if (rc)
1505429305e4STejun Heo 		goto err_tp;
1506dbf9bfe6Sjack wang 	return 0;
1507429305e4STejun Heo 
1508429305e4STejun Heo err_tp:
1509dbf9bfe6Sjack wang 	sas_release_transport(pm8001_stt);
1510429305e4STejun Heo err_wq:
1511429305e4STejun Heo 	destroy_workqueue(pm8001_wq);
1512429305e4STejun Heo err:
1513dbf9bfe6Sjack wang 	return rc;
1514dbf9bfe6Sjack wang }
1515dbf9bfe6Sjack wang 
1516dbf9bfe6Sjack wang static void __exit pm8001_exit(void)
1517dbf9bfe6Sjack wang {
1518dbf9bfe6Sjack wang 	pci_unregister_driver(&pm8001_pci_driver);
1519dbf9bfe6Sjack wang 	sas_release_transport(pm8001_stt);
1520429305e4STejun Heo 	destroy_workqueue(pm8001_wq);
1521dbf9bfe6Sjack wang }
1522dbf9bfe6Sjack wang 
1523dbf9bfe6Sjack wang module_init(pm8001_init);
1524dbf9bfe6Sjack wang module_exit(pm8001_exit);
1525dbf9bfe6Sjack wang 
1526dbf9bfe6Sjack wang MODULE_AUTHOR("Jack Wang <jack_wang@usish.com>");
1527a9a923e5SAnand Kumar Santhanam MODULE_AUTHOR("Anand Kumar Santhanam <AnandKumar.Santhanam@pmcs.com>");
1528a9a923e5SAnand Kumar Santhanam MODULE_AUTHOR("Sangeetha Gnanasekaran <Sangeetha.Gnanasekaran@pmcs.com>");
152994f33c16SNikith Ganigarakoppal MODULE_AUTHOR("Nikith Ganigarakoppal <Nikith.Ganigarakoppal@pmcs.com>");
1530e5742101SSakthivel K MODULE_DESCRIPTION(
1531db9d4034SBenjamin Rood 		"PMC-Sierra PM8001/8006/8081/8088/8089/8074/8076/8077/8070/8072 "
1532a9a923e5SAnand Kumar Santhanam 		"SAS/SATA controller driver");
1533dbf9bfe6Sjack wang MODULE_VERSION(DRV_VERSION);
1534dbf9bfe6Sjack wang MODULE_LICENSE("GPL");
1535dbf9bfe6Sjack wang MODULE_DEVICE_TABLE(pci, pm8001_pci_table);
1536dbf9bfe6Sjack wang 
1537