VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / scsi / sata_sil.c
1 /*
2  *  sata_sil.c - Silicon Image SATA
3  *
4  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5  *                  Please ALWAYS copy linux-ide@vger.kernel.org
6  *                  on emails.
7  *
8  *  Copyright 2003 Red Hat, Inc.
9  *  Copyright 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org>
10  *
11  *  The contents of this file are subject to the Open
12  *  Software License version 1.1 that can be found at
13  *  http://www.opensource.org/licenses/osl-1.1.txt and is included herein
14  *  by reference.
15  *
16  *  Alternatively, the contents of this file may be used under the terms
17  *  of the GNU General Public License version 2 (the "GPL") as distributed
18  *  in the kernel source COPYING file, in which case the provisions of
19  *  the GPL are applicable instead of the above.  If you wish to allow
20  *  the use of your version of this file only under the terms of the
21  *  GPL and not to allow others to use your version of this file under
22  *  the OSL, indicate your decision by deleting the provisions above and
23  *  replace them with the notice and other provisions required by the GPL.
24  *  If you do not delete the provisions above, a recipient may use your
25  *  version of this file under either the OSL or the GPL.
26  *
27  */
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/init.h>
33 #include <linux/blkdev.h>
34 #include <linux/delay.h>
35 #include <linux/interrupt.h>
36 #include "scsi.h"
37 #include <scsi/scsi_host.h>
38 #include <linux/libata.h>
39
40 #define DRV_NAME        "sata_sil"
41 #define DRV_VERSION     "0.54"
42
43 enum {
44         sil_3112                = 0,
45         sil_3114                = 1,
46
47         SIL_SYSCFG              = 0x48,
48         SIL_MASK_IDE0_INT       = (1 << 22),
49         SIL_MASK_IDE1_INT       = (1 << 23),
50         SIL_MASK_IDE2_INT       = (1 << 24),
51         SIL_MASK_IDE3_INT       = (1 << 25),
52         SIL_MASK_2PORT          = SIL_MASK_IDE0_INT | SIL_MASK_IDE1_INT,
53         SIL_MASK_4PORT          = SIL_MASK_2PORT |
54                                   SIL_MASK_IDE2_INT | SIL_MASK_IDE3_INT,
55
56         SIL_IDE2_BMDMA          = 0x200,
57
58         SIL_INTR_STEERING       = (1 << 1),
59         SIL_QUIRK_MOD15WRITE    = (1 << 0),
60         SIL_QUIRK_UDMA5MAX      = (1 << 1),
61 };
62
63 static int sil_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
64 static void sil_dev_config(struct ata_port *ap, struct ata_device *dev);
65 static u32 sil_scr_read (struct ata_port *ap, unsigned int sc_reg);
66 static void sil_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
67 static void sil_post_set_mode (struct ata_port *ap);
68
69 static struct pci_device_id sil_pci_tbl[] = {
70         { 0x1095, 0x3112, PCI_ANY_ID, PCI_ANY_ID, 0, 0, sil_3112 },
71         { 0x1095, 0x0240, PCI_ANY_ID, PCI_ANY_ID, 0, 0, sil_3112 },
72         { 0x1095, 0x3512, PCI_ANY_ID, PCI_ANY_ID, 0, 0, sil_3112 },
73         { 0x1095, 0x3114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, sil_3114 },
74         { }     /* terminate list */
75 };
76
77
78 /* TODO firmware versions should be added - eric */
79 struct sil_drivelist {
80         const char * product;
81         unsigned int quirk;
82 } sil_blacklist [] = {
83         { "ST320012AS",         SIL_QUIRK_MOD15WRITE },
84         { "ST330013AS",         SIL_QUIRK_MOD15WRITE },
85         { "ST340017AS",         SIL_QUIRK_MOD15WRITE },
86         { "ST360015AS",         SIL_QUIRK_MOD15WRITE },
87         { "ST380023AS",         SIL_QUIRK_MOD15WRITE },
88         { "ST3120023AS",        SIL_QUIRK_MOD15WRITE },
89         { "ST3160023AS",        SIL_QUIRK_MOD15WRITE },
90         { "ST340014ASL",        SIL_QUIRK_MOD15WRITE },
91         { "ST360014ASL",        SIL_QUIRK_MOD15WRITE },
92         { "ST380011ASL",        SIL_QUIRK_MOD15WRITE },
93         { "ST3120022ASL",       SIL_QUIRK_MOD15WRITE },
94         { "ST3160021ASL",       SIL_QUIRK_MOD15WRITE },
95         { "Maxtor 4D060H3",     SIL_QUIRK_UDMA5MAX },
96         { }
97 };
98
99 static struct pci_driver sil_pci_driver = {
100         .name                   = DRV_NAME,
101         .id_table               = sil_pci_tbl,
102         .probe                  = sil_init_one,
103         .remove                 = ata_pci_remove_one,
104 };
105
106 static Scsi_Host_Template sil_sht = {
107         .module                 = THIS_MODULE,
108         .name                   = DRV_NAME,
109         .queuecommand           = ata_scsi_queuecmd,
110         .eh_strategy_handler    = ata_scsi_error,
111         .can_queue              = ATA_DEF_QUEUE,
112         .this_id                = ATA_SHT_THIS_ID,
113         .sg_tablesize           = LIBATA_MAX_PRD,
114         .max_sectors            = ATA_MAX_SECTORS,
115         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
116         .emulated               = ATA_SHT_EMULATED,
117         .use_clustering         = ATA_SHT_USE_CLUSTERING,
118         .proc_name              = DRV_NAME,
119         .dma_boundary           = ATA_DMA_BOUNDARY,
120         .slave_configure        = ata_scsi_slave_config,
121         .bios_param             = ata_std_bios_param,
122 };
123
124 static struct ata_port_operations sil_ops = {
125         .port_disable           = ata_port_disable,
126         .dev_config             = sil_dev_config,
127         .tf_load                = ata_tf_load_mmio,
128         .tf_read                = ata_tf_read_mmio,
129         .check_status           = ata_check_status_mmio,
130         .exec_command           = ata_exec_command_mmio,
131         .phy_reset              = sata_phy_reset,
132         .post_set_mode          = sil_post_set_mode,
133         .bmdma_setup            = ata_bmdma_setup_mmio,
134         .bmdma_start            = ata_bmdma_start_mmio,
135         .qc_prep                = ata_qc_prep,
136         .qc_issue               = ata_qc_issue_prot,
137         .eng_timeout            = ata_eng_timeout,
138         .irq_handler            = ata_interrupt,
139         .irq_clear              = ata_bmdma_irq_clear,
140         .scr_read               = sil_scr_read,
141         .scr_write              = sil_scr_write,
142         .port_start             = ata_port_start,
143         .port_stop              = ata_port_stop,
144 };
145
146 static struct ata_port_info sil_port_info[] = {
147         /* sil_3112 */
148         {
149                 .sht            = &sil_sht,
150                 .host_flags     = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
151                                   ATA_FLAG_SRST | ATA_FLAG_MMIO,
152                 .pio_mask       = 0x03,                 /* pio3-4 */
153                 .udma_mask      = 0x3f,                 /* udma0-5 */
154                 .port_ops       = &sil_ops,
155         }, /* sil_3114 */
156         {
157                 .sht            = &sil_sht,
158                 .host_flags     = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
159                                   ATA_FLAG_SRST | ATA_FLAG_MMIO,
160                 .pio_mask       = 0x03,                 /* pio3-4 */
161                 .udma_mask      = 0x3f,                 /* udma0-5 */
162                 .port_ops       = &sil_ops,
163         },
164 };
165
166 /* per-port register offsets */
167 /* TODO: we can probably calculate rather than use a table */
168 static const struct {
169         unsigned long tf;       /* ATA taskfile register block */
170         unsigned long ctl;      /* ATA control/altstatus register block */
171         unsigned long bmdma;    /* DMA register block */
172         unsigned long scr;      /* SATA control register block */
173         unsigned long sien;     /* SATA Interrupt Enable register */
174         unsigned long xfer_mode;/* data transfer mode register */
175 } sil_port[] = {
176         /* port 0 ... */
177         { 0x80, 0x8A, 0x00, 0x100, 0x148, 0xb4 },
178         { 0xC0, 0xCA, 0x08, 0x180, 0x1c8, 0xf4 },
179         { 0x280, 0x28A, 0x200, 0x300, 0x348, 0x2b4 },
180         { 0x2C0, 0x2CA, 0x208, 0x380, 0x3c8, 0x2f4 },
181         /* ... port 3 */
182 };
183
184 MODULE_AUTHOR("Jeff Garzik");
185 MODULE_DESCRIPTION("low-level driver for Silicon Image SATA controller");
186 MODULE_LICENSE("GPL");
187 MODULE_DEVICE_TABLE(pci, sil_pci_tbl);
188
189 static void sil_post_set_mode (struct ata_port *ap)
190 {
191         struct ata_host_set *host_set = ap->host_set;
192         struct ata_device *dev;
193         void *addr = host_set->mmio_base + sil_port[ap->port_no].xfer_mode;
194         u32 tmp, dev_mode[2];
195         unsigned int i;
196
197         for (i = 0; i < 2; i++) {
198                 dev = &ap->device[i];
199                 if (!ata_dev_present(dev))
200                         dev_mode[i] = 0;        /* PIO0/1/2 */
201                 else if (dev->flags & ATA_DFLAG_PIO)
202                         dev_mode[i] = 1;        /* PIO3/4 */
203                 else
204                         dev_mode[i] = 3;        /* UDMA */
205                 /* value 2 indicates MDMA */
206         }
207
208         tmp = readl(addr);
209         tmp &= ~((1<<5) | (1<<4) | (1<<1) | (1<<0));
210         tmp |= dev_mode[0];
211         tmp |= (dev_mode[1] << 4);
212         writel(tmp, addr);
213         readl(addr);    /* flush */
214 }
215
216 static inline unsigned long sil_scr_addr(struct ata_port *ap, unsigned int sc_reg)
217 {
218         unsigned long offset = ap->ioaddr.scr_addr;
219
220         switch (sc_reg) {
221         case SCR_STATUS:
222                 return offset + 4;
223         case SCR_ERROR:
224                 return offset + 8;
225         case SCR_CONTROL:
226                 return offset;
227         default:
228                 /* do nothing */
229                 break;
230         }
231
232         return 0;
233 }
234
235 static u32 sil_scr_read (struct ata_port *ap, unsigned int sc_reg)
236 {
237         void *mmio = (void *) sil_scr_addr(ap, sc_reg);
238         if (mmio)
239                 return readl(mmio);
240         return 0xffffffffU;
241 }
242
243 static void sil_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
244 {
245         void *mmio = (void *) sil_scr_addr(ap, sc_reg);
246         if (mmio)
247                 writel(val, mmio);
248 }
249
250 /**
251  *      sil_dev_config - Apply device/host-specific errata fixups
252  *      @ap: Port containing device to be examined
253  *      @dev: Device to be examined
254  *
255  *      After the IDENTIFY [PACKET] DEVICE step is complete, and a
256  *      device is known to be present, this function is called.
257  *      We apply two errata fixups which are specific to Silicon Image,
258  *      a Seagate and a Maxtor fixup.
259  *
260  *      For certain Seagate devices, we must limit the maximum sectors
261  *      to under 8K.
262  *
263  *      For certain Maxtor devices, we must not program the drive
264  *      beyond udma5.
265  *
266  *      Both fixups are unfairly pessimistic.  As soon as I get more
267  *      information on these errata, I will create a more exhaustive
268  *      list, and apply the fixups to only the specific
269  *      devices/hosts/firmwares that need it.
270  *
271  *      20040111 - Seagate drives affected by the Mod15Write bug are blacklisted
272  *      The Maxtor quirk is in the blacklist, but I'm keeping the original
273  *      pessimistic fix for the following reasons...
274  *      - There seems to be less info on it, only one device gleaned off the
275  *      Windows driver, maybe only one is affected.  More info would be greatly
276  *      appreciated.
277  *      - But then again UDMA5 is hardly anything to complain about
278  */
279 static void sil_dev_config(struct ata_port *ap, struct ata_device *dev)
280 {
281         unsigned int n, quirks = 0;
282         unsigned char model_num[40];
283         const char *s;
284         unsigned int len;
285
286         ata_dev_id_string(dev, model_num, ATA_ID_PROD_OFS,
287                           sizeof(model_num));
288         s = &model_num[0];
289         len = strnlen(s, sizeof(model_num));
290
291         /* ATAPI specifies that empty space is blank-filled; remove blanks */
292         while ((len > 0) && (s[len - 1] == ' '))
293                 len--;
294
295         for (n = 0; sil_blacklist[n].product; n++) 
296                 if (!memcmp(sil_blacklist[n].product, s,
297                             strlen(sil_blacklist[n].product))) {
298                         quirks = sil_blacklist[n].quirk;
299                         break;
300                 }
301         
302         /* limit requests to 15 sectors */
303         if (quirks & SIL_QUIRK_MOD15WRITE) {
304                 printk(KERN_INFO "ata%u(%u): applying Seagate errata fix\n",
305                        ap->id, dev->devno);
306                 ap->host->max_sectors = 15;
307                 ap->host->hostt->max_sectors = 15;
308                 dev->flags |= ATA_DFLAG_LOCK_SECTORS;
309                 return;
310         }
311
312         /* limit to udma5 */
313         if (quirks & SIL_QUIRK_UDMA5MAX) {
314                 printk(KERN_INFO "ata%u(%u): applying Maxtor errata fix %s\n",
315                        ap->id, dev->devno, s);
316                 ap->udma_mask &= ATA_UDMA5;
317                 return;
318         }
319 }
320
321 static int sil_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
322 {
323         static int printed_version;
324         struct ata_probe_ent *probe_ent = NULL;
325         unsigned long base;
326         void *mmio_base;
327         int rc;
328         unsigned int i;
329         u32 tmp, irq_mask;
330
331         if (!printed_version++)
332                 printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
333
334         /*
335          * If this driver happens to only be useful on Apple's K2, then
336          * we should check that here as it has a normal Serverworks ID
337          */
338         rc = pci_enable_device(pdev);
339         if (rc)
340                 return rc;
341
342         rc = pci_request_regions(pdev, DRV_NAME);
343         if (rc)
344                 goto err_out;
345
346         rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
347         if (rc)
348                 goto err_out_regions;
349         rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
350         if (rc)
351                 goto err_out_regions;
352
353         probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
354         if (probe_ent == NULL) {
355                 rc = -ENOMEM;
356                 goto err_out_regions;
357         }
358
359         memset(probe_ent, 0, sizeof(*probe_ent));
360         INIT_LIST_HEAD(&probe_ent->node);
361         probe_ent->pdev = pdev;
362         probe_ent->port_ops = sil_port_info[ent->driver_data].port_ops;
363         probe_ent->sht = sil_port_info[ent->driver_data].sht;
364         probe_ent->n_ports = (ent->driver_data == sil_3114) ? 4 : 2;
365         probe_ent->pio_mask = sil_port_info[ent->driver_data].pio_mask;
366         probe_ent->udma_mask = sil_port_info[ent->driver_data].udma_mask;
367         probe_ent->irq = pdev->irq;
368         probe_ent->irq_flags = SA_SHIRQ;
369         probe_ent->host_flags = sil_port_info[ent->driver_data].host_flags;
370
371         mmio_base = ioremap(pci_resource_start(pdev, 5),
372                             pci_resource_len(pdev, 5));
373         if (mmio_base == NULL) {
374                 rc = -ENOMEM;
375                 goto err_out_free_ent;
376         }
377
378         probe_ent->mmio_base = mmio_base;
379
380         base = (unsigned long) mmio_base;
381
382         for (i = 0; i < probe_ent->n_ports; i++) {
383                 probe_ent->port[i].cmd_addr = base + sil_port[i].tf;
384                 probe_ent->port[i].altstatus_addr =
385                 probe_ent->port[i].ctl_addr = base + sil_port[i].ctl;
386                 probe_ent->port[i].bmdma_addr = base + sil_port[i].bmdma;
387                 probe_ent->port[i].scr_addr = base + sil_port[i].scr;
388                 ata_std_ports(&probe_ent->port[i]);
389         }
390
391         if (ent->driver_data == sil_3114) {
392                 irq_mask = SIL_MASK_4PORT;
393
394                 /* flip the magic "make 4 ports work" bit */
395                 tmp = readl(mmio_base + SIL_IDE2_BMDMA);
396                 if ((tmp & SIL_INTR_STEERING) == 0)
397                         writel(tmp | SIL_INTR_STEERING,
398                                mmio_base + SIL_IDE2_BMDMA);
399
400         } else {
401                 irq_mask = SIL_MASK_2PORT;
402         }
403
404         /* make sure IDE0/1/2/3 interrupts are not masked */
405         tmp = readl(mmio_base + SIL_SYSCFG);
406         if (tmp & irq_mask) {
407                 tmp &= ~irq_mask;
408                 writel(tmp, mmio_base + SIL_SYSCFG);
409                 readl(mmio_base + SIL_SYSCFG);  /* flush */
410         }
411
412         /* mask all SATA phy-related interrupts */
413         /* TODO: unmask bit 6 (SError N bit) for hotplug */
414         for (i = 0; i < probe_ent->n_ports; i++)
415                 writel(0, mmio_base + sil_port[i].sien);
416
417         pci_set_master(pdev);
418
419         /* FIXME: check ata_device_add return value */
420         ata_device_add(probe_ent);
421         kfree(probe_ent);
422
423         return 0;
424
425 err_out_free_ent:
426         kfree(probe_ent);
427 err_out_regions:
428         pci_release_regions(pdev);
429 err_out:
430         pci_disable_device(pdev);
431         return rc;
432 }
433
434 static int __init sil_init(void)
435 {
436         return pci_module_init(&sil_pci_driver);
437 }
438
439 static void __exit sil_exit(void)
440 {
441         pci_unregister_driver(&sil_pci_driver);
442 }
443
444
445 module_init(sil_init);
446 module_exit(sil_exit);