27d2225c9083ee4c26323d32df7e9a1d6b96eae9
[safe/jmp/linux-2.6] / drivers / ata / sata_nv.c
1 /*
2  *  sata_nv.c - NVIDIA nForce SATA
3  *
4  *  Copyright 2004 NVIDIA Corp.  All rights reserved.
5  *  Copyright 2004 Andrew Chew
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2, or (at your option)
11  *  any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; see the file COPYING.  If not, write to
20  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  *
23  *  libata documentation is available via 'make {ps|pdf}docs',
24  *  as Documentation/DocBook/libata.*
25  *
26  *  No hardware documentation available outside of NVIDIA.
27  *  This driver programs the NVIDIA SATA controller in a similar
28  *  fashion as with other PCI IDE BMDMA controllers, with a few
29  *  NV-specific details such as register offsets, SATA phy location,
30  *  hotplug info, etc.
31  *
32  *  CK804/MCP04 controllers support an alternate programming interface
33  *  similar to the ADMA specification (with some modifications).
34  *  This allows the use of NCQ. Non-DMA-mapped ATA commands are still
35  *  sent through the legacy interface.
36  *
37  */
38
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/pci.h>
42 #include <linux/init.h>
43 #include <linux/blkdev.h>
44 #include <linux/delay.h>
45 #include <linux/interrupt.h>
46 #include <linux/device.h>
47 #include <scsi/scsi_host.h>
48 #include <scsi/scsi_device.h>
49 #include <linux/libata.h>
50
51 #define DRV_NAME                        "sata_nv"
52 #define DRV_VERSION                     "3.2"
53
54 #define NV_ADMA_DMA_BOUNDARY            0xffffffffUL
55
56 enum {
57         NV_PORTS                        = 2,
58         NV_PIO_MASK                     = 0x1f,
59         NV_MWDMA_MASK                   = 0x07,
60         NV_UDMA_MASK                    = 0x7f,
61         NV_PORT0_SCR_REG_OFFSET         = 0x00,
62         NV_PORT1_SCR_REG_OFFSET         = 0x40,
63
64         /* INT_STATUS/ENABLE */
65         NV_INT_STATUS                   = 0x10,
66         NV_INT_ENABLE                   = 0x11,
67         NV_INT_STATUS_CK804             = 0x440,
68         NV_INT_ENABLE_CK804             = 0x441,
69
70         /* INT_STATUS/ENABLE bits */
71         NV_INT_DEV                      = 0x01,
72         NV_INT_PM                       = 0x02,
73         NV_INT_ADDED                    = 0x04,
74         NV_INT_REMOVED                  = 0x08,
75
76         NV_INT_PORT_SHIFT               = 4,    /* each port occupies 4 bits */
77
78         NV_INT_ALL                      = 0x0f,
79         NV_INT_MASK                     = NV_INT_DEV |
80                                           NV_INT_ADDED | NV_INT_REMOVED,
81
82         /* INT_CONFIG */
83         NV_INT_CONFIG                   = 0x12,
84         NV_INT_CONFIG_METHD             = 0x01, // 0 = INT, 1 = SMI
85
86         // For PCI config register 20
87         NV_MCP_SATA_CFG_20              = 0x50,
88         NV_MCP_SATA_CFG_20_SATA_SPACE_EN = 0x04,
89         NV_MCP_SATA_CFG_20_PORT0_EN     = (1 << 17),
90         NV_MCP_SATA_CFG_20_PORT1_EN     = (1 << 16),
91         NV_MCP_SATA_CFG_20_PORT0_PWB_EN = (1 << 14),
92         NV_MCP_SATA_CFG_20_PORT1_PWB_EN = (1 << 12),
93
94         NV_ADMA_MAX_CPBS                = 32,
95         NV_ADMA_CPB_SZ                  = 128,
96         NV_ADMA_APRD_SZ                 = 16,
97         NV_ADMA_SGTBL_LEN               = (1024 - NV_ADMA_CPB_SZ) /
98                                            NV_ADMA_APRD_SZ,
99         NV_ADMA_SGTBL_TOTAL_LEN         = NV_ADMA_SGTBL_LEN + 5,
100         NV_ADMA_SGTBL_SZ                = NV_ADMA_SGTBL_LEN * NV_ADMA_APRD_SZ,
101         NV_ADMA_PORT_PRIV_DMA_SZ        = NV_ADMA_MAX_CPBS *
102                                            (NV_ADMA_CPB_SZ + NV_ADMA_SGTBL_SZ),
103
104         /* BAR5 offset to ADMA general registers */
105         NV_ADMA_GEN                     = 0x400,
106         NV_ADMA_GEN_CTL                 = 0x00,
107         NV_ADMA_NOTIFIER_CLEAR          = 0x30,
108
109         /* BAR5 offset to ADMA ports */
110         NV_ADMA_PORT                    = 0x480,
111
112         /* size of ADMA port register space  */
113         NV_ADMA_PORT_SIZE               = 0x100,
114
115         /* ADMA port registers */
116         NV_ADMA_CTL                     = 0x40,
117         NV_ADMA_CPB_COUNT               = 0x42,
118         NV_ADMA_NEXT_CPB_IDX            = 0x43,
119         NV_ADMA_STAT                    = 0x44,
120         NV_ADMA_CPB_BASE_LOW            = 0x48,
121         NV_ADMA_CPB_BASE_HIGH           = 0x4C,
122         NV_ADMA_APPEND                  = 0x50,
123         NV_ADMA_NOTIFIER                = 0x68,
124         NV_ADMA_NOTIFIER_ERROR          = 0x6C,
125
126         /* NV_ADMA_CTL register bits */
127         NV_ADMA_CTL_HOTPLUG_IEN         = (1 << 0),
128         NV_ADMA_CTL_CHANNEL_RESET       = (1 << 5),
129         NV_ADMA_CTL_GO                  = (1 << 7),
130         NV_ADMA_CTL_AIEN                = (1 << 8),
131         NV_ADMA_CTL_READ_NON_COHERENT   = (1 << 11),
132         NV_ADMA_CTL_WRITE_NON_COHERENT  = (1 << 12),
133
134         /* CPB response flag bits */
135         NV_CPB_RESP_DONE                = (1 << 0),
136         NV_CPB_RESP_ATA_ERR             = (1 << 3),
137         NV_CPB_RESP_CMD_ERR             = (1 << 4),
138         NV_CPB_RESP_CPB_ERR             = (1 << 7),
139
140         /* CPB control flag bits */
141         NV_CPB_CTL_CPB_VALID            = (1 << 0),
142         NV_CPB_CTL_QUEUE                = (1 << 1),
143         NV_CPB_CTL_APRD_VALID           = (1 << 2),
144         NV_CPB_CTL_IEN                  = (1 << 3),
145         NV_CPB_CTL_FPDMA                = (1 << 4),
146
147         /* APRD flags */
148         NV_APRD_WRITE                   = (1 << 1),
149         NV_APRD_END                     = (1 << 2),
150         NV_APRD_CONT                    = (1 << 3),
151
152         /* NV_ADMA_STAT flags */
153         NV_ADMA_STAT_TIMEOUT            = (1 << 0),
154         NV_ADMA_STAT_HOTUNPLUG          = (1 << 1),
155         NV_ADMA_STAT_HOTPLUG            = (1 << 2),
156         NV_ADMA_STAT_CPBERR             = (1 << 4),
157         NV_ADMA_STAT_SERROR             = (1 << 5),
158         NV_ADMA_STAT_CMD_COMPLETE       = (1 << 6),
159         NV_ADMA_STAT_IDLE               = (1 << 8),
160         NV_ADMA_STAT_LEGACY             = (1 << 9),
161         NV_ADMA_STAT_STOPPED            = (1 << 10),
162         NV_ADMA_STAT_DONE               = (1 << 12),
163         NV_ADMA_STAT_ERR                = NV_ADMA_STAT_CPBERR |
164                                           NV_ADMA_STAT_TIMEOUT,
165
166         /* port flags */
167         NV_ADMA_PORT_REGISTER_MODE      = (1 << 0),
168         NV_ADMA_ATAPI_SETUP_COMPLETE    = (1 << 1),
169
170 };
171
172 /* ADMA Physical Region Descriptor - one SG segment */
173 struct nv_adma_prd {
174         __le64                  addr;
175         __le32                  len;
176         u8                      flags;
177         u8                      packet_len;
178         __le16                  reserved;
179 };
180
181 enum nv_adma_regbits {
182         CMDEND  = (1 << 15),            /* end of command list */
183         WNB     = (1 << 14),            /* wait-not-BSY */
184         IGN     = (1 << 13),            /* ignore this entry */
185         CS1n    = (1 << (4 + 8)),       /* std. PATA signals follow... */
186         DA2     = (1 << (2 + 8)),
187         DA1     = (1 << (1 + 8)),
188         DA0     = (1 << (0 + 8)),
189 };
190
191 /* ADMA Command Parameter Block
192    The first 5 SG segments are stored inside the Command Parameter Block itself.
193    If there are more than 5 segments the remainder are stored in a separate
194    memory area indicated by next_aprd. */
195 struct nv_adma_cpb {
196         u8                      resp_flags;    /* 0 */
197         u8                      reserved1;     /* 1 */
198         u8                      ctl_flags;     /* 2 */
199         /* len is length of taskfile in 64 bit words */
200         u8                      len;           /* 3  */
201         u8                      tag;           /* 4 */
202         u8                      next_cpb_idx;  /* 5 */
203         __le16                  reserved2;     /* 6-7 */
204         __le16                  tf[12];        /* 8-31 */
205         struct nv_adma_prd      aprd[5];       /* 32-111 */
206         __le64                  next_aprd;     /* 112-119 */
207         __le64                  reserved3;     /* 120-127 */
208 };
209
210
211 struct nv_adma_port_priv {
212         struct nv_adma_cpb      *cpb;
213         dma_addr_t              cpb_dma;
214         struct nv_adma_prd      *aprd;
215         dma_addr_t              aprd_dma;
216         u8                      flags;
217 };
218
219 #define NV_ADMA_CHECK_INTR(GCTL, PORT) ((GCTL) & ( 1 << (19 + (12 * (PORT)))))
220
221 static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
222 static void nv_ck804_host_stop(struct ata_host *host);
223 static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance);
224 static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance);
225 static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance);
226 static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg);
227 static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
228
229 static void nv_nf2_freeze(struct ata_port *ap);
230 static void nv_nf2_thaw(struct ata_port *ap);
231 static void nv_ck804_freeze(struct ata_port *ap);
232 static void nv_ck804_thaw(struct ata_port *ap);
233 static void nv_error_handler(struct ata_port *ap);
234 static int nv_adma_slave_config(struct scsi_device *sdev);
235 static int nv_adma_check_atapi_dma(struct ata_queued_cmd *qc);
236 static void nv_adma_qc_prep(struct ata_queued_cmd *qc);
237 static unsigned int nv_adma_qc_issue(struct ata_queued_cmd *qc);
238 static irqreturn_t nv_adma_interrupt(int irq, void *dev_instance);
239 static void nv_adma_irq_clear(struct ata_port *ap);
240 static int nv_adma_port_start(struct ata_port *ap);
241 static void nv_adma_port_stop(struct ata_port *ap);
242 static void nv_adma_error_handler(struct ata_port *ap);
243 static void nv_adma_host_stop(struct ata_host *host);
244 static void nv_adma_bmdma_setup(struct ata_queued_cmd *qc);
245 static void nv_adma_bmdma_start(struct ata_queued_cmd *qc);
246 static void nv_adma_bmdma_stop(struct ata_queued_cmd *qc);
247 static u8 nv_adma_bmdma_status(struct ata_port *ap);
248
249 enum nv_host_type
250 {
251         GENERIC,
252         NFORCE2,
253         NFORCE3 = NFORCE2,      /* NF2 == NF3 as far as sata_nv is concerned */
254         CK804,
255         ADMA
256 };
257
258 static const struct pci_device_id nv_pci_tbl[] = {
259         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA), NFORCE2 },
260         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA), NFORCE3 },
261         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2), NFORCE3 },
262         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA), CK804 },
263         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2), CK804 },
264         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA), CK804 },
265         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2), CK804 },
266         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA), GENERIC },
267         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2), GENERIC },
268         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA), GENERIC },
269         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2), GENERIC },
270         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA), GENERIC },
271         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA2), GENERIC },
272         { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA3), GENERIC },
273         { PCI_VDEVICE(NVIDIA, 0x045c), GENERIC }, /* MCP65 */
274         { PCI_VDEVICE(NVIDIA, 0x045d), GENERIC }, /* MCP65 */
275         { PCI_VDEVICE(NVIDIA, 0x045e), GENERIC }, /* MCP65 */
276         { PCI_VDEVICE(NVIDIA, 0x045f), GENERIC }, /* MCP65 */
277         { PCI_VDEVICE(NVIDIA, 0x0550), GENERIC }, /* MCP67 */
278         { PCI_VDEVICE(NVIDIA, 0x0551), GENERIC }, /* MCP67 */
279         { PCI_VDEVICE(NVIDIA, 0x0552), GENERIC }, /* MCP67 */
280         { PCI_VDEVICE(NVIDIA, 0x0553), GENERIC }, /* MCP67 */
281         { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
282                 PCI_ANY_ID, PCI_ANY_ID,
283                 PCI_CLASS_STORAGE_IDE<<8, 0xffff00, GENERIC },
284         { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
285                 PCI_ANY_ID, PCI_ANY_ID,
286                 PCI_CLASS_STORAGE_RAID<<8, 0xffff00, GENERIC },
287
288         { } /* terminate list */
289 };
290
291 static struct pci_driver nv_pci_driver = {
292         .name                   = DRV_NAME,
293         .id_table               = nv_pci_tbl,
294         .probe                  = nv_init_one,
295         .remove                 = ata_pci_remove_one,
296 };
297
298 static struct scsi_host_template nv_sht = {
299         .module                 = THIS_MODULE,
300         .name                   = DRV_NAME,
301         .ioctl                  = ata_scsi_ioctl,
302         .queuecommand           = ata_scsi_queuecmd,
303         .can_queue              = ATA_DEF_QUEUE,
304         .this_id                = ATA_SHT_THIS_ID,
305         .sg_tablesize           = LIBATA_MAX_PRD,
306         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
307         .emulated               = ATA_SHT_EMULATED,
308         .use_clustering         = ATA_SHT_USE_CLUSTERING,
309         .proc_name              = DRV_NAME,
310         .dma_boundary           = ATA_DMA_BOUNDARY,
311         .slave_configure        = ata_scsi_slave_config,
312         .slave_destroy          = ata_scsi_slave_destroy,
313         .bios_param             = ata_std_bios_param,
314 };
315
316 static struct scsi_host_template nv_adma_sht = {
317         .module                 = THIS_MODULE,
318         .name                   = DRV_NAME,
319         .ioctl                  = ata_scsi_ioctl,
320         .queuecommand           = ata_scsi_queuecmd,
321         .can_queue              = NV_ADMA_MAX_CPBS,
322         .this_id                = ATA_SHT_THIS_ID,
323         .sg_tablesize           = NV_ADMA_SGTBL_TOTAL_LEN,
324         .max_sectors            = ATA_MAX_SECTORS,
325         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
326         .emulated               = ATA_SHT_EMULATED,
327         .use_clustering         = ATA_SHT_USE_CLUSTERING,
328         .proc_name              = DRV_NAME,
329         .dma_boundary           = NV_ADMA_DMA_BOUNDARY,
330         .slave_configure        = nv_adma_slave_config,
331         .slave_destroy          = ata_scsi_slave_destroy,
332         .bios_param             = ata_std_bios_param,
333 };
334
335 static const struct ata_port_operations nv_generic_ops = {
336         .port_disable           = ata_port_disable,
337         .tf_load                = ata_tf_load,
338         .tf_read                = ata_tf_read,
339         .exec_command           = ata_exec_command,
340         .check_status           = ata_check_status,
341         .dev_select             = ata_std_dev_select,
342         .bmdma_setup            = ata_bmdma_setup,
343         .bmdma_start            = ata_bmdma_start,
344         .bmdma_stop             = ata_bmdma_stop,
345         .bmdma_status           = ata_bmdma_status,
346         .qc_prep                = ata_qc_prep,
347         .qc_issue               = ata_qc_issue_prot,
348         .freeze                 = ata_bmdma_freeze,
349         .thaw                   = ata_bmdma_thaw,
350         .error_handler          = nv_error_handler,
351         .post_internal_cmd      = ata_bmdma_post_internal_cmd,
352         .data_xfer              = ata_pio_data_xfer,
353         .irq_handler            = nv_generic_interrupt,
354         .irq_clear              = ata_bmdma_irq_clear,
355         .scr_read               = nv_scr_read,
356         .scr_write              = nv_scr_write,
357         .port_start             = ata_port_start,
358         .port_stop              = ata_port_stop,
359         .host_stop              = ata_pci_host_stop,
360 };
361
362 static const struct ata_port_operations nv_nf2_ops = {
363         .port_disable           = ata_port_disable,
364         .tf_load                = ata_tf_load,
365         .tf_read                = ata_tf_read,
366         .exec_command           = ata_exec_command,
367         .check_status           = ata_check_status,
368         .dev_select             = ata_std_dev_select,
369         .bmdma_setup            = ata_bmdma_setup,
370         .bmdma_start            = ata_bmdma_start,
371         .bmdma_stop             = ata_bmdma_stop,
372         .bmdma_status           = ata_bmdma_status,
373         .qc_prep                = ata_qc_prep,
374         .qc_issue               = ata_qc_issue_prot,
375         .freeze                 = nv_nf2_freeze,
376         .thaw                   = nv_nf2_thaw,
377         .error_handler          = nv_error_handler,
378         .post_internal_cmd      = ata_bmdma_post_internal_cmd,
379         .data_xfer              = ata_pio_data_xfer,
380         .irq_handler            = nv_nf2_interrupt,
381         .irq_clear              = ata_bmdma_irq_clear,
382         .scr_read               = nv_scr_read,
383         .scr_write              = nv_scr_write,
384         .port_start             = ata_port_start,
385         .port_stop              = ata_port_stop,
386         .host_stop              = ata_pci_host_stop,
387 };
388
389 static const struct ata_port_operations nv_ck804_ops = {
390         .port_disable           = ata_port_disable,
391         .tf_load                = ata_tf_load,
392         .tf_read                = ata_tf_read,
393         .exec_command           = ata_exec_command,
394         .check_status           = ata_check_status,
395         .dev_select             = ata_std_dev_select,
396         .bmdma_setup            = ata_bmdma_setup,
397         .bmdma_start            = ata_bmdma_start,
398         .bmdma_stop             = ata_bmdma_stop,
399         .bmdma_status           = ata_bmdma_status,
400         .qc_prep                = ata_qc_prep,
401         .qc_issue               = ata_qc_issue_prot,
402         .freeze                 = nv_ck804_freeze,
403         .thaw                   = nv_ck804_thaw,
404         .error_handler          = nv_error_handler,
405         .post_internal_cmd      = ata_bmdma_post_internal_cmd,
406         .data_xfer              = ata_pio_data_xfer,
407         .irq_handler            = nv_ck804_interrupt,
408         .irq_clear              = ata_bmdma_irq_clear,
409         .scr_read               = nv_scr_read,
410         .scr_write              = nv_scr_write,
411         .port_start             = ata_port_start,
412         .port_stop              = ata_port_stop,
413         .host_stop              = nv_ck804_host_stop,
414 };
415
416 static const struct ata_port_operations nv_adma_ops = {
417         .port_disable           = ata_port_disable,
418         .tf_load                = ata_tf_load,
419         .tf_read                = ata_tf_read,
420         .check_atapi_dma        = nv_adma_check_atapi_dma,
421         .exec_command           = ata_exec_command,
422         .check_status           = ata_check_status,
423         .dev_select             = ata_std_dev_select,
424         .bmdma_setup            = nv_adma_bmdma_setup,
425         .bmdma_start            = nv_adma_bmdma_start,
426         .bmdma_stop             = nv_adma_bmdma_stop,
427         .bmdma_status           = nv_adma_bmdma_status,
428         .qc_prep                = nv_adma_qc_prep,
429         .qc_issue               = nv_adma_qc_issue,
430         .freeze                 = nv_ck804_freeze,
431         .thaw                   = nv_ck804_thaw,
432         .error_handler          = nv_adma_error_handler,
433         .post_internal_cmd      = nv_adma_bmdma_stop,
434         .data_xfer              = ata_mmio_data_xfer,
435         .irq_handler            = nv_adma_interrupt,
436         .irq_clear              = nv_adma_irq_clear,
437         .scr_read               = nv_scr_read,
438         .scr_write              = nv_scr_write,
439         .port_start             = nv_adma_port_start,
440         .port_stop              = nv_adma_port_stop,
441         .host_stop              = nv_adma_host_stop,
442 };
443
444 static struct ata_port_info nv_port_info[] = {
445         /* generic */
446         {
447                 .sht            = &nv_sht,
448                 .flags          = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
449                                   ATA_FLAG_HRST_TO_RESUME,
450                 .pio_mask       = NV_PIO_MASK,
451                 .mwdma_mask     = NV_MWDMA_MASK,
452                 .udma_mask      = NV_UDMA_MASK,
453                 .port_ops       = &nv_generic_ops,
454         },
455         /* nforce2/3 */
456         {
457                 .sht            = &nv_sht,
458                 .flags          = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
459                                   ATA_FLAG_HRST_TO_RESUME,
460                 .pio_mask       = NV_PIO_MASK,
461                 .mwdma_mask     = NV_MWDMA_MASK,
462                 .udma_mask      = NV_UDMA_MASK,
463                 .port_ops       = &nv_nf2_ops,
464         },
465         /* ck804 */
466         {
467                 .sht            = &nv_sht,
468                 .flags          = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
469                                   ATA_FLAG_HRST_TO_RESUME,
470                 .pio_mask       = NV_PIO_MASK,
471                 .mwdma_mask     = NV_MWDMA_MASK,
472                 .udma_mask      = NV_UDMA_MASK,
473                 .port_ops       = &nv_ck804_ops,
474         },
475         /* ADMA */
476         {
477                 .sht            = &nv_adma_sht,
478                 .flags          = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
479                                   ATA_FLAG_MMIO | ATA_FLAG_NCQ,
480                 .pio_mask       = NV_PIO_MASK,
481                 .mwdma_mask     = NV_MWDMA_MASK,
482                 .udma_mask      = NV_UDMA_MASK,
483                 .port_ops       = &nv_adma_ops,
484         },
485 };
486
487 MODULE_AUTHOR("NVIDIA");
488 MODULE_DESCRIPTION("low-level driver for NVIDIA nForce SATA controller");
489 MODULE_LICENSE("GPL");
490 MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
491 MODULE_VERSION(DRV_VERSION);
492
493 static int adma_enabled = 1;
494
495 static inline void __iomem *__nv_adma_ctl_block(void __iomem *mmio,
496                                                 unsigned int port_no)
497 {
498         mmio += NV_ADMA_PORT + port_no * NV_ADMA_PORT_SIZE;
499         return mmio;
500 }
501
502 static inline void __iomem *nv_adma_ctl_block(struct ata_port *ap)
503 {
504         return __nv_adma_ctl_block(ap->host->mmio_base, ap->port_no);
505 }
506
507 static inline void __iomem *nv_adma_gen_block(struct ata_port *ap)
508 {
509         return (ap->host->mmio_base + NV_ADMA_GEN);
510 }
511
512 static inline void __iomem *nv_adma_notifier_clear_block(struct ata_port *ap)
513 {
514         return (nv_adma_gen_block(ap) + NV_ADMA_NOTIFIER_CLEAR + (4 * ap->port_no));
515 }
516
517 static void nv_adma_register_mode(struct ata_port *ap)
518 {
519         void __iomem *mmio = nv_adma_ctl_block(ap);
520         struct nv_adma_port_priv *pp = ap->private_data;
521         u16 tmp;
522
523         if (pp->flags & NV_ADMA_PORT_REGISTER_MODE)
524                 return;
525
526         tmp = readw(mmio + NV_ADMA_CTL);
527         writew(tmp & ~NV_ADMA_CTL_GO, mmio + NV_ADMA_CTL);
528
529         pp->flags |= NV_ADMA_PORT_REGISTER_MODE;
530 }
531
532 static void nv_adma_mode(struct ata_port *ap)
533 {
534         void __iomem *mmio = nv_adma_ctl_block(ap);
535         struct nv_adma_port_priv *pp = ap->private_data;
536         u16 tmp;
537
538         if (!(pp->flags & NV_ADMA_PORT_REGISTER_MODE))
539                 return;
540                 
541         WARN_ON(pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE);
542
543         tmp = readw(mmio + NV_ADMA_CTL);
544         writew(tmp | NV_ADMA_CTL_GO, mmio + NV_ADMA_CTL);
545
546         pp->flags &= ~NV_ADMA_PORT_REGISTER_MODE;
547 }
548
549 static int nv_adma_slave_config(struct scsi_device *sdev)
550 {
551         struct ata_port *ap = ata_shost_to_port(sdev->host);
552         struct nv_adma_port_priv *pp = ap->private_data;
553         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
554         u64 bounce_limit;
555         unsigned long segment_boundary;
556         unsigned short sg_tablesize;
557         int rc;
558         int adma_enable;
559         u32 current_reg, new_reg, config_mask;
560
561         rc = ata_scsi_slave_config(sdev);
562
563         if (sdev->id >= ATA_MAX_DEVICES || sdev->channel || sdev->lun)
564                 /* Not a proper libata device, ignore */
565                 return rc;
566
567         if (ap->device[sdev->id].class == ATA_DEV_ATAPI) {
568                 /*
569                  * NVIDIA reports that ADMA mode does not support ATAPI commands.
570                  * Therefore ATAPI commands are sent through the legacy interface.
571                  * However, the legacy interface only supports 32-bit DMA.
572                  * Restrict DMA parameters as required by the legacy interface
573                  * when an ATAPI device is connected.
574                  */
575                 bounce_limit = ATA_DMA_MASK;
576                 segment_boundary = ATA_DMA_BOUNDARY;
577                 /* Subtract 1 since an extra entry may be needed for padding, see
578                    libata-scsi.c */
579                 sg_tablesize = LIBATA_MAX_PRD - 1;
580                 
581                 /* Since the legacy DMA engine is in use, we need to disable ADMA
582                    on the port. */
583                 adma_enable = 0;
584                 nv_adma_register_mode(ap);
585         }
586         else {
587                 bounce_limit = *ap->dev->dma_mask;
588                 segment_boundary = NV_ADMA_DMA_BOUNDARY;
589                 sg_tablesize = NV_ADMA_SGTBL_TOTAL_LEN;
590                 adma_enable = 1;
591         }
592         
593         pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &current_reg);
594
595         if(ap->port_no == 1)
596                 config_mask = NV_MCP_SATA_CFG_20_PORT1_EN |
597                               NV_MCP_SATA_CFG_20_PORT1_PWB_EN;
598         else
599                 config_mask = NV_MCP_SATA_CFG_20_PORT0_EN |
600                               NV_MCP_SATA_CFG_20_PORT0_PWB_EN;
601         
602         if(adma_enable) {
603                 new_reg = current_reg | config_mask;
604                 pp->flags &= ~NV_ADMA_ATAPI_SETUP_COMPLETE;
605         }
606         else {
607                 new_reg = current_reg & ~config_mask;
608                 pp->flags |= NV_ADMA_ATAPI_SETUP_COMPLETE;
609         }
610         
611         if(current_reg != new_reg)
612                 pci_write_config_dword(pdev, NV_MCP_SATA_CFG_20, new_reg);
613         
614         blk_queue_bounce_limit(sdev->request_queue, bounce_limit);
615         blk_queue_segment_boundary(sdev->request_queue, segment_boundary);
616         blk_queue_max_hw_segments(sdev->request_queue, sg_tablesize);
617         ata_port_printk(ap, KERN_INFO,
618                 "bounce limit 0x%llX, segment boundary 0x%lX, hw segs %hu\n",
619                 (unsigned long long)bounce_limit, segment_boundary, sg_tablesize);
620         return rc;
621 }
622
623 static int nv_adma_check_atapi_dma(struct ata_queued_cmd *qc)
624 {
625         struct nv_adma_port_priv *pp = qc->ap->private_data;
626         return !(pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE);
627 }
628
629 static unsigned int nv_adma_tf_to_cpb(struct ata_taskfile *tf, __le16 *cpb)
630 {
631         unsigned int idx = 0;
632
633         cpb[idx++] = cpu_to_le16((ATA_REG_DEVICE << 8) | tf->device | WNB);
634
635         if ((tf->flags & ATA_TFLAG_LBA48) == 0) {
636                 cpb[idx++] = cpu_to_le16(IGN);
637                 cpb[idx++] = cpu_to_le16(IGN);
638                 cpb[idx++] = cpu_to_le16(IGN);
639                 cpb[idx++] = cpu_to_le16(IGN);
640                 cpb[idx++] = cpu_to_le16(IGN);
641         }
642         else {
643                 cpb[idx++] = cpu_to_le16((ATA_REG_ERR   << 8) | tf->hob_feature);
644                 cpb[idx++] = cpu_to_le16((ATA_REG_NSECT << 8) | tf->hob_nsect);
645                 cpb[idx++] = cpu_to_le16((ATA_REG_LBAL  << 8) | tf->hob_lbal);
646                 cpb[idx++] = cpu_to_le16((ATA_REG_LBAM  << 8) | tf->hob_lbam);
647                 cpb[idx++] = cpu_to_le16((ATA_REG_LBAH  << 8) | tf->hob_lbah);
648         }
649         cpb[idx++] = cpu_to_le16((ATA_REG_ERR    << 8) | tf->feature);
650         cpb[idx++] = cpu_to_le16((ATA_REG_NSECT  << 8) | tf->nsect);
651         cpb[idx++] = cpu_to_le16((ATA_REG_LBAL   << 8) | tf->lbal);
652         cpb[idx++] = cpu_to_le16((ATA_REG_LBAM   << 8) | tf->lbam);
653         cpb[idx++] = cpu_to_le16((ATA_REG_LBAH   << 8) | tf->lbah);
654
655         cpb[idx++] = cpu_to_le16((ATA_REG_CMD    << 8) | tf->command | CMDEND);
656
657         return idx;
658 }
659
660 static void nv_adma_check_cpb(struct ata_port *ap, int cpb_num, int force_err)
661 {
662         struct nv_adma_port_priv *pp = ap->private_data;
663         int complete = 0, have_err = 0;
664         u8 flags = pp->cpb[cpb_num].resp_flags;
665
666         VPRINTK("CPB %d, flags=0x%x\n", cpb_num, flags);
667
668         if (flags & NV_CPB_RESP_DONE) {
669                 VPRINTK("CPB flags done, flags=0x%x\n", flags);
670                 complete = 1;
671         }
672         if (flags & NV_CPB_RESP_ATA_ERR) {
673                 ata_port_printk(ap, KERN_ERR, "CPB flags ATA err, flags=0x%x\n", flags);
674                 have_err = 1;
675                 complete = 1;
676         }
677         if (flags & NV_CPB_RESP_CMD_ERR) {
678                 ata_port_printk(ap, KERN_ERR, "CPB flags CMD err, flags=0x%x\n", flags);
679                 have_err = 1;
680                 complete = 1;
681         }
682         if (flags & NV_CPB_RESP_CPB_ERR) {
683                 ata_port_printk(ap, KERN_ERR, "CPB flags CPB err, flags=0x%x\n", flags);
684                 have_err = 1;
685                 complete = 1;
686         }
687         if(complete || force_err)
688         {
689                 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, cpb_num);
690                 if(likely(qc)) {
691                         u8 ata_status = 0;
692                         /* Only use the ATA port status for non-NCQ commands.
693                            For NCQ commands the current status may have nothing to do with
694                            the command just completed. */
695                         if(qc->tf.protocol != ATA_PROT_NCQ)
696                                 ata_status = readb(nv_adma_ctl_block(ap) + (ATA_REG_STATUS * 4));
697
698                         if(have_err || force_err)
699                                 ata_status |= ATA_ERR;
700
701                         qc->err_mask |= ac_err_mask(ata_status);
702                         DPRINTK("Completing qc from tag %d with err_mask %u\n",cpb_num,
703                                 qc->err_mask);
704                         ata_qc_complete(qc);
705                 }
706         }
707 }
708
709 static int nv_host_intr(struct ata_port *ap, u8 irq_stat)
710 {
711         struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
712         int handled;
713
714         /* freeze if hotplugged */
715         if (unlikely(irq_stat & (NV_INT_ADDED | NV_INT_REMOVED))) {
716                 ata_port_freeze(ap);
717                 return 1;
718         }
719
720         /* bail out if not our interrupt */
721         if (!(irq_stat & NV_INT_DEV))
722                 return 0;
723
724         /* DEV interrupt w/ no active qc? */
725         if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
726                 ata_check_status(ap);
727                 return 1;
728         }
729
730         /* handle interrupt */
731         handled = ata_host_intr(ap, qc);
732         if (unlikely(!handled)) {
733                 /* spurious, clear it */
734                 ata_check_status(ap);
735         }
736
737         return 1;
738 }
739
740 static irqreturn_t nv_adma_interrupt(int irq, void *dev_instance)
741 {
742         struct ata_host *host = dev_instance;
743         int i, handled = 0;
744         u32 notifier_clears[2];
745
746         spin_lock(&host->lock);
747
748         for (i = 0; i < host->n_ports; i++) {
749                 struct ata_port *ap = host->ports[i];
750                 notifier_clears[i] = 0;
751
752                 if (ap && !(ap->flags & ATA_FLAG_DISABLED)) {
753                         struct nv_adma_port_priv *pp = ap->private_data;
754                         void __iomem *mmio = nv_adma_ctl_block(ap);
755                         u16 status;
756                         u32 gen_ctl;
757                         int have_global_err = 0;
758                         u32 notifier, notifier_error;
759
760                         /* if in ATA register mode, use standard ata interrupt handler */
761                         if (pp->flags & NV_ADMA_PORT_REGISTER_MODE) {
762                                 u8 irq_stat = readb(host->mmio_base + NV_INT_STATUS_CK804)
763                                         >> (NV_INT_PORT_SHIFT * i);
764                                 handled += nv_host_intr(ap, irq_stat);
765                                 continue;
766                         }
767
768                         notifier = readl(mmio + NV_ADMA_NOTIFIER);
769                         notifier_error = readl(mmio + NV_ADMA_NOTIFIER_ERROR);
770                         notifier_clears[i] = notifier | notifier_error;
771
772                         gen_ctl = readl(nv_adma_gen_block(ap) + NV_ADMA_GEN_CTL);
773
774                         if( !NV_ADMA_CHECK_INTR(gen_ctl, ap->port_no) && !notifier &&
775                             !notifier_error)
776                                 /* Nothing to do */
777                                 continue;
778
779                         status = readw(mmio + NV_ADMA_STAT);
780
781                         /* Clear status. Ensure the controller sees the clearing before we start
782                            looking at any of the CPB statuses, so that any CPB completions after
783                            this point in the handler will raise another interrupt. */
784                         writew(status, mmio + NV_ADMA_STAT);
785                         readw(mmio + NV_ADMA_STAT); /* flush posted write */
786                         rmb();
787
788                         /* freeze if hotplugged */
789                         if (unlikely(status & (NV_ADMA_STAT_HOTPLUG | NV_ADMA_STAT_HOTUNPLUG))) {
790                                 ata_port_printk(ap, KERN_NOTICE, "Hotplug event, freezing\n");
791                                 ata_port_freeze(ap);
792                                 handled++;
793                                 continue;
794                         }
795
796                         if (status & NV_ADMA_STAT_TIMEOUT) {
797                                 ata_port_printk(ap, KERN_ERR, "timeout, stat=0x%x\n", status);
798                                 have_global_err = 1;
799                         }
800                         if (status & NV_ADMA_STAT_CPBERR) {
801                                 ata_port_printk(ap, KERN_ERR, "CPB error, stat=0x%x\n", status);
802                                 have_global_err = 1;
803                         }
804                         if ((status & NV_ADMA_STAT_DONE) || have_global_err) {
805                                 /** Check CPBs for completed commands */
806
807                                 if(ata_tag_valid(ap->active_tag))
808                                         /* Non-NCQ command */
809                                         nv_adma_check_cpb(ap, ap->active_tag, have_global_err ||
810                                                 (notifier_error & (1 << ap->active_tag)));
811                                 else {
812                                         int pos;
813                                         u32 active = ap->sactive;
814                                         while( (pos = ffs(active)) ) {
815                                                 pos--;
816                                                 nv_adma_check_cpb(ap, pos, have_global_err ||
817                                                         (notifier_error & (1 << pos)) );
818                                                 active &= ~(1 << pos );
819                                         }
820                                 }
821                         }
822
823                         handled++; /* irq handled if we got here */
824                 }
825         }
826         
827         if(notifier_clears[0] || notifier_clears[1]) {
828                 /* Note: Both notifier clear registers must be written
829                    if either is set, even if one is zero, according to NVIDIA. */
830                 writel(notifier_clears[0], 
831                         nv_adma_notifier_clear_block(host->ports[0]));
832                 writel(notifier_clears[1], 
833                         nv_adma_notifier_clear_block(host->ports[1]));
834         }
835
836         spin_unlock(&host->lock);
837
838         return IRQ_RETVAL(handled);
839 }
840
841 static void nv_adma_irq_clear(struct ata_port *ap)
842 {
843         void __iomem *mmio = nv_adma_ctl_block(ap);
844         u16 status = readw(mmio + NV_ADMA_STAT);
845         u32 notifier = readl(mmio + NV_ADMA_NOTIFIER);
846         u32 notifier_error = readl(mmio + NV_ADMA_NOTIFIER_ERROR);
847         unsigned long dma_stat_addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
848
849         /* clear ADMA status */
850         writew(status, mmio + NV_ADMA_STAT);
851         writel(notifier | notifier_error,
852                nv_adma_notifier_clear_block(ap));
853
854         /** clear legacy status */
855         outb(inb(dma_stat_addr), dma_stat_addr);
856 }
857
858 static void nv_adma_bmdma_setup(struct ata_queued_cmd *qc)
859 {
860         struct ata_port *ap = qc->ap;
861         unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
862         struct nv_adma_port_priv *pp = ap->private_data;
863         u8 dmactl;
864
865         if(!(pp->flags & NV_ADMA_PORT_REGISTER_MODE)) {
866                 WARN_ON(1);
867                 return;
868         }
869
870         /* load PRD table addr. */
871         outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
872
873         /* specify data direction, triple-check start bit is clear */
874         dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
875         dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
876         if (!rw)
877                 dmactl |= ATA_DMA_WR;
878
879         outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
880
881         /* issue r/w command */
882         ata_exec_command(ap, &qc->tf);
883 }
884
885 static void nv_adma_bmdma_start(struct ata_queued_cmd *qc)
886 {
887         struct ata_port *ap = qc->ap;
888         struct nv_adma_port_priv *pp = ap->private_data;
889         u8 dmactl;
890
891         if(!(pp->flags & NV_ADMA_PORT_REGISTER_MODE)) {
892                 WARN_ON(1);
893                 return;
894         }
895
896         /* start host DMA transaction */
897         dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
898         outb(dmactl | ATA_DMA_START,
899              ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
900 }
901
902 static void nv_adma_bmdma_stop(struct ata_queued_cmd *qc)
903 {
904         struct ata_port *ap = qc->ap;
905         struct nv_adma_port_priv *pp = ap->private_data;
906
907         if(!(pp->flags & NV_ADMA_PORT_REGISTER_MODE))
908                 return;
909
910         /* clear start/stop bit */
911         outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
912                 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
913
914         /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
915         ata_altstatus(ap);        /* dummy read */
916 }
917
918 static u8 nv_adma_bmdma_status(struct ata_port *ap)
919 {
920         struct nv_adma_port_priv *pp = ap->private_data;
921
922         WARN_ON(!(pp->flags & NV_ADMA_PORT_REGISTER_MODE));
923
924         return inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
925 }
926
927 static int nv_adma_port_start(struct ata_port *ap)
928 {
929         struct device *dev = ap->host->dev;
930         struct nv_adma_port_priv *pp;
931         int rc;
932         void *mem;
933         dma_addr_t mem_dma;
934         void __iomem *mmio = nv_adma_ctl_block(ap);
935         u16 tmp;
936
937         VPRINTK("ENTER\n");
938
939         rc = ata_port_start(ap);
940         if (rc)
941                 return rc;
942
943         pp = kzalloc(sizeof(*pp), GFP_KERNEL);
944         if (!pp) {
945                 rc = -ENOMEM;
946                 goto err_out;
947         }
948
949         mem = dma_alloc_coherent(dev, NV_ADMA_PORT_PRIV_DMA_SZ,
950                                  &mem_dma, GFP_KERNEL);
951
952         if (!mem) {
953                 rc = -ENOMEM;
954                 goto err_out_kfree;
955         }
956         memset(mem, 0, NV_ADMA_PORT_PRIV_DMA_SZ);
957
958         /*
959          * First item in chunk of DMA memory:
960          * 128-byte command parameter block (CPB)
961          * one for each command tag
962          */
963         pp->cpb     = mem;
964         pp->cpb_dma = mem_dma;
965
966         writel(mem_dma & 0xFFFFFFFF,    mmio + NV_ADMA_CPB_BASE_LOW);
967         writel((mem_dma >> 16 ) >> 16,  mmio + NV_ADMA_CPB_BASE_HIGH);
968
969         mem     += NV_ADMA_MAX_CPBS * NV_ADMA_CPB_SZ;
970         mem_dma += NV_ADMA_MAX_CPBS * NV_ADMA_CPB_SZ;
971
972         /*
973          * Second item: block of ADMA_SGTBL_LEN s/g entries
974          */
975         pp->aprd = mem;
976         pp->aprd_dma = mem_dma;
977
978         ap->private_data = pp;
979
980         /* clear any outstanding interrupt conditions */
981         writew(0xffff, mmio + NV_ADMA_STAT);
982
983         /* initialize port variables */
984         pp->flags = NV_ADMA_PORT_REGISTER_MODE;
985
986         /* clear CPB fetch count */
987         writew(0, mmio + NV_ADMA_CPB_COUNT);
988
989         /* clear GO for register mode */
990         tmp = readw(mmio + NV_ADMA_CTL);
991         writew(tmp & ~NV_ADMA_CTL_GO, mmio + NV_ADMA_CTL);
992
993         tmp = readw(mmio + NV_ADMA_CTL);
994         writew(tmp | NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
995         readl( mmio + NV_ADMA_CTL );    /* flush posted write */
996         udelay(1);
997         writew(tmp & ~NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
998         readl( mmio + NV_ADMA_CTL );    /* flush posted write */
999
1000         return 0;
1001
1002 err_out_kfree:
1003         kfree(pp);
1004 err_out:
1005         ata_port_stop(ap);
1006         return rc;
1007 }
1008
1009 static void nv_adma_port_stop(struct ata_port *ap)
1010 {
1011         struct device *dev = ap->host->dev;
1012         struct nv_adma_port_priv *pp = ap->private_data;
1013         void __iomem *mmio = nv_adma_ctl_block(ap);
1014
1015         VPRINTK("ENTER\n");
1016
1017         writew(0, mmio + NV_ADMA_CTL);
1018
1019         ap->private_data = NULL;
1020         dma_free_coherent(dev, NV_ADMA_PORT_PRIV_DMA_SZ, pp->cpb, pp->cpb_dma);
1021         kfree(pp);
1022         ata_port_stop(ap);
1023 }
1024
1025
1026 static void nv_adma_setup_port(struct ata_probe_ent *probe_ent, unsigned int port)
1027 {
1028         void __iomem *mmio = probe_ent->mmio_base;
1029         struct ata_ioports *ioport = &probe_ent->port[port];
1030
1031         VPRINTK("ENTER\n");
1032
1033         mmio += NV_ADMA_PORT + port * NV_ADMA_PORT_SIZE;
1034
1035         ioport->cmd_addr        = (unsigned long) mmio;
1036         ioport->data_addr       = (unsigned long) mmio + (ATA_REG_DATA * 4);
1037         ioport->error_addr      =
1038         ioport->feature_addr    = (unsigned long) mmio + (ATA_REG_ERR * 4);
1039         ioport->nsect_addr      = (unsigned long) mmio + (ATA_REG_NSECT * 4);
1040         ioport->lbal_addr       = (unsigned long) mmio + (ATA_REG_LBAL * 4);
1041         ioport->lbam_addr       = (unsigned long) mmio + (ATA_REG_LBAM * 4);
1042         ioport->lbah_addr       = (unsigned long) mmio + (ATA_REG_LBAH * 4);
1043         ioport->device_addr     = (unsigned long) mmio + (ATA_REG_DEVICE * 4);
1044         ioport->status_addr     =
1045         ioport->command_addr    = (unsigned long) mmio + (ATA_REG_STATUS * 4);
1046         ioport->altstatus_addr  =
1047         ioport->ctl_addr        = (unsigned long) mmio + 0x20;
1048 }
1049
1050 static int nv_adma_host_init(struct ata_probe_ent *probe_ent)
1051 {
1052         struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
1053         unsigned int i;
1054         u32 tmp32;
1055
1056         VPRINTK("ENTER\n");
1057
1058         /* enable ADMA on the ports */
1059         pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &tmp32);
1060         tmp32 |= NV_MCP_SATA_CFG_20_PORT0_EN |
1061                  NV_MCP_SATA_CFG_20_PORT0_PWB_EN |
1062                  NV_MCP_SATA_CFG_20_PORT1_EN |
1063                  NV_MCP_SATA_CFG_20_PORT1_PWB_EN;
1064
1065         pci_write_config_dword(pdev, NV_MCP_SATA_CFG_20, tmp32);
1066
1067         for (i = 0; i < probe_ent->n_ports; i++)
1068                 nv_adma_setup_port(probe_ent, i);
1069
1070         for (i = 0; i < probe_ent->n_ports; i++) {
1071                 void __iomem *mmio = __nv_adma_ctl_block(probe_ent->mmio_base, i);
1072                 u16 tmp;
1073
1074                 /* enable interrupt, clear reset if not already clear */
1075                 tmp = readw(mmio + NV_ADMA_CTL);
1076                 writew(tmp | NV_ADMA_CTL_AIEN, mmio + NV_ADMA_CTL);
1077         }
1078
1079         return 0;
1080 }
1081
1082 static void nv_adma_fill_aprd(struct ata_queued_cmd *qc,
1083                               struct scatterlist *sg,
1084                               int idx,
1085                               struct nv_adma_prd *aprd)
1086 {
1087         u8 flags;
1088
1089         memset(aprd, 0, sizeof(struct nv_adma_prd));
1090
1091         flags = 0;
1092         if (qc->tf.flags & ATA_TFLAG_WRITE)
1093                 flags |= NV_APRD_WRITE;
1094         if (idx == qc->n_elem - 1)
1095                 flags |= NV_APRD_END;
1096         else if (idx != 4)
1097                 flags |= NV_APRD_CONT;
1098
1099         aprd->addr  = cpu_to_le64(((u64)sg_dma_address(sg)));
1100         aprd->len   = cpu_to_le32(((u32)sg_dma_len(sg))); /* len in bytes */
1101         aprd->flags = flags;
1102 }
1103
1104 static void nv_adma_fill_sg(struct ata_queued_cmd *qc, struct nv_adma_cpb *cpb)
1105 {
1106         struct nv_adma_port_priv *pp = qc->ap->private_data;
1107         unsigned int idx;
1108         struct nv_adma_prd *aprd;
1109         struct scatterlist *sg;
1110
1111         VPRINTK("ENTER\n");
1112
1113         idx = 0;
1114
1115         ata_for_each_sg(sg, qc) {
1116                 aprd = (idx < 5) ? &cpb->aprd[idx] : &pp->aprd[NV_ADMA_SGTBL_LEN * qc->tag + (idx-5)];
1117                 nv_adma_fill_aprd(qc, sg, idx, aprd);
1118                 idx++;
1119         }
1120         if (idx > 5)
1121                 cpb->next_aprd = cpu_to_le64(((u64)(pp->aprd_dma + NV_ADMA_SGTBL_SZ * qc->tag)));
1122 }
1123
1124 static void nv_adma_qc_prep(struct ata_queued_cmd *qc)
1125 {
1126         struct nv_adma_port_priv *pp = qc->ap->private_data;
1127         struct nv_adma_cpb *cpb = &pp->cpb[qc->tag];
1128         u8 ctl_flags = NV_CPB_CTL_CPB_VALID |
1129                        NV_CPB_CTL_APRD_VALID |
1130                        NV_CPB_CTL_IEN;
1131
1132         VPRINTK("qc->flags = 0x%lx\n", qc->flags);
1133
1134         if (!(qc->flags & ATA_QCFLAG_DMAMAP) ||
1135              (pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)) {
1136                 nv_adma_register_mode(qc->ap);
1137                 ata_qc_prep(qc);
1138                 return;
1139         }
1140
1141         memset(cpb, 0, sizeof(struct nv_adma_cpb));
1142
1143         cpb->len                = 3;
1144         cpb->tag                = qc->tag;
1145         cpb->next_cpb_idx       = 0;
1146
1147         /* turn on NCQ flags for NCQ commands */
1148         if (qc->tf.protocol == ATA_PROT_NCQ)
1149                 ctl_flags |= NV_CPB_CTL_QUEUE | NV_CPB_CTL_FPDMA;
1150
1151         nv_adma_tf_to_cpb(&qc->tf, cpb->tf);
1152
1153         nv_adma_fill_sg(qc, cpb);
1154
1155         /* Be paranoid and don't let the device see NV_CPB_CTL_CPB_VALID until we are
1156            finished filling in all of the contents */
1157         wmb();
1158         cpb->ctl_flags = ctl_flags;
1159 }
1160
1161 static unsigned int nv_adma_qc_issue(struct ata_queued_cmd *qc)
1162 {
1163         struct nv_adma_port_priv *pp = qc->ap->private_data;
1164         void __iomem *mmio = nv_adma_ctl_block(qc->ap);
1165
1166         VPRINTK("ENTER\n");
1167
1168         if (!(qc->flags & ATA_QCFLAG_DMAMAP) ||
1169              (pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)) {
1170                 /* use ATA register mode */
1171                 VPRINTK("no dmamap or ATAPI, using ATA register mode: 0x%lx\n", qc->flags);
1172                 nv_adma_register_mode(qc->ap);
1173                 return ata_qc_issue_prot(qc);
1174         } else
1175                 nv_adma_mode(qc->ap);
1176
1177         /* write append register, command tag in lower 8 bits
1178            and (number of cpbs to append -1) in top 8 bits */
1179         wmb();
1180         writew(qc->tag, mmio + NV_ADMA_APPEND);
1181
1182         DPRINTK("Issued tag %u\n",qc->tag);
1183
1184         return 0;
1185 }
1186
1187 static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance)
1188 {
1189         struct ata_host *host = dev_instance;
1190         unsigned int i;
1191         unsigned int handled = 0;
1192         unsigned long flags;
1193
1194         spin_lock_irqsave(&host->lock, flags);
1195
1196         for (i = 0; i < host->n_ports; i++) {
1197                 struct ata_port *ap;
1198
1199                 ap = host->ports[i];
1200                 if (ap &&
1201                     !(ap->flags & ATA_FLAG_DISABLED)) {
1202                         struct ata_queued_cmd *qc;
1203
1204                         qc = ata_qc_from_tag(ap, ap->active_tag);
1205                         if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
1206                                 handled += ata_host_intr(ap, qc);
1207                         else
1208                                 // No request pending?  Clear interrupt status
1209                                 // anyway, in case there's one pending.
1210                                 ap->ops->check_status(ap);
1211                 }
1212
1213         }
1214
1215         spin_unlock_irqrestore(&host->lock, flags);
1216
1217         return IRQ_RETVAL(handled);
1218 }
1219
1220 static irqreturn_t nv_do_interrupt(struct ata_host *host, u8 irq_stat)
1221 {
1222         int i, handled = 0;
1223
1224         for (i = 0; i < host->n_ports; i++) {
1225                 struct ata_port *ap = host->ports[i];
1226
1227                 if (ap && !(ap->flags & ATA_FLAG_DISABLED))
1228                         handled += nv_host_intr(ap, irq_stat);
1229
1230                 irq_stat >>= NV_INT_PORT_SHIFT;
1231         }
1232
1233         return IRQ_RETVAL(handled);
1234 }
1235
1236 static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance)
1237 {
1238         struct ata_host *host = dev_instance;
1239         u8 irq_stat;
1240         irqreturn_t ret;
1241
1242         spin_lock(&host->lock);
1243         irq_stat = inb(host->ports[0]->ioaddr.scr_addr + NV_INT_STATUS);
1244         ret = nv_do_interrupt(host, irq_stat);
1245         spin_unlock(&host->lock);
1246
1247         return ret;
1248 }
1249
1250 static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance)
1251 {
1252         struct ata_host *host = dev_instance;
1253         u8 irq_stat;
1254         irqreturn_t ret;
1255
1256         spin_lock(&host->lock);
1257         irq_stat = readb(host->mmio_base + NV_INT_STATUS_CK804);
1258         ret = nv_do_interrupt(host, irq_stat);
1259         spin_unlock(&host->lock);
1260
1261         return ret;
1262 }
1263
1264 static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg)
1265 {
1266         if (sc_reg > SCR_CONTROL)
1267                 return 0xffffffffU;
1268
1269         return ioread32((void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
1270 }
1271
1272 static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
1273 {
1274         if (sc_reg > SCR_CONTROL)
1275                 return;
1276
1277         iowrite32(val, (void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
1278 }
1279
1280 static void nv_nf2_freeze(struct ata_port *ap)
1281 {
1282         unsigned long scr_addr = ap->host->ports[0]->ioaddr.scr_addr;
1283         int shift = ap->port_no * NV_INT_PORT_SHIFT;
1284         u8 mask;
1285
1286         mask = inb(scr_addr + NV_INT_ENABLE);
1287         mask &= ~(NV_INT_ALL << shift);
1288         outb(mask, scr_addr + NV_INT_ENABLE);
1289 }
1290
1291 static void nv_nf2_thaw(struct ata_port *ap)
1292 {
1293         unsigned long scr_addr = ap->host->ports[0]->ioaddr.scr_addr;
1294         int shift = ap->port_no * NV_INT_PORT_SHIFT;
1295         u8 mask;
1296
1297         outb(NV_INT_ALL << shift, scr_addr + NV_INT_STATUS);
1298
1299         mask = inb(scr_addr + NV_INT_ENABLE);
1300         mask |= (NV_INT_MASK << shift);
1301         outb(mask, scr_addr + NV_INT_ENABLE);
1302 }
1303
1304 static void nv_ck804_freeze(struct ata_port *ap)
1305 {
1306         void __iomem *mmio_base = ap->host->mmio_base;
1307         int shift = ap->port_no * NV_INT_PORT_SHIFT;
1308         u8 mask;
1309
1310         mask = readb(mmio_base + NV_INT_ENABLE_CK804);
1311         mask &= ~(NV_INT_ALL << shift);
1312         writeb(mask, mmio_base + NV_INT_ENABLE_CK804);
1313 }
1314
1315 static void nv_ck804_thaw(struct ata_port *ap)
1316 {
1317         void __iomem *mmio_base = ap->host->mmio_base;
1318         int shift = ap->port_no * NV_INT_PORT_SHIFT;
1319         u8 mask;
1320
1321         writeb(NV_INT_ALL << shift, mmio_base + NV_INT_STATUS_CK804);
1322
1323         mask = readb(mmio_base + NV_INT_ENABLE_CK804);
1324         mask |= (NV_INT_MASK << shift);
1325         writeb(mask, mmio_base + NV_INT_ENABLE_CK804);
1326 }
1327
1328 static int nv_hardreset(struct ata_port *ap, unsigned int *class)
1329 {
1330         unsigned int dummy;
1331
1332         /* SATA hardreset fails to retrieve proper device signature on
1333          * some controllers.  Don't classify on hardreset.  For more
1334          * info, see http://bugme.osdl.org/show_bug.cgi?id=3352
1335          */
1336         return sata_std_hardreset(ap, &dummy);
1337 }
1338
1339 static void nv_error_handler(struct ata_port *ap)
1340 {
1341         ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset,
1342                            nv_hardreset, ata_std_postreset);
1343 }
1344
1345 static void nv_adma_error_handler(struct ata_port *ap)
1346 {
1347         struct nv_adma_port_priv *pp = ap->private_data;
1348         if(!(pp->flags & NV_ADMA_PORT_REGISTER_MODE)) {
1349                 void __iomem *mmio = nv_adma_ctl_block(ap);
1350                 int i;
1351                 u16 tmp;
1352
1353                 u32 notifier = readl(mmio + NV_ADMA_NOTIFIER);
1354                 u32 notifier_error = readl(mmio + NV_ADMA_NOTIFIER_ERROR);
1355                 u32 gen_ctl = readl(nv_adma_gen_block(ap) + NV_ADMA_GEN_CTL);
1356                 u32 status = readw(mmio + NV_ADMA_STAT);
1357
1358                 ata_port_printk(ap, KERN_ERR, "EH in ADMA mode, notifier 0x%X "
1359                         "notifier_error 0x%X gen_ctl 0x%X status 0x%X\n",
1360                         notifier, notifier_error, gen_ctl, status);
1361
1362                 for( i=0;i<NV_ADMA_MAX_CPBS;i++) {
1363                         struct nv_adma_cpb *cpb = &pp->cpb[i];
1364                         if( cpb->ctl_flags || cpb->resp_flags )
1365                                 ata_port_printk(ap, KERN_ERR,
1366                                         "CPB %d: ctl_flags 0x%x, resp_flags 0x%x\n",
1367                                         i, cpb->ctl_flags, cpb->resp_flags);
1368                 }
1369
1370                 /* Push us back into port register mode for error handling. */
1371                 nv_adma_register_mode(ap);
1372
1373                 ata_port_printk(ap, KERN_ERR, "Resetting port\n");
1374
1375                 /* Mark all of the CPBs as invalid to prevent them from being executed */
1376                 for( i=0;i<NV_ADMA_MAX_CPBS;i++)
1377                         pp->cpb[i].ctl_flags &= ~NV_CPB_CTL_CPB_VALID;
1378
1379                 /* clear CPB fetch count */
1380                 writew(0, mmio + NV_ADMA_CPB_COUNT);
1381
1382                 /* Reset channel */
1383                 tmp = readw(mmio + NV_ADMA_CTL);
1384                 writew(tmp | NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
1385                 readl( mmio + NV_ADMA_CTL );    /* flush posted write */
1386                 udelay(1);
1387                 writew(tmp & ~NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
1388                 readl( mmio + NV_ADMA_CTL );    /* flush posted write */
1389         }
1390
1391         ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset,
1392                            nv_hardreset, ata_std_postreset);
1393 }
1394
1395 static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1396 {
1397         static int printed_version = 0;
1398         struct ata_port_info *ppi[2];
1399         struct ata_probe_ent *probe_ent;
1400         int pci_dev_busy = 0;
1401         int rc;
1402         u32 bar;
1403         unsigned long base;
1404         unsigned long type = ent->driver_data;
1405         int mask_set = 0;
1406
1407         // Make sure this is a SATA controller by counting the number of bars
1408         // (NVIDIA SATA controllers will always have six bars).  Otherwise,
1409         // it's an IDE controller and we ignore it.
1410         for (bar=0; bar<6; bar++)
1411                 if (pci_resource_start(pdev, bar) == 0)
1412                         return -ENODEV;
1413
1414         if (    !printed_version++)
1415                 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
1416
1417         rc = pci_enable_device(pdev);
1418         if (rc)
1419                 goto err_out;
1420
1421         rc = pci_request_regions(pdev, DRV_NAME);
1422         if (rc) {
1423                 pci_dev_busy = 1;
1424                 goto err_out_disable;
1425         }
1426
1427         if(type >= CK804 && adma_enabled) {
1428                 dev_printk(KERN_NOTICE, &pdev->dev, "Using ADMA mode\n");
1429                 type = ADMA;
1430                 if(!pci_set_dma_mask(pdev, DMA_64BIT_MASK) &&
1431                    !pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
1432                         mask_set = 1;
1433         }
1434
1435         if(!mask_set) {
1436                 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
1437                 if (rc)
1438                         goto err_out_regions;
1439                 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
1440                 if (rc)
1441                         goto err_out_regions;
1442         }
1443
1444         rc = -ENOMEM;
1445
1446         ppi[0] = ppi[1] = &nv_port_info[type];
1447         probe_ent = ata_pci_init_native_mode(pdev, ppi, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
1448         if (!probe_ent)
1449                 goto err_out_regions;
1450
1451         probe_ent->mmio_base = pci_iomap(pdev, 5, 0);
1452         if (!probe_ent->mmio_base) {
1453                 rc = -EIO;
1454                 goto err_out_free_ent;
1455         }
1456
1457         base = (unsigned long)probe_ent->mmio_base;
1458
1459         probe_ent->port[0].scr_addr = base + NV_PORT0_SCR_REG_OFFSET;
1460         probe_ent->port[1].scr_addr = base + NV_PORT1_SCR_REG_OFFSET;
1461
1462         /* enable SATA space for CK804 */
1463         if (type >= CK804) {
1464                 u8 regval;
1465
1466                 pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
1467                 regval |= NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
1468                 pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
1469         }
1470
1471         pci_set_master(pdev);
1472
1473         if (type == ADMA) {
1474                 rc = nv_adma_host_init(probe_ent);
1475                 if (rc)
1476                         goto err_out_iounmap;
1477         }
1478
1479         rc = ata_device_add(probe_ent);
1480         if (rc != NV_PORTS)
1481                 goto err_out_iounmap;
1482
1483         kfree(probe_ent);
1484
1485         return 0;
1486
1487 err_out_iounmap:
1488         pci_iounmap(pdev, probe_ent->mmio_base);
1489 err_out_free_ent:
1490         kfree(probe_ent);
1491 err_out_regions:
1492         pci_release_regions(pdev);
1493 err_out_disable:
1494         if (!pci_dev_busy)
1495                 pci_disable_device(pdev);
1496 err_out:
1497         return rc;
1498 }
1499
1500 static void nv_ck804_host_stop(struct ata_host *host)
1501 {
1502         struct pci_dev *pdev = to_pci_dev(host->dev);
1503         u8 regval;
1504
1505         /* disable SATA space for CK804 */
1506         pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
1507         regval &= ~NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
1508         pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
1509
1510         ata_pci_host_stop(host);
1511 }
1512
1513 static void nv_adma_host_stop(struct ata_host *host)
1514 {
1515         struct pci_dev *pdev = to_pci_dev(host->dev);
1516         int i;
1517         u32 tmp32;
1518
1519         for (i = 0; i < host->n_ports; i++) {
1520                 void __iomem *mmio = __nv_adma_ctl_block(host->mmio_base, i);
1521                 u16 tmp;
1522
1523                 /* disable interrupt */
1524                 tmp = readw(mmio + NV_ADMA_CTL);
1525                 writew(tmp & ~NV_ADMA_CTL_AIEN, mmio + NV_ADMA_CTL);
1526         }
1527
1528         /* disable ADMA on the ports */
1529         pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &tmp32);
1530         tmp32 &= ~(NV_MCP_SATA_CFG_20_PORT0_EN |
1531                    NV_MCP_SATA_CFG_20_PORT0_PWB_EN |
1532                    NV_MCP_SATA_CFG_20_PORT1_EN |
1533                    NV_MCP_SATA_CFG_20_PORT1_PWB_EN);
1534
1535         pci_write_config_dword(pdev, NV_MCP_SATA_CFG_20, tmp32);
1536
1537         nv_ck804_host_stop(host);
1538 }
1539
1540 static int __init nv_init(void)
1541 {
1542         return pci_register_driver(&nv_pci_driver);
1543 }
1544
1545 static void __exit nv_exit(void)
1546 {
1547         pci_unregister_driver(&nv_pci_driver);
1548 }
1549
1550 module_init(nv_init);
1551 module_exit(nv_exit);
1552 module_param_named(adma, adma_enabled, bool, 0444);
1553 MODULE_PARM_DESC(adma, "Enable use of ADMA (Default: true)");