Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * pata_it821x.c    - IT821x PATA for new ATA layer
0003  *            (C) 2005 Red Hat Inc
0004  *            Alan Cox <alan@lxorguk.ukuu.org.uk>
0005  *            (C) 2007 Bartlomiej Zolnierkiewicz
0006  *
0007  * based upon
0008  *
0009  * it821x.c
0010  *
0011  * linux/drivers/ide/pci/it821x.c       Version 0.09    December 2004
0012  *
0013  * Copyright (C) 2004       Red Hat
0014  *
0015  *  May be copied or modified under the terms of the GNU General Public License
0016  *  Based in part on the ITE vendor provided SCSI driver.
0017  *
0018  *  Documentation available from IT8212F_V04.pdf
0019  *  http://www.ite.com.tw/EN/products_more.aspx?CategoryID=3&ID=5,91
0020  *  Some other documents are NDA.
0021  *
0022  *  The ITE8212 isn't exactly a standard IDE controller. It has two
0023  *  modes. In pass through mode then it is an IDE controller. In its smart
0024  *  mode its actually quite a capable hardware raid controller disguised
0025  *  as an IDE controller. Smart mode only understands DMA read/write and
0026  *  identify, none of the fancier commands apply. The IT8211 is identical
0027  *  in other respects but lacks the raid mode.
0028  *
0029  *  Errata:
0030  *  o   Rev 0x10 also requires master/slave hold the same DMA timings and
0031  *  cannot do ATAPI MWDMA.
0032  *  o   The identify data for raid volumes lacks CHS info (technically ok)
0033  *  but also fails to set the LBA28 and other bits. We fix these in
0034  *  the IDE probe quirk code.
0035  *  o   If you write LBA48 sized I/O's (ie > 256 sector) in smart mode
0036  *  raid then the controller firmware dies
0037  *  o   Smart mode without RAID doesn't clear all the necessary identify
0038  *  bits to reduce the command set to the one used
0039  *
0040  *  This has a few impacts on the driver
0041  *  - In pass through mode we do all the work you would expect
0042  *  - In smart mode the clocking set up is done by the controller generally
0043  *    but we must watch the other limits and filter.
0044  *  - There are a few extra vendor commands that actually talk to the
0045  *    controller but only work PIO with no IRQ.
0046  *
0047  *  Vendor areas of the identify block in smart mode are used for the
0048  *  timing and policy set up. Each HDD in raid mode also has a serial
0049  *  block on the disk. The hardware extra commands are get/set chip status,
0050  *  rebuild, get rebuild status.
0051  *
0052  *  In Linux the driver supports pass through mode as if the device was
0053  *  just another IDE controller. If the smart mode is running then
0054  *  volumes are managed by the controller firmware and each IDE "disk"
0055  *  is a raid volume. Even more cute - the controller can do automated
0056  *  hotplug and rebuild.
0057  *
0058  *  The pass through controller itself is a little demented. It has a
0059  *  flaw that it has a single set of PIO/MWDMA timings per channel so
0060  *  non UDMA devices restrict each others performance. It also has a
0061  *  single clock source per channel so mixed UDMA100/133 performance
0062  *  isn't perfect and we have to pick a clock. Thankfully none of this
0063  *  matters in smart mode. ATAPI DMA is not currently supported.
0064  *
0065  *  It seems the smart mode is a win for RAID1/RAID10 but otherwise not.
0066  *
0067  *  TODO
0068  *  -   ATAPI and other speed filtering
0069  *  -   RAID configuration ioctls
0070  */
0071 
0072 #include <linux/kernel.h>
0073 #include <linux/module.h>
0074 #include <linux/pci.h>
0075 #include <linux/blkdev.h>
0076 #include <linux/delay.h>
0077 #include <linux/slab.h>
0078 #include <scsi/scsi_host.h>
0079 #include <linux/libata.h>
0080 
0081 
0082 #define DRV_NAME "pata_it821x"
0083 #define DRV_VERSION "0.4.2"
0084 
0085 struct it821x_dev
0086 {
0087     unsigned int smart:1,       /* Are we in smart raid mode */
0088         timing10:1;     /* Rev 0x10 */
0089     u8  clock_mode;     /* 0, ATA_50 or ATA_66 */
0090     u8  want[2][2];     /* Mode/Pri log for master slave */
0091     /* We need these for switching the clock when DMA goes on/off
0092        The high byte is the 66Mhz timing */
0093     u16 pio[2];         /* Cached PIO values */
0094     u16 mwdma[2];       /* Cached MWDMA values */
0095     u16 udma[2];        /* Cached UDMA values (per drive) */
0096     u16 last_device;        /* Master or slave loaded ? */
0097 };
0098 
0099 #define ATA_66      0
0100 #define ATA_50      1
0101 #define ATA_ANY     2
0102 
0103 #define UDMA_OFF    0
0104 #define MWDMA_OFF   0
0105 
0106 /*
0107  *  We allow users to force the card into non raid mode without
0108  *  flashing the alternative BIOS. This is also necessary right now
0109  *  for embedded platforms that cannot run a PC BIOS but are using this
0110  *  device.
0111  */
0112 
0113 static int it8212_noraid;
0114 
0115 /**
0116  *  it821x_program  -   program the PIO/MWDMA registers
0117  *  @ap: ATA port
0118  *  @adev: Device to program
0119  *  @timing: Timing value (66Mhz in top 8bits, 50 in the low 8)
0120  *
0121  *  Program the PIO/MWDMA timing for this channel according to the
0122  *  current clock. These share the same register so are managed by
0123  *  the DMA start/stop sequence as with the old driver.
0124  */
0125 
0126 static void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing)
0127 {
0128     struct pci_dev *pdev = to_pci_dev(ap->host->dev);
0129     struct it821x_dev *itdev = ap->private_data;
0130     int channel = ap->port_no;
0131     u8 conf;
0132 
0133     /* Program PIO/MWDMA timing bits */
0134     if (itdev->clock_mode == ATA_66)
0135         conf = timing >> 8;
0136     else
0137         conf = timing & 0xFF;
0138     pci_write_config_byte(pdev, 0x54 + 4 * channel, conf);
0139 }
0140 
0141 
0142 /**
0143  *  it821x_program_udma -   program the UDMA registers
0144  *  @ap: ATA port
0145  *  @adev: ATA device to update
0146  *  @timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz
0147  *
0148  *  Program the UDMA timing for this drive according to the
0149  *  current clock. Handles the dual clocks and also knows about
0150  *  the errata on the 0x10 revision. The UDMA errata is partly handled
0151  *  here and partly in start_dma.
0152  */
0153 
0154 static void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing)
0155 {
0156     struct it821x_dev *itdev = ap->private_data;
0157     struct pci_dev *pdev = to_pci_dev(ap->host->dev);
0158     int channel = ap->port_no;
0159     int unit = adev->devno;
0160     u8 conf;
0161 
0162     /* Program UDMA timing bits */
0163     if (itdev->clock_mode == ATA_66)
0164         conf = timing >> 8;
0165     else
0166         conf = timing & 0xFF;
0167     if (itdev->timing10 == 0)
0168         pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf);
0169     else {
0170         /* Early revision must be programmed for both together */
0171         pci_write_config_byte(pdev, 0x56 + 4 * channel, conf);
0172         pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf);
0173     }
0174 }
0175 
0176 /**
0177  *  it821x_clock_strategy
0178  *  @ap: ATA interface
0179  *  @adev: ATA device being updated
0180  *
0181  *  Select between the 50 and 66Mhz base clocks to get the best
0182  *  results for this interface.
0183  */
0184 
0185 static void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev)
0186 {
0187     struct pci_dev *pdev = to_pci_dev(ap->host->dev);
0188     struct it821x_dev *itdev = ap->private_data;
0189     u8 unit = adev->devno;
0190     struct ata_device *pair = ata_dev_pair(adev);
0191 
0192     int clock, altclock;
0193     u8 v;
0194     int sel = 0;
0195 
0196     /* Look for the most wanted clocking */
0197     if (itdev->want[0][0] > itdev->want[1][0]) {
0198         clock = itdev->want[0][1];
0199         altclock = itdev->want[1][1];
0200     } else {
0201         clock = itdev->want[1][1];
0202         altclock = itdev->want[0][1];
0203     }
0204 
0205     /* Master doesn't care does the slave ? */
0206     if (clock == ATA_ANY)
0207         clock = altclock;
0208 
0209     /* Nobody cares - keep the same clock */
0210     if (clock == ATA_ANY)
0211         return;
0212     /* No change */
0213     if (clock == itdev->clock_mode)
0214         return;
0215 
0216     /* Load this into the controller */
0217     if (clock == ATA_66)
0218         itdev->clock_mode = ATA_66;
0219     else {
0220         itdev->clock_mode = ATA_50;
0221         sel = 1;
0222     }
0223     pci_read_config_byte(pdev, 0x50, &v);
0224     v &= ~(1 << (1 + ap->port_no));
0225     v |= sel << (1 + ap->port_no);
0226     pci_write_config_byte(pdev, 0x50, v);
0227 
0228     /*
0229      *  Reprogram the UDMA/PIO of the pair drive for the switch
0230      *  MWDMA will be dealt with by the dma switcher
0231      */
0232     if (pair && itdev->udma[1-unit] != UDMA_OFF) {
0233         it821x_program_udma(ap, pair, itdev->udma[1-unit]);
0234         it821x_program(ap, pair, itdev->pio[1-unit]);
0235     }
0236     /*
0237      *  Reprogram the UDMA/PIO of our drive for the switch.
0238      *  MWDMA will be dealt with by the dma switcher
0239      */
0240     if (itdev->udma[unit] != UDMA_OFF) {
0241         it821x_program_udma(ap, adev, itdev->udma[unit]);
0242         it821x_program(ap, adev, itdev->pio[unit]);
0243     }
0244 }
0245 
0246 /**
0247  *  it821x_passthru_set_piomode -   set PIO mode data
0248  *  @ap: ATA interface
0249  *  @adev: ATA device
0250  *
0251  *  Configure for PIO mode. This is complicated as the register is
0252  *  shared by PIO and MWDMA and for both channels.
0253  */
0254 
0255 static void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev)
0256 {
0257     /* Spec says 89 ref driver uses 88 */
0258     static const u16 pio[]  = { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
0259     static const u8 pio_want[]    = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
0260 
0261     struct it821x_dev *itdev = ap->private_data;
0262     int unit = adev->devno;
0263     int mode_wanted = adev->pio_mode - XFER_PIO_0;
0264 
0265     /* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
0266     itdev->want[unit][1] = pio_want[mode_wanted];
0267     itdev->want[unit][0] = 1;   /* PIO is lowest priority */
0268     itdev->pio[unit] = pio[mode_wanted];
0269     it821x_clock_strategy(ap, adev);
0270     it821x_program(ap, adev, itdev->pio[unit]);
0271 }
0272 
0273 /**
0274  *  it821x_passthru_set_dmamode -   set initial DMA mode data
0275  *  @ap: ATA interface
0276  *  @adev: ATA device
0277  *
0278  *  Set up the DMA modes. The actions taken depend heavily on the mode
0279  *  to use. If UDMA is used as is hopefully the usual case then the
0280  *  timing register is private and we need only consider the clock. If
0281  *  we are using MWDMA then we have to manage the setting ourself as
0282  *  we switch devices and mode.
0283  */
0284 
0285 static void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev)
0286 {
0287     static const u16 dma[]  =   { 0x8866, 0x3222, 0x3121 };
0288     static const u8 mwdma_want[] =  { ATA_ANY, ATA_66, ATA_ANY };
0289     static const u16 udma[] =   { 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
0290     static const u8 udma_want[] =   { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
0291 
0292     struct pci_dev *pdev = to_pci_dev(ap->host->dev);
0293     struct it821x_dev *itdev = ap->private_data;
0294     int channel = ap->port_no;
0295     int unit = adev->devno;
0296     u8 conf;
0297 
0298     if (adev->dma_mode >= XFER_UDMA_0) {
0299         int mode_wanted = adev->dma_mode - XFER_UDMA_0;
0300 
0301         itdev->want[unit][1] = udma_want[mode_wanted];
0302         itdev->want[unit][0] = 3;   /* UDMA is high priority */
0303         itdev->mwdma[unit] = MWDMA_OFF;
0304         itdev->udma[unit] = udma[mode_wanted];
0305         if (mode_wanted >= 5)
0306             itdev->udma[unit] |= 0x8080;    /* UDMA 5/6 select on */
0307 
0308         /* UDMA on. Again revision 0x10 must do the pair */
0309         pci_read_config_byte(pdev, 0x50, &conf);
0310         if (itdev->timing10)
0311             conf &= channel ? 0x9F: 0xE7;
0312         else
0313             conf &= ~ (1 << (3 + 2 * channel + unit));
0314         pci_write_config_byte(pdev, 0x50, conf);
0315         it821x_clock_strategy(ap, adev);
0316         it821x_program_udma(ap, adev, itdev->udma[unit]);
0317     } else {
0318         int mode_wanted = adev->dma_mode - XFER_MW_DMA_0;
0319 
0320         itdev->want[unit][1] = mwdma_want[mode_wanted];
0321         itdev->want[unit][0] = 2;   /* MWDMA is low priority */
0322         itdev->mwdma[unit] = dma[mode_wanted];
0323         itdev->udma[unit] = UDMA_OFF;
0324 
0325         /* UDMA bits off - Revision 0x10 do them in pairs */
0326         pci_read_config_byte(pdev, 0x50, &conf);
0327         if (itdev->timing10)
0328             conf |= channel ? 0x60: 0x18;
0329         else
0330             conf |= 1 << (3 + 2 * channel + unit);
0331         pci_write_config_byte(pdev, 0x50, conf);
0332         it821x_clock_strategy(ap, adev);
0333     }
0334 }
0335 
0336 /**
0337  *  it821x_passthru_bmdma_start -   DMA start callback
0338  *  @qc: Command in progress
0339  *
0340  *  Usually drivers set the DMA timing at the point the set_dmamode call
0341  *  is made. IT821x however requires we load new timings on the
0342  *  transitions in some cases.
0343  */
0344 
0345 static void it821x_passthru_bmdma_start(struct ata_queued_cmd *qc)
0346 {
0347     struct ata_port *ap = qc->ap;
0348     struct ata_device *adev = qc->dev;
0349     struct it821x_dev *itdev = ap->private_data;
0350     int unit = adev->devno;
0351 
0352     if (itdev->mwdma[unit] != MWDMA_OFF)
0353         it821x_program(ap, adev, itdev->mwdma[unit]);
0354     else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10)
0355         it821x_program_udma(ap, adev, itdev->udma[unit]);
0356     ata_bmdma_start(qc);
0357 }
0358 
0359 /**
0360  *  it821x_passthru_bmdma_stop  -   DMA stop callback
0361  *  @qc: ATA command
0362  *
0363  *  We loaded new timings in dma_start, as a result we need to restore
0364  *  the PIO timings in dma_stop so that the next command issue gets the
0365  *  right clock values.
0366  */
0367 
0368 static void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc)
0369 {
0370     struct ata_port *ap = qc->ap;
0371     struct ata_device *adev = qc->dev;
0372     struct it821x_dev *itdev = ap->private_data;
0373     int unit = adev->devno;
0374 
0375     ata_bmdma_stop(qc);
0376     if (itdev->mwdma[unit] != MWDMA_OFF)
0377         it821x_program(ap, adev, itdev->pio[unit]);
0378 }
0379 
0380 
0381 /**
0382  *  it821x_passthru_dev_select  -   Select master/slave
0383  *  @ap: ATA port
0384  *  @device: Device number (not pointer)
0385  *
0386  *  Device selection hook. If necessary perform clock switching
0387  */
0388 
0389 static void it821x_passthru_dev_select(struct ata_port *ap,
0390                        unsigned int device)
0391 {
0392     struct it821x_dev *itdev = ap->private_data;
0393     if (itdev && device != itdev->last_device) {
0394         struct ata_device *adev = &ap->link.device[device];
0395         it821x_program(ap, adev, itdev->pio[adev->devno]);
0396         itdev->last_device = device;
0397     }
0398     ata_sff_dev_select(ap, device);
0399 }
0400 
0401 /**
0402  *  it821x_smart_qc_issue       -   wrap qc issue prot
0403  *  @qc: command
0404  *
0405  *  Wrap the command issue sequence for the IT821x. We need to
0406  *  perform out own device selection timing loads before the
0407  *  usual happenings kick off
0408  */
0409 
0410 static unsigned int it821x_smart_qc_issue(struct ata_queued_cmd *qc)
0411 {
0412     switch(qc->tf.command)
0413     {
0414         /* Commands the firmware supports */
0415         case ATA_CMD_READ:
0416         case ATA_CMD_READ_EXT:
0417         case ATA_CMD_WRITE:
0418         case ATA_CMD_WRITE_EXT:
0419         case ATA_CMD_PIO_READ:
0420         case ATA_CMD_PIO_READ_EXT:
0421         case ATA_CMD_PIO_WRITE:
0422         case ATA_CMD_PIO_WRITE_EXT:
0423         case ATA_CMD_READ_MULTI:
0424         case ATA_CMD_READ_MULTI_EXT:
0425         case ATA_CMD_WRITE_MULTI:
0426         case ATA_CMD_WRITE_MULTI_EXT:
0427         case ATA_CMD_ID_ATA:
0428         case ATA_CMD_INIT_DEV_PARAMS:
0429         case 0xFC:  /* Internal 'report rebuild state' */
0430         /* Arguably should just no-op this one */
0431         case ATA_CMD_SET_FEATURES:
0432             return ata_bmdma_qc_issue(qc);
0433     }
0434     ata_dev_dbg(qc->dev, "it821x: can't process command 0x%02X\n",
0435             qc->tf.command);
0436     return AC_ERR_DEV;
0437 }
0438 
0439 /**
0440  *  it821x_passthru_qc_issue    -   wrap qc issue prot
0441  *  @qc: command
0442  *
0443  *  Wrap the command issue sequence for the IT821x. We need to
0444  *  perform out own device selection timing loads before the
0445  *  usual happenings kick off
0446  */
0447 
0448 static unsigned int it821x_passthru_qc_issue(struct ata_queued_cmd *qc)
0449 {
0450     it821x_passthru_dev_select(qc->ap, qc->dev->devno);
0451     return ata_bmdma_qc_issue(qc);
0452 }
0453 
0454 /**
0455  *  it821x_smart_set_mode   -   mode setting
0456  *  @link: interface to set up
0457  *  @unused: device that failed (error only)
0458  *
0459  *  Use a non standard set_mode function. We don't want to be tuned.
0460  *  The BIOS configured everything. Our job is not to fiddle. We
0461  *  read the dma enabled bits from the PCI configuration of the device
0462  *  and respect them.
0463  */
0464 
0465 static int it821x_smart_set_mode(struct ata_link *link, struct ata_device **unused)
0466 {
0467     struct ata_device *dev;
0468 
0469     ata_for_each_dev(dev, link, ENABLED) {
0470         /* We don't really care */
0471         dev->pio_mode = XFER_PIO_0;
0472         dev->dma_mode = XFER_MW_DMA_0;
0473         /* We do need the right mode information for DMA or PIO
0474            and this comes from the current configuration flags */
0475         if (ata_id_has_dma(dev->id)) {
0476             ata_dev_info(dev, "configured for DMA\n");
0477             dev->xfer_mode = XFER_MW_DMA_0;
0478             dev->xfer_shift = ATA_SHIFT_MWDMA;
0479             dev->flags &= ~ATA_DFLAG_PIO;
0480         } else {
0481             ata_dev_info(dev, "configured for PIO\n");
0482             dev->xfer_mode = XFER_PIO_0;
0483             dev->xfer_shift = ATA_SHIFT_PIO;
0484             dev->flags |= ATA_DFLAG_PIO;
0485         }
0486     }
0487     return 0;
0488 }
0489 
0490 /**
0491  *  it821x_dev_config   -   Called each device identify
0492  *  @adev: Device that has just been identified
0493  *
0494  *  Perform the initial setup needed for each device that is chip
0495  *  special. In our case we need to lock the sector count to avoid
0496  *  blowing the brains out of the firmware with large LBA48 requests
0497  *
0498  */
0499 
0500 static void it821x_dev_config(struct ata_device *adev)
0501 {
0502     unsigned char model_num[ATA_ID_PROD_LEN + 1];
0503 
0504     ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
0505 
0506     if (adev->max_sectors > 255)
0507         adev->max_sectors = 255;
0508 
0509     if (strstr(model_num, "Integrated Technology Express")) {
0510         /* RAID mode */
0511         if (adev->id[129] == 1)
0512             ata_dev_info(adev, "%sRAID%d volume\n",
0513                      adev->id[147] ? "Bootable " : "",
0514                      adev->id[129]);
0515         else
0516             ata_dev_info(adev, "%sRAID%d volume (%dK stripe)\n",
0517                      adev->id[147] ? "Bootable " : "",
0518                      adev->id[129], adev->id[146]);
0519     }
0520     /* This is a controller firmware triggered funny, don't
0521        report the drive faulty! */
0522     adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
0523     /* No HPA in 'smart' mode */
0524     adev->horkage |= ATA_HORKAGE_BROKEN_HPA;
0525 }
0526 
0527 /**
0528  *  it821x_read_id  -   Hack identify data up
0529  *  @adev: device to read
0530  *  @tf: proposed taskfile
0531  *  @id: buffer for returned ident data
0532  *
0533  *  Query the devices on this firmware driven port and slightly
0534  *  mash the identify data to stop us and common tools trying to
0535  *  use features not firmware supported. The firmware itself does
0536  *  some masking (eg SMART) but not enough.
0537  */
0538 
0539 static unsigned int it821x_read_id(struct ata_device *adev,
0540                    struct ata_taskfile *tf, __le16 *id)
0541 {
0542     unsigned int err_mask;
0543     unsigned char model_num[ATA_ID_PROD_LEN + 1];
0544 
0545     err_mask = ata_do_dev_read_id(adev, tf, id);
0546     if (err_mask)
0547         return err_mask;
0548     ata_id_c_string((u16 *)id, model_num, ATA_ID_PROD, sizeof(model_num));
0549 
0550     id[83] &= cpu_to_le16(~(1 << 12)); /* Cache flush is firmware handled */
0551     id[84] &= cpu_to_le16(~(1 << 6));  /* No FUA */
0552     id[85] &= cpu_to_le16(~(1 << 10)); /* No HPA */
0553     id[76] = 0;            /* No NCQ/AN etc */
0554 
0555     if (strstr(model_num, "Integrated Technology Express")) {
0556         /* Set feature bits the firmware neglects */
0557         id[49] |= cpu_to_le16(0x0300);  /* LBA, DMA */
0558         id[83] &= cpu_to_le16(0x7FFF);
0559         id[83] |= cpu_to_le16(0x4400);  /* Word 83 is valid and LBA48 */
0560         id[86] |= cpu_to_le16(0x0400);  /* LBA48 on */
0561         id[ATA_ID_MAJOR_VER] |= cpu_to_le16(0x1F);
0562         /* Clear the serial number because it's different each boot
0563            which breaks validation on resume */
0564         memset(&id[ATA_ID_SERNO], 0x20, ATA_ID_SERNO_LEN);
0565     }
0566     return err_mask;
0567 }
0568 
0569 /**
0570  *  it821x_check_atapi_dma  -   ATAPI DMA handler
0571  *  @qc: Command we are about to issue
0572  *
0573  *  Decide if this ATAPI command can be issued by DMA on this
0574  *  controller. Return 0 if it can be.
0575  */
0576 
0577 static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
0578 {
0579     struct ata_port *ap = qc->ap;
0580     struct it821x_dev *itdev = ap->private_data;
0581 
0582     /* Only use dma for transfers to/from the media. */
0583     if (ata_qc_raw_nbytes(qc) < 2048)
0584         return -EOPNOTSUPP;
0585 
0586     /* No ATAPI DMA in smart mode */
0587     if (itdev->smart)
0588         return -EOPNOTSUPP;
0589     /* No ATAPI DMA on rev 10 */
0590     if (itdev->timing10)
0591         return -EOPNOTSUPP;
0592     /* Cool */
0593     return 0;
0594 }
0595 
0596 /**
0597  *  it821x_display_disk -   display disk setup
0598  *  @ap: ATA port
0599  *  @n: Device number
0600  *  @buf: Buffer block from firmware
0601  *
0602  *  Produce a nice informative display of the device setup as provided
0603  *  by the firmware.
0604  */
0605 
0606 static void it821x_display_disk(struct ata_port *ap, int n, u8 *buf)
0607 {
0608     unsigned char id[41];
0609     int mode = 0;
0610     const char *mtype = "";
0611     char mbuf[8];
0612     const char *cbl = "(40 wire cable)";
0613 
0614     static const char *types[5] = {
0615         "RAID0", "RAID1", "RAID 0+1", "JBOD", "DISK"
0616     };
0617 
0618     if (buf[52] > 4)    /* No Disk */
0619         return;
0620 
0621     ata_id_c_string((u16 *)buf, id, 0, 41);
0622 
0623     if (buf[51]) {
0624         mode = ffs(buf[51]);
0625         mtype = "UDMA";
0626     } else if (buf[49]) {
0627         mode = ffs(buf[49]);
0628         mtype = "MWDMA";
0629     }
0630 
0631     if (buf[76])
0632         cbl = "";
0633 
0634     if (mode)
0635         snprintf(mbuf, 8, "%5s%d", mtype, mode - 1);
0636     else
0637         strcpy(mbuf, "PIO");
0638     if (buf[52] == 4)
0639         ata_port_info(ap, "%d: %-6s %-8s          %s %s\n",
0640                 n, mbuf, types[buf[52]], id, cbl);
0641     else
0642         ata_port_info(ap, "%d: %-6s %-8s Volume: %1d %s %s\n",
0643                 n, mbuf, types[buf[52]], buf[53], id, cbl);
0644     if (buf[125] < 100)
0645         ata_port_info(ap, "%d: Rebuilding: %d%%\n", n, buf[125]);
0646 }
0647 
0648 /**
0649  *  it821x_firmware_command     -   issue firmware command
0650  *  @ap: IT821x port to interrogate
0651  *  @cmd: command
0652  *  @len: length
0653  *
0654  *  Issue firmware commands expecting data back from the controller. We
0655  *  use this to issue commands that do not go via the normal paths. Other
0656  *  commands such as 0xFC can be issued normally.
0657  */
0658 
0659 static u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len)
0660 {
0661     u8 status;
0662     int n = 0;
0663     u16 *buf = kmalloc(len, GFP_KERNEL);
0664 
0665     if (!buf)
0666         return NULL;
0667 
0668     /* This isn't quite a normal ATA command as we are talking to the
0669        firmware not the drives */
0670     ap->ctl |= ATA_NIEN;
0671     iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
0672     ata_wait_idle(ap);
0673     iowrite8(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
0674     iowrite8(cmd, ap->ioaddr.command_addr);
0675     udelay(1);
0676     /* This should be almost immediate but a little paranoia goes a long
0677        way. */
0678     while(n++ < 10) {
0679         status = ioread8(ap->ioaddr.status_addr);
0680         if (status & ATA_ERR) {
0681             kfree(buf);
0682             ata_port_err(ap, "%s: rejected\n", __func__);
0683             return NULL;
0684         }
0685         if (status & ATA_DRQ) {
0686             ioread16_rep(ap->ioaddr.data_addr, buf, len/2);
0687             return (u8 *)buf;
0688         }
0689         usleep_range(500, 1000);
0690     }
0691     kfree(buf);
0692     ata_port_err(ap, "%s: timeout\n", __func__);
0693     return NULL;
0694 }
0695 
0696 /**
0697  *  it821x_probe_firmware   -   firmware reporting/setup
0698  *  @ap: IT821x port being probed
0699  *
0700  *  Probe the firmware of the controller by issuing firmware command
0701  *  0xFA and analysing the returned data.
0702  */
0703 
0704 static void it821x_probe_firmware(struct ata_port *ap)
0705 {
0706     u8 *buf;
0707     int i;
0708 
0709     /* This is a bit ugly as we can't just issue a task file to a device
0710        as this is controller magic */
0711 
0712     buf = it821x_firmware_command(ap, 0xFA, 512);
0713 
0714     if (buf != NULL) {
0715         ata_port_info(ap, "pata_it821x: Firmware %02X/%02X/%02X%02X\n",
0716                 buf[505],
0717                 buf[506],
0718                 buf[507],
0719                 buf[508]);
0720         for (i = 0; i < 4; i++)
0721             it821x_display_disk(ap, i, buf + 128 * i);
0722         kfree(buf);
0723     }
0724 }
0725 
0726 
0727 
0728 /**
0729  *  it821x_port_start   -   port setup
0730  *  @ap: ATA port being set up
0731  *
0732  *  The it821x needs to maintain private data structures and also to
0733  *  use the standard PCI interface which lacks support for this
0734  *  functionality. We instead set up the private data on the port
0735  *  start hook, and tear it down on port stop
0736  */
0737 
0738 static int it821x_port_start(struct ata_port *ap)
0739 {
0740     struct pci_dev *pdev = to_pci_dev(ap->host->dev);
0741     struct it821x_dev *itdev;
0742     u8 conf;
0743 
0744     int ret = ata_bmdma_port_start(ap);
0745     if (ret < 0)
0746         return ret;
0747 
0748     itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL);
0749     if (itdev == NULL)
0750         return -ENOMEM;
0751     ap->private_data = itdev;
0752 
0753     pci_read_config_byte(pdev, 0x50, &conf);
0754 
0755     if (conf & 1) {
0756         itdev->smart = 1;
0757         /* Long I/O's although allowed in LBA48 space cause the
0758            onboard firmware to enter the twighlight zone */
0759         /* No ATAPI DMA in this mode either */
0760         if (ap->port_no == 0)
0761             it821x_probe_firmware(ap);
0762     }
0763     /* Pull the current clocks from 0x50 */
0764     if (conf & (1 << (1 + ap->port_no)))
0765         itdev->clock_mode = ATA_50;
0766     else
0767         itdev->clock_mode = ATA_66;
0768 
0769     itdev->want[0][1] = ATA_ANY;
0770     itdev->want[1][1] = ATA_ANY;
0771     itdev->last_device = -1;
0772 
0773     if (pdev->revision == 0x10) {
0774         itdev->timing10 = 1;
0775         /* Need to disable ATAPI DMA for this case */
0776         if (!itdev->smart)
0777             dev_warn(&pdev->dev,
0778                  "Revision 0x10, workarounds activated.\n");
0779     }
0780 
0781     return 0;
0782 }
0783 
0784 /**
0785  *  it821x_rdc_cable    -   Cable detect for RDC1010
0786  *  @ap: port we are checking
0787  *
0788  *  Return the RDC1010 cable type. Unlike the IT821x we know how to do
0789  *  this and can do host side cable detect
0790  */
0791 
0792 static int it821x_rdc_cable(struct ata_port *ap)
0793 {
0794     u16 r40;
0795     struct pci_dev *pdev = to_pci_dev(ap->host->dev);
0796 
0797     pci_read_config_word(pdev, 0x40, &r40);
0798     if (r40 & (1 << (2 + ap->port_no)))
0799         return ATA_CBL_PATA40;
0800     return ATA_CBL_PATA80;
0801 }
0802 
0803 static struct scsi_host_template it821x_sht = {
0804     ATA_BMDMA_SHT(DRV_NAME),
0805 };
0806 
0807 static struct ata_port_operations it821x_smart_port_ops = {
0808     .inherits   = &ata_bmdma_port_ops,
0809 
0810     .check_atapi_dma= it821x_check_atapi_dma,
0811     .qc_issue   = it821x_smart_qc_issue,
0812 
0813     .cable_detect   = ata_cable_80wire,
0814     .set_mode   = it821x_smart_set_mode,
0815     .dev_config = it821x_dev_config,
0816     .read_id    = it821x_read_id,
0817 
0818     .port_start = it821x_port_start,
0819 };
0820 
0821 static struct ata_port_operations it821x_passthru_port_ops = {
0822     .inherits   = &ata_bmdma_port_ops,
0823 
0824     .check_atapi_dma= it821x_check_atapi_dma,
0825     .sff_dev_select = it821x_passthru_dev_select,
0826     .bmdma_start    = it821x_passthru_bmdma_start,
0827     .bmdma_stop = it821x_passthru_bmdma_stop,
0828     .qc_issue   = it821x_passthru_qc_issue,
0829 
0830     .cable_detect   = ata_cable_unknown,
0831     .set_piomode    = it821x_passthru_set_piomode,
0832     .set_dmamode    = it821x_passthru_set_dmamode,
0833 
0834     .port_start = it821x_port_start,
0835 };
0836 
0837 static struct ata_port_operations it821x_rdc_port_ops = {
0838     .inherits   = &ata_bmdma_port_ops,
0839 
0840     .check_atapi_dma= it821x_check_atapi_dma,
0841     .sff_dev_select = it821x_passthru_dev_select,
0842     .bmdma_start    = it821x_passthru_bmdma_start,
0843     .bmdma_stop = it821x_passthru_bmdma_stop,
0844     .qc_issue   = it821x_passthru_qc_issue,
0845 
0846     .cable_detect   = it821x_rdc_cable,
0847     .set_piomode    = it821x_passthru_set_piomode,
0848     .set_dmamode    = it821x_passthru_set_dmamode,
0849 
0850     .port_start = it821x_port_start,
0851 };
0852 
0853 static void it821x_disable_raid(struct pci_dev *pdev)
0854 {
0855     /* Neither the RDC nor the IT8211 */
0856     if (pdev->vendor != PCI_VENDOR_ID_ITE ||
0857             pdev->device != PCI_DEVICE_ID_ITE_8212)
0858             return;
0859 
0860     /* Reset local CPU, and set BIOS not ready */
0861     pci_write_config_byte(pdev, 0x5E, 0x01);
0862 
0863     /* Set to bypass mode, and reset PCI bus */
0864     pci_write_config_byte(pdev, 0x50, 0x00);
0865     pci_write_config_word(pdev, PCI_COMMAND,
0866                   PCI_COMMAND_PARITY | PCI_COMMAND_IO |
0867                   PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
0868     pci_write_config_word(pdev, 0x40, 0xA0F3);
0869 
0870     pci_write_config_dword(pdev,0x4C, 0x02040204);
0871     pci_write_config_byte(pdev, 0x42, 0x36);
0872     pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
0873 }
0874 
0875 
0876 static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
0877 {
0878     u8 conf;
0879 
0880     static const struct ata_port_info info_smart = {
0881         .flags = ATA_FLAG_SLAVE_POSS,
0882         .pio_mask = ATA_PIO4,
0883         .mwdma_mask = ATA_MWDMA2,
0884         .udma_mask = ATA_UDMA6,
0885         .port_ops = &it821x_smart_port_ops
0886     };
0887     static const struct ata_port_info info_passthru = {
0888         .flags = ATA_FLAG_SLAVE_POSS,
0889         .pio_mask = ATA_PIO4,
0890         .mwdma_mask = ATA_MWDMA2,
0891         .udma_mask = ATA_UDMA6,
0892         .port_ops = &it821x_passthru_port_ops
0893     };
0894     static const struct ata_port_info info_rdc = {
0895         .flags = ATA_FLAG_SLAVE_POSS,
0896         .pio_mask = ATA_PIO4,
0897         .mwdma_mask = ATA_MWDMA2,
0898         .udma_mask = ATA_UDMA6,
0899         .port_ops = &it821x_rdc_port_ops
0900     };
0901     static const struct ata_port_info info_rdc_11 = {
0902         .flags = ATA_FLAG_SLAVE_POSS,
0903         .pio_mask = ATA_PIO4,
0904         .mwdma_mask = ATA_MWDMA2,
0905         /* No UDMA */
0906         .port_ops = &it821x_rdc_port_ops
0907     };
0908 
0909     const struct ata_port_info *ppi[] = { NULL, NULL };
0910     static const char *mode[2] = { "pass through", "smart" };
0911     int rc;
0912 
0913     rc = pcim_enable_device(pdev);
0914     if (rc)
0915         return rc;
0916 
0917     if (pdev->vendor == PCI_VENDOR_ID_RDC) {
0918         /* Deal with Vortex86SX */
0919         if (pdev->revision == 0x11)
0920             ppi[0] = &info_rdc_11;
0921         else
0922             ppi[0] = &info_rdc;
0923     } else {
0924         /* Force the card into bypass mode if so requested */
0925         if (it8212_noraid) {
0926             dev_info(&pdev->dev, "forcing bypass mode.\n");
0927             it821x_disable_raid(pdev);
0928         }
0929         pci_read_config_byte(pdev, 0x50, &conf);
0930         conf &= 1;
0931 
0932         dev_info(&pdev->dev, "controller in %s mode.\n", mode[conf]);
0933 
0934         if (conf == 0)
0935             ppi[0] = &info_passthru;
0936         else
0937             ppi[0] = &info_smart;
0938     }
0939     return ata_pci_bmdma_init_one(pdev, ppi, &it821x_sht, NULL, 0);
0940 }
0941 
0942 #ifdef CONFIG_PM_SLEEP
0943 static int it821x_reinit_one(struct pci_dev *pdev)
0944 {
0945     struct ata_host *host = pci_get_drvdata(pdev);
0946     int rc;
0947 
0948     rc = ata_pci_device_do_resume(pdev);
0949     if (rc)
0950         return rc;
0951     /* Resume - turn raid back off if need be */
0952     if (it8212_noraid)
0953         it821x_disable_raid(pdev);
0954     ata_host_resume(host);
0955     return rc;
0956 }
0957 #endif
0958 
0959 static const struct pci_device_id it821x[] = {
0960     { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
0961     { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
0962     { PCI_VDEVICE(RDC, PCI_DEVICE_ID_RDC_D1010), },
0963 
0964     { },
0965 };
0966 
0967 static struct pci_driver it821x_pci_driver = {
0968     .name       = DRV_NAME,
0969     .id_table   = it821x,
0970     .probe      = it821x_init_one,
0971     .remove     = ata_pci_remove_one,
0972 #ifdef CONFIG_PM_SLEEP
0973     .suspend    = ata_pci_device_suspend,
0974     .resume     = it821x_reinit_one,
0975 #endif
0976 };
0977 
0978 module_pci_driver(it821x_pci_driver);
0979 
0980 MODULE_AUTHOR("Alan Cox");
0981 MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
0982 MODULE_LICENSE("GPL");
0983 MODULE_DEVICE_TABLE(pci, it821x);
0984 MODULE_VERSION(DRV_VERSION);
0985 
0986 module_param_named(noraid, it8212_noraid, int, S_IRUGO);
0987 MODULE_PARM_DESC(noraid, "Force card into bypass mode");