]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/pm8001/pm8001_init.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/hirofumi/fatfs-2.6
[karo-tx-linux.git] / drivers / scsi / pm8001 / pm8001_init.c
1 /*
2  * PMC-Sierra SPC 8001 SAS/SATA based host adapters driver
3  *
4  * Copyright (c) 2008-2009 USI Co., Ltd.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    substantially similar to the "NO WARRANTY" disclaimer below
15  *    ("Disclaimer") and any redistribution must be conditioned upon
16  *    including a substantially similar Disclaimer requirement for further
17  *    binary redistribution.
18  * 3. Neither the names of the above-listed copyright holders nor the names
19  *    of any contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * Alternatively, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") version 2 as published by the Free
24  * Software Foundation.
25  *
26  * NO WARRANTY
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGES.
38  *
39  */
40
41 #include "pm8001_sas.h"
42 #include "pm8001_chips.h"
43
44 static struct scsi_transport_template *pm8001_stt;
45
46 static const struct pm8001_chip_info pm8001_chips[] = {
47         [chip_8001] = {  8, &pm8001_8001_dispatch,},
48 };
49 static int pm8001_id;
50
51 LIST_HEAD(hba_list);
52
53 /**
54  * The main structure which LLDD must register for scsi core.
55  */
56 static struct scsi_host_template pm8001_sht = {
57         .module                 = THIS_MODULE,
58         .name                   = DRV_NAME,
59         .queuecommand           = sas_queuecommand,
60         .target_alloc           = sas_target_alloc,
61         .slave_configure        = pm8001_slave_configure,
62         .slave_destroy          = sas_slave_destroy,
63         .scan_finished          = pm8001_scan_finished,
64         .scan_start             = pm8001_scan_start,
65         .change_queue_depth     = sas_change_queue_depth,
66         .change_queue_type      = sas_change_queue_type,
67         .bios_param             = sas_bios_param,
68         .can_queue              = 1,
69         .cmd_per_lun            = 1,
70         .this_id                = -1,
71         .sg_tablesize           = SG_ALL,
72         .max_sectors            = SCSI_DEFAULT_MAX_SECTORS,
73         .use_clustering         = ENABLE_CLUSTERING,
74         .eh_device_reset_handler = sas_eh_device_reset_handler,
75         .eh_bus_reset_handler   = sas_eh_bus_reset_handler,
76         .slave_alloc            = pm8001_slave_alloc,
77         .target_destroy         = sas_target_destroy,
78         .ioctl                  = sas_ioctl,
79         .shost_attrs            = pm8001_host_attrs,
80 };
81
82 /**
83  * Sas layer call this function to execute specific task.
84  */
85 static struct sas_domain_function_template pm8001_transport_ops = {
86         .lldd_dev_found         = pm8001_dev_found,
87         .lldd_dev_gone          = pm8001_dev_gone,
88
89         .lldd_execute_task      = pm8001_queue_command,
90         .lldd_control_phy       = pm8001_phy_control,
91
92         .lldd_abort_task        = pm8001_abort_task,
93         .lldd_abort_task_set    = pm8001_abort_task_set,
94         .lldd_clear_aca         = pm8001_clear_aca,
95         .lldd_clear_task_set    = pm8001_clear_task_set,
96         .lldd_I_T_nexus_reset   = pm8001_I_T_nexus_reset,
97         .lldd_lu_reset          = pm8001_lu_reset,
98         .lldd_query_task        = pm8001_query_task,
99 };
100
101 /**
102  *pm8001_phy_init - initiate our adapter phys
103  *@pm8001_ha: our hba structure.
104  *@phy_id: phy id.
105  */
106 static void __devinit pm8001_phy_init(struct pm8001_hba_info *pm8001_ha,
107         int phy_id)
108 {
109         struct pm8001_phy *phy = &pm8001_ha->phy[phy_id];
110         struct asd_sas_phy *sas_phy = &phy->sas_phy;
111         phy->phy_state = 0;
112         phy->pm8001_ha = pm8001_ha;
113         sas_phy->enabled = (phy_id < pm8001_ha->chip->n_phy) ? 1 : 0;
114         sas_phy->class = SAS;
115         sas_phy->iproto = SAS_PROTOCOL_ALL;
116         sas_phy->tproto = 0;
117         sas_phy->type = PHY_TYPE_PHYSICAL;
118         sas_phy->role = PHY_ROLE_INITIATOR;
119         sas_phy->oob_mode = OOB_NOT_CONNECTED;
120         sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
121         sas_phy->id = phy_id;
122         sas_phy->sas_addr = &pm8001_ha->sas_addr[0];
123         sas_phy->frame_rcvd = &phy->frame_rcvd[0];
124         sas_phy->ha = (struct sas_ha_struct *)pm8001_ha->shost->hostdata;
125         sas_phy->lldd_phy = phy;
126 }
127
128 /**
129  *pm8001_free - free hba
130  *@pm8001_ha:   our hba structure.
131  *
132  */
133 static void pm8001_free(struct pm8001_hba_info *pm8001_ha)
134 {
135         int i;
136         struct pm8001_wq *wq;
137
138         if (!pm8001_ha)
139                 return;
140
141         for (i = 0; i < USI_MAX_MEMCNT; i++) {
142                 if (pm8001_ha->memoryMap.region[i].virt_ptr != NULL) {
143                         pci_free_consistent(pm8001_ha->pdev,
144                                 pm8001_ha->memoryMap.region[i].element_size,
145                                 pm8001_ha->memoryMap.region[i].virt_ptr,
146                                 pm8001_ha->memoryMap.region[i].phys_addr);
147                         }
148         }
149         PM8001_CHIP_DISP->chip_iounmap(pm8001_ha);
150         if (pm8001_ha->shost)
151                 scsi_host_put(pm8001_ha->shost);
152         list_for_each_entry(wq, &pm8001_ha->wq_list, entry)
153                 cancel_delayed_work(&wq->work_q);
154         kfree(pm8001_ha->tags);
155         kfree(pm8001_ha);
156 }
157
158 #ifdef PM8001_USE_TASKLET
159 static void pm8001_tasklet(unsigned long opaque)
160 {
161         struct pm8001_hba_info *pm8001_ha;
162         pm8001_ha = (struct pm8001_hba_info *)opaque;;
163         if (unlikely(!pm8001_ha))
164                 BUG_ON(1);
165         PM8001_CHIP_DISP->isr(pm8001_ha);
166 }
167 #endif
168
169
170  /**
171   * pm8001_interrupt - when HBA originate a interrupt,we should invoke this
172   * dispatcher to handle each case.
173   * @irq: irq number.
174   * @opaque: the passed general host adapter struct
175   */
176 static irqreturn_t pm8001_interrupt(int irq, void *opaque)
177 {
178         struct pm8001_hba_info *pm8001_ha;
179         irqreturn_t ret = IRQ_HANDLED;
180         struct sas_ha_struct *sha = opaque;
181         pm8001_ha = sha->lldd_ha;
182         if (unlikely(!pm8001_ha))
183                 return IRQ_NONE;
184         if (!PM8001_CHIP_DISP->is_our_interupt(pm8001_ha))
185                 return IRQ_NONE;
186 #ifdef PM8001_USE_TASKLET
187         tasklet_schedule(&pm8001_ha->tasklet);
188 #else
189         ret = PM8001_CHIP_DISP->isr(pm8001_ha);
190 #endif
191         return ret;
192 }
193
194 /**
195  * pm8001_alloc - initiate our hba structure and 6 DMAs area.
196  * @pm8001_ha:our hba structure.
197  *
198  */
199 static int __devinit pm8001_alloc(struct pm8001_hba_info *pm8001_ha)
200 {
201         int i;
202         spin_lock_init(&pm8001_ha->lock);
203         for (i = 0; i < pm8001_ha->chip->n_phy; i++)
204                 pm8001_phy_init(pm8001_ha, i);
205
206         pm8001_ha->tags = kzalloc(PM8001_MAX_CCB, GFP_KERNEL);
207         if (!pm8001_ha->tags)
208                 goto err_out;
209         /* MPI Memory region 1 for AAP Event Log for fw */
210         pm8001_ha->memoryMap.region[AAP1].num_elements = 1;
211         pm8001_ha->memoryMap.region[AAP1].element_size = PM8001_EVENT_LOG_SIZE;
212         pm8001_ha->memoryMap.region[AAP1].total_len = PM8001_EVENT_LOG_SIZE;
213         pm8001_ha->memoryMap.region[AAP1].alignment = 32;
214
215         /* MPI Memory region 2 for IOP Event Log for fw */
216         pm8001_ha->memoryMap.region[IOP].num_elements = 1;
217         pm8001_ha->memoryMap.region[IOP].element_size = PM8001_EVENT_LOG_SIZE;
218         pm8001_ha->memoryMap.region[IOP].total_len = PM8001_EVENT_LOG_SIZE;
219         pm8001_ha->memoryMap.region[IOP].alignment = 32;
220
221         /* MPI Memory region 3 for consumer Index of inbound queues */
222         pm8001_ha->memoryMap.region[CI].num_elements = 1;
223         pm8001_ha->memoryMap.region[CI].element_size = 4;
224         pm8001_ha->memoryMap.region[CI].total_len = 4;
225         pm8001_ha->memoryMap.region[CI].alignment = 4;
226
227         /* MPI Memory region 4 for producer Index of outbound queues */
228         pm8001_ha->memoryMap.region[PI].num_elements = 1;
229         pm8001_ha->memoryMap.region[PI].element_size = 4;
230         pm8001_ha->memoryMap.region[PI].total_len = 4;
231         pm8001_ha->memoryMap.region[PI].alignment = 4;
232
233         /* MPI Memory region 5 inbound queues */
234         pm8001_ha->memoryMap.region[IB].num_elements = 256;
235         pm8001_ha->memoryMap.region[IB].element_size = 64;
236         pm8001_ha->memoryMap.region[IB].total_len = 256 * 64;
237         pm8001_ha->memoryMap.region[IB].alignment = 64;
238
239         /* MPI Memory region 6 inbound queues */
240         pm8001_ha->memoryMap.region[OB].num_elements = 256;
241         pm8001_ha->memoryMap.region[OB].element_size = 64;
242         pm8001_ha->memoryMap.region[OB].total_len = 256 * 64;
243         pm8001_ha->memoryMap.region[OB].alignment = 64;
244
245         /* Memory region write DMA*/
246         pm8001_ha->memoryMap.region[NVMD].num_elements = 1;
247         pm8001_ha->memoryMap.region[NVMD].element_size = 4096;
248         pm8001_ha->memoryMap.region[NVMD].total_len = 4096;
249         /* Memory region for devices*/
250         pm8001_ha->memoryMap.region[DEV_MEM].num_elements = 1;
251         pm8001_ha->memoryMap.region[DEV_MEM].element_size = PM8001_MAX_DEVICES *
252                 sizeof(struct pm8001_device);
253         pm8001_ha->memoryMap.region[DEV_MEM].total_len = PM8001_MAX_DEVICES *
254                 sizeof(struct pm8001_device);
255
256         /* Memory region for ccb_info*/
257         pm8001_ha->memoryMap.region[CCB_MEM].num_elements = 1;
258         pm8001_ha->memoryMap.region[CCB_MEM].element_size = PM8001_MAX_CCB *
259                 sizeof(struct pm8001_ccb_info);
260         pm8001_ha->memoryMap.region[CCB_MEM].total_len = PM8001_MAX_CCB *
261                 sizeof(struct pm8001_ccb_info);
262
263         for (i = 0; i < USI_MAX_MEMCNT; i++) {
264                 if (pm8001_mem_alloc(pm8001_ha->pdev,
265                         &pm8001_ha->memoryMap.region[i].virt_ptr,
266                         &pm8001_ha->memoryMap.region[i].phys_addr,
267                         &pm8001_ha->memoryMap.region[i].phys_addr_hi,
268                         &pm8001_ha->memoryMap.region[i].phys_addr_lo,
269                         pm8001_ha->memoryMap.region[i].total_len,
270                         pm8001_ha->memoryMap.region[i].alignment) != 0) {
271                                 PM8001_FAIL_DBG(pm8001_ha,
272                                         pm8001_printk("Mem%d alloc failed\n",
273                                         i));
274                                 goto err_out;
275                 }
276         }
277
278         pm8001_ha->devices = pm8001_ha->memoryMap.region[DEV_MEM].virt_ptr;
279         for (i = 0; i < PM8001_MAX_DEVICES; i++) {
280                 pm8001_ha->devices[i].dev_type = NO_DEVICE;
281                 pm8001_ha->devices[i].id = i;
282                 pm8001_ha->devices[i].device_id = PM8001_MAX_DEVICES;
283                 pm8001_ha->devices[i].running_req = 0;
284         }
285         pm8001_ha->ccb_info = pm8001_ha->memoryMap.region[CCB_MEM].virt_ptr;
286         for (i = 0; i < PM8001_MAX_CCB; i++) {
287                 pm8001_ha->ccb_info[i].ccb_dma_handle =
288                         pm8001_ha->memoryMap.region[CCB_MEM].phys_addr +
289                         i * sizeof(struct pm8001_ccb_info);
290                 pm8001_ha->ccb_info[i].task = NULL;
291                 pm8001_ha->ccb_info[i].ccb_tag = 0xffffffff;
292                 pm8001_ha->ccb_info[i].device = NULL;
293                 ++pm8001_ha->tags_num;
294         }
295         pm8001_ha->flags = PM8001F_INIT_TIME;
296         /* Initialize tags */
297         pm8001_tag_init(pm8001_ha);
298         return 0;
299 err_out:
300         return 1;
301 }
302
303 /**
304  * pm8001_ioremap - remap the pci high physical address to kernal virtual
305  * address so that we can access them.
306  * @pm8001_ha:our hba structure.
307  */
308 static int pm8001_ioremap(struct pm8001_hba_info *pm8001_ha)
309 {
310         u32 bar;
311         u32 logicalBar = 0;
312         struct pci_dev *pdev;
313
314         pdev = pm8001_ha->pdev;
315         /* map pci mem (PMC pci base 0-3)*/
316         for (bar = 0; bar < 6; bar++) {
317                 /*
318                 ** logical BARs for SPC:
319                 ** bar 0 and 1 - logical BAR0
320                 ** bar 2 and 3 - logical BAR1
321                 ** bar4 - logical BAR2
322                 ** bar5 - logical BAR3
323                 ** Skip the appropriate assignments:
324                 */
325                 if ((bar == 1) || (bar == 3))
326                         continue;
327                 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
328                         pm8001_ha->io_mem[logicalBar].membase =
329                                 pci_resource_start(pdev, bar);
330                         pm8001_ha->io_mem[logicalBar].membase &=
331                                 (u32)PCI_BASE_ADDRESS_MEM_MASK;
332                         pm8001_ha->io_mem[logicalBar].memsize =
333                                 pci_resource_len(pdev, bar);
334                         pm8001_ha->io_mem[logicalBar].memvirtaddr =
335                                 ioremap(pm8001_ha->io_mem[logicalBar].membase,
336                                 pm8001_ha->io_mem[logicalBar].memsize);
337                         PM8001_INIT_DBG(pm8001_ha,
338                                 pm8001_printk("PCI: bar %d, logicalBar %d "
339                                 "virt_addr=%lx,len=%d\n", bar, logicalBar,
340                                 (unsigned long)
341                                 pm8001_ha->io_mem[logicalBar].memvirtaddr,
342                                 pm8001_ha->io_mem[logicalBar].memsize));
343                 } else {
344                         pm8001_ha->io_mem[logicalBar].membase   = 0;
345                         pm8001_ha->io_mem[logicalBar].memsize   = 0;
346                         pm8001_ha->io_mem[logicalBar].memvirtaddr = 0;
347                 }
348                 logicalBar++;
349         }
350         return 0;
351 }
352
353 /**
354  * pm8001_pci_alloc - initialize our ha card structure
355  * @pdev: pci device.
356  * @ent: ent
357  * @shost: scsi host struct which has been initialized before.
358  */
359 static struct pm8001_hba_info *__devinit
360 pm8001_pci_alloc(struct pci_dev *pdev, u32 chip_id, struct Scsi_Host *shost)
361 {
362         struct pm8001_hba_info *pm8001_ha;
363         struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
364
365
366         pm8001_ha = sha->lldd_ha;
367         if (!pm8001_ha)
368                 return NULL;
369
370         pm8001_ha->pdev = pdev;
371         pm8001_ha->dev = &pdev->dev;
372         pm8001_ha->chip_id = chip_id;
373         pm8001_ha->chip = &pm8001_chips[pm8001_ha->chip_id];
374         pm8001_ha->irq = pdev->irq;
375         pm8001_ha->sas = sha;
376         pm8001_ha->shost = shost;
377         pm8001_ha->id = pm8001_id++;
378         INIT_LIST_HEAD(&pm8001_ha->wq_list);
379         pm8001_ha->logging_level = 0x01;
380         sprintf(pm8001_ha->name, "%s%d", DRV_NAME, pm8001_ha->id);
381 #ifdef PM8001_USE_TASKLET
382         tasklet_init(&pm8001_ha->tasklet, pm8001_tasklet,
383                 (unsigned long)pm8001_ha);
384 #endif
385         pm8001_ioremap(pm8001_ha);
386         if (!pm8001_alloc(pm8001_ha))
387                 return pm8001_ha;
388         pm8001_free(pm8001_ha);
389         return NULL;
390 }
391
392 /**
393  * pci_go_44 - pm8001 specified, its DMA is 44 bit rather than 64 bit
394  * @pdev: pci device.
395  */
396 static int pci_go_44(struct pci_dev *pdev)
397 {
398         int rc;
399
400         if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(44))) {
401                 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(44));
402                 if (rc) {
403                         rc = pci_set_consistent_dma_mask(pdev,
404                                 DMA_BIT_MASK(32));
405                         if (rc) {
406                                 dev_printk(KERN_ERR, &pdev->dev,
407                                         "44-bit DMA enable failed\n");
408                                 return rc;
409                         }
410                 }
411         } else {
412                 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
413                 if (rc) {
414                         dev_printk(KERN_ERR, &pdev->dev,
415                                 "32-bit DMA enable failed\n");
416                         return rc;
417                 }
418                 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
419                 if (rc) {
420                         dev_printk(KERN_ERR, &pdev->dev,
421                                 "32-bit consistent DMA enable failed\n");
422                         return rc;
423                 }
424         }
425         return rc;
426 }
427
428 /**
429  * pm8001_prep_sas_ha_init - allocate memory in general hba struct && init them.
430  * @shost: scsi host which has been allocated outside.
431  * @chip_info: our ha struct.
432  */
433 static int __devinit pm8001_prep_sas_ha_init(struct Scsi_Host * shost,
434         const struct pm8001_chip_info *chip_info)
435 {
436         int phy_nr, port_nr;
437         struct asd_sas_phy **arr_phy;
438         struct asd_sas_port **arr_port;
439         struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
440
441         phy_nr = chip_info->n_phy;
442         port_nr = phy_nr;
443         memset(sha, 0x00, sizeof(*sha));
444         arr_phy = kcalloc(phy_nr, sizeof(void *), GFP_KERNEL);
445         if (!arr_phy)
446                 goto exit;
447         arr_port = kcalloc(port_nr, sizeof(void *), GFP_KERNEL);
448         if (!arr_port)
449                 goto exit_free2;
450
451         sha->sas_phy = arr_phy;
452         sha->sas_port = arr_port;
453         sha->lldd_ha = kzalloc(sizeof(struct pm8001_hba_info), GFP_KERNEL);
454         if (!sha->lldd_ha)
455                 goto exit_free1;
456
457         shost->transportt = pm8001_stt;
458         shost->max_id = PM8001_MAX_DEVICES;
459         shost->max_lun = 8;
460         shost->max_channel = 0;
461         shost->unique_id = pm8001_id;
462         shost->max_cmd_len = 16;
463         shost->can_queue = PM8001_CAN_QUEUE;
464         shost->cmd_per_lun = 32;
465         return 0;
466 exit_free1:
467         kfree(arr_port);
468 exit_free2:
469         kfree(arr_phy);
470 exit:
471         return -1;
472 }
473
474 /**
475  * pm8001_post_sas_ha_init - initialize general hba struct defined in libsas
476  * @shost: scsi host which has been allocated outside
477  * @chip_info: our ha struct.
478  */
479 static void  __devinit pm8001_post_sas_ha_init(struct Scsi_Host *shost,
480         const struct pm8001_chip_info *chip_info)
481 {
482         int i = 0;
483         struct pm8001_hba_info *pm8001_ha;
484         struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
485
486         pm8001_ha = sha->lldd_ha;
487         for (i = 0; i < chip_info->n_phy; i++) {
488                 sha->sas_phy[i] = &pm8001_ha->phy[i].sas_phy;
489                 sha->sas_port[i] = &pm8001_ha->port[i].sas_port;
490         }
491         sha->sas_ha_name = DRV_NAME;
492         sha->dev = pm8001_ha->dev;
493
494         sha->lldd_module = THIS_MODULE;
495         sha->sas_addr = &pm8001_ha->sas_addr[0];
496         sha->num_phys = chip_info->n_phy;
497         sha->lldd_max_execute_num = 1;
498         sha->lldd_queue_size = PM8001_CAN_QUEUE;
499         sha->core.shost = shost;
500 }
501
502 /**
503  * pm8001_init_sas_add - initialize sas address
504  * @chip_info: our ha struct.
505  *
506  * Currently we just set the fixed SAS address to our HBA,for manufacture,
507  * it should read from the EEPROM
508  */
509 static void pm8001_init_sas_add(struct pm8001_hba_info *pm8001_ha)
510 {
511         u8 i;
512 #ifdef PM8001_READ_VPD
513         DECLARE_COMPLETION_ONSTACK(completion);
514         pm8001_ha->nvmd_completion = &completion;
515         PM8001_CHIP_DISP->get_nvmd_req(pm8001_ha, 0, 0);
516         wait_for_completion(&completion);
517         for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
518                 memcpy(&pm8001_ha->phy[i].dev_sas_addr, pm8001_ha->sas_addr,
519                         SAS_ADDR_SIZE);
520                 PM8001_INIT_DBG(pm8001_ha,
521                         pm8001_printk("phy %d sas_addr = %x \n", i,
522                         (u64)pm8001_ha->phy[i].dev_sas_addr));
523         }
524 #else
525         for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
526                 pm8001_ha->phy[i].dev_sas_addr = 0x500e004010000004ULL;
527                 pm8001_ha->phy[i].dev_sas_addr =
528                         cpu_to_be64((u64)
529                                 (*(u64 *)&pm8001_ha->phy[i].dev_sas_addr));
530         }
531         memcpy(pm8001_ha->sas_addr, &pm8001_ha->phy[0].dev_sas_addr,
532                 SAS_ADDR_SIZE);
533 #endif
534 }
535
536 #ifdef PM8001_USE_MSIX
537 /**
538  * pm8001_setup_msix - enable MSI-X interrupt
539  * @chip_info: our ha struct.
540  * @irq_handler: irq_handler
541  */
542 static u32 pm8001_setup_msix(struct pm8001_hba_info *pm8001_ha,
543         irq_handler_t irq_handler)
544 {
545         u32 i = 0, j = 0;
546         u32 number_of_intr = 1;
547         int flag = 0;
548         u32 max_entry;
549         int rc;
550         max_entry = sizeof(pm8001_ha->msix_entries) /
551                 sizeof(pm8001_ha->msix_entries[0]);
552         flag |= IRQF_DISABLED;
553         for (i = 0; i < max_entry ; i++)
554                 pm8001_ha->msix_entries[i].entry = i;
555         rc = pci_enable_msix(pm8001_ha->pdev, pm8001_ha->msix_entries,
556                 number_of_intr);
557         pm8001_ha->number_of_intr = number_of_intr;
558         if (!rc) {
559                 for (i = 0; i < number_of_intr; i++) {
560                         if (request_irq(pm8001_ha->msix_entries[i].vector,
561                                 irq_handler, flag, DRV_NAME,
562                                 SHOST_TO_SAS_HA(pm8001_ha->shost))) {
563                                 for (j = 0; j < i; j++)
564                                         free_irq(
565                                         pm8001_ha->msix_entries[j].vector,
566                                         SHOST_TO_SAS_HA(pm8001_ha->shost));
567                                 pci_disable_msix(pm8001_ha->pdev);
568                                 break;
569                         }
570                 }
571         }
572         return rc;
573 }
574 #endif
575
576 /**
577  * pm8001_request_irq - register interrupt
578  * @chip_info: our ha struct.
579  */
580 static u32 pm8001_request_irq(struct pm8001_hba_info *pm8001_ha)
581 {
582         struct pci_dev *pdev;
583         irq_handler_t irq_handler = pm8001_interrupt;
584         int rc;
585
586         pdev = pm8001_ha->pdev;
587
588 #ifdef PM8001_USE_MSIX
589         if (pci_find_capability(pdev, PCI_CAP_ID_MSIX))
590                 return pm8001_setup_msix(pm8001_ha, irq_handler);
591         else
592                 goto intx;
593 #endif
594
595 intx:
596         /* intialize the INT-X interrupt */
597         rc = request_irq(pdev->irq, irq_handler, IRQF_SHARED, DRV_NAME,
598                 SHOST_TO_SAS_HA(pm8001_ha->shost));
599         return rc;
600 }
601
602 /**
603  * pm8001_pci_probe - probe supported device
604  * @pdev: pci device which kernel has been prepared for.
605  * @ent: pci device id
606  *
607  * This function is the main initialization function, when register a new
608  * pci driver it is invoked, all struct an hardware initilization should be done
609  * here, also, register interrupt
610  */
611 static int __devinit pm8001_pci_probe(struct pci_dev *pdev,
612         const struct pci_device_id *ent)
613 {
614         unsigned int rc;
615         u32     pci_reg;
616         struct pm8001_hba_info *pm8001_ha;
617         struct Scsi_Host *shost = NULL;
618         const struct pm8001_chip_info *chip;
619
620         dev_printk(KERN_INFO, &pdev->dev,
621                 "pm8001: driver version %s\n", DRV_VERSION);
622         rc = pci_enable_device(pdev);
623         if (rc)
624                 goto err_out_enable;
625         pci_set_master(pdev);
626         /*
627          * Enable pci slot busmaster by setting pci command register.
628          * This is required by FW for Cyclone card.
629          */
630
631         pci_read_config_dword(pdev, PCI_COMMAND, &pci_reg);
632         pci_reg |= 0x157;
633         pci_write_config_dword(pdev, PCI_COMMAND, pci_reg);
634         rc = pci_request_regions(pdev, DRV_NAME);
635         if (rc)
636                 goto err_out_disable;
637         rc = pci_go_44(pdev);
638         if (rc)
639                 goto err_out_regions;
640
641         shost = scsi_host_alloc(&pm8001_sht, sizeof(void *));
642         if (!shost) {
643                 rc = -ENOMEM;
644                 goto err_out_regions;
645         }
646         chip = &pm8001_chips[ent->driver_data];
647         SHOST_TO_SAS_HA(shost) =
648                 kcalloc(1, sizeof(struct sas_ha_struct), GFP_KERNEL);
649         if (!SHOST_TO_SAS_HA(shost)) {
650                 rc = -ENOMEM;
651                 goto err_out_free_host;
652         }
653
654         rc = pm8001_prep_sas_ha_init(shost, chip);
655         if (rc) {
656                 rc = -ENOMEM;
657                 goto err_out_free;
658         }
659         pci_set_drvdata(pdev, SHOST_TO_SAS_HA(shost));
660         pm8001_ha = pm8001_pci_alloc(pdev, chip_8001, shost);
661         if (!pm8001_ha) {
662                 rc = -ENOMEM;
663                 goto err_out_free;
664         }
665         list_add_tail(&pm8001_ha->list, &hba_list);
666         PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
667         rc = PM8001_CHIP_DISP->chip_init(pm8001_ha);
668         if (rc)
669                 goto err_out_ha_free;
670
671         rc = scsi_add_host(shost, &pdev->dev);
672         if (rc)
673                 goto err_out_ha_free;
674         rc = pm8001_request_irq(pm8001_ha);
675         if (rc)
676                 goto err_out_shost;
677
678         PM8001_CHIP_DISP->interrupt_enable(pm8001_ha);
679         pm8001_init_sas_add(pm8001_ha);
680         pm8001_post_sas_ha_init(shost, chip);
681         rc = sas_register_ha(SHOST_TO_SAS_HA(shost));
682         if (rc)
683                 goto err_out_shost;
684         scsi_scan_host(pm8001_ha->shost);
685         return 0;
686
687 err_out_shost:
688         scsi_remove_host(pm8001_ha->shost);
689 err_out_ha_free:
690         pm8001_free(pm8001_ha);
691 err_out_free:
692         kfree(SHOST_TO_SAS_HA(shost));
693 err_out_free_host:
694         kfree(shost);
695 err_out_regions:
696         pci_release_regions(pdev);
697 err_out_disable:
698         pci_disable_device(pdev);
699 err_out_enable:
700         return rc;
701 }
702
703 static void __devexit pm8001_pci_remove(struct pci_dev *pdev)
704 {
705         struct sas_ha_struct *sha = pci_get_drvdata(pdev);
706         struct pm8001_hba_info *pm8001_ha;
707         int i;
708         pm8001_ha = sha->lldd_ha;
709         pci_set_drvdata(pdev, NULL);
710         sas_unregister_ha(sha);
711         sas_remove_host(pm8001_ha->shost);
712         list_del(&pm8001_ha->list);
713         scsi_remove_host(pm8001_ha->shost);
714         PM8001_CHIP_DISP->interrupt_disable(pm8001_ha);
715         PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
716
717 #ifdef PM8001_USE_MSIX
718         for (i = 0; i < pm8001_ha->number_of_intr; i++)
719                 synchronize_irq(pm8001_ha->msix_entries[i].vector);
720         for (i = 0; i < pm8001_ha->number_of_intr; i++)
721                 free_irq(pm8001_ha->msix_entries[i].vector, sha);
722         pci_disable_msix(pdev);
723 #else
724         free_irq(pm8001_ha->irq, sha);
725 #endif
726 #ifdef PM8001_USE_TASKLET
727         tasklet_kill(&pm8001_ha->tasklet);
728 #endif
729         pm8001_free(pm8001_ha);
730         kfree(sha->sas_phy);
731         kfree(sha->sas_port);
732         kfree(sha);
733         pci_release_regions(pdev);
734         pci_disable_device(pdev);
735 }
736
737 /**
738  * pm8001_pci_suspend - power management suspend main entry point
739  * @pdev: PCI device struct
740  * @state: PM state change to (usually PCI_D3)
741  *
742  * Returns 0 success, anything else error.
743  */
744 static int pm8001_pci_suspend(struct pci_dev *pdev, pm_message_t state)
745 {
746         struct sas_ha_struct *sha = pci_get_drvdata(pdev);
747         struct pm8001_hba_info *pm8001_ha;
748         int i , pos;
749         u32 device_state;
750         pm8001_ha = sha->lldd_ha;
751         flush_scheduled_work();
752         scsi_block_requests(pm8001_ha->shost);
753         pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
754         if (pos == 0) {
755                 printk(KERN_ERR " PCI PM not supported\n");
756                 return -ENODEV;
757         }
758         PM8001_CHIP_DISP->interrupt_disable(pm8001_ha);
759         PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
760 #ifdef PM8001_USE_MSIX
761         for (i = 0; i < pm8001_ha->number_of_intr; i++)
762                 synchronize_irq(pm8001_ha->msix_entries[i].vector);
763         for (i = 0; i < pm8001_ha->number_of_intr; i++)
764                 free_irq(pm8001_ha->msix_entries[i].vector, sha);
765         pci_disable_msix(pdev);
766 #else
767         free_irq(pm8001_ha->irq, sha);
768 #endif
769 #ifdef PM8001_USE_TASKLET
770         tasklet_kill(&pm8001_ha->tasklet);
771 #endif
772         device_state = pci_choose_state(pdev, state);
773         pm8001_printk("pdev=0x%p, slot=%s, entering "
774                       "operating state [D%d]\n", pdev,
775                       pm8001_ha->name, device_state);
776         pci_save_state(pdev);
777         pci_disable_device(pdev);
778         pci_set_power_state(pdev, device_state);
779         return 0;
780 }
781
782 /**
783  * pm8001_pci_resume - power management resume main entry point
784  * @pdev: PCI device struct
785  *
786  * Returns 0 success, anything else error.
787  */
788 static int pm8001_pci_resume(struct pci_dev *pdev)
789 {
790         struct sas_ha_struct *sha = pci_get_drvdata(pdev);
791         struct pm8001_hba_info *pm8001_ha;
792         int rc;
793         u32 device_state;
794         pm8001_ha = sha->lldd_ha;
795         device_state = pdev->current_state;
796
797         pm8001_printk("pdev=0x%p, slot=%s, resuming from previous "
798                 "operating state [D%d]\n", pdev, pm8001_ha->name, device_state);
799
800         pci_set_power_state(pdev, PCI_D0);
801         pci_enable_wake(pdev, PCI_D0, 0);
802         pci_restore_state(pdev);
803         rc = pci_enable_device(pdev);
804         if (rc) {
805                 pm8001_printk("slot=%s Enable device failed during resume\n",
806                               pm8001_ha->name);
807                 goto err_out_enable;
808         }
809
810         pci_set_master(pdev);
811         rc = pci_go_44(pdev);
812         if (rc)
813                 goto err_out_disable;
814
815         PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
816         rc = PM8001_CHIP_DISP->chip_init(pm8001_ha);
817         if (rc)
818                 goto err_out_disable;
819         PM8001_CHIP_DISP->interrupt_disable(pm8001_ha);
820         rc = pm8001_request_irq(pm8001_ha);
821         if (rc)
822                 goto err_out_disable;
823         #ifdef PM8001_USE_TASKLET
824         tasklet_init(&pm8001_ha->tasklet, pm8001_tasklet,
825                     (unsigned long)pm8001_ha);
826         #endif
827         PM8001_CHIP_DISP->interrupt_enable(pm8001_ha);
828         scsi_unblock_requests(pm8001_ha->shost);
829         return 0;
830
831 err_out_disable:
832         scsi_remove_host(pm8001_ha->shost);
833         pci_disable_device(pdev);
834 err_out_enable:
835         return rc;
836 }
837
838 static struct pci_device_id __devinitdata pm8001_pci_table[] = {
839         {
840                 PCI_VDEVICE(PMC_Sierra, 0x8001), chip_8001
841         },
842         {
843                 PCI_DEVICE(0x117c, 0x0042),
844                 .driver_data = chip_8001
845         },
846         {} /* terminate list */
847 };
848
849 static struct pci_driver pm8001_pci_driver = {
850         .name           = DRV_NAME,
851         .id_table       = pm8001_pci_table,
852         .probe          = pm8001_pci_probe,
853         .remove         = __devexit_p(pm8001_pci_remove),
854         .suspend        = pm8001_pci_suspend,
855         .resume         = pm8001_pci_resume,
856 };
857
858 /**
859  *      pm8001_init - initialize scsi transport template
860  */
861 static int __init pm8001_init(void)
862 {
863         int rc;
864         pm8001_id = 0;
865         pm8001_stt = sas_domain_attach_transport(&pm8001_transport_ops);
866         if (!pm8001_stt)
867                 return -ENOMEM;
868         rc = pci_register_driver(&pm8001_pci_driver);
869         if (rc)
870                 goto err_out;
871         return 0;
872 err_out:
873         sas_release_transport(pm8001_stt);
874         return rc;
875 }
876
877 static void __exit pm8001_exit(void)
878 {
879         pci_unregister_driver(&pm8001_pci_driver);
880         sas_release_transport(pm8001_stt);
881 }
882
883 module_init(pm8001_init);
884 module_exit(pm8001_exit);
885
886 MODULE_AUTHOR("Jack Wang <jack_wang@usish.com>");
887 MODULE_DESCRIPTION("PMC-Sierra PM8001 SAS/SATA controller driver");
888 MODULE_VERSION(DRV_VERSION);
889 MODULE_LICENSE("GPL");
890 MODULE_DEVICE_TABLE(pci, pm8001_pci_table);
891