- Update to 3.3-rc2.
[linux-flexiantxendom0-3.2.10.git] / drivers / ata / ata_piix.c
1 /*
2  *    ata_piix.c - Intel PATA/SATA controllers
3  *
4  *    Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5  *                  Please ALWAYS copy linux-ide@vger.kernel.org
6  *                  on emails.
7  *
8  *
9  *      Copyright 2003-2005 Red Hat Inc
10  *      Copyright 2003-2005 Jeff Garzik
11  *
12  *
13  *      Copyright header from piix.c:
14  *
15  *  Copyright (C) 1998-1999 Andrzej Krzysztofowicz, Author and Maintainer
16  *  Copyright (C) 1998-2000 Andre Hedrick <andre@linux-ide.org>
17  *  Copyright (C) 2003 Red Hat Inc
18  *
19  *
20  *  This program is free software; you can redistribute it and/or modify
21  *  it under the terms of the GNU General Public License as published by
22  *  the Free Software Foundation; either version 2, or (at your option)
23  *  any later version.
24  *
25  *  This program is distributed in the hope that it will be useful,
26  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *  GNU General Public License for more details.
29  *
30  *  You should have received a copy of the GNU General Public License
31  *  along with this program; see the file COPYING.  If not, write to
32  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
33  *
34  *
35  *  libata documentation is available via 'make {ps|pdf}docs',
36  *  as Documentation/DocBook/libata.*
37  *
38  *  Hardware documentation available at http://developer.intel.com/
39  *
40  * Documentation
41  *      Publicly available from Intel web site. Errata documentation
42  * is also publicly available. As an aide to anyone hacking on this
43  * driver the list of errata that are relevant is below, going back to
44  * PIIX4. Older device documentation is now a bit tricky to find.
45  *
46  * The chipsets all follow very much the same design. The original Triton
47  * series chipsets do _not_ support independent device timings, but this
48  * is fixed in Triton II. With the odd mobile exception the chips then
49  * change little except in gaining more modes until SATA arrives. This
50  * driver supports only the chips with independent timing (that is those
51  * with SITRE and the 0x44 timing register). See pata_oldpiix and pata_mpiix
52  * for the early chip drivers.
53  *
54  * Errata of note:
55  *
56  * Unfixable
57  *      PIIX4    errata #9      - Only on ultra obscure hw
58  *      ICH3     errata #13     - Not observed to affect real hw
59  *                                by Intel
60  *
61  * Things we must deal with
62  *      PIIX4   errata #10      - BM IDE hang with non UDMA
63  *                                (must stop/start dma to recover)
64  *      440MX   errata #15      - As PIIX4 errata #10
65  *      PIIX4   errata #15      - Must not read control registers
66  *                                during a PIO transfer
67  *      440MX   errata #13      - As PIIX4 errata #15
68  *      ICH2    errata #21      - DMA mode 0 doesn't work right
69  *      ICH0/1  errata #55      - As ICH2 errata #21
70  *      ICH2    spec c #9       - Extra operations needed to handle
71  *                                drive hotswap [NOT YET SUPPORTED]
72  *      ICH2    spec c #20      - IDE PRD must not cross a 64K boundary
73  *                                and must be dword aligned
74  *      ICH2    spec c #24      - UDMA mode 4,5 t85/86 should be 6ns not 3.3
75  *      ICH7    errata #16      - MWDMA1 timings are incorrect
76  *
77  * Should have been BIOS fixed:
78  *      450NX:  errata #19      - DMA hangs on old 450NX
79  *      450NX:  errata #20      - DMA hangs on old 450NX
80  *      450NX:  errata #25      - Corruption with DMA on old 450NX
81  *      ICH3    errata #15      - IDE deadlock under high load
82  *                                (BIOS must set dev 31 fn 0 bit 23)
83  *      ICH3    errata #18      - Don't use native mode
84  */
85
86 #include <linux/kernel.h>
87 #include <linux/module.h>
88 #include <linux/pci.h>
89 #include <linux/init.h>
90 #include <linux/blkdev.h>
91 #include <linux/delay.h>
92 #include <linux/device.h>
93 #include <linux/gfp.h>
94 #include <scsi/scsi_host.h>
95 #include <linux/libata.h>
96 #include <linux/dmi.h>
97
98 #define DRV_NAME        "ata_piix"
99 #define DRV_VERSION     "2.13"
100
101 enum {
102         PIIX_IOCFG              = 0x54, /* IDE I/O configuration register */
103         ICH5_PMR                = 0x90, /* port mapping register */
104         ICH5_PCS                = 0x92, /* port control and status */
105         PIIX_SIDPR_BAR          = 5,
106         PIIX_SIDPR_LEN          = 16,
107         PIIX_SIDPR_IDX          = 0,
108         PIIX_SIDPR_DATA         = 4,
109
110         PIIX_FLAG_CHECKINTR     = (1 << 28), /* make sure PCI INTx enabled */
111         PIIX_FLAG_SIDPR         = (1 << 29), /* SATA idx/data pair regs */
112
113         PIIX_PATA_FLAGS         = ATA_FLAG_SLAVE_POSS,
114         PIIX_SATA_FLAGS         = ATA_FLAG_SATA | PIIX_FLAG_CHECKINTR,
115
116         PIIX_FLAG_PIO16         = (1 << 30), /*support 16bit PIO only*/
117
118         PIIX_80C_PRI            = (1 << 5) | (1 << 4),
119         PIIX_80C_SEC            = (1 << 7) | (1 << 6),
120
121         /* constants for mapping table */
122         P0                      = 0,  /* port 0 */
123         P1                      = 1,  /* port 1 */
124         P2                      = 2,  /* port 2 */
125         P3                      = 3,  /* port 3 */
126         IDE                     = -1, /* IDE */
127         NA                      = -2, /* not available */
128         RV                      = -3, /* reserved */
129
130         PIIX_AHCI_DEVICE        = 6,
131
132         /* host->flags bits */
133         PIIX_HOST_BROKEN_SUSPEND = (1 << 24),
134 };
135
136 enum piix_controller_ids {
137         /* controller IDs */
138         piix_pata_mwdma,        /* PIIX3 MWDMA only */
139         piix_pata_33,           /* PIIX4 at 33Mhz */
140         ich_pata_33,            /* ICH up to UDMA 33 only */
141         ich_pata_66,            /* ICH up to 66 Mhz */
142         ich_pata_100,           /* ICH up to UDMA 100 */
143         ich_pata_100_nomwdma1,  /* ICH up to UDMA 100 but with no MWDMA1*/
144         ich5_sata,
145         ich6_sata,
146         ich6m_sata,
147         ich8_sata,
148         ich8_2port_sata,
149         ich8m_apple_sata,       /* locks up on second port enable */
150         tolapai_sata,
151         piix_pata_vmw,                  /* PIIX4 for VMware, spurious DMA_ERR */
152         ich8_sata_snb,
153 };
154
155 struct piix_map_db {
156         const u32 mask;
157         const u16 port_enable;
158         const int map[][4];
159 };
160
161 struct piix_host_priv {
162         const int *map;
163         u32 saved_iocfg;
164         void __iomem *sidpr;
165 };
166
167 static int piix_init_one(struct pci_dev *pdev,
168                          const struct pci_device_id *ent);
169 static void piix_remove_one(struct pci_dev *pdev);
170 static unsigned int piix_pata_read_id(struct ata_device *adev, struct ata_taskfile *tf, u16 *id);
171 static int piix_pata_prereset(struct ata_link *link, unsigned long deadline);
172 static void piix_set_piomode(struct ata_port *ap, struct ata_device *adev);
173 static void piix_set_dmamode(struct ata_port *ap, struct ata_device *adev);
174 static void ich_set_dmamode(struct ata_port *ap, struct ata_device *adev);
175 static int ich_pata_cable_detect(struct ata_port *ap);
176 static u8 piix_vmw_bmdma_status(struct ata_port *ap);
177 static int piix_sidpr_scr_read(struct ata_link *link,
178                                unsigned int reg, u32 *val);
179 static int piix_sidpr_scr_write(struct ata_link *link,
180                                 unsigned int reg, u32 val);
181 static int piix_sidpr_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
182                               unsigned hints);
183 static bool piix_irq_check(struct ata_port *ap);
184 static int piix_port_start(struct ata_port *ap);
185 #ifdef CONFIG_PM
186 static int piix_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
187 static int piix_pci_device_resume(struct pci_dev *pdev);
188 #endif
189
190 static unsigned int in_module_init = 1;
191
192 static const struct pci_device_id piix_pci_tbl[] = {
193         /* Intel PIIX3 for the 430HX etc */
194         { 0x8086, 0x7010, PCI_ANY_ID, PCI_ANY_ID, 0, 0, piix_pata_mwdma },
195         /* VMware ICH4 */
196         { 0x8086, 0x7111, 0x15ad, 0x1976, 0, 0, piix_pata_vmw },
197         /* Intel PIIX4 for the 430TX/440BX/MX chipset: UDMA 33 */
198         /* Also PIIX4E (fn3 rev 2) and PIIX4M (fn3 rev 3) */
199         { 0x8086, 0x7111, PCI_ANY_ID, PCI_ANY_ID, 0, 0, piix_pata_33 },
200         /* Intel PIIX4 */
201         { 0x8086, 0x7199, PCI_ANY_ID, PCI_ANY_ID, 0, 0, piix_pata_33 },
202         /* Intel PIIX4 */
203         { 0x8086, 0x7601, PCI_ANY_ID, PCI_ANY_ID, 0, 0, piix_pata_33 },
204         /* Intel PIIX */
205         { 0x8086, 0x84CA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, piix_pata_33 },
206         /* Intel ICH (i810, i815, i840) UDMA 66*/
207         { 0x8086, 0x2411, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_66 },
208         /* Intel ICH0 : UDMA 33*/
209         { 0x8086, 0x2421, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_33 },
210         /* Intel ICH2M */
211         { 0x8086, 0x244A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
212         /* Intel ICH2 (i810E2, i845, 850, 860) UDMA 100 */
213         { 0x8086, 0x244B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
214         /*  Intel ICH3M */
215         { 0x8086, 0x248A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
216         /* Intel ICH3 (E7500/1) UDMA 100 */
217         { 0x8086, 0x248B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
218         /* Intel ICH4-L */
219         { 0x8086, 0x24C1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
220         /* Intel ICH4 (i845GV, i845E, i852, i855) UDMA 100 */
221         { 0x8086, 0x24CA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
222         { 0x8086, 0x24CB, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
223         /* Intel ICH5 */
224         { 0x8086, 0x24DB, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
225         /* C-ICH (i810E2) */
226         { 0x8086, 0x245B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
227         /* ESB (855GME/875P + 6300ESB) UDMA 100  */
228         { 0x8086, 0x25A2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
229         /* ICH6 (and 6) (i915) UDMA 100 */
230         { 0x8086, 0x266F, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
231         /* ICH7/7-R (i945, i975) UDMA 100*/
232         { 0x8086, 0x27DF, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100_nomwdma1 },
233         { 0x8086, 0x269E, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100_nomwdma1 },
234         /* ICH8 Mobile PATA Controller */
235         { 0x8086, 0x2850, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich_pata_100 },
236
237         /* SATA ports */
238
239         /* 82801EB (ICH5) */
240         { 0x8086, 0x24d1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
241         /* 82801EB (ICH5) */
242         { 0x8086, 0x24df, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
243         /* 6300ESB (ICH5 variant with broken PCS present bits) */
244         { 0x8086, 0x25a3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
245         /* 6300ESB pretending RAID */
246         { 0x8086, 0x25b0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
247         /* 82801FB/FW (ICH6/ICH6W) */
248         { 0x8086, 0x2651, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata },
249         /* 82801FR/FRW (ICH6R/ICH6RW) */
250         { 0x8086, 0x2652, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata },
251         /* 82801FBM ICH6M (ICH6R with only port 0 and 2 implemented).
252          * Attach iff the controller is in IDE mode. */
253         { 0x8086, 0x2653, PCI_ANY_ID, PCI_ANY_ID,
254           PCI_CLASS_STORAGE_IDE << 8, 0xffff00, ich6m_sata },
255         /* 82801GB/GR/GH (ICH7, identical to ICH6) */
256         { 0x8086, 0x27c0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata },
257         /* 2801GBM/GHM (ICH7M, identical to ICH6M) */
258         { 0x8086, 0x27c4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6m_sata },
259         /* Enterprise Southbridge 2 (631xESB/632xESB) */
260         { 0x8086, 0x2680, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata },
261         /* SATA Controller 1 IDE (ICH8) */
262         { 0x8086, 0x2820, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata },
263         /* SATA Controller 2 IDE (ICH8) */
264         { 0x8086, 0x2825, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
265         /* Mobile SATA Controller IDE (ICH8M), Apple */
266         { 0x8086, 0x2828, 0x106b, 0x00a0, 0, 0, ich8m_apple_sata },
267         { 0x8086, 0x2828, 0x106b, 0x00a1, 0, 0, ich8m_apple_sata },
268         { 0x8086, 0x2828, 0x106b, 0x00a3, 0, 0, ich8m_apple_sata },
269         /* Mobile SATA Controller IDE (ICH8M) */
270         { 0x8086, 0x2828, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata },
271         /* SATA Controller IDE (ICH9) */
272         { 0x8086, 0x2920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata },
273         /* SATA Controller IDE (ICH9) */
274         { 0x8086, 0x2921, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
275         /* SATA Controller IDE (ICH9) */
276         { 0x8086, 0x2926, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
277         /* SATA Controller IDE (ICH9M) */
278         { 0x8086, 0x2928, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
279         /* SATA Controller IDE (ICH9M) */
280         { 0x8086, 0x292d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
281         /* SATA Controller IDE (ICH9M) */
282         { 0x8086, 0x292e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata },
283         /* SATA Controller IDE (Tolapai) */
284         { 0x8086, 0x5028, PCI_ANY_ID, PCI_ANY_ID, 0, 0, tolapai_sata },
285         /* SATA Controller IDE (ICH10) */
286         { 0x8086, 0x3a00, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata },
287         /* SATA Controller IDE (ICH10) */
288         { 0x8086, 0x3a06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
289         /* SATA Controller IDE (ICH10) */
290         { 0x8086, 0x3a20, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata },
291         /* SATA Controller IDE (ICH10) */
292         { 0x8086, 0x3a26, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
293         /* SATA Controller IDE (PCH) */
294         { 0x8086, 0x3b20, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata },
295         /* SATA Controller IDE (PCH) */
296         { 0x8086, 0x3b21, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
297         /* SATA Controller IDE (PCH) */
298         { 0x8086, 0x3b26, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
299         /* SATA Controller IDE (PCH) */
300         { 0x8086, 0x3b28, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata },
301         /* SATA Controller IDE (PCH) */
302         { 0x8086, 0x3b2d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
303         /* SATA Controller IDE (PCH) */
304         { 0x8086, 0x3b2e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata },
305         /* SATA Controller IDE (CPT) */
306         { 0x8086, 0x1c00, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata_snb },
307         /* SATA Controller IDE (CPT) */
308         { 0x8086, 0x1c01, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata_snb },
309         /* SATA Controller IDE (CPT) */
310         { 0x8086, 0x1c08, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
311         /* SATA Controller IDE (CPT) */
312         { 0x8086, 0x1c09, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
313         /* SATA Controller IDE (PBG) */
314         { 0x8086, 0x1d00, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata_snb },
315         /* SATA Controller IDE (PBG) */
316         { 0x8086, 0x1d08, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
317         /* SATA Controller IDE (Panther Point) */
318         { 0x8086, 0x1e00, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata_snb },
319         /* SATA Controller IDE (Panther Point) */
320         { 0x8086, 0x1e01, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata_snb },
321         /* SATA Controller IDE (Panther Point) */
322         { 0x8086, 0x1e08, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
323         /* SATA Controller IDE (Panther Point) */
324         { 0x8086, 0x1e09, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
325         { }     /* terminate list */
326 };
327
328 static struct pci_driver piix_pci_driver = {
329         .name                   = DRV_NAME,
330         .id_table               = piix_pci_tbl,
331         .probe                  = piix_init_one,
332         .remove                 = piix_remove_one,
333 #ifdef CONFIG_PM
334         .suspend                = piix_pci_device_suspend,
335         .resume                 = piix_pci_device_resume,
336 #endif
337 };
338
339 static struct scsi_host_template piix_sht = {
340         ATA_BMDMA_SHT(DRV_NAME),
341 };
342
343 static struct ata_port_operations piix_sata_ops = {
344         .inherits               = &ata_bmdma32_port_ops,
345         .sff_irq_check          = piix_irq_check,
346         .port_start             = piix_port_start,
347 };
348
349 static struct ata_port_operations piix_pata_ops = {
350         .inherits               = &piix_sata_ops,
351         .cable_detect           = ata_cable_40wire,
352         .set_piomode            = piix_set_piomode,
353         .set_dmamode            = piix_set_dmamode,
354         .prereset               = piix_pata_prereset,
355         .read_id                = piix_pata_read_id,
356 };
357
358 static struct ata_port_operations piix_vmw_ops = {
359         .inherits               = &piix_pata_ops,
360         .bmdma_status           = piix_vmw_bmdma_status,
361 };
362
363 static struct ata_port_operations ich_pata_ops = {
364         .inherits               = &piix_pata_ops,
365         .cable_detect           = ich_pata_cable_detect,
366         .set_dmamode            = ich_set_dmamode,
367 };
368
369 static struct device_attribute *piix_sidpr_shost_attrs[] = {
370         &dev_attr_link_power_management_policy,
371         NULL
372 };
373
374 static struct scsi_host_template piix_sidpr_sht = {
375         ATA_BMDMA_SHT(DRV_NAME),
376         .shost_attrs            = piix_sidpr_shost_attrs,
377 };
378
379 static struct ata_port_operations piix_sidpr_sata_ops = {
380         .inherits               = &piix_sata_ops,
381         .hardreset              = sata_std_hardreset,
382         .scr_read               = piix_sidpr_scr_read,
383         .scr_write              = piix_sidpr_scr_write,
384         .set_lpm                = piix_sidpr_set_lpm,
385 };
386
387 static const struct piix_map_db ich5_map_db = {
388         .mask = 0x7,
389         .port_enable = 0x3,
390         .map = {
391                 /* PM   PS   SM   SS       MAP  */
392                 {  P0,  NA,  P1,  NA }, /* 000b */
393                 {  P1,  NA,  P0,  NA }, /* 001b */
394                 {  RV,  RV,  RV,  RV },
395                 {  RV,  RV,  RV,  RV },
396                 {  P0,  P1, IDE, IDE }, /* 100b */
397                 {  P1,  P0, IDE, IDE }, /* 101b */
398                 { IDE, IDE,  P0,  P1 }, /* 110b */
399                 { IDE, IDE,  P1,  P0 }, /* 111b */
400         },
401 };
402
403 static const struct piix_map_db ich6_map_db = {
404         .mask = 0x3,
405         .port_enable = 0xf,
406         .map = {
407                 /* PM   PS   SM   SS       MAP */
408                 {  P0,  P2,  P1,  P3 }, /* 00b */
409                 { IDE, IDE,  P1,  P3 }, /* 01b */
410                 {  P0,  P2, IDE, IDE }, /* 10b */
411                 {  RV,  RV,  RV,  RV },
412         },
413 };
414
415 static const struct piix_map_db ich6m_map_db = {
416         .mask = 0x3,
417         .port_enable = 0x5,
418
419         /* Map 01b isn't specified in the doc but some notebooks use
420          * it anyway.  MAP 01b have been spotted on both ICH6M and
421          * ICH7M.
422          */
423         .map = {
424                 /* PM   PS   SM   SS       MAP */
425                 {  P0,  P2,  NA,  NA }, /* 00b */
426                 { IDE, IDE,  P1,  P3 }, /* 01b */
427                 {  P0,  P2, IDE, IDE }, /* 10b */
428                 {  RV,  RV,  RV,  RV },
429         },
430 };
431
432 static const struct piix_map_db ich8_map_db = {
433         .mask = 0x3,
434         .port_enable = 0xf,
435         .map = {
436                 /* PM   PS   SM   SS       MAP */
437                 {  P0,  P2,  P1,  P3 }, /* 00b (hardwired when in AHCI) */
438                 {  RV,  RV,  RV,  RV },
439                 {  P0,  P2, IDE, IDE }, /* 10b (IDE mode) */
440                 {  RV,  RV,  RV,  RV },
441         },
442 };
443
444 static const struct piix_map_db ich8_2port_map_db = {
445         .mask = 0x3,
446         .port_enable = 0x3,
447         .map = {
448                 /* PM   PS   SM   SS       MAP */
449                 {  P0,  NA,  P1,  NA }, /* 00b */
450                 {  RV,  RV,  RV,  RV }, /* 01b */
451                 {  RV,  RV,  RV,  RV }, /* 10b */
452                 {  RV,  RV,  RV,  RV },
453         },
454 };
455
456 static const struct piix_map_db ich8m_apple_map_db = {
457         .mask = 0x3,
458         .port_enable = 0x1,
459         .map = {
460                 /* PM   PS   SM   SS       MAP */
461                 {  P0,  NA,  NA,  NA }, /* 00b */
462                 {  RV,  RV,  RV,  RV },
463                 {  P0,  P2, IDE, IDE }, /* 10b */
464                 {  RV,  RV,  RV,  RV },
465         },
466 };
467
468 static const struct piix_map_db tolapai_map_db = {
469         .mask = 0x3,
470         .port_enable = 0x3,
471         .map = {
472                 /* PM   PS   SM   SS       MAP */
473                 {  P0,  NA,  P1,  NA }, /* 00b */
474                 {  RV,  RV,  RV,  RV }, /* 01b */
475                 {  RV,  RV,  RV,  RV }, /* 10b */
476                 {  RV,  RV,  RV,  RV },
477         },
478 };
479
480 static const struct piix_map_db *piix_map_db_table[] = {
481         [ich5_sata]             = &ich5_map_db,
482         [ich6_sata]             = &ich6_map_db,
483         [ich6m_sata]            = &ich6m_map_db,
484         [ich8_sata]             = &ich8_map_db,
485         [ich8_2port_sata]       = &ich8_2port_map_db,
486         [ich8m_apple_sata]      = &ich8m_apple_map_db,
487         [tolapai_sata]          = &tolapai_map_db,
488         [ich8_sata_snb]         = &ich8_map_db,
489 };
490
491 static struct ata_port_info piix_port_info[] = {
492         [piix_pata_mwdma] =     /* PIIX3 MWDMA only */
493         {
494                 .flags          = PIIX_PATA_FLAGS,
495                 .pio_mask       = ATA_PIO4,
496                 .mwdma_mask     = ATA_MWDMA12_ONLY, /* mwdma1-2 ?? CHECK 0 should be ok but slow */
497                 .port_ops       = &piix_pata_ops,
498         },
499
500         [piix_pata_33] =        /* PIIX4 at 33MHz */
501         {
502                 .flags          = PIIX_PATA_FLAGS,
503                 .pio_mask       = ATA_PIO4,
504                 .mwdma_mask     = ATA_MWDMA12_ONLY, /* mwdma1-2 ?? CHECK 0 should be ok but slow */
505                 .udma_mask      = ATA_UDMA2,
506                 .port_ops       = &piix_pata_ops,
507         },
508
509         [ich_pata_33] =         /* ICH0 - ICH at 33Mhz*/
510         {
511                 .flags          = PIIX_PATA_FLAGS,
512                 .pio_mask       = ATA_PIO4,
513                 .mwdma_mask     = ATA_MWDMA12_ONLY, /* Check: maybe MWDMA0 is ok  */
514                 .udma_mask      = ATA_UDMA2,
515                 .port_ops       = &ich_pata_ops,
516         },
517
518         [ich_pata_66] =         /* ICH controllers up to 66MHz */
519         {
520                 .flags          = PIIX_PATA_FLAGS,
521                 .pio_mask       = ATA_PIO4,
522                 .mwdma_mask     = ATA_MWDMA12_ONLY, /* MWDMA0 is broken on chip */
523                 .udma_mask      = ATA_UDMA4,
524                 .port_ops       = &ich_pata_ops,
525         },
526
527         [ich_pata_100] =
528         {
529                 .flags          = PIIX_PATA_FLAGS | PIIX_FLAG_CHECKINTR,
530                 .pio_mask       = ATA_PIO4,
531                 .mwdma_mask     = ATA_MWDMA12_ONLY,
532                 .udma_mask      = ATA_UDMA5,
533                 .port_ops       = &ich_pata_ops,
534         },
535
536         [ich_pata_100_nomwdma1] =
537         {
538                 .flags          = PIIX_PATA_FLAGS | PIIX_FLAG_CHECKINTR,
539                 .pio_mask       = ATA_PIO4,
540                 .mwdma_mask     = ATA_MWDMA2_ONLY,
541                 .udma_mask      = ATA_UDMA5,
542                 .port_ops       = &ich_pata_ops,
543         },
544
545         [ich5_sata] =
546         {
547                 .flags          = PIIX_SATA_FLAGS,
548                 .pio_mask       = ATA_PIO4,
549                 .mwdma_mask     = ATA_MWDMA2,
550                 .udma_mask      = ATA_UDMA6,
551                 .port_ops       = &piix_sata_ops,
552         },
553
554         [ich6_sata] =
555         {
556                 .flags          = PIIX_SATA_FLAGS,
557                 .pio_mask       = ATA_PIO4,
558                 .mwdma_mask     = ATA_MWDMA2,
559                 .udma_mask      = ATA_UDMA6,
560                 .port_ops       = &piix_sata_ops,
561         },
562
563         [ich6m_sata] =
564         {
565                 .flags          = PIIX_SATA_FLAGS,
566                 .pio_mask       = ATA_PIO4,
567                 .mwdma_mask     = ATA_MWDMA2,
568                 .udma_mask      = ATA_UDMA6,
569                 .port_ops       = &piix_sata_ops,
570         },
571
572         [ich8_sata] =
573         {
574                 .flags          = PIIX_SATA_FLAGS | PIIX_FLAG_SIDPR,
575                 .pio_mask       = ATA_PIO4,
576                 .mwdma_mask     = ATA_MWDMA2,
577                 .udma_mask      = ATA_UDMA6,
578                 .port_ops       = &piix_sata_ops,
579         },
580
581         [ich8_2port_sata] =
582         {
583                 .flags          = PIIX_SATA_FLAGS | PIIX_FLAG_SIDPR,
584                 .pio_mask       = ATA_PIO4,
585                 .mwdma_mask     = ATA_MWDMA2,
586                 .udma_mask      = ATA_UDMA6,
587                 .port_ops       = &piix_sata_ops,
588         },
589
590         [tolapai_sata] =
591         {
592                 .flags          = PIIX_SATA_FLAGS,
593                 .pio_mask       = ATA_PIO4,
594                 .mwdma_mask     = ATA_MWDMA2,
595                 .udma_mask      = ATA_UDMA6,
596                 .port_ops       = &piix_sata_ops,
597         },
598
599         [ich8m_apple_sata] =
600         {
601                 .flags          = PIIX_SATA_FLAGS,
602                 .pio_mask       = ATA_PIO4,
603                 .mwdma_mask     = ATA_MWDMA2,
604                 .udma_mask      = ATA_UDMA6,
605                 .port_ops       = &piix_sata_ops,
606         },
607
608         [piix_pata_vmw] =
609         {
610                 .flags          = PIIX_PATA_FLAGS,
611                 .pio_mask       = ATA_PIO4,
612                 .mwdma_mask     = ATA_MWDMA12_ONLY, /* mwdma1-2 ?? CHECK 0 should be ok but slow */
613                 .udma_mask      = ATA_UDMA2,
614                 .port_ops       = &piix_vmw_ops,
615         },
616
617         /*
618          * some Sandybridge chipsets have broken 32 mode up to now,
619          * see https://bugzilla.kernel.org/show_bug.cgi?id=40592
620          */
621         [ich8_sata_snb] =
622         {
623                 .flags          = PIIX_SATA_FLAGS | PIIX_FLAG_SIDPR | PIIX_FLAG_PIO16,
624                 .pio_mask       = ATA_PIO4,
625                 .mwdma_mask     = ATA_MWDMA2,
626                 .udma_mask      = ATA_UDMA6,
627                 .port_ops       = &piix_sata_ops,
628         },
629
630 };
631
632 static struct pci_bits piix_enable_bits[] = {
633         { 0x41U, 1U, 0x80UL, 0x80UL },  /* port 0 */
634         { 0x43U, 1U, 0x80UL, 0x80UL },  /* port 1 */
635 };
636
637 MODULE_AUTHOR("Andre Hedrick, Alan Cox, Andrzej Krzysztofowicz, Jeff Garzik");
638 MODULE_DESCRIPTION("SCSI low-level driver for Intel PIIX/ICH ATA controllers");
639 MODULE_LICENSE("GPL");
640 MODULE_DEVICE_TABLE(pci, piix_pci_tbl);
641 MODULE_VERSION(DRV_VERSION);
642
643 static int piix_msft_hyperv(void)
644 {
645         int hv = 0;
646 #if defined(CONFIG_HYPERV_STORAGE) || defined(CONFIG_HYPERV_STORAGE_MODULE)
647         static const struct dmi_system_id hv_dmi_ident[]  = {
648                 {
649                         .ident = "Hyper-V",
650                         .matches = {
651                                 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
652                                 DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
653                                 DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
654                         },
655                 },
656                 { }     /* terminate list */
657         };
658         hv = !!dmi_check_system(hv_dmi_ident);
659 #endif
660         return hv;
661 }
662
663 struct ich_laptop {
664         u16 device;
665         u16 subvendor;
666         u16 subdevice;
667 };
668
669 /*
670  *      List of laptops that use short cables rather than 80 wire
671  */
672
673 static const struct ich_laptop ich_laptop[] = {
674         /* devid, subvendor, subdev */
675         { 0x27DF, 0x0005, 0x0280 },     /* ICH7 on Acer 5602WLMi */
676         { 0x27DF, 0x1025, 0x0102 },     /* ICH7 on Acer 5602aWLMi */
677         { 0x27DF, 0x1025, 0x0110 },     /* ICH7 on Acer 3682WLMi */
678         { 0x27DF, 0x1028, 0x02b0 },     /* ICH7 on unknown Dell */
679         { 0x27DF, 0x1043, 0x1267 },     /* ICH7 on Asus W5F */
680         { 0x27DF, 0x103C, 0x30A1 },     /* ICH7 on HP Compaq nc2400 */
681         { 0x27DF, 0x103C, 0x361a },     /* ICH7 on unknown HP  */
682         { 0x27DF, 0x1071, 0xD221 },     /* ICH7 on Hercules EC-900 */
683         { 0x27DF, 0x152D, 0x0778 },     /* ICH7 on unknown Intel */
684         { 0x24CA, 0x1025, 0x0061 },     /* ICH4 on ACER Aspire 2023WLMi */
685         { 0x24CA, 0x1025, 0x003d },     /* ICH4 on ACER TM290 */
686         { 0x266F, 0x1025, 0x0066 },     /* ICH6 on ACER Aspire 1694WLMi */
687         { 0x2653, 0x1043, 0x82D8 },     /* ICH6M on Asus Eee 701 */
688         { 0x27df, 0x104d, 0x900e },     /* ICH7 on Sony TZ-90 */
689         /* end marker */
690         { 0, }
691 };
692
693 static int piix_port_start(struct ata_port *ap)
694 {
695         if (!(ap->flags & PIIX_FLAG_PIO16))
696                 ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE;
697
698         return ata_bmdma_port_start(ap);
699 }
700
701 /**
702  *      ich_pata_cable_detect - Probe host controller cable detect info
703  *      @ap: Port for which cable detect info is desired
704  *
705  *      Read 80c cable indicator from ATA PCI device's PCI config
706  *      register.  This register is normally set by firmware (BIOS).
707  *
708  *      LOCKING:
709  *      None (inherited from caller).
710  */
711
712 static int ich_pata_cable_detect(struct ata_port *ap)
713 {
714         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
715         struct piix_host_priv *hpriv = ap->host->private_data;
716         const struct ich_laptop *lap = &ich_laptop[0];
717         u8 mask;
718
719         /* Check for specials - Acer Aspire 5602WLMi */
720         while (lap->device) {
721                 if (lap->device == pdev->device &&
722                     lap->subvendor == pdev->subsystem_vendor &&
723                     lap->subdevice == pdev->subsystem_device)
724                         return ATA_CBL_PATA40_SHORT;
725
726                 lap++;
727         }
728
729         /* check BIOS cable detect results */
730         mask = ap->port_no == 0 ? PIIX_80C_PRI : PIIX_80C_SEC;
731         if ((hpriv->saved_iocfg & mask) == 0)
732                 return ATA_CBL_PATA40;
733         return ATA_CBL_PATA80;
734 }
735
736 /**
737  *      piix_pata_prereset - prereset for PATA host controller
738  *      @link: Target link
739  *      @deadline: deadline jiffies for the operation
740  *
741  *      LOCKING:
742  *      None (inherited from caller).
743  */
744 static int piix_pata_prereset(struct ata_link *link, unsigned long deadline)
745 {
746         struct ata_port *ap = link->ap;
747         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
748
749         if (!pci_test_config_bits(pdev, &piix_enable_bits[ap->port_no]))
750                 return -ENOENT;
751         return ata_sff_prereset(link, deadline);
752 }
753
754 static unsigned int piix_pata_read_id(struct ata_device *adev, struct ata_taskfile *tf, u16 *id)
755 {
756         unsigned int err_mask = ata_do_dev_read_id(adev, tf, id);
757         /*
758          * Ignore disks in a hyper-v guest.
759          * There is no unplug protocol like it is done with xen_emul_unplug= option.
760          * Emulate the unplug by ignoring disks when the hv_storvsc driver is enabled.
761          * If the disks are not ignored, they will appear twice: once through
762          * piix and once through hv_storvsc.
763          * hv_storvsc can not handle ATAPI devices because they can only be
764          * accessed through the emulated code path (not through the vm_bus
765          * channel), the piix driver is still required.
766          */
767         if (ata_id_is_ata(id) && piix_msft_hyperv()) {
768                 ata_dev_printk(adev, KERN_WARNING, "ATA device ignored in Hyper-V guest\n");
769                 id[ATA_ID_CONFIG] |= (1 << 15);
770         }
771         return err_mask;
772 }
773
774 static DEFINE_SPINLOCK(piix_lock);
775
776 static void piix_set_timings(struct ata_port *ap, struct ata_device *adev,
777                              u8 pio)
778 {
779         struct pci_dev *dev     = to_pci_dev(ap->host->dev);
780         unsigned long flags;
781         unsigned int is_slave   = (adev->devno != 0);
782         unsigned int master_port= ap->port_no ? 0x42 : 0x40;
783         unsigned int slave_port = 0x44;
784         u16 master_data;
785         u8 slave_data;
786         u8 udma_enable;
787         int control = 0;
788
789         /*
790          *      See Intel Document 298600-004 for the timing programing rules
791          *      for ICH controllers.
792          */
793
794         static const     /* ISP  RTC */
795         u8 timings[][2] = { { 0, 0 },
796                             { 0, 0 },
797                             { 1, 0 },
798                             { 2, 1 },
799                             { 2, 3 }, };
800
801         if (pio >= 2)
802                 control |= 1;   /* TIME1 enable */
803         if (ata_pio_need_iordy(adev))
804                 control |= 2;   /* IE enable */
805         /* Intel specifies that the PPE functionality is for disk only */
806         if (adev->class == ATA_DEV_ATA)
807                 control |= 4;   /* PPE enable */
808         /*
809          * If the drive MWDMA is faster than it can do PIO then
810          * we must force PIO into PIO0
811          */
812         if (adev->pio_mode < XFER_PIO_0 + pio)
813                 /* Enable DMA timing only */
814                 control |= 8;   /* PIO cycles in PIO0 */
815
816         spin_lock_irqsave(&piix_lock, flags);
817
818         /* PIO configuration clears DTE unconditionally.  It will be
819          * programmed in set_dmamode which is guaranteed to be called
820          * after set_piomode if any DMA mode is available.
821          */
822         pci_read_config_word(dev, master_port, &master_data);
823         if (is_slave) {
824                 /* clear TIME1|IE1|PPE1|DTE1 */
825                 master_data &= 0xff0f;
826                 /* enable PPE1, IE1 and TIME1 as needed */
827                 master_data |= (control << 4);
828                 pci_read_config_byte(dev, slave_port, &slave_data);
829                 slave_data &= (ap->port_no ? 0x0f : 0xf0);
830                 /* Load the timing nibble for this slave */
831                 slave_data |= ((timings[pio][0] << 2) | timings[pio][1])
832                                                 << (ap->port_no ? 4 : 0);
833         } else {
834                 /* clear ISP|RCT|TIME0|IE0|PPE0|DTE0 */
835                 master_data &= 0xccf0;
836                 /* Enable PPE, IE and TIME as appropriate */
837                 master_data |= control;
838                 /* load ISP and RCT */
839                 master_data |=
840                         (timings[pio][0] << 12) |
841                         (timings[pio][1] << 8);
842         }
843
844         /* Enable SITRE (separate slave timing register) */
845         master_data |= 0x4000;
846         pci_write_config_word(dev, master_port, master_data);
847         if (is_slave)
848                 pci_write_config_byte(dev, slave_port, slave_data);
849
850         /* Ensure the UDMA bit is off - it will be turned back on if
851            UDMA is selected */
852
853         if (ap->udma_mask) {
854                 pci_read_config_byte(dev, 0x48, &udma_enable);
855                 udma_enable &= ~(1 << (2 * ap->port_no + adev->devno));
856                 pci_write_config_byte(dev, 0x48, udma_enable);
857         }
858
859         spin_unlock_irqrestore(&piix_lock, flags);
860 }
861
862 /**
863  *      piix_set_piomode - Initialize host controller PATA PIO timings
864  *      @ap: Port whose timings we are configuring
865  *      @adev: Drive in question
866  *
867  *      Set PIO mode for device, in host controller PCI config space.
868  *
869  *      LOCKING:
870  *      None (inherited from caller).
871  */
872
873 static void piix_set_piomode(struct ata_port *ap, struct ata_device *adev)
874 {
875         piix_set_timings(ap, adev, adev->pio_mode - XFER_PIO_0);
876 }
877
878 /**
879  *      do_pata_set_dmamode - Initialize host controller PATA PIO timings
880  *      @ap: Port whose timings we are configuring
881  *      @adev: Drive in question
882  *      @isich: set if the chip is an ICH device
883  *
884  *      Set UDMA mode for device, in host controller PCI config space.
885  *
886  *      LOCKING:
887  *      None (inherited from caller).
888  */
889
890 static void do_pata_set_dmamode(struct ata_port *ap, struct ata_device *adev, int isich)
891 {
892         struct pci_dev *dev     = to_pci_dev(ap->host->dev);
893         unsigned long flags;
894         u8 speed                = adev->dma_mode;
895         int devid               = adev->devno + 2 * ap->port_no;
896         u8 udma_enable          = 0;
897
898         if (speed >= XFER_UDMA_0) {
899                 unsigned int udma = speed - XFER_UDMA_0;
900                 u16 udma_timing;
901                 u16 ideconf;
902                 int u_clock, u_speed;
903
904                 spin_lock_irqsave(&piix_lock, flags);
905
906                 pci_read_config_byte(dev, 0x48, &udma_enable);
907
908                 /*
909                  * UDMA is handled by a combination of clock switching and
910                  * selection of dividers
911                  *
912                  * Handy rule: Odd modes are UDMATIMx 01, even are 02
913                  *             except UDMA0 which is 00
914                  */
915                 u_speed = min(2 - (udma & 1), udma);
916                 if (udma == 5)
917                         u_clock = 0x1000;       /* 100Mhz */
918                 else if (udma > 2)
919                         u_clock = 1;            /* 66Mhz */
920                 else
921                         u_clock = 0;            /* 33Mhz */
922
923                 udma_enable |= (1 << devid);
924
925                 /* Load the CT/RP selection */
926                 pci_read_config_word(dev, 0x4A, &udma_timing);
927                 udma_timing &= ~(3 << (4 * devid));
928                 udma_timing |= u_speed << (4 * devid);
929                 pci_write_config_word(dev, 0x4A, udma_timing);
930
931                 if (isich) {
932                         /* Select a 33/66/100Mhz clock */
933                         pci_read_config_word(dev, 0x54, &ideconf);
934                         ideconf &= ~(0x1001 << devid);
935                         ideconf |= u_clock << devid;
936                         /* For ICH or later we should set bit 10 for better
937                            performance (WR_PingPong_En) */
938                         pci_write_config_word(dev, 0x54, ideconf);
939                 }
940
941                 pci_write_config_byte(dev, 0x48, udma_enable);
942
943                 spin_unlock_irqrestore(&piix_lock, flags);
944         } else {
945                 /* MWDMA is driven by the PIO timings. */
946                 unsigned int mwdma = speed - XFER_MW_DMA_0;
947                 const unsigned int needed_pio[3] = {
948                         XFER_PIO_0, XFER_PIO_3, XFER_PIO_4
949                 };
950                 int pio = needed_pio[mwdma] - XFER_PIO_0;
951
952                 /* XFER_PIO_0 is never used currently */
953                 piix_set_timings(ap, adev, pio);
954         }
955 }
956
957 /**
958  *      piix_set_dmamode - Initialize host controller PATA DMA timings
959  *      @ap: Port whose timings we are configuring
960  *      @adev: um
961  *
962  *      Set MW/UDMA mode for device, in host controller PCI config space.
963  *
964  *      LOCKING:
965  *      None (inherited from caller).
966  */
967
968 static void piix_set_dmamode(struct ata_port *ap, struct ata_device *adev)
969 {
970         do_pata_set_dmamode(ap, adev, 0);
971 }
972
973 /**
974  *      ich_set_dmamode - Initialize host controller PATA DMA timings
975  *      @ap: Port whose timings we are configuring
976  *      @adev: um
977  *
978  *      Set MW/UDMA mode for device, in host controller PCI config space.
979  *
980  *      LOCKING:
981  *      None (inherited from caller).
982  */
983
984 static void ich_set_dmamode(struct ata_port *ap, struct ata_device *adev)
985 {
986         do_pata_set_dmamode(ap, adev, 1);
987 }
988
989 /*
990  * Serial ATA Index/Data Pair Superset Registers access
991  *
992  * Beginning from ICH8, there's a sane way to access SCRs using index
993  * and data register pair located at BAR5 which means that we have
994  * separate SCRs for master and slave.  This is handled using libata
995  * slave_link facility.
996  */
997 static const int piix_sidx_map[] = {
998         [SCR_STATUS]    = 0,
999         [SCR_ERROR]     = 2,
1000         [SCR_CONTROL]   = 1,
1001 };
1002
1003 static void piix_sidpr_sel(struct ata_link *link, unsigned int reg)
1004 {
1005         struct ata_port *ap = link->ap;
1006         struct piix_host_priv *hpriv = ap->host->private_data;
1007
1008         iowrite32(((ap->port_no * 2 + link->pmp) << 8) | piix_sidx_map[reg],
1009                   hpriv->sidpr + PIIX_SIDPR_IDX);
1010 }
1011
1012 static int piix_sidpr_scr_read(struct ata_link *link,
1013                                unsigned int reg, u32 *val)
1014 {
1015         struct piix_host_priv *hpriv = link->ap->host->private_data;
1016
1017         if (reg >= ARRAY_SIZE(piix_sidx_map))
1018                 return -EINVAL;
1019
1020         piix_sidpr_sel(link, reg);
1021         *val = ioread32(hpriv->sidpr + PIIX_SIDPR_DATA);
1022         return 0;
1023 }
1024
1025 static int piix_sidpr_scr_write(struct ata_link *link,
1026                                 unsigned int reg, u32 val)
1027 {
1028         struct piix_host_priv *hpriv = link->ap->host->private_data;
1029
1030         if (reg >= ARRAY_SIZE(piix_sidx_map))
1031                 return -EINVAL;
1032
1033         piix_sidpr_sel(link, reg);
1034         iowrite32(val, hpriv->sidpr + PIIX_SIDPR_DATA);
1035         return 0;
1036 }
1037
1038 static int piix_sidpr_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
1039                               unsigned hints)
1040 {
1041         return sata_link_scr_lpm(link, policy, false);
1042 }
1043
1044 static bool piix_irq_check(struct ata_port *ap)
1045 {
1046         if (unlikely(!ap->ioaddr.bmdma_addr))
1047                 return false;
1048
1049         return ap->ops->bmdma_status(ap) & ATA_DMA_INTR;
1050 }
1051
1052 #ifdef CONFIG_PM
1053 static int piix_broken_suspend(void)
1054 {
1055         static const struct dmi_system_id sysids[] = {
1056                 {
1057                         .ident = "TECRA M3",
1058                         .matches = {
1059                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1060                                 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA M3"),
1061                         },
1062                 },
1063                 {
1064                         .ident = "TECRA M3",
1065                         .matches = {
1066                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1067                                 DMI_MATCH(DMI_PRODUCT_NAME, "Tecra M3"),
1068                         },
1069                 },
1070                 {
1071                         .ident = "TECRA M4",
1072                         .matches = {
1073                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1074                                 DMI_MATCH(DMI_PRODUCT_NAME, "Tecra M4"),
1075                         },
1076                 },
1077                 {
1078                         .ident = "TECRA M4",
1079                         .matches = {
1080                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1081                                 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA M4"),
1082                         },
1083                 },
1084                 {
1085                         .ident = "TECRA M5",
1086                         .matches = {
1087                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1088                                 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA M5"),
1089                         },
1090                 },
1091                 {
1092                         .ident = "TECRA M6",
1093                         .matches = {
1094                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1095                                 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA M6"),
1096                         },
1097                 },
1098                 {
1099                         .ident = "TECRA M7",
1100                         .matches = {
1101                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1102                                 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA M7"),
1103                         },
1104                 },
1105                 {
1106                         .ident = "TECRA A8",
1107                         .matches = {
1108                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1109                                 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA A8"),
1110                         },
1111                 },
1112                 {
1113                         .ident = "Satellite R20",
1114                         .matches = {
1115                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1116                                 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite R20"),
1117                         },
1118                 },
1119                 {
1120                         .ident = "Satellite R25",
1121                         .matches = {
1122                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1123                                 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite R25"),
1124                         },
1125                 },
1126                 {
1127                         .ident = "Satellite U200",
1128                         .matches = {
1129                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1130                                 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite U200"),
1131                         },
1132                 },
1133                 {
1134                         .ident = "Satellite U200",
1135                         .matches = {
1136                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1137                                 DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE U200"),
1138                         },
1139                 },
1140                 {
1141                         .ident = "Satellite Pro U200",
1142                         .matches = {
1143                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1144                                 DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE PRO U200"),
1145                         },
1146                 },
1147                 {
1148                         .ident = "Satellite U205",
1149                         .matches = {
1150                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1151                                 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite U205"),
1152                         },
1153                 },
1154                 {
1155                         .ident = "SATELLITE U205",
1156                         .matches = {
1157                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1158                                 DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE U205"),
1159                         },
1160                 },
1161                 {
1162                         .ident = "Satellite Pro A120",
1163                         .matches = {
1164                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1165                                 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite Pro A120"),
1166                         },
1167                 },
1168                 {
1169                         .ident = "Portege M500",
1170                         .matches = {
1171                                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1172                                 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M500"),
1173                         },
1174                 },
1175                 {
1176                         .ident = "VGN-BX297XP",
1177                         .matches = {
1178                                 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1179                                 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-BX297XP"),
1180                         },
1181                 },
1182
1183                 { }     /* terminate list */
1184         };
1185         static const char *oemstrs[] = {
1186                 "Tecra M3,",
1187         };
1188         int i;
1189
1190         if (dmi_check_system(sysids))
1191                 return 1;
1192
1193         for (i = 0; i < ARRAY_SIZE(oemstrs); i++)
1194                 if (dmi_find_device(DMI_DEV_TYPE_OEM_STRING, oemstrs[i], NULL))
1195                         return 1;
1196
1197         /* TECRA M4 sometimes forgets its identify and reports bogus
1198          * DMI information.  As the bogus information is a bit
1199          * generic, match as many entries as possible.  This manual
1200          * matching is necessary because dmi_system_id.matches is
1201          * limited to four entries.
1202          */
1203         if (dmi_match(DMI_SYS_VENDOR, "TOSHIBA") &&
1204             dmi_match(DMI_PRODUCT_NAME, "000000") &&
1205             dmi_match(DMI_PRODUCT_VERSION, "000000") &&
1206             dmi_match(DMI_PRODUCT_SERIAL, "000000") &&
1207             dmi_match(DMI_BOARD_VENDOR, "TOSHIBA") &&
1208             dmi_match(DMI_BOARD_NAME, "Portable PC") &&
1209             dmi_match(DMI_BOARD_VERSION, "Version A0"))
1210                 return 1;
1211
1212         return 0;
1213 }
1214
1215 static int piix_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
1216 {
1217         struct ata_host *host = dev_get_drvdata(&pdev->dev);
1218         unsigned long flags;
1219         int rc = 0;
1220
1221         rc = ata_host_suspend(host, mesg);
1222         if (rc)
1223                 return rc;
1224
1225         /* Some braindamaged ACPI suspend implementations expect the
1226          * controller to be awake on entry; otherwise, it burns cpu
1227          * cycles and power trying to do something to the sleeping
1228          * beauty.
1229          */
1230         if (piix_broken_suspend() && (mesg.event & PM_EVENT_SLEEP)) {
1231                 pci_save_state(pdev);
1232
1233                 /* mark its power state as "unknown", since we don't
1234                  * know if e.g. the BIOS will change its device state
1235                  * when we suspend.
1236                  */
1237                 if (pdev->current_state == PCI_D0)
1238                         pdev->current_state = PCI_UNKNOWN;
1239
1240                 /* tell resume that it's waking up from broken suspend */
1241                 spin_lock_irqsave(&host->lock, flags);
1242                 host->flags |= PIIX_HOST_BROKEN_SUSPEND;
1243                 spin_unlock_irqrestore(&host->lock, flags);
1244         } else
1245                 ata_pci_device_do_suspend(pdev, mesg);
1246
1247         return 0;
1248 }
1249
1250 static int piix_pci_device_resume(struct pci_dev *pdev)
1251 {
1252         struct ata_host *host = dev_get_drvdata(&pdev->dev);
1253         unsigned long flags;
1254         int rc;
1255
1256         if (host->flags & PIIX_HOST_BROKEN_SUSPEND) {
1257                 spin_lock_irqsave(&host->lock, flags);
1258                 host->flags &= ~PIIX_HOST_BROKEN_SUSPEND;
1259                 spin_unlock_irqrestore(&host->lock, flags);
1260
1261                 pci_set_power_state(pdev, PCI_D0);
1262                 pci_restore_state(pdev);
1263
1264                 /* PCI device wasn't disabled during suspend.  Use
1265                  * pci_reenable_device() to avoid affecting the enable
1266                  * count.
1267                  */
1268                 rc = pci_reenable_device(pdev);
1269                 if (rc)
1270                         dev_err(&pdev->dev,
1271                                 "failed to enable device after resume (%d)\n",
1272                                 rc);
1273         } else
1274                 rc = ata_pci_device_do_resume(pdev);
1275
1276         if (rc == 0)
1277                 ata_host_resume(host);
1278
1279         return rc;
1280 }
1281 #endif
1282
1283 static u8 piix_vmw_bmdma_status(struct ata_port *ap)
1284 {
1285         return ata_bmdma_status(ap) & ~ATA_DMA_ERR;
1286 }
1287
1288 #define AHCI_PCI_BAR 5
1289 #define AHCI_GLOBAL_CTL 0x04
1290 #define AHCI_ENABLE (1 << 31)
1291 static int piix_disable_ahci(struct pci_dev *pdev)
1292 {
1293         void __iomem *mmio;
1294         u32 tmp;
1295         int rc = 0;
1296
1297         /* BUG: pci_enable_device has not yet been called.  This
1298          * works because this device is usually set up by BIOS.
1299          */
1300
1301         if (!pci_resource_start(pdev, AHCI_PCI_BAR) ||
1302             !pci_resource_len(pdev, AHCI_PCI_BAR))
1303                 return 0;
1304
1305         mmio = pci_iomap(pdev, AHCI_PCI_BAR, 64);
1306         if (!mmio)
1307                 return -ENOMEM;
1308
1309         tmp = ioread32(mmio + AHCI_GLOBAL_CTL);
1310         if (tmp & AHCI_ENABLE) {
1311                 tmp &= ~AHCI_ENABLE;
1312                 iowrite32(tmp, mmio + AHCI_GLOBAL_CTL);
1313
1314                 tmp = ioread32(mmio + AHCI_GLOBAL_CTL);
1315                 if (tmp & AHCI_ENABLE)
1316                         rc = -EIO;
1317         }
1318
1319         pci_iounmap(pdev, mmio);
1320         return rc;
1321 }
1322
1323 /**
1324  *      piix_check_450nx_errata -       Check for problem 450NX setup
1325  *      @ata_dev: the PCI device to check
1326  *
1327  *      Check for the present of 450NX errata #19 and errata #25. If
1328  *      they are found return an error code so we can turn off DMA
1329  */
1330
1331 static int __devinit piix_check_450nx_errata(struct pci_dev *ata_dev)
1332 {
1333         struct pci_dev *pdev = NULL;
1334         u16 cfg;
1335         int no_piix_dma = 0;
1336
1337         while ((pdev = pci_get_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82454NX, pdev)) != NULL) {
1338                 /* Look for 450NX PXB. Check for problem configurations
1339                    A PCI quirk checks bit 6 already */
1340                 pci_read_config_word(pdev, 0x41, &cfg);
1341                 /* Only on the original revision: IDE DMA can hang */
1342                 if (pdev->revision == 0x00)
1343                         no_piix_dma = 1;
1344                 /* On all revisions below 5 PXB bus lock must be disabled for IDE */
1345                 else if (cfg & (1<<14) && pdev->revision < 5)
1346                         no_piix_dma = 2;
1347         }
1348         if (no_piix_dma)
1349                 dev_warn(&ata_dev->dev,
1350                          "450NX errata present, disabling IDE DMA%s\n",
1351                          no_piix_dma == 2 ? " - a BIOS update may resolve this"
1352                          : "");
1353
1354         return no_piix_dma;
1355 }
1356
1357 static void __devinit piix_init_pcs(struct ata_host *host,
1358                                     const struct piix_map_db *map_db)
1359 {
1360         struct pci_dev *pdev = to_pci_dev(host->dev);
1361         u16 pcs, new_pcs;
1362
1363         pci_read_config_word(pdev, ICH5_PCS, &pcs);
1364
1365         new_pcs = pcs | map_db->port_enable;
1366
1367         if (new_pcs != pcs) {
1368                 DPRINTK("updating PCS from 0x%x to 0x%x\n", pcs, new_pcs);
1369                 pci_write_config_word(pdev, ICH5_PCS, new_pcs);
1370                 msleep(150);
1371         }
1372 }
1373
1374 static const int *__devinit piix_init_sata_map(struct pci_dev *pdev,
1375                                                struct ata_port_info *pinfo,
1376                                                const struct piix_map_db *map_db)
1377 {
1378         const int *map;
1379         int i, invalid_map = 0;
1380         u8 map_value;
1381
1382         pci_read_config_byte(pdev, ICH5_PMR, &map_value);
1383
1384         map = map_db->map[map_value & map_db->mask];
1385
1386         dev_info(&pdev->dev, "MAP [");
1387         for (i = 0; i < 4; i++) {
1388                 switch (map[i]) {
1389                 case RV:
1390                         invalid_map = 1;
1391                         pr_cont(" XX");
1392                         break;
1393
1394                 case NA:
1395                         pr_cont(" --");
1396                         break;
1397
1398                 case IDE:
1399                         WARN_ON((i & 1) || map[i + 1] != IDE);
1400                         pinfo[i / 2] = piix_port_info[ich_pata_100];
1401                         i++;
1402                         pr_cont(" IDE IDE");
1403                         break;
1404
1405                 default:
1406                         pr_cont(" P%d", map[i]);
1407                         if (i & 1)
1408                                 pinfo[i / 2].flags |= ATA_FLAG_SLAVE_POSS;
1409                         break;
1410                 }
1411         }
1412         pr_cont(" ]\n");
1413
1414         if (invalid_map)
1415                 dev_err(&pdev->dev, "invalid MAP value %u\n", map_value);
1416
1417         return map;
1418 }
1419
1420 static bool piix_no_sidpr(struct ata_host *host)
1421 {
1422         struct pci_dev *pdev = to_pci_dev(host->dev);
1423
1424         /*
1425          * Samsung DB-P70 only has three ATA ports exposed and
1426          * curiously the unconnected first port reports link online
1427          * while not responding to SRST protocol causing excessive
1428          * detection delay.
1429          *
1430          * Unfortunately, the system doesn't carry enough DMI
1431          * information to identify the machine but does have subsystem
1432          * vendor and device set.  As it's unclear whether the
1433          * subsystem vendor/device is used only for this specific
1434          * board, the port can't be disabled solely with the
1435          * information; however, turning off SIDPR access works around
1436          * the problem.  Turn it off.
1437          *
1438          * This problem is reported in bnc#441240.
1439          *
1440          * https://bugzilla.novell.com/show_bug.cgi?id=441420
1441          */
1442         if (pdev->vendor == PCI_VENDOR_ID_INTEL && pdev->device == 0x2920 &&
1443             pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG &&
1444             pdev->subsystem_device == 0xb049) {
1445                 dev_warn(host->dev,
1446                          "Samsung DB-P70 detected, disabling SIDPR\n");
1447                 return true;
1448         }
1449
1450         return false;
1451 }
1452
1453 static int __devinit piix_init_sidpr(struct ata_host *host)
1454 {
1455         struct pci_dev *pdev = to_pci_dev(host->dev);
1456         struct piix_host_priv *hpriv = host->private_data;
1457         struct ata_link *link0 = &host->ports[0]->link;
1458         u32 scontrol;
1459         int i, rc;
1460
1461         /* check for availability */
1462         for (i = 0; i < 4; i++)
1463                 if (hpriv->map[i] == IDE)
1464                         return 0;
1465
1466         /* is it blacklisted? */
1467         if (piix_no_sidpr(host))
1468                 return 0;
1469
1470         if (!(host->ports[0]->flags & PIIX_FLAG_SIDPR))
1471                 return 0;
1472
1473         if (pci_resource_start(pdev, PIIX_SIDPR_BAR) == 0 ||
1474             pci_resource_len(pdev, PIIX_SIDPR_BAR) != PIIX_SIDPR_LEN)
1475                 return 0;
1476
1477         if (pcim_iomap_regions(pdev, 1 << PIIX_SIDPR_BAR, DRV_NAME))
1478                 return 0;
1479
1480         hpriv->sidpr = pcim_iomap_table(pdev)[PIIX_SIDPR_BAR];
1481
1482         /* SCR access via SIDPR doesn't work on some configurations.
1483          * Give it a test drive by inhibiting power save modes which
1484          * we'll do anyway.
1485          */
1486         piix_sidpr_scr_read(link0, SCR_CONTROL, &scontrol);
1487
1488         /* if IPM is already 3, SCR access is probably working.  Don't
1489          * un-inhibit power save modes as BIOS might have inhibited
1490          * them for a reason.
1491          */
1492         if ((scontrol & 0xf00) != 0x300) {
1493                 scontrol |= 0x300;
1494                 piix_sidpr_scr_write(link0, SCR_CONTROL, scontrol);
1495                 piix_sidpr_scr_read(link0, SCR_CONTROL, &scontrol);
1496
1497                 if ((scontrol & 0xf00) != 0x300) {
1498                         dev_info(host->dev,
1499                                  "SCR access via SIDPR is available but doesn't work\n");
1500                         return 0;
1501                 }
1502         }
1503
1504         /* okay, SCRs available, set ops and ask libata for slave_link */
1505         for (i = 0; i < 2; i++) {
1506                 struct ata_port *ap = host->ports[i];
1507
1508                 ap->ops = &piix_sidpr_sata_ops;
1509
1510                 if (ap->flags & ATA_FLAG_SLAVE_POSS) {
1511                         rc = ata_slave_link_init(ap);
1512                         if (rc)
1513                                 return rc;
1514                 }
1515         }
1516
1517         return 0;
1518 }
1519
1520 static void piix_iocfg_bit18_quirk(struct ata_host *host)
1521 {
1522         static const struct dmi_system_id sysids[] = {
1523                 {
1524                         /* Clevo M570U sets IOCFG bit 18 if the cdrom
1525                          * isn't used to boot the system which
1526                          * disables the channel.
1527                          */
1528                         .ident = "M570U",
1529                         .matches = {
1530                                 DMI_MATCH(DMI_SYS_VENDOR, "Clevo Co."),
1531                                 DMI_MATCH(DMI_PRODUCT_NAME, "M570U"),
1532                         },
1533                 },
1534
1535                 { }     /* terminate list */
1536         };
1537         struct pci_dev *pdev = to_pci_dev(host->dev);
1538         struct piix_host_priv *hpriv = host->private_data;
1539
1540         if (!dmi_check_system(sysids))
1541                 return;
1542
1543         /* The datasheet says that bit 18 is NOOP but certain systems
1544          * seem to use it to disable a channel.  Clear the bit on the
1545          * affected systems.
1546          */
1547         if (hpriv->saved_iocfg & (1 << 18)) {
1548                 dev_info(&pdev->dev, "applying IOCFG bit18 quirk\n");
1549                 pci_write_config_dword(pdev, PIIX_IOCFG,
1550                                        hpriv->saved_iocfg & ~(1 << 18));
1551         }
1552 }
1553
1554 static bool piix_broken_system_poweroff(struct pci_dev *pdev)
1555 {
1556         static const struct dmi_system_id broken_systems[] = {
1557                 {
1558                         .ident = "HP Compaq 2510p",
1559                         .matches = {
1560                                 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1561                                 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq 2510p"),
1562                         },
1563                         /* PCI slot number of the controller */
1564                         .driver_data = (void *)0x1FUL,
1565                 },
1566                 {
1567                         .ident = "HP Compaq nc6000",
1568                         .matches = {
1569                                 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1570                                 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nc6000"),
1571                         },
1572                         /* PCI slot number of the controller */
1573                         .driver_data = (void *)0x1FUL,
1574                 },
1575
1576                 { }     /* terminate list */
1577         };
1578         const struct dmi_system_id *dmi = dmi_first_match(broken_systems);
1579
1580         if (dmi) {
1581                 unsigned long slot = (unsigned long)dmi->driver_data;
1582                 /* apply the quirk only to on-board controllers */
1583                 return slot == PCI_SLOT(pdev->devfn);
1584         }
1585
1586         return false;
1587 }
1588
1589 /**
1590  *      piix_init_one - Register PIIX ATA PCI device with kernel services
1591  *      @pdev: PCI device to register
1592  *      @ent: Entry in piix_pci_tbl matching with @pdev
1593  *
1594  *      Called from kernel PCI layer.  We probe for combined mode (sigh),
1595  *      and then hand over control to libata, for it to do the rest.
1596  *
1597  *      LOCKING:
1598  *      Inherited from PCI layer (may sleep).
1599  *
1600  *      RETURNS:
1601  *      Zero on success, or -ERRNO value.
1602  */
1603
1604 static int __devinit piix_init_one(struct pci_dev *pdev,
1605                                    const struct pci_device_id *ent)
1606 {
1607         struct device *dev = &pdev->dev;
1608         struct ata_port_info port_info[2];
1609         const struct ata_port_info *ppi[] = { &port_info[0], &port_info[1] };
1610         struct scsi_host_template *sht = &piix_sht;
1611         unsigned long port_flags;
1612         struct ata_host *host;
1613         struct piix_host_priv *hpriv;
1614         int rc;
1615
1616         ata_print_version_once(&pdev->dev, DRV_VERSION);
1617
1618         /* no hotplugging support for later devices (FIXME) */
1619         if (!in_module_init && ent->driver_data >= ich5_sata)
1620                 return -ENODEV;
1621
1622         if (piix_broken_system_poweroff(pdev)) {
1623                 piix_port_info[ent->driver_data].flags |=
1624                                 ATA_FLAG_NO_POWEROFF_SPINDOWN |
1625                                         ATA_FLAG_NO_HIBERNATE_SPINDOWN;
1626                 dev_info(&pdev->dev, "quirky BIOS, skipping spindown "
1627                                 "on poweroff and hibernation\n");
1628         }
1629
1630         port_info[0] = piix_port_info[ent->driver_data];
1631         port_info[1] = piix_port_info[ent->driver_data];
1632
1633         port_flags = port_info[0].flags;
1634
1635         /* enable device and prepare host */
1636         rc = pcim_enable_device(pdev);
1637         if (rc)
1638                 return rc;
1639
1640         hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
1641         if (!hpriv)
1642                 return -ENOMEM;
1643
1644         /* Save IOCFG, this will be used for cable detection, quirk
1645          * detection and restoration on detach.  This is necessary
1646          * because some ACPI implementations mess up cable related
1647          * bits on _STM.  Reported on kernel bz#11879.
1648          */
1649         pci_read_config_dword(pdev, PIIX_IOCFG, &hpriv->saved_iocfg);
1650
1651         /* ICH6R may be driven by either ata_piix or ahci driver
1652          * regardless of BIOS configuration.  Make sure AHCI mode is
1653          * off.
1654          */
1655         if (pdev->vendor == PCI_VENDOR_ID_INTEL && pdev->device == 0x2652) {
1656                 rc = piix_disable_ahci(pdev);
1657                 if (rc)
1658                         return rc;
1659         }
1660
1661         /* SATA map init can change port_info, do it before prepping host */
1662         if (port_flags & ATA_FLAG_SATA)
1663                 hpriv->map = piix_init_sata_map(pdev, port_info,
1664                                         piix_map_db_table[ent->driver_data]);
1665
1666         rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
1667         if (rc)
1668                 return rc;
1669         host->private_data = hpriv;
1670
1671         /* initialize controller */
1672         if (port_flags & ATA_FLAG_SATA) {
1673                 piix_init_pcs(host, piix_map_db_table[ent->driver_data]);
1674                 rc = piix_init_sidpr(host);
1675                 if (rc)
1676                         return rc;
1677                 if (host->ports[0]->ops == &piix_sidpr_sata_ops)
1678                         sht = &piix_sidpr_sht;
1679         }
1680
1681         /* apply IOCFG bit18 quirk */
1682         piix_iocfg_bit18_quirk(host);
1683
1684         /* On ICH5, some BIOSen disable the interrupt using the
1685          * PCI_COMMAND_INTX_DISABLE bit added in PCI 2.3.
1686          * On ICH6, this bit has the same effect, but only when
1687          * MSI is disabled (and it is disabled, as we don't use
1688          * message-signalled interrupts currently).
1689          */
1690         if (port_flags & PIIX_FLAG_CHECKINTR)
1691                 pci_intx(pdev, 1);
1692
1693         if (piix_check_450nx_errata(pdev)) {
1694                 /* This writes into the master table but it does not
1695                    really matter for this errata as we will apply it to
1696                    all the PIIX devices on the board */
1697                 host->ports[0]->mwdma_mask = 0;
1698                 host->ports[0]->udma_mask = 0;
1699                 host->ports[1]->mwdma_mask = 0;
1700                 host->ports[1]->udma_mask = 0;
1701         }
1702         host->flags |= ATA_HOST_PARALLEL_SCAN;
1703
1704         pci_set_master(pdev);
1705         return ata_pci_sff_activate_host(host, ata_bmdma_interrupt, sht);
1706 }
1707
1708 static void piix_remove_one(struct pci_dev *pdev)
1709 {
1710         struct ata_host *host = dev_get_drvdata(&pdev->dev);
1711         struct piix_host_priv *hpriv = host->private_data;
1712
1713         pci_write_config_dword(pdev, PIIX_IOCFG, hpriv->saved_iocfg);
1714
1715         ata_pci_remove_one(pdev);
1716 }
1717
1718 static int __init piix_init(void)
1719 {
1720         int rc;
1721
1722         DPRINTK("pci_register_driver\n");
1723         rc = pci_register_driver(&piix_pci_driver);
1724         if (rc)
1725                 return rc;
1726
1727         in_module_init = 0;
1728
1729         DPRINTK("done\n");
1730         return 0;
1731 }
1732
1733 static void __exit piix_exit(void)
1734 {
1735         pci_unregister_driver(&piix_pci_driver);
1736 }
1737
1738 module_init(piix_init);
1739 module_exit(piix_exit);